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>
37 static kmem_zone_t *xfs_buf_zone;
38 static kmem_shaker_t xfs_buf_shake;
39 STATIC int xfsbufd(void *);
40 STATIC int xfsbufd_wakeup(int, gfp_t);
41 STATIC void xfs_buf_delwri_queue(xfs_buf_t *, int);
43 static struct workqueue_struct *xfslogd_workqueue;
44 struct workqueue_struct *xfsdatad_workqueue;
54 ktrace_enter(xfs_buf_trace_buf,
56 (void *)(unsigned long)bp->b_flags,
57 (void *)(unsigned long)bp->b_hold.counter,
58 (void *)(unsigned long)bp->b_sema.count.counter,
61 (void *)(unsigned long)((bp->b_file_offset>>32) & 0xffffffff),
62 (void *)(unsigned long)(bp->b_file_offset & 0xffffffff),
63 (void *)(unsigned long)bp->b_buffer_length,
64 NULL, NULL, NULL, NULL, NULL);
66 ktrace_t *xfs_buf_trace_buf;
67 #define XFS_BUF_TRACE_SIZE 4096
68 #define XB_TRACE(bp, id, data) \
69 xfs_buf_trace(bp, id, (void *)data, (void *)__builtin_return_address(0))
71 #define XB_TRACE(bp, id, data) do { } while (0)
74 #ifdef XFS_BUF_LOCK_TRACKING
75 # define XB_SET_OWNER(bp) ((bp)->b_last_holder = current->pid)
76 # define XB_CLEAR_OWNER(bp) ((bp)->b_last_holder = -1)
77 # define XB_GET_OWNER(bp) ((bp)->b_last_holder)
79 # define XB_SET_OWNER(bp) do { } while (0)
80 # define XB_CLEAR_OWNER(bp) do { } while (0)
81 # define XB_GET_OWNER(bp) do { } while (0)
84 #define xb_to_gfp(flags) \
85 ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : \
86 ((flags) & XBF_DONT_BLOCK) ? GFP_NOFS : GFP_KERNEL) | __GFP_NOWARN)
88 #define xb_to_km(flags) \
89 (((flags) & XBF_DONT_BLOCK) ? KM_NOFS : KM_SLEEP)
91 #define xfs_buf_allocate(flags) \
92 kmem_zone_alloc(xfs_buf_zone, xb_to_km(flags))
93 #define xfs_buf_deallocate(bp) \
94 kmem_zone_free(xfs_buf_zone, (bp));
97 * Page Region interfaces.
99 * For pages in filesystems where the blocksize is smaller than the
100 * pagesize, we use the page->private field (long) to hold a bitmap
101 * of uptodate regions within the page.
103 * Each such region is "bytes per page / bits per long" bytes long.
105 * NBPPR == number-of-bytes-per-page-region
106 * BTOPR == bytes-to-page-region (rounded up)
107 * BTOPRT == bytes-to-page-region-truncated (rounded down)
109 #if (BITS_PER_LONG == 32)
110 #define PRSHIFT (PAGE_CACHE_SHIFT - 5) /* (32 == 1<<5) */
111 #elif (BITS_PER_LONG == 64)
112 #define PRSHIFT (PAGE_CACHE_SHIFT - 6) /* (64 == 1<<6) */
114 #error BITS_PER_LONG must be 32 or 64
116 #define NBPPR (PAGE_CACHE_SIZE/BITS_PER_LONG)
117 #define BTOPR(b) (((unsigned int)(b) + (NBPPR - 1)) >> PRSHIFT)
118 #define BTOPRT(b) (((unsigned int)(b) >> PRSHIFT))
128 first = BTOPR(offset);
129 final = BTOPRT(offset + length - 1);
130 first = min(first, final);
133 mask <<= BITS_PER_LONG - (final - first);
134 mask >>= BITS_PER_LONG - (final);
136 ASSERT(offset + length <= PAGE_CACHE_SIZE);
137 ASSERT((final - first) < BITS_PER_LONG && (final - first) >= 0);
148 set_page_private(page,
149 page_private(page) | page_region_mask(offset, length));
150 if (page_private(page) == ~0UL)
151 SetPageUptodate(page);
160 unsigned long mask = page_region_mask(offset, length);
162 return (mask && (page_private(page) & mask) == mask);
166 * Mapping of multi-page buffers into contiguous virtual space
169 typedef struct a_list {
174 static a_list_t *as_free_head;
175 static int as_list_len;
176 static DEFINE_SPINLOCK(as_lock);
179 * Try to batch vunmaps because they are costly.
187 aentry = kmalloc(sizeof(a_list_t), GFP_NOWAIT);
188 if (likely(aentry)) {
190 aentry->next = as_free_head;
191 aentry->vm_addr = addr;
192 as_free_head = aentry;
194 spin_unlock(&as_lock);
201 purge_addresses(void)
203 a_list_t *aentry, *old;
205 if (as_free_head == NULL)
209 aentry = as_free_head;
212 spin_unlock(&as_lock);
214 while ((old = aentry) != NULL) {
215 vunmap(aentry->vm_addr);
216 aentry = aentry->next;
222 * Internal xfs_buf_t object manipulation
228 xfs_buftarg_t *target,
229 xfs_off_t range_base,
231 xfs_buf_flags_t flags)
234 * We don't want certain flags to appear in b_flags.
236 flags &= ~(XBF_LOCK|XBF_MAPPED|XBF_DONT_BLOCK|XBF_READ_AHEAD);
238 memset(bp, 0, sizeof(xfs_buf_t));
239 atomic_set(&bp->b_hold, 1);
240 init_MUTEX_LOCKED(&bp->b_iodonesema);
241 INIT_LIST_HEAD(&bp->b_list);
242 INIT_LIST_HEAD(&bp->b_hash_list);
243 init_MUTEX_LOCKED(&bp->b_sema); /* held, no waiters */
245 bp->b_target = target;
246 bp->b_file_offset = range_base;
248 * Set buffer_length and count_desired to the same value initially.
249 * I/O routines should use count_desired, which will be the same in
250 * most cases but may be reset (e.g. XFS recovery).
252 bp->b_buffer_length = bp->b_count_desired = range_length;
254 bp->b_bn = XFS_BUF_DADDR_NULL;
255 atomic_set(&bp->b_pin_count, 0);
256 init_waitqueue_head(&bp->b_waiters);
258 XFS_STATS_INC(xb_create);
259 XB_TRACE(bp, "initialize", target);
263 * Allocate a page array capable of holding a specified number
264 * of pages, and point the page buf at it.
270 xfs_buf_flags_t flags)
272 /* Make sure that we have a page list */
273 if (bp->b_pages == NULL) {
274 bp->b_offset = xfs_buf_poff(bp->b_file_offset);
275 bp->b_page_count = page_count;
276 if (page_count <= XB_PAGES) {
277 bp->b_pages = bp->b_page_array;
279 bp->b_pages = kmem_alloc(sizeof(struct page *) *
280 page_count, xb_to_km(flags));
281 if (bp->b_pages == NULL)
284 memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
290 * Frees b_pages if it was allocated.
296 if (bp->b_pages != bp->b_page_array) {
297 kmem_free(bp->b_pages,
298 bp->b_page_count * sizeof(struct page *));
303 * Releases the specified buffer.
305 * The modification state of any associated pages is left unchanged.
306 * The buffer most not be on any hash - use xfs_buf_rele instead for
307 * hashed and refcounted buffers
313 XB_TRACE(bp, "free", 0);
315 ASSERT(list_empty(&bp->b_hash_list));
317 if (bp->b_flags & _XBF_PAGE_CACHE) {
320 if ((bp->b_flags & XBF_MAPPED) && (bp->b_page_count > 1))
321 free_address(bp->b_addr - bp->b_offset);
323 for (i = 0; i < bp->b_page_count; i++) {
324 struct page *page = bp->b_pages[i];
326 ASSERT(!PagePrivate(page));
327 page_cache_release(page);
329 _xfs_buf_free_pages(bp);
330 } else if (bp->b_flags & _XBF_KMEM_ALLOC) {
332 * XXX(hch): bp->b_count_desired might be incorrect (see
333 * xfs_buf_associate_memory for details), but fortunately
334 * the Linux version of kmem_free ignores the len argument..
336 kmem_free(bp->b_addr, bp->b_count_desired);
337 _xfs_buf_free_pages(bp);
340 xfs_buf_deallocate(bp);
344 * Finds all pages for buffer in question and builds it's page list.
347 _xfs_buf_lookup_pages(
351 struct address_space *mapping = bp->b_target->bt_mapping;
352 size_t blocksize = bp->b_target->bt_bsize;
353 size_t size = bp->b_count_desired;
354 size_t nbytes, offset;
355 gfp_t gfp_mask = xb_to_gfp(flags);
356 unsigned short page_count, i;
361 end = bp->b_file_offset + bp->b_buffer_length;
362 page_count = xfs_buf_btoc(end) - xfs_buf_btoct(bp->b_file_offset);
364 error = _xfs_buf_get_pages(bp, page_count, flags);
367 bp->b_flags |= _XBF_PAGE_CACHE;
369 offset = bp->b_offset;
370 first = bp->b_file_offset >> PAGE_CACHE_SHIFT;
372 for (i = 0; i < bp->b_page_count; i++) {
377 page = find_or_create_page(mapping, first + i, gfp_mask);
378 if (unlikely(page == NULL)) {
379 if (flags & XBF_READ_AHEAD) {
380 bp->b_page_count = i;
381 for (i = 0; i < bp->b_page_count; i++)
382 unlock_page(bp->b_pages[i]);
387 * This could deadlock.
389 * But until all the XFS lowlevel code is revamped to
390 * handle buffer allocation failures we can't do much.
392 if (!(++retries % 100))
394 "XFS: possible memory allocation "
395 "deadlock in %s (mode:0x%x)\n",
396 __FUNCTION__, gfp_mask);
398 XFS_STATS_INC(xb_page_retries);
399 xfsbufd_wakeup(0, gfp_mask);
400 congestion_wait(WRITE, HZ/50);
404 XFS_STATS_INC(xb_page_found);
406 nbytes = min_t(size_t, size, PAGE_CACHE_SIZE - offset);
409 ASSERT(!PagePrivate(page));
410 if (!PageUptodate(page)) {
412 if (blocksize >= PAGE_CACHE_SIZE) {
413 if (flags & XBF_READ)
415 } else if (!PagePrivate(page)) {
416 if (test_page_region(page, offset, nbytes))
421 bp->b_pages[i] = page;
426 for (i = 0; i < bp->b_page_count; i++)
427 unlock_page(bp->b_pages[i]);
430 if (page_count == bp->b_page_count)
431 bp->b_flags |= XBF_DONE;
433 XB_TRACE(bp, "lookup_pages", (long)page_count);
438 * Map buffer into kernel address-space if nessecary.
445 /* A single page buffer is always mappable */
446 if (bp->b_page_count == 1) {
447 bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
448 bp->b_flags |= XBF_MAPPED;
449 } else if (flags & XBF_MAPPED) {
450 if (as_list_len > 64)
452 bp->b_addr = vmap(bp->b_pages, bp->b_page_count,
453 VM_MAP, PAGE_KERNEL);
454 if (unlikely(bp->b_addr == NULL))
456 bp->b_addr += bp->b_offset;
457 bp->b_flags |= XBF_MAPPED;
464 * Finding and Reading Buffers
468 * Look up, and creates if absent, a lockable buffer for
469 * a given range of an inode. The buffer is returned
470 * locked. If other overlapping buffers exist, they are
471 * released before the new buffer is created and locked,
472 * which may imply that this call will block until those buffers
473 * are unlocked. No I/O is implied by this call.
477 xfs_buftarg_t *btp, /* block device target */
478 xfs_off_t ioff, /* starting offset of range */
479 size_t isize, /* length of range */
480 xfs_buf_flags_t flags,
483 xfs_off_t range_base;
488 range_base = (ioff << BBSHIFT);
489 range_length = (isize << BBSHIFT);
491 /* Check for IOs smaller than the sector size / not sector aligned */
492 ASSERT(!(range_length < (1 << btp->bt_sshift)));
493 ASSERT(!(range_base & (xfs_off_t)btp->bt_smask));
495 hash = &btp->bt_hash[hash_long((unsigned long)ioff, btp->bt_hashshift)];
497 spin_lock(&hash->bh_lock);
499 list_for_each_entry_safe(bp, n, &hash->bh_list, b_hash_list) {
500 ASSERT(btp == bp->b_target);
501 if (bp->b_file_offset == range_base &&
502 bp->b_buffer_length == range_length) {
504 * If we look at something, bring it to the
505 * front of the list for next time.
507 atomic_inc(&bp->b_hold);
508 list_move(&bp->b_hash_list, &hash->bh_list);
515 _xfs_buf_initialize(new_bp, btp, range_base,
516 range_length, flags);
517 new_bp->b_hash = hash;
518 list_add(&new_bp->b_hash_list, &hash->bh_list);
520 XFS_STATS_INC(xb_miss_locked);
523 spin_unlock(&hash->bh_lock);
527 spin_unlock(&hash->bh_lock);
529 /* Attempt to get the semaphore without sleeping,
530 * if this does not work then we need to drop the
531 * spinlock and do a hard attempt on the semaphore.
533 if (down_trylock(&bp->b_sema)) {
534 if (!(flags & XBF_TRYLOCK)) {
535 /* wait for buffer ownership */
536 XB_TRACE(bp, "get_lock", 0);
538 XFS_STATS_INC(xb_get_locked_waited);
540 /* We asked for a trylock and failed, no need
541 * to look at file offset and length here, we
542 * know that this buffer at least overlaps our
543 * buffer and is locked, therefore our buffer
544 * either does not exist, or is this buffer.
547 XFS_STATS_INC(xb_busy_locked);
555 if (bp->b_flags & XBF_STALE) {
556 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
557 bp->b_flags &= XBF_MAPPED;
559 XB_TRACE(bp, "got_lock", 0);
560 XFS_STATS_INC(xb_get_locked);
565 * Assembles a buffer covering the specified range.
566 * Storage in memory for all portions of the buffer will be allocated,
567 * although backing storage may not be.
571 xfs_buftarg_t *target,/* target for buffer */
572 xfs_off_t ioff, /* starting offset of range */
573 size_t isize, /* length of range */
574 xfs_buf_flags_t flags)
576 xfs_buf_t *bp, *new_bp;
579 new_bp = xfs_buf_allocate(flags);
580 if (unlikely(!new_bp))
583 bp = _xfs_buf_find(target, ioff, isize, flags, new_bp);
585 error = _xfs_buf_lookup_pages(bp, flags);
589 xfs_buf_deallocate(new_bp);
590 if (unlikely(bp == NULL))
594 for (i = 0; i < bp->b_page_count; i++)
595 mark_page_accessed(bp->b_pages[i]);
597 if (!(bp->b_flags & XBF_MAPPED)) {
598 error = _xfs_buf_map_pages(bp, flags);
599 if (unlikely(error)) {
600 printk(KERN_WARNING "%s: failed to map pages\n",
606 XFS_STATS_INC(xb_get);
609 * Always fill in the block number now, the mapped cases can do
610 * their own overlay of this later.
613 bp->b_count_desired = bp->b_buffer_length;
615 XB_TRACE(bp, "get", (unsigned long)flags);
619 if (flags & (XBF_LOCK | XBF_TRYLOCK))
627 xfs_buftarg_t *target,
630 xfs_buf_flags_t flags)
636 bp = xfs_buf_get_flags(target, ioff, isize, flags);
638 if (!XFS_BUF_ISDONE(bp)) {
639 XB_TRACE(bp, "read", (unsigned long)flags);
640 XFS_STATS_INC(xb_get_read);
641 xfs_buf_iostart(bp, flags);
642 } else if (flags & XBF_ASYNC) {
643 XB_TRACE(bp, "read_async", (unsigned long)flags);
645 * Read ahead call which is already satisfied,
650 XB_TRACE(bp, "read_done", (unsigned long)flags);
651 /* We do not want read in the flags */
652 bp->b_flags &= ~XBF_READ;
659 if (flags & (XBF_LOCK | XBF_TRYLOCK))
666 * If we are not low on memory then do the readahead in a deadlock
671 xfs_buftarg_t *target,
674 xfs_buf_flags_t flags)
676 struct backing_dev_info *bdi;
678 bdi = target->bt_mapping->backing_dev_info;
679 if (bdi_read_congested(bdi))
682 flags |= (XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD);
683 xfs_buf_read_flags(target, ioff, isize, flags);
689 xfs_buftarg_t *target)
693 bp = xfs_buf_allocate(0);
695 _xfs_buf_initialize(bp, target, 0, len, 0);
699 static inline struct page *
703 if (((unsigned long)addr < VMALLOC_START) ||
704 ((unsigned long)addr >= VMALLOC_END)) {
705 return virt_to_page(addr);
707 return vmalloc_to_page(addr);
712 xfs_buf_associate_memory(
724 page_count = PAGE_CACHE_ALIGN(len) >> PAGE_CACHE_SHIFT;
725 offset = (off_t) mem - ((off_t)mem & PAGE_CACHE_MASK);
726 if (offset && (len > PAGE_CACHE_SIZE))
729 /* Free any previous set of page pointers */
731 _xfs_buf_free_pages(bp);
736 rval = _xfs_buf_get_pages(bp, page_count, 0);
740 bp->b_offset = offset;
741 ptr = (size_t) mem & PAGE_CACHE_MASK;
742 end = PAGE_CACHE_ALIGN((size_t) mem + len);
744 /* set up first page */
745 bp->b_pages[0] = mem_to_page(mem);
747 ptr += PAGE_CACHE_SIZE;
748 bp->b_page_count = ++i;
750 bp->b_pages[i] = mem_to_page((void *)ptr);
751 bp->b_page_count = ++i;
752 ptr += PAGE_CACHE_SIZE;
756 bp->b_count_desired = bp->b_buffer_length = len;
757 bp->b_flags |= XBF_MAPPED;
765 xfs_buftarg_t *target)
767 size_t malloc_len = len;
772 bp = xfs_buf_allocate(0);
773 if (unlikely(bp == NULL))
775 _xfs_buf_initialize(bp, target, 0, len, 0);
778 data = kmem_alloc(malloc_len, KM_SLEEP | KM_MAYFAIL | KM_LARGE);
779 if (unlikely(data == NULL))
782 /* check whether alignment matches.. */
783 if ((__psunsigned_t)data !=
784 ((__psunsigned_t)data & ~target->bt_smask)) {
785 /* .. else double the size and try again */
786 kmem_free(data, malloc_len);
791 error = xfs_buf_associate_memory(bp, data, len);
794 bp->b_flags |= _XBF_KMEM_ALLOC;
798 XB_TRACE(bp, "no_daddr", data);
801 kmem_free(data, malloc_len);
809 * Increment reference count on buffer, to hold the buffer concurrently
810 * with another thread which may release (free) the buffer asynchronously.
811 * Must hold the buffer already to call this function.
817 atomic_inc(&bp->b_hold);
818 XB_TRACE(bp, "hold", 0);
822 * Releases a hold on the specified buffer. If the
823 * the hold count is 1, calls xfs_buf_free.
829 xfs_bufhash_t *hash = bp->b_hash;
831 XB_TRACE(bp, "rele", bp->b_relse);
833 if (unlikely(!hash)) {
834 ASSERT(!bp->b_relse);
835 if (atomic_dec_and_test(&bp->b_hold))
840 if (atomic_dec_and_lock(&bp->b_hold, &hash->bh_lock)) {
842 atomic_inc(&bp->b_hold);
843 spin_unlock(&hash->bh_lock);
844 (*(bp->b_relse)) (bp);
845 } else if (bp->b_flags & XBF_FS_MANAGED) {
846 spin_unlock(&hash->bh_lock);
848 ASSERT(!(bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)));
849 list_del_init(&bp->b_hash_list);
850 spin_unlock(&hash->bh_lock);
855 * Catch reference count leaks
857 ASSERT(atomic_read(&bp->b_hold) >= 0);
863 * Mutual exclusion on buffers. Locking model:
865 * Buffers associated with inodes for which buffer locking
866 * is not enabled are not protected by semaphores, and are
867 * assumed to be exclusively owned by the caller. There is a
868 * spinlock in the buffer, used by the caller when concurrent
869 * access is possible.
873 * Locks a buffer object, if it is not already locked.
874 * Note that this in no way locks the underlying pages, so it is only
875 * useful for synchronizing concurrent use of buffer objects, not for
876 * synchronizing independent access to the underlying pages.
884 locked = down_trylock(&bp->b_sema) == 0;
888 XB_TRACE(bp, "cond_lock", (long)locked);
889 return locked ? 0 : -EBUSY;
892 #if defined(DEBUG) || defined(XFS_BLI_TRACE)
897 return atomic_read(&bp->b_sema.count);
902 * Locks a buffer object.
903 * Note that this in no way locks the underlying pages, so it is only
904 * useful for synchronizing concurrent use of buffer objects, not for
905 * synchronizing independent access to the underlying pages.
911 XB_TRACE(bp, "lock", 0);
912 if (atomic_read(&bp->b_io_remaining))
913 blk_run_address_space(bp->b_target->bt_mapping);
916 XB_TRACE(bp, "locked", 0);
920 * Releases the lock on the buffer object.
921 * If the buffer is marked delwri but is not queued, do so before we
922 * unlock the buffer as we need to set flags correctly. We also need to
923 * take a reference for the delwri queue because the unlocker is going to
924 * drop their's and they don't know we just queued it.
930 if ((bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)) == XBF_DELWRI) {
931 atomic_inc(&bp->b_hold);
932 bp->b_flags |= XBF_ASYNC;
933 xfs_buf_delwri_queue(bp, 0);
938 XB_TRACE(bp, "unlock", 0);
943 * Pinning Buffer Storage in Memory
944 * Ensure that no attempt to force a buffer to disk will succeed.
950 atomic_inc(&bp->b_pin_count);
951 XB_TRACE(bp, "pin", (long)bp->b_pin_count.counter);
958 if (atomic_dec_and_test(&bp->b_pin_count))
959 wake_up_all(&bp->b_waiters);
960 XB_TRACE(bp, "unpin", (long)bp->b_pin_count.counter);
967 return atomic_read(&bp->b_pin_count);
974 DECLARE_WAITQUEUE (wait, current);
976 if (atomic_read(&bp->b_pin_count) == 0)
979 add_wait_queue(&bp->b_waiters, &wait);
981 set_current_state(TASK_UNINTERRUPTIBLE);
982 if (atomic_read(&bp->b_pin_count) == 0)
984 if (atomic_read(&bp->b_io_remaining))
985 blk_run_address_space(bp->b_target->bt_mapping);
988 remove_wait_queue(&bp->b_waiters, &wait);
989 set_current_state(TASK_RUNNING);
993 * Buffer Utility Routines
998 struct work_struct *work)
1001 container_of(work, xfs_buf_t, b_iodone_work);
1004 (*(bp->b_iodone))(bp);
1005 else if (bp->b_flags & XBF_ASYNC)
1014 bp->b_flags &= ~(XBF_READ | XBF_WRITE);
1015 if (bp->b_error == 0)
1016 bp->b_flags |= XBF_DONE;
1018 XB_TRACE(bp, "iodone", bp->b_iodone);
1020 if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) {
1022 INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
1023 queue_work(xfslogd_workqueue, &bp->b_iodone_work);
1025 xfs_buf_iodone_work(&bp->b_iodone_work);
1028 up(&bp->b_iodonesema);
1037 ASSERT(error >= 0 && error <= 0xffff);
1038 bp->b_error = (unsigned short)error;
1039 XB_TRACE(bp, "ioerror", (unsigned long)error);
1043 * Initiate I/O on a buffer, based on the flags supplied.
1044 * The b_iodone routine in the buffer supplied will only be called
1045 * when all of the subsidiary I/O requests, if any, have been completed.
1050 xfs_buf_flags_t flags)
1054 XB_TRACE(bp, "iostart", (unsigned long)flags);
1056 if (flags & XBF_DELWRI) {
1057 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_ASYNC);
1058 bp->b_flags |= flags & (XBF_DELWRI | XBF_ASYNC);
1059 xfs_buf_delwri_queue(bp, 1);
1063 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_ASYNC | XBF_DELWRI | \
1064 XBF_READ_AHEAD | _XBF_RUN_QUEUES);
1065 bp->b_flags |= flags & (XBF_READ | XBF_WRITE | XBF_ASYNC | \
1066 XBF_READ_AHEAD | _XBF_RUN_QUEUES);
1068 BUG_ON(bp->b_bn == XFS_BUF_DADDR_NULL);
1070 /* For writes allow an alternate strategy routine to precede
1071 * the actual I/O request (which may not be issued at all in
1072 * a shutdown situation, for example).
1074 status = (flags & XBF_WRITE) ?
1075 xfs_buf_iostrategy(bp) : xfs_buf_iorequest(bp);
1077 /* Wait for I/O if we are not an async request.
1078 * Note: async I/O request completion will release the buffer,
1079 * and that can already be done by this point. So using the
1080 * buffer pointer from here on, after async I/O, is invalid.
1082 if (!status && !(flags & XBF_ASYNC))
1083 status = xfs_buf_iowait(bp);
1092 ASSERT(bp->b_flags & (XBF_READ | XBF_WRITE));
1093 if (bp->b_flags & XBF_READ)
1094 return bp->b_locked;
1103 if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
1105 xfs_buf_ioend(bp, schedule);
1112 unsigned int bytes_done,
1115 xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
1116 unsigned int blocksize = bp->b_target->bt_bsize;
1117 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1122 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
1126 struct page *page = bvec->bv_page;
1128 ASSERT(!PagePrivate(page));
1129 if (unlikely(bp->b_error)) {
1130 if (bp->b_flags & XBF_READ)
1131 ClearPageUptodate(page);
1132 } else if (blocksize >= PAGE_CACHE_SIZE) {
1133 SetPageUptodate(page);
1134 } else if (!PagePrivate(page) &&
1135 (bp->b_flags & _XBF_PAGE_CACHE)) {
1136 set_page_region(page, bvec->bv_offset, bvec->bv_len);
1139 if (--bvec >= bio->bi_io_vec)
1140 prefetchw(&bvec->bv_page->flags);
1142 if (_xfs_buf_iolocked(bp)) {
1145 } while (bvec >= bio->bi_io_vec);
1147 _xfs_buf_ioend(bp, 1);
1156 int i, rw, map_i, total_nr_pages, nr_pages;
1158 int offset = bp->b_offset;
1159 int size = bp->b_count_desired;
1160 sector_t sector = bp->b_bn;
1161 unsigned int blocksize = bp->b_target->bt_bsize;
1162 int locking = _xfs_buf_iolocked(bp);
1164 total_nr_pages = bp->b_page_count;
1167 if (bp->b_flags & XBF_ORDERED) {
1168 ASSERT(!(bp->b_flags & XBF_READ));
1170 } else if (bp->b_flags & _XBF_RUN_QUEUES) {
1171 ASSERT(!(bp->b_flags & XBF_READ_AHEAD));
1172 bp->b_flags &= ~_XBF_RUN_QUEUES;
1173 rw = (bp->b_flags & XBF_WRITE) ? WRITE_SYNC : READ_SYNC;
1175 rw = (bp->b_flags & XBF_WRITE) ? WRITE :
1176 (bp->b_flags & XBF_READ_AHEAD) ? READA : READ;
1179 /* Special code path for reading a sub page size buffer in --
1180 * we populate up the whole page, and hence the other metadata
1181 * in the same page. This optimization is only valid when the
1182 * filesystem block size is not smaller than the page size.
1184 if ((bp->b_buffer_length < PAGE_CACHE_SIZE) &&
1185 (bp->b_flags & XBF_READ) && locking &&
1186 (blocksize >= PAGE_CACHE_SIZE)) {
1187 bio = bio_alloc(GFP_NOIO, 1);
1189 bio->bi_bdev = bp->b_target->bt_bdev;
1190 bio->bi_sector = sector - (offset >> BBSHIFT);
1191 bio->bi_end_io = xfs_buf_bio_end_io;
1192 bio->bi_private = bp;
1194 bio_add_page(bio, bp->b_pages[0], PAGE_CACHE_SIZE, 0);
1197 atomic_inc(&bp->b_io_remaining);
1202 /* Lock down the pages which we need to for the request */
1203 if (locking && (bp->b_flags & XBF_WRITE) && (bp->b_locked == 0)) {
1204 for (i = 0; size; i++) {
1205 int nbytes = PAGE_CACHE_SIZE - offset;
1206 struct page *page = bp->b_pages[i];
1216 offset = bp->b_offset;
1217 size = bp->b_count_desired;
1221 atomic_inc(&bp->b_io_remaining);
1222 nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
1223 if (nr_pages > total_nr_pages)
1224 nr_pages = total_nr_pages;
1226 bio = bio_alloc(GFP_NOIO, nr_pages);
1227 bio->bi_bdev = bp->b_target->bt_bdev;
1228 bio->bi_sector = sector;
1229 bio->bi_end_io = xfs_buf_bio_end_io;
1230 bio->bi_private = bp;
1232 for (; size && nr_pages; nr_pages--, map_i++) {
1233 int rbytes, nbytes = PAGE_CACHE_SIZE - offset;
1238 rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset);
1239 if (rbytes < nbytes)
1243 sector += nbytes >> BBSHIFT;
1249 if (likely(bio->bi_size)) {
1250 submit_bio(rw, bio);
1255 xfs_buf_ioerror(bp, EIO);
1263 XB_TRACE(bp, "iorequest", 0);
1265 if (bp->b_flags & XBF_DELWRI) {
1266 xfs_buf_delwri_queue(bp, 1);
1270 if (bp->b_flags & XBF_WRITE) {
1271 xfs_buf_wait_unpin(bp);
1276 /* Set the count to 1 initially, this will stop an I/O
1277 * completion callout which happens before we have started
1278 * all the I/O from calling xfs_buf_ioend too early.
1280 atomic_set(&bp->b_io_remaining, 1);
1281 _xfs_buf_ioapply(bp);
1282 _xfs_buf_ioend(bp, 0);
1289 * Waits for I/O to complete on the buffer supplied.
1290 * It returns immediately if no I/O is pending.
1291 * It returns the I/O error code, if any, or 0 if there was no error.
1297 XB_TRACE(bp, "iowait", 0);
1298 if (atomic_read(&bp->b_io_remaining))
1299 blk_run_address_space(bp->b_target->bt_mapping);
1300 down(&bp->b_iodonesema);
1301 XB_TRACE(bp, "iowaited", (long)bp->b_error);
1312 if (bp->b_flags & XBF_MAPPED)
1313 return XFS_BUF_PTR(bp) + offset;
1315 offset += bp->b_offset;
1316 page = bp->b_pages[offset >> PAGE_CACHE_SHIFT];
1317 return (xfs_caddr_t)page_address(page) + (offset & (PAGE_CACHE_SIZE-1));
1321 * Move data into or out of a buffer.
1325 xfs_buf_t *bp, /* buffer to process */
1326 size_t boff, /* starting buffer offset */
1327 size_t bsize, /* length to copy */
1328 caddr_t data, /* data address */
1329 xfs_buf_rw_t mode) /* read/write/zero flag */
1331 size_t bend, cpoff, csize;
1334 bend = boff + bsize;
1335 while (boff < bend) {
1336 page = bp->b_pages[xfs_buf_btoct(boff + bp->b_offset)];
1337 cpoff = xfs_buf_poff(boff + bp->b_offset);
1338 csize = min_t(size_t,
1339 PAGE_CACHE_SIZE-cpoff, bp->b_count_desired-boff);
1341 ASSERT(((csize + cpoff) <= PAGE_CACHE_SIZE));
1345 memset(page_address(page) + cpoff, 0, csize);
1348 memcpy(data, page_address(page) + cpoff, csize);
1351 memcpy(page_address(page) + cpoff, data, csize);
1360 * Handling of buffer targets (buftargs).
1364 * Wait for any bufs with callbacks that have been submitted but
1365 * have not yet returned... walk the hash list for the target.
1372 xfs_bufhash_t *hash;
1375 for (i = 0; i < (1 << btp->bt_hashshift); i++) {
1376 hash = &btp->bt_hash[i];
1378 spin_lock(&hash->bh_lock);
1379 list_for_each_entry_safe(bp, n, &hash->bh_list, b_hash_list) {
1380 ASSERT(btp == bp->b_target);
1381 if (!(bp->b_flags & XBF_FS_MANAGED)) {
1382 spin_unlock(&hash->bh_lock);
1384 * Catch superblock reference count leaks
1387 BUG_ON(bp->b_bn == 0);
1392 spin_unlock(&hash->bh_lock);
1397 * Allocate buffer hash table for a given target.
1398 * For devices containing metadata (i.e. not the log/realtime devices)
1399 * we need to allocate a much larger hash table.
1408 btp->bt_hashshift = external ? 3 : 8; /* 8 or 256 buckets */
1409 btp->bt_hashmask = (1 << btp->bt_hashshift) - 1;
1410 btp->bt_hash = kmem_zalloc((1 << btp->bt_hashshift) *
1411 sizeof(xfs_bufhash_t), KM_SLEEP | KM_LARGE);
1412 for (i = 0; i < (1 << btp->bt_hashshift); i++) {
1413 spin_lock_init(&btp->bt_hash[i].bh_lock);
1414 INIT_LIST_HEAD(&btp->bt_hash[i].bh_list);
1422 kmem_free(btp->bt_hash, (1<<btp->bt_hashshift) * sizeof(xfs_bufhash_t));
1423 btp->bt_hash = NULL;
1427 * buftarg list for delwrite queue processing
1429 LIST_HEAD(xfs_buftarg_list);
1430 static DEFINE_SPINLOCK(xfs_buftarg_lock);
1433 xfs_register_buftarg(
1436 spin_lock(&xfs_buftarg_lock);
1437 list_add(&btp->bt_list, &xfs_buftarg_list);
1438 spin_unlock(&xfs_buftarg_lock);
1442 xfs_unregister_buftarg(
1445 spin_lock(&xfs_buftarg_lock);
1446 list_del(&btp->bt_list);
1447 spin_unlock(&xfs_buftarg_lock);
1455 xfs_flush_buftarg(btp, 1);
1457 xfs_blkdev_put(btp->bt_bdev);
1458 xfs_free_bufhash(btp);
1459 iput(btp->bt_mapping->host);
1461 /* Unregister the buftarg first so that we don't get a
1462 * wakeup finding a non-existent task
1464 xfs_unregister_buftarg(btp);
1465 kthread_stop(btp->bt_task);
1467 kmem_free(btp, sizeof(*btp));
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)) {
1483 "XFS: Cannot set_blocksize to %u on device %s\n",
1484 sectorsize, XFS_BUFTARG_NAME(btp));
1489 (PAGE_CACHE_SIZE / BITS_PER_LONG) > sectorsize) {
1491 "XFS: %u byte sectors in use on device %s. "
1492 "This is suboptimal; %u or greater is ideal.\n",
1493 sectorsize, XFS_BUFTARG_NAME(btp),
1494 (unsigned int)PAGE_CACHE_SIZE / BITS_PER_LONG);
1501 * When allocating the initial buffer target we have not yet
1502 * read in the superblock, so don't know what sized sectors
1503 * are being used is at this early stage. Play safe.
1506 xfs_setsize_buftarg_early(
1508 struct block_device *bdev)
1510 return xfs_setsize_buftarg_flags(btp,
1511 PAGE_CACHE_SIZE, bdev_hardsect_size(bdev), 0);
1515 xfs_setsize_buftarg(
1517 unsigned int blocksize,
1518 unsigned int sectorsize)
1520 return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
1524 xfs_mapping_buftarg(
1526 struct block_device *bdev)
1528 struct backing_dev_info *bdi;
1529 struct inode *inode;
1530 struct address_space *mapping;
1531 static const struct address_space_operations mapping_aops = {
1532 .sync_page = block_sync_page,
1533 .migratepage = fail_migrate_page,
1536 inode = new_inode(bdev->bd_inode->i_sb);
1539 "XFS: Cannot allocate mapping inode for device %s\n",
1540 XFS_BUFTARG_NAME(btp));
1543 inode->i_mode = S_IFBLK;
1544 inode->i_bdev = bdev;
1545 inode->i_rdev = bdev->bd_dev;
1546 bdi = blk_get_backing_dev_info(bdev);
1548 bdi = &default_backing_dev_info;
1549 mapping = &inode->i_data;
1550 mapping->a_ops = &mapping_aops;
1551 mapping->backing_dev_info = bdi;
1552 mapping_set_gfp_mask(mapping, GFP_NOFS);
1553 btp->bt_mapping = mapping;
1558 xfs_alloc_delwrite_queue(
1563 INIT_LIST_HEAD(&btp->bt_list);
1564 INIT_LIST_HEAD(&btp->bt_delwrite_queue);
1565 spinlock_init(&btp->bt_delwrite_lock, "delwri_lock");
1567 btp->bt_task = kthread_run(xfsbufd, btp, "xfsbufd");
1568 if (IS_ERR(btp->bt_task)) {
1569 error = PTR_ERR(btp->bt_task);
1572 xfs_register_buftarg(btp);
1579 struct block_device *bdev,
1584 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
1586 btp->bt_dev = bdev->bd_dev;
1587 btp->bt_bdev = bdev;
1588 if (xfs_setsize_buftarg_early(btp, bdev))
1590 if (xfs_mapping_buftarg(btp, bdev))
1592 if (xfs_alloc_delwrite_queue(btp))
1594 xfs_alloc_bufhash(btp, external);
1598 kmem_free(btp, sizeof(*btp));
1604 * Delayed write buffer handling
1607 xfs_buf_delwri_queue(
1611 struct list_head *dwq = &bp->b_target->bt_delwrite_queue;
1612 spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
1614 XB_TRACE(bp, "delwri_q", (long)unlock);
1615 ASSERT((bp->b_flags&(XBF_DELWRI|XBF_ASYNC)) == (XBF_DELWRI|XBF_ASYNC));
1618 /* If already in the queue, dequeue and place at tail */
1619 if (!list_empty(&bp->b_list)) {
1620 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1622 atomic_dec(&bp->b_hold);
1623 list_del(&bp->b_list);
1626 bp->b_flags |= _XBF_DELWRI_Q;
1627 list_add_tail(&bp->b_list, dwq);
1628 bp->b_queuetime = jiffies;
1636 xfs_buf_delwri_dequeue(
1639 spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
1643 if ((bp->b_flags & XBF_DELWRI) && !list_empty(&bp->b_list)) {
1644 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1645 list_del_init(&bp->b_list);
1648 bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q);
1654 XB_TRACE(bp, "delwri_dq", (long)dequeued);
1658 xfs_buf_runall_queues(
1659 struct workqueue_struct *queue)
1661 flush_workqueue(queue);
1671 spin_lock(&xfs_buftarg_lock);
1672 list_for_each_entry(btp, &xfs_buftarg_list, bt_list) {
1673 if (test_bit(XBT_FORCE_SLEEP, &btp->bt_flags))
1675 set_bit(XBT_FORCE_FLUSH, &btp->bt_flags);
1676 wake_up_process(btp->bt_task);
1678 spin_unlock(&xfs_buftarg_lock);
1683 * Move as many buffers as specified to the supplied list
1684 * idicating if we skipped any buffers to prevent deadlocks.
1687 xfs_buf_delwri_split(
1688 xfs_buftarg_t *target,
1689 struct list_head *list,
1693 struct list_head *dwq = &target->bt_delwrite_queue;
1694 spinlock_t *dwlk = &target->bt_delwrite_lock;
1698 force = test_and_clear_bit(XBT_FORCE_FLUSH, &target->bt_flags);
1699 INIT_LIST_HEAD(list);
1701 list_for_each_entry_safe(bp, n, dwq, b_list) {
1702 XB_TRACE(bp, "walkq1", (long)xfs_buf_ispin(bp));
1703 ASSERT(bp->b_flags & XBF_DELWRI);
1705 if (!xfs_buf_ispin(bp) && !xfs_buf_cond_lock(bp)) {
1707 time_before(jiffies, bp->b_queuetime + age)) {
1712 bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q|
1714 bp->b_flags |= XBF_WRITE;
1715 list_move_tail(&bp->b_list, list);
1729 struct list_head tmp;
1730 xfs_buftarg_t *target = (xfs_buftarg_t *)data;
1734 current->flags |= PF_MEMALLOC;
1737 if (unlikely(freezing(current))) {
1738 set_bit(XBT_FORCE_SLEEP, &target->bt_flags);
1741 clear_bit(XBT_FORCE_SLEEP, &target->bt_flags);
1744 schedule_timeout_interruptible(
1745 xfs_buf_timer_centisecs * msecs_to_jiffies(10));
1747 xfs_buf_delwri_split(target, &tmp,
1748 xfs_buf_age_centisecs * msecs_to_jiffies(10));
1751 while (!list_empty(&tmp)) {
1752 bp = list_entry(tmp.next, xfs_buf_t, b_list);
1753 ASSERT(target == bp->b_target);
1755 list_del_init(&bp->b_list);
1756 xfs_buf_iostrategy(bp);
1760 if (as_list_len > 0)
1763 blk_run_address_space(target->bt_mapping);
1765 } while (!kthread_should_stop());
1771 * Go through all incore buffers, and release buffers if they belong to
1772 * the given device. This is used in filesystem error handling to
1773 * preserve the consistency of its metadata.
1777 xfs_buftarg_t *target,
1780 struct list_head tmp;
1784 xfs_buf_runall_queues(xfsdatad_workqueue);
1785 xfs_buf_runall_queues(xfslogd_workqueue);
1787 set_bit(XBT_FORCE_FLUSH, &target->bt_flags);
1788 pincount = xfs_buf_delwri_split(target, &tmp, 0);
1791 * Dropped the delayed write list lock, now walk the temporary list
1793 list_for_each_entry_safe(bp, n, &tmp, b_list) {
1794 ASSERT(target == bp->b_target);
1796 bp->b_flags &= ~XBF_ASYNC;
1798 list_del_init(&bp->b_list);
1800 xfs_buf_iostrategy(bp);
1804 blk_run_address_space(target->bt_mapping);
1807 * Remaining list items must be flushed before returning
1809 while (!list_empty(&tmp)) {
1810 bp = list_entry(tmp.next, xfs_buf_t, b_list);
1812 list_del_init(&bp->b_list);
1823 #ifdef XFS_BUF_TRACE
1824 xfs_buf_trace_buf = ktrace_alloc(XFS_BUF_TRACE_SIZE, KM_SLEEP);
1827 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
1828 KM_ZONE_HWALIGN, NULL);
1830 goto out_free_trace_buf;
1832 xfslogd_workqueue = create_freezeable_workqueue("xfslogd");
1833 if (!xfslogd_workqueue)
1834 goto out_free_buf_zone;
1836 xfsdatad_workqueue = create_freezeable_workqueue("xfsdatad");
1837 if (!xfsdatad_workqueue)
1838 goto out_destroy_xfslogd_workqueue;
1840 xfs_buf_shake = kmem_shake_register(xfsbufd_wakeup);
1842 goto out_destroy_xfsdatad_workqueue;
1846 out_destroy_xfsdatad_workqueue:
1847 destroy_workqueue(xfsdatad_workqueue);
1848 out_destroy_xfslogd_workqueue:
1849 destroy_workqueue(xfslogd_workqueue);
1851 kmem_zone_destroy(xfs_buf_zone);
1853 #ifdef XFS_BUF_TRACE
1854 ktrace_free(xfs_buf_trace_buf);
1860 xfs_buf_terminate(void)
1862 kmem_shake_deregister(xfs_buf_shake);
1863 destroy_workqueue(xfsdatad_workqueue);
1864 destroy_workqueue(xfslogd_workqueue);
1865 kmem_zone_destroy(xfs_buf_zone);
1866 #ifdef XFS_BUF_TRACE
1867 ktrace_free(xfs_buf_trace_buf);