arm64: kgdb: Fix single-step exception handling oops
[linux/fpc-iii.git] / fs / xfs / xfs_buf.c
blob0b58b9d419e84d4d1def0d275a5cff67b4956ae5
1 /*
2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
3 * All Rights Reserved.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 #include "xfs.h"
19 #include <linux/stddef.h>
20 #include <linux/errno.h>
21 #include <linux/gfp.h>
22 #include <linux/pagemap.h>
23 #include <linux/init.h>
24 #include <linux/vmalloc.h>
25 #include <linux/bio.h>
26 #include <linux/sysctl.h>
27 #include <linux/proc_fs.h>
28 #include <linux/workqueue.h>
29 #include <linux/percpu.h>
30 #include <linux/blkdev.h>
31 #include <linux/hash.h>
32 #include <linux/kthread.h>
33 #include <linux/migrate.h>
34 #include <linux/backing-dev.h>
35 #include <linux/freezer.h>
37 #include "xfs_format.h"
38 #include "xfs_log_format.h"
39 #include "xfs_trans_resv.h"
40 #include "xfs_sb.h"
41 #include "xfs_mount.h"
42 #include "xfs_trace.h"
43 #include "xfs_log.h"
45 static kmem_zone_t *xfs_buf_zone;
47 #ifdef XFS_BUF_LOCK_TRACKING
48 # define XB_SET_OWNER(bp) ((bp)->b_last_holder = current->pid)
49 # define XB_CLEAR_OWNER(bp) ((bp)->b_last_holder = -1)
50 # define XB_GET_OWNER(bp) ((bp)->b_last_holder)
51 #else
52 # define XB_SET_OWNER(bp) do { } while (0)
53 # define XB_CLEAR_OWNER(bp) do { } while (0)
54 # define XB_GET_OWNER(bp) do { } while (0)
55 #endif
57 #define xb_to_gfp(flags) \
58 ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : GFP_NOFS) | __GFP_NOWARN)
61 * Locking orders
63 * xfs_buf_ioacct_inc:
64 * xfs_buf_ioacct_dec:
65 * b_sema (caller holds)
66 * b_lock
68 * xfs_buf_stale:
69 * b_sema (caller holds)
70 * b_lock
71 * lru_lock
73 * xfs_buf_rele:
74 * b_lock
75 * pag_buf_lock
76 * lru_lock
78 * xfs_buftarg_wait_rele
79 * lru_lock
80 * b_lock (trylock due to inversion)
82 * xfs_buftarg_isolate
83 * lru_lock
84 * b_lock (trylock due to inversion)
87 static inline int
88 xfs_buf_is_vmapped(
89 struct xfs_buf *bp)
92 * Return true if the buffer is vmapped.
94 * b_addr is null if the buffer is not mapped, but the code is clever
95 * enough to know it doesn't have to map a single page, so the check has
96 * to be both for b_addr and bp->b_page_count > 1.
98 return bp->b_addr && bp->b_page_count > 1;
101 static inline int
102 xfs_buf_vmap_len(
103 struct xfs_buf *bp)
105 return (bp->b_page_count * PAGE_SIZE) - bp->b_offset;
109 * Bump the I/O in flight count on the buftarg if we haven't yet done so for
110 * this buffer. The count is incremented once per buffer (per hold cycle)
111 * because the corresponding decrement is deferred to buffer release. Buffers
112 * can undergo I/O multiple times in a hold-release cycle and per buffer I/O
113 * tracking adds unnecessary overhead. This is used for sychronization purposes
114 * with unmount (see xfs_wait_buftarg()), so all we really need is a count of
115 * in-flight buffers.
117 * Buffers that are never released (e.g., superblock, iclog buffers) must set
118 * the XBF_NO_IOACCT flag before I/O submission. Otherwise, the buftarg count
119 * never reaches zero and unmount hangs indefinitely.
121 static inline void
122 xfs_buf_ioacct_inc(
123 struct xfs_buf *bp)
125 if (bp->b_flags & XBF_NO_IOACCT)
126 return;
128 ASSERT(bp->b_flags & XBF_ASYNC);
129 spin_lock(&bp->b_lock);
130 if (!(bp->b_state & XFS_BSTATE_IN_FLIGHT)) {
131 bp->b_state |= XFS_BSTATE_IN_FLIGHT;
132 percpu_counter_inc(&bp->b_target->bt_io_count);
134 spin_unlock(&bp->b_lock);
138 * Clear the in-flight state on a buffer about to be released to the LRU or
139 * freed and unaccount from the buftarg.
141 static inline void
142 __xfs_buf_ioacct_dec(
143 struct xfs_buf *bp)
145 lockdep_assert_held(&bp->b_lock);
147 if (bp->b_state & XFS_BSTATE_IN_FLIGHT) {
148 bp->b_state &= ~XFS_BSTATE_IN_FLIGHT;
149 percpu_counter_dec(&bp->b_target->bt_io_count);
153 static inline void
154 xfs_buf_ioacct_dec(
155 struct xfs_buf *bp)
157 spin_lock(&bp->b_lock);
158 __xfs_buf_ioacct_dec(bp);
159 spin_unlock(&bp->b_lock);
163 * When we mark a buffer stale, we remove the buffer from the LRU and clear the
164 * b_lru_ref count so that the buffer is freed immediately when the buffer
165 * reference count falls to zero. If the buffer is already on the LRU, we need
166 * to remove the reference that LRU holds on the buffer.
168 * This prevents build-up of stale buffers on the LRU.
170 void
171 xfs_buf_stale(
172 struct xfs_buf *bp)
174 ASSERT(xfs_buf_islocked(bp));
176 bp->b_flags |= XBF_STALE;
179 * Clear the delwri status so that a delwri queue walker will not
180 * flush this buffer to disk now that it is stale. The delwri queue has
181 * a reference to the buffer, so this is safe to do.
183 bp->b_flags &= ~_XBF_DELWRI_Q;
186 * Once the buffer is marked stale and unlocked, a subsequent lookup
187 * could reset b_flags. There is no guarantee that the buffer is
188 * unaccounted (released to LRU) before that occurs. Drop in-flight
189 * status now to preserve accounting consistency.
191 spin_lock(&bp->b_lock);
192 __xfs_buf_ioacct_dec(bp);
194 atomic_set(&bp->b_lru_ref, 0);
195 if (!(bp->b_state & XFS_BSTATE_DISPOSE) &&
196 (list_lru_del(&bp->b_target->bt_lru, &bp->b_lru)))
197 atomic_dec(&bp->b_hold);
199 ASSERT(atomic_read(&bp->b_hold) >= 1);
200 spin_unlock(&bp->b_lock);
203 static int
204 xfs_buf_get_maps(
205 struct xfs_buf *bp,
206 int map_count)
208 ASSERT(bp->b_maps == NULL);
209 bp->b_map_count = map_count;
211 if (map_count == 1) {
212 bp->b_maps = &bp->__b_map;
213 return 0;
216 bp->b_maps = kmem_zalloc(map_count * sizeof(struct xfs_buf_map),
217 KM_NOFS);
218 if (!bp->b_maps)
219 return -ENOMEM;
220 return 0;
224 * Frees b_pages if it was allocated.
226 static void
227 xfs_buf_free_maps(
228 struct xfs_buf *bp)
230 if (bp->b_maps != &bp->__b_map) {
231 kmem_free(bp->b_maps);
232 bp->b_maps = NULL;
236 struct xfs_buf *
237 _xfs_buf_alloc(
238 struct xfs_buftarg *target,
239 struct xfs_buf_map *map,
240 int nmaps,
241 xfs_buf_flags_t flags)
243 struct xfs_buf *bp;
244 int error;
245 int i;
247 bp = kmem_zone_zalloc(xfs_buf_zone, KM_NOFS);
248 if (unlikely(!bp))
249 return NULL;
252 * We don't want certain flags to appear in b_flags unless they are
253 * specifically set by later operations on the buffer.
255 flags &= ~(XBF_UNMAPPED | XBF_TRYLOCK | XBF_ASYNC | XBF_READ_AHEAD);
257 atomic_set(&bp->b_hold, 1);
258 atomic_set(&bp->b_lru_ref, 1);
259 init_completion(&bp->b_iowait);
260 INIT_LIST_HEAD(&bp->b_lru);
261 INIT_LIST_HEAD(&bp->b_list);
262 RB_CLEAR_NODE(&bp->b_rbnode);
263 sema_init(&bp->b_sema, 0); /* held, no waiters */
264 spin_lock_init(&bp->b_lock);
265 XB_SET_OWNER(bp);
266 bp->b_target = target;
267 bp->b_flags = flags;
270 * Set length and io_length to the same value initially.
271 * I/O routines should use io_length, which will be the same in
272 * most cases but may be reset (e.g. XFS recovery).
274 error = xfs_buf_get_maps(bp, nmaps);
275 if (error) {
276 kmem_zone_free(xfs_buf_zone, bp);
277 return NULL;
280 bp->b_bn = map[0].bm_bn;
281 bp->b_length = 0;
282 for (i = 0; i < nmaps; i++) {
283 bp->b_maps[i].bm_bn = map[i].bm_bn;
284 bp->b_maps[i].bm_len = map[i].bm_len;
285 bp->b_length += map[i].bm_len;
287 bp->b_io_length = bp->b_length;
289 atomic_set(&bp->b_pin_count, 0);
290 init_waitqueue_head(&bp->b_waiters);
292 XFS_STATS_INC(target->bt_mount, xb_create);
293 trace_xfs_buf_init(bp, _RET_IP_);
295 return bp;
299 * Allocate a page array capable of holding a specified number
300 * of pages, and point the page buf at it.
302 STATIC int
303 _xfs_buf_get_pages(
304 xfs_buf_t *bp,
305 int page_count)
307 /* Make sure that we have a page list */
308 if (bp->b_pages == NULL) {
309 bp->b_page_count = page_count;
310 if (page_count <= XB_PAGES) {
311 bp->b_pages = bp->b_page_array;
312 } else {
313 bp->b_pages = kmem_alloc(sizeof(struct page *) *
314 page_count, KM_NOFS);
315 if (bp->b_pages == NULL)
316 return -ENOMEM;
318 memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
320 return 0;
324 * Frees b_pages if it was allocated.
326 STATIC void
327 _xfs_buf_free_pages(
328 xfs_buf_t *bp)
330 if (bp->b_pages != bp->b_page_array) {
331 kmem_free(bp->b_pages);
332 bp->b_pages = NULL;
337 * Releases the specified buffer.
339 * The modification state of any associated pages is left unchanged.
340 * The buffer must not be on any hash - use xfs_buf_rele instead for
341 * hashed and refcounted buffers
343 void
344 xfs_buf_free(
345 xfs_buf_t *bp)
347 trace_xfs_buf_free(bp, _RET_IP_);
349 ASSERT(list_empty(&bp->b_lru));
351 if (bp->b_flags & _XBF_PAGES) {
352 uint i;
354 if (xfs_buf_is_vmapped(bp))
355 vm_unmap_ram(bp->b_addr - bp->b_offset,
356 bp->b_page_count);
358 for (i = 0; i < bp->b_page_count; i++) {
359 struct page *page = bp->b_pages[i];
361 __free_page(page);
363 } else if (bp->b_flags & _XBF_KMEM)
364 kmem_free(bp->b_addr);
365 _xfs_buf_free_pages(bp);
366 xfs_buf_free_maps(bp);
367 kmem_zone_free(xfs_buf_zone, bp);
371 * Allocates all the pages for buffer in question and builds it's page list.
373 STATIC int
374 xfs_buf_allocate_memory(
375 xfs_buf_t *bp,
376 uint flags)
378 size_t size;
379 size_t nbytes, offset;
380 gfp_t gfp_mask = xb_to_gfp(flags);
381 unsigned short page_count, i;
382 xfs_off_t start, end;
383 int error;
386 * for buffers that are contained within a single page, just allocate
387 * the memory from the heap - there's no need for the complexity of
388 * page arrays to keep allocation down to order 0.
390 size = BBTOB(bp->b_length);
391 if (size < PAGE_SIZE) {
392 bp->b_addr = kmem_alloc(size, KM_NOFS);
393 if (!bp->b_addr) {
394 /* low memory - use alloc_page loop instead */
395 goto use_alloc_page;
398 if (((unsigned long)(bp->b_addr + size - 1) & PAGE_MASK) !=
399 ((unsigned long)bp->b_addr & PAGE_MASK)) {
400 /* b_addr spans two pages - use alloc_page instead */
401 kmem_free(bp->b_addr);
402 bp->b_addr = NULL;
403 goto use_alloc_page;
405 bp->b_offset = offset_in_page(bp->b_addr);
406 bp->b_pages = bp->b_page_array;
407 bp->b_pages[0] = virt_to_page(bp->b_addr);
408 bp->b_page_count = 1;
409 bp->b_flags |= _XBF_KMEM;
410 return 0;
413 use_alloc_page:
414 start = BBTOB(bp->b_maps[0].bm_bn) >> PAGE_SHIFT;
415 end = (BBTOB(bp->b_maps[0].bm_bn + bp->b_length) + PAGE_SIZE - 1)
416 >> PAGE_SHIFT;
417 page_count = end - start;
418 error = _xfs_buf_get_pages(bp, page_count);
419 if (unlikely(error))
420 return error;
422 offset = bp->b_offset;
423 bp->b_flags |= _XBF_PAGES;
425 for (i = 0; i < bp->b_page_count; i++) {
426 struct page *page;
427 uint retries = 0;
428 retry:
429 page = alloc_page(gfp_mask);
430 if (unlikely(page == NULL)) {
431 if (flags & XBF_READ_AHEAD) {
432 bp->b_page_count = i;
433 error = -ENOMEM;
434 goto out_free_pages;
438 * This could deadlock.
440 * But until all the XFS lowlevel code is revamped to
441 * handle buffer allocation failures we can't do much.
443 if (!(++retries % 100))
444 xfs_err(NULL,
445 "%s(%u) possible memory allocation deadlock in %s (mode:0x%x)",
446 current->comm, current->pid,
447 __func__, gfp_mask);
449 XFS_STATS_INC(bp->b_target->bt_mount, xb_page_retries);
450 congestion_wait(BLK_RW_ASYNC, HZ/50);
451 goto retry;
454 XFS_STATS_INC(bp->b_target->bt_mount, xb_page_found);
456 nbytes = min_t(size_t, size, PAGE_SIZE - offset);
457 size -= nbytes;
458 bp->b_pages[i] = page;
459 offset = 0;
461 return 0;
463 out_free_pages:
464 for (i = 0; i < bp->b_page_count; i++)
465 __free_page(bp->b_pages[i]);
466 bp->b_flags &= ~_XBF_PAGES;
467 return error;
471 * Map buffer into kernel address-space if necessary.
473 STATIC int
474 _xfs_buf_map_pages(
475 xfs_buf_t *bp,
476 uint flags)
478 ASSERT(bp->b_flags & _XBF_PAGES);
479 if (bp->b_page_count == 1) {
480 /* A single page buffer is always mappable */
481 bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
482 } else if (flags & XBF_UNMAPPED) {
483 bp->b_addr = NULL;
484 } else {
485 int retried = 0;
486 unsigned noio_flag;
489 * vm_map_ram() will allocate auxillary structures (e.g.
490 * pagetables) with GFP_KERNEL, yet we are likely to be under
491 * GFP_NOFS context here. Hence we need to tell memory reclaim
492 * that we are in such a context via PF_MEMALLOC_NOIO to prevent
493 * memory reclaim re-entering the filesystem here and
494 * potentially deadlocking.
496 noio_flag = memalloc_noio_save();
497 do {
498 bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
499 -1, PAGE_KERNEL);
500 if (bp->b_addr)
501 break;
502 vm_unmap_aliases();
503 } while (retried++ <= 1);
504 memalloc_noio_restore(noio_flag);
506 if (!bp->b_addr)
507 return -ENOMEM;
508 bp->b_addr += bp->b_offset;
511 return 0;
515 * Finding and Reading Buffers
519 * Look up, and creates if absent, a lockable buffer for
520 * a given range of an inode. The buffer is returned
521 * locked. No I/O is implied by this call.
523 xfs_buf_t *
524 _xfs_buf_find(
525 struct xfs_buftarg *btp,
526 struct xfs_buf_map *map,
527 int nmaps,
528 xfs_buf_flags_t flags,
529 xfs_buf_t *new_bp)
531 struct xfs_perag *pag;
532 struct rb_node **rbp;
533 struct rb_node *parent;
534 xfs_buf_t *bp;
535 xfs_daddr_t blkno = map[0].bm_bn;
536 xfs_daddr_t eofs;
537 int numblks = 0;
538 int i;
540 for (i = 0; i < nmaps; i++)
541 numblks += map[i].bm_len;
543 /* Check for IOs smaller than the sector size / not sector aligned */
544 ASSERT(!(BBTOB(numblks) < btp->bt_meta_sectorsize));
545 ASSERT(!(BBTOB(blkno) & (xfs_off_t)btp->bt_meta_sectormask));
548 * Corrupted block numbers can get through to here, unfortunately, so we
549 * have to check that the buffer falls within the filesystem bounds.
551 eofs = XFS_FSB_TO_BB(btp->bt_mount, btp->bt_mount->m_sb.sb_dblocks);
552 if (blkno < 0 || blkno >= eofs) {
554 * XXX (dgc): we should really be returning -EFSCORRUPTED here,
555 * but none of the higher level infrastructure supports
556 * returning a specific error on buffer lookup failures.
558 xfs_alert(btp->bt_mount,
559 "%s: Block out of range: block 0x%llx, EOFS 0x%llx ",
560 __func__, blkno, eofs);
561 WARN_ON(1);
562 return NULL;
565 /* get tree root */
566 pag = xfs_perag_get(btp->bt_mount,
567 xfs_daddr_to_agno(btp->bt_mount, blkno));
569 /* walk tree */
570 spin_lock(&pag->pag_buf_lock);
571 rbp = &pag->pag_buf_tree.rb_node;
572 parent = NULL;
573 bp = NULL;
574 while (*rbp) {
575 parent = *rbp;
576 bp = rb_entry(parent, struct xfs_buf, b_rbnode);
578 if (blkno < bp->b_bn)
579 rbp = &(*rbp)->rb_left;
580 else if (blkno > bp->b_bn)
581 rbp = &(*rbp)->rb_right;
582 else {
584 * found a block number match. If the range doesn't
585 * match, the only way this is allowed is if the buffer
586 * in the cache is stale and the transaction that made
587 * it stale has not yet committed. i.e. we are
588 * reallocating a busy extent. Skip this buffer and
589 * continue searching to the right for an exact match.
591 if (bp->b_length != numblks) {
592 ASSERT(bp->b_flags & XBF_STALE);
593 rbp = &(*rbp)->rb_right;
594 continue;
596 atomic_inc(&bp->b_hold);
597 goto found;
601 /* No match found */
602 if (new_bp) {
603 rb_link_node(&new_bp->b_rbnode, parent, rbp);
604 rb_insert_color(&new_bp->b_rbnode, &pag->pag_buf_tree);
605 /* the buffer keeps the perag reference until it is freed */
606 new_bp->b_pag = pag;
607 spin_unlock(&pag->pag_buf_lock);
608 } else {
609 XFS_STATS_INC(btp->bt_mount, xb_miss_locked);
610 spin_unlock(&pag->pag_buf_lock);
611 xfs_perag_put(pag);
613 return new_bp;
615 found:
616 spin_unlock(&pag->pag_buf_lock);
617 xfs_perag_put(pag);
619 if (!xfs_buf_trylock(bp)) {
620 if (flags & XBF_TRYLOCK) {
621 xfs_buf_rele(bp);
622 XFS_STATS_INC(btp->bt_mount, xb_busy_locked);
623 return NULL;
625 xfs_buf_lock(bp);
626 XFS_STATS_INC(btp->bt_mount, xb_get_locked_waited);
630 * if the buffer is stale, clear all the external state associated with
631 * it. We need to keep flags such as how we allocated the buffer memory
632 * intact here.
634 if (bp->b_flags & XBF_STALE) {
635 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
636 ASSERT(bp->b_iodone == NULL);
637 bp->b_flags &= _XBF_KMEM | _XBF_PAGES;
638 bp->b_ops = NULL;
641 trace_xfs_buf_find(bp, flags, _RET_IP_);
642 XFS_STATS_INC(btp->bt_mount, xb_get_locked);
643 return bp;
647 * Assembles a buffer covering the specified range. The code is optimised for
648 * cache hits, as metadata intensive workloads will see 3 orders of magnitude
649 * more hits than misses.
651 struct xfs_buf *
652 xfs_buf_get_map(
653 struct xfs_buftarg *target,
654 struct xfs_buf_map *map,
655 int nmaps,
656 xfs_buf_flags_t flags)
658 struct xfs_buf *bp;
659 struct xfs_buf *new_bp;
660 int error = 0;
662 bp = _xfs_buf_find(target, map, nmaps, flags, NULL);
663 if (likely(bp))
664 goto found;
666 new_bp = _xfs_buf_alloc(target, map, nmaps, flags);
667 if (unlikely(!new_bp))
668 return NULL;
670 error = xfs_buf_allocate_memory(new_bp, flags);
671 if (error) {
672 xfs_buf_free(new_bp);
673 return NULL;
676 bp = _xfs_buf_find(target, map, nmaps, flags, new_bp);
677 if (!bp) {
678 xfs_buf_free(new_bp);
679 return NULL;
682 if (bp != new_bp)
683 xfs_buf_free(new_bp);
685 found:
686 if (!bp->b_addr) {
687 error = _xfs_buf_map_pages(bp, flags);
688 if (unlikely(error)) {
689 xfs_warn(target->bt_mount,
690 "%s: failed to map pagesn", __func__);
691 xfs_buf_relse(bp);
692 return NULL;
697 * Clear b_error if this is a lookup from a caller that doesn't expect
698 * valid data to be found in the buffer.
700 if (!(flags & XBF_READ))
701 xfs_buf_ioerror(bp, 0);
703 XFS_STATS_INC(target->bt_mount, xb_get);
704 trace_xfs_buf_get(bp, flags, _RET_IP_);
705 return bp;
708 STATIC int
709 _xfs_buf_read(
710 xfs_buf_t *bp,
711 xfs_buf_flags_t flags)
713 ASSERT(!(flags & XBF_WRITE));
714 ASSERT(bp->b_maps[0].bm_bn != XFS_BUF_DADDR_NULL);
716 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD);
717 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
719 if (flags & XBF_ASYNC) {
720 xfs_buf_submit(bp);
721 return 0;
723 return xfs_buf_submit_wait(bp);
726 xfs_buf_t *
727 xfs_buf_read_map(
728 struct xfs_buftarg *target,
729 struct xfs_buf_map *map,
730 int nmaps,
731 xfs_buf_flags_t flags,
732 const struct xfs_buf_ops *ops)
734 struct xfs_buf *bp;
736 flags |= XBF_READ;
738 bp = xfs_buf_get_map(target, map, nmaps, flags);
739 if (bp) {
740 trace_xfs_buf_read(bp, flags, _RET_IP_);
742 if (!(bp->b_flags & XBF_DONE)) {
743 XFS_STATS_INC(target->bt_mount, xb_get_read);
744 bp->b_ops = ops;
745 _xfs_buf_read(bp, flags);
746 } else if (flags & XBF_ASYNC) {
748 * Read ahead call which is already satisfied,
749 * drop the buffer
751 xfs_buf_relse(bp);
752 return NULL;
753 } else {
754 /* We do not want read in the flags */
755 bp->b_flags &= ~XBF_READ;
759 return bp;
763 * If we are not low on memory then do the readahead in a deadlock
764 * safe manner.
766 void
767 xfs_buf_readahead_map(
768 struct xfs_buftarg *target,
769 struct xfs_buf_map *map,
770 int nmaps,
771 const struct xfs_buf_ops *ops)
773 if (bdi_read_congested(target->bt_bdi))
774 return;
776 xfs_buf_read_map(target, map, nmaps,
777 XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD, ops);
781 * Read an uncached buffer from disk. Allocates and returns a locked
782 * buffer containing the disk contents or nothing.
785 xfs_buf_read_uncached(
786 struct xfs_buftarg *target,
787 xfs_daddr_t daddr,
788 size_t numblks,
789 int flags,
790 struct xfs_buf **bpp,
791 const struct xfs_buf_ops *ops)
793 struct xfs_buf *bp;
795 *bpp = NULL;
797 bp = xfs_buf_get_uncached(target, numblks, flags);
798 if (!bp)
799 return -ENOMEM;
801 /* set up the buffer for a read IO */
802 ASSERT(bp->b_map_count == 1);
803 bp->b_bn = XFS_BUF_DADDR_NULL; /* always null for uncached buffers */
804 bp->b_maps[0].bm_bn = daddr;
805 bp->b_flags |= XBF_READ;
806 bp->b_ops = ops;
808 xfs_buf_submit_wait(bp);
809 if (bp->b_error) {
810 int error = bp->b_error;
811 xfs_buf_relse(bp);
812 return error;
815 *bpp = bp;
816 return 0;
820 * Return a buffer allocated as an empty buffer and associated to external
821 * memory via xfs_buf_associate_memory() back to it's empty state.
823 void
824 xfs_buf_set_empty(
825 struct xfs_buf *bp,
826 size_t numblks)
828 if (bp->b_pages)
829 _xfs_buf_free_pages(bp);
831 bp->b_pages = NULL;
832 bp->b_page_count = 0;
833 bp->b_addr = NULL;
834 bp->b_length = numblks;
835 bp->b_io_length = numblks;
837 ASSERT(bp->b_map_count == 1);
838 bp->b_bn = XFS_BUF_DADDR_NULL;
839 bp->b_maps[0].bm_bn = XFS_BUF_DADDR_NULL;
840 bp->b_maps[0].bm_len = bp->b_length;
843 static inline struct page *
844 mem_to_page(
845 void *addr)
847 if ((!is_vmalloc_addr(addr))) {
848 return virt_to_page(addr);
849 } else {
850 return vmalloc_to_page(addr);
855 xfs_buf_associate_memory(
856 xfs_buf_t *bp,
857 void *mem,
858 size_t len)
860 int rval;
861 int i = 0;
862 unsigned long pageaddr;
863 unsigned long offset;
864 size_t buflen;
865 int page_count;
867 pageaddr = (unsigned long)mem & PAGE_MASK;
868 offset = (unsigned long)mem - pageaddr;
869 buflen = PAGE_ALIGN(len + offset);
870 page_count = buflen >> PAGE_SHIFT;
872 /* Free any previous set of page pointers */
873 if (bp->b_pages)
874 _xfs_buf_free_pages(bp);
876 bp->b_pages = NULL;
877 bp->b_addr = mem;
879 rval = _xfs_buf_get_pages(bp, page_count);
880 if (rval)
881 return rval;
883 bp->b_offset = offset;
885 for (i = 0; i < bp->b_page_count; i++) {
886 bp->b_pages[i] = mem_to_page((void *)pageaddr);
887 pageaddr += PAGE_SIZE;
890 bp->b_io_length = BTOBB(len);
891 bp->b_length = BTOBB(buflen);
893 return 0;
896 xfs_buf_t *
897 xfs_buf_get_uncached(
898 struct xfs_buftarg *target,
899 size_t numblks,
900 int flags)
902 unsigned long page_count;
903 int error, i;
904 struct xfs_buf *bp;
905 DEFINE_SINGLE_BUF_MAP(map, XFS_BUF_DADDR_NULL, numblks);
907 /* flags might contain irrelevant bits, pass only what we care about */
908 bp = _xfs_buf_alloc(target, &map, 1, flags & XBF_NO_IOACCT);
909 if (unlikely(bp == NULL))
910 goto fail;
912 page_count = PAGE_ALIGN(numblks << BBSHIFT) >> PAGE_SHIFT;
913 error = _xfs_buf_get_pages(bp, page_count);
914 if (error)
915 goto fail_free_buf;
917 for (i = 0; i < page_count; i++) {
918 bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
919 if (!bp->b_pages[i])
920 goto fail_free_mem;
922 bp->b_flags |= _XBF_PAGES;
924 error = _xfs_buf_map_pages(bp, 0);
925 if (unlikely(error)) {
926 xfs_warn(target->bt_mount,
927 "%s: failed to map pages", __func__);
928 goto fail_free_mem;
931 trace_xfs_buf_get_uncached(bp, _RET_IP_);
932 return bp;
934 fail_free_mem:
935 while (--i >= 0)
936 __free_page(bp->b_pages[i]);
937 _xfs_buf_free_pages(bp);
938 fail_free_buf:
939 xfs_buf_free_maps(bp);
940 kmem_zone_free(xfs_buf_zone, bp);
941 fail:
942 return NULL;
946 * Increment reference count on buffer, to hold the buffer concurrently
947 * with another thread which may release (free) the buffer asynchronously.
948 * Must hold the buffer already to call this function.
950 void
951 xfs_buf_hold(
952 xfs_buf_t *bp)
954 trace_xfs_buf_hold(bp, _RET_IP_);
955 atomic_inc(&bp->b_hold);
959 * Release a hold on the specified buffer. If the hold count is 1, the buffer is
960 * placed on LRU or freed (depending on b_lru_ref).
962 void
963 xfs_buf_rele(
964 xfs_buf_t *bp)
966 struct xfs_perag *pag = bp->b_pag;
967 bool release;
968 bool freebuf = false;
970 trace_xfs_buf_rele(bp, _RET_IP_);
972 if (!pag) {
973 ASSERT(list_empty(&bp->b_lru));
974 ASSERT(RB_EMPTY_NODE(&bp->b_rbnode));
975 if (atomic_dec_and_test(&bp->b_hold)) {
976 xfs_buf_ioacct_dec(bp);
977 xfs_buf_free(bp);
979 return;
982 ASSERT(!RB_EMPTY_NODE(&bp->b_rbnode));
984 ASSERT(atomic_read(&bp->b_hold) > 0);
987 * We grab the b_lock here first to serialise racing xfs_buf_rele()
988 * calls. The pag_buf_lock being taken on the last reference only
989 * serialises against racing lookups in xfs_buf_find(). IOWs, the second
990 * to last reference we drop here is not serialised against the last
991 * reference until we take bp->b_lock. Hence if we don't grab b_lock
992 * first, the last "release" reference can win the race to the lock and
993 * free the buffer before the second-to-last reference is processed,
994 * leading to a use-after-free scenario.
996 spin_lock(&bp->b_lock);
997 release = atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock);
998 if (!release) {
1000 * Drop the in-flight state if the buffer is already on the LRU
1001 * and it holds the only reference. This is racy because we
1002 * haven't acquired the pag lock, but the use of _XBF_IN_FLIGHT
1003 * ensures the decrement occurs only once per-buf.
1005 if ((atomic_read(&bp->b_hold) == 1) && !list_empty(&bp->b_lru))
1006 __xfs_buf_ioacct_dec(bp);
1007 goto out_unlock;
1010 /* the last reference has been dropped ... */
1011 __xfs_buf_ioacct_dec(bp);
1012 if (!(bp->b_flags & XBF_STALE) && atomic_read(&bp->b_lru_ref)) {
1014 * If the buffer is added to the LRU take a new reference to the
1015 * buffer for the LRU and clear the (now stale) dispose list
1016 * state flag
1018 if (list_lru_add(&bp->b_target->bt_lru, &bp->b_lru)) {
1019 bp->b_state &= ~XFS_BSTATE_DISPOSE;
1020 atomic_inc(&bp->b_hold);
1022 spin_unlock(&pag->pag_buf_lock);
1023 } else {
1025 * most of the time buffers will already be removed from the
1026 * LRU, so optimise that case by checking for the
1027 * XFS_BSTATE_DISPOSE flag indicating the last list the buffer
1028 * was on was the disposal list
1030 if (!(bp->b_state & XFS_BSTATE_DISPOSE)) {
1031 list_lru_del(&bp->b_target->bt_lru, &bp->b_lru);
1032 } else {
1033 ASSERT(list_empty(&bp->b_lru));
1036 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
1037 rb_erase(&bp->b_rbnode, &pag->pag_buf_tree);
1038 spin_unlock(&pag->pag_buf_lock);
1039 xfs_perag_put(pag);
1040 freebuf = true;
1043 out_unlock:
1044 spin_unlock(&bp->b_lock);
1046 if (freebuf)
1047 xfs_buf_free(bp);
1052 * Lock a buffer object, if it is not already locked.
1054 * If we come across a stale, pinned, locked buffer, we know that we are
1055 * being asked to lock a buffer that has been reallocated. Because it is
1056 * pinned, we know that the log has not been pushed to disk and hence it
1057 * will still be locked. Rather than continuing to have trylock attempts
1058 * fail until someone else pushes the log, push it ourselves before
1059 * returning. This means that the xfsaild will not get stuck trying
1060 * to push on stale inode buffers.
1063 xfs_buf_trylock(
1064 struct xfs_buf *bp)
1066 int locked;
1068 locked = down_trylock(&bp->b_sema) == 0;
1069 if (locked) {
1070 XB_SET_OWNER(bp);
1071 trace_xfs_buf_trylock(bp, _RET_IP_);
1072 } else {
1073 trace_xfs_buf_trylock_fail(bp, _RET_IP_);
1075 return locked;
1079 * Lock a buffer object.
1081 * If we come across a stale, pinned, locked buffer, we know that we
1082 * are being asked to lock a buffer that has been reallocated. Because
1083 * it is pinned, we know that the log has not been pushed to disk and
1084 * hence it will still be locked. Rather than sleeping until someone
1085 * else pushes the log, push it ourselves before trying to get the lock.
1087 void
1088 xfs_buf_lock(
1089 struct xfs_buf *bp)
1091 trace_xfs_buf_lock(bp, _RET_IP_);
1093 if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
1094 xfs_log_force(bp->b_target->bt_mount, 0);
1095 down(&bp->b_sema);
1096 XB_SET_OWNER(bp);
1098 trace_xfs_buf_lock_done(bp, _RET_IP_);
1101 void
1102 xfs_buf_unlock(
1103 struct xfs_buf *bp)
1105 ASSERT(xfs_buf_islocked(bp));
1107 XB_CLEAR_OWNER(bp);
1108 up(&bp->b_sema);
1110 trace_xfs_buf_unlock(bp, _RET_IP_);
1113 STATIC void
1114 xfs_buf_wait_unpin(
1115 xfs_buf_t *bp)
1117 DECLARE_WAITQUEUE (wait, current);
1119 if (atomic_read(&bp->b_pin_count) == 0)
1120 return;
1122 add_wait_queue(&bp->b_waiters, &wait);
1123 for (;;) {
1124 set_current_state(TASK_UNINTERRUPTIBLE);
1125 if (atomic_read(&bp->b_pin_count) == 0)
1126 break;
1127 io_schedule();
1129 remove_wait_queue(&bp->b_waiters, &wait);
1130 set_current_state(TASK_RUNNING);
1134 * Buffer Utility Routines
1137 void
1138 xfs_buf_ioend(
1139 struct xfs_buf *bp)
1141 bool read = bp->b_flags & XBF_READ;
1143 trace_xfs_buf_iodone(bp, _RET_IP_);
1145 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
1148 * Pull in IO completion errors now. We are guaranteed to be running
1149 * single threaded, so we don't need the lock to read b_io_error.
1151 if (!bp->b_error && bp->b_io_error)
1152 xfs_buf_ioerror(bp, bp->b_io_error);
1154 /* Only validate buffers that were read without errors */
1155 if (read && !bp->b_error && bp->b_ops) {
1156 ASSERT(!bp->b_iodone);
1157 bp->b_ops->verify_read(bp);
1160 if (!bp->b_error)
1161 bp->b_flags |= XBF_DONE;
1163 if (bp->b_iodone)
1164 (*(bp->b_iodone))(bp);
1165 else if (bp->b_flags & XBF_ASYNC)
1166 xfs_buf_relse(bp);
1167 else
1168 complete(&bp->b_iowait);
1171 static void
1172 xfs_buf_ioend_work(
1173 struct work_struct *work)
1175 struct xfs_buf *bp =
1176 container_of(work, xfs_buf_t, b_ioend_work);
1178 xfs_buf_ioend(bp);
1181 static void
1182 xfs_buf_ioend_async(
1183 struct xfs_buf *bp)
1185 INIT_WORK(&bp->b_ioend_work, xfs_buf_ioend_work);
1186 queue_work(bp->b_ioend_wq, &bp->b_ioend_work);
1189 void
1190 xfs_buf_ioerror(
1191 xfs_buf_t *bp,
1192 int error)
1194 ASSERT(error <= 0 && error >= -1000);
1195 bp->b_error = error;
1196 trace_xfs_buf_ioerror(bp, error, _RET_IP_);
1199 void
1200 xfs_buf_ioerror_alert(
1201 struct xfs_buf *bp,
1202 const char *func)
1204 xfs_alert(bp->b_target->bt_mount,
1205 "metadata I/O error: block 0x%llx (\"%s\") error %d numblks %d",
1206 (__uint64_t)XFS_BUF_ADDR(bp), func, -bp->b_error, bp->b_length);
1210 xfs_bwrite(
1211 struct xfs_buf *bp)
1213 int error;
1215 ASSERT(xfs_buf_islocked(bp));
1217 bp->b_flags |= XBF_WRITE;
1218 bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q |
1219 XBF_WRITE_FAIL | XBF_DONE);
1221 error = xfs_buf_submit_wait(bp);
1222 if (error) {
1223 xfs_force_shutdown(bp->b_target->bt_mount,
1224 SHUTDOWN_META_IO_ERROR);
1226 return error;
1229 static void
1230 xfs_buf_bio_end_io(
1231 struct bio *bio)
1233 struct xfs_buf *bp = (struct xfs_buf *)bio->bi_private;
1236 * don't overwrite existing errors - otherwise we can lose errors on
1237 * buffers that require multiple bios to complete.
1239 if (bio->bi_error)
1240 cmpxchg(&bp->b_io_error, 0, bio->bi_error);
1242 if (!bp->b_error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
1243 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1245 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
1246 xfs_buf_ioend_async(bp);
1247 bio_put(bio);
1250 static void
1251 xfs_buf_ioapply_map(
1252 struct xfs_buf *bp,
1253 int map,
1254 int *buf_offset,
1255 int *count,
1256 int op,
1257 int op_flags)
1259 int page_index;
1260 int total_nr_pages = bp->b_page_count;
1261 int nr_pages;
1262 struct bio *bio;
1263 sector_t sector = bp->b_maps[map].bm_bn;
1264 int size;
1265 int offset;
1267 total_nr_pages = bp->b_page_count;
1269 /* skip the pages in the buffer before the start offset */
1270 page_index = 0;
1271 offset = *buf_offset;
1272 while (offset >= PAGE_SIZE) {
1273 page_index++;
1274 offset -= PAGE_SIZE;
1278 * Limit the IO size to the length of the current vector, and update the
1279 * remaining IO count for the next time around.
1281 size = min_t(int, BBTOB(bp->b_maps[map].bm_len), *count);
1282 *count -= size;
1283 *buf_offset += size;
1285 next_chunk:
1286 atomic_inc(&bp->b_io_remaining);
1287 nr_pages = min(total_nr_pages, BIO_MAX_PAGES);
1289 bio = bio_alloc(GFP_NOIO, nr_pages);
1290 bio->bi_bdev = bp->b_target->bt_bdev;
1291 bio->bi_iter.bi_sector = sector;
1292 bio->bi_end_io = xfs_buf_bio_end_io;
1293 bio->bi_private = bp;
1294 bio_set_op_attrs(bio, op, op_flags);
1296 for (; size && nr_pages; nr_pages--, page_index++) {
1297 int rbytes, nbytes = PAGE_SIZE - offset;
1299 if (nbytes > size)
1300 nbytes = size;
1302 rbytes = bio_add_page(bio, bp->b_pages[page_index], nbytes,
1303 offset);
1304 if (rbytes < nbytes)
1305 break;
1307 offset = 0;
1308 sector += BTOBB(nbytes);
1309 size -= nbytes;
1310 total_nr_pages--;
1313 if (likely(bio->bi_iter.bi_size)) {
1314 if (xfs_buf_is_vmapped(bp)) {
1315 flush_kernel_vmap_range(bp->b_addr,
1316 xfs_buf_vmap_len(bp));
1318 submit_bio(bio);
1319 if (size)
1320 goto next_chunk;
1321 } else {
1323 * This is guaranteed not to be the last io reference count
1324 * because the caller (xfs_buf_submit) holds a count itself.
1326 atomic_dec(&bp->b_io_remaining);
1327 xfs_buf_ioerror(bp, -EIO);
1328 bio_put(bio);
1333 STATIC void
1334 _xfs_buf_ioapply(
1335 struct xfs_buf *bp)
1337 struct blk_plug plug;
1338 int op;
1339 int op_flags = 0;
1340 int offset;
1341 int size;
1342 int i;
1345 * Make sure we capture only current IO errors rather than stale errors
1346 * left over from previous use of the buffer (e.g. failed readahead).
1348 bp->b_error = 0;
1351 * Initialize the I/O completion workqueue if we haven't yet or the
1352 * submitter has not opted to specify a custom one.
1354 if (!bp->b_ioend_wq)
1355 bp->b_ioend_wq = bp->b_target->bt_mount->m_buf_workqueue;
1357 if (bp->b_flags & XBF_WRITE) {
1358 op = REQ_OP_WRITE;
1359 if (bp->b_flags & XBF_SYNCIO)
1360 op_flags = WRITE_SYNC;
1361 if (bp->b_flags & XBF_FUA)
1362 op_flags |= REQ_FUA;
1363 if (bp->b_flags & XBF_FLUSH)
1364 op_flags |= REQ_PREFLUSH;
1367 * Run the write verifier callback function if it exists. If
1368 * this function fails it will mark the buffer with an error and
1369 * the IO should not be dispatched.
1371 if (bp->b_ops) {
1372 bp->b_ops->verify_write(bp);
1373 if (bp->b_error) {
1374 xfs_force_shutdown(bp->b_target->bt_mount,
1375 SHUTDOWN_CORRUPT_INCORE);
1376 return;
1378 } else if (bp->b_bn != XFS_BUF_DADDR_NULL) {
1379 struct xfs_mount *mp = bp->b_target->bt_mount;
1382 * non-crc filesystems don't attach verifiers during
1383 * log recovery, so don't warn for such filesystems.
1385 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1386 xfs_warn(mp,
1387 "%s: no ops on block 0x%llx/0x%x",
1388 __func__, bp->b_bn, bp->b_length);
1389 xfs_hex_dump(bp->b_addr, 64);
1390 dump_stack();
1393 } else if (bp->b_flags & XBF_READ_AHEAD) {
1394 op = REQ_OP_READ;
1395 op_flags = REQ_RAHEAD;
1396 } else {
1397 op = REQ_OP_READ;
1400 /* we only use the buffer cache for meta-data */
1401 op_flags |= REQ_META;
1404 * Walk all the vectors issuing IO on them. Set up the initial offset
1405 * into the buffer and the desired IO size before we start -
1406 * _xfs_buf_ioapply_vec() will modify them appropriately for each
1407 * subsequent call.
1409 offset = bp->b_offset;
1410 size = BBTOB(bp->b_io_length);
1411 blk_start_plug(&plug);
1412 for (i = 0; i < bp->b_map_count; i++) {
1413 xfs_buf_ioapply_map(bp, i, &offset, &size, op, op_flags);
1414 if (bp->b_error)
1415 break;
1416 if (size <= 0)
1417 break; /* all done */
1419 blk_finish_plug(&plug);
1423 * Asynchronous IO submission path. This transfers the buffer lock ownership and
1424 * the current reference to the IO. It is not safe to reference the buffer after
1425 * a call to this function unless the caller holds an additional reference
1426 * itself.
1428 void
1429 xfs_buf_submit(
1430 struct xfs_buf *bp)
1432 trace_xfs_buf_submit(bp, _RET_IP_);
1434 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
1435 ASSERT(bp->b_flags & XBF_ASYNC);
1437 /* on shutdown we stale and complete the buffer immediately */
1438 if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
1439 xfs_buf_ioerror(bp, -EIO);
1440 bp->b_flags &= ~XBF_DONE;
1441 xfs_buf_stale(bp);
1442 xfs_buf_ioend(bp);
1443 return;
1446 if (bp->b_flags & XBF_WRITE)
1447 xfs_buf_wait_unpin(bp);
1449 /* clear the internal error state to avoid spurious errors */
1450 bp->b_io_error = 0;
1453 * The caller's reference is released during I/O completion.
1454 * This occurs some time after the last b_io_remaining reference is
1455 * released, so after we drop our Io reference we have to have some
1456 * other reference to ensure the buffer doesn't go away from underneath
1457 * us. Take a direct reference to ensure we have safe access to the
1458 * buffer until we are finished with it.
1460 xfs_buf_hold(bp);
1463 * Set the count to 1 initially, this will stop an I/O completion
1464 * callout which happens before we have started all the I/O from calling
1465 * xfs_buf_ioend too early.
1467 atomic_set(&bp->b_io_remaining, 1);
1468 xfs_buf_ioacct_inc(bp);
1469 _xfs_buf_ioapply(bp);
1472 * If _xfs_buf_ioapply failed, we can get back here with only the IO
1473 * reference we took above. If we drop it to zero, run completion so
1474 * that we don't return to the caller with completion still pending.
1476 if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
1477 if (bp->b_error)
1478 xfs_buf_ioend(bp);
1479 else
1480 xfs_buf_ioend_async(bp);
1483 xfs_buf_rele(bp);
1484 /* Note: it is not safe to reference bp now we've dropped our ref */
1488 * Synchronous buffer IO submission path, read or write.
1491 xfs_buf_submit_wait(
1492 struct xfs_buf *bp)
1494 int error;
1496 trace_xfs_buf_submit_wait(bp, _RET_IP_);
1498 ASSERT(!(bp->b_flags & (_XBF_DELWRI_Q | XBF_ASYNC)));
1500 if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
1501 xfs_buf_ioerror(bp, -EIO);
1502 xfs_buf_stale(bp);
1503 bp->b_flags &= ~XBF_DONE;
1504 return -EIO;
1507 if (bp->b_flags & XBF_WRITE)
1508 xfs_buf_wait_unpin(bp);
1510 /* clear the internal error state to avoid spurious errors */
1511 bp->b_io_error = 0;
1514 * For synchronous IO, the IO does not inherit the submitters reference
1515 * count, nor the buffer lock. Hence we cannot release the reference we
1516 * are about to take until we've waited for all IO completion to occur,
1517 * including any xfs_buf_ioend_async() work that may be pending.
1519 xfs_buf_hold(bp);
1522 * Set the count to 1 initially, this will stop an I/O completion
1523 * callout which happens before we have started all the I/O from calling
1524 * xfs_buf_ioend too early.
1526 atomic_set(&bp->b_io_remaining, 1);
1527 _xfs_buf_ioapply(bp);
1530 * make sure we run completion synchronously if it raced with us and is
1531 * already complete.
1533 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
1534 xfs_buf_ioend(bp);
1536 /* wait for completion before gathering the error from the buffer */
1537 trace_xfs_buf_iowait(bp, _RET_IP_);
1538 wait_for_completion(&bp->b_iowait);
1539 trace_xfs_buf_iowait_done(bp, _RET_IP_);
1540 error = bp->b_error;
1543 * all done now, we can release the hold that keeps the buffer
1544 * referenced for the entire IO.
1546 xfs_buf_rele(bp);
1547 return error;
1550 void *
1551 xfs_buf_offset(
1552 struct xfs_buf *bp,
1553 size_t offset)
1555 struct page *page;
1557 if (bp->b_addr)
1558 return bp->b_addr + offset;
1560 offset += bp->b_offset;
1561 page = bp->b_pages[offset >> PAGE_SHIFT];
1562 return page_address(page) + (offset & (PAGE_SIZE-1));
1566 * Move data into or out of a buffer.
1568 void
1569 xfs_buf_iomove(
1570 xfs_buf_t *bp, /* buffer to process */
1571 size_t boff, /* starting buffer offset */
1572 size_t bsize, /* length to copy */
1573 void *data, /* data address */
1574 xfs_buf_rw_t mode) /* read/write/zero flag */
1576 size_t bend;
1578 bend = boff + bsize;
1579 while (boff < bend) {
1580 struct page *page;
1581 int page_index, page_offset, csize;
1583 page_index = (boff + bp->b_offset) >> PAGE_SHIFT;
1584 page_offset = (boff + bp->b_offset) & ~PAGE_MASK;
1585 page = bp->b_pages[page_index];
1586 csize = min_t(size_t, PAGE_SIZE - page_offset,
1587 BBTOB(bp->b_io_length) - boff);
1589 ASSERT((csize + page_offset) <= PAGE_SIZE);
1591 switch (mode) {
1592 case XBRW_ZERO:
1593 memset(page_address(page) + page_offset, 0, csize);
1594 break;
1595 case XBRW_READ:
1596 memcpy(data, page_address(page) + page_offset, csize);
1597 break;
1598 case XBRW_WRITE:
1599 memcpy(page_address(page) + page_offset, data, csize);
1602 boff += csize;
1603 data += csize;
1608 * Handling of buffer targets (buftargs).
1612 * Wait for any bufs with callbacks that have been submitted but have not yet
1613 * returned. These buffers will have an elevated hold count, so wait on those
1614 * while freeing all the buffers only held by the LRU.
1616 static enum lru_status
1617 xfs_buftarg_wait_rele(
1618 struct list_head *item,
1619 struct list_lru_one *lru,
1620 spinlock_t *lru_lock,
1621 void *arg)
1624 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
1625 struct list_head *dispose = arg;
1627 if (atomic_read(&bp->b_hold) > 1) {
1628 /* need to wait, so skip it this pass */
1629 trace_xfs_buf_wait_buftarg(bp, _RET_IP_);
1630 return LRU_SKIP;
1632 if (!spin_trylock(&bp->b_lock))
1633 return LRU_SKIP;
1636 * clear the LRU reference count so the buffer doesn't get
1637 * ignored in xfs_buf_rele().
1639 atomic_set(&bp->b_lru_ref, 0);
1640 bp->b_state |= XFS_BSTATE_DISPOSE;
1641 list_lru_isolate_move(lru, item, dispose);
1642 spin_unlock(&bp->b_lock);
1643 return LRU_REMOVED;
1646 void
1647 xfs_wait_buftarg(
1648 struct xfs_buftarg *btp)
1650 LIST_HEAD(dispose);
1651 int loop = 0;
1654 * First wait on the buftarg I/O count for all in-flight buffers to be
1655 * released. This is critical as new buffers do not make the LRU until
1656 * they are released.
1658 * Next, flush the buffer workqueue to ensure all completion processing
1659 * has finished. Just waiting on buffer locks is not sufficient for
1660 * async IO as the reference count held over IO is not released until
1661 * after the buffer lock is dropped. Hence we need to ensure here that
1662 * all reference counts have been dropped before we start walking the
1663 * LRU list.
1665 while (percpu_counter_sum(&btp->bt_io_count))
1666 delay(100);
1667 flush_workqueue(btp->bt_mount->m_buf_workqueue);
1669 /* loop until there is nothing left on the lru list. */
1670 while (list_lru_count(&btp->bt_lru)) {
1671 list_lru_walk(&btp->bt_lru, xfs_buftarg_wait_rele,
1672 &dispose, LONG_MAX);
1674 while (!list_empty(&dispose)) {
1675 struct xfs_buf *bp;
1676 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1677 list_del_init(&bp->b_lru);
1678 if (bp->b_flags & XBF_WRITE_FAIL) {
1679 xfs_alert(btp->bt_mount,
1680 "Corruption Alert: Buffer at block 0x%llx had permanent write failures!",
1681 (long long)bp->b_bn);
1682 xfs_alert(btp->bt_mount,
1683 "Please run xfs_repair to determine the extent of the problem.");
1685 xfs_buf_rele(bp);
1687 if (loop++ != 0)
1688 delay(100);
1692 static enum lru_status
1693 xfs_buftarg_isolate(
1694 struct list_head *item,
1695 struct list_lru_one *lru,
1696 spinlock_t *lru_lock,
1697 void *arg)
1699 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
1700 struct list_head *dispose = arg;
1703 * we are inverting the lru lock/bp->b_lock here, so use a trylock.
1704 * If we fail to get the lock, just skip it.
1706 if (!spin_trylock(&bp->b_lock))
1707 return LRU_SKIP;
1709 * Decrement the b_lru_ref count unless the value is already
1710 * zero. If the value is already zero, we need to reclaim the
1711 * buffer, otherwise it gets another trip through the LRU.
1713 if (atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
1714 spin_unlock(&bp->b_lock);
1715 return LRU_ROTATE;
1718 bp->b_state |= XFS_BSTATE_DISPOSE;
1719 list_lru_isolate_move(lru, item, dispose);
1720 spin_unlock(&bp->b_lock);
1721 return LRU_REMOVED;
1724 static unsigned long
1725 xfs_buftarg_shrink_scan(
1726 struct shrinker *shrink,
1727 struct shrink_control *sc)
1729 struct xfs_buftarg *btp = container_of(shrink,
1730 struct xfs_buftarg, bt_shrinker);
1731 LIST_HEAD(dispose);
1732 unsigned long freed;
1734 freed = list_lru_shrink_walk(&btp->bt_lru, sc,
1735 xfs_buftarg_isolate, &dispose);
1737 while (!list_empty(&dispose)) {
1738 struct xfs_buf *bp;
1739 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1740 list_del_init(&bp->b_lru);
1741 xfs_buf_rele(bp);
1744 return freed;
1747 static unsigned long
1748 xfs_buftarg_shrink_count(
1749 struct shrinker *shrink,
1750 struct shrink_control *sc)
1752 struct xfs_buftarg *btp = container_of(shrink,
1753 struct xfs_buftarg, bt_shrinker);
1754 return list_lru_shrink_count(&btp->bt_lru, sc);
1757 void
1758 xfs_free_buftarg(
1759 struct xfs_mount *mp,
1760 struct xfs_buftarg *btp)
1762 unregister_shrinker(&btp->bt_shrinker);
1763 ASSERT(percpu_counter_sum(&btp->bt_io_count) == 0);
1764 percpu_counter_destroy(&btp->bt_io_count);
1765 list_lru_destroy(&btp->bt_lru);
1767 if (mp->m_flags & XFS_MOUNT_BARRIER)
1768 xfs_blkdev_issue_flush(btp);
1770 kmem_free(btp);
1774 xfs_setsize_buftarg(
1775 xfs_buftarg_t *btp,
1776 unsigned int sectorsize)
1778 /* Set up metadata sector size info */
1779 btp->bt_meta_sectorsize = sectorsize;
1780 btp->bt_meta_sectormask = sectorsize - 1;
1782 if (set_blocksize(btp->bt_bdev, sectorsize)) {
1783 xfs_warn(btp->bt_mount,
1784 "Cannot set_blocksize to %u on device %pg",
1785 sectorsize, btp->bt_bdev);
1786 return -EINVAL;
1789 /* Set up device logical sector size mask */
1790 btp->bt_logical_sectorsize = bdev_logical_block_size(btp->bt_bdev);
1791 btp->bt_logical_sectormask = bdev_logical_block_size(btp->bt_bdev) - 1;
1793 return 0;
1797 * When allocating the initial buffer target we have not yet
1798 * read in the superblock, so don't know what sized sectors
1799 * are being used at this early stage. Play safe.
1801 STATIC int
1802 xfs_setsize_buftarg_early(
1803 xfs_buftarg_t *btp,
1804 struct block_device *bdev)
1806 return xfs_setsize_buftarg(btp, bdev_logical_block_size(bdev));
1809 xfs_buftarg_t *
1810 xfs_alloc_buftarg(
1811 struct xfs_mount *mp,
1812 struct block_device *bdev)
1814 xfs_buftarg_t *btp;
1816 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP | KM_NOFS);
1818 btp->bt_mount = mp;
1819 btp->bt_dev = bdev->bd_dev;
1820 btp->bt_bdev = bdev;
1821 btp->bt_bdi = blk_get_backing_dev_info(bdev);
1823 if (xfs_setsize_buftarg_early(btp, bdev))
1824 goto error_free;
1826 if (list_lru_init(&btp->bt_lru))
1827 goto error_free;
1829 if (percpu_counter_init(&btp->bt_io_count, 0, GFP_KERNEL))
1830 goto error_lru;
1832 btp->bt_shrinker.count_objects = xfs_buftarg_shrink_count;
1833 btp->bt_shrinker.scan_objects = xfs_buftarg_shrink_scan;
1834 btp->bt_shrinker.seeks = DEFAULT_SEEKS;
1835 btp->bt_shrinker.flags = SHRINKER_NUMA_AWARE;
1836 if (register_shrinker(&btp->bt_shrinker))
1837 goto error_pcpu;
1838 return btp;
1840 error_pcpu:
1841 percpu_counter_destroy(&btp->bt_io_count);
1842 error_lru:
1843 list_lru_destroy(&btp->bt_lru);
1844 error_free:
1845 kmem_free(btp);
1846 return NULL;
1850 * Cancel a delayed write list.
1852 * Remove each buffer from the list, clear the delwri queue flag and drop the
1853 * associated buffer reference.
1855 void
1856 xfs_buf_delwri_cancel(
1857 struct list_head *list)
1859 struct xfs_buf *bp;
1861 while (!list_empty(list)) {
1862 bp = list_first_entry(list, struct xfs_buf, b_list);
1864 xfs_buf_lock(bp);
1865 bp->b_flags &= ~_XBF_DELWRI_Q;
1866 list_del_init(&bp->b_list);
1867 xfs_buf_relse(bp);
1872 * Add a buffer to the delayed write list.
1874 * This queues a buffer for writeout if it hasn't already been. Note that
1875 * neither this routine nor the buffer list submission functions perform
1876 * any internal synchronization. It is expected that the lists are thread-local
1877 * to the callers.
1879 * Returns true if we queued up the buffer, or false if it already had
1880 * been on the buffer list.
1882 bool
1883 xfs_buf_delwri_queue(
1884 struct xfs_buf *bp,
1885 struct list_head *list)
1887 ASSERT(xfs_buf_islocked(bp));
1888 ASSERT(!(bp->b_flags & XBF_READ));
1891 * If the buffer is already marked delwri it already is queued up
1892 * by someone else for imediate writeout. Just ignore it in that
1893 * case.
1895 if (bp->b_flags & _XBF_DELWRI_Q) {
1896 trace_xfs_buf_delwri_queued(bp, _RET_IP_);
1897 return false;
1900 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
1903 * If a buffer gets written out synchronously or marked stale while it
1904 * is on a delwri list we lazily remove it. To do this, the other party
1905 * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
1906 * It remains referenced and on the list. In a rare corner case it
1907 * might get readded to a delwri list after the synchronous writeout, in
1908 * which case we need just need to re-add the flag here.
1910 bp->b_flags |= _XBF_DELWRI_Q;
1911 if (list_empty(&bp->b_list)) {
1912 atomic_inc(&bp->b_hold);
1913 list_add_tail(&bp->b_list, list);
1916 return true;
1920 * Compare function is more complex than it needs to be because
1921 * the return value is only 32 bits and we are doing comparisons
1922 * on 64 bit values
1924 static int
1925 xfs_buf_cmp(
1926 void *priv,
1927 struct list_head *a,
1928 struct list_head *b)
1930 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
1931 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
1932 xfs_daddr_t diff;
1934 diff = ap->b_maps[0].bm_bn - bp->b_maps[0].bm_bn;
1935 if (diff < 0)
1936 return -1;
1937 if (diff > 0)
1938 return 1;
1939 return 0;
1943 * submit buffers for write.
1945 * When we have a large buffer list, we do not want to hold all the buffers
1946 * locked while we block on the request queue waiting for IO dispatch. To avoid
1947 * this problem, we lock and submit buffers in groups of 50, thereby minimising
1948 * the lock hold times for lists which may contain thousands of objects.
1950 * To do this, we sort the buffer list before we walk the list to lock and
1951 * submit buffers, and we plug and unplug around each group of buffers we
1952 * submit.
1954 static int
1955 xfs_buf_delwri_submit_buffers(
1956 struct list_head *buffer_list,
1957 struct list_head *wait_list)
1959 struct xfs_buf *bp, *n;
1960 LIST_HEAD (submit_list);
1961 int pinned = 0;
1962 struct blk_plug plug;
1964 list_sort(NULL, buffer_list, xfs_buf_cmp);
1966 blk_start_plug(&plug);
1967 list_for_each_entry_safe(bp, n, buffer_list, b_list) {
1968 if (!wait_list) {
1969 if (xfs_buf_ispinned(bp)) {
1970 pinned++;
1971 continue;
1973 if (!xfs_buf_trylock(bp))
1974 continue;
1975 } else {
1976 xfs_buf_lock(bp);
1980 * Someone else might have written the buffer synchronously or
1981 * marked it stale in the meantime. In that case only the
1982 * _XBF_DELWRI_Q flag got cleared, and we have to drop the
1983 * reference and remove it from the list here.
1985 if (!(bp->b_flags & _XBF_DELWRI_Q)) {
1986 list_del_init(&bp->b_list);
1987 xfs_buf_relse(bp);
1988 continue;
1991 trace_xfs_buf_delwri_split(bp, _RET_IP_);
1994 * We do all IO submission async. This means if we need
1995 * to wait for IO completion we need to take an extra
1996 * reference so the buffer is still valid on the other
1997 * side. We need to move the buffer onto the io_list
1998 * at this point so the caller can still access it.
2000 bp->b_flags &= ~(_XBF_DELWRI_Q | XBF_WRITE_FAIL);
2001 bp->b_flags |= XBF_WRITE | XBF_ASYNC;
2002 if (wait_list) {
2003 xfs_buf_hold(bp);
2004 list_move_tail(&bp->b_list, wait_list);
2005 } else
2006 list_del_init(&bp->b_list);
2008 xfs_buf_submit(bp);
2010 blk_finish_plug(&plug);
2012 return pinned;
2016 * Write out a buffer list asynchronously.
2018 * This will take the @buffer_list, write all non-locked and non-pinned buffers
2019 * out and not wait for I/O completion on any of the buffers. This interface
2020 * is only safely useable for callers that can track I/O completion by higher
2021 * level means, e.g. AIL pushing as the @buffer_list is consumed in this
2022 * function.
2025 xfs_buf_delwri_submit_nowait(
2026 struct list_head *buffer_list)
2028 return xfs_buf_delwri_submit_buffers(buffer_list, NULL);
2032 * Write out a buffer list synchronously.
2034 * This will take the @buffer_list, write all buffers out and wait for I/O
2035 * completion on all of the buffers. @buffer_list is consumed by the function,
2036 * so callers must have some other way of tracking buffers if they require such
2037 * functionality.
2040 xfs_buf_delwri_submit(
2041 struct list_head *buffer_list)
2043 LIST_HEAD (wait_list);
2044 int error = 0, error2;
2045 struct xfs_buf *bp;
2047 xfs_buf_delwri_submit_buffers(buffer_list, &wait_list);
2049 /* Wait for IO to complete. */
2050 while (!list_empty(&wait_list)) {
2051 bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
2053 list_del_init(&bp->b_list);
2055 /* locking the buffer will wait for async IO completion. */
2056 xfs_buf_lock(bp);
2057 error2 = bp->b_error;
2058 xfs_buf_relse(bp);
2059 if (!error)
2060 error = error2;
2063 return error;
2067 * Push a single buffer on a delwri queue.
2069 * The purpose of this function is to submit a single buffer of a delwri queue
2070 * and return with the buffer still on the original queue. The waiting delwri
2071 * buffer submission infrastructure guarantees transfer of the delwri queue
2072 * buffer reference to a temporary wait list. We reuse this infrastructure to
2073 * transfer the buffer back to the original queue.
2075 * Note the buffer transitions from the queued state, to the submitted and wait
2076 * listed state and back to the queued state during this call. The buffer
2077 * locking and queue management logic between _delwri_pushbuf() and
2078 * _delwri_queue() guarantee that the buffer cannot be queued to another list
2079 * before returning.
2082 xfs_buf_delwri_pushbuf(
2083 struct xfs_buf *bp,
2084 struct list_head *buffer_list)
2086 LIST_HEAD (submit_list);
2087 int error;
2089 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
2091 trace_xfs_buf_delwri_pushbuf(bp, _RET_IP_);
2094 * Isolate the buffer to a new local list so we can submit it for I/O
2095 * independently from the rest of the original list.
2097 xfs_buf_lock(bp);
2098 list_move(&bp->b_list, &submit_list);
2099 xfs_buf_unlock(bp);
2102 * Delwri submission clears the DELWRI_Q buffer flag and returns with
2103 * the buffer on the wait list with an associated reference. Rather than
2104 * bounce the buffer from a local wait list back to the original list
2105 * after I/O completion, reuse the original list as the wait list.
2107 xfs_buf_delwri_submit_buffers(&submit_list, buffer_list);
2110 * The buffer is now under I/O and wait listed as during typical delwri
2111 * submission. Lock the buffer to wait for I/O completion. Rather than
2112 * remove the buffer from the wait list and release the reference, we
2113 * want to return with the buffer queued to the original list. The
2114 * buffer already sits on the original list with a wait list reference,
2115 * however. If we let the queue inherit that wait list reference, all we
2116 * need to do is reset the DELWRI_Q flag.
2118 xfs_buf_lock(bp);
2119 error = bp->b_error;
2120 bp->b_flags |= _XBF_DELWRI_Q;
2121 xfs_buf_unlock(bp);
2123 return error;
2126 int __init
2127 xfs_buf_init(void)
2129 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
2130 KM_ZONE_HWALIGN, NULL);
2131 if (!xfs_buf_zone)
2132 goto out;
2134 return 0;
2136 out:
2137 return -ENOMEM;
2140 void
2141 xfs_buf_terminate(void)
2143 kmem_zone_destroy(xfs_buf_zone);