2984 lines · 78.7 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/module.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;} static int sync_buffer(void *word){	struct block_device *bd;	struct buffer_head *bh		= container_of(word, struct buffer_head, b_state); 	smp_mb();	bd = bh->b_bdev;	if (bd)		blk_run_address_space(bd->bd_inode->i_mapping);	io_schedule();	return 0;} void fastcall __lock_buffer(struct buffer_head *bh){	wait_on_bit_lock(&bh->b_state, BH_Lock, sync_buffer,							TASK_UNINTERRUPTIBLE);}EXPORT_SYMBOL(__lock_buffer); void fastcall unlock_buffer(struct buffer_head *bh){	smp_mb__before_clear_bit();	clear_buffer_locked(bh);	smp_mb__after_clear_bit();	wake_up_bit(&bh->b_state, BH_Lock);} /* * 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, sync_buffer, TASK_UNINTERRUPTIBLE);} static void__clear_page_buffers(struct page *page){	ClearPagePrivate(page);	set_page_private(page, 0);	page_cache_release(page);} 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);} /* * 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){	if (uptodate) {		set_buffer_uptodate(bh);	} else {		/* This happens, due to failed READA attempts. */		clear_buffer_uptodate(bh);	}	unlock_buffer(bh);	put_bh(bh);} void end_buffer_write_sync(struct buffer_head *bh, int uptodate){	char b[BDEVNAME_SIZE]; 	if (uptodate) {		set_buffer_uptodate(bh);	} else {		if (!buffer_eopnotsupp(bh) && printk_ratelimit()) {			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);} /* * Write out and wait upon all the dirty data associated with a block * device via its mapping.  Does not take the superblock lock. */int sync_blockdev(struct block_device *bdev){	int ret = 0; 	if (bdev)		ret = filemap_write_and_wait(bdev->bd_inode->i_mapping);	return ret;}EXPORT_SYMBOL(sync_blockdev); /* * Write out and wait upon all dirty data associated with this * device.   Filesystem data as well as the underlying block * device.  Takes the superblock lock. */int fsync_bdev(struct block_device *bdev){	struct super_block *sb = get_super(bdev);	if (sb) {		int res = fsync_super(sb);		drop_super(sb);		return res;	}	return sync_blockdev(bdev);} /** * freeze_bdev  --  lock a filesystem and force it into a consistent state * @bdev:	blockdevice to lock * * This takes the block device bd_mount_sem to make sure no new mounts * happen on bdev until thaw_bdev() is called. * If a superblock is found on this device, we take the s_umount semaphore * on it to make sure nobody unmounts until the snapshot creation is done. */struct super_block *freeze_bdev(struct block_device *bdev){	struct super_block *sb; 	down(&bdev->bd_mount_sem);	sb = get_super(bdev);	if (sb && !(sb->s_flags & MS_RDONLY)) {		sb->s_frozen = SB_FREEZE_WRITE;		smp_wmb(); 		__fsync_super(sb); 		sb->s_frozen = SB_FREEZE_TRANS;		smp_wmb(); 		sync_blockdev(sb->s_bdev); 		if (sb->s_op->write_super_lockfs)			sb->s_op->write_super_lockfs(sb);	} 	sync_blockdev(bdev);	return sb;	/* thaw_bdev releases s->s_umount and bd_mount_sem */}EXPORT_SYMBOL(freeze_bdev); /** * thaw_bdev  -- unlock filesystem * @bdev:	blockdevice to unlock * @sb:		associated superblock * * Unlocks the filesystem and marks it writeable again after freeze_bdev(). */void thaw_bdev(struct block_device *bdev, struct super_block *sb){	if (sb) {		BUG_ON(sb->s_bdev != bdev); 		if (sb->s_op->unlockfs)			sb->s_op->unlockfs(sb);		sb->s_frozen = SB_UNFROZEN;		smp_wmb();		wake_up(&sb->s_wait_unfrozen);		drop_super(sb);	} 	up(&bdev->bd_mount_sem);}EXPORT_SYMBOL(thaw_bdev); /* * 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 (bh->b_blocknr == block) {			ret = bh;			get_bh(bh);			goto out_unlock;		}		if (!buffer_mapped(bh))			all_mapped = 0;		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) {		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 blocksize: %d\n", 1 << bd_inode->i_blkbits);	}out_unlock:	spin_unlock(&bd_mapping->private_lock);	page_cache_release(page);out:	return ret;} /* If invalidate_buffers() will trash dirty buffers, it means some kind   of fs corruption is going on. Trashing dirty data always imply losing   information that was supposed to be just stored on the physical layer   by the user.    Thus invalidate_buffers in general usage is not allwowed to trash   dirty buffers. For example ioctl(FLSBLKBUF) expects dirty data to   be preserved.  These buffers are simply skipped.     We also skip buffers which are still in use.  For example this can   happen if a userspace program is reading the block device.    NOTE: In the case where the user removed a removable-media-disk even if   there's still dirty data not synced on disk (due a bug in the device driver   or due an error of the user), by not destroying the dirty buffers we could   generate corruption also on the next media inserted, thus a parameter is   necessary to handle this case in the most safe way possible (trying   to not corrupt also the new disk inserted with the data belonging to   the old now corrupted disk). Also for the ramdisk the natural thing   to do in order to release the ramdisk memory is to destroy dirty buffers.    These are two special cases. Normal usage imply the device driver   to issue a sync on the device (without waiting I/O completion) and   then an invalidate_buffers call that doesn't trash dirty buffers.    For handling cache coherency with the blkdev pagecache the 'update' case   is been introduced. It is needed to re-read from disk any pinned   buffer. NOTE: re-reading from disk is destructive so we can do it only   when we assume nobody is changing the buffercache under our I/O and when   we think the disk contains more recent information than the buffercache.   The update == 1 pass marks the buffers we need to update, the update == 2   pass does the actual I/O. */void invalidate_bdev(struct block_device *bdev){	struct address_space *mapping = bdev->bd_inode->i_mapping; 	if (mapping->nrpages == 0)		return; 	invalidate_bh_lrus();	invalidate_mapping_pages(mapping, 0, -1);} /* * Kick pdflush then try to free up some ZONE_NORMAL memory. */static void free_more_memory(void){	struct zone **zones;	pg_data_t *pgdat; 	wakeup_pdflush(1024);	yield(); 	for_each_online_pgdat(pgdat) {		zones = pgdat->node_zonelists[gfp_zone(GFP_NOFS)].zones;		if (*zones)			try_to_free_pages(zones, GFP_NOFS);	}} /* * 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 (printk_ratelimit())			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. */static 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 (printk_ratelimit()) {			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;} /* * 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). */

Lines 1–500 of 2984.