2 * High-level sync()-related operations
5 #include <linux/kernel.h>
6 #include <linux/file.h>
8 #include <linux/module.h>
9 #include <linux/sched.h>
10 #include <linux/writeback.h>
11 #include <linux/syscalls.h>
12 #include <linux/linkage.h>
13 #include <linux/pagemap.h>
14 #include <linux/quotaops.h>
15 #include <linux/buffer_head.h>
18 #define VALID_FLAGS (SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE| \
19 SYNC_FILE_RANGE_WAIT_AFTER)
22 * sync everything. Start out by waking pdflush, because that writes back
23 * all queues in parallel.
25 static void do_sync(unsigned long wait)
28 sync_inodes(0); /* All mappings, inodes and their blockdevs */
30 sync_inodes(wait); /* Mappings, inodes and blockdevs, again. */
31 sync_supers(); /* Write the superblocks */
32 sync_filesystems(0); /* Start syncing the filesystems */
33 sync_filesystems(wait); /* Waitingly sync the filesystems */
36 printk("Emergency Sync complete\n");
37 if (unlikely(laptop_mode))
38 laptop_sync_completion();
47 static void do_sync_work(struct work_struct *work)
53 void emergency_sync(void)
55 struct work_struct *work;
57 work = kmalloc(sizeof(*work), GFP_ATOMIC);
59 INIT_WORK(work, do_sync_work);
65 * Generic function to fsync a file.
67 * filp may be NULL if called via the msync of a vma.
69 int file_fsync(struct file *filp, struct dentry *dentry, int datasync)
71 struct inode * inode = dentry->d_inode;
72 struct super_block * sb;
75 /* sync the inode to buffers */
76 ret = write_inode_now(inode, 0);
78 /* sync the superblock to buffers */
81 if (sb->s_dirt && sb->s_op->write_super)
82 sb->s_op->write_super(sb);
85 /* .. finally sync the buffers to disk */
86 err = sync_blockdev(sb->s_bdev);
93 * vfs_fsync - perform a fsync or fdatasync on a file
95 * @dentry: dentry of @file
96 * @data: only perform a fdatasync operation
98 * Write back data and metadata for @file to disk. If @datasync is
99 * set only metadata needed to access modified file data is written.
101 * In case this function is called from nfsd @file may be %NULL and
102 * only @dentry is set. This can only happen when the filesystem
103 * implements the export_operations API.
105 int vfs_fsync(struct file *file, struct dentry *dentry, int datasync)
107 const struct file_operations *fop;
108 struct address_space *mapping;
112 * Get mapping and operations from the file in case we have
113 * as file, or get the default values for them in case we
114 * don't have a struct file available. Damn nfsd..
117 mapping = file->f_mapping;
120 mapping = dentry->d_inode->i_mapping;
121 fop = dentry->d_inode->i_fop;
124 if (!fop || !fop->fsync) {
129 ret = filemap_fdatawrite(mapping);
132 * We need to protect against concurrent writers, which could cause
133 * livelocks in fsync_buffers_list().
135 mutex_lock(&mapping->host->i_mutex);
136 err = fop->fsync(file, dentry, datasync);
139 mutex_unlock(&mapping->host->i_mutex);
140 err = filemap_fdatawait(mapping);
146 EXPORT_SYMBOL(vfs_fsync);
148 static int do_fsync(unsigned int fd, int datasync)
155 ret = vfs_fsync(file, file->f_path.dentry, datasync);
161 SYSCALL_DEFINE1(fsync, unsigned int, fd)
163 return do_fsync(fd, 0);
166 SYSCALL_DEFINE1(fdatasync, unsigned int, fd)
168 return do_fsync(fd, 1);
172 * sys_sync_file_range() permits finely controlled syncing over a segment of
173 * a file in the range offset .. (offset+nbytes-1) inclusive. If nbytes is
174 * zero then sys_sync_file_range() will operate from offset out to EOF.
178 * SYNC_FILE_RANGE_WAIT_BEFORE: wait upon writeout of all pages in the range
179 * before performing the write.
181 * SYNC_FILE_RANGE_WRITE: initiate writeout of all those dirty pages in the
182 * range which are not presently under writeback. Note that this may block for
183 * significant periods due to exhaustion of disk request structures.
185 * SYNC_FILE_RANGE_WAIT_AFTER: wait upon writeout of all pages in the range
186 * after performing the write.
188 * Useful combinations of the flag bits are:
190 * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE: ensures that all pages
191 * in the range which were dirty on entry to sys_sync_file_range() are placed
192 * under writeout. This is a start-write-for-data-integrity operation.
194 * SYNC_FILE_RANGE_WRITE: start writeout of all dirty pages in the range which
195 * are not presently under writeout. This is an asynchronous flush-to-disk
196 * operation. Not suitable for data integrity operations.
198 * SYNC_FILE_RANGE_WAIT_BEFORE (or SYNC_FILE_RANGE_WAIT_AFTER): wait for
199 * completion of writeout of all pages in the range. This will be used after an
200 * earlier SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE operation to wait
201 * for that operation to complete and to return the result.
203 * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE|SYNC_FILE_RANGE_WAIT_AFTER:
204 * a traditional sync() operation. This is a write-for-data-integrity operation
205 * which will ensure that all pages in the range which were dirty on entry to
206 * sys_sync_file_range() are committed to disk.
209 * SYNC_FILE_RANGE_WAIT_BEFORE and SYNC_FILE_RANGE_WAIT_AFTER will detect any
210 * I/O errors or ENOSPC conditions and will return those to the caller, after
211 * clearing the EIO and ENOSPC flags in the address_space.
213 * It should be noted that none of these operations write out the file's
214 * metadata. So unless the application is strictly performing overwrites of
215 * already-instantiated disk blocks, there are no guarantees here that the data
216 * will be available after a crash.
218 SYSCALL_DEFINE(sync_file_range)(int fd, loff_t offset, loff_t nbytes,
223 loff_t endbyte; /* inclusive */
228 if (flags & ~VALID_FLAGS)
231 endbyte = offset + nbytes;
235 if ((s64)endbyte < 0)
237 if (endbyte < offset)
240 if (sizeof(pgoff_t) == 4) {
241 if (offset >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
243 * The range starts outside a 32 bit machine's
244 * pagecache addressing capabilities. Let it "succeed"
249 if (endbyte >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
260 endbyte--; /* inclusive */
263 file = fget_light(fd, &fput_needed);
267 i_mode = file->f_path.dentry->d_inode->i_mode;
269 if (!S_ISREG(i_mode) && !S_ISBLK(i_mode) && !S_ISDIR(i_mode) &&
273 ret = do_sync_mapping_range(file->f_mapping, offset, endbyte, flags);
275 fput_light(file, fput_needed);
279 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
280 asmlinkage long SyS_sync_file_range(long fd, loff_t offset, loff_t nbytes,
283 return SYSC_sync_file_range((int) fd, offset, nbytes,
284 (unsigned int) flags);
286 SYSCALL_ALIAS(sys_sync_file_range, SyS_sync_file_range);
289 /* It would be nice if people remember that not all the world's an i386
290 when they introduce new system calls */
291 SYSCALL_DEFINE(sync_file_range2)(int fd, unsigned int flags,
292 loff_t offset, loff_t nbytes)
294 return sys_sync_file_range(fd, offset, nbytes, flags);
296 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
297 asmlinkage long SyS_sync_file_range2(long fd, long flags,
298 loff_t offset, loff_t nbytes)
300 return SYSC_sync_file_range2((int) fd, (unsigned int) flags,
303 SYSCALL_ALIAS(sys_sync_file_range2, SyS_sync_file_range2);
307 * `endbyte' is inclusive
309 int do_sync_mapping_range(struct address_space *mapping, loff_t offset,
310 loff_t endbyte, unsigned int flags)
320 if (flags & SYNC_FILE_RANGE_WAIT_BEFORE) {
321 ret = wait_on_page_writeback_range(mapping,
322 offset >> PAGE_CACHE_SHIFT,
323 endbyte >> PAGE_CACHE_SHIFT);
328 if (flags & SYNC_FILE_RANGE_WRITE) {
329 ret = __filemap_fdatawrite_range(mapping, offset, endbyte,
335 if (flags & SYNC_FILE_RANGE_WAIT_AFTER) {
336 ret = wait_on_page_writeback_range(mapping,
337 offset >> PAGE_CACHE_SHIFT,
338 endbyte >> PAGE_CACHE_SHIFT);
343 EXPORT_SYMBOL_GPL(do_sync_mapping_range);