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 40528751 : xfs_buf_submit(
59 : struct xfs_buf *bp)
60 : {
61 40528751 : 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 82315257 : 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 175911688 : xfs_buf_ioacct_inc(
100 : struct xfs_buf *bp)
101 : {
102 175911688 : if (bp->b_flags & XBF_NO_IOACCT)
103 : return;
104 :
105 175643765 : ASSERT(bp->b_flags & XBF_ASYNC);
106 175643765 : spin_lock(&bp->b_lock);
107 175643918 : if (!(bp->b_state & XFS_BSTATE_IN_FLIGHT)) {
108 166941898 : bp->b_state |= XFS_BSTATE_IN_FLIGHT;
109 166941898 : percpu_counter_inc(&bp->b_target->bt_io_count);
110 : }
111 175643864 : 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 10915006482 : __xfs_buf_ioacct_dec(
120 : struct xfs_buf *bp)
121 : {
122 10915006482 : lockdep_assert_held(&bp->b_lock);
123 :
124 10915006482 : if (bp->b_state & XFS_BSTATE_IN_FLIGHT) {
125 166941338 : bp->b_state &= ~XFS_BSTATE_IN_FLIGHT;
126 166941338 : percpu_counter_dec(&bp->b_target->bt_io_count);
127 : }
128 10915004900 : }
129 :
130 : static inline void
131 156304 : xfs_buf_ioacct_dec(
132 : struct xfs_buf *bp)
133 : {
134 156304 : spin_lock(&bp->b_lock);
135 156303 : __xfs_buf_ioacct_dec(bp);
136 156303 : spin_unlock(&bp->b_lock);
137 156303 : }
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 37942074 : xfs_buf_stale(
149 : struct xfs_buf *bp)
150 : {
151 37942074 : ASSERT(xfs_buf_islocked(bp));
152 :
153 37942074 : 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 37942074 : 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 37942074 : spin_lock(&bp->b_lock);
169 37942818 : __xfs_buf_ioacct_dec(bp);
170 :
171 37942822 : atomic_set(&bp->b_lru_ref, 0);
172 75885336 : if (!(bp->b_state & XFS_BSTATE_DISPOSE) &&
173 37940481 : (list_lru_del(&bp->b_target->bt_lru, &bp->b_lru)))
174 9896751 : atomic_dec(&bp->b_hold);
175 :
176 37944839 : ASSERT(atomic_read(&bp->b_hold) >= 1);
177 37944839 : spin_unlock(&bp->b_lock);
178 37942924 : }
179 :
180 : static int
181 82308909 : xfs_buf_get_maps(
182 : struct xfs_buf *bp,
183 : int map_count)
184 : {
185 82308909 : ASSERT(bp->b_maps == NULL);
186 82308909 : bp->b_map_count = map_count;
187 :
188 82308909 : if (map_count == 1) {
189 82308869 : bp->b_maps = &bp->__b_map;
190 82308869 : return 0;
191 : }
192 :
193 40 : bp->b_maps = kmem_zalloc(map_count * sizeof(struct xfs_buf_map),
194 : KM_NOFS);
195 40 : 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 81986632 : if (bp->b_maps != &bp->__b_map) {
208 40 : kmem_free(bp->b_maps);
209 40 : bp->b_maps = NULL;
210 : }
211 : }
212 :
213 : static int
214 82311181 : _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 82311181 : struct xfs_buf *bp;
222 82311181 : int error;
223 82311181 : int i;
224 :
225 82311181 : *bpp = NULL;
226 82311181 : 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 82307547 : flags &= ~(XBF_UNMAPPED | XBF_TRYLOCK | XBF_ASYNC | XBF_READ_AHEAD);
233 :
234 82307547 : atomic_set(&bp->b_hold, 1);
235 82307547 : atomic_set(&bp->b_lru_ref, 1);
236 82307547 : init_completion(&bp->b_iowait);
237 82306272 : INIT_LIST_HEAD(&bp->b_lru);
238 82306272 : INIT_LIST_HEAD(&bp->b_list);
239 82306272 : INIT_LIST_HEAD(&bp->b_li_list);
240 82306272 : sema_init(&bp->b_sema, 0); /* held, no waiters */
241 82306272 : spin_lock_init(&bp->b_lock);
242 82308163 : bp->b_target = target;
243 82308163 : bp->b_mount = target->bt_mount;
244 82308163 : 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 82308163 : error = xfs_buf_get_maps(bp, nmaps);
252 82308381 : if (error) {
253 0 : kmem_cache_free(xfs_buf_cache, bp);
254 0 : return error;
255 : }
256 :
257 82308381 : bp->b_rhash_key = map[0].bm_bn;
258 82308381 : bp->b_length = 0;
259 164621639 : for (i = 0; i < nmaps; i++) {
260 82313258 : bp->b_maps[i].bm_bn = map[i].bm_bn;
261 82313258 : bp->b_maps[i].bm_len = map[i].bm_len;
262 82313258 : bp->b_length += map[i].bm_len;
263 : }
264 :
265 82308381 : atomic_set(&bp->b_pin_count, 0);
266 82308381 : init_waitqueue_head(&bp->b_waiters);
267 :
268 82312086 : XFS_STATS_INC(bp->b_mount, xb_create);
269 82312086 : trace_xfs_buf_init(bp, _RET_IP_);
270 :
271 82311252 : *bpp = bp;
272 82311252 : return 0;
273 : }
274 :
275 : static void
276 157055 : xfs_buf_free_pages(
277 : struct xfs_buf *bp)
278 : {
279 157055 : uint i;
280 :
281 157055 : ASSERT(bp->b_flags & _XBF_PAGES);
282 :
283 314309 : for (i = 0; i < bp->b_page_count; i++) {
284 157252 : if (bp->b_pages[i])
285 157252 : __free_page(bp->b_pages[i]);
286 : }
287 157057 : mm_account_reclaimed_pages(bp->b_page_count);
288 :
289 157057 : xfs_buf_free_page_array(bp);
290 157057 : }
291 :
292 : void
293 325303 : xfs_buf_free_page_array(
294 : struct xfs_buf *bp)
295 : {
296 325303 : ASSERT(bp->b_flags & _XBF_PAGES);
297 :
298 325303 : if (bp->b_pages != bp->b_page_array)
299 0 : kmem_free(bp->b_pages);
300 325303 : bp->b_pages = NULL;
301 325303 : bp->b_flags &= ~_XBF_PAGES;
302 325303 : bp->b_page_count = 0;
303 325303 : }
304 :
305 : static void
306 81986632 : xfs_buf_free_callback(
307 : struct callback_head *cb)
308 : {
309 81986632 : struct xfs_buf *bp = container_of(cb, struct xfs_buf, b_rcu);
310 :
311 81986632 : xfs_buf_free_maps(bp);
312 81986632 : kmem_cache_free(xfs_buf_cache, bp);
313 82034952 : }
314 :
315 : static void
316 82315240 : xfs_buf_free(
317 : struct xfs_buf *bp)
318 : {
319 82315240 : trace_xfs_buf_free(bp, _RET_IP_);
320 :
321 82315257 : ASSERT(list_empty(&bp->b_lru));
322 :
323 82315257 : if (xfs_buf_is_vmapped(bp))
324 195 : vm_unmap_ram(bp->b_addr - bp->b_offset, bp->b_page_count);
325 :
326 82315262 : if (bp->b_flags & _XBF_DIRECT_MAP)
327 168242 : xfile_buf_unmap_pages(bp);
328 82147020 : else if (bp->b_flags & _XBF_PAGES)
329 157057 : xfs_buf_free_pages(bp);
330 81989963 : else if (bp->b_flags & _XBF_KMEM)
331 81989531 : kmem_free(bp->b_addr);
332 :
333 82315642 : call_rcu(&bp->b_rcu, xfs_buf_free_callback);
334 82315183 : }
335 :
336 : static int
337 81987559 : xfs_buf_alloc_kmem(
338 : struct xfs_buf *bp,
339 : xfs_buf_flags_t flags)
340 : {
341 81987559 : xfs_km_flags_t kmflag_mask = KM_NOFS;
342 81987559 : size_t size = BBTOB(bp->b_length);
343 :
344 : /* Assure zeroed buffer for non-read cases. */
345 81987559 : if (!(flags & XBF_READ))
346 41802925 : kmflag_mask |= KM_ZERO;
347 :
348 81987559 : bp->b_addr = kmem_alloc(size, kmflag_mask);
349 81984323 : if (!bp->b_addr)
350 : return -ENOMEM;
351 :
352 81984323 : 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 81984323 : bp->b_offset = offset_in_page(bp->b_addr);
360 81984323 : bp->b_pages = bp->b_page_array;
361 81984323 : bp->b_pages[0] = kmem_to_page(bp->b_addr);
362 81984409 : bp->b_page_count = 1;
363 81984409 : bp->b_flags |= _XBF_KMEM;
364 81984409 : return 0;
365 : }
366 :
367 : /* Make sure that we have a page list */
368 : int
369 325294 : xfs_buf_alloc_page_array(
370 : struct xfs_buf *bp,
371 : gfp_t gfp_mask)
372 : {
373 325294 : ASSERT(!(bp->b_flags & _XBF_PAGES));
374 :
375 325294 : bp->b_page_count = DIV_ROUND_UP(BBTOB(bp->b_length), PAGE_SIZE);
376 325294 : if (bp->b_page_count <= XB_PAGES) {
377 325294 : bp->b_pages = bp->b_page_array;
378 : } else {
379 0 : bp->b_pages = kzalloc(sizeof(struct page *) * bp->b_page_count,
380 : gfp_mask);
381 0 : if (!bp->b_pages)
382 : return -ENOMEM;
383 : }
384 :
385 325294 : bp->b_flags |= _XBF_PAGES;
386 325294 : return 0;
387 : }
388 :
389 : static int
390 157050 : xfs_buf_alloc_pages(
391 : struct xfs_buf *bp,
392 : xfs_buf_flags_t flags)
393 : {
394 157050 : gfp_t gfp_mask = __GFP_NOWARN;
395 157050 : long filled = 0;
396 157050 : int error;
397 :
398 157050 : if (flags & XBF_READ_AHEAD)
399 : gfp_mask |= __GFP_NORETRY;
400 : else
401 157042 : gfp_mask |= GFP_NOFS;
402 :
403 157050 : error = xfs_buf_alloc_page_array(bp, gfp_mask);
404 157049 : if (error)
405 : return error;
406 :
407 : /* Assure zeroed buffer for non-read cases. */
408 157049 : if (!(flags & XBF_READ))
409 156663 : 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 157049 : for (;;) {
417 157049 : long last = filled;
418 :
419 157049 : filled = alloc_pages_bulk_array(gfp_mask, bp->b_page_count,
420 : bp->b_pages);
421 157050 : if (filled == bp->b_page_count) {
422 157050 : XFS_STATS_INC(bp->b_mount, xb_page_found);
423 157050 : break;
424 : }
425 :
426 0 : if (filled != last)
427 0 : 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 157050 : bp->b_offset = 0;
439 157050 : return 0;
440 : }
441 :
442 : /*
443 : * Map buffer into kernel address-space if necessary.
444 : */
445 : STATIC int
446 325296 : _xfs_buf_map_pages(
447 : struct xfs_buf *bp,
448 : xfs_buf_flags_t flags)
449 : {
450 325296 : ASSERT(bp->b_flags & (_XBF_PAGES | _XBF_DIRECT_MAP));
451 :
452 325296 : if (bp->b_page_count == 1) {
453 : /* A single page buffer is always mappable */
454 325101 : bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
455 195 : } else if (flags & XBF_UNMAPPED) {
456 0 : bp->b_addr = NULL;
457 : } else {
458 195 : int retried = 0;
459 195 : 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 195 : nofs_flag = memalloc_nofs_save();
470 195 : do {
471 195 : bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
472 : -1);
473 195 : if (bp->b_addr)
474 : break;
475 0 : vm_unmap_aliases();
476 0 : } while (retried++ <= 1);
477 195 : memalloc_nofs_restore(nofs_flag);
478 :
479 195 : if (!bp->b_addr)
480 : return -ENOMEM;
481 :
482 195 : bp->b_addr += bp->b_offset;
483 : }
484 :
485 : return 0;
486 : }
487 :
488 : /*
489 : * Finding and Reading Buffers
490 : */
491 : static int
492 36795296455 : _xfs_buf_obj_cmp(
493 : struct rhashtable_compare_arg *arg,
494 : const void *obj)
495 : {
496 36795296455 : const struct xfs_buf_map *map = arg->key;
497 36795296455 : 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 36795296455 : BUILD_BUG_ON(offsetof(struct xfs_buf_map, bm_bn) != 0);
504 :
505 36795296455 : if (bp->b_rhash_key != map->bm_bn)
506 : return 1;
507 :
508 28473352262 : 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 33797877 : if (!(map->bm_flags & XBM_LIVESCAN))
521 5 : ASSERT(bp->b_flags & XBF_STALE);
522 33797877 : 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 214973 : xfs_buf_cache_init(
539 : struct xfs_buf_cache *bch)
540 : {
541 214973 : spin_lock_init(&bch->bc_lock);
542 214967 : return rhashtable_init(&bch->bc_hash, &xfs_buf_hash_params);
543 : }
544 :
545 : void
546 215001 : xfs_buf_cache_destroy(
547 : struct xfs_buf_cache *bch)
548 : {
549 215001 : rhashtable_destroy(&bch->bc_hash);
550 214995 : }
551 :
552 : static int
553 28553781201 : xfs_buf_map_verify(
554 : struct xfs_buftarg *btp,
555 : struct xfs_buf_map *map)
556 : {
557 28553781201 : xfs_daddr_t eofs;
558 :
559 : /* Check for IOs smaller than the sector size / not sector aligned */
560 28553781201 : ASSERT(!(BBTOB(map->bm_len) < btp->bt_meta_sectorsize));
561 28553781201 : 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 28553781201 : eofs = XFS_FSB_TO_BB(btp->bt_mount, btp->bt_mount->m_sb.sb_dblocks);
568 28553781201 : 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 28466914738 : xfs_buf_find_lock(
580 : struct xfs_buf *bp,
581 : xfs_buf_flags_t flags)
582 : {
583 28466914738 : if (flags & XBF_TRYLOCK) {
584 5627986317 : if (!xfs_buf_trylock(bp)) {
585 104921093 : XFS_STATS_INC(bp->b_mount, xb_busy_locked);
586 104921093 : return -EAGAIN;
587 : }
588 : } else {
589 22838928421 : xfs_buf_lock(bp);
590 22838788043 : 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 28362406187 : if (bp->b_flags & XBF_STALE) {
599 78055 : if (flags & XBF_LIVESCAN) {
600 0 : xfs_buf_unlock(bp);
601 0 : return -ENOENT;
602 : }
603 78055 : ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
604 78055 : bp->b_flags &= _XBF_KMEM | _XBF_PAGES | _XBF_DIRECT_MAP;
605 78055 : bp->b_ops = NULL;
606 : }
607 : return 0;
608 : }
609 :
610 : static inline int
611 28556236618 : 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 28556236618 : struct xfs_buf *bp;
618 28556236618 : int error;
619 :
620 28556236618 : rcu_read_lock();
621 28559531159 : bp = rhashtable_lookup(&bch->bc_hash, map, xfs_buf_hash_params);
622 57009081625 : if (!bp || !atomic_inc_not_zero(&bp->b_hold)) {
623 116097717 : rcu_read_unlock();
624 116097717 : return -ENOENT;
625 : }
626 28458043624 : rcu_read_unlock();
627 :
628 28458688931 : error = xfs_buf_find_lock(bp, flags);
629 28453088933 : if (error) {
630 104920736 : xfs_buf_rele(bp);
631 104920736 : return error;
632 : }
633 :
634 28348168197 : trace_xfs_buf_find(bp, flags, _RET_IP_);
635 28356991074 : *bpp = bp;
636 28356991074 : 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 82155967 : 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 82155967 : struct xfs_buf *new_bp;
655 82155967 : struct xfs_buf *bp;
656 82155967 : int error;
657 :
658 82155967 : error = _xfs_buf_alloc(btp, map, nmaps, flags, &new_bp);
659 82155681 : 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 82155681 : if (xfile_buftarg_can_direct_map(btp)) {
667 168242 : error = xfile_buf_map_pages(new_bp, flags);
668 168234 : if (error && error != -ENOTBLK)
669 0 : goto out_free_buf;
670 168234 : if (!error)
671 168235 : 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 81987438 : if (BBTOB(new_bp->b_length) < PAGE_SIZE) {
679 81986684 : error = xfs_buf_alloc_kmem(new_bp, flags);
680 81984968 : if (!error)
681 81984328 : 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 1394 : error = xfs_buf_alloc_pages(new_bp, flags);
689 754 : if (error)
690 0 : goto out_free_buf;
691 :
692 754 : insert:
693 82153317 : spin_lock(&bch->bc_lock);
694 82157425 : bp = rhashtable_lookup_get_insert_fast(&bch->bc_hash,
695 82157425 : &new_bp->b_rhash_head, xfs_buf_hash_params);
696 82156942 : 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 82156942 : if (bp) {
702 : /* found an existing buffer */
703 998 : atomic_inc(&bp->b_hold);
704 998 : spin_unlock(&bch->bc_lock);
705 998 : error = xfs_buf_find_lock(bp, flags);
706 998 : if (error)
707 1 : xfs_buf_rele(bp);
708 : else
709 997 : *bpp = bp;
710 998 : goto out_free_buf;
711 : }
712 :
713 : /* The new buffer keeps the perag reference until it is freed. */
714 82155944 : new_bp->b_pag = pag;
715 82155944 : new_bp->b_cache = bch;
716 82155944 : spin_unlock(&bch->bc_lock);
717 82155473 : *bpp = new_bp;
718 82155473 : return 0;
719 :
720 998 : out_free_buf:
721 998 : xfs_buf_free(new_bp);
722 998 : out_drop_pag:
723 998 : if (pag)
724 998 : 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 28555198917 : xfs_buftarg_get_cache(
731 : struct xfs_buftarg *btp,
732 : const struct xfs_buf_map *map,
733 : struct xfs_perag **pagp)
734 : {
735 28555198917 : struct xfs_mount *mp = btp->bt_mount;
736 :
737 28555198917 : if (btp->bt_cache) {
738 2131082351 : *pagp = NULL;
739 2131082351 : return btp->bt_cache;
740 : }
741 :
742 26424116566 : *pagp = xfs_perag_get(mp, xfs_daddr_to_agno(mp, map->bm_bn));
743 26438964743 : ASSERT(*pagp != NULL);
744 26438964743 : 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 28550199482 : 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 28550199482 : struct xfs_buf_cache *bch;
761 28550199482 : struct xfs_perag *pag;
762 28550199482 : struct xfs_buf *bp = NULL;
763 28550199482 : struct xfs_buf_map cmap = { .bm_bn = map[0].bm_bn };
764 28550199482 : int error;
765 28550199482 : int i;
766 :
767 28550199482 : if (flags & XBF_LIVESCAN)
768 37168410 : cmap.bm_flags |= XBM_LIVESCAN;
769 57102797341 : for (i = 0; i < nmaps; i++)
770 28552597859 : cmap.bm_len += map[i].bm_len;
771 :
772 28550199482 : error = xfs_buf_map_verify(btp, &cmap);
773 28556838845 : if (error)
774 : return error;
775 :
776 28556046035 : bch = xfs_buftarg_get_cache(btp, &cmap, &pag);
777 :
778 28558141147 : error = xfs_buf_lookup(bch, &cmap, flags, &bp);
779 28576085055 : if (error && error != -ENOENT)
780 104921298 : goto out_put_perag;
781 :
782 : /* cache hits always outnumber misses by at least 10:1 */
783 28471163757 : if (unlikely(!bp)) {
784 116096869 : XFS_STATS_INC(btp->bt_mount, xb_miss_locked);
785 :
786 116096869 : if (flags & XBF_INCORE)
787 33940989 : goto out_put_perag;
788 :
789 : /* xfs_buf_find_insert() consumes the perag reference. */
790 82155880 : error = xfs_buf_find_insert(btp, bch, pag, &cmap, map, nmaps,
791 : flags, &bp);
792 82155903 : if (error)
793 : return error;
794 : } else {
795 28355066888 : XFS_STATS_INC(btp->bt_mount, xb_get_locked);
796 28355066888 : if (pag)
797 26224151848 : xfs_perag_put(pag);
798 : }
799 :
800 : /* We do not hold a perag reference anymore. */
801 28442724775 : if (!bp->b_addr) {
802 168991 : error = _xfs_buf_map_pages(bp, flags);
803 168992 : 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 28442724776 : if (!(flags & XBF_READ))
817 68499534 : xfs_buf_ioerror(bp, 0);
818 :
819 28442726381 : XFS_STATS_INC(btp->bt_mount, xb_get);
820 28442726381 : trace_xfs_buf_get(bp, flags, _RET_IP_);
821 28438332688 : *bpp = bp;
822 28438332688 : return 0;
823 :
824 138862287 : out_put_perag:
825 138862287 : if (pag)
826 138862255 : xfs_perag_put(pag);
827 : return error;
828 : }
829 :
830 : int
831 40214996 : _xfs_buf_read(
832 : struct xfs_buf *bp,
833 : xfs_buf_flags_t flags)
834 : {
835 40214996 : ASSERT(!(flags & XBF_WRITE));
836 40214996 : ASSERT(bp->b_maps[0].bm_bn != XFS_BUF_DADDR_NULL);
837 :
838 40214996 : bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD | XBF_DONE);
839 40214996 : bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
840 :
841 40214996 : 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 29083818471 : xfs_buf_reverify(
863 : struct xfs_buf *bp,
864 : const struct xfs_buf_ops *ops)
865 : {
866 29083818471 : ASSERT(bp->b_flags & XBF_DONE);
867 29083818471 : ASSERT(bp->b_error == 0);
868 :
869 29083818471 : if (!ops || bp->b_ops)
870 : return 0;
871 :
872 3440 : bp->b_ops = ops;
873 3440 : bp->b_ops->verify_read(bp);
874 3440 : if (bp->b_error)
875 3436 : bp->b_flags &= ~XBF_DONE;
876 : return bp->b_error;
877 : }
878 :
879 : int
880 28446910423 : 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 28446910423 : struct xfs_buf *bp;
890 28446910423 : int error;
891 :
892 28446910423 : flags |= XBF_READ;
893 28446910423 : *bpp = NULL;
894 :
895 28446910423 : error = xfs_buf_get_map(target, map, nmaps, flags, &bp);
896 28477310291 : if (error)
897 : return error;
898 :
899 28373262657 : trace_xfs_buf_read(bp, flags, _RET_IP_);
900 :
901 28375000305 : if (!(bp->b_flags & XBF_DONE)) {
902 : /* Initiate the buffer read and wait. */
903 40203764 : XFS_STATS_INC(target->bt_mount, xb_get_read);
904 40203764 : bp->b_ops = ops;
905 40203764 : error = _xfs_buf_read(bp, flags);
906 :
907 : /* Readahead iodone already dropped the buffer, so exit. */
908 40879934 : if (flags & XBF_ASYNC)
909 : return 0;
910 : } else {
911 : /* Buffer already read; all we need to do is check it. */
912 28334796541 : error = xfs_buf_reverify(bp, ops);
913 :
914 : /* Readahead already finished; drop the buffer and exit. */
915 28338371873 : if (flags & XBF_ASYNC) {
916 5439854897 : xfs_buf_relse(bp);
917 5439834949 : return 0;
918 : }
919 :
920 : /* We do not want read in the flags */
921 22898516976 : bp->b_flags &= ~XBF_READ;
922 22898516976 : 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 22918401724 : 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 70816 : if (!xlog_is_shutdown(target->bt_mount->m_log))
944 22992 : xfs_buf_ioerror_alert(bp, fa);
945 :
946 35444 : bp->b_flags &= ~XBF_DONE;
947 35444 : xfs_buf_stale(bp);
948 35449 : xfs_buf_relse(bp);
949 :
950 : /* bad CRC means corrupted metadata */
951 35448 : if (error == -EFSBADCRC)
952 663 : error = -EFSCORRUPTED;
953 35448 : return error;
954 : }
955 :
956 22918366316 : *bpp = bp;
957 22918366316 : 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 5561225130 : 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 5561225130 : struct xfs_buf *bp;
972 :
973 11122691603 : xfs_buf_read_map(target, map, nmaps,
974 : XBF_TRYLOCK | XBF_ASYNC | XBF_READ_AHEAD, &bp, ops,
975 5561225130 : __this_address);
976 5563036962 : }
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 84229 : 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 84229 : struct xfs_buf *bp;
994 84229 : int error;
995 :
996 84229 : *bpp = NULL;
997 :
998 84229 : error = xfs_buf_get_uncached(target, numblks, flags, &bp);
999 84228 : if (error)
1000 : return error;
1001 :
1002 : /* set up the buffer for a read IO */
1003 84228 : ASSERT(bp->b_map_count == 1);
1004 84228 : bp->b_rhash_key = XFS_BUF_DADDR_NULL;
1005 84228 : bp->b_maps[0].bm_bn = daddr;
1006 84228 : bp->b_flags |= XBF_READ;
1007 84228 : bp->b_ops = ops;
1008 :
1009 84228 : xfs_buf_submit(bp);
1010 84230 : if (bp->b_error) {
1011 16 : error = bp->b_error;
1012 16 : xfs_buf_relse(bp);
1013 16 : return error;
1014 : }
1015 :
1016 84214 : *bpp = bp;
1017 84214 : return 0;
1018 : }
1019 :
1020 : int
1021 156295 : 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 156295 : int error;
1028 156295 : struct xfs_buf *bp;
1029 156295 : DEFINE_SINGLE_BUF_MAP(map, XFS_BUF_DADDR_NULL, numblks);
1030 :
1031 156295 : *bpp = NULL;
1032 :
1033 : /* flags might contain irrelevant bits, pass only what we care about */
1034 156295 : error = _xfs_buf_alloc(target, &map, 1, flags & XBF_NO_IOACCT, &bp);
1035 156295 : if (error)
1036 : return error;
1037 :
1038 156294 : error = xfs_buf_alloc_pages(bp, flags);
1039 156295 : if (error)
1040 0 : goto fail_free_buf;
1041 :
1042 156295 : error = _xfs_buf_map_pages(bp, 0);
1043 156294 : 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 156294 : trace_xfs_buf_get_uncached(bp, _RET_IP_);
1050 156294 : *bpp = bp;
1051 156294 : 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 9497329131 : xfs_buf_hold(
1065 : struct xfs_buf *bp)
1066 : {
1067 9497329131 : trace_xfs_buf_hold(bp, _RET_IP_);
1068 9497957003 : atomic_inc(&bp->b_hold);
1069 9497924480 : }
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 38207962118 : xfs_buf_rele(
1077 : struct xfs_buf *bp)
1078 : {
1079 38207962118 : struct xfs_perag *pag = bp->b_pag;
1080 38207962118 : struct xfs_buf_cache *bch = bp->b_cache;
1081 38207962118 : bool release;
1082 38207962118 : bool freebuf = false;
1083 :
1084 38207962118 : trace_xfs_buf_rele(bp, _RET_IP_);
1085 :
1086 38226799417 : if (!bch) {
1087 2211731 : ASSERT(list_empty(&bp->b_lru));
1088 4423460 : if (atomic_dec_and_test(&bp->b_hold)) {
1089 156302 : xfs_buf_ioacct_dec(bp);
1090 156303 : xfs_buf_free(bp);
1091 : }
1092 2211730 : return;
1093 : }
1094 :
1095 38224587686 : 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 38224587686 : spin_lock(&bp->b_lock);
1108 38262124634 : release = atomic_dec_and_lock(&bp->b_hold, &bch->bc_lock);
1109 38277885708 : 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 38138404529 : if ((atomic_read(&bp->b_hold) == 1) && !list_empty(&bp->b_lru))
1117 10736769779 : __xfs_buf_ioacct_dec(bp);
1118 38138485332 : goto out_unlock;
1119 : }
1120 :
1121 : /* the last reference has been dropped ... */
1122 139481179 : __xfs_buf_ioacct_dec(bp);
1123 139481510 : 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 57323527 : if (list_lru_add(&bp->b_target->bt_lru, &bp->b_lru)) {
1130 57323876 : bp->b_state &= ~XFS_BSTATE_DISPOSE;
1131 57323876 : atomic_inc(&bp->b_hold);
1132 : }
1133 57323882 : 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 82157983 : if (!(bp->b_state & XFS_BSTATE_DISPOSE)) {
1142 35376666 : list_lru_del(&bp->b_target->bt_lru, &bp->b_lru);
1143 : } else {
1144 46781317 : ASSERT(list_empty(&bp->b_lru));
1145 : }
1146 :
1147 82157989 : ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
1148 82157989 : rhashtable_remove_fast(&bch->bc_hash, &bp->b_rhash_head,
1149 : xfs_buf_hash_params);
1150 82157872 : spin_unlock(&bch->bc_lock);
1151 82157950 : if (pag)
1152 81989705 : xfs_perag_put(pag);
1153 82157944 : bp->b_cache = NULL;
1154 82157944 : bp->b_pag = NULL;
1155 82157944 : freebuf = true;
1156 : }
1157 :
1158 38277967157 : out_unlock:
1159 38277967157 : spin_unlock(&bp->b_lock);
1160 :
1161 38273930325 : if (freebuf)
1162 82157949 : 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 5928166341 : xfs_buf_trylock(
1179 : struct xfs_buf *bp)
1180 : {
1181 5928166341 : int locked;
1182 :
1183 5928166341 : locked = down_trylock(&bp->b_sema) == 0;
1184 5928678232 : if (locked)
1185 5823320280 : trace_xfs_buf_trylock(bp, _RET_IP_);
1186 : else
1187 105357952 : trace_xfs_buf_trylock_fail(bp, _RET_IP_);
1188 5929044580 : 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 22863423073 : xfs_buf_lock(
1202 : struct xfs_buf *bp)
1203 : {
1204 22863423073 : trace_xfs_buf_lock(bp, _RET_IP_);
1205 :
1206 22861643552 : if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
1207 29236 : xfs_log_force(bp->b_mount, 0);
1208 22861643552 : down(&bp->b_sema);
1209 :
1210 22861853707 : trace_xfs_buf_lock_done(bp, _RET_IP_);
1211 22866625727 : }
1212 :
1213 : void
1214 28738950780 : xfs_buf_unlock(
1215 : struct xfs_buf *bp)
1216 : {
1217 28738950780 : ASSERT(xfs_buf_islocked(bp));
1218 :
1219 28738950780 : up(&bp->b_sema);
1220 28753285606 : trace_xfs_buf_unlock(bp, _RET_IP_);
1221 28757411585 : }
1222 :
1223 : STATIC void
1224 179025857 : xfs_buf_wait_unpin(
1225 : struct xfs_buf *bp)
1226 : {
1227 179025857 : DECLARE_WAITQUEUE (wait, current);
1228 :
1229 179025857 : if (atomic_read(&bp->b_pin_count) == 0)
1230 179021722 : return;
1231 :
1232 4135 : add_wait_queue(&bp->b_waiters, &wait);
1233 12405 : for (;;) {
1234 8270 : set_current_state(TASK_UNINTERRUPTIBLE);
1235 8270 : if (atomic_read(&bp->b_pin_count) == 0)
1236 : break;
1237 4135 : io_schedule();
1238 : }
1239 4135 : remove_wait_queue(&bp->b_waiters, &wait);
1240 4135 : set_current_state(TASK_RUNNING);
1241 : }
1242 :
1243 : static void
1244 1760 : xfs_buf_ioerror_alert_ratelimited(
1245 : struct xfs_buf *bp)
1246 : {
1247 1760 : static unsigned long lasttime;
1248 1760 : static struct xfs_buftarg *lasttarg;
1249 :
1250 1760 : if (bp->b_target != lasttarg ||
1251 1649 : time_after(jiffies, (lasttime + 5*HZ))) {
1252 113 : lasttime = jiffies;
1253 113 : xfs_buf_ioerror_alert(bp, __this_address);
1254 : }
1255 1760 : lasttarg = bp->b_target;
1256 1760 : }
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 1488 : xfs_buf_ioerror_permanent(
1264 : struct xfs_buf *bp,
1265 : struct xfs_error_cfg *cfg)
1266 : {
1267 1488 : struct xfs_mount *mp = bp->b_mount;
1268 :
1269 1488 : if (cfg->max_retries != XFS_ERR_RETRY_FOREVER &&
1270 16 : ++bp->b_retries > cfg->max_retries)
1271 : return true;
1272 1472 : 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 2944 : if (xfs_is_unmounting(mp) && mp->m_fail_unmount)
1278 2 : 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 2613748 : xfs_buf_ioend_handle_error(
1302 : struct xfs_buf *bp)
1303 : {
1304 2613748 : struct xfs_mount *mp = bp->b_mount;
1305 2613748 : 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 5227496 : if (xlog_is_shutdown(mp->m_log))
1312 2611988 : goto out_stale;
1313 :
1314 1760 : 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 1760 : 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 1760 : if (!(bp->b_flags & XBF_ASYNC))
1329 92 : goto out_stale;
1330 :
1331 1668 : trace_xfs_buf_iodone_async(bp, _RET_IP_);
1332 :
1333 1668 : cfg = xfs_error_get_cfg(mp, XFS_ERR_METADATA, bp->b_error);
1334 1668 : if (bp->b_last_error != bp->b_error ||
1335 1488 : !(bp->b_flags & (XBF_STALE | XBF_WRITE_FAIL))) {
1336 180 : bp->b_last_error = bp->b_error;
1337 180 : if (cfg->retry_timeout != XFS_ERR_RETRY_FOREVER &&
1338 170 : !bp->b_first_retry_time)
1339 170 : bp->b_first_retry_time = jiffies;
1340 180 : 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 1488 : if (xfs_buf_ioerror_permanent(bp, cfg)) {
1348 18 : xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1349 18 : goto out_stale;
1350 : }
1351 :
1352 : /* Still considered a transient error. Caller will schedule retries. */
1353 1470 : if (bp->b_flags & _XBF_INODES)
1354 0 : xfs_buf_inode_io_fail(bp);
1355 1470 : else if (bp->b_flags & _XBF_DQUOTS)
1356 42 : xfs_buf_dquot_io_fail(bp);
1357 : else
1358 1428 : ASSERT(list_empty(&bp->b_li_list));
1359 1470 : xfs_buf_ioerror(bp, 0);
1360 1470 : xfs_buf_relse(bp);
1361 1470 : return true;
1362 :
1363 : resubmit:
1364 180 : xfs_buf_ioerror(bp, 0);
1365 180 : bp->b_flags |= (XBF_DONE | XBF_WRITE_FAIL);
1366 180 : xfs_buf_submit(bp);
1367 180 : return true;
1368 2612098 : out_stale:
1369 2612098 : xfs_buf_stale(bp);
1370 2612098 : bp->b_flags |= XBF_DONE;
1371 2612098 : bp->b_flags &= ~XBF_WRITE;
1372 2612098 : trace_xfs_buf_error_relse(bp, _RET_IP_);
1373 2612098 : return false;
1374 : }
1375 :
1376 : static void
1377 221937467 : xfs_buf_ioend(
1378 : struct xfs_buf *bp)
1379 : {
1380 221937467 : 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 221937465 : if (!bp->b_error && bp->b_io_error)
1387 52958 : xfs_buf_ioerror(bp, bp->b_io_error);
1388 :
1389 221937466 : if (bp->b_flags & XBF_READ) {
1390 40299514 : if (!bp->b_error && bp->b_ops)
1391 37871806 : bp->b_ops->verify_read(bp);
1392 40299516 : if (!bp->b_error)
1393 40233686 : bp->b_flags |= XBF_DONE;
1394 : } else {
1395 181637952 : if (!bp->b_error) {
1396 179024202 : bp->b_flags &= ~XBF_WRITE_FAIL;
1397 179024202 : bp->b_flags |= XBF_DONE;
1398 : }
1399 :
1400 181637952 : if (unlikely(bp->b_error) && xfs_buf_ioend_handle_error(bp))
1401 : return;
1402 :
1403 : /* clear the retry state */
1404 181636302 : bp->b_last_error = 0;
1405 181636302 : bp->b_retries = 0;
1406 181636302 : 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 181636302 : if (bp->b_log_item)
1414 106915729 : xfs_buf_item_done(bp);
1415 :
1416 181636301 : if (bp->b_flags & _XBF_INODES)
1417 47344429 : xfs_buf_inode_iodone(bp);
1418 134291872 : else if (bp->b_flags & _XBF_DQUOTS)
1419 21011796 : xfs_buf_dquot_iodone(bp);
1420 :
1421 : }
1422 :
1423 221935817 : bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD |
1424 : _XBF_LOGRECOVERY);
1425 :
1426 221935817 : if (bp->b_flags & XBF_ASYNC)
1427 178522355 : xfs_buf_relse(bp);
1428 : else
1429 43413462 : complete(&bp->b_iowait);
1430 : }
1431 :
1432 : static void
1433 219086316 : xfs_buf_ioend_work(
1434 : struct work_struct *work)
1435 : {
1436 219086316 : struct xfs_buf *bp =
1437 219086316 : container_of(work, struct xfs_buf, b_ioend_work);
1438 :
1439 219086316 : xfs_buf_ioend(bp);
1440 219086316 : }
1441 :
1442 : static void
1443 219086316 : xfs_buf_ioend_async(
1444 : struct xfs_buf *bp)
1445 : {
1446 219086316 : INIT_WORK(&bp->b_ioend_work, xfs_buf_ioend_work);
1447 219086316 : queue_work(bp->b_mount->m_buf_workqueue, &bp->b_ioend_work);
1448 219086314 : }
1449 :
1450 : void
1451 71185508 : __xfs_buf_ioerror(
1452 : struct xfs_buf *bp,
1453 : int error,
1454 : xfs_failaddr_t failaddr)
1455 : {
1456 71185508 : ASSERT(error <= 0 && error >= -1000);
1457 71185508 : bp->b_error = error;
1458 71185508 : trace_xfs_buf_ioerror(bp, error, failaddr);
1459 71186801 : }
1460 :
1461 : void
1462 23107 : xfs_buf_ioerror_alert(
1463 : struct xfs_buf *bp,
1464 : xfs_failaddr_t func)
1465 : {
1466 23107 : 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 23107 : bp->b_length, -bp->b_error);
1470 23142 : }
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 2623185 : xfs_buf_ioend_fail(
1480 : struct xfs_buf *bp)
1481 : {
1482 2623185 : bp->b_flags &= ~XBF_DONE;
1483 2623185 : xfs_buf_stale(bp);
1484 2623185 : xfs_buf_ioerror(bp, -EIO);
1485 2623185 : xfs_buf_ioend(bp);
1486 2623183 : }
1487 :
1488 : int
1489 229300 : xfs_bwrite(
1490 : struct xfs_buf *bp)
1491 : {
1492 229300 : int error;
1493 :
1494 229300 : ASSERT(xfs_buf_islocked(bp));
1495 :
1496 229300 : bp->b_flags |= XBF_WRITE;
1497 229300 : bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q |
1498 : XBF_DONE);
1499 :
1500 229300 : error = xfs_buf_submit(bp);
1501 229298 : if (error)
1502 92 : xfs_force_shutdown(bp->b_mount, SHUTDOWN_META_IO_ERROR);
1503 229298 : return error;
1504 : }
1505 :
1506 : static void
1507 219157315 : xfs_buf_bio_end_io(
1508 : struct bio *bio)
1509 : {
1510 219157315 : struct xfs_buf *bp = (struct xfs_buf *)bio->bi_private;
1511 :
1512 219157315 : if (!bio->bi_status &&
1513 374019580 : (bp->b_flags & XBF_WRITE) && (bp->b_flags & XBF_ASYNC) &&
1514 154915224 : 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 219157315 : if (bio->bi_status) {
1522 52959 : int error = blk_status_to_errno(bio->bi_status);
1523 :
1524 52958 : cmpxchg(&bp->b_io_error, 0, error);
1525 : }
1526 :
1527 219157314 : 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 438314629 : if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
1531 219055587 : xfs_buf_ioend_async(bp);
1532 219157315 : bio_put(bio);
1533 219157315 : }
1534 :
1535 : static void
1536 219156335 : 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 219156335 : int page_index;
1544 219156335 : unsigned int total_nr_pages = bp->b_page_count;
1545 219156335 : int nr_pages;
1546 219156335 : struct bio *bio;
1547 219156335 : sector_t sector = bp->b_maps[map].bm_bn;
1548 219156335 : int size;
1549 219156335 : int offset;
1550 :
1551 : /* skip the pages in the buffer before the start offset */
1552 219156335 : page_index = 0;
1553 219156335 : offset = *buf_offset;
1554 219156335 : while (offset >= PAGE_SIZE) {
1555 0 : page_index++;
1556 0 : 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 219156335 : size = min_t(int, BBTOB(bp->b_maps[map].bm_len), *count);
1564 219156335 : *count -= size;
1565 219156335 : *buf_offset += size;
1566 :
1567 219156335 : next_chunk:
1568 219156335 : atomic_inc(&bp->b_io_remaining);
1569 219155727 : nr_pages = bio_max_segs(total_nr_pages);
1570 :
1571 219155727 : bio = bio_alloc(bp->b_target->bt_bdev, nr_pages, op, GFP_NOIO);
1572 219156081 : bio->bi_iter.bi_sector = sector;
1573 219156081 : bio->bi_end_io = xfs_buf_bio_end_io;
1574 219156081 : bio->bi_private = bp;
1575 :
1576 438312387 : for (; size && nr_pages; nr_pages--, page_index++) {
1577 219156187 : int rbytes, nbytes = PAGE_SIZE - offset;
1578 :
1579 219156187 : if (nbytes > size)
1580 : nbytes = size;
1581 :
1582 219156187 : rbytes = bio_add_page(bio, bp->b_pages[page_index], nbytes,
1583 : offset);
1584 219156306 : if (rbytes < nbytes)
1585 : break;
1586 :
1587 219156306 : offset = 0;
1588 219156306 : sector += BTOBB(nbytes);
1589 219156306 : size -= nbytes;
1590 219156306 : total_nr_pages--;
1591 : }
1592 :
1593 219156200 : if (likely(bio->bi_iter.bi_size)) {
1594 219156200 : if (xfs_buf_is_vmapped(bp)) {
1595 : flush_kernel_vmap_range(bp->b_addr,
1596 : xfs_buf_vmap_len(bp));
1597 : }
1598 219156200 : submit_bio(bio);
1599 219154582 : 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 219154582 : }
1612 :
1613 : /* Start a synchronous process-context buffer IO. */
1614 : static inline void
1615 157029 : xfs_buf_start_sync_io(
1616 : struct xfs_buf *bp)
1617 : {
1618 157029 : atomic_inc(&bp->b_io_remaining);
1619 157038 : }
1620 :
1621 : /* Finish a synchronous bprocess-context uffer IO. */
1622 : static void
1623 157030 : xfs_buf_end_sync_io(
1624 : struct xfs_buf *bp,
1625 : int error)
1626 : {
1627 157030 : if (error)
1628 0 : cmpxchg(&bp->b_io_error, 0, error);
1629 :
1630 157030 : 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 314082 : if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
1634 0 : xfs_buf_ioend(bp);
1635 157052 : }
1636 :
1637 : bool
1638 731398292 : xfs_buf_check_poisoned(
1639 : struct xfs_buf *bp)
1640 : {
1641 731398292 : unsigned int i;
1642 :
1643 1462796637 : for (i = 0; i < bp->b_page_count; i++) {
1644 731398345 : if (PageHWPoison(bp->b_pages[i]))
1645 : return true;
1646 : }
1647 :
1648 731398292 : return false;
1649 : }
1650 :
1651 : STATIC void
1652 219313440 : _xfs_buf_ioapply(
1653 : struct xfs_buf *bp)
1654 : {
1655 219313440 : struct blk_plug plug;
1656 219313440 : blk_opf_t op;
1657 219313440 : int offset;
1658 219313440 : int size;
1659 219313440 : 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 219313440 : bp->b_error = 0;
1666 :
1667 219313440 : if (bp->b_flags & XBF_WRITE) {
1668 179025593 : 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 179025593 : if (bp->b_ops) {
1676 179025593 : bp->b_ops->verify_write(bp);
1677 179025793 : if (bp->b_error) {
1678 4 : xfs_force_shutdown(bp->b_mount,
1679 : SHUTDOWN_CORRUPT_INCORE);
1680 157044 : 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 40287847 : op = REQ_OP_READ;
1701 40287847 : if (bp->b_flags & XBF_READ_AHEAD)
1702 20994860 : op |= REQ_RAHEAD;
1703 : }
1704 :
1705 : /* we only use the buffer cache for meta-data */
1706 219313636 : op |= REQ_META;
1707 :
1708 219313636 : if (bp->b_target->bt_flags & XFS_BUFTARG_XFILE) {
1709 157033 : int error;
1710 :
1711 157033 : xfs_buf_start_sync_io(bp);
1712 157026 : error = xfile_buf_ioapply(bp);
1713 157030 : xfs_buf_end_sync_io(bp, error);
1714 157030 : 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 219156603 : offset = bp->b_offset;
1724 219156603 : size = BBTOB(bp->b_length);
1725 219156603 : blk_start_plug(&plug);
1726 438312951 : for (i = 0; i < bp->b_map_count; i++) {
1727 219156348 : xfs_buf_ioapply_map(bp, i, &offset, &size, op);
1728 219155147 : if (bp->b_error)
1729 : break;
1730 219155147 : if (size <= 0)
1731 : break; /* all done */
1732 : }
1733 219155071 : 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 43402219 : xfs_buf_iowait(
1741 : struct xfs_buf *bp)
1742 : {
1743 43402219 : ASSERT(!(bp->b_flags & XBF_ASYNC));
1744 :
1745 43402219 : trace_xfs_buf_iowait(bp, _RET_IP_);
1746 43402250 : wait_for_completion(&bp->b_iowait);
1747 43401207 : trace_xfs_buf_iowait_done(bp, _RET_IP_);
1748 :
1749 43401743 : 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 220968647 : __xfs_buf_submit(
1760 : struct xfs_buf *bp,
1761 : bool wait)
1762 : {
1763 220968647 : int error = 0;
1764 :
1765 220968647 : trace_xfs_buf_submit(bp, _RET_IP_);
1766 :
1767 220968936 : 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 441864309 : if (bp->b_mount->m_log &&
1785 : xlog_is_shutdown(bp->b_mount->m_log)) {
1786 1655941 : xfs_buf_ioend_fail(bp);
1787 1655941 : 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 219312995 : xfs_buf_hold(bp);
1796 :
1797 219313581 : if (bp->b_flags & XBF_WRITE)
1798 179025880 : xfs_buf_wait_unpin(bp);
1799 :
1800 : /* clear the internal error state to avoid spurious errors */
1801 219313633 : 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 219313633 : atomic_set(&bp->b_io_remaining, 1);
1809 219313633 : if (bp->b_flags & XBF_ASYNC)
1810 175911691 : xfs_buf_ioacct_inc(bp);
1811 219313658 : _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 438626515 : if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
1819 258698 : if (bp->b_error || !(bp->b_flags & XBF_ASYNC))
1820 227969 : xfs_buf_ioend(bp);
1821 : else
1822 30729 : xfs_buf_ioend_async(bp);
1823 : }
1824 :
1825 219313380 : if (wait)
1826 19522445 : 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 219312581 : xfs_buf_rele(bp);
1834 219312581 : return error;
1835 : }
1836 :
1837 : void *
1838 21223458901 : xfs_buf_offset(
1839 : struct xfs_buf *bp,
1840 : size_t offset)
1841 : {
1842 21223458901 : struct page *page;
1843 :
1844 21223458901 : if (bp->b_addr)
1845 21223458901 : return bp->b_addr + offset;
1846 :
1847 0 : page = bp->b_pages[offset >> PAGE_SHIFT];
1848 0 : return page_address(page) + (offset & (PAGE_SIZE-1));
1849 : }
1850 :
1851 : void
1852 1121325 : xfs_buf_zero(
1853 : struct xfs_buf *bp,
1854 : size_t boff,
1855 : size_t bsize)
1856 : {
1857 1121325 : size_t bend;
1858 :
1859 1121325 : bend = boff + bsize;
1860 2242642 : while (boff < bend) {
1861 1121324 : struct page *page;
1862 1121324 : int page_index, page_offset, csize;
1863 :
1864 1121324 : page_index = (boff + bp->b_offset) >> PAGE_SHIFT;
1865 1121324 : page_offset = (boff + bp->b_offset) & ~PAGE_MASK;
1866 1121324 : page = bp->b_pages[page_index];
1867 1121324 : csize = min_t(size_t, PAGE_SIZE - page_offset,
1868 : BBTOB(bp->b_length) - boff);
1869 :
1870 1121324 : ASSERT((csize + page_offset) <= PAGE_SIZE);
1871 :
1872 1121317 : memset(page_address(page) + page_offset, 0, csize);
1873 :
1874 1121317 : boff += csize;
1875 : }
1876 1121318 : }
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 26570376 : 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 26570376 : struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
1918 26570376 : struct list_head *dispose = arg;
1919 :
1920 26570376 : 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 26570376 : 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 26570378 : atomic_set(&bp->b_lru_ref, 0);
1933 26570378 : bp->b_state |= XFS_BSTATE_DISPOSE;
1934 26570378 : list_lru_isolate_move(lru, item, dispose);
1935 26570378 : spin_unlock(&bp->b_lock);
1936 26570378 : return LRU_REMOVED;
1937 : }
1938 :
1939 : /*
1940 : * Wait for outstanding I/O on the buftarg to complete.
1941 : */
1942 : void
1943 202392 : 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 205031 : while (percpu_counter_sum(&btp->bt_io_count))
1959 2639 : delay(100);
1960 202386 : flush_workqueue(btp->bt_mount->m_buf_workqueue);
1961 202395 : }
1962 :
1963 : void
1964 127154 : xfs_buftarg_drain(
1965 : struct xfs_buftarg *btp)
1966 : {
1967 127154 : LIST_HEAD(dispose);
1968 127154 : int loop = 0;
1969 127154 : bool write_fail = false;
1970 :
1971 127154 : xfs_buftarg_wait(btp);
1972 :
1973 : /* loop until there is nothing left on the lru list. */
1974 251898 : while (list_lru_count(&btp->bt_lru)) {
1975 124744 : list_lru_walk(&btp->bt_lru, xfs_buftarg_drain_rele,
1976 : &dispose, LONG_MAX);
1977 :
1978 26695114 : while (!list_empty(&dispose)) {
1979 26570371 : struct xfs_buf *bp;
1980 26570371 : bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1981 26570371 : list_del_init(&bp->b_lru);
1982 26570370 : 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 26570370 : xfs_buf_rele(bp);
1990 : }
1991 124744 : 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 127158 : 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 127158 : }
2007 :
2008 : static enum lru_status
2009 66054429 : xfs_buftarg_isolate(
2010 : struct list_head *item,
2011 : struct list_lru_one *lru,
2012 : spinlock_t *lru_lock,
2013 : void *arg)
2014 : {
2015 66054429 : struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
2016 66054429 : 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 66054429 : 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 132108440 : if (atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
2030 45197396 : spin_unlock(&bp->b_lock);
2031 45197396 : return LRU_ROTATE;
2032 : }
2033 :
2034 20856824 : bp->b_state |= XFS_BSTATE_DISPOSE;
2035 20856824 : list_lru_isolate_move(lru, item, dispose);
2036 20856824 : spin_unlock(&bp->b_lock);
2037 20856824 : return LRU_REMOVED;
2038 : }
2039 :
2040 : static unsigned long
2041 522863 : xfs_buftarg_shrink_scan(
2042 : struct shrinker *shrink,
2043 : struct shrink_control *sc)
2044 : {
2045 522863 : struct xfs_buftarg *btp = container_of(shrink,
2046 : struct xfs_buftarg, bt_shrinker);
2047 522863 : LIST_HEAD(dispose);
2048 522863 : unsigned long freed;
2049 :
2050 522863 : freed = list_lru_shrink_walk(&btp->bt_lru, sc,
2051 : xfs_buftarg_isolate, &dispose);
2052 :
2053 21379687 : while (!list_empty(&dispose)) {
2054 20856824 : struct xfs_buf *bp;
2055 20856824 : bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
2056 20856824 : list_del_init(&bp->b_lru);
2057 20856824 : xfs_buf_rele(bp);
2058 : }
2059 :
2060 522863 : return freed;
2061 : }
2062 :
2063 : static unsigned long
2064 8058 : xfs_buftarg_shrink_count(
2065 : struct shrinker *shrink,
2066 : struct shrink_control *sc)
2067 : {
2068 8058 : struct xfs_buftarg *btp = container_of(shrink,
2069 : struct xfs_buftarg, bt_shrinker);
2070 8058 : return list_lru_shrink_count(&btp->bt_lru, sc);
2071 : }
2072 :
2073 : void
2074 105314 : xfs_free_buftarg(
2075 : struct xfs_buftarg *btp)
2076 : {
2077 105314 : unregister_shrinker(&btp->bt_shrinker);
2078 105348 : ASSERT(percpu_counter_sum(&btp->bt_io_count) == 0);
2079 105348 : percpu_counter_destroy(&btp->bt_io_count);
2080 105348 : list_lru_destroy(&btp->bt_lru);
2081 :
2082 105348 : if (!(btp->bt_flags & XFS_BUFTARG_XFILE)) {
2083 24628 : blkdev_issue_flush(btp->bt_bdev);
2084 24628 : invalidate_bdev(btp->bt_bdev);
2085 24628 : fs_put_dax(btp->bt_daxdev, btp->bt_mount);
2086 : }
2087 :
2088 105348 : kvfree(btp);
2089 105348 : }
2090 :
2091 : int
2092 49210 : xfs_setsize_buftarg(
2093 : xfs_buftarg_t *btp,
2094 : unsigned int sectorsize)
2095 : {
2096 : /* Set up metadata sector size info */
2097 49210 : btp->bt_meta_sectorsize = sectorsize;
2098 49210 : btp->bt_meta_sectormask = sectorsize - 1;
2099 :
2100 49210 : 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 49210 : btp->bt_logical_sectorsize = bdev_logical_block_size(btp->bt_bdev);
2109 49210 : btp->bt_logical_sectormask = bdev_logical_block_size(btp->bt_bdev) - 1;
2110 :
2111 49210 : 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 24620 : xfs_setsize_buftarg_early(
2121 : xfs_buftarg_t *btp,
2122 : struct block_device *bdev)
2123 : {
2124 49240 : return xfs_setsize_buftarg(btp, bdev_logical_block_size(bdev));
2125 : }
2126 :
2127 : struct xfs_buftarg *
2128 105278 : xfs_alloc_buftarg_common(
2129 : struct xfs_mount *mp,
2130 : const char *descr)
2131 : {
2132 105278 : struct xfs_buftarg *btp;
2133 :
2134 105278 : btp = kzalloc(sizeof(*btp), GFP_NOFS);
2135 105253 : if (!btp)
2136 : return NULL;
2137 :
2138 105253 : 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 105253 : ratelimit_state_init(&btp->bt_ioerror_rl, 30 * HZ,
2145 : DEFAULT_RATELIMIT_BURST);
2146 :
2147 105256 : if (list_lru_init(&btp->bt_lru))
2148 0 : goto error_free;
2149 :
2150 105275 : if (percpu_counter_init(&btp->bt_io_count, 0, GFP_NOFS))
2151 0 : goto error_lru;
2152 :
2153 105340 : btp->bt_shrinker.count_objects = xfs_buftarg_shrink_count;
2154 105340 : btp->bt_shrinker.scan_objects = xfs_buftarg_shrink_scan;
2155 105340 : btp->bt_shrinker.seeks = DEFAULT_SEEKS;
2156 105340 : btp->bt_shrinker.flags = SHRINKER_NUMA_AWARE;
2157 105340 : if (register_shrinker(&btp->bt_shrinker, "xfs-%s:%s", descr,
2158 105340 : 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 180531709 : xfs_buf_list_del(
2174 : struct xfs_buf *bp)
2175 : {
2176 180531709 : list_del_init(&bp->b_list);
2177 180531635 : wake_up_var(&bp->b_list);
2178 180531620 : }
2179 :
2180 : /* Allocate a buffer cache target for a persistent block device. */
2181 : struct xfs_buftarg *
2182 24620 : xfs_alloc_buftarg(
2183 : struct xfs_mount *mp,
2184 : struct block_device *bdev)
2185 : {
2186 24620 : struct xfs_buftarg *btp;
2187 24620 : const struct dax_holder_operations *ops = NULL;
2188 :
2189 : #if defined(CONFIG_FS_DAX) && defined(CONFIG_MEMORY_FAILURE)
2190 : ops = &xfs_dax_holder_operations;
2191 : #endif
2192 :
2193 24620 : btp = xfs_alloc_buftarg_common(mp, "buf");
2194 24620 : if (!btp)
2195 : return NULL;
2196 :
2197 24620 : btp->bt_dev = bdev->bd_dev;
2198 24620 : btp->bt_bdev = bdev;
2199 24620 : btp->bt_daxdev = fs_dax_get_by_bdev(bdev, &btp->bt_dax_part_off,
2200 : mp, ops);
2201 :
2202 24620 : 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 428761 : xfs_buf_delwri_cancel(
2220 : struct list_head *list)
2221 : {
2222 428761 : struct xfs_buf *bp;
2223 :
2224 428757 : 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 428757 : }
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 193831552 : xfs_buf_delwri_queue(
2247 : struct xfs_buf *bp,
2248 : struct list_head *list)
2249 : {
2250 193831552 : ASSERT(xfs_buf_islocked(bp));
2251 193831552 : 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 193831552 : if (bp->b_flags & _XBF_DELWRI_Q) {
2259 13299601 : trace_xfs_buf_delwri_queued(bp, _RET_IP_);
2260 13299601 : return false;
2261 : }
2262 :
2263 180531951 : 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 180531875 : bp->b_flags |= _XBF_DELWRI_Q;
2274 180531875 : if (list_empty(&bp->b_list)) {
2275 180531725 : atomic_inc(&bp->b_hold);
2276 180531701 : 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 3143066 : 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 3143085 : while (!list_empty(&bp->b_list)) {
2300 17 : xfs_buf_unlock(bp);
2301 34 : wait_var_event(&bp->b_list, list_empty(&bp->b_list));
2302 17 : xfs_buf_lock(bp);
2303 : }
2304 :
2305 3143068 : ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
2306 :
2307 : /* This buffer is uptodate; don't let it get reread. */
2308 3143068 : bp->b_flags |= XBF_DONE;
2309 :
2310 3143068 : xfs_buf_delwri_queue(bp, buffer_list);
2311 3143028 : }
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 1658614382 : xfs_buf_cmp(
2320 : void *priv,
2321 : const struct list_head *a,
2322 : const struct list_head *b)
2323 : {
2324 1658614382 : struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
2325 1658614382 : struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
2326 1658614382 : xfs_daddr_t diff;
2327 :
2328 1658614382 : diff = ap->b_maps[0].bm_bn - bp->b_maps[0].bm_bn;
2329 1658614382 : if (diff < 0)
2330 : return -1;
2331 822969207 : if (diff > 0)
2332 822892367 : 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 4550688 : xfs_buf_delwri_submit_buffers(
2345 : struct list_head *buffer_list,
2346 : struct list_head *wait_list)
2347 : {
2348 4550688 : struct xfs_buf *bp, *n;
2349 4550688 : int pinned = 0;
2350 4550688 : struct blk_plug plug;
2351 :
2352 4550688 : list_sort(NULL, buffer_list, xfs_buf_cmp);
2353 :
2354 4550669 : blk_start_plug(&plug);
2355 186974820 : list_for_each_entry_safe(bp, n, buffer_list, b_list) {
2356 182424169 : if (!wait_list) {
2357 158544358 : if (!xfs_buf_trylock(bp))
2358 72335 : continue;
2359 158472023 : if (xfs_buf_ispinned(bp)) {
2360 1819925 : xfs_buf_unlock(bp);
2361 1819925 : pinned++;
2362 1819925 : continue;
2363 : }
2364 : } else {
2365 23879811 : 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 180531849 : if (!(bp->b_flags & _XBF_DELWRI_Q)) {
2375 90718 : xfs_buf_list_del(bp);
2376 90718 : xfs_buf_relse(bp);
2377 90718 : continue;
2378 : }
2379 :
2380 180441131 : 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 180441206 : bp->b_flags &= ~_XBF_DELWRI_Q;
2389 180441206 : bp->b_flags |= XBF_WRITE;
2390 180441206 : if (wait_list) {
2391 23879826 : bp->b_flags &= ~XBF_ASYNC;
2392 23879826 : list_move_tail(&bp->b_list, wait_list);
2393 : } else {
2394 156561380 : bp->b_flags |= XBF_ASYNC;
2395 156561380 : xfs_buf_list_del(bp);
2396 : }
2397 180441198 : __xfs_buf_submit(bp, false);
2398 : }
2399 4550651 : blk_finish_plug(&plug);
2400 :
2401 4550669 : 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 3120476 : xfs_buf_delwri_submit_nowait(
2422 : struct list_head *buffer_list)
2423 : {
2424 3120476 : 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 1430195 : xfs_buf_delwri_submit(
2437 : struct list_head *buffer_list)
2438 : {
2439 1430195 : LIST_HEAD (wait_list);
2440 1430195 : int error = 0, error2;
2441 1430195 : struct xfs_buf *bp;
2442 :
2443 1430195 : xfs_buf_delwri_submit_buffers(buffer_list, &wait_list);
2444 :
2445 : /* Wait for IO to complete. */
2446 25310042 : while (!list_empty(&wait_list)) {
2447 23879841 : bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
2448 :
2449 23879841 : 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 23879835 : error2 = xfs_buf_iowait(bp);
2456 23879844 : xfs_buf_relse(bp);
2457 23879847 : if (!error)
2458 23879844 : error = error2;
2459 : }
2460 :
2461 1430211 : 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 19346404892 : 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 19346404892 : if (XFS_TEST_ERROR(false, bp->b_mount, XFS_ERRTAG_BUF_LRU_REF))
2527 20 : lru_ref = 0;
2528 :
2529 19349467967 : atomic_set(&bp->b_lru_ref, lru_ref);
2530 19349467967 : }
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 885727428 : xfs_verify_magic(
2539 : struct xfs_buf *bp,
2540 : __be32 dmagic)
2541 : {
2542 885727428 : struct xfs_mount *mp = bp->b_mount;
2543 885727428 : int idx;
2544 :
2545 885727428 : idx = xfs_has_crc(mp);
2546 1771457024 : if (WARN_ON(!bp->b_ops || !bp->b_ops->magic[idx]))
2547 : return false;
2548 885727428 : 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 2057937153 : xfs_verify_magic16(
2557 : struct xfs_buf *bp,
2558 : __be16 dmagic)
2559 : {
2560 2057937153 : struct xfs_mount *mp = bp->b_mount;
2561 2057937153 : int idx;
2562 :
2563 2057937153 : idx = xfs_has_crc(mp);
2564 4115876497 : if (WARN_ON(!bp->b_ops || !bp->b_ops->magic16[idx]))
2565 : return false;
2566 2057937153 : return dmagic == bp->b_ops->magic16[idx];
2567 : }
2568 :
2569 : /* Return the number of sectors for a buffer target. */
2570 : xfs_daddr_t
2571 2761619397 : xfs_buftarg_nr_sectors(
2572 : struct xfs_buftarg *btp)
2573 : {
2574 2761619397 : if (btp->bt_flags & XFS_BUFTARG_XFILE)
2575 2761619397 : return xfile_buftarg_nr_sectors(btp);
2576 0 : return bdev_nr_sectors(btp->bt_bdev);
2577 : }
|