blob: be0798cc33d78608aa4c702697e383573b9e8421 [file] [log] [blame]
Andrew Mortonf79e2ab2006-03-31 02:30:42 -08001/*
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>
Al Viro914e2632006-10-18 13:55:46 -04009#include <linux/sched.h>
Andrew Mortonf79e2ab2006-03-31 02:30:42 -080010#include <linux/writeback.h>
11#include <linux/syscalls.h>
12#include <linux/linkage.h>
13#include <linux/pagemap.h>
David Howellscf9a2ae2006-08-29 19:05:54 +010014#include <linux/quotaops.h>
15#include <linux/buffer_head.h>
Jan Kara5a3e5cb2009-04-27 16:43:48 +020016#include "internal.h"
Andrew Mortonf79e2ab2006-03-31 02:30:42 -080017
18#define VALID_FLAGS (SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE| \
19 SYNC_FILE_RANGE_WAIT_AFTER)
20
Heiko Carstensa5f8fa92009-01-14 14:14:11 +010021SYSCALL_DEFINE0(sync)
David Howellscf9a2ae2006-08-29 19:05:54 +010022{
Jan Kara5cee5812009-04-27 16:43:51 +020023 sync_filesystems(0);
24 sync_filesystems(1);
25 if (unlikely(laptop_mode))
26 laptop_sync_completion();
David Howellscf9a2ae2006-08-29 19:05:54 +010027 return 0;
28}
29
Jens Axboea2a95372009-03-17 09:38:40 +010030static void do_sync_work(struct work_struct *work)
31{
Jan Kara5cee5812009-04-27 16:43:51 +020032 /*
33 * Sync twice to reduce the possibility we skipped some inodes / pages
34 * because they were temporarily locked
35 */
36 sync_filesystems(0);
37 sync_filesystems(0);
38 printk("Emergency Sync complete\n");
Jens Axboea2a95372009-03-17 09:38:40 +010039 kfree(work);
40}
41
David Howellscf9a2ae2006-08-29 19:05:54 +010042void emergency_sync(void)
43{
Jens Axboea2a95372009-03-17 09:38:40 +010044 struct work_struct *work;
45
46 work = kmalloc(sizeof(*work), GFP_ATOMIC);
47 if (work) {
48 INIT_WORK(work, do_sync_work);
49 schedule_work(work);
50 }
David Howellscf9a2ae2006-08-29 19:05:54 +010051}
52
53/*
54 * Generic function to fsync a file.
55 *
56 * filp may be NULL if called via the msync of a vma.
57 */
58int file_fsync(struct file *filp, struct dentry *dentry, int datasync)
59{
60 struct inode * inode = dentry->d_inode;
61 struct super_block * sb;
62 int ret, err;
63
64 /* sync the inode to buffers */
65 ret = write_inode_now(inode, 0);
66
67 /* sync the superblock to buffers */
68 sb = inode->i_sb;
69 lock_super(sb);
OGAWA Hirofumi762873c2008-04-29 00:59:42 -070070 if (sb->s_dirt && sb->s_op->write_super)
David Howellscf9a2ae2006-08-29 19:05:54 +010071 sb->s_op->write_super(sb);
72 unlock_super(sb);
73
74 /* .. finally sync the buffers to disk */
75 err = sync_blockdev(sb->s_bdev);
76 if (!ret)
77 ret = err;
78 return ret;
79}
80
Christoph Hellwig4c728ef2008-12-22 21:11:15 +010081/**
82 * vfs_fsync - perform a fsync or fdatasync on a file
83 * @file: file to sync
84 * @dentry: dentry of @file
85 * @data: only perform a fdatasync operation
86 *
87 * Write back data and metadata for @file to disk. If @datasync is
88 * set only metadata needed to access modified file data is written.
89 *
90 * In case this function is called from nfsd @file may be %NULL and
91 * only @dentry is set. This can only happen when the filesystem
92 * implements the export_operations API.
93 */
94int vfs_fsync(struct file *file, struct dentry *dentry, int datasync)
David Howellscf9a2ae2006-08-29 19:05:54 +010095{
Christoph Hellwig4c728ef2008-12-22 21:11:15 +010096 const struct file_operations *fop;
97 struct address_space *mapping;
98 int err, ret;
David Howellscf9a2ae2006-08-29 19:05:54 +010099
Christoph Hellwig4c728ef2008-12-22 21:11:15 +0100100 /*
101 * Get mapping and operations from the file in case we have
102 * as file, or get the default values for them in case we
103 * don't have a struct file available. Damn nfsd..
104 */
105 if (file) {
106 mapping = file->f_mapping;
107 fop = file->f_op;
108 } else {
109 mapping = dentry->d_inode->i_mapping;
110 fop = dentry->d_inode->i_fop;
111 }
112
113 if (!fop || !fop->fsync) {
David Howellscf9a2ae2006-08-29 19:05:54 +0100114 ret = -EINVAL;
115 goto out;
116 }
117
118 ret = filemap_fdatawrite(mapping);
119
120 /*
121 * We need to protect against concurrent writers, which could cause
122 * livelocks in fsync_buffers_list().
123 */
124 mutex_lock(&mapping->host->i_mutex);
Christoph Hellwig4c728ef2008-12-22 21:11:15 +0100125 err = fop->fsync(file, dentry, datasync);
David Howellscf9a2ae2006-08-29 19:05:54 +0100126 if (!ret)
127 ret = err;
128 mutex_unlock(&mapping->host->i_mutex);
129 err = filemap_fdatawait(mapping);
130 if (!ret)
131 ret = err;
132out:
133 return ret;
134}
Christoph Hellwig4c728ef2008-12-22 21:11:15 +0100135EXPORT_SYMBOL(vfs_fsync);
David Howellscf9a2ae2006-08-29 19:05:54 +0100136
Christoph Hellwig4c728ef2008-12-22 21:11:15 +0100137static int do_fsync(unsigned int fd, int datasync)
David Howellscf9a2ae2006-08-29 19:05:54 +0100138{
139 struct file *file;
140 int ret = -EBADF;
141
142 file = fget(fd);
143 if (file) {
Christoph Hellwig4c728ef2008-12-22 21:11:15 +0100144 ret = vfs_fsync(file, file->f_path.dentry, datasync);
David Howellscf9a2ae2006-08-29 19:05:54 +0100145 fput(file);
146 }
147 return ret;
148}
149
Heiko Carstensa5f8fa92009-01-14 14:14:11 +0100150SYSCALL_DEFINE1(fsync, unsigned int, fd)
David Howellscf9a2ae2006-08-29 19:05:54 +0100151{
Christoph Hellwig4c728ef2008-12-22 21:11:15 +0100152 return do_fsync(fd, 0);
David Howellscf9a2ae2006-08-29 19:05:54 +0100153}
154
Heiko Carstensa5f8fa92009-01-14 14:14:11 +0100155SYSCALL_DEFINE1(fdatasync, unsigned int, fd)
David Howellscf9a2ae2006-08-29 19:05:54 +0100156{
Christoph Hellwig4c728ef2008-12-22 21:11:15 +0100157 return do_fsync(fd, 1);
David Howellscf9a2ae2006-08-29 19:05:54 +0100158}
159
160/*
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800161 * sys_sync_file_range() permits finely controlled syncing over a segment of
162 * a file in the range offset .. (offset+nbytes-1) inclusive. If nbytes is
163 * zero then sys_sync_file_range() will operate from offset out to EOF.
164 *
165 * The flag bits are:
166 *
167 * SYNC_FILE_RANGE_WAIT_BEFORE: wait upon writeout of all pages in the range
168 * before performing the write.
169 *
170 * SYNC_FILE_RANGE_WRITE: initiate writeout of all those dirty pages in the
Pavel Machekcce77082008-07-23 21:27:36 -0700171 * range which are not presently under writeback. Note that this may block for
172 * significant periods due to exhaustion of disk request structures.
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800173 *
174 * SYNC_FILE_RANGE_WAIT_AFTER: wait upon writeout of all pages in the range
175 * after performing the write.
176 *
177 * Useful combinations of the flag bits are:
178 *
179 * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE: ensures that all pages
180 * in the range which were dirty on entry to sys_sync_file_range() are placed
181 * under writeout. This is a start-write-for-data-integrity operation.
182 *
183 * SYNC_FILE_RANGE_WRITE: start writeout of all dirty pages in the range which
184 * are not presently under writeout. This is an asynchronous flush-to-disk
185 * operation. Not suitable for data integrity operations.
186 *
187 * SYNC_FILE_RANGE_WAIT_BEFORE (or SYNC_FILE_RANGE_WAIT_AFTER): wait for
188 * completion of writeout of all pages in the range. This will be used after an
189 * earlier SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE operation to wait
190 * for that operation to complete and to return the result.
191 *
192 * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE|SYNC_FILE_RANGE_WAIT_AFTER:
193 * a traditional sync() operation. This is a write-for-data-integrity operation
194 * which will ensure that all pages in the range which were dirty on entry to
195 * sys_sync_file_range() are committed to disk.
196 *
197 *
198 * SYNC_FILE_RANGE_WAIT_BEFORE and SYNC_FILE_RANGE_WAIT_AFTER will detect any
199 * I/O errors or ENOSPC conditions and will return those to the caller, after
200 * clearing the EIO and ENOSPC flags in the address_space.
201 *
202 * It should be noted that none of these operations write out the file's
203 * metadata. So unless the application is strictly performing overwrites of
204 * already-instantiated disk blocks, there are no guarantees here that the data
205 * will be available after a crash.
206 */
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100207SYSCALL_DEFINE(sync_file_range)(int fd, loff_t offset, loff_t nbytes,
208 unsigned int flags)
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800209{
210 int ret;
211 struct file *file;
212 loff_t endbyte; /* inclusive */
213 int fput_needed;
214 umode_t i_mode;
215
216 ret = -EINVAL;
217 if (flags & ~VALID_FLAGS)
218 goto out;
219
220 endbyte = offset + nbytes;
221
222 if ((s64)offset < 0)
223 goto out;
224 if ((s64)endbyte < 0)
225 goto out;
226 if (endbyte < offset)
227 goto out;
228
229 if (sizeof(pgoff_t) == 4) {
230 if (offset >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
231 /*
232 * The range starts outside a 32 bit machine's
233 * pagecache addressing capabilities. Let it "succeed"
234 */
235 ret = 0;
236 goto out;
237 }
238 if (endbyte >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
239 /*
240 * Out to EOF
241 */
242 nbytes = 0;
243 }
244 }
245
246 if (nbytes == 0)
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700247 endbyte = LLONG_MAX;
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800248 else
249 endbyte--; /* inclusive */
250
251 ret = -EBADF;
252 file = fget_light(fd, &fput_needed);
253 if (!file)
254 goto out;
255
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800256 i_mode = file->f_path.dentry->d_inode->i_mode;
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800257 ret = -ESPIPE;
258 if (!S_ISREG(i_mode) && !S_ISBLK(i_mode) && !S_ISDIR(i_mode) &&
259 !S_ISLNK(i_mode))
260 goto out_put;
261
Mark Fashehef51c972007-05-08 00:27:10 -0700262 ret = do_sync_mapping_range(file->f_mapping, offset, endbyte, flags);
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800263out_put:
264 fput_light(file, fput_needed);
265out:
266 return ret;
267}
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100268#ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
269asmlinkage long SyS_sync_file_range(long fd, loff_t offset, loff_t nbytes,
270 long flags)
271{
272 return SYSC_sync_file_range((int) fd, offset, nbytes,
273 (unsigned int) flags);
274}
275SYSCALL_ALIAS(sys_sync_file_range, SyS_sync_file_range);
276#endif
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800277
David Woodhouseedd5cd42007-06-27 14:10:09 -0700278/* It would be nice if people remember that not all the world's an i386
279 when they introduce new system calls */
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100280SYSCALL_DEFINE(sync_file_range2)(int fd, unsigned int flags,
281 loff_t offset, loff_t nbytes)
David Woodhouseedd5cd42007-06-27 14:10:09 -0700282{
283 return sys_sync_file_range(fd, offset, nbytes, flags);
284}
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100285#ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
286asmlinkage long SyS_sync_file_range2(long fd, long flags,
287 loff_t offset, loff_t nbytes)
288{
289 return SYSC_sync_file_range2((int) fd, (unsigned int) flags,
290 offset, nbytes);
291}
292SYSCALL_ALIAS(sys_sync_file_range2, SyS_sync_file_range2);
293#endif
David Woodhouseedd5cd42007-06-27 14:10:09 -0700294
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800295/*
296 * `endbyte' is inclusive
297 */
Mark Fasheh5b04aa32007-03-01 11:01:55 -0800298int do_sync_mapping_range(struct address_space *mapping, loff_t offset,
299 loff_t endbyte, unsigned int flags)
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800300{
301 int ret;
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800302
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800303 if (!mapping) {
304 ret = -EINVAL;
305 goto out;
306 }
307
308 ret = 0;
309 if (flags & SYNC_FILE_RANGE_WAIT_BEFORE) {
310 ret = wait_on_page_writeback_range(mapping,
311 offset >> PAGE_CACHE_SHIFT,
312 endbyte >> PAGE_CACHE_SHIFT);
313 if (ret < 0)
314 goto out;
315 }
316
317 if (flags & SYNC_FILE_RANGE_WRITE) {
318 ret = __filemap_fdatawrite_range(mapping, offset, endbyte,
Nick Pigginee53a892009-01-06 14:39:12 -0800319 WB_SYNC_ALL);
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800320 if (ret < 0)
321 goto out;
322 }
323
324 if (flags & SYNC_FILE_RANGE_WAIT_AFTER) {
325 ret = wait_on_page_writeback_range(mapping,
326 offset >> PAGE_CACHE_SHIFT,
327 endbyte >> PAGE_CACHE_SHIFT);
328 }
329out:
330 return ret;
331}
Mark Fasheh5b04aa32007-03-01 11:01:55 -0800332EXPORT_SYMBOL_GPL(do_sync_mapping_range);