]>
Commit | Line | Data |
---|---|---|
0b61f8a4 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 | 2 | /* |
f07c2250 | 3 | * Copyright (c) 2000-2006 Silicon Graphics, Inc. |
7b718769 | 4 | * All Rights Reserved. |
1da177e4 | 5 | */ |
93c189c1 | 6 | #include "xfs.h" |
1da177e4 LT |
7 | #include <linux/stddef.h> |
8 | #include <linux/errno.h> | |
5a0e3ad6 | 9 | #include <linux/gfp.h> |
1da177e4 LT |
10 | #include <linux/pagemap.h> |
11 | #include <linux/init.h> | |
12 | #include <linux/vmalloc.h> | |
13 | #include <linux/bio.h> | |
14 | #include <linux/sysctl.h> | |
15 | #include <linux/proc_fs.h> | |
16 | #include <linux/workqueue.h> | |
17 | #include <linux/percpu.h> | |
18 | #include <linux/blkdev.h> | |
19 | #include <linux/hash.h> | |
4df08c52 | 20 | #include <linux/kthread.h> |
b20a3503 | 21 | #include <linux/migrate.h> |
3fcfab16 | 22 | #include <linux/backing-dev.h> |
7dfb7103 | 23 | #include <linux/freezer.h> |
1da177e4 | 24 | |
4fb6e8ad | 25 | #include "xfs_format.h" |
239880ef | 26 | #include "xfs_log_format.h" |
7fd36c44 | 27 | #include "xfs_trans_resv.h" |
239880ef | 28 | #include "xfs_sb.h" |
b7963133 | 29 | #include "xfs_mount.h" |
0b1b213f | 30 | #include "xfs_trace.h" |
239880ef | 31 | #include "xfs_log.h" |
e9e899a2 | 32 | #include "xfs_errortag.h" |
7561d27e | 33 | #include "xfs_error.h" |
b7963133 | 34 | |
7989cb8e | 35 | static kmem_zone_t *xfs_buf_zone; |
23ea4032 | 36 | |
ce8e922c NS |
37 | #ifdef XFS_BUF_LOCK_TRACKING |
38 | # define XB_SET_OWNER(bp) ((bp)->b_last_holder = current->pid) | |
39 | # define XB_CLEAR_OWNER(bp) ((bp)->b_last_holder = -1) | |
40 | # define XB_GET_OWNER(bp) ((bp)->b_last_holder) | |
1da177e4 | 41 | #else |
ce8e922c NS |
42 | # define XB_SET_OWNER(bp) do { } while (0) |
43 | # define XB_CLEAR_OWNER(bp) do { } while (0) | |
44 | # define XB_GET_OWNER(bp) do { } while (0) | |
1da177e4 LT |
45 | #endif |
46 | ||
ce8e922c | 47 | #define xb_to_gfp(flags) \ |
aa5c158e | 48 | ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : GFP_NOFS) | __GFP_NOWARN) |
1da177e4 | 49 | |
1da177e4 | 50 | |
73c77e2c JB |
51 | static inline int |
52 | xfs_buf_is_vmapped( | |
53 | struct xfs_buf *bp) | |
54 | { | |
55 | /* | |
56 | * Return true if the buffer is vmapped. | |
57 | * | |
611c9946 DC |
58 | * b_addr is null if the buffer is not mapped, but the code is clever |
59 | * enough to know it doesn't have to map a single page, so the check has | |
60 | * to be both for b_addr and bp->b_page_count > 1. | |
73c77e2c | 61 | */ |
611c9946 | 62 | return bp->b_addr && bp->b_page_count > 1; |
73c77e2c JB |
63 | } |
64 | ||
65 | static inline int | |
66 | xfs_buf_vmap_len( | |
67 | struct xfs_buf *bp) | |
68 | { | |
69 | return (bp->b_page_count * PAGE_SIZE) - bp->b_offset; | |
70 | } | |
71 | ||
9c7504aa BF |
72 | /* |
73 | * Bump the I/O in flight count on the buftarg if we haven't yet done so for | |
74 | * this buffer. The count is incremented once per buffer (per hold cycle) | |
75 | * because the corresponding decrement is deferred to buffer release. Buffers | |
76 | * can undergo I/O multiple times in a hold-release cycle and per buffer I/O | |
77 | * tracking adds unnecessary overhead. This is used for sychronization purposes | |
78 | * with unmount (see xfs_wait_buftarg()), so all we really need is a count of | |
79 | * in-flight buffers. | |
80 | * | |
81 | * Buffers that are never released (e.g., superblock, iclog buffers) must set | |
82 | * the XBF_NO_IOACCT flag before I/O submission. Otherwise, the buftarg count | |
83 | * never reaches zero and unmount hangs indefinitely. | |
84 | */ | |
85 | static inline void | |
86 | xfs_buf_ioacct_inc( | |
87 | struct xfs_buf *bp) | |
88 | { | |
63db7c81 | 89 | if (bp->b_flags & XBF_NO_IOACCT) |
9c7504aa BF |
90 | return; |
91 | ||
92 | ASSERT(bp->b_flags & XBF_ASYNC); | |
63db7c81 BF |
93 | spin_lock(&bp->b_lock); |
94 | if (!(bp->b_state & XFS_BSTATE_IN_FLIGHT)) { | |
95 | bp->b_state |= XFS_BSTATE_IN_FLIGHT; | |
96 | percpu_counter_inc(&bp->b_target->bt_io_count); | |
97 | } | |
98 | spin_unlock(&bp->b_lock); | |
9c7504aa BF |
99 | } |
100 | ||
101 | /* | |
102 | * Clear the in-flight state on a buffer about to be released to the LRU or | |
103 | * freed and unaccount from the buftarg. | |
104 | */ | |
105 | static inline void | |
63db7c81 | 106 | __xfs_buf_ioacct_dec( |
9c7504aa BF |
107 | struct xfs_buf *bp) |
108 | { | |
95989c46 | 109 | lockdep_assert_held(&bp->b_lock); |
9c7504aa | 110 | |
63db7c81 BF |
111 | if (bp->b_state & XFS_BSTATE_IN_FLIGHT) { |
112 | bp->b_state &= ~XFS_BSTATE_IN_FLIGHT; | |
113 | percpu_counter_dec(&bp->b_target->bt_io_count); | |
114 | } | |
115 | } | |
116 | ||
117 | static inline void | |
118 | xfs_buf_ioacct_dec( | |
119 | struct xfs_buf *bp) | |
120 | { | |
121 | spin_lock(&bp->b_lock); | |
122 | __xfs_buf_ioacct_dec(bp); | |
123 | spin_unlock(&bp->b_lock); | |
9c7504aa BF |
124 | } |
125 | ||
430cbeb8 DC |
126 | /* |
127 | * When we mark a buffer stale, we remove the buffer from the LRU and clear the | |
128 | * b_lru_ref count so that the buffer is freed immediately when the buffer | |
129 | * reference count falls to zero. If the buffer is already on the LRU, we need | |
130 | * to remove the reference that LRU holds on the buffer. | |
131 | * | |
132 | * This prevents build-up of stale buffers on the LRU. | |
133 | */ | |
134 | void | |
135 | xfs_buf_stale( | |
136 | struct xfs_buf *bp) | |
137 | { | |
43ff2122 CH |
138 | ASSERT(xfs_buf_islocked(bp)); |
139 | ||
430cbeb8 | 140 | bp->b_flags |= XBF_STALE; |
43ff2122 CH |
141 | |
142 | /* | |
143 | * Clear the delwri status so that a delwri queue walker will not | |
144 | * flush this buffer to disk now that it is stale. The delwri queue has | |
145 | * a reference to the buffer, so this is safe to do. | |
146 | */ | |
147 | bp->b_flags &= ~_XBF_DELWRI_Q; | |
148 | ||
9c7504aa BF |
149 | /* |
150 | * Once the buffer is marked stale and unlocked, a subsequent lookup | |
151 | * could reset b_flags. There is no guarantee that the buffer is | |
152 | * unaccounted (released to LRU) before that occurs. Drop in-flight | |
153 | * status now to preserve accounting consistency. | |
154 | */ | |
a4082357 | 155 | spin_lock(&bp->b_lock); |
63db7c81 BF |
156 | __xfs_buf_ioacct_dec(bp); |
157 | ||
a4082357 DC |
158 | atomic_set(&bp->b_lru_ref, 0); |
159 | if (!(bp->b_state & XFS_BSTATE_DISPOSE) && | |
e80dfa19 DC |
160 | (list_lru_del(&bp->b_target->bt_lru, &bp->b_lru))) |
161 | atomic_dec(&bp->b_hold); | |
162 | ||
430cbeb8 | 163 | ASSERT(atomic_read(&bp->b_hold) >= 1); |
a4082357 | 164 | spin_unlock(&bp->b_lock); |
430cbeb8 | 165 | } |
1da177e4 | 166 | |
3e85c868 DC |
167 | static int |
168 | xfs_buf_get_maps( | |
169 | struct xfs_buf *bp, | |
170 | int map_count) | |
171 | { | |
172 | ASSERT(bp->b_maps == NULL); | |
173 | bp->b_map_count = map_count; | |
174 | ||
175 | if (map_count == 1) { | |
f4b42421 | 176 | bp->b_maps = &bp->__b_map; |
3e85c868 DC |
177 | return 0; |
178 | } | |
179 | ||
180 | bp->b_maps = kmem_zalloc(map_count * sizeof(struct xfs_buf_map), | |
181 | KM_NOFS); | |
182 | if (!bp->b_maps) | |
2451337d | 183 | return -ENOMEM; |
3e85c868 DC |
184 | return 0; |
185 | } | |
186 | ||
187 | /* | |
188 | * Frees b_pages if it was allocated. | |
189 | */ | |
190 | static void | |
191 | xfs_buf_free_maps( | |
192 | struct xfs_buf *bp) | |
193 | { | |
f4b42421 | 194 | if (bp->b_maps != &bp->__b_map) { |
3e85c868 DC |
195 | kmem_free(bp->b_maps); |
196 | bp->b_maps = NULL; | |
197 | } | |
198 | } | |
199 | ||
4347b9d7 | 200 | struct xfs_buf * |
3e85c868 | 201 | _xfs_buf_alloc( |
4347b9d7 | 202 | struct xfs_buftarg *target, |
3e85c868 DC |
203 | struct xfs_buf_map *map, |
204 | int nmaps, | |
ce8e922c | 205 | xfs_buf_flags_t flags) |
1da177e4 | 206 | { |
4347b9d7 | 207 | struct xfs_buf *bp; |
3e85c868 DC |
208 | int error; |
209 | int i; | |
4347b9d7 | 210 | |
aa5c158e | 211 | bp = kmem_zone_zalloc(xfs_buf_zone, KM_NOFS); |
4347b9d7 CH |
212 | if (unlikely(!bp)) |
213 | return NULL; | |
214 | ||
1da177e4 | 215 | /* |
12bcb3f7 DC |
216 | * We don't want certain flags to appear in b_flags unless they are |
217 | * specifically set by later operations on the buffer. | |
1da177e4 | 218 | */ |
611c9946 | 219 | flags &= ~(XBF_UNMAPPED | XBF_TRYLOCK | XBF_ASYNC | XBF_READ_AHEAD); |
ce8e922c | 220 | |
ce8e922c | 221 | atomic_set(&bp->b_hold, 1); |
430cbeb8 | 222 | atomic_set(&bp->b_lru_ref, 1); |
b4dd330b | 223 | init_completion(&bp->b_iowait); |
430cbeb8 | 224 | INIT_LIST_HEAD(&bp->b_lru); |
ce8e922c | 225 | INIT_LIST_HEAD(&bp->b_list); |
643c8c05 | 226 | INIT_LIST_HEAD(&bp->b_li_list); |
a731cd11 | 227 | sema_init(&bp->b_sema, 0); /* held, no waiters */ |
a4082357 | 228 | spin_lock_init(&bp->b_lock); |
ce8e922c NS |
229 | XB_SET_OWNER(bp); |
230 | bp->b_target = target; | |
3e85c868 | 231 | bp->b_flags = flags; |
de1cbee4 | 232 | |
1da177e4 | 233 | /* |
aa0e8833 DC |
234 | * Set length and io_length to the same value initially. |
235 | * I/O routines should use io_length, which will be the same in | |
1da177e4 LT |
236 | * most cases but may be reset (e.g. XFS recovery). |
237 | */ | |
3e85c868 DC |
238 | error = xfs_buf_get_maps(bp, nmaps); |
239 | if (error) { | |
240 | kmem_zone_free(xfs_buf_zone, bp); | |
241 | return NULL; | |
242 | } | |
243 | ||
244 | bp->b_bn = map[0].bm_bn; | |
245 | bp->b_length = 0; | |
246 | for (i = 0; i < nmaps; i++) { | |
247 | bp->b_maps[i].bm_bn = map[i].bm_bn; | |
248 | bp->b_maps[i].bm_len = map[i].bm_len; | |
249 | bp->b_length += map[i].bm_len; | |
250 | } | |
251 | bp->b_io_length = bp->b_length; | |
252 | ||
ce8e922c NS |
253 | atomic_set(&bp->b_pin_count, 0); |
254 | init_waitqueue_head(&bp->b_waiters); | |
255 | ||
ff6d6af2 | 256 | XFS_STATS_INC(target->bt_mount, xb_create); |
0b1b213f | 257 | trace_xfs_buf_init(bp, _RET_IP_); |
4347b9d7 CH |
258 | |
259 | return bp; | |
1da177e4 LT |
260 | } |
261 | ||
262 | /* | |
ce8e922c NS |
263 | * Allocate a page array capable of holding a specified number |
264 | * of pages, and point the page buf at it. | |
1da177e4 LT |
265 | */ |
266 | STATIC int | |
ce8e922c NS |
267 | _xfs_buf_get_pages( |
268 | xfs_buf_t *bp, | |
87937bf8 | 269 | int page_count) |
1da177e4 LT |
270 | { |
271 | /* Make sure that we have a page list */ | |
ce8e922c | 272 | if (bp->b_pages == NULL) { |
ce8e922c NS |
273 | bp->b_page_count = page_count; |
274 | if (page_count <= XB_PAGES) { | |
275 | bp->b_pages = bp->b_page_array; | |
1da177e4 | 276 | } else { |
ce8e922c | 277 | bp->b_pages = kmem_alloc(sizeof(struct page *) * |
aa5c158e | 278 | page_count, KM_NOFS); |
ce8e922c | 279 | if (bp->b_pages == NULL) |
1da177e4 LT |
280 | return -ENOMEM; |
281 | } | |
ce8e922c | 282 | memset(bp->b_pages, 0, sizeof(struct page *) * page_count); |
1da177e4 LT |
283 | } |
284 | return 0; | |
285 | } | |
286 | ||
287 | /* | |
ce8e922c | 288 | * Frees b_pages if it was allocated. |
1da177e4 LT |
289 | */ |
290 | STATIC void | |
ce8e922c | 291 | _xfs_buf_free_pages( |
1da177e4 LT |
292 | xfs_buf_t *bp) |
293 | { | |
ce8e922c | 294 | if (bp->b_pages != bp->b_page_array) { |
f0e2d93c | 295 | kmem_free(bp->b_pages); |
3fc98b1a | 296 | bp->b_pages = NULL; |
1da177e4 LT |
297 | } |
298 | } | |
299 | ||
300 | /* | |
301 | * Releases the specified buffer. | |
302 | * | |
303 | * The modification state of any associated pages is left unchanged. | |
b46fe825 | 304 | * The buffer must not be on any hash - use xfs_buf_rele instead for |
1da177e4 LT |
305 | * hashed and refcounted buffers |
306 | */ | |
307 | void | |
ce8e922c | 308 | xfs_buf_free( |
1da177e4 LT |
309 | xfs_buf_t *bp) |
310 | { | |
0b1b213f | 311 | trace_xfs_buf_free(bp, _RET_IP_); |
1da177e4 | 312 | |
430cbeb8 DC |
313 | ASSERT(list_empty(&bp->b_lru)); |
314 | ||
0e6e847f | 315 | if (bp->b_flags & _XBF_PAGES) { |
1da177e4 LT |
316 | uint i; |
317 | ||
73c77e2c | 318 | if (xfs_buf_is_vmapped(bp)) |
8a262e57 AE |
319 | vm_unmap_ram(bp->b_addr - bp->b_offset, |
320 | bp->b_page_count); | |
1da177e4 | 321 | |
948ecdb4 NS |
322 | for (i = 0; i < bp->b_page_count; i++) { |
323 | struct page *page = bp->b_pages[i]; | |
324 | ||
0e6e847f | 325 | __free_page(page); |
948ecdb4 | 326 | } |
0e6e847f DC |
327 | } else if (bp->b_flags & _XBF_KMEM) |
328 | kmem_free(bp->b_addr); | |
3fc98b1a | 329 | _xfs_buf_free_pages(bp); |
3e85c868 | 330 | xfs_buf_free_maps(bp); |
4347b9d7 | 331 | kmem_zone_free(xfs_buf_zone, bp); |
1da177e4 LT |
332 | } |
333 | ||
334 | /* | |
0e6e847f | 335 | * Allocates all the pages for buffer in question and builds it's page list. |
1da177e4 LT |
336 | */ |
337 | STATIC int | |
0e6e847f | 338 | xfs_buf_allocate_memory( |
1da177e4 LT |
339 | xfs_buf_t *bp, |
340 | uint flags) | |
341 | { | |
aa0e8833 | 342 | size_t size; |
1da177e4 | 343 | size_t nbytes, offset; |
ce8e922c | 344 | gfp_t gfp_mask = xb_to_gfp(flags); |
1da177e4 | 345 | unsigned short page_count, i; |
795cac72 | 346 | xfs_off_t start, end; |
1da177e4 LT |
347 | int error; |
348 | ||
0e6e847f DC |
349 | /* |
350 | * for buffers that are contained within a single page, just allocate | |
351 | * the memory from the heap - there's no need for the complexity of | |
352 | * page arrays to keep allocation down to order 0. | |
353 | */ | |
795cac72 DC |
354 | size = BBTOB(bp->b_length); |
355 | if (size < PAGE_SIZE) { | |
aa5c158e | 356 | bp->b_addr = kmem_alloc(size, KM_NOFS); |
0e6e847f DC |
357 | if (!bp->b_addr) { |
358 | /* low memory - use alloc_page loop instead */ | |
359 | goto use_alloc_page; | |
360 | } | |
361 | ||
795cac72 | 362 | if (((unsigned long)(bp->b_addr + size - 1) & PAGE_MASK) != |
0e6e847f DC |
363 | ((unsigned long)bp->b_addr & PAGE_MASK)) { |
364 | /* b_addr spans two pages - use alloc_page instead */ | |
365 | kmem_free(bp->b_addr); | |
366 | bp->b_addr = NULL; | |
367 | goto use_alloc_page; | |
368 | } | |
369 | bp->b_offset = offset_in_page(bp->b_addr); | |
370 | bp->b_pages = bp->b_page_array; | |
371 | bp->b_pages[0] = virt_to_page(bp->b_addr); | |
372 | bp->b_page_count = 1; | |
611c9946 | 373 | bp->b_flags |= _XBF_KMEM; |
0e6e847f DC |
374 | return 0; |
375 | } | |
376 | ||
377 | use_alloc_page: | |
f4b42421 MT |
378 | start = BBTOB(bp->b_maps[0].bm_bn) >> PAGE_SHIFT; |
379 | end = (BBTOB(bp->b_maps[0].bm_bn + bp->b_length) + PAGE_SIZE - 1) | |
cbb7baab | 380 | >> PAGE_SHIFT; |
795cac72 | 381 | page_count = end - start; |
87937bf8 | 382 | error = _xfs_buf_get_pages(bp, page_count); |
1da177e4 LT |
383 | if (unlikely(error)) |
384 | return error; | |
1da177e4 | 385 | |
ce8e922c | 386 | offset = bp->b_offset; |
0e6e847f | 387 | bp->b_flags |= _XBF_PAGES; |
1da177e4 | 388 | |
ce8e922c | 389 | for (i = 0; i < bp->b_page_count; i++) { |
1da177e4 LT |
390 | struct page *page; |
391 | uint retries = 0; | |
0e6e847f DC |
392 | retry: |
393 | page = alloc_page(gfp_mask); | |
1da177e4 | 394 | if (unlikely(page == NULL)) { |
ce8e922c NS |
395 | if (flags & XBF_READ_AHEAD) { |
396 | bp->b_page_count = i; | |
2451337d | 397 | error = -ENOMEM; |
0e6e847f | 398 | goto out_free_pages; |
1da177e4 LT |
399 | } |
400 | ||
401 | /* | |
402 | * This could deadlock. | |
403 | * | |
404 | * But until all the XFS lowlevel code is revamped to | |
405 | * handle buffer allocation failures we can't do much. | |
406 | */ | |
407 | if (!(++retries % 100)) | |
4f10700a | 408 | xfs_err(NULL, |
5bf97b1c TH |
409 | "%s(%u) possible memory allocation deadlock in %s (mode:0x%x)", |
410 | current->comm, current->pid, | |
34a622b2 | 411 | __func__, gfp_mask); |
1da177e4 | 412 | |
ff6d6af2 | 413 | XFS_STATS_INC(bp->b_target->bt_mount, xb_page_retries); |
8aa7e847 | 414 | congestion_wait(BLK_RW_ASYNC, HZ/50); |
1da177e4 LT |
415 | goto retry; |
416 | } | |
417 | ||
ff6d6af2 | 418 | XFS_STATS_INC(bp->b_target->bt_mount, xb_page_found); |
1da177e4 | 419 | |
0e6e847f | 420 | nbytes = min_t(size_t, size, PAGE_SIZE - offset); |
1da177e4 | 421 | size -= nbytes; |
ce8e922c | 422 | bp->b_pages[i] = page; |
1da177e4 LT |
423 | offset = 0; |
424 | } | |
0e6e847f | 425 | return 0; |
1da177e4 | 426 | |
0e6e847f DC |
427 | out_free_pages: |
428 | for (i = 0; i < bp->b_page_count; i++) | |
429 | __free_page(bp->b_pages[i]); | |
2aa6ba7b | 430 | bp->b_flags &= ~_XBF_PAGES; |
1da177e4 LT |
431 | return error; |
432 | } | |
433 | ||
434 | /* | |
25985edc | 435 | * Map buffer into kernel address-space if necessary. |
1da177e4 LT |
436 | */ |
437 | STATIC int | |
ce8e922c | 438 | _xfs_buf_map_pages( |
1da177e4 LT |
439 | xfs_buf_t *bp, |
440 | uint flags) | |
441 | { | |
0e6e847f | 442 | ASSERT(bp->b_flags & _XBF_PAGES); |
ce8e922c | 443 | if (bp->b_page_count == 1) { |
0e6e847f | 444 | /* A single page buffer is always mappable */ |
ce8e922c | 445 | bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset; |
611c9946 DC |
446 | } else if (flags & XBF_UNMAPPED) { |
447 | bp->b_addr = NULL; | |
448 | } else { | |
a19fb380 | 449 | int retried = 0; |
9ba1fb2c | 450 | unsigned nofs_flag; |
ae687e58 DC |
451 | |
452 | /* | |
453 | * vm_map_ram() will allocate auxillary structures (e.g. | |
454 | * pagetables) with GFP_KERNEL, yet we are likely to be under | |
455 | * GFP_NOFS context here. Hence we need to tell memory reclaim | |
9ba1fb2c | 456 | * that we are in such a context via PF_MEMALLOC_NOFS to prevent |
ae687e58 DC |
457 | * memory reclaim re-entering the filesystem here and |
458 | * potentially deadlocking. | |
459 | */ | |
9ba1fb2c | 460 | nofs_flag = memalloc_nofs_save(); |
a19fb380 DC |
461 | do { |
462 | bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count, | |
463 | -1, PAGE_KERNEL); | |
464 | if (bp->b_addr) | |
465 | break; | |
466 | vm_unmap_aliases(); | |
467 | } while (retried++ <= 1); | |
9ba1fb2c | 468 | memalloc_nofs_restore(nofs_flag); |
a19fb380 DC |
469 | |
470 | if (!bp->b_addr) | |
1da177e4 | 471 | return -ENOMEM; |
ce8e922c | 472 | bp->b_addr += bp->b_offset; |
1da177e4 LT |
473 | } |
474 | ||
475 | return 0; | |
476 | } | |
477 | ||
478 | /* | |
479 | * Finding and Reading Buffers | |
480 | */ | |
6031e73a LS |
481 | static int |
482 | _xfs_buf_obj_cmp( | |
483 | struct rhashtable_compare_arg *arg, | |
484 | const void *obj) | |
485 | { | |
486 | const struct xfs_buf_map *map = arg->key; | |
487 | const struct xfs_buf *bp = obj; | |
488 | ||
489 | /* | |
490 | * The key hashing in the lookup path depends on the key being the | |
491 | * first element of the compare_arg, make sure to assert this. | |
492 | */ | |
493 | BUILD_BUG_ON(offsetof(struct xfs_buf_map, bm_bn) != 0); | |
494 | ||
495 | if (bp->b_bn != map->bm_bn) | |
496 | return 1; | |
497 | ||
498 | if (unlikely(bp->b_length != map->bm_len)) { | |
499 | /* | |
500 | * found a block number match. If the range doesn't | |
501 | * match, the only way this is allowed is if the buffer | |
502 | * in the cache is stale and the transaction that made | |
503 | * it stale has not yet committed. i.e. we are | |
504 | * reallocating a busy extent. Skip this buffer and | |
505 | * continue searching for an exact match. | |
506 | */ | |
507 | ASSERT(bp->b_flags & XBF_STALE); | |
508 | return 1; | |
509 | } | |
510 | return 0; | |
511 | } | |
512 | ||
513 | static const struct rhashtable_params xfs_buf_hash_params = { | |
514 | .min_size = 32, /* empty AGs have minimal footprint */ | |
515 | .nelem_hint = 16, | |
516 | .key_len = sizeof(xfs_daddr_t), | |
517 | .key_offset = offsetof(struct xfs_buf, b_bn), | |
518 | .head_offset = offsetof(struct xfs_buf, b_rhash_head), | |
519 | .automatic_shrinking = true, | |
520 | .obj_cmpfn = _xfs_buf_obj_cmp, | |
521 | }; | |
522 | ||
523 | int | |
524 | xfs_buf_hash_init( | |
525 | struct xfs_perag *pag) | |
526 | { | |
527 | spin_lock_init(&pag->pag_buf_lock); | |
528 | return rhashtable_init(&pag->pag_buf_hash, &xfs_buf_hash_params); | |
529 | } | |
530 | ||
531 | void | |
532 | xfs_buf_hash_destroy( | |
533 | struct xfs_perag *pag) | |
534 | { | |
535 | rhashtable_destroy(&pag->pag_buf_hash); | |
536 | } | |
1da177e4 LT |
537 | |
538 | /* | |
b027d4c9 DC |
539 | * Look up a buffer in the buffer cache and return it referenced and locked |
540 | * in @found_bp. | |
541 | * | |
542 | * If @new_bp is supplied and we have a lookup miss, insert @new_bp into the | |
543 | * cache. | |
544 | * | |
545 | * If XBF_TRYLOCK is set in @flags, only try to lock the buffer and return | |
546 | * -EAGAIN if we fail to lock it. | |
547 | * | |
548 | * Return values are: | |
549 | * -EFSCORRUPTED if have been supplied with an invalid address | |
550 | * -EAGAIN on trylock failure | |
551 | * -ENOENT if we fail to find a match and @new_bp was NULL | |
552 | * 0, with @found_bp: | |
553 | * - @new_bp if we inserted it into the cache | |
554 | * - the buffer we found and locked. | |
1da177e4 | 555 | */ |
b027d4c9 DC |
556 | static int |
557 | xfs_buf_find( | |
e70b73f8 | 558 | struct xfs_buftarg *btp, |
3e85c868 DC |
559 | struct xfs_buf_map *map, |
560 | int nmaps, | |
ce8e922c | 561 | xfs_buf_flags_t flags, |
b027d4c9 DC |
562 | struct xfs_buf *new_bp, |
563 | struct xfs_buf **found_bp) | |
1da177e4 | 564 | { |
74f75a0c | 565 | struct xfs_perag *pag; |
74f75a0c | 566 | xfs_buf_t *bp; |
6031e73a | 567 | struct xfs_buf_map cmap = { .bm_bn = map[0].bm_bn }; |
10616b80 | 568 | xfs_daddr_t eofs; |
3e85c868 | 569 | int i; |
1da177e4 | 570 | |
b027d4c9 DC |
571 | *found_bp = NULL; |
572 | ||
3e85c868 | 573 | for (i = 0; i < nmaps; i++) |
6031e73a | 574 | cmap.bm_len += map[i].bm_len; |
1da177e4 LT |
575 | |
576 | /* Check for IOs smaller than the sector size / not sector aligned */ | |
6031e73a LS |
577 | ASSERT(!(BBTOB(cmap.bm_len) < btp->bt_meta_sectorsize)); |
578 | ASSERT(!(BBTOB(cmap.bm_bn) & (xfs_off_t)btp->bt_meta_sectormask)); | |
1da177e4 | 579 | |
10616b80 DC |
580 | /* |
581 | * Corrupted block numbers can get through to here, unfortunately, so we | |
582 | * have to check that the buffer falls within the filesystem bounds. | |
583 | */ | |
584 | eofs = XFS_FSB_TO_BB(btp->bt_mount, btp->bt_mount->m_sb.sb_dblocks); | |
6031e73a | 585 | if (cmap.bm_bn < 0 || cmap.bm_bn >= eofs) { |
10616b80 | 586 | xfs_alert(btp->bt_mount, |
c219b015 | 587 | "%s: daddr 0x%llx out of range, EOFS 0x%llx", |
6031e73a | 588 | __func__, cmap.bm_bn, eofs); |
7bc0dc27 | 589 | WARN_ON(1); |
b027d4c9 | 590 | return -EFSCORRUPTED; |
10616b80 DC |
591 | } |
592 | ||
74f75a0c | 593 | pag = xfs_perag_get(btp->bt_mount, |
6031e73a | 594 | xfs_daddr_to_agno(btp->bt_mount, cmap.bm_bn)); |
74f75a0c | 595 | |
74f75a0c | 596 | spin_lock(&pag->pag_buf_lock); |
6031e73a LS |
597 | bp = rhashtable_lookup_fast(&pag->pag_buf_hash, &cmap, |
598 | xfs_buf_hash_params); | |
599 | if (bp) { | |
600 | atomic_inc(&bp->b_hold); | |
601 | goto found; | |
1da177e4 LT |
602 | } |
603 | ||
604 | /* No match found */ | |
b027d4c9 | 605 | if (!new_bp) { |
ff6d6af2 | 606 | XFS_STATS_INC(btp->bt_mount, xb_miss_locked); |
74f75a0c DC |
607 | spin_unlock(&pag->pag_buf_lock); |
608 | xfs_perag_put(pag); | |
b027d4c9 | 609 | return -ENOENT; |
1da177e4 | 610 | } |
b027d4c9 DC |
611 | |
612 | /* the buffer keeps the perag reference until it is freed */ | |
613 | new_bp->b_pag = pag; | |
614 | rhashtable_insert_fast(&pag->pag_buf_hash, &new_bp->b_rhash_head, | |
615 | xfs_buf_hash_params); | |
616 | spin_unlock(&pag->pag_buf_lock); | |
617 | *found_bp = new_bp; | |
618 | return 0; | |
1da177e4 LT |
619 | |
620 | found: | |
74f75a0c DC |
621 | spin_unlock(&pag->pag_buf_lock); |
622 | xfs_perag_put(pag); | |
1da177e4 | 623 | |
0c842ad4 CH |
624 | if (!xfs_buf_trylock(bp)) { |
625 | if (flags & XBF_TRYLOCK) { | |
ce8e922c | 626 | xfs_buf_rele(bp); |
ff6d6af2 | 627 | XFS_STATS_INC(btp->bt_mount, xb_busy_locked); |
b027d4c9 | 628 | return -EAGAIN; |
1da177e4 | 629 | } |
0c842ad4 | 630 | xfs_buf_lock(bp); |
ff6d6af2 | 631 | XFS_STATS_INC(btp->bt_mount, xb_get_locked_waited); |
1da177e4 LT |
632 | } |
633 | ||
0e6e847f DC |
634 | /* |
635 | * if the buffer is stale, clear all the external state associated with | |
636 | * it. We need to keep flags such as how we allocated the buffer memory | |
637 | * intact here. | |
638 | */ | |
ce8e922c NS |
639 | if (bp->b_flags & XBF_STALE) { |
640 | ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0); | |
cfb02852 | 641 | ASSERT(bp->b_iodone == NULL); |
611c9946 | 642 | bp->b_flags &= _XBF_KMEM | _XBF_PAGES; |
1813dd64 | 643 | bp->b_ops = NULL; |
2f926587 | 644 | } |
0b1b213f CH |
645 | |
646 | trace_xfs_buf_find(bp, flags, _RET_IP_); | |
ff6d6af2 | 647 | XFS_STATS_INC(btp->bt_mount, xb_get_locked); |
b027d4c9 DC |
648 | *found_bp = bp; |
649 | return 0; | |
1da177e4 LT |
650 | } |
651 | ||
8925a3dc DC |
652 | struct xfs_buf * |
653 | xfs_buf_incore( | |
654 | struct xfs_buftarg *target, | |
655 | xfs_daddr_t blkno, | |
656 | size_t numblks, | |
657 | xfs_buf_flags_t flags) | |
658 | { | |
b027d4c9 DC |
659 | struct xfs_buf *bp; |
660 | int error; | |
8925a3dc | 661 | DEFINE_SINGLE_BUF_MAP(map, blkno, numblks); |
b027d4c9 DC |
662 | |
663 | error = xfs_buf_find(target, &map, 1, flags, NULL, &bp); | |
664 | if (error) | |
665 | return NULL; | |
666 | return bp; | |
8925a3dc DC |
667 | } |
668 | ||
1da177e4 | 669 | /* |
3815832a DC |
670 | * Assembles a buffer covering the specified range. The code is optimised for |
671 | * cache hits, as metadata intensive workloads will see 3 orders of magnitude | |
672 | * more hits than misses. | |
1da177e4 | 673 | */ |
3815832a | 674 | struct xfs_buf * |
6dde2707 DC |
675 | xfs_buf_get_map( |
676 | struct xfs_buftarg *target, | |
677 | struct xfs_buf_map *map, | |
678 | int nmaps, | |
ce8e922c | 679 | xfs_buf_flags_t flags) |
1da177e4 | 680 | { |
3815832a DC |
681 | struct xfs_buf *bp; |
682 | struct xfs_buf *new_bp; | |
0e6e847f | 683 | int error = 0; |
1da177e4 | 684 | |
b027d4c9 DC |
685 | error = xfs_buf_find(target, map, nmaps, flags, NULL, &bp); |
686 | ||
687 | switch (error) { | |
688 | case 0: | |
689 | /* cache hit */ | |
3815832a | 690 | goto found; |
b027d4c9 DC |
691 | case -EAGAIN: |
692 | /* cache hit, trylock failure, caller handles failure */ | |
693 | ASSERT(flags & XBF_TRYLOCK); | |
694 | return NULL; | |
695 | case -ENOENT: | |
696 | /* cache miss, go for insert */ | |
697 | break; | |
698 | case -EFSCORRUPTED: | |
699 | default: | |
700 | /* | |
701 | * None of the higher layers understand failure types | |
702 | * yet, so return NULL to signal a fatal lookup error. | |
703 | */ | |
704 | return NULL; | |
705 | } | |
3815832a | 706 | |
6dde2707 | 707 | new_bp = _xfs_buf_alloc(target, map, nmaps, flags); |
ce8e922c | 708 | if (unlikely(!new_bp)) |
1da177e4 LT |
709 | return NULL; |
710 | ||
fe2429b0 DC |
711 | error = xfs_buf_allocate_memory(new_bp, flags); |
712 | if (error) { | |
3e85c868 | 713 | xfs_buf_free(new_bp); |
fe2429b0 DC |
714 | return NULL; |
715 | } | |
716 | ||
b027d4c9 DC |
717 | error = xfs_buf_find(target, map, nmaps, flags, new_bp, &bp); |
718 | if (error) { | |
fe2429b0 | 719 | xfs_buf_free(new_bp); |
3815832a DC |
720 | return NULL; |
721 | } | |
722 | ||
fe2429b0 DC |
723 | if (bp != new_bp) |
724 | xfs_buf_free(new_bp); | |
1da177e4 | 725 | |
3815832a | 726 | found: |
611c9946 | 727 | if (!bp->b_addr) { |
ce8e922c | 728 | error = _xfs_buf_map_pages(bp, flags); |
1da177e4 | 729 | if (unlikely(error)) { |
4f10700a | 730 | xfs_warn(target->bt_mount, |
08e96e1a | 731 | "%s: failed to map pagesn", __func__); |
a8acad70 DC |
732 | xfs_buf_relse(bp); |
733 | return NULL; | |
1da177e4 LT |
734 | } |
735 | } | |
736 | ||
b79f4a1c DC |
737 | /* |
738 | * Clear b_error if this is a lookup from a caller that doesn't expect | |
739 | * valid data to be found in the buffer. | |
740 | */ | |
741 | if (!(flags & XBF_READ)) | |
742 | xfs_buf_ioerror(bp, 0); | |
743 | ||
ff6d6af2 | 744 | XFS_STATS_INC(target->bt_mount, xb_get); |
0b1b213f | 745 | trace_xfs_buf_get(bp, flags, _RET_IP_); |
ce8e922c | 746 | return bp; |
1da177e4 LT |
747 | } |
748 | ||
5d765b97 CH |
749 | STATIC int |
750 | _xfs_buf_read( | |
751 | xfs_buf_t *bp, | |
752 | xfs_buf_flags_t flags) | |
753 | { | |
43ff2122 | 754 | ASSERT(!(flags & XBF_WRITE)); |
f4b42421 | 755 | ASSERT(bp->b_maps[0].bm_bn != XFS_BUF_DADDR_NULL); |
5d765b97 | 756 | |
43ff2122 | 757 | bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD); |
1d5ae5df | 758 | bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD); |
5d765b97 | 759 | |
6af88cda | 760 | return xfs_buf_submit(bp); |
5d765b97 CH |
761 | } |
762 | ||
1da177e4 | 763 | xfs_buf_t * |
6dde2707 DC |
764 | xfs_buf_read_map( |
765 | struct xfs_buftarg *target, | |
766 | struct xfs_buf_map *map, | |
767 | int nmaps, | |
c3f8fc73 | 768 | xfs_buf_flags_t flags, |
1813dd64 | 769 | const struct xfs_buf_ops *ops) |
1da177e4 | 770 | { |
6dde2707 | 771 | struct xfs_buf *bp; |
ce8e922c NS |
772 | |
773 | flags |= XBF_READ; | |
774 | ||
6dde2707 | 775 | bp = xfs_buf_get_map(target, map, nmaps, flags); |
ce8e922c | 776 | if (bp) { |
0b1b213f CH |
777 | trace_xfs_buf_read(bp, flags, _RET_IP_); |
778 | ||
b0388bf1 | 779 | if (!(bp->b_flags & XBF_DONE)) { |
ff6d6af2 | 780 | XFS_STATS_INC(target->bt_mount, xb_get_read); |
1813dd64 | 781 | bp->b_ops = ops; |
5d765b97 | 782 | _xfs_buf_read(bp, flags); |
ce8e922c | 783 | } else if (flags & XBF_ASYNC) { |
1da177e4 LT |
784 | /* |
785 | * Read ahead call which is already satisfied, | |
786 | * drop the buffer | |
787 | */ | |
a8acad70 DC |
788 | xfs_buf_relse(bp); |
789 | return NULL; | |
1da177e4 | 790 | } else { |
1da177e4 | 791 | /* We do not want read in the flags */ |
ce8e922c | 792 | bp->b_flags &= ~XBF_READ; |
1da177e4 LT |
793 | } |
794 | } | |
795 | ||
ce8e922c | 796 | return bp; |
1da177e4 LT |
797 | } |
798 | ||
1da177e4 | 799 | /* |
ce8e922c NS |
800 | * If we are not low on memory then do the readahead in a deadlock |
801 | * safe manner. | |
1da177e4 LT |
802 | */ |
803 | void | |
6dde2707 DC |
804 | xfs_buf_readahead_map( |
805 | struct xfs_buftarg *target, | |
806 | struct xfs_buf_map *map, | |
c3f8fc73 | 807 | int nmaps, |
1813dd64 | 808 | const struct xfs_buf_ops *ops) |
1da177e4 | 809 | { |
efa7c9f9 | 810 | if (bdi_read_congested(target->bt_bdev->bd_bdi)) |
1da177e4 LT |
811 | return; |
812 | ||
6dde2707 | 813 | xfs_buf_read_map(target, map, nmaps, |
1813dd64 | 814 | XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD, ops); |
1da177e4 LT |
815 | } |
816 | ||
5adc94c2 DC |
817 | /* |
818 | * Read an uncached buffer from disk. Allocates and returns a locked | |
819 | * buffer containing the disk contents or nothing. | |
820 | */ | |
ba372674 | 821 | int |
5adc94c2 | 822 | xfs_buf_read_uncached( |
5adc94c2 DC |
823 | struct xfs_buftarg *target, |
824 | xfs_daddr_t daddr, | |
e70b73f8 | 825 | size_t numblks, |
c3f8fc73 | 826 | int flags, |
ba372674 | 827 | struct xfs_buf **bpp, |
1813dd64 | 828 | const struct xfs_buf_ops *ops) |
5adc94c2 | 829 | { |
eab4e633 | 830 | struct xfs_buf *bp; |
5adc94c2 | 831 | |
ba372674 DC |
832 | *bpp = NULL; |
833 | ||
e70b73f8 | 834 | bp = xfs_buf_get_uncached(target, numblks, flags); |
5adc94c2 | 835 | if (!bp) |
ba372674 | 836 | return -ENOMEM; |
5adc94c2 DC |
837 | |
838 | /* set up the buffer for a read IO */ | |
3e85c868 | 839 | ASSERT(bp->b_map_count == 1); |
ba372674 | 840 | bp->b_bn = XFS_BUF_DADDR_NULL; /* always null for uncached buffers */ |
3e85c868 | 841 | bp->b_maps[0].bm_bn = daddr; |
cbb7baab | 842 | bp->b_flags |= XBF_READ; |
1813dd64 | 843 | bp->b_ops = ops; |
5adc94c2 | 844 | |
6af88cda | 845 | xfs_buf_submit(bp); |
ba372674 DC |
846 | if (bp->b_error) { |
847 | int error = bp->b_error; | |
83a0adc3 | 848 | xfs_buf_relse(bp); |
ba372674 | 849 | return error; |
83a0adc3 | 850 | } |
ba372674 DC |
851 | |
852 | *bpp = bp; | |
853 | return 0; | |
1da177e4 LT |
854 | } |
855 | ||
44396476 DC |
856 | /* |
857 | * Return a buffer allocated as an empty buffer and associated to external | |
858 | * memory via xfs_buf_associate_memory() back to it's empty state. | |
859 | */ | |
860 | void | |
861 | xfs_buf_set_empty( | |
862 | struct xfs_buf *bp, | |
e70b73f8 | 863 | size_t numblks) |
44396476 DC |
864 | { |
865 | if (bp->b_pages) | |
866 | _xfs_buf_free_pages(bp); | |
867 | ||
868 | bp->b_pages = NULL; | |
869 | bp->b_page_count = 0; | |
870 | bp->b_addr = NULL; | |
4e94b71b | 871 | bp->b_length = numblks; |
aa0e8833 | 872 | bp->b_io_length = numblks; |
3e85c868 DC |
873 | |
874 | ASSERT(bp->b_map_count == 1); | |
44396476 | 875 | bp->b_bn = XFS_BUF_DADDR_NULL; |
3e85c868 DC |
876 | bp->b_maps[0].bm_bn = XFS_BUF_DADDR_NULL; |
877 | bp->b_maps[0].bm_len = bp->b_length; | |
44396476 DC |
878 | } |
879 | ||
1da177e4 LT |
880 | static inline struct page * |
881 | mem_to_page( | |
882 | void *addr) | |
883 | { | |
9e2779fa | 884 | if ((!is_vmalloc_addr(addr))) { |
1da177e4 LT |
885 | return virt_to_page(addr); |
886 | } else { | |
887 | return vmalloc_to_page(addr); | |
888 | } | |
889 | } | |
890 | ||
891 | int | |
ce8e922c NS |
892 | xfs_buf_associate_memory( |
893 | xfs_buf_t *bp, | |
1da177e4 LT |
894 | void *mem, |
895 | size_t len) | |
896 | { | |
897 | int rval; | |
898 | int i = 0; | |
d1afb678 LM |
899 | unsigned long pageaddr; |
900 | unsigned long offset; | |
901 | size_t buflen; | |
1da177e4 LT |
902 | int page_count; |
903 | ||
0e6e847f | 904 | pageaddr = (unsigned long)mem & PAGE_MASK; |
d1afb678 | 905 | offset = (unsigned long)mem - pageaddr; |
0e6e847f DC |
906 | buflen = PAGE_ALIGN(len + offset); |
907 | page_count = buflen >> PAGE_SHIFT; | |
1da177e4 LT |
908 | |
909 | /* Free any previous set of page pointers */ | |
ce8e922c NS |
910 | if (bp->b_pages) |
911 | _xfs_buf_free_pages(bp); | |
1da177e4 | 912 | |
ce8e922c NS |
913 | bp->b_pages = NULL; |
914 | bp->b_addr = mem; | |
1da177e4 | 915 | |
87937bf8 | 916 | rval = _xfs_buf_get_pages(bp, page_count); |
1da177e4 LT |
917 | if (rval) |
918 | return rval; | |
919 | ||
ce8e922c | 920 | bp->b_offset = offset; |
d1afb678 LM |
921 | |
922 | for (i = 0; i < bp->b_page_count; i++) { | |
923 | bp->b_pages[i] = mem_to_page((void *)pageaddr); | |
0e6e847f | 924 | pageaddr += PAGE_SIZE; |
1da177e4 | 925 | } |
1da177e4 | 926 | |
aa0e8833 | 927 | bp->b_io_length = BTOBB(len); |
4e94b71b | 928 | bp->b_length = BTOBB(buflen); |
1da177e4 LT |
929 | |
930 | return 0; | |
931 | } | |
932 | ||
933 | xfs_buf_t * | |
686865f7 DC |
934 | xfs_buf_get_uncached( |
935 | struct xfs_buftarg *target, | |
e70b73f8 | 936 | size_t numblks, |
686865f7 | 937 | int flags) |
1da177e4 | 938 | { |
e70b73f8 | 939 | unsigned long page_count; |
1fa40b01 | 940 | int error, i; |
3e85c868 DC |
941 | struct xfs_buf *bp; |
942 | DEFINE_SINGLE_BUF_MAP(map, XFS_BUF_DADDR_NULL, numblks); | |
1da177e4 | 943 | |
c891c30a BF |
944 | /* flags might contain irrelevant bits, pass only what we care about */ |
945 | bp = _xfs_buf_alloc(target, &map, 1, flags & XBF_NO_IOACCT); | |
1da177e4 LT |
946 | if (unlikely(bp == NULL)) |
947 | goto fail; | |
1da177e4 | 948 | |
e70b73f8 | 949 | page_count = PAGE_ALIGN(numblks << BBSHIFT) >> PAGE_SHIFT; |
87937bf8 | 950 | error = _xfs_buf_get_pages(bp, page_count); |
1fa40b01 | 951 | if (error) |
1da177e4 LT |
952 | goto fail_free_buf; |
953 | ||
1fa40b01 | 954 | for (i = 0; i < page_count; i++) { |
686865f7 | 955 | bp->b_pages[i] = alloc_page(xb_to_gfp(flags)); |
1fa40b01 CH |
956 | if (!bp->b_pages[i]) |
957 | goto fail_free_mem; | |
1da177e4 | 958 | } |
1fa40b01 | 959 | bp->b_flags |= _XBF_PAGES; |
1da177e4 | 960 | |
611c9946 | 961 | error = _xfs_buf_map_pages(bp, 0); |
1fa40b01 | 962 | if (unlikely(error)) { |
4f10700a | 963 | xfs_warn(target->bt_mount, |
08e96e1a | 964 | "%s: failed to map pages", __func__); |
1da177e4 | 965 | goto fail_free_mem; |
1fa40b01 | 966 | } |
1da177e4 | 967 | |
686865f7 | 968 | trace_xfs_buf_get_uncached(bp, _RET_IP_); |
1da177e4 | 969 | return bp; |
1fa40b01 | 970 | |
1da177e4 | 971 | fail_free_mem: |
1fa40b01 CH |
972 | while (--i >= 0) |
973 | __free_page(bp->b_pages[i]); | |
ca165b88 | 974 | _xfs_buf_free_pages(bp); |
1da177e4 | 975 | fail_free_buf: |
3e85c868 | 976 | xfs_buf_free_maps(bp); |
4347b9d7 | 977 | kmem_zone_free(xfs_buf_zone, bp); |
1da177e4 LT |
978 | fail: |
979 | return NULL; | |
980 | } | |
981 | ||
982 | /* | |
1da177e4 LT |
983 | * Increment reference count on buffer, to hold the buffer concurrently |
984 | * with another thread which may release (free) the buffer asynchronously. | |
1da177e4 LT |
985 | * Must hold the buffer already to call this function. |
986 | */ | |
987 | void | |
ce8e922c NS |
988 | xfs_buf_hold( |
989 | xfs_buf_t *bp) | |
1da177e4 | 990 | { |
0b1b213f | 991 | trace_xfs_buf_hold(bp, _RET_IP_); |
ce8e922c | 992 | atomic_inc(&bp->b_hold); |
1da177e4 LT |
993 | } |
994 | ||
995 | /* | |
9c7504aa BF |
996 | * Release a hold on the specified buffer. If the hold count is 1, the buffer is |
997 | * placed on LRU or freed (depending on b_lru_ref). | |
1da177e4 LT |
998 | */ |
999 | void | |
ce8e922c NS |
1000 | xfs_buf_rele( |
1001 | xfs_buf_t *bp) | |
1da177e4 | 1002 | { |
74f75a0c | 1003 | struct xfs_perag *pag = bp->b_pag; |
9c7504aa BF |
1004 | bool release; |
1005 | bool freebuf = false; | |
1da177e4 | 1006 | |
0b1b213f | 1007 | trace_xfs_buf_rele(bp, _RET_IP_); |
1da177e4 | 1008 | |
74f75a0c | 1009 | if (!pag) { |
430cbeb8 | 1010 | ASSERT(list_empty(&bp->b_lru)); |
9c7504aa BF |
1011 | if (atomic_dec_and_test(&bp->b_hold)) { |
1012 | xfs_buf_ioacct_dec(bp); | |
fad3aa1e | 1013 | xfs_buf_free(bp); |
9c7504aa | 1014 | } |
fad3aa1e NS |
1015 | return; |
1016 | } | |
1017 | ||
3790689f | 1018 | ASSERT(atomic_read(&bp->b_hold) > 0); |
a4082357 | 1019 | |
9c7504aa BF |
1020 | release = atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock); |
1021 | spin_lock(&bp->b_lock); | |
1022 | if (!release) { | |
1023 | /* | |
1024 | * Drop the in-flight state if the buffer is already on the LRU | |
1025 | * and it holds the only reference. This is racy because we | |
1026 | * haven't acquired the pag lock, but the use of _XBF_IN_FLIGHT | |
1027 | * ensures the decrement occurs only once per-buf. | |
1028 | */ | |
1029 | if ((atomic_read(&bp->b_hold) == 1) && !list_empty(&bp->b_lru)) | |
63db7c81 | 1030 | __xfs_buf_ioacct_dec(bp); |
9c7504aa BF |
1031 | goto out_unlock; |
1032 | } | |
1033 | ||
1034 | /* the last reference has been dropped ... */ | |
63db7c81 | 1035 | __xfs_buf_ioacct_dec(bp); |
9c7504aa BF |
1036 | if (!(bp->b_flags & XBF_STALE) && atomic_read(&bp->b_lru_ref)) { |
1037 | /* | |
1038 | * If the buffer is added to the LRU take a new reference to the | |
1039 | * buffer for the LRU and clear the (now stale) dispose list | |
1040 | * state flag | |
1041 | */ | |
1042 | if (list_lru_add(&bp->b_target->bt_lru, &bp->b_lru)) { | |
1043 | bp->b_state &= ~XFS_BSTATE_DISPOSE; | |
1044 | atomic_inc(&bp->b_hold); | |
1da177e4 | 1045 | } |
9c7504aa BF |
1046 | spin_unlock(&pag->pag_buf_lock); |
1047 | } else { | |
1048 | /* | |
1049 | * most of the time buffers will already be removed from the | |
1050 | * LRU, so optimise that case by checking for the | |
1051 | * XFS_BSTATE_DISPOSE flag indicating the last list the buffer | |
1052 | * was on was the disposal list | |
1053 | */ | |
1054 | if (!(bp->b_state & XFS_BSTATE_DISPOSE)) { | |
1055 | list_lru_del(&bp->b_target->bt_lru, &bp->b_lru); | |
1056 | } else { | |
1057 | ASSERT(list_empty(&bp->b_lru)); | |
1da177e4 | 1058 | } |
9c7504aa BF |
1059 | |
1060 | ASSERT(!(bp->b_flags & _XBF_DELWRI_Q)); | |
6031e73a LS |
1061 | rhashtable_remove_fast(&pag->pag_buf_hash, &bp->b_rhash_head, |
1062 | xfs_buf_hash_params); | |
9c7504aa BF |
1063 | spin_unlock(&pag->pag_buf_lock); |
1064 | xfs_perag_put(pag); | |
1065 | freebuf = true; | |
1da177e4 | 1066 | } |
9c7504aa BF |
1067 | |
1068 | out_unlock: | |
1069 | spin_unlock(&bp->b_lock); | |
1070 | ||
1071 | if (freebuf) | |
1072 | xfs_buf_free(bp); | |
1da177e4 LT |
1073 | } |
1074 | ||
1075 | ||
1076 | /* | |
0e6e847f | 1077 | * Lock a buffer object, if it is not already locked. |
90810b9e DC |
1078 | * |
1079 | * If we come across a stale, pinned, locked buffer, we know that we are | |
1080 | * being asked to lock a buffer that has been reallocated. Because it is | |
1081 | * pinned, we know that the log has not been pushed to disk and hence it | |
1082 | * will still be locked. Rather than continuing to have trylock attempts | |
1083 | * fail until someone else pushes the log, push it ourselves before | |
1084 | * returning. This means that the xfsaild will not get stuck trying | |
1085 | * to push on stale inode buffers. | |
1da177e4 LT |
1086 | */ |
1087 | int | |
0c842ad4 CH |
1088 | xfs_buf_trylock( |
1089 | struct xfs_buf *bp) | |
1da177e4 LT |
1090 | { |
1091 | int locked; | |
1092 | ||
ce8e922c | 1093 | locked = down_trylock(&bp->b_sema) == 0; |
479c6412 | 1094 | if (locked) { |
ce8e922c | 1095 | XB_SET_OWNER(bp); |
479c6412 DW |
1096 | trace_xfs_buf_trylock(bp, _RET_IP_); |
1097 | } else { | |
1098 | trace_xfs_buf_trylock_fail(bp, _RET_IP_); | |
1099 | } | |
0c842ad4 | 1100 | return locked; |
1da177e4 | 1101 | } |
1da177e4 LT |
1102 | |
1103 | /* | |
0e6e847f | 1104 | * Lock a buffer object. |
ed3b4d6c DC |
1105 | * |
1106 | * If we come across a stale, pinned, locked buffer, we know that we | |
1107 | * are being asked to lock a buffer that has been reallocated. Because | |
1108 | * it is pinned, we know that the log has not been pushed to disk and | |
1109 | * hence it will still be locked. Rather than sleeping until someone | |
1110 | * else pushes the log, push it ourselves before trying to get the lock. | |
1da177e4 | 1111 | */ |
ce8e922c NS |
1112 | void |
1113 | xfs_buf_lock( | |
0c842ad4 | 1114 | struct xfs_buf *bp) |
1da177e4 | 1115 | { |
0b1b213f CH |
1116 | trace_xfs_buf_lock(bp, _RET_IP_); |
1117 | ||
ed3b4d6c | 1118 | if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE)) |
ebad861b | 1119 | xfs_log_force(bp->b_target->bt_mount, 0); |
ce8e922c NS |
1120 | down(&bp->b_sema); |
1121 | XB_SET_OWNER(bp); | |
0b1b213f CH |
1122 | |
1123 | trace_xfs_buf_lock_done(bp, _RET_IP_); | |
1da177e4 LT |
1124 | } |
1125 | ||
1da177e4 | 1126 | void |
ce8e922c | 1127 | xfs_buf_unlock( |
0c842ad4 | 1128 | struct xfs_buf *bp) |
1da177e4 | 1129 | { |
20e8a063 BF |
1130 | ASSERT(xfs_buf_islocked(bp)); |
1131 | ||
ce8e922c NS |
1132 | XB_CLEAR_OWNER(bp); |
1133 | up(&bp->b_sema); | |
0b1b213f CH |
1134 | |
1135 | trace_xfs_buf_unlock(bp, _RET_IP_); | |
1da177e4 LT |
1136 | } |
1137 | ||
ce8e922c NS |
1138 | STATIC void |
1139 | xfs_buf_wait_unpin( | |
1140 | xfs_buf_t *bp) | |
1da177e4 LT |
1141 | { |
1142 | DECLARE_WAITQUEUE (wait, current); | |
1143 | ||
ce8e922c | 1144 | if (atomic_read(&bp->b_pin_count) == 0) |
1da177e4 LT |
1145 | return; |
1146 | ||
ce8e922c | 1147 | add_wait_queue(&bp->b_waiters, &wait); |
1da177e4 LT |
1148 | for (;;) { |
1149 | set_current_state(TASK_UNINTERRUPTIBLE); | |
ce8e922c | 1150 | if (atomic_read(&bp->b_pin_count) == 0) |
1da177e4 | 1151 | break; |
7eaceacc | 1152 | io_schedule(); |
1da177e4 | 1153 | } |
ce8e922c | 1154 | remove_wait_queue(&bp->b_waiters, &wait); |
1da177e4 LT |
1155 | set_current_state(TASK_RUNNING); |
1156 | } | |
1157 | ||
1158 | /* | |
1159 | * Buffer Utility Routines | |
1160 | */ | |
1161 | ||
e8aaba9a DC |
1162 | void |
1163 | xfs_buf_ioend( | |
1164 | struct xfs_buf *bp) | |
1da177e4 | 1165 | { |
e8aaba9a DC |
1166 | bool read = bp->b_flags & XBF_READ; |
1167 | ||
1168 | trace_xfs_buf_iodone(bp, _RET_IP_); | |
1813dd64 DC |
1169 | |
1170 | bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD); | |
d5929de8 | 1171 | |
61be9c52 DC |
1172 | /* |
1173 | * Pull in IO completion errors now. We are guaranteed to be running | |
1174 | * single threaded, so we don't need the lock to read b_io_error. | |
1175 | */ | |
1176 | if (!bp->b_error && bp->b_io_error) | |
1177 | xfs_buf_ioerror(bp, bp->b_io_error); | |
1178 | ||
e8aaba9a DC |
1179 | /* Only validate buffers that were read without errors */ |
1180 | if (read && !bp->b_error && bp->b_ops) { | |
1181 | ASSERT(!bp->b_iodone); | |
1813dd64 | 1182 | bp->b_ops->verify_read(bp); |
e8aaba9a DC |
1183 | } |
1184 | ||
1185 | if (!bp->b_error) | |
1186 | bp->b_flags |= XBF_DONE; | |
1da177e4 | 1187 | |
80f6c29d | 1188 | if (bp->b_iodone) |
ce8e922c NS |
1189 | (*(bp->b_iodone))(bp); |
1190 | else if (bp->b_flags & XBF_ASYNC) | |
1da177e4 | 1191 | xfs_buf_relse(bp); |
595bff75 | 1192 | else |
1813dd64 | 1193 | complete(&bp->b_iowait); |
1da177e4 LT |
1194 | } |
1195 | ||
e8aaba9a DC |
1196 | static void |
1197 | xfs_buf_ioend_work( | |
1198 | struct work_struct *work) | |
1da177e4 | 1199 | { |
e8aaba9a | 1200 | struct xfs_buf *bp = |
b29c70f5 | 1201 | container_of(work, xfs_buf_t, b_ioend_work); |
0b1b213f | 1202 | |
e8aaba9a DC |
1203 | xfs_buf_ioend(bp); |
1204 | } | |
1da177e4 | 1205 | |
211fe1a4 | 1206 | static void |
e8aaba9a DC |
1207 | xfs_buf_ioend_async( |
1208 | struct xfs_buf *bp) | |
1209 | { | |
b29c70f5 BF |
1210 | INIT_WORK(&bp->b_ioend_work, xfs_buf_ioend_work); |
1211 | queue_work(bp->b_ioend_wq, &bp->b_ioend_work); | |
1da177e4 LT |
1212 | } |
1213 | ||
1da177e4 | 1214 | void |
31ca03c9 | 1215 | __xfs_buf_ioerror( |
ce8e922c | 1216 | xfs_buf_t *bp, |
31ca03c9 DW |
1217 | int error, |
1218 | xfs_failaddr_t failaddr) | |
1da177e4 | 1219 | { |
2451337d DC |
1220 | ASSERT(error <= 0 && error >= -1000); |
1221 | bp->b_error = error; | |
31ca03c9 | 1222 | trace_xfs_buf_ioerror(bp, error, failaddr); |
1da177e4 LT |
1223 | } |
1224 | ||
901796af CH |
1225 | void |
1226 | xfs_buf_ioerror_alert( | |
1227 | struct xfs_buf *bp, | |
1228 | const char *func) | |
1229 | { | |
1230 | xfs_alert(bp->b_target->bt_mount, | |
c219b015 DW |
1231 | "metadata I/O error in \"%s\" at daddr 0x%llx len %d error %d", |
1232 | func, (uint64_t)XFS_BUF_ADDR(bp), bp->b_length, | |
1233 | -bp->b_error); | |
901796af CH |
1234 | } |
1235 | ||
a2dcf5df CH |
1236 | int |
1237 | xfs_bwrite( | |
1238 | struct xfs_buf *bp) | |
1239 | { | |
1240 | int error; | |
1241 | ||
1242 | ASSERT(xfs_buf_islocked(bp)); | |
1243 | ||
1244 | bp->b_flags |= XBF_WRITE; | |
27187754 DC |
1245 | bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q | |
1246 | XBF_WRITE_FAIL | XBF_DONE); | |
a2dcf5df | 1247 | |
6af88cda | 1248 | error = xfs_buf_submit(bp); |
a2dcf5df CH |
1249 | if (error) { |
1250 | xfs_force_shutdown(bp->b_target->bt_mount, | |
1251 | SHUTDOWN_META_IO_ERROR); | |
1252 | } | |
1253 | return error; | |
1254 | } | |
1255 | ||
9bdd9bd6 | 1256 | static void |
ce8e922c | 1257 | xfs_buf_bio_end_io( |
4246a0b6 | 1258 | struct bio *bio) |
1da177e4 | 1259 | { |
9bdd9bd6 | 1260 | struct xfs_buf *bp = (struct xfs_buf *)bio->bi_private; |
1da177e4 | 1261 | |
37eb17e6 DC |
1262 | /* |
1263 | * don't overwrite existing errors - otherwise we can lose errors on | |
1264 | * buffers that require multiple bios to complete. | |
1265 | */ | |
4e4cbee9 CH |
1266 | if (bio->bi_status) { |
1267 | int error = blk_status_to_errno(bio->bi_status); | |
1268 | ||
1269 | cmpxchg(&bp->b_io_error, 0, error); | |
1270 | } | |
1da177e4 | 1271 | |
37eb17e6 | 1272 | if (!bp->b_error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ)) |
73c77e2c JB |
1273 | invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp)); |
1274 | ||
e8aaba9a DC |
1275 | if (atomic_dec_and_test(&bp->b_io_remaining) == 1) |
1276 | xfs_buf_ioend_async(bp); | |
1da177e4 | 1277 | bio_put(bio); |
1da177e4 LT |
1278 | } |
1279 | ||
3e85c868 DC |
1280 | static void |
1281 | xfs_buf_ioapply_map( | |
1282 | struct xfs_buf *bp, | |
1283 | int map, | |
1284 | int *buf_offset, | |
1285 | int *count, | |
50bfcd0c MC |
1286 | int op, |
1287 | int op_flags) | |
1da177e4 | 1288 | { |
3e85c868 DC |
1289 | int page_index; |
1290 | int total_nr_pages = bp->b_page_count; | |
1291 | int nr_pages; | |
1292 | struct bio *bio; | |
1293 | sector_t sector = bp->b_maps[map].bm_bn; | |
1294 | int size; | |
1295 | int offset; | |
1da177e4 | 1296 | |
3e85c868 DC |
1297 | /* skip the pages in the buffer before the start offset */ |
1298 | page_index = 0; | |
1299 | offset = *buf_offset; | |
1300 | while (offset >= PAGE_SIZE) { | |
1301 | page_index++; | |
1302 | offset -= PAGE_SIZE; | |
f538d4da CH |
1303 | } |
1304 | ||
3e85c868 DC |
1305 | /* |
1306 | * Limit the IO size to the length of the current vector, and update the | |
1307 | * remaining IO count for the next time around. | |
1308 | */ | |
1309 | size = min_t(int, BBTOB(bp->b_maps[map].bm_len), *count); | |
1310 | *count -= size; | |
1311 | *buf_offset += size; | |
34951f5c | 1312 | |
1da177e4 | 1313 | next_chunk: |
ce8e922c | 1314 | atomic_inc(&bp->b_io_remaining); |
c908e380 | 1315 | nr_pages = min(total_nr_pages, BIO_MAX_PAGES); |
1da177e4 LT |
1316 | |
1317 | bio = bio_alloc(GFP_NOIO, nr_pages); | |
74d46992 | 1318 | bio_set_dev(bio, bp->b_target->bt_bdev); |
4f024f37 | 1319 | bio->bi_iter.bi_sector = sector; |
ce8e922c NS |
1320 | bio->bi_end_io = xfs_buf_bio_end_io; |
1321 | bio->bi_private = bp; | |
50bfcd0c | 1322 | bio_set_op_attrs(bio, op, op_flags); |
0e6e847f | 1323 | |
3e85c868 | 1324 | for (; size && nr_pages; nr_pages--, page_index++) { |
0e6e847f | 1325 | int rbytes, nbytes = PAGE_SIZE - offset; |
1da177e4 LT |
1326 | |
1327 | if (nbytes > size) | |
1328 | nbytes = size; | |
1329 | ||
3e85c868 DC |
1330 | rbytes = bio_add_page(bio, bp->b_pages[page_index], nbytes, |
1331 | offset); | |
ce8e922c | 1332 | if (rbytes < nbytes) |
1da177e4 LT |
1333 | break; |
1334 | ||
1335 | offset = 0; | |
aa0e8833 | 1336 | sector += BTOBB(nbytes); |
1da177e4 LT |
1337 | size -= nbytes; |
1338 | total_nr_pages--; | |
1339 | } | |
1340 | ||
4f024f37 | 1341 | if (likely(bio->bi_iter.bi_size)) { |
73c77e2c JB |
1342 | if (xfs_buf_is_vmapped(bp)) { |
1343 | flush_kernel_vmap_range(bp->b_addr, | |
1344 | xfs_buf_vmap_len(bp)); | |
1345 | } | |
4e49ea4a | 1346 | submit_bio(bio); |
1da177e4 LT |
1347 | if (size) |
1348 | goto next_chunk; | |
1349 | } else { | |
37eb17e6 DC |
1350 | /* |
1351 | * This is guaranteed not to be the last io reference count | |
595bff75 | 1352 | * because the caller (xfs_buf_submit) holds a count itself. |
37eb17e6 DC |
1353 | */ |
1354 | atomic_dec(&bp->b_io_remaining); | |
2451337d | 1355 | xfs_buf_ioerror(bp, -EIO); |
ec53d1db | 1356 | bio_put(bio); |
1da177e4 | 1357 | } |
3e85c868 DC |
1358 | |
1359 | } | |
1360 | ||
1361 | STATIC void | |
1362 | _xfs_buf_ioapply( | |
1363 | struct xfs_buf *bp) | |
1364 | { | |
1365 | struct blk_plug plug; | |
50bfcd0c MC |
1366 | int op; |
1367 | int op_flags = 0; | |
3e85c868 DC |
1368 | int offset; |
1369 | int size; | |
1370 | int i; | |
1371 | ||
c163f9a1 DC |
1372 | /* |
1373 | * Make sure we capture only current IO errors rather than stale errors | |
1374 | * left over from previous use of the buffer (e.g. failed readahead). | |
1375 | */ | |
1376 | bp->b_error = 0; | |
1377 | ||
b29c70f5 BF |
1378 | /* |
1379 | * Initialize the I/O completion workqueue if we haven't yet or the | |
1380 | * submitter has not opted to specify a custom one. | |
1381 | */ | |
1382 | if (!bp->b_ioend_wq) | |
1383 | bp->b_ioend_wq = bp->b_target->bt_mount->m_buf_workqueue; | |
1384 | ||
3e85c868 | 1385 | if (bp->b_flags & XBF_WRITE) { |
50bfcd0c | 1386 | op = REQ_OP_WRITE; |
3e85c868 | 1387 | if (bp->b_flags & XBF_SYNCIO) |
70fd7614 | 1388 | op_flags = REQ_SYNC; |
3e85c868 | 1389 | if (bp->b_flags & XBF_FUA) |
50bfcd0c | 1390 | op_flags |= REQ_FUA; |
3e85c868 | 1391 | if (bp->b_flags & XBF_FLUSH) |
28a8f0d3 | 1392 | op_flags |= REQ_PREFLUSH; |
1813dd64 DC |
1393 | |
1394 | /* | |
1395 | * Run the write verifier callback function if it exists. If | |
1396 | * this function fails it will mark the buffer with an error and | |
1397 | * the IO should not be dispatched. | |
1398 | */ | |
1399 | if (bp->b_ops) { | |
1400 | bp->b_ops->verify_write(bp); | |
1401 | if (bp->b_error) { | |
1402 | xfs_force_shutdown(bp->b_target->bt_mount, | |
1403 | SHUTDOWN_CORRUPT_INCORE); | |
1404 | return; | |
1405 | } | |
400b9d88 DC |
1406 | } else if (bp->b_bn != XFS_BUF_DADDR_NULL) { |
1407 | struct xfs_mount *mp = bp->b_target->bt_mount; | |
1408 | ||
1409 | /* | |
1410 | * non-crc filesystems don't attach verifiers during | |
1411 | * log recovery, so don't warn for such filesystems. | |
1412 | */ | |
1413 | if (xfs_sb_version_hascrc(&mp->m_sb)) { | |
1414 | xfs_warn(mp, | |
c219b015 | 1415 | "%s: no buf ops on daddr 0x%llx len %d", |
400b9d88 | 1416 | __func__, bp->b_bn, bp->b_length); |
9c712a13 DW |
1417 | xfs_hex_dump(bp->b_addr, |
1418 | XFS_CORRUPTION_DUMP_LEN); | |
400b9d88 DC |
1419 | dump_stack(); |
1420 | } | |
1813dd64 | 1421 | } |
3e85c868 | 1422 | } else if (bp->b_flags & XBF_READ_AHEAD) { |
50bfcd0c MC |
1423 | op = REQ_OP_READ; |
1424 | op_flags = REQ_RAHEAD; | |
3e85c868 | 1425 | } else { |
50bfcd0c | 1426 | op = REQ_OP_READ; |
3e85c868 DC |
1427 | } |
1428 | ||
1429 | /* we only use the buffer cache for meta-data */ | |
50bfcd0c | 1430 | op_flags |= REQ_META; |
3e85c868 DC |
1431 | |
1432 | /* | |
1433 | * Walk all the vectors issuing IO on them. Set up the initial offset | |
1434 | * into the buffer and the desired IO size before we start - | |
1435 | * _xfs_buf_ioapply_vec() will modify them appropriately for each | |
1436 | * subsequent call. | |
1437 | */ | |
1438 | offset = bp->b_offset; | |
1439 | size = BBTOB(bp->b_io_length); | |
1440 | blk_start_plug(&plug); | |
1441 | for (i = 0; i < bp->b_map_count; i++) { | |
50bfcd0c | 1442 | xfs_buf_ioapply_map(bp, i, &offset, &size, op, op_flags); |
3e85c868 DC |
1443 | if (bp->b_error) |
1444 | break; | |
1445 | if (size <= 0) | |
1446 | break; /* all done */ | |
1447 | } | |
1448 | blk_finish_plug(&plug); | |
1da177e4 LT |
1449 | } |
1450 | ||
595bff75 | 1451 | /* |
bb00b6f1 | 1452 | * Wait for I/O completion of a sync buffer and return the I/O error code. |
595bff75 | 1453 | */ |
eaebb515 | 1454 | static int |
bb00b6f1 | 1455 | xfs_buf_iowait( |
595bff75 | 1456 | struct xfs_buf *bp) |
1da177e4 | 1457 | { |
bb00b6f1 BF |
1458 | ASSERT(!(bp->b_flags & XBF_ASYNC)); |
1459 | ||
1460 | trace_xfs_buf_iowait(bp, _RET_IP_); | |
1461 | wait_for_completion(&bp->b_iowait); | |
1462 | trace_xfs_buf_iowait_done(bp, _RET_IP_); | |
1463 | ||
1464 | return bp->b_error; | |
1465 | } | |
1466 | ||
1467 | /* | |
1468 | * Buffer I/O submission path, read or write. Asynchronous submission transfers | |
1469 | * the buffer lock ownership and the current reference to the IO. It is not | |
1470 | * safe to reference the buffer after a call to this function unless the caller | |
1471 | * holds an additional reference itself. | |
1472 | */ | |
1473 | int | |
1474 | __xfs_buf_submit( | |
1475 | struct xfs_buf *bp, | |
1476 | bool wait) | |
1477 | { | |
1478 | int error = 0; | |
1479 | ||
595bff75 | 1480 | trace_xfs_buf_submit(bp, _RET_IP_); |
1da177e4 | 1481 | |
43ff2122 | 1482 | ASSERT(!(bp->b_flags & _XBF_DELWRI_Q)); |
595bff75 DC |
1483 | |
1484 | /* on shutdown we stale and complete the buffer immediately */ | |
1485 | if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) { | |
1486 | xfs_buf_ioerror(bp, -EIO); | |
1487 | bp->b_flags &= ~XBF_DONE; | |
1488 | xfs_buf_stale(bp); | |
bb00b6f1 BF |
1489 | if (bp->b_flags & XBF_ASYNC) |
1490 | xfs_buf_ioend(bp); | |
eaebb515 | 1491 | return -EIO; |
595bff75 | 1492 | } |
1da177e4 | 1493 | |
bb00b6f1 BF |
1494 | /* |
1495 | * Grab a reference so the buffer does not go away underneath us. For | |
1496 | * async buffers, I/O completion drops the callers reference, which | |
1497 | * could occur before submission returns. | |
1498 | */ | |
1499 | xfs_buf_hold(bp); | |
1500 | ||
375ec69d | 1501 | if (bp->b_flags & XBF_WRITE) |
ce8e922c | 1502 | xfs_buf_wait_unpin(bp); |
e11bb805 | 1503 | |
61be9c52 DC |
1504 | /* clear the internal error state to avoid spurious errors */ |
1505 | bp->b_io_error = 0; | |
1506 | ||
8d6c1210 | 1507 | /* |
e11bb805 DC |
1508 | * Set the count to 1 initially, this will stop an I/O completion |
1509 | * callout which happens before we have started all the I/O from calling | |
1510 | * xfs_buf_ioend too early. | |
1da177e4 | 1511 | */ |
ce8e922c | 1512 | atomic_set(&bp->b_io_remaining, 1); |
eaebb515 BF |
1513 | if (bp->b_flags & XBF_ASYNC) |
1514 | xfs_buf_ioacct_inc(bp); | |
ce8e922c | 1515 | _xfs_buf_ioapply(bp); |
e11bb805 | 1516 | |
8d6c1210 | 1517 | /* |
595bff75 DC |
1518 | * If _xfs_buf_ioapply failed, we can get back here with only the IO |
1519 | * reference we took above. If we drop it to zero, run completion so | |
1520 | * that we don't return to the caller with completion still pending. | |
8d6c1210 | 1521 | */ |
e8aaba9a | 1522 | if (atomic_dec_and_test(&bp->b_io_remaining) == 1) { |
eaebb515 | 1523 | if (bp->b_error || !(bp->b_flags & XBF_ASYNC)) |
e8aaba9a DC |
1524 | xfs_buf_ioend(bp); |
1525 | else | |
1526 | xfs_buf_ioend_async(bp); | |
1527 | } | |
1da177e4 | 1528 | |
6af88cda BF |
1529 | if (wait) |
1530 | error = xfs_buf_iowait(bp); | |
bb00b6f1 | 1531 | |
595bff75 | 1532 | /* |
6af88cda BF |
1533 | * Release the hold that keeps the buffer referenced for the entire |
1534 | * I/O. Note that if the buffer is async, it is not safe to reference | |
1535 | * after this release. | |
595bff75 DC |
1536 | */ |
1537 | xfs_buf_rele(bp); | |
1538 | return error; | |
1da177e4 LT |
1539 | } |
1540 | ||
88ee2df7 | 1541 | void * |
ce8e922c | 1542 | xfs_buf_offset( |
88ee2df7 | 1543 | struct xfs_buf *bp, |
1da177e4 LT |
1544 | size_t offset) |
1545 | { | |
1546 | struct page *page; | |
1547 | ||
611c9946 | 1548 | if (bp->b_addr) |
62926044 | 1549 | return bp->b_addr + offset; |
1da177e4 | 1550 | |
ce8e922c | 1551 | offset += bp->b_offset; |
0e6e847f | 1552 | page = bp->b_pages[offset >> PAGE_SHIFT]; |
88ee2df7 | 1553 | return page_address(page) + (offset & (PAGE_SIZE-1)); |
1da177e4 LT |
1554 | } |
1555 | ||
1556 | /* | |
1da177e4 LT |
1557 | * Move data into or out of a buffer. |
1558 | */ | |
1559 | void | |
ce8e922c NS |
1560 | xfs_buf_iomove( |
1561 | xfs_buf_t *bp, /* buffer to process */ | |
1da177e4 LT |
1562 | size_t boff, /* starting buffer offset */ |
1563 | size_t bsize, /* length to copy */ | |
b9c48649 | 1564 | void *data, /* data address */ |
ce8e922c | 1565 | xfs_buf_rw_t mode) /* read/write/zero flag */ |
1da177e4 | 1566 | { |
795cac72 | 1567 | size_t bend; |
1da177e4 LT |
1568 | |
1569 | bend = boff + bsize; | |
1570 | while (boff < bend) { | |
795cac72 DC |
1571 | struct page *page; |
1572 | int page_index, page_offset, csize; | |
1573 | ||
1574 | page_index = (boff + bp->b_offset) >> PAGE_SHIFT; | |
1575 | page_offset = (boff + bp->b_offset) & ~PAGE_MASK; | |
1576 | page = bp->b_pages[page_index]; | |
1577 | csize = min_t(size_t, PAGE_SIZE - page_offset, | |
1578 | BBTOB(bp->b_io_length) - boff); | |
1da177e4 | 1579 | |
795cac72 | 1580 | ASSERT((csize + page_offset) <= PAGE_SIZE); |
1da177e4 LT |
1581 | |
1582 | switch (mode) { | |
ce8e922c | 1583 | case XBRW_ZERO: |
795cac72 | 1584 | memset(page_address(page) + page_offset, 0, csize); |
1da177e4 | 1585 | break; |
ce8e922c | 1586 | case XBRW_READ: |
795cac72 | 1587 | memcpy(data, page_address(page) + page_offset, csize); |
1da177e4 | 1588 | break; |
ce8e922c | 1589 | case XBRW_WRITE: |
795cac72 | 1590 | memcpy(page_address(page) + page_offset, data, csize); |
1da177e4 LT |
1591 | } |
1592 | ||
1593 | boff += csize; | |
1594 | data += csize; | |
1595 | } | |
1596 | } | |
1597 | ||
1598 | /* | |
ce8e922c | 1599 | * Handling of buffer targets (buftargs). |
1da177e4 LT |
1600 | */ |
1601 | ||
1602 | /* | |
430cbeb8 DC |
1603 | * Wait for any bufs with callbacks that have been submitted but have not yet |
1604 | * returned. These buffers will have an elevated hold count, so wait on those | |
1605 | * while freeing all the buffers only held by the LRU. | |
1da177e4 | 1606 | */ |
e80dfa19 DC |
1607 | static enum lru_status |
1608 | xfs_buftarg_wait_rele( | |
1609 | struct list_head *item, | |
3f97b163 | 1610 | struct list_lru_one *lru, |
e80dfa19 DC |
1611 | spinlock_t *lru_lock, |
1612 | void *arg) | |
1613 | ||
1da177e4 | 1614 | { |
e80dfa19 | 1615 | struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru); |
a4082357 | 1616 | struct list_head *dispose = arg; |
430cbeb8 | 1617 | |
e80dfa19 | 1618 | if (atomic_read(&bp->b_hold) > 1) { |
a4082357 | 1619 | /* need to wait, so skip it this pass */ |
e80dfa19 | 1620 | trace_xfs_buf_wait_buftarg(bp, _RET_IP_); |
a4082357 | 1621 | return LRU_SKIP; |
1da177e4 | 1622 | } |
a4082357 DC |
1623 | if (!spin_trylock(&bp->b_lock)) |
1624 | return LRU_SKIP; | |
e80dfa19 | 1625 | |
a4082357 DC |
1626 | /* |
1627 | * clear the LRU reference count so the buffer doesn't get | |
1628 | * ignored in xfs_buf_rele(). | |
1629 | */ | |
1630 | atomic_set(&bp->b_lru_ref, 0); | |
1631 | bp->b_state |= XFS_BSTATE_DISPOSE; | |
3f97b163 | 1632 | list_lru_isolate_move(lru, item, dispose); |
a4082357 DC |
1633 | spin_unlock(&bp->b_lock); |
1634 | return LRU_REMOVED; | |
1da177e4 LT |
1635 | } |
1636 | ||
e80dfa19 DC |
1637 | void |
1638 | xfs_wait_buftarg( | |
1639 | struct xfs_buftarg *btp) | |
1640 | { | |
a4082357 DC |
1641 | LIST_HEAD(dispose); |
1642 | int loop = 0; | |
1643 | ||
85bec546 | 1644 | /* |
9c7504aa BF |
1645 | * First wait on the buftarg I/O count for all in-flight buffers to be |
1646 | * released. This is critical as new buffers do not make the LRU until | |
1647 | * they are released. | |
1648 | * | |
1649 | * Next, flush the buffer workqueue to ensure all completion processing | |
1650 | * has finished. Just waiting on buffer locks is not sufficient for | |
1651 | * async IO as the reference count held over IO is not released until | |
1652 | * after the buffer lock is dropped. Hence we need to ensure here that | |
1653 | * all reference counts have been dropped before we start walking the | |
1654 | * LRU list. | |
85bec546 | 1655 | */ |
9c7504aa BF |
1656 | while (percpu_counter_sum(&btp->bt_io_count)) |
1657 | delay(100); | |
800b2694 | 1658 | flush_workqueue(btp->bt_mount->m_buf_workqueue); |
85bec546 | 1659 | |
a4082357 DC |
1660 | /* loop until there is nothing left on the lru list. */ |
1661 | while (list_lru_count(&btp->bt_lru)) { | |
e80dfa19 | 1662 | list_lru_walk(&btp->bt_lru, xfs_buftarg_wait_rele, |
a4082357 DC |
1663 | &dispose, LONG_MAX); |
1664 | ||
1665 | while (!list_empty(&dispose)) { | |
1666 | struct xfs_buf *bp; | |
1667 | bp = list_first_entry(&dispose, struct xfs_buf, b_lru); | |
1668 | list_del_init(&bp->b_lru); | |
ac8809f9 DC |
1669 | if (bp->b_flags & XBF_WRITE_FAIL) { |
1670 | xfs_alert(btp->bt_mount, | |
c219b015 | 1671 | "Corruption Alert: Buffer at daddr 0x%llx had permanent write failures!", |
ac8809f9 | 1672 | (long long)bp->b_bn); |
f41febd2 JP |
1673 | xfs_alert(btp->bt_mount, |
1674 | "Please run xfs_repair to determine the extent of the problem."); | |
ac8809f9 | 1675 | } |
a4082357 DC |
1676 | xfs_buf_rele(bp); |
1677 | } | |
1678 | if (loop++ != 0) | |
1679 | delay(100); | |
1680 | } | |
e80dfa19 DC |
1681 | } |
1682 | ||
1683 | static enum lru_status | |
1684 | xfs_buftarg_isolate( | |
1685 | struct list_head *item, | |
3f97b163 | 1686 | struct list_lru_one *lru, |
e80dfa19 DC |
1687 | spinlock_t *lru_lock, |
1688 | void *arg) | |
1689 | { | |
1690 | struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru); | |
1691 | struct list_head *dispose = arg; | |
1692 | ||
a4082357 DC |
1693 | /* |
1694 | * we are inverting the lru lock/bp->b_lock here, so use a trylock. | |
1695 | * If we fail to get the lock, just skip it. | |
1696 | */ | |
1697 | if (!spin_trylock(&bp->b_lock)) | |
1698 | return LRU_SKIP; | |
e80dfa19 DC |
1699 | /* |
1700 | * Decrement the b_lru_ref count unless the value is already | |
1701 | * zero. If the value is already zero, we need to reclaim the | |
1702 | * buffer, otherwise it gets another trip through the LRU. | |
1703 | */ | |
19957a18 | 1704 | if (atomic_add_unless(&bp->b_lru_ref, -1, 0)) { |
a4082357 | 1705 | spin_unlock(&bp->b_lock); |
e80dfa19 | 1706 | return LRU_ROTATE; |
a4082357 | 1707 | } |
e80dfa19 | 1708 | |
a4082357 | 1709 | bp->b_state |= XFS_BSTATE_DISPOSE; |
3f97b163 | 1710 | list_lru_isolate_move(lru, item, dispose); |
a4082357 | 1711 | spin_unlock(&bp->b_lock); |
e80dfa19 DC |
1712 | return LRU_REMOVED; |
1713 | } | |
1714 | ||
addbda40 | 1715 | static unsigned long |
e80dfa19 | 1716 | xfs_buftarg_shrink_scan( |
ff57ab21 | 1717 | struct shrinker *shrink, |
1495f230 | 1718 | struct shrink_control *sc) |
a6867a68 | 1719 | { |
ff57ab21 DC |
1720 | struct xfs_buftarg *btp = container_of(shrink, |
1721 | struct xfs_buftarg, bt_shrinker); | |
430cbeb8 | 1722 | LIST_HEAD(dispose); |
addbda40 | 1723 | unsigned long freed; |
430cbeb8 | 1724 | |
503c358c VD |
1725 | freed = list_lru_shrink_walk(&btp->bt_lru, sc, |
1726 | xfs_buftarg_isolate, &dispose); | |
430cbeb8 DC |
1727 | |
1728 | while (!list_empty(&dispose)) { | |
e80dfa19 | 1729 | struct xfs_buf *bp; |
430cbeb8 DC |
1730 | bp = list_first_entry(&dispose, struct xfs_buf, b_lru); |
1731 | list_del_init(&bp->b_lru); | |
1732 | xfs_buf_rele(bp); | |
1733 | } | |
1734 | ||
e80dfa19 DC |
1735 | return freed; |
1736 | } | |
1737 | ||
addbda40 | 1738 | static unsigned long |
e80dfa19 DC |
1739 | xfs_buftarg_shrink_count( |
1740 | struct shrinker *shrink, | |
1741 | struct shrink_control *sc) | |
1742 | { | |
1743 | struct xfs_buftarg *btp = container_of(shrink, | |
1744 | struct xfs_buftarg, bt_shrinker); | |
503c358c | 1745 | return list_lru_shrink_count(&btp->bt_lru, sc); |
a6867a68 DC |
1746 | } |
1747 | ||
1da177e4 LT |
1748 | void |
1749 | xfs_free_buftarg( | |
b7963133 | 1750 | struct xfs_buftarg *btp) |
1da177e4 | 1751 | { |
ff57ab21 | 1752 | unregister_shrinker(&btp->bt_shrinker); |
9c7504aa BF |
1753 | ASSERT(percpu_counter_sum(&btp->bt_io_count) == 0); |
1754 | percpu_counter_destroy(&btp->bt_io_count); | |
f5e1dd34 | 1755 | list_lru_destroy(&btp->bt_lru); |
ff57ab21 | 1756 | |
2291dab2 | 1757 | xfs_blkdev_issue_flush(btp); |
a6867a68 | 1758 | |
f0e2d93c | 1759 | kmem_free(btp); |
1da177e4 LT |
1760 | } |
1761 | ||
3fefdeee ES |
1762 | int |
1763 | xfs_setsize_buftarg( | |
1da177e4 | 1764 | xfs_buftarg_t *btp, |
3fefdeee | 1765 | unsigned int sectorsize) |
1da177e4 | 1766 | { |
7c71ee78 | 1767 | /* Set up metadata sector size info */ |
6da54179 ES |
1768 | btp->bt_meta_sectorsize = sectorsize; |
1769 | btp->bt_meta_sectormask = sectorsize - 1; | |
1da177e4 | 1770 | |
ce8e922c | 1771 | if (set_blocksize(btp->bt_bdev, sectorsize)) { |
4f10700a | 1772 | xfs_warn(btp->bt_mount, |
a1c6f057 DM |
1773 | "Cannot set_blocksize to %u on device %pg", |
1774 | sectorsize, btp->bt_bdev); | |
2451337d | 1775 | return -EINVAL; |
1da177e4 LT |
1776 | } |
1777 | ||
7c71ee78 ES |
1778 | /* Set up device logical sector size mask */ |
1779 | btp->bt_logical_sectorsize = bdev_logical_block_size(btp->bt_bdev); | |
1780 | btp->bt_logical_sectormask = bdev_logical_block_size(btp->bt_bdev) - 1; | |
1781 | ||
1da177e4 LT |
1782 | return 0; |
1783 | } | |
1784 | ||
1785 | /* | |
3fefdeee ES |
1786 | * When allocating the initial buffer target we have not yet |
1787 | * read in the superblock, so don't know what sized sectors | |
1788 | * are being used at this early stage. Play safe. | |
ce8e922c | 1789 | */ |
1da177e4 LT |
1790 | STATIC int |
1791 | xfs_setsize_buftarg_early( | |
1792 | xfs_buftarg_t *btp, | |
1793 | struct block_device *bdev) | |
1794 | { | |
a96c4151 | 1795 | return xfs_setsize_buftarg(btp, bdev_logical_block_size(bdev)); |
1da177e4 LT |
1796 | } |
1797 | ||
1da177e4 LT |
1798 | xfs_buftarg_t * |
1799 | xfs_alloc_buftarg( | |
ebad861b | 1800 | struct xfs_mount *mp, |
486aff5e DW |
1801 | struct block_device *bdev, |
1802 | struct dax_device *dax_dev) | |
1da177e4 LT |
1803 | { |
1804 | xfs_buftarg_t *btp; | |
1805 | ||
b17cb364 | 1806 | btp = kmem_zalloc(sizeof(*btp), KM_SLEEP | KM_NOFS); |
1da177e4 | 1807 | |
ebad861b | 1808 | btp->bt_mount = mp; |
ce8e922c NS |
1809 | btp->bt_dev = bdev->bd_dev; |
1810 | btp->bt_bdev = bdev; | |
486aff5e | 1811 | btp->bt_daxdev = dax_dev; |
0e6e847f | 1812 | |
1da177e4 | 1813 | if (xfs_setsize_buftarg_early(btp, bdev)) |
d210a987 | 1814 | goto error_free; |
5ca302c8 GC |
1815 | |
1816 | if (list_lru_init(&btp->bt_lru)) | |
d210a987 | 1817 | goto error_free; |
5ca302c8 | 1818 | |
9c7504aa | 1819 | if (percpu_counter_init(&btp->bt_io_count, 0, GFP_KERNEL)) |
d210a987 | 1820 | goto error_lru; |
9c7504aa | 1821 | |
e80dfa19 DC |
1822 | btp->bt_shrinker.count_objects = xfs_buftarg_shrink_count; |
1823 | btp->bt_shrinker.scan_objects = xfs_buftarg_shrink_scan; | |
ff57ab21 | 1824 | btp->bt_shrinker.seeks = DEFAULT_SEEKS; |
e80dfa19 | 1825 | btp->bt_shrinker.flags = SHRINKER_NUMA_AWARE; |
d210a987 MH |
1826 | if (register_shrinker(&btp->bt_shrinker)) |
1827 | goto error_pcpu; | |
1da177e4 LT |
1828 | return btp; |
1829 | ||
d210a987 MH |
1830 | error_pcpu: |
1831 | percpu_counter_destroy(&btp->bt_io_count); | |
1832 | error_lru: | |
1833 | list_lru_destroy(&btp->bt_lru); | |
1834 | error_free: | |
f0e2d93c | 1835 | kmem_free(btp); |
1da177e4 LT |
1836 | return NULL; |
1837 | } | |
1838 | ||
20e8a063 BF |
1839 | /* |
1840 | * Cancel a delayed write list. | |
1841 | * | |
1842 | * Remove each buffer from the list, clear the delwri queue flag and drop the | |
1843 | * associated buffer reference. | |
1844 | */ | |
1845 | void | |
1846 | xfs_buf_delwri_cancel( | |
1847 | struct list_head *list) | |
1848 | { | |
1849 | struct xfs_buf *bp; | |
1850 | ||
1851 | while (!list_empty(list)) { | |
1852 | bp = list_first_entry(list, struct xfs_buf, b_list); | |
1853 | ||
1854 | xfs_buf_lock(bp); | |
1855 | bp->b_flags &= ~_XBF_DELWRI_Q; | |
1856 | list_del_init(&bp->b_list); | |
1857 | xfs_buf_relse(bp); | |
1858 | } | |
1859 | } | |
1860 | ||
1da177e4 | 1861 | /* |
43ff2122 CH |
1862 | * Add a buffer to the delayed write list. |
1863 | * | |
1864 | * This queues a buffer for writeout if it hasn't already been. Note that | |
1865 | * neither this routine nor the buffer list submission functions perform | |
1866 | * any internal synchronization. It is expected that the lists are thread-local | |
1867 | * to the callers. | |
1868 | * | |
1869 | * Returns true if we queued up the buffer, or false if it already had | |
1870 | * been on the buffer list. | |
1da177e4 | 1871 | */ |
43ff2122 | 1872 | bool |
ce8e922c | 1873 | xfs_buf_delwri_queue( |
43ff2122 CH |
1874 | struct xfs_buf *bp, |
1875 | struct list_head *list) | |
1da177e4 | 1876 | { |
43ff2122 | 1877 | ASSERT(xfs_buf_islocked(bp)); |
5a8ee6ba | 1878 | ASSERT(!(bp->b_flags & XBF_READ)); |
1da177e4 | 1879 | |
43ff2122 CH |
1880 | /* |
1881 | * If the buffer is already marked delwri it already is queued up | |
1882 | * by someone else for imediate writeout. Just ignore it in that | |
1883 | * case. | |
1884 | */ | |
1885 | if (bp->b_flags & _XBF_DELWRI_Q) { | |
1886 | trace_xfs_buf_delwri_queued(bp, _RET_IP_); | |
1887 | return false; | |
1da177e4 | 1888 | } |
1da177e4 | 1889 | |
43ff2122 | 1890 | trace_xfs_buf_delwri_queue(bp, _RET_IP_); |
d808f617 DC |
1891 | |
1892 | /* | |
43ff2122 CH |
1893 | * If a buffer gets written out synchronously or marked stale while it |
1894 | * is on a delwri list we lazily remove it. To do this, the other party | |
1895 | * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone. | |
1896 | * It remains referenced and on the list. In a rare corner case it | |
1897 | * might get readded to a delwri list after the synchronous writeout, in | |
1898 | * which case we need just need to re-add the flag here. | |
d808f617 | 1899 | */ |
43ff2122 CH |
1900 | bp->b_flags |= _XBF_DELWRI_Q; |
1901 | if (list_empty(&bp->b_list)) { | |
1902 | atomic_inc(&bp->b_hold); | |
1903 | list_add_tail(&bp->b_list, list); | |
585e6d88 | 1904 | } |
585e6d88 | 1905 | |
43ff2122 | 1906 | return true; |
585e6d88 DC |
1907 | } |
1908 | ||
089716aa DC |
1909 | /* |
1910 | * Compare function is more complex than it needs to be because | |
1911 | * the return value is only 32 bits and we are doing comparisons | |
1912 | * on 64 bit values | |
1913 | */ | |
1914 | static int | |
1915 | xfs_buf_cmp( | |
1916 | void *priv, | |
1917 | struct list_head *a, | |
1918 | struct list_head *b) | |
1919 | { | |
1920 | struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list); | |
1921 | struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list); | |
1922 | xfs_daddr_t diff; | |
1923 | ||
f4b42421 | 1924 | diff = ap->b_maps[0].bm_bn - bp->b_maps[0].bm_bn; |
089716aa DC |
1925 | if (diff < 0) |
1926 | return -1; | |
1927 | if (diff > 0) | |
1928 | return 1; | |
1929 | return 0; | |
1930 | } | |
1931 | ||
26f1fe85 | 1932 | /* |
e339dd8d BF |
1933 | * Submit buffers for write. If wait_list is specified, the buffers are |
1934 | * submitted using sync I/O and placed on the wait list such that the caller can | |
1935 | * iowait each buffer. Otherwise async I/O is used and the buffers are released | |
1936 | * at I/O completion time. In either case, buffers remain locked until I/O | |
1937 | * completes and the buffer is released from the queue. | |
26f1fe85 | 1938 | */ |
43ff2122 | 1939 | static int |
26f1fe85 | 1940 | xfs_buf_delwri_submit_buffers( |
43ff2122 | 1941 | struct list_head *buffer_list, |
26f1fe85 | 1942 | struct list_head *wait_list) |
1da177e4 | 1943 | { |
43ff2122 | 1944 | struct xfs_buf *bp, *n; |
26f1fe85 | 1945 | LIST_HEAD (submit_list); |
43ff2122 | 1946 | int pinned = 0; |
26f1fe85 | 1947 | struct blk_plug plug; |
43ff2122 | 1948 | |
26f1fe85 | 1949 | list_sort(NULL, buffer_list, xfs_buf_cmp); |
43ff2122 | 1950 | |
26f1fe85 | 1951 | blk_start_plug(&plug); |
43ff2122 | 1952 | list_for_each_entry_safe(bp, n, buffer_list, b_list) { |
26f1fe85 | 1953 | if (!wait_list) { |
43ff2122 CH |
1954 | if (xfs_buf_ispinned(bp)) { |
1955 | pinned++; | |
1956 | continue; | |
1957 | } | |
1958 | if (!xfs_buf_trylock(bp)) | |
1959 | continue; | |
1960 | } else { | |
1961 | xfs_buf_lock(bp); | |
1962 | } | |
978c7b2f | 1963 | |
43ff2122 CH |
1964 | /* |
1965 | * Someone else might have written the buffer synchronously or | |
1966 | * marked it stale in the meantime. In that case only the | |
1967 | * _XBF_DELWRI_Q flag got cleared, and we have to drop the | |
1968 | * reference and remove it from the list here. | |
1969 | */ | |
1970 | if (!(bp->b_flags & _XBF_DELWRI_Q)) { | |
1971 | list_del_init(&bp->b_list); | |
1972 | xfs_buf_relse(bp); | |
1973 | continue; | |
1974 | } | |
c9c12971 | 1975 | |
43ff2122 | 1976 | trace_xfs_buf_delwri_split(bp, _RET_IP_); |
a1b7ea5d | 1977 | |
cf53e99d | 1978 | /* |
e339dd8d BF |
1979 | * If we have a wait list, each buffer (and associated delwri |
1980 | * queue reference) transfers to it and is submitted | |
1981 | * synchronously. Otherwise, drop the buffer from the delwri | |
1982 | * queue and submit async. | |
cf53e99d | 1983 | */ |
bbfeb614 | 1984 | bp->b_flags &= ~(_XBF_DELWRI_Q | XBF_WRITE_FAIL); |
e339dd8d | 1985 | bp->b_flags |= XBF_WRITE; |
26f1fe85 | 1986 | if (wait_list) { |
e339dd8d | 1987 | bp->b_flags &= ~XBF_ASYNC; |
26f1fe85 | 1988 | list_move_tail(&bp->b_list, wait_list); |
e339dd8d BF |
1989 | } else { |
1990 | bp->b_flags |= XBF_ASYNC; | |
ce8e922c | 1991 | list_del_init(&bp->b_list); |
e339dd8d | 1992 | } |
6af88cda | 1993 | __xfs_buf_submit(bp, false); |
43ff2122 CH |
1994 | } |
1995 | blk_finish_plug(&plug); | |
1da177e4 | 1996 | |
43ff2122 | 1997 | return pinned; |
1da177e4 LT |
1998 | } |
1999 | ||
2000 | /* | |
43ff2122 CH |
2001 | * Write out a buffer list asynchronously. |
2002 | * | |
2003 | * This will take the @buffer_list, write all non-locked and non-pinned buffers | |
2004 | * out and not wait for I/O completion on any of the buffers. This interface | |
2005 | * is only safely useable for callers that can track I/O completion by higher | |
2006 | * level means, e.g. AIL pushing as the @buffer_list is consumed in this | |
2007 | * function. | |
1da177e4 LT |
2008 | */ |
2009 | int | |
43ff2122 CH |
2010 | xfs_buf_delwri_submit_nowait( |
2011 | struct list_head *buffer_list) | |
1da177e4 | 2012 | { |
26f1fe85 | 2013 | return xfs_buf_delwri_submit_buffers(buffer_list, NULL); |
43ff2122 | 2014 | } |
1da177e4 | 2015 | |
43ff2122 CH |
2016 | /* |
2017 | * Write out a buffer list synchronously. | |
2018 | * | |
2019 | * This will take the @buffer_list, write all buffers out and wait for I/O | |
2020 | * completion on all of the buffers. @buffer_list is consumed by the function, | |
2021 | * so callers must have some other way of tracking buffers if they require such | |
2022 | * functionality. | |
2023 | */ | |
2024 | int | |
2025 | xfs_buf_delwri_submit( | |
2026 | struct list_head *buffer_list) | |
2027 | { | |
26f1fe85 | 2028 | LIST_HEAD (wait_list); |
43ff2122 CH |
2029 | int error = 0, error2; |
2030 | struct xfs_buf *bp; | |
1da177e4 | 2031 | |
26f1fe85 | 2032 | xfs_buf_delwri_submit_buffers(buffer_list, &wait_list); |
1da177e4 | 2033 | |
43ff2122 | 2034 | /* Wait for IO to complete. */ |
26f1fe85 DC |
2035 | while (!list_empty(&wait_list)) { |
2036 | bp = list_first_entry(&wait_list, struct xfs_buf, b_list); | |
a1b7ea5d | 2037 | |
089716aa | 2038 | list_del_init(&bp->b_list); |
cf53e99d | 2039 | |
e339dd8d BF |
2040 | /* |
2041 | * Wait on the locked buffer, check for errors and unlock and | |
2042 | * release the delwri queue reference. | |
2043 | */ | |
2044 | error2 = xfs_buf_iowait(bp); | |
43ff2122 CH |
2045 | xfs_buf_relse(bp); |
2046 | if (!error) | |
2047 | error = error2; | |
1da177e4 LT |
2048 | } |
2049 | ||
43ff2122 | 2050 | return error; |
1da177e4 LT |
2051 | } |
2052 | ||
7912e7fe BF |
2053 | /* |
2054 | * Push a single buffer on a delwri queue. | |
2055 | * | |
2056 | * The purpose of this function is to submit a single buffer of a delwri queue | |
2057 | * and return with the buffer still on the original queue. The waiting delwri | |
2058 | * buffer submission infrastructure guarantees transfer of the delwri queue | |
2059 | * buffer reference to a temporary wait list. We reuse this infrastructure to | |
2060 | * transfer the buffer back to the original queue. | |
2061 | * | |
2062 | * Note the buffer transitions from the queued state, to the submitted and wait | |
2063 | * listed state and back to the queued state during this call. The buffer | |
2064 | * locking and queue management logic between _delwri_pushbuf() and | |
2065 | * _delwri_queue() guarantee that the buffer cannot be queued to another list | |
2066 | * before returning. | |
2067 | */ | |
2068 | int | |
2069 | xfs_buf_delwri_pushbuf( | |
2070 | struct xfs_buf *bp, | |
2071 | struct list_head *buffer_list) | |
2072 | { | |
2073 | LIST_HEAD (submit_list); | |
2074 | int error; | |
2075 | ||
2076 | ASSERT(bp->b_flags & _XBF_DELWRI_Q); | |
2077 | ||
2078 | trace_xfs_buf_delwri_pushbuf(bp, _RET_IP_); | |
2079 | ||
2080 | /* | |
2081 | * Isolate the buffer to a new local list so we can submit it for I/O | |
2082 | * independently from the rest of the original list. | |
2083 | */ | |
2084 | xfs_buf_lock(bp); | |
2085 | list_move(&bp->b_list, &submit_list); | |
2086 | xfs_buf_unlock(bp); | |
2087 | ||
2088 | /* | |
2089 | * Delwri submission clears the DELWRI_Q buffer flag and returns with | |
e339dd8d | 2090 | * the buffer on the wait list with the original reference. Rather than |
7912e7fe BF |
2091 | * bounce the buffer from a local wait list back to the original list |
2092 | * after I/O completion, reuse the original list as the wait list. | |
2093 | */ | |
2094 | xfs_buf_delwri_submit_buffers(&submit_list, buffer_list); | |
2095 | ||
2096 | /* | |
e339dd8d BF |
2097 | * The buffer is now locked, under I/O and wait listed on the original |
2098 | * delwri queue. Wait for I/O completion, restore the DELWRI_Q flag and | |
2099 | * return with the buffer unlocked and on the original queue. | |
7912e7fe | 2100 | */ |
e339dd8d | 2101 | error = xfs_buf_iowait(bp); |
7912e7fe BF |
2102 | bp->b_flags |= _XBF_DELWRI_Q; |
2103 | xfs_buf_unlock(bp); | |
2104 | ||
2105 | return error; | |
2106 | } | |
2107 | ||
04d8b284 | 2108 | int __init |
ce8e922c | 2109 | xfs_buf_init(void) |
1da177e4 | 2110 | { |
8758280f NS |
2111 | xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf", |
2112 | KM_ZONE_HWALIGN, NULL); | |
ce8e922c | 2113 | if (!xfs_buf_zone) |
0b1b213f | 2114 | goto out; |
04d8b284 | 2115 | |
23ea4032 | 2116 | return 0; |
1da177e4 | 2117 | |
0b1b213f | 2118 | out: |
8758280f | 2119 | return -ENOMEM; |
1da177e4 LT |
2120 | } |
2121 | ||
1da177e4 | 2122 | void |
ce8e922c | 2123 | xfs_buf_terminate(void) |
1da177e4 | 2124 | { |
ce8e922c | 2125 | kmem_zone_destroy(xfs_buf_zone); |
1da177e4 | 2126 | } |
7561d27e BF |
2127 | |
2128 | void xfs_buf_set_ref(struct xfs_buf *bp, int lru_ref) | |
2129 | { | |
7561d27e BF |
2130 | /* |
2131 | * Set the lru reference count to 0 based on the error injection tag. | |
2132 | * This allows userspace to disrupt buffer caching for debug/testing | |
2133 | * purposes. | |
2134 | */ | |
4eadcf9a BF |
2135 | if (XFS_TEST_ERROR(false, bp->b_target->bt_mount, |
2136 | XFS_ERRTAG_BUF_LRU_REF)) | |
7561d27e BF |
2137 | lru_ref = 0; |
2138 | ||
2139 | atomic_set(&bp->b_lru_ref, lru_ref); | |
2140 | } |