2 * Copyright (c) 2000-2005 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
18 #include <linux/stddef.h>
19 #include <linux/errno.h>
20 #include <linux/slab.h>
21 #include <linux/pagemap.h>
22 #include <linux/init.h>
23 #include <linux/vmalloc.h>
24 #include <linux/bio.h>
25 #include <linux/sysctl.h>
26 #include <linux/proc_fs.h>
27 #include <linux/workqueue.h>
28 #include <linux/percpu.h>
29 #include <linux/blkdev.h>
30 #include <linux/hash.h>
31 #include <linux/kthread.h>
32 #include "xfs_linux.h"
34 STATIC kmem_cache_t *pagebuf_zone;
35 STATIC kmem_shaker_t pagebuf_shake;
36 STATIC int xfsbufd_wakeup(int, gfp_t);
37 STATIC void pagebuf_delwri_queue(xfs_buf_t *, int);
39 STATIC struct workqueue_struct *xfslogd_workqueue;
40 struct workqueue_struct *xfsdatad_workqueue;
50 ktrace_enter(pagebuf_trace_buf,
52 (void *)(unsigned long)pb->pb_flags,
53 (void *)(unsigned long)pb->pb_hold.counter,
54 (void *)(unsigned long)pb->pb_sema.count.counter,
57 (void *)(unsigned long)((pb->pb_file_offset>>32) & 0xffffffff),
58 (void *)(unsigned long)(pb->pb_file_offset & 0xffffffff),
59 (void *)(unsigned long)pb->pb_buffer_length,
60 NULL, NULL, NULL, NULL, NULL);
62 ktrace_t *pagebuf_trace_buf;
63 #define PAGEBUF_TRACE_SIZE 4096
64 #define PB_TRACE(pb, id, data) \
65 pagebuf_trace(pb, id, (void *)data, (void *)__builtin_return_address(0))
67 #define PB_TRACE(pb, id, data) do { } while (0)
70 #ifdef PAGEBUF_LOCK_TRACKING
71 # define PB_SET_OWNER(pb) ((pb)->pb_last_holder = current->pid)
72 # define PB_CLEAR_OWNER(pb) ((pb)->pb_last_holder = -1)
73 # define PB_GET_OWNER(pb) ((pb)->pb_last_holder)
75 # define PB_SET_OWNER(pb) do { } while (0)
76 # define PB_CLEAR_OWNER(pb) do { } while (0)
77 # define PB_GET_OWNER(pb) do { } while (0)
80 #define pb_to_gfp(flags) \
81 ((((flags) & PBF_READ_AHEAD) ? __GFP_NORETRY : \
82 ((flags) & PBF_DONT_BLOCK) ? GFP_NOFS : GFP_KERNEL) | __GFP_NOWARN)
84 #define pb_to_km(flags) \
85 (((flags) & PBF_DONT_BLOCK) ? KM_NOFS : KM_SLEEP)
87 #define pagebuf_allocate(flags) \
88 kmem_zone_alloc(pagebuf_zone, pb_to_km(flags))
89 #define pagebuf_deallocate(pb) \
90 kmem_zone_free(pagebuf_zone, (pb));
93 * Page Region interfaces.
95 * For pages in filesystems where the blocksize is smaller than the
96 * pagesize, we use the page->private field (long) to hold a bitmap
97 * of uptodate regions within the page.
99 * Each such region is "bytes per page / bits per long" bytes long.
101 * NBPPR == number-of-bytes-per-page-region
102 * BTOPR == bytes-to-page-region (rounded up)
103 * BTOPRT == bytes-to-page-region-truncated (rounded down)
105 #if (BITS_PER_LONG == 32)
106 #define PRSHIFT (PAGE_CACHE_SHIFT - 5) /* (32 == 1<<5) */
107 #elif (BITS_PER_LONG == 64)
108 #define PRSHIFT (PAGE_CACHE_SHIFT - 6) /* (64 == 1<<6) */
110 #error BITS_PER_LONG must be 32 or 64
112 #define NBPPR (PAGE_CACHE_SIZE/BITS_PER_LONG)
113 #define BTOPR(b) (((unsigned int)(b) + (NBPPR - 1)) >> PRSHIFT)
114 #define BTOPRT(b) (((unsigned int)(b) >> PRSHIFT))
124 first = BTOPR(offset);
125 final = BTOPRT(offset + length - 1);
126 first = min(first, final);
129 mask <<= BITS_PER_LONG - (final - first);
130 mask >>= BITS_PER_LONG - (final);
132 ASSERT(offset + length <= PAGE_CACHE_SIZE);
133 ASSERT((final - first) < BITS_PER_LONG && (final - first) >= 0);
144 set_page_private(page,
145 page_private(page) | page_region_mask(offset, length));
146 if (page_private(page) == ~0UL)
147 SetPageUptodate(page);
156 unsigned long mask = page_region_mask(offset, length);
158 return (mask && (page_private(page) & mask) == mask);
162 * Mapping of multi-page buffers into contiguous virtual space
165 typedef struct a_list {
170 STATIC a_list_t *as_free_head;
171 STATIC int as_list_len;
172 STATIC DEFINE_SPINLOCK(as_lock);
175 * Try to batch vunmaps because they are costly.
183 aentry = kmalloc(sizeof(a_list_t), GFP_ATOMIC & ~__GFP_HIGH);
184 if (likely(aentry)) {
186 aentry->next = as_free_head;
187 aentry->vm_addr = addr;
188 as_free_head = aentry;
190 spin_unlock(&as_lock);
197 purge_addresses(void)
199 a_list_t *aentry, *old;
201 if (as_free_head == NULL)
205 aentry = as_free_head;
208 spin_unlock(&as_lock);
210 while ((old = aentry) != NULL) {
211 vunmap(aentry->vm_addr);
212 aentry = aentry->next;
218 * Internal pagebuf object manipulation
224 xfs_buftarg_t *target,
227 page_buf_flags_t flags)
230 * We don't want certain flags to appear in pb->pb_flags.
232 flags &= ~(PBF_LOCK|PBF_MAPPED|PBF_DONT_BLOCK|PBF_READ_AHEAD);
234 memset(pb, 0, sizeof(xfs_buf_t));
235 atomic_set(&pb->pb_hold, 1);
236 init_MUTEX_LOCKED(&pb->pb_iodonesema);
237 INIT_LIST_HEAD(&pb->pb_list);
238 INIT_LIST_HEAD(&pb->pb_hash_list);
239 init_MUTEX_LOCKED(&pb->pb_sema); /* held, no waiters */
241 pb->pb_target = target;
242 pb->pb_file_offset = range_base;
244 * Set buffer_length and count_desired to the same value initially.
245 * I/O routines should use count_desired, which will be the same in
246 * most cases but may be reset (e.g. XFS recovery).
248 pb->pb_buffer_length = pb->pb_count_desired = range_length;
249 pb->pb_flags = flags;
250 pb->pb_bn = XFS_BUF_DADDR_NULL;
251 atomic_set(&pb->pb_pin_count, 0);
252 init_waitqueue_head(&pb->pb_waiters);
254 XFS_STATS_INC(pb_create);
255 PB_TRACE(pb, "initialize", target);
259 * Allocate a page array capable of holding a specified number
260 * of pages, and point the page buf at it.
266 page_buf_flags_t flags)
268 /* Make sure that we have a page list */
269 if (pb->pb_pages == NULL) {
270 pb->pb_offset = page_buf_poff(pb->pb_file_offset);
271 pb->pb_page_count = page_count;
272 if (page_count <= PB_PAGES) {
273 pb->pb_pages = pb->pb_page_array;
275 pb->pb_pages = kmem_alloc(sizeof(struct page *) *
276 page_count, pb_to_km(flags));
277 if (pb->pb_pages == NULL)
280 memset(pb->pb_pages, 0, sizeof(struct page *) * page_count);
286 * Frees pb_pages if it was malloced.
292 if (bp->pb_pages != bp->pb_page_array) {
293 kmem_free(bp->pb_pages,
294 bp->pb_page_count * sizeof(struct page *));
299 * Releases the specified buffer.
301 * The modification state of any associated pages is left unchanged.
302 * The buffer most not be on any hash - use pagebuf_rele instead for
303 * hashed and refcounted buffers
309 PB_TRACE(bp, "free", 0);
311 ASSERT(list_empty(&bp->pb_hash_list));
313 if (bp->pb_flags & _PBF_PAGE_CACHE) {
316 if ((bp->pb_flags & PBF_MAPPED) && (bp->pb_page_count > 1))
317 free_address(bp->pb_addr - bp->pb_offset);
319 for (i = 0; i < bp->pb_page_count; i++)
320 page_cache_release(bp->pb_pages[i]);
321 _pagebuf_free_pages(bp);
322 } else if (bp->pb_flags & _PBF_KMEM_ALLOC) {
324 * XXX(hch): bp->pb_count_desired might be incorrect (see
325 * pagebuf_associate_memory for details), but fortunately
326 * the Linux version of kmem_free ignores the len argument..
328 kmem_free(bp->pb_addr, bp->pb_count_desired);
329 _pagebuf_free_pages(bp);
332 pagebuf_deallocate(bp);
336 * Finds all pages for buffer in question and builds it's page list.
339 _pagebuf_lookup_pages(
343 struct address_space *mapping = bp->pb_target->pbr_mapping;
344 size_t blocksize = bp->pb_target->pbr_bsize;
345 size_t size = bp->pb_count_desired;
346 size_t nbytes, offset;
347 gfp_t gfp_mask = pb_to_gfp(flags);
348 unsigned short page_count, i;
353 end = bp->pb_file_offset + bp->pb_buffer_length;
354 page_count = page_buf_btoc(end) - page_buf_btoct(bp->pb_file_offset);
356 error = _pagebuf_get_pages(bp, page_count, flags);
359 bp->pb_flags |= _PBF_PAGE_CACHE;
361 offset = bp->pb_offset;
362 first = bp->pb_file_offset >> PAGE_CACHE_SHIFT;
364 for (i = 0; i < bp->pb_page_count; i++) {
369 page = find_or_create_page(mapping, first + i, gfp_mask);
370 if (unlikely(page == NULL)) {
371 if (flags & PBF_READ_AHEAD) {
372 bp->pb_page_count = i;
373 for (i = 0; i < bp->pb_page_count; i++)
374 unlock_page(bp->pb_pages[i]);
379 * This could deadlock.
381 * But until all the XFS lowlevel code is revamped to
382 * handle buffer allocation failures we can't do much.
384 if (!(++retries % 100))
386 "XFS: possible memory allocation "
387 "deadlock in %s (mode:0x%x)\n",
388 __FUNCTION__, gfp_mask);
390 XFS_STATS_INC(pb_page_retries);
391 xfsbufd_wakeup(0, gfp_mask);
392 blk_congestion_wait(WRITE, HZ/50);
396 XFS_STATS_INC(pb_page_found);
398 nbytes = min_t(size_t, size, PAGE_CACHE_SIZE - offset);
401 if (!PageUptodate(page)) {
403 if (blocksize >= PAGE_CACHE_SIZE) {
404 if (flags & PBF_READ)
406 } else if (!PagePrivate(page)) {
407 if (test_page_region(page, offset, nbytes))
412 bp->pb_pages[i] = page;
416 if (!bp->pb_locked) {
417 for (i = 0; i < bp->pb_page_count; i++)
418 unlock_page(bp->pb_pages[i]);
421 if (page_count == bp->pb_page_count)
422 bp->pb_flags |= PBF_DONE;
424 PB_TRACE(bp, "lookup_pages", (long)page_count);
429 * Map buffer into kernel address-space if nessecary.
436 /* A single page buffer is always mappable */
437 if (bp->pb_page_count == 1) {
438 bp->pb_addr = page_address(bp->pb_pages[0]) + bp->pb_offset;
439 bp->pb_flags |= PBF_MAPPED;
440 } else if (flags & PBF_MAPPED) {
441 if (as_list_len > 64)
443 bp->pb_addr = vmap(bp->pb_pages, bp->pb_page_count,
444 VM_MAP, PAGE_KERNEL);
445 if (unlikely(bp->pb_addr == NULL))
447 bp->pb_addr += bp->pb_offset;
448 bp->pb_flags |= PBF_MAPPED;
455 * Finding and Reading Buffers
461 * Looks up, and creates if absent, a lockable buffer for
462 * a given range of an inode. The buffer is returned
463 * locked. If other overlapping buffers exist, they are
464 * released before the new buffer is created and locked,
465 * which may imply that this call will block until those buffers
466 * are unlocked. No I/O is implied by this call.
470 xfs_buftarg_t *btp, /* block device target */
471 loff_t ioff, /* starting offset of range */
472 size_t isize, /* length of range */
473 page_buf_flags_t flags, /* PBF_TRYLOCK */
474 xfs_buf_t *new_pb)/* newly allocated buffer */
481 range_base = (ioff << BBSHIFT);
482 range_length = (isize << BBSHIFT);
484 /* Check for IOs smaller than the sector size / not sector aligned */
485 ASSERT(!(range_length < (1 << btp->pbr_sshift)));
486 ASSERT(!(range_base & (loff_t)btp->pbr_smask));
488 hash = &btp->bt_hash[hash_long((unsigned long)ioff, btp->bt_hashshift)];
490 spin_lock(&hash->bh_lock);
492 list_for_each_entry_safe(pb, n, &hash->bh_list, pb_hash_list) {
493 ASSERT(btp == pb->pb_target);
494 if (pb->pb_file_offset == range_base &&
495 pb->pb_buffer_length == range_length) {
497 * If we look at something bring it to the
498 * front of the list for next time.
500 atomic_inc(&pb->pb_hold);
501 list_move(&pb->pb_hash_list, &hash->bh_list);
508 _pagebuf_initialize(new_pb, btp, range_base,
509 range_length, flags);
510 new_pb->pb_hash = hash;
511 list_add(&new_pb->pb_hash_list, &hash->bh_list);
513 XFS_STATS_INC(pb_miss_locked);
516 spin_unlock(&hash->bh_lock);
520 spin_unlock(&hash->bh_lock);
522 /* Attempt to get the semaphore without sleeping,
523 * if this does not work then we need to drop the
524 * spinlock and do a hard attempt on the semaphore.
526 if (down_trylock(&pb->pb_sema)) {
527 if (!(flags & PBF_TRYLOCK)) {
528 /* wait for buffer ownership */
529 PB_TRACE(pb, "get_lock", 0);
531 XFS_STATS_INC(pb_get_locked_waited);
533 /* We asked for a trylock and failed, no need
534 * to look at file offset and length here, we
535 * know that this pagebuf at least overlaps our
536 * pagebuf and is locked, therefore our buffer
537 * either does not exist, or is this buffer
541 XFS_STATS_INC(pb_busy_locked);
549 if (pb->pb_flags & PBF_STALE) {
550 ASSERT((pb->pb_flags & _PBF_DELWRI_Q) == 0);
551 pb->pb_flags &= PBF_MAPPED;
553 PB_TRACE(pb, "got_lock", 0);
554 XFS_STATS_INC(pb_get_locked);
559 * xfs_buf_get_flags assembles a buffer covering the specified range.
561 * Storage in memory for all portions of the buffer will be allocated,
562 * although backing storage may not be.
565 xfs_buf_get_flags( /* allocate a buffer */
566 xfs_buftarg_t *target,/* target for buffer */
567 loff_t ioff, /* starting offset of range */
568 size_t isize, /* length of range */
569 page_buf_flags_t flags) /* PBF_TRYLOCK */
571 xfs_buf_t *pb, *new_pb;
574 new_pb = pagebuf_allocate(flags);
575 if (unlikely(!new_pb))
578 pb = _pagebuf_find(target, ioff, isize, flags, new_pb);
580 error = _pagebuf_lookup_pages(pb, flags);
584 pagebuf_deallocate(new_pb);
585 if (unlikely(pb == NULL))
589 for (i = 0; i < pb->pb_page_count; i++)
590 mark_page_accessed(pb->pb_pages[i]);
592 if (!(pb->pb_flags & PBF_MAPPED)) {
593 error = _pagebuf_map_pages(pb, flags);
594 if (unlikely(error)) {
595 printk(KERN_WARNING "%s: failed to map pages\n",
601 XFS_STATS_INC(pb_get);
604 * Always fill in the block number now, the mapped cases can do
605 * their own overlay of this later.
608 pb->pb_count_desired = pb->pb_buffer_length;
610 PB_TRACE(pb, "get", (unsigned long)flags);
614 if (flags & (PBF_LOCK | PBF_TRYLOCK))
622 xfs_buftarg_t *target,
625 page_buf_flags_t flags)
631 pb = xfs_buf_get_flags(target, ioff, isize, flags);
633 if (!XFS_BUF_ISDONE(pb)) {
634 PB_TRACE(pb, "read", (unsigned long)flags);
635 XFS_STATS_INC(pb_get_read);
636 pagebuf_iostart(pb, flags);
637 } else if (flags & PBF_ASYNC) {
638 PB_TRACE(pb, "read_async", (unsigned long)flags);
640 * Read ahead call which is already satisfied,
645 PB_TRACE(pb, "read_done", (unsigned long)flags);
646 /* We do not want read in the flags */
647 pb->pb_flags &= ~PBF_READ;
654 if (flags & (PBF_LOCK | PBF_TRYLOCK))
661 * If we are not low on memory then do the readahead in a deadlock
666 xfs_buftarg_t *target,
669 page_buf_flags_t flags)
671 struct backing_dev_info *bdi;
673 bdi = target->pbr_mapping->backing_dev_info;
674 if (bdi_read_congested(bdi))
677 flags |= (PBF_TRYLOCK|PBF_ASYNC|PBF_READ_AHEAD);
678 xfs_buf_read_flags(target, ioff, isize, flags);
684 xfs_buftarg_t *target)
688 pb = pagebuf_allocate(0);
690 _pagebuf_initialize(pb, target, 0, len, 0);
694 static inline struct page *
698 if (((unsigned long)addr < VMALLOC_START) ||
699 ((unsigned long)addr >= VMALLOC_END)) {
700 return virt_to_page(addr);
702 return vmalloc_to_page(addr);
707 pagebuf_associate_memory(
719 page_count = PAGE_CACHE_ALIGN(len) >> PAGE_CACHE_SHIFT;
720 offset = (off_t) mem - ((off_t)mem & PAGE_CACHE_MASK);
721 if (offset && (len > PAGE_CACHE_SIZE))
724 /* Free any previous set of page pointers */
726 _pagebuf_free_pages(pb);
731 rval = _pagebuf_get_pages(pb, page_count, 0);
735 pb->pb_offset = offset;
736 ptr = (size_t) mem & PAGE_CACHE_MASK;
737 end = PAGE_CACHE_ALIGN((size_t) mem + len);
739 /* set up first page */
740 pb->pb_pages[0] = mem_to_page(mem);
742 ptr += PAGE_CACHE_SIZE;
743 pb->pb_page_count = ++i;
745 pb->pb_pages[i] = mem_to_page((void *)ptr);
746 pb->pb_page_count = ++i;
747 ptr += PAGE_CACHE_SIZE;
751 pb->pb_count_desired = pb->pb_buffer_length = len;
752 pb->pb_flags |= PBF_MAPPED;
758 pagebuf_get_no_daddr(
760 xfs_buftarg_t *target)
762 size_t malloc_len = len;
767 bp = pagebuf_allocate(0);
768 if (unlikely(bp == NULL))
770 _pagebuf_initialize(bp, target, 0, len, 0);
773 data = kmem_alloc(malloc_len, KM_SLEEP | KM_MAYFAIL);
774 if (unlikely(data == NULL))
777 /* check whether alignment matches.. */
778 if ((__psunsigned_t)data !=
779 ((__psunsigned_t)data & ~target->pbr_smask)) {
780 /* .. else double the size and try again */
781 kmem_free(data, malloc_len);
786 error = pagebuf_associate_memory(bp, data, len);
789 bp->pb_flags |= _PBF_KMEM_ALLOC;
793 PB_TRACE(bp, "no_daddr", data);
796 kmem_free(data, malloc_len);
806 * Increment reference count on buffer, to hold the buffer concurrently
807 * with another thread which may release (free) the buffer asynchronously.
809 * Must hold the buffer already to call this function.
815 atomic_inc(&pb->pb_hold);
816 PB_TRACE(pb, "hold", 0);
822 * pagebuf_rele releases a hold on the specified buffer. If the
823 * the hold count is 1, pagebuf_rele calls pagebuf_free.
829 xfs_bufhash_t *hash = pb->pb_hash;
831 PB_TRACE(pb, "rele", pb->pb_relse);
833 if (atomic_dec_and_lock(&pb->pb_hold, &hash->bh_lock)) {
835 atomic_inc(&pb->pb_hold);
836 spin_unlock(&hash->bh_lock);
837 (*(pb->pb_relse)) (pb);
838 } else if (pb->pb_flags & PBF_FS_MANAGED) {
839 spin_unlock(&hash->bh_lock);
841 ASSERT(!(pb->pb_flags & (PBF_DELWRI|_PBF_DELWRI_Q)));
842 list_del_init(&pb->pb_hash_list);
843 spin_unlock(&hash->bh_lock);
848 * Catch reference count leaks
850 ASSERT(atomic_read(&pb->pb_hold) >= 0);
856 * Mutual exclusion on buffers. Locking model:
858 * Buffers associated with inodes for which buffer locking
859 * is not enabled are not protected by semaphores, and are
860 * assumed to be exclusively owned by the caller. There is a
861 * spinlock in the buffer, used by the caller when concurrent
862 * access is possible.
868 * pagebuf_cond_lock locks a buffer object, if it is not already locked.
869 * Note that this in no way
870 * locks the underlying pages, so it is only useful for synchronizing
871 * concurrent use of page buffer objects, not for synchronizing independent
872 * access to the underlying pages.
875 pagebuf_cond_lock( /* lock buffer, if not locked */
876 /* returns -EBUSY if locked) */
881 locked = down_trylock(&pb->pb_sema) == 0;
885 PB_TRACE(pb, "cond_lock", (long)locked);
886 return(locked ? 0 : -EBUSY);
889 #if defined(DEBUG) || defined(XFS_BLI_TRACE)
893 * Return lock value for a pagebuf
899 return(atomic_read(&pb->pb_sema.count));
906 * pagebuf_lock locks a buffer object. Note that this in no way
907 * locks the underlying pages, so it is only useful for synchronizing
908 * concurrent use of page buffer objects, not for synchronizing independent
909 * access to the underlying pages.
915 PB_TRACE(pb, "lock", 0);
916 if (atomic_read(&pb->pb_io_remaining))
917 blk_run_address_space(pb->pb_target->pbr_mapping);
920 PB_TRACE(pb, "locked", 0);
927 * pagebuf_unlock releases the lock on the buffer object created by
928 * pagebuf_lock or pagebuf_cond_lock (not any pinning of underlying pages
929 * created by pagebuf_pin).
931 * If the buffer is marked delwri but is not queued, do so before we
932 * unlock the buffer as we need to set flags correctly. We also need to
933 * take a reference for the delwri queue because the unlocker is going to
934 * drop their's and they don't know we just queued it.
937 pagebuf_unlock( /* unlock buffer */
938 xfs_buf_t *pb) /* buffer to unlock */
940 if ((pb->pb_flags & (PBF_DELWRI|_PBF_DELWRI_Q)) == PBF_DELWRI) {
941 atomic_inc(&pb->pb_hold);
942 pb->pb_flags |= PBF_ASYNC;
943 pagebuf_delwri_queue(pb, 0);
948 PB_TRACE(pb, "unlock", 0);
953 * Pinning Buffer Storage in Memory
959 * pagebuf_pin locks all of the memory represented by a buffer in
960 * memory. Multiple calls to pagebuf_pin and pagebuf_unpin, for
961 * the same or different buffers affecting a given page, will
962 * properly count the number of outstanding "pin" requests. The
963 * buffer may be released after the pagebuf_pin and a different
964 * buffer used when calling pagebuf_unpin, if desired.
965 * pagebuf_pin should be used by the file system when it wants be
966 * assured that no attempt will be made to force the affected
967 * memory to disk. It does not assure that a given logical page
968 * will not be moved to a different physical page.
974 atomic_inc(&pb->pb_pin_count);
975 PB_TRACE(pb, "pin", (long)pb->pb_pin_count.counter);
981 * pagebuf_unpin reverses the locking of memory performed by
982 * pagebuf_pin. Note that both functions affected the logical
983 * pages associated with the buffer, not the buffer itself.
989 if (atomic_dec_and_test(&pb->pb_pin_count)) {
990 wake_up_all(&pb->pb_waiters);
992 PB_TRACE(pb, "unpin", (long)pb->pb_pin_count.counter);
999 return atomic_read(&pb->pb_pin_count);
1003 * pagebuf_wait_unpin
1005 * pagebuf_wait_unpin waits until all of the memory associated
1006 * with the buffer is not longer locked in memory. It returns
1007 * immediately if none of the affected pages are locked.
1010 _pagebuf_wait_unpin(
1013 DECLARE_WAITQUEUE (wait, current);
1015 if (atomic_read(&pb->pb_pin_count) == 0)
1018 add_wait_queue(&pb->pb_waiters, &wait);
1020 set_current_state(TASK_UNINTERRUPTIBLE);
1021 if (atomic_read(&pb->pb_pin_count) == 0)
1023 if (atomic_read(&pb->pb_io_remaining))
1024 blk_run_address_space(pb->pb_target->pbr_mapping);
1027 remove_wait_queue(&pb->pb_waiters, &wait);
1028 set_current_state(TASK_RUNNING);
1032 * Buffer Utility Routines
1038 * pagebuf_iodone marks a buffer for which I/O is in progress
1039 * done with respect to that I/O. The pb_iodone routine, if
1040 * present, will be called as a side-effect.
1043 pagebuf_iodone_work(
1046 xfs_buf_t *bp = (xfs_buf_t *)v;
1049 (*(bp->pb_iodone))(bp);
1050 else if (bp->pb_flags & PBF_ASYNC)
1059 pb->pb_flags &= ~(PBF_READ | PBF_WRITE);
1060 if (pb->pb_error == 0)
1061 pb->pb_flags |= PBF_DONE;
1063 PB_TRACE(pb, "iodone", pb->pb_iodone);
1065 if ((pb->pb_iodone) || (pb->pb_flags & PBF_ASYNC)) {
1067 INIT_WORK(&pb->pb_iodone_work, pagebuf_iodone_work, pb);
1068 queue_work(xfslogd_workqueue, &pb->pb_iodone_work);
1070 pagebuf_iodone_work(pb);
1073 up(&pb->pb_iodonesema);
1080 * pagebuf_ioerror sets the error code for a buffer.
1083 pagebuf_ioerror( /* mark/clear buffer error flag */
1084 xfs_buf_t *pb, /* buffer to mark */
1085 int error) /* error to store (0 if none) */
1087 ASSERT(error >= 0 && error <= 0xffff);
1088 pb->pb_error = (unsigned short)error;
1089 PB_TRACE(pb, "ioerror", (unsigned long)error);
1095 * pagebuf_iostart initiates I/O on a buffer, based on the flags supplied.
1096 * If necessary, it will arrange for any disk space allocation required,
1097 * and it will break up the request if the block mappings require it.
1098 * The pb_iodone routine in the buffer supplied will only be called
1099 * when all of the subsidiary I/O requests, if any, have been completed.
1100 * pagebuf_iostart calls the pagebuf_ioinitiate routine or
1101 * pagebuf_iorequest, if the former routine is not defined, to start
1102 * the I/O on a given low-level request.
1105 pagebuf_iostart( /* start I/O on a buffer */
1106 xfs_buf_t *pb, /* buffer to start */
1107 page_buf_flags_t flags) /* PBF_LOCK, PBF_ASYNC, PBF_READ, */
1108 /* PBF_WRITE, PBF_DELWRI, */
1109 /* PBF_DONT_BLOCK */
1113 PB_TRACE(pb, "iostart", (unsigned long)flags);
1115 if (flags & PBF_DELWRI) {
1116 pb->pb_flags &= ~(PBF_READ | PBF_WRITE | PBF_ASYNC);
1117 pb->pb_flags |= flags & (PBF_DELWRI | PBF_ASYNC);
1118 pagebuf_delwri_queue(pb, 1);
1122 pb->pb_flags &= ~(PBF_READ | PBF_WRITE | PBF_ASYNC | PBF_DELWRI | \
1123 PBF_READ_AHEAD | _PBF_RUN_QUEUES);
1124 pb->pb_flags |= flags & (PBF_READ | PBF_WRITE | PBF_ASYNC | \
1125 PBF_READ_AHEAD | _PBF_RUN_QUEUES);
1127 BUG_ON(pb->pb_bn == XFS_BUF_DADDR_NULL);
1129 /* For writes allow an alternate strategy routine to precede
1130 * the actual I/O request (which may not be issued at all in
1131 * a shutdown situation, for example).
1133 status = (flags & PBF_WRITE) ?
1134 pagebuf_iostrategy(pb) : pagebuf_iorequest(pb);
1136 /* Wait for I/O if we are not an async request.
1137 * Note: async I/O request completion will release the buffer,
1138 * and that can already be done by this point. So using the
1139 * buffer pointer from here on, after async I/O, is invalid.
1141 if (!status && !(flags & PBF_ASYNC))
1142 status = pagebuf_iowait(pb);
1148 * Helper routine for pagebuf_iorequest
1151 STATIC __inline__ int
1155 ASSERT(pb->pb_flags & (PBF_READ|PBF_WRITE));
1156 if (pb->pb_flags & PBF_READ)
1157 return pb->pb_locked;
1161 STATIC __inline__ void
1166 if (atomic_dec_and_test(&pb->pb_io_remaining) == 1) {
1168 pagebuf_iodone(pb, schedule);
1175 unsigned int bytes_done,
1178 xfs_buf_t *pb = (xfs_buf_t *)bio->bi_private;
1179 unsigned int blocksize = pb->pb_target->pbr_bsize;
1180 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1185 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
1189 struct page *page = bvec->bv_page;
1191 if (unlikely(pb->pb_error)) {
1192 if (pb->pb_flags & PBF_READ)
1193 ClearPageUptodate(page);
1195 } else if (blocksize == PAGE_CACHE_SIZE) {
1196 SetPageUptodate(page);
1197 } else if (!PagePrivate(page) &&
1198 (pb->pb_flags & _PBF_PAGE_CACHE)) {
1199 set_page_region(page, bvec->bv_offset, bvec->bv_len);
1202 if (--bvec >= bio->bi_io_vec)
1203 prefetchw(&bvec->bv_page->flags);
1205 if (_pagebuf_iolocked(pb)) {
1208 } while (bvec >= bio->bi_io_vec);
1210 _pagebuf_iodone(pb, 1);
1219 int i, rw, map_i, total_nr_pages, nr_pages;
1221 int offset = pb->pb_offset;
1222 int size = pb->pb_count_desired;
1223 sector_t sector = pb->pb_bn;
1224 unsigned int blocksize = pb->pb_target->pbr_bsize;
1225 int locking = _pagebuf_iolocked(pb);
1227 total_nr_pages = pb->pb_page_count;
1230 if (pb->pb_flags & _PBF_RUN_QUEUES) {
1231 pb->pb_flags &= ~_PBF_RUN_QUEUES;
1232 rw = (pb->pb_flags & PBF_READ) ? READ_SYNC : WRITE_SYNC;
1234 rw = (pb->pb_flags & PBF_READ) ? READ : WRITE;
1237 if (pb->pb_flags & PBF_ORDERED) {
1238 ASSERT(!(pb->pb_flags & PBF_READ));
1242 /* Special code path for reading a sub page size pagebuf in --
1243 * we populate up the whole page, and hence the other metadata
1244 * in the same page. This optimization is only valid when the
1245 * filesystem block size and the page size are equal.
1247 if ((pb->pb_buffer_length < PAGE_CACHE_SIZE) &&
1248 (pb->pb_flags & PBF_READ) && locking &&
1249 (blocksize == PAGE_CACHE_SIZE)) {
1250 bio = bio_alloc(GFP_NOIO, 1);
1252 bio->bi_bdev = pb->pb_target->pbr_bdev;
1253 bio->bi_sector = sector - (offset >> BBSHIFT);
1254 bio->bi_end_io = bio_end_io_pagebuf;
1255 bio->bi_private = pb;
1257 bio_add_page(bio, pb->pb_pages[0], PAGE_CACHE_SIZE, 0);
1260 atomic_inc(&pb->pb_io_remaining);
1265 /* Lock down the pages which we need to for the request */
1266 if (locking && (pb->pb_flags & PBF_WRITE) && (pb->pb_locked == 0)) {
1267 for (i = 0; size; i++) {
1268 int nbytes = PAGE_CACHE_SIZE - offset;
1269 struct page *page = pb->pb_pages[i];
1279 offset = pb->pb_offset;
1280 size = pb->pb_count_desired;
1284 atomic_inc(&pb->pb_io_remaining);
1285 nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
1286 if (nr_pages > total_nr_pages)
1287 nr_pages = total_nr_pages;
1289 bio = bio_alloc(GFP_NOIO, nr_pages);
1290 bio->bi_bdev = pb->pb_target->pbr_bdev;
1291 bio->bi_sector = sector;
1292 bio->bi_end_io = bio_end_io_pagebuf;
1293 bio->bi_private = pb;
1295 for (; size && nr_pages; nr_pages--, map_i++) {
1296 int nbytes = PAGE_CACHE_SIZE - offset;
1301 if (bio_add_page(bio, pb->pb_pages[map_i],
1302 nbytes, offset) < nbytes)
1306 sector += nbytes >> BBSHIFT;
1312 if (likely(bio->bi_size)) {
1313 submit_bio(rw, bio);
1318 pagebuf_ioerror(pb, EIO);
1323 * pagebuf_iorequest -- the core I/O request routine.
1326 pagebuf_iorequest( /* start real I/O */
1327 xfs_buf_t *pb) /* buffer to convey to device */
1329 PB_TRACE(pb, "iorequest", 0);
1331 if (pb->pb_flags & PBF_DELWRI) {
1332 pagebuf_delwri_queue(pb, 1);
1336 if (pb->pb_flags & PBF_WRITE) {
1337 _pagebuf_wait_unpin(pb);
1342 /* Set the count to 1 initially, this will stop an I/O
1343 * completion callout which happens before we have started
1344 * all the I/O from calling pagebuf_iodone too early.
1346 atomic_set(&pb->pb_io_remaining, 1);
1347 _pagebuf_ioapply(pb);
1348 _pagebuf_iodone(pb, 0);
1357 * pagebuf_iowait waits for I/O to complete on the buffer supplied.
1358 * It returns immediately if no I/O is pending. In any case, it returns
1359 * the error code, if any, or 0 if there is no error.
1365 PB_TRACE(pb, "iowait", 0);
1366 if (atomic_read(&pb->pb_io_remaining))
1367 blk_run_address_space(pb->pb_target->pbr_mapping);
1368 down(&pb->pb_iodonesema);
1369 PB_TRACE(pb, "iowaited", (long)pb->pb_error);
1370 return pb->pb_error;
1380 offset += pb->pb_offset;
1382 page = pb->pb_pages[offset >> PAGE_CACHE_SHIFT];
1383 return (caddr_t) page_address(page) + (offset & (PAGE_CACHE_SIZE - 1));
1389 * Move data into or out of a buffer.
1393 xfs_buf_t *pb, /* buffer to process */
1394 size_t boff, /* starting buffer offset */
1395 size_t bsize, /* length to copy */
1396 caddr_t data, /* data address */
1397 page_buf_rw_t mode) /* read/write flag */
1399 size_t bend, cpoff, csize;
1402 bend = boff + bsize;
1403 while (boff < bend) {
1404 page = pb->pb_pages[page_buf_btoct(boff + pb->pb_offset)];
1405 cpoff = page_buf_poff(boff + pb->pb_offset);
1406 csize = min_t(size_t,
1407 PAGE_CACHE_SIZE-cpoff, pb->pb_count_desired-boff);
1409 ASSERT(((csize + cpoff) <= PAGE_CACHE_SIZE));
1413 memset(page_address(page) + cpoff, 0, csize);
1416 memcpy(data, page_address(page) + cpoff, csize);
1419 memcpy(page_address(page) + cpoff, data, csize);
1428 * Handling of buftargs.
1432 * Wait for any bufs with callbacks that have been submitted but
1433 * have not yet returned... walk the hash list for the target.
1440 xfs_bufhash_t *hash;
1443 for (i = 0; i < (1 << btp->bt_hashshift); i++) {
1444 hash = &btp->bt_hash[i];
1446 spin_lock(&hash->bh_lock);
1447 list_for_each_entry_safe(bp, n, &hash->bh_list, pb_hash_list) {
1448 ASSERT(btp == bp->pb_target);
1449 if (!(bp->pb_flags & PBF_FS_MANAGED)) {
1450 spin_unlock(&hash->bh_lock);
1452 * Catch superblock reference count leaks
1455 BUG_ON(bp->pb_bn == 0);
1460 spin_unlock(&hash->bh_lock);
1465 * Allocate buffer hash table for a given target.
1466 * For devices containing metadata (i.e. not the log/realtime devices)
1467 * we need to allocate a much larger hash table.
1476 btp->bt_hashshift = external ? 3 : 8; /* 8 or 256 buckets */
1477 btp->bt_hashmask = (1 << btp->bt_hashshift) - 1;
1478 btp->bt_hash = kmem_zalloc((1 << btp->bt_hashshift) *
1479 sizeof(xfs_bufhash_t), KM_SLEEP);
1480 for (i = 0; i < (1 << btp->bt_hashshift); i++) {
1481 spin_lock_init(&btp->bt_hash[i].bh_lock);
1482 INIT_LIST_HEAD(&btp->bt_hash[i].bh_list);
1490 kmem_free(btp->bt_hash,
1491 (1 << btp->bt_hashshift) * sizeof(xfs_bufhash_t));
1492 btp->bt_hash = NULL;
1500 xfs_flush_buftarg(btp, 1);
1502 xfs_blkdev_put(btp->pbr_bdev);
1503 xfs_free_bufhash(btp);
1504 iput(btp->pbr_mapping->host);
1505 kmem_free(btp, sizeof(*btp));
1509 xfs_setsize_buftarg_flags(
1511 unsigned int blocksize,
1512 unsigned int sectorsize,
1515 btp->pbr_bsize = blocksize;
1516 btp->pbr_sshift = ffs(sectorsize) - 1;
1517 btp->pbr_smask = sectorsize - 1;
1519 if (set_blocksize(btp->pbr_bdev, sectorsize)) {
1521 "XFS: Cannot set_blocksize to %u on device %s\n",
1522 sectorsize, XFS_BUFTARG_NAME(btp));
1527 (PAGE_CACHE_SIZE / BITS_PER_LONG) > sectorsize) {
1529 "XFS: %u byte sectors in use on device %s. "
1530 "This is suboptimal; %u or greater is ideal.\n",
1531 sectorsize, XFS_BUFTARG_NAME(btp),
1532 (unsigned int)PAGE_CACHE_SIZE / BITS_PER_LONG);
1539 * When allocating the initial buffer target we have not yet
1540 * read in the superblock, so don't know what sized sectors
1541 * are being used is at this early stage. Play safe.
1544 xfs_setsize_buftarg_early(
1546 struct block_device *bdev)
1548 return xfs_setsize_buftarg_flags(btp,
1549 PAGE_CACHE_SIZE, bdev_hardsect_size(bdev), 0);
1553 xfs_setsize_buftarg(
1555 unsigned int blocksize,
1556 unsigned int sectorsize)
1558 return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
1562 xfs_mapping_buftarg(
1564 struct block_device *bdev)
1566 struct backing_dev_info *bdi;
1567 struct inode *inode;
1568 struct address_space *mapping;
1569 static struct address_space_operations mapping_aops = {
1570 .sync_page = block_sync_page,
1573 inode = new_inode(bdev->bd_inode->i_sb);
1576 "XFS: Cannot allocate mapping inode for device %s\n",
1577 XFS_BUFTARG_NAME(btp));
1580 inode->i_mode = S_IFBLK;
1581 inode->i_bdev = bdev;
1582 inode->i_rdev = bdev->bd_dev;
1583 bdi = blk_get_backing_dev_info(bdev);
1585 bdi = &default_backing_dev_info;
1586 mapping = &inode->i_data;
1587 mapping->a_ops = &mapping_aops;
1588 mapping->backing_dev_info = bdi;
1589 mapping_set_gfp_mask(mapping, GFP_NOFS);
1590 btp->pbr_mapping = mapping;
1596 struct block_device *bdev,
1601 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
1603 btp->pbr_dev = bdev->bd_dev;
1604 btp->pbr_bdev = bdev;
1605 if (xfs_setsize_buftarg_early(btp, bdev))
1607 if (xfs_mapping_buftarg(btp, bdev))
1609 xfs_alloc_bufhash(btp, external);
1613 kmem_free(btp, sizeof(*btp));
1619 * Pagebuf delayed write buffer handling
1622 STATIC LIST_HEAD(pbd_delwrite_queue);
1623 STATIC DEFINE_SPINLOCK(pbd_delwrite_lock);
1626 pagebuf_delwri_queue(
1630 PB_TRACE(pb, "delwri_q", (long)unlock);
1631 ASSERT((pb->pb_flags & (PBF_DELWRI|PBF_ASYNC)) ==
1632 (PBF_DELWRI|PBF_ASYNC));
1634 spin_lock(&pbd_delwrite_lock);
1635 /* If already in the queue, dequeue and place at tail */
1636 if (!list_empty(&pb->pb_list)) {
1637 ASSERT(pb->pb_flags & _PBF_DELWRI_Q);
1639 atomic_dec(&pb->pb_hold);
1641 list_del(&pb->pb_list);
1644 pb->pb_flags |= _PBF_DELWRI_Q;
1645 list_add_tail(&pb->pb_list, &pbd_delwrite_queue);
1646 pb->pb_queuetime = jiffies;
1647 spin_unlock(&pbd_delwrite_lock);
1654 pagebuf_delwri_dequeue(
1659 spin_lock(&pbd_delwrite_lock);
1660 if ((pb->pb_flags & PBF_DELWRI) && !list_empty(&pb->pb_list)) {
1661 ASSERT(pb->pb_flags & _PBF_DELWRI_Q);
1662 list_del_init(&pb->pb_list);
1665 pb->pb_flags &= ~(PBF_DELWRI|_PBF_DELWRI_Q);
1666 spin_unlock(&pbd_delwrite_lock);
1671 PB_TRACE(pb, "delwri_dq", (long)dequeued);
1675 pagebuf_runall_queues(
1676 struct workqueue_struct *queue)
1678 flush_workqueue(queue);
1681 /* Defines for pagebuf daemon */
1682 STATIC struct task_struct *xfsbufd_task;
1683 STATIC int xfsbufd_force_flush;
1684 STATIC int xfsbufd_force_sleep;
1691 if (xfsbufd_force_sleep)
1693 xfsbufd_force_flush = 1;
1695 wake_up_process(xfsbufd_task);
1703 struct list_head tmp;
1705 xfs_buftarg_t *target;
1708 current->flags |= PF_MEMALLOC;
1710 INIT_LIST_HEAD(&tmp);
1712 if (unlikely(freezing(current))) {
1713 xfsbufd_force_sleep = 1;
1716 xfsbufd_force_sleep = 0;
1719 schedule_timeout_interruptible(
1720 xfs_buf_timer_centisecs * msecs_to_jiffies(10));
1722 age = xfs_buf_age_centisecs * msecs_to_jiffies(10);
1723 spin_lock(&pbd_delwrite_lock);
1724 list_for_each_entry_safe(pb, n, &pbd_delwrite_queue, pb_list) {
1725 PB_TRACE(pb, "walkq1", (long)pagebuf_ispin(pb));
1726 ASSERT(pb->pb_flags & PBF_DELWRI);
1728 if (!pagebuf_ispin(pb) && !pagebuf_cond_lock(pb)) {
1729 if (!xfsbufd_force_flush &&
1730 time_before(jiffies,
1731 pb->pb_queuetime + age)) {
1736 pb->pb_flags &= ~(PBF_DELWRI|_PBF_DELWRI_Q);
1737 pb->pb_flags |= PBF_WRITE;
1738 list_move(&pb->pb_list, &tmp);
1741 spin_unlock(&pbd_delwrite_lock);
1743 while (!list_empty(&tmp)) {
1744 pb = list_entry(tmp.next, xfs_buf_t, pb_list);
1745 target = pb->pb_target;
1747 list_del_init(&pb->pb_list);
1748 pagebuf_iostrategy(pb);
1750 blk_run_address_space(target->pbr_mapping);
1753 if (as_list_len > 0)
1756 xfsbufd_force_flush = 0;
1757 } while (!kthread_should_stop());
1763 * Go through all incore buffers, and release buffers if they belong to
1764 * the given device. This is used in filesystem error handling to
1765 * preserve the consistency of its metadata.
1769 xfs_buftarg_t *target,
1772 struct list_head tmp;
1776 pagebuf_runall_queues(xfsdatad_workqueue);
1777 pagebuf_runall_queues(xfslogd_workqueue);
1779 INIT_LIST_HEAD(&tmp);
1780 spin_lock(&pbd_delwrite_lock);
1781 list_for_each_entry_safe(pb, n, &pbd_delwrite_queue, pb_list) {
1783 if (pb->pb_target != target)
1786 ASSERT(pb->pb_flags & (PBF_DELWRI|_PBF_DELWRI_Q));
1787 PB_TRACE(pb, "walkq2", (long)pagebuf_ispin(pb));
1788 if (pagebuf_ispin(pb)) {
1793 list_move(&pb->pb_list, &tmp);
1795 spin_unlock(&pbd_delwrite_lock);
1798 * Dropped the delayed write list lock, now walk the temporary list
1800 list_for_each_entry_safe(pb, n, &tmp, pb_list) {
1802 pb->pb_flags &= ~(PBF_DELWRI|_PBF_DELWRI_Q);
1803 pb->pb_flags |= PBF_WRITE;
1805 pb->pb_flags &= ~PBF_ASYNC;
1807 list_del_init(&pb->pb_list);
1809 pagebuf_iostrategy(pb);
1813 * Remaining list items must be flushed before returning
1815 while (!list_empty(&tmp)) {
1816 pb = list_entry(tmp.next, xfs_buf_t, pb_list);
1818 list_del_init(&pb->pb_list);
1824 blk_run_address_space(target->pbr_mapping);
1832 int error = -ENOMEM;
1834 #ifdef PAGEBUF_TRACE
1835 pagebuf_trace_buf = ktrace_alloc(PAGEBUF_TRACE_SIZE, KM_SLEEP);
1838 pagebuf_zone = kmem_zone_init(sizeof(xfs_buf_t), "xfs_buf");
1840 goto out_free_trace_buf;
1842 xfslogd_workqueue = create_workqueue("xfslogd");
1843 if (!xfslogd_workqueue)
1844 goto out_free_buf_zone;
1846 xfsdatad_workqueue = create_workqueue("xfsdatad");
1847 if (!xfsdatad_workqueue)
1848 goto out_destroy_xfslogd_workqueue;
1850 xfsbufd_task = kthread_run(xfsbufd, NULL, "xfsbufd");
1851 if (IS_ERR(xfsbufd_task)) {
1852 error = PTR_ERR(xfsbufd_task);
1853 goto out_destroy_xfsdatad_workqueue;
1856 pagebuf_shake = kmem_shake_register(xfsbufd_wakeup);
1858 goto out_stop_xfsbufd;
1863 kthread_stop(xfsbufd_task);
1864 out_destroy_xfsdatad_workqueue:
1865 destroy_workqueue(xfsdatad_workqueue);
1866 out_destroy_xfslogd_workqueue:
1867 destroy_workqueue(xfslogd_workqueue);
1869 kmem_zone_destroy(pagebuf_zone);
1871 #ifdef PAGEBUF_TRACE
1872 ktrace_free(pagebuf_trace_buf);
1878 pagebuf_terminate(void)
1880 kmem_shake_deregister(pagebuf_shake);
1881 kthread_stop(xfsbufd_task);
1882 destroy_workqueue(xfsdatad_workqueue);
1883 destroy_workqueue(xfslogd_workqueue);
1884 kmem_zone_destroy(pagebuf_zone);
1885 #ifdef PAGEBUF_TRACE
1886 ktrace_free(pagebuf_trace_buf);