1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
7 #include <linux/backing-dev.h>
9 #include "xfs_shared.h"
10 #include "xfs_format.h"
11 #include "xfs_log_format.h"
12 #include "xfs_trans_resv.h"
14 #include "xfs_mount.h"
15 #include "xfs_trace.h"
17 #include "xfs_log_recover.h"
18 #include "xfs_trans.h"
19 #include "xfs_buf_item.h"
20 #include "xfs_errortag.h"
21 #include "xfs_error.h"
23 static kmem_zone_t
*xfs_buf_zone
;
25 #define xb_to_gfp(flags) \
26 ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : GFP_NOFS) | __GFP_NOWARN)
33 * b_sema (caller holds)
37 * b_sema (caller holds)
46 * xfs_buftarg_wait_rele
48 * b_lock (trylock due to inversion)
52 * b_lock (trylock due to inversion)
55 static int __xfs_buf_submit(struct xfs_buf
*bp
, bool wait
);
61 return __xfs_buf_submit(bp
, !(bp
->b_flags
& XBF_ASYNC
));
69 * Return true if the buffer is vmapped.
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.
75 return bp
->b_addr
&& bp
->b_page_count
> 1;
82 return (bp
->b_page_count
* PAGE_SIZE
) - bp
->b_offset
;
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_wait_buftarg()), so all we really need is a count of
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.
102 if (bp
->b_flags
& XBF_NO_IOACCT
)
105 ASSERT(bp
->b_flags
& XBF_ASYNC
);
106 spin_lock(&bp
->b_lock
);
107 if (!(bp
->b_state
& XFS_BSTATE_IN_FLIGHT
)) {
108 bp
->b_state
|= XFS_BSTATE_IN_FLIGHT
;
109 percpu_counter_inc(&bp
->b_target
->bt_io_count
);
111 spin_unlock(&bp
->b_lock
);
115 * Clear the in-flight state on a buffer about to be released to the LRU or
116 * freed and unaccount from the buftarg.
119 __xfs_buf_ioacct_dec(
122 lockdep_assert_held(&bp
->b_lock
);
124 if (bp
->b_state
& XFS_BSTATE_IN_FLIGHT
) {
125 bp
->b_state
&= ~XFS_BSTATE_IN_FLIGHT
;
126 percpu_counter_dec(&bp
->b_target
->bt_io_count
);
134 spin_lock(&bp
->b_lock
);
135 __xfs_buf_ioacct_dec(bp
);
136 spin_unlock(&bp
->b_lock
);
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.
145 * This prevents build-up of stale buffers on the LRU.
151 ASSERT(xfs_buf_islocked(bp
));
153 bp
->b_flags
|= XBF_STALE
;
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.
160 bp
->b_flags
&= ~_XBF_DELWRI_Q
;
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.
168 spin_lock(&bp
->b_lock
);
169 __xfs_buf_ioacct_dec(bp
);
171 atomic_set(&bp
->b_lru_ref
, 0);
172 if (!(bp
->b_state
& XFS_BSTATE_DISPOSE
) &&
173 (list_lru_del(&bp
->b_target
->bt_lru
, &bp
->b_lru
)))
174 atomic_dec(&bp
->b_hold
);
176 ASSERT(atomic_read(&bp
->b_hold
) >= 1);
177 spin_unlock(&bp
->b_lock
);
185 ASSERT(bp
->b_maps
== NULL
);
186 bp
->b_map_count
= map_count
;
188 if (map_count
== 1) {
189 bp
->b_maps
= &bp
->__b_map
;
193 bp
->b_maps
= kmem_zalloc(map_count
* sizeof(struct xfs_buf_map
),
201 * Frees b_pages if it was allocated.
207 if (bp
->b_maps
!= &bp
->__b_map
) {
208 kmem_free(bp
->b_maps
);
215 struct xfs_buftarg
*target
,
216 struct xfs_buf_map
*map
,
218 xfs_buf_flags_t flags
,
219 struct xfs_buf
**bpp
)
226 bp
= kmem_cache_zalloc(xfs_buf_zone
, GFP_NOFS
| __GFP_NOFAIL
);
229 * We don't want certain flags to appear in b_flags unless they are
230 * specifically set by later operations on the buffer.
232 flags
&= ~(XBF_UNMAPPED
| XBF_TRYLOCK
| XBF_ASYNC
| XBF_READ_AHEAD
);
234 atomic_set(&bp
->b_hold
, 1);
235 atomic_set(&bp
->b_lru_ref
, 1);
236 init_completion(&bp
->b_iowait
);
237 INIT_LIST_HEAD(&bp
->b_lru
);
238 INIT_LIST_HEAD(&bp
->b_list
);
239 INIT_LIST_HEAD(&bp
->b_li_list
);
240 sema_init(&bp
->b_sema
, 0); /* held, no waiters */
241 spin_lock_init(&bp
->b_lock
);
242 bp
->b_target
= target
;
243 bp
->b_mount
= target
->bt_mount
;
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).
251 error
= xfs_buf_get_maps(bp
, nmaps
);
253 kmem_cache_free(xfs_buf_zone
, bp
);
257 bp
->b_bn
= map
[0].bm_bn
;
259 for (i
= 0; i
< nmaps
; i
++) {
260 bp
->b_maps
[i
].bm_bn
= map
[i
].bm_bn
;
261 bp
->b_maps
[i
].bm_len
= map
[i
].bm_len
;
262 bp
->b_length
+= map
[i
].bm_len
;
265 atomic_set(&bp
->b_pin_count
, 0);
266 init_waitqueue_head(&bp
->b_waiters
);
268 XFS_STATS_INC(bp
->b_mount
, xb_create
);
269 trace_xfs_buf_init(bp
, _RET_IP_
);
276 * Allocate a page array capable of holding a specified number
277 * of pages, and point the page buf at it.
284 /* Make sure that we have a page list */
285 if (bp
->b_pages
== NULL
) {
286 bp
->b_page_count
= page_count
;
287 if (page_count
<= XB_PAGES
) {
288 bp
->b_pages
= bp
->b_page_array
;
290 bp
->b_pages
= kmem_alloc(sizeof(struct page
*) *
291 page_count
, KM_NOFS
);
292 if (bp
->b_pages
== NULL
)
295 memset(bp
->b_pages
, 0, sizeof(struct page
*) * page_count
);
301 * Frees b_pages if it was allocated.
307 if (bp
->b_pages
!= bp
->b_page_array
) {
308 kmem_free(bp
->b_pages
);
314 * Releases the specified buffer.
316 * The modification state of any associated pages is left unchanged.
317 * The buffer must not be on any hash - use xfs_buf_rele instead for
318 * hashed and refcounted buffers
324 trace_xfs_buf_free(bp
, _RET_IP_
);
326 ASSERT(list_empty(&bp
->b_lru
));
328 if (bp
->b_flags
& _XBF_PAGES
) {
331 if (xfs_buf_is_vmapped(bp
))
332 vm_unmap_ram(bp
->b_addr
- bp
->b_offset
,
335 for (i
= 0; i
< bp
->b_page_count
; i
++) {
336 struct page
*page
= bp
->b_pages
[i
];
340 if (current
->reclaim_state
)
341 current
->reclaim_state
->reclaimed_slab
+=
343 } else if (bp
->b_flags
& _XBF_KMEM
)
344 kmem_free(bp
->b_addr
);
345 _xfs_buf_free_pages(bp
);
346 xfs_buf_free_maps(bp
);
347 kmem_cache_free(xfs_buf_zone
, bp
);
351 * Allocates all the pages for buffer in question and builds it's page list.
354 xfs_buf_allocate_memory(
359 size_t nbytes
, offset
;
360 gfp_t gfp_mask
= xb_to_gfp(flags
);
361 unsigned short page_count
, i
;
362 xfs_off_t start
, end
;
364 xfs_km_flags_t kmflag_mask
= 0;
367 * assure zeroed buffer for non-read cases.
369 if (!(flags
& XBF_READ
)) {
370 kmflag_mask
|= KM_ZERO
;
371 gfp_mask
|= __GFP_ZERO
;
375 * for buffers that are contained within a single page, just allocate
376 * the memory from the heap - there's no need for the complexity of
377 * page arrays to keep allocation down to order 0.
379 size
= BBTOB(bp
->b_length
);
380 if (size
< PAGE_SIZE
) {
381 int align_mask
= xfs_buftarg_dma_alignment(bp
->b_target
);
382 bp
->b_addr
= kmem_alloc_io(size
, align_mask
,
383 KM_NOFS
| kmflag_mask
);
385 /* low memory - use alloc_page loop instead */
389 if (((unsigned long)(bp
->b_addr
+ size
- 1) & PAGE_MASK
) !=
390 ((unsigned long)bp
->b_addr
& PAGE_MASK
)) {
391 /* b_addr spans two pages - use alloc_page instead */
392 kmem_free(bp
->b_addr
);
396 bp
->b_offset
= offset_in_page(bp
->b_addr
);
397 bp
->b_pages
= bp
->b_page_array
;
398 bp
->b_pages
[0] = kmem_to_page(bp
->b_addr
);
399 bp
->b_page_count
= 1;
400 bp
->b_flags
|= _XBF_KMEM
;
405 start
= BBTOB(bp
->b_maps
[0].bm_bn
) >> PAGE_SHIFT
;
406 end
= (BBTOB(bp
->b_maps
[0].bm_bn
+ bp
->b_length
) + PAGE_SIZE
- 1)
408 page_count
= end
- start
;
409 error
= _xfs_buf_get_pages(bp
, page_count
);
413 offset
= bp
->b_offset
;
414 bp
->b_flags
|= _XBF_PAGES
;
416 for (i
= 0; i
< bp
->b_page_count
; i
++) {
420 page
= alloc_page(gfp_mask
);
421 if (unlikely(page
== NULL
)) {
422 if (flags
& XBF_READ_AHEAD
) {
423 bp
->b_page_count
= i
;
429 * This could deadlock.
431 * But until all the XFS lowlevel code is revamped to
432 * handle buffer allocation failures we can't do much.
434 if (!(++retries
% 100))
436 "%s(%u) possible memory allocation deadlock in %s (mode:0x%x)",
437 current
->comm
, current
->pid
,
440 XFS_STATS_INC(bp
->b_mount
, xb_page_retries
);
441 congestion_wait(BLK_RW_ASYNC
, HZ
/50);
445 XFS_STATS_INC(bp
->b_mount
, xb_page_found
);
447 nbytes
= min_t(size_t, size
, PAGE_SIZE
- offset
);
449 bp
->b_pages
[i
] = page
;
455 for (i
= 0; i
< bp
->b_page_count
; i
++)
456 __free_page(bp
->b_pages
[i
]);
457 bp
->b_flags
&= ~_XBF_PAGES
;
462 * Map buffer into kernel address-space if necessary.
469 ASSERT(bp
->b_flags
& _XBF_PAGES
);
470 if (bp
->b_page_count
== 1) {
471 /* A single page buffer is always mappable */
472 bp
->b_addr
= page_address(bp
->b_pages
[0]) + bp
->b_offset
;
473 } else if (flags
& XBF_UNMAPPED
) {
480 * vm_map_ram() will allocate auxiliary structures (e.g.
481 * pagetables) with GFP_KERNEL, yet we are likely to be under
482 * GFP_NOFS context here. Hence we need to tell memory reclaim
483 * that we are in such a context via PF_MEMALLOC_NOFS to prevent
484 * memory reclaim re-entering the filesystem here and
485 * potentially deadlocking.
487 nofs_flag
= memalloc_nofs_save();
489 bp
->b_addr
= vm_map_ram(bp
->b_pages
, bp
->b_page_count
,
494 } while (retried
++ <= 1);
495 memalloc_nofs_restore(nofs_flag
);
499 bp
->b_addr
+= bp
->b_offset
;
506 * Finding and Reading Buffers
510 struct rhashtable_compare_arg
*arg
,
513 const struct xfs_buf_map
*map
= arg
->key
;
514 const struct xfs_buf
*bp
= obj
;
517 * The key hashing in the lookup path depends on the key being the
518 * first element of the compare_arg, make sure to assert this.
520 BUILD_BUG_ON(offsetof(struct xfs_buf_map
, bm_bn
) != 0);
522 if (bp
->b_bn
!= map
->bm_bn
)
525 if (unlikely(bp
->b_length
!= map
->bm_len
)) {
527 * found a block number match. If the range doesn't
528 * match, the only way this is allowed is if the buffer
529 * in the cache is stale and the transaction that made
530 * it stale has not yet committed. i.e. we are
531 * reallocating a busy extent. Skip this buffer and
532 * continue searching for an exact match.
534 ASSERT(bp
->b_flags
& XBF_STALE
);
540 static const struct rhashtable_params xfs_buf_hash_params
= {
541 .min_size
= 32, /* empty AGs have minimal footprint */
543 .key_len
= sizeof(xfs_daddr_t
),
544 .key_offset
= offsetof(struct xfs_buf
, b_bn
),
545 .head_offset
= offsetof(struct xfs_buf
, b_rhash_head
),
546 .automatic_shrinking
= true,
547 .obj_cmpfn
= _xfs_buf_obj_cmp
,
552 struct xfs_perag
*pag
)
554 spin_lock_init(&pag
->pag_buf_lock
);
555 return rhashtable_init(&pag
->pag_buf_hash
, &xfs_buf_hash_params
);
559 xfs_buf_hash_destroy(
560 struct xfs_perag
*pag
)
562 rhashtable_destroy(&pag
->pag_buf_hash
);
566 * Look up a buffer in the buffer cache and return it referenced and locked
569 * If @new_bp is supplied and we have a lookup miss, insert @new_bp into the
572 * If XBF_TRYLOCK is set in @flags, only try to lock the buffer and return
573 * -EAGAIN if we fail to lock it.
576 * -EFSCORRUPTED if have been supplied with an invalid address
577 * -EAGAIN on trylock failure
578 * -ENOENT if we fail to find a match and @new_bp was NULL
580 * - @new_bp if we inserted it into the cache
581 * - the buffer we found and locked.
585 struct xfs_buftarg
*btp
,
586 struct xfs_buf_map
*map
,
588 xfs_buf_flags_t flags
,
589 struct xfs_buf
*new_bp
,
590 struct xfs_buf
**found_bp
)
592 struct xfs_perag
*pag
;
594 struct xfs_buf_map cmap
= { .bm_bn
= map
[0].bm_bn
};
600 for (i
= 0; i
< nmaps
; i
++)
601 cmap
.bm_len
+= map
[i
].bm_len
;
603 /* Check for IOs smaller than the sector size / not sector aligned */
604 ASSERT(!(BBTOB(cmap
.bm_len
) < btp
->bt_meta_sectorsize
));
605 ASSERT(!(BBTOB(cmap
.bm_bn
) & (xfs_off_t
)btp
->bt_meta_sectormask
));
608 * Corrupted block numbers can get through to here, unfortunately, so we
609 * have to check that the buffer falls within the filesystem bounds.
611 eofs
= XFS_FSB_TO_BB(btp
->bt_mount
, btp
->bt_mount
->m_sb
.sb_dblocks
);
612 if (cmap
.bm_bn
< 0 || cmap
.bm_bn
>= eofs
) {
613 xfs_alert(btp
->bt_mount
,
614 "%s: daddr 0x%llx out of range, EOFS 0x%llx",
615 __func__
, cmap
.bm_bn
, eofs
);
617 return -EFSCORRUPTED
;
620 pag
= xfs_perag_get(btp
->bt_mount
,
621 xfs_daddr_to_agno(btp
->bt_mount
, cmap
.bm_bn
));
623 spin_lock(&pag
->pag_buf_lock
);
624 bp
= rhashtable_lookup_fast(&pag
->pag_buf_hash
, &cmap
,
625 xfs_buf_hash_params
);
627 atomic_inc(&bp
->b_hold
);
633 XFS_STATS_INC(btp
->bt_mount
, xb_miss_locked
);
634 spin_unlock(&pag
->pag_buf_lock
);
639 /* the buffer keeps the perag reference until it is freed */
641 rhashtable_insert_fast(&pag
->pag_buf_hash
, &new_bp
->b_rhash_head
,
642 xfs_buf_hash_params
);
643 spin_unlock(&pag
->pag_buf_lock
);
648 spin_unlock(&pag
->pag_buf_lock
);
651 if (!xfs_buf_trylock(bp
)) {
652 if (flags
& XBF_TRYLOCK
) {
654 XFS_STATS_INC(btp
->bt_mount
, xb_busy_locked
);
658 XFS_STATS_INC(btp
->bt_mount
, xb_get_locked_waited
);
662 * if the buffer is stale, clear all the external state associated with
663 * it. We need to keep flags such as how we allocated the buffer memory
666 if (bp
->b_flags
& XBF_STALE
) {
667 ASSERT((bp
->b_flags
& _XBF_DELWRI_Q
) == 0);
668 bp
->b_flags
&= _XBF_KMEM
| _XBF_PAGES
;
672 trace_xfs_buf_find(bp
, flags
, _RET_IP_
);
673 XFS_STATS_INC(btp
->bt_mount
, xb_get_locked
);
680 struct xfs_buftarg
*target
,
683 xfs_buf_flags_t flags
)
687 DEFINE_SINGLE_BUF_MAP(map
, blkno
, numblks
);
689 error
= xfs_buf_find(target
, &map
, 1, flags
, NULL
, &bp
);
696 * Assembles a buffer covering the specified range. The code is optimised for
697 * cache hits, as metadata intensive workloads will see 3 orders of magnitude
698 * more hits than misses.
702 struct xfs_buftarg
*target
,
703 struct xfs_buf_map
*map
,
705 xfs_buf_flags_t flags
,
706 struct xfs_buf
**bpp
)
709 struct xfs_buf
*new_bp
;
713 error
= xfs_buf_find(target
, map
, nmaps
, flags
, NULL
, &bp
);
716 if (error
!= -ENOENT
)
719 error
= _xfs_buf_alloc(target
, map
, nmaps
, flags
, &new_bp
);
723 error
= xfs_buf_allocate_memory(new_bp
, flags
);
725 xfs_buf_free(new_bp
);
729 error
= xfs_buf_find(target
, map
, nmaps
, flags
, new_bp
, &bp
);
731 xfs_buf_free(new_bp
);
736 xfs_buf_free(new_bp
);
740 error
= _xfs_buf_map_pages(bp
, flags
);
741 if (unlikely(error
)) {
742 xfs_warn_ratelimited(target
->bt_mount
,
743 "%s: failed to map %u pages", __func__
,
751 * Clear b_error if this is a lookup from a caller that doesn't expect
752 * valid data to be found in the buffer.
754 if (!(flags
& XBF_READ
))
755 xfs_buf_ioerror(bp
, 0);
757 XFS_STATS_INC(target
->bt_mount
, xb_get
);
758 trace_xfs_buf_get(bp
, flags
, _RET_IP_
);
766 xfs_buf_flags_t flags
)
768 ASSERT(!(flags
& XBF_WRITE
));
769 ASSERT(bp
->b_maps
[0].bm_bn
!= XFS_BUF_DADDR_NULL
);
771 bp
->b_flags
&= ~(XBF_WRITE
| XBF_ASYNC
| XBF_READ_AHEAD
| XBF_DONE
);
772 bp
->b_flags
|= flags
& (XBF_READ
| XBF_ASYNC
| XBF_READ_AHEAD
);
774 return xfs_buf_submit(bp
);
778 * Reverify a buffer found in cache without an attached ->b_ops.
780 * If the caller passed an ops structure and the buffer doesn't have ops
781 * assigned, set the ops and use it to verify the contents. If verification
782 * fails, clear XBF_DONE. We assume the buffer has no recorded errors and is
783 * already in XBF_DONE state on entry.
785 * Under normal operations, every in-core buffer is verified on read I/O
786 * completion. There are two scenarios that can lead to in-core buffers without
787 * an assigned ->b_ops. The first is during log recovery of buffers on a V4
788 * filesystem, though these buffers are purged at the end of recovery. The
789 * other is online repair, which intentionally reads with a NULL buffer ops to
790 * run several verifiers across an in-core buffer in order to establish buffer
791 * type. If repair can't establish that, the buffer will be left in memory
792 * with NULL buffer ops.
797 const struct xfs_buf_ops
*ops
)
799 ASSERT(bp
->b_flags
& XBF_DONE
);
800 ASSERT(bp
->b_error
== 0);
802 if (!ops
|| bp
->b_ops
)
806 bp
->b_ops
->verify_read(bp
);
808 bp
->b_flags
&= ~XBF_DONE
;
814 struct xfs_buftarg
*target
,
815 struct xfs_buf_map
*map
,
817 xfs_buf_flags_t flags
,
818 struct xfs_buf
**bpp
,
819 const struct xfs_buf_ops
*ops
,
828 error
= xfs_buf_get_map(target
, map
, nmaps
, flags
, &bp
);
832 trace_xfs_buf_read(bp
, flags
, _RET_IP_
);
834 if (!(bp
->b_flags
& XBF_DONE
)) {
835 /* Initiate the buffer read and wait. */
836 XFS_STATS_INC(target
->bt_mount
, xb_get_read
);
838 error
= _xfs_buf_read(bp
, flags
);
840 /* Readahead iodone already dropped the buffer, so exit. */
841 if (flags
& XBF_ASYNC
)
844 /* Buffer already read; all we need to do is check it. */
845 error
= xfs_buf_reverify(bp
, ops
);
847 /* Readahead already finished; drop the buffer and exit. */
848 if (flags
& XBF_ASYNC
) {
853 /* We do not want read in the flags */
854 bp
->b_flags
&= ~XBF_READ
;
855 ASSERT(bp
->b_ops
!= NULL
|| ops
== NULL
);
859 * If we've had a read error, then the contents of the buffer are
860 * invalid and should not be used. To ensure that a followup read tries
861 * to pull the buffer from disk again, we clear the XBF_DONE flag and
862 * mark the buffer stale. This ensures that anyone who has a current
863 * reference to the buffer will interpret it's contents correctly and
864 * future cache lookups will also treat it as an empty, uninitialised
868 if (!XFS_FORCED_SHUTDOWN(target
->bt_mount
))
869 xfs_buf_ioerror_alert(bp
, fa
);
871 bp
->b_flags
&= ~XBF_DONE
;
875 /* bad CRC means corrupted metadata */
876 if (error
== -EFSBADCRC
)
877 error
= -EFSCORRUPTED
;
886 * If we are not low on memory then do the readahead in a deadlock
890 xfs_buf_readahead_map(
891 struct xfs_buftarg
*target
,
892 struct xfs_buf_map
*map
,
894 const struct xfs_buf_ops
*ops
)
898 if (bdi_read_congested(target
->bt_bdev
->bd_bdi
))
901 xfs_buf_read_map(target
, map
, nmaps
,
902 XBF_TRYLOCK
| XBF_ASYNC
| XBF_READ_AHEAD
, &bp
, ops
,
907 * Read an uncached buffer from disk. Allocates and returns a locked
908 * buffer containing the disk contents or nothing.
911 xfs_buf_read_uncached(
912 struct xfs_buftarg
*target
,
916 struct xfs_buf
**bpp
,
917 const struct xfs_buf_ops
*ops
)
924 error
= xfs_buf_get_uncached(target
, numblks
, flags
, &bp
);
928 /* set up the buffer for a read IO */
929 ASSERT(bp
->b_map_count
== 1);
930 bp
->b_bn
= XFS_BUF_DADDR_NULL
; /* always null for uncached buffers */
931 bp
->b_maps
[0].bm_bn
= daddr
;
932 bp
->b_flags
|= XBF_READ
;
947 xfs_buf_get_uncached(
948 struct xfs_buftarg
*target
,
951 struct xfs_buf
**bpp
)
953 unsigned long page_count
;
956 DEFINE_SINGLE_BUF_MAP(map
, XFS_BUF_DADDR_NULL
, numblks
);
960 /* flags might contain irrelevant bits, pass only what we care about */
961 error
= _xfs_buf_alloc(target
, &map
, 1, flags
& XBF_NO_IOACCT
, &bp
);
965 page_count
= PAGE_ALIGN(numblks
<< BBSHIFT
) >> PAGE_SHIFT
;
966 error
= _xfs_buf_get_pages(bp
, page_count
);
970 for (i
= 0; i
< page_count
; i
++) {
971 bp
->b_pages
[i
] = alloc_page(xb_to_gfp(flags
));
972 if (!bp
->b_pages
[i
]) {
977 bp
->b_flags
|= _XBF_PAGES
;
979 error
= _xfs_buf_map_pages(bp
, 0);
980 if (unlikely(error
)) {
981 xfs_warn(target
->bt_mount
,
982 "%s: failed to map pages", __func__
);
986 trace_xfs_buf_get_uncached(bp
, _RET_IP_
);
992 __free_page(bp
->b_pages
[i
]);
993 _xfs_buf_free_pages(bp
);
995 xfs_buf_free_maps(bp
);
996 kmem_cache_free(xfs_buf_zone
, bp
);
1002 * Increment reference count on buffer, to hold the buffer concurrently
1003 * with another thread which may release (free) the buffer asynchronously.
1004 * Must hold the buffer already to call this function.
1010 trace_xfs_buf_hold(bp
, _RET_IP_
);
1011 atomic_inc(&bp
->b_hold
);
1015 * Release a hold on the specified buffer. If the hold count is 1, the buffer is
1016 * placed on LRU or freed (depending on b_lru_ref).
1022 struct xfs_perag
*pag
= bp
->b_pag
;
1024 bool freebuf
= false;
1026 trace_xfs_buf_rele(bp
, _RET_IP_
);
1029 ASSERT(list_empty(&bp
->b_lru
));
1030 if (atomic_dec_and_test(&bp
->b_hold
)) {
1031 xfs_buf_ioacct_dec(bp
);
1037 ASSERT(atomic_read(&bp
->b_hold
) > 0);
1040 * We grab the b_lock here first to serialise racing xfs_buf_rele()
1041 * calls. The pag_buf_lock being taken on the last reference only
1042 * serialises against racing lookups in xfs_buf_find(). IOWs, the second
1043 * to last reference we drop here is not serialised against the last
1044 * reference until we take bp->b_lock. Hence if we don't grab b_lock
1045 * first, the last "release" reference can win the race to the lock and
1046 * free the buffer before the second-to-last reference is processed,
1047 * leading to a use-after-free scenario.
1049 spin_lock(&bp
->b_lock
);
1050 release
= atomic_dec_and_lock(&bp
->b_hold
, &pag
->pag_buf_lock
);
1053 * Drop the in-flight state if the buffer is already on the LRU
1054 * and it holds the only reference. This is racy because we
1055 * haven't acquired the pag lock, but the use of _XBF_IN_FLIGHT
1056 * ensures the decrement occurs only once per-buf.
1058 if ((atomic_read(&bp
->b_hold
) == 1) && !list_empty(&bp
->b_lru
))
1059 __xfs_buf_ioacct_dec(bp
);
1063 /* the last reference has been dropped ... */
1064 __xfs_buf_ioacct_dec(bp
);
1065 if (!(bp
->b_flags
& XBF_STALE
) && atomic_read(&bp
->b_lru_ref
)) {
1067 * If the buffer is added to the LRU take a new reference to the
1068 * buffer for the LRU and clear the (now stale) dispose list
1071 if (list_lru_add(&bp
->b_target
->bt_lru
, &bp
->b_lru
)) {
1072 bp
->b_state
&= ~XFS_BSTATE_DISPOSE
;
1073 atomic_inc(&bp
->b_hold
);
1075 spin_unlock(&pag
->pag_buf_lock
);
1078 * most of the time buffers will already be removed from the
1079 * LRU, so optimise that case by checking for the
1080 * XFS_BSTATE_DISPOSE flag indicating the last list the buffer
1081 * was on was the disposal list
1083 if (!(bp
->b_state
& XFS_BSTATE_DISPOSE
)) {
1084 list_lru_del(&bp
->b_target
->bt_lru
, &bp
->b_lru
);
1086 ASSERT(list_empty(&bp
->b_lru
));
1089 ASSERT(!(bp
->b_flags
& _XBF_DELWRI_Q
));
1090 rhashtable_remove_fast(&pag
->pag_buf_hash
, &bp
->b_rhash_head
,
1091 xfs_buf_hash_params
);
1092 spin_unlock(&pag
->pag_buf_lock
);
1098 spin_unlock(&bp
->b_lock
);
1106 * Lock a buffer object, if it is not already locked.
1108 * If we come across a stale, pinned, locked buffer, we know that we are
1109 * being asked to lock a buffer that has been reallocated. Because it is
1110 * pinned, we know that the log has not been pushed to disk and hence it
1111 * will still be locked. Rather than continuing to have trylock attempts
1112 * fail until someone else pushes the log, push it ourselves before
1113 * returning. This means that the xfsaild will not get stuck trying
1114 * to push on stale inode buffers.
1122 locked
= down_trylock(&bp
->b_sema
) == 0;
1124 trace_xfs_buf_trylock(bp
, _RET_IP_
);
1126 trace_xfs_buf_trylock_fail(bp
, _RET_IP_
);
1131 * Lock a buffer object.
1133 * If we come across a stale, pinned, locked buffer, we know that we
1134 * are being asked to lock a buffer that has been reallocated. Because
1135 * it is pinned, we know that the log has not been pushed to disk and
1136 * hence it will still be locked. Rather than sleeping until someone
1137 * else pushes the log, push it ourselves before trying to get the lock.
1143 trace_xfs_buf_lock(bp
, _RET_IP_
);
1145 if (atomic_read(&bp
->b_pin_count
) && (bp
->b_flags
& XBF_STALE
))
1146 xfs_log_force(bp
->b_mount
, 0);
1149 trace_xfs_buf_lock_done(bp
, _RET_IP_
);
1156 ASSERT(xfs_buf_islocked(bp
));
1159 trace_xfs_buf_unlock(bp
, _RET_IP_
);
1166 DECLARE_WAITQUEUE (wait
, current
);
1168 if (atomic_read(&bp
->b_pin_count
) == 0)
1171 add_wait_queue(&bp
->b_waiters
, &wait
);
1173 set_current_state(TASK_UNINTERRUPTIBLE
);
1174 if (atomic_read(&bp
->b_pin_count
) == 0)
1178 remove_wait_queue(&bp
->b_waiters
, &wait
);
1179 set_current_state(TASK_RUNNING
);
1183 xfs_buf_ioerror_alert_ratelimited(
1186 static unsigned long lasttime
;
1187 static struct xfs_buftarg
*lasttarg
;
1189 if (bp
->b_target
!= lasttarg
||
1190 time_after(jiffies
, (lasttime
+ 5*HZ
))) {
1192 xfs_buf_ioerror_alert(bp
, __this_address
);
1194 lasttarg
= bp
->b_target
;
1198 * Account for this latest trip around the retry handler, and decide if
1199 * we've failed enough times to constitute a permanent failure.
1202 xfs_buf_ioerror_permanent(
1204 struct xfs_error_cfg
*cfg
)
1206 struct xfs_mount
*mp
= bp
->b_mount
;
1208 if (cfg
->max_retries
!= XFS_ERR_RETRY_FOREVER
&&
1209 ++bp
->b_retries
> cfg
->max_retries
)
1211 if (cfg
->retry_timeout
!= XFS_ERR_RETRY_FOREVER
&&
1212 time_after(jiffies
, cfg
->retry_timeout
+ bp
->b_first_retry_time
))
1215 /* At unmount we may treat errors differently */
1216 if ((mp
->m_flags
& XFS_MOUNT_UNMOUNTING
) && mp
->m_fail_unmount
)
1223 * On a sync write or shutdown we just want to stale the buffer and let the
1224 * caller handle the error in bp->b_error appropriately.
1226 * If the write was asynchronous then no one will be looking for the error. If
1227 * this is the first failure of this type, clear the error state and write the
1228 * buffer out again. This means we always retry an async write failure at least
1229 * once, but we also need to set the buffer up to behave correctly now for
1230 * repeated failures.
1232 * If we get repeated async write failures, then we take action according to the
1233 * error configuration we have been set up to use.
1235 * Returns true if this function took care of error handling and the caller must
1236 * not touch the buffer again. Return false if the caller should proceed with
1237 * normal I/O completion handling.
1240 xfs_buf_ioend_handle_error(
1243 struct xfs_mount
*mp
= bp
->b_mount
;
1244 struct xfs_error_cfg
*cfg
;
1247 * If we've already decided to shutdown the filesystem because of I/O
1248 * errors, there's no point in giving this a retry.
1250 if (XFS_FORCED_SHUTDOWN(mp
))
1253 xfs_buf_ioerror_alert_ratelimited(bp
);
1256 * We're not going to bother about retrying this during recovery.
1259 if (bp
->b_flags
& _XBF_LOGRECOVERY
) {
1260 xfs_force_shutdown(mp
, SHUTDOWN_META_IO_ERROR
);
1265 * Synchronous writes will have callers process the error.
1267 if (!(bp
->b_flags
& XBF_ASYNC
))
1270 trace_xfs_buf_iodone_async(bp
, _RET_IP_
);
1272 cfg
= xfs_error_get_cfg(mp
, XFS_ERR_METADATA
, bp
->b_error
);
1273 if (bp
->b_last_error
!= bp
->b_error
||
1274 !(bp
->b_flags
& (XBF_STALE
| XBF_WRITE_FAIL
))) {
1275 bp
->b_last_error
= bp
->b_error
;
1276 if (cfg
->retry_timeout
!= XFS_ERR_RETRY_FOREVER
&&
1277 !bp
->b_first_retry_time
)
1278 bp
->b_first_retry_time
= jiffies
;
1283 * Permanent error - we need to trigger a shutdown if we haven't already
1284 * to indicate that inconsistency will result from this action.
1286 if (xfs_buf_ioerror_permanent(bp
, cfg
)) {
1287 xfs_force_shutdown(mp
, SHUTDOWN_META_IO_ERROR
);
1291 /* Still considered a transient error. Caller will schedule retries. */
1292 if (bp
->b_flags
& _XBF_INODES
)
1293 xfs_buf_inode_io_fail(bp
);
1294 else if (bp
->b_flags
& _XBF_DQUOTS
)
1295 xfs_buf_dquot_io_fail(bp
);
1297 ASSERT(list_empty(&bp
->b_li_list
));
1298 xfs_buf_ioerror(bp
, 0);
1303 xfs_buf_ioerror(bp
, 0);
1304 bp
->b_flags
|= (XBF_DONE
| XBF_WRITE_FAIL
);
1309 bp
->b_flags
|= XBF_DONE
;
1310 bp
->b_flags
&= ~XBF_WRITE
;
1311 trace_xfs_buf_error_relse(bp
, _RET_IP_
);
1319 trace_xfs_buf_iodone(bp
, _RET_IP_
);
1322 * Pull in IO completion errors now. We are guaranteed to be running
1323 * single threaded, so we don't need the lock to read b_io_error.
1325 if (!bp
->b_error
&& bp
->b_io_error
)
1326 xfs_buf_ioerror(bp
, bp
->b_io_error
);
1328 if (bp
->b_flags
& XBF_READ
) {
1329 if (!bp
->b_error
&& bp
->b_ops
)
1330 bp
->b_ops
->verify_read(bp
);
1332 bp
->b_flags
|= XBF_DONE
;
1335 bp
->b_flags
&= ~XBF_WRITE_FAIL
;
1336 bp
->b_flags
|= XBF_DONE
;
1339 if (unlikely(bp
->b_error
) && xfs_buf_ioend_handle_error(bp
))
1342 /* clear the retry state */
1343 bp
->b_last_error
= 0;
1345 bp
->b_first_retry_time
= 0;
1348 * Note that for things like remote attribute buffers, there may
1349 * not be a buffer log item here, so processing the buffer log
1350 * item must remain optional.
1353 xfs_buf_item_done(bp
);
1355 if (bp
->b_flags
& _XBF_INODES
)
1356 xfs_buf_inode_iodone(bp
);
1357 else if (bp
->b_flags
& _XBF_DQUOTS
)
1358 xfs_buf_dquot_iodone(bp
);
1362 bp
->b_flags
&= ~(XBF_READ
| XBF_WRITE
| XBF_READ_AHEAD
|
1365 if (bp
->b_flags
& XBF_ASYNC
)
1368 complete(&bp
->b_iowait
);
1373 struct work_struct
*work
)
1375 struct xfs_buf
*bp
=
1376 container_of(work
, struct xfs_buf
, b_ioend_work
);
1382 xfs_buf_ioend_async(
1385 INIT_WORK(&bp
->b_ioend_work
, xfs_buf_ioend_work
);
1386 queue_work(bp
->b_mount
->m_buf_workqueue
, &bp
->b_ioend_work
);
1393 xfs_failaddr_t failaddr
)
1395 ASSERT(error
<= 0 && error
>= -1000);
1396 bp
->b_error
= error
;
1397 trace_xfs_buf_ioerror(bp
, error
, failaddr
);
1401 xfs_buf_ioerror_alert(
1403 xfs_failaddr_t func
)
1405 xfs_buf_alert_ratelimited(bp
, "XFS: metadata IO error",
1406 "metadata I/O error in \"%pS\" at daddr 0x%llx len %d error %d",
1407 func
, (uint64_t)XFS_BUF_ADDR(bp
),
1408 bp
->b_length
, -bp
->b_error
);
1412 * To simulate an I/O failure, the buffer must be locked and held with at least
1413 * three references. The LRU reference is dropped by the stale call. The buf
1414 * item reference is dropped via ioend processing. The third reference is owned
1415 * by the caller and is dropped on I/O completion if the buffer is XBF_ASYNC.
1421 bp
->b_flags
&= ~XBF_DONE
;
1423 xfs_buf_ioerror(bp
, -EIO
);
1433 ASSERT(xfs_buf_islocked(bp
));
1435 bp
->b_flags
|= XBF_WRITE
;
1436 bp
->b_flags
&= ~(XBF_ASYNC
| XBF_READ
| _XBF_DELWRI_Q
|
1439 error
= xfs_buf_submit(bp
);
1441 xfs_force_shutdown(bp
->b_mount
, SHUTDOWN_META_IO_ERROR
);
1449 struct xfs_buf
*bp
= (struct xfs_buf
*)bio
->bi_private
;
1451 if (!bio
->bi_status
&&
1452 (bp
->b_flags
& XBF_WRITE
) && (bp
->b_flags
& XBF_ASYNC
) &&
1453 XFS_TEST_ERROR(false, bp
->b_mount
, XFS_ERRTAG_BUF_IOERROR
))
1454 bio
->bi_status
= BLK_STS_IOERR
;
1457 * don't overwrite existing errors - otherwise we can lose errors on
1458 * buffers that require multiple bios to complete.
1460 if (bio
->bi_status
) {
1461 int error
= blk_status_to_errno(bio
->bi_status
);
1463 cmpxchg(&bp
->b_io_error
, 0, error
);
1466 if (!bp
->b_error
&& xfs_buf_is_vmapped(bp
) && (bp
->b_flags
& XBF_READ
))
1467 invalidate_kernel_vmap_range(bp
->b_addr
, xfs_buf_vmap_len(bp
));
1469 if (atomic_dec_and_test(&bp
->b_io_remaining
) == 1)
1470 xfs_buf_ioend_async(bp
);
1475 xfs_buf_ioapply_map(
1483 int total_nr_pages
= bp
->b_page_count
;
1486 sector_t sector
= bp
->b_maps
[map
].bm_bn
;
1490 /* skip the pages in the buffer before the start offset */
1492 offset
= *buf_offset
;
1493 while (offset
>= PAGE_SIZE
) {
1495 offset
-= PAGE_SIZE
;
1499 * Limit the IO size to the length of the current vector, and update the
1500 * remaining IO count for the next time around.
1502 size
= min_t(int, BBTOB(bp
->b_maps
[map
].bm_len
), *count
);
1504 *buf_offset
+= size
;
1507 atomic_inc(&bp
->b_io_remaining
);
1508 nr_pages
= min(total_nr_pages
, BIO_MAX_PAGES
);
1510 bio
= bio_alloc(GFP_NOIO
, nr_pages
);
1511 bio_set_dev(bio
, bp
->b_target
->bt_bdev
);
1512 bio
->bi_iter
.bi_sector
= sector
;
1513 bio
->bi_end_io
= xfs_buf_bio_end_io
;
1514 bio
->bi_private
= bp
;
1517 for (; size
&& nr_pages
; nr_pages
--, page_index
++) {
1518 int rbytes
, nbytes
= PAGE_SIZE
- offset
;
1523 rbytes
= bio_add_page(bio
, bp
->b_pages
[page_index
], nbytes
,
1525 if (rbytes
< nbytes
)
1529 sector
+= BTOBB(nbytes
);
1534 if (likely(bio
->bi_iter
.bi_size
)) {
1535 if (xfs_buf_is_vmapped(bp
)) {
1536 flush_kernel_vmap_range(bp
->b_addr
,
1537 xfs_buf_vmap_len(bp
));
1544 * This is guaranteed not to be the last io reference count
1545 * because the caller (xfs_buf_submit) holds a count itself.
1547 atomic_dec(&bp
->b_io_remaining
);
1548 xfs_buf_ioerror(bp
, -EIO
);
1558 struct blk_plug plug
;
1565 * Make sure we capture only current IO errors rather than stale errors
1566 * left over from previous use of the buffer (e.g. failed readahead).
1570 if (bp
->b_flags
& XBF_WRITE
) {
1574 * Run the write verifier callback function if it exists. If
1575 * this function fails it will mark the buffer with an error and
1576 * the IO should not be dispatched.
1579 bp
->b_ops
->verify_write(bp
);
1581 xfs_force_shutdown(bp
->b_mount
,
1582 SHUTDOWN_CORRUPT_INCORE
);
1585 } else if (bp
->b_bn
!= XFS_BUF_DADDR_NULL
) {
1586 struct xfs_mount
*mp
= bp
->b_mount
;
1589 * non-crc filesystems don't attach verifiers during
1590 * log recovery, so don't warn for such filesystems.
1592 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
1594 "%s: no buf ops on daddr 0x%llx len %d",
1595 __func__
, bp
->b_bn
, bp
->b_length
);
1596 xfs_hex_dump(bp
->b_addr
,
1597 XFS_CORRUPTION_DUMP_LEN
);
1603 if (bp
->b_flags
& XBF_READ_AHEAD
)
1607 /* we only use the buffer cache for meta-data */
1611 * Walk all the vectors issuing IO on them. Set up the initial offset
1612 * into the buffer and the desired IO size before we start -
1613 * _xfs_buf_ioapply_vec() will modify them appropriately for each
1616 offset
= bp
->b_offset
;
1617 size
= BBTOB(bp
->b_length
);
1618 blk_start_plug(&plug
);
1619 for (i
= 0; i
< bp
->b_map_count
; i
++) {
1620 xfs_buf_ioapply_map(bp
, i
, &offset
, &size
, op
);
1624 break; /* all done */
1626 blk_finish_plug(&plug
);
1630 * Wait for I/O completion of a sync buffer and return the I/O error code.
1636 ASSERT(!(bp
->b_flags
& XBF_ASYNC
));
1638 trace_xfs_buf_iowait(bp
, _RET_IP_
);
1639 wait_for_completion(&bp
->b_iowait
);
1640 trace_xfs_buf_iowait_done(bp
, _RET_IP_
);
1646 * Buffer I/O submission path, read or write. Asynchronous submission transfers
1647 * the buffer lock ownership and the current reference to the IO. It is not
1648 * safe to reference the buffer after a call to this function unless the caller
1649 * holds an additional reference itself.
1658 trace_xfs_buf_submit(bp
, _RET_IP_
);
1660 ASSERT(!(bp
->b_flags
& _XBF_DELWRI_Q
));
1662 /* on shutdown we stale and complete the buffer immediately */
1663 if (XFS_FORCED_SHUTDOWN(bp
->b_mount
)) {
1664 xfs_buf_ioend_fail(bp
);
1669 * Grab a reference so the buffer does not go away underneath us. For
1670 * async buffers, I/O completion drops the callers reference, which
1671 * could occur before submission returns.
1675 if (bp
->b_flags
& XBF_WRITE
)
1676 xfs_buf_wait_unpin(bp
);
1678 /* clear the internal error state to avoid spurious errors */
1682 * Set the count to 1 initially, this will stop an I/O completion
1683 * callout which happens before we have started all the I/O from calling
1684 * xfs_buf_ioend too early.
1686 atomic_set(&bp
->b_io_remaining
, 1);
1687 if (bp
->b_flags
& XBF_ASYNC
)
1688 xfs_buf_ioacct_inc(bp
);
1689 _xfs_buf_ioapply(bp
);
1692 * If _xfs_buf_ioapply failed, we can get back here with only the IO
1693 * reference we took above. If we drop it to zero, run completion so
1694 * that we don't return to the caller with completion still pending.
1696 if (atomic_dec_and_test(&bp
->b_io_remaining
) == 1) {
1697 if (bp
->b_error
|| !(bp
->b_flags
& XBF_ASYNC
))
1700 xfs_buf_ioend_async(bp
);
1704 error
= xfs_buf_iowait(bp
);
1707 * Release the hold that keeps the buffer referenced for the entire
1708 * I/O. Note that if the buffer is async, it is not safe to reference
1709 * after this release.
1723 return bp
->b_addr
+ offset
;
1725 offset
+= bp
->b_offset
;
1726 page
= bp
->b_pages
[offset
>> PAGE_SHIFT
];
1727 return page_address(page
) + (offset
& (PAGE_SIZE
-1));
1738 bend
= boff
+ bsize
;
1739 while (boff
< bend
) {
1741 int page_index
, page_offset
, csize
;
1743 page_index
= (boff
+ bp
->b_offset
) >> PAGE_SHIFT
;
1744 page_offset
= (boff
+ bp
->b_offset
) & ~PAGE_MASK
;
1745 page
= bp
->b_pages
[page_index
];
1746 csize
= min_t(size_t, PAGE_SIZE
- page_offset
,
1747 BBTOB(bp
->b_length
) - boff
);
1749 ASSERT((csize
+ page_offset
) <= PAGE_SIZE
);
1751 memset(page_address(page
) + page_offset
, 0, csize
);
1758 * Log a message about and stale a buffer that a caller has decided is corrupt.
1760 * This function should be called for the kinds of metadata corruption that
1761 * cannot be detect from a verifier, such as incorrect inter-block relationship
1762 * data. Do /not/ call this function from a verifier function.
1764 * The buffer must be XBF_DONE prior to the call. Afterwards, the buffer will
1765 * be marked stale, but b_error will not be set. The caller is responsible for
1766 * releasing the buffer or fixing it.
1769 __xfs_buf_mark_corrupt(
1773 ASSERT(bp
->b_flags
& XBF_DONE
);
1775 xfs_buf_corruption_error(bp
, fa
);
1780 * Handling of buffer targets (buftargs).
1784 * Wait for any bufs with callbacks that have been submitted but have not yet
1785 * returned. These buffers will have an elevated hold count, so wait on those
1786 * while freeing all the buffers only held by the LRU.
1788 static enum lru_status
1789 xfs_buftarg_wait_rele(
1790 struct list_head
*item
,
1791 struct list_lru_one
*lru
,
1792 spinlock_t
*lru_lock
,
1796 struct xfs_buf
*bp
= container_of(item
, struct xfs_buf
, b_lru
);
1797 struct list_head
*dispose
= arg
;
1799 if (atomic_read(&bp
->b_hold
) > 1) {
1800 /* need to wait, so skip it this pass */
1801 trace_xfs_buf_wait_buftarg(bp
, _RET_IP_
);
1804 if (!spin_trylock(&bp
->b_lock
))
1808 * clear the LRU reference count so the buffer doesn't get
1809 * ignored in xfs_buf_rele().
1811 atomic_set(&bp
->b_lru_ref
, 0);
1812 bp
->b_state
|= XFS_BSTATE_DISPOSE
;
1813 list_lru_isolate_move(lru
, item
, dispose
);
1814 spin_unlock(&bp
->b_lock
);
1820 struct xfs_buftarg
*btp
)
1824 bool write_fail
= false;
1827 * First wait on the buftarg I/O count for all in-flight buffers to be
1828 * released. This is critical as new buffers do not make the LRU until
1829 * they are released.
1831 * Next, flush the buffer workqueue to ensure all completion processing
1832 * has finished. Just waiting on buffer locks is not sufficient for
1833 * async IO as the reference count held over IO is not released until
1834 * after the buffer lock is dropped. Hence we need to ensure here that
1835 * all reference counts have been dropped before we start walking the
1838 while (percpu_counter_sum(&btp
->bt_io_count
))
1840 flush_workqueue(btp
->bt_mount
->m_buf_workqueue
);
1842 /* loop until there is nothing left on the lru list. */
1843 while (list_lru_count(&btp
->bt_lru
)) {
1844 list_lru_walk(&btp
->bt_lru
, xfs_buftarg_wait_rele
,
1845 &dispose
, LONG_MAX
);
1847 while (!list_empty(&dispose
)) {
1849 bp
= list_first_entry(&dispose
, struct xfs_buf
, b_lru
);
1850 list_del_init(&bp
->b_lru
);
1851 if (bp
->b_flags
& XBF_WRITE_FAIL
) {
1853 xfs_buf_alert_ratelimited(bp
,
1854 "XFS: Corruption Alert",
1855 "Corruption Alert: Buffer at daddr 0x%llx had permanent write failures!",
1856 (long long)bp
->b_bn
);
1865 * If one or more failed buffers were freed, that means dirty metadata
1866 * was thrown away. This should only ever happen after I/O completion
1867 * handling has elevated I/O error(s) to permanent failures and shuts
1871 ASSERT(XFS_FORCED_SHUTDOWN(btp
->bt_mount
));
1872 xfs_alert(btp
->bt_mount
,
1873 "Please run xfs_repair to determine the extent of the problem.");
1877 static enum lru_status
1878 xfs_buftarg_isolate(
1879 struct list_head
*item
,
1880 struct list_lru_one
*lru
,
1881 spinlock_t
*lru_lock
,
1884 struct xfs_buf
*bp
= container_of(item
, struct xfs_buf
, b_lru
);
1885 struct list_head
*dispose
= arg
;
1888 * we are inverting the lru lock/bp->b_lock here, so use a trylock.
1889 * If we fail to get the lock, just skip it.
1891 if (!spin_trylock(&bp
->b_lock
))
1894 * Decrement the b_lru_ref count unless the value is already
1895 * zero. If the value is already zero, we need to reclaim the
1896 * buffer, otherwise it gets another trip through the LRU.
1898 if (atomic_add_unless(&bp
->b_lru_ref
, -1, 0)) {
1899 spin_unlock(&bp
->b_lock
);
1903 bp
->b_state
|= XFS_BSTATE_DISPOSE
;
1904 list_lru_isolate_move(lru
, item
, dispose
);
1905 spin_unlock(&bp
->b_lock
);
1909 static unsigned long
1910 xfs_buftarg_shrink_scan(
1911 struct shrinker
*shrink
,
1912 struct shrink_control
*sc
)
1914 struct xfs_buftarg
*btp
= container_of(shrink
,
1915 struct xfs_buftarg
, bt_shrinker
);
1917 unsigned long freed
;
1919 freed
= list_lru_shrink_walk(&btp
->bt_lru
, sc
,
1920 xfs_buftarg_isolate
, &dispose
);
1922 while (!list_empty(&dispose
)) {
1924 bp
= list_first_entry(&dispose
, struct xfs_buf
, b_lru
);
1925 list_del_init(&bp
->b_lru
);
1932 static unsigned long
1933 xfs_buftarg_shrink_count(
1934 struct shrinker
*shrink
,
1935 struct shrink_control
*sc
)
1937 struct xfs_buftarg
*btp
= container_of(shrink
,
1938 struct xfs_buftarg
, bt_shrinker
);
1939 return list_lru_shrink_count(&btp
->bt_lru
, sc
);
1944 struct xfs_buftarg
*btp
)
1946 unregister_shrinker(&btp
->bt_shrinker
);
1947 ASSERT(percpu_counter_sum(&btp
->bt_io_count
) == 0);
1948 percpu_counter_destroy(&btp
->bt_io_count
);
1949 list_lru_destroy(&btp
->bt_lru
);
1951 xfs_blkdev_issue_flush(btp
);
1957 xfs_setsize_buftarg(
1959 unsigned int sectorsize
)
1961 /* Set up metadata sector size info */
1962 btp
->bt_meta_sectorsize
= sectorsize
;
1963 btp
->bt_meta_sectormask
= sectorsize
- 1;
1965 if (set_blocksize(btp
->bt_bdev
, sectorsize
)) {
1966 xfs_warn(btp
->bt_mount
,
1967 "Cannot set_blocksize to %u on device %pg",
1968 sectorsize
, btp
->bt_bdev
);
1972 /* Set up device logical sector size mask */
1973 btp
->bt_logical_sectorsize
= bdev_logical_block_size(btp
->bt_bdev
);
1974 btp
->bt_logical_sectormask
= bdev_logical_block_size(btp
->bt_bdev
) - 1;
1980 * When allocating the initial buffer target we have not yet
1981 * read in the superblock, so don't know what sized sectors
1982 * are being used at this early stage. Play safe.
1985 xfs_setsize_buftarg_early(
1987 struct block_device
*bdev
)
1989 return xfs_setsize_buftarg(btp
, bdev_logical_block_size(bdev
));
1994 struct xfs_mount
*mp
,
1995 struct block_device
*bdev
,
1996 struct dax_device
*dax_dev
)
2000 btp
= kmem_zalloc(sizeof(*btp
), KM_NOFS
);
2003 btp
->bt_dev
= bdev
->bd_dev
;
2004 btp
->bt_bdev
= bdev
;
2005 btp
->bt_daxdev
= dax_dev
;
2008 * Buffer IO error rate limiting. Limit it to no more than 10 messages
2009 * per 30 seconds so as to not spam logs too much on repeated errors.
2011 ratelimit_state_init(&btp
->bt_ioerror_rl
, 30 * HZ
,
2012 DEFAULT_RATELIMIT_BURST
);
2014 if (xfs_setsize_buftarg_early(btp
, bdev
))
2017 if (list_lru_init(&btp
->bt_lru
))
2020 if (percpu_counter_init(&btp
->bt_io_count
, 0, GFP_KERNEL
))
2023 btp
->bt_shrinker
.count_objects
= xfs_buftarg_shrink_count
;
2024 btp
->bt_shrinker
.scan_objects
= xfs_buftarg_shrink_scan
;
2025 btp
->bt_shrinker
.seeks
= DEFAULT_SEEKS
;
2026 btp
->bt_shrinker
.flags
= SHRINKER_NUMA_AWARE
;
2027 if (register_shrinker(&btp
->bt_shrinker
))
2032 percpu_counter_destroy(&btp
->bt_io_count
);
2034 list_lru_destroy(&btp
->bt_lru
);
2041 * Cancel a delayed write list.
2043 * Remove each buffer from the list, clear the delwri queue flag and drop the
2044 * associated buffer reference.
2047 xfs_buf_delwri_cancel(
2048 struct list_head
*list
)
2052 while (!list_empty(list
)) {
2053 bp
= list_first_entry(list
, struct xfs_buf
, b_list
);
2056 bp
->b_flags
&= ~_XBF_DELWRI_Q
;
2057 list_del_init(&bp
->b_list
);
2063 * Add a buffer to the delayed write list.
2065 * This queues a buffer for writeout if it hasn't already been. Note that
2066 * neither this routine nor the buffer list submission functions perform
2067 * any internal synchronization. It is expected that the lists are thread-local
2070 * Returns true if we queued up the buffer, or false if it already had
2071 * been on the buffer list.
2074 xfs_buf_delwri_queue(
2076 struct list_head
*list
)
2078 ASSERT(xfs_buf_islocked(bp
));
2079 ASSERT(!(bp
->b_flags
& XBF_READ
));
2082 * If the buffer is already marked delwri it already is queued up
2083 * by someone else for imediate writeout. Just ignore it in that
2086 if (bp
->b_flags
& _XBF_DELWRI_Q
) {
2087 trace_xfs_buf_delwri_queued(bp
, _RET_IP_
);
2091 trace_xfs_buf_delwri_queue(bp
, _RET_IP_
);
2094 * If a buffer gets written out synchronously or marked stale while it
2095 * is on a delwri list we lazily remove it. To do this, the other party
2096 * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
2097 * It remains referenced and on the list. In a rare corner case it
2098 * might get readded to a delwri list after the synchronous writeout, in
2099 * which case we need just need to re-add the flag here.
2101 bp
->b_flags
|= _XBF_DELWRI_Q
;
2102 if (list_empty(&bp
->b_list
)) {
2103 atomic_inc(&bp
->b_hold
);
2104 list_add_tail(&bp
->b_list
, list
);
2111 * Compare function is more complex than it needs to be because
2112 * the return value is only 32 bits and we are doing comparisons
2118 struct list_head
*a
,
2119 struct list_head
*b
)
2121 struct xfs_buf
*ap
= container_of(a
, struct xfs_buf
, b_list
);
2122 struct xfs_buf
*bp
= container_of(b
, struct xfs_buf
, b_list
);
2125 diff
= ap
->b_maps
[0].bm_bn
- bp
->b_maps
[0].bm_bn
;
2134 * Submit buffers for write. If wait_list is specified, the buffers are
2135 * submitted using sync I/O and placed on the wait list such that the caller can
2136 * iowait each buffer. Otherwise async I/O is used and the buffers are released
2137 * at I/O completion time. In either case, buffers remain locked until I/O
2138 * completes and the buffer is released from the queue.
2141 xfs_buf_delwri_submit_buffers(
2142 struct list_head
*buffer_list
,
2143 struct list_head
*wait_list
)
2145 struct xfs_buf
*bp
, *n
;
2147 struct blk_plug plug
;
2149 list_sort(NULL
, buffer_list
, xfs_buf_cmp
);
2151 blk_start_plug(&plug
);
2152 list_for_each_entry_safe(bp
, n
, buffer_list
, b_list
) {
2154 if (xfs_buf_ispinned(bp
)) {
2158 if (!xfs_buf_trylock(bp
))
2165 * Someone else might have written the buffer synchronously or
2166 * marked it stale in the meantime. In that case only the
2167 * _XBF_DELWRI_Q flag got cleared, and we have to drop the
2168 * reference and remove it from the list here.
2170 if (!(bp
->b_flags
& _XBF_DELWRI_Q
)) {
2171 list_del_init(&bp
->b_list
);
2176 trace_xfs_buf_delwri_split(bp
, _RET_IP_
);
2179 * If we have a wait list, each buffer (and associated delwri
2180 * queue reference) transfers to it and is submitted
2181 * synchronously. Otherwise, drop the buffer from the delwri
2182 * queue and submit async.
2184 bp
->b_flags
&= ~_XBF_DELWRI_Q
;
2185 bp
->b_flags
|= XBF_WRITE
;
2187 bp
->b_flags
&= ~XBF_ASYNC
;
2188 list_move_tail(&bp
->b_list
, wait_list
);
2190 bp
->b_flags
|= XBF_ASYNC
;
2191 list_del_init(&bp
->b_list
);
2193 __xfs_buf_submit(bp
, false);
2195 blk_finish_plug(&plug
);
2201 * Write out a buffer list asynchronously.
2203 * This will take the @buffer_list, write all non-locked and non-pinned buffers
2204 * out and not wait for I/O completion on any of the buffers. This interface
2205 * is only safely useable for callers that can track I/O completion by higher
2206 * level means, e.g. AIL pushing as the @buffer_list is consumed in this
2209 * Note: this function will skip buffers it would block on, and in doing so
2210 * leaves them on @buffer_list so they can be retried on a later pass. As such,
2211 * it is up to the caller to ensure that the buffer list is fully submitted or
2212 * cancelled appropriately when they are finished with the list. Failure to
2213 * cancel or resubmit the list until it is empty will result in leaked buffers
2217 xfs_buf_delwri_submit_nowait(
2218 struct list_head
*buffer_list
)
2220 return xfs_buf_delwri_submit_buffers(buffer_list
, NULL
);
2224 * Write out a buffer list synchronously.
2226 * This will take the @buffer_list, write all buffers out and wait for I/O
2227 * completion on all of the buffers. @buffer_list is consumed by the function,
2228 * so callers must have some other way of tracking buffers if they require such
2232 xfs_buf_delwri_submit(
2233 struct list_head
*buffer_list
)
2235 LIST_HEAD (wait_list
);
2236 int error
= 0, error2
;
2239 xfs_buf_delwri_submit_buffers(buffer_list
, &wait_list
);
2241 /* Wait for IO to complete. */
2242 while (!list_empty(&wait_list
)) {
2243 bp
= list_first_entry(&wait_list
, struct xfs_buf
, b_list
);
2245 list_del_init(&bp
->b_list
);
2248 * Wait on the locked buffer, check for errors and unlock and
2249 * release the delwri queue reference.
2251 error2
= xfs_buf_iowait(bp
);
2261 * Push a single buffer on a delwri queue.
2263 * The purpose of this function is to submit a single buffer of a delwri queue
2264 * and return with the buffer still on the original queue. The waiting delwri
2265 * buffer submission infrastructure guarantees transfer of the delwri queue
2266 * buffer reference to a temporary wait list. We reuse this infrastructure to
2267 * transfer the buffer back to the original queue.
2269 * Note the buffer transitions from the queued state, to the submitted and wait
2270 * listed state and back to the queued state during this call. The buffer
2271 * locking and queue management logic between _delwri_pushbuf() and
2272 * _delwri_queue() guarantee that the buffer cannot be queued to another list
2276 xfs_buf_delwri_pushbuf(
2278 struct list_head
*buffer_list
)
2280 LIST_HEAD (submit_list
);
2283 ASSERT(bp
->b_flags
& _XBF_DELWRI_Q
);
2285 trace_xfs_buf_delwri_pushbuf(bp
, _RET_IP_
);
2288 * Isolate the buffer to a new local list so we can submit it for I/O
2289 * independently from the rest of the original list.
2292 list_move(&bp
->b_list
, &submit_list
);
2296 * Delwri submission clears the DELWRI_Q buffer flag and returns with
2297 * the buffer on the wait list with the original reference. Rather than
2298 * bounce the buffer from a local wait list back to the original list
2299 * after I/O completion, reuse the original list as the wait list.
2301 xfs_buf_delwri_submit_buffers(&submit_list
, buffer_list
);
2304 * The buffer is now locked, under I/O and wait listed on the original
2305 * delwri queue. Wait for I/O completion, restore the DELWRI_Q flag and
2306 * return with the buffer unlocked and on the original queue.
2308 error
= xfs_buf_iowait(bp
);
2309 bp
->b_flags
|= _XBF_DELWRI_Q
;
2318 xfs_buf_zone
= kmem_cache_create("xfs_buf", sizeof(struct xfs_buf
), 0,
2319 SLAB_HWCACHE_ALIGN
|
2320 SLAB_RECLAIM_ACCOUNT
|
2333 xfs_buf_terminate(void)
2335 kmem_cache_destroy(xfs_buf_zone
);
2338 void xfs_buf_set_ref(struct xfs_buf
*bp
, int lru_ref
)
2341 * Set the lru reference count to 0 based on the error injection tag.
2342 * This allows userspace to disrupt buffer caching for debug/testing
2345 if (XFS_TEST_ERROR(false, bp
->b_mount
, XFS_ERRTAG_BUF_LRU_REF
))
2348 atomic_set(&bp
->b_lru_ref
, lru_ref
);
2352 * Verify an on-disk magic value against the magic value specified in the
2353 * verifier structure. The verifier magic is in disk byte order so the caller is
2354 * expected to pass the value directly from disk.
2361 struct xfs_mount
*mp
= bp
->b_mount
;
2364 idx
= xfs_sb_version_hascrc(&mp
->m_sb
);
2365 if (WARN_ON(!bp
->b_ops
|| !bp
->b_ops
->magic
[idx
]))
2367 return dmagic
== bp
->b_ops
->magic
[idx
];
2370 * Verify an on-disk magic value against the magic value specified in the
2371 * verifier structure. The verifier magic is in disk byte order so the caller is
2372 * expected to pass the value directly from disk.
2379 struct xfs_mount
*mp
= bp
->b_mount
;
2382 idx
= xfs_sb_version_hascrc(&mp
->m_sb
);
2383 if (WARN_ON(!bp
->b_ops
|| !bp
->b_ops
->magic16
[idx
]))
2385 return dmagic
== bp
->b_ops
->magic16
[idx
];