blob: 1de747b5ddb9dcaf12ac0dc164f9a614db7b6d13 [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>
9#include <linux/writeback.h>
10#include <linux/syscalls.h>
11#include <linux/linkage.h>
12#include <linux/pagemap.h>
David Howellscf9a2ae2006-08-29 19:05:54 +010013#include <linux/quotaops.h>
14#include <linux/buffer_head.h>
Andrew Mortonf79e2ab2006-03-31 02:30:42 -080015
16#define VALID_FLAGS (SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE| \
17 SYNC_FILE_RANGE_WAIT_AFTER)
18
19/*
David Howellscf9a2ae2006-08-29 19:05:54 +010020 * sync everything. Start out by waking pdflush, because that writes back
21 * all queues in parallel.
22 */
23static void do_sync(unsigned long wait)
24{
25 wakeup_pdflush(0);
26 sync_inodes(0); /* All mappings, inodes and their blockdevs */
27 DQUOT_SYNC(NULL);
28 sync_supers(); /* Write the superblocks */
29 sync_filesystems(0); /* Start syncing the filesystems */
30 sync_filesystems(wait); /* Waitingly sync the filesystems */
31 sync_inodes(wait); /* Mappings, inodes and blockdevs, again. */
32 if (!wait)
33 printk("Emergency Sync complete\n");
34 if (unlikely(laptop_mode))
35 laptop_sync_completion();
36}
37
38asmlinkage long sys_sync(void)
39{
40 do_sync(1);
41 return 0;
42}
43
44void emergency_sync(void)
45{
46 pdflush_operation(do_sync, 0);
47}
48
49/*
50 * Generic function to fsync a file.
51 *
52 * filp may be NULL if called via the msync of a vma.
53 */
54int file_fsync(struct file *filp, struct dentry *dentry, int datasync)
55{
56 struct inode * inode = dentry->d_inode;
57 struct super_block * sb;
58 int ret, err;
59
60 /* sync the inode to buffers */
61 ret = write_inode_now(inode, 0);
62
63 /* sync the superblock to buffers */
64 sb = inode->i_sb;
65 lock_super(sb);
66 if (sb->s_op->write_super)
67 sb->s_op->write_super(sb);
68 unlock_super(sb);
69
70 /* .. finally sync the buffers to disk */
71 err = sync_blockdev(sb->s_bdev);
72 if (!ret)
73 ret = err;
74 return ret;
75}
76
77long do_fsync(struct file *file, int datasync)
78{
79 int ret;
80 int err;
81 struct address_space *mapping = file->f_mapping;
82
83 if (!file->f_op || !file->f_op->fsync) {
84 /* Why? We can still call filemap_fdatawrite */
85 ret = -EINVAL;
86 goto out;
87 }
88
89 ret = filemap_fdatawrite(mapping);
90
91 /*
92 * We need to protect against concurrent writers, which could cause
93 * livelocks in fsync_buffers_list().
94 */
95 mutex_lock(&mapping->host->i_mutex);
96 err = file->f_op->fsync(file, file->f_dentry, datasync);
97 if (!ret)
98 ret = err;
99 mutex_unlock(&mapping->host->i_mutex);
100 err = filemap_fdatawait(mapping);
101 if (!ret)
102 ret = err;
103out:
104 return ret;
105}
106
107static long __do_fsync(unsigned int fd, int datasync)
108{
109 struct file *file;
110 int ret = -EBADF;
111
112 file = fget(fd);
113 if (file) {
114 ret = do_fsync(file, datasync);
115 fput(file);
116 }
117 return ret;
118}
119
120asmlinkage long sys_fsync(unsigned int fd)
121{
122 return __do_fsync(fd, 0);
123}
124
125asmlinkage long sys_fdatasync(unsigned int fd)
126{
127 return __do_fsync(fd, 1);
128}
129
130/*
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800131 * sys_sync_file_range() permits finely controlled syncing over a segment of
132 * a file in the range offset .. (offset+nbytes-1) inclusive. If nbytes is
133 * zero then sys_sync_file_range() will operate from offset out to EOF.
134 *
135 * The flag bits are:
136 *
137 * SYNC_FILE_RANGE_WAIT_BEFORE: wait upon writeout of all pages in the range
138 * before performing the write.
139 *
140 * SYNC_FILE_RANGE_WRITE: initiate writeout of all those dirty pages in the
141 * range which are not presently under writeback.
142 *
143 * SYNC_FILE_RANGE_WAIT_AFTER: wait upon writeout of all pages in the range
144 * after performing the write.
145 *
146 * Useful combinations of the flag bits are:
147 *
148 * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE: ensures that all pages
149 * in the range which were dirty on entry to sys_sync_file_range() are placed
150 * under writeout. This is a start-write-for-data-integrity operation.
151 *
152 * SYNC_FILE_RANGE_WRITE: start writeout of all dirty pages in the range which
153 * are not presently under writeout. This is an asynchronous flush-to-disk
154 * operation. Not suitable for data integrity operations.
155 *
156 * SYNC_FILE_RANGE_WAIT_BEFORE (or SYNC_FILE_RANGE_WAIT_AFTER): wait for
157 * completion of writeout of all pages in the range. This will be used after an
158 * earlier SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE operation to wait
159 * for that operation to complete and to return the result.
160 *
161 * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE|SYNC_FILE_RANGE_WAIT_AFTER:
162 * a traditional sync() operation. This is a write-for-data-integrity operation
163 * which will ensure that all pages in the range which were dirty on entry to
164 * sys_sync_file_range() are committed to disk.
165 *
166 *
167 * SYNC_FILE_RANGE_WAIT_BEFORE and SYNC_FILE_RANGE_WAIT_AFTER will detect any
168 * I/O errors or ENOSPC conditions and will return those to the caller, after
169 * clearing the EIO and ENOSPC flags in the address_space.
170 *
171 * It should be noted that none of these operations write out the file's
172 * metadata. So unless the application is strictly performing overwrites of
173 * already-instantiated disk blocks, there are no guarantees here that the data
174 * will be available after a crash.
175 */
176asmlinkage long sys_sync_file_range(int fd, loff_t offset, loff_t nbytes,
Andrew Morton5246d052006-04-10 22:53:57 -0700177 unsigned int flags)
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800178{
179 int ret;
180 struct file *file;
181 loff_t endbyte; /* inclusive */
182 int fput_needed;
183 umode_t i_mode;
184
185 ret = -EINVAL;
186 if (flags & ~VALID_FLAGS)
187 goto out;
188
189 endbyte = offset + nbytes;
190
191 if ((s64)offset < 0)
192 goto out;
193 if ((s64)endbyte < 0)
194 goto out;
195 if (endbyte < offset)
196 goto out;
197
198 if (sizeof(pgoff_t) == 4) {
199 if (offset >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
200 /*
201 * The range starts outside a 32 bit machine's
202 * pagecache addressing capabilities. Let it "succeed"
203 */
204 ret = 0;
205 goto out;
206 }
207 if (endbyte >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
208 /*
209 * Out to EOF
210 */
211 nbytes = 0;
212 }
213 }
214
215 if (nbytes == 0)
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700216 endbyte = LLONG_MAX;
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800217 else
218 endbyte--; /* inclusive */
219
220 ret = -EBADF;
221 file = fget_light(fd, &fput_needed);
222 if (!file)
223 goto out;
224
225 i_mode = file->f_dentry->d_inode->i_mode;
226 ret = -ESPIPE;
227 if (!S_ISREG(i_mode) && !S_ISBLK(i_mode) && !S_ISDIR(i_mode) &&
228 !S_ISLNK(i_mode))
229 goto out_put;
230
231 ret = do_sync_file_range(file, offset, endbyte, flags);
232out_put:
233 fput_light(file, fput_needed);
234out:
235 return ret;
236}
237
238/*
239 * `endbyte' is inclusive
240 */
241int do_sync_file_range(struct file *file, loff_t offset, loff_t endbyte,
Andrew Morton5246d052006-04-10 22:53:57 -0700242 unsigned int flags)
Andrew Mortonf79e2ab2006-03-31 02:30:42 -0800243{
244 int ret;
245 struct address_space *mapping;
246
247 mapping = file->f_mapping;
248 if (!mapping) {
249 ret = -EINVAL;
250 goto out;
251 }
252
253 ret = 0;
254 if (flags & SYNC_FILE_RANGE_WAIT_BEFORE) {
255 ret = wait_on_page_writeback_range(mapping,
256 offset >> PAGE_CACHE_SHIFT,
257 endbyte >> PAGE_CACHE_SHIFT);
258 if (ret < 0)
259 goto out;
260 }
261
262 if (flags & SYNC_FILE_RANGE_WRITE) {
263 ret = __filemap_fdatawrite_range(mapping, offset, endbyte,
264 WB_SYNC_NONE);
265 if (ret < 0)
266 goto out;
267 }
268
269 if (flags & SYNC_FILE_RANGE_WAIT_AFTER) {
270 ret = wait_on_page_writeback_range(mapping,
271 offset >> PAGE_CACHE_SHIFT,
272 endbyte >> PAGE_CACHE_SHIFT);
273 }
274out:
275 return ret;
276}
277EXPORT_SYMBOL_GPL(do_sync_file_range);