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