blob: d50df3a8101cdb50abffdd65d8c656e52262c787 [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>
Dave Chinner089716a2010-01-26 15:13:25 +110036#include <linux/list_sort.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Christoph Hellwigb7963132009-03-03 14:48:37 -050038#include "xfs_sb.h"
39#include "xfs_inum.h"
40#include "xfs_ag.h"
41#include "xfs_dmapi.h"
42#include "xfs_mount.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000043#include "xfs_trace.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050044
David Chinner7989cb82007-02-10 18:34:56 +110045static kmem_zone_t *xfs_buf_zone;
David Chinnera6867a62006-01-11 15:37:58 +110046STATIC int xfsbufd(void *);
Al Viro27496a82005-10-21 03:20:48 -040047STATIC int xfsbufd_wakeup(int, gfp_t);
Nathan Scottce8e9222006-01-11 15:39:08 +110048STATIC void xfs_buf_delwri_queue(xfs_buf_t *, int);
Rusty Russell8e1f9362007-07-17 04:03:17 -070049static struct shrinker xfs_buf_shake = {
50 .shrink = xfsbufd_wakeup,
51 .seeks = DEFAULT_SEEKS,
52};
Christoph Hellwig23ea4032005-06-21 15:14:01 +100053
David Chinner7989cb82007-02-10 18:34:56 +110054static struct workqueue_struct *xfslogd_workqueue;
Christoph Hellwig0829c362005-09-02 16:58:49 +100055struct workqueue_struct *xfsdatad_workqueue;
Dave Chinnerc626d172009-04-06 18:42:11 +020056struct workqueue_struct *xfsconvertd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Nathan Scottce8e9222006-01-11 15:39:08 +110058#ifdef XFS_BUF_LOCK_TRACKING
59# define XB_SET_OWNER(bp) ((bp)->b_last_holder = current->pid)
60# define XB_CLEAR_OWNER(bp) ((bp)->b_last_holder = -1)
61# define XB_GET_OWNER(bp) ((bp)->b_last_holder)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#else
Nathan Scottce8e9222006-01-11 15:39:08 +110063# define XB_SET_OWNER(bp) do { } while (0)
64# define XB_CLEAR_OWNER(bp) do { } while (0)
65# define XB_GET_OWNER(bp) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#endif
67
Nathan Scottce8e9222006-01-11 15:39:08 +110068#define xb_to_gfp(flags) \
69 ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : \
70 ((flags) & XBF_DONT_BLOCK) ? GFP_NOFS : GFP_KERNEL) | __GFP_NOWARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Nathan Scottce8e9222006-01-11 15:39:08 +110072#define xb_to_km(flags) \
73 (((flags) & XBF_DONT_BLOCK) ? KM_NOFS : KM_SLEEP)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Nathan Scottce8e9222006-01-11 15:39:08 +110075#define xfs_buf_allocate(flags) \
76 kmem_zone_alloc(xfs_buf_zone, xb_to_km(flags))
77#define xfs_buf_deallocate(bp) \
78 kmem_zone_free(xfs_buf_zone, (bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80/*
Nathan Scottce8e9222006-01-11 15:39:08 +110081 * Page Region interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 *
Nathan Scottce8e9222006-01-11 15:39:08 +110083 * For pages in filesystems where the blocksize is smaller than the
84 * pagesize, we use the page->private field (long) to hold a bitmap
85 * of uptodate regions within the page.
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 *
Nathan Scottce8e9222006-01-11 15:39:08 +110087 * Each such region is "bytes per page / bits per long" bytes long.
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 *
Nathan Scottce8e9222006-01-11 15:39:08 +110089 * NBPPR == number-of-bytes-per-page-region
90 * BTOPR == bytes-to-page-region (rounded up)
91 * BTOPRT == bytes-to-page-region-truncated (rounded down)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 */
93#if (BITS_PER_LONG == 32)
94#define PRSHIFT (PAGE_CACHE_SHIFT - 5) /* (32 == 1<<5) */
95#elif (BITS_PER_LONG == 64)
96#define PRSHIFT (PAGE_CACHE_SHIFT - 6) /* (64 == 1<<6) */
97#else
98#error BITS_PER_LONG must be 32 or 64
99#endif
100#define NBPPR (PAGE_CACHE_SIZE/BITS_PER_LONG)
101#define BTOPR(b) (((unsigned int)(b) + (NBPPR - 1)) >> PRSHIFT)
102#define BTOPRT(b) (((unsigned int)(b) >> PRSHIFT))
103
104STATIC unsigned long
105page_region_mask(
106 size_t offset,
107 size_t length)
108{
109 unsigned long mask;
110 int first, final;
111
112 first = BTOPR(offset);
113 final = BTOPRT(offset + length - 1);
114 first = min(first, final);
115
116 mask = ~0UL;
117 mask <<= BITS_PER_LONG - (final - first);
118 mask >>= BITS_PER_LONG - (final);
119
120 ASSERT(offset + length <= PAGE_CACHE_SIZE);
121 ASSERT((final - first) < BITS_PER_LONG && (final - first) >= 0);
122
123 return mask;
124}
125
Christoph Hellwigb8f82a42009-11-14 16:17:22 +0000126STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127set_page_region(
128 struct page *page,
129 size_t offset,
130 size_t length)
131{
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700132 set_page_private(page,
133 page_private(page) | page_region_mask(offset, length));
134 if (page_private(page) == ~0UL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 SetPageUptodate(page);
136}
137
Christoph Hellwigb8f82a42009-11-14 16:17:22 +0000138STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139test_page_region(
140 struct page *page,
141 size_t offset,
142 size_t length)
143{
144 unsigned long mask = page_region_mask(offset, length);
145
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700146 return (mask && (page_private(page) & mask) == mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
149/*
Felix Blyakher3a011a12009-02-18 15:56:51 -0600150 * Mapping of multi-page buffers into contiguous virtual space
151 */
152
153typedef struct a_list {
154 void *vm_addr;
155 struct a_list *next;
156} a_list_t;
157
158static a_list_t *as_free_head;
159static int as_list_len;
160static DEFINE_SPINLOCK(as_lock);
161
162/*
163 * Try to batch vunmaps because they are costly.
164 */
165STATIC void
166free_address(
167 void *addr)
168{
169 a_list_t *aentry;
170
171#ifdef CONFIG_XEN
172 /*
173 * Xen needs to be able to make sure it can get an exclusive
174 * RO mapping of pages it wants to turn into a pagetable. If
175 * a newly allocated page is also still being vmap()ed by xfs,
176 * it will cause pagetable construction to fail. This is a
177 * quick workaround to always eagerly unmap pages so that Xen
178 * is happy.
179 */
180 vunmap(addr);
181 return;
182#endif
183
184 aentry = kmalloc(sizeof(a_list_t), GFP_NOWAIT);
185 if (likely(aentry)) {
186 spin_lock(&as_lock);
187 aentry->next = as_free_head;
188 aentry->vm_addr = addr;
189 as_free_head = aentry;
190 as_list_len++;
191 spin_unlock(&as_lock);
192 } else {
193 vunmap(addr);
194 }
195}
196
197STATIC void
198purge_addresses(void)
199{
200 a_list_t *aentry, *old;
201
202 if (as_free_head == NULL)
203 return;
204
205 spin_lock(&as_lock);
206 aentry = as_free_head;
207 as_free_head = NULL;
208 as_list_len = 0;
209 spin_unlock(&as_lock);
210
211 while ((old = aentry) != NULL) {
212 vunmap(aentry->vm_addr);
213 aentry = aentry->next;
214 kfree(old);
215 }
216}
217
218/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100219 * Internal xfs_buf_t object manipulation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 */
221
222STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100223_xfs_buf_initialize(
224 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100226 xfs_off_t range_base,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 size_t range_length,
Nathan Scottce8e9222006-01-11 15:39:08 +1100228 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 /*
Nathan Scottce8e9222006-01-11 15:39:08 +1100231 * We don't want certain flags to appear in b_flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100233 flags &= ~(XBF_LOCK|XBF_MAPPED|XBF_DONT_BLOCK|XBF_READ_AHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Nathan Scottce8e9222006-01-11 15:39:08 +1100235 memset(bp, 0, sizeof(xfs_buf_t));
236 atomic_set(&bp->b_hold, 1);
David Chinnerb4dd3302008-08-13 16:36:11 +1000237 init_completion(&bp->b_iowait);
Nathan Scottce8e9222006-01-11 15:39:08 +1100238 INIT_LIST_HEAD(&bp->b_list);
239 INIT_LIST_HEAD(&bp->b_hash_list);
240 init_MUTEX_LOCKED(&bp->b_sema); /* held, no waiters */
241 XB_SET_OWNER(bp);
242 bp->b_target = target;
243 bp->b_file_offset = range_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 /*
245 * Set buffer_length and count_desired to the same value initially.
246 * I/O routines should use count_desired, which will be the same in
247 * most cases but may be reset (e.g. XFS recovery).
248 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100249 bp->b_buffer_length = bp->b_count_desired = range_length;
250 bp->b_flags = flags;
251 bp->b_bn = XFS_BUF_DADDR_NULL;
252 atomic_set(&bp->b_pin_count, 0);
253 init_waitqueue_head(&bp->b_waiters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Nathan Scottce8e9222006-01-11 15:39:08 +1100255 XFS_STATS_INC(xb_create);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000256
257 trace_xfs_buf_init(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
260/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100261 * Allocate a page array capable of holding a specified number
262 * of pages, and point the page buf at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 */
264STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100265_xfs_buf_get_pages(
266 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 int page_count,
Nathan Scottce8e9222006-01-11 15:39:08 +1100268 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
270 /* Make sure that we have a page list */
Nathan Scottce8e9222006-01-11 15:39:08 +1100271 if (bp->b_pages == NULL) {
272 bp->b_offset = xfs_buf_poff(bp->b_file_offset);
273 bp->b_page_count = page_count;
274 if (page_count <= XB_PAGES) {
275 bp->b_pages = bp->b_page_array;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100277 bp->b_pages = kmem_alloc(sizeof(struct page *) *
278 page_count, xb_to_km(flags));
279 if (bp->b_pages == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return -ENOMEM;
281 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100282 memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
284 return 0;
285}
286
287/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100288 * Frees b_pages if it was allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 */
290STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100291_xfs_buf_free_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 xfs_buf_t *bp)
293{
Nathan Scottce8e9222006-01-11 15:39:08 +1100294 if (bp->b_pages != bp->b_page_array) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000295 kmem_free(bp->b_pages);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000296 bp->b_pages = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
298}
299
300/*
301 * Releases the specified buffer.
302 *
303 * The modification state of any associated pages is left unchanged.
Nathan Scottce8e9222006-01-11 15:39:08 +1100304 * The buffer most not be on any hash - use xfs_buf_rele instead for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 * hashed and refcounted buffers
306 */
307void
Nathan Scottce8e9222006-01-11 15:39:08 +1100308xfs_buf_free(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 xfs_buf_t *bp)
310{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000311 trace_xfs_buf_free(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Nathan Scottce8e9222006-01-11 15:39:08 +1100313 ASSERT(list_empty(&bp->b_hash_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000315 if (bp->b_flags & (_XBF_PAGE_CACHE|_XBF_PAGES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 uint i;
317
Nathan Scottce8e9222006-01-11 15:39:08 +1100318 if ((bp->b_flags & XBF_MAPPED) && (bp->b_page_count > 1))
Felix Blyakher3a011a12009-02-18 15:56:51 -0600319 free_address(bp->b_addr - bp->b_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Nathan Scott948ecdb2006-09-28 11:03:13 +1000321 for (i = 0; i < bp->b_page_count; i++) {
322 struct page *page = bp->b_pages[i];
323
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000324 if (bp->b_flags & _XBF_PAGE_CACHE)
325 ASSERT(!PagePrivate(page));
Nathan Scott948ecdb2006-09-28 11:03:13 +1000326 page_cache_release(page);
327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
Dave Chinner3fc98b12009-12-14 23:11:57 +0000329 _xfs_buf_free_pages(bp);
Nathan Scottce8e9222006-01-11 15:39:08 +1100330 xfs_buf_deallocate(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
333/*
334 * Finds all pages for buffer in question and builds it's page list.
335 */
336STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100337_xfs_buf_lookup_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 xfs_buf_t *bp,
339 uint flags)
340{
Nathan Scottce8e9222006-01-11 15:39:08 +1100341 struct address_space *mapping = bp->b_target->bt_mapping;
342 size_t blocksize = bp->b_target->bt_bsize;
343 size_t size = bp->b_count_desired;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 size_t nbytes, offset;
Nathan Scottce8e9222006-01-11 15:39:08 +1100345 gfp_t gfp_mask = xb_to_gfp(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 unsigned short page_count, i;
347 pgoff_t first;
Nathan Scott204ab252006-01-11 20:50:22 +1100348 xfs_off_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 int error;
350
Nathan Scottce8e9222006-01-11 15:39:08 +1100351 end = bp->b_file_offset + bp->b_buffer_length;
352 page_count = xfs_buf_btoc(end) - xfs_buf_btoct(bp->b_file_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Nathan Scottce8e9222006-01-11 15:39:08 +1100354 error = _xfs_buf_get_pages(bp, page_count, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (unlikely(error))
356 return error;
Nathan Scottce8e9222006-01-11 15:39:08 +1100357 bp->b_flags |= _XBF_PAGE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Nathan Scottce8e9222006-01-11 15:39:08 +1100359 offset = bp->b_offset;
360 first = bp->b_file_offset >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Nathan Scottce8e9222006-01-11 15:39:08 +1100362 for (i = 0; i < bp->b_page_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 struct page *page;
364 uint retries = 0;
365
366 retry:
367 page = find_or_create_page(mapping, first + i, gfp_mask);
368 if (unlikely(page == NULL)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100369 if (flags & XBF_READ_AHEAD) {
370 bp->b_page_count = i;
Christoph Hellwig6ab455e2008-05-19 16:34:42 +1000371 for (i = 0; i < bp->b_page_count; i++)
372 unlock_page(bp->b_pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return -ENOMEM;
374 }
375
376 /*
377 * This could deadlock.
378 *
379 * But until all the XFS lowlevel code is revamped to
380 * handle buffer allocation failures we can't do much.
381 */
382 if (!(++retries % 100))
383 printk(KERN_ERR
384 "XFS: possible memory allocation "
385 "deadlock in %s (mode:0x%x)\n",
Harvey Harrison34a622b2008-04-10 12:19:21 +1000386 __func__, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Nathan Scottce8e9222006-01-11 15:39:08 +1100388 XFS_STATS_INC(xb_page_retries);
Christoph Hellwig23ea4032005-06-21 15:14:01 +1000389 xfsbufd_wakeup(0, gfp_mask);
Jens Axboe8aa7e842009-07-09 14:52:32 +0200390 congestion_wait(BLK_RW_ASYNC, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 goto retry;
392 }
393
Nathan Scottce8e9222006-01-11 15:39:08 +1100394 XFS_STATS_INC(xb_page_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 nbytes = min_t(size_t, size, PAGE_CACHE_SIZE - offset);
397 size -= nbytes;
398
Nathan Scott948ecdb2006-09-28 11:03:13 +1000399 ASSERT(!PagePrivate(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (!PageUptodate(page)) {
401 page_count--;
Christoph Hellwig6ab455e2008-05-19 16:34:42 +1000402 if (blocksize >= PAGE_CACHE_SIZE) {
403 if (flags & XBF_READ)
404 bp->b_flags |= _XBF_PAGE_LOCKED;
405 } else if (!PagePrivate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (test_page_region(page, offset, nbytes))
407 page_count++;
408 }
409 }
410
Nathan Scottce8e9222006-01-11 15:39:08 +1100411 bp->b_pages[i] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 offset = 0;
413 }
414
Christoph Hellwig6ab455e2008-05-19 16:34:42 +1000415 if (!(bp->b_flags & _XBF_PAGE_LOCKED)) {
416 for (i = 0; i < bp->b_page_count; i++)
417 unlock_page(bp->b_pages[i]);
418 }
419
Nathan Scottce8e9222006-01-11 15:39:08 +1100420 if (page_count == bp->b_page_count)
421 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return error;
424}
425
426/*
427 * Map buffer into kernel address-space if nessecary.
428 */
429STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100430_xfs_buf_map_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 xfs_buf_t *bp,
432 uint flags)
433{
434 /* A single page buffer is always mappable */
Nathan Scottce8e9222006-01-11 15:39:08 +1100435 if (bp->b_page_count == 1) {
436 bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
437 bp->b_flags |= XBF_MAPPED;
438 } else if (flags & XBF_MAPPED) {
Felix Blyakher3a011a12009-02-18 15:56:51 -0600439 if (as_list_len > 64)
440 purge_addresses();
Felix Blyakhercf7dab82009-02-18 15:41:28 -0600441 bp->b_addr = vmap(bp->b_pages, bp->b_page_count,
442 VM_MAP, PAGE_KERNEL);
Nathan Scottce8e9222006-01-11 15:39:08 +1100443 if (unlikely(bp->b_addr == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return -ENOMEM;
Nathan Scottce8e9222006-01-11 15:39:08 +1100445 bp->b_addr += bp->b_offset;
446 bp->b_flags |= XBF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
448
449 return 0;
450}
451
452/*
453 * Finding and Reading Buffers
454 */
455
456/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100457 * Look up, and creates if absent, a lockable buffer for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 * a given range of an inode. The buffer is returned
459 * locked. If other overlapping buffers exist, they are
460 * released before the new buffer is created and locked,
461 * which may imply that this call will block until those buffers
462 * are unlocked. No I/O is implied by this call.
463 */
464xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100465_xfs_buf_find(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 xfs_buftarg_t *btp, /* block device target */
Nathan Scott204ab252006-01-11 20:50:22 +1100467 xfs_off_t ioff, /* starting offset of range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 size_t isize, /* length of range */
Nathan Scottce8e9222006-01-11 15:39:08 +1100469 xfs_buf_flags_t flags,
470 xfs_buf_t *new_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Nathan Scott204ab252006-01-11 20:50:22 +1100472 xfs_off_t range_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 size_t range_length;
474 xfs_bufhash_t *hash;
Nathan Scottce8e9222006-01-11 15:39:08 +1100475 xfs_buf_t *bp, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 range_base = (ioff << BBSHIFT);
478 range_length = (isize << BBSHIFT);
479
480 /* Check for IOs smaller than the sector size / not sector aligned */
Nathan Scottce8e9222006-01-11 15:39:08 +1100481 ASSERT(!(range_length < (1 << btp->bt_sshift)));
Nathan Scott204ab252006-01-11 20:50:22 +1100482 ASSERT(!(range_base & (xfs_off_t)btp->bt_smask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 hash = &btp->bt_hash[hash_long((unsigned long)ioff, btp->bt_hashshift)];
485
486 spin_lock(&hash->bh_lock);
487
Nathan Scottce8e9222006-01-11 15:39:08 +1100488 list_for_each_entry_safe(bp, n, &hash->bh_list, b_hash_list) {
489 ASSERT(btp == bp->b_target);
490 if (bp->b_file_offset == range_base &&
491 bp->b_buffer_length == range_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 /*
Nathan Scottce8e9222006-01-11 15:39:08 +1100493 * If we look at something, bring it to the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 * front of the list for next time.
495 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100496 atomic_inc(&bp->b_hold);
497 list_move(&bp->b_hash_list, &hash->bh_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 goto found;
499 }
500 }
501
502 /* No match found */
Nathan Scottce8e9222006-01-11 15:39:08 +1100503 if (new_bp) {
504 _xfs_buf_initialize(new_bp, btp, range_base,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 range_length, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100506 new_bp->b_hash = hash;
507 list_add(&new_bp->b_hash_list, &hash->bh_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100509 XFS_STATS_INC(xb_miss_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
511
512 spin_unlock(&hash->bh_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +1100513 return new_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515found:
516 spin_unlock(&hash->bh_lock);
517
518 /* Attempt to get the semaphore without sleeping,
519 * if this does not work then we need to drop the
520 * spinlock and do a hard attempt on the semaphore.
521 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100522 if (down_trylock(&bp->b_sema)) {
523 if (!(flags & XBF_TRYLOCK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 /* wait for buffer ownership */
Nathan Scottce8e9222006-01-11 15:39:08 +1100525 xfs_buf_lock(bp);
526 XFS_STATS_INC(xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 } else {
528 /* We asked for a trylock and failed, no need
529 * to look at file offset and length here, we
Nathan Scottce8e9222006-01-11 15:39:08 +1100530 * know that this buffer at least overlaps our
531 * buffer and is locked, therefore our buffer
532 * either does not exist, or is this buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100534 xfs_buf_rele(bp);
535 XFS_STATS_INC(xb_busy_locked);
536 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
538 } else {
539 /* trylock worked */
Nathan Scottce8e9222006-01-11 15:39:08 +1100540 XB_SET_OWNER(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
542
Nathan Scottce8e9222006-01-11 15:39:08 +1100543 if (bp->b_flags & XBF_STALE) {
544 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
545 bp->b_flags &= XBF_MAPPED;
David Chinner2f926582005-09-05 08:33:35 +1000546 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000547
548 trace_xfs_buf_find(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100549 XFS_STATS_INC(xb_get_locked);
550 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
553/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100554 * Assembles a buffer covering the specified range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 * Storage in memory for all portions of the buffer will be allocated,
556 * although backing storage may not be.
557 */
558xfs_buf_t *
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000559xfs_buf_get(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 xfs_buftarg_t *target,/* target for buffer */
Nathan Scott204ab252006-01-11 20:50:22 +1100561 xfs_off_t ioff, /* starting offset of range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 size_t isize, /* length of range */
Nathan Scottce8e9222006-01-11 15:39:08 +1100563 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Nathan Scottce8e9222006-01-11 15:39:08 +1100565 xfs_buf_t *bp, *new_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 int error = 0, i;
567
Nathan Scottce8e9222006-01-11 15:39:08 +1100568 new_bp = xfs_buf_allocate(flags);
569 if (unlikely(!new_bp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return NULL;
571
Nathan Scottce8e9222006-01-11 15:39:08 +1100572 bp = _xfs_buf_find(target, ioff, isize, flags, new_bp);
573 if (bp == new_bp) {
574 error = _xfs_buf_lookup_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (error)
576 goto no_buffer;
577 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100578 xfs_buf_deallocate(new_bp);
579 if (unlikely(bp == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return NULL;
581 }
582
Nathan Scottce8e9222006-01-11 15:39:08 +1100583 for (i = 0; i < bp->b_page_count; i++)
584 mark_page_accessed(bp->b_pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Nathan Scottce8e9222006-01-11 15:39:08 +1100586 if (!(bp->b_flags & XBF_MAPPED)) {
587 error = _xfs_buf_map_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (unlikely(error)) {
589 printk(KERN_WARNING "%s: failed to map pages\n",
Harvey Harrison34a622b2008-04-10 12:19:21 +1000590 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 goto no_buffer;
592 }
593 }
594
Nathan Scottce8e9222006-01-11 15:39:08 +1100595 XFS_STATS_INC(xb_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 /*
598 * Always fill in the block number now, the mapped cases can do
599 * their own overlay of this later.
600 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100601 bp->b_bn = ioff;
602 bp->b_count_desired = bp->b_buffer_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000604 trace_xfs_buf_get(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100605 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100608 if (flags & (XBF_LOCK | XBF_TRYLOCK))
609 xfs_buf_unlock(bp);
610 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 return NULL;
612}
613
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100614STATIC int
615_xfs_buf_read(
616 xfs_buf_t *bp,
617 xfs_buf_flags_t flags)
618{
619 int status;
620
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100621 ASSERT(!(flags & (XBF_DELWRI|XBF_WRITE)));
622 ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL);
623
624 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_DELWRI | \
625 XBF_READ_AHEAD | _XBF_RUN_QUEUES);
626 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | \
627 XBF_READ_AHEAD | _XBF_RUN_QUEUES);
628
629 status = xfs_buf_iorequest(bp);
630 if (!status && !(flags & XBF_ASYNC))
631 status = xfs_buf_iowait(bp);
632 return status;
633}
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635xfs_buf_t *
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000636xfs_buf_read(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100638 xfs_off_t ioff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 size_t isize,
Nathan Scottce8e9222006-01-11 15:39:08 +1100640 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Nathan Scottce8e9222006-01-11 15:39:08 +1100642 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Nathan Scottce8e9222006-01-11 15:39:08 +1100644 flags |= XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000646 bp = xfs_buf_get(target, ioff, isize, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100647 if (bp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000648 trace_xfs_buf_read(bp, flags, _RET_IP_);
649
Nathan Scottce8e9222006-01-11 15:39:08 +1100650 if (!XFS_BUF_ISDONE(bp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100651 XFS_STATS_INC(xb_get_read);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100652 _xfs_buf_read(bp, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100653 } else if (flags & XBF_ASYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 /*
655 * Read ahead call which is already satisfied,
656 * drop the buffer
657 */
658 goto no_buffer;
659 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 /* We do not want read in the flags */
Nathan Scottce8e9222006-01-11 15:39:08 +1100661 bp->b_flags &= ~XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
663 }
664
Nathan Scottce8e9222006-01-11 15:39:08 +1100665 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100668 if (flags & (XBF_LOCK | XBF_TRYLOCK))
669 xfs_buf_unlock(bp);
670 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 return NULL;
672}
673
674/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100675 * If we are not low on memory then do the readahead in a deadlock
676 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 */
678void
Nathan Scottce8e9222006-01-11 15:39:08 +1100679xfs_buf_readahead(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100681 xfs_off_t ioff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 size_t isize,
Nathan Scottce8e9222006-01-11 15:39:08 +1100683 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
685 struct backing_dev_info *bdi;
686
Nathan Scottce8e9222006-01-11 15:39:08 +1100687 bdi = target->bt_mapping->backing_dev_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (bdi_read_congested(bdi))
689 return;
690
Nathan Scottce8e9222006-01-11 15:39:08 +1100691 flags |= (XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD);
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000692 xfs_buf_read(target, ioff, isize, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
695xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100696xfs_buf_get_empty(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 size_t len,
698 xfs_buftarg_t *target)
699{
Nathan Scottce8e9222006-01-11 15:39:08 +1100700 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Nathan Scottce8e9222006-01-11 15:39:08 +1100702 bp = xfs_buf_allocate(0);
703 if (bp)
704 _xfs_buf_initialize(bp, target, 0, len, 0);
705 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
708static inline struct page *
709mem_to_page(
710 void *addr)
711{
Christoph Lameter9e2779f2008-02-04 22:28:34 -0800712 if ((!is_vmalloc_addr(addr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return virt_to_page(addr);
714 } else {
715 return vmalloc_to_page(addr);
716 }
717}
718
719int
Nathan Scottce8e9222006-01-11 15:39:08 +1100720xfs_buf_associate_memory(
721 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 void *mem,
723 size_t len)
724{
725 int rval;
726 int i = 0;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100727 unsigned long pageaddr;
728 unsigned long offset;
729 size_t buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 int page_count;
731
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100732 pageaddr = (unsigned long)mem & PAGE_CACHE_MASK;
733 offset = (unsigned long)mem - pageaddr;
734 buflen = PAGE_CACHE_ALIGN(len + offset);
735 page_count = buflen >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 /* Free any previous set of page pointers */
Nathan Scottce8e9222006-01-11 15:39:08 +1100738 if (bp->b_pages)
739 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Nathan Scottce8e9222006-01-11 15:39:08 +1100741 bp->b_pages = NULL;
742 bp->b_addr = mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Christoph Hellwig36fae172009-07-18 18:14:58 -0400744 rval = _xfs_buf_get_pages(bp, page_count, XBF_DONT_BLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (rval)
746 return rval;
747
Nathan Scottce8e9222006-01-11 15:39:08 +1100748 bp->b_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100750 for (i = 0; i < bp->b_page_count; i++) {
751 bp->b_pages[i] = mem_to_page((void *)pageaddr);
752 pageaddr += PAGE_CACHE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100755 bp->b_count_desired = len;
756 bp->b_buffer_length = buflen;
Nathan Scottce8e9222006-01-11 15:39:08 +1100757 bp->b_flags |= XBF_MAPPED;
Christoph Hellwig6ab455e2008-05-19 16:34:42 +1000758 bp->b_flags &= ~_XBF_PAGE_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 return 0;
761}
762
763xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100764xfs_buf_get_noaddr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 size_t len,
766 xfs_buftarg_t *target)
767{
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000768 unsigned long page_count = PAGE_ALIGN(len) >> PAGE_SHIFT;
769 int error, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Nathan Scottce8e9222006-01-11 15:39:08 +1100772 bp = xfs_buf_allocate(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (unlikely(bp == NULL))
774 goto fail;
Nathan Scottce8e9222006-01-11 15:39:08 +1100775 _xfs_buf_initialize(bp, target, 0, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000777 error = _xfs_buf_get_pages(bp, page_count, 0);
778 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 goto fail_free_buf;
780
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000781 for (i = 0; i < page_count; i++) {
782 bp->b_pages[i] = alloc_page(GFP_KERNEL);
783 if (!bp->b_pages[i])
784 goto fail_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000786 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000788 error = _xfs_buf_map_pages(bp, XBF_MAPPED);
789 if (unlikely(error)) {
790 printk(KERN_WARNING "%s: failed to map pages\n",
Harvey Harrison34a622b2008-04-10 12:19:21 +1000791 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 goto fail_free_mem;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Nathan Scottce8e9222006-01-11 15:39:08 +1100795 xfs_buf_unlock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000797 trace_xfs_buf_get_noaddr(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return bp;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 fail_free_mem:
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000801 while (--i >= 0)
802 __free_page(bp->b_pages[i]);
Christoph Hellwigca165b82007-05-24 15:21:11 +1000803 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 fail_free_buf:
Christoph Hellwigca165b82007-05-24 15:21:11 +1000805 xfs_buf_deallocate(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 fail:
807 return NULL;
808}
809
810/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 * Increment reference count on buffer, to hold the buffer concurrently
812 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 * Must hold the buffer already to call this function.
814 */
815void
Nathan Scottce8e9222006-01-11 15:39:08 +1100816xfs_buf_hold(
817 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000819 trace_xfs_buf_hold(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100820 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
822
823/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100824 * Releases a hold on the specified buffer. If the
825 * the hold count is 1, calls xfs_buf_free.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 */
827void
Nathan Scottce8e9222006-01-11 15:39:08 +1100828xfs_buf_rele(
829 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
Nathan Scottce8e9222006-01-11 15:39:08 +1100831 xfs_bufhash_t *hash = bp->b_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000833 trace_xfs_buf_rele(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Nathan Scottfad3aa12006-02-01 12:14:52 +1100835 if (unlikely(!hash)) {
836 ASSERT(!bp->b_relse);
837 if (atomic_dec_and_test(&bp->b_hold))
838 xfs_buf_free(bp);
839 return;
840 }
841
Lachlan McIlroy37906892008-08-13 15:42:10 +1000842 ASSERT(atomic_read(&bp->b_hold) > 0);
Nathan Scottce8e9222006-01-11 15:39:08 +1100843 if (atomic_dec_and_lock(&bp->b_hold, &hash->bh_lock)) {
844 if (bp->b_relse) {
845 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 spin_unlock(&hash->bh_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +1100847 (*(bp->b_relse)) (bp);
848 } else if (bp->b_flags & XBF_FS_MANAGED) {
Christoph Hellwig7f14d0a2005-11-02 15:09:35 +1100849 spin_unlock(&hash->bh_lock);
850 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100851 ASSERT(!(bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)));
852 list_del_init(&bp->b_hash_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 spin_unlock(&hash->bh_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +1100854 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
856 }
857}
858
859
860/*
861 * Mutual exclusion on buffers. Locking model:
862 *
863 * Buffers associated with inodes for which buffer locking
864 * is not enabled are not protected by semaphores, and are
865 * assumed to be exclusively owned by the caller. There is a
866 * spinlock in the buffer, used by the caller when concurrent
867 * access is possible.
868 */
869
870/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100871 * Locks a buffer object, if it is not already locked.
872 * Note that this in no way locks the underlying pages, so it is only
873 * useful for synchronizing concurrent use of buffer objects, not for
874 * synchronizing independent access to the underlying pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 */
876int
Nathan Scottce8e9222006-01-11 15:39:08 +1100877xfs_buf_cond_lock(
878 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
880 int locked;
881
Nathan Scottce8e9222006-01-11 15:39:08 +1100882 locked = down_trylock(&bp->b_sema) == 0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000883 if (locked)
Nathan Scottce8e9222006-01-11 15:39:08 +1100884 XB_SET_OWNER(bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000885
886 trace_xfs_buf_cond_lock(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100887 return locked ? 0 : -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890int
Nathan Scottce8e9222006-01-11 15:39:08 +1100891xfs_buf_lock_value(
892 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Stephen Rothwelladaa6932008-04-22 15:26:13 +1000894 return bp->b_sema.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100898 * Locks a buffer object.
899 * Note that this in no way locks the underlying pages, so it is only
900 * useful for synchronizing concurrent use of buffer objects, not for
901 * synchronizing independent access to the underlying pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100903void
904xfs_buf_lock(
905 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000907 trace_xfs_buf_lock(bp, _RET_IP_);
908
Nathan Scottce8e9222006-01-11 15:39:08 +1100909 if (atomic_read(&bp->b_io_remaining))
910 blk_run_address_space(bp->b_target->bt_mapping);
911 down(&bp->b_sema);
912 XB_SET_OWNER(bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000913
914 trace_xfs_buf_lock_done(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
917/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100918 * Releases the lock on the buffer object.
David Chinner2f926582005-09-05 08:33:35 +1000919 * If the buffer is marked delwri but is not queued, do so before we
Nathan Scottce8e9222006-01-11 15:39:08 +1100920 * unlock the buffer as we need to set flags correctly. We also need to
David Chinner2f926582005-09-05 08:33:35 +1000921 * take a reference for the delwri queue because the unlocker is going to
922 * drop their's and they don't know we just queued it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 */
924void
Nathan Scottce8e9222006-01-11 15:39:08 +1100925xfs_buf_unlock(
926 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Nathan Scottce8e9222006-01-11 15:39:08 +1100928 if ((bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)) == XBF_DELWRI) {
929 atomic_inc(&bp->b_hold);
930 bp->b_flags |= XBF_ASYNC;
931 xfs_buf_delwri_queue(bp, 0);
David Chinner2f926582005-09-05 08:33:35 +1000932 }
933
Nathan Scottce8e9222006-01-11 15:39:08 +1100934 XB_CLEAR_OWNER(bp);
935 up(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000936
937 trace_xfs_buf_unlock(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
940
941/*
942 * Pinning Buffer Storage in Memory
Nathan Scottce8e9222006-01-11 15:39:08 +1100943 * Ensure that no attempt to force a buffer to disk will succeed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 */
945void
Nathan Scottce8e9222006-01-11 15:39:08 +1100946xfs_buf_pin(
947 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000949 trace_xfs_buf_pin(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100950 atomic_inc(&bp->b_pin_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953void
Nathan Scottce8e9222006-01-11 15:39:08 +1100954xfs_buf_unpin(
955 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000957 trace_xfs_buf_unpin(bp, _RET_IP_);
958
Nathan Scottce8e9222006-01-11 15:39:08 +1100959 if (atomic_dec_and_test(&bp->b_pin_count))
960 wake_up_all(&bp->b_waiters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
963int
Nathan Scottce8e9222006-01-11 15:39:08 +1100964xfs_buf_ispin(
965 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Nathan Scottce8e9222006-01-11 15:39:08 +1100967 return atomic_read(&bp->b_pin_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
969
Nathan Scottce8e9222006-01-11 15:39:08 +1100970STATIC void
971xfs_buf_wait_unpin(
972 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
974 DECLARE_WAITQUEUE (wait, current);
975
Nathan Scottce8e9222006-01-11 15:39:08 +1100976 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return;
978
Nathan Scottce8e9222006-01-11 15:39:08 +1100979 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 for (;;) {
981 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +1100982 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 break;
Nathan Scottce8e9222006-01-11 15:39:08 +1100984 if (atomic_read(&bp->b_io_remaining))
985 blk_run_address_space(bp->b_target->bt_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 schedule();
987 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100988 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 set_current_state(TASK_RUNNING);
990}
991
992/*
993 * Buffer Utility Routines
994 */
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100997xfs_buf_iodone_work(
David Howellsc4028952006-11-22 14:57:56 +0000998 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
David Howellsc4028952006-11-22 14:57:56 +00001000 xfs_buf_t *bp =
1001 container_of(work, xfs_buf_t, b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
David Chinner0bfefc42007-05-14 18:24:23 +10001003 /*
1004 * We can get an EOPNOTSUPP to ordered writes. Here we clear the
1005 * ordered flag and reissue them. Because we can't tell the higher
1006 * layers directly that they should not issue ordered I/O anymore, they
Christoph Hellwig73f6aa42008-10-10 17:28:29 +11001007 * need to check if the _XFS_BARRIER_FAILED flag was set during I/O completion.
David Chinner0bfefc42007-05-14 18:24:23 +10001008 */
1009 if ((bp->b_error == EOPNOTSUPP) &&
1010 (bp->b_flags & (XBF_ORDERED|XBF_ASYNC)) == (XBF_ORDERED|XBF_ASYNC)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001011 trace_xfs_buf_ordered_retry(bp, _RET_IP_);
David Chinner0bfefc42007-05-14 18:24:23 +10001012 bp->b_flags &= ~XBF_ORDERED;
Christoph Hellwig73f6aa42008-10-10 17:28:29 +11001013 bp->b_flags |= _XFS_BARRIER_FAILED;
David Chinner0bfefc42007-05-14 18:24:23 +10001014 xfs_buf_iorequest(bp);
1015 } else if (bp->b_iodone)
Nathan Scottce8e9222006-01-11 15:39:08 +11001016 (*(bp->b_iodone))(bp);
1017 else if (bp->b_flags & XBF_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 xfs_buf_relse(bp);
1019}
1020
1021void
Nathan Scottce8e9222006-01-11 15:39:08 +11001022xfs_buf_ioend(
1023 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 int schedule)
1025{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001026 trace_xfs_buf_iodone(bp, _RET_IP_);
1027
Lachlan McIlroy77be55a2007-11-23 16:31:00 +11001028 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
Nathan Scottce8e9222006-01-11 15:39:08 +11001029 if (bp->b_error == 0)
1030 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Nathan Scottce8e9222006-01-11 15:39:08 +11001032 if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (schedule) {
David Howellsc4028952006-11-22 14:57:56 +00001034 INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
Nathan Scottce8e9222006-01-11 15:39:08 +11001035 queue_work(xfslogd_workqueue, &bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 } else {
David Howellsc4028952006-11-22 14:57:56 +00001037 xfs_buf_iodone_work(&bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 }
1039 } else {
David Chinnerb4dd3302008-08-13 16:36:11 +10001040 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
1042}
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044void
Nathan Scottce8e9222006-01-11 15:39:08 +11001045xfs_buf_ioerror(
1046 xfs_buf_t *bp,
1047 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
1049 ASSERT(error >= 0 && error <= 0xffff);
Nathan Scottce8e9222006-01-11 15:39:08 +11001050 bp->b_error = (unsigned short)error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001051 trace_xfs_buf_ioerror(bp, error, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054int
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001055xfs_bwrite(
1056 struct xfs_mount *mp,
1057 struct xfs_buf *bp)
1058{
1059 int iowait = (bp->b_flags & XBF_ASYNC) == 0;
1060 int error = 0;
1061
1062 bp->b_strat = xfs_bdstrat_cb;
1063 bp->b_mount = mp;
1064 bp->b_flags |= XBF_WRITE;
1065 if (!iowait)
1066 bp->b_flags |= _XBF_RUN_QUEUES;
1067
1068 xfs_buf_delwri_dequeue(bp);
1069 xfs_buf_iostrategy(bp);
1070
1071 if (iowait) {
1072 error = xfs_buf_iowait(bp);
1073 if (error)
1074 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1075 xfs_buf_relse(bp);
1076 }
1077
1078 return error;
1079}
1080
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001081void
1082xfs_bdwrite(
1083 void *mp,
1084 struct xfs_buf *bp)
1085{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001086 trace_xfs_buf_bdwrite(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001088 bp->b_strat = xfs_bdstrat_cb;
Christoph Hellwig15ac08a2008-12-09 04:47:30 -05001089 bp->b_mount = mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001091 bp->b_flags &= ~XBF_READ;
1092 bp->b_flags |= (XBF_DELWRI | XBF_ASYNC);
1093
1094 xfs_buf_delwri_queue(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095}
1096
Christoph Hellwig4e234712010-01-13 22:17:56 +00001097/*
1098 * Called when we want to stop a buffer from getting written or read.
1099 * We attach the EIO error, muck with its flags, and call biodone
1100 * so that the proper iodone callbacks get called.
1101 */
1102STATIC int
1103xfs_bioerror(
1104 xfs_buf_t *bp)
1105{
1106#ifdef XFSERRORDEBUG
1107 ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
1108#endif
1109
1110 /*
1111 * No need to wait until the buffer is unpinned, we aren't flushing it.
1112 */
1113 XFS_BUF_ERROR(bp, EIO);
1114
1115 /*
1116 * We're calling biodone, so delete XBF_DONE flag.
1117 */
1118 XFS_BUF_UNREAD(bp);
1119 XFS_BUF_UNDELAYWRITE(bp);
1120 XFS_BUF_UNDONE(bp);
1121 XFS_BUF_STALE(bp);
1122
1123 XFS_BUF_CLR_BDSTRAT_FUNC(bp);
1124 xfs_biodone(bp);
1125
1126 return EIO;
1127}
1128
1129/*
1130 * Same as xfs_bioerror, except that we are releasing the buffer
1131 * here ourselves, and avoiding the biodone call.
1132 * This is meant for userdata errors; metadata bufs come with
1133 * iodone functions attached, so that we can track down errors.
1134 */
1135STATIC int
1136xfs_bioerror_relse(
1137 struct xfs_buf *bp)
1138{
1139 int64_t fl = XFS_BUF_BFLAGS(bp);
1140 /*
1141 * No need to wait until the buffer is unpinned.
1142 * We aren't flushing it.
1143 *
1144 * chunkhold expects B_DONE to be set, whether
1145 * we actually finish the I/O or not. We don't want to
1146 * change that interface.
1147 */
1148 XFS_BUF_UNREAD(bp);
1149 XFS_BUF_UNDELAYWRITE(bp);
1150 XFS_BUF_DONE(bp);
1151 XFS_BUF_STALE(bp);
1152 XFS_BUF_CLR_IODONE_FUNC(bp);
1153 XFS_BUF_CLR_BDSTRAT_FUNC(bp);
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001154 if (!(fl & XBF_ASYNC)) {
Christoph Hellwig4e234712010-01-13 22:17:56 +00001155 /*
1156 * Mark b_error and B_ERROR _both_.
1157 * Lot's of chunkcache code assumes that.
1158 * There's no reason to mark error for
1159 * ASYNC buffers.
1160 */
1161 XFS_BUF_ERROR(bp, EIO);
1162 XFS_BUF_FINISH_IOWAIT(bp);
1163 } else {
1164 xfs_buf_relse(bp);
1165 }
1166
1167 return EIO;
1168}
1169
1170
1171/*
1172 * All xfs metadata buffers except log state machine buffers
1173 * get this attached as their b_bdstrat callback function.
1174 * This is so that we can catch a buffer
1175 * after prematurely unpinning it to forcibly shutdown the filesystem.
1176 */
1177int
1178xfs_bdstrat_cb(
1179 struct xfs_buf *bp)
1180{
1181 if (XFS_FORCED_SHUTDOWN(bp->b_mount)) {
1182 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1183 /*
1184 * Metadata write that didn't get logged but
1185 * written delayed anyway. These aren't associated
1186 * with a transaction, and can be ignored.
1187 */
1188 if (!bp->b_iodone && !XFS_BUF_ISREAD(bp))
1189 return xfs_bioerror_relse(bp);
1190 else
1191 return xfs_bioerror(bp);
1192 }
1193
1194 xfs_buf_iorequest(bp);
1195 return 0;
1196}
1197
1198/*
1199 * Wrapper around bdstrat so that we can stop data from going to disk in case
1200 * we are shutting down the filesystem. Typically user data goes thru this
1201 * path; one of the exceptions is the superblock.
1202 */
1203void
1204xfsbdstrat(
1205 struct xfs_mount *mp,
1206 struct xfs_buf *bp)
1207{
1208 if (XFS_FORCED_SHUTDOWN(mp)) {
1209 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1210 xfs_bioerror_relse(bp);
1211 return;
1212 }
1213
1214 xfs_buf_iorequest(bp);
1215}
1216
Christoph Hellwigb8f82a42009-11-14 16:17:22 +00001217STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001218_xfs_buf_ioend(
1219 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 int schedule)
1221{
Christoph Hellwig6ab455e2008-05-19 16:34:42 +10001222 if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
1223 bp->b_flags &= ~_XBF_PAGE_LOCKED;
Nathan Scottce8e9222006-01-11 15:39:08 +11001224 xfs_buf_ioend(bp, schedule);
Christoph Hellwig6ab455e2008-05-19 16:34:42 +10001225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226}
1227
Al Viro782e3b32007-10-12 07:17:47 +01001228STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001229xfs_buf_bio_end_io(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 struct bio *bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 int error)
1232{
Nathan Scottce8e9222006-01-11 15:39:08 +11001233 xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
1234 unsigned int blocksize = bp->b_target->bt_bsize;
Nathan Scotteedb5532005-09-02 16:39:56 +10001235 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Lachlan McIlroycfbe5262008-12-12 15:27:25 +11001237 xfs_buf_ioerror(bp, -error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Nathan Scotteedb5532005-09-02 16:39:56 +10001239 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 struct page *page = bvec->bv_page;
1241
Nathan Scott948ecdb2006-09-28 11:03:13 +10001242 ASSERT(!PagePrivate(page));
Nathan Scottce8e9222006-01-11 15:39:08 +11001243 if (unlikely(bp->b_error)) {
1244 if (bp->b_flags & XBF_READ)
Nathan Scotteedb5532005-09-02 16:39:56 +10001245 ClearPageUptodate(page);
Nathan Scottce8e9222006-01-11 15:39:08 +11001246 } else if (blocksize >= PAGE_CACHE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 SetPageUptodate(page);
1248 } else if (!PagePrivate(page) &&
Nathan Scottce8e9222006-01-11 15:39:08 +11001249 (bp->b_flags & _XBF_PAGE_CACHE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 set_page_region(page, bvec->bv_offset, bvec->bv_len);
1251 }
1252
Nathan Scotteedb5532005-09-02 16:39:56 +10001253 if (--bvec >= bio->bi_io_vec)
1254 prefetchw(&bvec->bv_page->flags);
Christoph Hellwig6ab455e2008-05-19 16:34:42 +10001255
1256 if (bp->b_flags & _XBF_PAGE_LOCKED)
1257 unlock_page(page);
Nathan Scotteedb5532005-09-02 16:39:56 +10001258 } while (bvec >= bio->bi_io_vec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Nathan Scottce8e9222006-01-11 15:39:08 +11001260 _xfs_buf_ioend(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262}
1263
1264STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001265_xfs_buf_ioapply(
1266 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
Christoph Hellwiga9759f22007-12-07 14:07:08 +11001268 int rw, map_i, total_nr_pages, nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 struct bio *bio;
Nathan Scottce8e9222006-01-11 15:39:08 +11001270 int offset = bp->b_offset;
1271 int size = bp->b_count_desired;
1272 sector_t sector = bp->b_bn;
1273 unsigned int blocksize = bp->b_target->bt_bsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Nathan Scottce8e9222006-01-11 15:39:08 +11001275 total_nr_pages = bp->b_page_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 map_i = 0;
1277
Nathan Scottce8e9222006-01-11 15:39:08 +11001278 if (bp->b_flags & XBF_ORDERED) {
1279 ASSERT(!(bp->b_flags & XBF_READ));
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001280 rw = WRITE_BARRIER;
Dave Chinner2ee1aba2009-11-24 18:03:15 +00001281 } else if (bp->b_flags & XBF_LOG_BUFFER) {
Nathan Scott51bdd702006-09-28 11:01:57 +10001282 ASSERT(!(bp->b_flags & XBF_READ_AHEAD));
1283 bp->b_flags &= ~_XBF_RUN_QUEUES;
1284 rw = (bp->b_flags & XBF_WRITE) ? WRITE_SYNC : READ_SYNC;
Dave Chinner2ee1aba2009-11-24 18:03:15 +00001285 } else if (bp->b_flags & _XBF_RUN_QUEUES) {
1286 ASSERT(!(bp->b_flags & XBF_READ_AHEAD));
1287 bp->b_flags &= ~_XBF_RUN_QUEUES;
1288 rw = (bp->b_flags & XBF_WRITE) ? WRITE_META : READ_META;
Nathan Scott51bdd702006-09-28 11:01:57 +10001289 } else {
1290 rw = (bp->b_flags & XBF_WRITE) ? WRITE :
1291 (bp->b_flags & XBF_READ_AHEAD) ? READA : READ;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001292 }
1293
Nathan Scottce8e9222006-01-11 15:39:08 +11001294 /* Special code path for reading a sub page size buffer in --
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 * we populate up the whole page, and hence the other metadata
1296 * in the same page. This optimization is only valid when the
Nathan Scottce8e9222006-01-11 15:39:08 +11001297 * filesystem block size is not smaller than the page size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001299 if ((bp->b_buffer_length < PAGE_CACHE_SIZE) &&
Christoph Hellwig6ab455e2008-05-19 16:34:42 +10001300 ((bp->b_flags & (XBF_READ|_XBF_PAGE_LOCKED)) ==
1301 (XBF_READ|_XBF_PAGE_LOCKED)) &&
Nathan Scottce8e9222006-01-11 15:39:08 +11001302 (blocksize >= PAGE_CACHE_SIZE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 bio = bio_alloc(GFP_NOIO, 1);
1304
Nathan Scottce8e9222006-01-11 15:39:08 +11001305 bio->bi_bdev = bp->b_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 bio->bi_sector = sector - (offset >> BBSHIFT);
Nathan Scottce8e9222006-01-11 15:39:08 +11001307 bio->bi_end_io = xfs_buf_bio_end_io;
1308 bio->bi_private = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Nathan Scottce8e9222006-01-11 15:39:08 +11001310 bio_add_page(bio, bp->b_pages[0], PAGE_CACHE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 size = 0;
1312
Nathan Scottce8e9222006-01-11 15:39:08 +11001313 atomic_inc(&bp->b_io_remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 goto submit_io;
1316 }
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001319 atomic_inc(&bp->b_io_remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
1321 if (nr_pages > total_nr_pages)
1322 nr_pages = total_nr_pages;
1323
1324 bio = bio_alloc(GFP_NOIO, nr_pages);
Nathan Scottce8e9222006-01-11 15:39:08 +11001325 bio->bi_bdev = bp->b_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 bio->bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001327 bio->bi_end_io = xfs_buf_bio_end_io;
1328 bio->bi_private = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 for (; size && nr_pages; nr_pages--, map_i++) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001331 int rbytes, nbytes = PAGE_CACHE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 if (nbytes > size)
1334 nbytes = size;
1335
Nathan Scottce8e9222006-01-11 15:39:08 +11001336 rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset);
1337 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 break;
1339
1340 offset = 0;
1341 sector += nbytes >> BBSHIFT;
1342 size -= nbytes;
1343 total_nr_pages--;
1344 }
1345
1346submit_io:
1347 if (likely(bio->bi_size)) {
1348 submit_bio(rw, bio);
1349 if (size)
1350 goto next_chunk;
1351 } else {
1352 bio_put(bio);
Nathan Scottce8e9222006-01-11 15:39:08 +11001353 xfs_buf_ioerror(bp, EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
1355}
1356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357int
Nathan Scottce8e9222006-01-11 15:39:08 +11001358xfs_buf_iorequest(
1359 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001361 trace_xfs_buf_iorequest(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Nathan Scottce8e9222006-01-11 15:39:08 +11001363 if (bp->b_flags & XBF_DELWRI) {
1364 xfs_buf_delwri_queue(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return 0;
1366 }
1367
Nathan Scottce8e9222006-01-11 15:39:08 +11001368 if (bp->b_flags & XBF_WRITE) {
1369 xfs_buf_wait_unpin(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 }
1371
Nathan Scottce8e9222006-01-11 15:39:08 +11001372 xfs_buf_hold(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 /* Set the count to 1 initially, this will stop an I/O
1375 * completion callout which happens before we have started
Nathan Scottce8e9222006-01-11 15:39:08 +11001376 * all the I/O from calling xfs_buf_ioend too early.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001378 atomic_set(&bp->b_io_remaining, 1);
1379 _xfs_buf_ioapply(bp);
1380 _xfs_buf_ioend(bp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Nathan Scottce8e9222006-01-11 15:39:08 +11001382 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 return 0;
1384}
1385
1386/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001387 * Waits for I/O to complete on the buffer supplied.
1388 * It returns immediately if no I/O is pending.
1389 * It returns the I/O error code, if any, or 0 if there was no error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 */
1391int
Nathan Scottce8e9222006-01-11 15:39:08 +11001392xfs_buf_iowait(
1393 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001395 trace_xfs_buf_iowait(bp, _RET_IP_);
1396
Nathan Scottce8e9222006-01-11 15:39:08 +11001397 if (atomic_read(&bp->b_io_remaining))
1398 blk_run_address_space(bp->b_target->bt_mapping);
David Chinnerb4dd3302008-08-13 16:36:11 +10001399 wait_for_completion(&bp->b_iowait);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001400
1401 trace_xfs_buf_iowait_done(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +11001402 return bp->b_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403}
1404
Nathan Scottce8e9222006-01-11 15:39:08 +11001405xfs_caddr_t
1406xfs_buf_offset(
1407 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 size_t offset)
1409{
1410 struct page *page;
1411
Nathan Scottce8e9222006-01-11 15:39:08 +11001412 if (bp->b_flags & XBF_MAPPED)
1413 return XFS_BUF_PTR(bp) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Nathan Scottce8e9222006-01-11 15:39:08 +11001415 offset += bp->b_offset;
1416 page = bp->b_pages[offset >> PAGE_CACHE_SHIFT];
1417 return (xfs_caddr_t)page_address(page) + (offset & (PAGE_CACHE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418}
1419
1420/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 * Move data into or out of a buffer.
1422 */
1423void
Nathan Scottce8e9222006-01-11 15:39:08 +11001424xfs_buf_iomove(
1425 xfs_buf_t *bp, /* buffer to process */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 size_t boff, /* starting buffer offset */
1427 size_t bsize, /* length to copy */
Dave Chinnerb9c48642010-01-20 10:47:39 +11001428 void *data, /* data address */
Nathan Scottce8e9222006-01-11 15:39:08 +11001429 xfs_buf_rw_t mode) /* read/write/zero flag */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
1431 size_t bend, cpoff, csize;
1432 struct page *page;
1433
1434 bend = boff + bsize;
1435 while (boff < bend) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001436 page = bp->b_pages[xfs_buf_btoct(boff + bp->b_offset)];
1437 cpoff = xfs_buf_poff(boff + bp->b_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 csize = min_t(size_t,
Nathan Scottce8e9222006-01-11 15:39:08 +11001439 PAGE_CACHE_SIZE-cpoff, bp->b_count_desired-boff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441 ASSERT(((csize + cpoff) <= PAGE_CACHE_SIZE));
1442
1443 switch (mode) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001444 case XBRW_ZERO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 memset(page_address(page) + cpoff, 0, csize);
1446 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001447 case XBRW_READ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 memcpy(data, page_address(page) + cpoff, csize);
1449 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001450 case XBRW_WRITE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 memcpy(page_address(page) + cpoff, data, csize);
1452 }
1453
1454 boff += csize;
1455 data += csize;
1456 }
1457}
1458
1459/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001460 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 */
1462
1463/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001464 * Wait for any bufs with callbacks that have been submitted but
1465 * have not yet returned... walk the hash list for the target.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 */
1467void
1468xfs_wait_buftarg(
1469 xfs_buftarg_t *btp)
1470{
1471 xfs_buf_t *bp, *n;
1472 xfs_bufhash_t *hash;
1473 uint i;
1474
1475 for (i = 0; i < (1 << btp->bt_hashshift); i++) {
1476 hash = &btp->bt_hash[i];
1477again:
1478 spin_lock(&hash->bh_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +11001479 list_for_each_entry_safe(bp, n, &hash->bh_list, b_hash_list) {
1480 ASSERT(btp == bp->b_target);
1481 if (!(bp->b_flags & XBF_FS_MANAGED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 spin_unlock(&hash->bh_lock);
David Chinner2f926582005-09-05 08:33:35 +10001483 /*
1484 * Catch superblock reference count leaks
1485 * immediately
1486 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001487 BUG_ON(bp->b_bn == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 delay(100);
1489 goto again;
1490 }
1491 }
1492 spin_unlock(&hash->bh_lock);
1493 }
1494}
1495
1496/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001497 * Allocate buffer hash table for a given target.
1498 * For devices containing metadata (i.e. not the log/realtime devices)
1499 * we need to allocate a much larger hash table.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 */
1501STATIC void
1502xfs_alloc_bufhash(
1503 xfs_buftarg_t *btp,
1504 int external)
1505{
1506 unsigned int i;
1507
1508 btp->bt_hashshift = external ? 3 : 8; /* 8 or 256 buckets */
1509 btp->bt_hashmask = (1 << btp->bt_hashshift) - 1;
Christoph Hellwigbdfb0432010-01-20 21:55:30 +00001510 btp->bt_hash = kmem_zalloc_large((1 << btp->bt_hashshift) *
1511 sizeof(xfs_bufhash_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 for (i = 0; i < (1 << btp->bt_hashshift); i++) {
1513 spin_lock_init(&btp->bt_hash[i].bh_lock);
1514 INIT_LIST_HEAD(&btp->bt_hash[i].bh_list);
1515 }
1516}
1517
1518STATIC void
1519xfs_free_bufhash(
1520 xfs_buftarg_t *btp)
1521{
Christoph Hellwigbdfb0432010-01-20 21:55:30 +00001522 kmem_free_large(btp->bt_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 btp->bt_hash = NULL;
1524}
1525
David Chinnera6867a62006-01-11 15:37:58 +11001526/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001527 * buftarg list for delwrite queue processing
David Chinnera6867a62006-01-11 15:37:58 +11001528 */
Tim Shimmine6a0e9c2007-05-08 13:49:59 +10001529static LIST_HEAD(xfs_buftarg_list);
David Chinner7989cb82007-02-10 18:34:56 +11001530static DEFINE_SPINLOCK(xfs_buftarg_lock);
David Chinnera6867a62006-01-11 15:37:58 +11001531
1532STATIC void
1533xfs_register_buftarg(
1534 xfs_buftarg_t *btp)
1535{
1536 spin_lock(&xfs_buftarg_lock);
1537 list_add(&btp->bt_list, &xfs_buftarg_list);
1538 spin_unlock(&xfs_buftarg_lock);
1539}
1540
1541STATIC void
1542xfs_unregister_buftarg(
1543 xfs_buftarg_t *btp)
1544{
1545 spin_lock(&xfs_buftarg_lock);
1546 list_del(&btp->bt_list);
1547 spin_unlock(&xfs_buftarg_lock);
1548}
1549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550void
1551xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001552 struct xfs_mount *mp,
1553 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
1555 xfs_flush_buftarg(btp, 1);
Christoph Hellwigb7963132009-03-03 14:48:37 -05001556 if (mp->m_flags & XFS_MOUNT_BARRIER)
1557 xfs_blkdev_issue_flush(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 xfs_free_bufhash(btp);
Nathan Scottce8e9222006-01-11 15:39:08 +11001559 iput(btp->bt_mapping->host);
David Chinnera6867a62006-01-11 15:37:58 +11001560
Nathan Scottce8e9222006-01-11 15:39:08 +11001561 /* Unregister the buftarg first so that we don't get a
1562 * wakeup finding a non-existent task
1563 */
David Chinnera6867a62006-01-11 15:37:58 +11001564 xfs_unregister_buftarg(btp);
1565 kthread_stop(btp->bt_task);
1566
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001567 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568}
1569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570STATIC int
1571xfs_setsize_buftarg_flags(
1572 xfs_buftarg_t *btp,
1573 unsigned int blocksize,
1574 unsigned int sectorsize,
1575 int verbose)
1576{
Nathan Scottce8e9222006-01-11 15:39:08 +11001577 btp->bt_bsize = blocksize;
1578 btp->bt_sshift = ffs(sectorsize) - 1;
1579 btp->bt_smask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Nathan Scottce8e9222006-01-11 15:39:08 +11001581 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 printk(KERN_WARNING
1583 "XFS: Cannot set_blocksize to %u on device %s\n",
1584 sectorsize, XFS_BUFTARG_NAME(btp));
1585 return EINVAL;
1586 }
1587
1588 if (verbose &&
1589 (PAGE_CACHE_SIZE / BITS_PER_LONG) > sectorsize) {
1590 printk(KERN_WARNING
1591 "XFS: %u byte sectors in use on device %s. "
1592 "This is suboptimal; %u or greater is ideal.\n",
1593 sectorsize, XFS_BUFTARG_NAME(btp),
1594 (unsigned int)PAGE_CACHE_SIZE / BITS_PER_LONG);
1595 }
1596
1597 return 0;
1598}
1599
1600/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001601 * When allocating the initial buffer target we have not yet
1602 * read in the superblock, so don't know what sized sectors
1603 * are being used is at this early stage. Play safe.
1604 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605STATIC int
1606xfs_setsize_buftarg_early(
1607 xfs_buftarg_t *btp,
1608 struct block_device *bdev)
1609{
1610 return xfs_setsize_buftarg_flags(btp,
Martin K. Petersene1defc42009-05-22 17:17:49 -04001611 PAGE_CACHE_SIZE, bdev_logical_block_size(bdev), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612}
1613
1614int
1615xfs_setsize_buftarg(
1616 xfs_buftarg_t *btp,
1617 unsigned int blocksize,
1618 unsigned int sectorsize)
1619{
1620 return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
1621}
1622
1623STATIC int
1624xfs_mapping_buftarg(
1625 xfs_buftarg_t *btp,
1626 struct block_device *bdev)
1627{
1628 struct backing_dev_info *bdi;
1629 struct inode *inode;
1630 struct address_space *mapping;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07001631 static const struct address_space_operations mapping_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 .sync_page = block_sync_page,
Christoph Lametere965f962006-02-01 03:05:41 -08001633 .migratepage = fail_migrate_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 };
1635
1636 inode = new_inode(bdev->bd_inode->i_sb);
1637 if (!inode) {
1638 printk(KERN_WARNING
1639 "XFS: Cannot allocate mapping inode for device %s\n",
1640 XFS_BUFTARG_NAME(btp));
1641 return ENOMEM;
1642 }
1643 inode->i_mode = S_IFBLK;
1644 inode->i_bdev = bdev;
1645 inode->i_rdev = bdev->bd_dev;
1646 bdi = blk_get_backing_dev_info(bdev);
1647 if (!bdi)
1648 bdi = &default_backing_dev_info;
1649 mapping = &inode->i_data;
1650 mapping->a_ops = &mapping_aops;
1651 mapping->backing_dev_info = bdi;
1652 mapping_set_gfp_mask(mapping, GFP_NOFS);
Nathan Scottce8e9222006-01-11 15:39:08 +11001653 btp->bt_mapping = mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 return 0;
1655}
1656
David Chinnera6867a62006-01-11 15:37:58 +11001657STATIC int
1658xfs_alloc_delwrite_queue(
1659 xfs_buftarg_t *btp)
1660{
1661 int error = 0;
1662
1663 INIT_LIST_HEAD(&btp->bt_list);
1664 INIT_LIST_HEAD(&btp->bt_delwrite_queue);
Eric Sandeen007c61c2007-10-11 17:43:56 +10001665 spin_lock_init(&btp->bt_delwrite_lock);
David Chinnera6867a62006-01-11 15:37:58 +11001666 btp->bt_flags = 0;
1667 btp->bt_task = kthread_run(xfsbufd, btp, "xfsbufd");
1668 if (IS_ERR(btp->bt_task)) {
1669 error = PTR_ERR(btp->bt_task);
1670 goto out_error;
1671 }
1672 xfs_register_buftarg(btp);
1673out_error:
1674 return error;
1675}
1676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677xfs_buftarg_t *
1678xfs_alloc_buftarg(
1679 struct block_device *bdev,
1680 int external)
1681{
1682 xfs_buftarg_t *btp;
1683
1684 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
1685
Nathan Scottce8e9222006-01-11 15:39:08 +11001686 btp->bt_dev = bdev->bd_dev;
1687 btp->bt_bdev = bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 if (xfs_setsize_buftarg_early(btp, bdev))
1689 goto error;
1690 if (xfs_mapping_buftarg(btp, bdev))
1691 goto error;
David Chinnera6867a62006-01-11 15:37:58 +11001692 if (xfs_alloc_delwrite_queue(btp))
1693 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 xfs_alloc_bufhash(btp, external);
1695 return btp;
1696
1697error:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001698 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 return NULL;
1700}
1701
1702
1703/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001704 * Delayed write buffer handling
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001707xfs_buf_delwri_queue(
1708 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 int unlock)
1710{
Nathan Scottce8e9222006-01-11 15:39:08 +11001711 struct list_head *dwq = &bp->b_target->bt_delwrite_queue;
1712 spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
David Chinnera6867a62006-01-11 15:37:58 +11001713
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001714 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
1715
Nathan Scottce8e9222006-01-11 15:39:08 +11001716 ASSERT((bp->b_flags&(XBF_DELWRI|XBF_ASYNC)) == (XBF_DELWRI|XBF_ASYNC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
David Chinnera6867a62006-01-11 15:37:58 +11001718 spin_lock(dwlk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 /* If already in the queue, dequeue and place at tail */
Nathan Scottce8e9222006-01-11 15:39:08 +11001720 if (!list_empty(&bp->b_list)) {
1721 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1722 if (unlock)
1723 atomic_dec(&bp->b_hold);
1724 list_del(&bp->b_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 }
1726
Dave Chinnerc9c12972010-01-11 11:49:59 +00001727 if (list_empty(dwq)) {
1728 /* start xfsbufd as it is about to have something to do */
1729 wake_up_process(bp->b_target->bt_task);
1730 }
1731
Nathan Scottce8e9222006-01-11 15:39:08 +11001732 bp->b_flags |= _XBF_DELWRI_Q;
1733 list_add_tail(&bp->b_list, dwq);
1734 bp->b_queuetime = jiffies;
David Chinnera6867a62006-01-11 15:37:58 +11001735 spin_unlock(dwlk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
1737 if (unlock)
Nathan Scottce8e9222006-01-11 15:39:08 +11001738 xfs_buf_unlock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739}
1740
1741void
Nathan Scottce8e9222006-01-11 15:39:08 +11001742xfs_buf_delwri_dequeue(
1743 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
Nathan Scottce8e9222006-01-11 15:39:08 +11001745 spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 int dequeued = 0;
1747
David Chinnera6867a62006-01-11 15:37:58 +11001748 spin_lock(dwlk);
Nathan Scottce8e9222006-01-11 15:39:08 +11001749 if ((bp->b_flags & XBF_DELWRI) && !list_empty(&bp->b_list)) {
1750 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1751 list_del_init(&bp->b_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 dequeued = 1;
1753 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001754 bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q);
David Chinnera6867a62006-01-11 15:37:58 +11001755 spin_unlock(dwlk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757 if (dequeued)
Nathan Scottce8e9222006-01-11 15:39:08 +11001758 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001760 trace_xfs_buf_delwri_dequeue(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761}
1762
Dave Chinnerd808f612010-02-02 10:13:42 +11001763/*
1764 * If a delwri buffer needs to be pushed before it has aged out, then promote
1765 * it to the head of the delwri queue so that it will be flushed on the next
1766 * xfsbufd run. We do this by resetting the queuetime of the buffer to be older
1767 * than the age currently needed to flush the buffer. Hence the next time the
1768 * xfsbufd sees it is guaranteed to be considered old enough to flush.
1769 */
1770void
1771xfs_buf_delwri_promote(
1772 struct xfs_buf *bp)
1773{
1774 struct xfs_buftarg *btp = bp->b_target;
1775 long age = xfs_buf_age_centisecs * msecs_to_jiffies(10) + 1;
1776
1777 ASSERT(bp->b_flags & XBF_DELWRI);
1778 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1779
1780 /*
1781 * Check the buffer age before locking the delayed write queue as we
1782 * don't need to promote buffers that are already past the flush age.
1783 */
1784 if (bp->b_queuetime < jiffies - age)
1785 return;
1786 bp->b_queuetime = jiffies - age;
1787 spin_lock(&btp->bt_delwrite_lock);
1788 list_move(&bp->b_list, &btp->bt_delwrite_queue);
1789 spin_unlock(&btp->bt_delwrite_lock);
1790}
1791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001793xfs_buf_runall_queues(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 struct workqueue_struct *queue)
1795{
1796 flush_workqueue(queue);
1797}
1798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799STATIC int
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001800xfsbufd_wakeup(
Nathan Scott15c84a42005-11-04 10:51:01 +11001801 int priority,
1802 gfp_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803{
Christoph Hellwigda7f93e2006-01-11 20:49:57 +11001804 xfs_buftarg_t *btp;
David Chinnera6867a62006-01-11 15:37:58 +11001805
1806 spin_lock(&xfs_buftarg_lock);
Christoph Hellwigda7f93e2006-01-11 20:49:57 +11001807 list_for_each_entry(btp, &xfs_buftarg_list, bt_list) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001808 if (test_bit(XBT_FORCE_SLEEP, &btp->bt_flags))
David Chinnera6867a62006-01-11 15:37:58 +11001809 continue;
Dave Chinnerc9c12972010-01-11 11:49:59 +00001810 if (list_empty(&btp->bt_delwrite_queue))
1811 continue;
Nathan Scottce8e9222006-01-11 15:39:08 +11001812 set_bit(XBT_FORCE_FLUSH, &btp->bt_flags);
David Chinnera6867a62006-01-11 15:37:58 +11001813 wake_up_process(btp->bt_task);
1814 }
1815 spin_unlock(&xfs_buftarg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 return 0;
1817}
1818
David Chinner585e6d82007-02-10 18:32:29 +11001819/*
1820 * Move as many buffers as specified to the supplied list
1821 * idicating if we skipped any buffers to prevent deadlocks.
1822 */
1823STATIC int
1824xfs_buf_delwri_split(
1825 xfs_buftarg_t *target,
1826 struct list_head *list,
David Chinner5e6a07d2007-02-10 18:34:49 +11001827 unsigned long age)
David Chinner585e6d82007-02-10 18:32:29 +11001828{
1829 xfs_buf_t *bp, *n;
1830 struct list_head *dwq = &target->bt_delwrite_queue;
1831 spinlock_t *dwlk = &target->bt_delwrite_lock;
1832 int skipped = 0;
David Chinner5e6a07d2007-02-10 18:34:49 +11001833 int force;
David Chinner585e6d82007-02-10 18:32:29 +11001834
David Chinner5e6a07d2007-02-10 18:34:49 +11001835 force = test_and_clear_bit(XBT_FORCE_FLUSH, &target->bt_flags);
David Chinner585e6d82007-02-10 18:32:29 +11001836 INIT_LIST_HEAD(list);
1837 spin_lock(dwlk);
1838 list_for_each_entry_safe(bp, n, dwq, b_list) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001839 trace_xfs_buf_delwri_split(bp, _RET_IP_);
David Chinner585e6d82007-02-10 18:32:29 +11001840 ASSERT(bp->b_flags & XBF_DELWRI);
1841
1842 if (!xfs_buf_ispin(bp) && !xfs_buf_cond_lock(bp)) {
David Chinner5e6a07d2007-02-10 18:34:49 +11001843 if (!force &&
David Chinner585e6d82007-02-10 18:32:29 +11001844 time_before(jiffies, bp->b_queuetime + age)) {
1845 xfs_buf_unlock(bp);
1846 break;
1847 }
1848
1849 bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q|
1850 _XBF_RUN_QUEUES);
1851 bp->b_flags |= XBF_WRITE;
1852 list_move_tail(&bp->b_list, list);
1853 } else
1854 skipped++;
1855 }
1856 spin_unlock(dwlk);
1857
1858 return skipped;
1859
1860}
1861
Dave Chinner089716a2010-01-26 15:13:25 +11001862/*
1863 * Compare function is more complex than it needs to be because
1864 * the return value is only 32 bits and we are doing comparisons
1865 * on 64 bit values
1866 */
1867static int
1868xfs_buf_cmp(
1869 void *priv,
1870 struct list_head *a,
1871 struct list_head *b)
1872{
1873 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
1874 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
1875 xfs_daddr_t diff;
1876
1877 diff = ap->b_bn - bp->b_bn;
1878 if (diff < 0)
1879 return -1;
1880 if (diff > 0)
1881 return 1;
1882 return 0;
1883}
1884
1885void
1886xfs_buf_delwri_sort(
1887 xfs_buftarg_t *target,
1888 struct list_head *list)
1889{
1890 list_sort(NULL, list, xfs_buf_cmp);
1891}
1892
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893STATIC int
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001894xfsbufd(
David Chinner585e6d82007-02-10 18:32:29 +11001895 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896{
Dave Chinner089716a2010-01-26 15:13:25 +11001897 xfs_buftarg_t *target = (xfs_buftarg_t *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 current->flags |= PF_MEMALLOC;
1900
Rafael J. Wysocki978c7b22007-12-07 14:09:02 +11001901 set_freezable();
1902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 do {
Dave Chinnerc9c12972010-01-11 11:49:59 +00001904 long age = xfs_buf_age_centisecs * msecs_to_jiffies(10);
1905 long tout = xfs_buf_timer_centisecs * msecs_to_jiffies(10);
Dave Chinner089716a2010-01-26 15:13:25 +11001906 int count = 0;
1907 struct list_head tmp;
Dave Chinnerc9c12972010-01-11 11:49:59 +00001908
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001909 if (unlikely(freezing(current))) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001910 set_bit(XBT_FORCE_SLEEP, &target->bt_flags);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001911 refrigerator();
Nathan Scottabd0cf72005-05-05 13:30:13 -07001912 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +11001913 clear_bit(XBT_FORCE_SLEEP, &target->bt_flags);
Nathan Scottabd0cf72005-05-05 13:30:13 -07001914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Dave Chinnerc9c12972010-01-11 11:49:59 +00001916 /* sleep for a long time if there is nothing to do. */
1917 if (list_empty(&target->bt_delwrite_queue))
1918 tout = MAX_SCHEDULE_TIMEOUT;
1919 schedule_timeout_interruptible(tout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Dave Chinnerc9c12972010-01-11 11:49:59 +00001921 xfs_buf_delwri_split(target, &tmp, age);
Dave Chinner089716a2010-01-26 15:13:25 +11001922 list_sort(NULL, &tmp, xfs_buf_cmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 while (!list_empty(&tmp)) {
Dave Chinner089716a2010-01-26 15:13:25 +11001924 struct xfs_buf *bp;
1925 bp = list_first_entry(&tmp, struct xfs_buf, b_list);
Nathan Scottce8e9222006-01-11 15:39:08 +11001926 list_del_init(&bp->b_list);
1927 xfs_buf_iostrategy(bp);
David Chinner585e6d82007-02-10 18:32:29 +11001928 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 }
1930
Felix Blyakher3a011a12009-02-18 15:56:51 -06001931 if (as_list_len > 0)
1932 purge_addresses();
Nathan Scottf07c2252006-09-28 10:52:15 +10001933 if (count)
1934 blk_run_address_space(target->bt_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Christoph Hellwig4df08c52005-09-05 08:34:18 +10001936 } while (!kthread_should_stop());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Christoph Hellwig4df08c52005-09-05 08:34:18 +10001938 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939}
1940
1941/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001942 * Go through all incore buffers, and release buffers if they belong to
1943 * the given device. This is used in filesystem error handling to
1944 * preserve the consistency of its metadata.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 */
1946int
1947xfs_flush_buftarg(
David Chinner585e6d82007-02-10 18:32:29 +11001948 xfs_buftarg_t *target,
1949 int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
Dave Chinner089716a2010-01-26 15:13:25 +11001951 xfs_buf_t *bp;
David Chinner585e6d82007-02-10 18:32:29 +11001952 int pincount = 0;
Dave Chinner089716a2010-01-26 15:13:25 +11001953 LIST_HEAD(tmp_list);
1954 LIST_HEAD(wait_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Dave Chinnerc626d172009-04-06 18:42:11 +02001956 xfs_buf_runall_queues(xfsconvertd_workqueue);
Nathan Scottce8e9222006-01-11 15:39:08 +11001957 xfs_buf_runall_queues(xfsdatad_workqueue);
1958 xfs_buf_runall_queues(xfslogd_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
David Chinner5e6a07d2007-02-10 18:34:49 +11001960 set_bit(XBT_FORCE_FLUSH, &target->bt_flags);
Dave Chinner089716a2010-01-26 15:13:25 +11001961 pincount = xfs_buf_delwri_split(target, &tmp_list, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
1963 /*
Dave Chinner089716a2010-01-26 15:13:25 +11001964 * Dropped the delayed write list lock, now walk the temporary list.
1965 * All I/O is issued async and then if we need to wait for completion
1966 * we do that after issuing all the IO.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 */
Dave Chinner089716a2010-01-26 15:13:25 +11001968 list_sort(NULL, &tmp_list, xfs_buf_cmp);
1969 while (!list_empty(&tmp_list)) {
1970 bp = list_first_entry(&tmp_list, struct xfs_buf, b_list);
David Chinner585e6d82007-02-10 18:32:29 +11001971 ASSERT(target == bp->b_target);
Dave Chinner089716a2010-01-26 15:13:25 +11001972 list_del_init(&bp->b_list);
1973 if (wait) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001974 bp->b_flags &= ~XBF_ASYNC;
Dave Chinner089716a2010-01-26 15:13:25 +11001975 list_add(&bp->b_list, &wait_list);
1976 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001977 xfs_buf_iostrategy(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 }
1979
Dave Chinner089716a2010-01-26 15:13:25 +11001980 if (wait) {
1981 /* Expedite and wait for IO to complete. */
Nathan Scottf07c2252006-09-28 10:52:15 +10001982 blk_run_address_space(target->bt_mapping);
Dave Chinner089716a2010-01-26 15:13:25 +11001983 while (!list_empty(&wait_list)) {
1984 bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
Nathan Scottf07c2252006-09-28 10:52:15 +10001985
Dave Chinner089716a2010-01-26 15:13:25 +11001986 list_del_init(&bp->b_list);
1987 xfs_iowait(bp);
1988 xfs_buf_relse(bp);
1989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 }
1991
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 return pincount;
1993}
1994
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001995int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11001996xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
Nathan Scott87582802006-03-14 13:18:19 +11001998 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
1999 KM_ZONE_HWALIGN, NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11002000 if (!xfs_buf_zone)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002001 goto out;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11002002
Rafael J. Wysockib4337692007-03-22 00:11:27 -08002003 xfslogd_workqueue = create_workqueue("xfslogd");
Christoph Hellwig23ea4032005-06-21 15:14:01 +10002004 if (!xfslogd_workqueue)
Christoph Hellwig04d8b282005-11-02 10:15:05 +11002005 goto out_free_buf_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
Rafael J. Wysockib4337692007-03-22 00:11:27 -08002007 xfsdatad_workqueue = create_workqueue("xfsdatad");
Christoph Hellwig23ea4032005-06-21 15:14:01 +10002008 if (!xfsdatad_workqueue)
2009 goto out_destroy_xfslogd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Dave Chinnerc626d172009-04-06 18:42:11 +02002011 xfsconvertd_workqueue = create_workqueue("xfsconvertd");
2012 if (!xfsconvertd_workqueue)
2013 goto out_destroy_xfsdatad_workqueue;
2014
Rusty Russell8e1f9362007-07-17 04:03:17 -07002015 register_shrinker(&xfs_buf_shake);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10002016 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Dave Chinnerc626d172009-04-06 18:42:11 +02002018 out_destroy_xfsdatad_workqueue:
2019 destroy_workqueue(xfsdatad_workqueue);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10002020 out_destroy_xfslogd_workqueue:
2021 destroy_workqueue(xfslogd_workqueue);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10002022 out_free_buf_zone:
Nathan Scottce8e9222006-01-11 15:39:08 +11002023 kmem_zone_destroy(xfs_buf_zone);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002024 out:
Nathan Scott87582802006-03-14 13:18:19 +11002025 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026}
2027
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028void
Nathan Scottce8e9222006-01-11 15:39:08 +11002029xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030{
Rusty Russell8e1f9362007-07-17 04:03:17 -07002031 unregister_shrinker(&xfs_buf_shake);
Dave Chinnerc626d172009-04-06 18:42:11 +02002032 destroy_workqueue(xfsconvertd_workqueue);
Christoph Hellwig04d8b282005-11-02 10:15:05 +11002033 destroy_workqueue(xfsdatad_workqueue);
2034 destroy_workqueue(xfslogd_workqueue);
Nathan Scottce8e9222006-01-11 15:39:08 +11002035 kmem_zone_destroy(xfs_buf_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036}
Tim Shimmine6a0e9c2007-05-08 13:49:59 +10002037
2038#ifdef CONFIG_KDB_MODULES
2039struct list_head *
2040xfs_get_buftarg_list(void)
2041{
2042 return &xfs_buftarg_list;
2043}
2044#endif