fsync.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * linux/fs/ext4/fsync.c
  3. *
  4. * Copyright (C) 1993 Stephen Tweedie (sct@redhat.com)
  5. * from
  6. * Copyright (C) 1992 Remy Card (card@masi.ibp.fr)
  7. * Laboratoire MASI - Institut Blaise Pascal
  8. * Universite Pierre et Marie Curie (Paris VI)
  9. * from
  10. * linux/fs/minix/truncate.c Copyright (C) 1991, 1992 Linus Torvalds
  11. *
  12. * ext4fs fsync primitive
  13. *
  14. * Big-endian to little-endian byte-swapping/bitmaps by
  15. * David S. Miller (davem@caip.rutgers.edu), 1995
  16. *
  17. * Removed unnecessary code duplication for little endian machines
  18. * and excessive __inline__s.
  19. * Andi Kleen, 1997
  20. *
  21. * Major simplications and cleanup - we only need to do the metadata, because
  22. * we can depend on generic_block_fdatasync() to sync the data blocks.
  23. */
  24. #include <linux/time.h>
  25. #include <linux/fs.h>
  26. #include <linux/sched.h>
  27. #include <linux/writeback.h>
  28. #include <linux/jbd2.h>
  29. #include <linux/blkdev.h>
  30. #include "ext4.h"
  31. #include "ext4_jbd2.h"
  32. #include <trace/events/ext4.h>
  33. /*
  34. * akpm: A new design for ext4_sync_file().
  35. *
  36. * This is only called from sys_fsync(), sys_fdatasync() and sys_msync().
  37. * There cannot be a transaction open by this task.
  38. * Another task could have dirtied this inode. Its data can be in any
  39. * state in the journalling system.
  40. *
  41. * What we do is just kick off a commit and wait on it. This will snapshot the
  42. * inode to disk.
  43. *
  44. * i_mutex lock is held when entering and exiting this function
  45. */
  46. int ext4_sync_file(struct file *file, struct dentry *dentry, int datasync)
  47. {
  48. struct inode *inode = dentry->d_inode;
  49. struct ext4_inode_info *ei = EXT4_I(inode);
  50. journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
  51. int ret;
  52. tid_t commit_tid;
  53. J_ASSERT(ext4_journal_current_handle() == NULL);
  54. trace_ext4_sync_file(file, dentry, datasync);
  55. if (inode->i_sb->s_flags & MS_RDONLY)
  56. return 0;
  57. ret = flush_completed_IO(inode);
  58. if (ret < 0)
  59. return ret;
  60. if (!journal)
  61. return simple_fsync(file, dentry, datasync);
  62. /*
  63. * data=writeback,ordered:
  64. * The caller's filemap_fdatawrite()/wait will sync the data.
  65. * Metadata is in the journal, we wait for proper transaction to
  66. * commit here.
  67. *
  68. * data=journal:
  69. * filemap_fdatawrite won't do anything (the buffers are clean).
  70. * ext4_force_commit will write the file data into the journal and
  71. * will wait on that.
  72. * filemap_fdatawait() will encounter a ton of newly-dirtied pages
  73. * (they were dirtied by commit). But that's OK - the blocks are
  74. * safe in-journal, which is all fsync() needs to ensure.
  75. */
  76. if (ext4_should_journal_data(inode))
  77. return ext4_force_commit(inode->i_sb);
  78. commit_tid = datasync ? ei->i_datasync_tid : ei->i_sync_tid;
  79. if (jbd2_log_start_commit(journal, commit_tid)) {
  80. /*
  81. * When the journal is on a different device than the
  82. * fs data disk, we need to issue the barrier in
  83. * writeback mode. (In ordered mode, the jbd2 layer
  84. * will take care of issuing the barrier. In
  85. * data=journal, all of the data blocks are written to
  86. * the journal device.)
  87. */
  88. if (ext4_should_writeback_data(inode) &&
  89. (journal->j_fs_dev != journal->j_dev) &&
  90. (journal->j_flags & JBD2_BARRIER))
  91. blkdev_issue_flush(inode->i_sb->s_bdev, NULL);
  92. jbd2_log_wait_commit(journal, commit_tid);
  93. } else if (journal->j_flags & JBD2_BARRIER)
  94. blkdev_issue_flush(inode->i_sb->s_bdev, NULL);
  95. return ret;
  96. }