blob: 1418b916fc2755306cca61e60c734154d543c82f [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Christoph Hellwigb7963132009-03-03 14:48:37 -050037#include "xfs_sb.h"
38#include "xfs_inum.h"
39#include "xfs_ag.h"
40#include "xfs_dmapi.h"
41#include "xfs_mount.h"
42
David Chinner7989cb82007-02-10 18:34:56 +110043static kmem_zone_t *xfs_buf_zone;
David Chinnera6867a62006-01-11 15:37:58 +110044STATIC int xfsbufd(void *);
Al Viro27496a82005-10-21 03:20:48 -040045STATIC int xfsbufd_wakeup(int, gfp_t);
Nathan Scottce8e9222006-01-11 15:39:08 +110046STATIC void xfs_buf_delwri_queue(xfs_buf_t *, int);
Rusty Russell8e1f9362007-07-17 04:03:17 -070047static struct shrinker xfs_buf_shake = {
48 .shrink = xfsbufd_wakeup,
49 .seeks = DEFAULT_SEEKS,
50};
Christoph Hellwig23ea4032005-06-21 15:14:01 +100051
David Chinner7989cb82007-02-10 18:34:56 +110052static struct workqueue_struct *xfslogd_workqueue;
Christoph Hellwig0829c362005-09-02 16:58:49 +100053struct workqueue_struct *xfsdatad_workqueue;
Dave Chinnerc626d172009-04-06 18:42:11 +020054struct workqueue_struct *xfsconvertd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Nathan Scottce8e9222006-01-11 15:39:08 +110056#ifdef XFS_BUF_TRACE
Linus Torvalds1da177e2005-04-16 15:20:36 -070057void
Nathan Scottce8e9222006-01-11 15:39:08 +110058xfs_buf_trace(
59 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 char *id,
61 void *data,
62 void *ra)
63{
Nathan Scottce8e9222006-01-11 15:39:08 +110064 ktrace_enter(xfs_buf_trace_buf,
65 bp, id,
66 (void *)(unsigned long)bp->b_flags,
67 (void *)(unsigned long)bp->b_hold.counter,
Lachlan McIlroyd63f1542008-08-13 16:28:40 +100068 (void *)(unsigned long)bp->b_sema.count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 (void *)current,
70 data, ra,
Nathan Scottce8e9222006-01-11 15:39:08 +110071 (void *)(unsigned long)((bp->b_file_offset>>32) & 0xffffffff),
72 (void *)(unsigned long)(bp->b_file_offset & 0xffffffff),
73 (void *)(unsigned long)bp->b_buffer_length,
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 NULL, NULL, NULL, NULL, NULL);
75}
Nathan Scottce8e9222006-01-11 15:39:08 +110076ktrace_t *xfs_buf_trace_buf;
77#define XFS_BUF_TRACE_SIZE 4096
78#define XB_TRACE(bp, id, data) \
79 xfs_buf_trace(bp, id, (void *)data, (void *)__builtin_return_address(0))
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#else
Nathan Scottce8e9222006-01-11 15:39:08 +110081#define XB_TRACE(bp, id, data) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#endif
83
Nathan Scottce8e9222006-01-11 15:39:08 +110084#ifdef XFS_BUF_LOCK_TRACKING
85# define XB_SET_OWNER(bp) ((bp)->b_last_holder = current->pid)
86# define XB_CLEAR_OWNER(bp) ((bp)->b_last_holder = -1)
87# define XB_GET_OWNER(bp) ((bp)->b_last_holder)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#else
Nathan Scottce8e9222006-01-11 15:39:08 +110089# define XB_SET_OWNER(bp) do { } while (0)
90# define XB_CLEAR_OWNER(bp) do { } while (0)
91# define XB_GET_OWNER(bp) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#endif
93
Nathan Scottce8e9222006-01-11 15:39:08 +110094#define xb_to_gfp(flags) \
95 ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : \
96 ((flags) & XBF_DONT_BLOCK) ? GFP_NOFS : GFP_KERNEL) | __GFP_NOWARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Nathan Scottce8e9222006-01-11 15:39:08 +110098#define xb_to_km(flags) \
99 (((flags) & XBF_DONT_BLOCK) ? KM_NOFS : KM_SLEEP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Nathan Scottce8e9222006-01-11 15:39:08 +1100101#define xfs_buf_allocate(flags) \
102 kmem_zone_alloc(xfs_buf_zone, xb_to_km(flags))
103#define xfs_buf_deallocate(bp) \
104 kmem_zone_free(xfs_buf_zone, (bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100107 * Page Region interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 *
Nathan Scottce8e9222006-01-11 15:39:08 +1100109 * For pages in filesystems where the blocksize is smaller than the
110 * pagesize, we use the page->private field (long) to hold a bitmap
111 * of uptodate regions within the page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 *
Nathan Scottce8e9222006-01-11 15:39:08 +1100113 * Each such region is "bytes per page / bits per long" bytes long.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 *
Nathan Scottce8e9222006-01-11 15:39:08 +1100115 * NBPPR == number-of-bytes-per-page-region
116 * BTOPR == bytes-to-page-region (rounded up)
117 * BTOPRT == bytes-to-page-region-truncated (rounded down)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 */
119#if (BITS_PER_LONG == 32)
120#define PRSHIFT (PAGE_CACHE_SHIFT - 5) /* (32 == 1<<5) */
121#elif (BITS_PER_LONG == 64)
122#define PRSHIFT (PAGE_CACHE_SHIFT - 6) /* (64 == 1<<6) */
123#else
124#error BITS_PER_LONG must be 32 or 64
125#endif
126#define NBPPR (PAGE_CACHE_SIZE/BITS_PER_LONG)
127#define BTOPR(b) (((unsigned int)(b) + (NBPPR - 1)) >> PRSHIFT)
128#define BTOPRT(b) (((unsigned int)(b) >> PRSHIFT))
129
130STATIC unsigned long
131page_region_mask(
132 size_t offset,
133 size_t length)
134{
135 unsigned long mask;
136 int first, final;
137
138 first = BTOPR(offset);
139 final = BTOPRT(offset + length - 1);
140 first = min(first, final);
141
142 mask = ~0UL;
143 mask <<= BITS_PER_LONG - (final - first);
144 mask >>= BITS_PER_LONG - (final);
145
146 ASSERT(offset + length <= PAGE_CACHE_SIZE);
147 ASSERT((final - first) < BITS_PER_LONG && (final - first) >= 0);
148
149 return mask;
150}
151
David Chinner7989cb82007-02-10 18:34:56 +1100152STATIC_INLINE void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153set_page_region(
154 struct page *page,
155 size_t offset,
156 size_t length)
157{
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700158 set_page_private(page,
159 page_private(page) | page_region_mask(offset, length));
160 if (page_private(page) == ~0UL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 SetPageUptodate(page);
162}
163
David Chinner7989cb82007-02-10 18:34:56 +1100164STATIC_INLINE int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165test_page_region(
166 struct page *page,
167 size_t offset,
168 size_t length)
169{
170 unsigned long mask = page_region_mask(offset, length);
171
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700172 return (mask && (page_private(page) & mask) == mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
175/*
Felix Blyakher3a011a12009-02-18 15:56:51 -0600176 * Mapping of multi-page buffers into contiguous virtual space
177 */
178
179typedef struct a_list {
180 void *vm_addr;
181 struct a_list *next;
182} a_list_t;
183
184static a_list_t *as_free_head;
185static int as_list_len;
186static DEFINE_SPINLOCK(as_lock);
187
188/*
189 * Try to batch vunmaps because they are costly.
190 */
191STATIC void
192free_address(
193 void *addr)
194{
195 a_list_t *aentry;
196
197#ifdef CONFIG_XEN
198 /*
199 * Xen needs to be able to make sure it can get an exclusive
200 * RO mapping of pages it wants to turn into a pagetable. If
201 * a newly allocated page is also still being vmap()ed by xfs,
202 * it will cause pagetable construction to fail. This is a
203 * quick workaround to always eagerly unmap pages so that Xen
204 * is happy.
205 */
206 vunmap(addr);
207 return;
208#endif
209
210 aentry = kmalloc(sizeof(a_list_t), GFP_NOWAIT);
211 if (likely(aentry)) {
212 spin_lock(&as_lock);
213 aentry->next = as_free_head;
214 aentry->vm_addr = addr;
215 as_free_head = aentry;
216 as_list_len++;
217 spin_unlock(&as_lock);
218 } else {
219 vunmap(addr);
220 }
221}
222
223STATIC void
224purge_addresses(void)
225{
226 a_list_t *aentry, *old;
227
228 if (as_free_head == NULL)
229 return;
230
231 spin_lock(&as_lock);
232 aentry = as_free_head;
233 as_free_head = NULL;
234 as_list_len = 0;
235 spin_unlock(&as_lock);
236
237 while ((old = aentry) != NULL) {
238 vunmap(aentry->vm_addr);
239 aentry = aentry->next;
240 kfree(old);
241 }
242}
243
244/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100245 * Internal xfs_buf_t object manipulation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 */
247
248STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100249_xfs_buf_initialize(
250 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100252 xfs_off_t range_base,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 size_t range_length,
Nathan Scottce8e9222006-01-11 15:39:08 +1100254 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 /*
Nathan Scottce8e9222006-01-11 15:39:08 +1100257 * We don't want certain flags to appear in b_flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100259 flags &= ~(XBF_LOCK|XBF_MAPPED|XBF_DONT_BLOCK|XBF_READ_AHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Nathan Scottce8e9222006-01-11 15:39:08 +1100261 memset(bp, 0, sizeof(xfs_buf_t));
262 atomic_set(&bp->b_hold, 1);
David Chinnerb4dd3302008-08-13 16:36:11 +1000263 init_completion(&bp->b_iowait);
Nathan Scottce8e9222006-01-11 15:39:08 +1100264 INIT_LIST_HEAD(&bp->b_list);
265 INIT_LIST_HEAD(&bp->b_hash_list);
266 init_MUTEX_LOCKED(&bp->b_sema); /* held, no waiters */
267 XB_SET_OWNER(bp);
268 bp->b_target = target;
269 bp->b_file_offset = range_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 /*
271 * Set buffer_length and count_desired to the same value initially.
272 * I/O routines should use count_desired, which will be the same in
273 * most cases but may be reset (e.g. XFS recovery).
274 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100275 bp->b_buffer_length = bp->b_count_desired = range_length;
276 bp->b_flags = flags;
277 bp->b_bn = XFS_BUF_DADDR_NULL;
278 atomic_set(&bp->b_pin_count, 0);
279 init_waitqueue_head(&bp->b_waiters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Nathan Scottce8e9222006-01-11 15:39:08 +1100281 XFS_STATS_INC(xb_create);
282 XB_TRACE(bp, "initialize", target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
285/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100286 * Allocate a page array capable of holding a specified number
287 * of pages, and point the page buf at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 */
289STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100290_xfs_buf_get_pages(
291 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 int page_count,
Nathan Scottce8e9222006-01-11 15:39:08 +1100293 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295 /* Make sure that we have a page list */
Nathan Scottce8e9222006-01-11 15:39:08 +1100296 if (bp->b_pages == NULL) {
297 bp->b_offset = xfs_buf_poff(bp->b_file_offset);
298 bp->b_page_count = page_count;
299 if (page_count <= XB_PAGES) {
300 bp->b_pages = bp->b_page_array;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100302 bp->b_pages = kmem_alloc(sizeof(struct page *) *
303 page_count, xb_to_km(flags));
304 if (bp->b_pages == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return -ENOMEM;
306 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100307 memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
309 return 0;
310}
311
312/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100313 * Frees b_pages if it was allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 */
315STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100316_xfs_buf_free_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 xfs_buf_t *bp)
318{
Nathan Scottce8e9222006-01-11 15:39:08 +1100319 if (bp->b_pages != bp->b_page_array) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000320 kmem_free(bp->b_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
322}
323
324/*
325 * Releases the specified buffer.
326 *
327 * The modification state of any associated pages is left unchanged.
Nathan Scottce8e9222006-01-11 15:39:08 +1100328 * The buffer most not be on any hash - use xfs_buf_rele instead for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 * hashed and refcounted buffers
330 */
331void
Nathan Scottce8e9222006-01-11 15:39:08 +1100332xfs_buf_free(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 xfs_buf_t *bp)
334{
Nathan Scottce8e9222006-01-11 15:39:08 +1100335 XB_TRACE(bp, "free", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Nathan Scottce8e9222006-01-11 15:39:08 +1100337 ASSERT(list_empty(&bp->b_hash_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000339 if (bp->b_flags & (_XBF_PAGE_CACHE|_XBF_PAGES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 uint i;
341
Nathan Scottce8e9222006-01-11 15:39:08 +1100342 if ((bp->b_flags & XBF_MAPPED) && (bp->b_page_count > 1))
Felix Blyakher3a011a12009-02-18 15:56:51 -0600343 free_address(bp->b_addr - bp->b_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Nathan Scott948ecdb2006-09-28 11:03:13 +1000345 for (i = 0; i < bp->b_page_count; i++) {
346 struct page *page = bp->b_pages[i];
347
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000348 if (bp->b_flags & _XBF_PAGE_CACHE)
349 ASSERT(!PagePrivate(page));
Nathan Scott948ecdb2006-09-28 11:03:13 +1000350 page_cache_release(page);
351 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100352 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354
Nathan Scottce8e9222006-01-11 15:39:08 +1100355 xfs_buf_deallocate(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
358/*
359 * Finds all pages for buffer in question and builds it's page list.
360 */
361STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100362_xfs_buf_lookup_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 xfs_buf_t *bp,
364 uint flags)
365{
Nathan Scottce8e9222006-01-11 15:39:08 +1100366 struct address_space *mapping = bp->b_target->bt_mapping;
367 size_t blocksize = bp->b_target->bt_bsize;
368 size_t size = bp->b_count_desired;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 size_t nbytes, offset;
Nathan Scottce8e9222006-01-11 15:39:08 +1100370 gfp_t gfp_mask = xb_to_gfp(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 unsigned short page_count, i;
372 pgoff_t first;
Nathan Scott204ab252006-01-11 20:50:22 +1100373 xfs_off_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 int error;
375
Nathan Scottce8e9222006-01-11 15:39:08 +1100376 end = bp->b_file_offset + bp->b_buffer_length;
377 page_count = xfs_buf_btoc(end) - xfs_buf_btoct(bp->b_file_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Nathan Scottce8e9222006-01-11 15:39:08 +1100379 error = _xfs_buf_get_pages(bp, page_count, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (unlikely(error))
381 return error;
Nathan Scottce8e9222006-01-11 15:39:08 +1100382 bp->b_flags |= _XBF_PAGE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Nathan Scottce8e9222006-01-11 15:39:08 +1100384 offset = bp->b_offset;
385 first = bp->b_file_offset >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Nathan Scottce8e9222006-01-11 15:39:08 +1100387 for (i = 0; i < bp->b_page_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 struct page *page;
389 uint retries = 0;
390
391 retry:
392 page = find_or_create_page(mapping, first + i, gfp_mask);
393 if (unlikely(page == NULL)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100394 if (flags & XBF_READ_AHEAD) {
395 bp->b_page_count = i;
Christoph Hellwig6ab455e2008-05-19 16:34:42 +1000396 for (i = 0; i < bp->b_page_count; i++)
397 unlock_page(bp->b_pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return -ENOMEM;
399 }
400
401 /*
402 * This could deadlock.
403 *
404 * But until all the XFS lowlevel code is revamped to
405 * handle buffer allocation failures we can't do much.
406 */
407 if (!(++retries % 100))
408 printk(KERN_ERR
409 "XFS: possible memory allocation "
410 "deadlock in %s (mode:0x%x)\n",
Harvey Harrison34a622b2008-04-10 12:19:21 +1000411 __func__, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Nathan Scottce8e9222006-01-11 15:39:08 +1100413 XFS_STATS_INC(xb_page_retries);
Christoph Hellwig23ea4032005-06-21 15:14:01 +1000414 xfsbufd_wakeup(0, gfp_mask);
Andrew Morton3fcfab12006-10-19 23:28:16 -0700415 congestion_wait(WRITE, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 goto retry;
417 }
418
Nathan Scottce8e9222006-01-11 15:39:08 +1100419 XFS_STATS_INC(xb_page_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 nbytes = min_t(size_t, size, PAGE_CACHE_SIZE - offset);
422 size -= nbytes;
423
Nathan Scott948ecdb2006-09-28 11:03:13 +1000424 ASSERT(!PagePrivate(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (!PageUptodate(page)) {
426 page_count--;
Christoph Hellwig6ab455e2008-05-19 16:34:42 +1000427 if (blocksize >= PAGE_CACHE_SIZE) {
428 if (flags & XBF_READ)
429 bp->b_flags |= _XBF_PAGE_LOCKED;
430 } else if (!PagePrivate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (test_page_region(page, offset, nbytes))
432 page_count++;
433 }
434 }
435
Nathan Scottce8e9222006-01-11 15:39:08 +1100436 bp->b_pages[i] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 offset = 0;
438 }
439
Christoph Hellwig6ab455e2008-05-19 16:34:42 +1000440 if (!(bp->b_flags & _XBF_PAGE_LOCKED)) {
441 for (i = 0; i < bp->b_page_count; i++)
442 unlock_page(bp->b_pages[i]);
443 }
444
Nathan Scottce8e9222006-01-11 15:39:08 +1100445 if (page_count == bp->b_page_count)
446 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Nathan Scottce8e9222006-01-11 15:39:08 +1100448 XB_TRACE(bp, "lookup_pages", (long)page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return error;
450}
451
452/*
453 * Map buffer into kernel address-space if nessecary.
454 */
455STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100456_xfs_buf_map_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 xfs_buf_t *bp,
458 uint flags)
459{
460 /* A single page buffer is always mappable */
Nathan Scottce8e9222006-01-11 15:39:08 +1100461 if (bp->b_page_count == 1) {
462 bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
463 bp->b_flags |= XBF_MAPPED;
464 } else if (flags & XBF_MAPPED) {
Felix Blyakher3a011a12009-02-18 15:56:51 -0600465 if (as_list_len > 64)
466 purge_addresses();
Felix Blyakhercf7dab82009-02-18 15:41:28 -0600467 bp->b_addr = vmap(bp->b_pages, bp->b_page_count,
468 VM_MAP, PAGE_KERNEL);
Nathan Scottce8e9222006-01-11 15:39:08 +1100469 if (unlikely(bp->b_addr == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return -ENOMEM;
Nathan Scottce8e9222006-01-11 15:39:08 +1100471 bp->b_addr += bp->b_offset;
472 bp->b_flags |= XBF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474
475 return 0;
476}
477
478/*
479 * Finding and Reading Buffers
480 */
481
482/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100483 * Look up, and creates if absent, a lockable buffer for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 * a given range of an inode. The buffer is returned
485 * locked. If other overlapping buffers exist, they are
486 * released before the new buffer is created and locked,
487 * which may imply that this call will block until those buffers
488 * are unlocked. No I/O is implied by this call.
489 */
490xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100491_xfs_buf_find(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 xfs_buftarg_t *btp, /* block device target */
Nathan Scott204ab252006-01-11 20:50:22 +1100493 xfs_off_t ioff, /* starting offset of range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 size_t isize, /* length of range */
Nathan Scottce8e9222006-01-11 15:39:08 +1100495 xfs_buf_flags_t flags,
496 xfs_buf_t *new_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Nathan Scott204ab252006-01-11 20:50:22 +1100498 xfs_off_t range_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 size_t range_length;
500 xfs_bufhash_t *hash;
Nathan Scottce8e9222006-01-11 15:39:08 +1100501 xfs_buf_t *bp, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 range_base = (ioff << BBSHIFT);
504 range_length = (isize << BBSHIFT);
505
506 /* Check for IOs smaller than the sector size / not sector aligned */
Nathan Scottce8e9222006-01-11 15:39:08 +1100507 ASSERT(!(range_length < (1 << btp->bt_sshift)));
Nathan Scott204ab252006-01-11 20:50:22 +1100508 ASSERT(!(range_base & (xfs_off_t)btp->bt_smask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 hash = &btp->bt_hash[hash_long((unsigned long)ioff, btp->bt_hashshift)];
511
512 spin_lock(&hash->bh_lock);
513
Nathan Scottce8e9222006-01-11 15:39:08 +1100514 list_for_each_entry_safe(bp, n, &hash->bh_list, b_hash_list) {
515 ASSERT(btp == bp->b_target);
516 if (bp->b_file_offset == range_base &&
517 bp->b_buffer_length == range_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 /*
Nathan Scottce8e9222006-01-11 15:39:08 +1100519 * If we look at something, bring it to the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 * front of the list for next time.
521 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100522 atomic_inc(&bp->b_hold);
523 list_move(&bp->b_hash_list, &hash->bh_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 goto found;
525 }
526 }
527
528 /* No match found */
Nathan Scottce8e9222006-01-11 15:39:08 +1100529 if (new_bp) {
530 _xfs_buf_initialize(new_bp, btp, range_base,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 range_length, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100532 new_bp->b_hash = hash;
533 list_add(&new_bp->b_hash_list, &hash->bh_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100535 XFS_STATS_INC(xb_miss_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
537
538 spin_unlock(&hash->bh_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +1100539 return new_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541found:
542 spin_unlock(&hash->bh_lock);
543
544 /* Attempt to get the semaphore without sleeping,
545 * if this does not work then we need to drop the
546 * spinlock and do a hard attempt on the semaphore.
547 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100548 if (down_trylock(&bp->b_sema)) {
549 if (!(flags & XBF_TRYLOCK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 /* wait for buffer ownership */
Nathan Scottce8e9222006-01-11 15:39:08 +1100551 XB_TRACE(bp, "get_lock", 0);
552 xfs_buf_lock(bp);
553 XFS_STATS_INC(xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 } else {
555 /* We asked for a trylock and failed, no need
556 * to look at file offset and length here, we
Nathan Scottce8e9222006-01-11 15:39:08 +1100557 * know that this buffer at least overlaps our
558 * buffer and is locked, therefore our buffer
559 * either does not exist, or is this buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100561 xfs_buf_rele(bp);
562 XFS_STATS_INC(xb_busy_locked);
563 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565 } else {
566 /* trylock worked */
Nathan Scottce8e9222006-01-11 15:39:08 +1100567 XB_SET_OWNER(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
569
Nathan Scottce8e9222006-01-11 15:39:08 +1100570 if (bp->b_flags & XBF_STALE) {
571 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
572 bp->b_flags &= XBF_MAPPED;
David Chinner2f926582005-09-05 08:33:35 +1000573 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100574 XB_TRACE(bp, "got_lock", 0);
575 XFS_STATS_INC(xb_get_locked);
576 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
579/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100580 * Assembles a buffer covering the specified range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 * Storage in memory for all portions of the buffer will be allocated,
582 * although backing storage may not be.
583 */
584xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100585xfs_buf_get_flags(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 xfs_buftarg_t *target,/* target for buffer */
Nathan Scott204ab252006-01-11 20:50:22 +1100587 xfs_off_t ioff, /* starting offset of range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 size_t isize, /* length of range */
Nathan Scottce8e9222006-01-11 15:39:08 +1100589 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Nathan Scottce8e9222006-01-11 15:39:08 +1100591 xfs_buf_t *bp, *new_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 int error = 0, i;
593
Nathan Scottce8e9222006-01-11 15:39:08 +1100594 new_bp = xfs_buf_allocate(flags);
595 if (unlikely(!new_bp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 return NULL;
597
Nathan Scottce8e9222006-01-11 15:39:08 +1100598 bp = _xfs_buf_find(target, ioff, isize, flags, new_bp);
599 if (bp == new_bp) {
600 error = _xfs_buf_lookup_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (error)
602 goto no_buffer;
603 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100604 xfs_buf_deallocate(new_bp);
605 if (unlikely(bp == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return NULL;
607 }
608
Nathan Scottce8e9222006-01-11 15:39:08 +1100609 for (i = 0; i < bp->b_page_count; i++)
610 mark_page_accessed(bp->b_pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Nathan Scottce8e9222006-01-11 15:39:08 +1100612 if (!(bp->b_flags & XBF_MAPPED)) {
613 error = _xfs_buf_map_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (unlikely(error)) {
615 printk(KERN_WARNING "%s: failed to map pages\n",
Harvey Harrison34a622b2008-04-10 12:19:21 +1000616 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 goto no_buffer;
618 }
619 }
620
Nathan Scottce8e9222006-01-11 15:39:08 +1100621 XFS_STATS_INC(xb_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 /*
624 * Always fill in the block number now, the mapped cases can do
625 * their own overlay of this later.
626 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100627 bp->b_bn = ioff;
628 bp->b_count_desired = bp->b_buffer_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Nathan Scottce8e9222006-01-11 15:39:08 +1100630 XB_TRACE(bp, "get", (unsigned long)flags);
631 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100634 if (flags & (XBF_LOCK | XBF_TRYLOCK))
635 xfs_buf_unlock(bp);
636 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return NULL;
638}
639
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100640STATIC int
641_xfs_buf_read(
642 xfs_buf_t *bp,
643 xfs_buf_flags_t flags)
644{
645 int status;
646
647 XB_TRACE(bp, "_xfs_buf_read", (unsigned long)flags);
648
649 ASSERT(!(flags & (XBF_DELWRI|XBF_WRITE)));
650 ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL);
651
652 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_DELWRI | \
653 XBF_READ_AHEAD | _XBF_RUN_QUEUES);
654 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | \
655 XBF_READ_AHEAD | _XBF_RUN_QUEUES);
656
657 status = xfs_buf_iorequest(bp);
658 if (!status && !(flags & XBF_ASYNC))
659 status = xfs_buf_iowait(bp);
660 return status;
661}
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663xfs_buf_t *
664xfs_buf_read_flags(
665 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100666 xfs_off_t ioff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 size_t isize,
Nathan Scottce8e9222006-01-11 15:39:08 +1100668 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Nathan Scottce8e9222006-01-11 15:39:08 +1100670 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Nathan Scottce8e9222006-01-11 15:39:08 +1100672 flags |= XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Nathan Scottce8e9222006-01-11 15:39:08 +1100674 bp = xfs_buf_get_flags(target, ioff, isize, flags);
675 if (bp) {
676 if (!XFS_BUF_ISDONE(bp)) {
677 XB_TRACE(bp, "read", (unsigned long)flags);
678 XFS_STATS_INC(xb_get_read);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100679 _xfs_buf_read(bp, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100680 } else if (flags & XBF_ASYNC) {
681 XB_TRACE(bp, "read_async", (unsigned long)flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 /*
683 * Read ahead call which is already satisfied,
684 * drop the buffer
685 */
686 goto no_buffer;
687 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100688 XB_TRACE(bp, "read_done", (unsigned long)flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 /* We do not want read in the flags */
Nathan Scottce8e9222006-01-11 15:39:08 +1100690 bp->b_flags &= ~XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
692 }
693
Nathan Scottce8e9222006-01-11 15:39:08 +1100694 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100697 if (flags & (XBF_LOCK | XBF_TRYLOCK))
698 xfs_buf_unlock(bp);
699 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return NULL;
701}
702
703/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100704 * If we are not low on memory then do the readahead in a deadlock
705 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 */
707void
Nathan Scottce8e9222006-01-11 15:39:08 +1100708xfs_buf_readahead(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100710 xfs_off_t ioff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 size_t isize,
Nathan Scottce8e9222006-01-11 15:39:08 +1100712 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
714 struct backing_dev_info *bdi;
715
Nathan Scottce8e9222006-01-11 15:39:08 +1100716 bdi = target->bt_mapping->backing_dev_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (bdi_read_congested(bdi))
718 return;
719
Nathan Scottce8e9222006-01-11 15:39:08 +1100720 flags |= (XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 xfs_buf_read_flags(target, ioff, isize, flags);
722}
723
724xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100725xfs_buf_get_empty(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 size_t len,
727 xfs_buftarg_t *target)
728{
Nathan Scottce8e9222006-01-11 15:39:08 +1100729 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Nathan Scottce8e9222006-01-11 15:39:08 +1100731 bp = xfs_buf_allocate(0);
732 if (bp)
733 _xfs_buf_initialize(bp, target, 0, len, 0);
734 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
736
737static inline struct page *
738mem_to_page(
739 void *addr)
740{
Christoph Lameter9e2779f2008-02-04 22:28:34 -0800741 if ((!is_vmalloc_addr(addr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 return virt_to_page(addr);
743 } else {
744 return vmalloc_to_page(addr);
745 }
746}
747
748int
Nathan Scottce8e9222006-01-11 15:39:08 +1100749xfs_buf_associate_memory(
750 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 void *mem,
752 size_t len)
753{
754 int rval;
755 int i = 0;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100756 unsigned long pageaddr;
757 unsigned long offset;
758 size_t buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 int page_count;
760
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100761 pageaddr = (unsigned long)mem & PAGE_CACHE_MASK;
762 offset = (unsigned long)mem - pageaddr;
763 buflen = PAGE_CACHE_ALIGN(len + offset);
764 page_count = buflen >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 /* Free any previous set of page pointers */
Nathan Scottce8e9222006-01-11 15:39:08 +1100767 if (bp->b_pages)
768 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Nathan Scottce8e9222006-01-11 15:39:08 +1100770 bp->b_pages = NULL;
771 bp->b_addr = mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Nathan Scottce8e9222006-01-11 15:39:08 +1100773 rval = _xfs_buf_get_pages(bp, page_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (rval)
775 return rval;
776
Nathan Scottce8e9222006-01-11 15:39:08 +1100777 bp->b_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100779 for (i = 0; i < bp->b_page_count; i++) {
780 bp->b_pages[i] = mem_to_page((void *)pageaddr);
781 pageaddr += PAGE_CACHE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100784 bp->b_count_desired = len;
785 bp->b_buffer_length = buflen;
Nathan Scottce8e9222006-01-11 15:39:08 +1100786 bp->b_flags |= XBF_MAPPED;
Christoph Hellwig6ab455e2008-05-19 16:34:42 +1000787 bp->b_flags &= ~_XBF_PAGE_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 return 0;
790}
791
792xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100793xfs_buf_get_noaddr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 size_t len,
795 xfs_buftarg_t *target)
796{
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000797 unsigned long page_count = PAGE_ALIGN(len) >> PAGE_SHIFT;
798 int error, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Nathan Scottce8e9222006-01-11 15:39:08 +1100801 bp = xfs_buf_allocate(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (unlikely(bp == NULL))
803 goto fail;
Nathan Scottce8e9222006-01-11 15:39:08 +1100804 _xfs_buf_initialize(bp, target, 0, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000806 error = _xfs_buf_get_pages(bp, page_count, 0);
807 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 goto fail_free_buf;
809
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000810 for (i = 0; i < page_count; i++) {
811 bp->b_pages[i] = alloc_page(GFP_KERNEL);
812 if (!bp->b_pages[i])
813 goto fail_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000815 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000817 error = _xfs_buf_map_pages(bp, XBF_MAPPED);
818 if (unlikely(error)) {
819 printk(KERN_WARNING "%s: failed to map pages\n",
Harvey Harrison34a622b2008-04-10 12:19:21 +1000820 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 goto fail_free_mem;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Nathan Scottce8e9222006-01-11 15:39:08 +1100824 xfs_buf_unlock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000826 XB_TRACE(bp, "no_daddr", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return bp;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 fail_free_mem:
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000830 while (--i >= 0)
831 __free_page(bp->b_pages[i]);
Christoph Hellwigca165b82007-05-24 15:21:11 +1000832 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 fail_free_buf:
Christoph Hellwigca165b82007-05-24 15:21:11 +1000834 xfs_buf_deallocate(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 fail:
836 return NULL;
837}
838
839/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 * Increment reference count on buffer, to hold the buffer concurrently
841 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 * Must hold the buffer already to call this function.
843 */
844void
Nathan Scottce8e9222006-01-11 15:39:08 +1100845xfs_buf_hold(
846 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Nathan Scottce8e9222006-01-11 15:39:08 +1100848 atomic_inc(&bp->b_hold);
849 XB_TRACE(bp, "hold", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
851
852/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100853 * Releases a hold on the specified buffer. If the
854 * the hold count is 1, calls xfs_buf_free.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 */
856void
Nathan Scottce8e9222006-01-11 15:39:08 +1100857xfs_buf_rele(
858 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
Nathan Scottce8e9222006-01-11 15:39:08 +1100860 xfs_bufhash_t *hash = bp->b_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Nathan Scottce8e9222006-01-11 15:39:08 +1100862 XB_TRACE(bp, "rele", bp->b_relse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Nathan Scottfad3aa12006-02-01 12:14:52 +1100864 if (unlikely(!hash)) {
865 ASSERT(!bp->b_relse);
866 if (atomic_dec_and_test(&bp->b_hold))
867 xfs_buf_free(bp);
868 return;
869 }
870
Lachlan McIlroy37906892008-08-13 15:42:10 +1000871 ASSERT(atomic_read(&bp->b_hold) > 0);
Nathan Scottce8e9222006-01-11 15:39:08 +1100872 if (atomic_dec_and_lock(&bp->b_hold, &hash->bh_lock)) {
873 if (bp->b_relse) {
874 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 spin_unlock(&hash->bh_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +1100876 (*(bp->b_relse)) (bp);
877 } else if (bp->b_flags & XBF_FS_MANAGED) {
Christoph Hellwig7f14d0a2005-11-02 15:09:35 +1100878 spin_unlock(&hash->bh_lock);
879 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100880 ASSERT(!(bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)));
881 list_del_init(&bp->b_hash_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 spin_unlock(&hash->bh_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +1100883 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
885 }
886}
887
888
889/*
890 * Mutual exclusion on buffers. Locking model:
891 *
892 * Buffers associated with inodes for which buffer locking
893 * is not enabled are not protected by semaphores, and are
894 * assumed to be exclusively owned by the caller. There is a
895 * spinlock in the buffer, used by the caller when concurrent
896 * access is possible.
897 */
898
899/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100900 * Locks a buffer object, if it is not already locked.
901 * Note that this in no way locks the underlying pages, so it is only
902 * useful for synchronizing concurrent use of buffer objects, not for
903 * synchronizing independent access to the underlying pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 */
905int
Nathan Scottce8e9222006-01-11 15:39:08 +1100906xfs_buf_cond_lock(
907 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
909 int locked;
910
Nathan Scottce8e9222006-01-11 15:39:08 +1100911 locked = down_trylock(&bp->b_sema) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 if (locked) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100913 XB_SET_OWNER(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100915 XB_TRACE(bp, "cond_lock", (long)locked);
916 return locked ? 0 : -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917}
918
919#if defined(DEBUG) || defined(XFS_BLI_TRACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920int
Nathan Scottce8e9222006-01-11 15:39:08 +1100921xfs_buf_lock_value(
922 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Stephen Rothwelladaa6932008-04-22 15:26:13 +1000924 return bp->b_sema.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
926#endif
927
928/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100929 * Locks a buffer object.
930 * Note that this in no way locks the underlying pages, so it is only
931 * useful for synchronizing concurrent use of buffer objects, not for
932 * synchronizing independent access to the underlying pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100934void
935xfs_buf_lock(
936 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Nathan Scottce8e9222006-01-11 15:39:08 +1100938 XB_TRACE(bp, "lock", 0);
939 if (atomic_read(&bp->b_io_remaining))
940 blk_run_address_space(bp->b_target->bt_mapping);
941 down(&bp->b_sema);
942 XB_SET_OWNER(bp);
943 XB_TRACE(bp, "locked", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
945
946/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100947 * Releases the lock on the buffer object.
David Chinner2f926582005-09-05 08:33:35 +1000948 * If the buffer is marked delwri but is not queued, do so before we
Nathan Scottce8e9222006-01-11 15:39:08 +1100949 * unlock the buffer as we need to set flags correctly. We also need to
David Chinner2f926582005-09-05 08:33:35 +1000950 * take a reference for the delwri queue because the unlocker is going to
951 * drop their's and they don't know we just queued it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 */
953void
Nathan Scottce8e9222006-01-11 15:39:08 +1100954xfs_buf_unlock(
955 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
Nathan Scottce8e9222006-01-11 15:39:08 +1100957 if ((bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)) == XBF_DELWRI) {
958 atomic_inc(&bp->b_hold);
959 bp->b_flags |= XBF_ASYNC;
960 xfs_buf_delwri_queue(bp, 0);
David Chinner2f926582005-09-05 08:33:35 +1000961 }
962
Nathan Scottce8e9222006-01-11 15:39:08 +1100963 XB_CLEAR_OWNER(bp);
964 up(&bp->b_sema);
965 XB_TRACE(bp, "unlock", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
967
968
969/*
970 * Pinning Buffer Storage in Memory
Nathan Scottce8e9222006-01-11 15:39:08 +1100971 * Ensure that no attempt to force a buffer to disk will succeed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 */
973void
Nathan Scottce8e9222006-01-11 15:39:08 +1100974xfs_buf_pin(
975 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
Nathan Scottce8e9222006-01-11 15:39:08 +1100977 atomic_inc(&bp->b_pin_count);
978 XB_TRACE(bp, "pin", (long)bp->b_pin_count.counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981void
Nathan Scottce8e9222006-01-11 15:39:08 +1100982xfs_buf_unpin(
983 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
Nathan Scottce8e9222006-01-11 15:39:08 +1100985 if (atomic_dec_and_test(&bp->b_pin_count))
986 wake_up_all(&bp->b_waiters);
987 XB_TRACE(bp, "unpin", (long)bp->b_pin_count.counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
989
990int
Nathan Scottce8e9222006-01-11 15:39:08 +1100991xfs_buf_ispin(
992 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
Nathan Scottce8e9222006-01-11 15:39:08 +1100994 return atomic_read(&bp->b_pin_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995}
996
Nathan Scottce8e9222006-01-11 15:39:08 +1100997STATIC void
998xfs_buf_wait_unpin(
999 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
1001 DECLARE_WAITQUEUE (wait, current);
1002
Nathan Scottce8e9222006-01-11 15:39:08 +11001003 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return;
1005
Nathan Scottce8e9222006-01-11 15:39:08 +11001006 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 for (;;) {
1008 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +11001009 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001011 if (atomic_read(&bp->b_io_remaining))
1012 blk_run_address_space(bp->b_target->bt_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 schedule();
1014 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001015 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 set_current_state(TASK_RUNNING);
1017}
1018
1019/*
1020 * Buffer Utility Routines
1021 */
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001024xfs_buf_iodone_work(
David Howellsc4028952006-11-22 14:57:56 +00001025 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
David Howellsc4028952006-11-22 14:57:56 +00001027 xfs_buf_t *bp =
1028 container_of(work, xfs_buf_t, b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
David Chinner0bfefc42007-05-14 18:24:23 +10001030 /*
1031 * We can get an EOPNOTSUPP to ordered writes. Here we clear the
1032 * ordered flag and reissue them. Because we can't tell the higher
1033 * layers directly that they should not issue ordered I/O anymore, they
Christoph Hellwig73f6aa42008-10-10 17:28:29 +11001034 * need to check if the _XFS_BARRIER_FAILED flag was set during I/O completion.
David Chinner0bfefc42007-05-14 18:24:23 +10001035 */
1036 if ((bp->b_error == EOPNOTSUPP) &&
1037 (bp->b_flags & (XBF_ORDERED|XBF_ASYNC)) == (XBF_ORDERED|XBF_ASYNC)) {
1038 XB_TRACE(bp, "ordered_retry", bp->b_iodone);
1039 bp->b_flags &= ~XBF_ORDERED;
Christoph Hellwig73f6aa42008-10-10 17:28:29 +11001040 bp->b_flags |= _XFS_BARRIER_FAILED;
David Chinner0bfefc42007-05-14 18:24:23 +10001041 xfs_buf_iorequest(bp);
1042 } else if (bp->b_iodone)
Nathan Scottce8e9222006-01-11 15:39:08 +11001043 (*(bp->b_iodone))(bp);
1044 else if (bp->b_flags & XBF_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 xfs_buf_relse(bp);
1046}
1047
1048void
Nathan Scottce8e9222006-01-11 15:39:08 +11001049xfs_buf_ioend(
1050 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 int schedule)
1052{
Lachlan McIlroy77be55a2007-11-23 16:31:00 +11001053 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
Nathan Scottce8e9222006-01-11 15:39:08 +11001054 if (bp->b_error == 0)
1055 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Nathan Scottce8e9222006-01-11 15:39:08 +11001057 XB_TRACE(bp, "iodone", bp->b_iodone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Nathan Scottce8e9222006-01-11 15:39:08 +11001059 if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 if (schedule) {
David Howellsc4028952006-11-22 14:57:56 +00001061 INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
Nathan Scottce8e9222006-01-11 15:39:08 +11001062 queue_work(xfslogd_workqueue, &bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 } else {
David Howellsc4028952006-11-22 14:57:56 +00001064 xfs_buf_iodone_work(&bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
1066 } else {
David Chinnerb4dd3302008-08-13 16:36:11 +10001067 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
1069}
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071void
Nathan Scottce8e9222006-01-11 15:39:08 +11001072xfs_buf_ioerror(
1073 xfs_buf_t *bp,
1074 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
1076 ASSERT(error >= 0 && error <= 0xffff);
Nathan Scottce8e9222006-01-11 15:39:08 +11001077 bp->b_error = (unsigned short)error;
1078 XB_TRACE(bp, "ioerror", (unsigned long)error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079}
1080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081int
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001082xfs_bawrite(
1083 void *mp,
1084 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001086 XB_TRACE(bp, "bawrite", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001088 ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001090 xfs_buf_delwri_dequeue(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001092 bp->b_flags &= ~(XBF_READ | XBF_DELWRI | XBF_READ_AHEAD);
1093 bp->b_flags |= (XBF_WRITE | XBF_ASYNC | _XBF_RUN_QUEUES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Christoph Hellwig15ac08a2008-12-09 04:47:30 -05001095 bp->b_mount = mp;
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001096 bp->b_strat = xfs_bdstrat_cb;
1097 return xfs_bdstrat_cb(bp);
1098}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001100void
1101xfs_bdwrite(
1102 void *mp,
1103 struct xfs_buf *bp)
1104{
1105 XB_TRACE(bp, "bdwrite", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001107 bp->b_strat = xfs_bdstrat_cb;
Christoph Hellwig15ac08a2008-12-09 04:47:30 -05001108 bp->b_mount = mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001110 bp->b_flags &= ~XBF_READ;
1111 bp->b_flags |= (XBF_DELWRI | XBF_ASYNC);
1112
1113 xfs_buf_delwri_queue(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114}
1115
David Chinner7989cb82007-02-10 18:34:56 +11001116STATIC_INLINE void
Nathan Scottce8e9222006-01-11 15:39:08 +11001117_xfs_buf_ioend(
1118 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 int schedule)
1120{
Christoph Hellwig6ab455e2008-05-19 16:34:42 +10001121 if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
1122 bp->b_flags &= ~_XBF_PAGE_LOCKED;
Nathan Scottce8e9222006-01-11 15:39:08 +11001123 xfs_buf_ioend(bp, schedule);
Christoph Hellwig6ab455e2008-05-19 16:34:42 +10001124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125}
1126
Al Viro782e3b32007-10-12 07:17:47 +01001127STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001128xfs_buf_bio_end_io(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 struct bio *bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 int error)
1131{
Nathan Scottce8e9222006-01-11 15:39:08 +11001132 xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
1133 unsigned int blocksize = bp->b_target->bt_bsize;
Nathan Scotteedb5532005-09-02 16:39:56 +10001134 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Lachlan McIlroycfbe5262008-12-12 15:27:25 +11001136 xfs_buf_ioerror(bp, -error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Nathan Scotteedb5532005-09-02 16:39:56 +10001138 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 struct page *page = bvec->bv_page;
1140
Nathan Scott948ecdb2006-09-28 11:03:13 +10001141 ASSERT(!PagePrivate(page));
Nathan Scottce8e9222006-01-11 15:39:08 +11001142 if (unlikely(bp->b_error)) {
1143 if (bp->b_flags & XBF_READ)
Nathan Scotteedb5532005-09-02 16:39:56 +10001144 ClearPageUptodate(page);
Nathan Scottce8e9222006-01-11 15:39:08 +11001145 } else if (blocksize >= PAGE_CACHE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 SetPageUptodate(page);
1147 } else if (!PagePrivate(page) &&
Nathan Scottce8e9222006-01-11 15:39:08 +11001148 (bp->b_flags & _XBF_PAGE_CACHE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 set_page_region(page, bvec->bv_offset, bvec->bv_len);
1150 }
1151
Nathan Scotteedb5532005-09-02 16:39:56 +10001152 if (--bvec >= bio->bi_io_vec)
1153 prefetchw(&bvec->bv_page->flags);
Christoph Hellwig6ab455e2008-05-19 16:34:42 +10001154
1155 if (bp->b_flags & _XBF_PAGE_LOCKED)
1156 unlock_page(page);
Nathan Scotteedb5532005-09-02 16:39:56 +10001157 } while (bvec >= bio->bi_io_vec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Nathan Scottce8e9222006-01-11 15:39:08 +11001159 _xfs_buf_ioend(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
1162
1163STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001164_xfs_buf_ioapply(
1165 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
Christoph Hellwiga9759f22007-12-07 14:07:08 +11001167 int rw, map_i, total_nr_pages, nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 struct bio *bio;
Nathan Scottce8e9222006-01-11 15:39:08 +11001169 int offset = bp->b_offset;
1170 int size = bp->b_count_desired;
1171 sector_t sector = bp->b_bn;
1172 unsigned int blocksize = bp->b_target->bt_bsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Nathan Scottce8e9222006-01-11 15:39:08 +11001174 total_nr_pages = bp->b_page_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 map_i = 0;
1176
Nathan Scottce8e9222006-01-11 15:39:08 +11001177 if (bp->b_flags & XBF_ORDERED) {
1178 ASSERT(!(bp->b_flags & XBF_READ));
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001179 rw = WRITE_BARRIER;
Nathan Scott51bdd702006-09-28 11:01:57 +10001180 } else if (bp->b_flags & _XBF_RUN_QUEUES) {
1181 ASSERT(!(bp->b_flags & XBF_READ_AHEAD));
1182 bp->b_flags &= ~_XBF_RUN_QUEUES;
1183 rw = (bp->b_flags & XBF_WRITE) ? WRITE_SYNC : READ_SYNC;
1184 } else {
1185 rw = (bp->b_flags & XBF_WRITE) ? WRITE :
1186 (bp->b_flags & XBF_READ_AHEAD) ? READA : READ;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001187 }
1188
Nathan Scottce8e9222006-01-11 15:39:08 +11001189 /* Special code path for reading a sub page size buffer in --
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 * we populate up the whole page, and hence the other metadata
1191 * in the same page. This optimization is only valid when the
Nathan Scottce8e9222006-01-11 15:39:08 +11001192 * filesystem block size is not smaller than the page size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001194 if ((bp->b_buffer_length < PAGE_CACHE_SIZE) &&
Christoph Hellwig6ab455e2008-05-19 16:34:42 +10001195 ((bp->b_flags & (XBF_READ|_XBF_PAGE_LOCKED)) ==
1196 (XBF_READ|_XBF_PAGE_LOCKED)) &&
Nathan Scottce8e9222006-01-11 15:39:08 +11001197 (blocksize >= PAGE_CACHE_SIZE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 bio = bio_alloc(GFP_NOIO, 1);
1199
Nathan Scottce8e9222006-01-11 15:39:08 +11001200 bio->bi_bdev = bp->b_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 bio->bi_sector = sector - (offset >> BBSHIFT);
Nathan Scottce8e9222006-01-11 15:39:08 +11001202 bio->bi_end_io = xfs_buf_bio_end_io;
1203 bio->bi_private = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Nathan Scottce8e9222006-01-11 15:39:08 +11001205 bio_add_page(bio, bp->b_pages[0], PAGE_CACHE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 size = 0;
1207
Nathan Scottce8e9222006-01-11 15:39:08 +11001208 atomic_inc(&bp->b_io_remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210 goto submit_io;
1211 }
1212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001214 atomic_inc(&bp->b_io_remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
1216 if (nr_pages > total_nr_pages)
1217 nr_pages = total_nr_pages;
1218
1219 bio = bio_alloc(GFP_NOIO, nr_pages);
Nathan Scottce8e9222006-01-11 15:39:08 +11001220 bio->bi_bdev = bp->b_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 bio->bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001222 bio->bi_end_io = xfs_buf_bio_end_io;
1223 bio->bi_private = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225 for (; size && nr_pages; nr_pages--, map_i++) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001226 int rbytes, nbytes = PAGE_CACHE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 if (nbytes > size)
1229 nbytes = size;
1230
Nathan Scottce8e9222006-01-11 15:39:08 +11001231 rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset);
1232 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 break;
1234
1235 offset = 0;
1236 sector += nbytes >> BBSHIFT;
1237 size -= nbytes;
1238 total_nr_pages--;
1239 }
1240
1241submit_io:
1242 if (likely(bio->bi_size)) {
1243 submit_bio(rw, bio);
1244 if (size)
1245 goto next_chunk;
1246 } else {
1247 bio_put(bio);
Nathan Scottce8e9222006-01-11 15:39:08 +11001248 xfs_buf_ioerror(bp, EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
1250}
1251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252int
Nathan Scottce8e9222006-01-11 15:39:08 +11001253xfs_buf_iorequest(
1254 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
Nathan Scottce8e9222006-01-11 15:39:08 +11001256 XB_TRACE(bp, "iorequest", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Nathan Scottce8e9222006-01-11 15:39:08 +11001258 if (bp->b_flags & XBF_DELWRI) {
1259 xfs_buf_delwri_queue(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 return 0;
1261 }
1262
Nathan Scottce8e9222006-01-11 15:39:08 +11001263 if (bp->b_flags & XBF_WRITE) {
1264 xfs_buf_wait_unpin(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
1266
Nathan Scottce8e9222006-01-11 15:39:08 +11001267 xfs_buf_hold(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 /* Set the count to 1 initially, this will stop an I/O
1270 * completion callout which happens before we have started
Nathan Scottce8e9222006-01-11 15:39:08 +11001271 * all the I/O from calling xfs_buf_ioend too early.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001273 atomic_set(&bp->b_io_remaining, 1);
1274 _xfs_buf_ioapply(bp);
1275 _xfs_buf_ioend(bp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Nathan Scottce8e9222006-01-11 15:39:08 +11001277 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 return 0;
1279}
1280
1281/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001282 * Waits for I/O to complete on the buffer supplied.
1283 * It returns immediately if no I/O is pending.
1284 * It returns the I/O error code, if any, or 0 if there was no error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 */
1286int
Nathan Scottce8e9222006-01-11 15:39:08 +11001287xfs_buf_iowait(
1288 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
Nathan Scottce8e9222006-01-11 15:39:08 +11001290 XB_TRACE(bp, "iowait", 0);
1291 if (atomic_read(&bp->b_io_remaining))
1292 blk_run_address_space(bp->b_target->bt_mapping);
David Chinnerb4dd3302008-08-13 16:36:11 +10001293 wait_for_completion(&bp->b_iowait);
Nathan Scottce8e9222006-01-11 15:39:08 +11001294 XB_TRACE(bp, "iowaited", (long)bp->b_error);
1295 return bp->b_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296}
1297
Nathan Scottce8e9222006-01-11 15:39:08 +11001298xfs_caddr_t
1299xfs_buf_offset(
1300 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 size_t offset)
1302{
1303 struct page *page;
1304
Nathan Scottce8e9222006-01-11 15:39:08 +11001305 if (bp->b_flags & XBF_MAPPED)
1306 return XFS_BUF_PTR(bp) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Nathan Scottce8e9222006-01-11 15:39:08 +11001308 offset += bp->b_offset;
1309 page = bp->b_pages[offset >> PAGE_CACHE_SHIFT];
1310 return (xfs_caddr_t)page_address(page) + (offset & (PAGE_CACHE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311}
1312
1313/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 * Move data into or out of a buffer.
1315 */
1316void
Nathan Scottce8e9222006-01-11 15:39:08 +11001317xfs_buf_iomove(
1318 xfs_buf_t *bp, /* buffer to process */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 size_t boff, /* starting buffer offset */
1320 size_t bsize, /* length to copy */
1321 caddr_t data, /* data address */
Nathan Scottce8e9222006-01-11 15:39:08 +11001322 xfs_buf_rw_t mode) /* read/write/zero flag */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
1324 size_t bend, cpoff, csize;
1325 struct page *page;
1326
1327 bend = boff + bsize;
1328 while (boff < bend) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001329 page = bp->b_pages[xfs_buf_btoct(boff + bp->b_offset)];
1330 cpoff = xfs_buf_poff(boff + bp->b_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 csize = min_t(size_t,
Nathan Scottce8e9222006-01-11 15:39:08 +11001332 PAGE_CACHE_SIZE-cpoff, bp->b_count_desired-boff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 ASSERT(((csize + cpoff) <= PAGE_CACHE_SIZE));
1335
1336 switch (mode) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001337 case XBRW_ZERO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 memset(page_address(page) + cpoff, 0, csize);
1339 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001340 case XBRW_READ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 memcpy(data, page_address(page) + cpoff, csize);
1342 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001343 case XBRW_WRITE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 memcpy(page_address(page) + cpoff, data, csize);
1345 }
1346
1347 boff += csize;
1348 data += csize;
1349 }
1350}
1351
1352/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001353 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 */
1355
1356/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001357 * Wait for any bufs with callbacks that have been submitted but
1358 * have not yet returned... walk the hash list for the target.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 */
1360void
1361xfs_wait_buftarg(
1362 xfs_buftarg_t *btp)
1363{
1364 xfs_buf_t *bp, *n;
1365 xfs_bufhash_t *hash;
1366 uint i;
1367
1368 for (i = 0; i < (1 << btp->bt_hashshift); i++) {
1369 hash = &btp->bt_hash[i];
1370again:
1371 spin_lock(&hash->bh_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +11001372 list_for_each_entry_safe(bp, n, &hash->bh_list, b_hash_list) {
1373 ASSERT(btp == bp->b_target);
1374 if (!(bp->b_flags & XBF_FS_MANAGED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 spin_unlock(&hash->bh_lock);
David Chinner2f926582005-09-05 08:33:35 +10001376 /*
1377 * Catch superblock reference count leaks
1378 * immediately
1379 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001380 BUG_ON(bp->b_bn == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 delay(100);
1382 goto again;
1383 }
1384 }
1385 spin_unlock(&hash->bh_lock);
1386 }
1387}
1388
1389/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001390 * Allocate buffer hash table for a given target.
1391 * For devices containing metadata (i.e. not the log/realtime devices)
1392 * we need to allocate a much larger hash table.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 */
1394STATIC void
1395xfs_alloc_bufhash(
1396 xfs_buftarg_t *btp,
1397 int external)
1398{
1399 unsigned int i;
1400
1401 btp->bt_hashshift = external ? 3 : 8; /* 8 or 256 buckets */
1402 btp->bt_hashmask = (1 << btp->bt_hashshift) - 1;
1403 btp->bt_hash = kmem_zalloc((1 << btp->bt_hashshift) *
Vlad Apostolov93c189c2006-11-11 18:03:49 +11001404 sizeof(xfs_bufhash_t), KM_SLEEP | KM_LARGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 for (i = 0; i < (1 << btp->bt_hashshift); i++) {
1406 spin_lock_init(&btp->bt_hash[i].bh_lock);
1407 INIT_LIST_HEAD(&btp->bt_hash[i].bh_list);
1408 }
1409}
1410
1411STATIC void
1412xfs_free_bufhash(
1413 xfs_buftarg_t *btp)
1414{
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001415 kmem_free(btp->bt_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 btp->bt_hash = NULL;
1417}
1418
David Chinnera6867a62006-01-11 15:37:58 +11001419/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001420 * buftarg list for delwrite queue processing
David Chinnera6867a62006-01-11 15:37:58 +11001421 */
Tim Shimmine6a0e9c2007-05-08 13:49:59 +10001422static LIST_HEAD(xfs_buftarg_list);
David Chinner7989cb82007-02-10 18:34:56 +11001423static DEFINE_SPINLOCK(xfs_buftarg_lock);
David Chinnera6867a62006-01-11 15:37:58 +11001424
1425STATIC void
1426xfs_register_buftarg(
1427 xfs_buftarg_t *btp)
1428{
1429 spin_lock(&xfs_buftarg_lock);
1430 list_add(&btp->bt_list, &xfs_buftarg_list);
1431 spin_unlock(&xfs_buftarg_lock);
1432}
1433
1434STATIC void
1435xfs_unregister_buftarg(
1436 xfs_buftarg_t *btp)
1437{
1438 spin_lock(&xfs_buftarg_lock);
1439 list_del(&btp->bt_list);
1440 spin_unlock(&xfs_buftarg_lock);
1441}
1442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443void
1444xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001445 struct xfs_mount *mp,
1446 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
1448 xfs_flush_buftarg(btp, 1);
Christoph Hellwigb7963132009-03-03 14:48:37 -05001449 if (mp->m_flags & XFS_MOUNT_BARRIER)
1450 xfs_blkdev_issue_flush(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 xfs_free_bufhash(btp);
Nathan Scottce8e9222006-01-11 15:39:08 +11001452 iput(btp->bt_mapping->host);
David Chinnera6867a62006-01-11 15:37:58 +11001453
Nathan Scottce8e9222006-01-11 15:39:08 +11001454 /* Unregister the buftarg first so that we don't get a
1455 * wakeup finding a non-existent task
1456 */
David Chinnera6867a62006-01-11 15:37:58 +11001457 xfs_unregister_buftarg(btp);
1458 kthread_stop(btp->bt_task);
1459
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001460 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461}
1462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463STATIC int
1464xfs_setsize_buftarg_flags(
1465 xfs_buftarg_t *btp,
1466 unsigned int blocksize,
1467 unsigned int sectorsize,
1468 int verbose)
1469{
Nathan Scottce8e9222006-01-11 15:39:08 +11001470 btp->bt_bsize = blocksize;
1471 btp->bt_sshift = ffs(sectorsize) - 1;
1472 btp->bt_smask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Nathan Scottce8e9222006-01-11 15:39:08 +11001474 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 printk(KERN_WARNING
1476 "XFS: Cannot set_blocksize to %u on device %s\n",
1477 sectorsize, XFS_BUFTARG_NAME(btp));
1478 return EINVAL;
1479 }
1480
1481 if (verbose &&
1482 (PAGE_CACHE_SIZE / BITS_PER_LONG) > sectorsize) {
1483 printk(KERN_WARNING
1484 "XFS: %u byte sectors in use on device %s. "
1485 "This is suboptimal; %u or greater is ideal.\n",
1486 sectorsize, XFS_BUFTARG_NAME(btp),
1487 (unsigned int)PAGE_CACHE_SIZE / BITS_PER_LONG);
1488 }
1489
1490 return 0;
1491}
1492
1493/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001494 * When allocating the initial buffer target we have not yet
1495 * read in the superblock, so don't know what sized sectors
1496 * are being used is at this early stage. Play safe.
1497 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498STATIC int
1499xfs_setsize_buftarg_early(
1500 xfs_buftarg_t *btp,
1501 struct block_device *bdev)
1502{
1503 return xfs_setsize_buftarg_flags(btp,
Martin K. Petersene1defc42009-05-22 17:17:49 -04001504 PAGE_CACHE_SIZE, bdev_logical_block_size(bdev), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505}
1506
1507int
1508xfs_setsize_buftarg(
1509 xfs_buftarg_t *btp,
1510 unsigned int blocksize,
1511 unsigned int sectorsize)
1512{
1513 return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
1514}
1515
1516STATIC int
1517xfs_mapping_buftarg(
1518 xfs_buftarg_t *btp,
1519 struct block_device *bdev)
1520{
1521 struct backing_dev_info *bdi;
1522 struct inode *inode;
1523 struct address_space *mapping;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07001524 static const struct address_space_operations mapping_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 .sync_page = block_sync_page,
Christoph Lametere965f962006-02-01 03:05:41 -08001526 .migratepage = fail_migrate_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 };
1528
1529 inode = new_inode(bdev->bd_inode->i_sb);
1530 if (!inode) {
1531 printk(KERN_WARNING
1532 "XFS: Cannot allocate mapping inode for device %s\n",
1533 XFS_BUFTARG_NAME(btp));
1534 return ENOMEM;
1535 }
1536 inode->i_mode = S_IFBLK;
1537 inode->i_bdev = bdev;
1538 inode->i_rdev = bdev->bd_dev;
1539 bdi = blk_get_backing_dev_info(bdev);
1540 if (!bdi)
1541 bdi = &default_backing_dev_info;
1542 mapping = &inode->i_data;
1543 mapping->a_ops = &mapping_aops;
1544 mapping->backing_dev_info = bdi;
1545 mapping_set_gfp_mask(mapping, GFP_NOFS);
Nathan Scottce8e9222006-01-11 15:39:08 +11001546 btp->bt_mapping = mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 return 0;
1548}
1549
David Chinnera6867a62006-01-11 15:37:58 +11001550STATIC int
1551xfs_alloc_delwrite_queue(
1552 xfs_buftarg_t *btp)
1553{
1554 int error = 0;
1555
1556 INIT_LIST_HEAD(&btp->bt_list);
1557 INIT_LIST_HEAD(&btp->bt_delwrite_queue);
Eric Sandeen007c61c2007-10-11 17:43:56 +10001558 spin_lock_init(&btp->bt_delwrite_lock);
David Chinnera6867a62006-01-11 15:37:58 +11001559 btp->bt_flags = 0;
1560 btp->bt_task = kthread_run(xfsbufd, btp, "xfsbufd");
1561 if (IS_ERR(btp->bt_task)) {
1562 error = PTR_ERR(btp->bt_task);
1563 goto out_error;
1564 }
1565 xfs_register_buftarg(btp);
1566out_error:
1567 return error;
1568}
1569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570xfs_buftarg_t *
1571xfs_alloc_buftarg(
1572 struct block_device *bdev,
1573 int external)
1574{
1575 xfs_buftarg_t *btp;
1576
1577 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
1578
Nathan Scottce8e9222006-01-11 15:39:08 +11001579 btp->bt_dev = bdev->bd_dev;
1580 btp->bt_bdev = bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 if (xfs_setsize_buftarg_early(btp, bdev))
1582 goto error;
1583 if (xfs_mapping_buftarg(btp, bdev))
1584 goto error;
David Chinnera6867a62006-01-11 15:37:58 +11001585 if (xfs_alloc_delwrite_queue(btp))
1586 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 xfs_alloc_bufhash(btp, external);
1588 return btp;
1589
1590error:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001591 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 return NULL;
1593}
1594
1595
1596/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001597 * Delayed write buffer handling
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001600xfs_buf_delwri_queue(
1601 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 int unlock)
1603{
Nathan Scottce8e9222006-01-11 15:39:08 +11001604 struct list_head *dwq = &bp->b_target->bt_delwrite_queue;
1605 spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
David Chinnera6867a62006-01-11 15:37:58 +11001606
Nathan Scottce8e9222006-01-11 15:39:08 +11001607 XB_TRACE(bp, "delwri_q", (long)unlock);
1608 ASSERT((bp->b_flags&(XBF_DELWRI|XBF_ASYNC)) == (XBF_DELWRI|XBF_ASYNC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
David Chinnera6867a62006-01-11 15:37:58 +11001610 spin_lock(dwlk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 /* If already in the queue, dequeue and place at tail */
Nathan Scottce8e9222006-01-11 15:39:08 +11001612 if (!list_empty(&bp->b_list)) {
1613 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1614 if (unlock)
1615 atomic_dec(&bp->b_hold);
1616 list_del(&bp->b_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 }
1618
Nathan Scottce8e9222006-01-11 15:39:08 +11001619 bp->b_flags |= _XBF_DELWRI_Q;
1620 list_add_tail(&bp->b_list, dwq);
1621 bp->b_queuetime = jiffies;
David Chinnera6867a62006-01-11 15:37:58 +11001622 spin_unlock(dwlk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 if (unlock)
Nathan Scottce8e9222006-01-11 15:39:08 +11001625 xfs_buf_unlock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626}
1627
1628void
Nathan Scottce8e9222006-01-11 15:39:08 +11001629xfs_buf_delwri_dequeue(
1630 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631{
Nathan Scottce8e9222006-01-11 15:39:08 +11001632 spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 int dequeued = 0;
1634
David Chinnera6867a62006-01-11 15:37:58 +11001635 spin_lock(dwlk);
Nathan Scottce8e9222006-01-11 15:39:08 +11001636 if ((bp->b_flags & XBF_DELWRI) && !list_empty(&bp->b_list)) {
1637 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1638 list_del_init(&bp->b_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 dequeued = 1;
1640 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001641 bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q);
David Chinnera6867a62006-01-11 15:37:58 +11001642 spin_unlock(dwlk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
1644 if (dequeued)
Nathan Scottce8e9222006-01-11 15:39:08 +11001645 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Nathan Scottce8e9222006-01-11 15:39:08 +11001647 XB_TRACE(bp, "delwri_dq", (long)dequeued);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648}
1649
1650STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001651xfs_buf_runall_queues(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 struct workqueue_struct *queue)
1653{
1654 flush_workqueue(queue);
1655}
1656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657STATIC int
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001658xfsbufd_wakeup(
Nathan Scott15c84a42005-11-04 10:51:01 +11001659 int priority,
1660 gfp_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661{
Christoph Hellwigda7f93e2006-01-11 20:49:57 +11001662 xfs_buftarg_t *btp;
David Chinnera6867a62006-01-11 15:37:58 +11001663
1664 spin_lock(&xfs_buftarg_lock);
Christoph Hellwigda7f93e2006-01-11 20:49:57 +11001665 list_for_each_entry(btp, &xfs_buftarg_list, bt_list) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001666 if (test_bit(XBT_FORCE_SLEEP, &btp->bt_flags))
David Chinnera6867a62006-01-11 15:37:58 +11001667 continue;
Nathan Scottce8e9222006-01-11 15:39:08 +11001668 set_bit(XBT_FORCE_FLUSH, &btp->bt_flags);
David Chinnera6867a62006-01-11 15:37:58 +11001669 wake_up_process(btp->bt_task);
1670 }
1671 spin_unlock(&xfs_buftarg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 return 0;
1673}
1674
David Chinner585e6d82007-02-10 18:32:29 +11001675/*
1676 * Move as many buffers as specified to the supplied list
1677 * idicating if we skipped any buffers to prevent deadlocks.
1678 */
1679STATIC int
1680xfs_buf_delwri_split(
1681 xfs_buftarg_t *target,
1682 struct list_head *list,
David Chinner5e6a07d2007-02-10 18:34:49 +11001683 unsigned long age)
David Chinner585e6d82007-02-10 18:32:29 +11001684{
1685 xfs_buf_t *bp, *n;
1686 struct list_head *dwq = &target->bt_delwrite_queue;
1687 spinlock_t *dwlk = &target->bt_delwrite_lock;
1688 int skipped = 0;
David Chinner5e6a07d2007-02-10 18:34:49 +11001689 int force;
David Chinner585e6d82007-02-10 18:32:29 +11001690
David Chinner5e6a07d2007-02-10 18:34:49 +11001691 force = test_and_clear_bit(XBT_FORCE_FLUSH, &target->bt_flags);
David Chinner585e6d82007-02-10 18:32:29 +11001692 INIT_LIST_HEAD(list);
1693 spin_lock(dwlk);
1694 list_for_each_entry_safe(bp, n, dwq, b_list) {
1695 XB_TRACE(bp, "walkq1", (long)xfs_buf_ispin(bp));
1696 ASSERT(bp->b_flags & XBF_DELWRI);
1697
1698 if (!xfs_buf_ispin(bp) && !xfs_buf_cond_lock(bp)) {
David Chinner5e6a07d2007-02-10 18:34:49 +11001699 if (!force &&
David Chinner585e6d82007-02-10 18:32:29 +11001700 time_before(jiffies, bp->b_queuetime + age)) {
1701 xfs_buf_unlock(bp);
1702 break;
1703 }
1704
1705 bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q|
1706 _XBF_RUN_QUEUES);
1707 bp->b_flags |= XBF_WRITE;
1708 list_move_tail(&bp->b_list, list);
1709 } else
1710 skipped++;
1711 }
1712 spin_unlock(dwlk);
1713
1714 return skipped;
1715
1716}
1717
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718STATIC int
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001719xfsbufd(
David Chinner585e6d82007-02-10 18:32:29 +11001720 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721{
David Chinner585e6d82007-02-10 18:32:29 +11001722 struct list_head tmp;
1723 xfs_buftarg_t *target = (xfs_buftarg_t *)data;
1724 int count;
1725 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 current->flags |= PF_MEMALLOC;
1728
Rafael J. Wysocki978c7b22007-12-07 14:09:02 +11001729 set_freezable();
1730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 do {
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001732 if (unlikely(freezing(current))) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001733 set_bit(XBT_FORCE_SLEEP, &target->bt_flags);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001734 refrigerator();
Nathan Scottabd0cf72005-05-05 13:30:13 -07001735 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +11001736 clear_bit(XBT_FORCE_SLEEP, &target->bt_flags);
Nathan Scottabd0cf72005-05-05 13:30:13 -07001737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Nathan Scott15c84a42005-11-04 10:51:01 +11001739 schedule_timeout_interruptible(
1740 xfs_buf_timer_centisecs * msecs_to_jiffies(10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
David Chinner585e6d82007-02-10 18:32:29 +11001742 xfs_buf_delwri_split(target, &tmp,
David Chinner5e6a07d2007-02-10 18:34:49 +11001743 xfs_buf_age_centisecs * msecs_to_jiffies(10));
David Chinner585e6d82007-02-10 18:32:29 +11001744
Nathan Scottf07c2252006-09-28 10:52:15 +10001745 count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 while (!list_empty(&tmp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001747 bp = list_entry(tmp.next, xfs_buf_t, b_list);
1748 ASSERT(target == bp->b_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Nathan Scottce8e9222006-01-11 15:39:08 +11001750 list_del_init(&bp->b_list);
1751 xfs_buf_iostrategy(bp);
David Chinner585e6d82007-02-10 18:32:29 +11001752 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 }
1754
Felix Blyakher3a011a12009-02-18 15:56:51 -06001755 if (as_list_len > 0)
1756 purge_addresses();
Nathan Scottf07c2252006-09-28 10:52:15 +10001757 if (count)
1758 blk_run_address_space(target->bt_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Christoph Hellwig4df08c52005-09-05 08:34:18 +10001760 } while (!kthread_should_stop());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Christoph Hellwig4df08c52005-09-05 08:34:18 +10001762 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763}
1764
1765/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001766 * Go through all incore buffers, and release buffers if they belong to
1767 * the given device. This is used in filesystem error handling to
1768 * preserve the consistency of its metadata.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 */
1770int
1771xfs_flush_buftarg(
David Chinner585e6d82007-02-10 18:32:29 +11001772 xfs_buftarg_t *target,
1773 int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
David Chinner585e6d82007-02-10 18:32:29 +11001775 struct list_head tmp;
1776 xfs_buf_t *bp, *n;
1777 int pincount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
Dave Chinnerc626d172009-04-06 18:42:11 +02001779 xfs_buf_runall_queues(xfsconvertd_workqueue);
Nathan Scottce8e9222006-01-11 15:39:08 +11001780 xfs_buf_runall_queues(xfsdatad_workqueue);
1781 xfs_buf_runall_queues(xfslogd_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
David Chinner5e6a07d2007-02-10 18:34:49 +11001783 set_bit(XBT_FORCE_FLUSH, &target->bt_flags);
1784 pincount = xfs_buf_delwri_split(target, &tmp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
1786 /*
1787 * Dropped the delayed write list lock, now walk the temporary list
1788 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001789 list_for_each_entry_safe(bp, n, &tmp, b_list) {
David Chinner585e6d82007-02-10 18:32:29 +11001790 ASSERT(target == bp->b_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 if (wait)
Nathan Scottce8e9222006-01-11 15:39:08 +11001792 bp->b_flags &= ~XBF_ASYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 else
Nathan Scottce8e9222006-01-11 15:39:08 +11001794 list_del_init(&bp->b_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Nathan Scottce8e9222006-01-11 15:39:08 +11001796 xfs_buf_iostrategy(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 }
1798
Nathan Scottf07c2252006-09-28 10:52:15 +10001799 if (wait)
1800 blk_run_address_space(target->bt_mapping);
1801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 /*
1803 * Remaining list items must be flushed before returning
1804 */
1805 while (!list_empty(&tmp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001806 bp = list_entry(tmp.next, xfs_buf_t, b_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Nathan Scottce8e9222006-01-11 15:39:08 +11001808 list_del_init(&bp->b_list);
1809 xfs_iowait(bp);
1810 xfs_buf_relse(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 }
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 return pincount;
1814}
1815
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001816int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11001817xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818{
Nathan Scottce8e9222006-01-11 15:39:08 +11001819#ifdef XFS_BUF_TRACE
Lachlan McIlroy5695ef42008-08-13 16:51:57 +10001820 xfs_buf_trace_buf = ktrace_alloc(XFS_BUF_TRACE_SIZE, KM_NOFS);
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001821#endif
1822
Nathan Scott87582802006-03-14 13:18:19 +11001823 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
1824 KM_ZONE_HWALIGN, NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11001825 if (!xfs_buf_zone)
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001826 goto out_free_trace_buf;
1827
Rafael J. Wysockib4337692007-03-22 00:11:27 -08001828 xfslogd_workqueue = create_workqueue("xfslogd");
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001829 if (!xfslogd_workqueue)
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001830 goto out_free_buf_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Rafael J. Wysockib4337692007-03-22 00:11:27 -08001832 xfsdatad_workqueue = create_workqueue("xfsdatad");
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001833 if (!xfsdatad_workqueue)
1834 goto out_destroy_xfslogd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Dave Chinnerc626d172009-04-06 18:42:11 +02001836 xfsconvertd_workqueue = create_workqueue("xfsconvertd");
1837 if (!xfsconvertd_workqueue)
1838 goto out_destroy_xfsdatad_workqueue;
1839
Rusty Russell8e1f9362007-07-17 04:03:17 -07001840 register_shrinker(&xfs_buf_shake);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001841 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Dave Chinnerc626d172009-04-06 18:42:11 +02001843 out_destroy_xfsdatad_workqueue:
1844 destroy_workqueue(xfsdatad_workqueue);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001845 out_destroy_xfslogd_workqueue:
1846 destroy_workqueue(xfslogd_workqueue);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001847 out_free_buf_zone:
Nathan Scottce8e9222006-01-11 15:39:08 +11001848 kmem_zone_destroy(xfs_buf_zone);
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001849 out_free_trace_buf:
Nathan Scottce8e9222006-01-11 15:39:08 +11001850#ifdef XFS_BUF_TRACE
1851 ktrace_free(xfs_buf_trace_buf);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001852#endif
Nathan Scott87582802006-03-14 13:18:19 +11001853 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854}
1855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856void
Nathan Scottce8e9222006-01-11 15:39:08 +11001857xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858{
Rusty Russell8e1f9362007-07-17 04:03:17 -07001859 unregister_shrinker(&xfs_buf_shake);
Dave Chinnerc626d172009-04-06 18:42:11 +02001860 destroy_workqueue(xfsconvertd_workqueue);
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001861 destroy_workqueue(xfsdatad_workqueue);
1862 destroy_workqueue(xfslogd_workqueue);
Nathan Scottce8e9222006-01-11 15:39:08 +11001863 kmem_zone_destroy(xfs_buf_zone);
1864#ifdef XFS_BUF_TRACE
1865 ktrace_free(xfs_buf_trace_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867}
Tim Shimmine6a0e9c2007-05-08 13:49:59 +10001868
1869#ifdef CONFIG_KDB_MODULES
1870struct list_head *
1871xfs_get_buftarg_list(void)
1872{
1873 return &xfs_buftarg_list;
1874}
1875#endif