blob: 4556a4c31e36f8eded1f1bcf50df1af19450ee3d [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
1081int
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001082xfs_bawrite(
1083 void *mp,
1084 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001086 trace_xfs_buf_bawrite(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001088 ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001090 xfs_buf_delwri_dequeue(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001092 bp->b_flags &= ~(XBF_READ | XBF_DELWRI | XBF_READ_AHEAD);
1093 bp->b_flags |= (XBF_WRITE | XBF_ASYNC | _XBF_RUN_QUEUES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Christoph Hellwig15ac08a2008-12-09 04:47:30 -05001095 bp->b_mount = mp;
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001096 bp->b_strat = xfs_bdstrat_cb;
1097 return xfs_bdstrat_cb(bp);
1098}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001100void
1101xfs_bdwrite(
1102 void *mp,
1103 struct xfs_buf *bp)
1104{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001105 trace_xfs_buf_bdwrite(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001107 bp->b_strat = xfs_bdstrat_cb;
Christoph Hellwig15ac08a2008-12-09 04:47:30 -05001108 bp->b_mount = mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001110 bp->b_flags &= ~XBF_READ;
1111 bp->b_flags |= (XBF_DELWRI | XBF_ASYNC);
1112
1113 xfs_buf_delwri_queue(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114}
1115
Christoph Hellwig4e234712010-01-13 22:17:56 +00001116/*
1117 * Called when we want to stop a buffer from getting written or read.
1118 * We attach the EIO error, muck with its flags, and call biodone
1119 * so that the proper iodone callbacks get called.
1120 */
1121STATIC int
1122xfs_bioerror(
1123 xfs_buf_t *bp)
1124{
1125#ifdef XFSERRORDEBUG
1126 ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
1127#endif
1128
1129 /*
1130 * No need to wait until the buffer is unpinned, we aren't flushing it.
1131 */
1132 XFS_BUF_ERROR(bp, EIO);
1133
1134 /*
1135 * We're calling biodone, so delete XBF_DONE flag.
1136 */
1137 XFS_BUF_UNREAD(bp);
1138 XFS_BUF_UNDELAYWRITE(bp);
1139 XFS_BUF_UNDONE(bp);
1140 XFS_BUF_STALE(bp);
1141
1142 XFS_BUF_CLR_BDSTRAT_FUNC(bp);
1143 xfs_biodone(bp);
1144
1145 return EIO;
1146}
1147
1148/*
1149 * Same as xfs_bioerror, except that we are releasing the buffer
1150 * here ourselves, and avoiding the biodone call.
1151 * This is meant for userdata errors; metadata bufs come with
1152 * iodone functions attached, so that we can track down errors.
1153 */
1154STATIC int
1155xfs_bioerror_relse(
1156 struct xfs_buf *bp)
1157{
1158 int64_t fl = XFS_BUF_BFLAGS(bp);
1159 /*
1160 * No need to wait until the buffer is unpinned.
1161 * We aren't flushing it.
1162 *
1163 * chunkhold expects B_DONE to be set, whether
1164 * we actually finish the I/O or not. We don't want to
1165 * change that interface.
1166 */
1167 XFS_BUF_UNREAD(bp);
1168 XFS_BUF_UNDELAYWRITE(bp);
1169 XFS_BUF_DONE(bp);
1170 XFS_BUF_STALE(bp);
1171 XFS_BUF_CLR_IODONE_FUNC(bp);
1172 XFS_BUF_CLR_BDSTRAT_FUNC(bp);
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001173 if (!(fl & XBF_ASYNC)) {
Christoph Hellwig4e234712010-01-13 22:17:56 +00001174 /*
1175 * Mark b_error and B_ERROR _both_.
1176 * Lot's of chunkcache code assumes that.
1177 * There's no reason to mark error for
1178 * ASYNC buffers.
1179 */
1180 XFS_BUF_ERROR(bp, EIO);
1181 XFS_BUF_FINISH_IOWAIT(bp);
1182 } else {
1183 xfs_buf_relse(bp);
1184 }
1185
1186 return EIO;
1187}
1188
1189
1190/*
1191 * All xfs metadata buffers except log state machine buffers
1192 * get this attached as their b_bdstrat callback function.
1193 * This is so that we can catch a buffer
1194 * after prematurely unpinning it to forcibly shutdown the filesystem.
1195 */
1196int
1197xfs_bdstrat_cb(
1198 struct xfs_buf *bp)
1199{
1200 if (XFS_FORCED_SHUTDOWN(bp->b_mount)) {
1201 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1202 /*
1203 * Metadata write that didn't get logged but
1204 * written delayed anyway. These aren't associated
1205 * with a transaction, and can be ignored.
1206 */
1207 if (!bp->b_iodone && !XFS_BUF_ISREAD(bp))
1208 return xfs_bioerror_relse(bp);
1209 else
1210 return xfs_bioerror(bp);
1211 }
1212
1213 xfs_buf_iorequest(bp);
1214 return 0;
1215}
1216
1217/*
1218 * Wrapper around bdstrat so that we can stop data from going to disk in case
1219 * we are shutting down the filesystem. Typically user data goes thru this
1220 * path; one of the exceptions is the superblock.
1221 */
1222void
1223xfsbdstrat(
1224 struct xfs_mount *mp,
1225 struct xfs_buf *bp)
1226{
1227 if (XFS_FORCED_SHUTDOWN(mp)) {
1228 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1229 xfs_bioerror_relse(bp);
1230 return;
1231 }
1232
1233 xfs_buf_iorequest(bp);
1234}
1235
Christoph Hellwigb8f82a42009-11-14 16:17:22 +00001236STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001237_xfs_buf_ioend(
1238 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 int schedule)
1240{
Christoph Hellwig6ab455e2008-05-19 16:34:42 +10001241 if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
1242 bp->b_flags &= ~_XBF_PAGE_LOCKED;
Nathan Scottce8e9222006-01-11 15:39:08 +11001243 xfs_buf_ioend(bp, schedule);
Christoph Hellwig6ab455e2008-05-19 16:34:42 +10001244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245}
1246
Al Viro782e3b32007-10-12 07:17:47 +01001247STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001248xfs_buf_bio_end_io(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 struct bio *bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 int error)
1251{
Nathan Scottce8e9222006-01-11 15:39:08 +11001252 xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
1253 unsigned int blocksize = bp->b_target->bt_bsize;
Nathan Scotteedb5532005-09-02 16:39:56 +10001254 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Lachlan McIlroycfbe5262008-12-12 15:27:25 +11001256 xfs_buf_ioerror(bp, -error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Nathan Scotteedb5532005-09-02 16:39:56 +10001258 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 struct page *page = bvec->bv_page;
1260
Nathan Scott948ecdb2006-09-28 11:03:13 +10001261 ASSERT(!PagePrivate(page));
Nathan Scottce8e9222006-01-11 15:39:08 +11001262 if (unlikely(bp->b_error)) {
1263 if (bp->b_flags & XBF_READ)
Nathan Scotteedb5532005-09-02 16:39:56 +10001264 ClearPageUptodate(page);
Nathan Scottce8e9222006-01-11 15:39:08 +11001265 } else if (blocksize >= PAGE_CACHE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 SetPageUptodate(page);
1267 } else if (!PagePrivate(page) &&
Nathan Scottce8e9222006-01-11 15:39:08 +11001268 (bp->b_flags & _XBF_PAGE_CACHE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 set_page_region(page, bvec->bv_offset, bvec->bv_len);
1270 }
1271
Nathan Scotteedb5532005-09-02 16:39:56 +10001272 if (--bvec >= bio->bi_io_vec)
1273 prefetchw(&bvec->bv_page->flags);
Christoph Hellwig6ab455e2008-05-19 16:34:42 +10001274
1275 if (bp->b_flags & _XBF_PAGE_LOCKED)
1276 unlock_page(page);
Nathan Scotteedb5532005-09-02 16:39:56 +10001277 } while (bvec >= bio->bi_io_vec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Nathan Scottce8e9222006-01-11 15:39:08 +11001279 _xfs_buf_ioend(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
1282
1283STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001284_xfs_buf_ioapply(
1285 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
Christoph Hellwiga9759f22007-12-07 14:07:08 +11001287 int rw, map_i, total_nr_pages, nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 struct bio *bio;
Nathan Scottce8e9222006-01-11 15:39:08 +11001289 int offset = bp->b_offset;
1290 int size = bp->b_count_desired;
1291 sector_t sector = bp->b_bn;
1292 unsigned int blocksize = bp->b_target->bt_bsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Nathan Scottce8e9222006-01-11 15:39:08 +11001294 total_nr_pages = bp->b_page_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 map_i = 0;
1296
Nathan Scottce8e9222006-01-11 15:39:08 +11001297 if (bp->b_flags & XBF_ORDERED) {
1298 ASSERT(!(bp->b_flags & XBF_READ));
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001299 rw = WRITE_BARRIER;
Dave Chinner2ee1aba2009-11-24 18:03:15 +00001300 } else if (bp->b_flags & XBF_LOG_BUFFER) {
Nathan Scott51bdd702006-09-28 11:01:57 +10001301 ASSERT(!(bp->b_flags & XBF_READ_AHEAD));
1302 bp->b_flags &= ~_XBF_RUN_QUEUES;
1303 rw = (bp->b_flags & XBF_WRITE) ? WRITE_SYNC : READ_SYNC;
Dave Chinner2ee1aba2009-11-24 18:03:15 +00001304 } else if (bp->b_flags & _XBF_RUN_QUEUES) {
1305 ASSERT(!(bp->b_flags & XBF_READ_AHEAD));
1306 bp->b_flags &= ~_XBF_RUN_QUEUES;
1307 rw = (bp->b_flags & XBF_WRITE) ? WRITE_META : READ_META;
Nathan Scott51bdd702006-09-28 11:01:57 +10001308 } else {
1309 rw = (bp->b_flags & XBF_WRITE) ? WRITE :
1310 (bp->b_flags & XBF_READ_AHEAD) ? READA : READ;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001311 }
1312
Nathan Scottce8e9222006-01-11 15:39:08 +11001313 /* Special code path for reading a sub page size buffer in --
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 * we populate up the whole page, and hence the other metadata
1315 * in the same page. This optimization is only valid when the
Nathan Scottce8e9222006-01-11 15:39:08 +11001316 * filesystem block size is not smaller than the page size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001318 if ((bp->b_buffer_length < PAGE_CACHE_SIZE) &&
Christoph Hellwig6ab455e2008-05-19 16:34:42 +10001319 ((bp->b_flags & (XBF_READ|_XBF_PAGE_LOCKED)) ==
1320 (XBF_READ|_XBF_PAGE_LOCKED)) &&
Nathan Scottce8e9222006-01-11 15:39:08 +11001321 (blocksize >= PAGE_CACHE_SIZE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 bio = bio_alloc(GFP_NOIO, 1);
1323
Nathan Scottce8e9222006-01-11 15:39:08 +11001324 bio->bi_bdev = bp->b_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 bio->bi_sector = sector - (offset >> BBSHIFT);
Nathan Scottce8e9222006-01-11 15:39:08 +11001326 bio->bi_end_io = xfs_buf_bio_end_io;
1327 bio->bi_private = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Nathan Scottce8e9222006-01-11 15:39:08 +11001329 bio_add_page(bio, bp->b_pages[0], PAGE_CACHE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 size = 0;
1331
Nathan Scottce8e9222006-01-11 15:39:08 +11001332 atomic_inc(&bp->b_io_remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 goto submit_io;
1335 }
1336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001338 atomic_inc(&bp->b_io_remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
1340 if (nr_pages > total_nr_pages)
1341 nr_pages = total_nr_pages;
1342
1343 bio = bio_alloc(GFP_NOIO, nr_pages);
Nathan Scottce8e9222006-01-11 15:39:08 +11001344 bio->bi_bdev = bp->b_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 bio->bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001346 bio->bi_end_io = xfs_buf_bio_end_io;
1347 bio->bi_private = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 for (; size && nr_pages; nr_pages--, map_i++) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001350 int rbytes, nbytes = PAGE_CACHE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 if (nbytes > size)
1353 nbytes = size;
1354
Nathan Scottce8e9222006-01-11 15:39:08 +11001355 rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset);
1356 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 break;
1358
1359 offset = 0;
1360 sector += nbytes >> BBSHIFT;
1361 size -= nbytes;
1362 total_nr_pages--;
1363 }
1364
1365submit_io:
1366 if (likely(bio->bi_size)) {
1367 submit_bio(rw, bio);
1368 if (size)
1369 goto next_chunk;
1370 } else {
1371 bio_put(bio);
Nathan Scottce8e9222006-01-11 15:39:08 +11001372 xfs_buf_ioerror(bp, EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 }
1374}
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376int
Nathan Scottce8e9222006-01-11 15:39:08 +11001377xfs_buf_iorequest(
1378 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001380 trace_xfs_buf_iorequest(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Nathan Scottce8e9222006-01-11 15:39:08 +11001382 if (bp->b_flags & XBF_DELWRI) {
1383 xfs_buf_delwri_queue(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return 0;
1385 }
1386
Nathan Scottce8e9222006-01-11 15:39:08 +11001387 if (bp->b_flags & XBF_WRITE) {
1388 xfs_buf_wait_unpin(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 }
1390
Nathan Scottce8e9222006-01-11 15:39:08 +11001391 xfs_buf_hold(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
1393 /* Set the count to 1 initially, this will stop an I/O
1394 * completion callout which happens before we have started
Nathan Scottce8e9222006-01-11 15:39:08 +11001395 * all the I/O from calling xfs_buf_ioend too early.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001397 atomic_set(&bp->b_io_remaining, 1);
1398 _xfs_buf_ioapply(bp);
1399 _xfs_buf_ioend(bp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Nathan Scottce8e9222006-01-11 15:39:08 +11001401 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 return 0;
1403}
1404
1405/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001406 * Waits for I/O to complete on the buffer supplied.
1407 * It returns immediately if no I/O is pending.
1408 * It returns the I/O error code, if any, or 0 if there was no error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 */
1410int
Nathan Scottce8e9222006-01-11 15:39:08 +11001411xfs_buf_iowait(
1412 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001414 trace_xfs_buf_iowait(bp, _RET_IP_);
1415
Nathan Scottce8e9222006-01-11 15:39:08 +11001416 if (atomic_read(&bp->b_io_remaining))
1417 blk_run_address_space(bp->b_target->bt_mapping);
David Chinnerb4dd3302008-08-13 16:36:11 +10001418 wait_for_completion(&bp->b_iowait);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001419
1420 trace_xfs_buf_iowait_done(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +11001421 return bp->b_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422}
1423
Nathan Scottce8e9222006-01-11 15:39:08 +11001424xfs_caddr_t
1425xfs_buf_offset(
1426 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 size_t offset)
1428{
1429 struct page *page;
1430
Nathan Scottce8e9222006-01-11 15:39:08 +11001431 if (bp->b_flags & XBF_MAPPED)
1432 return XFS_BUF_PTR(bp) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Nathan Scottce8e9222006-01-11 15:39:08 +11001434 offset += bp->b_offset;
1435 page = bp->b_pages[offset >> PAGE_CACHE_SHIFT];
1436 return (xfs_caddr_t)page_address(page) + (offset & (PAGE_CACHE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437}
1438
1439/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 * Move data into or out of a buffer.
1441 */
1442void
Nathan Scottce8e9222006-01-11 15:39:08 +11001443xfs_buf_iomove(
1444 xfs_buf_t *bp, /* buffer to process */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 size_t boff, /* starting buffer offset */
1446 size_t bsize, /* length to copy */
Dave Chinnerb9c48642010-01-20 10:47:39 +11001447 void *data, /* data address */
Nathan Scottce8e9222006-01-11 15:39:08 +11001448 xfs_buf_rw_t mode) /* read/write/zero flag */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
1450 size_t bend, cpoff, csize;
1451 struct page *page;
1452
1453 bend = boff + bsize;
1454 while (boff < bend) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001455 page = bp->b_pages[xfs_buf_btoct(boff + bp->b_offset)];
1456 cpoff = xfs_buf_poff(boff + bp->b_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 csize = min_t(size_t,
Nathan Scottce8e9222006-01-11 15:39:08 +11001458 PAGE_CACHE_SIZE-cpoff, bp->b_count_desired-boff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 ASSERT(((csize + cpoff) <= PAGE_CACHE_SIZE));
1461
1462 switch (mode) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001463 case XBRW_ZERO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 memset(page_address(page) + cpoff, 0, csize);
1465 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001466 case XBRW_READ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 memcpy(data, page_address(page) + cpoff, csize);
1468 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001469 case XBRW_WRITE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 memcpy(page_address(page) + cpoff, data, csize);
1471 }
1472
1473 boff += csize;
1474 data += csize;
1475 }
1476}
1477
1478/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001479 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 */
1481
1482/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001483 * Wait for any bufs with callbacks that have been submitted but
1484 * have not yet returned... walk the hash list for the target.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 */
1486void
1487xfs_wait_buftarg(
1488 xfs_buftarg_t *btp)
1489{
1490 xfs_buf_t *bp, *n;
1491 xfs_bufhash_t *hash;
1492 uint i;
1493
1494 for (i = 0; i < (1 << btp->bt_hashshift); i++) {
1495 hash = &btp->bt_hash[i];
1496again:
1497 spin_lock(&hash->bh_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +11001498 list_for_each_entry_safe(bp, n, &hash->bh_list, b_hash_list) {
1499 ASSERT(btp == bp->b_target);
1500 if (!(bp->b_flags & XBF_FS_MANAGED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 spin_unlock(&hash->bh_lock);
David Chinner2f926582005-09-05 08:33:35 +10001502 /*
1503 * Catch superblock reference count leaks
1504 * immediately
1505 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001506 BUG_ON(bp->b_bn == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 delay(100);
1508 goto again;
1509 }
1510 }
1511 spin_unlock(&hash->bh_lock);
1512 }
1513}
1514
1515/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001516 * Allocate buffer hash table for a given target.
1517 * For devices containing metadata (i.e. not the log/realtime devices)
1518 * we need to allocate a much larger hash table.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 */
1520STATIC void
1521xfs_alloc_bufhash(
1522 xfs_buftarg_t *btp,
1523 int external)
1524{
1525 unsigned int i;
1526
1527 btp->bt_hashshift = external ? 3 : 8; /* 8 or 256 buckets */
1528 btp->bt_hashmask = (1 << btp->bt_hashshift) - 1;
Christoph Hellwigbdfb0432010-01-20 21:55:30 +00001529 btp->bt_hash = kmem_zalloc_large((1 << btp->bt_hashshift) *
1530 sizeof(xfs_bufhash_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 for (i = 0; i < (1 << btp->bt_hashshift); i++) {
1532 spin_lock_init(&btp->bt_hash[i].bh_lock);
1533 INIT_LIST_HEAD(&btp->bt_hash[i].bh_list);
1534 }
1535}
1536
1537STATIC void
1538xfs_free_bufhash(
1539 xfs_buftarg_t *btp)
1540{
Christoph Hellwigbdfb0432010-01-20 21:55:30 +00001541 kmem_free_large(btp->bt_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 btp->bt_hash = NULL;
1543}
1544
David Chinnera6867a62006-01-11 15:37:58 +11001545/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001546 * buftarg list for delwrite queue processing
David Chinnera6867a62006-01-11 15:37:58 +11001547 */
Tim Shimmine6a0e9c2007-05-08 13:49:59 +10001548static LIST_HEAD(xfs_buftarg_list);
David Chinner7989cb82007-02-10 18:34:56 +11001549static DEFINE_SPINLOCK(xfs_buftarg_lock);
David Chinnera6867a62006-01-11 15:37:58 +11001550
1551STATIC void
1552xfs_register_buftarg(
1553 xfs_buftarg_t *btp)
1554{
1555 spin_lock(&xfs_buftarg_lock);
1556 list_add(&btp->bt_list, &xfs_buftarg_list);
1557 spin_unlock(&xfs_buftarg_lock);
1558}
1559
1560STATIC void
1561xfs_unregister_buftarg(
1562 xfs_buftarg_t *btp)
1563{
1564 spin_lock(&xfs_buftarg_lock);
1565 list_del(&btp->bt_list);
1566 spin_unlock(&xfs_buftarg_lock);
1567}
1568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569void
1570xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001571 struct xfs_mount *mp,
1572 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573{
1574 xfs_flush_buftarg(btp, 1);
Christoph Hellwigb7963132009-03-03 14:48:37 -05001575 if (mp->m_flags & XFS_MOUNT_BARRIER)
1576 xfs_blkdev_issue_flush(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 xfs_free_bufhash(btp);
Nathan Scottce8e9222006-01-11 15:39:08 +11001578 iput(btp->bt_mapping->host);
David Chinnera6867a62006-01-11 15:37:58 +11001579
Nathan Scottce8e9222006-01-11 15:39:08 +11001580 /* Unregister the buftarg first so that we don't get a
1581 * wakeup finding a non-existent task
1582 */
David Chinnera6867a62006-01-11 15:37:58 +11001583 xfs_unregister_buftarg(btp);
1584 kthread_stop(btp->bt_task);
1585
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001586 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587}
1588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589STATIC int
1590xfs_setsize_buftarg_flags(
1591 xfs_buftarg_t *btp,
1592 unsigned int blocksize,
1593 unsigned int sectorsize,
1594 int verbose)
1595{
Nathan Scottce8e9222006-01-11 15:39:08 +11001596 btp->bt_bsize = blocksize;
1597 btp->bt_sshift = ffs(sectorsize) - 1;
1598 btp->bt_smask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Nathan Scottce8e9222006-01-11 15:39:08 +11001600 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 printk(KERN_WARNING
1602 "XFS: Cannot set_blocksize to %u on device %s\n",
1603 sectorsize, XFS_BUFTARG_NAME(btp));
1604 return EINVAL;
1605 }
1606
1607 if (verbose &&
1608 (PAGE_CACHE_SIZE / BITS_PER_LONG) > sectorsize) {
1609 printk(KERN_WARNING
1610 "XFS: %u byte sectors in use on device %s. "
1611 "This is suboptimal; %u or greater is ideal.\n",
1612 sectorsize, XFS_BUFTARG_NAME(btp),
1613 (unsigned int)PAGE_CACHE_SIZE / BITS_PER_LONG);
1614 }
1615
1616 return 0;
1617}
1618
1619/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001620 * When allocating the initial buffer target we have not yet
1621 * read in the superblock, so don't know what sized sectors
1622 * are being used is at this early stage. Play safe.
1623 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624STATIC int
1625xfs_setsize_buftarg_early(
1626 xfs_buftarg_t *btp,
1627 struct block_device *bdev)
1628{
1629 return xfs_setsize_buftarg_flags(btp,
Martin K. Petersene1defc42009-05-22 17:17:49 -04001630 PAGE_CACHE_SIZE, bdev_logical_block_size(bdev), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631}
1632
1633int
1634xfs_setsize_buftarg(
1635 xfs_buftarg_t *btp,
1636 unsigned int blocksize,
1637 unsigned int sectorsize)
1638{
1639 return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
1640}
1641
1642STATIC int
1643xfs_mapping_buftarg(
1644 xfs_buftarg_t *btp,
1645 struct block_device *bdev)
1646{
1647 struct backing_dev_info *bdi;
1648 struct inode *inode;
1649 struct address_space *mapping;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07001650 static const struct address_space_operations mapping_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 .sync_page = block_sync_page,
Christoph Lametere965f962006-02-01 03:05:41 -08001652 .migratepage = fail_migrate_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 };
1654
1655 inode = new_inode(bdev->bd_inode->i_sb);
1656 if (!inode) {
1657 printk(KERN_WARNING
1658 "XFS: Cannot allocate mapping inode for device %s\n",
1659 XFS_BUFTARG_NAME(btp));
1660 return ENOMEM;
1661 }
1662 inode->i_mode = S_IFBLK;
1663 inode->i_bdev = bdev;
1664 inode->i_rdev = bdev->bd_dev;
1665 bdi = blk_get_backing_dev_info(bdev);
1666 if (!bdi)
1667 bdi = &default_backing_dev_info;
1668 mapping = &inode->i_data;
1669 mapping->a_ops = &mapping_aops;
1670 mapping->backing_dev_info = bdi;
1671 mapping_set_gfp_mask(mapping, GFP_NOFS);
Nathan Scottce8e9222006-01-11 15:39:08 +11001672 btp->bt_mapping = mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 return 0;
1674}
1675
David Chinnera6867a62006-01-11 15:37:58 +11001676STATIC int
1677xfs_alloc_delwrite_queue(
1678 xfs_buftarg_t *btp)
1679{
1680 int error = 0;
1681
1682 INIT_LIST_HEAD(&btp->bt_list);
1683 INIT_LIST_HEAD(&btp->bt_delwrite_queue);
Eric Sandeen007c61c2007-10-11 17:43:56 +10001684 spin_lock_init(&btp->bt_delwrite_lock);
David Chinnera6867a62006-01-11 15:37:58 +11001685 btp->bt_flags = 0;
1686 btp->bt_task = kthread_run(xfsbufd, btp, "xfsbufd");
1687 if (IS_ERR(btp->bt_task)) {
1688 error = PTR_ERR(btp->bt_task);
1689 goto out_error;
1690 }
1691 xfs_register_buftarg(btp);
1692out_error:
1693 return error;
1694}
1695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696xfs_buftarg_t *
1697xfs_alloc_buftarg(
1698 struct block_device *bdev,
1699 int external)
1700{
1701 xfs_buftarg_t *btp;
1702
1703 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
1704
Nathan Scottce8e9222006-01-11 15:39:08 +11001705 btp->bt_dev = bdev->bd_dev;
1706 btp->bt_bdev = bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 if (xfs_setsize_buftarg_early(btp, bdev))
1708 goto error;
1709 if (xfs_mapping_buftarg(btp, bdev))
1710 goto error;
David Chinnera6867a62006-01-11 15:37:58 +11001711 if (xfs_alloc_delwrite_queue(btp))
1712 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 xfs_alloc_bufhash(btp, external);
1714 return btp;
1715
1716error:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001717 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 return NULL;
1719}
1720
1721
1722/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001723 * Delayed write buffer handling
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001726xfs_buf_delwri_queue(
1727 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 int unlock)
1729{
Nathan Scottce8e9222006-01-11 15:39:08 +11001730 struct list_head *dwq = &bp->b_target->bt_delwrite_queue;
1731 spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
David Chinnera6867a62006-01-11 15:37:58 +11001732
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001733 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
1734
Nathan Scottce8e9222006-01-11 15:39:08 +11001735 ASSERT((bp->b_flags&(XBF_DELWRI|XBF_ASYNC)) == (XBF_DELWRI|XBF_ASYNC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
David Chinnera6867a62006-01-11 15:37:58 +11001737 spin_lock(dwlk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 /* If already in the queue, dequeue and place at tail */
Nathan Scottce8e9222006-01-11 15:39:08 +11001739 if (!list_empty(&bp->b_list)) {
1740 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1741 if (unlock)
1742 atomic_dec(&bp->b_hold);
1743 list_del(&bp->b_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 }
1745
Dave Chinnerc9c12972010-01-11 11:49:59 +00001746 if (list_empty(dwq)) {
1747 /* start xfsbufd as it is about to have something to do */
1748 wake_up_process(bp->b_target->bt_task);
1749 }
1750
Nathan Scottce8e9222006-01-11 15:39:08 +11001751 bp->b_flags |= _XBF_DELWRI_Q;
1752 list_add_tail(&bp->b_list, dwq);
1753 bp->b_queuetime = jiffies;
David Chinnera6867a62006-01-11 15:37:58 +11001754 spin_unlock(dwlk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756 if (unlock)
Nathan Scottce8e9222006-01-11 15:39:08 +11001757 xfs_buf_unlock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758}
1759
1760void
Nathan Scottce8e9222006-01-11 15:39:08 +11001761xfs_buf_delwri_dequeue(
1762 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763{
Nathan Scottce8e9222006-01-11 15:39:08 +11001764 spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 int dequeued = 0;
1766
David Chinnera6867a62006-01-11 15:37:58 +11001767 spin_lock(dwlk);
Nathan Scottce8e9222006-01-11 15:39:08 +11001768 if ((bp->b_flags & XBF_DELWRI) && !list_empty(&bp->b_list)) {
1769 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1770 list_del_init(&bp->b_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 dequeued = 1;
1772 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001773 bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q);
David Chinnera6867a62006-01-11 15:37:58 +11001774 spin_unlock(dwlk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
1776 if (dequeued)
Nathan Scottce8e9222006-01-11 15:39:08 +11001777 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001779 trace_xfs_buf_delwri_dequeue(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780}
1781
Dave Chinnerd808f612010-02-02 10:13:42 +11001782/*
1783 * If a delwri buffer needs to be pushed before it has aged out, then promote
1784 * it to the head of the delwri queue so that it will be flushed on the next
1785 * xfsbufd run. We do this by resetting the queuetime of the buffer to be older
1786 * than the age currently needed to flush the buffer. Hence the next time the
1787 * xfsbufd sees it is guaranteed to be considered old enough to flush.
1788 */
1789void
1790xfs_buf_delwri_promote(
1791 struct xfs_buf *bp)
1792{
1793 struct xfs_buftarg *btp = bp->b_target;
1794 long age = xfs_buf_age_centisecs * msecs_to_jiffies(10) + 1;
1795
1796 ASSERT(bp->b_flags & XBF_DELWRI);
1797 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1798
1799 /*
1800 * Check the buffer age before locking the delayed write queue as we
1801 * don't need to promote buffers that are already past the flush age.
1802 */
1803 if (bp->b_queuetime < jiffies - age)
1804 return;
1805 bp->b_queuetime = jiffies - age;
1806 spin_lock(&btp->bt_delwrite_lock);
1807 list_move(&bp->b_list, &btp->bt_delwrite_queue);
1808 spin_unlock(&btp->bt_delwrite_lock);
1809}
1810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001812xfs_buf_runall_queues(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 struct workqueue_struct *queue)
1814{
1815 flush_workqueue(queue);
1816}
1817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818STATIC int
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001819xfsbufd_wakeup(
Nathan Scott15c84a42005-11-04 10:51:01 +11001820 int priority,
1821 gfp_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
Christoph Hellwigda7f93e2006-01-11 20:49:57 +11001823 xfs_buftarg_t *btp;
David Chinnera6867a62006-01-11 15:37:58 +11001824
1825 spin_lock(&xfs_buftarg_lock);
Christoph Hellwigda7f93e2006-01-11 20:49:57 +11001826 list_for_each_entry(btp, &xfs_buftarg_list, bt_list) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001827 if (test_bit(XBT_FORCE_SLEEP, &btp->bt_flags))
David Chinnera6867a62006-01-11 15:37:58 +11001828 continue;
Dave Chinnerc9c12972010-01-11 11:49:59 +00001829 if (list_empty(&btp->bt_delwrite_queue))
1830 continue;
Nathan Scottce8e9222006-01-11 15:39:08 +11001831 set_bit(XBT_FORCE_FLUSH, &btp->bt_flags);
David Chinnera6867a62006-01-11 15:37:58 +11001832 wake_up_process(btp->bt_task);
1833 }
1834 spin_unlock(&xfs_buftarg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 return 0;
1836}
1837
David Chinner585e6d82007-02-10 18:32:29 +11001838/*
1839 * Move as many buffers as specified to the supplied list
1840 * idicating if we skipped any buffers to prevent deadlocks.
1841 */
1842STATIC int
1843xfs_buf_delwri_split(
1844 xfs_buftarg_t *target,
1845 struct list_head *list,
David Chinner5e6a07d2007-02-10 18:34:49 +11001846 unsigned long age)
David Chinner585e6d82007-02-10 18:32:29 +11001847{
1848 xfs_buf_t *bp, *n;
1849 struct list_head *dwq = &target->bt_delwrite_queue;
1850 spinlock_t *dwlk = &target->bt_delwrite_lock;
1851 int skipped = 0;
David Chinner5e6a07d2007-02-10 18:34:49 +11001852 int force;
David Chinner585e6d82007-02-10 18:32:29 +11001853
David Chinner5e6a07d2007-02-10 18:34:49 +11001854 force = test_and_clear_bit(XBT_FORCE_FLUSH, &target->bt_flags);
David Chinner585e6d82007-02-10 18:32:29 +11001855 INIT_LIST_HEAD(list);
1856 spin_lock(dwlk);
1857 list_for_each_entry_safe(bp, n, dwq, b_list) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001858 trace_xfs_buf_delwri_split(bp, _RET_IP_);
David Chinner585e6d82007-02-10 18:32:29 +11001859 ASSERT(bp->b_flags & XBF_DELWRI);
1860
1861 if (!xfs_buf_ispin(bp) && !xfs_buf_cond_lock(bp)) {
David Chinner5e6a07d2007-02-10 18:34:49 +11001862 if (!force &&
David Chinner585e6d82007-02-10 18:32:29 +11001863 time_before(jiffies, bp->b_queuetime + age)) {
1864 xfs_buf_unlock(bp);
1865 break;
1866 }
1867
1868 bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q|
1869 _XBF_RUN_QUEUES);
1870 bp->b_flags |= XBF_WRITE;
1871 list_move_tail(&bp->b_list, list);
1872 } else
1873 skipped++;
1874 }
1875 spin_unlock(dwlk);
1876
1877 return skipped;
1878
1879}
1880
Dave Chinner089716a2010-01-26 15:13:25 +11001881/*
1882 * Compare function is more complex than it needs to be because
1883 * the return value is only 32 bits and we are doing comparisons
1884 * on 64 bit values
1885 */
1886static int
1887xfs_buf_cmp(
1888 void *priv,
1889 struct list_head *a,
1890 struct list_head *b)
1891{
1892 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
1893 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
1894 xfs_daddr_t diff;
1895
1896 diff = ap->b_bn - bp->b_bn;
1897 if (diff < 0)
1898 return -1;
1899 if (diff > 0)
1900 return 1;
1901 return 0;
1902}
1903
1904void
1905xfs_buf_delwri_sort(
1906 xfs_buftarg_t *target,
1907 struct list_head *list)
1908{
1909 list_sort(NULL, list, xfs_buf_cmp);
1910}
1911
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912STATIC int
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001913xfsbufd(
David Chinner585e6d82007-02-10 18:32:29 +11001914 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915{
Dave Chinner089716a2010-01-26 15:13:25 +11001916 xfs_buftarg_t *target = (xfs_buftarg_t *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 current->flags |= PF_MEMALLOC;
1919
Rafael J. Wysocki978c7b22007-12-07 14:09:02 +11001920 set_freezable();
1921
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 do {
Dave Chinnerc9c12972010-01-11 11:49:59 +00001923 long age = xfs_buf_age_centisecs * msecs_to_jiffies(10);
1924 long tout = xfs_buf_timer_centisecs * msecs_to_jiffies(10);
Dave Chinner089716a2010-01-26 15:13:25 +11001925 int count = 0;
1926 struct list_head tmp;
Dave Chinnerc9c12972010-01-11 11:49:59 +00001927
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001928 if (unlikely(freezing(current))) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001929 set_bit(XBT_FORCE_SLEEP, &target->bt_flags);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001930 refrigerator();
Nathan Scottabd0cf72005-05-05 13:30:13 -07001931 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +11001932 clear_bit(XBT_FORCE_SLEEP, &target->bt_flags);
Nathan Scottabd0cf72005-05-05 13:30:13 -07001933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Dave Chinnerc9c12972010-01-11 11:49:59 +00001935 /* sleep for a long time if there is nothing to do. */
1936 if (list_empty(&target->bt_delwrite_queue))
1937 tout = MAX_SCHEDULE_TIMEOUT;
1938 schedule_timeout_interruptible(tout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Dave Chinnerc9c12972010-01-11 11:49:59 +00001940 xfs_buf_delwri_split(target, &tmp, age);
Dave Chinner089716a2010-01-26 15:13:25 +11001941 list_sort(NULL, &tmp, xfs_buf_cmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 while (!list_empty(&tmp)) {
Dave Chinner089716a2010-01-26 15:13:25 +11001943 struct xfs_buf *bp;
1944 bp = list_first_entry(&tmp, struct xfs_buf, b_list);
Nathan Scottce8e9222006-01-11 15:39:08 +11001945 list_del_init(&bp->b_list);
1946 xfs_buf_iostrategy(bp);
David Chinner585e6d82007-02-10 18:32:29 +11001947 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 }
1949
Felix Blyakher3a011a12009-02-18 15:56:51 -06001950 if (as_list_len > 0)
1951 purge_addresses();
Nathan Scottf07c2252006-09-28 10:52:15 +10001952 if (count)
1953 blk_run_address_space(target->bt_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Christoph Hellwig4df08c52005-09-05 08:34:18 +10001955 } while (!kthread_should_stop());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Christoph Hellwig4df08c52005-09-05 08:34:18 +10001957 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958}
1959
1960/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001961 * Go through all incore buffers, and release buffers if they belong to
1962 * the given device. This is used in filesystem error handling to
1963 * preserve the consistency of its metadata.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 */
1965int
1966xfs_flush_buftarg(
David Chinner585e6d82007-02-10 18:32:29 +11001967 xfs_buftarg_t *target,
1968 int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
Dave Chinner089716a2010-01-26 15:13:25 +11001970 xfs_buf_t *bp;
David Chinner585e6d82007-02-10 18:32:29 +11001971 int pincount = 0;
Dave Chinner089716a2010-01-26 15:13:25 +11001972 LIST_HEAD(tmp_list);
1973 LIST_HEAD(wait_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Dave Chinnerc626d172009-04-06 18:42:11 +02001975 xfs_buf_runall_queues(xfsconvertd_workqueue);
Nathan Scottce8e9222006-01-11 15:39:08 +11001976 xfs_buf_runall_queues(xfsdatad_workqueue);
1977 xfs_buf_runall_queues(xfslogd_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
David Chinner5e6a07d2007-02-10 18:34:49 +11001979 set_bit(XBT_FORCE_FLUSH, &target->bt_flags);
Dave Chinner089716a2010-01-26 15:13:25 +11001980 pincount = xfs_buf_delwri_split(target, &tmp_list, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
1982 /*
Dave Chinner089716a2010-01-26 15:13:25 +11001983 * Dropped the delayed write list lock, now walk the temporary list.
1984 * All I/O is issued async and then if we need to wait for completion
1985 * we do that after issuing all the IO.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 */
Dave Chinner089716a2010-01-26 15:13:25 +11001987 list_sort(NULL, &tmp_list, xfs_buf_cmp);
1988 while (!list_empty(&tmp_list)) {
1989 bp = list_first_entry(&tmp_list, struct xfs_buf, b_list);
David Chinner585e6d82007-02-10 18:32:29 +11001990 ASSERT(target == bp->b_target);
Dave Chinner089716a2010-01-26 15:13:25 +11001991 list_del_init(&bp->b_list);
1992 if (wait) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001993 bp->b_flags &= ~XBF_ASYNC;
Dave Chinner089716a2010-01-26 15:13:25 +11001994 list_add(&bp->b_list, &wait_list);
1995 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001996 xfs_buf_iostrategy(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 }
1998
Dave Chinner089716a2010-01-26 15:13:25 +11001999 if (wait) {
2000 /* Expedite and wait for IO to complete. */
Nathan Scottf07c2252006-09-28 10:52:15 +10002001 blk_run_address_space(target->bt_mapping);
Dave Chinner089716a2010-01-26 15:13:25 +11002002 while (!list_empty(&wait_list)) {
2003 bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
Nathan Scottf07c2252006-09-28 10:52:15 +10002004
Dave Chinner089716a2010-01-26 15:13:25 +11002005 list_del_init(&bp->b_list);
2006 xfs_iowait(bp);
2007 xfs_buf_relse(bp);
2008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 }
2010
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 return pincount;
2012}
2013
Christoph Hellwig04d8b282005-11-02 10:15:05 +11002014int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11002015xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016{
Nathan Scott87582802006-03-14 13:18:19 +11002017 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
2018 KM_ZONE_HWALIGN, NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11002019 if (!xfs_buf_zone)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002020 goto out;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11002021
Rafael J. Wysockib4337692007-03-22 00:11:27 -08002022 xfslogd_workqueue = create_workqueue("xfslogd");
Christoph Hellwig23ea4032005-06-21 15:14:01 +10002023 if (!xfslogd_workqueue)
Christoph Hellwig04d8b282005-11-02 10:15:05 +11002024 goto out_free_buf_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Rafael J. Wysockib4337692007-03-22 00:11:27 -08002026 xfsdatad_workqueue = create_workqueue("xfsdatad");
Christoph Hellwig23ea4032005-06-21 15:14:01 +10002027 if (!xfsdatad_workqueue)
2028 goto out_destroy_xfslogd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Dave Chinnerc626d172009-04-06 18:42:11 +02002030 xfsconvertd_workqueue = create_workqueue("xfsconvertd");
2031 if (!xfsconvertd_workqueue)
2032 goto out_destroy_xfsdatad_workqueue;
2033
Rusty Russell8e1f9362007-07-17 04:03:17 -07002034 register_shrinker(&xfs_buf_shake);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10002035 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Dave Chinnerc626d172009-04-06 18:42:11 +02002037 out_destroy_xfsdatad_workqueue:
2038 destroy_workqueue(xfsdatad_workqueue);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10002039 out_destroy_xfslogd_workqueue:
2040 destroy_workqueue(xfslogd_workqueue);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10002041 out_free_buf_zone:
Nathan Scottce8e9222006-01-11 15:39:08 +11002042 kmem_zone_destroy(xfs_buf_zone);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002043 out:
Nathan Scott87582802006-03-14 13:18:19 +11002044 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045}
2046
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047void
Nathan Scottce8e9222006-01-11 15:39:08 +11002048xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
Rusty Russell8e1f9362007-07-17 04:03:17 -07002050 unregister_shrinker(&xfs_buf_shake);
Dave Chinnerc626d172009-04-06 18:42:11 +02002051 destroy_workqueue(xfsconvertd_workqueue);
Christoph Hellwig04d8b282005-11-02 10:15:05 +11002052 destroy_workqueue(xfsdatad_workqueue);
2053 destroy_workqueue(xfslogd_workqueue);
Nathan Scottce8e9222006-01-11 15:39:08 +11002054 kmem_zone_destroy(xfs_buf_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055}
Tim Shimmine6a0e9c2007-05-08 13:49:59 +10002056
2057#ifdef CONFIG_KDB_MODULES
2058struct list_head *
2059xfs_get_buftarg_list(void)
2060{
2061 return &xfs_buftarg_list;
2062}
2063#endif