OSDN Git Service

f2fs: update multi-dev metadata in resize_fs
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / fs / sync.c
1 /*
2  * High-level sync()-related operations
3  */
4
5 #include <linux/kernel.h>
6 #include <linux/file.h>
7 #include <linux/fs.h>
8 #include <linux/slab.h>
9 #include <linux/export.h>
10 #include <linux/module.h>
11 #include <linux/namei.h>
12 #include <linux/sched.h>
13 #include <linux/writeback.h>
14 #include <linux/syscalls.h>
15 #include <linux/linkage.h>
16 #include <linux/pagemap.h>
17 #include <linux/quotaops.h>
18 #include <linux/backing-dev.h>
19 #include "internal.h"
20
21 bool fsync_enabled = true;
22 module_param(fsync_enabled, bool, 0644);
23
24 #define VALID_FLAGS (SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE| \
25                         SYNC_FILE_RANGE_WAIT_AFTER)
26
27 /*
28  * Do the filesystem syncing work. For simple filesystems
29  * writeback_inodes_sb(sb) just dirties buffers with inodes so we have to
30  * submit IO for these buffers via __sync_blockdev(). This also speeds up the
31  * wait == 1 case since in that case write_inode() functions do
32  * sync_dirty_buffer() and thus effectively write one block at a time.
33  */
34 static int __sync_filesystem(struct super_block *sb, int wait)
35 {
36         if (wait)
37                 sync_inodes_sb(sb);
38         else
39                 writeback_inodes_sb(sb, WB_REASON_SYNC);
40
41         if (sb->s_op->sync_fs)
42                 sb->s_op->sync_fs(sb, wait);
43         return __sync_blockdev(sb->s_bdev, wait);
44 }
45
46 /*
47  * Write out and wait upon all dirty data associated with this
48  * superblock.  Filesystem data as well as the underlying block
49  * device.  Takes the superblock lock.
50  */
51 int sync_filesystem(struct super_block *sb)
52 {
53         int ret;
54
55         /*
56          * We need to be protected against the filesystem going from
57          * r/o to r/w or vice versa.
58          */
59         WARN_ON(!rwsem_is_locked(&sb->s_umount));
60
61         /*
62          * No point in syncing out anything if the filesystem is read-only.
63          */
64         if (sb->s_flags & MS_RDONLY)
65                 return 0;
66
67         ret = __sync_filesystem(sb, 0);
68         if (ret < 0)
69                 return ret;
70         return __sync_filesystem(sb, 1);
71 }
72 EXPORT_SYMBOL(sync_filesystem);
73
74 static void sync_inodes_one_sb(struct super_block *sb, void *arg)
75 {
76         if (!(sb->s_flags & MS_RDONLY))
77                 sync_inodes_sb(sb);
78 }
79
80 static void sync_fs_one_sb(struct super_block *sb, void *arg)
81 {
82         if (!(sb->s_flags & MS_RDONLY) && sb->s_op->sync_fs)
83                 sb->s_op->sync_fs(sb, *(int *)arg);
84 }
85
86 static void fdatawrite_one_bdev(struct block_device *bdev, void *arg)
87 {
88         filemap_fdatawrite(bdev->bd_inode->i_mapping);
89 }
90
91 static void fdatawait_one_bdev(struct block_device *bdev, void *arg)
92 {
93         /*
94          * We keep the error status of individual mapping so that
95          * applications can catch the writeback error using fsync(2).
96          * See filemap_fdatawait_keep_errors() for details.
97          */
98         filemap_fdatawait_keep_errors(bdev->bd_inode->i_mapping);
99 }
100
101 /*
102  * Sync everything. We start by waking flusher threads so that most of
103  * writeback runs on all devices in parallel. Then we sync all inodes reliably
104  * which effectively also waits for all flusher threads to finish doing
105  * writeback. At this point all data is on disk so metadata should be stable
106  * and we tell filesystems to sync their metadata via ->sync_fs() calls.
107  * Finally, we writeout all block devices because some filesystems (e.g. ext2)
108  * just write metadata (such as inodes or bitmaps) to block device page cache
109  * and do not sync it on their own in ->sync_fs().
110  */
111 SYSCALL_DEFINE0(sync)
112 {
113         int nowait = 0, wait = 1;
114
115         wakeup_flusher_threads(0, WB_REASON_SYNC);
116         iterate_supers(sync_inodes_one_sb, NULL);
117         iterate_supers(sync_fs_one_sb, &nowait);
118         iterate_supers(sync_fs_one_sb, &wait);
119         iterate_bdevs(fdatawrite_one_bdev, NULL);
120         iterate_bdevs(fdatawait_one_bdev, NULL);
121         if (unlikely(laptop_mode))
122                 laptop_sync_completion();
123         return 0;
124 }
125
126 static void do_sync_work(struct work_struct *work)
127 {
128         int nowait = 0;
129
130         /*
131          * Sync twice to reduce the possibility we skipped some inodes / pages
132          * because they were temporarily locked
133          */
134         iterate_supers(sync_inodes_one_sb, &nowait);
135         iterate_supers(sync_fs_one_sb, &nowait);
136         iterate_bdevs(fdatawrite_one_bdev, NULL);
137         iterate_supers(sync_inodes_one_sb, &nowait);
138         iterate_supers(sync_fs_one_sb, &nowait);
139         iterate_bdevs(fdatawrite_one_bdev, NULL);
140         printk("Emergency Sync complete\n");
141         kfree(work);
142 }
143
144 void emergency_sync(void)
145 {
146         struct work_struct *work;
147
148         work = kmalloc(sizeof(*work), GFP_ATOMIC);
149         if (work) {
150                 INIT_WORK(work, do_sync_work);
151                 schedule_work(work);
152         }
153 }
154
155 /*
156  * sync a single super
157  */
158 SYSCALL_DEFINE1(syncfs, int, fd)
159 {
160         struct fd f;
161         struct super_block *sb;
162         int ret;
163
164         if (!fsync_enabled)
165                 return 0;
166
167         f = fdget(fd);
168         if (!f.file)
169                 return -EBADF;
170         sb = f.file->f_path.dentry->d_sb;
171
172         down_read(&sb->s_umount);
173         ret = sync_filesystem(sb);
174         up_read(&sb->s_umount);
175
176         fdput(f);
177         return ret;
178 }
179
180 /**
181  * vfs_fsync_range - helper to sync a range of data & metadata to disk
182  * @file:               file to sync
183  * @start:              offset in bytes of the beginning of data range to sync
184  * @end:                offset in bytes of the end of data range (inclusive)
185  * @datasync:           perform only datasync
186  *
187  * Write back data in range @start..@end and metadata for @file to disk.  If
188  * @datasync is set only metadata needed to access modified file data is
189  * written.
190  */
191 int vfs_fsync_range(struct file *file, loff_t start, loff_t end, int datasync)
192 {
193         struct inode *inode = file->f_mapping->host;
194
195         if (!fsync_enabled)
196                 return 0;
197
198         if (!file->f_op->fsync)
199                 return -EINVAL;
200         if (!datasync && (inode->i_state & I_DIRTY_TIME)) {
201                 spin_lock(&inode->i_lock);
202                 inode->i_state &= ~I_DIRTY_TIME;
203                 spin_unlock(&inode->i_lock);
204                 mark_inode_dirty_sync(inode);
205         }
206         return file->f_op->fsync(file, start, end, datasync);
207 }
208 EXPORT_SYMBOL(vfs_fsync_range);
209
210 /**
211  * vfs_fsync - perform a fsync or fdatasync on a file
212  * @file:               file to sync
213  * @datasync:           only perform a fdatasync operation
214  *
215  * Write back data and metadata for @file to disk.  If @datasync is
216  * set only metadata needed to access modified file data is written.
217  */
218 int vfs_fsync(struct file *file, int datasync)
219 {
220         return vfs_fsync_range(file, 0, LLONG_MAX, datasync);
221 }
222 EXPORT_SYMBOL(vfs_fsync);
223
224 static int do_fsync(unsigned int fd, int datasync)
225 {
226         struct fd f;
227         int ret = -EBADF;
228
229         if (!fsync_enabled)
230                 return 0;
231
232         f = fdget(fd);
233         if (f.file) {
234                 ret = vfs_fsync(f.file, datasync);
235                 fdput(f);
236                 inc_syscfs(current);
237         }
238         return ret;
239 }
240
241 SYSCALL_DEFINE1(fsync, unsigned int, fd)
242 {
243         return do_fsync(fd, 0);
244 }
245
246 SYSCALL_DEFINE1(fdatasync, unsigned int, fd)
247 {
248         return do_fsync(fd, 1);
249 }
250
251 /*
252  * sys_sync_file_range() permits finely controlled syncing over a segment of
253  * a file in the range offset .. (offset+nbytes-1) inclusive.  If nbytes is
254  * zero then sys_sync_file_range() will operate from offset out to EOF.
255  *
256  * The flag bits are:
257  *
258  * SYNC_FILE_RANGE_WAIT_BEFORE: wait upon writeout of all pages in the range
259  * before performing the write.
260  *
261  * SYNC_FILE_RANGE_WRITE: initiate writeout of all those dirty pages in the
262  * range which are not presently under writeback. Note that this may block for
263  * significant periods due to exhaustion of disk request structures.
264  *
265  * SYNC_FILE_RANGE_WAIT_AFTER: wait upon writeout of all pages in the range
266  * after performing the write.
267  *
268  * Useful combinations of the flag bits are:
269  *
270  * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE: ensures that all pages
271  * in the range which were dirty on entry to sys_sync_file_range() are placed
272  * under writeout.  This is a start-write-for-data-integrity operation.
273  *
274  * SYNC_FILE_RANGE_WRITE: start writeout of all dirty pages in the range which
275  * are not presently under writeout.  This is an asynchronous flush-to-disk
276  * operation.  Not suitable for data integrity operations.
277  *
278  * SYNC_FILE_RANGE_WAIT_BEFORE (or SYNC_FILE_RANGE_WAIT_AFTER): wait for
279  * completion of writeout of all pages in the range.  This will be used after an
280  * earlier SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE operation to wait
281  * for that operation to complete and to return the result.
282  *
283  * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE|SYNC_FILE_RANGE_WAIT_AFTER:
284  * a traditional sync() operation.  This is a write-for-data-integrity operation
285  * which will ensure that all pages in the range which were dirty on entry to
286  * sys_sync_file_range() are committed to disk.
287  *
288  *
289  * SYNC_FILE_RANGE_WAIT_BEFORE and SYNC_FILE_RANGE_WAIT_AFTER will detect any
290  * I/O errors or ENOSPC conditions and will return those to the caller, after
291  * clearing the EIO and ENOSPC flags in the address_space.
292  *
293  * It should be noted that none of these operations write out the file's
294  * metadata.  So unless the application is strictly performing overwrites of
295  * already-instantiated disk blocks, there are no guarantees here that the data
296  * will be available after a crash.
297  */
298 SYSCALL_DEFINE4(sync_file_range, int, fd, loff_t, offset, loff_t, nbytes,
299                                 unsigned int, flags)
300 {
301         int ret;
302         struct fd f;
303         struct address_space *mapping;
304         loff_t endbyte;                 /* inclusive */
305         umode_t i_mode;
306
307         if (!fsync_enabled)
308                 return 0;
309
310         ret = -EINVAL;
311         if (flags & ~VALID_FLAGS)
312                 goto out;
313
314         endbyte = offset + nbytes;
315
316         if ((s64)offset < 0)
317                 goto out;
318         if ((s64)endbyte < 0)
319                 goto out;
320         if (endbyte < offset)
321                 goto out;
322
323         if (sizeof(pgoff_t) == 4) {
324                 if (offset >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
325                         /*
326                          * The range starts outside a 32 bit machine's
327                          * pagecache addressing capabilities.  Let it "succeed"
328                          */
329                         ret = 0;
330                         goto out;
331                 }
332                 if (endbyte >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
333                         /*
334                          * Out to EOF
335                          */
336                         nbytes = 0;
337                 }
338         }
339
340         if (nbytes == 0)
341                 endbyte = LLONG_MAX;
342         else
343                 endbyte--;              /* inclusive */
344
345         ret = -EBADF;
346         f = fdget(fd);
347         if (!f.file)
348                 goto out;
349
350         i_mode = file_inode(f.file)->i_mode;
351         ret = -ESPIPE;
352         if (!S_ISREG(i_mode) && !S_ISBLK(i_mode) && !S_ISDIR(i_mode) &&
353                         !S_ISLNK(i_mode))
354                 goto out_put;
355
356         mapping = f.file->f_mapping;
357         if (!mapping) {
358                 ret = -EINVAL;
359                 goto out_put;
360         }
361
362         ret = 0;
363         if (flags & SYNC_FILE_RANGE_WAIT_BEFORE) {
364                 ret = filemap_fdatawait_range(mapping, offset, endbyte);
365                 if (ret < 0)
366                         goto out_put;
367         }
368
369         if (flags & SYNC_FILE_RANGE_WRITE) {
370                 ret = __filemap_fdatawrite_range(mapping, offset, endbyte,
371                                                  WB_SYNC_NONE);
372                 if (ret < 0)
373                         goto out_put;
374         }
375
376         if (flags & SYNC_FILE_RANGE_WAIT_AFTER)
377                 ret = filemap_fdatawait_range(mapping, offset, endbyte);
378
379 out_put:
380         fdput(f);
381 out:
382         return ret;
383 }
384
385 /* It would be nice if people remember that not all the world's an i386
386    when they introduce new system calls */
387 SYSCALL_DEFINE4(sync_file_range2, int, fd, unsigned int, flags,
388                                  loff_t, offset, loff_t, nbytes)
389 {
390         return sys_sync_file_range(fd, offset, nbytes, flags);
391 }