blob: 6f3ebb634b8bc71089fbcb4dc69fea0e658d185a [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
Christoph Hellwigb7963132009-03-03 14:48:37 -050037#include "xfs_sb.h"
38#include "xfs_inum.h"
39#include "xfs_ag.h"
40#include "xfs_dmapi.h"
41#include "xfs_mount.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000042#include "xfs_trace.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050043
David Chinner7989cb82007-02-10 18:34:56 +110044static kmem_zone_t *xfs_buf_zone;
David Chinnera6867a62006-01-11 15:37:58 +110045STATIC int xfsbufd(void *);
Al Viro27496a82005-10-21 03:20:48 -040046STATIC int xfsbufd_wakeup(int, gfp_t);
Nathan Scottce8e9222006-01-11 15:39:08 +110047STATIC void xfs_buf_delwri_queue(xfs_buf_t *, int);
Rusty Russell8e1f9362007-07-17 04:03:17 -070048static struct shrinker xfs_buf_shake = {
49 .shrink = xfsbufd_wakeup,
50 .seeks = DEFAULT_SEEKS,
51};
Christoph Hellwig23ea4032005-06-21 15:14:01 +100052
David Chinner7989cb82007-02-10 18:34:56 +110053static struct workqueue_struct *xfslogd_workqueue;
Christoph Hellwig0829c362005-09-02 16:58:49 +100054struct workqueue_struct *xfsdatad_workqueue;
Dave Chinnerc626d172009-04-06 18:42:11 +020055struct workqueue_struct *xfsconvertd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Nathan Scottce8e9222006-01-11 15:39:08 +110057#ifdef XFS_BUF_LOCK_TRACKING
58# define XB_SET_OWNER(bp) ((bp)->b_last_holder = current->pid)
59# define XB_CLEAR_OWNER(bp) ((bp)->b_last_holder = -1)
60# define XB_GET_OWNER(bp) ((bp)->b_last_holder)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#else
Nathan Scottce8e9222006-01-11 15:39:08 +110062# define XB_SET_OWNER(bp) do { } while (0)
63# define XB_CLEAR_OWNER(bp) do { } while (0)
64# define XB_GET_OWNER(bp) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#endif
66
Nathan Scottce8e9222006-01-11 15:39:08 +110067#define xb_to_gfp(flags) \
68 ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : \
69 ((flags) & XBF_DONT_BLOCK) ? GFP_NOFS : GFP_KERNEL) | __GFP_NOWARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Nathan Scottce8e9222006-01-11 15:39:08 +110071#define xb_to_km(flags) \
72 (((flags) & XBF_DONT_BLOCK) ? KM_NOFS : KM_SLEEP)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Nathan Scottce8e9222006-01-11 15:39:08 +110074#define xfs_buf_allocate(flags) \
75 kmem_zone_alloc(xfs_buf_zone, xb_to_km(flags))
76#define xfs_buf_deallocate(bp) \
77 kmem_zone_free(xfs_buf_zone, (bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
James Bottomley73c77e22010-01-25 11:42:24 -060079static inline int
80xfs_buf_is_vmapped(
81 struct xfs_buf *bp)
82{
83 /*
84 * Return true if the buffer is vmapped.
85 *
86 * The XBF_MAPPED flag is set if the buffer should be mapped, but the
87 * code is clever enough to know it doesn't have to map a single page,
88 * so the check has to be both for XBF_MAPPED and bp->b_page_count > 1.
89 */
90 return (bp->b_flags & XBF_MAPPED) && bp->b_page_count > 1;
91}
92
93static inline int
94xfs_buf_vmap_len(
95 struct xfs_buf *bp)
96{
97 return (bp->b_page_count * PAGE_SIZE) - bp->b_offset;
98}
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100101 * Page Region interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 *
Nathan Scottce8e9222006-01-11 15:39:08 +1100103 * For pages in filesystems where the blocksize is smaller than the
104 * pagesize, we use the page->private field (long) to hold a bitmap
105 * of uptodate regions within the page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 *
Nathan Scottce8e9222006-01-11 15:39:08 +1100107 * Each such region is "bytes per page / bits per long" bytes long.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 *
Nathan Scottce8e9222006-01-11 15:39:08 +1100109 * NBPPR == number-of-bytes-per-page-region
110 * BTOPR == bytes-to-page-region (rounded up)
111 * BTOPRT == bytes-to-page-region-truncated (rounded down)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 */
113#if (BITS_PER_LONG == 32)
114#define PRSHIFT (PAGE_CACHE_SHIFT - 5) /* (32 == 1<<5) */
115#elif (BITS_PER_LONG == 64)
116#define PRSHIFT (PAGE_CACHE_SHIFT - 6) /* (64 == 1<<6) */
117#else
118#error BITS_PER_LONG must be 32 or 64
119#endif
120#define NBPPR (PAGE_CACHE_SIZE/BITS_PER_LONG)
121#define BTOPR(b) (((unsigned int)(b) + (NBPPR - 1)) >> PRSHIFT)
122#define BTOPRT(b) (((unsigned int)(b) >> PRSHIFT))
123
124STATIC unsigned long
125page_region_mask(
126 size_t offset,
127 size_t length)
128{
129 unsigned long mask;
130 int first, final;
131
132 first = BTOPR(offset);
133 final = BTOPRT(offset + length - 1);
134 first = min(first, final);
135
136 mask = ~0UL;
137 mask <<= BITS_PER_LONG - (final - first);
138 mask >>= BITS_PER_LONG - (final);
139
140 ASSERT(offset + length <= PAGE_CACHE_SIZE);
141 ASSERT((final - first) < BITS_PER_LONG && (final - first) >= 0);
142
143 return mask;
144}
145
Christoph Hellwigb8f82a42009-11-14 16:17:22 +0000146STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147set_page_region(
148 struct page *page,
149 size_t offset,
150 size_t length)
151{
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700152 set_page_private(page,
153 page_private(page) | page_region_mask(offset, length));
154 if (page_private(page) == ~0UL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 SetPageUptodate(page);
156}
157
Christoph Hellwigb8f82a42009-11-14 16:17:22 +0000158STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159test_page_region(
160 struct page *page,
161 size_t offset,
162 size_t length)
163{
164 unsigned long mask = page_region_mask(offset, length);
165
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700166 return (mask && (page_private(page) & mask) == mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
169/*
Felix Blyakher3a011a12009-02-18 15:56:51 -0600170 * Mapping of multi-page buffers into contiguous virtual space
171 */
172
173typedef struct a_list {
174 void *vm_addr;
175 struct a_list *next;
176} a_list_t;
177
178static a_list_t *as_free_head;
179static int as_list_len;
180static DEFINE_SPINLOCK(as_lock);
181
182/*
183 * Try to batch vunmaps because they are costly.
184 */
185STATIC void
186free_address(
187 void *addr)
188{
189 a_list_t *aentry;
190
191#ifdef CONFIG_XEN
192 /*
193 * Xen needs to be able to make sure it can get an exclusive
194 * RO mapping of pages it wants to turn into a pagetable. If
195 * a newly allocated page is also still being vmap()ed by xfs,
196 * it will cause pagetable construction to fail. This is a
197 * quick workaround to always eagerly unmap pages so that Xen
198 * is happy.
199 */
200 vunmap(addr);
201 return;
202#endif
203
204 aentry = kmalloc(sizeof(a_list_t), GFP_NOWAIT);
205 if (likely(aentry)) {
206 spin_lock(&as_lock);
207 aentry->next = as_free_head;
208 aentry->vm_addr = addr;
209 as_free_head = aentry;
210 as_list_len++;
211 spin_unlock(&as_lock);
212 } else {
213 vunmap(addr);
214 }
215}
216
217STATIC void
218purge_addresses(void)
219{
220 a_list_t *aentry, *old;
221
222 if (as_free_head == NULL)
223 return;
224
225 spin_lock(&as_lock);
226 aentry = as_free_head;
227 as_free_head = NULL;
228 as_list_len = 0;
229 spin_unlock(&as_lock);
230
231 while ((old = aentry) != NULL) {
232 vunmap(aentry->vm_addr);
233 aentry = aentry->next;
234 kfree(old);
235 }
236}
237
238/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100239 * Internal xfs_buf_t object manipulation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 */
241
242STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100243_xfs_buf_initialize(
244 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100246 xfs_off_t range_base,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 size_t range_length,
Nathan Scottce8e9222006-01-11 15:39:08 +1100248 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 /*
Nathan Scottce8e9222006-01-11 15:39:08 +1100251 * We don't want certain flags to appear in b_flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100253 flags &= ~(XBF_LOCK|XBF_MAPPED|XBF_DONT_BLOCK|XBF_READ_AHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Nathan Scottce8e9222006-01-11 15:39:08 +1100255 memset(bp, 0, sizeof(xfs_buf_t));
256 atomic_set(&bp->b_hold, 1);
David Chinnerb4dd3302008-08-13 16:36:11 +1000257 init_completion(&bp->b_iowait);
Nathan Scottce8e9222006-01-11 15:39:08 +1100258 INIT_LIST_HEAD(&bp->b_list);
259 INIT_LIST_HEAD(&bp->b_hash_list);
260 init_MUTEX_LOCKED(&bp->b_sema); /* held, no waiters */
261 XB_SET_OWNER(bp);
262 bp->b_target = target;
263 bp->b_file_offset = range_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 /*
265 * Set buffer_length and count_desired to the same value initially.
266 * I/O routines should use count_desired, which will be the same in
267 * most cases but may be reset (e.g. XFS recovery).
268 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100269 bp->b_buffer_length = bp->b_count_desired = range_length;
270 bp->b_flags = flags;
271 bp->b_bn = XFS_BUF_DADDR_NULL;
272 atomic_set(&bp->b_pin_count, 0);
273 init_waitqueue_head(&bp->b_waiters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Nathan Scottce8e9222006-01-11 15:39:08 +1100275 XFS_STATS_INC(xb_create);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000276
277 trace_xfs_buf_init(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
280/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100281 * Allocate a page array capable of holding a specified number
282 * of pages, and point the page buf at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 */
284STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100285_xfs_buf_get_pages(
286 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 int page_count,
Nathan Scottce8e9222006-01-11 15:39:08 +1100288 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 /* Make sure that we have a page list */
Nathan Scottce8e9222006-01-11 15:39:08 +1100291 if (bp->b_pages == NULL) {
292 bp->b_offset = xfs_buf_poff(bp->b_file_offset);
293 bp->b_page_count = page_count;
294 if (page_count <= XB_PAGES) {
295 bp->b_pages = bp->b_page_array;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100297 bp->b_pages = kmem_alloc(sizeof(struct page *) *
298 page_count, xb_to_km(flags));
299 if (bp->b_pages == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return -ENOMEM;
301 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100302 memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
304 return 0;
305}
306
307/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100308 * Frees b_pages if it was allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 */
310STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100311_xfs_buf_free_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 xfs_buf_t *bp)
313{
Nathan Scottce8e9222006-01-11 15:39:08 +1100314 if (bp->b_pages != bp->b_page_array) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000315 kmem_free(bp->b_pages);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000316 bp->b_pages = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
318}
319
320/*
321 * Releases the specified buffer.
322 *
323 * The modification state of any associated pages is left unchanged.
Nathan Scottce8e9222006-01-11 15:39:08 +1100324 * The buffer most not be on any hash - use xfs_buf_rele instead for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 * hashed and refcounted buffers
326 */
327void
Nathan Scottce8e9222006-01-11 15:39:08 +1100328xfs_buf_free(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 xfs_buf_t *bp)
330{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000331 trace_xfs_buf_free(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Nathan Scottce8e9222006-01-11 15:39:08 +1100333 ASSERT(list_empty(&bp->b_hash_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000335 if (bp->b_flags & (_XBF_PAGE_CACHE|_XBF_PAGES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 uint i;
337
James Bottomley73c77e22010-01-25 11:42:24 -0600338 if (xfs_buf_is_vmapped(bp))
Felix Blyakher3a011a12009-02-18 15:56:51 -0600339 free_address(bp->b_addr - bp->b_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Nathan Scott948ecdb2006-09-28 11:03:13 +1000341 for (i = 0; i < bp->b_page_count; i++) {
342 struct page *page = bp->b_pages[i];
343
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000344 if (bp->b_flags & _XBF_PAGE_CACHE)
345 ASSERT(!PagePrivate(page));
Nathan Scott948ecdb2006-09-28 11:03:13 +1000346 page_cache_release(page);
347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
Dave Chinner3fc98b12009-12-14 23:11:57 +0000349 _xfs_buf_free_pages(bp);
Nathan Scottce8e9222006-01-11 15:39:08 +1100350 xfs_buf_deallocate(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
353/*
354 * Finds all pages for buffer in question and builds it's page list.
355 */
356STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100357_xfs_buf_lookup_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 xfs_buf_t *bp,
359 uint flags)
360{
Nathan Scottce8e9222006-01-11 15:39:08 +1100361 struct address_space *mapping = bp->b_target->bt_mapping;
362 size_t blocksize = bp->b_target->bt_bsize;
363 size_t size = bp->b_count_desired;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 size_t nbytes, offset;
Nathan Scottce8e9222006-01-11 15:39:08 +1100365 gfp_t gfp_mask = xb_to_gfp(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 unsigned short page_count, i;
367 pgoff_t first;
Nathan Scott204ab252006-01-11 20:50:22 +1100368 xfs_off_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 int error;
370
Nathan Scottce8e9222006-01-11 15:39:08 +1100371 end = bp->b_file_offset + bp->b_buffer_length;
372 page_count = xfs_buf_btoc(end) - xfs_buf_btoct(bp->b_file_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Nathan Scottce8e9222006-01-11 15:39:08 +1100374 error = _xfs_buf_get_pages(bp, page_count, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (unlikely(error))
376 return error;
Nathan Scottce8e9222006-01-11 15:39:08 +1100377 bp->b_flags |= _XBF_PAGE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Nathan Scottce8e9222006-01-11 15:39:08 +1100379 offset = bp->b_offset;
380 first = bp->b_file_offset >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Nathan Scottce8e9222006-01-11 15:39:08 +1100382 for (i = 0; i < bp->b_page_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 struct page *page;
384 uint retries = 0;
385
386 retry:
387 page = find_or_create_page(mapping, first + i, gfp_mask);
388 if (unlikely(page == NULL)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100389 if (flags & XBF_READ_AHEAD) {
390 bp->b_page_count = i;
Christoph Hellwig6ab455e2008-05-19 16:34:42 +1000391 for (i = 0; i < bp->b_page_count; i++)
392 unlock_page(bp->b_pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return -ENOMEM;
394 }
395
396 /*
397 * This could deadlock.
398 *
399 * But until all the XFS lowlevel code is revamped to
400 * handle buffer allocation failures we can't do much.
401 */
402 if (!(++retries % 100))
403 printk(KERN_ERR
404 "XFS: possible memory allocation "
405 "deadlock in %s (mode:0x%x)\n",
Harvey Harrison34a622b2008-04-10 12:19:21 +1000406 __func__, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Nathan Scottce8e9222006-01-11 15:39:08 +1100408 XFS_STATS_INC(xb_page_retries);
Christoph Hellwig23ea4032005-06-21 15:14:01 +1000409 xfsbufd_wakeup(0, gfp_mask);
Jens Axboe8aa7e842009-07-09 14:52:32 +0200410 congestion_wait(BLK_RW_ASYNC, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 goto retry;
412 }
413
Nathan Scottce8e9222006-01-11 15:39:08 +1100414 XFS_STATS_INC(xb_page_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 nbytes = min_t(size_t, size, PAGE_CACHE_SIZE - offset);
417 size -= nbytes;
418
Nathan Scott948ecdb2006-09-28 11:03:13 +1000419 ASSERT(!PagePrivate(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if (!PageUptodate(page)) {
421 page_count--;
Christoph Hellwig6ab455e2008-05-19 16:34:42 +1000422 if (blocksize >= PAGE_CACHE_SIZE) {
423 if (flags & XBF_READ)
424 bp->b_flags |= _XBF_PAGE_LOCKED;
425 } else if (!PagePrivate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (test_page_region(page, offset, nbytes))
427 page_count++;
428 }
429 }
430
Nathan Scottce8e9222006-01-11 15:39:08 +1100431 bp->b_pages[i] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 offset = 0;
433 }
434
Christoph Hellwig6ab455e2008-05-19 16:34:42 +1000435 if (!(bp->b_flags & _XBF_PAGE_LOCKED)) {
436 for (i = 0; i < bp->b_page_count; i++)
437 unlock_page(bp->b_pages[i]);
438 }
439
Nathan Scottce8e9222006-01-11 15:39:08 +1100440 if (page_count == bp->b_page_count)
441 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return error;
444}
445
446/*
447 * Map buffer into kernel address-space if nessecary.
448 */
449STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100450_xfs_buf_map_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 xfs_buf_t *bp,
452 uint flags)
453{
454 /* A single page buffer is always mappable */
Nathan Scottce8e9222006-01-11 15:39:08 +1100455 if (bp->b_page_count == 1) {
456 bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
457 bp->b_flags |= XBF_MAPPED;
458 } else if (flags & XBF_MAPPED) {
Felix Blyakher3a011a12009-02-18 15:56:51 -0600459 if (as_list_len > 64)
460 purge_addresses();
Felix Blyakhercf7dab82009-02-18 15:41:28 -0600461 bp->b_addr = vmap(bp->b_pages, bp->b_page_count,
462 VM_MAP, PAGE_KERNEL);
Nathan Scottce8e9222006-01-11 15:39:08 +1100463 if (unlikely(bp->b_addr == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return -ENOMEM;
Nathan Scottce8e9222006-01-11 15:39:08 +1100465 bp->b_addr += bp->b_offset;
466 bp->b_flags |= XBF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
468
469 return 0;
470}
471
472/*
473 * Finding and Reading Buffers
474 */
475
476/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100477 * Look up, and creates if absent, a lockable buffer for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 * a given range of an inode. The buffer is returned
479 * locked. If other overlapping buffers exist, they are
480 * released before the new buffer is created and locked,
481 * which may imply that this call will block until those buffers
482 * are unlocked. No I/O is implied by this call.
483 */
484xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100485_xfs_buf_find(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 xfs_buftarg_t *btp, /* block device target */
Nathan Scott204ab252006-01-11 20:50:22 +1100487 xfs_off_t ioff, /* starting offset of range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 size_t isize, /* length of range */
Nathan Scottce8e9222006-01-11 15:39:08 +1100489 xfs_buf_flags_t flags,
490 xfs_buf_t *new_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Nathan Scott204ab252006-01-11 20:50:22 +1100492 xfs_off_t range_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 size_t range_length;
494 xfs_bufhash_t *hash;
Nathan Scottce8e9222006-01-11 15:39:08 +1100495 xfs_buf_t *bp, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 range_base = (ioff << BBSHIFT);
498 range_length = (isize << BBSHIFT);
499
500 /* Check for IOs smaller than the sector size / not sector aligned */
Nathan Scottce8e9222006-01-11 15:39:08 +1100501 ASSERT(!(range_length < (1 << btp->bt_sshift)));
Nathan Scott204ab252006-01-11 20:50:22 +1100502 ASSERT(!(range_base & (xfs_off_t)btp->bt_smask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 hash = &btp->bt_hash[hash_long((unsigned long)ioff, btp->bt_hashshift)];
505
506 spin_lock(&hash->bh_lock);
507
Nathan Scottce8e9222006-01-11 15:39:08 +1100508 list_for_each_entry_safe(bp, n, &hash->bh_list, b_hash_list) {
509 ASSERT(btp == bp->b_target);
510 if (bp->b_file_offset == range_base &&
511 bp->b_buffer_length == range_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 /*
Nathan Scottce8e9222006-01-11 15:39:08 +1100513 * If we look at something, bring it to the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 * front of the list for next time.
515 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100516 atomic_inc(&bp->b_hold);
517 list_move(&bp->b_hash_list, &hash->bh_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 goto found;
519 }
520 }
521
522 /* No match found */
Nathan Scottce8e9222006-01-11 15:39:08 +1100523 if (new_bp) {
524 _xfs_buf_initialize(new_bp, btp, range_base,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 range_length, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100526 new_bp->b_hash = hash;
527 list_add(&new_bp->b_hash_list, &hash->bh_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100529 XFS_STATS_INC(xb_miss_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
531
532 spin_unlock(&hash->bh_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +1100533 return new_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535found:
536 spin_unlock(&hash->bh_lock);
537
538 /* Attempt to get the semaphore without sleeping,
539 * if this does not work then we need to drop the
540 * spinlock and do a hard attempt on the semaphore.
541 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100542 if (down_trylock(&bp->b_sema)) {
543 if (!(flags & XBF_TRYLOCK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 /* wait for buffer ownership */
Nathan Scottce8e9222006-01-11 15:39:08 +1100545 xfs_buf_lock(bp);
546 XFS_STATS_INC(xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 } else {
548 /* We asked for a trylock and failed, no need
549 * to look at file offset and length here, we
Nathan Scottce8e9222006-01-11 15:39:08 +1100550 * know that this buffer at least overlaps our
551 * buffer and is locked, therefore our buffer
552 * either does not exist, or is this buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100554 xfs_buf_rele(bp);
555 XFS_STATS_INC(xb_busy_locked);
556 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558 } else {
559 /* trylock worked */
Nathan Scottce8e9222006-01-11 15:39:08 +1100560 XB_SET_OWNER(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
562
Nathan Scottce8e9222006-01-11 15:39:08 +1100563 if (bp->b_flags & XBF_STALE) {
564 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
565 bp->b_flags &= XBF_MAPPED;
David Chinner2f926582005-09-05 08:33:35 +1000566 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000567
568 trace_xfs_buf_find(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100569 XFS_STATS_INC(xb_get_locked);
570 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
573/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100574 * Assembles a buffer covering the specified range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 * Storage in memory for all portions of the buffer will be allocated,
576 * although backing storage may not be.
577 */
578xfs_buf_t *
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000579xfs_buf_get(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 xfs_buftarg_t *target,/* target for buffer */
Nathan Scott204ab252006-01-11 20:50:22 +1100581 xfs_off_t ioff, /* starting offset of range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 size_t isize, /* length of range */
Nathan Scottce8e9222006-01-11 15:39:08 +1100583 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Nathan Scottce8e9222006-01-11 15:39:08 +1100585 xfs_buf_t *bp, *new_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 int error = 0, i;
587
Nathan Scottce8e9222006-01-11 15:39:08 +1100588 new_bp = xfs_buf_allocate(flags);
589 if (unlikely(!new_bp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return NULL;
591
Nathan Scottce8e9222006-01-11 15:39:08 +1100592 bp = _xfs_buf_find(target, ioff, isize, flags, new_bp);
593 if (bp == new_bp) {
594 error = _xfs_buf_lookup_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (error)
596 goto no_buffer;
597 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100598 xfs_buf_deallocate(new_bp);
599 if (unlikely(bp == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 return NULL;
601 }
602
Nathan Scottce8e9222006-01-11 15:39:08 +1100603 for (i = 0; i < bp->b_page_count; i++)
604 mark_page_accessed(bp->b_pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Nathan Scottce8e9222006-01-11 15:39:08 +1100606 if (!(bp->b_flags & XBF_MAPPED)) {
607 error = _xfs_buf_map_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (unlikely(error)) {
609 printk(KERN_WARNING "%s: failed to map pages\n",
Harvey Harrison34a622b2008-04-10 12:19:21 +1000610 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 goto no_buffer;
612 }
613 }
614
Nathan Scottce8e9222006-01-11 15:39:08 +1100615 XFS_STATS_INC(xb_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 /*
618 * Always fill in the block number now, the mapped cases can do
619 * their own overlay of this later.
620 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100621 bp->b_bn = ioff;
622 bp->b_count_desired = bp->b_buffer_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000624 trace_xfs_buf_get(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100625 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100628 if (flags & (XBF_LOCK | XBF_TRYLOCK))
629 xfs_buf_unlock(bp);
630 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return NULL;
632}
633
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100634STATIC int
635_xfs_buf_read(
636 xfs_buf_t *bp,
637 xfs_buf_flags_t flags)
638{
639 int status;
640
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100641 ASSERT(!(flags & (XBF_DELWRI|XBF_WRITE)));
642 ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL);
643
644 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_DELWRI | \
645 XBF_READ_AHEAD | _XBF_RUN_QUEUES);
646 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | \
647 XBF_READ_AHEAD | _XBF_RUN_QUEUES);
648
649 status = xfs_buf_iorequest(bp);
650 if (!status && !(flags & XBF_ASYNC))
651 status = xfs_buf_iowait(bp);
652 return status;
653}
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655xfs_buf_t *
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000656xfs_buf_read(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100658 xfs_off_t ioff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 size_t isize,
Nathan Scottce8e9222006-01-11 15:39:08 +1100660 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Nathan Scottce8e9222006-01-11 15:39:08 +1100662 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Nathan Scottce8e9222006-01-11 15:39:08 +1100664 flags |= XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000666 bp = xfs_buf_get(target, ioff, isize, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100667 if (bp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000668 trace_xfs_buf_read(bp, flags, _RET_IP_);
669
Nathan Scottce8e9222006-01-11 15:39:08 +1100670 if (!XFS_BUF_ISDONE(bp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100671 XFS_STATS_INC(xb_get_read);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100672 _xfs_buf_read(bp, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100673 } else if (flags & XBF_ASYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 /*
675 * Read ahead call which is already satisfied,
676 * drop the buffer
677 */
678 goto no_buffer;
679 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 /* We do not want read in the flags */
Nathan Scottce8e9222006-01-11 15:39:08 +1100681 bp->b_flags &= ~XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
683 }
684
Nathan Scottce8e9222006-01-11 15:39:08 +1100685 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100688 if (flags & (XBF_LOCK | XBF_TRYLOCK))
689 xfs_buf_unlock(bp);
690 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return NULL;
692}
693
694/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100695 * If we are not low on memory then do the readahead in a deadlock
696 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 */
698void
Nathan Scottce8e9222006-01-11 15:39:08 +1100699xfs_buf_readahead(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100701 xfs_off_t ioff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 size_t isize,
Nathan Scottce8e9222006-01-11 15:39:08 +1100703 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 struct backing_dev_info *bdi;
706
Nathan Scottce8e9222006-01-11 15:39:08 +1100707 bdi = target->bt_mapping->backing_dev_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (bdi_read_congested(bdi))
709 return;
710
Nathan Scottce8e9222006-01-11 15:39:08 +1100711 flags |= (XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD);
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000712 xfs_buf_read(target, ioff, isize, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
715xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100716xfs_buf_get_empty(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 size_t len,
718 xfs_buftarg_t *target)
719{
Nathan Scottce8e9222006-01-11 15:39:08 +1100720 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Nathan Scottce8e9222006-01-11 15:39:08 +1100722 bp = xfs_buf_allocate(0);
723 if (bp)
724 _xfs_buf_initialize(bp, target, 0, len, 0);
725 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
727
728static inline struct page *
729mem_to_page(
730 void *addr)
731{
Christoph Lameter9e2779f2008-02-04 22:28:34 -0800732 if ((!is_vmalloc_addr(addr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return virt_to_page(addr);
734 } else {
735 return vmalloc_to_page(addr);
736 }
737}
738
739int
Nathan Scottce8e9222006-01-11 15:39:08 +1100740xfs_buf_associate_memory(
741 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 void *mem,
743 size_t len)
744{
745 int rval;
746 int i = 0;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100747 unsigned long pageaddr;
748 unsigned long offset;
749 size_t buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 int page_count;
751
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100752 pageaddr = (unsigned long)mem & PAGE_CACHE_MASK;
753 offset = (unsigned long)mem - pageaddr;
754 buflen = PAGE_CACHE_ALIGN(len + offset);
755 page_count = buflen >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 /* Free any previous set of page pointers */
Nathan Scottce8e9222006-01-11 15:39:08 +1100758 if (bp->b_pages)
759 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Nathan Scottce8e9222006-01-11 15:39:08 +1100761 bp->b_pages = NULL;
762 bp->b_addr = mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Christoph Hellwig36fae172009-07-18 18:14:58 -0400764 rval = _xfs_buf_get_pages(bp, page_count, XBF_DONT_BLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 if (rval)
766 return rval;
767
Nathan Scottce8e9222006-01-11 15:39:08 +1100768 bp->b_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100770 for (i = 0; i < bp->b_page_count; i++) {
771 bp->b_pages[i] = mem_to_page((void *)pageaddr);
772 pageaddr += PAGE_CACHE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100775 bp->b_count_desired = len;
776 bp->b_buffer_length = buflen;
Nathan Scottce8e9222006-01-11 15:39:08 +1100777 bp->b_flags |= XBF_MAPPED;
Christoph Hellwig6ab455e2008-05-19 16:34:42 +1000778 bp->b_flags &= ~_XBF_PAGE_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 return 0;
781}
782
783xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100784xfs_buf_get_noaddr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 size_t len,
786 xfs_buftarg_t *target)
787{
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000788 unsigned long page_count = PAGE_ALIGN(len) >> PAGE_SHIFT;
789 int error, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Nathan Scottce8e9222006-01-11 15:39:08 +1100792 bp = xfs_buf_allocate(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (unlikely(bp == NULL))
794 goto fail;
Nathan Scottce8e9222006-01-11 15:39:08 +1100795 _xfs_buf_initialize(bp, target, 0, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000797 error = _xfs_buf_get_pages(bp, page_count, 0);
798 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 goto fail_free_buf;
800
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000801 for (i = 0; i < page_count; i++) {
802 bp->b_pages[i] = alloc_page(GFP_KERNEL);
803 if (!bp->b_pages[i])
804 goto fail_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000806 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000808 error = _xfs_buf_map_pages(bp, XBF_MAPPED);
809 if (unlikely(error)) {
810 printk(KERN_WARNING "%s: failed to map pages\n",
Harvey Harrison34a622b2008-04-10 12:19:21 +1000811 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 goto fail_free_mem;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Nathan Scottce8e9222006-01-11 15:39:08 +1100815 xfs_buf_unlock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000817 trace_xfs_buf_get_noaddr(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return bp;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 fail_free_mem:
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000821 while (--i >= 0)
822 __free_page(bp->b_pages[i]);
Christoph Hellwigca165b82007-05-24 15:21:11 +1000823 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 fail_free_buf:
Christoph Hellwigca165b82007-05-24 15:21:11 +1000825 xfs_buf_deallocate(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 fail:
827 return NULL;
828}
829
830/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 * Increment reference count on buffer, to hold the buffer concurrently
832 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 * Must hold the buffer already to call this function.
834 */
835void
Nathan Scottce8e9222006-01-11 15:39:08 +1100836xfs_buf_hold(
837 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000839 trace_xfs_buf_hold(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100840 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842
843/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100844 * Releases a hold on the specified buffer. If the
845 * the hold count is 1, calls xfs_buf_free.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 */
847void
Nathan Scottce8e9222006-01-11 15:39:08 +1100848xfs_buf_rele(
849 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Nathan Scottce8e9222006-01-11 15:39:08 +1100851 xfs_bufhash_t *hash = bp->b_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000853 trace_xfs_buf_rele(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Nathan Scottfad3aa12006-02-01 12:14:52 +1100855 if (unlikely(!hash)) {
856 ASSERT(!bp->b_relse);
857 if (atomic_dec_and_test(&bp->b_hold))
858 xfs_buf_free(bp);
859 return;
860 }
861
Lachlan McIlroy37906892008-08-13 15:42:10 +1000862 ASSERT(atomic_read(&bp->b_hold) > 0);
Nathan Scottce8e9222006-01-11 15:39:08 +1100863 if (atomic_dec_and_lock(&bp->b_hold, &hash->bh_lock)) {
864 if (bp->b_relse) {
865 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 spin_unlock(&hash->bh_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +1100867 (*(bp->b_relse)) (bp);
868 } else if (bp->b_flags & XBF_FS_MANAGED) {
Christoph Hellwig7f14d0a2005-11-02 15:09:35 +1100869 spin_unlock(&hash->bh_lock);
870 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100871 ASSERT(!(bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)));
872 list_del_init(&bp->b_hash_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 spin_unlock(&hash->bh_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +1100874 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
876 }
877}
878
879
880/*
881 * Mutual exclusion on buffers. Locking model:
882 *
883 * Buffers associated with inodes for which buffer locking
884 * is not enabled are not protected by semaphores, and are
885 * assumed to be exclusively owned by the caller. There is a
886 * spinlock in the buffer, used by the caller when concurrent
887 * access is possible.
888 */
889
890/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100891 * Locks a buffer object, if it is not already locked.
892 * Note that this in no way locks the underlying pages, so it is only
893 * useful for synchronizing concurrent use of buffer objects, not for
894 * synchronizing independent access to the underlying pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 */
896int
Nathan Scottce8e9222006-01-11 15:39:08 +1100897xfs_buf_cond_lock(
898 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
900 int locked;
901
Nathan Scottce8e9222006-01-11 15:39:08 +1100902 locked = down_trylock(&bp->b_sema) == 0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000903 if (locked)
Nathan Scottce8e9222006-01-11 15:39:08 +1100904 XB_SET_OWNER(bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000905
906 trace_xfs_buf_cond_lock(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100907 return locked ? 0 : -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910int
Nathan Scottce8e9222006-01-11 15:39:08 +1100911xfs_buf_lock_value(
912 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Stephen Rothwelladaa6932008-04-22 15:26:13 +1000914 return bp->b_sema.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100918 * Locks a buffer object.
919 * Note that this in no way locks the underlying pages, so it is only
920 * useful for synchronizing concurrent use of buffer objects, not for
921 * synchronizing independent access to the underlying pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100923void
924xfs_buf_lock(
925 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000927 trace_xfs_buf_lock(bp, _RET_IP_);
928
Nathan Scottce8e9222006-01-11 15:39:08 +1100929 if (atomic_read(&bp->b_io_remaining))
930 blk_run_address_space(bp->b_target->bt_mapping);
931 down(&bp->b_sema);
932 XB_SET_OWNER(bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000933
934 trace_xfs_buf_lock_done(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
937/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100938 * Releases the lock on the buffer object.
David Chinner2f926582005-09-05 08:33:35 +1000939 * If the buffer is marked delwri but is not queued, do so before we
Nathan Scottce8e9222006-01-11 15:39:08 +1100940 * unlock the buffer as we need to set flags correctly. We also need to
David Chinner2f926582005-09-05 08:33:35 +1000941 * take a reference for the delwri queue because the unlocker is going to
942 * drop their's and they don't know we just queued it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 */
944void
Nathan Scottce8e9222006-01-11 15:39:08 +1100945xfs_buf_unlock(
946 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
Nathan Scottce8e9222006-01-11 15:39:08 +1100948 if ((bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)) == XBF_DELWRI) {
949 atomic_inc(&bp->b_hold);
950 bp->b_flags |= XBF_ASYNC;
951 xfs_buf_delwri_queue(bp, 0);
David Chinner2f926582005-09-05 08:33:35 +1000952 }
953
Nathan Scottce8e9222006-01-11 15:39:08 +1100954 XB_CLEAR_OWNER(bp);
955 up(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000956
957 trace_xfs_buf_unlock(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
959
960
961/*
962 * Pinning Buffer Storage in Memory
Nathan Scottce8e9222006-01-11 15:39:08 +1100963 * Ensure that no attempt to force a buffer to disk will succeed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 */
965void
Nathan Scottce8e9222006-01-11 15:39:08 +1100966xfs_buf_pin(
967 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000969 trace_xfs_buf_pin(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100970 atomic_inc(&bp->b_pin_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973void
Nathan Scottce8e9222006-01-11 15:39:08 +1100974xfs_buf_unpin(
975 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000977 trace_xfs_buf_unpin(bp, _RET_IP_);
978
Nathan Scottce8e9222006-01-11 15:39:08 +1100979 if (atomic_dec_and_test(&bp->b_pin_count))
980 wake_up_all(&bp->b_waiters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
983int
Nathan Scottce8e9222006-01-11 15:39:08 +1100984xfs_buf_ispin(
985 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
Nathan Scottce8e9222006-01-11 15:39:08 +1100987 return atomic_read(&bp->b_pin_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
989
Nathan Scottce8e9222006-01-11 15:39:08 +1100990STATIC void
991xfs_buf_wait_unpin(
992 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
994 DECLARE_WAITQUEUE (wait, current);
995
Nathan Scottce8e9222006-01-11 15:39:08 +1100996 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return;
998
Nathan Scottce8e9222006-01-11 15:39:08 +1100999 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 for (;;) {
1001 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +11001002 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001004 if (atomic_read(&bp->b_io_remaining))
1005 blk_run_address_space(bp->b_target->bt_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 schedule();
1007 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001008 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 set_current_state(TASK_RUNNING);
1010}
1011
1012/*
1013 * Buffer Utility Routines
1014 */
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001017xfs_buf_iodone_work(
David Howellsc4028952006-11-22 14:57:56 +00001018 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
David Howellsc4028952006-11-22 14:57:56 +00001020 xfs_buf_t *bp =
1021 container_of(work, xfs_buf_t, b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
David Chinner0bfefc42007-05-14 18:24:23 +10001023 /*
1024 * We can get an EOPNOTSUPP to ordered writes. Here we clear the
1025 * ordered flag and reissue them. Because we can't tell the higher
1026 * layers directly that they should not issue ordered I/O anymore, they
Christoph Hellwig73f6aa42008-10-10 17:28:29 +11001027 * need to check if the _XFS_BARRIER_FAILED flag was set during I/O completion.
David Chinner0bfefc42007-05-14 18:24:23 +10001028 */
1029 if ((bp->b_error == EOPNOTSUPP) &&
1030 (bp->b_flags & (XBF_ORDERED|XBF_ASYNC)) == (XBF_ORDERED|XBF_ASYNC)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001031 trace_xfs_buf_ordered_retry(bp, _RET_IP_);
David Chinner0bfefc42007-05-14 18:24:23 +10001032 bp->b_flags &= ~XBF_ORDERED;
Christoph Hellwig73f6aa42008-10-10 17:28:29 +11001033 bp->b_flags |= _XFS_BARRIER_FAILED;
David Chinner0bfefc42007-05-14 18:24:23 +10001034 xfs_buf_iorequest(bp);
1035 } else if (bp->b_iodone)
Nathan Scottce8e9222006-01-11 15:39:08 +11001036 (*(bp->b_iodone))(bp);
1037 else if (bp->b_flags & XBF_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 xfs_buf_relse(bp);
1039}
1040
1041void
Nathan Scottce8e9222006-01-11 15:39:08 +11001042xfs_buf_ioend(
1043 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 int schedule)
1045{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001046 trace_xfs_buf_iodone(bp, _RET_IP_);
1047
Lachlan McIlroy77be55a2007-11-23 16:31:00 +11001048 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
Nathan Scottce8e9222006-01-11 15:39:08 +11001049 if (bp->b_error == 0)
1050 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Nathan Scottce8e9222006-01-11 15:39:08 +11001052 if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 if (schedule) {
David Howellsc4028952006-11-22 14:57:56 +00001054 INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
Nathan Scottce8e9222006-01-11 15:39:08 +11001055 queue_work(xfslogd_workqueue, &bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 } else {
David Howellsc4028952006-11-22 14:57:56 +00001057 xfs_buf_iodone_work(&bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
1059 } else {
David Chinnerb4dd3302008-08-13 16:36:11 +10001060 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
1062}
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064void
Nathan Scottce8e9222006-01-11 15:39:08 +11001065xfs_buf_ioerror(
1066 xfs_buf_t *bp,
1067 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
1069 ASSERT(error >= 0 && error <= 0xffff);
Nathan Scottce8e9222006-01-11 15:39:08 +11001070 bp->b_error = (unsigned short)error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001071 trace_xfs_buf_ioerror(bp, error, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074int
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001075xfs_bawrite(
1076 void *mp,
1077 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001079 trace_xfs_buf_bawrite(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001081 ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001083 xfs_buf_delwri_dequeue(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001085 bp->b_flags &= ~(XBF_READ | XBF_DELWRI | XBF_READ_AHEAD);
1086 bp->b_flags |= (XBF_WRITE | XBF_ASYNC | _XBF_RUN_QUEUES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Christoph Hellwig15ac08a2008-12-09 04:47:30 -05001088 bp->b_mount = mp;
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001089 bp->b_strat = xfs_bdstrat_cb;
1090 return xfs_bdstrat_cb(bp);
1091}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001093void
1094xfs_bdwrite(
1095 void *mp,
1096 struct xfs_buf *bp)
1097{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001098 trace_xfs_buf_bdwrite(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001100 bp->b_strat = xfs_bdstrat_cb;
Christoph Hellwig15ac08a2008-12-09 04:47:30 -05001101 bp->b_mount = mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001103 bp->b_flags &= ~XBF_READ;
1104 bp->b_flags |= (XBF_DELWRI | XBF_ASYNC);
1105
1106 xfs_buf_delwri_queue(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107}
1108
Christoph Hellwigb8f82a42009-11-14 16:17:22 +00001109STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001110_xfs_buf_ioend(
1111 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 int schedule)
1113{
Christoph Hellwig6ab455e2008-05-19 16:34:42 +10001114 if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
1115 bp->b_flags &= ~_XBF_PAGE_LOCKED;
Nathan Scottce8e9222006-01-11 15:39:08 +11001116 xfs_buf_ioend(bp, schedule);
Christoph Hellwig6ab455e2008-05-19 16:34:42 +10001117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
1119
Al Viro782e3b32007-10-12 07:17:47 +01001120STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001121xfs_buf_bio_end_io(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 struct bio *bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 int error)
1124{
Nathan Scottce8e9222006-01-11 15:39:08 +11001125 xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
1126 unsigned int blocksize = bp->b_target->bt_bsize;
Nathan Scotteedb5532005-09-02 16:39:56 +10001127 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Lachlan McIlroycfbe5262008-12-12 15:27:25 +11001129 xfs_buf_ioerror(bp, -error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
James Bottomley73c77e22010-01-25 11:42:24 -06001131 if (!error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
1132 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1133
Nathan Scotteedb5532005-09-02 16:39:56 +10001134 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 struct page *page = bvec->bv_page;
1136
Nathan Scott948ecdb2006-09-28 11:03:13 +10001137 ASSERT(!PagePrivate(page));
Nathan Scottce8e9222006-01-11 15:39:08 +11001138 if (unlikely(bp->b_error)) {
1139 if (bp->b_flags & XBF_READ)
Nathan Scotteedb5532005-09-02 16:39:56 +10001140 ClearPageUptodate(page);
Nathan Scottce8e9222006-01-11 15:39:08 +11001141 } else if (blocksize >= PAGE_CACHE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 SetPageUptodate(page);
1143 } else if (!PagePrivate(page) &&
Nathan Scottce8e9222006-01-11 15:39:08 +11001144 (bp->b_flags & _XBF_PAGE_CACHE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 set_page_region(page, bvec->bv_offset, bvec->bv_len);
1146 }
1147
Nathan Scotteedb5532005-09-02 16:39:56 +10001148 if (--bvec >= bio->bi_io_vec)
1149 prefetchw(&bvec->bv_page->flags);
Christoph Hellwig6ab455e2008-05-19 16:34:42 +10001150
1151 if (bp->b_flags & _XBF_PAGE_LOCKED)
1152 unlock_page(page);
Nathan Scotteedb5532005-09-02 16:39:56 +10001153 } while (bvec >= bio->bi_io_vec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Nathan Scottce8e9222006-01-11 15:39:08 +11001155 _xfs_buf_ioend(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157}
1158
1159STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001160_xfs_buf_ioapply(
1161 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
Christoph Hellwiga9759f22007-12-07 14:07:08 +11001163 int rw, map_i, total_nr_pages, nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 struct bio *bio;
Nathan Scottce8e9222006-01-11 15:39:08 +11001165 int offset = bp->b_offset;
1166 int size = bp->b_count_desired;
1167 sector_t sector = bp->b_bn;
1168 unsigned int blocksize = bp->b_target->bt_bsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Nathan Scottce8e9222006-01-11 15:39:08 +11001170 total_nr_pages = bp->b_page_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 map_i = 0;
1172
Nathan Scottce8e9222006-01-11 15:39:08 +11001173 if (bp->b_flags & XBF_ORDERED) {
1174 ASSERT(!(bp->b_flags & XBF_READ));
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001175 rw = WRITE_BARRIER;
Dave Chinner2ee1aba2009-11-24 18:03:15 +00001176 } else if (bp->b_flags & XBF_LOG_BUFFER) {
Nathan Scott51bdd702006-09-28 11:01:57 +10001177 ASSERT(!(bp->b_flags & XBF_READ_AHEAD));
1178 bp->b_flags &= ~_XBF_RUN_QUEUES;
1179 rw = (bp->b_flags & XBF_WRITE) ? WRITE_SYNC : READ_SYNC;
Dave Chinner2ee1aba2009-11-24 18:03:15 +00001180 } else if (bp->b_flags & _XBF_RUN_QUEUES) {
1181 ASSERT(!(bp->b_flags & XBF_READ_AHEAD));
1182 bp->b_flags &= ~_XBF_RUN_QUEUES;
1183 rw = (bp->b_flags & XBF_WRITE) ? WRITE_META : READ_META;
Nathan Scott51bdd702006-09-28 11:01:57 +10001184 } else {
1185 rw = (bp->b_flags & XBF_WRITE) ? WRITE :
1186 (bp->b_flags & XBF_READ_AHEAD) ? READA : READ;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001187 }
1188
Nathan Scottce8e9222006-01-11 15:39:08 +11001189 /* Special code path for reading a sub page size buffer in --
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 * we populate up the whole page, and hence the other metadata
1191 * in the same page. This optimization is only valid when the
Nathan Scottce8e9222006-01-11 15:39:08 +11001192 * filesystem block size is not smaller than the page size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001194 if ((bp->b_buffer_length < PAGE_CACHE_SIZE) &&
Christoph Hellwig6ab455e2008-05-19 16:34:42 +10001195 ((bp->b_flags & (XBF_READ|_XBF_PAGE_LOCKED)) ==
1196 (XBF_READ|_XBF_PAGE_LOCKED)) &&
Nathan Scottce8e9222006-01-11 15:39:08 +11001197 (blocksize >= PAGE_CACHE_SIZE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 bio = bio_alloc(GFP_NOIO, 1);
1199
Nathan Scottce8e9222006-01-11 15:39:08 +11001200 bio->bi_bdev = bp->b_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 bio->bi_sector = sector - (offset >> BBSHIFT);
Nathan Scottce8e9222006-01-11 15:39:08 +11001202 bio->bi_end_io = xfs_buf_bio_end_io;
1203 bio->bi_private = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Nathan Scottce8e9222006-01-11 15:39:08 +11001205 bio_add_page(bio, bp->b_pages[0], PAGE_CACHE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 size = 0;
1207
Nathan Scottce8e9222006-01-11 15:39:08 +11001208 atomic_inc(&bp->b_io_remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210 goto submit_io;
1211 }
1212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001214 atomic_inc(&bp->b_io_remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
1216 if (nr_pages > total_nr_pages)
1217 nr_pages = total_nr_pages;
1218
1219 bio = bio_alloc(GFP_NOIO, nr_pages);
Nathan Scottce8e9222006-01-11 15:39:08 +11001220 bio->bi_bdev = bp->b_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 bio->bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001222 bio->bi_end_io = xfs_buf_bio_end_io;
1223 bio->bi_private = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225 for (; size && nr_pages; nr_pages--, map_i++) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001226 int rbytes, nbytes = PAGE_CACHE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 if (nbytes > size)
1229 nbytes = size;
1230
Nathan Scottce8e9222006-01-11 15:39:08 +11001231 rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset);
1232 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 break;
1234
1235 offset = 0;
1236 sector += nbytes >> BBSHIFT;
1237 size -= nbytes;
1238 total_nr_pages--;
1239 }
1240
1241submit_io:
1242 if (likely(bio->bi_size)) {
James Bottomley73c77e22010-01-25 11:42:24 -06001243 if (xfs_buf_is_vmapped(bp)) {
1244 flush_kernel_vmap_range(bp->b_addr,
1245 xfs_buf_vmap_len(bp));
1246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 submit_bio(rw, bio);
1248 if (size)
1249 goto next_chunk;
1250 } else {
1251 bio_put(bio);
Nathan Scottce8e9222006-01-11 15:39:08 +11001252 xfs_buf_ioerror(bp, EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
1254}
1255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256int
Nathan Scottce8e9222006-01-11 15:39:08 +11001257xfs_buf_iorequest(
1258 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001260 trace_xfs_buf_iorequest(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Nathan Scottce8e9222006-01-11 15:39:08 +11001262 if (bp->b_flags & XBF_DELWRI) {
1263 xfs_buf_delwri_queue(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 return 0;
1265 }
1266
Nathan Scottce8e9222006-01-11 15:39:08 +11001267 if (bp->b_flags & XBF_WRITE) {
1268 xfs_buf_wait_unpin(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 }
1270
Nathan Scottce8e9222006-01-11 15:39:08 +11001271 xfs_buf_hold(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 /* Set the count to 1 initially, this will stop an I/O
1274 * completion callout which happens before we have started
Nathan Scottce8e9222006-01-11 15:39:08 +11001275 * all the I/O from calling xfs_buf_ioend too early.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001277 atomic_set(&bp->b_io_remaining, 1);
1278 _xfs_buf_ioapply(bp);
1279 _xfs_buf_ioend(bp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Nathan Scottce8e9222006-01-11 15:39:08 +11001281 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 return 0;
1283}
1284
1285/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001286 * Waits for I/O to complete on the buffer supplied.
1287 * It returns immediately if no I/O is pending.
1288 * It returns the I/O error code, if any, or 0 if there was no error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 */
1290int
Nathan Scottce8e9222006-01-11 15:39:08 +11001291xfs_buf_iowait(
1292 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001294 trace_xfs_buf_iowait(bp, _RET_IP_);
1295
Nathan Scottce8e9222006-01-11 15:39:08 +11001296 if (atomic_read(&bp->b_io_remaining))
1297 blk_run_address_space(bp->b_target->bt_mapping);
David Chinnerb4dd3302008-08-13 16:36:11 +10001298 wait_for_completion(&bp->b_iowait);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001299
1300 trace_xfs_buf_iowait_done(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +11001301 return bp->b_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302}
1303
Nathan Scottce8e9222006-01-11 15:39:08 +11001304xfs_caddr_t
1305xfs_buf_offset(
1306 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 size_t offset)
1308{
1309 struct page *page;
1310
Nathan Scottce8e9222006-01-11 15:39:08 +11001311 if (bp->b_flags & XBF_MAPPED)
1312 return XFS_BUF_PTR(bp) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Nathan Scottce8e9222006-01-11 15:39:08 +11001314 offset += bp->b_offset;
1315 page = bp->b_pages[offset >> PAGE_CACHE_SHIFT];
1316 return (xfs_caddr_t)page_address(page) + (offset & (PAGE_CACHE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317}
1318
1319/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 * Move data into or out of a buffer.
1321 */
1322void
Nathan Scottce8e9222006-01-11 15:39:08 +11001323xfs_buf_iomove(
1324 xfs_buf_t *bp, /* buffer to process */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 size_t boff, /* starting buffer offset */
1326 size_t bsize, /* length to copy */
1327 caddr_t data, /* data address */
Nathan Scottce8e9222006-01-11 15:39:08 +11001328 xfs_buf_rw_t mode) /* read/write/zero flag */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
1330 size_t bend, cpoff, csize;
1331 struct page *page;
1332
1333 bend = boff + bsize;
1334 while (boff < bend) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001335 page = bp->b_pages[xfs_buf_btoct(boff + bp->b_offset)];
1336 cpoff = xfs_buf_poff(boff + bp->b_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 csize = min_t(size_t,
Nathan Scottce8e9222006-01-11 15:39:08 +11001338 PAGE_CACHE_SIZE-cpoff, bp->b_count_desired-boff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 ASSERT(((csize + cpoff) <= PAGE_CACHE_SIZE));
1341
1342 switch (mode) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001343 case XBRW_ZERO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 memset(page_address(page) + cpoff, 0, csize);
1345 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001346 case XBRW_READ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 memcpy(data, page_address(page) + cpoff, csize);
1348 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001349 case XBRW_WRITE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 memcpy(page_address(page) + cpoff, data, csize);
1351 }
1352
1353 boff += csize;
1354 data += csize;
1355 }
1356}
1357
1358/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001359 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 */
1361
1362/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001363 * Wait for any bufs with callbacks that have been submitted but
1364 * have not yet returned... walk the hash list for the target.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 */
1366void
1367xfs_wait_buftarg(
1368 xfs_buftarg_t *btp)
1369{
1370 xfs_buf_t *bp, *n;
1371 xfs_bufhash_t *hash;
1372 uint i;
1373
1374 for (i = 0; i < (1 << btp->bt_hashshift); i++) {
1375 hash = &btp->bt_hash[i];
1376again:
1377 spin_lock(&hash->bh_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +11001378 list_for_each_entry_safe(bp, n, &hash->bh_list, b_hash_list) {
1379 ASSERT(btp == bp->b_target);
1380 if (!(bp->b_flags & XBF_FS_MANAGED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 spin_unlock(&hash->bh_lock);
David Chinner2f926582005-09-05 08:33:35 +10001382 /*
1383 * Catch superblock reference count leaks
1384 * immediately
1385 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001386 BUG_ON(bp->b_bn == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 delay(100);
1388 goto again;
1389 }
1390 }
1391 spin_unlock(&hash->bh_lock);
1392 }
1393}
1394
1395/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001396 * Allocate buffer hash table for a given target.
1397 * For devices containing metadata (i.e. not the log/realtime devices)
1398 * we need to allocate a much larger hash table.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 */
1400STATIC void
1401xfs_alloc_bufhash(
1402 xfs_buftarg_t *btp,
1403 int external)
1404{
1405 unsigned int i;
1406
1407 btp->bt_hashshift = external ? 3 : 8; /* 8 or 256 buckets */
1408 btp->bt_hashmask = (1 << btp->bt_hashshift) - 1;
1409 btp->bt_hash = kmem_zalloc((1 << btp->bt_hashshift) *
Vlad Apostolov93c189c2006-11-11 18:03:49 +11001410 sizeof(xfs_bufhash_t), KM_SLEEP | KM_LARGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 for (i = 0; i < (1 << btp->bt_hashshift); i++) {
1412 spin_lock_init(&btp->bt_hash[i].bh_lock);
1413 INIT_LIST_HEAD(&btp->bt_hash[i].bh_list);
1414 }
1415}
1416
1417STATIC void
1418xfs_free_bufhash(
1419 xfs_buftarg_t *btp)
1420{
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001421 kmem_free(btp->bt_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 btp->bt_hash = NULL;
1423}
1424
David Chinnera6867a62006-01-11 15:37:58 +11001425/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001426 * buftarg list for delwrite queue processing
David Chinnera6867a62006-01-11 15:37:58 +11001427 */
Tim Shimmine6a0e9c2007-05-08 13:49:59 +10001428static LIST_HEAD(xfs_buftarg_list);
David Chinner7989cb82007-02-10 18:34:56 +11001429static DEFINE_SPINLOCK(xfs_buftarg_lock);
David Chinnera6867a62006-01-11 15:37:58 +11001430
1431STATIC void
1432xfs_register_buftarg(
1433 xfs_buftarg_t *btp)
1434{
1435 spin_lock(&xfs_buftarg_lock);
1436 list_add(&btp->bt_list, &xfs_buftarg_list);
1437 spin_unlock(&xfs_buftarg_lock);
1438}
1439
1440STATIC void
1441xfs_unregister_buftarg(
1442 xfs_buftarg_t *btp)
1443{
1444 spin_lock(&xfs_buftarg_lock);
1445 list_del(&btp->bt_list);
1446 spin_unlock(&xfs_buftarg_lock);
1447}
1448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449void
1450xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001451 struct xfs_mount *mp,
1452 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
1454 xfs_flush_buftarg(btp, 1);
Christoph Hellwigb7963132009-03-03 14:48:37 -05001455 if (mp->m_flags & XFS_MOUNT_BARRIER)
1456 xfs_blkdev_issue_flush(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 xfs_free_bufhash(btp);
Nathan Scottce8e9222006-01-11 15:39:08 +11001458 iput(btp->bt_mapping->host);
David Chinnera6867a62006-01-11 15:37:58 +11001459
Nathan Scottce8e9222006-01-11 15:39:08 +11001460 /* Unregister the buftarg first so that we don't get a
1461 * wakeup finding a non-existent task
1462 */
David Chinnera6867a62006-01-11 15:37:58 +11001463 xfs_unregister_buftarg(btp);
1464 kthread_stop(btp->bt_task);
1465
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001466 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467}
1468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469STATIC int
1470xfs_setsize_buftarg_flags(
1471 xfs_buftarg_t *btp,
1472 unsigned int blocksize,
1473 unsigned int sectorsize,
1474 int verbose)
1475{
Nathan Scottce8e9222006-01-11 15:39:08 +11001476 btp->bt_bsize = blocksize;
1477 btp->bt_sshift = ffs(sectorsize) - 1;
1478 btp->bt_smask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Nathan Scottce8e9222006-01-11 15:39:08 +11001480 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 printk(KERN_WARNING
1482 "XFS: Cannot set_blocksize to %u on device %s\n",
1483 sectorsize, XFS_BUFTARG_NAME(btp));
1484 return EINVAL;
1485 }
1486
1487 if (verbose &&
1488 (PAGE_CACHE_SIZE / BITS_PER_LONG) > sectorsize) {
1489 printk(KERN_WARNING
1490 "XFS: %u byte sectors in use on device %s. "
1491 "This is suboptimal; %u or greater is ideal.\n",
1492 sectorsize, XFS_BUFTARG_NAME(btp),
1493 (unsigned int)PAGE_CACHE_SIZE / BITS_PER_LONG);
1494 }
1495
1496 return 0;
1497}
1498
1499/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001500 * When allocating the initial buffer target we have not yet
1501 * read in the superblock, so don't know what sized sectors
1502 * are being used is at this early stage. Play safe.
1503 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504STATIC int
1505xfs_setsize_buftarg_early(
1506 xfs_buftarg_t *btp,
1507 struct block_device *bdev)
1508{
1509 return xfs_setsize_buftarg_flags(btp,
Martin K. Petersene1defc42009-05-22 17:17:49 -04001510 PAGE_CACHE_SIZE, bdev_logical_block_size(bdev), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511}
1512
1513int
1514xfs_setsize_buftarg(
1515 xfs_buftarg_t *btp,
1516 unsigned int blocksize,
1517 unsigned int sectorsize)
1518{
1519 return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
1520}
1521
1522STATIC int
1523xfs_mapping_buftarg(
1524 xfs_buftarg_t *btp,
1525 struct block_device *bdev)
1526{
1527 struct backing_dev_info *bdi;
1528 struct inode *inode;
1529 struct address_space *mapping;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07001530 static const struct address_space_operations mapping_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 .sync_page = block_sync_page,
Christoph Lametere965f962006-02-01 03:05:41 -08001532 .migratepage = fail_migrate_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 };
1534
1535 inode = new_inode(bdev->bd_inode->i_sb);
1536 if (!inode) {
1537 printk(KERN_WARNING
1538 "XFS: Cannot allocate mapping inode for device %s\n",
1539 XFS_BUFTARG_NAME(btp));
1540 return ENOMEM;
1541 }
1542 inode->i_mode = S_IFBLK;
1543 inode->i_bdev = bdev;
1544 inode->i_rdev = bdev->bd_dev;
1545 bdi = blk_get_backing_dev_info(bdev);
1546 if (!bdi)
1547 bdi = &default_backing_dev_info;
1548 mapping = &inode->i_data;
1549 mapping->a_ops = &mapping_aops;
1550 mapping->backing_dev_info = bdi;
1551 mapping_set_gfp_mask(mapping, GFP_NOFS);
Nathan Scottce8e9222006-01-11 15:39:08 +11001552 btp->bt_mapping = mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 return 0;
1554}
1555
David Chinnera6867a62006-01-11 15:37:58 +11001556STATIC int
1557xfs_alloc_delwrite_queue(
1558 xfs_buftarg_t *btp)
1559{
1560 int error = 0;
1561
1562 INIT_LIST_HEAD(&btp->bt_list);
1563 INIT_LIST_HEAD(&btp->bt_delwrite_queue);
Eric Sandeen007c61c2007-10-11 17:43:56 +10001564 spin_lock_init(&btp->bt_delwrite_lock);
David Chinnera6867a62006-01-11 15:37:58 +11001565 btp->bt_flags = 0;
1566 btp->bt_task = kthread_run(xfsbufd, btp, "xfsbufd");
1567 if (IS_ERR(btp->bt_task)) {
1568 error = PTR_ERR(btp->bt_task);
1569 goto out_error;
1570 }
1571 xfs_register_buftarg(btp);
1572out_error:
1573 return error;
1574}
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576xfs_buftarg_t *
1577xfs_alloc_buftarg(
1578 struct block_device *bdev,
1579 int external)
1580{
1581 xfs_buftarg_t *btp;
1582
1583 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
1584
Nathan Scottce8e9222006-01-11 15:39:08 +11001585 btp->bt_dev = bdev->bd_dev;
1586 btp->bt_bdev = bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 if (xfs_setsize_buftarg_early(btp, bdev))
1588 goto error;
1589 if (xfs_mapping_buftarg(btp, bdev))
1590 goto error;
David Chinnera6867a62006-01-11 15:37:58 +11001591 if (xfs_alloc_delwrite_queue(btp))
1592 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 xfs_alloc_bufhash(btp, external);
1594 return btp;
1595
1596error:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001597 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 return NULL;
1599}
1600
1601
1602/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001603 * Delayed write buffer handling
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001606xfs_buf_delwri_queue(
1607 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 int unlock)
1609{
Nathan Scottce8e9222006-01-11 15:39:08 +11001610 struct list_head *dwq = &bp->b_target->bt_delwrite_queue;
1611 spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
David Chinnera6867a62006-01-11 15:37:58 +11001612
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001613 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
1614
Nathan Scottce8e9222006-01-11 15:39:08 +11001615 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
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001654 trace_xfs_buf_delwri_dequeue(bp, _RET_IP_);
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
David Chinner585e6d82007-02-10 18:32:29 +11001682/*
1683 * Move as many buffers as specified to the supplied list
1684 * idicating if we skipped any buffers to prevent deadlocks.
1685 */
1686STATIC int
1687xfs_buf_delwri_split(
1688 xfs_buftarg_t *target,
1689 struct list_head *list,
David Chinner5e6a07d2007-02-10 18:34:49 +11001690 unsigned long age)
David Chinner585e6d82007-02-10 18:32:29 +11001691{
1692 xfs_buf_t *bp, *n;
1693 struct list_head *dwq = &target->bt_delwrite_queue;
1694 spinlock_t *dwlk = &target->bt_delwrite_lock;
1695 int skipped = 0;
David Chinner5e6a07d2007-02-10 18:34:49 +11001696 int force;
David Chinner585e6d82007-02-10 18:32:29 +11001697
David Chinner5e6a07d2007-02-10 18:34:49 +11001698 force = test_and_clear_bit(XBT_FORCE_FLUSH, &target->bt_flags);
David Chinner585e6d82007-02-10 18:32:29 +11001699 INIT_LIST_HEAD(list);
1700 spin_lock(dwlk);
1701 list_for_each_entry_safe(bp, n, dwq, b_list) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001702 trace_xfs_buf_delwri_split(bp, _RET_IP_);
David Chinner585e6d82007-02-10 18:32:29 +11001703 ASSERT(bp->b_flags & XBF_DELWRI);
1704
1705 if (!xfs_buf_ispin(bp) && !xfs_buf_cond_lock(bp)) {
David Chinner5e6a07d2007-02-10 18:34:49 +11001706 if (!force &&
David Chinner585e6d82007-02-10 18:32:29 +11001707 time_before(jiffies, bp->b_queuetime + age)) {
1708 xfs_buf_unlock(bp);
1709 break;
1710 }
1711
1712 bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q|
1713 _XBF_RUN_QUEUES);
1714 bp->b_flags |= XBF_WRITE;
1715 list_move_tail(&bp->b_list, list);
1716 } else
1717 skipped++;
1718 }
1719 spin_unlock(dwlk);
1720
1721 return skipped;
1722
1723}
1724
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725STATIC int
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001726xfsbufd(
David Chinner585e6d82007-02-10 18:32:29 +11001727 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
David Chinner585e6d82007-02-10 18:32:29 +11001729 struct list_head tmp;
1730 xfs_buftarg_t *target = (xfs_buftarg_t *)data;
1731 int count;
1732 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 current->flags |= PF_MEMALLOC;
1735
Rafael J. Wysocki978c7b22007-12-07 14:09:02 +11001736 set_freezable();
1737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 do {
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001739 if (unlikely(freezing(current))) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001740 set_bit(XBT_FORCE_SLEEP, &target->bt_flags);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001741 refrigerator();
Nathan Scottabd0cf72005-05-05 13:30:13 -07001742 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +11001743 clear_bit(XBT_FORCE_SLEEP, &target->bt_flags);
Nathan Scottabd0cf72005-05-05 13:30:13 -07001744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Nathan Scott15c84a42005-11-04 10:51:01 +11001746 schedule_timeout_interruptible(
1747 xfs_buf_timer_centisecs * msecs_to_jiffies(10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
David Chinner585e6d82007-02-10 18:32:29 +11001749 xfs_buf_delwri_split(target, &tmp,
David Chinner5e6a07d2007-02-10 18:34:49 +11001750 xfs_buf_age_centisecs * msecs_to_jiffies(10));
David Chinner585e6d82007-02-10 18:32:29 +11001751
Nathan Scottf07c2252006-09-28 10:52:15 +10001752 count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 while (!list_empty(&tmp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001754 bp = list_entry(tmp.next, xfs_buf_t, b_list);
1755 ASSERT(target == bp->b_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Nathan Scottce8e9222006-01-11 15:39:08 +11001757 list_del_init(&bp->b_list);
1758 xfs_buf_iostrategy(bp);
David Chinner585e6d82007-02-10 18:32:29 +11001759 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 }
1761
Felix Blyakher3a011a12009-02-18 15:56:51 -06001762 if (as_list_len > 0)
1763 purge_addresses();
Nathan Scottf07c2252006-09-28 10:52:15 +10001764 if (count)
1765 blk_run_address_space(target->bt_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Christoph Hellwig4df08c52005-09-05 08:34:18 +10001767 } while (!kthread_should_stop());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
Christoph Hellwig4df08c52005-09-05 08:34:18 +10001769 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770}
1771
1772/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001773 * Go through all incore buffers, and release buffers if they belong to
1774 * the given device. This is used in filesystem error handling to
1775 * preserve the consistency of its metadata.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 */
1777int
1778xfs_flush_buftarg(
David Chinner585e6d82007-02-10 18:32:29 +11001779 xfs_buftarg_t *target,
1780 int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
David Chinner585e6d82007-02-10 18:32:29 +11001782 struct list_head tmp;
1783 xfs_buf_t *bp, *n;
1784 int pincount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Dave Chinnerc626d172009-04-06 18:42:11 +02001786 xfs_buf_runall_queues(xfsconvertd_workqueue);
Nathan Scottce8e9222006-01-11 15:39:08 +11001787 xfs_buf_runall_queues(xfsdatad_workqueue);
1788 xfs_buf_runall_queues(xfslogd_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
David Chinner5e6a07d2007-02-10 18:34:49 +11001790 set_bit(XBT_FORCE_FLUSH, &target->bt_flags);
1791 pincount = xfs_buf_delwri_split(target, &tmp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
1793 /*
1794 * Dropped the delayed write list lock, now walk the temporary list
1795 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001796 list_for_each_entry_safe(bp, n, &tmp, b_list) {
David Chinner585e6d82007-02-10 18:32:29 +11001797 ASSERT(target == bp->b_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 if (wait)
Nathan Scottce8e9222006-01-11 15:39:08 +11001799 bp->b_flags &= ~XBF_ASYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 else
Nathan Scottce8e9222006-01-11 15:39:08 +11001801 list_del_init(&bp->b_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Nathan Scottce8e9222006-01-11 15:39:08 +11001803 xfs_buf_iostrategy(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 }
1805
Nathan Scottf07c2252006-09-28 10:52:15 +10001806 if (wait)
1807 blk_run_address_space(target->bt_mapping);
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 /*
1810 * Remaining list items must be flushed before returning
1811 */
1812 while (!list_empty(&tmp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001813 bp = list_entry(tmp.next, xfs_buf_t, b_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Nathan Scottce8e9222006-01-11 15:39:08 +11001815 list_del_init(&bp->b_list);
1816 xfs_iowait(bp);
1817 xfs_buf_relse(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 }
1819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 return pincount;
1821}
1822
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001823int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11001824xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
Nathan Scott87582802006-03-14 13:18:19 +11001826 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
1827 KM_ZONE_HWALIGN, NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11001828 if (!xfs_buf_zone)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001829 goto out;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001830
Rafael J. Wysockib4337692007-03-22 00:11:27 -08001831 xfslogd_workqueue = create_workqueue("xfslogd");
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001832 if (!xfslogd_workqueue)
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001833 goto out_free_buf_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Rafael J. Wysockib4337692007-03-22 00:11:27 -08001835 xfsdatad_workqueue = create_workqueue("xfsdatad");
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001836 if (!xfsdatad_workqueue)
1837 goto out_destroy_xfslogd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Dave Chinnerc626d172009-04-06 18:42:11 +02001839 xfsconvertd_workqueue = create_workqueue("xfsconvertd");
1840 if (!xfsconvertd_workqueue)
1841 goto out_destroy_xfsdatad_workqueue;
1842
Rusty Russell8e1f9362007-07-17 04:03:17 -07001843 register_shrinker(&xfs_buf_shake);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001844 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Dave Chinnerc626d172009-04-06 18:42:11 +02001846 out_destroy_xfsdatad_workqueue:
1847 destroy_workqueue(xfsdatad_workqueue);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001848 out_destroy_xfslogd_workqueue:
1849 destroy_workqueue(xfslogd_workqueue);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001850 out_free_buf_zone:
Nathan Scottce8e9222006-01-11 15:39:08 +11001851 kmem_zone_destroy(xfs_buf_zone);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001852 out:
Nathan Scott87582802006-03-14 13:18:19 +11001853 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854}
1855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856void
Nathan Scottce8e9222006-01-11 15:39:08 +11001857xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858{
Rusty Russell8e1f9362007-07-17 04:03:17 -07001859 unregister_shrinker(&xfs_buf_shake);
Dave Chinnerc626d172009-04-06 18:42:11 +02001860 destroy_workqueue(xfsconvertd_workqueue);
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864}
Tim Shimmine6a0e9c2007-05-08 13:49:59 +10001865
1866#ifdef CONFIG_KDB_MODULES
1867struct list_head *
1868xfs_get_buftarg_list(void)
1869{
1870 return &xfs_buftarg_list;
1871}
1872#endif