Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
7b718769 NS |
2 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
3 | * All Rights Reserved. | |
1da177e4 | 4 | * |
7b718769 NS |
5 | * This program is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU General Public License as | |
1da177e4 LT |
7 | * published by the Free Software Foundation. |
8 | * | |
7b718769 NS |
9 | * This program is distributed in the hope that it would be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
1da177e4 | 13 | * |
7b718769 NS |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write the Free Software Foundation, | |
16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
1da177e4 | 17 | */ |
1da177e4 LT |
18 | #ifndef __XFS_BUF_H__ |
19 | #define __XFS_BUF_H__ | |
20 | ||
1da177e4 LT |
21 | #include <linux/list.h> |
22 | #include <linux/types.h> | |
23 | #include <linux/spinlock.h> | |
1da177e4 LT |
24 | #include <linux/mm.h> |
25 | #include <linux/fs.h> | |
26 | #include <linux/buffer_head.h> | |
27 | #include <linux/uio.h> | |
28 | ||
29 | /* | |
30 | * Base types | |
31 | */ | |
32 | ||
ce8e922c NS |
33 | #define XFS_BUF_DADDR_NULL ((xfs_daddr_t) (-1LL)) |
34 | ||
ce8e922c NS |
35 | typedef enum { |
36 | XBRW_READ = 1, /* transfer into target memory */ | |
37 | XBRW_WRITE = 2, /* transfer from target memory */ | |
38 | XBRW_ZERO = 3, /* Zero target memory */ | |
39 | } xfs_buf_rw_t; | |
40 | ||
6fb8a90a CM |
41 | #define XBF_READ (1 << 0) /* buffer intended for reading from device */ |
42 | #define XBF_WRITE (1 << 1) /* buffer intended for writing to device */ | |
43 | #define XBF_READ_AHEAD (1 << 2) /* asynchronous read-ahead */ | |
44 | #define XBF_ASYNC (1 << 4) /* initiator will not wait for completion */ | |
45 | #define XBF_DONE (1 << 5) /* all pages in the buffer uptodate */ | |
46 | #define XBF_STALE (1 << 6) /* buffer has been staled, do not find it */ | |
1d5ae5df CH |
47 | |
48 | /* I/O hints for the BIO layer */ | |
6fb8a90a CM |
49 | #define XBF_SYNCIO (1 << 10)/* treat this buffer as synchronous I/O */ |
50 | #define XBF_FUA (1 << 11)/* force cache write through mode */ | |
51 | #define XBF_FLUSH (1 << 12)/* flush the disk cache before a write */ | |
1da177e4 | 52 | |
807cbbdb | 53 | /* flags used only as arguments to access routines */ |
6fb8a90a CM |
54 | #define XBF_TRYLOCK (1 << 16)/* lock requested, but do not wait */ |
55 | #define XBF_UNMAPPED (1 << 17)/* do not map the buffer */ | |
1da177e4 | 56 | |
807cbbdb | 57 | /* flags used only internally */ |
6fb8a90a CM |
58 | #define _XBF_PAGES (1 << 20)/* backed by refcounted pages */ |
59 | #define _XBF_KMEM (1 << 21)/* backed by heap memory */ | |
60 | #define _XBF_DELWRI_Q (1 << 22)/* buffer on a delwri queue */ | |
61 | #define _XBF_COMPOUND (1 << 23)/* compound buffer */ | |
62 | #define _XBF_LRU_DISPOSE (1 << 24)/* buffer being discarded */ | |
6ab455ee | 63 | |
807cbbdb | 64 | typedef unsigned int xfs_buf_flags_t; |
1da177e4 | 65 | |
0b1b213f CH |
66 | #define XFS_BUF_FLAGS \ |
67 | { XBF_READ, "READ" }, \ | |
68 | { XBF_WRITE, "WRITE" }, \ | |
1d5ae5df | 69 | { XBF_READ_AHEAD, "READ_AHEAD" }, \ |
0b1b213f CH |
70 | { XBF_ASYNC, "ASYNC" }, \ |
71 | { XBF_DONE, "DONE" }, \ | |
0b1b213f | 72 | { XBF_STALE, "STALE" }, \ |
1d5ae5df CH |
73 | { XBF_SYNCIO, "SYNCIO" }, \ |
74 | { XBF_FUA, "FUA" }, \ | |
75 | { XBF_FLUSH, "FLUSH" }, \ | |
6fb8a90a | 76 | { XBF_TRYLOCK, "TRYLOCK" }, /* should never be set */\ |
611c9946 | 77 | { XBF_UNMAPPED, "UNMAPPED" }, /* ditto */\ |
0b1b213f | 78 | { _XBF_PAGES, "PAGES" }, \ |
0e6e847f | 79 | { _XBF_KMEM, "KMEM" }, \ |
cbb7baab | 80 | { _XBF_DELWRI_Q, "DELWRI_Q" }, \ |
6fb8a90a CM |
81 | { _XBF_COMPOUND, "COMPOUND" }, \ |
82 | { _XBF_LRU_DISPOSE, "LRU_DISPOSE" } | |
0b1b213f | 83 | |
1da177e4 | 84 | typedef struct xfs_buftarg { |
ce8e922c NS |
85 | dev_t bt_dev; |
86 | struct block_device *bt_bdev; | |
0e6e847f | 87 | struct backing_dev_info *bt_bdi; |
ebad861b | 88 | struct xfs_mount *bt_mount; |
ce8e922c NS |
89 | unsigned int bt_bsize; |
90 | unsigned int bt_sshift; | |
91 | size_t bt_smask; | |
92 | ||
ff57ab21 DC |
93 | /* LRU control structures */ |
94 | struct shrinker bt_shrinker; | |
430cbeb8 DC |
95 | struct list_head bt_lru; |
96 | spinlock_t bt_lru_lock; | |
97 | unsigned int bt_lru_nr; | |
1da177e4 LT |
98 | } xfs_buftarg_t; |
99 | ||
1da177e4 | 100 | struct xfs_buf; |
ce8e922c | 101 | typedef void (*xfs_buf_iodone_t)(struct xfs_buf *); |
1da177e4 | 102 | |
c3f8fc73 | 103 | |
ce8e922c | 104 | #define XB_PAGES 2 |
1da177e4 | 105 | |
cbb7baab DC |
106 | struct xfs_buf_map { |
107 | xfs_daddr_t bm_bn; /* block number for I/O */ | |
108 | int bm_len; /* size of I/O */ | |
109 | }; | |
110 | ||
3e85c868 DC |
111 | #define DEFINE_SINGLE_BUF_MAP(map, blkno, numblk) \ |
112 | struct xfs_buf_map (map) = { .bm_bn = (blkno), .bm_len = (numblk) }; | |
113 | ||
1da177e4 | 114 | typedef struct xfs_buf { |
50f59e8e DC |
115 | /* |
116 | * first cacheline holds all the fields needed for an uncontended cache | |
117 | * hit to be fully processed. The semaphore straddles the cacheline | |
118 | * boundary, but the counter and lock sits on the first cacheline, | |
119 | * which is the only bit that is touched if we hit the semaphore | |
120 | * fast-path on locking. | |
121 | */ | |
122 | struct rb_node b_rbnode; /* rbtree node */ | |
cbb7baab | 123 | xfs_daddr_t b_bn; /* block number of buffer */ |
4e94b71b | 124 | int b_length; /* size of buffer in BBs */ |
50f59e8e | 125 | atomic_t b_hold; /* reference count */ |
430cbeb8 | 126 | atomic_t b_lru_ref; /* lru reclaim ref count */ |
50f59e8e | 127 | xfs_buf_flags_t b_flags; /* status flags */ |
ce8e922c | 128 | struct semaphore b_sema; /* semaphore for lockables */ |
50f59e8e | 129 | |
6fb8a90a CM |
130 | /* |
131 | * concurrent access to b_lru and b_lru_flags are protected by | |
132 | * bt_lru_lock and not by b_sema | |
133 | */ | |
430cbeb8 | 134 | struct list_head b_lru; /* lru list */ |
6fb8a90a | 135 | xfs_buf_flags_t b_lru_flags; /* internal lru status flags */ |
ce8e922c NS |
136 | wait_queue_head_t b_waiters; /* unpin waiters */ |
137 | struct list_head b_list; | |
74f75a0c | 138 | struct xfs_perag *b_pag; /* contains rbtree root */ |
ce8e922c | 139 | xfs_buftarg_t *b_target; /* buffer target (device) */ |
ce8e922c NS |
140 | void *b_addr; /* virtual address of buffer */ |
141 | struct work_struct b_iodone_work; | |
ce8e922c | 142 | xfs_buf_iodone_t b_iodone; /* I/O completion function */ |
b4dd330b | 143 | struct completion b_iowait; /* queue for I/O waiters */ |
ce8e922c | 144 | void *b_fspriv; |
bf9d9013 | 145 | struct xfs_trans *b_transp; |
ce8e922c NS |
146 | struct page **b_pages; /* array of page pointers */ |
147 | struct page *b_page_array[XB_PAGES]; /* inline pages */ | |
3e85c868 DC |
148 | struct xfs_buf_map *b_maps; /* compound buffer map */ |
149 | struct xfs_buf_map b_map; /* inline compound buffer map */ | |
150 | int b_map_count; | |
aa0e8833 | 151 | int b_io_length; /* IO size in BBs */ |
50f59e8e DC |
152 | atomic_t b_pin_count; /* pin count */ |
153 | atomic_t b_io_remaining; /* #outstanding I/O requests */ | |
154 | unsigned int b_page_count; /* size of page array */ | |
155 | unsigned int b_offset; /* page offset in first page */ | |
156 | unsigned short b_error; /* error code on I/O */ | |
cbb7baab | 157 | |
ce8e922c NS |
158 | #ifdef XFS_BUF_LOCK_TRACKING |
159 | int b_last_holder; | |
1da177e4 LT |
160 | #endif |
161 | } xfs_buf_t; | |
162 | ||
1da177e4 | 163 | /* Finding and Reading Buffers */ |
3e85c868 DC |
164 | struct xfs_buf *_xfs_buf_find(struct xfs_buftarg *target, |
165 | struct xfs_buf_map *map, int nmaps, | |
166 | xfs_buf_flags_t flags, struct xfs_buf *new_bp); | |
167 | ||
168 | static inline struct xfs_buf * | |
169 | xfs_incore( | |
170 | struct xfs_buftarg *target, | |
171 | xfs_daddr_t blkno, | |
172 | size_t numblks, | |
173 | xfs_buf_flags_t flags) | |
174 | { | |
175 | DEFINE_SINGLE_BUF_MAP(map, blkno, numblks); | |
176 | return _xfs_buf_find(target, &map, 1, flags, NULL); | |
177 | } | |
178 | ||
179 | struct xfs_buf *_xfs_buf_alloc(struct xfs_buftarg *target, | |
180 | struct xfs_buf_map *map, int nmaps, | |
181 | xfs_buf_flags_t flags); | |
182 | ||
183 | static inline struct xfs_buf * | |
184 | xfs_buf_alloc( | |
185 | struct xfs_buftarg *target, | |
186 | xfs_daddr_t blkno, | |
187 | size_t numblks, | |
188 | xfs_buf_flags_t flags) | |
189 | { | |
190 | DEFINE_SINGLE_BUF_MAP(map, blkno, numblks); | |
191 | return _xfs_buf_alloc(target, &map, 1, flags); | |
192 | } | |
1da177e4 | 193 | |
6dde2707 DC |
194 | struct xfs_buf *xfs_buf_get_map(struct xfs_buftarg *target, |
195 | struct xfs_buf_map *map, int nmaps, | |
196 | xfs_buf_flags_t flags); | |
197 | struct xfs_buf *xfs_buf_read_map(struct xfs_buftarg *target, | |
198 | struct xfs_buf_map *map, int nmaps, | |
c3f8fc73 | 199 | xfs_buf_flags_t flags, xfs_buf_iodone_t verify); |
6dde2707 | 200 | void xfs_buf_readahead_map(struct xfs_buftarg *target, |
c3f8fc73 DC |
201 | struct xfs_buf_map *map, int nmaps, |
202 | xfs_buf_iodone_t verify); | |
6dde2707 DC |
203 | |
204 | static inline struct xfs_buf * | |
205 | xfs_buf_get( | |
206 | struct xfs_buftarg *target, | |
207 | xfs_daddr_t blkno, | |
208 | size_t numblks, | |
209 | xfs_buf_flags_t flags) | |
210 | { | |
211 | DEFINE_SINGLE_BUF_MAP(map, blkno, numblks); | |
212 | return xfs_buf_get_map(target, &map, 1, flags); | |
213 | } | |
214 | ||
215 | static inline struct xfs_buf * | |
216 | xfs_buf_read( | |
217 | struct xfs_buftarg *target, | |
218 | xfs_daddr_t blkno, | |
219 | size_t numblks, | |
c3f8fc73 DC |
220 | xfs_buf_flags_t flags, |
221 | xfs_buf_iodone_t verify) | |
6dde2707 DC |
222 | { |
223 | DEFINE_SINGLE_BUF_MAP(map, blkno, numblks); | |
c3f8fc73 | 224 | return xfs_buf_read_map(target, &map, 1, flags, verify); |
6dde2707 DC |
225 | } |
226 | ||
227 | static inline void | |
228 | xfs_buf_readahead( | |
229 | struct xfs_buftarg *target, | |
230 | xfs_daddr_t blkno, | |
c3f8fc73 DC |
231 | size_t numblks, |
232 | xfs_buf_iodone_t verify) | |
6dde2707 DC |
233 | { |
234 | DEFINE_SINGLE_BUF_MAP(map, blkno, numblks); | |
c3f8fc73 | 235 | return xfs_buf_readahead_map(target, &map, 1, verify); |
6dde2707 | 236 | } |
e70b73f8 DC |
237 | |
238 | struct xfs_buf *xfs_buf_get_empty(struct xfs_buftarg *target, size_t numblks); | |
e70b73f8 DC |
239 | void xfs_buf_set_empty(struct xfs_buf *bp, size_t numblks); |
240 | int xfs_buf_associate_memory(struct xfs_buf *bp, void *mem, size_t length); | |
241 | ||
242 | struct xfs_buf *xfs_buf_get_uncached(struct xfs_buftarg *target, size_t numblks, | |
243 | int flags); | |
244 | struct xfs_buf *xfs_buf_read_uncached(struct xfs_buftarg *target, | |
c3f8fc73 DC |
245 | xfs_daddr_t daddr, size_t numblks, int flags, |
246 | xfs_buf_iodone_t verify); | |
e70b73f8 | 247 | void xfs_buf_hold(struct xfs_buf *bp); |
1da177e4 LT |
248 | |
249 | /* Releasing Buffers */ | |
ce8e922c NS |
250 | extern void xfs_buf_free(xfs_buf_t *); |
251 | extern void xfs_buf_rele(xfs_buf_t *); | |
1da177e4 LT |
252 | |
253 | /* Locking and Unlocking Buffers */ | |
0c842ad4 | 254 | extern int xfs_buf_trylock(xfs_buf_t *); |
ce8e922c NS |
255 | extern void xfs_buf_lock(xfs_buf_t *); |
256 | extern void xfs_buf_unlock(xfs_buf_t *); | |
0c842ad4 CH |
257 | #define xfs_buf_islocked(bp) \ |
258 | ((bp)->b_sema.count <= 0) | |
1da177e4 LT |
259 | |
260 | /* Buffer Read and Write Routines */ | |
c2b006c1 | 261 | extern int xfs_bwrite(struct xfs_buf *bp); |
4e23471a CH |
262 | |
263 | extern void xfsbdstrat(struct xfs_mount *, struct xfs_buf *); | |
4e23471a | 264 | |
ce8e922c NS |
265 | extern void xfs_buf_ioend(xfs_buf_t *, int); |
266 | extern void xfs_buf_ioerror(xfs_buf_t *, int); | |
901796af | 267 | extern void xfs_buf_ioerror_alert(struct xfs_buf *, const char *func); |
0e95f19a | 268 | extern void xfs_buf_iorequest(xfs_buf_t *); |
ce8e922c | 269 | extern int xfs_buf_iowait(xfs_buf_t *); |
b9c48649 | 270 | extern void xfs_buf_iomove(xfs_buf_t *, size_t, size_t, void *, |
ce8e922c | 271 | xfs_buf_rw_t); |
1a1a3e97 CH |
272 | #define xfs_buf_zero(bp, off, len) \ |
273 | xfs_buf_iomove((bp), (off), (len), NULL, XBRW_ZERO) | |
ce8e922c | 274 | |
ce8e922c | 275 | static inline int xfs_buf_geterror(xfs_buf_t *bp) |
1da177e4 | 276 | { |
ce8e922c | 277 | return bp ? bp->b_error : ENOMEM; |
1da177e4 LT |
278 | } |
279 | ||
280 | /* Buffer Utility Routines */ | |
ce8e922c | 281 | extern xfs_caddr_t xfs_buf_offset(xfs_buf_t *, size_t); |
1da177e4 | 282 | |
1da177e4 | 283 | /* Delayed Write Buffer Routines */ |
43ff2122 CH |
284 | extern bool xfs_buf_delwri_queue(struct xfs_buf *, struct list_head *); |
285 | extern int xfs_buf_delwri_submit(struct list_head *); | |
286 | extern int xfs_buf_delwri_submit_nowait(struct list_head *); | |
1da177e4 LT |
287 | |
288 | /* Buffer Daemon Setup Routines */ | |
ce8e922c NS |
289 | extern int xfs_buf_init(void); |
290 | extern void xfs_buf_terminate(void); | |
1da177e4 | 291 | |
1d5ae5df | 292 | #define XFS_BUF_ZEROFLAGS(bp) \ |
43ff2122 | 293 | ((bp)->b_flags &= ~(XBF_READ|XBF_WRITE|XBF_ASYNC| \ |
1d5ae5df | 294 | XBF_SYNCIO|XBF_FUA|XBF_FLUSH)) |
ce8e922c | 295 | |
430cbeb8 | 296 | void xfs_buf_stale(struct xfs_buf *bp); |
0cadda1c CH |
297 | #define XFS_BUF_UNSTALE(bp) ((bp)->b_flags &= ~XBF_STALE) |
298 | #define XFS_BUF_ISSTALE(bp) ((bp)->b_flags & XBF_STALE) | |
1da177e4 | 299 | |
ce8e922c NS |
300 | #define XFS_BUF_DONE(bp) ((bp)->b_flags |= XBF_DONE) |
301 | #define XFS_BUF_UNDONE(bp) ((bp)->b_flags &= ~XBF_DONE) | |
302 | #define XFS_BUF_ISDONE(bp) ((bp)->b_flags & XBF_DONE) | |
303 | ||
ce8e922c NS |
304 | #define XFS_BUF_ASYNC(bp) ((bp)->b_flags |= XBF_ASYNC) |
305 | #define XFS_BUF_UNASYNC(bp) ((bp)->b_flags &= ~XBF_ASYNC) | |
306 | #define XFS_BUF_ISASYNC(bp) ((bp)->b_flags & XBF_ASYNC) | |
307 | ||
ce8e922c NS |
308 | #define XFS_BUF_READ(bp) ((bp)->b_flags |= XBF_READ) |
309 | #define XFS_BUF_UNREAD(bp) ((bp)->b_flags &= ~XBF_READ) | |
310 | #define XFS_BUF_ISREAD(bp) ((bp)->b_flags & XBF_READ) | |
311 | ||
312 | #define XFS_BUF_WRITE(bp) ((bp)->b_flags |= XBF_WRITE) | |
313 | #define XFS_BUF_UNWRITE(bp) ((bp)->b_flags &= ~XBF_WRITE) | |
314 | #define XFS_BUF_ISWRITE(bp) ((bp)->b_flags & XBF_WRITE) | |
315 | ||
cbb7baab DC |
316 | /* |
317 | * These macros use the IO block map rather than b_bn. b_bn is now really | |
318 | * just for the buffer cache index for cached buffers. As IO does not use b_bn | |
319 | * anymore, uncached buffers do not use b_bn at all and hence must modify the IO | |
320 | * map directly. Uncached buffers are not allowed to be discontiguous, so this | |
321 | * is safe to do. | |
322 | * | |
323 | * In future, uncached buffers will pass the block number directly to the io | |
324 | * request function and hence these macros will go away at that point. | |
325 | */ | |
326 | #define XFS_BUF_ADDR(bp) ((bp)->b_map.bm_bn) | |
327 | #define XFS_BUF_SET_ADDR(bp, bno) ((bp)->b_map.bm_bn = (xfs_daddr_t)(bno)) | |
ce8e922c | 328 | |
38f23232 | 329 | static inline void xfs_buf_set_ref(struct xfs_buf *bp, int lru_ref) |
821eb21d DC |
330 | { |
331 | atomic_set(&bp->b_lru_ref, lru_ref); | |
332 | } | |
ce8e922c | 333 | |
811e64c7 CS |
334 | static inline int xfs_buf_ispinned(struct xfs_buf *bp) |
335 | { | |
336 | return atomic_read(&bp->b_pin_count); | |
337 | } | |
ce8e922c | 338 | |
ce8e922c | 339 | static inline void xfs_buf_relse(xfs_buf_t *bp) |
1da177e4 | 340 | { |
bfc60177 | 341 | xfs_buf_unlock(bp); |
ce8e922c | 342 | xfs_buf_rele(bp); |
1da177e4 LT |
343 | } |
344 | ||
1da177e4 LT |
345 | /* |
346 | * Handling of buftargs. | |
347 | */ | |
ebad861b DC |
348 | extern xfs_buftarg_t *xfs_alloc_buftarg(struct xfs_mount *, |
349 | struct block_device *, int, const char *); | |
c141b292 | 350 | extern void xfs_free_buftarg(struct xfs_mount *, struct xfs_buftarg *); |
1da177e4 LT |
351 | extern void xfs_wait_buftarg(xfs_buftarg_t *); |
352 | extern int xfs_setsize_buftarg(xfs_buftarg_t *, unsigned int, unsigned int); | |
d808f617 | 353 | |
ce8e922c NS |
354 | #define xfs_getsize_buftarg(buftarg) block_size((buftarg)->bt_bdev) |
355 | #define xfs_readonly_buftarg(buftarg) bdev_read_only((buftarg)->bt_bdev) | |
356 | ||
1da177e4 | 357 | #endif /* __XFS_BUF_H__ */ |