fs-writeback.c 22 KB

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