4 * Copyright (C) 1991, 1992, 2002 Linus Torvalds
8 * Start bdflush() with kernel_thread not syscall - Paul Gortmaker, 12/95
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
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
16 * Added 32k buffer block sizes - these are required older ARM systems. - RMK
18 * async buffer flushing, 1999 Andrea Arcangeli <andrea@suse.de>
21 #include <linux/kernel.h>
22 #include <linux/syscalls.h>
25 #include <linux/percpu.h>
26 #include <linux/slab.h>
27 #include <linux/capability.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/task_io_accounting_ops.h>
38 #include <linux/bio.h>
39 #include <linux/notifier.h>
40 #include <linux/cpu.h>
41 #include <linux/bitops.h>
42 #include <linux/mpage.h>
43 #include <linux/bit_spinlock.h>
45 static int fsync_buffers_list(spinlock_t *lock, struct list_head *list);
47 #define BH_ENTRY(list) list_entry((list), struct buffer_head, b_assoc_buffers)
50 init_buffer(struct buffer_head *bh, bh_end_io_t *handler, void *private)
52 bh->b_end_io = handler;
53 bh->b_private = private;
56 static int sync_buffer(void *word)
58 struct block_device *bd;
59 struct buffer_head *bh
60 = container_of(word, struct buffer_head, b_state);
65 blk_run_address_space(bd->bd_inode->i_mapping);
70 void __lock_buffer(struct buffer_head *bh)
72 wait_on_bit_lock(&bh->b_state, BH_Lock, sync_buffer,
73 TASK_UNINTERRUPTIBLE);
75 EXPORT_SYMBOL(__lock_buffer);
77 void unlock_buffer(struct buffer_head *bh)
79 clear_bit_unlock(BH_Lock, &bh->b_state);
80 smp_mb__after_clear_bit();
81 wake_up_bit(&bh->b_state, BH_Lock);
85 * Block until a buffer comes unlocked. This doesn't stop it
86 * from becoming locked again - you have to lock it yourself
87 * if you want to preserve its state.
89 void __wait_on_buffer(struct buffer_head * bh)
91 wait_on_bit(&bh->b_state, BH_Lock, sync_buffer, TASK_UNINTERRUPTIBLE);
95 __clear_page_buffers(struct page *page)
97 ClearPagePrivate(page);
98 set_page_private(page, 0);
99 page_cache_release(page);
103 static int quiet_error(struct buffer_head *bh)
105 if (!test_bit(BH_Quiet, &bh->b_state) && printk_ratelimit())
111 static void buffer_io_error(struct buffer_head *bh)
113 char b[BDEVNAME_SIZE];
114 printk(KERN_ERR "Buffer I/O error on device %s, logical block %Lu\n",
115 bdevname(bh->b_bdev, b),
116 (unsigned long long)bh->b_blocknr);
120 * End-of-IO handler helper function which does not touch the bh after
122 * Note: unlock_buffer() sort-of does touch the bh after unlocking it, but
123 * a race there is benign: unlock_buffer() only use the bh's address for
124 * hashing after unlocking the buffer, so it doesn't actually touch the bh
127 static void __end_buffer_read_notouch(struct buffer_head *bh, int uptodate)
130 set_buffer_uptodate(bh);
132 /* This happens, due to failed READA attempts. */
133 clear_buffer_uptodate(bh);
139 * Default synchronous end-of-IO handler.. Just mark it up-to-date and
140 * unlock the buffer. This is what ll_rw_block uses too.
142 void end_buffer_read_sync(struct buffer_head *bh, int uptodate)
144 __end_buffer_read_notouch(bh, uptodate);
148 void end_buffer_write_sync(struct buffer_head *bh, int uptodate)
150 char b[BDEVNAME_SIZE];
153 set_buffer_uptodate(bh);
155 if (!buffer_eopnotsupp(bh) && !quiet_error(bh)) {
157 printk(KERN_WARNING "lost page write due to "
159 bdevname(bh->b_bdev, b));
161 set_buffer_write_io_error(bh);
162 clear_buffer_uptodate(bh);
169 * Various filesystems appear to want __find_get_block to be non-blocking.
170 * But it's the page lock which protects the buffers. To get around this,
171 * we get exclusion from try_to_free_buffers with the blockdev mapping's
174 * Hack idea: for the blockdev mapping, i_bufferlist_lock contention
175 * may be quite high. This code could TryLock the page, and if that
176 * succeeds, there is no need to take private_lock. (But if
177 * private_lock is contended then so is mapping->tree_lock).
179 static struct buffer_head *
180 __find_get_block_slow(struct block_device *bdev, sector_t block)
182 struct inode *bd_inode = bdev->bd_inode;
183 struct address_space *bd_mapping = bd_inode->i_mapping;
184 struct buffer_head *ret = NULL;
186 struct buffer_head *bh;
187 struct buffer_head *head;
191 index = block >> (PAGE_CACHE_SHIFT - bd_inode->i_blkbits);
192 page = find_get_page(bd_mapping, index);
196 spin_lock(&bd_mapping->private_lock);
197 if (!page_has_buffers(page))
199 head = page_buffers(page);
202 if (!buffer_mapped(bh))
204 else if (bh->b_blocknr == block) {
209 bh = bh->b_this_page;
210 } while (bh != head);
212 /* we might be here because some of the buffers on this page are
213 * not mapped. This is due to various races between
214 * file io on the block device and getblk. It gets dealt with
215 * elsewhere, don't buffer_error if we had some unmapped buffers
218 printk("__find_get_block_slow() failed. "
219 "block=%llu, b_blocknr=%llu\n",
220 (unsigned long long)block,
221 (unsigned long long)bh->b_blocknr);
222 printk("b_state=0x%08lx, b_size=%zu\n",
223 bh->b_state, bh->b_size);
224 printk("device blocksize: %d\n", 1 << bd_inode->i_blkbits);
227 spin_unlock(&bd_mapping->private_lock);
228 page_cache_release(page);
233 /* If invalidate_buffers() will trash dirty buffers, it means some kind
234 of fs corruption is going on. Trashing dirty data always imply losing
235 information that was supposed to be just stored on the physical layer
238 Thus invalidate_buffers in general usage is not allwowed to trash
239 dirty buffers. For example ioctl(FLSBLKBUF) expects dirty data to
240 be preserved. These buffers are simply skipped.
242 We also skip buffers which are still in use. For example this can
243 happen if a userspace program is reading the block device.
245 NOTE: In the case where the user removed a removable-media-disk even if
246 there's still dirty data not synced on disk (due a bug in the device driver
247 or due an error of the user), by not destroying the dirty buffers we could
248 generate corruption also on the next media inserted, thus a parameter is
249 necessary to handle this case in the most safe way possible (trying
250 to not corrupt also the new disk inserted with the data belonging to
251 the old now corrupted disk). Also for the ramdisk the natural thing
252 to do in order to release the ramdisk memory is to destroy dirty buffers.
254 These are two special cases. Normal usage imply the device driver
255 to issue a sync on the device (without waiting I/O completion) and
256 then an invalidate_buffers call that doesn't trash dirty buffers.
258 For handling cache coherency with the blkdev pagecache the 'update' case
259 is been introduced. It is needed to re-read from disk any pinned
260 buffer. NOTE: re-reading from disk is destructive so we can do it only
261 when we assume nobody is changing the buffercache under our I/O and when
262 we think the disk contains more recent information than the buffercache.
263 The update == 1 pass marks the buffers we need to update, the update == 2
264 pass does the actual I/O. */
265 void invalidate_bdev(struct block_device *bdev)
267 struct address_space *mapping = bdev->bd_inode->i_mapping;
269 if (mapping->nrpages == 0)
272 invalidate_bh_lrus();
273 invalidate_mapping_pages(mapping, 0, -1);
277 * Kick pdflush then try to free up some ZONE_NORMAL memory.
279 static void free_more_memory(void)
284 wakeup_pdflush(1024);
287 for_each_online_node(nid) {
288 (void)first_zones_zonelist(node_zonelist(nid, GFP_NOFS),
289 gfp_zone(GFP_NOFS), NULL,
292 try_to_free_pages(node_zonelist(nid, GFP_NOFS), 0,
298 * I/O completion handler for block_read_full_page() - pages
299 * which come unlocked at the end of I/O.
301 static void end_buffer_async_read(struct buffer_head *bh, int uptodate)
304 struct buffer_head *first;
305 struct buffer_head *tmp;
307 int page_uptodate = 1;
309 BUG_ON(!buffer_async_read(bh));
313 set_buffer_uptodate(bh);
315 clear_buffer_uptodate(bh);
316 if (!quiet_error(bh))
322 * Be _very_ careful from here on. Bad things can happen if
323 * two buffer heads end IO at almost the same time and both
324 * decide that the page is now completely done.
326 first = page_buffers(page);
327 local_irq_save(flags);
328 bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
329 clear_buffer_async_read(bh);
333 if (!buffer_uptodate(tmp))
335 if (buffer_async_read(tmp)) {
336 BUG_ON(!buffer_locked(tmp));
339 tmp = tmp->b_this_page;
341 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
342 local_irq_restore(flags);
345 * If none of the buffers had errors and they are all
346 * uptodate then we can set the page uptodate.
348 if (page_uptodate && !PageError(page))
349 SetPageUptodate(page);
354 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
355 local_irq_restore(flags);
360 * Completion handler for block_write_full_page() - pages which are unlocked
361 * during I/O, and which have PageWriteback cleared upon I/O completion.
363 static void end_buffer_async_write(struct buffer_head *bh, int uptodate)
365 char b[BDEVNAME_SIZE];
367 struct buffer_head *first;
368 struct buffer_head *tmp;
371 BUG_ON(!buffer_async_write(bh));
375 set_buffer_uptodate(bh);
377 if (!quiet_error(bh)) {
379 printk(KERN_WARNING "lost page write due to "
381 bdevname(bh->b_bdev, b));
383 set_bit(AS_EIO, &page->mapping->flags);
384 set_buffer_write_io_error(bh);
385 clear_buffer_uptodate(bh);
389 first = page_buffers(page);
390 local_irq_save(flags);
391 bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
393 clear_buffer_async_write(bh);
395 tmp = bh->b_this_page;
397 if (buffer_async_write(tmp)) {
398 BUG_ON(!buffer_locked(tmp));
401 tmp = tmp->b_this_page;
403 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
404 local_irq_restore(flags);
405 end_page_writeback(page);
409 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
410 local_irq_restore(flags);
415 * If a page's buffers are under async readin (end_buffer_async_read
416 * completion) then there is a possibility that another thread of
417 * control could lock one of the buffers after it has completed
418 * but while some of the other buffers have not completed. This
419 * locked buffer would confuse end_buffer_async_read() into not unlocking
420 * the page. So the absence of BH_Async_Read tells end_buffer_async_read()
421 * that this buffer is not under async I/O.
423 * The page comes unlocked when it has no locked buffer_async buffers
426 * PageLocked prevents anyone starting new async I/O reads any of
429 * PageWriteback is used to prevent simultaneous writeout of the same
432 * PageLocked prevents anyone from starting writeback of a page which is
433 * under read I/O (PageWriteback is only ever set against a locked page).
435 static void mark_buffer_async_read(struct buffer_head *bh)
437 bh->b_end_io = end_buffer_async_read;
438 set_buffer_async_read(bh);
441 void mark_buffer_async_write(struct buffer_head *bh)
443 bh->b_end_io = end_buffer_async_write;
444 set_buffer_async_write(bh);
446 EXPORT_SYMBOL(mark_buffer_async_write);
450 * fs/buffer.c contains helper functions for buffer-backed address space's
451 * fsync functions. A common requirement for buffer-based filesystems is
452 * that certain data from the backing blockdev needs to be written out for
453 * a successful fsync(). For example, ext2 indirect blocks need to be
454 * written back and waited upon before fsync() returns.
456 * The functions mark_buffer_inode_dirty(), fsync_inode_buffers(),
457 * inode_has_buffers() and invalidate_inode_buffers() are provided for the
458 * management of a list of dependent buffers at ->i_mapping->private_list.
460 * Locking is a little subtle: try_to_free_buffers() will remove buffers
461 * from their controlling inode's queue when they are being freed. But
462 * try_to_free_buffers() will be operating against the *blockdev* mapping
463 * at the time, not against the S_ISREG file which depends on those buffers.
464 * So the locking for private_list is via the private_lock in the address_space
465 * which backs the buffers. Which is different from the address_space
466 * against which the buffers are listed. So for a particular address_space,
467 * mapping->private_lock does *not* protect mapping->private_list! In fact,
468 * mapping->private_list will always be protected by the backing blockdev's
471 * Which introduces a requirement: all buffers on an address_space's
472 * ->private_list must be from the same address_space: the blockdev's.
474 * address_spaces which do not place buffers at ->private_list via these
475 * utility functions are free to use private_lock and private_list for
476 * whatever they want. The only requirement is that list_empty(private_list)
477 * be true at clear_inode() time.
479 * FIXME: clear_inode should not call invalidate_inode_buffers(). The
480 * filesystems should do that. invalidate_inode_buffers() should just go
481 * BUG_ON(!list_empty).
483 * FIXME: mark_buffer_dirty_inode() is a data-plane operation. It should
484 * take an address_space, not an inode. And it should be called
485 * mark_buffer_dirty_fsync() to clearly define why those buffers are being
488 * FIXME: mark_buffer_dirty_inode() doesn't need to add the buffer to the
489 * list if it is already on a list. Because if the buffer is on a list,
490 * it *must* already be on the right one. If not, the filesystem is being
491 * silly. This will save a ton of locking. But first we have to ensure
492 * that buffers are taken *off* the old inode's list when they are freed
493 * (presumably in truncate). That requires careful auditing of all
494 * filesystems (do it inside bforget()). It could also be done by bringing
499 * The buffer's backing address_space's private_lock must be held
501 static void __remove_assoc_queue(struct buffer_head *bh)
503 list_del_init(&bh->b_assoc_buffers);
504 WARN_ON(!bh->b_assoc_map);
505 if (buffer_write_io_error(bh))
506 set_bit(AS_EIO, &bh->b_assoc_map->flags);
507 bh->b_assoc_map = NULL;
510 int inode_has_buffers(struct inode *inode)
512 return !list_empty(&inode->i_data.private_list);
516 * osync is designed to support O_SYNC io. It waits synchronously for
517 * all already-submitted IO to complete, but does not queue any new
518 * writes to the disk.
520 * To do O_SYNC writes, just queue the buffer writes with ll_rw_block as
521 * you dirty the buffers, and then use osync_inode_buffers to wait for
522 * completion. Any other dirty buffers which are not yet queued for
523 * write will not be flushed to disk by the osync.
525 static int osync_buffers_list(spinlock_t *lock, struct list_head *list)
527 struct buffer_head *bh;
533 list_for_each_prev(p, list) {
535 if (buffer_locked(bh)) {
539 if (!buffer_uptodate(bh))
550 void do_thaw_all(unsigned long unused)
552 struct super_block *sb;
553 char b[BDEVNAME_SIZE];
557 list_for_each_entry(sb, &super_blocks, s_list) {
559 spin_unlock(&sb_lock);
560 down_read(&sb->s_umount);
561 while (sb->s_bdev && !thaw_bdev(sb->s_bdev, sb))
562 printk(KERN_WARNING "Emergency Thaw on %s\n",
563 bdevname(sb->s_bdev, b));
564 up_read(&sb->s_umount);
566 if (__put_super_and_need_restart(sb))
569 spin_unlock(&sb_lock);
570 printk(KERN_WARNING "Emergency Thaw complete\n");
574 * emergency_thaw_all -- forcibly thaw every frozen filesystem
576 * Used for emergency unfreeze of all filesystems via SysRq
578 void emergency_thaw_all(void)
580 pdflush_operation(do_thaw_all, 0);
584 * sync_mapping_buffers - write out & wait upon a mapping's "associated" buffers
585 * @mapping: the mapping which wants those buffers written
587 * Starts I/O against the buffers at mapping->private_list, and waits upon
590 * Basically, this is a convenience function for fsync().
591 * @mapping is a file or directory which needs those buffers to be written for
592 * a successful fsync().
594 int sync_mapping_buffers(struct address_space *mapping)
596 struct address_space *buffer_mapping = mapping->assoc_mapping;
598 if (buffer_mapping == NULL || list_empty(&mapping->private_list))
601 return fsync_buffers_list(&buffer_mapping->private_lock,
602 &mapping->private_list);
604 EXPORT_SYMBOL(sync_mapping_buffers);
607 * Called when we've recently written block `bblock', and it is known that
608 * `bblock' was for a buffer_boundary() buffer. This means that the block at
609 * `bblock + 1' is probably a dirty indirect block. Hunt it down and, if it's
610 * dirty, schedule it for IO. So that indirects merge nicely with their data.
612 void write_boundary_block(struct block_device *bdev,
613 sector_t bblock, unsigned blocksize)
615 struct buffer_head *bh = __find_get_block(bdev, bblock + 1, blocksize);
617 if (buffer_dirty(bh))
618 ll_rw_block(WRITE, 1, &bh);
623 void mark_buffer_dirty_inode(struct buffer_head *bh, struct inode *inode)
625 struct address_space *mapping = inode->i_mapping;
626 struct address_space *buffer_mapping = bh->b_page->mapping;
628 mark_buffer_dirty(bh);
629 if (!mapping->assoc_mapping) {
630 mapping->assoc_mapping = buffer_mapping;
632 BUG_ON(mapping->assoc_mapping != buffer_mapping);
634 if (!bh->b_assoc_map) {
635 spin_lock(&buffer_mapping->private_lock);
636 list_move_tail(&bh->b_assoc_buffers,
637 &mapping->private_list);
638 bh->b_assoc_map = mapping;
639 spin_unlock(&buffer_mapping->private_lock);
642 EXPORT_SYMBOL(mark_buffer_dirty_inode);
645 * Mark the page dirty, and set it dirty in the radix tree, and mark the inode
648 * If warn is true, then emit a warning if the page is not uptodate and has
649 * not been truncated.
651 static void __set_page_dirty(struct page *page,
652 struct address_space *mapping, int warn)
654 spin_lock_irq(&mapping->tree_lock);
655 if (page->mapping) { /* Race with truncate? */
656 WARN_ON_ONCE(warn && !PageUptodate(page));
657 account_page_dirtied(page, mapping);
658 radix_tree_tag_set(&mapping->page_tree,
659 page_index(page), PAGECACHE_TAG_DIRTY);
661 spin_unlock_irq(&mapping->tree_lock);
662 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
666 * Add a page to the dirty page list.
668 * It is a sad fact of life that this function is called from several places
669 * deeply under spinlocking. It may not sleep.
671 * If the page has buffers, the uptodate buffers are set dirty, to preserve
672 * dirty-state coherency between the page and the buffers. It the page does
673 * not have buffers then when they are later attached they will all be set
676 * The buffers are dirtied before the page is dirtied. There's a small race
677 * window in which a writepage caller may see the page cleanness but not the
678 * buffer dirtiness. That's fine. If this code were to set the page dirty
679 * before the buffers, a concurrent writepage caller could clear the page dirty
680 * bit, see a bunch of clean buffers and we'd end up with dirty buffers/clean
681 * page on the dirty page list.
683 * We use private_lock to lock against try_to_free_buffers while using the
684 * page's buffer list. Also use this to protect against clean buffers being
685 * added to the page after it was set dirty.
687 * FIXME: may need to call ->reservepage here as well. That's rather up to the
688 * address_space though.
690 int __set_page_dirty_buffers(struct page *page)
693 struct address_space *mapping = page_mapping(page);
695 if (unlikely(!mapping))
696 return !TestSetPageDirty(page);
698 spin_lock(&mapping->private_lock);
699 if (page_has_buffers(page)) {
700 struct buffer_head *head = page_buffers(page);
701 struct buffer_head *bh = head;
704 set_buffer_dirty(bh);
705 bh = bh->b_this_page;
706 } while (bh != head);
708 newly_dirty = !TestSetPageDirty(page);
709 spin_unlock(&mapping->private_lock);
712 __set_page_dirty(page, mapping, 1);
715 EXPORT_SYMBOL(__set_page_dirty_buffers);
718 * Write out and wait upon a list of buffers.
720 * We have conflicting pressures: we want to make sure that all
721 * initially dirty buffers get waited on, but that any subsequently
722 * dirtied buffers don't. After all, we don't want fsync to last
723 * forever if somebody is actively writing to the file.
725 * Do this in two main stages: first we copy dirty buffers to a
726 * temporary inode list, queueing the writes as we go. Then we clean
727 * up, waiting for those writes to complete.
729 * During this second stage, any subsequent updates to the file may end
730 * up refiling the buffer on the original inode's dirty list again, so
731 * there is a chance we will end up with a buffer queued for write but
732 * not yet completed on that list. So, as a final cleanup we go through
733 * the osync code to catch these locked, dirty buffers without requeuing
734 * any newly dirty buffers for write.
736 static int fsync_buffers_list(spinlock_t *lock, struct list_head *list)
738 struct buffer_head *bh;
739 struct list_head tmp;
740 struct address_space *mapping, *prev_mapping = NULL;
743 INIT_LIST_HEAD(&tmp);
746 while (!list_empty(list)) {
747 bh = BH_ENTRY(list->next);
748 mapping = bh->b_assoc_map;
749 __remove_assoc_queue(bh);
750 /* Avoid race with mark_buffer_dirty_inode() which does
751 * a lockless check and we rely on seeing the dirty bit */
753 if (buffer_dirty(bh) || buffer_locked(bh)) {
754 list_add(&bh->b_assoc_buffers, &tmp);
755 bh->b_assoc_map = mapping;
756 if (buffer_dirty(bh)) {
760 * Ensure any pending I/O completes so that
761 * ll_rw_block() actually writes the current
762 * contents - it is a noop if I/O is still in
763 * flight on potentially older contents.
765 ll_rw_block(SWRITE_SYNC_PLUG, 1, &bh);
768 * Kick off IO for the previous mapping. Note
769 * that we will not run the very last mapping,
770 * wait_on_buffer() will do that for us
771 * through sync_buffer().
773 if (prev_mapping && prev_mapping != mapping)
774 blk_run_address_space(prev_mapping);
775 prev_mapping = mapping;
783 while (!list_empty(&tmp)) {
784 bh = BH_ENTRY(tmp.prev);
786 mapping = bh->b_assoc_map;
787 __remove_assoc_queue(bh);
788 /* Avoid race with mark_buffer_dirty_inode() which does
789 * a lockless check and we rely on seeing the dirty bit */
791 if (buffer_dirty(bh)) {
792 list_add(&bh->b_assoc_buffers,
793 &mapping->private_list);
794 bh->b_assoc_map = mapping;
798 if (!buffer_uptodate(bh))
805 err2 = osync_buffers_list(lock, list);
813 * Invalidate any and all dirty buffers on a given inode. We are
814 * probably unmounting the fs, but that doesn't mean we have already
815 * done a sync(). Just drop the buffers from the inode list.
817 * NOTE: we take the inode's blockdev's mapping's private_lock. Which
818 * assumes that all the buffers are against the blockdev. Not true
821 void invalidate_inode_buffers(struct inode *inode)
823 if (inode_has_buffers(inode)) {
824 struct address_space *mapping = &inode->i_data;
825 struct list_head *list = &mapping->private_list;
826 struct address_space *buffer_mapping = mapping->assoc_mapping;
828 spin_lock(&buffer_mapping->private_lock);
829 while (!list_empty(list))
830 __remove_assoc_queue(BH_ENTRY(list->next));
831 spin_unlock(&buffer_mapping->private_lock);
834 EXPORT_SYMBOL(invalidate_inode_buffers);
837 * Remove any clean buffers from the inode's buffer list. This is called
838 * when we're trying to free the inode itself. Those buffers can pin it.
840 * Returns true if all buffers were removed.
842 int remove_inode_buffers(struct inode *inode)
846 if (inode_has_buffers(inode)) {
847 struct address_space *mapping = &inode->i_data;
848 struct list_head *list = &mapping->private_list;
849 struct address_space *buffer_mapping = mapping->assoc_mapping;
851 spin_lock(&buffer_mapping->private_lock);
852 while (!list_empty(list)) {
853 struct buffer_head *bh = BH_ENTRY(list->next);
854 if (buffer_dirty(bh)) {
858 __remove_assoc_queue(bh);
860 spin_unlock(&buffer_mapping->private_lock);
866 * Create the appropriate buffers when given a page for data area and
867 * the size of each buffer.. Use the bh->b_this_page linked list to
868 * follow the buffers created. Return NULL if unable to create more
871 * The retry flag is used to differentiate async IO (paging, swapping)
872 * which may not fail from ordinary buffer allocations.
874 struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size,
877 struct buffer_head *bh, *head;
883 while ((offset -= size) >= 0) {
884 bh = alloc_buffer_head(GFP_NOFS);
889 bh->b_this_page = head;
894 atomic_set(&bh->b_count, 0);
895 bh->b_private = NULL;
898 /* Link the buffer to its page */
899 set_bh_page(bh, page, offset);
901 init_buffer(bh, NULL, NULL);
905 * In case anything failed, we just free everything we got.
911 head = head->b_this_page;
912 free_buffer_head(bh);
917 * Return failure for non-async IO requests. Async IO requests
918 * are not allowed to fail, so we have to wait until buffer heads
919 * become available. But we don't want tasks sleeping with
920 * partially complete buffers, so all were released above.
925 /* We're _really_ low on memory. Now we just
926 * wait for old buffer heads to become free due to
927 * finishing IO. Since this is an async request and
928 * the reserve list is empty, we're sure there are
929 * async buffer heads in use.
934 EXPORT_SYMBOL_GPL(alloc_page_buffers);
937 link_dev_buffers(struct page *page, struct buffer_head *head)
939 struct buffer_head *bh, *tail;
944 bh = bh->b_this_page;
946 tail->b_this_page = head;
947 attach_page_buffers(page, head);
951 * Initialise the state of a blockdev page's buffers.
954 init_page_buffers(struct page *page, struct block_device *bdev,
955 sector_t block, int size)
957 struct buffer_head *head = page_buffers(page);
958 struct buffer_head *bh = head;
959 int uptodate = PageUptodate(page);
962 if (!buffer_mapped(bh)) {
963 init_buffer(bh, NULL, NULL);
965 bh->b_blocknr = block;
967 set_buffer_uptodate(bh);
968 set_buffer_mapped(bh);
971 bh = bh->b_this_page;
972 } while (bh != head);
976 * Create the page-cache page that contains the requested block.
978 * This is user purely for blockdev mappings.
981 grow_dev_page(struct block_device *bdev, sector_t block,
982 pgoff_t index, int size)
984 struct inode *inode = bdev->bd_inode;
986 struct buffer_head *bh;
988 page = find_or_create_page(inode->i_mapping, index,
989 (mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS)|__GFP_MOVABLE);
993 BUG_ON(!PageLocked(page));
995 if (page_has_buffers(page)) {
996 bh = page_buffers(page);
997 if (bh->b_size == size) {
998 init_page_buffers(page, bdev, block, size);
1001 if (!try_to_free_buffers(page))
1006 * Allocate some buffers for this page
1008 bh = alloc_page_buffers(page, size, 0);
1013 * Link the page to the buffers and initialise them. Take the
1014 * lock to be atomic wrt __find_get_block(), which does not
1015 * run under the page lock.
1017 spin_lock(&inode->i_mapping->private_lock);
1018 link_dev_buffers(page, bh);
1019 init_page_buffers(page, bdev, block, size);
1020 spin_unlock(&inode->i_mapping->private_lock);
1026 page_cache_release(page);
1031 * Create buffers for the specified block device block's page. If
1032 * that page was dirty, the buffers are set dirty also.
1035 grow_buffers(struct block_device *bdev, sector_t block, int size)
1044 } while ((size << sizebits) < PAGE_SIZE);
1046 index = block >> sizebits;
1049 * Check for a block which wants to lie outside our maximum possible
1050 * pagecache index. (this comparison is done using sector_t types).
1052 if (unlikely(index != block >> sizebits)) {
1053 char b[BDEVNAME_SIZE];
1055 printk(KERN_ERR "%s: requested out-of-range block %llu for "
1057 __func__, (unsigned long long)block,
1061 block = index << sizebits;
1062 /* Create a page with the proper size buffers.. */
1063 page = grow_dev_page(bdev, block, index, size);
1067 page_cache_release(page);
1071 static struct buffer_head *
1072 __getblk_slow(struct block_device *bdev, sector_t block, int size)
1074 /* Size must be multiple of hard sectorsize */
1075 if (unlikely(size & (bdev_hardsect_size(bdev)-1) ||
1076 (size < 512 || size > PAGE_SIZE))) {
1077 printk(KERN_ERR "getblk(): invalid block size %d requested\n",
1079 printk(KERN_ERR "hardsect size: %d\n",
1080 bdev_hardsect_size(bdev));
1087 struct buffer_head * bh;
1090 bh = __find_get_block(bdev, block, size);
1094 ret = grow_buffers(bdev, block, size);
1103 * The relationship between dirty buffers and dirty pages:
1105 * Whenever a page has any dirty buffers, the page's dirty bit is set, and
1106 * the page is tagged dirty in its radix tree.
1108 * At all times, the dirtiness of the buffers represents the dirtiness of
1109 * subsections of the page. If the page has buffers, the page dirty bit is
1110 * merely a hint about the true dirty state.
1112 * When a page is set dirty in its entirety, all its buffers are marked dirty
1113 * (if the page has buffers).
1115 * When a buffer is marked dirty, its page is dirtied, but the page's other
1118 * Also. When blockdev buffers are explicitly read with bread(), they
1119 * individually become uptodate. But their backing page remains not
1120 * uptodate - even if all of its buffers are uptodate. A subsequent
1121 * block_read_full_page() against that page will discover all the uptodate
1122 * buffers, will set the page uptodate and will perform no I/O.
1126 * mark_buffer_dirty - mark a buffer_head as needing writeout
1127 * @bh: the buffer_head to mark dirty
1129 * mark_buffer_dirty() will set the dirty bit against the buffer, then set its
1130 * backing page dirty, then tag the page as dirty in its address_space's radix
1131 * tree and then attach the address_space's inode to its superblock's dirty
1134 * mark_buffer_dirty() is atomic. It takes bh->b_page->mapping->private_lock,
1135 * mapping->tree_lock and the global inode_lock.
1137 void mark_buffer_dirty(struct buffer_head *bh)
1139 WARN_ON_ONCE(!buffer_uptodate(bh));
1142 * Very *carefully* optimize the it-is-already-dirty case.
1144 * Don't let the final "is it dirty" escape to before we
1145 * perhaps modified the buffer.
1147 if (buffer_dirty(bh)) {
1149 if (buffer_dirty(bh))
1153 if (!test_set_buffer_dirty(bh)) {
1154 struct page *page = bh->b_page;
1155 if (!TestSetPageDirty(page))
1156 __set_page_dirty(page, page_mapping(page), 0);
1161 * Decrement a buffer_head's reference count. If all buffers against a page
1162 * have zero reference count, are clean and unlocked, and if the page is clean
1163 * and unlocked then try_to_free_buffers() may strip the buffers from the page
1164 * in preparation for freeing it (sometimes, rarely, buffers are removed from
1165 * a page but it ends up not being freed, and buffers may later be reattached).
1167 void __brelse(struct buffer_head * buf)
1169 if (atomic_read(&buf->b_count)) {
1173 WARN(1, KERN_ERR "VFS: brelse: Trying to free free buffer\n");
1177 * bforget() is like brelse(), except it discards any
1178 * potentially dirty data.
1180 void __bforget(struct buffer_head *bh)
1182 clear_buffer_dirty(bh);
1183 if (bh->b_assoc_map) {
1184 struct address_space *buffer_mapping = bh->b_page->mapping;
1186 spin_lock(&buffer_mapping->private_lock);
1187 list_del_init(&bh->b_assoc_buffers);
1188 bh->b_assoc_map = NULL;
1189 spin_unlock(&buffer_mapping->private_lock);
1194 static struct buffer_head *__bread_slow(struct buffer_head *bh)
1197 if (buffer_uptodate(bh)) {
1202 bh->b_end_io = end_buffer_read_sync;
1203 submit_bh(READ, bh);
1205 if (buffer_uptodate(bh))
1213 * Per-cpu buffer LRU implementation. To reduce the cost of __find_get_block().
1214 * The bhs[] array is sorted - newest buffer is at bhs[0]. Buffers have their
1215 * refcount elevated by one when they're in an LRU. A buffer can only appear
1216 * once in a particular CPU's LRU. A single buffer can be present in multiple
1217 * CPU's LRUs at the same time.
1219 * This is a transparent caching front-end to sb_bread(), sb_getblk() and
1220 * sb_find_get_block().
1222 * The LRUs themselves only need locking against invalidate_bh_lrus. We use
1223 * a local interrupt disable for that.
1226 #define BH_LRU_SIZE 8
1229 struct buffer_head *bhs[BH_LRU_SIZE];
1232 static DEFINE_PER_CPU(struct bh_lru, bh_lrus) = {{ NULL }};
1235 #define bh_lru_lock() local_irq_disable()
1236 #define bh_lru_unlock() local_irq_enable()
1238 #define bh_lru_lock() preempt_disable()
1239 #define bh_lru_unlock() preempt_enable()
1242 static inline void check_irqs_on(void)
1244 #ifdef irqs_disabled
1245 BUG_ON(irqs_disabled());
1250 * The LRU management algorithm is dopey-but-simple. Sorry.
1252 static void bh_lru_install(struct buffer_head *bh)
1254 struct buffer_head *evictee = NULL;
1259 lru = &__get_cpu_var(bh_lrus);
1260 if (lru->bhs[0] != bh) {
1261 struct buffer_head *bhs[BH_LRU_SIZE];
1267 for (in = 0; in < BH_LRU_SIZE; in++) {
1268 struct buffer_head *bh2 = lru->bhs[in];
1273 if (out >= BH_LRU_SIZE) {
1274 BUG_ON(evictee != NULL);
1281 while (out < BH_LRU_SIZE)
1283 memcpy(lru->bhs, bhs, sizeof(bhs));
1292 * Look up the bh in this cpu's LRU. If it's there, move it to the head.
1294 static struct buffer_head *
1295 lookup_bh_lru(struct block_device *bdev, sector_t block, unsigned size)
1297 struct buffer_head *ret = NULL;
1303 lru = &__get_cpu_var(bh_lrus);
1304 for (i = 0; i < BH_LRU_SIZE; i++) {
1305 struct buffer_head *bh = lru->bhs[i];
1307 if (bh && bh->b_bdev == bdev &&
1308 bh->b_blocknr == block && bh->b_size == size) {
1311 lru->bhs[i] = lru->bhs[i - 1];
1326 * Perform a pagecache lookup for the matching buffer. If it's there, refresh
1327 * it in the LRU and mark it as accessed. If it is not present then return
1330 struct buffer_head *
1331 __find_get_block(struct block_device *bdev, sector_t block, unsigned size)
1333 struct buffer_head *bh = lookup_bh_lru(bdev, block, size);
1336 bh = __find_get_block_slow(bdev, block);
1344 EXPORT_SYMBOL(__find_get_block);
1347 * __getblk will locate (and, if necessary, create) the buffer_head
1348 * which corresponds to the passed block_device, block and size. The
1349 * returned buffer has its reference count incremented.
1351 * __getblk() cannot fail - it just keeps trying. If you pass it an
1352 * illegal block number, __getblk() will happily return a buffer_head
1353 * which represents the non-existent block. Very weird.
1355 * __getblk() will lock up the machine if grow_dev_page's try_to_free_buffers()
1356 * attempt is failing. FIXME, perhaps?
1358 struct buffer_head *
1359 __getblk(struct block_device *bdev, sector_t block, unsigned size)
1361 struct buffer_head *bh = __find_get_block(bdev, block, size);
1365 bh = __getblk_slow(bdev, block, size);
1368 EXPORT_SYMBOL(__getblk);
1371 * Do async read-ahead on a buffer..
1373 void __breadahead(struct block_device *bdev, sector_t block, unsigned size)
1375 struct buffer_head *bh = __getblk(bdev, block, size);
1377 ll_rw_block(READA, 1, &bh);
1381 EXPORT_SYMBOL(__breadahead);
1384 * __bread() - reads a specified block and returns the bh
1385 * @bdev: the block_device to read from
1386 * @block: number of block
1387 * @size: size (in bytes) to read
1389 * Reads a specified block, and returns buffer head that contains it.
1390 * It returns NULL if the block was unreadable.
1392 struct buffer_head *
1393 __bread(struct block_device *bdev, sector_t block, unsigned size)
1395 struct buffer_head *bh = __getblk(bdev, block, size);
1397 if (likely(bh) && !buffer_uptodate(bh))
1398 bh = __bread_slow(bh);
1401 EXPORT_SYMBOL(__bread);
1404 * invalidate_bh_lrus() is called rarely - but not only at unmount.
1405 * This doesn't race because it runs in each cpu either in irq
1406 * or with preempt disabled.
1408 static void invalidate_bh_lru(void *arg)
1410 struct bh_lru *b = &get_cpu_var(bh_lrus);
1413 for (i = 0; i < BH_LRU_SIZE; i++) {
1417 put_cpu_var(bh_lrus);
1420 void invalidate_bh_lrus(void)
1422 on_each_cpu(invalidate_bh_lru, NULL, 1);
1424 EXPORT_SYMBOL_GPL(invalidate_bh_lrus);
1426 void set_bh_page(struct buffer_head *bh,
1427 struct page *page, unsigned long offset)
1430 BUG_ON(offset >= PAGE_SIZE);
1431 if (PageHighMem(page))
1433 * This catches illegal uses and preserves the offset:
1435 bh->b_data = (char *)(0 + offset);
1437 bh->b_data = page_address(page) + offset;
1439 EXPORT_SYMBOL(set_bh_page);
1442 * Called when truncating a buffer on a page completely.
1444 static void discard_buffer(struct buffer_head * bh)
1447 clear_buffer_dirty(bh);
1449 clear_buffer_mapped(bh);
1450 clear_buffer_req(bh);
1451 clear_buffer_new(bh);
1452 clear_buffer_delay(bh);
1453 clear_buffer_unwritten(bh);
1458 * block_invalidatepage - invalidate part of all of a buffer-backed page
1460 * @page: the page which is affected
1461 * @offset: the index of the truncation point
1463 * block_invalidatepage() is called when all or part of the page has become
1464 * invalidatedby a truncate operation.
1466 * block_invalidatepage() does not have to release all buffers, but it must
1467 * ensure that no dirty buffer is left outside @offset and that no I/O
1468 * is underway against any of the blocks which are outside the truncation
1469 * point. Because the caller is about to free (and possibly reuse) those
1472 void block_invalidatepage(struct page *page, unsigned long offset)
1474 struct buffer_head *head, *bh, *next;
1475 unsigned int curr_off = 0;
1477 BUG_ON(!PageLocked(page));
1478 if (!page_has_buffers(page))
1481 head = page_buffers(page);
1484 unsigned int next_off = curr_off + bh->b_size;
1485 next = bh->b_this_page;
1488 * is this block fully invalidated?
1490 if (offset <= curr_off)
1492 curr_off = next_off;
1494 } while (bh != head);
1497 * We release buffers only if the entire page is being invalidated.
1498 * The get_block cached value has been unconditionally invalidated,
1499 * so real IO is not possible anymore.
1502 try_to_release_page(page, 0);
1506 EXPORT_SYMBOL(block_invalidatepage);
1509 * We attach and possibly dirty the buffers atomically wrt
1510 * __set_page_dirty_buffers() via private_lock. try_to_free_buffers
1511 * is already excluded via the page lock.
1513 void create_empty_buffers(struct page *page,
1514 unsigned long blocksize, unsigned long b_state)
1516 struct buffer_head *bh, *head, *tail;
1518 head = alloc_page_buffers(page, blocksize, 1);
1521 bh->b_state |= b_state;
1523 bh = bh->b_this_page;
1525 tail->b_this_page = head;
1527 spin_lock(&page->mapping->private_lock);
1528 if (PageUptodate(page) || PageDirty(page)) {
1531 if (PageDirty(page))
1532 set_buffer_dirty(bh);
1533 if (PageUptodate(page))
1534 set_buffer_uptodate(bh);
1535 bh = bh->b_this_page;
1536 } while (bh != head);
1538 attach_page_buffers(page, head);
1539 spin_unlock(&page->mapping->private_lock);
1541 EXPORT_SYMBOL(create_empty_buffers);
1544 * We are taking a block for data and we don't want any output from any
1545 * buffer-cache aliases starting from return from that function and
1546 * until the moment when something will explicitly mark the buffer
1547 * dirty (hopefully that will not happen until we will free that block ;-)
1548 * We don't even need to mark it not-uptodate - nobody can expect
1549 * anything from a newly allocated buffer anyway. We used to used
1550 * unmap_buffer() for such invalidation, but that was wrong. We definitely
1551 * don't want to mark the alias unmapped, for example - it would confuse
1552 * anyone who might pick it with bread() afterwards...
1554 * Also.. Note that bforget() doesn't lock the buffer. So there can
1555 * be writeout I/O going on against recently-freed buffers. We don't
1556 * wait on that I/O in bforget() - it's more efficient to wait on the I/O
1557 * only if we really need to. That happens here.
1559 void unmap_underlying_metadata(struct block_device *bdev, sector_t block)
1561 struct buffer_head *old_bh;
1565 old_bh = __find_get_block_slow(bdev, block);
1567 clear_buffer_dirty(old_bh);
1568 wait_on_buffer(old_bh);
1569 clear_buffer_req(old_bh);
1573 EXPORT_SYMBOL(unmap_underlying_metadata);
1576 * NOTE! All mapped/uptodate combinations are valid:
1578 * Mapped Uptodate Meaning
1580 * No No "unknown" - must do get_block()
1581 * No Yes "hole" - zero-filled
1582 * Yes No "allocated" - allocated on disk, not read in
1583 * Yes Yes "valid" - allocated and up-to-date in memory.
1585 * "Dirty" is valid only with the last case (mapped+uptodate).
1589 * While block_write_full_page is writing back the dirty buffers under
1590 * the page lock, whoever dirtied the buffers may decide to clean them
1591 * again at any time. We handle that by only looking at the buffer
1592 * state inside lock_buffer().
1594 * If block_write_full_page() is called for regular writeback
1595 * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
1596 * locked buffer. This only can happen if someone has written the buffer
1597 * directly, with submit_bh(). At the address_space level PageWriteback
1598 * prevents this contention from occurring.
1600 static int __block_write_full_page(struct inode *inode, struct page *page,
1601 get_block_t *get_block, struct writeback_control *wbc)
1605 sector_t last_block;
1606 struct buffer_head *bh, *head;
1607 const unsigned blocksize = 1 << inode->i_blkbits;
1608 int nr_underway = 0;
1609 int write_op = (wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE);
1611 BUG_ON(!PageLocked(page));
1613 last_block = (i_size_read(inode) - 1) >> inode->i_blkbits;
1615 if (!page_has_buffers(page)) {
1616 create_empty_buffers(page, blocksize,
1617 (1 << BH_Dirty)|(1 << BH_Uptodate));
1621 * Be very careful. We have no exclusion from __set_page_dirty_buffers
1622 * here, and the (potentially unmapped) buffers may become dirty at
1623 * any time. If a buffer becomes dirty here after we've inspected it
1624 * then we just miss that fact, and the page stays dirty.
1626 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
1627 * handle that here by just cleaning them.
1630 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1631 head = page_buffers(page);
1635 * Get all the dirty buffers mapped to disk addresses and
1636 * handle any aliases from the underlying blockdev's mapping.
1639 if (block > last_block) {
1641 * mapped buffers outside i_size will occur, because
1642 * this page can be outside i_size when there is a
1643 * truncate in progress.
1646 * The buffer was zeroed by block_write_full_page()
1648 clear_buffer_dirty(bh);
1649 set_buffer_uptodate(bh);
1650 } else if ((!buffer_mapped(bh) || buffer_delay(bh)) &&
1652 WARN_ON(bh->b_size != blocksize);
1653 err = get_block(inode, block, bh, 1);
1656 clear_buffer_delay(bh);
1657 if (buffer_new(bh)) {
1658 /* blockdev mappings never come here */
1659 clear_buffer_new(bh);
1660 unmap_underlying_metadata(bh->b_bdev,
1664 bh = bh->b_this_page;
1666 } while (bh != head);
1669 if (!buffer_mapped(bh))
1672 * If it's a fully non-blocking write attempt and we cannot
1673 * lock the buffer then redirty the page. Note that this can
1674 * potentially cause a busy-wait loop from pdflush and kswapd
1675 * activity, but those code paths have their own higher-level
1678 if (wbc->sync_mode != WB_SYNC_NONE || !wbc->nonblocking) {
1680 } else if (!trylock_buffer(bh)) {
1681 redirty_page_for_writepage(wbc, page);
1684 if (test_clear_buffer_dirty(bh)) {
1685 mark_buffer_async_write(bh);
1689 } while ((bh = bh->b_this_page) != head);
1692 * The page and its buffers are protected by PageWriteback(), so we can
1693 * drop the bh refcounts early.
1695 BUG_ON(PageWriteback(page));
1696 set_page_writeback(page);
1699 struct buffer_head *next = bh->b_this_page;
1700 if (buffer_async_write(bh)) {
1701 submit_bh(write_op, bh);
1705 } while (bh != head);
1710 if (nr_underway == 0) {
1712 * The page was marked dirty, but the buffers were
1713 * clean. Someone wrote them back by hand with
1714 * ll_rw_block/submit_bh. A rare case.
1716 end_page_writeback(page);
1719 * The page and buffer_heads can be released at any time from
1727 * ENOSPC, or some other error. We may already have added some
1728 * blocks to the file, so we need to write these out to avoid
1729 * exposing stale data.
1730 * The page is currently locked and not marked for writeback
1733 /* Recovery: lock and submit the mapped buffers */
1735 if (buffer_mapped(bh) && buffer_dirty(bh) &&
1736 !buffer_delay(bh)) {
1738 mark_buffer_async_write(bh);
1741 * The buffer may have been set dirty during
1742 * attachment to a dirty page.
1744 clear_buffer_dirty(bh);
1746 } while ((bh = bh->b_this_page) != head);
1748 BUG_ON(PageWriteback(page));
1749 mapping_set_error(page->mapping, err);
1750 set_page_writeback(page);
1752 struct buffer_head *next = bh->b_this_page;
1753 if (buffer_async_write(bh)) {
1754 clear_buffer_dirty(bh);
1755 submit_bh(write_op, bh);
1759 } while (bh != head);
1765 * If a page has any new buffers, zero them out here, and mark them uptodate
1766 * and dirty so they'll be written out (in order to prevent uninitialised
1767 * block data from leaking). And clear the new bit.
1769 void page_zero_new_buffers(struct page *page, unsigned from, unsigned to)
1771 unsigned int block_start, block_end;
1772 struct buffer_head *head, *bh;
1774 BUG_ON(!PageLocked(page));
1775 if (!page_has_buffers(page))
1778 bh = head = page_buffers(page);
1781 block_end = block_start + bh->b_size;
1783 if (buffer_new(bh)) {
1784 if (block_end > from && block_start < to) {
1785 if (!PageUptodate(page)) {
1786 unsigned start, size;
1788 start = max(from, block_start);
1789 size = min(to, block_end) - start;
1791 zero_user(page, start, size);
1792 set_buffer_uptodate(bh);
1795 clear_buffer_new(bh);
1796 mark_buffer_dirty(bh);
1800 block_start = block_end;
1801 bh = bh->b_this_page;
1802 } while (bh != head);
1804 EXPORT_SYMBOL(page_zero_new_buffers);
1806 static int __block_prepare_write(struct inode *inode, struct page *page,
1807 unsigned from, unsigned to, get_block_t *get_block)
1809 unsigned block_start, block_end;
1812 unsigned blocksize, bbits;
1813 struct buffer_head *bh, *head, *wait[2], **wait_bh=wait;
1815 BUG_ON(!PageLocked(page));
1816 BUG_ON(from > PAGE_CACHE_SIZE);
1817 BUG_ON(to > PAGE_CACHE_SIZE);
1820 blocksize = 1 << inode->i_blkbits;
1821 if (!page_has_buffers(page))
1822 create_empty_buffers(page, blocksize, 0);
1823 head = page_buffers(page);
1825 bbits = inode->i_blkbits;
1826 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - bbits);
1828 for(bh = head, block_start = 0; bh != head || !block_start;
1829 block++, block_start=block_end, bh = bh->b_this_page) {
1830 block_end = block_start + blocksize;
1831 if (block_end <= from || block_start >= to) {
1832 if (PageUptodate(page)) {
1833 if (!buffer_uptodate(bh))
1834 set_buffer_uptodate(bh);
1839 clear_buffer_new(bh);
1840 if (!buffer_mapped(bh)) {
1841 WARN_ON(bh->b_size != blocksize);
1842 err = get_block(inode, block, bh, 1);
1845 if (buffer_new(bh)) {
1846 unmap_underlying_metadata(bh->b_bdev,
1848 if (PageUptodate(page)) {
1849 clear_buffer_new(bh);
1850 set_buffer_uptodate(bh);
1851 mark_buffer_dirty(bh);
1854 if (block_end > to || block_start < from)
1855 zero_user_segments(page,
1861 if (PageUptodate(page)) {
1862 if (!buffer_uptodate(bh))
1863 set_buffer_uptodate(bh);
1866 if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
1867 !buffer_unwritten(bh) &&
1868 (block_start < from || block_end > to)) {
1869 ll_rw_block(READ, 1, &bh);
1874 * If we issued read requests - let them complete.
1876 while(wait_bh > wait) {
1877 wait_on_buffer(*--wait_bh);
1878 if (!buffer_uptodate(*wait_bh))
1882 page_zero_new_buffers(page, from, to);
1886 static int __block_commit_write(struct inode *inode, struct page *page,
1887 unsigned from, unsigned to)
1889 unsigned block_start, block_end;
1892 struct buffer_head *bh, *head;
1894 blocksize = 1 << inode->i_blkbits;
1896 for(bh = head = page_buffers(page), block_start = 0;
1897 bh != head || !block_start;
1898 block_start=block_end, bh = bh->b_this_page) {
1899 block_end = block_start + blocksize;
1900 if (block_end <= from || block_start >= to) {
1901 if (!buffer_uptodate(bh))
1904 set_buffer_uptodate(bh);
1905 mark_buffer_dirty(bh);
1907 clear_buffer_new(bh);
1911 * If this is a partial write which happened to make all buffers
1912 * uptodate then we can optimize away a bogus readpage() for
1913 * the next read(). Here we 'discover' whether the page went
1914 * uptodate as a result of this (potentially partial) write.
1917 SetPageUptodate(page);
1922 * block_write_begin takes care of the basic task of block allocation and
1923 * bringing partial write blocks uptodate first.
1925 * If *pagep is not NULL, then block_write_begin uses the locked page
1926 * at *pagep rather than allocating its own. In this case, the page will
1927 * not be unlocked or deallocated on failure.
1929 int block_write_begin(struct file *file, struct address_space *mapping,
1930 loff_t pos, unsigned len, unsigned flags,
1931 struct page **pagep, void **fsdata,
1932 get_block_t *get_block)
1934 struct inode *inode = mapping->host;
1938 unsigned start, end;
1941 index = pos >> PAGE_CACHE_SHIFT;
1942 start = pos & (PAGE_CACHE_SIZE - 1);
1948 page = grab_cache_page_write_begin(mapping, index, flags);
1955 BUG_ON(!PageLocked(page));
1957 status = __block_prepare_write(inode, page, start, end, get_block);
1958 if (unlikely(status)) {
1959 ClearPageUptodate(page);
1963 page_cache_release(page);
1967 * prepare_write() may have instantiated a few blocks
1968 * outside i_size. Trim these off again. Don't need
1969 * i_size_read because we hold i_mutex.
1971 if (pos + len > inode->i_size)
1972 vmtruncate(inode, inode->i_size);
1979 EXPORT_SYMBOL(block_write_begin);
1981 int block_write_end(struct file *file, struct address_space *mapping,
1982 loff_t pos, unsigned len, unsigned copied,
1983 struct page *page, void *fsdata)
1985 struct inode *inode = mapping->host;
1988 start = pos & (PAGE_CACHE_SIZE - 1);
1990 if (unlikely(copied < len)) {
1992 * The buffers that were written will now be uptodate, so we
1993 * don't have to worry about a readpage reading them and
1994 * overwriting a partial write. However if we have encountered
1995 * a short write and only partially written into a buffer, it
1996 * will not be marked uptodate, so a readpage might come in and
1997 * destroy our partial write.
1999 * Do the simplest thing, and just treat any short write to a
2000 * non uptodate page as a zero-length write, and force the
2001 * caller to redo the whole thing.
2003 if (!PageUptodate(page))
2006 page_zero_new_buffers(page, start+copied, start+len);
2008 flush_dcache_page(page);
2010 /* This could be a short (even 0-length) commit */
2011 __block_commit_write(inode, page, start, start+copied);
2015 EXPORT_SYMBOL(block_write_end);
2017 int generic_write_end(struct file *file, struct address_space *mapping,
2018 loff_t pos, unsigned len, unsigned copied,
2019 struct page *page, void *fsdata)
2021 struct inode *inode = mapping->host;
2022 int i_size_changed = 0;
2024 copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
2027 * No need to use i_size_read() here, the i_size
2028 * cannot change under us because we hold i_mutex.
2030 * But it's important to update i_size while still holding page lock:
2031 * page writeout could otherwise come in and zero beyond i_size.
2033 if (pos+copied > inode->i_size) {
2034 i_size_write(inode, pos+copied);
2039 page_cache_release(page);
2042 * Don't mark the inode dirty under page lock. First, it unnecessarily
2043 * makes the holding time of page lock longer. Second, it forces lock
2044 * ordering of page lock and transaction start for journaling
2048 mark_inode_dirty(inode);
2052 EXPORT_SYMBOL(generic_write_end);
2055 * block_is_partially_uptodate checks whether buffers within a page are
2058 * Returns true if all buffers which correspond to a file portion
2059 * we want to read are uptodate.
2061 int block_is_partially_uptodate(struct page *page, read_descriptor_t *desc,
2064 struct inode *inode = page->mapping->host;
2065 unsigned block_start, block_end, blocksize;
2067 struct buffer_head *bh, *head;
2070 if (!page_has_buffers(page))
2073 blocksize = 1 << inode->i_blkbits;
2074 to = min_t(unsigned, PAGE_CACHE_SIZE - from, desc->count);
2076 if (from < blocksize && to > PAGE_CACHE_SIZE - blocksize)
2079 head = page_buffers(page);
2083 block_end = block_start + blocksize;
2084 if (block_end > from && block_start < to) {
2085 if (!buffer_uptodate(bh)) {
2089 if (block_end >= to)
2092 block_start = block_end;
2093 bh = bh->b_this_page;
2094 } while (bh != head);
2098 EXPORT_SYMBOL(block_is_partially_uptodate);
2101 * Generic "read page" function for block devices that have the normal
2102 * get_block functionality. This is most of the block device filesystems.
2103 * Reads the page asynchronously --- the unlock_buffer() and
2104 * set/clear_buffer_uptodate() functions propagate buffer state into the
2105 * page struct once IO has completed.
2107 int block_read_full_page(struct page *page, get_block_t *get_block)
2109 struct inode *inode = page->mapping->host;
2110 sector_t iblock, lblock;
2111 struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
2112 unsigned int blocksize;
2114 int fully_mapped = 1;
2116 BUG_ON(!PageLocked(page));
2117 blocksize = 1 << inode->i_blkbits;
2118 if (!page_has_buffers(page))
2119 create_empty_buffers(page, blocksize, 0);
2120 head = page_buffers(page);
2122 iblock = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
2123 lblock = (i_size_read(inode)+blocksize-1) >> inode->i_blkbits;
2129 if (buffer_uptodate(bh))
2132 if (!buffer_mapped(bh)) {
2136 if (iblock < lblock) {
2137 WARN_ON(bh->b_size != blocksize);
2138 err = get_block(inode, iblock, bh, 0);
2142 if (!buffer_mapped(bh)) {
2143 zero_user(page, i * blocksize, blocksize);
2145 set_buffer_uptodate(bh);
2149 * get_block() might have updated the buffer
2152 if (buffer_uptodate(bh))
2156 } while (i++, iblock++, (bh = bh->b_this_page) != head);
2159 SetPageMappedToDisk(page);
2163 * All buffers are uptodate - we can set the page uptodate
2164 * as well. But not if get_block() returned an error.
2166 if (!PageError(page))
2167 SetPageUptodate(page);
2172 /* Stage two: lock the buffers */
2173 for (i = 0; i < nr; i++) {
2176 mark_buffer_async_read(bh);
2180 * Stage 3: start the IO. Check for uptodateness
2181 * inside the buffer lock in case another process reading
2182 * the underlying blockdev brought it uptodate (the sct fix).
2184 for (i = 0; i < nr; i++) {
2186 if (buffer_uptodate(bh))
2187 end_buffer_async_read(bh, 1);
2189 submit_bh(READ, bh);
2194 /* utility function for filesystems that need to do work on expanding
2195 * truncates. Uses filesystem pagecache writes to allow the filesystem to
2196 * deal with the hole.
2198 int generic_cont_expand_simple(struct inode *inode, loff_t size)
2200 struct address_space *mapping = inode->i_mapping;
2203 unsigned long limit;
2207 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
2208 if (limit != RLIM_INFINITY && size > (loff_t)limit) {
2209 send_sig(SIGXFSZ, current, 0);
2212 if (size > inode->i_sb->s_maxbytes)
2215 err = pagecache_write_begin(NULL, mapping, size, 0,
2216 AOP_FLAG_UNINTERRUPTIBLE|AOP_FLAG_CONT_EXPAND,
2221 err = pagecache_write_end(NULL, mapping, size, 0, 0, page, fsdata);
2228 static int cont_expand_zero(struct file *file, struct address_space *mapping,
2229 loff_t pos, loff_t *bytes)
2231 struct inode *inode = mapping->host;
2232 unsigned blocksize = 1 << inode->i_blkbits;
2235 pgoff_t index, curidx;
2237 unsigned zerofrom, offset, len;
2240 index = pos >> PAGE_CACHE_SHIFT;
2241 offset = pos & ~PAGE_CACHE_MASK;
2243 while (index > (curidx = (curpos = *bytes)>>PAGE_CACHE_SHIFT)) {
2244 zerofrom = curpos & ~PAGE_CACHE_MASK;
2245 if (zerofrom & (blocksize-1)) {
2246 *bytes |= (blocksize-1);
2249 len = PAGE_CACHE_SIZE - zerofrom;
2251 err = pagecache_write_begin(file, mapping, curpos, len,
2252 AOP_FLAG_UNINTERRUPTIBLE,
2256 zero_user(page, zerofrom, len);
2257 err = pagecache_write_end(file, mapping, curpos, len, len,
2264 balance_dirty_pages_ratelimited(mapping);
2267 /* page covers the boundary, find the boundary offset */
2268 if (index == curidx) {
2269 zerofrom = curpos & ~PAGE_CACHE_MASK;
2270 /* if we will expand the thing last block will be filled */
2271 if (offset <= zerofrom) {
2274 if (zerofrom & (blocksize-1)) {
2275 *bytes |= (blocksize-1);
2278 len = offset - zerofrom;
2280 err = pagecache_write_begin(file, mapping, curpos, len,
2281 AOP_FLAG_UNINTERRUPTIBLE,
2285 zero_user(page, zerofrom, len);
2286 err = pagecache_write_end(file, mapping, curpos, len, len,
2298 * For moronic filesystems that do not allow holes in file.
2299 * We may have to extend the file.
2301 int cont_write_begin(struct file *file, struct address_space *mapping,
2302 loff_t pos, unsigned len, unsigned flags,
2303 struct page **pagep, void **fsdata,
2304 get_block_t *get_block, loff_t *bytes)
2306 struct inode *inode = mapping->host;
2307 unsigned blocksize = 1 << inode->i_blkbits;
2311 err = cont_expand_zero(file, mapping, pos, bytes);
2315 zerofrom = *bytes & ~PAGE_CACHE_MASK;
2316 if (pos+len > *bytes && zerofrom & (blocksize-1)) {
2317 *bytes |= (blocksize-1);
2322 err = block_write_begin(file, mapping, pos, len,
2323 flags, pagep, fsdata, get_block);
2328 int block_prepare_write(struct page *page, unsigned from, unsigned to,
2329 get_block_t *get_block)
2331 struct inode *inode = page->mapping->host;
2332 int err = __block_prepare_write(inode, page, from, to, get_block);
2334 ClearPageUptodate(page);
2338 int block_commit_write(struct page *page, unsigned from, unsigned to)
2340 struct inode *inode = page->mapping->host;
2341 __block_commit_write(inode,page,from,to);
2346 * block_page_mkwrite() is not allowed to change the file size as it gets
2347 * called from a page fault handler when a page is first dirtied. Hence we must
2348 * be careful to check for EOF conditions here. We set the page up correctly
2349 * for a written page which means we get ENOSPC checking when writing into
2350 * holes and correct delalloc and unwritten extent mapping on filesystems that
2351 * support these features.
2353 * We are not allowed to take the i_mutex here so we have to play games to
2354 * protect against truncate races as the page could now be beyond EOF. Because
2355 * vmtruncate() writes the inode size before removing pages, once we have the
2356 * page lock we can determine safely if the page is beyond EOF. If it is not
2357 * beyond EOF, then the page is guaranteed safe against truncation until we
2361 block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
2362 get_block_t get_block)
2364 struct page *page = vmf->page;
2365 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
2368 int ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
2371 size = i_size_read(inode);
2372 if ((page->mapping != inode->i_mapping) ||
2373 (page_offset(page) > size)) {
2374 /* page got truncated out from underneath us */
2378 /* page is wholly or partially inside EOF */
2379 if (((page->index + 1) << PAGE_CACHE_SHIFT) > size)
2380 end = size & ~PAGE_CACHE_MASK;
2382 end = PAGE_CACHE_SIZE;
2384 ret = block_prepare_write(page, 0, end, get_block);
2386 ret = block_commit_write(page, 0, end);
2388 if (unlikely(ret)) {
2391 else /* -ENOSPC, -EIO, etc */
2392 ret = VM_FAULT_SIGBUS;
2401 * nobh_write_begin()'s prereads are special: the buffer_heads are freed
2402 * immediately, while under the page lock. So it needs a special end_io
2403 * handler which does not touch the bh after unlocking it.
2405 static void end_buffer_read_nobh(struct buffer_head *bh, int uptodate)
2407 __end_buffer_read_notouch(bh, uptodate);
2411 * Attach the singly-linked list of buffers created by nobh_write_begin, to
2412 * the page (converting it to circular linked list and taking care of page
2415 static void attach_nobh_buffers(struct page *page, struct buffer_head *head)
2417 struct buffer_head *bh;
2419 BUG_ON(!PageLocked(page));
2421 spin_lock(&page->mapping->private_lock);
2424 if (PageDirty(page))
2425 set_buffer_dirty(bh);
2426 if (!bh->b_this_page)
2427 bh->b_this_page = head;
2428 bh = bh->b_this_page;
2429 } while (bh != head);
2430 attach_page_buffers(page, head);
2431 spin_unlock(&page->mapping->private_lock);
2435 * On entry, the page is fully not uptodate.
2436 * On exit the page is fully uptodate in the areas outside (from,to)
2438 int nobh_write_begin(struct file *file, struct address_space *mapping,
2439 loff_t pos, unsigned len, unsigned flags,
2440 struct page **pagep, void **fsdata,
2441 get_block_t *get_block)
2443 struct inode *inode = mapping->host;
2444 const unsigned blkbits = inode->i_blkbits;
2445 const unsigned blocksize = 1 << blkbits;
2446 struct buffer_head *head, *bh;
2450 unsigned block_in_page;
2451 unsigned block_start, block_end;
2452 sector_t block_in_file;
2455 int is_mapped_to_disk = 1;
2457 index = pos >> PAGE_CACHE_SHIFT;
2458 from = pos & (PAGE_CACHE_SIZE - 1);
2461 page = grab_cache_page_write_begin(mapping, index, flags);
2467 if (page_has_buffers(page)) {
2469 page_cache_release(page);
2471 return block_write_begin(file, mapping, pos, len, flags, pagep,
2475 if (PageMappedToDisk(page))
2479 * Allocate buffers so that we can keep track of state, and potentially
2480 * attach them to the page if an error occurs. In the common case of
2481 * no error, they will just be freed again without ever being attached
2482 * to the page (which is all OK, because we're under the page lock).
2484 * Be careful: the buffer linked list is a NULL terminated one, rather
2485 * than the circular one we're used to.
2487 head = alloc_page_buffers(page, blocksize, 0);
2493 block_in_file = (sector_t)page->index << (PAGE_CACHE_SHIFT - blkbits);
2496 * We loop across all blocks in the page, whether or not they are
2497 * part of the affected region. This is so we can discover if the
2498 * page is fully mapped-to-disk.
2500 for (block_start = 0, block_in_page = 0, bh = head;
2501 block_start < PAGE_CACHE_SIZE;
2502 block_in_page++, block_start += blocksize, bh = bh->b_this_page) {
2505 block_end = block_start + blocksize;
2508 if (block_start >= to)
2510 ret = get_block(inode, block_in_file + block_in_page,
2514 if (!buffer_mapped(bh))
2515 is_mapped_to_disk = 0;
2517 unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
2518 if (PageUptodate(page)) {
2519 set_buffer_uptodate(bh);
2522 if (buffer_new(bh) || !buffer_mapped(bh)) {
2523 zero_user_segments(page, block_start, from,
2527 if (buffer_uptodate(bh))
2528 continue; /* reiserfs does this */
2529 if (block_start < from || block_end > to) {
2531 bh->b_end_io = end_buffer_read_nobh;
2532 submit_bh(READ, bh);
2539 * The page is locked, so these buffers are protected from
2540 * any VM or truncate activity. Hence we don't need to care
2541 * for the buffer_head refcounts.
2543 for (bh = head; bh; bh = bh->b_this_page) {
2545 if (!buffer_uptodate(bh))
2552 if (is_mapped_to_disk)
2553 SetPageMappedToDisk(page);
2555 *fsdata = head; /* to be released by nobh_write_end */
2562 * Error recovery is a bit difficult. We need to zero out blocks that
2563 * were newly allocated, and dirty them to ensure they get written out.
2564 * Buffers need to be attached to the page at this point, otherwise
2565 * the handling of potential IO errors during writeout would be hard
2566 * (could try doing synchronous writeout, but what if that fails too?)
2568 attach_nobh_buffers(page, head);
2569 page_zero_new_buffers(page, from, to);
2573 page_cache_release(page);
2576 if (pos + len > inode->i_size)
2577 vmtruncate(inode, inode->i_size);
2581 EXPORT_SYMBOL(nobh_write_begin);
2583 int nobh_write_end(struct file *file, struct address_space *mapping,
2584 loff_t pos, unsigned len, unsigned copied,
2585 struct page *page, void *fsdata)
2587 struct inode *inode = page->mapping->host;
2588 struct buffer_head *head = fsdata;
2589 struct buffer_head *bh;
2590 BUG_ON(fsdata != NULL && page_has_buffers(page));
2592 if (unlikely(copied < len) && head)
2593 attach_nobh_buffers(page, head);
2594 if (page_has_buffers(page))
2595 return generic_write_end(file, mapping, pos, len,
2596 copied, page, fsdata);
2598 SetPageUptodate(page);
2599 set_page_dirty(page);
2600 if (pos+copied > inode->i_size) {
2601 i_size_write(inode, pos+copied);
2602 mark_inode_dirty(inode);
2606 page_cache_release(page);
2610 head = head->b_this_page;
2611 free_buffer_head(bh);
2616 EXPORT_SYMBOL(nobh_write_end);
2619 * nobh_writepage() - based on block_full_write_page() except
2620 * that it tries to operate without attaching bufferheads to
2623 int nobh_writepage(struct page *page, get_block_t *get_block,
2624 struct writeback_control *wbc)
2626 struct inode * const inode = page->mapping->host;
2627 loff_t i_size = i_size_read(inode);
2628 const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
2632 /* Is the page fully inside i_size? */
2633 if (page->index < end_index)
2636 /* Is the page fully outside i_size? (truncate in progress) */
2637 offset = i_size & (PAGE_CACHE_SIZE-1);
2638 if (page->index >= end_index+1 || !offset) {
2640 * The page may have dirty, unmapped buffers. For example,
2641 * they may have been added in ext3_writepage(). Make them
2642 * freeable here, so the page does not leak.
2645 /* Not really sure about this - do we need this ? */
2646 if (page->mapping->a_ops->invalidatepage)
2647 page->mapping->a_ops->invalidatepage(page, offset);
2650 return 0; /* don't care */
2654 * The page straddles i_size. It must be zeroed out on each and every
2655 * writepage invocation because it may be mmapped. "A file is mapped
2656 * in multiples of the page size. For a file that is not a multiple of
2657 * the page size, the remaining memory is zeroed when mapped, and
2658 * writes to that region are not written out to the file."
2660 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
2662 ret = mpage_writepage(page, get_block, wbc);
2664 ret = __block_write_full_page(inode, page, get_block, wbc);
2667 EXPORT_SYMBOL(nobh_writepage);
2669 int nobh_truncate_page(struct address_space *mapping,
2670 loff_t from, get_block_t *get_block)
2672 pgoff_t index = from >> PAGE_CACHE_SHIFT;
2673 unsigned offset = from & (PAGE_CACHE_SIZE-1);
2676 unsigned length, pos;
2677 struct inode *inode = mapping->host;
2679 struct buffer_head map_bh;
2682 blocksize = 1 << inode->i_blkbits;
2683 length = offset & (blocksize - 1);
2685 /* Block boundary? Nothing to do */
2689 length = blocksize - length;
2690 iblock = (sector_t)index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
2692 page = grab_cache_page(mapping, index);
2697 if (page_has_buffers(page)) {
2700 page_cache_release(page);
2701 return block_truncate_page(mapping, from, get_block);
2704 /* Find the buffer that contains "offset" */
2706 while (offset >= pos) {
2711 err = get_block(inode, iblock, &map_bh, 0);
2714 /* unmapped? It's a hole - nothing to do */
2715 if (!buffer_mapped(&map_bh))
2718 /* Ok, it's mapped. Make sure it's up-to-date */
2719 if (!PageUptodate(page)) {
2720 err = mapping->a_ops->readpage(NULL, page);
2722 page_cache_release(page);
2726 if (!PageUptodate(page)) {
2730 if (page_has_buffers(page))
2733 zero_user(page, offset, length);
2734 set_page_dirty(page);
2739 page_cache_release(page);
2743 EXPORT_SYMBOL(nobh_truncate_page);
2745 int block_truncate_page(struct address_space *mapping,
2746 loff_t from, get_block_t *get_block)
2748 pgoff_t index = from >> PAGE_CACHE_SHIFT;
2749 unsigned offset = from & (PAGE_CACHE_SIZE-1);
2752 unsigned length, pos;
2753 struct inode *inode = mapping->host;
2755 struct buffer_head *bh;
2758 blocksize = 1 << inode->i_blkbits;
2759 length = offset & (blocksize - 1);
2761 /* Block boundary? Nothing to do */
2765 length = blocksize - length;
2766 iblock = (sector_t)index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
2768 page = grab_cache_page(mapping, index);
2773 if (!page_has_buffers(page))
2774 create_empty_buffers(page, blocksize, 0);
2776 /* Find the buffer that contains "offset" */
2777 bh = page_buffers(page);
2779 while (offset >= pos) {
2780 bh = bh->b_this_page;
2786 if (!buffer_mapped(bh)) {
2787 WARN_ON(bh->b_size != blocksize);
2788 err = get_block(inode, iblock, bh, 0);
2791 /* unmapped? It's a hole - nothing to do */
2792 if (!buffer_mapped(bh))
2796 /* Ok, it's mapped. Make sure it's up-to-date */
2797 if (PageUptodate(page))
2798 set_buffer_uptodate(bh);
2800 if (!buffer_uptodate(bh) && !buffer_delay(bh) && !buffer_unwritten(bh)) {
2802 ll_rw_block(READ, 1, &bh);
2804 /* Uhhuh. Read error. Complain and punt. */
2805 if (!buffer_uptodate(bh))
2809 zero_user(page, offset, length);
2810 mark_buffer_dirty(bh);
2815 page_cache_release(page);
2821 * The generic ->writepage function for buffer-backed address_spaces
2823 int block_write_full_page(struct page *page, get_block_t *get_block,
2824 struct writeback_control *wbc)
2826 struct inode * const inode = page->mapping->host;
2827 loff_t i_size = i_size_read(inode);
2828 const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
2831 /* Is the page fully inside i_size? */
2832 if (page->index < end_index)
2833 return __block_write_full_page(inode, page, get_block, wbc);
2835 /* Is the page fully outside i_size? (truncate in progress) */
2836 offset = i_size & (PAGE_CACHE_SIZE-1);
2837 if (page->index >= end_index+1 || !offset) {
2839 * The page may have dirty, unmapped buffers. For example,
2840 * they may have been added in ext3_writepage(). Make them
2841 * freeable here, so the page does not leak.
2843 do_invalidatepage(page, 0);
2845 return 0; /* don't care */
2849 * The page straddles i_size. It must be zeroed out on each and every
2850 * writepage invokation because it may be mmapped. "A file is mapped
2851 * in multiples of the page size. For a file that is not a multiple of
2852 * the page size, the remaining memory is zeroed when mapped, and
2853 * writes to that region are not written out to the file."
2855 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
2856 return __block_write_full_page(inode, page, get_block, wbc);
2859 sector_t generic_block_bmap(struct address_space *mapping, sector_t block,
2860 get_block_t *get_block)
2862 struct buffer_head tmp;
2863 struct inode *inode = mapping->host;
2866 tmp.b_size = 1 << inode->i_blkbits;
2867 get_block(inode, block, &tmp, 0);
2868 return tmp.b_blocknr;
2871 static void end_bio_bh_io_sync(struct bio *bio, int err)
2873 struct buffer_head *bh = bio->bi_private;
2875 if (err == -EOPNOTSUPP) {
2876 set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
2877 set_bit(BH_Eopnotsupp, &bh->b_state);
2880 if (unlikely (test_bit(BIO_QUIET,&bio->bi_flags)))
2881 set_bit(BH_Quiet, &bh->b_state);
2883 bh->b_end_io(bh, test_bit(BIO_UPTODATE, &bio->bi_flags));
2887 int submit_bh(int rw, struct buffer_head * bh)
2892 BUG_ON(!buffer_locked(bh));
2893 BUG_ON(!buffer_mapped(bh));
2894 BUG_ON(!bh->b_end_io);
2897 * Mask in barrier bit for a write (could be either a WRITE or a
2900 if (buffer_ordered(bh) && (rw & WRITE))
2901 rw |= WRITE_BARRIER;
2904 * Only clear out a write error when rewriting
2906 if (test_set_buffer_req(bh) && (rw & WRITE))
2907 clear_buffer_write_io_error(bh);
2910 * from here on down, it's all bio -- do the initial mapping,
2911 * submit_bio -> generic_make_request may further map this bio around
2913 bio = bio_alloc(GFP_NOIO, 1);
2915 bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
2916 bio->bi_bdev = bh->b_bdev;
2917 bio->bi_io_vec[0].bv_page = bh->b_page;
2918 bio->bi_io_vec[0].bv_len = bh->b_size;
2919 bio->bi_io_vec[0].bv_offset = bh_offset(bh);
2923 bio->bi_size = bh->b_size;
2925 bio->bi_end_io = end_bio_bh_io_sync;
2926 bio->bi_private = bh;
2929 submit_bio(rw, bio);
2931 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2939 * ll_rw_block: low-level access to block devices (DEPRECATED)
2940 * @rw: whether to %READ or %WRITE or %SWRITE or maybe %READA (readahead)
2941 * @nr: number of &struct buffer_heads in the array
2942 * @bhs: array of pointers to &struct buffer_head
2944 * ll_rw_block() takes an array of pointers to &struct buffer_heads, and
2945 * requests an I/O operation on them, either a %READ or a %WRITE. The third
2946 * %SWRITE is like %WRITE only we make sure that the *current* data in buffers
2947 * are sent to disk. The fourth %READA option is described in the documentation
2948 * for generic_make_request() which ll_rw_block() calls.
2950 * This function drops any buffer that it cannot get a lock on (with the
2951 * BH_Lock state bit) unless SWRITE is required, any buffer that appears to be
2952 * clean when doing a write request, and any buffer that appears to be
2953 * up-to-date when doing read request. Further it marks as clean buffers that
2954 * are processed for writing (the buffer cache won't assume that they are
2955 * actually clean until the buffer gets unlocked).
2957 * ll_rw_block sets b_end_io to simple completion handler that marks
2958 * the buffer up-to-date (if approriate), unlocks the buffer and wakes
2961 * All of the buffers must be for the same device, and must also be a
2962 * multiple of the current approved size for the device.
2964 void ll_rw_block(int rw, int nr, struct buffer_head *bhs[])
2968 for (i = 0; i < nr; i++) {
2969 struct buffer_head *bh = bhs[i];
2971 if (rw == SWRITE || rw == SWRITE_SYNC || rw == SWRITE_SYNC_PLUG)
2973 else if (!trylock_buffer(bh))
2976 if (rw == WRITE || rw == SWRITE || rw == SWRITE_SYNC ||
2977 rw == SWRITE_SYNC_PLUG) {
2978 if (test_clear_buffer_dirty(bh)) {
2979 bh->b_end_io = end_buffer_write_sync;
2981 if (rw == SWRITE_SYNC)
2982 submit_bh(WRITE_SYNC, bh);
2984 submit_bh(WRITE, bh);
2988 if (!buffer_uptodate(bh)) {
2989 bh->b_end_io = end_buffer_read_sync;
3000 * For a data-integrity writeout, we need to wait upon any in-progress I/O
3001 * and then start new I/O and then wait upon it. The caller must have a ref on
3004 int sync_dirty_buffer(struct buffer_head *bh)
3008 WARN_ON(atomic_read(&bh->b_count) < 1);
3010 if (test_clear_buffer_dirty(bh)) {
3012 bh->b_end_io = end_buffer_write_sync;
3013 ret = submit_bh(WRITE_SYNC, bh);
3015 if (buffer_eopnotsupp(bh)) {
3016 clear_buffer_eopnotsupp(bh);
3019 if (!ret && !buffer_uptodate(bh))
3028 * try_to_free_buffers() checks if all the buffers on this particular page
3029 * are unused, and releases them if so.
3031 * Exclusion against try_to_free_buffers may be obtained by either
3032 * locking the page or by holding its mapping's private_lock.
3034 * If the page is dirty but all the buffers are clean then we need to
3035 * be sure to mark the page clean as well. This is because the page
3036 * may be against a block device, and a later reattachment of buffers
3037 * to a dirty page will set *all* buffers dirty. Which would corrupt
3038 * filesystem data on the same device.
3040 * The same applies to regular filesystem pages: if all the buffers are
3041 * clean then we set the page clean and proceed. To do that, we require
3042 * total exclusion from __set_page_dirty_buffers(). That is obtained with
3045 * try_to_free_buffers() is non-blocking.
3047 static inline int buffer_busy(struct buffer_head *bh)
3049 return atomic_read(&bh->b_count) |
3050 (bh->b_state & ((1 << BH_Dirty) | (1 << BH_Lock)));
3054 drop_buffers(struct page *page, struct buffer_head **buffers_to_free)
3056 struct buffer_head *head = page_buffers(page);
3057 struct buffer_head *bh;
3061 if (buffer_write_io_error(bh) && page->mapping)
3062 set_bit(AS_EIO, &page->mapping->flags);
3063 if (buffer_busy(bh))
3065 bh = bh->b_this_page;
3066 } while (bh != head);
3069 struct buffer_head *next = bh->b_this_page;
3071 if (bh->b_assoc_map)
3072 __remove_assoc_queue(bh);
3074 } while (bh != head);
3075 *buffers_to_free = head;
3076 __clear_page_buffers(page);
3082 int try_to_free_buffers(struct page *page)
3084 struct address_space * const mapping = page->mapping;
3085 struct buffer_head *buffers_to_free = NULL;
3088 BUG_ON(!PageLocked(page));
3089 if (PageWriteback(page))
3092 if (mapping == NULL) { /* can this still happen? */
3093 ret = drop_buffers(page, &buffers_to_free);
3097 spin_lock(&mapping->private_lock);
3098 ret = drop_buffers(page, &buffers_to_free);
3101 * If the filesystem writes its buffers by hand (eg ext3)
3102 * then we can have clean buffers against a dirty page. We
3103 * clean the page here; otherwise the VM will never notice
3104 * that the filesystem did any IO at all.
3106 * Also, during truncate, discard_buffer will have marked all
3107 * the page's buffers clean. We discover that here and clean
3110 * private_lock must be held over this entire operation in order
3111 * to synchronise against __set_page_dirty_buffers and prevent the
3112 * dirty bit from being lost.
3115 cancel_dirty_page(page, PAGE_CACHE_SIZE);
3116 spin_unlock(&mapping->private_lock);
3118 if (buffers_to_free) {
3119 struct buffer_head *bh = buffers_to_free;
3122 struct buffer_head *next = bh->b_this_page;
3123 free_buffer_head(bh);
3125 } while (bh != buffers_to_free);
3129 EXPORT_SYMBOL(try_to_free_buffers);
3131 void block_sync_page(struct page *page)
3133 struct address_space *mapping;
3136 mapping = page_mapping(page);
3138 blk_run_backing_dev(mapping->backing_dev_info, page);
3142 * There are no bdflush tunables left. But distributions are
3143 * still running obsolete flush daemons, so we terminate them here.
3145 * Use of bdflush() is deprecated and will be removed in a future kernel.
3146 * The `pdflush' kernel threads fully replace bdflush daemons and this call.
3148 SYSCALL_DEFINE2(bdflush, int, func, long, data)
3150 static int msg_count;
3152 if (!capable(CAP_SYS_ADMIN))
3155 if (msg_count < 5) {
3158 "warning: process `%s' used the obsolete bdflush"
3159 " system call\n", current->comm);
3160 printk(KERN_INFO "Fix your initscripts?\n");
3169 * Buffer-head allocation
3171 static struct kmem_cache *bh_cachep;
3174 * Once the number of bh's in the machine exceeds this level, we start
3175 * stripping them in writeback.
3177 static int max_buffer_heads;
3179 int buffer_heads_over_limit;
3181 struct bh_accounting {
3182 int nr; /* Number of live bh's */
3183 int ratelimit; /* Limit cacheline bouncing */
3186 static DEFINE_PER_CPU(struct bh_accounting, bh_accounting) = {0, 0};
3188 static void recalc_bh_state(void)
3193 if (__get_cpu_var(bh_accounting).ratelimit++ < 4096)
3195 __get_cpu_var(bh_accounting).ratelimit = 0;
3196 for_each_online_cpu(i)
3197 tot += per_cpu(bh_accounting, i).nr;
3198 buffer_heads_over_limit = (tot > max_buffer_heads);
3201 struct buffer_head *alloc_buffer_head(gfp_t gfp_flags)
3203 struct buffer_head *ret = kmem_cache_alloc(bh_cachep, gfp_flags);
3205 INIT_LIST_HEAD(&ret->b_assoc_buffers);
3206 get_cpu_var(bh_accounting).nr++;
3208 put_cpu_var(bh_accounting);
3212 EXPORT_SYMBOL(alloc_buffer_head);
3214 void free_buffer_head(struct buffer_head *bh)
3216 BUG_ON(!list_empty(&bh->b_assoc_buffers));
3217 kmem_cache_free(bh_cachep, bh);
3218 get_cpu_var(bh_accounting).nr--;
3220 put_cpu_var(bh_accounting);
3222 EXPORT_SYMBOL(free_buffer_head);
3224 static void buffer_exit_cpu(int cpu)
3227 struct bh_lru *b = &per_cpu(bh_lrus, cpu);
3229 for (i = 0; i < BH_LRU_SIZE; i++) {
3233 get_cpu_var(bh_accounting).nr += per_cpu(bh_accounting, cpu).nr;
3234 per_cpu(bh_accounting, cpu).nr = 0;
3235 put_cpu_var(bh_accounting);
3238 static int buffer_cpu_notify(struct notifier_block *self,
3239 unsigned long action, void *hcpu)
3241 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
3242 buffer_exit_cpu((unsigned long)hcpu);
3247 * bh_uptodate_or_lock - Test whether the buffer is uptodate
3248 * @bh: struct buffer_head
3250 * Return true if the buffer is up-to-date and false,
3251 * with the buffer locked, if not.
3253 int bh_uptodate_or_lock(struct buffer_head *bh)
3255 if (!buffer_uptodate(bh)) {
3257 if (!buffer_uptodate(bh))
3263 EXPORT_SYMBOL(bh_uptodate_or_lock);
3266 * bh_submit_read - Submit a locked buffer for reading
3267 * @bh: struct buffer_head
3269 * Returns zero on success and -EIO on error.
3271 int bh_submit_read(struct buffer_head *bh)
3273 BUG_ON(!buffer_locked(bh));
3275 if (buffer_uptodate(bh)) {
3281 bh->b_end_io = end_buffer_read_sync;
3282 submit_bh(READ, bh);
3284 if (buffer_uptodate(bh))
3288 EXPORT_SYMBOL(bh_submit_read);
3291 init_buffer_head(void *data)
3293 struct buffer_head *bh = data;
3295 memset(bh, 0, sizeof(*bh));
3296 INIT_LIST_HEAD(&bh->b_assoc_buffers);
3299 void __init buffer_init(void)
3303 bh_cachep = kmem_cache_create("buffer_head",
3304 sizeof(struct buffer_head), 0,
3305 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
3310 * Limit the bh occupancy to 10% of ZONE_NORMAL
3312 nrpages = (nr_free_buffer_pages() * 10) / 100;
3313 max_buffer_heads = nrpages * (PAGE_SIZE / sizeof(struct buffer_head));
3314 hotcpu_notifier(buffer_cpu_notify, 0);
3317 EXPORT_SYMBOL(__bforget);
3318 EXPORT_SYMBOL(__brelse);
3319 EXPORT_SYMBOL(__wait_on_buffer);
3320 EXPORT_SYMBOL(block_commit_write);
3321 EXPORT_SYMBOL(block_prepare_write);
3322 EXPORT_SYMBOL(block_page_mkwrite);
3323 EXPORT_SYMBOL(block_read_full_page);
3324 EXPORT_SYMBOL(block_sync_page);
3325 EXPORT_SYMBOL(block_truncate_page);
3326 EXPORT_SYMBOL(block_write_full_page);
3327 EXPORT_SYMBOL(cont_write_begin);
3328 EXPORT_SYMBOL(end_buffer_read_sync);
3329 EXPORT_SYMBOL(end_buffer_write_sync);
3330 EXPORT_SYMBOL(file_fsync);
3331 EXPORT_SYMBOL(generic_block_bmap);
3332 EXPORT_SYMBOL(generic_cont_expand_simple);
3333 EXPORT_SYMBOL(init_buffer);
3334 EXPORT_SYMBOL(invalidate_bdev);
3335 EXPORT_SYMBOL(ll_rw_block);
3336 EXPORT_SYMBOL(mark_buffer_dirty);
3337 EXPORT_SYMBOL(submit_bh);
3338 EXPORT_SYMBOL(sync_dirty_buffer);
3339 EXPORT_SYMBOL(unlock_buffer);