3295 lines · 85.5 KB · C
RawDownload
/* *  linux/fs/buffer.c * *  Copyright (C) 1991, 1992, 2002  Linus Torvalds */ /* * Start bdflush() with kernel_thread not syscall - Paul Gortmaker, 12/95 * * Removed a lot of unnecessary code and simplified things now that * the buffer cache isn't our primary cache - Andrew Tridgell 12/96 * * Speed up hash, lru, and free list operations.  Use gfp() for allocating * hash table, use SLAB cache for buffer heads. SMP threading.  -DaveM * * Added 32k buffer block sizes - these are required older ARM systems. - RMK * * async buffer flushing, 1999 Andrea Arcangeli <andrea@suse.de> */ #include <linux/kernel.h>#include <linux/syscalls.h>#include <linux/fs.h>#include <linux/mm.h>#include <linux/percpu.h>#include <linux/slab.h>#include <linux/capability.h>#include <linux/blkdev.h>#include <linux/file.h>#include <linux/quotaops.h>#include <linux/highmem.h>#include <linux/export.h>#include <linux/writeback.h>#include <linux/hash.h>#include <linux/suspend.h>#include <linux/buffer_head.h>#include <linux/task_io_accounting_ops.h>#include <linux/bio.h>#include <linux/notifier.h>#include <linux/cpu.h>#include <linux/bitops.h>#include <linux/mpage.h>#include <linux/bit_spinlock.h> static int fsync_buffers_list(spinlock_t *lock, struct list_head *list); #define BH_ENTRY(list) list_entry((list), struct buffer_head, b_assoc_buffers) inline voidinit_buffer(struct buffer_head *bh, bh_end_io_t *handler, void *private){	bh->b_end_io = handler;	bh->b_private = private;}EXPORT_SYMBOL(init_buffer); static int sleep_on_buffer(void *word){	io_schedule();	return 0;} void __lock_buffer(struct buffer_head *bh){	wait_on_bit_lock(&bh->b_state, BH_Lock, sleep_on_buffer,							TASK_UNINTERRUPTIBLE);}EXPORT_SYMBOL(__lock_buffer); void unlock_buffer(struct buffer_head *bh){	clear_bit_unlock(BH_Lock, &bh->b_state);	smp_mb__after_clear_bit();	wake_up_bit(&bh->b_state, BH_Lock);}EXPORT_SYMBOL(unlock_buffer); /* * Block until a buffer comes unlocked.  This doesn't stop it * from becoming locked again - you have to lock it yourself * if you want to preserve its state. */void __wait_on_buffer(struct buffer_head * bh){	wait_on_bit(&bh->b_state, BH_Lock, sleep_on_buffer, TASK_UNINTERRUPTIBLE);}EXPORT_SYMBOL(__wait_on_buffer); static void__clear_page_buffers(struct page *page){	ClearPagePrivate(page);	set_page_private(page, 0);	page_cache_release(page);}  static int quiet_error(struct buffer_head *bh){	if (!test_bit(BH_Quiet, &bh->b_state) && printk_ratelimit())		return 0;	return 1;}  static void buffer_io_error(struct buffer_head *bh){	char b[BDEVNAME_SIZE];	printk(KERN_ERR "Buffer I/O error on device %s, logical block %Lu\n",			bdevname(bh->b_bdev, b),			(unsigned long long)bh->b_blocknr);} /* * End-of-IO handler helper function which does not touch the bh after * unlocking it. * Note: unlock_buffer() sort-of does touch the bh after unlocking it, but * a race there is benign: unlock_buffer() only use the bh's address for * hashing after unlocking the buffer, so it doesn't actually touch the bh * itself. */static void __end_buffer_read_notouch(struct buffer_head *bh, int uptodate){	if (uptodate) {		set_buffer_uptodate(bh);	} else {		/* This happens, due to failed READA attempts. */		clear_buffer_uptodate(bh);	}	unlock_buffer(bh);} /* * Default synchronous end-of-IO handler..  Just mark it up-to-date and * unlock the buffer. This is what ll_rw_block uses too. */void end_buffer_read_sync(struct buffer_head *bh, int uptodate){	__end_buffer_read_notouch(bh, uptodate);	put_bh(bh);}EXPORT_SYMBOL(end_buffer_read_sync); void end_buffer_write_sync(struct buffer_head *bh, int uptodate){	char b[BDEVNAME_SIZE]; 	if (uptodate) {		set_buffer_uptodate(bh);	} else {		if (!quiet_error(bh)) {			buffer_io_error(bh);			printk(KERN_WARNING "lost page write due to "					"I/O error on %s\n",				       bdevname(bh->b_bdev, b));		}		set_buffer_write_io_error(bh);		clear_buffer_uptodate(bh);	}	unlock_buffer(bh);	put_bh(bh);}EXPORT_SYMBOL(end_buffer_write_sync); /* * Various filesystems appear to want __find_get_block to be non-blocking. * But it's the page lock which protects the buffers.  To get around this, * we get exclusion from try_to_free_buffers with the blockdev mapping's * private_lock. * * Hack idea: for the blockdev mapping, i_bufferlist_lock contention * may be quite high.  This code could TryLock the page, and if that * succeeds, there is no need to take private_lock. (But if * private_lock is contended then so is mapping->tree_lock). */static struct buffer_head *__find_get_block_slow(struct block_device *bdev, sector_t block){	struct inode *bd_inode = bdev->bd_inode;	struct address_space *bd_mapping = bd_inode->i_mapping;	struct buffer_head *ret = NULL;	pgoff_t index;	struct buffer_head *bh;	struct buffer_head *head;	struct page *page;	int all_mapped = 1; 	index = block >> (PAGE_CACHE_SHIFT - bd_inode->i_blkbits);	page = find_get_page(bd_mapping, index);	if (!page)		goto out; 	spin_lock(&bd_mapping->private_lock);	if (!page_has_buffers(page))		goto out_unlock;	head = page_buffers(page);	bh = head;	do {		if (!buffer_mapped(bh))			all_mapped = 0;		else if (bh->b_blocknr == block) {			ret = bh;			get_bh(bh);			goto out_unlock;		}		bh = bh->b_this_page;	} while (bh != head); 	/* we might be here because some of the buffers on this page are	 * not mapped.  This is due to various races between	 * file io on the block device and getblk.  It gets dealt with	 * elsewhere, don't buffer_error if we had some unmapped buffers	 */	if (all_mapped) {		char b[BDEVNAME_SIZE]; 		printk("__find_get_block_slow() failed. "			"block=%llu, b_blocknr=%llu\n",			(unsigned long long)block,			(unsigned long long)bh->b_blocknr);		printk("b_state=0x%08lx, b_size=%zu\n",			bh->b_state, bh->b_size);		printk("device %s blocksize: %d\n", bdevname(bdev, b),			1 << bd_inode->i_blkbits);	}out_unlock:	spin_unlock(&bd_mapping->private_lock);	page_cache_release(page);out:	return ret;} /* * Kick the writeback threads then try to free up some ZONE_NORMAL memory. */static void free_more_memory(void){	struct zone *zone;	int nid; 	wakeup_flusher_threads(1024, WB_REASON_FREE_MORE_MEM);	yield(); 	for_each_online_node(nid) {		(void)first_zones_zonelist(node_zonelist(nid, GFP_NOFS),						gfp_zone(GFP_NOFS), NULL,						&zone);		if (zone)			try_to_free_pages(node_zonelist(nid, GFP_NOFS), 0,						GFP_NOFS, NULL);	}} /* * I/O completion handler for block_read_full_page() - pages * which come unlocked at the end of I/O. */static void end_buffer_async_read(struct buffer_head *bh, int uptodate){	unsigned long flags;	struct buffer_head *first;	struct buffer_head *tmp;	struct page *page;	int page_uptodate = 1; 	BUG_ON(!buffer_async_read(bh)); 	page = bh->b_page;	if (uptodate) {		set_buffer_uptodate(bh);	} else {		clear_buffer_uptodate(bh);		if (!quiet_error(bh))			buffer_io_error(bh);		SetPageError(page);	} 	/*	 * Be _very_ careful from here on. Bad things can happen if	 * two buffer heads end IO at almost the same time and both	 * decide that the page is now completely done.	 */	first = page_buffers(page);	local_irq_save(flags);	bit_spin_lock(BH_Uptodate_Lock, &first->b_state);	clear_buffer_async_read(bh);	unlock_buffer(bh);	tmp = bh;	do {		if (!buffer_uptodate(tmp))			page_uptodate = 0;		if (buffer_async_read(tmp)) {			BUG_ON(!buffer_locked(tmp));			goto still_busy;		}		tmp = tmp->b_this_page;	} while (tmp != bh);	bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);	local_irq_restore(flags); 	/*	 * If none of the buffers had errors and they are all	 * uptodate then we can set the page uptodate.	 */	if (page_uptodate && !PageError(page))		SetPageUptodate(page);	unlock_page(page);	return; still_busy:	bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);	local_irq_restore(flags);	return;} /* * Completion handler for block_write_full_page() - pages which are unlocked * during I/O, and which have PageWriteback cleared upon I/O completion. */void end_buffer_async_write(struct buffer_head *bh, int uptodate){	char b[BDEVNAME_SIZE];	unsigned long flags;	struct buffer_head *first;	struct buffer_head *tmp;	struct page *page; 	BUG_ON(!buffer_async_write(bh)); 	page = bh->b_page;	if (uptodate) {		set_buffer_uptodate(bh);	} else {		if (!quiet_error(bh)) {			buffer_io_error(bh);			printk(KERN_WARNING "lost page write due to "					"I/O error on %s\n",			       bdevname(bh->b_bdev, b));		}		set_bit(AS_EIO, &page->mapping->flags);		set_buffer_write_io_error(bh);		clear_buffer_uptodate(bh);		SetPageError(page);	} 	first = page_buffers(page);	local_irq_save(flags);	bit_spin_lock(BH_Uptodate_Lock, &first->b_state); 	clear_buffer_async_write(bh);	unlock_buffer(bh);	tmp = bh->b_this_page;	while (tmp != bh) {		if (buffer_async_write(tmp)) {			BUG_ON(!buffer_locked(tmp));			goto still_busy;		}		tmp = tmp->b_this_page;	}	bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);	local_irq_restore(flags);	end_page_writeback(page);	return; still_busy:	bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);	local_irq_restore(flags);	return;}EXPORT_SYMBOL(end_buffer_async_write); /* * If a page's buffers are under async readin (end_buffer_async_read * completion) then there is a possibility that another thread of * control could lock one of the buffers after it has completed * but while some of the other buffers have not completed.  This * locked buffer would confuse end_buffer_async_read() into not unlocking * the page.  So the absence of BH_Async_Read tells end_buffer_async_read() * that this buffer is not under async I/O. * * The page comes unlocked when it has no locked buffer_async buffers * left. * * PageLocked prevents anyone starting new async I/O reads any of * the buffers. * * PageWriteback is used to prevent simultaneous writeout of the same * page. * * PageLocked prevents anyone from starting writeback of a page which is * under read I/O (PageWriteback is only ever set against a locked page). */static void mark_buffer_async_read(struct buffer_head *bh){	bh->b_end_io = end_buffer_async_read;	set_buffer_async_read(bh);} static void mark_buffer_async_write_endio(struct buffer_head *bh,					  bh_end_io_t *handler){	bh->b_end_io = handler;	set_buffer_async_write(bh);} void mark_buffer_async_write(struct buffer_head *bh){	mark_buffer_async_write_endio(bh, end_buffer_async_write);}EXPORT_SYMBOL(mark_buffer_async_write);  /* * fs/buffer.c contains helper functions for buffer-backed address space's * fsync functions.  A common requirement for buffer-based filesystems is * that certain data from the backing blockdev needs to be written out for * a successful fsync().  For example, ext2 indirect blocks need to be * written back and waited upon before fsync() returns. * * The functions mark_buffer_inode_dirty(), fsync_inode_buffers(), * inode_has_buffers() and invalidate_inode_buffers() are provided for the * management of a list of dependent buffers at ->i_mapping->private_list. * * Locking is a little subtle: try_to_free_buffers() will remove buffers * from their controlling inode's queue when they are being freed.  But * try_to_free_buffers() will be operating against the *blockdev* mapping * at the time, not against the S_ISREG file which depends on those buffers. * So the locking for private_list is via the private_lock in the address_space * which backs the buffers.  Which is different from the address_space  * against which the buffers are listed.  So for a particular address_space, * mapping->private_lock does *not* protect mapping->private_list!  In fact, * mapping->private_list will always be protected by the backing blockdev's * ->private_lock. * * Which introduces a requirement: all buffers on an address_space's * ->private_list must be from the same address_space: the blockdev's. * * address_spaces which do not place buffers at ->private_list via these * utility functions are free to use private_lock and private_list for * whatever they want.  The only requirement is that list_empty(private_list) * be true at clear_inode() time. * * FIXME: clear_inode should not call invalidate_inode_buffers().  The * filesystems should do that.  invalidate_inode_buffers() should just go * BUG_ON(!list_empty). * * FIXME: mark_buffer_dirty_inode() is a data-plane operation.  It should * take an address_space, not an inode.  And it should be called * mark_buffer_dirty_fsync() to clearly define why those buffers are being * queued up. * * FIXME: mark_buffer_dirty_inode() doesn't need to add the buffer to the * list if it is already on a list.  Because if the buffer is on a list, * it *must* already be on the right one.  If not, the filesystem is being * silly.  This will save a ton of locking.  But first we have to ensure * that buffers are taken *off* the old inode's list when they are freed * (presumably in truncate).  That requires careful auditing of all * filesystems (do it inside bforget()).  It could also be done by bringing * b_inode back. */ /* * The buffer's backing address_space's private_lock must be held */static void __remove_assoc_queue(struct buffer_head *bh){	list_del_init(&bh->b_assoc_buffers);	WARN_ON(!bh->b_assoc_map);	if (buffer_write_io_error(bh))		set_bit(AS_EIO, &bh->b_assoc_map->flags);	bh->b_assoc_map = NULL;} int inode_has_buffers(struct inode *inode){	return !list_empty(&inode->i_data.private_list);} /* * osync is designed to support O_SYNC io.  It waits synchronously for * all already-submitted IO to complete, but does not queue any new * writes to the disk. * * To do O_SYNC writes, just queue the buffer writes with ll_rw_block as * you dirty the buffers, and then use osync_inode_buffers to wait for * completion.  Any other dirty buffers which are not yet queued for * write will not be flushed to disk by the osync. */static int osync_buffers_list(spinlock_t *lock, struct list_head *list){	struct buffer_head *bh;	struct list_head *p;	int err = 0; 	spin_lock(lock);repeat:	list_for_each_prev(p, list) {		bh = BH_ENTRY(p);		if (buffer_locked(bh)) {			get_bh(bh);

Lines 1–500 of 3295.