blob: 631fd5aece78dc8c52f066fca01db901da653a67 [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
21/*
David Howellscf9a2ae2006-08-29 19:05:54 +010022 * sync everything. Start out by waking pdflush, because that writes back
23 * all queues in parallel.
24 */
25static void do_sync(unsigned long wait)
26{
27 wakeup_pdflush(0);
28 sync_inodes(0); /* All mappings, inodes and their blockdevs */
Jan Kara9e3509e2009-01-26 16:45:12 +010029 vfs_dq_sync(NULL);
Jan Kara5a3e5cb2009-04-27 16:43:48 +020030 sync_inodes(wait); /* Mappings, inodes and blockdevs, again. */
David Howellscf9a2ae2006-08-29 19:05:54 +010031 sync_supers(); /* Write the superblocks */
32 sync_filesystems(0); /* Start syncing the filesystems */
33 sync_filesystems(wait); /* Waitingly sync the filesystems */
Jan Kara5a3e5cb2009-04-27 16:43:48 +020034 sync_blockdevs();
David Howellscf9a2ae2006-08-29 19:05:54 +010035 if (!wait)
36 printk("Emergency Sync complete\n");
37 if (unlikely(laptop_mode))
38 laptop_sync_completion();
39}
40
Heiko Carstensa5f8fa92009-01-14 14:14:11 +010041SYSCALL_DEFINE0(sync)
David Howellscf9a2ae2006-08-29 19:05:54 +010042{
43 do_sync(1);
44 return 0;
45}
46
Jens Axboea2a95372009-03-17 09:38:40 +010047static void do_sync_work(struct work_struct *work)
48{
49 do_sync(0);
50 kfree(work);
51}
52
David Howellscf9a2ae2006-08-29 19:05:54 +010053void emergency_sync(void)
54{
Jens Axboea2a95372009-03-17 09:38:40 +010055 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 }
David Howellscf9a2ae2006-08-29 19:05:54 +010062}
63
64/*
65 * Generic function to fsync a file.
66 *
67 * filp may be NULL if called via the msync of a vma.
68 */
69int 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);
OGAWA Hirofumi762873c2008-04-29 00:59:42 -070081 if (sb->s_dirt && sb->s_op->write_super)
David Howellscf9a2ae2006-08-29 19:05:54 +010082 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
Christoph Hellwig4c728ef2008-12-22 21:11:15 +010092/**
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 */
105int vfs_fsync(struct file *file, struct dentry *dentry, int datasync)
David Howellscf9a2ae2006-08-29 19:05:54 +0100106{
Christoph Hellwig4c728ef2008-12-22 21:11:15 +0100107 const struct file_operations *fop;
108 struct address_space *mapping;
109 int err, ret;
David Howellscf9a2ae2006-08-29 19:05:54 +0100110
Christoph Hellwig4c728ef2008-12-22 21:11:15 +0100111 /*
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) {
David Howellscf9a2ae2006-08-29 19:05:54 +0100125 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);
Christoph Hellwig4c728ef2008-12-22 21:11:15 +0100136 err = fop->fsync(file, dentry, datasync);
David Howellscf9a2ae2006-08-29 19:05:54 +0100137 if (!ret)
138 ret = err;
139 mutex_unlock(&mapping->host->i_mutex);
140 err = filemap_fdatawait(mapping);
141 if (!ret)
142 ret = err;
143out:
144 return ret;
145}
Christoph Hellwig4c728ef2008-12-22 21:11:15 +0100146EXPORT_SYMBOL(vfs_fsync);
David Howellscf9a2ae2006-08-29 19:05:54 +0100147
Christoph Hellwig4c728ef2008-12-22 21:11:15 +0100148static int do_fsync(unsigned int fd, int datasync)
David Howellscf9a2ae2006-08-29 19:05:54 +0100149{
150 struct file *file;
151 int ret = -EBADF;
152
153 file = fget(fd);
154 if (file) {
Christoph Hellwig4c728ef2008-12-22 21:11:15 +0100155 ret = vfs_fsync(file, file->f_path.dentry, datasync);
David Howellscf9a2ae2006-08-29 19:05:54 +0100156 fput(file);
157 }
158 return ret;
159}
160
Heiko Carstensa5f8fa92009-01-14 14:14:11 +0100161SYSCALL_DEFINE1(fsync, unsigned int, fd)
David Howellscf9a2ae2006-08-29 19:05:54 +0100162{
Christoph Hellwig4c728ef2008-12-22 21:11:15 +0100163 return do_fsync(fd, 0);
David Howellscf9a2ae2006-08-29 19:05:54 +0100164}
165
Heiko Carstensa5f8fa92009-01-14 14:14:11 +0100166SYSCALL_DEFINE1(fdatasync, unsigned int, fd)
David Howellscf9a2ae2006-08-29 19:05:54 +0100167{
Christoph Hellwig4c728ef2008-12-22 21:11:15 +0100168 return do_fsync(fd, 1);
David Howellscf9a2ae2006-08-29 19:05:54 +0100169}
170
171/*
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800172 * 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
Pavel Machekcce77082008-07-23 21:27:36 -0700182 * range which are not presently under writeback. Note that this may block for
183 * significant periods due to exhaustion of disk request structures.
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800184 *
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 */
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100218SYSCALL_DEFINE(sync_file_range)(int fd, loff_t offset, loff_t nbytes,
219 unsigned int flags)
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800220{
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)
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700258 endbyte = LLONG_MAX;
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800259 else
260 endbyte--; /* inclusive */
261
262 ret = -EBADF;
263 file = fget_light(fd, &fput_needed);
264 if (!file)
265 goto out;
266
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800267 i_mode = file->f_path.dentry->d_inode->i_mode;
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800268 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
Mark Fashehef51c972007-05-08 00:27:10 -0700273 ret = do_sync_mapping_range(file->f_mapping, offset, endbyte, flags);
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800274out_put:
275 fput_light(file, fput_needed);
276out:
277 return ret;
278}
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100279#ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
280asmlinkage 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}
286SYSCALL_ALIAS(sys_sync_file_range, SyS_sync_file_range);
287#endif
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800288
David Woodhouseedd5cd42007-06-27 14:10:09 -0700289/* It would be nice if people remember that not all the world's an i386
290 when they introduce new system calls */
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100291SYSCALL_DEFINE(sync_file_range2)(int fd, unsigned int flags,
292 loff_t offset, loff_t nbytes)
David Woodhouseedd5cd42007-06-27 14:10:09 -0700293{
294 return sys_sync_file_range(fd, offset, nbytes, flags);
295}
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100296#ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
297asmlinkage 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}
303SYSCALL_ALIAS(sys_sync_file_range2, SyS_sync_file_range2);
304#endif
David Woodhouseedd5cd42007-06-27 14:10:09 -0700305
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800306/*
307 * `endbyte' is inclusive
308 */
Mark Fasheh5b04aa32007-03-01 11:01:55 -0800309int do_sync_mapping_range(struct address_space *mapping, loff_t offset,
310 loff_t endbyte, unsigned int flags)
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800311{
312 int ret;
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800313
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800314 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,
Nick Pigginee53a892009-01-06 14:39:12 -0800330 WB_SYNC_ALL);
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800331 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 }
340out:
341 return ret;
342}
Mark Fasheh5b04aa32007-03-01 11:01:55 -0800343EXPORT_SYMBOL_GPL(do_sync_mapping_range);