[SCSI] fcoe: removes fc_transport_fcoe.[ch] code files
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / buffer.c
blobc2fa1be4923d19885dab161d45a53588ff296ac5
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 clear_bit_unlock(BH_Lock, &bh->b_state);
80 smp_mb__after_clear_bit();
81 wake_up_bit(&bh->b_state, BH_Lock);
85 * Block until a buffer comes unlocked. This doesn't stop it
86 * from becoming locked again - you have to lock it yourself
87 * if you want to preserve its state.
89 void __wait_on_buffer(struct buffer_head * bh)
91 wait_on_bit(&bh->b_state, BH_Lock, sync_buffer, TASK_UNINTERRUPTIBLE);
94 static void
95 __clear_page_buffers(struct page *page)
97 ClearPagePrivate(page);
98 set_page_private(page, 0);
99 page_cache_release(page);
103 static int quiet_error(struct buffer_head *bh)
105 if (!test_bit(BH_Quiet, &bh->b_state) && printk_ratelimit())
106 return 0;
107 return 1;
111 static void buffer_io_error(struct buffer_head *bh)
113 char b[BDEVNAME_SIZE];
114 printk(KERN_ERR "Buffer I/O error on device %s, logical block %Lu\n",
115 bdevname(bh->b_bdev, b),
116 (unsigned long long)bh->b_blocknr);
120 * End-of-IO handler helper function which does not touch the bh after
121 * unlocking it.
122 * Note: unlock_buffer() sort-of does touch the bh after unlocking it, but
123 * a race there is benign: unlock_buffer() only use the bh's address for
124 * hashing after unlocking the buffer, so it doesn't actually touch the bh
125 * itself.
127 static void __end_buffer_read_notouch(struct buffer_head *bh, int uptodate)
129 if (uptodate) {
130 set_buffer_uptodate(bh);
131 } else {
132 /* This happens, due to failed READA attempts. */
133 clear_buffer_uptodate(bh);
135 unlock_buffer(bh);
139 * Default synchronous end-of-IO handler.. Just mark it up-to-date and
140 * unlock the buffer. This is what ll_rw_block uses too.
142 void end_buffer_read_sync(struct buffer_head *bh, int uptodate)
144 __end_buffer_read_notouch(bh, uptodate);
145 put_bh(bh);
148 void end_buffer_write_sync(struct buffer_head *bh, int uptodate)
150 char b[BDEVNAME_SIZE];
152 if (uptodate) {
153 set_buffer_uptodate(bh);
154 } else {
155 if (!buffer_eopnotsupp(bh) && !quiet_error(bh)) {
156 buffer_io_error(bh);
157 printk(KERN_WARNING "lost page write due to "
158 "I/O error on %s\n",
159 bdevname(bh->b_bdev, b));
161 set_buffer_write_io_error(bh);
162 clear_buffer_uptodate(bh);
164 unlock_buffer(bh);
165 put_bh(bh);
169 * Various filesystems appear to want __find_get_block to be non-blocking.
170 * But it's the page lock which protects the buffers. To get around this,
171 * we get exclusion from try_to_free_buffers with the blockdev mapping's
172 * private_lock.
174 * Hack idea: for the blockdev mapping, i_bufferlist_lock contention
175 * may be quite high. This code could TryLock the page, and if that
176 * succeeds, there is no need to take private_lock. (But if
177 * private_lock is contended then so is mapping->tree_lock).
179 static struct buffer_head *
180 __find_get_block_slow(struct block_device *bdev, sector_t block)
182 struct inode *bd_inode = bdev->bd_inode;
183 struct address_space *bd_mapping = bd_inode->i_mapping;
184 struct buffer_head *ret = NULL;
185 pgoff_t index;
186 struct buffer_head *bh;
187 struct buffer_head *head;
188 struct page *page;
189 int all_mapped = 1;
191 index = block >> (PAGE_CACHE_SHIFT - bd_inode->i_blkbits);
192 page = find_get_page(bd_mapping, index);
193 if (!page)
194 goto out;
196 spin_lock(&bd_mapping->private_lock);
197 if (!page_has_buffers(page))
198 goto out_unlock;
199 head = page_buffers(page);
200 bh = head;
201 do {
202 if (!buffer_mapped(bh))
203 all_mapped = 0;
204 else if (bh->b_blocknr == block) {
205 ret = bh;
206 get_bh(bh);
207 goto out_unlock;
209 bh = bh->b_this_page;
210 } while (bh != head);
212 /* we might be here because some of the buffers on this page are
213 * not mapped. This is due to various races between
214 * file io on the block device and getblk. It gets dealt with
215 * elsewhere, don't buffer_error if we had some unmapped buffers
217 if (all_mapped) {
218 printk("__find_get_block_slow() failed. "
219 "block=%llu, b_blocknr=%llu\n",
220 (unsigned long long)block,
221 (unsigned long long)bh->b_blocknr);
222 printk("b_state=0x%08lx, b_size=%zu\n",
223 bh->b_state, bh->b_size);
224 printk("device blocksize: %d\n", 1 << bd_inode->i_blkbits);
226 out_unlock:
227 spin_unlock(&bd_mapping->private_lock);
228 page_cache_release(page);
229 out:
230 return ret;
233 /* If invalidate_buffers() will trash dirty buffers, it means some kind
234 of fs corruption is going on. Trashing dirty data always imply losing
235 information that was supposed to be just stored on the physical layer
236 by the user.
238 Thus invalidate_buffers in general usage is not allwowed to trash
239 dirty buffers. For example ioctl(FLSBLKBUF) expects dirty data to
240 be preserved. These buffers are simply skipped.
242 We also skip buffers which are still in use. For example this can
243 happen if a userspace program is reading the block device.
245 NOTE: In the case where the user removed a removable-media-disk even if
246 there's still dirty data not synced on disk (due a bug in the device driver
247 or due an error of the user), by not destroying the dirty buffers we could
248 generate corruption also on the next media inserted, thus a parameter is
249 necessary to handle this case in the most safe way possible (trying
250 to not corrupt also the new disk inserted with the data belonging to
251 the old now corrupted disk). Also for the ramdisk the natural thing
252 to do in order to release the ramdisk memory is to destroy dirty buffers.
254 These are two special cases. Normal usage imply the device driver
255 to issue a sync on the device (without waiting I/O completion) and
256 then an invalidate_buffers call that doesn't trash dirty buffers.
258 For handling cache coherency with the blkdev pagecache the 'update' case
259 is been introduced. It is needed to re-read from disk any pinned
260 buffer. NOTE: re-reading from disk is destructive so we can do it only
261 when we assume nobody is changing the buffercache under our I/O and when
262 we think the disk contains more recent information than the buffercache.
263 The update == 1 pass marks the buffers we need to update, the update == 2
264 pass does the actual I/O. */
265 void invalidate_bdev(struct block_device *bdev)
267 struct address_space *mapping = bdev->bd_inode->i_mapping;
269 if (mapping->nrpages == 0)
270 return;
272 invalidate_bh_lrus();
273 invalidate_mapping_pages(mapping, 0, -1);
277 * Kick pdflush then try to free up some ZONE_NORMAL memory.
279 static void free_more_memory(void)
281 struct zone *zone;
282 int nid;
284 wakeup_pdflush(1024);
285 yield();
287 for_each_online_node(nid) {
288 (void)first_zones_zonelist(node_zonelist(nid, GFP_NOFS),
289 gfp_zone(GFP_NOFS), NULL,
290 &zone);
291 if (zone)
292 try_to_free_pages(node_zonelist(nid, GFP_NOFS), 0,
293 GFP_NOFS, NULL);
298 * I/O completion handler for block_read_full_page() - pages
299 * which come unlocked at the end of I/O.
301 static void end_buffer_async_read(struct buffer_head *bh, int uptodate)
303 unsigned long flags;
304 struct buffer_head *first;
305 struct buffer_head *tmp;
306 struct page *page;
307 int page_uptodate = 1;
309 BUG_ON(!buffer_async_read(bh));
311 page = bh->b_page;
312 if (uptodate) {
313 set_buffer_uptodate(bh);
314 } else {
315 clear_buffer_uptodate(bh);
316 if (!quiet_error(bh))
317 buffer_io_error(bh);
318 SetPageError(page);
322 * Be _very_ careful from here on. Bad things can happen if
323 * two buffer heads end IO at almost the same time and both
324 * decide that the page is now completely done.
326 first = page_buffers(page);
327 local_irq_save(flags);
328 bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
329 clear_buffer_async_read(bh);
330 unlock_buffer(bh);
331 tmp = bh;
332 do {
333 if (!buffer_uptodate(tmp))
334 page_uptodate = 0;
335 if (buffer_async_read(tmp)) {
336 BUG_ON(!buffer_locked(tmp));
337 goto still_busy;
339 tmp = tmp->b_this_page;
340 } while (tmp != bh);
341 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
342 local_irq_restore(flags);
345 * If none of the buffers had errors and they are all
346 * uptodate then we can set the page uptodate.
348 if (page_uptodate && !PageError(page))
349 SetPageUptodate(page);
350 unlock_page(page);
351 return;
353 still_busy:
354 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
355 local_irq_restore(flags);
356 return;
360 * Completion handler for block_write_full_page() - pages which are unlocked
361 * during I/O, and which have PageWriteback cleared upon I/O completion.
363 static void end_buffer_async_write(struct buffer_head *bh, int uptodate)
365 char b[BDEVNAME_SIZE];
366 unsigned long flags;
367 struct buffer_head *first;
368 struct buffer_head *tmp;
369 struct page *page;
371 BUG_ON(!buffer_async_write(bh));
373 page = bh->b_page;
374 if (uptodate) {
375 set_buffer_uptodate(bh);
376 } else {
377 if (!quiet_error(bh)) {
378 buffer_io_error(bh);
379 printk(KERN_WARNING "lost page write due to "
380 "I/O error on %s\n",
381 bdevname(bh->b_bdev, b));
383 set_bit(AS_EIO, &page->mapping->flags);
384 set_buffer_write_io_error(bh);
385 clear_buffer_uptodate(bh);
386 SetPageError(page);
389 first = page_buffers(page);
390 local_irq_save(flags);
391 bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
393 clear_buffer_async_write(bh);
394 unlock_buffer(bh);
395 tmp = bh->b_this_page;
396 while (tmp != bh) {
397 if (buffer_async_write(tmp)) {
398 BUG_ON(!buffer_locked(tmp));
399 goto still_busy;
401 tmp = tmp->b_this_page;
403 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
404 local_irq_restore(flags);
405 end_page_writeback(page);
406 return;
408 still_busy:
409 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
410 local_irq_restore(flags);
411 return;
415 * If a page's buffers are under async readin (end_buffer_async_read
416 * completion) then there is a possibility that another thread of
417 * control could lock one of the buffers after it has completed
418 * but while some of the other buffers have not completed. This
419 * locked buffer would confuse end_buffer_async_read() into not unlocking
420 * the page. So the absence of BH_Async_Read tells end_buffer_async_read()
421 * that this buffer is not under async I/O.
423 * The page comes unlocked when it has no locked buffer_async buffers
424 * left.
426 * PageLocked prevents anyone starting new async I/O reads any of
427 * the buffers.
429 * PageWriteback is used to prevent simultaneous writeout of the same
430 * page.
432 * PageLocked prevents anyone from starting writeback of a page which is
433 * under read I/O (PageWriteback is only ever set against a locked page).
435 static void mark_buffer_async_read(struct buffer_head *bh)
437 bh->b_end_io = end_buffer_async_read;
438 set_buffer_async_read(bh);
441 void mark_buffer_async_write(struct buffer_head *bh)
443 bh->b_end_io = end_buffer_async_write;
444 set_buffer_async_write(bh);
446 EXPORT_SYMBOL(mark_buffer_async_write);
450 * fs/buffer.c contains helper functions for buffer-backed address space's
451 * fsync functions. A common requirement for buffer-based filesystems is
452 * that certain data from the backing blockdev needs to be written out for
453 * a successful fsync(). For example, ext2 indirect blocks need to be
454 * written back and waited upon before fsync() returns.
456 * The functions mark_buffer_inode_dirty(), fsync_inode_buffers(),
457 * inode_has_buffers() and invalidate_inode_buffers() are provided for the
458 * management of a list of dependent buffers at ->i_mapping->private_list.
460 * Locking is a little subtle: try_to_free_buffers() will remove buffers
461 * from their controlling inode's queue when they are being freed. But
462 * try_to_free_buffers() will be operating against the *blockdev* mapping
463 * at the time, not against the S_ISREG file which depends on those buffers.
464 * So the locking for private_list is via the private_lock in the address_space
465 * which backs the buffers. Which is different from the address_space
466 * against which the buffers are listed. So for a particular address_space,
467 * mapping->private_lock does *not* protect mapping->private_list! In fact,
468 * mapping->private_list will always be protected by the backing blockdev's
469 * ->private_lock.
471 * Which introduces a requirement: all buffers on an address_space's
472 * ->private_list must be from the same address_space: the blockdev's.
474 * address_spaces which do not place buffers at ->private_list via these
475 * utility functions are free to use private_lock and private_list for
476 * whatever they want. The only requirement is that list_empty(private_list)
477 * be true at clear_inode() time.
479 * FIXME: clear_inode should not call invalidate_inode_buffers(). The
480 * filesystems should do that. invalidate_inode_buffers() should just go
481 * BUG_ON(!list_empty).
483 * FIXME: mark_buffer_dirty_inode() is a data-plane operation. It should
484 * take an address_space, not an inode. And it should be called
485 * mark_buffer_dirty_fsync() to clearly define why those buffers are being
486 * queued up.
488 * FIXME: mark_buffer_dirty_inode() doesn't need to add the buffer to the
489 * list if it is already on a list. Because if the buffer is on a list,
490 * it *must* already be on the right one. If not, the filesystem is being
491 * silly. This will save a ton of locking. But first we have to ensure
492 * that buffers are taken *off* the old inode's list when they are freed
493 * (presumably in truncate). That requires careful auditing of all
494 * filesystems (do it inside bforget()). It could also be done by bringing
495 * b_inode back.
499 * The buffer's backing address_space's private_lock must be held
501 static void __remove_assoc_queue(struct buffer_head *bh)
503 list_del_init(&bh->b_assoc_buffers);
504 WARN_ON(!bh->b_assoc_map);
505 if (buffer_write_io_error(bh))
506 set_bit(AS_EIO, &bh->b_assoc_map->flags);
507 bh->b_assoc_map = NULL;
510 int inode_has_buffers(struct inode *inode)
512 return !list_empty(&inode->i_data.private_list);
516 * osync is designed to support O_SYNC io. It waits synchronously for
517 * all already-submitted IO to complete, but does not queue any new
518 * writes to the disk.
520 * To do O_SYNC writes, just queue the buffer writes with ll_rw_block as
521 * you dirty the buffers, and then use osync_inode_buffers to wait for
522 * completion. Any other dirty buffers which are not yet queued for
523 * write will not be flushed to disk by the osync.
525 static int osync_buffers_list(spinlock_t *lock, struct list_head *list)
527 struct buffer_head *bh;
528 struct list_head *p;
529 int err = 0;
531 spin_lock(lock);
532 repeat:
533 list_for_each_prev(p, list) {
534 bh = BH_ENTRY(p);
535 if (buffer_locked(bh)) {
536 get_bh(bh);
537 spin_unlock(lock);
538 wait_on_buffer(bh);
539 if (!buffer_uptodate(bh))
540 err = -EIO;
541 brelse(bh);
542 spin_lock(lock);
543 goto repeat;
546 spin_unlock(lock);
547 return err;
550 void do_thaw_all(unsigned long unused)
552 struct super_block *sb;
553 char b[BDEVNAME_SIZE];
555 spin_lock(&sb_lock);
556 restart:
557 list_for_each_entry(sb, &super_blocks, s_list) {
558 sb->s_count++;
559 spin_unlock(&sb_lock);
560 down_read(&sb->s_umount);
561 while (sb->s_bdev && !thaw_bdev(sb->s_bdev, sb))
562 printk(KERN_WARNING "Emergency Thaw on %s\n",
563 bdevname(sb->s_bdev, b));
564 up_read(&sb->s_umount);
565 spin_lock(&sb_lock);
566 if (__put_super_and_need_restart(sb))
567 goto restart;
569 spin_unlock(&sb_lock);
570 printk(KERN_WARNING "Emergency Thaw complete\n");
574 * emergency_thaw_all -- forcibly thaw every frozen filesystem
576 * Used for emergency unfreeze of all filesystems via SysRq
578 void emergency_thaw_all(void)
580 pdflush_operation(do_thaw_all, 0);
584 * sync_mapping_buffers - write out & wait upon a mapping's "associated" buffers
585 * @mapping: the mapping which wants those buffers written
587 * Starts I/O against the buffers at mapping->private_list, and waits upon
588 * that I/O.
590 * Basically, this is a convenience function for fsync().
591 * @mapping is a file or directory which needs those buffers to be written for
592 * a successful fsync().
594 int sync_mapping_buffers(struct address_space *mapping)
596 struct address_space *buffer_mapping = mapping->assoc_mapping;
598 if (buffer_mapping == NULL || list_empty(&mapping->private_list))
599 return 0;
601 return fsync_buffers_list(&buffer_mapping->private_lock,
602 &mapping->private_list);
604 EXPORT_SYMBOL(sync_mapping_buffers);
607 * Called when we've recently written block `bblock', and it is known that
608 * `bblock' was for a buffer_boundary() buffer. This means that the block at
609 * `bblock + 1' is probably a dirty indirect block. Hunt it down and, if it's
610 * dirty, schedule it for IO. So that indirects merge nicely with their data.
612 void write_boundary_block(struct block_device *bdev,
613 sector_t bblock, unsigned blocksize)
615 struct buffer_head *bh = __find_get_block(bdev, bblock + 1, blocksize);
616 if (bh) {
617 if (buffer_dirty(bh))
618 ll_rw_block(WRITE, 1, &bh);
619 put_bh(bh);
623 void mark_buffer_dirty_inode(struct buffer_head *bh, struct inode *inode)
625 struct address_space *mapping = inode->i_mapping;
626 struct address_space *buffer_mapping = bh->b_page->mapping;
628 mark_buffer_dirty(bh);
629 if (!mapping->assoc_mapping) {
630 mapping->assoc_mapping = buffer_mapping;
631 } else {
632 BUG_ON(mapping->assoc_mapping != buffer_mapping);
634 if (!bh->b_assoc_map) {
635 spin_lock(&buffer_mapping->private_lock);
636 list_move_tail(&bh->b_assoc_buffers,
637 &mapping->private_list);
638 bh->b_assoc_map = mapping;
639 spin_unlock(&buffer_mapping->private_lock);
642 EXPORT_SYMBOL(mark_buffer_dirty_inode);
645 * Mark the page dirty, and set it dirty in the radix tree, and mark the inode
646 * dirty.
648 * If warn is true, then emit a warning if the page is not uptodate and has
649 * not been truncated.
651 static void __set_page_dirty(struct page *page,
652 struct address_space *mapping, int warn)
654 spin_lock_irq(&mapping->tree_lock);
655 if (page->mapping) { /* Race with truncate? */
656 WARN_ON_ONCE(warn && !PageUptodate(page));
657 account_page_dirtied(page, mapping);
658 radix_tree_tag_set(&mapping->page_tree,
659 page_index(page), PAGECACHE_TAG_DIRTY);
661 spin_unlock_irq(&mapping->tree_lock);
662 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
666 * Add a page to the dirty page list.
668 * It is a sad fact of life that this function is called from several places
669 * deeply under spinlocking. It may not sleep.
671 * If the page has buffers, the uptodate buffers are set dirty, to preserve
672 * dirty-state coherency between the page and the buffers. It the page does
673 * not have buffers then when they are later attached they will all be set
674 * dirty.
676 * The buffers are dirtied before the page is dirtied. There's a small race
677 * window in which a writepage caller may see the page cleanness but not the
678 * buffer dirtiness. That's fine. If this code were to set the page dirty
679 * before the buffers, a concurrent writepage caller could clear the page dirty
680 * bit, see a bunch of clean buffers and we'd end up with dirty buffers/clean
681 * page on the dirty page list.
683 * We use private_lock to lock against try_to_free_buffers while using the
684 * page's buffer list. Also use this to protect against clean buffers being
685 * added to the page after it was set dirty.
687 * FIXME: may need to call ->reservepage here as well. That's rather up to the
688 * address_space though.
690 int __set_page_dirty_buffers(struct page *page)
692 int newly_dirty;
693 struct address_space *mapping = page_mapping(page);
695 if (unlikely(!mapping))
696 return !TestSetPageDirty(page);
698 spin_lock(&mapping->private_lock);
699 if (page_has_buffers(page)) {
700 struct buffer_head *head = page_buffers(page);
701 struct buffer_head *bh = head;
703 do {
704 set_buffer_dirty(bh);
705 bh = bh->b_this_page;
706 } while (bh != head);
708 newly_dirty = !TestSetPageDirty(page);
709 spin_unlock(&mapping->private_lock);
711 if (newly_dirty)
712 __set_page_dirty(page, mapping, 1);
713 return newly_dirty;
715 EXPORT_SYMBOL(__set_page_dirty_buffers);
718 * Write out and wait upon a list of buffers.
720 * We have conflicting pressures: we want to make sure that all
721 * initially dirty buffers get waited on, but that any subsequently
722 * dirtied buffers don't. After all, we don't want fsync to last
723 * forever if somebody is actively writing to the file.
725 * Do this in two main stages: first we copy dirty buffers to a
726 * temporary inode list, queueing the writes as we go. Then we clean
727 * up, waiting for those writes to complete.
729 * During this second stage, any subsequent updates to the file may end
730 * up refiling the buffer on the original inode's dirty list again, so
731 * there is a chance we will end up with a buffer queued for write but
732 * not yet completed on that list. So, as a final cleanup we go through
733 * the osync code to catch these locked, dirty buffers without requeuing
734 * any newly dirty buffers for write.
736 static int fsync_buffers_list(spinlock_t *lock, struct list_head *list)
738 struct buffer_head *bh;
739 struct list_head tmp;
740 struct address_space *mapping;
741 int err = 0, err2;
743 INIT_LIST_HEAD(&tmp);
745 spin_lock(lock);
746 while (!list_empty(list)) {
747 bh = BH_ENTRY(list->next);
748 mapping = bh->b_assoc_map;
749 __remove_assoc_queue(bh);
750 /* Avoid race with mark_buffer_dirty_inode() which does
751 * a lockless check and we rely on seeing the dirty bit */
752 smp_mb();
753 if (buffer_dirty(bh) || buffer_locked(bh)) {
754 list_add(&bh->b_assoc_buffers, &tmp);
755 bh->b_assoc_map = mapping;
756 if (buffer_dirty(bh)) {
757 get_bh(bh);
758 spin_unlock(lock);
760 * Ensure any pending I/O completes so that
761 * ll_rw_block() actually writes the current
762 * contents - it is a noop if I/O is still in
763 * flight on potentially older contents.
765 ll_rw_block(SWRITE_SYNC, 1, &bh);
766 brelse(bh);
767 spin_lock(lock);
772 while (!list_empty(&tmp)) {
773 bh = BH_ENTRY(tmp.prev);
774 get_bh(bh);
775 mapping = bh->b_assoc_map;
776 __remove_assoc_queue(bh);
777 /* Avoid race with mark_buffer_dirty_inode() which does
778 * a lockless check and we rely on seeing the dirty bit */
779 smp_mb();
780 if (buffer_dirty(bh)) {
781 list_add(&bh->b_assoc_buffers,
782 &mapping->private_list);
783 bh->b_assoc_map = mapping;
785 spin_unlock(lock);
786 wait_on_buffer(bh);
787 if (!buffer_uptodate(bh))
788 err = -EIO;
789 brelse(bh);
790 spin_lock(lock);
793 spin_unlock(lock);
794 err2 = osync_buffers_list(lock, list);
795 if (err)
796 return err;
797 else
798 return err2;
802 * Invalidate any and all dirty buffers on a given inode. We are
803 * probably unmounting the fs, but that doesn't mean we have already
804 * done a sync(). Just drop the buffers from the inode list.
806 * NOTE: we take the inode's blockdev's mapping's private_lock. Which
807 * assumes that all the buffers are against the blockdev. Not true
808 * for reiserfs.
810 void invalidate_inode_buffers(struct inode *inode)
812 if (inode_has_buffers(inode)) {
813 struct address_space *mapping = &inode->i_data;
814 struct list_head *list = &mapping->private_list;
815 struct address_space *buffer_mapping = mapping->assoc_mapping;
817 spin_lock(&buffer_mapping->private_lock);
818 while (!list_empty(list))
819 __remove_assoc_queue(BH_ENTRY(list->next));
820 spin_unlock(&buffer_mapping->private_lock);
823 EXPORT_SYMBOL(invalidate_inode_buffers);
826 * Remove any clean buffers from the inode's buffer list. This is called
827 * when we're trying to free the inode itself. Those buffers can pin it.
829 * Returns true if all buffers were removed.
831 int remove_inode_buffers(struct inode *inode)
833 int ret = 1;
835 if (inode_has_buffers(inode)) {
836 struct address_space *mapping = &inode->i_data;
837 struct list_head *list = &mapping->private_list;
838 struct address_space *buffer_mapping = mapping->assoc_mapping;
840 spin_lock(&buffer_mapping->private_lock);
841 while (!list_empty(list)) {
842 struct buffer_head *bh = BH_ENTRY(list->next);
843 if (buffer_dirty(bh)) {
844 ret = 0;
845 break;
847 __remove_assoc_queue(bh);
849 spin_unlock(&buffer_mapping->private_lock);
851 return ret;
855 * Create the appropriate buffers when given a page for data area and
856 * the size of each buffer.. Use the bh->b_this_page linked list to
857 * follow the buffers created. Return NULL if unable to create more
858 * buffers.
860 * The retry flag is used to differentiate async IO (paging, swapping)
861 * which may not fail from ordinary buffer allocations.
863 struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size,
864 int retry)
866 struct buffer_head *bh, *head;
867 long offset;
869 try_again:
870 head = NULL;
871 offset = PAGE_SIZE;
872 while ((offset -= size) >= 0) {
873 bh = alloc_buffer_head(GFP_NOFS);
874 if (!bh)
875 goto no_grow;
877 bh->b_bdev = NULL;
878 bh->b_this_page = head;
879 bh->b_blocknr = -1;
880 head = bh;
882 bh->b_state = 0;
883 atomic_set(&bh->b_count, 0);
884 bh->b_private = NULL;
885 bh->b_size = size;
887 /* Link the buffer to its page */
888 set_bh_page(bh, page, offset);
890 init_buffer(bh, NULL, NULL);
892 return head;
894 * In case anything failed, we just free everything we got.
896 no_grow:
897 if (head) {
898 do {
899 bh = head;
900 head = head->b_this_page;
901 free_buffer_head(bh);
902 } while (head);
906 * Return failure for non-async IO requests. Async IO requests
907 * are not allowed to fail, so we have to wait until buffer heads
908 * become available. But we don't want tasks sleeping with
909 * partially complete buffers, so all were released above.
911 if (!retry)
912 return NULL;
914 /* We're _really_ low on memory. Now we just
915 * wait for old buffer heads to become free due to
916 * finishing IO. Since this is an async request and
917 * the reserve list is empty, we're sure there are
918 * async buffer heads in use.
920 free_more_memory();
921 goto try_again;
923 EXPORT_SYMBOL_GPL(alloc_page_buffers);
925 static inline void
926 link_dev_buffers(struct page *page, struct buffer_head *head)
928 struct buffer_head *bh, *tail;
930 bh = head;
931 do {
932 tail = bh;
933 bh = bh->b_this_page;
934 } while (bh);
935 tail->b_this_page = head;
936 attach_page_buffers(page, head);
940 * Initialise the state of a blockdev page's buffers.
942 static void
943 init_page_buffers(struct page *page, struct block_device *bdev,
944 sector_t block, int size)
946 struct buffer_head *head = page_buffers(page);
947 struct buffer_head *bh = head;
948 int uptodate = PageUptodate(page);
950 do {
951 if (!buffer_mapped(bh)) {
952 init_buffer(bh, NULL, NULL);
953 bh->b_bdev = bdev;
954 bh->b_blocknr = block;
955 if (uptodate)
956 set_buffer_uptodate(bh);
957 set_buffer_mapped(bh);
959 block++;
960 bh = bh->b_this_page;
961 } while (bh != head);
965 * Create the page-cache page that contains the requested block.
967 * This is user purely for blockdev mappings.
969 static struct page *
970 grow_dev_page(struct block_device *bdev, sector_t block,
971 pgoff_t index, int size)
973 struct inode *inode = bdev->bd_inode;
974 struct page *page;
975 struct buffer_head *bh;
977 page = find_or_create_page(inode->i_mapping, index,
978 (mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS)|__GFP_MOVABLE);
979 if (!page)
980 return NULL;
982 BUG_ON(!PageLocked(page));
984 if (page_has_buffers(page)) {
985 bh = page_buffers(page);
986 if (bh->b_size == size) {
987 init_page_buffers(page, bdev, block, size);
988 return page;
990 if (!try_to_free_buffers(page))
991 goto failed;
995 * Allocate some buffers for this page
997 bh = alloc_page_buffers(page, size, 0);
998 if (!bh)
999 goto failed;
1002 * Link the page to the buffers and initialise them. Take the
1003 * lock to be atomic wrt __find_get_block(), which does not
1004 * run under the page lock.
1006 spin_lock(&inode->i_mapping->private_lock);
1007 link_dev_buffers(page, bh);
1008 init_page_buffers(page, bdev, block, size);
1009 spin_unlock(&inode->i_mapping->private_lock);
1010 return page;
1012 failed:
1013 BUG();
1014 unlock_page(page);
1015 page_cache_release(page);
1016 return NULL;
1020 * Create buffers for the specified block device block's page. If
1021 * that page was dirty, the buffers are set dirty also.
1023 static int
1024 grow_buffers(struct block_device *bdev, sector_t block, int size)
1026 struct page *page;
1027 pgoff_t index;
1028 int sizebits;
1030 sizebits = -1;
1031 do {
1032 sizebits++;
1033 } while ((size << sizebits) < PAGE_SIZE);
1035 index = block >> sizebits;
1038 * Check for a block which wants to lie outside our maximum possible
1039 * pagecache index. (this comparison is done using sector_t types).
1041 if (unlikely(index != block >> sizebits)) {
1042 char b[BDEVNAME_SIZE];
1044 printk(KERN_ERR "%s: requested out-of-range block %llu for "
1045 "device %s\n",
1046 __func__, (unsigned long long)block,
1047 bdevname(bdev, b));
1048 return -EIO;
1050 block = index << sizebits;
1051 /* Create a page with the proper size buffers.. */
1052 page = grow_dev_page(bdev, block, index, size);
1053 if (!page)
1054 return 0;
1055 unlock_page(page);
1056 page_cache_release(page);
1057 return 1;
1060 static struct buffer_head *
1061 __getblk_slow(struct block_device *bdev, sector_t block, int size)
1063 /* Size must be multiple of hard sectorsize */
1064 if (unlikely(size & (bdev_hardsect_size(bdev)-1) ||
1065 (size < 512 || size > PAGE_SIZE))) {
1066 printk(KERN_ERR "getblk(): invalid block size %d requested\n",
1067 size);
1068 printk(KERN_ERR "hardsect size: %d\n",
1069 bdev_hardsect_size(bdev));
1071 dump_stack();
1072 return NULL;
1075 for (;;) {
1076 struct buffer_head * bh;
1077 int ret;
1079 bh = __find_get_block(bdev, block, size);
1080 if (bh)
1081 return bh;
1083 ret = grow_buffers(bdev, block, size);
1084 if (ret < 0)
1085 return NULL;
1086 if (ret == 0)
1087 free_more_memory();
1092 * The relationship between dirty buffers and dirty pages:
1094 * Whenever a page has any dirty buffers, the page's dirty bit is set, and
1095 * the page is tagged dirty in its radix tree.
1097 * At all times, the dirtiness of the buffers represents the dirtiness of
1098 * subsections of the page. If the page has buffers, the page dirty bit is
1099 * merely a hint about the true dirty state.
1101 * When a page is set dirty in its entirety, all its buffers are marked dirty
1102 * (if the page has buffers).
1104 * When a buffer is marked dirty, its page is dirtied, but the page's other
1105 * buffers are not.
1107 * Also. When blockdev buffers are explicitly read with bread(), they
1108 * individually become uptodate. But their backing page remains not
1109 * uptodate - even if all of its buffers are uptodate. A subsequent
1110 * block_read_full_page() against that page will discover all the uptodate
1111 * buffers, will set the page uptodate and will perform no I/O.
1115 * mark_buffer_dirty - mark a buffer_head as needing writeout
1116 * @bh: the buffer_head to mark dirty
1118 * mark_buffer_dirty() will set the dirty bit against the buffer, then set its
1119 * backing page dirty, then tag the page as dirty in its address_space's radix
1120 * tree and then attach the address_space's inode to its superblock's dirty
1121 * inode list.
1123 * mark_buffer_dirty() is atomic. It takes bh->b_page->mapping->private_lock,
1124 * mapping->tree_lock and the global inode_lock.
1126 void mark_buffer_dirty(struct buffer_head *bh)
1128 WARN_ON_ONCE(!buffer_uptodate(bh));
1131 * Very *carefully* optimize the it-is-already-dirty case.
1133 * Don't let the final "is it dirty" escape to before we
1134 * perhaps modified the buffer.
1136 if (buffer_dirty(bh)) {
1137 smp_mb();
1138 if (buffer_dirty(bh))
1139 return;
1142 if (!test_set_buffer_dirty(bh)) {
1143 struct page *page = bh->b_page;
1144 if (!TestSetPageDirty(page))
1145 __set_page_dirty(page, page_mapping(page), 0);
1150 * Decrement a buffer_head's reference count. If all buffers against a page
1151 * have zero reference count, are clean and unlocked, and if the page is clean
1152 * and unlocked then try_to_free_buffers() may strip the buffers from the page
1153 * in preparation for freeing it (sometimes, rarely, buffers are removed from
1154 * a page but it ends up not being freed, and buffers may later be reattached).
1156 void __brelse(struct buffer_head * buf)
1158 if (atomic_read(&buf->b_count)) {
1159 put_bh(buf);
1160 return;
1162 WARN(1, KERN_ERR "VFS: brelse: Trying to free free buffer\n");
1166 * bforget() is like brelse(), except it discards any
1167 * potentially dirty data.
1169 void __bforget(struct buffer_head *bh)
1171 clear_buffer_dirty(bh);
1172 if (bh->b_assoc_map) {
1173 struct address_space *buffer_mapping = bh->b_page->mapping;
1175 spin_lock(&buffer_mapping->private_lock);
1176 list_del_init(&bh->b_assoc_buffers);
1177 bh->b_assoc_map = NULL;
1178 spin_unlock(&buffer_mapping->private_lock);
1180 __brelse(bh);
1183 static struct buffer_head *__bread_slow(struct buffer_head *bh)
1185 lock_buffer(bh);
1186 if (buffer_uptodate(bh)) {
1187 unlock_buffer(bh);
1188 return bh;
1189 } else {
1190 get_bh(bh);
1191 bh->b_end_io = end_buffer_read_sync;
1192 submit_bh(READ, bh);
1193 wait_on_buffer(bh);
1194 if (buffer_uptodate(bh))
1195 return bh;
1197 brelse(bh);
1198 return NULL;
1202 * Per-cpu buffer LRU implementation. To reduce the cost of __find_get_block().
1203 * The bhs[] array is sorted - newest buffer is at bhs[0]. Buffers have their
1204 * refcount elevated by one when they're in an LRU. A buffer can only appear
1205 * once in a particular CPU's LRU. A single buffer can be present in multiple
1206 * CPU's LRUs at the same time.
1208 * This is a transparent caching front-end to sb_bread(), sb_getblk() and
1209 * sb_find_get_block().
1211 * The LRUs themselves only need locking against invalidate_bh_lrus. We use
1212 * a local interrupt disable for that.
1215 #define BH_LRU_SIZE 8
1217 struct bh_lru {
1218 struct buffer_head *bhs[BH_LRU_SIZE];
1221 static DEFINE_PER_CPU(struct bh_lru, bh_lrus) = {{ NULL }};
1223 #ifdef CONFIG_SMP
1224 #define bh_lru_lock() local_irq_disable()
1225 #define bh_lru_unlock() local_irq_enable()
1226 #else
1227 #define bh_lru_lock() preempt_disable()
1228 #define bh_lru_unlock() preempt_enable()
1229 #endif
1231 static inline void check_irqs_on(void)
1233 #ifdef irqs_disabled
1234 BUG_ON(irqs_disabled());
1235 #endif
1239 * The LRU management algorithm is dopey-but-simple. Sorry.
1241 static void bh_lru_install(struct buffer_head *bh)
1243 struct buffer_head *evictee = NULL;
1244 struct bh_lru *lru;
1246 check_irqs_on();
1247 bh_lru_lock();
1248 lru = &__get_cpu_var(bh_lrus);
1249 if (lru->bhs[0] != bh) {
1250 struct buffer_head *bhs[BH_LRU_SIZE];
1251 int in;
1252 int out = 0;
1254 get_bh(bh);
1255 bhs[out++] = bh;
1256 for (in = 0; in < BH_LRU_SIZE; in++) {
1257 struct buffer_head *bh2 = lru->bhs[in];
1259 if (bh2 == bh) {
1260 __brelse(bh2);
1261 } else {
1262 if (out >= BH_LRU_SIZE) {
1263 BUG_ON(evictee != NULL);
1264 evictee = bh2;
1265 } else {
1266 bhs[out++] = bh2;
1270 while (out < BH_LRU_SIZE)
1271 bhs[out++] = NULL;
1272 memcpy(lru->bhs, bhs, sizeof(bhs));
1274 bh_lru_unlock();
1276 if (evictee)
1277 __brelse(evictee);
1281 * Look up the bh in this cpu's LRU. If it's there, move it to the head.
1283 static struct buffer_head *
1284 lookup_bh_lru(struct block_device *bdev, sector_t block, unsigned size)
1286 struct buffer_head *ret = NULL;
1287 struct bh_lru *lru;
1288 unsigned int i;
1290 check_irqs_on();
1291 bh_lru_lock();
1292 lru = &__get_cpu_var(bh_lrus);
1293 for (i = 0; i < BH_LRU_SIZE; i++) {
1294 struct buffer_head *bh = lru->bhs[i];
1296 if (bh && bh->b_bdev == bdev &&
1297 bh->b_blocknr == block && bh->b_size == size) {
1298 if (i) {
1299 while (i) {
1300 lru->bhs[i] = lru->bhs[i - 1];
1301 i--;
1303 lru->bhs[0] = bh;
1305 get_bh(bh);
1306 ret = bh;
1307 break;
1310 bh_lru_unlock();
1311 return ret;
1315 * Perform a pagecache lookup for the matching buffer. If it's there, refresh
1316 * it in the LRU and mark it as accessed. If it is not present then return
1317 * NULL
1319 struct buffer_head *
1320 __find_get_block(struct block_device *bdev, sector_t block, unsigned size)
1322 struct buffer_head *bh = lookup_bh_lru(bdev, block, size);
1324 if (bh == NULL) {
1325 bh = __find_get_block_slow(bdev, block);
1326 if (bh)
1327 bh_lru_install(bh);
1329 if (bh)
1330 touch_buffer(bh);
1331 return bh;
1333 EXPORT_SYMBOL(__find_get_block);
1336 * __getblk will locate (and, if necessary, create) the buffer_head
1337 * which corresponds to the passed block_device, block and size. The
1338 * returned buffer has its reference count incremented.
1340 * __getblk() cannot fail - it just keeps trying. If you pass it an
1341 * illegal block number, __getblk() will happily return a buffer_head
1342 * which represents the non-existent block. Very weird.
1344 * __getblk() will lock up the machine if grow_dev_page's try_to_free_buffers()
1345 * attempt is failing. FIXME, perhaps?
1347 struct buffer_head *
1348 __getblk(struct block_device *bdev, sector_t block, unsigned size)
1350 struct buffer_head *bh = __find_get_block(bdev, block, size);
1352 might_sleep();
1353 if (bh == NULL)
1354 bh = __getblk_slow(bdev, block, size);
1355 return bh;
1357 EXPORT_SYMBOL(__getblk);
1360 * Do async read-ahead on a buffer..
1362 void __breadahead(struct block_device *bdev, sector_t block, unsigned size)
1364 struct buffer_head *bh = __getblk(bdev, block, size);
1365 if (likely(bh)) {
1366 ll_rw_block(READA, 1, &bh);
1367 brelse(bh);
1370 EXPORT_SYMBOL(__breadahead);
1373 * __bread() - reads a specified block and returns the bh
1374 * @bdev: the block_device to read from
1375 * @block: number of block
1376 * @size: size (in bytes) to read
1378 * Reads a specified block, and returns buffer head that contains it.
1379 * It returns NULL if the block was unreadable.
1381 struct buffer_head *
1382 __bread(struct block_device *bdev, sector_t block, unsigned size)
1384 struct buffer_head *bh = __getblk(bdev, block, size);
1386 if (likely(bh) && !buffer_uptodate(bh))
1387 bh = __bread_slow(bh);
1388 return bh;
1390 EXPORT_SYMBOL(__bread);
1393 * invalidate_bh_lrus() is called rarely - but not only at unmount.
1394 * This doesn't race because it runs in each cpu either in irq
1395 * or with preempt disabled.
1397 static void invalidate_bh_lru(void *arg)
1399 struct bh_lru *b = &get_cpu_var(bh_lrus);
1400 int i;
1402 for (i = 0; i < BH_LRU_SIZE; i++) {
1403 brelse(b->bhs[i]);
1404 b->bhs[i] = NULL;
1406 put_cpu_var(bh_lrus);
1409 void invalidate_bh_lrus(void)
1411 on_each_cpu(invalidate_bh_lru, NULL, 1);
1413 EXPORT_SYMBOL_GPL(invalidate_bh_lrus);
1415 void set_bh_page(struct buffer_head *bh,
1416 struct page *page, unsigned long offset)
1418 bh->b_page = page;
1419 BUG_ON(offset >= PAGE_SIZE);
1420 if (PageHighMem(page))
1422 * This catches illegal uses and preserves the offset:
1424 bh->b_data = (char *)(0 + offset);
1425 else
1426 bh->b_data = page_address(page) + offset;
1428 EXPORT_SYMBOL(set_bh_page);
1431 * Called when truncating a buffer on a page completely.
1433 static void discard_buffer(struct buffer_head * bh)
1435 lock_buffer(bh);
1436 clear_buffer_dirty(bh);
1437 bh->b_bdev = NULL;
1438 clear_buffer_mapped(bh);
1439 clear_buffer_req(bh);
1440 clear_buffer_new(bh);
1441 clear_buffer_delay(bh);
1442 clear_buffer_unwritten(bh);
1443 unlock_buffer(bh);
1447 * block_invalidatepage - invalidate part of all of a buffer-backed page
1449 * @page: the page which is affected
1450 * @offset: the index of the truncation point
1452 * block_invalidatepage() is called when all or part of the page has become
1453 * invalidatedby a truncate operation.
1455 * block_invalidatepage() does not have to release all buffers, but it must
1456 * ensure that no dirty buffer is left outside @offset and that no I/O
1457 * is underway against any of the blocks which are outside the truncation
1458 * point. Because the caller is about to free (and possibly reuse) those
1459 * blocks on-disk.
1461 void block_invalidatepage(struct page *page, unsigned long offset)
1463 struct buffer_head *head, *bh, *next;
1464 unsigned int curr_off = 0;
1466 BUG_ON(!PageLocked(page));
1467 if (!page_has_buffers(page))
1468 goto out;
1470 head = page_buffers(page);
1471 bh = head;
1472 do {
1473 unsigned int next_off = curr_off + bh->b_size;
1474 next = bh->b_this_page;
1477 * is this block fully invalidated?
1479 if (offset <= curr_off)
1480 discard_buffer(bh);
1481 curr_off = next_off;
1482 bh = next;
1483 } while (bh != head);
1486 * We release buffers only if the entire page is being invalidated.
1487 * The get_block cached value has been unconditionally invalidated,
1488 * so real IO is not possible anymore.
1490 if (offset == 0)
1491 try_to_release_page(page, 0);
1492 out:
1493 return;
1495 EXPORT_SYMBOL(block_invalidatepage);
1498 * We attach and possibly dirty the buffers atomically wrt
1499 * __set_page_dirty_buffers() via private_lock. try_to_free_buffers
1500 * is already excluded via the page lock.
1502 void create_empty_buffers(struct page *page,
1503 unsigned long blocksize, unsigned long b_state)
1505 struct buffer_head *bh, *head, *tail;
1507 head = alloc_page_buffers(page, blocksize, 1);
1508 bh = head;
1509 do {
1510 bh->b_state |= b_state;
1511 tail = bh;
1512 bh = bh->b_this_page;
1513 } while (bh);
1514 tail->b_this_page = head;
1516 spin_lock(&page->mapping->private_lock);
1517 if (PageUptodate(page) || PageDirty(page)) {
1518 bh = head;
1519 do {
1520 if (PageDirty(page))
1521 set_buffer_dirty(bh);
1522 if (PageUptodate(page))
1523 set_buffer_uptodate(bh);
1524 bh = bh->b_this_page;
1525 } while (bh != head);
1527 attach_page_buffers(page, head);
1528 spin_unlock(&page->mapping->private_lock);
1530 EXPORT_SYMBOL(create_empty_buffers);
1533 * We are taking a block for data and we don't want any output from any
1534 * buffer-cache aliases starting from return from that function and
1535 * until the moment when something will explicitly mark the buffer
1536 * dirty (hopefully that will not happen until we will free that block ;-)
1537 * We don't even need to mark it not-uptodate - nobody can expect
1538 * anything from a newly allocated buffer anyway. We used to used
1539 * unmap_buffer() for such invalidation, but that was wrong. We definitely
1540 * don't want to mark the alias unmapped, for example - it would confuse
1541 * anyone who might pick it with bread() afterwards...
1543 * Also.. Note that bforget() doesn't lock the buffer. So there can
1544 * be writeout I/O going on against recently-freed buffers. We don't
1545 * wait on that I/O in bforget() - it's more efficient to wait on the I/O
1546 * only if we really need to. That happens here.
1548 void unmap_underlying_metadata(struct block_device *bdev, sector_t block)
1550 struct buffer_head *old_bh;
1552 might_sleep();
1554 old_bh = __find_get_block_slow(bdev, block);
1555 if (old_bh) {
1556 clear_buffer_dirty(old_bh);
1557 wait_on_buffer(old_bh);
1558 clear_buffer_req(old_bh);
1559 __brelse(old_bh);
1562 EXPORT_SYMBOL(unmap_underlying_metadata);
1565 * NOTE! All mapped/uptodate combinations are valid:
1567 * Mapped Uptodate Meaning
1569 * No No "unknown" - must do get_block()
1570 * No Yes "hole" - zero-filled
1571 * Yes No "allocated" - allocated on disk, not read in
1572 * Yes Yes "valid" - allocated and up-to-date in memory.
1574 * "Dirty" is valid only with the last case (mapped+uptodate).
1578 * While block_write_full_page is writing back the dirty buffers under
1579 * the page lock, whoever dirtied the buffers may decide to clean them
1580 * again at any time. We handle that by only looking at the buffer
1581 * state inside lock_buffer().
1583 * If block_write_full_page() is called for regular writeback
1584 * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
1585 * locked buffer. This only can happen if someone has written the buffer
1586 * directly, with submit_bh(). At the address_space level PageWriteback
1587 * prevents this contention from occurring.
1589 static int __block_write_full_page(struct inode *inode, struct page *page,
1590 get_block_t *get_block, struct writeback_control *wbc)
1592 int err;
1593 sector_t block;
1594 sector_t last_block;
1595 struct buffer_head *bh, *head;
1596 const unsigned blocksize = 1 << inode->i_blkbits;
1597 int nr_underway = 0;
1599 BUG_ON(!PageLocked(page));
1601 last_block = (i_size_read(inode) - 1) >> inode->i_blkbits;
1603 if (!page_has_buffers(page)) {
1604 create_empty_buffers(page, blocksize,
1605 (1 << BH_Dirty)|(1 << BH_Uptodate));
1609 * Be very careful. We have no exclusion from __set_page_dirty_buffers
1610 * here, and the (potentially unmapped) buffers may become dirty at
1611 * any time. If a buffer becomes dirty here after we've inspected it
1612 * then we just miss that fact, and the page stays dirty.
1614 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
1615 * handle that here by just cleaning them.
1618 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1619 head = page_buffers(page);
1620 bh = head;
1623 * Get all the dirty buffers mapped to disk addresses and
1624 * handle any aliases from the underlying blockdev's mapping.
1626 do {
1627 if (block > last_block) {
1629 * mapped buffers outside i_size will occur, because
1630 * this page can be outside i_size when there is a
1631 * truncate in progress.
1634 * The buffer was zeroed by block_write_full_page()
1636 clear_buffer_dirty(bh);
1637 set_buffer_uptodate(bh);
1638 } else if ((!buffer_mapped(bh) || buffer_delay(bh)) &&
1639 buffer_dirty(bh)) {
1640 WARN_ON(bh->b_size != blocksize);
1641 err = get_block(inode, block, bh, 1);
1642 if (err)
1643 goto recover;
1644 clear_buffer_delay(bh);
1645 if (buffer_new(bh)) {
1646 /* blockdev mappings never come here */
1647 clear_buffer_new(bh);
1648 unmap_underlying_metadata(bh->b_bdev,
1649 bh->b_blocknr);
1652 bh = bh->b_this_page;
1653 block++;
1654 } while (bh != head);
1656 do {
1657 if (!buffer_mapped(bh))
1658 continue;
1660 * If it's a fully non-blocking write attempt and we cannot
1661 * lock the buffer then redirty the page. Note that this can
1662 * potentially cause a busy-wait loop from pdflush and kswapd
1663 * activity, but those code paths have their own higher-level
1664 * throttling.
1666 if (wbc->sync_mode != WB_SYNC_NONE || !wbc->nonblocking) {
1667 lock_buffer(bh);
1668 } else if (!trylock_buffer(bh)) {
1669 redirty_page_for_writepage(wbc, page);
1670 continue;
1672 if (test_clear_buffer_dirty(bh)) {
1673 mark_buffer_async_write(bh);
1674 } else {
1675 unlock_buffer(bh);
1677 } while ((bh = bh->b_this_page) != head);
1680 * The page and its buffers are protected by PageWriteback(), so we can
1681 * drop the bh refcounts early.
1683 BUG_ON(PageWriteback(page));
1684 set_page_writeback(page);
1686 do {
1687 struct buffer_head *next = bh->b_this_page;
1688 if (buffer_async_write(bh)) {
1689 submit_bh(WRITE, bh);
1690 nr_underway++;
1692 bh = next;
1693 } while (bh != head);
1694 unlock_page(page);
1696 err = 0;
1697 done:
1698 if (nr_underway == 0) {
1700 * The page was marked dirty, but the buffers were
1701 * clean. Someone wrote them back by hand with
1702 * ll_rw_block/submit_bh. A rare case.
1704 end_page_writeback(page);
1707 * The page and buffer_heads can be released at any time from
1708 * here on.
1711 return err;
1713 recover:
1715 * ENOSPC, or some other error. We may already have added some
1716 * blocks to the file, so we need to write these out to avoid
1717 * exposing stale data.
1718 * The page is currently locked and not marked for writeback
1720 bh = head;
1721 /* Recovery: lock and submit the mapped buffers */
1722 do {
1723 if (buffer_mapped(bh) && buffer_dirty(bh) &&
1724 !buffer_delay(bh)) {
1725 lock_buffer(bh);
1726 mark_buffer_async_write(bh);
1727 } else {
1729 * The buffer may have been set dirty during
1730 * attachment to a dirty page.
1732 clear_buffer_dirty(bh);
1734 } while ((bh = bh->b_this_page) != head);
1735 SetPageError(page);
1736 BUG_ON(PageWriteback(page));
1737 mapping_set_error(page->mapping, err);
1738 set_page_writeback(page);
1739 do {
1740 struct buffer_head *next = bh->b_this_page;
1741 if (buffer_async_write(bh)) {
1742 clear_buffer_dirty(bh);
1743 submit_bh(WRITE, bh);
1744 nr_underway++;
1746 bh = next;
1747 } while (bh != head);
1748 unlock_page(page);
1749 goto done;
1753 * If a page has any new buffers, zero them out here, and mark them uptodate
1754 * and dirty so they'll be written out (in order to prevent uninitialised
1755 * block data from leaking). And clear the new bit.
1757 void page_zero_new_buffers(struct page *page, unsigned from, unsigned to)
1759 unsigned int block_start, block_end;
1760 struct buffer_head *head, *bh;
1762 BUG_ON(!PageLocked(page));
1763 if (!page_has_buffers(page))
1764 return;
1766 bh = head = page_buffers(page);
1767 block_start = 0;
1768 do {
1769 block_end = block_start + bh->b_size;
1771 if (buffer_new(bh)) {
1772 if (block_end > from && block_start < to) {
1773 if (!PageUptodate(page)) {
1774 unsigned start, size;
1776 start = max(from, block_start);
1777 size = min(to, block_end) - start;
1779 zero_user(page, start, size);
1780 set_buffer_uptodate(bh);
1783 clear_buffer_new(bh);
1784 mark_buffer_dirty(bh);
1788 block_start = block_end;
1789 bh = bh->b_this_page;
1790 } while (bh != head);
1792 EXPORT_SYMBOL(page_zero_new_buffers);
1794 static int __block_prepare_write(struct inode *inode, struct page *page,
1795 unsigned from, unsigned to, get_block_t *get_block)
1797 unsigned block_start, block_end;
1798 sector_t block;
1799 int err = 0;
1800 unsigned blocksize, bbits;
1801 struct buffer_head *bh, *head, *wait[2], **wait_bh=wait;
1803 BUG_ON(!PageLocked(page));
1804 BUG_ON(from > PAGE_CACHE_SIZE);
1805 BUG_ON(to > PAGE_CACHE_SIZE);
1806 BUG_ON(from > to);
1808 blocksize = 1 << inode->i_blkbits;
1809 if (!page_has_buffers(page))
1810 create_empty_buffers(page, blocksize, 0);
1811 head = page_buffers(page);
1813 bbits = inode->i_blkbits;
1814 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - bbits);
1816 for(bh = head, block_start = 0; bh != head || !block_start;
1817 block++, block_start=block_end, bh = bh->b_this_page) {
1818 block_end = block_start + blocksize;
1819 if (block_end <= from || block_start >= to) {
1820 if (PageUptodate(page)) {
1821 if (!buffer_uptodate(bh))
1822 set_buffer_uptodate(bh);
1824 continue;
1826 if (buffer_new(bh))
1827 clear_buffer_new(bh);
1828 if (!buffer_mapped(bh)) {
1829 WARN_ON(bh->b_size != blocksize);
1830 err = get_block(inode, block, bh, 1);
1831 if (err)
1832 break;
1833 if (buffer_new(bh)) {
1834 unmap_underlying_metadata(bh->b_bdev,
1835 bh->b_blocknr);
1836 if (PageUptodate(page)) {
1837 clear_buffer_new(bh);
1838 set_buffer_uptodate(bh);
1839 mark_buffer_dirty(bh);
1840 continue;
1842 if (block_end > to || block_start < from)
1843 zero_user_segments(page,
1844 to, block_end,
1845 block_start, from);
1846 continue;
1849 if (PageUptodate(page)) {
1850 if (!buffer_uptodate(bh))
1851 set_buffer_uptodate(bh);
1852 continue;
1854 if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
1855 !buffer_unwritten(bh) &&
1856 (block_start < from || block_end > to)) {
1857 ll_rw_block(READ, 1, &bh);
1858 *wait_bh++=bh;
1862 * If we issued read requests - let them complete.
1864 while(wait_bh > wait) {
1865 wait_on_buffer(*--wait_bh);
1866 if (!buffer_uptodate(*wait_bh))
1867 err = -EIO;
1869 if (unlikely(err))
1870 page_zero_new_buffers(page, from, to);
1871 return err;
1874 static int __block_commit_write(struct inode *inode, struct page *page,
1875 unsigned from, unsigned to)
1877 unsigned block_start, block_end;
1878 int partial = 0;
1879 unsigned blocksize;
1880 struct buffer_head *bh, *head;
1882 blocksize = 1 << inode->i_blkbits;
1884 for(bh = head = page_buffers(page), block_start = 0;
1885 bh != head || !block_start;
1886 block_start=block_end, bh = bh->b_this_page) {
1887 block_end = block_start + blocksize;
1888 if (block_end <= from || block_start >= to) {
1889 if (!buffer_uptodate(bh))
1890 partial = 1;
1891 } else {
1892 set_buffer_uptodate(bh);
1893 mark_buffer_dirty(bh);
1895 clear_buffer_new(bh);
1899 * If this is a partial write which happened to make all buffers
1900 * uptodate then we can optimize away a bogus readpage() for
1901 * the next read(). Here we 'discover' whether the page went
1902 * uptodate as a result of this (potentially partial) write.
1904 if (!partial)
1905 SetPageUptodate(page);
1906 return 0;
1910 * block_write_begin takes care of the basic task of block allocation and
1911 * bringing partial write blocks uptodate first.
1913 * If *pagep is not NULL, then block_write_begin uses the locked page
1914 * at *pagep rather than allocating its own. In this case, the page will
1915 * not be unlocked or deallocated on failure.
1917 int block_write_begin(struct file *file, struct address_space *mapping,
1918 loff_t pos, unsigned len, unsigned flags,
1919 struct page **pagep, void **fsdata,
1920 get_block_t *get_block)
1922 struct inode *inode = mapping->host;
1923 int status = 0;
1924 struct page *page;
1925 pgoff_t index;
1926 unsigned start, end;
1927 int ownpage = 0;
1929 index = pos >> PAGE_CACHE_SHIFT;
1930 start = pos & (PAGE_CACHE_SIZE - 1);
1931 end = start + len;
1933 page = *pagep;
1934 if (page == NULL) {
1935 ownpage = 1;
1936 page = grab_cache_page_write_begin(mapping, index, flags);
1937 if (!page) {
1938 status = -ENOMEM;
1939 goto out;
1941 *pagep = page;
1942 } else
1943 BUG_ON(!PageLocked(page));
1945 status = __block_prepare_write(inode, page, start, end, get_block);
1946 if (unlikely(status)) {
1947 ClearPageUptodate(page);
1949 if (ownpage) {
1950 unlock_page(page);
1951 page_cache_release(page);
1952 *pagep = NULL;
1955 * prepare_write() may have instantiated a few blocks
1956 * outside i_size. Trim these off again. Don't need
1957 * i_size_read because we hold i_mutex.
1959 if (pos + len > inode->i_size)
1960 vmtruncate(inode, inode->i_size);
1964 out:
1965 return status;
1967 EXPORT_SYMBOL(block_write_begin);
1969 int block_write_end(struct file *file, struct address_space *mapping,
1970 loff_t pos, unsigned len, unsigned copied,
1971 struct page *page, void *fsdata)
1973 struct inode *inode = mapping->host;
1974 unsigned start;
1976 start = pos & (PAGE_CACHE_SIZE - 1);
1978 if (unlikely(copied < len)) {
1980 * The buffers that were written will now be uptodate, so we
1981 * don't have to worry about a readpage reading them and
1982 * overwriting a partial write. However if we have encountered
1983 * a short write and only partially written into a buffer, it
1984 * will not be marked uptodate, so a readpage might come in and
1985 * destroy our partial write.
1987 * Do the simplest thing, and just treat any short write to a
1988 * non uptodate page as a zero-length write, and force the
1989 * caller to redo the whole thing.
1991 if (!PageUptodate(page))
1992 copied = 0;
1994 page_zero_new_buffers(page, start+copied, start+len);
1996 flush_dcache_page(page);
1998 /* This could be a short (even 0-length) commit */
1999 __block_commit_write(inode, page, start, start+copied);
2001 return copied;
2003 EXPORT_SYMBOL(block_write_end);
2005 int generic_write_end(struct file *file, struct address_space *mapping,
2006 loff_t pos, unsigned len, unsigned copied,
2007 struct page *page, void *fsdata)
2009 struct inode *inode = mapping->host;
2010 int i_size_changed = 0;
2012 copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
2015 * No need to use i_size_read() here, the i_size
2016 * cannot change under us because we hold i_mutex.
2018 * But it's important to update i_size while still holding page lock:
2019 * page writeout could otherwise come in and zero beyond i_size.
2021 if (pos+copied > inode->i_size) {
2022 i_size_write(inode, pos+copied);
2023 i_size_changed = 1;
2026 unlock_page(page);
2027 page_cache_release(page);
2030 * Don't mark the inode dirty under page lock. First, it unnecessarily
2031 * makes the holding time of page lock longer. Second, it forces lock
2032 * ordering of page lock and transaction start for journaling
2033 * filesystems.
2035 if (i_size_changed)
2036 mark_inode_dirty(inode);
2038 return copied;
2040 EXPORT_SYMBOL(generic_write_end);
2043 * block_is_partially_uptodate checks whether buffers within a page are
2044 * uptodate or not.
2046 * Returns true if all buffers which correspond to a file portion
2047 * we want to read are uptodate.
2049 int block_is_partially_uptodate(struct page *page, read_descriptor_t *desc,
2050 unsigned long from)
2052 struct inode *inode = page->mapping->host;
2053 unsigned block_start, block_end, blocksize;
2054 unsigned to;
2055 struct buffer_head *bh, *head;
2056 int ret = 1;
2058 if (!page_has_buffers(page))
2059 return 0;
2061 blocksize = 1 << inode->i_blkbits;
2062 to = min_t(unsigned, PAGE_CACHE_SIZE - from, desc->count);
2063 to = from + to;
2064 if (from < blocksize && to > PAGE_CACHE_SIZE - blocksize)
2065 return 0;
2067 head = page_buffers(page);
2068 bh = head;
2069 block_start = 0;
2070 do {
2071 block_end = block_start + blocksize;
2072 if (block_end > from && block_start < to) {
2073 if (!buffer_uptodate(bh)) {
2074 ret = 0;
2075 break;
2077 if (block_end >= to)
2078 break;
2080 block_start = block_end;
2081 bh = bh->b_this_page;
2082 } while (bh != head);
2084 return ret;
2086 EXPORT_SYMBOL(block_is_partially_uptodate);
2089 * Generic "read page" function for block devices that have the normal
2090 * get_block functionality. This is most of the block device filesystems.
2091 * Reads the page asynchronously --- the unlock_buffer() and
2092 * set/clear_buffer_uptodate() functions propagate buffer state into the
2093 * page struct once IO has completed.
2095 int block_read_full_page(struct page *page, get_block_t *get_block)
2097 struct inode *inode = page->mapping->host;
2098 sector_t iblock, lblock;
2099 struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
2100 unsigned int blocksize;
2101 int nr, i;
2102 int fully_mapped = 1;
2104 BUG_ON(!PageLocked(page));
2105 blocksize = 1 << inode->i_blkbits;
2106 if (!page_has_buffers(page))
2107 create_empty_buffers(page, blocksize, 0);
2108 head = page_buffers(page);
2110 iblock = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
2111 lblock = (i_size_read(inode)+blocksize-1) >> inode->i_blkbits;
2112 bh = head;
2113 nr = 0;
2114 i = 0;
2116 do {
2117 if (buffer_uptodate(bh))
2118 continue;
2120 if (!buffer_mapped(bh)) {
2121 int err = 0;
2123 fully_mapped = 0;
2124 if (iblock < lblock) {
2125 WARN_ON(bh->b_size != blocksize);
2126 err = get_block(inode, iblock, bh, 0);
2127 if (err)
2128 SetPageError(page);
2130 if (!buffer_mapped(bh)) {
2131 zero_user(page, i * blocksize, blocksize);
2132 if (!err)
2133 set_buffer_uptodate(bh);
2134 continue;
2137 * get_block() might have updated the buffer
2138 * synchronously
2140 if (buffer_uptodate(bh))
2141 continue;
2143 arr[nr++] = bh;
2144 } while (i++, iblock++, (bh = bh->b_this_page) != head);
2146 if (fully_mapped)
2147 SetPageMappedToDisk(page);
2149 if (!nr) {
2151 * All buffers are uptodate - we can set the page uptodate
2152 * as well. But not if get_block() returned an error.
2154 if (!PageError(page))
2155 SetPageUptodate(page);
2156 unlock_page(page);
2157 return 0;
2160 /* Stage two: lock the buffers */
2161 for (i = 0; i < nr; i++) {
2162 bh = arr[i];
2163 lock_buffer(bh);
2164 mark_buffer_async_read(bh);
2168 * Stage 3: start the IO. Check for uptodateness
2169 * inside the buffer lock in case another process reading
2170 * the underlying blockdev brought it uptodate (the sct fix).
2172 for (i = 0; i < nr; i++) {
2173 bh = arr[i];
2174 if (buffer_uptodate(bh))
2175 end_buffer_async_read(bh, 1);
2176 else
2177 submit_bh(READ, bh);
2179 return 0;
2182 /* utility function for filesystems that need to do work on expanding
2183 * truncates. Uses filesystem pagecache writes to allow the filesystem to
2184 * deal with the hole.
2186 int generic_cont_expand_simple(struct inode *inode, loff_t size)
2188 struct address_space *mapping = inode->i_mapping;
2189 struct page *page;
2190 void *fsdata;
2191 unsigned long limit;
2192 int err;
2194 err = -EFBIG;
2195 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
2196 if (limit != RLIM_INFINITY && size > (loff_t)limit) {
2197 send_sig(SIGXFSZ, current, 0);
2198 goto out;
2200 if (size > inode->i_sb->s_maxbytes)
2201 goto out;
2203 err = pagecache_write_begin(NULL, mapping, size, 0,
2204 AOP_FLAG_UNINTERRUPTIBLE|AOP_FLAG_CONT_EXPAND,
2205 &page, &fsdata);
2206 if (err)
2207 goto out;
2209 err = pagecache_write_end(NULL, mapping, size, 0, 0, page, fsdata);
2210 BUG_ON(err > 0);
2212 out:
2213 return err;
2216 static int cont_expand_zero(struct file *file, struct address_space *mapping,
2217 loff_t pos, loff_t *bytes)
2219 struct inode *inode = mapping->host;
2220 unsigned blocksize = 1 << inode->i_blkbits;
2221 struct page *page;
2222 void *fsdata;
2223 pgoff_t index, curidx;
2224 loff_t curpos;
2225 unsigned zerofrom, offset, len;
2226 int err = 0;
2228 index = pos >> PAGE_CACHE_SHIFT;
2229 offset = pos & ~PAGE_CACHE_MASK;
2231 while (index > (curidx = (curpos = *bytes)>>PAGE_CACHE_SHIFT)) {
2232 zerofrom = curpos & ~PAGE_CACHE_MASK;
2233 if (zerofrom & (blocksize-1)) {
2234 *bytes |= (blocksize-1);
2235 (*bytes)++;
2237 len = PAGE_CACHE_SIZE - zerofrom;
2239 err = pagecache_write_begin(file, mapping, curpos, len,
2240 AOP_FLAG_UNINTERRUPTIBLE,
2241 &page, &fsdata);
2242 if (err)
2243 goto out;
2244 zero_user(page, zerofrom, len);
2245 err = pagecache_write_end(file, mapping, curpos, len, len,
2246 page, fsdata);
2247 if (err < 0)
2248 goto out;
2249 BUG_ON(err != len);
2250 err = 0;
2252 balance_dirty_pages_ratelimited(mapping);
2255 /* page covers the boundary, find the boundary offset */
2256 if (index == curidx) {
2257 zerofrom = curpos & ~PAGE_CACHE_MASK;
2258 /* if we will expand the thing last block will be filled */
2259 if (offset <= zerofrom) {
2260 goto out;
2262 if (zerofrom & (blocksize-1)) {
2263 *bytes |= (blocksize-1);
2264 (*bytes)++;
2266 len = offset - zerofrom;
2268 err = pagecache_write_begin(file, mapping, curpos, len,
2269 AOP_FLAG_UNINTERRUPTIBLE,
2270 &page, &fsdata);
2271 if (err)
2272 goto out;
2273 zero_user(page, zerofrom, len);
2274 err = pagecache_write_end(file, mapping, curpos, len, len,
2275 page, fsdata);
2276 if (err < 0)
2277 goto out;
2278 BUG_ON(err != len);
2279 err = 0;
2281 out:
2282 return err;
2286 * For moronic filesystems that do not allow holes in file.
2287 * We may have to extend the file.
2289 int cont_write_begin(struct file *file, struct address_space *mapping,
2290 loff_t pos, unsigned len, unsigned flags,
2291 struct page **pagep, void **fsdata,
2292 get_block_t *get_block, loff_t *bytes)
2294 struct inode *inode = mapping->host;
2295 unsigned blocksize = 1 << inode->i_blkbits;
2296 unsigned zerofrom;
2297 int err;
2299 err = cont_expand_zero(file, mapping, pos, bytes);
2300 if (err)
2301 goto out;
2303 zerofrom = *bytes & ~PAGE_CACHE_MASK;
2304 if (pos+len > *bytes && zerofrom & (blocksize-1)) {
2305 *bytes |= (blocksize-1);
2306 (*bytes)++;
2309 *pagep = NULL;
2310 err = block_write_begin(file, mapping, pos, len,
2311 flags, pagep, fsdata, get_block);
2312 out:
2313 return err;
2316 int block_prepare_write(struct page *page, unsigned from, unsigned to,
2317 get_block_t *get_block)
2319 struct inode *inode = page->mapping->host;
2320 int err = __block_prepare_write(inode, page, from, to, get_block);
2321 if (err)
2322 ClearPageUptodate(page);
2323 return err;
2326 int block_commit_write(struct page *page, unsigned from, unsigned to)
2328 struct inode *inode = page->mapping->host;
2329 __block_commit_write(inode,page,from,to);
2330 return 0;
2334 * block_page_mkwrite() is not allowed to change the file size as it gets
2335 * called from a page fault handler when a page is first dirtied. Hence we must
2336 * be careful to check for EOF conditions here. We set the page up correctly
2337 * for a written page which means we get ENOSPC checking when writing into
2338 * holes and correct delalloc and unwritten extent mapping on filesystems that
2339 * support these features.
2341 * We are not allowed to take the i_mutex here so we have to play games to
2342 * protect against truncate races as the page could now be beyond EOF. Because
2343 * vmtruncate() writes the inode size before removing pages, once we have the
2344 * page lock we can determine safely if the page is beyond EOF. If it is not
2345 * beyond EOF, then the page is guaranteed safe against truncation until we
2346 * unlock the page.
2349 block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
2350 get_block_t get_block)
2352 struct page *page = vmf->page;
2353 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
2354 unsigned long end;
2355 loff_t size;
2356 int ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
2358 lock_page(page);
2359 size = i_size_read(inode);
2360 if ((page->mapping != inode->i_mapping) ||
2361 (page_offset(page) > size)) {
2362 /* page got truncated out from underneath us */
2363 goto out_unlock;
2366 /* page is wholly or partially inside EOF */
2367 if (((page->index + 1) << PAGE_CACHE_SHIFT) > size)
2368 end = size & ~PAGE_CACHE_MASK;
2369 else
2370 end = PAGE_CACHE_SIZE;
2372 ret = block_prepare_write(page, 0, end, get_block);
2373 if (!ret)
2374 ret = block_commit_write(page, 0, end);
2376 if (unlikely(ret)) {
2377 if (ret == -ENOMEM)
2378 ret = VM_FAULT_OOM;
2379 else /* -ENOSPC, -EIO, etc */
2380 ret = VM_FAULT_SIGBUS;
2383 out_unlock:
2384 unlock_page(page);
2385 return ret;
2389 * nobh_write_begin()'s prereads are special: the buffer_heads are freed
2390 * immediately, while under the page lock. So it needs a special end_io
2391 * handler which does not touch the bh after unlocking it.
2393 static void end_buffer_read_nobh(struct buffer_head *bh, int uptodate)
2395 __end_buffer_read_notouch(bh, uptodate);
2399 * Attach the singly-linked list of buffers created by nobh_write_begin, to
2400 * the page (converting it to circular linked list and taking care of page
2401 * dirty races).
2403 static void attach_nobh_buffers(struct page *page, struct buffer_head *head)
2405 struct buffer_head *bh;
2407 BUG_ON(!PageLocked(page));
2409 spin_lock(&page->mapping->private_lock);
2410 bh = head;
2411 do {
2412 if (PageDirty(page))
2413 set_buffer_dirty(bh);
2414 if (!bh->b_this_page)
2415 bh->b_this_page = head;
2416 bh = bh->b_this_page;
2417 } while (bh != head);
2418 attach_page_buffers(page, head);
2419 spin_unlock(&page->mapping->private_lock);
2423 * On entry, the page is fully not uptodate.
2424 * On exit the page is fully uptodate in the areas outside (from,to)
2426 int nobh_write_begin(struct file *file, struct address_space *mapping,
2427 loff_t pos, unsigned len, unsigned flags,
2428 struct page **pagep, void **fsdata,
2429 get_block_t *get_block)
2431 struct inode *inode = mapping->host;
2432 const unsigned blkbits = inode->i_blkbits;
2433 const unsigned blocksize = 1 << blkbits;
2434 struct buffer_head *head, *bh;
2435 struct page *page;
2436 pgoff_t index;
2437 unsigned from, to;
2438 unsigned block_in_page;
2439 unsigned block_start, block_end;
2440 sector_t block_in_file;
2441 int nr_reads = 0;
2442 int ret = 0;
2443 int is_mapped_to_disk = 1;
2445 index = pos >> PAGE_CACHE_SHIFT;
2446 from = pos & (PAGE_CACHE_SIZE - 1);
2447 to = from + len;
2449 page = grab_cache_page_write_begin(mapping, index, flags);
2450 if (!page)
2451 return -ENOMEM;
2452 *pagep = page;
2453 *fsdata = NULL;
2455 if (page_has_buffers(page)) {
2456 unlock_page(page);
2457 page_cache_release(page);
2458 *pagep = NULL;
2459 return block_write_begin(file, mapping, pos, len, flags, pagep,
2460 fsdata, get_block);
2463 if (PageMappedToDisk(page))
2464 return 0;
2467 * Allocate buffers so that we can keep track of state, and potentially
2468 * attach them to the page if an error occurs. In the common case of
2469 * no error, they will just be freed again without ever being attached
2470 * to the page (which is all OK, because we're under the page lock).
2472 * Be careful: the buffer linked list is a NULL terminated one, rather
2473 * than the circular one we're used to.
2475 head = alloc_page_buffers(page, blocksize, 0);
2476 if (!head) {
2477 ret = -ENOMEM;
2478 goto out_release;
2481 block_in_file = (sector_t)page->index << (PAGE_CACHE_SHIFT - blkbits);
2484 * We loop across all blocks in the page, whether or not they are
2485 * part of the affected region. This is so we can discover if the
2486 * page is fully mapped-to-disk.
2488 for (block_start = 0, block_in_page = 0, bh = head;
2489 block_start < PAGE_CACHE_SIZE;
2490 block_in_page++, block_start += blocksize, bh = bh->b_this_page) {
2491 int create;
2493 block_end = block_start + blocksize;
2494 bh->b_state = 0;
2495 create = 1;
2496 if (block_start >= to)
2497 create = 0;
2498 ret = get_block(inode, block_in_file + block_in_page,
2499 bh, create);
2500 if (ret)
2501 goto failed;
2502 if (!buffer_mapped(bh))
2503 is_mapped_to_disk = 0;
2504 if (buffer_new(bh))
2505 unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
2506 if (PageUptodate(page)) {
2507 set_buffer_uptodate(bh);
2508 continue;
2510 if (buffer_new(bh) || !buffer_mapped(bh)) {
2511 zero_user_segments(page, block_start, from,
2512 to, block_end);
2513 continue;
2515 if (buffer_uptodate(bh))
2516 continue; /* reiserfs does this */
2517 if (block_start < from || block_end > to) {
2518 lock_buffer(bh);
2519 bh->b_end_io = end_buffer_read_nobh;
2520 submit_bh(READ, bh);
2521 nr_reads++;
2525 if (nr_reads) {
2527 * The page is locked, so these buffers are protected from
2528 * any VM or truncate activity. Hence we don't need to care
2529 * for the buffer_head refcounts.
2531 for (bh = head; bh; bh = bh->b_this_page) {
2532 wait_on_buffer(bh);
2533 if (!buffer_uptodate(bh))
2534 ret = -EIO;
2536 if (ret)
2537 goto failed;
2540 if (is_mapped_to_disk)
2541 SetPageMappedToDisk(page);
2543 *fsdata = head; /* to be released by nobh_write_end */
2545 return 0;
2547 failed:
2548 BUG_ON(!ret);
2550 * Error recovery is a bit difficult. We need to zero out blocks that
2551 * were newly allocated, and dirty them to ensure they get written out.
2552 * Buffers need to be attached to the page at this point, otherwise
2553 * the handling of potential IO errors during writeout would be hard
2554 * (could try doing synchronous writeout, but what if that fails too?)
2556 attach_nobh_buffers(page, head);
2557 page_zero_new_buffers(page, from, to);
2559 out_release:
2560 unlock_page(page);
2561 page_cache_release(page);
2562 *pagep = NULL;
2564 if (pos + len > inode->i_size)
2565 vmtruncate(inode, inode->i_size);
2567 return ret;
2569 EXPORT_SYMBOL(nobh_write_begin);
2571 int nobh_write_end(struct file *file, struct address_space *mapping,
2572 loff_t pos, unsigned len, unsigned copied,
2573 struct page *page, void *fsdata)
2575 struct inode *inode = page->mapping->host;
2576 struct buffer_head *head = fsdata;
2577 struct buffer_head *bh;
2578 BUG_ON(fsdata != NULL && page_has_buffers(page));
2580 if (unlikely(copied < len) && head)
2581 attach_nobh_buffers(page, head);
2582 if (page_has_buffers(page))
2583 return generic_write_end(file, mapping, pos, len,
2584 copied, page, fsdata);
2586 SetPageUptodate(page);
2587 set_page_dirty(page);
2588 if (pos+copied > inode->i_size) {
2589 i_size_write(inode, pos+copied);
2590 mark_inode_dirty(inode);
2593 unlock_page(page);
2594 page_cache_release(page);
2596 while (head) {
2597 bh = head;
2598 head = head->b_this_page;
2599 free_buffer_head(bh);
2602 return copied;
2604 EXPORT_SYMBOL(nobh_write_end);
2607 * nobh_writepage() - based on block_full_write_page() except
2608 * that it tries to operate without attaching bufferheads to
2609 * the page.
2611 int nobh_writepage(struct page *page, get_block_t *get_block,
2612 struct writeback_control *wbc)
2614 struct inode * const inode = page->mapping->host;
2615 loff_t i_size = i_size_read(inode);
2616 const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
2617 unsigned offset;
2618 int ret;
2620 /* Is the page fully inside i_size? */
2621 if (page->index < end_index)
2622 goto out;
2624 /* Is the page fully outside i_size? (truncate in progress) */
2625 offset = i_size & (PAGE_CACHE_SIZE-1);
2626 if (page->index >= end_index+1 || !offset) {
2628 * The page may have dirty, unmapped buffers. For example,
2629 * they may have been added in ext3_writepage(). Make them
2630 * freeable here, so the page does not leak.
2632 #if 0
2633 /* Not really sure about this - do we need this ? */
2634 if (page->mapping->a_ops->invalidatepage)
2635 page->mapping->a_ops->invalidatepage(page, offset);
2636 #endif
2637 unlock_page(page);
2638 return 0; /* don't care */
2642 * The page straddles i_size. It must be zeroed out on each and every
2643 * writepage invocation because it may be mmapped. "A file is mapped
2644 * in multiples of the page size. For a file that is not a multiple of
2645 * the page size, the remaining memory is zeroed when mapped, and
2646 * writes to that region are not written out to the file."
2648 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
2649 out:
2650 ret = mpage_writepage(page, get_block, wbc);
2651 if (ret == -EAGAIN)
2652 ret = __block_write_full_page(inode, page, get_block, wbc);
2653 return ret;
2655 EXPORT_SYMBOL(nobh_writepage);
2657 int nobh_truncate_page(struct address_space *mapping,
2658 loff_t from, get_block_t *get_block)
2660 pgoff_t index = from >> PAGE_CACHE_SHIFT;
2661 unsigned offset = from & (PAGE_CACHE_SIZE-1);
2662 unsigned blocksize;
2663 sector_t iblock;
2664 unsigned length, pos;
2665 struct inode *inode = mapping->host;
2666 struct page *page;
2667 struct buffer_head map_bh;
2668 int err;
2670 blocksize = 1 << inode->i_blkbits;
2671 length = offset & (blocksize - 1);
2673 /* Block boundary? Nothing to do */
2674 if (!length)
2675 return 0;
2677 length = blocksize - length;
2678 iblock = (sector_t)index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
2680 page = grab_cache_page(mapping, index);
2681 err = -ENOMEM;
2682 if (!page)
2683 goto out;
2685 if (page_has_buffers(page)) {
2686 has_buffers:
2687 unlock_page(page);
2688 page_cache_release(page);
2689 return block_truncate_page(mapping, from, get_block);
2692 /* Find the buffer that contains "offset" */
2693 pos = blocksize;
2694 while (offset >= pos) {
2695 iblock++;
2696 pos += blocksize;
2699 err = get_block(inode, iblock, &map_bh, 0);
2700 if (err)
2701 goto unlock;
2702 /* unmapped? It's a hole - nothing to do */
2703 if (!buffer_mapped(&map_bh))
2704 goto unlock;
2706 /* Ok, it's mapped. Make sure it's up-to-date */
2707 if (!PageUptodate(page)) {
2708 err = mapping->a_ops->readpage(NULL, page);
2709 if (err) {
2710 page_cache_release(page);
2711 goto out;
2713 lock_page(page);
2714 if (!PageUptodate(page)) {
2715 err = -EIO;
2716 goto unlock;
2718 if (page_has_buffers(page))
2719 goto has_buffers;
2721 zero_user(page, offset, length);
2722 set_page_dirty(page);
2723 err = 0;
2725 unlock:
2726 unlock_page(page);
2727 page_cache_release(page);
2728 out:
2729 return err;
2731 EXPORT_SYMBOL(nobh_truncate_page);
2733 int block_truncate_page(struct address_space *mapping,
2734 loff_t from, get_block_t *get_block)
2736 pgoff_t index = from >> PAGE_CACHE_SHIFT;
2737 unsigned offset = from & (PAGE_CACHE_SIZE-1);
2738 unsigned blocksize;
2739 sector_t iblock;
2740 unsigned length, pos;
2741 struct inode *inode = mapping->host;
2742 struct page *page;
2743 struct buffer_head *bh;
2744 int err;
2746 blocksize = 1 << inode->i_blkbits;
2747 length = offset & (blocksize - 1);
2749 /* Block boundary? Nothing to do */
2750 if (!length)
2751 return 0;
2753 length = blocksize - length;
2754 iblock = (sector_t)index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
2756 page = grab_cache_page(mapping, index);
2757 err = -ENOMEM;
2758 if (!page)
2759 goto out;
2761 if (!page_has_buffers(page))
2762 create_empty_buffers(page, blocksize, 0);
2764 /* Find the buffer that contains "offset" */
2765 bh = page_buffers(page);
2766 pos = blocksize;
2767 while (offset >= pos) {
2768 bh = bh->b_this_page;
2769 iblock++;
2770 pos += blocksize;
2773 err = 0;
2774 if (!buffer_mapped(bh)) {
2775 WARN_ON(bh->b_size != blocksize);
2776 err = get_block(inode, iblock, bh, 0);
2777 if (err)
2778 goto unlock;
2779 /* unmapped? It's a hole - nothing to do */
2780 if (!buffer_mapped(bh))
2781 goto unlock;
2784 /* Ok, it's mapped. Make sure it's up-to-date */
2785 if (PageUptodate(page))
2786 set_buffer_uptodate(bh);
2788 if (!buffer_uptodate(bh) && !buffer_delay(bh) && !buffer_unwritten(bh)) {
2789 err = -EIO;
2790 ll_rw_block(READ, 1, &bh);
2791 wait_on_buffer(bh);
2792 /* Uhhuh. Read error. Complain and punt. */
2793 if (!buffer_uptodate(bh))
2794 goto unlock;
2797 zero_user(page, offset, length);
2798 mark_buffer_dirty(bh);
2799 err = 0;
2801 unlock:
2802 unlock_page(page);
2803 page_cache_release(page);
2804 out:
2805 return err;
2809 * The generic ->writepage function for buffer-backed address_spaces
2811 int block_write_full_page(struct page *page, get_block_t *get_block,
2812 struct writeback_control *wbc)
2814 struct inode * const inode = page->mapping->host;
2815 loff_t i_size = i_size_read(inode);
2816 const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
2817 unsigned offset;
2819 /* Is the page fully inside i_size? */
2820 if (page->index < end_index)
2821 return __block_write_full_page(inode, page, get_block, wbc);
2823 /* Is the page fully outside i_size? (truncate in progress) */
2824 offset = i_size & (PAGE_CACHE_SIZE-1);
2825 if (page->index >= end_index+1 || !offset) {
2827 * The page may have dirty, unmapped buffers. For example,
2828 * they may have been added in ext3_writepage(). Make them
2829 * freeable here, so the page does not leak.
2831 do_invalidatepage(page, 0);
2832 unlock_page(page);
2833 return 0; /* don't care */
2837 * The page straddles i_size. It must be zeroed out on each and every
2838 * writepage invokation because it may be mmapped. "A file is mapped
2839 * in multiples of the page size. For a file that is not a multiple of
2840 * the page size, the remaining memory is zeroed when mapped, and
2841 * writes to that region are not written out to the file."
2843 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
2844 return __block_write_full_page(inode, page, get_block, wbc);
2847 sector_t generic_block_bmap(struct address_space *mapping, sector_t block,
2848 get_block_t *get_block)
2850 struct buffer_head tmp;
2851 struct inode *inode = mapping->host;
2852 tmp.b_state = 0;
2853 tmp.b_blocknr = 0;
2854 tmp.b_size = 1 << inode->i_blkbits;
2855 get_block(inode, block, &tmp, 0);
2856 return tmp.b_blocknr;
2859 static void end_bio_bh_io_sync(struct bio *bio, int err)
2861 struct buffer_head *bh = bio->bi_private;
2863 if (err == -EOPNOTSUPP) {
2864 set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
2865 set_bit(BH_Eopnotsupp, &bh->b_state);
2868 if (unlikely (test_bit(BIO_QUIET,&bio->bi_flags)))
2869 set_bit(BH_Quiet, &bh->b_state);
2871 bh->b_end_io(bh, test_bit(BIO_UPTODATE, &bio->bi_flags));
2872 bio_put(bio);
2875 int submit_bh(int rw, struct buffer_head * bh)
2877 struct bio *bio;
2878 int ret = 0;
2880 BUG_ON(!buffer_locked(bh));
2881 BUG_ON(!buffer_mapped(bh));
2882 BUG_ON(!bh->b_end_io);
2885 * Mask in barrier bit for a write (could be either a WRITE or a
2886 * WRITE_SYNC
2888 if (buffer_ordered(bh) && (rw & WRITE))
2889 rw |= WRITE_BARRIER;
2892 * Only clear out a write error when rewriting
2894 if (test_set_buffer_req(bh) && (rw & WRITE))
2895 clear_buffer_write_io_error(bh);
2898 * from here on down, it's all bio -- do the initial mapping,
2899 * submit_bio -> generic_make_request may further map this bio around
2901 bio = bio_alloc(GFP_NOIO, 1);
2903 bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
2904 bio->bi_bdev = bh->b_bdev;
2905 bio->bi_io_vec[0].bv_page = bh->b_page;
2906 bio->bi_io_vec[0].bv_len = bh->b_size;
2907 bio->bi_io_vec[0].bv_offset = bh_offset(bh);
2909 bio->bi_vcnt = 1;
2910 bio->bi_idx = 0;
2911 bio->bi_size = bh->b_size;
2913 bio->bi_end_io = end_bio_bh_io_sync;
2914 bio->bi_private = bh;
2916 bio_get(bio);
2917 submit_bio(rw, bio);
2919 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2920 ret = -EOPNOTSUPP;
2922 bio_put(bio);
2923 return ret;
2927 * ll_rw_block: low-level access to block devices (DEPRECATED)
2928 * @rw: whether to %READ or %WRITE or %SWRITE or maybe %READA (readahead)
2929 * @nr: number of &struct buffer_heads in the array
2930 * @bhs: array of pointers to &struct buffer_head
2932 * ll_rw_block() takes an array of pointers to &struct buffer_heads, and
2933 * requests an I/O operation on them, either a %READ or a %WRITE. The third
2934 * %SWRITE is like %WRITE only we make sure that the *current* data in buffers
2935 * are sent to disk. The fourth %READA option is described in the documentation
2936 * for generic_make_request() which ll_rw_block() calls.
2938 * This function drops any buffer that it cannot get a lock on (with the
2939 * BH_Lock state bit) unless SWRITE is required, any buffer that appears to be
2940 * clean when doing a write request, and any buffer that appears to be
2941 * up-to-date when doing read request. Further it marks as clean buffers that
2942 * are processed for writing (the buffer cache won't assume that they are
2943 * actually clean until the buffer gets unlocked).
2945 * ll_rw_block sets b_end_io to simple completion handler that marks
2946 * the buffer up-to-date (if approriate), unlocks the buffer and wakes
2947 * any waiters.
2949 * All of the buffers must be for the same device, and must also be a
2950 * multiple of the current approved size for the device.
2952 void ll_rw_block(int rw, int nr, struct buffer_head *bhs[])
2954 int i;
2956 for (i = 0; i < nr; i++) {
2957 struct buffer_head *bh = bhs[i];
2959 if (rw == SWRITE || rw == SWRITE_SYNC)
2960 lock_buffer(bh);
2961 else if (!trylock_buffer(bh))
2962 continue;
2964 if (rw == WRITE || rw == SWRITE || rw == SWRITE_SYNC) {
2965 if (test_clear_buffer_dirty(bh)) {
2966 bh->b_end_io = end_buffer_write_sync;
2967 get_bh(bh);
2968 if (rw == SWRITE_SYNC)
2969 submit_bh(WRITE_SYNC, bh);
2970 else
2971 submit_bh(WRITE, bh);
2972 continue;
2974 } else {
2975 if (!buffer_uptodate(bh)) {
2976 bh->b_end_io = end_buffer_read_sync;
2977 get_bh(bh);
2978 submit_bh(rw, bh);
2979 continue;
2982 unlock_buffer(bh);
2987 * For a data-integrity writeout, we need to wait upon any in-progress I/O
2988 * and then start new I/O and then wait upon it. The caller must have a ref on
2989 * the buffer_head.
2991 int sync_dirty_buffer(struct buffer_head *bh)
2993 int ret = 0;
2995 WARN_ON(atomic_read(&bh->b_count) < 1);
2996 lock_buffer(bh);
2997 if (test_clear_buffer_dirty(bh)) {
2998 get_bh(bh);
2999 bh->b_end_io = end_buffer_write_sync;
3000 ret = submit_bh(WRITE, bh);
3001 wait_on_buffer(bh);
3002 if (buffer_eopnotsupp(bh)) {
3003 clear_buffer_eopnotsupp(bh);
3004 ret = -EOPNOTSUPP;
3006 if (!ret && !buffer_uptodate(bh))
3007 ret = -EIO;
3008 } else {
3009 unlock_buffer(bh);
3011 return ret;
3015 * try_to_free_buffers() checks if all the buffers on this particular page
3016 * are unused, and releases them if so.
3018 * Exclusion against try_to_free_buffers may be obtained by either
3019 * locking the page or by holding its mapping's private_lock.
3021 * If the page is dirty but all the buffers are clean then we need to
3022 * be sure to mark the page clean as well. This is because the page
3023 * may be against a block device, and a later reattachment of buffers
3024 * to a dirty page will set *all* buffers dirty. Which would corrupt
3025 * filesystem data on the same device.
3027 * The same applies to regular filesystem pages: if all the buffers are
3028 * clean then we set the page clean and proceed. To do that, we require
3029 * total exclusion from __set_page_dirty_buffers(). That is obtained with
3030 * private_lock.
3032 * try_to_free_buffers() is non-blocking.
3034 static inline int buffer_busy(struct buffer_head *bh)
3036 return atomic_read(&bh->b_count) |
3037 (bh->b_state & ((1 << BH_Dirty) | (1 << BH_Lock)));
3040 static int
3041 drop_buffers(struct page *page, struct buffer_head **buffers_to_free)
3043 struct buffer_head *head = page_buffers(page);
3044 struct buffer_head *bh;
3046 bh = head;
3047 do {
3048 if (buffer_write_io_error(bh) && page->mapping)
3049 set_bit(AS_EIO, &page->mapping->flags);
3050 if (buffer_busy(bh))
3051 goto failed;
3052 bh = bh->b_this_page;
3053 } while (bh != head);
3055 do {
3056 struct buffer_head *next = bh->b_this_page;
3058 if (bh->b_assoc_map)
3059 __remove_assoc_queue(bh);
3060 bh = next;
3061 } while (bh != head);
3062 *buffers_to_free = head;
3063 __clear_page_buffers(page);
3064 return 1;
3065 failed:
3066 return 0;
3069 int try_to_free_buffers(struct page *page)
3071 struct address_space * const mapping = page->mapping;
3072 struct buffer_head *buffers_to_free = NULL;
3073 int ret = 0;
3075 BUG_ON(!PageLocked(page));
3076 if (PageWriteback(page))
3077 return 0;
3079 if (mapping == NULL) { /* can this still happen? */
3080 ret = drop_buffers(page, &buffers_to_free);
3081 goto out;
3084 spin_lock(&mapping->private_lock);
3085 ret = drop_buffers(page, &buffers_to_free);
3088 * If the filesystem writes its buffers by hand (eg ext3)
3089 * then we can have clean buffers against a dirty page. We
3090 * clean the page here; otherwise the VM will never notice
3091 * that the filesystem did any IO at all.
3093 * Also, during truncate, discard_buffer will have marked all
3094 * the page's buffers clean. We discover that here and clean
3095 * the page also.
3097 * private_lock must be held over this entire operation in order
3098 * to synchronise against __set_page_dirty_buffers and prevent the
3099 * dirty bit from being lost.
3101 if (ret)
3102 cancel_dirty_page(page, PAGE_CACHE_SIZE);
3103 spin_unlock(&mapping->private_lock);
3104 out:
3105 if (buffers_to_free) {
3106 struct buffer_head *bh = buffers_to_free;
3108 do {
3109 struct buffer_head *next = bh->b_this_page;
3110 free_buffer_head(bh);
3111 bh = next;
3112 } while (bh != buffers_to_free);
3114 return ret;
3116 EXPORT_SYMBOL(try_to_free_buffers);
3118 void block_sync_page(struct page *page)
3120 struct address_space *mapping;
3122 smp_mb();
3123 mapping = page_mapping(page);
3124 if (mapping)
3125 blk_run_backing_dev(mapping->backing_dev_info, page);
3129 * There are no bdflush tunables left. But distributions are
3130 * still running obsolete flush daemons, so we terminate them here.
3132 * Use of bdflush() is deprecated and will be removed in a future kernel.
3133 * The `pdflush' kernel threads fully replace bdflush daemons and this call.
3135 SYSCALL_DEFINE2(bdflush, int, func, long, data)
3137 static int msg_count;
3139 if (!capable(CAP_SYS_ADMIN))
3140 return -EPERM;
3142 if (msg_count < 5) {
3143 msg_count++;
3144 printk(KERN_INFO
3145 "warning: process `%s' used the obsolete bdflush"
3146 " system call\n", current->comm);
3147 printk(KERN_INFO "Fix your initscripts?\n");
3150 if (func == 1)
3151 do_exit(0);
3152 return 0;
3156 * Buffer-head allocation
3158 static struct kmem_cache *bh_cachep;
3161 * Once the number of bh's in the machine exceeds this level, we start
3162 * stripping them in writeback.
3164 static int max_buffer_heads;
3166 int buffer_heads_over_limit;
3168 struct bh_accounting {
3169 int nr; /* Number of live bh's */
3170 int ratelimit; /* Limit cacheline bouncing */
3173 static DEFINE_PER_CPU(struct bh_accounting, bh_accounting) = {0, 0};
3175 static void recalc_bh_state(void)
3177 int i;
3178 int tot = 0;
3180 if (__get_cpu_var(bh_accounting).ratelimit++ < 4096)
3181 return;
3182 __get_cpu_var(bh_accounting).ratelimit = 0;
3183 for_each_online_cpu(i)
3184 tot += per_cpu(bh_accounting, i).nr;
3185 buffer_heads_over_limit = (tot > max_buffer_heads);
3188 struct buffer_head *alloc_buffer_head(gfp_t gfp_flags)
3190 struct buffer_head *ret = kmem_cache_alloc(bh_cachep, gfp_flags);
3191 if (ret) {
3192 INIT_LIST_HEAD(&ret->b_assoc_buffers);
3193 get_cpu_var(bh_accounting).nr++;
3194 recalc_bh_state();
3195 put_cpu_var(bh_accounting);
3197 return ret;
3199 EXPORT_SYMBOL(alloc_buffer_head);
3201 void free_buffer_head(struct buffer_head *bh)
3203 BUG_ON(!list_empty(&bh->b_assoc_buffers));
3204 kmem_cache_free(bh_cachep, bh);
3205 get_cpu_var(bh_accounting).nr--;
3206 recalc_bh_state();
3207 put_cpu_var(bh_accounting);
3209 EXPORT_SYMBOL(free_buffer_head);
3211 static void buffer_exit_cpu(int cpu)
3213 int i;
3214 struct bh_lru *b = &per_cpu(bh_lrus, cpu);
3216 for (i = 0; i < BH_LRU_SIZE; i++) {
3217 brelse(b->bhs[i]);
3218 b->bhs[i] = NULL;
3220 get_cpu_var(bh_accounting).nr += per_cpu(bh_accounting, cpu).nr;
3221 per_cpu(bh_accounting, cpu).nr = 0;
3222 put_cpu_var(bh_accounting);
3225 static int buffer_cpu_notify(struct notifier_block *self,
3226 unsigned long action, void *hcpu)
3228 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
3229 buffer_exit_cpu((unsigned long)hcpu);
3230 return NOTIFY_OK;
3234 * bh_uptodate_or_lock - Test whether the buffer is uptodate
3235 * @bh: struct buffer_head
3237 * Return true if the buffer is up-to-date and false,
3238 * with the buffer locked, if not.
3240 int bh_uptodate_or_lock(struct buffer_head *bh)
3242 if (!buffer_uptodate(bh)) {
3243 lock_buffer(bh);
3244 if (!buffer_uptodate(bh))
3245 return 0;
3246 unlock_buffer(bh);
3248 return 1;
3250 EXPORT_SYMBOL(bh_uptodate_or_lock);
3253 * bh_submit_read - Submit a locked buffer for reading
3254 * @bh: struct buffer_head
3256 * Returns zero on success and -EIO on error.
3258 int bh_submit_read(struct buffer_head *bh)
3260 BUG_ON(!buffer_locked(bh));
3262 if (buffer_uptodate(bh)) {
3263 unlock_buffer(bh);
3264 return 0;
3267 get_bh(bh);
3268 bh->b_end_io = end_buffer_read_sync;
3269 submit_bh(READ, bh);
3270 wait_on_buffer(bh);
3271 if (buffer_uptodate(bh))
3272 return 0;
3273 return -EIO;
3275 EXPORT_SYMBOL(bh_submit_read);
3277 static void
3278 init_buffer_head(void *data)
3280 struct buffer_head *bh = data;
3282 memset(bh, 0, sizeof(*bh));
3283 INIT_LIST_HEAD(&bh->b_assoc_buffers);
3286 void __init buffer_init(void)
3288 int nrpages;
3290 bh_cachep = kmem_cache_create("buffer_head",
3291 sizeof(struct buffer_head), 0,
3292 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
3293 SLAB_MEM_SPREAD),
3294 init_buffer_head);
3297 * Limit the bh occupancy to 10% of ZONE_NORMAL
3299 nrpages = (nr_free_buffer_pages() * 10) / 100;
3300 max_buffer_heads = nrpages * (PAGE_SIZE / sizeof(struct buffer_head));
3301 hotcpu_notifier(buffer_cpu_notify, 0);
3304 EXPORT_SYMBOL(__bforget);
3305 EXPORT_SYMBOL(__brelse);
3306 EXPORT_SYMBOL(__wait_on_buffer);
3307 EXPORT_SYMBOL(block_commit_write);
3308 EXPORT_SYMBOL(block_prepare_write);
3309 EXPORT_SYMBOL(block_page_mkwrite);
3310 EXPORT_SYMBOL(block_read_full_page);
3311 EXPORT_SYMBOL(block_sync_page);
3312 EXPORT_SYMBOL(block_truncate_page);
3313 EXPORT_SYMBOL(block_write_full_page);
3314 EXPORT_SYMBOL(cont_write_begin);
3315 EXPORT_SYMBOL(end_buffer_read_sync);
3316 EXPORT_SYMBOL(end_buffer_write_sync);
3317 EXPORT_SYMBOL(file_fsync);
3318 EXPORT_SYMBOL(generic_block_bmap);
3319 EXPORT_SYMBOL(generic_cont_expand_simple);
3320 EXPORT_SYMBOL(init_buffer);
3321 EXPORT_SYMBOL(invalidate_bdev);
3322 EXPORT_SYMBOL(ll_rw_block);
3323 EXPORT_SYMBOL(mark_buffer_dirty);
3324 EXPORT_SYMBOL(submit_bh);
3325 EXPORT_SYMBOL(sync_dirty_buffer);
3326 EXPORT_SYMBOL(unlock_buffer);