This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / fs / xfs / linux / xfs_buf.c
1 /*
2  * Copyright (c) 2000-2004 Silicon Graphics, Inc.  All Rights Reserved.
3  *
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/suspend.h>
55 #include <linux/percpu.h>
56
57 #include "xfs_linux.h"
58
59 #ifndef GFP_READAHEAD
60 #define GFP_READAHEAD   (__GFP_NOWARN|__GFP_NORETRY)
61 #endif
62
63 /*
64  * File wide globals
65  */
66
67 STATIC kmem_cache_t *pagebuf_cache;
68 STATIC void pagebuf_daemon_wakeup(void);
69 STATIC void pagebuf_delwri_queue(xfs_buf_t *, int);
70 STATIC struct workqueue_struct *pagebuf_logio_workqueue;
71 STATIC struct workqueue_struct *pagebuf_dataio_workqueue;
72
73 /*
74  * Pagebuf debugging
75  */
76
77 #ifdef PAGEBUF_TRACE
78 void
79 pagebuf_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 }
97 ktrace_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_READAHEAD : \
121          ((flags) & PBF_DONT_BLOCK) ? GFP_NOFS : GFP_KERNEL)
122
123 #define pb_to_km(flags) \
124          (((flags) & PBF_DONT_BLOCK) ? KM_NOFS : KM_SLEEP)
125
126
127 #define pagebuf_allocate(flags) \
128         kmem_zone_alloc(pagebuf_cache, pb_to_km(flags))
129 #define pagebuf_deallocate(pb) \
130         kmem_zone_free(pagebuf_cache, (pb));
131
132 /*
133  * Pagebuf hashing
134  */
135
136 #define NBITS   8
137 #define NHASH   (1<<NBITS)
138
139 typedef struct {
140         struct list_head        pb_hash;
141         spinlock_t              pb_hash_lock;
142 } pb_hash_t;
143
144 STATIC pb_hash_t        pbhash[NHASH];
145 #define pb_hash(pb)     &pbhash[pb->pb_hash_index]
146
147 STATIC int
148 _bhash(
149         struct block_device *bdev,
150         loff_t          base)
151 {
152         int             bit, hval;
153
154         base >>= 9;
155         base ^= (unsigned long)bdev / L1_CACHE_BYTES;
156         for (bit = hval = 0; base && bit < sizeof(base) * 8; bit += NBITS) {
157                 hval ^= (int)base & (NHASH-1);
158                 base >>= NBITS;
159         }
160         return hval;
161 }
162
163 /*
164  * Mapping of multi-page buffers into contiguous virtual space
165  */
166
167 typedef struct a_list {
168         void            *vm_addr;
169         struct a_list   *next;
170 } a_list_t;
171
172 STATIC a_list_t         *as_free_head;
173 STATIC int              as_list_len;
174 STATIC spinlock_t       as_lock = SPIN_LOCK_UNLOCKED;
175
176 /*
177  * Try to batch vunmaps because they are costly.
178  */
179 STATIC void
180 free_address(
181         void            *addr)
182 {
183         a_list_t        *aentry;
184
185         aentry = kmalloc(sizeof(a_list_t), GFP_ATOMIC);
186         if (aentry) {
187                 spin_lock(&as_lock);
188                 aentry->next = as_free_head;
189                 aentry->vm_addr = addr;
190                 as_free_head = aentry;
191                 as_list_len++;
192                 spin_unlock(&as_lock);
193         } else {
194                 vunmap(addr);
195         }
196 }
197
198 STATIC void
199 purge_addresses(void)
200 {
201         a_list_t        *aentry, *old;
202
203         if (as_free_head == NULL)
204                 return;
205
206         spin_lock(&as_lock);
207         aentry = as_free_head;
208         as_free_head = NULL;
209         as_list_len = 0;
210         spin_unlock(&as_lock);
211
212         while ((old = aentry) != NULL) {
213                 vunmap(aentry->vm_addr);
214                 aentry = aentry->next;
215                 kfree(old);
216         }
217 }
218
219 /*
220  *      Internal pagebuf object manipulation
221  */
222
223 STATIC void
224 _pagebuf_initialize(
225         xfs_buf_t               *pb,
226         xfs_buftarg_t           *target,
227         loff_t                  range_base,
228         size_t                  range_length,
229         page_buf_flags_t        flags)
230 {
231         /*
232          * We don't want certain flags to appear in pb->pb_flags.
233          */
234         flags &= ~(PBF_LOCK|PBF_MAPPED|PBF_DONT_BLOCK|PBF_READ_AHEAD);
235
236         memset(pb, 0, sizeof(xfs_buf_t));
237         atomic_set(&pb->pb_hold, 1);
238         init_MUTEX_LOCKED(&pb->pb_iodonesema);
239         INIT_LIST_HEAD(&pb->pb_list);
240         INIT_LIST_HEAD(&pb->pb_hash_list);
241         init_MUTEX_LOCKED(&pb->pb_sema); /* held, no waiters */
242         PB_SET_OWNER(pb);
243         pb->pb_target = target;
244         pb->pb_file_offset = range_base;
245         /*
246          * Set buffer_length and count_desired to the same value initially.
247          * I/O routines should use count_desired, which will be the same in
248          * most cases but may be reset (e.g. XFS recovery).
249          */
250         pb->pb_buffer_length = pb->pb_count_desired = range_length;
251         pb->pb_flags = flags | PBF_NONE;
252         pb->pb_bn = XFS_BUF_DADDR_NULL;
253         atomic_set(&pb->pb_pin_count, 0);
254         init_waitqueue_head(&pb->pb_waiters);
255
256         XFS_STATS_INC(pb_create);
257         PB_TRACE(pb, "initialize", target);
258 }
259
260 /*
261  * Allocate a page array capable of holding a specified number
262  * of pages, and point the page buf at it.
263  */
264 STATIC int
265 _pagebuf_get_pages(
266         xfs_buf_t               *pb,
267         int                     page_count,
268         page_buf_flags_t        flags)
269 {
270         /* Make sure that we have a page list */
271         if (pb->pb_pages == NULL) {
272                 pb->pb_offset = page_buf_poff(pb->pb_file_offset);
273                 pb->pb_page_count = page_count;
274                 if (page_count <= PB_PAGES) {
275                         pb->pb_pages = pb->pb_page_array;
276                 } else {
277                         pb->pb_pages = kmem_alloc(sizeof(struct page *) *
278                                         page_count, pb_to_km(flags));
279                         if (pb->pb_pages == NULL)
280                                 return -ENOMEM;
281                 }
282                 memset(pb->pb_pages, 0, sizeof(struct page *) * page_count);
283         }
284         return 0;
285 }
286
287 /*
288  *      Frees pb_pages if it was malloced.
289  */
290 STATIC void
291 _pagebuf_free_pages(
292         xfs_buf_t       *bp)
293 {
294         if (bp->pb_pages != bp->pb_page_array) {
295                 kmem_free(bp->pb_pages,
296                           bp->pb_page_count * sizeof(struct page *));
297         }
298 }
299
300 /*
301  *      Releases the specified buffer.
302  *
303  *      The modification state of any associated pages is left unchanged.
304  *      The buffer most not be on any hash - use pagebuf_rele instead for
305  *      hashed and refcounted buffers
306  */
307 void
308 pagebuf_free(
309         xfs_buf_t               *bp)
310 {
311         PB_TRACE(bp, "free", 0);
312
313         ASSERT(list_empty(&bp->pb_hash_list));
314
315         if (bp->pb_flags & _PBF_PAGE_CACHE) {
316                 uint            i;
317
318                 if ((bp->pb_flags & PBF_MAPPED) && (bp->pb_page_count > 1))
319                         free_address(bp->pb_addr - bp->pb_offset);
320
321                 for (i = 0; i < bp->pb_page_count; i++)
322                         page_cache_release(bp->pb_pages[i]);
323                 _pagebuf_free_pages(bp);
324         } else if (bp->pb_flags & _PBF_KMEM_ALLOC) {
325                  /*
326                   * XXX(hch): bp->pb_count_desired might be incorrect (see
327                   * pagebuf_associate_memory for details), but fortunately
328                   * the Linux version of kmem_free ignores the len argument..
329                   */
330                 kmem_free(bp->pb_addr, bp->pb_count_desired);
331                 _pagebuf_free_pages(bp);
332         }
333
334         pagebuf_deallocate(bp);
335 }
336
337 /*
338  *      Finds all pages for buffer in question and builds it's page list.
339  */
340 STATIC int
341 _pagebuf_lookup_pages(
342         xfs_buf_t               *bp,
343         uint                    flags)
344 {
345         struct address_space    *mapping = bp->pb_target->pbr_mapping;
346         unsigned int            sectorshift = bp->pb_target->pbr_sshift;
347         size_t                  blocksize = bp->pb_target->pbr_bsize;
348         size_t                  size = bp->pb_count_desired;
349         size_t                  nbytes, offset;
350         int                     gfp_mask = pb_to_gfp(flags);
351         unsigned short          page_count, i;
352         pgoff_t                 first;
353         loff_t                  end;
354         int                     error;
355
356         end = bp->pb_file_offset + bp->pb_buffer_length;
357         page_count = page_buf_btoc(end) - page_buf_btoct(bp->pb_file_offset);
358
359         error = _pagebuf_get_pages(bp, page_count, flags);
360         if (unlikely(error))
361                 return error;
362
363         offset = bp->pb_offset;
364         first = bp->pb_file_offset >> PAGE_CACHE_SHIFT;
365
366         for (i = 0; i < bp->pb_page_count; i++) {
367                 struct page     *page;
368                 uint            retries = 0;
369
370               retry:
371                 page = find_or_create_page(mapping, first + i, gfp_mask);
372                 if (unlikely(page == NULL)) {
373                         if (flags & PBF_READ_AHEAD)
374                                 return -ENOMEM;
375
376                         /*
377                          * This could deadlock.
378                          *
379                          * But until all the XFS lowlevel code is revamped to
380                          * handle buffer allocation failures we can't do much.
381                          */
382                         if (!(++retries % 100)) {
383                                 printk(KERN_ERR "possibly deadlocking in %s\n",
384                                                 __FUNCTION__);
385                         }
386
387                         XFS_STATS_INC(pb_page_retries);
388                         pagebuf_daemon_wakeup();
389                         current->state = TASK_UNINTERRUPTIBLE;
390                         schedule_timeout(10);
391                         goto retry;
392                 }
393
394                 XFS_STATS_INC(pb_page_found);
395
396                 nbytes = min_t(size_t, size, PAGE_CACHE_SIZE - offset);
397                 size -= nbytes;
398
399                 if (!PageUptodate(page)) {
400                         page_count--;
401                         if (blocksize == PAGE_CACHE_SIZE) {
402                                 if (flags & PBF_READ)
403                                         bp->pb_locked = 1;
404                         } else if (!PagePrivate(page)) {
405                                 unsigned long   j, range;
406
407                                 /*
408                                  * In this case page->private holds a bitmap
409                                  * of uptodate sectors within the page
410                                  */
411                                 ASSERT(blocksize < PAGE_CACHE_SIZE);
412                                 range = (offset + nbytes) >> sectorshift;
413                                 for (j = offset >> sectorshift; j < range; j++)
414                                         if (!test_bit(j, &page->private))
415                                                 break;
416                                 if (j == range)
417                                         page_count++;
418                         }
419                 }
420
421                 bp->pb_pages[i] = page;
422                 offset = 0;
423         }
424
425         if (!bp->pb_locked) {
426                 for (i = 0; i < bp->pb_page_count; i++)
427                         unlock_page(bp->pb_pages[i]);
428         }
429
430         bp->pb_flags |= _PBF_PAGE_CACHE;
431
432         if (page_count) {
433                 /* if we have any uptodate pages, mark that in the buffer */
434                 bp->pb_flags &= ~PBF_NONE;
435
436                 /* if some pages aren't uptodate, mark that in the buffer */
437                 if (page_count != bp->pb_page_count)
438                         bp->pb_flags |= PBF_PARTIAL;
439         }
440
441         PB_TRACE(bp, "lookup_pages", (long)page_count);
442         return error;
443 }
444
445 /*
446  *      Map buffer into kernel address-space if nessecary.
447  */
448 STATIC int
449 _pagebuf_map_pages(
450         xfs_buf_t               *bp,
451         uint                    flags)
452 {
453         /* A single page buffer is always mappable */
454         if (bp->pb_page_count == 1) {
455                 bp->pb_addr = page_address(bp->pb_pages[0]) + bp->pb_offset;
456                 bp->pb_flags |= PBF_MAPPED;
457         } else if (flags & PBF_MAPPED) {
458                 if (as_list_len > 64)
459                         purge_addresses();
460                 bp->pb_addr = vmap(bp->pb_pages, bp->pb_page_count,
461                                 VM_MAP, PAGE_KERNEL);
462                 if (unlikely(bp->pb_addr == NULL))
463                         return -ENOMEM;
464                 bp->pb_addr += bp->pb_offset;
465                 bp->pb_flags |= PBF_MAPPED;
466         }
467
468         return 0;
469 }
470
471 /*
472  *      Finding and Reading Buffers
473  */
474
475 /*
476  *      _pagebuf_find
477  *
478  *      Looks up, and creates if absent, a lockable buffer for
479  *      a given range of an inode.  The buffer is returned
480  *      locked.  If other overlapping buffers exist, they are
481  *      released before the new buffer is created and locked,
482  *      which may imply that this call will block until those buffers
483  *      are unlocked.  No I/O is implied by this call.
484  */
485 STATIC xfs_buf_t *
486 _pagebuf_find(                          /* find buffer for block        */
487         xfs_buftarg_t           *target,/* target for block             */
488         loff_t                  ioff,   /* starting offset of range     */
489         size_t                  isize,  /* length of range              */
490         page_buf_flags_t        flags,  /* PBF_TRYLOCK                  */
491         xfs_buf_t               *new_pb)/* newly allocated buffer       */
492 {
493         loff_t                  range_base;
494         size_t                  range_length;
495         int                     hval;
496         pb_hash_t               *h;
497         xfs_buf_t               *pb, *n;
498         int                     not_locked;
499
500         range_base = (ioff << BBSHIFT);
501         range_length = (isize << BBSHIFT);
502
503         /* Ensure we never do IOs smaller than the sector size */
504         BUG_ON(range_length < (1 << target->pbr_sshift));
505
506         /* Ensure we never do IOs that are not sector aligned */
507         BUG_ON(range_base & (loff_t)target->pbr_smask);
508
509         hval = _bhash(target->pbr_bdev, range_base);
510         h = &pbhash[hval];
511
512         spin_lock(&h->pb_hash_lock);
513         list_for_each_entry_safe(pb, n, &h->pb_hash, pb_hash_list) {
514                 if (pb->pb_target == target &&
515                     pb->pb_file_offset == range_base &&
516                     pb->pb_buffer_length == range_length) {
517                         /* If we look at something bring it to the
518                          * front of the list for next time
519                          */
520                         atomic_inc(&pb->pb_hold);
521                         list_move(&pb->pb_hash_list, &h->pb_hash);
522                         goto found;
523                 }
524         }
525
526         /* No match found */
527         if (new_pb) {
528                 _pagebuf_initialize(new_pb, target, range_base,
529                                 range_length, flags);
530                 new_pb->pb_hash_index = hval;
531                 list_add(&new_pb->pb_hash_list, &h->pb_hash);
532         } else {
533                 XFS_STATS_INC(pb_miss_locked);
534         }
535
536         spin_unlock(&h->pb_hash_lock);
537         return (new_pb);
538
539 found:
540         spin_unlock(&h->pb_hash_lock);
541
542         /* Attempt to get the semaphore without sleeping,
543          * if this does not work then we need to drop the
544          * spinlock and do a hard attempt on the semaphore.
545          */
546         not_locked = down_trylock(&pb->pb_sema);
547         if (not_locked) {
548                 if (!(flags & PBF_TRYLOCK)) {
549                         /* wait for buffer ownership */
550                         PB_TRACE(pb, "get_lock", 0);
551                         pagebuf_lock(pb);
552                         XFS_STATS_INC(pb_get_locked_waited);
553                 } else {
554                         /* We asked for a trylock and failed, no need
555                          * to look at file offset and length here, we
556                          * know that this pagebuf at least overlaps our
557                          * pagebuf and is locked, therefore our buffer
558                          * either does not exist, or is this buffer
559                          */
560
561                         pagebuf_rele(pb);
562                         XFS_STATS_INC(pb_busy_locked);
563                         return (NULL);
564                 }
565         } else {
566                 /* trylock worked */
567                 PB_SET_OWNER(pb);
568         }
569
570         if (pb->pb_flags & PBF_STALE)
571                 pb->pb_flags &= PBF_MAPPED;
572         PB_TRACE(pb, "got_lock", 0);
573         XFS_STATS_INC(pb_get_locked);
574         return (pb);
575 }
576
577
578 /*
579  *      pagebuf_find
580  *
581  *      pagebuf_find returns a buffer matching the specified range of
582  *      data for the specified target, if any of the relevant blocks
583  *      are in memory.  The buffer may have unallocated holes, if
584  *      some, but not all, of the blocks are in memory.  Even where
585  *      pages are present in the buffer, not all of every page may be
586  *      valid.
587  */
588 xfs_buf_t *
589 pagebuf_find(                           /* find buffer for block        */
590                                         /* if the block is in memory    */
591         xfs_buftarg_t           *target,/* target for block             */
592         loff_t                  ioff,   /* starting offset of range     */
593         size_t                  isize,  /* length of range              */
594         page_buf_flags_t        flags)  /* PBF_TRYLOCK                  */
595 {
596         return _pagebuf_find(target, ioff, isize, flags, NULL);
597 }
598
599 /*
600  *      pagebuf_get
601  *
602  *      pagebuf_get assembles a buffer covering the specified range.
603  *      Some or all of the blocks in the range may be valid.  Storage
604  *      in memory for all portions of the buffer will be allocated,
605  *      although backing storage may not be.  If PBF_READ is set in
606  *      flags, pagebuf_iostart is called also.
607  */
608 xfs_buf_t *
609 pagebuf_get(                            /* allocate a buffer            */
610         xfs_buftarg_t           *target,/* target for buffer            */
611         loff_t                  ioff,   /* starting offset of range     */
612         size_t                  isize,  /* length of range              */
613         page_buf_flags_t        flags)  /* PBF_TRYLOCK                  */
614 {
615         xfs_buf_t               *pb, *new_pb;
616         int                     error = 0, i;
617
618         new_pb = pagebuf_allocate(flags);
619         if (unlikely(!new_pb))
620                 return NULL;
621
622         pb = _pagebuf_find(target, ioff, isize, flags, new_pb);
623         if (pb == new_pb) {
624                 error = _pagebuf_lookup_pages(pb, flags);
625                 if (unlikely(error)) {
626                         printk(KERN_WARNING
627                                "pagebuf_get: failed to lookup pages\n");
628                         goto no_buffer;
629                 }
630         } else {
631                 pagebuf_deallocate(new_pb);
632                 if (unlikely(pb == NULL))
633                         return NULL;
634         }
635
636         for (i = 0; i < pb->pb_page_count; i++)
637                 mark_page_accessed(pb->pb_pages[i]);
638
639         if (!(pb->pb_flags & PBF_MAPPED)) {
640                 error = _pagebuf_map_pages(pb, flags);
641                 if (unlikely(error)) {
642                         printk(KERN_WARNING
643                                "pagebuf_get: failed to map pages\n");
644                         goto no_buffer;
645                 }
646         }
647
648         XFS_STATS_INC(pb_get);
649
650         /*
651          * Always fill in the block number now, the mapped cases can do
652          * their own overlay of this later.
653          */
654         pb->pb_bn = ioff;
655         pb->pb_count_desired = pb->pb_buffer_length;
656
657         if (flags & PBF_READ) {
658                 if (PBF_NOT_DONE(pb)) {
659                         PB_TRACE(pb, "get_read", (unsigned long)flags);
660                         XFS_STATS_INC(pb_get_read);
661                         pagebuf_iostart(pb, flags);
662                 } else if (flags & PBF_ASYNC) {
663                         PB_TRACE(pb, "get_read_async", (unsigned long)flags);
664                         /*
665                          * Read ahead call which is already satisfied,
666                          * drop the buffer
667                          */
668                         goto no_buffer;
669                 } else {
670                         PB_TRACE(pb, "get_read_done", (unsigned long)flags);
671                         /* We do not want read in the flags */
672                         pb->pb_flags &= ~PBF_READ;
673                 }
674         } else {
675                 PB_TRACE(pb, "get_write", (unsigned long)flags);
676         }
677
678         return pb;
679
680 no_buffer:
681         if (flags & (PBF_LOCK | PBF_TRYLOCK))
682                 pagebuf_unlock(pb);
683         pagebuf_rele(pb);
684         return NULL;
685 }
686
687 /*
688  * Create a skeletal pagebuf (no pages associated with it).
689  */
690 xfs_buf_t *
691 pagebuf_lookup(
692         xfs_buftarg_t           *target,
693         loff_t                  ioff,
694         size_t                  isize,
695         page_buf_flags_t        flags)
696 {
697         xfs_buf_t               *pb;
698
699         pb = pagebuf_allocate(flags);
700         if (pb) {
701                 _pagebuf_initialize(pb, target, ioff, isize, flags);
702         }
703         return pb;
704 }
705
706 /*
707  * If we are not low on memory then do the readahead in a deadlock
708  * safe manner.
709  */
710 void
711 pagebuf_readahead(
712         xfs_buftarg_t           *target,
713         loff_t                  ioff,
714         size_t                  isize,
715         page_buf_flags_t        flags)
716 {
717         struct backing_dev_info *bdi;
718
719         bdi = target->pbr_mapping->backing_dev_info;
720         if (bdi_read_congested(bdi))
721                 return;
722         if (bdi_write_congested(bdi))
723                 return;
724
725         flags |= (PBF_TRYLOCK|PBF_READ|PBF_ASYNC|PBF_READ_AHEAD);
726         pagebuf_get(target, ioff, isize, flags);
727 }
728
729 xfs_buf_t *
730 pagebuf_get_empty(
731         size_t                  len,
732         xfs_buftarg_t           *target)
733 {
734         xfs_buf_t               *pb;
735
736         pb = pagebuf_allocate(0);
737         if (pb)
738                 _pagebuf_initialize(pb, target, 0, len, 0);
739         return pb;
740 }
741
742 static inline struct page *
743 mem_to_page(
744         void                    *addr)
745 {
746         if (((unsigned long)addr < VMALLOC_START) ||
747             ((unsigned long)addr >= VMALLOC_END)) {
748                 return virt_to_page(addr);
749         } else {
750                 return vmalloc_to_page(addr);
751         }
752 }
753
754 int
755 pagebuf_associate_memory(
756         xfs_buf_t               *pb,
757         void                    *mem,
758         size_t                  len)
759 {
760         int                     rval;
761         int                     i = 0;
762         size_t                  ptr;
763         size_t                  end, end_cur;
764         off_t                   offset;
765         int                     page_count;
766
767         page_count = PAGE_CACHE_ALIGN(len) >> PAGE_CACHE_SHIFT;
768         offset = (off_t) mem - ((off_t)mem & PAGE_CACHE_MASK);
769         if (offset && (len > PAGE_CACHE_SIZE))
770                 page_count++;
771
772         /* Free any previous set of page pointers */
773         if (pb->pb_pages)
774                 _pagebuf_free_pages(pb);
775
776         pb->pb_pages = NULL;
777         pb->pb_addr = mem;
778
779         rval = _pagebuf_get_pages(pb, page_count, 0);
780         if (rval)
781                 return rval;
782
783         pb->pb_offset = offset;
784         ptr = (size_t) mem & PAGE_CACHE_MASK;
785         end = PAGE_CACHE_ALIGN((size_t) mem + len);
786         end_cur = end;
787         /* set up first page */
788         pb->pb_pages[0] = mem_to_page(mem);
789
790         ptr += PAGE_CACHE_SIZE;
791         pb->pb_page_count = ++i;
792         while (ptr < end) {
793                 pb->pb_pages[i] = mem_to_page((void *)ptr);
794                 pb->pb_page_count = ++i;
795                 ptr += PAGE_CACHE_SIZE;
796         }
797         pb->pb_locked = 0;
798
799         pb->pb_count_desired = pb->pb_buffer_length = len;
800         pb->pb_flags |= PBF_MAPPED;
801
802         return 0;
803 }
804
805 xfs_buf_t *
806 pagebuf_get_no_daddr(
807         size_t                  len,
808         xfs_buftarg_t           *target)
809 {
810         size_t                  malloc_len = len;
811         xfs_buf_t               *bp;
812         void                    *data;
813         int                     error;
814
815         if (unlikely(len > 0x20000))
816                 goto fail;
817
818         bp = pagebuf_allocate(0);
819         if (unlikely(bp == NULL))
820                 goto fail;
821         _pagebuf_initialize(bp, target, 0, len, PBF_FORCEIO);
822
823  try_again:
824         data = kmem_alloc(malloc_len, KM_SLEEP);
825         if (unlikely(data == NULL))
826                 goto fail_free_buf;
827
828         /* check whether alignment matches.. */
829         if ((__psunsigned_t)data !=
830             ((__psunsigned_t)data & ~target->pbr_smask)) {
831                 /* .. else double the size and try again */
832                 kmem_free(data, malloc_len);
833                 malloc_len <<= 1;
834                 goto try_again;
835         }
836
837         error = pagebuf_associate_memory(bp, data, len);
838         if (error)
839                 goto fail_free_mem;
840         bp->pb_flags |= _PBF_KMEM_ALLOC;
841
842         pagebuf_unlock(bp);
843
844         PB_TRACE(bp, "no_daddr", data);
845         return bp;
846  fail_free_mem:
847         kmem_free(data, malloc_len);
848  fail_free_buf:
849         pagebuf_free(bp);
850  fail:
851         return NULL;
852 }
853
854 /*
855  *      pagebuf_hold
856  *
857  *      Increment reference count on buffer, to hold the buffer concurrently
858  *      with another thread which may release (free) the buffer asynchronously.
859  *
860  *      Must hold the buffer already to call this function.
861  */
862 void
863 pagebuf_hold(
864         xfs_buf_t               *pb)
865 {
866         atomic_inc(&pb->pb_hold);
867         PB_TRACE(pb, "hold", 0);
868 }
869
870 /*
871  *      pagebuf_rele
872  *
873  *      pagebuf_rele releases a hold on the specified buffer.  If the
874  *      the hold count is 1, pagebuf_rele calls pagebuf_free.
875  */
876 void
877 pagebuf_rele(
878         xfs_buf_t               *pb)
879 {
880         pb_hash_t               *hash = pb_hash(pb);
881
882         PB_TRACE(pb, "rele", pb->pb_relse);
883
884         if (atomic_dec_and_lock(&pb->pb_hold, &hash->pb_hash_lock)) {
885                 int             do_free = 1;
886
887                 if (pb->pb_relse) {
888                         atomic_inc(&pb->pb_hold);
889                         spin_unlock(&hash->pb_hash_lock);
890                         (*(pb->pb_relse)) (pb);
891                         spin_lock(&hash->pb_hash_lock);
892                         do_free = 0;
893                 }
894
895                 if (pb->pb_flags & PBF_DELWRI) {
896                         pb->pb_flags |= PBF_ASYNC;
897                         atomic_inc(&pb->pb_hold);
898                         pagebuf_delwri_queue(pb, 0);
899                         do_free = 0;
900                 } else if (pb->pb_flags & PBF_FS_MANAGED) {
901                         do_free = 0;
902                 }
903
904                 if (do_free) {
905                         list_del_init(&pb->pb_hash_list);
906                         spin_unlock(&hash->pb_hash_lock);
907                         pagebuf_free(pb);
908                 } else {
909                         spin_unlock(&hash->pb_hash_lock);
910                 }
911         }
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  */
934 int
935 pagebuf_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 /*
950  *      pagebuf_lock_value
951  *
952  *      Return lock value for a pagebuf
953  */
954 int
955 pagebuf_lock_value(
956         xfs_buf_t               *pb)
957 {
958         return(atomic_read(&pb->pb_sema.count));
959 }
960
961 /*
962  *      pagebuf_lock
963  *
964  *      pagebuf_lock locks a buffer object.  Note that this in no way
965  *      locks the underlying pages, so it is only useful for synchronizing
966  *      concurrent use of page buffer objects, not for synchronizing independent
967  *      access to the underlying pages.
968  */
969 int
970 pagebuf_lock(
971         xfs_buf_t               *pb)
972 {
973         PB_TRACE(pb, "lock", 0);
974         if (atomic_read(&pb->pb_io_remaining))
975                 blk_run_address_space(pb->pb_target->pbr_mapping);
976         down(&pb->pb_sema);
977         PB_SET_OWNER(pb);
978         PB_TRACE(pb, "locked", 0);
979         return 0;
980 }
981
982 /*
983  *      pagebuf_unlock
984  *
985  *      pagebuf_unlock releases the lock on the buffer object created by
986  *      pagebuf_lock or pagebuf_cond_lock (not any
987  *      pinning of underlying pages created by pagebuf_pin).
988  */
989 void
990 pagebuf_unlock(                         /* unlock buffer                */
991         xfs_buf_t               *pb)    /* buffer to unlock             */
992 {
993         PB_CLEAR_OWNER(pb);
994         up(&pb->pb_sema);
995         PB_TRACE(pb, "unlock", 0);
996 }
997
998
999 /*
1000  *      Pinning Buffer Storage in Memory
1001  */
1002
1003 /*
1004  *      pagebuf_pin
1005  *
1006  *      pagebuf_pin locks all of the memory represented by a buffer in
1007  *      memory.  Multiple calls to pagebuf_pin and pagebuf_unpin, for
1008  *      the same or different buffers affecting a given page, will
1009  *      properly count the number of outstanding "pin" requests.  The
1010  *      buffer may be released after the pagebuf_pin and a different
1011  *      buffer used when calling pagebuf_unpin, if desired.
1012  *      pagebuf_pin should be used by the file system when it wants be
1013  *      assured that no attempt will be made to force the affected
1014  *      memory to disk.  It does not assure that a given logical page
1015  *      will not be moved to a different physical page.
1016  */
1017 void
1018 pagebuf_pin(
1019         xfs_buf_t               *pb)
1020 {
1021         atomic_inc(&pb->pb_pin_count);
1022         PB_TRACE(pb, "pin", (long)pb->pb_pin_count.counter);
1023 }
1024
1025 /*
1026  *      pagebuf_unpin
1027  *
1028  *      pagebuf_unpin reverses the locking of memory performed by
1029  *      pagebuf_pin.  Note that both functions affected the logical
1030  *      pages associated with the buffer, not the buffer itself.
1031  */
1032 void
1033 pagebuf_unpin(
1034         xfs_buf_t               *pb)
1035 {
1036         if (atomic_dec_and_test(&pb->pb_pin_count)) {
1037                 wake_up_all(&pb->pb_waiters);
1038         }
1039         PB_TRACE(pb, "unpin", (long)pb->pb_pin_count.counter);
1040 }
1041
1042 int
1043 pagebuf_ispin(
1044         xfs_buf_t               *pb)
1045 {
1046         return atomic_read(&pb->pb_pin_count);
1047 }
1048
1049 /*
1050  *      pagebuf_wait_unpin
1051  *
1052  *      pagebuf_wait_unpin waits until all of the memory associated
1053  *      with the buffer is not longer locked in memory.  It returns
1054  *      immediately if none of the affected pages are locked.
1055  */
1056 static inline void
1057 _pagebuf_wait_unpin(
1058         xfs_buf_t               *pb)
1059 {
1060         DECLARE_WAITQUEUE       (wait, current);
1061
1062         if (atomic_read(&pb->pb_pin_count) == 0)
1063                 return;
1064
1065         add_wait_queue(&pb->pb_waiters, &wait);
1066         for (;;) {
1067                 current->state = TASK_UNINTERRUPTIBLE;
1068                 if (atomic_read(&pb->pb_pin_count) == 0)
1069                         break;
1070                 if (atomic_read(&pb->pb_io_remaining))
1071                         blk_run_address_space(pb->pb_target->pbr_mapping);
1072                 schedule();
1073         }
1074         remove_wait_queue(&pb->pb_waiters, &wait);
1075         current->state = TASK_RUNNING;
1076 }
1077
1078 /*
1079  *      Buffer Utility Routines
1080  */
1081
1082 /*
1083  *      pagebuf_iodone
1084  *
1085  *      pagebuf_iodone marks a buffer for which I/O is in progress
1086  *      done with respect to that I/O.  The pb_iodone routine, if
1087  *      present, will be called as a side-effect.
1088  */
1089 void
1090 pagebuf_iodone_work(
1091         void                    *v)
1092 {
1093         xfs_buf_t               *bp = (xfs_buf_t *)v;
1094
1095         if (bp->pb_iodone)
1096                 (*(bp->pb_iodone))(bp);
1097         else if (bp->pb_flags & PBF_ASYNC)
1098                 xfs_buf_relse(bp);
1099 }
1100
1101 void
1102 pagebuf_iodone(
1103         xfs_buf_t               *pb,
1104         int                     dataio,
1105         int                     schedule)
1106 {
1107         pb->pb_flags &= ~(PBF_READ | PBF_WRITE);
1108         if (pb->pb_error == 0) {
1109                 pb->pb_flags &= ~(PBF_PARTIAL | PBF_NONE);
1110         }
1111
1112         PB_TRACE(pb, "iodone", pb->pb_iodone);
1113
1114         if ((pb->pb_iodone) || (pb->pb_flags & PBF_ASYNC)) {
1115                 if (schedule) {
1116                         INIT_WORK(&pb->pb_iodone_work, pagebuf_iodone_work, pb);
1117                         queue_work(dataio ? pagebuf_dataio_workqueue :
1118                                 pagebuf_logio_workqueue, &pb->pb_iodone_work);
1119                 } else {
1120                         pagebuf_iodone_work(pb);
1121                 }
1122         } else {
1123                 up(&pb->pb_iodonesema);
1124         }
1125 }
1126
1127 /*
1128  *      pagebuf_ioerror
1129  *
1130  *      pagebuf_ioerror sets the error code for a buffer.
1131  */
1132 void
1133 pagebuf_ioerror(                        /* mark/clear buffer error flag */
1134         xfs_buf_t               *pb,    /* buffer to mark               */
1135         int                     error)  /* error to store (0 if none)   */
1136 {
1137         ASSERT(error >= 0 && error <= 0xffff);
1138         pb->pb_error = (unsigned short)error;
1139         PB_TRACE(pb, "ioerror", (unsigned long)error);
1140 }
1141
1142 /*
1143  *      pagebuf_iostart
1144  *
1145  *      pagebuf_iostart initiates I/O on a buffer, based on the flags supplied.
1146  *      If necessary, it will arrange for any disk space allocation required,
1147  *      and it will break up the request if the block mappings require it.
1148  *      The pb_iodone routine in the buffer supplied will only be called
1149  *      when all of the subsidiary I/O requests, if any, have been completed.
1150  *      pagebuf_iostart calls the pagebuf_ioinitiate routine or
1151  *      pagebuf_iorequest, if the former routine is not defined, to start
1152  *      the I/O on a given low-level request.
1153  */
1154 int
1155 pagebuf_iostart(                        /* start I/O on a buffer          */
1156         xfs_buf_t               *pb,    /* buffer to start                */
1157         page_buf_flags_t        flags)  /* PBF_LOCK, PBF_ASYNC, PBF_READ, */
1158                                         /* PBF_WRITE, PBF_DELWRI,         */
1159                                         /* PBF_DONT_BLOCK                 */
1160 {
1161         int                     status = 0;
1162
1163         PB_TRACE(pb, "iostart", (unsigned long)flags);
1164
1165         if (flags & PBF_DELWRI) {
1166                 pb->pb_flags &= ~(PBF_READ | PBF_WRITE | PBF_ASYNC);
1167                 pb->pb_flags |= flags & (PBF_DELWRI | PBF_ASYNC);
1168                 pagebuf_delwri_queue(pb, 1);
1169                 return status;
1170         }
1171
1172         pb->pb_flags &= ~(PBF_READ | PBF_WRITE | PBF_ASYNC | PBF_DELWRI | \
1173                         PBF_READ_AHEAD | _PBF_RUN_QUEUES);
1174         pb->pb_flags |= flags & (PBF_READ | PBF_WRITE | PBF_ASYNC | \
1175                         PBF_READ_AHEAD | _PBF_RUN_QUEUES);
1176
1177         BUG_ON(pb->pb_bn == XFS_BUF_DADDR_NULL);
1178
1179         /* For writes allow an alternate strategy routine to precede
1180          * the actual I/O request (which may not be issued at all in
1181          * a shutdown situation, for example).
1182          */
1183         status = (flags & PBF_WRITE) ?
1184                 pagebuf_iostrategy(pb) : pagebuf_iorequest(pb);
1185
1186         /* Wait for I/O if we are not an async request.
1187          * Note: async I/O request completion will release the buffer,
1188          * and that can already be done by this point.  So using the
1189          * buffer pointer from here on, after async I/O, is invalid.
1190          */
1191         if (!status && !(flags & PBF_ASYNC))
1192                 status = pagebuf_iowait(pb);
1193
1194         return status;
1195 }
1196
1197 /*
1198  * Helper routine for pagebuf_iorequest
1199  */
1200
1201 STATIC __inline__ int
1202 _pagebuf_iolocked(
1203         xfs_buf_t               *pb)
1204 {
1205         ASSERT(pb->pb_flags & (PBF_READ|PBF_WRITE));
1206         if (pb->pb_flags & PBF_READ)
1207                 return pb->pb_locked;
1208         return 0;
1209 }
1210
1211 STATIC __inline__ void
1212 _pagebuf_iodone(
1213         xfs_buf_t               *pb,
1214         int                     schedule)
1215 {
1216         if (atomic_dec_and_test(&pb->pb_io_remaining) == 1) {
1217                 pb->pb_locked = 0;
1218                 pagebuf_iodone(pb, (pb->pb_flags & PBF_FS_DATAIOD), schedule);
1219         }
1220 }
1221
1222 STATIC int
1223 bio_end_io_pagebuf(
1224         struct bio              *bio,
1225         unsigned int            bytes_done,
1226         int                     error)
1227 {
1228         xfs_buf_t               *pb = (xfs_buf_t *)bio->bi_private;
1229         unsigned int            i, blocksize = pb->pb_target->pbr_bsize;
1230         unsigned int            sectorshift = pb->pb_target->pbr_sshift;
1231         struct bio_vec          *bvec = bio->bi_io_vec;
1232
1233         if (bio->bi_size)
1234                 return 1;
1235
1236         if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
1237                 pb->pb_error = EIO;
1238
1239         for (i = 0; i < bio->bi_vcnt; i++, bvec++) {
1240                 struct page     *page = bvec->bv_page;
1241
1242                 if (pb->pb_error) {
1243                         SetPageError(page);
1244                 } else if (blocksize == PAGE_CACHE_SIZE) {
1245                         SetPageUptodate(page);
1246                 } else if (!PagePrivate(page) &&
1247                                 (pb->pb_flags & _PBF_PAGE_CACHE)) {
1248                         unsigned long   j, range;
1249
1250                         ASSERT(blocksize < PAGE_CACHE_SIZE);
1251                         range = (bvec->bv_offset + bvec->bv_len) >> sectorshift;
1252                         for (j = bvec->bv_offset >> sectorshift; j < range; j++)
1253                                 set_bit(j, &page->private);
1254                         if (page->private == (unsigned long)(PAGE_CACHE_SIZE-1))
1255                                 SetPageUptodate(page);
1256                 }
1257
1258                 if (_pagebuf_iolocked(pb)) {
1259                         unlock_page(page);
1260                 }
1261         }
1262
1263         _pagebuf_iodone(pb, 1);
1264         bio_put(bio);
1265         return 0;
1266 }
1267
1268 void
1269 _pagebuf_ioapply(
1270         xfs_buf_t               *pb)
1271 {
1272         int                     i, map_i, total_nr_pages, nr_pages;
1273         struct bio              *bio;
1274         int                     offset = pb->pb_offset;
1275         int                     size = pb->pb_count_desired;
1276         sector_t                sector = pb->pb_bn;
1277         unsigned int            blocksize = pb->pb_target->pbr_bsize;
1278         int                     locking = _pagebuf_iolocked(pb);
1279
1280         total_nr_pages = pb->pb_page_count;
1281         map_i = 0;
1282
1283         /* Special code path for reading a sub page size pagebuf in --
1284          * we populate up the whole page, and hence the other metadata
1285          * in the same page.  This optimization is only valid when the
1286          * filesystem block size and the page size are equal.
1287          */
1288         if ((pb->pb_buffer_length < PAGE_CACHE_SIZE) &&
1289             (pb->pb_flags & PBF_READ) && locking &&
1290             (blocksize == PAGE_CACHE_SIZE)) {
1291                 bio = bio_alloc(GFP_NOIO, 1);
1292
1293                 bio->bi_bdev = pb->pb_target->pbr_bdev;
1294                 bio->bi_sector = sector - (offset >> BBSHIFT);
1295                 bio->bi_end_io = bio_end_io_pagebuf;
1296                 bio->bi_private = pb;
1297
1298                 bio_add_page(bio, pb->pb_pages[0], PAGE_CACHE_SIZE, 0);
1299                 size = 0;
1300
1301                 atomic_inc(&pb->pb_io_remaining);
1302
1303                 goto submit_io;
1304         }
1305
1306         /* Lock down the pages which we need to for the request */
1307         if (locking && (pb->pb_flags & PBF_WRITE) && (pb->pb_locked == 0)) {
1308                 for (i = 0; size; i++) {
1309                         int             nbytes = PAGE_CACHE_SIZE - offset;
1310                         struct page     *page = pb->pb_pages[i];
1311
1312                         if (nbytes > size)
1313                                 nbytes = size;
1314
1315                         lock_page(page);
1316
1317                         size -= nbytes;
1318                         offset = 0;
1319                 }
1320                 offset = pb->pb_offset;
1321                 size = pb->pb_count_desired;
1322         }
1323
1324 next_chunk:
1325         atomic_inc(&pb->pb_io_remaining);
1326         nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
1327         if (nr_pages > total_nr_pages)
1328                 nr_pages = total_nr_pages;
1329
1330         bio = bio_alloc(GFP_NOIO, nr_pages);
1331         bio->bi_bdev = pb->pb_target->pbr_bdev;
1332         bio->bi_sector = sector;
1333         bio->bi_end_io = bio_end_io_pagebuf;
1334         bio->bi_private = pb;
1335
1336         for (; size && nr_pages; nr_pages--, map_i++) {
1337                 int     nbytes = PAGE_CACHE_SIZE - offset;
1338
1339                 if (nbytes > size)
1340                         nbytes = size;
1341
1342                 if (bio_add_page(bio, pb->pb_pages[map_i],
1343                                         nbytes, offset) < nbytes)
1344                         break;
1345
1346                 offset = 0;
1347                 sector += nbytes >> BBSHIFT;
1348                 size -= nbytes;
1349                 total_nr_pages--;
1350         }
1351
1352 submit_io:
1353         if (likely(bio->bi_size)) {
1354                 submit_bio((pb->pb_flags & PBF_READ) ? READ : WRITE, bio);
1355                 if (size)
1356                         goto next_chunk;
1357         } else {
1358                 bio_put(bio);
1359                 pagebuf_ioerror(pb, EIO);
1360         }
1361
1362         if (pb->pb_flags & _PBF_RUN_QUEUES) {
1363                 pb->pb_flags &= ~_PBF_RUN_QUEUES;
1364                 if (atomic_read(&pb->pb_io_remaining) > 1)
1365                         blk_run_address_space(pb->pb_target->pbr_mapping);
1366         }
1367 }
1368
1369 /*
1370  *      pagebuf_iorequest -- the core I/O request routine.
1371  */
1372 int
1373 pagebuf_iorequest(                      /* start real I/O               */
1374         xfs_buf_t               *pb)    /* buffer to convey to device   */
1375 {
1376         PB_TRACE(pb, "iorequest", 0);
1377
1378         if (pb->pb_flags & PBF_DELWRI) {
1379                 pagebuf_delwri_queue(pb, 1);
1380                 return 0;
1381         }
1382
1383         if (pb->pb_flags & PBF_WRITE) {
1384                 _pagebuf_wait_unpin(pb);
1385         }
1386
1387         pagebuf_hold(pb);
1388
1389         /* Set the count to 1 initially, this will stop an I/O
1390          * completion callout which happens before we have started
1391          * all the I/O from calling pagebuf_iodone too early.
1392          */
1393         atomic_set(&pb->pb_io_remaining, 1);
1394         _pagebuf_ioapply(pb);
1395         _pagebuf_iodone(pb, 0);
1396
1397         pagebuf_rele(pb);
1398         return 0;
1399 }
1400
1401 /*
1402  *      pagebuf_iowait
1403  *
1404  *      pagebuf_iowait waits for I/O to complete on the buffer supplied.
1405  *      It returns immediately if no I/O is pending.  In any case, it returns
1406  *      the error code, if any, or 0 if there is no error.
1407  */
1408 int
1409 pagebuf_iowait(
1410         xfs_buf_t               *pb)
1411 {
1412         PB_TRACE(pb, "iowait", 0);
1413         if (atomic_read(&pb->pb_io_remaining))
1414                 blk_run_address_space(pb->pb_target->pbr_mapping);
1415         down(&pb->pb_iodonesema);
1416         PB_TRACE(pb, "iowaited", (long)pb->pb_error);
1417         return pb->pb_error;
1418 }
1419
1420 caddr_t
1421 pagebuf_offset(
1422         xfs_buf_t               *pb,
1423         size_t                  offset)
1424 {
1425         struct page             *page;
1426
1427         offset += pb->pb_offset;
1428
1429         page = pb->pb_pages[offset >> PAGE_CACHE_SHIFT];
1430         return (caddr_t) page_address(page) + (offset & (PAGE_CACHE_SIZE - 1));
1431 }
1432
1433 /*
1434  *      pagebuf_iomove
1435  *
1436  *      Move data into or out of a buffer.
1437  */
1438 void
1439 pagebuf_iomove(
1440         xfs_buf_t               *pb,    /* buffer to process            */
1441         size_t                  boff,   /* starting buffer offset       */
1442         size_t                  bsize,  /* length to copy               */
1443         caddr_t                 data,   /* data address                 */
1444         page_buf_rw_t           mode)   /* read/write flag              */
1445 {
1446         size_t                  bend, cpoff, csize;
1447         struct page             *page;
1448
1449         bend = boff + bsize;
1450         while (boff < bend) {
1451                 page = pb->pb_pages[page_buf_btoct(boff + pb->pb_offset)];
1452                 cpoff = page_buf_poff(boff + pb->pb_offset);
1453                 csize = min_t(size_t,
1454                               PAGE_CACHE_SIZE-cpoff, pb->pb_count_desired-boff);
1455
1456                 ASSERT(((csize + cpoff) <= PAGE_CACHE_SIZE));
1457
1458                 switch (mode) {
1459                 case PBRW_ZERO:
1460                         memset(page_address(page) + cpoff, 0, csize);
1461                         break;
1462                 case PBRW_READ:
1463                         memcpy(data, page_address(page) + cpoff, csize);
1464                         break;
1465                 case PBRW_WRITE:
1466                         memcpy(page_address(page) + cpoff, data, csize);
1467                 }
1468
1469                 boff += csize;
1470                 data += csize;
1471         }
1472 }
1473
1474 /*
1475  *      Handling of buftargs.
1476  */
1477
1478 void
1479 xfs_free_buftarg(
1480         xfs_buftarg_t           *btp,
1481         int                     external)
1482 {
1483         xfs_flush_buftarg(btp, 1);
1484         if (external)
1485                 xfs_blkdev_put(btp->pbr_bdev);
1486         kmem_free(btp, sizeof(*btp));
1487 }
1488
1489 void
1490 xfs_incore_relse(
1491         xfs_buftarg_t           *btp,
1492         int                     delwri_only,
1493         int                     wait)
1494 {
1495         invalidate_bdev(btp->pbr_bdev, 1);
1496         truncate_inode_pages(btp->pbr_mapping, 0LL);
1497 }
1498
1499 void
1500 xfs_setsize_buftarg(
1501         xfs_buftarg_t           *btp,
1502         unsigned int            blocksize,
1503         unsigned int            sectorsize)
1504 {
1505         btp->pbr_bsize = blocksize;
1506         btp->pbr_sshift = ffs(sectorsize) - 1;
1507         btp->pbr_smask = sectorsize - 1;
1508
1509         if (set_blocksize(btp->pbr_bdev, sectorsize)) {
1510                 printk(KERN_WARNING
1511                         "XFS: Cannot set_blocksize to %u on device %s\n",
1512                         sectorsize, XFS_BUFTARG_NAME(btp));
1513         }
1514 }
1515
1516 xfs_buftarg_t *
1517 xfs_alloc_buftarg(
1518         struct block_device     *bdev)
1519 {
1520         xfs_buftarg_t           *btp;
1521
1522         btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
1523
1524         btp->pbr_dev =  bdev->bd_dev;
1525         btp->pbr_bdev = bdev;
1526         btp->pbr_mapping = bdev->bd_inode->i_mapping;
1527         xfs_setsize_buftarg(btp, PAGE_CACHE_SIZE, bdev_hardsect_size(bdev));
1528
1529         return btp;
1530 }
1531
1532
1533 /*
1534  * Pagebuf delayed write buffer handling
1535  */
1536
1537 STATIC LIST_HEAD(pbd_delwrite_queue);
1538 STATIC spinlock_t pbd_delwrite_lock = SPIN_LOCK_UNLOCKED;
1539
1540 STATIC void
1541 pagebuf_delwri_queue(
1542         xfs_buf_t               *pb,
1543         int                     unlock)
1544 {
1545         PB_TRACE(pb, "delwri_q", (long)unlock);
1546         ASSERT(pb->pb_flags & PBF_DELWRI);
1547
1548         spin_lock(&pbd_delwrite_lock);
1549         /* If already in the queue, dequeue and place at tail */
1550         if (!list_empty(&pb->pb_list)) {
1551                 if (unlock) {
1552                         atomic_dec(&pb->pb_hold);
1553                 }
1554                 list_del(&pb->pb_list);
1555         }
1556
1557         list_add_tail(&pb->pb_list, &pbd_delwrite_queue);
1558         pb->pb_queuetime = jiffies;
1559         spin_unlock(&pbd_delwrite_lock);
1560
1561         if (unlock)
1562                 pagebuf_unlock(pb);
1563 }
1564
1565 void
1566 pagebuf_delwri_dequeue(
1567         xfs_buf_t               *pb)
1568 {
1569         PB_TRACE(pb, "delwri_uq", 0);
1570         spin_lock(&pbd_delwrite_lock);
1571         list_del_init(&pb->pb_list);
1572         pb->pb_flags &= ~PBF_DELWRI;
1573         spin_unlock(&pbd_delwrite_lock);
1574 }
1575
1576 STATIC void
1577 pagebuf_runall_queues(
1578         struct workqueue_struct *queue)
1579 {
1580         flush_workqueue(queue);
1581 }
1582
1583 /* Defines for pagebuf daemon */
1584 STATIC DECLARE_COMPLETION(pagebuf_daemon_done);
1585 STATIC struct task_struct *pagebuf_daemon_task;
1586 STATIC int pagebuf_daemon_active;
1587 STATIC int force_flush;
1588
1589 STATIC void
1590 pagebuf_daemon_wakeup(void)
1591 {
1592         force_flush = 1;
1593         barrier();
1594         wake_up_process(pagebuf_daemon_task);
1595 }
1596
1597 STATIC int
1598 pagebuf_daemon(
1599         void                    *data)
1600 {
1601         struct list_head        tmp;
1602         xfs_buf_t               *pb, *n;
1603
1604         /*  Set up the thread  */
1605         daemonize("xfsbufd");
1606         current->flags |= PF_MEMALLOC;
1607
1608         pagebuf_daemon_task = current;
1609         pagebuf_daemon_active = 1;
1610         barrier();
1611
1612         INIT_LIST_HEAD(&tmp);
1613         do {
1614                 /* swsusp */
1615                 if (current->flags & PF_FREEZE)
1616                         refrigerator(PF_FREEZE);
1617
1618                 set_current_state(TASK_INTERRUPTIBLE);
1619                 schedule_timeout(xfs_flush_interval);
1620
1621                 spin_lock(&pbd_delwrite_lock);
1622                 list_for_each_entry_safe(pb, n, &pbd_delwrite_queue, pb_list) {
1623                         PB_TRACE(pb, "walkq1", (long)pagebuf_ispin(pb));
1624                         ASSERT(pb->pb_flags & PBF_DELWRI);
1625
1626                         if (!pagebuf_ispin(pb) && !pagebuf_cond_lock(pb)) {
1627                                 if (!force_flush &&
1628                                     time_before(jiffies,
1629                                                 pb->pb_queuetime +
1630                                                 xfs_age_buffer)) {
1631                                         pagebuf_unlock(pb);
1632                                         break;
1633                                 }
1634
1635                                 pb->pb_flags &= ~PBF_DELWRI;
1636                                 pb->pb_flags |= PBF_WRITE;
1637                                 list_move(&pb->pb_list, &tmp);
1638                         }
1639                 }
1640                 spin_unlock(&pbd_delwrite_lock);
1641
1642                 while (!list_empty(&tmp)) {
1643                         pb = list_entry(tmp.next, xfs_buf_t, pb_list);
1644                         list_del_init(&pb->pb_list);
1645                         pagebuf_iostrategy(pb);
1646                         blk_run_address_space(pb->pb_target->pbr_mapping);
1647                 }
1648
1649                 if (as_list_len > 0)
1650                         purge_addresses();
1651
1652                 force_flush = 0;
1653         } while (pagebuf_daemon_active);
1654
1655         complete_and_exit(&pagebuf_daemon_done, 0);
1656 }
1657
1658 /*
1659  * Go through all incore buffers, and release buffers if they belong to
1660  * the given device. This is used in filesystem error handling to
1661  * preserve the consistency of its metadata.
1662  */
1663 int
1664 xfs_flush_buftarg(
1665         xfs_buftarg_t           *target,
1666         int                     wait)
1667 {
1668         struct list_head        tmp;
1669         xfs_buf_t               *pb, *n;
1670         int                     pincount = 0;
1671
1672         pagebuf_runall_queues(pagebuf_dataio_workqueue);
1673         pagebuf_runall_queues(pagebuf_logio_workqueue);
1674
1675         INIT_LIST_HEAD(&tmp);
1676         spin_lock(&pbd_delwrite_lock);
1677         list_for_each_entry_safe(pb, n, &pbd_delwrite_queue, pb_list) {
1678
1679                 if (pb->pb_target != target)
1680                         continue;
1681
1682                 ASSERT(pb->pb_flags & PBF_DELWRI);
1683                 PB_TRACE(pb, "walkq2", (long)pagebuf_ispin(pb));
1684                 if (pagebuf_ispin(pb)) {
1685                         pincount++;
1686                         continue;
1687                 }
1688
1689                 pb->pb_flags &= ~PBF_DELWRI;
1690                 pb->pb_flags |= PBF_WRITE;
1691                 list_move(&pb->pb_list, &tmp);
1692         }
1693         spin_unlock(&pbd_delwrite_lock);
1694
1695         /*
1696          * Dropped the delayed write list lock, now walk the temporary list
1697          */
1698         list_for_each_entry_safe(pb, n, &tmp, pb_list) {
1699                 if (wait)
1700                         pb->pb_flags &= ~PBF_ASYNC;
1701                 else
1702                         list_del_init(&pb->pb_list);
1703
1704                 pagebuf_lock(pb);
1705                 pagebuf_iostrategy(pb);
1706         }
1707
1708         /*
1709          * Remaining list items must be flushed before returning
1710          */
1711         while (!list_empty(&tmp)) {
1712                 pb = list_entry(tmp.next, xfs_buf_t, pb_list);
1713
1714                 list_del_init(&pb->pb_list);
1715                 xfs_iowait(pb);
1716                 xfs_buf_relse(pb);
1717         }
1718
1719         if (wait)
1720                 blk_run_address_space(target->pbr_mapping);
1721
1722         return pincount;
1723 }
1724
1725 STATIC int
1726 pagebuf_daemon_start(void)
1727 {
1728         int             rval;
1729
1730         pagebuf_logio_workqueue = create_workqueue("xfslogd");
1731         if (!pagebuf_logio_workqueue)
1732                 return -ENOMEM;
1733
1734         pagebuf_dataio_workqueue = create_workqueue("xfsdatad");
1735         if (!pagebuf_dataio_workqueue) {
1736                 destroy_workqueue(pagebuf_logio_workqueue);
1737                 return -ENOMEM;
1738         }
1739
1740         rval = kernel_thread(pagebuf_daemon, NULL, CLONE_FS|CLONE_FILES);
1741         if (rval < 0) {
1742                 destroy_workqueue(pagebuf_logio_workqueue);
1743                 destroy_workqueue(pagebuf_dataio_workqueue);
1744         }
1745
1746         return rval;
1747 }
1748
1749 /*
1750  * pagebuf_daemon_stop
1751  *
1752  * Note: do not mark as __exit, it is called from pagebuf_terminate.
1753  */
1754 STATIC void
1755 pagebuf_daemon_stop(void)
1756 {
1757         pagebuf_daemon_active = 0;
1758         barrier();
1759         wait_for_completion(&pagebuf_daemon_done);
1760
1761         destroy_workqueue(pagebuf_logio_workqueue);
1762         destroy_workqueue(pagebuf_dataio_workqueue);
1763 }
1764
1765 /*
1766  *      Initialization and Termination
1767  */
1768
1769 int __init
1770 pagebuf_init(void)
1771 {
1772         int                     i;
1773
1774         pagebuf_cache = kmem_cache_create("xfs_buf_t", sizeof(xfs_buf_t), 0,
1775                         SLAB_HWCACHE_ALIGN, NULL, NULL);
1776         if (pagebuf_cache == NULL) {
1777                 printk("pagebuf: couldn't init pagebuf cache\n");
1778                 pagebuf_terminate();
1779                 return -ENOMEM;
1780         }
1781
1782         for (i = 0; i < NHASH; i++) {
1783                 spin_lock_init(&pbhash[i].pb_hash_lock);
1784                 INIT_LIST_HEAD(&pbhash[i].pb_hash);
1785         }
1786
1787 #ifdef PAGEBUF_TRACE
1788         pagebuf_trace_buf = ktrace_alloc(PAGEBUF_TRACE_SIZE, KM_SLEEP);
1789 #endif
1790
1791         pagebuf_daemon_start();
1792         return 0;
1793 }
1794
1795
1796 /*
1797  *      pagebuf_terminate.
1798  *
1799  *      Note: do not mark as __exit, this is also called from the __init code.
1800  */
1801 void
1802 pagebuf_terminate(void)
1803 {
1804         pagebuf_daemon_stop();
1805
1806 #ifdef PAGEBUF_TRACE
1807         ktrace_free(pagebuf_trace_buf);
1808 #endif
1809
1810         kmem_cache_destroy(pagebuf_cache);
1811 }