Commit | Line | Data |
---|---|---|
f79e2abb AM |
1 | /* |
2 | * High-level sync()-related operations | |
3 | */ | |
4 | ||
5 | #include <linux/kernel.h> | |
6 | #include <linux/file.h> | |
7 | #include <linux/fs.h> | |
5a0e3ad6 | 8 | #include <linux/slab.h> |
f79e2abb | 9 | #include <linux/module.h> |
914e2637 | 10 | #include <linux/sched.h> |
f79e2abb AM |
11 | #include <linux/writeback.h> |
12 | #include <linux/syscalls.h> | |
13 | #include <linux/linkage.h> | |
14 | #include <linux/pagemap.h> | |
cf9a2ae8 DH |
15 | #include <linux/quotaops.h> |
16 | #include <linux/buffer_head.h> | |
5a3e5cb8 | 17 | #include "internal.h" |
f79e2abb AM |
18 | |
19 | #define VALID_FLAGS (SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE| \ | |
20 | SYNC_FILE_RANGE_WAIT_AFTER) | |
21 | ||
c15c54f5 | 22 | /* |
d8a8559c JA |
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. | |
c15c54f5 | 28 | */ |
60b0680f | 29 | static int __sync_filesystem(struct super_block *sb, int wait) |
c15c54f5 | 30 | { |
32a88aa1 JA |
31 | /* |
32 | * This should be safe, as we require bdi backing to actually | |
33 | * write out data in the first place | |
34 | */ | |
35 | if (!sb->s_bdi) | |
36 | return 0; | |
37 | ||
5fb324ad CH |
38 | if (sb->s_qcop && sb->s_qcop->quota_sync) |
39 | sb->s_qcop->quota_sync(sb, -1, wait); | |
40 | ||
41 | if (wait) | |
d8a8559c | 42 | sync_inodes_sb(sb); |
5fb324ad CH |
43 | else |
44 | writeback_inodes_sb(sb); | |
45 | ||
c15c54f5 JK |
46 | if (sb->s_op->sync_fs) |
47 | sb->s_op->sync_fs(sb, wait); | |
48 | return __sync_blockdev(sb->s_bdev, wait); | |
49 | } | |
50 | ||
51 | /* | |
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. | |
55 | */ | |
60b0680f | 56 | int sync_filesystem(struct super_block *sb) |
c15c54f5 JK |
57 | { |
58 | int ret; | |
59 | ||
5af7926f CH |
60 | /* |
61 | * We need to be protected against the filesystem going from | |
62 | * r/o to r/w or vice versa. | |
63 | */ | |
64 | WARN_ON(!rwsem_is_locked(&sb->s_umount)); | |
65 | ||
66 | /* | |
67 | * No point in syncing out anything if the filesystem is read-only. | |
68 | */ | |
69 | if (sb->s_flags & MS_RDONLY) | |
70 | return 0; | |
71 | ||
60b0680f | 72 | ret = __sync_filesystem(sb, 0); |
c15c54f5 JK |
73 | if (ret < 0) |
74 | return ret; | |
60b0680f | 75 | return __sync_filesystem(sb, 1); |
c15c54f5 | 76 | } |
60b0680f | 77 | EXPORT_SYMBOL_GPL(sync_filesystem); |
c15c54f5 JK |
78 | |
79 | /* | |
80 | * Sync all the data for all the filesystems (called by sys_sync() and | |
81 | * emergency sync) | |
82 | * | |
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. | |
87 | * | |
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. | |
92 | */ | |
93 | static void sync_filesystems(int wait) | |
94 | { | |
95 | struct super_block *sb; | |
96 | static DEFINE_MUTEX(mutex); | |
97 | ||
98 | mutex_lock(&mutex); /* Could be down_interruptible */ | |
99 | spin_lock(&sb_lock); | |
5af7926f | 100 | list_for_each_entry(sb, &super_blocks, s_list) |
c15c54f5 | 101 | sb->s_need_sync = 1; |
c15c54f5 JK |
102 | |
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; | |
c15c54f5 JK |
108 | sb->s_count++; |
109 | spin_unlock(&sb_lock); | |
5af7926f | 110 | |
c15c54f5 | 111 | down_read(&sb->s_umount); |
32a88aa1 | 112 | if (!(sb->s_flags & MS_RDONLY) && sb->s_root && sb->s_bdi) |
60b0680f | 113 | __sync_filesystem(sb, wait); |
c15c54f5 | 114 | up_read(&sb->s_umount); |
5af7926f | 115 | |
c15c54f5 JK |
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; | |
120 | } | |
121 | spin_unlock(&sb_lock); | |
122 | mutex_unlock(&mutex); | |
123 | } | |
124 | ||
3beab0b4 ZY |
125 | /* |
126 | * sync everything. Start out by waking pdflush, because that writes back | |
127 | * all queues in parallel. | |
128 | */ | |
5cee5815 | 129 | SYSCALL_DEFINE0(sync) |
cf9a2ae8 | 130 | { |
03ba3782 | 131 | wakeup_flusher_threads(0); |
5cee5815 JK |
132 | sync_filesystems(0); |
133 | sync_filesystems(1); | |
cf9a2ae8 DH |
134 | if (unlikely(laptop_mode)) |
135 | laptop_sync_completion(); | |
cf9a2ae8 DH |
136 | return 0; |
137 | } | |
138 | ||
a2a9537a JA |
139 | static void do_sync_work(struct work_struct *work) |
140 | { | |
5cee5815 JK |
141 | /* |
142 | * Sync twice to reduce the possibility we skipped some inodes / pages | |
143 | * because they were temporarily locked | |
144 | */ | |
145 | sync_filesystems(0); | |
146 | sync_filesystems(0); | |
147 | printk("Emergency Sync complete\n"); | |
a2a9537a JA |
148 | kfree(work); |
149 | } | |
150 | ||
cf9a2ae8 DH |
151 | void emergency_sync(void) |
152 | { | |
a2a9537a JA |
153 | struct work_struct *work; |
154 | ||
155 | work = kmalloc(sizeof(*work), GFP_ATOMIC); | |
156 | if (work) { | |
157 | INIT_WORK(work, do_sync_work); | |
158 | schedule_work(work); | |
159 | } | |
cf9a2ae8 DH |
160 | } |
161 | ||
162 | /* | |
163 | * Generic function to fsync a file. | |
164 | * | |
165 | * filp may be NULL if called via the msync of a vma. | |
166 | */ | |
167 | int file_fsync(struct file *filp, struct dentry *dentry, int datasync) | |
168 | { | |
169 | struct inode * inode = dentry->d_inode; | |
170 | struct super_block * sb; | |
171 | int ret, err; | |
172 | ||
173 | /* sync the inode to buffers */ | |
174 | ret = write_inode_now(inode, 0); | |
175 | ||
176 | /* sync the superblock to buffers */ | |
177 | sb = inode->i_sb; | |
762873c2 | 178 | if (sb->s_dirt && sb->s_op->write_super) |
cf9a2ae8 | 179 | sb->s_op->write_super(sb); |
cf9a2ae8 DH |
180 | |
181 | /* .. finally sync the buffers to disk */ | |
182 | err = sync_blockdev(sb->s_bdev); | |
183 | if (!ret) | |
184 | ret = err; | |
185 | return ret; | |
186 | } | |
1fe72eaa | 187 | EXPORT_SYMBOL(file_fsync); |
cf9a2ae8 | 188 | |
4c728ef5 | 189 | /** |
148f948b | 190 | * vfs_fsync_range - helper to sync a range of data & metadata to disk |
4c728ef5 CH |
191 | * @file: file to sync |
192 | * @dentry: dentry of @file | |
148f948b JK |
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 | |
4c728ef5 | 196 | * |
148f948b JK |
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. | |
4c728ef5 CH |
200 | * |
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. | |
204 | */ | |
148f948b JK |
205 | int vfs_fsync_range(struct file *file, struct dentry *dentry, loff_t start, |
206 | loff_t end, int datasync) | |
cf9a2ae8 | 207 | { |
4c728ef5 CH |
208 | const struct file_operations *fop; |
209 | struct address_space *mapping; | |
210 | int err, ret; | |
211 | ||
212 | /* | |
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.. | |
216 | */ | |
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; | |
223 | } | |
cf9a2ae8 | 224 | |
4c728ef5 | 225 | if (!fop || !fop->fsync) { |
cf9a2ae8 DH |
226 | ret = -EINVAL; |
227 | goto out; | |
228 | } | |
229 | ||
2daea67e | 230 | ret = filemap_write_and_wait_range(mapping, start, end); |
cf9a2ae8 DH |
231 | |
232 | /* | |
233 | * We need to protect against concurrent writers, which could cause | |
234 | * livelocks in fsync_buffers_list(). | |
235 | */ | |
236 | mutex_lock(&mapping->host->i_mutex); | |
4c728ef5 | 237 | err = fop->fsync(file, dentry, datasync); |
cf9a2ae8 DH |
238 | if (!ret) |
239 | ret = err; | |
240 | mutex_unlock(&mapping->host->i_mutex); | |
148f948b | 241 | |
cf9a2ae8 DH |
242 | out: |
243 | return ret; | |
244 | } | |
148f948b JK |
245 | EXPORT_SYMBOL(vfs_fsync_range); |
246 | ||
247 | /** | |
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 | |
252 | * | |
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. | |
255 | * | |
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. | |
259 | */ | |
260 | int vfs_fsync(struct file *file, struct dentry *dentry, int datasync) | |
261 | { | |
262 | return vfs_fsync_range(file, dentry, 0, LLONG_MAX, datasync); | |
263 | } | |
4c728ef5 | 264 | EXPORT_SYMBOL(vfs_fsync); |
cf9a2ae8 | 265 | |
4c728ef5 | 266 | static int do_fsync(unsigned int fd, int datasync) |
cf9a2ae8 DH |
267 | { |
268 | struct file *file; | |
269 | int ret = -EBADF; | |
270 | ||
271 | file = fget(fd); | |
272 | if (file) { | |
4c728ef5 | 273 | ret = vfs_fsync(file, file->f_path.dentry, datasync); |
cf9a2ae8 DH |
274 | fput(file); |
275 | } | |
276 | return ret; | |
277 | } | |
278 | ||
a5f8fa9e | 279 | SYSCALL_DEFINE1(fsync, unsigned int, fd) |
cf9a2ae8 | 280 | { |
4c728ef5 | 281 | return do_fsync(fd, 0); |
cf9a2ae8 DH |
282 | } |
283 | ||
a5f8fa9e | 284 | SYSCALL_DEFINE1(fdatasync, unsigned int, fd) |
cf9a2ae8 | 285 | { |
4c728ef5 | 286 | return do_fsync(fd, 1); |
cf9a2ae8 DH |
287 | } |
288 | ||
148f948b JK |
289 | /** |
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 | |
294 | * | |
295 | * This is just a simple wrapper about our general syncing function. | |
296 | */ | |
297 | int generic_write_sync(struct file *file, loff_t pos, loff_t count) | |
298 | { | |
6b2f3d1f | 299 | if (!(file->f_flags & O_DSYNC) && !IS_SYNC(file->f_mapping->host)) |
148f948b JK |
300 | return 0; |
301 | return vfs_fsync_range(file, file->f_path.dentry, pos, | |
6b2f3d1f CH |
302 | pos + count - 1, |
303 | (file->f_flags & __O_SYNC) ? 0 : 1); | |
148f948b JK |
304 | } |
305 | EXPORT_SYMBOL(generic_write_sync); | |
306 | ||
f79e2abb AM |
307 | /* |
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. | |
311 | * | |
312 | * The flag bits are: | |
313 | * | |
314 | * SYNC_FILE_RANGE_WAIT_BEFORE: wait upon writeout of all pages in the range | |
315 | * before performing the write. | |
316 | * | |
317 | * SYNC_FILE_RANGE_WRITE: initiate writeout of all those dirty pages in the | |
cce77081 PM |
318 | * range which are not presently under writeback. Note that this may block for |
319 | * significant periods due to exhaustion of disk request structures. | |
f79e2abb AM |
320 | * |
321 | * SYNC_FILE_RANGE_WAIT_AFTER: wait upon writeout of all pages in the range | |
322 | * after performing the write. | |
323 | * | |
324 | * Useful combinations of the flag bits are: | |
325 | * | |
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. | |
329 | * | |
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. | |
333 | * | |
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. | |
338 | * | |
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. | |
343 | * | |
344 | * | |
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. | |
348 | * | |
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. | |
353 | */ | |
6673e0c3 HC |
354 | SYSCALL_DEFINE(sync_file_range)(int fd, loff_t offset, loff_t nbytes, |
355 | unsigned int flags) | |
f79e2abb AM |
356 | { |
357 | int ret; | |
358 | struct file *file; | |
7a0ad10c | 359 | struct address_space *mapping; |
f79e2abb AM |
360 | loff_t endbyte; /* inclusive */ |
361 | int fput_needed; | |
362 | umode_t i_mode; | |
363 | ||
364 | ret = -EINVAL; | |
365 | if (flags & ~VALID_FLAGS) | |
366 | goto out; | |
367 | ||
368 | endbyte = offset + nbytes; | |
369 | ||
370 | if ((s64)offset < 0) | |
371 | goto out; | |
372 | if ((s64)endbyte < 0) | |
373 | goto out; | |
374 | if (endbyte < offset) | |
375 | goto out; | |
376 | ||
377 | if (sizeof(pgoff_t) == 4) { | |
378 | if (offset >= (0x100000000ULL << PAGE_CACHE_SHIFT)) { | |
379 | /* | |
380 | * The range starts outside a 32 bit machine's | |
381 | * pagecache addressing capabilities. Let it "succeed" | |
382 | */ | |
383 | ret = 0; | |
384 | goto out; | |
385 | } | |
386 | if (endbyte >= (0x100000000ULL << PAGE_CACHE_SHIFT)) { | |
387 | /* | |
388 | * Out to EOF | |
389 | */ | |
390 | nbytes = 0; | |
391 | } | |
392 | } | |
393 | ||
394 | if (nbytes == 0) | |
111ebb6e | 395 | endbyte = LLONG_MAX; |
f79e2abb AM |
396 | else |
397 | endbyte--; /* inclusive */ | |
398 | ||
399 | ret = -EBADF; | |
400 | file = fget_light(fd, &fput_needed); | |
401 | if (!file) | |
402 | goto out; | |
403 | ||
0f7fc9e4 | 404 | i_mode = file->f_path.dentry->d_inode->i_mode; |
f79e2abb AM |
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; | |
409 | ||
7a0ad10c CH |
410 | mapping = file->f_mapping; |
411 | if (!mapping) { | |
412 | ret = -EINVAL; | |
413 | goto out_put; | |
414 | } | |
415 | ||
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; | |
421 | } | |
422 | ||
423 | if (flags & SYNC_FILE_RANGE_WRITE) { | |
424 | ret = filemap_fdatawrite_range(mapping, offset, endbyte); | |
425 | if (ret < 0) | |
426 | goto out_put; | |
427 | } | |
428 | ||
429 | if (flags & SYNC_FILE_RANGE_WAIT_AFTER) | |
430 | ret = filemap_fdatawait_range(mapping, offset, endbyte); | |
431 | ||
f79e2abb AM |
432 | out_put: |
433 | fput_light(file, fput_needed); | |
434 | out: | |
435 | return ret; | |
436 | } | |
6673e0c3 HC |
437 | #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS |
438 | asmlinkage long SyS_sync_file_range(long fd, loff_t offset, loff_t nbytes, | |
439 | long flags) | |
440 | { | |
441 | return SYSC_sync_file_range((int) fd, offset, nbytes, | |
442 | (unsigned int) flags); | |
443 | } | |
444 | SYSCALL_ALIAS(sys_sync_file_range, SyS_sync_file_range); | |
445 | #endif | |
f79e2abb | 446 | |
edd5cd4a DW |
447 | /* It would be nice if people remember that not all the world's an i386 |
448 | when they introduce new system calls */ | |
6673e0c3 HC |
449 | SYSCALL_DEFINE(sync_file_range2)(int fd, unsigned int flags, |
450 | loff_t offset, loff_t nbytes) | |
edd5cd4a DW |
451 | { |
452 | return sys_sync_file_range(fd, offset, nbytes, flags); | |
453 | } | |
6673e0c3 HC |
454 | #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS |
455 | asmlinkage long SyS_sync_file_range2(long fd, long flags, | |
456 | loff_t offset, loff_t nbytes) | |
457 | { | |
458 | return SYSC_sync_file_range2((int) fd, (unsigned int) flags, | |
459 | offset, nbytes); | |
460 | } | |
461 | SYSCALL_ALIAS(sys_sync_file_range2, SyS_sync_file_range2); | |
462 | #endif |