^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) * High-level sync()-related operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/namei.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/sched/xacct.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/writeback.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/syscalls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/linkage.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/quotaops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/backing-dev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define VALID_FLAGS (SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE| \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) SYNC_FILE_RANGE_WAIT_AFTER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * Do the filesystem syncing work. For simple filesystems
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * writeback_inodes_sb(sb) just dirties buffers with inodes so we have to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * submit IO for these buffers via __sync_blockdev(). This also speeds up the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * wait == 1 case since in that case write_inode() functions do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * sync_dirty_buffer() and thus effectively write one block at a time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static int __sync_filesystem(struct super_block *sb, int wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) sync_inodes_sb(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) writeback_inodes_sb(sb, WB_REASON_SYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (sb->s_op->sync_fs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) sb->s_op->sync_fs(sb, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return __sync_blockdev(sb->s_bdev, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * Write out and wait upon all dirty data associated with this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * superblock. Filesystem data as well as the underlying block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * device. Takes the superblock lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) int sync_filesystem(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * We need to be protected against the filesystem going from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * r/o to r/w or vice versa.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) WARN_ON(!rwsem_is_locked(&sb->s_umount));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * No point in syncing out anything if the filesystem is read-only.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (sb_rdonly(sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) ret = __sync_filesystem(sb, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return __sync_filesystem(sb, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) EXPORT_SYMBOL_NS(sync_filesystem, ANDROID_GKI_VFS_EXPORT_ONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static void sync_inodes_one_sb(struct super_block *sb, void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (!sb_rdonly(sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) sync_inodes_sb(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) static void sync_fs_one_sb(struct super_block *sb, void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (!sb_rdonly(sb) && !(sb->s_iflags & SB_I_SKIP_SYNC) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) sb->s_op->sync_fs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) sb->s_op->sync_fs(sb, *(int *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) static void fdatawrite_one_bdev(struct block_device *bdev, void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) filemap_fdatawrite(bdev->bd_inode->i_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static void fdatawait_one_bdev(struct block_device *bdev, void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * We keep the error status of individual mapping so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * applications can catch the writeback error using fsync(2).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * See filemap_fdatawait_keep_errors() for details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) filemap_fdatawait_keep_errors(bdev->bd_inode->i_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * Sync everything. We start by waking flusher threads so that most of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * writeback runs on all devices in parallel. Then we sync all inodes reliably
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * which effectively also waits for all flusher threads to finish doing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * writeback. At this point all data is on disk so metadata should be stable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * and we tell filesystems to sync their metadata via ->sync_fs() calls.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * Finally, we writeout all block devices because some filesystems (e.g. ext2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * just write metadata (such as inodes or bitmaps) to block device page cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * and do not sync it on their own in ->sync_fs().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) void ksys_sync(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) int nowait = 0, wait = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) wakeup_flusher_threads(WB_REASON_SYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) iterate_supers(sync_inodes_one_sb, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) iterate_supers(sync_fs_one_sb, &nowait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) iterate_supers(sync_fs_one_sb, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) iterate_bdevs(fdatawrite_one_bdev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) iterate_bdevs(fdatawait_one_bdev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (unlikely(laptop_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) laptop_sync_completion();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) SYSCALL_DEFINE0(sync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) ksys_sync();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) static void do_sync_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) int nowait = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * Sync twice to reduce the possibility we skipped some inodes / pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * because they were temporarily locked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) iterate_supers(sync_inodes_one_sb, &nowait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) iterate_supers(sync_fs_one_sb, &nowait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) iterate_bdevs(fdatawrite_one_bdev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) iterate_supers(sync_inodes_one_sb, &nowait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) iterate_supers(sync_fs_one_sb, &nowait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) iterate_bdevs(fdatawrite_one_bdev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) printk("Emergency Sync complete\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) kfree(work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) void emergency_sync(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) struct work_struct *work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) work = kmalloc(sizeof(*work), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (work) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) INIT_WORK(work, do_sync_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) schedule_work(work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) * sync a single super
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) SYSCALL_DEFINE1(syncfs, int, fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) struct fd f = fdget(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) struct super_block *sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) int ret, ret2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) if (!f.file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) return -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) sb = f.file->f_path.dentry->d_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) down_read(&sb->s_umount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) ret = sync_filesystem(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) up_read(&sb->s_umount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) ret2 = errseq_check_and_advance(&sb->s_wb_err, &f.file->f_sb_err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) fdput(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return ret ? ret : ret2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * vfs_fsync_range - helper to sync a range of data & metadata to disk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) * @file: file to sync
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) * @start: offset in bytes of the beginning of data range to sync
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * @end: offset in bytes of the end of data range (inclusive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * @datasync: perform only datasync
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) * Write back data in range @start..@end and metadata for @file to disk. If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * @datasync is set only metadata needed to access modified file data is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) * written.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) int vfs_fsync_range(struct file *file, loff_t start, loff_t end, int datasync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) struct inode *inode = file->f_mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (!file->f_op->fsync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (!datasync && (inode->i_state & I_DIRTY_TIME))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) mark_inode_dirty_sync(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) return file->f_op->fsync(file, start, end, datasync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) EXPORT_SYMBOL(vfs_fsync_range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * vfs_fsync - perform a fsync or fdatasync on a file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) * @file: file to sync
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * @datasync: only perform a fdatasync operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * Write back data and metadata for @file to disk. If @datasync is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * set only metadata needed to access modified file data is written.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) int vfs_fsync(struct file *file, int datasync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) return vfs_fsync_range(file, 0, LLONG_MAX, datasync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) EXPORT_SYMBOL(vfs_fsync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static int do_fsync(unsigned int fd, int datasync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) struct fd f = fdget(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) int ret = -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) if (f.file) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) ret = vfs_fsync(f.file, datasync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) fdput(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) inc_syscfs(current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) SYSCALL_DEFINE1(fsync, unsigned int, fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) return do_fsync(fd, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) SYSCALL_DEFINE1(fdatasync, unsigned int, fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) return do_fsync(fd, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) int sync_file_range(struct file *file, loff_t offset, loff_t nbytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) struct address_space *mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) loff_t endbyte; /* inclusive */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) umode_t i_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) if (flags & ~VALID_FLAGS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) endbyte = offset + nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) if ((s64)offset < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if ((s64)endbyte < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) if (endbyte < offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) if (sizeof(pgoff_t) == 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) if (offset >= (0x100000000ULL << PAGE_SHIFT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) * The range starts outside a 32 bit machine's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) * pagecache addressing capabilities. Let it "succeed"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) if (endbyte >= (0x100000000ULL << PAGE_SHIFT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) * Out to EOF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) nbytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) if (nbytes == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) endbyte = LLONG_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) endbyte--; /* inclusive */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) i_mode = file_inode(file)->i_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) ret = -ESPIPE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) if (!S_ISREG(i_mode) && !S_ISBLK(i_mode) && !S_ISDIR(i_mode) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) !S_ISLNK(i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) mapping = file->f_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) if (flags & SYNC_FILE_RANGE_WAIT_BEFORE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) ret = file_fdatawait_range(file, offset, endbyte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) if (flags & SYNC_FILE_RANGE_WRITE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) int sync_mode = WB_SYNC_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) if ((flags & SYNC_FILE_RANGE_WRITE_AND_WAIT) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) SYNC_FILE_RANGE_WRITE_AND_WAIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) sync_mode = WB_SYNC_ALL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) ret = __filemap_fdatawrite_range(mapping, offset, endbyte,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) sync_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (flags & SYNC_FILE_RANGE_WAIT_AFTER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) ret = file_fdatawait_range(file, offset, endbyte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) * ksys_sync_file_range() permits finely controlled syncing over a segment of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) * a file in the range offset .. (offset+nbytes-1) inclusive. If nbytes is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) * zero then ksys_sync_file_range() will operate from offset out to EOF.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * The flag bits are:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) * SYNC_FILE_RANGE_WAIT_BEFORE: wait upon writeout of all pages in the range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) * before performing the write.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) * SYNC_FILE_RANGE_WRITE: initiate writeout of all those dirty pages in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) * range which are not presently under writeback. Note that this may block for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) * significant periods due to exhaustion of disk request structures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) * SYNC_FILE_RANGE_WAIT_AFTER: wait upon writeout of all pages in the range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) * after performing the write.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) * Useful combinations of the flag bits are:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE: ensures that all pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) * in the range which were dirty on entry to ksys_sync_file_range() are placed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) * under writeout. This is a start-write-for-data-integrity operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) * SYNC_FILE_RANGE_WRITE: start writeout of all dirty pages in the range which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) * are not presently under writeout. This is an asynchronous flush-to-disk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) * operation. Not suitable for data integrity operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) * SYNC_FILE_RANGE_WAIT_BEFORE (or SYNC_FILE_RANGE_WAIT_AFTER): wait for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) * completion of writeout of all pages in the range. This will be used after an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) * earlier SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE operation to wait
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) * for that operation to complete and to return the result.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE|SYNC_FILE_RANGE_WAIT_AFTER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) * (a.k.a. SYNC_FILE_RANGE_WRITE_AND_WAIT):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) * a traditional sync() operation. This is a write-for-data-integrity operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) * which will ensure that all pages in the range which were dirty on entry to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) * ksys_sync_file_range() are written to disk. It should be noted that disk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) * caches are not flushed by this call, so there are no guarantees here that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) * data will be available on disk after a crash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) * SYNC_FILE_RANGE_WAIT_BEFORE and SYNC_FILE_RANGE_WAIT_AFTER will detect any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) * I/O errors or ENOSPC conditions and will return those to the caller, after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) * clearing the EIO and ENOSPC flags in the address_space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) * It should be noted that none of these operations write out the file's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) * metadata. So unless the application is strictly performing overwrites of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) * already-instantiated disk blocks, there are no guarantees here that the data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) * will be available after a crash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) int ksys_sync_file_range(int fd, loff_t offset, loff_t nbytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) struct fd f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) ret = -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) f = fdget(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) if (f.file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) ret = sync_file_range(f.file, offset, nbytes, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) fdput(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) SYSCALL_DEFINE4(sync_file_range, int, fd, loff_t, offset, loff_t, nbytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) unsigned int, flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) return ksys_sync_file_range(fd, offset, nbytes, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) /* It would be nice if people remember that not all the world's an i386
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) when they introduce new system calls */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) SYSCALL_DEFINE4(sync_file_range2, int, fd, unsigned int, flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) loff_t, offset, loff_t, nbytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) return ksys_sync_file_range(fd, offset, nbytes, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }