This commit was manufactured by cvs2svn to create branch
[linux-2.6.git] / fs / buffer.c
1 /*
2  *  linux/fs/buffer.c
3  *
4  *  Copyright (C) 1991, 1992, 2002  Linus Torvalds
5  */
6
7 /*
8  * Start bdflush() with kernel_thread not syscall - Paul Gortmaker, 12/95
9  *
10  * Removed a lot of unnecessary code and simplified things now that
11  * the buffer cache isn't our primary cache - Andrew Tridgell 12/96
12  *
13  * Speed up hash, lru, and free list operations.  Use gfp() for allocating
14  * hash table, use SLAB cache for buffer heads. SMP threading.  -DaveM
15  *
16  * Added 32k buffer block sizes - these are required older ARM systems. - RMK
17  *
18  * async buffer flushing, 1999 Andrea Arcangeli <andrea@suse.de>
19  */
20
21 #include <linux/config.h>
22 #include <linux/kernel.h>
23 #include <linux/fs.h>
24 #include <linux/mm.h>
25 #include <linux/percpu.h>
26 #include <linux/slab.h>
27 #include <linux/smp_lock.h>
28 #include <linux/blkdev.h>
29 #include <linux/file.h>
30 #include <linux/quotaops.h>
31 #include <linux/highmem.h>
32 #include <linux/module.h>
33 #include <linux/writeback.h>
34 #include <linux/hash.h>
35 #include <linux/suspend.h>
36 #include <linux/buffer_head.h>
37 #include <linux/bio.h>
38 #include <linux/notifier.h>
39 #include <linux/cpu.h>
40 #include <asm/bitops.h>
41
42 static void invalidate_bh_lrus(void);
43
44 #define BH_ENTRY(list) list_entry((list), struct buffer_head, b_assoc_buffers)
45
46 struct bh_wait_queue {
47         struct buffer_head *bh;
48         wait_queue_t wait;
49 };
50
51 #define __DEFINE_BH_WAIT(name, b, f)                                    \
52         struct bh_wait_queue name = {                                   \
53                 .bh     = b,                                            \
54                 .wait   = {                                             \
55                                 .task   = current,                      \
56                                 .flags  = f,                            \
57                                 .func   = bh_wake_function,             \
58                                 .task_list =                            \
59                                         LIST_HEAD_INIT(name.wait.task_list),\
60                         },                                              \
61         }
62 #define DEFINE_BH_WAIT(name, bh)        __DEFINE_BH_WAIT(name, bh, 0)
63 #define DEFINE_BH_WAIT_EXCLUSIVE(name, bh) \
64                 __DEFINE_BH_WAIT(name, bh, WQ_FLAG_EXCLUSIVE)
65
66 /*
67  * Hashed waitqueue_head's for wait_on_buffer()
68  */
69 #define BH_WAIT_TABLE_ORDER     7
70 static struct bh_wait_queue_head {
71         wait_queue_head_t wqh;
72 } ____cacheline_aligned_in_smp bh_wait_queue_heads[1<<BH_WAIT_TABLE_ORDER];
73
74 inline void
75 init_buffer(struct buffer_head *bh, bh_end_io_t *handler, void *private)
76 {
77         bh->b_end_io = handler;
78         bh->b_private = private;
79 }
80
81 /*
82  * Return the address of the waitqueue_head to be used for this
83  * buffer_head
84  */
85 wait_queue_head_t *bh_waitq_head(struct buffer_head *bh)
86 {
87         return &bh_wait_queue_heads[hash_ptr(bh, BH_WAIT_TABLE_ORDER)].wqh;
88 }
89 EXPORT_SYMBOL(bh_waitq_head);
90
91 void wake_up_buffer(struct buffer_head *bh)
92 {
93         wait_queue_head_t *wq = bh_waitq_head(bh);
94
95         smp_mb();
96         if (waitqueue_active(wq))
97                 __wake_up(wq, TASK_INTERRUPTIBLE|TASK_UNINTERRUPTIBLE, 1, bh);
98 }
99 EXPORT_SYMBOL(wake_up_buffer);
100
101 static int bh_wake_function(wait_queue_t *wait, unsigned mode,
102                                 int sync, void *key)
103 {
104         struct buffer_head *bh = key;
105         struct bh_wait_queue *wq;
106
107         wq = container_of(wait, struct bh_wait_queue, wait);
108         if (wq->bh != bh || buffer_locked(bh))
109                 return 0;
110         else
111                 return autoremove_wake_function(wait, mode, sync, key);
112 }
113
114 static void sync_buffer(struct buffer_head *bh)
115 {
116         struct block_device *bd;
117
118         smp_mb();
119         bd = bh->b_bdev;
120         if (bd)
121                 blk_run_address_space(bd->bd_inode->i_mapping);
122 }
123
124 void fastcall __lock_buffer(struct buffer_head *bh)
125 {
126         wait_queue_head_t *wqh = bh_waitq_head(bh);
127         DEFINE_BH_WAIT_EXCLUSIVE(wait, bh);
128
129         do {
130                 prepare_to_wait_exclusive(wqh, &wait.wait,
131                                         TASK_UNINTERRUPTIBLE);
132                 if (buffer_locked(bh)) {
133                         sync_buffer(bh);
134                         io_schedule();
135                 }
136         } while (test_set_buffer_locked(bh));
137         finish_wait(wqh, &wait.wait);
138 }
139 EXPORT_SYMBOL(__lock_buffer);
140
141 void fastcall unlock_buffer(struct buffer_head *bh)
142 {
143         clear_buffer_locked(bh);
144         smp_mb__after_clear_bit();
145         wake_up_buffer(bh);
146 }
147
148 /*
149  * Block until a buffer comes unlocked.  This doesn't stop it
150  * from becoming locked again - you have to lock it yourself
151  * if you want to preserve its state.
152  */
153 void __wait_on_buffer(struct buffer_head * bh)
154 {
155         wait_queue_head_t *wqh = bh_waitq_head(bh);
156         DEFINE_BH_WAIT(wait, bh);
157
158         do {
159                 prepare_to_wait(wqh, &wait.wait, TASK_UNINTERRUPTIBLE);
160                 if (buffer_locked(bh)) {
161                         sync_buffer(bh);
162                         io_schedule();
163                 }
164         } while (buffer_locked(bh));
165         finish_wait(wqh, &wait.wait);
166 }
167
168 static void
169 __set_page_buffers(struct page *page, struct buffer_head *head)
170 {
171         page_cache_get(page);
172         SetPagePrivate(page);
173         page->private = (unsigned long)head;
174 }
175
176 static void
177 __clear_page_buffers(struct page *page)
178 {
179         ClearPagePrivate(page);
180         page->private = 0;
181         page_cache_release(page);
182 }
183
184 static void buffer_io_error(struct buffer_head *bh)
185 {
186         char b[BDEVNAME_SIZE];
187
188         printk(KERN_ERR "Buffer I/O error on device %s, logical block %Lu\n",
189                         bdevname(bh->b_bdev, b),
190                         (unsigned long long)bh->b_blocknr);
191 }
192
193 /*
194  * Default synchronous end-of-IO handler..  Just mark it up-to-date and
195  * unlock the buffer. This is what ll_rw_block uses too.
196  */
197 void end_buffer_read_sync(struct buffer_head *bh, int uptodate)
198 {
199         if (uptodate) {
200                 set_buffer_uptodate(bh);
201         } else {
202                 /* This happens, due to failed READA attempts. */
203                 clear_buffer_uptodate(bh);
204         }
205         unlock_buffer(bh);
206         put_bh(bh);
207 }
208
209 void end_buffer_write_sync(struct buffer_head *bh, int uptodate)
210 {
211         char b[BDEVNAME_SIZE];
212
213         if (uptodate) {
214                 set_buffer_uptodate(bh);
215         } else {
216                 if (printk_ratelimit()) {
217                         buffer_io_error(bh);
218                         printk(KERN_WARNING "lost page write due to "
219                                         "I/O error on %s\n",
220                                        bdevname(bh->b_bdev, b));
221                 }
222                 set_buffer_write_io_error(bh);
223                 clear_buffer_uptodate(bh);
224         }
225         unlock_buffer(bh);
226         put_bh(bh);
227 }
228
229 /*
230  * Write out and wait upon all the dirty data associated with a block
231  * device via its mapping.  Does not take the superblock lock.
232  */
233 int sync_blockdev(struct block_device *bdev)
234 {
235         int ret = 0;
236
237         if (bdev) {
238                 int err;
239
240                 ret = filemap_fdatawrite(bdev->bd_inode->i_mapping);
241                 err = filemap_fdatawait(bdev->bd_inode->i_mapping);
242                 if (!ret)
243                         ret = err;
244         }
245         return ret;
246 }
247 EXPORT_SYMBOL(sync_blockdev);
248
249 /*
250  * Write out and wait upon all dirty data associated with this
251  * superblock.  Filesystem data as well as the underlying block
252  * device.  Takes the superblock lock.
253  */
254 int fsync_super(struct super_block *sb)
255 {
256         sync_inodes_sb(sb, 0);
257         DQUOT_SYNC(sb);
258         lock_super(sb);
259         if (sb->s_dirt && sb->s_op->write_super)
260                 sb->s_op->write_super(sb);
261         unlock_super(sb);
262         if (sb->s_op->sync_fs)
263                 sb->s_op->sync_fs(sb, 1);
264         sync_blockdev(sb->s_bdev);
265         sync_inodes_sb(sb, 1);
266
267         return sync_blockdev(sb->s_bdev);
268 }
269
270 /*
271  * Write out and wait upon all dirty data associated with this
272  * device.   Filesystem data as well as the underlying block
273  * device.  Takes the superblock lock.
274  */
275 int fsync_bdev(struct block_device *bdev)
276 {
277         struct super_block *sb = get_super(bdev);
278         if (sb) {
279                 int res = fsync_super(sb);
280                 drop_super(sb);
281                 return res;
282         }
283         return sync_blockdev(bdev);
284 }
285
286 /**
287  * freeze_bdev  --  lock a filesystem and force it into a consistent state
288  * @bdev:       blockdevice to lock
289  *
290  * This takes the block device bd_mount_sem to make sure no new mounts
291  * happen on bdev until thaw_bdev() is called.
292  * If a superblock is found on this device, we take the s_umount semaphore
293  * on it to make sure nobody unmounts until the snapshot creation is done.
294  */
295 struct super_block *freeze_bdev(struct block_device *bdev)
296 {
297         struct super_block *sb;
298
299         down(&bdev->bd_mount_sem);
300         sb = get_super(bdev);
301         if (sb && !(sb->s_flags & MS_RDONLY)) {
302                 sb->s_frozen = SB_FREEZE_WRITE;
303                 wmb();
304
305                 sync_inodes_sb(sb, 0);
306                 DQUOT_SYNC(sb);
307
308                 lock_super(sb);
309                 if (sb->s_dirt && sb->s_op->write_super)
310                         sb->s_op->write_super(sb);
311                 unlock_super(sb);
312
313                 if (sb->s_op->sync_fs)
314                         sb->s_op->sync_fs(sb, 1);
315
316                 sync_blockdev(sb->s_bdev);
317                 sync_inodes_sb(sb, 1);
318
319                 sb->s_frozen = SB_FREEZE_TRANS;
320                 wmb();
321
322                 sync_blockdev(sb->s_bdev);
323
324                 if (sb->s_op->write_super_lockfs)
325                         sb->s_op->write_super_lockfs(sb);
326         }
327
328         sync_blockdev(bdev);
329         return sb;      /* thaw_bdev releases s->s_umount and bd_mount_sem */
330 }
331 EXPORT_SYMBOL(freeze_bdev);
332
333 /**
334  * thaw_bdev  -- unlock filesystem
335  * @bdev:       blockdevice to unlock
336  * @sb:         associated superblock
337  *
338  * Unlocks the filesystem and marks it writeable again after freeze_bdev().
339  */
340 void thaw_bdev(struct block_device *bdev, struct super_block *sb)
341 {
342         if (sb) {
343                 BUG_ON(sb->s_bdev != bdev);
344
345                 if (sb->s_op->unlockfs)
346                         sb->s_op->unlockfs(sb);
347                 sb->s_frozen = SB_UNFROZEN;
348                 wmb();
349                 wake_up(&sb->s_wait_unfrozen);
350                 drop_super(sb);
351         }
352
353         up(&bdev->bd_mount_sem);
354 }
355 EXPORT_SYMBOL(thaw_bdev);
356
357 /*
358  * sync everything.  Start out by waking pdflush, because that writes back
359  * all queues in parallel.
360  */
361 static void do_sync(unsigned long wait)
362 {
363         wakeup_bdflush(0);
364         sync_inodes(0);         /* All mappings, inodes and their blockdevs */
365         DQUOT_SYNC(NULL);
366         sync_supers();          /* Write the superblocks */
367         sync_filesystems(0);    /* Start syncing the filesystems */
368         sync_filesystems(wait); /* Waitingly sync the filesystems */
369         sync_inodes(wait);      /* Mappings, inodes and blockdevs, again. */
370         if (!wait)
371                 printk("Emergency Sync complete\n");
372         if (unlikely(laptop_mode))
373                 laptop_sync_completion();
374 }
375
376 asmlinkage long sys_sync(void)
377 {
378         do_sync(1);
379         return 0;
380 }
381
382 void emergency_sync(void)
383 {
384         pdflush_operation(do_sync, 0);
385 }
386
387 /*
388  * Generic function to fsync a file.
389  *
390  * filp may be NULL if called via the msync of a vma.
391  */
392  
393 int file_fsync(struct file *filp, struct dentry *dentry, int datasync)
394 {
395         struct inode * inode = dentry->d_inode;
396         struct super_block * sb;
397         int ret;
398
399         /* sync the inode to buffers */
400         write_inode_now(inode, 0);
401
402         /* sync the superblock to buffers */
403         sb = inode->i_sb;
404         lock_super(sb);
405         if (sb->s_op->write_super)
406                 sb->s_op->write_super(sb);
407         unlock_super(sb);
408
409         /* .. finally sync the buffers to disk */
410         ret = sync_blockdev(sb->s_bdev);
411         return ret;
412 }
413
414 asmlinkage long sys_fsync(unsigned int fd)
415 {
416         struct file * file;
417         struct address_space *mapping;
418         int ret, err;
419
420         ret = -EBADF;
421         file = fget(fd);
422         if (!file)
423                 goto out;
424
425         mapping = file->f_mapping;
426
427         ret = -EINVAL;
428         if (!file->f_op || !file->f_op->fsync) {
429                 /* Why?  We can still call filemap_fdatawrite */
430                 goto out_putf;
431         }
432
433         /* We need to protect against concurrent writers.. */
434         down(&mapping->host->i_sem);
435         current->flags |= PF_SYNCWRITE;
436         ret = filemap_fdatawrite(mapping);
437         err = file->f_op->fsync(file, file->f_dentry, 0);
438         if (!ret)
439                 ret = err;
440         err = filemap_fdatawait(mapping);
441         if (!ret)
442                 ret = err;
443         current->flags &= ~PF_SYNCWRITE;
444         up(&mapping->host->i_sem);
445
446 out_putf:
447         fput(file);
448 out:
449         return ret;
450 }
451
452 asmlinkage long sys_fdatasync(unsigned int fd)
453 {
454         struct file * file;
455         struct address_space *mapping;
456         int ret, err;
457
458         ret = -EBADF;
459         file = fget(fd);
460         if (!file)
461                 goto out;
462
463         ret = -EINVAL;
464         if (!file->f_op || !file->f_op->fsync)
465                 goto out_putf;
466
467         mapping = file->f_mapping;
468
469         down(&mapping->host->i_sem);
470         current->flags |= PF_SYNCWRITE;
471         ret = filemap_fdatawrite(mapping);
472         err = file->f_op->fsync(file, file->f_dentry, 1);
473         if (!ret)
474                 ret = err;
475         err = filemap_fdatawait(mapping);
476         if (!ret)
477                 ret = err;
478         current->flags &= ~PF_SYNCWRITE;
479         up(&mapping->host->i_sem);
480
481 out_putf:
482         fput(file);
483 out:
484         return ret;
485 }
486
487 /*
488  * Various filesystems appear to want __find_get_block to be non-blocking.
489  * But it's the page lock which protects the buffers.  To get around this,
490  * we get exclusion from try_to_free_buffers with the blockdev mapping's
491  * private_lock.
492  *
493  * Hack idea: for the blockdev mapping, i_bufferlist_lock contention
494  * may be quite high.  This code could TryLock the page, and if that
495  * succeeds, there is no need to take private_lock. (But if
496  * private_lock is contended then so is mapping->tree_lock).
497  */
498 static struct buffer_head *
499 __find_get_block_slow(struct block_device *bdev, sector_t block, int unused)
500 {
501         struct inode *bd_inode = bdev->bd_inode;
502         struct address_space *bd_mapping = bd_inode->i_mapping;
503         struct buffer_head *ret = NULL;
504         pgoff_t index;
505         struct buffer_head *bh;
506         struct buffer_head *head;
507         struct page *page;
508
509         index = block >> (PAGE_CACHE_SHIFT - bd_inode->i_blkbits);
510         page = find_get_page(bd_mapping, index);
511         if (!page)
512                 goto out;
513
514         spin_lock(&bd_mapping->private_lock);
515         if (!page_has_buffers(page))
516                 goto out_unlock;
517         head = page_buffers(page);
518         bh = head;
519         do {
520                 if (bh->b_blocknr == block) {
521                         ret = bh;
522                         get_bh(bh);
523                         goto out_unlock;
524                 }
525                 bh = bh->b_this_page;
526         } while (bh != head);
527
528         printk("__find_get_block_slow() failed. "
529                 "block=%llu, b_blocknr=%llu\n",
530                 (unsigned long long)block, (unsigned long long)bh->b_blocknr);
531         printk("b_state=0x%08lx, b_size=%u\n", bh->b_state, bh->b_size);
532         printk("device blocksize: %d\n", 1 << bd_inode->i_blkbits);
533 out_unlock:
534         spin_unlock(&bd_mapping->private_lock);
535         page_cache_release(page);
536 out:
537         return ret;
538 }
539
540 /* If invalidate_buffers() will trash dirty buffers, it means some kind
541    of fs corruption is going on. Trashing dirty data always imply losing
542    information that was supposed to be just stored on the physical layer
543    by the user.
544
545    Thus invalidate_buffers in general usage is not allwowed to trash
546    dirty buffers. For example ioctl(FLSBLKBUF) expects dirty data to
547    be preserved.  These buffers are simply skipped.
548   
549    We also skip buffers which are still in use.  For example this can
550    happen if a userspace program is reading the block device.
551
552    NOTE: In the case where the user removed a removable-media-disk even if
553    there's still dirty data not synced on disk (due a bug in the device driver
554    or due an error of the user), by not destroying the dirty buffers we could
555    generate corruption also on the next media inserted, thus a parameter is
556    necessary to handle this case in the most safe way possible (trying
557    to not corrupt also the new disk inserted with the data belonging to
558    the old now corrupted disk). Also for the ramdisk the natural thing
559    to do in order to release the ramdisk memory is to destroy dirty buffers.
560
561    These are two special cases. Normal usage imply the device driver
562    to issue a sync on the device (without waiting I/O completion) and
563    then an invalidate_buffers call that doesn't trash dirty buffers.
564
565    For handling cache coherency with the blkdev pagecache the 'update' case
566    is been introduced. It is needed to re-read from disk any pinned
567    buffer. NOTE: re-reading from disk is destructive so we can do it only
568    when we assume nobody is changing the buffercache under our I/O and when
569    we think the disk contains more recent information than the buffercache.
570    The update == 1 pass marks the buffers we need to update, the update == 2
571    pass does the actual I/O. */
572 void invalidate_bdev(struct block_device *bdev, int destroy_dirty_buffers)
573 {
574         invalidate_bh_lrus();
575         /*
576          * FIXME: what about destroy_dirty_buffers?
577          * We really want to use invalidate_inode_pages2() for
578          * that, but not until that's cleaned up.
579          */
580         invalidate_inode_pages(bdev->bd_inode->i_mapping);
581 }
582
583 /*
584  * Kick pdflush then try to free up some ZONE_NORMAL memory.
585  */
586 static void free_more_memory(void)
587 {
588         struct zone **zones;
589         pg_data_t *pgdat;
590
591         wakeup_bdflush(1024);
592         yield();
593
594         for_each_pgdat(pgdat) {
595                 zones = pgdat->node_zonelists[GFP_NOFS&GFP_ZONEMASK].zones;
596                 if (*zones)
597                         try_to_free_pages(zones, GFP_NOFS, 0);
598         }
599 }
600
601 /*
602  * I/O completion handler for block_read_full_page() - pages
603  * which come unlocked at the end of I/O.
604  */
605 static void end_buffer_async_read(struct buffer_head *bh, int uptodate)
606 {
607         static spinlock_t page_uptodate_lock = SPIN_LOCK_UNLOCKED;
608         unsigned long flags;
609         struct buffer_head *tmp;
610         struct page *page;
611         int page_uptodate = 1;
612
613         BUG_ON(!buffer_async_read(bh));
614
615         page = bh->b_page;
616         if (uptodate) {
617                 set_buffer_uptodate(bh);
618         } else {
619                 clear_buffer_uptodate(bh);
620                 buffer_io_error(bh);
621                 SetPageError(page);
622         }
623
624         /*
625          * Be _very_ careful from here on. Bad things can happen if
626          * two buffer heads end IO at almost the same time and both
627          * decide that the page is now completely done.
628          */
629         spin_lock_irqsave(&page_uptodate_lock, flags);
630         clear_buffer_async_read(bh);
631         unlock_buffer(bh);
632         tmp = bh;
633         do {
634                 if (!buffer_uptodate(tmp))
635                         page_uptodate = 0;
636                 if (buffer_async_read(tmp)) {
637                         BUG_ON(!buffer_locked(tmp));
638                         goto still_busy;
639                 }
640                 tmp = tmp->b_this_page;
641         } while (tmp != bh);
642         spin_unlock_irqrestore(&page_uptodate_lock, flags);
643
644         /*
645          * If none of the buffers had errors and they are all
646          * uptodate then we can set the page uptodate.
647          */
648         if (page_uptodate && !PageError(page))
649                 SetPageUptodate(page);
650         unlock_page(page);
651         return;
652
653 still_busy:
654         spin_unlock_irqrestore(&page_uptodate_lock, flags);
655         return;
656 }
657
658 /*
659  * Completion handler for block_write_full_page() - pages which are unlocked
660  * during I/O, and which have PageWriteback cleared upon I/O completion.
661  */
662 void end_buffer_async_write(struct buffer_head *bh, int uptodate)
663 {
664         char b[BDEVNAME_SIZE];
665         static spinlock_t page_uptodate_lock = SPIN_LOCK_UNLOCKED;
666         unsigned long flags;
667         struct buffer_head *tmp;
668         struct page *page;
669
670         BUG_ON(!buffer_async_write(bh));
671
672         page = bh->b_page;
673         if (uptodate) {
674                 set_buffer_uptodate(bh);
675         } else {
676                 if (printk_ratelimit()) {
677                         buffer_io_error(bh);
678                         printk(KERN_WARNING "lost page write due to "
679                                         "I/O error on %s\n",
680                                bdevname(bh->b_bdev, b));
681                 }
682                 set_bit(AS_EIO, &page->mapping->flags);
683                 clear_buffer_uptodate(bh);
684                 SetPageError(page);
685         }
686
687         spin_lock_irqsave(&page_uptodate_lock, flags);
688         clear_buffer_async_write(bh);
689         unlock_buffer(bh);
690         tmp = bh->b_this_page;
691         while (tmp != bh) {
692                 if (buffer_async_write(tmp)) {
693                         BUG_ON(!buffer_locked(tmp));
694                         goto still_busy;
695                 }
696                 tmp = tmp->b_this_page;
697         }
698         spin_unlock_irqrestore(&page_uptodate_lock, flags);
699         end_page_writeback(page);
700         return;
701
702 still_busy:
703         spin_unlock_irqrestore(&page_uptodate_lock, flags);
704         return;
705 }
706
707 /*
708  * If a page's buffers are under async readin (end_buffer_async_read
709  * completion) then there is a possibility that another thread of
710  * control could lock one of the buffers after it has completed
711  * but while some of the other buffers have not completed.  This
712  * locked buffer would confuse end_buffer_async_read() into not unlocking
713  * the page.  So the absence of BH_Async_Read tells end_buffer_async_read()
714  * that this buffer is not under async I/O.
715  *
716  * The page comes unlocked when it has no locked buffer_async buffers
717  * left.
718  *
719  * PageLocked prevents anyone starting new async I/O reads any of
720  * the buffers.
721  *
722  * PageWriteback is used to prevent simultaneous writeout of the same
723  * page.
724  *
725  * PageLocked prevents anyone from starting writeback of a page which is
726  * under read I/O (PageWriteback is only ever set against a locked page).
727  */
728 void mark_buffer_async_read(struct buffer_head *bh)
729 {
730         bh->b_end_io = end_buffer_async_read;
731         set_buffer_async_read(bh);
732 }
733 EXPORT_SYMBOL(mark_buffer_async_read);
734
735 void mark_buffer_async_write(struct buffer_head *bh)
736 {
737         bh->b_end_io = end_buffer_async_write;
738         set_buffer_async_write(bh);
739 }
740 EXPORT_SYMBOL(mark_buffer_async_write);
741
742
743 /*
744  * fs/buffer.c contains helper functions for buffer-backed address space's
745  * fsync functions.  A common requirement for buffer-based filesystems is
746  * that certain data from the backing blockdev needs to be written out for
747  * a successful fsync().  For example, ext2 indirect blocks need to be
748  * written back and waited upon before fsync() returns.
749  *
750  * The functions mark_buffer_inode_dirty(), fsync_inode_buffers(),
751  * inode_has_buffers() and invalidate_inode_buffers() are provided for the
752  * management of a list of dependent buffers at ->i_mapping->private_list.
753  *
754  * Locking is a little subtle: try_to_free_buffers() will remove buffers
755  * from their controlling inode's queue when they are being freed.  But
756  * try_to_free_buffers() will be operating against the *blockdev* mapping
757  * at the time, not against the S_ISREG file which depends on those buffers.
758  * So the locking for private_list is via the private_lock in the address_space
759  * which backs the buffers.  Which is different from the address_space 
760  * against which the buffers are listed.  So for a particular address_space,
761  * mapping->private_lock does *not* protect mapping->private_list!  In fact,
762  * mapping->private_list will always be protected by the backing blockdev's
763  * ->private_lock.
764  *
765  * Which introduces a requirement: all buffers on an address_space's
766  * ->private_list must be from the same address_space: the blockdev's.
767  *
768  * address_spaces which do not place buffers at ->private_list via these
769  * utility functions are free to use private_lock and private_list for
770  * whatever they want.  The only requirement is that list_empty(private_list)
771  * be true at clear_inode() time.
772  *
773  * FIXME: clear_inode should not call invalidate_inode_buffers().  The
774  * filesystems should do that.  invalidate_inode_buffers() should just go
775  * BUG_ON(!list_empty).
776  *
777  * FIXME: mark_buffer_dirty_inode() is a data-plane operation.  It should
778  * take an address_space, not an inode.  And it should be called
779  * mark_buffer_dirty_fsync() to clearly define why those buffers are being
780  * queued up.
781  *
782  * FIXME: mark_buffer_dirty_inode() doesn't need to add the buffer to the
783  * list if it is already on a list.  Because if the buffer is on a list,
784  * it *must* already be on the right one.  If not, the filesystem is being
785  * silly.  This will save a ton of locking.  But first we have to ensure
786  * that buffers are taken *off* the old inode's list when they are freed
787  * (presumably in truncate).  That requires careful auditing of all
788  * filesystems (do it inside bforget()).  It could also be done by bringing
789  * b_inode back.
790  */
791
792 void buffer_insert_list(spinlock_t *lock,
793                 struct buffer_head *bh, struct list_head *list)
794 {
795         spin_lock(lock);
796         list_move_tail(&bh->b_assoc_buffers, list);
797         spin_unlock(lock);
798 }
799
800 /*
801  * The buffer's backing address_space's private_lock must be held
802  */
803 static inline void __remove_assoc_queue(struct buffer_head *bh)
804 {
805         list_del_init(&bh->b_assoc_buffers);
806 }
807
808 int inode_has_buffers(struct inode *inode)
809 {
810         return !list_empty(&inode->i_data.private_list);
811 }
812
813 /*
814  * osync is designed to support O_SYNC io.  It waits synchronously for
815  * all already-submitted IO to complete, but does not queue any new
816  * writes to the disk.
817  *
818  * To do O_SYNC writes, just queue the buffer writes with ll_rw_block as
819  * you dirty the buffers, and then use osync_inode_buffers to wait for
820  * completion.  Any other dirty buffers which are not yet queued for
821  * write will not be flushed to disk by the osync.
822  */
823 static int osync_buffers_list(spinlock_t *lock, struct list_head *list)
824 {
825         struct buffer_head *bh;
826         struct list_head *p;
827         int err = 0;
828
829         spin_lock(lock);
830 repeat:
831         list_for_each_prev(p, list) {
832                 bh = BH_ENTRY(p);
833                 if (buffer_locked(bh)) {
834                         get_bh(bh);
835                         spin_unlock(lock);
836                         wait_on_buffer(bh);
837                         if (!buffer_uptodate(bh))
838                                 err = -EIO;
839                         brelse(bh);
840                         spin_lock(lock);
841                         goto repeat;
842                 }
843         }
844         spin_unlock(lock);
845         return err;
846 }
847
848 /**
849  * sync_mapping_buffers - write out and wait upon a mapping's "associated"
850  *                        buffers
851  * @buffer_mapping - the mapping which backs the buffers' data
852  * @mapping - the mapping which wants those buffers written
853  *
854  * Starts I/O against the buffers at mapping->private_list, and waits upon
855  * that I/O.
856  *
857  * Basically, this is a convenience function for fsync().  @buffer_mapping is
858  * the blockdev which "owns" the buffers and @mapping is a file or directory
859  * which needs those buffers to be written for a successful fsync().
860  */
861 int sync_mapping_buffers(struct address_space *mapping)
862 {
863         struct address_space *buffer_mapping = mapping->assoc_mapping;
864
865         if (buffer_mapping == NULL || list_empty(&mapping->private_list))
866                 return 0;
867
868         return fsync_buffers_list(&buffer_mapping->private_lock,
869                                         &mapping->private_list);
870 }
871 EXPORT_SYMBOL(sync_mapping_buffers);
872
873 /*
874  * Called when we've recently written block `bblock', and it is known that
875  * `bblock' was for a buffer_boundary() buffer.  This means that the block at
876  * `bblock + 1' is probably a dirty indirect block.  Hunt it down and, if it's
877  * dirty, schedule it for IO.  So that indirects merge nicely with their data.
878  */
879 void write_boundary_block(struct block_device *bdev,
880                         sector_t bblock, unsigned blocksize)
881 {
882         struct buffer_head *bh = __find_get_block(bdev, bblock + 1, blocksize);
883         if (bh) {
884                 if (buffer_dirty(bh))
885                         ll_rw_block(WRITE, 1, &bh);
886                 put_bh(bh);
887         }
888 }
889
890 void mark_buffer_dirty_inode(struct buffer_head *bh, struct inode *inode)
891 {
892         struct address_space *mapping = inode->i_mapping;
893         struct address_space *buffer_mapping = bh->b_page->mapping;
894
895         mark_buffer_dirty(bh);
896         if (!mapping->assoc_mapping) {
897                 mapping->assoc_mapping = buffer_mapping;
898         } else {
899                 if (mapping->assoc_mapping != buffer_mapping)
900                         BUG();
901         }
902         if (list_empty(&bh->b_assoc_buffers))
903                 buffer_insert_list(&buffer_mapping->private_lock,
904                                 bh, &mapping->private_list);
905 }
906 EXPORT_SYMBOL(mark_buffer_dirty_inode);
907
908 /*
909  * Add a page to the dirty page list.
910  *
911  * It is a sad fact of life that this function is called from several places
912  * deeply under spinlocking.  It may not sleep.
913  *
914  * If the page has buffers, the uptodate buffers are set dirty, to preserve
915  * dirty-state coherency between the page and the buffers.  It the page does
916  * not have buffers then when they are later attached they will all be set
917  * dirty.
918  *
919  * The buffers are dirtied before the page is dirtied.  There's a small race
920  * window in which a writepage caller may see the page cleanness but not the
921  * buffer dirtiness.  That's fine.  If this code were to set the page dirty
922  * before the buffers, a concurrent writepage caller could clear the page dirty
923  * bit, see a bunch of clean buffers and we'd end up with dirty buffers/clean
924  * page on the dirty page list.
925  *
926  * We use private_lock to lock against try_to_free_buffers while using the
927  * page's buffer list.  Also use this to protect against clean buffers being
928  * added to the page after it was set dirty.
929  *
930  * FIXME: may need to call ->reservepage here as well.  That's rather up to the
931  * address_space though.
932  */
933 int __set_page_dirty_buffers(struct page *page)
934 {
935         struct address_space * const mapping = page->mapping;
936
937         spin_lock(&mapping->private_lock);
938         if (page_has_buffers(page)) {
939                 struct buffer_head *head = page_buffers(page);
940                 struct buffer_head *bh = head;
941
942                 do {
943                         set_buffer_dirty(bh);
944                         bh = bh->b_this_page;
945                 } while (bh != head);
946         }
947         spin_unlock(&mapping->private_lock);
948
949         if (!TestSetPageDirty(page)) {
950                 spin_lock_irq(&mapping->tree_lock);
951                 if (page->mapping) {    /* Race with truncate? */
952                         if (!mapping->backing_dev_info->memory_backed)
953                                 inc_page_state(nr_dirty);
954                         radix_tree_tag_set(&mapping->page_tree,
955                                                 page_index(page),
956                                                 PAGECACHE_TAG_DIRTY);
957                 }
958                 spin_unlock_irq(&mapping->tree_lock);
959                 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
960         }
961         
962         return 0;
963 }
964 EXPORT_SYMBOL(__set_page_dirty_buffers);
965
966 /*
967  * Write out and wait upon a list of buffers.
968  *
969  * We have conflicting pressures: we want to make sure that all
970  * initially dirty buffers get waited on, but that any subsequently
971  * dirtied buffers don't.  After all, we don't want fsync to last
972  * forever if somebody is actively writing to the file.
973  *
974  * Do this in two main stages: first we copy dirty buffers to a
975  * temporary inode list, queueing the writes as we go.  Then we clean
976  * up, waiting for those writes to complete.
977  * 
978  * During this second stage, any subsequent updates to the file may end
979  * up refiling the buffer on the original inode's dirty list again, so
980  * there is a chance we will end up with a buffer queued for write but
981  * not yet completed on that list.  So, as a final cleanup we go through
982  * the osync code to catch these locked, dirty buffers without requeuing
983  * any newly dirty buffers for write.
984  */
985 int fsync_buffers_list(spinlock_t *lock, struct list_head *list)
986 {
987         struct buffer_head *bh;
988         struct list_head tmp;
989         int err = 0, err2;
990
991         INIT_LIST_HEAD(&tmp);
992
993         spin_lock(lock);
994         while (!list_empty(list)) {
995                 bh = BH_ENTRY(list->next);
996                 list_del_init(&bh->b_assoc_buffers);
997                 if (buffer_dirty(bh) || buffer_locked(bh)) {
998                         list_add(&bh->b_assoc_buffers, &tmp);
999                         if (buffer_dirty(bh)) {
1000                                 get_bh(bh);
1001                                 spin_unlock(lock);
1002                                 /*
1003                                  * Ensure any pending I/O completes so that
1004                                  * ll_rw_block() actually writes the current
1005                                  * contents - it is a noop if I/O is still in
1006                                  * flight on potentially older contents.
1007                                  */
1008                                 wait_on_buffer(bh);
1009                                 ll_rw_block(WRITE, 1, &bh);
1010                                 brelse(bh);
1011                                 spin_lock(lock);
1012                         }
1013                 }
1014         }
1015
1016         while (!list_empty(&tmp)) {
1017                 bh = BH_ENTRY(tmp.prev);
1018                 __remove_assoc_queue(bh);
1019                 get_bh(bh);
1020                 spin_unlock(lock);
1021                 wait_on_buffer(bh);
1022                 if (!buffer_uptodate(bh))
1023                         err = -EIO;
1024                 brelse(bh);
1025                 spin_lock(lock);
1026         }
1027         
1028         spin_unlock(lock);
1029         err2 = osync_buffers_list(lock, list);
1030         if (err)
1031                 return err;
1032         else
1033                 return err2;
1034 }
1035
1036 /*
1037  * Invalidate any and all dirty buffers on a given inode.  We are
1038  * probably unmounting the fs, but that doesn't mean we have already
1039  * done a sync().  Just drop the buffers from the inode list.
1040  *
1041  * NOTE: we take the inode's blockdev's mapping's private_lock.  Which
1042  * assumes that all the buffers are against the blockdev.  Not true
1043  * for reiserfs.
1044  */
1045 void invalidate_inode_buffers(struct inode *inode)
1046 {
1047         if (inode_has_buffers(inode)) {
1048                 struct address_space *mapping = &inode->i_data;
1049                 struct list_head *list = &mapping->private_list;
1050                 struct address_space *buffer_mapping = mapping->assoc_mapping;
1051
1052                 spin_lock(&buffer_mapping->private_lock);
1053                 while (!list_empty(list))
1054                         __remove_assoc_queue(BH_ENTRY(list->next));
1055                 spin_unlock(&buffer_mapping->private_lock);
1056         }
1057 }
1058
1059 /*
1060  * Remove any clean buffers from the inode's buffer list.  This is called
1061  * when we're trying to free the inode itself.  Those buffers can pin it.
1062  *
1063  * Returns true if all buffers were removed.
1064  */
1065 int remove_inode_buffers(struct inode *inode)
1066 {
1067         int ret = 1;
1068
1069         if (inode_has_buffers(inode)) {
1070                 struct address_space *mapping = &inode->i_data;
1071                 struct list_head *list = &mapping->private_list;
1072                 struct address_space *buffer_mapping = mapping->assoc_mapping;
1073
1074                 spin_lock(&buffer_mapping->private_lock);
1075                 while (!list_empty(list)) {
1076                         struct buffer_head *bh = BH_ENTRY(list->next);
1077                         if (buffer_dirty(bh)) {
1078                                 ret = 0;
1079                                 break;
1080                         }
1081                         __remove_assoc_queue(bh);
1082                 }
1083                 spin_unlock(&buffer_mapping->private_lock);
1084         }
1085         return ret;
1086 }
1087
1088 /*
1089  * Create the appropriate buffers when given a page for data area and
1090  * the size of each buffer.. Use the bh->b_this_page linked list to
1091  * follow the buffers created.  Return NULL if unable to create more
1092  * buffers.
1093  *
1094  * The retry flag is used to differentiate async IO (paging, swapping)
1095  * which may not fail from ordinary buffer allocations.
1096  */
1097 static struct buffer_head *
1098 create_buffers(struct page * page, unsigned long size, int retry)
1099 {
1100         struct buffer_head *bh, *head;
1101         long offset;
1102
1103 try_again:
1104         head = NULL;
1105         offset = PAGE_SIZE;
1106         while ((offset -= size) >= 0) {
1107                 bh = alloc_buffer_head(GFP_NOFS);
1108                 if (!bh)
1109                         goto no_grow;
1110
1111                 bh->b_bdev = NULL;
1112                 bh->b_this_page = head;
1113                 bh->b_blocknr = -1;
1114                 head = bh;
1115
1116                 bh->b_state = 0;
1117                 atomic_set(&bh->b_count, 0);
1118                 bh->b_size = size;
1119
1120                 /* Link the buffer to its page */
1121                 set_bh_page(bh, page, offset);
1122
1123                 bh->b_end_io = NULL;
1124         }
1125         return head;
1126 /*
1127  * In case anything failed, we just free everything we got.
1128  */
1129 no_grow:
1130         if (head) {
1131                 do {
1132                         bh = head;
1133                         head = head->b_this_page;
1134                         free_buffer_head(bh);
1135                 } while (head);
1136         }
1137
1138         /*
1139          * Return failure for non-async IO requests.  Async IO requests
1140          * are not allowed to fail, so we have to wait until buffer heads
1141          * become available.  But we don't want tasks sleeping with 
1142          * partially complete buffers, so all were released above.
1143          */
1144         if (!retry)
1145                 return NULL;
1146
1147         /* We're _really_ low on memory. Now we just
1148          * wait for old buffer heads to become free due to
1149          * finishing IO.  Since this is an async request and
1150          * the reserve list is empty, we're sure there are 
1151          * async buffer heads in use.
1152          */
1153         free_more_memory();
1154         goto try_again;
1155 }
1156
1157 static inline void
1158 link_dev_buffers(struct page *page, struct buffer_head *head)
1159 {
1160         struct buffer_head *bh, *tail;
1161
1162         bh = head;
1163         do {
1164                 tail = bh;
1165                 bh = bh->b_this_page;
1166         } while (bh);
1167         tail->b_this_page = head;
1168         __set_page_buffers(page, head);
1169 }
1170
1171 /*
1172  * Initialise the state of a blockdev page's buffers.
1173  */ 
1174 static void
1175 init_page_buffers(struct page *page, struct block_device *bdev,
1176                         sector_t block, int size)
1177 {
1178         struct buffer_head *head = page_buffers(page);
1179         struct buffer_head *bh = head;
1180         unsigned int b_state;
1181
1182         b_state = 1 << BH_Mapped;
1183         if (PageUptodate(page))
1184                 b_state |= 1 << BH_Uptodate;
1185
1186         do {
1187                 if (!(bh->b_state & (1 << BH_Mapped))) {
1188                         init_buffer(bh, NULL, NULL);
1189                         bh->b_bdev = bdev;
1190                         bh->b_blocknr = block;
1191                         bh->b_state = b_state;
1192                 }
1193                 block++;
1194                 bh = bh->b_this_page;
1195         } while (bh != head);
1196 }
1197
1198 /*
1199  * Create the page-cache page that contains the requested block.
1200  *
1201  * This is user purely for blockdev mappings.
1202  */
1203 static struct page *
1204 grow_dev_page(struct block_device *bdev, sector_t block,
1205                 pgoff_t index, int size)
1206 {
1207         struct inode *inode = bdev->bd_inode;
1208         struct page *page;
1209         struct buffer_head *bh;
1210
1211         page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
1212         if (!page)
1213                 return NULL;
1214
1215         if (!PageLocked(page))
1216                 BUG();
1217
1218         if (page_has_buffers(page)) {
1219                 bh = page_buffers(page);
1220                 if (bh->b_size == size)
1221                         return page;
1222                 if (!try_to_free_buffers(page))
1223                         goto failed;
1224         }
1225
1226         /*
1227          * Allocate some buffers for this page
1228          */
1229         bh = create_buffers(page, size, 0);
1230         if (!bh)
1231                 goto failed;
1232
1233         /*
1234          * Link the page to the buffers and initialise them.  Take the
1235          * lock to be atomic wrt __find_get_block(), which does not
1236          * run under the page lock.
1237          */
1238         spin_lock(&inode->i_mapping->private_lock);
1239         link_dev_buffers(page, bh);
1240         init_page_buffers(page, bdev, block, size);
1241         spin_unlock(&inode->i_mapping->private_lock);
1242         return page;
1243
1244 failed:
1245         BUG();
1246         unlock_page(page);
1247         page_cache_release(page);
1248         return NULL;
1249 }
1250
1251 /*
1252  * Create buffers for the specified block device block's page.  If
1253  * that page was dirty, the buffers are set dirty also.
1254  *
1255  * Except that's a bug.  Attaching dirty buffers to a dirty
1256  * blockdev's page can result in filesystem corruption, because
1257  * some of those buffers may be aliases of filesystem data.
1258  * grow_dev_page() will go BUG() if this happens.
1259  */
1260 static inline int
1261 grow_buffers(struct block_device *bdev, sector_t block, int size)
1262 {
1263         struct page *page;
1264         pgoff_t index;
1265         int sizebits;
1266
1267         sizebits = -1;
1268         do {
1269                 sizebits++;
1270         } while ((size << sizebits) < PAGE_SIZE);
1271
1272         index = block >> sizebits;
1273         block = index << sizebits;
1274
1275         /* Create a page with the proper size buffers.. */
1276         page = grow_dev_page(bdev, block, index, size);
1277         if (!page)
1278                 return 0;
1279         unlock_page(page);
1280         page_cache_release(page);
1281         return 1;
1282 }
1283
1284 struct buffer_head *
1285 __getblk_slow(struct block_device *bdev, sector_t block, int size)
1286 {
1287         /* Size must be multiple of hard sectorsize */
1288         if (unlikely(size & (bdev_hardsect_size(bdev)-1) ||
1289                         (size < 512 || size > PAGE_SIZE))) {
1290                 printk(KERN_ERR "getblk(): invalid block size %d requested\n",
1291                                         size);
1292                 printk(KERN_ERR "hardsect size: %d\n",
1293                                         bdev_hardsect_size(bdev));
1294
1295                 dump_stack();
1296                 return NULL;
1297         }
1298
1299         for (;;) {
1300                 struct buffer_head * bh;
1301
1302                 bh = __find_get_block(bdev, block, size);
1303                 if (bh)
1304                         return bh;
1305
1306                 if (!grow_buffers(bdev, block, size))
1307                         free_more_memory();
1308         }
1309 }
1310
1311 /*
1312  * The relationship between dirty buffers and dirty pages:
1313  *
1314  * Whenever a page has any dirty buffers, the page's dirty bit is set, and
1315  * the page is tagged dirty in its radix tree.
1316  *
1317  * At all times, the dirtiness of the buffers represents the dirtiness of
1318  * subsections of the page.  If the page has buffers, the page dirty bit is
1319  * merely a hint about the true dirty state.
1320  *
1321  * When a page is set dirty in its entirety, all its buffers are marked dirty
1322  * (if the page has buffers).
1323  *
1324  * When a buffer is marked dirty, its page is dirtied, but the page's other
1325  * buffers are not.
1326  *
1327  * Also.  When blockdev buffers are explicitly read with bread(), they
1328  * individually become uptodate.  But their backing page remains not
1329  * uptodate - even if all of its buffers are uptodate.  A subsequent
1330  * block_read_full_page() against that page will discover all the uptodate
1331  * buffers, will set the page uptodate and will perform no I/O.
1332  */
1333
1334 /**
1335  * mark_buffer_dirty - mark a buffer_head as needing writeout
1336  *
1337  * mark_buffer_dirty() will set the dirty bit against the buffer, then set its
1338  * backing page dirty, then tag the page as dirty in its address_space's radix
1339  * tree and then attach the address_space's inode to its superblock's dirty
1340  * inode list.
1341  *
1342  * mark_buffer_dirty() is atomic.  It takes bh->b_page->mapping->private_lock,
1343  * mapping->tree_lock and the global inode_lock.
1344  */
1345 void fastcall mark_buffer_dirty(struct buffer_head *bh)
1346 {
1347         if (!buffer_dirty(bh) && !test_set_buffer_dirty(bh))
1348                 __set_page_dirty_nobuffers(bh->b_page);
1349 }
1350
1351 /*
1352  * Decrement a buffer_head's reference count.  If all buffers against a page
1353  * have zero reference count, are clean and unlocked, and if the page is clean
1354  * and unlocked then try_to_free_buffers() may strip the buffers from the page
1355  * in preparation for freeing it (sometimes, rarely, buffers are removed from
1356  * a page but it ends up not being freed, and buffers may later be reattached).
1357  */
1358 void __brelse(struct buffer_head * buf)
1359 {
1360         if (atomic_read(&buf->b_count)) {
1361                 put_bh(buf);
1362                 return;
1363         }
1364         printk(KERN_ERR "VFS: brelse: Trying to free free buffer\n");
1365         WARN_ON(1);
1366 }
1367
1368 /*
1369  * bforget() is like brelse(), except it discards any
1370  * potentially dirty data.
1371  */
1372 void __bforget(struct buffer_head *bh)
1373 {
1374         clear_buffer_dirty(bh);
1375         if (!list_empty(&bh->b_assoc_buffers)) {
1376                 struct address_space *buffer_mapping = bh->b_page->mapping;
1377
1378                 spin_lock(&buffer_mapping->private_lock);
1379                 list_del_init(&bh->b_assoc_buffers);
1380                 spin_unlock(&buffer_mapping->private_lock);
1381         }
1382         __brelse(bh);
1383 }
1384
1385 static struct buffer_head *__bread_slow(struct buffer_head *bh)
1386 {
1387         lock_buffer(bh);
1388         if (buffer_uptodate(bh)) {
1389                 unlock_buffer(bh);
1390                 return bh;
1391         } else {
1392                 get_bh(bh);
1393                 bh->b_end_io = end_buffer_read_sync;
1394                 submit_bh(READ, bh);
1395                 wait_on_buffer(bh);
1396                 if (buffer_uptodate(bh))
1397                         return bh;
1398         }
1399         brelse(bh);
1400         return NULL;
1401 }
1402
1403 /*
1404  * Per-cpu buffer LRU implementation.  To reduce the cost of __find_get_block().
1405  * The bhs[] array is sorted - newest buffer is at bhs[0].  Buffers have their
1406  * refcount elevated by one when they're in an LRU.  A buffer can only appear
1407  * once in a particular CPU's LRU.  A single buffer can be present in multiple
1408  * CPU's LRUs at the same time.
1409  *
1410  * This is a transparent caching front-end to sb_bread(), sb_getblk() and
1411  * sb_find_get_block().
1412  *
1413  * The LRUs themselves only need locking against invalidate_bh_lrus.  We use
1414  * a local interrupt disable for that.
1415  */
1416
1417 #define BH_LRU_SIZE     8
1418
1419 struct bh_lru {
1420         struct buffer_head *bhs[BH_LRU_SIZE];
1421 };
1422
1423 static DEFINE_PER_CPU(struct bh_lru, bh_lrus) = {{ NULL }};
1424
1425 #ifdef CONFIG_SMP
1426 #define bh_lru_lock()   local_irq_disable()
1427 #define bh_lru_unlock() local_irq_enable()
1428 #else
1429 #define bh_lru_lock()   preempt_disable()
1430 #define bh_lru_unlock() preempt_enable()
1431 #endif
1432
1433 static inline void check_irqs_on(void)
1434 {
1435 #ifdef irqs_disabled
1436         BUG_ON(irqs_disabled());
1437 #endif
1438 }
1439
1440 /*
1441  * The LRU management algorithm is dopey-but-simple.  Sorry.
1442  */
1443 static void bh_lru_install(struct buffer_head *bh)
1444 {
1445         struct buffer_head *evictee = NULL;
1446         struct bh_lru *lru;
1447
1448         check_irqs_on();
1449         bh_lru_lock();
1450         lru = &__get_cpu_var(bh_lrus);
1451         if (lru->bhs[0] != bh) {
1452                 struct buffer_head *bhs[BH_LRU_SIZE];
1453                 int in;
1454                 int out = 0;
1455
1456                 get_bh(bh);
1457                 bhs[out++] = bh;
1458                 for (in = 0; in < BH_LRU_SIZE; in++) {
1459                         struct buffer_head *bh2 = lru->bhs[in];
1460
1461                         if (bh2 == bh) {
1462                                 __brelse(bh2);
1463                         } else {
1464                                 if (out >= BH_LRU_SIZE) {
1465                                         BUG_ON(evictee != NULL);
1466                                         evictee = bh2;
1467                                 } else {
1468                                         bhs[out++] = bh2;
1469                                 }
1470                         }
1471                 }
1472                 while (out < BH_LRU_SIZE)
1473                         bhs[out++] = NULL;
1474                 memcpy(lru->bhs, bhs, sizeof(bhs));
1475         }
1476         bh_lru_unlock();
1477
1478         if (evictee)
1479                 __brelse(evictee);
1480 }
1481
1482 /*
1483  * Look up the bh in this cpu's LRU.  If it's there, move it to the head.
1484  */
1485 static inline struct buffer_head *
1486 lookup_bh_lru(struct block_device *bdev, sector_t block, int size)
1487 {
1488         struct buffer_head *ret = NULL;
1489         struct bh_lru *lru;
1490         int i;
1491
1492         check_irqs_on();
1493         bh_lru_lock();
1494         lru = &__get_cpu_var(bh_lrus);
1495         for (i = 0; i < BH_LRU_SIZE; i++) {
1496                 struct buffer_head *bh = lru->bhs[i];
1497
1498                 if (bh && bh->b_bdev == bdev &&
1499                                 bh->b_blocknr == block && bh->b_size == size) {
1500                         if (i) {
1501                                 while (i) {
1502                                         lru->bhs[i] = lru->bhs[i - 1];
1503                                         i--;
1504                                 }
1505                                 lru->bhs[0] = bh;
1506                         }
1507                         get_bh(bh);
1508                         ret = bh;
1509                         break;
1510                 }
1511         }
1512         bh_lru_unlock();
1513         return ret;
1514 }
1515
1516 /*
1517  * Perform a pagecache lookup for the matching buffer.  If it's there, refresh
1518  * it in the LRU and mark it as accessed.  If it is not present then return
1519  * NULL
1520  */
1521 struct buffer_head *
1522 __find_get_block(struct block_device *bdev, sector_t block, int size)
1523 {
1524         struct buffer_head *bh = lookup_bh_lru(bdev, block, size);
1525
1526         if (bh == NULL) {
1527                 bh = __find_get_block_slow(bdev, block, size);
1528                 if (bh)
1529                         bh_lru_install(bh);
1530         }
1531         if (bh)
1532                 touch_buffer(bh);
1533         return bh;
1534 }
1535 EXPORT_SYMBOL(__find_get_block);
1536
1537 /*
1538  * __getblk will locate (and, if necessary, create) the buffer_head
1539  * which corresponds to the passed block_device, block and size. The
1540  * returned buffer has its reference count incremented.
1541  *
1542  * __getblk() cannot fail - it just keeps trying.  If you pass it an
1543  * illegal block number, __getblk() will happily return a buffer_head
1544  * which represents the non-existent block.  Very weird.
1545  *
1546  * __getblk() will lock up the machine if grow_dev_page's try_to_free_buffers()
1547  * attempt is failing.  FIXME, perhaps?
1548  */
1549 struct buffer_head *
1550 __getblk(struct block_device *bdev, sector_t block, int size)
1551 {
1552         struct buffer_head *bh = __find_get_block(bdev, block, size);
1553
1554         might_sleep();
1555         if (bh == NULL)
1556                 bh = __getblk_slow(bdev, block, size);
1557         return bh;
1558 }
1559 EXPORT_SYMBOL(__getblk);
1560
1561 /*
1562  * Do async read-ahead on a buffer..
1563  */
1564 void __breadahead(struct block_device *bdev, sector_t block, int size)
1565 {
1566         struct buffer_head *bh = __getblk(bdev, block, size);
1567         ll_rw_block(READA, 1, &bh);
1568         brelse(bh);
1569 }
1570 EXPORT_SYMBOL(__breadahead);
1571
1572 /**
1573  *  __bread() - reads a specified block and returns the bh
1574  *  @block: number of block
1575  *  @size: size (in bytes) to read
1576  * 
1577  *  Reads a specified block, and returns buffer head that contains it.
1578  *  It returns NULL if the block was unreadable.
1579  */
1580 struct buffer_head *
1581 __bread(struct block_device *bdev, sector_t block, int size)
1582 {
1583         struct buffer_head *bh = __getblk(bdev, block, size);
1584
1585         if (!buffer_uptodate(bh))
1586                 bh = __bread_slow(bh);
1587         return bh;
1588 }
1589 EXPORT_SYMBOL(__bread);
1590
1591 /*
1592  * invalidate_bh_lrus() is called rarely - but not only at unmount.
1593  * This doesn't race because it runs in each cpu either in irq
1594  * or with preempt disabled.
1595  */
1596 static void invalidate_bh_lru(void *arg)
1597 {
1598         struct bh_lru *b = &get_cpu_var(bh_lrus);
1599         int i;
1600
1601         for (i = 0; i < BH_LRU_SIZE; i++) {
1602                 brelse(b->bhs[i]);
1603                 b->bhs[i] = NULL;
1604         }
1605         put_cpu_var(bh_lrus);
1606 }
1607         
1608 static void invalidate_bh_lrus(void)
1609 {
1610         on_each_cpu(invalidate_bh_lru, NULL, 1, 1);
1611 }
1612
1613 void set_bh_page(struct buffer_head *bh,
1614                 struct page *page, unsigned long offset)
1615 {
1616         bh->b_page = page;
1617         if (offset >= PAGE_SIZE)
1618                 BUG();
1619         if (PageHighMem(page))
1620                 /*
1621                  * This catches illegal uses and preserves the offset:
1622                  */
1623                 bh->b_data = (char *)(0 + offset);
1624         else
1625                 bh->b_data = page_address(page) + offset;
1626 }
1627 EXPORT_SYMBOL(set_bh_page);
1628
1629 /*
1630  * Called when truncating a buffer on a page completely.
1631  */
1632 static inline void discard_buffer(struct buffer_head * bh)
1633 {
1634         lock_buffer(bh);
1635         clear_buffer_dirty(bh);
1636         bh->b_bdev = NULL;
1637         clear_buffer_mapped(bh);
1638         clear_buffer_req(bh);
1639         clear_buffer_new(bh);
1640         clear_buffer_delay(bh);
1641         unlock_buffer(bh);
1642 }
1643
1644 /**
1645  * try_to_release_page() - release old fs-specific metadata on a page
1646  *
1647  * @page: the page which the kernel is trying to free
1648  * @gfp_mask: memory allocation flags (and I/O mode)
1649  *
1650  * The address_space is to try to release any data against the page
1651  * (presumably at page->private).  If the release was successful, return `1'.
1652  * Otherwise return zero.
1653  *
1654  * The @gfp_mask argument specifies whether I/O may be performed to release
1655  * this page (__GFP_IO), and whether the call may block (__GFP_WAIT).
1656  *
1657  * NOTE: @gfp_mask may go away, and this function may become non-blocking.
1658  */
1659 int try_to_release_page(struct page *page, int gfp_mask)
1660 {
1661         struct address_space * const mapping = page->mapping;
1662
1663         BUG_ON(!PageLocked(page));
1664         if (PageWriteback(page))
1665                 return 0;
1666         
1667         if (mapping && mapping->a_ops->releasepage)
1668                 return mapping->a_ops->releasepage(page, gfp_mask);
1669         return try_to_free_buffers(page);
1670 }
1671 EXPORT_SYMBOL(try_to_release_page);
1672
1673 /**
1674  * block_invalidatepage - invalidate part of all of a buffer-backed page
1675  *
1676  * @page: the page which is affected
1677  * @offset: the index of the truncation point
1678  *
1679  * block_invalidatepage() is called when all or part of the page has become
1680  * invalidatedby a truncate operation.
1681  *
1682  * block_invalidatepage() does not have to release all buffers, but it must
1683  * ensure that no dirty buffer is left outside @offset and that no I/O
1684  * is underway against any of the blocks which are outside the truncation
1685  * point.  Because the caller is about to free (and possibly reuse) those
1686  * blocks on-disk.
1687  */
1688 int block_invalidatepage(struct page *page, unsigned long offset)
1689 {
1690         struct buffer_head *head, *bh, *next;
1691         unsigned int curr_off = 0;
1692         int ret = 1;
1693
1694         BUG_ON(!PageLocked(page));
1695         if (!page_has_buffers(page))
1696                 goto out;
1697
1698         head = page_buffers(page);
1699         bh = head;
1700         do {
1701                 unsigned int next_off = curr_off + bh->b_size;
1702                 next = bh->b_this_page;
1703
1704                 /*
1705                  * is this block fully invalidated?
1706                  */
1707                 if (offset <= curr_off)
1708                         discard_buffer(bh);
1709                 curr_off = next_off;
1710                 bh = next;
1711         } while (bh != head);
1712
1713         /*
1714          * We release buffers only if the entire page is being invalidated.
1715          * The get_block cached value has been unconditionally invalidated,
1716          * so real IO is not possible anymore.
1717          */
1718         if (offset == 0)
1719                 ret = try_to_release_page(page, 0);
1720 out:
1721         return ret;
1722 }
1723 EXPORT_SYMBOL(block_invalidatepage);
1724
1725 /*
1726  * We attach and possibly dirty the buffers atomically wrt
1727  * __set_page_dirty_buffers() via private_lock.  try_to_free_buffers
1728  * is already excluded via the page lock.
1729  */
1730 void create_empty_buffers(struct page *page,
1731                         unsigned long blocksize, unsigned long b_state)
1732 {
1733         struct buffer_head *bh, *head, *tail;
1734
1735         head = create_buffers(page, blocksize, 1);
1736         bh = head;
1737         do {
1738                 bh->b_state |= b_state;
1739                 tail = bh;
1740                 bh = bh->b_this_page;
1741         } while (bh);
1742         tail->b_this_page = head;
1743
1744         spin_lock(&page->mapping->private_lock);
1745         if (PageUptodate(page) || PageDirty(page)) {
1746                 bh = head;
1747                 do {
1748                         if (PageDirty(page))
1749                                 set_buffer_dirty(bh);
1750                         if (PageUptodate(page))
1751                                 set_buffer_uptodate(bh);
1752                         bh = bh->b_this_page;
1753                 } while (bh != head);
1754         }
1755         __set_page_buffers(page, head);
1756         spin_unlock(&page->mapping->private_lock);
1757 }
1758 EXPORT_SYMBOL(create_empty_buffers);
1759
1760 /*
1761  * We are taking a block for data and we don't want any output from any
1762  * buffer-cache aliases starting from return from that function and
1763  * until the moment when something will explicitly mark the buffer
1764  * dirty (hopefully that will not happen until we will free that block ;-)
1765  * We don't even need to mark it not-uptodate - nobody can expect
1766  * anything from a newly allocated buffer anyway. We used to used
1767  * unmap_buffer() for such invalidation, but that was wrong. We definitely
1768  * don't want to mark the alias unmapped, for example - it would confuse
1769  * anyone who might pick it with bread() afterwards...
1770  *
1771  * Also..  Note that bforget() doesn't lock the buffer.  So there can
1772  * be writeout I/O going on against recently-freed buffers.  We don't
1773  * wait on that I/O in bforget() - it's more efficient to wait on the I/O
1774  * only if we really need to.  That happens here.
1775  */
1776 void unmap_underlying_metadata(struct block_device *bdev, sector_t block)
1777 {
1778         struct buffer_head *old_bh;
1779
1780         might_sleep();
1781
1782         old_bh = __find_get_block_slow(bdev, block, 0);
1783         if (old_bh) {
1784                 clear_buffer_dirty(old_bh);
1785                 wait_on_buffer(old_bh);
1786                 clear_buffer_req(old_bh);
1787                 __brelse(old_bh);
1788         }
1789 }
1790 EXPORT_SYMBOL(unmap_underlying_metadata);
1791
1792 /*
1793  * NOTE! All mapped/uptodate combinations are valid:
1794  *
1795  *      Mapped  Uptodate        Meaning
1796  *
1797  *      No      No              "unknown" - must do get_block()
1798  *      No      Yes             "hole" - zero-filled
1799  *      Yes     No              "allocated" - allocated on disk, not read in
1800  *      Yes     Yes             "valid" - allocated and up-to-date in memory.
1801  *
1802  * "Dirty" is valid only with the last case (mapped+uptodate).
1803  */
1804
1805 /*
1806  * While block_write_full_page is writing back the dirty buffers under
1807  * the page lock, whoever dirtied the buffers may decide to clean them
1808  * again at any time.  We handle that by only looking at the buffer
1809  * state inside lock_buffer().
1810  *
1811  * If block_write_full_page() is called for regular writeback
1812  * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
1813  * locked buffer.   This only can happen if someone has written the buffer
1814  * directly, with submit_bh().  At the address_space level PageWriteback
1815  * prevents this contention from occurring.
1816  */
1817 static int __block_write_full_page(struct inode *inode, struct page *page,
1818                         get_block_t *get_block, struct writeback_control *wbc)
1819 {
1820         int err;
1821         sector_t block;
1822         sector_t last_block;
1823         struct buffer_head *bh, *head;
1824         int nr_underway = 0;
1825
1826         BUG_ON(!PageLocked(page));
1827
1828         last_block = (i_size_read(inode) - 1) >> inode->i_blkbits;
1829
1830         if (!page_has_buffers(page)) {
1831                 create_empty_buffers(page, 1 << inode->i_blkbits,
1832                                         (1 << BH_Dirty)|(1 << BH_Uptodate));
1833         }
1834
1835         /*
1836          * Be very careful.  We have no exclusion from __set_page_dirty_buffers
1837          * here, and the (potentially unmapped) buffers may become dirty at
1838          * any time.  If a buffer becomes dirty here after we've inspected it
1839          * then we just miss that fact, and the page stays dirty.
1840          *
1841          * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
1842          * handle that here by just cleaning them.
1843          */
1844
1845         block = page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1846         head = page_buffers(page);
1847         bh = head;
1848
1849         /*
1850          * Get all the dirty buffers mapped to disk addresses and
1851          * handle any aliases from the underlying blockdev's mapping.
1852          */
1853         do {
1854                 if (block > last_block) {
1855                         /*
1856                          * mapped buffers outside i_size will occur, because
1857                          * this page can be outside i_size when there is a
1858                          * truncate in progress.
1859                          */
1860                         /*
1861                          * The buffer was zeroed by block_write_full_page()
1862                          */
1863                         clear_buffer_dirty(bh);
1864                         set_buffer_uptodate(bh);
1865                 } else if (!buffer_mapped(bh) && buffer_dirty(bh)) {
1866                         err = get_block(inode, block, bh, 1);
1867                         if (err)
1868                                 goto recover;
1869                         if (buffer_new(bh)) {
1870                                 /* blockdev mappings never come here */
1871                                 clear_buffer_new(bh);
1872                                 unmap_underlying_metadata(bh->b_bdev,
1873                                                         bh->b_blocknr);
1874                         }
1875                 }
1876                 bh = bh->b_this_page;
1877                 block++;
1878         } while (bh != head);
1879
1880         do {
1881                 get_bh(bh);
1882                 if (!buffer_mapped(bh))
1883                         continue;
1884                 /*
1885                  * If it's a fully non-blocking write attempt and we cannot
1886                  * lock the buffer then redirty the page.  Note that this can
1887                  * potentially cause a busy-wait loop from pdflush and kswapd
1888                  * activity, but those code paths have their own higher-level
1889                  * throttling.
1890                  */
1891                 if (wbc->sync_mode != WB_SYNC_NONE || !wbc->nonblocking) {
1892                         lock_buffer(bh);
1893                 } else if (test_set_buffer_locked(bh)) {
1894                         redirty_page_for_writepage(wbc, page);
1895                         continue;
1896                 }
1897                 if (test_clear_buffer_dirty(bh)) {
1898                         mark_buffer_async_write(bh);
1899                 } else {
1900                         unlock_buffer(bh);
1901                 }
1902         } while ((bh = bh->b_this_page) != head);
1903
1904         /*
1905          * The page and its buffers are protected by PageWriteback(), so we can
1906          * drop the bh refcounts early.
1907          */
1908         BUG_ON(PageWriteback(page));
1909         set_page_writeback(page);
1910         unlock_page(page);
1911
1912         do {
1913                 struct buffer_head *next = bh->b_this_page;
1914                 if (buffer_async_write(bh)) {
1915                         submit_bh(WRITE, bh);
1916                         nr_underway++;
1917                 }
1918                 put_bh(bh);
1919                 bh = next;
1920         } while (bh != head);
1921
1922         err = 0;
1923 done:
1924         if (nr_underway == 0) {
1925                 /*
1926                  * The page was marked dirty, but the buffers were
1927                  * clean.  Someone wrote them back by hand with
1928                  * ll_rw_block/submit_bh.  A rare case.
1929                  */
1930                 int uptodate = 1;
1931                 do {
1932                         if (!buffer_uptodate(bh)) {
1933                                 uptodate = 0;
1934                                 break;
1935                         }
1936                         bh = bh->b_this_page;
1937                 } while (bh != head);
1938                 if (uptodate)
1939                         SetPageUptodate(page);
1940                 end_page_writeback(page);
1941                 /*
1942                  * The page and buffer_heads can be released at any time from
1943                  * here on.
1944                  */
1945                 wbc->pages_skipped++;   /* We didn't write this page */
1946         }
1947         return err;
1948
1949 recover:
1950         /*
1951          * ENOSPC, or some other error.  We may already have added some
1952          * blocks to the file, so we need to write these out to avoid
1953          * exposing stale data.
1954          * The page is currently locked and not marked for writeback
1955          */
1956         bh = head;
1957         /* Recovery: lock and submit the mapped buffers */
1958         do {
1959                 get_bh(bh);
1960                 if (buffer_mapped(bh) && buffer_dirty(bh)) {
1961                         lock_buffer(bh);
1962                         mark_buffer_async_write(bh);
1963                 } else {
1964                         /*
1965                          * The buffer may have been set dirty during
1966                          * attachment to a dirty page.
1967                          */
1968                         clear_buffer_dirty(bh);
1969                 }
1970         } while ((bh = bh->b_this_page) != head);
1971         SetPageError(page);
1972         BUG_ON(PageWriteback(page));
1973         set_page_writeback(page);
1974         unlock_page(page);
1975         do {
1976                 struct buffer_head *next = bh->b_this_page;
1977                 if (buffer_async_write(bh)) {
1978                         clear_buffer_dirty(bh);
1979                         submit_bh(WRITE, bh);
1980                         nr_underway++;
1981                 }
1982                 put_bh(bh);
1983                 bh = next;
1984         } while (bh != head);
1985         goto done;
1986 }
1987
1988 static int __block_prepare_write(struct inode *inode, struct page *page,
1989                 unsigned from, unsigned to, get_block_t *get_block)
1990 {
1991         unsigned block_start, block_end;
1992         sector_t block;
1993         int err = 0;
1994         unsigned blocksize, bbits;
1995         struct buffer_head *bh, *head, *wait[2], **wait_bh=wait;
1996
1997         BUG_ON(!PageLocked(page));
1998         BUG_ON(from > PAGE_CACHE_SIZE);
1999         BUG_ON(to > PAGE_CACHE_SIZE);
2000         BUG_ON(from > to);
2001
2002         blocksize = 1 << inode->i_blkbits;
2003         if (!page_has_buffers(page))
2004                 create_empty_buffers(page, blocksize, 0);
2005         head = page_buffers(page);
2006
2007         bbits = inode->i_blkbits;
2008         block = (sector_t)page->index << (PAGE_CACHE_SHIFT - bbits);
2009
2010         for(bh = head, block_start = 0; bh != head || !block_start;
2011             block++, block_start=block_end, bh = bh->b_this_page) {
2012                 block_end = block_start + blocksize;
2013                 if (block_end <= from || block_start >= to) {
2014                         if (PageUptodate(page)) {
2015                                 if (!buffer_uptodate(bh))
2016                                         set_buffer_uptodate(bh);
2017                         }
2018                         continue;
2019                 }
2020                 if (buffer_new(bh))
2021                         clear_buffer_new(bh);
2022                 if (!buffer_mapped(bh)) {
2023                         err = get_block(inode, block, bh, 1);
2024                         if (err)
2025                                 goto out;
2026                         if (buffer_new(bh)) {
2027                                 clear_buffer_new(bh);
2028                                 unmap_underlying_metadata(bh->b_bdev,
2029                                                         bh->b_blocknr);
2030                                 if (PageUptodate(page)) {
2031                                         set_buffer_uptodate(bh);
2032                                         continue;
2033                                 }
2034                                 if (block_end > to || block_start < from) {
2035                                         void *kaddr;
2036
2037                                         kaddr = kmap_atomic(page, KM_USER0);
2038                                         if (block_end > to)
2039                                                 memset(kaddr+to, 0,
2040                                                         block_end-to);
2041                                         if (block_start < from)
2042                                                 memset(kaddr+block_start,
2043                                                         0, from-block_start);
2044                                         flush_dcache_page(page);
2045                                         kunmap_atomic(kaddr, KM_USER0);
2046                                 }
2047                                 continue;
2048                         }
2049                 }
2050                 if (PageUptodate(page)) {
2051                         if (!buffer_uptodate(bh))
2052                                 set_buffer_uptodate(bh);
2053                         continue; 
2054                 }
2055                 if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
2056                      (block_start < from || block_end > to)) {
2057                         ll_rw_block(READ, 1, &bh);
2058                         *wait_bh++=bh;
2059                 }
2060         }
2061         /*
2062          * If we issued read requests - let them complete.
2063          */
2064         while(wait_bh > wait) {
2065                 wait_on_buffer(*--wait_bh);
2066                 if (!buffer_uptodate(*wait_bh))
2067                         return -EIO;
2068         }
2069         return 0;
2070 out:
2071         /*
2072          * Zero out any newly allocated blocks to avoid exposing stale
2073          * data.  If BH_New is set, we know that the block was newly
2074          * allocated in the above loop.
2075          */
2076         bh = head;
2077         block_start = 0;
2078         do {
2079                 block_end = block_start+blocksize;
2080                 if (block_end <= from)
2081                         goto next_bh;
2082                 if (block_start >= to)
2083                         break;
2084                 if (buffer_new(bh)) {
2085                         void *kaddr;
2086
2087                         clear_buffer_new(bh);
2088                         kaddr = kmap_atomic(page, KM_USER0);
2089                         memset(kaddr+block_start, 0, bh->b_size);
2090                         kunmap_atomic(kaddr, KM_USER0);
2091                         set_buffer_uptodate(bh);
2092                         mark_buffer_dirty(bh);
2093                 }
2094 next_bh:
2095                 block_start = block_end;
2096                 bh = bh->b_this_page;
2097         } while (bh != head);
2098         return err;
2099 }
2100
2101 static int __block_commit_write(struct inode *inode, struct page *page,
2102                 unsigned from, unsigned to)
2103 {
2104         unsigned block_start, block_end;
2105         int partial = 0;
2106         unsigned blocksize;
2107         struct buffer_head *bh, *head;
2108
2109         blocksize = 1 << inode->i_blkbits;
2110
2111         for(bh = head = page_buffers(page), block_start = 0;
2112             bh != head || !block_start;
2113             block_start=block_end, bh = bh->b_this_page) {
2114                 block_end = block_start + blocksize;
2115                 if (block_end <= from || block_start >= to) {
2116                         if (!buffer_uptodate(bh))
2117                                 partial = 1;
2118                 } else {
2119                         set_buffer_uptodate(bh);
2120                         mark_buffer_dirty(bh);
2121                 }
2122         }
2123
2124         /*
2125          * If this is a partial write which happened to make all buffers
2126          * uptodate then we can optimize away a bogus readpage() for
2127          * the next read(). Here we 'discover' whether the page went
2128          * uptodate as a result of this (potentially partial) write.
2129          */
2130         if (!partial)
2131                 SetPageUptodate(page);
2132         return 0;
2133 }
2134
2135 /*
2136  * Generic "read page" function for block devices that have the normal
2137  * get_block functionality. This is most of the block device filesystems.
2138  * Reads the page asynchronously --- the unlock_buffer() and
2139  * set/clear_buffer_uptodate() functions propagate buffer state into the
2140  * page struct once IO has completed.
2141  */
2142 int block_read_full_page(struct page *page, get_block_t *get_block)
2143 {
2144         struct inode *inode = page->mapping->host;
2145         sector_t iblock, lblock;
2146         struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
2147         unsigned int blocksize;
2148         int nr, i;
2149         int fully_mapped = 1;
2150
2151         if (!PageLocked(page))
2152                 PAGE_BUG(page);
2153         blocksize = 1 << inode->i_blkbits;
2154         if (!page_has_buffers(page))
2155                 create_empty_buffers(page, blocksize, 0);
2156         head = page_buffers(page);
2157
2158         iblock = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
2159         lblock = (i_size_read(inode)+blocksize-1) >> inode->i_blkbits;
2160         bh = head;
2161         nr = 0;
2162         i = 0;
2163
2164         do {
2165                 if (buffer_uptodate(bh))
2166                         continue;
2167
2168                 if (!buffer_mapped(bh)) {
2169                         fully_mapped = 0;
2170                         if (iblock < lblock) {
2171                                 if (get_block(inode, iblock, bh, 0))
2172                                         SetPageError(page);
2173                         }
2174                         if (!buffer_mapped(bh)) {
2175                                 void *kaddr = kmap_atomic(page, KM_USER0);
2176                                 memset(kaddr + i * blocksize, 0, blocksize);
2177                                 flush_dcache_page(page);
2178                                 kunmap_atomic(kaddr, KM_USER0);
2179                                 set_buffer_uptodate(bh);
2180                                 continue;
2181                         }
2182                         /*
2183                          * get_block() might have updated the buffer
2184                          * synchronously
2185                          */
2186                         if (buffer_uptodate(bh))
2187                                 continue;
2188                 }
2189                 arr[nr++] = bh;
2190         } while (i++, iblock++, (bh = bh->b_this_page) != head);
2191
2192         if (fully_mapped)
2193                 SetPageMappedToDisk(page);
2194
2195         if (!nr) {
2196                 /*
2197                  * All buffers are uptodate - we can set the page uptodate
2198                  * as well. But not if get_block() returned an error.
2199                  */
2200                 if (!PageError(page))
2201                         SetPageUptodate(page);
2202                 unlock_page(page);
2203                 return 0;
2204         }
2205
2206         /* Stage two: lock the buffers */
2207         for (i = 0; i < nr; i++) {
2208                 bh = arr[i];
2209                 lock_buffer(bh);
2210                 mark_buffer_async_read(bh);
2211         }
2212
2213         /*
2214          * Stage 3: start the IO.  Check for uptodateness
2215          * inside the buffer lock in case another process reading
2216          * the underlying blockdev brought it uptodate (the sct fix).
2217          */
2218         for (i = 0; i < nr; i++) {
2219                 bh = arr[i];
2220                 if (buffer_uptodate(bh))
2221                         end_buffer_async_read(bh, 1);
2222                 else
2223                         submit_bh(READ, bh);
2224         }
2225         return 0;
2226 }
2227
2228 /* utility function for filesystems that need to do work on expanding
2229  * truncates.  Uses prepare/commit_write to allow the filesystem to
2230  * deal with the hole.  
2231  */
2232 int generic_cont_expand(struct inode *inode, loff_t size)
2233 {
2234         struct address_space *mapping = inode->i_mapping;
2235         struct page *page;
2236         unsigned long index, offset, limit;
2237         int err;
2238
2239         err = -EFBIG;
2240         limit = current->rlim[RLIMIT_FSIZE].rlim_cur;
2241         if (limit != RLIM_INFINITY && size > (loff_t)limit) {
2242                 send_sig(SIGXFSZ, current, 0);
2243                 goto out;
2244         }
2245         if (size > inode->i_sb->s_maxbytes)
2246                 goto out;
2247
2248         offset = (size & (PAGE_CACHE_SIZE-1)); /* Within page */
2249
2250         /* ugh.  in prepare/commit_write, if from==to==start of block, we 
2251         ** skip the prepare.  make sure we never send an offset for the start
2252         ** of a block
2253         */
2254         if ((offset & (inode->i_sb->s_blocksize - 1)) == 0) {
2255                 offset++;
2256         }
2257         index = size >> PAGE_CACHE_SHIFT;
2258         err = -ENOMEM;
2259         page = grab_cache_page(mapping, index);
2260         if (!page)
2261                 goto out;
2262         err = mapping->a_ops->prepare_write(NULL, page, offset, offset);
2263         if (!err) {
2264                 err = mapping->a_ops->commit_write(NULL, page, offset, offset);
2265         }
2266         unlock_page(page);
2267         page_cache_release(page);
2268         if (err > 0)
2269                 err = 0;
2270 out:
2271         return err;
2272 }
2273
2274 /*
2275  * For moronic filesystems that do not allow holes in file.
2276  * We may have to extend the file.
2277  */
2278
2279 int cont_prepare_write(struct page *page, unsigned offset,
2280                 unsigned to, get_block_t *get_block, loff_t *bytes)
2281 {
2282         struct address_space *mapping = page->mapping;
2283         struct inode *inode = mapping->host;
2284         struct page *new_page;
2285         pgoff_t pgpos;
2286         long status;
2287         unsigned zerofrom;
2288         unsigned blocksize = 1 << inode->i_blkbits;
2289         void *kaddr;
2290
2291         while(page->index > (pgpos = *bytes>>PAGE_CACHE_SHIFT)) {
2292                 status = -ENOMEM;
2293                 new_page = grab_cache_page(mapping, pgpos);
2294                 if (!new_page)
2295                         goto out;
2296                 /* we might sleep */
2297                 if (*bytes>>PAGE_CACHE_SHIFT != pgpos) {
2298                         unlock_page(new_page);
2299                         page_cache_release(new_page);
2300                         continue;
2301                 }
2302                 zerofrom = *bytes & ~PAGE_CACHE_MASK;
2303                 if (zerofrom & (blocksize-1)) {
2304                         *bytes |= (blocksize-1);
2305                         (*bytes)++;
2306                 }
2307                 status = __block_prepare_write(inode, new_page, zerofrom,
2308                                                 PAGE_CACHE_SIZE, get_block);
2309                 if (status)
2310                         goto out_unmap;
2311                 kaddr = kmap_atomic(new_page, KM_USER0);
2312                 memset(kaddr+zerofrom, 0, PAGE_CACHE_SIZE-zerofrom);
2313                 flush_dcache_page(new_page);
2314                 kunmap_atomic(kaddr, KM_USER0);
2315                 __block_commit_write(inode, new_page,
2316                                 zerofrom, PAGE_CACHE_SIZE);
2317                 unlock_page(new_page);
2318                 page_cache_release(new_page);
2319         }
2320
2321         if (page->index < pgpos) {
2322                 /* completely inside the area */
2323                 zerofrom = offset;
2324         } else {
2325                 /* page covers the boundary, find the boundary offset */
2326                 zerofrom = *bytes & ~PAGE_CACHE_MASK;
2327
2328                 /* if we will expand the thing last block will be filled */
2329                 if (to > zerofrom && (zerofrom & (blocksize-1))) {
2330                         *bytes |= (blocksize-1);
2331                         (*bytes)++;
2332                 }
2333
2334                 /* starting below the boundary? Nothing to zero out */
2335                 if (offset <= zerofrom)
2336                         zerofrom = offset;
2337         }
2338         status = __block_prepare_write(inode, page, zerofrom, to, get_block);
2339         if (status)
2340                 goto out1;
2341         if (zerofrom < offset) {
2342                 kaddr = kmap_atomic(page, KM_USER0);
2343                 memset(kaddr+zerofrom, 0, offset-zerofrom);
2344                 flush_dcache_page(page);
2345                 kunmap_atomic(kaddr, KM_USER0);
2346                 __block_commit_write(inode, page, zerofrom, offset);
2347         }
2348         return 0;
2349 out1:
2350         ClearPageUptodate(page);
2351         return status;
2352
2353 out_unmap:
2354         ClearPageUptodate(new_page);
2355         unlock_page(new_page);
2356         page_cache_release(new_page);
2357 out:
2358         return status;
2359 }
2360
2361 int block_prepare_write(struct page *page, unsigned from, unsigned to,
2362                         get_block_t *get_block)
2363 {
2364         struct inode *inode = page->mapping->host;
2365         int err = __block_prepare_write(inode, page, from, to, get_block);
2366         if (err)
2367                 ClearPageUptodate(page);
2368         return err;
2369 }
2370
2371 int block_commit_write(struct page *page, unsigned from, unsigned to)
2372 {
2373         struct inode *inode = page->mapping->host;
2374         __block_commit_write(inode,page,from,to);
2375         return 0;
2376 }
2377
2378 int generic_commit_write(struct file *file, struct page *page,
2379                 unsigned from, unsigned to)
2380 {
2381         struct inode *inode = page->mapping->host;
2382         loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2383         __block_commit_write(inode,page,from,to);
2384         /*
2385          * No need to use i_size_read() here, the i_size
2386          * cannot change under us because we hold i_sem.
2387          */
2388         if (pos > inode->i_size) {
2389                 i_size_write(inode, pos);
2390                 mark_inode_dirty(inode);
2391         }
2392         return 0;
2393 }
2394
2395
2396 /*
2397  * nobh_prepare_write()'s prereads are special: the buffer_heads are freed
2398  * immediately, while under the page lock.  So it needs a special end_io
2399  * handler which does not touch the bh after unlocking it.
2400  *
2401  * Note: unlock_buffer() sort-of does touch the bh after unlocking it, but
2402  * a race there is benign: unlock_buffer() only use the bh's address for
2403  * hashing after unlocking the buffer, so it doesn't actually touch the bh
2404  * itself.
2405  */
2406 static void end_buffer_read_nobh(struct buffer_head *bh, int uptodate)
2407 {
2408         if (uptodate) {
2409                 set_buffer_uptodate(bh);
2410         } else {
2411                 /* This happens, due to failed READA attempts. */
2412                 clear_buffer_uptodate(bh);
2413         }
2414         unlock_buffer(bh);
2415 }
2416
2417 /*
2418  * On entry, the page is fully not uptodate.
2419  * On exit the page is fully uptodate in the areas outside (from,to)
2420  */
2421 int nobh_prepare_write(struct page *page, unsigned from, unsigned to,
2422                         get_block_t *get_block)
2423 {
2424         struct inode *inode = page->mapping->host;
2425         const unsigned blkbits = inode->i_blkbits;
2426         const unsigned blocksize = 1 << blkbits;
2427         struct buffer_head map_bh;
2428         struct buffer_head *read_bh[MAX_BUF_PER_PAGE];
2429         unsigned block_in_page;
2430         unsigned block_start;
2431         sector_t block_in_file;
2432         char *kaddr;
2433         int nr_reads = 0;
2434         int i;
2435         int ret = 0;
2436         int is_mapped_to_disk = 1;
2437         int dirtied_it = 0;
2438
2439         if (PageMappedToDisk(page))
2440                 return 0;
2441
2442         block_in_file = (sector_t)page->index << (PAGE_CACHE_SHIFT - blkbits);
2443         map_bh.b_page = page;
2444
2445         /*
2446          * We loop across all blocks in the page, whether or not they are
2447          * part of the affected region.  This is so we can discover if the
2448          * page is fully mapped-to-disk.
2449          */
2450         for (block_start = 0, block_in_page = 0;
2451                   block_start < PAGE_CACHE_SIZE;
2452                   block_in_page++, block_start += blocksize) {
2453                 unsigned block_end = block_start + blocksize;
2454                 int create;
2455
2456                 map_bh.b_state = 0;
2457                 create = 1;
2458                 if (block_start >= to)
2459                         create = 0;
2460                 ret = get_block(inode, block_in_file + block_in_page,
2461                                         &map_bh, create);
2462                 if (ret)
2463                         goto failed;
2464                 if (!buffer_mapped(&map_bh))
2465                         is_mapped_to_disk = 0;
2466                 if (buffer_new(&map_bh))
2467                         unmap_underlying_metadata(map_bh.b_bdev,
2468                                                         map_bh.b_blocknr);
2469                 if (PageUptodate(page))
2470                         continue;
2471                 if (buffer_new(&map_bh) || !buffer_mapped(&map_bh)) {
2472                         kaddr = kmap_atomic(page, KM_USER0);
2473                         if (block_start < from) {
2474                                 memset(kaddr+block_start, 0, from-block_start);
2475                                 dirtied_it = 1;
2476                         }
2477                         if (block_end > to) {
2478                                 memset(kaddr + to, 0, block_end - to);
2479                                 dirtied_it = 1;
2480                         }
2481                         flush_dcache_page(page);
2482                         kunmap_atomic(kaddr, KM_USER0);
2483                         continue;
2484                 }
2485                 if (buffer_uptodate(&map_bh))
2486                         continue;       /* reiserfs does this */
2487                 if (block_start < from || block_end > to) {
2488                         struct buffer_head *bh = alloc_buffer_head(GFP_NOFS);
2489
2490                         if (!bh) {
2491                                 ret = -ENOMEM;
2492                                 goto failed;
2493                         }
2494                         bh->b_state = map_bh.b_state;
2495                         atomic_set(&bh->b_count, 0);
2496                         bh->b_this_page = NULL;
2497                         bh->b_page = page;
2498                         bh->b_blocknr = map_bh.b_blocknr;
2499                         bh->b_size = blocksize;
2500                         bh->b_data = (char *)(long)block_start;
2501                         bh->b_bdev = map_bh.b_bdev;
2502                         bh->b_private = NULL;
2503                         read_bh[nr_reads++] = bh;
2504                 }
2505         }
2506
2507         if (nr_reads) {
2508                 struct buffer_head *bh;
2509
2510                 /*
2511                  * The page is locked, so these buffers are protected from
2512                  * any VM or truncate activity.  Hence we don't need to care
2513                  * for the buffer_head refcounts.
2514                  */
2515                 for (i = 0; i < nr_reads; i++) {
2516                         bh = read_bh[i];
2517                         lock_buffer(bh);
2518                         bh->b_end_io = end_buffer_read_nobh;
2519                         submit_bh(READ, bh);
2520                 }
2521                 for (i = 0; i < nr_reads; i++) {
2522                         bh = read_bh[i];
2523                         wait_on_buffer(bh);
2524                         if (!buffer_uptodate(bh))
2525                                 ret = -EIO;
2526                         free_buffer_head(bh);
2527                         read_bh[i] = NULL;
2528                 }
2529                 if (ret)
2530                         goto failed;
2531         }
2532
2533         if (is_mapped_to_disk)
2534                 SetPageMappedToDisk(page);
2535         SetPageUptodate(page);
2536
2537         /*
2538          * Setting the page dirty here isn't necessary for the prepare_write
2539          * function - commit_write will do that.  But if/when this function is
2540          * used within the pagefault handler to ensure that all mmapped pages
2541          * have backing space in the filesystem, we will need to dirty the page
2542          * if its contents were altered.
2543          */
2544         if (dirtied_it)
2545                 set_page_dirty(page);
2546
2547         return 0;
2548
2549 failed:
2550         for (i = 0; i < nr_reads; i++) {
2551                 if (read_bh[i])
2552                         free_buffer_head(read_bh[i]);
2553         }
2554
2555         /*
2556          * Error recovery is pretty slack.  Clear the page and mark it dirty
2557          * so we'll later zero out any blocks which _were_ allocated.
2558          */
2559         kaddr = kmap_atomic(page, KM_USER0);
2560         memset(kaddr, 0, PAGE_CACHE_SIZE);
2561         kunmap_atomic(kaddr, KM_USER0);
2562         SetPageUptodate(page);
2563         set_page_dirty(page);
2564         return ret;
2565 }
2566 EXPORT_SYMBOL(nobh_prepare_write);
2567
2568 int nobh_commit_write(struct file *file, struct page *page,
2569                 unsigned from, unsigned to)
2570 {
2571         struct inode *inode = page->mapping->host;
2572         loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2573
2574         set_page_dirty(page);
2575         if (pos > inode->i_size) {
2576                 i_size_write(inode, pos);
2577                 mark_inode_dirty(inode);
2578         }
2579         return 0;
2580 }
2581 EXPORT_SYMBOL(nobh_commit_write);
2582
2583 /*
2584  * This function assumes that ->prepare_write() uses nobh_prepare_write().
2585  */
2586 int nobh_truncate_page(struct address_space *mapping, loff_t from)
2587 {
2588         struct inode *inode = mapping->host;
2589         unsigned blocksize = 1 << inode->i_blkbits;
2590         pgoff_t index = from >> PAGE_CACHE_SHIFT;
2591         unsigned offset = from & (PAGE_CACHE_SIZE-1);
2592         unsigned to;
2593         struct page *page;
2594         struct address_space_operations *a_ops = mapping->a_ops;
2595         char *kaddr;
2596         int ret = 0;
2597
2598         if ((offset & (blocksize - 1)) == 0)
2599                 goto out;
2600
2601         ret = -ENOMEM;
2602         page = grab_cache_page(mapping, index);
2603         if (!page)
2604                 goto out;
2605
2606         to = (offset + blocksize) & ~(blocksize - 1);
2607         ret = a_ops->prepare_write(NULL, page, offset, to);
2608         if (ret == 0) {
2609                 kaddr = kmap_atomic(page, KM_USER0);
2610                 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
2611                 flush_dcache_page(page);
2612                 kunmap_atomic(kaddr, KM_USER0);
2613                 set_page_dirty(page);
2614         }
2615         unlock_page(page);
2616         page_cache_release(page);
2617 out:
2618         return ret;
2619 }
2620 EXPORT_SYMBOL(nobh_truncate_page);
2621
2622 int block_truncate_page(struct address_space *mapping,
2623                         loff_t from, get_block_t *get_block)
2624 {
2625         pgoff_t index = from >> PAGE_CACHE_SHIFT;
2626         unsigned offset = from & (PAGE_CACHE_SIZE-1);
2627         unsigned blocksize;
2628         pgoff_t iblock;
2629         unsigned length, pos;
2630         struct inode *inode = mapping->host;
2631         struct page *page;
2632         struct buffer_head *bh;
2633         void *kaddr;
2634         int err;
2635
2636         blocksize = 1 << inode->i_blkbits;
2637         length = offset & (blocksize - 1);
2638
2639         /* Block boundary? Nothing to do */
2640         if (!length)
2641                 return 0;
2642
2643         length = blocksize - length;
2644         iblock = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
2645         
2646         page = grab_cache_page(mapping, index);
2647         err = -ENOMEM;
2648         if (!page)
2649                 goto out;
2650
2651         if (!page_has_buffers(page))
2652                 create_empty_buffers(page, blocksize, 0);
2653
2654         /* Find the buffer that contains "offset" */
2655         bh = page_buffers(page);
2656         pos = blocksize;
2657         while (offset >= pos) {
2658                 bh = bh->b_this_page;
2659                 iblock++;
2660                 pos += blocksize;
2661         }
2662
2663         err = 0;
2664         if (!buffer_mapped(bh)) {
2665                 err = get_block(inode, iblock, bh, 0);
2666                 if (err)
2667                         goto unlock;
2668                 /* unmapped? It's a hole - nothing to do */
2669                 if (!buffer_mapped(bh))
2670                         goto unlock;
2671         }
2672
2673         /* Ok, it's mapped. Make sure it's up-to-date */
2674         if (PageUptodate(page))
2675                 set_buffer_uptodate(bh);
2676
2677         if (!buffer_uptodate(bh) && !buffer_delay(bh)) {
2678                 err = -EIO;
2679                 ll_rw_block(READ, 1, &bh);
2680                 wait_on_buffer(bh);
2681                 /* Uhhuh. Read error. Complain and punt. */
2682                 if (!buffer_uptodate(bh))
2683                         goto unlock;
2684         }
2685
2686         kaddr = kmap_atomic(page, KM_USER0);
2687         memset(kaddr + offset, 0, length);
2688         flush_dcache_page(page);
2689         kunmap_atomic(kaddr, KM_USER0);
2690
2691         mark_buffer_dirty(bh);
2692         err = 0;
2693
2694 unlock:
2695         unlock_page(page);
2696         page_cache_release(page);
2697 out:
2698         return err;
2699 }
2700
2701 /*
2702  * The generic ->writepage function for buffer-backed address_spaces
2703  */
2704 int block_write_full_page(struct page *page, get_block_t *get_block,
2705                         struct writeback_control *wbc)
2706 {
2707         struct inode * const inode = page->mapping->host;
2708         loff_t i_size = i_size_read(inode);
2709         const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
2710         unsigned offset;
2711         void *kaddr;
2712
2713         /* Is the page fully inside i_size? */
2714         if (page->index < end_index)
2715                 return __block_write_full_page(inode, page, get_block, wbc);
2716
2717         /* Is the page fully outside i_size? (truncate in progress) */
2718         offset = i_size & (PAGE_CACHE_SIZE-1);
2719         if (page->index >= end_index+1 || !offset) {
2720                 /*
2721                  * The page may have dirty, unmapped buffers.  For example,
2722                  * they may have been added in ext3_writepage().  Make them
2723                  * freeable here, so the page does not leak.
2724                  */
2725                 block_invalidatepage(page, 0);
2726                 unlock_page(page);
2727                 return 0; /* don't care */
2728         }
2729
2730         /*
2731          * The page straddles i_size.  It must be zeroed out on each and every
2732          * writepage invokation because it may be mmapped.  "A file is mapped
2733          * in multiples of the page size.  For a file that is not a multiple of
2734          * the  page size, the remaining memory is zeroed when mapped, and
2735          * writes to that region are not written out to the file."
2736          */
2737         kaddr = kmap_atomic(page, KM_USER0);
2738         memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
2739         flush_dcache_page(page);
2740         kunmap_atomic(kaddr, KM_USER0);
2741         return __block_write_full_page(inode, page, get_block, wbc);
2742 }
2743
2744 sector_t generic_block_bmap(struct address_space *mapping, sector_t block,
2745                             get_block_t *get_block)
2746 {
2747         struct buffer_head tmp;
2748         struct inode *inode = mapping->host;
2749         tmp.b_state = 0;
2750         tmp.b_blocknr = 0;
2751         get_block(inode, block, &tmp, 0);
2752         return tmp.b_blocknr;
2753 }
2754
2755 static int end_bio_bh_io_sync(struct bio *bio, unsigned int bytes_done, int err)
2756 {
2757         struct buffer_head *bh = bio->bi_private;
2758
2759         if (bio->bi_size)
2760                 return 1;
2761
2762         bh->b_end_io(bh, test_bit(BIO_UPTODATE, &bio->bi_flags));
2763         bio_put(bio);
2764         return 0;
2765 }
2766
2767 void submit_bh(int rw, struct buffer_head * bh)
2768 {
2769         struct bio *bio;
2770
2771         BUG_ON(!buffer_locked(bh));
2772         BUG_ON(!buffer_mapped(bh));
2773         BUG_ON(!bh->b_end_io);
2774
2775         /* Only clear out a write error when rewriting */
2776         if (test_set_buffer_req(bh) && rw == WRITE)
2777                 clear_buffer_write_io_error(bh);
2778
2779         /*
2780          * from here on down, it's all bio -- do the initial mapping,
2781          * submit_bio -> generic_make_request may further map this bio around
2782          */
2783         bio = bio_alloc(GFP_NOIO, 1);
2784
2785         bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
2786         bio->bi_bdev = bh->b_bdev;
2787         bio->bi_io_vec[0].bv_page = bh->b_page;
2788         bio->bi_io_vec[0].bv_len = bh->b_size;
2789         bio->bi_io_vec[0].bv_offset = bh_offset(bh);
2790
2791         bio->bi_vcnt = 1;
2792         bio->bi_idx = 0;
2793         bio->bi_size = bh->b_size;
2794
2795         bio->bi_end_io = end_bio_bh_io_sync;
2796         bio->bi_private = bh;
2797
2798         submit_bio(rw, bio);
2799 }
2800
2801 /**
2802  * ll_rw_block: low-level access to block devices (DEPRECATED)
2803  * @rw: whether to %READ or %WRITE or maybe %READA (readahead)
2804  * @nr: number of &struct buffer_heads in the array
2805  * @bhs: array of pointers to &struct buffer_head
2806  *
2807  * ll_rw_block() takes an array of pointers to &struct buffer_heads,
2808  * and requests an I/O operation on them, either a %READ or a %WRITE.
2809  * The third %READA option is described in the documentation for
2810  * generic_make_request() which ll_rw_block() calls.
2811  *
2812  * This function drops any buffer that it cannot get a lock on (with the
2813  * BH_Lock state bit), any buffer that appears to be clean when doing a
2814  * write request, and any buffer that appears to be up-to-date when doing
2815  * read request.  Further it marks as clean buffers that are processed for
2816  * writing (the buffer cache won't assume that they are actually clean until
2817  * the buffer gets unlocked).
2818  *
2819  * ll_rw_block sets b_end_io to simple completion handler that marks
2820  * the buffer up-to-date (if approriate), unlocks the buffer and wakes
2821  * any waiters. 
2822  *
2823  * All of the buffers must be for the same device, and must also be a
2824  * multiple of the current approved size for the device.
2825  */
2826 void ll_rw_block(int rw, int nr, struct buffer_head *bhs[])
2827 {
2828         int i;
2829
2830         for (i = 0; i < nr; i++) {
2831                 struct buffer_head *bh = bhs[i];
2832
2833                 if (test_set_buffer_locked(bh))
2834                         continue;
2835
2836                 get_bh(bh);
2837                 if (rw == WRITE) {
2838                         bh->b_end_io = end_buffer_write_sync;
2839                         if (test_clear_buffer_dirty(bh)) {
2840                                 submit_bh(WRITE, bh);
2841                                 continue;
2842                         }
2843                 } else {
2844                         bh->b_end_io = end_buffer_read_sync;
2845                         if (!buffer_uptodate(bh)) {
2846                                 submit_bh(rw, bh);
2847                                 continue;
2848                         }
2849                 }
2850                 unlock_buffer(bh);
2851                 put_bh(bh);
2852         }
2853 }
2854
2855 /*
2856  * For a data-integrity writeout, we need to wait upon any in-progress I/O
2857  * and then start new I/O and then wait upon it.
2858  */
2859 void sync_dirty_buffer(struct buffer_head *bh)
2860 {
2861         WARN_ON(atomic_read(&bh->b_count) < 1);
2862         lock_buffer(bh);
2863         if (test_clear_buffer_dirty(bh)) {
2864                 get_bh(bh);
2865                 bh->b_end_io = end_buffer_write_sync;
2866                 submit_bh(WRITE, bh);
2867                 wait_on_buffer(bh);
2868         } else {
2869                 unlock_buffer(bh);
2870         }
2871 }
2872
2873 /*
2874  * try_to_free_buffers() checks if all the buffers on this particular page
2875  * are unused, and releases them if so.
2876  *
2877  * Exclusion against try_to_free_buffers may be obtained by either
2878  * locking the page or by holding its mapping's private_lock.
2879  *
2880  * If the page is dirty but all the buffers are clean then we need to
2881  * be sure to mark the page clean as well.  This is because the page
2882  * may be against a block device, and a later reattachment of buffers
2883  * to a dirty page will set *all* buffers dirty.  Which would corrupt
2884  * filesystem data on the same device.
2885  *
2886  * The same applies to regular filesystem pages: if all the buffers are
2887  * clean then we set the page clean and proceed.  To do that, we require
2888  * total exclusion from __set_page_dirty_buffers().  That is obtained with
2889  * private_lock.
2890  *
2891  * try_to_free_buffers() is non-blocking.
2892  */
2893 static inline int buffer_busy(struct buffer_head *bh)
2894 {
2895         return atomic_read(&bh->b_count) |
2896                 (bh->b_state & ((1 << BH_Dirty) | (1 << BH_Lock)));
2897 }
2898
2899 static int
2900 drop_buffers(struct page *page, struct buffer_head **buffers_to_free)
2901 {
2902         struct buffer_head *head = page_buffers(page);
2903         struct buffer_head *bh;
2904
2905         bh = head;
2906         do {
2907                 if (buffer_write_io_error(bh))
2908                         set_bit(AS_EIO, &page->mapping->flags);
2909                 if (buffer_busy(bh))
2910                         goto failed;
2911                 bh = bh->b_this_page;
2912         } while (bh != head);
2913
2914         do {
2915                 struct buffer_head *next = bh->b_this_page;
2916
2917                 if (!list_empty(&bh->b_assoc_buffers))
2918                         __remove_assoc_queue(bh);
2919                 bh = next;
2920         } while (bh != head);
2921         *buffers_to_free = head;
2922         __clear_page_buffers(page);
2923         return 1;
2924 failed:
2925         return 0;
2926 }
2927
2928 int try_to_free_buffers(struct page *page)
2929 {
2930         struct address_space * const mapping = page->mapping;
2931         struct buffer_head *buffers_to_free = NULL;
2932         int ret = 0;
2933
2934         BUG_ON(!PageLocked(page));
2935         if (PageWriteback(page))
2936                 return 0;
2937
2938         if (mapping == NULL) {          /* can this still happen? */
2939                 ret = drop_buffers(page, &buffers_to_free);
2940                 goto out;
2941         }
2942
2943         spin_lock(&mapping->private_lock);
2944         ret = drop_buffers(page, &buffers_to_free);
2945         if (ret) {
2946                 /*
2947                  * If the filesystem writes its buffers by hand (eg ext3)
2948                  * then we can have clean buffers against a dirty page.  We
2949                  * clean the page here; otherwise later reattachment of buffers
2950                  * could encounter a non-uptodate page, which is unresolvable.
2951                  * This only applies in the rare case where try_to_free_buffers
2952                  * succeeds but the page is not freed.
2953                  */
2954                 clear_page_dirty(page);
2955         }
2956         spin_unlock(&mapping->private_lock);
2957 out:
2958         if (buffers_to_free) {
2959                 struct buffer_head *bh = buffers_to_free;
2960
2961                 do {
2962                         struct buffer_head *next = bh->b_this_page;
2963                         free_buffer_head(bh);
2964                         bh = next;
2965                 } while (bh != buffers_to_free);
2966         }
2967         return ret;
2968 }
2969 EXPORT_SYMBOL(try_to_free_buffers);
2970
2971 int block_sync_page(struct page *page)
2972 {
2973         struct address_space *mapping;
2974
2975         smp_mb();
2976         mapping = page_mapping(page);
2977         if (mapping)
2978                 blk_run_backing_dev(mapping->backing_dev_info, page);
2979         return 0;
2980 }
2981
2982 /*
2983  * There are no bdflush tunables left.  But distributions are
2984  * still running obsolete flush daemons, so we terminate them here.
2985  *
2986  * Use of bdflush() is deprecated and will be removed in a future kernel.
2987  * The `pdflush' kernel threads fully replace bdflush daemons and this call.
2988  */
2989 asmlinkage long sys_bdflush(int func, long data)
2990 {
2991         static int msg_count;
2992
2993         if (!capable(CAP_SYS_ADMIN))
2994                 return -EPERM;
2995
2996         if (msg_count < 5) {
2997                 msg_count++;
2998                 printk(KERN_INFO
2999                         "warning: process `%s' used the obsolete bdflush"
3000                         " system call\n", current->comm);
3001                 printk(KERN_INFO "Fix your initscripts?\n");
3002         }
3003
3004         if (func == 1)
3005                 do_exit(0);
3006         return 0;
3007 }
3008
3009 /*
3010  * Buffer-head allocation
3011  */
3012 static kmem_cache_t *bh_cachep;
3013
3014 /*
3015  * Once the number of bh's in the machine exceeds this level, we start
3016  * stripping them in writeback.
3017  */
3018 static int max_buffer_heads;
3019
3020 int buffer_heads_over_limit;
3021
3022 struct bh_accounting {
3023         int nr;                 /* Number of live bh's */
3024         int ratelimit;          /* Limit cacheline bouncing */
3025 };
3026
3027 static DEFINE_PER_CPU(struct bh_accounting, bh_accounting) = {0, 0};
3028
3029 static void recalc_bh_state(void)
3030 {
3031         int i;
3032         int tot = 0;
3033
3034         if (__get_cpu_var(bh_accounting).ratelimit++ < 4096)
3035                 return;
3036         __get_cpu_var(bh_accounting).ratelimit = 0;
3037         for_each_cpu(i)
3038                 tot += per_cpu(bh_accounting, i).nr;
3039         buffer_heads_over_limit = (tot > max_buffer_heads);
3040 }
3041         
3042 struct buffer_head *alloc_buffer_head(int gfp_flags)
3043 {
3044         struct buffer_head *ret = kmem_cache_alloc(bh_cachep, gfp_flags);
3045         if (ret) {
3046                 preempt_disable();
3047                 __get_cpu_var(bh_accounting).nr++;
3048                 recalc_bh_state();
3049                 preempt_enable();
3050         }
3051         return ret;
3052 }
3053 EXPORT_SYMBOL(alloc_buffer_head);
3054
3055 void free_buffer_head(struct buffer_head *bh)
3056 {
3057         BUG_ON(!list_empty(&bh->b_assoc_buffers));
3058         kmem_cache_free(bh_cachep, bh);
3059         preempt_disable();
3060         __get_cpu_var(bh_accounting).nr--;
3061         recalc_bh_state();
3062         preempt_enable();
3063 }
3064 EXPORT_SYMBOL(free_buffer_head);
3065
3066 static void
3067 init_buffer_head(void *data, kmem_cache_t *cachep, unsigned long flags)
3068 {
3069         if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
3070                             SLAB_CTOR_CONSTRUCTOR) {
3071                 struct buffer_head * bh = (struct buffer_head *)data;
3072
3073                 memset(bh, 0, sizeof(*bh));
3074                 INIT_LIST_HEAD(&bh->b_assoc_buffers);
3075         }
3076 }
3077
3078 #ifdef CONFIG_HOTPLUG_CPU
3079 static void buffer_exit_cpu(int cpu)
3080 {
3081         int i;
3082         struct bh_lru *b = &per_cpu(bh_lrus, cpu);
3083
3084         for (i = 0; i < BH_LRU_SIZE; i++) {
3085                 brelse(b->bhs[i]);
3086                 b->bhs[i] = NULL;
3087         }
3088 }
3089
3090 static int buffer_cpu_notify(struct notifier_block *self,
3091                               unsigned long action, void *hcpu)
3092 {
3093         if (action == CPU_DEAD)
3094                 buffer_exit_cpu((unsigned long)hcpu);
3095         return NOTIFY_OK;
3096 }
3097 #endif /* CONFIG_HOTPLUG_CPU */
3098
3099 void __init buffer_init(void)
3100 {
3101         int i;
3102         int nrpages;
3103
3104         bh_cachep = kmem_cache_create("buffer_head",
3105                         sizeof(struct buffer_head), 0,
3106                         SLAB_PANIC, init_buffer_head, NULL);
3107         for (i = 0; i < ARRAY_SIZE(bh_wait_queue_heads); i++)
3108                 init_waitqueue_head(&bh_wait_queue_heads[i].wqh);
3109
3110         /*
3111          * Limit the bh occupancy to 10% of ZONE_NORMAL
3112          */
3113         nrpages = (nr_free_buffer_pages() * 10) / 100;
3114         max_buffer_heads = nrpages * (PAGE_SIZE / sizeof(struct buffer_head));
3115         hotcpu_notifier(buffer_cpu_notify, 0);
3116 }
3117
3118 EXPORT_SYMBOL(__bforget);
3119 EXPORT_SYMBOL(__brelse);
3120 EXPORT_SYMBOL(__wait_on_buffer);
3121 EXPORT_SYMBOL(block_commit_write);
3122 EXPORT_SYMBOL(block_prepare_write);
3123 EXPORT_SYMBOL(block_read_full_page);
3124 EXPORT_SYMBOL(block_sync_page);
3125 EXPORT_SYMBOL(block_truncate_page);
3126 EXPORT_SYMBOL(block_write_full_page);
3127 EXPORT_SYMBOL(buffer_insert_list);
3128 EXPORT_SYMBOL(cont_prepare_write);
3129 EXPORT_SYMBOL(end_buffer_async_write);
3130 EXPORT_SYMBOL(end_buffer_read_sync);
3131 EXPORT_SYMBOL(end_buffer_write_sync);
3132 EXPORT_SYMBOL(file_fsync);
3133 EXPORT_SYMBOL(fsync_bdev);
3134 EXPORT_SYMBOL(fsync_buffers_list);
3135 EXPORT_SYMBOL(generic_block_bmap);
3136 EXPORT_SYMBOL(generic_commit_write);
3137 EXPORT_SYMBOL(generic_cont_expand);
3138 EXPORT_SYMBOL(init_buffer);
3139 EXPORT_SYMBOL(invalidate_bdev);
3140 EXPORT_SYMBOL(ll_rw_block);
3141 EXPORT_SYMBOL(mark_buffer_dirty);
3142 EXPORT_SYMBOL(submit_bh);
3143 EXPORT_SYMBOL(sync_dirty_buffer);
3144 EXPORT_SYMBOL(unlock_buffer);