fs-writeback.c 23 KB

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