blob: 489b7f63fd1fe9d3e2faa997a6b976d22b586a2f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scotteedb5532005-09-02 16:39:56 +10002 * Copyright (c) 2000-2005 Silicon Graphics, Inc. All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31 */
32
33/*
34 * The xfs_buf.c code provides an abstract buffer cache model on top
35 * of the Linux page cache. Cached metadata blocks for a file system
36 * are hashed to the inode for the block device. xfs_buf.c assembles
37 * buffers (xfs_buf_t) on demand to aggregate such cached pages for I/O.
38 *
39 * Written by Steve Lord, Jim Mostek, Russell Cattelan
40 * and Rajagopal Ananthanarayanan ("ananth") at SGI.
41 *
42 */
43
44#include <linux/stddef.h>
45#include <linux/errno.h>
46#include <linux/slab.h>
47#include <linux/pagemap.h>
48#include <linux/init.h>
49#include <linux/vmalloc.h>
50#include <linux/bio.h>
51#include <linux/sysctl.h>
52#include <linux/proc_fs.h>
53#include <linux/workqueue.h>
54#include <linux/percpu.h>
55#include <linux/blkdev.h>
56#include <linux/hash.h>
Christoph Hellwig4df08c52005-09-05 08:34:18 +100057#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#include "xfs_linux.h"
60
61/*
62 * File wide globals
63 */
64
Christoph Hellwig23ea4032005-06-21 15:14:01 +100065STATIC kmem_cache_t *pagebuf_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066STATIC kmem_shaker_t pagebuf_shake;
Al Viro27496a82005-10-21 03:20:48 -040067STATIC int xfsbufd_wakeup(int, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068STATIC void pagebuf_delwri_queue(xfs_buf_t *, int);
Christoph Hellwig23ea4032005-06-21 15:14:01 +100069
70STATIC struct workqueue_struct *xfslogd_workqueue;
Christoph Hellwig0829c362005-09-02 16:58:49 +100071struct workqueue_struct *xfsdatad_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73/*
74 * Pagebuf debugging
75 */
76
77#ifdef PAGEBUF_TRACE
78void
79pagebuf_trace(
80 xfs_buf_t *pb,
81 char *id,
82 void *data,
83 void *ra)
84{
85 ktrace_enter(pagebuf_trace_buf,
86 pb, id,
87 (void *)(unsigned long)pb->pb_flags,
88 (void *)(unsigned long)pb->pb_hold.counter,
89 (void *)(unsigned long)pb->pb_sema.count.counter,
90 (void *)current,
91 data, ra,
92 (void *)(unsigned long)((pb->pb_file_offset>>32) & 0xffffffff),
93 (void *)(unsigned long)(pb->pb_file_offset & 0xffffffff),
94 (void *)(unsigned long)pb->pb_buffer_length,
95 NULL, NULL, NULL, NULL, NULL);
96}
97ktrace_t *pagebuf_trace_buf;
98#define PAGEBUF_TRACE_SIZE 4096
99#define PB_TRACE(pb, id, data) \
100 pagebuf_trace(pb, id, (void *)data, (void *)__builtin_return_address(0))
101#else
102#define PB_TRACE(pb, id, data) do { } while (0)
103#endif
104
105#ifdef PAGEBUF_LOCK_TRACKING
106# define PB_SET_OWNER(pb) ((pb)->pb_last_holder = current->pid)
107# define PB_CLEAR_OWNER(pb) ((pb)->pb_last_holder = -1)
108# define PB_GET_OWNER(pb) ((pb)->pb_last_holder)
109#else
110# define PB_SET_OWNER(pb) do { } while (0)
111# define PB_CLEAR_OWNER(pb) do { } while (0)
112# define PB_GET_OWNER(pb) do { } while (0)
113#endif
114
115/*
116 * Pagebuf allocation / freeing.
117 */
118
119#define pb_to_gfp(flags) \
120 ((((flags) & PBF_READ_AHEAD) ? __GFP_NORETRY : \
121 ((flags) & PBF_DONT_BLOCK) ? GFP_NOFS : GFP_KERNEL) | __GFP_NOWARN)
122
123#define pb_to_km(flags) \
124 (((flags) & PBF_DONT_BLOCK) ? KM_NOFS : KM_SLEEP)
125
126
127#define pagebuf_allocate(flags) \
Christoph Hellwig23ea4032005-06-21 15:14:01 +1000128 kmem_zone_alloc(pagebuf_zone, pb_to_km(flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129#define pagebuf_deallocate(pb) \
Christoph Hellwig23ea4032005-06-21 15:14:01 +1000130 kmem_zone_free(pagebuf_zone, (pb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132/*
133 * Page Region interfaces.
134 *
135 * For pages in filesystems where the blocksize is smaller than the
136 * pagesize, we use the page->private field (long) to hold a bitmap
137 * of uptodate regions within the page.
138 *
139 * Each such region is "bytes per page / bits per long" bytes long.
140 *
141 * NBPPR == number-of-bytes-per-page-region
142 * BTOPR == bytes-to-page-region (rounded up)
143 * BTOPRT == bytes-to-page-region-truncated (rounded down)
144 */
145#if (BITS_PER_LONG == 32)
146#define PRSHIFT (PAGE_CACHE_SHIFT - 5) /* (32 == 1<<5) */
147#elif (BITS_PER_LONG == 64)
148#define PRSHIFT (PAGE_CACHE_SHIFT - 6) /* (64 == 1<<6) */
149#else
150#error BITS_PER_LONG must be 32 or 64
151#endif
152#define NBPPR (PAGE_CACHE_SIZE/BITS_PER_LONG)
153#define BTOPR(b) (((unsigned int)(b) + (NBPPR - 1)) >> PRSHIFT)
154#define BTOPRT(b) (((unsigned int)(b) >> PRSHIFT))
155
156STATIC unsigned long
157page_region_mask(
158 size_t offset,
159 size_t length)
160{
161 unsigned long mask;
162 int first, final;
163
164 first = BTOPR(offset);
165 final = BTOPRT(offset + length - 1);
166 first = min(first, final);
167
168 mask = ~0UL;
169 mask <<= BITS_PER_LONG - (final - first);
170 mask >>= BITS_PER_LONG - (final);
171
172 ASSERT(offset + length <= PAGE_CACHE_SIZE);
173 ASSERT((final - first) < BITS_PER_LONG && (final - first) >= 0);
174
175 return mask;
176}
177
178STATIC inline void
179set_page_region(
180 struct page *page,
181 size_t offset,
182 size_t length)
183{
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700184 set_page_private(page,
185 page_private(page) | page_region_mask(offset, length));
186 if (page_private(page) == ~0UL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 SetPageUptodate(page);
188}
189
190STATIC inline int
191test_page_region(
192 struct page *page,
193 size_t offset,
194 size_t length)
195{
196 unsigned long mask = page_region_mask(offset, length);
197
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700198 return (mask && (page_private(page) & mask) == mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
201/*
202 * Mapping of multi-page buffers into contiguous virtual space
203 */
204
205typedef struct a_list {
206 void *vm_addr;
207 struct a_list *next;
208} a_list_t;
209
210STATIC a_list_t *as_free_head;
211STATIC int as_list_len;
212STATIC DEFINE_SPINLOCK(as_lock);
213
214/*
215 * Try to batch vunmaps because they are costly.
216 */
217STATIC void
218free_address(
219 void *addr)
220{
221 a_list_t *aentry;
222
223 aentry = kmalloc(sizeof(a_list_t), GFP_ATOMIC & ~__GFP_HIGH);
224 if (likely(aentry)) {
225 spin_lock(&as_lock);
226 aentry->next = as_free_head;
227 aentry->vm_addr = addr;
228 as_free_head = aentry;
229 as_list_len++;
230 spin_unlock(&as_lock);
231 } else {
232 vunmap(addr);
233 }
234}
235
236STATIC void
237purge_addresses(void)
238{
239 a_list_t *aentry, *old;
240
241 if (as_free_head == NULL)
242 return;
243
244 spin_lock(&as_lock);
245 aentry = as_free_head;
246 as_free_head = NULL;
247 as_list_len = 0;
248 spin_unlock(&as_lock);
249
250 while ((old = aentry) != NULL) {
251 vunmap(aentry->vm_addr);
252 aentry = aentry->next;
253 kfree(old);
254 }
255}
256
257/*
258 * Internal pagebuf object manipulation
259 */
260
261STATIC void
262_pagebuf_initialize(
263 xfs_buf_t *pb,
264 xfs_buftarg_t *target,
265 loff_t range_base,
266 size_t range_length,
267 page_buf_flags_t flags)
268{
269 /*
270 * We don't want certain flags to appear in pb->pb_flags.
271 */
272 flags &= ~(PBF_LOCK|PBF_MAPPED|PBF_DONT_BLOCK|PBF_READ_AHEAD);
273
274 memset(pb, 0, sizeof(xfs_buf_t));
275 atomic_set(&pb->pb_hold, 1);
276 init_MUTEX_LOCKED(&pb->pb_iodonesema);
277 INIT_LIST_HEAD(&pb->pb_list);
278 INIT_LIST_HEAD(&pb->pb_hash_list);
279 init_MUTEX_LOCKED(&pb->pb_sema); /* held, no waiters */
280 PB_SET_OWNER(pb);
281 pb->pb_target = target;
282 pb->pb_file_offset = range_base;
283 /*
284 * Set buffer_length and count_desired to the same value initially.
285 * I/O routines should use count_desired, which will be the same in
286 * most cases but may be reset (e.g. XFS recovery).
287 */
288 pb->pb_buffer_length = pb->pb_count_desired = range_length;
289 pb->pb_flags = flags | PBF_NONE;
290 pb->pb_bn = XFS_BUF_DADDR_NULL;
291 atomic_set(&pb->pb_pin_count, 0);
292 init_waitqueue_head(&pb->pb_waiters);
293
294 XFS_STATS_INC(pb_create);
295 PB_TRACE(pb, "initialize", target);
296}
297
298/*
299 * Allocate a page array capable of holding a specified number
300 * of pages, and point the page buf at it.
301 */
302STATIC int
303_pagebuf_get_pages(
304 xfs_buf_t *pb,
305 int page_count,
306 page_buf_flags_t flags)
307{
308 /* Make sure that we have a page list */
309 if (pb->pb_pages == NULL) {
310 pb->pb_offset = page_buf_poff(pb->pb_file_offset);
311 pb->pb_page_count = page_count;
312 if (page_count <= PB_PAGES) {
313 pb->pb_pages = pb->pb_page_array;
314 } else {
315 pb->pb_pages = kmem_alloc(sizeof(struct page *) *
316 page_count, pb_to_km(flags));
317 if (pb->pb_pages == NULL)
318 return -ENOMEM;
319 }
320 memset(pb->pb_pages, 0, sizeof(struct page *) * page_count);
321 }
322 return 0;
323}
324
325/*
326 * Frees pb_pages if it was malloced.
327 */
328STATIC void
329_pagebuf_free_pages(
330 xfs_buf_t *bp)
331{
332 if (bp->pb_pages != bp->pb_page_array) {
333 kmem_free(bp->pb_pages,
334 bp->pb_page_count * sizeof(struct page *));
335 }
336}
337
338/*
339 * Releases the specified buffer.
340 *
341 * The modification state of any associated pages is left unchanged.
342 * The buffer most not be on any hash - use pagebuf_rele instead for
343 * hashed and refcounted buffers
344 */
345void
346pagebuf_free(
347 xfs_buf_t *bp)
348{
349 PB_TRACE(bp, "free", 0);
350
351 ASSERT(list_empty(&bp->pb_hash_list));
352
353 if (bp->pb_flags & _PBF_PAGE_CACHE) {
354 uint i;
355
356 if ((bp->pb_flags & PBF_MAPPED) && (bp->pb_page_count > 1))
357 free_address(bp->pb_addr - bp->pb_offset);
358
359 for (i = 0; i < bp->pb_page_count; i++)
360 page_cache_release(bp->pb_pages[i]);
361 _pagebuf_free_pages(bp);
362 } else if (bp->pb_flags & _PBF_KMEM_ALLOC) {
363 /*
364 * XXX(hch): bp->pb_count_desired might be incorrect (see
365 * pagebuf_associate_memory for details), but fortunately
366 * the Linux version of kmem_free ignores the len argument..
367 */
368 kmem_free(bp->pb_addr, bp->pb_count_desired);
369 _pagebuf_free_pages(bp);
370 }
371
372 pagebuf_deallocate(bp);
373}
374
375/*
376 * Finds all pages for buffer in question and builds it's page list.
377 */
378STATIC int
379_pagebuf_lookup_pages(
380 xfs_buf_t *bp,
381 uint flags)
382{
383 struct address_space *mapping = bp->pb_target->pbr_mapping;
384 size_t blocksize = bp->pb_target->pbr_bsize;
385 size_t size = bp->pb_count_desired;
386 size_t nbytes, offset;
Al Viro27496a82005-10-21 03:20:48 -0400387 gfp_t gfp_mask = pb_to_gfp(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 unsigned short page_count, i;
389 pgoff_t first;
390 loff_t end;
391 int error;
392
393 end = bp->pb_file_offset + bp->pb_buffer_length;
394 page_count = page_buf_btoc(end) - page_buf_btoct(bp->pb_file_offset);
395
396 error = _pagebuf_get_pages(bp, page_count, flags);
397 if (unlikely(error))
398 return error;
399 bp->pb_flags |= _PBF_PAGE_CACHE;
400
401 offset = bp->pb_offset;
402 first = bp->pb_file_offset >> PAGE_CACHE_SHIFT;
403
404 for (i = 0; i < bp->pb_page_count; i++) {
405 struct page *page;
406 uint retries = 0;
407
408 retry:
409 page = find_or_create_page(mapping, first + i, gfp_mask);
410 if (unlikely(page == NULL)) {
411 if (flags & PBF_READ_AHEAD) {
412 bp->pb_page_count = i;
413 for (i = 0; i < bp->pb_page_count; i++)
414 unlock_page(bp->pb_pages[i]);
415 return -ENOMEM;
416 }
417
418 /*
419 * This could deadlock.
420 *
421 * But until all the XFS lowlevel code is revamped to
422 * handle buffer allocation failures we can't do much.
423 */
424 if (!(++retries % 100))
425 printk(KERN_ERR
426 "XFS: possible memory allocation "
427 "deadlock in %s (mode:0x%x)\n",
428 __FUNCTION__, gfp_mask);
429
430 XFS_STATS_INC(pb_page_retries);
Christoph Hellwig23ea4032005-06-21 15:14:01 +1000431 xfsbufd_wakeup(0, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 blk_congestion_wait(WRITE, HZ/50);
433 goto retry;
434 }
435
436 XFS_STATS_INC(pb_page_found);
437
438 nbytes = min_t(size_t, size, PAGE_CACHE_SIZE - offset);
439 size -= nbytes;
440
441 if (!PageUptodate(page)) {
442 page_count--;
443 if (blocksize >= PAGE_CACHE_SIZE) {
444 if (flags & PBF_READ)
445 bp->pb_locked = 1;
446 } else if (!PagePrivate(page)) {
447 if (test_page_region(page, offset, nbytes))
448 page_count++;
449 }
450 }
451
452 bp->pb_pages[i] = page;
453 offset = 0;
454 }
455
456 if (!bp->pb_locked) {
457 for (i = 0; i < bp->pb_page_count; i++)
458 unlock_page(bp->pb_pages[i]);
459 }
460
Christoph Hellwig88741a92005-11-02 10:21:14 +1100461 bp->pb_flags &= ~PBF_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 PB_TRACE(bp, "lookup_pages", (long)page_count);
464 return error;
465}
466
467/*
468 * Map buffer into kernel address-space if nessecary.
469 */
470STATIC int
471_pagebuf_map_pages(
472 xfs_buf_t *bp,
473 uint flags)
474{
475 /* A single page buffer is always mappable */
476 if (bp->pb_page_count == 1) {
477 bp->pb_addr = page_address(bp->pb_pages[0]) + bp->pb_offset;
478 bp->pb_flags |= PBF_MAPPED;
479 } else if (flags & PBF_MAPPED) {
480 if (as_list_len > 64)
481 purge_addresses();
482 bp->pb_addr = vmap(bp->pb_pages, bp->pb_page_count,
483 VM_MAP, PAGE_KERNEL);
484 if (unlikely(bp->pb_addr == NULL))
485 return -ENOMEM;
486 bp->pb_addr += bp->pb_offset;
487 bp->pb_flags |= PBF_MAPPED;
488 }
489
490 return 0;
491}
492
493/*
494 * Finding and Reading Buffers
495 */
496
497/*
498 * _pagebuf_find
499 *
500 * Looks up, and creates if absent, a lockable buffer for
501 * a given range of an inode. The buffer is returned
502 * locked. If other overlapping buffers exist, they are
503 * released before the new buffer is created and locked,
504 * which may imply that this call will block until those buffers
505 * are unlocked. No I/O is implied by this call.
506 */
507xfs_buf_t *
508_pagebuf_find(
509 xfs_buftarg_t *btp, /* block device target */
510 loff_t ioff, /* starting offset of range */
511 size_t isize, /* length of range */
512 page_buf_flags_t flags, /* PBF_TRYLOCK */
513 xfs_buf_t *new_pb)/* newly allocated buffer */
514{
515 loff_t range_base;
516 size_t range_length;
517 xfs_bufhash_t *hash;
518 xfs_buf_t *pb, *n;
519
520 range_base = (ioff << BBSHIFT);
521 range_length = (isize << BBSHIFT);
522
523 /* Check for IOs smaller than the sector size / not sector aligned */
524 ASSERT(!(range_length < (1 << btp->pbr_sshift)));
525 ASSERT(!(range_base & (loff_t)btp->pbr_smask));
526
527 hash = &btp->bt_hash[hash_long((unsigned long)ioff, btp->bt_hashshift)];
528
529 spin_lock(&hash->bh_lock);
530
531 list_for_each_entry_safe(pb, n, &hash->bh_list, pb_hash_list) {
532 ASSERT(btp == pb->pb_target);
533 if (pb->pb_file_offset == range_base &&
534 pb->pb_buffer_length == range_length) {
535 /*
536 * If we look at something bring it to the
537 * front of the list for next time.
538 */
539 atomic_inc(&pb->pb_hold);
540 list_move(&pb->pb_hash_list, &hash->bh_list);
541 goto found;
542 }
543 }
544
545 /* No match found */
546 if (new_pb) {
547 _pagebuf_initialize(new_pb, btp, range_base,
548 range_length, flags);
549 new_pb->pb_hash = hash;
550 list_add(&new_pb->pb_hash_list, &hash->bh_list);
551 } else {
552 XFS_STATS_INC(pb_miss_locked);
553 }
554
555 spin_unlock(&hash->bh_lock);
556 return new_pb;
557
558found:
559 spin_unlock(&hash->bh_lock);
560
561 /* Attempt to get the semaphore without sleeping,
562 * if this does not work then we need to drop the
563 * spinlock and do a hard attempt on the semaphore.
564 */
565 if (down_trylock(&pb->pb_sema)) {
566 if (!(flags & PBF_TRYLOCK)) {
567 /* wait for buffer ownership */
568 PB_TRACE(pb, "get_lock", 0);
569 pagebuf_lock(pb);
570 XFS_STATS_INC(pb_get_locked_waited);
571 } else {
572 /* We asked for a trylock and failed, no need
573 * to look at file offset and length here, we
574 * know that this pagebuf at least overlaps our
575 * pagebuf and is locked, therefore our buffer
576 * either does not exist, or is this buffer
577 */
578
579 pagebuf_rele(pb);
580 XFS_STATS_INC(pb_busy_locked);
581 return (NULL);
582 }
583 } else {
584 /* trylock worked */
585 PB_SET_OWNER(pb);
586 }
587
David Chinner2f926582005-09-05 08:33:35 +1000588 if (pb->pb_flags & PBF_STALE) {
589 ASSERT((pb->pb_flags & _PBF_DELWRI_Q) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 pb->pb_flags &= PBF_MAPPED;
David Chinner2f926582005-09-05 08:33:35 +1000591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 PB_TRACE(pb, "got_lock", 0);
593 XFS_STATS_INC(pb_get_locked);
594 return (pb);
595}
596
597/*
598 * xfs_buf_get_flags assembles a buffer covering the specified range.
599 *
600 * Storage in memory for all portions of the buffer will be allocated,
601 * although backing storage may not be.
602 */
603xfs_buf_t *
604xfs_buf_get_flags( /* allocate a buffer */
605 xfs_buftarg_t *target,/* target for buffer */
606 loff_t ioff, /* starting offset of range */
607 size_t isize, /* length of range */
608 page_buf_flags_t flags) /* PBF_TRYLOCK */
609{
610 xfs_buf_t *pb, *new_pb;
611 int error = 0, i;
612
613 new_pb = pagebuf_allocate(flags);
614 if (unlikely(!new_pb))
615 return NULL;
616
617 pb = _pagebuf_find(target, ioff, isize, flags, new_pb);
618 if (pb == new_pb) {
619 error = _pagebuf_lookup_pages(pb, flags);
620 if (error)
621 goto no_buffer;
622 } else {
623 pagebuf_deallocate(new_pb);
624 if (unlikely(pb == NULL))
625 return NULL;
626 }
627
628 for (i = 0; i < pb->pb_page_count; i++)
629 mark_page_accessed(pb->pb_pages[i]);
630
631 if (!(pb->pb_flags & PBF_MAPPED)) {
632 error = _pagebuf_map_pages(pb, flags);
633 if (unlikely(error)) {
634 printk(KERN_WARNING "%s: failed to map pages\n",
635 __FUNCTION__);
636 goto no_buffer;
637 }
638 }
639
640 XFS_STATS_INC(pb_get);
641
642 /*
643 * Always fill in the block number now, the mapped cases can do
644 * their own overlay of this later.
645 */
646 pb->pb_bn = ioff;
647 pb->pb_count_desired = pb->pb_buffer_length;
648
649 PB_TRACE(pb, "get", (unsigned long)flags);
650 return pb;
651
652 no_buffer:
653 if (flags & (PBF_LOCK | PBF_TRYLOCK))
654 pagebuf_unlock(pb);
655 pagebuf_rele(pb);
656 return NULL;
657}
658
659xfs_buf_t *
660xfs_buf_read_flags(
661 xfs_buftarg_t *target,
662 loff_t ioff,
663 size_t isize,
664 page_buf_flags_t flags)
665{
666 xfs_buf_t *pb;
667
668 flags |= PBF_READ;
669
670 pb = xfs_buf_get_flags(target, ioff, isize, flags);
671 if (pb) {
Christoph Hellwig88741a92005-11-02 10:21:14 +1100672 if (!XFS_BUF_ISDONE(pb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 PB_TRACE(pb, "read", (unsigned long)flags);
674 XFS_STATS_INC(pb_get_read);
675 pagebuf_iostart(pb, flags);
676 } else if (flags & PBF_ASYNC) {
677 PB_TRACE(pb, "read_async", (unsigned long)flags);
678 /*
679 * Read ahead call which is already satisfied,
680 * drop the buffer
681 */
682 goto no_buffer;
683 } else {
684 PB_TRACE(pb, "read_done", (unsigned long)flags);
685 /* We do not want read in the flags */
686 pb->pb_flags &= ~PBF_READ;
687 }
688 }
689
690 return pb;
691
692 no_buffer:
693 if (flags & (PBF_LOCK | PBF_TRYLOCK))
694 pagebuf_unlock(pb);
695 pagebuf_rele(pb);
696 return NULL;
697}
698
699/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 * If we are not low on memory then do the readahead in a deadlock
701 * safe manner.
702 */
703void
704pagebuf_readahead(
705 xfs_buftarg_t *target,
706 loff_t ioff,
707 size_t isize,
708 page_buf_flags_t flags)
709{
710 struct backing_dev_info *bdi;
711
712 bdi = target->pbr_mapping->backing_dev_info;
713 if (bdi_read_congested(bdi))
714 return;
715
716 flags |= (PBF_TRYLOCK|PBF_ASYNC|PBF_READ_AHEAD);
717 xfs_buf_read_flags(target, ioff, isize, flags);
718}
719
720xfs_buf_t *
721pagebuf_get_empty(
722 size_t len,
723 xfs_buftarg_t *target)
724{
725 xfs_buf_t *pb;
726
727 pb = pagebuf_allocate(0);
728 if (pb)
729 _pagebuf_initialize(pb, target, 0, len, 0);
730 return pb;
731}
732
733static inline struct page *
734mem_to_page(
735 void *addr)
736{
737 if (((unsigned long)addr < VMALLOC_START) ||
738 ((unsigned long)addr >= VMALLOC_END)) {
739 return virt_to_page(addr);
740 } else {
741 return vmalloc_to_page(addr);
742 }
743}
744
745int
746pagebuf_associate_memory(
747 xfs_buf_t *pb,
748 void *mem,
749 size_t len)
750{
751 int rval;
752 int i = 0;
753 size_t ptr;
754 size_t end, end_cur;
755 off_t offset;
756 int page_count;
757
758 page_count = PAGE_CACHE_ALIGN(len) >> PAGE_CACHE_SHIFT;
759 offset = (off_t) mem - ((off_t)mem & PAGE_CACHE_MASK);
760 if (offset && (len > PAGE_CACHE_SIZE))
761 page_count++;
762
763 /* Free any previous set of page pointers */
764 if (pb->pb_pages)
765 _pagebuf_free_pages(pb);
766
767 pb->pb_pages = NULL;
768 pb->pb_addr = mem;
769
770 rval = _pagebuf_get_pages(pb, page_count, 0);
771 if (rval)
772 return rval;
773
774 pb->pb_offset = offset;
775 ptr = (size_t) mem & PAGE_CACHE_MASK;
776 end = PAGE_CACHE_ALIGN((size_t) mem + len);
777 end_cur = end;
778 /* set up first page */
779 pb->pb_pages[0] = mem_to_page(mem);
780
781 ptr += PAGE_CACHE_SIZE;
782 pb->pb_page_count = ++i;
783 while (ptr < end) {
784 pb->pb_pages[i] = mem_to_page((void *)ptr);
785 pb->pb_page_count = ++i;
786 ptr += PAGE_CACHE_SIZE;
787 }
788 pb->pb_locked = 0;
789
790 pb->pb_count_desired = pb->pb_buffer_length = len;
791 pb->pb_flags |= PBF_MAPPED;
792
793 return 0;
794}
795
796xfs_buf_t *
797pagebuf_get_no_daddr(
798 size_t len,
799 xfs_buftarg_t *target)
800{
801 size_t malloc_len = len;
802 xfs_buf_t *bp;
803 void *data;
804 int error;
805
806 bp = pagebuf_allocate(0);
807 if (unlikely(bp == NULL))
808 goto fail;
Christoph Hellwig88741a92005-11-02 10:21:14 +1100809 _pagebuf_initialize(bp, target, 0, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 try_again:
812 data = kmem_alloc(malloc_len, KM_SLEEP | KM_MAYFAIL);
813 if (unlikely(data == NULL))
814 goto fail_free_buf;
815
816 /* check whether alignment matches.. */
817 if ((__psunsigned_t)data !=
818 ((__psunsigned_t)data & ~target->pbr_smask)) {
819 /* .. else double the size and try again */
820 kmem_free(data, malloc_len);
821 malloc_len <<= 1;
822 goto try_again;
823 }
824
825 error = pagebuf_associate_memory(bp, data, len);
826 if (error)
827 goto fail_free_mem;
828 bp->pb_flags |= _PBF_KMEM_ALLOC;
829
830 pagebuf_unlock(bp);
831
832 PB_TRACE(bp, "no_daddr", data);
833 return bp;
834 fail_free_mem:
835 kmem_free(data, malloc_len);
836 fail_free_buf:
837 pagebuf_free(bp);
838 fail:
839 return NULL;
840}
841
842/*
843 * pagebuf_hold
844 *
845 * Increment reference count on buffer, to hold the buffer concurrently
846 * with another thread which may release (free) the buffer asynchronously.
847 *
848 * Must hold the buffer already to call this function.
849 */
850void
851pagebuf_hold(
852 xfs_buf_t *pb)
853{
854 atomic_inc(&pb->pb_hold);
855 PB_TRACE(pb, "hold", 0);
856}
857
858/*
859 * pagebuf_rele
860 *
861 * pagebuf_rele releases a hold on the specified buffer. If the
862 * the hold count is 1, pagebuf_rele calls pagebuf_free.
863 */
864void
865pagebuf_rele(
866 xfs_buf_t *pb)
867{
868 xfs_bufhash_t *hash = pb->pb_hash;
869
870 PB_TRACE(pb, "rele", pb->pb_relse);
871
David Chinner2f926582005-09-05 08:33:35 +1000872 /*
873 * pagebuf_lookup buffers are not hashed, not delayed write,
874 * and don't have their own release routines. Special case.
875 */
876 if (unlikely(!hash)) {
877 ASSERT(!pb->pb_relse);
878 if (atomic_dec_and_test(&pb->pb_hold))
879 xfs_buf_free(pb);
880 return;
881 }
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 if (atomic_dec_and_lock(&pb->pb_hold, &hash->bh_lock)) {
884 int do_free = 1;
885
886 if (pb->pb_relse) {
887 atomic_inc(&pb->pb_hold);
888 spin_unlock(&hash->bh_lock);
889 (*(pb->pb_relse)) (pb);
890 spin_lock(&hash->bh_lock);
891 do_free = 0;
892 }
893
David Chinner2f926582005-09-05 08:33:35 +1000894 if (pb->pb_flags & PBF_FS_MANAGED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 do_free = 0;
896 }
897
898 if (do_free) {
David Chinner2f926582005-09-05 08:33:35 +1000899 ASSERT((pb->pb_flags & (PBF_DELWRI|_PBF_DELWRI_Q)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 list_del_init(&pb->pb_hash_list);
901 spin_unlock(&hash->bh_lock);
902 pagebuf_free(pb);
903 } else {
904 spin_unlock(&hash->bh_lock);
905 }
David Chinner2f926582005-09-05 08:33:35 +1000906 } else {
907 /*
908 * Catch reference count leaks
909 */
910 ASSERT(atomic_read(&pb->pb_hold) >= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
912}
913
914
915/*
916 * Mutual exclusion on buffers. Locking model:
917 *
918 * Buffers associated with inodes for which buffer locking
919 * is not enabled are not protected by semaphores, and are
920 * assumed to be exclusively owned by the caller. There is a
921 * spinlock in the buffer, used by the caller when concurrent
922 * access is possible.
923 */
924
925/*
926 * pagebuf_cond_lock
927 *
928 * pagebuf_cond_lock locks a buffer object, if it is not already locked.
929 * Note that this in no way
930 * locks the underlying pages, so it is only useful for synchronizing
931 * concurrent use of page buffer objects, not for synchronizing independent
932 * access to the underlying pages.
933 */
934int
935pagebuf_cond_lock( /* lock buffer, if not locked */
936 /* returns -EBUSY if locked) */
937 xfs_buf_t *pb)
938{
939 int locked;
940
941 locked = down_trylock(&pb->pb_sema) == 0;
942 if (locked) {
943 PB_SET_OWNER(pb);
944 }
945 PB_TRACE(pb, "cond_lock", (long)locked);
946 return(locked ? 0 : -EBUSY);
947}
948
949#if defined(DEBUG) || defined(XFS_BLI_TRACE)
950/*
951 * pagebuf_lock_value
952 *
953 * Return lock value for a pagebuf
954 */
955int
956pagebuf_lock_value(
957 xfs_buf_t *pb)
958{
959 return(atomic_read(&pb->pb_sema.count));
960}
961#endif
962
963/*
964 * pagebuf_lock
965 *
966 * pagebuf_lock locks a buffer object. Note that this in no way
967 * locks the underlying pages, so it is only useful for synchronizing
968 * concurrent use of page buffer objects, not for synchronizing independent
969 * access to the underlying pages.
970 */
971int
972pagebuf_lock(
973 xfs_buf_t *pb)
974{
975 PB_TRACE(pb, "lock", 0);
976 if (atomic_read(&pb->pb_io_remaining))
977 blk_run_address_space(pb->pb_target->pbr_mapping);
978 down(&pb->pb_sema);
979 PB_SET_OWNER(pb);
980 PB_TRACE(pb, "locked", 0);
981 return 0;
982}
983
984/*
985 * pagebuf_unlock
986 *
987 * pagebuf_unlock releases the lock on the buffer object created by
David Chinner2f926582005-09-05 08:33:35 +1000988 * pagebuf_lock or pagebuf_cond_lock (not any pinning of underlying pages
989 * created by pagebuf_pin).
990 *
991 * If the buffer is marked delwri but is not queued, do so before we
992 * unlock the buffer as we need to set flags correctly. We also need to
993 * take a reference for the delwri queue because the unlocker is going to
994 * drop their's and they don't know we just queued it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 */
996void
997pagebuf_unlock( /* unlock buffer */
998 xfs_buf_t *pb) /* buffer to unlock */
999{
David Chinner2f926582005-09-05 08:33:35 +10001000 if ((pb->pb_flags & (PBF_DELWRI|_PBF_DELWRI_Q)) == PBF_DELWRI) {
1001 atomic_inc(&pb->pb_hold);
1002 pb->pb_flags |= PBF_ASYNC;
1003 pagebuf_delwri_queue(pb, 0);
1004 }
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 PB_CLEAR_OWNER(pb);
1007 up(&pb->pb_sema);
1008 PB_TRACE(pb, "unlock", 0);
1009}
1010
1011
1012/*
1013 * Pinning Buffer Storage in Memory
1014 */
1015
1016/*
1017 * pagebuf_pin
1018 *
1019 * pagebuf_pin locks all of the memory represented by a buffer in
1020 * memory. Multiple calls to pagebuf_pin and pagebuf_unpin, for
1021 * the same or different buffers affecting a given page, will
1022 * properly count the number of outstanding "pin" requests. The
1023 * buffer may be released after the pagebuf_pin and a different
1024 * buffer used when calling pagebuf_unpin, if desired.
1025 * pagebuf_pin should be used by the file system when it wants be
1026 * assured that no attempt will be made to force the affected
1027 * memory to disk. It does not assure that a given logical page
1028 * will not be moved to a different physical page.
1029 */
1030void
1031pagebuf_pin(
1032 xfs_buf_t *pb)
1033{
1034 atomic_inc(&pb->pb_pin_count);
1035 PB_TRACE(pb, "pin", (long)pb->pb_pin_count.counter);
1036}
1037
1038/*
1039 * pagebuf_unpin
1040 *
1041 * pagebuf_unpin reverses the locking of memory performed by
1042 * pagebuf_pin. Note that both functions affected the logical
1043 * pages associated with the buffer, not the buffer itself.
1044 */
1045void
1046pagebuf_unpin(
1047 xfs_buf_t *pb)
1048{
1049 if (atomic_dec_and_test(&pb->pb_pin_count)) {
1050 wake_up_all(&pb->pb_waiters);
1051 }
1052 PB_TRACE(pb, "unpin", (long)pb->pb_pin_count.counter);
1053}
1054
1055int
1056pagebuf_ispin(
1057 xfs_buf_t *pb)
1058{
1059 return atomic_read(&pb->pb_pin_count);
1060}
1061
1062/*
1063 * pagebuf_wait_unpin
1064 *
1065 * pagebuf_wait_unpin waits until all of the memory associated
1066 * with the buffer is not longer locked in memory. It returns
1067 * immediately if none of the affected pages are locked.
1068 */
1069static inline void
1070_pagebuf_wait_unpin(
1071 xfs_buf_t *pb)
1072{
1073 DECLARE_WAITQUEUE (wait, current);
1074
1075 if (atomic_read(&pb->pb_pin_count) == 0)
1076 return;
1077
1078 add_wait_queue(&pb->pb_waiters, &wait);
1079 for (;;) {
1080 set_current_state(TASK_UNINTERRUPTIBLE);
1081 if (atomic_read(&pb->pb_pin_count) == 0)
1082 break;
1083 if (atomic_read(&pb->pb_io_remaining))
1084 blk_run_address_space(pb->pb_target->pbr_mapping);
1085 schedule();
1086 }
1087 remove_wait_queue(&pb->pb_waiters, &wait);
1088 set_current_state(TASK_RUNNING);
1089}
1090
1091/*
1092 * Buffer Utility Routines
1093 */
1094
1095/*
1096 * pagebuf_iodone
1097 *
1098 * pagebuf_iodone marks a buffer for which I/O is in progress
1099 * done with respect to that I/O. The pb_iodone routine, if
1100 * present, will be called as a side-effect.
1101 */
1102STATIC void
1103pagebuf_iodone_work(
1104 void *v)
1105{
1106 xfs_buf_t *bp = (xfs_buf_t *)v;
1107
1108 if (bp->pb_iodone)
1109 (*(bp->pb_iodone))(bp);
1110 else if (bp->pb_flags & PBF_ASYNC)
1111 xfs_buf_relse(bp);
1112}
1113
1114void
1115pagebuf_iodone(
1116 xfs_buf_t *pb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 int schedule)
1118{
1119 pb->pb_flags &= ~(PBF_READ | PBF_WRITE);
Christoph Hellwig88741a92005-11-02 10:21:14 +11001120 if (pb->pb_error == 0)
1121 pb->pb_flags &= ~PBF_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 PB_TRACE(pb, "iodone", pb->pb_iodone);
1124
1125 if ((pb->pb_iodone) || (pb->pb_flags & PBF_ASYNC)) {
1126 if (schedule) {
1127 INIT_WORK(&pb->pb_iodone_work, pagebuf_iodone_work, pb);
Christoph Hellwig88741a92005-11-02 10:21:14 +11001128 queue_work(xfslogd_workqueue, &pb->pb_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 } else {
1130 pagebuf_iodone_work(pb);
1131 }
1132 } else {
1133 up(&pb->pb_iodonesema);
1134 }
1135}
1136
1137/*
1138 * pagebuf_ioerror
1139 *
1140 * pagebuf_ioerror sets the error code for a buffer.
1141 */
1142void
1143pagebuf_ioerror( /* mark/clear buffer error flag */
1144 xfs_buf_t *pb, /* buffer to mark */
1145 int error) /* error to store (0 if none) */
1146{
1147 ASSERT(error >= 0 && error <= 0xffff);
1148 pb->pb_error = (unsigned short)error;
1149 PB_TRACE(pb, "ioerror", (unsigned long)error);
1150}
1151
1152/*
1153 * pagebuf_iostart
1154 *
1155 * pagebuf_iostart initiates I/O on a buffer, based on the flags supplied.
1156 * If necessary, it will arrange for any disk space allocation required,
1157 * and it will break up the request if the block mappings require it.
1158 * The pb_iodone routine in the buffer supplied will only be called
1159 * when all of the subsidiary I/O requests, if any, have been completed.
1160 * pagebuf_iostart calls the pagebuf_ioinitiate routine or
1161 * pagebuf_iorequest, if the former routine is not defined, to start
1162 * the I/O on a given low-level request.
1163 */
1164int
1165pagebuf_iostart( /* start I/O on a buffer */
1166 xfs_buf_t *pb, /* buffer to start */
1167 page_buf_flags_t flags) /* PBF_LOCK, PBF_ASYNC, PBF_READ, */
1168 /* PBF_WRITE, PBF_DELWRI, */
1169 /* PBF_DONT_BLOCK */
1170{
1171 int status = 0;
1172
1173 PB_TRACE(pb, "iostart", (unsigned long)flags);
1174
1175 if (flags & PBF_DELWRI) {
1176 pb->pb_flags &= ~(PBF_READ | PBF_WRITE | PBF_ASYNC);
1177 pb->pb_flags |= flags & (PBF_DELWRI | PBF_ASYNC);
1178 pagebuf_delwri_queue(pb, 1);
1179 return status;
1180 }
1181
1182 pb->pb_flags &= ~(PBF_READ | PBF_WRITE | PBF_ASYNC | PBF_DELWRI | \
1183 PBF_READ_AHEAD | _PBF_RUN_QUEUES);
1184 pb->pb_flags |= flags & (PBF_READ | PBF_WRITE | PBF_ASYNC | \
1185 PBF_READ_AHEAD | _PBF_RUN_QUEUES);
1186
1187 BUG_ON(pb->pb_bn == XFS_BUF_DADDR_NULL);
1188
1189 /* For writes allow an alternate strategy routine to precede
1190 * the actual I/O request (which may not be issued at all in
1191 * a shutdown situation, for example).
1192 */
1193 status = (flags & PBF_WRITE) ?
1194 pagebuf_iostrategy(pb) : pagebuf_iorequest(pb);
1195
1196 /* Wait for I/O if we are not an async request.
1197 * Note: async I/O request completion will release the buffer,
1198 * and that can already be done by this point. So using the
1199 * buffer pointer from here on, after async I/O, is invalid.
1200 */
1201 if (!status && !(flags & PBF_ASYNC))
1202 status = pagebuf_iowait(pb);
1203
1204 return status;
1205}
1206
1207/*
1208 * Helper routine for pagebuf_iorequest
1209 */
1210
1211STATIC __inline__ int
1212_pagebuf_iolocked(
1213 xfs_buf_t *pb)
1214{
1215 ASSERT(pb->pb_flags & (PBF_READ|PBF_WRITE));
1216 if (pb->pb_flags & PBF_READ)
1217 return pb->pb_locked;
1218 return 0;
1219}
1220
1221STATIC __inline__ void
1222_pagebuf_iodone(
1223 xfs_buf_t *pb,
1224 int schedule)
1225{
1226 if (atomic_dec_and_test(&pb->pb_io_remaining) == 1) {
1227 pb->pb_locked = 0;
Christoph Hellwig88741a92005-11-02 10:21:14 +11001228 pagebuf_iodone(pb, schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 }
1230}
1231
1232STATIC int
1233bio_end_io_pagebuf(
1234 struct bio *bio,
1235 unsigned int bytes_done,
1236 int error)
1237{
1238 xfs_buf_t *pb = (xfs_buf_t *)bio->bi_private;
Nathan Scotteedb5532005-09-02 16:39:56 +10001239 unsigned int blocksize = pb->pb_target->pbr_bsize;
1240 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 if (bio->bi_size)
1243 return 1;
1244
1245 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
1246 pb->pb_error = EIO;
1247
Nathan Scotteedb5532005-09-02 16:39:56 +10001248 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 struct page *page = bvec->bv_page;
1250
Nathan Scotteedb5532005-09-02 16:39:56 +10001251 if (unlikely(pb->pb_error)) {
1252 if (pb->pb_flags & PBF_READ)
1253 ClearPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 SetPageError(page);
1255 } else if (blocksize == PAGE_CACHE_SIZE) {
1256 SetPageUptodate(page);
1257 } else if (!PagePrivate(page) &&
1258 (pb->pb_flags & _PBF_PAGE_CACHE)) {
1259 set_page_region(page, bvec->bv_offset, bvec->bv_len);
1260 }
1261
Nathan Scotteedb5532005-09-02 16:39:56 +10001262 if (--bvec >= bio->bi_io_vec)
1263 prefetchw(&bvec->bv_page->flags);
1264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 if (_pagebuf_iolocked(pb)) {
1266 unlock_page(page);
1267 }
Nathan Scotteedb5532005-09-02 16:39:56 +10001268 } while (bvec >= bio->bi_io_vec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 _pagebuf_iodone(pb, 1);
1271 bio_put(bio);
1272 return 0;
1273}
1274
1275STATIC void
1276_pagebuf_ioapply(
1277 xfs_buf_t *pb)
1278{
1279 int i, rw, map_i, total_nr_pages, nr_pages;
1280 struct bio *bio;
1281 int offset = pb->pb_offset;
1282 int size = pb->pb_count_desired;
1283 sector_t sector = pb->pb_bn;
1284 unsigned int blocksize = pb->pb_target->pbr_bsize;
1285 int locking = _pagebuf_iolocked(pb);
1286
1287 total_nr_pages = pb->pb_page_count;
1288 map_i = 0;
1289
1290 if (pb->pb_flags & _PBF_RUN_QUEUES) {
1291 pb->pb_flags &= ~_PBF_RUN_QUEUES;
1292 rw = (pb->pb_flags & PBF_READ) ? READ_SYNC : WRITE_SYNC;
1293 } else {
1294 rw = (pb->pb_flags & PBF_READ) ? READ : WRITE;
1295 }
1296
1297 /* Special code path for reading a sub page size pagebuf in --
1298 * we populate up the whole page, and hence the other metadata
1299 * in the same page. This optimization is only valid when the
1300 * filesystem block size and the page size are equal.
1301 */
1302 if ((pb->pb_buffer_length < PAGE_CACHE_SIZE) &&
1303 (pb->pb_flags & PBF_READ) && locking &&
1304 (blocksize == PAGE_CACHE_SIZE)) {
1305 bio = bio_alloc(GFP_NOIO, 1);
1306
1307 bio->bi_bdev = pb->pb_target->pbr_bdev;
1308 bio->bi_sector = sector - (offset >> BBSHIFT);
1309 bio->bi_end_io = bio_end_io_pagebuf;
1310 bio->bi_private = pb;
1311
1312 bio_add_page(bio, pb->pb_pages[0], PAGE_CACHE_SIZE, 0);
1313 size = 0;
1314
1315 atomic_inc(&pb->pb_io_remaining);
1316
1317 goto submit_io;
1318 }
1319
1320 /* Lock down the pages which we need to for the request */
1321 if (locking && (pb->pb_flags & PBF_WRITE) && (pb->pb_locked == 0)) {
1322 for (i = 0; size; i++) {
1323 int nbytes = PAGE_CACHE_SIZE - offset;
1324 struct page *page = pb->pb_pages[i];
1325
1326 if (nbytes > size)
1327 nbytes = size;
1328
1329 lock_page(page);
1330
1331 size -= nbytes;
1332 offset = 0;
1333 }
1334 offset = pb->pb_offset;
1335 size = pb->pb_count_desired;
1336 }
1337
1338next_chunk:
1339 atomic_inc(&pb->pb_io_remaining);
1340 nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
1341 if (nr_pages > total_nr_pages)
1342 nr_pages = total_nr_pages;
1343
1344 bio = bio_alloc(GFP_NOIO, nr_pages);
1345 bio->bi_bdev = pb->pb_target->pbr_bdev;
1346 bio->bi_sector = sector;
1347 bio->bi_end_io = bio_end_io_pagebuf;
1348 bio->bi_private = pb;
1349
1350 for (; size && nr_pages; nr_pages--, map_i++) {
1351 int nbytes = PAGE_CACHE_SIZE - offset;
1352
1353 if (nbytes > size)
1354 nbytes = size;
1355
1356 if (bio_add_page(bio, pb->pb_pages[map_i],
1357 nbytes, offset) < nbytes)
1358 break;
1359
1360 offset = 0;
1361 sector += nbytes >> BBSHIFT;
1362 size -= nbytes;
1363 total_nr_pages--;
1364 }
1365
1366submit_io:
1367 if (likely(bio->bi_size)) {
1368 submit_bio(rw, bio);
1369 if (size)
1370 goto next_chunk;
1371 } else {
1372 bio_put(bio);
1373 pagebuf_ioerror(pb, EIO);
1374 }
1375}
1376
1377/*
1378 * pagebuf_iorequest -- the core I/O request routine.
1379 */
1380int
1381pagebuf_iorequest( /* start real I/O */
1382 xfs_buf_t *pb) /* buffer to convey to device */
1383{
1384 PB_TRACE(pb, "iorequest", 0);
1385
1386 if (pb->pb_flags & PBF_DELWRI) {
1387 pagebuf_delwri_queue(pb, 1);
1388 return 0;
1389 }
1390
1391 if (pb->pb_flags & PBF_WRITE) {
1392 _pagebuf_wait_unpin(pb);
1393 }
1394
1395 pagebuf_hold(pb);
1396
1397 /* Set the count to 1 initially, this will stop an I/O
1398 * completion callout which happens before we have started
1399 * all the I/O from calling pagebuf_iodone too early.
1400 */
1401 atomic_set(&pb->pb_io_remaining, 1);
1402 _pagebuf_ioapply(pb);
1403 _pagebuf_iodone(pb, 0);
1404
1405 pagebuf_rele(pb);
1406 return 0;
1407}
1408
1409/*
1410 * pagebuf_iowait
1411 *
1412 * pagebuf_iowait waits for I/O to complete on the buffer supplied.
1413 * It returns immediately if no I/O is pending. In any case, it returns
1414 * the error code, if any, or 0 if there is no error.
1415 */
1416int
1417pagebuf_iowait(
1418 xfs_buf_t *pb)
1419{
1420 PB_TRACE(pb, "iowait", 0);
1421 if (atomic_read(&pb->pb_io_remaining))
1422 blk_run_address_space(pb->pb_target->pbr_mapping);
1423 down(&pb->pb_iodonesema);
1424 PB_TRACE(pb, "iowaited", (long)pb->pb_error);
1425 return pb->pb_error;
1426}
1427
1428caddr_t
1429pagebuf_offset(
1430 xfs_buf_t *pb,
1431 size_t offset)
1432{
1433 struct page *page;
1434
1435 offset += pb->pb_offset;
1436
1437 page = pb->pb_pages[offset >> PAGE_CACHE_SHIFT];
1438 return (caddr_t) page_address(page) + (offset & (PAGE_CACHE_SIZE - 1));
1439}
1440
1441/*
1442 * pagebuf_iomove
1443 *
1444 * Move data into or out of a buffer.
1445 */
1446void
1447pagebuf_iomove(
1448 xfs_buf_t *pb, /* buffer to process */
1449 size_t boff, /* starting buffer offset */
1450 size_t bsize, /* length to copy */
1451 caddr_t data, /* data address */
1452 page_buf_rw_t mode) /* read/write flag */
1453{
1454 size_t bend, cpoff, csize;
1455 struct page *page;
1456
1457 bend = boff + bsize;
1458 while (boff < bend) {
1459 page = pb->pb_pages[page_buf_btoct(boff + pb->pb_offset)];
1460 cpoff = page_buf_poff(boff + pb->pb_offset);
1461 csize = min_t(size_t,
1462 PAGE_CACHE_SIZE-cpoff, pb->pb_count_desired-boff);
1463
1464 ASSERT(((csize + cpoff) <= PAGE_CACHE_SIZE));
1465
1466 switch (mode) {
1467 case PBRW_ZERO:
1468 memset(page_address(page) + cpoff, 0, csize);
1469 break;
1470 case PBRW_READ:
1471 memcpy(data, page_address(page) + cpoff, csize);
1472 break;
1473 case PBRW_WRITE:
1474 memcpy(page_address(page) + cpoff, data, csize);
1475 }
1476
1477 boff += csize;
1478 data += csize;
1479 }
1480}
1481
1482/*
1483 * Handling of buftargs.
1484 */
1485
1486/*
1487 * Wait for any bufs with callbacks that have been submitted but
1488 * have not yet returned... walk the hash list for the target.
1489 */
1490void
1491xfs_wait_buftarg(
1492 xfs_buftarg_t *btp)
1493{
1494 xfs_buf_t *bp, *n;
1495 xfs_bufhash_t *hash;
1496 uint i;
1497
1498 for (i = 0; i < (1 << btp->bt_hashshift); i++) {
1499 hash = &btp->bt_hash[i];
1500again:
1501 spin_lock(&hash->bh_lock);
1502 list_for_each_entry_safe(bp, n, &hash->bh_list, pb_hash_list) {
1503 ASSERT(btp == bp->pb_target);
1504 if (!(bp->pb_flags & PBF_FS_MANAGED)) {
1505 spin_unlock(&hash->bh_lock);
David Chinner2f926582005-09-05 08:33:35 +10001506 /*
1507 * Catch superblock reference count leaks
1508 * immediately
1509 */
1510 BUG_ON(bp->pb_bn == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 delay(100);
1512 goto again;
1513 }
1514 }
1515 spin_unlock(&hash->bh_lock);
1516 }
1517}
1518
1519/*
1520 * Allocate buffer hash table for a given target.
1521 * For devices containing metadata (i.e. not the log/realtime devices)
1522 * we need to allocate a much larger hash table.
1523 */
1524STATIC void
1525xfs_alloc_bufhash(
1526 xfs_buftarg_t *btp,
1527 int external)
1528{
1529 unsigned int i;
1530
1531 btp->bt_hashshift = external ? 3 : 8; /* 8 or 256 buckets */
1532 btp->bt_hashmask = (1 << btp->bt_hashshift) - 1;
1533 btp->bt_hash = kmem_zalloc((1 << btp->bt_hashshift) *
1534 sizeof(xfs_bufhash_t), KM_SLEEP);
1535 for (i = 0; i < (1 << btp->bt_hashshift); i++) {
1536 spin_lock_init(&btp->bt_hash[i].bh_lock);
1537 INIT_LIST_HEAD(&btp->bt_hash[i].bh_list);
1538 }
1539}
1540
1541STATIC void
1542xfs_free_bufhash(
1543 xfs_buftarg_t *btp)
1544{
1545 kmem_free(btp->bt_hash,
1546 (1 << btp->bt_hashshift) * sizeof(xfs_bufhash_t));
1547 btp->bt_hash = NULL;
1548}
1549
1550void
1551xfs_free_buftarg(
1552 xfs_buftarg_t *btp,
1553 int external)
1554{
1555 xfs_flush_buftarg(btp, 1);
1556 if (external)
1557 xfs_blkdev_put(btp->pbr_bdev);
1558 xfs_free_bufhash(btp);
1559 iput(btp->pbr_mapping->host);
1560 kmem_free(btp, sizeof(*btp));
1561}
1562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563STATIC int
1564xfs_setsize_buftarg_flags(
1565 xfs_buftarg_t *btp,
1566 unsigned int blocksize,
1567 unsigned int sectorsize,
1568 int verbose)
1569{
1570 btp->pbr_bsize = blocksize;
1571 btp->pbr_sshift = ffs(sectorsize) - 1;
1572 btp->pbr_smask = sectorsize - 1;
1573
1574 if (set_blocksize(btp->pbr_bdev, sectorsize)) {
1575 printk(KERN_WARNING
1576 "XFS: Cannot set_blocksize to %u on device %s\n",
1577 sectorsize, XFS_BUFTARG_NAME(btp));
1578 return EINVAL;
1579 }
1580
1581 if (verbose &&
1582 (PAGE_CACHE_SIZE / BITS_PER_LONG) > sectorsize) {
1583 printk(KERN_WARNING
1584 "XFS: %u byte sectors in use on device %s. "
1585 "This is suboptimal; %u or greater is ideal.\n",
1586 sectorsize, XFS_BUFTARG_NAME(btp),
1587 (unsigned int)PAGE_CACHE_SIZE / BITS_PER_LONG);
1588 }
1589
1590 return 0;
1591}
1592
1593/*
1594* When allocating the initial buffer target we have not yet
1595* read in the superblock, so don't know what sized sectors
1596* are being used is at this early stage. Play safe.
1597*/
1598STATIC int
1599xfs_setsize_buftarg_early(
1600 xfs_buftarg_t *btp,
1601 struct block_device *bdev)
1602{
1603 return xfs_setsize_buftarg_flags(btp,
1604 PAGE_CACHE_SIZE, bdev_hardsect_size(bdev), 0);
1605}
1606
1607int
1608xfs_setsize_buftarg(
1609 xfs_buftarg_t *btp,
1610 unsigned int blocksize,
1611 unsigned int sectorsize)
1612{
1613 return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
1614}
1615
1616STATIC int
1617xfs_mapping_buftarg(
1618 xfs_buftarg_t *btp,
1619 struct block_device *bdev)
1620{
1621 struct backing_dev_info *bdi;
1622 struct inode *inode;
1623 struct address_space *mapping;
1624 static struct address_space_operations mapping_aops = {
1625 .sync_page = block_sync_page,
1626 };
1627
1628 inode = new_inode(bdev->bd_inode->i_sb);
1629 if (!inode) {
1630 printk(KERN_WARNING
1631 "XFS: Cannot allocate mapping inode for device %s\n",
1632 XFS_BUFTARG_NAME(btp));
1633 return ENOMEM;
1634 }
1635 inode->i_mode = S_IFBLK;
1636 inode->i_bdev = bdev;
1637 inode->i_rdev = bdev->bd_dev;
1638 bdi = blk_get_backing_dev_info(bdev);
1639 if (!bdi)
1640 bdi = &default_backing_dev_info;
1641 mapping = &inode->i_data;
1642 mapping->a_ops = &mapping_aops;
1643 mapping->backing_dev_info = bdi;
1644 mapping_set_gfp_mask(mapping, GFP_NOFS);
1645 btp->pbr_mapping = mapping;
1646 return 0;
1647}
1648
1649xfs_buftarg_t *
1650xfs_alloc_buftarg(
1651 struct block_device *bdev,
1652 int external)
1653{
1654 xfs_buftarg_t *btp;
1655
1656 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
1657
1658 btp->pbr_dev = bdev->bd_dev;
1659 btp->pbr_bdev = bdev;
1660 if (xfs_setsize_buftarg_early(btp, bdev))
1661 goto error;
1662 if (xfs_mapping_buftarg(btp, bdev))
1663 goto error;
1664 xfs_alloc_bufhash(btp, external);
1665 return btp;
1666
1667error:
1668 kmem_free(btp, sizeof(*btp));
1669 return NULL;
1670}
1671
1672
1673/*
1674 * Pagebuf delayed write buffer handling
1675 */
1676
1677STATIC LIST_HEAD(pbd_delwrite_queue);
1678STATIC DEFINE_SPINLOCK(pbd_delwrite_lock);
1679
1680STATIC void
1681pagebuf_delwri_queue(
1682 xfs_buf_t *pb,
1683 int unlock)
1684{
1685 PB_TRACE(pb, "delwri_q", (long)unlock);
David Chinner2f926582005-09-05 08:33:35 +10001686 ASSERT((pb->pb_flags & (PBF_DELWRI|PBF_ASYNC)) ==
1687 (PBF_DELWRI|PBF_ASYNC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689 spin_lock(&pbd_delwrite_lock);
1690 /* If already in the queue, dequeue and place at tail */
1691 if (!list_empty(&pb->pb_list)) {
David Chinner2f926582005-09-05 08:33:35 +10001692 ASSERT(pb->pb_flags & _PBF_DELWRI_Q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 if (unlock) {
1694 atomic_dec(&pb->pb_hold);
1695 }
1696 list_del(&pb->pb_list);
1697 }
1698
David Chinner2f926582005-09-05 08:33:35 +10001699 pb->pb_flags |= _PBF_DELWRI_Q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 list_add_tail(&pb->pb_list, &pbd_delwrite_queue);
1701 pb->pb_queuetime = jiffies;
1702 spin_unlock(&pbd_delwrite_lock);
1703
1704 if (unlock)
1705 pagebuf_unlock(pb);
1706}
1707
1708void
1709pagebuf_delwri_dequeue(
1710 xfs_buf_t *pb)
1711{
1712 int dequeued = 0;
1713
1714 spin_lock(&pbd_delwrite_lock);
1715 if ((pb->pb_flags & PBF_DELWRI) && !list_empty(&pb->pb_list)) {
David Chinner2f926582005-09-05 08:33:35 +10001716 ASSERT(pb->pb_flags & _PBF_DELWRI_Q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 list_del_init(&pb->pb_list);
1718 dequeued = 1;
1719 }
David Chinner2f926582005-09-05 08:33:35 +10001720 pb->pb_flags &= ~(PBF_DELWRI|_PBF_DELWRI_Q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 spin_unlock(&pbd_delwrite_lock);
1722
1723 if (dequeued)
1724 pagebuf_rele(pb);
1725
1726 PB_TRACE(pb, "delwri_dq", (long)dequeued);
1727}
1728
1729STATIC void
1730pagebuf_runall_queues(
1731 struct workqueue_struct *queue)
1732{
1733 flush_workqueue(queue);
1734}
1735
1736/* Defines for pagebuf daemon */
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001737STATIC struct task_struct *xfsbufd_task;
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001738STATIC int xfsbufd_force_flush;
1739STATIC int xfsbufd_force_sleep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
1741STATIC int
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001742xfsbufd_wakeup(
Al Viro27496a82005-10-21 03:20:48 -04001743 int priority,
1744 gfp_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745{
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001746 if (xfsbufd_force_sleep)
Nathan Scottabd0cf72005-05-05 13:30:13 -07001747 return 0;
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001748 xfsbufd_force_flush = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 barrier();
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001750 wake_up_process(xfsbufd_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 return 0;
1752}
1753
1754STATIC int
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001755xfsbufd(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 void *data)
1757{
1758 struct list_head tmp;
1759 unsigned long age;
1760 xfs_buftarg_t *target;
1761 xfs_buf_t *pb, *n;
1762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 current->flags |= PF_MEMALLOC;
1764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 INIT_LIST_HEAD(&tmp);
1766 do {
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001767 if (unlikely(freezing(current))) {
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001768 xfsbufd_force_sleep = 1;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001769 refrigerator();
Nathan Scottabd0cf72005-05-05 13:30:13 -07001770 } else {
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001771 xfsbufd_force_sleep = 0;
Nathan Scottabd0cf72005-05-05 13:30:13 -07001772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07001774 schedule_timeout_interruptible
1775 (xfs_buf_timer_centisecs * msecs_to_jiffies(10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07001777 age = xfs_buf_age_centisecs * msecs_to_jiffies(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 spin_lock(&pbd_delwrite_lock);
1779 list_for_each_entry_safe(pb, n, &pbd_delwrite_queue, pb_list) {
1780 PB_TRACE(pb, "walkq1", (long)pagebuf_ispin(pb));
1781 ASSERT(pb->pb_flags & PBF_DELWRI);
1782
1783 if (!pagebuf_ispin(pb) && !pagebuf_cond_lock(pb)) {
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001784 if (!xfsbufd_force_flush &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 time_before(jiffies,
1786 pb->pb_queuetime + age)) {
1787 pagebuf_unlock(pb);
1788 break;
1789 }
1790
David Chinner2f926582005-09-05 08:33:35 +10001791 pb->pb_flags &= ~(PBF_DELWRI|_PBF_DELWRI_Q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 pb->pb_flags |= PBF_WRITE;
1793 list_move(&pb->pb_list, &tmp);
1794 }
1795 }
1796 spin_unlock(&pbd_delwrite_lock);
1797
1798 while (!list_empty(&tmp)) {
1799 pb = list_entry(tmp.next, xfs_buf_t, pb_list);
1800 target = pb->pb_target;
1801
1802 list_del_init(&pb->pb_list);
1803 pagebuf_iostrategy(pb);
1804
1805 blk_run_address_space(target->pbr_mapping);
1806 }
1807
1808 if (as_list_len > 0)
1809 purge_addresses();
1810
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001811 xfsbufd_force_flush = 0;
Christoph Hellwig4df08c52005-09-05 08:34:18 +10001812 } while (!kthread_should_stop());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Christoph Hellwig4df08c52005-09-05 08:34:18 +10001814 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815}
1816
1817/*
1818 * Go through all incore buffers, and release buffers if they belong to
1819 * the given device. This is used in filesystem error handling to
1820 * preserve the consistency of its metadata.
1821 */
1822int
1823xfs_flush_buftarg(
1824 xfs_buftarg_t *target,
1825 int wait)
1826{
1827 struct list_head tmp;
1828 xfs_buf_t *pb, *n;
1829 int pincount = 0;
1830
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001831 pagebuf_runall_queues(xfsdatad_workqueue);
1832 pagebuf_runall_queues(xfslogd_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
1834 INIT_LIST_HEAD(&tmp);
1835 spin_lock(&pbd_delwrite_lock);
1836 list_for_each_entry_safe(pb, n, &pbd_delwrite_queue, pb_list) {
1837
1838 if (pb->pb_target != target)
1839 continue;
1840
David Chinner2f926582005-09-05 08:33:35 +10001841 ASSERT(pb->pb_flags & (PBF_DELWRI|_PBF_DELWRI_Q));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 PB_TRACE(pb, "walkq2", (long)pagebuf_ispin(pb));
1843 if (pagebuf_ispin(pb)) {
1844 pincount++;
1845 continue;
1846 }
1847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 list_move(&pb->pb_list, &tmp);
1849 }
1850 spin_unlock(&pbd_delwrite_lock);
1851
1852 /*
1853 * Dropped the delayed write list lock, now walk the temporary list
1854 */
1855 list_for_each_entry_safe(pb, n, &tmp, pb_list) {
David Chinner2f926582005-09-05 08:33:35 +10001856 pagebuf_lock(pb);
1857 pb->pb_flags &= ~(PBF_DELWRI|_PBF_DELWRI_Q);
1858 pb->pb_flags |= PBF_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 if (wait)
1860 pb->pb_flags &= ~PBF_ASYNC;
1861 else
1862 list_del_init(&pb->pb_list);
1863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 pagebuf_iostrategy(pb);
1865 }
1866
1867 /*
1868 * Remaining list items must be flushed before returning
1869 */
1870 while (!list_empty(&tmp)) {
1871 pb = list_entry(tmp.next, xfs_buf_t, pb_list);
1872
1873 list_del_init(&pb->pb_list);
1874 xfs_iowait(pb);
1875 xfs_buf_relse(pb);
1876 }
1877
1878 if (wait)
1879 blk_run_address_space(target->pbr_mapping);
1880
1881 return pincount;
1882}
1883
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001884int __init
1885pagebuf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001887 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001889#ifdef PAGEBUF_TRACE
1890 pagebuf_trace_buf = ktrace_alloc(PAGEBUF_TRACE_SIZE, KM_SLEEP);
1891#endif
1892
1893 pagebuf_zone = kmem_zone_init(sizeof(xfs_buf_t), "xfs_buf");
1894 if (!pagebuf_zone)
1895 goto out_free_trace_buf;
1896
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001897 xfslogd_workqueue = create_workqueue("xfslogd");
1898 if (!xfslogd_workqueue)
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001899 goto out_free_buf_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001901 xfsdatad_workqueue = create_workqueue("xfsdatad");
1902 if (!xfsdatad_workqueue)
1903 goto out_destroy_xfslogd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Christoph Hellwig4df08c52005-09-05 08:34:18 +10001905 xfsbufd_task = kthread_run(xfsbufd, NULL, "xfsbufd");
1906 if (IS_ERR(xfsbufd_task)) {
1907 error = PTR_ERR(xfsbufd_task);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001908 goto out_destroy_xfsdatad_workqueue;
Christoph Hellwig4df08c52005-09-05 08:34:18 +10001909 }
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001910
1911 pagebuf_shake = kmem_shake_register(xfsbufd_wakeup);
1912 if (!pagebuf_shake)
1913 goto out_stop_xfsbufd;
1914
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001915 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001917 out_stop_xfsbufd:
1918 kthread_stop(xfsbufd_task);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001919 out_destroy_xfsdatad_workqueue:
1920 destroy_workqueue(xfsdatad_workqueue);
1921 out_destroy_xfslogd_workqueue:
1922 destroy_workqueue(xfslogd_workqueue);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001923 out_free_buf_zone:
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001924 kmem_zone_destroy(pagebuf_zone);
1925 out_free_trace_buf:
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001926#ifdef PAGEBUF_TRACE
1927 ktrace_free(pagebuf_trace_buf);
1928#endif
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001929 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930}
1931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932void
1933pagebuf_terminate(void)
1934{
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001935 kmem_shake_deregister(pagebuf_shake);
1936 kthread_stop(xfsbufd_task);
1937 destroy_workqueue(xfsdatad_workqueue);
1938 destroy_workqueue(xfslogd_workqueue);
1939 kmem_zone_destroy(pagebuf_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940#ifdef PAGEBUF_TRACE
1941 ktrace_free(pagebuf_trace_buf);
1942#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943}