vfs: Fix sys_sync() and fsync_super() reliability (version 4)
[firefly-linux-kernel-4.4.55.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/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>
16 #include "internal.h"
17
18 #define VALID_FLAGS (SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE| \
19                         SYNC_FILE_RANGE_WAIT_AFTER)
20
21 /*
22  * sync everything.  Start out by waking pdflush, because that writes back
23  * all queues in parallel.
24  */
25 static void do_sync(unsigned long wait)
26 {
27         wakeup_pdflush(0);
28         sync_inodes(0);         /* All mappings, inodes and their blockdevs */
29         vfs_dq_sync(NULL);
30         sync_inodes(wait);      /* Mappings, inodes and blockdevs, again. */
31         sync_supers();          /* Write the superblocks */
32         sync_filesystems(0);    /* Start syncing the filesystems */
33         sync_filesystems(wait); /* Waitingly sync the filesystems */
34         sync_blockdevs();
35         if (!wait)
36                 printk("Emergency Sync complete\n");
37         if (unlikely(laptop_mode))
38                 laptop_sync_completion();
39 }
40
41 SYSCALL_DEFINE0(sync)
42 {
43         do_sync(1);
44         return 0;
45 }
46
47 static void do_sync_work(struct work_struct *work)
48 {
49         do_sync(0);
50         kfree(work);
51 }
52
53 void emergency_sync(void)
54 {
55         struct work_struct *work;
56
57         work = kmalloc(sizeof(*work), GFP_ATOMIC);
58         if (work) {
59                 INIT_WORK(work, do_sync_work);
60                 schedule_work(work);
61         }
62 }
63
64 /*
65  * Generic function to fsync a file.
66  *
67  * filp may be NULL if called via the msync of a vma.
68  */
69 int file_fsync(struct file *filp, struct dentry *dentry, int datasync)
70 {
71         struct inode * inode = dentry->d_inode;
72         struct super_block * sb;
73         int ret, err;
74
75         /* sync the inode to buffers */
76         ret = write_inode_now(inode, 0);
77
78         /* sync the superblock to buffers */
79         sb = inode->i_sb;
80         lock_super(sb);
81         if (sb->s_dirt && sb->s_op->write_super)
82                 sb->s_op->write_super(sb);
83         unlock_super(sb);
84
85         /* .. finally sync the buffers to disk */
86         err = sync_blockdev(sb->s_bdev);
87         if (!ret)
88                 ret = err;
89         return ret;
90 }
91
92 /**
93  * vfs_fsync - perform a fsync or fdatasync on a file
94  * @file:               file to sync
95  * @dentry:             dentry of @file
96  * @data:               only perform a fdatasync operation
97  *
98  * Write back data and metadata for @file to disk.  If @datasync is
99  * set only metadata needed to access modified file data is written.
100  *
101  * In case this function is called from nfsd @file may be %NULL and
102  * only @dentry is set.  This can only happen when the filesystem
103  * implements the export_operations API.
104  */
105 int vfs_fsync(struct file *file, struct dentry *dentry, int datasync)
106 {
107         const struct file_operations *fop;
108         struct address_space *mapping;
109         int err, ret;
110
111         /*
112          * Get mapping and operations from the file in case we have
113          * as file, or get the default values for them in case we
114          * don't have a struct file available.  Damn nfsd..
115          */
116         if (file) {
117                 mapping = file->f_mapping;
118                 fop = file->f_op;
119         } else {
120                 mapping = dentry->d_inode->i_mapping;
121                 fop = dentry->d_inode->i_fop;
122         }
123
124         if (!fop || !fop->fsync) {
125                 ret = -EINVAL;
126                 goto out;
127         }
128
129         ret = filemap_fdatawrite(mapping);
130
131         /*
132          * We need to protect against concurrent writers, which could cause
133          * livelocks in fsync_buffers_list().
134          */
135         mutex_lock(&mapping->host->i_mutex);
136         err = fop->fsync(file, dentry, datasync);
137         if (!ret)
138                 ret = err;
139         mutex_unlock(&mapping->host->i_mutex);
140         err = filemap_fdatawait(mapping);
141         if (!ret)
142                 ret = err;
143 out:
144         return ret;
145 }
146 EXPORT_SYMBOL(vfs_fsync);
147
148 static int do_fsync(unsigned int fd, int datasync)
149 {
150         struct file *file;
151         int ret = -EBADF;
152
153         file = fget(fd);
154         if (file) {
155                 ret = vfs_fsync(file, file->f_path.dentry, datasync);
156                 fput(file);
157         }
158         return ret;
159 }
160
161 SYSCALL_DEFINE1(fsync, unsigned int, fd)
162 {
163         return do_fsync(fd, 0);
164 }
165
166 SYSCALL_DEFINE1(fdatasync, unsigned int, fd)
167 {
168         return do_fsync(fd, 1);
169 }
170
171 /*
172  * sys_sync_file_range() permits finely controlled syncing over a segment of
173  * a file in the range offset .. (offset+nbytes-1) inclusive.  If nbytes is
174  * zero then sys_sync_file_range() will operate from offset out to EOF.
175  *
176  * The flag bits are:
177  *
178  * SYNC_FILE_RANGE_WAIT_BEFORE: wait upon writeout of all pages in the range
179  * before performing the write.
180  *
181  * SYNC_FILE_RANGE_WRITE: initiate writeout of all those dirty pages in the
182  * range which are not presently under writeback. Note that this may block for
183  * significant periods due to exhaustion of disk request structures.
184  *
185  * SYNC_FILE_RANGE_WAIT_AFTER: wait upon writeout of all pages in the range
186  * after performing the write.
187  *
188  * Useful combinations of the flag bits are:
189  *
190  * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE: ensures that all pages
191  * in the range which were dirty on entry to sys_sync_file_range() are placed
192  * under writeout.  This is a start-write-for-data-integrity operation.
193  *
194  * SYNC_FILE_RANGE_WRITE: start writeout of all dirty pages in the range which
195  * are not presently under writeout.  This is an asynchronous flush-to-disk
196  * operation.  Not suitable for data integrity operations.
197  *
198  * SYNC_FILE_RANGE_WAIT_BEFORE (or SYNC_FILE_RANGE_WAIT_AFTER): wait for
199  * completion of writeout of all pages in the range.  This will be used after an
200  * earlier SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE operation to wait
201  * for that operation to complete and to return the result.
202  *
203  * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE|SYNC_FILE_RANGE_WAIT_AFTER:
204  * a traditional sync() operation.  This is a write-for-data-integrity operation
205  * which will ensure that all pages in the range which were dirty on entry to
206  * sys_sync_file_range() are committed to disk.
207  *
208  *
209  * SYNC_FILE_RANGE_WAIT_BEFORE and SYNC_FILE_RANGE_WAIT_AFTER will detect any
210  * I/O errors or ENOSPC conditions and will return those to the caller, after
211  * clearing the EIO and ENOSPC flags in the address_space.
212  *
213  * It should be noted that none of these operations write out the file's
214  * metadata.  So unless the application is strictly performing overwrites of
215  * already-instantiated disk blocks, there are no guarantees here that the data
216  * will be available after a crash.
217  */
218 SYSCALL_DEFINE(sync_file_range)(int fd, loff_t offset, loff_t nbytes,
219                                 unsigned int flags)
220 {
221         int ret;
222         struct file *file;
223         loff_t endbyte;                 /* inclusive */
224         int fput_needed;
225         umode_t i_mode;
226
227         ret = -EINVAL;
228         if (flags & ~VALID_FLAGS)
229                 goto out;
230
231         endbyte = offset + nbytes;
232
233         if ((s64)offset < 0)
234                 goto out;
235         if ((s64)endbyte < 0)
236                 goto out;
237         if (endbyte < offset)
238                 goto out;
239
240         if (sizeof(pgoff_t) == 4) {
241                 if (offset >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
242                         /*
243                          * The range starts outside a 32 bit machine's
244                          * pagecache addressing capabilities.  Let it "succeed"
245                          */
246                         ret = 0;
247                         goto out;
248                 }
249                 if (endbyte >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
250                         /*
251                          * Out to EOF
252                          */
253                         nbytes = 0;
254                 }
255         }
256
257         if (nbytes == 0)
258                 endbyte = LLONG_MAX;
259         else
260                 endbyte--;              /* inclusive */
261
262         ret = -EBADF;
263         file = fget_light(fd, &fput_needed);
264         if (!file)
265                 goto out;
266
267         i_mode = file->f_path.dentry->d_inode->i_mode;
268         ret = -ESPIPE;
269         if (!S_ISREG(i_mode) && !S_ISBLK(i_mode) && !S_ISDIR(i_mode) &&
270                         !S_ISLNK(i_mode))
271                 goto out_put;
272
273         ret = do_sync_mapping_range(file->f_mapping, offset, endbyte, flags);
274 out_put:
275         fput_light(file, fput_needed);
276 out:
277         return ret;
278 }
279 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
280 asmlinkage long SyS_sync_file_range(long fd, loff_t offset, loff_t nbytes,
281                                     long flags)
282 {
283         return SYSC_sync_file_range((int) fd, offset, nbytes,
284                                     (unsigned int) flags);
285 }
286 SYSCALL_ALIAS(sys_sync_file_range, SyS_sync_file_range);
287 #endif
288
289 /* It would be nice if people remember that not all the world's an i386
290    when they introduce new system calls */
291 SYSCALL_DEFINE(sync_file_range2)(int fd, unsigned int flags,
292                                  loff_t offset, loff_t nbytes)
293 {
294         return sys_sync_file_range(fd, offset, nbytes, flags);
295 }
296 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
297 asmlinkage long SyS_sync_file_range2(long fd, long flags,
298                                      loff_t offset, loff_t nbytes)
299 {
300         return SYSC_sync_file_range2((int) fd, (unsigned int) flags,
301                                      offset, nbytes);
302 }
303 SYSCALL_ALIAS(sys_sync_file_range2, SyS_sync_file_range2);
304 #endif
305
306 /*
307  * `endbyte' is inclusive
308  */
309 int do_sync_mapping_range(struct address_space *mapping, loff_t offset,
310                           loff_t endbyte, unsigned int flags)
311 {
312         int ret;
313
314         if (!mapping) {
315                 ret = -EINVAL;
316                 goto out;
317         }
318
319         ret = 0;
320         if (flags & SYNC_FILE_RANGE_WAIT_BEFORE) {
321                 ret = wait_on_page_writeback_range(mapping,
322                                         offset >> PAGE_CACHE_SHIFT,
323                                         endbyte >> PAGE_CACHE_SHIFT);
324                 if (ret < 0)
325                         goto out;
326         }
327
328         if (flags & SYNC_FILE_RANGE_WRITE) {
329                 ret = __filemap_fdatawrite_range(mapping, offset, endbyte,
330                                                 WB_SYNC_ALL);
331                 if (ret < 0)
332                         goto out;
333         }
334
335         if (flags & SYNC_FILE_RANGE_WAIT_AFTER) {
336                 ret = wait_on_page_writeback_range(mapping,
337                                         offset >> PAGE_CACHE_SHIFT,
338                                         endbyte >> PAGE_CACHE_SHIFT);
339         }
340 out:
341         return ret;
342 }
343 EXPORT_SYMBOL_GPL(do_sync_mapping_range);