fs-writeback.c 42 KB

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