fs-writeback.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. /*
  2. * fs/fs-writeback.c
  3. *
  4. * Copyright (C) 2002, Linus Torvalds.
  5. *
  6. * Contains all the functions related to writing back and waiting
  7. * upon dirty inodes against superblocks, and writing back dirty
  8. * pages against inodes. ie: data writeback. Writeout of the
  9. * inode itself is not handled here.
  10. *
  11. * 10Apr2002 Andrew Morton
  12. * Split out of fs/inode.c
  13. * Additions for address_space-based writeback
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/sched.h>
  19. #include <linux/fs.h>
  20. #include <linux/mm.h>
  21. #include <linux/writeback.h>
  22. #include <linux/blkdev.h>
  23. #include <linux/backing-dev.h>
  24. #include <linux/buffer_head.h>
  25. #include "internal.h"
  26. /**
  27. * writeback_acquire - attempt to get exclusive writeback access to a device
  28. * @bdi: the device's backing_dev_info structure
  29. *
  30. * It is a waste of resources to have more than one pdflush thread blocked on
  31. * a single request queue. Exclusion at the request_queue level is obtained
  32. * via a flag in the request_queue's backing_dev_info.state.
  33. *
  34. * Non-request_queue-backed address_spaces will share default_backing_dev_info,
  35. * unless they implement their own. Which is somewhat inefficient, as this
  36. * may prevent concurrent writeback against multiple devices.
  37. */
  38. static int writeback_acquire(struct backing_dev_info *bdi)
  39. {
  40. return !test_and_set_bit(BDI_pdflush, &bdi->state);
  41. }
  42. /**
  43. * writeback_in_progress - determine whether there is writeback in progress
  44. * @bdi: the device's backing_dev_info structure.
  45. *
  46. * Determine whether there is writeback in progress against a backing device.
  47. */
  48. int writeback_in_progress(struct backing_dev_info *bdi)
  49. {
  50. return test_bit(BDI_pdflush, &bdi->state);
  51. }
  52. /**
  53. * writeback_release - relinquish exclusive writeback access against a device.
  54. * @bdi: the device's backing_dev_info structure
  55. */
  56. static void writeback_release(struct backing_dev_info *bdi)
  57. {
  58. BUG_ON(!writeback_in_progress(bdi));
  59. clear_bit(BDI_pdflush, &bdi->state);
  60. }
  61. static noinline void block_dump___mark_inode_dirty(struct inode *inode)
  62. {
  63. if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) {
  64. struct dentry *dentry;
  65. const char *name = "?";
  66. dentry = d_find_alias(inode);
  67. if (dentry) {
  68. spin_lock(&dentry->d_lock);
  69. name = (const char *) dentry->d_name.name;
  70. }
  71. printk(KERN_DEBUG
  72. "%s(%d): dirtied inode %lu (%s) on %s\n",
  73. current->comm, task_pid_nr(current), inode->i_ino,
  74. name, inode->i_sb->s_id);
  75. if (dentry) {
  76. spin_unlock(&dentry->d_lock);
  77. dput(dentry);
  78. }
  79. }
  80. }
  81. /**
  82. * __mark_inode_dirty - internal function
  83. * @inode: inode to mark
  84. * @flags: what kind of dirty (i.e. I_DIRTY_SYNC)
  85. * Mark an inode as dirty. Callers should use mark_inode_dirty or
  86. * mark_inode_dirty_sync.
  87. *
  88. * Put the inode on the super block's dirty list.
  89. *
  90. * CAREFUL! We mark it dirty unconditionally, but move it onto the
  91. * dirty list only if it is hashed or if it refers to a blockdev.
  92. * If it was not hashed, it will never be added to the dirty list
  93. * even if it is later hashed, as it will have been marked dirty already.
  94. *
  95. * In short, make sure you hash any inodes _before_ you start marking
  96. * them dirty.
  97. *
  98. * This function *must* be atomic for the I_DIRTY_PAGES case -
  99. * set_page_dirty() is called under spinlock in several places.
  100. *
  101. * Note that for blockdevs, inode->dirtied_when represents the dirtying time of
  102. * the block-special inode (/dev/hda1) itself. And the ->dirtied_when field of
  103. * the kernel-internal blockdev inode represents the dirtying time of the
  104. * blockdev's pages. This is why for I_DIRTY_PAGES we always use
  105. * page->mapping->host, so the page-dirtying time is recorded in the internal
  106. * blockdev inode.
  107. */
  108. void __mark_inode_dirty(struct inode *inode, int flags)
  109. {
  110. struct super_block *sb = inode->i_sb;
  111. /*
  112. * Don't do this for I_DIRTY_PAGES - that doesn't actually
  113. * dirty the inode itself
  114. */
  115. if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
  116. if (sb->s_op->dirty_inode)
  117. sb->s_op->dirty_inode(inode);
  118. }
  119. /*
  120. * make sure that changes are seen by all cpus before we test i_state
  121. * -- mikulas
  122. */
  123. smp_mb();
  124. /* avoid the locking if we can */
  125. if ((inode->i_state & flags) == flags)
  126. return;
  127. if (unlikely(block_dump))
  128. block_dump___mark_inode_dirty(inode);
  129. spin_lock(&inode_lock);
  130. if ((inode->i_state & flags) != flags) {
  131. const int was_dirty = inode->i_state & I_DIRTY;
  132. inode->i_state |= flags;
  133. /*
  134. * If the inode is being synced, just update its dirty state.
  135. * The unlocker will place the inode on the appropriate
  136. * superblock list, based upon its state.
  137. */
  138. if (inode->i_state & I_SYNC)
  139. goto out;
  140. /*
  141. * Only add valid (hashed) inodes to the superblock's
  142. * dirty list. Add blockdev inodes as well.
  143. */
  144. if (!S_ISBLK(inode->i_mode)) {
  145. if (hlist_unhashed(&inode->i_hash))
  146. goto out;
  147. }
  148. if (inode->i_state & (I_FREEING|I_CLEAR))
  149. goto out;
  150. /*
  151. * If the inode was already on s_dirty/s_io/s_more_io, don't
  152. * reposition it (that would break s_dirty time-ordering).
  153. */
  154. if (!was_dirty) {
  155. inode->dirtied_when = jiffies;
  156. list_move(&inode->i_list, &sb->s_dirty);
  157. }
  158. }
  159. out:
  160. spin_unlock(&inode_lock);
  161. }
  162. EXPORT_SYMBOL(__mark_inode_dirty);
  163. static int write_inode(struct inode *inode, int sync)
  164. {
  165. if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode))
  166. return inode->i_sb->s_op->write_inode(inode, sync);
  167. return 0;
  168. }
  169. /*
  170. * Redirty an inode: set its when-it-was dirtied timestamp and move it to the
  171. * furthest end of its superblock's dirty-inode list.
  172. *
  173. * Before stamping the inode's ->dirtied_when, we check to see whether it is
  174. * already the most-recently-dirtied inode on the s_dirty list. If that is
  175. * the case then the inode must have been redirtied while it was being written
  176. * out and we don't reset its dirtied_when.
  177. */
  178. static void redirty_tail(struct inode *inode)
  179. {
  180. struct super_block *sb = inode->i_sb;
  181. if (!list_empty(&sb->s_dirty)) {
  182. struct inode *tail_inode;
  183. tail_inode = list_entry(sb->s_dirty.next, struct inode, i_list);
  184. if (time_before(inode->dirtied_when,
  185. tail_inode->dirtied_when))
  186. inode->dirtied_when = jiffies;
  187. }
  188. list_move(&inode->i_list, &sb->s_dirty);
  189. }
  190. /*
  191. * requeue inode for re-scanning after sb->s_io list is exhausted.
  192. */
  193. static void requeue_io(struct inode *inode)
  194. {
  195. list_move(&inode->i_list, &inode->i_sb->s_more_io);
  196. }
  197. static void inode_sync_complete(struct inode *inode)
  198. {
  199. /*
  200. * Prevent speculative execution through spin_unlock(&inode_lock);
  201. */
  202. smp_mb();
  203. wake_up_bit(&inode->i_state, __I_SYNC);
  204. }
  205. static bool inode_dirtied_after(struct inode *inode, unsigned long t)
  206. {
  207. bool ret = time_after(inode->dirtied_when, t);
  208. #ifndef CONFIG_64BIT
  209. /*
  210. * For inodes being constantly redirtied, dirtied_when can get stuck.
  211. * It _appears_ to be in the future, but is actually in distant past.
  212. * This test is necessary to prevent such wrapped-around relative times
  213. * from permanently stopping the whole pdflush writeback.
  214. */
  215. ret = ret && time_before_eq(inode->dirtied_when, jiffies);
  216. #endif
  217. return ret;
  218. }
  219. /*
  220. * Move expired dirty inodes from @delaying_queue to @dispatch_queue.
  221. */
  222. static void move_expired_inodes(struct list_head *delaying_queue,
  223. struct list_head *dispatch_queue,
  224. unsigned long *older_than_this)
  225. {
  226. while (!list_empty(delaying_queue)) {
  227. struct inode *inode = list_entry(delaying_queue->prev,
  228. struct inode, i_list);
  229. if (older_than_this &&
  230. inode_dirtied_after(inode, *older_than_this))
  231. break;
  232. list_move(&inode->i_list, dispatch_queue);
  233. }
  234. }
  235. /*
  236. * Queue all expired dirty inodes for io, eldest first.
  237. */
  238. static void queue_io(struct super_block *sb,
  239. unsigned long *older_than_this)
  240. {
  241. list_splice_init(&sb->s_more_io, sb->s_io.prev);
  242. move_expired_inodes(&sb->s_dirty, &sb->s_io, older_than_this);
  243. }
  244. int sb_has_dirty_inodes(struct super_block *sb)
  245. {
  246. return !list_empty(&sb->s_dirty) ||
  247. !list_empty(&sb->s_io) ||
  248. !list_empty(&sb->s_more_io);
  249. }
  250. EXPORT_SYMBOL(sb_has_dirty_inodes);
  251. /*
  252. * Wait for writeback on an inode to complete.
  253. */
  254. static void inode_wait_for_writeback(struct inode *inode)
  255. {
  256. DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC);
  257. wait_queue_head_t *wqh;
  258. wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
  259. do {
  260. spin_unlock(&inode_lock);
  261. __wait_on_bit(wqh, &wq, inode_wait, TASK_UNINTERRUPTIBLE);
  262. spin_lock(&inode_lock);
  263. } while (inode->i_state & I_SYNC);
  264. }
  265. /*
  266. * Write out an inode's dirty pages. Called under inode_lock. Either the
  267. * caller has ref on the inode (either via __iget or via syscall against an fd)
  268. * or the inode has I_WILL_FREE set (via generic_forget_inode)
  269. *
  270. * If `wait' is set, wait on the writeout.
  271. *
  272. * The whole writeout design is quite complex and fragile. We want to avoid
  273. * starvation of particular inodes when others are being redirtied, prevent
  274. * livelocks, etc.
  275. *
  276. * Called under inode_lock.
  277. */
  278. static int
  279. writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
  280. {
  281. struct address_space *mapping = inode->i_mapping;
  282. int wait = wbc->sync_mode == WB_SYNC_ALL;
  283. unsigned dirty;
  284. int ret;
  285. if (!atomic_read(&inode->i_count))
  286. WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING)));
  287. else
  288. WARN_ON(inode->i_state & I_WILL_FREE);
  289. if (inode->i_state & I_SYNC) {
  290. /*
  291. * If this inode is locked for writeback and we are not doing
  292. * writeback-for-data-integrity, move it to s_more_io so that
  293. * writeback can proceed with the other inodes on s_io.
  294. *
  295. * We'll have another go at writing back this inode when we
  296. * completed a full scan of s_io.
  297. */
  298. if (!wait) {
  299. requeue_io(inode);
  300. return 0;
  301. }
  302. /*
  303. * It's a data-integrity sync. We must wait.
  304. */
  305. inode_wait_for_writeback(inode);
  306. }
  307. BUG_ON(inode->i_state & I_SYNC);
  308. /* Set I_SYNC, reset I_DIRTY */
  309. dirty = inode->i_state & I_DIRTY;
  310. inode->i_state |= I_SYNC;
  311. inode->i_state &= ~I_DIRTY;
  312. spin_unlock(&inode_lock);
  313. ret = do_writepages(mapping, wbc);
  314. /* Don't write the inode if only I_DIRTY_PAGES was set */
  315. if (dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
  316. int err = write_inode(inode, wait);
  317. if (ret == 0)
  318. ret = err;
  319. }
  320. if (wait) {
  321. int err = filemap_fdatawait(mapping);
  322. if (ret == 0)
  323. ret = err;
  324. }
  325. spin_lock(&inode_lock);
  326. inode->i_state &= ~I_SYNC;
  327. if (!(inode->i_state & (I_FREEING | I_CLEAR))) {
  328. if (!(inode->i_state & I_DIRTY) &&
  329. mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
  330. /*
  331. * We didn't write back all the pages. nfs_writepages()
  332. * sometimes bales out without doing anything. Redirty
  333. * the inode; Move it from s_io onto s_more_io/s_dirty.
  334. */
  335. /*
  336. * akpm: if the caller was the kupdate function we put
  337. * this inode at the head of s_dirty so it gets first
  338. * consideration. Otherwise, move it to the tail, for
  339. * the reasons described there. I'm not really sure
  340. * how much sense this makes. Presumably I had a good
  341. * reasons for doing it this way, and I'd rather not
  342. * muck with it at present.
  343. */
  344. if (wbc->for_kupdate) {
  345. /*
  346. * For the kupdate function we move the inode
  347. * to s_more_io so it will get more writeout as
  348. * soon as the queue becomes uncongested.
  349. */
  350. inode->i_state |= I_DIRTY_PAGES;
  351. if (wbc->nr_to_write <= 0) {
  352. /*
  353. * slice used up: queue for next turn
  354. */
  355. requeue_io(inode);
  356. } else {
  357. /*
  358. * somehow blocked: retry later
  359. */
  360. redirty_tail(inode);
  361. }
  362. } else {
  363. /*
  364. * Otherwise fully redirty the inode so that
  365. * other inodes on this superblock will get some
  366. * writeout. Otherwise heavy writing to one
  367. * file would indefinitely suspend writeout of
  368. * all the other files.
  369. */
  370. inode->i_state |= I_DIRTY_PAGES;
  371. redirty_tail(inode);
  372. }
  373. } else if (inode->i_state & I_DIRTY) {
  374. /*
  375. * Someone redirtied the inode while were writing back
  376. * the pages.
  377. */
  378. redirty_tail(inode);
  379. } else if (atomic_read(&inode->i_count)) {
  380. /*
  381. * The inode is clean, inuse
  382. */
  383. list_move(&inode->i_list, &inode_in_use);
  384. } else {
  385. /*
  386. * The inode is clean, unused
  387. */
  388. list_move(&inode->i_list, &inode_unused);
  389. }
  390. }
  391. inode_sync_complete(inode);
  392. return ret;
  393. }
  394. /*
  395. * Write out a superblock's list of dirty inodes. A wait will be performed
  396. * upon no inodes, all inodes or the final one, depending upon sync_mode.
  397. *
  398. * If older_than_this is non-NULL, then only write out inodes which
  399. * had their first dirtying at a time earlier than *older_than_this.
  400. *
  401. * If we're a pdflush thread, then implement pdflush collision avoidance
  402. * against the entire list.
  403. *
  404. * If `bdi' is non-zero then we're being asked to writeback a specific queue.
  405. * This function assumes that the blockdev superblock's inodes are backed by
  406. * a variety of queues, so all inodes are searched. For other superblocks,
  407. * assume that all inodes are backed by the same queue.
  408. *
  409. * FIXME: this linear search could get expensive with many fileystems. But
  410. * how to fix? We need to go from an address_space to all inodes which share
  411. * a queue with that address_space. (Easy: have a global "dirty superblocks"
  412. * list).
  413. *
  414. * The inodes to be written are parked on sb->s_io. They are moved back onto
  415. * sb->s_dirty as they are selected for writing. This way, none can be missed
  416. * on the writer throttling path, and we get decent balancing between many
  417. * throttled threads: we don't want them all piling up on inode_sync_wait.
  418. */
  419. void generic_sync_sb_inodes(struct super_block *sb,
  420. struct writeback_control *wbc)
  421. {
  422. const unsigned long start = jiffies; /* livelock avoidance */
  423. int sync = wbc->sync_mode == WB_SYNC_ALL;
  424. spin_lock(&inode_lock);
  425. if (!wbc->for_kupdate || list_empty(&sb->s_io))
  426. queue_io(sb, wbc->older_than_this);
  427. while (!list_empty(&sb->s_io)) {
  428. struct inode *inode = list_entry(sb->s_io.prev,
  429. struct inode, i_list);
  430. struct address_space *mapping = inode->i_mapping;
  431. struct backing_dev_info *bdi = mapping->backing_dev_info;
  432. long pages_skipped;
  433. if (!bdi_cap_writeback_dirty(bdi)) {
  434. redirty_tail(inode);
  435. if (sb_is_blkdev_sb(sb)) {
  436. /*
  437. * Dirty memory-backed blockdev: the ramdisk
  438. * driver does this. Skip just this inode
  439. */
  440. continue;
  441. }
  442. /*
  443. * Dirty memory-backed inode against a filesystem other
  444. * than the kernel-internal bdev filesystem. Skip the
  445. * entire superblock.
  446. */
  447. break;
  448. }
  449. if (inode->i_state & (I_NEW | I_WILL_FREE)) {
  450. requeue_io(inode);
  451. continue;
  452. }
  453. if (wbc->nonblocking && bdi_write_congested(bdi)) {
  454. wbc->encountered_congestion = 1;
  455. if (!sb_is_blkdev_sb(sb))
  456. break; /* Skip a congested fs */
  457. requeue_io(inode);
  458. continue; /* Skip a congested blockdev */
  459. }
  460. if (wbc->bdi && bdi != wbc->bdi) {
  461. if (!sb_is_blkdev_sb(sb))
  462. break; /* fs has the wrong queue */
  463. requeue_io(inode);
  464. continue; /* blockdev has wrong queue */
  465. }
  466. /*
  467. * Was this inode dirtied after sync_sb_inodes was called?
  468. * This keeps sync from extra jobs and livelock.
  469. */
  470. if (inode_dirtied_after(inode, start))
  471. break;
  472. /* Is another pdflush already flushing this queue? */
  473. if (current_is_pdflush() && !writeback_acquire(bdi))
  474. break;
  475. BUG_ON(inode->i_state & (I_FREEING | I_CLEAR));
  476. __iget(inode);
  477. pages_skipped = wbc->pages_skipped;
  478. writeback_single_inode(inode, wbc);
  479. if (current_is_pdflush())
  480. writeback_release(bdi);
  481. if (wbc->pages_skipped != pages_skipped) {
  482. /*
  483. * writeback is not making progress due to locked
  484. * buffers. Skip this inode for now.
  485. */
  486. redirty_tail(inode);
  487. }
  488. spin_unlock(&inode_lock);
  489. iput(inode);
  490. cond_resched();
  491. spin_lock(&inode_lock);
  492. if (wbc->nr_to_write <= 0) {
  493. wbc->more_io = 1;
  494. break;
  495. }
  496. if (!list_empty(&sb->s_more_io))
  497. wbc->more_io = 1;
  498. }
  499. if (sync) {
  500. struct inode *inode, *old_inode = NULL;
  501. /*
  502. * Data integrity sync. Must wait for all pages under writeback,
  503. * because there may have been pages dirtied before our sync
  504. * call, but which had writeout started before we write it out.
  505. * In which case, the inode may not be on the dirty list, but
  506. * we still have to wait for that writeout.
  507. */
  508. list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
  509. struct address_space *mapping;
  510. if (inode->i_state &
  511. (I_FREEING|I_CLEAR|I_WILL_FREE|I_NEW))
  512. continue;
  513. mapping = inode->i_mapping;
  514. if (mapping->nrpages == 0)
  515. continue;
  516. __iget(inode);
  517. spin_unlock(&inode_lock);
  518. /*
  519. * We hold a reference to 'inode' so it couldn't have
  520. * been removed from s_inodes list while we dropped the
  521. * inode_lock. We cannot iput the inode now as we can
  522. * be holding the last reference and we cannot iput it
  523. * under inode_lock. So we keep the reference and iput
  524. * it later.
  525. */
  526. iput(old_inode);
  527. old_inode = inode;
  528. filemap_fdatawait(mapping);
  529. cond_resched();
  530. spin_lock(&inode_lock);
  531. }
  532. spin_unlock(&inode_lock);
  533. iput(old_inode);
  534. } else
  535. spin_unlock(&inode_lock);
  536. return; /* Leave any unwritten inodes on s_io */
  537. }
  538. EXPORT_SYMBOL_GPL(generic_sync_sb_inodes);
  539. static void sync_sb_inodes(struct super_block *sb,
  540. struct writeback_control *wbc)
  541. {
  542. generic_sync_sb_inodes(sb, wbc);
  543. }
  544. /*
  545. * Start writeback of dirty pagecache data against all unlocked inodes.
  546. *
  547. * Note:
  548. * We don't need to grab a reference to superblock here. If it has non-empty
  549. * ->s_dirty it's hadn't been killed yet and kill_super() won't proceed
  550. * past sync_inodes_sb() until the ->s_dirty/s_io/s_more_io lists are all
  551. * empty. Since __sync_single_inode() regains inode_lock before it finally moves
  552. * inode from superblock lists we are OK.
  553. *
  554. * If `older_than_this' is non-zero then only flush inodes which have a
  555. * flushtime older than *older_than_this.
  556. *
  557. * If `bdi' is non-zero then we will scan the first inode against each
  558. * superblock until we find the matching ones. One group will be the dirty
  559. * inodes against a filesystem. Then when we hit the dummy blockdev superblock,
  560. * sync_sb_inodes will seekout the blockdev which matches `bdi'. Maybe not
  561. * super-efficient but we're about to do a ton of I/O...
  562. */
  563. void
  564. writeback_inodes(struct writeback_control *wbc)
  565. {
  566. struct super_block *sb;
  567. might_sleep();
  568. spin_lock(&sb_lock);
  569. restart:
  570. list_for_each_entry_reverse(sb, &super_blocks, s_list) {
  571. if (sb_has_dirty_inodes(sb)) {
  572. /* we're making our own get_super here */
  573. sb->s_count++;
  574. spin_unlock(&sb_lock);
  575. /*
  576. * If we can't get the readlock, there's no sense in
  577. * waiting around, most of the time the FS is going to
  578. * be unmounted by the time it is released.
  579. */
  580. if (down_read_trylock(&sb->s_umount)) {
  581. if (sb->s_root)
  582. sync_sb_inodes(sb, wbc);
  583. up_read(&sb->s_umount);
  584. }
  585. spin_lock(&sb_lock);
  586. if (__put_super_and_need_restart(sb))
  587. goto restart;
  588. }
  589. if (wbc->nr_to_write <= 0)
  590. break;
  591. }
  592. spin_unlock(&sb_lock);
  593. }
  594. /*
  595. * writeback and wait upon the filesystem's dirty inodes. The caller will
  596. * do this in two passes - one to write, and one to wait.
  597. *
  598. * A finite limit is set on the number of pages which will be written.
  599. * To prevent infinite livelock of sys_sync().
  600. *
  601. * We add in the number of potentially dirty inodes, because each inode write
  602. * can dirty pagecache in the underlying blockdev.
  603. */
  604. void sync_inodes_sb(struct super_block *sb, int wait)
  605. {
  606. struct writeback_control wbc = {
  607. .sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_NONE,
  608. .range_start = 0,
  609. .range_end = LLONG_MAX,
  610. };
  611. if (!wait) {
  612. unsigned long nr_dirty = global_page_state(NR_FILE_DIRTY);
  613. unsigned long nr_unstable = global_page_state(NR_UNSTABLE_NFS);
  614. wbc.nr_to_write = nr_dirty + nr_unstable +
  615. (inodes_stat.nr_inodes - inodes_stat.nr_unused);
  616. } else
  617. wbc.nr_to_write = LONG_MAX; /* doesn't actually matter */
  618. sync_sb_inodes(sb, &wbc);
  619. }
  620. /**
  621. * write_inode_now - write an inode to disk
  622. * @inode: inode to write to disk
  623. * @sync: whether the write should be synchronous or not
  624. *
  625. * This function commits an inode to disk immediately if it is dirty. This is
  626. * primarily needed by knfsd.
  627. *
  628. * The caller must either have a ref on the inode or must have set I_WILL_FREE.
  629. */
  630. int write_inode_now(struct inode *inode, int sync)
  631. {
  632. int ret;
  633. struct writeback_control wbc = {
  634. .nr_to_write = LONG_MAX,
  635. .sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE,
  636. .range_start = 0,
  637. .range_end = LLONG_MAX,
  638. };
  639. if (!mapping_cap_writeback_dirty(inode->i_mapping))
  640. wbc.nr_to_write = 0;
  641. might_sleep();
  642. spin_lock(&inode_lock);
  643. ret = writeback_single_inode(inode, &wbc);
  644. spin_unlock(&inode_lock);
  645. if (sync)
  646. inode_sync_wait(inode);
  647. return ret;
  648. }
  649. EXPORT_SYMBOL(write_inode_now);
  650. /**
  651. * sync_inode - write an inode and its pages to disk.
  652. * @inode: the inode to sync
  653. * @wbc: controls the writeback mode
  654. *
  655. * sync_inode() will write an inode and its pages to disk. It will also
  656. * correctly update the inode on its superblock's dirty inode lists and will
  657. * update inode->i_state.
  658. *
  659. * The caller must have a ref on the inode.
  660. */
  661. int sync_inode(struct inode *inode, struct writeback_control *wbc)
  662. {
  663. int ret;
  664. spin_lock(&inode_lock);
  665. ret = writeback_single_inode(inode, wbc);
  666. spin_unlock(&inode_lock);
  667. return ret;
  668. }
  669. EXPORT_SYMBOL(sync_inode);
  670. /**
  671. * generic_osync_inode - flush all dirty data for a given inode to disk
  672. * @inode: inode to write
  673. * @mapping: the address_space that should be flushed
  674. * @what: what to write and wait upon
  675. *
  676. * This can be called by file_write functions for files which have the
  677. * O_SYNC flag set, to flush dirty writes to disk.
  678. *
  679. * @what is a bitmask, specifying which part of the inode's data should be
  680. * written and waited upon.
  681. *
  682. * OSYNC_DATA: i_mapping's dirty data
  683. * OSYNC_METADATA: the buffers at i_mapping->private_list
  684. * OSYNC_INODE: the inode itself
  685. */
  686. int generic_osync_inode(struct inode *inode, struct address_space *mapping, int what)
  687. {
  688. int err = 0;
  689. int need_write_inode_now = 0;
  690. int err2;
  691. if (what & OSYNC_DATA)
  692. err = filemap_fdatawrite(mapping);
  693. if (what & (OSYNC_METADATA|OSYNC_DATA)) {
  694. err2 = sync_mapping_buffers(mapping);
  695. if (!err)
  696. err = err2;
  697. }
  698. if (what & OSYNC_DATA) {
  699. err2 = filemap_fdatawait(mapping);
  700. if (!err)
  701. err = err2;
  702. }
  703. spin_lock(&inode_lock);
  704. if ((inode->i_state & I_DIRTY) &&
  705. ((what & OSYNC_INODE) || (inode->i_state & I_DIRTY_DATASYNC)))
  706. need_write_inode_now = 1;
  707. spin_unlock(&inode_lock);
  708. if (need_write_inode_now) {
  709. err2 = write_inode_now(inode, 1);
  710. if (!err)
  711. err = err2;
  712. }
  713. else
  714. inode_sync_wait(inode);
  715. return err;
  716. }
  717. EXPORT_SYMBOL(generic_osync_inode);