xref: /linux/fs/sync.c (revision a33f32244d8550da8b4a26e277ce07d5c6d158b5)
1 /*
2  * High-level sync()-related operations
3  */
4 
5 #include <linux/kernel.h>
6 #include <linux/file.h>
7 #include <linux/fs.h>
8 #include <linux/slab.h>
9 #include <linux/module.h>
10 #include <linux/sched.h>
11 #include <linux/writeback.h>
12 #include <linux/syscalls.h>
13 #include <linux/linkage.h>
14 #include <linux/pagemap.h>
15 #include <linux/quotaops.h>
16 #include <linux/buffer_head.h>
17 #include "internal.h"
18 
19 #define VALID_FLAGS (SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE| \
20 			SYNC_FILE_RANGE_WAIT_AFTER)
21 
22 /*
23  * 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.
28  */
29 static int __sync_filesystem(struct super_block *sb, int wait)
30 {
31 	/*
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 
38 	if (sb->s_qcop && sb->s_qcop->quota_sync)
39 		sb->s_qcop->quota_sync(sb, -1, wait);
40 
41 	if (wait)
42 		sync_inodes_sb(sb);
43 	else
44 		writeback_inodes_sb(sb);
45 
46 	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  */
56 int sync_filesystem(struct super_block *sb)
57 {
58 	int ret;
59 
60 	/*
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 
72 	ret = __sync_filesystem(sb, 0);
73 	if (ret < 0)
74 		return ret;
75 	return __sync_filesystem(sb, 1);
76 }
77 EXPORT_SYMBOL_GPL(sync_filesystem);
78 
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  */
93 static 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);
100 	list_for_each_entry(sb, &super_blocks, s_list)
101 		sb->s_need_sync = 1;
102 
103 restart:
104 	list_for_each_entry(sb, &super_blocks, s_list) {
105 		if (!sb->s_need_sync)
106 			continue;
107 		sb->s_need_sync = 0;
108 		sb->s_count++;
109 		spin_unlock(&sb_lock);
110 
111 		down_read(&sb->s_umount);
112 		if (!(sb->s_flags & MS_RDONLY) && sb->s_root && sb->s_bdi)
113 			__sync_filesystem(sb, wait);
114 		up_read(&sb->s_umount);
115 
116 		/* 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 
125 /*
126  * sync everything.  Start out by waking pdflush, because that writes back
127  * all queues in parallel.
128  */
129 SYSCALL_DEFINE0(sync)
130 {
131 	wakeup_flusher_threads(0);
132 	sync_filesystems(0);
133 	sync_filesystems(1);
134 	if (unlikely(laptop_mode))
135 		laptop_sync_completion();
136 	return 0;
137 }
138 
139 static void do_sync_work(struct work_struct *work)
140 {
141 	/*
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");
148 	kfree(work);
149 }
150 
151 void emergency_sync(void)
152 {
153 	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 	}
160 }
161 
162 /*
163  * Generic function to fsync a file.
164  *
165  * filp may be NULL if called via the msync of a vma.
166  */
167 int 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;
178 	if (sb->s_dirt && sb->s_op->write_super)
179 		sb->s_op->write_super(sb);
180 
181 	/* .. finally sync the buffers to disk */
182 	err = sync_blockdev(sb->s_bdev);
183 	if (!ret)
184 		ret = err;
185 	return ret;
186 }
187 EXPORT_SYMBOL(file_fsync);
188 
189 /**
190  * vfs_fsync_range - helper to sync a range of data & metadata to disk
191  * @file:		file to sync
192  * @dentry:		dentry of @file
193  * @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
196  *
197  * 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.
200  *
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  */
205 int vfs_fsync_range(struct file *file, struct dentry *dentry, loff_t start,
206 		    loff_t end, int datasync)
207 {
208 	const struct file_operations *fop;
209 	struct address_space *mapping;
210 	int err, ret;
211 
212 	/*
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) {
226 		ret = -EINVAL;
227 		goto out;
228 	}
229 
230 	ret = filemap_write_and_wait_range(mapping, start, end);
231 
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);
237 	err = fop->fsync(file, dentry, datasync);
238 	if (!ret)
239 		ret = err;
240 	mutex_unlock(&mapping->host->i_mutex);
241 
242 out:
243 	return ret;
244 }
245 EXPORT_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  */
260 int vfs_fsync(struct file *file, struct dentry *dentry, int datasync)
261 {
262 	return vfs_fsync_range(file, dentry, 0, LLONG_MAX, datasync);
263 }
264 EXPORT_SYMBOL(vfs_fsync);
265 
266 static int do_fsync(unsigned int fd, int datasync)
267 {
268 	struct file *file;
269 	int ret = -EBADF;
270 
271 	file = fget(fd);
272 	if (file) {
273 		ret = vfs_fsync(file, file->f_path.dentry, datasync);
274 		fput(file);
275 	}
276 	return ret;
277 }
278 
279 SYSCALL_DEFINE1(fsync, unsigned int, fd)
280 {
281 	return do_fsync(fd, 0);
282 }
283 
284 SYSCALL_DEFINE1(fdatasync, unsigned int, fd)
285 {
286 	return do_fsync(fd, 1);
287 }
288 
289 /**
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  */
297 int generic_write_sync(struct file *file, loff_t pos, loff_t count)
298 {
299 	if (!(file->f_flags & O_DSYNC) && !IS_SYNC(file->f_mapping->host))
300 		return 0;
301 	return vfs_fsync_range(file, file->f_path.dentry, pos,
302 			       pos + count - 1,
303 			       (file->f_flags & __O_SYNC) ? 0 : 1);
304 }
305 EXPORT_SYMBOL(generic_write_sync);
306 
307 /*
308  * 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
318  * range which are not presently under writeback. Note that this may block for
319  * significant periods due to exhaustion of disk request structures.
320  *
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  */
354 SYSCALL_DEFINE(sync_file_range)(int fd, loff_t offset, loff_t nbytes,
355 				unsigned int flags)
356 {
357 	int ret;
358 	struct file *file;
359 	struct address_space *mapping;
360 	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)
395 		endbyte = LLONG_MAX;
396 	else
397 		endbyte--;		/* inclusive */
398 
399 	ret = -EBADF;
400 	file = fget_light(fd, &fput_needed);
401 	if (!file)
402 		goto out;
403 
404 	i_mode = file->f_path.dentry->d_inode->i_mode;
405 	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 
410 	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 
432 out_put:
433 	fput_light(file, fput_needed);
434 out:
435 	return ret;
436 }
437 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
438 asmlinkage 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 }
444 SYSCALL_ALIAS(sys_sync_file_range, SyS_sync_file_range);
445 #endif
446 
447 /* It would be nice if people remember that not all the world's an i386
448    when they introduce new system calls */
449 SYSCALL_DEFINE(sync_file_range2)(int fd, unsigned int flags,
450 				 loff_t offset, loff_t nbytes)
451 {
452 	return sys_sync_file_range(fd, offset, nbytes, flags);
453 }
454 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
455 asmlinkage 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 }
461 SYSCALL_ALIAS(sys_sync_file_range2, SyS_sync_file_range2);
462 #endif
463