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);
632 pagebuf_deallocate(new_pb);
633 if (unlikely(pb == NULL))
637 for (i = 0; i < pb->pb_page_count; i++)
638 mark_page_accessed(pb->pb_pages[i]);
640 if (!(pb->pb_flags & PBF_MAPPED)) {
641 error = _pagebuf_map_pages(pb, flags);
642 if (unlikely(error)) {
644 "pagebuf_get: failed to map pages\n");
649 XFS_STATS_INC(pb_get);
652 * Always fill in the block number now, the mapped cases can do
653 * their own overlay of this later.
656 pb->pb_count_desired = pb->pb_buffer_length;
658 if (flags & PBF_READ) {
659 if (PBF_NOT_DONE(pb)) {
660 PB_TRACE(pb, "get_read", (unsigned long)flags);
661 XFS_STATS_INC(pb_get_read);
662 pagebuf_iostart(pb, flags);
663 } else if (flags & PBF_ASYNC) {
664 PB_TRACE(pb, "get_read_async", (unsigned long)flags);
666 * Read ahead call which is already satisfied,
671 PB_TRACE(pb, "get_read_done", (unsigned long)flags);
672 /* We do not want read in the flags */
673 pb->pb_flags &= ~PBF_READ;
676 PB_TRACE(pb, "get_write", (unsigned long)flags);
682 if (flags & (PBF_LOCK | PBF_TRYLOCK))
689 * Create a skeletal pagebuf (no pages associated with it).
693 xfs_buftarg_t *target,
696 page_buf_flags_t flags)
700 pb = pagebuf_allocate(flags);
702 _pagebuf_initialize(pb, target, ioff, isize, flags);
708 * If we are not low on memory then do the readahead in a deadlock
713 xfs_buftarg_t *target,
716 page_buf_flags_t flags)
718 struct backing_dev_info *bdi;
720 bdi = target->pbr_mapping->backing_dev_info;
721 if (bdi_read_congested(bdi))
723 if (bdi_write_congested(bdi))
726 flags |= (PBF_TRYLOCK|PBF_READ|PBF_ASYNC|PBF_READ_AHEAD);
727 pagebuf_get(target, ioff, isize, flags);
733 xfs_buftarg_t *target)
737 pb = pagebuf_allocate(0);
739 _pagebuf_initialize(pb, target, 0, len, 0);
743 static inline struct page *
747 if (((unsigned long)addr < VMALLOC_START) ||
748 ((unsigned long)addr >= VMALLOC_END)) {
749 return virt_to_page(addr);
751 return vmalloc_to_page(addr);
756 pagebuf_associate_memory(
768 page_count = PAGE_CACHE_ALIGN(len) >> PAGE_CACHE_SHIFT;
769 offset = (off_t) mem - ((off_t)mem & PAGE_CACHE_MASK);
770 if (offset && (len > PAGE_CACHE_SIZE))
773 /* Free any previous set of page pointers */
775 _pagebuf_free_pages(pb);
780 rval = _pagebuf_get_pages(pb, page_count, 0);
784 pb->pb_offset = offset;
785 ptr = (size_t) mem & PAGE_CACHE_MASK;
786 end = PAGE_CACHE_ALIGN((size_t) mem + len);
788 /* set up first page */
789 pb->pb_pages[0] = mem_to_page(mem);
791 ptr += PAGE_CACHE_SIZE;
792 pb->pb_page_count = ++i;
794 pb->pb_pages[i] = mem_to_page((void *)ptr);
795 pb->pb_page_count = ++i;
796 ptr += PAGE_CACHE_SIZE;
800 pb->pb_count_desired = pb->pb_buffer_length = len;
801 pb->pb_flags |= PBF_MAPPED;
807 pagebuf_get_no_daddr(
809 xfs_buftarg_t *target)
811 size_t malloc_len = len;
816 bp = pagebuf_allocate(0);
817 if (unlikely(bp == NULL))
819 _pagebuf_initialize(bp, target, 0, len, PBF_FORCEIO);
822 data = kmem_alloc(malloc_len, KM_SLEEP | KM_MAYFAIL);
823 if (unlikely(data == NULL))
826 /* check whether alignment matches.. */
827 if ((__psunsigned_t)data !=
828 ((__psunsigned_t)data & ~target->pbr_smask)) {
829 /* .. else double the size and try again */
830 kmem_free(data, malloc_len);
835 error = pagebuf_associate_memory(bp, data, len);
838 bp->pb_flags |= _PBF_KMEM_ALLOC;
842 PB_TRACE(bp, "no_daddr", data);
845 kmem_free(data, malloc_len);
855 * Increment reference count on buffer, to hold the buffer concurrently
856 * with another thread which may release (free) the buffer asynchronously.
858 * Must hold the buffer already to call this function.
864 atomic_inc(&pb->pb_hold);
865 PB_TRACE(pb, "hold", 0);
871 * pagebuf_rele releases a hold on the specified buffer. If the
872 * the hold count is 1, pagebuf_rele calls pagebuf_free.
878 pb_hash_t *hash = pb_hash(pb);
880 PB_TRACE(pb, "rele", pb->pb_relse);
882 if (atomic_dec_and_lock(&pb->pb_hold, &hash->pb_hash_lock)) {
886 atomic_inc(&pb->pb_hold);
887 spin_unlock(&hash->pb_hash_lock);
888 (*(pb->pb_relse)) (pb);
889 spin_lock(&hash->pb_hash_lock);
893 if (pb->pb_flags & PBF_DELWRI) {
894 pb->pb_flags |= PBF_ASYNC;
895 atomic_inc(&pb->pb_hold);
896 pagebuf_delwri_queue(pb, 0);
898 } else if (pb->pb_flags & PBF_FS_MANAGED) {
903 list_del_init(&pb->pb_hash_list);
904 spin_unlock(&hash->pb_hash_lock);
907 spin_unlock(&hash->pb_hash_lock);
914 * Mutual exclusion on buffers. Locking model:
916 * Buffers associated with inodes for which buffer locking
917 * is not enabled are not protected by semaphores, and are
918 * assumed to be exclusively owned by the caller. There is a
919 * spinlock in the buffer, used by the caller when concurrent
920 * access is possible.
926 * pagebuf_cond_lock locks a buffer object, if it is not already locked.
927 * Note that this in no way
928 * locks the underlying pages, so it is only useful for synchronizing
929 * concurrent use of page buffer objects, not for synchronizing independent
930 * access to the underlying pages.
933 pagebuf_cond_lock( /* lock buffer, if not locked */
934 /* returns -EBUSY if locked) */
939 locked = down_trylock(&pb->pb_sema) == 0;
943 PB_TRACE(pb, "cond_lock", (long)locked);
944 return(locked ? 0 : -EBUSY);
950 * Return lock value for a pagebuf
956 return(atomic_read(&pb->pb_sema.count));
962 * pagebuf_lock locks a buffer object. Note that this in no way
963 * locks the underlying pages, so it is only useful for synchronizing
964 * concurrent use of page buffer objects, not for synchronizing independent
965 * access to the underlying pages.
971 PB_TRACE(pb, "lock", 0);
972 if (atomic_read(&pb->pb_io_remaining))
973 blk_run_address_space(pb->pb_target->pbr_mapping);
976 PB_TRACE(pb, "locked", 0);
983 * pagebuf_unlock releases the lock on the buffer object created by
984 * pagebuf_lock or pagebuf_cond_lock (not any
985 * pinning of underlying pages created by pagebuf_pin).
988 pagebuf_unlock( /* unlock buffer */
989 xfs_buf_t *pb) /* buffer to unlock */
993 PB_TRACE(pb, "unlock", 0);
998 * Pinning Buffer Storage in Memory
1004 * pagebuf_pin locks all of the memory represented by a buffer in
1005 * memory. Multiple calls to pagebuf_pin and pagebuf_unpin, for
1006 * the same or different buffers affecting a given page, will
1007 * properly count the number of outstanding "pin" requests. The
1008 * buffer may be released after the pagebuf_pin and a different
1009 * buffer used when calling pagebuf_unpin, if desired.
1010 * pagebuf_pin should be used by the file system when it wants be
1011 * assured that no attempt will be made to force the affected
1012 * memory to disk. It does not assure that a given logical page
1013 * will not be moved to a different physical page.
1019 atomic_inc(&pb->pb_pin_count);
1020 PB_TRACE(pb, "pin", (long)pb->pb_pin_count.counter);
1026 * pagebuf_unpin reverses the locking of memory performed by
1027 * pagebuf_pin. Note that both functions affected the logical
1028 * pages associated with the buffer, not the buffer itself.
1034 if (atomic_dec_and_test(&pb->pb_pin_count)) {
1035 wake_up_all(&pb->pb_waiters);
1037 PB_TRACE(pb, "unpin", (long)pb->pb_pin_count.counter);
1044 return atomic_read(&pb->pb_pin_count);
1048 * pagebuf_wait_unpin
1050 * pagebuf_wait_unpin waits until all of the memory associated
1051 * with the buffer is not longer locked in memory. It returns
1052 * immediately if none of the affected pages are locked.
1055 _pagebuf_wait_unpin(
1058 DECLARE_WAITQUEUE (wait, current);
1060 if (atomic_read(&pb->pb_pin_count) == 0)
1063 add_wait_queue(&pb->pb_waiters, &wait);
1065 set_current_state(TASK_UNINTERRUPTIBLE);
1066 if (atomic_read(&pb->pb_pin_count) == 0)
1068 if (atomic_read(&pb->pb_io_remaining))
1069 blk_run_address_space(pb->pb_target->pbr_mapping);
1072 remove_wait_queue(&pb->pb_waiters, &wait);
1073 set_current_state(TASK_RUNNING);
1077 * Buffer Utility Routines
1083 * pagebuf_iodone marks a buffer for which I/O is in progress
1084 * done with respect to that I/O. The pb_iodone routine, if
1085 * present, will be called as a side-effect.
1088 pagebuf_iodone_work(
1091 xfs_buf_t *bp = (xfs_buf_t *)v;
1094 (*(bp->pb_iodone))(bp);
1095 else if (bp->pb_flags & PBF_ASYNC)
1105 pb->pb_flags &= ~(PBF_READ | PBF_WRITE);
1106 if (pb->pb_error == 0) {
1107 pb->pb_flags &= ~(PBF_PARTIAL | PBF_NONE);
1110 PB_TRACE(pb, "iodone", pb->pb_iodone);
1112 if ((pb->pb_iodone) || (pb->pb_flags & PBF_ASYNC)) {
1114 INIT_WORK(&pb->pb_iodone_work, pagebuf_iodone_work, pb);
1115 queue_work(dataio ? pagebuf_dataio_workqueue :
1116 pagebuf_logio_workqueue, &pb->pb_iodone_work);
1118 pagebuf_iodone_work(pb);
1121 up(&pb->pb_iodonesema);
1128 * pagebuf_ioerror sets the error code for a buffer.
1131 pagebuf_ioerror( /* mark/clear buffer error flag */
1132 xfs_buf_t *pb, /* buffer to mark */
1133 int error) /* error to store (0 if none) */
1135 ASSERT(error >= 0 && error <= 0xffff);
1136 pb->pb_error = (unsigned short)error;
1137 PB_TRACE(pb, "ioerror", (unsigned long)error);
1143 * pagebuf_iostart initiates I/O on a buffer, based on the flags supplied.
1144 * If necessary, it will arrange for any disk space allocation required,
1145 * and it will break up the request if the block mappings require it.
1146 * The pb_iodone routine in the buffer supplied will only be called
1147 * when all of the subsidiary I/O requests, if any, have been completed.
1148 * pagebuf_iostart calls the pagebuf_ioinitiate routine or
1149 * pagebuf_iorequest, if the former routine is not defined, to start
1150 * the I/O on a given low-level request.
1153 pagebuf_iostart( /* start I/O on a buffer */
1154 xfs_buf_t *pb, /* buffer to start */
1155 page_buf_flags_t flags) /* PBF_LOCK, PBF_ASYNC, PBF_READ, */
1156 /* PBF_WRITE, PBF_DELWRI, */
1157 /* PBF_DONT_BLOCK */
1161 PB_TRACE(pb, "iostart", (unsigned long)flags);
1163 if (flags & PBF_DELWRI) {
1164 pb->pb_flags &= ~(PBF_READ | PBF_WRITE | PBF_ASYNC);
1165 pb->pb_flags |= flags & (PBF_DELWRI | PBF_ASYNC);
1166 pagebuf_delwri_queue(pb, 1);
1170 pb->pb_flags &= ~(PBF_READ | PBF_WRITE | PBF_ASYNC | PBF_DELWRI | \
1171 PBF_READ_AHEAD | _PBF_RUN_QUEUES);
1172 pb->pb_flags |= flags & (PBF_READ | PBF_WRITE | PBF_ASYNC | \
1173 PBF_READ_AHEAD | _PBF_RUN_QUEUES);
1175 BUG_ON(pb->pb_bn == XFS_BUF_DADDR_NULL);
1177 /* For writes allow an alternate strategy routine to precede
1178 * the actual I/O request (which may not be issued at all in
1179 * a shutdown situation, for example).
1181 status = (flags & PBF_WRITE) ?
1182 pagebuf_iostrategy(pb) : pagebuf_iorequest(pb);
1184 /* Wait for I/O if we are not an async request.
1185 * Note: async I/O request completion will release the buffer,
1186 * and that can already be done by this point. So using the
1187 * buffer pointer from here on, after async I/O, is invalid.
1189 if (!status && !(flags & PBF_ASYNC))
1190 status = pagebuf_iowait(pb);
1196 * Helper routine for pagebuf_iorequest
1199 STATIC __inline__ int
1203 ASSERT(pb->pb_flags & (PBF_READ|PBF_WRITE));
1204 if (pb->pb_flags & PBF_READ)
1205 return pb->pb_locked;
1209 STATIC __inline__ void
1214 if (atomic_dec_and_test(&pb->pb_io_remaining) == 1) {
1216 pagebuf_iodone(pb, (pb->pb_flags & PBF_FS_DATAIOD), schedule);
1223 unsigned int bytes_done,
1226 xfs_buf_t *pb = (xfs_buf_t *)bio->bi_private;
1227 unsigned int i, blocksize = pb->pb_target->pbr_bsize;
1228 unsigned int sectorshift = pb->pb_target->pbr_sshift;
1229 struct bio_vec *bvec = bio->bi_io_vec;
1234 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
1237 for (i = 0; i < bio->bi_vcnt; i++, bvec++) {
1238 struct page *page = bvec->bv_page;
1242 } else if (blocksize == PAGE_CACHE_SIZE) {
1243 SetPageUptodate(page);
1244 } else if (!PagePrivate(page) &&
1245 (pb->pb_flags & _PBF_PAGE_CACHE)) {
1246 unsigned long j, range;
1248 ASSERT(blocksize < PAGE_CACHE_SIZE);
1249 range = (bvec->bv_offset + bvec->bv_len) >> sectorshift;
1250 for (j = bvec->bv_offset >> sectorshift; j < range; j++)
1251 set_bit(j, &page->private);
1252 if (page->private == (unsigned long)(PAGE_CACHE_SIZE-1))
1253 SetPageUptodate(page);
1256 if (_pagebuf_iolocked(pb)) {
1261 _pagebuf_iodone(pb, 1);
1270 int i, map_i, total_nr_pages, nr_pages;
1272 int offset = pb->pb_offset;
1273 int size = pb->pb_count_desired;
1274 sector_t sector = pb->pb_bn;
1275 unsigned int blocksize = pb->pb_target->pbr_bsize;
1276 int locking = _pagebuf_iolocked(pb);
1278 total_nr_pages = pb->pb_page_count;
1281 /* Special code path for reading a sub page size pagebuf in --
1282 * we populate up the whole page, and hence the other metadata
1283 * in the same page. This optimization is only valid when the
1284 * filesystem block size and the page size are equal.
1286 if ((pb->pb_buffer_length < PAGE_CACHE_SIZE) &&
1287 (pb->pb_flags & PBF_READ) && locking &&
1288 (blocksize == PAGE_CACHE_SIZE)) {
1289 bio = bio_alloc(GFP_NOIO, 1);
1291 bio->bi_bdev = pb->pb_target->pbr_bdev;
1292 bio->bi_sector = sector - (offset >> BBSHIFT);
1293 bio->bi_end_io = bio_end_io_pagebuf;
1294 bio->bi_private = pb;
1296 bio_add_page(bio, pb->pb_pages[0], PAGE_CACHE_SIZE, 0);
1299 atomic_inc(&pb->pb_io_remaining);
1304 /* Lock down the pages which we need to for the request */
1305 if (locking && (pb->pb_flags & PBF_WRITE) && (pb->pb_locked == 0)) {
1306 for (i = 0; size; i++) {
1307 int nbytes = PAGE_CACHE_SIZE - offset;
1308 struct page *page = pb->pb_pages[i];
1318 offset = pb->pb_offset;
1319 size = pb->pb_count_desired;
1323 atomic_inc(&pb->pb_io_remaining);
1324 nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
1325 if (nr_pages > total_nr_pages)
1326 nr_pages = total_nr_pages;
1328 bio = bio_alloc(GFP_NOIO, nr_pages);
1329 bio->bi_bdev = pb->pb_target->pbr_bdev;
1330 bio->bi_sector = sector;
1331 bio->bi_end_io = bio_end_io_pagebuf;
1332 bio->bi_private = pb;
1334 for (; size && nr_pages; nr_pages--, map_i++) {
1335 int nbytes = PAGE_CACHE_SIZE - offset;
1340 if (bio_add_page(bio, pb->pb_pages[map_i],
1341 nbytes, offset) < nbytes)
1345 sector += nbytes >> BBSHIFT;
1351 if (likely(bio->bi_size)) {
1352 submit_bio((pb->pb_flags & PBF_READ) ? READ : WRITE, bio);
1357 pagebuf_ioerror(pb, EIO);
1360 if (pb->pb_flags & _PBF_RUN_QUEUES) {
1361 pb->pb_flags &= ~_PBF_RUN_QUEUES;
1362 if (atomic_read(&pb->pb_io_remaining) > 1)
1363 blk_run_address_space(pb->pb_target->pbr_mapping);
1368 * pagebuf_iorequest -- the core I/O request routine.
1371 pagebuf_iorequest( /* start real I/O */
1372 xfs_buf_t *pb) /* buffer to convey to device */
1374 PB_TRACE(pb, "iorequest", 0);
1376 if (pb->pb_flags & PBF_DELWRI) {
1377 pagebuf_delwri_queue(pb, 1);
1381 if (pb->pb_flags & PBF_WRITE) {
1382 _pagebuf_wait_unpin(pb);
1387 /* Set the count to 1 initially, this will stop an I/O
1388 * completion callout which happens before we have started
1389 * all the I/O from calling pagebuf_iodone too early.
1391 atomic_set(&pb->pb_io_remaining, 1);
1392 _pagebuf_ioapply(pb);
1393 _pagebuf_iodone(pb, 0);
1402 * pagebuf_iowait waits for I/O to complete on the buffer supplied.
1403 * It returns immediately if no I/O is pending. In any case, it returns
1404 * the error code, if any, or 0 if there is no error.
1410 PB_TRACE(pb, "iowait", 0);
1411 if (atomic_read(&pb->pb_io_remaining))
1412 blk_run_address_space(pb->pb_target->pbr_mapping);
1413 down(&pb->pb_iodonesema);
1414 PB_TRACE(pb, "iowaited", (long)pb->pb_error);
1415 return pb->pb_error;
1425 offset += pb->pb_offset;
1427 page = pb->pb_pages[offset >> PAGE_CACHE_SHIFT];
1428 return (caddr_t) page_address(page) + (offset & (PAGE_CACHE_SIZE - 1));
1434 * Move data into or out of a buffer.
1438 xfs_buf_t *pb, /* buffer to process */
1439 size_t boff, /* starting buffer offset */
1440 size_t bsize, /* length to copy */
1441 caddr_t data, /* data address */
1442 page_buf_rw_t mode) /* read/write flag */
1444 size_t bend, cpoff, csize;
1447 bend = boff + bsize;
1448 while (boff < bend) {
1449 page = pb->pb_pages[page_buf_btoct(boff + pb->pb_offset)];
1450 cpoff = page_buf_poff(boff + pb->pb_offset);
1451 csize = min_t(size_t,
1452 PAGE_CACHE_SIZE-cpoff, pb->pb_count_desired-boff);
1454 ASSERT(((csize + cpoff) <= PAGE_CACHE_SIZE));
1458 memset(page_address(page) + cpoff, 0, csize);
1461 memcpy(data, page_address(page) + cpoff, csize);
1464 memcpy(page_address(page) + cpoff, data, csize);
1473 * Handling of buftargs.
1481 xfs_flush_buftarg(btp, 1);
1483 xfs_blkdev_put(btp->pbr_bdev);
1484 iput(btp->pbr_mapping->host);
1485 kmem_free(btp, sizeof(*btp));
1494 invalidate_bdev(btp->pbr_bdev, 1);
1495 truncate_inode_pages(btp->pbr_mapping, 0LL);
1499 xfs_setsize_buftarg(
1501 unsigned int blocksize,
1502 unsigned int sectorsize)
1504 btp->pbr_bsize = blocksize;
1505 btp->pbr_sshift = ffs(sectorsize) - 1;
1506 btp->pbr_smask = sectorsize - 1;
1508 if (set_blocksize(btp->pbr_bdev, sectorsize)) {
1510 "XFS: Cannot set_blocksize to %u on device %s\n",
1511 sectorsize, XFS_BUFTARG_NAME(btp));
1518 xfs_mapping_buftarg(
1520 struct block_device *bdev)
1522 struct backing_dev_info *bdi;
1523 struct inode *inode;
1524 struct address_space *mapping;
1525 static struct address_space_operations mapping_aops = {
1526 .sync_page = block_sync_page,
1529 inode = new_inode(bdev->bd_inode->i_sb);
1532 "XFS: Cannot allocate mapping inode for device %s\n",
1533 XFS_BUFTARG_NAME(btp));
1536 inode->i_mode = S_IFBLK;
1537 inode->i_bdev = bdev;
1538 inode->i_rdev = bdev->bd_dev;
1539 bdi = blk_get_backing_dev_info(bdev);
1541 bdi = &default_backing_dev_info;
1542 mapping = &inode->i_data;
1543 mapping->a_ops = &mapping_aops;
1544 mapping->backing_dev_info = bdi;
1545 mapping_set_gfp_mask(mapping, GFP_KERNEL);
1546 btp->pbr_mapping = mapping;
1552 struct block_device *bdev)
1556 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
1558 btp->pbr_dev = bdev->bd_dev;
1559 btp->pbr_bdev = bdev;
1560 if (xfs_setsize_buftarg(btp, PAGE_CACHE_SIZE, bdev_hardsect_size(bdev)))
1562 if (xfs_mapping_buftarg(btp, bdev))
1567 kmem_free(btp, sizeof(*btp));
1573 * Pagebuf delayed write buffer handling
1576 STATIC LIST_HEAD(pbd_delwrite_queue);
1577 STATIC spinlock_t pbd_delwrite_lock = SPIN_LOCK_UNLOCKED;
1580 pagebuf_delwri_queue(
1584 PB_TRACE(pb, "delwri_q", (long)unlock);
1585 ASSERT(pb->pb_flags & PBF_DELWRI);
1587 spin_lock(&pbd_delwrite_lock);
1588 /* If already in the queue, dequeue and place at tail */
1589 if (!list_empty(&pb->pb_list)) {
1591 atomic_dec(&pb->pb_hold);
1593 list_del(&pb->pb_list);
1596 list_add_tail(&pb->pb_list, &pbd_delwrite_queue);
1597 pb->pb_queuetime = jiffies;
1598 spin_unlock(&pbd_delwrite_lock);
1605 pagebuf_delwri_dequeue(
1610 spin_lock(&pbd_delwrite_lock);
1611 if ((pb->pb_flags & PBF_DELWRI) && !list_empty(&pb->pb_list)) {
1612 list_del_init(&pb->pb_list);
1615 pb->pb_flags &= ~PBF_DELWRI;
1616 spin_unlock(&pbd_delwrite_lock);
1621 PB_TRACE(pb, "delwri_dq", (long)dequeued);
1625 pagebuf_runall_queues(
1626 struct workqueue_struct *queue)
1628 flush_workqueue(queue);
1631 /* Defines for pagebuf daemon */
1632 STATIC DECLARE_COMPLETION(pagebuf_daemon_done);
1633 STATIC struct task_struct *pagebuf_daemon_task;
1634 STATIC int pagebuf_daemon_active;
1635 STATIC int force_flush;
1639 pagebuf_daemon_wakeup(
1645 wake_up_process(pagebuf_daemon_task);
1653 struct list_head tmp;
1655 xfs_buftarg_t *target;
1658 /* Set up the thread */
1659 daemonize("xfsbufd");
1660 current->flags |= PF_MEMALLOC;
1662 pagebuf_daemon_task = current;
1663 pagebuf_daemon_active = 1;
1666 INIT_LIST_HEAD(&tmp);
1669 if (current->flags & PF_FREEZE)
1670 refrigerator(PF_FREEZE);
1672 set_current_state(TASK_INTERRUPTIBLE);
1673 schedule_timeout((xfs_buf_timer_centisecs * HZ) / 100);
1675 age = (xfs_buf_age_centisecs * HZ) / 100;
1676 spin_lock(&pbd_delwrite_lock);
1677 list_for_each_entry_safe(pb, n, &pbd_delwrite_queue, pb_list) {
1678 PB_TRACE(pb, "walkq1", (long)pagebuf_ispin(pb));
1679 ASSERT(pb->pb_flags & PBF_DELWRI);
1681 if (!pagebuf_ispin(pb) && !pagebuf_cond_lock(pb)) {
1683 time_before(jiffies,
1684 pb->pb_queuetime + age)) {
1689 pb->pb_flags &= ~PBF_DELWRI;
1690 pb->pb_flags |= PBF_WRITE;
1691 list_move(&pb->pb_list, &tmp);
1694 spin_unlock(&pbd_delwrite_lock);
1696 while (!list_empty(&tmp)) {
1697 pb = list_entry(tmp.next, xfs_buf_t, pb_list);
1698 target = pb->pb_target;
1700 list_del_init(&pb->pb_list);
1701 pagebuf_iostrategy(pb);
1703 blk_run_address_space(target->pbr_mapping);
1706 if (as_list_len > 0)
1710 } while (pagebuf_daemon_active);
1712 complete_and_exit(&pagebuf_daemon_done, 0);
1716 * Go through all incore buffers, and release buffers if they belong to
1717 * the given device. This is used in filesystem error handling to
1718 * preserve the consistency of its metadata.
1722 xfs_buftarg_t *target,
1725 struct list_head tmp;
1729 pagebuf_runall_queues(pagebuf_dataio_workqueue);
1730 pagebuf_runall_queues(pagebuf_logio_workqueue);
1732 INIT_LIST_HEAD(&tmp);
1733 spin_lock(&pbd_delwrite_lock);
1734 list_for_each_entry_safe(pb, n, &pbd_delwrite_queue, pb_list) {
1736 if (pb->pb_target != target)
1739 ASSERT(pb->pb_flags & PBF_DELWRI);
1740 PB_TRACE(pb, "walkq2", (long)pagebuf_ispin(pb));
1741 if (pagebuf_ispin(pb)) {
1746 pb->pb_flags &= ~PBF_DELWRI;
1747 pb->pb_flags |= PBF_WRITE;
1748 list_move(&pb->pb_list, &tmp);
1750 spin_unlock(&pbd_delwrite_lock);
1753 * Dropped the delayed write list lock, now walk the temporary list
1755 list_for_each_entry_safe(pb, n, &tmp, pb_list) {
1757 pb->pb_flags &= ~PBF_ASYNC;
1759 list_del_init(&pb->pb_list);
1762 pagebuf_iostrategy(pb);
1766 * Remaining list items must be flushed before returning
1768 while (!list_empty(&tmp)) {
1769 pb = list_entry(tmp.next, xfs_buf_t, pb_list);
1771 list_del_init(&pb->pb_list);
1777 blk_run_address_space(target->pbr_mapping);
1783 pagebuf_daemon_start(void)
1787 pagebuf_logio_workqueue = create_workqueue("xfslogd");
1788 if (!pagebuf_logio_workqueue)
1791 pagebuf_dataio_workqueue = create_workqueue("xfsdatad");
1792 if (!pagebuf_dataio_workqueue) {
1793 destroy_workqueue(pagebuf_logio_workqueue);
1797 rval = kernel_thread(pagebuf_daemon, NULL, CLONE_FS|CLONE_FILES);
1799 destroy_workqueue(pagebuf_logio_workqueue);
1800 destroy_workqueue(pagebuf_dataio_workqueue);
1807 * pagebuf_daemon_stop
1809 * Note: do not mark as __exit, it is called from pagebuf_terminate.
1812 pagebuf_daemon_stop(void)
1814 pagebuf_daemon_active = 0;
1816 wait_for_completion(&pagebuf_daemon_done);
1818 destroy_workqueue(pagebuf_logio_workqueue);
1819 destroy_workqueue(pagebuf_dataio_workqueue);
1823 * Initialization and Termination
1831 pagebuf_cache = kmem_cache_create("xfs_buf_t", sizeof(xfs_buf_t), 0,
1832 SLAB_HWCACHE_ALIGN, NULL, NULL);
1833 if (pagebuf_cache == NULL) {
1834 printk("XFS: couldn't init xfs_buf_t cache\n");
1835 pagebuf_terminate();
1839 #ifdef PAGEBUF_TRACE
1840 pagebuf_trace_buf = ktrace_alloc(PAGEBUF_TRACE_SIZE, KM_SLEEP);
1843 pagebuf_daemon_start();
1845 pagebuf_shake = kmem_shake_register(pagebuf_daemon_wakeup);
1846 if (pagebuf_shake == NULL) {
1847 pagebuf_terminate();
1851 for (i = 0; i < NHASH; i++) {
1852 spin_lock_init(&pbhash[i].pb_hash_lock);
1853 INIT_LIST_HEAD(&pbhash[i].pb_hash);
1861 * pagebuf_terminate.
1863 * Note: do not mark as __exit, this is also called from the __init code.
1866 pagebuf_terminate(void)
1868 pagebuf_daemon_stop();
1870 #ifdef PAGEBUF_TRACE
1871 ktrace_free(pagebuf_trace_buf);
1874 kmem_zone_destroy(pagebuf_cache);
1875 kmem_shake_deregister(pagebuf_shake);