fsync.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. * If we're not journaling and this is a just-created file, we have to
  35. * sync our parent directory (if it was freshly created) since
  36. * otherwise it will only be written by writeback, leaving a huge
  37. * window during which a crash may lose the file. This may apply for
  38. * the parent directory's parent as well, and so on recursively, if
  39. * they are also freshly created.
  40. */
  41. static void ext4_sync_parent(struct inode *inode)
  42. {
  43. struct dentry *dentry = NULL;
  44. while (inode && ext4_test_inode_state(inode, EXT4_STATE_NEWENTRY)) {
  45. ext4_clear_inode_state(inode, EXT4_STATE_NEWENTRY);
  46. dentry = list_entry(inode->i_dentry.next,
  47. struct dentry, d_alias);
  48. if (!dentry || !dentry->d_parent || !dentry->d_parent->d_inode)
  49. break;
  50. inode = dentry->d_parent->d_inode;
  51. sync_mapping_buffers(inode->i_mapping);
  52. }
  53. }
  54. /*
  55. * akpm: A new design for ext4_sync_file().
  56. *
  57. * This is only called from sys_fsync(), sys_fdatasync() and sys_msync().
  58. * There cannot be a transaction open by this task.
  59. * Another task could have dirtied this inode. Its data can be in any
  60. * state in the journalling system.
  61. *
  62. * What we do is just kick off a commit and wait on it. This will snapshot the
  63. * inode to disk.
  64. *
  65. * i_mutex lock is held when entering and exiting this function
  66. */
  67. int ext4_sync_file(struct file *file, int datasync)
  68. {
  69. struct inode *inode = file->f_mapping->host;
  70. struct ext4_inode_info *ei = EXT4_I(inode);
  71. journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
  72. int ret;
  73. tid_t commit_tid;
  74. J_ASSERT(ext4_journal_current_handle() == NULL);
  75. trace_ext4_sync_file(file, datasync);
  76. if (inode->i_sb->s_flags & MS_RDONLY)
  77. return 0;
  78. ret = flush_completed_IO(inode);
  79. if (ret < 0)
  80. return ret;
  81. if (!journal) {
  82. ret = generic_file_fsync(file, datasync);
  83. if (!ret && !list_empty(&inode->i_dentry))
  84. ext4_sync_parent(inode);
  85. return ret;
  86. }
  87. /*
  88. * data=writeback,ordered:
  89. * The caller's filemap_fdatawrite()/wait will sync the data.
  90. * Metadata is in the journal, we wait for proper transaction to
  91. * commit here.
  92. *
  93. * data=journal:
  94. * filemap_fdatawrite won't do anything (the buffers are clean).
  95. * ext4_force_commit will write the file data into the journal and
  96. * will wait on that.
  97. * filemap_fdatawait() will encounter a ton of newly-dirtied pages
  98. * (they were dirtied by commit). But that's OK - the blocks are
  99. * safe in-journal, which is all fsync() needs to ensure.
  100. */
  101. if (ext4_should_journal_data(inode))
  102. return ext4_force_commit(inode->i_sb);
  103. commit_tid = datasync ? ei->i_datasync_tid : ei->i_sync_tid;
  104. if (jbd2_log_start_commit(journal, commit_tid)) {
  105. /*
  106. * When the journal is on a different device than the
  107. * fs data disk, we need to issue the barrier in
  108. * writeback mode. (In ordered mode, the jbd2 layer
  109. * will take care of issuing the barrier. In
  110. * data=journal, all of the data blocks are written to
  111. * the journal device.)
  112. */
  113. if (ext4_should_writeback_data(inode) &&
  114. (journal->j_fs_dev != journal->j_dev) &&
  115. (journal->j_flags & JBD2_BARRIER))
  116. blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL,
  117. NULL, BLKDEV_IFL_WAIT);
  118. ret = jbd2_log_wait_commit(journal, commit_tid);
  119. } else if (journal->j_flags & JBD2_BARRIER)
  120. blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL,
  121. BLKDEV_IFL_WAIT);
  122. return ret;
  123. }