fs-writeback.c 20 KB

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