blob: 0b2177a9fbdcfc5677c9b9937f050c5e6c0060b0 [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
David Chinner7989cb82007-02-10 18:34:56 +110037static kmem_zone_t *xfs_buf_zone;
David Chinnera6867a62006-01-11 15:37:58 +110038STATIC int xfsbufd(void *);
Al Viro27496a82005-10-21 03:20:48 -040039STATIC int xfsbufd_wakeup(int, gfp_t);
Nathan Scottce8e9222006-01-11 15:39:08 +110040STATIC void xfs_buf_delwri_queue(xfs_buf_t *, int);
Rusty Russell8e1f9362007-07-17 04:03:17 -070041static struct shrinker xfs_buf_shake = {
42 .shrink = xfsbufd_wakeup,
43 .seeks = DEFAULT_SEEKS,
44};
Christoph Hellwig23ea4032005-06-21 15:14:01 +100045
David Chinner7989cb82007-02-10 18:34:56 +110046static struct workqueue_struct *xfslogd_workqueue;
Christoph Hellwig0829c362005-09-02 16:58:49 +100047struct workqueue_struct *xfsdatad_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Nathan Scottce8e9222006-01-11 15:39:08 +110049#ifdef XFS_BUF_TRACE
Linus Torvalds1da177e2005-04-16 15:20:36 -070050void
Nathan Scottce8e9222006-01-11 15:39:08 +110051xfs_buf_trace(
52 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 char *id,
54 void *data,
55 void *ra)
56{
Nathan Scottce8e9222006-01-11 15:39:08 +110057 ktrace_enter(xfs_buf_trace_buf,
58 bp, id,
59 (void *)(unsigned long)bp->b_flags,
60 (void *)(unsigned long)bp->b_hold.counter,
Lachlan McIlroyd63f1542008-08-13 16:28:40 +100061 (void *)(unsigned long)bp->b_sema.count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 (void *)current,
63 data, ra,
Nathan Scottce8e9222006-01-11 15:39:08 +110064 (void *)(unsigned long)((bp->b_file_offset>>32) & 0xffffffff),
65 (void *)(unsigned long)(bp->b_file_offset & 0xffffffff),
66 (void *)(unsigned long)bp->b_buffer_length,
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 NULL, NULL, NULL, NULL, NULL);
68}
Nathan Scottce8e9222006-01-11 15:39:08 +110069ktrace_t *xfs_buf_trace_buf;
70#define XFS_BUF_TRACE_SIZE 4096
71#define XB_TRACE(bp, id, data) \
72 xfs_buf_trace(bp, id, (void *)data, (void *)__builtin_return_address(0))
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#else
Nathan Scottce8e9222006-01-11 15:39:08 +110074#define XB_TRACE(bp, id, data) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#endif
76
Nathan Scottce8e9222006-01-11 15:39:08 +110077#ifdef XFS_BUF_LOCK_TRACKING
78# define XB_SET_OWNER(bp) ((bp)->b_last_holder = current->pid)
79# define XB_CLEAR_OWNER(bp) ((bp)->b_last_holder = -1)
80# define XB_GET_OWNER(bp) ((bp)->b_last_holder)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#else
Nathan Scottce8e9222006-01-11 15:39:08 +110082# define XB_SET_OWNER(bp) do { } while (0)
83# define XB_CLEAR_OWNER(bp) do { } while (0)
84# define XB_GET_OWNER(bp) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#endif
86
Nathan Scottce8e9222006-01-11 15:39:08 +110087#define xb_to_gfp(flags) \
88 ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : \
89 ((flags) & XBF_DONT_BLOCK) ? GFP_NOFS : GFP_KERNEL) | __GFP_NOWARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Nathan Scottce8e9222006-01-11 15:39:08 +110091#define xb_to_km(flags) \
92 (((flags) & XBF_DONT_BLOCK) ? KM_NOFS : KM_SLEEP)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Nathan Scottce8e9222006-01-11 15:39:08 +110094#define xfs_buf_allocate(flags) \
95 kmem_zone_alloc(xfs_buf_zone, xb_to_km(flags))
96#define xfs_buf_deallocate(bp) \
97 kmem_zone_free(xfs_buf_zone, (bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100100 * Page Region interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 *
Nathan Scottce8e9222006-01-11 15:39:08 +1100102 * For pages in filesystems where the blocksize is smaller than the
103 * pagesize, we use the page->private field (long) to hold a bitmap
104 * of uptodate regions within the page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 *
Nathan Scottce8e9222006-01-11 15:39:08 +1100106 * Each such region is "bytes per page / bits per long" bytes long.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 *
Nathan Scottce8e9222006-01-11 15:39:08 +1100108 * NBPPR == number-of-bytes-per-page-region
109 * BTOPR == bytes-to-page-region (rounded up)
110 * BTOPRT == bytes-to-page-region-truncated (rounded down)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 */
112#if (BITS_PER_LONG == 32)
113#define PRSHIFT (PAGE_CACHE_SHIFT - 5) /* (32 == 1<<5) */
114#elif (BITS_PER_LONG == 64)
115#define PRSHIFT (PAGE_CACHE_SHIFT - 6) /* (64 == 1<<6) */
116#else
117#error BITS_PER_LONG must be 32 or 64
118#endif
119#define NBPPR (PAGE_CACHE_SIZE/BITS_PER_LONG)
120#define BTOPR(b) (((unsigned int)(b) + (NBPPR - 1)) >> PRSHIFT)
121#define BTOPRT(b) (((unsigned int)(b) >> PRSHIFT))
122
123STATIC unsigned long
124page_region_mask(
125 size_t offset,
126 size_t length)
127{
128 unsigned long mask;
129 int first, final;
130
131 first = BTOPR(offset);
132 final = BTOPRT(offset + length - 1);
133 first = min(first, final);
134
135 mask = ~0UL;
136 mask <<= BITS_PER_LONG - (final - first);
137 mask >>= BITS_PER_LONG - (final);
138
139 ASSERT(offset + length <= PAGE_CACHE_SIZE);
140 ASSERT((final - first) < BITS_PER_LONG && (final - first) >= 0);
141
142 return mask;
143}
144
David Chinner7989cb82007-02-10 18:34:56 +1100145STATIC_INLINE void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146set_page_region(
147 struct page *page,
148 size_t offset,
149 size_t length)
150{
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700151 set_page_private(page,
152 page_private(page) | page_region_mask(offset, length));
153 if (page_private(page) == ~0UL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 SetPageUptodate(page);
155}
156
David Chinner7989cb82007-02-10 18:34:56 +1100157STATIC_INLINE int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158test_page_region(
159 struct page *page,
160 size_t offset,
161 size_t length)
162{
163 unsigned long mask = page_region_mask(offset, length);
164
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700165 return (mask && (page_private(page) & mask) == mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
168/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100169 * Internal xfs_buf_t object manipulation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 */
171
172STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100173_xfs_buf_initialize(
174 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100176 xfs_off_t range_base,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 size_t range_length,
Nathan Scottce8e9222006-01-11 15:39:08 +1100178 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 /*
Nathan Scottce8e9222006-01-11 15:39:08 +1100181 * We don't want certain flags to appear in b_flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100183 flags &= ~(XBF_LOCK|XBF_MAPPED|XBF_DONT_BLOCK|XBF_READ_AHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Nathan Scottce8e9222006-01-11 15:39:08 +1100185 memset(bp, 0, sizeof(xfs_buf_t));
186 atomic_set(&bp->b_hold, 1);
David Chinnerb4dd3302008-08-13 16:36:11 +1000187 init_completion(&bp->b_iowait);
Nathan Scottce8e9222006-01-11 15:39:08 +1100188 INIT_LIST_HEAD(&bp->b_list);
189 INIT_LIST_HEAD(&bp->b_hash_list);
190 init_MUTEX_LOCKED(&bp->b_sema); /* held, no waiters */
191 XB_SET_OWNER(bp);
192 bp->b_target = target;
193 bp->b_file_offset = range_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 /*
195 * Set buffer_length and count_desired to the same value initially.
196 * I/O routines should use count_desired, which will be the same in
197 * most cases but may be reset (e.g. XFS recovery).
198 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100199 bp->b_buffer_length = bp->b_count_desired = range_length;
200 bp->b_flags = flags;
201 bp->b_bn = XFS_BUF_DADDR_NULL;
202 atomic_set(&bp->b_pin_count, 0);
203 init_waitqueue_head(&bp->b_waiters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Nathan Scottce8e9222006-01-11 15:39:08 +1100205 XFS_STATS_INC(xb_create);
206 XB_TRACE(bp, "initialize", target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
209/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100210 * Allocate a page array capable of holding a specified number
211 * of pages, and point the page buf at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 */
213STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100214_xfs_buf_get_pages(
215 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 int page_count,
Nathan Scottce8e9222006-01-11 15:39:08 +1100217 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 /* Make sure that we have a page list */
Nathan Scottce8e9222006-01-11 15:39:08 +1100220 if (bp->b_pages == NULL) {
221 bp->b_offset = xfs_buf_poff(bp->b_file_offset);
222 bp->b_page_count = page_count;
223 if (page_count <= XB_PAGES) {
224 bp->b_pages = bp->b_page_array;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100226 bp->b_pages = kmem_alloc(sizeof(struct page *) *
227 page_count, xb_to_km(flags));
228 if (bp->b_pages == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return -ENOMEM;
230 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100231 memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233 return 0;
234}
235
236/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100237 * Frees b_pages if it was allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 */
239STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100240_xfs_buf_free_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 xfs_buf_t *bp)
242{
Nathan Scottce8e9222006-01-11 15:39:08 +1100243 if (bp->b_pages != bp->b_page_array) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000244 kmem_free(bp->b_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246}
247
248/*
249 * Releases the specified buffer.
250 *
251 * The modification state of any associated pages is left unchanged.
Nathan Scottce8e9222006-01-11 15:39:08 +1100252 * The buffer most not be on any hash - use xfs_buf_rele instead for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 * hashed and refcounted buffers
254 */
255void
Nathan Scottce8e9222006-01-11 15:39:08 +1100256xfs_buf_free(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 xfs_buf_t *bp)
258{
Nathan Scottce8e9222006-01-11 15:39:08 +1100259 XB_TRACE(bp, "free", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Nathan Scottce8e9222006-01-11 15:39:08 +1100261 ASSERT(list_empty(&bp->b_hash_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000263 if (bp->b_flags & (_XBF_PAGE_CACHE|_XBF_PAGES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 uint i;
265
Nathan Scottce8e9222006-01-11 15:39:08 +1100266 if ((bp->b_flags & XBF_MAPPED) && (bp->b_page_count > 1))
Felix Blyakhercf7dab82009-02-18 15:41:28 -0600267 vunmap(bp->b_addr - bp->b_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Nathan Scott948ecdb2006-09-28 11:03:13 +1000269 for (i = 0; i < bp->b_page_count; i++) {
270 struct page *page = bp->b_pages[i];
271
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000272 if (bp->b_flags & _XBF_PAGE_CACHE)
273 ASSERT(!PagePrivate(page));
Nathan Scott948ecdb2006-09-28 11:03:13 +1000274 page_cache_release(page);
275 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100276 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
278
Nathan Scottce8e9222006-01-11 15:39:08 +1100279 xfs_buf_deallocate(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282/*
283 * Finds all pages for buffer in question and builds it's page list.
284 */
285STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100286_xfs_buf_lookup_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 xfs_buf_t *bp,
288 uint flags)
289{
Nathan Scottce8e9222006-01-11 15:39:08 +1100290 struct address_space *mapping = bp->b_target->bt_mapping;
291 size_t blocksize = bp->b_target->bt_bsize;
292 size_t size = bp->b_count_desired;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 size_t nbytes, offset;
Nathan Scottce8e9222006-01-11 15:39:08 +1100294 gfp_t gfp_mask = xb_to_gfp(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 unsigned short page_count, i;
296 pgoff_t first;
Nathan Scott204ab252006-01-11 20:50:22 +1100297 xfs_off_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 int error;
299
Nathan Scottce8e9222006-01-11 15:39:08 +1100300 end = bp->b_file_offset + bp->b_buffer_length;
301 page_count = xfs_buf_btoc(end) - xfs_buf_btoct(bp->b_file_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Nathan Scottce8e9222006-01-11 15:39:08 +1100303 error = _xfs_buf_get_pages(bp, page_count, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (unlikely(error))
305 return error;
Nathan Scottce8e9222006-01-11 15:39:08 +1100306 bp->b_flags |= _XBF_PAGE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Nathan Scottce8e9222006-01-11 15:39:08 +1100308 offset = bp->b_offset;
309 first = bp->b_file_offset >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Nathan Scottce8e9222006-01-11 15:39:08 +1100311 for (i = 0; i < bp->b_page_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 struct page *page;
313 uint retries = 0;
314
315 retry:
316 page = find_or_create_page(mapping, first + i, gfp_mask);
317 if (unlikely(page == NULL)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100318 if (flags & XBF_READ_AHEAD) {
319 bp->b_page_count = i;
Christoph Hellwig6ab455e2008-05-19 16:34:42 +1000320 for (i = 0; i < bp->b_page_count; i++)
321 unlock_page(bp->b_pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return -ENOMEM;
323 }
324
325 /*
326 * This could deadlock.
327 *
328 * But until all the XFS lowlevel code is revamped to
329 * handle buffer allocation failures we can't do much.
330 */
331 if (!(++retries % 100))
332 printk(KERN_ERR
333 "XFS: possible memory allocation "
334 "deadlock in %s (mode:0x%x)\n",
Harvey Harrison34a622b2008-04-10 12:19:21 +1000335 __func__, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Nathan Scottce8e9222006-01-11 15:39:08 +1100337 XFS_STATS_INC(xb_page_retries);
Christoph Hellwig23ea4032005-06-21 15:14:01 +1000338 xfsbufd_wakeup(0, gfp_mask);
Andrew Morton3fcfab12006-10-19 23:28:16 -0700339 congestion_wait(WRITE, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 goto retry;
341 }
342
Nathan Scottce8e9222006-01-11 15:39:08 +1100343 XFS_STATS_INC(xb_page_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 nbytes = min_t(size_t, size, PAGE_CACHE_SIZE - offset);
346 size -= nbytes;
347
Nathan Scott948ecdb2006-09-28 11:03:13 +1000348 ASSERT(!PagePrivate(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (!PageUptodate(page)) {
350 page_count--;
Christoph Hellwig6ab455e2008-05-19 16:34:42 +1000351 if (blocksize >= PAGE_CACHE_SIZE) {
352 if (flags & XBF_READ)
353 bp->b_flags |= _XBF_PAGE_LOCKED;
354 } else if (!PagePrivate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (test_page_region(page, offset, nbytes))
356 page_count++;
357 }
358 }
359
Nathan Scottce8e9222006-01-11 15:39:08 +1100360 bp->b_pages[i] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 offset = 0;
362 }
363
Christoph Hellwig6ab455e2008-05-19 16:34:42 +1000364 if (!(bp->b_flags & _XBF_PAGE_LOCKED)) {
365 for (i = 0; i < bp->b_page_count; i++)
366 unlock_page(bp->b_pages[i]);
367 }
368
Nathan Scottce8e9222006-01-11 15:39:08 +1100369 if (page_count == bp->b_page_count)
370 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Nathan Scottce8e9222006-01-11 15:39:08 +1100372 XB_TRACE(bp, "lookup_pages", (long)page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return error;
374}
375
376/*
377 * Map buffer into kernel address-space if nessecary.
378 */
379STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100380_xfs_buf_map_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 xfs_buf_t *bp,
382 uint flags)
383{
384 /* A single page buffer is always mappable */
Nathan Scottce8e9222006-01-11 15:39:08 +1100385 if (bp->b_page_count == 1) {
386 bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
387 bp->b_flags |= XBF_MAPPED;
388 } else if (flags & XBF_MAPPED) {
Felix Blyakhercf7dab82009-02-18 15:41:28 -0600389 bp->b_addr = vmap(bp->b_pages, bp->b_page_count,
390 VM_MAP, PAGE_KERNEL);
Nathan Scottce8e9222006-01-11 15:39:08 +1100391 if (unlikely(bp->b_addr == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return -ENOMEM;
Nathan Scottce8e9222006-01-11 15:39:08 +1100393 bp->b_addr += bp->b_offset;
394 bp->b_flags |= XBF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396
397 return 0;
398}
399
400/*
401 * Finding and Reading Buffers
402 */
403
404/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100405 * Look up, and creates if absent, a lockable buffer for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 * a given range of an inode. The buffer is returned
407 * locked. If other overlapping buffers exist, they are
408 * released before the new buffer is created and locked,
409 * which may imply that this call will block until those buffers
410 * are unlocked. No I/O is implied by this call.
411 */
412xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100413_xfs_buf_find(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 xfs_buftarg_t *btp, /* block device target */
Nathan Scott204ab252006-01-11 20:50:22 +1100415 xfs_off_t ioff, /* starting offset of range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 size_t isize, /* length of range */
Nathan Scottce8e9222006-01-11 15:39:08 +1100417 xfs_buf_flags_t flags,
418 xfs_buf_t *new_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Nathan Scott204ab252006-01-11 20:50:22 +1100420 xfs_off_t range_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 size_t range_length;
422 xfs_bufhash_t *hash;
Nathan Scottce8e9222006-01-11 15:39:08 +1100423 xfs_buf_t *bp, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 range_base = (ioff << BBSHIFT);
426 range_length = (isize << BBSHIFT);
427
428 /* Check for IOs smaller than the sector size / not sector aligned */
Nathan Scottce8e9222006-01-11 15:39:08 +1100429 ASSERT(!(range_length < (1 << btp->bt_sshift)));
Nathan Scott204ab252006-01-11 20:50:22 +1100430 ASSERT(!(range_base & (xfs_off_t)btp->bt_smask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 hash = &btp->bt_hash[hash_long((unsigned long)ioff, btp->bt_hashshift)];
433
434 spin_lock(&hash->bh_lock);
435
Nathan Scottce8e9222006-01-11 15:39:08 +1100436 list_for_each_entry_safe(bp, n, &hash->bh_list, b_hash_list) {
437 ASSERT(btp == bp->b_target);
438 if (bp->b_file_offset == range_base &&
439 bp->b_buffer_length == range_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 /*
Nathan Scottce8e9222006-01-11 15:39:08 +1100441 * If we look at something, bring it to the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 * front of the list for next time.
443 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100444 atomic_inc(&bp->b_hold);
445 list_move(&bp->b_hash_list, &hash->bh_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 goto found;
447 }
448 }
449
450 /* No match found */
Nathan Scottce8e9222006-01-11 15:39:08 +1100451 if (new_bp) {
452 _xfs_buf_initialize(new_bp, btp, range_base,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 range_length, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100454 new_bp->b_hash = hash;
455 list_add(&new_bp->b_hash_list, &hash->bh_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100457 XFS_STATS_INC(xb_miss_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
459
460 spin_unlock(&hash->bh_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +1100461 return new_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463found:
464 spin_unlock(&hash->bh_lock);
465
466 /* Attempt to get the semaphore without sleeping,
467 * if this does not work then we need to drop the
468 * spinlock and do a hard attempt on the semaphore.
469 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100470 if (down_trylock(&bp->b_sema)) {
471 if (!(flags & XBF_TRYLOCK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 /* wait for buffer ownership */
Nathan Scottce8e9222006-01-11 15:39:08 +1100473 XB_TRACE(bp, "get_lock", 0);
474 xfs_buf_lock(bp);
475 XFS_STATS_INC(xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 } else {
477 /* We asked for a trylock and failed, no need
478 * to look at file offset and length here, we
Nathan Scottce8e9222006-01-11 15:39:08 +1100479 * know that this buffer at least overlaps our
480 * buffer and is locked, therefore our buffer
481 * either does not exist, or is this buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100483 xfs_buf_rele(bp);
484 XFS_STATS_INC(xb_busy_locked);
485 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487 } else {
488 /* trylock worked */
Nathan Scottce8e9222006-01-11 15:39:08 +1100489 XB_SET_OWNER(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
491
Nathan Scottce8e9222006-01-11 15:39:08 +1100492 if (bp->b_flags & XBF_STALE) {
493 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
494 bp->b_flags &= XBF_MAPPED;
David Chinner2f926582005-09-05 08:33:35 +1000495 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100496 XB_TRACE(bp, "got_lock", 0);
497 XFS_STATS_INC(xb_get_locked);
498 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
501/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100502 * Assembles a buffer covering the specified range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 * Storage in memory for all portions of the buffer will be allocated,
504 * although backing storage may not be.
505 */
506xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100507xfs_buf_get_flags(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 xfs_buftarg_t *target,/* target for buffer */
Nathan Scott204ab252006-01-11 20:50:22 +1100509 xfs_off_t ioff, /* starting offset of range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 size_t isize, /* length of range */
Nathan Scottce8e9222006-01-11 15:39:08 +1100511 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Nathan Scottce8e9222006-01-11 15:39:08 +1100513 xfs_buf_t *bp, *new_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 int error = 0, i;
515
Nathan Scottce8e9222006-01-11 15:39:08 +1100516 new_bp = xfs_buf_allocate(flags);
517 if (unlikely(!new_bp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return NULL;
519
Nathan Scottce8e9222006-01-11 15:39:08 +1100520 bp = _xfs_buf_find(target, ioff, isize, flags, new_bp);
521 if (bp == new_bp) {
522 error = _xfs_buf_lookup_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (error)
524 goto no_buffer;
525 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100526 xfs_buf_deallocate(new_bp);
527 if (unlikely(bp == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return NULL;
529 }
530
Nathan Scottce8e9222006-01-11 15:39:08 +1100531 for (i = 0; i < bp->b_page_count; i++)
532 mark_page_accessed(bp->b_pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Nathan Scottce8e9222006-01-11 15:39:08 +1100534 if (!(bp->b_flags & XBF_MAPPED)) {
535 error = _xfs_buf_map_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (unlikely(error)) {
537 printk(KERN_WARNING "%s: failed to map pages\n",
Harvey Harrison34a622b2008-04-10 12:19:21 +1000538 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 goto no_buffer;
540 }
541 }
542
Nathan Scottce8e9222006-01-11 15:39:08 +1100543 XFS_STATS_INC(xb_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 /*
546 * Always fill in the block number now, the mapped cases can do
547 * their own overlay of this later.
548 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100549 bp->b_bn = ioff;
550 bp->b_count_desired = bp->b_buffer_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Nathan Scottce8e9222006-01-11 15:39:08 +1100552 XB_TRACE(bp, "get", (unsigned long)flags);
553 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100556 if (flags & (XBF_LOCK | XBF_TRYLOCK))
557 xfs_buf_unlock(bp);
558 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return NULL;
560}
561
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100562STATIC int
563_xfs_buf_read(
564 xfs_buf_t *bp,
565 xfs_buf_flags_t flags)
566{
567 int status;
568
569 XB_TRACE(bp, "_xfs_buf_read", (unsigned long)flags);
570
571 ASSERT(!(flags & (XBF_DELWRI|XBF_WRITE)));
572 ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL);
573
574 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_DELWRI | \
575 XBF_READ_AHEAD | _XBF_RUN_QUEUES);
576 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | \
577 XBF_READ_AHEAD | _XBF_RUN_QUEUES);
578
579 status = xfs_buf_iorequest(bp);
580 if (!status && !(flags & XBF_ASYNC))
581 status = xfs_buf_iowait(bp);
582 return status;
583}
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585xfs_buf_t *
586xfs_buf_read_flags(
587 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100588 xfs_off_t ioff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 size_t isize,
Nathan Scottce8e9222006-01-11 15:39:08 +1100590 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Nathan Scottce8e9222006-01-11 15:39:08 +1100592 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Nathan Scottce8e9222006-01-11 15:39:08 +1100594 flags |= XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Nathan Scottce8e9222006-01-11 15:39:08 +1100596 bp = xfs_buf_get_flags(target, ioff, isize, flags);
597 if (bp) {
598 if (!XFS_BUF_ISDONE(bp)) {
599 XB_TRACE(bp, "read", (unsigned long)flags);
600 XFS_STATS_INC(xb_get_read);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100601 _xfs_buf_read(bp, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100602 } else if (flags & XBF_ASYNC) {
603 XB_TRACE(bp, "read_async", (unsigned long)flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 /*
605 * Read ahead call which is already satisfied,
606 * drop the buffer
607 */
608 goto no_buffer;
609 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100610 XB_TRACE(bp, "read_done", (unsigned long)flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 /* We do not want read in the flags */
Nathan Scottce8e9222006-01-11 15:39:08 +1100612 bp->b_flags &= ~XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
614 }
615
Nathan Scottce8e9222006-01-11 15:39:08 +1100616 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
625/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100626 * If we are not low on memory then do the readahead in a deadlock
627 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 */
629void
Nathan Scottce8e9222006-01-11 15:39:08 +1100630xfs_buf_readahead(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100632 xfs_off_t ioff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 size_t isize,
Nathan Scottce8e9222006-01-11 15:39:08 +1100634 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
636 struct backing_dev_info *bdi;
637
Nathan Scottce8e9222006-01-11 15:39:08 +1100638 bdi = target->bt_mapping->backing_dev_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (bdi_read_congested(bdi))
640 return;
641
Nathan Scottce8e9222006-01-11 15:39:08 +1100642 flags |= (XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 xfs_buf_read_flags(target, ioff, isize, flags);
644}
645
646xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100647xfs_buf_get_empty(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 size_t len,
649 xfs_buftarg_t *target)
650{
Nathan Scottce8e9222006-01-11 15:39:08 +1100651 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Nathan Scottce8e9222006-01-11 15:39:08 +1100653 bp = xfs_buf_allocate(0);
654 if (bp)
655 _xfs_buf_initialize(bp, target, 0, len, 0);
656 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
658
659static inline struct page *
660mem_to_page(
661 void *addr)
662{
Christoph Lameter9e2779f2008-02-04 22:28:34 -0800663 if ((!is_vmalloc_addr(addr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return virt_to_page(addr);
665 } else {
666 return vmalloc_to_page(addr);
667 }
668}
669
670int
Nathan Scottce8e9222006-01-11 15:39:08 +1100671xfs_buf_associate_memory(
672 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 void *mem,
674 size_t len)
675{
676 int rval;
677 int i = 0;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100678 unsigned long pageaddr;
679 unsigned long offset;
680 size_t buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 int page_count;
682
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100683 pageaddr = (unsigned long)mem & PAGE_CACHE_MASK;
684 offset = (unsigned long)mem - pageaddr;
685 buflen = PAGE_CACHE_ALIGN(len + offset);
686 page_count = buflen >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 /* Free any previous set of page pointers */
Nathan Scottce8e9222006-01-11 15:39:08 +1100689 if (bp->b_pages)
690 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Nathan Scottce8e9222006-01-11 15:39:08 +1100692 bp->b_pages = NULL;
693 bp->b_addr = mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Nathan Scottce8e9222006-01-11 15:39:08 +1100695 rval = _xfs_buf_get_pages(bp, page_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (rval)
697 return rval;
698
Nathan Scottce8e9222006-01-11 15:39:08 +1100699 bp->b_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100701 for (i = 0; i < bp->b_page_count; i++) {
702 bp->b_pages[i] = mem_to_page((void *)pageaddr);
703 pageaddr += PAGE_CACHE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100706 bp->b_count_desired = len;
707 bp->b_buffer_length = buflen;
Nathan Scottce8e9222006-01-11 15:39:08 +1100708 bp->b_flags |= XBF_MAPPED;
Christoph Hellwig6ab455e2008-05-19 16:34:42 +1000709 bp->b_flags &= ~_XBF_PAGE_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 return 0;
712}
713
714xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100715xfs_buf_get_noaddr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 size_t len,
717 xfs_buftarg_t *target)
718{
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000719 unsigned long page_count = PAGE_ALIGN(len) >> PAGE_SHIFT;
720 int error, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Nathan Scottce8e9222006-01-11 15:39:08 +1100723 bp = xfs_buf_allocate(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (unlikely(bp == NULL))
725 goto fail;
Nathan Scottce8e9222006-01-11 15:39:08 +1100726 _xfs_buf_initialize(bp, target, 0, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000728 error = _xfs_buf_get_pages(bp, page_count, 0);
729 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 goto fail_free_buf;
731
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000732 for (i = 0; i < page_count; i++) {
733 bp->b_pages[i] = alloc_page(GFP_KERNEL);
734 if (!bp->b_pages[i])
735 goto fail_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000737 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000739 error = _xfs_buf_map_pages(bp, XBF_MAPPED);
740 if (unlikely(error)) {
741 printk(KERN_WARNING "%s: failed to map pages\n",
Harvey Harrison34a622b2008-04-10 12:19:21 +1000742 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 goto fail_free_mem;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Nathan Scottce8e9222006-01-11 15:39:08 +1100746 xfs_buf_unlock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000748 XB_TRACE(bp, "no_daddr", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 return bp;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 fail_free_mem:
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000752 while (--i >= 0)
753 __free_page(bp->b_pages[i]);
Christoph Hellwigca165b82007-05-24 15:21:11 +1000754 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 fail_free_buf:
Christoph Hellwigca165b82007-05-24 15:21:11 +1000756 xfs_buf_deallocate(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 fail:
758 return NULL;
759}
760
761/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 * Increment reference count on buffer, to hold the buffer concurrently
763 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 * Must hold the buffer already to call this function.
765 */
766void
Nathan Scottce8e9222006-01-11 15:39:08 +1100767xfs_buf_hold(
768 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
Nathan Scottce8e9222006-01-11 15:39:08 +1100770 atomic_inc(&bp->b_hold);
771 XB_TRACE(bp, "hold", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
774/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100775 * Releases a hold on the specified buffer. If the
776 * the hold count is 1, calls xfs_buf_free.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 */
778void
Nathan Scottce8e9222006-01-11 15:39:08 +1100779xfs_buf_rele(
780 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Nathan Scottce8e9222006-01-11 15:39:08 +1100782 xfs_bufhash_t *hash = bp->b_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Nathan Scottce8e9222006-01-11 15:39:08 +1100784 XB_TRACE(bp, "rele", bp->b_relse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Nathan Scottfad3aa12006-02-01 12:14:52 +1100786 if (unlikely(!hash)) {
787 ASSERT(!bp->b_relse);
788 if (atomic_dec_and_test(&bp->b_hold))
789 xfs_buf_free(bp);
790 return;
791 }
792
Lachlan McIlroy37906892008-08-13 15:42:10 +1000793 ASSERT(atomic_read(&bp->b_hold) > 0);
Nathan Scottce8e9222006-01-11 15:39:08 +1100794 if (atomic_dec_and_lock(&bp->b_hold, &hash->bh_lock)) {
795 if (bp->b_relse) {
796 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 spin_unlock(&hash->bh_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +1100798 (*(bp->b_relse)) (bp);
799 } else if (bp->b_flags & XBF_FS_MANAGED) {
Christoph Hellwig7f14d0a2005-11-02 15:09:35 +1100800 spin_unlock(&hash->bh_lock);
801 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100802 ASSERT(!(bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)));
803 list_del_init(&bp->b_hash_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 spin_unlock(&hash->bh_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +1100805 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
807 }
808}
809
810
811/*
812 * Mutual exclusion on buffers. Locking model:
813 *
814 * Buffers associated with inodes for which buffer locking
815 * is not enabled are not protected by semaphores, and are
816 * assumed to be exclusively owned by the caller. There is a
817 * spinlock in the buffer, used by the caller when concurrent
818 * access is possible.
819 */
820
821/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100822 * Locks a buffer object, if it is not already locked.
823 * Note that this in no way locks the underlying pages, so it is only
824 * useful for synchronizing concurrent use of buffer objects, not for
825 * synchronizing independent access to the underlying pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 */
827int
Nathan Scottce8e9222006-01-11 15:39:08 +1100828xfs_buf_cond_lock(
829 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
831 int locked;
832
Nathan Scottce8e9222006-01-11 15:39:08 +1100833 locked = down_trylock(&bp->b_sema) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (locked) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100835 XB_SET_OWNER(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100837 XB_TRACE(bp, "cond_lock", (long)locked);
838 return locked ? 0 : -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
841#if defined(DEBUG) || defined(XFS_BLI_TRACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842int
Nathan Scottce8e9222006-01-11 15:39:08 +1100843xfs_buf_lock_value(
844 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
Stephen Rothwelladaa6932008-04-22 15:26:13 +1000846 return bp->b_sema.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848#endif
849
850/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100851 * Locks a buffer object.
852 * Note that this in no way locks the underlying pages, so it is only
853 * useful for synchronizing concurrent use of buffer objects, not for
854 * synchronizing independent access to the underlying pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100856void
857xfs_buf_lock(
858 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
Nathan Scottce8e9222006-01-11 15:39:08 +1100860 XB_TRACE(bp, "lock", 0);
861 if (atomic_read(&bp->b_io_remaining))
862 blk_run_address_space(bp->b_target->bt_mapping);
863 down(&bp->b_sema);
864 XB_SET_OWNER(bp);
865 XB_TRACE(bp, "locked", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}
867
868/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100869 * Releases the lock on the buffer object.
David Chinner2f926582005-09-05 08:33:35 +1000870 * If the buffer is marked delwri but is not queued, do so before we
Nathan Scottce8e9222006-01-11 15:39:08 +1100871 * unlock the buffer as we need to set flags correctly. We also need to
David Chinner2f926582005-09-05 08:33:35 +1000872 * take a reference for the delwri queue because the unlocker is going to
873 * drop their's and they don't know we just queued it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 */
875void
Nathan Scottce8e9222006-01-11 15:39:08 +1100876xfs_buf_unlock(
877 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Nathan Scottce8e9222006-01-11 15:39:08 +1100879 if ((bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)) == XBF_DELWRI) {
880 atomic_inc(&bp->b_hold);
881 bp->b_flags |= XBF_ASYNC;
882 xfs_buf_delwri_queue(bp, 0);
David Chinner2f926582005-09-05 08:33:35 +1000883 }
884
Nathan Scottce8e9222006-01-11 15:39:08 +1100885 XB_CLEAR_OWNER(bp);
886 up(&bp->b_sema);
887 XB_TRACE(bp, "unlock", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
889
890
891/*
892 * Pinning Buffer Storage in Memory
Nathan Scottce8e9222006-01-11 15:39:08 +1100893 * Ensure that no attempt to force a buffer to disk will succeed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 */
895void
Nathan Scottce8e9222006-01-11 15:39:08 +1100896xfs_buf_pin(
897 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Nathan Scottce8e9222006-01-11 15:39:08 +1100899 atomic_inc(&bp->b_pin_count);
900 XB_TRACE(bp, "pin", (long)bp->b_pin_count.counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903void
Nathan Scottce8e9222006-01-11 15:39:08 +1100904xfs_buf_unpin(
905 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Nathan Scottce8e9222006-01-11 15:39:08 +1100907 if (atomic_dec_and_test(&bp->b_pin_count))
908 wake_up_all(&bp->b_waiters);
909 XB_TRACE(bp, "unpin", (long)bp->b_pin_count.counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910}
911
912int
Nathan Scottce8e9222006-01-11 15:39:08 +1100913xfs_buf_ispin(
914 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
Nathan Scottce8e9222006-01-11 15:39:08 +1100916 return atomic_read(&bp->b_pin_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917}
918
Nathan Scottce8e9222006-01-11 15:39:08 +1100919STATIC void
920xfs_buf_wait_unpin(
921 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
923 DECLARE_WAITQUEUE (wait, current);
924
Nathan Scottce8e9222006-01-11 15:39:08 +1100925 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return;
927
Nathan Scottce8e9222006-01-11 15:39:08 +1100928 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 for (;;) {
930 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +1100931 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 break;
Nathan Scottce8e9222006-01-11 15:39:08 +1100933 if (atomic_read(&bp->b_io_remaining))
934 blk_run_address_space(bp->b_target->bt_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 schedule();
936 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100937 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 set_current_state(TASK_RUNNING);
939}
940
941/*
942 * Buffer Utility Routines
943 */
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100946xfs_buf_iodone_work(
David Howellsc4028952006-11-22 14:57:56 +0000947 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
David Howellsc4028952006-11-22 14:57:56 +0000949 xfs_buf_t *bp =
950 container_of(work, xfs_buf_t, b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
David Chinner0bfefc42007-05-14 18:24:23 +1000952 /*
953 * We can get an EOPNOTSUPP to ordered writes. Here we clear the
954 * ordered flag and reissue them. Because we can't tell the higher
955 * layers directly that they should not issue ordered I/O anymore, they
Christoph Hellwig73f6aa42008-10-10 17:28:29 +1100956 * need to check if the _XFS_BARRIER_FAILED flag was set during I/O completion.
David Chinner0bfefc42007-05-14 18:24:23 +1000957 */
958 if ((bp->b_error == EOPNOTSUPP) &&
959 (bp->b_flags & (XBF_ORDERED|XBF_ASYNC)) == (XBF_ORDERED|XBF_ASYNC)) {
960 XB_TRACE(bp, "ordered_retry", bp->b_iodone);
961 bp->b_flags &= ~XBF_ORDERED;
Christoph Hellwig73f6aa42008-10-10 17:28:29 +1100962 bp->b_flags |= _XFS_BARRIER_FAILED;
David Chinner0bfefc42007-05-14 18:24:23 +1000963 xfs_buf_iorequest(bp);
964 } else if (bp->b_iodone)
Nathan Scottce8e9222006-01-11 15:39:08 +1100965 (*(bp->b_iodone))(bp);
966 else if (bp->b_flags & XBF_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 xfs_buf_relse(bp);
968}
969
970void
Nathan Scottce8e9222006-01-11 15:39:08 +1100971xfs_buf_ioend(
972 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 int schedule)
974{
Lachlan McIlroy77be55a2007-11-23 16:31:00 +1100975 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
Nathan Scottce8e9222006-01-11 15:39:08 +1100976 if (bp->b_error == 0)
977 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Nathan Scottce8e9222006-01-11 15:39:08 +1100979 XB_TRACE(bp, "iodone", bp->b_iodone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Nathan Scottce8e9222006-01-11 15:39:08 +1100981 if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 if (schedule) {
David Howellsc4028952006-11-22 14:57:56 +0000983 INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
Nathan Scottce8e9222006-01-11 15:39:08 +1100984 queue_work(xfslogd_workqueue, &bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 } else {
David Howellsc4028952006-11-22 14:57:56 +0000986 xfs_buf_iodone_work(&bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
988 } else {
David Chinnerb4dd3302008-08-13 16:36:11 +1000989 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
991}
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993void
Nathan Scottce8e9222006-01-11 15:39:08 +1100994xfs_buf_ioerror(
995 xfs_buf_t *bp,
996 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
998 ASSERT(error >= 0 && error <= 0xffff);
Nathan Scottce8e9222006-01-11 15:39:08 +1100999 bp->b_error = (unsigned short)error;
1000 XB_TRACE(bp, "ioerror", (unsigned long)error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001}
1002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003int
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001004xfs_bawrite(
1005 void *mp,
1006 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001008 XB_TRACE(bp, "bawrite", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001010 ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001012 xfs_buf_delwri_dequeue(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001014 bp->b_flags &= ~(XBF_READ | XBF_DELWRI | XBF_READ_AHEAD);
1015 bp->b_flags |= (XBF_WRITE | XBF_ASYNC | _XBF_RUN_QUEUES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Christoph Hellwig15ac08a2008-12-09 04:47:30 -05001017 bp->b_mount = mp;
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001018 bp->b_strat = xfs_bdstrat_cb;
1019 return xfs_bdstrat_cb(bp);
1020}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001022void
1023xfs_bdwrite(
1024 void *mp,
1025 struct xfs_buf *bp)
1026{
1027 XB_TRACE(bp, "bdwrite", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001029 bp->b_strat = xfs_bdstrat_cb;
Christoph Hellwig15ac08a2008-12-09 04:47:30 -05001030 bp->b_mount = mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001032 bp->b_flags &= ~XBF_READ;
1033 bp->b_flags |= (XBF_DELWRI | XBF_ASYNC);
1034
1035 xfs_buf_delwri_queue(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037
David Chinner7989cb82007-02-10 18:34:56 +11001038STATIC_INLINE void
Nathan Scottce8e9222006-01-11 15:39:08 +11001039_xfs_buf_ioend(
1040 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 int schedule)
1042{
Christoph Hellwig6ab455e2008-05-19 16:34:42 +10001043 if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
1044 bp->b_flags &= ~_XBF_PAGE_LOCKED;
Nathan Scottce8e9222006-01-11 15:39:08 +11001045 xfs_buf_ioend(bp, schedule);
Christoph Hellwig6ab455e2008-05-19 16:34:42 +10001046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047}
1048
Al Viro782e3b32007-10-12 07:17:47 +01001049STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001050xfs_buf_bio_end_io(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 struct bio *bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 int error)
1053{
Nathan Scottce8e9222006-01-11 15:39:08 +11001054 xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
1055 unsigned int blocksize = bp->b_target->bt_bsize;
Nathan Scotteedb5532005-09-02 16:39:56 +10001056 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Lachlan McIlroycfbe5262008-12-12 15:27:25 +11001058 xfs_buf_ioerror(bp, -error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Nathan Scotteedb5532005-09-02 16:39:56 +10001060 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 struct page *page = bvec->bv_page;
1062
Nathan Scott948ecdb2006-09-28 11:03:13 +10001063 ASSERT(!PagePrivate(page));
Nathan Scottce8e9222006-01-11 15:39:08 +11001064 if (unlikely(bp->b_error)) {
1065 if (bp->b_flags & XBF_READ)
Nathan Scotteedb5532005-09-02 16:39:56 +10001066 ClearPageUptodate(page);
Nathan Scottce8e9222006-01-11 15:39:08 +11001067 } else if (blocksize >= PAGE_CACHE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 SetPageUptodate(page);
1069 } else if (!PagePrivate(page) &&
Nathan Scottce8e9222006-01-11 15:39:08 +11001070 (bp->b_flags & _XBF_PAGE_CACHE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 set_page_region(page, bvec->bv_offset, bvec->bv_len);
1072 }
1073
Nathan Scotteedb5532005-09-02 16:39:56 +10001074 if (--bvec >= bio->bi_io_vec)
1075 prefetchw(&bvec->bv_page->flags);
Christoph Hellwig6ab455e2008-05-19 16:34:42 +10001076
1077 if (bp->b_flags & _XBF_PAGE_LOCKED)
1078 unlock_page(page);
Nathan Scotteedb5532005-09-02 16:39:56 +10001079 } while (bvec >= bio->bi_io_vec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Nathan Scottce8e9222006-01-11 15:39:08 +11001081 _xfs_buf_ioend(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083}
1084
1085STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001086_xfs_buf_ioapply(
1087 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
Christoph Hellwiga9759f22007-12-07 14:07:08 +11001089 int rw, map_i, total_nr_pages, nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 struct bio *bio;
Nathan Scottce8e9222006-01-11 15:39:08 +11001091 int offset = bp->b_offset;
1092 int size = bp->b_count_desired;
1093 sector_t sector = bp->b_bn;
1094 unsigned int blocksize = bp->b_target->bt_bsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Nathan Scottce8e9222006-01-11 15:39:08 +11001096 total_nr_pages = bp->b_page_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 map_i = 0;
1098
Nathan Scottce8e9222006-01-11 15:39:08 +11001099 if (bp->b_flags & XBF_ORDERED) {
1100 ASSERT(!(bp->b_flags & XBF_READ));
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001101 rw = WRITE_BARRIER;
Nathan Scott51bdd702006-09-28 11:01:57 +10001102 } else if (bp->b_flags & _XBF_RUN_QUEUES) {
1103 ASSERT(!(bp->b_flags & XBF_READ_AHEAD));
1104 bp->b_flags &= ~_XBF_RUN_QUEUES;
1105 rw = (bp->b_flags & XBF_WRITE) ? WRITE_SYNC : READ_SYNC;
1106 } else {
1107 rw = (bp->b_flags & XBF_WRITE) ? WRITE :
1108 (bp->b_flags & XBF_READ_AHEAD) ? READA : READ;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001109 }
1110
Nathan Scottce8e9222006-01-11 15:39:08 +11001111 /* Special code path for reading a sub page size buffer in --
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 * we populate up the whole page, and hence the other metadata
1113 * in the same page. This optimization is only valid when the
Nathan Scottce8e9222006-01-11 15:39:08 +11001114 * filesystem block size is not smaller than the page size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001116 if ((bp->b_buffer_length < PAGE_CACHE_SIZE) &&
Christoph Hellwig6ab455e2008-05-19 16:34:42 +10001117 ((bp->b_flags & (XBF_READ|_XBF_PAGE_LOCKED)) ==
1118 (XBF_READ|_XBF_PAGE_LOCKED)) &&
Nathan Scottce8e9222006-01-11 15:39:08 +11001119 (blocksize >= PAGE_CACHE_SIZE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 bio = bio_alloc(GFP_NOIO, 1);
1121
Nathan Scottce8e9222006-01-11 15:39:08 +11001122 bio->bi_bdev = bp->b_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 bio->bi_sector = sector - (offset >> BBSHIFT);
Nathan Scottce8e9222006-01-11 15:39:08 +11001124 bio->bi_end_io = xfs_buf_bio_end_io;
1125 bio->bi_private = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Nathan Scottce8e9222006-01-11 15:39:08 +11001127 bio_add_page(bio, bp->b_pages[0], PAGE_CACHE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 size = 0;
1129
Nathan Scottce8e9222006-01-11 15:39:08 +11001130 atomic_inc(&bp->b_io_remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 goto submit_io;
1133 }
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001136 atomic_inc(&bp->b_io_remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
1138 if (nr_pages > total_nr_pages)
1139 nr_pages = total_nr_pages;
1140
1141 bio = bio_alloc(GFP_NOIO, nr_pages);
Nathan Scottce8e9222006-01-11 15:39:08 +11001142 bio->bi_bdev = bp->b_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 bio->bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001144 bio->bi_end_io = xfs_buf_bio_end_io;
1145 bio->bi_private = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147 for (; size && nr_pages; nr_pages--, map_i++) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001148 int rbytes, nbytes = PAGE_CACHE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150 if (nbytes > size)
1151 nbytes = size;
1152
Nathan Scottce8e9222006-01-11 15:39:08 +11001153 rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset);
1154 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 break;
1156
1157 offset = 0;
1158 sector += nbytes >> BBSHIFT;
1159 size -= nbytes;
1160 total_nr_pages--;
1161 }
1162
1163submit_io:
1164 if (likely(bio->bi_size)) {
1165 submit_bio(rw, bio);
1166 if (size)
1167 goto next_chunk;
1168 } else {
1169 bio_put(bio);
Nathan Scottce8e9222006-01-11 15:39:08 +11001170 xfs_buf_ioerror(bp, EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
1172}
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174int
Nathan Scottce8e9222006-01-11 15:39:08 +11001175xfs_buf_iorequest(
1176 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
Nathan Scottce8e9222006-01-11 15:39:08 +11001178 XB_TRACE(bp, "iorequest", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Nathan Scottce8e9222006-01-11 15:39:08 +11001180 if (bp->b_flags & XBF_DELWRI) {
1181 xfs_buf_delwri_queue(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return 0;
1183 }
1184
Nathan Scottce8e9222006-01-11 15:39:08 +11001185 if (bp->b_flags & XBF_WRITE) {
1186 xfs_buf_wait_unpin(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
1188
Nathan Scottce8e9222006-01-11 15:39:08 +11001189 xfs_buf_hold(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 /* Set the count to 1 initially, this will stop an I/O
1192 * completion callout which happens before we have started
Nathan Scottce8e9222006-01-11 15:39:08 +11001193 * all the I/O from calling xfs_buf_ioend too early.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001195 atomic_set(&bp->b_io_remaining, 1);
1196 _xfs_buf_ioapply(bp);
1197 _xfs_buf_ioend(bp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Nathan Scottce8e9222006-01-11 15:39:08 +11001199 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 return 0;
1201}
1202
1203/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001204 * Waits for I/O to complete on the buffer supplied.
1205 * It returns immediately if no I/O is pending.
1206 * It returns the I/O error code, if any, or 0 if there was no error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 */
1208int
Nathan Scottce8e9222006-01-11 15:39:08 +11001209xfs_buf_iowait(
1210 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Nathan Scottce8e9222006-01-11 15:39:08 +11001212 XB_TRACE(bp, "iowait", 0);
1213 if (atomic_read(&bp->b_io_remaining))
1214 blk_run_address_space(bp->b_target->bt_mapping);
David Chinnerb4dd3302008-08-13 16:36:11 +10001215 wait_for_completion(&bp->b_iowait);
Nathan Scottce8e9222006-01-11 15:39:08 +11001216 XB_TRACE(bp, "iowaited", (long)bp->b_error);
1217 return bp->b_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
1219
Nathan Scottce8e9222006-01-11 15:39:08 +11001220xfs_caddr_t
1221xfs_buf_offset(
1222 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 size_t offset)
1224{
1225 struct page *page;
1226
Nathan Scottce8e9222006-01-11 15:39:08 +11001227 if (bp->b_flags & XBF_MAPPED)
1228 return XFS_BUF_PTR(bp) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Nathan Scottce8e9222006-01-11 15:39:08 +11001230 offset += bp->b_offset;
1231 page = bp->b_pages[offset >> PAGE_CACHE_SHIFT];
1232 return (xfs_caddr_t)page_address(page) + (offset & (PAGE_CACHE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233}
1234
1235/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 * Move data into or out of a buffer.
1237 */
1238void
Nathan Scottce8e9222006-01-11 15:39:08 +11001239xfs_buf_iomove(
1240 xfs_buf_t *bp, /* buffer to process */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 size_t boff, /* starting buffer offset */
1242 size_t bsize, /* length to copy */
1243 caddr_t data, /* data address */
Nathan Scottce8e9222006-01-11 15:39:08 +11001244 xfs_buf_rw_t mode) /* read/write/zero flag */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
1246 size_t bend, cpoff, csize;
1247 struct page *page;
1248
1249 bend = boff + bsize;
1250 while (boff < bend) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001251 page = bp->b_pages[xfs_buf_btoct(boff + bp->b_offset)];
1252 cpoff = xfs_buf_poff(boff + bp->b_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 csize = min_t(size_t,
Nathan Scottce8e9222006-01-11 15:39:08 +11001254 PAGE_CACHE_SIZE-cpoff, bp->b_count_desired-boff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256 ASSERT(((csize + cpoff) <= PAGE_CACHE_SIZE));
1257
1258 switch (mode) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001259 case XBRW_ZERO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 memset(page_address(page) + cpoff, 0, csize);
1261 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001262 case XBRW_READ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 memcpy(data, page_address(page) + cpoff, csize);
1264 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001265 case XBRW_WRITE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 memcpy(page_address(page) + cpoff, data, csize);
1267 }
1268
1269 boff += csize;
1270 data += csize;
1271 }
1272}
1273
1274/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001275 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 */
1277
1278/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001279 * Wait for any bufs with callbacks that have been submitted but
1280 * have not yet returned... walk the hash list for the target.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 */
1282void
1283xfs_wait_buftarg(
1284 xfs_buftarg_t *btp)
1285{
1286 xfs_buf_t *bp, *n;
1287 xfs_bufhash_t *hash;
1288 uint i;
1289
1290 for (i = 0; i < (1 << btp->bt_hashshift); i++) {
1291 hash = &btp->bt_hash[i];
1292again:
1293 spin_lock(&hash->bh_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +11001294 list_for_each_entry_safe(bp, n, &hash->bh_list, b_hash_list) {
1295 ASSERT(btp == bp->b_target);
1296 if (!(bp->b_flags & XBF_FS_MANAGED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 spin_unlock(&hash->bh_lock);
David Chinner2f926582005-09-05 08:33:35 +10001298 /*
1299 * Catch superblock reference count leaks
1300 * immediately
1301 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001302 BUG_ON(bp->b_bn == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 delay(100);
1304 goto again;
1305 }
1306 }
1307 spin_unlock(&hash->bh_lock);
1308 }
1309}
1310
1311/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001312 * Allocate buffer hash table for a given target.
1313 * For devices containing metadata (i.e. not the log/realtime devices)
1314 * we need to allocate a much larger hash table.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 */
1316STATIC void
1317xfs_alloc_bufhash(
1318 xfs_buftarg_t *btp,
1319 int external)
1320{
1321 unsigned int i;
1322
1323 btp->bt_hashshift = external ? 3 : 8; /* 8 or 256 buckets */
1324 btp->bt_hashmask = (1 << btp->bt_hashshift) - 1;
1325 btp->bt_hash = kmem_zalloc((1 << btp->bt_hashshift) *
Vlad Apostolov93c189c2006-11-11 18:03:49 +11001326 sizeof(xfs_bufhash_t), KM_SLEEP | KM_LARGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 for (i = 0; i < (1 << btp->bt_hashshift); i++) {
1328 spin_lock_init(&btp->bt_hash[i].bh_lock);
1329 INIT_LIST_HEAD(&btp->bt_hash[i].bh_list);
1330 }
1331}
1332
1333STATIC void
1334xfs_free_bufhash(
1335 xfs_buftarg_t *btp)
1336{
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001337 kmem_free(btp->bt_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 btp->bt_hash = NULL;
1339}
1340
David Chinnera6867a62006-01-11 15:37:58 +11001341/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001342 * buftarg list for delwrite queue processing
David Chinnera6867a62006-01-11 15:37:58 +11001343 */
Tim Shimmine6a0e9c2007-05-08 13:49:59 +10001344static LIST_HEAD(xfs_buftarg_list);
David Chinner7989cb82007-02-10 18:34:56 +11001345static DEFINE_SPINLOCK(xfs_buftarg_lock);
David Chinnera6867a62006-01-11 15:37:58 +11001346
1347STATIC void
1348xfs_register_buftarg(
1349 xfs_buftarg_t *btp)
1350{
1351 spin_lock(&xfs_buftarg_lock);
1352 list_add(&btp->bt_list, &xfs_buftarg_list);
1353 spin_unlock(&xfs_buftarg_lock);
1354}
1355
1356STATIC void
1357xfs_unregister_buftarg(
1358 xfs_buftarg_t *btp)
1359{
1360 spin_lock(&xfs_buftarg_lock);
1361 list_del(&btp->bt_list);
1362 spin_unlock(&xfs_buftarg_lock);
1363}
1364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365void
1366xfs_free_buftarg(
Christoph Hellwig19f354d2008-05-20 11:31:13 +10001367 xfs_buftarg_t *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
1369 xfs_flush_buftarg(btp, 1);
David Chinnerf4a9f282007-06-05 16:24:27 +10001370 xfs_blkdev_issue_flush(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 xfs_free_bufhash(btp);
Nathan Scottce8e9222006-01-11 15:39:08 +11001372 iput(btp->bt_mapping->host);
David Chinnera6867a62006-01-11 15:37:58 +11001373
Nathan Scottce8e9222006-01-11 15:39:08 +11001374 /* Unregister the buftarg first so that we don't get a
1375 * wakeup finding a non-existent task
1376 */
David Chinnera6867a62006-01-11 15:37:58 +11001377 xfs_unregister_buftarg(btp);
1378 kthread_stop(btp->bt_task);
1379
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001380 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381}
1382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383STATIC int
1384xfs_setsize_buftarg_flags(
1385 xfs_buftarg_t *btp,
1386 unsigned int blocksize,
1387 unsigned int sectorsize,
1388 int verbose)
1389{
Nathan Scottce8e9222006-01-11 15:39:08 +11001390 btp->bt_bsize = blocksize;
1391 btp->bt_sshift = ffs(sectorsize) - 1;
1392 btp->bt_smask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Nathan Scottce8e9222006-01-11 15:39:08 +11001394 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 printk(KERN_WARNING
1396 "XFS: Cannot set_blocksize to %u on device %s\n",
1397 sectorsize, XFS_BUFTARG_NAME(btp));
1398 return EINVAL;
1399 }
1400
1401 if (verbose &&
1402 (PAGE_CACHE_SIZE / BITS_PER_LONG) > sectorsize) {
1403 printk(KERN_WARNING
1404 "XFS: %u byte sectors in use on device %s. "
1405 "This is suboptimal; %u or greater is ideal.\n",
1406 sectorsize, XFS_BUFTARG_NAME(btp),
1407 (unsigned int)PAGE_CACHE_SIZE / BITS_PER_LONG);
1408 }
1409
1410 return 0;
1411}
1412
1413/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001414 * When allocating the initial buffer target we have not yet
1415 * read in the superblock, so don't know what sized sectors
1416 * are being used is at this early stage. Play safe.
1417 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418STATIC int
1419xfs_setsize_buftarg_early(
1420 xfs_buftarg_t *btp,
1421 struct block_device *bdev)
1422{
1423 return xfs_setsize_buftarg_flags(btp,
1424 PAGE_CACHE_SIZE, bdev_hardsect_size(bdev), 0);
1425}
1426
1427int
1428xfs_setsize_buftarg(
1429 xfs_buftarg_t *btp,
1430 unsigned int blocksize,
1431 unsigned int sectorsize)
1432{
1433 return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
1434}
1435
1436STATIC int
1437xfs_mapping_buftarg(
1438 xfs_buftarg_t *btp,
1439 struct block_device *bdev)
1440{
1441 struct backing_dev_info *bdi;
1442 struct inode *inode;
1443 struct address_space *mapping;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07001444 static const struct address_space_operations mapping_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 .sync_page = block_sync_page,
Christoph Lametere965f962006-02-01 03:05:41 -08001446 .migratepage = fail_migrate_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 };
1448
1449 inode = new_inode(bdev->bd_inode->i_sb);
1450 if (!inode) {
1451 printk(KERN_WARNING
1452 "XFS: Cannot allocate mapping inode for device %s\n",
1453 XFS_BUFTARG_NAME(btp));
1454 return ENOMEM;
1455 }
1456 inode->i_mode = S_IFBLK;
1457 inode->i_bdev = bdev;
1458 inode->i_rdev = bdev->bd_dev;
1459 bdi = blk_get_backing_dev_info(bdev);
1460 if (!bdi)
1461 bdi = &default_backing_dev_info;
1462 mapping = &inode->i_data;
1463 mapping->a_ops = &mapping_aops;
1464 mapping->backing_dev_info = bdi;
1465 mapping_set_gfp_mask(mapping, GFP_NOFS);
Nathan Scottce8e9222006-01-11 15:39:08 +11001466 btp->bt_mapping = mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return 0;
1468}
1469
David Chinnera6867a62006-01-11 15:37:58 +11001470STATIC int
1471xfs_alloc_delwrite_queue(
1472 xfs_buftarg_t *btp)
1473{
1474 int error = 0;
1475
1476 INIT_LIST_HEAD(&btp->bt_list);
1477 INIT_LIST_HEAD(&btp->bt_delwrite_queue);
Eric Sandeen007c61c2007-10-11 17:43:56 +10001478 spin_lock_init(&btp->bt_delwrite_lock);
David Chinnera6867a62006-01-11 15:37:58 +11001479 btp->bt_flags = 0;
1480 btp->bt_task = kthread_run(xfsbufd, btp, "xfsbufd");
1481 if (IS_ERR(btp->bt_task)) {
1482 error = PTR_ERR(btp->bt_task);
1483 goto out_error;
1484 }
1485 xfs_register_buftarg(btp);
1486out_error:
1487 return error;
1488}
1489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490xfs_buftarg_t *
1491xfs_alloc_buftarg(
1492 struct block_device *bdev,
1493 int external)
1494{
1495 xfs_buftarg_t *btp;
1496
1497 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
1498
Nathan Scottce8e9222006-01-11 15:39:08 +11001499 btp->bt_dev = bdev->bd_dev;
1500 btp->bt_bdev = bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 if (xfs_setsize_buftarg_early(btp, bdev))
1502 goto error;
1503 if (xfs_mapping_buftarg(btp, bdev))
1504 goto error;
David Chinnera6867a62006-01-11 15:37:58 +11001505 if (xfs_alloc_delwrite_queue(btp))
1506 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 xfs_alloc_bufhash(btp, external);
1508 return btp;
1509
1510error:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001511 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 return NULL;
1513}
1514
1515
1516/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001517 * Delayed write buffer handling
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001520xfs_buf_delwri_queue(
1521 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 int unlock)
1523{
Nathan Scottce8e9222006-01-11 15:39:08 +11001524 struct list_head *dwq = &bp->b_target->bt_delwrite_queue;
1525 spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
David Chinnera6867a62006-01-11 15:37:58 +11001526
Nathan Scottce8e9222006-01-11 15:39:08 +11001527 XB_TRACE(bp, "delwri_q", (long)unlock);
1528 ASSERT((bp->b_flags&(XBF_DELWRI|XBF_ASYNC)) == (XBF_DELWRI|XBF_ASYNC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
David Chinnera6867a62006-01-11 15:37:58 +11001530 spin_lock(dwlk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 /* If already in the queue, dequeue and place at tail */
Nathan Scottce8e9222006-01-11 15:39:08 +11001532 if (!list_empty(&bp->b_list)) {
1533 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1534 if (unlock)
1535 atomic_dec(&bp->b_hold);
1536 list_del(&bp->b_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 }
1538
Nathan Scottce8e9222006-01-11 15:39:08 +11001539 bp->b_flags |= _XBF_DELWRI_Q;
1540 list_add_tail(&bp->b_list, dwq);
1541 bp->b_queuetime = jiffies;
David Chinnera6867a62006-01-11 15:37:58 +11001542 spin_unlock(dwlk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
1544 if (unlock)
Nathan Scottce8e9222006-01-11 15:39:08 +11001545 xfs_buf_unlock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546}
1547
1548void
Nathan Scottce8e9222006-01-11 15:39:08 +11001549xfs_buf_delwri_dequeue(
1550 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
Nathan Scottce8e9222006-01-11 15:39:08 +11001552 spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 int dequeued = 0;
1554
David Chinnera6867a62006-01-11 15:37:58 +11001555 spin_lock(dwlk);
Nathan Scottce8e9222006-01-11 15:39:08 +11001556 if ((bp->b_flags & XBF_DELWRI) && !list_empty(&bp->b_list)) {
1557 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1558 list_del_init(&bp->b_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 dequeued = 1;
1560 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001561 bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q);
David Chinnera6867a62006-01-11 15:37:58 +11001562 spin_unlock(dwlk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
1564 if (dequeued)
Nathan Scottce8e9222006-01-11 15:39:08 +11001565 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Nathan Scottce8e9222006-01-11 15:39:08 +11001567 XB_TRACE(bp, "delwri_dq", (long)dequeued);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568}
1569
1570STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001571xfs_buf_runall_queues(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 struct workqueue_struct *queue)
1573{
1574 flush_workqueue(queue);
1575}
1576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577STATIC int
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001578xfsbufd_wakeup(
Nathan Scott15c84a42005-11-04 10:51:01 +11001579 int priority,
1580 gfp_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
Christoph Hellwigda7f93e2006-01-11 20:49:57 +11001582 xfs_buftarg_t *btp;
David Chinnera6867a62006-01-11 15:37:58 +11001583
1584 spin_lock(&xfs_buftarg_lock);
Christoph Hellwigda7f93e2006-01-11 20:49:57 +11001585 list_for_each_entry(btp, &xfs_buftarg_list, bt_list) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001586 if (test_bit(XBT_FORCE_SLEEP, &btp->bt_flags))
David Chinnera6867a62006-01-11 15:37:58 +11001587 continue;
Nathan Scottce8e9222006-01-11 15:39:08 +11001588 set_bit(XBT_FORCE_FLUSH, &btp->bt_flags);
David Chinnera6867a62006-01-11 15:37:58 +11001589 wake_up_process(btp->bt_task);
1590 }
1591 spin_unlock(&xfs_buftarg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 return 0;
1593}
1594
David Chinner585e6d82007-02-10 18:32:29 +11001595/*
1596 * Move as many buffers as specified to the supplied list
1597 * idicating if we skipped any buffers to prevent deadlocks.
1598 */
1599STATIC int
1600xfs_buf_delwri_split(
1601 xfs_buftarg_t *target,
1602 struct list_head *list,
David Chinner5e6a07d2007-02-10 18:34:49 +11001603 unsigned long age)
David Chinner585e6d82007-02-10 18:32:29 +11001604{
1605 xfs_buf_t *bp, *n;
1606 struct list_head *dwq = &target->bt_delwrite_queue;
1607 spinlock_t *dwlk = &target->bt_delwrite_lock;
1608 int skipped = 0;
David Chinner5e6a07d2007-02-10 18:34:49 +11001609 int force;
David Chinner585e6d82007-02-10 18:32:29 +11001610
David Chinner5e6a07d2007-02-10 18:34:49 +11001611 force = test_and_clear_bit(XBT_FORCE_FLUSH, &target->bt_flags);
David Chinner585e6d82007-02-10 18:32:29 +11001612 INIT_LIST_HEAD(list);
1613 spin_lock(dwlk);
1614 list_for_each_entry_safe(bp, n, dwq, b_list) {
1615 XB_TRACE(bp, "walkq1", (long)xfs_buf_ispin(bp));
1616 ASSERT(bp->b_flags & XBF_DELWRI);
1617
1618 if (!xfs_buf_ispin(bp) && !xfs_buf_cond_lock(bp)) {
David Chinner5e6a07d2007-02-10 18:34:49 +11001619 if (!force &&
David Chinner585e6d82007-02-10 18:32:29 +11001620 time_before(jiffies, bp->b_queuetime + age)) {
1621 xfs_buf_unlock(bp);
1622 break;
1623 }
1624
1625 bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q|
1626 _XBF_RUN_QUEUES);
1627 bp->b_flags |= XBF_WRITE;
1628 list_move_tail(&bp->b_list, list);
1629 } else
1630 skipped++;
1631 }
1632 spin_unlock(dwlk);
1633
1634 return skipped;
1635
1636}
1637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638STATIC int
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001639xfsbufd(
David Chinner585e6d82007-02-10 18:32:29 +11001640 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641{
David Chinner585e6d82007-02-10 18:32:29 +11001642 struct list_head tmp;
1643 xfs_buftarg_t *target = (xfs_buftarg_t *)data;
1644 int count;
1645 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 current->flags |= PF_MEMALLOC;
1648
Rafael J. Wysocki978c7b22007-12-07 14:09:02 +11001649 set_freezable();
1650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 do {
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001652 if (unlikely(freezing(current))) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001653 set_bit(XBT_FORCE_SLEEP, &target->bt_flags);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001654 refrigerator();
Nathan Scottabd0cf72005-05-05 13:30:13 -07001655 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +11001656 clear_bit(XBT_FORCE_SLEEP, &target->bt_flags);
Nathan Scottabd0cf72005-05-05 13:30:13 -07001657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Nathan Scott15c84a42005-11-04 10:51:01 +11001659 schedule_timeout_interruptible(
1660 xfs_buf_timer_centisecs * msecs_to_jiffies(10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
David Chinner585e6d82007-02-10 18:32:29 +11001662 xfs_buf_delwri_split(target, &tmp,
David Chinner5e6a07d2007-02-10 18:34:49 +11001663 xfs_buf_age_centisecs * msecs_to_jiffies(10));
David Chinner585e6d82007-02-10 18:32:29 +11001664
Nathan Scottf07c2252006-09-28 10:52:15 +10001665 count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 while (!list_empty(&tmp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001667 bp = list_entry(tmp.next, xfs_buf_t, b_list);
1668 ASSERT(target == bp->b_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Nathan Scottce8e9222006-01-11 15:39:08 +11001670 list_del_init(&bp->b_list);
1671 xfs_buf_iostrategy(bp);
David Chinner585e6d82007-02-10 18:32:29 +11001672 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 }
1674
Nathan Scottf07c2252006-09-28 10:52:15 +10001675 if (count)
1676 blk_run_address_space(target->bt_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Christoph Hellwig4df08c52005-09-05 08:34:18 +10001678 } while (!kthread_should_stop());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Christoph Hellwig4df08c52005-09-05 08:34:18 +10001680 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681}
1682
1683/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001684 * Go through all incore buffers, and release buffers if they belong to
1685 * the given device. This is used in filesystem error handling to
1686 * preserve the consistency of its metadata.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 */
1688int
1689xfs_flush_buftarg(
David Chinner585e6d82007-02-10 18:32:29 +11001690 xfs_buftarg_t *target,
1691 int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
David Chinner585e6d82007-02-10 18:32:29 +11001693 struct list_head tmp;
1694 xfs_buf_t *bp, *n;
1695 int pincount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Nathan Scottce8e9222006-01-11 15:39:08 +11001697 xfs_buf_runall_queues(xfsdatad_workqueue);
1698 xfs_buf_runall_queues(xfslogd_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
David Chinner5e6a07d2007-02-10 18:34:49 +11001700 set_bit(XBT_FORCE_FLUSH, &target->bt_flags);
1701 pincount = xfs_buf_delwri_split(target, &tmp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
1703 /*
1704 * Dropped the delayed write list lock, now walk the temporary list
1705 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001706 list_for_each_entry_safe(bp, n, &tmp, b_list) {
David Chinner585e6d82007-02-10 18:32:29 +11001707 ASSERT(target == bp->b_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 if (wait)
Nathan Scottce8e9222006-01-11 15:39:08 +11001709 bp->b_flags &= ~XBF_ASYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 else
Nathan Scottce8e9222006-01-11 15:39:08 +11001711 list_del_init(&bp->b_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Nathan Scottce8e9222006-01-11 15:39:08 +11001713 xfs_buf_iostrategy(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 }
1715
Nathan Scottf07c2252006-09-28 10:52:15 +10001716 if (wait)
1717 blk_run_address_space(target->bt_mapping);
1718
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 /*
1720 * Remaining list items must be flushed before returning
1721 */
1722 while (!list_empty(&tmp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001723 bp = list_entry(tmp.next, xfs_buf_t, b_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Nathan Scottce8e9222006-01-11 15:39:08 +11001725 list_del_init(&bp->b_list);
1726 xfs_iowait(bp);
1727 xfs_buf_relse(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 }
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 return pincount;
1731}
1732
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001733int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11001734xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
Nathan Scottce8e9222006-01-11 15:39:08 +11001736#ifdef XFS_BUF_TRACE
Lachlan McIlroy5695ef462008-08-13 16:51:57 +10001737 xfs_buf_trace_buf = ktrace_alloc(XFS_BUF_TRACE_SIZE, KM_NOFS);
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001738#endif
1739
Nathan Scott87582802006-03-14 13:18:19 +11001740 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
1741 KM_ZONE_HWALIGN, NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11001742 if (!xfs_buf_zone)
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001743 goto out_free_trace_buf;
1744
Rafael J. Wysockib4337692007-03-22 00:11:27 -08001745 xfslogd_workqueue = create_workqueue("xfslogd");
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001746 if (!xfslogd_workqueue)
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001747 goto out_free_buf_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Rafael J. Wysockib4337692007-03-22 00:11:27 -08001749 xfsdatad_workqueue = create_workqueue("xfsdatad");
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001750 if (!xfsdatad_workqueue)
1751 goto out_destroy_xfslogd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Rusty Russell8e1f9362007-07-17 04:03:17 -07001753 register_shrinker(&xfs_buf_shake);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001754 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001756 out_destroy_xfslogd_workqueue:
1757 destroy_workqueue(xfslogd_workqueue);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001758 out_free_buf_zone:
Nathan Scottce8e9222006-01-11 15:39:08 +11001759 kmem_zone_destroy(xfs_buf_zone);
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001760 out_free_trace_buf:
Nathan Scottce8e9222006-01-11 15:39:08 +11001761#ifdef XFS_BUF_TRACE
1762 ktrace_free(xfs_buf_trace_buf);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001763#endif
Nathan Scott87582802006-03-14 13:18:19 +11001764 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765}
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767void
Nathan Scottce8e9222006-01-11 15:39:08 +11001768xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769{
Rusty Russell8e1f9362007-07-17 04:03:17 -07001770 unregister_shrinker(&xfs_buf_shake);
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001771 destroy_workqueue(xfsdatad_workqueue);
1772 destroy_workqueue(xfslogd_workqueue);
Nathan Scottce8e9222006-01-11 15:39:08 +11001773 kmem_zone_destroy(xfs_buf_zone);
1774#ifdef XFS_BUF_TRACE
1775 ktrace_free(xfs_buf_trace_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777}
Tim Shimmine6a0e9c2007-05-08 13:49:59 +10001778
1779#ifdef CONFIG_KDB_MODULES
1780struct list_head *
1781xfs_get_buftarg_list(void)
1782{
1783 return &xfs_buftarg_list;
1784}
1785#endif