PageRenderTime 31ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/fs-writeback.c

https://github.com/mstsirkin/linux
C | 1383 lines | 749 code | 158 blank | 476 comment | 119 complexity | bf4290f14439c089cfecb1ea7d77f9ca MD5 | raw file
  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. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/slab.h>
  19. #include <linux/sched.h>
  20. #include <linux/fs.h>
  21. #include <linux/mm.h>
  22. #include <linux/kthread.h>
  23. #include <linux/freezer.h>
  24. #include <linux/writeback.h>
  25. #include <linux/blkdev.h>
  26. #include <linux/backing-dev.h>
  27. #include <linux/buffer_head.h>
  28. #include <linux/tracepoint.h>
  29. #include "internal.h"
  30. /*
  31. * Passed into wb_writeback(), essentially a subset of writeback_control
  32. */
  33. struct wb_writeback_work {
  34. long nr_pages;
  35. struct super_block *sb;
  36. unsigned long *older_than_this;
  37. enum writeback_sync_modes sync_mode;
  38. unsigned int tagged_writepages:1;
  39. unsigned int for_kupdate:1;
  40. unsigned int range_cyclic:1;
  41. unsigned int for_background:1;
  42. struct list_head list; /* pending work list */
  43. struct completion *done; /* set if the caller waits */
  44. };
  45. /*
  46. * Include the creation of the trace points after defining the
  47. * wb_writeback_work structure so that the definition remains local to this
  48. * file.
  49. */
  50. #define CREATE_TRACE_POINTS
  51. #include <trace/events/writeback.h>
  52. /*
  53. * We don't actually have pdflush, but this one is exported though /proc...
  54. */
  55. int nr_pdflush_threads;
  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. */
  63. int writeback_in_progress(struct backing_dev_info *bdi)
  64. {
  65. return test_bit(BDI_writeback_running, &bdi->state);
  66. }
  67. static inline struct backing_dev_info *inode_to_bdi(struct inode *inode)
  68. {
  69. struct super_block *sb = inode->i_sb;
  70. if (strcmp(sb->s_type->name, "bdev") == 0)
  71. return inode->i_mapping->backing_dev_info;
  72. return sb->s_bdi;
  73. }
  74. static inline struct inode *wb_inode(struct list_head *head)
  75. {
  76. return list_entry(head, struct inode, i_wb_list);
  77. }
  78. /* Wakeup flusher thread or forker thread to fork it. Requires bdi->wb_lock. */
  79. static void bdi_wakeup_flusher(struct backing_dev_info *bdi)
  80. {
  81. if (bdi->wb.task) {
  82. wake_up_process(bdi->wb.task);
  83. } else {
  84. /*
  85. * The bdi thread isn't there, wake up the forker thread which
  86. * will create and run it.
  87. */
  88. wake_up_process(default_backing_dev_info.wb.task);
  89. }
  90. }
  91. static void bdi_queue_work(struct backing_dev_info *bdi,
  92. struct wb_writeback_work *work)
  93. {
  94. trace_writeback_queue(bdi, work);
  95. spin_lock_bh(&bdi->wb_lock);
  96. list_add_tail(&work->list, &bdi->work_list);
  97. if (!bdi->wb.task)
  98. trace_writeback_nothread(bdi, work);
  99. bdi_wakeup_flusher(bdi);
  100. spin_unlock_bh(&bdi->wb_lock);
  101. }
  102. static void
  103. __bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages,
  104. bool range_cyclic)
  105. {
  106. struct wb_writeback_work *work;
  107. /*
  108. * This is WB_SYNC_NONE writeback, so if allocation fails just
  109. * wakeup the thread for old dirty data writeback
  110. */
  111. work = kzalloc(sizeof(*work), GFP_ATOMIC);
  112. if (!work) {
  113. if (bdi->wb.task) {
  114. trace_writeback_nowork(bdi);
  115. wake_up_process(bdi->wb.task);
  116. }
  117. return;
  118. }
  119. work->sync_mode = WB_SYNC_NONE;
  120. work->nr_pages = nr_pages;
  121. work->range_cyclic = range_cyclic;
  122. bdi_queue_work(bdi, work);
  123. }
  124. /**
  125. * bdi_start_writeback - start writeback
  126. * @bdi: the backing device to write from
  127. * @nr_pages: the number of pages to write
  128. *
  129. * Description:
  130. * This does WB_SYNC_NONE opportunistic writeback. The IO is only
  131. * started when this function returns, we make no guarantees on
  132. * completion. Caller need not hold sb s_umount semaphore.
  133. *
  134. */
  135. void bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages)
  136. {
  137. __bdi_start_writeback(bdi, nr_pages, true);
  138. }
  139. /**
  140. * bdi_start_background_writeback - start background writeback
  141. * @bdi: the backing device to write from
  142. *
  143. * Description:
  144. * This makes sure WB_SYNC_NONE background writeback happens. When
  145. * this function returns, it is only guaranteed that for given BDI
  146. * some IO is happening if we are over background dirty threshold.
  147. * Caller need not hold sb s_umount semaphore.
  148. */
  149. void bdi_start_background_writeback(struct backing_dev_info *bdi)
  150. {
  151. /*
  152. * We just wake up the flusher thread. It will perform background
  153. * writeback as soon as there is no other work to do.
  154. */
  155. trace_writeback_wake_background(bdi);
  156. spin_lock_bh(&bdi->wb_lock);
  157. bdi_wakeup_flusher(bdi);
  158. spin_unlock_bh(&bdi->wb_lock);
  159. }
  160. /*
  161. * Remove the inode from the writeback list it is on.
  162. */
  163. void inode_wb_list_del(struct inode *inode)
  164. {
  165. struct backing_dev_info *bdi = inode_to_bdi(inode);
  166. spin_lock(&bdi->wb.list_lock);
  167. list_del_init(&inode->i_wb_list);
  168. spin_unlock(&bdi->wb.list_lock);
  169. }
  170. /*
  171. * Redirty an inode: set its when-it-was dirtied timestamp and move it to the
  172. * furthest end of its superblock's dirty-inode list.
  173. *
  174. * Before stamping the inode's ->dirtied_when, we check to see whether it is
  175. * already the most-recently-dirtied inode on the b_dirty list. If that is
  176. * the case then the inode must have been redirtied while it was being written
  177. * out and we don't reset its dirtied_when.
  178. */
  179. static void redirty_tail(struct inode *inode, struct bdi_writeback *wb)
  180. {
  181. assert_spin_locked(&wb->list_lock);
  182. if (!list_empty(&wb->b_dirty)) {
  183. struct inode *tail;
  184. tail = wb_inode(wb->b_dirty.next);
  185. if (time_before(inode->dirtied_when, tail->dirtied_when))
  186. inode->dirtied_when = jiffies;
  187. }
  188. list_move(&inode->i_wb_list, &wb->b_dirty);
  189. }
  190. /*
  191. * requeue inode for re-scanning after bdi->b_io list is exhausted.
  192. */
  193. static void requeue_io(struct inode *inode, struct bdi_writeback *wb)
  194. {
  195. assert_spin_locked(&wb->list_lock);
  196. list_move(&inode->i_wb_list, &wb->b_more_io);
  197. }
  198. static void inode_sync_complete(struct inode *inode)
  199. {
  200. /*
  201. * Prevent speculative execution through
  202. * spin_unlock(&wb->list_lock);
  203. */
  204. smp_mb();
  205. wake_up_bit(&inode->i_state, __I_SYNC);
  206. }
  207. static bool inode_dirtied_after(struct inode *inode, unsigned long t)
  208. {
  209. bool ret = time_after(inode->dirtied_when, t);
  210. #ifndef CONFIG_64BIT
  211. /*
  212. * For inodes being constantly redirtied, dirtied_when can get stuck.
  213. * It _appears_ to be in the future, but is actually in distant past.
  214. * This test is necessary to prevent such wrapped-around relative times
  215. * from permanently stopping the whole bdi writeback.
  216. */
  217. ret = ret && time_before_eq(inode->dirtied_when, jiffies);
  218. #endif
  219. return ret;
  220. }
  221. /*
  222. * Move expired dirty inodes from @delaying_queue to @dispatch_queue.
  223. */
  224. static int move_expired_inodes(struct list_head *delaying_queue,
  225. struct list_head *dispatch_queue,
  226. unsigned long *older_than_this)
  227. {
  228. LIST_HEAD(tmp);
  229. struct list_head *pos, *node;
  230. struct super_block *sb = NULL;
  231. struct inode *inode;
  232. int do_sb_sort = 0;
  233. int moved = 0;
  234. while (!list_empty(delaying_queue)) {
  235. inode = wb_inode(delaying_queue->prev);
  236. if (older_than_this &&
  237. inode_dirtied_after(inode, *older_than_this))
  238. break;
  239. if (sb && sb != inode->i_sb)
  240. do_sb_sort = 1;
  241. sb = inode->i_sb;
  242. list_move(&inode->i_wb_list, &tmp);
  243. moved++;
  244. }
  245. /* just one sb in list, splice to dispatch_queue and we're done */
  246. if (!do_sb_sort) {
  247. list_splice(&tmp, dispatch_queue);
  248. goto out;
  249. }
  250. /* Move inodes from one superblock together */
  251. while (!list_empty(&tmp)) {
  252. sb = wb_inode(tmp.prev)->i_sb;
  253. list_for_each_prev_safe(pos, node, &tmp) {
  254. inode = wb_inode(pos);
  255. if (inode->i_sb == sb)
  256. list_move(&inode->i_wb_list, dispatch_queue);
  257. }
  258. }
  259. out:
  260. return moved;
  261. }
  262. /*
  263. * Queue all expired dirty inodes for io, eldest first.
  264. * Before
  265. * newly dirtied b_dirty b_io b_more_io
  266. * =============> gf edc BA
  267. * After
  268. * newly dirtied b_dirty b_io b_more_io
  269. * =============> g fBAedc
  270. * |
  271. * +--> dequeue for IO
  272. */
  273. static void queue_io(struct bdi_writeback *wb, unsigned long *older_than_this)
  274. {
  275. int moved;
  276. assert_spin_locked(&wb->list_lock);
  277. list_splice_init(&wb->b_more_io, &wb->b_io);
  278. moved = move_expired_inodes(&wb->b_dirty, &wb->b_io, older_than_this);
  279. trace_writeback_queue_io(wb, older_than_this, moved);
  280. }
  281. static int write_inode(struct inode *inode, struct writeback_control *wbc)
  282. {
  283. if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode))
  284. return inode->i_sb->s_op->write_inode(inode, wbc);
  285. return 0;
  286. }
  287. /*
  288. * Wait for writeback on an inode to complete.
  289. */
  290. static void inode_wait_for_writeback(struct inode *inode,
  291. struct bdi_writeback *wb)
  292. {
  293. DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC);
  294. wait_queue_head_t *wqh;
  295. wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
  296. while (inode->i_state & I_SYNC) {
  297. spin_unlock(&inode->i_lock);
  298. spin_unlock(&wb->list_lock);
  299. __wait_on_bit(wqh, &wq, inode_wait, TASK_UNINTERRUPTIBLE);
  300. spin_lock(&wb->list_lock);
  301. spin_lock(&inode->i_lock);
  302. }
  303. }
  304. /*
  305. * Write out an inode's dirty pages. Called under wb->list_lock and
  306. * inode->i_lock. Either the caller has an active reference on the inode or
  307. * the inode has I_WILL_FREE set.
  308. *
  309. * If `wait' is set, wait on the writeout.
  310. *
  311. * The whole writeout design is quite complex and fragile. We want to avoid
  312. * starvation of particular inodes when others are being redirtied, prevent
  313. * livelocks, etc.
  314. */
  315. static int
  316. writeback_single_inode(struct inode *inode, struct bdi_writeback *wb,
  317. struct writeback_control *wbc)
  318. {
  319. struct address_space *mapping = inode->i_mapping;
  320. long nr_to_write = wbc->nr_to_write;
  321. unsigned dirty;
  322. int ret;
  323. assert_spin_locked(&wb->list_lock);
  324. assert_spin_locked(&inode->i_lock);
  325. if (!atomic_read(&inode->i_count))
  326. WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING)));
  327. else
  328. WARN_ON(inode->i_state & I_WILL_FREE);
  329. if (inode->i_state & I_SYNC) {
  330. /*
  331. * If this inode is locked for writeback and we are not doing
  332. * writeback-for-data-integrity, move it to b_more_io so that
  333. * writeback can proceed with the other inodes on s_io.
  334. *
  335. * We'll have another go at writing back this inode when we
  336. * completed a full scan of b_io.
  337. */
  338. if (wbc->sync_mode != WB_SYNC_ALL) {
  339. requeue_io(inode, wb);
  340. trace_writeback_single_inode_requeue(inode, wbc,
  341. nr_to_write);
  342. return 0;
  343. }
  344. /*
  345. * It's a data-integrity sync. We must wait.
  346. */
  347. inode_wait_for_writeback(inode, wb);
  348. }
  349. BUG_ON(inode->i_state & I_SYNC);
  350. /* Set I_SYNC, reset I_DIRTY_PAGES */
  351. inode->i_state |= I_SYNC;
  352. inode->i_state &= ~I_DIRTY_PAGES;
  353. spin_unlock(&inode->i_lock);
  354. spin_unlock(&wb->list_lock);
  355. ret = do_writepages(mapping, wbc);
  356. /*
  357. * Make sure to wait on the data before writing out the metadata.
  358. * This is important for filesystems that modify metadata on data
  359. * I/O completion.
  360. */
  361. if (wbc->sync_mode == WB_SYNC_ALL) {
  362. int err = filemap_fdatawait(mapping);
  363. if (ret == 0)
  364. ret = err;
  365. }
  366. /*
  367. * Some filesystems may redirty the inode during the writeback
  368. * due to delalloc, clear dirty metadata flags right before
  369. * write_inode()
  370. */
  371. spin_lock(&inode->i_lock);
  372. dirty = inode->i_state & I_DIRTY;
  373. inode->i_state &= ~(I_DIRTY_SYNC | I_DIRTY_DATASYNC);
  374. spin_unlock(&inode->i_lock);
  375. /* Don't write the inode if only I_DIRTY_PAGES was set */
  376. if (dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
  377. int err = write_inode(inode, wbc);
  378. if (ret == 0)
  379. ret = err;
  380. }
  381. spin_lock(&wb->list_lock);
  382. spin_lock(&inode->i_lock);
  383. inode->i_state &= ~I_SYNC;
  384. if (!(inode->i_state & I_FREEING)) {
  385. /*
  386. * Sync livelock prevention. Each inode is tagged and synced in
  387. * one shot. If still dirty, it will be redirty_tail()'ed below.
  388. * Update the dirty time to prevent enqueue and sync it again.
  389. */
  390. if ((inode->i_state & I_DIRTY) &&
  391. (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages))
  392. inode->dirtied_when = jiffies;
  393. if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
  394. /*
  395. * We didn't write back all the pages. nfs_writepages()
  396. * sometimes bales out without doing anything.
  397. */
  398. inode->i_state |= I_DIRTY_PAGES;
  399. if (wbc->nr_to_write <= 0) {
  400. /*
  401. * slice used up: queue for next turn
  402. */
  403. requeue_io(inode, wb);
  404. } else {
  405. /*
  406. * Writeback blocked by something other than
  407. * congestion. Delay the inode for some time to
  408. * avoid spinning on the CPU (100% iowait)
  409. * retrying writeback of the dirty page/inode
  410. * that cannot be performed immediately.
  411. */
  412. redirty_tail(inode, wb);
  413. }
  414. } else if (inode->i_state & I_DIRTY) {
  415. /*
  416. * Filesystems can dirty the inode during writeback
  417. * operations, such as delayed allocation during
  418. * submission or metadata updates after data IO
  419. * completion.
  420. */
  421. redirty_tail(inode, wb);
  422. } else {
  423. /*
  424. * The inode is clean. At this point we either have
  425. * a reference to the inode or it's on it's way out.
  426. * No need to add it back to the LRU.
  427. */
  428. list_del_init(&inode->i_wb_list);
  429. }
  430. }
  431. inode_sync_complete(inode);
  432. trace_writeback_single_inode(inode, wbc, nr_to_write);
  433. return ret;
  434. }
  435. static long writeback_chunk_size(struct backing_dev_info *bdi,
  436. struct wb_writeback_work *work)
  437. {
  438. long pages;
  439. /*
  440. * WB_SYNC_ALL mode does livelock avoidance by syncing dirty
  441. * inodes/pages in one big loop. Setting wbc.nr_to_write=LONG_MAX
  442. * here avoids calling into writeback_inodes_wb() more than once.
  443. *
  444. * The intended call sequence for WB_SYNC_ALL writeback is:
  445. *
  446. * wb_writeback()
  447. * writeback_sb_inodes() <== called only once
  448. * write_cache_pages() <== called once for each inode
  449. * (quickly) tag currently dirty pages
  450. * (maybe slowly) sync all tagged pages
  451. */
  452. if (work->sync_mode == WB_SYNC_ALL || work->tagged_writepages)
  453. pages = LONG_MAX;
  454. else {
  455. pages = min(bdi->avg_write_bandwidth / 2,
  456. global_dirty_limit / DIRTY_SCOPE);
  457. pages = min(pages, work->nr_pages);
  458. pages = round_down(pages + MIN_WRITEBACK_PAGES,
  459. MIN_WRITEBACK_PAGES);
  460. }
  461. return pages;
  462. }
  463. /*
  464. * Write a portion of b_io inodes which belong to @sb.
  465. *
  466. * If @only_this_sb is true, then find and write all such
  467. * inodes. Otherwise write only ones which go sequentially
  468. * in reverse order.
  469. *
  470. * Return the number of pages and/or inodes written.
  471. */
  472. static long writeback_sb_inodes(struct super_block *sb,
  473. struct bdi_writeback *wb,
  474. struct wb_writeback_work *work)
  475. {
  476. struct writeback_control wbc = {
  477. .sync_mode = work->sync_mode,
  478. .tagged_writepages = work->tagged_writepages,
  479. .for_kupdate = work->for_kupdate,
  480. .for_background = work->for_background,
  481. .range_cyclic = work->range_cyclic,
  482. .range_start = 0,
  483. .range_end = LLONG_MAX,
  484. };
  485. unsigned long start_time = jiffies;
  486. long write_chunk;
  487. long wrote = 0; /* count both pages and inodes */
  488. while (!list_empty(&wb->b_io)) {
  489. struct inode *inode = wb_inode(wb->b_io.prev);
  490. if (inode->i_sb != sb) {
  491. if (work->sb) {
  492. /*
  493. * We only want to write back data for this
  494. * superblock, move all inodes not belonging
  495. * to it back onto the dirty list.
  496. */
  497. redirty_tail(inode, wb);
  498. continue;
  499. }
  500. /*
  501. * The inode belongs to a different superblock.
  502. * Bounce back to the caller to unpin this and
  503. * pin the next superblock.
  504. */
  505. break;
  506. }
  507. /*
  508. * Don't bother with new inodes or inodes beeing freed, first
  509. * kind does not need peridic writeout yet, and for the latter
  510. * kind writeout is handled by the freer.
  511. */
  512. spin_lock(&inode->i_lock);
  513. if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
  514. spin_unlock(&inode->i_lock);
  515. redirty_tail(inode, wb);
  516. continue;
  517. }
  518. __iget(inode);
  519. write_chunk = writeback_chunk_size(wb->bdi, work);
  520. wbc.nr_to_write = write_chunk;
  521. wbc.pages_skipped = 0;
  522. writeback_single_inode(inode, wb, &wbc);
  523. work->nr_pages -= write_chunk - wbc.nr_to_write;
  524. wrote += write_chunk - wbc.nr_to_write;
  525. if (!(inode->i_state & I_DIRTY))
  526. wrote++;
  527. if (wbc.pages_skipped) {
  528. /*
  529. * writeback is not making progress due to locked
  530. * buffers. Skip this inode for now.
  531. */
  532. redirty_tail(inode, wb);
  533. }
  534. spin_unlock(&inode->i_lock);
  535. spin_unlock(&wb->list_lock);
  536. iput(inode);
  537. cond_resched();
  538. spin_lock(&wb->list_lock);
  539. /*
  540. * bail out to wb_writeback() often enough to check
  541. * background threshold and other termination conditions.
  542. */
  543. if (wrote) {
  544. if (time_is_before_jiffies(start_time + HZ / 10UL))
  545. break;
  546. if (work->nr_pages <= 0)
  547. break;
  548. }
  549. }
  550. return wrote;
  551. }
  552. static long __writeback_inodes_wb(struct bdi_writeback *wb,
  553. struct wb_writeback_work *work)
  554. {
  555. unsigned long start_time = jiffies;
  556. long wrote = 0;
  557. while (!list_empty(&wb->b_io)) {
  558. struct inode *inode = wb_inode(wb->b_io.prev);
  559. struct super_block *sb = inode->i_sb;
  560. if (!grab_super_passive(sb)) {
  561. /*
  562. * grab_super_passive() may fail consistently due to
  563. * s_umount being grabbed by someone else. Don't use
  564. * requeue_io() to avoid busy retrying the inode/sb.
  565. */
  566. redirty_tail(inode, wb);
  567. continue;
  568. }
  569. wrote += writeback_sb_inodes(sb, wb, work);
  570. drop_super(sb);
  571. /* refer to the same tests at the end of writeback_sb_inodes */
  572. if (wrote) {
  573. if (time_is_before_jiffies(start_time + HZ / 10UL))
  574. break;
  575. if (work->nr_pages <= 0)
  576. break;
  577. }
  578. }
  579. /* Leave any unwritten inodes on b_io */
  580. return wrote;
  581. }
  582. long writeback_inodes_wb(struct bdi_writeback *wb, long nr_pages)
  583. {
  584. struct wb_writeback_work work = {
  585. .nr_pages = nr_pages,
  586. .sync_mode = WB_SYNC_NONE,
  587. .range_cyclic = 1,
  588. };
  589. spin_lock(&wb->list_lock);
  590. if (list_empty(&wb->b_io))
  591. queue_io(wb, NULL);
  592. __writeback_inodes_wb(wb, &work);
  593. spin_unlock(&wb->list_lock);
  594. return nr_pages - work.nr_pages;
  595. }
  596. static inline bool over_bground_thresh(void)
  597. {
  598. unsigned long background_thresh, dirty_thresh;
  599. global_dirty_limits(&background_thresh, &dirty_thresh);
  600. return (global_page_state(NR_FILE_DIRTY) +
  601. global_page_state(NR_UNSTABLE_NFS) > background_thresh);
  602. }
  603. /*
  604. * Called under wb->list_lock. If there are multiple wb per bdi,
  605. * only the flusher working on the first wb should do it.
  606. */
  607. static void wb_update_bandwidth(struct bdi_writeback *wb,
  608. unsigned long start_time)
  609. {
  610. __bdi_update_bandwidth(wb->bdi, 0, 0, 0, 0, start_time);
  611. }
  612. /*
  613. * Explicit flushing or periodic writeback of "old" data.
  614. *
  615. * Define "old": the first time one of an inode's pages is dirtied, we mark the
  616. * dirtying-time in the inode's address_space. So this periodic writeback code
  617. * just walks the superblock inode list, writing back any inodes which are
  618. * older than a specific point in time.
  619. *
  620. * Try to run once per dirty_writeback_interval. But if a writeback event
  621. * takes longer than a dirty_writeback_interval interval, then leave a
  622. * one-second gap.
  623. *
  624. * older_than_this takes precedence over nr_to_write. So we'll only write back
  625. * all dirty pages if they are all attached to "old" mappings.
  626. */
  627. static long wb_writeback(struct bdi_writeback *wb,
  628. struct wb_writeback_work *work)
  629. {
  630. unsigned long wb_start = jiffies;
  631. long nr_pages = work->nr_pages;
  632. unsigned long oldest_jif;
  633. struct inode *inode;
  634. long progress;
  635. oldest_jif = jiffies;
  636. work->older_than_this = &oldest_jif;
  637. spin_lock(&wb->list_lock);
  638. for (;;) {
  639. /*
  640. * Stop writeback when nr_pages has been consumed
  641. */
  642. if (work->nr_pages <= 0)
  643. break;
  644. /*
  645. * Background writeout and kupdate-style writeback may
  646. * run forever. Stop them if there is other work to do
  647. * so that e.g. sync can proceed. They'll be restarted
  648. * after the other works are all done.
  649. */
  650. if ((work->for_background || work->for_kupdate) &&
  651. !list_empty(&wb->bdi->work_list))
  652. break;
  653. /*
  654. * For background writeout, stop when we are below the
  655. * background dirty threshold
  656. */
  657. if (work->for_background && !over_bground_thresh())
  658. break;
  659. if (work->for_kupdate) {
  660. oldest_jif = jiffies -
  661. msecs_to_jiffies(dirty_expire_interval * 10);
  662. work->older_than_this = &oldest_jif;
  663. }
  664. trace_writeback_start(wb->bdi, work);
  665. if (list_empty(&wb->b_io))
  666. queue_io(wb, work->older_than_this);
  667. if (work->sb)
  668. progress = writeback_sb_inodes(work->sb, wb, work);
  669. else
  670. progress = __writeback_inodes_wb(wb, work);
  671. trace_writeback_written(wb->bdi, work);
  672. wb_update_bandwidth(wb, wb_start);
  673. /*
  674. * Did we write something? Try for more
  675. *
  676. * Dirty inodes are moved to b_io for writeback in batches.
  677. * The completion of the current batch does not necessarily
  678. * mean the overall work is done. So we keep looping as long
  679. * as made some progress on cleaning pages or inodes.
  680. */
  681. if (progress)
  682. continue;
  683. /*
  684. * No more inodes for IO, bail
  685. */
  686. if (list_empty(&wb->b_more_io))
  687. break;
  688. /*
  689. * Nothing written. Wait for some inode to
  690. * become available for writeback. Otherwise
  691. * we'll just busyloop.
  692. */
  693. if (!list_empty(&wb->b_more_io)) {
  694. trace_writeback_wait(wb->bdi, work);
  695. inode = wb_inode(wb->b_more_io.prev);
  696. spin_lock(&inode->i_lock);
  697. inode_wait_for_writeback(inode, wb);
  698. spin_unlock(&inode->i_lock);
  699. }
  700. }
  701. spin_unlock(&wb->list_lock);
  702. return nr_pages - work->nr_pages;
  703. }
  704. /*
  705. * Return the next wb_writeback_work struct that hasn't been processed yet.
  706. */
  707. static struct wb_writeback_work *
  708. get_next_work_item(struct backing_dev_info *bdi)
  709. {
  710. struct wb_writeback_work *work = NULL;
  711. spin_lock_bh(&bdi->wb_lock);
  712. if (!list_empty(&bdi->work_list)) {
  713. work = list_entry(bdi->work_list.next,
  714. struct wb_writeback_work, list);
  715. list_del_init(&work->list);
  716. }
  717. spin_unlock_bh(&bdi->wb_lock);
  718. return work;
  719. }
  720. /*
  721. * Add in the number of potentially dirty inodes, because each inode
  722. * write can dirty pagecache in the underlying blockdev.
  723. */
  724. static unsigned long get_nr_dirty_pages(void)
  725. {
  726. return global_page_state(NR_FILE_DIRTY) +
  727. global_page_state(NR_UNSTABLE_NFS) +
  728. get_nr_dirty_inodes();
  729. }
  730. static long wb_check_background_flush(struct bdi_writeback *wb)
  731. {
  732. if (over_bground_thresh()) {
  733. struct wb_writeback_work work = {
  734. .nr_pages = LONG_MAX,
  735. .sync_mode = WB_SYNC_NONE,
  736. .for_background = 1,
  737. .range_cyclic = 1,
  738. };
  739. return wb_writeback(wb, &work);
  740. }
  741. return 0;
  742. }
  743. static long wb_check_old_data_flush(struct bdi_writeback *wb)
  744. {
  745. unsigned long expired;
  746. long nr_pages;
  747. /*
  748. * When set to zero, disable periodic writeback
  749. */
  750. if (!dirty_writeback_interval)
  751. return 0;
  752. expired = wb->last_old_flush +
  753. msecs_to_jiffies(dirty_writeback_interval * 10);
  754. if (time_before(jiffies, expired))
  755. return 0;
  756. wb->last_old_flush = jiffies;
  757. nr_pages = get_nr_dirty_pages();
  758. if (nr_pages) {
  759. struct wb_writeback_work work = {
  760. .nr_pages = nr_pages,
  761. .sync_mode = WB_SYNC_NONE,
  762. .for_kupdate = 1,
  763. .range_cyclic = 1,
  764. };
  765. return wb_writeback(wb, &work);
  766. }
  767. return 0;
  768. }
  769. /*
  770. * Retrieve work items and do the writeback they describe
  771. */
  772. long wb_do_writeback(struct bdi_writeback *wb, int force_wait)
  773. {
  774. struct backing_dev_info *bdi = wb->bdi;
  775. struct wb_writeback_work *work;
  776. long wrote = 0;
  777. set_bit(BDI_writeback_running, &wb->bdi->state);
  778. while ((work = get_next_work_item(bdi)) != NULL) {
  779. /*
  780. * Override sync mode, in case we must wait for completion
  781. * because this thread is exiting now.
  782. */
  783. if (force_wait)
  784. work->sync_mode = WB_SYNC_ALL;
  785. trace_writeback_exec(bdi, work);
  786. wrote += wb_writeback(wb, work);
  787. /*
  788. * Notify the caller of completion if this is a synchronous
  789. * work item, otherwise just free it.
  790. */
  791. if (work->done)
  792. complete(work->done);
  793. else
  794. kfree(work);
  795. }
  796. /*
  797. * Check for periodic writeback, kupdated() style
  798. */
  799. wrote += wb_check_old_data_flush(wb);
  800. wrote += wb_check_background_flush(wb);
  801. clear_bit(BDI_writeback_running, &wb->bdi->state);
  802. return wrote;
  803. }
  804. /*
  805. * Handle writeback of dirty data for the device backed by this bdi. Also
  806. * wakes up periodically and does kupdated style flushing.
  807. */
  808. int bdi_writeback_thread(void *data)
  809. {
  810. struct bdi_writeback *wb = data;
  811. struct backing_dev_info *bdi = wb->bdi;
  812. long pages_written;
  813. current->flags |= PF_SWAPWRITE;
  814. set_freezable();
  815. wb->last_active = jiffies;
  816. /*
  817. * Our parent may run at a different priority, just set us to normal
  818. */
  819. set_user_nice(current, 0);
  820. trace_writeback_thread_start(bdi);
  821. while (!kthread_should_stop()) {
  822. /*
  823. * Remove own delayed wake-up timer, since we are already awake
  824. * and we'll take care of the preriodic write-back.
  825. */
  826. del_timer(&wb->wakeup_timer);
  827. pages_written = wb_do_writeback(wb, 0);
  828. trace_writeback_pages_written(pages_written);
  829. if (pages_written)
  830. wb->last_active = jiffies;
  831. set_current_state(TASK_INTERRUPTIBLE);
  832. if (!list_empty(&bdi->work_list) || kthread_should_stop()) {
  833. __set_current_state(TASK_RUNNING);
  834. continue;
  835. }
  836. if (wb_has_dirty_io(wb) && dirty_writeback_interval)
  837. schedule_timeout(msecs_to_jiffies(dirty_writeback_interval * 10));
  838. else {
  839. /*
  840. * We have nothing to do, so can go sleep without any
  841. * timeout and save power. When a work is queued or
  842. * something is made dirty - we will be woken up.
  843. */
  844. schedule();
  845. }
  846. try_to_freeze();
  847. }
  848. /* Flush any work that raced with us exiting */
  849. if (!list_empty(&bdi->work_list))
  850. wb_do_writeback(wb, 1);
  851. trace_writeback_thread_stop(bdi);
  852. return 0;
  853. }
  854. /*
  855. * Start writeback of `nr_pages' pages. If `nr_pages' is zero, write back
  856. * the whole world.
  857. */
  858. void wakeup_flusher_threads(long nr_pages)
  859. {
  860. struct backing_dev_info *bdi;
  861. if (!nr_pages) {
  862. nr_pages = global_page_state(NR_FILE_DIRTY) +
  863. global_page_state(NR_UNSTABLE_NFS);
  864. }
  865. rcu_read_lock();
  866. list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) {
  867. if (!bdi_has_dirty_io(bdi))
  868. continue;
  869. __bdi_start_writeback(bdi, nr_pages, false);
  870. }
  871. rcu_read_unlock();
  872. }
  873. static noinline void block_dump___mark_inode_dirty(struct inode *inode)
  874. {
  875. if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) {
  876. struct dentry *dentry;
  877. const char *name = "?";
  878. dentry = d_find_alias(inode);
  879. if (dentry) {
  880. spin_lock(&dentry->d_lock);
  881. name = (const char *) dentry->d_name.name;
  882. }
  883. printk(KERN_DEBUG
  884. "%s(%d): dirtied inode %lu (%s) on %s\n",
  885. current->comm, task_pid_nr(current), inode->i_ino,
  886. name, inode->i_sb->s_id);
  887. if (dentry) {
  888. spin_unlock(&dentry->d_lock);
  889. dput(dentry);
  890. }
  891. }
  892. }
  893. /**
  894. * __mark_inode_dirty - internal function
  895. * @inode: inode to mark
  896. * @flags: what kind of dirty (i.e. I_DIRTY_SYNC)
  897. * Mark an inode as dirty. Callers should use mark_inode_dirty or
  898. * mark_inode_dirty_sync.
  899. *
  900. * Put the inode on the super block's dirty list.
  901. *
  902. * CAREFUL! We mark it dirty unconditionally, but move it onto the
  903. * dirty list only if it is hashed or if it refers to a blockdev.
  904. * If it was not hashed, it will never be added to the dirty list
  905. * even if it is later hashed, as it will have been marked dirty already.
  906. *
  907. * In short, make sure you hash any inodes _before_ you start marking
  908. * them dirty.
  909. *
  910. * Note that for blockdevs, inode->dirtied_when represents the dirtying time of
  911. * the block-special inode (/dev/hda1) itself. And the ->dirtied_when field of
  912. * the kernel-internal blockdev inode represents the dirtying time of the
  913. * blockdev's pages. This is why for I_DIRTY_PAGES we always use
  914. * page->mapping->host, so the page-dirtying time is recorded in the internal
  915. * blockdev inode.
  916. */
  917. void __mark_inode_dirty(struct inode *inode, int flags)
  918. {
  919. struct super_block *sb = inode->i_sb;
  920. struct backing_dev_info *bdi = NULL;
  921. /*
  922. * Don't do this for I_DIRTY_PAGES - that doesn't actually
  923. * dirty the inode itself
  924. */
  925. if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
  926. if (sb->s_op->dirty_inode)
  927. sb->s_op->dirty_inode(inode, flags);
  928. }
  929. /*
  930. * make sure that changes are seen by all cpus before we test i_state
  931. * -- mikulas
  932. */
  933. smp_mb();
  934. /* avoid the locking if we can */
  935. if ((inode->i_state & flags) == flags)
  936. return;
  937. if (unlikely(block_dump))
  938. block_dump___mark_inode_dirty(inode);
  939. spin_lock(&inode->i_lock);
  940. if ((inode->i_state & flags) != flags) {
  941. const int was_dirty = inode->i_state & I_DIRTY;
  942. inode->i_state |= flags;
  943. /*
  944. * If the inode is being synced, just update its dirty state.
  945. * The unlocker will place the inode on the appropriate
  946. * superblock list, based upon its state.
  947. */
  948. if (inode->i_state & I_SYNC)
  949. goto out_unlock_inode;
  950. /*
  951. * Only add valid (hashed) inodes to the superblock's
  952. * dirty list. Add blockdev inodes as well.
  953. */
  954. if (!S_ISBLK(inode->i_mode)) {
  955. if (inode_unhashed(inode))
  956. goto out_unlock_inode;
  957. }
  958. if (inode->i_state & I_FREEING)
  959. goto out_unlock_inode;
  960. /*
  961. * If the inode was already on b_dirty/b_io/b_more_io, don't
  962. * reposition it (that would break b_dirty time-ordering).
  963. */
  964. if (!was_dirty) {
  965. bool wakeup_bdi = false;
  966. bdi = inode_to_bdi(inode);
  967. if (bdi_cap_writeback_dirty(bdi)) {
  968. WARN(!test_bit(BDI_registered, &bdi->state),
  969. "bdi-%s not registered\n", bdi->name);
  970. /*
  971. * If this is the first dirty inode for this
  972. * bdi, we have to wake-up the corresponding
  973. * bdi thread to make sure background
  974. * write-back happens later.
  975. */
  976. if (!wb_has_dirty_io(&bdi->wb))
  977. wakeup_bdi = true;
  978. }
  979. spin_unlock(&inode->i_lock);
  980. spin_lock(&bdi->wb.list_lock);
  981. inode->dirtied_when = jiffies;
  982. list_move(&inode->i_wb_list, &bdi->wb.b_dirty);
  983. spin_unlock(&bdi->wb.list_lock);
  984. if (wakeup_bdi)
  985. bdi_wakeup_thread_delayed(bdi);
  986. return;
  987. }
  988. }
  989. out_unlock_inode:
  990. spin_unlock(&inode->i_lock);
  991. }
  992. EXPORT_SYMBOL(__mark_inode_dirty);
  993. /*
  994. * Write out a superblock's list of dirty inodes. A wait will be performed
  995. * upon no inodes, all inodes or the final one, depending upon sync_mode.
  996. *
  997. * If older_than_this is non-NULL, then only write out inodes which
  998. * had their first dirtying at a time earlier than *older_than_this.
  999. *
  1000. * If `bdi' is non-zero then we're being asked to writeback a specific queue.
  1001. * This function assumes that the blockdev superblock's inodes are backed by
  1002. * a variety of queues, so all inodes are searched. For other superblocks,
  1003. * assume that all inodes are backed by the same queue.
  1004. *
  1005. * The inodes to be written are parked on bdi->b_io. They are moved back onto
  1006. * bdi->b_dirty as they are selected for writing. This way, none can be missed
  1007. * on the writer throttling path, and we get decent balancing between many
  1008. * throttled threads: we don't want them all piling up on inode_sync_wait.
  1009. */
  1010. static void wait_sb_inodes(struct super_block *sb)
  1011. {
  1012. struct inode *inode, *old_inode = NULL;
  1013. /*
  1014. * We need to be protected against the filesystem going from
  1015. * r/o to r/w or vice versa.
  1016. */
  1017. WARN_ON(!rwsem_is_locked(&sb->s_umount));
  1018. spin_lock(&inode_sb_list_lock);
  1019. /*
  1020. * Data integrity sync. Must wait for all pages under writeback,
  1021. * because there may have been pages dirtied before our sync
  1022. * call, but which had writeout started before we write it out.
  1023. * In which case, the inode may not be on the dirty list, but
  1024. * we still have to wait for that writeout.
  1025. */
  1026. list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
  1027. struct address_space *mapping = inode->i_mapping;
  1028. spin_lock(&inode->i_lock);
  1029. if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
  1030. (mapping->nrpages == 0)) {
  1031. spin_unlock(&inode->i_lock);
  1032. continue;
  1033. }
  1034. __iget(inode);
  1035. spin_unlock(&inode->i_lock);
  1036. spin_unlock(&inode_sb_list_lock);
  1037. /*
  1038. * We hold a reference to 'inode' so it couldn't have been
  1039. * removed from s_inodes list while we dropped the
  1040. * inode_sb_list_lock. We cannot iput the inode now as we can
  1041. * be holding the last reference and we cannot iput it under
  1042. * inode_sb_list_lock. So we keep the reference and iput it
  1043. * later.
  1044. */
  1045. iput(old_inode);
  1046. old_inode = inode;
  1047. filemap_fdatawait(mapping);
  1048. cond_resched();
  1049. spin_lock(&inode_sb_list_lock);
  1050. }
  1051. spin_unlock(&inode_sb_list_lock);
  1052. iput(old_inode);
  1053. }
  1054. /**
  1055. * writeback_inodes_sb_nr - writeback dirty inodes from given super_block
  1056. * @sb: the superblock
  1057. * @nr: the number of pages to write
  1058. *
  1059. * Start writeback on some inodes on this super_block. No guarantees are made
  1060. * on how many (if any) will be written, and this function does not wait
  1061. * for IO completion of submitted IO.
  1062. */
  1063. void writeback_inodes_sb_nr(struct super_block *sb, unsigned long nr)
  1064. {
  1065. DECLARE_COMPLETION_ONSTACK(done);
  1066. struct wb_writeback_work work = {
  1067. .sb = sb,
  1068. .sync_mode = WB_SYNC_NONE,
  1069. .tagged_writepages = 1,
  1070. .done = &done,
  1071. .nr_pages = nr,
  1072. };
  1073. WARN_ON(!rwsem_is_locked(&sb->s_umount));
  1074. bdi_queue_work(sb->s_bdi, &work);
  1075. wait_for_completion(&done);
  1076. }
  1077. EXPORT_SYMBOL(writeback_inodes_sb_nr);
  1078. /**
  1079. * writeback_inodes_sb - writeback dirty inodes from given super_block
  1080. * @sb: the superblock
  1081. *
  1082. * Start writeback on some inodes on this super_block. No guarantees are made
  1083. * on how many (if any) will be written, and this function does not wait
  1084. * for IO completion of submitted IO.
  1085. */
  1086. void writeback_inodes_sb(struct super_block *sb)
  1087. {
  1088. return writeback_inodes_sb_nr(sb, get_nr_dirty_pages());
  1089. }
  1090. EXPORT_SYMBOL(writeback_inodes_sb);
  1091. /**
  1092. * writeback_inodes_sb_if_idle - start writeback if none underway
  1093. * @sb: the superblock
  1094. *
  1095. * Invoke writeback_inodes_sb if no writeback is currently underway.
  1096. * Returns 1 if writeback was started, 0 if not.
  1097. */
  1098. int writeback_inodes_sb_if_idle(struct super_block *sb)
  1099. {
  1100. if (!writeback_in_progress(sb->s_bdi)) {
  1101. down_read(&sb->s_umount);
  1102. writeback_inodes_sb(sb);
  1103. up_read(&sb->s_umount);
  1104. return 1;
  1105. } else
  1106. return 0;
  1107. }
  1108. EXPORT_SYMBOL(writeback_inodes_sb_if_idle);
  1109. /**
  1110. * writeback_inodes_sb_if_idle - start writeback if none underway
  1111. * @sb: the superblock
  1112. * @nr: the number of pages to write
  1113. *
  1114. * Invoke writeback_inodes_sb if no writeback is currently underway.
  1115. * Returns 1 if writeback was started, 0 if not.
  1116. */
  1117. int writeback_inodes_sb_nr_if_idle(struct super_block *sb,
  1118. unsigned long nr)
  1119. {
  1120. if (!writeback_in_progress(sb->s_bdi)) {
  1121. down_read(&sb->s_umount);
  1122. writeback_inodes_sb_nr(sb, nr);
  1123. up_read(&sb->s_umount);
  1124. return 1;
  1125. } else
  1126. return 0;
  1127. }
  1128. EXPORT_SYMBOL(writeback_inodes_sb_nr_if_idle);
  1129. /**
  1130. * sync_inodes_sb - sync sb inode pages
  1131. * @sb: the superblock
  1132. *
  1133. * This function writes and waits on any dirty inode belonging to this
  1134. * super_block.
  1135. */
  1136. void sync_inodes_sb(struct super_block *sb)
  1137. {
  1138. DECLARE_COMPLETION_ONSTACK(done);
  1139. struct wb_writeback_work work = {
  1140. .sb = sb,
  1141. .sync_mode = WB_SYNC_ALL,
  1142. .nr_pages = LONG_MAX,
  1143. .range_cyclic = 0,
  1144. .done = &done,
  1145. };
  1146. WARN_ON(!rwsem_is_locked(&sb->s_umount));
  1147. bdi_queue_work(sb->s_bdi, &work);
  1148. wait_for_completion(&done);
  1149. wait_sb_inodes(sb);
  1150. }
  1151. EXPORT_SYMBOL(sync_inodes_sb);
  1152. /**
  1153. * write_inode_now - write an inode to disk
  1154. * @inode: inode to write to disk
  1155. * @sync: whether the write should be synchronous or not
  1156. *
  1157. * This function commits an inode to disk immediately if it is dirty. This is
  1158. * primarily needed by knfsd.
  1159. *
  1160. * The caller must either have a ref on the inode or must have set I_WILL_FREE.
  1161. */
  1162. int write_inode_now(struct inode *inode, int sync)
  1163. {
  1164. struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
  1165. int ret;
  1166. struct writeback_control wbc = {
  1167. .nr_to_write = LONG_MAX,
  1168. .sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE,
  1169. .range_start = 0,
  1170. .range_end = LLONG_MAX,
  1171. };
  1172. if (!mapping_cap_writeback_dirty(inode->i_mapping))
  1173. wbc.nr_to_write = 0;
  1174. might_sleep();
  1175. spin_lock(&wb->list_lock);
  1176. spin_lock(&inode->i_lock);
  1177. ret = writeback_single_inode(inode, wb, &wbc);
  1178. spin_unlock(&inode->i_lock);
  1179. spin_unlock(&wb->list_lock);
  1180. if (sync)
  1181. inode_sync_wait(inode);
  1182. return ret;
  1183. }
  1184. EXPORT_SYMBOL(write_inode_now);
  1185. /**
  1186. * sync_inode - write an inode and its pages to disk.
  1187. * @inode: the inode to sync
  1188. * @wbc: controls the writeback mode
  1189. *
  1190. * sync_inode() will write an inode and its pages to disk. It will also
  1191. * correctly update the inode on its superblock's dirty inode lists and will
  1192. * update inode->i_state.
  1193. *
  1194. * The caller must have a ref on the inode.
  1195. */
  1196. int sync_inode(struct inode *inode, struct writeback_control *wbc)
  1197. {
  1198. struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
  1199. int ret;
  1200. spin_lock(&wb->list_lock);
  1201. spin_lock(&inode->i_lock);
  1202. ret = writeback_single_inode(inode, wb, wbc);
  1203. spin_unlock(&inode->i_lock);
  1204. spin_unlock(&wb->list_lock);
  1205. return ret;
  1206. }
  1207. EXPORT_SYMBOL(sync_inode);
  1208. /**
  1209. * sync_inode_metadata - write an inode to disk
  1210. * @inode: the inode to sync
  1211. * @wait: wait for I/O to complete.
  1212. *
  1213. * Write an inode to disk and adjust its dirty state after completion.
  1214. *
  1215. * Note: only writes the actual inode, no associated data or other metadata.
  1216. */
  1217. int sync_inode_metadata(struct inode *inode, int wait)
  1218. {
  1219. struct writeback_control wbc = {
  1220. .sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_NONE,
  1221. .nr_to_write = 0, /* metadata-only */
  1222. };
  1223. return sync_inode(inode, &wbc);
  1224. }
  1225. EXPORT_SYMBOL(sync_inode_metadata);