1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
7 #include <linux/stddef.h>
8 #include <linux/errno.h>
10 #include <linux/pagemap.h>
11 #include <linux/init.h>
12 #include <linux/vmalloc.h>
13 #include <linux/bio.h>
14 #include <linux/sysctl.h>
15 #include <linux/proc_fs.h>
16 #include <linux/workqueue.h>
17 #include <linux/percpu.h>
18 #include <linux/blkdev.h>
19 #include <linux/hash.h>
20 #include <linux/kthread.h>
21 #include <linux/migrate.h>
22 #include <linux/backing-dev.h>
23 #include <linux/freezer.h>
25 #include "xfs_format.h"
26 #include "xfs_log_format.h"
27 #include "xfs_trans_resv.h"
29 #include "xfs_mount.h"
30 #include "xfs_trace.h"
32 #include "xfs_errortag.h"
33 #include "xfs_error.h"
35 static kmem_zone_t
*xfs_buf_zone
;
37 #define xb_to_gfp(flags) \
38 ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : GFP_NOFS) | __GFP_NOWARN)
46 * Return true if the buffer is vmapped.
48 * b_addr is null if the buffer is not mapped, but the code is clever
49 * enough to know it doesn't have to map a single page, so the check has
50 * to be both for b_addr and bp->b_page_count > 1.
52 return bp
->b_addr
&& bp
->b_page_count
> 1;
59 return (bp
->b_page_count
* PAGE_SIZE
) - bp
->b_offset
;
63 * Bump the I/O in flight count on the buftarg if we haven't yet done so for
64 * this buffer. The count is incremented once per buffer (per hold cycle)
65 * because the corresponding decrement is deferred to buffer release. Buffers
66 * can undergo I/O multiple times in a hold-release cycle and per buffer I/O
67 * tracking adds unnecessary overhead. This is used for sychronization purposes
68 * with unmount (see xfs_wait_buftarg()), so all we really need is a count of
71 * Buffers that are never released (e.g., superblock, iclog buffers) must set
72 * the XBF_NO_IOACCT flag before I/O submission. Otherwise, the buftarg count
73 * never reaches zero and unmount hangs indefinitely.
79 if (bp
->b_flags
& XBF_NO_IOACCT
)
82 ASSERT(bp
->b_flags
& XBF_ASYNC
);
83 spin_lock(&bp
->b_lock
);
84 if (!(bp
->b_state
& XFS_BSTATE_IN_FLIGHT
)) {
85 bp
->b_state
|= XFS_BSTATE_IN_FLIGHT
;
86 percpu_counter_inc(&bp
->b_target
->bt_io_count
);
88 spin_unlock(&bp
->b_lock
);
92 * Clear the in-flight state on a buffer about to be released to the LRU or
93 * freed and unaccount from the buftarg.
99 lockdep_assert_held(&bp
->b_lock
);
101 if (bp
->b_state
& XFS_BSTATE_IN_FLIGHT
) {
102 bp
->b_state
&= ~XFS_BSTATE_IN_FLIGHT
;
103 percpu_counter_dec(&bp
->b_target
->bt_io_count
);
111 spin_lock(&bp
->b_lock
);
112 __xfs_buf_ioacct_dec(bp
);
113 spin_unlock(&bp
->b_lock
);
117 * When we mark a buffer stale, we remove the buffer from the LRU and clear the
118 * b_lru_ref count so that the buffer is freed immediately when the buffer
119 * reference count falls to zero. If the buffer is already on the LRU, we need
120 * to remove the reference that LRU holds on the buffer.
122 * This prevents build-up of stale buffers on the LRU.
128 ASSERT(xfs_buf_islocked(bp
));
130 bp
->b_flags
|= XBF_STALE
;
133 * Clear the delwri status so that a delwri queue walker will not
134 * flush this buffer to disk now that it is stale. The delwri queue has
135 * a reference to the buffer, so this is safe to do.
137 bp
->b_flags
&= ~_XBF_DELWRI_Q
;
140 * Once the buffer is marked stale and unlocked, a subsequent lookup
141 * could reset b_flags. There is no guarantee that the buffer is
142 * unaccounted (released to LRU) before that occurs. Drop in-flight
143 * status now to preserve accounting consistency.
145 spin_lock(&bp
->b_lock
);
146 __xfs_buf_ioacct_dec(bp
);
148 atomic_set(&bp
->b_lru_ref
, 0);
149 if (!(bp
->b_state
& XFS_BSTATE_DISPOSE
) &&
150 (list_lru_del(&bp
->b_target
->bt_lru
, &bp
->b_lru
)))
151 atomic_dec(&bp
->b_hold
);
153 ASSERT(atomic_read(&bp
->b_hold
) >= 1);
154 spin_unlock(&bp
->b_lock
);
162 ASSERT(bp
->b_maps
== NULL
);
163 bp
->b_map_count
= map_count
;
165 if (map_count
== 1) {
166 bp
->b_maps
= &bp
->__b_map
;
170 bp
->b_maps
= kmem_zalloc(map_count
* sizeof(struct xfs_buf_map
),
178 * Frees b_pages if it was allocated.
184 if (bp
->b_maps
!= &bp
->__b_map
) {
185 kmem_free(bp
->b_maps
);
192 struct xfs_buftarg
*target
,
193 struct xfs_buf_map
*map
,
195 xfs_buf_flags_t flags
)
201 bp
= kmem_zone_zalloc(xfs_buf_zone
, KM_NOFS
);
206 * We don't want certain flags to appear in b_flags unless they are
207 * specifically set by later operations on the buffer.
209 flags
&= ~(XBF_UNMAPPED
| XBF_TRYLOCK
| XBF_ASYNC
| XBF_READ_AHEAD
);
211 atomic_set(&bp
->b_hold
, 1);
212 atomic_set(&bp
->b_lru_ref
, 1);
213 init_completion(&bp
->b_iowait
);
214 INIT_LIST_HEAD(&bp
->b_lru
);
215 INIT_LIST_HEAD(&bp
->b_list
);
216 INIT_LIST_HEAD(&bp
->b_li_list
);
217 sema_init(&bp
->b_sema
, 0); /* held, no waiters */
218 spin_lock_init(&bp
->b_lock
);
219 bp
->b_target
= target
;
223 * Set length and io_length to the same value initially.
224 * I/O routines should use io_length, which will be the same in
225 * most cases but may be reset (e.g. XFS recovery).
227 error
= xfs_buf_get_maps(bp
, nmaps
);
229 kmem_zone_free(xfs_buf_zone
, bp
);
233 bp
->b_bn
= map
[0].bm_bn
;
235 for (i
= 0; i
< nmaps
; i
++) {
236 bp
->b_maps
[i
].bm_bn
= map
[i
].bm_bn
;
237 bp
->b_maps
[i
].bm_len
= map
[i
].bm_len
;
238 bp
->b_length
+= map
[i
].bm_len
;
240 bp
->b_io_length
= bp
->b_length
;
242 atomic_set(&bp
->b_pin_count
, 0);
243 init_waitqueue_head(&bp
->b_waiters
);
245 XFS_STATS_INC(target
->bt_mount
, xb_create
);
246 trace_xfs_buf_init(bp
, _RET_IP_
);
252 * Allocate a page array capable of holding a specified number
253 * of pages, and point the page buf at it.
260 /* Make sure that we have a page list */
261 if (bp
->b_pages
== NULL
) {
262 bp
->b_page_count
= page_count
;
263 if (page_count
<= XB_PAGES
) {
264 bp
->b_pages
= bp
->b_page_array
;
266 bp
->b_pages
= kmem_alloc(sizeof(struct page
*) *
267 page_count
, KM_NOFS
);
268 if (bp
->b_pages
== NULL
)
271 memset(bp
->b_pages
, 0, sizeof(struct page
*) * page_count
);
277 * Frees b_pages if it was allocated.
283 if (bp
->b_pages
!= bp
->b_page_array
) {
284 kmem_free(bp
->b_pages
);
290 * Releases the specified buffer.
292 * The modification state of any associated pages is left unchanged.
293 * The buffer must not be on any hash - use xfs_buf_rele instead for
294 * hashed and refcounted buffers
300 trace_xfs_buf_free(bp
, _RET_IP_
);
302 ASSERT(list_empty(&bp
->b_lru
));
304 if (bp
->b_flags
& _XBF_PAGES
) {
307 if (xfs_buf_is_vmapped(bp
))
308 vm_unmap_ram(bp
->b_addr
- bp
->b_offset
,
311 for (i
= 0; i
< bp
->b_page_count
; i
++) {
312 struct page
*page
= bp
->b_pages
[i
];
316 } else if (bp
->b_flags
& _XBF_KMEM
)
317 kmem_free(bp
->b_addr
);
318 _xfs_buf_free_pages(bp
);
319 xfs_buf_free_maps(bp
);
320 kmem_zone_free(xfs_buf_zone
, bp
);
324 * Allocates all the pages for buffer in question and builds it's page list.
327 xfs_buf_allocate_memory(
332 size_t nbytes
, offset
;
333 gfp_t gfp_mask
= xb_to_gfp(flags
);
334 unsigned short page_count
, i
;
335 xfs_off_t start
, end
;
339 * for buffers that are contained within a single page, just allocate
340 * the memory from the heap - there's no need for the complexity of
341 * page arrays to keep allocation down to order 0.
343 size
= BBTOB(bp
->b_length
);
344 if (size
< PAGE_SIZE
) {
345 bp
->b_addr
= kmem_alloc(size
, KM_NOFS
);
347 /* low memory - use alloc_page loop instead */
351 if (((unsigned long)(bp
->b_addr
+ size
- 1) & PAGE_MASK
) !=
352 ((unsigned long)bp
->b_addr
& PAGE_MASK
)) {
353 /* b_addr spans two pages - use alloc_page instead */
354 kmem_free(bp
->b_addr
);
358 bp
->b_offset
= offset_in_page(bp
->b_addr
);
359 bp
->b_pages
= bp
->b_page_array
;
360 bp
->b_pages
[0] = virt_to_page(bp
->b_addr
);
361 bp
->b_page_count
= 1;
362 bp
->b_flags
|= _XBF_KMEM
;
367 start
= BBTOB(bp
->b_maps
[0].bm_bn
) >> PAGE_SHIFT
;
368 end
= (BBTOB(bp
->b_maps
[0].bm_bn
+ bp
->b_length
) + PAGE_SIZE
- 1)
370 page_count
= end
- start
;
371 error
= _xfs_buf_get_pages(bp
, page_count
);
375 offset
= bp
->b_offset
;
376 bp
->b_flags
|= _XBF_PAGES
;
378 for (i
= 0; i
< bp
->b_page_count
; i
++) {
382 page
= alloc_page(gfp_mask
);
383 if (unlikely(page
== NULL
)) {
384 if (flags
& XBF_READ_AHEAD
) {
385 bp
->b_page_count
= i
;
391 * This could deadlock.
393 * But until all the XFS lowlevel code is revamped to
394 * handle buffer allocation failures we can't do much.
396 if (!(++retries
% 100))
398 "%s(%u) possible memory allocation deadlock in %s (mode:0x%x)",
399 current
->comm
, current
->pid
,
402 XFS_STATS_INC(bp
->b_target
->bt_mount
, xb_page_retries
);
403 congestion_wait(BLK_RW_ASYNC
, HZ
/50);
407 XFS_STATS_INC(bp
->b_target
->bt_mount
, xb_page_found
);
409 nbytes
= min_t(size_t, size
, PAGE_SIZE
- offset
);
411 bp
->b_pages
[i
] = page
;
417 for (i
= 0; i
< bp
->b_page_count
; i
++)
418 __free_page(bp
->b_pages
[i
]);
419 bp
->b_flags
&= ~_XBF_PAGES
;
424 * Map buffer into kernel address-space if necessary.
431 ASSERT(bp
->b_flags
& _XBF_PAGES
);
432 if (bp
->b_page_count
== 1) {
433 /* A single page buffer is always mappable */
434 bp
->b_addr
= page_address(bp
->b_pages
[0]) + bp
->b_offset
;
435 } else if (flags
& XBF_UNMAPPED
) {
442 * vm_map_ram() will allocate auxillary structures (e.g.
443 * pagetables) with GFP_KERNEL, yet we are likely to be under
444 * GFP_NOFS context here. Hence we need to tell memory reclaim
445 * that we are in such a context via PF_MEMALLOC_NOFS to prevent
446 * memory reclaim re-entering the filesystem here and
447 * potentially deadlocking.
449 nofs_flag
= memalloc_nofs_save();
451 bp
->b_addr
= vm_map_ram(bp
->b_pages
, bp
->b_page_count
,
456 } while (retried
++ <= 1);
457 memalloc_nofs_restore(nofs_flag
);
461 bp
->b_addr
+= bp
->b_offset
;
468 * Finding and Reading Buffers
472 struct rhashtable_compare_arg
*arg
,
475 const struct xfs_buf_map
*map
= arg
->key
;
476 const struct xfs_buf
*bp
= obj
;
479 * The key hashing in the lookup path depends on the key being the
480 * first element of the compare_arg, make sure to assert this.
482 BUILD_BUG_ON(offsetof(struct xfs_buf_map
, bm_bn
) != 0);
484 if (bp
->b_bn
!= map
->bm_bn
)
487 if (unlikely(bp
->b_length
!= map
->bm_len
)) {
489 * found a block number match. If the range doesn't
490 * match, the only way this is allowed is if the buffer
491 * in the cache is stale and the transaction that made
492 * it stale has not yet committed. i.e. we are
493 * reallocating a busy extent. Skip this buffer and
494 * continue searching for an exact match.
496 ASSERT(bp
->b_flags
& XBF_STALE
);
502 static const struct rhashtable_params xfs_buf_hash_params
= {
503 .min_size
= 32, /* empty AGs have minimal footprint */
505 .key_len
= sizeof(xfs_daddr_t
),
506 .key_offset
= offsetof(struct xfs_buf
, b_bn
),
507 .head_offset
= offsetof(struct xfs_buf
, b_rhash_head
),
508 .automatic_shrinking
= true,
509 .obj_cmpfn
= _xfs_buf_obj_cmp
,
514 struct xfs_perag
*pag
)
516 spin_lock_init(&pag
->pag_buf_lock
);
517 return rhashtable_init(&pag
->pag_buf_hash
, &xfs_buf_hash_params
);
521 xfs_buf_hash_destroy(
522 struct xfs_perag
*pag
)
524 rhashtable_destroy(&pag
->pag_buf_hash
);
528 * Look up a buffer in the buffer cache and return it referenced and locked
531 * If @new_bp is supplied and we have a lookup miss, insert @new_bp into the
534 * If XBF_TRYLOCK is set in @flags, only try to lock the buffer and return
535 * -EAGAIN if we fail to lock it.
538 * -EFSCORRUPTED if have been supplied with an invalid address
539 * -EAGAIN on trylock failure
540 * -ENOENT if we fail to find a match and @new_bp was NULL
542 * - @new_bp if we inserted it into the cache
543 * - the buffer we found and locked.
547 struct xfs_buftarg
*btp
,
548 struct xfs_buf_map
*map
,
550 xfs_buf_flags_t flags
,
551 struct xfs_buf
*new_bp
,
552 struct xfs_buf
**found_bp
)
554 struct xfs_perag
*pag
;
556 struct xfs_buf_map cmap
= { .bm_bn
= map
[0].bm_bn
};
562 for (i
= 0; i
< nmaps
; i
++)
563 cmap
.bm_len
+= map
[i
].bm_len
;
565 /* Check for IOs smaller than the sector size / not sector aligned */
566 ASSERT(!(BBTOB(cmap
.bm_len
) < btp
->bt_meta_sectorsize
));
567 ASSERT(!(BBTOB(cmap
.bm_bn
) & (xfs_off_t
)btp
->bt_meta_sectormask
));
570 * Corrupted block numbers can get through to here, unfortunately, so we
571 * have to check that the buffer falls within the filesystem bounds.
573 eofs
= XFS_FSB_TO_BB(btp
->bt_mount
, btp
->bt_mount
->m_sb
.sb_dblocks
);
574 if (cmap
.bm_bn
< 0 || cmap
.bm_bn
>= eofs
) {
575 xfs_alert(btp
->bt_mount
,
576 "%s: daddr 0x%llx out of range, EOFS 0x%llx",
577 __func__
, cmap
.bm_bn
, eofs
);
579 return -EFSCORRUPTED
;
582 pag
= xfs_perag_get(btp
->bt_mount
,
583 xfs_daddr_to_agno(btp
->bt_mount
, cmap
.bm_bn
));
585 spin_lock(&pag
->pag_buf_lock
);
586 bp
= rhashtable_lookup_fast(&pag
->pag_buf_hash
, &cmap
,
587 xfs_buf_hash_params
);
589 atomic_inc(&bp
->b_hold
);
595 XFS_STATS_INC(btp
->bt_mount
, xb_miss_locked
);
596 spin_unlock(&pag
->pag_buf_lock
);
601 /* the buffer keeps the perag reference until it is freed */
603 rhashtable_insert_fast(&pag
->pag_buf_hash
, &new_bp
->b_rhash_head
,
604 xfs_buf_hash_params
);
605 spin_unlock(&pag
->pag_buf_lock
);
610 spin_unlock(&pag
->pag_buf_lock
);
613 if (!xfs_buf_trylock(bp
)) {
614 if (flags
& XBF_TRYLOCK
) {
616 XFS_STATS_INC(btp
->bt_mount
, xb_busy_locked
);
620 XFS_STATS_INC(btp
->bt_mount
, xb_get_locked_waited
);
624 * if the buffer is stale, clear all the external state associated with
625 * it. We need to keep flags such as how we allocated the buffer memory
628 if (bp
->b_flags
& XBF_STALE
) {
629 ASSERT((bp
->b_flags
& _XBF_DELWRI_Q
) == 0);
630 ASSERT(bp
->b_iodone
== NULL
);
631 bp
->b_flags
&= _XBF_KMEM
| _XBF_PAGES
;
635 trace_xfs_buf_find(bp
, flags
, _RET_IP_
);
636 XFS_STATS_INC(btp
->bt_mount
, xb_get_locked
);
643 struct xfs_buftarg
*target
,
646 xfs_buf_flags_t flags
)
650 DEFINE_SINGLE_BUF_MAP(map
, blkno
, numblks
);
652 error
= xfs_buf_find(target
, &map
, 1, flags
, NULL
, &bp
);
659 * Assembles a buffer covering the specified range. The code is optimised for
660 * cache hits, as metadata intensive workloads will see 3 orders of magnitude
661 * more hits than misses.
665 struct xfs_buftarg
*target
,
666 struct xfs_buf_map
*map
,
668 xfs_buf_flags_t flags
)
671 struct xfs_buf
*new_bp
;
674 error
= xfs_buf_find(target
, map
, nmaps
, flags
, NULL
, &bp
);
681 /* cache hit, trylock failure, caller handles failure */
682 ASSERT(flags
& XBF_TRYLOCK
);
685 /* cache miss, go for insert */
690 * None of the higher layers understand failure types
691 * yet, so return NULL to signal a fatal lookup error.
696 new_bp
= _xfs_buf_alloc(target
, map
, nmaps
, flags
);
697 if (unlikely(!new_bp
))
700 error
= xfs_buf_allocate_memory(new_bp
, flags
);
702 xfs_buf_free(new_bp
);
706 error
= xfs_buf_find(target
, map
, nmaps
, flags
, new_bp
, &bp
);
708 xfs_buf_free(new_bp
);
713 xfs_buf_free(new_bp
);
717 error
= _xfs_buf_map_pages(bp
, flags
);
718 if (unlikely(error
)) {
719 xfs_warn(target
->bt_mount
,
720 "%s: failed to map pagesn", __func__
);
727 * Clear b_error if this is a lookup from a caller that doesn't expect
728 * valid data to be found in the buffer.
730 if (!(flags
& XBF_READ
))
731 xfs_buf_ioerror(bp
, 0);
733 XFS_STATS_INC(target
->bt_mount
, xb_get
);
734 trace_xfs_buf_get(bp
, flags
, _RET_IP_
);
741 xfs_buf_flags_t flags
)
743 ASSERT(!(flags
& XBF_WRITE
));
744 ASSERT(bp
->b_maps
[0].bm_bn
!= XFS_BUF_DADDR_NULL
);
746 bp
->b_flags
&= ~(XBF_WRITE
| XBF_ASYNC
| XBF_READ_AHEAD
);
747 bp
->b_flags
|= flags
& (XBF_READ
| XBF_ASYNC
| XBF_READ_AHEAD
);
749 return xfs_buf_submit(bp
);
754 struct xfs_buftarg
*target
,
755 struct xfs_buf_map
*map
,
757 xfs_buf_flags_t flags
,
758 const struct xfs_buf_ops
*ops
)
764 bp
= xfs_buf_get_map(target
, map
, nmaps
, flags
);
766 trace_xfs_buf_read(bp
, flags
, _RET_IP_
);
768 if (!(bp
->b_flags
& XBF_DONE
)) {
769 XFS_STATS_INC(target
->bt_mount
, xb_get_read
);
771 _xfs_buf_read(bp
, flags
);
772 } else if (flags
& XBF_ASYNC
) {
774 * Read ahead call which is already satisfied,
780 /* We do not want read in the flags */
781 bp
->b_flags
&= ~XBF_READ
;
789 * If we are not low on memory then do the readahead in a deadlock
793 xfs_buf_readahead_map(
794 struct xfs_buftarg
*target
,
795 struct xfs_buf_map
*map
,
797 const struct xfs_buf_ops
*ops
)
799 if (bdi_read_congested(target
->bt_bdev
->bd_bdi
))
802 xfs_buf_read_map(target
, map
, nmaps
,
803 XBF_TRYLOCK
|XBF_ASYNC
|XBF_READ_AHEAD
, ops
);
807 * Read an uncached buffer from disk. Allocates and returns a locked
808 * buffer containing the disk contents or nothing.
811 xfs_buf_read_uncached(
812 struct xfs_buftarg
*target
,
816 struct xfs_buf
**bpp
,
817 const struct xfs_buf_ops
*ops
)
823 bp
= xfs_buf_get_uncached(target
, numblks
, flags
);
827 /* set up the buffer for a read IO */
828 ASSERT(bp
->b_map_count
== 1);
829 bp
->b_bn
= XFS_BUF_DADDR_NULL
; /* always null for uncached buffers */
830 bp
->b_maps
[0].bm_bn
= daddr
;
831 bp
->b_flags
|= XBF_READ
;
836 int error
= bp
->b_error
;
846 * Return a buffer allocated as an empty buffer and associated to external
847 * memory via xfs_buf_associate_memory() back to it's empty state.
855 _xfs_buf_free_pages(bp
);
858 bp
->b_page_count
= 0;
860 bp
->b_length
= numblks
;
861 bp
->b_io_length
= numblks
;
863 ASSERT(bp
->b_map_count
== 1);
864 bp
->b_bn
= XFS_BUF_DADDR_NULL
;
865 bp
->b_maps
[0].bm_bn
= XFS_BUF_DADDR_NULL
;
866 bp
->b_maps
[0].bm_len
= bp
->b_length
;
869 static inline struct page
*
873 if ((!is_vmalloc_addr(addr
))) {
874 return virt_to_page(addr
);
876 return vmalloc_to_page(addr
);
881 xfs_buf_associate_memory(
888 unsigned long pageaddr
;
889 unsigned long offset
;
893 pageaddr
= (unsigned long)mem
& PAGE_MASK
;
894 offset
= (unsigned long)mem
- pageaddr
;
895 buflen
= PAGE_ALIGN(len
+ offset
);
896 page_count
= buflen
>> PAGE_SHIFT
;
898 /* Free any previous set of page pointers */
900 _xfs_buf_free_pages(bp
);
905 rval
= _xfs_buf_get_pages(bp
, page_count
);
909 bp
->b_offset
= offset
;
911 for (i
= 0; i
< bp
->b_page_count
; i
++) {
912 bp
->b_pages
[i
] = mem_to_page((void *)pageaddr
);
913 pageaddr
+= PAGE_SIZE
;
916 bp
->b_io_length
= BTOBB(len
);
917 bp
->b_length
= BTOBB(buflen
);
923 xfs_buf_get_uncached(
924 struct xfs_buftarg
*target
,
928 unsigned long page_count
;
931 DEFINE_SINGLE_BUF_MAP(map
, XFS_BUF_DADDR_NULL
, numblks
);
933 /* flags might contain irrelevant bits, pass only what we care about */
934 bp
= _xfs_buf_alloc(target
, &map
, 1, flags
& XBF_NO_IOACCT
);
935 if (unlikely(bp
== NULL
))
938 page_count
= PAGE_ALIGN(numblks
<< BBSHIFT
) >> PAGE_SHIFT
;
939 error
= _xfs_buf_get_pages(bp
, page_count
);
943 for (i
= 0; i
< page_count
; i
++) {
944 bp
->b_pages
[i
] = alloc_page(xb_to_gfp(flags
));
948 bp
->b_flags
|= _XBF_PAGES
;
950 error
= _xfs_buf_map_pages(bp
, 0);
951 if (unlikely(error
)) {
952 xfs_warn(target
->bt_mount
,
953 "%s: failed to map pages", __func__
);
957 trace_xfs_buf_get_uncached(bp
, _RET_IP_
);
962 __free_page(bp
->b_pages
[i
]);
963 _xfs_buf_free_pages(bp
);
965 xfs_buf_free_maps(bp
);
966 kmem_zone_free(xfs_buf_zone
, bp
);
972 * Increment reference count on buffer, to hold the buffer concurrently
973 * with another thread which may release (free) the buffer asynchronously.
974 * Must hold the buffer already to call this function.
980 trace_xfs_buf_hold(bp
, _RET_IP_
);
981 atomic_inc(&bp
->b_hold
);
985 * Release a hold on the specified buffer. If the hold count is 1, the buffer is
986 * placed on LRU or freed (depending on b_lru_ref).
992 struct xfs_perag
*pag
= bp
->b_pag
;
994 bool freebuf
= false;
996 trace_xfs_buf_rele(bp
, _RET_IP_
);
999 ASSERT(list_empty(&bp
->b_lru
));
1000 if (atomic_dec_and_test(&bp
->b_hold
)) {
1001 xfs_buf_ioacct_dec(bp
);
1007 ASSERT(atomic_read(&bp
->b_hold
) > 0);
1009 release
= atomic_dec_and_lock(&bp
->b_hold
, &pag
->pag_buf_lock
);
1010 spin_lock(&bp
->b_lock
);
1013 * Drop the in-flight state if the buffer is already on the LRU
1014 * and it holds the only reference. This is racy because we
1015 * haven't acquired the pag lock, but the use of _XBF_IN_FLIGHT
1016 * ensures the decrement occurs only once per-buf.
1018 if ((atomic_read(&bp
->b_hold
) == 1) && !list_empty(&bp
->b_lru
))
1019 __xfs_buf_ioacct_dec(bp
);
1023 /* the last reference has been dropped ... */
1024 __xfs_buf_ioacct_dec(bp
);
1025 if (!(bp
->b_flags
& XBF_STALE
) && atomic_read(&bp
->b_lru_ref
)) {
1027 * If the buffer is added to the LRU take a new reference to the
1028 * buffer for the LRU and clear the (now stale) dispose list
1031 if (list_lru_add(&bp
->b_target
->bt_lru
, &bp
->b_lru
)) {
1032 bp
->b_state
&= ~XFS_BSTATE_DISPOSE
;
1033 atomic_inc(&bp
->b_hold
);
1035 spin_unlock(&pag
->pag_buf_lock
);
1038 * most of the time buffers will already be removed from the
1039 * LRU, so optimise that case by checking for the
1040 * XFS_BSTATE_DISPOSE flag indicating the last list the buffer
1041 * was on was the disposal list
1043 if (!(bp
->b_state
& XFS_BSTATE_DISPOSE
)) {
1044 list_lru_del(&bp
->b_target
->bt_lru
, &bp
->b_lru
);
1046 ASSERT(list_empty(&bp
->b_lru
));
1049 ASSERT(!(bp
->b_flags
& _XBF_DELWRI_Q
));
1050 rhashtable_remove_fast(&pag
->pag_buf_hash
, &bp
->b_rhash_head
,
1051 xfs_buf_hash_params
);
1052 spin_unlock(&pag
->pag_buf_lock
);
1058 spin_unlock(&bp
->b_lock
);
1066 * Lock a buffer object, if it is not already locked.
1068 * If we come across a stale, pinned, locked buffer, we know that we are
1069 * being asked to lock a buffer that has been reallocated. Because it is
1070 * pinned, we know that the log has not been pushed to disk and hence it
1071 * will still be locked. Rather than continuing to have trylock attempts
1072 * fail until someone else pushes the log, push it ourselves before
1073 * returning. This means that the xfsaild will not get stuck trying
1074 * to push on stale inode buffers.
1082 locked
= down_trylock(&bp
->b_sema
) == 0;
1084 trace_xfs_buf_trylock(bp
, _RET_IP_
);
1086 trace_xfs_buf_trylock_fail(bp
, _RET_IP_
);
1091 * Lock a buffer object.
1093 * If we come across a stale, pinned, locked buffer, we know that we
1094 * are being asked to lock a buffer that has been reallocated. Because
1095 * it is pinned, we know that the log has not been pushed to disk and
1096 * hence it will still be locked. Rather than sleeping until someone
1097 * else pushes the log, push it ourselves before trying to get the lock.
1103 trace_xfs_buf_lock(bp
, _RET_IP_
);
1105 if (atomic_read(&bp
->b_pin_count
) && (bp
->b_flags
& XBF_STALE
))
1106 xfs_log_force(bp
->b_target
->bt_mount
, 0);
1109 trace_xfs_buf_lock_done(bp
, _RET_IP_
);
1116 ASSERT(xfs_buf_islocked(bp
));
1119 trace_xfs_buf_unlock(bp
, _RET_IP_
);
1126 DECLARE_WAITQUEUE (wait
, current
);
1128 if (atomic_read(&bp
->b_pin_count
) == 0)
1131 add_wait_queue(&bp
->b_waiters
, &wait
);
1133 set_current_state(TASK_UNINTERRUPTIBLE
);
1134 if (atomic_read(&bp
->b_pin_count
) == 0)
1138 remove_wait_queue(&bp
->b_waiters
, &wait
);
1139 set_current_state(TASK_RUNNING
);
1143 * Buffer Utility Routines
1150 bool read
= bp
->b_flags
& XBF_READ
;
1152 trace_xfs_buf_iodone(bp
, _RET_IP_
);
1154 bp
->b_flags
&= ~(XBF_READ
| XBF_WRITE
| XBF_READ_AHEAD
);
1157 * Pull in IO completion errors now. We are guaranteed to be running
1158 * single threaded, so we don't need the lock to read b_io_error.
1160 if (!bp
->b_error
&& bp
->b_io_error
)
1161 xfs_buf_ioerror(bp
, bp
->b_io_error
);
1163 /* Only validate buffers that were read without errors */
1164 if (read
&& !bp
->b_error
&& bp
->b_ops
) {
1165 ASSERT(!bp
->b_iodone
);
1166 bp
->b_ops
->verify_read(bp
);
1170 bp
->b_flags
|= XBF_DONE
;
1173 (*(bp
->b_iodone
))(bp
);
1174 else if (bp
->b_flags
& XBF_ASYNC
)
1177 complete(&bp
->b_iowait
);
1182 struct work_struct
*work
)
1184 struct xfs_buf
*bp
=
1185 container_of(work
, xfs_buf_t
, b_ioend_work
);
1191 xfs_buf_ioend_async(
1194 INIT_WORK(&bp
->b_ioend_work
, xfs_buf_ioend_work
);
1195 queue_work(bp
->b_ioend_wq
, &bp
->b_ioend_work
);
1202 xfs_failaddr_t failaddr
)
1204 ASSERT(error
<= 0 && error
>= -1000);
1205 bp
->b_error
= error
;
1206 trace_xfs_buf_ioerror(bp
, error
, failaddr
);
1210 xfs_buf_ioerror_alert(
1214 xfs_alert(bp
->b_target
->bt_mount
,
1215 "metadata I/O error in \"%s\" at daddr 0x%llx len %d error %d",
1216 func
, (uint64_t)XFS_BUF_ADDR(bp
), bp
->b_length
,
1226 ASSERT(xfs_buf_islocked(bp
));
1228 bp
->b_flags
|= XBF_WRITE
;
1229 bp
->b_flags
&= ~(XBF_ASYNC
| XBF_READ
| _XBF_DELWRI_Q
|
1230 XBF_WRITE_FAIL
| XBF_DONE
);
1232 error
= xfs_buf_submit(bp
);
1234 xfs_force_shutdown(bp
->b_target
->bt_mount
,
1235 SHUTDOWN_META_IO_ERROR
);
1244 struct xfs_buf
*bp
= (struct xfs_buf
*)bio
->bi_private
;
1247 * don't overwrite existing errors - otherwise we can lose errors on
1248 * buffers that require multiple bios to complete.
1250 if (bio
->bi_status
) {
1251 int error
= blk_status_to_errno(bio
->bi_status
);
1253 cmpxchg(&bp
->b_io_error
, 0, error
);
1256 if (!bp
->b_error
&& xfs_buf_is_vmapped(bp
) && (bp
->b_flags
& XBF_READ
))
1257 invalidate_kernel_vmap_range(bp
->b_addr
, xfs_buf_vmap_len(bp
));
1259 if (atomic_dec_and_test(&bp
->b_io_remaining
) == 1)
1260 xfs_buf_ioend_async(bp
);
1265 xfs_buf_ioapply_map(
1274 int total_nr_pages
= bp
->b_page_count
;
1277 sector_t sector
= bp
->b_maps
[map
].bm_bn
;
1281 /* skip the pages in the buffer before the start offset */
1283 offset
= *buf_offset
;
1284 while (offset
>= PAGE_SIZE
) {
1286 offset
-= PAGE_SIZE
;
1290 * Limit the IO size to the length of the current vector, and update the
1291 * remaining IO count for the next time around.
1293 size
= min_t(int, BBTOB(bp
->b_maps
[map
].bm_len
), *count
);
1295 *buf_offset
+= size
;
1298 atomic_inc(&bp
->b_io_remaining
);
1299 nr_pages
= min(total_nr_pages
, BIO_MAX_PAGES
);
1301 bio
= bio_alloc(GFP_NOIO
, nr_pages
);
1302 bio_set_dev(bio
, bp
->b_target
->bt_bdev
);
1303 bio
->bi_iter
.bi_sector
= sector
;
1304 bio
->bi_end_io
= xfs_buf_bio_end_io
;
1305 bio
->bi_private
= bp
;
1306 bio_set_op_attrs(bio
, op
, op_flags
);
1308 for (; size
&& nr_pages
; nr_pages
--, page_index
++) {
1309 int rbytes
, nbytes
= PAGE_SIZE
- offset
;
1314 rbytes
= bio_add_page(bio
, bp
->b_pages
[page_index
], nbytes
,
1316 if (rbytes
< nbytes
)
1320 sector
+= BTOBB(nbytes
);
1325 if (likely(bio
->bi_iter
.bi_size
)) {
1326 if (xfs_buf_is_vmapped(bp
)) {
1327 flush_kernel_vmap_range(bp
->b_addr
,
1328 xfs_buf_vmap_len(bp
));
1335 * This is guaranteed not to be the last io reference count
1336 * because the caller (xfs_buf_submit) holds a count itself.
1338 atomic_dec(&bp
->b_io_remaining
);
1339 xfs_buf_ioerror(bp
, -EIO
);
1349 struct blk_plug plug
;
1357 * Make sure we capture only current IO errors rather than stale errors
1358 * left over from previous use of the buffer (e.g. failed readahead).
1363 * Initialize the I/O completion workqueue if we haven't yet or the
1364 * submitter has not opted to specify a custom one.
1366 if (!bp
->b_ioend_wq
)
1367 bp
->b_ioend_wq
= bp
->b_target
->bt_mount
->m_buf_workqueue
;
1369 if (bp
->b_flags
& XBF_WRITE
) {
1371 if (bp
->b_flags
& XBF_SYNCIO
)
1372 op_flags
= REQ_SYNC
;
1373 if (bp
->b_flags
& XBF_FUA
)
1374 op_flags
|= REQ_FUA
;
1375 if (bp
->b_flags
& XBF_FLUSH
)
1376 op_flags
|= REQ_PREFLUSH
;
1379 * Run the write verifier callback function if it exists. If
1380 * this function fails it will mark the buffer with an error and
1381 * the IO should not be dispatched.
1384 bp
->b_ops
->verify_write(bp
);
1386 xfs_force_shutdown(bp
->b_target
->bt_mount
,
1387 SHUTDOWN_CORRUPT_INCORE
);
1390 } else if (bp
->b_bn
!= XFS_BUF_DADDR_NULL
) {
1391 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
1394 * non-crc filesystems don't attach verifiers during
1395 * log recovery, so don't warn for such filesystems.
1397 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
1399 "%s: no buf ops on daddr 0x%llx len %d",
1400 __func__
, bp
->b_bn
, bp
->b_length
);
1401 xfs_hex_dump(bp
->b_addr
,
1402 XFS_CORRUPTION_DUMP_LEN
);
1406 } else if (bp
->b_flags
& XBF_READ_AHEAD
) {
1408 op_flags
= REQ_RAHEAD
;
1413 /* we only use the buffer cache for meta-data */
1414 op_flags
|= REQ_META
;
1417 * Walk all the vectors issuing IO on them. Set up the initial offset
1418 * into the buffer and the desired IO size before we start -
1419 * _xfs_buf_ioapply_vec() will modify them appropriately for each
1422 offset
= bp
->b_offset
;
1423 size
= BBTOB(bp
->b_io_length
);
1424 blk_start_plug(&plug
);
1425 for (i
= 0; i
< bp
->b_map_count
; i
++) {
1426 xfs_buf_ioapply_map(bp
, i
, &offset
, &size
, op
, op_flags
);
1430 break; /* all done */
1432 blk_finish_plug(&plug
);
1436 * Wait for I/O completion of a sync buffer and return the I/O error code.
1442 ASSERT(!(bp
->b_flags
& XBF_ASYNC
));
1444 trace_xfs_buf_iowait(bp
, _RET_IP_
);
1445 wait_for_completion(&bp
->b_iowait
);
1446 trace_xfs_buf_iowait_done(bp
, _RET_IP_
);
1452 * Buffer I/O submission path, read or write. Asynchronous submission transfers
1453 * the buffer lock ownership and the current reference to the IO. It is not
1454 * safe to reference the buffer after a call to this function unless the caller
1455 * holds an additional reference itself.
1464 trace_xfs_buf_submit(bp
, _RET_IP_
);
1466 ASSERT(!(bp
->b_flags
& _XBF_DELWRI_Q
));
1468 /* on shutdown we stale and complete the buffer immediately */
1469 if (XFS_FORCED_SHUTDOWN(bp
->b_target
->bt_mount
)) {
1470 xfs_buf_ioerror(bp
, -EIO
);
1471 bp
->b_flags
&= ~XBF_DONE
;
1473 if (bp
->b_flags
& XBF_ASYNC
)
1479 * Grab a reference so the buffer does not go away underneath us. For
1480 * async buffers, I/O completion drops the callers reference, which
1481 * could occur before submission returns.
1485 if (bp
->b_flags
& XBF_WRITE
)
1486 xfs_buf_wait_unpin(bp
);
1488 /* clear the internal error state to avoid spurious errors */
1492 * Set the count to 1 initially, this will stop an I/O completion
1493 * callout which happens before we have started all the I/O from calling
1494 * xfs_buf_ioend too early.
1496 atomic_set(&bp
->b_io_remaining
, 1);
1497 if (bp
->b_flags
& XBF_ASYNC
)
1498 xfs_buf_ioacct_inc(bp
);
1499 _xfs_buf_ioapply(bp
);
1502 * If _xfs_buf_ioapply failed, we can get back here with only the IO
1503 * reference we took above. If we drop it to zero, run completion so
1504 * that we don't return to the caller with completion still pending.
1506 if (atomic_dec_and_test(&bp
->b_io_remaining
) == 1) {
1507 if (bp
->b_error
|| !(bp
->b_flags
& XBF_ASYNC
))
1510 xfs_buf_ioend_async(bp
);
1514 error
= xfs_buf_iowait(bp
);
1517 * Release the hold that keeps the buffer referenced for the entire
1518 * I/O. Note that if the buffer is async, it is not safe to reference
1519 * after this release.
1533 return bp
->b_addr
+ offset
;
1535 offset
+= bp
->b_offset
;
1536 page
= bp
->b_pages
[offset
>> PAGE_SHIFT
];
1537 return page_address(page
) + (offset
& (PAGE_SIZE
-1));
1541 * Move data into or out of a buffer.
1545 xfs_buf_t
*bp
, /* buffer to process */
1546 size_t boff
, /* starting buffer offset */
1547 size_t bsize
, /* length to copy */
1548 void *data
, /* data address */
1549 xfs_buf_rw_t mode
) /* read/write/zero flag */
1553 bend
= boff
+ bsize
;
1554 while (boff
< bend
) {
1556 int page_index
, page_offset
, csize
;
1558 page_index
= (boff
+ bp
->b_offset
) >> PAGE_SHIFT
;
1559 page_offset
= (boff
+ bp
->b_offset
) & ~PAGE_MASK
;
1560 page
= bp
->b_pages
[page_index
];
1561 csize
= min_t(size_t, PAGE_SIZE
- page_offset
,
1562 BBTOB(bp
->b_io_length
) - boff
);
1564 ASSERT((csize
+ page_offset
) <= PAGE_SIZE
);
1568 memset(page_address(page
) + page_offset
, 0, csize
);
1571 memcpy(data
, page_address(page
) + page_offset
, csize
);
1574 memcpy(page_address(page
) + page_offset
, data
, csize
);
1583 * Handling of buffer targets (buftargs).
1587 * Wait for any bufs with callbacks that have been submitted but have not yet
1588 * returned. These buffers will have an elevated hold count, so wait on those
1589 * while freeing all the buffers only held by the LRU.
1591 static enum lru_status
1592 xfs_buftarg_wait_rele(
1593 struct list_head
*item
,
1594 struct list_lru_one
*lru
,
1595 spinlock_t
*lru_lock
,
1599 struct xfs_buf
*bp
= container_of(item
, struct xfs_buf
, b_lru
);
1600 struct list_head
*dispose
= arg
;
1602 if (atomic_read(&bp
->b_hold
) > 1) {
1603 /* need to wait, so skip it this pass */
1604 trace_xfs_buf_wait_buftarg(bp
, _RET_IP_
);
1607 if (!spin_trylock(&bp
->b_lock
))
1611 * clear the LRU reference count so the buffer doesn't get
1612 * ignored in xfs_buf_rele().
1614 atomic_set(&bp
->b_lru_ref
, 0);
1615 bp
->b_state
|= XFS_BSTATE_DISPOSE
;
1616 list_lru_isolate_move(lru
, item
, dispose
);
1617 spin_unlock(&bp
->b_lock
);
1623 struct xfs_buftarg
*btp
)
1629 * First wait on the buftarg I/O count for all in-flight buffers to be
1630 * released. This is critical as new buffers do not make the LRU until
1631 * they are released.
1633 * Next, flush the buffer workqueue to ensure all completion processing
1634 * has finished. Just waiting on buffer locks is not sufficient for
1635 * async IO as the reference count held over IO is not released until
1636 * after the buffer lock is dropped. Hence we need to ensure here that
1637 * all reference counts have been dropped before we start walking the
1640 while (percpu_counter_sum(&btp
->bt_io_count
))
1642 flush_workqueue(btp
->bt_mount
->m_buf_workqueue
);
1644 /* loop until there is nothing left on the lru list. */
1645 while (list_lru_count(&btp
->bt_lru
)) {
1646 list_lru_walk(&btp
->bt_lru
, xfs_buftarg_wait_rele
,
1647 &dispose
, LONG_MAX
);
1649 while (!list_empty(&dispose
)) {
1651 bp
= list_first_entry(&dispose
, struct xfs_buf
, b_lru
);
1652 list_del_init(&bp
->b_lru
);
1653 if (bp
->b_flags
& XBF_WRITE_FAIL
) {
1654 xfs_alert(btp
->bt_mount
,
1655 "Corruption Alert: Buffer at daddr 0x%llx had permanent write failures!",
1656 (long long)bp
->b_bn
);
1657 xfs_alert(btp
->bt_mount
,
1658 "Please run xfs_repair to determine the extent of the problem.");
1667 static enum lru_status
1668 xfs_buftarg_isolate(
1669 struct list_head
*item
,
1670 struct list_lru_one
*lru
,
1671 spinlock_t
*lru_lock
,
1674 struct xfs_buf
*bp
= container_of(item
, struct xfs_buf
, b_lru
);
1675 struct list_head
*dispose
= arg
;
1678 * we are inverting the lru lock/bp->b_lock here, so use a trylock.
1679 * If we fail to get the lock, just skip it.
1681 if (!spin_trylock(&bp
->b_lock
))
1684 * Decrement the b_lru_ref count unless the value is already
1685 * zero. If the value is already zero, we need to reclaim the
1686 * buffer, otherwise it gets another trip through the LRU.
1688 if (atomic_add_unless(&bp
->b_lru_ref
, -1, 0)) {
1689 spin_unlock(&bp
->b_lock
);
1693 bp
->b_state
|= XFS_BSTATE_DISPOSE
;
1694 list_lru_isolate_move(lru
, item
, dispose
);
1695 spin_unlock(&bp
->b_lock
);
1699 static unsigned long
1700 xfs_buftarg_shrink_scan(
1701 struct shrinker
*shrink
,
1702 struct shrink_control
*sc
)
1704 struct xfs_buftarg
*btp
= container_of(shrink
,
1705 struct xfs_buftarg
, bt_shrinker
);
1707 unsigned long freed
;
1709 freed
= list_lru_shrink_walk(&btp
->bt_lru
, sc
,
1710 xfs_buftarg_isolate
, &dispose
);
1712 while (!list_empty(&dispose
)) {
1714 bp
= list_first_entry(&dispose
, struct xfs_buf
, b_lru
);
1715 list_del_init(&bp
->b_lru
);
1722 static unsigned long
1723 xfs_buftarg_shrink_count(
1724 struct shrinker
*shrink
,
1725 struct shrink_control
*sc
)
1727 struct xfs_buftarg
*btp
= container_of(shrink
,
1728 struct xfs_buftarg
, bt_shrinker
);
1729 return list_lru_shrink_count(&btp
->bt_lru
, sc
);
1734 struct xfs_buftarg
*btp
)
1736 unregister_shrinker(&btp
->bt_shrinker
);
1737 ASSERT(percpu_counter_sum(&btp
->bt_io_count
) == 0);
1738 percpu_counter_destroy(&btp
->bt_io_count
);
1739 list_lru_destroy(&btp
->bt_lru
);
1741 xfs_blkdev_issue_flush(btp
);
1747 xfs_setsize_buftarg(
1749 unsigned int sectorsize
)
1751 /* Set up metadata sector size info */
1752 btp
->bt_meta_sectorsize
= sectorsize
;
1753 btp
->bt_meta_sectormask
= sectorsize
- 1;
1755 if (set_blocksize(btp
->bt_bdev
, sectorsize
)) {
1756 xfs_warn(btp
->bt_mount
,
1757 "Cannot set_blocksize to %u on device %pg",
1758 sectorsize
, btp
->bt_bdev
);
1762 /* Set up device logical sector size mask */
1763 btp
->bt_logical_sectorsize
= bdev_logical_block_size(btp
->bt_bdev
);
1764 btp
->bt_logical_sectormask
= bdev_logical_block_size(btp
->bt_bdev
) - 1;
1770 * When allocating the initial buffer target we have not yet
1771 * read in the superblock, so don't know what sized sectors
1772 * are being used at this early stage. Play safe.
1775 xfs_setsize_buftarg_early(
1777 struct block_device
*bdev
)
1779 return xfs_setsize_buftarg(btp
, bdev_logical_block_size(bdev
));
1784 struct xfs_mount
*mp
,
1785 struct block_device
*bdev
,
1786 struct dax_device
*dax_dev
)
1790 btp
= kmem_zalloc(sizeof(*btp
), KM_SLEEP
| KM_NOFS
);
1793 btp
->bt_dev
= bdev
->bd_dev
;
1794 btp
->bt_bdev
= bdev
;
1795 btp
->bt_daxdev
= dax_dev
;
1797 if (xfs_setsize_buftarg_early(btp
, bdev
))
1800 if (list_lru_init(&btp
->bt_lru
))
1803 if (percpu_counter_init(&btp
->bt_io_count
, 0, GFP_KERNEL
))
1806 btp
->bt_shrinker
.count_objects
= xfs_buftarg_shrink_count
;
1807 btp
->bt_shrinker
.scan_objects
= xfs_buftarg_shrink_scan
;
1808 btp
->bt_shrinker
.seeks
= DEFAULT_SEEKS
;
1809 btp
->bt_shrinker
.flags
= SHRINKER_NUMA_AWARE
;
1810 if (register_shrinker(&btp
->bt_shrinker
))
1815 percpu_counter_destroy(&btp
->bt_io_count
);
1817 list_lru_destroy(&btp
->bt_lru
);
1824 * Cancel a delayed write list.
1826 * Remove each buffer from the list, clear the delwri queue flag and drop the
1827 * associated buffer reference.
1830 xfs_buf_delwri_cancel(
1831 struct list_head
*list
)
1835 while (!list_empty(list
)) {
1836 bp
= list_first_entry(list
, struct xfs_buf
, b_list
);
1839 bp
->b_flags
&= ~_XBF_DELWRI_Q
;
1840 list_del_init(&bp
->b_list
);
1846 * Add a buffer to the delayed write list.
1848 * This queues a buffer for writeout if it hasn't already been. Note that
1849 * neither this routine nor the buffer list submission functions perform
1850 * any internal synchronization. It is expected that the lists are thread-local
1853 * Returns true if we queued up the buffer, or false if it already had
1854 * been on the buffer list.
1857 xfs_buf_delwri_queue(
1859 struct list_head
*list
)
1861 ASSERT(xfs_buf_islocked(bp
));
1862 ASSERT(!(bp
->b_flags
& XBF_READ
));
1865 * If the buffer is already marked delwri it already is queued up
1866 * by someone else for imediate writeout. Just ignore it in that
1869 if (bp
->b_flags
& _XBF_DELWRI_Q
) {
1870 trace_xfs_buf_delwri_queued(bp
, _RET_IP_
);
1874 trace_xfs_buf_delwri_queue(bp
, _RET_IP_
);
1877 * If a buffer gets written out synchronously or marked stale while it
1878 * is on a delwri list we lazily remove it. To do this, the other party
1879 * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
1880 * It remains referenced and on the list. In a rare corner case it
1881 * might get readded to a delwri list after the synchronous writeout, in
1882 * which case we need just need to re-add the flag here.
1884 bp
->b_flags
|= _XBF_DELWRI_Q
;
1885 if (list_empty(&bp
->b_list
)) {
1886 atomic_inc(&bp
->b_hold
);
1887 list_add_tail(&bp
->b_list
, list
);
1894 * Compare function is more complex than it needs to be because
1895 * the return value is only 32 bits and we are doing comparisons
1901 struct list_head
*a
,
1902 struct list_head
*b
)
1904 struct xfs_buf
*ap
= container_of(a
, struct xfs_buf
, b_list
);
1905 struct xfs_buf
*bp
= container_of(b
, struct xfs_buf
, b_list
);
1908 diff
= ap
->b_maps
[0].bm_bn
- bp
->b_maps
[0].bm_bn
;
1917 * Submit buffers for write. If wait_list is specified, the buffers are
1918 * submitted using sync I/O and placed on the wait list such that the caller can
1919 * iowait each buffer. Otherwise async I/O is used and the buffers are released
1920 * at I/O completion time. In either case, buffers remain locked until I/O
1921 * completes and the buffer is released from the queue.
1924 xfs_buf_delwri_submit_buffers(
1925 struct list_head
*buffer_list
,
1926 struct list_head
*wait_list
)
1928 struct xfs_buf
*bp
, *n
;
1929 LIST_HEAD (submit_list
);
1931 struct blk_plug plug
;
1933 list_sort(NULL
, buffer_list
, xfs_buf_cmp
);
1935 blk_start_plug(&plug
);
1936 list_for_each_entry_safe(bp
, n
, buffer_list
, b_list
) {
1938 if (xfs_buf_ispinned(bp
)) {
1942 if (!xfs_buf_trylock(bp
))
1949 * Someone else might have written the buffer synchronously or
1950 * marked it stale in the meantime. In that case only the
1951 * _XBF_DELWRI_Q flag got cleared, and we have to drop the
1952 * reference and remove it from the list here.
1954 if (!(bp
->b_flags
& _XBF_DELWRI_Q
)) {
1955 list_del_init(&bp
->b_list
);
1960 trace_xfs_buf_delwri_split(bp
, _RET_IP_
);
1963 * If we have a wait list, each buffer (and associated delwri
1964 * queue reference) transfers to it and is submitted
1965 * synchronously. Otherwise, drop the buffer from the delwri
1966 * queue and submit async.
1968 bp
->b_flags
&= ~(_XBF_DELWRI_Q
| XBF_WRITE_FAIL
);
1969 bp
->b_flags
|= XBF_WRITE
;
1971 bp
->b_flags
&= ~XBF_ASYNC
;
1972 list_move_tail(&bp
->b_list
, wait_list
);
1974 bp
->b_flags
|= XBF_ASYNC
;
1975 list_del_init(&bp
->b_list
);
1977 __xfs_buf_submit(bp
, false);
1979 blk_finish_plug(&plug
);
1985 * Write out a buffer list asynchronously.
1987 * This will take the @buffer_list, write all non-locked and non-pinned buffers
1988 * out and not wait for I/O completion on any of the buffers. This interface
1989 * is only safely useable for callers that can track I/O completion by higher
1990 * level means, e.g. AIL pushing as the @buffer_list is consumed in this
1994 xfs_buf_delwri_submit_nowait(
1995 struct list_head
*buffer_list
)
1997 return xfs_buf_delwri_submit_buffers(buffer_list
, NULL
);
2001 * Write out a buffer list synchronously.
2003 * This will take the @buffer_list, write all buffers out and wait for I/O
2004 * completion on all of the buffers. @buffer_list is consumed by the function,
2005 * so callers must have some other way of tracking buffers if they require such
2009 xfs_buf_delwri_submit(
2010 struct list_head
*buffer_list
)
2012 LIST_HEAD (wait_list
);
2013 int error
= 0, error2
;
2016 xfs_buf_delwri_submit_buffers(buffer_list
, &wait_list
);
2018 /* Wait for IO to complete. */
2019 while (!list_empty(&wait_list
)) {
2020 bp
= list_first_entry(&wait_list
, struct xfs_buf
, b_list
);
2022 list_del_init(&bp
->b_list
);
2025 * Wait on the locked buffer, check for errors and unlock and
2026 * release the delwri queue reference.
2028 error2
= xfs_buf_iowait(bp
);
2038 * Push a single buffer on a delwri queue.
2040 * The purpose of this function is to submit a single buffer of a delwri queue
2041 * and return with the buffer still on the original queue. The waiting delwri
2042 * buffer submission infrastructure guarantees transfer of the delwri queue
2043 * buffer reference to a temporary wait list. We reuse this infrastructure to
2044 * transfer the buffer back to the original queue.
2046 * Note the buffer transitions from the queued state, to the submitted and wait
2047 * listed state and back to the queued state during this call. The buffer
2048 * locking and queue management logic between _delwri_pushbuf() and
2049 * _delwri_queue() guarantee that the buffer cannot be queued to another list
2053 xfs_buf_delwri_pushbuf(
2055 struct list_head
*buffer_list
)
2057 LIST_HEAD (submit_list
);
2060 ASSERT(bp
->b_flags
& _XBF_DELWRI_Q
);
2062 trace_xfs_buf_delwri_pushbuf(bp
, _RET_IP_
);
2065 * Isolate the buffer to a new local list so we can submit it for I/O
2066 * independently from the rest of the original list.
2069 list_move(&bp
->b_list
, &submit_list
);
2073 * Delwri submission clears the DELWRI_Q buffer flag and returns with
2074 * the buffer on the wait list with the original reference. Rather than
2075 * bounce the buffer from a local wait list back to the original list
2076 * after I/O completion, reuse the original list as the wait list.
2078 xfs_buf_delwri_submit_buffers(&submit_list
, buffer_list
);
2081 * The buffer is now locked, under I/O and wait listed on the original
2082 * delwri queue. Wait for I/O completion, restore the DELWRI_Q flag and
2083 * return with the buffer unlocked and on the original queue.
2085 error
= xfs_buf_iowait(bp
);
2086 bp
->b_flags
|= _XBF_DELWRI_Q
;
2095 xfs_buf_zone
= kmem_zone_init_flags(sizeof(xfs_buf_t
), "xfs_buf",
2096 KM_ZONE_HWALIGN
, NULL
);
2107 xfs_buf_terminate(void)
2109 kmem_zone_destroy(xfs_buf_zone
);
2112 void xfs_buf_set_ref(struct xfs_buf
*bp
, int lru_ref
)
2115 * Set the lru reference count to 0 based on the error injection tag.
2116 * This allows userspace to disrupt buffer caching for debug/testing
2119 if (XFS_TEST_ERROR(false, bp
->b_target
->bt_mount
,
2120 XFS_ERRTAG_BUF_LRU_REF
))
2123 atomic_set(&bp
->b_lru_ref
, lru_ref
);