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