Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / fs / buffer.c
blob105d0a7bd74f6696dd746a8ada645b15944b4e5c
1 /*
2 * linux/fs/buffer.c
4 * Copyright (C) 1991, 1992, 2002 Linus Torvalds
5 */
7 /*
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>
23 #include <linux/fs.h>
24 #include <linux/mm.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)
49 inline void
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);
62 smp_mb();
63 bd = bh->b_bdev;
64 if (bd)
65 blk_run_address_space(bd->bd_inode->i_mapping);
66 io_schedule();
67 return 0;
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 smp_mb__before_clear_bit();
80 clear_buffer_locked(bh);
81 smp_mb__after_clear_bit();
82 wake_up_bit(&bh->b_state, BH_Lock);
86 * Block until a buffer comes unlocked. This doesn't stop it
87 * from becoming locked again - you have to lock it yourself
88 * if you want to preserve its state.
90 void __wait_on_buffer(struct buffer_head * bh)
92 wait_on_bit(&bh->b_state, BH_Lock, sync_buffer, TASK_UNINTERRUPTIBLE);
95 static void
96 __clear_page_buffers(struct page *page)
98 ClearPagePrivate(page);
99 set_page_private(page, 0);
100 page_cache_release(page);
103 static void buffer_io_error(struct buffer_head *bh)
105 char b[BDEVNAME_SIZE];
107 printk(KERN_ERR "Buffer I/O error on device %s, logical block %Lu\n",
108 bdevname(bh->b_bdev, b),
109 (unsigned long long)bh->b_blocknr);
113 * End-of-IO handler helper function which does not touch the bh after
114 * unlocking it.
115 * Note: unlock_buffer() sort-of does touch the bh after unlocking it, but
116 * a race there is benign: unlock_buffer() only use the bh's address for
117 * hashing after unlocking the buffer, so it doesn't actually touch the bh
118 * itself.
120 static void __end_buffer_read_notouch(struct buffer_head *bh, int uptodate)
122 if (uptodate) {
123 set_buffer_uptodate(bh);
124 } else {
125 /* This happens, due to failed READA attempts. */
126 clear_buffer_uptodate(bh);
128 unlock_buffer(bh);
132 * Default synchronous end-of-IO handler.. Just mark it up-to-date and
133 * unlock the buffer. This is what ll_rw_block uses too.
135 void end_buffer_read_sync(struct buffer_head *bh, int uptodate)
137 __end_buffer_read_notouch(bh, uptodate);
138 put_bh(bh);
141 void end_buffer_write_sync(struct buffer_head *bh, int uptodate)
143 char b[BDEVNAME_SIZE];
145 if (uptodate) {
146 set_buffer_uptodate(bh);
147 } else {
148 if (!buffer_eopnotsupp(bh) && printk_ratelimit()) {
149 buffer_io_error(bh);
150 printk(KERN_WARNING "lost page write due to "
151 "I/O error on %s\n",
152 bdevname(bh->b_bdev, b));
154 set_buffer_write_io_error(bh);
155 clear_buffer_uptodate(bh);
157 unlock_buffer(bh);
158 put_bh(bh);
162 * Write out and wait upon all the dirty data associated with a block
163 * device via its mapping. Does not take the superblock lock.
165 int sync_blockdev(struct block_device *bdev)
167 int ret = 0;
169 if (bdev)
170 ret = filemap_write_and_wait(bdev->bd_inode->i_mapping);
171 return ret;
173 EXPORT_SYMBOL(sync_blockdev);
176 * Write out and wait upon all dirty data associated with this
177 * device. Filesystem data as well as the underlying block
178 * device. Takes the superblock lock.
180 int fsync_bdev(struct block_device *bdev)
182 struct super_block *sb = get_super(bdev);
183 if (sb) {
184 int res = fsync_super(sb);
185 drop_super(sb);
186 return res;
188 return sync_blockdev(bdev);
192 * freeze_bdev -- lock a filesystem and force it into a consistent state
193 * @bdev: blockdevice to lock
195 * This takes the block device bd_mount_sem to make sure no new mounts
196 * happen on bdev until thaw_bdev() is called.
197 * If a superblock is found on this device, we take the s_umount semaphore
198 * on it to make sure nobody unmounts until the snapshot creation is done.
200 struct super_block *freeze_bdev(struct block_device *bdev)
202 struct super_block *sb;
204 down(&bdev->bd_mount_sem);
205 sb = get_super(bdev);
206 if (sb && !(sb->s_flags & MS_RDONLY)) {
207 sb->s_frozen = SB_FREEZE_WRITE;
208 smp_wmb();
210 __fsync_super(sb);
212 sb->s_frozen = SB_FREEZE_TRANS;
213 smp_wmb();
215 sync_blockdev(sb->s_bdev);
217 if (sb->s_op->write_super_lockfs)
218 sb->s_op->write_super_lockfs(sb);
221 sync_blockdev(bdev);
222 return sb; /* thaw_bdev releases s->s_umount and bd_mount_sem */
224 EXPORT_SYMBOL(freeze_bdev);
227 * thaw_bdev -- unlock filesystem
228 * @bdev: blockdevice to unlock
229 * @sb: associated superblock
231 * Unlocks the filesystem and marks it writeable again after freeze_bdev().
233 void thaw_bdev(struct block_device *bdev, struct super_block *sb)
235 if (sb) {
236 BUG_ON(sb->s_bdev != bdev);
238 if (sb->s_op->unlockfs)
239 sb->s_op->unlockfs(sb);
240 sb->s_frozen = SB_UNFROZEN;
241 smp_wmb();
242 wake_up(&sb->s_wait_unfrozen);
243 drop_super(sb);
246 up(&bdev->bd_mount_sem);
248 EXPORT_SYMBOL(thaw_bdev);
251 * Various filesystems appear to want __find_get_block to be non-blocking.
252 * But it's the page lock which protects the buffers. To get around this,
253 * we get exclusion from try_to_free_buffers with the blockdev mapping's
254 * private_lock.
256 * Hack idea: for the blockdev mapping, i_bufferlist_lock contention
257 * may be quite high. This code could TryLock the page, and if that
258 * succeeds, there is no need to take private_lock. (But if
259 * private_lock is contended then so is mapping->tree_lock).
261 static struct buffer_head *
262 __find_get_block_slow(struct block_device *bdev, sector_t block)
264 struct inode *bd_inode = bdev->bd_inode;
265 struct address_space *bd_mapping = bd_inode->i_mapping;
266 struct buffer_head *ret = NULL;
267 pgoff_t index;
268 struct buffer_head *bh;
269 struct buffer_head *head;
270 struct page *page;
271 int all_mapped = 1;
273 index = block >> (PAGE_CACHE_SHIFT - bd_inode->i_blkbits);
274 page = find_get_page(bd_mapping, index);
275 if (!page)
276 goto out;
278 spin_lock(&bd_mapping->private_lock);
279 if (!page_has_buffers(page))
280 goto out_unlock;
281 head = page_buffers(page);
282 bh = head;
283 do {
284 if (bh->b_blocknr == block) {
285 ret = bh;
286 get_bh(bh);
287 goto out_unlock;
289 if (!buffer_mapped(bh))
290 all_mapped = 0;
291 bh = bh->b_this_page;
292 } while (bh != head);
294 /* we might be here because some of the buffers on this page are
295 * not mapped. This is due to various races between
296 * file io on the block device and getblk. It gets dealt with
297 * elsewhere, don't buffer_error if we had some unmapped buffers
299 if (all_mapped) {
300 printk("__find_get_block_slow() failed. "
301 "block=%llu, b_blocknr=%llu\n",
302 (unsigned long long)block,
303 (unsigned long long)bh->b_blocknr);
304 printk("b_state=0x%08lx, b_size=%zu\n",
305 bh->b_state, bh->b_size);
306 printk("device blocksize: %d\n", 1 << bd_inode->i_blkbits);
308 out_unlock:
309 spin_unlock(&bd_mapping->private_lock);
310 page_cache_release(page);
311 out:
312 return ret;
315 /* If invalidate_buffers() will trash dirty buffers, it means some kind
316 of fs corruption is going on. Trashing dirty data always imply losing
317 information that was supposed to be just stored on the physical layer
318 by the user.
320 Thus invalidate_buffers in general usage is not allwowed to trash
321 dirty buffers. For example ioctl(FLSBLKBUF) expects dirty data to
322 be preserved. These buffers are simply skipped.
324 We also skip buffers which are still in use. For example this can
325 happen if a userspace program is reading the block device.
327 NOTE: In the case where the user removed a removable-media-disk even if
328 there's still dirty data not synced on disk (due a bug in the device driver
329 or due an error of the user), by not destroying the dirty buffers we could
330 generate corruption also on the next media inserted, thus a parameter is
331 necessary to handle this case in the most safe way possible (trying
332 to not corrupt also the new disk inserted with the data belonging to
333 the old now corrupted disk). Also for the ramdisk the natural thing
334 to do in order to release the ramdisk memory is to destroy dirty buffers.
336 These are two special cases. Normal usage imply the device driver
337 to issue a sync on the device (without waiting I/O completion) and
338 then an invalidate_buffers call that doesn't trash dirty buffers.
340 For handling cache coherency with the blkdev pagecache the 'update' case
341 is been introduced. It is needed to re-read from disk any pinned
342 buffer. NOTE: re-reading from disk is destructive so we can do it only
343 when we assume nobody is changing the buffercache under our I/O and when
344 we think the disk contains more recent information than the buffercache.
345 The update == 1 pass marks the buffers we need to update, the update == 2
346 pass does the actual I/O. */
347 void invalidate_bdev(struct block_device *bdev)
349 struct address_space *mapping = bdev->bd_inode->i_mapping;
351 if (mapping->nrpages == 0)
352 return;
354 invalidate_bh_lrus();
355 invalidate_mapping_pages(mapping, 0, -1);
359 * Kick pdflush then try to free up some ZONE_NORMAL memory.
361 static void free_more_memory(void)
363 struct zone **zones;
364 pg_data_t *pgdat;
366 wakeup_pdflush(1024);
367 yield();
369 for_each_online_pgdat(pgdat) {
370 zones = pgdat->node_zonelists[gfp_zone(GFP_NOFS)].zones;
371 if (*zones)
372 try_to_free_pages(zones, 0, GFP_NOFS);
377 * I/O completion handler for block_read_full_page() - pages
378 * which come unlocked at the end of I/O.
380 static void end_buffer_async_read(struct buffer_head *bh, int uptodate)
382 unsigned long flags;
383 struct buffer_head *first;
384 struct buffer_head *tmp;
385 struct page *page;
386 int page_uptodate = 1;
388 BUG_ON(!buffer_async_read(bh));
390 page = bh->b_page;
391 if (uptodate) {
392 set_buffer_uptodate(bh);
393 } else {
394 clear_buffer_uptodate(bh);
395 if (printk_ratelimit())
396 buffer_io_error(bh);
397 SetPageError(page);
401 * Be _very_ careful from here on. Bad things can happen if
402 * two buffer heads end IO at almost the same time and both
403 * decide that the page is now completely done.
405 first = page_buffers(page);
406 local_irq_save(flags);
407 bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
408 clear_buffer_async_read(bh);
409 unlock_buffer(bh);
410 tmp = bh;
411 do {
412 if (!buffer_uptodate(tmp))
413 page_uptodate = 0;
414 if (buffer_async_read(tmp)) {
415 BUG_ON(!buffer_locked(tmp));
416 goto still_busy;
418 tmp = tmp->b_this_page;
419 } while (tmp != bh);
420 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
421 local_irq_restore(flags);
424 * If none of the buffers had errors and they are all
425 * uptodate then we can set the page uptodate.
427 if (page_uptodate && !PageError(page))
428 SetPageUptodate(page);
429 unlock_page(page);
430 return;
432 still_busy:
433 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
434 local_irq_restore(flags);
435 return;
439 * Completion handler for block_write_full_page() - pages which are unlocked
440 * during I/O, and which have PageWriteback cleared upon I/O completion.
442 static void end_buffer_async_write(struct buffer_head *bh, int uptodate)
444 char b[BDEVNAME_SIZE];
445 unsigned long flags;
446 struct buffer_head *first;
447 struct buffer_head *tmp;
448 struct page *page;
450 BUG_ON(!buffer_async_write(bh));
452 page = bh->b_page;
453 if (uptodate) {
454 set_buffer_uptodate(bh);
455 } else {
456 if (printk_ratelimit()) {
457 buffer_io_error(bh);
458 printk(KERN_WARNING "lost page write due to "
459 "I/O error on %s\n",
460 bdevname(bh->b_bdev, b));
462 set_bit(AS_EIO, &page->mapping->flags);
463 set_buffer_write_io_error(bh);
464 clear_buffer_uptodate(bh);
465 SetPageError(page);
468 first = page_buffers(page);
469 local_irq_save(flags);
470 bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
472 clear_buffer_async_write(bh);
473 unlock_buffer(bh);
474 tmp = bh->b_this_page;
475 while (tmp != bh) {
476 if (buffer_async_write(tmp)) {
477 BUG_ON(!buffer_locked(tmp));
478 goto still_busy;
480 tmp = tmp->b_this_page;
482 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
483 local_irq_restore(flags);
484 end_page_writeback(page);
485 return;
487 still_busy:
488 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
489 local_irq_restore(flags);
490 return;
494 * If a page's buffers are under async readin (end_buffer_async_read
495 * completion) then there is a possibility that another thread of
496 * control could lock one of the buffers after it has completed
497 * but while some of the other buffers have not completed. This
498 * locked buffer would confuse end_buffer_async_read() into not unlocking
499 * the page. So the absence of BH_Async_Read tells end_buffer_async_read()
500 * that this buffer is not under async I/O.
502 * The page comes unlocked when it has no locked buffer_async buffers
503 * left.
505 * PageLocked prevents anyone starting new async I/O reads any of
506 * the buffers.
508 * PageWriteback is used to prevent simultaneous writeout of the same
509 * page.
511 * PageLocked prevents anyone from starting writeback of a page which is
512 * under read I/O (PageWriteback is only ever set against a locked page).
514 static void mark_buffer_async_read(struct buffer_head *bh)
516 bh->b_end_io = end_buffer_async_read;
517 set_buffer_async_read(bh);
520 void mark_buffer_async_write(struct buffer_head *bh)
522 bh->b_end_io = end_buffer_async_write;
523 set_buffer_async_write(bh);
525 EXPORT_SYMBOL(mark_buffer_async_write);
529 * fs/buffer.c contains helper functions for buffer-backed address space's
530 * fsync functions. A common requirement for buffer-based filesystems is
531 * that certain data from the backing blockdev needs to be written out for
532 * a successful fsync(). For example, ext2 indirect blocks need to be
533 * written back and waited upon before fsync() returns.
535 * The functions mark_buffer_inode_dirty(), fsync_inode_buffers(),
536 * inode_has_buffers() and invalidate_inode_buffers() are provided for the
537 * management of a list of dependent buffers at ->i_mapping->private_list.
539 * Locking is a little subtle: try_to_free_buffers() will remove buffers
540 * from their controlling inode's queue when they are being freed. But
541 * try_to_free_buffers() will be operating against the *blockdev* mapping
542 * at the time, not against the S_ISREG file which depends on those buffers.
543 * So the locking for private_list is via the private_lock in the address_space
544 * which backs the buffers. Which is different from the address_space
545 * against which the buffers are listed. So for a particular address_space,
546 * mapping->private_lock does *not* protect mapping->private_list! In fact,
547 * mapping->private_list will always be protected by the backing blockdev's
548 * ->private_lock.
550 * Which introduces a requirement: all buffers on an address_space's
551 * ->private_list must be from the same address_space: the blockdev's.
553 * address_spaces which do not place buffers at ->private_list via these
554 * utility functions are free to use private_lock and private_list for
555 * whatever they want. The only requirement is that list_empty(private_list)
556 * be true at clear_inode() time.
558 * FIXME: clear_inode should not call invalidate_inode_buffers(). The
559 * filesystems should do that. invalidate_inode_buffers() should just go
560 * BUG_ON(!list_empty).
562 * FIXME: mark_buffer_dirty_inode() is a data-plane operation. It should
563 * take an address_space, not an inode. And it should be called
564 * mark_buffer_dirty_fsync() to clearly define why those buffers are being
565 * queued up.
567 * FIXME: mark_buffer_dirty_inode() doesn't need to add the buffer to the
568 * list if it is already on a list. Because if the buffer is on a list,
569 * it *must* already be on the right one. If not, the filesystem is being
570 * silly. This will save a ton of locking. But first we have to ensure
571 * that buffers are taken *off* the old inode's list when they are freed
572 * (presumably in truncate). That requires careful auditing of all
573 * filesystems (do it inside bforget()). It could also be done by bringing
574 * b_inode back.
578 * The buffer's backing address_space's private_lock must be held
580 static inline void __remove_assoc_queue(struct buffer_head *bh)
582 list_del_init(&bh->b_assoc_buffers);
583 WARN_ON(!bh->b_assoc_map);
584 if (buffer_write_io_error(bh))
585 set_bit(AS_EIO, &bh->b_assoc_map->flags);
586 bh->b_assoc_map = NULL;
589 int inode_has_buffers(struct inode *inode)
591 return !list_empty(&inode->i_data.private_list);
595 * osync is designed to support O_SYNC io. It waits synchronously for
596 * all already-submitted IO to complete, but does not queue any new
597 * writes to the disk.
599 * To do O_SYNC writes, just queue the buffer writes with ll_rw_block as
600 * you dirty the buffers, and then use osync_inode_buffers to wait for
601 * completion. Any other dirty buffers which are not yet queued for
602 * write will not be flushed to disk by the osync.
604 static int osync_buffers_list(spinlock_t *lock, struct list_head *list)
606 struct buffer_head *bh;
607 struct list_head *p;
608 int err = 0;
610 spin_lock(lock);
611 repeat:
612 list_for_each_prev(p, list) {
613 bh = BH_ENTRY(p);
614 if (buffer_locked(bh)) {
615 get_bh(bh);
616 spin_unlock(lock);
617 wait_on_buffer(bh);
618 if (!buffer_uptodate(bh))
619 err = -EIO;
620 brelse(bh);
621 spin_lock(lock);
622 goto repeat;
625 spin_unlock(lock);
626 return err;
630 <<<<<<< HEAD:fs/buffer.c
631 * sync_mapping_buffers - write out and wait upon a mapping's "associated"
632 * buffers
633 =======
634 * sync_mapping_buffers - write out & wait upon a mapping's "associated" buffers
635 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/buffer.c
636 * @mapping: the mapping which wants those buffers written
638 * Starts I/O against the buffers at mapping->private_list, and waits upon
639 * that I/O.
641 * Basically, this is a convenience function for fsync().
642 * @mapping is a file or directory which needs those buffers to be written for
643 * a successful fsync().
645 int sync_mapping_buffers(struct address_space *mapping)
647 struct address_space *buffer_mapping = mapping->assoc_mapping;
649 if (buffer_mapping == NULL || list_empty(&mapping->private_list))
650 return 0;
652 return fsync_buffers_list(&buffer_mapping->private_lock,
653 &mapping->private_list);
655 EXPORT_SYMBOL(sync_mapping_buffers);
658 * Called when we've recently written block `bblock', and it is known that
659 * `bblock' was for a buffer_boundary() buffer. This means that the block at
660 * `bblock + 1' is probably a dirty indirect block. Hunt it down and, if it's
661 * dirty, schedule it for IO. So that indirects merge nicely with their data.
663 void write_boundary_block(struct block_device *bdev,
664 sector_t bblock, unsigned blocksize)
666 struct buffer_head *bh = __find_get_block(bdev, bblock + 1, blocksize);
667 if (bh) {
668 if (buffer_dirty(bh))
669 ll_rw_block(WRITE, 1, &bh);
670 put_bh(bh);
674 void mark_buffer_dirty_inode(struct buffer_head *bh, struct inode *inode)
676 struct address_space *mapping = inode->i_mapping;
677 struct address_space *buffer_mapping = bh->b_page->mapping;
679 mark_buffer_dirty(bh);
680 if (!mapping->assoc_mapping) {
681 mapping->assoc_mapping = buffer_mapping;
682 } else {
683 BUG_ON(mapping->assoc_mapping != buffer_mapping);
685 if (!bh->b_assoc_map) {
686 spin_lock(&buffer_mapping->private_lock);
687 list_move_tail(&bh->b_assoc_buffers,
688 &mapping->private_list);
689 bh->b_assoc_map = mapping;
690 spin_unlock(&buffer_mapping->private_lock);
693 EXPORT_SYMBOL(mark_buffer_dirty_inode);
696 * Mark the page dirty, and set it dirty in the radix tree, and mark the inode
697 * dirty.
699 * If warn is true, then emit a warning if the page is not uptodate and has
700 * not been truncated.
702 static int __set_page_dirty(struct page *page,
703 struct address_space *mapping, int warn)
705 if (unlikely(!mapping))
706 return !TestSetPageDirty(page);
708 if (TestSetPageDirty(page))
709 return 0;
711 write_lock_irq(&mapping->tree_lock);
712 if (page->mapping) { /* Race with truncate? */
713 WARN_ON_ONCE(warn && !PageUptodate(page));
715 if (mapping_cap_account_dirty(mapping)) {
716 __inc_zone_page_state(page, NR_FILE_DIRTY);
717 __inc_bdi_stat(mapping->backing_dev_info,
718 BDI_RECLAIMABLE);
719 task_io_account_write(PAGE_CACHE_SIZE);
721 radix_tree_tag_set(&mapping->page_tree,
722 page_index(page), PAGECACHE_TAG_DIRTY);
724 write_unlock_irq(&mapping->tree_lock);
725 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
727 return 1;
731 * Add a page to the dirty page list.
733 * It is a sad fact of life that this function is called from several places
734 * deeply under spinlocking. It may not sleep.
736 * If the page has buffers, the uptodate buffers are set dirty, to preserve
737 * dirty-state coherency between the page and the buffers. It the page does
738 * not have buffers then when they are later attached they will all be set
739 * dirty.
741 * The buffers are dirtied before the page is dirtied. There's a small race
742 * window in which a writepage caller may see the page cleanness but not the
743 * buffer dirtiness. That's fine. If this code were to set the page dirty
744 * before the buffers, a concurrent writepage caller could clear the page dirty
745 * bit, see a bunch of clean buffers and we'd end up with dirty buffers/clean
746 * page on the dirty page list.
748 * We use private_lock to lock against try_to_free_buffers while using the
749 * page's buffer list. Also use this to protect against clean buffers being
750 * added to the page after it was set dirty.
752 * FIXME: may need to call ->reservepage here as well. That's rather up to the
753 * address_space though.
755 int __set_page_dirty_buffers(struct page *page)
757 struct address_space *mapping = page_mapping(page);
759 if (unlikely(!mapping))
760 return !TestSetPageDirty(page);
762 spin_lock(&mapping->private_lock);
763 if (page_has_buffers(page)) {
764 struct buffer_head *head = page_buffers(page);
765 struct buffer_head *bh = head;
767 do {
768 set_buffer_dirty(bh);
769 bh = bh->b_this_page;
770 } while (bh != head);
772 spin_unlock(&mapping->private_lock);
774 return __set_page_dirty(page, mapping, 1);
776 EXPORT_SYMBOL(__set_page_dirty_buffers);
779 * Write out and wait upon a list of buffers.
781 * We have conflicting pressures: we want to make sure that all
782 * initially dirty buffers get waited on, but that any subsequently
783 * dirtied buffers don't. After all, we don't want fsync to last
784 * forever if somebody is actively writing to the file.
786 * Do this in two main stages: first we copy dirty buffers to a
787 * temporary inode list, queueing the writes as we go. Then we clean
788 * up, waiting for those writes to complete.
790 * During this second stage, any subsequent updates to the file may end
791 * up refiling the buffer on the original inode's dirty list again, so
792 * there is a chance we will end up with a buffer queued for write but
793 * not yet completed on that list. So, as a final cleanup we go through
794 * the osync code to catch these locked, dirty buffers without requeuing
795 * any newly dirty buffers for write.
797 static int fsync_buffers_list(spinlock_t *lock, struct list_head *list)
799 struct buffer_head *bh;
800 struct list_head tmp;
801 struct address_space *mapping;
802 int err = 0, err2;
804 INIT_LIST_HEAD(&tmp);
806 spin_lock(lock);
807 while (!list_empty(list)) {
808 bh = BH_ENTRY(list->next);
809 mapping = bh->b_assoc_map;
810 __remove_assoc_queue(bh);
811 /* Avoid race with mark_buffer_dirty_inode() which does
812 * a lockless check and we rely on seeing the dirty bit */
813 smp_mb();
814 if (buffer_dirty(bh) || buffer_locked(bh)) {
815 list_add(&bh->b_assoc_buffers, &tmp);
816 bh->b_assoc_map = mapping;
817 if (buffer_dirty(bh)) {
818 get_bh(bh);
819 spin_unlock(lock);
821 * Ensure any pending I/O completes so that
822 * ll_rw_block() actually writes the current
823 * contents - it is a noop if I/O is still in
824 * flight on potentially older contents.
826 ll_rw_block(SWRITE, 1, &bh);
827 brelse(bh);
828 spin_lock(lock);
833 while (!list_empty(&tmp)) {
834 bh = BH_ENTRY(tmp.prev);
835 get_bh(bh);
836 mapping = bh->b_assoc_map;
837 __remove_assoc_queue(bh);
838 /* Avoid race with mark_buffer_dirty_inode() which does
839 * a lockless check and we rely on seeing the dirty bit */
840 smp_mb();
841 if (buffer_dirty(bh)) {
842 list_add(&bh->b_assoc_buffers,
843 <<<<<<< HEAD:fs/buffer.c
844 &bh->b_assoc_map->private_list);
845 =======
846 &mapping->private_list);
847 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/buffer.c
848 bh->b_assoc_map = mapping;
850 spin_unlock(lock);
851 wait_on_buffer(bh);
852 if (!buffer_uptodate(bh))
853 err = -EIO;
854 brelse(bh);
855 spin_lock(lock);
858 spin_unlock(lock);
859 err2 = osync_buffers_list(lock, list);
860 if (err)
861 return err;
862 else
863 return err2;
867 * Invalidate any and all dirty buffers on a given inode. We are
868 * probably unmounting the fs, but that doesn't mean we have already
869 * done a sync(). Just drop the buffers from the inode list.
871 * NOTE: we take the inode's blockdev's mapping's private_lock. Which
872 * assumes that all the buffers are against the blockdev. Not true
873 * for reiserfs.
875 void invalidate_inode_buffers(struct inode *inode)
877 if (inode_has_buffers(inode)) {
878 struct address_space *mapping = &inode->i_data;
879 struct list_head *list = &mapping->private_list;
880 struct address_space *buffer_mapping = mapping->assoc_mapping;
882 spin_lock(&buffer_mapping->private_lock);
883 while (!list_empty(list))
884 __remove_assoc_queue(BH_ENTRY(list->next));
885 spin_unlock(&buffer_mapping->private_lock);
890 * Remove any clean buffers from the inode's buffer list. This is called
891 * when we're trying to free the inode itself. Those buffers can pin it.
893 * Returns true if all buffers were removed.
895 int remove_inode_buffers(struct inode *inode)
897 int ret = 1;
899 if (inode_has_buffers(inode)) {
900 struct address_space *mapping = &inode->i_data;
901 struct list_head *list = &mapping->private_list;
902 struct address_space *buffer_mapping = mapping->assoc_mapping;
904 spin_lock(&buffer_mapping->private_lock);
905 while (!list_empty(list)) {
906 struct buffer_head *bh = BH_ENTRY(list->next);
907 if (buffer_dirty(bh)) {
908 ret = 0;
909 break;
911 __remove_assoc_queue(bh);
913 spin_unlock(&buffer_mapping->private_lock);
915 return ret;
919 * Create the appropriate buffers when given a page for data area and
920 * the size of each buffer.. Use the bh->b_this_page linked list to
921 * follow the buffers created. Return NULL if unable to create more
922 * buffers.
924 * The retry flag is used to differentiate async IO (paging, swapping)
925 * which may not fail from ordinary buffer allocations.
927 struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size,
928 int retry)
930 struct buffer_head *bh, *head;
931 long offset;
933 try_again:
934 head = NULL;
935 offset = PAGE_SIZE;
936 while ((offset -= size) >= 0) {
937 bh = alloc_buffer_head(GFP_NOFS);
938 if (!bh)
939 goto no_grow;
941 bh->b_bdev = NULL;
942 bh->b_this_page = head;
943 bh->b_blocknr = -1;
944 head = bh;
946 bh->b_state = 0;
947 atomic_set(&bh->b_count, 0);
948 bh->b_private = NULL;
949 bh->b_size = size;
951 /* Link the buffer to its page */
952 set_bh_page(bh, page, offset);
954 init_buffer(bh, NULL, NULL);
956 return head;
958 * In case anything failed, we just free everything we got.
960 no_grow:
961 if (head) {
962 do {
963 bh = head;
964 head = head->b_this_page;
965 free_buffer_head(bh);
966 } while (head);
970 * Return failure for non-async IO requests. Async IO requests
971 * are not allowed to fail, so we have to wait until buffer heads
972 * become available. But we don't want tasks sleeping with
973 * partially complete buffers, so all were released above.
975 if (!retry)
976 return NULL;
978 /* We're _really_ low on memory. Now we just
979 * wait for old buffer heads to become free due to
980 * finishing IO. Since this is an async request and
981 * the reserve list is empty, we're sure there are
982 * async buffer heads in use.
984 free_more_memory();
985 goto try_again;
987 EXPORT_SYMBOL_GPL(alloc_page_buffers);
989 static inline void
990 link_dev_buffers(struct page *page, struct buffer_head *head)
992 struct buffer_head *bh, *tail;
994 bh = head;
995 do {
996 tail = bh;
997 bh = bh->b_this_page;
998 } while (bh);
999 tail->b_this_page = head;
1000 attach_page_buffers(page, head);
1004 * Initialise the state of a blockdev page's buffers.
1006 static void
1007 init_page_buffers(struct page *page, struct block_device *bdev,
1008 sector_t block, int size)
1010 struct buffer_head *head = page_buffers(page);
1011 struct buffer_head *bh = head;
1012 int uptodate = PageUptodate(page);
1014 do {
1015 if (!buffer_mapped(bh)) {
1016 init_buffer(bh, NULL, NULL);
1017 bh->b_bdev = bdev;
1018 bh->b_blocknr = block;
1019 if (uptodate)
1020 set_buffer_uptodate(bh);
1021 set_buffer_mapped(bh);
1023 block++;
1024 bh = bh->b_this_page;
1025 } while (bh != head);
1029 * Create the page-cache page that contains the requested block.
1031 * This is user purely for blockdev mappings.
1033 static struct page *
1034 grow_dev_page(struct block_device *bdev, sector_t block,
1035 pgoff_t index, int size)
1037 struct inode *inode = bdev->bd_inode;
1038 struct page *page;
1039 struct buffer_head *bh;
1041 page = find_or_create_page(inode->i_mapping, index,
1042 (mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS)|__GFP_MOVABLE);
1043 if (!page)
1044 return NULL;
1046 BUG_ON(!PageLocked(page));
1048 if (page_has_buffers(page)) {
1049 bh = page_buffers(page);
1050 if (bh->b_size == size) {
1051 init_page_buffers(page, bdev, block, size);
1052 return page;
1054 if (!try_to_free_buffers(page))
1055 goto failed;
1059 * Allocate some buffers for this page
1061 bh = alloc_page_buffers(page, size, 0);
1062 if (!bh)
1063 goto failed;
1066 * Link the page to the buffers and initialise them. Take the
1067 * lock to be atomic wrt __find_get_block(), which does not
1068 * run under the page lock.
1070 spin_lock(&inode->i_mapping->private_lock);
1071 link_dev_buffers(page, bh);
1072 init_page_buffers(page, bdev, block, size);
1073 spin_unlock(&inode->i_mapping->private_lock);
1074 return page;
1076 failed:
1077 BUG();
1078 unlock_page(page);
1079 page_cache_release(page);
1080 return NULL;
1084 * Create buffers for the specified block device block's page. If
1085 * that page was dirty, the buffers are set dirty also.
1087 static int
1088 grow_buffers(struct block_device *bdev, sector_t block, int size)
1090 struct page *page;
1091 pgoff_t index;
1092 int sizebits;
1094 sizebits = -1;
1095 do {
1096 sizebits++;
1097 } while ((size << sizebits) < PAGE_SIZE);
1099 index = block >> sizebits;
1102 * Check for a block which wants to lie outside our maximum possible
1103 * pagecache index. (this comparison is done using sector_t types).
1105 if (unlikely(index != block >> sizebits)) {
1106 char b[BDEVNAME_SIZE];
1108 printk(KERN_ERR "%s: requested out-of-range block %llu for "
1109 "device %s\n",
1110 __FUNCTION__, (unsigned long long)block,
1111 bdevname(bdev, b));
1112 return -EIO;
1114 block = index << sizebits;
1115 /* Create a page with the proper size buffers.. */
1116 page = grow_dev_page(bdev, block, index, size);
1117 if (!page)
1118 return 0;
1119 unlock_page(page);
1120 page_cache_release(page);
1121 return 1;
1124 static struct buffer_head *
1125 __getblk_slow(struct block_device *bdev, sector_t block, int size)
1127 /* Size must be multiple of hard sectorsize */
1128 if (unlikely(size & (bdev_hardsect_size(bdev)-1) ||
1129 (size < 512 || size > PAGE_SIZE))) {
1130 printk(KERN_ERR "getblk(): invalid block size %d requested\n",
1131 size);
1132 printk(KERN_ERR "hardsect size: %d\n",
1133 bdev_hardsect_size(bdev));
1135 dump_stack();
1136 return NULL;
1139 for (;;) {
1140 struct buffer_head * bh;
1141 int ret;
1143 bh = __find_get_block(bdev, block, size);
1144 if (bh)
1145 return bh;
1147 ret = grow_buffers(bdev, block, size);
1148 if (ret < 0)
1149 return NULL;
1150 if (ret == 0)
1151 free_more_memory();
1156 * The relationship between dirty buffers and dirty pages:
1158 * Whenever a page has any dirty buffers, the page's dirty bit is set, and
1159 * the page is tagged dirty in its radix tree.
1161 * At all times, the dirtiness of the buffers represents the dirtiness of
1162 * subsections of the page. If the page has buffers, the page dirty bit is
1163 * merely a hint about the true dirty state.
1165 * When a page is set dirty in its entirety, all its buffers are marked dirty
1166 * (if the page has buffers).
1168 * When a buffer is marked dirty, its page is dirtied, but the page's other
1169 * buffers are not.
1171 * Also. When blockdev buffers are explicitly read with bread(), they
1172 * individually become uptodate. But their backing page remains not
1173 * uptodate - even if all of its buffers are uptodate. A subsequent
1174 * block_read_full_page() against that page will discover all the uptodate
1175 * buffers, will set the page uptodate and will perform no I/O.
1179 * mark_buffer_dirty - mark a buffer_head as needing writeout
1180 * @bh: the buffer_head to mark dirty
1182 * mark_buffer_dirty() will set the dirty bit against the buffer, then set its
1183 * backing page dirty, then tag the page as dirty in its address_space's radix
1184 * tree and then attach the address_space's inode to its superblock's dirty
1185 * inode list.
1187 * mark_buffer_dirty() is atomic. It takes bh->b_page->mapping->private_lock,
1188 * mapping->tree_lock and the global inode_lock.
1190 void mark_buffer_dirty(struct buffer_head *bh)
1192 WARN_ON_ONCE(!buffer_uptodate(bh));
1193 if (!buffer_dirty(bh) && !test_set_buffer_dirty(bh))
1194 __set_page_dirty(bh->b_page, page_mapping(bh->b_page), 0);
1198 * Decrement a buffer_head's reference count. If all buffers against a page
1199 * have zero reference count, are clean and unlocked, and if the page is clean
1200 * and unlocked then try_to_free_buffers() may strip the buffers from the page
1201 * in preparation for freeing it (sometimes, rarely, buffers are removed from
1202 * a page but it ends up not being freed, and buffers may later be reattached).
1204 void __brelse(struct buffer_head * buf)
1206 if (atomic_read(&buf->b_count)) {
1207 put_bh(buf);
1208 return;
1210 printk(KERN_ERR "VFS: brelse: Trying to free free buffer\n");
1211 WARN_ON(1);
1215 * bforget() is like brelse(), except it discards any
1216 * potentially dirty data.
1218 void __bforget(struct buffer_head *bh)
1220 clear_buffer_dirty(bh);
1221 if (bh->b_assoc_map) {
1222 struct address_space *buffer_mapping = bh->b_page->mapping;
1224 spin_lock(&buffer_mapping->private_lock);
1225 list_del_init(&bh->b_assoc_buffers);
1226 bh->b_assoc_map = NULL;
1227 spin_unlock(&buffer_mapping->private_lock);
1229 __brelse(bh);
1232 static struct buffer_head *__bread_slow(struct buffer_head *bh)
1234 lock_buffer(bh);
1235 if (buffer_uptodate(bh)) {
1236 unlock_buffer(bh);
1237 return bh;
1238 } else {
1239 get_bh(bh);
1240 bh->b_end_io = end_buffer_read_sync;
1241 submit_bh(READ, bh);
1242 wait_on_buffer(bh);
1243 if (buffer_uptodate(bh))
1244 return bh;
1246 brelse(bh);
1247 return NULL;
1251 * Per-cpu buffer LRU implementation. To reduce the cost of __find_get_block().
1252 * The bhs[] array is sorted - newest buffer is at bhs[0]. Buffers have their
1253 * refcount elevated by one when they're in an LRU. A buffer can only appear
1254 * once in a particular CPU's LRU. A single buffer can be present in multiple
1255 * CPU's LRUs at the same time.
1257 * This is a transparent caching front-end to sb_bread(), sb_getblk() and
1258 * sb_find_get_block().
1260 * The LRUs themselves only need locking against invalidate_bh_lrus. We use
1261 * a local interrupt disable for that.
1264 #define BH_LRU_SIZE 8
1266 struct bh_lru {
1267 struct buffer_head *bhs[BH_LRU_SIZE];
1270 static DEFINE_PER_CPU(struct bh_lru, bh_lrus) = {{ NULL }};
1272 #ifdef CONFIG_SMP
1273 #define bh_lru_lock() local_irq_disable()
1274 #define bh_lru_unlock() local_irq_enable()
1275 #else
1276 #define bh_lru_lock() preempt_disable()
1277 #define bh_lru_unlock() preempt_enable()
1278 #endif
1280 static inline void check_irqs_on(void)
1282 #ifdef irqs_disabled
1283 BUG_ON(irqs_disabled());
1284 #endif
1288 * The LRU management algorithm is dopey-but-simple. Sorry.
1290 static void bh_lru_install(struct buffer_head *bh)
1292 struct buffer_head *evictee = NULL;
1293 struct bh_lru *lru;
1295 check_irqs_on();
1296 bh_lru_lock();
1297 lru = &__get_cpu_var(bh_lrus);
1298 if (lru->bhs[0] != bh) {
1299 struct buffer_head *bhs[BH_LRU_SIZE];
1300 int in;
1301 int out = 0;
1303 get_bh(bh);
1304 bhs[out++] = bh;
1305 for (in = 0; in < BH_LRU_SIZE; in++) {
1306 struct buffer_head *bh2 = lru->bhs[in];
1308 if (bh2 == bh) {
1309 __brelse(bh2);
1310 } else {
1311 if (out >= BH_LRU_SIZE) {
1312 BUG_ON(evictee != NULL);
1313 evictee = bh2;
1314 } else {
1315 bhs[out++] = bh2;
1319 while (out < BH_LRU_SIZE)
1320 bhs[out++] = NULL;
1321 memcpy(lru->bhs, bhs, sizeof(bhs));
1323 bh_lru_unlock();
1325 if (evictee)
1326 __brelse(evictee);
1330 * Look up the bh in this cpu's LRU. If it's there, move it to the head.
1332 static struct buffer_head *
1333 lookup_bh_lru(struct block_device *bdev, sector_t block, unsigned size)
1335 struct buffer_head *ret = NULL;
1336 struct bh_lru *lru;
1337 unsigned int i;
1339 check_irqs_on();
1340 bh_lru_lock();
1341 lru = &__get_cpu_var(bh_lrus);
1342 for (i = 0; i < BH_LRU_SIZE; i++) {
1343 struct buffer_head *bh = lru->bhs[i];
1345 if (bh && bh->b_bdev == bdev &&
1346 bh->b_blocknr == block && bh->b_size == size) {
1347 if (i) {
1348 while (i) {
1349 lru->bhs[i] = lru->bhs[i - 1];
1350 i--;
1352 lru->bhs[0] = bh;
1354 get_bh(bh);
1355 ret = bh;
1356 break;
1359 bh_lru_unlock();
1360 return ret;
1364 * Perform a pagecache lookup for the matching buffer. If it's there, refresh
1365 * it in the LRU and mark it as accessed. If it is not present then return
1366 * NULL
1368 struct buffer_head *
1369 __find_get_block(struct block_device *bdev, sector_t block, unsigned size)
1371 struct buffer_head *bh = lookup_bh_lru(bdev, block, size);
1373 if (bh == NULL) {
1374 bh = __find_get_block_slow(bdev, block);
1375 if (bh)
1376 bh_lru_install(bh);
1378 if (bh)
1379 touch_buffer(bh);
1380 return bh;
1382 EXPORT_SYMBOL(__find_get_block);
1385 * __getblk will locate (and, if necessary, create) the buffer_head
1386 * which corresponds to the passed block_device, block and size. The
1387 * returned buffer has its reference count incremented.
1389 * __getblk() cannot fail - it just keeps trying. If you pass it an
1390 * illegal block number, __getblk() will happily return a buffer_head
1391 * which represents the non-existent block. Very weird.
1393 * __getblk() will lock up the machine if grow_dev_page's try_to_free_buffers()
1394 * attempt is failing. FIXME, perhaps?
1396 struct buffer_head *
1397 __getblk(struct block_device *bdev, sector_t block, unsigned size)
1399 struct buffer_head *bh = __find_get_block(bdev, block, size);
1401 might_sleep();
1402 if (bh == NULL)
1403 bh = __getblk_slow(bdev, block, size);
1404 return bh;
1406 EXPORT_SYMBOL(__getblk);
1409 * Do async read-ahead on a buffer..
1411 void __breadahead(struct block_device *bdev, sector_t block, unsigned size)
1413 struct buffer_head *bh = __getblk(bdev, block, size);
1414 if (likely(bh)) {
1415 ll_rw_block(READA, 1, &bh);
1416 brelse(bh);
1419 EXPORT_SYMBOL(__breadahead);
1422 * __bread() - reads a specified block and returns the bh
1423 * @bdev: the block_device to read from
1424 * @block: number of block
1425 * @size: size (in bytes) to read
1427 * Reads a specified block, and returns buffer head that contains it.
1428 * It returns NULL if the block was unreadable.
1430 struct buffer_head *
1431 __bread(struct block_device *bdev, sector_t block, unsigned size)
1433 struct buffer_head *bh = __getblk(bdev, block, size);
1435 if (likely(bh) && !buffer_uptodate(bh))
1436 bh = __bread_slow(bh);
1437 return bh;
1439 EXPORT_SYMBOL(__bread);
1442 * invalidate_bh_lrus() is called rarely - but not only at unmount.
1443 * This doesn't race because it runs in each cpu either in irq
1444 * or with preempt disabled.
1446 static void invalidate_bh_lru(void *arg)
1448 struct bh_lru *b = &get_cpu_var(bh_lrus);
1449 int i;
1451 for (i = 0; i < BH_LRU_SIZE; i++) {
1452 brelse(b->bhs[i]);
1453 b->bhs[i] = NULL;
1455 put_cpu_var(bh_lrus);
1458 void invalidate_bh_lrus(void)
1460 on_each_cpu(invalidate_bh_lru, NULL, 1, 1);
1462 EXPORT_SYMBOL_GPL(invalidate_bh_lrus);
1464 void set_bh_page(struct buffer_head *bh,
1465 struct page *page, unsigned long offset)
1467 bh->b_page = page;
1468 BUG_ON(offset >= PAGE_SIZE);
1469 if (PageHighMem(page))
1471 * This catches illegal uses and preserves the offset:
1473 bh->b_data = (char *)(0 + offset);
1474 else
1475 bh->b_data = page_address(page) + offset;
1477 EXPORT_SYMBOL(set_bh_page);
1480 * Called when truncating a buffer on a page completely.
1482 static void discard_buffer(struct buffer_head * bh)
1484 lock_buffer(bh);
1485 clear_buffer_dirty(bh);
1486 bh->b_bdev = NULL;
1487 clear_buffer_mapped(bh);
1488 clear_buffer_req(bh);
1489 clear_buffer_new(bh);
1490 clear_buffer_delay(bh);
1491 clear_buffer_unwritten(bh);
1492 unlock_buffer(bh);
1496 * block_invalidatepage - invalidate part of all of a buffer-backed page
1498 * @page: the page which is affected
1499 * @offset: the index of the truncation point
1501 * block_invalidatepage() is called when all or part of the page has become
1502 * invalidatedby a truncate operation.
1504 * block_invalidatepage() does not have to release all buffers, but it must
1505 * ensure that no dirty buffer is left outside @offset and that no I/O
1506 * is underway against any of the blocks which are outside the truncation
1507 * point. Because the caller is about to free (and possibly reuse) those
1508 * blocks on-disk.
1510 void block_invalidatepage(struct page *page, unsigned long offset)
1512 struct buffer_head *head, *bh, *next;
1513 unsigned int curr_off = 0;
1515 BUG_ON(!PageLocked(page));
1516 if (!page_has_buffers(page))
1517 goto out;
1519 head = page_buffers(page);
1520 bh = head;
1521 do {
1522 unsigned int next_off = curr_off + bh->b_size;
1523 next = bh->b_this_page;
1526 * is this block fully invalidated?
1528 if (offset <= curr_off)
1529 discard_buffer(bh);
1530 curr_off = next_off;
1531 bh = next;
1532 } while (bh != head);
1535 * We release buffers only if the entire page is being invalidated.
1536 * The get_block cached value has been unconditionally invalidated,
1537 * so real IO is not possible anymore.
1539 if (offset == 0)
1540 try_to_release_page(page, 0);
1541 out:
1542 return;
1544 EXPORT_SYMBOL(block_invalidatepage);
1547 * We attach and possibly dirty the buffers atomically wrt
1548 * __set_page_dirty_buffers() via private_lock. try_to_free_buffers
1549 * is already excluded via the page lock.
1551 void create_empty_buffers(struct page *page,
1552 unsigned long blocksize, unsigned long b_state)
1554 struct buffer_head *bh, *head, *tail;
1556 head = alloc_page_buffers(page, blocksize, 1);
1557 bh = head;
1558 do {
1559 bh->b_state |= b_state;
1560 tail = bh;
1561 bh = bh->b_this_page;
1562 } while (bh);
1563 tail->b_this_page = head;
1565 spin_lock(&page->mapping->private_lock);
1566 if (PageUptodate(page) || PageDirty(page)) {
1567 bh = head;
1568 do {
1569 if (PageDirty(page))
1570 set_buffer_dirty(bh);
1571 if (PageUptodate(page))
1572 set_buffer_uptodate(bh);
1573 bh = bh->b_this_page;
1574 } while (bh != head);
1576 attach_page_buffers(page, head);
1577 spin_unlock(&page->mapping->private_lock);
1579 EXPORT_SYMBOL(create_empty_buffers);
1582 * We are taking a block for data and we don't want any output from any
1583 * buffer-cache aliases starting from return from that function and
1584 * until the moment when something will explicitly mark the buffer
1585 * dirty (hopefully that will not happen until we will free that block ;-)
1586 * We don't even need to mark it not-uptodate - nobody can expect
1587 * anything from a newly allocated buffer anyway. We used to used
1588 * unmap_buffer() for such invalidation, but that was wrong. We definitely
1589 * don't want to mark the alias unmapped, for example - it would confuse
1590 * anyone who might pick it with bread() afterwards...
1592 * Also.. Note that bforget() doesn't lock the buffer. So there can
1593 * be writeout I/O going on against recently-freed buffers. We don't
1594 * wait on that I/O in bforget() - it's more efficient to wait on the I/O
1595 * only if we really need to. That happens here.
1597 void unmap_underlying_metadata(struct block_device *bdev, sector_t block)
1599 struct buffer_head *old_bh;
1601 might_sleep();
1603 old_bh = __find_get_block_slow(bdev, block);
1604 if (old_bh) {
1605 clear_buffer_dirty(old_bh);
1606 wait_on_buffer(old_bh);
1607 clear_buffer_req(old_bh);
1608 __brelse(old_bh);
1611 EXPORT_SYMBOL(unmap_underlying_metadata);
1614 * NOTE! All mapped/uptodate combinations are valid:
1616 * Mapped Uptodate Meaning
1618 * No No "unknown" - must do get_block()
1619 * No Yes "hole" - zero-filled
1620 * Yes No "allocated" - allocated on disk, not read in
1621 * Yes Yes "valid" - allocated and up-to-date in memory.
1623 * "Dirty" is valid only with the last case (mapped+uptodate).
1627 * While block_write_full_page is writing back the dirty buffers under
1628 * the page lock, whoever dirtied the buffers may decide to clean them
1629 * again at any time. We handle that by only looking at the buffer
1630 * state inside lock_buffer().
1632 * If block_write_full_page() is called for regular writeback
1633 * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
1634 * locked buffer. This only can happen if someone has written the buffer
1635 * directly, with submit_bh(). At the address_space level PageWriteback
1636 * prevents this contention from occurring.
1638 static int __block_write_full_page(struct inode *inode, struct page *page,
1639 get_block_t *get_block, struct writeback_control *wbc)
1641 int err;
1642 sector_t block;
1643 sector_t last_block;
1644 struct buffer_head *bh, *head;
1645 const unsigned blocksize = 1 << inode->i_blkbits;
1646 int nr_underway = 0;
1648 BUG_ON(!PageLocked(page));
1650 last_block = (i_size_read(inode) - 1) >> inode->i_blkbits;
1652 if (!page_has_buffers(page)) {
1653 create_empty_buffers(page, blocksize,
1654 (1 << BH_Dirty)|(1 << BH_Uptodate));
1658 * Be very careful. We have no exclusion from __set_page_dirty_buffers
1659 * here, and the (potentially unmapped) buffers may become dirty at
1660 * any time. If a buffer becomes dirty here after we've inspected it
1661 * then we just miss that fact, and the page stays dirty.
1663 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
1664 * handle that here by just cleaning them.
1667 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1668 head = page_buffers(page);
1669 bh = head;
1672 * Get all the dirty buffers mapped to disk addresses and
1673 * handle any aliases from the underlying blockdev's mapping.
1675 do {
1676 if (block > last_block) {
1678 * mapped buffers outside i_size will occur, because
1679 * this page can be outside i_size when there is a
1680 * truncate in progress.
1683 * The buffer was zeroed by block_write_full_page()
1685 clear_buffer_dirty(bh);
1686 set_buffer_uptodate(bh);
1687 } else if (!buffer_mapped(bh) && buffer_dirty(bh)) {
1688 WARN_ON(bh->b_size != blocksize);
1689 err = get_block(inode, block, bh, 1);
1690 if (err)
1691 goto recover;
1692 if (buffer_new(bh)) {
1693 /* blockdev mappings never come here */
1694 clear_buffer_new(bh);
1695 unmap_underlying_metadata(bh->b_bdev,
1696 bh->b_blocknr);
1699 bh = bh->b_this_page;
1700 block++;
1701 } while (bh != head);
1703 do {
1704 if (!buffer_mapped(bh))
1705 continue;
1707 * If it's a fully non-blocking write attempt and we cannot
1708 * lock the buffer then redirty the page. Note that this can
1709 * potentially cause a busy-wait loop from pdflush and kswapd
1710 * activity, but those code paths have their own higher-level
1711 * throttling.
1713 if (wbc->sync_mode != WB_SYNC_NONE || !wbc->nonblocking) {
1714 lock_buffer(bh);
1715 } else if (test_set_buffer_locked(bh)) {
1716 redirty_page_for_writepage(wbc, page);
1717 continue;
1719 if (test_clear_buffer_dirty(bh)) {
1720 mark_buffer_async_write(bh);
1721 } else {
1722 unlock_buffer(bh);
1724 } while ((bh = bh->b_this_page) != head);
1727 * The page and its buffers are protected by PageWriteback(), so we can
1728 * drop the bh refcounts early.
1730 BUG_ON(PageWriteback(page));
1731 set_page_writeback(page);
1733 do {
1734 struct buffer_head *next = bh->b_this_page;
1735 if (buffer_async_write(bh)) {
1736 submit_bh(WRITE, bh);
1737 nr_underway++;
1739 bh = next;
1740 } while (bh != head);
1741 unlock_page(page);
1743 err = 0;
1744 done:
1745 if (nr_underway == 0) {
1747 * The page was marked dirty, but the buffers were
1748 * clean. Someone wrote them back by hand with
1749 * ll_rw_block/submit_bh. A rare case.
1751 end_page_writeback(page);
1754 * The page and buffer_heads can be released at any time from
1755 * here on.
1758 return err;
1760 recover:
1762 * ENOSPC, or some other error. We may already have added some
1763 * blocks to the file, so we need to write these out to avoid
1764 * exposing stale data.
1765 * The page is currently locked and not marked for writeback
1767 bh = head;
1768 /* Recovery: lock and submit the mapped buffers */
1769 do {
1770 if (buffer_mapped(bh) && buffer_dirty(bh)) {
1771 lock_buffer(bh);
1772 mark_buffer_async_write(bh);
1773 } else {
1775 * The buffer may have been set dirty during
1776 * attachment to a dirty page.
1778 clear_buffer_dirty(bh);
1780 } while ((bh = bh->b_this_page) != head);
1781 SetPageError(page);
1782 BUG_ON(PageWriteback(page));
1783 mapping_set_error(page->mapping, err);
1784 set_page_writeback(page);
1785 do {
1786 struct buffer_head *next = bh->b_this_page;
1787 if (buffer_async_write(bh)) {
1788 clear_buffer_dirty(bh);
1789 submit_bh(WRITE, bh);
1790 nr_underway++;
1792 bh = next;
1793 } while (bh != head);
1794 unlock_page(page);
1795 goto done;
1799 * If a page has any new buffers, zero them out here, and mark them uptodate
1800 * and dirty so they'll be written out (in order to prevent uninitialised
1801 * block data from leaking). And clear the new bit.
1803 void page_zero_new_buffers(struct page *page, unsigned from, unsigned to)
1805 unsigned int block_start, block_end;
1806 struct buffer_head *head, *bh;
1808 BUG_ON(!PageLocked(page));
1809 if (!page_has_buffers(page))
1810 return;
1812 bh = head = page_buffers(page);
1813 block_start = 0;
1814 do {
1815 block_end = block_start + bh->b_size;
1817 if (buffer_new(bh)) {
1818 if (block_end > from && block_start < to) {
1819 if (!PageUptodate(page)) {
1820 unsigned start, size;
1822 start = max(from, block_start);
1823 size = min(to, block_end) - start;
1825 zero_user(page, start, size);
1826 set_buffer_uptodate(bh);
1829 clear_buffer_new(bh);
1830 mark_buffer_dirty(bh);
1834 block_start = block_end;
1835 bh = bh->b_this_page;
1836 } while (bh != head);
1838 EXPORT_SYMBOL(page_zero_new_buffers);
1840 static int __block_prepare_write(struct inode *inode, struct page *page,
1841 unsigned from, unsigned to, get_block_t *get_block)
1843 unsigned block_start, block_end;
1844 sector_t block;
1845 int err = 0;
1846 unsigned blocksize, bbits;
1847 struct buffer_head *bh, *head, *wait[2], **wait_bh=wait;
1849 BUG_ON(!PageLocked(page));
1850 BUG_ON(from > PAGE_CACHE_SIZE);
1851 BUG_ON(to > PAGE_CACHE_SIZE);
1852 BUG_ON(from > to);
1854 blocksize = 1 << inode->i_blkbits;
1855 if (!page_has_buffers(page))
1856 create_empty_buffers(page, blocksize, 0);
1857 head = page_buffers(page);
1859 bbits = inode->i_blkbits;
1860 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - bbits);
1862 for(bh = head, block_start = 0; bh != head || !block_start;
1863 block++, block_start=block_end, bh = bh->b_this_page) {
1864 block_end = block_start + blocksize;
1865 if (block_end <= from || block_start >= to) {
1866 if (PageUptodate(page)) {
1867 if (!buffer_uptodate(bh))
1868 set_buffer_uptodate(bh);
1870 continue;
1872 if (buffer_new(bh))
1873 clear_buffer_new(bh);
1874 if (!buffer_mapped(bh)) {
1875 WARN_ON(bh->b_size != blocksize);
1876 err = get_block(inode, block, bh, 1);
1877 if (err)
1878 break;
1879 if (buffer_new(bh)) {
1880 unmap_underlying_metadata(bh->b_bdev,
1881 bh->b_blocknr);
1882 if (PageUptodate(page)) {
1883 clear_buffer_new(bh);
1884 set_buffer_uptodate(bh);
1885 mark_buffer_dirty(bh);
1886 continue;
1888 if (block_end > to || block_start < from)
1889 zero_user_segments(page,
1890 to, block_end,
1891 block_start, from);
1892 continue;
1895 if (PageUptodate(page)) {
1896 if (!buffer_uptodate(bh))
1897 set_buffer_uptodate(bh);
1898 continue;
1900 if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
1901 !buffer_unwritten(bh) &&
1902 (block_start < from || block_end > to)) {
1903 ll_rw_block(READ, 1, &bh);
1904 *wait_bh++=bh;
1908 * If we issued read requests - let them complete.
1910 while(wait_bh > wait) {
1911 wait_on_buffer(*--wait_bh);
1912 if (!buffer_uptodate(*wait_bh))
1913 err = -EIO;
1915 if (unlikely(err))
1916 page_zero_new_buffers(page, from, to);
1917 return err;
1920 static int __block_commit_write(struct inode *inode, struct page *page,
1921 unsigned from, unsigned to)
1923 unsigned block_start, block_end;
1924 int partial = 0;
1925 unsigned blocksize;
1926 struct buffer_head *bh, *head;
1928 blocksize = 1 << inode->i_blkbits;
1930 for(bh = head = page_buffers(page), block_start = 0;
1931 bh != head || !block_start;
1932 block_start=block_end, bh = bh->b_this_page) {
1933 block_end = block_start + blocksize;
1934 if (block_end <= from || block_start >= to) {
1935 if (!buffer_uptodate(bh))
1936 partial = 1;
1937 } else {
1938 set_buffer_uptodate(bh);
1939 mark_buffer_dirty(bh);
1941 clear_buffer_new(bh);
1945 * If this is a partial write which happened to make all buffers
1946 * uptodate then we can optimize away a bogus readpage() for
1947 * the next read(). Here we 'discover' whether the page went
1948 * uptodate as a result of this (potentially partial) write.
1950 if (!partial)
1951 SetPageUptodate(page);
1952 return 0;
1956 * block_write_begin takes care of the basic task of block allocation and
1957 * bringing partial write blocks uptodate first.
1959 * If *pagep is not NULL, then block_write_begin uses the locked page
1960 * at *pagep rather than allocating its own. In this case, the page will
1961 * not be unlocked or deallocated on failure.
1963 int block_write_begin(struct file *file, struct address_space *mapping,
1964 loff_t pos, unsigned len, unsigned flags,
1965 struct page **pagep, void **fsdata,
1966 get_block_t *get_block)
1968 struct inode *inode = mapping->host;
1969 int status = 0;
1970 struct page *page;
1971 pgoff_t index;
1972 unsigned start, end;
1973 int ownpage = 0;
1975 index = pos >> PAGE_CACHE_SHIFT;
1976 start = pos & (PAGE_CACHE_SIZE - 1);
1977 end = start + len;
1979 page = *pagep;
1980 if (page == NULL) {
1981 ownpage = 1;
1982 page = __grab_cache_page(mapping, index);
1983 if (!page) {
1984 status = -ENOMEM;
1985 goto out;
1987 *pagep = page;
1988 } else
1989 BUG_ON(!PageLocked(page));
1991 status = __block_prepare_write(inode, page, start, end, get_block);
1992 if (unlikely(status)) {
1993 ClearPageUptodate(page);
1995 if (ownpage) {
1996 unlock_page(page);
1997 page_cache_release(page);
1998 *pagep = NULL;
2001 * prepare_write() may have instantiated a few blocks
2002 * outside i_size. Trim these off again. Don't need
2003 * i_size_read because we hold i_mutex.
2005 if (pos + len > inode->i_size)
2006 vmtruncate(inode, inode->i_size);
2008 goto out;
2011 out:
2012 return status;
2014 EXPORT_SYMBOL(block_write_begin);
2016 int block_write_end(struct file *file, struct address_space *mapping,
2017 loff_t pos, unsigned len, unsigned copied,
2018 struct page *page, void *fsdata)
2020 struct inode *inode = mapping->host;
2021 unsigned start;
2023 start = pos & (PAGE_CACHE_SIZE - 1);
2025 if (unlikely(copied < len)) {
2027 * The buffers that were written will now be uptodate, so we
2028 * don't have to worry about a readpage reading them and
2029 * overwriting a partial write. However if we have encountered
2030 * a short write and only partially written into a buffer, it
2031 * will not be marked uptodate, so a readpage might come in and
2032 * destroy our partial write.
2034 * Do the simplest thing, and just treat any short write to a
2035 * non uptodate page as a zero-length write, and force the
2036 * caller to redo the whole thing.
2038 if (!PageUptodate(page))
2039 copied = 0;
2041 page_zero_new_buffers(page, start+copied, start+len);
2043 flush_dcache_page(page);
2045 /* This could be a short (even 0-length) commit */
2046 __block_commit_write(inode, page, start, start+copied);
2048 return copied;
2050 EXPORT_SYMBOL(block_write_end);
2052 int generic_write_end(struct file *file, struct address_space *mapping,
2053 loff_t pos, unsigned len, unsigned copied,
2054 struct page *page, void *fsdata)
2056 struct inode *inode = mapping->host;
2058 copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
2061 * No need to use i_size_read() here, the i_size
2062 * cannot change under us because we hold i_mutex.
2064 * But it's important to update i_size while still holding page lock:
2065 * page writeout could otherwise come in and zero beyond i_size.
2067 if (pos+copied > inode->i_size) {
2068 i_size_write(inode, pos+copied);
2069 mark_inode_dirty(inode);
2072 unlock_page(page);
2073 page_cache_release(page);
2075 return copied;
2077 EXPORT_SYMBOL(generic_write_end);
2080 * Generic "read page" function for block devices that have the normal
2081 * get_block functionality. This is most of the block device filesystems.
2082 * Reads the page asynchronously --- the unlock_buffer() and
2083 * set/clear_buffer_uptodate() functions propagate buffer state into the
2084 * page struct once IO has completed.
2086 int block_read_full_page(struct page *page, get_block_t *get_block)
2088 struct inode *inode = page->mapping->host;
2089 sector_t iblock, lblock;
2090 struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
2091 unsigned int blocksize;
2092 int nr, i;
2093 int fully_mapped = 1;
2095 BUG_ON(!PageLocked(page));
2096 blocksize = 1 << inode->i_blkbits;
2097 if (!page_has_buffers(page))
2098 create_empty_buffers(page, blocksize, 0);
2099 head = page_buffers(page);
2101 iblock = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
2102 lblock = (i_size_read(inode)+blocksize-1) >> inode->i_blkbits;
2103 bh = head;
2104 nr = 0;
2105 i = 0;
2107 do {
2108 if (buffer_uptodate(bh))
2109 continue;
2111 if (!buffer_mapped(bh)) {
2112 int err = 0;
2114 fully_mapped = 0;
2115 if (iblock < lblock) {
2116 WARN_ON(bh->b_size != blocksize);
2117 err = get_block(inode, iblock, bh, 0);
2118 if (err)
2119 SetPageError(page);
2121 if (!buffer_mapped(bh)) {
2122 zero_user(page, i * blocksize, blocksize);
2123 if (!err)
2124 set_buffer_uptodate(bh);
2125 continue;
2128 * get_block() might have updated the buffer
2129 * synchronously
2131 if (buffer_uptodate(bh))
2132 continue;
2134 arr[nr++] = bh;
2135 } while (i++, iblock++, (bh = bh->b_this_page) != head);
2137 if (fully_mapped)
2138 SetPageMappedToDisk(page);
2140 if (!nr) {
2142 * All buffers are uptodate - we can set the page uptodate
2143 * as well. But not if get_block() returned an error.
2145 if (!PageError(page))
2146 SetPageUptodate(page);
2147 unlock_page(page);
2148 return 0;
2151 /* Stage two: lock the buffers */
2152 for (i = 0; i < nr; i++) {
2153 bh = arr[i];
2154 lock_buffer(bh);
2155 mark_buffer_async_read(bh);
2159 * Stage 3: start the IO. Check for uptodateness
2160 * inside the buffer lock in case another process reading
2161 * the underlying blockdev brought it uptodate (the sct fix).
2163 for (i = 0; i < nr; i++) {
2164 bh = arr[i];
2165 if (buffer_uptodate(bh))
2166 end_buffer_async_read(bh, 1);
2167 else
2168 submit_bh(READ, bh);
2170 return 0;
2173 /* utility function for filesystems that need to do work on expanding
2174 * truncates. Uses filesystem pagecache writes to allow the filesystem to
2175 * deal with the hole.
2177 int generic_cont_expand_simple(struct inode *inode, loff_t size)
2179 struct address_space *mapping = inode->i_mapping;
2180 struct page *page;
2181 void *fsdata;
2182 unsigned long limit;
2183 int err;
2185 err = -EFBIG;
2186 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
2187 if (limit != RLIM_INFINITY && size > (loff_t)limit) {
2188 send_sig(SIGXFSZ, current, 0);
2189 goto out;
2191 if (size > inode->i_sb->s_maxbytes)
2192 goto out;
2194 err = pagecache_write_begin(NULL, mapping, size, 0,
2195 AOP_FLAG_UNINTERRUPTIBLE|AOP_FLAG_CONT_EXPAND,
2196 &page, &fsdata);
2197 if (err)
2198 goto out;
2200 err = pagecache_write_end(NULL, mapping, size, 0, 0, page, fsdata);
2201 BUG_ON(err > 0);
2203 out:
2204 return err;
2207 int cont_expand_zero(struct file *file, struct address_space *mapping,
2208 loff_t pos, loff_t *bytes)
2210 struct inode *inode = mapping->host;
2211 unsigned blocksize = 1 << inode->i_blkbits;
2212 struct page *page;
2213 void *fsdata;
2214 pgoff_t index, curidx;
2215 loff_t curpos;
2216 unsigned zerofrom, offset, len;
2217 int err = 0;
2219 index = pos >> PAGE_CACHE_SHIFT;
2220 offset = pos & ~PAGE_CACHE_MASK;
2222 while (index > (curidx = (curpos = *bytes)>>PAGE_CACHE_SHIFT)) {
2223 zerofrom = curpos & ~PAGE_CACHE_MASK;
2224 if (zerofrom & (blocksize-1)) {
2225 *bytes |= (blocksize-1);
2226 (*bytes)++;
2228 len = PAGE_CACHE_SIZE - zerofrom;
2230 err = pagecache_write_begin(file, mapping, curpos, len,
2231 AOP_FLAG_UNINTERRUPTIBLE,
2232 &page, &fsdata);
2233 if (err)
2234 goto out;
2235 zero_user(page, zerofrom, len);
2236 err = pagecache_write_end(file, mapping, curpos, len, len,
2237 page, fsdata);
2238 if (err < 0)
2239 goto out;
2240 BUG_ON(err != len);
2241 err = 0;
2244 /* page covers the boundary, find the boundary offset */
2245 if (index == curidx) {
2246 zerofrom = curpos & ~PAGE_CACHE_MASK;
2247 /* if we will expand the thing last block will be filled */
2248 if (offset <= zerofrom) {
2249 goto out;
2251 if (zerofrom & (blocksize-1)) {
2252 *bytes |= (blocksize-1);
2253 (*bytes)++;
2255 len = offset - zerofrom;
2257 err = pagecache_write_begin(file, mapping, curpos, len,
2258 AOP_FLAG_UNINTERRUPTIBLE,
2259 &page, &fsdata);
2260 if (err)
2261 goto out;
2262 zero_user(page, zerofrom, len);
2263 err = pagecache_write_end(file, mapping, curpos, len, len,
2264 page, fsdata);
2265 if (err < 0)
2266 goto out;
2267 BUG_ON(err != len);
2268 err = 0;
2270 out:
2271 return err;
2275 * For moronic filesystems that do not allow holes in file.
2276 * We may have to extend the file.
2278 int cont_write_begin(struct file *file, struct address_space *mapping,
2279 loff_t pos, unsigned len, unsigned flags,
2280 struct page **pagep, void **fsdata,
2281 get_block_t *get_block, loff_t *bytes)
2283 struct inode *inode = mapping->host;
2284 unsigned blocksize = 1 << inode->i_blkbits;
2285 unsigned zerofrom;
2286 int err;
2288 err = cont_expand_zero(file, mapping, pos, bytes);
2289 if (err)
2290 goto out;
2292 zerofrom = *bytes & ~PAGE_CACHE_MASK;
2293 if (pos+len > *bytes && zerofrom & (blocksize-1)) {
2294 *bytes |= (blocksize-1);
2295 (*bytes)++;
2298 *pagep = NULL;
2299 err = block_write_begin(file, mapping, pos, len,
2300 flags, pagep, fsdata, get_block);
2301 out:
2302 return err;
2305 int block_prepare_write(struct page *page, unsigned from, unsigned to,
2306 get_block_t *get_block)
2308 struct inode *inode = page->mapping->host;
2309 int err = __block_prepare_write(inode, page, from, to, get_block);
2310 if (err)
2311 ClearPageUptodate(page);
2312 return err;
2315 int block_commit_write(struct page *page, unsigned from, unsigned to)
2317 struct inode *inode = page->mapping->host;
2318 __block_commit_write(inode,page,from,to);
2319 return 0;
2322 int generic_commit_write(struct file *file, struct page *page,
2323 unsigned from, unsigned to)
2325 struct inode *inode = page->mapping->host;
2326 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2327 __block_commit_write(inode,page,from,to);
2329 * No need to use i_size_read() here, the i_size
2330 * cannot change under us because we hold i_mutex.
2332 if (pos > inode->i_size) {
2333 i_size_write(inode, pos);
2334 mark_inode_dirty(inode);
2336 return 0;
2340 * block_page_mkwrite() is not allowed to change the file size as it gets
2341 * called from a page fault handler when a page is first dirtied. Hence we must
2342 * be careful to check for EOF conditions here. We set the page up correctly
2343 * for a written page which means we get ENOSPC checking when writing into
2344 * holes and correct delalloc and unwritten extent mapping on filesystems that
2345 * support these features.
2347 * We are not allowed to take the i_mutex here so we have to play games to
2348 * protect against truncate races as the page could now be beyond EOF. Because
2349 * vmtruncate() writes the inode size before removing pages, once we have the
2350 * page lock we can determine safely if the page is beyond EOF. If it is not
2351 * beyond EOF, then the page is guaranteed safe against truncation until we
2352 * unlock the page.
2355 block_page_mkwrite(struct vm_area_struct *vma, struct page *page,
2356 get_block_t get_block)
2358 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
2359 unsigned long end;
2360 loff_t size;
2361 int ret = -EINVAL;
2363 lock_page(page);
2364 size = i_size_read(inode);
2365 if ((page->mapping != inode->i_mapping) ||
2366 (page_offset(page) > size)) {
2367 /* page got truncated out from underneath us */
2368 goto out_unlock;
2371 /* page is wholly or partially inside EOF */
2372 if (((page->index + 1) << PAGE_CACHE_SHIFT) > size)
2373 end = size & ~PAGE_CACHE_MASK;
2374 else
2375 end = PAGE_CACHE_SIZE;
2377 ret = block_prepare_write(page, 0, end, get_block);
2378 if (!ret)
2379 ret = block_commit_write(page, 0, end);
2381 out_unlock:
2382 unlock_page(page);
2383 return ret;
2387 * nobh_write_begin()'s prereads are special: the buffer_heads are freed
2388 * immediately, while under the page lock. So it needs a special end_io
2389 * handler which does not touch the bh after unlocking it.
2391 static void end_buffer_read_nobh(struct buffer_head *bh, int uptodate)
2393 __end_buffer_read_notouch(bh, uptodate);
2397 * Attach the singly-linked list of buffers created by nobh_write_begin, to
2398 * the page (converting it to circular linked list and taking care of page
2399 * dirty races).
2401 static void attach_nobh_buffers(struct page *page, struct buffer_head *head)
2403 struct buffer_head *bh;
2405 BUG_ON(!PageLocked(page));
2407 spin_lock(&page->mapping->private_lock);
2408 bh = head;
2409 do {
2410 if (PageDirty(page))
2411 set_buffer_dirty(bh);
2412 if (!bh->b_this_page)
2413 bh->b_this_page = head;
2414 bh = bh->b_this_page;
2415 } while (bh != head);
2416 attach_page_buffers(page, head);
2417 spin_unlock(&page->mapping->private_lock);
2421 * On entry, the page is fully not uptodate.
2422 * On exit the page is fully uptodate in the areas outside (from,to)
2424 int nobh_write_begin(struct file *file, struct address_space *mapping,
2425 loff_t pos, unsigned len, unsigned flags,
2426 struct page **pagep, void **fsdata,
2427 get_block_t *get_block)
2429 struct inode *inode = mapping->host;
2430 const unsigned blkbits = inode->i_blkbits;
2431 const unsigned blocksize = 1 << blkbits;
2432 struct buffer_head *head, *bh;
2433 struct page *page;
2434 pgoff_t index;
2435 unsigned from, to;
2436 unsigned block_in_page;
2437 unsigned block_start, block_end;
2438 sector_t block_in_file;
2439 int nr_reads = 0;
2440 int ret = 0;
2441 int is_mapped_to_disk = 1;
2443 index = pos >> PAGE_CACHE_SHIFT;
2444 from = pos & (PAGE_CACHE_SIZE - 1);
2445 to = from + len;
2447 page = __grab_cache_page(mapping, index);
2448 if (!page)
2449 return -ENOMEM;
2450 *pagep = page;
2451 *fsdata = NULL;
2453 if (page_has_buffers(page)) {
2454 unlock_page(page);
2455 page_cache_release(page);
2456 *pagep = NULL;
2457 return block_write_begin(file, mapping, pos, len, flags, pagep,
2458 fsdata, get_block);
2461 if (PageMappedToDisk(page))
2462 return 0;
2465 * Allocate buffers so that we can keep track of state, and potentially
2466 * attach them to the page if an error occurs. In the common case of
2467 * no error, they will just be freed again without ever being attached
2468 * to the page (which is all OK, because we're under the page lock).
2470 * Be careful: the buffer linked list is a NULL terminated one, rather
2471 * than the circular one we're used to.
2473 head = alloc_page_buffers(page, blocksize, 0);
2474 if (!head) {
2475 ret = -ENOMEM;
2476 goto out_release;
2479 block_in_file = (sector_t)page->index << (PAGE_CACHE_SHIFT - blkbits);
2482 * We loop across all blocks in the page, whether or not they are
2483 * part of the affected region. This is so we can discover if the
2484 * page is fully mapped-to-disk.
2486 for (block_start = 0, block_in_page = 0, bh = head;
2487 block_start < PAGE_CACHE_SIZE;
2488 block_in_page++, block_start += blocksize, bh = bh->b_this_page) {
2489 int create;
2491 block_end = block_start + blocksize;
2492 bh->b_state = 0;
2493 create = 1;
2494 if (block_start >= to)
2495 create = 0;
2496 ret = get_block(inode, block_in_file + block_in_page,
2497 bh, create);
2498 if (ret)
2499 goto failed;
2500 if (!buffer_mapped(bh))
2501 is_mapped_to_disk = 0;
2502 if (buffer_new(bh))
2503 unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
2504 if (PageUptodate(page)) {
2505 set_buffer_uptodate(bh);
2506 continue;
2508 if (buffer_new(bh) || !buffer_mapped(bh)) {
2509 zero_user_segments(page, block_start, from,
2510 to, block_end);
2511 continue;
2513 if (buffer_uptodate(bh))
2514 continue; /* reiserfs does this */
2515 if (block_start < from || block_end > to) {
2516 lock_buffer(bh);
2517 bh->b_end_io = end_buffer_read_nobh;
2518 submit_bh(READ, bh);
2519 nr_reads++;
2523 if (nr_reads) {
2525 * The page is locked, so these buffers are protected from
2526 * any VM or truncate activity. Hence we don't need to care
2527 * for the buffer_head refcounts.
2529 for (bh = head; bh; bh = bh->b_this_page) {
2530 wait_on_buffer(bh);
2531 if (!buffer_uptodate(bh))
2532 ret = -EIO;
2534 if (ret)
2535 goto failed;
2538 if (is_mapped_to_disk)
2539 SetPageMappedToDisk(page);
2541 *fsdata = head; /* to be released by nobh_write_end */
2543 return 0;
2545 failed:
2546 BUG_ON(!ret);
2548 * Error recovery is a bit difficult. We need to zero out blocks that
2549 * were newly allocated, and dirty them to ensure they get written out.
2550 * Buffers need to be attached to the page at this point, otherwise
2551 * the handling of potential IO errors during writeout would be hard
2552 * (could try doing synchronous writeout, but what if that fails too?)
2554 attach_nobh_buffers(page, head);
2555 page_zero_new_buffers(page, from, to);
2557 out_release:
2558 unlock_page(page);
2559 page_cache_release(page);
2560 *pagep = NULL;
2562 if (pos + len > inode->i_size)
2563 vmtruncate(inode, inode->i_size);
2565 return ret;
2567 EXPORT_SYMBOL(nobh_write_begin);
2569 int nobh_write_end(struct file *file, struct address_space *mapping,
2570 loff_t pos, unsigned len, unsigned copied,
2571 struct page *page, void *fsdata)
2573 struct inode *inode = page->mapping->host;
2574 struct buffer_head *head = fsdata;
2575 struct buffer_head *bh;
2577 if (!PageMappedToDisk(page)) {
2578 if (unlikely(copied < len) && !page_has_buffers(page))
2579 attach_nobh_buffers(page, head);
2580 if (page_has_buffers(page))
2581 return generic_write_end(file, mapping, pos, len,
2582 copied, page, fsdata);
2585 SetPageUptodate(page);
2586 set_page_dirty(page);
2587 if (pos+copied > inode->i_size) {
2588 i_size_write(inode, pos+copied);
2589 mark_inode_dirty(inode);
2592 unlock_page(page);
2593 page_cache_release(page);
2595 while (head) {
2596 bh = head;
2597 head = head->b_this_page;
2598 free_buffer_head(bh);
2601 return copied;
2603 EXPORT_SYMBOL(nobh_write_end);
2606 * nobh_writepage() - based on block_full_write_page() except
2607 * that it tries to operate without attaching bufferheads to
2608 * the page.
2610 int nobh_writepage(struct page *page, get_block_t *get_block,
2611 struct writeback_control *wbc)
2613 struct inode * const inode = page->mapping->host;
2614 loff_t i_size = i_size_read(inode);
2615 const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
2616 unsigned offset;
2617 int ret;
2619 /* Is the page fully inside i_size? */
2620 if (page->index < end_index)
2621 goto out;
2623 /* Is the page fully outside i_size? (truncate in progress) */
2624 offset = i_size & (PAGE_CACHE_SIZE-1);
2625 if (page->index >= end_index+1 || !offset) {
2627 * The page may have dirty, unmapped buffers. For example,
2628 * they may have been added in ext3_writepage(). Make them
2629 * freeable here, so the page does not leak.
2631 #if 0
2632 /* Not really sure about this - do we need this ? */
2633 if (page->mapping->a_ops->invalidatepage)
2634 page->mapping->a_ops->invalidatepage(page, offset);
2635 #endif
2636 unlock_page(page);
2637 return 0; /* don't care */
2641 * The page straddles i_size. It must be zeroed out on each and every
2642 * writepage invocation because it may be mmapped. "A file is mapped
2643 * in multiples of the page size. For a file that is not a multiple of
2644 * the page size, the remaining memory is zeroed when mapped, and
2645 * writes to that region are not written out to the file."
2647 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
2648 out:
2649 ret = mpage_writepage(page, get_block, wbc);
2650 if (ret == -EAGAIN)
2651 ret = __block_write_full_page(inode, page, get_block, wbc);
2652 return ret;
2654 EXPORT_SYMBOL(nobh_writepage);
2656 int nobh_truncate_page(struct address_space *mapping,
2657 loff_t from, get_block_t *get_block)
2659 pgoff_t index = from >> PAGE_CACHE_SHIFT;
2660 unsigned offset = from & (PAGE_CACHE_SIZE-1);
2661 unsigned blocksize;
2662 sector_t iblock;
2663 unsigned length, pos;
2664 struct inode *inode = mapping->host;
2665 struct page *page;
2666 struct buffer_head map_bh;
2667 int err;
2669 blocksize = 1 << inode->i_blkbits;
2670 length = offset & (blocksize - 1);
2672 /* Block boundary? Nothing to do */
2673 if (!length)
2674 return 0;
2676 length = blocksize - length;
2677 iblock = (sector_t)index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
2679 page = grab_cache_page(mapping, index);
2680 err = -ENOMEM;
2681 if (!page)
2682 goto out;
2684 if (page_has_buffers(page)) {
2685 has_buffers:
2686 unlock_page(page);
2687 page_cache_release(page);
2688 return block_truncate_page(mapping, from, get_block);
2691 /* Find the buffer that contains "offset" */
2692 pos = blocksize;
2693 while (offset >= pos) {
2694 iblock++;
2695 pos += blocksize;
2698 err = get_block(inode, iblock, &map_bh, 0);
2699 if (err)
2700 goto unlock;
2701 /* unmapped? It's a hole - nothing to do */
2702 if (!buffer_mapped(&map_bh))
2703 goto unlock;
2705 /* Ok, it's mapped. Make sure it's up-to-date */
2706 if (!PageUptodate(page)) {
2707 err = mapping->a_ops->readpage(NULL, page);
2708 if (err) {
2709 page_cache_release(page);
2710 goto out;
2712 lock_page(page);
2713 if (!PageUptodate(page)) {
2714 err = -EIO;
2715 goto unlock;
2717 if (page_has_buffers(page))
2718 goto has_buffers;
2720 zero_user(page, offset, length);
2721 set_page_dirty(page);
2722 err = 0;
2724 unlock:
2725 unlock_page(page);
2726 page_cache_release(page);
2727 out:
2728 return err;
2730 EXPORT_SYMBOL(nobh_truncate_page);
2732 int block_truncate_page(struct address_space *mapping,
2733 loff_t from, get_block_t *get_block)
2735 pgoff_t index = from >> PAGE_CACHE_SHIFT;
2736 unsigned offset = from & (PAGE_CACHE_SIZE-1);
2737 unsigned blocksize;
2738 sector_t iblock;
2739 unsigned length, pos;
2740 struct inode *inode = mapping->host;
2741 struct page *page;
2742 struct buffer_head *bh;
2743 int err;
2745 blocksize = 1 << inode->i_blkbits;
2746 length = offset & (blocksize - 1);
2748 /* Block boundary? Nothing to do */
2749 if (!length)
2750 return 0;
2752 length = blocksize - length;
2753 iblock = (sector_t)index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
2755 page = grab_cache_page(mapping, index);
2756 err = -ENOMEM;
2757 if (!page)
2758 goto out;
2760 if (!page_has_buffers(page))
2761 create_empty_buffers(page, blocksize, 0);
2763 /* Find the buffer that contains "offset" */
2764 bh = page_buffers(page);
2765 pos = blocksize;
2766 while (offset >= pos) {
2767 bh = bh->b_this_page;
2768 iblock++;
2769 pos += blocksize;
2772 err = 0;
2773 if (!buffer_mapped(bh)) {
2774 WARN_ON(bh->b_size != blocksize);
2775 err = get_block(inode, iblock, bh, 0);
2776 if (err)
2777 goto unlock;
2778 /* unmapped? It's a hole - nothing to do */
2779 if (!buffer_mapped(bh))
2780 goto unlock;
2783 /* Ok, it's mapped. Make sure it's up-to-date */
2784 if (PageUptodate(page))
2785 set_buffer_uptodate(bh);
2787 if (!buffer_uptodate(bh) && !buffer_delay(bh) && !buffer_unwritten(bh)) {
2788 err = -EIO;
2789 ll_rw_block(READ, 1, &bh);
2790 wait_on_buffer(bh);
2791 /* Uhhuh. Read error. Complain and punt. */
2792 if (!buffer_uptodate(bh))
2793 goto unlock;
2796 zero_user(page, offset, length);
2797 mark_buffer_dirty(bh);
2798 err = 0;
2800 unlock:
2801 unlock_page(page);
2802 page_cache_release(page);
2803 out:
2804 return err;
2808 * The generic ->writepage function for buffer-backed address_spaces
2810 int block_write_full_page(struct page *page, get_block_t *get_block,
2811 struct writeback_control *wbc)
2813 struct inode * const inode = page->mapping->host;
2814 loff_t i_size = i_size_read(inode);
2815 const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
2816 unsigned offset;
2818 /* Is the page fully inside i_size? */
2819 if (page->index < end_index)
2820 return __block_write_full_page(inode, page, get_block, wbc);
2822 /* Is the page fully outside i_size? (truncate in progress) */
2823 offset = i_size & (PAGE_CACHE_SIZE-1);
2824 if (page->index >= end_index+1 || !offset) {
2826 * The page may have dirty, unmapped buffers. For example,
2827 * they may have been added in ext3_writepage(). Make them
2828 * freeable here, so the page does not leak.
2830 do_invalidatepage(page, 0);
2831 unlock_page(page);
2832 return 0; /* don't care */
2836 * The page straddles i_size. It must be zeroed out on each and every
2837 * writepage invokation because it may be mmapped. "A file is mapped
2838 * in multiples of the page size. For a file that is not a multiple of
2839 * the page size, the remaining memory is zeroed when mapped, and
2840 * writes to that region are not written out to the file."
2842 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
2843 return __block_write_full_page(inode, page, get_block, wbc);
2846 sector_t generic_block_bmap(struct address_space *mapping, sector_t block,
2847 get_block_t *get_block)
2849 struct buffer_head tmp;
2850 struct inode *inode = mapping->host;
2851 tmp.b_state = 0;
2852 tmp.b_blocknr = 0;
2853 tmp.b_size = 1 << inode->i_blkbits;
2854 get_block(inode, block, &tmp, 0);
2855 return tmp.b_blocknr;
2858 static void end_bio_bh_io_sync(struct bio *bio, int err)
2860 struct buffer_head *bh = bio->bi_private;
2862 if (err == -EOPNOTSUPP) {
2863 set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
2864 set_bit(BH_Eopnotsupp, &bh->b_state);
2867 bh->b_end_io(bh, test_bit(BIO_UPTODATE, &bio->bi_flags));
2868 bio_put(bio);
2871 int submit_bh(int rw, struct buffer_head * bh)
2873 struct bio *bio;
2874 int ret = 0;
2876 BUG_ON(!buffer_locked(bh));
2877 BUG_ON(!buffer_mapped(bh));
2878 BUG_ON(!bh->b_end_io);
2880 if (buffer_ordered(bh) && (rw == WRITE))
2881 rw = WRITE_BARRIER;
2884 * Only clear out a write error when rewriting, should this
2885 * include WRITE_SYNC as well?
2887 if (test_set_buffer_req(bh) && (rw == WRITE || rw == WRITE_BARRIER))
2888 clear_buffer_write_io_error(bh);
2891 * from here on down, it's all bio -- do the initial mapping,
2892 * submit_bio -> generic_make_request may further map this bio around
2894 bio = bio_alloc(GFP_NOIO, 1);
2896 bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
2897 bio->bi_bdev = bh->b_bdev;
2898 bio->bi_io_vec[0].bv_page = bh->b_page;
2899 bio->bi_io_vec[0].bv_len = bh->b_size;
2900 bio->bi_io_vec[0].bv_offset = bh_offset(bh);
2902 bio->bi_vcnt = 1;
2903 bio->bi_idx = 0;
2904 bio->bi_size = bh->b_size;
2906 bio->bi_end_io = end_bio_bh_io_sync;
2907 bio->bi_private = bh;
2909 bio_get(bio);
2910 submit_bio(rw, bio);
2912 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2913 ret = -EOPNOTSUPP;
2915 bio_put(bio);
2916 return ret;
2920 * ll_rw_block: low-level access to block devices (DEPRECATED)
2921 * @rw: whether to %READ or %WRITE or %SWRITE or maybe %READA (readahead)
2922 * @nr: number of &struct buffer_heads in the array
2923 * @bhs: array of pointers to &struct buffer_head
2925 * ll_rw_block() takes an array of pointers to &struct buffer_heads, and
2926 * requests an I/O operation on them, either a %READ or a %WRITE. The third
2927 * %SWRITE is like %WRITE only we make sure that the *current* data in buffers
2928 * are sent to disk. The fourth %READA option is described in the documentation
2929 * for generic_make_request() which ll_rw_block() calls.
2931 * This function drops any buffer that it cannot get a lock on (with the
2932 * BH_Lock state bit) unless SWRITE is required, any buffer that appears to be
2933 * clean when doing a write request, and any buffer that appears to be
2934 * up-to-date when doing read request. Further it marks as clean buffers that
2935 * are processed for writing (the buffer cache won't assume that they are
2936 * actually clean until the buffer gets unlocked).
2938 * ll_rw_block sets b_end_io to simple completion handler that marks
2939 * the buffer up-to-date (if approriate), unlocks the buffer and wakes
2940 * any waiters.
2942 * All of the buffers must be for the same device, and must also be a
2943 * multiple of the current approved size for the device.
2945 void ll_rw_block(int rw, int nr, struct buffer_head *bhs[])
2947 int i;
2949 for (i = 0; i < nr; i++) {
2950 struct buffer_head *bh = bhs[i];
2952 if (rw == SWRITE)
2953 lock_buffer(bh);
2954 else if (test_set_buffer_locked(bh))
2955 continue;
2957 if (rw == WRITE || rw == SWRITE) {
2958 if (test_clear_buffer_dirty(bh)) {
2959 bh->b_end_io = end_buffer_write_sync;
2960 get_bh(bh);
2961 submit_bh(WRITE, bh);
2962 continue;
2964 } else {
2965 if (!buffer_uptodate(bh)) {
2966 bh->b_end_io = end_buffer_read_sync;
2967 get_bh(bh);
2968 submit_bh(rw, bh);
2969 continue;
2972 unlock_buffer(bh);
2977 * For a data-integrity writeout, we need to wait upon any in-progress I/O
2978 * and then start new I/O and then wait upon it. The caller must have a ref on
2979 * the buffer_head.
2981 int sync_dirty_buffer(struct buffer_head *bh)
2983 int ret = 0;
2985 WARN_ON(atomic_read(&bh->b_count) < 1);
2986 lock_buffer(bh);
2987 if (test_clear_buffer_dirty(bh)) {
2988 get_bh(bh);
2989 bh->b_end_io = end_buffer_write_sync;
2990 ret = submit_bh(WRITE, bh);
2991 wait_on_buffer(bh);
2992 if (buffer_eopnotsupp(bh)) {
2993 clear_buffer_eopnotsupp(bh);
2994 ret = -EOPNOTSUPP;
2996 if (!ret && !buffer_uptodate(bh))
2997 ret = -EIO;
2998 } else {
2999 unlock_buffer(bh);
3001 return ret;
3005 * try_to_free_buffers() checks if all the buffers on this particular page
3006 * are unused, and releases them if so.
3008 * Exclusion against try_to_free_buffers may be obtained by either
3009 * locking the page or by holding its mapping's private_lock.
3011 * If the page is dirty but all the buffers are clean then we need to
3012 * be sure to mark the page clean as well. This is because the page
3013 * may be against a block device, and a later reattachment of buffers
3014 * to a dirty page will set *all* buffers dirty. Which would corrupt
3015 * filesystem data on the same device.
3017 * The same applies to regular filesystem pages: if all the buffers are
3018 * clean then we set the page clean and proceed. To do that, we require
3019 * total exclusion from __set_page_dirty_buffers(). That is obtained with
3020 * private_lock.
3022 * try_to_free_buffers() is non-blocking.
3024 static inline int buffer_busy(struct buffer_head *bh)
3026 return atomic_read(&bh->b_count) |
3027 (bh->b_state & ((1 << BH_Dirty) | (1 << BH_Lock)));
3030 static int
3031 drop_buffers(struct page *page, struct buffer_head **buffers_to_free)
3033 struct buffer_head *head = page_buffers(page);
3034 struct buffer_head *bh;
3036 bh = head;
3037 do {
3038 if (buffer_write_io_error(bh) && page->mapping)
3039 set_bit(AS_EIO, &page->mapping->flags);
3040 if (buffer_busy(bh))
3041 goto failed;
3042 bh = bh->b_this_page;
3043 } while (bh != head);
3045 do {
3046 struct buffer_head *next = bh->b_this_page;
3048 if (bh->b_assoc_map)
3049 __remove_assoc_queue(bh);
3050 bh = next;
3051 } while (bh != head);
3052 *buffers_to_free = head;
3053 __clear_page_buffers(page);
3054 return 1;
3055 failed:
3056 return 0;
3059 int try_to_free_buffers(struct page *page)
3061 struct address_space * const mapping = page->mapping;
3062 struct buffer_head *buffers_to_free = NULL;
3063 int ret = 0;
3065 BUG_ON(!PageLocked(page));
3066 if (PageWriteback(page))
3067 return 0;
3069 if (mapping == NULL) { /* can this still happen? */
3070 ret = drop_buffers(page, &buffers_to_free);
3071 goto out;
3074 spin_lock(&mapping->private_lock);
3075 ret = drop_buffers(page, &buffers_to_free);
3078 * If the filesystem writes its buffers by hand (eg ext3)
3079 * then we can have clean buffers against a dirty page. We
3080 * clean the page here; otherwise the VM will never notice
3081 * that the filesystem did any IO at all.
3083 * Also, during truncate, discard_buffer will have marked all
3084 * the page's buffers clean. We discover that here and clean
3085 * the page also.
3087 * private_lock must be held over this entire operation in order
3088 * to synchronise against __set_page_dirty_buffers and prevent the
3089 * dirty bit from being lost.
3091 if (ret)
3092 cancel_dirty_page(page, PAGE_CACHE_SIZE);
3093 spin_unlock(&mapping->private_lock);
3094 out:
3095 if (buffers_to_free) {
3096 struct buffer_head *bh = buffers_to_free;
3098 do {
3099 struct buffer_head *next = bh->b_this_page;
3100 free_buffer_head(bh);
3101 bh = next;
3102 } while (bh != buffers_to_free);
3104 return ret;
3106 EXPORT_SYMBOL(try_to_free_buffers);
3108 void block_sync_page(struct page *page)
3110 struct address_space *mapping;
3112 smp_mb();
3113 mapping = page_mapping(page);
3114 if (mapping)
3115 blk_run_backing_dev(mapping->backing_dev_info, page);
3119 * There are no bdflush tunables left. But distributions are
3120 * still running obsolete flush daemons, so we terminate them here.
3122 * Use of bdflush() is deprecated and will be removed in a future kernel.
3123 * The `pdflush' kernel threads fully replace bdflush daemons and this call.
3125 asmlinkage long sys_bdflush(int func, long data)
3127 static int msg_count;
3129 if (!capable(CAP_SYS_ADMIN))
3130 return -EPERM;
3132 if (msg_count < 5) {
3133 msg_count++;
3134 printk(KERN_INFO
3135 "warning: process `%s' used the obsolete bdflush"
3136 " system call\n", current->comm);
3137 printk(KERN_INFO "Fix your initscripts?\n");
3140 if (func == 1)
3141 do_exit(0);
3142 return 0;
3146 * Buffer-head allocation
3148 static struct kmem_cache *bh_cachep;
3151 * Once the number of bh's in the machine exceeds this level, we start
3152 * stripping them in writeback.
3154 static int max_buffer_heads;
3156 int buffer_heads_over_limit;
3158 struct bh_accounting {
3159 int nr; /* Number of live bh's */
3160 int ratelimit; /* Limit cacheline bouncing */
3163 static DEFINE_PER_CPU(struct bh_accounting, bh_accounting) = {0, 0};
3165 static void recalc_bh_state(void)
3167 int i;
3168 int tot = 0;
3170 if (__get_cpu_var(bh_accounting).ratelimit++ < 4096)
3171 return;
3172 __get_cpu_var(bh_accounting).ratelimit = 0;
3173 for_each_online_cpu(i)
3174 tot += per_cpu(bh_accounting, i).nr;
3175 buffer_heads_over_limit = (tot > max_buffer_heads);
3178 struct buffer_head *alloc_buffer_head(gfp_t gfp_flags)
3180 struct buffer_head *ret = kmem_cache_alloc(bh_cachep,
3181 set_migrateflags(gfp_flags, __GFP_RECLAIMABLE));
3182 if (ret) {
3183 INIT_LIST_HEAD(&ret->b_assoc_buffers);
3184 get_cpu_var(bh_accounting).nr++;
3185 recalc_bh_state();
3186 put_cpu_var(bh_accounting);
3188 return ret;
3190 EXPORT_SYMBOL(alloc_buffer_head);
3192 void free_buffer_head(struct buffer_head *bh)
3194 BUG_ON(!list_empty(&bh->b_assoc_buffers));
3195 kmem_cache_free(bh_cachep, bh);
3196 get_cpu_var(bh_accounting).nr--;
3197 recalc_bh_state();
3198 put_cpu_var(bh_accounting);
3200 EXPORT_SYMBOL(free_buffer_head);
3202 static void buffer_exit_cpu(int cpu)
3204 int i;
3205 struct bh_lru *b = &per_cpu(bh_lrus, cpu);
3207 for (i = 0; i < BH_LRU_SIZE; i++) {
3208 brelse(b->bhs[i]);
3209 b->bhs[i] = NULL;
3211 get_cpu_var(bh_accounting).nr += per_cpu(bh_accounting, cpu).nr;
3212 per_cpu(bh_accounting, cpu).nr = 0;
3213 put_cpu_var(bh_accounting);
3216 static int buffer_cpu_notify(struct notifier_block *self,
3217 unsigned long action, void *hcpu)
3219 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
3220 buffer_exit_cpu((unsigned long)hcpu);
3221 return NOTIFY_OK;
3225 * bh_uptodate_or_lock: Test whether the buffer is uptodate
3226 * @bh: struct buffer_head
3228 * Return true if the buffer is up-to-date and false,
3229 * with the buffer locked, if not.
3231 int bh_uptodate_or_lock(struct buffer_head *bh)
3233 if (!buffer_uptodate(bh)) {
3234 lock_buffer(bh);
3235 if (!buffer_uptodate(bh))
3236 return 0;
3237 unlock_buffer(bh);
3239 return 1;
3241 EXPORT_SYMBOL(bh_uptodate_or_lock);
3244 * bh_submit_read: Submit a locked buffer for reading
3245 * @bh: struct buffer_head
3247 * Returns zero on success and -EIO on error.
3249 int bh_submit_read(struct buffer_head *bh)
3251 BUG_ON(!buffer_locked(bh));
3253 if (buffer_uptodate(bh)) {
3254 unlock_buffer(bh);
3255 return 0;
3258 get_bh(bh);
3259 bh->b_end_io = end_buffer_read_sync;
3260 submit_bh(READ, bh);
3261 wait_on_buffer(bh);
3262 if (buffer_uptodate(bh))
3263 return 0;
3264 return -EIO;
3266 EXPORT_SYMBOL(bh_submit_read);
3268 static void
3269 init_buffer_head(struct kmem_cache *cachep, void *data)
3271 struct buffer_head *bh = data;
3273 memset(bh, 0, sizeof(*bh));
3274 INIT_LIST_HEAD(&bh->b_assoc_buffers);
3277 void __init buffer_init(void)
3279 int nrpages;
3281 bh_cachep = kmem_cache_create("buffer_head",
3282 sizeof(struct buffer_head), 0,
3283 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
3284 SLAB_MEM_SPREAD),
3285 init_buffer_head);
3288 * Limit the bh occupancy to 10% of ZONE_NORMAL
3290 nrpages = (nr_free_buffer_pages() * 10) / 100;
3291 max_buffer_heads = nrpages * (PAGE_SIZE / sizeof(struct buffer_head));
3292 hotcpu_notifier(buffer_cpu_notify, 0);
3295 EXPORT_SYMBOL(__bforget);
3296 EXPORT_SYMBOL(__brelse);
3297 EXPORT_SYMBOL(__wait_on_buffer);
3298 EXPORT_SYMBOL(block_commit_write);
3299 EXPORT_SYMBOL(block_prepare_write);
3300 EXPORT_SYMBOL(block_page_mkwrite);
3301 EXPORT_SYMBOL(block_read_full_page);
3302 EXPORT_SYMBOL(block_sync_page);
3303 EXPORT_SYMBOL(block_truncate_page);
3304 EXPORT_SYMBOL(block_write_full_page);
3305 EXPORT_SYMBOL(cont_write_begin);
3306 EXPORT_SYMBOL(end_buffer_read_sync);
3307 EXPORT_SYMBOL(end_buffer_write_sync);
3308 EXPORT_SYMBOL(file_fsync);
3309 EXPORT_SYMBOL(fsync_bdev);
3310 EXPORT_SYMBOL(generic_block_bmap);
3311 EXPORT_SYMBOL(generic_commit_write);
3312 EXPORT_SYMBOL(generic_cont_expand_simple);
3313 EXPORT_SYMBOL(init_buffer);
3314 EXPORT_SYMBOL(invalidate_bdev);
3315 EXPORT_SYMBOL(ll_rw_block);
3316 EXPORT_SYMBOL(mark_buffer_dirty);
3317 EXPORT_SYMBOL(submit_bh);
3318 EXPORT_SYMBOL(sync_dirty_buffer);
3319 EXPORT_SYMBOL(unlock_buffer);