added 2.6.29.6 aldebaran kernel
[nao-ulib.git] / kernel / 2.6.29.6-aldebaran-rt / fs / xfs / linux-2.6 / xfs_buf.c
blobaa1016bb913405e4e4f77c5bee28e7f38db2b44c
1 /*
2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
3 * All Rights Reserved.
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 "xfs.h"
19 #include <linux/stddef.h>
20 #include <linux/errno.h>
21 #include <linux/slab.h>
22 #include <linux/pagemap.h>
23 #include <linux/init.h>
24 #include <linux/vmalloc.h>
25 #include <linux/bio.h>
26 #include <linux/sysctl.h>
27 #include <linux/proc_fs.h>
28 #include <linux/workqueue.h>
29 #include <linux/percpu.h>
30 #include <linux/blkdev.h>
31 #include <linux/hash.h>
32 #include <linux/kthread.h>
33 #include <linux/migrate.h>
34 #include <linux/backing-dev.h>
35 #include <linux/freezer.h>
37 #include "xfs_sb.h"
38 #include "xfs_inum.h"
39 #include "xfs_ag.h"
40 #include "xfs_dmapi.h"
41 #include "xfs_mount.h"
43 static kmem_zone_t *xfs_buf_zone;
44 STATIC int xfsbufd(void *);
45 STATIC int xfsbufd_wakeup(int, gfp_t);
46 STATIC void xfs_buf_delwri_queue(xfs_buf_t *, int);
47 static struct shrinker xfs_buf_shake = {
48 .shrink = xfsbufd_wakeup,
49 .seeks = DEFAULT_SEEKS,
52 static struct workqueue_struct *xfslogd_workqueue;
53 struct workqueue_struct *xfsdatad_workqueue;
55 #ifdef XFS_BUF_TRACE
56 void
57 xfs_buf_trace(
58 xfs_buf_t *bp,
59 char *id,
60 void *data,
61 void *ra)
63 ktrace_enter(xfs_buf_trace_buf,
64 bp, id,
65 (void *)(unsigned long)bp->b_flags,
66 (void *)(unsigned long)bp->b_hold.counter,
67 (void *)(unsigned long)bp->b_sema.count,
68 (void *)current,
69 data, ra,
70 (void *)(unsigned long)((bp->b_file_offset>>32) & 0xffffffff),
71 (void *)(unsigned long)(bp->b_file_offset & 0xffffffff),
72 (void *)(unsigned long)bp->b_buffer_length,
73 NULL, NULL, NULL, NULL, NULL);
75 ktrace_t *xfs_buf_trace_buf;
76 #define XFS_BUF_TRACE_SIZE 4096
77 #define XB_TRACE(bp, id, data) \
78 xfs_buf_trace(bp, id, (void *)data, (void *)__builtin_return_address(0))
79 #else
80 #define XB_TRACE(bp, id, data) do { } while (0)
81 #endif
83 #ifdef XFS_BUF_LOCK_TRACKING
84 # define XB_SET_OWNER(bp) ((bp)->b_last_holder = current->pid)
85 # define XB_CLEAR_OWNER(bp) ((bp)->b_last_holder = -1)
86 # define XB_GET_OWNER(bp) ((bp)->b_last_holder)
87 #else
88 # define XB_SET_OWNER(bp) do { } while (0)
89 # define XB_CLEAR_OWNER(bp) do { } while (0)
90 # define XB_GET_OWNER(bp) do { } while (0)
91 #endif
93 #define xb_to_gfp(flags) \
94 ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : \
95 ((flags) & XBF_DONT_BLOCK) ? GFP_NOFS : GFP_KERNEL) | __GFP_NOWARN)
97 #define xb_to_km(flags) \
98 (((flags) & XBF_DONT_BLOCK) ? KM_NOFS : KM_SLEEP)
100 #define xfs_buf_allocate(flags) \
101 kmem_zone_alloc(xfs_buf_zone, xb_to_km(flags))
102 #define xfs_buf_deallocate(bp) \
103 kmem_zone_free(xfs_buf_zone, (bp));
106 * Page Region interfaces.
108 * For pages in filesystems where the blocksize is smaller than the
109 * pagesize, we use the page->private field (long) to hold a bitmap
110 * of uptodate regions within the page.
112 * Each such region is "bytes per page / bits per long" bytes long.
114 * NBPPR == number-of-bytes-per-page-region
115 * BTOPR == bytes-to-page-region (rounded up)
116 * BTOPRT == bytes-to-page-region-truncated (rounded down)
118 #if (BITS_PER_LONG == 32)
119 #define PRSHIFT (PAGE_CACHE_SHIFT - 5) /* (32 == 1<<5) */
120 #elif (BITS_PER_LONG == 64)
121 #define PRSHIFT (PAGE_CACHE_SHIFT - 6) /* (64 == 1<<6) */
122 #else
123 #error BITS_PER_LONG must be 32 or 64
124 #endif
125 #define NBPPR (PAGE_CACHE_SIZE/BITS_PER_LONG)
126 #define BTOPR(b) (((unsigned int)(b) + (NBPPR - 1)) >> PRSHIFT)
127 #define BTOPRT(b) (((unsigned int)(b) >> PRSHIFT))
129 STATIC unsigned long
130 page_region_mask(
131 size_t offset,
132 size_t length)
134 unsigned long mask;
135 int first, final;
137 first = BTOPR(offset);
138 final = BTOPRT(offset + length - 1);
139 first = min(first, final);
141 mask = ~0UL;
142 mask <<= BITS_PER_LONG - (final - first);
143 mask >>= BITS_PER_LONG - (final);
145 ASSERT(offset + length <= PAGE_CACHE_SIZE);
146 ASSERT((final - first) < BITS_PER_LONG && (final - first) >= 0);
148 return mask;
151 STATIC_INLINE void
152 set_page_region(
153 struct page *page,
154 size_t offset,
155 size_t length)
157 set_page_private(page,
158 page_private(page) | page_region_mask(offset, length));
159 if (page_private(page) == ~0UL)
160 SetPageUptodate(page);
163 STATIC_INLINE int
164 test_page_region(
165 struct page *page,
166 size_t offset,
167 size_t length)
169 unsigned long mask = page_region_mask(offset, length);
171 return (mask && (page_private(page) & mask) == mask);
175 * Mapping of multi-page buffers into contiguous virtual space
178 typedef struct a_list {
179 void *vm_addr;
180 struct a_list *next;
181 } a_list_t;
183 static a_list_t *as_free_head;
184 static int as_list_len;
185 static DEFINE_SPINLOCK(as_lock);
188 * Try to batch vunmaps because they are costly.
190 STATIC void
191 free_address(
192 void *addr)
194 a_list_t *aentry;
196 #ifdef CONFIG_XEN
198 * Xen needs to be able to make sure it can get an exclusive
199 * RO mapping of pages it wants to turn into a pagetable. If
200 * a newly allocated page is also still being vmap()ed by xfs,
201 * it will cause pagetable construction to fail. This is a
202 * quick workaround to always eagerly unmap pages so that Xen
203 * is happy.
205 vunmap(addr);
206 return;
207 #endif
209 aentry = kmalloc(sizeof(a_list_t), GFP_NOWAIT);
210 if (likely(aentry)) {
211 spin_lock(&as_lock);
212 aentry->next = as_free_head;
213 aentry->vm_addr = addr;
214 as_free_head = aentry;
215 as_list_len++;
216 spin_unlock(&as_lock);
217 } else {
218 vunmap(addr);
222 STATIC void
223 purge_addresses(void)
225 a_list_t *aentry, *old;
227 if (as_free_head == NULL)
228 return;
230 spin_lock(&as_lock);
231 aentry = as_free_head;
232 as_free_head = NULL;
233 as_list_len = 0;
234 spin_unlock(&as_lock);
236 while ((old = aentry) != NULL) {
237 vunmap(aentry->vm_addr);
238 aentry = aentry->next;
239 kfree(old);
244 * Internal xfs_buf_t object manipulation
247 STATIC void
248 _xfs_buf_initialize(
249 xfs_buf_t *bp,
250 xfs_buftarg_t *target,
251 xfs_off_t range_base,
252 size_t range_length,
253 xfs_buf_flags_t flags)
256 * We don't want certain flags to appear in b_flags.
258 flags &= ~(XBF_LOCK|XBF_MAPPED|XBF_DONT_BLOCK|XBF_READ_AHEAD);
260 memset(bp, 0, sizeof(xfs_buf_t));
261 atomic_set(&bp->b_hold, 1);
262 init_completion(&bp->b_iowait);
263 INIT_LIST_HEAD(&bp->b_list);
264 INIT_LIST_HEAD(&bp->b_hash_list);
265 init_MUTEX_LOCKED(&bp->b_sema); /* held, no waiters */
266 XB_SET_OWNER(bp);
267 bp->b_target = target;
268 bp->b_file_offset = range_base;
270 * Set buffer_length and count_desired to the same value initially.
271 * I/O routines should use count_desired, which will be the same in
272 * most cases but may be reset (e.g. XFS recovery).
274 bp->b_buffer_length = bp->b_count_desired = range_length;
275 bp->b_flags = flags;
276 bp->b_bn = XFS_BUF_DADDR_NULL;
277 atomic_set(&bp->b_pin_count, 0);
278 init_waitqueue_head(&bp->b_waiters);
280 XFS_STATS_INC(xb_create);
281 XB_TRACE(bp, "initialize", target);
285 * Allocate a page array capable of holding a specified number
286 * of pages, and point the page buf at it.
288 STATIC int
289 _xfs_buf_get_pages(
290 xfs_buf_t *bp,
291 int page_count,
292 xfs_buf_flags_t flags)
294 /* Make sure that we have a page list */
295 if (bp->b_pages == NULL) {
296 bp->b_offset = xfs_buf_poff(bp->b_file_offset);
297 bp->b_page_count = page_count;
298 if (page_count <= XB_PAGES) {
299 bp->b_pages = bp->b_page_array;
300 } else {
301 bp->b_pages = kmem_alloc(sizeof(struct page *) *
302 page_count, xb_to_km(flags));
303 if (bp->b_pages == NULL)
304 return -ENOMEM;
306 memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
308 return 0;
312 * Frees b_pages if it was allocated.
314 STATIC void
315 _xfs_buf_free_pages(
316 xfs_buf_t *bp)
318 if (bp->b_pages != bp->b_page_array) {
319 kmem_free(bp->b_pages);
324 * Releases the specified buffer.
326 * The modification state of any associated pages is left unchanged.
327 * The buffer most not be on any hash - use xfs_buf_rele instead for
328 * hashed and refcounted buffers
330 void
331 xfs_buf_free(
332 xfs_buf_t *bp)
334 XB_TRACE(bp, "free", 0);
336 ASSERT(list_empty(&bp->b_hash_list));
338 if (bp->b_flags & (_XBF_PAGE_CACHE|_XBF_PAGES)) {
339 uint i;
341 if ((bp->b_flags & XBF_MAPPED) && (bp->b_page_count > 1))
342 free_address(bp->b_addr - bp->b_offset);
344 for (i = 0; i < bp->b_page_count; i++) {
345 struct page *page = bp->b_pages[i];
347 if (bp->b_flags & _XBF_PAGE_CACHE)
348 ASSERT(!PagePrivate(page));
349 page_cache_release(page);
351 _xfs_buf_free_pages(bp);
354 xfs_buf_deallocate(bp);
358 * Finds all pages for buffer in question and builds it's page list.
360 STATIC int
361 _xfs_buf_lookup_pages(
362 xfs_buf_t *bp,
363 uint flags)
365 struct address_space *mapping = bp->b_target->bt_mapping;
366 size_t blocksize = bp->b_target->bt_bsize;
367 size_t size = bp->b_count_desired;
368 size_t nbytes, offset;
369 gfp_t gfp_mask = xb_to_gfp(flags);
370 unsigned short page_count, i;
371 pgoff_t first;
372 xfs_off_t end;
373 int error;
375 end = bp->b_file_offset + bp->b_buffer_length;
376 page_count = xfs_buf_btoc(end) - xfs_buf_btoct(bp->b_file_offset);
378 error = _xfs_buf_get_pages(bp, page_count, flags);
379 if (unlikely(error))
380 return error;
381 bp->b_flags |= _XBF_PAGE_CACHE;
383 offset = bp->b_offset;
384 first = bp->b_file_offset >> PAGE_CACHE_SHIFT;
386 for (i = 0; i < bp->b_page_count; i++) {
387 struct page *page;
388 uint retries = 0;
390 retry:
391 page = find_or_create_page(mapping, first + i, gfp_mask);
392 if (unlikely(page == NULL)) {
393 if (flags & XBF_READ_AHEAD) {
394 bp->b_page_count = i;
395 for (i = 0; i < bp->b_page_count; i++)
396 unlock_page(bp->b_pages[i]);
397 return -ENOMEM;
401 * This could deadlock.
403 * But until all the XFS lowlevel code is revamped to
404 * handle buffer allocation failures we can't do much.
406 if (!(++retries % 100))
407 printk(KERN_ERR
408 "XFS: possible memory allocation "
409 "deadlock in %s (mode:0x%x)\n",
410 __func__, gfp_mask);
412 XFS_STATS_INC(xb_page_retries);
413 xfsbufd_wakeup(0, gfp_mask);
414 congestion_wait(WRITE, HZ/50);
415 goto retry;
418 XFS_STATS_INC(xb_page_found);
420 nbytes = min_t(size_t, size, PAGE_CACHE_SIZE - offset);
421 size -= nbytes;
423 ASSERT(!PagePrivate(page));
424 if (!PageUptodate(page)) {
425 page_count--;
426 if (blocksize >= PAGE_CACHE_SIZE) {
427 if (flags & XBF_READ)
428 bp->b_flags |= _XBF_PAGE_LOCKED;
429 } else if (!PagePrivate(page)) {
430 if (test_page_region(page, offset, nbytes))
431 page_count++;
435 bp->b_pages[i] = page;
436 offset = 0;
439 if (!(bp->b_flags & _XBF_PAGE_LOCKED)) {
440 for (i = 0; i < bp->b_page_count; i++)
441 unlock_page(bp->b_pages[i]);
444 if (page_count == bp->b_page_count)
445 bp->b_flags |= XBF_DONE;
447 XB_TRACE(bp, "lookup_pages", (long)page_count);
448 return error;
452 * Map buffer into kernel address-space if nessecary.
454 STATIC int
455 _xfs_buf_map_pages(
456 xfs_buf_t *bp,
457 uint flags)
459 /* A single page buffer is always mappable */
460 if (bp->b_page_count == 1) {
461 bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
462 bp->b_flags |= XBF_MAPPED;
463 } else if (flags & XBF_MAPPED) {
464 if (as_list_len > 64)
465 purge_addresses();
466 bp->b_addr = vmap(bp->b_pages, bp->b_page_count,
467 VM_MAP, PAGE_KERNEL);
468 if (unlikely(bp->b_addr == NULL))
469 return -ENOMEM;
470 bp->b_addr += bp->b_offset;
471 bp->b_flags |= XBF_MAPPED;
474 return 0;
478 * Finding and Reading Buffers
482 * Look up, and creates if absent, a lockable buffer for
483 * a given range of an inode. The buffer is returned
484 * locked. If other overlapping buffers exist, they are
485 * released before the new buffer is created and locked,
486 * which may imply that this call will block until those buffers
487 * are unlocked. No I/O is implied by this call.
489 xfs_buf_t *
490 _xfs_buf_find(
491 xfs_buftarg_t *btp, /* block device target */
492 xfs_off_t ioff, /* starting offset of range */
493 size_t isize, /* length of range */
494 xfs_buf_flags_t flags,
495 xfs_buf_t *new_bp)
497 xfs_off_t range_base;
498 size_t range_length;
499 xfs_bufhash_t *hash;
500 xfs_buf_t *bp, *n;
502 range_base = (ioff << BBSHIFT);
503 range_length = (isize << BBSHIFT);
505 /* Check for IOs smaller than the sector size / not sector aligned */
506 ASSERT(!(range_length < (1 << btp->bt_sshift)));
507 ASSERT(!(range_base & (xfs_off_t)btp->bt_smask));
509 hash = &btp->bt_hash[hash_long((unsigned long)ioff, btp->bt_hashshift)];
511 spin_lock(&hash->bh_lock);
513 list_for_each_entry_safe(bp, n, &hash->bh_list, b_hash_list) {
514 ASSERT(btp == bp->b_target);
515 if (bp->b_file_offset == range_base &&
516 bp->b_buffer_length == range_length) {
518 * If we look at something, bring it to the
519 * front of the list for next time.
521 atomic_inc(&bp->b_hold);
522 list_move(&bp->b_hash_list, &hash->bh_list);
523 goto found;
527 /* No match found */
528 if (new_bp) {
529 _xfs_buf_initialize(new_bp, btp, range_base,
530 range_length, flags);
531 new_bp->b_hash = hash;
532 list_add(&new_bp->b_hash_list, &hash->bh_list);
533 } else {
534 XFS_STATS_INC(xb_miss_locked);
537 spin_unlock(&hash->bh_lock);
538 return new_bp;
540 found:
541 spin_unlock(&hash->bh_lock);
543 /* Attempt to get the semaphore without sleeping,
544 * if this does not work then we need to drop the
545 * spinlock and do a hard attempt on the semaphore.
547 if (down_trylock(&bp->b_sema)) {
548 if (!(flags & XBF_TRYLOCK)) {
549 /* wait for buffer ownership */
550 XB_TRACE(bp, "get_lock", 0);
551 xfs_buf_lock(bp);
552 XFS_STATS_INC(xb_get_locked_waited);
553 } else {
554 /* We asked for a trylock and failed, no need
555 * to look at file offset and length here, we
556 * know that this buffer at least overlaps our
557 * buffer and is locked, therefore our buffer
558 * either does not exist, or is this buffer.
560 xfs_buf_rele(bp);
561 XFS_STATS_INC(xb_busy_locked);
562 return NULL;
564 } else {
565 /* trylock worked */
566 XB_SET_OWNER(bp);
569 if (bp->b_flags & XBF_STALE) {
570 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
571 bp->b_flags &= XBF_MAPPED;
573 XB_TRACE(bp, "got_lock", 0);
574 XFS_STATS_INC(xb_get_locked);
575 return bp;
579 * Assembles a buffer covering the specified range.
580 * Storage in memory for all portions of the buffer will be allocated,
581 * although backing storage may not be.
583 xfs_buf_t *
584 xfs_buf_get_flags(
585 xfs_buftarg_t *target,/* target for buffer */
586 xfs_off_t ioff, /* starting offset of range */
587 size_t isize, /* length of range */
588 xfs_buf_flags_t flags)
590 xfs_buf_t *bp, *new_bp;
591 int error = 0, i;
593 new_bp = xfs_buf_allocate(flags);
594 if (unlikely(!new_bp))
595 return NULL;
597 bp = _xfs_buf_find(target, ioff, isize, flags, new_bp);
598 if (bp == new_bp) {
599 error = _xfs_buf_lookup_pages(bp, flags);
600 if (error)
601 goto no_buffer;
602 } else {
603 xfs_buf_deallocate(new_bp);
604 if (unlikely(bp == NULL))
605 return NULL;
608 for (i = 0; i < bp->b_page_count; i++)
609 mark_page_accessed(bp->b_pages[i]);
611 if (!(bp->b_flags & XBF_MAPPED)) {
612 error = _xfs_buf_map_pages(bp, flags);
613 if (unlikely(error)) {
614 printk(KERN_WARNING "%s: failed to map pages\n",
615 __func__);
616 goto no_buffer;
620 XFS_STATS_INC(xb_get);
623 * Always fill in the block number now, the mapped cases can do
624 * their own overlay of this later.
626 bp->b_bn = ioff;
627 bp->b_count_desired = bp->b_buffer_length;
629 XB_TRACE(bp, "get", (unsigned long)flags);
630 return bp;
632 no_buffer:
633 if (flags & (XBF_LOCK | XBF_TRYLOCK))
634 xfs_buf_unlock(bp);
635 xfs_buf_rele(bp);
636 return NULL;
639 STATIC int
640 _xfs_buf_read(
641 xfs_buf_t *bp,
642 xfs_buf_flags_t flags)
644 int status;
646 XB_TRACE(bp, "_xfs_buf_read", (unsigned long)flags);
648 ASSERT(!(flags & (XBF_DELWRI|XBF_WRITE)));
649 ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL);
651 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_DELWRI | \
652 XBF_READ_AHEAD | _XBF_RUN_QUEUES);
653 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | \
654 XBF_READ_AHEAD | _XBF_RUN_QUEUES);
656 status = xfs_buf_iorequest(bp);
657 if (!status && !(flags & XBF_ASYNC))
658 status = xfs_buf_iowait(bp);
659 return status;
662 xfs_buf_t *
663 xfs_buf_read_flags(
664 xfs_buftarg_t *target,
665 xfs_off_t ioff,
666 size_t isize,
667 xfs_buf_flags_t flags)
669 xfs_buf_t *bp;
671 flags |= XBF_READ;
673 bp = xfs_buf_get_flags(target, ioff, isize, flags);
674 if (bp) {
675 if (!XFS_BUF_ISDONE(bp)) {
676 XB_TRACE(bp, "read", (unsigned long)flags);
677 XFS_STATS_INC(xb_get_read);
678 _xfs_buf_read(bp, flags);
679 } else if (flags & XBF_ASYNC) {
680 XB_TRACE(bp, "read_async", (unsigned long)flags);
682 * Read ahead call which is already satisfied,
683 * drop the buffer
685 goto no_buffer;
686 } else {
687 XB_TRACE(bp, "read_done", (unsigned long)flags);
688 /* We do not want read in the flags */
689 bp->b_flags &= ~XBF_READ;
693 return bp;
695 no_buffer:
696 if (flags & (XBF_LOCK | XBF_TRYLOCK))
697 xfs_buf_unlock(bp);
698 xfs_buf_rele(bp);
699 return NULL;
703 * If we are not low on memory then do the readahead in a deadlock
704 * safe manner.
706 void
707 xfs_buf_readahead(
708 xfs_buftarg_t *target,
709 xfs_off_t ioff,
710 size_t isize,
711 xfs_buf_flags_t flags)
713 struct backing_dev_info *bdi;
715 bdi = target->bt_mapping->backing_dev_info;
716 if (bdi_read_congested(bdi))
717 return;
719 flags |= (XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD);
720 xfs_buf_read_flags(target, ioff, isize, flags);
723 xfs_buf_t *
724 xfs_buf_get_empty(
725 size_t len,
726 xfs_buftarg_t *target)
728 xfs_buf_t *bp;
730 bp = xfs_buf_allocate(0);
731 if (bp)
732 _xfs_buf_initialize(bp, target, 0, len, 0);
733 return bp;
736 static inline struct page *
737 mem_to_page(
738 void *addr)
740 if ((!is_vmalloc_addr(addr))) {
741 return virt_to_page(addr);
742 } else {
743 return vmalloc_to_page(addr);
748 xfs_buf_associate_memory(
749 xfs_buf_t *bp,
750 void *mem,
751 size_t len)
753 int rval;
754 int i = 0;
755 unsigned long pageaddr;
756 unsigned long offset;
757 size_t buflen;
758 int page_count;
760 pageaddr = (unsigned long)mem & PAGE_CACHE_MASK;
761 offset = (unsigned long)mem - pageaddr;
762 buflen = PAGE_CACHE_ALIGN(len + offset);
763 page_count = buflen >> PAGE_CACHE_SHIFT;
765 /* Free any previous set of page pointers */
766 if (bp->b_pages)
767 _xfs_buf_free_pages(bp);
769 bp->b_pages = NULL;
770 bp->b_addr = mem;
772 rval = _xfs_buf_get_pages(bp, page_count, 0);
773 if (rval)
774 return rval;
776 bp->b_offset = offset;
778 for (i = 0; i < bp->b_page_count; i++) {
779 bp->b_pages[i] = mem_to_page((void *)pageaddr);
780 pageaddr += PAGE_CACHE_SIZE;
783 bp->b_count_desired = len;
784 bp->b_buffer_length = buflen;
785 bp->b_flags |= XBF_MAPPED;
786 bp->b_flags &= ~_XBF_PAGE_LOCKED;
788 return 0;
791 xfs_buf_t *
792 xfs_buf_get_noaddr(
793 size_t len,
794 xfs_buftarg_t *target)
796 unsigned long page_count = PAGE_ALIGN(len) >> PAGE_SHIFT;
797 int error, i;
798 xfs_buf_t *bp;
800 bp = xfs_buf_allocate(0);
801 if (unlikely(bp == NULL))
802 goto fail;
803 _xfs_buf_initialize(bp, target, 0, len, 0);
805 error = _xfs_buf_get_pages(bp, page_count, 0);
806 if (error)
807 goto fail_free_buf;
809 for (i = 0; i < page_count; i++) {
810 bp->b_pages[i] = alloc_page(GFP_KERNEL);
811 if (!bp->b_pages[i])
812 goto fail_free_mem;
814 bp->b_flags |= _XBF_PAGES;
816 error = _xfs_buf_map_pages(bp, XBF_MAPPED);
817 if (unlikely(error)) {
818 printk(KERN_WARNING "%s: failed to map pages\n",
819 __func__);
820 goto fail_free_mem;
823 xfs_buf_unlock(bp);
825 XB_TRACE(bp, "no_daddr", len);
826 return bp;
828 fail_free_mem:
829 while (--i >= 0)
830 __free_page(bp->b_pages[i]);
831 _xfs_buf_free_pages(bp);
832 fail_free_buf:
833 xfs_buf_deallocate(bp);
834 fail:
835 return NULL;
839 * Increment reference count on buffer, to hold the buffer concurrently
840 * with another thread which may release (free) the buffer asynchronously.
841 * Must hold the buffer already to call this function.
843 void
844 xfs_buf_hold(
845 xfs_buf_t *bp)
847 atomic_inc(&bp->b_hold);
848 XB_TRACE(bp, "hold", 0);
852 * Releases a hold on the specified buffer. If the
853 * the hold count is 1, calls xfs_buf_free.
855 void
856 xfs_buf_rele(
857 xfs_buf_t *bp)
859 xfs_bufhash_t *hash = bp->b_hash;
861 XB_TRACE(bp, "rele", bp->b_relse);
863 if (unlikely(!hash)) {
864 ASSERT(!bp->b_relse);
865 if (atomic_dec_and_test(&bp->b_hold))
866 xfs_buf_free(bp);
867 return;
870 ASSERT(atomic_read(&bp->b_hold) > 0);
871 if (atomic_dec_and_lock(&bp->b_hold, &hash->bh_lock)) {
872 if (bp->b_relse) {
873 atomic_inc(&bp->b_hold);
874 spin_unlock(&hash->bh_lock);
875 (*(bp->b_relse)) (bp);
876 } else if (bp->b_flags & XBF_FS_MANAGED) {
877 spin_unlock(&hash->bh_lock);
878 } else {
879 ASSERT(!(bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)));
880 list_del_init(&bp->b_hash_list);
881 spin_unlock(&hash->bh_lock);
882 xfs_buf_free(bp);
889 * Mutual exclusion on buffers. Locking model:
891 * Buffers associated with inodes for which buffer locking
892 * is not enabled are not protected by semaphores, and are
893 * assumed to be exclusively owned by the caller. There is a
894 * spinlock in the buffer, used by the caller when concurrent
895 * access is possible.
899 * Locks a buffer object, if it is not already locked.
900 * Note that this in no way locks the underlying pages, so it is only
901 * useful for synchronizing concurrent use of buffer objects, not for
902 * synchronizing independent access to the underlying pages.
905 xfs_buf_cond_lock(
906 xfs_buf_t *bp)
908 int locked;
910 locked = down_trylock(&bp->b_sema) == 0;
911 if (locked) {
912 XB_SET_OWNER(bp);
914 XB_TRACE(bp, "cond_lock", (long)locked);
915 return locked ? 0 : -EBUSY;
918 #if defined(DEBUG) || defined(XFS_BLI_TRACE)
920 xfs_buf_lock_value(
921 xfs_buf_t *bp)
923 return bp->b_sema.count;
925 #endif
928 * Locks a buffer object.
929 * Note that this in no way locks the underlying pages, so it is only
930 * useful for synchronizing concurrent use of buffer objects, not for
931 * synchronizing independent access to the underlying pages.
933 void
934 xfs_buf_lock(
935 xfs_buf_t *bp)
937 XB_TRACE(bp, "lock", 0);
938 if (atomic_read(&bp->b_io_remaining))
939 blk_run_address_space(bp->b_target->bt_mapping);
940 down(&bp->b_sema);
941 XB_SET_OWNER(bp);
942 XB_TRACE(bp, "locked", 0);
946 * Releases the lock on the buffer object.
947 * If the buffer is marked delwri but is not queued, do so before we
948 * unlock the buffer as we need to set flags correctly. We also need to
949 * take a reference for the delwri queue because the unlocker is going to
950 * drop their's and they don't know we just queued it.
952 void
953 xfs_buf_unlock(
954 xfs_buf_t *bp)
956 if ((bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)) == XBF_DELWRI) {
957 atomic_inc(&bp->b_hold);
958 bp->b_flags |= XBF_ASYNC;
959 xfs_buf_delwri_queue(bp, 0);
962 XB_CLEAR_OWNER(bp);
963 up(&bp->b_sema);
964 XB_TRACE(bp, "unlock", 0);
969 * Pinning Buffer Storage in Memory
970 * Ensure that no attempt to force a buffer to disk will succeed.
972 void
973 xfs_buf_pin(
974 xfs_buf_t *bp)
976 atomic_inc(&bp->b_pin_count);
977 XB_TRACE(bp, "pin", (long)bp->b_pin_count.counter);
980 void
981 xfs_buf_unpin(
982 xfs_buf_t *bp)
984 if (atomic_dec_and_test(&bp->b_pin_count))
985 wake_up_all(&bp->b_waiters);
986 XB_TRACE(bp, "unpin", (long)bp->b_pin_count.counter);
990 xfs_buf_ispin(
991 xfs_buf_t *bp)
993 return atomic_read(&bp->b_pin_count);
996 STATIC void
997 xfs_buf_wait_unpin(
998 xfs_buf_t *bp)
1000 DECLARE_WAITQUEUE (wait, current);
1002 if (atomic_read(&bp->b_pin_count) == 0)
1003 return;
1005 add_wait_queue(&bp->b_waiters, &wait);
1006 for (;;) {
1007 set_current_state(TASK_UNINTERRUPTIBLE);
1008 if (atomic_read(&bp->b_pin_count) == 0)
1009 break;
1010 if (atomic_read(&bp->b_io_remaining))
1011 blk_run_address_space(bp->b_target->bt_mapping);
1012 schedule();
1014 remove_wait_queue(&bp->b_waiters, &wait);
1015 set_current_state(TASK_RUNNING);
1019 * Buffer Utility Routines
1022 STATIC void
1023 xfs_buf_iodone_work(
1024 struct work_struct *work)
1026 xfs_buf_t *bp =
1027 container_of(work, xfs_buf_t, b_iodone_work);
1030 * We can get an EOPNOTSUPP to ordered writes. Here we clear the
1031 * ordered flag and reissue them. Because we can't tell the higher
1032 * layers directly that they should not issue ordered I/O anymore, they
1033 * need to check if the _XFS_BARRIER_FAILED flag was set during I/O completion.
1035 if ((bp->b_error == EOPNOTSUPP) &&
1036 (bp->b_flags & (XBF_ORDERED|XBF_ASYNC)) == (XBF_ORDERED|XBF_ASYNC)) {
1037 XB_TRACE(bp, "ordered_retry", bp->b_iodone);
1038 bp->b_flags &= ~XBF_ORDERED;
1039 bp->b_flags |= _XFS_BARRIER_FAILED;
1040 xfs_buf_iorequest(bp);
1041 } else if (bp->b_iodone)
1042 (*(bp->b_iodone))(bp);
1043 else if (bp->b_flags & XBF_ASYNC)
1044 xfs_buf_relse(bp);
1047 void
1048 xfs_buf_ioend(
1049 xfs_buf_t *bp,
1050 int schedule)
1052 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
1053 if (bp->b_error == 0)
1054 bp->b_flags |= XBF_DONE;
1056 XB_TRACE(bp, "iodone", bp->b_iodone);
1058 if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) {
1059 if (schedule) {
1060 INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
1061 queue_work(xfslogd_workqueue, &bp->b_iodone_work);
1062 } else {
1063 xfs_buf_iodone_work(&bp->b_iodone_work);
1065 } else {
1066 complete(&bp->b_iowait);
1070 void
1071 xfs_buf_ioerror(
1072 xfs_buf_t *bp,
1073 int error)
1075 ASSERT(error >= 0 && error <= 0xffff);
1076 bp->b_error = (unsigned short)error;
1077 XB_TRACE(bp, "ioerror", (unsigned long)error);
1081 xfs_bawrite(
1082 void *mp,
1083 struct xfs_buf *bp)
1085 XB_TRACE(bp, "bawrite", 0);
1087 ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL);
1089 xfs_buf_delwri_dequeue(bp);
1091 bp->b_flags &= ~(XBF_READ | XBF_DELWRI | XBF_READ_AHEAD);
1092 bp->b_flags |= (XBF_WRITE | XBF_ASYNC | _XBF_RUN_QUEUES);
1094 bp->b_mount = mp;
1095 bp->b_strat = xfs_bdstrat_cb;
1096 return xfs_bdstrat_cb(bp);
1099 void
1100 xfs_bdwrite(
1101 void *mp,
1102 struct xfs_buf *bp)
1104 XB_TRACE(bp, "bdwrite", 0);
1106 bp->b_strat = xfs_bdstrat_cb;
1107 bp->b_mount = mp;
1109 bp->b_flags &= ~XBF_READ;
1110 bp->b_flags |= (XBF_DELWRI | XBF_ASYNC);
1112 xfs_buf_delwri_queue(bp, 1);
1115 STATIC_INLINE void
1116 _xfs_buf_ioend(
1117 xfs_buf_t *bp,
1118 int schedule)
1120 if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
1121 bp->b_flags &= ~_XBF_PAGE_LOCKED;
1122 xfs_buf_ioend(bp, schedule);
1126 STATIC void
1127 xfs_buf_bio_end_io(
1128 struct bio *bio,
1129 int error)
1131 xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
1132 unsigned int blocksize = bp->b_target->bt_bsize;
1133 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1135 xfs_buf_ioerror(bp, -error);
1137 do {
1138 struct page *page = bvec->bv_page;
1140 ASSERT(!PagePrivate(page));
1141 if (unlikely(bp->b_error)) {
1142 if (bp->b_flags & XBF_READ)
1143 ClearPageUptodate(page);
1144 } else if (blocksize >= PAGE_CACHE_SIZE) {
1145 SetPageUptodate(page);
1146 } else if (!PagePrivate(page) &&
1147 (bp->b_flags & _XBF_PAGE_CACHE)) {
1148 set_page_region(page, bvec->bv_offset, bvec->bv_len);
1151 if (--bvec >= bio->bi_io_vec)
1152 prefetchw(&bvec->bv_page->flags);
1154 if (bp->b_flags & _XBF_PAGE_LOCKED)
1155 unlock_page(page);
1156 } while (bvec >= bio->bi_io_vec);
1158 _xfs_buf_ioend(bp, 1);
1159 bio_put(bio);
1162 STATIC void
1163 _xfs_buf_ioapply(
1164 xfs_buf_t *bp)
1166 int rw, map_i, total_nr_pages, nr_pages;
1167 struct bio *bio;
1168 int offset = bp->b_offset;
1169 int size = bp->b_count_desired;
1170 sector_t sector = bp->b_bn;
1171 unsigned int blocksize = bp->b_target->bt_bsize;
1173 total_nr_pages = bp->b_page_count;
1174 map_i = 0;
1176 if (bp->b_flags & XBF_ORDERED) {
1177 ASSERT(!(bp->b_flags & XBF_READ));
1178 rw = WRITE_BARRIER;
1179 } else if (bp->b_flags & _XBF_RUN_QUEUES) {
1180 ASSERT(!(bp->b_flags & XBF_READ_AHEAD));
1181 bp->b_flags &= ~_XBF_RUN_QUEUES;
1182 rw = (bp->b_flags & XBF_WRITE) ? WRITE_SYNC : READ_SYNC;
1183 } else {
1184 rw = (bp->b_flags & XBF_WRITE) ? WRITE :
1185 (bp->b_flags & XBF_READ_AHEAD) ? READA : READ;
1188 /* Special code path for reading a sub page size buffer in --
1189 * we populate up the whole page, and hence the other metadata
1190 * in the same page. This optimization is only valid when the
1191 * filesystem block size is not smaller than the page size.
1193 if ((bp->b_buffer_length < PAGE_CACHE_SIZE) &&
1194 ((bp->b_flags & (XBF_READ|_XBF_PAGE_LOCKED)) ==
1195 (XBF_READ|_XBF_PAGE_LOCKED)) &&
1196 (blocksize >= PAGE_CACHE_SIZE)) {
1197 bio = bio_alloc(GFP_NOIO, 1);
1199 bio->bi_bdev = bp->b_target->bt_bdev;
1200 bio->bi_sector = sector - (offset >> BBSHIFT);
1201 bio->bi_end_io = xfs_buf_bio_end_io;
1202 bio->bi_private = bp;
1204 bio_add_page(bio, bp->b_pages[0], PAGE_CACHE_SIZE, 0);
1205 size = 0;
1207 atomic_inc(&bp->b_io_remaining);
1209 goto submit_io;
1212 next_chunk:
1213 atomic_inc(&bp->b_io_remaining);
1214 nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
1215 if (nr_pages > total_nr_pages)
1216 nr_pages = total_nr_pages;
1218 bio = bio_alloc(GFP_NOIO, nr_pages);
1219 bio->bi_bdev = bp->b_target->bt_bdev;
1220 bio->bi_sector = sector;
1221 bio->bi_end_io = xfs_buf_bio_end_io;
1222 bio->bi_private = bp;
1224 for (; size && nr_pages; nr_pages--, map_i++) {
1225 int rbytes, nbytes = PAGE_CACHE_SIZE - offset;
1227 if (nbytes > size)
1228 nbytes = size;
1230 rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset);
1231 if (rbytes < nbytes)
1232 break;
1234 offset = 0;
1235 sector += nbytes >> BBSHIFT;
1236 size -= nbytes;
1237 total_nr_pages--;
1240 submit_io:
1241 if (likely(bio->bi_size)) {
1242 submit_bio(rw, bio);
1243 if (size)
1244 goto next_chunk;
1245 } else {
1246 bio_put(bio);
1247 xfs_buf_ioerror(bp, EIO);
1252 xfs_buf_iorequest(
1253 xfs_buf_t *bp)
1255 XB_TRACE(bp, "iorequest", 0);
1257 if (bp->b_flags & XBF_DELWRI) {
1258 xfs_buf_delwri_queue(bp, 1);
1259 return 0;
1262 if (bp->b_flags & XBF_WRITE) {
1263 xfs_buf_wait_unpin(bp);
1266 xfs_buf_hold(bp);
1268 /* Set the count to 1 initially, this will stop an I/O
1269 * completion callout which happens before we have started
1270 * all the I/O from calling xfs_buf_ioend too early.
1272 atomic_set(&bp->b_io_remaining, 1);
1273 _xfs_buf_ioapply(bp);
1274 _xfs_buf_ioend(bp, 0);
1276 xfs_buf_rele(bp);
1277 return 0;
1281 * Waits for I/O to complete on the buffer supplied.
1282 * It returns immediately if no I/O is pending.
1283 * It returns the I/O error code, if any, or 0 if there was no error.
1286 xfs_buf_iowait(
1287 xfs_buf_t *bp)
1289 XB_TRACE(bp, "iowait", 0);
1290 if (atomic_read(&bp->b_io_remaining))
1291 blk_run_address_space(bp->b_target->bt_mapping);
1292 wait_for_completion(&bp->b_iowait);
1293 XB_TRACE(bp, "iowaited", (long)bp->b_error);
1294 return bp->b_error;
1297 xfs_caddr_t
1298 xfs_buf_offset(
1299 xfs_buf_t *bp,
1300 size_t offset)
1302 struct page *page;
1304 if (bp->b_flags & XBF_MAPPED)
1305 return XFS_BUF_PTR(bp) + offset;
1307 offset += bp->b_offset;
1308 page = bp->b_pages[offset >> PAGE_CACHE_SHIFT];
1309 return (xfs_caddr_t)page_address(page) + (offset & (PAGE_CACHE_SIZE-1));
1313 * Move data into or out of a buffer.
1315 void
1316 xfs_buf_iomove(
1317 xfs_buf_t *bp, /* buffer to process */
1318 size_t boff, /* starting buffer offset */
1319 size_t bsize, /* length to copy */
1320 caddr_t data, /* data address */
1321 xfs_buf_rw_t mode) /* read/write/zero flag */
1323 size_t bend, cpoff, csize;
1324 struct page *page;
1326 bend = boff + bsize;
1327 while (boff < bend) {
1328 page = bp->b_pages[xfs_buf_btoct(boff + bp->b_offset)];
1329 cpoff = xfs_buf_poff(boff + bp->b_offset);
1330 csize = min_t(size_t,
1331 PAGE_CACHE_SIZE-cpoff, bp->b_count_desired-boff);
1333 ASSERT(((csize + cpoff) <= PAGE_CACHE_SIZE));
1335 switch (mode) {
1336 case XBRW_ZERO:
1337 memset(page_address(page) + cpoff, 0, csize);
1338 break;
1339 case XBRW_READ:
1340 memcpy(data, page_address(page) + cpoff, csize);
1341 break;
1342 case XBRW_WRITE:
1343 memcpy(page_address(page) + cpoff, data, csize);
1346 boff += csize;
1347 data += csize;
1352 * Handling of buffer targets (buftargs).
1356 * Wait for any bufs with callbacks that have been submitted but
1357 * have not yet returned... walk the hash list for the target.
1359 void
1360 xfs_wait_buftarg(
1361 xfs_buftarg_t *btp)
1363 xfs_buf_t *bp, *n;
1364 xfs_bufhash_t *hash;
1365 uint i;
1367 for (i = 0; i < (1 << btp->bt_hashshift); i++) {
1368 hash = &btp->bt_hash[i];
1369 again:
1370 spin_lock(&hash->bh_lock);
1371 list_for_each_entry_safe(bp, n, &hash->bh_list, b_hash_list) {
1372 ASSERT(btp == bp->b_target);
1373 if (!(bp->b_flags & XBF_FS_MANAGED)) {
1374 spin_unlock(&hash->bh_lock);
1376 * Catch superblock reference count leaks
1377 * immediately
1379 BUG_ON(bp->b_bn == 0);
1380 delay(100);
1381 goto again;
1384 spin_unlock(&hash->bh_lock);
1389 * Allocate buffer hash table for a given target.
1390 * For devices containing metadata (i.e. not the log/realtime devices)
1391 * we need to allocate a much larger hash table.
1393 STATIC void
1394 xfs_alloc_bufhash(
1395 xfs_buftarg_t *btp,
1396 int external)
1398 unsigned int i;
1400 btp->bt_hashshift = external ? 3 : 8; /* 8 or 256 buckets */
1401 btp->bt_hashmask = (1 << btp->bt_hashshift) - 1;
1402 btp->bt_hash = kmem_zalloc((1 << btp->bt_hashshift) *
1403 sizeof(xfs_bufhash_t), KM_SLEEP | KM_LARGE);
1404 for (i = 0; i < (1 << btp->bt_hashshift); i++) {
1405 spin_lock_init(&btp->bt_hash[i].bh_lock);
1406 INIT_LIST_HEAD(&btp->bt_hash[i].bh_list);
1410 STATIC void
1411 xfs_free_bufhash(
1412 xfs_buftarg_t *btp)
1414 kmem_free(btp->bt_hash);
1415 btp->bt_hash = NULL;
1419 * buftarg list for delwrite queue processing
1421 static LIST_HEAD(xfs_buftarg_list);
1422 static DEFINE_SPINLOCK(xfs_buftarg_lock);
1424 STATIC void
1425 xfs_register_buftarg(
1426 xfs_buftarg_t *btp)
1428 spin_lock(&xfs_buftarg_lock);
1429 list_add(&btp->bt_list, &xfs_buftarg_list);
1430 spin_unlock(&xfs_buftarg_lock);
1433 STATIC void
1434 xfs_unregister_buftarg(
1435 xfs_buftarg_t *btp)
1437 spin_lock(&xfs_buftarg_lock);
1438 list_del(&btp->bt_list);
1439 spin_unlock(&xfs_buftarg_lock);
1442 void
1443 xfs_free_buftarg(
1444 struct xfs_mount *mp,
1445 struct xfs_buftarg *btp)
1447 xfs_flush_buftarg(btp, 1);
1448 if (mp->m_flags & XFS_MOUNT_BARRIER)
1449 xfs_blkdev_issue_flush(btp);
1450 xfs_free_bufhash(btp);
1451 iput(btp->bt_mapping->host);
1453 /* Unregister the buftarg first so that we don't get a
1454 * wakeup finding a non-existent task
1456 xfs_unregister_buftarg(btp);
1457 kthread_stop(btp->bt_task);
1459 kmem_free(btp);
1462 STATIC int
1463 xfs_setsize_buftarg_flags(
1464 xfs_buftarg_t *btp,
1465 unsigned int blocksize,
1466 unsigned int sectorsize,
1467 int verbose)
1469 btp->bt_bsize = blocksize;
1470 btp->bt_sshift = ffs(sectorsize) - 1;
1471 btp->bt_smask = sectorsize - 1;
1473 if (set_blocksize(btp->bt_bdev, sectorsize)) {
1474 printk(KERN_WARNING
1475 "XFS: Cannot set_blocksize to %u on device %s\n",
1476 sectorsize, XFS_BUFTARG_NAME(btp));
1477 return EINVAL;
1480 if (verbose &&
1481 (PAGE_CACHE_SIZE / BITS_PER_LONG) > sectorsize) {
1482 printk(KERN_WARNING
1483 "XFS: %u byte sectors in use on device %s. "
1484 "This is suboptimal; %u or greater is ideal.\n",
1485 sectorsize, XFS_BUFTARG_NAME(btp),
1486 (unsigned int)PAGE_CACHE_SIZE / BITS_PER_LONG);
1489 return 0;
1493 * When allocating the initial buffer target we have not yet
1494 * read in the superblock, so don't know what sized sectors
1495 * are being used is at this early stage. Play safe.
1497 STATIC int
1498 xfs_setsize_buftarg_early(
1499 xfs_buftarg_t *btp,
1500 struct block_device *bdev)
1502 return xfs_setsize_buftarg_flags(btp,
1503 PAGE_CACHE_SIZE, bdev_hardsect_size(bdev), 0);
1507 xfs_setsize_buftarg(
1508 xfs_buftarg_t *btp,
1509 unsigned int blocksize,
1510 unsigned int sectorsize)
1512 return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
1515 STATIC int
1516 xfs_mapping_buftarg(
1517 xfs_buftarg_t *btp,
1518 struct block_device *bdev)
1520 struct backing_dev_info *bdi;
1521 struct inode *inode;
1522 struct address_space *mapping;
1523 static const struct address_space_operations mapping_aops = {
1524 .sync_page = block_sync_page,
1525 .migratepage = fail_migrate_page,
1528 inode = new_inode(bdev->bd_inode->i_sb);
1529 if (!inode) {
1530 printk(KERN_WARNING
1531 "XFS: Cannot allocate mapping inode for device %s\n",
1532 XFS_BUFTARG_NAME(btp));
1533 return ENOMEM;
1535 inode->i_mode = S_IFBLK;
1536 inode->i_bdev = bdev;
1537 inode->i_rdev = bdev->bd_dev;
1538 bdi = blk_get_backing_dev_info(bdev);
1539 if (!bdi)
1540 bdi = &default_backing_dev_info;
1541 mapping = &inode->i_data;
1542 mapping->a_ops = &mapping_aops;
1543 mapping->backing_dev_info = bdi;
1544 mapping_set_gfp_mask(mapping, GFP_NOFS);
1545 btp->bt_mapping = mapping;
1546 return 0;
1549 STATIC int
1550 xfs_alloc_delwrite_queue(
1551 xfs_buftarg_t *btp)
1553 int error = 0;
1555 INIT_LIST_HEAD(&btp->bt_list);
1556 INIT_LIST_HEAD(&btp->bt_delwrite_queue);
1557 spin_lock_init(&btp->bt_delwrite_lock);
1558 btp->bt_flags = 0;
1559 btp->bt_task = kthread_run(xfsbufd, btp, "xfsbufd");
1560 if (IS_ERR(btp->bt_task)) {
1561 error = PTR_ERR(btp->bt_task);
1562 goto out_error;
1564 xfs_register_buftarg(btp);
1565 out_error:
1566 return error;
1569 xfs_buftarg_t *
1570 xfs_alloc_buftarg(
1571 struct block_device *bdev,
1572 int external)
1574 xfs_buftarg_t *btp;
1576 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
1578 btp->bt_dev = bdev->bd_dev;
1579 btp->bt_bdev = bdev;
1580 if (xfs_setsize_buftarg_early(btp, bdev))
1581 goto error;
1582 if (xfs_mapping_buftarg(btp, bdev))
1583 goto error;
1584 if (xfs_alloc_delwrite_queue(btp))
1585 goto error;
1586 xfs_alloc_bufhash(btp, external);
1587 return btp;
1589 error:
1590 kmem_free(btp);
1591 return NULL;
1596 * Delayed write buffer handling
1598 STATIC void
1599 xfs_buf_delwri_queue(
1600 xfs_buf_t *bp,
1601 int unlock)
1603 struct list_head *dwq = &bp->b_target->bt_delwrite_queue;
1604 spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
1606 XB_TRACE(bp, "delwri_q", (long)unlock);
1607 ASSERT((bp->b_flags&(XBF_DELWRI|XBF_ASYNC)) == (XBF_DELWRI|XBF_ASYNC));
1609 spin_lock(dwlk);
1610 /* If already in the queue, dequeue and place at tail */
1611 if (!list_empty(&bp->b_list)) {
1612 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1613 if (unlock)
1614 atomic_dec(&bp->b_hold);
1615 list_del(&bp->b_list);
1618 bp->b_flags |= _XBF_DELWRI_Q;
1619 list_add_tail(&bp->b_list, dwq);
1620 bp->b_queuetime = jiffies;
1621 spin_unlock(dwlk);
1623 if (unlock)
1624 xfs_buf_unlock(bp);
1627 void
1628 xfs_buf_delwri_dequeue(
1629 xfs_buf_t *bp)
1631 spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
1632 int dequeued = 0;
1634 spin_lock(dwlk);
1635 if ((bp->b_flags & XBF_DELWRI) && !list_empty(&bp->b_list)) {
1636 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1637 list_del_init(&bp->b_list);
1638 dequeued = 1;
1640 bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q);
1641 spin_unlock(dwlk);
1643 if (dequeued)
1644 xfs_buf_rele(bp);
1646 XB_TRACE(bp, "delwri_dq", (long)dequeued);
1649 STATIC void
1650 xfs_buf_runall_queues(
1651 struct workqueue_struct *queue)
1653 flush_workqueue(queue);
1656 STATIC int
1657 xfsbufd_wakeup(
1658 int priority,
1659 gfp_t mask)
1661 xfs_buftarg_t *btp;
1663 spin_lock(&xfs_buftarg_lock);
1664 list_for_each_entry(btp, &xfs_buftarg_list, bt_list) {
1665 if (test_bit(XBT_FORCE_SLEEP, &btp->bt_flags))
1666 continue;
1667 set_bit(XBT_FORCE_FLUSH, &btp->bt_flags);
1668 wake_up_process(btp->bt_task);
1670 spin_unlock(&xfs_buftarg_lock);
1671 return 0;
1675 * Move as many buffers as specified to the supplied list
1676 * idicating if we skipped any buffers to prevent deadlocks.
1678 STATIC int
1679 xfs_buf_delwri_split(
1680 xfs_buftarg_t *target,
1681 struct list_head *list,
1682 unsigned long age)
1684 xfs_buf_t *bp, *n;
1685 struct list_head *dwq = &target->bt_delwrite_queue;
1686 spinlock_t *dwlk = &target->bt_delwrite_lock;
1687 int skipped = 0;
1688 int force;
1690 force = test_and_clear_bit(XBT_FORCE_FLUSH, &target->bt_flags);
1691 INIT_LIST_HEAD(list);
1692 spin_lock(dwlk);
1693 list_for_each_entry_safe(bp, n, dwq, b_list) {
1694 XB_TRACE(bp, "walkq1", (long)xfs_buf_ispin(bp));
1695 ASSERT(bp->b_flags & XBF_DELWRI);
1697 if (!xfs_buf_ispin(bp) && !xfs_buf_cond_lock(bp)) {
1698 if (!force &&
1699 time_before(jiffies, bp->b_queuetime + age)) {
1700 xfs_buf_unlock(bp);
1701 break;
1704 bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q|
1705 _XBF_RUN_QUEUES);
1706 bp->b_flags |= XBF_WRITE;
1707 list_move_tail(&bp->b_list, list);
1708 } else
1709 skipped++;
1711 spin_unlock(dwlk);
1713 return skipped;
1717 STATIC int
1718 xfsbufd(
1719 void *data)
1721 struct list_head tmp;
1722 xfs_buftarg_t *target = (xfs_buftarg_t *)data;
1723 int count;
1724 xfs_buf_t *bp;
1726 current->flags |= PF_MEMALLOC;
1728 set_freezable();
1730 do {
1731 if (unlikely(freezing(current))) {
1732 set_bit(XBT_FORCE_SLEEP, &target->bt_flags);
1733 refrigerator();
1734 } else {
1735 clear_bit(XBT_FORCE_SLEEP, &target->bt_flags);
1738 schedule_timeout_interruptible(
1739 xfs_buf_timer_centisecs * msecs_to_jiffies(10));
1741 xfs_buf_delwri_split(target, &tmp,
1742 xfs_buf_age_centisecs * msecs_to_jiffies(10));
1744 count = 0;
1745 while (!list_empty(&tmp)) {
1746 bp = list_entry(tmp.next, xfs_buf_t, b_list);
1747 ASSERT(target == bp->b_target);
1749 list_del_init(&bp->b_list);
1750 xfs_buf_iostrategy(bp);
1751 count++;
1754 if (as_list_len > 0)
1755 purge_addresses();
1756 if (count)
1757 blk_run_address_space(target->bt_mapping);
1759 } while (!kthread_should_stop());
1761 return 0;
1765 * Go through all incore buffers, and release buffers if they belong to
1766 * the given device. This is used in filesystem error handling to
1767 * preserve the consistency of its metadata.
1770 xfs_flush_buftarg(
1771 xfs_buftarg_t *target,
1772 int wait)
1774 struct list_head tmp;
1775 xfs_buf_t *bp, *n;
1776 int pincount = 0;
1778 xfs_buf_runall_queues(xfsdatad_workqueue);
1779 xfs_buf_runall_queues(xfslogd_workqueue);
1781 set_bit(XBT_FORCE_FLUSH, &target->bt_flags);
1782 pincount = xfs_buf_delwri_split(target, &tmp, 0);
1785 * Dropped the delayed write list lock, now walk the temporary list
1787 list_for_each_entry_safe(bp, n, &tmp, b_list) {
1788 ASSERT(target == bp->b_target);
1789 if (wait)
1790 bp->b_flags &= ~XBF_ASYNC;
1791 else
1792 list_del_init(&bp->b_list);
1794 xfs_buf_iostrategy(bp);
1797 if (wait)
1798 blk_run_address_space(target->bt_mapping);
1801 * Remaining list items must be flushed before returning
1803 while (!list_empty(&tmp)) {
1804 bp = list_entry(tmp.next, xfs_buf_t, b_list);
1806 list_del_init(&bp->b_list);
1807 xfs_iowait(bp);
1808 xfs_buf_relse(bp);
1811 return pincount;
1814 int __init
1815 xfs_buf_init(void)
1817 #ifdef XFS_BUF_TRACE
1818 xfs_buf_trace_buf = ktrace_alloc(XFS_BUF_TRACE_SIZE, KM_NOFS);
1819 #endif
1821 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
1822 KM_ZONE_HWALIGN, NULL);
1823 if (!xfs_buf_zone)
1824 goto out_free_trace_buf;
1826 xfslogd_workqueue = create_workqueue("xfslogd");
1827 if (!xfslogd_workqueue)
1828 goto out_free_buf_zone;
1830 xfsdatad_workqueue = create_workqueue("xfsdatad");
1831 if (!xfsdatad_workqueue)
1832 goto out_destroy_xfslogd_workqueue;
1834 register_shrinker(&xfs_buf_shake);
1835 return 0;
1837 out_destroy_xfslogd_workqueue:
1838 destroy_workqueue(xfslogd_workqueue);
1839 out_free_buf_zone:
1840 kmem_zone_destroy(xfs_buf_zone);
1841 out_free_trace_buf:
1842 #ifdef XFS_BUF_TRACE
1843 ktrace_free(xfs_buf_trace_buf);
1844 #endif
1845 return -ENOMEM;
1848 void
1849 xfs_buf_terminate(void)
1851 unregister_shrinker(&xfs_buf_shake);
1852 destroy_workqueue(xfsdatad_workqueue);
1853 destroy_workqueue(xfslogd_workqueue);
1854 kmem_zone_destroy(xfs_buf_zone);
1855 #ifdef XFS_BUF_TRACE
1856 ktrace_free(xfs_buf_trace_buf);
1857 #endif
1860 #ifdef CONFIG_KDB_MODULES
1861 struct list_head *
1862 xfs_get_buftarg_list(void)
1864 return &xfs_buftarg_list;
1866 #endif