fs-writeback.c 31 KB

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