2 * High-level sync()-related operations
5 #include <linux/kernel.h>
6 #include <linux/file.h>
8 #include <linux/slab.h>
9 #include <linux/export.h>
10 #include <linux/module.h>
11 #include <linux/namei.h>
12 #include <linux/sched.h>
13 #include <linux/writeback.h>
14 #include <linux/syscalls.h>
15 #include <linux/linkage.h>
16 #include <linux/pagemap.h>
17 #include <linux/quotaops.h>
18 #include <linux/backing-dev.h>
21 bool fsync_enabled = true;
22 module_param(fsync_enabled, bool, 0644);
24 #define VALID_FLAGS (SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE| \
25 SYNC_FILE_RANGE_WAIT_AFTER)
28 * Do the filesystem syncing work. For simple filesystems
29 * writeback_inodes_sb(sb) just dirties buffers with inodes so we have to
30 * submit IO for these buffers via __sync_blockdev(). This also speeds up the
31 * wait == 1 case since in that case write_inode() functions do
32 * sync_dirty_buffer() and thus effectively write one block at a time.
34 static int __sync_filesystem(struct super_block *sb, int wait)
39 writeback_inodes_sb(sb, WB_REASON_SYNC);
41 if (sb->s_op->sync_fs)
42 sb->s_op->sync_fs(sb, wait);
43 return __sync_blockdev(sb->s_bdev, wait);
47 * Write out and wait upon all dirty data associated with this
48 * superblock. Filesystem data as well as the underlying block
49 * device. Takes the superblock lock.
51 int sync_filesystem(struct super_block *sb)
56 * We need to be protected against the filesystem going from
57 * r/o to r/w or vice versa.
59 WARN_ON(!rwsem_is_locked(&sb->s_umount));
62 * No point in syncing out anything if the filesystem is read-only.
64 if (sb->s_flags & MS_RDONLY)
67 ret = __sync_filesystem(sb, 0);
70 return __sync_filesystem(sb, 1);
72 EXPORT_SYMBOL(sync_filesystem);
74 static void sync_inodes_one_sb(struct super_block *sb, void *arg)
76 if (!(sb->s_flags & MS_RDONLY))
80 static void sync_fs_one_sb(struct super_block *sb, void *arg)
82 if (!(sb->s_flags & MS_RDONLY) && sb->s_op->sync_fs)
83 sb->s_op->sync_fs(sb, *(int *)arg);
86 static void fdatawrite_one_bdev(struct block_device *bdev, void *arg)
88 filemap_fdatawrite(bdev->bd_inode->i_mapping);
91 static void fdatawait_one_bdev(struct block_device *bdev, void *arg)
94 * We keep the error status of individual mapping so that
95 * applications can catch the writeback error using fsync(2).
96 * See filemap_fdatawait_keep_errors() for details.
98 filemap_fdatawait_keep_errors(bdev->bd_inode->i_mapping);
102 * Sync everything. We start by waking flusher threads so that most of
103 * writeback runs on all devices in parallel. Then we sync all inodes reliably
104 * which effectively also waits for all flusher threads to finish doing
105 * writeback. At this point all data is on disk so metadata should be stable
106 * and we tell filesystems to sync their metadata via ->sync_fs() calls.
107 * Finally, we writeout all block devices because some filesystems (e.g. ext2)
108 * just write metadata (such as inodes or bitmaps) to block device page cache
109 * and do not sync it on their own in ->sync_fs().
111 SYSCALL_DEFINE0(sync)
113 int nowait = 0, wait = 1;
115 wakeup_flusher_threads(0, WB_REASON_SYNC);
116 iterate_supers(sync_inodes_one_sb, NULL);
117 iterate_supers(sync_fs_one_sb, &nowait);
118 iterate_supers(sync_fs_one_sb, &wait);
119 iterate_bdevs(fdatawrite_one_bdev, NULL);
120 iterate_bdevs(fdatawait_one_bdev, NULL);
121 if (unlikely(laptop_mode))
122 laptop_sync_completion();
126 static void do_sync_work(struct work_struct *work)
131 * Sync twice to reduce the possibility we skipped some inodes / pages
132 * because they were temporarily locked
134 iterate_supers(sync_inodes_one_sb, &nowait);
135 iterate_supers(sync_fs_one_sb, &nowait);
136 iterate_bdevs(fdatawrite_one_bdev, NULL);
137 iterate_supers(sync_inodes_one_sb, &nowait);
138 iterate_supers(sync_fs_one_sb, &nowait);
139 iterate_bdevs(fdatawrite_one_bdev, NULL);
140 printk("Emergency Sync complete\n");
144 void emergency_sync(void)
146 struct work_struct *work;
148 work = kmalloc(sizeof(*work), GFP_ATOMIC);
150 INIT_WORK(work, do_sync_work);
156 * sync a single super
158 SYSCALL_DEFINE1(syncfs, int, fd)
161 struct super_block *sb;
170 sb = f.file->f_path.dentry->d_sb;
172 down_read(&sb->s_umount);
173 ret = sync_filesystem(sb);
174 up_read(&sb->s_umount);
181 * vfs_fsync_range - helper to sync a range of data & metadata to disk
182 * @file: file to sync
183 * @start: offset in bytes of the beginning of data range to sync
184 * @end: offset in bytes of the end of data range (inclusive)
185 * @datasync: perform only datasync
187 * Write back data in range @start..@end and metadata for @file to disk. If
188 * @datasync is set only metadata needed to access modified file data is
191 int vfs_fsync_range(struct file *file, loff_t start, loff_t end, int datasync)
193 struct inode *inode = file->f_mapping->host;
198 if (!file->f_op->fsync)
200 if (!datasync && (inode->i_state & I_DIRTY_TIME)) {
201 spin_lock(&inode->i_lock);
202 inode->i_state &= ~I_DIRTY_TIME;
203 spin_unlock(&inode->i_lock);
204 mark_inode_dirty_sync(inode);
206 return file->f_op->fsync(file, start, end, datasync);
208 EXPORT_SYMBOL(vfs_fsync_range);
211 * vfs_fsync - perform a fsync or fdatasync on a file
212 * @file: file to sync
213 * @datasync: only perform a fdatasync operation
215 * Write back data and metadata for @file to disk. If @datasync is
216 * set only metadata needed to access modified file data is written.
218 int vfs_fsync(struct file *file, int datasync)
220 return vfs_fsync_range(file, 0, LLONG_MAX, datasync);
222 EXPORT_SYMBOL(vfs_fsync);
224 static int do_fsync(unsigned int fd, int datasync)
234 ret = vfs_fsync(f.file, datasync);
241 SYSCALL_DEFINE1(fsync, unsigned int, fd)
243 return do_fsync(fd, 0);
246 SYSCALL_DEFINE1(fdatasync, unsigned int, fd)
248 return do_fsync(fd, 1);
252 * sys_sync_file_range() permits finely controlled syncing over a segment of
253 * a file in the range offset .. (offset+nbytes-1) inclusive. If nbytes is
254 * zero then sys_sync_file_range() will operate from offset out to EOF.
258 * SYNC_FILE_RANGE_WAIT_BEFORE: wait upon writeout of all pages in the range
259 * before performing the write.
261 * SYNC_FILE_RANGE_WRITE: initiate writeout of all those dirty pages in the
262 * range which are not presently under writeback. Note that this may block for
263 * significant periods due to exhaustion of disk request structures.
265 * SYNC_FILE_RANGE_WAIT_AFTER: wait upon writeout of all pages in the range
266 * after performing the write.
268 * Useful combinations of the flag bits are:
270 * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE: ensures that all pages
271 * in the range which were dirty on entry to sys_sync_file_range() are placed
272 * under writeout. This is a start-write-for-data-integrity operation.
274 * SYNC_FILE_RANGE_WRITE: start writeout of all dirty pages in the range which
275 * are not presently under writeout. This is an asynchronous flush-to-disk
276 * operation. Not suitable for data integrity operations.
278 * SYNC_FILE_RANGE_WAIT_BEFORE (or SYNC_FILE_RANGE_WAIT_AFTER): wait for
279 * completion of writeout of all pages in the range. This will be used after an
280 * earlier SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE operation to wait
281 * for that operation to complete and to return the result.
283 * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE|SYNC_FILE_RANGE_WAIT_AFTER:
284 * a traditional sync() operation. This is a write-for-data-integrity operation
285 * which will ensure that all pages in the range which were dirty on entry to
286 * sys_sync_file_range() are committed to disk.
289 * SYNC_FILE_RANGE_WAIT_BEFORE and SYNC_FILE_RANGE_WAIT_AFTER will detect any
290 * I/O errors or ENOSPC conditions and will return those to the caller, after
291 * clearing the EIO and ENOSPC flags in the address_space.
293 * It should be noted that none of these operations write out the file's
294 * metadata. So unless the application is strictly performing overwrites of
295 * already-instantiated disk blocks, there are no guarantees here that the data
296 * will be available after a crash.
298 SYSCALL_DEFINE4(sync_file_range, int, fd, loff_t, offset, loff_t, nbytes,
303 struct address_space *mapping;
304 loff_t endbyte; /* inclusive */
311 if (flags & ~VALID_FLAGS)
314 endbyte = offset + nbytes;
318 if ((s64)endbyte < 0)
320 if (endbyte < offset)
323 if (sizeof(pgoff_t) == 4) {
324 if (offset >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
326 * The range starts outside a 32 bit machine's
327 * pagecache addressing capabilities. Let it "succeed"
332 if (endbyte >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
343 endbyte--; /* inclusive */
350 i_mode = file_inode(f.file)->i_mode;
352 if (!S_ISREG(i_mode) && !S_ISBLK(i_mode) && !S_ISDIR(i_mode) &&
356 mapping = f.file->f_mapping;
363 if (flags & SYNC_FILE_RANGE_WAIT_BEFORE) {
364 ret = filemap_fdatawait_range(mapping, offset, endbyte);
369 if (flags & SYNC_FILE_RANGE_WRITE) {
370 ret = __filemap_fdatawrite_range(mapping, offset, endbyte,
376 if (flags & SYNC_FILE_RANGE_WAIT_AFTER)
377 ret = filemap_fdatawait_range(mapping, offset, endbyte);
385 /* It would be nice if people remember that not all the world's an i386
386 when they introduce new system calls */
387 SYSCALL_DEFINE4(sync_file_range2, int, fd, unsigned int, flags,
388 loff_t, offset, loff_t, nbytes)
390 return sys_sync_file_range(fd, offset, nbytes, flags);