blob: 52785189212fad95e7428c254b864216f0374f05 [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
491 /* Attempt to get the semaphore without sleeping,
492 * if this does not work then we need to drop the
493 * spinlock and do a hard attempt on the semaphore.
494 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100495 if (down_trylock(&bp->b_sema)) {
496 if (!(flags & XBF_TRYLOCK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 /* wait for buffer ownership */
Nathan Scottce8e9222006-01-11 15:39:08 +1100498 xfs_buf_lock(bp);
499 XFS_STATS_INC(xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 } else {
501 /* We asked for a trylock and failed, no need
502 * to look at file offset and length here, we
Nathan Scottce8e9222006-01-11 15:39:08 +1100503 * know that this buffer at least overlaps our
504 * buffer and is locked, therefore our buffer
505 * either does not exist, or is this buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100507 xfs_buf_rele(bp);
508 XFS_STATS_INC(xb_busy_locked);
509 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
511 } else {
512 /* trylock worked */
Nathan Scottce8e9222006-01-11 15:39:08 +1100513 XB_SET_OWNER(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515
Nathan Scottce8e9222006-01-11 15:39:08 +1100516 if (bp->b_flags & XBF_STALE) {
517 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
518 bp->b_flags &= XBF_MAPPED;
David Chinner2f926582005-09-05 08:33:35 +1000519 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000520
521 trace_xfs_buf_find(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100522 XFS_STATS_INC(xb_get_locked);
523 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
526/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100527 * Assembles a buffer covering the specified range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 * Storage in memory for all portions of the buffer will be allocated,
529 * although backing storage may not be.
530 */
531xfs_buf_t *
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000532xfs_buf_get(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 xfs_buftarg_t *target,/* target for buffer */
Nathan Scott204ab252006-01-11 20:50:22 +1100534 xfs_off_t ioff, /* starting offset of range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 size_t isize, /* length of range */
Nathan Scottce8e9222006-01-11 15:39:08 +1100536 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
Nathan Scottce8e9222006-01-11 15:39:08 +1100538 xfs_buf_t *bp, *new_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 int error = 0, i;
540
Nathan Scottce8e9222006-01-11 15:39:08 +1100541 new_bp = xfs_buf_allocate(flags);
542 if (unlikely(!new_bp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return NULL;
544
Nathan Scottce8e9222006-01-11 15:39:08 +1100545 bp = _xfs_buf_find(target, ioff, isize, flags, new_bp);
546 if (bp == new_bp) {
547 error = _xfs_buf_lookup_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (error)
549 goto no_buffer;
550 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100551 xfs_buf_deallocate(new_bp);
552 if (unlikely(bp == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return NULL;
554 }
555
Nathan Scottce8e9222006-01-11 15:39:08 +1100556 for (i = 0; i < bp->b_page_count; i++)
557 mark_page_accessed(bp->b_pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Nathan Scottce8e9222006-01-11 15:39:08 +1100559 if (!(bp->b_flags & XBF_MAPPED)) {
560 error = _xfs_buf_map_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (unlikely(error)) {
562 printk(KERN_WARNING "%s: failed to map pages\n",
Harvey Harrison34a622b2008-04-10 12:19:21 +1000563 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 goto no_buffer;
565 }
566 }
567
Nathan Scottce8e9222006-01-11 15:39:08 +1100568 XFS_STATS_INC(xb_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 /*
571 * Always fill in the block number now, the mapped cases can do
572 * their own overlay of this later.
573 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100574 bp->b_bn = ioff;
575 bp->b_count_desired = bp->b_buffer_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000577 trace_xfs_buf_get(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100578 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100581 if (flags & (XBF_LOCK | XBF_TRYLOCK))
582 xfs_buf_unlock(bp);
583 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return NULL;
585}
586
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100587STATIC int
588_xfs_buf_read(
589 xfs_buf_t *bp,
590 xfs_buf_flags_t flags)
591{
592 int status;
593
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100594 ASSERT(!(flags & (XBF_DELWRI|XBF_WRITE)));
595 ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL);
596
597 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_DELWRI | \
598 XBF_READ_AHEAD | _XBF_RUN_QUEUES);
599 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | \
600 XBF_READ_AHEAD | _XBF_RUN_QUEUES);
601
602 status = xfs_buf_iorequest(bp);
Dave Chinnerec53d1d2010-07-20 17:52:59 +1000603 if (status || XFS_BUF_ISERROR(bp) || (flags & XBF_ASYNC))
604 return status;
605 return xfs_buf_iowait(bp);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100606}
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608xfs_buf_t *
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000609xfs_buf_read(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100611 xfs_off_t ioff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 size_t isize,
Nathan Scottce8e9222006-01-11 15:39:08 +1100613 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Nathan Scottce8e9222006-01-11 15:39:08 +1100615 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Nathan Scottce8e9222006-01-11 15:39:08 +1100617 flags |= XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000619 bp = xfs_buf_get(target, ioff, isize, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100620 if (bp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000621 trace_xfs_buf_read(bp, flags, _RET_IP_);
622
Nathan Scottce8e9222006-01-11 15:39:08 +1100623 if (!XFS_BUF_ISDONE(bp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100624 XFS_STATS_INC(xb_get_read);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100625 _xfs_buf_read(bp, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100626 } else if (flags & XBF_ASYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 /*
628 * Read ahead call which is already satisfied,
629 * drop the buffer
630 */
631 goto no_buffer;
632 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 /* We do not want read in the flags */
Nathan Scottce8e9222006-01-11 15:39:08 +1100634 bp->b_flags &= ~XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636 }
637
Nathan Scottce8e9222006-01-11 15:39:08 +1100638 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100641 if (flags & (XBF_LOCK | XBF_TRYLOCK))
642 xfs_buf_unlock(bp);
643 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return NULL;
645}
646
647/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100648 * If we are not low on memory then do the readahead in a deadlock
649 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 */
651void
Nathan Scottce8e9222006-01-11 15:39:08 +1100652xfs_buf_readahead(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100654 xfs_off_t ioff,
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000655 size_t isize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
657 struct backing_dev_info *bdi;
658
Nathan Scottce8e9222006-01-11 15:39:08 +1100659 bdi = target->bt_mapping->backing_dev_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (bdi_read_congested(bdi))
661 return;
662
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000663 xfs_buf_read(target, ioff, isize,
664 XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD|XBF_DONT_BLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
Dave Chinner5adc94c2010-09-24 21:58:31 +1000667/*
668 * Read an uncached buffer from disk. Allocates and returns a locked
669 * buffer containing the disk contents or nothing.
670 */
671struct xfs_buf *
672xfs_buf_read_uncached(
673 struct xfs_mount *mp,
674 struct xfs_buftarg *target,
675 xfs_daddr_t daddr,
676 size_t length,
677 int flags)
678{
679 xfs_buf_t *bp;
680 int error;
681
682 bp = xfs_buf_get_uncached(target, length, flags);
683 if (!bp)
684 return NULL;
685
686 /* set up the buffer for a read IO */
687 xfs_buf_lock(bp);
688 XFS_BUF_SET_ADDR(bp, daddr);
689 XFS_BUF_READ(bp);
690 XFS_BUF_BUSY(bp);
691
692 xfsbdstrat(mp, bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000693 error = xfs_buf_iowait(bp);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000694 if (error || bp->b_error) {
695 xfs_buf_relse(bp);
696 return NULL;
697 }
698 return bp;
699}
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100702xfs_buf_get_empty(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 size_t len,
704 xfs_buftarg_t *target)
705{
Nathan Scottce8e9222006-01-11 15:39:08 +1100706 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Nathan Scottce8e9222006-01-11 15:39:08 +1100708 bp = xfs_buf_allocate(0);
709 if (bp)
710 _xfs_buf_initialize(bp, target, 0, len, 0);
711 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
714static inline struct page *
715mem_to_page(
716 void *addr)
717{
Christoph Lameter9e2779f2008-02-04 22:28:34 -0800718 if ((!is_vmalloc_addr(addr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return virt_to_page(addr);
720 } else {
721 return vmalloc_to_page(addr);
722 }
723}
724
725int
Nathan Scottce8e9222006-01-11 15:39:08 +1100726xfs_buf_associate_memory(
727 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 void *mem,
729 size_t len)
730{
731 int rval;
732 int i = 0;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100733 unsigned long pageaddr;
734 unsigned long offset;
735 size_t buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 int page_count;
737
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100738 pageaddr = (unsigned long)mem & PAGE_CACHE_MASK;
739 offset = (unsigned long)mem - pageaddr;
740 buflen = PAGE_CACHE_ALIGN(len + offset);
741 page_count = buflen >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 /* Free any previous set of page pointers */
Nathan Scottce8e9222006-01-11 15:39:08 +1100744 if (bp->b_pages)
745 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Nathan Scottce8e9222006-01-11 15:39:08 +1100747 bp->b_pages = NULL;
748 bp->b_addr = mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Christoph Hellwig36fae172009-07-18 18:14:58 -0400750 rval = _xfs_buf_get_pages(bp, page_count, XBF_DONT_BLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (rval)
752 return rval;
753
Nathan Scottce8e9222006-01-11 15:39:08 +1100754 bp->b_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100756 for (i = 0; i < bp->b_page_count; i++) {
757 bp->b_pages[i] = mem_to_page((void *)pageaddr);
758 pageaddr += PAGE_CACHE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100761 bp->b_count_desired = len;
762 bp->b_buffer_length = buflen;
Nathan Scottce8e9222006-01-11 15:39:08 +1100763 bp->b_flags |= XBF_MAPPED;
Christoph Hellwig6ab455e2008-05-19 16:34:42 +1000764 bp->b_flags &= ~_XBF_PAGE_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 return 0;
767}
768
769xfs_buf_t *
Dave Chinner686865f2010-09-24 20:07:47 +1000770xfs_buf_get_uncached(
771 struct xfs_buftarg *target,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 size_t len,
Dave Chinner686865f2010-09-24 20:07:47 +1000773 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000775 unsigned long page_count = PAGE_ALIGN(len) >> PAGE_SHIFT;
776 int error, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Nathan Scottce8e9222006-01-11 15:39:08 +1100779 bp = xfs_buf_allocate(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (unlikely(bp == NULL))
781 goto fail;
Nathan Scottce8e9222006-01-11 15:39:08 +1100782 _xfs_buf_initialize(bp, target, 0, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000784 error = _xfs_buf_get_pages(bp, page_count, 0);
785 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 goto fail_free_buf;
787
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000788 for (i = 0; i < page_count; i++) {
Dave Chinner686865f2010-09-24 20:07:47 +1000789 bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000790 if (!bp->b_pages[i])
791 goto fail_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000793 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000795 error = _xfs_buf_map_pages(bp, XBF_MAPPED);
796 if (unlikely(error)) {
797 printk(KERN_WARNING "%s: failed to map pages\n",
Harvey Harrison34a622b2008-04-10 12:19:21 +1000798 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 goto fail_free_mem;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Nathan Scottce8e9222006-01-11 15:39:08 +1100802 xfs_buf_unlock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Dave Chinner686865f2010-09-24 20:07:47 +1000804 trace_xfs_buf_get_uncached(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return bp;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 fail_free_mem:
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000808 while (--i >= 0)
809 __free_page(bp->b_pages[i]);
Christoph Hellwigca165b82007-05-24 15:21:11 +1000810 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 fail_free_buf:
Christoph Hellwigca165b82007-05-24 15:21:11 +1000812 xfs_buf_deallocate(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 fail:
814 return NULL;
815}
816
817/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 * Increment reference count on buffer, to hold the buffer concurrently
819 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 * Must hold the buffer already to call this function.
821 */
822void
Nathan Scottce8e9222006-01-11 15:39:08 +1100823xfs_buf_hold(
824 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000826 trace_xfs_buf_hold(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100827 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
829
830/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100831 * Releases a hold on the specified buffer. If the
832 * the hold count is 1, calls xfs_buf_free.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 */
834void
Nathan Scottce8e9222006-01-11 15:39:08 +1100835xfs_buf_rele(
836 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
Dave Chinner74f75a02010-09-24 19:59:04 +1000838 struct xfs_perag *pag = bp->b_pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000840 trace_xfs_buf_rele(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Dave Chinner74f75a02010-09-24 19:59:04 +1000842 if (!pag) {
Nathan Scottfad3aa12006-02-01 12:14:52 +1100843 ASSERT(!bp->b_relse);
Dave Chinner74f75a02010-09-24 19:59:04 +1000844 ASSERT(RB_EMPTY_NODE(&bp->b_rbnode));
Nathan Scottfad3aa12006-02-01 12:14:52 +1100845 if (atomic_dec_and_test(&bp->b_hold))
846 xfs_buf_free(bp);
847 return;
848 }
849
Dave Chinner74f75a02010-09-24 19:59:04 +1000850 ASSERT(!RB_EMPTY_NODE(&bp->b_rbnode));
Lachlan McIlroy37906892008-08-13 15:42:10 +1000851 ASSERT(atomic_read(&bp->b_hold) > 0);
Dave Chinner74f75a02010-09-24 19:59:04 +1000852 if (atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100853 if (bp->b_relse) {
854 atomic_inc(&bp->b_hold);
Dave Chinner74f75a02010-09-24 19:59:04 +1000855 spin_unlock(&pag->pag_buf_lock);
856 bp->b_relse(bp);
Christoph Hellwig7f14d0a2005-11-02 15:09:35 +1100857 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100858 ASSERT(!(bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)));
Dave Chinner74f75a02010-09-24 19:59:04 +1000859 rb_erase(&bp->b_rbnode, &pag->pag_buf_tree);
860 spin_unlock(&pag->pag_buf_lock);
861 xfs_perag_put(pag);
Nathan Scottce8e9222006-01-11 15:39:08 +1100862 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
864 }
865}
866
867
868/*
869 * Mutual exclusion on buffers. Locking model:
870 *
871 * Buffers associated with inodes for which buffer locking
872 * is not enabled are not protected by semaphores, and are
873 * assumed to be exclusively owned by the caller. There is a
874 * spinlock in the buffer, used by the caller when concurrent
875 * access is possible.
876 */
877
878/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100879 * Locks a buffer object, if it is not already locked.
880 * Note that this in no way locks the underlying pages, so it is only
881 * useful for synchronizing concurrent use of buffer objects, not for
882 * synchronizing independent access to the underlying pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 */
884int
Nathan Scottce8e9222006-01-11 15:39:08 +1100885xfs_buf_cond_lock(
886 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
888 int locked;
889
Nathan Scottce8e9222006-01-11 15:39:08 +1100890 locked = down_trylock(&bp->b_sema) == 0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000891 if (locked)
Nathan Scottce8e9222006-01-11 15:39:08 +1100892 XB_SET_OWNER(bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000893
894 trace_xfs_buf_cond_lock(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100895 return locked ? 0 : -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896}
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898int
Nathan Scottce8e9222006-01-11 15:39:08 +1100899xfs_buf_lock_value(
900 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Stephen Rothwelladaa6932008-04-22 15:26:13 +1000902 return bp->b_sema.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100906 * Locks a buffer object.
907 * Note that this in no way locks the underlying pages, so it is only
908 * useful for synchronizing concurrent use of buffer objects, not for
909 * synchronizing independent access to the underlying pages.
Dave Chinnered3b4d62010-05-21 12:07:08 +1000910 *
911 * If we come across a stale, pinned, locked buffer, we know that we
912 * are being asked to lock a buffer that has been reallocated. Because
913 * it is pinned, we know that the log has not been pushed to disk and
914 * hence it will still be locked. Rather than sleeping until someone
915 * else pushes the log, push it ourselves before trying to get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100917void
918xfs_buf_lock(
919 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000921 trace_xfs_buf_lock(bp, _RET_IP_);
922
Dave Chinnered3b4d62010-05-21 12:07:08 +1000923 if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
Dave Chinnerebad8612010-09-22 10:47:20 +1000924 xfs_log_force(bp->b_target->bt_mount, 0);
Nathan Scottce8e9222006-01-11 15:39:08 +1100925 if (atomic_read(&bp->b_io_remaining))
926 blk_run_address_space(bp->b_target->bt_mapping);
927 down(&bp->b_sema);
928 XB_SET_OWNER(bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000929
930 trace_xfs_buf_lock_done(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
932
933/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100934 * Releases the lock on the buffer object.
David Chinner2f926582005-09-05 08:33:35 +1000935 * If the buffer is marked delwri but is not queued, do so before we
Nathan Scottce8e9222006-01-11 15:39:08 +1100936 * unlock the buffer as we need to set flags correctly. We also need to
David Chinner2f926582005-09-05 08:33:35 +1000937 * take a reference for the delwri queue because the unlocker is going to
938 * drop their's and they don't know we just queued it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 */
940void
Nathan Scottce8e9222006-01-11 15:39:08 +1100941xfs_buf_unlock(
942 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
Nathan Scottce8e9222006-01-11 15:39:08 +1100944 if ((bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)) == XBF_DELWRI) {
945 atomic_inc(&bp->b_hold);
946 bp->b_flags |= XBF_ASYNC;
947 xfs_buf_delwri_queue(bp, 0);
David Chinner2f926582005-09-05 08:33:35 +1000948 }
949
Nathan Scottce8e9222006-01-11 15:39:08 +1100950 XB_CLEAR_OWNER(bp);
951 up(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000952
953 trace_xfs_buf_unlock(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954}
955
Nathan Scottce8e9222006-01-11 15:39:08 +1100956STATIC void
957xfs_buf_wait_unpin(
958 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
960 DECLARE_WAITQUEUE (wait, current);
961
Nathan Scottce8e9222006-01-11 15:39:08 +1100962 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 return;
964
Nathan Scottce8e9222006-01-11 15:39:08 +1100965 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 for (;;) {
967 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +1100968 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 break;
Nathan Scottce8e9222006-01-11 15:39:08 +1100970 if (atomic_read(&bp->b_io_remaining))
971 blk_run_address_space(bp->b_target->bt_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 schedule();
973 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100974 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 set_current_state(TASK_RUNNING);
976}
977
978/*
979 * Buffer Utility Routines
980 */
981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100983xfs_buf_iodone_work(
David Howellsc4028952006-11-22 14:57:56 +0000984 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
David Howellsc4028952006-11-22 14:57:56 +0000986 xfs_buf_t *bp =
987 container_of(work, xfs_buf_t, b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
David Chinner0bfefc42007-05-14 18:24:23 +1000989 /*
990 * We can get an EOPNOTSUPP to ordered writes. Here we clear the
991 * ordered flag and reissue them. Because we can't tell the higher
992 * layers directly that they should not issue ordered I/O anymore, they
Christoph Hellwig73f6aa42008-10-10 17:28:29 +1100993 * need to check if the _XFS_BARRIER_FAILED flag was set during I/O completion.
David Chinner0bfefc42007-05-14 18:24:23 +1000994 */
995 if ((bp->b_error == EOPNOTSUPP) &&
996 (bp->b_flags & (XBF_ORDERED|XBF_ASYNC)) == (XBF_ORDERED|XBF_ASYNC)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000997 trace_xfs_buf_ordered_retry(bp, _RET_IP_);
David Chinner0bfefc42007-05-14 18:24:23 +1000998 bp->b_flags &= ~XBF_ORDERED;
Christoph Hellwig73f6aa42008-10-10 17:28:29 +1100999 bp->b_flags |= _XFS_BARRIER_FAILED;
David Chinner0bfefc42007-05-14 18:24:23 +10001000 xfs_buf_iorequest(bp);
1001 } else if (bp->b_iodone)
Nathan Scottce8e9222006-01-11 15:39:08 +11001002 (*(bp->b_iodone))(bp);
1003 else if (bp->b_flags & XBF_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 xfs_buf_relse(bp);
1005}
1006
1007void
Nathan Scottce8e9222006-01-11 15:39:08 +11001008xfs_buf_ioend(
1009 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 int schedule)
1011{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001012 trace_xfs_buf_iodone(bp, _RET_IP_);
1013
Lachlan McIlroy77be55a2007-11-23 16:31:00 +11001014 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
Nathan Scottce8e9222006-01-11 15:39:08 +11001015 if (bp->b_error == 0)
1016 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Nathan Scottce8e9222006-01-11 15:39:08 +11001018 if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (schedule) {
David Howellsc4028952006-11-22 14:57:56 +00001020 INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
Nathan Scottce8e9222006-01-11 15:39:08 +11001021 queue_work(xfslogd_workqueue, &bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 } else {
David Howellsc4028952006-11-22 14:57:56 +00001023 xfs_buf_iodone_work(&bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 }
1025 } else {
David Chinnerb4dd3302008-08-13 16:36:11 +10001026 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
1028}
1029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030void
Nathan Scottce8e9222006-01-11 15:39:08 +11001031xfs_buf_ioerror(
1032 xfs_buf_t *bp,
1033 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
1035 ASSERT(error >= 0 && error <= 0xffff);
Nathan Scottce8e9222006-01-11 15:39:08 +11001036 bp->b_error = (unsigned short)error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001037 trace_xfs_buf_ioerror(bp, error, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038}
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040int
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001041xfs_bwrite(
1042 struct xfs_mount *mp,
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001043 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
Christoph Hellwig8c383662010-03-12 10:59:40 +00001045 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001047 bp->b_flags |= XBF_WRITE;
Christoph Hellwig8c383662010-03-12 10:59:40 +00001048 bp->b_flags &= ~(XBF_ASYNC | XBF_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001050 xfs_buf_delwri_dequeue(bp);
Christoph Hellwig939d7232010-07-20 17:51:16 +10001051 xfs_bdstrat_cb(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Christoph Hellwig8c383662010-03-12 10:59:40 +00001053 error = xfs_buf_iowait(bp);
1054 if (error)
1055 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1056 xfs_buf_relse(bp);
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001057 return error;
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001058}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001060void
1061xfs_bdwrite(
1062 void *mp,
1063 struct xfs_buf *bp)
1064{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001065 trace_xfs_buf_bdwrite(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001067 bp->b_flags &= ~XBF_READ;
1068 bp->b_flags |= (XBF_DELWRI | XBF_ASYNC);
1069
1070 xfs_buf_delwri_queue(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
1072
Christoph Hellwig4e234712010-01-13 22:17:56 +00001073/*
1074 * Called when we want to stop a buffer from getting written or read.
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001075 * We attach the EIO error, muck with its flags, and call xfs_buf_ioend
Christoph Hellwig4e234712010-01-13 22:17:56 +00001076 * so that the proper iodone callbacks get called.
1077 */
1078STATIC int
1079xfs_bioerror(
1080 xfs_buf_t *bp)
1081{
1082#ifdef XFSERRORDEBUG
1083 ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
1084#endif
1085
1086 /*
1087 * No need to wait until the buffer is unpinned, we aren't flushing it.
1088 */
1089 XFS_BUF_ERROR(bp, EIO);
1090
1091 /*
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001092 * We're calling xfs_buf_ioend, so delete XBF_DONE flag.
Christoph Hellwig4e234712010-01-13 22:17:56 +00001093 */
1094 XFS_BUF_UNREAD(bp);
1095 XFS_BUF_UNDELAYWRITE(bp);
1096 XFS_BUF_UNDONE(bp);
1097 XFS_BUF_STALE(bp);
1098
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001099 xfs_buf_ioend(bp, 0);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001100
1101 return EIO;
1102}
1103
1104/*
1105 * Same as xfs_bioerror, except that we are releasing the buffer
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001106 * here ourselves, and avoiding the xfs_buf_ioend call.
Christoph Hellwig4e234712010-01-13 22:17:56 +00001107 * This is meant for userdata errors; metadata bufs come with
1108 * iodone functions attached, so that we can track down errors.
1109 */
1110STATIC int
1111xfs_bioerror_relse(
1112 struct xfs_buf *bp)
1113{
1114 int64_t fl = XFS_BUF_BFLAGS(bp);
1115 /*
1116 * No need to wait until the buffer is unpinned.
1117 * We aren't flushing it.
1118 *
1119 * chunkhold expects B_DONE to be set, whether
1120 * we actually finish the I/O or not. We don't want to
1121 * change that interface.
1122 */
1123 XFS_BUF_UNREAD(bp);
1124 XFS_BUF_UNDELAYWRITE(bp);
1125 XFS_BUF_DONE(bp);
1126 XFS_BUF_STALE(bp);
1127 XFS_BUF_CLR_IODONE_FUNC(bp);
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001128 if (!(fl & XBF_ASYNC)) {
Christoph Hellwig4e234712010-01-13 22:17:56 +00001129 /*
1130 * Mark b_error and B_ERROR _both_.
1131 * Lot's of chunkcache code assumes that.
1132 * There's no reason to mark error for
1133 * ASYNC buffers.
1134 */
1135 XFS_BUF_ERROR(bp, EIO);
1136 XFS_BUF_FINISH_IOWAIT(bp);
1137 } else {
1138 xfs_buf_relse(bp);
1139 }
1140
1141 return EIO;
1142}
1143
1144
1145/*
1146 * All xfs metadata buffers except log state machine buffers
1147 * get this attached as their b_bdstrat callback function.
1148 * This is so that we can catch a buffer
1149 * after prematurely unpinning it to forcibly shutdown the filesystem.
1150 */
1151int
1152xfs_bdstrat_cb(
1153 struct xfs_buf *bp)
1154{
Dave Chinnerebad8612010-09-22 10:47:20 +10001155 if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
Christoph Hellwig4e234712010-01-13 22:17:56 +00001156 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1157 /*
1158 * Metadata write that didn't get logged but
1159 * written delayed anyway. These aren't associated
1160 * with a transaction, and can be ignored.
1161 */
1162 if (!bp->b_iodone && !XFS_BUF_ISREAD(bp))
1163 return xfs_bioerror_relse(bp);
1164 else
1165 return xfs_bioerror(bp);
1166 }
1167
1168 xfs_buf_iorequest(bp);
1169 return 0;
1170}
1171
1172/*
1173 * Wrapper around bdstrat so that we can stop data from going to disk in case
1174 * we are shutting down the filesystem. Typically user data goes thru this
1175 * path; one of the exceptions is the superblock.
1176 */
1177void
1178xfsbdstrat(
1179 struct xfs_mount *mp,
1180 struct xfs_buf *bp)
1181{
1182 if (XFS_FORCED_SHUTDOWN(mp)) {
1183 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1184 xfs_bioerror_relse(bp);
1185 return;
1186 }
1187
1188 xfs_buf_iorequest(bp);
1189}
1190
Christoph Hellwigb8f82a42009-11-14 16:17:22 +00001191STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001192_xfs_buf_ioend(
1193 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 int schedule)
1195{
Christoph Hellwig6ab455e2008-05-19 16:34:42 +10001196 if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
1197 bp->b_flags &= ~_XBF_PAGE_LOCKED;
Nathan Scottce8e9222006-01-11 15:39:08 +11001198 xfs_buf_ioend(bp, schedule);
Christoph Hellwig6ab455e2008-05-19 16:34:42 +10001199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200}
1201
Al Viro782e3b32007-10-12 07:17:47 +01001202STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001203xfs_buf_bio_end_io(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 struct bio *bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 int error)
1206{
Nathan Scottce8e9222006-01-11 15:39:08 +11001207 xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
1208 unsigned int blocksize = bp->b_target->bt_bsize;
Nathan Scotteedb5532005-09-02 16:39:56 +10001209 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Lachlan McIlroycfbe5262008-12-12 15:27:25 +11001211 xfs_buf_ioerror(bp, -error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
James Bottomley73c77e22010-01-25 11:42:24 -06001213 if (!error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
1214 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1215
Nathan Scotteedb5532005-09-02 16:39:56 +10001216 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 struct page *page = bvec->bv_page;
1218
Nathan Scott948ecdb2006-09-28 11:03:13 +10001219 ASSERT(!PagePrivate(page));
Nathan Scottce8e9222006-01-11 15:39:08 +11001220 if (unlikely(bp->b_error)) {
1221 if (bp->b_flags & XBF_READ)
Nathan Scotteedb5532005-09-02 16:39:56 +10001222 ClearPageUptodate(page);
Nathan Scottce8e9222006-01-11 15:39:08 +11001223 } else if (blocksize >= PAGE_CACHE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 SetPageUptodate(page);
1225 } else if (!PagePrivate(page) &&
Nathan Scottce8e9222006-01-11 15:39:08 +11001226 (bp->b_flags & _XBF_PAGE_CACHE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 set_page_region(page, bvec->bv_offset, bvec->bv_len);
1228 }
1229
Nathan Scotteedb5532005-09-02 16:39:56 +10001230 if (--bvec >= bio->bi_io_vec)
1231 prefetchw(&bvec->bv_page->flags);
Christoph Hellwig6ab455e2008-05-19 16:34:42 +10001232
1233 if (bp->b_flags & _XBF_PAGE_LOCKED)
1234 unlock_page(page);
Nathan Scotteedb5532005-09-02 16:39:56 +10001235 } while (bvec >= bio->bi_io_vec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Nathan Scottce8e9222006-01-11 15:39:08 +11001237 _xfs_buf_ioend(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239}
1240
1241STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001242_xfs_buf_ioapply(
1243 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
Christoph Hellwiga9759f22007-12-07 14:07:08 +11001245 int rw, map_i, total_nr_pages, nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 struct bio *bio;
Nathan Scottce8e9222006-01-11 15:39:08 +11001247 int offset = bp->b_offset;
1248 int size = bp->b_count_desired;
1249 sector_t sector = bp->b_bn;
1250 unsigned int blocksize = bp->b_target->bt_bsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Nathan Scottce8e9222006-01-11 15:39:08 +11001252 total_nr_pages = bp->b_page_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 map_i = 0;
1254
Nathan Scottce8e9222006-01-11 15:39:08 +11001255 if (bp->b_flags & XBF_ORDERED) {
1256 ASSERT(!(bp->b_flags & XBF_READ));
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001257 rw = WRITE_BARRIER;
Dave Chinner2ee1aba2009-11-24 18:03:15 +00001258 } else if (bp->b_flags & XBF_LOG_BUFFER) {
Nathan Scott51bdd702006-09-28 11:01:57 +10001259 ASSERT(!(bp->b_flags & XBF_READ_AHEAD));
1260 bp->b_flags &= ~_XBF_RUN_QUEUES;
1261 rw = (bp->b_flags & XBF_WRITE) ? WRITE_SYNC : READ_SYNC;
Dave Chinner2ee1aba2009-11-24 18:03:15 +00001262 } else if (bp->b_flags & _XBF_RUN_QUEUES) {
1263 ASSERT(!(bp->b_flags & XBF_READ_AHEAD));
1264 bp->b_flags &= ~_XBF_RUN_QUEUES;
1265 rw = (bp->b_flags & XBF_WRITE) ? WRITE_META : READ_META;
Nathan Scott51bdd702006-09-28 11:01:57 +10001266 } else {
1267 rw = (bp->b_flags & XBF_WRITE) ? WRITE :
1268 (bp->b_flags & XBF_READ_AHEAD) ? READA : READ;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001269 }
1270
Nathan Scottce8e9222006-01-11 15:39:08 +11001271 /* Special code path for reading a sub page size buffer in --
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 * we populate up the whole page, and hence the other metadata
1273 * in the same page. This optimization is only valid when the
Nathan Scottce8e9222006-01-11 15:39:08 +11001274 * filesystem block size is not smaller than the page size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001276 if ((bp->b_buffer_length < PAGE_CACHE_SIZE) &&
Christoph Hellwig6ab455e2008-05-19 16:34:42 +10001277 ((bp->b_flags & (XBF_READ|_XBF_PAGE_LOCKED)) ==
1278 (XBF_READ|_XBF_PAGE_LOCKED)) &&
Nathan Scottce8e9222006-01-11 15:39:08 +11001279 (blocksize >= PAGE_CACHE_SIZE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 bio = bio_alloc(GFP_NOIO, 1);
1281
Nathan Scottce8e9222006-01-11 15:39:08 +11001282 bio->bi_bdev = bp->b_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 bio->bi_sector = sector - (offset >> BBSHIFT);
Nathan Scottce8e9222006-01-11 15:39:08 +11001284 bio->bi_end_io = xfs_buf_bio_end_io;
1285 bio->bi_private = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Nathan Scottce8e9222006-01-11 15:39:08 +11001287 bio_add_page(bio, bp->b_pages[0], PAGE_CACHE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 size = 0;
1289
Nathan Scottce8e9222006-01-11 15:39:08 +11001290 atomic_inc(&bp->b_io_remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 goto submit_io;
1293 }
1294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001296 atomic_inc(&bp->b_io_remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
1298 if (nr_pages > total_nr_pages)
1299 nr_pages = total_nr_pages;
1300
1301 bio = bio_alloc(GFP_NOIO, nr_pages);
Nathan Scottce8e9222006-01-11 15:39:08 +11001302 bio->bi_bdev = bp->b_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 bio->bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001304 bio->bi_end_io = xfs_buf_bio_end_io;
1305 bio->bi_private = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 for (; size && nr_pages; nr_pages--, map_i++) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001308 int rbytes, nbytes = PAGE_CACHE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 if (nbytes > size)
1311 nbytes = size;
1312
Nathan Scottce8e9222006-01-11 15:39:08 +11001313 rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset);
1314 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 break;
1316
1317 offset = 0;
1318 sector += nbytes >> BBSHIFT;
1319 size -= nbytes;
1320 total_nr_pages--;
1321 }
1322
1323submit_io:
1324 if (likely(bio->bi_size)) {
James Bottomley73c77e22010-01-25 11:42:24 -06001325 if (xfs_buf_is_vmapped(bp)) {
1326 flush_kernel_vmap_range(bp->b_addr,
1327 xfs_buf_vmap_len(bp));
1328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 submit_bio(rw, bio);
1330 if (size)
1331 goto next_chunk;
1332 } else {
Dave Chinnerec53d1d2010-07-20 17:52:59 +10001333 /*
1334 * if we get here, no pages were added to the bio. However,
1335 * we can't just error out here - if the pages are locked then
1336 * we have to unlock them otherwise we can hang on a later
1337 * access to the page.
1338 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001339 xfs_buf_ioerror(bp, EIO);
Dave Chinnerec53d1d2010-07-20 17:52:59 +10001340 if (bp->b_flags & _XBF_PAGE_LOCKED) {
1341 int i;
1342 for (i = 0; i < bp->b_page_count; i++)
1343 unlock_page(bp->b_pages[i]);
1344 }
1345 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 }
1347}
1348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349int
Nathan Scottce8e9222006-01-11 15:39:08 +11001350xfs_buf_iorequest(
1351 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001353 trace_xfs_buf_iorequest(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Nathan Scottce8e9222006-01-11 15:39:08 +11001355 if (bp->b_flags & XBF_DELWRI) {
1356 xfs_buf_delwri_queue(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 return 0;
1358 }
1359
Nathan Scottce8e9222006-01-11 15:39:08 +11001360 if (bp->b_flags & XBF_WRITE) {
1361 xfs_buf_wait_unpin(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
1363
Nathan Scottce8e9222006-01-11 15:39:08 +11001364 xfs_buf_hold(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 /* Set the count to 1 initially, this will stop an I/O
1367 * completion callout which happens before we have started
Nathan Scottce8e9222006-01-11 15:39:08 +11001368 * all the I/O from calling xfs_buf_ioend too early.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001370 atomic_set(&bp->b_io_remaining, 1);
1371 _xfs_buf_ioapply(bp);
1372 _xfs_buf_ioend(bp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Nathan Scottce8e9222006-01-11 15:39:08 +11001374 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 return 0;
1376}
1377
1378/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001379 * Waits for I/O to complete on the buffer supplied.
1380 * It returns immediately if no I/O is pending.
1381 * It returns the I/O error code, if any, or 0 if there was no error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 */
1383int
Nathan Scottce8e9222006-01-11 15:39:08 +11001384xfs_buf_iowait(
1385 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001387 trace_xfs_buf_iowait(bp, _RET_IP_);
1388
Nathan Scottce8e9222006-01-11 15:39:08 +11001389 if (atomic_read(&bp->b_io_remaining))
1390 blk_run_address_space(bp->b_target->bt_mapping);
David Chinnerb4dd3302008-08-13 16:36:11 +10001391 wait_for_completion(&bp->b_iowait);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001392
1393 trace_xfs_buf_iowait_done(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +11001394 return bp->b_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395}
1396
Nathan Scottce8e9222006-01-11 15:39:08 +11001397xfs_caddr_t
1398xfs_buf_offset(
1399 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 size_t offset)
1401{
1402 struct page *page;
1403
Nathan Scottce8e9222006-01-11 15:39:08 +11001404 if (bp->b_flags & XBF_MAPPED)
1405 return XFS_BUF_PTR(bp) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Nathan Scottce8e9222006-01-11 15:39:08 +11001407 offset += bp->b_offset;
1408 page = bp->b_pages[offset >> PAGE_CACHE_SHIFT];
1409 return (xfs_caddr_t)page_address(page) + (offset & (PAGE_CACHE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410}
1411
1412/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 * Move data into or out of a buffer.
1414 */
1415void
Nathan Scottce8e9222006-01-11 15:39:08 +11001416xfs_buf_iomove(
1417 xfs_buf_t *bp, /* buffer to process */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 size_t boff, /* starting buffer offset */
1419 size_t bsize, /* length to copy */
Dave Chinnerb9c48642010-01-20 10:47:39 +11001420 void *data, /* data address */
Nathan Scottce8e9222006-01-11 15:39:08 +11001421 xfs_buf_rw_t mode) /* read/write/zero flag */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
1423 size_t bend, cpoff, csize;
1424 struct page *page;
1425
1426 bend = boff + bsize;
1427 while (boff < bend) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001428 page = bp->b_pages[xfs_buf_btoct(boff + bp->b_offset)];
1429 cpoff = xfs_buf_poff(boff + bp->b_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 csize = min_t(size_t,
Nathan Scottce8e9222006-01-11 15:39:08 +11001431 PAGE_CACHE_SIZE-cpoff, bp->b_count_desired-boff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 ASSERT(((csize + cpoff) <= PAGE_CACHE_SIZE));
1434
1435 switch (mode) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001436 case XBRW_ZERO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 memset(page_address(page) + cpoff, 0, csize);
1438 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001439 case XBRW_READ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 memcpy(data, page_address(page) + cpoff, csize);
1441 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001442 case XBRW_WRITE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 memcpy(page_address(page) + cpoff, data, csize);
1444 }
1445
1446 boff += csize;
1447 data += csize;
1448 }
1449}
1450
1451/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001452 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 */
1454
1455/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001456 * Wait for any bufs with callbacks that have been submitted but
1457 * have not yet returned... walk the hash list for the target.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 */
1459void
1460xfs_wait_buftarg(
Dave Chinner74f75a02010-09-24 19:59:04 +10001461 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
Dave Chinner74f75a02010-09-24 19:59:04 +10001463 struct xfs_perag *pag;
1464 uint i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Dave Chinner74f75a02010-09-24 19:59:04 +10001466 for (i = 0; i < btp->bt_mount->m_sb.sb_agcount; i++) {
1467 pag = xfs_perag_get(btp->bt_mount, i);
1468 spin_lock(&pag->pag_buf_lock);
1469 while (rb_first(&pag->pag_buf_tree)) {
1470 spin_unlock(&pag->pag_buf_lock);
Dave Chinner26af6552010-09-22 10:47:20 +10001471 delay(100);
Dave Chinner74f75a02010-09-24 19:59:04 +10001472 spin_lock(&pag->pag_buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 }
Dave Chinner74f75a02010-09-24 19:59:04 +10001474 spin_unlock(&pag->pag_buf_lock);
1475 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 }
1477}
1478
1479/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001480 * buftarg list for delwrite queue processing
David Chinnera6867a62006-01-11 15:37:58 +11001481 */
Tim Shimmine6a0e9c2007-05-08 13:49:59 +10001482static LIST_HEAD(xfs_buftarg_list);
David Chinner7989cb82007-02-10 18:34:56 +11001483static DEFINE_SPINLOCK(xfs_buftarg_lock);
David Chinnera6867a62006-01-11 15:37:58 +11001484
1485STATIC void
1486xfs_register_buftarg(
1487 xfs_buftarg_t *btp)
1488{
1489 spin_lock(&xfs_buftarg_lock);
1490 list_add(&btp->bt_list, &xfs_buftarg_list);
1491 spin_unlock(&xfs_buftarg_lock);
1492}
1493
1494STATIC void
1495xfs_unregister_buftarg(
1496 xfs_buftarg_t *btp)
1497{
1498 spin_lock(&xfs_buftarg_lock);
1499 list_del(&btp->bt_list);
1500 spin_unlock(&xfs_buftarg_lock);
1501}
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503void
1504xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001505 struct xfs_mount *mp,
1506 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507{
1508 xfs_flush_buftarg(btp, 1);
Christoph Hellwigb7963132009-03-03 14:48:37 -05001509 if (mp->m_flags & XFS_MOUNT_BARRIER)
1510 xfs_blkdev_issue_flush(btp);
Nathan Scottce8e9222006-01-11 15:39:08 +11001511 iput(btp->bt_mapping->host);
David Chinnera6867a62006-01-11 15:37:58 +11001512
Nathan Scottce8e9222006-01-11 15:39:08 +11001513 /* Unregister the buftarg first so that we don't get a
1514 * wakeup finding a non-existent task
1515 */
David Chinnera6867a62006-01-11 15:37:58 +11001516 xfs_unregister_buftarg(btp);
1517 kthread_stop(btp->bt_task);
1518
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001519 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520}
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522STATIC int
1523xfs_setsize_buftarg_flags(
1524 xfs_buftarg_t *btp,
1525 unsigned int blocksize,
1526 unsigned int sectorsize,
1527 int verbose)
1528{
Nathan Scottce8e9222006-01-11 15:39:08 +11001529 btp->bt_bsize = blocksize;
1530 btp->bt_sshift = ffs(sectorsize) - 1;
1531 btp->bt_smask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Nathan Scottce8e9222006-01-11 15:39:08 +11001533 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 printk(KERN_WARNING
1535 "XFS: Cannot set_blocksize to %u on device %s\n",
1536 sectorsize, XFS_BUFTARG_NAME(btp));
1537 return EINVAL;
1538 }
1539
1540 if (verbose &&
1541 (PAGE_CACHE_SIZE / BITS_PER_LONG) > sectorsize) {
1542 printk(KERN_WARNING
1543 "XFS: %u byte sectors in use on device %s. "
1544 "This is suboptimal; %u or greater is ideal.\n",
1545 sectorsize, XFS_BUFTARG_NAME(btp),
1546 (unsigned int)PAGE_CACHE_SIZE / BITS_PER_LONG);
1547 }
1548
1549 return 0;
1550}
1551
1552/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001553 * When allocating the initial buffer target we have not yet
1554 * read in the superblock, so don't know what sized sectors
1555 * are being used is at this early stage. Play safe.
1556 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557STATIC int
1558xfs_setsize_buftarg_early(
1559 xfs_buftarg_t *btp,
1560 struct block_device *bdev)
1561{
1562 return xfs_setsize_buftarg_flags(btp,
Martin K. Petersene1defc42009-05-22 17:17:49 -04001563 PAGE_CACHE_SIZE, bdev_logical_block_size(bdev), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564}
1565
1566int
1567xfs_setsize_buftarg(
1568 xfs_buftarg_t *btp,
1569 unsigned int blocksize,
1570 unsigned int sectorsize)
1571{
1572 return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
1573}
1574
1575STATIC int
1576xfs_mapping_buftarg(
1577 xfs_buftarg_t *btp,
1578 struct block_device *bdev)
1579{
1580 struct backing_dev_info *bdi;
1581 struct inode *inode;
1582 struct address_space *mapping;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07001583 static const struct address_space_operations mapping_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 .sync_page = block_sync_page,
Christoph Lametere965f962006-02-01 03:05:41 -08001585 .migratepage = fail_migrate_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 };
1587
1588 inode = new_inode(bdev->bd_inode->i_sb);
1589 if (!inode) {
1590 printk(KERN_WARNING
1591 "XFS: Cannot allocate mapping inode for device %s\n",
1592 XFS_BUFTARG_NAME(btp));
1593 return ENOMEM;
1594 }
1595 inode->i_mode = S_IFBLK;
1596 inode->i_bdev = bdev;
1597 inode->i_rdev = bdev->bd_dev;
1598 bdi = blk_get_backing_dev_info(bdev);
1599 if (!bdi)
1600 bdi = &default_backing_dev_info;
1601 mapping = &inode->i_data;
1602 mapping->a_ops = &mapping_aops;
1603 mapping->backing_dev_info = bdi;
1604 mapping_set_gfp_mask(mapping, GFP_NOFS);
Nathan Scottce8e9222006-01-11 15:39:08 +11001605 btp->bt_mapping = mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 return 0;
1607}
1608
David Chinnera6867a62006-01-11 15:37:58 +11001609STATIC int
1610xfs_alloc_delwrite_queue(
Jan Engelhardte2a07812010-03-23 09:52:55 +11001611 xfs_buftarg_t *btp,
1612 const char *fsname)
David Chinnera6867a62006-01-11 15:37:58 +11001613{
1614 int error = 0;
1615
1616 INIT_LIST_HEAD(&btp->bt_list);
1617 INIT_LIST_HEAD(&btp->bt_delwrite_queue);
Eric Sandeen007c61c2007-10-11 17:43:56 +10001618 spin_lock_init(&btp->bt_delwrite_lock);
David Chinnera6867a62006-01-11 15:37:58 +11001619 btp->bt_flags = 0;
Jan Engelhardte2a07812010-03-23 09:52:55 +11001620 btp->bt_task = kthread_run(xfsbufd, btp, "xfsbufd/%s", fsname);
David Chinnera6867a62006-01-11 15:37:58 +11001621 if (IS_ERR(btp->bt_task)) {
1622 error = PTR_ERR(btp->bt_task);
1623 goto out_error;
1624 }
1625 xfs_register_buftarg(btp);
1626out_error:
1627 return error;
1628}
1629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630xfs_buftarg_t *
1631xfs_alloc_buftarg(
Dave Chinnerebad8612010-09-22 10:47:20 +10001632 struct xfs_mount *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 struct block_device *bdev,
Jan Engelhardte2a07812010-03-23 09:52:55 +11001634 int external,
1635 const char *fsname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
1637 xfs_buftarg_t *btp;
1638
1639 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
1640
Dave Chinnerebad8612010-09-22 10:47:20 +10001641 btp->bt_mount = mp;
Nathan Scottce8e9222006-01-11 15:39:08 +11001642 btp->bt_dev = bdev->bd_dev;
1643 btp->bt_bdev = bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 if (xfs_setsize_buftarg_early(btp, bdev))
1645 goto error;
1646 if (xfs_mapping_buftarg(btp, bdev))
1647 goto error;
Jan Engelhardte2a07812010-03-23 09:52:55 +11001648 if (xfs_alloc_delwrite_queue(btp, fsname))
David Chinnera6867a62006-01-11 15:37:58 +11001649 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 return btp;
1651
1652error:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001653 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 return NULL;
1655}
1656
1657
1658/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001659 * Delayed write buffer handling
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001662xfs_buf_delwri_queue(
1663 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 int unlock)
1665{
Nathan Scottce8e9222006-01-11 15:39:08 +11001666 struct list_head *dwq = &bp->b_target->bt_delwrite_queue;
1667 spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
David Chinnera6867a62006-01-11 15:37:58 +11001668
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001669 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
1670
Nathan Scottce8e9222006-01-11 15:39:08 +11001671 ASSERT((bp->b_flags&(XBF_DELWRI|XBF_ASYNC)) == (XBF_DELWRI|XBF_ASYNC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
David Chinnera6867a62006-01-11 15:37:58 +11001673 spin_lock(dwlk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 /* If already in the queue, dequeue and place at tail */
Nathan Scottce8e9222006-01-11 15:39:08 +11001675 if (!list_empty(&bp->b_list)) {
1676 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1677 if (unlock)
1678 atomic_dec(&bp->b_hold);
1679 list_del(&bp->b_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 }
1681
Dave Chinnerc9c12972010-01-11 11:49:59 +00001682 if (list_empty(dwq)) {
1683 /* start xfsbufd as it is about to have something to do */
1684 wake_up_process(bp->b_target->bt_task);
1685 }
1686
Nathan Scottce8e9222006-01-11 15:39:08 +11001687 bp->b_flags |= _XBF_DELWRI_Q;
1688 list_add_tail(&bp->b_list, dwq);
1689 bp->b_queuetime = jiffies;
David Chinnera6867a62006-01-11 15:37:58 +11001690 spin_unlock(dwlk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
1692 if (unlock)
Nathan Scottce8e9222006-01-11 15:39:08 +11001693 xfs_buf_unlock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694}
1695
1696void
Nathan Scottce8e9222006-01-11 15:39:08 +11001697xfs_buf_delwri_dequeue(
1698 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699{
Nathan Scottce8e9222006-01-11 15:39:08 +11001700 spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 int dequeued = 0;
1702
David Chinnera6867a62006-01-11 15:37:58 +11001703 spin_lock(dwlk);
Nathan Scottce8e9222006-01-11 15:39:08 +11001704 if ((bp->b_flags & XBF_DELWRI) && !list_empty(&bp->b_list)) {
1705 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1706 list_del_init(&bp->b_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 dequeued = 1;
1708 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001709 bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q);
David Chinnera6867a62006-01-11 15:37:58 +11001710 spin_unlock(dwlk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712 if (dequeued)
Nathan Scottce8e9222006-01-11 15:39:08 +11001713 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001715 trace_xfs_buf_delwri_dequeue(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716}
1717
Dave Chinnerd808f612010-02-02 10:13:42 +11001718/*
1719 * If a delwri buffer needs to be pushed before it has aged out, then promote
1720 * it to the head of the delwri queue so that it will be flushed on the next
1721 * xfsbufd run. We do this by resetting the queuetime of the buffer to be older
1722 * than the age currently needed to flush the buffer. Hence the next time the
1723 * xfsbufd sees it is guaranteed to be considered old enough to flush.
1724 */
1725void
1726xfs_buf_delwri_promote(
1727 struct xfs_buf *bp)
1728{
1729 struct xfs_buftarg *btp = bp->b_target;
1730 long age = xfs_buf_age_centisecs * msecs_to_jiffies(10) + 1;
1731
1732 ASSERT(bp->b_flags & XBF_DELWRI);
1733 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1734
1735 /*
1736 * Check the buffer age before locking the delayed write queue as we
1737 * don't need to promote buffers that are already past the flush age.
1738 */
1739 if (bp->b_queuetime < jiffies - age)
1740 return;
1741 bp->b_queuetime = jiffies - age;
1742 spin_lock(&btp->bt_delwrite_lock);
1743 list_move(&bp->b_list, &btp->bt_delwrite_queue);
1744 spin_unlock(&btp->bt_delwrite_lock);
1745}
1746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001748xfs_buf_runall_queues(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 struct workqueue_struct *queue)
1750{
1751 flush_workqueue(queue);
1752}
1753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754STATIC int
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001755xfsbufd_wakeup(
Dave Chinner7f8275d2010-07-19 14:56:17 +10001756 struct shrinker *shrink,
Nathan Scott15c84a42005-11-04 10:51:01 +11001757 int priority,
1758 gfp_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
Christoph Hellwigda7f93e2006-01-11 20:49:57 +11001760 xfs_buftarg_t *btp;
David Chinnera6867a62006-01-11 15:37:58 +11001761
1762 spin_lock(&xfs_buftarg_lock);
Christoph Hellwigda7f93e2006-01-11 20:49:57 +11001763 list_for_each_entry(btp, &xfs_buftarg_list, bt_list) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001764 if (test_bit(XBT_FORCE_SLEEP, &btp->bt_flags))
David Chinnera6867a62006-01-11 15:37:58 +11001765 continue;
Dave Chinnerc9c12972010-01-11 11:49:59 +00001766 if (list_empty(&btp->bt_delwrite_queue))
1767 continue;
Nathan Scottce8e9222006-01-11 15:39:08 +11001768 set_bit(XBT_FORCE_FLUSH, &btp->bt_flags);
David Chinnera6867a62006-01-11 15:37:58 +11001769 wake_up_process(btp->bt_task);
1770 }
1771 spin_unlock(&xfs_buftarg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 return 0;
1773}
1774
David Chinner585e6d82007-02-10 18:32:29 +11001775/*
1776 * Move as many buffers as specified to the supplied list
1777 * idicating if we skipped any buffers to prevent deadlocks.
1778 */
1779STATIC int
1780xfs_buf_delwri_split(
1781 xfs_buftarg_t *target,
1782 struct list_head *list,
David Chinner5e6a07d2007-02-10 18:34:49 +11001783 unsigned long age)
David Chinner585e6d82007-02-10 18:32:29 +11001784{
1785 xfs_buf_t *bp, *n;
1786 struct list_head *dwq = &target->bt_delwrite_queue;
1787 spinlock_t *dwlk = &target->bt_delwrite_lock;
1788 int skipped = 0;
David Chinner5e6a07d2007-02-10 18:34:49 +11001789 int force;
David Chinner585e6d82007-02-10 18:32:29 +11001790
David Chinner5e6a07d2007-02-10 18:34:49 +11001791 force = test_and_clear_bit(XBT_FORCE_FLUSH, &target->bt_flags);
David Chinner585e6d82007-02-10 18:32:29 +11001792 INIT_LIST_HEAD(list);
1793 spin_lock(dwlk);
1794 list_for_each_entry_safe(bp, n, dwq, b_list) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001795 trace_xfs_buf_delwri_split(bp, _RET_IP_);
David Chinner585e6d82007-02-10 18:32:29 +11001796 ASSERT(bp->b_flags & XBF_DELWRI);
1797
Christoph Hellwig4d16e922010-06-23 18:11:15 +10001798 if (!XFS_BUF_ISPINNED(bp) && !xfs_buf_cond_lock(bp)) {
David Chinner5e6a07d2007-02-10 18:34:49 +11001799 if (!force &&
David Chinner585e6d82007-02-10 18:32:29 +11001800 time_before(jiffies, bp->b_queuetime + age)) {
1801 xfs_buf_unlock(bp);
1802 break;
1803 }
1804
1805 bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q|
1806 _XBF_RUN_QUEUES);
1807 bp->b_flags |= XBF_WRITE;
1808 list_move_tail(&bp->b_list, list);
1809 } else
1810 skipped++;
1811 }
1812 spin_unlock(dwlk);
1813
1814 return skipped;
1815
1816}
1817
Dave Chinner089716a2010-01-26 15:13:25 +11001818/*
1819 * Compare function is more complex than it needs to be because
1820 * the return value is only 32 bits and we are doing comparisons
1821 * on 64 bit values
1822 */
1823static int
1824xfs_buf_cmp(
1825 void *priv,
1826 struct list_head *a,
1827 struct list_head *b)
1828{
1829 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
1830 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
1831 xfs_daddr_t diff;
1832
1833 diff = ap->b_bn - bp->b_bn;
1834 if (diff < 0)
1835 return -1;
1836 if (diff > 0)
1837 return 1;
1838 return 0;
1839}
1840
1841void
1842xfs_buf_delwri_sort(
1843 xfs_buftarg_t *target,
1844 struct list_head *list)
1845{
1846 list_sort(NULL, list, xfs_buf_cmp);
1847}
1848
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849STATIC int
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001850xfsbufd(
David Chinner585e6d82007-02-10 18:32:29 +11001851 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
Dave Chinner089716a2010-01-26 15:13:25 +11001853 xfs_buftarg_t *target = (xfs_buftarg_t *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 current->flags |= PF_MEMALLOC;
1856
Rafael J. Wysocki978c7b22007-12-07 14:09:02 +11001857 set_freezable();
1858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 do {
Dave Chinnerc9c12972010-01-11 11:49:59 +00001860 long age = xfs_buf_age_centisecs * msecs_to_jiffies(10);
1861 long tout = xfs_buf_timer_centisecs * msecs_to_jiffies(10);
Dave Chinner089716a2010-01-26 15:13:25 +11001862 int count = 0;
1863 struct list_head tmp;
Dave Chinnerc9c12972010-01-11 11:49:59 +00001864
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001865 if (unlikely(freezing(current))) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001866 set_bit(XBT_FORCE_SLEEP, &target->bt_flags);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001867 refrigerator();
Nathan Scottabd0cf72005-05-05 13:30:13 -07001868 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +11001869 clear_bit(XBT_FORCE_SLEEP, &target->bt_flags);
Nathan Scottabd0cf72005-05-05 13:30:13 -07001870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Dave Chinnerc9c12972010-01-11 11:49:59 +00001872 /* sleep for a long time if there is nothing to do. */
1873 if (list_empty(&target->bt_delwrite_queue))
1874 tout = MAX_SCHEDULE_TIMEOUT;
1875 schedule_timeout_interruptible(tout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Dave Chinnerc9c12972010-01-11 11:49:59 +00001877 xfs_buf_delwri_split(target, &tmp, age);
Dave Chinner089716a2010-01-26 15:13:25 +11001878 list_sort(NULL, &tmp, xfs_buf_cmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 while (!list_empty(&tmp)) {
Dave Chinner089716a2010-01-26 15:13:25 +11001880 struct xfs_buf *bp;
1881 bp = list_first_entry(&tmp, struct xfs_buf, b_list);
Nathan Scottce8e9222006-01-11 15:39:08 +11001882 list_del_init(&bp->b_list);
Christoph Hellwig939d7232010-07-20 17:51:16 +10001883 xfs_bdstrat_cb(bp);
David Chinner585e6d82007-02-10 18:32:29 +11001884 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 }
Nathan Scottf07c2252006-09-28 10:52:15 +10001886 if (count)
1887 blk_run_address_space(target->bt_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Christoph Hellwig4df08c52005-09-05 08:34:18 +10001889 } while (!kthread_should_stop());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Christoph Hellwig4df08c52005-09-05 08:34:18 +10001891 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892}
1893
1894/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001895 * Go through all incore buffers, and release buffers if they belong to
1896 * the given device. This is used in filesystem error handling to
1897 * preserve the consistency of its metadata.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 */
1899int
1900xfs_flush_buftarg(
David Chinner585e6d82007-02-10 18:32:29 +11001901 xfs_buftarg_t *target,
1902 int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
Dave Chinner089716a2010-01-26 15:13:25 +11001904 xfs_buf_t *bp;
David Chinner585e6d82007-02-10 18:32:29 +11001905 int pincount = 0;
Dave Chinner089716a2010-01-26 15:13:25 +11001906 LIST_HEAD(tmp_list);
1907 LIST_HEAD(wait_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Dave Chinnerc626d172009-04-06 18:42:11 +02001909 xfs_buf_runall_queues(xfsconvertd_workqueue);
Nathan Scottce8e9222006-01-11 15:39:08 +11001910 xfs_buf_runall_queues(xfsdatad_workqueue);
1911 xfs_buf_runall_queues(xfslogd_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
David Chinner5e6a07d2007-02-10 18:34:49 +11001913 set_bit(XBT_FORCE_FLUSH, &target->bt_flags);
Dave Chinner089716a2010-01-26 15:13:25 +11001914 pincount = xfs_buf_delwri_split(target, &tmp_list, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
1916 /*
Dave Chinner089716a2010-01-26 15:13:25 +11001917 * Dropped the delayed write list lock, now walk the temporary list.
1918 * All I/O is issued async and then if we need to wait for completion
1919 * we do that after issuing all the IO.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 */
Dave Chinner089716a2010-01-26 15:13:25 +11001921 list_sort(NULL, &tmp_list, xfs_buf_cmp);
1922 while (!list_empty(&tmp_list)) {
1923 bp = list_first_entry(&tmp_list, struct xfs_buf, b_list);
David Chinner585e6d82007-02-10 18:32:29 +11001924 ASSERT(target == bp->b_target);
Dave Chinner089716a2010-01-26 15:13:25 +11001925 list_del_init(&bp->b_list);
1926 if (wait) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001927 bp->b_flags &= ~XBF_ASYNC;
Dave Chinner089716a2010-01-26 15:13:25 +11001928 list_add(&bp->b_list, &wait_list);
1929 }
Christoph Hellwig939d7232010-07-20 17:51:16 +10001930 xfs_bdstrat_cb(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 }
1932
Dave Chinner089716a2010-01-26 15:13:25 +11001933 if (wait) {
1934 /* Expedite and wait for IO to complete. */
Nathan Scottf07c2252006-09-28 10:52:15 +10001935 blk_run_address_space(target->bt_mapping);
Dave Chinner089716a2010-01-26 15:13:25 +11001936 while (!list_empty(&wait_list)) {
1937 bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
Nathan Scottf07c2252006-09-28 10:52:15 +10001938
Dave Chinner089716a2010-01-26 15:13:25 +11001939 list_del_init(&bp->b_list);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001940 xfs_buf_iowait(bp);
Dave Chinner089716a2010-01-26 15:13:25 +11001941 xfs_buf_relse(bp);
1942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 }
1944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 return pincount;
1946}
1947
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001948int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11001949xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
Nathan Scott87582802006-03-14 13:18:19 +11001951 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
1952 KM_ZONE_HWALIGN, NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11001953 if (!xfs_buf_zone)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001954 goto out;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001955
Dave Chinner51749e42010-09-08 09:00:22 +00001956 xfslogd_workqueue = alloc_workqueue("xfslogd",
1957 WQ_RESCUER | WQ_HIGHPRI, 1);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001958 if (!xfslogd_workqueue)
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001959 goto out_free_buf_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Rafael J. Wysockib4337692007-03-22 00:11:27 -08001961 xfsdatad_workqueue = create_workqueue("xfsdatad");
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001962 if (!xfsdatad_workqueue)
1963 goto out_destroy_xfslogd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Dave Chinnerc626d172009-04-06 18:42:11 +02001965 xfsconvertd_workqueue = create_workqueue("xfsconvertd");
1966 if (!xfsconvertd_workqueue)
1967 goto out_destroy_xfsdatad_workqueue;
1968
Rusty Russell8e1f9362007-07-17 04:03:17 -07001969 register_shrinker(&xfs_buf_shake);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001970 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Dave Chinnerc626d172009-04-06 18:42:11 +02001972 out_destroy_xfsdatad_workqueue:
1973 destroy_workqueue(xfsdatad_workqueue);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001974 out_destroy_xfslogd_workqueue:
1975 destroy_workqueue(xfslogd_workqueue);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001976 out_free_buf_zone:
Nathan Scottce8e9222006-01-11 15:39:08 +11001977 kmem_zone_destroy(xfs_buf_zone);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001978 out:
Nathan Scott87582802006-03-14 13:18:19 +11001979 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980}
1981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982void
Nathan Scottce8e9222006-01-11 15:39:08 +11001983xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984{
Rusty Russell8e1f9362007-07-17 04:03:17 -07001985 unregister_shrinker(&xfs_buf_shake);
Dave Chinnerc626d172009-04-06 18:42:11 +02001986 destroy_workqueue(xfsconvertd_workqueue);
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001987 destroy_workqueue(xfsdatad_workqueue);
1988 destroy_workqueue(xfslogd_workqueue);
Nathan Scottce8e9222006-01-11 15:39:08 +11001989 kmem_zone_destroy(xfs_buf_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990}
Tim Shimmine6a0e9c2007-05-08 13:49:59 +10001991
1992#ifdef CONFIG_KDB_MODULES
1993struct list_head *
1994xfs_get_buftarg_list(void)
1995{
1996 return &xfs_buftarg_list;
1997}
1998#endif