fs-writeback.c 44 KB

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