fs-writeback.c 36 KB

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