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/slab.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>
40 #include "xfs_dmapi.h"
41 #include "xfs_mount.h"
43 static kmem_zone_t *xfs_buf_zone;
44 STATIC int xfsbufd(void *);
45 STATIC int xfsbufd_wakeup(int, gfp_t);
46 STATIC void xfs_buf_delwri_queue(xfs_buf_t *, int);
47 static struct shrinker xfs_buf_shake = {
48 .shrink = xfsbufd_wakeup,
49 .seeks = DEFAULT_SEEKS,
52 static struct workqueue_struct *xfslogd_workqueue;
53 struct workqueue_struct *xfsdatad_workqueue;
63 ktrace_enter(xfs_buf_trace_buf,
65 (void *)(unsigned long)bp->b_flags,
66 (void *)(unsigned long)bp->b_hold.counter,
67 (void *)(unsigned long)bp->b_sema.count,
70 (void *)(unsigned long)((bp->b_file_offset>>32) & 0xffffffff),
71 (void *)(unsigned long)(bp->b_file_offset & 0xffffffff),
72 (void *)(unsigned long)bp->b_buffer_length,
73 NULL, NULL, NULL, NULL, NULL);
75 ktrace_t *xfs_buf_trace_buf;
76 #define XFS_BUF_TRACE_SIZE 4096
77 #define XB_TRACE(bp, id, data) \
78 xfs_buf_trace(bp, id, (void *)data, (void *)__builtin_return_address(0))
80 #define XB_TRACE(bp, id, data) do { } while (0)
83 #ifdef XFS_BUF_LOCK_TRACKING
84 # define XB_SET_OWNER(bp) ((bp)->b_last_holder = current->pid)
85 # define XB_CLEAR_OWNER(bp) ((bp)->b_last_holder = -1)
86 # define XB_GET_OWNER(bp) ((bp)->b_last_holder)
88 # define XB_SET_OWNER(bp) do { } while (0)
89 # define XB_CLEAR_OWNER(bp) do { } while (0)
90 # define XB_GET_OWNER(bp) do { } while (0)
93 #define xb_to_gfp(flags) \
94 ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : \
95 ((flags) & XBF_DONT_BLOCK) ? GFP_NOFS : GFP_KERNEL) | __GFP_NOWARN)
97 #define xb_to_km(flags) \
98 (((flags) & XBF_DONT_BLOCK) ? KM_NOFS : KM_SLEEP)
100 #define xfs_buf_allocate(flags) \
101 kmem_zone_alloc(xfs_buf_zone, xb_to_km(flags))
102 #define xfs_buf_deallocate(bp) \
103 kmem_zone_free(xfs_buf_zone, (bp));
106 * Page Region interfaces.
108 * For pages in filesystems where the blocksize is smaller than the
109 * pagesize, we use the page->private field (long) to hold a bitmap
110 * of uptodate regions within the page.
112 * Each such region is "bytes per page / bits per long" bytes long.
114 * NBPPR == number-of-bytes-per-page-region
115 * BTOPR == bytes-to-page-region (rounded up)
116 * BTOPRT == bytes-to-page-region-truncated (rounded down)
118 #if (BITS_PER_LONG == 32)
119 #define PRSHIFT (PAGE_CACHE_SHIFT - 5) /* (32 == 1<<5) */
120 #elif (BITS_PER_LONG == 64)
121 #define PRSHIFT (PAGE_CACHE_SHIFT - 6) /* (64 == 1<<6) */
123 #error BITS_PER_LONG must be 32 or 64
125 #define NBPPR (PAGE_CACHE_SIZE/BITS_PER_LONG)
126 #define BTOPR(b) (((unsigned int)(b) + (NBPPR - 1)) >> PRSHIFT)
127 #define BTOPRT(b) (((unsigned int)(b) >> PRSHIFT))
137 first = BTOPR(offset);
138 final = BTOPRT(offset + length - 1);
139 first = min(first, final);
142 mask <<= BITS_PER_LONG - (final - first);
143 mask >>= BITS_PER_LONG - (final);
145 ASSERT(offset + length <= PAGE_CACHE_SIZE);
146 ASSERT((final - first) < BITS_PER_LONG && (final - first) >= 0);
157 set_page_private(page,
158 page_private(page) | page_region_mask(offset, length));
159 if (page_private(page) == ~0UL)
160 SetPageUptodate(page);
169 unsigned long mask = page_region_mask(offset, length);
171 return (mask && (page_private(page) & mask) == mask);
175 * Mapping of multi-page buffers into contiguous virtual space
178 typedef struct a_list {
183 static a_list_t *as_free_head;
184 static int as_list_len;
185 static DEFINE_SPINLOCK(as_lock);
188 * Try to batch vunmaps because they are costly.
198 * Xen needs to be able to make sure it can get an exclusive
199 * RO mapping of pages it wants to turn into a pagetable. If
200 * a newly allocated page is also still being vmap()ed by xfs,
201 * it will cause pagetable construction to fail. This is a
202 * quick workaround to always eagerly unmap pages so that Xen
209 aentry = kmalloc(sizeof(a_list_t), GFP_NOWAIT);
210 if (likely(aentry)) {
212 aentry->next = as_free_head;
213 aentry->vm_addr = addr;
214 as_free_head = aentry;
216 spin_unlock(&as_lock);
223 purge_addresses(void)
225 a_list_t *aentry, *old;
227 if (as_free_head == NULL)
231 aentry = as_free_head;
234 spin_unlock(&as_lock);
236 while ((old = aentry) != NULL) {
237 vunmap(aentry->vm_addr);
238 aentry = aentry->next;
244 * Internal xfs_buf_t object manipulation
250 xfs_buftarg_t *target,
251 xfs_off_t range_base,
253 xfs_buf_flags_t flags)
256 * We don't want certain flags to appear in b_flags.
258 flags &= ~(XBF_LOCK|XBF_MAPPED|XBF_DONT_BLOCK|XBF_READ_AHEAD);
260 memset(bp, 0, sizeof(xfs_buf_t));
261 atomic_set(&bp->b_hold, 1);
262 init_completion(&bp->b_iowait);
263 INIT_LIST_HEAD(&bp->b_list);
264 INIT_LIST_HEAD(&bp->b_hash_list);
265 init_MUTEX_LOCKED(&bp->b_sema); /* held, no waiters */
267 bp->b_target = target;
268 bp->b_file_offset = range_base;
270 * Set buffer_length and count_desired to the same value initially.
271 * I/O routines should use count_desired, which will be the same in
272 * most cases but may be reset (e.g. XFS recovery).
274 bp->b_buffer_length = bp->b_count_desired = range_length;
276 bp->b_bn = XFS_BUF_DADDR_NULL;
277 atomic_set(&bp->b_pin_count, 0);
278 init_waitqueue_head(&bp->b_waiters);
280 XFS_STATS_INC(xb_create);
281 XB_TRACE(bp, "initialize", target);
285 * Allocate a page array capable of holding a specified number
286 * of pages, and point the page buf at it.
292 xfs_buf_flags_t flags)
294 /* Make sure that we have a page list */
295 if (bp->b_pages == NULL) {
296 bp->b_offset = xfs_buf_poff(bp->b_file_offset);
297 bp->b_page_count = page_count;
298 if (page_count <= XB_PAGES) {
299 bp->b_pages = bp->b_page_array;
301 bp->b_pages = kmem_alloc(sizeof(struct page *) *
302 page_count, xb_to_km(flags));
303 if (bp->b_pages == NULL)
306 memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
312 * Frees b_pages if it was allocated.
318 if (bp->b_pages != bp->b_page_array) {
319 kmem_free(bp->b_pages);
324 * Releases the specified buffer.
326 * The modification state of any associated pages is left unchanged.
327 * The buffer most not be on any hash - use xfs_buf_rele instead for
328 * hashed and refcounted buffers
334 XB_TRACE(bp, "free", 0);
336 ASSERT(list_empty(&bp->b_hash_list));
338 if (bp->b_flags & (_XBF_PAGE_CACHE|_XBF_PAGES)) {
341 if ((bp->b_flags & XBF_MAPPED) && (bp->b_page_count > 1))
342 free_address(bp->b_addr - bp->b_offset);
344 for (i = 0; i < bp->b_page_count; i++) {
345 struct page *page = bp->b_pages[i];
347 if (bp->b_flags & _XBF_PAGE_CACHE)
348 ASSERT(!PagePrivate(page));
349 page_cache_release(page);
351 _xfs_buf_free_pages(bp);
354 xfs_buf_deallocate(bp);
358 * Finds all pages for buffer in question and builds it's page list.
361 _xfs_buf_lookup_pages(
365 struct address_space *mapping = bp->b_target->bt_mapping;
366 size_t blocksize = bp->b_target->bt_bsize;
367 size_t size = bp->b_count_desired;
368 size_t nbytes, offset;
369 gfp_t gfp_mask = xb_to_gfp(flags);
370 unsigned short page_count, i;
375 end = bp->b_file_offset + bp->b_buffer_length;
376 page_count = xfs_buf_btoc(end) - xfs_buf_btoct(bp->b_file_offset);
378 error = _xfs_buf_get_pages(bp, page_count, flags);
381 bp->b_flags |= _XBF_PAGE_CACHE;
383 offset = bp->b_offset;
384 first = bp->b_file_offset >> PAGE_CACHE_SHIFT;
386 for (i = 0; i < bp->b_page_count; i++) {
391 page = find_or_create_page(mapping, first + i, gfp_mask);
392 if (unlikely(page == NULL)) {
393 if (flags & XBF_READ_AHEAD) {
394 bp->b_page_count = i;
395 for (i = 0; i < bp->b_page_count; i++)
396 unlock_page(bp->b_pages[i]);
401 * This could deadlock.
403 * But until all the XFS lowlevel code is revamped to
404 * handle buffer allocation failures we can't do much.
406 if (!(++retries % 100))
408 "XFS: possible memory allocation "
409 "deadlock in %s (mode:0x%x)\n",
412 XFS_STATS_INC(xb_page_retries);
413 xfsbufd_wakeup(0, gfp_mask);
414 congestion_wait(WRITE, HZ/50);
418 XFS_STATS_INC(xb_page_found);
420 nbytes = min_t(size_t, size, PAGE_CACHE_SIZE - offset);
423 ASSERT(!PagePrivate(page));
424 if (!PageUptodate(page)) {
426 if (blocksize >= PAGE_CACHE_SIZE) {
427 if (flags & XBF_READ)
428 bp->b_flags |= _XBF_PAGE_LOCKED;
429 } else if (!PagePrivate(page)) {
430 if (test_page_region(page, offset, nbytes))
435 bp->b_pages[i] = page;
439 if (!(bp->b_flags & _XBF_PAGE_LOCKED)) {
440 for (i = 0; i < bp->b_page_count; i++)
441 unlock_page(bp->b_pages[i]);
444 if (page_count == bp->b_page_count)
445 bp->b_flags |= XBF_DONE;
447 XB_TRACE(bp, "lookup_pages", (long)page_count);
452 * Map buffer into kernel address-space if nessecary.
459 /* A single page buffer is always mappable */
460 if (bp->b_page_count == 1) {
461 bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
462 bp->b_flags |= XBF_MAPPED;
463 } else if (flags & XBF_MAPPED) {
464 if (as_list_len > 64)
466 bp->b_addr = vmap(bp->b_pages, bp->b_page_count,
467 VM_MAP, PAGE_KERNEL);
468 if (unlikely(bp->b_addr == NULL))
470 bp->b_addr += bp->b_offset;
471 bp->b_flags |= XBF_MAPPED;
478 * Finding and Reading Buffers
482 * Look up, and creates if absent, a lockable buffer for
483 * a given range of an inode. The buffer is returned
484 * locked. If other overlapping buffers exist, they are
485 * released before the new buffer is created and locked,
486 * which may imply that this call will block until those buffers
487 * are unlocked. No I/O is implied by this call.
491 xfs_buftarg_t *btp, /* block device target */
492 xfs_off_t ioff, /* starting offset of range */
493 size_t isize, /* length of range */
494 xfs_buf_flags_t flags,
497 xfs_off_t range_base;
502 range_base = (ioff << BBSHIFT);
503 range_length = (isize << BBSHIFT);
505 /* Check for IOs smaller than the sector size / not sector aligned */
506 ASSERT(!(range_length < (1 << btp->bt_sshift)));
507 ASSERT(!(range_base & (xfs_off_t)btp->bt_smask));
509 hash = &btp->bt_hash[hash_long((unsigned long)ioff, btp->bt_hashshift)];
511 spin_lock(&hash->bh_lock);
513 list_for_each_entry_safe(bp, n, &hash->bh_list, b_hash_list) {
514 ASSERT(btp == bp->b_target);
515 if (bp->b_file_offset == range_base &&
516 bp->b_buffer_length == range_length) {
518 * If we look at something, bring it to the
519 * front of the list for next time.
521 atomic_inc(&bp->b_hold);
522 list_move(&bp->b_hash_list, &hash->bh_list);
529 _xfs_buf_initialize(new_bp, btp, range_base,
530 range_length, flags);
531 new_bp->b_hash = hash;
532 list_add(&new_bp->b_hash_list, &hash->bh_list);
534 XFS_STATS_INC(xb_miss_locked);
537 spin_unlock(&hash->bh_lock);
541 spin_unlock(&hash->bh_lock);
543 /* Attempt to get the semaphore without sleeping,
544 * if this does not work then we need to drop the
545 * spinlock and do a hard attempt on the semaphore.
547 if (down_trylock(&bp->b_sema)) {
548 if (!(flags & XBF_TRYLOCK)) {
549 /* wait for buffer ownership */
550 XB_TRACE(bp, "get_lock", 0);
552 XFS_STATS_INC(xb_get_locked_waited);
554 /* We asked for a trylock and failed, no need
555 * to look at file offset and length here, we
556 * know that this buffer at least overlaps our
557 * buffer and is locked, therefore our buffer
558 * either does not exist, or is this buffer.
561 XFS_STATS_INC(xb_busy_locked);
569 if (bp->b_flags & XBF_STALE) {
570 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
571 bp->b_flags &= XBF_MAPPED;
573 XB_TRACE(bp, "got_lock", 0);
574 XFS_STATS_INC(xb_get_locked);
579 * Assembles a buffer covering the specified range.
580 * Storage in memory for all portions of the buffer will be allocated,
581 * although backing storage may not be.
585 xfs_buftarg_t *target,/* target for buffer */
586 xfs_off_t ioff, /* starting offset of range */
587 size_t isize, /* length of range */
588 xfs_buf_flags_t flags)
590 xfs_buf_t *bp, *new_bp;
593 new_bp = xfs_buf_allocate(flags);
594 if (unlikely(!new_bp))
597 bp = _xfs_buf_find(target, ioff, isize, flags, new_bp);
599 error = _xfs_buf_lookup_pages(bp, flags);
603 xfs_buf_deallocate(new_bp);
604 if (unlikely(bp == NULL))
608 for (i = 0; i < bp->b_page_count; i++)
609 mark_page_accessed(bp->b_pages[i]);
611 if (!(bp->b_flags & XBF_MAPPED)) {
612 error = _xfs_buf_map_pages(bp, flags);
613 if (unlikely(error)) {
614 printk(KERN_WARNING "%s: failed to map pages\n",
620 XFS_STATS_INC(xb_get);
623 * Always fill in the block number now, the mapped cases can do
624 * their own overlay of this later.
627 bp->b_count_desired = bp->b_buffer_length;
629 XB_TRACE(bp, "get", (unsigned long)flags);
633 if (flags & (XBF_LOCK | XBF_TRYLOCK))
642 xfs_buf_flags_t flags)
646 XB_TRACE(bp, "_xfs_buf_read", (unsigned long)flags);
648 ASSERT(!(flags & (XBF_DELWRI|XBF_WRITE)));
649 ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL);
651 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_DELWRI | \
652 XBF_READ_AHEAD | _XBF_RUN_QUEUES);
653 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | \
654 XBF_READ_AHEAD | _XBF_RUN_QUEUES);
656 status = xfs_buf_iorequest(bp);
657 if (!status && !(flags & XBF_ASYNC))
658 status = xfs_buf_iowait(bp);
664 xfs_buftarg_t *target,
667 xfs_buf_flags_t flags)
673 bp = xfs_buf_get_flags(target, ioff, isize, flags);
675 if (!XFS_BUF_ISDONE(bp)) {
676 XB_TRACE(bp, "read", (unsigned long)flags);
677 XFS_STATS_INC(xb_get_read);
678 _xfs_buf_read(bp, flags);
679 } else if (flags & XBF_ASYNC) {
680 XB_TRACE(bp, "read_async", (unsigned long)flags);
682 * Read ahead call which is already satisfied,
687 XB_TRACE(bp, "read_done", (unsigned long)flags);
688 /* We do not want read in the flags */
689 bp->b_flags &= ~XBF_READ;
696 if (flags & (XBF_LOCK | XBF_TRYLOCK))
703 * If we are not low on memory then do the readahead in a deadlock
708 xfs_buftarg_t *target,
711 xfs_buf_flags_t flags)
713 struct backing_dev_info *bdi;
715 bdi = target->bt_mapping->backing_dev_info;
716 if (bdi_read_congested(bdi))
719 flags |= (XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD);
720 xfs_buf_read_flags(target, ioff, isize, flags);
726 xfs_buftarg_t *target)
730 bp = xfs_buf_allocate(0);
732 _xfs_buf_initialize(bp, target, 0, len, 0);
736 static inline struct page *
740 if ((!is_vmalloc_addr(addr))) {
741 return virt_to_page(addr);
743 return vmalloc_to_page(addr);
748 xfs_buf_associate_memory(
755 unsigned long pageaddr;
756 unsigned long offset;
760 pageaddr = (unsigned long)mem & PAGE_CACHE_MASK;
761 offset = (unsigned long)mem - pageaddr;
762 buflen = PAGE_CACHE_ALIGN(len + offset);
763 page_count = buflen >> PAGE_CACHE_SHIFT;
765 /* Free any previous set of page pointers */
767 _xfs_buf_free_pages(bp);
772 rval = _xfs_buf_get_pages(bp, page_count, 0);
776 bp->b_offset = offset;
778 for (i = 0; i < bp->b_page_count; i++) {
779 bp->b_pages[i] = mem_to_page((void *)pageaddr);
780 pageaddr += PAGE_CACHE_SIZE;
783 bp->b_count_desired = len;
784 bp->b_buffer_length = buflen;
785 bp->b_flags |= XBF_MAPPED;
786 bp->b_flags &= ~_XBF_PAGE_LOCKED;
794 xfs_buftarg_t *target)
796 unsigned long page_count = PAGE_ALIGN(len) >> PAGE_SHIFT;
800 bp = xfs_buf_allocate(0);
801 if (unlikely(bp == NULL))
803 _xfs_buf_initialize(bp, target, 0, len, 0);
805 error = _xfs_buf_get_pages(bp, page_count, 0);
809 for (i = 0; i < page_count; i++) {
810 bp->b_pages[i] = alloc_page(GFP_KERNEL);
814 bp->b_flags |= _XBF_PAGES;
816 error = _xfs_buf_map_pages(bp, XBF_MAPPED);
817 if (unlikely(error)) {
818 printk(KERN_WARNING "%s: failed to map pages\n",
825 XB_TRACE(bp, "no_daddr", len);
830 __free_page(bp->b_pages[i]);
831 _xfs_buf_free_pages(bp);
833 xfs_buf_deallocate(bp);
839 * Increment reference count on buffer, to hold the buffer concurrently
840 * with another thread which may release (free) the buffer asynchronously.
841 * Must hold the buffer already to call this function.
847 atomic_inc(&bp->b_hold);
848 XB_TRACE(bp, "hold", 0);
852 * Releases a hold on the specified buffer. If the
853 * the hold count is 1, calls xfs_buf_free.
859 xfs_bufhash_t *hash = bp->b_hash;
861 XB_TRACE(bp, "rele", bp->b_relse);
863 if (unlikely(!hash)) {
864 ASSERT(!bp->b_relse);
865 if (atomic_dec_and_test(&bp->b_hold))
870 ASSERT(atomic_read(&bp->b_hold) > 0);
871 if (atomic_dec_and_lock(&bp->b_hold, &hash->bh_lock)) {
873 atomic_inc(&bp->b_hold);
874 spin_unlock(&hash->bh_lock);
875 (*(bp->b_relse)) (bp);
876 } else if (bp->b_flags & XBF_FS_MANAGED) {
877 spin_unlock(&hash->bh_lock);
879 ASSERT(!(bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)));
880 list_del_init(&bp->b_hash_list);
881 spin_unlock(&hash->bh_lock);
889 * Mutual exclusion on buffers. Locking model:
891 * Buffers associated with inodes for which buffer locking
892 * is not enabled are not protected by semaphores, and are
893 * assumed to be exclusively owned by the caller. There is a
894 * spinlock in the buffer, used by the caller when concurrent
895 * access is possible.
899 * Locks a buffer object, if it is not already locked.
900 * Note that this in no way locks the underlying pages, so it is only
901 * useful for synchronizing concurrent use of buffer objects, not for
902 * synchronizing independent access to the underlying pages.
910 locked = down_trylock(&bp->b_sema) == 0;
914 XB_TRACE(bp, "cond_lock", (long)locked);
915 return locked ? 0 : -EBUSY;
918 #if defined(DEBUG) || defined(XFS_BLI_TRACE)
923 return bp->b_sema.count;
928 * Locks a buffer object.
929 * Note that this in no way locks the underlying pages, so it is only
930 * useful for synchronizing concurrent use of buffer objects, not for
931 * synchronizing independent access to the underlying pages.
937 XB_TRACE(bp, "lock", 0);
938 if (atomic_read(&bp->b_io_remaining))
939 blk_run_address_space(bp->b_target->bt_mapping);
942 XB_TRACE(bp, "locked", 0);
946 * Releases the lock on the buffer object.
947 * If the buffer is marked delwri but is not queued, do so before we
948 * unlock the buffer as we need to set flags correctly. We also need to
949 * take a reference for the delwri queue because the unlocker is going to
950 * drop their's and they don't know we just queued it.
956 if ((bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)) == XBF_DELWRI) {
957 atomic_inc(&bp->b_hold);
958 bp->b_flags |= XBF_ASYNC;
959 xfs_buf_delwri_queue(bp, 0);
964 XB_TRACE(bp, "unlock", 0);
969 * Pinning Buffer Storage in Memory
970 * Ensure that no attempt to force a buffer to disk will succeed.
976 atomic_inc(&bp->b_pin_count);
977 XB_TRACE(bp, "pin", (long)bp->b_pin_count.counter);
984 if (atomic_dec_and_test(&bp->b_pin_count))
985 wake_up_all(&bp->b_waiters);
986 XB_TRACE(bp, "unpin", (long)bp->b_pin_count.counter);
993 return atomic_read(&bp->b_pin_count);
1000 DECLARE_WAITQUEUE (wait, current);
1002 if (atomic_read(&bp->b_pin_count) == 0)
1005 add_wait_queue(&bp->b_waiters, &wait);
1007 set_current_state(TASK_UNINTERRUPTIBLE);
1008 if (atomic_read(&bp->b_pin_count) == 0)
1010 if (atomic_read(&bp->b_io_remaining))
1011 blk_run_address_space(bp->b_target->bt_mapping);
1014 remove_wait_queue(&bp->b_waiters, &wait);
1015 set_current_state(TASK_RUNNING);
1019 * Buffer Utility Routines
1023 xfs_buf_iodone_work(
1024 struct work_struct *work)
1027 container_of(work, xfs_buf_t, b_iodone_work);
1030 * We can get an EOPNOTSUPP to ordered writes. Here we clear the
1031 * ordered flag and reissue them. Because we can't tell the higher
1032 * layers directly that they should not issue ordered I/O anymore, they
1033 * need to check if the _XFS_BARRIER_FAILED flag was set during I/O completion.
1035 if ((bp->b_error == EOPNOTSUPP) &&
1036 (bp->b_flags & (XBF_ORDERED|XBF_ASYNC)) == (XBF_ORDERED|XBF_ASYNC)) {
1037 XB_TRACE(bp, "ordered_retry", bp->b_iodone);
1038 bp->b_flags &= ~XBF_ORDERED;
1039 bp->b_flags |= _XFS_BARRIER_FAILED;
1040 xfs_buf_iorequest(bp);
1041 } else if (bp->b_iodone)
1042 (*(bp->b_iodone))(bp);
1043 else if (bp->b_flags & XBF_ASYNC)
1052 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
1053 if (bp->b_error == 0)
1054 bp->b_flags |= XBF_DONE;
1056 XB_TRACE(bp, "iodone", bp->b_iodone);
1058 if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) {
1060 INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
1061 queue_work(xfslogd_workqueue, &bp->b_iodone_work);
1063 xfs_buf_iodone_work(&bp->b_iodone_work);
1066 complete(&bp->b_iowait);
1075 ASSERT(error >= 0 && error <= 0xffff);
1076 bp->b_error = (unsigned short)error;
1077 XB_TRACE(bp, "ioerror", (unsigned long)error);
1085 XB_TRACE(bp, "bawrite", 0);
1087 ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL);
1089 xfs_buf_delwri_dequeue(bp);
1091 bp->b_flags &= ~(XBF_READ | XBF_DELWRI | XBF_READ_AHEAD);
1092 bp->b_flags |= (XBF_WRITE | XBF_ASYNC | _XBF_RUN_QUEUES);
1095 bp->b_strat = xfs_bdstrat_cb;
1096 return xfs_bdstrat_cb(bp);
1104 XB_TRACE(bp, "bdwrite", 0);
1106 bp->b_strat = xfs_bdstrat_cb;
1109 bp->b_flags &= ~XBF_READ;
1110 bp->b_flags |= (XBF_DELWRI | XBF_ASYNC);
1112 xfs_buf_delwri_queue(bp, 1);
1120 if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
1121 bp->b_flags &= ~_XBF_PAGE_LOCKED;
1122 xfs_buf_ioend(bp, schedule);
1131 xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
1132 unsigned int blocksize = bp->b_target->bt_bsize;
1133 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1135 xfs_buf_ioerror(bp, -error);
1138 struct page *page = bvec->bv_page;
1140 ASSERT(!PagePrivate(page));
1141 if (unlikely(bp->b_error)) {
1142 if (bp->b_flags & XBF_READ)
1143 ClearPageUptodate(page);
1144 } else if (blocksize >= PAGE_CACHE_SIZE) {
1145 SetPageUptodate(page);
1146 } else if (!PagePrivate(page) &&
1147 (bp->b_flags & _XBF_PAGE_CACHE)) {
1148 set_page_region(page, bvec->bv_offset, bvec->bv_len);
1151 if (--bvec >= bio->bi_io_vec)
1152 prefetchw(&bvec->bv_page->flags);
1154 if (bp->b_flags & _XBF_PAGE_LOCKED)
1156 } while (bvec >= bio->bi_io_vec);
1158 _xfs_buf_ioend(bp, 1);
1166 int rw, map_i, total_nr_pages, nr_pages;
1168 int offset = bp->b_offset;
1169 int size = bp->b_count_desired;
1170 sector_t sector = bp->b_bn;
1171 unsigned int blocksize = bp->b_target->bt_bsize;
1173 total_nr_pages = bp->b_page_count;
1176 if (bp->b_flags & XBF_ORDERED) {
1177 ASSERT(!(bp->b_flags & XBF_READ));
1179 } else if (bp->b_flags & _XBF_RUN_QUEUES) {
1180 ASSERT(!(bp->b_flags & XBF_READ_AHEAD));
1181 bp->b_flags &= ~_XBF_RUN_QUEUES;
1182 rw = (bp->b_flags & XBF_WRITE) ? WRITE_SYNC : READ_SYNC;
1184 rw = (bp->b_flags & XBF_WRITE) ? WRITE :
1185 (bp->b_flags & XBF_READ_AHEAD) ? READA : READ;
1188 /* Special code path for reading a sub page size buffer in --
1189 * we populate up the whole page, and hence the other metadata
1190 * in the same page. This optimization is only valid when the
1191 * filesystem block size is not smaller than the page size.
1193 if ((bp->b_buffer_length < PAGE_CACHE_SIZE) &&
1194 ((bp->b_flags & (XBF_READ|_XBF_PAGE_LOCKED)) ==
1195 (XBF_READ|_XBF_PAGE_LOCKED)) &&
1196 (blocksize >= PAGE_CACHE_SIZE)) {
1197 bio = bio_alloc(GFP_NOIO, 1);
1199 bio->bi_bdev = bp->b_target->bt_bdev;
1200 bio->bi_sector = sector - (offset >> BBSHIFT);
1201 bio->bi_end_io = xfs_buf_bio_end_io;
1202 bio->bi_private = bp;
1204 bio_add_page(bio, bp->b_pages[0], PAGE_CACHE_SIZE, 0);
1207 atomic_inc(&bp->b_io_remaining);
1213 atomic_inc(&bp->b_io_remaining);
1214 nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
1215 if (nr_pages > total_nr_pages)
1216 nr_pages = total_nr_pages;
1218 bio = bio_alloc(GFP_NOIO, nr_pages);
1219 bio->bi_bdev = bp->b_target->bt_bdev;
1220 bio->bi_sector = sector;
1221 bio->bi_end_io = xfs_buf_bio_end_io;
1222 bio->bi_private = bp;
1224 for (; size && nr_pages; nr_pages--, map_i++) {
1225 int rbytes, nbytes = PAGE_CACHE_SIZE - offset;
1230 rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset);
1231 if (rbytes < nbytes)
1235 sector += nbytes >> BBSHIFT;
1241 if (likely(bio->bi_size)) {
1242 submit_bio(rw, bio);
1247 xfs_buf_ioerror(bp, EIO);
1255 XB_TRACE(bp, "iorequest", 0);
1257 if (bp->b_flags & XBF_DELWRI) {
1258 xfs_buf_delwri_queue(bp, 1);
1262 if (bp->b_flags & XBF_WRITE) {
1263 xfs_buf_wait_unpin(bp);
1268 /* Set the count to 1 initially, this will stop an I/O
1269 * completion callout which happens before we have started
1270 * all the I/O from calling xfs_buf_ioend too early.
1272 atomic_set(&bp->b_io_remaining, 1);
1273 _xfs_buf_ioapply(bp);
1274 _xfs_buf_ioend(bp, 0);
1281 * Waits for I/O to complete on the buffer supplied.
1282 * It returns immediately if no I/O is pending.
1283 * It returns the I/O error code, if any, or 0 if there was no error.
1289 XB_TRACE(bp, "iowait", 0);
1290 if (atomic_read(&bp->b_io_remaining))
1291 blk_run_address_space(bp->b_target->bt_mapping);
1292 wait_for_completion(&bp->b_iowait);
1293 XB_TRACE(bp, "iowaited", (long)bp->b_error);
1304 if (bp->b_flags & XBF_MAPPED)
1305 return XFS_BUF_PTR(bp) + offset;
1307 offset += bp->b_offset;
1308 page = bp->b_pages[offset >> PAGE_CACHE_SHIFT];
1309 return (xfs_caddr_t)page_address(page) + (offset & (PAGE_CACHE_SIZE-1));
1313 * Move data into or out of a buffer.
1317 xfs_buf_t *bp, /* buffer to process */
1318 size_t boff, /* starting buffer offset */
1319 size_t bsize, /* length to copy */
1320 caddr_t data, /* data address */
1321 xfs_buf_rw_t mode) /* read/write/zero flag */
1323 size_t bend, cpoff, csize;
1326 bend = boff + bsize;
1327 while (boff < bend) {
1328 page = bp->b_pages[xfs_buf_btoct(boff + bp->b_offset)];
1329 cpoff = xfs_buf_poff(boff + bp->b_offset);
1330 csize = min_t(size_t,
1331 PAGE_CACHE_SIZE-cpoff, bp->b_count_desired-boff);
1333 ASSERT(((csize + cpoff) <= PAGE_CACHE_SIZE));
1337 memset(page_address(page) + cpoff, 0, csize);
1340 memcpy(data, page_address(page) + cpoff, csize);
1343 memcpy(page_address(page) + cpoff, data, csize);
1352 * Handling of buffer targets (buftargs).
1356 * Wait for any bufs with callbacks that have been submitted but
1357 * have not yet returned... walk the hash list for the target.
1364 xfs_bufhash_t *hash;
1367 for (i = 0; i < (1 << btp->bt_hashshift); i++) {
1368 hash = &btp->bt_hash[i];
1370 spin_lock(&hash->bh_lock);
1371 list_for_each_entry_safe(bp, n, &hash->bh_list, b_hash_list) {
1372 ASSERT(btp == bp->b_target);
1373 if (!(bp->b_flags & XBF_FS_MANAGED)) {
1374 spin_unlock(&hash->bh_lock);
1376 * Catch superblock reference count leaks
1379 BUG_ON(bp->b_bn == 0);
1384 spin_unlock(&hash->bh_lock);
1389 * Allocate buffer hash table for a given target.
1390 * For devices containing metadata (i.e. not the log/realtime devices)
1391 * we need to allocate a much larger hash table.
1400 btp->bt_hashshift = external ? 3 : 8; /* 8 or 256 buckets */
1401 btp->bt_hashmask = (1 << btp->bt_hashshift) - 1;
1402 btp->bt_hash = kmem_zalloc((1 << btp->bt_hashshift) *
1403 sizeof(xfs_bufhash_t), KM_SLEEP | KM_LARGE);
1404 for (i = 0; i < (1 << btp->bt_hashshift); i++) {
1405 spin_lock_init(&btp->bt_hash[i].bh_lock);
1406 INIT_LIST_HEAD(&btp->bt_hash[i].bh_list);
1414 kmem_free(btp->bt_hash);
1415 btp->bt_hash = NULL;
1419 * buftarg list for delwrite queue processing
1421 static LIST_HEAD(xfs_buftarg_list);
1422 static DEFINE_SPINLOCK(xfs_buftarg_lock);
1425 xfs_register_buftarg(
1428 spin_lock(&xfs_buftarg_lock);
1429 list_add(&btp->bt_list, &xfs_buftarg_list);
1430 spin_unlock(&xfs_buftarg_lock);
1434 xfs_unregister_buftarg(
1437 spin_lock(&xfs_buftarg_lock);
1438 list_del(&btp->bt_list);
1439 spin_unlock(&xfs_buftarg_lock);
1444 struct xfs_mount *mp,
1445 struct xfs_buftarg *btp)
1447 xfs_flush_buftarg(btp, 1);
1448 if (mp->m_flags & XFS_MOUNT_BARRIER)
1449 xfs_blkdev_issue_flush(btp);
1450 xfs_free_bufhash(btp);
1451 iput(btp->bt_mapping->host);
1453 /* Unregister the buftarg first so that we don't get a
1454 * wakeup finding a non-existent task
1456 xfs_unregister_buftarg(btp);
1457 kthread_stop(btp->bt_task);
1463 xfs_setsize_buftarg_flags(
1465 unsigned int blocksize,
1466 unsigned int sectorsize,
1469 btp->bt_bsize = blocksize;
1470 btp->bt_sshift = ffs(sectorsize) - 1;
1471 btp->bt_smask = sectorsize - 1;
1473 if (set_blocksize(btp->bt_bdev, sectorsize)) {
1475 "XFS: Cannot set_blocksize to %u on device %s\n",
1476 sectorsize, XFS_BUFTARG_NAME(btp));
1481 (PAGE_CACHE_SIZE / BITS_PER_LONG) > sectorsize) {
1483 "XFS: %u byte sectors in use on device %s. "
1484 "This is suboptimal; %u or greater is ideal.\n",
1485 sectorsize, XFS_BUFTARG_NAME(btp),
1486 (unsigned int)PAGE_CACHE_SIZE / BITS_PER_LONG);
1493 * When allocating the initial buffer target we have not yet
1494 * read in the superblock, so don't know what sized sectors
1495 * are being used is at this early stage. Play safe.
1498 xfs_setsize_buftarg_early(
1500 struct block_device *bdev)
1502 return xfs_setsize_buftarg_flags(btp,
1503 PAGE_CACHE_SIZE, bdev_hardsect_size(bdev), 0);
1507 xfs_setsize_buftarg(
1509 unsigned int blocksize,
1510 unsigned int sectorsize)
1512 return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
1516 xfs_mapping_buftarg(
1518 struct block_device *bdev)
1520 struct backing_dev_info *bdi;
1521 struct inode *inode;
1522 struct address_space *mapping;
1523 static const struct address_space_operations mapping_aops = {
1524 .sync_page = block_sync_page,
1525 .migratepage = fail_migrate_page,
1528 inode = new_inode(bdev->bd_inode->i_sb);
1531 "XFS: Cannot allocate mapping inode for device %s\n",
1532 XFS_BUFTARG_NAME(btp));
1535 inode->i_mode = S_IFBLK;
1536 inode->i_bdev = bdev;
1537 inode->i_rdev = bdev->bd_dev;
1538 bdi = blk_get_backing_dev_info(bdev);
1540 bdi = &default_backing_dev_info;
1541 mapping = &inode->i_data;
1542 mapping->a_ops = &mapping_aops;
1543 mapping->backing_dev_info = bdi;
1544 mapping_set_gfp_mask(mapping, GFP_NOFS);
1545 btp->bt_mapping = mapping;
1550 xfs_alloc_delwrite_queue(
1555 INIT_LIST_HEAD(&btp->bt_list);
1556 INIT_LIST_HEAD(&btp->bt_delwrite_queue);
1557 spin_lock_init(&btp->bt_delwrite_lock);
1559 btp->bt_task = kthread_run(xfsbufd, btp, "xfsbufd");
1560 if (IS_ERR(btp->bt_task)) {
1561 error = PTR_ERR(btp->bt_task);
1564 xfs_register_buftarg(btp);
1571 struct block_device *bdev,
1576 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
1578 btp->bt_dev = bdev->bd_dev;
1579 btp->bt_bdev = bdev;
1580 if (xfs_setsize_buftarg_early(btp, bdev))
1582 if (xfs_mapping_buftarg(btp, bdev))
1584 if (xfs_alloc_delwrite_queue(btp))
1586 xfs_alloc_bufhash(btp, external);
1596 * Delayed write buffer handling
1599 xfs_buf_delwri_queue(
1603 struct list_head *dwq = &bp->b_target->bt_delwrite_queue;
1604 spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
1606 XB_TRACE(bp, "delwri_q", (long)unlock);
1607 ASSERT((bp->b_flags&(XBF_DELWRI|XBF_ASYNC)) == (XBF_DELWRI|XBF_ASYNC));
1610 /* If already in the queue, dequeue and place at tail */
1611 if (!list_empty(&bp->b_list)) {
1612 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1614 atomic_dec(&bp->b_hold);
1615 list_del(&bp->b_list);
1618 bp->b_flags |= _XBF_DELWRI_Q;
1619 list_add_tail(&bp->b_list, dwq);
1620 bp->b_queuetime = jiffies;
1628 xfs_buf_delwri_dequeue(
1631 spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
1635 if ((bp->b_flags & XBF_DELWRI) && !list_empty(&bp->b_list)) {
1636 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1637 list_del_init(&bp->b_list);
1640 bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q);
1646 XB_TRACE(bp, "delwri_dq", (long)dequeued);
1650 xfs_buf_runall_queues(
1651 struct workqueue_struct *queue)
1653 flush_workqueue(queue);
1663 spin_lock(&xfs_buftarg_lock);
1664 list_for_each_entry(btp, &xfs_buftarg_list, bt_list) {
1665 if (test_bit(XBT_FORCE_SLEEP, &btp->bt_flags))
1667 set_bit(XBT_FORCE_FLUSH, &btp->bt_flags);
1668 wake_up_process(btp->bt_task);
1670 spin_unlock(&xfs_buftarg_lock);
1675 * Move as many buffers as specified to the supplied list
1676 * idicating if we skipped any buffers to prevent deadlocks.
1679 xfs_buf_delwri_split(
1680 xfs_buftarg_t *target,
1681 struct list_head *list,
1685 struct list_head *dwq = &target->bt_delwrite_queue;
1686 spinlock_t *dwlk = &target->bt_delwrite_lock;
1690 force = test_and_clear_bit(XBT_FORCE_FLUSH, &target->bt_flags);
1691 INIT_LIST_HEAD(list);
1693 list_for_each_entry_safe(bp, n, dwq, b_list) {
1694 XB_TRACE(bp, "walkq1", (long)xfs_buf_ispin(bp));
1695 ASSERT(bp->b_flags & XBF_DELWRI);
1697 if (!xfs_buf_ispin(bp) && !xfs_buf_cond_lock(bp)) {
1699 time_before(jiffies, bp->b_queuetime + age)) {
1704 bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q|
1706 bp->b_flags |= XBF_WRITE;
1707 list_move_tail(&bp->b_list, list);
1721 struct list_head tmp;
1722 xfs_buftarg_t *target = (xfs_buftarg_t *)data;
1726 current->flags |= PF_MEMALLOC;
1731 if (unlikely(freezing(current))) {
1732 set_bit(XBT_FORCE_SLEEP, &target->bt_flags);
1735 clear_bit(XBT_FORCE_SLEEP, &target->bt_flags);
1738 schedule_timeout_interruptible(
1739 xfs_buf_timer_centisecs * msecs_to_jiffies(10));
1741 xfs_buf_delwri_split(target, &tmp,
1742 xfs_buf_age_centisecs * msecs_to_jiffies(10));
1745 while (!list_empty(&tmp)) {
1746 bp = list_entry(tmp.next, xfs_buf_t, b_list);
1747 ASSERT(target == bp->b_target);
1749 list_del_init(&bp->b_list);
1750 xfs_buf_iostrategy(bp);
1754 if (as_list_len > 0)
1757 blk_run_address_space(target->bt_mapping);
1759 } while (!kthread_should_stop());
1765 * Go through all incore buffers, and release buffers if they belong to
1766 * the given device. This is used in filesystem error handling to
1767 * preserve the consistency of its metadata.
1771 xfs_buftarg_t *target,
1774 struct list_head tmp;
1778 xfs_buf_runall_queues(xfsdatad_workqueue);
1779 xfs_buf_runall_queues(xfslogd_workqueue);
1781 set_bit(XBT_FORCE_FLUSH, &target->bt_flags);
1782 pincount = xfs_buf_delwri_split(target, &tmp, 0);
1785 * Dropped the delayed write list lock, now walk the temporary list
1787 list_for_each_entry_safe(bp, n, &tmp, b_list) {
1788 ASSERT(target == bp->b_target);
1790 bp->b_flags &= ~XBF_ASYNC;
1792 list_del_init(&bp->b_list);
1794 xfs_buf_iostrategy(bp);
1798 blk_run_address_space(target->bt_mapping);
1801 * Remaining list items must be flushed before returning
1803 while (!list_empty(&tmp)) {
1804 bp = list_entry(tmp.next, xfs_buf_t, b_list);
1806 list_del_init(&bp->b_list);
1817 #ifdef XFS_BUF_TRACE
1818 xfs_buf_trace_buf = ktrace_alloc(XFS_BUF_TRACE_SIZE, KM_NOFS);
1821 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
1822 KM_ZONE_HWALIGN, NULL);
1824 goto out_free_trace_buf;
1826 xfslogd_workqueue = create_workqueue("xfslogd");
1827 if (!xfslogd_workqueue)
1828 goto out_free_buf_zone;
1830 xfsdatad_workqueue = create_workqueue("xfsdatad");
1831 if (!xfsdatad_workqueue)
1832 goto out_destroy_xfslogd_workqueue;
1834 register_shrinker(&xfs_buf_shake);
1837 out_destroy_xfslogd_workqueue:
1838 destroy_workqueue(xfslogd_workqueue);
1840 kmem_zone_destroy(xfs_buf_zone);
1842 #ifdef XFS_BUF_TRACE
1843 ktrace_free(xfs_buf_trace_buf);
1849 xfs_buf_terminate(void)
1851 unregister_shrinker(&xfs_buf_shake);
1852 destroy_workqueue(xfsdatad_workqueue);
1853 destroy_workqueue(xfslogd_workqueue);
1854 kmem_zone_destroy(xfs_buf_zone);
1855 #ifdef XFS_BUF_TRACE
1856 ktrace_free(xfs_buf_trace_buf);
1860 #ifdef CONFIG_KDB_MODULES
1862 xfs_get_buftarg_list(void)
1864 return &xfs_buftarg_list;