2 * High-level sync()-related operations
5 #include <linux/kernel.h>
6 #include <linux/file.h>
8 #include <linux/slab.h>
9 #include <linux/module.h>
10 #include <linux/namei.h>
11 #include <linux/sched.h>
12 #include <linux/writeback.h>
13 #include <linux/syscalls.h>
14 #include <linux/linkage.h>
15 #include <linux/pagemap.h>
16 #include <linux/quotaops.h>
17 #include <linux/buffer_head.h>
18 #include <linux/backing-dev.h>
21 #define VALID_FLAGS (SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE| \
22 SYNC_FILE_RANGE_WAIT_AFTER)
24 #ifdef CONFIG_FSYNC_CONTROL
25 extern bool fsynccontrol_fsync_enabled();
29 * Do the filesystem syncing work. For simple filesystems
30 * writeback_inodes_sb(sb) just dirties buffers with inodes so we have to
31 * submit IO for these buffers via __sync_blockdev(). This also speeds up the
32 * wait == 1 case since in that case write_inode() functions do
33 * sync_dirty_buffer() and thus effectively write one block at a time.
35 static int __sync_filesystem(struct super_block
*sb
, int wait
)
38 * This should be safe, as we require bdi backing to actually
39 * write out data in the first place
41 if (sb
->s_bdi
== &noop_backing_dev_info
)
44 if (sb
->s_qcop
&& sb
->s_qcop
->quota_sync
)
45 sb
->s_qcop
->quota_sync(sb
, -1, wait
);
50 writeback_inodes_sb(sb
);
52 if (sb
->s_op
->sync_fs
)
53 sb
->s_op
->sync_fs(sb
, wait
);
54 return __sync_blockdev(sb
->s_bdev
, wait
);
58 * Write out and wait upon all dirty data associated with this
59 * superblock. Filesystem data as well as the underlying block
60 * device. Takes the superblock lock.
62 int sync_filesystem(struct super_block
*sb
)
67 * We need to be protected against the filesystem going from
68 * r/o to r/w or vice versa.
70 WARN_ON(!rwsem_is_locked(&sb
->s_umount
));
73 * No point in syncing out anything if the filesystem is read-only.
75 if (sb
->s_flags
& MS_RDONLY
)
78 ret
= __sync_filesystem(sb
, 0);
81 return __sync_filesystem(sb
, 1);
83 EXPORT_SYMBOL_GPL(sync_filesystem
);
85 static void sync_one_sb(struct super_block
*sb
, void *arg
)
87 if (!(sb
->s_flags
& MS_RDONLY
))
88 __sync_filesystem(sb
, *(int *)arg
);
91 * Sync all the data for all the filesystems (called by sys_sync() and
94 static void sync_filesystems(int wait
)
96 iterate_supers(sync_one_sb
, &wait
);
100 * sync everything. Start out by waking pdflush, because that writes back
101 * all queues in parallel.
103 SYSCALL_DEFINE0(sync
)
105 wakeup_flusher_threads(0);
108 if (unlikely(laptop_mode
))
109 laptop_sync_completion();
113 static void do_sync_work(struct work_struct
*work
)
116 * Sync twice to reduce the possibility we skipped some inodes / pages
117 * because they were temporarily locked
121 printk("Emergency Sync complete\n");
125 void emergency_sync(void)
127 struct work_struct
*work
;
129 work
= kmalloc(sizeof(*work
), GFP_ATOMIC
);
131 INIT_WORK(work
, do_sync_work
);
137 * sync a single super
139 SYSCALL_DEFINE1(syncfs
, int, fd
)
142 struct super_block
*sb
;
146 #ifdef CONFIG_FSYNC_CONTROL
147 if (!fsynccontrol_fsync_enabled())
151 file
= fget_light(fd
, &fput_needed
);
154 sb
= file
->f_dentry
->d_sb
;
156 down_read(&sb
->s_umount
);
157 ret
= sync_filesystem(sb
);
158 up_read(&sb
->s_umount
);
160 fput_light(file
, fput_needed
);
165 * vfs_fsync_range - helper to sync a range of data & metadata to disk
166 * @file: file to sync
167 * @start: offset in bytes of the beginning of data range to sync
168 * @end: offset in bytes of the end of data range (inclusive)
169 * @datasync: perform only datasync
171 * Write back data in range @start..@end and metadata for @file to disk. If
172 * @datasync is set only metadata needed to access modified file data is
175 int vfs_fsync_range(struct file
*file
, loff_t start
, loff_t end
, int datasync
)
177 struct address_space
*mapping
= file
->f_mapping
;
180 #ifdef CONFIG_FSYNC_CONTROL
181 if (!fsynccontrol_fsync_enabled())
185 if (!file
->f_op
|| !file
->f_op
->fsync
) {
190 ret
= filemap_write_and_wait_range(mapping
, start
, end
);
193 * We need to protect against concurrent writers, which could cause
194 * livelocks in fsync_buffers_list().
196 mutex_lock(&mapping
->host
->i_mutex
);
197 err
= file
->f_op
->fsync(file
, datasync
);
200 mutex_unlock(&mapping
->host
->i_mutex
);
205 EXPORT_SYMBOL(vfs_fsync_range
);
208 * vfs_fsync - perform a fsync or fdatasync on a file
209 * @file: file to sync
210 * @datasync: only perform a fdatasync operation
212 * Write back data and metadata for @file to disk. If @datasync is
213 * set only metadata needed to access modified file data is written.
215 int vfs_fsync(struct file
*file
, int datasync
)
217 #ifdef CONFIG_FSYNC_CONTROL
218 if (!fsynccontrol_fsync_enabled())
222 return vfs_fsync_range(file
, 0, LLONG_MAX
, datasync
);
224 EXPORT_SYMBOL(vfs_fsync
);
226 static int do_fsync(unsigned int fd
, int datasync
)
231 #ifdef CONFIG_FSYNC_CONTROL
232 if (!fsynccontrol_fsync_enabled())
238 ret
= vfs_fsync(file
, datasync
);
244 SYSCALL_DEFINE1(fsync
, unsigned int, fd
)
246 #ifdef CONFIG_FSYNC_CONTROL
247 if (!fsynccontrol_fsync_enabled())
251 return do_fsync(fd
, 0);
254 SYSCALL_DEFINE1(fdatasync
, unsigned int, fd
)
256 #ifdef CONFIG_FSYNC_CONTROL
257 if (!fsynccontrol_fsync_enabled())
261 return do_fsync(fd
, 1);
265 * generic_write_sync - perform syncing after a write if file / inode is sync
266 * @file: file to which the write happened
267 * @pos: offset where the write started
268 * @count: length of the write
270 * This is just a simple wrapper about our general syncing function.
272 int generic_write_sync(struct file
*file
, loff_t pos
, loff_t count
)
274 #ifdef CONFIG_FSYNC_CONTROL
275 if (!fsynccontrol_fsync_enabled())
279 if (!(file
->f_flags
& O_DSYNC
) && !IS_SYNC(file
->f_mapping
->host
))
281 return vfs_fsync_range(file
, pos
, pos
+ count
- 1,
282 (file
->f_flags
& __O_SYNC
) ? 0 : 1);
284 EXPORT_SYMBOL(generic_write_sync
);
287 * sys_sync_file_range() permits finely controlled syncing over a segment of
288 * a file in the range offset .. (offset+nbytes-1) inclusive. If nbytes is
289 * zero then sys_sync_file_range() will operate from offset out to EOF.
293 * SYNC_FILE_RANGE_WAIT_BEFORE: wait upon writeout of all pages in the range
294 * before performing the write.
296 * SYNC_FILE_RANGE_WRITE: initiate writeout of all those dirty pages in the
297 * range which are not presently under writeback. Note that this may block for
298 * significant periods due to exhaustion of disk request structures.
300 * SYNC_FILE_RANGE_WAIT_AFTER: wait upon writeout of all pages in the range
301 * after performing the write.
303 * Useful combinations of the flag bits are:
305 * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE: ensures that all pages
306 * in the range which were dirty on entry to sys_sync_file_range() are placed
307 * under writeout. This is a start-write-for-data-integrity operation.
309 * SYNC_FILE_RANGE_WRITE: start writeout of all dirty pages in the range which
310 * are not presently under writeout. This is an asynchronous flush-to-disk
311 * operation. Not suitable for data integrity operations.
313 * SYNC_FILE_RANGE_WAIT_BEFORE (or SYNC_FILE_RANGE_WAIT_AFTER): wait for
314 * completion of writeout of all pages in the range. This will be used after an
315 * earlier SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE operation to wait
316 * for that operation to complete and to return the result.
318 * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE|SYNC_FILE_RANGE_WAIT_AFTER:
319 * a traditional sync() operation. This is a write-for-data-integrity operation
320 * which will ensure that all pages in the range which were dirty on entry to
321 * sys_sync_file_range() are committed to disk.
324 * SYNC_FILE_RANGE_WAIT_BEFORE and SYNC_FILE_RANGE_WAIT_AFTER will detect any
325 * I/O errors or ENOSPC conditions and will return those to the caller, after
326 * clearing the EIO and ENOSPC flags in the address_space.
328 * It should be noted that none of these operations write out the file's
329 * metadata. So unless the application is strictly performing overwrites of
330 * already-instantiated disk blocks, there are no guarantees here that the data
331 * will be available after a crash.
333 SYSCALL_DEFINE(sync_file_range
)(int fd
, loff_t offset
, loff_t nbytes
,
338 struct address_space
*mapping
;
339 loff_t endbyte
; /* inclusive */
343 #ifdef CONFIG_FSYNC_CONTROL
344 if (!fsynccontrol_fsync_enabled())
349 if (flags
& ~VALID_FLAGS
)
352 endbyte
= offset
+ nbytes
;
356 if ((s64
)endbyte
< 0)
358 if (endbyte
< offset
)
361 if (sizeof(pgoff_t
) == 4) {
362 if (offset
>= (0x100000000ULL
<< PAGE_CACHE_SHIFT
)) {
364 * The range starts outside a 32 bit machine's
365 * pagecache addressing capabilities. Let it "succeed"
370 if (endbyte
>= (0x100000000ULL
<< PAGE_CACHE_SHIFT
)) {
381 endbyte
--; /* inclusive */
384 file
= fget_light(fd
, &fput_needed
);
388 i_mode
= file
->f_path
.dentry
->d_inode
->i_mode
;
390 if (!S_ISREG(i_mode
) && !S_ISBLK(i_mode
) && !S_ISDIR(i_mode
) &&
394 mapping
= file
->f_mapping
;
401 if (flags
& SYNC_FILE_RANGE_WAIT_BEFORE
) {
402 ret
= filemap_fdatawait_range(mapping
, offset
, endbyte
);
407 if (flags
& SYNC_FILE_RANGE_WRITE
) {
408 ret
= filemap_fdatawrite_range(mapping
, offset
, endbyte
);
413 if (flags
& SYNC_FILE_RANGE_WAIT_AFTER
)
414 ret
= filemap_fdatawait_range(mapping
, offset
, endbyte
);
417 fput_light(file
, fput_needed
);
421 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
422 asmlinkage
long SyS_sync_file_range(long fd
, loff_t offset
, loff_t nbytes
,
425 return SYSC_sync_file_range((int) fd
, offset
, nbytes
,
426 (unsigned int) flags
);
428 SYSCALL_ALIAS(sys_sync_file_range
, SyS_sync_file_range
);
431 /* It would be nice if people remember that not all the world's an i386
432 when they introduce new system calls */
433 SYSCALL_DEFINE(sync_file_range2
)(int fd
, unsigned int flags
,
434 loff_t offset
, loff_t nbytes
)
436 return sys_sync_file_range(fd
, offset
, nbytes
, flags
);
438 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
439 asmlinkage
long SyS_sync_file_range2(long fd
, long flags
,
440 loff_t offset
, loff_t nbytes
)
442 return SYSC_sync_file_range2((int) fd
, (unsigned int) flags
,
445 SYSCALL_ALIAS(sys_sync_file_range2
, SyS_sync_file_range2
);