blob: c7690016453e460c4b295df9f5c15f7b7f0bf756 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Andrew Mortonf79e2ab2006-03-31 02:30:42 -08002/*
3 * High-level sync()-related operations
4 */
5
Christoph Hellwig70164eb2021-10-19 08:25:25 +02006#include <linux/blkdev.h>
Andrew Mortonf79e2ab2006-03-31 02:30:42 -08007#include <linux/kernel.h>
8#include <linux/file.h>
9#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050011#include <linux/export.h>
Sage Weilb7ed78f2011-03-10 11:31:30 -080012#include <linux/namei.h>
Al Viro914e2632006-10-18 13:55:46 -040013#include <linux/sched.h>
Andrew Mortonf79e2ab2006-03-31 02:30:42 -080014#include <linux/writeback.h>
15#include <linux/syscalls.h>
16#include <linux/linkage.h>
17#include <linux/pagemap.h>
David Howellscf9a2ae2006-08-29 19:05:54 +010018#include <linux/quotaops.h>
Jörn Engel5129a462010-04-25 08:54:42 +020019#include <linux/backing-dev.h>
Jan Kara5a3e5cb2009-04-27 16:43:48 +020020#include "internal.h"
Andrew Mortonf79e2ab2006-03-31 02:30:42 -080021
22#define VALID_FLAGS (SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE| \
23 SYNC_FILE_RANGE_WAIT_AFTER)
24
Jan Karac15c54f2009-04-27 16:43:52 +020025/*
Jan Karac15c54f2009-04-27 16:43:52 +020026 * 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 */
Jan Kara60b06802009-04-27 16:43:53 +020030int sync_filesystem(struct super_block *sb)
Jan Karac15c54f2009-04-27 16:43:52 +020031{
Darrick J. Wong56798972022-01-30 08:53:16 -080032 int ret = 0;
Jan Karac15c54f2009-04-27 16:43:52 +020033
Christoph Hellwig5af79262009-05-05 15:41:25 +020034 /*
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 */
David Howellsbc98a422017-07-17 08:45:34 +010043 if (sb_rdonly(sb))
Christoph Hellwig5af79262009-05-05 15:41:25 +020044 return 0;
45
Christoph Hellwig9a208ba2021-10-19 08:25:24 +020046 /*
47 * Do the filesystem syncing work. For simple filesystems
48 * writeback_inodes_sb(sb) just dirties buffers with inodes so we have
Christoph Hellwig70164eb2021-10-19 08:25:25 +020049 * to submit I/O for these buffers via sync_blockdev(). This also
Christoph Hellwig9a208ba2021-10-19 08:25:24 +020050 * 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);
Darrick J. Wong56798972022-01-30 08:53:16 -080055 if (sb->s_op->sync_fs) {
56 ret = sb->s_op->sync_fs(sb, 0);
57 if (ret)
58 return ret;
59 }
Christoph Hellwig70164eb2021-10-19 08:25:25 +020060 ret = sync_blockdev_nowait(sb->s_bdev);
Darrick J. Wong56798972022-01-30 08:53:16 -080061 if (ret)
Jan Karac15c54f2009-04-27 16:43:52 +020062 return ret;
Christoph Hellwig9a208ba2021-10-19 08:25:24 +020063
64 sync_inodes_sb(sb);
Darrick J. Wong56798972022-01-30 08:53:16 -080065 if (sb->s_op->sync_fs) {
66 ret = sb->s_op->sync_fs(sb, 1);
67 if (ret)
68 return ret;
69 }
Christoph Hellwig70164eb2021-10-19 08:25:25 +020070 return sync_blockdev(sb->s_bdev);
Jan Karac15c54f2009-04-27 16:43:52 +020071}
Anton Altaparmakov10096fb2014-08-21 11:09:27 +010072EXPORT_SYMBOL(sync_filesystem);
Jan Karac15c54f2009-04-27 16:43:52 +020073
Jan Karab3de6532012-07-03 16:45:30 +020074static void sync_inodes_one_sb(struct super_block *sb, void *arg)
Al Viro01a05b32010-03-23 06:06:58 -040075{
David Howellsbc98a422017-07-17 08:45:34 +010076 if (!sb_rdonly(sb))
Jan Kara0dc83bd2014-02-21 11:19:04 +010077 sync_inodes_sb(sb);
Al Viro01a05b32010-03-23 06:06:58 -040078}
Jan Karab3de6532012-07-03 16:45:30 +020079
Jan Karab3de6532012-07-03 16:45:30 +020080static void sync_fs_one_sb(struct super_block *sb, void *arg)
81{
Konstantin Khlebnikov32b1924b22020-04-09 11:29:47 +030082 if (!sb_rdonly(sb) && !(sb->s_iflags & SB_I_SKIP_SYNC) &&
83 sb->s_op->sync_fs)
Jan Karab3de6532012-07-03 16:45:30 +020084 sb->s_op->sync_fs(sb, *(int *)arg);
85}
86
Zhang, Yanmin3beab0b2009-07-05 12:08:08 -070087/*
Jan Kara4ea425b2012-07-03 16:45:34 +020088 * 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().
Zhang, Yanmin3beab0b2009-07-05 12:08:08 -070096 */
Dominik Brodowski70f68ee2018-03-14 22:35:11 +010097void ksys_sync(void)
David Howellscf9a2ae2006-08-29 19:05:54 +010098{
Jan Karab3de6532012-07-03 16:45:30 +020099 int nowait = 0, wait = 1;
100
Jens Axboe9ba4b2d2017-09-20 08:58:25 -0600101 wakeup_flusher_threads(WB_REASON_SYNC);
Jan Kara0dc83bd2014-02-21 11:19:04 +0100102 iterate_supers(sync_inodes_one_sb, NULL);
Jan Kara4ea425b2012-07-03 16:45:34 +0200103 iterate_supers(sync_fs_one_sb, &nowait);
Jan Karab3de6532012-07-03 16:45:30 +0200104 iterate_supers(sync_fs_one_sb, &wait);
Christoph Hellwig1e03a362021-10-19 08:25:30 +0200105 sync_bdevs(false);
106 sync_bdevs(true);
Jan Kara5cee5812009-04-27 16:43:51 +0200107 if (unlikely(laptop_mode))
108 laptop_sync_completion();
Dominik Brodowski70f68ee2018-03-14 22:35:11 +0100109}
110
111SYSCALL_DEFINE0(sync)
112{
113 ksys_sync();
David Howellscf9a2ae2006-08-29 19:05:54 +0100114 return 0;
115}
116
Jens Axboea2a95372009-03-17 09:38:40 +0100117static void do_sync_work(struct work_struct *work)
118{
Jan Karab3de6532012-07-03 16:45:30 +0200119 int nowait = 0;
120
Jan Kara5cee5812009-04-27 16:43:51 +0200121 /*
122 * Sync twice to reduce the possibility we skipped some inodes / pages
123 * because they were temporarily locked
124 */
Jan Karab3de6532012-07-03 16:45:30 +0200125 iterate_supers(sync_inodes_one_sb, &nowait);
126 iterate_supers(sync_fs_one_sb, &nowait);
Christoph Hellwig1e03a362021-10-19 08:25:30 +0200127 sync_bdevs(false);
Jan Karab3de6532012-07-03 16:45:30 +0200128 iterate_supers(sync_inodes_one_sb, &nowait);
129 iterate_supers(sync_fs_one_sb, &nowait);
Christoph Hellwig1e03a362021-10-19 08:25:30 +0200130 sync_bdevs(false);
Jan Kara5cee5812009-04-27 16:43:51 +0200131 printk("Emergency Sync complete\n");
Jens Axboea2a95372009-03-17 09:38:40 +0100132 kfree(work);
133}
134
David Howellscf9a2ae2006-08-29 19:05:54 +0100135void emergency_sync(void)
136{
Jens Axboea2a95372009-03-17 09:38:40 +0100137 struct work_struct *work;
138
139 work = kmalloc(sizeof(*work), GFP_ATOMIC);
140 if (work) {
141 INIT_WORK(work, do_sync_work);
142 schedule_work(work);
143 }
David Howellscf9a2ae2006-08-29 19:05:54 +0100144}
145
Sage Weilb7ed78f2011-03-10 11:31:30 -0800146/*
147 * sync a single super
148 */
149SYSCALL_DEFINE1(syncfs, int, fd)
150{
Al Viro2903ff02012-08-28 12:52:22 -0400151 struct fd f = fdget(fd);
Sage Weilb7ed78f2011-03-10 11:31:30 -0800152 struct super_block *sb;
Jeff Layton735e4ae2020-06-01 21:45:36 -0700153 int ret, ret2;
Sage Weilb7ed78f2011-03-10 11:31:30 -0800154
Al Viro2903ff02012-08-28 12:52:22 -0400155 if (!f.file)
Sage Weilb7ed78f2011-03-10 11:31:30 -0800156 return -EBADF;
Al Virob5830432014-10-31 01:22:04 -0400157 sb = f.file->f_path.dentry->d_sb;
Sage Weilb7ed78f2011-03-10 11:31:30 -0800158
159 down_read(&sb->s_umount);
160 ret = sync_filesystem(sb);
161 up_read(&sb->s_umount);
162
Jeff Layton735e4ae2020-06-01 21:45:36 -0700163 ret2 = errseq_check_and_advance(&sb->s_wb_err, &f.file->f_sb_err);
164
Al Viro2903ff02012-08-28 12:52:22 -0400165 fdput(f);
Jeff Layton735e4ae2020-06-01 21:45:36 -0700166 return ret ? ret : ret2;
Sage Weilb7ed78f2011-03-10 11:31:30 -0800167}
168
Christoph Hellwig4c728ef2008-12-22 21:11:15 +0100169/**
Jan Kara148f9482009-08-17 19:52:36 +0200170 * vfs_fsync_range - helper to sync a range of data & metadata to disk
Christoph Hellwig4c728ef2008-12-22 21:11:15 +0100171 * @file: file to sync
Jan Kara148f9482009-08-17 19:52:36 +0200172 * @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
Christoph Hellwig4c728ef2008-12-22 21:11:15 +0100175 *
Jan Kara148f9482009-08-17 19:52:36 +0200176 * 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.
Christoph Hellwig4c728ef2008-12-22 21:11:15 +0100179 */
Christoph Hellwig8018ab02010-03-22 17:32:25 +0100180int vfs_fsync_range(struct file *file, loff_t start, loff_t end, int datasync)
David Howellscf9a2ae2006-08-29 19:05:54 +0100181{
Theodore Ts'o0ae45f62015-02-02 00:37:00 -0500182 struct inode *inode = file->f_mapping->host;
183
Al Viro72c2d532013-09-22 16:27:52 -0400184 if (!file->f_op->fsync)
Josef Bacik02c24a82011-07-16 20:44:56 -0400185 return -EINVAL;
Christoph Hellwig0d07e552018-03-06 17:03:31 -0800186 if (!datasync && (inode->i_state & I_DIRTY_TIME))
Theodore Ts'o0ae45f62015-02-02 00:37:00 -0500187 mark_inode_dirty_sync(inode);
Jeff Layton0f410742017-07-05 15:26:50 -0400188 return file->f_op->fsync(file, start, end, datasync);
David Howellscf9a2ae2006-08-29 19:05:54 +0100189}
Jan Kara148f9482009-08-17 19:52:36 +0200190EXPORT_SYMBOL(vfs_fsync_range);
191
192/**
193 * vfs_fsync - perform a fsync or fdatasync on a file
194 * @file: file to sync
Jan Kara148f9482009-08-17 19:52:36 +0200195 * @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.
Jan Kara148f9482009-08-17 19:52:36 +0200199 */
Christoph Hellwig8018ab02010-03-22 17:32:25 +0100200int vfs_fsync(struct file *file, int datasync)
Jan Kara148f9482009-08-17 19:52:36 +0200201{
Christoph Hellwig8018ab02010-03-22 17:32:25 +0100202 return vfs_fsync_range(file, 0, LLONG_MAX, datasync);
Jan Kara148f9482009-08-17 19:52:36 +0200203}
Christoph Hellwig4c728ef2008-12-22 21:11:15 +0100204EXPORT_SYMBOL(vfs_fsync);
David Howellscf9a2ae2006-08-29 19:05:54 +0100205
Christoph Hellwig4c728ef2008-12-22 21:11:15 +0100206static int do_fsync(unsigned int fd, int datasync)
David Howellscf9a2ae2006-08-29 19:05:54 +0100207{
Al Viro2903ff02012-08-28 12:52:22 -0400208 struct fd f = fdget(fd);
David Howellscf9a2ae2006-08-29 19:05:54 +0100209 int ret = -EBADF;
210
Al Viro2903ff02012-08-28 12:52:22 -0400211 if (f.file) {
212 ret = vfs_fsync(f.file, datasync);
213 fdput(f);
David Howellscf9a2ae2006-08-29 19:05:54 +0100214 }
215 return ret;
216}
217
Heiko Carstensa5f8fa92009-01-14 14:14:11 +0100218SYSCALL_DEFINE1(fsync, unsigned int, fd)
David Howellscf9a2ae2006-08-29 19:05:54 +0100219{
Christoph Hellwig4c728ef2008-12-22 21:11:15 +0100220 return do_fsync(fd, 0);
David Howellscf9a2ae2006-08-29 19:05:54 +0100221}
222
Heiko Carstensa5f8fa92009-01-14 14:14:11 +0100223SYSCALL_DEFINE1(fdatasync, unsigned int, fd)
David Howellscf9a2ae2006-08-29 19:05:54 +0100224{
Christoph Hellwig4c728ef2008-12-22 21:11:15 +0100225 return do_fsync(fd, 1);
David Howellscf9a2ae2006-08-29 19:05:54 +0100226}
227
Jens Axboe22f96b32019-04-09 14:51:48 -0600228int sync_file_range(struct file *file, loff_t offset, loff_t nbytes,
229 unsigned int flags)
230{
231 int ret;
232 struct address_space *mapping;
233 loff_t endbyte; /* inclusive */
234 umode_t i_mode;
235
236 ret = -EINVAL;
237 if (flags & ~VALID_FLAGS)
238 goto out;
239
240 endbyte = offset + nbytes;
241
242 if ((s64)offset < 0)
243 goto out;
244 if ((s64)endbyte < 0)
245 goto out;
246 if (endbyte < offset)
247 goto out;
248
249 if (sizeof(pgoff_t) == 4) {
250 if (offset >= (0x100000000ULL << PAGE_SHIFT)) {
251 /*
252 * The range starts outside a 32 bit machine's
253 * pagecache addressing capabilities. Let it "succeed"
254 */
255 ret = 0;
256 goto out;
257 }
258 if (endbyte >= (0x100000000ULL << PAGE_SHIFT)) {
259 /*
260 * Out to EOF
261 */
262 nbytes = 0;
263 }
264 }
265
266 if (nbytes == 0)
267 endbyte = LLONG_MAX;
268 else
269 endbyte--; /* inclusive */
270
271 i_mode = file_inode(file)->i_mode;
272 ret = -ESPIPE;
273 if (!S_ISREG(i_mode) && !S_ISBLK(i_mode) && !S_ISDIR(i_mode) &&
274 !S_ISLNK(i_mode))
275 goto out;
276
277 mapping = file->f_mapping;
278 ret = 0;
279 if (flags & SYNC_FILE_RANGE_WAIT_BEFORE) {
280 ret = file_fdatawait_range(file, offset, endbyte);
281 if (ret < 0)
282 goto out;
283 }
284
285 if (flags & SYNC_FILE_RANGE_WRITE) {
Amir Goldsteinc553ea42019-05-13 17:22:30 -0700286 int sync_mode = WB_SYNC_NONE;
287
288 if ((flags & SYNC_FILE_RANGE_WRITE_AND_WAIT) ==
289 SYNC_FILE_RANGE_WRITE_AND_WAIT)
290 sync_mode = WB_SYNC_ALL;
291
Jens Axboe22f96b32019-04-09 14:51:48 -0600292 ret = __filemap_fdatawrite_range(mapping, offset, endbyte,
Amir Goldsteinc553ea42019-05-13 17:22:30 -0700293 sync_mode);
Jens Axboe22f96b32019-04-09 14:51:48 -0600294 if (ret < 0)
295 goto out;
296 }
297
298 if (flags & SYNC_FILE_RANGE_WAIT_AFTER)
299 ret = file_fdatawait_range(file, offset, endbyte);
300
301out:
302 return ret;
303}
304
David Howellscf9a2ae2006-08-29 19:05:54 +0100305/*
Amir Goldsteinc553ea42019-05-13 17:22:30 -0700306 * ksys_sync_file_range() permits finely controlled syncing over a segment of
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800307 * a file in the range offset .. (offset+nbytes-1) inclusive. If nbytes is
Amir Goldsteinc553ea42019-05-13 17:22:30 -0700308 * zero then ksys_sync_file_range() will operate from offset out to EOF.
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800309 *
310 * The flag bits are:
311 *
312 * SYNC_FILE_RANGE_WAIT_BEFORE: wait upon writeout of all pages in the range
313 * before performing the write.
314 *
315 * SYNC_FILE_RANGE_WRITE: initiate writeout of all those dirty pages in the
Pavel Machekcce77082008-07-23 21:27:36 -0700316 * range which are not presently under writeback. Note that this may block for
317 * significant periods due to exhaustion of disk request structures.
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800318 *
319 * SYNC_FILE_RANGE_WAIT_AFTER: wait upon writeout of all pages in the range
320 * after performing the write.
321 *
322 * Useful combinations of the flag bits are:
323 *
324 * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE: ensures that all pages
Amir Goldsteinc553ea42019-05-13 17:22:30 -0700325 * in the range which were dirty on entry to ksys_sync_file_range() are placed
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800326 * under writeout. This is a start-write-for-data-integrity operation.
327 *
328 * SYNC_FILE_RANGE_WRITE: start writeout of all dirty pages in the range which
329 * are not presently under writeout. This is an asynchronous flush-to-disk
330 * operation. Not suitable for data integrity operations.
331 *
332 * SYNC_FILE_RANGE_WAIT_BEFORE (or SYNC_FILE_RANGE_WAIT_AFTER): wait for
333 * completion of writeout of all pages in the range. This will be used after an
334 * earlier SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE operation to wait
335 * for that operation to complete and to return the result.
336 *
Amir Goldsteinc553ea42019-05-13 17:22:30 -0700337 * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE|SYNC_FILE_RANGE_WAIT_AFTER
338 * (a.k.a. SYNC_FILE_RANGE_WRITE_AND_WAIT):
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800339 * a traditional sync() operation. This is a write-for-data-integrity operation
340 * which will ensure that all pages in the range which were dirty on entry to
Amir Goldsteinc553ea42019-05-13 17:22:30 -0700341 * ksys_sync_file_range() are written to disk. It should be noted that disk
342 * caches are not flushed by this call, so there are no guarantees here that the
343 * data will be available on disk after a crash.
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800344 *
345 *
346 * SYNC_FILE_RANGE_WAIT_BEFORE and SYNC_FILE_RANGE_WAIT_AFTER will detect any
347 * I/O errors or ENOSPC conditions and will return those to the caller, after
348 * clearing the EIO and ENOSPC flags in the address_space.
349 *
350 * It should be noted that none of these operations write out the file's
351 * metadata. So unless the application is strictly performing overwrites of
352 * already-instantiated disk blocks, there are no guarantees here that the data
353 * will be available after a crash.
354 */
Dominik Brodowski806cbae2018-03-11 11:34:47 +0100355int ksys_sync_file_range(int fd, loff_t offset, loff_t nbytes,
356 unsigned int flags)
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800357{
358 int ret;
Al Viro2903ff02012-08-28 12:52:22 -0400359 struct fd f;
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800360
361 ret = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -0400362 f = fdget(fd);
Jens Axboe22f96b32019-04-09 14:51:48 -0600363 if (f.file)
364 ret = sync_file_range(f.file, offset, nbytes, flags);
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800365
Al Viro2903ff02012-08-28 12:52:22 -0400366 fdput(f);
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800367 return ret;
368}
369
Dominik Brodowski806cbae2018-03-11 11:34:47 +0100370SYSCALL_DEFINE4(sync_file_range, int, fd, loff_t, offset, loff_t, nbytes,
371 unsigned int, flags)
372{
373 return ksys_sync_file_range(fd, offset, nbytes, flags);
374}
375
David Woodhouseedd5cd42007-06-27 14:10:09 -0700376/* It would be nice if people remember that not all the world's an i386
377 when they introduce new system calls */
Al Viro4a0fd5b2013-01-21 15:16:58 -0500378SYSCALL_DEFINE4(sync_file_range2, int, fd, unsigned int, flags,
379 loff_t, offset, loff_t, nbytes)
David Woodhouseedd5cd42007-06-27 14:10:09 -0700380{
Dominik Brodowski806cbae2018-03-11 11:34:47 +0100381 return ksys_sync_file_range(fd, offset, nbytes, flags);
David Woodhouseedd5cd42007-06-27 14:10:09 -0700382}