blob: b971237c5a9fe41ef7e3310d3809e1437341401d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scottf07c2252006-09-28 10:52:15 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * 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 Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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 Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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 Torvalds1da177e2005-04-16 15:20:36 -070017 */
Vlad Apostolov93c189c2006-11-11 18:03:49 +110018#include "xfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#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 Hellwig4df08c52005-09-05 08:34:18 +100032#include <linux/kthread.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080033#include <linux/migrate.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070034#include <linux/backing-dev.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080035#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Nathan Scottce8e9222006-01-11 15:39:08 +110037STATIC kmem_zone_t *xfs_buf_zone;
38STATIC kmem_shaker_t xfs_buf_shake;
David Chinnera6867a62006-01-11 15:37:58 +110039STATIC int xfsbufd(void *);
Al Viro27496a82005-10-21 03:20:48 -040040STATIC int xfsbufd_wakeup(int, gfp_t);
Nathan Scottce8e9222006-01-11 15:39:08 +110041STATIC void xfs_buf_delwri_queue(xfs_buf_t *, int);
Christoph Hellwig23ea4032005-06-21 15:14:01 +100042
43STATIC struct workqueue_struct *xfslogd_workqueue;
Christoph Hellwig0829c362005-09-02 16:58:49 +100044struct workqueue_struct *xfsdatad_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Nathan Scottce8e9222006-01-11 15:39:08 +110046#ifdef XFS_BUF_TRACE
Linus Torvalds1da177e2005-04-16 15:20:36 -070047void
Nathan Scottce8e9222006-01-11 15:39:08 +110048xfs_buf_trace(
49 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 char *id,
51 void *data,
52 void *ra)
53{
Nathan Scottce8e9222006-01-11 15:39:08 +110054 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 Torvalds1da177e2005-04-16 15:20:36 -070059 (void *)current,
60 data, ra,
Nathan Scottce8e9222006-01-11 15:39:08 +110061 (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 Torvalds1da177e2005-04-16 15:20:36 -070064 NULL, NULL, NULL, NULL, NULL);
65}
Nathan Scottce8e9222006-01-11 15:39:08 +110066ktrace_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 Torvalds1da177e2005-04-16 15:20:36 -070070#else
Nathan Scottce8e9222006-01-11 15:39:08 +110071#define XB_TRACE(bp, id, data) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#endif
73
Nathan Scottce8e9222006-01-11 15:39:08 +110074#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 Torvalds1da177e2005-04-16 15:20:36 -070078#else
Nathan Scottce8e9222006-01-11 15:39:08 +110079# 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 Torvalds1da177e2005-04-16 15:20:36 -070082#endif
83
Nathan Scottce8e9222006-01-11 15:39:08 +110084#define xb_to_gfp(flags) \
85 ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : \
86 ((flags) & XBF_DONT_BLOCK) ? GFP_NOFS : GFP_KERNEL) | __GFP_NOWARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Nathan Scottce8e9222006-01-11 15:39:08 +110088#define xb_to_km(flags) \
89 (((flags) & XBF_DONT_BLOCK) ? KM_NOFS : KM_SLEEP)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Nathan Scottce8e9222006-01-11 15:39:08 +110091#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 Torvalds1da177e2005-04-16 15:20:36 -070095
96/*
Nathan Scottce8e9222006-01-11 15:39:08 +110097 * Page Region interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 *
Nathan Scottce8e9222006-01-11 15:39:08 +110099 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700102 *
Nathan Scottce8e9222006-01-11 15:39:08 +1100103 * Each such region is "bytes per page / bits per long" bytes long.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 *
Nathan Scottce8e9222006-01-11 15:39:08 +1100105 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700108 */
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
120STATIC unsigned long
121page_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
142STATIC inline void
143set_page_region(
144 struct page *page,
145 size_t offset,
146 size_t length)
147{
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700148 set_page_private(page,
149 page_private(page) | page_region_mask(offset, length));
150 if (page_private(page) == ~0UL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 SetPageUptodate(page);
152}
153
154STATIC inline int
155test_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 Dickins4c21e2f2005-10-29 18:16:40 -0700162 return (mask && (page_private(page) & mask) == mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
165/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100166 * Mapping of multi-page buffers into contiguous virtual space
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 */
168
169typedef struct a_list {
170 void *vm_addr;
171 struct a_list *next;
172} a_list_t;
173
174STATIC a_list_t *as_free_head;
175STATIC int as_list_len;
176STATIC DEFINE_SPINLOCK(as_lock);
177
178/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100179 * Try to batch vunmaps because they are costly.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 */
181STATIC void
182free_address(
183 void *addr)
184{
185 a_list_t *aentry;
186
Jeff Dike7b04d712006-04-10 22:53:27 -0700187 aentry = kmalloc(sizeof(a_list_t), GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 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
200STATIC void
201purge_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 Scottce8e9222006-01-11 15:39:08 +1100222 * Internal xfs_buf_t object manipulation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 */
224
225STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100226_xfs_buf_initialize(
227 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100229 xfs_off_t range_base,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 size_t range_length,
Nathan Scottce8e9222006-01-11 15:39:08 +1100231 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 /*
Nathan Scottce8e9222006-01-11 15:39:08 +1100234 * We don't want certain flags to appear in b_flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100236 flags &= ~(XBF_LOCK|XBF_MAPPED|XBF_DONT_BLOCK|XBF_READ_AHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Nathan Scottce8e9222006-01-11 15:39:08 +1100238 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 Torvalds1da177e2005-04-16 15:20:36 -0700247 /*
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 Scottce8e9222006-01-11 15:39:08 +1100252 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 Torvalds1da177e2005-04-16 15:20:36 -0700257
Nathan Scottce8e9222006-01-11 15:39:08 +1100258 XFS_STATS_INC(xb_create);
259 XB_TRACE(bp, "initialize", target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
262/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100263 * Allocate a page array capable of holding a specified number
264 * of pages, and point the page buf at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 */
266STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100267_xfs_buf_get_pages(
268 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 int page_count,
Nathan Scottce8e9222006-01-11 15:39:08 +1100270 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
272 /* Make sure that we have a page list */
Nathan Scottce8e9222006-01-11 15:39:08 +1100273 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 Torvalds1da177e2005-04-16 15:20:36 -0700278 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100279 bp->b_pages = kmem_alloc(sizeof(struct page *) *
280 page_count, xb_to_km(flags));
281 if (bp->b_pages == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return -ENOMEM;
283 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100284 memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286 return 0;
287}
288
289/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100290 * Frees b_pages if it was allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 */
292STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100293_xfs_buf_free_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 xfs_buf_t *bp)
295{
Nathan Scottce8e9222006-01-11 15:39:08 +1100296 if (bp->b_pages != bp->b_page_array) {
297 kmem_free(bp->b_pages,
298 bp->b_page_count * sizeof(struct page *));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300}
301
302/*
303 * Releases the specified buffer.
304 *
305 * The modification state of any associated pages is left unchanged.
Nathan Scottce8e9222006-01-11 15:39:08 +1100306 * The buffer most not be on any hash - use xfs_buf_rele instead for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 * hashed and refcounted buffers
308 */
309void
Nathan Scottce8e9222006-01-11 15:39:08 +1100310xfs_buf_free(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 xfs_buf_t *bp)
312{
Nathan Scottce8e9222006-01-11 15:39:08 +1100313 XB_TRACE(bp, "free", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Nathan Scottce8e9222006-01-11 15:39:08 +1100315 ASSERT(list_empty(&bp->b_hash_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Nathan Scottce8e9222006-01-11 15:39:08 +1100317 if (bp->b_flags & _XBF_PAGE_CACHE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 uint i;
319
Nathan Scottce8e9222006-01-11 15:39:08 +1100320 if ((bp->b_flags & XBF_MAPPED) && (bp->b_page_count > 1))
321 free_address(bp->b_addr - bp->b_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Nathan Scott948ecdb2006-09-28 11:03:13 +1000323 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 Scottce8e9222006-01-11 15:39:08 +1100329 _xfs_buf_free_pages(bp);
330 } else if (bp->b_flags & _XBF_KMEM_ALLOC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 /*
Nathan Scottce8e9222006-01-11 15:39:08 +1100332 * XXX(hch): bp->b_count_desired might be incorrect (see
333 * xfs_buf_associate_memory for details), but fortunately
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 * the Linux version of kmem_free ignores the len argument..
335 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100336 kmem_free(bp->b_addr, bp->b_count_desired);
337 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339
Nathan Scottce8e9222006-01-11 15:39:08 +1100340 xfs_buf_deallocate(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
343/*
344 * Finds all pages for buffer in question and builds it's page list.
345 */
346STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100347_xfs_buf_lookup_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 xfs_buf_t *bp,
349 uint flags)
350{
Nathan Scottce8e9222006-01-11 15:39:08 +1100351 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 Torvalds1da177e2005-04-16 15:20:36 -0700354 size_t nbytes, offset;
Nathan Scottce8e9222006-01-11 15:39:08 +1100355 gfp_t gfp_mask = xb_to_gfp(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 unsigned short page_count, i;
357 pgoff_t first;
Nathan Scott204ab252006-01-11 20:50:22 +1100358 xfs_off_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 int error;
360
Nathan Scottce8e9222006-01-11 15:39:08 +1100361 end = bp->b_file_offset + bp->b_buffer_length;
362 page_count = xfs_buf_btoc(end) - xfs_buf_btoct(bp->b_file_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Nathan Scottce8e9222006-01-11 15:39:08 +1100364 error = _xfs_buf_get_pages(bp, page_count, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (unlikely(error))
366 return error;
Nathan Scottce8e9222006-01-11 15:39:08 +1100367 bp->b_flags |= _XBF_PAGE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Nathan Scottce8e9222006-01-11 15:39:08 +1100369 offset = bp->b_offset;
370 first = bp->b_file_offset >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Nathan Scottce8e9222006-01-11 15:39:08 +1100372 for (i = 0; i < bp->b_page_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 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 Scottce8e9222006-01-11 15:39:08 +1100379 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 Torvalds1da177e2005-04-16 15:20:36 -0700383 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 Scottce8e9222006-01-11 15:39:08 +1100398 XFS_STATS_INC(xb_page_retries);
Christoph Hellwig23ea4032005-06-21 15:14:01 +1000399 xfsbufd_wakeup(0, gfp_mask);
Andrew Morton3fcfab12006-10-19 23:28:16 -0700400 congestion_wait(WRITE, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 goto retry;
402 }
403
Nathan Scottce8e9222006-01-11 15:39:08 +1100404 XFS_STATS_INC(xb_page_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 nbytes = min_t(size_t, size, PAGE_CACHE_SIZE - offset);
407 size -= nbytes;
408
Nathan Scott948ecdb2006-09-28 11:03:13 +1000409 ASSERT(!PagePrivate(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (!PageUptodate(page)) {
411 page_count--;
412 if (blocksize >= PAGE_CACHE_SIZE) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100413 if (flags & XBF_READ)
414 bp->b_locked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 } else if (!PagePrivate(page)) {
416 if (test_page_region(page, offset, nbytes))
417 page_count++;
418 }
419 }
420
Nathan Scottce8e9222006-01-11 15:39:08 +1100421 bp->b_pages[i] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 offset = 0;
423 }
424
Nathan Scottce8e9222006-01-11 15:39:08 +1100425 if (!bp->b_locked) {
426 for (i = 0; i < bp->b_page_count; i++)
427 unlock_page(bp->b_pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429
Nathan Scottce8e9222006-01-11 15:39:08 +1100430 if (page_count == bp->b_page_count)
431 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Nathan Scottce8e9222006-01-11 15:39:08 +1100433 XB_TRACE(bp, "lookup_pages", (long)page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return error;
435}
436
437/*
438 * Map buffer into kernel address-space if nessecary.
439 */
440STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100441_xfs_buf_map_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 xfs_buf_t *bp,
443 uint flags)
444{
445 /* A single page buffer is always mappable */
Nathan Scottce8e9222006-01-11 15:39:08 +1100446 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 Torvalds1da177e2005-04-16 15:20:36 -0700450 if (as_list_len > 64)
451 purge_addresses();
Nathan Scottce8e9222006-01-11 15:39:08 +1100452 bp->b_addr = vmap(bp->b_pages, bp->b_page_count,
453 VM_MAP, PAGE_KERNEL);
454 if (unlikely(bp->b_addr == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return -ENOMEM;
Nathan Scottce8e9222006-01-11 15:39:08 +1100456 bp->b_addr += bp->b_offset;
457 bp->b_flags |= XBF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
459
460 return 0;
461}
462
463/*
464 * Finding and Reading Buffers
465 */
466
467/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100468 * Look up, and creates if absent, a lockable buffer for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 * 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 */
475xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100476_xfs_buf_find(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 xfs_buftarg_t *btp, /* block device target */
Nathan Scott204ab252006-01-11 20:50:22 +1100478 xfs_off_t ioff, /* starting offset of range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 size_t isize, /* length of range */
Nathan Scottce8e9222006-01-11 15:39:08 +1100480 xfs_buf_flags_t flags,
481 xfs_buf_t *new_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Nathan Scott204ab252006-01-11 20:50:22 +1100483 xfs_off_t range_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 size_t range_length;
485 xfs_bufhash_t *hash;
Nathan Scottce8e9222006-01-11 15:39:08 +1100486 xfs_buf_t *bp, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 range_base = (ioff << BBSHIFT);
489 range_length = (isize << BBSHIFT);
490
491 /* Check for IOs smaller than the sector size / not sector aligned */
Nathan Scottce8e9222006-01-11 15:39:08 +1100492 ASSERT(!(range_length < (1 << btp->bt_sshift)));
Nathan Scott204ab252006-01-11 20:50:22 +1100493 ASSERT(!(range_base & (xfs_off_t)btp->bt_smask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 hash = &btp->bt_hash[hash_long((unsigned long)ioff, btp->bt_hashshift)];
496
497 spin_lock(&hash->bh_lock);
498
Nathan Scottce8e9222006-01-11 15:39:08 +1100499 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 Torvalds1da177e2005-04-16 15:20:36 -0700503 /*
Nathan Scottce8e9222006-01-11 15:39:08 +1100504 * If we look at something, bring it to the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 * front of the list for next time.
506 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100507 atomic_inc(&bp->b_hold);
508 list_move(&bp->b_hash_list, &hash->bh_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 goto found;
510 }
511 }
512
513 /* No match found */
Nathan Scottce8e9222006-01-11 15:39:08 +1100514 if (new_bp) {
515 _xfs_buf_initialize(new_bp, btp, range_base,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 range_length, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100517 new_bp->b_hash = hash;
518 list_add(&new_bp->b_hash_list, &hash->bh_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100520 XFS_STATS_INC(xb_miss_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
522
523 spin_unlock(&hash->bh_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +1100524 return new_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526found:
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 Scottce8e9222006-01-11 15:39:08 +1100533 if (down_trylock(&bp->b_sema)) {
534 if (!(flags & XBF_TRYLOCK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 /* wait for buffer ownership */
Nathan Scottce8e9222006-01-11 15:39:08 +1100536 XB_TRACE(bp, "get_lock", 0);
537 xfs_buf_lock(bp);
538 XFS_STATS_INC(xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 } else {
540 /* We asked for a trylock and failed, no need
541 * to look at file offset and length here, we
Nathan Scottce8e9222006-01-11 15:39:08 +1100542 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700545 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100546 xfs_buf_rele(bp);
547 XFS_STATS_INC(xb_busy_locked);
548 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
550 } else {
551 /* trylock worked */
Nathan Scottce8e9222006-01-11 15:39:08 +1100552 XB_SET_OWNER(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
554
Nathan Scottce8e9222006-01-11 15:39:08 +1100555 if (bp->b_flags & XBF_STALE) {
556 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
557 bp->b_flags &= XBF_MAPPED;
David Chinner2f926582005-09-05 08:33:35 +1000558 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100559 XB_TRACE(bp, "got_lock", 0);
560 XFS_STATS_INC(xb_get_locked);
561 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
564/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100565 * Assembles a buffer covering the specified range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 * Storage in memory for all portions of the buffer will be allocated,
567 * although backing storage may not be.
568 */
569xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100570xfs_buf_get_flags(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 xfs_buftarg_t *target,/* target for buffer */
Nathan Scott204ab252006-01-11 20:50:22 +1100572 xfs_off_t ioff, /* starting offset of range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 size_t isize, /* length of range */
Nathan Scottce8e9222006-01-11 15:39:08 +1100574 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Nathan Scottce8e9222006-01-11 15:39:08 +1100576 xfs_buf_t *bp, *new_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 int error = 0, i;
578
Nathan Scottce8e9222006-01-11 15:39:08 +1100579 new_bp = xfs_buf_allocate(flags);
580 if (unlikely(!new_bp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return NULL;
582
Nathan Scottce8e9222006-01-11 15:39:08 +1100583 bp = _xfs_buf_find(target, ioff, isize, flags, new_bp);
584 if (bp == new_bp) {
585 error = _xfs_buf_lookup_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (error)
587 goto no_buffer;
588 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100589 xfs_buf_deallocate(new_bp);
590 if (unlikely(bp == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return NULL;
592 }
593
Nathan Scottce8e9222006-01-11 15:39:08 +1100594 for (i = 0; i < bp->b_page_count; i++)
595 mark_page_accessed(bp->b_pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Nathan Scottce8e9222006-01-11 15:39:08 +1100597 if (!(bp->b_flags & XBF_MAPPED)) {
598 error = _xfs_buf_map_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (unlikely(error)) {
600 printk(KERN_WARNING "%s: failed to map pages\n",
601 __FUNCTION__);
602 goto no_buffer;
603 }
604 }
605
Nathan Scottce8e9222006-01-11 15:39:08 +1100606 XFS_STATS_INC(xb_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 /*
609 * Always fill in the block number now, the mapped cases can do
610 * their own overlay of this later.
611 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100612 bp->b_bn = ioff;
613 bp->b_count_desired = bp->b_buffer_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Nathan Scottce8e9222006-01-11 15:39:08 +1100615 XB_TRACE(bp, "get", (unsigned long)flags);
616 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100619 if (flags & (XBF_LOCK | XBF_TRYLOCK))
620 xfs_buf_unlock(bp);
621 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return NULL;
623}
624
625xfs_buf_t *
626xfs_buf_read_flags(
627 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100628 xfs_off_t ioff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 size_t isize,
Nathan Scottce8e9222006-01-11 15:39:08 +1100630 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Nathan Scottce8e9222006-01-11 15:39:08 +1100632 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Nathan Scottce8e9222006-01-11 15:39:08 +1100634 flags |= XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Nathan Scottce8e9222006-01-11 15:39:08 +1100636 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 Torvalds1da177e2005-04-16 15:20:36 -0700644 /*
645 * Read ahead call which is already satisfied,
646 * drop the buffer
647 */
648 goto no_buffer;
649 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100650 XB_TRACE(bp, "read_done", (unsigned long)flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 /* We do not want read in the flags */
Nathan Scottce8e9222006-01-11 15:39:08 +1100652 bp->b_flags &= ~XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
654 }
655
Nathan Scottce8e9222006-01-11 15:39:08 +1100656 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100659 if (flags & (XBF_LOCK | XBF_TRYLOCK))
660 xfs_buf_unlock(bp);
661 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return NULL;
663}
664
665/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100666 * If we are not low on memory then do the readahead in a deadlock
667 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 */
669void
Nathan Scottce8e9222006-01-11 15:39:08 +1100670xfs_buf_readahead(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100672 xfs_off_t ioff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 size_t isize,
Nathan Scottce8e9222006-01-11 15:39:08 +1100674 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
676 struct backing_dev_info *bdi;
677
Nathan Scottce8e9222006-01-11 15:39:08 +1100678 bdi = target->bt_mapping->backing_dev_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (bdi_read_congested(bdi))
680 return;
681
Nathan Scottce8e9222006-01-11 15:39:08 +1100682 flags |= (XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 xfs_buf_read_flags(target, ioff, isize, flags);
684}
685
686xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100687xfs_buf_get_empty(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 size_t len,
689 xfs_buftarg_t *target)
690{
Nathan Scottce8e9222006-01-11 15:39:08 +1100691 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Nathan Scottce8e9222006-01-11 15:39:08 +1100693 bp = xfs_buf_allocate(0);
694 if (bp)
695 _xfs_buf_initialize(bp, target, 0, len, 0);
696 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
699static inline struct page *
700mem_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
711int
Nathan Scottce8e9222006-01-11 15:39:08 +1100712xfs_buf_associate_memory(
713 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 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 Scottce8e9222006-01-11 15:39:08 +1100730 if (bp->b_pages)
731 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Nathan Scottce8e9222006-01-11 15:39:08 +1100733 bp->b_pages = NULL;
734 bp->b_addr = mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Nathan Scottce8e9222006-01-11 15:39:08 +1100736 rval = _xfs_buf_get_pages(bp, page_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (rval)
738 return rval;
739
Nathan Scottce8e9222006-01-11 15:39:08 +1100740 bp->b_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 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 Scottce8e9222006-01-11 15:39:08 +1100745 bp->b_pages[0] = mem_to_page(mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 ptr += PAGE_CACHE_SIZE;
Nathan Scottce8e9222006-01-11 15:39:08 +1100748 bp->b_page_count = ++i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 while (ptr < end) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100750 bp->b_pages[i] = mem_to_page((void *)ptr);
751 bp->b_page_count = ++i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 ptr += PAGE_CACHE_SIZE;
753 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100754 bp->b_locked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Nathan Scottce8e9222006-01-11 15:39:08 +1100756 bp->b_count_desired = bp->b_buffer_length = len;
757 bp->b_flags |= XBF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 return 0;
760}
761
762xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100763xfs_buf_get_noaddr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 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 Scottce8e9222006-01-11 15:39:08 +1100772 bp = xfs_buf_allocate(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (unlikely(bp == NULL))
774 goto fail;
Nathan Scottce8e9222006-01-11 15:39:08 +1100775 _xfs_buf_initialize(bp, target, 0, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 try_again:
Nathan Scottefb8ad72006-09-28 11:03:05 +1000778 data = kmem_alloc(malloc_len, KM_SLEEP | KM_MAYFAIL | KM_LARGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 if (unlikely(data == NULL))
780 goto fail_free_buf;
781
782 /* check whether alignment matches.. */
783 if ((__psunsigned_t)data !=
Nathan Scottce8e9222006-01-11 15:39:08 +1100784 ((__psunsigned_t)data & ~target->bt_smask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 /* .. else double the size and try again */
786 kmem_free(data, malloc_len);
787 malloc_len <<= 1;
788 goto try_again;
789 }
790
Nathan Scottce8e9222006-01-11 15:39:08 +1100791 error = xfs_buf_associate_memory(bp, data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (error)
793 goto fail_free_mem;
Nathan Scottce8e9222006-01-11 15:39:08 +1100794 bp->b_flags |= _XBF_KMEM_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Nathan Scottce8e9222006-01-11 15:39:08 +1100796 xfs_buf_unlock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Nathan Scottce8e9222006-01-11 15:39:08 +1100798 XB_TRACE(bp, "no_daddr", data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 return bp;
800 fail_free_mem:
801 kmem_free(data, malloc_len);
802 fail_free_buf:
Nathan Scottce8e9222006-01-11 15:39:08 +1100803 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 fail:
805 return NULL;
806}
807
808/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 * Increment reference count on buffer, to hold the buffer concurrently
810 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 * Must hold the buffer already to call this function.
812 */
813void
Nathan Scottce8e9222006-01-11 15:39:08 +1100814xfs_buf_hold(
815 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Nathan Scottce8e9222006-01-11 15:39:08 +1100817 atomic_inc(&bp->b_hold);
818 XB_TRACE(bp, "hold", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
821/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100822 * Releases a hold on the specified buffer. If the
823 * the hold count is 1, calls xfs_buf_free.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 */
825void
Nathan Scottce8e9222006-01-11 15:39:08 +1100826xfs_buf_rele(
827 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
Nathan Scottce8e9222006-01-11 15:39:08 +1100829 xfs_bufhash_t *hash = bp->b_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Nathan Scottce8e9222006-01-11 15:39:08 +1100831 XB_TRACE(bp, "rele", bp->b_relse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Nathan Scottfad3aa12006-02-01 12:14:52 +1100833 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 Scottce8e9222006-01-11 15:39:08 +1100840 if (atomic_dec_and_lock(&bp->b_hold, &hash->bh_lock)) {
841 if (bp->b_relse) {
842 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 spin_unlock(&hash->bh_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +1100844 (*(bp->b_relse)) (bp);
845 } else if (bp->b_flags & XBF_FS_MANAGED) {
Christoph Hellwig7f14d0a2005-11-02 15:09:35 +1100846 spin_unlock(&hash->bh_lock);
847 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100848 ASSERT(!(bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)));
849 list_del_init(&bp->b_hash_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 spin_unlock(&hash->bh_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +1100851 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
David Chinner2f926582005-09-05 08:33:35 +1000853 } else {
854 /*
855 * Catch reference count leaks
856 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100857 ASSERT(atomic_read(&bp->b_hold) >= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 }
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 Scottce8e9222006-01-11 15:39:08 +1100873 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700877 */
878int
Nathan Scottce8e9222006-01-11 15:39:08 +1100879xfs_buf_cond_lock(
880 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
882 int locked;
883
Nathan Scottce8e9222006-01-11 15:39:08 +1100884 locked = down_trylock(&bp->b_sema) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 if (locked) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100886 XB_SET_OWNER(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100888 XB_TRACE(bp, "cond_lock", (long)locked);
889 return locked ? 0 : -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
892#if defined(DEBUG) || defined(XFS_BLI_TRACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893int
Nathan Scottce8e9222006-01-11 15:39:08 +1100894xfs_buf_lock_value(
895 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Nathan Scottce8e9222006-01-11 15:39:08 +1100897 return atomic_read(&bp->b_sema.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
899#endif
900
901/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100902 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700906 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100907void
908xfs_buf_lock(
909 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Nathan Scottce8e9222006-01-11 15:39:08 +1100911 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 Torvalds1da177e2005-04-16 15:20:36 -0700917}
918
919/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100920 * Releases the lock on the buffer object.
David Chinner2f926582005-09-05 08:33:35 +1000921 * If the buffer is marked delwri but is not queued, do so before we
Nathan Scottce8e9222006-01-11 15:39:08 +1100922 * unlock the buffer as we need to set flags correctly. We also need to
David Chinner2f926582005-09-05 08:33:35 +1000923 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700925 */
926void
Nathan Scottce8e9222006-01-11 15:39:08 +1100927xfs_buf_unlock(
928 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Nathan Scottce8e9222006-01-11 15:39:08 +1100930 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 Chinner2f926582005-09-05 08:33:35 +1000934 }
935
Nathan Scottce8e9222006-01-11 15:39:08 +1100936 XB_CLEAR_OWNER(bp);
937 up(&bp->b_sema);
938 XB_TRACE(bp, "unlock", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
940
941
942/*
943 * Pinning Buffer Storage in Memory
Nathan Scottce8e9222006-01-11 15:39:08 +1100944 * Ensure that no attempt to force a buffer to disk will succeed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 */
946void
Nathan Scottce8e9222006-01-11 15:39:08 +1100947xfs_buf_pin(
948 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
Nathan Scottce8e9222006-01-11 15:39:08 +1100950 atomic_inc(&bp->b_pin_count);
951 XB_TRACE(bp, "pin", (long)bp->b_pin_count.counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954void
Nathan Scottce8e9222006-01-11 15:39:08 +1100955xfs_buf_unpin(
956 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
Nathan Scottce8e9222006-01-11 15:39:08 +1100958 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 Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
963int
Nathan Scottce8e9222006-01-11 15:39:08 +1100964xfs_buf_ispin(
965 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Nathan Scottce8e9222006-01-11 15:39:08 +1100967 return atomic_read(&bp->b_pin_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
969
Nathan Scottce8e9222006-01-11 15:39:08 +1100970STATIC void
971xfs_buf_wait_unpin(
972 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
974 DECLARE_WAITQUEUE (wait, current);
975
Nathan Scottce8e9222006-01-11 15:39:08 +1100976 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return;
978
Nathan Scottce8e9222006-01-11 15:39:08 +1100979 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 for (;;) {
981 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +1100982 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 break;
Nathan Scottce8e9222006-01-11 15:39:08 +1100984 if (atomic_read(&bp->b_io_remaining))
985 blk_run_address_space(bp->b_target->bt_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 schedule();
987 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100988 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 set_current_state(TASK_RUNNING);
990}
991
992/*
993 * Buffer Utility Routines
994 */
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100997xfs_buf_iodone_work(
David Howellsc4028952006-11-22 14:57:56 +0000998 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
David Howellsc4028952006-11-22 14:57:56 +00001000 xfs_buf_t *bp =
1001 container_of(work, xfs_buf_t, b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Nathan Scottce8e9222006-01-11 15:39:08 +11001003 if (bp->b_iodone)
1004 (*(bp->b_iodone))(bp);
1005 else if (bp->b_flags & XBF_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 xfs_buf_relse(bp);
1007}
1008
1009void
Nathan Scottce8e9222006-01-11 15:39:08 +11001010xfs_buf_ioend(
1011 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 int schedule)
1013{
Nathan Scottce8e9222006-01-11 15:39:08 +11001014 bp->b_flags &= ~(XBF_READ | XBF_WRITE);
1015 if (bp->b_error == 0)
1016 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Nathan Scottce8e9222006-01-11 15:39:08 +11001018 XB_TRACE(bp, "iodone", bp->b_iodone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Nathan Scottce8e9222006-01-11 15:39:08 +11001020 if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if (schedule) {
David Howellsc4028952006-11-22 14:57:56 +00001022 INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
Nathan Scottce8e9222006-01-11 15:39:08 +11001023 queue_work(xfslogd_workqueue, &bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 } else {
David Howellsc4028952006-11-22 14:57:56 +00001025 xfs_buf_iodone_work(&bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
1027 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +11001028 up(&bp->b_iodonesema);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 }
1030}
1031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032void
Nathan Scottce8e9222006-01-11 15:39:08 +11001033xfs_buf_ioerror(
1034 xfs_buf_t *bp,
1035 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
1037 ASSERT(error >= 0 && error <= 0xffff);
Nathan Scottce8e9222006-01-11 15:39:08 +11001038 bp->b_error = (unsigned short)error;
1039 XB_TRACE(bp, "ioerror", (unsigned long)error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
1042/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001043 * 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 Torvalds1da177e2005-04-16 15:20:36 -07001045 * when all of the subsidiary I/O requests, if any, have been completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 */
1047int
Nathan Scottce8e9222006-01-11 15:39:08 +11001048xfs_buf_iostart(
1049 xfs_buf_t *bp,
1050 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
1052 int status = 0;
1053
Nathan Scottce8e9222006-01-11 15:39:08 +11001054 XB_TRACE(bp, "iostart", (unsigned long)flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Nathan Scottce8e9222006-01-11 15:39:08 +11001056 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 Torvalds1da177e2005-04-16 15:20:36 -07001060 return status;
1061 }
1062
Nathan Scottce8e9222006-01-11 15:39:08 +11001063 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 Torvalds1da177e2005-04-16 15:20:36 -07001067
Nathan Scottce8e9222006-01-11 15:39:08 +11001068 BUG_ON(bp->b_bn == XFS_BUF_DADDR_NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
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 Scottce8e9222006-01-11 15:39:08 +11001074 status = (flags & XBF_WRITE) ?
1075 xfs_buf_iostrategy(bp) : xfs_buf_iorequest(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
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 Scottce8e9222006-01-11 15:39:08 +11001082 if (!status && !(flags & XBF_ASYNC))
1083 status = xfs_buf_iowait(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 return status;
1086}
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088STATIC __inline__ int
Nathan Scottce8e9222006-01-11 15:39:08 +11001089_xfs_buf_iolocked(
1090 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Nathan Scottce8e9222006-01-11 15:39:08 +11001092 ASSERT(bp->b_flags & (XBF_READ | XBF_WRITE));
1093 if (bp->b_flags & XBF_READ)
1094 return bp->b_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 return 0;
1096}
1097
1098STATIC __inline__ void
Nathan Scottce8e9222006-01-11 15:39:08 +11001099_xfs_buf_ioend(
1100 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 int schedule)
1102{
Nathan Scottce8e9222006-01-11 15:39:08 +11001103 if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
1104 bp->b_locked = 0;
1105 xfs_buf_ioend(bp, schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
1107}
1108
1109STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +11001110xfs_buf_bio_end_io(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 struct bio *bio,
1112 unsigned int bytes_done,
1113 int error)
1114{
Nathan Scottce8e9222006-01-11 15:39:08 +11001115 xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
1116 unsigned int blocksize = bp->b_target->bt_bsize;
Nathan Scotteedb5532005-09-02 16:39:56 +10001117 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 if (bio->bi_size)
1120 return 1;
1121
1122 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
Nathan Scottce8e9222006-01-11 15:39:08 +11001123 bp->b_error = EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Nathan Scotteedb5532005-09-02 16:39:56 +10001125 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 struct page *page = bvec->bv_page;
1127
Nathan Scott948ecdb2006-09-28 11:03:13 +10001128 ASSERT(!PagePrivate(page));
Nathan Scottce8e9222006-01-11 15:39:08 +11001129 if (unlikely(bp->b_error)) {
1130 if (bp->b_flags & XBF_READ)
Nathan Scotteedb5532005-09-02 16:39:56 +10001131 ClearPageUptodate(page);
Nathan Scottce8e9222006-01-11 15:39:08 +11001132 } else if (blocksize >= PAGE_CACHE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 SetPageUptodate(page);
1134 } else if (!PagePrivate(page) &&
Nathan Scottce8e9222006-01-11 15:39:08 +11001135 (bp->b_flags & _XBF_PAGE_CACHE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 set_page_region(page, bvec->bv_offset, bvec->bv_len);
1137 }
1138
Nathan Scotteedb5532005-09-02 16:39:56 +10001139 if (--bvec >= bio->bi_io_vec)
1140 prefetchw(&bvec->bv_page->flags);
1141
Nathan Scottce8e9222006-01-11 15:39:08 +11001142 if (_xfs_buf_iolocked(bp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 unlock_page(page);
1144 }
Nathan Scotteedb5532005-09-02 16:39:56 +10001145 } while (bvec >= bio->bi_io_vec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Nathan Scottce8e9222006-01-11 15:39:08 +11001147 _xfs_buf_ioend(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 bio_put(bio);
1149 return 0;
1150}
1151
1152STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001153_xfs_buf_ioapply(
1154 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
1156 int i, rw, map_i, total_nr_pages, nr_pages;
1157 struct bio *bio;
Nathan Scottce8e9222006-01-11 15:39:08 +11001158 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 Torvalds1da177e2005-04-16 15:20:36 -07001163
Nathan Scottce8e9222006-01-11 15:39:08 +11001164 total_nr_pages = bp->b_page_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 map_i = 0;
1166
Nathan Scottce8e9222006-01-11 15:39:08 +11001167 if (bp->b_flags & XBF_ORDERED) {
1168 ASSERT(!(bp->b_flags & XBF_READ));
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001169 rw = WRITE_BARRIER;
Nathan Scott51bdd702006-09-28 11:01:57 +10001170 } 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 Hellwigf538d4d2005-11-02 10:26:59 +11001177 }
1178
Nathan Scottce8e9222006-01-11 15:39:08 +11001179 /* Special code path for reading a sub page size buffer in --
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 * we populate up the whole page, and hence the other metadata
1181 * in the same page. This optimization is only valid when the
Nathan Scottce8e9222006-01-11 15:39:08 +11001182 * filesystem block size is not smaller than the page size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001184 if ((bp->b_buffer_length < PAGE_CACHE_SIZE) &&
1185 (bp->b_flags & XBF_READ) && locking &&
1186 (blocksize >= PAGE_CACHE_SIZE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 bio = bio_alloc(GFP_NOIO, 1);
1188
Nathan Scottce8e9222006-01-11 15:39:08 +11001189 bio->bi_bdev = bp->b_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 bio->bi_sector = sector - (offset >> BBSHIFT);
Nathan Scottce8e9222006-01-11 15:39:08 +11001191 bio->bi_end_io = xfs_buf_bio_end_io;
1192 bio->bi_private = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Nathan Scottce8e9222006-01-11 15:39:08 +11001194 bio_add_page(bio, bp->b_pages[0], PAGE_CACHE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 size = 0;
1196
Nathan Scottce8e9222006-01-11 15:39:08 +11001197 atomic_inc(&bp->b_io_remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 goto submit_io;
1200 }
1201
1202 /* Lock down the pages which we need to for the request */
Nathan Scottce8e9222006-01-11 15:39:08 +11001203 if (locking && (bp->b_flags & XBF_WRITE) && (bp->b_locked == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 for (i = 0; size; i++) {
1205 int nbytes = PAGE_CACHE_SIZE - offset;
Nathan Scottce8e9222006-01-11 15:39:08 +11001206 struct page *page = bp->b_pages[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 if (nbytes > size)
1209 nbytes = size;
1210
1211 lock_page(page);
1212
1213 size -= nbytes;
1214 offset = 0;
1215 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001216 offset = bp->b_offset;
1217 size = bp->b_count_desired;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
1219
1220next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001221 atomic_inc(&bp->b_io_remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 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 Scottce8e9222006-01-11 15:39:08 +11001227 bio->bi_bdev = bp->b_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 bio->bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001229 bio->bi_end_io = xfs_buf_bio_end_io;
1230 bio->bi_private = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 for (; size && nr_pages; nr_pages--, map_i++) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001233 int rbytes, nbytes = PAGE_CACHE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 if (nbytes > size)
1236 nbytes = size;
1237
Nathan Scottce8e9222006-01-11 15:39:08 +11001238 rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset);
1239 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 break;
1241
1242 offset = 0;
1243 sector += nbytes >> BBSHIFT;
1244 size -= nbytes;
1245 total_nr_pages--;
1246 }
1247
1248submit_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 Scottce8e9222006-01-11 15:39:08 +11001255 xfs_buf_ioerror(bp, EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 }
1257}
1258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259int
Nathan Scottce8e9222006-01-11 15:39:08 +11001260xfs_buf_iorequest(
1261 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
Nathan Scottce8e9222006-01-11 15:39:08 +11001263 XB_TRACE(bp, "iorequest", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Nathan Scottce8e9222006-01-11 15:39:08 +11001265 if (bp->b_flags & XBF_DELWRI) {
1266 xfs_buf_delwri_queue(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 return 0;
1268 }
1269
Nathan Scottce8e9222006-01-11 15:39:08 +11001270 if (bp->b_flags & XBF_WRITE) {
1271 xfs_buf_wait_unpin(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 }
1273
Nathan Scottce8e9222006-01-11 15:39:08 +11001274 xfs_buf_hold(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 /* Set the count to 1 initially, this will stop an I/O
1277 * completion callout which happens before we have started
Nathan Scottce8e9222006-01-11 15:39:08 +11001278 * all the I/O from calling xfs_buf_ioend too early.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001280 atomic_set(&bp->b_io_remaining, 1);
1281 _xfs_buf_ioapply(bp);
1282 _xfs_buf_ioend(bp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Nathan Scottce8e9222006-01-11 15:39:08 +11001284 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 return 0;
1286}
1287
1288/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001289 * 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 Torvalds1da177e2005-04-16 15:20:36 -07001292 */
1293int
Nathan Scottce8e9222006-01-11 15:39:08 +11001294xfs_buf_iowait(
1295 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
Nathan Scottce8e9222006-01-11 15:39:08 +11001297 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 Torvalds1da177e2005-04-16 15:20:36 -07001303}
1304
Nathan Scottce8e9222006-01-11 15:39:08 +11001305xfs_caddr_t
1306xfs_buf_offset(
1307 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 size_t offset)
1309{
1310 struct page *page;
1311
Nathan Scottce8e9222006-01-11 15:39:08 +11001312 if (bp->b_flags & XBF_MAPPED)
1313 return XFS_BUF_PTR(bp) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Nathan Scottce8e9222006-01-11 15:39:08 +11001315 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 Torvalds1da177e2005-04-16 15:20:36 -07001318}
1319
1320/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 * Move data into or out of a buffer.
1322 */
1323void
Nathan Scottce8e9222006-01-11 15:39:08 +11001324xfs_buf_iomove(
1325 xfs_buf_t *bp, /* buffer to process */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 size_t boff, /* starting buffer offset */
1327 size_t bsize, /* length to copy */
1328 caddr_t data, /* data address */
Nathan Scottce8e9222006-01-11 15:39:08 +11001329 xfs_buf_rw_t mode) /* read/write/zero flag */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
1331 size_t bend, cpoff, csize;
1332 struct page *page;
1333
1334 bend = boff + bsize;
1335 while (boff < bend) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001336 page = bp->b_pages[xfs_buf_btoct(boff + bp->b_offset)];
1337 cpoff = xfs_buf_poff(boff + bp->b_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 csize = min_t(size_t,
Nathan Scottce8e9222006-01-11 15:39:08 +11001339 PAGE_CACHE_SIZE-cpoff, bp->b_count_desired-boff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 ASSERT(((csize + cpoff) <= PAGE_CACHE_SIZE));
1342
1343 switch (mode) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001344 case XBRW_ZERO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 memset(page_address(page) + cpoff, 0, csize);
1346 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001347 case XBRW_READ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 memcpy(data, page_address(page) + cpoff, csize);
1349 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001350 case XBRW_WRITE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 memcpy(page_address(page) + cpoff, data, csize);
1352 }
1353
1354 boff += csize;
1355 data += csize;
1356 }
1357}
1358
1359/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001360 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 */
1362
1363/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001364 * Wait for any bufs with callbacks that have been submitted but
1365 * have not yet returned... walk the hash list for the target.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 */
1367void
1368xfs_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];
1377again:
1378 spin_lock(&hash->bh_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +11001379 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 Torvalds1da177e2005-04-16 15:20:36 -07001382 spin_unlock(&hash->bh_lock);
David Chinner2f926582005-09-05 08:33:35 +10001383 /*
1384 * Catch superblock reference count leaks
1385 * immediately
1386 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001387 BUG_ON(bp->b_bn == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 delay(100);
1389 goto again;
1390 }
1391 }
1392 spin_unlock(&hash->bh_lock);
1393 }
1394}
1395
1396/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001397 * 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 Torvalds1da177e2005-04-16 15:20:36 -07001400 */
1401STATIC void
1402xfs_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 Apostolov93c189c2006-11-11 18:03:49 +11001411 sizeof(xfs_bufhash_t), KM_SLEEP | KM_LARGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 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
1418STATIC void
1419xfs_free_bufhash(
1420 xfs_buftarg_t *btp)
1421{
Nathan Scottce8e9222006-01-11 15:39:08 +11001422 kmem_free(btp->bt_hash, (1<<btp->bt_hashshift) * sizeof(xfs_bufhash_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 btp->bt_hash = NULL;
1424}
1425
David Chinnera6867a62006-01-11 15:37:58 +11001426/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001427 * buftarg list for delwrite queue processing
David Chinnera6867a62006-01-11 15:37:58 +11001428 */
1429STATIC LIST_HEAD(xfs_buftarg_list);
1430STATIC DEFINE_SPINLOCK(xfs_buftarg_lock);
1431
1432STATIC void
1433xfs_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
1441STATIC void
1442xfs_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 Torvalds1da177e2005-04-16 15:20:36 -07001450void
1451xfs_free_buftarg(
1452 xfs_buftarg_t *btp,
1453 int external)
1454{
1455 xfs_flush_buftarg(btp, 1);
1456 if (external)
Nathan Scottce8e9222006-01-11 15:39:08 +11001457 xfs_blkdev_put(btp->bt_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 xfs_free_bufhash(btp);
Nathan Scottce8e9222006-01-11 15:39:08 +11001459 iput(btp->bt_mapping->host);
David Chinnera6867a62006-01-11 15:37:58 +11001460
Nathan Scottce8e9222006-01-11 15:39:08 +11001461 /* Unregister the buftarg first so that we don't get a
1462 * wakeup finding a non-existent task
1463 */
David Chinnera6867a62006-01-11 15:37:58 +11001464 xfs_unregister_buftarg(btp);
1465 kthread_stop(btp->bt_task);
1466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 kmem_free(btp, sizeof(*btp));
1468}
1469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470STATIC int
1471xfs_setsize_buftarg_flags(
1472 xfs_buftarg_t *btp,
1473 unsigned int blocksize,
1474 unsigned int sectorsize,
1475 int verbose)
1476{
Nathan Scottce8e9222006-01-11 15:39:08 +11001477 btp->bt_bsize = blocksize;
1478 btp->bt_sshift = ffs(sectorsize) - 1;
1479 btp->bt_smask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Nathan Scottce8e9222006-01-11 15:39:08 +11001481 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 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 Scottce8e9222006-01-11 15:39:08 +11001501 * 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 Torvalds1da177e2005-04-16 15:20:36 -07001505STATIC int
1506xfs_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
1514int
1515xfs_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
1523STATIC int
1524xfs_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 Hellwigf5e54d62006-06-28 04:26:44 -07001531 static const struct address_space_operations mapping_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 .sync_page = block_sync_page,
Christoph Lametere965f962006-02-01 03:05:41 -08001533 .migratepage = fail_migrate_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 };
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 Scottce8e9222006-01-11 15:39:08 +11001553 btp->bt_mapping = mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 return 0;
1555}
1556
David Chinnera6867a62006-01-11 15:37:58 +11001557STATIC int
1558xfs_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);
1573out_error:
1574 return error;
1575}
1576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577xfs_buftarg_t *
1578xfs_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 Scottce8e9222006-01-11 15:39:08 +11001586 btp->bt_dev = bdev->bd_dev;
1587 btp->bt_bdev = bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 if (xfs_setsize_buftarg_early(btp, bdev))
1589 goto error;
1590 if (xfs_mapping_buftarg(btp, bdev))
1591 goto error;
David Chinnera6867a62006-01-11 15:37:58 +11001592 if (xfs_alloc_delwrite_queue(btp))
1593 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 xfs_alloc_bufhash(btp, external);
1595 return btp;
1596
1597error:
1598 kmem_free(btp, sizeof(*btp));
1599 return NULL;
1600}
1601
1602
1603/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001604 * Delayed write buffer handling
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001607xfs_buf_delwri_queue(
1608 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 int unlock)
1610{
Nathan Scottce8e9222006-01-11 15:39:08 +11001611 struct list_head *dwq = &bp->b_target->bt_delwrite_queue;
1612 spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
David Chinnera6867a62006-01-11 15:37:58 +11001613
Nathan Scottce8e9222006-01-11 15:39:08 +11001614 XB_TRACE(bp, "delwri_q", (long)unlock);
1615 ASSERT((bp->b_flags&(XBF_DELWRI|XBF_ASYNC)) == (XBF_DELWRI|XBF_ASYNC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
David Chinnera6867a62006-01-11 15:37:58 +11001617 spin_lock(dwlk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 /* If already in the queue, dequeue and place at tail */
Nathan Scottce8e9222006-01-11 15:39:08 +11001619 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 Torvalds1da177e2005-04-16 15:20:36 -07001624 }
1625
Nathan Scottce8e9222006-01-11 15:39:08 +11001626 bp->b_flags |= _XBF_DELWRI_Q;
1627 list_add_tail(&bp->b_list, dwq);
1628 bp->b_queuetime = jiffies;
David Chinnera6867a62006-01-11 15:37:58 +11001629 spin_unlock(dwlk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
1631 if (unlock)
Nathan Scottce8e9222006-01-11 15:39:08 +11001632 xfs_buf_unlock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633}
1634
1635void
Nathan Scottce8e9222006-01-11 15:39:08 +11001636xfs_buf_delwri_dequeue(
1637 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
Nathan Scottce8e9222006-01-11 15:39:08 +11001639 spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 int dequeued = 0;
1641
David Chinnera6867a62006-01-11 15:37:58 +11001642 spin_lock(dwlk);
Nathan Scottce8e9222006-01-11 15:39:08 +11001643 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 Torvalds1da177e2005-04-16 15:20:36 -07001646 dequeued = 1;
1647 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001648 bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q);
David Chinnera6867a62006-01-11 15:37:58 +11001649 spin_unlock(dwlk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
1651 if (dequeued)
Nathan Scottce8e9222006-01-11 15:39:08 +11001652 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Nathan Scottce8e9222006-01-11 15:39:08 +11001654 XB_TRACE(bp, "delwri_dq", (long)dequeued);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655}
1656
1657STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001658xfs_buf_runall_queues(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 struct workqueue_struct *queue)
1660{
1661 flush_workqueue(queue);
1662}
1663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664STATIC int
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001665xfsbufd_wakeup(
Nathan Scott15c84a42005-11-04 10:51:01 +11001666 int priority,
1667 gfp_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668{
Christoph Hellwigda7f93e2006-01-11 20:49:57 +11001669 xfs_buftarg_t *btp;
David Chinnera6867a62006-01-11 15:37:58 +11001670
1671 spin_lock(&xfs_buftarg_lock);
Christoph Hellwigda7f93e2006-01-11 20:49:57 +11001672 list_for_each_entry(btp, &xfs_buftarg_list, bt_list) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001673 if (test_bit(XBT_FORCE_SLEEP, &btp->bt_flags))
David Chinnera6867a62006-01-11 15:37:58 +11001674 continue;
Nathan Scottce8e9222006-01-11 15:39:08 +11001675 set_bit(XBT_FORCE_FLUSH, &btp->bt_flags);
David Chinnera6867a62006-01-11 15:37:58 +11001676 wake_up_process(btp->bt_task);
1677 }
1678 spin_unlock(&xfs_buftarg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 return 0;
1680}
1681
1682STATIC int
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001683xfsbufd(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 void *data)
1685{
1686 struct list_head tmp;
1687 unsigned long age;
David Chinnera6867a62006-01-11 15:37:58 +11001688 xfs_buftarg_t *target = (xfs_buftarg_t *)data;
Nathan Scottce8e9222006-01-11 15:39:08 +11001689 xfs_buf_t *bp, *n;
David Chinnera6867a62006-01-11 15:37:58 +11001690 struct list_head *dwq = &target->bt_delwrite_queue;
1691 spinlock_t *dwlk = &target->bt_delwrite_lock;
Nathan Scottf07c2252006-09-28 10:52:15 +10001692 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 current->flags |= PF_MEMALLOC;
1695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 INIT_LIST_HEAD(&tmp);
1697 do {
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001698 if (unlikely(freezing(current))) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001699 set_bit(XBT_FORCE_SLEEP, &target->bt_flags);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001700 refrigerator();
Nathan Scottabd0cf72005-05-05 13:30:13 -07001701 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +11001702 clear_bit(XBT_FORCE_SLEEP, &target->bt_flags);
Nathan Scottabd0cf72005-05-05 13:30:13 -07001703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
Nathan Scott15c84a42005-11-04 10:51:01 +11001705 schedule_timeout_interruptible(
1706 xfs_buf_timer_centisecs * msecs_to_jiffies(10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Nathan Scottf07c2252006-09-28 10:52:15 +10001708 count = 0;
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07001709 age = xfs_buf_age_centisecs * msecs_to_jiffies(10);
David Chinnera6867a62006-01-11 15:37:58 +11001710 spin_lock(dwlk);
Nathan Scottce8e9222006-01-11 15:39:08 +11001711 list_for_each_entry_safe(bp, n, dwq, b_list) {
1712 XB_TRACE(bp, "walkq1", (long)xfs_buf_ispin(bp));
1713 ASSERT(bp->b_flags & XBF_DELWRI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Nathan Scottce8e9222006-01-11 15:39:08 +11001715 if (!xfs_buf_ispin(bp) && !xfs_buf_cond_lock(bp)) {
1716 if (!test_bit(XBT_FORCE_FLUSH,
David Chinnera6867a62006-01-11 15:37:58 +11001717 &target->bt_flags) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 time_before(jiffies,
Nathan Scottce8e9222006-01-11 15:39:08 +11001719 bp->b_queuetime + age)) {
1720 xfs_buf_unlock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 break;
1722 }
1723
Nathan Scottf07c2252006-09-28 10:52:15 +10001724 bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q|
1725 _XBF_RUN_QUEUES);
Nathan Scottce8e9222006-01-11 15:39:08 +11001726 bp->b_flags |= XBF_WRITE;
Nathan Scottf07c2252006-09-28 10:52:15 +10001727 list_move_tail(&bp->b_list, &tmp);
1728 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 }
1730 }
David Chinnera6867a62006-01-11 15:37:58 +11001731 spin_unlock(dwlk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733 while (!list_empty(&tmp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001734 bp = list_entry(tmp.next, xfs_buf_t, b_list);
1735 ASSERT(target == bp->b_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Nathan Scottce8e9222006-01-11 15:39:08 +11001737 list_del_init(&bp->b_list);
1738 xfs_buf_iostrategy(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 }
1740
1741 if (as_list_len > 0)
1742 purge_addresses();
Nathan Scottf07c2252006-09-28 10:52:15 +10001743 if (count)
1744 blk_run_address_space(target->bt_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Nathan Scottce8e9222006-01-11 15:39:08 +11001746 clear_bit(XBT_FORCE_FLUSH, &target->bt_flags);
Christoph Hellwig4df08c52005-09-05 08:34:18 +10001747 } while (!kthread_should_stop());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Christoph Hellwig4df08c52005-09-05 08:34:18 +10001749 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750}
1751
1752/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001753 * Go through all incore buffers, and release buffers if they belong to
1754 * the given device. This is used in filesystem error handling to
1755 * preserve the consistency of its metadata.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 */
1757int
1758xfs_flush_buftarg(
1759 xfs_buftarg_t *target,
1760 int wait)
1761{
1762 struct list_head tmp;
Nathan Scottce8e9222006-01-11 15:39:08 +11001763 xfs_buf_t *bp, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 int pincount = 0;
David Chinnera6867a62006-01-11 15:37:58 +11001765 struct list_head *dwq = &target->bt_delwrite_queue;
1766 spinlock_t *dwlk = &target->bt_delwrite_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Nathan Scottce8e9222006-01-11 15:39:08 +11001768 xfs_buf_runall_queues(xfsdatad_workqueue);
1769 xfs_buf_runall_queues(xfslogd_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771 INIT_LIST_HEAD(&tmp);
David Chinnera6867a62006-01-11 15:37:58 +11001772 spin_lock(dwlk);
Nathan Scottce8e9222006-01-11 15:39:08 +11001773 list_for_each_entry_safe(bp, n, dwq, b_list) {
1774 ASSERT(bp->b_target == target);
1775 ASSERT(bp->b_flags & (XBF_DELWRI | _XBF_DELWRI_Q));
1776 XB_TRACE(bp, "walkq2", (long)xfs_buf_ispin(bp));
1777 if (xfs_buf_ispin(bp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 pincount++;
1779 continue;
1780 }
1781
Nathan Scottf07c2252006-09-28 10:52:15 +10001782 list_move_tail(&bp->b_list, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 }
David Chinnera6867a62006-01-11 15:37:58 +11001784 spin_unlock(dwlk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
1786 /*
1787 * Dropped the delayed write list lock, now walk the temporary list
1788 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001789 list_for_each_entry_safe(bp, n, &tmp, b_list) {
1790 xfs_buf_lock(bp);
Nathan Scottf07c2252006-09-28 10:52:15 +10001791 bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q|_XBF_RUN_QUEUES);
Nathan Scottce8e9222006-01-11 15:39:08 +11001792 bp->b_flags |= XBF_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 if (wait)
Nathan Scottce8e9222006-01-11 15:39:08 +11001794 bp->b_flags &= ~XBF_ASYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 else
Nathan Scottce8e9222006-01-11 15:39:08 +11001796 list_del_init(&bp->b_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Nathan Scottce8e9222006-01-11 15:39:08 +11001798 xfs_buf_iostrategy(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 }
1800
Nathan Scottf07c2252006-09-28 10:52:15 +10001801 if (wait)
1802 blk_run_address_space(target->bt_mapping);
1803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 /*
1805 * Remaining list items must be flushed before returning
1806 */
1807 while (!list_empty(&tmp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001808 bp = list_entry(tmp.next, xfs_buf_t, b_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Nathan Scottce8e9222006-01-11 15:39:08 +11001810 list_del_init(&bp->b_list);
1811 xfs_iowait(bp);
1812 xfs_buf_relse(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 }
1814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 return pincount;
1816}
1817
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001818int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11001819xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820{
Nathan Scottce8e9222006-01-11 15:39:08 +11001821#ifdef XFS_BUF_TRACE
1822 xfs_buf_trace_buf = ktrace_alloc(XFS_BUF_TRACE_SIZE, KM_SLEEP);
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001823#endif
1824
Nathan Scott87582802006-03-14 13:18:19 +11001825 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
1826 KM_ZONE_HWALIGN, NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11001827 if (!xfs_buf_zone)
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001828 goto out_free_trace_buf;
1829
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001830 xfslogd_workqueue = create_workqueue("xfslogd");
1831 if (!xfslogd_workqueue)
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001832 goto out_free_buf_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001834 xfsdatad_workqueue = create_workqueue("xfsdatad");
1835 if (!xfsdatad_workqueue)
1836 goto out_destroy_xfslogd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Nathan Scottce8e9222006-01-11 15:39:08 +11001838 xfs_buf_shake = kmem_shake_register(xfsbufd_wakeup);
1839 if (!xfs_buf_shake)
David Chinnera6867a62006-01-11 15:37:58 +11001840 goto out_destroy_xfsdatad_workqueue;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001841
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001842 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001844 out_destroy_xfsdatad_workqueue:
1845 destroy_workqueue(xfsdatad_workqueue);
1846 out_destroy_xfslogd_workqueue:
1847 destroy_workqueue(xfslogd_workqueue);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001848 out_free_buf_zone:
Nathan Scottce8e9222006-01-11 15:39:08 +11001849 kmem_zone_destroy(xfs_buf_zone);
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001850 out_free_trace_buf:
Nathan Scottce8e9222006-01-11 15:39:08 +11001851#ifdef XFS_BUF_TRACE
1852 ktrace_free(xfs_buf_trace_buf);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001853#endif
Nathan Scott87582802006-03-14 13:18:19 +11001854 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855}
1856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857void
Nathan Scottce8e9222006-01-11 15:39:08 +11001858xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
Nathan Scottce8e9222006-01-11 15:39:08 +11001860 kmem_shake_deregister(xfs_buf_shake);
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001861 destroy_workqueue(xfsdatad_workqueue);
1862 destroy_workqueue(xfslogd_workqueue);
Nathan Scottce8e9222006-01-11 15:39:08 +11001863 kmem_zone_destroy(xfs_buf_zone);
1864#ifdef XFS_BUF_TRACE
1865 ktrace_free(xfs_buf_trace_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867}