fs-writeback.c 40 KB

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