fs-writeback.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  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 "internal.h"
  29. #define inode_to_bdi(inode) ((inode)->i_mapping->backing_dev_info)
  30. /*
  31. * We don't actually have pdflush, but this one is exported though /proc...
  32. */
  33. int nr_pdflush_threads;
  34. /*
  35. * Passed into wb_writeback(), essentially a subset of writeback_control
  36. */
  37. struct wb_writeback_work {
  38. long nr_pages;
  39. struct super_block *sb;
  40. enum writeback_sync_modes sync_mode;
  41. unsigned int for_kupdate:1;
  42. unsigned int range_cyclic:1;
  43. unsigned int for_background:1;
  44. struct list_head list; /* pending work list */
  45. struct completion *done; /* set if the caller waits */
  46. };
  47. /**
  48. * writeback_in_progress - determine whether there is writeback in progress
  49. * @bdi: the device's backing_dev_info structure.
  50. *
  51. * Determine whether there is writeback waiting to be handled against a
  52. * backing device.
  53. */
  54. int writeback_in_progress(struct backing_dev_info *bdi)
  55. {
  56. return !list_empty(&bdi->work_list);
  57. }
  58. static void bdi_queue_work(struct backing_dev_info *bdi,
  59. struct wb_writeback_work *work)
  60. {
  61. spin_lock(&bdi->wb_lock);
  62. list_add_tail(&work->list, &bdi->work_list);
  63. spin_unlock(&bdi->wb_lock);
  64. /*
  65. * If the default thread isn't there, make sure we add it. When
  66. * it gets created and wakes up, we'll run this work.
  67. */
  68. if (unlikely(list_empty_careful(&bdi->wb_list)))
  69. wake_up_process(default_backing_dev_info.wb.task);
  70. else {
  71. struct bdi_writeback *wb = &bdi->wb;
  72. if (wb->task)
  73. wake_up_process(wb->task);
  74. }
  75. }
  76. static void
  77. __bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages,
  78. bool range_cyclic, bool for_background)
  79. {
  80. struct wb_writeback_work *work;
  81. /*
  82. * This is WB_SYNC_NONE writeback, so if allocation fails just
  83. * wakeup the thread for old dirty data writeback
  84. */
  85. work = kzalloc(sizeof(*work), GFP_ATOMIC);
  86. if (!work) {
  87. if (bdi->wb.task)
  88. wake_up_process(bdi->wb.task);
  89. return;
  90. }
  91. work->sync_mode = WB_SYNC_NONE;
  92. work->nr_pages = nr_pages;
  93. work->range_cyclic = range_cyclic;
  94. work->for_background = for_background;
  95. bdi_queue_work(bdi, work);
  96. }
  97. /**
  98. * bdi_start_writeback - start writeback
  99. * @bdi: the backing device to write from
  100. * @nr_pages: the number of pages to write
  101. *
  102. * Description:
  103. * This does WB_SYNC_NONE opportunistic writeback. The IO is only
  104. * started when this function returns, we make no guarentees on
  105. * completion. Caller need not hold sb s_umount semaphore.
  106. *
  107. */
  108. void bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages)
  109. {
  110. __bdi_start_writeback(bdi, nr_pages, true, false);
  111. }
  112. /**
  113. * bdi_start_background_writeback - start background writeback
  114. * @bdi: the backing device to write from
  115. *
  116. * Description:
  117. * This does WB_SYNC_NONE background writeback. The IO is only
  118. * started when this function returns, we make no guarentees on
  119. * completion. Caller need not hold sb s_umount semaphore.
  120. */
  121. void bdi_start_background_writeback(struct backing_dev_info *bdi)
  122. {
  123. __bdi_start_writeback(bdi, LONG_MAX, true, true);
  124. }
  125. /*
  126. * Redirty an inode: set its when-it-was dirtied timestamp and move it to the
  127. * furthest end of its superblock's dirty-inode list.
  128. *
  129. * Before stamping the inode's ->dirtied_when, we check to see whether it is
  130. * already the most-recently-dirtied inode on the b_dirty list. If that is
  131. * the case then the inode must have been redirtied while it was being written
  132. * out and we don't reset its dirtied_when.
  133. */
  134. static void redirty_tail(struct inode *inode)
  135. {
  136. struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
  137. if (!list_empty(&wb->b_dirty)) {
  138. struct inode *tail;
  139. tail = list_entry(wb->b_dirty.next, struct inode, i_list);
  140. if (time_before(inode->dirtied_when, tail->dirtied_when))
  141. inode->dirtied_when = jiffies;
  142. }
  143. list_move(&inode->i_list, &wb->b_dirty);
  144. }
  145. /*
  146. * requeue inode for re-scanning after bdi->b_io list is exhausted.
  147. */
  148. static void requeue_io(struct inode *inode)
  149. {
  150. struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
  151. list_move(&inode->i_list, &wb->b_more_io);
  152. }
  153. static void inode_sync_complete(struct inode *inode)
  154. {
  155. /*
  156. * Prevent speculative execution through spin_unlock(&inode_lock);
  157. */
  158. smp_mb();
  159. wake_up_bit(&inode->i_state, __I_SYNC);
  160. }
  161. static bool inode_dirtied_after(struct inode *inode, unsigned long t)
  162. {
  163. bool ret = time_after(inode->dirtied_when, t);
  164. #ifndef CONFIG_64BIT
  165. /*
  166. * For inodes being constantly redirtied, dirtied_when can get stuck.
  167. * It _appears_ to be in the future, but is actually in distant past.
  168. * This test is necessary to prevent such wrapped-around relative times
  169. * from permanently stopping the whole bdi writeback.
  170. */
  171. ret = ret && time_before_eq(inode->dirtied_when, jiffies);
  172. #endif
  173. return ret;
  174. }
  175. /*
  176. * Move expired dirty inodes from @delaying_queue to @dispatch_queue.
  177. */
  178. static void move_expired_inodes(struct list_head *delaying_queue,
  179. struct list_head *dispatch_queue,
  180. unsigned long *older_than_this)
  181. {
  182. LIST_HEAD(tmp);
  183. struct list_head *pos, *node;
  184. struct super_block *sb = NULL;
  185. struct inode *inode;
  186. int do_sb_sort = 0;
  187. while (!list_empty(delaying_queue)) {
  188. inode = list_entry(delaying_queue->prev, struct inode, i_list);
  189. if (older_than_this &&
  190. inode_dirtied_after(inode, *older_than_this))
  191. break;
  192. if (sb && sb != inode->i_sb)
  193. do_sb_sort = 1;
  194. sb = inode->i_sb;
  195. list_move(&inode->i_list, &tmp);
  196. }
  197. /* just one sb in list, splice to dispatch_queue and we're done */
  198. if (!do_sb_sort) {
  199. list_splice(&tmp, dispatch_queue);
  200. return;
  201. }
  202. /* Move inodes from one superblock together */
  203. while (!list_empty(&tmp)) {
  204. inode = list_entry(tmp.prev, struct inode, i_list);
  205. sb = inode->i_sb;
  206. list_for_each_prev_safe(pos, node, &tmp) {
  207. inode = list_entry(pos, struct inode, i_list);
  208. if (inode->i_sb == sb)
  209. list_move(&inode->i_list, dispatch_queue);
  210. }
  211. }
  212. }
  213. /*
  214. * Queue all expired dirty inodes for io, eldest first.
  215. */
  216. static void queue_io(struct bdi_writeback *wb, unsigned long *older_than_this)
  217. {
  218. list_splice_init(&wb->b_more_io, wb->b_io.prev);
  219. move_expired_inodes(&wb->b_dirty, &wb->b_io, older_than_this);
  220. }
  221. static int write_inode(struct inode *inode, struct writeback_control *wbc)
  222. {
  223. if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode))
  224. return inode->i_sb->s_op->write_inode(inode, wbc);
  225. return 0;
  226. }
  227. /*
  228. * Wait for writeback on an inode to complete.
  229. */
  230. static void inode_wait_for_writeback(struct inode *inode)
  231. {
  232. DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC);
  233. wait_queue_head_t *wqh;
  234. wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
  235. while (inode->i_state & I_SYNC) {
  236. spin_unlock(&inode_lock);
  237. __wait_on_bit(wqh, &wq, inode_wait, TASK_UNINTERRUPTIBLE);
  238. spin_lock(&inode_lock);
  239. }
  240. }
  241. /*
  242. * Write out an inode's dirty pages. Called under inode_lock. Either the
  243. * caller has ref on the inode (either via __iget or via syscall against an fd)
  244. * or the inode has I_WILL_FREE set (via generic_forget_inode)
  245. *
  246. * If `wait' is set, wait on the writeout.
  247. *
  248. * The whole writeout design is quite complex and fragile. We want to avoid
  249. * starvation of particular inodes when others are being redirtied, prevent
  250. * livelocks, etc.
  251. *
  252. * Called under inode_lock.
  253. */
  254. static int
  255. writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
  256. {
  257. struct address_space *mapping = inode->i_mapping;
  258. unsigned dirty;
  259. int ret;
  260. if (!atomic_read(&inode->i_count))
  261. WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING)));
  262. else
  263. WARN_ON(inode->i_state & I_WILL_FREE);
  264. if (inode->i_state & I_SYNC) {
  265. /*
  266. * If this inode is locked for writeback and we are not doing
  267. * writeback-for-data-integrity, move it to b_more_io so that
  268. * writeback can proceed with the other inodes on s_io.
  269. *
  270. * We'll have another go at writing back this inode when we
  271. * completed a full scan of b_io.
  272. */
  273. if (wbc->sync_mode != WB_SYNC_ALL) {
  274. requeue_io(inode);
  275. return 0;
  276. }
  277. /*
  278. * It's a data-integrity sync. We must wait.
  279. */
  280. inode_wait_for_writeback(inode);
  281. }
  282. BUG_ON(inode->i_state & I_SYNC);
  283. /* Set I_SYNC, reset I_DIRTY_PAGES */
  284. inode->i_state |= I_SYNC;
  285. inode->i_state &= ~I_DIRTY_PAGES;
  286. spin_unlock(&inode_lock);
  287. ret = do_writepages(mapping, wbc);
  288. /*
  289. * Make sure to wait on the data before writing out the metadata.
  290. * This is important for filesystems that modify metadata on data
  291. * I/O completion.
  292. */
  293. if (wbc->sync_mode == WB_SYNC_ALL) {
  294. int err = filemap_fdatawait(mapping);
  295. if (ret == 0)
  296. ret = err;
  297. }
  298. /*
  299. * Some filesystems may redirty the inode during the writeback
  300. * due to delalloc, clear dirty metadata flags right before
  301. * write_inode()
  302. */
  303. spin_lock(&inode_lock);
  304. dirty = inode->i_state & I_DIRTY;
  305. inode->i_state &= ~(I_DIRTY_SYNC | I_DIRTY_DATASYNC);
  306. spin_unlock(&inode_lock);
  307. /* Don't write the inode if only I_DIRTY_PAGES was set */
  308. if (dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
  309. int err = write_inode(inode, wbc);
  310. if (ret == 0)
  311. ret = err;
  312. }
  313. spin_lock(&inode_lock);
  314. inode->i_state &= ~I_SYNC;
  315. if (!(inode->i_state & (I_FREEING | I_CLEAR))) {
  316. if ((inode->i_state & I_DIRTY_PAGES) && wbc->for_kupdate) {
  317. /*
  318. * More pages get dirtied by a fast dirtier.
  319. */
  320. goto select_queue;
  321. } else if (inode->i_state & I_DIRTY) {
  322. /*
  323. * At least XFS will redirty the inode during the
  324. * writeback (delalloc) and on io completion (isize).
  325. */
  326. redirty_tail(inode);
  327. } else if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
  328. /*
  329. * We didn't write back all the pages. nfs_writepages()
  330. * sometimes bales out without doing anything. Redirty
  331. * the inode; Move it from b_io onto b_more_io/b_dirty.
  332. */
  333. /*
  334. * akpm: if the caller was the kupdate function we put
  335. * this inode at the head of b_dirty so it gets first
  336. * consideration. Otherwise, move it to the tail, for
  337. * the reasons described there. I'm not really sure
  338. * how much sense this makes. Presumably I had a good
  339. * reasons for doing it this way, and I'd rather not
  340. * muck with it at present.
  341. */
  342. if (wbc->for_kupdate) {
  343. /*
  344. * For the kupdate function we move the inode
  345. * to b_more_io so it will get more writeout as
  346. * soon as the queue becomes uncongested.
  347. */
  348. inode->i_state |= I_DIRTY_PAGES;
  349. select_queue:
  350. if (wbc->nr_to_write <= 0) {
  351. /*
  352. * slice used up: queue for next turn
  353. */
  354. requeue_io(inode);
  355. } else {
  356. /*
  357. * somehow blocked: retry later
  358. */
  359. redirty_tail(inode);
  360. }
  361. } else {
  362. /*
  363. * Otherwise fully redirty the inode so that
  364. * other inodes on this superblock will get some
  365. * writeout. Otherwise heavy writing to one
  366. * file would indefinitely suspend writeout of
  367. * all the other files.
  368. */
  369. inode->i_state |= I_DIRTY_PAGES;
  370. redirty_tail(inode);
  371. }
  372. } else if (atomic_read(&inode->i_count)) {
  373. /*
  374. * The inode is clean, inuse
  375. */
  376. list_move(&inode->i_list, &inode_in_use);
  377. } else {
  378. /*
  379. * The inode is clean, unused
  380. */
  381. list_move(&inode->i_list, &inode_unused);
  382. }
  383. }
  384. inode_sync_complete(inode);
  385. return ret;
  386. }
  387. /*
  388. * For background writeback the caller does not have the sb pinned
  389. * before calling writeback. So make sure that we do pin it, so it doesn't
  390. * go away while we are writing inodes from it.
  391. */
  392. static bool pin_sb_for_writeback(struct super_block *sb)
  393. {
  394. spin_lock(&sb_lock);
  395. if (list_empty(&sb->s_instances)) {
  396. spin_unlock(&sb_lock);
  397. return false;
  398. }
  399. sb->s_count++;
  400. spin_unlock(&sb_lock);
  401. if (down_read_trylock(&sb->s_umount)) {
  402. if (sb->s_root)
  403. return true;
  404. up_read(&sb->s_umount);
  405. }
  406. put_super(sb);
  407. return false;
  408. }
  409. /*
  410. * Write a portion of b_io inodes which belong to @sb.
  411. *
  412. * If @only_this_sb is true, then find and write all such
  413. * inodes. Otherwise write only ones which go sequentially
  414. * in reverse order.
  415. *
  416. * Return 1, if the caller writeback routine should be
  417. * interrupted. Otherwise return 0.
  418. */
  419. static int writeback_sb_inodes(struct super_block *sb, struct bdi_writeback *wb,
  420. struct writeback_control *wbc, bool only_this_sb)
  421. {
  422. while (!list_empty(&wb->b_io)) {
  423. long pages_skipped;
  424. struct inode *inode = list_entry(wb->b_io.prev,
  425. struct inode, i_list);
  426. if (inode->i_sb != sb) {
  427. if (only_this_sb) {
  428. /*
  429. * We only want to write back data for this
  430. * superblock, move all inodes not belonging
  431. * to it back onto the dirty list.
  432. */
  433. redirty_tail(inode);
  434. continue;
  435. }
  436. /*
  437. * The inode belongs to a different superblock.
  438. * Bounce back to the caller to unpin this and
  439. * pin the next superblock.
  440. */
  441. return 0;
  442. }
  443. if (inode->i_state & (I_NEW | I_WILL_FREE)) {
  444. requeue_io(inode);
  445. continue;
  446. }
  447. /*
  448. * Was this inode dirtied after sync_sb_inodes was called?
  449. * This keeps sync from extra jobs and livelock.
  450. */
  451. if (inode_dirtied_after(inode, wbc->wb_start))
  452. return 1;
  453. BUG_ON(inode->i_state & (I_FREEING | I_CLEAR));
  454. __iget(inode);
  455. pages_skipped = wbc->pages_skipped;
  456. writeback_single_inode(inode, wbc);
  457. if (wbc->pages_skipped != pages_skipped) {
  458. /*
  459. * writeback is not making progress due to locked
  460. * buffers. Skip this inode for now.
  461. */
  462. redirty_tail(inode);
  463. }
  464. spin_unlock(&inode_lock);
  465. iput(inode);
  466. cond_resched();
  467. spin_lock(&inode_lock);
  468. if (wbc->nr_to_write <= 0) {
  469. wbc->more_io = 1;
  470. return 1;
  471. }
  472. if (!list_empty(&wb->b_more_io))
  473. wbc->more_io = 1;
  474. }
  475. /* b_io is empty */
  476. return 1;
  477. }
  478. void writeback_inodes_wb(struct bdi_writeback *wb,
  479. struct writeback_control *wbc)
  480. {
  481. int ret = 0;
  482. wbc->wb_start = jiffies; /* livelock avoidance */
  483. spin_lock(&inode_lock);
  484. if (!wbc->for_kupdate || list_empty(&wb->b_io))
  485. queue_io(wb, wbc->older_than_this);
  486. while (!list_empty(&wb->b_io)) {
  487. struct inode *inode = list_entry(wb->b_io.prev,
  488. struct inode, i_list);
  489. struct super_block *sb = inode->i_sb;
  490. if (!pin_sb_for_writeback(sb)) {
  491. requeue_io(inode);
  492. continue;
  493. }
  494. ret = writeback_sb_inodes(sb, wb, wbc, false);
  495. drop_super(sb);
  496. if (ret)
  497. break;
  498. }
  499. spin_unlock(&inode_lock);
  500. /* Leave any unwritten inodes on b_io */
  501. }
  502. static void __writeback_inodes_sb(struct super_block *sb,
  503. struct bdi_writeback *wb, struct writeback_control *wbc)
  504. {
  505. WARN_ON(!rwsem_is_locked(&sb->s_umount));
  506. wbc->wb_start = jiffies; /* livelock avoidance */
  507. spin_lock(&inode_lock);
  508. if (!wbc->for_kupdate || list_empty(&wb->b_io))
  509. queue_io(wb, wbc->older_than_this);
  510. writeback_sb_inodes(sb, wb, wbc, true);
  511. spin_unlock(&inode_lock);
  512. }
  513. /*
  514. * The maximum number of pages to writeout in a single bdi flush/kupdate
  515. * operation. We do this so we don't hold I_SYNC against an inode for
  516. * enormous amounts of time, which would block a userspace task which has
  517. * been forced to throttle against that inode. Also, the code reevaluates
  518. * the dirty each time it has written this many pages.
  519. */
  520. #define MAX_WRITEBACK_PAGES 1024
  521. static inline bool over_bground_thresh(void)
  522. {
  523. unsigned long background_thresh, dirty_thresh;
  524. get_dirty_limits(&background_thresh, &dirty_thresh, NULL, NULL);
  525. return (global_page_state(NR_FILE_DIRTY) +
  526. global_page_state(NR_UNSTABLE_NFS) >= background_thresh);
  527. }
  528. /*
  529. * Explicit flushing or periodic writeback of "old" data.
  530. *
  531. * Define "old": the first time one of an inode's pages is dirtied, we mark the
  532. * dirtying-time in the inode's address_space. So this periodic writeback code
  533. * just walks the superblock inode list, writing back any inodes which are
  534. * older than a specific point in time.
  535. *
  536. * Try to run once per dirty_writeback_interval. But if a writeback event
  537. * takes longer than a dirty_writeback_interval interval, then leave a
  538. * one-second gap.
  539. *
  540. * older_than_this takes precedence over nr_to_write. So we'll only write back
  541. * all dirty pages if they are all attached to "old" mappings.
  542. */
  543. static long wb_writeback(struct bdi_writeback *wb,
  544. struct wb_writeback_work *work)
  545. {
  546. struct writeback_control wbc = {
  547. .sync_mode = work->sync_mode,
  548. .older_than_this = NULL,
  549. .for_kupdate = work->for_kupdate,
  550. .for_background = work->for_background,
  551. .range_cyclic = work->range_cyclic,
  552. };
  553. unsigned long oldest_jif;
  554. long wrote = 0;
  555. struct inode *inode;
  556. if (wbc.for_kupdate) {
  557. wbc.older_than_this = &oldest_jif;
  558. oldest_jif = jiffies -
  559. msecs_to_jiffies(dirty_expire_interval * 10);
  560. }
  561. if (!wbc.range_cyclic) {
  562. wbc.range_start = 0;
  563. wbc.range_end = LLONG_MAX;
  564. }
  565. for (;;) {
  566. /*
  567. * Stop writeback when nr_pages has been consumed
  568. */
  569. if (work->nr_pages <= 0)
  570. break;
  571. /*
  572. * For background writeout, stop when we are below the
  573. * background dirty threshold
  574. */
  575. if (work->for_background && !over_bground_thresh())
  576. break;
  577. wbc.more_io = 0;
  578. wbc.nr_to_write = MAX_WRITEBACK_PAGES;
  579. wbc.pages_skipped = 0;
  580. if (work->sb)
  581. __writeback_inodes_sb(work->sb, wb, &wbc);
  582. else
  583. writeback_inodes_wb(wb, &wbc);
  584. work->nr_pages -= MAX_WRITEBACK_PAGES - wbc.nr_to_write;
  585. wrote += MAX_WRITEBACK_PAGES - wbc.nr_to_write;
  586. /*
  587. * If we consumed everything, see if we have more
  588. */
  589. if (wbc.nr_to_write <= 0)
  590. continue;
  591. /*
  592. * Didn't write everything and we don't have more IO, bail
  593. */
  594. if (!wbc.more_io)
  595. break;
  596. /*
  597. * Did we write something? Try for more
  598. */
  599. if (wbc.nr_to_write < MAX_WRITEBACK_PAGES)
  600. continue;
  601. /*
  602. * Nothing written. Wait for some inode to
  603. * become available for writeback. Otherwise
  604. * we'll just busyloop.
  605. */
  606. spin_lock(&inode_lock);
  607. if (!list_empty(&wb->b_more_io)) {
  608. inode = list_entry(wb->b_more_io.prev,
  609. struct inode, i_list);
  610. inode_wait_for_writeback(inode);
  611. }
  612. spin_unlock(&inode_lock);
  613. }
  614. return wrote;
  615. }
  616. /*
  617. * Return the next wb_writeback_work struct that hasn't been processed yet.
  618. */
  619. static struct wb_writeback_work *
  620. get_next_work_item(struct backing_dev_info *bdi, struct bdi_writeback *wb)
  621. {
  622. struct wb_writeback_work *work = NULL;
  623. spin_lock(&bdi->wb_lock);
  624. if (!list_empty(&bdi->work_list)) {
  625. work = list_entry(bdi->work_list.next,
  626. struct wb_writeback_work, list);
  627. list_del_init(&work->list);
  628. }
  629. spin_unlock(&bdi->wb_lock);
  630. return work;
  631. }
  632. static long wb_check_old_data_flush(struct bdi_writeback *wb)
  633. {
  634. unsigned long expired;
  635. long nr_pages;
  636. /*
  637. * When set to zero, disable periodic writeback
  638. */
  639. if (!dirty_writeback_interval)
  640. return 0;
  641. expired = wb->last_old_flush +
  642. msecs_to_jiffies(dirty_writeback_interval * 10);
  643. if (time_before(jiffies, expired))
  644. return 0;
  645. wb->last_old_flush = jiffies;
  646. nr_pages = global_page_state(NR_FILE_DIRTY) +
  647. global_page_state(NR_UNSTABLE_NFS) +
  648. (inodes_stat.nr_inodes - inodes_stat.nr_unused);
  649. if (nr_pages) {
  650. struct wb_writeback_work work = {
  651. .nr_pages = nr_pages,
  652. .sync_mode = WB_SYNC_NONE,
  653. .for_kupdate = 1,
  654. .range_cyclic = 1,
  655. };
  656. return wb_writeback(wb, &work);
  657. }
  658. return 0;
  659. }
  660. /*
  661. * Retrieve work items and do the writeback they describe
  662. */
  663. long wb_do_writeback(struct bdi_writeback *wb, int force_wait)
  664. {
  665. struct backing_dev_info *bdi = wb->bdi;
  666. struct wb_writeback_work *work;
  667. long wrote = 0;
  668. while ((work = get_next_work_item(bdi, wb)) != NULL) {
  669. /*
  670. * Override sync mode, in case we must wait for completion
  671. * because this thread is exiting now.
  672. */
  673. if (force_wait)
  674. work->sync_mode = WB_SYNC_ALL;
  675. wrote += wb_writeback(wb, work);
  676. /*
  677. * Notify the caller of completion if this is a synchronous
  678. * work item, otherwise just free it.
  679. */
  680. if (work->done)
  681. complete(work->done);
  682. else
  683. kfree(work);
  684. }
  685. /*
  686. * Check for periodic writeback, kupdated() style
  687. */
  688. wrote += wb_check_old_data_flush(wb);
  689. return wrote;
  690. }
  691. /*
  692. * Handle writeback of dirty data for the device backed by this bdi. Also
  693. * wakes up periodically and does kupdated style flushing.
  694. */
  695. int bdi_writeback_task(struct bdi_writeback *wb)
  696. {
  697. unsigned long last_active = jiffies;
  698. unsigned long wait_jiffies = -1UL;
  699. long pages_written;
  700. while (!kthread_should_stop()) {
  701. pages_written = wb_do_writeback(wb, 0);
  702. if (pages_written)
  703. last_active = jiffies;
  704. else if (wait_jiffies != -1UL) {
  705. unsigned long max_idle;
  706. /*
  707. * Longest period of inactivity that we tolerate. If we
  708. * see dirty data again later, the task will get
  709. * recreated automatically.
  710. */
  711. max_idle = max(5UL * 60 * HZ, wait_jiffies);
  712. if (time_after(jiffies, max_idle + last_active))
  713. break;
  714. }
  715. if (dirty_writeback_interval) {
  716. wait_jiffies = msecs_to_jiffies(dirty_writeback_interval * 10);
  717. schedule_timeout_interruptible(wait_jiffies);
  718. } else {
  719. set_current_state(TASK_INTERRUPTIBLE);
  720. if (list_empty_careful(&wb->bdi->work_list) &&
  721. !kthread_should_stop())
  722. schedule();
  723. __set_current_state(TASK_RUNNING);
  724. }
  725. try_to_freeze();
  726. }
  727. return 0;
  728. }
  729. /*
  730. * Start writeback of `nr_pages' pages. If `nr_pages' is zero, write back
  731. * the whole world.
  732. */
  733. void wakeup_flusher_threads(long nr_pages)
  734. {
  735. struct backing_dev_info *bdi;
  736. if (!nr_pages) {
  737. nr_pages = global_page_state(NR_FILE_DIRTY) +
  738. global_page_state(NR_UNSTABLE_NFS);
  739. }
  740. rcu_read_lock();
  741. list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) {
  742. if (!bdi_has_dirty_io(bdi))
  743. continue;
  744. __bdi_start_writeback(bdi, nr_pages, false, false);
  745. }
  746. rcu_read_unlock();
  747. }
  748. static noinline void block_dump___mark_inode_dirty(struct inode *inode)
  749. {
  750. if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) {
  751. struct dentry *dentry;
  752. const char *name = "?";
  753. dentry = d_find_alias(inode);
  754. if (dentry) {
  755. spin_lock(&dentry->d_lock);
  756. name = (const char *) dentry->d_name.name;
  757. }
  758. printk(KERN_DEBUG
  759. "%s(%d): dirtied inode %lu (%s) on %s\n",
  760. current->comm, task_pid_nr(current), inode->i_ino,
  761. name, inode->i_sb->s_id);
  762. if (dentry) {
  763. spin_unlock(&dentry->d_lock);
  764. dput(dentry);
  765. }
  766. }
  767. }
  768. /**
  769. * __mark_inode_dirty - internal function
  770. * @inode: inode to mark
  771. * @flags: what kind of dirty (i.e. I_DIRTY_SYNC)
  772. * Mark an inode as dirty. Callers should use mark_inode_dirty or
  773. * mark_inode_dirty_sync.
  774. *
  775. * Put the inode on the super block's dirty list.
  776. *
  777. * CAREFUL! We mark it dirty unconditionally, but move it onto the
  778. * dirty list only if it is hashed or if it refers to a blockdev.
  779. * If it was not hashed, it will never be added to the dirty list
  780. * even if it is later hashed, as it will have been marked dirty already.
  781. *
  782. * In short, make sure you hash any inodes _before_ you start marking
  783. * them dirty.
  784. *
  785. * This function *must* be atomic for the I_DIRTY_PAGES case -
  786. * set_page_dirty() is called under spinlock in several places.
  787. *
  788. * Note that for blockdevs, inode->dirtied_when represents the dirtying time of
  789. * the block-special inode (/dev/hda1) itself. And the ->dirtied_when field of
  790. * the kernel-internal blockdev inode represents the dirtying time of the
  791. * blockdev's pages. This is why for I_DIRTY_PAGES we always use
  792. * page->mapping->host, so the page-dirtying time is recorded in the internal
  793. * blockdev inode.
  794. */
  795. void __mark_inode_dirty(struct inode *inode, int flags)
  796. {
  797. struct super_block *sb = inode->i_sb;
  798. /*
  799. * Don't do this for I_DIRTY_PAGES - that doesn't actually
  800. * dirty the inode itself
  801. */
  802. if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
  803. if (sb->s_op->dirty_inode)
  804. sb->s_op->dirty_inode(inode);
  805. }
  806. /*
  807. * make sure that changes are seen by all cpus before we test i_state
  808. * -- mikulas
  809. */
  810. smp_mb();
  811. /* avoid the locking if we can */
  812. if ((inode->i_state & flags) == flags)
  813. return;
  814. if (unlikely(block_dump))
  815. block_dump___mark_inode_dirty(inode);
  816. spin_lock(&inode_lock);
  817. if ((inode->i_state & flags) != flags) {
  818. const int was_dirty = inode->i_state & I_DIRTY;
  819. inode->i_state |= flags;
  820. /*
  821. * If the inode is being synced, just update its dirty state.
  822. * The unlocker will place the inode on the appropriate
  823. * superblock list, based upon its state.
  824. */
  825. if (inode->i_state & I_SYNC)
  826. goto out;
  827. /*
  828. * Only add valid (hashed) inodes to the superblock's
  829. * dirty list. Add blockdev inodes as well.
  830. */
  831. if (!S_ISBLK(inode->i_mode)) {
  832. if (hlist_unhashed(&inode->i_hash))
  833. goto out;
  834. }
  835. if (inode->i_state & (I_FREEING|I_CLEAR))
  836. goto out;
  837. /*
  838. * If the inode was already on b_dirty/b_io/b_more_io, don't
  839. * reposition it (that would break b_dirty time-ordering).
  840. */
  841. if (!was_dirty) {
  842. struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
  843. struct backing_dev_info *bdi = wb->bdi;
  844. if (bdi_cap_writeback_dirty(bdi) &&
  845. !test_bit(BDI_registered, &bdi->state)) {
  846. WARN_ON(1);
  847. printk(KERN_ERR "bdi-%s not registered\n",
  848. bdi->name);
  849. }
  850. inode->dirtied_when = jiffies;
  851. list_move(&inode->i_list, &wb->b_dirty);
  852. }
  853. }
  854. out:
  855. spin_unlock(&inode_lock);
  856. }
  857. EXPORT_SYMBOL(__mark_inode_dirty);
  858. /*
  859. * Write out a superblock's list of dirty inodes. A wait will be performed
  860. * upon no inodes, all inodes or the final one, depending upon sync_mode.
  861. *
  862. * If older_than_this is non-NULL, then only write out inodes which
  863. * had their first dirtying at a time earlier than *older_than_this.
  864. *
  865. * If `bdi' is non-zero then we're being asked to writeback a specific queue.
  866. * This function assumes that the blockdev superblock's inodes are backed by
  867. * a variety of queues, so all inodes are searched. For other superblocks,
  868. * assume that all inodes are backed by the same queue.
  869. *
  870. * The inodes to be written are parked on bdi->b_io. They are moved back onto
  871. * bdi->b_dirty as they are selected for writing. This way, none can be missed
  872. * on the writer throttling path, and we get decent balancing between many
  873. * throttled threads: we don't want them all piling up on inode_sync_wait.
  874. */
  875. static void wait_sb_inodes(struct super_block *sb)
  876. {
  877. struct inode *inode, *old_inode = NULL;
  878. /*
  879. * We need to be protected against the filesystem going from
  880. * r/o to r/w or vice versa.
  881. */
  882. WARN_ON(!rwsem_is_locked(&sb->s_umount));
  883. spin_lock(&inode_lock);
  884. /*
  885. * Data integrity sync. Must wait for all pages under writeback,
  886. * because there may have been pages dirtied before our sync
  887. * call, but which had writeout started before we write it out.
  888. * In which case, the inode may not be on the dirty list, but
  889. * we still have to wait for that writeout.
  890. */
  891. list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
  892. struct address_space *mapping;
  893. if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE|I_NEW))
  894. continue;
  895. mapping = inode->i_mapping;
  896. if (mapping->nrpages == 0)
  897. continue;
  898. __iget(inode);
  899. spin_unlock(&inode_lock);
  900. /*
  901. * We hold a reference to 'inode' so it couldn't have
  902. * been removed from s_inodes list while we dropped the
  903. * inode_lock. We cannot iput the inode now as we can
  904. * be holding the last reference and we cannot iput it
  905. * under inode_lock. So we keep the reference and iput
  906. * it later.
  907. */
  908. iput(old_inode);
  909. old_inode = inode;
  910. filemap_fdatawait(mapping);
  911. cond_resched();
  912. spin_lock(&inode_lock);
  913. }
  914. spin_unlock(&inode_lock);
  915. iput(old_inode);
  916. }
  917. /**
  918. * writeback_inodes_sb - writeback dirty inodes from given super_block
  919. * @sb: the superblock
  920. *
  921. * Start writeback on some inodes on this super_block. No guarantees are made
  922. * on how many (if any) will be written, and this function does not wait
  923. * for IO completion of submitted IO. The number of pages submitted is
  924. * returned.
  925. */
  926. void writeback_inodes_sb(struct super_block *sb)
  927. {
  928. unsigned long nr_dirty = global_page_state(NR_FILE_DIRTY);
  929. unsigned long nr_unstable = global_page_state(NR_UNSTABLE_NFS);
  930. DECLARE_COMPLETION_ONSTACK(done);
  931. struct wb_writeback_work work = {
  932. .sb = sb,
  933. .sync_mode = WB_SYNC_NONE,
  934. .done = &done,
  935. };
  936. WARN_ON(!rwsem_is_locked(&sb->s_umount));
  937. work.nr_pages = nr_dirty + nr_unstable +
  938. (inodes_stat.nr_inodes - inodes_stat.nr_unused);
  939. bdi_queue_work(sb->s_bdi, &work);
  940. wait_for_completion(&done);
  941. }
  942. EXPORT_SYMBOL(writeback_inodes_sb);
  943. /**
  944. * writeback_inodes_sb_if_idle - start writeback if none underway
  945. * @sb: the superblock
  946. *
  947. * Invoke writeback_inodes_sb if no writeback is currently underway.
  948. * Returns 1 if writeback was started, 0 if not.
  949. */
  950. int writeback_inodes_sb_if_idle(struct super_block *sb)
  951. {
  952. if (!writeback_in_progress(sb->s_bdi)) {
  953. down_read(&sb->s_umount);
  954. writeback_inodes_sb(sb);
  955. up_read(&sb->s_umount);
  956. return 1;
  957. } else
  958. return 0;
  959. }
  960. EXPORT_SYMBOL(writeback_inodes_sb_if_idle);
  961. /**
  962. * sync_inodes_sb - sync sb inode pages
  963. * @sb: the superblock
  964. *
  965. * This function writes and waits on any dirty inode belonging to this
  966. * super_block. The number of pages synced is returned.
  967. */
  968. void sync_inodes_sb(struct super_block *sb)
  969. {
  970. DECLARE_COMPLETION_ONSTACK(done);
  971. struct wb_writeback_work work = {
  972. .sb = sb,
  973. .sync_mode = WB_SYNC_ALL,
  974. .nr_pages = LONG_MAX,
  975. .range_cyclic = 0,
  976. .done = &done,
  977. };
  978. WARN_ON(!rwsem_is_locked(&sb->s_umount));
  979. bdi_queue_work(sb->s_bdi, &work);
  980. wait_for_completion(&done);
  981. wait_sb_inodes(sb);
  982. }
  983. EXPORT_SYMBOL(sync_inodes_sb);
  984. /**
  985. * write_inode_now - write an inode to disk
  986. * @inode: inode to write to disk
  987. * @sync: whether the write should be synchronous or not
  988. *
  989. * This function commits an inode to disk immediately if it is dirty. This is
  990. * primarily needed by knfsd.
  991. *
  992. * The caller must either have a ref on the inode or must have set I_WILL_FREE.
  993. */
  994. int write_inode_now(struct inode *inode, int sync)
  995. {
  996. int ret;
  997. struct writeback_control wbc = {
  998. .nr_to_write = LONG_MAX,
  999. .sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE,
  1000. .range_start = 0,
  1001. .range_end = LLONG_MAX,
  1002. };
  1003. if (!mapping_cap_writeback_dirty(inode->i_mapping))
  1004. wbc.nr_to_write = 0;
  1005. might_sleep();
  1006. spin_lock(&inode_lock);
  1007. ret = writeback_single_inode(inode, &wbc);
  1008. spin_unlock(&inode_lock);
  1009. if (sync)
  1010. inode_sync_wait(inode);
  1011. return ret;
  1012. }
  1013. EXPORT_SYMBOL(write_inode_now);
  1014. /**
  1015. * sync_inode - write an inode and its pages to disk.
  1016. * @inode: the inode to sync
  1017. * @wbc: controls the writeback mode
  1018. *
  1019. * sync_inode() will write an inode and its pages to disk. It will also
  1020. * correctly update the inode on its superblock's dirty inode lists and will
  1021. * update inode->i_state.
  1022. *
  1023. * The caller must have a ref on the inode.
  1024. */
  1025. int sync_inode(struct inode *inode, struct writeback_control *wbc)
  1026. {
  1027. int ret;
  1028. spin_lock(&inode_lock);
  1029. ret = writeback_single_inode(inode, wbc);
  1030. spin_unlock(&inode_lock);
  1031. return ret;
  1032. }
  1033. EXPORT_SYMBOL(sync_inode);