2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
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
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>
36 #include <linux/list_sort.h>
42 #include "xfs_mount.h"
43 #include "xfs_trace.h"
45 static kmem_zone_t *xfs_buf_zone;
46 STATIC int xfsbufd(void *);
47 STATIC void xfs_buf_delwri_queue(xfs_buf_t *, int);
49 static struct workqueue_struct *xfslogd_workqueue;
50 struct workqueue_struct *xfsdatad_workqueue;
51 struct workqueue_struct *xfsconvertd_workqueue;
53 #ifdef XFS_BUF_LOCK_TRACKING
54 # define XB_SET_OWNER(bp) ((bp)->b_last_holder = current->pid)
55 # define XB_CLEAR_OWNER(bp) ((bp)->b_last_holder = -1)
56 # define XB_GET_OWNER(bp) ((bp)->b_last_holder)
58 # define XB_SET_OWNER(bp) do { } while (0)
59 # define XB_CLEAR_OWNER(bp) do { } while (0)
60 # define XB_GET_OWNER(bp) do { } while (0)
63 #define xb_to_gfp(flags) \
64 ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : \
65 ((flags) & XBF_DONT_BLOCK) ? GFP_NOFS : GFP_KERNEL) | __GFP_NOWARN)
67 #define xb_to_km(flags) \
68 (((flags) & XBF_DONT_BLOCK) ? KM_NOFS : KM_SLEEP)
70 #define xfs_buf_allocate(flags) \
71 kmem_zone_alloc(xfs_buf_zone, xb_to_km(flags))
72 #define xfs_buf_deallocate(bp) \
73 kmem_zone_free(xfs_buf_zone, (bp));
80 * Return true if the buffer is vmapped.
82 * The XBF_MAPPED flag is set if the buffer should be mapped, but the
83 * code is clever enough to know it doesn't have to map a single page,
84 * so the check has to be both for XBF_MAPPED and bp->b_page_count > 1.
86 return (bp->b_flags & XBF_MAPPED) && bp->b_page_count > 1;
93 return (bp->b_page_count * PAGE_SIZE) - bp->b_offset;
97 * xfs_buf_lru_add - add a buffer to the LRU.
99 * The LRU takes a new reference to the buffer so that it will only be freed
100 * once the shrinker takes the buffer off the LRU.
106 struct xfs_buftarg *btp = bp->b_target;
108 spin_lock(&btp->bt_lru_lock);
109 if (list_empty(&bp->b_lru)) {
110 atomic_inc(&bp->b_hold);
111 list_add_tail(&bp->b_lru, &btp->bt_lru);
114 spin_unlock(&btp->bt_lru_lock);
118 * xfs_buf_lru_del - remove a buffer from the LRU
120 * The unlocked check is safe here because it only occurs when there are not
121 * b_lru_ref counts left on the inode under the pag->pag_buf_lock. it is there
122 * to optimise the shrinker removing the buffer from the LRU and calling
123 * xfs_buf_free(). i.e. it removes an unneccessary round trip on the
130 struct xfs_buftarg *btp = bp->b_target;
132 if (list_empty(&bp->b_lru))
135 spin_lock(&btp->bt_lru_lock);
136 if (!list_empty(&bp->b_lru)) {
137 list_del_init(&bp->b_lru);
140 spin_unlock(&btp->bt_lru_lock);
144 * When we mark a buffer stale, we remove the buffer from the LRU and clear the
145 * b_lru_ref count so that the buffer is freed immediately when the buffer
146 * reference count falls to zero. If the buffer is already on the LRU, we need
147 * to remove the reference that LRU holds on the buffer.
149 * This prevents build-up of stale buffers on the LRU.
155 bp->b_flags |= XBF_STALE;
156 atomic_set(&(bp)->b_lru_ref, 0);
157 if (!list_empty(&bp->b_lru)) {
158 struct xfs_buftarg *btp = bp->b_target;
160 spin_lock(&btp->bt_lru_lock);
161 if (!list_empty(&bp->b_lru)) {
162 list_del_init(&bp->b_lru);
164 atomic_dec(&bp->b_hold);
166 spin_unlock(&btp->bt_lru_lock);
168 ASSERT(atomic_read(&bp->b_hold) >= 1);
174 xfs_buftarg_t *target,
175 xfs_off_t range_base,
177 xfs_buf_flags_t flags)
180 * We don't want certain flags to appear in b_flags.
182 flags &= ~(XBF_LOCK|XBF_MAPPED|XBF_DONT_BLOCK|XBF_READ_AHEAD);
184 memset(bp, 0, sizeof(xfs_buf_t));
185 atomic_set(&bp->b_hold, 1);
186 atomic_set(&bp->b_lru_ref, 1);
187 init_completion(&bp->b_iowait);
188 INIT_LIST_HEAD(&bp->b_lru);
189 INIT_LIST_HEAD(&bp->b_list);
190 RB_CLEAR_NODE(&bp->b_rbnode);
191 sema_init(&bp->b_sema, 0); /* held, no waiters */
193 bp->b_target = target;
194 bp->b_file_offset = range_base;
196 * Set buffer_length and count_desired to the same value initially.
197 * I/O routines should use count_desired, which will be the same in
198 * most cases but may be reset (e.g. XFS recovery).
200 bp->b_buffer_length = bp->b_count_desired = range_length;
202 bp->b_bn = XFS_BUF_DADDR_NULL;
203 atomic_set(&bp->b_pin_count, 0);
204 init_waitqueue_head(&bp->b_waiters);
206 XFS_STATS_INC(xb_create);
208 trace_xfs_buf_init(bp, _RET_IP_);
212 * Allocate a page array capable of holding a specified number
213 * of pages, and point the page buf at it.
219 xfs_buf_flags_t flags)
221 /* Make sure that we have a page list */
222 if (bp->b_pages == NULL) {
223 bp->b_offset = xfs_buf_poff(bp->b_file_offset);
224 bp->b_page_count = page_count;
225 if (page_count <= XB_PAGES) {
226 bp->b_pages = bp->b_page_array;
228 bp->b_pages = kmem_alloc(sizeof(struct page *) *
229 page_count, xb_to_km(flags));
230 if (bp->b_pages == NULL)
233 memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
239 * Frees b_pages if it was allocated.
245 if (bp->b_pages != bp->b_page_array) {
246 kmem_free(bp->b_pages);
252 * Releases the specified buffer.
254 * The modification state of any associated pages is left unchanged.
255 * The buffer most not be on any hash - use xfs_buf_rele instead for
256 * hashed and refcounted buffers
262 trace_xfs_buf_free(bp, _RET_IP_);
264 ASSERT(list_empty(&bp->b_lru));
266 if (bp->b_flags & _XBF_PAGES) {
269 if (xfs_buf_is_vmapped(bp))
270 vm_unmap_ram(bp->b_addr - bp->b_offset,
273 for (i = 0; i < bp->b_page_count; i++) {
274 struct page *page = bp->b_pages[i];
278 } else if (bp->b_flags & _XBF_KMEM)
279 kmem_free(bp->b_addr);
280 _xfs_buf_free_pages(bp);
281 xfs_buf_deallocate(bp);
285 * Allocates all the pages for buffer in question and builds it's page list.
288 xfs_buf_allocate_memory(
292 size_t size = bp->b_count_desired;
293 size_t nbytes, offset;
294 gfp_t gfp_mask = xb_to_gfp(flags);
295 unsigned short page_count, i;
300 * for buffers that are contained within a single page, just allocate
301 * the memory from the heap - there's no need for the complexity of
302 * page arrays to keep allocation down to order 0.
304 if (bp->b_buffer_length < PAGE_SIZE) {
305 bp->b_addr = kmem_alloc(bp->b_buffer_length, xb_to_km(flags));
307 /* low memory - use alloc_page loop instead */
311 if (((unsigned long)(bp->b_addr + bp->b_buffer_length - 1) &
313 ((unsigned long)bp->b_addr & PAGE_MASK)) {
314 /* b_addr spans two pages - use alloc_page instead */
315 kmem_free(bp->b_addr);
319 bp->b_offset = offset_in_page(bp->b_addr);
320 bp->b_pages = bp->b_page_array;
321 bp->b_pages[0] = virt_to_page(bp->b_addr);
322 bp->b_page_count = 1;
323 bp->b_flags |= XBF_MAPPED | _XBF_KMEM;
328 end = bp->b_file_offset + bp->b_buffer_length;
329 page_count = xfs_buf_btoc(end) - xfs_buf_btoct(bp->b_file_offset);
330 error = _xfs_buf_get_pages(bp, page_count, flags);
334 offset = bp->b_offset;
335 bp->b_flags |= _XBF_PAGES;
337 for (i = 0; i < bp->b_page_count; i++) {
341 page = alloc_page(gfp_mask);
342 if (unlikely(page == NULL)) {
343 if (flags & XBF_READ_AHEAD) {
344 bp->b_page_count = i;
350 * This could deadlock.
352 * But until all the XFS lowlevel code is revamped to
353 * handle buffer allocation failures we can't do much.
355 if (!(++retries % 100))
357 "possible memory allocation deadlock in %s (mode:0x%x)",
360 XFS_STATS_INC(xb_page_retries);
361 congestion_wait(BLK_RW_ASYNC, HZ/50);
365 XFS_STATS_INC(xb_page_found);
367 nbytes = min_t(size_t, size, PAGE_SIZE - offset);
369 bp->b_pages[i] = page;
375 for (i = 0; i < bp->b_page_count; i++)
376 __free_page(bp->b_pages[i]);
381 * Map buffer into kernel address-space if nessecary.
388 ASSERT(bp->b_flags & _XBF_PAGES);
389 if (bp->b_page_count == 1) {
390 /* A single page buffer is always mappable */
391 bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
392 bp->b_flags |= XBF_MAPPED;
393 } else if (flags & XBF_MAPPED) {
397 bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
402 } while (retried++ <= 1);
406 bp->b_addr += bp->b_offset;
407 bp->b_flags |= XBF_MAPPED;
414 * Finding and Reading Buffers
418 * Look up, and creates if absent, a lockable buffer for
419 * a given range of an inode. The buffer is returned
420 * locked. If other overlapping buffers exist, they are
421 * released before the new buffer is created and locked,
422 * which may imply that this call will block until those buffers
423 * are unlocked. No I/O is implied by this call.
427 xfs_buftarg_t *btp, /* block device target */
428 xfs_off_t ioff, /* starting offset of range */
429 size_t isize, /* length of range */
430 xfs_buf_flags_t flags,
433 xfs_off_t range_base;
435 struct xfs_perag *pag;
436 struct rb_node **rbp;
437 struct rb_node *parent;
440 range_base = (ioff << BBSHIFT);
441 range_length = (isize << BBSHIFT);
443 /* Check for IOs smaller than the sector size / not sector aligned */
444 ASSERT(!(range_length < (1 << btp->bt_sshift)));
445 ASSERT(!(range_base & (xfs_off_t)btp->bt_smask));
448 pag = xfs_perag_get(btp->bt_mount,
449 xfs_daddr_to_agno(btp->bt_mount, ioff));
452 spin_lock(&pag->pag_buf_lock);
453 rbp = &pag->pag_buf_tree.rb_node;
458 bp = rb_entry(parent, struct xfs_buf, b_rbnode);
460 if (range_base < bp->b_file_offset)
461 rbp = &(*rbp)->rb_left;
462 else if (range_base > bp->b_file_offset)
463 rbp = &(*rbp)->rb_right;
466 * found a block offset match. If the range doesn't
467 * match, the only way this is allowed is if the buffer
468 * in the cache is stale and the transaction that made
469 * it stale has not yet committed. i.e. we are
470 * reallocating a busy extent. Skip this buffer and
471 * continue searching to the right for an exact match.
473 if (bp->b_buffer_length != range_length) {
474 ASSERT(bp->b_flags & XBF_STALE);
475 rbp = &(*rbp)->rb_right;
478 atomic_inc(&bp->b_hold);
485 _xfs_buf_initialize(new_bp, btp, range_base,
486 range_length, flags);
487 rb_link_node(&new_bp->b_rbnode, parent, rbp);
488 rb_insert_color(&new_bp->b_rbnode, &pag->pag_buf_tree);
489 /* the buffer keeps the perag reference until it is freed */
491 spin_unlock(&pag->pag_buf_lock);
493 XFS_STATS_INC(xb_miss_locked);
494 spin_unlock(&pag->pag_buf_lock);
500 spin_unlock(&pag->pag_buf_lock);
503 if (xfs_buf_cond_lock(bp)) {
504 /* failed, so wait for the lock if requested. */
505 if (!(flags & XBF_TRYLOCK)) {
507 XFS_STATS_INC(xb_get_locked_waited);
510 XFS_STATS_INC(xb_busy_locked);
516 * if the buffer is stale, clear all the external state associated with
517 * it. We need to keep flags such as how we allocated the buffer memory
520 if (bp->b_flags & XBF_STALE) {
521 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
522 bp->b_flags &= XBF_MAPPED | _XBF_KMEM | _XBF_PAGES;
525 trace_xfs_buf_find(bp, flags, _RET_IP_);
526 XFS_STATS_INC(xb_get_locked);
531 * Assembles a buffer covering the specified range.
532 * Storage in memory for all portions of the buffer will be allocated,
533 * although backing storage may not be.
537 xfs_buftarg_t *target,/* target for buffer */
538 xfs_off_t ioff, /* starting offset of range */
539 size_t isize, /* length of range */
540 xfs_buf_flags_t flags)
542 xfs_buf_t *bp, *new_bp;
545 new_bp = xfs_buf_allocate(flags);
546 if (unlikely(!new_bp))
549 bp = _xfs_buf_find(target, ioff, isize, flags, new_bp);
551 error = xfs_buf_allocate_memory(bp, flags);
555 xfs_buf_deallocate(new_bp);
556 if (unlikely(bp == NULL))
560 if (!(bp->b_flags & XBF_MAPPED)) {
561 error = _xfs_buf_map_pages(bp, flags);
562 if (unlikely(error)) {
563 xfs_warn(target->bt_mount,
564 "%s: failed to map pages\n", __func__);
569 XFS_STATS_INC(xb_get);
572 * Always fill in the block number now, the mapped cases can do
573 * their own overlay of this later.
576 bp->b_count_desired = bp->b_buffer_length;
578 trace_xfs_buf_get(bp, flags, _RET_IP_);
582 if (flags & (XBF_LOCK | XBF_TRYLOCK))
591 xfs_buf_flags_t flags)
595 ASSERT(!(flags & (XBF_DELWRI|XBF_WRITE)));
596 ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL);
598 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_DELWRI | \
599 XBF_READ_AHEAD | _XBF_RUN_QUEUES);
600 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | \
601 XBF_READ_AHEAD | _XBF_RUN_QUEUES);
603 status = xfs_buf_iorequest(bp);
604 if (status || XFS_BUF_ISERROR(bp) || (flags & XBF_ASYNC))
606 return xfs_buf_iowait(bp);
611 xfs_buftarg_t *target,
614 xfs_buf_flags_t flags)
620 bp = xfs_buf_get(target, ioff, isize, flags);
622 trace_xfs_buf_read(bp, flags, _RET_IP_);
624 if (!XFS_BUF_ISDONE(bp)) {
625 XFS_STATS_INC(xb_get_read);
626 _xfs_buf_read(bp, flags);
627 } else if (flags & XBF_ASYNC) {
629 * Read ahead call which is already satisfied,
634 /* We do not want read in the flags */
635 bp->b_flags &= ~XBF_READ;
642 if (flags & (XBF_LOCK | XBF_TRYLOCK))
649 * If we are not low on memory then do the readahead in a deadlock
654 xfs_buftarg_t *target,
658 if (bdi_read_congested(target->bt_bdi))
661 xfs_buf_read(target, ioff, isize,
662 XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD|XBF_DONT_BLOCK);
666 * Read an uncached buffer from disk. Allocates and returns a locked
667 * buffer containing the disk contents or nothing.
670 xfs_buf_read_uncached(
671 struct xfs_mount *mp,
672 struct xfs_buftarg *target,
680 bp = xfs_buf_get_uncached(target, length, flags);
684 /* set up the buffer for a read IO */
686 XFS_BUF_SET_ADDR(bp, daddr);
691 error = xfs_buf_iowait(bp);
692 if (error || bp->b_error) {
702 xfs_buftarg_t *target)
706 bp = xfs_buf_allocate(0);
708 _xfs_buf_initialize(bp, target, 0, len, 0);
712 static inline struct page *
716 if ((!is_vmalloc_addr(addr))) {
717 return virt_to_page(addr);
719 return vmalloc_to_page(addr);
724 xfs_buf_associate_memory(
731 unsigned long pageaddr;
732 unsigned long offset;
736 pageaddr = (unsigned long)mem & PAGE_MASK;
737 offset = (unsigned long)mem - pageaddr;
738 buflen = PAGE_ALIGN(len + offset);
739 page_count = buflen >> PAGE_SHIFT;
741 /* Free any previous set of page pointers */
743 _xfs_buf_free_pages(bp);
748 rval = _xfs_buf_get_pages(bp, page_count, XBF_DONT_BLOCK);
752 bp->b_offset = offset;
754 for (i = 0; i < bp->b_page_count; i++) {
755 bp->b_pages[i] = mem_to_page((void *)pageaddr);
756 pageaddr += PAGE_SIZE;
759 bp->b_count_desired = len;
760 bp->b_buffer_length = buflen;
761 bp->b_flags |= XBF_MAPPED;
767 xfs_buf_get_uncached(
768 struct xfs_buftarg *target,
772 unsigned long page_count = PAGE_ALIGN(len) >> PAGE_SHIFT;
776 bp = xfs_buf_allocate(0);
777 if (unlikely(bp == NULL))
779 _xfs_buf_initialize(bp, target, 0, len, 0);
781 error = _xfs_buf_get_pages(bp, page_count, 0);
785 for (i = 0; i < page_count; i++) {
786 bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
790 bp->b_flags |= _XBF_PAGES;
792 error = _xfs_buf_map_pages(bp, XBF_MAPPED);
793 if (unlikely(error)) {
794 xfs_warn(target->bt_mount,
795 "%s: failed to map pages\n", __func__);
801 trace_xfs_buf_get_uncached(bp, _RET_IP_);
806 __free_page(bp->b_pages[i]);
807 _xfs_buf_free_pages(bp);
809 xfs_buf_deallocate(bp);
815 * Increment reference count on buffer, to hold the buffer concurrently
816 * with another thread which may release (free) the buffer asynchronously.
817 * Must hold the buffer already to call this function.
823 trace_xfs_buf_hold(bp, _RET_IP_);
824 atomic_inc(&bp->b_hold);
828 * Releases a hold on the specified buffer. If the
829 * the hold count is 1, calls xfs_buf_free.
835 struct xfs_perag *pag = bp->b_pag;
837 trace_xfs_buf_rele(bp, _RET_IP_);
840 ASSERT(list_empty(&bp->b_lru));
841 ASSERT(RB_EMPTY_NODE(&bp->b_rbnode));
842 if (atomic_dec_and_test(&bp->b_hold))
847 ASSERT(!RB_EMPTY_NODE(&bp->b_rbnode));
849 ASSERT(atomic_read(&bp->b_hold) > 0);
850 if (atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock)) {
851 if (!(bp->b_flags & XBF_STALE) &&
852 atomic_read(&bp->b_lru_ref)) {
854 spin_unlock(&pag->pag_buf_lock);
857 ASSERT(!(bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)));
858 rb_erase(&bp->b_rbnode, &pag->pag_buf_tree);
859 spin_unlock(&pag->pag_buf_lock);
868 * Lock a buffer object, if it is not already locked.
870 * If we come across a stale, pinned, locked buffer, we know that we are
871 * being asked to lock a buffer that has been reallocated. Because it is
872 * pinned, we know that the log has not been pushed to disk and hence it
873 * will still be locked. Rather than continuing to have trylock attempts
874 * fail until someone else pushes the log, push it ourselves before
875 * returning. This means that the xfsaild will not get stuck trying
876 * to push on stale inode buffers.
884 locked = down_trylock(&bp->b_sema) == 0;
887 else if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
888 xfs_log_force(bp->b_target->bt_mount, 0);
890 trace_xfs_buf_cond_lock(bp, _RET_IP_);
891 return locked ? 0 : -EBUSY;
898 return bp->b_sema.count;
902 * Lock a buffer object.
904 * If we come across a stale, pinned, locked buffer, we know that we
905 * are being asked to lock a buffer that has been reallocated. Because
906 * it is pinned, we know that the log has not been pushed to disk and
907 * hence it will still be locked. Rather than sleeping until someone
908 * else pushes the log, push it ourselves before trying to get the lock.
914 trace_xfs_buf_lock(bp, _RET_IP_);
916 if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
917 xfs_log_force(bp->b_target->bt_mount, 0);
918 if (atomic_read(&bp->b_io_remaining))
919 blk_flush_plug(current);
923 trace_xfs_buf_lock_done(bp, _RET_IP_);
927 * Releases the lock on the buffer object.
928 * If the buffer is marked delwri but is not queued, do so before we
929 * unlock the buffer as we need to set flags correctly. We also need to
930 * take a reference for the delwri queue because the unlocker is going to
931 * drop their's and they don't know we just queued it.
937 if ((bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)) == XBF_DELWRI) {
938 atomic_inc(&bp->b_hold);
939 bp->b_flags |= XBF_ASYNC;
940 xfs_buf_delwri_queue(bp, 0);
946 trace_xfs_buf_unlock(bp, _RET_IP_);
953 DECLARE_WAITQUEUE (wait, current);
955 if (atomic_read(&bp->b_pin_count) == 0)
958 add_wait_queue(&bp->b_waiters, &wait);
960 set_current_state(TASK_UNINTERRUPTIBLE);
961 if (atomic_read(&bp->b_pin_count) == 0)
965 remove_wait_queue(&bp->b_waiters, &wait);
966 set_current_state(TASK_RUNNING);
970 * Buffer Utility Routines
975 struct work_struct *work)
978 container_of(work, xfs_buf_t, b_iodone_work);
981 (*(bp->b_iodone))(bp);
982 else if (bp->b_flags & XBF_ASYNC)
991 trace_xfs_buf_iodone(bp, _RET_IP_);
993 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
994 if (bp->b_error == 0)
995 bp->b_flags |= XBF_DONE;
997 if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) {
999 INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
1000 queue_work(xfslogd_workqueue, &bp->b_iodone_work);
1002 xfs_buf_iodone_work(&bp->b_iodone_work);
1005 complete(&bp->b_iowait);
1014 ASSERT(error >= 0 && error <= 0xffff);
1015 bp->b_error = (unsigned short)error;
1016 trace_xfs_buf_ioerror(bp, error, _RET_IP_);
1021 struct xfs_mount *mp,
1026 bp->b_flags |= XBF_WRITE;
1027 bp->b_flags &= ~(XBF_ASYNC | XBF_READ);
1029 xfs_buf_delwri_dequeue(bp);
1032 error = xfs_buf_iowait(bp);
1034 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1044 trace_xfs_buf_bdwrite(bp, _RET_IP_);
1046 bp->b_flags &= ~XBF_READ;
1047 bp->b_flags |= (XBF_DELWRI | XBF_ASYNC);
1049 xfs_buf_delwri_queue(bp, 1);
1053 * Called when we want to stop a buffer from getting written or read.
1054 * We attach the EIO error, muck with its flags, and call xfs_buf_ioend
1055 * so that the proper iodone callbacks get called.
1061 #ifdef XFSERRORDEBUG
1062 ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
1066 * No need to wait until the buffer is unpinned, we aren't flushing it.
1068 XFS_BUF_ERROR(bp, EIO);
1071 * We're calling xfs_buf_ioend, so delete XBF_DONE flag.
1074 XFS_BUF_UNDELAYWRITE(bp);
1078 xfs_buf_ioend(bp, 0);
1084 * Same as xfs_bioerror, except that we are releasing the buffer
1085 * here ourselves, and avoiding the xfs_buf_ioend call.
1086 * This is meant for userdata errors; metadata bufs come with
1087 * iodone functions attached, so that we can track down errors.
1093 int64_t fl = XFS_BUF_BFLAGS(bp);
1095 * No need to wait until the buffer is unpinned.
1096 * We aren't flushing it.
1098 * chunkhold expects B_DONE to be set, whether
1099 * we actually finish the I/O or not. We don't want to
1100 * change that interface.
1103 XFS_BUF_UNDELAYWRITE(bp);
1106 XFS_BUF_CLR_IODONE_FUNC(bp);
1107 if (!(fl & XBF_ASYNC)) {
1109 * Mark b_error and B_ERROR _both_.
1110 * Lot's of chunkcache code assumes that.
1111 * There's no reason to mark error for
1114 XFS_BUF_ERROR(bp, EIO);
1115 XFS_BUF_FINISH_IOWAIT(bp);
1125 * All xfs metadata buffers except log state machine buffers
1126 * get this attached as their b_bdstrat callback function.
1127 * This is so that we can catch a buffer
1128 * after prematurely unpinning it to forcibly shutdown the filesystem.
1134 if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
1135 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1137 * Metadata write that didn't get logged but
1138 * written delayed anyway. These aren't associated
1139 * with a transaction, and can be ignored.
1141 if (!bp->b_iodone && !XFS_BUF_ISREAD(bp))
1142 return xfs_bioerror_relse(bp);
1144 return xfs_bioerror(bp);
1147 xfs_buf_iorequest(bp);
1152 * Wrapper around bdstrat so that we can stop data from going to disk in case
1153 * we are shutting down the filesystem. Typically user data goes thru this
1154 * path; one of the exceptions is the superblock.
1158 struct xfs_mount *mp,
1161 if (XFS_FORCED_SHUTDOWN(mp)) {
1162 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1163 xfs_bioerror_relse(bp);
1167 xfs_buf_iorequest(bp);
1175 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
1176 xfs_buf_ioend(bp, schedule);
1184 xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
1186 xfs_buf_ioerror(bp, -error);
1188 if (!error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
1189 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1191 _xfs_buf_ioend(bp, 1);
1199 int rw, map_i, total_nr_pages, nr_pages;
1201 int offset = bp->b_offset;
1202 int size = bp->b_count_desired;
1203 sector_t sector = bp->b_bn;
1205 total_nr_pages = bp->b_page_count;
1208 if (bp->b_flags & XBF_ORDERED) {
1209 ASSERT(!(bp->b_flags & XBF_READ));
1210 rw = WRITE_FLUSH_FUA;
1211 } else if (bp->b_flags & XBF_LOG_BUFFER) {
1212 ASSERT(!(bp->b_flags & XBF_READ_AHEAD));
1213 bp->b_flags &= ~_XBF_RUN_QUEUES;
1214 rw = (bp->b_flags & XBF_WRITE) ? WRITE_SYNC : READ_SYNC;
1215 } else if (bp->b_flags & _XBF_RUN_QUEUES) {
1216 ASSERT(!(bp->b_flags & XBF_READ_AHEAD));
1217 bp->b_flags &= ~_XBF_RUN_QUEUES;
1218 rw = (bp->b_flags & XBF_WRITE) ? WRITE_META : READ_META;
1220 rw = (bp->b_flags & XBF_WRITE) ? WRITE :
1221 (bp->b_flags & XBF_READ_AHEAD) ? READA : READ;
1226 atomic_inc(&bp->b_io_remaining);
1227 nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
1228 if (nr_pages > total_nr_pages)
1229 nr_pages = total_nr_pages;
1231 bio = bio_alloc(GFP_NOIO, nr_pages);
1232 bio->bi_bdev = bp->b_target->bt_bdev;
1233 bio->bi_sector = sector;
1234 bio->bi_end_io = xfs_buf_bio_end_io;
1235 bio->bi_private = bp;
1238 for (; size && nr_pages; nr_pages--, map_i++) {
1239 int rbytes, nbytes = PAGE_SIZE - offset;
1244 rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset);
1245 if (rbytes < nbytes)
1249 sector += nbytes >> BBSHIFT;
1254 if (likely(bio->bi_size)) {
1255 if (xfs_buf_is_vmapped(bp)) {
1256 flush_kernel_vmap_range(bp->b_addr,
1257 xfs_buf_vmap_len(bp));
1259 submit_bio(rw, bio);
1263 xfs_buf_ioerror(bp, EIO);
1272 trace_xfs_buf_iorequest(bp, _RET_IP_);
1274 if (bp->b_flags & XBF_DELWRI) {
1275 xfs_buf_delwri_queue(bp, 1);
1279 if (bp->b_flags & XBF_WRITE) {
1280 xfs_buf_wait_unpin(bp);
1285 /* Set the count to 1 initially, this will stop an I/O
1286 * completion callout which happens before we have started
1287 * all the I/O from calling xfs_buf_ioend too early.
1289 atomic_set(&bp->b_io_remaining, 1);
1290 _xfs_buf_ioapply(bp);
1291 _xfs_buf_ioend(bp, 0);
1298 * Waits for I/O to complete on the buffer supplied.
1299 * It returns immediately if no I/O is pending.
1300 * It returns the I/O error code, if any, or 0 if there was no error.
1306 trace_xfs_buf_iowait(bp, _RET_IP_);
1308 if (atomic_read(&bp->b_io_remaining))
1309 blk_flush_plug(current);
1310 wait_for_completion(&bp->b_iowait);
1312 trace_xfs_buf_iowait_done(bp, _RET_IP_);
1323 if (bp->b_flags & XBF_MAPPED)
1324 return XFS_BUF_PTR(bp) + offset;
1326 offset += bp->b_offset;
1327 page = bp->b_pages[offset >> PAGE_SHIFT];
1328 return (xfs_caddr_t)page_address(page) + (offset & (PAGE_SIZE-1));
1332 * Move data into or out of a buffer.
1336 xfs_buf_t *bp, /* buffer to process */
1337 size_t boff, /* starting buffer offset */
1338 size_t bsize, /* length to copy */
1339 void *data, /* data address */
1340 xfs_buf_rw_t mode) /* read/write/zero flag */
1342 size_t bend, cpoff, csize;
1345 bend = boff + bsize;
1346 while (boff < bend) {
1347 page = bp->b_pages[xfs_buf_btoct(boff + bp->b_offset)];
1348 cpoff = xfs_buf_poff(boff + bp->b_offset);
1349 csize = min_t(size_t,
1350 PAGE_SIZE-cpoff, bp->b_count_desired-boff);
1352 ASSERT(((csize + cpoff) <= PAGE_SIZE));
1356 memset(page_address(page) + cpoff, 0, csize);
1359 memcpy(data, page_address(page) + cpoff, csize);
1362 memcpy(page_address(page) + cpoff, data, csize);
1371 * Handling of buffer targets (buftargs).
1375 * Wait for any bufs with callbacks that have been submitted but have not yet
1376 * returned. These buffers will have an elevated hold count, so wait on those
1377 * while freeing all the buffers only held by the LRU.
1381 struct xfs_buftarg *btp)
1386 spin_lock(&btp->bt_lru_lock);
1387 while (!list_empty(&btp->bt_lru)) {
1388 bp = list_first_entry(&btp->bt_lru, struct xfs_buf, b_lru);
1389 if (atomic_read(&bp->b_hold) > 1) {
1390 spin_unlock(&btp->bt_lru_lock);
1395 * clear the LRU reference count so the bufer doesn't get
1396 * ignored in xfs_buf_rele().
1398 atomic_set(&bp->b_lru_ref, 0);
1399 spin_unlock(&btp->bt_lru_lock);
1401 spin_lock(&btp->bt_lru_lock);
1403 spin_unlock(&btp->bt_lru_lock);
1408 struct shrinker *shrink,
1412 struct xfs_buftarg *btp = container_of(shrink,
1413 struct xfs_buftarg, bt_shrinker);
1418 return btp->bt_lru_nr;
1420 spin_lock(&btp->bt_lru_lock);
1421 while (!list_empty(&btp->bt_lru)) {
1422 if (nr_to_scan-- <= 0)
1425 bp = list_first_entry(&btp->bt_lru, struct xfs_buf, b_lru);
1428 * Decrement the b_lru_ref count unless the value is already
1429 * zero. If the value is already zero, we need to reclaim the
1430 * buffer, otherwise it gets another trip through the LRU.
1432 if (!atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
1433 list_move_tail(&bp->b_lru, &btp->bt_lru);
1438 * remove the buffer from the LRU now to avoid needing another
1439 * lock round trip inside xfs_buf_rele().
1441 list_move(&bp->b_lru, &dispose);
1444 spin_unlock(&btp->bt_lru_lock);
1446 while (!list_empty(&dispose)) {
1447 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1448 list_del_init(&bp->b_lru);
1452 return btp->bt_lru_nr;
1457 struct xfs_mount *mp,
1458 struct xfs_buftarg *btp)
1460 unregister_shrinker(&btp->bt_shrinker);
1462 xfs_flush_buftarg(btp, 1);
1463 if (mp->m_flags & XFS_MOUNT_BARRIER)
1464 xfs_blkdev_issue_flush(btp);
1466 kthread_stop(btp->bt_task);
1471 xfs_setsize_buftarg_flags(
1473 unsigned int blocksize,
1474 unsigned int sectorsize,
1477 btp->bt_bsize = blocksize;
1478 btp->bt_sshift = ffs(sectorsize) - 1;
1479 btp->bt_smask = sectorsize - 1;
1481 if (set_blocksize(btp->bt_bdev, sectorsize)) {
1482 xfs_warn(btp->bt_mount,
1483 "Cannot set_blocksize to %u on device %s\n",
1484 sectorsize, XFS_BUFTARG_NAME(btp));
1492 * When allocating the initial buffer target we have not yet
1493 * read in the superblock, so don't know what sized sectors
1494 * are being used is at this early stage. Play safe.
1497 xfs_setsize_buftarg_early(
1499 struct block_device *bdev)
1501 return xfs_setsize_buftarg_flags(btp,
1502 PAGE_SIZE, bdev_logical_block_size(bdev), 0);
1506 xfs_setsize_buftarg(
1508 unsigned int blocksize,
1509 unsigned int sectorsize)
1511 return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
1515 xfs_alloc_delwrite_queue(
1519 INIT_LIST_HEAD(&btp->bt_delwrite_queue);
1520 spin_lock_init(&btp->bt_delwrite_lock);
1522 btp->bt_task = kthread_run(xfsbufd, btp, "xfsbufd/%s", fsname);
1523 if (IS_ERR(btp->bt_task))
1524 return PTR_ERR(btp->bt_task);
1530 struct xfs_mount *mp,
1531 struct block_device *bdev,
1537 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
1540 btp->bt_dev = bdev->bd_dev;
1541 btp->bt_bdev = bdev;
1542 btp->bt_bdi = blk_get_backing_dev_info(bdev);
1546 INIT_LIST_HEAD(&btp->bt_lru);
1547 spin_lock_init(&btp->bt_lru_lock);
1548 if (xfs_setsize_buftarg_early(btp, bdev))
1550 if (xfs_alloc_delwrite_queue(btp, fsname))
1552 btp->bt_shrinker.shrink = xfs_buftarg_shrink;
1553 btp->bt_shrinker.seeks = DEFAULT_SEEKS;
1554 register_shrinker(&btp->bt_shrinker);
1564 * Delayed write buffer handling
1567 xfs_buf_delwri_queue(
1571 struct list_head *dwq = &bp->b_target->bt_delwrite_queue;
1572 spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
1574 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
1576 ASSERT((bp->b_flags&(XBF_DELWRI|XBF_ASYNC)) == (XBF_DELWRI|XBF_ASYNC));
1579 /* If already in the queue, dequeue and place at tail */
1580 if (!list_empty(&bp->b_list)) {
1581 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1583 atomic_dec(&bp->b_hold);
1584 list_del(&bp->b_list);
1587 if (list_empty(dwq)) {
1588 /* start xfsbufd as it is about to have something to do */
1589 wake_up_process(bp->b_target->bt_task);
1592 bp->b_flags |= _XBF_DELWRI_Q;
1593 list_add_tail(&bp->b_list, dwq);
1594 bp->b_queuetime = jiffies;
1602 xfs_buf_delwri_dequeue(
1605 spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
1609 if ((bp->b_flags & XBF_DELWRI) && !list_empty(&bp->b_list)) {
1610 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1611 list_del_init(&bp->b_list);
1614 bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q);
1620 trace_xfs_buf_delwri_dequeue(bp, _RET_IP_);
1624 * If a delwri buffer needs to be pushed before it has aged out, then promote
1625 * it to the head of the delwri queue so that it will be flushed on the next
1626 * xfsbufd run. We do this by resetting the queuetime of the buffer to be older
1627 * than the age currently needed to flush the buffer. Hence the next time the
1628 * xfsbufd sees it is guaranteed to be considered old enough to flush.
1631 xfs_buf_delwri_promote(
1634 struct xfs_buftarg *btp = bp->b_target;
1635 long age = xfs_buf_age_centisecs * msecs_to_jiffies(10) + 1;
1637 ASSERT(bp->b_flags & XBF_DELWRI);
1638 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1641 * Check the buffer age before locking the delayed write queue as we
1642 * don't need to promote buffers that are already past the flush age.
1644 if (bp->b_queuetime < jiffies - age)
1646 bp->b_queuetime = jiffies - age;
1647 spin_lock(&btp->bt_delwrite_lock);
1648 list_move(&bp->b_list, &btp->bt_delwrite_queue);
1649 spin_unlock(&btp->bt_delwrite_lock);
1653 xfs_buf_runall_queues(
1654 struct workqueue_struct *queue)
1656 flush_workqueue(queue);
1660 * Move as many buffers as specified to the supplied list
1661 * idicating if we skipped any buffers to prevent deadlocks.
1664 xfs_buf_delwri_split(
1665 xfs_buftarg_t *target,
1666 struct list_head *list,
1670 struct list_head *dwq = &target->bt_delwrite_queue;
1671 spinlock_t *dwlk = &target->bt_delwrite_lock;
1675 force = test_and_clear_bit(XBT_FORCE_FLUSH, &target->bt_flags);
1676 INIT_LIST_HEAD(list);
1678 list_for_each_entry_safe(bp, n, dwq, b_list) {
1679 ASSERT(bp->b_flags & XBF_DELWRI);
1681 if (!XFS_BUF_ISPINNED(bp) && !xfs_buf_cond_lock(bp)) {
1683 time_before(jiffies, bp->b_queuetime + age)) {
1688 bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q|
1690 bp->b_flags |= XBF_WRITE;
1691 list_move_tail(&bp->b_list, list);
1692 trace_xfs_buf_delwri_split(bp, _RET_IP_);
1703 * Compare function is more complex than it needs to be because
1704 * the return value is only 32 bits and we are doing comparisons
1710 struct list_head *a,
1711 struct list_head *b)
1713 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
1714 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
1717 diff = ap->b_bn - bp->b_bn;
1726 xfs_buf_delwri_sort(
1727 xfs_buftarg_t *target,
1728 struct list_head *list)
1730 list_sort(NULL, list, xfs_buf_cmp);
1737 xfs_buftarg_t *target = (xfs_buftarg_t *)data;
1739 current->flags |= PF_MEMALLOC;
1744 long age = xfs_buf_age_centisecs * msecs_to_jiffies(10);
1745 long tout = xfs_buf_timer_centisecs * msecs_to_jiffies(10);
1747 struct list_head tmp;
1749 if (unlikely(freezing(current))) {
1750 set_bit(XBT_FORCE_SLEEP, &target->bt_flags);
1753 clear_bit(XBT_FORCE_SLEEP, &target->bt_flags);
1756 /* sleep for a long time if there is nothing to do. */
1757 if (list_empty(&target->bt_delwrite_queue))
1758 tout = MAX_SCHEDULE_TIMEOUT;
1759 schedule_timeout_interruptible(tout);
1761 xfs_buf_delwri_split(target, &tmp, age);
1762 list_sort(NULL, &tmp, xfs_buf_cmp);
1763 while (!list_empty(&tmp)) {
1765 bp = list_first_entry(&tmp, struct xfs_buf, b_list);
1766 list_del_init(&bp->b_list);
1771 blk_flush_plug(current);
1773 } while (!kthread_should_stop());
1779 * Go through all incore buffers, and release buffers if they belong to
1780 * the given device. This is used in filesystem error handling to
1781 * preserve the consistency of its metadata.
1785 xfs_buftarg_t *target,
1790 LIST_HEAD(tmp_list);
1791 LIST_HEAD(wait_list);
1793 xfs_buf_runall_queues(xfsconvertd_workqueue);
1794 xfs_buf_runall_queues(xfsdatad_workqueue);
1795 xfs_buf_runall_queues(xfslogd_workqueue);
1797 set_bit(XBT_FORCE_FLUSH, &target->bt_flags);
1798 pincount = xfs_buf_delwri_split(target, &tmp_list, 0);
1801 * Dropped the delayed write list lock, now walk the temporary list.
1802 * All I/O is issued async and then if we need to wait for completion
1803 * we do that after issuing all the IO.
1805 list_sort(NULL, &tmp_list, xfs_buf_cmp);
1806 while (!list_empty(&tmp_list)) {
1807 bp = list_first_entry(&tmp_list, struct xfs_buf, b_list);
1808 ASSERT(target == bp->b_target);
1809 list_del_init(&bp->b_list);
1811 bp->b_flags &= ~XBF_ASYNC;
1812 list_add(&bp->b_list, &wait_list);
1818 /* Expedite and wait for IO to complete. */
1819 blk_flush_plug(current);
1820 while (!list_empty(&wait_list)) {
1821 bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
1823 list_del_init(&bp->b_list);
1835 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
1836 KM_ZONE_HWALIGN, NULL);
1840 xfslogd_workqueue = alloc_workqueue("xfslogd",
1841 WQ_MEM_RECLAIM | WQ_HIGHPRI, 1);
1842 if (!xfslogd_workqueue)
1843 goto out_free_buf_zone;
1845 xfsdatad_workqueue = alloc_workqueue("xfsdatad", WQ_MEM_RECLAIM, 1);
1846 if (!xfsdatad_workqueue)
1847 goto out_destroy_xfslogd_workqueue;
1849 xfsconvertd_workqueue = alloc_workqueue("xfsconvertd",
1851 if (!xfsconvertd_workqueue)
1852 goto out_destroy_xfsdatad_workqueue;
1856 out_destroy_xfsdatad_workqueue:
1857 destroy_workqueue(xfsdatad_workqueue);
1858 out_destroy_xfslogd_workqueue:
1859 destroy_workqueue(xfslogd_workqueue);
1861 kmem_zone_destroy(xfs_buf_zone);
1867 xfs_buf_terminate(void)
1869 destroy_workqueue(xfsconvertd_workqueue);
1870 destroy_workqueue(xfsdatad_workqueue);
1871 destroy_workqueue(xfslogd_workqueue);
1872 kmem_zone_destroy(xfs_buf_zone);
1875 #ifdef CONFIG_KDB_MODULES
1877 xfs_get_buftarg_list(void)
1879 return &xfs_buftarg_list;