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