fs-writeback.c 24 KB

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