^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * linux/fs/ext4/fsync.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 1993 Stephen Tweedie (sct@redhat.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (C) 1992 Remy Card (card@masi.ibp.fr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Laboratoire MASI - Institut Blaise Pascal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Universite Pierre et Marie Curie (Paris VI)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * linux/fs/minix/truncate.c Copyright (C) 1991, 1992 Linus Torvalds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * ext4fs fsync primitive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * Big-endian to little-endian byte-swapping/bitmaps by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * David S. Miller (davem@caip.rutgers.edu), 1995
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * Removed unnecessary code duplication for little endian machines
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * and excessive __inline__s.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * Andi Kleen, 1997
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * Major simplications and cleanup - we only need to do the metadata, because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * we can depend on generic_block_fdatasync() to sync the data blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/writeback.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include "ext4.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include "ext4_jbd2.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <trace/events/ext4.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * If we're not journaling and this is a just-created file, we have to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * sync our parent directory (if it was freshly created) since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * otherwise it will only be written by writeback, leaving a huge
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * window during which a crash may lose the file. This may apply for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * the parent directory's parent as well, and so on recursively, if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * they are also freshly created.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static int ext4_sync_parent(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct dentry *dentry, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (!ext4_test_inode_state(inode, EXT4_STATE_NEWENTRY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) dentry = d_find_any_alias(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (!dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) while (ext4_test_inode_state(inode, EXT4_STATE_NEWENTRY)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) ext4_clear_inode_state(inode, EXT4_STATE_NEWENTRY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) next = dget_parent(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) dput(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) dentry = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) inode = dentry->d_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * The directory inode may have gone through rmdir by now. But
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * the inode itself and its blocks are still allocated (we hold
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * a reference to the inode via its dentry), so it didn't go
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * through ext4_evict_inode()) and so we are safe to flush
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * metadata blocks and the inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) ret = sync_mapping_buffers(inode->i_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) ret = sync_inode_metadata(inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) dput(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static int ext4_fsync_nojournal(struct inode *inode, bool datasync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) bool *needs_barrier)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) int ret, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) ret = sync_mapping_buffers(inode->i_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (!(inode->i_state & I_DIRTY_ALL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) err = sync_inode_metadata(inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) ret = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) ret = ext4_sync_parent(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (test_opt(inode->i_sb, BARRIER))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) *needs_barrier = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static int ext4_fsync_journal(struct inode *inode, bool datasync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) bool *needs_barrier)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) struct ext4_inode_info *ei = EXT4_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) tid_t commit_tid = datasync ? ei->i_datasync_tid : ei->i_sync_tid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (journal->j_flags & JBD2_BARRIER &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) !jbd2_trans_will_send_data_barrier(journal, commit_tid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) *needs_barrier = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return ext4_fc_commit(journal, commit_tid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * akpm: A new design for ext4_sync_file().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * This is only called from sys_fsync(), sys_fdatasync() and sys_msync().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * There cannot be a transaction open by this task.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * Another task could have dirtied this inode. Its data can be in any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * state in the journalling system.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * What we do is just kick off a commit and wait on it. This will snapshot the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * inode to disk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) int ext4_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) int ret = 0, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) bool needs_barrier = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) struct inode *inode = file->f_mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (unlikely(ext4_forced_shutdown(sbi)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) J_ASSERT(ext4_journal_current_handle() == NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) trace_ext4_sync_file_enter(file, datasync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (sb_rdonly(inode->i_sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /* Make sure that we read updated s_mount_flags value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) smp_rmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (ext4_test_mount_flag(inode->i_sb, EXT4_MF_FS_ABORTED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) ret = -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) ret = file_write_and_wait_range(file, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * data=writeback,ordered:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * The caller's filemap_fdatawrite()/wait will sync the data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * Metadata is in the journal, we wait for proper transaction to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) * commit here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * data=journal:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * filemap_fdatawrite won't do anything (the buffers are clean).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * ext4_force_commit will write the file data into the journal and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * will wait on that.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) * filemap_fdatawait() will encounter a ton of newly-dirtied pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * (they were dirtied by commit). But that's OK - the blocks are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * safe in-journal, which is all fsync() needs to ensure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (!sbi->s_journal)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) ret = ext4_fsync_nojournal(inode, datasync, &needs_barrier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) else if (ext4_should_journal_data(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) ret = ext4_force_commit(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) ret = ext4_fsync_journal(inode, datasync, &needs_barrier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) if (needs_barrier) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) err = blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) ret = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) err = file_check_and_advance_wb_err(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) ret = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) trace_ext4_sync_file_exit(inode, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }