1407 lines · 37.1 KB · C
RawDownload
/* * fs/fs-writeback.c * * Copyright (C) 2002, Linus Torvalds. * * Contains all the functions related to writing back and waiting * upon dirty inodes against superblocks, and writing back dirty * pages against inodes.  ie: data writeback.  Writeout of the * inode itself is not handled here. * * 10Apr2002	Andrew Morton *		Split out of fs/inode.c *		Additions for address_space-based writeback */ #include <linux/kernel.h>#include <linux/module.h>#include <linux/spinlock.h>#include <linux/slab.h>#include <linux/sched.h>#include <linux/fs.h>#include <linux/mm.h>#include <linux/kthread.h>#include <linux/freezer.h>#include <linux/writeback.h>#include <linux/blkdev.h>#include <linux/backing-dev.h>#include <linux/buffer_head.h>#include <linux/tracepoint.h>#include "internal.h" /* * Passed into wb_writeback(), essentially a subset of writeback_control */struct wb_writeback_work {	long nr_pages;	struct super_block *sb;	unsigned long *older_than_this;	enum writeback_sync_modes sync_mode;	unsigned int tagged_writepages:1;	unsigned int for_kupdate:1;	unsigned int range_cyclic:1;	unsigned int for_background:1;	enum wb_reason reason;		/* why was writeback initiated? */ 	struct list_head list;		/* pending work list */	struct completion *done;	/* set if the caller waits */}; /* * Include the creation of the trace points after defining the * wb_writeback_work structure so that the definition remains local to this * file. */#define CREATE_TRACE_POINTS#include <trace/events/writeback.h> /* * We don't actually have pdflush, but this one is exported though /proc... */int nr_pdflush_threads; /** * writeback_in_progress - determine whether there is writeback in progress * @bdi: the device's backing_dev_info structure. * * Determine whether there is writeback waiting to be handled against a * backing device. */int writeback_in_progress(struct backing_dev_info *bdi){	return test_bit(BDI_writeback_running, &bdi->state);} static inline struct backing_dev_info *inode_to_bdi(struct inode *inode){	struct super_block *sb = inode->i_sb; 	if (strcmp(sb->s_type->name, "bdev") == 0)		return inode->i_mapping->backing_dev_info; 	return sb->s_bdi;} static inline struct inode *wb_inode(struct list_head *head){	return list_entry(head, struct inode, i_wb_list);} /* Wakeup flusher thread or forker thread to fork it. Requires bdi->wb_lock. */static void bdi_wakeup_flusher(struct backing_dev_info *bdi){	if (bdi->wb.task) {		wake_up_process(bdi->wb.task);	} else {		/*		 * The bdi thread isn't there, wake up the forker thread which		 * will create and run it.		 */		wake_up_process(default_backing_dev_info.wb.task);	}} static void bdi_queue_work(struct backing_dev_info *bdi,			   struct wb_writeback_work *work){	trace_writeback_queue(bdi, work); 	spin_lock_bh(&bdi->wb_lock);	list_add_tail(&work->list, &bdi->work_list);	if (!bdi->wb.task)		trace_writeback_nothread(bdi, work);	bdi_wakeup_flusher(bdi);	spin_unlock_bh(&bdi->wb_lock);} static void__bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages,		      bool range_cyclic, enum wb_reason reason){	struct wb_writeback_work *work; 	/*	 * This is WB_SYNC_NONE writeback, so if allocation fails just	 * wakeup the thread for old dirty data writeback	 */	work = kzalloc(sizeof(*work), GFP_ATOMIC);	if (!work) {		if (bdi->wb.task) {			trace_writeback_nowork(bdi);			wake_up_process(bdi->wb.task);		}		return;	} 	work->sync_mode	= WB_SYNC_NONE;	work->nr_pages	= nr_pages;	work->range_cyclic = range_cyclic;	work->reason	= reason; 	bdi_queue_work(bdi, work);} /** * bdi_start_writeback - start writeback * @bdi: the backing device to write from * @nr_pages: the number of pages to write * @reason: reason why some writeback work was initiated * * Description: *   This does WB_SYNC_NONE opportunistic writeback. The IO is only *   started when this function returns, we make no guarantees on *   completion. Caller need not hold sb s_umount semaphore. * */void bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages,			enum wb_reason reason){	__bdi_start_writeback(bdi, nr_pages, true, reason);} /** * bdi_start_background_writeback - start background writeback * @bdi: the backing device to write from * * Description: *   This makes sure WB_SYNC_NONE background writeback happens. When *   this function returns, it is only guaranteed that for given BDI *   some IO is happening if we are over background dirty threshold. *   Caller need not hold sb s_umount semaphore. */void bdi_start_background_writeback(struct backing_dev_info *bdi){	/*	 * We just wake up the flusher thread. It will perform background	 * writeback as soon as there is no other work to do.	 */	trace_writeback_wake_background(bdi);	spin_lock_bh(&bdi->wb_lock);	bdi_wakeup_flusher(bdi);	spin_unlock_bh(&bdi->wb_lock);} /* * Remove the inode from the writeback list it is on. */void inode_wb_list_del(struct inode *inode){	struct backing_dev_info *bdi = inode_to_bdi(inode); 	spin_lock(&bdi->wb.list_lock);	list_del_init(&inode->i_wb_list);	spin_unlock(&bdi->wb.list_lock);} /* * Redirty an inode: set its when-it-was dirtied timestamp and move it to the * furthest end of its superblock's dirty-inode list. * * Before stamping the inode's ->dirtied_when, we check to see whether it is * already the most-recently-dirtied inode on the b_dirty list.  If that is * the case then the inode must have been redirtied while it was being written * out and we don't reset its dirtied_when. */static void redirty_tail(struct inode *inode, struct bdi_writeback *wb){	assert_spin_locked(&wb->list_lock);	if (!list_empty(&wb->b_dirty)) {		struct inode *tail; 		tail = wb_inode(wb->b_dirty.next);		if (time_before(inode->dirtied_when, tail->dirtied_when))			inode->dirtied_when = jiffies;	}	list_move(&inode->i_wb_list, &wb->b_dirty);} /* * requeue inode for re-scanning after bdi->b_io list is exhausted. */static void requeue_io(struct inode *inode, struct bdi_writeback *wb){	assert_spin_locked(&wb->list_lock);	list_move(&inode->i_wb_list, &wb->b_more_io);} static void inode_sync_complete(struct inode *inode){	/*	 * Prevent speculative execution through	 * spin_unlock(&wb->list_lock);	 */ 	smp_mb();	wake_up_bit(&inode->i_state, __I_SYNC);} static bool inode_dirtied_after(struct inode *inode, unsigned long t){	bool ret = time_after(inode->dirtied_when, t);#ifndef CONFIG_64BIT	/*	 * For inodes being constantly redirtied, dirtied_when can get stuck.	 * It _appears_ to be in the future, but is actually in distant past.	 * This test is necessary to prevent such wrapped-around relative times	 * from permanently stopping the whole bdi writeback.	 */	ret = ret && time_before_eq(inode->dirtied_when, jiffies);#endif	return ret;} /* * Move expired dirty inodes from @delaying_queue to @dispatch_queue. */static int move_expired_inodes(struct list_head *delaying_queue,			       struct list_head *dispatch_queue,			       struct wb_writeback_work *work){	LIST_HEAD(tmp);	struct list_head *pos, *node;	struct super_block *sb = NULL;	struct inode *inode;	int do_sb_sort = 0;	int moved = 0; 	while (!list_empty(delaying_queue)) {		inode = wb_inode(delaying_queue->prev);		if (work->older_than_this &&		    inode_dirtied_after(inode, *work->older_than_this))			break;		if (sb && sb != inode->i_sb)			do_sb_sort = 1;		sb = inode->i_sb;		list_move(&inode->i_wb_list, &tmp);		moved++;	} 	/* just one sb in list, splice to dispatch_queue and we're done */	if (!do_sb_sort) {		list_splice(&tmp, dispatch_queue);		goto out;	} 	/* Move inodes from one superblock together */	while (!list_empty(&tmp)) {		sb = wb_inode(tmp.prev)->i_sb;		list_for_each_prev_safe(pos, node, &tmp) {			inode = wb_inode(pos);			if (inode->i_sb == sb)				list_move(&inode->i_wb_list, dispatch_queue);		}	}out:	return moved;} /* * Queue all expired dirty inodes for io, eldest first. * Before *         newly dirtied     b_dirty    b_io    b_more_io *         =============>    gf         edc     BA * After *         newly dirtied     b_dirty    b_io    b_more_io *         =============>    g          fBAedc *                                           | *                                           +--> dequeue for IO */static void queue_io(struct bdi_writeback *wb, struct wb_writeback_work *work){	int moved;	assert_spin_locked(&wb->list_lock);	list_splice_init(&wb->b_more_io, &wb->b_io);	moved = move_expired_inodes(&wb->b_dirty, &wb->b_io, work);	trace_writeback_queue_io(wb, work, moved);} static int write_inode(struct inode *inode, struct writeback_control *wbc){	if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode))		return inode->i_sb->s_op->write_inode(inode, wbc);	return 0;} /* * Wait for writeback on an inode to complete. */static void inode_wait_for_writeback(struct inode *inode,				     struct bdi_writeback *wb){	DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC);	wait_queue_head_t *wqh; 	wqh = bit_waitqueue(&inode->i_state, __I_SYNC);	while (inode->i_state & I_SYNC) {		spin_unlock(&inode->i_lock);		spin_unlock(&wb->list_lock);		__wait_on_bit(wqh, &wq, inode_wait, TASK_UNINTERRUPTIBLE);		spin_lock(&wb->list_lock);		spin_lock(&inode->i_lock);	}} /* * Write out an inode's dirty pages.  Called under wb->list_lock and * inode->i_lock.  Either the caller has an active reference on the inode or * the inode has I_WILL_FREE set. * * If `wait' is set, wait on the writeout. * * The whole writeout design is quite complex and fragile.  We want to avoid * starvation of particular inodes when others are being redirtied, prevent * livelocks, etc. */static intwriteback_single_inode(struct inode *inode, struct bdi_writeback *wb,		       struct writeback_control *wbc){	struct address_space *mapping = inode->i_mapping;	long nr_to_write = wbc->nr_to_write;	unsigned dirty;	int ret; 	assert_spin_locked(&wb->list_lock);	assert_spin_locked(&inode->i_lock); 	if (!atomic_read(&inode->i_count))		WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING)));	else		WARN_ON(inode->i_state & I_WILL_FREE); 	if (inode->i_state & I_SYNC) {		/*		 * If this inode is locked for writeback and we are not doing		 * writeback-for-data-integrity, move it to b_more_io so that		 * writeback can proceed with the other inodes on s_io.		 *		 * We'll have another go at writing back this inode when we		 * completed a full scan of b_io.		 */		if (wbc->sync_mode != WB_SYNC_ALL) {			requeue_io(inode, wb);			trace_writeback_single_inode_requeue(inode, wbc,							     nr_to_write);			return 0;		} 		/*		 * It's a data-integrity sync.  We must wait.		 */		inode_wait_for_writeback(inode, wb);	} 	BUG_ON(inode->i_state & I_SYNC); 	/* Set I_SYNC, reset I_DIRTY_PAGES */	inode->i_state |= I_SYNC;	inode->i_state &= ~I_DIRTY_PAGES;	spin_unlock(&inode->i_lock);	spin_unlock(&wb->list_lock); 	ret = do_writepages(mapping, wbc); 	/*	 * Make sure to wait on the data before writing out the metadata.	 * This is important for filesystems that modify metadata on data	 * I/O completion.	 */	if (wbc->sync_mode == WB_SYNC_ALL) {		int err = filemap_fdatawait(mapping);		if (ret == 0)			ret = err;	} 	/*	 * Some filesystems may redirty the inode during the writeback	 * due to delalloc, clear dirty metadata flags right before	 * write_inode()	 */	spin_lock(&inode->i_lock);	dirty = inode->i_state & I_DIRTY;	inode->i_state &= ~(I_DIRTY_SYNC | I_DIRTY_DATASYNC);	spin_unlock(&inode->i_lock);	/* Don't write the inode if only I_DIRTY_PAGES was set */	if (dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {		int err = write_inode(inode, wbc);		if (ret == 0)			ret = err;	} 	spin_lock(&wb->list_lock);	spin_lock(&inode->i_lock);	inode->i_state &= ~I_SYNC;	if (!(inode->i_state & I_FREEING)) {		/*		 * Sync livelock prevention. Each inode is tagged and synced in		 * one shot. If still dirty, it will be redirty_tail()'ed below.		 * Update the dirty time to prevent enqueue and sync it again.		 */		if ((inode->i_state & I_DIRTY) &&		    (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages))			inode->dirtied_when = jiffies; 		if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {			/*			 * We didn't write back all the pages.  nfs_writepages()			 * sometimes bales out without doing anything.			 */			inode->i_state |= I_DIRTY_PAGES;			if (wbc->nr_to_write <= 0) {				/*				 * slice used up: queue for next turn				 */				requeue_io(inode, wb);			} else {				/*				 * Writeback blocked by something other than				 * congestion. Delay the inode for some time to				 * avoid spinning on the CPU (100% iowait)				 * retrying writeback of the dirty page/inode				 * that cannot be performed immediately.				 */				redirty_tail(inode, wb);			}		} else if (inode->i_state & I_DIRTY) {			/*			 * Filesystems can dirty the inode during writeback			 * operations, such as delayed allocation during			 * submission or metadata updates after data IO			 * completion.			 */			redirty_tail(inode, wb);		} else {			/*			 * The inode is clean.  At this point we either have			 * a reference to the inode or it's on it's way out.			 * No need to add it back to the LRU.			 */			list_del_init(&inode->i_wb_list);		}	}	inode_sync_complete(inode);	trace_writeback_single_inode(inode, wbc, nr_to_write);	return ret;} static long writeback_chunk_size(struct backing_dev_info *bdi,				 struct wb_writeback_work *work){	long pages; 	/*	 * WB_SYNC_ALL mode does livelock avoidance by syncing dirty	 * inodes/pages in one big loop. Setting wbc.nr_to_write=LONG_MAX	 * here avoids calling into writeback_inodes_wb() more than once.	 *	 * The intended call sequence for WB_SYNC_ALL writeback is:	 *	 *      wb_writeback()	 *          writeback_sb_inodes()       <== called only once

Lines 1–500 of 1407.