Blame view

fs/sync.c 10.6 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
f79e2abb9   Andrew Morton   [PATCH] sys_sync_...
2
3
4
5
6
7
8
  /*
   * High-level sync()-related operations
   */
  
  #include <linux/kernel.h>
  #include <linux/file.h>
  #include <linux/fs.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
9
  #include <linux/slab.h>
630d9c472   Paul Gortmaker   fs: reduce the us...
10
  #include <linux/export.h>
b7ed78f56   Sage Weil   introduce sys_syn...
11
  #include <linux/namei.h>
ad4b32c45   Jin Qian   ANDROID: taskstat...
12
  #include <linux/sched/xacct.h>
f79e2abb9   Andrew Morton   [PATCH] sys_sync_...
13
14
15
16
  #include <linux/writeback.h>
  #include <linux/syscalls.h>
  #include <linux/linkage.h>
  #include <linux/pagemap.h>
cf9a2ae8d   David Howells   [PATCH] BLOCK: Mo...
17
  #include <linux/quotaops.h>
5129a469a   Jörn Engel   Catch filesystems...
18
  #include <linux/backing-dev.h>
5a3e5cb8e   Jan Kara   vfs: Fix sys_sync...
19
  #include "internal.h"
f79e2abb9   Andrew Morton   [PATCH] sys_sync_...
20
21
22
  
  #define VALID_FLAGS (SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE| \
  			SYNC_FILE_RANGE_WAIT_AFTER)
c15c54f5f   Jan Kara   vfs: Move syncing...
23
  /*
d8a8559cd   Jens Axboe   writeback: get ri...
24
25
26
27
28
   * Do the filesystem syncing work. For simple filesystems
   * writeback_inodes_sb(sb) just dirties buffers with inodes so we have to
   * submit IO for these buffers via __sync_blockdev(). This also speeds up the
   * wait == 1 case since in that case write_inode() functions do
   * sync_dirty_buffer() and thus effectively write one block at a time.
c15c54f5f   Jan Kara   vfs: Move syncing...
29
   */
0dc83bd30   Jan Kara   Revert "writeback...
30
  static int __sync_filesystem(struct super_block *sb, int wait)
c15c54f5f   Jan Kara   vfs: Move syncing...
31
  {
5fb324ad2   Christoph Hellwig   quota: move code ...
32
  	if (wait)
0dc83bd30   Jan Kara   Revert "writeback...
33
  		sync_inodes_sb(sb);
5fb324ad2   Christoph Hellwig   quota: move code ...
34
  	else
0e175a183   Curt Wohlgemuth   writeback: Add a ...
35
  		writeback_inodes_sb(sb, WB_REASON_SYNC);
5fb324ad2   Christoph Hellwig   quota: move code ...
36

c15c54f5f   Jan Kara   vfs: Move syncing...
37
38
39
40
41
42
43
44
45
46
  	if (sb->s_op->sync_fs)
  		sb->s_op->sync_fs(sb, wait);
  	return __sync_blockdev(sb->s_bdev, wait);
  }
  
  /*
   * Write out and wait upon all dirty data associated with this
   * superblock.  Filesystem data as well as the underlying block
   * device.  Takes the superblock lock.
   */
60b0680fa   Jan Kara   vfs: Rename fsync...
47
  int sync_filesystem(struct super_block *sb)
c15c54f5f   Jan Kara   vfs: Move syncing...
48
49
  {
  	int ret;
5af7926ff   Christoph Hellwig   enforce ->sync_fs...
50
51
52
53
54
55
56
57
58
  	/*
  	 * We need to be protected against the filesystem going from
  	 * r/o to r/w or vice versa.
  	 */
  	WARN_ON(!rwsem_is_locked(&sb->s_umount));
  
  	/*
  	 * No point in syncing out anything if the filesystem is read-only.
  	 */
bc98a42c1   David Howells   VFS: Convert sb->...
59
  	if (sb_rdonly(sb))
5af7926ff   Christoph Hellwig   enforce ->sync_fs...
60
  		return 0;
0dc83bd30   Jan Kara   Revert "writeback...
61
  	ret = __sync_filesystem(sb, 0);
c15c54f5f   Jan Kara   vfs: Move syncing...
62
63
  	if (ret < 0)
  		return ret;
0dc83bd30   Jan Kara   Revert "writeback...
64
  	return __sync_filesystem(sb, 1);
c15c54f5f   Jan Kara   vfs: Move syncing...
65
  }
10096fb10   Anton Altaparmakov   Export sync_files...
66
  EXPORT_SYMBOL(sync_filesystem);
c15c54f5f   Jan Kara   vfs: Move syncing...
67

b3de65310   Jan Kara   vfs: Reorder oper...
68
  static void sync_inodes_one_sb(struct super_block *sb, void *arg)
01a05b337   Al Viro   new helper: itera...
69
  {
bc98a42c1   David Howells   VFS: Convert sb->...
70
  	if (!sb_rdonly(sb))
0dc83bd30   Jan Kara   Revert "writeback...
71
  		sync_inodes_sb(sb);
01a05b337   Al Viro   new helper: itera...
72
  }
b3de65310   Jan Kara   vfs: Reorder oper...
73

b3de65310   Jan Kara   vfs: Reorder oper...
74
75
  static void sync_fs_one_sb(struct super_block *sb, void *arg)
  {
32b1924b2   Konstantin Khlebnikov   ovl: skip overlay...
76
77
  	if (!sb_rdonly(sb) && !(sb->s_iflags & SB_I_SKIP_SYNC) &&
  	    sb->s_op->sync_fs)
b3de65310   Jan Kara   vfs: Reorder oper...
78
79
  		sb->s_op->sync_fs(sb, *(int *)arg);
  }
d0e91b13e   Jan Kara   vfs: Remove unnec...
80
  static void fdatawrite_one_bdev(struct block_device *bdev, void *arg)
b3de65310   Jan Kara   vfs: Reorder oper...
81
  {
d0e91b13e   Jan Kara   vfs: Remove unnec...
82
  	filemap_fdatawrite(bdev->bd_inode->i_mapping);
a8c7176b6   Jan Kara   vfs: Make sys_syn...
83
  }
d0e91b13e   Jan Kara   vfs: Remove unnec...
84
  static void fdatawait_one_bdev(struct block_device *bdev, void *arg)
a8c7176b6   Jan Kara   vfs: Make sys_syn...
85
  {
aa750fd71   Junichi Nomura   mm/filemap.c: mak...
86
87
88
89
90
91
  	/*
  	 * We keep the error status of individual mapping so that
  	 * applications can catch the writeback error using fsync(2).
  	 * See filemap_fdatawait_keep_errors() for details.
  	 */
  	filemap_fdatawait_keep_errors(bdev->bd_inode->i_mapping);
c15c54f5f   Jan Kara   vfs: Move syncing...
92
  }
3beab0b42   Zhang, Yanmin   sys_sync(): fix 1...
93
  /*
4ea425b63   Jan Kara   vfs: Avoid unnece...
94
95
96
97
98
99
100
101
   * Sync everything. We start by waking flusher threads so that most of
   * writeback runs on all devices in parallel. Then we sync all inodes reliably
   * which effectively also waits for all flusher threads to finish doing
   * writeback. At this point all data is on disk so metadata should be stable
   * and we tell filesystems to sync their metadata via ->sync_fs() calls.
   * Finally, we writeout all block devices because some filesystems (e.g. ext2)
   * just write metadata (such as inodes or bitmaps) to block device page cache
   * and do not sync it on their own in ->sync_fs().
3beab0b42   Zhang, Yanmin   sys_sync(): fix 1...
102
   */
70f68ee81   Dominik Brodowski   fs: add ksys_sync...
103
  void ksys_sync(void)
cf9a2ae8d   David Howells   [PATCH] BLOCK: Mo...
104
  {
b3de65310   Jan Kara   vfs: Reorder oper...
105
  	int nowait = 0, wait = 1;
9ba4b2dfa   Jens Axboe   fs: kill 'nr_page...
106
  	wakeup_flusher_threads(WB_REASON_SYNC);
0dc83bd30   Jan Kara   Revert "writeback...
107
  	iterate_supers(sync_inodes_one_sb, NULL);
4ea425b63   Jan Kara   vfs: Avoid unnece...
108
  	iterate_supers(sync_fs_one_sb, &nowait);
b3de65310   Jan Kara   vfs: Reorder oper...
109
  	iterate_supers(sync_fs_one_sb, &wait);
d0e91b13e   Jan Kara   vfs: Remove unnec...
110
111
  	iterate_bdevs(fdatawrite_one_bdev, NULL);
  	iterate_bdevs(fdatawait_one_bdev, NULL);
cf9a2ae8d   David Howells   [PATCH] BLOCK: Mo...
112
113
  	if (unlikely(laptop_mode))
  		laptop_sync_completion();
70f68ee81   Dominik Brodowski   fs: add ksys_sync...
114
115
116
117
118
  }
  
  SYSCALL_DEFINE0(sync)
  {
  	ksys_sync();
cf9a2ae8d   David Howells   [PATCH] BLOCK: Mo...
119
120
  	return 0;
  }
a2a9537ac   Jens Axboe   Get rid of pdflus...
121
122
  static void do_sync_work(struct work_struct *work)
  {
b3de65310   Jan Kara   vfs: Reorder oper...
123
  	int nowait = 0;
5cee5815d   Jan Kara   vfs: Make sys_syn...
124
125
126
127
  	/*
  	 * Sync twice to reduce the possibility we skipped some inodes / pages
  	 * because they were temporarily locked
  	 */
b3de65310   Jan Kara   vfs: Reorder oper...
128
129
  	iterate_supers(sync_inodes_one_sb, &nowait);
  	iterate_supers(sync_fs_one_sb, &nowait);
d0e91b13e   Jan Kara   vfs: Remove unnec...
130
  	iterate_bdevs(fdatawrite_one_bdev, NULL);
b3de65310   Jan Kara   vfs: Reorder oper...
131
132
  	iterate_supers(sync_inodes_one_sb, &nowait);
  	iterate_supers(sync_fs_one_sb, &nowait);
d0e91b13e   Jan Kara   vfs: Remove unnec...
133
  	iterate_bdevs(fdatawrite_one_bdev, NULL);
5cee5815d   Jan Kara   vfs: Make sys_syn...
134
135
  	printk("Emergency Sync complete
  ");
a2a9537ac   Jens Axboe   Get rid of pdflus...
136
137
  	kfree(work);
  }
cf9a2ae8d   David Howells   [PATCH] BLOCK: Mo...
138
139
  void emergency_sync(void)
  {
a2a9537ac   Jens Axboe   Get rid of pdflus...
140
141
142
143
144
145
146
  	struct work_struct *work;
  
  	work = kmalloc(sizeof(*work), GFP_ATOMIC);
  	if (work) {
  		INIT_WORK(work, do_sync_work);
  		schedule_work(work);
  	}
cf9a2ae8d   David Howells   [PATCH] BLOCK: Mo...
147
  }
b7ed78f56   Sage Weil   introduce sys_syn...
148
149
150
151
152
  /*
   * sync a single super
   */
  SYSCALL_DEFINE1(syncfs, int, fd)
  {
2903ff019   Al Viro   switch simple cas...
153
  	struct fd f = fdget(fd);
b7ed78f56   Sage Weil   introduce sys_syn...
154
  	struct super_block *sb;
735e4ae5b   Jeff Layton   vfs: track per-sb...
155
  	int ret, ret2;
b7ed78f56   Sage Weil   introduce sys_syn...
156

2903ff019   Al Viro   switch simple cas...
157
  	if (!f.file)
b7ed78f56   Sage Weil   introduce sys_syn...
158
  		return -EBADF;
b583043e9   Al Viro   kill f_dentry uses
159
  	sb = f.file->f_path.dentry->d_sb;
b7ed78f56   Sage Weil   introduce sys_syn...
160
161
162
163
  
  	down_read(&sb->s_umount);
  	ret = sync_filesystem(sb);
  	up_read(&sb->s_umount);
735e4ae5b   Jeff Layton   vfs: track per-sb...
164
  	ret2 = errseq_check_and_advance(&sb->s_wb_err, &f.file->f_sb_err);
2903ff019   Al Viro   switch simple cas...
165
  	fdput(f);
735e4ae5b   Jeff Layton   vfs: track per-sb...
166
  	return ret ? ret : ret2;
b7ed78f56   Sage Weil   introduce sys_syn...
167
  }
4c728ef58   Christoph Hellwig   add a vfs_fsync h...
168
  /**
148f948ba   Jan Kara   vfs: Introduce ne...
169
   * vfs_fsync_range - helper to sync a range of data & metadata to disk
4c728ef58   Christoph Hellwig   add a vfs_fsync h...
170
   * @file:		file to sync
148f948ba   Jan Kara   vfs: Introduce ne...
171
172
173
   * @start:		offset in bytes of the beginning of data range to sync
   * @end:		offset in bytes of the end of data range (inclusive)
   * @datasync:		perform only datasync
4c728ef58   Christoph Hellwig   add a vfs_fsync h...
174
   *
148f948ba   Jan Kara   vfs: Introduce ne...
175
176
177
   * Write back data in range @start..@end and metadata for @file to disk.  If
   * @datasync is set only metadata needed to access modified file data is
   * written.
4c728ef58   Christoph Hellwig   add a vfs_fsync h...
178
   */
8018ab057   Christoph Hellwig   sanitize vfs_fsyn...
179
  int vfs_fsync_range(struct file *file, loff_t start, loff_t end, int datasync)
cf9a2ae8d   David Howells   [PATCH] BLOCK: Mo...
180
  {
0ae45f63d   Theodore Ts'o   vfs: add support ...
181
  	struct inode *inode = file->f_mapping->host;
72c2d5319   Al Viro   file->f_op is nev...
182
  	if (!file->f_op->fsync)
02c24a821   Josef Bacik   fs: push i_mutex ...
183
  		return -EINVAL;
0d07e5573   Christoph Hellwig   fs: don't clear I...
184
  	if (!datasync && (inode->i_state & I_DIRTY_TIME))
0ae45f63d   Theodore Ts'o   vfs: add support ...
185
  		mark_inode_dirty_sync(inode);
0f41074a6   Jeff Layton   fs: remove call_f...
186
  	return file->f_op->fsync(file, start, end, datasync);
cf9a2ae8d   David Howells   [PATCH] BLOCK: Mo...
187
  }
148f948ba   Jan Kara   vfs: Introduce ne...
188
189
190
191
192
  EXPORT_SYMBOL(vfs_fsync_range);
  
  /**
   * vfs_fsync - perform a fsync or fdatasync on a file
   * @file:		file to sync
148f948ba   Jan Kara   vfs: Introduce ne...
193
194
195
196
   * @datasync:		only perform a fdatasync operation
   *
   * Write back data and metadata for @file to disk.  If @datasync is
   * set only metadata needed to access modified file data is written.
148f948ba   Jan Kara   vfs: Introduce ne...
197
   */
8018ab057   Christoph Hellwig   sanitize vfs_fsyn...
198
  int vfs_fsync(struct file *file, int datasync)
148f948ba   Jan Kara   vfs: Introduce ne...
199
  {
8018ab057   Christoph Hellwig   sanitize vfs_fsyn...
200
  	return vfs_fsync_range(file, 0, LLONG_MAX, datasync);
148f948ba   Jan Kara   vfs: Introduce ne...
201
  }
4c728ef58   Christoph Hellwig   add a vfs_fsync h...
202
  EXPORT_SYMBOL(vfs_fsync);
cf9a2ae8d   David Howells   [PATCH] BLOCK: Mo...
203

4c728ef58   Christoph Hellwig   add a vfs_fsync h...
204
  static int do_fsync(unsigned int fd, int datasync)
cf9a2ae8d   David Howells   [PATCH] BLOCK: Mo...
205
  {
2903ff019   Al Viro   switch simple cas...
206
  	struct fd f = fdget(fd);
cf9a2ae8d   David Howells   [PATCH] BLOCK: Mo...
207
  	int ret = -EBADF;
2903ff019   Al Viro   switch simple cas...
208
209
210
  	if (f.file) {
  		ret = vfs_fsync(f.file, datasync);
  		fdput(f);
ad4b32c45   Jin Qian   ANDROID: taskstat...
211
  		inc_syscfs(current);
cf9a2ae8d   David Howells   [PATCH] BLOCK: Mo...
212
213
214
  	}
  	return ret;
  }
a5f8fa9e9   Heiko Carstens   [CVE-2009-0029] S...
215
  SYSCALL_DEFINE1(fsync, unsigned int, fd)
cf9a2ae8d   David Howells   [PATCH] BLOCK: Mo...
216
  {
4c728ef58   Christoph Hellwig   add a vfs_fsync h...
217
  	return do_fsync(fd, 0);
cf9a2ae8d   David Howells   [PATCH] BLOCK: Mo...
218
  }
a5f8fa9e9   Heiko Carstens   [CVE-2009-0029] S...
219
  SYSCALL_DEFINE1(fdatasync, unsigned int, fd)
cf9a2ae8d   David Howells   [PATCH] BLOCK: Mo...
220
  {
4c728ef58   Christoph Hellwig   add a vfs_fsync h...
221
  	return do_fsync(fd, 1);
cf9a2ae8d   David Howells   [PATCH] BLOCK: Mo...
222
  }
22f96b380   Jens Axboe   fs: add sync_file...
223
224
  int sync_file_range(struct file *file, loff_t offset, loff_t nbytes,
  		    unsigned int flags)
f79e2abb9   Andrew Morton   [PATCH] sys_sync_...
225
226
  {
  	int ret;
7a0ad10c3   Christoph Hellwig   fold do_sync_file...
227
  	struct address_space *mapping;
f79e2abb9   Andrew Morton   [PATCH] sys_sync_...
228
  	loff_t endbyte;			/* inclusive */
f79e2abb9   Andrew Morton   [PATCH] sys_sync_...
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
  	umode_t i_mode;
  
  	ret = -EINVAL;
  	if (flags & ~VALID_FLAGS)
  		goto out;
  
  	endbyte = offset + nbytes;
  
  	if ((s64)offset < 0)
  		goto out;
  	if ((s64)endbyte < 0)
  		goto out;
  	if (endbyte < offset)
  		goto out;
  
  	if (sizeof(pgoff_t) == 4) {
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
245
  		if (offset >= (0x100000000ULL << PAGE_SHIFT)) {
f79e2abb9   Andrew Morton   [PATCH] sys_sync_...
246
247
248
249
250
251
252
  			/*
  			 * The range starts outside a 32 bit machine's
  			 * pagecache addressing capabilities.  Let it "succeed"
  			 */
  			ret = 0;
  			goto out;
  		}
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
253
  		if (endbyte >= (0x100000000ULL << PAGE_SHIFT)) {
f79e2abb9   Andrew Morton   [PATCH] sys_sync_...
254
255
256
257
258
259
260
261
  			/*
  			 * Out to EOF
  			 */
  			nbytes = 0;
  		}
  	}
  
  	if (nbytes == 0)
111ebb6e6   OGAWA Hirofumi   [PATCH] writeback...
262
  		endbyte = LLONG_MAX;
f79e2abb9   Andrew Morton   [PATCH] sys_sync_...
263
264
  	else
  		endbyte--;		/* inclusive */
22f96b380   Jens Axboe   fs: add sync_file...
265
  	i_mode = file_inode(file)->i_mode;
f79e2abb9   Andrew Morton   [PATCH] sys_sync_...
266
267
268
  	ret = -ESPIPE;
  	if (!S_ISREG(i_mode) && !S_ISBLK(i_mode) && !S_ISDIR(i_mode) &&
  			!S_ISLNK(i_mode))
22f96b380   Jens Axboe   fs: add sync_file...
269
  		goto out;
f79e2abb9   Andrew Morton   [PATCH] sys_sync_...
270

22f96b380   Jens Axboe   fs: add sync_file...
271
  	mapping = file->f_mapping;
7a0ad10c3   Christoph Hellwig   fold do_sync_file...
272
273
  	ret = 0;
  	if (flags & SYNC_FILE_RANGE_WAIT_BEFORE) {
22f96b380   Jens Axboe   fs: add sync_file...
274
  		ret = file_fdatawait_range(file, offset, endbyte);
7a0ad10c3   Christoph Hellwig   fold do_sync_file...
275
  		if (ret < 0)
22f96b380   Jens Axboe   fs: add sync_file...
276
  			goto out;
7a0ad10c3   Christoph Hellwig   fold do_sync_file...
277
278
279
  	}
  
  	if (flags & SYNC_FILE_RANGE_WRITE) {
c553ea4fd   Amir Goldstein   fs/sync.c: sync_f...
280
281
282
283
284
  		int sync_mode = WB_SYNC_NONE;
  
  		if ((flags & SYNC_FILE_RANGE_WRITE_AND_WAIT) ==
  			     SYNC_FILE_RANGE_WRITE_AND_WAIT)
  			sync_mode = WB_SYNC_ALL;
23d012709   Jan Kara   fs/sync.c: make s...
285
  		ret = __filemap_fdatawrite_range(mapping, offset, endbyte,
c553ea4fd   Amir Goldstein   fs/sync.c: sync_f...
286
  						 sync_mode);
7a0ad10c3   Christoph Hellwig   fold do_sync_file...
287
  		if (ret < 0)
22f96b380   Jens Axboe   fs: add sync_file...
288
  			goto out;
7a0ad10c3   Christoph Hellwig   fold do_sync_file...
289
290
291
  	}
  
  	if (flags & SYNC_FILE_RANGE_WAIT_AFTER)
22f96b380   Jens Axboe   fs: add sync_file...
292
  		ret = file_fdatawait_range(file, offset, endbyte);
7a0ad10c3   Christoph Hellwig   fold do_sync_file...
293

f79e2abb9   Andrew Morton   [PATCH] sys_sync_...
294
295
296
  out:
  	return ret;
  }
22f96b380   Jens Axboe   fs: add sync_file...
297
  /*
c553ea4fd   Amir Goldstein   fs/sync.c: sync_f...
298
   * ksys_sync_file_range() permits finely controlled syncing over a segment of
22f96b380   Jens Axboe   fs: add sync_file...
299
   * a file in the range offset .. (offset+nbytes-1) inclusive.  If nbytes is
c553ea4fd   Amir Goldstein   fs/sync.c: sync_f...
300
   * zero then ksys_sync_file_range() will operate from offset out to EOF.
22f96b380   Jens Axboe   fs: add sync_file...
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
   *
   * The flag bits are:
   *
   * SYNC_FILE_RANGE_WAIT_BEFORE: wait upon writeout of all pages in the range
   * before performing the write.
   *
   * SYNC_FILE_RANGE_WRITE: initiate writeout of all those dirty pages in the
   * range which are not presently under writeback. Note that this may block for
   * significant periods due to exhaustion of disk request structures.
   *
   * SYNC_FILE_RANGE_WAIT_AFTER: wait upon writeout of all pages in the range
   * after performing the write.
   *
   * Useful combinations of the flag bits are:
   *
   * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE: ensures that all pages
c553ea4fd   Amir Goldstein   fs/sync.c: sync_f...
317
   * in the range which were dirty on entry to ksys_sync_file_range() are placed
22f96b380   Jens Axboe   fs: add sync_file...
318
319
320
321
322
323
324
325
326
327
328
   * under writeout.  This is a start-write-for-data-integrity operation.
   *
   * SYNC_FILE_RANGE_WRITE: start writeout of all dirty pages in the range which
   * are not presently under writeout.  This is an asynchronous flush-to-disk
   * operation.  Not suitable for data integrity operations.
   *
   * SYNC_FILE_RANGE_WAIT_BEFORE (or SYNC_FILE_RANGE_WAIT_AFTER): wait for
   * completion of writeout of all pages in the range.  This will be used after an
   * earlier SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE operation to wait
   * for that operation to complete and to return the result.
   *
c553ea4fd   Amir Goldstein   fs/sync.c: sync_f...
329
330
   * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE|SYNC_FILE_RANGE_WAIT_AFTER
   * (a.k.a. SYNC_FILE_RANGE_WRITE_AND_WAIT):
22f96b380   Jens Axboe   fs: add sync_file...
331
332
   * a traditional sync() operation.  This is a write-for-data-integrity operation
   * which will ensure that all pages in the range which were dirty on entry to
c553ea4fd   Amir Goldstein   fs/sync.c: sync_f...
333
334
335
   * ksys_sync_file_range() are written to disk.  It should be noted that disk
   * caches are not flushed by this call, so there are no guarantees here that the
   * data will be available on disk after a crash.
22f96b380   Jens Axboe   fs: add sync_file...
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
   *
   *
   * SYNC_FILE_RANGE_WAIT_BEFORE and SYNC_FILE_RANGE_WAIT_AFTER will detect any
   * I/O errors or ENOSPC conditions and will return those to the caller, after
   * clearing the EIO and ENOSPC flags in the address_space.
   *
   * It should be noted that none of these operations write out the file's
   * metadata.  So unless the application is strictly performing overwrites of
   * already-instantiated disk blocks, there are no guarantees here that the data
   * will be available after a crash.
   */
  int ksys_sync_file_range(int fd, loff_t offset, loff_t nbytes,
  			 unsigned int flags)
  {
  	int ret;
  	struct fd f;
  
  	ret = -EBADF;
  	f = fdget(fd);
  	if (f.file)
  		ret = sync_file_range(f.file, offset, nbytes, flags);
  
  	fdput(f);
  	return ret;
  }
806cbae12   Dominik Brodowski   fs: add ksys_sync...
361
362
363
364
365
  SYSCALL_DEFINE4(sync_file_range, int, fd, loff_t, offset, loff_t, nbytes,
  				unsigned int, flags)
  {
  	return ksys_sync_file_range(fd, offset, nbytes, flags);
  }
edd5cd4a9   David Woodhouse   Introduce fixed s...
366
367
  /* It would be nice if people remember that not all the world's an i386
     when they introduce new system calls */
4a0fd5bf0   Al Viro   teach SYSCALL_DEF...
368
369
  SYSCALL_DEFINE4(sync_file_range2, int, fd, unsigned int, flags,
  				 loff_t, offset, loff_t, nbytes)
edd5cd4a9   David Woodhouse   Introduce fixed s...
370
  {
806cbae12   Dominik Brodowski   fs: add ksys_sync...
371
  	return ksys_sync_file_range(fd, offset, nbytes, flags);
edd5cd4a9   David Woodhouse   Introduce fixed s...
372
  }