2 * High-level sync()-related operations
5 #include <linux/kernel.h>
6 #include <linux/file.h>
8 #include <linux/module.h>
9 #include <linux/writeback.h>
10 #include <linux/syscalls.h>
11 #include <linux/linkage.h>
12 #include <linux/pagemap.h>
13 #include <linux/quotaops.h>
14 #include <linux/buffer_head.h>
16 #define VALID_FLAGS (SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE| \
17 SYNC_FILE_RANGE_WAIT_AFTER)
20 * sync everything. Start out by waking pdflush, because that writes back
21 * all queues in parallel.
23 static void do_sync(unsigned long wait)
26 sync_inodes(0); /* All mappings, inodes and their blockdevs */
28 sync_supers(); /* Write the superblocks */
29 sync_filesystems(0); /* Start syncing the filesystems */
30 sync_filesystems(wait); /* Waitingly sync the filesystems */
31 sync_inodes(wait); /* Mappings, inodes and blockdevs, again. */
33 printk("Emergency Sync complete\n");
34 if (unlikely(laptop_mode))
35 laptop_sync_completion();
38 asmlinkage long sys_sync(void)
44 void emergency_sync(void)
46 pdflush_operation(do_sync, 0);
50 * Generic function to fsync a file.
52 * filp may be NULL if called via the msync of a vma.
54 int file_fsync(struct file *filp, struct dentry *dentry, int datasync)
56 struct inode * inode = dentry->d_inode;
57 struct super_block * sb;
60 /* sync the inode to buffers */
61 ret = write_inode_now(inode, 0);
63 /* sync the superblock to buffers */
66 if (sb->s_op->write_super)
67 sb->s_op->write_super(sb);
70 /* .. finally sync the buffers to disk */
71 err = sync_blockdev(sb->s_bdev);
77 long do_fsync(struct file *file, int datasync)
81 struct address_space *mapping = file->f_mapping;
83 if (!file->f_op || !file->f_op->fsync) {
84 /* Why? We can still call filemap_fdatawrite */
89 ret = filemap_fdatawrite(mapping);
92 * We need to protect against concurrent writers, which could cause
93 * livelocks in fsync_buffers_list().
95 mutex_lock(&mapping->host->i_mutex);
96 err = file->f_op->fsync(file, file->f_dentry, datasync);
99 mutex_unlock(&mapping->host->i_mutex);
100 err = filemap_fdatawait(mapping);
107 static long __do_fsync(unsigned int fd, int datasync)
114 ret = do_fsync(file, datasync);
120 asmlinkage long sys_fsync(unsigned int fd)
122 return __do_fsync(fd, 0);
125 asmlinkage long sys_fdatasync(unsigned int fd)
127 return __do_fsync(fd, 1);
131 * sys_sync_file_range() permits finely controlled syncing over a segment of
132 * a file in the range offset .. (offset+nbytes-1) inclusive. If nbytes is
133 * zero then sys_sync_file_range() will operate from offset out to EOF.
137 * SYNC_FILE_RANGE_WAIT_BEFORE: wait upon writeout of all pages in the range
138 * before performing the write.
140 * SYNC_FILE_RANGE_WRITE: initiate writeout of all those dirty pages in the
141 * range which are not presently under writeback.
143 * SYNC_FILE_RANGE_WAIT_AFTER: wait upon writeout of all pages in the range
144 * after performing the write.
146 * Useful combinations of the flag bits are:
148 * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE: ensures that all pages
149 * in the range which were dirty on entry to sys_sync_file_range() are placed
150 * under writeout. This is a start-write-for-data-integrity operation.
152 * SYNC_FILE_RANGE_WRITE: start writeout of all dirty pages in the range which
153 * are not presently under writeout. This is an asynchronous flush-to-disk
154 * operation. Not suitable for data integrity operations.
156 * SYNC_FILE_RANGE_WAIT_BEFORE (or SYNC_FILE_RANGE_WAIT_AFTER): wait for
157 * completion of writeout of all pages in the range. This will be used after an
158 * earlier SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE operation to wait
159 * for that operation to complete and to return the result.
161 * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE|SYNC_FILE_RANGE_WAIT_AFTER:
162 * a traditional sync() operation. This is a write-for-data-integrity operation
163 * which will ensure that all pages in the range which were dirty on entry to
164 * sys_sync_file_range() are committed to disk.
167 * SYNC_FILE_RANGE_WAIT_BEFORE and SYNC_FILE_RANGE_WAIT_AFTER will detect any
168 * I/O errors or ENOSPC conditions and will return those to the caller, after
169 * clearing the EIO and ENOSPC flags in the address_space.
171 * It should be noted that none of these operations write out the file's
172 * metadata. So unless the application is strictly performing overwrites of
173 * already-instantiated disk blocks, there are no guarantees here that the data
174 * will be available after a crash.
176 asmlinkage long sys_sync_file_range(int fd, loff_t offset, loff_t nbytes,
181 loff_t endbyte; /* inclusive */
186 if (flags & ~VALID_FLAGS)
189 endbyte = offset + nbytes;
193 if ((s64)endbyte < 0)
195 if (endbyte < offset)
198 if (sizeof(pgoff_t) == 4) {
199 if (offset >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
201 * The range starts outside a 32 bit machine's
202 * pagecache addressing capabilities. Let it "succeed"
207 if (endbyte >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
218 endbyte--; /* inclusive */
221 file = fget_light(fd, &fput_needed);
225 i_mode = file->f_dentry->d_inode->i_mode;
227 if (!S_ISREG(i_mode) && !S_ISBLK(i_mode) && !S_ISDIR(i_mode) &&
231 ret = do_sync_file_range(file, offset, endbyte, flags);
233 fput_light(file, fput_needed);
239 * `endbyte' is inclusive
241 int do_sync_file_range(struct file *file, loff_t offset, loff_t endbyte,
245 struct address_space *mapping;
247 mapping = file->f_mapping;
254 if (flags & SYNC_FILE_RANGE_WAIT_BEFORE) {
255 ret = wait_on_page_writeback_range(mapping,
256 offset >> PAGE_CACHE_SHIFT,
257 endbyte >> PAGE_CACHE_SHIFT);
262 if (flags & SYNC_FILE_RANGE_WRITE) {
263 ret = __filemap_fdatawrite_range(mapping, offset, endbyte,
269 if (flags & SYNC_FILE_RANGE_WAIT_AFTER) {
270 ret = wait_on_page_writeback_range(mapping,
271 offset >> PAGE_CACHE_SHIFT,
272 endbyte >> PAGE_CACHE_SHIFT);
277 EXPORT_SYMBOL_GPL(do_sync_file_range);