Merge branch 'for-2.6.34' of git://linux-nfs.org/~bfields/linux
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / sync.c
blobfc5c3d75cf3c736a7255b2101d1169786753f634
1 /*
2 * High-level sync()-related operations
3 */
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"
19 #define VALID_FLAGS (SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE| \
20 SYNC_FILE_RANGE_WAIT_AFTER)
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.
29 static int __sync_filesystem(struct super_block *sb, int wait)
32 * This should be safe, as we require bdi backing to actually
33 * write out data in the first place
35 if (!sb->s_bdi)
36 return 0;
38 if (sb->s_qcop && sb->s_qcop->quota_sync)
39 sb->s_qcop->quota_sync(sb, -1, wait);
41 if (wait)
42 sync_inodes_sb(sb);
43 else
44 writeback_inodes_sb(sb);
46 if (sb->s_op->sync_fs)
47 sb->s_op->sync_fs(sb, wait);
48 return __sync_blockdev(sb->s_bdev, wait);
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.
56 int sync_filesystem(struct super_block *sb)
58 int ret;
61 * We need to be protected against the filesystem going from
62 * r/o to r/w or vice versa.
64 WARN_ON(!rwsem_is_locked(&sb->s_umount));
67 * No point in syncing out anything if the filesystem is read-only.
69 if (sb->s_flags & MS_RDONLY)
70 return 0;
72 ret = __sync_filesystem(sb, 0);
73 if (ret < 0)
74 return ret;
75 return __sync_filesystem(sb, 1);
77 EXPORT_SYMBOL_GPL(sync_filesystem);
80 * Sync all the data for all the filesystems (called by sys_sync() and
81 * emergency sync)
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.
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.
93 static void sync_filesystems(int wait)
95 struct super_block *sb;
96 static DEFINE_MUTEX(mutex);
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;
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);
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);
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;
121 spin_unlock(&sb_lock);
122 mutex_unlock(&mutex);
126 * sync everything. Start out by waking pdflush, because that writes back
127 * all queues in parallel.
129 SYSCALL_DEFINE0(sync)
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;
139 static void do_sync_work(struct work_struct *work)
142 * Sync twice to reduce the possibility we skipped some inodes / pages
143 * because they were temporarily locked
145 sync_filesystems(0);
146 sync_filesystems(0);
147 printk("Emergency Sync complete\n");
148 kfree(work);
151 void emergency_sync(void)
153 struct work_struct *work;
155 work = kmalloc(sizeof(*work), GFP_ATOMIC);
156 if (work) {
157 INIT_WORK(work, do_sync_work);
158 schedule_work(work);
163 * Generic function to fsync a file.
165 * filp may be NULL if called via the msync of a vma.
167 int file_fsync(struct file *filp, struct dentry *dentry, int datasync)
169 struct inode * inode = dentry->d_inode;
170 struct super_block * sb;
171 int ret, err;
173 /* sync the inode to buffers */
174 ret = write_inode_now(inode, 0);
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);
181 /* .. finally sync the buffers to disk */
182 err = sync_blockdev(sb->s_bdev);
183 if (!ret)
184 ret = err;
185 return ret;
187 EXPORT_SYMBOL(file_fsync);
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
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.
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.
205 int vfs_fsync_range(struct file *file, struct dentry *dentry, loff_t start,
206 loff_t end, int datasync)
208 const struct file_operations *fop;
209 struct address_space *mapping;
210 int err, ret;
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..
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;
225 if (!fop || !fop->fsync) {
226 ret = -EINVAL;
227 goto out;
230 ret = filemap_write_and_wait_range(mapping, start, end);
233 * We need to protect against concurrent writers, which could cause
234 * livelocks in fsync_buffers_list().
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);
242 out:
243 return ret;
245 EXPORT_SYMBOL(vfs_fsync_range);
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
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.
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.
260 int vfs_fsync(struct file *file, struct dentry *dentry, int datasync)
262 return vfs_fsync_range(file, dentry, 0, LLONG_MAX, datasync);
264 EXPORT_SYMBOL(vfs_fsync);
266 static int do_fsync(unsigned int fd, int datasync)
268 struct file *file;
269 int ret = -EBADF;
271 file = fget(fd);
272 if (file) {
273 ret = vfs_fsync(file, file->f_path.dentry, datasync);
274 fput(file);
276 return ret;
279 SYSCALL_DEFINE1(fsync, unsigned int, fd)
281 return do_fsync(fd, 0);
284 SYSCALL_DEFINE1(fdatasync, unsigned int, fd)
286 return do_fsync(fd, 1);
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
295 * This is just a simple wrapper about our general syncing function.
297 int generic_write_sync(struct file *file, loff_t pos, loff_t count)
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);
305 EXPORT_SYMBOL(generic_write_sync);
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.
312 * The flag bits are:
314 * SYNC_FILE_RANGE_WAIT_BEFORE: wait upon writeout of all pages in the range
315 * before performing the write.
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.
321 * SYNC_FILE_RANGE_WAIT_AFTER: wait upon writeout of all pages in the range
322 * after performing the write.
324 * Useful combinations of the flag bits are:
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.
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.
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.
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.
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.
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.
354 SYSCALL_DEFINE(sync_file_range)(int fd, loff_t offset, loff_t nbytes,
355 unsigned int flags)
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;
364 ret = -EINVAL;
365 if (flags & ~VALID_FLAGS)
366 goto out;
368 endbyte = offset + nbytes;
370 if ((s64)offset < 0)
371 goto out;
372 if ((s64)endbyte < 0)
373 goto out;
374 if (endbyte < offset)
375 goto out;
377 if (sizeof(pgoff_t) == 4) {
378 if (offset >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
380 * The range starts outside a 32 bit machine's
381 * pagecache addressing capabilities. Let it "succeed"
383 ret = 0;
384 goto out;
386 if (endbyte >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
388 * Out to EOF
390 nbytes = 0;
394 if (nbytes == 0)
395 endbyte = LLONG_MAX;
396 else
397 endbyte--; /* inclusive */
399 ret = -EBADF;
400 file = fget_light(fd, &fput_needed);
401 if (!file)
402 goto out;
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;
410 mapping = file->f_mapping;
411 if (!mapping) {
412 ret = -EINVAL;
413 goto out_put;
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;
423 if (flags & SYNC_FILE_RANGE_WRITE) {
424 ret = filemap_fdatawrite_range(mapping, offset, endbyte);
425 if (ret < 0)
426 goto out_put;
429 if (flags & SYNC_FILE_RANGE_WAIT_AFTER)
430 ret = filemap_fdatawait_range(mapping, offset, endbyte);
432 out_put:
433 fput_light(file, fput_needed);
434 out:
435 return ret;
437 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
438 asmlinkage long SyS_sync_file_range(long fd, loff_t offset, loff_t nbytes,
439 long flags)
441 return SYSC_sync_file_range((int) fd, offset, nbytes,
442 (unsigned int) flags);
444 SYSCALL_ALIAS(sys_sync_file_range, SyS_sync_file_range);
445 #endif
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)
452 return sys_sync_file_range(fd, offset, nbytes, flags);
454 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
455 asmlinkage long SyS_sync_file_range2(long fd, long flags,
456 loff_t offset, loff_t nbytes)
458 return SYSC_sync_file_range2((int) fd, (unsigned int) flags,
459 offset, nbytes);
461 SYSCALL_ALIAS(sys_sync_file_range2, SyS_sync_file_range2);
462 #endif