blob: 4c5deb6e9e3192f1bd279ca9e7b02e824fe9e7cc [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#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"
Dave Chinnered3b4d62010-05-21 12:07:08 +100040#include "xfs_log.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050041#include "xfs_ag.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050042#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 *);
Dave Chinner7f8275d2010-07-19 14:56:17 +100047STATIC int xfsbufd_wakeup(struct shrinker *, 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
James Bottomley73c77e22010-01-25 11:42:24 -060080static inline int
81xfs_buf_is_vmapped(
82 struct xfs_buf *bp)
83{
84 /*
85 * Return true if the buffer is vmapped.
86 *
87 * The XBF_MAPPED flag is set if the buffer should be mapped, but the
88 * code is clever enough to know it doesn't have to map a single page,
89 * so the check has to be both for XBF_MAPPED and bp->b_page_count > 1.
90 */
91 return (bp->b_flags & XBF_MAPPED) && bp->b_page_count > 1;
92}
93
94static inline int
95xfs_buf_vmap_len(
96 struct xfs_buf *bp)
97{
98 return (bp->b_page_count * PAGE_SIZE) - bp->b_offset;
99}
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100102 * Page Region interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 *
Nathan Scottce8e9222006-01-11 15:39:08 +1100104 * For pages in filesystems where the blocksize is smaller than the
105 * pagesize, we use the page->private field (long) to hold a bitmap
106 * of uptodate regions within the page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 *
Nathan Scottce8e9222006-01-11 15:39:08 +1100108 * Each such region is "bytes per page / bits per long" bytes long.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 *
Nathan Scottce8e9222006-01-11 15:39:08 +1100110 * NBPPR == number-of-bytes-per-page-region
111 * BTOPR == bytes-to-page-region (rounded up)
112 * BTOPRT == bytes-to-page-region-truncated (rounded down)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 */
114#if (BITS_PER_LONG == 32)
115#define PRSHIFT (PAGE_CACHE_SHIFT - 5) /* (32 == 1<<5) */
116#elif (BITS_PER_LONG == 64)
117#define PRSHIFT (PAGE_CACHE_SHIFT - 6) /* (64 == 1<<6) */
118#else
119#error BITS_PER_LONG must be 32 or 64
120#endif
121#define NBPPR (PAGE_CACHE_SIZE/BITS_PER_LONG)
122#define BTOPR(b) (((unsigned int)(b) + (NBPPR - 1)) >> PRSHIFT)
123#define BTOPRT(b) (((unsigned int)(b) >> PRSHIFT))
124
125STATIC unsigned long
126page_region_mask(
127 size_t offset,
128 size_t length)
129{
130 unsigned long mask;
131 int first, final;
132
133 first = BTOPR(offset);
134 final = BTOPRT(offset + length - 1);
135 first = min(first, final);
136
137 mask = ~0UL;
138 mask <<= BITS_PER_LONG - (final - first);
139 mask >>= BITS_PER_LONG - (final);
140
141 ASSERT(offset + length <= PAGE_CACHE_SIZE);
142 ASSERT((final - first) < BITS_PER_LONG && (final - first) >= 0);
143
144 return mask;
145}
146
Christoph Hellwigb8f82a42009-11-14 16:17:22 +0000147STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148set_page_region(
149 struct page *page,
150 size_t offset,
151 size_t length)
152{
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700153 set_page_private(page,
154 page_private(page) | page_region_mask(offset, length));
155 if (page_private(page) == ~0UL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 SetPageUptodate(page);
157}
158
Christoph Hellwigb8f82a42009-11-14 16:17:22 +0000159STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160test_page_region(
161 struct page *page,
162 size_t offset,
163 size_t length)
164{
165 unsigned long mask = page_region_mask(offset, length);
166
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700167 return (mask && (page_private(page) & mask) == mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
170/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100171 * Internal xfs_buf_t object manipulation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 */
173
174STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100175_xfs_buf_initialize(
176 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100178 xfs_off_t range_base,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 size_t range_length,
Nathan Scottce8e9222006-01-11 15:39:08 +1100180 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 /*
Nathan Scottce8e9222006-01-11 15:39:08 +1100183 * We don't want certain flags to appear in b_flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100185 flags &= ~(XBF_LOCK|XBF_MAPPED|XBF_DONT_BLOCK|XBF_READ_AHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Nathan Scottce8e9222006-01-11 15:39:08 +1100187 memset(bp, 0, sizeof(xfs_buf_t));
188 atomic_set(&bp->b_hold, 1);
David Chinnerb4dd3302008-08-13 16:36:11 +1000189 init_completion(&bp->b_iowait);
Nathan Scottce8e9222006-01-11 15:39:08 +1100190 INIT_LIST_HEAD(&bp->b_list);
Dave Chinner74f75a02010-09-24 19:59:04 +1000191 RB_CLEAR_NODE(&bp->b_rbnode);
Thomas Gleixnera731cd12010-09-07 14:33:15 +0000192 sema_init(&bp->b_sema, 0); /* held, no waiters */
Nathan Scottce8e9222006-01-11 15:39:08 +1100193 XB_SET_OWNER(bp);
194 bp->b_target = target;
195 bp->b_file_offset = range_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 /*
197 * Set buffer_length and count_desired to the same value initially.
198 * I/O routines should use count_desired, which will be the same in
199 * most cases but may be reset (e.g. XFS recovery).
200 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100201 bp->b_buffer_length = bp->b_count_desired = range_length;
202 bp->b_flags = flags;
203 bp->b_bn = XFS_BUF_DADDR_NULL;
204 atomic_set(&bp->b_pin_count, 0);
205 init_waitqueue_head(&bp->b_waiters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Nathan Scottce8e9222006-01-11 15:39:08 +1100207 XFS_STATS_INC(xb_create);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000208
209 trace_xfs_buf_init(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
212/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100213 * Allocate a page array capable of holding a specified number
214 * of pages, and point the page buf at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 */
216STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100217_xfs_buf_get_pages(
218 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 int page_count,
Nathan Scottce8e9222006-01-11 15:39:08 +1100220 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 /* Make sure that we have a page list */
Nathan Scottce8e9222006-01-11 15:39:08 +1100223 if (bp->b_pages == NULL) {
224 bp->b_offset = xfs_buf_poff(bp->b_file_offset);
225 bp->b_page_count = page_count;
226 if (page_count <= XB_PAGES) {
227 bp->b_pages = bp->b_page_array;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100229 bp->b_pages = kmem_alloc(sizeof(struct page *) *
230 page_count, xb_to_km(flags));
231 if (bp->b_pages == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return -ENOMEM;
233 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100234 memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236 return 0;
237}
238
239/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100240 * Frees b_pages if it was allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 */
242STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100243_xfs_buf_free_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 xfs_buf_t *bp)
245{
Nathan Scottce8e9222006-01-11 15:39:08 +1100246 if (bp->b_pages != bp->b_page_array) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000247 kmem_free(bp->b_pages);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000248 bp->b_pages = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
250}
251
252/*
253 * Releases the specified buffer.
254 *
255 * The modification state of any associated pages is left unchanged.
Nathan Scottce8e9222006-01-11 15:39:08 +1100256 * The buffer most not be on any hash - use xfs_buf_rele instead for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 * hashed and refcounted buffers
258 */
259void
Nathan Scottce8e9222006-01-11 15:39:08 +1100260xfs_buf_free(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 xfs_buf_t *bp)
262{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000263 trace_xfs_buf_free(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000265 if (bp->b_flags & (_XBF_PAGE_CACHE|_XBF_PAGES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 uint i;
267
James Bottomley73c77e22010-01-25 11:42:24 -0600268 if (xfs_buf_is_vmapped(bp))
Alex Elder8a262e52010-03-16 18:55:56 +0000269 vm_unmap_ram(bp->b_addr - bp->b_offset,
270 bp->b_page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Nathan Scott948ecdb2006-09-28 11:03:13 +1000272 for (i = 0; i < bp->b_page_count; i++) {
273 struct page *page = bp->b_pages[i];
274
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000275 if (bp->b_flags & _XBF_PAGE_CACHE)
276 ASSERT(!PagePrivate(page));
Nathan Scott948ecdb2006-09-28 11:03:13 +1000277 page_cache_release(page);
278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
Dave Chinner3fc98b12009-12-14 23:11:57 +0000280 _xfs_buf_free_pages(bp);
Nathan Scottce8e9222006-01-11 15:39:08 +1100281 xfs_buf_deallocate(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
284/*
285 * Finds all pages for buffer in question and builds it's page list.
286 */
287STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100288_xfs_buf_lookup_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 xfs_buf_t *bp,
290 uint flags)
291{
Nathan Scottce8e9222006-01-11 15:39:08 +1100292 struct address_space *mapping = bp->b_target->bt_mapping;
293 size_t blocksize = bp->b_target->bt_bsize;
294 size_t size = bp->b_count_desired;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 size_t nbytes, offset;
Nathan Scottce8e9222006-01-11 15:39:08 +1100296 gfp_t gfp_mask = xb_to_gfp(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 unsigned short page_count, i;
298 pgoff_t first;
Nathan Scott204ab252006-01-11 20:50:22 +1100299 xfs_off_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 int error;
301
Nathan Scottce8e9222006-01-11 15:39:08 +1100302 end = bp->b_file_offset + bp->b_buffer_length;
303 page_count = xfs_buf_btoc(end) - xfs_buf_btoct(bp->b_file_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Nathan Scottce8e9222006-01-11 15:39:08 +1100305 error = _xfs_buf_get_pages(bp, page_count, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (unlikely(error))
307 return error;
Nathan Scottce8e9222006-01-11 15:39:08 +1100308 bp->b_flags |= _XBF_PAGE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Nathan Scottce8e9222006-01-11 15:39:08 +1100310 offset = bp->b_offset;
311 first = bp->b_file_offset >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Nathan Scottce8e9222006-01-11 15:39:08 +1100313 for (i = 0; i < bp->b_page_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 struct page *page;
315 uint retries = 0;
316
317 retry:
318 page = find_or_create_page(mapping, first + i, gfp_mask);
319 if (unlikely(page == NULL)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100320 if (flags & XBF_READ_AHEAD) {
321 bp->b_page_count = i;
Christoph Hellwig6ab455e2008-05-19 16:34:42 +1000322 for (i = 0; i < bp->b_page_count; i++)
323 unlock_page(bp->b_pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return -ENOMEM;
325 }
326
327 /*
328 * This could deadlock.
329 *
330 * But until all the XFS lowlevel code is revamped to
331 * handle buffer allocation failures we can't do much.
332 */
333 if (!(++retries % 100))
334 printk(KERN_ERR
335 "XFS: possible memory allocation "
336 "deadlock in %s (mode:0x%x)\n",
Harvey Harrison34a622b2008-04-10 12:19:21 +1000337 __func__, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Nathan Scottce8e9222006-01-11 15:39:08 +1100339 XFS_STATS_INC(xb_page_retries);
Dave Chinner7f8275d2010-07-19 14:56:17 +1000340 xfsbufd_wakeup(NULL, 0, gfp_mask);
Jens Axboe8aa7e842009-07-09 14:52:32 +0200341 congestion_wait(BLK_RW_ASYNC, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 goto retry;
343 }
344
Nathan Scottce8e9222006-01-11 15:39:08 +1100345 XFS_STATS_INC(xb_page_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 nbytes = min_t(size_t, size, PAGE_CACHE_SIZE - offset);
348 size -= nbytes;
349
Nathan Scott948ecdb2006-09-28 11:03:13 +1000350 ASSERT(!PagePrivate(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (!PageUptodate(page)) {
352 page_count--;
Christoph Hellwig6ab455e2008-05-19 16:34:42 +1000353 if (blocksize >= PAGE_CACHE_SIZE) {
354 if (flags & XBF_READ)
355 bp->b_flags |= _XBF_PAGE_LOCKED;
356 } else if (!PagePrivate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (test_page_region(page, offset, nbytes))
358 page_count++;
359 }
360 }
361
Nathan Scottce8e9222006-01-11 15:39:08 +1100362 bp->b_pages[i] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 offset = 0;
364 }
365
Christoph Hellwig6ab455e2008-05-19 16:34:42 +1000366 if (!(bp->b_flags & _XBF_PAGE_LOCKED)) {
367 for (i = 0; i < bp->b_page_count; i++)
368 unlock_page(bp->b_pages[i]);
369 }
370
Nathan Scottce8e9222006-01-11 15:39:08 +1100371 if (page_count == bp->b_page_count)
372 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return error;
375}
376
377/*
378 * Map buffer into kernel address-space if nessecary.
379 */
380STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100381_xfs_buf_map_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 xfs_buf_t *bp,
383 uint flags)
384{
385 /* A single page buffer is always mappable */
Nathan Scottce8e9222006-01-11 15:39:08 +1100386 if (bp->b_page_count == 1) {
387 bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
388 bp->b_flags |= XBF_MAPPED;
389 } else if (flags & XBF_MAPPED) {
Alex Elder8a262e52010-03-16 18:55:56 +0000390 bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
391 -1, PAGE_KERNEL);
Nathan Scottce8e9222006-01-11 15:39:08 +1100392 if (unlikely(bp->b_addr == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return -ENOMEM;
Nathan Scottce8e9222006-01-11 15:39:08 +1100394 bp->b_addr += bp->b_offset;
395 bp->b_flags |= XBF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
397
398 return 0;
399}
400
401/*
402 * Finding and Reading Buffers
403 */
404
405/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100406 * Look up, and creates if absent, a lockable buffer for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 * a given range of an inode. The buffer is returned
408 * locked. If other overlapping buffers exist, they are
409 * released before the new buffer is created and locked,
410 * which may imply that this call will block until those buffers
411 * are unlocked. No I/O is implied by this call.
412 */
413xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100414_xfs_buf_find(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 xfs_buftarg_t *btp, /* block device target */
Nathan Scott204ab252006-01-11 20:50:22 +1100416 xfs_off_t ioff, /* starting offset of range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 size_t isize, /* length of range */
Nathan Scottce8e9222006-01-11 15:39:08 +1100418 xfs_buf_flags_t flags,
419 xfs_buf_t *new_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Nathan Scott204ab252006-01-11 20:50:22 +1100421 xfs_off_t range_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 size_t range_length;
Dave Chinner74f75a02010-09-24 19:59:04 +1000423 struct xfs_perag *pag;
424 struct rb_node **rbp;
425 struct rb_node *parent;
426 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 range_base = (ioff << BBSHIFT);
429 range_length = (isize << BBSHIFT);
430
431 /* Check for IOs smaller than the sector size / not sector aligned */
Nathan Scottce8e9222006-01-11 15:39:08 +1100432 ASSERT(!(range_length < (1 << btp->bt_sshift)));
Nathan Scott204ab252006-01-11 20:50:22 +1100433 ASSERT(!(range_base & (xfs_off_t)btp->bt_smask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Dave Chinner74f75a02010-09-24 19:59:04 +1000435 /* get tree root */
436 pag = xfs_perag_get(btp->bt_mount,
437 xfs_daddr_to_agno(btp->bt_mount, ioff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Dave Chinner74f75a02010-09-24 19:59:04 +1000439 /* walk tree */
440 spin_lock(&pag->pag_buf_lock);
441 rbp = &pag->pag_buf_tree.rb_node;
442 parent = NULL;
443 bp = NULL;
444 while (*rbp) {
445 parent = *rbp;
446 bp = rb_entry(parent, struct xfs_buf, b_rbnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Dave Chinner74f75a02010-09-24 19:59:04 +1000448 if (range_base < bp->b_file_offset)
449 rbp = &(*rbp)->rb_left;
450 else if (range_base > bp->b_file_offset)
451 rbp = &(*rbp)->rb_right;
452 else {
453 /*
454 * found a block offset match. If the range doesn't
455 * match, the only way this is allowed is if the buffer
456 * in the cache is stale and the transaction that made
457 * it stale has not yet committed. i.e. we are
458 * reallocating a busy extent. Skip this buffer and
459 * continue searching to the right for an exact match.
460 */
461 if (bp->b_buffer_length != range_length) {
462 ASSERT(bp->b_flags & XBF_STALE);
463 rbp = &(*rbp)->rb_right;
464 continue;
465 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100466 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 goto found;
468 }
469 }
470
471 /* No match found */
Nathan Scottce8e9222006-01-11 15:39:08 +1100472 if (new_bp) {
473 _xfs_buf_initialize(new_bp, btp, range_base,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 range_length, flags);
Dave Chinner74f75a02010-09-24 19:59:04 +1000475 rb_link_node(&new_bp->b_rbnode, parent, rbp);
476 rb_insert_color(&new_bp->b_rbnode, &pag->pag_buf_tree);
477 /* the buffer keeps the perag reference until it is freed */
478 new_bp->b_pag = pag;
479 spin_unlock(&pag->pag_buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100481 XFS_STATS_INC(xb_miss_locked);
Dave Chinner74f75a02010-09-24 19:59:04 +1000482 spin_unlock(&pag->pag_buf_lock);
483 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100485 return new_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487found:
Dave Chinner74f75a02010-09-24 19:59:04 +1000488 spin_unlock(&pag->pag_buf_lock);
489 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Dave Chinner90810b92010-11-30 15:16:16 +1100491 if (xfs_buf_cond_lock(bp)) {
492 /* failed, so wait for the lock if requested. */
Nathan Scottce8e9222006-01-11 15:39:08 +1100493 if (!(flags & XBF_TRYLOCK)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100494 xfs_buf_lock(bp);
495 XFS_STATS_INC(xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100497 xfs_buf_rele(bp);
498 XFS_STATS_INC(xb_busy_locked);
499 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
502
Nathan Scottce8e9222006-01-11 15:39:08 +1100503 if (bp->b_flags & XBF_STALE) {
504 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
505 bp->b_flags &= XBF_MAPPED;
David Chinner2f926582005-09-05 08:33:35 +1000506 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000507
508 trace_xfs_buf_find(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100509 XFS_STATS_INC(xb_get_locked);
510 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
513/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100514 * Assembles a buffer covering the specified range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 * Storage in memory for all portions of the buffer will be allocated,
516 * although backing storage may not be.
517 */
518xfs_buf_t *
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000519xfs_buf_get(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 xfs_buftarg_t *target,/* target for buffer */
Nathan Scott204ab252006-01-11 20:50:22 +1100521 xfs_off_t ioff, /* starting offset of range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 size_t isize, /* length of range */
Nathan Scottce8e9222006-01-11 15:39:08 +1100523 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
Nathan Scottce8e9222006-01-11 15:39:08 +1100525 xfs_buf_t *bp, *new_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 int error = 0, i;
527
Nathan Scottce8e9222006-01-11 15:39:08 +1100528 new_bp = xfs_buf_allocate(flags);
529 if (unlikely(!new_bp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return NULL;
531
Nathan Scottce8e9222006-01-11 15:39:08 +1100532 bp = _xfs_buf_find(target, ioff, isize, flags, new_bp);
533 if (bp == new_bp) {
534 error = _xfs_buf_lookup_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (error)
536 goto no_buffer;
537 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100538 xfs_buf_deallocate(new_bp);
539 if (unlikely(bp == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return NULL;
541 }
542
Nathan Scottce8e9222006-01-11 15:39:08 +1100543 for (i = 0; i < bp->b_page_count; i++)
544 mark_page_accessed(bp->b_pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Nathan Scottce8e9222006-01-11 15:39:08 +1100546 if (!(bp->b_flags & XBF_MAPPED)) {
547 error = _xfs_buf_map_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (unlikely(error)) {
549 printk(KERN_WARNING "%s: failed to map pages\n",
Harvey Harrison34a622b2008-04-10 12:19:21 +1000550 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 goto no_buffer;
552 }
553 }
554
Nathan Scottce8e9222006-01-11 15:39:08 +1100555 XFS_STATS_INC(xb_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 /*
558 * Always fill in the block number now, the mapped cases can do
559 * their own overlay of this later.
560 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100561 bp->b_bn = ioff;
562 bp->b_count_desired = bp->b_buffer_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000564 trace_xfs_buf_get(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100565 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100568 if (flags & (XBF_LOCK | XBF_TRYLOCK))
569 xfs_buf_unlock(bp);
570 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return NULL;
572}
573
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100574STATIC int
575_xfs_buf_read(
576 xfs_buf_t *bp,
577 xfs_buf_flags_t flags)
578{
579 int status;
580
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100581 ASSERT(!(flags & (XBF_DELWRI|XBF_WRITE)));
582 ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL);
583
584 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_DELWRI | \
585 XBF_READ_AHEAD | _XBF_RUN_QUEUES);
586 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | \
587 XBF_READ_AHEAD | _XBF_RUN_QUEUES);
588
589 status = xfs_buf_iorequest(bp);
Dave Chinnerec53d1d2010-07-20 17:52:59 +1000590 if (status || XFS_BUF_ISERROR(bp) || (flags & XBF_ASYNC))
591 return status;
592 return xfs_buf_iowait(bp);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100593}
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595xfs_buf_t *
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000596xfs_buf_read(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100598 xfs_off_t ioff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 size_t isize,
Nathan Scottce8e9222006-01-11 15:39:08 +1100600 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Nathan Scottce8e9222006-01-11 15:39:08 +1100602 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Nathan Scottce8e9222006-01-11 15:39:08 +1100604 flags |= XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000606 bp = xfs_buf_get(target, ioff, isize, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100607 if (bp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000608 trace_xfs_buf_read(bp, flags, _RET_IP_);
609
Nathan Scottce8e9222006-01-11 15:39:08 +1100610 if (!XFS_BUF_ISDONE(bp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100611 XFS_STATS_INC(xb_get_read);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100612 _xfs_buf_read(bp, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100613 } else if (flags & XBF_ASYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 /*
615 * Read ahead call which is already satisfied,
616 * drop the buffer
617 */
618 goto no_buffer;
619 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 /* We do not want read in the flags */
Nathan Scottce8e9222006-01-11 15:39:08 +1100621 bp->b_flags &= ~XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
623 }
624
Nathan Scottce8e9222006-01-11 15:39:08 +1100625 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100628 if (flags & (XBF_LOCK | XBF_TRYLOCK))
629 xfs_buf_unlock(bp);
630 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return NULL;
632}
633
634/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100635 * If we are not low on memory then do the readahead in a deadlock
636 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 */
638void
Nathan Scottce8e9222006-01-11 15:39:08 +1100639xfs_buf_readahead(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100641 xfs_off_t ioff,
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000642 size_t isize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
644 struct backing_dev_info *bdi;
645
Nathan Scottce8e9222006-01-11 15:39:08 +1100646 bdi = target->bt_mapping->backing_dev_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (bdi_read_congested(bdi))
648 return;
649
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000650 xfs_buf_read(target, ioff, isize,
651 XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD|XBF_DONT_BLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
Dave Chinner5adc94c2010-09-24 21:58:31 +1000654/*
655 * Read an uncached buffer from disk. Allocates and returns a locked
656 * buffer containing the disk contents or nothing.
657 */
658struct xfs_buf *
659xfs_buf_read_uncached(
660 struct xfs_mount *mp,
661 struct xfs_buftarg *target,
662 xfs_daddr_t daddr,
663 size_t length,
664 int flags)
665{
666 xfs_buf_t *bp;
667 int error;
668
669 bp = xfs_buf_get_uncached(target, length, flags);
670 if (!bp)
671 return NULL;
672
673 /* set up the buffer for a read IO */
674 xfs_buf_lock(bp);
675 XFS_BUF_SET_ADDR(bp, daddr);
676 XFS_BUF_READ(bp);
677 XFS_BUF_BUSY(bp);
678
679 xfsbdstrat(mp, bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000680 error = xfs_buf_iowait(bp);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000681 if (error || bp->b_error) {
682 xfs_buf_relse(bp);
683 return NULL;
684 }
685 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
688xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100689xfs_buf_get_empty(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 size_t len,
691 xfs_buftarg_t *target)
692{
Nathan Scottce8e9222006-01-11 15:39:08 +1100693 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Nathan Scottce8e9222006-01-11 15:39:08 +1100695 bp = xfs_buf_allocate(0);
696 if (bp)
697 _xfs_buf_initialize(bp, target, 0, len, 0);
698 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
701static inline struct page *
702mem_to_page(
703 void *addr)
704{
Christoph Lameter9e2779f2008-02-04 22:28:34 -0800705 if ((!is_vmalloc_addr(addr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return virt_to_page(addr);
707 } else {
708 return vmalloc_to_page(addr);
709 }
710}
711
712int
Nathan Scottce8e9222006-01-11 15:39:08 +1100713xfs_buf_associate_memory(
714 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 void *mem,
716 size_t len)
717{
718 int rval;
719 int i = 0;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100720 unsigned long pageaddr;
721 unsigned long offset;
722 size_t buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 int page_count;
724
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100725 pageaddr = (unsigned long)mem & PAGE_CACHE_MASK;
726 offset = (unsigned long)mem - pageaddr;
727 buflen = PAGE_CACHE_ALIGN(len + offset);
728 page_count = buflen >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 /* Free any previous set of page pointers */
Nathan Scottce8e9222006-01-11 15:39:08 +1100731 if (bp->b_pages)
732 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Nathan Scottce8e9222006-01-11 15:39:08 +1100734 bp->b_pages = NULL;
735 bp->b_addr = mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Christoph Hellwig36fae172009-07-18 18:14:58 -0400737 rval = _xfs_buf_get_pages(bp, page_count, XBF_DONT_BLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 if (rval)
739 return rval;
740
Nathan Scottce8e9222006-01-11 15:39:08 +1100741 bp->b_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100743 for (i = 0; i < bp->b_page_count; i++) {
744 bp->b_pages[i] = mem_to_page((void *)pageaddr);
745 pageaddr += PAGE_CACHE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100748 bp->b_count_desired = len;
749 bp->b_buffer_length = buflen;
Nathan Scottce8e9222006-01-11 15:39:08 +1100750 bp->b_flags |= XBF_MAPPED;
Christoph Hellwig6ab455e2008-05-19 16:34:42 +1000751 bp->b_flags &= ~_XBF_PAGE_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 return 0;
754}
755
756xfs_buf_t *
Dave Chinner686865f2010-09-24 20:07:47 +1000757xfs_buf_get_uncached(
758 struct xfs_buftarg *target,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 size_t len,
Dave Chinner686865f2010-09-24 20:07:47 +1000760 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000762 unsigned long page_count = PAGE_ALIGN(len) >> PAGE_SHIFT;
763 int error, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Nathan Scottce8e9222006-01-11 15:39:08 +1100766 bp = xfs_buf_allocate(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (unlikely(bp == NULL))
768 goto fail;
Nathan Scottce8e9222006-01-11 15:39:08 +1100769 _xfs_buf_initialize(bp, target, 0, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000771 error = _xfs_buf_get_pages(bp, page_count, 0);
772 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 goto fail_free_buf;
774
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000775 for (i = 0; i < page_count; i++) {
Dave Chinner686865f2010-09-24 20:07:47 +1000776 bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000777 if (!bp->b_pages[i])
778 goto fail_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000780 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000782 error = _xfs_buf_map_pages(bp, XBF_MAPPED);
783 if (unlikely(error)) {
784 printk(KERN_WARNING "%s: failed to map pages\n",
Harvey Harrison34a622b2008-04-10 12:19:21 +1000785 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 goto fail_free_mem;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Nathan Scottce8e9222006-01-11 15:39:08 +1100789 xfs_buf_unlock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Dave Chinner686865f2010-09-24 20:07:47 +1000791 trace_xfs_buf_get_uncached(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return bp;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 fail_free_mem:
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000795 while (--i >= 0)
796 __free_page(bp->b_pages[i]);
Christoph Hellwigca165b82007-05-24 15:21:11 +1000797 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 fail_free_buf:
Christoph Hellwigca165b82007-05-24 15:21:11 +1000799 xfs_buf_deallocate(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 fail:
801 return NULL;
802}
803
804/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 * Increment reference count on buffer, to hold the buffer concurrently
806 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 * Must hold the buffer already to call this function.
808 */
809void
Nathan Scottce8e9222006-01-11 15:39:08 +1100810xfs_buf_hold(
811 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000813 trace_xfs_buf_hold(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100814 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815}
816
817/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100818 * Releases a hold on the specified buffer. If the
819 * the hold count is 1, calls xfs_buf_free.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 */
821void
Nathan Scottce8e9222006-01-11 15:39:08 +1100822xfs_buf_rele(
823 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Dave Chinner74f75a02010-09-24 19:59:04 +1000825 struct xfs_perag *pag = bp->b_pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000827 trace_xfs_buf_rele(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Dave Chinner74f75a02010-09-24 19:59:04 +1000829 if (!pag) {
Nathan Scottfad3aa12006-02-01 12:14:52 +1100830 ASSERT(!bp->b_relse);
Dave Chinner74f75a02010-09-24 19:59:04 +1000831 ASSERT(RB_EMPTY_NODE(&bp->b_rbnode));
Nathan Scottfad3aa12006-02-01 12:14:52 +1100832 if (atomic_dec_and_test(&bp->b_hold))
833 xfs_buf_free(bp);
834 return;
835 }
836
Dave Chinner74f75a02010-09-24 19:59:04 +1000837 ASSERT(!RB_EMPTY_NODE(&bp->b_rbnode));
Lachlan McIlroy37906892008-08-13 15:42:10 +1000838 ASSERT(atomic_read(&bp->b_hold) > 0);
Dave Chinner74f75a02010-09-24 19:59:04 +1000839 if (atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100840 if (bp->b_relse) {
841 atomic_inc(&bp->b_hold);
Dave Chinner74f75a02010-09-24 19:59:04 +1000842 spin_unlock(&pag->pag_buf_lock);
843 bp->b_relse(bp);
Christoph Hellwig7f14d0a2005-11-02 15:09:35 +1100844 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100845 ASSERT(!(bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)));
Dave Chinner74f75a02010-09-24 19:59:04 +1000846 rb_erase(&bp->b_rbnode, &pag->pag_buf_tree);
847 spin_unlock(&pag->pag_buf_lock);
848 xfs_perag_put(pag);
Nathan Scottce8e9222006-01-11 15:39:08 +1100849 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 }
851 }
852}
853
854
855/*
856 * Mutual exclusion on buffers. Locking model:
857 *
858 * Buffers associated with inodes for which buffer locking
859 * is not enabled are not protected by semaphores, and are
860 * assumed to be exclusively owned by the caller. There is a
861 * spinlock in the buffer, used by the caller when concurrent
862 * access is possible.
863 */
864
865/*
Dave Chinner90810b92010-11-30 15:16:16 +1100866 * Locks a buffer object, if it is not already locked. Note that this in
867 * no way locks the underlying pages, so it is only useful for
868 * synchronizing concurrent use of buffer objects, not for synchronizing
869 * independent access to the underlying pages.
870 *
871 * If we come across a stale, pinned, locked buffer, we know that we are
872 * being asked to lock a buffer that has been reallocated. Because it is
873 * pinned, we know that the log has not been pushed to disk and hence it
874 * will still be locked. Rather than continuing to have trylock attempts
875 * fail until someone else pushes the log, push it ourselves before
876 * returning. This means that the xfsaild will not get stuck trying
877 * to push on stale inode buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 */
879int
Nathan Scottce8e9222006-01-11 15:39:08 +1100880xfs_buf_cond_lock(
881 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
883 int locked;
884
Nathan Scottce8e9222006-01-11 15:39:08 +1100885 locked = down_trylock(&bp->b_sema) == 0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000886 if (locked)
Nathan Scottce8e9222006-01-11 15:39:08 +1100887 XB_SET_OWNER(bp);
Dave Chinner90810b92010-11-30 15:16:16 +1100888 else if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
889 xfs_log_force(bp->b_target->bt_mount, 0);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000890
891 trace_xfs_buf_cond_lock(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100892 return locked ? 0 : -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895int
Nathan Scottce8e9222006-01-11 15:39:08 +1100896xfs_buf_lock_value(
897 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Stephen Rothwelladaa6932008-04-22 15:26:13 +1000899 return bp->b_sema.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100903 * Locks a buffer object.
904 * Note that this in no way locks the underlying pages, so it is only
905 * useful for synchronizing concurrent use of buffer objects, not for
906 * synchronizing independent access to the underlying pages.
Dave Chinnered3b4d62010-05-21 12:07:08 +1000907 *
908 * If we come across a stale, pinned, locked buffer, we know that we
909 * are being asked to lock a buffer that has been reallocated. Because
910 * it is pinned, we know that the log has not been pushed to disk and
911 * hence it will still be locked. Rather than sleeping until someone
912 * else pushes the log, push it ourselves before trying to get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100914void
915xfs_buf_lock(
916 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000918 trace_xfs_buf_lock(bp, _RET_IP_);
919
Dave Chinnered3b4d62010-05-21 12:07:08 +1000920 if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
Dave Chinnerebad8612010-09-22 10:47:20 +1000921 xfs_log_force(bp->b_target->bt_mount, 0);
Nathan Scottce8e9222006-01-11 15:39:08 +1100922 if (atomic_read(&bp->b_io_remaining))
923 blk_run_address_space(bp->b_target->bt_mapping);
924 down(&bp->b_sema);
925 XB_SET_OWNER(bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000926
927 trace_xfs_buf_lock_done(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928}
929
930/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100931 * Releases the lock on the buffer object.
David Chinner2f926582005-09-05 08:33:35 +1000932 * If the buffer is marked delwri but is not queued, do so before we
Nathan Scottce8e9222006-01-11 15:39:08 +1100933 * unlock the buffer as we need to set flags correctly. We also need to
David Chinner2f926582005-09-05 08:33:35 +1000934 * take a reference for the delwri queue because the unlocker is going to
935 * drop their's and they don't know we just queued it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 */
937void
Nathan Scottce8e9222006-01-11 15:39:08 +1100938xfs_buf_unlock(
939 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
Nathan Scottce8e9222006-01-11 15:39:08 +1100941 if ((bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)) == XBF_DELWRI) {
942 atomic_inc(&bp->b_hold);
943 bp->b_flags |= XBF_ASYNC;
944 xfs_buf_delwri_queue(bp, 0);
David Chinner2f926582005-09-05 08:33:35 +1000945 }
946
Nathan Scottce8e9222006-01-11 15:39:08 +1100947 XB_CLEAR_OWNER(bp);
948 up(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000949
950 trace_xfs_buf_unlock(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952
Nathan Scottce8e9222006-01-11 15:39:08 +1100953STATIC void
954xfs_buf_wait_unpin(
955 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
957 DECLARE_WAITQUEUE (wait, current);
958
Nathan Scottce8e9222006-01-11 15:39:08 +1100959 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return;
961
Nathan Scottce8e9222006-01-11 15:39:08 +1100962 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 for (;;) {
964 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +1100965 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 break;
Nathan Scottce8e9222006-01-11 15:39:08 +1100967 if (atomic_read(&bp->b_io_remaining))
968 blk_run_address_space(bp->b_target->bt_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 schedule();
970 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100971 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 set_current_state(TASK_RUNNING);
973}
974
975/*
976 * Buffer Utility Routines
977 */
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100980xfs_buf_iodone_work(
David Howellsc4028952006-11-22 14:57:56 +0000981 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
David Howellsc4028952006-11-22 14:57:56 +0000983 xfs_buf_t *bp =
984 container_of(work, xfs_buf_t, b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Christoph Hellwig80f6c292010-08-18 05:29:11 -0400986 if (bp->b_iodone)
Nathan Scottce8e9222006-01-11 15:39:08 +1100987 (*(bp->b_iodone))(bp);
988 else if (bp->b_flags & XBF_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 xfs_buf_relse(bp);
990}
991
992void
Nathan Scottce8e9222006-01-11 15:39:08 +1100993xfs_buf_ioend(
994 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 int schedule)
996{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000997 trace_xfs_buf_iodone(bp, _RET_IP_);
998
Lachlan McIlroy77be55a2007-11-23 16:31:00 +1100999 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
Nathan Scottce8e9222006-01-11 15:39:08 +11001000 if (bp->b_error == 0)
1001 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Nathan Scottce8e9222006-01-11 15:39:08 +11001003 if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (schedule) {
David Howellsc4028952006-11-22 14:57:56 +00001005 INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
Nathan Scottce8e9222006-01-11 15:39:08 +11001006 queue_work(xfslogd_workqueue, &bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 } else {
David Howellsc4028952006-11-22 14:57:56 +00001008 xfs_buf_iodone_work(&bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
1010 } else {
David Chinnerb4dd3302008-08-13 16:36:11 +10001011 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
1013}
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015void
Nathan Scottce8e9222006-01-11 15:39:08 +11001016xfs_buf_ioerror(
1017 xfs_buf_t *bp,
1018 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
1020 ASSERT(error >= 0 && error <= 0xffff);
Nathan Scottce8e9222006-01-11 15:39:08 +11001021 bp->b_error = (unsigned short)error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001022 trace_xfs_buf_ioerror(bp, error, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025int
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001026xfs_bwrite(
1027 struct xfs_mount *mp,
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001028 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
Christoph Hellwig8c383662010-03-12 10:59:40 +00001030 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001032 bp->b_flags |= XBF_WRITE;
Christoph Hellwig8c383662010-03-12 10:59:40 +00001033 bp->b_flags &= ~(XBF_ASYNC | XBF_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001035 xfs_buf_delwri_dequeue(bp);
Christoph Hellwig939d7232010-07-20 17:51:16 +10001036 xfs_bdstrat_cb(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Christoph Hellwig8c383662010-03-12 10:59:40 +00001038 error = xfs_buf_iowait(bp);
1039 if (error)
1040 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1041 xfs_buf_relse(bp);
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001042 return error;
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001043}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001045void
1046xfs_bdwrite(
1047 void *mp,
1048 struct xfs_buf *bp)
1049{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001050 trace_xfs_buf_bdwrite(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001052 bp->b_flags &= ~XBF_READ;
1053 bp->b_flags |= (XBF_DELWRI | XBF_ASYNC);
1054
1055 xfs_buf_delwri_queue(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056}
1057
Christoph Hellwig4e234712010-01-13 22:17:56 +00001058/*
1059 * Called when we want to stop a buffer from getting written or read.
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001060 * We attach the EIO error, muck with its flags, and call xfs_buf_ioend
Christoph Hellwig4e234712010-01-13 22:17:56 +00001061 * so that the proper iodone callbacks get called.
1062 */
1063STATIC int
1064xfs_bioerror(
1065 xfs_buf_t *bp)
1066{
1067#ifdef XFSERRORDEBUG
1068 ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
1069#endif
1070
1071 /*
1072 * No need to wait until the buffer is unpinned, we aren't flushing it.
1073 */
1074 XFS_BUF_ERROR(bp, EIO);
1075
1076 /*
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001077 * We're calling xfs_buf_ioend, so delete XBF_DONE flag.
Christoph Hellwig4e234712010-01-13 22:17:56 +00001078 */
1079 XFS_BUF_UNREAD(bp);
1080 XFS_BUF_UNDELAYWRITE(bp);
1081 XFS_BUF_UNDONE(bp);
1082 XFS_BUF_STALE(bp);
1083
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001084 xfs_buf_ioend(bp, 0);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001085
1086 return EIO;
1087}
1088
1089/*
1090 * Same as xfs_bioerror, except that we are releasing the buffer
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001091 * here ourselves, and avoiding the xfs_buf_ioend call.
Christoph Hellwig4e234712010-01-13 22:17:56 +00001092 * This is meant for userdata errors; metadata bufs come with
1093 * iodone functions attached, so that we can track down errors.
1094 */
1095STATIC int
1096xfs_bioerror_relse(
1097 struct xfs_buf *bp)
1098{
1099 int64_t fl = XFS_BUF_BFLAGS(bp);
1100 /*
1101 * No need to wait until the buffer is unpinned.
1102 * We aren't flushing it.
1103 *
1104 * chunkhold expects B_DONE to be set, whether
1105 * we actually finish the I/O or not. We don't want to
1106 * change that interface.
1107 */
1108 XFS_BUF_UNREAD(bp);
1109 XFS_BUF_UNDELAYWRITE(bp);
1110 XFS_BUF_DONE(bp);
1111 XFS_BUF_STALE(bp);
1112 XFS_BUF_CLR_IODONE_FUNC(bp);
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001113 if (!(fl & XBF_ASYNC)) {
Christoph Hellwig4e234712010-01-13 22:17:56 +00001114 /*
1115 * Mark b_error and B_ERROR _both_.
1116 * Lot's of chunkcache code assumes that.
1117 * There's no reason to mark error for
1118 * ASYNC buffers.
1119 */
1120 XFS_BUF_ERROR(bp, EIO);
1121 XFS_BUF_FINISH_IOWAIT(bp);
1122 } else {
1123 xfs_buf_relse(bp);
1124 }
1125
1126 return EIO;
1127}
1128
1129
1130/*
1131 * All xfs metadata buffers except log state machine buffers
1132 * get this attached as their b_bdstrat callback function.
1133 * This is so that we can catch a buffer
1134 * after prematurely unpinning it to forcibly shutdown the filesystem.
1135 */
1136int
1137xfs_bdstrat_cb(
1138 struct xfs_buf *bp)
1139{
Dave Chinnerebad8612010-09-22 10:47:20 +10001140 if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
Christoph Hellwig4e234712010-01-13 22:17:56 +00001141 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1142 /*
1143 * Metadata write that didn't get logged but
1144 * written delayed anyway. These aren't associated
1145 * with a transaction, and can be ignored.
1146 */
1147 if (!bp->b_iodone && !XFS_BUF_ISREAD(bp))
1148 return xfs_bioerror_relse(bp);
1149 else
1150 return xfs_bioerror(bp);
1151 }
1152
1153 xfs_buf_iorequest(bp);
1154 return 0;
1155}
1156
1157/*
1158 * Wrapper around bdstrat so that we can stop data from going to disk in case
1159 * we are shutting down the filesystem. Typically user data goes thru this
1160 * path; one of the exceptions is the superblock.
1161 */
1162void
1163xfsbdstrat(
1164 struct xfs_mount *mp,
1165 struct xfs_buf *bp)
1166{
1167 if (XFS_FORCED_SHUTDOWN(mp)) {
1168 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1169 xfs_bioerror_relse(bp);
1170 return;
1171 }
1172
1173 xfs_buf_iorequest(bp);
1174}
1175
Christoph Hellwigb8f82a42009-11-14 16:17:22 +00001176STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001177_xfs_buf_ioend(
1178 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 int schedule)
1180{
Christoph Hellwig6ab455e2008-05-19 16:34:42 +10001181 if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
1182 bp->b_flags &= ~_XBF_PAGE_LOCKED;
Nathan Scottce8e9222006-01-11 15:39:08 +11001183 xfs_buf_ioend(bp, schedule);
Christoph Hellwig6ab455e2008-05-19 16:34:42 +10001184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185}
1186
Al Viro782e3b32007-10-12 07:17:47 +01001187STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001188xfs_buf_bio_end_io(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 struct bio *bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 int error)
1191{
Nathan Scottce8e9222006-01-11 15:39:08 +11001192 xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
1193 unsigned int blocksize = bp->b_target->bt_bsize;
Nathan Scotteedb5532005-09-02 16:39:56 +10001194 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Lachlan McIlroycfbe5262008-12-12 15:27:25 +11001196 xfs_buf_ioerror(bp, -error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
James Bottomley73c77e22010-01-25 11:42:24 -06001198 if (!error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
1199 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1200
Nathan Scotteedb5532005-09-02 16:39:56 +10001201 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 struct page *page = bvec->bv_page;
1203
Nathan Scott948ecdb2006-09-28 11:03:13 +10001204 ASSERT(!PagePrivate(page));
Nathan Scottce8e9222006-01-11 15:39:08 +11001205 if (unlikely(bp->b_error)) {
1206 if (bp->b_flags & XBF_READ)
Nathan Scotteedb5532005-09-02 16:39:56 +10001207 ClearPageUptodate(page);
Nathan Scottce8e9222006-01-11 15:39:08 +11001208 } else if (blocksize >= PAGE_CACHE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 SetPageUptodate(page);
1210 } else if (!PagePrivate(page) &&
Nathan Scottce8e9222006-01-11 15:39:08 +11001211 (bp->b_flags & _XBF_PAGE_CACHE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 set_page_region(page, bvec->bv_offset, bvec->bv_len);
1213 }
1214
Nathan Scotteedb5532005-09-02 16:39:56 +10001215 if (--bvec >= bio->bi_io_vec)
1216 prefetchw(&bvec->bv_page->flags);
Christoph Hellwig6ab455e2008-05-19 16:34:42 +10001217
1218 if (bp->b_flags & _XBF_PAGE_LOCKED)
1219 unlock_page(page);
Nathan Scotteedb5532005-09-02 16:39:56 +10001220 } while (bvec >= bio->bi_io_vec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Nathan Scottce8e9222006-01-11 15:39:08 +11001222 _xfs_buf_ioend(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
1226STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001227_xfs_buf_ioapply(
1228 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
Christoph Hellwiga9759f22007-12-07 14:07:08 +11001230 int rw, map_i, total_nr_pages, nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 struct bio *bio;
Nathan Scottce8e9222006-01-11 15:39:08 +11001232 int offset = bp->b_offset;
1233 int size = bp->b_count_desired;
1234 sector_t sector = bp->b_bn;
1235 unsigned int blocksize = bp->b_target->bt_bsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Nathan Scottce8e9222006-01-11 15:39:08 +11001237 total_nr_pages = bp->b_page_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 map_i = 0;
1239
Nathan Scottce8e9222006-01-11 15:39:08 +11001240 if (bp->b_flags & XBF_ORDERED) {
1241 ASSERT(!(bp->b_flags & XBF_READ));
Christoph Hellwig80f6c292010-08-18 05:29:11 -04001242 rw = WRITE_FLUSH_FUA;
Dave Chinner2ee1aba2009-11-24 18:03:15 +00001243 } else if (bp->b_flags & XBF_LOG_BUFFER) {
Nathan Scott51bdd702006-09-28 11:01:57 +10001244 ASSERT(!(bp->b_flags & XBF_READ_AHEAD));
1245 bp->b_flags &= ~_XBF_RUN_QUEUES;
1246 rw = (bp->b_flags & XBF_WRITE) ? WRITE_SYNC : READ_SYNC;
Dave Chinner2ee1aba2009-11-24 18:03:15 +00001247 } else if (bp->b_flags & _XBF_RUN_QUEUES) {
1248 ASSERT(!(bp->b_flags & XBF_READ_AHEAD));
1249 bp->b_flags &= ~_XBF_RUN_QUEUES;
1250 rw = (bp->b_flags & XBF_WRITE) ? WRITE_META : READ_META;
Nathan Scott51bdd702006-09-28 11:01:57 +10001251 } else {
1252 rw = (bp->b_flags & XBF_WRITE) ? WRITE :
1253 (bp->b_flags & XBF_READ_AHEAD) ? READA : READ;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001254 }
1255
Nathan Scottce8e9222006-01-11 15:39:08 +11001256 /* Special code path for reading a sub page size buffer in --
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 * we populate up the whole page, and hence the other metadata
1258 * in the same page. This optimization is only valid when the
Nathan Scottce8e9222006-01-11 15:39:08 +11001259 * filesystem block size is not smaller than the page size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001261 if ((bp->b_buffer_length < PAGE_CACHE_SIZE) &&
Christoph Hellwig6ab455e2008-05-19 16:34:42 +10001262 ((bp->b_flags & (XBF_READ|_XBF_PAGE_LOCKED)) ==
1263 (XBF_READ|_XBF_PAGE_LOCKED)) &&
Nathan Scottce8e9222006-01-11 15:39:08 +11001264 (blocksize >= PAGE_CACHE_SIZE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 bio = bio_alloc(GFP_NOIO, 1);
1266
Nathan Scottce8e9222006-01-11 15:39:08 +11001267 bio->bi_bdev = bp->b_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 bio->bi_sector = sector - (offset >> BBSHIFT);
Nathan Scottce8e9222006-01-11 15:39:08 +11001269 bio->bi_end_io = xfs_buf_bio_end_io;
1270 bio->bi_private = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Nathan Scottce8e9222006-01-11 15:39:08 +11001272 bio_add_page(bio, bp->b_pages[0], PAGE_CACHE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 size = 0;
1274
Nathan Scottce8e9222006-01-11 15:39:08 +11001275 atomic_inc(&bp->b_io_remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277 goto submit_io;
1278 }
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001281 atomic_inc(&bp->b_io_remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
1283 if (nr_pages > total_nr_pages)
1284 nr_pages = total_nr_pages;
1285
1286 bio = bio_alloc(GFP_NOIO, nr_pages);
Nathan Scottce8e9222006-01-11 15:39:08 +11001287 bio->bi_bdev = bp->b_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 bio->bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001289 bio->bi_end_io = xfs_buf_bio_end_io;
1290 bio->bi_private = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 for (; size && nr_pages; nr_pages--, map_i++) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001293 int rbytes, nbytes = PAGE_CACHE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 if (nbytes > size)
1296 nbytes = size;
1297
Nathan Scottce8e9222006-01-11 15:39:08 +11001298 rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset);
1299 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 break;
1301
1302 offset = 0;
1303 sector += nbytes >> BBSHIFT;
1304 size -= nbytes;
1305 total_nr_pages--;
1306 }
1307
1308submit_io:
1309 if (likely(bio->bi_size)) {
James Bottomley73c77e22010-01-25 11:42:24 -06001310 if (xfs_buf_is_vmapped(bp)) {
1311 flush_kernel_vmap_range(bp->b_addr,
1312 xfs_buf_vmap_len(bp));
1313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 submit_bio(rw, bio);
1315 if (size)
1316 goto next_chunk;
1317 } else {
Dave Chinnerec53d1d2010-07-20 17:52:59 +10001318 /*
1319 * if we get here, no pages were added to the bio. However,
1320 * we can't just error out here - if the pages are locked then
1321 * we have to unlock them otherwise we can hang on a later
1322 * access to the page.
1323 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001324 xfs_buf_ioerror(bp, EIO);
Dave Chinnerec53d1d2010-07-20 17:52:59 +10001325 if (bp->b_flags & _XBF_PAGE_LOCKED) {
1326 int i;
1327 for (i = 0; i < bp->b_page_count; i++)
1328 unlock_page(bp->b_pages[i]);
1329 }
1330 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
1332}
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334int
Nathan Scottce8e9222006-01-11 15:39:08 +11001335xfs_buf_iorequest(
1336 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001338 trace_xfs_buf_iorequest(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Nathan Scottce8e9222006-01-11 15:39:08 +11001340 if (bp->b_flags & XBF_DELWRI) {
1341 xfs_buf_delwri_queue(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return 0;
1343 }
1344
Nathan Scottce8e9222006-01-11 15:39:08 +11001345 if (bp->b_flags & XBF_WRITE) {
1346 xfs_buf_wait_unpin(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 }
1348
Nathan Scottce8e9222006-01-11 15:39:08 +11001349 xfs_buf_hold(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 /* Set the count to 1 initially, this will stop an I/O
1352 * completion callout which happens before we have started
Nathan Scottce8e9222006-01-11 15:39:08 +11001353 * all the I/O from calling xfs_buf_ioend too early.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001355 atomic_set(&bp->b_io_remaining, 1);
1356 _xfs_buf_ioapply(bp);
1357 _xfs_buf_ioend(bp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Nathan Scottce8e9222006-01-11 15:39:08 +11001359 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 return 0;
1361}
1362
1363/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001364 * Waits for I/O to complete on the buffer supplied.
1365 * It returns immediately if no I/O is pending.
1366 * It returns the I/O error code, if any, or 0 if there was no error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 */
1368int
Nathan Scottce8e9222006-01-11 15:39:08 +11001369xfs_buf_iowait(
1370 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001372 trace_xfs_buf_iowait(bp, _RET_IP_);
1373
Nathan Scottce8e9222006-01-11 15:39:08 +11001374 if (atomic_read(&bp->b_io_remaining))
1375 blk_run_address_space(bp->b_target->bt_mapping);
David Chinnerb4dd3302008-08-13 16:36:11 +10001376 wait_for_completion(&bp->b_iowait);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001377
1378 trace_xfs_buf_iowait_done(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +11001379 return bp->b_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380}
1381
Nathan Scottce8e9222006-01-11 15:39:08 +11001382xfs_caddr_t
1383xfs_buf_offset(
1384 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 size_t offset)
1386{
1387 struct page *page;
1388
Nathan Scottce8e9222006-01-11 15:39:08 +11001389 if (bp->b_flags & XBF_MAPPED)
1390 return XFS_BUF_PTR(bp) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Nathan Scottce8e9222006-01-11 15:39:08 +11001392 offset += bp->b_offset;
1393 page = bp->b_pages[offset >> PAGE_CACHE_SHIFT];
1394 return (xfs_caddr_t)page_address(page) + (offset & (PAGE_CACHE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395}
1396
1397/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 * Move data into or out of a buffer.
1399 */
1400void
Nathan Scottce8e9222006-01-11 15:39:08 +11001401xfs_buf_iomove(
1402 xfs_buf_t *bp, /* buffer to process */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 size_t boff, /* starting buffer offset */
1404 size_t bsize, /* length to copy */
Dave Chinnerb9c48642010-01-20 10:47:39 +11001405 void *data, /* data address */
Nathan Scottce8e9222006-01-11 15:39:08 +11001406 xfs_buf_rw_t mode) /* read/write/zero flag */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
1408 size_t bend, cpoff, csize;
1409 struct page *page;
1410
1411 bend = boff + bsize;
1412 while (boff < bend) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001413 page = bp->b_pages[xfs_buf_btoct(boff + bp->b_offset)];
1414 cpoff = xfs_buf_poff(boff + bp->b_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 csize = min_t(size_t,
Nathan Scottce8e9222006-01-11 15:39:08 +11001416 PAGE_CACHE_SIZE-cpoff, bp->b_count_desired-boff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 ASSERT(((csize + cpoff) <= PAGE_CACHE_SIZE));
1419
1420 switch (mode) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001421 case XBRW_ZERO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 memset(page_address(page) + cpoff, 0, csize);
1423 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001424 case XBRW_READ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 memcpy(data, page_address(page) + cpoff, csize);
1426 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001427 case XBRW_WRITE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 memcpy(page_address(page) + cpoff, data, csize);
1429 }
1430
1431 boff += csize;
1432 data += csize;
1433 }
1434}
1435
1436/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001437 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 */
1439
1440/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001441 * Wait for any bufs with callbacks that have been submitted but
1442 * have not yet returned... walk the hash list for the target.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 */
1444void
1445xfs_wait_buftarg(
Dave Chinner74f75a02010-09-24 19:59:04 +10001446 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
Dave Chinner74f75a02010-09-24 19:59:04 +10001448 struct xfs_perag *pag;
1449 uint i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Dave Chinner74f75a02010-09-24 19:59:04 +10001451 for (i = 0; i < btp->bt_mount->m_sb.sb_agcount; i++) {
1452 pag = xfs_perag_get(btp->bt_mount, i);
1453 spin_lock(&pag->pag_buf_lock);
1454 while (rb_first(&pag->pag_buf_tree)) {
1455 spin_unlock(&pag->pag_buf_lock);
Dave Chinner26af6552010-09-22 10:47:20 +10001456 delay(100);
Dave Chinner74f75a02010-09-24 19:59:04 +10001457 spin_lock(&pag->pag_buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 }
Dave Chinner74f75a02010-09-24 19:59:04 +10001459 spin_unlock(&pag->pag_buf_lock);
1460 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 }
1462}
1463
1464/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001465 * buftarg list for delwrite queue processing
David Chinnera6867a62006-01-11 15:37:58 +11001466 */
Tim Shimmine6a0e9c2007-05-08 13:49:59 +10001467static LIST_HEAD(xfs_buftarg_list);
David Chinner7989cb82007-02-10 18:34:56 +11001468static DEFINE_SPINLOCK(xfs_buftarg_lock);
David Chinnera6867a62006-01-11 15:37:58 +11001469
1470STATIC void
1471xfs_register_buftarg(
1472 xfs_buftarg_t *btp)
1473{
1474 spin_lock(&xfs_buftarg_lock);
1475 list_add(&btp->bt_list, &xfs_buftarg_list);
1476 spin_unlock(&xfs_buftarg_lock);
1477}
1478
1479STATIC void
1480xfs_unregister_buftarg(
1481 xfs_buftarg_t *btp)
1482{
1483 spin_lock(&xfs_buftarg_lock);
1484 list_del(&btp->bt_list);
1485 spin_unlock(&xfs_buftarg_lock);
1486}
1487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488void
1489xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001490 struct xfs_mount *mp,
1491 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
1493 xfs_flush_buftarg(btp, 1);
Christoph Hellwigb7963132009-03-03 14:48:37 -05001494 if (mp->m_flags & XFS_MOUNT_BARRIER)
1495 xfs_blkdev_issue_flush(btp);
Nathan Scottce8e9222006-01-11 15:39:08 +11001496 iput(btp->bt_mapping->host);
David Chinnera6867a62006-01-11 15:37:58 +11001497
Nathan Scottce8e9222006-01-11 15:39:08 +11001498 /* Unregister the buftarg first so that we don't get a
1499 * wakeup finding a non-existent task
1500 */
David Chinnera6867a62006-01-11 15:37:58 +11001501 xfs_unregister_buftarg(btp);
1502 kthread_stop(btp->bt_task);
1503
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001504 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505}
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507STATIC int
1508xfs_setsize_buftarg_flags(
1509 xfs_buftarg_t *btp,
1510 unsigned int blocksize,
1511 unsigned int sectorsize,
1512 int verbose)
1513{
Nathan Scottce8e9222006-01-11 15:39:08 +11001514 btp->bt_bsize = blocksize;
1515 btp->bt_sshift = ffs(sectorsize) - 1;
1516 btp->bt_smask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Nathan Scottce8e9222006-01-11 15:39:08 +11001518 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 printk(KERN_WARNING
1520 "XFS: Cannot set_blocksize to %u on device %s\n",
1521 sectorsize, XFS_BUFTARG_NAME(btp));
1522 return EINVAL;
1523 }
1524
1525 if (verbose &&
1526 (PAGE_CACHE_SIZE / BITS_PER_LONG) > sectorsize) {
1527 printk(KERN_WARNING
1528 "XFS: %u byte sectors in use on device %s. "
1529 "This is suboptimal; %u or greater is ideal.\n",
1530 sectorsize, XFS_BUFTARG_NAME(btp),
1531 (unsigned int)PAGE_CACHE_SIZE / BITS_PER_LONG);
1532 }
1533
1534 return 0;
1535}
1536
1537/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001538 * When allocating the initial buffer target we have not yet
1539 * read in the superblock, so don't know what sized sectors
1540 * are being used is at this early stage. Play safe.
1541 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542STATIC int
1543xfs_setsize_buftarg_early(
1544 xfs_buftarg_t *btp,
1545 struct block_device *bdev)
1546{
1547 return xfs_setsize_buftarg_flags(btp,
Martin K. Petersene1defc42009-05-22 17:17:49 -04001548 PAGE_CACHE_SIZE, bdev_logical_block_size(bdev), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549}
1550
1551int
1552xfs_setsize_buftarg(
1553 xfs_buftarg_t *btp,
1554 unsigned int blocksize,
1555 unsigned int sectorsize)
1556{
1557 return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
1558}
1559
1560STATIC int
1561xfs_mapping_buftarg(
1562 xfs_buftarg_t *btp,
1563 struct block_device *bdev)
1564{
1565 struct backing_dev_info *bdi;
1566 struct inode *inode;
1567 struct address_space *mapping;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07001568 static const struct address_space_operations mapping_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 .sync_page = block_sync_page,
Christoph Lametere965f962006-02-01 03:05:41 -08001570 .migratepage = fail_migrate_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 };
1572
1573 inode = new_inode(bdev->bd_inode->i_sb);
1574 if (!inode) {
1575 printk(KERN_WARNING
1576 "XFS: Cannot allocate mapping inode for device %s\n",
1577 XFS_BUFTARG_NAME(btp));
1578 return ENOMEM;
1579 }
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001580 inode->i_ino = get_next_ino();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 inode->i_mode = S_IFBLK;
1582 inode->i_bdev = bdev;
1583 inode->i_rdev = bdev->bd_dev;
1584 bdi = blk_get_backing_dev_info(bdev);
1585 if (!bdi)
1586 bdi = &default_backing_dev_info;
1587 mapping = &inode->i_data;
1588 mapping->a_ops = &mapping_aops;
1589 mapping->backing_dev_info = bdi;
1590 mapping_set_gfp_mask(mapping, GFP_NOFS);
Nathan Scottce8e9222006-01-11 15:39:08 +11001591 btp->bt_mapping = mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 return 0;
1593}
1594
David Chinnera6867a62006-01-11 15:37:58 +11001595STATIC int
1596xfs_alloc_delwrite_queue(
Jan Engelhardte2a07812010-03-23 09:52:55 +11001597 xfs_buftarg_t *btp,
1598 const char *fsname)
David Chinnera6867a62006-01-11 15:37:58 +11001599{
1600 int error = 0;
1601
1602 INIT_LIST_HEAD(&btp->bt_list);
1603 INIT_LIST_HEAD(&btp->bt_delwrite_queue);
Eric Sandeen007c61c2007-10-11 17:43:56 +10001604 spin_lock_init(&btp->bt_delwrite_lock);
David Chinnera6867a62006-01-11 15:37:58 +11001605 btp->bt_flags = 0;
Jan Engelhardte2a07812010-03-23 09:52:55 +11001606 btp->bt_task = kthread_run(xfsbufd, btp, "xfsbufd/%s", fsname);
David Chinnera6867a62006-01-11 15:37:58 +11001607 if (IS_ERR(btp->bt_task)) {
1608 error = PTR_ERR(btp->bt_task);
1609 goto out_error;
1610 }
1611 xfs_register_buftarg(btp);
1612out_error:
1613 return error;
1614}
1615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616xfs_buftarg_t *
1617xfs_alloc_buftarg(
Dave Chinnerebad8612010-09-22 10:47:20 +10001618 struct xfs_mount *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 struct block_device *bdev,
Jan Engelhardte2a07812010-03-23 09:52:55 +11001620 int external,
1621 const char *fsname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
1623 xfs_buftarg_t *btp;
1624
1625 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
1626
Dave Chinnerebad8612010-09-22 10:47:20 +10001627 btp->bt_mount = mp;
Nathan Scottce8e9222006-01-11 15:39:08 +11001628 btp->bt_dev = bdev->bd_dev;
1629 btp->bt_bdev = bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 if (xfs_setsize_buftarg_early(btp, bdev))
1631 goto error;
1632 if (xfs_mapping_buftarg(btp, bdev))
1633 goto error;
Jan Engelhardte2a07812010-03-23 09:52:55 +11001634 if (xfs_alloc_delwrite_queue(btp, fsname))
David Chinnera6867a62006-01-11 15:37:58 +11001635 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 return btp;
1637
1638error:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001639 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 return NULL;
1641}
1642
1643
1644/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001645 * Delayed write buffer handling
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001648xfs_buf_delwri_queue(
1649 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 int unlock)
1651{
Nathan Scottce8e9222006-01-11 15:39:08 +11001652 struct list_head *dwq = &bp->b_target->bt_delwrite_queue;
1653 spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
David Chinnera6867a62006-01-11 15:37:58 +11001654
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001655 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
1656
Nathan Scottce8e9222006-01-11 15:39:08 +11001657 ASSERT((bp->b_flags&(XBF_DELWRI|XBF_ASYNC)) == (XBF_DELWRI|XBF_ASYNC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
David Chinnera6867a62006-01-11 15:37:58 +11001659 spin_lock(dwlk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 /* If already in the queue, dequeue and place at tail */
Nathan Scottce8e9222006-01-11 15:39:08 +11001661 if (!list_empty(&bp->b_list)) {
1662 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1663 if (unlock)
1664 atomic_dec(&bp->b_hold);
1665 list_del(&bp->b_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 }
1667
Dave Chinnerc9c12972010-01-11 11:49:59 +00001668 if (list_empty(dwq)) {
1669 /* start xfsbufd as it is about to have something to do */
1670 wake_up_process(bp->b_target->bt_task);
1671 }
1672
Nathan Scottce8e9222006-01-11 15:39:08 +11001673 bp->b_flags |= _XBF_DELWRI_Q;
1674 list_add_tail(&bp->b_list, dwq);
1675 bp->b_queuetime = jiffies;
David Chinnera6867a62006-01-11 15:37:58 +11001676 spin_unlock(dwlk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
1678 if (unlock)
Nathan Scottce8e9222006-01-11 15:39:08 +11001679 xfs_buf_unlock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680}
1681
1682void
Nathan Scottce8e9222006-01-11 15:39:08 +11001683xfs_buf_delwri_dequeue(
1684 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
Nathan Scottce8e9222006-01-11 15:39:08 +11001686 spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 int dequeued = 0;
1688
David Chinnera6867a62006-01-11 15:37:58 +11001689 spin_lock(dwlk);
Nathan Scottce8e9222006-01-11 15:39:08 +11001690 if ((bp->b_flags & XBF_DELWRI) && !list_empty(&bp->b_list)) {
1691 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1692 list_del_init(&bp->b_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 dequeued = 1;
1694 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001695 bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q);
David Chinnera6867a62006-01-11 15:37:58 +11001696 spin_unlock(dwlk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
1698 if (dequeued)
Nathan Scottce8e9222006-01-11 15:39:08 +11001699 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001701 trace_xfs_buf_delwri_dequeue(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702}
1703
Dave Chinnerd808f612010-02-02 10:13:42 +11001704/*
1705 * If a delwri buffer needs to be pushed before it has aged out, then promote
1706 * it to the head of the delwri queue so that it will be flushed on the next
1707 * xfsbufd run. We do this by resetting the queuetime of the buffer to be older
1708 * than the age currently needed to flush the buffer. Hence the next time the
1709 * xfsbufd sees it is guaranteed to be considered old enough to flush.
1710 */
1711void
1712xfs_buf_delwri_promote(
1713 struct xfs_buf *bp)
1714{
1715 struct xfs_buftarg *btp = bp->b_target;
1716 long age = xfs_buf_age_centisecs * msecs_to_jiffies(10) + 1;
1717
1718 ASSERT(bp->b_flags & XBF_DELWRI);
1719 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1720
1721 /*
1722 * Check the buffer age before locking the delayed write queue as we
1723 * don't need to promote buffers that are already past the flush age.
1724 */
1725 if (bp->b_queuetime < jiffies - age)
1726 return;
1727 bp->b_queuetime = jiffies - age;
1728 spin_lock(&btp->bt_delwrite_lock);
1729 list_move(&bp->b_list, &btp->bt_delwrite_queue);
1730 spin_unlock(&btp->bt_delwrite_lock);
1731}
1732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001734xfs_buf_runall_queues(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 struct workqueue_struct *queue)
1736{
1737 flush_workqueue(queue);
1738}
1739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740STATIC int
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001741xfsbufd_wakeup(
Dave Chinner7f8275d2010-07-19 14:56:17 +10001742 struct shrinker *shrink,
Nathan Scott15c84a42005-11-04 10:51:01 +11001743 int priority,
1744 gfp_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745{
Christoph Hellwigda7f93e2006-01-11 20:49:57 +11001746 xfs_buftarg_t *btp;
David Chinnera6867a62006-01-11 15:37:58 +11001747
1748 spin_lock(&xfs_buftarg_lock);
Christoph Hellwigda7f93e2006-01-11 20:49:57 +11001749 list_for_each_entry(btp, &xfs_buftarg_list, bt_list) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001750 if (test_bit(XBT_FORCE_SLEEP, &btp->bt_flags))
David Chinnera6867a62006-01-11 15:37:58 +11001751 continue;
Dave Chinnerc9c12972010-01-11 11:49:59 +00001752 if (list_empty(&btp->bt_delwrite_queue))
1753 continue;
Nathan Scottce8e9222006-01-11 15:39:08 +11001754 set_bit(XBT_FORCE_FLUSH, &btp->bt_flags);
David Chinnera6867a62006-01-11 15:37:58 +11001755 wake_up_process(btp->bt_task);
1756 }
1757 spin_unlock(&xfs_buftarg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 return 0;
1759}
1760
David Chinner585e6d82007-02-10 18:32:29 +11001761/*
1762 * Move as many buffers as specified to the supplied list
1763 * idicating if we skipped any buffers to prevent deadlocks.
1764 */
1765STATIC int
1766xfs_buf_delwri_split(
1767 xfs_buftarg_t *target,
1768 struct list_head *list,
David Chinner5e6a07d2007-02-10 18:34:49 +11001769 unsigned long age)
David Chinner585e6d82007-02-10 18:32:29 +11001770{
1771 xfs_buf_t *bp, *n;
1772 struct list_head *dwq = &target->bt_delwrite_queue;
1773 spinlock_t *dwlk = &target->bt_delwrite_lock;
1774 int skipped = 0;
David Chinner5e6a07d2007-02-10 18:34:49 +11001775 int force;
David Chinner585e6d82007-02-10 18:32:29 +11001776
David Chinner5e6a07d2007-02-10 18:34:49 +11001777 force = test_and_clear_bit(XBT_FORCE_FLUSH, &target->bt_flags);
David Chinner585e6d82007-02-10 18:32:29 +11001778 INIT_LIST_HEAD(list);
1779 spin_lock(dwlk);
1780 list_for_each_entry_safe(bp, n, dwq, b_list) {
David Chinner585e6d82007-02-10 18:32:29 +11001781 ASSERT(bp->b_flags & XBF_DELWRI);
1782
Christoph Hellwig4d16e922010-06-23 18:11:15 +10001783 if (!XFS_BUF_ISPINNED(bp) && !xfs_buf_cond_lock(bp)) {
David Chinner5e6a07d2007-02-10 18:34:49 +11001784 if (!force &&
David Chinner585e6d82007-02-10 18:32:29 +11001785 time_before(jiffies, bp->b_queuetime + age)) {
1786 xfs_buf_unlock(bp);
1787 break;
1788 }
1789
1790 bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q|
1791 _XBF_RUN_QUEUES);
1792 bp->b_flags |= XBF_WRITE;
1793 list_move_tail(&bp->b_list, list);
Dave Chinnerbfe27412010-11-08 08:55:05 +00001794 trace_xfs_buf_delwri_split(bp, _RET_IP_);
David Chinner585e6d82007-02-10 18:32:29 +11001795 } else
1796 skipped++;
1797 }
1798 spin_unlock(dwlk);
1799
1800 return skipped;
1801
1802}
1803
Dave Chinner089716a2010-01-26 15:13:25 +11001804/*
1805 * Compare function is more complex than it needs to be because
1806 * the return value is only 32 bits and we are doing comparisons
1807 * on 64 bit values
1808 */
1809static int
1810xfs_buf_cmp(
1811 void *priv,
1812 struct list_head *a,
1813 struct list_head *b)
1814{
1815 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
1816 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
1817 xfs_daddr_t diff;
1818
1819 diff = ap->b_bn - bp->b_bn;
1820 if (diff < 0)
1821 return -1;
1822 if (diff > 0)
1823 return 1;
1824 return 0;
1825}
1826
1827void
1828xfs_buf_delwri_sort(
1829 xfs_buftarg_t *target,
1830 struct list_head *list)
1831{
1832 list_sort(NULL, list, xfs_buf_cmp);
1833}
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835STATIC int
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001836xfsbufd(
David Chinner585e6d82007-02-10 18:32:29 +11001837 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
Dave Chinner089716a2010-01-26 15:13:25 +11001839 xfs_buftarg_t *target = (xfs_buftarg_t *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 current->flags |= PF_MEMALLOC;
1842
Rafael J. Wysocki978c7b22007-12-07 14:09:02 +11001843 set_freezable();
1844
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 do {
Dave Chinnerc9c12972010-01-11 11:49:59 +00001846 long age = xfs_buf_age_centisecs * msecs_to_jiffies(10);
1847 long tout = xfs_buf_timer_centisecs * msecs_to_jiffies(10);
Dave Chinner089716a2010-01-26 15:13:25 +11001848 int count = 0;
1849 struct list_head tmp;
Dave Chinnerc9c12972010-01-11 11:49:59 +00001850
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001851 if (unlikely(freezing(current))) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001852 set_bit(XBT_FORCE_SLEEP, &target->bt_flags);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001853 refrigerator();
Nathan Scottabd0cf72005-05-05 13:30:13 -07001854 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +11001855 clear_bit(XBT_FORCE_SLEEP, &target->bt_flags);
Nathan Scottabd0cf72005-05-05 13:30:13 -07001856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Dave Chinnerc9c12972010-01-11 11:49:59 +00001858 /* sleep for a long time if there is nothing to do. */
1859 if (list_empty(&target->bt_delwrite_queue))
1860 tout = MAX_SCHEDULE_TIMEOUT;
1861 schedule_timeout_interruptible(tout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Dave Chinnerc9c12972010-01-11 11:49:59 +00001863 xfs_buf_delwri_split(target, &tmp, age);
Dave Chinner089716a2010-01-26 15:13:25 +11001864 list_sort(NULL, &tmp, xfs_buf_cmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 while (!list_empty(&tmp)) {
Dave Chinner089716a2010-01-26 15:13:25 +11001866 struct xfs_buf *bp;
1867 bp = list_first_entry(&tmp, struct xfs_buf, b_list);
Nathan Scottce8e9222006-01-11 15:39:08 +11001868 list_del_init(&bp->b_list);
Christoph Hellwig939d7232010-07-20 17:51:16 +10001869 xfs_bdstrat_cb(bp);
David Chinner585e6d82007-02-10 18:32:29 +11001870 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 }
Nathan Scottf07c2252006-09-28 10:52:15 +10001872 if (count)
1873 blk_run_address_space(target->bt_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Christoph Hellwig4df08c52005-09-05 08:34:18 +10001875 } while (!kthread_should_stop());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Christoph Hellwig4df08c52005-09-05 08:34:18 +10001877 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878}
1879
1880/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001881 * Go through all incore buffers, and release buffers if they belong to
1882 * the given device. This is used in filesystem error handling to
1883 * preserve the consistency of its metadata.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 */
1885int
1886xfs_flush_buftarg(
David Chinner585e6d82007-02-10 18:32:29 +11001887 xfs_buftarg_t *target,
1888 int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
Dave Chinner089716a2010-01-26 15:13:25 +11001890 xfs_buf_t *bp;
David Chinner585e6d82007-02-10 18:32:29 +11001891 int pincount = 0;
Dave Chinner089716a2010-01-26 15:13:25 +11001892 LIST_HEAD(tmp_list);
1893 LIST_HEAD(wait_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Dave Chinnerc626d172009-04-06 18:42:11 +02001895 xfs_buf_runall_queues(xfsconvertd_workqueue);
Nathan Scottce8e9222006-01-11 15:39:08 +11001896 xfs_buf_runall_queues(xfsdatad_workqueue);
1897 xfs_buf_runall_queues(xfslogd_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
David Chinner5e6a07d2007-02-10 18:34:49 +11001899 set_bit(XBT_FORCE_FLUSH, &target->bt_flags);
Dave Chinner089716a2010-01-26 15:13:25 +11001900 pincount = xfs_buf_delwri_split(target, &tmp_list, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
1902 /*
Dave Chinner089716a2010-01-26 15:13:25 +11001903 * Dropped the delayed write list lock, now walk the temporary list.
1904 * All I/O is issued async and then if we need to wait for completion
1905 * we do that after issuing all the IO.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 */
Dave Chinner089716a2010-01-26 15:13:25 +11001907 list_sort(NULL, &tmp_list, xfs_buf_cmp);
1908 while (!list_empty(&tmp_list)) {
1909 bp = list_first_entry(&tmp_list, struct xfs_buf, b_list);
David Chinner585e6d82007-02-10 18:32:29 +11001910 ASSERT(target == bp->b_target);
Dave Chinner089716a2010-01-26 15:13:25 +11001911 list_del_init(&bp->b_list);
1912 if (wait) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001913 bp->b_flags &= ~XBF_ASYNC;
Dave Chinner089716a2010-01-26 15:13:25 +11001914 list_add(&bp->b_list, &wait_list);
1915 }
Christoph Hellwig939d7232010-07-20 17:51:16 +10001916 xfs_bdstrat_cb(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 }
1918
Dave Chinner089716a2010-01-26 15:13:25 +11001919 if (wait) {
1920 /* Expedite and wait for IO to complete. */
Nathan Scottf07c2252006-09-28 10:52:15 +10001921 blk_run_address_space(target->bt_mapping);
Dave Chinner089716a2010-01-26 15:13:25 +11001922 while (!list_empty(&wait_list)) {
1923 bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
Nathan Scottf07c2252006-09-28 10:52:15 +10001924
Dave Chinner089716a2010-01-26 15:13:25 +11001925 list_del_init(&bp->b_list);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001926 xfs_buf_iowait(bp);
Dave Chinner089716a2010-01-26 15:13:25 +11001927 xfs_buf_relse(bp);
1928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 }
1930
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 return pincount;
1932}
1933
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001934int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11001935xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
Nathan Scott87582802006-03-14 13:18:19 +11001937 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
1938 KM_ZONE_HWALIGN, NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11001939 if (!xfs_buf_zone)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001940 goto out;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001941
Dave Chinner51749e42010-09-08 09:00:22 +00001942 xfslogd_workqueue = alloc_workqueue("xfslogd",
Tejun Heo6370a6a2010-10-11 15:12:27 +02001943 WQ_MEM_RECLAIM | WQ_HIGHPRI, 1);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001944 if (!xfslogd_workqueue)
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001945 goto out_free_buf_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Rafael J. Wysockib4337692007-03-22 00:11:27 -08001947 xfsdatad_workqueue = create_workqueue("xfsdatad");
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001948 if (!xfsdatad_workqueue)
1949 goto out_destroy_xfslogd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Dave Chinnerc626d172009-04-06 18:42:11 +02001951 xfsconvertd_workqueue = create_workqueue("xfsconvertd");
1952 if (!xfsconvertd_workqueue)
1953 goto out_destroy_xfsdatad_workqueue;
1954
Rusty Russell8e1f9362007-07-17 04:03:17 -07001955 register_shrinker(&xfs_buf_shake);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001956 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Dave Chinnerc626d172009-04-06 18:42:11 +02001958 out_destroy_xfsdatad_workqueue:
1959 destroy_workqueue(xfsdatad_workqueue);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001960 out_destroy_xfslogd_workqueue:
1961 destroy_workqueue(xfslogd_workqueue);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001962 out_free_buf_zone:
Nathan Scottce8e9222006-01-11 15:39:08 +11001963 kmem_zone_destroy(xfs_buf_zone);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001964 out:
Nathan Scott87582802006-03-14 13:18:19 +11001965 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966}
1967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968void
Nathan Scottce8e9222006-01-11 15:39:08 +11001969xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
Rusty Russell8e1f9362007-07-17 04:03:17 -07001971 unregister_shrinker(&xfs_buf_shake);
Dave Chinnerc626d172009-04-06 18:42:11 +02001972 destroy_workqueue(xfsconvertd_workqueue);
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001973 destroy_workqueue(xfsdatad_workqueue);
1974 destroy_workqueue(xfslogd_workqueue);
Nathan Scottce8e9222006-01-11 15:39:08 +11001975 kmem_zone_destroy(xfs_buf_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976}
Tim Shimmine6a0e9c2007-05-08 13:49:59 +10001977
1978#ifdef CONFIG_KDB_MODULES
1979struct list_head *
1980xfs_get_buftarg_list(void)
1981{
1982 return &xfs_buftarg_list;
1983}
1984#endif