2 * Copyright (c) 2000-2004 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
34 * The xfs_buf.c code provides an abstract buffer cache model on top
35 * of the Linux page cache. Cached metadata blocks for a file system
36 * are hashed to the inode for the block device. xfs_buf.c assembles
37 * buffers (xfs_buf_t) on demand to aggregate such cached pages for I/O.
39 * Written by Steve Lord, Jim Mostek, Russell Cattelan
40 * and Rajagopal Ananthanarayanan ("ananth") at SGI.
44 #include <linux/stddef.h>
45 #include <linux/errno.h>
46 #include <linux/slab.h>
47 #include <linux/pagemap.h>
48 #include <linux/init.h>
49 #include <linux/vmalloc.h>
50 #include <linux/bio.h>
51 #include <linux/sysctl.h>
52 #include <linux/proc_fs.h>
53 #include <linux/workqueue.h>
54 #include <linux/suspend.h>
55 #include <linux/percpu.h>
57 #include "xfs_linux.h"
60 #define GFP_READAHEAD (__GFP_NOWARN|__GFP_NORETRY)
67 STATIC kmem_cache_t *pagebuf_cache;
68 STATIC kmem_shaker_t pagebuf_shake;
69 STATIC int pagebuf_daemon_wakeup(int, unsigned int);
70 STATIC void pagebuf_delwri_queue(xfs_buf_t *, int);
71 STATIC struct workqueue_struct *pagebuf_logio_workqueue;
72 STATIC struct workqueue_struct *pagebuf_dataio_workqueue;
86 ktrace_enter(pagebuf_trace_buf,
88 (void *)(unsigned long)pb->pb_flags,
89 (void *)(unsigned long)pb->pb_hold.counter,
90 (void *)(unsigned long)pb->pb_sema.count.counter,
93 (void *)(unsigned long)((pb->pb_file_offset>>32) & 0xffffffff),
94 (void *)(unsigned long)(pb->pb_file_offset & 0xffffffff),
95 (void *)(unsigned long)pb->pb_buffer_length,
96 NULL, NULL, NULL, NULL, NULL);
98 ktrace_t *pagebuf_trace_buf;
99 #define PAGEBUF_TRACE_SIZE 4096
100 #define PB_TRACE(pb, id, data) \
101 pagebuf_trace(pb, id, (void *)data, (void *)__builtin_return_address(0))
103 #define PB_TRACE(pb, id, data) do { } while (0)
106 #ifdef PAGEBUF_LOCK_TRACKING
107 # define PB_SET_OWNER(pb) ((pb)->pb_last_holder = current->pid)
108 # define PB_CLEAR_OWNER(pb) ((pb)->pb_last_holder = -1)
109 # define PB_GET_OWNER(pb) ((pb)->pb_last_holder)
111 # define PB_SET_OWNER(pb) do { } while (0)
112 # define PB_CLEAR_OWNER(pb) do { } while (0)
113 # define PB_GET_OWNER(pb) do { } while (0)
117 * Pagebuf allocation / freeing.
120 #define pb_to_gfp(flags) \
121 (((flags) & PBF_READ_AHEAD) ? GFP_READAHEAD : \
122 ((flags) & PBF_DONT_BLOCK) ? GFP_NOFS : GFP_KERNEL)
124 #define pb_to_km(flags) \
125 (((flags) & PBF_DONT_BLOCK) ? KM_NOFS : KM_SLEEP)
128 #define pagebuf_allocate(flags) \
129 kmem_zone_alloc(pagebuf_cache, pb_to_km(flags))
130 #define pagebuf_deallocate(pb) \
131 kmem_zone_free(pagebuf_cache, (pb));
138 #define NHASH (1<<NBITS)
141 struct list_head pb_hash;
142 spinlock_t pb_hash_lock;
145 STATIC pb_hash_t pbhash[NHASH];
146 #define pb_hash(pb) &pbhash[pb->pb_hash_index]
150 struct block_device *bdev,
156 base ^= (unsigned long)bdev / L1_CACHE_BYTES;
157 for (bit = hval = 0; base && bit < sizeof(base) * 8; bit += NBITS) {
158 hval ^= (int)base & (NHASH-1);
165 * Mapping of multi-page buffers into contiguous virtual space
168 typedef struct a_list {
173 STATIC a_list_t *as_free_head;
174 STATIC int as_list_len;
175 STATIC spinlock_t as_lock = SPIN_LOCK_UNLOCKED;
178 * Try to batch vunmaps because they are costly.
186 aentry = kmalloc(sizeof(a_list_t), GFP_ATOMIC);
189 aentry->next = as_free_head;
190 aentry->vm_addr = addr;
191 as_free_head = aentry;
193 spin_unlock(&as_lock);
200 purge_addresses(void)
202 a_list_t *aentry, *old;
204 if (as_free_head == NULL)
208 aentry = as_free_head;
211 spin_unlock(&as_lock);
213 while ((old = aentry) != NULL) {
214 vunmap(aentry->vm_addr);
215 aentry = aentry->next;
221 * Internal pagebuf object manipulation
227 xfs_buftarg_t *target,
230 page_buf_flags_t flags)
233 * We don't want certain flags to appear in pb->pb_flags.
235 flags &= ~(PBF_LOCK|PBF_MAPPED|PBF_DONT_BLOCK|PBF_READ_AHEAD);
237 memset(pb, 0, sizeof(xfs_buf_t));
238 atomic_set(&pb->pb_hold, 1);
239 init_MUTEX_LOCKED(&pb->pb_iodonesema);
240 INIT_LIST_HEAD(&pb->pb_list);
241 INIT_LIST_HEAD(&pb->pb_hash_list);
242 init_MUTEX_LOCKED(&pb->pb_sema); /* held, no waiters */
244 pb->pb_target = target;
245 pb->pb_file_offset = range_base;
247 * Set buffer_length and count_desired to the same value initially.
248 * I/O routines should use count_desired, which will be the same in
249 * most cases but may be reset (e.g. XFS recovery).
251 pb->pb_buffer_length = pb->pb_count_desired = range_length;
252 pb->pb_flags = flags | PBF_NONE;
253 pb->pb_bn = XFS_BUF_DADDR_NULL;
254 atomic_set(&pb->pb_pin_count, 0);
255 init_waitqueue_head(&pb->pb_waiters);
257 XFS_STATS_INC(pb_create);
258 PB_TRACE(pb, "initialize", target);
262 * Allocate a page array capable of holding a specified number
263 * of pages, and point the page buf at it.
269 page_buf_flags_t flags)
271 /* Make sure that we have a page list */
272 if (pb->pb_pages == NULL) {
273 pb->pb_offset = page_buf_poff(pb->pb_file_offset);
274 pb->pb_page_count = page_count;
275 if (page_count <= PB_PAGES) {
276 pb->pb_pages = pb->pb_page_array;
278 pb->pb_pages = kmem_alloc(sizeof(struct page *) *
279 page_count, pb_to_km(flags));
280 if (pb->pb_pages == NULL)
283 memset(pb->pb_pages, 0, sizeof(struct page *) * page_count);
289 * Frees pb_pages if it was malloced.
295 if (bp->pb_pages != bp->pb_page_array) {
296 kmem_free(bp->pb_pages,
297 bp->pb_page_count * sizeof(struct page *));
302 * Releases the specified buffer.
304 * The modification state of any associated pages is left unchanged.
305 * The buffer most not be on any hash - use pagebuf_rele instead for
306 * hashed and refcounted buffers
312 PB_TRACE(bp, "free", 0);
314 ASSERT(list_empty(&bp->pb_hash_list));
316 if (bp->pb_flags & _PBF_PAGE_CACHE) {
319 if ((bp->pb_flags & PBF_MAPPED) && (bp->pb_page_count > 1))
320 free_address(bp->pb_addr - bp->pb_offset);
322 for (i = 0; i < bp->pb_page_count; i++)
323 page_cache_release(bp->pb_pages[i]);
324 _pagebuf_free_pages(bp);
325 } else if (bp->pb_flags & _PBF_KMEM_ALLOC) {
327 * XXX(hch): bp->pb_count_desired might be incorrect (see
328 * pagebuf_associate_memory for details), but fortunately
329 * the Linux version of kmem_free ignores the len argument..
331 kmem_free(bp->pb_addr, bp->pb_count_desired);
332 _pagebuf_free_pages(bp);
335 pagebuf_deallocate(bp);
339 * Finds all pages for buffer in question and builds it's page list.
342 _pagebuf_lookup_pages(
346 struct address_space *mapping = bp->pb_target->pbr_mapping;
347 unsigned int sectorshift = bp->pb_target->pbr_sshift;
348 size_t blocksize = bp->pb_target->pbr_bsize;
349 size_t size = bp->pb_count_desired;
350 size_t nbytes, offset;
351 int gfp_mask = pb_to_gfp(flags);
352 unsigned short page_count, i;
357 end = bp->pb_file_offset + bp->pb_buffer_length;
358 page_count = page_buf_btoc(end) - page_buf_btoct(bp->pb_file_offset);
360 error = _pagebuf_get_pages(bp, page_count, flags);
363 bp->pb_flags |= _PBF_PAGE_CACHE;
365 offset = bp->pb_offset;
366 first = bp->pb_file_offset >> PAGE_CACHE_SHIFT;
368 for (i = 0; i < bp->pb_page_count; i++) {
373 page = find_or_create_page(mapping, first + i, gfp_mask);
374 if (unlikely(page == NULL)) {
375 if (flags & PBF_READ_AHEAD) {
376 bp->pb_page_count = i;
377 for (i = 0; i < bp->pb_page_count; i++)
378 unlock_page(bp->pb_pages[i]);
383 * This could deadlock.
385 * But until all the XFS lowlevel code is revamped to
386 * handle buffer allocation failures we can't do much.
388 if (!(++retries % 100))
390 "possible deadlock in %s (mode:0x%x)\n",
391 __FUNCTION__, gfp_mask);
393 XFS_STATS_INC(pb_page_retries);
394 pagebuf_daemon_wakeup(0, gfp_mask);
395 set_current_state(TASK_UNINTERRUPTIBLE);
396 schedule_timeout(10);
400 XFS_STATS_INC(pb_page_found);
402 nbytes = min_t(size_t, size, PAGE_CACHE_SIZE - offset);
405 if (!PageUptodate(page)) {
407 if (blocksize == PAGE_CACHE_SIZE) {
408 if (flags & PBF_READ)
410 } else if (!PagePrivate(page)) {
411 unsigned long j, range;
414 * In this case page->private holds a bitmap
415 * of uptodate sectors within the page
417 ASSERT(blocksize < PAGE_CACHE_SIZE);
418 range = (offset + nbytes) >> sectorshift;
419 for (j = offset >> sectorshift; j < range; j++)
420 if (!test_bit(j, &page->private))
427 bp->pb_pages[i] = page;
431 if (!bp->pb_locked) {
432 for (i = 0; i < bp->pb_page_count; i++)
433 unlock_page(bp->pb_pages[i]);
437 /* if we have any uptodate pages, mark that in the buffer */
438 bp->pb_flags &= ~PBF_NONE;
440 /* if some pages aren't uptodate, mark that in the buffer */
441 if (page_count != bp->pb_page_count)
442 bp->pb_flags |= PBF_PARTIAL;
445 PB_TRACE(bp, "lookup_pages", (long)page_count);
450 * Map buffer into kernel address-space if nessecary.
457 /* A single page buffer is always mappable */
458 if (bp->pb_page_count == 1) {
459 bp->pb_addr = page_address(bp->pb_pages[0]) + bp->pb_offset;
460 bp->pb_flags |= PBF_MAPPED;
461 } else if (flags & PBF_MAPPED) {
462 if (as_list_len > 64)
464 bp->pb_addr = vmap(bp->pb_pages, bp->pb_page_count,
465 VM_MAP, PAGE_KERNEL);
466 if (unlikely(bp->pb_addr == NULL))
468 bp->pb_addr += bp->pb_offset;
469 bp->pb_flags |= PBF_MAPPED;
476 * Finding and Reading Buffers
482 * Looks 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.
490 _pagebuf_find( /* find buffer for block */
491 xfs_buftarg_t *target,/* target for block */
492 loff_t ioff, /* starting offset of range */
493 size_t isize, /* length of range */
494 page_buf_flags_t flags, /* PBF_TRYLOCK */
495 xfs_buf_t *new_pb)/* newly allocated buffer */
504 range_base = (ioff << BBSHIFT);
505 range_length = (isize << BBSHIFT);
507 /* Ensure we never do IOs smaller than the sector size */
508 BUG_ON(range_length < (1 << target->pbr_sshift));
510 /* Ensure we never do IOs that are not sector aligned */
511 BUG_ON(range_base & (loff_t)target->pbr_smask);
513 hval = _bhash(target->pbr_bdev, range_base);
516 spin_lock(&h->pb_hash_lock);
517 list_for_each_entry_safe(pb, n, &h->pb_hash, pb_hash_list) {
518 if (pb->pb_target == target &&
519 pb->pb_file_offset == range_base &&
520 pb->pb_buffer_length == range_length) {
521 /* If we look at something bring it to the
522 * front of the list for next time
524 atomic_inc(&pb->pb_hold);
525 list_move(&pb->pb_hash_list, &h->pb_hash);
532 _pagebuf_initialize(new_pb, target, range_base,
533 range_length, flags);
534 new_pb->pb_hash_index = hval;
535 list_add(&new_pb->pb_hash_list, &h->pb_hash);
537 XFS_STATS_INC(pb_miss_locked);
540 spin_unlock(&h->pb_hash_lock);
544 spin_unlock(&h->pb_hash_lock);
546 /* Attempt to get the semaphore without sleeping,
547 * if this does not work then we need to drop the
548 * spinlock and do a hard attempt on the semaphore.
550 not_locked = down_trylock(&pb->pb_sema);
552 if (!(flags & PBF_TRYLOCK)) {
553 /* wait for buffer ownership */
554 PB_TRACE(pb, "get_lock", 0);
556 XFS_STATS_INC(pb_get_locked_waited);
558 /* We asked for a trylock and failed, no need
559 * to look at file offset and length here, we
560 * know that this pagebuf at least overlaps our
561 * pagebuf and is locked, therefore our buffer
562 * either does not exist, or is this buffer
566 XFS_STATS_INC(pb_busy_locked);
574 if (pb->pb_flags & PBF_STALE)
575 pb->pb_flags &= PBF_MAPPED;
576 PB_TRACE(pb, "got_lock", 0);
577 XFS_STATS_INC(pb_get_locked);
585 * pagebuf_find returns a buffer matching the specified range of
586 * data for the specified target, if any of the relevant blocks
587 * are in memory. The buffer may have unallocated holes, if
588 * some, but not all, of the blocks are in memory. Even where
589 * pages are present in the buffer, not all of every page may be
593 pagebuf_find( /* find buffer for block */
594 /* if the block is in memory */
595 xfs_buftarg_t *target,/* target for block */
596 loff_t ioff, /* starting offset of range */
597 size_t isize, /* length of range */
598 page_buf_flags_t flags) /* PBF_TRYLOCK */
600 return _pagebuf_find(target, ioff, isize, flags, NULL);
606 * pagebuf_get assembles a buffer covering the specified range.
607 * Some or all of the blocks in the range may be valid. Storage
608 * in memory for all portions of the buffer will be allocated,
609 * although backing storage may not be. If PBF_READ is set in
610 * flags, pagebuf_iostart is called also.
613 pagebuf_get( /* allocate a buffer */
614 xfs_buftarg_t *target,/* target for buffer */
615 loff_t ioff, /* starting offset of range */
616 size_t isize, /* length of range */
617 page_buf_flags_t flags) /* PBF_TRYLOCK */
619 xfs_buf_t *pb, *new_pb;
622 new_pb = pagebuf_allocate(flags);
623 if (unlikely(!new_pb))
626 pb = _pagebuf_find(target, ioff, isize, flags, new_pb);
628 error = _pagebuf_lookup_pages(pb, flags);
629 if (unlikely(error)) {
631 "pagebuf_get: failed to lookup pages\n");
635 pagebuf_deallocate(new_pb);
636 if (unlikely(pb == NULL))
640 for (i = 0; i < pb->pb_page_count; i++)
641 mark_page_accessed(pb->pb_pages[i]);
643 if (!(pb->pb_flags & PBF_MAPPED)) {
644 error = _pagebuf_map_pages(pb, flags);
645 if (unlikely(error)) {
647 "pagebuf_get: failed to map pages\n");
652 XFS_STATS_INC(pb_get);
655 * Always fill in the block number now, the mapped cases can do
656 * their own overlay of this later.
659 pb->pb_count_desired = pb->pb_buffer_length;
661 if (flags & PBF_READ) {
662 if (PBF_NOT_DONE(pb)) {
663 PB_TRACE(pb, "get_read", (unsigned long)flags);
664 XFS_STATS_INC(pb_get_read);
665 pagebuf_iostart(pb, flags);
666 } else if (flags & PBF_ASYNC) {
667 PB_TRACE(pb, "get_read_async", (unsigned long)flags);
669 * Read ahead call which is already satisfied,
674 PB_TRACE(pb, "get_read_done", (unsigned long)flags);
675 /* We do not want read in the flags */
676 pb->pb_flags &= ~PBF_READ;
679 PB_TRACE(pb, "get_write", (unsigned long)flags);
685 if (flags & (PBF_LOCK | PBF_TRYLOCK))
692 * Create a skeletal pagebuf (no pages associated with it).
696 xfs_buftarg_t *target,
699 page_buf_flags_t flags)
703 pb = pagebuf_allocate(flags);
705 _pagebuf_initialize(pb, target, ioff, isize, flags);
711 * If we are not low on memory then do the readahead in a deadlock
716 xfs_buftarg_t *target,
719 page_buf_flags_t flags)
721 struct backing_dev_info *bdi;
723 bdi = target->pbr_mapping->backing_dev_info;
724 if (bdi_read_congested(bdi))
726 if (bdi_write_congested(bdi))
729 flags |= (PBF_TRYLOCK|PBF_READ|PBF_ASYNC|PBF_READ_AHEAD);
730 pagebuf_get(target, ioff, isize, flags);
736 xfs_buftarg_t *target)
740 pb = pagebuf_allocate(0);
742 _pagebuf_initialize(pb, target, 0, len, 0);
746 static inline struct page *
750 if (((unsigned long)addr < VMALLOC_START) ||
751 ((unsigned long)addr >= VMALLOC_END)) {
752 return virt_to_page(addr);
754 return vmalloc_to_page(addr);
759 pagebuf_associate_memory(
771 page_count = PAGE_CACHE_ALIGN(len) >> PAGE_CACHE_SHIFT;
772 offset = (off_t) mem - ((off_t)mem & PAGE_CACHE_MASK);
773 if (offset && (len > PAGE_CACHE_SIZE))
776 /* Free any previous set of page pointers */
778 _pagebuf_free_pages(pb);
783 rval = _pagebuf_get_pages(pb, page_count, 0);
787 pb->pb_offset = offset;
788 ptr = (size_t) mem & PAGE_CACHE_MASK;
789 end = PAGE_CACHE_ALIGN((size_t) mem + len);
791 /* set up first page */
792 pb->pb_pages[0] = mem_to_page(mem);
794 ptr += PAGE_CACHE_SIZE;
795 pb->pb_page_count = ++i;
797 pb->pb_pages[i] = mem_to_page((void *)ptr);
798 pb->pb_page_count = ++i;
799 ptr += PAGE_CACHE_SIZE;
803 pb->pb_count_desired = pb->pb_buffer_length = len;
804 pb->pb_flags |= PBF_MAPPED;
810 pagebuf_get_no_daddr(
812 xfs_buftarg_t *target)
814 size_t malloc_len = len;
819 bp = pagebuf_allocate(0);
820 if (unlikely(bp == NULL))
822 _pagebuf_initialize(bp, target, 0, len, PBF_FORCEIO);
825 data = kmem_alloc(malloc_len, KM_SLEEP | KM_MAYFAIL);
826 if (unlikely(data == NULL))
829 /* check whether alignment matches.. */
830 if ((__psunsigned_t)data !=
831 ((__psunsigned_t)data & ~target->pbr_smask)) {
832 /* .. else double the size and try again */
833 kmem_free(data, malloc_len);
838 error = pagebuf_associate_memory(bp, data, len);
841 bp->pb_flags |= _PBF_KMEM_ALLOC;
845 PB_TRACE(bp, "no_daddr", data);
848 kmem_free(data, malloc_len);
858 * Increment reference count on buffer, to hold the buffer concurrently
859 * with another thread which may release (free) the buffer asynchronously.
861 * Must hold the buffer already to call this function.
867 atomic_inc(&pb->pb_hold);
868 PB_TRACE(pb, "hold", 0);
874 * pagebuf_rele releases a hold on the specified buffer. If the
875 * the hold count is 1, pagebuf_rele calls pagebuf_free.
881 pb_hash_t *hash = pb_hash(pb);
883 PB_TRACE(pb, "rele", pb->pb_relse);
885 if (atomic_dec_and_lock(&pb->pb_hold, &hash->pb_hash_lock)) {
889 atomic_inc(&pb->pb_hold);
890 spin_unlock(&hash->pb_hash_lock);
891 (*(pb->pb_relse)) (pb);
892 spin_lock(&hash->pb_hash_lock);
896 if (pb->pb_flags & PBF_DELWRI) {
897 pb->pb_flags |= PBF_ASYNC;
898 atomic_inc(&pb->pb_hold);
899 pagebuf_delwri_queue(pb, 0);
901 } else if (pb->pb_flags & PBF_FS_MANAGED) {
906 list_del_init(&pb->pb_hash_list);
907 spin_unlock(&hash->pb_hash_lock);
910 spin_unlock(&hash->pb_hash_lock);
917 * Mutual exclusion on buffers. Locking model:
919 * Buffers associated with inodes for which buffer locking
920 * is not enabled are not protected by semaphores, and are
921 * assumed to be exclusively owned by the caller. There is a
922 * spinlock in the buffer, used by the caller when concurrent
923 * access is possible.
929 * pagebuf_cond_lock locks a buffer object, if it is not already locked.
930 * Note that this in no way
931 * locks the underlying pages, so it is only useful for synchronizing
932 * concurrent use of page buffer objects, not for synchronizing independent
933 * access to the underlying pages.
936 pagebuf_cond_lock( /* lock buffer, if not locked */
937 /* returns -EBUSY if locked) */
942 locked = down_trylock(&pb->pb_sema) == 0;
946 PB_TRACE(pb, "cond_lock", (long)locked);
947 return(locked ? 0 : -EBUSY);
953 * Return lock value for a pagebuf
959 return(atomic_read(&pb->pb_sema.count));
965 * pagebuf_lock locks a buffer object. Note that this in no way
966 * locks the underlying pages, so it is only useful for synchronizing
967 * concurrent use of page buffer objects, not for synchronizing independent
968 * access to the underlying pages.
974 PB_TRACE(pb, "lock", 0);
975 if (atomic_read(&pb->pb_io_remaining))
976 blk_run_address_space(pb->pb_target->pbr_mapping);
979 PB_TRACE(pb, "locked", 0);
986 * pagebuf_unlock releases the lock on the buffer object created by
987 * pagebuf_lock or pagebuf_cond_lock (not any
988 * pinning of underlying pages created by pagebuf_pin).
991 pagebuf_unlock( /* unlock buffer */
992 xfs_buf_t *pb) /* buffer to unlock */
996 PB_TRACE(pb, "unlock", 0);
1001 * Pinning Buffer Storage in Memory
1007 * pagebuf_pin locks all of the memory represented by a buffer in
1008 * memory. Multiple calls to pagebuf_pin and pagebuf_unpin, for
1009 * the same or different buffers affecting a given page, will
1010 * properly count the number of outstanding "pin" requests. The
1011 * buffer may be released after the pagebuf_pin and a different
1012 * buffer used when calling pagebuf_unpin, if desired.
1013 * pagebuf_pin should be used by the file system when it wants be
1014 * assured that no attempt will be made to force the affected
1015 * memory to disk. It does not assure that a given logical page
1016 * will not be moved to a different physical page.
1022 atomic_inc(&pb->pb_pin_count);
1023 PB_TRACE(pb, "pin", (long)pb->pb_pin_count.counter);
1029 * pagebuf_unpin reverses the locking of memory performed by
1030 * pagebuf_pin. Note that both functions affected the logical
1031 * pages associated with the buffer, not the buffer itself.
1037 if (atomic_dec_and_test(&pb->pb_pin_count)) {
1038 wake_up_all(&pb->pb_waiters);
1040 PB_TRACE(pb, "unpin", (long)pb->pb_pin_count.counter);
1047 return atomic_read(&pb->pb_pin_count);
1051 * pagebuf_wait_unpin
1053 * pagebuf_wait_unpin waits until all of the memory associated
1054 * with the buffer is not longer locked in memory. It returns
1055 * immediately if none of the affected pages are locked.
1058 _pagebuf_wait_unpin(
1061 DECLARE_WAITQUEUE (wait, current);
1063 if (atomic_read(&pb->pb_pin_count) == 0)
1066 add_wait_queue(&pb->pb_waiters, &wait);
1068 set_current_state(TASK_UNINTERRUPTIBLE);
1069 if (atomic_read(&pb->pb_pin_count) == 0)
1071 if (atomic_read(&pb->pb_io_remaining))
1072 blk_run_address_space(pb->pb_target->pbr_mapping);
1075 remove_wait_queue(&pb->pb_waiters, &wait);
1076 set_current_state(TASK_RUNNING);
1080 * Buffer Utility Routines
1086 * pagebuf_iodone marks a buffer for which I/O is in progress
1087 * done with respect to that I/O. The pb_iodone routine, if
1088 * present, will be called as a side-effect.
1091 pagebuf_iodone_work(
1094 xfs_buf_t *bp = (xfs_buf_t *)v;
1097 (*(bp->pb_iodone))(bp);
1098 else if (bp->pb_flags & PBF_ASYNC)
1108 pb->pb_flags &= ~(PBF_READ | PBF_WRITE);
1109 if (pb->pb_error == 0) {
1110 pb->pb_flags &= ~(PBF_PARTIAL | PBF_NONE);
1113 PB_TRACE(pb, "iodone", pb->pb_iodone);
1115 if ((pb->pb_iodone) || (pb->pb_flags & PBF_ASYNC)) {
1117 INIT_WORK(&pb->pb_iodone_work, pagebuf_iodone_work, pb);
1118 queue_work(dataio ? pagebuf_dataio_workqueue :
1119 pagebuf_logio_workqueue, &pb->pb_iodone_work);
1121 pagebuf_iodone_work(pb);
1124 up(&pb->pb_iodonesema);
1131 * pagebuf_ioerror sets the error code for a buffer.
1134 pagebuf_ioerror( /* mark/clear buffer error flag */
1135 xfs_buf_t *pb, /* buffer to mark */
1136 int error) /* error to store (0 if none) */
1138 ASSERT(error >= 0 && error <= 0xffff);
1139 pb->pb_error = (unsigned short)error;
1140 PB_TRACE(pb, "ioerror", (unsigned long)error);
1146 * pagebuf_iostart initiates I/O on a buffer, based on the flags supplied.
1147 * If necessary, it will arrange for any disk space allocation required,
1148 * and it will break up the request if the block mappings require it.
1149 * The pb_iodone routine in the buffer supplied will only be called
1150 * when all of the subsidiary I/O requests, if any, have been completed.
1151 * pagebuf_iostart calls the pagebuf_ioinitiate routine or
1152 * pagebuf_iorequest, if the former routine is not defined, to start
1153 * the I/O on a given low-level request.
1156 pagebuf_iostart( /* start I/O on a buffer */
1157 xfs_buf_t *pb, /* buffer to start */
1158 page_buf_flags_t flags) /* PBF_LOCK, PBF_ASYNC, PBF_READ, */
1159 /* PBF_WRITE, PBF_DELWRI, */
1160 /* PBF_DONT_BLOCK */
1164 PB_TRACE(pb, "iostart", (unsigned long)flags);
1166 if (flags & PBF_DELWRI) {
1167 pb->pb_flags &= ~(PBF_READ | PBF_WRITE | PBF_ASYNC);
1168 pb->pb_flags |= flags & (PBF_DELWRI | PBF_ASYNC);
1169 pagebuf_delwri_queue(pb, 1);
1173 pb->pb_flags &= ~(PBF_READ | PBF_WRITE | PBF_ASYNC | PBF_DELWRI | \
1174 PBF_READ_AHEAD | _PBF_RUN_QUEUES);
1175 pb->pb_flags |= flags & (PBF_READ | PBF_WRITE | PBF_ASYNC | \
1176 PBF_READ_AHEAD | _PBF_RUN_QUEUES);
1178 BUG_ON(pb->pb_bn == XFS_BUF_DADDR_NULL);
1180 /* For writes allow an alternate strategy routine to precede
1181 * the actual I/O request (which may not be issued at all in
1182 * a shutdown situation, for example).
1184 status = (flags & PBF_WRITE) ?
1185 pagebuf_iostrategy(pb) : pagebuf_iorequest(pb);
1187 /* Wait for I/O if we are not an async request.
1188 * Note: async I/O request completion will release the buffer,
1189 * and that can already be done by this point. So using the
1190 * buffer pointer from here on, after async I/O, is invalid.
1192 if (!status && !(flags & PBF_ASYNC))
1193 status = pagebuf_iowait(pb);
1199 * Helper routine for pagebuf_iorequest
1202 STATIC __inline__ int
1206 ASSERT(pb->pb_flags & (PBF_READ|PBF_WRITE));
1207 if (pb->pb_flags & PBF_READ)
1208 return pb->pb_locked;
1212 STATIC __inline__ void
1217 if (atomic_dec_and_test(&pb->pb_io_remaining) == 1) {
1219 pagebuf_iodone(pb, (pb->pb_flags & PBF_FS_DATAIOD), schedule);
1226 unsigned int bytes_done,
1229 xfs_buf_t *pb = (xfs_buf_t *)bio->bi_private;
1230 unsigned int i, blocksize = pb->pb_target->pbr_bsize;
1231 unsigned int sectorshift = pb->pb_target->pbr_sshift;
1232 struct bio_vec *bvec = bio->bi_io_vec;
1237 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
1240 for (i = 0; i < bio->bi_vcnt; i++, bvec++) {
1241 struct page *page = bvec->bv_page;
1245 } else if (blocksize == PAGE_CACHE_SIZE) {
1246 SetPageUptodate(page);
1247 } else if (!PagePrivate(page) &&
1248 (pb->pb_flags & _PBF_PAGE_CACHE)) {
1249 unsigned long j, range;
1251 ASSERT(blocksize < PAGE_CACHE_SIZE);
1252 range = (bvec->bv_offset + bvec->bv_len) >> sectorshift;
1253 for (j = bvec->bv_offset >> sectorshift; j < range; j++)
1254 set_bit(j, &page->private);
1255 if (page->private == (unsigned long)(PAGE_CACHE_SIZE-1))
1256 SetPageUptodate(page);
1259 if (_pagebuf_iolocked(pb)) {
1264 _pagebuf_iodone(pb, 1);
1273 int i, map_i, total_nr_pages, nr_pages;
1275 int offset = pb->pb_offset;
1276 int size = pb->pb_count_desired;
1277 sector_t sector = pb->pb_bn;
1278 unsigned int blocksize = pb->pb_target->pbr_bsize;
1279 int locking = _pagebuf_iolocked(pb);
1281 total_nr_pages = pb->pb_page_count;
1284 /* Special code path for reading a sub page size pagebuf in --
1285 * we populate up the whole page, and hence the other metadata
1286 * in the same page. This optimization is only valid when the
1287 * filesystem block size and the page size are equal.
1289 if ((pb->pb_buffer_length < PAGE_CACHE_SIZE) &&
1290 (pb->pb_flags & PBF_READ) && locking &&
1291 (blocksize == PAGE_CACHE_SIZE)) {
1292 bio = bio_alloc(GFP_NOIO, 1);
1294 bio->bi_bdev = pb->pb_target->pbr_bdev;
1295 bio->bi_sector = sector - (offset >> BBSHIFT);
1296 bio->bi_end_io = bio_end_io_pagebuf;
1297 bio->bi_private = pb;
1299 bio_add_page(bio, pb->pb_pages[0], PAGE_CACHE_SIZE, 0);
1302 atomic_inc(&pb->pb_io_remaining);
1307 /* Lock down the pages which we need to for the request */
1308 if (locking && (pb->pb_flags & PBF_WRITE) && (pb->pb_locked == 0)) {
1309 for (i = 0; size; i++) {
1310 int nbytes = PAGE_CACHE_SIZE - offset;
1311 struct page *page = pb->pb_pages[i];
1321 offset = pb->pb_offset;
1322 size = pb->pb_count_desired;
1326 atomic_inc(&pb->pb_io_remaining);
1327 nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
1328 if (nr_pages > total_nr_pages)
1329 nr_pages = total_nr_pages;
1331 bio = bio_alloc(GFP_NOIO, nr_pages);
1332 bio->bi_bdev = pb->pb_target->pbr_bdev;
1333 bio->bi_sector = sector;
1334 bio->bi_end_io = bio_end_io_pagebuf;
1335 bio->bi_private = pb;
1337 for (; size && nr_pages; nr_pages--, map_i++) {
1338 int nbytes = PAGE_CACHE_SIZE - offset;
1343 if (bio_add_page(bio, pb->pb_pages[map_i],
1344 nbytes, offset) < nbytes)
1348 sector += nbytes >> BBSHIFT;
1354 if (likely(bio->bi_size)) {
1355 submit_bio((pb->pb_flags & PBF_READ) ? READ : WRITE, bio);
1360 pagebuf_ioerror(pb, EIO);
1363 if (pb->pb_flags & _PBF_RUN_QUEUES) {
1364 pb->pb_flags &= ~_PBF_RUN_QUEUES;
1365 if (atomic_read(&pb->pb_io_remaining) > 1)
1366 blk_run_address_space(pb->pb_target->pbr_mapping);
1371 * pagebuf_iorequest -- the core I/O request routine.
1374 pagebuf_iorequest( /* start real I/O */
1375 xfs_buf_t *pb) /* buffer to convey to device */
1377 PB_TRACE(pb, "iorequest", 0);
1379 if (pb->pb_flags & PBF_DELWRI) {
1380 pagebuf_delwri_queue(pb, 1);
1384 if (pb->pb_flags & PBF_WRITE) {
1385 _pagebuf_wait_unpin(pb);
1390 /* Set the count to 1 initially, this will stop an I/O
1391 * completion callout which happens before we have started
1392 * all the I/O from calling pagebuf_iodone too early.
1394 atomic_set(&pb->pb_io_remaining, 1);
1395 _pagebuf_ioapply(pb);
1396 _pagebuf_iodone(pb, 0);
1405 * pagebuf_iowait waits for I/O to complete on the buffer supplied.
1406 * It returns immediately if no I/O is pending. In any case, it returns
1407 * the error code, if any, or 0 if there is no error.
1413 PB_TRACE(pb, "iowait", 0);
1414 if (atomic_read(&pb->pb_io_remaining))
1415 blk_run_address_space(pb->pb_target->pbr_mapping);
1416 down(&pb->pb_iodonesema);
1417 PB_TRACE(pb, "iowaited", (long)pb->pb_error);
1418 return pb->pb_error;
1428 offset += pb->pb_offset;
1430 page = pb->pb_pages[offset >> PAGE_CACHE_SHIFT];
1431 return (caddr_t) page_address(page) + (offset & (PAGE_CACHE_SIZE - 1));
1437 * Move data into or out of a buffer.
1441 xfs_buf_t *pb, /* buffer to process */
1442 size_t boff, /* starting buffer offset */
1443 size_t bsize, /* length to copy */
1444 caddr_t data, /* data address */
1445 page_buf_rw_t mode) /* read/write flag */
1447 size_t bend, cpoff, csize;
1450 bend = boff + bsize;
1451 while (boff < bend) {
1452 page = pb->pb_pages[page_buf_btoct(boff + pb->pb_offset)];
1453 cpoff = page_buf_poff(boff + pb->pb_offset);
1454 csize = min_t(size_t,
1455 PAGE_CACHE_SIZE-cpoff, pb->pb_count_desired-boff);
1457 ASSERT(((csize + cpoff) <= PAGE_CACHE_SIZE));
1461 memset(page_address(page) + cpoff, 0, csize);
1464 memcpy(data, page_address(page) + cpoff, csize);
1467 memcpy(page_address(page) + cpoff, data, csize);
1476 * Handling of buftargs.
1484 xfs_flush_buftarg(btp, 1);
1486 xfs_blkdev_put(btp->pbr_bdev);
1487 kmem_free(btp, sizeof(*btp));
1496 invalidate_bdev(btp->pbr_bdev, 1);
1497 truncate_inode_pages(btp->pbr_mapping, 0LL);
1501 xfs_setsize_buftarg(
1503 unsigned int blocksize,
1504 unsigned int sectorsize)
1506 btp->pbr_bsize = blocksize;
1507 btp->pbr_sshift = ffs(sectorsize) - 1;
1508 btp->pbr_smask = sectorsize - 1;
1510 if (set_blocksize(btp->pbr_bdev, sectorsize)) {
1512 "XFS: Cannot set_blocksize to %u on device %s\n",
1513 sectorsize, XFS_BUFTARG_NAME(btp));
1519 struct block_device *bdev)
1523 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
1525 btp->pbr_dev = bdev->bd_dev;
1526 btp->pbr_bdev = bdev;
1527 btp->pbr_mapping = bdev->bd_inode->i_mapping;
1528 xfs_setsize_buftarg(btp, PAGE_CACHE_SIZE, bdev_hardsect_size(bdev));
1535 * Pagebuf delayed write buffer handling
1538 STATIC LIST_HEAD(pbd_delwrite_queue);
1539 STATIC spinlock_t pbd_delwrite_lock = SPIN_LOCK_UNLOCKED;
1542 pagebuf_delwri_queue(
1546 PB_TRACE(pb, "delwri_q", (long)unlock);
1547 ASSERT(pb->pb_flags & PBF_DELWRI);
1549 spin_lock(&pbd_delwrite_lock);
1550 /* If already in the queue, dequeue and place at tail */
1551 if (!list_empty(&pb->pb_list)) {
1553 atomic_dec(&pb->pb_hold);
1555 list_del(&pb->pb_list);
1558 list_add_tail(&pb->pb_list, &pbd_delwrite_queue);
1559 pb->pb_queuetime = jiffies;
1560 spin_unlock(&pbd_delwrite_lock);
1567 pagebuf_delwri_dequeue(
1572 spin_lock(&pbd_delwrite_lock);
1573 if ((pb->pb_flags & PBF_DELWRI) && !list_empty(&pb->pb_list)) {
1574 list_del_init(&pb->pb_list);
1577 pb->pb_flags &= ~PBF_DELWRI;
1578 spin_unlock(&pbd_delwrite_lock);
1583 PB_TRACE(pb, "delwri_dq", (long)dequeued);
1587 pagebuf_runall_queues(
1588 struct workqueue_struct *queue)
1590 flush_workqueue(queue);
1593 /* Defines for pagebuf daemon */
1594 STATIC DECLARE_COMPLETION(pagebuf_daemon_done);
1595 STATIC struct task_struct *pagebuf_daemon_task;
1596 STATIC int pagebuf_daemon_active;
1597 STATIC int force_flush;
1601 pagebuf_daemon_wakeup(
1607 wake_up_process(pagebuf_daemon_task);
1615 struct list_head tmp;
1617 xfs_buftarg_t *target;
1620 /* Set up the thread */
1621 daemonize("xfsbufd");
1622 current->flags |= PF_MEMALLOC;
1624 pagebuf_daemon_task = current;
1625 pagebuf_daemon_active = 1;
1628 INIT_LIST_HEAD(&tmp);
1631 if (current->flags & PF_FREEZE)
1632 refrigerator(PF_FREEZE);
1634 set_current_state(TASK_INTERRUPTIBLE);
1635 schedule_timeout((xfs_buf_timer_centisecs * HZ) / 100);
1637 age = (xfs_buf_age_centisecs * HZ) / 100;
1638 spin_lock(&pbd_delwrite_lock);
1639 list_for_each_entry_safe(pb, n, &pbd_delwrite_queue, pb_list) {
1640 PB_TRACE(pb, "walkq1", (long)pagebuf_ispin(pb));
1641 ASSERT(pb->pb_flags & PBF_DELWRI);
1643 if (!pagebuf_ispin(pb) && !pagebuf_cond_lock(pb)) {
1645 time_before(jiffies,
1646 pb->pb_queuetime + age)) {
1651 pb->pb_flags &= ~PBF_DELWRI;
1652 pb->pb_flags |= PBF_WRITE;
1653 list_move(&pb->pb_list, &tmp);
1656 spin_unlock(&pbd_delwrite_lock);
1658 while (!list_empty(&tmp)) {
1659 pb = list_entry(tmp.next, xfs_buf_t, pb_list);
1660 target = pb->pb_target;
1662 list_del_init(&pb->pb_list);
1663 pagebuf_iostrategy(pb);
1665 blk_run_address_space(target->pbr_mapping);
1668 if (as_list_len > 0)
1672 } while (pagebuf_daemon_active);
1674 complete_and_exit(&pagebuf_daemon_done, 0);
1678 * Go through all incore buffers, and release buffers if they belong to
1679 * the given device. This is used in filesystem error handling to
1680 * preserve the consistency of its metadata.
1684 xfs_buftarg_t *target,
1687 struct list_head tmp;
1691 pagebuf_runall_queues(pagebuf_dataio_workqueue);
1692 pagebuf_runall_queues(pagebuf_logio_workqueue);
1694 INIT_LIST_HEAD(&tmp);
1695 spin_lock(&pbd_delwrite_lock);
1696 list_for_each_entry_safe(pb, n, &pbd_delwrite_queue, pb_list) {
1698 if (pb->pb_target != target)
1701 ASSERT(pb->pb_flags & PBF_DELWRI);
1702 PB_TRACE(pb, "walkq2", (long)pagebuf_ispin(pb));
1703 if (pagebuf_ispin(pb)) {
1708 pb->pb_flags &= ~PBF_DELWRI;
1709 pb->pb_flags |= PBF_WRITE;
1710 list_move(&pb->pb_list, &tmp);
1712 spin_unlock(&pbd_delwrite_lock);
1715 * Dropped the delayed write list lock, now walk the temporary list
1717 list_for_each_entry_safe(pb, n, &tmp, pb_list) {
1719 pb->pb_flags &= ~PBF_ASYNC;
1721 list_del_init(&pb->pb_list);
1724 pagebuf_iostrategy(pb);
1728 * Remaining list items must be flushed before returning
1730 while (!list_empty(&tmp)) {
1731 pb = list_entry(tmp.next, xfs_buf_t, pb_list);
1733 list_del_init(&pb->pb_list);
1739 blk_run_address_space(target->pbr_mapping);
1745 pagebuf_daemon_start(void)
1749 pagebuf_logio_workqueue = create_workqueue("xfslogd");
1750 if (!pagebuf_logio_workqueue)
1753 pagebuf_dataio_workqueue = create_workqueue("xfsdatad");
1754 if (!pagebuf_dataio_workqueue) {
1755 destroy_workqueue(pagebuf_logio_workqueue);
1759 rval = kernel_thread(pagebuf_daemon, NULL, CLONE_FS|CLONE_FILES);
1761 destroy_workqueue(pagebuf_logio_workqueue);
1762 destroy_workqueue(pagebuf_dataio_workqueue);
1769 * pagebuf_daemon_stop
1771 * Note: do not mark as __exit, it is called from pagebuf_terminate.
1774 pagebuf_daemon_stop(void)
1776 pagebuf_daemon_active = 0;
1778 wait_for_completion(&pagebuf_daemon_done);
1780 destroy_workqueue(pagebuf_logio_workqueue);
1781 destroy_workqueue(pagebuf_dataio_workqueue);
1785 * Initialization and Termination
1793 pagebuf_cache = kmem_cache_create("xfs_buf_t", sizeof(xfs_buf_t), 0,
1794 SLAB_HWCACHE_ALIGN, NULL, NULL);
1795 if (pagebuf_cache == NULL) {
1796 printk("XFS: couldn't init xfs_buf_t cache\n");
1797 pagebuf_terminate();
1801 #ifdef PAGEBUF_TRACE
1802 pagebuf_trace_buf = ktrace_alloc(PAGEBUF_TRACE_SIZE, KM_SLEEP);
1805 pagebuf_daemon_start();
1807 pagebuf_shake = kmem_shake_register(pagebuf_daemon_wakeup);
1808 if (pagebuf_shake == NULL) {
1809 pagebuf_terminate();
1813 for (i = 0; i < NHASH; i++) {
1814 spin_lock_init(&pbhash[i].pb_hash_lock);
1815 INIT_LIST_HEAD(&pbhash[i].pb_hash);
1823 * pagebuf_terminate.
1825 * Note: do not mark as __exit, this is also called from the __init code.
1828 pagebuf_terminate(void)
1830 pagebuf_daemon_stop();
1832 #ifdef PAGEBUF_TRACE
1833 ktrace_free(pagebuf_trace_buf);
1836 kmem_zone_destroy(pagebuf_cache);
1837 kmem_shake_deregister(pagebuf_shake);