• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * fs/fs-writeback.c
3  *
4  * Copyright (C) 2002, Linus Torvalds.
5  *
6  * Contains all the functions related to writing back and waiting
7  * upon dirty inodes against superblocks, and writing back dirty
8  * pages against inodes.  ie: data writeback.  Writeout of the
9  * inode itself is not handled here.
10  *
11  * 10Apr2002	Andrew Morton
12  *		Split out of fs/inode.c
13  *		Additions for address_space-based writeback
14  */
15 
16 #include <linux/kernel.h>
17 #include <linux/export.h>
18 #include <linux/spinlock.h>
19 #include <linux/slab.h>
20 #include <linux/sched.h>
21 #include <linux/fs.h>
22 #include <linux/mm.h>
23 #include <linux/pagemap.h>
24 #include <linux/kthread.h>
25 #include <linux/writeback.h>
26 #include <linux/blkdev.h>
27 #include <linux/backing-dev.h>
28 #include <linux/tracepoint.h>
29 #include <linux/device.h>
30 #include "internal.h"
31 
32 /*
33  * 4MB minimal write chunk size
34  */
35 #define MIN_WRITEBACK_PAGES	(4096UL >> (PAGE_CACHE_SHIFT - 10))
36 
37 /*
38  * Passed into wb_writeback(), essentially a subset of writeback_control
39  */
40 struct wb_writeback_work {
41 	long nr_pages;
42 	struct super_block *sb;
43 	unsigned long *older_than_this;
44 	enum writeback_sync_modes sync_mode;
45 	unsigned int tagged_writepages:1;
46 	unsigned int for_kupdate:1;
47 	unsigned int range_cyclic:1;
48 	unsigned int for_background:1;
49 	unsigned int for_sync:1;	/* sync(2) WB_SYNC_ALL writeback */
50 	enum wb_reason reason;		/* why was writeback initiated? */
51 
52 	struct list_head list;		/* pending work list */
53 	struct completion *done;	/* set if the caller waits */
54 };
55 
56 /**
57  * writeback_in_progress - determine whether there is writeback in progress
58  * @bdi: the device's backing_dev_info structure.
59  *
60  * Determine whether there is writeback waiting to be handled against a
61  * backing device.
62  */
writeback_in_progress(struct backing_dev_info * bdi)63 int writeback_in_progress(struct backing_dev_info *bdi)
64 {
65 	return test_bit(BDI_writeback_running, &bdi->state);
66 }
67 EXPORT_SYMBOL(writeback_in_progress);
68 
inode_to_bdi(struct inode * inode)69 static inline struct backing_dev_info *inode_to_bdi(struct inode *inode)
70 {
71 	struct super_block *sb = inode->i_sb;
72 
73 	if (sb_is_blkdev_sb(sb))
74 		return inode->i_mapping->backing_dev_info;
75 
76 	return sb->s_bdi;
77 }
78 
wb_inode(struct list_head * head)79 static inline struct inode *wb_inode(struct list_head *head)
80 {
81 	return list_entry(head, struct inode, i_wb_list);
82 }
83 
84 /*
85  * Include the creation of the trace points after defining the
86  * wb_writeback_work structure and inline functions so that the definition
87  * remains local to this file.
88  */
89 #define CREATE_TRACE_POINTS
90 #include <trace/events/writeback.h>
91 
92 EXPORT_TRACEPOINT_SYMBOL_GPL(wbc_writepage);
93 
bdi_wakeup_thread(struct backing_dev_info * bdi)94 static void bdi_wakeup_thread(struct backing_dev_info *bdi)
95 {
96 	spin_lock_bh(&bdi->wb_lock);
97 	if (test_bit(BDI_registered, &bdi->state))
98 		mod_delayed_work(bdi_wq, &bdi->wb.dwork, 0);
99 	spin_unlock_bh(&bdi->wb_lock);
100 }
101 
bdi_queue_work(struct backing_dev_info * bdi,struct wb_writeback_work * work)102 static void bdi_queue_work(struct backing_dev_info *bdi,
103 			   struct wb_writeback_work *work)
104 {
105 	trace_writeback_queue(bdi, work);
106 
107 	spin_lock_bh(&bdi->wb_lock);
108 	if (!test_bit(BDI_registered, &bdi->state)) {
109 		if (work->done)
110 			complete(work->done);
111 		goto out_unlock;
112 	}
113 	list_add_tail(&work->list, &bdi->work_list);
114 	mod_delayed_work(bdi_wq, &bdi->wb.dwork, 0);
115 out_unlock:
116 	spin_unlock_bh(&bdi->wb_lock);
117 }
118 
119 static void
__bdi_start_writeback(struct backing_dev_info * bdi,long nr_pages,bool range_cyclic,enum wb_reason reason)120 __bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages,
121 		      bool range_cyclic, enum wb_reason reason)
122 {
123 	struct wb_writeback_work *work;
124 
125 	/*
126 	 * This is WB_SYNC_NONE writeback, so if allocation fails just
127 	 * wakeup the thread for old dirty data writeback
128 	 */
129 	work = kzalloc(sizeof(*work), GFP_ATOMIC);
130 	if (!work) {
131 		trace_writeback_nowork(bdi);
132 		bdi_wakeup_thread(bdi);
133 		return;
134 	}
135 
136 	work->sync_mode	= WB_SYNC_NONE;
137 	work->nr_pages	= nr_pages;
138 	work->range_cyclic = range_cyclic;
139 	work->reason	= reason;
140 
141 	bdi_queue_work(bdi, work);
142 }
143 
144 /**
145  * bdi_start_writeback - start writeback
146  * @bdi: the backing device to write from
147  * @nr_pages: the number of pages to write
148  * @reason: reason why some writeback work was initiated
149  *
150  * Description:
151  *   This does WB_SYNC_NONE opportunistic writeback. The IO is only
152  *   started when this function returns, we make no guarantees on
153  *   completion. Caller need not hold sb s_umount semaphore.
154  *
155  */
bdi_start_writeback(struct backing_dev_info * bdi,long nr_pages,enum wb_reason reason)156 void bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages,
157 			enum wb_reason reason)
158 {
159 	__bdi_start_writeback(bdi, nr_pages, true, reason);
160 }
161 
162 /**
163  * bdi_start_background_writeback - start background writeback
164  * @bdi: the backing device to write from
165  *
166  * Description:
167  *   This makes sure WB_SYNC_NONE background writeback happens. When
168  *   this function returns, it is only guaranteed that for given BDI
169  *   some IO is happening if we are over background dirty threshold.
170  *   Caller need not hold sb s_umount semaphore.
171  */
bdi_start_background_writeback(struct backing_dev_info * bdi)172 void bdi_start_background_writeback(struct backing_dev_info *bdi)
173 {
174 	/*
175 	 * We just wake up the flusher thread. It will perform background
176 	 * writeback as soon as there is no other work to do.
177 	 */
178 	trace_writeback_wake_background(bdi);
179 	bdi_wakeup_thread(bdi);
180 }
181 
182 /*
183  * Remove the inode from the writeback list it is on.
184  */
inode_wb_list_del(struct inode * inode)185 void inode_wb_list_del(struct inode *inode)
186 {
187 	struct backing_dev_info *bdi = inode_to_bdi(inode);
188 
189 	spin_lock(&bdi->wb.list_lock);
190 	list_del_init(&inode->i_wb_list);
191 	spin_unlock(&bdi->wb.list_lock);
192 }
193 
194 /*
195  * Redirty an inode: set its when-it-was dirtied timestamp and move it to the
196  * furthest end of its superblock's dirty-inode list.
197  *
198  * Before stamping the inode's ->dirtied_when, we check to see whether it is
199  * already the most-recently-dirtied inode on the b_dirty list.  If that is
200  * the case then the inode must have been redirtied while it was being written
201  * out and we don't reset its dirtied_when.
202  */
redirty_tail(struct inode * inode,struct bdi_writeback * wb)203 static void redirty_tail(struct inode *inode, struct bdi_writeback *wb)
204 {
205 	assert_spin_locked(&wb->list_lock);
206 	if (!list_empty(&wb->b_dirty)) {
207 		struct inode *tail;
208 
209 		tail = wb_inode(wb->b_dirty.next);
210 		if (time_before(inode->dirtied_when, tail->dirtied_when))
211 			inode->dirtied_when = jiffies;
212 	}
213 	list_move(&inode->i_wb_list, &wb->b_dirty);
214 }
215 
216 /*
217  * requeue inode for re-scanning after bdi->b_io list is exhausted.
218  */
requeue_io(struct inode * inode,struct bdi_writeback * wb)219 static void requeue_io(struct inode *inode, struct bdi_writeback *wb)
220 {
221 	assert_spin_locked(&wb->list_lock);
222 	list_move(&inode->i_wb_list, &wb->b_more_io);
223 }
224 
inode_sync_complete(struct inode * inode)225 static void inode_sync_complete(struct inode *inode)
226 {
227 	inode->i_state &= ~I_SYNC;
228 	/* If inode is clean an unused, put it into LRU now... */
229 	inode_add_lru(inode);
230 	/* Waiters must see I_SYNC cleared before being woken up */
231 	smp_mb();
232 	wake_up_bit(&inode->i_state, __I_SYNC);
233 }
234 
inode_dirtied_after(struct inode * inode,unsigned long t)235 static bool inode_dirtied_after(struct inode *inode, unsigned long t)
236 {
237 	bool ret = time_after(inode->dirtied_when, t);
238 #ifndef CONFIG_64BIT
239 	/*
240 	 * For inodes being constantly redirtied, dirtied_when can get stuck.
241 	 * It _appears_ to be in the future, but is actually in distant past.
242 	 * This test is necessary to prevent such wrapped-around relative times
243 	 * from permanently stopping the whole bdi writeback.
244 	 */
245 	ret = ret && time_before_eq(inode->dirtied_when, jiffies);
246 #endif
247 	return ret;
248 }
249 
250 #define EXPIRE_DIRTY_ATIME 0x0001
251 
252 /*
253  * Move expired (dirtied before work->older_than_this) dirty inodes from
254  * @delaying_queue to @dispatch_queue.
255  */
move_expired_inodes(struct list_head * delaying_queue,struct list_head * dispatch_queue,int flags,struct wb_writeback_work * work)256 static int move_expired_inodes(struct list_head *delaying_queue,
257 			       struct list_head *dispatch_queue,
258 			       int flags,
259 			       struct wb_writeback_work *work)
260 {
261 	unsigned long *older_than_this = NULL;
262 	unsigned long expire_time;
263 	LIST_HEAD(tmp);
264 	struct list_head *pos, *node;
265 	struct super_block *sb = NULL;
266 	struct inode *inode;
267 	int do_sb_sort = 0;
268 	int moved = 0;
269 
270 	if ((flags & EXPIRE_DIRTY_ATIME) == 0)
271 		older_than_this = work->older_than_this;
272 	else if ((work->reason == WB_REASON_SYNC) == 0) {
273 		expire_time = jiffies - (HZ * 86400);
274 		older_than_this = &expire_time;
275 	}
276 	while (!list_empty(delaying_queue)) {
277 		inode = wb_inode(delaying_queue->prev);
278 		if (older_than_this &&
279 		    inode_dirtied_after(inode, *older_than_this))
280 			break;
281 		list_move(&inode->i_wb_list, &tmp);
282 		moved++;
283 		if (flags & EXPIRE_DIRTY_ATIME)
284 			set_bit(__I_DIRTY_TIME_EXPIRED, &inode->i_state);
285 		if (sb_is_blkdev_sb(inode->i_sb))
286 			continue;
287 		if (sb && sb != inode->i_sb)
288 			do_sb_sort = 1;
289 		sb = inode->i_sb;
290 	}
291 
292 	/* just one sb in list, splice to dispatch_queue and we're done */
293 	if (!do_sb_sort) {
294 		list_splice(&tmp, dispatch_queue);
295 		goto out;
296 	}
297 
298 	/* Move inodes from one superblock together */
299 	while (!list_empty(&tmp)) {
300 		sb = wb_inode(tmp.prev)->i_sb;
301 		list_for_each_prev_safe(pos, node, &tmp) {
302 			inode = wb_inode(pos);
303 			if (inode->i_sb == sb)
304 				list_move(&inode->i_wb_list, dispatch_queue);
305 		}
306 	}
307 out:
308 	return moved;
309 }
310 
311 /*
312  * Queue all expired dirty inodes for io, eldest first.
313  * Before
314  *         newly dirtied     b_dirty    b_io    b_more_io
315  *         =============>    gf         edc     BA
316  * After
317  *         newly dirtied     b_dirty    b_io    b_more_io
318  *         =============>    g          fBAedc
319  *                                           |
320  *                                           +--> dequeue for IO
321  */
queue_io(struct bdi_writeback * wb,struct wb_writeback_work * work)322 static void queue_io(struct bdi_writeback *wb, struct wb_writeback_work *work)
323 {
324 	int moved;
325 
326 	assert_spin_locked(&wb->list_lock);
327 	list_splice_init(&wb->b_more_io, &wb->b_io);
328 	moved = move_expired_inodes(&wb->b_dirty, &wb->b_io, 0, work);
329 	moved += move_expired_inodes(&wb->b_dirty_time, &wb->b_io,
330 				     EXPIRE_DIRTY_ATIME, work);
331 	trace_writeback_queue_io(wb, work, moved);
332 }
333 
write_inode(struct inode * inode,struct writeback_control * wbc)334 static int write_inode(struct inode *inode, struct writeback_control *wbc)
335 {
336 	int ret;
337 
338 	if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode)) {
339 		trace_writeback_write_inode_start(inode, wbc);
340 		ret = inode->i_sb->s_op->write_inode(inode, wbc);
341 		trace_writeback_write_inode(inode, wbc);
342 		return ret;
343 	}
344 	return 0;
345 }
346 
347 /*
348  * Wait for writeback on an inode to complete. Called with i_lock held.
349  * Caller must make sure inode cannot go away when we drop i_lock.
350  */
__inode_wait_for_writeback(struct inode * inode)351 static void __inode_wait_for_writeback(struct inode *inode)
352 	__releases(inode->i_lock)
353 	__acquires(inode->i_lock)
354 {
355 	DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC);
356 	wait_queue_head_t *wqh;
357 
358 	wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
359 	while (inode->i_state & I_SYNC) {
360 		spin_unlock(&inode->i_lock);
361 		__wait_on_bit(wqh, &wq, bit_wait,
362 			      TASK_UNINTERRUPTIBLE);
363 		spin_lock(&inode->i_lock);
364 	}
365 }
366 
367 /*
368  * Wait for writeback on an inode to complete. Caller must have inode pinned.
369  */
inode_wait_for_writeback(struct inode * inode)370 void inode_wait_for_writeback(struct inode *inode)
371 {
372 	spin_lock(&inode->i_lock);
373 	__inode_wait_for_writeback(inode);
374 	spin_unlock(&inode->i_lock);
375 }
376 
377 /*
378  * Sleep until I_SYNC is cleared. This function must be called with i_lock
379  * held and drops it. It is aimed for callers not holding any inode reference
380  * so once i_lock is dropped, inode can go away.
381  */
inode_sleep_on_writeback(struct inode * inode)382 static void inode_sleep_on_writeback(struct inode *inode)
383 	__releases(inode->i_lock)
384 {
385 	DEFINE_WAIT(wait);
386 	wait_queue_head_t *wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
387 	int sleep;
388 
389 	prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
390 	sleep = inode->i_state & I_SYNC;
391 	spin_unlock(&inode->i_lock);
392 	if (sleep)
393 		schedule();
394 	finish_wait(wqh, &wait);
395 }
396 
397 /*
398  * Find proper writeback list for the inode depending on its current state and
399  * possibly also change of its state while we were doing writeback.  Here we
400  * handle things such as livelock prevention or fairness of writeback among
401  * inodes. This function can be called only by flusher thread - noone else
402  * processes all inodes in writeback lists and requeueing inodes behind flusher
403  * thread's back can have unexpected consequences.
404  */
requeue_inode(struct inode * inode,struct bdi_writeback * wb,struct writeback_control * wbc)405 static void requeue_inode(struct inode *inode, struct bdi_writeback *wb,
406 			  struct writeback_control *wbc)
407 {
408 	if (inode->i_state & I_FREEING)
409 		return;
410 
411 	/*
412 	 * Sync livelock prevention. Each inode is tagged and synced in one
413 	 * shot. If still dirty, it will be redirty_tail()'ed below.  Update
414 	 * the dirty time to prevent enqueue and sync it again.
415 	 */
416 	if ((inode->i_state & I_DIRTY) &&
417 	    (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages))
418 		inode->dirtied_when = jiffies;
419 
420 	if (wbc->pages_skipped) {
421 		/*
422 		 * writeback is not making progress due to locked
423 		 * buffers. Skip this inode for now.
424 		 */
425 		redirty_tail(inode, wb);
426 		return;
427 	}
428 
429 	if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY)) {
430 		/*
431 		 * We didn't write back all the pages.  nfs_writepages()
432 		 * sometimes bales out without doing anything.
433 		 */
434 		if (wbc->nr_to_write <= 0) {
435 			/* Slice used up. Queue for next turn. */
436 			requeue_io(inode, wb);
437 		} else {
438 			/*
439 			 * Writeback blocked by something other than
440 			 * congestion. Delay the inode for some time to
441 			 * avoid spinning on the CPU (100% iowait)
442 			 * retrying writeback of the dirty page/inode
443 			 * that cannot be performed immediately.
444 			 */
445 			redirty_tail(inode, wb);
446 		}
447 	} else if (inode->i_state & I_DIRTY) {
448 		/*
449 		 * Filesystems can dirty the inode during writeback operations,
450 		 * such as delayed allocation during submission or metadata
451 		 * updates after data IO completion.
452 		 */
453 		redirty_tail(inode, wb);
454 	} else if (inode->i_state & I_DIRTY_TIME) {
455 		list_move(&inode->i_wb_list, &wb->b_dirty_time);
456 	} else {
457 		/* The inode is clean. Remove from writeback lists. */
458 		list_del_init(&inode->i_wb_list);
459 	}
460 }
461 
462 /*
463  * Write out an inode and its dirty pages. Do not update the writeback list
464  * linkage. That is left to the caller. The caller is also responsible for
465  * setting I_SYNC flag and calling inode_sync_complete() to clear it.
466  */
467 static int
__writeback_single_inode(struct inode * inode,struct writeback_control * wbc)468 __writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
469 {
470 	struct address_space *mapping = inode->i_mapping;
471 	long nr_to_write = wbc->nr_to_write;
472 	unsigned dirty;
473 	int ret;
474 
475 	WARN_ON(!(inode->i_state & I_SYNC));
476 
477 	trace_writeback_single_inode_start(inode, wbc, nr_to_write);
478 
479 	ret = do_writepages(mapping, wbc);
480 
481 	/*
482 	 * Make sure to wait on the data before writing out the metadata.
483 	 * This is important for filesystems that modify metadata on data
484 	 * I/O completion. We don't do it for sync(2) writeback because it has a
485 	 * separate, external IO completion path and ->sync_fs for guaranteeing
486 	 * inode metadata is written back correctly.
487 	 */
488 	if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync) {
489 		int err = filemap_fdatawait(mapping);
490 		if (ret == 0)
491 			ret = err;
492 	}
493 
494 	/*
495 	 * Some filesystems may redirty the inode during the writeback
496 	 * due to delalloc, clear dirty metadata flags right before
497 	 * write_inode()
498 	 */
499 	spin_lock(&inode->i_lock);
500 
501 	dirty = inode->i_state & I_DIRTY;
502 	if (((dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) &&
503 	     (inode->i_state & I_DIRTY_TIME)) ||
504 	    (inode->i_state & I_DIRTY_TIME_EXPIRED)) {
505 		dirty |= I_DIRTY_TIME | I_DIRTY_TIME_EXPIRED;
506 		trace_writeback_lazytime(inode);
507 	}
508 	inode->i_state &= ~dirty;
509 
510 	/*
511 	 * Paired with smp_mb() in __mark_inode_dirty().  This allows
512 	 * __mark_inode_dirty() to test i_state without grabbing i_lock -
513 	 * either they see the I_DIRTY bits cleared or we see the dirtied
514 	 * inode.
515 	 *
516 	 * I_DIRTY_PAGES is always cleared together above even if @mapping
517 	 * still has dirty pages.  The flag is reinstated after smp_mb() if
518 	 * necessary.  This guarantees that either __mark_inode_dirty()
519 	 * sees clear I_DIRTY_PAGES or we see PAGECACHE_TAG_DIRTY.
520 	 */
521 	smp_mb();
522 
523 	if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
524 		inode->i_state |= I_DIRTY_PAGES;
525 
526 	spin_unlock(&inode->i_lock);
527 
528 	if (dirty & I_DIRTY_TIME)
529 		mark_inode_dirty_sync(inode);
530 	/* Don't write the inode if only I_DIRTY_PAGES was set */
531 	if (dirty & ~I_DIRTY_PAGES) {
532 		int err = write_inode(inode, wbc);
533 		if (ret == 0)
534 			ret = err;
535 	}
536 	trace_writeback_single_inode(inode, wbc, nr_to_write);
537 	return ret;
538 }
539 
540 /*
541  * Write out an inode's dirty pages. Either the caller has an active reference
542  * on the inode or the inode has I_WILL_FREE set.
543  *
544  * This function is designed to be called for writing back one inode which
545  * we go e.g. from filesystem. Flusher thread uses __writeback_single_inode()
546  * and does more profound writeback list handling in writeback_sb_inodes().
547  */
548 static int
writeback_single_inode(struct inode * inode,struct bdi_writeback * wb,struct writeback_control * wbc)549 writeback_single_inode(struct inode *inode, struct bdi_writeback *wb,
550 		       struct writeback_control *wbc)
551 {
552 	int ret = 0;
553 
554 	spin_lock(&inode->i_lock);
555 	if (!atomic_read(&inode->i_count))
556 		WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING)));
557 	else
558 		WARN_ON(inode->i_state & I_WILL_FREE);
559 
560 	if (inode->i_state & I_SYNC) {
561 		if (wbc->sync_mode != WB_SYNC_ALL)
562 			goto out;
563 		/*
564 		 * It's a data-integrity sync. We must wait. Since callers hold
565 		 * inode reference or inode has I_WILL_FREE set, it cannot go
566 		 * away under us.
567 		 */
568 		__inode_wait_for_writeback(inode);
569 	}
570 	WARN_ON(inode->i_state & I_SYNC);
571 	/*
572 	 * Skip inode if it is clean and we have no outstanding writeback in
573 	 * WB_SYNC_ALL mode. We don't want to mess with writeback lists in this
574 	 * function since flusher thread may be doing for example sync in
575 	 * parallel and if we move the inode, it could get skipped. So here we
576 	 * make sure inode is on some writeback list and leave it there unless
577 	 * we have completely cleaned the inode.
578 	 */
579 	if (!(inode->i_state & I_DIRTY_ALL) &&
580 	    (wbc->sync_mode != WB_SYNC_ALL ||
581 	     !mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK)))
582 		goto out;
583 	inode->i_state |= I_SYNC;
584 	spin_unlock(&inode->i_lock);
585 
586 	ret = __writeback_single_inode(inode, wbc);
587 
588 	spin_lock(&wb->list_lock);
589 	spin_lock(&inode->i_lock);
590 	/*
591 	 * If inode is clean, remove it from writeback lists. Otherwise don't
592 	 * touch it. See comment above for explanation.
593 	 */
594 	if (!(inode->i_state & I_DIRTY_ALL))
595 		list_del_init(&inode->i_wb_list);
596 	spin_unlock(&wb->list_lock);
597 	inode_sync_complete(inode);
598 out:
599 	spin_unlock(&inode->i_lock);
600 	return ret;
601 }
602 
writeback_chunk_size(struct backing_dev_info * bdi,struct wb_writeback_work * work)603 static long writeback_chunk_size(struct backing_dev_info *bdi,
604 				 struct wb_writeback_work *work)
605 {
606 	long pages;
607 
608 	/*
609 	 * WB_SYNC_ALL mode does livelock avoidance by syncing dirty
610 	 * inodes/pages in one big loop. Setting wbc.nr_to_write=LONG_MAX
611 	 * here avoids calling into writeback_inodes_wb() more than once.
612 	 *
613 	 * The intended call sequence for WB_SYNC_ALL writeback is:
614 	 *
615 	 *      wb_writeback()
616 	 *          writeback_sb_inodes()       <== called only once
617 	 *              write_cache_pages()     <== called once for each inode
618 	 *                   (quickly) tag currently dirty pages
619 	 *                   (maybe slowly) sync all tagged pages
620 	 */
621 	if (work->sync_mode == WB_SYNC_ALL || work->tagged_writepages)
622 		pages = LONG_MAX;
623 	else {
624 		pages = min(bdi->avg_write_bandwidth / 2,
625 			    global_dirty_limit / DIRTY_SCOPE);
626 		pages = min(pages, work->nr_pages);
627 		pages = round_down(pages + MIN_WRITEBACK_PAGES,
628 				   MIN_WRITEBACK_PAGES);
629 	}
630 
631 	return pages;
632 }
633 
634 /*
635  * Write a portion of b_io inodes which belong to @sb.
636  *
637  * Return the number of pages and/or inodes written.
638  */
writeback_sb_inodes(struct super_block * sb,struct bdi_writeback * wb,struct wb_writeback_work * work)639 static long writeback_sb_inodes(struct super_block *sb,
640 				struct bdi_writeback *wb,
641 				struct wb_writeback_work *work)
642 {
643 	struct writeback_control wbc = {
644 		.sync_mode		= work->sync_mode,
645 		.tagged_writepages	= work->tagged_writepages,
646 		.for_kupdate		= work->for_kupdate,
647 		.for_background		= work->for_background,
648 		.for_sync		= work->for_sync,
649 		.range_cyclic		= work->range_cyclic,
650 		.range_start		= 0,
651 		.range_end		= LLONG_MAX,
652 	};
653 	unsigned long start_time = jiffies;
654 	long write_chunk;
655 	long wrote = 0;  /* count both pages and inodes */
656 
657 	while (!list_empty(&wb->b_io)) {
658 		struct inode *inode = wb_inode(wb->b_io.prev);
659 
660 		if (inode->i_sb != sb) {
661 			if (work->sb) {
662 				/*
663 				 * We only want to write back data for this
664 				 * superblock, move all inodes not belonging
665 				 * to it back onto the dirty list.
666 				 */
667 				redirty_tail(inode, wb);
668 				continue;
669 			}
670 
671 			/*
672 			 * The inode belongs to a different superblock.
673 			 * Bounce back to the caller to unpin this and
674 			 * pin the next superblock.
675 			 */
676 			break;
677 		}
678 
679 		/*
680 		 * Don't bother with new inodes or inodes being freed, first
681 		 * kind does not need periodic writeout yet, and for the latter
682 		 * kind writeout is handled by the freer.
683 		 */
684 		spin_lock(&inode->i_lock);
685 		if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
686 			spin_unlock(&inode->i_lock);
687 			redirty_tail(inode, wb);
688 			continue;
689 		}
690 		if ((inode->i_state & I_SYNC) && wbc.sync_mode != WB_SYNC_ALL) {
691 			/*
692 			 * If this inode is locked for writeback and we are not
693 			 * doing writeback-for-data-integrity, move it to
694 			 * b_more_io so that writeback can proceed with the
695 			 * other inodes on s_io.
696 			 *
697 			 * We'll have another go at writing back this inode
698 			 * when we completed a full scan of b_io.
699 			 */
700 			spin_unlock(&inode->i_lock);
701 			requeue_io(inode, wb);
702 			trace_writeback_sb_inodes_requeue(inode);
703 			continue;
704 		}
705 		spin_unlock(&wb->list_lock);
706 
707 		/*
708 		 * We already requeued the inode if it had I_SYNC set and we
709 		 * are doing WB_SYNC_NONE writeback. So this catches only the
710 		 * WB_SYNC_ALL case.
711 		 */
712 		if (inode->i_state & I_SYNC) {
713 			/* Wait for I_SYNC. This function drops i_lock... */
714 			inode_sleep_on_writeback(inode);
715 			/* Inode may be gone, start again */
716 			spin_lock(&wb->list_lock);
717 			continue;
718 		}
719 		inode->i_state |= I_SYNC;
720 		spin_unlock(&inode->i_lock);
721 
722 		write_chunk = writeback_chunk_size(wb->bdi, work);
723 		wbc.nr_to_write = write_chunk;
724 		wbc.pages_skipped = 0;
725 
726 		/*
727 		 * We use I_SYNC to pin the inode in memory. While it is set
728 		 * evict_inode() will wait so the inode cannot be freed.
729 		 */
730 		__writeback_single_inode(inode, &wbc);
731 
732 		work->nr_pages -= write_chunk - wbc.nr_to_write;
733 		wrote += write_chunk - wbc.nr_to_write;
734 
735 		if (need_resched()) {
736 			/*
737 			 * We're trying to balance between building up a nice
738 			 * long list of IOs to improve our merge rate, and
739 			 * getting those IOs out quickly for anyone throttling
740 			 * in balance_dirty_pages().  cond_resched() doesn't
741 			 * unplug, so get our IOs out the door before we
742 			 * give up the CPU.
743 			 */
744 			blk_flush_plug(current);
745 			cond_resched();
746 		}
747 
748 
749 		spin_lock(&wb->list_lock);
750 		spin_lock(&inode->i_lock);
751 		if (!(inode->i_state & I_DIRTY_ALL))
752 			wrote++;
753 		requeue_inode(inode, wb, &wbc);
754 		inode_sync_complete(inode);
755 		spin_unlock(&inode->i_lock);
756 
757 		/*
758 		 * bail out to wb_writeback() often enough to check
759 		 * background threshold and other termination conditions.
760 		 */
761 		if (wrote) {
762 			if (time_is_before_jiffies(start_time + HZ / 10UL))
763 				break;
764 			if (work->nr_pages <= 0)
765 				break;
766 		}
767 	}
768 	return wrote;
769 }
770 
__writeback_inodes_wb(struct bdi_writeback * wb,struct wb_writeback_work * work)771 static long __writeback_inodes_wb(struct bdi_writeback *wb,
772 				  struct wb_writeback_work *work)
773 {
774 	unsigned long start_time = jiffies;
775 	long wrote = 0;
776 
777 	while (!list_empty(&wb->b_io)) {
778 		struct inode *inode = wb_inode(wb->b_io.prev);
779 		struct super_block *sb = inode->i_sb;
780 
781 		if (!grab_super_passive(sb)) {
782 			/*
783 			 * grab_super_passive() may fail consistently due to
784 			 * s_umount being grabbed by someone else. Don't use
785 			 * requeue_io() to avoid busy retrying the inode/sb.
786 			 */
787 			redirty_tail(inode, wb);
788 			continue;
789 		}
790 		wrote += writeback_sb_inodes(sb, wb, work);
791 		drop_super(sb);
792 
793 		/* refer to the same tests at the end of writeback_sb_inodes */
794 		if (wrote) {
795 			if (time_is_before_jiffies(start_time + HZ / 10UL))
796 				break;
797 			if (work->nr_pages <= 0)
798 				break;
799 		}
800 	}
801 	/* Leave any unwritten inodes on b_io */
802 	return wrote;
803 }
804 
writeback_inodes_wb(struct bdi_writeback * wb,long nr_pages,enum wb_reason reason)805 static long writeback_inodes_wb(struct bdi_writeback *wb, long nr_pages,
806 				enum wb_reason reason)
807 {
808 	struct wb_writeback_work work = {
809 		.nr_pages	= nr_pages,
810 		.sync_mode	= WB_SYNC_NONE,
811 		.range_cyclic	= 1,
812 		.reason		= reason,
813 	};
814 
815 	spin_lock(&wb->list_lock);
816 	if (list_empty(&wb->b_io))
817 		queue_io(wb, &work);
818 	__writeback_inodes_wb(wb, &work);
819 	spin_unlock(&wb->list_lock);
820 
821 	return nr_pages - work.nr_pages;
822 }
823 
over_bground_thresh(struct backing_dev_info * bdi)824 static bool over_bground_thresh(struct backing_dev_info *bdi)
825 {
826 	unsigned long background_thresh, dirty_thresh;
827 
828 	global_dirty_limits(&background_thresh, &dirty_thresh);
829 
830 	if (global_page_state(NR_FILE_DIRTY) +
831 	    global_page_state(NR_UNSTABLE_NFS) > background_thresh)
832 		return true;
833 
834 	if (bdi_stat(bdi, BDI_RECLAIMABLE) >
835 				bdi_dirty_limit(bdi, background_thresh))
836 		return true;
837 
838 	return false;
839 }
840 
841 /*
842  * Called under wb->list_lock. If there are multiple wb per bdi,
843  * only the flusher working on the first wb should do it.
844  */
wb_update_bandwidth(struct bdi_writeback * wb,unsigned long start_time)845 static void wb_update_bandwidth(struct bdi_writeback *wb,
846 				unsigned long start_time)
847 {
848 	__bdi_update_bandwidth(wb->bdi, 0, 0, 0, 0, 0, start_time);
849 }
850 
851 /*
852  * Explicit flushing or periodic writeback of "old" data.
853  *
854  * Define "old": the first time one of an inode's pages is dirtied, we mark the
855  * dirtying-time in the inode's address_space.  So this periodic writeback code
856  * just walks the superblock inode list, writing back any inodes which are
857  * older than a specific point in time.
858  *
859  * Try to run once per dirty_writeback_interval.  But if a writeback event
860  * takes longer than a dirty_writeback_interval interval, then leave a
861  * one-second gap.
862  *
863  * older_than_this takes precedence over nr_to_write.  So we'll only write back
864  * all dirty pages if they are all attached to "old" mappings.
865  */
wb_writeback(struct bdi_writeback * wb,struct wb_writeback_work * work)866 static long wb_writeback(struct bdi_writeback *wb,
867 			 struct wb_writeback_work *work)
868 {
869 	unsigned long wb_start = jiffies;
870 	long nr_pages = work->nr_pages;
871 	unsigned long oldest_jif;
872 	struct inode *inode;
873 	long progress;
874 
875 	oldest_jif = jiffies;
876 	work->older_than_this = &oldest_jif;
877 
878 	spin_lock(&wb->list_lock);
879 	for (;;) {
880 		/*
881 		 * Stop writeback when nr_pages has been consumed
882 		 */
883 		if (work->nr_pages <= 0)
884 			break;
885 
886 		/*
887 		 * Background writeout and kupdate-style writeback may
888 		 * run forever. Stop them if there is other work to do
889 		 * so that e.g. sync can proceed. They'll be restarted
890 		 * after the other works are all done.
891 		 */
892 		if ((work->for_background || work->for_kupdate) &&
893 		    !list_empty(&wb->bdi->work_list))
894 			break;
895 
896 		/*
897 		 * For background writeout, stop when we are below the
898 		 * background dirty threshold
899 		 */
900 		if (work->for_background && !over_bground_thresh(wb->bdi))
901 			break;
902 
903 		/*
904 		 * Kupdate and background works are special and we want to
905 		 * include all inodes that need writing. Livelock avoidance is
906 		 * handled by these works yielding to any other work so we are
907 		 * safe.
908 		 */
909 		if (work->for_kupdate) {
910 			oldest_jif = jiffies -
911 				msecs_to_jiffies(dirty_expire_interval * 10);
912 		} else if (work->for_background)
913 			oldest_jif = jiffies;
914 
915 		trace_writeback_start(wb->bdi, work);
916 		if (list_empty(&wb->b_io))
917 			queue_io(wb, work);
918 		if (work->sb)
919 			progress = writeback_sb_inodes(work->sb, wb, work);
920 		else
921 			progress = __writeback_inodes_wb(wb, work);
922 		trace_writeback_written(wb->bdi, work);
923 
924 		wb_update_bandwidth(wb, wb_start);
925 
926 		/*
927 		 * Did we write something? Try for more
928 		 *
929 		 * Dirty inodes are moved to b_io for writeback in batches.
930 		 * The completion of the current batch does not necessarily
931 		 * mean the overall work is done. So we keep looping as long
932 		 * as made some progress on cleaning pages or inodes.
933 		 */
934 		if (progress)
935 			continue;
936 		/*
937 		 * No more inodes for IO, bail
938 		 */
939 		if (list_empty(&wb->b_more_io))
940 			break;
941 		/*
942 		 * Nothing written. Wait for some inode to
943 		 * become available for writeback. Otherwise
944 		 * we'll just busyloop.
945 		 */
946 		if (!list_empty(&wb->b_more_io))  {
947 			trace_writeback_wait(wb->bdi, work);
948 			inode = wb_inode(wb->b_more_io.prev);
949 			spin_lock(&inode->i_lock);
950 			spin_unlock(&wb->list_lock);
951 			/* This function drops i_lock... */
952 			inode_sleep_on_writeback(inode);
953 			spin_lock(&wb->list_lock);
954 		}
955 	}
956 	spin_unlock(&wb->list_lock);
957 
958 	return nr_pages - work->nr_pages;
959 }
960 
961 /*
962  * Return the next wb_writeback_work struct that hasn't been processed yet.
963  */
964 static struct wb_writeback_work *
get_next_work_item(struct backing_dev_info * bdi)965 get_next_work_item(struct backing_dev_info *bdi)
966 {
967 	struct wb_writeback_work *work = NULL;
968 
969 	spin_lock_bh(&bdi->wb_lock);
970 	if (!list_empty(&bdi->work_list)) {
971 		work = list_entry(bdi->work_list.next,
972 				  struct wb_writeback_work, list);
973 		list_del_init(&work->list);
974 	}
975 	spin_unlock_bh(&bdi->wb_lock);
976 	return work;
977 }
978 
979 /*
980  * Add in the number of potentially dirty inodes, because each inode
981  * write can dirty pagecache in the underlying blockdev.
982  */
get_nr_dirty_pages(void)983 static unsigned long get_nr_dirty_pages(void)
984 {
985 	return global_page_state(NR_FILE_DIRTY) +
986 		global_page_state(NR_UNSTABLE_NFS) +
987 		get_nr_dirty_inodes();
988 }
989 
wb_check_background_flush(struct bdi_writeback * wb)990 static long wb_check_background_flush(struct bdi_writeback *wb)
991 {
992 	if (over_bground_thresh(wb->bdi)) {
993 
994 		struct wb_writeback_work work = {
995 			.nr_pages	= LONG_MAX,
996 			.sync_mode	= WB_SYNC_NONE,
997 			.for_background	= 1,
998 			.range_cyclic	= 1,
999 			.reason		= WB_REASON_BACKGROUND,
1000 		};
1001 
1002 		return wb_writeback(wb, &work);
1003 	}
1004 
1005 	return 0;
1006 }
1007 
wb_check_old_data_flush(struct bdi_writeback * wb)1008 static long wb_check_old_data_flush(struct bdi_writeback *wb)
1009 {
1010 	unsigned long expired;
1011 	long nr_pages;
1012 
1013 	/*
1014 	 * When set to zero, disable periodic writeback
1015 	 */
1016 	if (!dirty_writeback_interval)
1017 		return 0;
1018 
1019 	expired = wb->last_old_flush +
1020 			msecs_to_jiffies(dirty_writeback_interval * 10);
1021 	if (time_before(jiffies, expired))
1022 		return 0;
1023 
1024 	wb->last_old_flush = jiffies;
1025 	nr_pages = get_nr_dirty_pages();
1026 
1027 	if (nr_pages) {
1028 		struct wb_writeback_work work = {
1029 			.nr_pages	= nr_pages,
1030 			.sync_mode	= WB_SYNC_NONE,
1031 			.for_kupdate	= 1,
1032 			.range_cyclic	= 1,
1033 			.reason		= WB_REASON_PERIODIC,
1034 		};
1035 
1036 		return wb_writeback(wb, &work);
1037 	}
1038 
1039 	return 0;
1040 }
1041 
1042 /*
1043  * Retrieve work items and do the writeback they describe
1044  */
wb_do_writeback(struct bdi_writeback * wb)1045 static long wb_do_writeback(struct bdi_writeback *wb)
1046 {
1047 	struct backing_dev_info *bdi = wb->bdi;
1048 	struct wb_writeback_work *work;
1049 	long wrote = 0;
1050 
1051 	set_bit(BDI_writeback_running, &wb->bdi->state);
1052 	while ((work = get_next_work_item(bdi)) != NULL) {
1053 
1054 		trace_writeback_exec(bdi, work);
1055 
1056 		wrote += wb_writeback(wb, work);
1057 
1058 		/*
1059 		 * Notify the caller of completion if this is a synchronous
1060 		 * work item, otherwise just free it.
1061 		 */
1062 		if (work->done)
1063 			complete(work->done);
1064 		else
1065 			kfree(work);
1066 	}
1067 
1068 	/*
1069 	 * Check for periodic writeback, kupdated() style
1070 	 */
1071 	wrote += wb_check_old_data_flush(wb);
1072 	wrote += wb_check_background_flush(wb);
1073 	clear_bit(BDI_writeback_running, &wb->bdi->state);
1074 
1075 	return wrote;
1076 }
1077 
1078 /*
1079  * Handle writeback of dirty data for the device backed by this bdi. Also
1080  * reschedules periodically and does kupdated style flushing.
1081  */
bdi_writeback_workfn(struct work_struct * work)1082 void bdi_writeback_workfn(struct work_struct *work)
1083 {
1084 	struct bdi_writeback *wb = container_of(to_delayed_work(work),
1085 						struct bdi_writeback, dwork);
1086 	struct backing_dev_info *bdi = wb->bdi;
1087 	long pages_written;
1088 
1089 	set_worker_desc("flush-%s", dev_name(bdi->dev));
1090 	current->flags |= PF_SWAPWRITE;
1091 
1092 	if (likely(!current_is_workqueue_rescuer() ||
1093 		   !test_bit(BDI_registered, &bdi->state))) {
1094 		/*
1095 		 * The normal path.  Keep writing back @bdi until its
1096 		 * work_list is empty.  Note that this path is also taken
1097 		 * if @bdi is shutting down even when we're running off the
1098 		 * rescuer as work_list needs to be drained.
1099 		 */
1100 		do {
1101 			pages_written = wb_do_writeback(wb);
1102 			trace_writeback_pages_written(pages_written);
1103 		} while (!list_empty(&bdi->work_list));
1104 	} else {
1105 		/*
1106 		 * bdi_wq can't get enough workers and we're running off
1107 		 * the emergency worker.  Don't hog it.  Hopefully, 1024 is
1108 		 * enough for efficient IO.
1109 		 */
1110 		pages_written = writeback_inodes_wb(&bdi->wb, 1024,
1111 						    WB_REASON_FORKER_THREAD);
1112 		trace_writeback_pages_written(pages_written);
1113 	}
1114 
1115 	if (!list_empty(&bdi->work_list))
1116 		mod_delayed_work(bdi_wq, &wb->dwork, 0);
1117 	else if (wb_has_dirty_io(wb) && dirty_writeback_interval)
1118 		bdi_wakeup_thread_delayed(bdi);
1119 
1120 	current->flags &= ~PF_SWAPWRITE;
1121 }
1122 
1123 /*
1124  * Start writeback of `nr_pages' pages.  If `nr_pages' is zero, write back
1125  * the whole world.
1126  */
wakeup_flusher_threads(long nr_pages,enum wb_reason reason)1127 void wakeup_flusher_threads(long nr_pages, enum wb_reason reason)
1128 {
1129 	struct backing_dev_info *bdi;
1130 
1131 	if (!nr_pages)
1132 		nr_pages = get_nr_dirty_pages();
1133 
1134 	rcu_read_lock();
1135 	list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) {
1136 		if (!bdi_has_dirty_io(bdi))
1137 			continue;
1138 		__bdi_start_writeback(bdi, nr_pages, false, reason);
1139 	}
1140 	rcu_read_unlock();
1141 }
1142 
block_dump___mark_inode_dirty(struct inode * inode)1143 static noinline void block_dump___mark_inode_dirty(struct inode *inode)
1144 {
1145 	if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) {
1146 		struct dentry *dentry;
1147 		const char *name = "?";
1148 
1149 		dentry = d_find_alias(inode);
1150 		if (dentry) {
1151 			spin_lock(&dentry->d_lock);
1152 			name = (const char *) dentry->d_name.name;
1153 		}
1154 		printk(KERN_DEBUG
1155 		       "%s(%d): dirtied inode %lu (%s) on %s\n",
1156 		       current->comm, task_pid_nr(current), inode->i_ino,
1157 		       name, inode->i_sb->s_id);
1158 		if (dentry) {
1159 			spin_unlock(&dentry->d_lock);
1160 			dput(dentry);
1161 		}
1162 	}
1163 }
1164 
1165 /**
1166  *	__mark_inode_dirty -	internal function
1167  *	@inode: inode to mark
1168  *	@flags: what kind of dirty (i.e. I_DIRTY_SYNC)
1169  *	Mark an inode as dirty. Callers should use mark_inode_dirty or
1170  *  	mark_inode_dirty_sync.
1171  *
1172  * Put the inode on the super block's dirty list.
1173  *
1174  * CAREFUL! We mark it dirty unconditionally, but move it onto the
1175  * dirty list only if it is hashed or if it refers to a blockdev.
1176  * If it was not hashed, it will never be added to the dirty list
1177  * even if it is later hashed, as it will have been marked dirty already.
1178  *
1179  * In short, make sure you hash any inodes _before_ you start marking
1180  * them dirty.
1181  *
1182  * Note that for blockdevs, inode->dirtied_when represents the dirtying time of
1183  * the block-special inode (/dev/hda1) itself.  And the ->dirtied_when field of
1184  * the kernel-internal blockdev inode represents the dirtying time of the
1185  * blockdev's pages.  This is why for I_DIRTY_PAGES we always use
1186  * page->mapping->host, so the page-dirtying time is recorded in the internal
1187  * blockdev inode.
1188  */
1189 #define I_DIRTY_INODE (I_DIRTY_SYNC | I_DIRTY_DATASYNC)
__mark_inode_dirty(struct inode * inode,int flags)1190 void __mark_inode_dirty(struct inode *inode, int flags)
1191 {
1192 	struct super_block *sb = inode->i_sb;
1193 	struct backing_dev_info *bdi = NULL;
1194 	int dirtytime;
1195 
1196 	trace_writeback_mark_inode_dirty(inode, flags);
1197 
1198 	/*
1199 	 * Don't do this for I_DIRTY_PAGES - that doesn't actually
1200 	 * dirty the inode itself
1201 	 */
1202 	if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC | I_DIRTY_TIME)) {
1203 		trace_writeback_dirty_inode_start(inode, flags);
1204 
1205 		if (sb->s_op->dirty_inode)
1206 			sb->s_op->dirty_inode(inode, flags);
1207 
1208 		trace_writeback_dirty_inode(inode, flags);
1209 	}
1210 	if (flags & I_DIRTY_INODE)
1211 		flags &= ~I_DIRTY_TIME;
1212 	dirtytime = flags & I_DIRTY_TIME;
1213 
1214 	/*
1215 	 * Paired with smp_mb() in __writeback_single_inode() for the
1216 	 * following lockless i_state test.  See there for details.
1217 	 */
1218 	smp_mb();
1219 
1220 	if (((inode->i_state & flags) == flags) ||
1221 	    (dirtytime && (inode->i_state & I_DIRTY_INODE)))
1222 		return;
1223 
1224 	if (unlikely(block_dump > 1))
1225 		block_dump___mark_inode_dirty(inode);
1226 
1227 	spin_lock(&inode->i_lock);
1228 	if (dirtytime && (inode->i_state & I_DIRTY_INODE))
1229 		goto out_unlock_inode;
1230 	if ((inode->i_state & flags) != flags) {
1231 		const int was_dirty = inode->i_state & I_DIRTY;
1232 
1233 		if (flags & I_DIRTY_INODE)
1234 			inode->i_state &= ~I_DIRTY_TIME;
1235 		inode->i_state |= flags;
1236 
1237 		/*
1238 		 * If the inode is being synced, just update its dirty state.
1239 		 * The unlocker will place the inode on the appropriate
1240 		 * superblock list, based upon its state.
1241 		 */
1242 		if (inode->i_state & I_SYNC)
1243 			goto out_unlock_inode;
1244 
1245 		/*
1246 		 * Only add valid (hashed) inodes to the superblock's
1247 		 * dirty list.  Add blockdev inodes as well.
1248 		 */
1249 		if (!S_ISBLK(inode->i_mode)) {
1250 			if (inode_unhashed(inode))
1251 				goto out_unlock_inode;
1252 		}
1253 		if (inode->i_state & I_FREEING)
1254 			goto out_unlock_inode;
1255 
1256 		/*
1257 		 * If the inode was already on b_dirty/b_io/b_more_io, don't
1258 		 * reposition it (that would break b_dirty time-ordering).
1259 		 */
1260 		if (!was_dirty) {
1261 			bool wakeup_bdi = false;
1262 			bdi = inode_to_bdi(inode);
1263 
1264 			spin_unlock(&inode->i_lock);
1265 			spin_lock(&bdi->wb.list_lock);
1266 			if (bdi_cap_writeback_dirty(bdi)) {
1267 				WARN(!test_bit(BDI_registered, &bdi->state),
1268 				     "bdi-%s not registered\n", bdi->name);
1269 
1270 				/*
1271 				 * If this is the first dirty inode for this
1272 				 * bdi, we have to wake-up the corresponding
1273 				 * bdi thread to make sure background
1274 				 * write-back happens later.
1275 				 */
1276 				if (!wb_has_dirty_io(&bdi->wb))
1277 					wakeup_bdi = true;
1278 			}
1279 
1280 			inode->dirtied_when = jiffies;
1281 			list_move(&inode->i_wb_list, dirtytime ?
1282 				  &bdi->wb.b_dirty_time : &bdi->wb.b_dirty);
1283 			spin_unlock(&bdi->wb.list_lock);
1284 			trace_writeback_dirty_inode_enqueue(inode);
1285 
1286 			if (wakeup_bdi)
1287 				bdi_wakeup_thread_delayed(bdi);
1288 			return;
1289 		}
1290 	}
1291 out_unlock_inode:
1292 	spin_unlock(&inode->i_lock);
1293 
1294 }
1295 EXPORT_SYMBOL(__mark_inode_dirty);
1296 
wait_sb_inodes(struct super_block * sb)1297 static void wait_sb_inodes(struct super_block *sb)
1298 {
1299 	struct inode *inode, *old_inode = NULL;
1300 
1301 	/*
1302 	 * We need to be protected against the filesystem going from
1303 	 * r/o to r/w or vice versa.
1304 	 */
1305 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
1306 
1307 	spin_lock(&inode_sb_list_lock);
1308 
1309 	/*
1310 	 * Data integrity sync. Must wait for all pages under writeback,
1311 	 * because there may have been pages dirtied before our sync
1312 	 * call, but which had writeout started before we write it out.
1313 	 * In which case, the inode may not be on the dirty list, but
1314 	 * we still have to wait for that writeout.
1315 	 */
1316 	list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
1317 		struct address_space *mapping = inode->i_mapping;
1318 
1319 		spin_lock(&inode->i_lock);
1320 		if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
1321 		    (mapping->nrpages == 0)) {
1322 			spin_unlock(&inode->i_lock);
1323 			continue;
1324 		}
1325 		__iget(inode);
1326 		spin_unlock(&inode->i_lock);
1327 		spin_unlock(&inode_sb_list_lock);
1328 
1329 		/*
1330 		 * We hold a reference to 'inode' so it couldn't have been
1331 		 * removed from s_inodes list while we dropped the
1332 		 * inode_sb_list_lock.  We cannot iput the inode now as we can
1333 		 * be holding the last reference and we cannot iput it under
1334 		 * inode_sb_list_lock. So we keep the reference and iput it
1335 		 * later.
1336 		 */
1337 		iput(old_inode);
1338 		old_inode = inode;
1339 
1340 		filemap_fdatawait(mapping);
1341 
1342 		cond_resched();
1343 
1344 		spin_lock(&inode_sb_list_lock);
1345 	}
1346 	spin_unlock(&inode_sb_list_lock);
1347 	iput(old_inode);
1348 }
1349 
1350 /**
1351  * writeback_inodes_sb_nr -	writeback dirty inodes from given super_block
1352  * @sb: the superblock
1353  * @nr: the number of pages to write
1354  * @reason: reason why some writeback work initiated
1355  *
1356  * Start writeback on some inodes on this super_block. No guarantees are made
1357  * on how many (if any) will be written, and this function does not wait
1358  * for IO completion of submitted IO.
1359  */
writeback_inodes_sb_nr(struct super_block * sb,unsigned long nr,enum wb_reason reason)1360 void writeback_inodes_sb_nr(struct super_block *sb,
1361 			    unsigned long nr,
1362 			    enum wb_reason reason)
1363 {
1364 	DECLARE_COMPLETION_ONSTACK(done);
1365 	struct wb_writeback_work work = {
1366 		.sb			= sb,
1367 		.sync_mode		= WB_SYNC_NONE,
1368 		.tagged_writepages	= 1,
1369 		.done			= &done,
1370 		.nr_pages		= nr,
1371 		.reason			= reason,
1372 	};
1373 
1374 	if (sb->s_bdi == &noop_backing_dev_info)
1375 		return;
1376 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
1377 	bdi_queue_work(sb->s_bdi, &work);
1378 	wait_for_completion(&done);
1379 }
1380 EXPORT_SYMBOL(writeback_inodes_sb_nr);
1381 
1382 /**
1383  * writeback_inodes_sb	-	writeback dirty inodes from given super_block
1384  * @sb: the superblock
1385  * @reason: reason why some writeback work was initiated
1386  *
1387  * Start writeback on some inodes on this super_block. No guarantees are made
1388  * on how many (if any) will be written, and this function does not wait
1389  * for IO completion of submitted IO.
1390  */
writeback_inodes_sb(struct super_block * sb,enum wb_reason reason)1391 void writeback_inodes_sb(struct super_block *sb, enum wb_reason reason)
1392 {
1393 	return writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason);
1394 }
1395 EXPORT_SYMBOL(writeback_inodes_sb);
1396 
1397 /**
1398  * try_to_writeback_inodes_sb_nr - try to start writeback if none underway
1399  * @sb: the superblock
1400  * @nr: the number of pages to write
1401  * @reason: the reason of writeback
1402  *
1403  * Invoke writeback_inodes_sb_nr if no writeback is currently underway.
1404  * Returns 1 if writeback was started, 0 if not.
1405  */
try_to_writeback_inodes_sb_nr(struct super_block * sb,unsigned long nr,enum wb_reason reason)1406 int try_to_writeback_inodes_sb_nr(struct super_block *sb,
1407 				  unsigned long nr,
1408 				  enum wb_reason reason)
1409 {
1410 	if (writeback_in_progress(sb->s_bdi))
1411 		return 1;
1412 
1413 	if (!down_read_trylock(&sb->s_umount))
1414 		return 0;
1415 
1416 	writeback_inodes_sb_nr(sb, nr, reason);
1417 	up_read(&sb->s_umount);
1418 	return 1;
1419 }
1420 EXPORT_SYMBOL(try_to_writeback_inodes_sb_nr);
1421 
1422 /**
1423  * try_to_writeback_inodes_sb - try to start writeback if none underway
1424  * @sb: the superblock
1425  * @reason: reason why some writeback work was initiated
1426  *
1427  * Implement by try_to_writeback_inodes_sb_nr()
1428  * Returns 1 if writeback was started, 0 if not.
1429  */
try_to_writeback_inodes_sb(struct super_block * sb,enum wb_reason reason)1430 int try_to_writeback_inodes_sb(struct super_block *sb, enum wb_reason reason)
1431 {
1432 	return try_to_writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason);
1433 }
1434 EXPORT_SYMBOL(try_to_writeback_inodes_sb);
1435 
1436 /**
1437  * sync_inodes_sb	-	sync sb inode pages
1438  * @sb: the superblock
1439  *
1440  * This function writes and waits on any dirty inode belonging to this
1441  * super_block.
1442  */
sync_inodes_sb(struct super_block * sb)1443 void sync_inodes_sb(struct super_block *sb)
1444 {
1445 	DECLARE_COMPLETION_ONSTACK(done);
1446 	struct wb_writeback_work work = {
1447 		.sb		= sb,
1448 		.sync_mode	= WB_SYNC_ALL,
1449 		.nr_pages	= LONG_MAX,
1450 		.range_cyclic	= 0,
1451 		.done		= &done,
1452 		.reason		= WB_REASON_SYNC,
1453 		.for_sync	= 1,
1454 	};
1455 
1456 	/* Nothing to do? */
1457 	if (sb->s_bdi == &noop_backing_dev_info)
1458 		return;
1459 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
1460 
1461 	bdi_queue_work(sb->s_bdi, &work);
1462 	wait_for_completion(&done);
1463 
1464 	wait_sb_inodes(sb);
1465 }
1466 EXPORT_SYMBOL(sync_inodes_sb);
1467 
1468 /**
1469  * write_inode_now	-	write an inode to disk
1470  * @inode: inode to write to disk
1471  * @sync: whether the write should be synchronous or not
1472  *
1473  * This function commits an inode to disk immediately if it is dirty. This is
1474  * primarily needed by knfsd.
1475  *
1476  * The caller must either have a ref on the inode or must have set I_WILL_FREE.
1477  */
write_inode_now(struct inode * inode,int sync)1478 int write_inode_now(struct inode *inode, int sync)
1479 {
1480 	struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
1481 	struct writeback_control wbc = {
1482 		.nr_to_write = LONG_MAX,
1483 		.sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE,
1484 		.range_start = 0,
1485 		.range_end = LLONG_MAX,
1486 	};
1487 
1488 	if (!mapping_cap_writeback_dirty(inode->i_mapping))
1489 		wbc.nr_to_write = 0;
1490 
1491 	might_sleep();
1492 	return writeback_single_inode(inode, wb, &wbc);
1493 }
1494 EXPORT_SYMBOL(write_inode_now);
1495 
1496 /**
1497  * sync_inode - write an inode and its pages to disk.
1498  * @inode: the inode to sync
1499  * @wbc: controls the writeback mode
1500  *
1501  * sync_inode() will write an inode and its pages to disk.  It will also
1502  * correctly update the inode on its superblock's dirty inode lists and will
1503  * update inode->i_state.
1504  *
1505  * The caller must have a ref on the inode.
1506  */
sync_inode(struct inode * inode,struct writeback_control * wbc)1507 int sync_inode(struct inode *inode, struct writeback_control *wbc)
1508 {
1509 	return writeback_single_inode(inode, &inode_to_bdi(inode)->wb, wbc);
1510 }
1511 EXPORT_SYMBOL(sync_inode);
1512 
1513 /**
1514  * sync_inode_metadata - write an inode to disk
1515  * @inode: the inode to sync
1516  * @wait: wait for I/O to complete.
1517  *
1518  * Write an inode to disk and adjust its dirty state after completion.
1519  *
1520  * Note: only writes the actual inode, no associated data or other metadata.
1521  */
sync_inode_metadata(struct inode * inode,int wait)1522 int sync_inode_metadata(struct inode *inode, int wait)
1523 {
1524 	struct writeback_control wbc = {
1525 		.sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_NONE,
1526 		.nr_to_write = 0, /* metadata-only */
1527 	};
1528 
1529 	return sync_inode(inode, &wbc);
1530 }
1531 EXPORT_SYMBOL(sync_inode_metadata);
1532