blob: 4663f7dbff1cbc9230eb5818a0a8c2fbf539992e [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 Hellwig739cafd2005-11-02 10:25:51 +1100461 if (page_count)
462 bp->pb_flags &= ~PBF_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 PB_TRACE(bp, "lookup_pages", (long)page_count);
465 return error;
466}
467
468/*
469 * Map buffer into kernel address-space if nessecary.
470 */
471STATIC int
472_pagebuf_map_pages(
473 xfs_buf_t *bp,
474 uint flags)
475{
476 /* A single page buffer is always mappable */
477 if (bp->pb_page_count == 1) {
478 bp->pb_addr = page_address(bp->pb_pages[0]) + bp->pb_offset;
479 bp->pb_flags |= PBF_MAPPED;
480 } else if (flags & PBF_MAPPED) {
481 if (as_list_len > 64)
482 purge_addresses();
483 bp->pb_addr = vmap(bp->pb_pages, bp->pb_page_count,
484 VM_MAP, PAGE_KERNEL);
485 if (unlikely(bp->pb_addr == NULL))
486 return -ENOMEM;
487 bp->pb_addr += bp->pb_offset;
488 bp->pb_flags |= PBF_MAPPED;
489 }
490
491 return 0;
492}
493
494/*
495 * Finding and Reading Buffers
496 */
497
498/*
499 * _pagebuf_find
500 *
501 * Looks up, and creates if absent, a lockable buffer for
502 * a given range of an inode. The buffer is returned
503 * locked. If other overlapping buffers exist, they are
504 * released before the new buffer is created and locked,
505 * which may imply that this call will block until those buffers
506 * are unlocked. No I/O is implied by this call.
507 */
508xfs_buf_t *
509_pagebuf_find(
510 xfs_buftarg_t *btp, /* block device target */
511 loff_t ioff, /* starting offset of range */
512 size_t isize, /* length of range */
513 page_buf_flags_t flags, /* PBF_TRYLOCK */
514 xfs_buf_t *new_pb)/* newly allocated buffer */
515{
516 loff_t range_base;
517 size_t range_length;
518 xfs_bufhash_t *hash;
519 xfs_buf_t *pb, *n;
520
521 range_base = (ioff << BBSHIFT);
522 range_length = (isize << BBSHIFT);
523
524 /* Check for IOs smaller than the sector size / not sector aligned */
525 ASSERT(!(range_length < (1 << btp->pbr_sshift)));
526 ASSERT(!(range_base & (loff_t)btp->pbr_smask));
527
528 hash = &btp->bt_hash[hash_long((unsigned long)ioff, btp->bt_hashshift)];
529
530 spin_lock(&hash->bh_lock);
531
532 list_for_each_entry_safe(pb, n, &hash->bh_list, pb_hash_list) {
533 ASSERT(btp == pb->pb_target);
534 if (pb->pb_file_offset == range_base &&
535 pb->pb_buffer_length == range_length) {
536 /*
537 * If we look at something bring it to the
538 * front of the list for next time.
539 */
540 atomic_inc(&pb->pb_hold);
541 list_move(&pb->pb_hash_list, &hash->bh_list);
542 goto found;
543 }
544 }
545
546 /* No match found */
547 if (new_pb) {
548 _pagebuf_initialize(new_pb, btp, range_base,
549 range_length, flags);
550 new_pb->pb_hash = hash;
551 list_add(&new_pb->pb_hash_list, &hash->bh_list);
552 } else {
553 XFS_STATS_INC(pb_miss_locked);
554 }
555
556 spin_unlock(&hash->bh_lock);
557 return new_pb;
558
559found:
560 spin_unlock(&hash->bh_lock);
561
562 /* Attempt to get the semaphore without sleeping,
563 * if this does not work then we need to drop the
564 * spinlock and do a hard attempt on the semaphore.
565 */
566 if (down_trylock(&pb->pb_sema)) {
567 if (!(flags & PBF_TRYLOCK)) {
568 /* wait for buffer ownership */
569 PB_TRACE(pb, "get_lock", 0);
570 pagebuf_lock(pb);
571 XFS_STATS_INC(pb_get_locked_waited);
572 } else {
573 /* We asked for a trylock and failed, no need
574 * to look at file offset and length here, we
575 * know that this pagebuf at least overlaps our
576 * pagebuf and is locked, therefore our buffer
577 * either does not exist, or is this buffer
578 */
579
580 pagebuf_rele(pb);
581 XFS_STATS_INC(pb_busy_locked);
582 return (NULL);
583 }
584 } else {
585 /* trylock worked */
586 PB_SET_OWNER(pb);
587 }
588
David Chinner2f926582005-09-05 08:33:35 +1000589 if (pb->pb_flags & PBF_STALE) {
590 ASSERT((pb->pb_flags & _PBF_DELWRI_Q) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 pb->pb_flags &= PBF_MAPPED;
David Chinner2f926582005-09-05 08:33:35 +1000592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 PB_TRACE(pb, "got_lock", 0);
594 XFS_STATS_INC(pb_get_locked);
595 return (pb);
596}
597
598/*
599 * xfs_buf_get_flags assembles a buffer covering the specified range.
600 *
601 * Storage in memory for all portions of the buffer will be allocated,
602 * although backing storage may not be.
603 */
604xfs_buf_t *
605xfs_buf_get_flags( /* allocate a buffer */
606 xfs_buftarg_t *target,/* target for buffer */
607 loff_t ioff, /* starting offset of range */
608 size_t isize, /* length of range */
609 page_buf_flags_t flags) /* PBF_TRYLOCK */
610{
611 xfs_buf_t *pb, *new_pb;
612 int error = 0, i;
613
614 new_pb = pagebuf_allocate(flags);
615 if (unlikely(!new_pb))
616 return NULL;
617
618 pb = _pagebuf_find(target, ioff, isize, flags, new_pb);
619 if (pb == new_pb) {
620 error = _pagebuf_lookup_pages(pb, flags);
621 if (error)
622 goto no_buffer;
623 } else {
624 pagebuf_deallocate(new_pb);
625 if (unlikely(pb == NULL))
626 return NULL;
627 }
628
629 for (i = 0; i < pb->pb_page_count; i++)
630 mark_page_accessed(pb->pb_pages[i]);
631
632 if (!(pb->pb_flags & PBF_MAPPED)) {
633 error = _pagebuf_map_pages(pb, flags);
634 if (unlikely(error)) {
635 printk(KERN_WARNING "%s: failed to map pages\n",
636 __FUNCTION__);
637 goto no_buffer;
638 }
639 }
640
641 XFS_STATS_INC(pb_get);
642
643 /*
644 * Always fill in the block number now, the mapped cases can do
645 * their own overlay of this later.
646 */
647 pb->pb_bn = ioff;
648 pb->pb_count_desired = pb->pb_buffer_length;
649
650 PB_TRACE(pb, "get", (unsigned long)flags);
651 return pb;
652
653 no_buffer:
654 if (flags & (PBF_LOCK | PBF_TRYLOCK))
655 pagebuf_unlock(pb);
656 pagebuf_rele(pb);
657 return NULL;
658}
659
660xfs_buf_t *
661xfs_buf_read_flags(
662 xfs_buftarg_t *target,
663 loff_t ioff,
664 size_t isize,
665 page_buf_flags_t flags)
666{
667 xfs_buf_t *pb;
668
669 flags |= PBF_READ;
670
671 pb = xfs_buf_get_flags(target, ioff, isize, flags);
672 if (pb) {
Christoph Hellwig88741a92005-11-02 10:21:14 +1100673 if (!XFS_BUF_ISDONE(pb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 PB_TRACE(pb, "read", (unsigned long)flags);
675 XFS_STATS_INC(pb_get_read);
676 pagebuf_iostart(pb, flags);
677 } else if (flags & PBF_ASYNC) {
678 PB_TRACE(pb, "read_async", (unsigned long)flags);
679 /*
680 * Read ahead call which is already satisfied,
681 * drop the buffer
682 */
683 goto no_buffer;
684 } else {
685 PB_TRACE(pb, "read_done", (unsigned long)flags);
686 /* We do not want read in the flags */
687 pb->pb_flags &= ~PBF_READ;
688 }
689 }
690
691 return pb;
692
693 no_buffer:
694 if (flags & (PBF_LOCK | PBF_TRYLOCK))
695 pagebuf_unlock(pb);
696 pagebuf_rele(pb);
697 return NULL;
698}
699
700/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 * If we are not low on memory then do the readahead in a deadlock
702 * safe manner.
703 */
704void
705pagebuf_readahead(
706 xfs_buftarg_t *target,
707 loff_t ioff,
708 size_t isize,
709 page_buf_flags_t flags)
710{
711 struct backing_dev_info *bdi;
712
713 bdi = target->pbr_mapping->backing_dev_info;
714 if (bdi_read_congested(bdi))
715 return;
716
717 flags |= (PBF_TRYLOCK|PBF_ASYNC|PBF_READ_AHEAD);
718 xfs_buf_read_flags(target, ioff, isize, flags);
719}
720
721xfs_buf_t *
722pagebuf_get_empty(
723 size_t len,
724 xfs_buftarg_t *target)
725{
726 xfs_buf_t *pb;
727
728 pb = pagebuf_allocate(0);
729 if (pb)
730 _pagebuf_initialize(pb, target, 0, len, 0);
731 return pb;
732}
733
734static inline struct page *
735mem_to_page(
736 void *addr)
737{
738 if (((unsigned long)addr < VMALLOC_START) ||
739 ((unsigned long)addr >= VMALLOC_END)) {
740 return virt_to_page(addr);
741 } else {
742 return vmalloc_to_page(addr);
743 }
744}
745
746int
747pagebuf_associate_memory(
748 xfs_buf_t *pb,
749 void *mem,
750 size_t len)
751{
752 int rval;
753 int i = 0;
754 size_t ptr;
755 size_t end, end_cur;
756 off_t offset;
757 int page_count;
758
759 page_count = PAGE_CACHE_ALIGN(len) >> PAGE_CACHE_SHIFT;
760 offset = (off_t) mem - ((off_t)mem & PAGE_CACHE_MASK);
761 if (offset && (len > PAGE_CACHE_SIZE))
762 page_count++;
763
764 /* Free any previous set of page pointers */
765 if (pb->pb_pages)
766 _pagebuf_free_pages(pb);
767
768 pb->pb_pages = NULL;
769 pb->pb_addr = mem;
770
771 rval = _pagebuf_get_pages(pb, page_count, 0);
772 if (rval)
773 return rval;
774
775 pb->pb_offset = offset;
776 ptr = (size_t) mem & PAGE_CACHE_MASK;
777 end = PAGE_CACHE_ALIGN((size_t) mem + len);
778 end_cur = end;
779 /* set up first page */
780 pb->pb_pages[0] = mem_to_page(mem);
781
782 ptr += PAGE_CACHE_SIZE;
783 pb->pb_page_count = ++i;
784 while (ptr < end) {
785 pb->pb_pages[i] = mem_to_page((void *)ptr);
786 pb->pb_page_count = ++i;
787 ptr += PAGE_CACHE_SIZE;
788 }
789 pb->pb_locked = 0;
790
791 pb->pb_count_desired = pb->pb_buffer_length = len;
792 pb->pb_flags |= PBF_MAPPED;
793
794 return 0;
795}
796
797xfs_buf_t *
798pagebuf_get_no_daddr(
799 size_t len,
800 xfs_buftarg_t *target)
801{
802 size_t malloc_len = len;
803 xfs_buf_t *bp;
804 void *data;
805 int error;
806
807 bp = pagebuf_allocate(0);
808 if (unlikely(bp == NULL))
809 goto fail;
Christoph Hellwig88741a92005-11-02 10:21:14 +1100810 _pagebuf_initialize(bp, target, 0, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 try_again:
813 data = kmem_alloc(malloc_len, KM_SLEEP | KM_MAYFAIL);
814 if (unlikely(data == NULL))
815 goto fail_free_buf;
816
817 /* check whether alignment matches.. */
818 if ((__psunsigned_t)data !=
819 ((__psunsigned_t)data & ~target->pbr_smask)) {
820 /* .. else double the size and try again */
821 kmem_free(data, malloc_len);
822 malloc_len <<= 1;
823 goto try_again;
824 }
825
826 error = pagebuf_associate_memory(bp, data, len);
827 if (error)
828 goto fail_free_mem;
829 bp->pb_flags |= _PBF_KMEM_ALLOC;
830
831 pagebuf_unlock(bp);
832
833 PB_TRACE(bp, "no_daddr", data);
834 return bp;
835 fail_free_mem:
836 kmem_free(data, malloc_len);
837 fail_free_buf:
838 pagebuf_free(bp);
839 fail:
840 return NULL;
841}
842
843/*
844 * pagebuf_hold
845 *
846 * Increment reference count on buffer, to hold the buffer concurrently
847 * with another thread which may release (free) the buffer asynchronously.
848 *
849 * Must hold the buffer already to call this function.
850 */
851void
852pagebuf_hold(
853 xfs_buf_t *pb)
854{
855 atomic_inc(&pb->pb_hold);
856 PB_TRACE(pb, "hold", 0);
857}
858
859/*
860 * pagebuf_rele
861 *
862 * pagebuf_rele releases a hold on the specified buffer. If the
863 * the hold count is 1, pagebuf_rele calls pagebuf_free.
864 */
865void
866pagebuf_rele(
867 xfs_buf_t *pb)
868{
869 xfs_bufhash_t *hash = pb->pb_hash;
870
871 PB_TRACE(pb, "rele", pb->pb_relse);
872
David Chinner2f926582005-09-05 08:33:35 +1000873 /*
874 * pagebuf_lookup buffers are not hashed, not delayed write,
875 * and don't have their own release routines. Special case.
876 */
877 if (unlikely(!hash)) {
878 ASSERT(!pb->pb_relse);
879 if (atomic_dec_and_test(&pb->pb_hold))
880 xfs_buf_free(pb);
881 return;
882 }
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (atomic_dec_and_lock(&pb->pb_hold, &hash->bh_lock)) {
885 int do_free = 1;
886
887 if (pb->pb_relse) {
888 atomic_inc(&pb->pb_hold);
889 spin_unlock(&hash->bh_lock);
890 (*(pb->pb_relse)) (pb);
891 spin_lock(&hash->bh_lock);
892 do_free = 0;
893 }
894
David Chinner2f926582005-09-05 08:33:35 +1000895 if (pb->pb_flags & PBF_FS_MANAGED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 do_free = 0;
897 }
898
899 if (do_free) {
David Chinner2f926582005-09-05 08:33:35 +1000900 ASSERT((pb->pb_flags & (PBF_DELWRI|_PBF_DELWRI_Q)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 list_del_init(&pb->pb_hash_list);
902 spin_unlock(&hash->bh_lock);
903 pagebuf_free(pb);
904 } else {
905 spin_unlock(&hash->bh_lock);
906 }
David Chinner2f926582005-09-05 08:33:35 +1000907 } else {
908 /*
909 * Catch reference count leaks
910 */
911 ASSERT(atomic_read(&pb->pb_hold) >= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 }
913}
914
915
916/*
917 * Mutual exclusion on buffers. Locking model:
918 *
919 * Buffers associated with inodes for which buffer locking
920 * is not enabled are not protected by semaphores, and are
921 * assumed to be exclusively owned by the caller. There is a
922 * spinlock in the buffer, used by the caller when concurrent
923 * access is possible.
924 */
925
926/*
927 * pagebuf_cond_lock
928 *
929 * pagebuf_cond_lock locks a buffer object, if it is not already locked.
930 * Note that this in no way
931 * locks the underlying pages, so it is only useful for synchronizing
932 * concurrent use of page buffer objects, not for synchronizing independent
933 * access to the underlying pages.
934 */
935int
936pagebuf_cond_lock( /* lock buffer, if not locked */
937 /* returns -EBUSY if locked) */
938 xfs_buf_t *pb)
939{
940 int locked;
941
942 locked = down_trylock(&pb->pb_sema) == 0;
943 if (locked) {
944 PB_SET_OWNER(pb);
945 }
946 PB_TRACE(pb, "cond_lock", (long)locked);
947 return(locked ? 0 : -EBUSY);
948}
949
950#if defined(DEBUG) || defined(XFS_BLI_TRACE)
951/*
952 * pagebuf_lock_value
953 *
954 * Return lock value for a pagebuf
955 */
956int
957pagebuf_lock_value(
958 xfs_buf_t *pb)
959{
960 return(atomic_read(&pb->pb_sema.count));
961}
962#endif
963
964/*
965 * pagebuf_lock
966 *
967 * pagebuf_lock locks a buffer object. Note that this in no way
968 * locks the underlying pages, so it is only useful for synchronizing
969 * concurrent use of page buffer objects, not for synchronizing independent
970 * access to the underlying pages.
971 */
972int
973pagebuf_lock(
974 xfs_buf_t *pb)
975{
976 PB_TRACE(pb, "lock", 0);
977 if (atomic_read(&pb->pb_io_remaining))
978 blk_run_address_space(pb->pb_target->pbr_mapping);
979 down(&pb->pb_sema);
980 PB_SET_OWNER(pb);
981 PB_TRACE(pb, "locked", 0);
982 return 0;
983}
984
985/*
986 * pagebuf_unlock
987 *
988 * pagebuf_unlock releases the lock on the buffer object created by
David Chinner2f926582005-09-05 08:33:35 +1000989 * pagebuf_lock or pagebuf_cond_lock (not any pinning of underlying pages
990 * created by pagebuf_pin).
991 *
992 * If the buffer is marked delwri but is not queued, do so before we
993 * unlock the buffer as we need to set flags correctly. We also need to
994 * take a reference for the delwri queue because the unlocker is going to
995 * drop their's and they don't know we just queued it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 */
997void
998pagebuf_unlock( /* unlock buffer */
999 xfs_buf_t *pb) /* buffer to unlock */
1000{
David Chinner2f926582005-09-05 08:33:35 +10001001 if ((pb->pb_flags & (PBF_DELWRI|_PBF_DELWRI_Q)) == PBF_DELWRI) {
1002 atomic_inc(&pb->pb_hold);
1003 pb->pb_flags |= PBF_ASYNC;
1004 pagebuf_delwri_queue(pb, 0);
1005 }
1006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 PB_CLEAR_OWNER(pb);
1008 up(&pb->pb_sema);
1009 PB_TRACE(pb, "unlock", 0);
1010}
1011
1012
1013/*
1014 * Pinning Buffer Storage in Memory
1015 */
1016
1017/*
1018 * pagebuf_pin
1019 *
1020 * pagebuf_pin locks all of the memory represented by a buffer in
1021 * memory. Multiple calls to pagebuf_pin and pagebuf_unpin, for
1022 * the same or different buffers affecting a given page, will
1023 * properly count the number of outstanding "pin" requests. The
1024 * buffer may be released after the pagebuf_pin and a different
1025 * buffer used when calling pagebuf_unpin, if desired.
1026 * pagebuf_pin should be used by the file system when it wants be
1027 * assured that no attempt will be made to force the affected
1028 * memory to disk. It does not assure that a given logical page
1029 * will not be moved to a different physical page.
1030 */
1031void
1032pagebuf_pin(
1033 xfs_buf_t *pb)
1034{
1035 atomic_inc(&pb->pb_pin_count);
1036 PB_TRACE(pb, "pin", (long)pb->pb_pin_count.counter);
1037}
1038
1039/*
1040 * pagebuf_unpin
1041 *
1042 * pagebuf_unpin reverses the locking of memory performed by
1043 * pagebuf_pin. Note that both functions affected the logical
1044 * pages associated with the buffer, not the buffer itself.
1045 */
1046void
1047pagebuf_unpin(
1048 xfs_buf_t *pb)
1049{
1050 if (atomic_dec_and_test(&pb->pb_pin_count)) {
1051 wake_up_all(&pb->pb_waiters);
1052 }
1053 PB_TRACE(pb, "unpin", (long)pb->pb_pin_count.counter);
1054}
1055
1056int
1057pagebuf_ispin(
1058 xfs_buf_t *pb)
1059{
1060 return atomic_read(&pb->pb_pin_count);
1061}
1062
1063/*
1064 * pagebuf_wait_unpin
1065 *
1066 * pagebuf_wait_unpin waits until all of the memory associated
1067 * with the buffer is not longer locked in memory. It returns
1068 * immediately if none of the affected pages are locked.
1069 */
1070static inline void
1071_pagebuf_wait_unpin(
1072 xfs_buf_t *pb)
1073{
1074 DECLARE_WAITQUEUE (wait, current);
1075
1076 if (atomic_read(&pb->pb_pin_count) == 0)
1077 return;
1078
1079 add_wait_queue(&pb->pb_waiters, &wait);
1080 for (;;) {
1081 set_current_state(TASK_UNINTERRUPTIBLE);
1082 if (atomic_read(&pb->pb_pin_count) == 0)
1083 break;
1084 if (atomic_read(&pb->pb_io_remaining))
1085 blk_run_address_space(pb->pb_target->pbr_mapping);
1086 schedule();
1087 }
1088 remove_wait_queue(&pb->pb_waiters, &wait);
1089 set_current_state(TASK_RUNNING);
1090}
1091
1092/*
1093 * Buffer Utility Routines
1094 */
1095
1096/*
1097 * pagebuf_iodone
1098 *
1099 * pagebuf_iodone marks a buffer for which I/O is in progress
1100 * done with respect to that I/O. The pb_iodone routine, if
1101 * present, will be called as a side-effect.
1102 */
1103STATIC void
1104pagebuf_iodone_work(
1105 void *v)
1106{
1107 xfs_buf_t *bp = (xfs_buf_t *)v;
1108
1109 if (bp->pb_iodone)
1110 (*(bp->pb_iodone))(bp);
1111 else if (bp->pb_flags & PBF_ASYNC)
1112 xfs_buf_relse(bp);
1113}
1114
1115void
1116pagebuf_iodone(
1117 xfs_buf_t *pb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 int schedule)
1119{
1120 pb->pb_flags &= ~(PBF_READ | PBF_WRITE);
Christoph Hellwig88741a92005-11-02 10:21:14 +11001121 if (pb->pb_error == 0)
1122 pb->pb_flags &= ~PBF_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 PB_TRACE(pb, "iodone", pb->pb_iodone);
1125
1126 if ((pb->pb_iodone) || (pb->pb_flags & PBF_ASYNC)) {
1127 if (schedule) {
1128 INIT_WORK(&pb->pb_iodone_work, pagebuf_iodone_work, pb);
Christoph Hellwig88741a92005-11-02 10:21:14 +11001129 queue_work(xfslogd_workqueue, &pb->pb_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 } else {
1131 pagebuf_iodone_work(pb);
1132 }
1133 } else {
1134 up(&pb->pb_iodonesema);
1135 }
1136}
1137
1138/*
1139 * pagebuf_ioerror
1140 *
1141 * pagebuf_ioerror sets the error code for a buffer.
1142 */
1143void
1144pagebuf_ioerror( /* mark/clear buffer error flag */
1145 xfs_buf_t *pb, /* buffer to mark */
1146 int error) /* error to store (0 if none) */
1147{
1148 ASSERT(error >= 0 && error <= 0xffff);
1149 pb->pb_error = (unsigned short)error;
1150 PB_TRACE(pb, "ioerror", (unsigned long)error);
1151}
1152
1153/*
1154 * pagebuf_iostart
1155 *
1156 * pagebuf_iostart initiates I/O on a buffer, based on the flags supplied.
1157 * If necessary, it will arrange for any disk space allocation required,
1158 * and it will break up the request if the block mappings require it.
1159 * The pb_iodone routine in the buffer supplied will only be called
1160 * when all of the subsidiary I/O requests, if any, have been completed.
1161 * pagebuf_iostart calls the pagebuf_ioinitiate routine or
1162 * pagebuf_iorequest, if the former routine is not defined, to start
1163 * the I/O on a given low-level request.
1164 */
1165int
1166pagebuf_iostart( /* start I/O on a buffer */
1167 xfs_buf_t *pb, /* buffer to start */
1168 page_buf_flags_t flags) /* PBF_LOCK, PBF_ASYNC, PBF_READ, */
1169 /* PBF_WRITE, PBF_DELWRI, */
1170 /* PBF_DONT_BLOCK */
1171{
1172 int status = 0;
1173
1174 PB_TRACE(pb, "iostart", (unsigned long)flags);
1175
1176 if (flags & PBF_DELWRI) {
1177 pb->pb_flags &= ~(PBF_READ | PBF_WRITE | PBF_ASYNC);
1178 pb->pb_flags |= flags & (PBF_DELWRI | PBF_ASYNC);
1179 pagebuf_delwri_queue(pb, 1);
1180 return status;
1181 }
1182
1183 pb->pb_flags &= ~(PBF_READ | PBF_WRITE | PBF_ASYNC | PBF_DELWRI | \
1184 PBF_READ_AHEAD | _PBF_RUN_QUEUES);
1185 pb->pb_flags |= flags & (PBF_READ | PBF_WRITE | PBF_ASYNC | \
1186 PBF_READ_AHEAD | _PBF_RUN_QUEUES);
1187
1188 BUG_ON(pb->pb_bn == XFS_BUF_DADDR_NULL);
1189
1190 /* For writes allow an alternate strategy routine to precede
1191 * the actual I/O request (which may not be issued at all in
1192 * a shutdown situation, for example).
1193 */
1194 status = (flags & PBF_WRITE) ?
1195 pagebuf_iostrategy(pb) : pagebuf_iorequest(pb);
1196
1197 /* Wait for I/O if we are not an async request.
1198 * Note: async I/O request completion will release the buffer,
1199 * and that can already be done by this point. So using the
1200 * buffer pointer from here on, after async I/O, is invalid.
1201 */
1202 if (!status && !(flags & PBF_ASYNC))
1203 status = pagebuf_iowait(pb);
1204
1205 return status;
1206}
1207
1208/*
1209 * Helper routine for pagebuf_iorequest
1210 */
1211
1212STATIC __inline__ int
1213_pagebuf_iolocked(
1214 xfs_buf_t *pb)
1215{
1216 ASSERT(pb->pb_flags & (PBF_READ|PBF_WRITE));
1217 if (pb->pb_flags & PBF_READ)
1218 return pb->pb_locked;
1219 return 0;
1220}
1221
1222STATIC __inline__ void
1223_pagebuf_iodone(
1224 xfs_buf_t *pb,
1225 int schedule)
1226{
1227 if (atomic_dec_and_test(&pb->pb_io_remaining) == 1) {
1228 pb->pb_locked = 0;
Christoph Hellwig88741a92005-11-02 10:21:14 +11001229 pagebuf_iodone(pb, schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 }
1231}
1232
1233STATIC int
1234bio_end_io_pagebuf(
1235 struct bio *bio,
1236 unsigned int bytes_done,
1237 int error)
1238{
1239 xfs_buf_t *pb = (xfs_buf_t *)bio->bi_private;
Nathan Scotteedb5532005-09-02 16:39:56 +10001240 unsigned int blocksize = pb->pb_target->pbr_bsize;
1241 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 if (bio->bi_size)
1244 return 1;
1245
1246 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
1247 pb->pb_error = EIO;
1248
Nathan Scotteedb5532005-09-02 16:39:56 +10001249 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 struct page *page = bvec->bv_page;
1251
Nathan Scotteedb5532005-09-02 16:39:56 +10001252 if (unlikely(pb->pb_error)) {
1253 if (pb->pb_flags & PBF_READ)
1254 ClearPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 SetPageError(page);
1256 } else if (blocksize == PAGE_CACHE_SIZE) {
1257 SetPageUptodate(page);
1258 } else if (!PagePrivate(page) &&
1259 (pb->pb_flags & _PBF_PAGE_CACHE)) {
1260 set_page_region(page, bvec->bv_offset, bvec->bv_len);
1261 }
1262
Nathan Scotteedb5532005-09-02 16:39:56 +10001263 if (--bvec >= bio->bi_io_vec)
1264 prefetchw(&bvec->bv_page->flags);
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 if (_pagebuf_iolocked(pb)) {
1267 unlock_page(page);
1268 }
Nathan Scotteedb5532005-09-02 16:39:56 +10001269 } while (bvec >= bio->bi_io_vec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 _pagebuf_iodone(pb, 1);
1272 bio_put(bio);
1273 return 0;
1274}
1275
1276STATIC void
1277_pagebuf_ioapply(
1278 xfs_buf_t *pb)
1279{
1280 int i, rw, map_i, total_nr_pages, nr_pages;
1281 struct bio *bio;
1282 int offset = pb->pb_offset;
1283 int size = pb->pb_count_desired;
1284 sector_t sector = pb->pb_bn;
1285 unsigned int blocksize = pb->pb_target->pbr_bsize;
1286 int locking = _pagebuf_iolocked(pb);
1287
1288 total_nr_pages = pb->pb_page_count;
1289 map_i = 0;
1290
1291 if (pb->pb_flags & _PBF_RUN_QUEUES) {
1292 pb->pb_flags &= ~_PBF_RUN_QUEUES;
1293 rw = (pb->pb_flags & PBF_READ) ? READ_SYNC : WRITE_SYNC;
1294 } else {
1295 rw = (pb->pb_flags & PBF_READ) ? READ : WRITE;
1296 }
1297
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001298 if (pb->pb_flags & PBF_ORDERED) {
1299 ASSERT(!(pb->pb_flags & PBF_READ));
1300 rw = WRITE_BARRIER;
1301 }
1302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 /* Special code path for reading a sub page size pagebuf in --
1304 * we populate up the whole page, and hence the other metadata
1305 * in the same page. This optimization is only valid when the
1306 * filesystem block size and the page size are equal.
1307 */
1308 if ((pb->pb_buffer_length < PAGE_CACHE_SIZE) &&
1309 (pb->pb_flags & PBF_READ) && locking &&
1310 (blocksize == PAGE_CACHE_SIZE)) {
1311 bio = bio_alloc(GFP_NOIO, 1);
1312
1313 bio->bi_bdev = pb->pb_target->pbr_bdev;
1314 bio->bi_sector = sector - (offset >> BBSHIFT);
1315 bio->bi_end_io = bio_end_io_pagebuf;
1316 bio->bi_private = pb;
1317
1318 bio_add_page(bio, pb->pb_pages[0], PAGE_CACHE_SIZE, 0);
1319 size = 0;
1320
1321 atomic_inc(&pb->pb_io_remaining);
1322
1323 goto submit_io;
1324 }
1325
1326 /* Lock down the pages which we need to for the request */
1327 if (locking && (pb->pb_flags & PBF_WRITE) && (pb->pb_locked == 0)) {
1328 for (i = 0; size; i++) {
1329 int nbytes = PAGE_CACHE_SIZE - offset;
1330 struct page *page = pb->pb_pages[i];
1331
1332 if (nbytes > size)
1333 nbytes = size;
1334
1335 lock_page(page);
1336
1337 size -= nbytes;
1338 offset = 0;
1339 }
1340 offset = pb->pb_offset;
1341 size = pb->pb_count_desired;
1342 }
1343
1344next_chunk:
1345 atomic_inc(&pb->pb_io_remaining);
1346 nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
1347 if (nr_pages > total_nr_pages)
1348 nr_pages = total_nr_pages;
1349
1350 bio = bio_alloc(GFP_NOIO, nr_pages);
1351 bio->bi_bdev = pb->pb_target->pbr_bdev;
1352 bio->bi_sector = sector;
1353 bio->bi_end_io = bio_end_io_pagebuf;
1354 bio->bi_private = pb;
1355
1356 for (; size && nr_pages; nr_pages--, map_i++) {
1357 int nbytes = PAGE_CACHE_SIZE - offset;
1358
1359 if (nbytes > size)
1360 nbytes = size;
1361
1362 if (bio_add_page(bio, pb->pb_pages[map_i],
1363 nbytes, offset) < nbytes)
1364 break;
1365
1366 offset = 0;
1367 sector += nbytes >> BBSHIFT;
1368 size -= nbytes;
1369 total_nr_pages--;
1370 }
1371
1372submit_io:
1373 if (likely(bio->bi_size)) {
1374 submit_bio(rw, bio);
1375 if (size)
1376 goto next_chunk;
1377 } else {
1378 bio_put(bio);
1379 pagebuf_ioerror(pb, EIO);
1380 }
1381}
1382
1383/*
1384 * pagebuf_iorequest -- the core I/O request routine.
1385 */
1386int
1387pagebuf_iorequest( /* start real I/O */
1388 xfs_buf_t *pb) /* buffer to convey to device */
1389{
1390 PB_TRACE(pb, "iorequest", 0);
1391
1392 if (pb->pb_flags & PBF_DELWRI) {
1393 pagebuf_delwri_queue(pb, 1);
1394 return 0;
1395 }
1396
1397 if (pb->pb_flags & PBF_WRITE) {
1398 _pagebuf_wait_unpin(pb);
1399 }
1400
1401 pagebuf_hold(pb);
1402
1403 /* Set the count to 1 initially, this will stop an I/O
1404 * completion callout which happens before we have started
1405 * all the I/O from calling pagebuf_iodone too early.
1406 */
1407 atomic_set(&pb->pb_io_remaining, 1);
1408 _pagebuf_ioapply(pb);
1409 _pagebuf_iodone(pb, 0);
1410
1411 pagebuf_rele(pb);
1412 return 0;
1413}
1414
1415/*
1416 * pagebuf_iowait
1417 *
1418 * pagebuf_iowait waits for I/O to complete on the buffer supplied.
1419 * It returns immediately if no I/O is pending. In any case, it returns
1420 * the error code, if any, or 0 if there is no error.
1421 */
1422int
1423pagebuf_iowait(
1424 xfs_buf_t *pb)
1425{
1426 PB_TRACE(pb, "iowait", 0);
1427 if (atomic_read(&pb->pb_io_remaining))
1428 blk_run_address_space(pb->pb_target->pbr_mapping);
1429 down(&pb->pb_iodonesema);
1430 PB_TRACE(pb, "iowaited", (long)pb->pb_error);
1431 return pb->pb_error;
1432}
1433
1434caddr_t
1435pagebuf_offset(
1436 xfs_buf_t *pb,
1437 size_t offset)
1438{
1439 struct page *page;
1440
1441 offset += pb->pb_offset;
1442
1443 page = pb->pb_pages[offset >> PAGE_CACHE_SHIFT];
1444 return (caddr_t) page_address(page) + (offset & (PAGE_CACHE_SIZE - 1));
1445}
1446
1447/*
1448 * pagebuf_iomove
1449 *
1450 * Move data into or out of a buffer.
1451 */
1452void
1453pagebuf_iomove(
1454 xfs_buf_t *pb, /* buffer to process */
1455 size_t boff, /* starting buffer offset */
1456 size_t bsize, /* length to copy */
1457 caddr_t data, /* data address */
1458 page_buf_rw_t mode) /* read/write flag */
1459{
1460 size_t bend, cpoff, csize;
1461 struct page *page;
1462
1463 bend = boff + bsize;
1464 while (boff < bend) {
1465 page = pb->pb_pages[page_buf_btoct(boff + pb->pb_offset)];
1466 cpoff = page_buf_poff(boff + pb->pb_offset);
1467 csize = min_t(size_t,
1468 PAGE_CACHE_SIZE-cpoff, pb->pb_count_desired-boff);
1469
1470 ASSERT(((csize + cpoff) <= PAGE_CACHE_SIZE));
1471
1472 switch (mode) {
1473 case PBRW_ZERO:
1474 memset(page_address(page) + cpoff, 0, csize);
1475 break;
1476 case PBRW_READ:
1477 memcpy(data, page_address(page) + cpoff, csize);
1478 break;
1479 case PBRW_WRITE:
1480 memcpy(page_address(page) + cpoff, data, csize);
1481 }
1482
1483 boff += csize;
1484 data += csize;
1485 }
1486}
1487
1488/*
1489 * Handling of buftargs.
1490 */
1491
1492/*
1493 * Wait for any bufs with callbacks that have been submitted but
1494 * have not yet returned... walk the hash list for the target.
1495 */
1496void
1497xfs_wait_buftarg(
1498 xfs_buftarg_t *btp)
1499{
1500 xfs_buf_t *bp, *n;
1501 xfs_bufhash_t *hash;
1502 uint i;
1503
1504 for (i = 0; i < (1 << btp->bt_hashshift); i++) {
1505 hash = &btp->bt_hash[i];
1506again:
1507 spin_lock(&hash->bh_lock);
1508 list_for_each_entry_safe(bp, n, &hash->bh_list, pb_hash_list) {
1509 ASSERT(btp == bp->pb_target);
1510 if (!(bp->pb_flags & PBF_FS_MANAGED)) {
1511 spin_unlock(&hash->bh_lock);
David Chinner2f926582005-09-05 08:33:35 +10001512 /*
1513 * Catch superblock reference count leaks
1514 * immediately
1515 */
1516 BUG_ON(bp->pb_bn == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 delay(100);
1518 goto again;
1519 }
1520 }
1521 spin_unlock(&hash->bh_lock);
1522 }
1523}
1524
1525/*
1526 * Allocate buffer hash table for a given target.
1527 * For devices containing metadata (i.e. not the log/realtime devices)
1528 * we need to allocate a much larger hash table.
1529 */
1530STATIC void
1531xfs_alloc_bufhash(
1532 xfs_buftarg_t *btp,
1533 int external)
1534{
1535 unsigned int i;
1536
1537 btp->bt_hashshift = external ? 3 : 8; /* 8 or 256 buckets */
1538 btp->bt_hashmask = (1 << btp->bt_hashshift) - 1;
1539 btp->bt_hash = kmem_zalloc((1 << btp->bt_hashshift) *
1540 sizeof(xfs_bufhash_t), KM_SLEEP);
1541 for (i = 0; i < (1 << btp->bt_hashshift); i++) {
1542 spin_lock_init(&btp->bt_hash[i].bh_lock);
1543 INIT_LIST_HEAD(&btp->bt_hash[i].bh_list);
1544 }
1545}
1546
1547STATIC void
1548xfs_free_bufhash(
1549 xfs_buftarg_t *btp)
1550{
1551 kmem_free(btp->bt_hash,
1552 (1 << btp->bt_hashshift) * sizeof(xfs_bufhash_t));
1553 btp->bt_hash = NULL;
1554}
1555
1556void
1557xfs_free_buftarg(
1558 xfs_buftarg_t *btp,
1559 int external)
1560{
1561 xfs_flush_buftarg(btp, 1);
1562 if (external)
1563 xfs_blkdev_put(btp->pbr_bdev);
1564 xfs_free_bufhash(btp);
1565 iput(btp->pbr_mapping->host);
1566 kmem_free(btp, sizeof(*btp));
1567}
1568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569STATIC int
1570xfs_setsize_buftarg_flags(
1571 xfs_buftarg_t *btp,
1572 unsigned int blocksize,
1573 unsigned int sectorsize,
1574 int verbose)
1575{
1576 btp->pbr_bsize = blocksize;
1577 btp->pbr_sshift = ffs(sectorsize) - 1;
1578 btp->pbr_smask = sectorsize - 1;
1579
1580 if (set_blocksize(btp->pbr_bdev, sectorsize)) {
1581 printk(KERN_WARNING
1582 "XFS: Cannot set_blocksize to %u on device %s\n",
1583 sectorsize, XFS_BUFTARG_NAME(btp));
1584 return EINVAL;
1585 }
1586
1587 if (verbose &&
1588 (PAGE_CACHE_SIZE / BITS_PER_LONG) > sectorsize) {
1589 printk(KERN_WARNING
1590 "XFS: %u byte sectors in use on device %s. "
1591 "This is suboptimal; %u or greater is ideal.\n",
1592 sectorsize, XFS_BUFTARG_NAME(btp),
1593 (unsigned int)PAGE_CACHE_SIZE / BITS_PER_LONG);
1594 }
1595
1596 return 0;
1597}
1598
1599/*
1600* When allocating the initial buffer target we have not yet
1601* read in the superblock, so don't know what sized sectors
1602* are being used is at this early stage. Play safe.
1603*/
1604STATIC int
1605xfs_setsize_buftarg_early(
1606 xfs_buftarg_t *btp,
1607 struct block_device *bdev)
1608{
1609 return xfs_setsize_buftarg_flags(btp,
1610 PAGE_CACHE_SIZE, bdev_hardsect_size(bdev), 0);
1611}
1612
1613int
1614xfs_setsize_buftarg(
1615 xfs_buftarg_t *btp,
1616 unsigned int blocksize,
1617 unsigned int sectorsize)
1618{
1619 return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
1620}
1621
1622STATIC int
1623xfs_mapping_buftarg(
1624 xfs_buftarg_t *btp,
1625 struct block_device *bdev)
1626{
1627 struct backing_dev_info *bdi;
1628 struct inode *inode;
1629 struct address_space *mapping;
1630 static struct address_space_operations mapping_aops = {
1631 .sync_page = block_sync_page,
1632 };
1633
1634 inode = new_inode(bdev->bd_inode->i_sb);
1635 if (!inode) {
1636 printk(KERN_WARNING
1637 "XFS: Cannot allocate mapping inode for device %s\n",
1638 XFS_BUFTARG_NAME(btp));
1639 return ENOMEM;
1640 }
1641 inode->i_mode = S_IFBLK;
1642 inode->i_bdev = bdev;
1643 inode->i_rdev = bdev->bd_dev;
1644 bdi = blk_get_backing_dev_info(bdev);
1645 if (!bdi)
1646 bdi = &default_backing_dev_info;
1647 mapping = &inode->i_data;
1648 mapping->a_ops = &mapping_aops;
1649 mapping->backing_dev_info = bdi;
1650 mapping_set_gfp_mask(mapping, GFP_NOFS);
1651 btp->pbr_mapping = mapping;
1652 return 0;
1653}
1654
1655xfs_buftarg_t *
1656xfs_alloc_buftarg(
1657 struct block_device *bdev,
1658 int external)
1659{
1660 xfs_buftarg_t *btp;
1661
1662 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
1663
1664 btp->pbr_dev = bdev->bd_dev;
1665 btp->pbr_bdev = bdev;
1666 if (xfs_setsize_buftarg_early(btp, bdev))
1667 goto error;
1668 if (xfs_mapping_buftarg(btp, bdev))
1669 goto error;
1670 xfs_alloc_bufhash(btp, external);
1671 return btp;
1672
1673error:
1674 kmem_free(btp, sizeof(*btp));
1675 return NULL;
1676}
1677
1678
1679/*
1680 * Pagebuf delayed write buffer handling
1681 */
1682
1683STATIC LIST_HEAD(pbd_delwrite_queue);
1684STATIC DEFINE_SPINLOCK(pbd_delwrite_lock);
1685
1686STATIC void
1687pagebuf_delwri_queue(
1688 xfs_buf_t *pb,
1689 int unlock)
1690{
1691 PB_TRACE(pb, "delwri_q", (long)unlock);
David Chinner2f926582005-09-05 08:33:35 +10001692 ASSERT((pb->pb_flags & (PBF_DELWRI|PBF_ASYNC)) ==
1693 (PBF_DELWRI|PBF_ASYNC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
1695 spin_lock(&pbd_delwrite_lock);
1696 /* If already in the queue, dequeue and place at tail */
1697 if (!list_empty(&pb->pb_list)) {
David Chinner2f926582005-09-05 08:33:35 +10001698 ASSERT(pb->pb_flags & _PBF_DELWRI_Q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 if (unlock) {
1700 atomic_dec(&pb->pb_hold);
1701 }
1702 list_del(&pb->pb_list);
1703 }
1704
David Chinner2f926582005-09-05 08:33:35 +10001705 pb->pb_flags |= _PBF_DELWRI_Q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 list_add_tail(&pb->pb_list, &pbd_delwrite_queue);
1707 pb->pb_queuetime = jiffies;
1708 spin_unlock(&pbd_delwrite_lock);
1709
1710 if (unlock)
1711 pagebuf_unlock(pb);
1712}
1713
1714void
1715pagebuf_delwri_dequeue(
1716 xfs_buf_t *pb)
1717{
1718 int dequeued = 0;
1719
1720 spin_lock(&pbd_delwrite_lock);
1721 if ((pb->pb_flags & PBF_DELWRI) && !list_empty(&pb->pb_list)) {
David Chinner2f926582005-09-05 08:33:35 +10001722 ASSERT(pb->pb_flags & _PBF_DELWRI_Q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 list_del_init(&pb->pb_list);
1724 dequeued = 1;
1725 }
David Chinner2f926582005-09-05 08:33:35 +10001726 pb->pb_flags &= ~(PBF_DELWRI|_PBF_DELWRI_Q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 spin_unlock(&pbd_delwrite_lock);
1728
1729 if (dequeued)
1730 pagebuf_rele(pb);
1731
1732 PB_TRACE(pb, "delwri_dq", (long)dequeued);
1733}
1734
1735STATIC void
1736pagebuf_runall_queues(
1737 struct workqueue_struct *queue)
1738{
1739 flush_workqueue(queue);
1740}
1741
1742/* Defines for pagebuf daemon */
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001743STATIC struct task_struct *xfsbufd_task;
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001744STATIC int xfsbufd_force_flush;
1745STATIC int xfsbufd_force_sleep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
1747STATIC int
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001748xfsbufd_wakeup(
Al Viro27496a82005-10-21 03:20:48 -04001749 int priority,
1750 gfp_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751{
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001752 if (xfsbufd_force_sleep)
Nathan Scottabd0cf72005-05-05 13:30:13 -07001753 return 0;
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001754 xfsbufd_force_flush = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 barrier();
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001756 wake_up_process(xfsbufd_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 return 0;
1758}
1759
1760STATIC int
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001761xfsbufd(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 void *data)
1763{
1764 struct list_head tmp;
1765 unsigned long age;
1766 xfs_buftarg_t *target;
1767 xfs_buf_t *pb, *n;
1768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 current->flags |= PF_MEMALLOC;
1770
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 INIT_LIST_HEAD(&tmp);
1772 do {
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001773 if (unlikely(freezing(current))) {
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001774 xfsbufd_force_sleep = 1;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001775 refrigerator();
Nathan Scottabd0cf72005-05-05 13:30:13 -07001776 } else {
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001777 xfsbufd_force_sleep = 0;
Nathan Scottabd0cf72005-05-05 13:30:13 -07001778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07001780 schedule_timeout_interruptible
1781 (xfs_buf_timer_centisecs * msecs_to_jiffies(10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07001783 age = xfs_buf_age_centisecs * msecs_to_jiffies(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 spin_lock(&pbd_delwrite_lock);
1785 list_for_each_entry_safe(pb, n, &pbd_delwrite_queue, pb_list) {
1786 PB_TRACE(pb, "walkq1", (long)pagebuf_ispin(pb));
1787 ASSERT(pb->pb_flags & PBF_DELWRI);
1788
1789 if (!pagebuf_ispin(pb) && !pagebuf_cond_lock(pb)) {
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001790 if (!xfsbufd_force_flush &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 time_before(jiffies,
1792 pb->pb_queuetime + age)) {
1793 pagebuf_unlock(pb);
1794 break;
1795 }
1796
David Chinner2f926582005-09-05 08:33:35 +10001797 pb->pb_flags &= ~(PBF_DELWRI|_PBF_DELWRI_Q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 pb->pb_flags |= PBF_WRITE;
1799 list_move(&pb->pb_list, &tmp);
1800 }
1801 }
1802 spin_unlock(&pbd_delwrite_lock);
1803
1804 while (!list_empty(&tmp)) {
1805 pb = list_entry(tmp.next, xfs_buf_t, pb_list);
1806 target = pb->pb_target;
1807
1808 list_del_init(&pb->pb_list);
1809 pagebuf_iostrategy(pb);
1810
1811 blk_run_address_space(target->pbr_mapping);
1812 }
1813
1814 if (as_list_len > 0)
1815 purge_addresses();
1816
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001817 xfsbufd_force_flush = 0;
Christoph Hellwig4df08c52005-09-05 08:34:18 +10001818 } while (!kthread_should_stop());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
Christoph Hellwig4df08c52005-09-05 08:34:18 +10001820 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821}
1822
1823/*
1824 * Go through all incore buffers, and release buffers if they belong to
1825 * the given device. This is used in filesystem error handling to
1826 * preserve the consistency of its metadata.
1827 */
1828int
1829xfs_flush_buftarg(
1830 xfs_buftarg_t *target,
1831 int wait)
1832{
1833 struct list_head tmp;
1834 xfs_buf_t *pb, *n;
1835 int pincount = 0;
1836
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001837 pagebuf_runall_queues(xfsdatad_workqueue);
1838 pagebuf_runall_queues(xfslogd_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
1840 INIT_LIST_HEAD(&tmp);
1841 spin_lock(&pbd_delwrite_lock);
1842 list_for_each_entry_safe(pb, n, &pbd_delwrite_queue, pb_list) {
1843
1844 if (pb->pb_target != target)
1845 continue;
1846
David Chinner2f926582005-09-05 08:33:35 +10001847 ASSERT(pb->pb_flags & (PBF_DELWRI|_PBF_DELWRI_Q));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 PB_TRACE(pb, "walkq2", (long)pagebuf_ispin(pb));
1849 if (pagebuf_ispin(pb)) {
1850 pincount++;
1851 continue;
1852 }
1853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 list_move(&pb->pb_list, &tmp);
1855 }
1856 spin_unlock(&pbd_delwrite_lock);
1857
1858 /*
1859 * Dropped the delayed write list lock, now walk the temporary list
1860 */
1861 list_for_each_entry_safe(pb, n, &tmp, pb_list) {
David Chinner2f926582005-09-05 08:33:35 +10001862 pagebuf_lock(pb);
1863 pb->pb_flags &= ~(PBF_DELWRI|_PBF_DELWRI_Q);
1864 pb->pb_flags |= PBF_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 if (wait)
1866 pb->pb_flags &= ~PBF_ASYNC;
1867 else
1868 list_del_init(&pb->pb_list);
1869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 pagebuf_iostrategy(pb);
1871 }
1872
1873 /*
1874 * Remaining list items must be flushed before returning
1875 */
1876 while (!list_empty(&tmp)) {
1877 pb = list_entry(tmp.next, xfs_buf_t, pb_list);
1878
1879 list_del_init(&pb->pb_list);
1880 xfs_iowait(pb);
1881 xfs_buf_relse(pb);
1882 }
1883
1884 if (wait)
1885 blk_run_address_space(target->pbr_mapping);
1886
1887 return pincount;
1888}
1889
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001890int __init
1891pagebuf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001893 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001895#ifdef PAGEBUF_TRACE
1896 pagebuf_trace_buf = ktrace_alloc(PAGEBUF_TRACE_SIZE, KM_SLEEP);
1897#endif
1898
1899 pagebuf_zone = kmem_zone_init(sizeof(xfs_buf_t), "xfs_buf");
1900 if (!pagebuf_zone)
1901 goto out_free_trace_buf;
1902
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001903 xfslogd_workqueue = create_workqueue("xfslogd");
1904 if (!xfslogd_workqueue)
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001905 goto out_free_buf_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001907 xfsdatad_workqueue = create_workqueue("xfsdatad");
1908 if (!xfsdatad_workqueue)
1909 goto out_destroy_xfslogd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Christoph Hellwig4df08c52005-09-05 08:34:18 +10001911 xfsbufd_task = kthread_run(xfsbufd, NULL, "xfsbufd");
1912 if (IS_ERR(xfsbufd_task)) {
1913 error = PTR_ERR(xfsbufd_task);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001914 goto out_destroy_xfsdatad_workqueue;
Christoph Hellwig4df08c52005-09-05 08:34:18 +10001915 }
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001916
1917 pagebuf_shake = kmem_shake_register(xfsbufd_wakeup);
1918 if (!pagebuf_shake)
1919 goto out_stop_xfsbufd;
1920
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001921 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001923 out_stop_xfsbufd:
1924 kthread_stop(xfsbufd_task);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001925 out_destroy_xfsdatad_workqueue:
1926 destroy_workqueue(xfsdatad_workqueue);
1927 out_destroy_xfslogd_workqueue:
1928 destroy_workqueue(xfslogd_workqueue);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001929 out_free_buf_zone:
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001930 kmem_zone_destroy(pagebuf_zone);
1931 out_free_trace_buf:
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001932#ifdef PAGEBUF_TRACE
1933 ktrace_free(pagebuf_trace_buf);
1934#endif
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001935 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936}
1937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938void
1939pagebuf_terminate(void)
1940{
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001941 kmem_shake_deregister(pagebuf_shake);
1942 kthread_stop(xfsbufd_task);
1943 destroy_workqueue(xfsdatad_workqueue);
1944 destroy_workqueue(xfslogd_workqueue);
1945 kmem_zone_destroy(pagebuf_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946#ifdef PAGEBUF_TRACE
1947 ktrace_free(pagebuf_trace_buf);
1948#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949}