Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Nathan Scott | f07c225 | 2006-09-28 10:52:15 +1000 | [diff] [blame] | 2 | * Copyright (c) 2000-2006 Silicon Graphics, Inc. |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Vlad Apostolov | 93c189c | 2006-11-11 18:03:49 +1100 | [diff] [blame] | 18 | #include "xfs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Christoph Hellwig | 4df08c5 | 2005-09-05 08:34:18 +1000 | [diff] [blame] | 32 | #include <linux/kthread.h> |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 33 | #include <linux/migrate.h> |
Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 34 | #include <linux/backing-dev.h> |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 35 | #include <linux/freezer.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
David Chinner | 7989cb8 | 2007-02-10 18:34:56 +1100 | [diff] [blame] | 37 | static kmem_zone_t *xfs_buf_zone; |
| 38 | static kmem_shaker_t xfs_buf_shake; |
David Chinner | a6867a6 | 2006-01-11 15:37:58 +1100 | [diff] [blame] | 39 | STATIC int xfsbufd(void *); |
Al Viro | 27496a8 | 2005-10-21 03:20:48 -0400 | [diff] [blame] | 40 | STATIC int xfsbufd_wakeup(int, gfp_t); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 41 | STATIC void xfs_buf_delwri_queue(xfs_buf_t *, int); |
Christoph Hellwig | 23ea403 | 2005-06-21 15:14:01 +1000 | [diff] [blame] | 42 | |
David Chinner | 7989cb8 | 2007-02-10 18:34:56 +1100 | [diff] [blame] | 43 | static struct workqueue_struct *xfslogd_workqueue; |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 44 | struct workqueue_struct *xfsdatad_workqueue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 46 | #ifdef XFS_BUF_TRACE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 48 | xfs_buf_trace( |
| 49 | xfs_buf_t *bp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | char *id, |
| 51 | void *data, |
| 52 | void *ra) |
| 53 | { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 54 | ktrace_enter(xfs_buf_trace_buf, |
| 55 | bp, id, |
| 56 | (void *)(unsigned long)bp->b_flags, |
| 57 | (void *)(unsigned long)bp->b_hold.counter, |
| 58 | (void *)(unsigned long)bp->b_sema.count.counter, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | (void *)current, |
| 60 | data, ra, |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 61 | (void *)(unsigned long)((bp->b_file_offset>>32) & 0xffffffff), |
| 62 | (void *)(unsigned long)(bp->b_file_offset & 0xffffffff), |
| 63 | (void *)(unsigned long)bp->b_buffer_length, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | NULL, NULL, NULL, NULL, NULL); |
| 65 | } |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 66 | ktrace_t *xfs_buf_trace_buf; |
| 67 | #define XFS_BUF_TRACE_SIZE 4096 |
| 68 | #define XB_TRACE(bp, id, data) \ |
| 69 | xfs_buf_trace(bp, id, (void *)data, (void *)__builtin_return_address(0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | #else |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 71 | #define XB_TRACE(bp, id, data) do { } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | #endif |
| 73 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 74 | #ifdef XFS_BUF_LOCK_TRACKING |
| 75 | # define XB_SET_OWNER(bp) ((bp)->b_last_holder = current->pid) |
| 76 | # define XB_CLEAR_OWNER(bp) ((bp)->b_last_holder = -1) |
| 77 | # define XB_GET_OWNER(bp) ((bp)->b_last_holder) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | #else |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 79 | # define XB_SET_OWNER(bp) do { } while (0) |
| 80 | # define XB_CLEAR_OWNER(bp) do { } while (0) |
| 81 | # define XB_GET_OWNER(bp) do { } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | #endif |
| 83 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 84 | #define xb_to_gfp(flags) \ |
| 85 | ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : \ |
| 86 | ((flags) & XBF_DONT_BLOCK) ? GFP_NOFS : GFP_KERNEL) | __GFP_NOWARN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 88 | #define xb_to_km(flags) \ |
| 89 | (((flags) & XBF_DONT_BLOCK) ? KM_NOFS : KM_SLEEP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 91 | #define xfs_buf_allocate(flags) \ |
| 92 | kmem_zone_alloc(xfs_buf_zone, xb_to_km(flags)) |
| 93 | #define xfs_buf_deallocate(bp) \ |
| 94 | kmem_zone_free(xfs_buf_zone, (bp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
| 96 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 97 | * Page Region interfaces. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | * |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 99 | * For pages in filesystems where the blocksize is smaller than the |
| 100 | * pagesize, we use the page->private field (long) to hold a bitmap |
| 101 | * of uptodate regions within the page. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | * |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 103 | * Each such region is "bytes per page / bits per long" bytes long. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | * |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 105 | * NBPPR == number-of-bytes-per-page-region |
| 106 | * BTOPR == bytes-to-page-region (rounded up) |
| 107 | * BTOPRT == bytes-to-page-region-truncated (rounded down) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | */ |
| 109 | #if (BITS_PER_LONG == 32) |
| 110 | #define PRSHIFT (PAGE_CACHE_SHIFT - 5) /* (32 == 1<<5) */ |
| 111 | #elif (BITS_PER_LONG == 64) |
| 112 | #define PRSHIFT (PAGE_CACHE_SHIFT - 6) /* (64 == 1<<6) */ |
| 113 | #else |
| 114 | #error BITS_PER_LONG must be 32 or 64 |
| 115 | #endif |
| 116 | #define NBPPR (PAGE_CACHE_SIZE/BITS_PER_LONG) |
| 117 | #define BTOPR(b) (((unsigned int)(b) + (NBPPR - 1)) >> PRSHIFT) |
| 118 | #define BTOPRT(b) (((unsigned int)(b) >> PRSHIFT)) |
| 119 | |
| 120 | STATIC unsigned long |
| 121 | page_region_mask( |
| 122 | size_t offset, |
| 123 | size_t length) |
| 124 | { |
| 125 | unsigned long mask; |
| 126 | int first, final; |
| 127 | |
| 128 | first = BTOPR(offset); |
| 129 | final = BTOPRT(offset + length - 1); |
| 130 | first = min(first, final); |
| 131 | |
| 132 | mask = ~0UL; |
| 133 | mask <<= BITS_PER_LONG - (final - first); |
| 134 | mask >>= BITS_PER_LONG - (final); |
| 135 | |
| 136 | ASSERT(offset + length <= PAGE_CACHE_SIZE); |
| 137 | ASSERT((final - first) < BITS_PER_LONG && (final - first) >= 0); |
| 138 | |
| 139 | return mask; |
| 140 | } |
| 141 | |
David Chinner | 7989cb8 | 2007-02-10 18:34:56 +1100 | [diff] [blame] | 142 | STATIC_INLINE void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | set_page_region( |
| 144 | struct page *page, |
| 145 | size_t offset, |
| 146 | size_t length) |
| 147 | { |
Hugh Dickins | 4c21e2f | 2005-10-29 18:16:40 -0700 | [diff] [blame] | 148 | set_page_private(page, |
| 149 | page_private(page) | page_region_mask(offset, length)); |
| 150 | if (page_private(page) == ~0UL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | SetPageUptodate(page); |
| 152 | } |
| 153 | |
David Chinner | 7989cb8 | 2007-02-10 18:34:56 +1100 | [diff] [blame] | 154 | STATIC_INLINE int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | test_page_region( |
| 156 | struct page *page, |
| 157 | size_t offset, |
| 158 | size_t length) |
| 159 | { |
| 160 | unsigned long mask = page_region_mask(offset, length); |
| 161 | |
Hugh Dickins | 4c21e2f | 2005-10-29 18:16:40 -0700 | [diff] [blame] | 162 | return (mask && (page_private(page) & mask) == mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 166 | * Mapping of multi-page buffers into contiguous virtual space |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | */ |
| 168 | |
| 169 | typedef struct a_list { |
| 170 | void *vm_addr; |
| 171 | struct a_list *next; |
| 172 | } a_list_t; |
| 173 | |
David Chinner | 7989cb8 | 2007-02-10 18:34:56 +1100 | [diff] [blame] | 174 | static a_list_t *as_free_head; |
| 175 | static int as_list_len; |
| 176 | static DEFINE_SPINLOCK(as_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
| 178 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 179 | * Try to batch vunmaps because they are costly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | */ |
| 181 | STATIC void |
| 182 | free_address( |
| 183 | void *addr) |
| 184 | { |
| 185 | a_list_t *aentry; |
| 186 | |
Jeff Dike | 7b04d71 | 2006-04-10 22:53:27 -0700 | [diff] [blame] | 187 | aentry = kmalloc(sizeof(a_list_t), GFP_NOWAIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | if (likely(aentry)) { |
| 189 | spin_lock(&as_lock); |
| 190 | aentry->next = as_free_head; |
| 191 | aentry->vm_addr = addr; |
| 192 | as_free_head = aentry; |
| 193 | as_list_len++; |
| 194 | spin_unlock(&as_lock); |
| 195 | } else { |
| 196 | vunmap(addr); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | STATIC void |
| 201 | purge_addresses(void) |
| 202 | { |
| 203 | a_list_t *aentry, *old; |
| 204 | |
| 205 | if (as_free_head == NULL) |
| 206 | return; |
| 207 | |
| 208 | spin_lock(&as_lock); |
| 209 | aentry = as_free_head; |
| 210 | as_free_head = NULL; |
| 211 | as_list_len = 0; |
| 212 | spin_unlock(&as_lock); |
| 213 | |
| 214 | while ((old = aentry) != NULL) { |
| 215 | vunmap(aentry->vm_addr); |
| 216 | aentry = aentry->next; |
| 217 | kfree(old); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 222 | * Internal xfs_buf_t object manipulation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | */ |
| 224 | |
| 225 | STATIC void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 226 | _xfs_buf_initialize( |
| 227 | xfs_buf_t *bp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | xfs_buftarg_t *target, |
Nathan Scott | 204ab25 | 2006-01-11 20:50:22 +1100 | [diff] [blame] | 229 | xfs_off_t range_base, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | size_t range_length, |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 231 | xfs_buf_flags_t flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | { |
| 233 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 234 | * We don't want certain flags to appear in b_flags. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 236 | flags &= ~(XBF_LOCK|XBF_MAPPED|XBF_DONT_BLOCK|XBF_READ_AHEAD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 238 | memset(bp, 0, sizeof(xfs_buf_t)); |
| 239 | atomic_set(&bp->b_hold, 1); |
| 240 | init_MUTEX_LOCKED(&bp->b_iodonesema); |
| 241 | INIT_LIST_HEAD(&bp->b_list); |
| 242 | INIT_LIST_HEAD(&bp->b_hash_list); |
| 243 | init_MUTEX_LOCKED(&bp->b_sema); /* held, no waiters */ |
| 244 | XB_SET_OWNER(bp); |
| 245 | bp->b_target = target; |
| 246 | bp->b_file_offset = range_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | /* |
| 248 | * Set buffer_length and count_desired to the same value initially. |
| 249 | * I/O routines should use count_desired, which will be the same in |
| 250 | * most cases but may be reset (e.g. XFS recovery). |
| 251 | */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 252 | bp->b_buffer_length = bp->b_count_desired = range_length; |
| 253 | bp->b_flags = flags; |
| 254 | bp->b_bn = XFS_BUF_DADDR_NULL; |
| 255 | atomic_set(&bp->b_pin_count, 0); |
| 256 | init_waitqueue_head(&bp->b_waiters); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 258 | XFS_STATS_INC(xb_create); |
| 259 | XB_TRACE(bp, "initialize", target); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 263 | * Allocate a page array capable of holding a specified number |
| 264 | * of pages, and point the page buf at it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | */ |
| 266 | STATIC int |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 267 | _xfs_buf_get_pages( |
| 268 | xfs_buf_t *bp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | int page_count, |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 270 | xfs_buf_flags_t flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | { |
| 272 | /* Make sure that we have a page list */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 273 | if (bp->b_pages == NULL) { |
| 274 | bp->b_offset = xfs_buf_poff(bp->b_file_offset); |
| 275 | bp->b_page_count = page_count; |
| 276 | if (page_count <= XB_PAGES) { |
| 277 | bp->b_pages = bp->b_page_array; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | } else { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 279 | bp->b_pages = kmem_alloc(sizeof(struct page *) * |
| 280 | page_count, xb_to_km(flags)); |
| 281 | if (bp->b_pages == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | return -ENOMEM; |
| 283 | } |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 284 | memset(bp->b_pages, 0, sizeof(struct page *) * page_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | } |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 290 | * Frees b_pages if it was allocated. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | */ |
| 292 | STATIC void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 293 | _xfs_buf_free_pages( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | xfs_buf_t *bp) |
| 295 | { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 296 | if (bp->b_pages != bp->b_page_array) { |
| 297 | kmem_free(bp->b_pages, |
| 298 | bp->b_page_count * sizeof(struct page *)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
| 302 | /* |
| 303 | * Releases the specified buffer. |
| 304 | * |
| 305 | * The modification state of any associated pages is left unchanged. |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 306 | * The buffer most not be on any hash - use xfs_buf_rele instead for |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | * hashed and refcounted buffers |
| 308 | */ |
| 309 | void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 310 | xfs_buf_free( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | xfs_buf_t *bp) |
| 312 | { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 313 | XB_TRACE(bp, "free", 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 315 | ASSERT(list_empty(&bp->b_hash_list)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 317 | if (bp->b_flags & _XBF_PAGE_CACHE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | uint i; |
| 319 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 320 | if ((bp->b_flags & XBF_MAPPED) && (bp->b_page_count > 1)) |
| 321 | free_address(bp->b_addr - bp->b_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | |
Nathan Scott | 948ecdb | 2006-09-28 11:03:13 +1000 | [diff] [blame] | 323 | for (i = 0; i < bp->b_page_count; i++) { |
| 324 | struct page *page = bp->b_pages[i]; |
| 325 | |
| 326 | ASSERT(!PagePrivate(page)); |
| 327 | page_cache_release(page); |
| 328 | } |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 329 | _xfs_buf_free_pages(bp); |
| 330 | } else if (bp->b_flags & _XBF_KMEM_ALLOC) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 332 | * XXX(hch): bp->b_count_desired might be incorrect (see |
| 333 | * xfs_buf_associate_memory for details), but fortunately |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | * the Linux version of kmem_free ignores the len argument.. |
| 335 | */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 336 | kmem_free(bp->b_addr, bp->b_count_desired); |
| 337 | _xfs_buf_free_pages(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 340 | xfs_buf_deallocate(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /* |
| 344 | * Finds all pages for buffer in question and builds it's page list. |
| 345 | */ |
| 346 | STATIC int |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 347 | _xfs_buf_lookup_pages( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | xfs_buf_t *bp, |
| 349 | uint flags) |
| 350 | { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 351 | struct address_space *mapping = bp->b_target->bt_mapping; |
| 352 | size_t blocksize = bp->b_target->bt_bsize; |
| 353 | size_t size = bp->b_count_desired; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | size_t nbytes, offset; |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 355 | gfp_t gfp_mask = xb_to_gfp(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | unsigned short page_count, i; |
| 357 | pgoff_t first; |
Nathan Scott | 204ab25 | 2006-01-11 20:50:22 +1100 | [diff] [blame] | 358 | xfs_off_t end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | int error; |
| 360 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 361 | end = bp->b_file_offset + bp->b_buffer_length; |
| 362 | page_count = xfs_buf_btoc(end) - xfs_buf_btoct(bp->b_file_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 364 | error = _xfs_buf_get_pages(bp, page_count, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | if (unlikely(error)) |
| 366 | return error; |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 367 | bp->b_flags |= _XBF_PAGE_CACHE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 369 | offset = bp->b_offset; |
| 370 | first = bp->b_file_offset >> PAGE_CACHE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 372 | for (i = 0; i < bp->b_page_count; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | struct page *page; |
| 374 | uint retries = 0; |
| 375 | |
| 376 | retry: |
| 377 | page = find_or_create_page(mapping, first + i, gfp_mask); |
| 378 | if (unlikely(page == NULL)) { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 379 | if (flags & XBF_READ_AHEAD) { |
| 380 | bp->b_page_count = i; |
| 381 | for (i = 0; i < bp->b_page_count; i++) |
| 382 | unlock_page(bp->b_pages[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | return -ENOMEM; |
| 384 | } |
| 385 | |
| 386 | /* |
| 387 | * This could deadlock. |
| 388 | * |
| 389 | * But until all the XFS lowlevel code is revamped to |
| 390 | * handle buffer allocation failures we can't do much. |
| 391 | */ |
| 392 | if (!(++retries % 100)) |
| 393 | printk(KERN_ERR |
| 394 | "XFS: possible memory allocation " |
| 395 | "deadlock in %s (mode:0x%x)\n", |
| 396 | __FUNCTION__, gfp_mask); |
| 397 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 398 | XFS_STATS_INC(xb_page_retries); |
Christoph Hellwig | 23ea403 | 2005-06-21 15:14:01 +1000 | [diff] [blame] | 399 | xfsbufd_wakeup(0, gfp_mask); |
Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 400 | congestion_wait(WRITE, HZ/50); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | goto retry; |
| 402 | } |
| 403 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 404 | XFS_STATS_INC(xb_page_found); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | |
| 406 | nbytes = min_t(size_t, size, PAGE_CACHE_SIZE - offset); |
| 407 | size -= nbytes; |
| 408 | |
Nathan Scott | 948ecdb | 2006-09-28 11:03:13 +1000 | [diff] [blame] | 409 | ASSERT(!PagePrivate(page)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | if (!PageUptodate(page)) { |
| 411 | page_count--; |
| 412 | if (blocksize >= PAGE_CACHE_SIZE) { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 413 | if (flags & XBF_READ) |
| 414 | bp->b_locked = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | } else if (!PagePrivate(page)) { |
| 416 | if (test_page_region(page, offset, nbytes)) |
| 417 | page_count++; |
| 418 | } |
| 419 | } |
| 420 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 421 | bp->b_pages[i] = page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | offset = 0; |
| 423 | } |
| 424 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 425 | if (!bp->b_locked) { |
| 426 | for (i = 0; i < bp->b_page_count; i++) |
| 427 | unlock_page(bp->b_pages[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 430 | if (page_count == bp->b_page_count) |
| 431 | bp->b_flags |= XBF_DONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 433 | XB_TRACE(bp, "lookup_pages", (long)page_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | return error; |
| 435 | } |
| 436 | |
| 437 | /* |
| 438 | * Map buffer into kernel address-space if nessecary. |
| 439 | */ |
| 440 | STATIC int |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 441 | _xfs_buf_map_pages( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | xfs_buf_t *bp, |
| 443 | uint flags) |
| 444 | { |
| 445 | /* A single page buffer is always mappable */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 446 | if (bp->b_page_count == 1) { |
| 447 | bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset; |
| 448 | bp->b_flags |= XBF_MAPPED; |
| 449 | } else if (flags & XBF_MAPPED) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | if (as_list_len > 64) |
| 451 | purge_addresses(); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 452 | bp->b_addr = vmap(bp->b_pages, bp->b_page_count, |
| 453 | VM_MAP, PAGE_KERNEL); |
| 454 | if (unlikely(bp->b_addr == NULL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | return -ENOMEM; |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 456 | bp->b_addr += bp->b_offset; |
| 457 | bp->b_flags |= XBF_MAPPED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | return 0; |
| 461 | } |
| 462 | |
| 463 | /* |
| 464 | * Finding and Reading Buffers |
| 465 | */ |
| 466 | |
| 467 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 468 | * Look up, and creates if absent, a lockable buffer for |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | * a given range of an inode. The buffer is returned |
| 470 | * locked. If other overlapping buffers exist, they are |
| 471 | * released before the new buffer is created and locked, |
| 472 | * which may imply that this call will block until those buffers |
| 473 | * are unlocked. No I/O is implied by this call. |
| 474 | */ |
| 475 | xfs_buf_t * |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 476 | _xfs_buf_find( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | xfs_buftarg_t *btp, /* block device target */ |
Nathan Scott | 204ab25 | 2006-01-11 20:50:22 +1100 | [diff] [blame] | 478 | xfs_off_t ioff, /* starting offset of range */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | size_t isize, /* length of range */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 480 | xfs_buf_flags_t flags, |
| 481 | xfs_buf_t *new_bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | { |
Nathan Scott | 204ab25 | 2006-01-11 20:50:22 +1100 | [diff] [blame] | 483 | xfs_off_t range_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | size_t range_length; |
| 485 | xfs_bufhash_t *hash; |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 486 | xfs_buf_t *bp, *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | |
| 488 | range_base = (ioff << BBSHIFT); |
| 489 | range_length = (isize << BBSHIFT); |
| 490 | |
| 491 | /* Check for IOs smaller than the sector size / not sector aligned */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 492 | ASSERT(!(range_length < (1 << btp->bt_sshift))); |
Nathan Scott | 204ab25 | 2006-01-11 20:50:22 +1100 | [diff] [blame] | 493 | ASSERT(!(range_base & (xfs_off_t)btp->bt_smask)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | |
| 495 | hash = &btp->bt_hash[hash_long((unsigned long)ioff, btp->bt_hashshift)]; |
| 496 | |
| 497 | spin_lock(&hash->bh_lock); |
| 498 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 499 | list_for_each_entry_safe(bp, n, &hash->bh_list, b_hash_list) { |
| 500 | ASSERT(btp == bp->b_target); |
| 501 | if (bp->b_file_offset == range_base && |
| 502 | bp->b_buffer_length == range_length) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 504 | * If we look at something, bring it to the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | * front of the list for next time. |
| 506 | */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 507 | atomic_inc(&bp->b_hold); |
| 508 | list_move(&bp->b_hash_list, &hash->bh_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | goto found; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | /* No match found */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 514 | if (new_bp) { |
| 515 | _xfs_buf_initialize(new_bp, btp, range_base, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | range_length, flags); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 517 | new_bp->b_hash = hash; |
| 518 | list_add(&new_bp->b_hash_list, &hash->bh_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | } else { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 520 | XFS_STATS_INC(xb_miss_locked); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | spin_unlock(&hash->bh_lock); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 524 | return new_bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | |
| 526 | found: |
| 527 | spin_unlock(&hash->bh_lock); |
| 528 | |
| 529 | /* Attempt to get the semaphore without sleeping, |
| 530 | * if this does not work then we need to drop the |
| 531 | * spinlock and do a hard attempt on the semaphore. |
| 532 | */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 533 | if (down_trylock(&bp->b_sema)) { |
| 534 | if (!(flags & XBF_TRYLOCK)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | /* wait for buffer ownership */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 536 | XB_TRACE(bp, "get_lock", 0); |
| 537 | xfs_buf_lock(bp); |
| 538 | XFS_STATS_INC(xb_get_locked_waited); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | } else { |
| 540 | /* We asked for a trylock and failed, no need |
| 541 | * to look at file offset and length here, we |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 542 | * know that this buffer at least overlaps our |
| 543 | * buffer and is locked, therefore our buffer |
| 544 | * either does not exist, or is this buffer. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 546 | xfs_buf_rele(bp); |
| 547 | XFS_STATS_INC(xb_busy_locked); |
| 548 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | } |
| 550 | } else { |
| 551 | /* trylock worked */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 552 | XB_SET_OWNER(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 555 | if (bp->b_flags & XBF_STALE) { |
| 556 | ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0); |
| 557 | bp->b_flags &= XBF_MAPPED; |
David Chinner | 2f92658 | 2005-09-05 08:33:35 +1000 | [diff] [blame] | 558 | } |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 559 | XB_TRACE(bp, "got_lock", 0); |
| 560 | XFS_STATS_INC(xb_get_locked); |
| 561 | return bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 565 | * Assembles a buffer covering the specified range. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | * Storage in memory for all portions of the buffer will be allocated, |
| 567 | * although backing storage may not be. |
| 568 | */ |
| 569 | xfs_buf_t * |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 570 | xfs_buf_get_flags( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | xfs_buftarg_t *target,/* target for buffer */ |
Nathan Scott | 204ab25 | 2006-01-11 20:50:22 +1100 | [diff] [blame] | 572 | xfs_off_t ioff, /* starting offset of range */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | size_t isize, /* length of range */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 574 | xfs_buf_flags_t flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 576 | xfs_buf_t *bp, *new_bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | int error = 0, i; |
| 578 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 579 | new_bp = xfs_buf_allocate(flags); |
| 580 | if (unlikely(!new_bp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | return NULL; |
| 582 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 583 | bp = _xfs_buf_find(target, ioff, isize, flags, new_bp); |
| 584 | if (bp == new_bp) { |
| 585 | error = _xfs_buf_lookup_pages(bp, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | if (error) |
| 587 | goto no_buffer; |
| 588 | } else { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 589 | xfs_buf_deallocate(new_bp); |
| 590 | if (unlikely(bp == NULL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | return NULL; |
| 592 | } |
| 593 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 594 | for (i = 0; i < bp->b_page_count; i++) |
| 595 | mark_page_accessed(bp->b_pages[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 597 | if (!(bp->b_flags & XBF_MAPPED)) { |
| 598 | error = _xfs_buf_map_pages(bp, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | if (unlikely(error)) { |
| 600 | printk(KERN_WARNING "%s: failed to map pages\n", |
| 601 | __FUNCTION__); |
| 602 | goto no_buffer; |
| 603 | } |
| 604 | } |
| 605 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 606 | XFS_STATS_INC(xb_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | |
| 608 | /* |
| 609 | * Always fill in the block number now, the mapped cases can do |
| 610 | * their own overlay of this later. |
| 611 | */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 612 | bp->b_bn = ioff; |
| 613 | bp->b_count_desired = bp->b_buffer_length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 615 | XB_TRACE(bp, "get", (unsigned long)flags); |
| 616 | return bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | |
| 618 | no_buffer: |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 619 | if (flags & (XBF_LOCK | XBF_TRYLOCK)) |
| 620 | xfs_buf_unlock(bp); |
| 621 | xfs_buf_rele(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | return NULL; |
| 623 | } |
| 624 | |
| 625 | xfs_buf_t * |
| 626 | xfs_buf_read_flags( |
| 627 | xfs_buftarg_t *target, |
Nathan Scott | 204ab25 | 2006-01-11 20:50:22 +1100 | [diff] [blame] | 628 | xfs_off_t ioff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | size_t isize, |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 630 | xfs_buf_flags_t flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 632 | xfs_buf_t *bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 634 | flags |= XBF_READ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 636 | bp = xfs_buf_get_flags(target, ioff, isize, flags); |
| 637 | if (bp) { |
| 638 | if (!XFS_BUF_ISDONE(bp)) { |
| 639 | XB_TRACE(bp, "read", (unsigned long)flags); |
| 640 | XFS_STATS_INC(xb_get_read); |
| 641 | xfs_buf_iostart(bp, flags); |
| 642 | } else if (flags & XBF_ASYNC) { |
| 643 | XB_TRACE(bp, "read_async", (unsigned long)flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | /* |
| 645 | * Read ahead call which is already satisfied, |
| 646 | * drop the buffer |
| 647 | */ |
| 648 | goto no_buffer; |
| 649 | } else { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 650 | XB_TRACE(bp, "read_done", (unsigned long)flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | /* We do not want read in the flags */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 652 | bp->b_flags &= ~XBF_READ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 656 | return bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | |
| 658 | no_buffer: |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 659 | if (flags & (XBF_LOCK | XBF_TRYLOCK)) |
| 660 | xfs_buf_unlock(bp); |
| 661 | xfs_buf_rele(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | return NULL; |
| 663 | } |
| 664 | |
| 665 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 666 | * If we are not low on memory then do the readahead in a deadlock |
| 667 | * safe manner. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | */ |
| 669 | void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 670 | xfs_buf_readahead( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | xfs_buftarg_t *target, |
Nathan Scott | 204ab25 | 2006-01-11 20:50:22 +1100 | [diff] [blame] | 672 | xfs_off_t ioff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | size_t isize, |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 674 | xfs_buf_flags_t flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | { |
| 676 | struct backing_dev_info *bdi; |
| 677 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 678 | bdi = target->bt_mapping->backing_dev_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | if (bdi_read_congested(bdi)) |
| 680 | return; |
| 681 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 682 | flags |= (XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | xfs_buf_read_flags(target, ioff, isize, flags); |
| 684 | } |
| 685 | |
| 686 | xfs_buf_t * |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 687 | xfs_buf_get_empty( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | size_t len, |
| 689 | xfs_buftarg_t *target) |
| 690 | { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 691 | xfs_buf_t *bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 693 | bp = xfs_buf_allocate(0); |
| 694 | if (bp) |
| 695 | _xfs_buf_initialize(bp, target, 0, len, 0); |
| 696 | return bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | static inline struct page * |
| 700 | mem_to_page( |
| 701 | void *addr) |
| 702 | { |
| 703 | if (((unsigned long)addr < VMALLOC_START) || |
| 704 | ((unsigned long)addr >= VMALLOC_END)) { |
| 705 | return virt_to_page(addr); |
| 706 | } else { |
| 707 | return vmalloc_to_page(addr); |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | int |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 712 | xfs_buf_associate_memory( |
| 713 | xfs_buf_t *bp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | void *mem, |
| 715 | size_t len) |
| 716 | { |
| 717 | int rval; |
| 718 | int i = 0; |
| 719 | size_t ptr; |
| 720 | size_t end, end_cur; |
| 721 | off_t offset; |
| 722 | int page_count; |
| 723 | |
| 724 | page_count = PAGE_CACHE_ALIGN(len) >> PAGE_CACHE_SHIFT; |
| 725 | offset = (off_t) mem - ((off_t)mem & PAGE_CACHE_MASK); |
| 726 | if (offset && (len > PAGE_CACHE_SIZE)) |
| 727 | page_count++; |
| 728 | |
| 729 | /* Free any previous set of page pointers */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 730 | if (bp->b_pages) |
| 731 | _xfs_buf_free_pages(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 733 | bp->b_pages = NULL; |
| 734 | bp->b_addr = mem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 736 | rval = _xfs_buf_get_pages(bp, page_count, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | if (rval) |
| 738 | return rval; |
| 739 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 740 | bp->b_offset = offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | ptr = (size_t) mem & PAGE_CACHE_MASK; |
| 742 | end = PAGE_CACHE_ALIGN((size_t) mem + len); |
| 743 | end_cur = end; |
| 744 | /* set up first page */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 745 | bp->b_pages[0] = mem_to_page(mem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | |
| 747 | ptr += PAGE_CACHE_SIZE; |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 748 | bp->b_page_count = ++i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | while (ptr < end) { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 750 | bp->b_pages[i] = mem_to_page((void *)ptr); |
| 751 | bp->b_page_count = ++i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | ptr += PAGE_CACHE_SIZE; |
| 753 | } |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 754 | bp->b_locked = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 756 | bp->b_count_desired = bp->b_buffer_length = len; |
| 757 | bp->b_flags |= XBF_MAPPED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | |
| 759 | return 0; |
| 760 | } |
| 761 | |
| 762 | xfs_buf_t * |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 763 | xfs_buf_get_noaddr( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | size_t len, |
| 765 | xfs_buftarg_t *target) |
| 766 | { |
| 767 | size_t malloc_len = len; |
| 768 | xfs_buf_t *bp; |
| 769 | void *data; |
| 770 | int error; |
| 771 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 772 | bp = xfs_buf_allocate(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | if (unlikely(bp == NULL)) |
| 774 | goto fail; |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 775 | _xfs_buf_initialize(bp, target, 0, len, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | |
| 777 | try_again: |
Nathan Scott | efb8ad7 | 2006-09-28 11:03:05 +1000 | [diff] [blame] | 778 | data = kmem_alloc(malloc_len, KM_SLEEP | KM_MAYFAIL | KM_LARGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | if (unlikely(data == NULL)) |
| 780 | goto fail_free_buf; |
| 781 | |
| 782 | /* check whether alignment matches.. */ |
| 783 | if ((__psunsigned_t)data != |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 784 | ((__psunsigned_t)data & ~target->bt_smask)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | /* .. else double the size and try again */ |
| 786 | kmem_free(data, malloc_len); |
| 787 | malloc_len <<= 1; |
| 788 | goto try_again; |
| 789 | } |
| 790 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 791 | error = xfs_buf_associate_memory(bp, data, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | if (error) |
| 793 | goto fail_free_mem; |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 794 | bp->b_flags |= _XBF_KMEM_ALLOC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 796 | xfs_buf_unlock(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 798 | XB_TRACE(bp, "no_daddr", data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | return bp; |
| 800 | fail_free_mem: |
| 801 | kmem_free(data, malloc_len); |
| 802 | fail_free_buf: |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 803 | xfs_buf_free(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | fail: |
| 805 | return NULL; |
| 806 | } |
| 807 | |
| 808 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | * Increment reference count on buffer, to hold the buffer concurrently |
| 810 | * with another thread which may release (free) the buffer asynchronously. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | * Must hold the buffer already to call this function. |
| 812 | */ |
| 813 | void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 814 | xfs_buf_hold( |
| 815 | xfs_buf_t *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 817 | atomic_inc(&bp->b_hold); |
| 818 | XB_TRACE(bp, "hold", 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 822 | * Releases a hold on the specified buffer. If the |
| 823 | * the hold count is 1, calls xfs_buf_free. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | */ |
| 825 | void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 826 | xfs_buf_rele( |
| 827 | xfs_buf_t *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 829 | xfs_bufhash_t *hash = bp->b_hash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 831 | XB_TRACE(bp, "rele", bp->b_relse); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | |
Nathan Scott | fad3aa1 | 2006-02-01 12:14:52 +1100 | [diff] [blame] | 833 | if (unlikely(!hash)) { |
| 834 | ASSERT(!bp->b_relse); |
| 835 | if (atomic_dec_and_test(&bp->b_hold)) |
| 836 | xfs_buf_free(bp); |
| 837 | return; |
| 838 | } |
| 839 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 840 | if (atomic_dec_and_lock(&bp->b_hold, &hash->bh_lock)) { |
| 841 | if (bp->b_relse) { |
| 842 | atomic_inc(&bp->b_hold); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | spin_unlock(&hash->bh_lock); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 844 | (*(bp->b_relse)) (bp); |
| 845 | } else if (bp->b_flags & XBF_FS_MANAGED) { |
Christoph Hellwig | 7f14d0a | 2005-11-02 15:09:35 +1100 | [diff] [blame] | 846 | spin_unlock(&hash->bh_lock); |
| 847 | } else { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 848 | ASSERT(!(bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q))); |
| 849 | list_del_init(&bp->b_hash_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | spin_unlock(&hash->bh_lock); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 851 | xfs_buf_free(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | } |
David Chinner | 2f92658 | 2005-09-05 08:33:35 +1000 | [diff] [blame] | 853 | } else { |
| 854 | /* |
| 855 | * Catch reference count leaks |
| 856 | */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 857 | ASSERT(atomic_read(&bp->b_hold) >= 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | } |
| 859 | } |
| 860 | |
| 861 | |
| 862 | /* |
| 863 | * Mutual exclusion on buffers. Locking model: |
| 864 | * |
| 865 | * Buffers associated with inodes for which buffer locking |
| 866 | * is not enabled are not protected by semaphores, and are |
| 867 | * assumed to be exclusively owned by the caller. There is a |
| 868 | * spinlock in the buffer, used by the caller when concurrent |
| 869 | * access is possible. |
| 870 | */ |
| 871 | |
| 872 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 873 | * Locks a buffer object, if it is not already locked. |
| 874 | * Note that this in no way locks the underlying pages, so it is only |
| 875 | * useful for synchronizing concurrent use of buffer objects, not for |
| 876 | * synchronizing independent access to the underlying pages. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | */ |
| 878 | int |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 879 | xfs_buf_cond_lock( |
| 880 | xfs_buf_t *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | { |
| 882 | int locked; |
| 883 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 884 | locked = down_trylock(&bp->b_sema) == 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | if (locked) { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 886 | XB_SET_OWNER(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | } |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 888 | XB_TRACE(bp, "cond_lock", (long)locked); |
| 889 | return locked ? 0 : -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | #if defined(DEBUG) || defined(XFS_BLI_TRACE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | int |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 894 | xfs_buf_lock_value( |
| 895 | xfs_buf_t *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 897 | return atomic_read(&bp->b_sema.count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | } |
| 899 | #endif |
| 900 | |
| 901 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 902 | * Locks a buffer object. |
| 903 | * Note that this in no way locks the underlying pages, so it is only |
| 904 | * useful for synchronizing concurrent use of buffer objects, not for |
| 905 | * synchronizing independent access to the underlying pages. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 907 | void |
| 908 | xfs_buf_lock( |
| 909 | xfs_buf_t *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 911 | XB_TRACE(bp, "lock", 0); |
| 912 | if (atomic_read(&bp->b_io_remaining)) |
| 913 | blk_run_address_space(bp->b_target->bt_mapping); |
| 914 | down(&bp->b_sema); |
| 915 | XB_SET_OWNER(bp); |
| 916 | XB_TRACE(bp, "locked", 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 920 | * Releases the lock on the buffer object. |
David Chinner | 2f92658 | 2005-09-05 08:33:35 +1000 | [diff] [blame] | 921 | * If the buffer is marked delwri but is not queued, do so before we |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 922 | * unlock the buffer as we need to set flags correctly. We also need to |
David Chinner | 2f92658 | 2005-09-05 08:33:35 +1000 | [diff] [blame] | 923 | * take a reference for the delwri queue because the unlocker is going to |
| 924 | * drop their's and they don't know we just queued it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | */ |
| 926 | void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 927 | xfs_buf_unlock( |
| 928 | xfs_buf_t *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 930 | if ((bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)) == XBF_DELWRI) { |
| 931 | atomic_inc(&bp->b_hold); |
| 932 | bp->b_flags |= XBF_ASYNC; |
| 933 | xfs_buf_delwri_queue(bp, 0); |
David Chinner | 2f92658 | 2005-09-05 08:33:35 +1000 | [diff] [blame] | 934 | } |
| 935 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 936 | XB_CLEAR_OWNER(bp); |
| 937 | up(&bp->b_sema); |
| 938 | XB_TRACE(bp, "unlock", 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | |
| 942 | /* |
| 943 | * Pinning Buffer Storage in Memory |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 944 | * Ensure that no attempt to force a buffer to disk will succeed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | */ |
| 946 | void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 947 | xfs_buf_pin( |
| 948 | xfs_buf_t *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 950 | atomic_inc(&bp->b_pin_count); |
| 951 | XB_TRACE(bp, "pin", (long)bp->b_pin_count.counter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | } |
| 953 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 955 | xfs_buf_unpin( |
| 956 | xfs_buf_t *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 958 | if (atomic_dec_and_test(&bp->b_pin_count)) |
| 959 | wake_up_all(&bp->b_waiters); |
| 960 | XB_TRACE(bp, "unpin", (long)bp->b_pin_count.counter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | int |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 964 | xfs_buf_ispin( |
| 965 | xfs_buf_t *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 967 | return atomic_read(&bp->b_pin_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | } |
| 969 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 970 | STATIC void |
| 971 | xfs_buf_wait_unpin( |
| 972 | xfs_buf_t *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | { |
| 974 | DECLARE_WAITQUEUE (wait, current); |
| 975 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 976 | if (atomic_read(&bp->b_pin_count) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | return; |
| 978 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 979 | add_wait_queue(&bp->b_waiters, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | for (;;) { |
| 981 | set_current_state(TASK_UNINTERRUPTIBLE); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 982 | if (atomic_read(&bp->b_pin_count) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | break; |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 984 | if (atomic_read(&bp->b_io_remaining)) |
| 985 | blk_run_address_space(bp->b_target->bt_mapping); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | schedule(); |
| 987 | } |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 988 | remove_wait_queue(&bp->b_waiters, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | set_current_state(TASK_RUNNING); |
| 990 | } |
| 991 | |
| 992 | /* |
| 993 | * Buffer Utility Routines |
| 994 | */ |
| 995 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | STATIC void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 997 | xfs_buf_iodone_work( |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 998 | struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1000 | xfs_buf_t *bp = |
| 1001 | container_of(work, xfs_buf_t, b_iodone_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1003 | if (bp->b_iodone) |
| 1004 | (*(bp->b_iodone))(bp); |
| 1005 | else if (bp->b_flags & XBF_ASYNC) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | xfs_buf_relse(bp); |
| 1007 | } |
| 1008 | |
| 1009 | void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1010 | xfs_buf_ioend( |
| 1011 | xfs_buf_t *bp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | int schedule) |
| 1013 | { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1014 | bp->b_flags &= ~(XBF_READ | XBF_WRITE); |
| 1015 | if (bp->b_error == 0) |
| 1016 | bp->b_flags |= XBF_DONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1018 | XB_TRACE(bp, "iodone", bp->b_iodone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1020 | if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | if (schedule) { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1022 | INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1023 | queue_work(xfslogd_workqueue, &bp->b_iodone_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | } else { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1025 | xfs_buf_iodone_work(&bp->b_iodone_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | } |
| 1027 | } else { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1028 | up(&bp->b_iodonesema); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | } |
| 1030 | } |
| 1031 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1033 | xfs_buf_ioerror( |
| 1034 | xfs_buf_t *bp, |
| 1035 | int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | { |
| 1037 | ASSERT(error >= 0 && error <= 0xffff); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1038 | bp->b_error = (unsigned short)error; |
| 1039 | XB_TRACE(bp, "ioerror", (unsigned long)error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1043 | * Initiate I/O on a buffer, based on the flags supplied. |
| 1044 | * The b_iodone routine in the buffer supplied will only be called |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | * when all of the subsidiary I/O requests, if any, have been completed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | */ |
| 1047 | int |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1048 | xfs_buf_iostart( |
| 1049 | xfs_buf_t *bp, |
| 1050 | xfs_buf_flags_t flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | { |
| 1052 | int status = 0; |
| 1053 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1054 | XB_TRACE(bp, "iostart", (unsigned long)flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1056 | if (flags & XBF_DELWRI) { |
| 1057 | bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_ASYNC); |
| 1058 | bp->b_flags |= flags & (XBF_DELWRI | XBF_ASYNC); |
| 1059 | xfs_buf_delwri_queue(bp, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | return status; |
| 1061 | } |
| 1062 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1063 | bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_ASYNC | XBF_DELWRI | \ |
| 1064 | XBF_READ_AHEAD | _XBF_RUN_QUEUES); |
| 1065 | bp->b_flags |= flags & (XBF_READ | XBF_WRITE | XBF_ASYNC | \ |
| 1066 | XBF_READ_AHEAD | _XBF_RUN_QUEUES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1068 | BUG_ON(bp->b_bn == XFS_BUF_DADDR_NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | |
| 1070 | /* For writes allow an alternate strategy routine to precede |
| 1071 | * the actual I/O request (which may not be issued at all in |
| 1072 | * a shutdown situation, for example). |
| 1073 | */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1074 | status = (flags & XBF_WRITE) ? |
| 1075 | xfs_buf_iostrategy(bp) : xfs_buf_iorequest(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | |
| 1077 | /* Wait for I/O if we are not an async request. |
| 1078 | * Note: async I/O request completion will release the buffer, |
| 1079 | * and that can already be done by this point. So using the |
| 1080 | * buffer pointer from here on, after async I/O, is invalid. |
| 1081 | */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1082 | if (!status && !(flags & XBF_ASYNC)) |
| 1083 | status = xfs_buf_iowait(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | |
| 1085 | return status; |
| 1086 | } |
| 1087 | |
David Chinner | 7989cb8 | 2007-02-10 18:34:56 +1100 | [diff] [blame] | 1088 | STATIC_INLINE int |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1089 | _xfs_buf_iolocked( |
| 1090 | xfs_buf_t *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1092 | ASSERT(bp->b_flags & (XBF_READ | XBF_WRITE)); |
| 1093 | if (bp->b_flags & XBF_READ) |
| 1094 | return bp->b_locked; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | return 0; |
| 1096 | } |
| 1097 | |
David Chinner | 7989cb8 | 2007-02-10 18:34:56 +1100 | [diff] [blame] | 1098 | STATIC_INLINE void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1099 | _xfs_buf_ioend( |
| 1100 | xfs_buf_t *bp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | int schedule) |
| 1102 | { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1103 | if (atomic_dec_and_test(&bp->b_io_remaining) == 1) { |
| 1104 | bp->b_locked = 0; |
| 1105 | xfs_buf_ioend(bp, schedule); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | } |
| 1107 | } |
| 1108 | |
| 1109 | STATIC int |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1110 | xfs_buf_bio_end_io( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | struct bio *bio, |
| 1112 | unsigned int bytes_done, |
| 1113 | int error) |
| 1114 | { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1115 | xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private; |
| 1116 | unsigned int blocksize = bp->b_target->bt_bsize; |
Nathan Scott | eedb553 | 2005-09-02 16:39:56 +1000 | [diff] [blame] | 1117 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | |
| 1119 | if (bio->bi_size) |
| 1120 | return 1; |
| 1121 | |
| 1122 | if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1123 | bp->b_error = EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | |
Nathan Scott | eedb553 | 2005-09-02 16:39:56 +1000 | [diff] [blame] | 1125 | do { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | struct page *page = bvec->bv_page; |
| 1127 | |
Nathan Scott | 948ecdb | 2006-09-28 11:03:13 +1000 | [diff] [blame] | 1128 | ASSERT(!PagePrivate(page)); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1129 | if (unlikely(bp->b_error)) { |
| 1130 | if (bp->b_flags & XBF_READ) |
Nathan Scott | eedb553 | 2005-09-02 16:39:56 +1000 | [diff] [blame] | 1131 | ClearPageUptodate(page); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1132 | } else if (blocksize >= PAGE_CACHE_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | SetPageUptodate(page); |
| 1134 | } else if (!PagePrivate(page) && |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1135 | (bp->b_flags & _XBF_PAGE_CACHE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | set_page_region(page, bvec->bv_offset, bvec->bv_len); |
| 1137 | } |
| 1138 | |
Nathan Scott | eedb553 | 2005-09-02 16:39:56 +1000 | [diff] [blame] | 1139 | if (--bvec >= bio->bi_io_vec) |
| 1140 | prefetchw(&bvec->bv_page->flags); |
| 1141 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1142 | if (_xfs_buf_iolocked(bp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | unlock_page(page); |
| 1144 | } |
Nathan Scott | eedb553 | 2005-09-02 16:39:56 +1000 | [diff] [blame] | 1145 | } while (bvec >= bio->bi_io_vec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1147 | _xfs_buf_ioend(bp, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | bio_put(bio); |
| 1149 | return 0; |
| 1150 | } |
| 1151 | |
| 1152 | STATIC void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1153 | _xfs_buf_ioapply( |
| 1154 | xfs_buf_t *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | { |
| 1156 | int i, rw, map_i, total_nr_pages, nr_pages; |
| 1157 | struct bio *bio; |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1158 | int offset = bp->b_offset; |
| 1159 | int size = bp->b_count_desired; |
| 1160 | sector_t sector = bp->b_bn; |
| 1161 | unsigned int blocksize = bp->b_target->bt_bsize; |
| 1162 | int locking = _xfs_buf_iolocked(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1164 | total_nr_pages = bp->b_page_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | map_i = 0; |
| 1166 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1167 | if (bp->b_flags & XBF_ORDERED) { |
| 1168 | ASSERT(!(bp->b_flags & XBF_READ)); |
Christoph Hellwig | f538d4d | 2005-11-02 10:26:59 +1100 | [diff] [blame] | 1169 | rw = WRITE_BARRIER; |
Nathan Scott | 51bdd70 | 2006-09-28 11:01:57 +1000 | [diff] [blame] | 1170 | } else if (bp->b_flags & _XBF_RUN_QUEUES) { |
| 1171 | ASSERT(!(bp->b_flags & XBF_READ_AHEAD)); |
| 1172 | bp->b_flags &= ~_XBF_RUN_QUEUES; |
| 1173 | rw = (bp->b_flags & XBF_WRITE) ? WRITE_SYNC : READ_SYNC; |
| 1174 | } else { |
| 1175 | rw = (bp->b_flags & XBF_WRITE) ? WRITE : |
| 1176 | (bp->b_flags & XBF_READ_AHEAD) ? READA : READ; |
Christoph Hellwig | f538d4d | 2005-11-02 10:26:59 +1100 | [diff] [blame] | 1177 | } |
| 1178 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1179 | /* Special code path for reading a sub page size buffer in -- |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | * we populate up the whole page, and hence the other metadata |
| 1181 | * in the same page. This optimization is only valid when the |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1182 | * filesystem block size is not smaller than the page size. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1184 | if ((bp->b_buffer_length < PAGE_CACHE_SIZE) && |
| 1185 | (bp->b_flags & XBF_READ) && locking && |
| 1186 | (blocksize >= PAGE_CACHE_SIZE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | bio = bio_alloc(GFP_NOIO, 1); |
| 1188 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1189 | bio->bi_bdev = bp->b_target->bt_bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | bio->bi_sector = sector - (offset >> BBSHIFT); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1191 | bio->bi_end_io = xfs_buf_bio_end_io; |
| 1192 | bio->bi_private = bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1194 | bio_add_page(bio, bp->b_pages[0], PAGE_CACHE_SIZE, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | size = 0; |
| 1196 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1197 | atomic_inc(&bp->b_io_remaining); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | |
| 1199 | goto submit_io; |
| 1200 | } |
| 1201 | |
| 1202 | /* Lock down the pages which we need to for the request */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1203 | if (locking && (bp->b_flags & XBF_WRITE) && (bp->b_locked == 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | for (i = 0; size; i++) { |
| 1205 | int nbytes = PAGE_CACHE_SIZE - offset; |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1206 | struct page *page = bp->b_pages[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | |
| 1208 | if (nbytes > size) |
| 1209 | nbytes = size; |
| 1210 | |
| 1211 | lock_page(page); |
| 1212 | |
| 1213 | size -= nbytes; |
| 1214 | offset = 0; |
| 1215 | } |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1216 | offset = bp->b_offset; |
| 1217 | size = bp->b_count_desired; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | next_chunk: |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1221 | atomic_inc(&bp->b_io_remaining); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT); |
| 1223 | if (nr_pages > total_nr_pages) |
| 1224 | nr_pages = total_nr_pages; |
| 1225 | |
| 1226 | bio = bio_alloc(GFP_NOIO, nr_pages); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1227 | bio->bi_bdev = bp->b_target->bt_bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | bio->bi_sector = sector; |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1229 | bio->bi_end_io = xfs_buf_bio_end_io; |
| 1230 | bio->bi_private = bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | |
| 1232 | for (; size && nr_pages; nr_pages--, map_i++) { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1233 | int rbytes, nbytes = PAGE_CACHE_SIZE - offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | |
| 1235 | if (nbytes > size) |
| 1236 | nbytes = size; |
| 1237 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1238 | rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset); |
| 1239 | if (rbytes < nbytes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | break; |
| 1241 | |
| 1242 | offset = 0; |
| 1243 | sector += nbytes >> BBSHIFT; |
| 1244 | size -= nbytes; |
| 1245 | total_nr_pages--; |
| 1246 | } |
| 1247 | |
| 1248 | submit_io: |
| 1249 | if (likely(bio->bi_size)) { |
| 1250 | submit_bio(rw, bio); |
| 1251 | if (size) |
| 1252 | goto next_chunk; |
| 1253 | } else { |
| 1254 | bio_put(bio); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1255 | xfs_buf_ioerror(bp, EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | } |
| 1257 | } |
| 1258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | int |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1260 | xfs_buf_iorequest( |
| 1261 | xfs_buf_t *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1263 | XB_TRACE(bp, "iorequest", 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1265 | if (bp->b_flags & XBF_DELWRI) { |
| 1266 | xfs_buf_delwri_queue(bp, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | return 0; |
| 1268 | } |
| 1269 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1270 | if (bp->b_flags & XBF_WRITE) { |
| 1271 | xfs_buf_wait_unpin(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | } |
| 1273 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1274 | xfs_buf_hold(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | |
| 1276 | /* Set the count to 1 initially, this will stop an I/O |
| 1277 | * completion callout which happens before we have started |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1278 | * all the I/O from calling xfs_buf_ioend too early. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1280 | atomic_set(&bp->b_io_remaining, 1); |
| 1281 | _xfs_buf_ioapply(bp); |
| 1282 | _xfs_buf_ioend(bp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1284 | xfs_buf_rele(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | return 0; |
| 1286 | } |
| 1287 | |
| 1288 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1289 | * Waits for I/O to complete on the buffer supplied. |
| 1290 | * It returns immediately if no I/O is pending. |
| 1291 | * It returns the I/O error code, if any, or 0 if there was no error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | */ |
| 1293 | int |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1294 | xfs_buf_iowait( |
| 1295 | xfs_buf_t *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1297 | XB_TRACE(bp, "iowait", 0); |
| 1298 | if (atomic_read(&bp->b_io_remaining)) |
| 1299 | blk_run_address_space(bp->b_target->bt_mapping); |
| 1300 | down(&bp->b_iodonesema); |
| 1301 | XB_TRACE(bp, "iowaited", (long)bp->b_error); |
| 1302 | return bp->b_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | } |
| 1304 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1305 | xfs_caddr_t |
| 1306 | xfs_buf_offset( |
| 1307 | xfs_buf_t *bp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | size_t offset) |
| 1309 | { |
| 1310 | struct page *page; |
| 1311 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1312 | if (bp->b_flags & XBF_MAPPED) |
| 1313 | return XFS_BUF_PTR(bp) + offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1315 | offset += bp->b_offset; |
| 1316 | page = bp->b_pages[offset >> PAGE_CACHE_SHIFT]; |
| 1317 | return (xfs_caddr_t)page_address(page) + (offset & (PAGE_CACHE_SIZE-1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | * Move data into or out of a buffer. |
| 1322 | */ |
| 1323 | void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1324 | xfs_buf_iomove( |
| 1325 | xfs_buf_t *bp, /* buffer to process */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | size_t boff, /* starting buffer offset */ |
| 1327 | size_t bsize, /* length to copy */ |
| 1328 | caddr_t data, /* data address */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1329 | xfs_buf_rw_t mode) /* read/write/zero flag */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | { |
| 1331 | size_t bend, cpoff, csize; |
| 1332 | struct page *page; |
| 1333 | |
| 1334 | bend = boff + bsize; |
| 1335 | while (boff < bend) { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1336 | page = bp->b_pages[xfs_buf_btoct(boff + bp->b_offset)]; |
| 1337 | cpoff = xfs_buf_poff(boff + bp->b_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1338 | csize = min_t(size_t, |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1339 | PAGE_CACHE_SIZE-cpoff, bp->b_count_desired-boff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | |
| 1341 | ASSERT(((csize + cpoff) <= PAGE_CACHE_SIZE)); |
| 1342 | |
| 1343 | switch (mode) { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1344 | case XBRW_ZERO: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | memset(page_address(page) + cpoff, 0, csize); |
| 1346 | break; |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1347 | case XBRW_READ: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | memcpy(data, page_address(page) + cpoff, csize); |
| 1349 | break; |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1350 | case XBRW_WRITE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | memcpy(page_address(page) + cpoff, data, csize); |
| 1352 | } |
| 1353 | |
| 1354 | boff += csize; |
| 1355 | data += csize; |
| 1356 | } |
| 1357 | } |
| 1358 | |
| 1359 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1360 | * Handling of buffer targets (buftargs). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | */ |
| 1362 | |
| 1363 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1364 | * Wait for any bufs with callbacks that have been submitted but |
| 1365 | * have not yet returned... walk the hash list for the target. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | */ |
| 1367 | void |
| 1368 | xfs_wait_buftarg( |
| 1369 | xfs_buftarg_t *btp) |
| 1370 | { |
| 1371 | xfs_buf_t *bp, *n; |
| 1372 | xfs_bufhash_t *hash; |
| 1373 | uint i; |
| 1374 | |
| 1375 | for (i = 0; i < (1 << btp->bt_hashshift); i++) { |
| 1376 | hash = &btp->bt_hash[i]; |
| 1377 | again: |
| 1378 | spin_lock(&hash->bh_lock); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1379 | list_for_each_entry_safe(bp, n, &hash->bh_list, b_hash_list) { |
| 1380 | ASSERT(btp == bp->b_target); |
| 1381 | if (!(bp->b_flags & XBF_FS_MANAGED)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | spin_unlock(&hash->bh_lock); |
David Chinner | 2f92658 | 2005-09-05 08:33:35 +1000 | [diff] [blame] | 1383 | /* |
| 1384 | * Catch superblock reference count leaks |
| 1385 | * immediately |
| 1386 | */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1387 | BUG_ON(bp->b_bn == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | delay(100); |
| 1389 | goto again; |
| 1390 | } |
| 1391 | } |
| 1392 | spin_unlock(&hash->bh_lock); |
| 1393 | } |
| 1394 | } |
| 1395 | |
| 1396 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1397 | * Allocate buffer hash table for a given target. |
| 1398 | * For devices containing metadata (i.e. not the log/realtime devices) |
| 1399 | * we need to allocate a much larger hash table. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | */ |
| 1401 | STATIC void |
| 1402 | xfs_alloc_bufhash( |
| 1403 | xfs_buftarg_t *btp, |
| 1404 | int external) |
| 1405 | { |
| 1406 | unsigned int i; |
| 1407 | |
| 1408 | btp->bt_hashshift = external ? 3 : 8; /* 8 or 256 buckets */ |
| 1409 | btp->bt_hashmask = (1 << btp->bt_hashshift) - 1; |
| 1410 | btp->bt_hash = kmem_zalloc((1 << btp->bt_hashshift) * |
Vlad Apostolov | 93c189c | 2006-11-11 18:03:49 +1100 | [diff] [blame] | 1411 | sizeof(xfs_bufhash_t), KM_SLEEP | KM_LARGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | for (i = 0; i < (1 << btp->bt_hashshift); i++) { |
| 1413 | spin_lock_init(&btp->bt_hash[i].bh_lock); |
| 1414 | INIT_LIST_HEAD(&btp->bt_hash[i].bh_list); |
| 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | STATIC void |
| 1419 | xfs_free_bufhash( |
| 1420 | xfs_buftarg_t *btp) |
| 1421 | { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1422 | kmem_free(btp->bt_hash, (1<<btp->bt_hashshift) * sizeof(xfs_bufhash_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | btp->bt_hash = NULL; |
| 1424 | } |
| 1425 | |
David Chinner | a6867a6 | 2006-01-11 15:37:58 +1100 | [diff] [blame] | 1426 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1427 | * buftarg list for delwrite queue processing |
David Chinner | a6867a6 | 2006-01-11 15:37:58 +1100 | [diff] [blame] | 1428 | */ |
Tim Shimmin | e6a0e9c | 2007-05-08 13:49:59 +1000 | [diff] [blame^] | 1429 | static LIST_HEAD(xfs_buftarg_list); |
David Chinner | 7989cb8 | 2007-02-10 18:34:56 +1100 | [diff] [blame] | 1430 | static DEFINE_SPINLOCK(xfs_buftarg_lock); |
David Chinner | a6867a6 | 2006-01-11 15:37:58 +1100 | [diff] [blame] | 1431 | |
| 1432 | STATIC void |
| 1433 | xfs_register_buftarg( |
| 1434 | xfs_buftarg_t *btp) |
| 1435 | { |
| 1436 | spin_lock(&xfs_buftarg_lock); |
| 1437 | list_add(&btp->bt_list, &xfs_buftarg_list); |
| 1438 | spin_unlock(&xfs_buftarg_lock); |
| 1439 | } |
| 1440 | |
| 1441 | STATIC void |
| 1442 | xfs_unregister_buftarg( |
| 1443 | xfs_buftarg_t *btp) |
| 1444 | { |
| 1445 | spin_lock(&xfs_buftarg_lock); |
| 1446 | list_del(&btp->bt_list); |
| 1447 | spin_unlock(&xfs_buftarg_lock); |
| 1448 | } |
| 1449 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1450 | void |
| 1451 | xfs_free_buftarg( |
| 1452 | xfs_buftarg_t *btp, |
| 1453 | int external) |
| 1454 | { |
| 1455 | xfs_flush_buftarg(btp, 1); |
| 1456 | if (external) |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1457 | xfs_blkdev_put(btp->bt_bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | xfs_free_bufhash(btp); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1459 | iput(btp->bt_mapping->host); |
David Chinner | a6867a6 | 2006-01-11 15:37:58 +1100 | [diff] [blame] | 1460 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1461 | /* Unregister the buftarg first so that we don't get a |
| 1462 | * wakeup finding a non-existent task |
| 1463 | */ |
David Chinner | a6867a6 | 2006-01-11 15:37:58 +1100 | [diff] [blame] | 1464 | xfs_unregister_buftarg(btp); |
| 1465 | kthread_stop(btp->bt_task); |
| 1466 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | kmem_free(btp, sizeof(*btp)); |
| 1468 | } |
| 1469 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | STATIC int |
| 1471 | xfs_setsize_buftarg_flags( |
| 1472 | xfs_buftarg_t *btp, |
| 1473 | unsigned int blocksize, |
| 1474 | unsigned int sectorsize, |
| 1475 | int verbose) |
| 1476 | { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1477 | btp->bt_bsize = blocksize; |
| 1478 | btp->bt_sshift = ffs(sectorsize) - 1; |
| 1479 | btp->bt_smask = sectorsize - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1481 | if (set_blocksize(btp->bt_bdev, sectorsize)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | printk(KERN_WARNING |
| 1483 | "XFS: Cannot set_blocksize to %u on device %s\n", |
| 1484 | sectorsize, XFS_BUFTARG_NAME(btp)); |
| 1485 | return EINVAL; |
| 1486 | } |
| 1487 | |
| 1488 | if (verbose && |
| 1489 | (PAGE_CACHE_SIZE / BITS_PER_LONG) > sectorsize) { |
| 1490 | printk(KERN_WARNING |
| 1491 | "XFS: %u byte sectors in use on device %s. " |
| 1492 | "This is suboptimal; %u or greater is ideal.\n", |
| 1493 | sectorsize, XFS_BUFTARG_NAME(btp), |
| 1494 | (unsigned int)PAGE_CACHE_SIZE / BITS_PER_LONG); |
| 1495 | } |
| 1496 | |
| 1497 | return 0; |
| 1498 | } |
| 1499 | |
| 1500 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1501 | * When allocating the initial buffer target we have not yet |
| 1502 | * read in the superblock, so don't know what sized sectors |
| 1503 | * are being used is at this early stage. Play safe. |
| 1504 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | STATIC int |
| 1506 | xfs_setsize_buftarg_early( |
| 1507 | xfs_buftarg_t *btp, |
| 1508 | struct block_device *bdev) |
| 1509 | { |
| 1510 | return xfs_setsize_buftarg_flags(btp, |
| 1511 | PAGE_CACHE_SIZE, bdev_hardsect_size(bdev), 0); |
| 1512 | } |
| 1513 | |
| 1514 | int |
| 1515 | xfs_setsize_buftarg( |
| 1516 | xfs_buftarg_t *btp, |
| 1517 | unsigned int blocksize, |
| 1518 | unsigned int sectorsize) |
| 1519 | { |
| 1520 | return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1); |
| 1521 | } |
| 1522 | |
| 1523 | STATIC int |
| 1524 | xfs_mapping_buftarg( |
| 1525 | xfs_buftarg_t *btp, |
| 1526 | struct block_device *bdev) |
| 1527 | { |
| 1528 | struct backing_dev_info *bdi; |
| 1529 | struct inode *inode; |
| 1530 | struct address_space *mapping; |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 1531 | static const struct address_space_operations mapping_aops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | .sync_page = block_sync_page, |
Christoph Lameter | e965f96 | 2006-02-01 03:05:41 -0800 | [diff] [blame] | 1533 | .migratepage = fail_migrate_page, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | }; |
| 1535 | |
| 1536 | inode = new_inode(bdev->bd_inode->i_sb); |
| 1537 | if (!inode) { |
| 1538 | printk(KERN_WARNING |
| 1539 | "XFS: Cannot allocate mapping inode for device %s\n", |
| 1540 | XFS_BUFTARG_NAME(btp)); |
| 1541 | return ENOMEM; |
| 1542 | } |
| 1543 | inode->i_mode = S_IFBLK; |
| 1544 | inode->i_bdev = bdev; |
| 1545 | inode->i_rdev = bdev->bd_dev; |
| 1546 | bdi = blk_get_backing_dev_info(bdev); |
| 1547 | if (!bdi) |
| 1548 | bdi = &default_backing_dev_info; |
| 1549 | mapping = &inode->i_data; |
| 1550 | mapping->a_ops = &mapping_aops; |
| 1551 | mapping->backing_dev_info = bdi; |
| 1552 | mapping_set_gfp_mask(mapping, GFP_NOFS); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1553 | btp->bt_mapping = mapping; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | return 0; |
| 1555 | } |
| 1556 | |
David Chinner | a6867a6 | 2006-01-11 15:37:58 +1100 | [diff] [blame] | 1557 | STATIC int |
| 1558 | xfs_alloc_delwrite_queue( |
| 1559 | xfs_buftarg_t *btp) |
| 1560 | { |
| 1561 | int error = 0; |
| 1562 | |
| 1563 | INIT_LIST_HEAD(&btp->bt_list); |
| 1564 | INIT_LIST_HEAD(&btp->bt_delwrite_queue); |
| 1565 | spinlock_init(&btp->bt_delwrite_lock, "delwri_lock"); |
| 1566 | btp->bt_flags = 0; |
| 1567 | btp->bt_task = kthread_run(xfsbufd, btp, "xfsbufd"); |
| 1568 | if (IS_ERR(btp->bt_task)) { |
| 1569 | error = PTR_ERR(btp->bt_task); |
| 1570 | goto out_error; |
| 1571 | } |
| 1572 | xfs_register_buftarg(btp); |
| 1573 | out_error: |
| 1574 | return error; |
| 1575 | } |
| 1576 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | xfs_buftarg_t * |
| 1578 | xfs_alloc_buftarg( |
| 1579 | struct block_device *bdev, |
| 1580 | int external) |
| 1581 | { |
| 1582 | xfs_buftarg_t *btp; |
| 1583 | |
| 1584 | btp = kmem_zalloc(sizeof(*btp), KM_SLEEP); |
| 1585 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1586 | btp->bt_dev = bdev->bd_dev; |
| 1587 | btp->bt_bdev = bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | if (xfs_setsize_buftarg_early(btp, bdev)) |
| 1589 | goto error; |
| 1590 | if (xfs_mapping_buftarg(btp, bdev)) |
| 1591 | goto error; |
David Chinner | a6867a6 | 2006-01-11 15:37:58 +1100 | [diff] [blame] | 1592 | if (xfs_alloc_delwrite_queue(btp)) |
| 1593 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | xfs_alloc_bufhash(btp, external); |
| 1595 | return btp; |
| 1596 | |
| 1597 | error: |
| 1598 | kmem_free(btp, sizeof(*btp)); |
| 1599 | return NULL; |
| 1600 | } |
| 1601 | |
| 1602 | |
| 1603 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1604 | * Delayed write buffer handling |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1605 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | STATIC void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1607 | xfs_buf_delwri_queue( |
| 1608 | xfs_buf_t *bp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | int unlock) |
| 1610 | { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1611 | struct list_head *dwq = &bp->b_target->bt_delwrite_queue; |
| 1612 | spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock; |
David Chinner | a6867a6 | 2006-01-11 15:37:58 +1100 | [diff] [blame] | 1613 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1614 | XB_TRACE(bp, "delwri_q", (long)unlock); |
| 1615 | ASSERT((bp->b_flags&(XBF_DELWRI|XBF_ASYNC)) == (XBF_DELWRI|XBF_ASYNC)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1616 | |
David Chinner | a6867a6 | 2006-01-11 15:37:58 +1100 | [diff] [blame] | 1617 | spin_lock(dwlk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | /* If already in the queue, dequeue and place at tail */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1619 | if (!list_empty(&bp->b_list)) { |
| 1620 | ASSERT(bp->b_flags & _XBF_DELWRI_Q); |
| 1621 | if (unlock) |
| 1622 | atomic_dec(&bp->b_hold); |
| 1623 | list_del(&bp->b_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | } |
| 1625 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1626 | bp->b_flags |= _XBF_DELWRI_Q; |
| 1627 | list_add_tail(&bp->b_list, dwq); |
| 1628 | bp->b_queuetime = jiffies; |
David Chinner | a6867a6 | 2006-01-11 15:37:58 +1100 | [diff] [blame] | 1629 | spin_unlock(dwlk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | |
| 1631 | if (unlock) |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1632 | xfs_buf_unlock(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | } |
| 1634 | |
| 1635 | void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1636 | xfs_buf_delwri_dequeue( |
| 1637 | xfs_buf_t *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1639 | spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | int dequeued = 0; |
| 1641 | |
David Chinner | a6867a6 | 2006-01-11 15:37:58 +1100 | [diff] [blame] | 1642 | spin_lock(dwlk); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1643 | if ((bp->b_flags & XBF_DELWRI) && !list_empty(&bp->b_list)) { |
| 1644 | ASSERT(bp->b_flags & _XBF_DELWRI_Q); |
| 1645 | list_del_init(&bp->b_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 | dequeued = 1; |
| 1647 | } |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1648 | bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q); |
David Chinner | a6867a6 | 2006-01-11 15:37:58 +1100 | [diff] [blame] | 1649 | spin_unlock(dwlk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1650 | |
| 1651 | if (dequeued) |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1652 | xfs_buf_rele(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1654 | XB_TRACE(bp, "delwri_dq", (long)dequeued); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | } |
| 1656 | |
| 1657 | STATIC void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1658 | xfs_buf_runall_queues( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | struct workqueue_struct *queue) |
| 1660 | { |
| 1661 | flush_workqueue(queue); |
| 1662 | } |
| 1663 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | STATIC int |
Christoph Hellwig | 23ea403 | 2005-06-21 15:14:01 +1000 | [diff] [blame] | 1665 | xfsbufd_wakeup( |
Nathan Scott | 15c84a4 | 2005-11-04 10:51:01 +1100 | [diff] [blame] | 1666 | int priority, |
| 1667 | gfp_t mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | { |
Christoph Hellwig | da7f93e | 2006-01-11 20:49:57 +1100 | [diff] [blame] | 1669 | xfs_buftarg_t *btp; |
David Chinner | a6867a6 | 2006-01-11 15:37:58 +1100 | [diff] [blame] | 1670 | |
| 1671 | spin_lock(&xfs_buftarg_lock); |
Christoph Hellwig | da7f93e | 2006-01-11 20:49:57 +1100 | [diff] [blame] | 1672 | list_for_each_entry(btp, &xfs_buftarg_list, bt_list) { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1673 | if (test_bit(XBT_FORCE_SLEEP, &btp->bt_flags)) |
David Chinner | a6867a6 | 2006-01-11 15:37:58 +1100 | [diff] [blame] | 1674 | continue; |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1675 | set_bit(XBT_FORCE_FLUSH, &btp->bt_flags); |
David Chinner | a6867a6 | 2006-01-11 15:37:58 +1100 | [diff] [blame] | 1676 | wake_up_process(btp->bt_task); |
| 1677 | } |
| 1678 | spin_unlock(&xfs_buftarg_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | return 0; |
| 1680 | } |
| 1681 | |
David Chinner | 585e6d8 | 2007-02-10 18:32:29 +1100 | [diff] [blame] | 1682 | /* |
| 1683 | * Move as many buffers as specified to the supplied list |
| 1684 | * idicating if we skipped any buffers to prevent deadlocks. |
| 1685 | */ |
| 1686 | STATIC int |
| 1687 | xfs_buf_delwri_split( |
| 1688 | xfs_buftarg_t *target, |
| 1689 | struct list_head *list, |
David Chinner | 5e6a07d | 2007-02-10 18:34:49 +1100 | [diff] [blame] | 1690 | unsigned long age) |
David Chinner | 585e6d8 | 2007-02-10 18:32:29 +1100 | [diff] [blame] | 1691 | { |
| 1692 | xfs_buf_t *bp, *n; |
| 1693 | struct list_head *dwq = &target->bt_delwrite_queue; |
| 1694 | spinlock_t *dwlk = &target->bt_delwrite_lock; |
| 1695 | int skipped = 0; |
David Chinner | 5e6a07d | 2007-02-10 18:34:49 +1100 | [diff] [blame] | 1696 | int force; |
David Chinner | 585e6d8 | 2007-02-10 18:32:29 +1100 | [diff] [blame] | 1697 | |
David Chinner | 5e6a07d | 2007-02-10 18:34:49 +1100 | [diff] [blame] | 1698 | force = test_and_clear_bit(XBT_FORCE_FLUSH, &target->bt_flags); |
David Chinner | 585e6d8 | 2007-02-10 18:32:29 +1100 | [diff] [blame] | 1699 | INIT_LIST_HEAD(list); |
| 1700 | spin_lock(dwlk); |
| 1701 | list_for_each_entry_safe(bp, n, dwq, b_list) { |
| 1702 | XB_TRACE(bp, "walkq1", (long)xfs_buf_ispin(bp)); |
| 1703 | ASSERT(bp->b_flags & XBF_DELWRI); |
| 1704 | |
| 1705 | if (!xfs_buf_ispin(bp) && !xfs_buf_cond_lock(bp)) { |
David Chinner | 5e6a07d | 2007-02-10 18:34:49 +1100 | [diff] [blame] | 1706 | if (!force && |
David Chinner | 585e6d8 | 2007-02-10 18:32:29 +1100 | [diff] [blame] | 1707 | time_before(jiffies, bp->b_queuetime + age)) { |
| 1708 | xfs_buf_unlock(bp); |
| 1709 | break; |
| 1710 | } |
| 1711 | |
| 1712 | bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q| |
| 1713 | _XBF_RUN_QUEUES); |
| 1714 | bp->b_flags |= XBF_WRITE; |
| 1715 | list_move_tail(&bp->b_list, list); |
| 1716 | } else |
| 1717 | skipped++; |
| 1718 | } |
| 1719 | spin_unlock(dwlk); |
| 1720 | |
| 1721 | return skipped; |
| 1722 | |
| 1723 | } |
| 1724 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | STATIC int |
Christoph Hellwig | 23ea403 | 2005-06-21 15:14:01 +1000 | [diff] [blame] | 1726 | xfsbufd( |
David Chinner | 585e6d8 | 2007-02-10 18:32:29 +1100 | [diff] [blame] | 1727 | void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1728 | { |
David Chinner | 585e6d8 | 2007-02-10 18:32:29 +1100 | [diff] [blame] | 1729 | struct list_head tmp; |
| 1730 | xfs_buftarg_t *target = (xfs_buftarg_t *)data; |
| 1731 | int count; |
| 1732 | xfs_buf_t *bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | current->flags |= PF_MEMALLOC; |
| 1735 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1736 | do { |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 1737 | if (unlikely(freezing(current))) { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1738 | set_bit(XBT_FORCE_SLEEP, &target->bt_flags); |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 1739 | refrigerator(); |
Nathan Scott | abd0cf7 | 2005-05-05 13:30:13 -0700 | [diff] [blame] | 1740 | } else { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1741 | clear_bit(XBT_FORCE_SLEEP, &target->bt_flags); |
Nathan Scott | abd0cf7 | 2005-05-05 13:30:13 -0700 | [diff] [blame] | 1742 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | |
Nathan Scott | 15c84a4 | 2005-11-04 10:51:01 +1100 | [diff] [blame] | 1744 | schedule_timeout_interruptible( |
| 1745 | xfs_buf_timer_centisecs * msecs_to_jiffies(10)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | |
David Chinner | 585e6d8 | 2007-02-10 18:32:29 +1100 | [diff] [blame] | 1747 | xfs_buf_delwri_split(target, &tmp, |
David Chinner | 5e6a07d | 2007-02-10 18:34:49 +1100 | [diff] [blame] | 1748 | xfs_buf_age_centisecs * msecs_to_jiffies(10)); |
David Chinner | 585e6d8 | 2007-02-10 18:32:29 +1100 | [diff] [blame] | 1749 | |
Nathan Scott | f07c225 | 2006-09-28 10:52:15 +1000 | [diff] [blame] | 1750 | count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1751 | while (!list_empty(&tmp)) { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1752 | bp = list_entry(tmp.next, xfs_buf_t, b_list); |
| 1753 | ASSERT(target == bp->b_target); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1755 | list_del_init(&bp->b_list); |
| 1756 | xfs_buf_iostrategy(bp); |
David Chinner | 585e6d8 | 2007-02-10 18:32:29 +1100 | [diff] [blame] | 1757 | count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1758 | } |
| 1759 | |
| 1760 | if (as_list_len > 0) |
| 1761 | purge_addresses(); |
Nathan Scott | f07c225 | 2006-09-28 10:52:15 +1000 | [diff] [blame] | 1762 | if (count) |
| 1763 | blk_run_address_space(target->bt_mapping); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1764 | |
Christoph Hellwig | 4df08c5 | 2005-09-05 08:34:18 +1000 | [diff] [blame] | 1765 | } while (!kthread_should_stop()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | |
Christoph Hellwig | 4df08c5 | 2005-09-05 08:34:18 +1000 | [diff] [blame] | 1767 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1768 | } |
| 1769 | |
| 1770 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1771 | * Go through all incore buffers, and release buffers if they belong to |
| 1772 | * the given device. This is used in filesystem error handling to |
| 1773 | * preserve the consistency of its metadata. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | */ |
| 1775 | int |
| 1776 | xfs_flush_buftarg( |
David Chinner | 585e6d8 | 2007-02-10 18:32:29 +1100 | [diff] [blame] | 1777 | xfs_buftarg_t *target, |
| 1778 | int wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1779 | { |
David Chinner | 585e6d8 | 2007-02-10 18:32:29 +1100 | [diff] [blame] | 1780 | struct list_head tmp; |
| 1781 | xfs_buf_t *bp, *n; |
| 1782 | int pincount = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1783 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1784 | xfs_buf_runall_queues(xfsdatad_workqueue); |
| 1785 | xfs_buf_runall_queues(xfslogd_workqueue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1786 | |
David Chinner | 5e6a07d | 2007-02-10 18:34:49 +1100 | [diff] [blame] | 1787 | set_bit(XBT_FORCE_FLUSH, &target->bt_flags); |
| 1788 | pincount = xfs_buf_delwri_split(target, &tmp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | |
| 1790 | /* |
| 1791 | * Dropped the delayed write list lock, now walk the temporary list |
| 1792 | */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1793 | list_for_each_entry_safe(bp, n, &tmp, b_list) { |
David Chinner | 585e6d8 | 2007-02-10 18:32:29 +1100 | [diff] [blame] | 1794 | ASSERT(target == bp->b_target); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1795 | if (wait) |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1796 | bp->b_flags &= ~XBF_ASYNC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | else |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1798 | list_del_init(&bp->b_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1800 | xfs_buf_iostrategy(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1801 | } |
| 1802 | |
Nathan Scott | f07c225 | 2006-09-28 10:52:15 +1000 | [diff] [blame] | 1803 | if (wait) |
| 1804 | blk_run_address_space(target->bt_mapping); |
| 1805 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1806 | /* |
| 1807 | * Remaining list items must be flushed before returning |
| 1808 | */ |
| 1809 | while (!list_empty(&tmp)) { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1810 | bp = list_entry(tmp.next, xfs_buf_t, b_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1811 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1812 | list_del_init(&bp->b_list); |
| 1813 | xfs_iowait(bp); |
| 1814 | xfs_buf_relse(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | } |
| 1816 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | return pincount; |
| 1818 | } |
| 1819 | |
Christoph Hellwig | 04d8b28 | 2005-11-02 10:15:05 +1100 | [diff] [blame] | 1820 | int __init |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1821 | xfs_buf_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1822 | { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1823 | #ifdef XFS_BUF_TRACE |
| 1824 | xfs_buf_trace_buf = ktrace_alloc(XFS_BUF_TRACE_SIZE, KM_SLEEP); |
Christoph Hellwig | 04d8b28 | 2005-11-02 10:15:05 +1100 | [diff] [blame] | 1825 | #endif |
| 1826 | |
Nathan Scott | 8758280 | 2006-03-14 13:18:19 +1100 | [diff] [blame] | 1827 | xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf", |
| 1828 | KM_ZONE_HWALIGN, NULL); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1829 | if (!xfs_buf_zone) |
Christoph Hellwig | 04d8b28 | 2005-11-02 10:15:05 +1100 | [diff] [blame] | 1830 | goto out_free_trace_buf; |
| 1831 | |
Rafael J. Wysocki | b433769 | 2007-03-22 00:11:27 -0800 | [diff] [blame] | 1832 | xfslogd_workqueue = create_workqueue("xfslogd"); |
Christoph Hellwig | 23ea403 | 2005-06-21 15:14:01 +1000 | [diff] [blame] | 1833 | if (!xfslogd_workqueue) |
Christoph Hellwig | 04d8b28 | 2005-11-02 10:15:05 +1100 | [diff] [blame] | 1834 | goto out_free_buf_zone; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1835 | |
Rafael J. Wysocki | b433769 | 2007-03-22 00:11:27 -0800 | [diff] [blame] | 1836 | xfsdatad_workqueue = create_workqueue("xfsdatad"); |
Christoph Hellwig | 23ea403 | 2005-06-21 15:14:01 +1000 | [diff] [blame] | 1837 | if (!xfsdatad_workqueue) |
| 1838 | goto out_destroy_xfslogd_workqueue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1839 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1840 | xfs_buf_shake = kmem_shake_register(xfsbufd_wakeup); |
| 1841 | if (!xfs_buf_shake) |
David Chinner | a6867a6 | 2006-01-11 15:37:58 +1100 | [diff] [blame] | 1842 | goto out_destroy_xfsdatad_workqueue; |
Christoph Hellwig | 04d8b28 | 2005-11-02 10:15:05 +1100 | [diff] [blame] | 1843 | |
Christoph Hellwig | 23ea403 | 2005-06-21 15:14:01 +1000 | [diff] [blame] | 1844 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1845 | |
Christoph Hellwig | 23ea403 | 2005-06-21 15:14:01 +1000 | [diff] [blame] | 1846 | out_destroy_xfsdatad_workqueue: |
| 1847 | destroy_workqueue(xfsdatad_workqueue); |
| 1848 | out_destroy_xfslogd_workqueue: |
| 1849 | destroy_workqueue(xfslogd_workqueue); |
Christoph Hellwig | 23ea403 | 2005-06-21 15:14:01 +1000 | [diff] [blame] | 1850 | out_free_buf_zone: |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1851 | kmem_zone_destroy(xfs_buf_zone); |
Christoph Hellwig | 04d8b28 | 2005-11-02 10:15:05 +1100 | [diff] [blame] | 1852 | out_free_trace_buf: |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1853 | #ifdef XFS_BUF_TRACE |
| 1854 | ktrace_free(xfs_buf_trace_buf); |
Christoph Hellwig | 23ea403 | 2005-06-21 15:14:01 +1000 | [diff] [blame] | 1855 | #endif |
Nathan Scott | 8758280 | 2006-03-14 13:18:19 +1100 | [diff] [blame] | 1856 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | } |
| 1858 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1860 | xfs_buf_terminate(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1862 | kmem_shake_deregister(xfs_buf_shake); |
Christoph Hellwig | 04d8b28 | 2005-11-02 10:15:05 +1100 | [diff] [blame] | 1863 | destroy_workqueue(xfsdatad_workqueue); |
| 1864 | destroy_workqueue(xfslogd_workqueue); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1865 | kmem_zone_destroy(xfs_buf_zone); |
| 1866 | #ifdef XFS_BUF_TRACE |
| 1867 | ktrace_free(xfs_buf_trace_buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1869 | } |
Tim Shimmin | e6a0e9c | 2007-05-08 13:49:59 +1000 | [diff] [blame^] | 1870 | |
| 1871 | #ifdef CONFIG_KDB_MODULES |
| 1872 | struct list_head * |
| 1873 | xfs_get_buftarg_list(void) |
| 1874 | { |
| 1875 | return &xfs_buftarg_list; |
| 1876 | } |
| 1877 | #endif |