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)
25 if (unlikely(laptop_mode))
26 laptop_sync_completion();
30 static void do_sync_work(struct work_struct *work)
33 * Sync twice to reduce the possibility we skipped some inodes / pages
34 * because they were temporarily locked
38 printk("Emergency Sync complete\n");
42 void emergency_sync(void)
44 struct work_struct *work;
46 work = kmalloc(sizeof(*work), GFP_ATOMIC);
48 INIT_WORK(work, do_sync_work);
54 * Generic function to fsync a file.
56 * filp may be NULL if called via the msync of a vma.
58 int file_fsync(struct file *filp, struct dentry *dentry, int datasync)
60 struct inode * inode = dentry->d_inode;
61 struct super_block * sb;
64 /* sync the inode to buffers */
65 ret = write_inode_now(inode, 0);
67 /* sync the superblock to buffers */
70 if (sb->s_dirt && sb->s_op->write_super)
71 sb->s_op->write_super(sb);
74 /* .. finally sync the buffers to disk */
75 err = sync_blockdev(sb->s_bdev);
82 * vfs_fsync - perform a fsync or fdatasync on a file
84 * @dentry: dentry of @file
85 * @data: only perform a fdatasync operation
87 * Write back data and metadata for @file to disk. If @datasync is
88 * set only metadata needed to access modified file data is written.
90 * In case this function is called from nfsd @file may be %NULL and
91 * only @dentry is set. This can only happen when the filesystem
92 * implements the export_operations API.
94 int vfs_fsync(struct file *file, struct dentry *dentry, int datasync)
96 const struct file_operations *fop;
97 struct address_space *mapping;
101 * Get mapping and operations from the file in case we have
102 * as file, or get the default values for them in case we
103 * don't have a struct file available. Damn nfsd..
106 mapping = file->f_mapping;
109 mapping = dentry->d_inode->i_mapping;
110 fop = dentry->d_inode->i_fop;
113 if (!fop || !fop->fsync) {
118 ret = filemap_fdatawrite(mapping);
121 * We need to protect against concurrent writers, which could cause
122 * livelocks in fsync_buffers_list().
124 mutex_lock(&mapping->host->i_mutex);
125 err = fop->fsync(file, dentry, datasync);
128 mutex_unlock(&mapping->host->i_mutex);
129 err = filemap_fdatawait(mapping);
135 EXPORT_SYMBOL(vfs_fsync);
137 static int do_fsync(unsigned int fd, int datasync)
144 ret = vfs_fsync(file, file->f_path.dentry, datasync);
150 SYSCALL_DEFINE1(fsync, unsigned int, fd)
152 return do_fsync(fd, 0);
155 SYSCALL_DEFINE1(fdatasync, unsigned int, fd)
157 return do_fsync(fd, 1);
161 * sys_sync_file_range() permits finely controlled syncing over a segment of
162 * a file in the range offset .. (offset+nbytes-1) inclusive. If nbytes is
163 * zero then sys_sync_file_range() will operate from offset out to EOF.
167 * SYNC_FILE_RANGE_WAIT_BEFORE: wait upon writeout of all pages in the range
168 * before performing the write.
170 * SYNC_FILE_RANGE_WRITE: initiate writeout of all those dirty pages in the
171 * range which are not presently under writeback. Note that this may block for
172 * significant periods due to exhaustion of disk request structures.
174 * SYNC_FILE_RANGE_WAIT_AFTER: wait upon writeout of all pages in the range
175 * after performing the write.
177 * Useful combinations of the flag bits are:
179 * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE: ensures that all pages
180 * in the range which were dirty on entry to sys_sync_file_range() are placed
181 * under writeout. This is a start-write-for-data-integrity operation.
183 * SYNC_FILE_RANGE_WRITE: start writeout of all dirty pages in the range which
184 * are not presently under writeout. This is an asynchronous flush-to-disk
185 * operation. Not suitable for data integrity operations.
187 * SYNC_FILE_RANGE_WAIT_BEFORE (or SYNC_FILE_RANGE_WAIT_AFTER): wait for
188 * completion of writeout of all pages in the range. This will be used after an
189 * earlier SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE operation to wait
190 * for that operation to complete and to return the result.
192 * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE|SYNC_FILE_RANGE_WAIT_AFTER:
193 * a traditional sync() operation. This is a write-for-data-integrity operation
194 * which will ensure that all pages in the range which were dirty on entry to
195 * sys_sync_file_range() are committed to disk.
198 * SYNC_FILE_RANGE_WAIT_BEFORE and SYNC_FILE_RANGE_WAIT_AFTER will detect any
199 * I/O errors or ENOSPC conditions and will return those to the caller, after
200 * clearing the EIO and ENOSPC flags in the address_space.
202 * It should be noted that none of these operations write out the file's
203 * metadata. So unless the application is strictly performing overwrites of
204 * already-instantiated disk blocks, there are no guarantees here that the data
205 * will be available after a crash.
207 SYSCALL_DEFINE(sync_file_range)(int fd, loff_t offset, loff_t nbytes,
212 loff_t endbyte; /* inclusive */
217 if (flags & ~VALID_FLAGS)
220 endbyte = offset + nbytes;
224 if ((s64)endbyte < 0)
226 if (endbyte < offset)
229 if (sizeof(pgoff_t) == 4) {
230 if (offset >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
232 * The range starts outside a 32 bit machine's
233 * pagecache addressing capabilities. Let it "succeed"
238 if (endbyte >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
249 endbyte--; /* inclusive */
252 file = fget_light(fd, &fput_needed);
256 i_mode = file->f_path.dentry->d_inode->i_mode;
258 if (!S_ISREG(i_mode) && !S_ISBLK(i_mode) && !S_ISDIR(i_mode) &&
262 ret = do_sync_mapping_range(file->f_mapping, offset, endbyte, flags);
264 fput_light(file, fput_needed);
268 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
269 asmlinkage long SyS_sync_file_range(long fd, loff_t offset, loff_t nbytes,
272 return SYSC_sync_file_range((int) fd, offset, nbytes,
273 (unsigned int) flags);
275 SYSCALL_ALIAS(sys_sync_file_range, SyS_sync_file_range);
278 /* It would be nice if people remember that not all the world's an i386
279 when they introduce new system calls */
280 SYSCALL_DEFINE(sync_file_range2)(int fd, unsigned int flags,
281 loff_t offset, loff_t nbytes)
283 return sys_sync_file_range(fd, offset, nbytes, flags);
285 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
286 asmlinkage long SyS_sync_file_range2(long fd, long flags,
287 loff_t offset, loff_t nbytes)
289 return SYSC_sync_file_range2((int) fd, (unsigned int) flags,
292 SYSCALL_ALIAS(sys_sync_file_range2, SyS_sync_file_range2);
296 * `endbyte' is inclusive
298 int do_sync_mapping_range(struct address_space *mapping, loff_t offset,
299 loff_t endbyte, unsigned int flags)
309 if (flags & SYNC_FILE_RANGE_WAIT_BEFORE) {
310 ret = wait_on_page_writeback_range(mapping,
311 offset >> PAGE_CACHE_SHIFT,
312 endbyte >> PAGE_CACHE_SHIFT);
317 if (flags & SYNC_FILE_RANGE_WRITE) {
318 ret = __filemap_fdatawrite_range(mapping, offset, endbyte,
324 if (flags & SYNC_FILE_RANGE_WAIT_AFTER) {
325 ret = wait_on_page_writeback_range(mapping,
326 offset >> PAGE_CACHE_SHIFT,
327 endbyte >> PAGE_CACHE_SHIFT);
332 EXPORT_SYMBOL_GPL(do_sync_mapping_range);