blob: fc5c3d75cf3c736a7255b2101d1169786753f634 [file] [log] [blame]
Andrew Mortonf79e2ab2006-03-31 02:30:42 -08001/*
2 * High-level sync()-related operations
3 */
4
5#include <linux/kernel.h>
6#include <linux/file.h>
7#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Andrew Mortonf79e2ab2006-03-31 02:30:42 -08009#include <linux/module.h>
Al Viro914e2632006-10-18 13:55:46 -040010#include <linux/sched.h>
Andrew Mortonf79e2ab2006-03-31 02:30:42 -080011#include <linux/writeback.h>
12#include <linux/syscalls.h>
13#include <linux/linkage.h>
14#include <linux/pagemap.h>
David Howellscf9a2ae2006-08-29 19:05:54 +010015#include <linux/quotaops.h>
16#include <linux/buffer_head.h>
Jan Kara5a3e5cb2009-04-27 16:43:48 +020017#include "internal.h"
Andrew Mortonf79e2ab2006-03-31 02:30:42 -080018
19#define VALID_FLAGS (SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE| \
20 SYNC_FILE_RANGE_WAIT_AFTER)
21
Jan Karac15c54f2009-04-27 16:43:52 +020022/*
Jens Axboed8a85592009-09-02 12:34:32 +020023 * Do the filesystem syncing work. For simple filesystems
24 * writeback_inodes_sb(sb) just dirties buffers with inodes so we have to
25 * submit IO for these buffers via __sync_blockdev(). This also speeds up the
26 * wait == 1 case since in that case write_inode() functions do
27 * sync_dirty_buffer() and thus effectively write one block at a time.
Jan Karac15c54f2009-04-27 16:43:52 +020028 */
Jan Kara60b06802009-04-27 16:43:53 +020029static int __sync_filesystem(struct super_block *sb, int wait)
Jan Karac15c54f2009-04-27 16:43:52 +020030{
Jens Axboe32a88aa2009-09-16 15:02:33 +020031 /*
32 * This should be safe, as we require bdi backing to actually
33 * write out data in the first place
34 */
35 if (!sb->s_bdi)
36 return 0;
37
Christoph Hellwig5fb324a2010-02-16 03:44:52 -050038 if (sb->s_qcop && sb->s_qcop->quota_sync)
39 sb->s_qcop->quota_sync(sb, -1, wait);
40
41 if (wait)
Jens Axboed8a85592009-09-02 12:34:32 +020042 sync_inodes_sb(sb);
Christoph Hellwig5fb324a2010-02-16 03:44:52 -050043 else
44 writeback_inodes_sb(sb);
45
Jan Karac15c54f2009-04-27 16:43:52 +020046 if (sb->s_op->sync_fs)
47 sb->s_op->sync_fs(sb, wait);
48 return __sync_blockdev(sb->s_bdev, wait);
49}
50
51/*
52 * Write out and wait upon all dirty data associated with this
53 * superblock. Filesystem data as well as the underlying block
54 * device. Takes the superblock lock.
55 */
Jan Kara60b06802009-04-27 16:43:53 +020056int sync_filesystem(struct super_block *sb)
Jan Karac15c54f2009-04-27 16:43:52 +020057{
58 int ret;
59
Christoph Hellwig5af79262009-05-05 15:41:25 +020060 /*
61 * We need to be protected against the filesystem going from
62 * r/o to r/w or vice versa.
63 */
64 WARN_ON(!rwsem_is_locked(&sb->s_umount));
65
66 /*
67 * No point in syncing out anything if the filesystem is read-only.
68 */
69 if (sb->s_flags & MS_RDONLY)
70 return 0;
71
Jan Kara60b06802009-04-27 16:43:53 +020072 ret = __sync_filesystem(sb, 0);
Jan Karac15c54f2009-04-27 16:43:52 +020073 if (ret < 0)
74 return ret;
Jan Kara60b06802009-04-27 16:43:53 +020075 return __sync_filesystem(sb, 1);
Jan Karac15c54f2009-04-27 16:43:52 +020076}
Jan Kara60b06802009-04-27 16:43:53 +020077EXPORT_SYMBOL_GPL(sync_filesystem);
Jan Karac15c54f2009-04-27 16:43:52 +020078
79/*
80 * Sync all the data for all the filesystems (called by sys_sync() and
81 * emergency sync)
82 *
83 * This operation is careful to avoid the livelock which could easily happen
84 * if two or more filesystems are being continuously dirtied. s_need_sync
85 * is used only here. We set it against all filesystems and then clear it as
86 * we sync them. So redirtied filesystems are skipped.
87 *
88 * But if process A is currently running sync_filesystems and then process B
89 * calls sync_filesystems as well, process B will set all the s_need_sync
90 * flags again, which will cause process A to resync everything. Fix that with
91 * a local mutex.
92 */
93static void sync_filesystems(int wait)
94{
95 struct super_block *sb;
96 static DEFINE_MUTEX(mutex);
97
98 mutex_lock(&mutex); /* Could be down_interruptible */
99 spin_lock(&sb_lock);
Christoph Hellwig5af79262009-05-05 15:41:25 +0200100 list_for_each_entry(sb, &super_blocks, s_list)
Jan Karac15c54f2009-04-27 16:43:52 +0200101 sb->s_need_sync = 1;
Jan Karac15c54f2009-04-27 16:43:52 +0200102
103restart:
104 list_for_each_entry(sb, &super_blocks, s_list) {
105 if (!sb->s_need_sync)
106 continue;
107 sb->s_need_sync = 0;
Jan Karac15c54f2009-04-27 16:43:52 +0200108 sb->s_count++;
109 spin_unlock(&sb_lock);
Christoph Hellwig5af79262009-05-05 15:41:25 +0200110
Jan Karac15c54f2009-04-27 16:43:52 +0200111 down_read(&sb->s_umount);
Jens Axboe32a88aa2009-09-16 15:02:33 +0200112 if (!(sb->s_flags & MS_RDONLY) && sb->s_root && sb->s_bdi)
Jan Kara60b06802009-04-27 16:43:53 +0200113 __sync_filesystem(sb, wait);
Jan Karac15c54f2009-04-27 16:43:52 +0200114 up_read(&sb->s_umount);
Christoph Hellwig5af79262009-05-05 15:41:25 +0200115
Jan Karac15c54f2009-04-27 16:43:52 +0200116 /* restart only when sb is no longer on the list */
117 spin_lock(&sb_lock);
118 if (__put_super_and_need_restart(sb))
119 goto restart;
120 }
121 spin_unlock(&sb_lock);
122 mutex_unlock(&mutex);
123}
124
Zhang, Yanmin3beab0b2009-07-05 12:08:08 -0700125/*
126 * sync everything. Start out by waking pdflush, because that writes back
127 * all queues in parallel.
128 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +0100129SYSCALL_DEFINE0(sync)
David Howellscf9a2ae2006-08-29 19:05:54 +0100130{
Jens Axboe03ba3782009-09-09 09:08:54 +0200131 wakeup_flusher_threads(0);
Jan Kara5cee5812009-04-27 16:43:51 +0200132 sync_filesystems(0);
133 sync_filesystems(1);
134 if (unlikely(laptop_mode))
135 laptop_sync_completion();
David Howellscf9a2ae2006-08-29 19:05:54 +0100136 return 0;
137}
138
Jens Axboea2a95372009-03-17 09:38:40 +0100139static void do_sync_work(struct work_struct *work)
140{
Jan Kara5cee5812009-04-27 16:43:51 +0200141 /*
142 * Sync twice to reduce the possibility we skipped some inodes / pages
143 * because they were temporarily locked
144 */
145 sync_filesystems(0);
146 sync_filesystems(0);
147 printk("Emergency Sync complete\n");
Jens Axboea2a95372009-03-17 09:38:40 +0100148 kfree(work);
149}
150
David Howellscf9a2ae2006-08-29 19:05:54 +0100151void emergency_sync(void)
152{
Jens Axboea2a95372009-03-17 09:38:40 +0100153 struct work_struct *work;
154
155 work = kmalloc(sizeof(*work), GFP_ATOMIC);
156 if (work) {
157 INIT_WORK(work, do_sync_work);
158 schedule_work(work);
159 }
David Howellscf9a2ae2006-08-29 19:05:54 +0100160}
161
162/*
163 * Generic function to fsync a file.
164 *
165 * filp may be NULL if called via the msync of a vma.
166 */
167int file_fsync(struct file *filp, struct dentry *dentry, int datasync)
168{
169 struct inode * inode = dentry->d_inode;
170 struct super_block * sb;
171 int ret, err;
172
173 /* sync the inode to buffers */
174 ret = write_inode_now(inode, 0);
175
176 /* sync the superblock to buffers */
177 sb = inode->i_sb;
OGAWA Hirofumi762873c2008-04-29 00:59:42 -0700178 if (sb->s_dirt && sb->s_op->write_super)
David Howellscf9a2ae2006-08-29 19:05:54 +0100179 sb->s_op->write_super(sb);
David Howellscf9a2ae2006-08-29 19:05:54 +0100180
181 /* .. finally sync the buffers to disk */
182 err = sync_blockdev(sb->s_bdev);
183 if (!ret)
184 ret = err;
185 return ret;
186}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -0700187EXPORT_SYMBOL(file_fsync);
David Howellscf9a2ae2006-08-29 19:05:54 +0100188
Christoph Hellwig4c728ef2008-12-22 21:11:15 +0100189/**
Jan Kara148f9482009-08-17 19:52:36 +0200190 * vfs_fsync_range - helper to sync a range of data & metadata to disk
Christoph Hellwig4c728ef2008-12-22 21:11:15 +0100191 * @file: file to sync
192 * @dentry: dentry of @file
Jan Kara148f9482009-08-17 19:52:36 +0200193 * @start: offset in bytes of the beginning of data range to sync
194 * @end: offset in bytes of the end of data range (inclusive)
195 * @datasync: perform only datasync
Christoph Hellwig4c728ef2008-12-22 21:11:15 +0100196 *
Jan Kara148f9482009-08-17 19:52:36 +0200197 * Write back data in range @start..@end and metadata for @file to disk. If
198 * @datasync is set only metadata needed to access modified file data is
199 * written.
Christoph Hellwig4c728ef2008-12-22 21:11:15 +0100200 *
201 * In case this function is called from nfsd @file may be %NULL and
202 * only @dentry is set. This can only happen when the filesystem
203 * implements the export_operations API.
204 */
Jan Kara148f9482009-08-17 19:52:36 +0200205int vfs_fsync_range(struct file *file, struct dentry *dentry, loff_t start,
206 loff_t end, int datasync)
David Howellscf9a2ae2006-08-29 19:05:54 +0100207{
Christoph Hellwig4c728ef2008-12-22 21:11:15 +0100208 const struct file_operations *fop;
209 struct address_space *mapping;
210 int err, ret;
David Howellscf9a2ae2006-08-29 19:05:54 +0100211
Christoph Hellwig4c728ef2008-12-22 21:11:15 +0100212 /*
213 * Get mapping and operations from the file in case we have
214 * as file, or get the default values for them in case we
215 * don't have a struct file available. Damn nfsd..
216 */
217 if (file) {
218 mapping = file->f_mapping;
219 fop = file->f_op;
220 } else {
221 mapping = dentry->d_inode->i_mapping;
222 fop = dentry->d_inode->i_fop;
223 }
224
225 if (!fop || !fop->fsync) {
David Howellscf9a2ae2006-08-29 19:05:54 +0100226 ret = -EINVAL;
227 goto out;
228 }
229
Christoph Hellwig2daea672009-09-03 12:39:39 +0200230 ret = filemap_write_and_wait_range(mapping, start, end);
David Howellscf9a2ae2006-08-29 19:05:54 +0100231
232 /*
233 * We need to protect against concurrent writers, which could cause
234 * livelocks in fsync_buffers_list().
235 */
236 mutex_lock(&mapping->host->i_mutex);
Christoph Hellwig4c728ef2008-12-22 21:11:15 +0100237 err = fop->fsync(file, dentry, datasync);
David Howellscf9a2ae2006-08-29 19:05:54 +0100238 if (!ret)
239 ret = err;
240 mutex_unlock(&mapping->host->i_mutex);
Jan Kara148f9482009-08-17 19:52:36 +0200241
David Howellscf9a2ae2006-08-29 19:05:54 +0100242out:
243 return ret;
244}
Jan Kara148f9482009-08-17 19:52:36 +0200245EXPORT_SYMBOL(vfs_fsync_range);
246
247/**
248 * vfs_fsync - perform a fsync or fdatasync on a file
249 * @file: file to sync
250 * @dentry: dentry of @file
251 * @datasync: only perform a fdatasync operation
252 *
253 * Write back data and metadata for @file to disk. If @datasync is
254 * set only metadata needed to access modified file data is written.
255 *
256 * In case this function is called from nfsd @file may be %NULL and
257 * only @dentry is set. This can only happen when the filesystem
258 * implements the export_operations API.
259 */
260int vfs_fsync(struct file *file, struct dentry *dentry, int datasync)
261{
262 return vfs_fsync_range(file, dentry, 0, LLONG_MAX, datasync);
263}
Christoph Hellwig4c728ef2008-12-22 21:11:15 +0100264EXPORT_SYMBOL(vfs_fsync);
David Howellscf9a2ae2006-08-29 19:05:54 +0100265
Christoph Hellwig4c728ef2008-12-22 21:11:15 +0100266static int do_fsync(unsigned int fd, int datasync)
David Howellscf9a2ae2006-08-29 19:05:54 +0100267{
268 struct file *file;
269 int ret = -EBADF;
270
271 file = fget(fd);
272 if (file) {
Christoph Hellwig4c728ef2008-12-22 21:11:15 +0100273 ret = vfs_fsync(file, file->f_path.dentry, datasync);
David Howellscf9a2ae2006-08-29 19:05:54 +0100274 fput(file);
275 }
276 return ret;
277}
278
Heiko Carstensa5f8fa92009-01-14 14:14:11 +0100279SYSCALL_DEFINE1(fsync, unsigned int, fd)
David Howellscf9a2ae2006-08-29 19:05:54 +0100280{
Christoph Hellwig4c728ef2008-12-22 21:11:15 +0100281 return do_fsync(fd, 0);
David Howellscf9a2ae2006-08-29 19:05:54 +0100282}
283
Heiko Carstensa5f8fa92009-01-14 14:14:11 +0100284SYSCALL_DEFINE1(fdatasync, unsigned int, fd)
David Howellscf9a2ae2006-08-29 19:05:54 +0100285{
Christoph Hellwig4c728ef2008-12-22 21:11:15 +0100286 return do_fsync(fd, 1);
David Howellscf9a2ae2006-08-29 19:05:54 +0100287}
288
Jan Kara148f9482009-08-17 19:52:36 +0200289/**
290 * generic_write_sync - perform syncing after a write if file / inode is sync
291 * @file: file to which the write happened
292 * @pos: offset where the write started
293 * @count: length of the write
294 *
295 * This is just a simple wrapper about our general syncing function.
296 */
297int generic_write_sync(struct file *file, loff_t pos, loff_t count)
298{
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +0100299 if (!(file->f_flags & O_DSYNC) && !IS_SYNC(file->f_mapping->host))
Jan Kara148f9482009-08-17 19:52:36 +0200300 return 0;
301 return vfs_fsync_range(file, file->f_path.dentry, pos,
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +0100302 pos + count - 1,
303 (file->f_flags & __O_SYNC) ? 0 : 1);
Jan Kara148f9482009-08-17 19:52:36 +0200304}
305EXPORT_SYMBOL(generic_write_sync);
306
David Howellscf9a2ae2006-08-29 19:05:54 +0100307/*
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800308 * sys_sync_file_range() permits finely controlled syncing over a segment of
309 * a file in the range offset .. (offset+nbytes-1) inclusive. If nbytes is
310 * zero then sys_sync_file_range() will operate from offset out to EOF.
311 *
312 * The flag bits are:
313 *
314 * SYNC_FILE_RANGE_WAIT_BEFORE: wait upon writeout of all pages in the range
315 * before performing the write.
316 *
317 * SYNC_FILE_RANGE_WRITE: initiate writeout of all those dirty pages in the
Pavel Machekcce77082008-07-23 21:27:36 -0700318 * range which are not presently under writeback. Note that this may block for
319 * significant periods due to exhaustion of disk request structures.
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800320 *
321 * SYNC_FILE_RANGE_WAIT_AFTER: wait upon writeout of all pages in the range
322 * after performing the write.
323 *
324 * Useful combinations of the flag bits are:
325 *
326 * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE: ensures that all pages
327 * in the range which were dirty on entry to sys_sync_file_range() are placed
328 * under writeout. This is a start-write-for-data-integrity operation.
329 *
330 * SYNC_FILE_RANGE_WRITE: start writeout of all dirty pages in the range which
331 * are not presently under writeout. This is an asynchronous flush-to-disk
332 * operation. Not suitable for data integrity operations.
333 *
334 * SYNC_FILE_RANGE_WAIT_BEFORE (or SYNC_FILE_RANGE_WAIT_AFTER): wait for
335 * completion of writeout of all pages in the range. This will be used after an
336 * earlier SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE operation to wait
337 * for that operation to complete and to return the result.
338 *
339 * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE|SYNC_FILE_RANGE_WAIT_AFTER:
340 * a traditional sync() operation. This is a write-for-data-integrity operation
341 * which will ensure that all pages in the range which were dirty on entry to
342 * sys_sync_file_range() are committed to disk.
343 *
344 *
345 * SYNC_FILE_RANGE_WAIT_BEFORE and SYNC_FILE_RANGE_WAIT_AFTER will detect any
346 * I/O errors or ENOSPC conditions and will return those to the caller, after
347 * clearing the EIO and ENOSPC flags in the address_space.
348 *
349 * It should be noted that none of these operations write out the file's
350 * metadata. So unless the application is strictly performing overwrites of
351 * already-instantiated disk blocks, there are no guarantees here that the data
352 * will be available after a crash.
353 */
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100354SYSCALL_DEFINE(sync_file_range)(int fd, loff_t offset, loff_t nbytes,
355 unsigned int flags)
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800356{
357 int ret;
358 struct file *file;
Christoph Hellwig7a0ad102009-12-17 14:24:40 +0100359 struct address_space *mapping;
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800360 loff_t endbyte; /* inclusive */
361 int fput_needed;
362 umode_t i_mode;
363
364 ret = -EINVAL;
365 if (flags & ~VALID_FLAGS)
366 goto out;
367
368 endbyte = offset + nbytes;
369
370 if ((s64)offset < 0)
371 goto out;
372 if ((s64)endbyte < 0)
373 goto out;
374 if (endbyte < offset)
375 goto out;
376
377 if (sizeof(pgoff_t) == 4) {
378 if (offset >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
379 /*
380 * The range starts outside a 32 bit machine's
381 * pagecache addressing capabilities. Let it "succeed"
382 */
383 ret = 0;
384 goto out;
385 }
386 if (endbyte >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
387 /*
388 * Out to EOF
389 */
390 nbytes = 0;
391 }
392 }
393
394 if (nbytes == 0)
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700395 endbyte = LLONG_MAX;
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800396 else
397 endbyte--; /* inclusive */
398
399 ret = -EBADF;
400 file = fget_light(fd, &fput_needed);
401 if (!file)
402 goto out;
403
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800404 i_mode = file->f_path.dentry->d_inode->i_mode;
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800405 ret = -ESPIPE;
406 if (!S_ISREG(i_mode) && !S_ISBLK(i_mode) && !S_ISDIR(i_mode) &&
407 !S_ISLNK(i_mode))
408 goto out_put;
409
Christoph Hellwig7a0ad102009-12-17 14:24:40 +0100410 mapping = file->f_mapping;
411 if (!mapping) {
412 ret = -EINVAL;
413 goto out_put;
414 }
415
416 ret = 0;
417 if (flags & SYNC_FILE_RANGE_WAIT_BEFORE) {
418 ret = filemap_fdatawait_range(mapping, offset, endbyte);
419 if (ret < 0)
420 goto out_put;
421 }
422
423 if (flags & SYNC_FILE_RANGE_WRITE) {
424 ret = filemap_fdatawrite_range(mapping, offset, endbyte);
425 if (ret < 0)
426 goto out_put;
427 }
428
429 if (flags & SYNC_FILE_RANGE_WAIT_AFTER)
430 ret = filemap_fdatawait_range(mapping, offset, endbyte);
431
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800432out_put:
433 fput_light(file, fput_needed);
434out:
435 return ret;
436}
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100437#ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
438asmlinkage long SyS_sync_file_range(long fd, loff_t offset, loff_t nbytes,
439 long flags)
440{
441 return SYSC_sync_file_range((int) fd, offset, nbytes,
442 (unsigned int) flags);
443}
444SYSCALL_ALIAS(sys_sync_file_range, SyS_sync_file_range);
445#endif
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800446
David Woodhouseedd5cd42007-06-27 14:10:09 -0700447/* It would be nice if people remember that not all the world's an i386
448 when they introduce new system calls */
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100449SYSCALL_DEFINE(sync_file_range2)(int fd, unsigned int flags,
450 loff_t offset, loff_t nbytes)
David Woodhouseedd5cd42007-06-27 14:10:09 -0700451{
452 return sys_sync_file_range(fd, offset, nbytes, flags);
453}
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100454#ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
455asmlinkage long SyS_sync_file_range2(long fd, long flags,
456 loff_t offset, loff_t nbytes)
457{
458 return SYSC_sync_file_range2((int) fd, (unsigned int) flags,
459 offset, nbytes);
460}
461SYSCALL_ALIAS(sys_sync_file_range2, SyS_sync_file_range2);
462#endif