fs-writeback.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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, current->pid, 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 locked, 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_LOCK)
  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 or s_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. * Redirty an inode, but mark it as the very next-to-be-written inode on its
  150. * superblock's dirty-inode list.
  151. * We need to preserve s_dirty's reverse-time-orderedness, so we cheat by
  152. * setting this inode's dirtied_when to the same value as that of the inode
  153. * which is presently head-of-list, if present head-of-list is newer than this
  154. * inode. (head-of-list is the least-recently-dirtied inode: the oldest one).
  155. */
  156. static void redirty_head(struct inode *inode)
  157. {
  158. struct super_block *sb = inode->i_sb;
  159. if (!list_empty(&sb->s_dirty)) {
  160. struct inode *head_inode;
  161. head_inode = list_entry(sb->s_dirty.prev, struct inode, i_list);
  162. if (time_after(inode->dirtied_when, head_inode->dirtied_when))
  163. inode->dirtied_when = head_inode->dirtied_when;
  164. }
  165. list_move_tail(&inode->i_list, &sb->s_dirty);
  166. }
  167. /*
  168. * Write a single inode's dirty pages and inode data out to disk.
  169. * If `wait' is set, wait on the writeout.
  170. *
  171. * The whole writeout design is quite complex and fragile. We want to avoid
  172. * starvation of particular inodes when others are being redirtied, prevent
  173. * livelocks, etc.
  174. *
  175. * Called under inode_lock.
  176. */
  177. static int
  178. __sync_single_inode(struct inode *inode, struct writeback_control *wbc)
  179. {
  180. unsigned dirty;
  181. struct address_space *mapping = inode->i_mapping;
  182. int wait = wbc->sync_mode == WB_SYNC_ALL;
  183. int ret;
  184. BUG_ON(inode->i_state & I_LOCK);
  185. /* Set I_LOCK, reset I_DIRTY */
  186. dirty = inode->i_state & I_DIRTY;
  187. inode->i_state |= I_LOCK;
  188. inode->i_state &= ~I_DIRTY;
  189. spin_unlock(&inode_lock);
  190. ret = do_writepages(mapping, wbc);
  191. /* Don't write the inode if only I_DIRTY_PAGES was set */
  192. if (dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
  193. int err = write_inode(inode, wait);
  194. if (ret == 0)
  195. ret = err;
  196. }
  197. if (wait) {
  198. int err = filemap_fdatawait(mapping);
  199. if (ret == 0)
  200. ret = err;
  201. }
  202. spin_lock(&inode_lock);
  203. inode->i_state &= ~I_LOCK;
  204. if (!(inode->i_state & I_FREEING)) {
  205. if (!(inode->i_state & I_DIRTY) &&
  206. mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
  207. /*
  208. * We didn't write back all the pages. nfs_writepages()
  209. * sometimes bales out without doing anything. Redirty
  210. * the inode. It is moved from s_io onto s_dirty.
  211. */
  212. /*
  213. * akpm: if the caller was the kupdate function we put
  214. * this inode at the head of s_dirty so it gets first
  215. * consideration. Otherwise, move it to the tail, for
  216. * the reasons described there. I'm not really sure
  217. * how much sense this makes. Presumably I had a good
  218. * reasons for doing it this way, and I'd rather not
  219. * muck with it at present.
  220. */
  221. if (wbc->for_kupdate) {
  222. /*
  223. * For the kupdate function we leave the inode
  224. * at the head of sb_dirty so it will get more
  225. * writeout as soon as the queue becomes
  226. * uncongested.
  227. */
  228. inode->i_state |= I_DIRTY_PAGES;
  229. redirty_head(inode);
  230. } else {
  231. /*
  232. * Otherwise fully redirty the inode so that
  233. * other inodes on this superblock will get some
  234. * writeout. Otherwise heavy writing to one
  235. * file would indefinitely suspend writeout of
  236. * all the other files.
  237. */
  238. inode->i_state |= I_DIRTY_PAGES;
  239. redirty_tail(inode);
  240. }
  241. } else if (inode->i_state & I_DIRTY) {
  242. /*
  243. * Someone redirtied the inode while were writing back
  244. * the pages.
  245. */
  246. redirty_tail(inode);
  247. } else if (atomic_read(&inode->i_count)) {
  248. /*
  249. * The inode is clean, inuse
  250. */
  251. list_move(&inode->i_list, &inode_in_use);
  252. } else {
  253. /*
  254. * The inode is clean, unused
  255. */
  256. list_move(&inode->i_list, &inode_unused);
  257. }
  258. }
  259. wake_up_inode(inode);
  260. return ret;
  261. }
  262. /*
  263. * Write out an inode's dirty pages. Called under inode_lock. Either the
  264. * caller has ref on the inode (either via __iget or via syscall against an fd)
  265. * or the inode has I_WILL_FREE set (via generic_forget_inode)
  266. */
  267. static int
  268. __writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
  269. {
  270. wait_queue_head_t *wqh;
  271. if (!atomic_read(&inode->i_count))
  272. WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING)));
  273. else
  274. WARN_ON(inode->i_state & I_WILL_FREE);
  275. if ((wbc->sync_mode != WB_SYNC_ALL) && (inode->i_state & I_LOCK)) {
  276. struct address_space *mapping = inode->i_mapping;
  277. int ret;
  278. redirty_tail(inode);
  279. /*
  280. * Even if we don't actually write the inode itself here,
  281. * we can at least start some of the data writeout..
  282. */
  283. spin_unlock(&inode_lock);
  284. ret = do_writepages(mapping, wbc);
  285. spin_lock(&inode_lock);
  286. return ret;
  287. }
  288. /*
  289. * It's a data-integrity sync. We must wait.
  290. */
  291. if (inode->i_state & I_LOCK) {
  292. DEFINE_WAIT_BIT(wq, &inode->i_state, __I_LOCK);
  293. wqh = bit_waitqueue(&inode->i_state, __I_LOCK);
  294. do {
  295. spin_unlock(&inode_lock);
  296. __wait_on_bit(wqh, &wq, inode_wait,
  297. TASK_UNINTERRUPTIBLE);
  298. spin_lock(&inode_lock);
  299. } while (inode->i_state & I_LOCK);
  300. }
  301. return __sync_single_inode(inode, wbc);
  302. }
  303. /*
  304. * Write out a superblock's list of dirty inodes. A wait will be performed
  305. * upon no inodes, all inodes or the final one, depending upon sync_mode.
  306. *
  307. * If older_than_this is non-NULL, then only write out inodes which
  308. * had their first dirtying at a time earlier than *older_than_this.
  309. *
  310. * If we're a pdlfush thread, then implement pdflush collision avoidance
  311. * against the entire list.
  312. *
  313. * WB_SYNC_HOLD is a hack for sys_sync(): reattach the inode to sb->s_dirty so
  314. * that it can be located for waiting on in __writeback_single_inode().
  315. *
  316. * Called under inode_lock.
  317. *
  318. * If `bdi' is non-zero then we're being asked to writeback a specific queue.
  319. * This function assumes that the blockdev superblock's inodes are backed by
  320. * a variety of queues, so all inodes are searched. For other superblocks,
  321. * assume that all inodes are backed by the same queue.
  322. *
  323. * FIXME: this linear search could get expensive with many fileystems. But
  324. * how to fix? We need to go from an address_space to all inodes which share
  325. * a queue with that address_space. (Easy: have a global "dirty superblocks"
  326. * list).
  327. *
  328. * The inodes to be written are parked on sb->s_io. They are moved back onto
  329. * sb->s_dirty as they are selected for writing. This way, none can be missed
  330. * on the writer throttling path, and we get decent balancing between many
  331. * throttled threads: we don't want them all piling up on __wait_on_inode.
  332. */
  333. static void
  334. sync_sb_inodes(struct super_block *sb, struct writeback_control *wbc)
  335. {
  336. const unsigned long start = jiffies; /* livelock avoidance */
  337. if (!wbc->for_kupdate || list_empty(&sb->s_io))
  338. list_splice_init(&sb->s_dirty, &sb->s_io);
  339. while (!list_empty(&sb->s_io)) {
  340. struct inode *inode = list_entry(sb->s_io.prev,
  341. struct inode, i_list);
  342. struct address_space *mapping = inode->i_mapping;
  343. struct backing_dev_info *bdi = mapping->backing_dev_info;
  344. long pages_skipped;
  345. if (!bdi_cap_writeback_dirty(bdi)) {
  346. redirty_tail(inode);
  347. if (sb_is_blkdev_sb(sb)) {
  348. /*
  349. * Dirty memory-backed blockdev: the ramdisk
  350. * driver does this. Skip just this inode
  351. */
  352. continue;
  353. }
  354. /*
  355. * Dirty memory-backed inode against a filesystem other
  356. * than the kernel-internal bdev filesystem. Skip the
  357. * entire superblock.
  358. */
  359. break;
  360. }
  361. if (wbc->nonblocking && bdi_write_congested(bdi)) {
  362. wbc->encountered_congestion = 1;
  363. if (!sb_is_blkdev_sb(sb))
  364. break; /* Skip a congested fs */
  365. list_move(&inode->i_list, &sb->s_dirty);
  366. continue; /* Skip a congested blockdev */
  367. }
  368. if (wbc->bdi && bdi != wbc->bdi) {
  369. if (!sb_is_blkdev_sb(sb))
  370. break; /* fs has the wrong queue */
  371. list_move(&inode->i_list, &sb->s_dirty);
  372. continue; /* blockdev has wrong queue */
  373. }
  374. /* Was this inode dirtied after sync_sb_inodes was called? */
  375. if (time_after(inode->dirtied_when, start))
  376. break;
  377. /* Was this inode dirtied too recently? */
  378. if (wbc->older_than_this && time_after(inode->dirtied_when,
  379. *wbc->older_than_this))
  380. break;
  381. /* Is another pdflush already flushing this queue? */
  382. if (current_is_pdflush() && !writeback_acquire(bdi))
  383. break;
  384. BUG_ON(inode->i_state & I_FREEING);
  385. __iget(inode);
  386. pages_skipped = wbc->pages_skipped;
  387. __writeback_single_inode(inode, wbc);
  388. if (wbc->sync_mode == WB_SYNC_HOLD) {
  389. inode->dirtied_when = jiffies;
  390. list_move(&inode->i_list, &sb->s_dirty);
  391. }
  392. if (current_is_pdflush())
  393. writeback_release(bdi);
  394. if (wbc->pages_skipped != pages_skipped) {
  395. /*
  396. * writeback is not making progress due to locked
  397. * buffers. Skip this inode for now.
  398. */
  399. redirty_tail(inode);
  400. }
  401. spin_unlock(&inode_lock);
  402. iput(inode);
  403. cond_resched();
  404. spin_lock(&inode_lock);
  405. if (wbc->nr_to_write <= 0)
  406. break;
  407. }
  408. return; /* Leave any unwritten inodes on s_io */
  409. }
  410. /*
  411. * Start writeback of dirty pagecache data against all unlocked inodes.
  412. *
  413. * Note:
  414. * We don't need to grab a reference to superblock here. If it has non-empty
  415. * ->s_dirty it's hadn't been killed yet and kill_super() won't proceed
  416. * past sync_inodes_sb() until both the ->s_dirty and ->s_io lists are
  417. * empty. Since __sync_single_inode() regains inode_lock before it finally moves
  418. * inode from superblock lists we are OK.
  419. *
  420. * If `older_than_this' is non-zero then only flush inodes which have a
  421. * flushtime older than *older_than_this.
  422. *
  423. * If `bdi' is non-zero then we will scan the first inode against each
  424. * superblock until we find the matching ones. One group will be the dirty
  425. * inodes against a filesystem. Then when we hit the dummy blockdev superblock,
  426. * sync_sb_inodes will seekout the blockdev which matches `bdi'. Maybe not
  427. * super-efficient but we're about to do a ton of I/O...
  428. */
  429. void
  430. writeback_inodes(struct writeback_control *wbc)
  431. {
  432. struct super_block *sb;
  433. might_sleep();
  434. spin_lock(&sb_lock);
  435. restart:
  436. sb = sb_entry(super_blocks.prev);
  437. for (; sb != sb_entry(&super_blocks); sb = sb_entry(sb->s_list.prev)) {
  438. if (!list_empty(&sb->s_dirty) || !list_empty(&sb->s_io)) {
  439. /* we're making our own get_super here */
  440. sb->s_count++;
  441. spin_unlock(&sb_lock);
  442. /*
  443. * If we can't get the readlock, there's no sense in
  444. * waiting around, most of the time the FS is going to
  445. * be unmounted by the time it is released.
  446. */
  447. if (down_read_trylock(&sb->s_umount)) {
  448. if (sb->s_root) {
  449. spin_lock(&inode_lock);
  450. sync_sb_inodes(sb, wbc);
  451. spin_unlock(&inode_lock);
  452. }
  453. up_read(&sb->s_umount);
  454. }
  455. spin_lock(&sb_lock);
  456. if (__put_super_and_need_restart(sb))
  457. goto restart;
  458. }
  459. if (wbc->nr_to_write <= 0)
  460. break;
  461. }
  462. spin_unlock(&sb_lock);
  463. }
  464. /*
  465. * writeback and wait upon the filesystem's dirty inodes. The caller will
  466. * do this in two passes - one to write, and one to wait. WB_SYNC_HOLD is
  467. * used to park the written inodes on sb->s_dirty for the wait pass.
  468. *
  469. * A finite limit is set on the number of pages which will be written.
  470. * To prevent infinite livelock of sys_sync().
  471. *
  472. * We add in the number of potentially dirty inodes, because each inode write
  473. * can dirty pagecache in the underlying blockdev.
  474. */
  475. void sync_inodes_sb(struct super_block *sb, int wait)
  476. {
  477. struct writeback_control wbc = {
  478. .sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_HOLD,
  479. .range_start = 0,
  480. .range_end = LLONG_MAX,
  481. };
  482. unsigned long nr_dirty = global_page_state(NR_FILE_DIRTY);
  483. unsigned long nr_unstable = global_page_state(NR_UNSTABLE_NFS);
  484. wbc.nr_to_write = nr_dirty + nr_unstable +
  485. (inodes_stat.nr_inodes - inodes_stat.nr_unused) +
  486. nr_dirty + nr_unstable;
  487. wbc.nr_to_write += wbc.nr_to_write / 2; /* Bit more for luck */
  488. spin_lock(&inode_lock);
  489. sync_sb_inodes(sb, &wbc);
  490. spin_unlock(&inode_lock);
  491. }
  492. /*
  493. * Rather lame livelock avoidance.
  494. */
  495. static void set_sb_syncing(int val)
  496. {
  497. struct super_block *sb;
  498. spin_lock(&sb_lock);
  499. sb = sb_entry(super_blocks.prev);
  500. for (; sb != sb_entry(&super_blocks); sb = sb_entry(sb->s_list.prev)) {
  501. sb->s_syncing = val;
  502. }
  503. spin_unlock(&sb_lock);
  504. }
  505. /**
  506. * sync_inodes - writes all inodes to disk
  507. * @wait: wait for completion
  508. *
  509. * sync_inodes() goes through each super block's dirty inode list, writes the
  510. * inodes out, waits on the writeout and puts the inodes back on the normal
  511. * list.
  512. *
  513. * This is for sys_sync(). fsync_dev() uses the same algorithm. The subtle
  514. * part of the sync functions is that the blockdev "superblock" is processed
  515. * last. This is because the write_inode() function of a typical fs will
  516. * perform no I/O, but will mark buffers in the blockdev mapping as dirty.
  517. * What we want to do is to perform all that dirtying first, and then write
  518. * back all those inode blocks via the blockdev mapping in one sweep. So the
  519. * additional (somewhat redundant) sync_blockdev() calls here are to make
  520. * sure that really happens. Because if we call sync_inodes_sb(wait=1) with
  521. * outstanding dirty inodes, the writeback goes block-at-a-time within the
  522. * filesystem's write_inode(). This is extremely slow.
  523. */
  524. static void __sync_inodes(int wait)
  525. {
  526. struct super_block *sb;
  527. spin_lock(&sb_lock);
  528. restart:
  529. list_for_each_entry(sb, &super_blocks, s_list) {
  530. if (sb->s_syncing)
  531. continue;
  532. sb->s_syncing = 1;
  533. sb->s_count++;
  534. spin_unlock(&sb_lock);
  535. down_read(&sb->s_umount);
  536. if (sb->s_root) {
  537. sync_inodes_sb(sb, wait);
  538. sync_blockdev(sb->s_bdev);
  539. }
  540. up_read(&sb->s_umount);
  541. spin_lock(&sb_lock);
  542. if (__put_super_and_need_restart(sb))
  543. goto restart;
  544. }
  545. spin_unlock(&sb_lock);
  546. }
  547. void sync_inodes(int wait)
  548. {
  549. set_sb_syncing(0);
  550. __sync_inodes(0);
  551. if (wait) {
  552. set_sb_syncing(0);
  553. __sync_inodes(1);
  554. }
  555. }
  556. /**
  557. * write_inode_now - write an inode to disk
  558. * @inode: inode to write to disk
  559. * @sync: whether the write should be synchronous or not
  560. *
  561. * This function commits an inode to disk immediately if it is dirty. This is
  562. * primarily needed by knfsd.
  563. *
  564. * The caller must either have a ref on the inode or must have set I_WILL_FREE.
  565. */
  566. int write_inode_now(struct inode *inode, int sync)
  567. {
  568. int ret;
  569. struct writeback_control wbc = {
  570. .nr_to_write = LONG_MAX,
  571. .sync_mode = WB_SYNC_ALL,
  572. .range_start = 0,
  573. .range_end = LLONG_MAX,
  574. };
  575. if (!mapping_cap_writeback_dirty(inode->i_mapping))
  576. wbc.nr_to_write = 0;
  577. might_sleep();
  578. spin_lock(&inode_lock);
  579. ret = __writeback_single_inode(inode, &wbc);
  580. spin_unlock(&inode_lock);
  581. if (sync)
  582. wait_on_inode(inode);
  583. return ret;
  584. }
  585. EXPORT_SYMBOL(write_inode_now);
  586. /**
  587. * sync_inode - write an inode and its pages to disk.
  588. * @inode: the inode to sync
  589. * @wbc: controls the writeback mode
  590. *
  591. * sync_inode() will write an inode and its pages to disk. It will also
  592. * correctly update the inode on its superblock's dirty inode lists and will
  593. * update inode->i_state.
  594. *
  595. * The caller must have a ref on the inode.
  596. */
  597. int sync_inode(struct inode *inode, struct writeback_control *wbc)
  598. {
  599. int ret;
  600. spin_lock(&inode_lock);
  601. ret = __writeback_single_inode(inode, wbc);
  602. spin_unlock(&inode_lock);
  603. return ret;
  604. }
  605. EXPORT_SYMBOL(sync_inode);
  606. /**
  607. * generic_osync_inode - flush all dirty data for a given inode to disk
  608. * @inode: inode to write
  609. * @mapping: the address_space that should be flushed
  610. * @what: what to write and wait upon
  611. *
  612. * This can be called by file_write functions for files which have the
  613. * O_SYNC flag set, to flush dirty writes to disk.
  614. *
  615. * @what is a bitmask, specifying which part of the inode's data should be
  616. * written and waited upon.
  617. *
  618. * OSYNC_DATA: i_mapping's dirty data
  619. * OSYNC_METADATA: the buffers at i_mapping->private_list
  620. * OSYNC_INODE: the inode itself
  621. */
  622. int generic_osync_inode(struct inode *inode, struct address_space *mapping, int what)
  623. {
  624. int err = 0;
  625. int need_write_inode_now = 0;
  626. int err2;
  627. if (what & OSYNC_DATA)
  628. err = filemap_fdatawrite(mapping);
  629. if (what & (OSYNC_METADATA|OSYNC_DATA)) {
  630. err2 = sync_mapping_buffers(mapping);
  631. if (!err)
  632. err = err2;
  633. }
  634. if (what & OSYNC_DATA) {
  635. err2 = filemap_fdatawait(mapping);
  636. if (!err)
  637. err = err2;
  638. }
  639. spin_lock(&inode_lock);
  640. if ((inode->i_state & I_DIRTY) &&
  641. ((what & OSYNC_INODE) || (inode->i_state & I_DIRTY_DATASYNC)))
  642. need_write_inode_now = 1;
  643. spin_unlock(&inode_lock);
  644. if (need_write_inode_now) {
  645. err2 = write_inode_now(inode, 1);
  646. if (!err)
  647. err = err2;
  648. }
  649. else
  650. wait_on_inode(inode);
  651. return err;
  652. }
  653. EXPORT_SYMBOL(generic_osync_inode);
  654. /**
  655. * writeback_acquire: attempt to get exclusive writeback access to a device
  656. * @bdi: the device's backing_dev_info structure
  657. *
  658. * It is a waste of resources to have more than one pdflush thread blocked on
  659. * a single request queue. Exclusion at the request_queue level is obtained
  660. * via a flag in the request_queue's backing_dev_info.state.
  661. *
  662. * Non-request_queue-backed address_spaces will share default_backing_dev_info,
  663. * unless they implement their own. Which is somewhat inefficient, as this
  664. * may prevent concurrent writeback against multiple devices.
  665. */
  666. int writeback_acquire(struct backing_dev_info *bdi)
  667. {
  668. return !test_and_set_bit(BDI_pdflush, &bdi->state);
  669. }
  670. /**
  671. * writeback_in_progress: determine whether there is writeback in progress
  672. * @bdi: the device's backing_dev_info structure.
  673. *
  674. * Determine whether there is writeback in progress against a backing device.
  675. */
  676. int writeback_in_progress(struct backing_dev_info *bdi)
  677. {
  678. return test_bit(BDI_pdflush, &bdi->state);
  679. }
  680. /**
  681. * writeback_release: relinquish exclusive writeback access against a device.
  682. * @bdi: the device's backing_dev_info structure
  683. */
  684. void writeback_release(struct backing_dev_info *bdi)
  685. {
  686. BUG_ON(!writeback_in_progress(bdi));
  687. clear_bit(BDI_pdflush, &bdi->state);
  688. }