Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0
2 : /*
3 : * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4 : * All Rights Reserved.
5 : */
6 : #include "xfs.h"
7 : #include <linux/backing-dev.h>
8 : #include <linux/dax.h>
9 :
10 : #include "xfs_shared.h"
11 : #include "xfs_format.h"
12 : #include "xfs_log_format.h"
13 : #include "xfs_trans_resv.h"
14 : #include "xfs_mount.h"
15 : #include "xfs_trace.h"
16 : #include "xfs_log.h"
17 : #include "xfs_log_recover.h"
18 : #include "xfs_log_priv.h"
19 : #include "xfs_trans.h"
20 : #include "xfs_buf_item.h"
21 : #include "xfs_errortag.h"
22 : #include "xfs_error.h"
23 : #include "xfs_ag.h"
24 : #include "xfs_buf_xfile.h"
25 :
26 : struct kmem_cache *xfs_buf_cache;
27 :
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 : *
46 : * xfs_buftarg_drain_rele
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 : */
54 :
55 : static int __xfs_buf_submit(struct xfs_buf *bp, bool wait);
56 :
57 : static inline int
58 31715205 : xfs_buf_submit(
59 : struct xfs_buf *bp)
60 : {
61 31715205 : return __xfs_buf_submit(bp, !(bp->b_flags & XBF_ASYNC));
62 : }
63 :
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 : *
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.
74 : */
75 71958926 : return bp->b_addr && bp->b_page_count > 1;
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);
83 : }
84 :
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
91 : * with unmount (see xfs_buftarg_drain()), so all we really need is a count of
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 120746939 : xfs_buf_ioacct_inc(
100 : struct xfs_buf *bp)
101 : {
102 120746939 : if (bp->b_flags & XBF_NO_IOACCT)
103 : return;
104 :
105 120444779 : ASSERT(bp->b_flags & XBF_ASYNC);
106 120444779 : spin_lock(&bp->b_lock);
107 120444837 : if (!(bp->b_state & XFS_BSTATE_IN_FLIGHT)) {
108 114184716 : bp->b_state |= XFS_BSTATE_IN_FLIGHT;
109 114184716 : percpu_counter_inc(&bp->b_target->bt_io_count);
110 : }
111 120444707 : spin_unlock(&bp->b_lock);
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
119 11520485380 : __xfs_buf_ioacct_dec(
120 : struct xfs_buf *bp)
121 : {
122 11520485380 : lockdep_assert_held(&bp->b_lock);
123 :
124 11520485380 : if (bp->b_state & XFS_BSTATE_IN_FLIGHT) {
125 114184635 : bp->b_state &= ~XFS_BSTATE_IN_FLIGHT;
126 114184635 : percpu_counter_dec(&bp->b_target->bt_io_count);
127 : }
128 11520485321 : }
129 :
130 : static inline void
131 493965 : xfs_buf_ioacct_dec(
132 : struct xfs_buf *bp)
133 : {
134 493965 : spin_lock(&bp->b_lock);
135 493965 : __xfs_buf_ioacct_dec(bp);
136 493965 : spin_unlock(&bp->b_lock);
137 493965 : }
138 :
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 37537119 : xfs_buf_stale(
149 : struct xfs_buf *bp)
150 : {
151 37537119 : ASSERT(xfs_buf_islocked(bp));
152 :
153 37537119 : bp->b_flags |= XBF_STALE;
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 37537119 : bp->b_flags &= ~_XBF_DELWRI_Q;
161 :
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 : */
168 37537119 : spin_lock(&bp->b_lock);
169 37567479 : __xfs_buf_ioacct_dec(bp);
170 :
171 37545867 : atomic_set(&bp->b_lru_ref, 0);
172 75127951 : if (!(bp->b_state & XFS_BSTATE_DISPOSE) &&
173 37545349 : (list_lru_del(&bp->b_target->bt_lru, &bp->b_lru)))
174 7404980 : atomic_dec(&bp->b_hold);
175 :
176 37582637 : ASSERT(atomic_read(&bp->b_hold) >= 1);
177 37582637 : spin_unlock(&bp->b_lock);
178 37581248 : }
179 :
180 : static int
181 72923905 : xfs_buf_get_maps(
182 : struct xfs_buf *bp,
183 : int map_count)
184 : {
185 72923905 : ASSERT(bp->b_maps == NULL);
186 72923905 : bp->b_map_count = map_count;
187 :
188 72923905 : if (map_count == 1) {
189 72923736 : bp->b_maps = &bp->__b_map;
190 72923736 : return 0;
191 : }
192 :
193 169 : bp->b_maps = kmem_zalloc(map_count * sizeof(struct xfs_buf_map),
194 : KM_NOFS);
195 169 : if (!bp->b_maps)
196 0 : return -ENOMEM;
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 : {
207 72878930 : if (bp->b_maps != &bp->__b_map) {
208 169 : kmem_free(bp->b_maps);
209 169 : bp->b_maps = NULL;
210 : }
211 : }
212 :
213 : static int
214 72907768 : _xfs_buf_alloc(
215 : struct xfs_buftarg *target,
216 : struct xfs_buf_map *map,
217 : int nmaps,
218 : xfs_buf_flags_t flags,
219 : struct xfs_buf **bpp)
220 : {
221 72907768 : struct xfs_buf *bp;
222 72907768 : int error;
223 72907768 : int i;
224 :
225 72907768 : *bpp = NULL;
226 72907768 : bp = kmem_cache_zalloc(xfs_buf_cache, GFP_NOFS | __GFP_NOFAIL);
227 :
228 : /*
229 : * We don't want certain flags to appear in b_flags unless they are
230 : * specifically set by later operations on the buffer.
231 : */
232 72937541 : flags &= ~(XBF_UNMAPPED | XBF_TRYLOCK | XBF_ASYNC | XBF_READ_AHEAD);
233 :
234 72937541 : atomic_set(&bp->b_hold, 1);
235 72937541 : atomic_set(&bp->b_lru_ref, 1);
236 72937541 : init_completion(&bp->b_iowait);
237 72927206 : INIT_LIST_HEAD(&bp->b_lru);
238 72927206 : INIT_LIST_HEAD(&bp->b_list);
239 72927206 : INIT_LIST_HEAD(&bp->b_li_list);
240 72927206 : sema_init(&bp->b_sema, 0); /* held, no waiters */
241 72927206 : spin_lock_init(&bp->b_lock);
242 72923792 : bp->b_target = target;
243 72923792 : bp->b_mount = target->bt_mount;
244 72923792 : bp->b_flags = flags;
245 :
246 : /*
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
249 : * most cases but may be reset (e.g. XFS recovery).
250 : */
251 72923792 : error = xfs_buf_get_maps(bp, nmaps);
252 72913613 : if (error) {
253 0 : kmem_cache_free(xfs_buf_cache, bp);
254 0 : return error;
255 : }
256 :
257 72913613 : bp->b_rhash_key = map[0].bm_bn;
258 72913613 : bp->b_length = 0;
259 145822468 : for (i = 0; i < nmaps; i++) {
260 72908855 : bp->b_maps[i].bm_bn = map[i].bm_bn;
261 72908855 : bp->b_maps[i].bm_len = map[i].bm_len;
262 72908855 : bp->b_length += map[i].bm_len;
263 : }
264 :
265 72913613 : atomic_set(&bp->b_pin_count, 0);
266 72913613 : init_waitqueue_head(&bp->b_waiters);
267 :
268 72917570 : XFS_STATS_INC(bp->b_mount, xb_create);
269 72918867 : trace_xfs_buf_init(bp, _RET_IP_);
270 :
271 72916824 : *bpp = bp;
272 72916824 : return 0;
273 : }
274 :
275 : static void
276 72175489 : xfs_buf_free_pages(
277 : struct xfs_buf *bp)
278 : {
279 72175489 : uint i;
280 :
281 72175489 : ASSERT(bp->b_flags & _XBF_PAGES);
282 :
283 189620452 : for (i = 0; i < bp->b_page_count; i++) {
284 117445175 : if (bp->b_pages[i])
285 117445175 : __free_page(bp->b_pages[i]);
286 : }
287 72175277 : mm_account_reclaimed_pages(bp->b_page_count);
288 :
289 72175276 : xfs_buf_free_page_array(bp);
290 72175083 : }
291 :
292 : void
293 72645645 : xfs_buf_free_page_array(
294 : struct xfs_buf *bp)
295 : {
296 72645645 : ASSERT(bp->b_flags & _XBF_PAGES);
297 :
298 72645645 : if (bp->b_pages != bp->b_page_array)
299 15047182 : kmem_free(bp->b_pages);
300 72645645 : bp->b_pages = NULL;
301 72645645 : bp->b_flags &= ~_XBF_PAGES;
302 72645645 : bp->b_page_count = 0;
303 72645645 : }
304 :
305 : static void
306 72878930 : xfs_buf_free_callback(
307 : struct callback_head *cb)
308 : {
309 72878930 : struct xfs_buf *bp = container_of(cb, struct xfs_buf, b_rcu);
310 :
311 72878930 : xfs_buf_free_maps(bp);
312 72878930 : kmem_cache_free(xfs_buf_cache, bp);
313 72903118 : }
314 :
315 : static void
316 72948986 : xfs_buf_free(
317 : struct xfs_buf *bp)
318 : {
319 72948986 : trace_xfs_buf_free(bp, _RET_IP_);
320 :
321 72948794 : ASSERT(list_empty(&bp->b_lru));
322 :
323 72948794 : if (xfs_buf_is_vmapped(bp))
324 14143297 : vm_unmap_ram(bp->b_addr - bp->b_offset, bp->b_page_count);
325 :
326 72948767 : if (bp->b_flags & _XBF_DIRECT_MAP)
327 470531 : xfile_buf_unmap_pages(bp);
328 72478236 : else if (bp->b_flags & _XBF_PAGES)
329 72175488 : xfs_buf_free_pages(bp);
330 302748 : else if (bp->b_flags & _XBF_KMEM)
331 302747 : kmem_free(bp->b_addr);
332 :
333 72948451 : call_rcu(&bp->b_rcu, xfs_buf_free_callback);
334 72947983 : }
335 :
336 : static int
337 302723 : xfs_buf_alloc_kmem(
338 : struct xfs_buf *bp,
339 : xfs_buf_flags_t flags)
340 : {
341 302723 : xfs_km_flags_t kmflag_mask = KM_NOFS;
342 302723 : size_t size = BBTOB(bp->b_length);
343 :
344 : /* Assure zeroed buffer for non-read cases. */
345 302723 : if (!(flags & XBF_READ))
346 26140 : kmflag_mask |= KM_ZERO;
347 :
348 302723 : bp->b_addr = kmem_alloc(size, kmflag_mask);
349 302736 : if (!bp->b_addr)
350 : return -ENOMEM;
351 :
352 302736 : if (((unsigned long)(bp->b_addr + size - 1) & PAGE_MASK) !=
353 : ((unsigned long)bp->b_addr & PAGE_MASK)) {
354 : /* b_addr spans two pages - use alloc_page instead */
355 0 : kmem_free(bp->b_addr);
356 0 : bp->b_addr = NULL;
357 0 : return -ENOMEM;
358 : }
359 302736 : bp->b_offset = offset_in_page(bp->b_addr);
360 302736 : bp->b_pages = bp->b_page_array;
361 302736 : bp->b_pages[0] = kmem_to_page(bp->b_addr);
362 302707 : bp->b_page_count = 1;
363 302707 : bp->b_flags |= _XBF_KMEM;
364 302707 : return 0;
365 : }
366 :
367 : /* Make sure that we have a page list */
368 : int
369 72583409 : xfs_buf_alloc_page_array(
370 : struct xfs_buf *bp,
371 : gfp_t gfp_mask)
372 : {
373 72583409 : ASSERT(!(bp->b_flags & _XBF_PAGES));
374 :
375 72583409 : bp->b_page_count = DIV_ROUND_UP(BBTOB(bp->b_length), PAGE_SIZE);
376 72583409 : if (bp->b_page_count <= XB_PAGES) {
377 57538195 : bp->b_pages = bp->b_page_array;
378 : } else {
379 15045214 : bp->b_pages = kzalloc(sizeof(struct page *) * bp->b_page_count,
380 : gfp_mask);
381 15045054 : if (!bp->b_pages)
382 : return -ENOMEM;
383 : }
384 :
385 72583249 : bp->b_flags |= _XBF_PAGES;
386 72583249 : return 0;
387 : }
388 :
389 : static int
390 72124049 : xfs_buf_alloc_pages(
391 : struct xfs_buf *bp,
392 : xfs_buf_flags_t flags)
393 : {
394 72124049 : gfp_t gfp_mask = __GFP_NOWARN;
395 72124049 : long filled = 0;
396 72124049 : int error;
397 :
398 72124049 : if (flags & XBF_READ_AHEAD)
399 : gfp_mask |= __GFP_NORETRY;
400 : else
401 51492256 : gfp_mask |= GFP_NOFS;
402 :
403 72124049 : error = xfs_buf_alloc_page_array(bp, gfp_mask);
404 72091941 : if (error)
405 : return error;
406 :
407 : /* Assure zeroed buffer for non-read cases. */
408 72091941 : if (!(flags & XBF_READ))
409 41090228 : gfp_mask |= __GFP_ZERO;
410 :
411 : /*
412 : * Bulk filling of pages can take multiple calls. Not filling the entire
413 : * array is not an allocation failure, so don't back off if we get at
414 : * least one extra page.
415 : */
416 72092030 : for (;;) {
417 72092030 : long last = filled;
418 :
419 72092030 : filled = alloc_pages_bulk_array(gfp_mask, bp->b_page_count,
420 : bp->b_pages);
421 72134026 : if (filled == bp->b_page_count) {
422 72133937 : XFS_STATS_INC(bp->b_mount, xb_page_found);
423 72124788 : break;
424 : }
425 :
426 89 : if (filled != last)
427 89 : continue;
428 :
429 0 : if (flags & XBF_READ_AHEAD) {
430 0 : xfs_buf_free_pages(bp);
431 0 : return -ENOMEM;
432 : }
433 :
434 0 : XFS_STATS_INC(bp->b_mount, xb_page_retries);
435 0 : memalloc_retry_wait(gfp_mask);
436 : }
437 :
438 72124788 : bp->b_offset = 0;
439 72124788 : return 0;
440 : }
441 :
442 : /*
443 : * Map buffer into kernel address-space if necessary.
444 : */
445 : STATIC int
446 397850737 : _xfs_buf_map_pages(
447 : struct xfs_buf *bp,
448 : xfs_buf_flags_t flags)
449 : {
450 397850737 : ASSERT(bp->b_flags & (_XBF_PAGES | _XBF_DIRECT_MAP));
451 :
452 397850737 : if (bp->b_page_count == 1) {
453 : /* A single page buffer is always mappable */
454 57432012 : bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
455 340418725 : } else if (flags & XBF_UNMAPPED) {
456 326275592 : bp->b_addr = NULL;
457 : } else {
458 14143133 : int retried = 0;
459 14143133 : unsigned nofs_flag;
460 :
461 : /*
462 : * vm_map_ram() will allocate auxiliary structures (e.g.
463 : * pagetables) with GFP_KERNEL, yet we are likely to be under
464 : * GFP_NOFS context here. Hence we need to tell memory reclaim
465 : * that we are in such a context via PF_MEMALLOC_NOFS to prevent
466 : * memory reclaim re-entering the filesystem here and
467 : * potentially deadlocking.
468 : */
469 14143133 : nofs_flag = memalloc_nofs_save();
470 14143148 : do {
471 14143148 : bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
472 : -1);
473 14143126 : if (bp->b_addr)
474 : break;
475 0 : vm_unmap_aliases();
476 0 : } while (retried++ <= 1);
477 14143111 : memalloc_nofs_restore(nofs_flag);
478 :
479 14143111 : if (!bp->b_addr)
480 : return -ENOMEM;
481 :
482 14143111 : bp->b_addr += bp->b_offset;
483 : }
484 :
485 : return 0;
486 : }
487 :
488 : /*
489 : * Finding and Reading Buffers
490 : */
491 : static int
492 38779040328 : _xfs_buf_obj_cmp(
493 : struct rhashtable_compare_arg *arg,
494 : const void *obj)
495 : {
496 38779040328 : const struct xfs_buf_map *map = arg->key;
497 38779040328 : const struct xfs_buf *bp = obj;
498 :
499 : /*
500 : * The key hashing in the lookup path depends on the key being the
501 : * first element of the compare_arg, make sure to assert this.
502 : */
503 38779040328 : BUILD_BUG_ON(offsetof(struct xfs_buf_map, bm_bn) != 0);
504 :
505 38779040328 : if (bp->b_rhash_key != map->bm_bn)
506 : return 1;
507 :
508 30541635291 : if (unlikely(bp->b_length != map->bm_len)) {
509 : /*
510 : * found a block number match. If the range doesn't
511 : * match, the only way this is allowed is if the buffer
512 : * in the cache is stale and the transaction that made
513 : * it stale has not yet committed. i.e. we are
514 : * reallocating a busy extent. Skip this buffer and
515 : * continue searching for an exact match.
516 : *
517 : * Note: If we're scanning for incore buffers to stale, don't
518 : * complain if we find non-stale buffers.
519 : */
520 7727419 : if (!(map->bm_flags & XBM_LIVESCAN))
521 27 : ASSERT(bp->b_flags & XBF_STALE);
522 7727419 : return 1;
523 : }
524 : return 0;
525 : }
526 :
527 : static const struct rhashtable_params xfs_buf_hash_params = {
528 : .min_size = 32, /* empty AGs have minimal footprint */
529 : .nelem_hint = 16,
530 : .key_len = sizeof(xfs_daddr_t),
531 : .key_offset = offsetof(struct xfs_buf, b_rhash_key),
532 : .head_offset = offsetof(struct xfs_buf, b_rhash_head),
533 : .automatic_shrinking = true,
534 : .obj_cmpfn = _xfs_buf_obj_cmp,
535 : };
536 :
537 : int
538 499829 : xfs_buf_cache_init(
539 : struct xfs_buf_cache *bch)
540 : {
541 499829 : spin_lock_init(&bch->bc_lock);
542 499828 : return rhashtable_init(&bch->bc_hash, &xfs_buf_hash_params);
543 : }
544 :
545 : void
546 499943 : xfs_buf_cache_destroy(
547 : struct xfs_buf_cache *bch)
548 : {
549 499943 : rhashtable_destroy(&bch->bc_hash);
550 499768 : }
551 :
552 : static int
553 30622322426 : xfs_buf_map_verify(
554 : struct xfs_buftarg *btp,
555 : struct xfs_buf_map *map)
556 : {
557 30622322426 : xfs_daddr_t eofs;
558 :
559 : /* Check for IOs smaller than the sector size / not sector aligned */
560 30622322426 : ASSERT(!(BBTOB(map->bm_len) < btp->bt_meta_sectorsize));
561 30622322426 : ASSERT(!(BBTOB(map->bm_bn) & (xfs_off_t)btp->bt_meta_sectormask));
562 :
563 : /*
564 : * Corrupted block numbers can get through to here, unfortunately, so we
565 : * have to check that the buffer falls within the filesystem bounds.
566 : */
567 30622322426 : eofs = XFS_FSB_TO_BB(btp->bt_mount, btp->bt_mount->m_sb.sb_dblocks);
568 30622322426 : if (map->bm_bn < 0 || map->bm_bn >= eofs) {
569 0 : xfs_alert(btp->bt_mount,
570 : "%s: daddr 0x%llx out of range, EOFS 0x%llx",
571 : __func__, map->bm_bn, eofs);
572 0 : WARN_ON(1);
573 0 : return -EFSCORRUPTED;
574 : }
575 : return 0;
576 : }
577 :
578 : static int
579 30554775081 : xfs_buf_find_lock(
580 : struct xfs_buf *bp,
581 : xfs_buf_flags_t flags)
582 : {
583 30554775081 : if (flags & XBF_TRYLOCK) {
584 6426364369 : if (!xfs_buf_trylock(bp)) {
585 131250888 : XFS_STATS_INC(bp->b_mount, xb_busy_locked);
586 131252588 : return -EAGAIN;
587 : }
588 : } else {
589 24128410712 : xfs_buf_lock(bp);
590 24112413160 : XFS_STATS_INC(bp->b_mount, xb_get_locked_waited);
591 : }
592 :
593 : /*
594 : * if the buffer is stale, clear all the external state associated with
595 : * it. We need to keep flags such as how we allocated the buffer memory
596 : * intact here.
597 : */
598 30412328834 : if (bp->b_flags & XBF_STALE) {
599 48169 : if (flags & XBF_LIVESCAN) {
600 5 : xfs_buf_unlock(bp);
601 5 : return -ENOENT;
602 : }
603 48164 : ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
604 48164 : bp->b_flags &= _XBF_KMEM | _XBF_PAGES | _XBF_DIRECT_MAP;
605 48164 : bp->b_ops = NULL;
606 : }
607 : return 0;
608 : }
609 :
610 : static inline int
611 30628700557 : xfs_buf_lookup(
612 : struct xfs_buf_cache *bch,
613 : struct xfs_buf_map *map,
614 : xfs_buf_flags_t flags,
615 : struct xfs_buf **bpp)
616 : {
617 30628700557 : struct xfs_buf *bp;
618 30628700557 : int error;
619 :
620 30628700557 : rcu_read_lock();
621 30627531217 : bp = rhashtable_lookup(&bch->bc_hash, map, xfs_buf_hash_params);
622 61174485357 : if (!bp || !atomic_inc_not_zero(&bp->b_hold)) {
623 82733181 : rcu_read_unlock();
624 82733181 : return -ENOENT;
625 : }
626 30557838849 : rcu_read_unlock();
627 :
628 30556046262 : error = xfs_buf_find_lock(bp, flags);
629 30536892178 : if (error) {
630 131253226 : xfs_buf_rele(bp);
631 131253226 : return error;
632 : }
633 :
634 30405638952 : trace_xfs_buf_find(bp, flags, _RET_IP_);
635 30403736930 : *bpp = bp;
636 30403736930 : return 0;
637 : }
638 :
639 : /*
640 : * Insert the new_bp into the hash table. This consumes the perag reference
641 : * taken for the lookup regardless of the result of the insert.
642 : */
643 : static int
644 72415478 : xfs_buf_find_insert(
645 : struct xfs_buftarg *btp,
646 : struct xfs_buf_cache *bch,
647 : struct xfs_perag *pag,
648 : struct xfs_buf_map *cmap,
649 : struct xfs_buf_map *map,
650 : int nmaps,
651 : xfs_buf_flags_t flags,
652 : struct xfs_buf **bpp)
653 : {
654 72415478 : struct xfs_buf *new_bp;
655 72415478 : struct xfs_buf *bp;
656 72415478 : int error;
657 :
658 72415478 : error = _xfs_buf_alloc(btp, map, nmaps, flags, &new_bp);
659 72408545 : if (error)
660 0 : goto out_drop_pag;
661 :
662 : /*
663 : * If the caller is ok with direct maps to xfile pages, try that.
664 : * ENOTBLK is the magic code to fall back to allocating memory.
665 : */
666 72408545 : if (xfile_buftarg_can_direct_map(btp)) {
667 470603 : error = xfile_buf_map_pages(new_bp, flags);
668 470605 : if (error && error != -ENOTBLK)
669 0 : goto out_free_buf;
670 470605 : if (!error)
671 470606 : goto insert;
672 : }
673 :
674 : /*
675 : * For buffers that fit entirely within a single page, first attempt to
676 : * allocate the memory from the heap to minimise memory usage.
677 : */
678 71937941 : if (BBTOB(new_bp->b_length) < PAGE_SIZE) {
679 302720 : error = xfs_buf_alloc_kmem(new_bp, flags);
680 302713 : if (!error)
681 302711 : goto insert;
682 : }
683 :
684 : /*
685 : * For larger buffers or if we can't get heap memory for these small
686 : * buffers, fall back to using the page allocator.
687 : */
688 71635223 : error = xfs_buf_alloc_pages(new_bp, flags);
689 71611654 : if (error)
690 0 : goto out_free_buf;
691 :
692 71611654 : insert:
693 72384971 : spin_lock(&bch->bc_lock);
694 72425155 : bp = rhashtable_lookup_get_insert_fast(&bch->bc_hash,
695 72425155 : &new_bp->b_rhash_head, xfs_buf_hash_params);
696 72397617 : if (IS_ERR(bp)) {
697 0 : error = PTR_ERR(bp);
698 0 : spin_unlock(&bch->bc_lock);
699 0 : goto out_free_buf;
700 : }
701 72397617 : if (bp) {
702 : /* found an existing buffer */
703 1801 : atomic_inc(&bp->b_hold);
704 1801 : spin_unlock(&bch->bc_lock);
705 1801 : error = xfs_buf_find_lock(bp, flags);
706 1801 : if (error)
707 0 : xfs_buf_rele(bp);
708 : else
709 1801 : *bpp = bp;
710 1801 : goto out_free_buf;
711 : }
712 :
713 : /* The new buffer keeps the perag reference until it is freed. */
714 72395816 : new_bp->b_pag = pag;
715 72395816 : new_bp->b_cache = bch;
716 72395816 : spin_unlock(&bch->bc_lock);
717 72441700 : *bpp = new_bp;
718 72441700 : return 0;
719 :
720 1801 : out_free_buf:
721 1801 : xfs_buf_free(new_bp);
722 1801 : out_drop_pag:
723 1801 : if (pag)
724 1801 : xfs_perag_put(pag);
725 : return error;
726 : }
727 :
728 : /* Find the buffer cache for a particular buftarg and map. */
729 : static inline struct xfs_buf_cache *
730 30616802152 : xfs_buftarg_get_cache(
731 : struct xfs_buftarg *btp,
732 : const struct xfs_buf_map *map,
733 : struct xfs_perag **pagp)
734 : {
735 30616802152 : struct xfs_mount *mp = btp->bt_mount;
736 :
737 30616802152 : if (btp->bt_cache) {
738 1078776377 : *pagp = NULL;
739 1078776377 : return btp->bt_cache;
740 : }
741 :
742 29538025775 : *pagp = xfs_perag_get(mp, xfs_daddr_to_agno(mp, map->bm_bn));
743 29556873176 : ASSERT(*pagp != NULL);
744 29556873176 : return &(*pagp)->pag_bcache;
745 : }
746 :
747 : /*
748 : * Assembles a buffer covering the specified range. The code is optimised for
749 : * cache hits, as metadata intensive workloads will see 3 orders of magnitude
750 : * more hits than misses.
751 : */
752 : int
753 30625524089 : xfs_buf_get_map(
754 : struct xfs_buftarg *btp,
755 : struct xfs_buf_map *map,
756 : int nmaps,
757 : xfs_buf_flags_t flags,
758 : struct xfs_buf **bpp)
759 : {
760 30625524089 : struct xfs_buf_cache *bch;
761 30625524089 : struct xfs_perag *pag;
762 30625524089 : struct xfs_buf *bp = NULL;
763 30625524089 : struct xfs_buf_map cmap = { .bm_bn = map[0].bm_bn };
764 30625524089 : int error;
765 30625524089 : int i;
766 :
767 30625524089 : if (flags & XBF_LIVESCAN)
768 11434873 : cmap.bm_flags |= XBM_LIVESCAN;
769 61246195989 : for (i = 0; i < nmaps; i++)
770 30620671900 : cmap.bm_len += map[i].bm_len;
771 :
772 30625524089 : error = xfs_buf_map_verify(btp, &cmap);
773 30614934155 : if (error)
774 : return error;
775 :
776 30615936370 : bch = xfs_buftarg_get_cache(btp, &cmap, &pag);
777 :
778 30633610592 : error = xfs_buf_lookup(bch, &cmap, flags, &bp);
779 30616945634 : if (error && error != -ENOENT)
780 131258403 : goto out_put_perag;
781 :
782 : /* cache hits always outnumber misses by at least 10:1 */
783 30485687231 : if (unlikely(!bp)) {
784 82722850 : XFS_STATS_INC(btp->bt_mount, xb_miss_locked);
785 :
786 82735273 : if (flags & XBF_INCORE)
787 10296980 : goto out_put_perag;
788 :
789 : /* xfs_buf_find_insert() consumes the perag reference. */
790 72438293 : error = xfs_buf_find_insert(btp, bch, pag, &cmap, map, nmaps,
791 : flags, &bp);
792 72440417 : if (error)
793 : return error;
794 : } else {
795 30402964381 : XFS_STATS_INC(btp->bt_mount, xb_get_locked);
796 30406261790 : if (pag)
797 29327948925 : xfs_perag_put(pag);
798 : }
799 :
800 : /* We do not hold a perag reference anymore. */
801 30496626267 : if (!bp->b_addr) {
802 397882070 : error = _xfs_buf_map_pages(bp, flags);
803 397247193 : if (unlikely(error)) {
804 0 : xfs_warn_ratelimited(btp->bt_mount,
805 : "%s: failed to map %u pages", __func__,
806 : bp->b_page_count);
807 0 : xfs_buf_relse(bp);
808 0 : return error;
809 : }
810 : }
811 :
812 : /*
813 : * Clear b_error if this is a lookup from a caller that doesn't expect
814 : * valid data to be found in the buffer.
815 : */
816 30495991390 : if (!(flags & XBF_READ))
817 67099882 : xfs_buf_ioerror(bp, 0);
818 :
819 30495898561 : XFS_STATS_INC(btp->bt_mount, xb_get);
820 30492364947 : trace_xfs_buf_get(bp, flags, _RET_IP_);
821 30485848001 : *bpp = bp;
822 30485848001 : return 0;
823 :
824 141555383 : out_put_perag:
825 141555383 : if (pag)
826 141554934 : xfs_perag_put(pag);
827 : return error;
828 : }
829 :
830 : int
831 31315138 : _xfs_buf_read(
832 : struct xfs_buf *bp,
833 : xfs_buf_flags_t flags)
834 : {
835 31315138 : ASSERT(!(flags & XBF_WRITE));
836 31315138 : ASSERT(bp->b_maps[0].bm_bn != XFS_BUF_DADDR_NULL);
837 :
838 31315138 : bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD | XBF_DONE);
839 31315138 : bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
840 :
841 31315138 : return xfs_buf_submit(bp);
842 : }
843 :
844 : /*
845 : * Reverify a buffer found in cache without an attached ->b_ops.
846 : *
847 : * If the caller passed an ops structure and the buffer doesn't have ops
848 : * assigned, set the ops and use it to verify the contents. If verification
849 : * fails, clear XBF_DONE. We assume the buffer has no recorded errors and is
850 : * already in XBF_DONE state on entry.
851 : *
852 : * Under normal operations, every in-core buffer is verified on read I/O
853 : * completion. There are two scenarios that can lead to in-core buffers without
854 : * an assigned ->b_ops. The first is during log recovery of buffers on a V4
855 : * filesystem, though these buffers are purged at the end of recovery. The
856 : * other is online repair, which intentionally reads with a NULL buffer ops to
857 : * run several verifiers across an in-core buffer in order to establish buffer
858 : * type. If repair can't establish that, the buffer will be left in memory
859 : * with NULL buffer ops.
860 : */
861 : int
862 31539700164 : xfs_buf_reverify(
863 : struct xfs_buf *bp,
864 : const struct xfs_buf_ops *ops)
865 : {
866 31539700164 : ASSERT(bp->b_flags & XBF_DONE);
867 31539700164 : ASSERT(bp->b_error == 0);
868 :
869 31539700164 : if (!ops || bp->b_ops)
870 : return 0;
871 :
872 189 : bp->b_ops = ops;
873 189 : bp->b_ops->verify_read(bp);
874 189 : if (bp->b_error)
875 189 : bp->b_flags &= ~XBF_DONE;
876 : return bp->b_error;
877 : }
878 :
879 : int
880 30545006058 : xfs_buf_read_map(
881 : struct xfs_buftarg *target,
882 : struct xfs_buf_map *map,
883 : int nmaps,
884 : xfs_buf_flags_t flags,
885 : struct xfs_buf **bpp,
886 : const struct xfs_buf_ops *ops,
887 : xfs_failaddr_t fa)
888 : {
889 30545006058 : struct xfs_buf *bp;
890 30545006058 : int error;
891 :
892 30545006058 : flags |= XBF_READ;
893 30545006058 : *bpp = NULL;
894 :
895 30545006058 : error = xfs_buf_get_map(target, map, nmaps, flags, &bp);
896 30550043836 : if (error)
897 : return error;
898 :
899 30420060929 : trace_xfs_buf_read(bp, flags, _RET_IP_);
900 :
901 30413301063 : if (!(bp->b_flags & XBF_DONE)) {
902 : /* Initiate the buffer read and wait. */
903 31301602 : XFS_STATS_INC(target->bt_mount, xb_get_read);
904 31301666 : bp->b_ops = ops;
905 31301666 : error = _xfs_buf_read(bp, flags);
906 :
907 : /* Readahead iodone already dropped the buffer, so exit. */
908 30875224 : if (flags & XBF_ASYNC)
909 : return 0;
910 : } else {
911 : /* Buffer already read; all we need to do is check it. */
912 30381999461 : error = xfs_buf_reverify(bp, ops);
913 :
914 : /* Readahead already finished; drop the buffer and exit. */
915 30385429578 : if (flags & XBF_ASYNC) {
916 6192498398 : xfs_buf_relse(bp);
917 6192502693 : return 0;
918 : }
919 :
920 : /* We do not want read in the flags */
921 24192931180 : bp->b_flags &= ~XBF_READ;
922 24192931180 : ASSERT(bp->b_ops != NULL || ops == NULL);
923 : }
924 :
925 : /*
926 : * If we've had a read error, then the contents of the buffer are
927 : * invalid and should not be used. To ensure that a followup read tries
928 : * to pull the buffer from disk again, we clear the XBF_DONE flag and
929 : * mark the buffer stale. This ensures that anyone who has a current
930 : * reference to the buffer will interpret it's contents correctly and
931 : * future cache lookups will also treat it as an empty, uninitialised
932 : * buffer.
933 : */
934 24203164580 : if (error) {
935 : /*
936 : * Check against log shutdown for error reporting because
937 : * metadata writeback may require a read first and we need to
938 : * report errors in metadata writeback until the log is shut
939 : * down. High level transaction read functions already check
940 : * against mount shutdown, anyway, so we only need to be
941 : * concerned about low level IO interactions here.
942 : */
943 72190 : if (!xlog_is_shutdown(target->bt_mount->m_log))
944 20700 : xfs_buf_ioerror_alert(bp, fa);
945 :
946 36112 : bp->b_flags &= ~XBF_DONE;
947 36112 : xfs_buf_stale(bp);
948 36109 : xfs_buf_relse(bp);
949 :
950 : /* bad CRC means corrupted metadata */
951 36106 : if (error == -EFSBADCRC)
952 2292 : error = -EFSCORRUPTED;
953 36106 : return error;
954 : }
955 :
956 24203128485 : *bpp = bp;
957 24203128485 : return 0;
958 : }
959 :
960 : /*
961 : * If we are not low on memory then do the readahead in a deadlock
962 : * safe manner.
963 : */
964 : void
965 6338203497 : xfs_buf_readahead_map(
966 : struct xfs_buftarg *target,
967 : struct xfs_buf_map *map,
968 : int nmaps,
969 : const struct xfs_buf_ops *ops)
970 : {
971 6338203497 : struct xfs_buf *bp;
972 :
973 12676383823 : xfs_buf_read_map(target, map, nmaps,
974 : XBF_TRYLOCK | XBF_ASYNC | XBF_READ_AHEAD, &bp, ops,
975 6338203497 : __this_address);
976 6339587656 : }
977 :
978 : /*
979 : * Read an uncached buffer from disk. Allocates and returns a locked
980 : * buffer containing the disk contents or nothing. Uncached buffers always have
981 : * a cache index of XFS_BUF_DADDR_NULL so we can easily determine if the buffer
982 : * is cached or uncached during fault diagnosis.
983 : */
984 : int
985 218988 : xfs_buf_read_uncached(
986 : struct xfs_buftarg *target,
987 : xfs_daddr_t daddr,
988 : size_t numblks,
989 : xfs_buf_flags_t flags,
990 : struct xfs_buf **bpp,
991 : const struct xfs_buf_ops *ops)
992 : {
993 218988 : struct xfs_buf *bp;
994 218988 : int error;
995 :
996 218988 : *bpp = NULL;
997 :
998 218988 : error = xfs_buf_get_uncached(target, numblks, flags, &bp);
999 218988 : if (error)
1000 : return error;
1001 :
1002 : /* set up the buffer for a read IO */
1003 218988 : ASSERT(bp->b_map_count == 1);
1004 218988 : bp->b_rhash_key = XFS_BUF_DADDR_NULL;
1005 218988 : bp->b_maps[0].bm_bn = daddr;
1006 218988 : bp->b_flags |= XBF_READ;
1007 218988 : bp->b_ops = ops;
1008 :
1009 218988 : xfs_buf_submit(bp);
1010 218988 : if (bp->b_error) {
1011 585 : error = bp->b_error;
1012 585 : xfs_buf_relse(bp);
1013 585 : return error;
1014 : }
1015 :
1016 218403 : *bpp = bp;
1017 218403 : return 0;
1018 : }
1019 :
1020 : int
1021 493955 : xfs_buf_get_uncached(
1022 : struct xfs_buftarg *target,
1023 : size_t numblks,
1024 : xfs_buf_flags_t flags,
1025 : struct xfs_buf **bpp)
1026 : {
1027 493955 : int error;
1028 493955 : struct xfs_buf *bp;
1029 493955 : DEFINE_SINGLE_BUF_MAP(map, XFS_BUF_DADDR_NULL, numblks);
1030 :
1031 493955 : *bpp = NULL;
1032 :
1033 : /* flags might contain irrelevant bits, pass only what we care about */
1034 493955 : error = _xfs_buf_alloc(target, &map, 1, flags & XBF_NO_IOACCT, &bp);
1035 493955 : if (error)
1036 : return error;
1037 :
1038 493955 : error = xfs_buf_alloc_pages(bp, flags);
1039 493955 : if (error)
1040 0 : goto fail_free_buf;
1041 :
1042 493955 : error = _xfs_buf_map_pages(bp, 0);
1043 493955 : if (unlikely(error)) {
1044 0 : xfs_warn(target->bt_mount,
1045 : "%s: failed to map pages", __func__);
1046 0 : goto fail_free_buf;
1047 : }
1048 :
1049 493955 : trace_xfs_buf_get_uncached(bp, _RET_IP_);
1050 493955 : *bpp = bp;
1051 493955 : return 0;
1052 :
1053 0 : fail_free_buf:
1054 0 : xfs_buf_free(bp);
1055 0 : return error;
1056 : }
1057 :
1058 : /*
1059 : * Increment reference count on buffer, to hold the buffer concurrently
1060 : * with another thread which may release (free) the buffer asynchronously.
1061 : * Must hold the buffer already to call this function.
1062 : */
1063 : void
1064 9580102543 : xfs_buf_hold(
1065 : struct xfs_buf *bp)
1066 : {
1067 9580102543 : trace_xfs_buf_hold(bp, _RET_IP_);
1068 9581994714 : atomic_inc(&bp->b_hold);
1069 9590440622 : }
1070 :
1071 : /*
1072 : * Release a hold on the specified buffer. If the hold count is 1, the buffer is
1073 : * placed on LRU or freed (depending on b_lru_ref).
1074 : */
1075 : void
1076 40347684717 : xfs_buf_rele(
1077 : struct xfs_buf *bp)
1078 : {
1079 40347684717 : struct xfs_perag *pag = bp->b_pag;
1080 40347684717 : struct xfs_buf_cache *bch = bp->b_cache;
1081 40347684717 : bool release;
1082 40347684717 : bool freebuf = false;
1083 :
1084 40347684717 : trace_xfs_buf_rele(bp, _RET_IP_);
1085 :
1086 40342256706 : if (!bch) {
1087 90931019 : ASSERT(list_empty(&bp->b_lru));
1088 90931019 : if (atomic_dec_and_test(&bp->b_hold)) {
1089 493965 : xfs_buf_ioacct_dec(bp);
1090 493965 : xfs_buf_free(bp);
1091 : }
1092 90931014 : return;
1093 : }
1094 :
1095 40251325687 : ASSERT(atomic_read(&bp->b_hold) > 0);
1096 :
1097 : /*
1098 : * We grab the b_lock here first to serialise racing xfs_buf_rele()
1099 : * calls. The pag_buf_lock being taken on the last reference only
1100 : * serialises against racing lookups in xfs_buf_find(). IOWs, the second
1101 : * to last reference we drop here is not serialised against the last
1102 : * reference until we take bp->b_lock. Hence if we don't grab b_lock
1103 : * first, the last "release" reference can win the race to the lock and
1104 : * free the buffer before the second-to-last reference is processed,
1105 : * leading to a use-after-free scenario.
1106 : */
1107 40251325687 : spin_lock(&bp->b_lock);
1108 40293050682 : release = atomic_dec_and_lock(&bp->b_hold, &bch->bc_lock);
1109 40281085932 : if (!release) {
1110 : /*
1111 : * Drop the in-flight state if the buffer is already on the LRU
1112 : * and it holds the only reference. This is racy because we
1113 : * haven't acquired the pag lock, but the use of _XBF_IN_FLIGHT
1114 : * ensures the decrement occurs only once per-buf.
1115 : */
1116 40163655484 : if ((atomic_read(&bp->b_hold) == 1) && !list_empty(&bp->b_lru))
1117 11367216771 : __xfs_buf_ioacct_dec(bp);
1118 40159465190 : goto out_unlock;
1119 : }
1120 :
1121 : /* the last reference has been dropped ... */
1122 117430448 : __xfs_buf_ioacct_dec(bp);
1123 117429870 : if (!(bp->b_flags & XBF_STALE) && atomic_read(&bp->b_lru_ref)) {
1124 : /*
1125 : * If the buffer is added to the LRU take a new reference to the
1126 : * buffer for the LRU and clear the (now stale) dispose list
1127 : * state flag
1128 : */
1129 44976866 : if (list_lru_add(&bp->b_target->bt_lru, &bp->b_lru)) {
1130 44981444 : bp->b_state &= ~XFS_BSTATE_DISPOSE;
1131 44981444 : atomic_inc(&bp->b_hold);
1132 : }
1133 44981794 : spin_unlock(&bch->bc_lock);
1134 : } else {
1135 : /*
1136 : * most of the time buffers will already be removed from the
1137 : * LRU, so optimise that case by checking for the
1138 : * XFS_BSTATE_DISPOSE flag indicating the last list the buffer
1139 : * was on was the disposal list
1140 : */
1141 72453004 : if (!(bp->b_state & XFS_BSTATE_DISPOSE)) {
1142 34883258 : list_lru_del(&bp->b_target->bt_lru, &bp->b_lru);
1143 : } else {
1144 37569746 : ASSERT(list_empty(&bp->b_lru));
1145 : }
1146 :
1147 72453899 : ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
1148 72453899 : rhashtable_remove_fast(&bch->bc_hash, &bp->b_rhash_head,
1149 : xfs_buf_hash_params);
1150 72453729 : spin_unlock(&bch->bc_lock);
1151 72453793 : if (pag)
1152 71983145 : xfs_perag_put(pag);
1153 72453650 : bp->b_cache = NULL;
1154 72453650 : bp->b_pag = NULL;
1155 72453650 : freebuf = true;
1156 : }
1157 :
1158 40276900402 : out_unlock:
1159 40276900402 : spin_unlock(&bp->b_lock);
1160 :
1161 40291566100 : if (freebuf)
1162 72453450 : xfs_buf_free(bp);
1163 : }
1164 :
1165 :
1166 : /*
1167 : * Lock a buffer object, if it is not already locked.
1168 : *
1169 : * If we come across a stale, pinned, locked buffer, we know that we are
1170 : * being asked to lock a buffer that has been reallocated. Because it is
1171 : * pinned, we know that the log has not been pushed to disk and hence it
1172 : * will still be locked. Rather than continuing to have trylock attempts
1173 : * fail until someone else pushes the log, push it ourselves before
1174 : * returning. This means that the xfsaild will not get stuck trying
1175 : * to push on stale inode buffers.
1176 : */
1177 : int
1178 6623625431 : xfs_buf_trylock(
1179 : struct xfs_buf *bp)
1180 : {
1181 6623625431 : int locked;
1182 :
1183 6623625431 : locked = down_trylock(&bp->b_sema) == 0;
1184 6624431526 : if (locked)
1185 6491904821 : trace_xfs_buf_trylock(bp, _RET_IP_);
1186 : else
1187 132526705 : trace_xfs_buf_trylock_fail(bp, _RET_IP_);
1188 6622770558 : return locked;
1189 : }
1190 :
1191 : /*
1192 : * Lock a buffer object.
1193 : *
1194 : * If we come across a stale, pinned, locked buffer, we know that we
1195 : * are being asked to lock a buffer that has been reallocated. Because
1196 : * it is pinned, we know that the log has not been pushed to disk and
1197 : * hence it will still be locked. Rather than sleeping until someone
1198 : * else pushes the log, push it ourselves before trying to get the lock.
1199 : */
1200 : void
1201 24238535632 : xfs_buf_lock(
1202 : struct xfs_buf *bp)
1203 : {
1204 24238535632 : trace_xfs_buf_lock(bp, _RET_IP_);
1205 :
1206 24232682994 : if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
1207 35629 : xfs_log_force(bp->b_mount, 0);
1208 24232682994 : down(&bp->b_sema);
1209 :
1210 24241779244 : trace_xfs_buf_lock_done(bp, _RET_IP_);
1211 24226923646 : }
1212 :
1213 : void
1214 30798274166 : xfs_buf_unlock(
1215 : struct xfs_buf *bp)
1216 : {
1217 30798274166 : ASSERT(xfs_buf_islocked(bp));
1218 :
1219 30798274166 : up(&bp->b_sema);
1220 30804977512 : trace_xfs_buf_unlock(bp, _RET_IP_);
1221 30790014953 : }
1222 :
1223 : STATIC void
1224 128069821 : xfs_buf_wait_unpin(
1225 : struct xfs_buf *bp)
1226 : {
1227 128069821 : DECLARE_WAITQUEUE (wait, current);
1228 :
1229 128069821 : if (atomic_read(&bp->b_pin_count) == 0)
1230 128056738 : return;
1231 :
1232 13083 : add_wait_queue(&bp->b_waiters, &wait);
1233 39239 : for (;;) {
1234 26161 : set_current_state(TASK_UNINTERRUPTIBLE);
1235 26161 : if (atomic_read(&bp->b_pin_count) == 0)
1236 : break;
1237 13078 : io_schedule();
1238 : }
1239 13083 : remove_wait_queue(&bp->b_waiters, &wait);
1240 13083 : set_current_state(TASK_RUNNING);
1241 : }
1242 :
1243 : static void
1244 12253 : xfs_buf_ioerror_alert_ratelimited(
1245 : struct xfs_buf *bp)
1246 : {
1247 12253 : static unsigned long lasttime;
1248 12253 : static struct xfs_buftarg *lasttarg;
1249 :
1250 12253 : if (bp->b_target != lasttarg ||
1251 12042 : time_after(jiffies, (lasttime + 5*HZ))) {
1252 222 : lasttime = jiffies;
1253 222 : xfs_buf_ioerror_alert(bp, __this_address);
1254 : }
1255 12253 : lasttarg = bp->b_target;
1256 12253 : }
1257 :
1258 : /*
1259 : * Account for this latest trip around the retry handler, and decide if
1260 : * we've failed enough times to constitute a permanent failure.
1261 : */
1262 : static bool
1263 11594 : xfs_buf_ioerror_permanent(
1264 : struct xfs_buf *bp,
1265 : struct xfs_error_cfg *cfg)
1266 : {
1267 11594 : struct xfs_mount *mp = bp->b_mount;
1268 :
1269 11594 : if (cfg->max_retries != XFS_ERR_RETRY_FOREVER &&
1270 55 : ++bp->b_retries > cfg->max_retries)
1271 : return true;
1272 11539 : if (cfg->retry_timeout != XFS_ERR_RETRY_FOREVER &&
1273 0 : time_after(jiffies, cfg->retry_timeout + bp->b_first_retry_time))
1274 : return true;
1275 :
1276 : /* At unmount we may treat errors differently */
1277 23078 : if (xfs_is_unmounting(mp) && mp->m_fail_unmount)
1278 7 : return true;
1279 :
1280 : return false;
1281 : }
1282 :
1283 : /*
1284 : * On a sync write or shutdown we just want to stale the buffer and let the
1285 : * caller handle the error in bp->b_error appropriately.
1286 : *
1287 : * If the write was asynchronous then no one will be looking for the error. If
1288 : * this is the first failure of this type, clear the error state and write the
1289 : * buffer out again. This means we always retry an async write failure at least
1290 : * once, but we also need to set the buffer up to behave correctly now for
1291 : * repeated failures.
1292 : *
1293 : * If we get repeated async write failures, then we take action according to the
1294 : * error configuration we have been set up to use.
1295 : *
1296 : * Returns true if this function took care of error handling and the caller must
1297 : * not touch the buffer again. Return false if the caller should proceed with
1298 : * normal I/O completion handling.
1299 : */
1300 : static bool
1301 2993656 : xfs_buf_ioend_handle_error(
1302 : struct xfs_buf *bp)
1303 : {
1304 2993656 : struct xfs_mount *mp = bp->b_mount;
1305 2993656 : struct xfs_error_cfg *cfg;
1306 :
1307 : /*
1308 : * If we've already shutdown the journal because of I/O errors, there's
1309 : * no point in giving this a retry.
1310 : */
1311 5987312 : if (xlog_is_shutdown(mp->m_log))
1312 2981404 : goto out_stale;
1313 :
1314 12252 : xfs_buf_ioerror_alert_ratelimited(bp);
1315 :
1316 : /*
1317 : * We're not going to bother about retrying this during recovery.
1318 : * One strike!
1319 : */
1320 12252 : if (bp->b_flags & _XBF_LOGRECOVERY) {
1321 0 : xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1322 0 : return false;
1323 : }
1324 :
1325 : /*
1326 : * Synchronous writes will have callers process the error.
1327 : */
1328 12252 : if (!(bp->b_flags & XBF_ASYNC))
1329 131 : goto out_stale;
1330 :
1331 12121 : trace_xfs_buf_iodone_async(bp, _RET_IP_);
1332 :
1333 12122 : cfg = xfs_error_get_cfg(mp, XFS_ERR_METADATA, bp->b_error);
1334 12121 : if (bp->b_last_error != bp->b_error ||
1335 11594 : !(bp->b_flags & (XBF_STALE | XBF_WRITE_FAIL))) {
1336 527 : bp->b_last_error = bp->b_error;
1337 527 : if (cfg->retry_timeout != XFS_ERR_RETRY_FOREVER &&
1338 451 : !bp->b_first_retry_time)
1339 451 : bp->b_first_retry_time = jiffies;
1340 527 : goto resubmit;
1341 : }
1342 :
1343 : /*
1344 : * Permanent error - we need to trigger a shutdown if we haven't already
1345 : * to indicate that inconsistency will result from this action.
1346 : */
1347 11594 : if (xfs_buf_ioerror_permanent(bp, cfg)) {
1348 62 : xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1349 63 : goto out_stale;
1350 : }
1351 :
1352 : /* Still considered a transient error. Caller will schedule retries. */
1353 11532 : if (bp->b_flags & _XBF_INODES)
1354 179 : xfs_buf_inode_io_fail(bp);
1355 11353 : else if (bp->b_flags & _XBF_DQUOTS)
1356 107 : xfs_buf_dquot_io_fail(bp);
1357 : else
1358 11246 : ASSERT(list_empty(&bp->b_li_list));
1359 11532 : xfs_buf_ioerror(bp, 0);
1360 11532 : xfs_buf_relse(bp);
1361 11532 : return true;
1362 :
1363 : resubmit:
1364 527 : xfs_buf_ioerror(bp, 0);
1365 527 : bp->b_flags |= (XBF_DONE | XBF_WRITE_FAIL);
1366 527 : xfs_buf_submit(bp);
1367 527 : return true;
1368 2981598 : out_stale:
1369 2981598 : xfs_buf_stale(bp);
1370 2981598 : bp->b_flags |= XBF_DONE;
1371 2981598 : bp->b_flags &= ~XBF_WRITE;
1372 2981598 : trace_xfs_buf_error_relse(bp, _RET_IP_);
1373 2981599 : return false;
1374 : }
1375 :
1376 : static void
1377 162591572 : xfs_buf_ioend(
1378 : struct xfs_buf *bp)
1379 : {
1380 162591572 : trace_xfs_buf_iodone(bp, _RET_IP_);
1381 :
1382 : /*
1383 : * Pull in IO completion errors now. We are guaranteed to be running
1384 : * single threaded, so we don't need the lock to read b_io_error.
1385 : */
1386 162591507 : if (!bp->b_error && bp->b_io_error)
1387 60767 : xfs_buf_ioerror(bp, bp->b_io_error);
1388 :
1389 162591506 : if (bp->b_flags & XBF_READ) {
1390 31539661 : if (!bp->b_error && bp->b_ops)
1391 28750805 : bp->b_ops->verify_read(bp);
1392 31539661 : if (!bp->b_error)
1393 31469055 : bp->b_flags |= XBF_DONE;
1394 : } else {
1395 131051845 : if (!bp->b_error) {
1396 128058195 : bp->b_flags &= ~XBF_WRITE_FAIL;
1397 128058195 : bp->b_flags |= XBF_DONE;
1398 : }
1399 :
1400 131051845 : if (unlikely(bp->b_error) && xfs_buf_ioend_handle_error(bp))
1401 : return;
1402 :
1403 : /* clear the retry state */
1404 131039787 : bp->b_last_error = 0;
1405 131039787 : bp->b_retries = 0;
1406 131039787 : bp->b_first_retry_time = 0;
1407 :
1408 : /*
1409 : * Note that for things like remote attribute buffers, there may
1410 : * not be a buffer log item here, so processing the buffer log
1411 : * item must remain optional.
1412 : */
1413 131039787 : if (bp->b_log_item)
1414 72007549 : xfs_buf_item_done(bp);
1415 :
1416 131039775 : if (bp->b_flags & _XBF_INODES)
1417 37669372 : xfs_buf_inode_iodone(bp);
1418 93370403 : else if (bp->b_flags & _XBF_DQUOTS)
1419 14679302 : xfs_buf_dquot_iodone(bp);
1420 :
1421 : }
1422 :
1423 162579436 : bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD |
1424 : _XBF_LOGRECOVERY);
1425 :
1426 162579436 : if (bp->b_flags & XBF_ASYNC)
1427 123716267 : xfs_buf_relse(bp);
1428 : else
1429 38863169 : complete(&bp->b_iowait);
1430 : }
1431 :
1432 : static void
1433 159489538 : xfs_buf_ioend_work(
1434 : struct work_struct *work)
1435 : {
1436 159489538 : struct xfs_buf *bp =
1437 159489538 : container_of(work, struct xfs_buf, b_ioend_work);
1438 :
1439 159489538 : xfs_buf_ioend(bp);
1440 159489540 : }
1441 :
1442 : static void
1443 159489542 : xfs_buf_ioend_async(
1444 : struct xfs_buf *bp)
1445 : {
1446 159489542 : INIT_WORK(&bp->b_ioend_work, xfs_buf_ioend_work);
1447 159489542 : queue_work(bp->b_mount->m_buf_workqueue, &bp->b_ioend_work);
1448 159489540 : }
1449 :
1450 : void
1451 70180239 : __xfs_buf_ioerror(
1452 : struct xfs_buf *bp,
1453 : int error,
1454 : xfs_failaddr_t failaddr)
1455 : {
1456 70180239 : ASSERT(error <= 0 && error >= -1000);
1457 70180239 : bp->b_error = error;
1458 70180239 : trace_xfs_buf_ioerror(bp, error, failaddr);
1459 70070629 : }
1460 :
1461 : void
1462 20918 : xfs_buf_ioerror_alert(
1463 : struct xfs_buf *bp,
1464 : xfs_failaddr_t func)
1465 : {
1466 20918 : xfs_buf_alert_ratelimited(bp, "XFS: metadata IO error",
1467 : "metadata I/O error in \"%pS\" at daddr 0x%llx len %d error %d",
1468 : func, (uint64_t)xfs_buf_daddr(bp),
1469 20918 : bp->b_length, -bp->b_error);
1470 20940 : }
1471 :
1472 : /*
1473 : * To simulate an I/O failure, the buffer must be locked and held with at least
1474 : * three references. The LRU reference is dropped by the stale call. The buf
1475 : * item reference is dropped via ioend processing. The third reference is owned
1476 : * by the caller and is dropped on I/O completion if the buffer is XBF_ASYNC.
1477 : */
1478 : void
1479 2995342 : xfs_buf_ioend_fail(
1480 : struct xfs_buf *bp)
1481 : {
1482 2995342 : bp->b_flags &= ~XBF_DONE;
1483 2995342 : xfs_buf_stale(bp);
1484 2995343 : xfs_buf_ioerror(bp, -EIO);
1485 2995343 : xfs_buf_ioend(bp);
1486 2995342 : }
1487 :
1488 : int
1489 180860 : xfs_bwrite(
1490 : struct xfs_buf *bp)
1491 : {
1492 180860 : int error;
1493 :
1494 180860 : ASSERT(xfs_buf_islocked(bp));
1495 :
1496 180860 : bp->b_flags |= XBF_WRITE;
1497 180860 : bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q |
1498 : XBF_DONE);
1499 :
1500 180860 : error = xfs_buf_submit(bp);
1501 180842 : if (error)
1502 131 : xfs_force_shutdown(bp->b_mount, SHUTDOWN_META_IO_ERROR);
1503 180842 : return error;
1504 : }
1505 :
1506 : static void
1507 159504852 : xfs_buf_bio_end_io(
1508 : struct bio *bio)
1509 : {
1510 159504852 : struct xfs_buf *bp = (struct xfs_buf *)bio->bi_private;
1511 :
1512 159504852 : if (!bio->bi_status &&
1513 259537357 : (bp->b_flags & XBF_WRITE) && (bp->b_flags & XBF_ASYNC) &&
1514 100093271 : XFS_TEST_ERROR(false, bp->b_mount, XFS_ERRTAG_BUF_IOERROR))
1515 0 : bio->bi_status = BLK_STS_IOERR;
1516 :
1517 : /*
1518 : * don't overwrite existing errors - otherwise we can lose errors on
1519 : * buffers that require multiple bios to complete.
1520 : */
1521 159504852 : if (bio->bi_status) {
1522 60767 : int error = blk_status_to_errno(bio->bi_status);
1523 :
1524 60764 : cmpxchg(&bp->b_io_error, 0, error);
1525 : }
1526 :
1527 159504853 : if (!bp->b_error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
1528 : invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1529 :
1530 159504853 : if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
1531 159443664 : xfs_buf_ioend_async(bp);
1532 159504852 : bio_put(bio);
1533 159504853 : }
1534 :
1535 : static void
1536 159495352 : xfs_buf_ioapply_map(
1537 : struct xfs_buf *bp,
1538 : int map,
1539 : int *buf_offset,
1540 : int *count,
1541 : blk_opf_t op)
1542 : {
1543 159495352 : int page_index;
1544 159495352 : unsigned int total_nr_pages = bp->b_page_count;
1545 159495352 : int nr_pages;
1546 159495352 : struct bio *bio;
1547 159495352 : sector_t sector = bp->b_maps[map].bm_bn;
1548 159495352 : int size;
1549 159495352 : int offset;
1550 :
1551 : /* skip the pages in the buffer before the start offset */
1552 159495352 : page_index = 0;
1553 159495352 : offset = *buf_offset;
1554 159496013 : while (offset >= PAGE_SIZE) {
1555 661 : page_index++;
1556 661 : offset -= PAGE_SIZE;
1557 : }
1558 :
1559 : /*
1560 : * Limit the IO size to the length of the current vector, and update the
1561 : * remaining IO count for the next time around.
1562 : */
1563 159495352 : size = min_t(int, BBTOB(bp->b_maps[map].bm_len), *count);
1564 159495352 : *count -= size;
1565 159495352 : *buf_offset += size;
1566 :
1567 159495352 : next_chunk:
1568 159495352 : atomic_inc(&bp->b_io_remaining);
1569 159500061 : nr_pages = bio_max_segs(total_nr_pages);
1570 :
1571 159500061 : bio = bio_alloc(bp->b_target->bt_bdev, nr_pages, op, GFP_NOIO);
1572 159499811 : bio->bi_iter.bi_sector = sector;
1573 159499811 : bio->bi_end_io = xfs_buf_bio_end_io;
1574 159499811 : bio->bi_private = bp;
1575 :
1576 483799706 : for (; size && nr_pages; nr_pages--, page_index++) {
1577 324307271 : int rbytes, nbytes = PAGE_SIZE - offset;
1578 :
1579 324307271 : if (nbytes > size)
1580 : nbytes = size;
1581 :
1582 324307271 : rbytes = bio_add_page(bio, bp->b_pages[page_index], nbytes,
1583 : offset);
1584 324299895 : if (rbytes < nbytes)
1585 : break;
1586 :
1587 324299895 : offset = 0;
1588 324299895 : sector += BTOBB(nbytes);
1589 324299895 : size -= nbytes;
1590 324299895 : total_nr_pages--;
1591 : }
1592 :
1593 159492435 : if (likely(bio->bi_iter.bi_size)) {
1594 159492435 : if (xfs_buf_is_vmapped(bp)) {
1595 : flush_kernel_vmap_range(bp->b_addr,
1596 : xfs_buf_vmap_len(bp));
1597 : }
1598 159492435 : submit_bio(bio);
1599 159494750 : if (size)
1600 0 : goto next_chunk;
1601 : } else {
1602 : /*
1603 : * This is guaranteed not to be the last io reference count
1604 : * because the caller (xfs_buf_submit) holds a count itself.
1605 : */
1606 0 : atomic_dec(&bp->b_io_remaining);
1607 0 : xfs_buf_ioerror(bp, -EIO);
1608 0 : bio_put(bio);
1609 : }
1610 :
1611 159494750 : }
1612 :
1613 : /* Start a synchronous process-context buffer IO. */
1614 : static inline void
1615 : xfs_buf_start_sync_io(
1616 : struct xfs_buf *bp)
1617 : {
1618 91450 : atomic_inc(&bp->b_io_remaining);
1619 : }
1620 :
1621 : /* Finish a synchronous bprocess-context uffer IO. */
1622 : static void
1623 91584 : xfs_buf_end_sync_io(
1624 : struct xfs_buf *bp,
1625 : int error)
1626 : {
1627 91584 : if (error)
1628 0 : cmpxchg(&bp->b_io_error, 0, error);
1629 :
1630 91584 : if (!bp->b_error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
1631 : invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1632 :
1633 91584 : if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
1634 0 : xfs_buf_ioend(bp);
1635 91718 : }
1636 :
1637 : bool
1638 392900021 : xfs_buf_check_poisoned(
1639 : struct xfs_buf *bp)
1640 : {
1641 392900021 : unsigned int i;
1642 :
1643 785800539 : for (i = 0; i < bp->b_page_count; i++) {
1644 392900518 : if (PageHWPoison(bp->b_pages[i]))
1645 : return true;
1646 : }
1647 :
1648 : return false;
1649 : }
1650 :
1651 : STATIC void
1652 159590719 : _xfs_buf_ioapply(
1653 : struct xfs_buf *bp)
1654 : {
1655 159590719 : struct blk_plug plug;
1656 159590719 : blk_opf_t op;
1657 159590719 : int offset;
1658 159590719 : int size;
1659 159590719 : int i;
1660 :
1661 : /*
1662 : * Make sure we capture only current IO errors rather than stale errors
1663 : * left over from previous use of the buffer (e.g. failed readahead).
1664 : */
1665 159590719 : bp->b_error = 0;
1666 :
1667 159590719 : if (bp->b_flags & XBF_WRITE) {
1668 128069915 : op = REQ_OP_WRITE;
1669 :
1670 : /*
1671 : * Run the write verifier callback function if it exists. If
1672 : * this function fails it will mark the buffer with an error and
1673 : * the IO should not be dispatched.
1674 : */
1675 128069915 : if (bp->b_ops) {
1676 128069915 : bp->b_ops->verify_write(bp);
1677 128069013 : if (bp->b_error) {
1678 22 : xfs_force_shutdown(bp->b_mount,
1679 : SHUTDOWN_CORRUPT_INCORE);
1680 91759 : return;
1681 : }
1682 0 : } else if (bp->b_rhash_key != XFS_BUF_DADDR_NULL) {
1683 0 : struct xfs_mount *mp = bp->b_mount;
1684 :
1685 : /*
1686 : * non-crc filesystems don't attach verifiers during
1687 : * log recovery, so don't warn for such filesystems.
1688 : */
1689 0 : if (xfs_has_crc(mp)) {
1690 0 : xfs_warn(mp,
1691 : "%s: no buf ops on daddr 0x%llx len %d",
1692 : __func__, xfs_buf_daddr(bp),
1693 : bp->b_length);
1694 0 : xfs_hex_dump(bp->b_addr,
1695 : XFS_CORRUPTION_DUMP_LEN);
1696 0 : dump_stack();
1697 : }
1698 : }
1699 : } else {
1700 31520804 : op = REQ_OP_READ;
1701 31520804 : if (bp->b_flags & XBF_READ_AHEAD)
1702 20641267 : op |= REQ_RAHEAD;
1703 : }
1704 :
1705 : /* we only use the buffer cache for meta-data */
1706 159589795 : op |= REQ_META;
1707 :
1708 159589795 : if (bp->b_target->bt_flags & XFS_BUFTARG_XFILE) {
1709 91450 : int error;
1710 :
1711 91450 : xfs_buf_start_sync_io(bp);
1712 91774 : error = xfile_buf_ioapply(bp);
1713 91623 : xfs_buf_end_sync_io(bp, error);
1714 91623 : return;
1715 : }
1716 :
1717 : /*
1718 : * Walk all the vectors issuing IO on them. Set up the initial offset
1719 : * into the buffer and the desired IO size before we start -
1720 : * _xfs_buf_ioapply_vec() will modify them appropriately for each
1721 : * subsequent call.
1722 : */
1723 159498345 : offset = bp->b_offset;
1724 159498345 : size = BBTOB(bp->b_length);
1725 159498345 : blk_start_plug(&plug);
1726 318994300 : for (i = 0; i < bp->b_map_count; i++) {
1727 159495955 : xfs_buf_ioapply_map(bp, i, &offset, &size, op);
1728 159487887 : if (bp->b_error)
1729 : break;
1730 159487887 : if (size <= 0)
1731 : break; /* all done */
1732 : }
1733 159487524 : blk_finish_plug(&plug);
1734 : }
1735 :
1736 : /*
1737 : * Wait for I/O completion of a sync buffer and return the I/O error code.
1738 : */
1739 : static int
1740 38847718 : xfs_buf_iowait(
1741 : struct xfs_buf *bp)
1742 : {
1743 38847718 : ASSERT(!(bp->b_flags & XBF_ASYNC));
1744 :
1745 38847718 : trace_xfs_buf_iowait(bp, _RET_IP_);
1746 38846611 : wait_for_completion(&bp->b_iowait);
1747 38847204 : trace_xfs_buf_iowait_done(bp, _RET_IP_);
1748 :
1749 38845778 : return bp->b_error;
1750 : }
1751 :
1752 : /*
1753 : * Buffer I/O submission path, read or write. Asynchronous submission transfers
1754 : * the buffer lock ownership and the current reference to the IO. It is not
1755 : * safe to reference the buffer after a call to this function unless the caller
1756 : * holds an additional reference itself.
1757 : */
1758 : static int
1759 161427763 : __xfs_buf_submit(
1760 : struct xfs_buf *bp,
1761 : bool wait)
1762 : {
1763 161427763 : int error = 0;
1764 :
1765 161427763 : trace_xfs_buf_submit(bp, _RET_IP_);
1766 :
1767 161425606 : ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
1768 :
1769 : /*
1770 : * On log shutdown we stale and complete the buffer immediately. We can
1771 : * be called to read the superblock before the log has been set up, so
1772 : * be careful checking the log state.
1773 : *
1774 : * Checking the mount shutdown state here can result in the log tail
1775 : * moving inappropriately on disk as the log may not yet be shut down.
1776 : * i.e. failing this buffer on mount shutdown can remove it from the AIL
1777 : * and move the tail of the log forwards without having written this
1778 : * buffer to disk. This corrupts the log tail state in memory, and
1779 : * because the log may not be shut down yet, it can then be propagated
1780 : * to disk before the log is shutdown. Hence we check log shutdown
1781 : * state here rather than mount state to avoid corrupting the log tail
1782 : * on shutdown.
1783 : */
1784 322632741 : if (bp->b_mount->m_log &&
1785 : xlog_is_shutdown(bp->b_mount->m_log)) {
1786 1838845 : xfs_buf_ioend_fail(bp);
1787 1838845 : return -EIO;
1788 : }
1789 :
1790 : /*
1791 : * Grab a reference so the buffer does not go away underneath us. For
1792 : * async buffers, I/O completion drops the callers reference, which
1793 : * could occur before submission returns.
1794 : */
1795 159586761 : xfs_buf_hold(bp);
1796 :
1797 159593109 : if (bp->b_flags & XBF_WRITE)
1798 128070522 : xfs_buf_wait_unpin(bp);
1799 :
1800 : /* clear the internal error state to avoid spurious errors */
1801 159592284 : bp->b_io_error = 0;
1802 :
1803 : /*
1804 : * Set the count to 1 initially, this will stop an I/O completion
1805 : * callout which happens before we have started all the I/O from calling
1806 : * xfs_buf_ioend too early.
1807 : */
1808 159592284 : atomic_set(&bp->b_io_remaining, 1);
1809 159592284 : if (bp->b_flags & XBF_ASYNC)
1810 120746955 : xfs_buf_ioacct_inc(bp);
1811 159592285 : _xfs_buf_ioapply(bp);
1812 :
1813 : /*
1814 : * If _xfs_buf_ioapply failed, we can get back here with only the IO
1815 : * reference we took above. If we drop it to zero, run completion so
1816 : * that we don't return to the caller with completion still pending.
1817 : */
1818 159592840 : if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
1819 152615 : if (bp->b_error || !(bp->b_flags & XBF_ASYNC))
1820 106738 : xfs_buf_ioend(bp);
1821 : else
1822 45877 : xfs_buf_ioend_async(bp);
1823 : }
1824 :
1825 159595443 : if (wait)
1826 11063894 : error = xfs_buf_iowait(bp);
1827 :
1828 : /*
1829 : * Release the hold that keeps the buffer referenced for the entire
1830 : * I/O. Note that if the buffer is async, it is not safe to reference
1831 : * after this release.
1832 : */
1833 159593265 : xfs_buf_rele(bp);
1834 159593265 : return error;
1835 : }
1836 :
1837 : void *
1838 26352828804 : xfs_buf_offset(
1839 : struct xfs_buf *bp,
1840 : size_t offset)
1841 : {
1842 26352828804 : struct page *page;
1843 :
1844 26352828804 : if (bp->b_addr)
1845 20590531243 : return bp->b_addr + offset;
1846 :
1847 5762297561 : page = bp->b_pages[offset >> PAGE_SHIFT];
1848 5762297561 : return page_address(page) + (offset & (PAGE_SIZE-1));
1849 : }
1850 :
1851 : void
1852 2383574 : xfs_buf_zero(
1853 : struct xfs_buf *bp,
1854 : size_t boff,
1855 : size_t bsize)
1856 : {
1857 2383574 : size_t bend;
1858 :
1859 2383574 : bend = boff + bsize;
1860 11051269 : while (boff < bend) {
1861 8667272 : struct page *page;
1862 8667272 : int page_index, page_offset, csize;
1863 :
1864 8667272 : page_index = (boff + bp->b_offset) >> PAGE_SHIFT;
1865 8667272 : page_offset = (boff + bp->b_offset) & ~PAGE_MASK;
1866 8667272 : page = bp->b_pages[page_index];
1867 8667272 : csize = min_t(size_t, PAGE_SIZE - page_offset,
1868 : BBTOB(bp->b_length) - boff);
1869 :
1870 8667272 : ASSERT((csize + page_offset) <= PAGE_SIZE);
1871 :
1872 8667695 : memset(page_address(page) + page_offset, 0, csize);
1873 :
1874 8667695 : boff += csize;
1875 : }
1876 2383997 : }
1877 :
1878 : /*
1879 : * Log a message about and stale a buffer that a caller has decided is corrupt.
1880 : *
1881 : * This function should be called for the kinds of metadata corruption that
1882 : * cannot be detect from a verifier, such as incorrect inter-block relationship
1883 : * data. Do /not/ call this function from a verifier function.
1884 : *
1885 : * The buffer must be XBF_DONE prior to the call. Afterwards, the buffer will
1886 : * be marked stale, but b_error will not be set. The caller is responsible for
1887 : * releasing the buffer or fixing it.
1888 : */
1889 : void
1890 0 : __xfs_buf_mark_corrupt(
1891 : struct xfs_buf *bp,
1892 : xfs_failaddr_t fa)
1893 : {
1894 0 : ASSERT(bp->b_flags & XBF_DONE);
1895 :
1896 0 : xfs_buf_corruption_error(bp, fa);
1897 0 : xfs_buf_stale(bp);
1898 0 : }
1899 :
1900 : /*
1901 : * Handling of buffer targets (buftargs).
1902 : */
1903 :
1904 : /*
1905 : * Wait for any bufs with callbacks that have been submitted but have not yet
1906 : * returned. These buffers will have an elevated hold count, so wait on those
1907 : * while freeing all the buffers only held by the LRU.
1908 : */
1909 : static enum lru_status
1910 36280925 : xfs_buftarg_drain_rele(
1911 : struct list_head *item,
1912 : struct list_lru_one *lru,
1913 : spinlock_t *lru_lock,
1914 : void *arg)
1915 :
1916 : {
1917 36280925 : struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
1918 36280925 : struct list_head *dispose = arg;
1919 :
1920 36280925 : if (atomic_read(&bp->b_hold) > 1) {
1921 : /* need to wait, so skip it this pass */
1922 0 : trace_xfs_buf_drain_buftarg(bp, _RET_IP_);
1923 0 : return LRU_SKIP;
1924 : }
1925 36280925 : if (!spin_trylock(&bp->b_lock))
1926 : return LRU_SKIP;
1927 :
1928 : /*
1929 : * clear the LRU reference count so the buffer doesn't get
1930 : * ignored in xfs_buf_rele().
1931 : */
1932 36280941 : atomic_set(&bp->b_lru_ref, 0);
1933 36280941 : bp->b_state |= XFS_BSTATE_DISPOSE;
1934 36280941 : list_lru_isolate_move(lru, item, dispose);
1935 36280941 : spin_unlock(&bp->b_lock);
1936 36280941 : return LRU_REMOVED;
1937 : }
1938 :
1939 : /*
1940 : * Wait for outstanding I/O on the buftarg to complete.
1941 : */
1942 : void
1943 287380 : xfs_buftarg_wait(
1944 : struct xfs_buftarg *btp)
1945 : {
1946 : /*
1947 : * First wait on the buftarg I/O count for all in-flight buffers to be
1948 : * released. This is critical as new buffers do not make the LRU until
1949 : * they are released.
1950 : *
1951 : * Next, flush the buffer workqueue to ensure all completion processing
1952 : * has finished. Just waiting on buffer locks is not sufficient for
1953 : * async IO as the reference count held over IO is not released until
1954 : * after the buffer lock is dropped. Hence we need to ensure here that
1955 : * all reference counts have been dropped before we start walking the
1956 : * LRU list.
1957 : */
1958 289097 : while (percpu_counter_sum(&btp->bt_io_count))
1959 1717 : delay(100);
1960 287405 : flush_workqueue(btp->bt_mount->m_buf_workqueue);
1961 287508 : }
1962 :
1963 : void
1964 167403 : xfs_buftarg_drain(
1965 : struct xfs_buftarg *btp)
1966 : {
1967 167403 : LIST_HEAD(dispose);
1968 167403 : int loop = 0;
1969 167403 : bool write_fail = false;
1970 :
1971 167403 : xfs_buftarg_wait(btp);
1972 :
1973 : /* loop until there is nothing left on the lru list. */
1974 332085 : while (list_lru_count(&btp->bt_lru)) {
1975 164714 : list_lru_walk(&btp->bt_lru, xfs_buftarg_drain_rele,
1976 : &dispose, LONG_MAX);
1977 :
1978 36445393 : while (!list_empty(&dispose)) {
1979 36280694 : struct xfs_buf *bp;
1980 36280694 : bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1981 36280694 : list_del_init(&bp->b_lru);
1982 36280697 : if (bp->b_flags & XBF_WRITE_FAIL) {
1983 0 : write_fail = true;
1984 0 : xfs_buf_alert_ratelimited(bp,
1985 : "XFS: Corruption Alert",
1986 : "Corruption Alert: Buffer at daddr 0x%llx had permanent write failures!",
1987 : (long long)xfs_buf_daddr(bp));
1988 : }
1989 36280697 : xfs_buf_rele(bp);
1990 : }
1991 164682 : if (loop++ != 0)
1992 0 : delay(100);
1993 : }
1994 :
1995 : /*
1996 : * If one or more failed buffers were freed, that means dirty metadata
1997 : * was thrown away. This should only ever happen after I/O completion
1998 : * handling has elevated I/O error(s) to permanent failures and shuts
1999 : * down the journal.
2000 : */
2001 167440 : if (write_fail) {
2002 0 : ASSERT(xlog_is_shutdown(btp->bt_mount->m_log));
2003 0 : xfs_alert(btp->bt_mount,
2004 : "Please run xfs_repair to determine the extent of the problem.");
2005 : }
2006 167440 : }
2007 :
2008 : static enum lru_status
2009 4238745 : xfs_buftarg_isolate(
2010 : struct list_head *item,
2011 : struct list_lru_one *lru,
2012 : spinlock_t *lru_lock,
2013 : void *arg)
2014 : {
2015 4238745 : struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
2016 4238745 : struct list_head *dispose = arg;
2017 :
2018 : /*
2019 : * we are inverting the lru lock/bp->b_lock here, so use a trylock.
2020 : * If we fail to get the lock, just skip it.
2021 : */
2022 4238745 : if (!spin_trylock(&bp->b_lock))
2023 : return LRU_SKIP;
2024 : /*
2025 : * Decrement the b_lru_ref count unless the value is already
2026 : * zero. If the value is already zero, we need to reclaim the
2027 : * buffer, otherwise it gets another trip through the LRU.
2028 : */
2029 8477488 : if (atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
2030 2942650 : spin_unlock(&bp->b_lock);
2031 2942650 : return LRU_ROTATE;
2032 : }
2033 :
2034 1296094 : bp->b_state |= XFS_BSTATE_DISPOSE;
2035 1296094 : list_lru_isolate_move(lru, item, dispose);
2036 1296094 : spin_unlock(&bp->b_lock);
2037 1296094 : return LRU_REMOVED;
2038 : }
2039 :
2040 : static unsigned long
2041 35377 : xfs_buftarg_shrink_scan(
2042 : struct shrinker *shrink,
2043 : struct shrink_control *sc)
2044 : {
2045 35377 : struct xfs_buftarg *btp = container_of(shrink,
2046 : struct xfs_buftarg, bt_shrinker);
2047 35377 : LIST_HEAD(dispose);
2048 35377 : unsigned long freed;
2049 :
2050 35377 : freed = list_lru_shrink_walk(&btp->bt_lru, sc,
2051 : xfs_buftarg_isolate, &dispose);
2052 :
2053 1331471 : while (!list_empty(&dispose)) {
2054 1296094 : struct xfs_buf *bp;
2055 1296094 : bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
2056 1296094 : list_del_init(&bp->b_lru);
2057 1296094 : xfs_buf_rele(bp);
2058 : }
2059 :
2060 35377 : return freed;
2061 : }
2062 :
2063 : static unsigned long
2064 13285 : xfs_buftarg_shrink_count(
2065 : struct shrinker *shrink,
2066 : struct shrink_control *sc)
2067 : {
2068 13285 : struct xfs_buftarg *btp = container_of(shrink,
2069 : struct xfs_buftarg, bt_shrinker);
2070 13285 : return list_lru_shrink_count(&btp->bt_lru, sc);
2071 : }
2072 :
2073 : void
2074 143140 : xfs_free_buftarg(
2075 : struct xfs_buftarg *btp)
2076 : {
2077 143140 : unregister_shrinker(&btp->bt_shrinker);
2078 143255 : ASSERT(percpu_counter_sum(&btp->bt_io_count) == 0);
2079 143285 : percpu_counter_destroy(&btp->bt_io_count);
2080 143301 : list_lru_destroy(&btp->bt_lru);
2081 :
2082 143301 : if (!(btp->bt_flags & XFS_BUFTARG_XFILE)) {
2083 96199 : blkdev_issue_flush(btp->bt_bdev);
2084 96199 : invalidate_bdev(btp->bt_bdev);
2085 96199 : fs_put_dax(btp->bt_daxdev, btp->bt_mount);
2086 : }
2087 :
2088 143301 : kvfree(btp);
2089 143293 : }
2090 :
2091 : int
2092 191656 : xfs_setsize_buftarg(
2093 : xfs_buftarg_t *btp,
2094 : unsigned int sectorsize)
2095 : {
2096 : /* Set up metadata sector size info */
2097 191656 : btp->bt_meta_sectorsize = sectorsize;
2098 191656 : btp->bt_meta_sectormask = sectorsize - 1;
2099 :
2100 191656 : if (set_blocksize(btp->bt_bdev, sectorsize)) {
2101 0 : xfs_warn(btp->bt_mount,
2102 : "Cannot set_blocksize to %u on device %pg",
2103 : sectorsize, btp->bt_bdev);
2104 0 : return -EINVAL;
2105 : }
2106 :
2107 : /* Set up device logical sector size mask */
2108 191656 : btp->bt_logical_sectorsize = bdev_logical_block_size(btp->bt_bdev);
2109 191656 : btp->bt_logical_sectormask = bdev_logical_block_size(btp->bt_bdev) - 1;
2110 :
2111 191656 : return 0;
2112 : }
2113 :
2114 : /*
2115 : * When allocating the initial buffer target we have not yet
2116 : * read in the superblock, so don't know what sized sectors
2117 : * are being used at this early stage. Play safe.
2118 : */
2119 : STATIC int
2120 96182 : xfs_setsize_buftarg_early(
2121 : xfs_buftarg_t *btp,
2122 : struct block_device *bdev)
2123 : {
2124 192364 : return xfs_setsize_buftarg(btp, bdev_logical_block_size(bdev));
2125 : }
2126 :
2127 : struct xfs_buftarg *
2128 143254 : xfs_alloc_buftarg_common(
2129 : struct xfs_mount *mp,
2130 : const char *descr)
2131 : {
2132 143254 : struct xfs_buftarg *btp;
2133 :
2134 143254 : btp = kzalloc(sizeof(*btp), GFP_NOFS);
2135 143263 : if (!btp)
2136 : return NULL;
2137 :
2138 143263 : btp->bt_mount = mp;
2139 :
2140 : /*
2141 : * Buffer IO error rate limiting. Limit it to no more than 10 messages
2142 : * per 30 seconds so as to not spam logs too much on repeated errors.
2143 : */
2144 143263 : ratelimit_state_init(&btp->bt_ioerror_rl, 30 * HZ,
2145 : DEFAULT_RATELIMIT_BURST);
2146 :
2147 143233 : if (list_lru_init(&btp->bt_lru))
2148 0 : goto error_free;
2149 :
2150 143231 : if (percpu_counter_init(&btp->bt_io_count, 0, GFP_NOFS))
2151 0 : goto error_lru;
2152 :
2153 143284 : btp->bt_shrinker.count_objects = xfs_buftarg_shrink_count;
2154 143284 : btp->bt_shrinker.scan_objects = xfs_buftarg_shrink_scan;
2155 143284 : btp->bt_shrinker.seeks = DEFAULT_SEEKS;
2156 143284 : btp->bt_shrinker.flags = SHRINKER_NUMA_AWARE;
2157 143284 : if (register_shrinker(&btp->bt_shrinker, "xfs-%s:%s", descr,
2158 143284 : mp->m_super->s_id))
2159 0 : goto error_pcpu;
2160 :
2161 : return btp;
2162 :
2163 : error_pcpu:
2164 0 : percpu_counter_destroy(&btp->bt_io_count);
2165 0 : error_lru:
2166 0 : list_lru_destroy(&btp->bt_lru);
2167 0 : error_free:
2168 0 : kvfree(btp);
2169 0 : return NULL;
2170 : }
2171 :
2172 : static inline void
2173 129754359 : xfs_buf_list_del(
2174 : struct xfs_buf *bp)
2175 : {
2176 129754359 : list_del_init(&bp->b_list);
2177 129754310 : wake_up_var(&bp->b_list);
2178 129754348 : }
2179 :
2180 : /* Allocate a buffer cache target for a persistent block device. */
2181 : struct xfs_buftarg *
2182 96182 : xfs_alloc_buftarg(
2183 : struct xfs_mount *mp,
2184 : struct block_device *bdev)
2185 : {
2186 96182 : struct xfs_buftarg *btp;
2187 96182 : const struct dax_holder_operations *ops = NULL;
2188 :
2189 : #if defined(CONFIG_FS_DAX) && defined(CONFIG_MEMORY_FAILURE)
2190 96182 : ops = &xfs_dax_holder_operations;
2191 : #endif
2192 :
2193 96182 : btp = xfs_alloc_buftarg_common(mp, "buf");
2194 96182 : if (!btp)
2195 : return NULL;
2196 :
2197 96182 : btp->bt_dev = bdev->bd_dev;
2198 96182 : btp->bt_bdev = bdev;
2199 96182 : btp->bt_daxdev = fs_dax_get_by_bdev(bdev, &btp->bt_dax_part_off,
2200 : mp, ops);
2201 :
2202 96182 : if (xfs_setsize_buftarg_early(btp, bdev))
2203 0 : goto error_free;
2204 :
2205 : return btp;
2206 :
2207 : error_free:
2208 0 : xfs_free_buftarg(btp);
2209 0 : return NULL;
2210 : }
2211 :
2212 : /*
2213 : * Cancel a delayed write list.
2214 : *
2215 : * Remove each buffer from the list, clear the delwri queue flag and drop the
2216 : * associated buffer reference.
2217 : */
2218 : void
2219 365414 : xfs_buf_delwri_cancel(
2220 : struct list_head *list)
2221 : {
2222 365414 : struct xfs_buf *bp;
2223 :
2224 365391 : while (!list_empty(list)) {
2225 0 : bp = list_first_entry(list, struct xfs_buf, b_list);
2226 :
2227 0 : xfs_buf_lock(bp);
2228 0 : bp->b_flags &= ~_XBF_DELWRI_Q;
2229 0 : xfs_buf_list_del(bp);
2230 0 : xfs_buf_relse(bp);
2231 : }
2232 365391 : }
2233 :
2234 : /*
2235 : * Add a buffer to the delayed write list.
2236 : *
2237 : * This queues a buffer for writeout if it hasn't already been. Note that
2238 : * neither this routine nor the buffer list submission functions perform
2239 : * any internal synchronization. It is expected that the lists are thread-local
2240 : * to the callers.
2241 : *
2242 : * Returns true if we queued up the buffer, or false if it already had
2243 : * been on the buffer list.
2244 : */
2245 : bool
2246 143472427 : xfs_buf_delwri_queue(
2247 : struct xfs_buf *bp,
2248 : struct list_head *list)
2249 : {
2250 143472427 : ASSERT(xfs_buf_islocked(bp));
2251 143472427 : ASSERT(!(bp->b_flags & XBF_READ));
2252 :
2253 : /*
2254 : * If the buffer is already marked delwri it already is queued up
2255 : * by someone else for imediate writeout. Just ignore it in that
2256 : * case.
2257 : */
2258 143472427 : if (bp->b_flags & _XBF_DELWRI_Q) {
2259 13719173 : trace_xfs_buf_delwri_queued(bp, _RET_IP_);
2260 13719173 : return false;
2261 : }
2262 :
2263 129753254 : trace_xfs_buf_delwri_queue(bp, _RET_IP_);
2264 :
2265 : /*
2266 : * If a buffer gets written out synchronously or marked stale while it
2267 : * is on a delwri list we lazily remove it. To do this, the other party
2268 : * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
2269 : * It remains referenced and on the list. In a rare corner case it
2270 : * might get readded to a delwri list after the synchronous writeout, in
2271 : * which case we need just need to re-add the flag here.
2272 : */
2273 129752932 : bp->b_flags |= _XBF_DELWRI_Q;
2274 129752932 : if (list_empty(&bp->b_list)) {
2275 129752886 : atomic_inc(&bp->b_hold);
2276 129754040 : list_add_tail(&bp->b_list, list);
2277 : }
2278 :
2279 : return true;
2280 : }
2281 :
2282 : /*
2283 : * Queue a buffer to this delwri list as part of a data integrity operation.
2284 : * If the buffer is on any other delwri list, we'll wait for that to clear
2285 : * so that the caller can submit the buffer for IO and wait for the result.
2286 : * Callers must ensure the buffer is not already on the list.
2287 : */
2288 : void
2289 1005756 : xfs_buf_delwri_queue_here(
2290 : struct xfs_buf *bp,
2291 : struct list_head *buffer_list)
2292 : {
2293 : /*
2294 : * We need this buffer to end up on the /caller's/ delwri list, not any
2295 : * old list. This can happen if the buffer is marked stale (which
2296 : * clears DELWRI_Q) after the AIL queues the buffer to its list but
2297 : * before the AIL has a chance to submit the list.
2298 : */
2299 1005400 : while (!list_empty(&bp->b_list)) {
2300 4 : xfs_buf_unlock(bp);
2301 8 : wait_var_event(&bp->b_list, list_empty(&bp->b_list));
2302 4 : xfs_buf_lock(bp);
2303 : }
2304 :
2305 1005396 : ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
2306 :
2307 : /* This buffer is uptodate; don't let it get reread. */
2308 1005396 : bp->b_flags |= XBF_DONE;
2309 :
2310 1005396 : xfs_buf_delwri_queue(bp, buffer_list);
2311 1006016 : }
2312 :
2313 : /*
2314 : * Compare function is more complex than it needs to be because
2315 : * the return value is only 32 bits and we are doing comparisons
2316 : * on 64 bit values
2317 : */
2318 : static int
2319 1110370977 : xfs_buf_cmp(
2320 : void *priv,
2321 : const struct list_head *a,
2322 : const struct list_head *b)
2323 : {
2324 1110370977 : struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
2325 1110370977 : struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
2326 1110370977 : xfs_daddr_t diff;
2327 :
2328 1110370977 : diff = ap->b_maps[0].bm_bn - bp->b_maps[0].bm_bn;
2329 1110370977 : if (diff < 0)
2330 : return -1;
2331 549335952 : if (diff > 0)
2332 549333616 : return 1;
2333 : return 0;
2334 : }
2335 :
2336 : /*
2337 : * Submit buffers for write. If wait_list is specified, the buffers are
2338 : * submitted using sync I/O and placed on the wait list such that the caller can
2339 : * iowait each buffer. Otherwise async I/O is used and the buffers are released
2340 : * at I/O completion time. In either case, buffers remain locked until I/O
2341 : * completes and the buffer is released from the queue.
2342 : */
2343 : static int
2344 6373853 : xfs_buf_delwri_submit_buffers(
2345 : struct list_head *buffer_list,
2346 : struct list_head *wait_list)
2347 : {
2348 6373853 : struct xfs_buf *bp, *n;
2349 6373853 : int pinned = 0;
2350 6373853 : struct blk_plug plug;
2351 :
2352 6373853 : list_sort(NULL, buffer_list, xfs_buf_cmp);
2353 :
2354 6373297 : blk_start_plug(&plug);
2355 137501750 : list_for_each_entry_safe(bp, n, buffer_list, b_list) {
2356 131127096 : if (!wait_list) {
2357 103344337 : if (!xfs_buf_trylock(bp))
2358 58998 : continue;
2359 103285339 : if (xfs_buf_ispinned(bp)) {
2360 1315373 : xfs_buf_unlock(bp);
2361 1315373 : pinned++;
2362 1315373 : continue;
2363 : }
2364 : } else {
2365 27782759 : xfs_buf_lock(bp);
2366 : }
2367 :
2368 : /*
2369 : * Someone else might have written the buffer synchronously or
2370 : * marked it stale in the meantime. In that case only the
2371 : * _XBF_DELWRI_Q flag got cleared, and we have to drop the
2372 : * reference and remove it from the list here.
2373 : */
2374 129753061 : if (!(bp->b_flags & _XBF_DELWRI_Q)) {
2375 40547 : xfs_buf_list_del(bp);
2376 40547 : xfs_buf_relse(bp);
2377 40547 : continue;
2378 : }
2379 :
2380 129712514 : trace_xfs_buf_delwri_split(bp, _RET_IP_);
2381 :
2382 : /*
2383 : * If we have a wait list, each buffer (and associated delwri
2384 : * queue reference) transfers to it and is submitted
2385 : * synchronously. Otherwise, drop the buffer from the delwri
2386 : * queue and submit async.
2387 : */
2388 129712704 : bp->b_flags &= ~_XBF_DELWRI_Q;
2389 129712704 : bp->b_flags |= XBF_WRITE;
2390 129712704 : if (wait_list) {
2391 27783285 : bp->b_flags &= ~XBF_ASYNC;
2392 27783285 : list_move_tail(&bp->b_list, wait_list);
2393 : } else {
2394 101929419 : bp->b_flags |= XBF_ASYNC;
2395 101929419 : xfs_buf_list_del(bp);
2396 : }
2397 129712750 : __xfs_buf_submit(bp, false);
2398 : }
2399 6374654 : blk_finish_plug(&plug);
2400 :
2401 6374660 : return pinned;
2402 : }
2403 :
2404 : /*
2405 : * Write out a buffer list asynchronously.
2406 : *
2407 : * This will take the @buffer_list, write all non-locked and non-pinned buffers
2408 : * out and not wait for I/O completion on any of the buffers. This interface
2409 : * is only safely useable for callers that can track I/O completion by higher
2410 : * level means, e.g. AIL pushing as the @buffer_list is consumed in this
2411 : * function.
2412 : *
2413 : * Note: this function will skip buffers it would block on, and in doing so
2414 : * leaves them on @buffer_list so they can be retried on a later pass. As such,
2415 : * it is up to the caller to ensure that the buffer list is fully submitted or
2416 : * cancelled appropriately when they are finished with the list. Failure to
2417 : * cancel or resubmit the list until it is empty will result in leaked buffers
2418 : * at unmount time.
2419 : */
2420 : int
2421 5107502 : xfs_buf_delwri_submit_nowait(
2422 : struct list_head *buffer_list)
2423 : {
2424 5107502 : return xfs_buf_delwri_submit_buffers(buffer_list, NULL);
2425 : }
2426 :
2427 : /*
2428 : * Write out a buffer list synchronously.
2429 : *
2430 : * This will take the @buffer_list, write all buffers out and wait for I/O
2431 : * completion on all of the buffers. @buffer_list is consumed by the function,
2432 : * so callers must have some other way of tracking buffers if they require such
2433 : * functionality.
2434 : */
2435 : int
2436 1266525 : xfs_buf_delwri_submit(
2437 : struct list_head *buffer_list)
2438 : {
2439 1266525 : LIST_HEAD (wait_list);
2440 1266525 : int error = 0, error2;
2441 1266525 : struct xfs_buf *bp;
2442 :
2443 1266525 : xfs_buf_delwri_submit_buffers(buffer_list, &wait_list);
2444 :
2445 : /* Wait for IO to complete. */
2446 29051034 : while (!list_empty(&wait_list)) {
2447 27784411 : bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
2448 :
2449 27784411 : xfs_buf_list_del(bp);
2450 :
2451 : /*
2452 : * Wait on the locked buffer, check for errors and unlock and
2453 : * release the delwri queue reference.
2454 : */
2455 27784403 : error2 = xfs_buf_iowait(bp);
2456 27784382 : xfs_buf_relse(bp);
2457 27784509 : if (!error)
2458 27784510 : error = error2;
2459 : }
2460 :
2461 1267613 : return error;
2462 : }
2463 :
2464 : /*
2465 : * Push a single buffer on a delwri queue.
2466 : *
2467 : * The purpose of this function is to submit a single buffer of a delwri queue
2468 : * and return with the buffer still on the original queue. The waiting delwri
2469 : * buffer submission infrastructure guarantees transfer of the delwri queue
2470 : * buffer reference to a temporary wait list. We reuse this infrastructure to
2471 : * transfer the buffer back to the original queue.
2472 : *
2473 : * Note the buffer transitions from the queued state, to the submitted and wait
2474 : * listed state and back to the queued state during this call. The buffer
2475 : * locking and queue management logic between _delwri_pushbuf() and
2476 : * _delwri_queue() guarantee that the buffer cannot be queued to another list
2477 : * before returning.
2478 : */
2479 : int
2480 0 : xfs_buf_delwri_pushbuf(
2481 : struct xfs_buf *bp,
2482 : struct list_head *buffer_list)
2483 : {
2484 0 : LIST_HEAD (submit_list);
2485 0 : int error;
2486 :
2487 0 : ASSERT(bp->b_flags & _XBF_DELWRI_Q);
2488 :
2489 0 : trace_xfs_buf_delwri_pushbuf(bp, _RET_IP_);
2490 :
2491 : /*
2492 : * Isolate the buffer to a new local list so we can submit it for I/O
2493 : * independently from the rest of the original list.
2494 : */
2495 0 : xfs_buf_lock(bp);
2496 0 : list_move(&bp->b_list, &submit_list);
2497 0 : xfs_buf_unlock(bp);
2498 :
2499 : /*
2500 : * Delwri submission clears the DELWRI_Q buffer flag and returns with
2501 : * the buffer on the wait list with the original reference. Rather than
2502 : * bounce the buffer from a local wait list back to the original list
2503 : * after I/O completion, reuse the original list as the wait list.
2504 : */
2505 0 : xfs_buf_delwri_submit_buffers(&submit_list, buffer_list);
2506 :
2507 : /*
2508 : * The buffer is now locked, under I/O and wait listed on the original
2509 : * delwri queue. Wait for I/O completion, restore the DELWRI_Q flag and
2510 : * return with the buffer unlocked and on the original queue.
2511 : */
2512 0 : error = xfs_buf_iowait(bp);
2513 0 : bp->b_flags |= _XBF_DELWRI_Q;
2514 0 : xfs_buf_unlock(bp);
2515 :
2516 0 : return error;
2517 : }
2518 :
2519 20816304361 : void xfs_buf_set_ref(struct xfs_buf *bp, int lru_ref)
2520 : {
2521 : /*
2522 : * Set the lru reference count to 0 based on the error injection tag.
2523 : * This allows userspace to disrupt buffer caching for debug/testing
2524 : * purposes.
2525 : */
2526 20816304361 : if (XFS_TEST_ERROR(false, bp->b_mount, XFS_ERRTAG_BUF_LRU_REF))
2527 90 : lru_ref = 0;
2528 :
2529 20812679216 : atomic_set(&bp->b_lru_ref, lru_ref);
2530 20812679216 : }
2531 :
2532 : /*
2533 : * Verify an on-disk magic value against the magic value specified in the
2534 : * verifier structure. The verifier magic is in disk byte order so the caller is
2535 : * expected to pass the value directly from disk.
2536 : */
2537 : bool
2538 561965502 : xfs_verify_magic(
2539 : struct xfs_buf *bp,
2540 : __be32 dmagic)
2541 : {
2542 561965502 : struct xfs_mount *mp = bp->b_mount;
2543 561965502 : int idx;
2544 :
2545 561965502 : idx = xfs_has_crc(mp);
2546 561965502 : if (WARN_ON(!bp->b_ops || !bp->b_ops->magic[idx]))
2547 : return false;
2548 561967826 : return dmagic == bp->b_ops->magic[idx];
2549 : }
2550 : /*
2551 : * Verify an on-disk magic value against the magic value specified in the
2552 : * verifier structure. The verifier magic is in disk byte order so the caller is
2553 : * expected to pass the value directly from disk.
2554 : */
2555 : bool
2556 1789463424 : xfs_verify_magic16(
2557 : struct xfs_buf *bp,
2558 : __be16 dmagic)
2559 : {
2560 1789463424 : struct xfs_mount *mp = bp->b_mount;
2561 1789463424 : int idx;
2562 :
2563 1789463424 : idx = xfs_has_crc(mp);
2564 1789463424 : if (WARN_ON(!bp->b_ops || !bp->b_ops->magic16[idx]))
2565 : return false;
2566 1789463469 : return dmagic == bp->b_ops->magic16[idx];
2567 : }
2568 :
2569 : /* Return the number of sectors for a buffer target. */
2570 : xfs_daddr_t
2571 3341806385 : xfs_buftarg_nr_sectors(
2572 : struct xfs_buftarg *btp)
2573 : {
2574 3341806385 : if (btp->bt_flags & XFS_BUFTARG_XFILE)
2575 3341806385 : return xfile_buftarg_nr_sectors(btp);
2576 0 : return bdev_nr_sectors(btp->bt_bdev);
2577 : }
|