fs-writeback.c 20 KB

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