blob: 81f4ef27de3e852fa804c5114dfd9e2a408d92af [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scottf07c2252006-09-28 10:52:15 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Vlad Apostolov93c189c2006-11-11 18:03:49 +110018#include "xfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/stddef.h>
20#include <linux/errno.h>
21#include <linux/slab.h>
22#include <linux/pagemap.h>
23#include <linux/init.h>
24#include <linux/vmalloc.h>
25#include <linux/bio.h>
26#include <linux/sysctl.h>
27#include <linux/proc_fs.h>
28#include <linux/workqueue.h>
29#include <linux/percpu.h>
30#include <linux/blkdev.h>
31#include <linux/hash.h>
Christoph Hellwig4df08c52005-09-05 08:34:18 +100032#include <linux/kthread.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080033#include <linux/migrate.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070034#include <linux/backing-dev.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080035#include <linux/freezer.h>
Dave Chinner089716a2010-01-26 15:13:25 +110036#include <linux/list_sort.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Christoph Hellwigb7963132009-03-03 14:48:37 -050038#include "xfs_sb.h"
39#include "xfs_inum.h"
40#include "xfs_ag.h"
41#include "xfs_dmapi.h"
42#include "xfs_mount.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000043#include "xfs_trace.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050044
David Chinner7989cb82007-02-10 18:34:56 +110045static kmem_zone_t *xfs_buf_zone;
David Chinnera6867a62006-01-11 15:37:58 +110046STATIC int xfsbufd(void *);
Al Viro27496a82005-10-21 03:20:48 -040047STATIC int xfsbufd_wakeup(int, gfp_t);
Nathan Scottce8e9222006-01-11 15:39:08 +110048STATIC void xfs_buf_delwri_queue(xfs_buf_t *, int);
Rusty Russell8e1f9362007-07-17 04:03:17 -070049static struct shrinker xfs_buf_shake = {
50 .shrink = xfsbufd_wakeup,
51 .seeks = DEFAULT_SEEKS,
52};
Christoph Hellwig23ea4032005-06-21 15:14:01 +100053
David Chinner7989cb82007-02-10 18:34:56 +110054static struct workqueue_struct *xfslogd_workqueue;
Christoph Hellwig0829c362005-09-02 16:58:49 +100055struct workqueue_struct *xfsdatad_workqueue;
Dave Chinnerc626d172009-04-06 18:42:11 +020056struct workqueue_struct *xfsconvertd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Nathan Scottce8e9222006-01-11 15:39:08 +110058#ifdef XFS_BUF_LOCK_TRACKING
59# define XB_SET_OWNER(bp) ((bp)->b_last_holder = current->pid)
60# define XB_CLEAR_OWNER(bp) ((bp)->b_last_holder = -1)
61# define XB_GET_OWNER(bp) ((bp)->b_last_holder)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#else
Nathan Scottce8e9222006-01-11 15:39:08 +110063# define XB_SET_OWNER(bp) do { } while (0)
64# define XB_CLEAR_OWNER(bp) do { } while (0)
65# define XB_GET_OWNER(bp) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#endif
67
Nathan Scottce8e9222006-01-11 15:39:08 +110068#define xb_to_gfp(flags) \
69 ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : \
70 ((flags) & XBF_DONT_BLOCK) ? GFP_NOFS : GFP_KERNEL) | __GFP_NOWARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Nathan Scottce8e9222006-01-11 15:39:08 +110072#define xb_to_km(flags) \
73 (((flags) & XBF_DONT_BLOCK) ? KM_NOFS : KM_SLEEP)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Nathan Scottce8e9222006-01-11 15:39:08 +110075#define xfs_buf_allocate(flags) \
76 kmem_zone_alloc(xfs_buf_zone, xb_to_km(flags))
77#define xfs_buf_deallocate(bp) \
78 kmem_zone_free(xfs_buf_zone, (bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
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);
191 INIT_LIST_HEAD(&bp->b_hash_list);
192 init_MUTEX_LOCKED(&bp->b_sema); /* held, no waiters */
193 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
Nathan Scottce8e9222006-01-11 15:39:08 +1100265 ASSERT(list_empty(&bp->b_hash_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000267 if (bp->b_flags & (_XBF_PAGE_CACHE|_XBF_PAGES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 uint i;
269
James Bottomley73c77e22010-01-25 11:42:24 -0600270 if (xfs_buf_is_vmapped(bp))
Alex Eldercd9640a2010-03-16 18:55:54 +0000271 vunmap(bp->b_addr - bp->b_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Nathan Scott948ecdb2006-09-28 11:03:13 +1000273 for (i = 0; i < bp->b_page_count; i++) {
274 struct page *page = bp->b_pages[i];
275
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000276 if (bp->b_flags & _XBF_PAGE_CACHE)
277 ASSERT(!PagePrivate(page));
Nathan Scott948ecdb2006-09-28 11:03:13 +1000278 page_cache_release(page);
279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
Dave Chinner3fc98b12009-12-14 23:11:57 +0000281 _xfs_buf_free_pages(bp);
Nathan Scottce8e9222006-01-11 15:39:08 +1100282 xfs_buf_deallocate(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
285/*
286 * Finds all pages for buffer in question and builds it's page list.
287 */
288STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100289_xfs_buf_lookup_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 xfs_buf_t *bp,
291 uint flags)
292{
Nathan Scottce8e9222006-01-11 15:39:08 +1100293 struct address_space *mapping = bp->b_target->bt_mapping;
294 size_t blocksize = bp->b_target->bt_bsize;
295 size_t size = bp->b_count_desired;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 size_t nbytes, offset;
Nathan Scottce8e9222006-01-11 15:39:08 +1100297 gfp_t gfp_mask = xb_to_gfp(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 unsigned short page_count, i;
299 pgoff_t first;
Nathan Scott204ab252006-01-11 20:50:22 +1100300 xfs_off_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 int error;
302
Nathan Scottce8e9222006-01-11 15:39:08 +1100303 end = bp->b_file_offset + bp->b_buffer_length;
304 page_count = xfs_buf_btoc(end) - xfs_buf_btoct(bp->b_file_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Nathan Scottce8e9222006-01-11 15:39:08 +1100306 error = _xfs_buf_get_pages(bp, page_count, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (unlikely(error))
308 return error;
Nathan Scottce8e9222006-01-11 15:39:08 +1100309 bp->b_flags |= _XBF_PAGE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Nathan Scottce8e9222006-01-11 15:39:08 +1100311 offset = bp->b_offset;
312 first = bp->b_file_offset >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Nathan Scottce8e9222006-01-11 15:39:08 +1100314 for (i = 0; i < bp->b_page_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 struct page *page;
316 uint retries = 0;
317
318 retry:
319 page = find_or_create_page(mapping, first + i, gfp_mask);
320 if (unlikely(page == NULL)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100321 if (flags & XBF_READ_AHEAD) {
322 bp->b_page_count = i;
Christoph Hellwig6ab455e2008-05-19 16:34:42 +1000323 for (i = 0; i < bp->b_page_count; i++)
324 unlock_page(bp->b_pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return -ENOMEM;
326 }
327
328 /*
329 * This could deadlock.
330 *
331 * But until all the XFS lowlevel code is revamped to
332 * handle buffer allocation failures we can't do much.
333 */
334 if (!(++retries % 100))
335 printk(KERN_ERR
336 "XFS: possible memory allocation "
337 "deadlock in %s (mode:0x%x)\n",
Harvey Harrison34a622b2008-04-10 12:19:21 +1000338 __func__, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Nathan Scottce8e9222006-01-11 15:39:08 +1100340 XFS_STATS_INC(xb_page_retries);
Christoph Hellwig23ea4032005-06-21 15:14:01 +1000341 xfsbufd_wakeup(0, gfp_mask);
Jens Axboe8aa7e842009-07-09 14:52:32 +0200342 congestion_wait(BLK_RW_ASYNC, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 goto retry;
344 }
345
Nathan Scottce8e9222006-01-11 15:39:08 +1100346 XFS_STATS_INC(xb_page_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 nbytes = min_t(size_t, size, PAGE_CACHE_SIZE - offset);
349 size -= nbytes;
350
Nathan Scott948ecdb2006-09-28 11:03:13 +1000351 ASSERT(!PagePrivate(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (!PageUptodate(page)) {
353 page_count--;
Christoph Hellwig6ab455e2008-05-19 16:34:42 +1000354 if (blocksize >= PAGE_CACHE_SIZE) {
355 if (flags & XBF_READ)
356 bp->b_flags |= _XBF_PAGE_LOCKED;
357 } else if (!PagePrivate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (test_page_region(page, offset, nbytes))
359 page_count++;
360 }
361 }
362
Nathan Scottce8e9222006-01-11 15:39:08 +1100363 bp->b_pages[i] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 offset = 0;
365 }
366
Christoph Hellwig6ab455e2008-05-19 16:34:42 +1000367 if (!(bp->b_flags & _XBF_PAGE_LOCKED)) {
368 for (i = 0; i < bp->b_page_count; i++)
369 unlock_page(bp->b_pages[i]);
370 }
371
Nathan Scottce8e9222006-01-11 15:39:08 +1100372 if (page_count == bp->b_page_count)
373 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return error;
376}
377
378/*
379 * Map buffer into kernel address-space if nessecary.
380 */
381STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100382_xfs_buf_map_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 xfs_buf_t *bp,
384 uint flags)
385{
386 /* A single page buffer is always mappable */
Nathan Scottce8e9222006-01-11 15:39:08 +1100387 if (bp->b_page_count == 1) {
388 bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
389 bp->b_flags |= XBF_MAPPED;
390 } else if (flags & XBF_MAPPED) {
Felix Blyakhercf7dab82009-02-18 15:41:28 -0600391 bp->b_addr = vmap(bp->b_pages, bp->b_page_count,
392 VM_MAP, PAGE_KERNEL);
Nathan Scottce8e9222006-01-11 15:39:08 +1100393 if (unlikely(bp->b_addr == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return -ENOMEM;
Nathan Scottce8e9222006-01-11 15:39:08 +1100395 bp->b_addr += bp->b_offset;
396 bp->b_flags |= XBF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398
399 return 0;
400}
401
402/*
403 * Finding and Reading Buffers
404 */
405
406/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100407 * Look up, and creates if absent, a lockable buffer for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 * a given range of an inode. The buffer is returned
409 * locked. If other overlapping buffers exist, they are
410 * released before the new buffer is created and locked,
411 * which may imply that this call will block until those buffers
412 * are unlocked. No I/O is implied by this call.
413 */
414xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100415_xfs_buf_find(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 xfs_buftarg_t *btp, /* block device target */
Nathan Scott204ab252006-01-11 20:50:22 +1100417 xfs_off_t ioff, /* starting offset of range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 size_t isize, /* length of range */
Nathan Scottce8e9222006-01-11 15:39:08 +1100419 xfs_buf_flags_t flags,
420 xfs_buf_t *new_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Nathan Scott204ab252006-01-11 20:50:22 +1100422 xfs_off_t range_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 size_t range_length;
424 xfs_bufhash_t *hash;
Nathan Scottce8e9222006-01-11 15:39:08 +1100425 xfs_buf_t *bp, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 range_base = (ioff << BBSHIFT);
428 range_length = (isize << BBSHIFT);
429
430 /* Check for IOs smaller than the sector size / not sector aligned */
Nathan Scottce8e9222006-01-11 15:39:08 +1100431 ASSERT(!(range_length < (1 << btp->bt_sshift)));
Nathan Scott204ab252006-01-11 20:50:22 +1100432 ASSERT(!(range_base & (xfs_off_t)btp->bt_smask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 hash = &btp->bt_hash[hash_long((unsigned long)ioff, btp->bt_hashshift)];
435
436 spin_lock(&hash->bh_lock);
437
Nathan Scottce8e9222006-01-11 15:39:08 +1100438 list_for_each_entry_safe(bp, n, &hash->bh_list, b_hash_list) {
439 ASSERT(btp == bp->b_target);
440 if (bp->b_file_offset == range_base &&
441 bp->b_buffer_length == range_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 /*
Nathan Scottce8e9222006-01-11 15:39:08 +1100443 * If we look at something, bring it to the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 * front of the list for next time.
445 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100446 atomic_inc(&bp->b_hold);
447 list_move(&bp->b_hash_list, &hash->bh_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 goto found;
449 }
450 }
451
452 /* No match found */
Nathan Scottce8e9222006-01-11 15:39:08 +1100453 if (new_bp) {
454 _xfs_buf_initialize(new_bp, btp, range_base,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 range_length, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100456 new_bp->b_hash = hash;
457 list_add(&new_bp->b_hash_list, &hash->bh_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100459 XFS_STATS_INC(xb_miss_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
461
462 spin_unlock(&hash->bh_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +1100463 return new_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465found:
466 spin_unlock(&hash->bh_lock);
467
468 /* Attempt to get the semaphore without sleeping,
469 * if this does not work then we need to drop the
470 * spinlock and do a hard attempt on the semaphore.
471 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100472 if (down_trylock(&bp->b_sema)) {
473 if (!(flags & XBF_TRYLOCK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 /* wait for buffer ownership */
Nathan Scottce8e9222006-01-11 15:39:08 +1100475 xfs_buf_lock(bp);
476 XFS_STATS_INC(xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 } else {
478 /* We asked for a trylock and failed, no need
479 * to look at file offset and length here, we
Nathan Scottce8e9222006-01-11 15:39:08 +1100480 * know that this buffer at least overlaps our
481 * buffer and is locked, therefore our buffer
482 * either does not exist, or is this buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100484 xfs_buf_rele(bp);
485 XFS_STATS_INC(xb_busy_locked);
486 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
488 } else {
489 /* trylock worked */
Nathan Scottce8e9222006-01-11 15:39:08 +1100490 XB_SET_OWNER(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
492
Nathan Scottce8e9222006-01-11 15:39:08 +1100493 if (bp->b_flags & XBF_STALE) {
494 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
495 bp->b_flags &= XBF_MAPPED;
David Chinner2f926582005-09-05 08:33:35 +1000496 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000497
498 trace_xfs_buf_find(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100499 XFS_STATS_INC(xb_get_locked);
500 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
503/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100504 * Assembles a buffer covering the specified range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 * Storage in memory for all portions of the buffer will be allocated,
506 * although backing storage may not be.
507 */
508xfs_buf_t *
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000509xfs_buf_get(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 xfs_buftarg_t *target,/* target for buffer */
Nathan Scott204ab252006-01-11 20:50:22 +1100511 xfs_off_t ioff, /* starting offset of range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 size_t isize, /* length of range */
Nathan Scottce8e9222006-01-11 15:39:08 +1100513 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Nathan Scottce8e9222006-01-11 15:39:08 +1100515 xfs_buf_t *bp, *new_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 int error = 0, i;
517
Nathan Scottce8e9222006-01-11 15:39:08 +1100518 new_bp = xfs_buf_allocate(flags);
519 if (unlikely(!new_bp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return NULL;
521
Nathan Scottce8e9222006-01-11 15:39:08 +1100522 bp = _xfs_buf_find(target, ioff, isize, flags, new_bp);
523 if (bp == new_bp) {
524 error = _xfs_buf_lookup_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if (error)
526 goto no_buffer;
527 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100528 xfs_buf_deallocate(new_bp);
529 if (unlikely(bp == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return NULL;
531 }
532
Nathan Scottce8e9222006-01-11 15:39:08 +1100533 for (i = 0; i < bp->b_page_count; i++)
534 mark_page_accessed(bp->b_pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Nathan Scottce8e9222006-01-11 15:39:08 +1100536 if (!(bp->b_flags & XBF_MAPPED)) {
537 error = _xfs_buf_map_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if (unlikely(error)) {
539 printk(KERN_WARNING "%s: failed to map pages\n",
Harvey Harrison34a622b2008-04-10 12:19:21 +1000540 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 goto no_buffer;
542 }
543 }
544
Nathan Scottce8e9222006-01-11 15:39:08 +1100545 XFS_STATS_INC(xb_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 /*
548 * Always fill in the block number now, the mapped cases can do
549 * their own overlay of this later.
550 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100551 bp->b_bn = ioff;
552 bp->b_count_desired = bp->b_buffer_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000554 trace_xfs_buf_get(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100555 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100558 if (flags & (XBF_LOCK | XBF_TRYLOCK))
559 xfs_buf_unlock(bp);
560 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return NULL;
562}
563
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100564STATIC int
565_xfs_buf_read(
566 xfs_buf_t *bp,
567 xfs_buf_flags_t flags)
568{
569 int status;
570
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100571 ASSERT(!(flags & (XBF_DELWRI|XBF_WRITE)));
572 ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL);
573
574 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_DELWRI | \
575 XBF_READ_AHEAD | _XBF_RUN_QUEUES);
576 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | \
577 XBF_READ_AHEAD | _XBF_RUN_QUEUES);
578
579 status = xfs_buf_iorequest(bp);
580 if (!status && !(flags & XBF_ASYNC))
581 status = xfs_buf_iowait(bp);
582 return status;
583}
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585xfs_buf_t *
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000586xfs_buf_read(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100588 xfs_off_t ioff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 size_t isize,
Nathan Scottce8e9222006-01-11 15:39:08 +1100590 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Nathan Scottce8e9222006-01-11 15:39:08 +1100592 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Nathan Scottce8e9222006-01-11 15:39:08 +1100594 flags |= XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000596 bp = xfs_buf_get(target, ioff, isize, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100597 if (bp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000598 trace_xfs_buf_read(bp, flags, _RET_IP_);
599
Nathan Scottce8e9222006-01-11 15:39:08 +1100600 if (!XFS_BUF_ISDONE(bp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100601 XFS_STATS_INC(xb_get_read);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100602 _xfs_buf_read(bp, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100603 } else if (flags & XBF_ASYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 /*
605 * Read ahead call which is already satisfied,
606 * drop the buffer
607 */
608 goto no_buffer;
609 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 /* We do not want read in the flags */
Nathan Scottce8e9222006-01-11 15:39:08 +1100611 bp->b_flags &= ~XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
613 }
614
Nathan Scottce8e9222006-01-11 15:39:08 +1100615 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100618 if (flags & (XBF_LOCK | XBF_TRYLOCK))
619 xfs_buf_unlock(bp);
620 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return NULL;
622}
623
624/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100625 * If we are not low on memory then do the readahead in a deadlock
626 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 */
628void
Nathan Scottce8e9222006-01-11 15:39:08 +1100629xfs_buf_readahead(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100631 xfs_off_t ioff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 size_t isize,
Nathan Scottce8e9222006-01-11 15:39:08 +1100633 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
635 struct backing_dev_info *bdi;
636
Nathan Scottce8e9222006-01-11 15:39:08 +1100637 bdi = target->bt_mapping->backing_dev_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (bdi_read_congested(bdi))
639 return;
640
Nathan Scottce8e9222006-01-11 15:39:08 +1100641 flags |= (XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD);
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000642 xfs_buf_read(target, ioff, isize, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
645xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100646xfs_buf_get_empty(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 size_t len,
648 xfs_buftarg_t *target)
649{
Nathan Scottce8e9222006-01-11 15:39:08 +1100650 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Nathan Scottce8e9222006-01-11 15:39:08 +1100652 bp = xfs_buf_allocate(0);
653 if (bp)
654 _xfs_buf_initialize(bp, target, 0, len, 0);
655 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
658static inline struct page *
659mem_to_page(
660 void *addr)
661{
Christoph Lameter9e2779f2008-02-04 22:28:34 -0800662 if ((!is_vmalloc_addr(addr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return virt_to_page(addr);
664 } else {
665 return vmalloc_to_page(addr);
666 }
667}
668
669int
Nathan Scottce8e9222006-01-11 15:39:08 +1100670xfs_buf_associate_memory(
671 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 void *mem,
673 size_t len)
674{
675 int rval;
676 int i = 0;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100677 unsigned long pageaddr;
678 unsigned long offset;
679 size_t buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 int page_count;
681
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100682 pageaddr = (unsigned long)mem & PAGE_CACHE_MASK;
683 offset = (unsigned long)mem - pageaddr;
684 buflen = PAGE_CACHE_ALIGN(len + offset);
685 page_count = buflen >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 /* Free any previous set of page pointers */
Nathan Scottce8e9222006-01-11 15:39:08 +1100688 if (bp->b_pages)
689 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Nathan Scottce8e9222006-01-11 15:39:08 +1100691 bp->b_pages = NULL;
692 bp->b_addr = mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Christoph Hellwig36fae172009-07-18 18:14:58 -0400694 rval = _xfs_buf_get_pages(bp, page_count, XBF_DONT_BLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (rval)
696 return rval;
697
Nathan Scottce8e9222006-01-11 15:39:08 +1100698 bp->b_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100700 for (i = 0; i < bp->b_page_count; i++) {
701 bp->b_pages[i] = mem_to_page((void *)pageaddr);
702 pageaddr += PAGE_CACHE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100705 bp->b_count_desired = len;
706 bp->b_buffer_length = buflen;
Nathan Scottce8e9222006-01-11 15:39:08 +1100707 bp->b_flags |= XBF_MAPPED;
Christoph Hellwig6ab455e2008-05-19 16:34:42 +1000708 bp->b_flags &= ~_XBF_PAGE_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 return 0;
711}
712
713xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100714xfs_buf_get_noaddr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 size_t len,
716 xfs_buftarg_t *target)
717{
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000718 unsigned long page_count = PAGE_ALIGN(len) >> PAGE_SHIFT;
719 int error, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Nathan Scottce8e9222006-01-11 15:39:08 +1100722 bp = xfs_buf_allocate(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (unlikely(bp == NULL))
724 goto fail;
Nathan Scottce8e9222006-01-11 15:39:08 +1100725 _xfs_buf_initialize(bp, target, 0, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000727 error = _xfs_buf_get_pages(bp, page_count, 0);
728 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 goto fail_free_buf;
730
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000731 for (i = 0; i < page_count; i++) {
732 bp->b_pages[i] = alloc_page(GFP_KERNEL);
733 if (!bp->b_pages[i])
734 goto fail_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000736 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000738 error = _xfs_buf_map_pages(bp, XBF_MAPPED);
739 if (unlikely(error)) {
740 printk(KERN_WARNING "%s: failed to map pages\n",
Harvey Harrison34a622b2008-04-10 12:19:21 +1000741 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 goto fail_free_mem;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Nathan Scottce8e9222006-01-11 15:39:08 +1100745 xfs_buf_unlock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000747 trace_xfs_buf_get_noaddr(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return bp;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 fail_free_mem:
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000751 while (--i >= 0)
752 __free_page(bp->b_pages[i]);
Christoph Hellwigca165b82007-05-24 15:21:11 +1000753 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 fail_free_buf:
Christoph Hellwigca165b82007-05-24 15:21:11 +1000755 xfs_buf_deallocate(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 fail:
757 return NULL;
758}
759
760/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 * Increment reference count on buffer, to hold the buffer concurrently
762 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 * Must hold the buffer already to call this function.
764 */
765void
Nathan Scottce8e9222006-01-11 15:39:08 +1100766xfs_buf_hold(
767 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000769 trace_xfs_buf_hold(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100770 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
773/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100774 * Releases a hold on the specified buffer. If the
775 * the hold count is 1, calls xfs_buf_free.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 */
777void
Nathan Scottce8e9222006-01-11 15:39:08 +1100778xfs_buf_rele(
779 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Nathan Scottce8e9222006-01-11 15:39:08 +1100781 xfs_bufhash_t *hash = bp->b_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000783 trace_xfs_buf_rele(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Nathan Scottfad3aa12006-02-01 12:14:52 +1100785 if (unlikely(!hash)) {
786 ASSERT(!bp->b_relse);
787 if (atomic_dec_and_test(&bp->b_hold))
788 xfs_buf_free(bp);
789 return;
790 }
791
Lachlan McIlroy37906892008-08-13 15:42:10 +1000792 ASSERT(atomic_read(&bp->b_hold) > 0);
Nathan Scottce8e9222006-01-11 15:39:08 +1100793 if (atomic_dec_and_lock(&bp->b_hold, &hash->bh_lock)) {
794 if (bp->b_relse) {
795 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 spin_unlock(&hash->bh_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +1100797 (*(bp->b_relse)) (bp);
798 } else if (bp->b_flags & XBF_FS_MANAGED) {
Christoph Hellwig7f14d0a2005-11-02 15:09:35 +1100799 spin_unlock(&hash->bh_lock);
800 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100801 ASSERT(!(bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)));
802 list_del_init(&bp->b_hash_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 spin_unlock(&hash->bh_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +1100804 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
806 }
807}
808
809
810/*
811 * Mutual exclusion on buffers. Locking model:
812 *
813 * Buffers associated with inodes for which buffer locking
814 * is not enabled are not protected by semaphores, and are
815 * assumed to be exclusively owned by the caller. There is a
816 * spinlock in the buffer, used by the caller when concurrent
817 * access is possible.
818 */
819
820/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100821 * Locks a buffer object, if it is not already locked.
822 * Note that this in no way locks the underlying pages, so it is only
823 * useful for synchronizing concurrent use of buffer objects, not for
824 * synchronizing independent access to the underlying pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 */
826int
Nathan Scottce8e9222006-01-11 15:39:08 +1100827xfs_buf_cond_lock(
828 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
830 int locked;
831
Nathan Scottce8e9222006-01-11 15:39:08 +1100832 locked = down_trylock(&bp->b_sema) == 0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000833 if (locked)
Nathan Scottce8e9222006-01-11 15:39:08 +1100834 XB_SET_OWNER(bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000835
836 trace_xfs_buf_cond_lock(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100837 return locked ? 0 : -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840int
Nathan Scottce8e9222006-01-11 15:39:08 +1100841xfs_buf_lock_value(
842 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Stephen Rothwelladaa6932008-04-22 15:26:13 +1000844 return bp->b_sema.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100848 * Locks a buffer object.
849 * Note that this in no way locks the underlying pages, so it is only
850 * useful for synchronizing concurrent use of buffer objects, not for
851 * synchronizing independent access to the underlying pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100853void
854xfs_buf_lock(
855 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000857 trace_xfs_buf_lock(bp, _RET_IP_);
858
Nathan Scottce8e9222006-01-11 15:39:08 +1100859 if (atomic_read(&bp->b_io_remaining))
860 blk_run_address_space(bp->b_target->bt_mapping);
861 down(&bp->b_sema);
862 XB_SET_OWNER(bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000863
864 trace_xfs_buf_lock_done(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
866
867/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100868 * Releases the lock on the buffer object.
David Chinner2f926582005-09-05 08:33:35 +1000869 * If the buffer is marked delwri but is not queued, do so before we
Nathan Scottce8e9222006-01-11 15:39:08 +1100870 * unlock the buffer as we need to set flags correctly. We also need to
David Chinner2f926582005-09-05 08:33:35 +1000871 * take a reference for the delwri queue because the unlocker is going to
872 * drop their's and they don't know we just queued it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 */
874void
Nathan Scottce8e9222006-01-11 15:39:08 +1100875xfs_buf_unlock(
876 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Nathan Scottce8e9222006-01-11 15:39:08 +1100878 if ((bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)) == XBF_DELWRI) {
879 atomic_inc(&bp->b_hold);
880 bp->b_flags |= XBF_ASYNC;
881 xfs_buf_delwri_queue(bp, 0);
David Chinner2f926582005-09-05 08:33:35 +1000882 }
883
Nathan Scottce8e9222006-01-11 15:39:08 +1100884 XB_CLEAR_OWNER(bp);
885 up(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000886
887 trace_xfs_buf_unlock(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
889
890
891/*
892 * Pinning Buffer Storage in Memory
Nathan Scottce8e9222006-01-11 15:39:08 +1100893 * Ensure that no attempt to force a buffer to disk will succeed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 */
895void
Nathan Scottce8e9222006-01-11 15:39:08 +1100896xfs_buf_pin(
897 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000899 trace_xfs_buf_pin(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100900 atomic_inc(&bp->b_pin_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903void
Nathan Scottce8e9222006-01-11 15:39:08 +1100904xfs_buf_unpin(
905 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000907 trace_xfs_buf_unpin(bp, _RET_IP_);
908
Nathan Scottce8e9222006-01-11 15:39:08 +1100909 if (atomic_dec_and_test(&bp->b_pin_count))
910 wake_up_all(&bp->b_waiters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912
913int
Nathan Scottce8e9222006-01-11 15:39:08 +1100914xfs_buf_ispin(
915 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Nathan Scottce8e9222006-01-11 15:39:08 +1100917 return atomic_read(&bp->b_pin_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
Nathan Scottce8e9222006-01-11 15:39:08 +1100920STATIC void
921xfs_buf_wait_unpin(
922 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
924 DECLARE_WAITQUEUE (wait, current);
925
Nathan Scottce8e9222006-01-11 15:39:08 +1100926 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 return;
928
Nathan Scottce8e9222006-01-11 15:39:08 +1100929 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 for (;;) {
931 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +1100932 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 break;
Nathan Scottce8e9222006-01-11 15:39:08 +1100934 if (atomic_read(&bp->b_io_remaining))
935 blk_run_address_space(bp->b_target->bt_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 schedule();
937 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100938 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 set_current_state(TASK_RUNNING);
940}
941
942/*
943 * Buffer Utility Routines
944 */
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100947xfs_buf_iodone_work(
David Howellsc4028952006-11-22 14:57:56 +0000948 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
David Howellsc4028952006-11-22 14:57:56 +0000950 xfs_buf_t *bp =
951 container_of(work, xfs_buf_t, b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
David Chinner0bfefc42007-05-14 18:24:23 +1000953 /*
954 * We can get an EOPNOTSUPP to ordered writes. Here we clear the
955 * ordered flag and reissue them. Because we can't tell the higher
956 * layers directly that they should not issue ordered I/O anymore, they
Christoph Hellwig73f6aa42008-10-10 17:28:29 +1100957 * need to check if the _XFS_BARRIER_FAILED flag was set during I/O completion.
David Chinner0bfefc42007-05-14 18:24:23 +1000958 */
959 if ((bp->b_error == EOPNOTSUPP) &&
960 (bp->b_flags & (XBF_ORDERED|XBF_ASYNC)) == (XBF_ORDERED|XBF_ASYNC)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000961 trace_xfs_buf_ordered_retry(bp, _RET_IP_);
David Chinner0bfefc42007-05-14 18:24:23 +1000962 bp->b_flags &= ~XBF_ORDERED;
Christoph Hellwig73f6aa42008-10-10 17:28:29 +1100963 bp->b_flags |= _XFS_BARRIER_FAILED;
David Chinner0bfefc42007-05-14 18:24:23 +1000964 xfs_buf_iorequest(bp);
965 } else if (bp->b_iodone)
Nathan Scottce8e9222006-01-11 15:39:08 +1100966 (*(bp->b_iodone))(bp);
967 else if (bp->b_flags & XBF_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 xfs_buf_relse(bp);
969}
970
971void
Nathan Scottce8e9222006-01-11 15:39:08 +1100972xfs_buf_ioend(
973 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 int schedule)
975{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000976 trace_xfs_buf_iodone(bp, _RET_IP_);
977
Lachlan McIlroy77be55a2007-11-23 16:31:00 +1100978 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
Nathan Scottce8e9222006-01-11 15:39:08 +1100979 if (bp->b_error == 0)
980 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Nathan Scottce8e9222006-01-11 15:39:08 +1100982 if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if (schedule) {
David Howellsc4028952006-11-22 14:57:56 +0000984 INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
Nathan Scottce8e9222006-01-11 15:39:08 +1100985 queue_work(xfslogd_workqueue, &bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 } else {
David Howellsc4028952006-11-22 14:57:56 +0000987 xfs_buf_iodone_work(&bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
989 } else {
David Chinnerb4dd3302008-08-13 16:36:11 +1000990 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
992}
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994void
Nathan Scottce8e9222006-01-11 15:39:08 +1100995xfs_buf_ioerror(
996 xfs_buf_t *bp,
997 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
999 ASSERT(error >= 0 && error <= 0xffff);
Nathan Scottce8e9222006-01-11 15:39:08 +11001000 bp->b_error = (unsigned short)error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001001 trace_xfs_buf_ioerror(bp, error, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004int
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001005xfs_bwrite(
1006 struct xfs_mount *mp,
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001007 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001009 int iowait = (bp->b_flags & XBF_ASYNC) == 0;
1010 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001012 bp->b_strat = xfs_bdstrat_cb;
1013 bp->b_mount = mp;
1014 bp->b_flags |= XBF_WRITE;
1015 if (!iowait)
1016 bp->b_flags |= _XBF_RUN_QUEUES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001018 xfs_buf_delwri_dequeue(bp);
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001019 xfs_buf_iostrategy(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001021 if (iowait) {
1022 error = xfs_buf_iowait(bp);
1023 if (error)
1024 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1025 xfs_buf_relse(bp);
1026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001028 return error;
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001029}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001031void
1032xfs_bdwrite(
1033 void *mp,
1034 struct xfs_buf *bp)
1035{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001036 trace_xfs_buf_bdwrite(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001038 bp->b_strat = xfs_bdstrat_cb;
Christoph Hellwig15ac08a2008-12-09 04:47:30 -05001039 bp->b_mount = mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001041 bp->b_flags &= ~XBF_READ;
1042 bp->b_flags |= (XBF_DELWRI | XBF_ASYNC);
1043
1044 xfs_buf_delwri_queue(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
1046
Christoph Hellwig4e234712010-01-13 22:17:56 +00001047/*
1048 * Called when we want to stop a buffer from getting written or read.
1049 * We attach the EIO error, muck with its flags, and call biodone
1050 * so that the proper iodone callbacks get called.
1051 */
1052STATIC int
1053xfs_bioerror(
1054 xfs_buf_t *bp)
1055{
1056#ifdef XFSERRORDEBUG
1057 ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
1058#endif
1059
1060 /*
1061 * No need to wait until the buffer is unpinned, we aren't flushing it.
1062 */
1063 XFS_BUF_ERROR(bp, EIO);
1064
1065 /*
1066 * We're calling biodone, so delete XBF_DONE flag.
1067 */
1068 XFS_BUF_UNREAD(bp);
1069 XFS_BUF_UNDELAYWRITE(bp);
1070 XFS_BUF_UNDONE(bp);
1071 XFS_BUF_STALE(bp);
1072
1073 XFS_BUF_CLR_BDSTRAT_FUNC(bp);
1074 xfs_biodone(bp);
1075
1076 return EIO;
1077}
1078
1079/*
1080 * Same as xfs_bioerror, except that we are releasing the buffer
1081 * here ourselves, and avoiding the biodone call.
1082 * This is meant for userdata errors; metadata bufs come with
1083 * iodone functions attached, so that we can track down errors.
1084 */
1085STATIC int
1086xfs_bioerror_relse(
1087 struct xfs_buf *bp)
1088{
1089 int64_t fl = XFS_BUF_BFLAGS(bp);
1090 /*
1091 * No need to wait until the buffer is unpinned.
1092 * We aren't flushing it.
1093 *
1094 * chunkhold expects B_DONE to be set, whether
1095 * we actually finish the I/O or not. We don't want to
1096 * change that interface.
1097 */
1098 XFS_BUF_UNREAD(bp);
1099 XFS_BUF_UNDELAYWRITE(bp);
1100 XFS_BUF_DONE(bp);
1101 XFS_BUF_STALE(bp);
1102 XFS_BUF_CLR_IODONE_FUNC(bp);
1103 XFS_BUF_CLR_BDSTRAT_FUNC(bp);
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001104 if (!(fl & XBF_ASYNC)) {
Christoph Hellwig4e234712010-01-13 22:17:56 +00001105 /*
1106 * Mark b_error and B_ERROR _both_.
1107 * Lot's of chunkcache code assumes that.
1108 * There's no reason to mark error for
1109 * ASYNC buffers.
1110 */
1111 XFS_BUF_ERROR(bp, EIO);
1112 XFS_BUF_FINISH_IOWAIT(bp);
1113 } else {
1114 xfs_buf_relse(bp);
1115 }
1116
1117 return EIO;
1118}
1119
1120
1121/*
1122 * All xfs metadata buffers except log state machine buffers
1123 * get this attached as their b_bdstrat callback function.
1124 * This is so that we can catch a buffer
1125 * after prematurely unpinning it to forcibly shutdown the filesystem.
1126 */
1127int
1128xfs_bdstrat_cb(
1129 struct xfs_buf *bp)
1130{
1131 if (XFS_FORCED_SHUTDOWN(bp->b_mount)) {
1132 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1133 /*
1134 * Metadata write that didn't get logged but
1135 * written delayed anyway. These aren't associated
1136 * with a transaction, and can be ignored.
1137 */
1138 if (!bp->b_iodone && !XFS_BUF_ISREAD(bp))
1139 return xfs_bioerror_relse(bp);
1140 else
1141 return xfs_bioerror(bp);
1142 }
1143
1144 xfs_buf_iorequest(bp);
1145 return 0;
1146}
1147
1148/*
1149 * Wrapper around bdstrat so that we can stop data from going to disk in case
1150 * we are shutting down the filesystem. Typically user data goes thru this
1151 * path; one of the exceptions is the superblock.
1152 */
1153void
1154xfsbdstrat(
1155 struct xfs_mount *mp,
1156 struct xfs_buf *bp)
1157{
1158 if (XFS_FORCED_SHUTDOWN(mp)) {
1159 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1160 xfs_bioerror_relse(bp);
1161 return;
1162 }
1163
1164 xfs_buf_iorequest(bp);
1165}
1166
Christoph Hellwigb8f82a42009-11-14 16:17:22 +00001167STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001168_xfs_buf_ioend(
1169 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 int schedule)
1171{
Christoph Hellwig6ab455e2008-05-19 16:34:42 +10001172 if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
1173 bp->b_flags &= ~_XBF_PAGE_LOCKED;
Nathan Scottce8e9222006-01-11 15:39:08 +11001174 xfs_buf_ioend(bp, schedule);
Christoph Hellwig6ab455e2008-05-19 16:34:42 +10001175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176}
1177
Al Viro782e3b32007-10-12 07:17:47 +01001178STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001179xfs_buf_bio_end_io(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 struct bio *bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 int error)
1182{
Nathan Scottce8e9222006-01-11 15:39:08 +11001183 xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
1184 unsigned int blocksize = bp->b_target->bt_bsize;
Nathan Scotteedb5532005-09-02 16:39:56 +10001185 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Lachlan McIlroycfbe5262008-12-12 15:27:25 +11001187 xfs_buf_ioerror(bp, -error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
James Bottomley73c77e22010-01-25 11:42:24 -06001189 if (!error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
1190 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1191
Nathan Scotteedb5532005-09-02 16:39:56 +10001192 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 struct page *page = bvec->bv_page;
1194
Nathan Scott948ecdb2006-09-28 11:03:13 +10001195 ASSERT(!PagePrivate(page));
Nathan Scottce8e9222006-01-11 15:39:08 +11001196 if (unlikely(bp->b_error)) {
1197 if (bp->b_flags & XBF_READ)
Nathan Scotteedb5532005-09-02 16:39:56 +10001198 ClearPageUptodate(page);
Nathan Scottce8e9222006-01-11 15:39:08 +11001199 } else if (blocksize >= PAGE_CACHE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 SetPageUptodate(page);
1201 } else if (!PagePrivate(page) &&
Nathan Scottce8e9222006-01-11 15:39:08 +11001202 (bp->b_flags & _XBF_PAGE_CACHE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 set_page_region(page, bvec->bv_offset, bvec->bv_len);
1204 }
1205
Nathan Scotteedb5532005-09-02 16:39:56 +10001206 if (--bvec >= bio->bi_io_vec)
1207 prefetchw(&bvec->bv_page->flags);
Christoph Hellwig6ab455e2008-05-19 16:34:42 +10001208
1209 if (bp->b_flags & _XBF_PAGE_LOCKED)
1210 unlock_page(page);
Nathan Scotteedb5532005-09-02 16:39:56 +10001211 } while (bvec >= bio->bi_io_vec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Nathan Scottce8e9222006-01-11 15:39:08 +11001213 _xfs_buf_ioend(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215}
1216
1217STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001218_xfs_buf_ioapply(
1219 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
Christoph Hellwiga9759f22007-12-07 14:07:08 +11001221 int rw, map_i, total_nr_pages, nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 struct bio *bio;
Nathan Scottce8e9222006-01-11 15:39:08 +11001223 int offset = bp->b_offset;
1224 int size = bp->b_count_desired;
1225 sector_t sector = bp->b_bn;
1226 unsigned int blocksize = bp->b_target->bt_bsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Nathan Scottce8e9222006-01-11 15:39:08 +11001228 total_nr_pages = bp->b_page_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 map_i = 0;
1230
Nathan Scottce8e9222006-01-11 15:39:08 +11001231 if (bp->b_flags & XBF_ORDERED) {
1232 ASSERT(!(bp->b_flags & XBF_READ));
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001233 rw = WRITE_BARRIER;
Dave Chinner2ee1aba2009-11-24 18:03:15 +00001234 } else if (bp->b_flags & XBF_LOG_BUFFER) {
Nathan Scott51bdd702006-09-28 11:01:57 +10001235 ASSERT(!(bp->b_flags & XBF_READ_AHEAD));
1236 bp->b_flags &= ~_XBF_RUN_QUEUES;
1237 rw = (bp->b_flags & XBF_WRITE) ? WRITE_SYNC : READ_SYNC;
Dave Chinner2ee1aba2009-11-24 18:03:15 +00001238 } else if (bp->b_flags & _XBF_RUN_QUEUES) {
1239 ASSERT(!(bp->b_flags & XBF_READ_AHEAD));
1240 bp->b_flags &= ~_XBF_RUN_QUEUES;
1241 rw = (bp->b_flags & XBF_WRITE) ? WRITE_META : READ_META;
Nathan Scott51bdd702006-09-28 11:01:57 +10001242 } else {
1243 rw = (bp->b_flags & XBF_WRITE) ? WRITE :
1244 (bp->b_flags & XBF_READ_AHEAD) ? READA : READ;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001245 }
1246
Nathan Scottce8e9222006-01-11 15:39:08 +11001247 /* Special code path for reading a sub page size buffer in --
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 * we populate up the whole page, and hence the other metadata
1249 * in the same page. This optimization is only valid when the
Nathan Scottce8e9222006-01-11 15:39:08 +11001250 * filesystem block size is not smaller than the page size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001252 if ((bp->b_buffer_length < PAGE_CACHE_SIZE) &&
Christoph Hellwig6ab455e2008-05-19 16:34:42 +10001253 ((bp->b_flags & (XBF_READ|_XBF_PAGE_LOCKED)) ==
1254 (XBF_READ|_XBF_PAGE_LOCKED)) &&
Nathan Scottce8e9222006-01-11 15:39:08 +11001255 (blocksize >= PAGE_CACHE_SIZE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 bio = bio_alloc(GFP_NOIO, 1);
1257
Nathan Scottce8e9222006-01-11 15:39:08 +11001258 bio->bi_bdev = bp->b_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 bio->bi_sector = sector - (offset >> BBSHIFT);
Nathan Scottce8e9222006-01-11 15:39:08 +11001260 bio->bi_end_io = xfs_buf_bio_end_io;
1261 bio->bi_private = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Nathan Scottce8e9222006-01-11 15:39:08 +11001263 bio_add_page(bio, bp->b_pages[0], PAGE_CACHE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 size = 0;
1265
Nathan Scottce8e9222006-01-11 15:39:08 +11001266 atomic_inc(&bp->b_io_remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 goto submit_io;
1269 }
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001272 atomic_inc(&bp->b_io_remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
1274 if (nr_pages > total_nr_pages)
1275 nr_pages = total_nr_pages;
1276
1277 bio = bio_alloc(GFP_NOIO, nr_pages);
Nathan Scottce8e9222006-01-11 15:39:08 +11001278 bio->bi_bdev = bp->b_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 bio->bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001280 bio->bi_end_io = xfs_buf_bio_end_io;
1281 bio->bi_private = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 for (; size && nr_pages; nr_pages--, map_i++) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001284 int rbytes, nbytes = PAGE_CACHE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 if (nbytes > size)
1287 nbytes = size;
1288
Nathan Scottce8e9222006-01-11 15:39:08 +11001289 rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset);
1290 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 break;
1292
1293 offset = 0;
1294 sector += nbytes >> BBSHIFT;
1295 size -= nbytes;
1296 total_nr_pages--;
1297 }
1298
1299submit_io:
1300 if (likely(bio->bi_size)) {
James Bottomley73c77e22010-01-25 11:42:24 -06001301 if (xfs_buf_is_vmapped(bp)) {
1302 flush_kernel_vmap_range(bp->b_addr,
1303 xfs_buf_vmap_len(bp));
1304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 submit_bio(rw, bio);
1306 if (size)
1307 goto next_chunk;
1308 } else {
1309 bio_put(bio);
Nathan Scottce8e9222006-01-11 15:39:08 +11001310 xfs_buf_ioerror(bp, EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 }
1312}
1313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314int
Nathan Scottce8e9222006-01-11 15:39:08 +11001315xfs_buf_iorequest(
1316 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001318 trace_xfs_buf_iorequest(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Nathan Scottce8e9222006-01-11 15:39:08 +11001320 if (bp->b_flags & XBF_DELWRI) {
1321 xfs_buf_delwri_queue(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 return 0;
1323 }
1324
Nathan Scottce8e9222006-01-11 15:39:08 +11001325 if (bp->b_flags & XBF_WRITE) {
1326 xfs_buf_wait_unpin(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 }
1328
Nathan Scottce8e9222006-01-11 15:39:08 +11001329 xfs_buf_hold(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 /* Set the count to 1 initially, this will stop an I/O
1332 * completion callout which happens before we have started
Nathan Scottce8e9222006-01-11 15:39:08 +11001333 * all the I/O from calling xfs_buf_ioend too early.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001335 atomic_set(&bp->b_io_remaining, 1);
1336 _xfs_buf_ioapply(bp);
1337 _xfs_buf_ioend(bp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Nathan Scottce8e9222006-01-11 15:39:08 +11001339 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 return 0;
1341}
1342
1343/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001344 * Waits for I/O to complete on the buffer supplied.
1345 * It returns immediately if no I/O is pending.
1346 * It returns the I/O error code, if any, or 0 if there was no error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 */
1348int
Nathan Scottce8e9222006-01-11 15:39:08 +11001349xfs_buf_iowait(
1350 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001352 trace_xfs_buf_iowait(bp, _RET_IP_);
1353
Nathan Scottce8e9222006-01-11 15:39:08 +11001354 if (atomic_read(&bp->b_io_remaining))
1355 blk_run_address_space(bp->b_target->bt_mapping);
David Chinnerb4dd3302008-08-13 16:36:11 +10001356 wait_for_completion(&bp->b_iowait);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001357
1358 trace_xfs_buf_iowait_done(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +11001359 return bp->b_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360}
1361
Nathan Scottce8e9222006-01-11 15:39:08 +11001362xfs_caddr_t
1363xfs_buf_offset(
1364 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 size_t offset)
1366{
1367 struct page *page;
1368
Nathan Scottce8e9222006-01-11 15:39:08 +11001369 if (bp->b_flags & XBF_MAPPED)
1370 return XFS_BUF_PTR(bp) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Nathan Scottce8e9222006-01-11 15:39:08 +11001372 offset += bp->b_offset;
1373 page = bp->b_pages[offset >> PAGE_CACHE_SHIFT];
1374 return (xfs_caddr_t)page_address(page) + (offset & (PAGE_CACHE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375}
1376
1377/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 * Move data into or out of a buffer.
1379 */
1380void
Nathan Scottce8e9222006-01-11 15:39:08 +11001381xfs_buf_iomove(
1382 xfs_buf_t *bp, /* buffer to process */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 size_t boff, /* starting buffer offset */
1384 size_t bsize, /* length to copy */
Dave Chinnerb9c48642010-01-20 10:47:39 +11001385 void *data, /* data address */
Nathan Scottce8e9222006-01-11 15:39:08 +11001386 xfs_buf_rw_t mode) /* read/write/zero flag */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
1388 size_t bend, cpoff, csize;
1389 struct page *page;
1390
1391 bend = boff + bsize;
1392 while (boff < bend) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001393 page = bp->b_pages[xfs_buf_btoct(boff + bp->b_offset)];
1394 cpoff = xfs_buf_poff(boff + bp->b_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 csize = min_t(size_t,
Nathan Scottce8e9222006-01-11 15:39:08 +11001396 PAGE_CACHE_SIZE-cpoff, bp->b_count_desired-boff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 ASSERT(((csize + cpoff) <= PAGE_CACHE_SIZE));
1399
1400 switch (mode) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001401 case XBRW_ZERO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 memset(page_address(page) + cpoff, 0, csize);
1403 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001404 case XBRW_READ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 memcpy(data, page_address(page) + cpoff, csize);
1406 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001407 case XBRW_WRITE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 memcpy(page_address(page) + cpoff, data, csize);
1409 }
1410
1411 boff += csize;
1412 data += csize;
1413 }
1414}
1415
1416/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001417 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 */
1419
1420/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001421 * Wait for any bufs with callbacks that have been submitted but
1422 * have not yet returned... walk the hash list for the target.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 */
1424void
1425xfs_wait_buftarg(
1426 xfs_buftarg_t *btp)
1427{
1428 xfs_buf_t *bp, *n;
1429 xfs_bufhash_t *hash;
1430 uint i;
1431
1432 for (i = 0; i < (1 << btp->bt_hashshift); i++) {
1433 hash = &btp->bt_hash[i];
1434again:
1435 spin_lock(&hash->bh_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +11001436 list_for_each_entry_safe(bp, n, &hash->bh_list, b_hash_list) {
1437 ASSERT(btp == bp->b_target);
1438 if (!(bp->b_flags & XBF_FS_MANAGED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 spin_unlock(&hash->bh_lock);
David Chinner2f926582005-09-05 08:33:35 +10001440 /*
1441 * Catch superblock reference count leaks
1442 * immediately
1443 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001444 BUG_ON(bp->b_bn == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 delay(100);
1446 goto again;
1447 }
1448 }
1449 spin_unlock(&hash->bh_lock);
1450 }
1451}
1452
1453/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001454 * Allocate buffer hash table for a given target.
1455 * For devices containing metadata (i.e. not the log/realtime devices)
1456 * we need to allocate a much larger hash table.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 */
1458STATIC void
1459xfs_alloc_bufhash(
1460 xfs_buftarg_t *btp,
1461 int external)
1462{
1463 unsigned int i;
1464
1465 btp->bt_hashshift = external ? 3 : 8; /* 8 or 256 buckets */
1466 btp->bt_hashmask = (1 << btp->bt_hashshift) - 1;
Christoph Hellwigbdfb0432010-01-20 21:55:30 +00001467 btp->bt_hash = kmem_zalloc_large((1 << btp->bt_hashshift) *
1468 sizeof(xfs_bufhash_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 for (i = 0; i < (1 << btp->bt_hashshift); i++) {
1470 spin_lock_init(&btp->bt_hash[i].bh_lock);
1471 INIT_LIST_HEAD(&btp->bt_hash[i].bh_list);
1472 }
1473}
1474
1475STATIC void
1476xfs_free_bufhash(
1477 xfs_buftarg_t *btp)
1478{
Christoph Hellwigbdfb0432010-01-20 21:55:30 +00001479 kmem_free_large(btp->bt_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 btp->bt_hash = NULL;
1481}
1482
David Chinnera6867a62006-01-11 15:37:58 +11001483/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001484 * buftarg list for delwrite queue processing
David Chinnera6867a62006-01-11 15:37:58 +11001485 */
Tim Shimmine6a0e9c2007-05-08 13:49:59 +10001486static LIST_HEAD(xfs_buftarg_list);
David Chinner7989cb82007-02-10 18:34:56 +11001487static DEFINE_SPINLOCK(xfs_buftarg_lock);
David Chinnera6867a62006-01-11 15:37:58 +11001488
1489STATIC void
1490xfs_register_buftarg(
1491 xfs_buftarg_t *btp)
1492{
1493 spin_lock(&xfs_buftarg_lock);
1494 list_add(&btp->bt_list, &xfs_buftarg_list);
1495 spin_unlock(&xfs_buftarg_lock);
1496}
1497
1498STATIC void
1499xfs_unregister_buftarg(
1500 xfs_buftarg_t *btp)
1501{
1502 spin_lock(&xfs_buftarg_lock);
1503 list_del(&btp->bt_list);
1504 spin_unlock(&xfs_buftarg_lock);
1505}
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507void
1508xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001509 struct xfs_mount *mp,
1510 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
1512 xfs_flush_buftarg(btp, 1);
Christoph Hellwigb7963132009-03-03 14:48:37 -05001513 if (mp->m_flags & XFS_MOUNT_BARRIER)
1514 xfs_blkdev_issue_flush(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 xfs_free_bufhash(btp);
Nathan Scottce8e9222006-01-11 15:39:08 +11001516 iput(btp->bt_mapping->host);
David Chinnera6867a62006-01-11 15:37:58 +11001517
Nathan Scottce8e9222006-01-11 15:39:08 +11001518 /* Unregister the buftarg first so that we don't get a
1519 * wakeup finding a non-existent task
1520 */
David Chinnera6867a62006-01-11 15:37:58 +11001521 xfs_unregister_buftarg(btp);
1522 kthread_stop(btp->bt_task);
1523
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001524 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525}
1526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527STATIC int
1528xfs_setsize_buftarg_flags(
1529 xfs_buftarg_t *btp,
1530 unsigned int blocksize,
1531 unsigned int sectorsize,
1532 int verbose)
1533{
Nathan Scottce8e9222006-01-11 15:39:08 +11001534 btp->bt_bsize = blocksize;
1535 btp->bt_sshift = ffs(sectorsize) - 1;
1536 btp->bt_smask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Nathan Scottce8e9222006-01-11 15:39:08 +11001538 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 printk(KERN_WARNING
1540 "XFS: Cannot set_blocksize to %u on device %s\n",
1541 sectorsize, XFS_BUFTARG_NAME(btp));
1542 return EINVAL;
1543 }
1544
1545 if (verbose &&
1546 (PAGE_CACHE_SIZE / BITS_PER_LONG) > sectorsize) {
1547 printk(KERN_WARNING
1548 "XFS: %u byte sectors in use on device %s. "
1549 "This is suboptimal; %u or greater is ideal.\n",
1550 sectorsize, XFS_BUFTARG_NAME(btp),
1551 (unsigned int)PAGE_CACHE_SIZE / BITS_PER_LONG);
1552 }
1553
1554 return 0;
1555}
1556
1557/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001558 * When allocating the initial buffer target we have not yet
1559 * read in the superblock, so don't know what sized sectors
1560 * are being used is at this early stage. Play safe.
1561 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562STATIC int
1563xfs_setsize_buftarg_early(
1564 xfs_buftarg_t *btp,
1565 struct block_device *bdev)
1566{
1567 return xfs_setsize_buftarg_flags(btp,
Martin K. Petersene1defc42009-05-22 17:17:49 -04001568 PAGE_CACHE_SIZE, bdev_logical_block_size(bdev), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569}
1570
1571int
1572xfs_setsize_buftarg(
1573 xfs_buftarg_t *btp,
1574 unsigned int blocksize,
1575 unsigned int sectorsize)
1576{
1577 return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
1578}
1579
1580STATIC int
1581xfs_mapping_buftarg(
1582 xfs_buftarg_t *btp,
1583 struct block_device *bdev)
1584{
1585 struct backing_dev_info *bdi;
1586 struct inode *inode;
1587 struct address_space *mapping;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07001588 static const struct address_space_operations mapping_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 .sync_page = block_sync_page,
Christoph Lametere965f962006-02-01 03:05:41 -08001590 .migratepage = fail_migrate_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 };
1592
1593 inode = new_inode(bdev->bd_inode->i_sb);
1594 if (!inode) {
1595 printk(KERN_WARNING
1596 "XFS: Cannot allocate mapping inode for device %s\n",
1597 XFS_BUFTARG_NAME(btp));
1598 return ENOMEM;
1599 }
1600 inode->i_mode = S_IFBLK;
1601 inode->i_bdev = bdev;
1602 inode->i_rdev = bdev->bd_dev;
1603 bdi = blk_get_backing_dev_info(bdev);
1604 if (!bdi)
1605 bdi = &default_backing_dev_info;
1606 mapping = &inode->i_data;
1607 mapping->a_ops = &mapping_aops;
1608 mapping->backing_dev_info = bdi;
1609 mapping_set_gfp_mask(mapping, GFP_NOFS);
Nathan Scottce8e9222006-01-11 15:39:08 +11001610 btp->bt_mapping = mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 return 0;
1612}
1613
David Chinnera6867a62006-01-11 15:37:58 +11001614STATIC int
1615xfs_alloc_delwrite_queue(
1616 xfs_buftarg_t *btp)
1617{
1618 int error = 0;
1619
1620 INIT_LIST_HEAD(&btp->bt_list);
1621 INIT_LIST_HEAD(&btp->bt_delwrite_queue);
Eric Sandeen007c61c2007-10-11 17:43:56 +10001622 spin_lock_init(&btp->bt_delwrite_lock);
David Chinnera6867a62006-01-11 15:37:58 +11001623 btp->bt_flags = 0;
1624 btp->bt_task = kthread_run(xfsbufd, btp, "xfsbufd");
1625 if (IS_ERR(btp->bt_task)) {
1626 error = PTR_ERR(btp->bt_task);
1627 goto out_error;
1628 }
1629 xfs_register_buftarg(btp);
1630out_error:
1631 return error;
1632}
1633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634xfs_buftarg_t *
1635xfs_alloc_buftarg(
1636 struct block_device *bdev,
1637 int external)
1638{
1639 xfs_buftarg_t *btp;
1640
1641 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
1642
Nathan Scottce8e9222006-01-11 15:39:08 +11001643 btp->bt_dev = bdev->bd_dev;
1644 btp->bt_bdev = bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 if (xfs_setsize_buftarg_early(btp, bdev))
1646 goto error;
1647 if (xfs_mapping_buftarg(btp, bdev))
1648 goto error;
David Chinnera6867a62006-01-11 15:37:58 +11001649 if (xfs_alloc_delwrite_queue(btp))
1650 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 xfs_alloc_bufhash(btp, external);
1652 return btp;
1653
1654error:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001655 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 return NULL;
1657}
1658
1659
1660/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001661 * Delayed write buffer handling
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001664xfs_buf_delwri_queue(
1665 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 int unlock)
1667{
Nathan Scottce8e9222006-01-11 15:39:08 +11001668 struct list_head *dwq = &bp->b_target->bt_delwrite_queue;
1669 spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
David Chinnera6867a62006-01-11 15:37:58 +11001670
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001671 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
1672
Nathan Scottce8e9222006-01-11 15:39:08 +11001673 ASSERT((bp->b_flags&(XBF_DELWRI|XBF_ASYNC)) == (XBF_DELWRI|XBF_ASYNC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
David Chinnera6867a62006-01-11 15:37:58 +11001675 spin_lock(dwlk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 /* If already in the queue, dequeue and place at tail */
Nathan Scottce8e9222006-01-11 15:39:08 +11001677 if (!list_empty(&bp->b_list)) {
1678 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1679 if (unlock)
1680 atomic_dec(&bp->b_hold);
1681 list_del(&bp->b_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 }
1683
Dave Chinnerc9c12972010-01-11 11:49:59 +00001684 if (list_empty(dwq)) {
1685 /* start xfsbufd as it is about to have something to do */
1686 wake_up_process(bp->b_target->bt_task);
1687 }
1688
Nathan Scottce8e9222006-01-11 15:39:08 +11001689 bp->b_flags |= _XBF_DELWRI_Q;
1690 list_add_tail(&bp->b_list, dwq);
1691 bp->b_queuetime = jiffies;
David Chinnera6867a62006-01-11 15:37:58 +11001692 spin_unlock(dwlk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
1694 if (unlock)
Nathan Scottce8e9222006-01-11 15:39:08 +11001695 xfs_buf_unlock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696}
1697
1698void
Nathan Scottce8e9222006-01-11 15:39:08 +11001699xfs_buf_delwri_dequeue(
1700 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701{
Nathan Scottce8e9222006-01-11 15:39:08 +11001702 spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 int dequeued = 0;
1704
David Chinnera6867a62006-01-11 15:37:58 +11001705 spin_lock(dwlk);
Nathan Scottce8e9222006-01-11 15:39:08 +11001706 if ((bp->b_flags & XBF_DELWRI) && !list_empty(&bp->b_list)) {
1707 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1708 list_del_init(&bp->b_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 dequeued = 1;
1710 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001711 bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q);
David Chinnera6867a62006-01-11 15:37:58 +11001712 spin_unlock(dwlk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
1714 if (dequeued)
Nathan Scottce8e9222006-01-11 15:39:08 +11001715 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001717 trace_xfs_buf_delwri_dequeue(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718}
1719
Dave Chinnerd808f612010-02-02 10:13:42 +11001720/*
1721 * If a delwri buffer needs to be pushed before it has aged out, then promote
1722 * it to the head of the delwri queue so that it will be flushed on the next
1723 * xfsbufd run. We do this by resetting the queuetime of the buffer to be older
1724 * than the age currently needed to flush the buffer. Hence the next time the
1725 * xfsbufd sees it is guaranteed to be considered old enough to flush.
1726 */
1727void
1728xfs_buf_delwri_promote(
1729 struct xfs_buf *bp)
1730{
1731 struct xfs_buftarg *btp = bp->b_target;
1732 long age = xfs_buf_age_centisecs * msecs_to_jiffies(10) + 1;
1733
1734 ASSERT(bp->b_flags & XBF_DELWRI);
1735 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1736
1737 /*
1738 * Check the buffer age before locking the delayed write queue as we
1739 * don't need to promote buffers that are already past the flush age.
1740 */
1741 if (bp->b_queuetime < jiffies - age)
1742 return;
1743 bp->b_queuetime = jiffies - age;
1744 spin_lock(&btp->bt_delwrite_lock);
1745 list_move(&bp->b_list, &btp->bt_delwrite_queue);
1746 spin_unlock(&btp->bt_delwrite_lock);
1747}
1748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001750xfs_buf_runall_queues(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 struct workqueue_struct *queue)
1752{
1753 flush_workqueue(queue);
1754}
1755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756STATIC int
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001757xfsbufd_wakeup(
Nathan Scott15c84a42005-11-04 10:51:01 +11001758 int priority,
1759 gfp_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
Christoph Hellwigda7f93e2006-01-11 20:49:57 +11001761 xfs_buftarg_t *btp;
David Chinnera6867a62006-01-11 15:37:58 +11001762
1763 spin_lock(&xfs_buftarg_lock);
Christoph Hellwigda7f93e2006-01-11 20:49:57 +11001764 list_for_each_entry(btp, &xfs_buftarg_list, bt_list) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001765 if (test_bit(XBT_FORCE_SLEEP, &btp->bt_flags))
David Chinnera6867a62006-01-11 15:37:58 +11001766 continue;
Dave Chinnerc9c12972010-01-11 11:49:59 +00001767 if (list_empty(&btp->bt_delwrite_queue))
1768 continue;
Nathan Scottce8e9222006-01-11 15:39:08 +11001769 set_bit(XBT_FORCE_FLUSH, &btp->bt_flags);
David Chinnera6867a62006-01-11 15:37:58 +11001770 wake_up_process(btp->bt_task);
1771 }
1772 spin_unlock(&xfs_buftarg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 return 0;
1774}
1775
David Chinner585e6d82007-02-10 18:32:29 +11001776/*
1777 * Move as many buffers as specified to the supplied list
1778 * idicating if we skipped any buffers to prevent deadlocks.
1779 */
1780STATIC int
1781xfs_buf_delwri_split(
1782 xfs_buftarg_t *target,
1783 struct list_head *list,
David Chinner5e6a07d2007-02-10 18:34:49 +11001784 unsigned long age)
David Chinner585e6d82007-02-10 18:32:29 +11001785{
1786 xfs_buf_t *bp, *n;
1787 struct list_head *dwq = &target->bt_delwrite_queue;
1788 spinlock_t *dwlk = &target->bt_delwrite_lock;
1789 int skipped = 0;
David Chinner5e6a07d2007-02-10 18:34:49 +11001790 int force;
David Chinner585e6d82007-02-10 18:32:29 +11001791
David Chinner5e6a07d2007-02-10 18:34:49 +11001792 force = test_and_clear_bit(XBT_FORCE_FLUSH, &target->bt_flags);
David Chinner585e6d82007-02-10 18:32:29 +11001793 INIT_LIST_HEAD(list);
1794 spin_lock(dwlk);
1795 list_for_each_entry_safe(bp, n, dwq, b_list) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001796 trace_xfs_buf_delwri_split(bp, _RET_IP_);
David Chinner585e6d82007-02-10 18:32:29 +11001797 ASSERT(bp->b_flags & XBF_DELWRI);
1798
1799 if (!xfs_buf_ispin(bp) && !xfs_buf_cond_lock(bp)) {
David Chinner5e6a07d2007-02-10 18:34:49 +11001800 if (!force &&
David Chinner585e6d82007-02-10 18:32:29 +11001801 time_before(jiffies, bp->b_queuetime + age)) {
1802 xfs_buf_unlock(bp);
1803 break;
1804 }
1805
1806 bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q|
1807 _XBF_RUN_QUEUES);
1808 bp->b_flags |= XBF_WRITE;
1809 list_move_tail(&bp->b_list, list);
1810 } else
1811 skipped++;
1812 }
1813 spin_unlock(dwlk);
1814
1815 return skipped;
1816
1817}
1818
Dave Chinner089716a2010-01-26 15:13:25 +11001819/*
1820 * Compare function is more complex than it needs to be because
1821 * the return value is only 32 bits and we are doing comparisons
1822 * on 64 bit values
1823 */
1824static int
1825xfs_buf_cmp(
1826 void *priv,
1827 struct list_head *a,
1828 struct list_head *b)
1829{
1830 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
1831 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
1832 xfs_daddr_t diff;
1833
1834 diff = ap->b_bn - bp->b_bn;
1835 if (diff < 0)
1836 return -1;
1837 if (diff > 0)
1838 return 1;
1839 return 0;
1840}
1841
1842void
1843xfs_buf_delwri_sort(
1844 xfs_buftarg_t *target,
1845 struct list_head *list)
1846{
1847 list_sort(NULL, list, xfs_buf_cmp);
1848}
1849
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850STATIC int
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001851xfsbufd(
David Chinner585e6d82007-02-10 18:32:29 +11001852 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853{
Dave Chinner089716a2010-01-26 15:13:25 +11001854 xfs_buftarg_t *target = (xfs_buftarg_t *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 current->flags |= PF_MEMALLOC;
1857
Rafael J. Wysocki978c7b22007-12-07 14:09:02 +11001858 set_freezable();
1859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 do {
Dave Chinnerc9c12972010-01-11 11:49:59 +00001861 long age = xfs_buf_age_centisecs * msecs_to_jiffies(10);
1862 long tout = xfs_buf_timer_centisecs * msecs_to_jiffies(10);
Dave Chinner089716a2010-01-26 15:13:25 +11001863 int count = 0;
1864 struct list_head tmp;
Dave Chinnerc9c12972010-01-11 11:49:59 +00001865
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001866 if (unlikely(freezing(current))) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001867 set_bit(XBT_FORCE_SLEEP, &target->bt_flags);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001868 refrigerator();
Nathan Scottabd0cf72005-05-05 13:30:13 -07001869 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +11001870 clear_bit(XBT_FORCE_SLEEP, &target->bt_flags);
Nathan Scottabd0cf72005-05-05 13:30:13 -07001871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Dave Chinnerc9c12972010-01-11 11:49:59 +00001873 /* sleep for a long time if there is nothing to do. */
1874 if (list_empty(&target->bt_delwrite_queue))
1875 tout = MAX_SCHEDULE_TIMEOUT;
1876 schedule_timeout_interruptible(tout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Dave Chinnerc9c12972010-01-11 11:49:59 +00001878 xfs_buf_delwri_split(target, &tmp, age);
Dave Chinner089716a2010-01-26 15:13:25 +11001879 list_sort(NULL, &tmp, xfs_buf_cmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 while (!list_empty(&tmp)) {
Dave Chinner089716a2010-01-26 15:13:25 +11001881 struct xfs_buf *bp;
1882 bp = list_first_entry(&tmp, struct xfs_buf, b_list);
Nathan Scottce8e9222006-01-11 15:39:08 +11001883 list_del_init(&bp->b_list);
1884 xfs_buf_iostrategy(bp);
David Chinner585e6d82007-02-10 18:32:29 +11001885 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 }
Nathan Scottf07c2252006-09-28 10:52:15 +10001887 if (count)
1888 blk_run_address_space(target->bt_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Christoph Hellwig4df08c52005-09-05 08:34:18 +10001890 } while (!kthread_should_stop());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Christoph Hellwig4df08c52005-09-05 08:34:18 +10001892 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893}
1894
1895/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001896 * Go through all incore buffers, and release buffers if they belong to
1897 * the given device. This is used in filesystem error handling to
1898 * preserve the consistency of its metadata.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 */
1900int
1901xfs_flush_buftarg(
David Chinner585e6d82007-02-10 18:32:29 +11001902 xfs_buftarg_t *target,
1903 int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904{
Dave Chinner089716a2010-01-26 15:13:25 +11001905 xfs_buf_t *bp;
David Chinner585e6d82007-02-10 18:32:29 +11001906 int pincount = 0;
Dave Chinner089716a2010-01-26 15:13:25 +11001907 LIST_HEAD(tmp_list);
1908 LIST_HEAD(wait_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
Dave Chinnerc626d172009-04-06 18:42:11 +02001910 xfs_buf_runall_queues(xfsconvertd_workqueue);
Nathan Scottce8e9222006-01-11 15:39:08 +11001911 xfs_buf_runall_queues(xfsdatad_workqueue);
1912 xfs_buf_runall_queues(xfslogd_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
David Chinner5e6a07d2007-02-10 18:34:49 +11001914 set_bit(XBT_FORCE_FLUSH, &target->bt_flags);
Dave Chinner089716a2010-01-26 15:13:25 +11001915 pincount = xfs_buf_delwri_split(target, &tmp_list, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
1917 /*
Dave Chinner089716a2010-01-26 15:13:25 +11001918 * Dropped the delayed write list lock, now walk the temporary list.
1919 * All I/O is issued async and then if we need to wait for completion
1920 * we do that after issuing all the IO.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 */
Dave Chinner089716a2010-01-26 15:13:25 +11001922 list_sort(NULL, &tmp_list, xfs_buf_cmp);
1923 while (!list_empty(&tmp_list)) {
1924 bp = list_first_entry(&tmp_list, struct xfs_buf, b_list);
David Chinner585e6d82007-02-10 18:32:29 +11001925 ASSERT(target == bp->b_target);
Dave Chinner089716a2010-01-26 15:13:25 +11001926 list_del_init(&bp->b_list);
1927 if (wait) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001928 bp->b_flags &= ~XBF_ASYNC;
Dave Chinner089716a2010-01-26 15:13:25 +11001929 list_add(&bp->b_list, &wait_list);
1930 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001931 xfs_buf_iostrategy(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 }
1933
Dave Chinner089716a2010-01-26 15:13:25 +11001934 if (wait) {
1935 /* Expedite and wait for IO to complete. */
Nathan Scottf07c2252006-09-28 10:52:15 +10001936 blk_run_address_space(target->bt_mapping);
Dave Chinner089716a2010-01-26 15:13:25 +11001937 while (!list_empty(&wait_list)) {
1938 bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
Nathan Scottf07c2252006-09-28 10:52:15 +10001939
Dave Chinner089716a2010-01-26 15:13:25 +11001940 list_del_init(&bp->b_list);
1941 xfs_iowait(bp);
1942 xfs_buf_relse(bp);
1943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 }
1945
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 return pincount;
1947}
1948
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001949int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11001950xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
Nathan Scott87582802006-03-14 13:18:19 +11001952 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
1953 KM_ZONE_HWALIGN, NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11001954 if (!xfs_buf_zone)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001955 goto out;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001956
Rafael J. Wysockib4337692007-03-22 00:11:27 -08001957 xfslogd_workqueue = create_workqueue("xfslogd");
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