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>
17 #define VALID_FLAGS (SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE| \
18 SYNC_FILE_RANGE_WAIT_AFTER)
21 * sync everything. Start out by waking pdflush, because that writes back
22 * all queues in parallel.
24 static void do_sync(unsigned long wait
)
27 sync_inodes(0); /* All mappings, inodes and their blockdevs */
29 sync_supers(); /* Write the superblocks */
30 sync_filesystems(0); /* Start syncing the filesystems */
31 sync_filesystems(wait
); /* Waitingly sync the filesystems */
32 sync_inodes(wait
); /* Mappings, inodes and blockdevs, again. */
34 printk("Emergency Sync complete\n");
35 if (unlikely(laptop_mode
))
36 laptop_sync_completion();
39 asmlinkage
long sys_sync(void)
45 void emergency_sync(void)
47 pdflush_operation(do_sync
, 0);
51 * Generic function to fsync a file.
53 * filp may be NULL if called via the msync of a vma.
55 int file_fsync(struct file
*filp
, struct dentry
*dentry
, int datasync
)
57 struct inode
* inode
= dentry
->d_inode
;
58 struct super_block
* sb
;
61 /* sync the inode to buffers */
62 ret
= write_inode_now(inode
, 0);
64 /* sync the superblock to buffers */
67 if (sb
->s_dirt
&& sb
->s_op
->write_super
)
68 sb
->s_op
->write_super(sb
);
71 /* .. finally sync the buffers to disk */
72 err
= sync_blockdev(sb
->s_bdev
);
78 long do_fsync(struct file
*file
, int datasync
)
82 struct address_space
*mapping
= file
->f_mapping
;
84 if (!file
->f_op
|| !file
->f_op
->fsync
) {
85 /* Why? We can still call filemap_fdatawrite */
90 ret
= filemap_fdatawrite(mapping
);
93 * We need to protect against concurrent writers, which could cause
94 * livelocks in fsync_buffers_list().
96 mutex_lock(&mapping
->host
->i_mutex
);
97 err
= file
->f_op
->fsync(file
, file
->f_path
.dentry
, datasync
);
100 mutex_unlock(&mapping
->host
->i_mutex
);
101 err
= filemap_fdatawait(mapping
);
108 static long __do_fsync(unsigned int fd
, int datasync
)
115 ret
= do_fsync(file
, datasync
);
121 asmlinkage
long sys_fsync(unsigned int fd
)
123 return __do_fsync(fd
, 0);
126 asmlinkage
long sys_fdatasync(unsigned int fd
)
128 return __do_fsync(fd
, 1);
132 * sys_sync_file_range() permits finely controlled syncing over a segment of
133 * a file in the range offset .. (offset+nbytes-1) inclusive. If nbytes is
134 * zero then sys_sync_file_range() will operate from offset out to EOF.
138 * SYNC_FILE_RANGE_WAIT_BEFORE: wait upon writeout of all pages in the range
139 * before performing the write.
141 * SYNC_FILE_RANGE_WRITE: initiate writeout of all those dirty pages in the
142 * range which are not presently under writeback. Note that this may block for
143 * significant periods due to exhaustion of disk request structures.
145 * SYNC_FILE_RANGE_WAIT_AFTER: wait upon writeout of all pages in the range
146 * after performing the write.
148 * Useful combinations of the flag bits are:
150 * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE: ensures that all pages
151 * in the range which were dirty on entry to sys_sync_file_range() are placed
152 * under writeout. This is a start-write-for-data-integrity operation.
154 * SYNC_FILE_RANGE_WRITE: start writeout of all dirty pages in the range which
155 * are not presently under writeout. This is an asynchronous flush-to-disk
156 * operation. Not suitable for data integrity operations.
158 * SYNC_FILE_RANGE_WAIT_BEFORE (or SYNC_FILE_RANGE_WAIT_AFTER): wait for
159 * completion of writeout of all pages in the range. This will be used after an
160 * earlier SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE operation to wait
161 * for that operation to complete and to return the result.
163 * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE|SYNC_FILE_RANGE_WAIT_AFTER:
164 * a traditional sync() operation. This is a write-for-data-integrity operation
165 * which will ensure that all pages in the range which were dirty on entry to
166 * sys_sync_file_range() are committed to disk.
169 * SYNC_FILE_RANGE_WAIT_BEFORE and SYNC_FILE_RANGE_WAIT_AFTER will detect any
170 * I/O errors or ENOSPC conditions and will return those to the caller, after
171 * clearing the EIO and ENOSPC flags in the address_space.
173 * It should be noted that none of these operations write out the file's
174 * metadata. So unless the application is strictly performing overwrites of
175 * already-instantiated disk blocks, there are no guarantees here that the data
176 * will be available after a crash.
178 asmlinkage
long sys_sync_file_range(int fd
, loff_t offset
, loff_t nbytes
,
183 loff_t endbyte
; /* inclusive */
188 if (flags
& ~VALID_FLAGS
)
191 endbyte
= offset
+ nbytes
;
195 if ((s64
)endbyte
< 0)
197 if (endbyte
< offset
)
200 if (sizeof(pgoff_t
) == 4) {
201 if (offset
>= (0x100000000ULL
<< PAGE_CACHE_SHIFT
)) {
203 * The range starts outside a 32 bit machine's
204 * pagecache addressing capabilities. Let it "succeed"
209 if (endbyte
>= (0x100000000ULL
<< PAGE_CACHE_SHIFT
)) {
220 endbyte
--; /* inclusive */
223 file
= fget_light(fd
, &fput_needed
);
227 i_mode
= file
->f_path
.dentry
->d_inode
->i_mode
;
229 if (!S_ISREG(i_mode
) && !S_ISBLK(i_mode
) && !S_ISDIR(i_mode
) &&
233 ret
= do_sync_mapping_range(file
->f_mapping
, offset
, endbyte
, flags
);
235 fput_light(file
, fput_needed
);
240 /* It would be nice if people remember that not all the world's an i386
241 when they introduce new system calls */
242 asmlinkage
long sys_sync_file_range2(int fd
, unsigned int flags
,
243 loff_t offset
, loff_t nbytes
)
245 return sys_sync_file_range(fd
, offset
, nbytes
, flags
);
249 * `endbyte' is inclusive
251 int do_sync_mapping_range(struct address_space
*mapping
, loff_t offset
,
252 loff_t endbyte
, unsigned int flags
)
262 if (flags
& SYNC_FILE_RANGE_WAIT_BEFORE
) {
263 ret
= wait_on_page_writeback_range(mapping
,
264 offset
>> PAGE_CACHE_SHIFT
,
265 endbyte
>> PAGE_CACHE_SHIFT
);
270 if (flags
& SYNC_FILE_RANGE_WRITE
) {
271 ret
= __filemap_fdatawrite_range(mapping
, offset
, endbyte
,
277 if (flags
& SYNC_FILE_RANGE_WAIT_AFTER
) {
278 ret
= wait_on_page_writeback_range(mapping
,
279 offset
>> PAGE_CACHE_SHIFT
,
280 endbyte
>> PAGE_CACHE_SHIFT
);
285 EXPORT_SYMBOL_GPL(do_sync_mapping_range
);