]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/xfs/linux-2.6/xfs_buf.c
xfs: remove old vmap cache
[mirror_ubuntu-jammy-kernel.git] / fs / xfs / linux-2.6 / xfs_buf.c
CommitLineData
1da177e4 1/*
f07c2250 2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
7b718769 3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
9 * 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.
1da177e4 13 *
7b718769
NS
14 * 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
1da177e4 17 */
93c189c1 18#include "xfs.h"
1da177e4
LT
19#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>
4df08c52 32#include <linux/kthread.h>
b20a3503 33#include <linux/migrate.h>
3fcfab16 34#include <linux/backing-dev.h>
7dfb7103 35#include <linux/freezer.h>
089716aa 36#include <linux/list_sort.h>
1da177e4 37
b7963133
CH
38#include "xfs_sb.h"
39#include "xfs_inum.h"
40#include "xfs_ag.h"
41#include "xfs_dmapi.h"
42#include "xfs_mount.h"
0b1b213f 43#include "xfs_trace.h"
b7963133 44
7989cb8e 45static kmem_zone_t *xfs_buf_zone;
a6867a68 46STATIC int xfsbufd(void *);
27496a8c 47STATIC int xfsbufd_wakeup(int, gfp_t);
ce8e922c 48STATIC void xfs_buf_delwri_queue(xfs_buf_t *, int);
8e1f936b
RR
49static struct shrinker xfs_buf_shake = {
50 .shrink = xfsbufd_wakeup,
51 .seeks = DEFAULT_SEEKS,
52};
23ea4032 53
7989cb8e 54static struct workqueue_struct *xfslogd_workqueue;
0829c360 55struct workqueue_struct *xfsdatad_workqueue;
c626d174 56struct workqueue_struct *xfsconvertd_workqueue;
1da177e4 57
ce8e922c
NS
58#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)
1da177e4 62#else
ce8e922c
NS
63# 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)
1da177e4
LT
66#endif
67
ce8e922c
NS
68#define xb_to_gfp(flags) \
69 ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : \
70 ((flags) & XBF_DONT_BLOCK) ? GFP_NOFS : GFP_KERNEL) | __GFP_NOWARN)
1da177e4 71
ce8e922c
NS
72#define xb_to_km(flags) \
73 (((flags) & XBF_DONT_BLOCK) ? KM_NOFS : KM_SLEEP)
1da177e4 74
ce8e922c
NS
75#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));
1da177e4 79
73c77e2c
JB
80static 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
1da177e4 101/*
ce8e922c 102 * Page Region interfaces.
1da177e4 103 *
ce8e922c
NS
104 * 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.
1da177e4 107 *
ce8e922c 108 * Each such region is "bytes per page / bits per long" bytes long.
1da177e4 109 *
ce8e922c
NS
110 * NBPPR == number-of-bytes-per-page-region
111 * BTOPR == bytes-to-page-region (rounded up)
112 * BTOPRT == bytes-to-page-region-truncated (rounded down)
1da177e4
LT
113 */
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
b8f82a4a 147STATIC void
1da177e4
LT
148set_page_region(
149 struct page *page,
150 size_t offset,
151 size_t length)
152{
4c21e2f2
HD
153 set_page_private(page,
154 page_private(page) | page_region_mask(offset, length));
155 if (page_private(page) == ~0UL)
1da177e4
LT
156 SetPageUptodate(page);
157}
158
b8f82a4a 159STATIC int
1da177e4
LT
160test_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
4c21e2f2 167 return (mask && (page_private(page) & mask) == mask);
1da177e4
LT
168}
169
1da177e4 170/*
ce8e922c 171 * Internal xfs_buf_t object manipulation
1da177e4
LT
172 */
173
174STATIC void
ce8e922c
NS
175_xfs_buf_initialize(
176 xfs_buf_t *bp,
1da177e4 177 xfs_buftarg_t *target,
204ab25f 178 xfs_off_t range_base,
1da177e4 179 size_t range_length,
ce8e922c 180 xfs_buf_flags_t flags)
1da177e4
LT
181{
182 /*
ce8e922c 183 * We don't want certain flags to appear in b_flags.
1da177e4 184 */
ce8e922c
NS
185 flags &= ~(XBF_LOCK|XBF_MAPPED|XBF_DONT_BLOCK|XBF_READ_AHEAD);
186
187 memset(bp, 0, sizeof(xfs_buf_t));
188 atomic_set(&bp->b_hold, 1);
b4dd330b 189 init_completion(&bp->b_iowait);
ce8e922c
NS
190 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;
1da177e4
LT
196 /*
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 */
ce8e922c
NS
201 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);
206
207 XFS_STATS_INC(xb_create);
0b1b213f
CH
208
209 trace_xfs_buf_init(bp, _RET_IP_);
1da177e4
LT
210}
211
212/*
ce8e922c
NS
213 * Allocate a page array capable of holding a specified number
214 * of pages, and point the page buf at it.
1da177e4
LT
215 */
216STATIC int
ce8e922c
NS
217_xfs_buf_get_pages(
218 xfs_buf_t *bp,
1da177e4 219 int page_count,
ce8e922c 220 xfs_buf_flags_t flags)
1da177e4
LT
221{
222 /* Make sure that we have a page list */
ce8e922c
NS
223 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;
1da177e4 228 } else {
ce8e922c
NS
229 bp->b_pages = kmem_alloc(sizeof(struct page *) *
230 page_count, xb_to_km(flags));
231 if (bp->b_pages == NULL)
1da177e4
LT
232 return -ENOMEM;
233 }
ce8e922c 234 memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
1da177e4
LT
235 }
236 return 0;
237}
238
239/*
ce8e922c 240 * Frees b_pages if it was allocated.
1da177e4
LT
241 */
242STATIC void
ce8e922c 243_xfs_buf_free_pages(
1da177e4
LT
244 xfs_buf_t *bp)
245{
ce8e922c 246 if (bp->b_pages != bp->b_page_array) {
f0e2d93c 247 kmem_free(bp->b_pages);
3fc98b1a 248 bp->b_pages = NULL;
1da177e4
LT
249 }
250}
251
252/*
253 * Releases the specified buffer.
254 *
255 * The modification state of any associated pages is left unchanged.
ce8e922c 256 * The buffer most not be on any hash - use xfs_buf_rele instead for
1da177e4
LT
257 * hashed and refcounted buffers
258 */
259void
ce8e922c 260xfs_buf_free(
1da177e4
LT
261 xfs_buf_t *bp)
262{
0b1b213f 263 trace_xfs_buf_free(bp, _RET_IP_);
1da177e4 264
ce8e922c 265 ASSERT(list_empty(&bp->b_hash_list));
1da177e4 266
1fa40b01 267 if (bp->b_flags & (_XBF_PAGE_CACHE|_XBF_PAGES)) {
1da177e4
LT
268 uint i;
269
73c77e2c 270 if (xfs_buf_is_vmapped(bp))
cd9640a7 271 vunmap(bp->b_addr - bp->b_offset);
1da177e4 272
948ecdb4
NS
273 for (i = 0; i < bp->b_page_count; i++) {
274 struct page *page = bp->b_pages[i];
275
1fa40b01
CH
276 if (bp->b_flags & _XBF_PAGE_CACHE)
277 ASSERT(!PagePrivate(page));
948ecdb4
NS
278 page_cache_release(page);
279 }
1da177e4 280 }
3fc98b1a 281 _xfs_buf_free_pages(bp);
ce8e922c 282 xfs_buf_deallocate(bp);
1da177e4
LT
283}
284
285/*
286 * Finds all pages for buffer in question and builds it's page list.
287 */
288STATIC int
ce8e922c 289_xfs_buf_lookup_pages(
1da177e4
LT
290 xfs_buf_t *bp,
291 uint flags)
292{
ce8e922c
NS
293 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;
1da177e4 296 size_t nbytes, offset;
ce8e922c 297 gfp_t gfp_mask = xb_to_gfp(flags);
1da177e4
LT
298 unsigned short page_count, i;
299 pgoff_t first;
204ab25f 300 xfs_off_t end;
1da177e4
LT
301 int error;
302
ce8e922c
NS
303 end = bp->b_file_offset + bp->b_buffer_length;
304 page_count = xfs_buf_btoc(end) - xfs_buf_btoct(bp->b_file_offset);
1da177e4 305
ce8e922c 306 error = _xfs_buf_get_pages(bp, page_count, flags);
1da177e4
LT
307 if (unlikely(error))
308 return error;
ce8e922c 309 bp->b_flags |= _XBF_PAGE_CACHE;
1da177e4 310
ce8e922c
NS
311 offset = bp->b_offset;
312 first = bp->b_file_offset >> PAGE_CACHE_SHIFT;
1da177e4 313
ce8e922c 314 for (i = 0; i < bp->b_page_count; i++) {
1da177e4
LT
315 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)) {
ce8e922c
NS
321 if (flags & XBF_READ_AHEAD) {
322 bp->b_page_count = i;
6ab455ee
CH
323 for (i = 0; i < bp->b_page_count; i++)
324 unlock_page(bp->b_pages[i]);
1da177e4
LT
325 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",
34a622b2 338 __func__, gfp_mask);
1da177e4 339
ce8e922c 340 XFS_STATS_INC(xb_page_retries);
23ea4032 341 xfsbufd_wakeup(0, gfp_mask);
8aa7e847 342 congestion_wait(BLK_RW_ASYNC, HZ/50);
1da177e4
LT
343 goto retry;
344 }
345
ce8e922c 346 XFS_STATS_INC(xb_page_found);
1da177e4
LT
347
348 nbytes = min_t(size_t, size, PAGE_CACHE_SIZE - offset);
349 size -= nbytes;
350
948ecdb4 351 ASSERT(!PagePrivate(page));
1da177e4
LT
352 if (!PageUptodate(page)) {
353 page_count--;
6ab455ee
CH
354 if (blocksize >= PAGE_CACHE_SIZE) {
355 if (flags & XBF_READ)
356 bp->b_flags |= _XBF_PAGE_LOCKED;
357 } else if (!PagePrivate(page)) {
1da177e4
LT
358 if (test_page_region(page, offset, nbytes))
359 page_count++;
360 }
361 }
362
ce8e922c 363 bp->b_pages[i] = page;
1da177e4
LT
364 offset = 0;
365 }
366
6ab455ee
CH
367 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
ce8e922c
NS
372 if (page_count == bp->b_page_count)
373 bp->b_flags |= XBF_DONE;
1da177e4 374
1da177e4
LT
375 return error;
376}
377
378/*
379 * Map buffer into kernel address-space if nessecary.
380 */
381STATIC int
ce8e922c 382_xfs_buf_map_pages(
1da177e4
LT
383 xfs_buf_t *bp,
384 uint flags)
385{
386 /* A single page buffer is always mappable */
ce8e922c
NS
387 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) {
cf7dab80
FB
391 bp->b_addr = vmap(bp->b_pages, bp->b_page_count,
392 VM_MAP, PAGE_KERNEL);
ce8e922c 393 if (unlikely(bp->b_addr == NULL))
1da177e4 394 return -ENOMEM;
ce8e922c
NS
395 bp->b_addr += bp->b_offset;
396 bp->b_flags |= XBF_MAPPED;
1da177e4
LT
397 }
398
399 return 0;
400}
401
402/*
403 * Finding and Reading Buffers
404 */
405
406/*
ce8e922c 407 * Look up, and creates if absent, a lockable buffer for
1da177e4
LT
408 * 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 *
ce8e922c 415_xfs_buf_find(
1da177e4 416 xfs_buftarg_t *btp, /* block device target */
204ab25f 417 xfs_off_t ioff, /* starting offset of range */
1da177e4 418 size_t isize, /* length of range */
ce8e922c
NS
419 xfs_buf_flags_t flags,
420 xfs_buf_t *new_bp)
1da177e4 421{
204ab25f 422 xfs_off_t range_base;
1da177e4
LT
423 size_t range_length;
424 xfs_bufhash_t *hash;
ce8e922c 425 xfs_buf_t *bp, *n;
1da177e4
LT
426
427 range_base = (ioff << BBSHIFT);
428 range_length = (isize << BBSHIFT);
429
430 /* Check for IOs smaller than the sector size / not sector aligned */
ce8e922c 431 ASSERT(!(range_length < (1 << btp->bt_sshift)));
204ab25f 432 ASSERT(!(range_base & (xfs_off_t)btp->bt_smask));
1da177e4
LT
433
434 hash = &btp->bt_hash[hash_long((unsigned long)ioff, btp->bt_hashshift)];
435
436 spin_lock(&hash->bh_lock);
437
ce8e922c
NS
438 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) {
1da177e4 442 /*
ce8e922c 443 * If we look at something, bring it to the
1da177e4
LT
444 * front of the list for next time.
445 */
ce8e922c
NS
446 atomic_inc(&bp->b_hold);
447 list_move(&bp->b_hash_list, &hash->bh_list);
1da177e4
LT
448 goto found;
449 }
450 }
451
452 /* No match found */
ce8e922c
NS
453 if (new_bp) {
454 _xfs_buf_initialize(new_bp, btp, range_base,
1da177e4 455 range_length, flags);
ce8e922c
NS
456 new_bp->b_hash = hash;
457 list_add(&new_bp->b_hash_list, &hash->bh_list);
1da177e4 458 } else {
ce8e922c 459 XFS_STATS_INC(xb_miss_locked);
1da177e4
LT
460 }
461
462 spin_unlock(&hash->bh_lock);
ce8e922c 463 return new_bp;
1da177e4
LT
464
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 */
ce8e922c
NS
472 if (down_trylock(&bp->b_sema)) {
473 if (!(flags & XBF_TRYLOCK)) {
1da177e4 474 /* wait for buffer ownership */
ce8e922c
NS
475 xfs_buf_lock(bp);
476 XFS_STATS_INC(xb_get_locked_waited);
1da177e4
LT
477 } else {
478 /* We asked for a trylock and failed, no need
479 * to look at file offset and length here, we
ce8e922c
NS
480 * 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.
1da177e4 483 */
ce8e922c
NS
484 xfs_buf_rele(bp);
485 XFS_STATS_INC(xb_busy_locked);
486 return NULL;
1da177e4
LT
487 }
488 } else {
489 /* trylock worked */
ce8e922c 490 XB_SET_OWNER(bp);
1da177e4
LT
491 }
492
ce8e922c
NS
493 if (bp->b_flags & XBF_STALE) {
494 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
495 bp->b_flags &= XBF_MAPPED;
2f926587 496 }
0b1b213f
CH
497
498 trace_xfs_buf_find(bp, flags, _RET_IP_);
ce8e922c
NS
499 XFS_STATS_INC(xb_get_locked);
500 return bp;
1da177e4
LT
501}
502
503/*
ce8e922c 504 * Assembles a buffer covering the specified range.
1da177e4
LT
505 * Storage in memory for all portions of the buffer will be allocated,
506 * although backing storage may not be.
507 */
508xfs_buf_t *
6ad112bf 509xfs_buf_get(
1da177e4 510 xfs_buftarg_t *target,/* target for buffer */
204ab25f 511 xfs_off_t ioff, /* starting offset of range */
1da177e4 512 size_t isize, /* length of range */
ce8e922c 513 xfs_buf_flags_t flags)
1da177e4 514{
ce8e922c 515 xfs_buf_t *bp, *new_bp;
1da177e4
LT
516 int error = 0, i;
517
ce8e922c
NS
518 new_bp = xfs_buf_allocate(flags);
519 if (unlikely(!new_bp))
1da177e4
LT
520 return NULL;
521
ce8e922c
NS
522 bp = _xfs_buf_find(target, ioff, isize, flags, new_bp);
523 if (bp == new_bp) {
524 error = _xfs_buf_lookup_pages(bp, flags);
1da177e4
LT
525 if (error)
526 goto no_buffer;
527 } else {
ce8e922c
NS
528 xfs_buf_deallocate(new_bp);
529 if (unlikely(bp == NULL))
1da177e4
LT
530 return NULL;
531 }
532
ce8e922c
NS
533 for (i = 0; i < bp->b_page_count; i++)
534 mark_page_accessed(bp->b_pages[i]);
1da177e4 535
ce8e922c
NS
536 if (!(bp->b_flags & XBF_MAPPED)) {
537 error = _xfs_buf_map_pages(bp, flags);
1da177e4
LT
538 if (unlikely(error)) {
539 printk(KERN_WARNING "%s: failed to map pages\n",
34a622b2 540 __func__);
1da177e4
LT
541 goto no_buffer;
542 }
543 }
544
ce8e922c 545 XFS_STATS_INC(xb_get);
1da177e4
LT
546
547 /*
548 * Always fill in the block number now, the mapped cases can do
549 * their own overlay of this later.
550 */
ce8e922c
NS
551 bp->b_bn = ioff;
552 bp->b_count_desired = bp->b_buffer_length;
1da177e4 553
0b1b213f 554 trace_xfs_buf_get(bp, flags, _RET_IP_);
ce8e922c 555 return bp;
1da177e4
LT
556
557 no_buffer:
ce8e922c
NS
558 if (flags & (XBF_LOCK | XBF_TRYLOCK))
559 xfs_buf_unlock(bp);
560 xfs_buf_rele(bp);
1da177e4
LT
561 return NULL;
562}
563
5d765b97
CH
564STATIC int
565_xfs_buf_read(
566 xfs_buf_t *bp,
567 xfs_buf_flags_t flags)
568{
569 int status;
570
5d765b97
CH
571 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
1da177e4 585xfs_buf_t *
6ad112bf 586xfs_buf_read(
1da177e4 587 xfs_buftarg_t *target,
204ab25f 588 xfs_off_t ioff,
1da177e4 589 size_t isize,
ce8e922c 590 xfs_buf_flags_t flags)
1da177e4 591{
ce8e922c
NS
592 xfs_buf_t *bp;
593
594 flags |= XBF_READ;
595
6ad112bf 596 bp = xfs_buf_get(target, ioff, isize, flags);
ce8e922c 597 if (bp) {
0b1b213f
CH
598 trace_xfs_buf_read(bp, flags, _RET_IP_);
599
ce8e922c 600 if (!XFS_BUF_ISDONE(bp)) {
ce8e922c 601 XFS_STATS_INC(xb_get_read);
5d765b97 602 _xfs_buf_read(bp, flags);
ce8e922c 603 } else if (flags & XBF_ASYNC) {
1da177e4
LT
604 /*
605 * Read ahead call which is already satisfied,
606 * drop the buffer
607 */
608 goto no_buffer;
609 } else {
1da177e4 610 /* We do not want read in the flags */
ce8e922c 611 bp->b_flags &= ~XBF_READ;
1da177e4
LT
612 }
613 }
614
ce8e922c 615 return bp;
1da177e4
LT
616
617 no_buffer:
ce8e922c
NS
618 if (flags & (XBF_LOCK | XBF_TRYLOCK))
619 xfs_buf_unlock(bp);
620 xfs_buf_rele(bp);
1da177e4
LT
621 return NULL;
622}
623
1da177e4 624/*
ce8e922c
NS
625 * If we are not low on memory then do the readahead in a deadlock
626 * safe manner.
1da177e4
LT
627 */
628void
ce8e922c 629xfs_buf_readahead(
1da177e4 630 xfs_buftarg_t *target,
204ab25f 631 xfs_off_t ioff,
1da177e4 632 size_t isize,
ce8e922c 633 xfs_buf_flags_t flags)
1da177e4
LT
634{
635 struct backing_dev_info *bdi;
636
ce8e922c 637 bdi = target->bt_mapping->backing_dev_info;
1da177e4
LT
638 if (bdi_read_congested(bdi))
639 return;
640
ce8e922c 641 flags |= (XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD);
6ad112bf 642 xfs_buf_read(target, ioff, isize, flags);
1da177e4
LT
643}
644
645xfs_buf_t *
ce8e922c 646xfs_buf_get_empty(
1da177e4
LT
647 size_t len,
648 xfs_buftarg_t *target)
649{
ce8e922c 650 xfs_buf_t *bp;
1da177e4 651
ce8e922c
NS
652 bp = xfs_buf_allocate(0);
653 if (bp)
654 _xfs_buf_initialize(bp, target, 0, len, 0);
655 return bp;
1da177e4
LT
656}
657
658static inline struct page *
659mem_to_page(
660 void *addr)
661{
9e2779fa 662 if ((!is_vmalloc_addr(addr))) {
1da177e4
LT
663 return virt_to_page(addr);
664 } else {
665 return vmalloc_to_page(addr);
666 }
667}
668
669int
ce8e922c
NS
670xfs_buf_associate_memory(
671 xfs_buf_t *bp,
1da177e4
LT
672 void *mem,
673 size_t len)
674{
675 int rval;
676 int i = 0;
d1afb678
LM
677 unsigned long pageaddr;
678 unsigned long offset;
679 size_t buflen;
1da177e4
LT
680 int page_count;
681
d1afb678
LM
682 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;
1da177e4
LT
686
687 /* Free any previous set of page pointers */
ce8e922c
NS
688 if (bp->b_pages)
689 _xfs_buf_free_pages(bp);
1da177e4 690
ce8e922c
NS
691 bp->b_pages = NULL;
692 bp->b_addr = mem;
1da177e4 693
36fae17a 694 rval = _xfs_buf_get_pages(bp, page_count, XBF_DONT_BLOCK);
1da177e4
LT
695 if (rval)
696 return rval;
697
ce8e922c 698 bp->b_offset = offset;
d1afb678
LM
699
700 for (i = 0; i < bp->b_page_count; i++) {
701 bp->b_pages[i] = mem_to_page((void *)pageaddr);
702 pageaddr += PAGE_CACHE_SIZE;
1da177e4 703 }
1da177e4 704
d1afb678
LM
705 bp->b_count_desired = len;
706 bp->b_buffer_length = buflen;
ce8e922c 707 bp->b_flags |= XBF_MAPPED;
6ab455ee 708 bp->b_flags &= ~_XBF_PAGE_LOCKED;
1da177e4
LT
709
710 return 0;
711}
712
713xfs_buf_t *
ce8e922c 714xfs_buf_get_noaddr(
1da177e4
LT
715 size_t len,
716 xfs_buftarg_t *target)
717{
1fa40b01
CH
718 unsigned long page_count = PAGE_ALIGN(len) >> PAGE_SHIFT;
719 int error, i;
1da177e4 720 xfs_buf_t *bp;
1da177e4 721
ce8e922c 722 bp = xfs_buf_allocate(0);
1da177e4
LT
723 if (unlikely(bp == NULL))
724 goto fail;
ce8e922c 725 _xfs_buf_initialize(bp, target, 0, len, 0);
1da177e4 726
1fa40b01
CH
727 error = _xfs_buf_get_pages(bp, page_count, 0);
728 if (error)
1da177e4
LT
729 goto fail_free_buf;
730
1fa40b01
CH
731 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;
1da177e4 735 }
1fa40b01 736 bp->b_flags |= _XBF_PAGES;
1da177e4 737
1fa40b01
CH
738 error = _xfs_buf_map_pages(bp, XBF_MAPPED);
739 if (unlikely(error)) {
740 printk(KERN_WARNING "%s: failed to map pages\n",
34a622b2 741 __func__);
1da177e4 742 goto fail_free_mem;
1fa40b01 743 }
1da177e4 744
ce8e922c 745 xfs_buf_unlock(bp);
1da177e4 746
0b1b213f 747 trace_xfs_buf_get_noaddr(bp, _RET_IP_);
1da177e4 748 return bp;
1fa40b01 749
1da177e4 750 fail_free_mem:
1fa40b01
CH
751 while (--i >= 0)
752 __free_page(bp->b_pages[i]);
ca165b88 753 _xfs_buf_free_pages(bp);
1da177e4 754 fail_free_buf:
ca165b88 755 xfs_buf_deallocate(bp);
1da177e4
LT
756 fail:
757 return NULL;
758}
759
760/*
1da177e4
LT
761 * Increment reference count on buffer, to hold the buffer concurrently
762 * with another thread which may release (free) the buffer asynchronously.
1da177e4
LT
763 * Must hold the buffer already to call this function.
764 */
765void
ce8e922c
NS
766xfs_buf_hold(
767 xfs_buf_t *bp)
1da177e4 768{
0b1b213f 769 trace_xfs_buf_hold(bp, _RET_IP_);
ce8e922c 770 atomic_inc(&bp->b_hold);
1da177e4
LT
771}
772
773/*
ce8e922c
NS
774 * Releases a hold on the specified buffer. If the
775 * the hold count is 1, calls xfs_buf_free.
1da177e4
LT
776 */
777void
ce8e922c
NS
778xfs_buf_rele(
779 xfs_buf_t *bp)
1da177e4 780{
ce8e922c 781 xfs_bufhash_t *hash = bp->b_hash;
1da177e4 782
0b1b213f 783 trace_xfs_buf_rele(bp, _RET_IP_);
1da177e4 784
fad3aa1e
NS
785 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
3790689f 792 ASSERT(atomic_read(&bp->b_hold) > 0);
ce8e922c
NS
793 if (atomic_dec_and_lock(&bp->b_hold, &hash->bh_lock)) {
794 if (bp->b_relse) {
795 atomic_inc(&bp->b_hold);
1da177e4 796 spin_unlock(&hash->bh_lock);
ce8e922c
NS
797 (*(bp->b_relse)) (bp);
798 } else if (bp->b_flags & XBF_FS_MANAGED) {
1da177e4 799 spin_unlock(&hash->bh_lock);
1da177e4 800 } else {
ce8e922c
NS
801 ASSERT(!(bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)));
802 list_del_init(&bp->b_hash_list);
1da177e4 803 spin_unlock(&hash->bh_lock);
ce8e922c 804 xfs_buf_free(bp);
1da177e4
LT
805 }
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/*
ce8e922c
NS
821 * 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.
1da177e4
LT
825 */
826int
ce8e922c
NS
827xfs_buf_cond_lock(
828 xfs_buf_t *bp)
1da177e4
LT
829{
830 int locked;
831
ce8e922c 832 locked = down_trylock(&bp->b_sema) == 0;
0b1b213f 833 if (locked)
ce8e922c 834 XB_SET_OWNER(bp);
0b1b213f
CH
835
836 trace_xfs_buf_cond_lock(bp, _RET_IP_);
ce8e922c 837 return locked ? 0 : -EBUSY;
1da177e4
LT
838}
839
1da177e4 840int
ce8e922c
NS
841xfs_buf_lock_value(
842 xfs_buf_t *bp)
1da177e4 843{
adaa693b 844 return bp->b_sema.count;
1da177e4 845}
1da177e4
LT
846
847/*
ce8e922c
NS
848 * 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.
1da177e4 852 */
ce8e922c
NS
853void
854xfs_buf_lock(
855 xfs_buf_t *bp)
1da177e4 856{
0b1b213f
CH
857 trace_xfs_buf_lock(bp, _RET_IP_);
858
ce8e922c
NS
859 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);
0b1b213f
CH
863
864 trace_xfs_buf_lock_done(bp, _RET_IP_);
1da177e4
LT
865}
866
867/*
ce8e922c 868 * Releases the lock on the buffer object.
2f926587 869 * If the buffer is marked delwri but is not queued, do so before we
ce8e922c 870 * unlock the buffer as we need to set flags correctly. We also need to
2f926587
DC
871 * 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.
1da177e4
LT
873 */
874void
ce8e922c
NS
875xfs_buf_unlock(
876 xfs_buf_t *bp)
1da177e4 877{
ce8e922c
NS
878 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);
2f926587
DC
882 }
883
ce8e922c
NS
884 XB_CLEAR_OWNER(bp);
885 up(&bp->b_sema);
0b1b213f
CH
886
887 trace_xfs_buf_unlock(bp, _RET_IP_);
1da177e4
LT
888}
889
890
891/*
892 * Pinning Buffer Storage in Memory
ce8e922c 893 * Ensure that no attempt to force a buffer to disk will succeed.
1da177e4
LT
894 */
895void
ce8e922c
NS
896xfs_buf_pin(
897 xfs_buf_t *bp)
1da177e4 898{
0b1b213f 899 trace_xfs_buf_pin(bp, _RET_IP_);
ce8e922c 900 atomic_inc(&bp->b_pin_count);
1da177e4
LT
901}
902
1da177e4 903void
ce8e922c
NS
904xfs_buf_unpin(
905 xfs_buf_t *bp)
1da177e4 906{
0b1b213f
CH
907 trace_xfs_buf_unpin(bp, _RET_IP_);
908
ce8e922c
NS
909 if (atomic_dec_and_test(&bp->b_pin_count))
910 wake_up_all(&bp->b_waiters);
1da177e4
LT
911}
912
913int
ce8e922c
NS
914xfs_buf_ispin(
915 xfs_buf_t *bp)
1da177e4 916{
ce8e922c 917 return atomic_read(&bp->b_pin_count);
1da177e4
LT
918}
919
ce8e922c
NS
920STATIC void
921xfs_buf_wait_unpin(
922 xfs_buf_t *bp)
1da177e4
LT
923{
924 DECLARE_WAITQUEUE (wait, current);
925
ce8e922c 926 if (atomic_read(&bp->b_pin_count) == 0)
1da177e4
LT
927 return;
928
ce8e922c 929 add_wait_queue(&bp->b_waiters, &wait);
1da177e4
LT
930 for (;;) {
931 set_current_state(TASK_UNINTERRUPTIBLE);
ce8e922c 932 if (atomic_read(&bp->b_pin_count) == 0)
1da177e4 933 break;
ce8e922c
NS
934 if (atomic_read(&bp->b_io_remaining))
935 blk_run_address_space(bp->b_target->bt_mapping);
1da177e4
LT
936 schedule();
937 }
ce8e922c 938 remove_wait_queue(&bp->b_waiters, &wait);
1da177e4
LT
939 set_current_state(TASK_RUNNING);
940}
941
942/*
943 * Buffer Utility Routines
944 */
945
1da177e4 946STATIC void
ce8e922c 947xfs_buf_iodone_work(
c4028958 948 struct work_struct *work)
1da177e4 949{
c4028958
DH
950 xfs_buf_t *bp =
951 container_of(work, xfs_buf_t, b_iodone_work);
1da177e4 952
0bfefc46
DC
953 /*
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
73f6aa4d 957 * need to check if the _XFS_BARRIER_FAILED flag was set during I/O completion.
0bfefc46
DC
958 */
959 if ((bp->b_error == EOPNOTSUPP) &&
960 (bp->b_flags & (XBF_ORDERED|XBF_ASYNC)) == (XBF_ORDERED|XBF_ASYNC)) {
0b1b213f 961 trace_xfs_buf_ordered_retry(bp, _RET_IP_);
0bfefc46 962 bp->b_flags &= ~XBF_ORDERED;
73f6aa4d 963 bp->b_flags |= _XFS_BARRIER_FAILED;
0bfefc46
DC
964 xfs_buf_iorequest(bp);
965 } else if (bp->b_iodone)
ce8e922c
NS
966 (*(bp->b_iodone))(bp);
967 else if (bp->b_flags & XBF_ASYNC)
1da177e4
LT
968 xfs_buf_relse(bp);
969}
970
971void
ce8e922c
NS
972xfs_buf_ioend(
973 xfs_buf_t *bp,
1da177e4
LT
974 int schedule)
975{
0b1b213f
CH
976 trace_xfs_buf_iodone(bp, _RET_IP_);
977
77be55a5 978 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
ce8e922c
NS
979 if (bp->b_error == 0)
980 bp->b_flags |= XBF_DONE;
1da177e4 981
ce8e922c 982 if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) {
1da177e4 983 if (schedule) {
c4028958 984 INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
ce8e922c 985 queue_work(xfslogd_workqueue, &bp->b_iodone_work);
1da177e4 986 } else {
c4028958 987 xfs_buf_iodone_work(&bp->b_iodone_work);
1da177e4
LT
988 }
989 } else {
b4dd330b 990 complete(&bp->b_iowait);
1da177e4
LT
991 }
992}
993
1da177e4 994void
ce8e922c
NS
995xfs_buf_ioerror(
996 xfs_buf_t *bp,
997 int error)
1da177e4
LT
998{
999 ASSERT(error >= 0 && error <= 0xffff);
ce8e922c 1000 bp->b_error = (unsigned short)error;
0b1b213f 1001 trace_xfs_buf_ioerror(bp, error, _RET_IP_);
1da177e4
LT
1002}
1003
1da177e4 1004int
64e0bc7d
CH
1005xfs_bwrite(
1006 struct xfs_mount *mp,
5d765b97 1007 struct xfs_buf *bp)
1da177e4 1008{
64e0bc7d
CH
1009 int iowait = (bp->b_flags & XBF_ASYNC) == 0;
1010 int error = 0;
1da177e4 1011
64e0bc7d
CH
1012 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;
1da177e4 1017
5d765b97 1018 xfs_buf_delwri_dequeue(bp);
64e0bc7d 1019 xfs_buf_iostrategy(bp);
1da177e4 1020
64e0bc7d
CH
1021 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 }
1da177e4 1027
64e0bc7d 1028 return error;
5d765b97 1029}
1da177e4 1030
5d765b97
CH
1031void
1032xfs_bdwrite(
1033 void *mp,
1034 struct xfs_buf *bp)
1035{
0b1b213f 1036 trace_xfs_buf_bdwrite(bp, _RET_IP_);
1da177e4 1037
5d765b97 1038 bp->b_strat = xfs_bdstrat_cb;
15ac08a8 1039 bp->b_mount = mp;
1da177e4 1040
5d765b97
CH
1041 bp->b_flags &= ~XBF_READ;
1042 bp->b_flags |= (XBF_DELWRI | XBF_ASYNC);
1043
1044 xfs_buf_delwri_queue(bp, 1);
1da177e4
LT
1045}
1046
4e23471a
CH
1047/*
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);
0cadda1c 1104 if (!(fl & XBF_ASYNC)) {
4e23471a
CH
1105 /*
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
b8f82a4a 1167STATIC void
ce8e922c
NS
1168_xfs_buf_ioend(
1169 xfs_buf_t *bp,
1da177e4
LT
1170 int schedule)
1171{
6ab455ee
CH
1172 if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
1173 bp->b_flags &= ~_XBF_PAGE_LOCKED;
ce8e922c 1174 xfs_buf_ioend(bp, schedule);
6ab455ee 1175 }
1da177e4
LT
1176}
1177
782e3b3b 1178STATIC void
ce8e922c 1179xfs_buf_bio_end_io(
1da177e4 1180 struct bio *bio,
1da177e4
LT
1181 int error)
1182{
ce8e922c
NS
1183 xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
1184 unsigned int blocksize = bp->b_target->bt_bsize;
eedb5530 1185 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1da177e4 1186
cfbe5267 1187 xfs_buf_ioerror(bp, -error);
1da177e4 1188
73c77e2c
JB
1189 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
eedb5530 1192 do {
1da177e4
LT
1193 struct page *page = bvec->bv_page;
1194
948ecdb4 1195 ASSERT(!PagePrivate(page));
ce8e922c
NS
1196 if (unlikely(bp->b_error)) {
1197 if (bp->b_flags & XBF_READ)
eedb5530 1198 ClearPageUptodate(page);
ce8e922c 1199 } else if (blocksize >= PAGE_CACHE_SIZE) {
1da177e4
LT
1200 SetPageUptodate(page);
1201 } else if (!PagePrivate(page) &&
ce8e922c 1202 (bp->b_flags & _XBF_PAGE_CACHE)) {
1da177e4
LT
1203 set_page_region(page, bvec->bv_offset, bvec->bv_len);
1204 }
1205
eedb5530
NS
1206 if (--bvec >= bio->bi_io_vec)
1207 prefetchw(&bvec->bv_page->flags);
6ab455ee
CH
1208
1209 if (bp->b_flags & _XBF_PAGE_LOCKED)
1210 unlock_page(page);
eedb5530 1211 } while (bvec >= bio->bi_io_vec);
1da177e4 1212
ce8e922c 1213 _xfs_buf_ioend(bp, 1);
1da177e4 1214 bio_put(bio);
1da177e4
LT
1215}
1216
1217STATIC void
ce8e922c
NS
1218_xfs_buf_ioapply(
1219 xfs_buf_t *bp)
1da177e4 1220{
a9759f2d 1221 int rw, map_i, total_nr_pages, nr_pages;
1da177e4 1222 struct bio *bio;
ce8e922c
NS
1223 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;
1da177e4 1227
ce8e922c 1228 total_nr_pages = bp->b_page_count;
1da177e4
LT
1229 map_i = 0;
1230
ce8e922c
NS
1231 if (bp->b_flags & XBF_ORDERED) {
1232 ASSERT(!(bp->b_flags & XBF_READ));
f538d4da 1233 rw = WRITE_BARRIER;
2ee1abad 1234 } else if (bp->b_flags & XBF_LOG_BUFFER) {
51bdd706
NS
1235 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;
2ee1abad
DC
1238 } 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;
51bdd706
NS
1242 } else {
1243 rw = (bp->b_flags & XBF_WRITE) ? WRITE :
1244 (bp->b_flags & XBF_READ_AHEAD) ? READA : READ;
f538d4da
CH
1245 }
1246
ce8e922c 1247 /* Special code path for reading a sub page size buffer in --
1da177e4
LT
1248 * we populate up the whole page, and hence the other metadata
1249 * in the same page. This optimization is only valid when the
ce8e922c 1250 * filesystem block size is not smaller than the page size.
1da177e4 1251 */
ce8e922c 1252 if ((bp->b_buffer_length < PAGE_CACHE_SIZE) &&
6ab455ee
CH
1253 ((bp->b_flags & (XBF_READ|_XBF_PAGE_LOCKED)) ==
1254 (XBF_READ|_XBF_PAGE_LOCKED)) &&
ce8e922c 1255 (blocksize >= PAGE_CACHE_SIZE)) {
1da177e4
LT
1256 bio = bio_alloc(GFP_NOIO, 1);
1257
ce8e922c 1258 bio->bi_bdev = bp->b_target->bt_bdev;
1da177e4 1259 bio->bi_sector = sector - (offset >> BBSHIFT);
ce8e922c
NS
1260 bio->bi_end_io = xfs_buf_bio_end_io;
1261 bio->bi_private = bp;
1da177e4 1262
ce8e922c 1263 bio_add_page(bio, bp->b_pages[0], PAGE_CACHE_SIZE, 0);
1da177e4
LT
1264 size = 0;
1265
ce8e922c 1266 atomic_inc(&bp->b_io_remaining);
1da177e4
LT
1267
1268 goto submit_io;
1269 }
1270
1da177e4 1271next_chunk:
ce8e922c 1272 atomic_inc(&bp->b_io_remaining);
1da177e4
LT
1273 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);
ce8e922c 1278 bio->bi_bdev = bp->b_target->bt_bdev;
1da177e4 1279 bio->bi_sector = sector;
ce8e922c
NS
1280 bio->bi_end_io = xfs_buf_bio_end_io;
1281 bio->bi_private = bp;
1da177e4
LT
1282
1283 for (; size && nr_pages; nr_pages--, map_i++) {
ce8e922c 1284 int rbytes, nbytes = PAGE_CACHE_SIZE - offset;
1da177e4
LT
1285
1286 if (nbytes > size)
1287 nbytes = size;
1288
ce8e922c
NS
1289 rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset);
1290 if (rbytes < nbytes)
1da177e4
LT
1291 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)) {
73c77e2c
JB
1301 if (xfs_buf_is_vmapped(bp)) {
1302 flush_kernel_vmap_range(bp->b_addr,
1303 xfs_buf_vmap_len(bp));
1304 }
1da177e4
LT
1305 submit_bio(rw, bio);
1306 if (size)
1307 goto next_chunk;
1308 } else {
1309 bio_put(bio);
ce8e922c 1310 xfs_buf_ioerror(bp, EIO);
1da177e4
LT
1311 }
1312}
1313
1da177e4 1314int
ce8e922c
NS
1315xfs_buf_iorequest(
1316 xfs_buf_t *bp)
1da177e4 1317{
0b1b213f 1318 trace_xfs_buf_iorequest(bp, _RET_IP_);
1da177e4 1319
ce8e922c
NS
1320 if (bp->b_flags & XBF_DELWRI) {
1321 xfs_buf_delwri_queue(bp, 1);
1da177e4
LT
1322 return 0;
1323 }
1324
ce8e922c
NS
1325 if (bp->b_flags & XBF_WRITE) {
1326 xfs_buf_wait_unpin(bp);
1da177e4
LT
1327 }
1328
ce8e922c 1329 xfs_buf_hold(bp);
1da177e4
LT
1330
1331 /* Set the count to 1 initially, this will stop an I/O
1332 * completion callout which happens before we have started
ce8e922c 1333 * all the I/O from calling xfs_buf_ioend too early.
1da177e4 1334 */
ce8e922c
NS
1335 atomic_set(&bp->b_io_remaining, 1);
1336 _xfs_buf_ioapply(bp);
1337 _xfs_buf_ioend(bp, 0);
1da177e4 1338
ce8e922c 1339 xfs_buf_rele(bp);
1da177e4
LT
1340 return 0;
1341}
1342
1343/*
ce8e922c
NS
1344 * 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.
1da177e4
LT
1347 */
1348int
ce8e922c
NS
1349xfs_buf_iowait(
1350 xfs_buf_t *bp)
1da177e4 1351{
0b1b213f
CH
1352 trace_xfs_buf_iowait(bp, _RET_IP_);
1353
ce8e922c
NS
1354 if (atomic_read(&bp->b_io_remaining))
1355 blk_run_address_space(bp->b_target->bt_mapping);
b4dd330b 1356 wait_for_completion(&bp->b_iowait);
0b1b213f
CH
1357
1358 trace_xfs_buf_iowait_done(bp, _RET_IP_);
ce8e922c 1359 return bp->b_error;
1da177e4
LT
1360}
1361
ce8e922c
NS
1362xfs_caddr_t
1363xfs_buf_offset(
1364 xfs_buf_t *bp,
1da177e4
LT
1365 size_t offset)
1366{
1367 struct page *page;
1368
ce8e922c
NS
1369 if (bp->b_flags & XBF_MAPPED)
1370 return XFS_BUF_PTR(bp) + offset;
1da177e4 1371
ce8e922c
NS
1372 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));
1da177e4
LT
1375}
1376
1377/*
1da177e4
LT
1378 * Move data into or out of a buffer.
1379 */
1380void
ce8e922c
NS
1381xfs_buf_iomove(
1382 xfs_buf_t *bp, /* buffer to process */
1da177e4
LT
1383 size_t boff, /* starting buffer offset */
1384 size_t bsize, /* length to copy */
b9c48649 1385 void *data, /* data address */
ce8e922c 1386 xfs_buf_rw_t mode) /* read/write/zero flag */
1da177e4
LT
1387{
1388 size_t bend, cpoff, csize;
1389 struct page *page;
1390
1391 bend = boff + bsize;
1392 while (boff < bend) {
ce8e922c
NS
1393 page = bp->b_pages[xfs_buf_btoct(boff + bp->b_offset)];
1394 cpoff = xfs_buf_poff(boff + bp->b_offset);
1da177e4 1395 csize = min_t(size_t,
ce8e922c 1396 PAGE_CACHE_SIZE-cpoff, bp->b_count_desired-boff);
1da177e4
LT
1397
1398 ASSERT(((csize + cpoff) <= PAGE_CACHE_SIZE));
1399
1400 switch (mode) {
ce8e922c 1401 case XBRW_ZERO:
1da177e4
LT
1402 memset(page_address(page) + cpoff, 0, csize);
1403 break;
ce8e922c 1404 case XBRW_READ:
1da177e4
LT
1405 memcpy(data, page_address(page) + cpoff, csize);
1406 break;
ce8e922c 1407 case XBRW_WRITE:
1da177e4
LT
1408 memcpy(page_address(page) + cpoff, data, csize);
1409 }
1410
1411 boff += csize;
1412 data += csize;
1413 }
1414}
1415
1416/*
ce8e922c 1417 * Handling of buffer targets (buftargs).
1da177e4
LT
1418 */
1419
1420/*
ce8e922c
NS
1421 * Wait for any bufs with callbacks that have been submitted but
1422 * have not yet returned... walk the hash list for the target.
1da177e4
LT
1423 */
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);
ce8e922c
NS
1436 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)) {
1da177e4 1439 spin_unlock(&hash->bh_lock);
2f926587
DC
1440 /*
1441 * Catch superblock reference count leaks
1442 * immediately
1443 */
ce8e922c 1444 BUG_ON(bp->b_bn == 0);
1da177e4
LT
1445 delay(100);
1446 goto again;
1447 }
1448 }
1449 spin_unlock(&hash->bh_lock);
1450 }
1451}
1452
1453/*
ce8e922c
NS
1454 * 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.
1da177e4
LT
1457 */
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;
bdfb0430
CH
1467 btp->bt_hash = kmem_zalloc_large((1 << btp->bt_hashshift) *
1468 sizeof(xfs_bufhash_t));
1da177e4
LT
1469 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{
bdfb0430 1479 kmem_free_large(btp->bt_hash);
1da177e4
LT
1480 btp->bt_hash = NULL;
1481}
1482
a6867a68 1483/*
ce8e922c 1484 * buftarg list for delwrite queue processing
a6867a68 1485 */
e6a0e9cd 1486static LIST_HEAD(xfs_buftarg_list);
7989cb8e 1487static DEFINE_SPINLOCK(xfs_buftarg_lock);
a6867a68
DC
1488
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
1da177e4
LT
1507void
1508xfs_free_buftarg(
b7963133
CH
1509 struct xfs_mount *mp,
1510 struct xfs_buftarg *btp)
1da177e4
LT
1511{
1512 xfs_flush_buftarg(btp, 1);
b7963133
CH
1513 if (mp->m_flags & XFS_MOUNT_BARRIER)
1514 xfs_blkdev_issue_flush(btp);
1da177e4 1515 xfs_free_bufhash(btp);
ce8e922c 1516 iput(btp->bt_mapping->host);
a6867a68 1517
ce8e922c
NS
1518 /* Unregister the buftarg first so that we don't get a
1519 * wakeup finding a non-existent task
1520 */
a6867a68
DC
1521 xfs_unregister_buftarg(btp);
1522 kthread_stop(btp->bt_task);
1523
f0e2d93c 1524 kmem_free(btp);
1da177e4
LT
1525}
1526
1da177e4
LT
1527STATIC int
1528xfs_setsize_buftarg_flags(
1529 xfs_buftarg_t *btp,
1530 unsigned int blocksize,
1531 unsigned int sectorsize,
1532 int verbose)
1533{
ce8e922c
NS
1534 btp->bt_bsize = blocksize;
1535 btp->bt_sshift = ffs(sectorsize) - 1;
1536 btp->bt_smask = sectorsize - 1;
1da177e4 1537
ce8e922c 1538 if (set_blocksize(btp->bt_bdev, sectorsize)) {
1da177e4
LT
1539 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/*
ce8e922c
NS
1558 * 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 */
1da177e4
LT
1562STATIC int
1563xfs_setsize_buftarg_early(
1564 xfs_buftarg_t *btp,
1565 struct block_device *bdev)
1566{
1567 return xfs_setsize_buftarg_flags(btp,
e1defc4f 1568 PAGE_CACHE_SIZE, bdev_logical_block_size(bdev), 0);
1da177e4
LT
1569}
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;
f5e54d6e 1588 static const struct address_space_operations mapping_aops = {
1da177e4 1589 .sync_page = block_sync_page,
e965f963 1590 .migratepage = fail_migrate_page,
1da177e4
LT
1591 };
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);
ce8e922c 1610 btp->bt_mapping = mapping;
1da177e4
LT
1611 return 0;
1612}
1613
a6867a68
DC
1614STATIC 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);
007c61c6 1622 spin_lock_init(&btp->bt_delwrite_lock);
a6867a68
DC
1623 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
1da177e4
LT
1634xfs_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
ce8e922c
NS
1643 btp->bt_dev = bdev->bd_dev;
1644 btp->bt_bdev = bdev;
1da177e4
LT
1645 if (xfs_setsize_buftarg_early(btp, bdev))
1646 goto error;
1647 if (xfs_mapping_buftarg(btp, bdev))
1648 goto error;
a6867a68
DC
1649 if (xfs_alloc_delwrite_queue(btp))
1650 goto error;
1da177e4
LT
1651 xfs_alloc_bufhash(btp, external);
1652 return btp;
1653
1654error:
f0e2d93c 1655 kmem_free(btp);
1da177e4
LT
1656 return NULL;
1657}
1658
1659
1660/*
ce8e922c 1661 * Delayed write buffer handling
1da177e4 1662 */
1da177e4 1663STATIC void
ce8e922c
NS
1664xfs_buf_delwri_queue(
1665 xfs_buf_t *bp,
1da177e4
LT
1666 int unlock)
1667{
ce8e922c
NS
1668 struct list_head *dwq = &bp->b_target->bt_delwrite_queue;
1669 spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
a6867a68 1670
0b1b213f
CH
1671 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
1672
ce8e922c 1673 ASSERT((bp->b_flags&(XBF_DELWRI|XBF_ASYNC)) == (XBF_DELWRI|XBF_ASYNC));
1da177e4 1674
a6867a68 1675 spin_lock(dwlk);
1da177e4 1676 /* If already in the queue, dequeue and place at tail */
ce8e922c
NS
1677 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);
1da177e4
LT
1682 }
1683
c9c12971
DC
1684 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
ce8e922c
NS
1689 bp->b_flags |= _XBF_DELWRI_Q;
1690 list_add_tail(&bp->b_list, dwq);
1691 bp->b_queuetime = jiffies;
a6867a68 1692 spin_unlock(dwlk);
1da177e4
LT
1693
1694 if (unlock)
ce8e922c 1695 xfs_buf_unlock(bp);
1da177e4
LT
1696}
1697
1698void
ce8e922c
NS
1699xfs_buf_delwri_dequeue(
1700 xfs_buf_t *bp)
1da177e4 1701{
ce8e922c 1702 spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
1da177e4
LT
1703 int dequeued = 0;
1704
a6867a68 1705 spin_lock(dwlk);
ce8e922c
NS
1706 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);
1da177e4
LT
1709 dequeued = 1;
1710 }
ce8e922c 1711 bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q);
a6867a68 1712 spin_unlock(dwlk);
1da177e4
LT
1713
1714 if (dequeued)
ce8e922c 1715 xfs_buf_rele(bp);
1da177e4 1716
0b1b213f 1717 trace_xfs_buf_delwri_dequeue(bp, _RET_IP_);
1da177e4
LT
1718}
1719
d808f617
DC
1720/*
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
1da177e4 1749STATIC void
ce8e922c 1750xfs_buf_runall_queues(
1da177e4
LT
1751 struct workqueue_struct *queue)
1752{
1753 flush_workqueue(queue);
1754}
1755
1da177e4 1756STATIC int
23ea4032 1757xfsbufd_wakeup(
15c84a47
NS
1758 int priority,
1759 gfp_t mask)
1da177e4 1760{
da7f93e9 1761 xfs_buftarg_t *btp;
a6867a68
DC
1762
1763 spin_lock(&xfs_buftarg_lock);
da7f93e9 1764 list_for_each_entry(btp, &xfs_buftarg_list, bt_list) {
ce8e922c 1765 if (test_bit(XBT_FORCE_SLEEP, &btp->bt_flags))
a6867a68 1766 continue;
c9c12971
DC
1767 if (list_empty(&btp->bt_delwrite_queue))
1768 continue;
ce8e922c 1769 set_bit(XBT_FORCE_FLUSH, &btp->bt_flags);
a6867a68
DC
1770 wake_up_process(btp->bt_task);
1771 }
1772 spin_unlock(&xfs_buftarg_lock);
1da177e4
LT
1773 return 0;
1774}
1775
585e6d88
DC
1776/*
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,
5e6a07df 1784 unsigned long age)
585e6d88
DC
1785{
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;
5e6a07df 1790 int force;
585e6d88 1791
5e6a07df 1792 force = test_and_clear_bit(XBT_FORCE_FLUSH, &target->bt_flags);
585e6d88
DC
1793 INIT_LIST_HEAD(list);
1794 spin_lock(dwlk);
1795 list_for_each_entry_safe(bp, n, dwq, b_list) {
0b1b213f 1796 trace_xfs_buf_delwri_split(bp, _RET_IP_);
585e6d88
DC
1797 ASSERT(bp->b_flags & XBF_DELWRI);
1798
1799 if (!xfs_buf_ispin(bp) && !xfs_buf_cond_lock(bp)) {
5e6a07df 1800 if (!force &&
585e6d88
DC
1801 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
089716aa
DC
1819/*
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
1da177e4 1850STATIC int
23ea4032 1851xfsbufd(
585e6d88 1852 void *data)
1da177e4 1853{
089716aa 1854 xfs_buftarg_t *target = (xfs_buftarg_t *)data;
1da177e4 1855
1da177e4
LT
1856 current->flags |= PF_MEMALLOC;
1857
978c7b2f
RW
1858 set_freezable();
1859
1da177e4 1860 do {
c9c12971
DC
1861 long age = xfs_buf_age_centisecs * msecs_to_jiffies(10);
1862 long tout = xfs_buf_timer_centisecs * msecs_to_jiffies(10);
089716aa
DC
1863 int count = 0;
1864 struct list_head tmp;
c9c12971 1865
3e1d1d28 1866 if (unlikely(freezing(current))) {
ce8e922c 1867 set_bit(XBT_FORCE_SLEEP, &target->bt_flags);
3e1d1d28 1868 refrigerator();
abd0cf7a 1869 } else {
ce8e922c 1870 clear_bit(XBT_FORCE_SLEEP, &target->bt_flags);
abd0cf7a 1871 }
1da177e4 1872
c9c12971
DC
1873 /* 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);
1da177e4 1877
c9c12971 1878 xfs_buf_delwri_split(target, &tmp, age);
089716aa 1879 list_sort(NULL, &tmp, xfs_buf_cmp);
1da177e4 1880 while (!list_empty(&tmp)) {
089716aa
DC
1881 struct xfs_buf *bp;
1882 bp = list_first_entry(&tmp, struct xfs_buf, b_list);
ce8e922c
NS
1883 list_del_init(&bp->b_list);
1884 xfs_buf_iostrategy(bp);
585e6d88 1885 count++;
1da177e4 1886 }
f07c2250
NS
1887 if (count)
1888 blk_run_address_space(target->bt_mapping);
1da177e4 1889
4df08c52 1890 } while (!kthread_should_stop());
1da177e4 1891
4df08c52 1892 return 0;
1da177e4
LT
1893}
1894
1895/*
ce8e922c
NS
1896 * 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.
1da177e4
LT
1899 */
1900int
1901xfs_flush_buftarg(
585e6d88
DC
1902 xfs_buftarg_t *target,
1903 int wait)
1da177e4 1904{
089716aa 1905 xfs_buf_t *bp;
585e6d88 1906 int pincount = 0;
089716aa
DC
1907 LIST_HEAD(tmp_list);
1908 LIST_HEAD(wait_list);
1da177e4 1909
c626d174 1910 xfs_buf_runall_queues(xfsconvertd_workqueue);
ce8e922c
NS
1911 xfs_buf_runall_queues(xfsdatad_workqueue);
1912 xfs_buf_runall_queues(xfslogd_workqueue);
1da177e4 1913
5e6a07df 1914 set_bit(XBT_FORCE_FLUSH, &target->bt_flags);
089716aa 1915 pincount = xfs_buf_delwri_split(target, &tmp_list, 0);
1da177e4
LT
1916
1917 /*
089716aa
DC
1918 * 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.
1da177e4 1921 */
089716aa
DC
1922 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);
585e6d88 1925 ASSERT(target == bp->b_target);
089716aa
DC
1926 list_del_init(&bp->b_list);
1927 if (wait) {
ce8e922c 1928 bp->b_flags &= ~XBF_ASYNC;
089716aa
DC
1929 list_add(&bp->b_list, &wait_list);
1930 }
ce8e922c 1931 xfs_buf_iostrategy(bp);
1da177e4
LT
1932 }
1933
089716aa
DC
1934 if (wait) {
1935 /* Expedite and wait for IO to complete. */
f07c2250 1936 blk_run_address_space(target->bt_mapping);
089716aa
DC
1937 while (!list_empty(&wait_list)) {
1938 bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
f07c2250 1939
089716aa
DC
1940 list_del_init(&bp->b_list);
1941 xfs_iowait(bp);
1942 xfs_buf_relse(bp);
1943 }
1da177e4
LT
1944 }
1945
1da177e4
LT
1946 return pincount;
1947}
1948
04d8b284 1949int __init
ce8e922c 1950xfs_buf_init(void)
1da177e4 1951{
8758280f
NS
1952 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
1953 KM_ZONE_HWALIGN, NULL);
ce8e922c 1954 if (!xfs_buf_zone)
0b1b213f 1955 goto out;
04d8b284 1956
b4337692 1957 xfslogd_workqueue = create_workqueue("xfslogd");
23ea4032 1958 if (!xfslogd_workqueue)
04d8b284 1959 goto out_free_buf_zone;
1da177e4 1960
b4337692 1961 xfsdatad_workqueue = create_workqueue("xfsdatad");
23ea4032
CH
1962 if (!xfsdatad_workqueue)
1963 goto out_destroy_xfslogd_workqueue;
1da177e4 1964
c626d174
DC
1965 xfsconvertd_workqueue = create_workqueue("xfsconvertd");
1966 if (!xfsconvertd_workqueue)
1967 goto out_destroy_xfsdatad_workqueue;
1968
8e1f936b 1969 register_shrinker(&xfs_buf_shake);
23ea4032 1970 return 0;
1da177e4 1971
c626d174
DC
1972 out_destroy_xfsdatad_workqueue:
1973 destroy_workqueue(xfsdatad_workqueue);
23ea4032
CH
1974 out_destroy_xfslogd_workqueue:
1975 destroy_workqueue(xfslogd_workqueue);
23ea4032 1976 out_free_buf_zone:
ce8e922c 1977 kmem_zone_destroy(xfs_buf_zone);
0b1b213f 1978 out:
8758280f 1979 return -ENOMEM;
1da177e4
LT
1980}
1981
1da177e4 1982void
ce8e922c 1983xfs_buf_terminate(void)
1da177e4 1984{
8e1f936b 1985 unregister_shrinker(&xfs_buf_shake);
c626d174 1986 destroy_workqueue(xfsconvertd_workqueue);
04d8b284
CH
1987 destroy_workqueue(xfsdatad_workqueue);
1988 destroy_workqueue(xfslogd_workqueue);
ce8e922c 1989 kmem_zone_destroy(xfs_buf_zone);
1da177e4 1990}
e6a0e9cd
TS
1991
1992#ifdef CONFIG_KDB_MODULES
1993struct list_head *
1994xfs_get_buftarg_list(void)
1995{
1996 return &xfs_buftarg_list;
1997}
1998#endif