]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/sync.c
vfs: Make __fsync_super() a static function (version 4)
[net-next-2.6.git] / fs / sync.c
CommitLineData
f79e2abb
AM
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>
914e2637 9#include <linux/sched.h>
f79e2abb
AM
10#include <linux/writeback.h>
11#include <linux/syscalls.h>
12#include <linux/linkage.h>
13#include <linux/pagemap.h>
cf9a2ae8
DH
14#include <linux/quotaops.h>
15#include <linux/buffer_head.h>
5a3e5cb8 16#include "internal.h"
f79e2abb
AM
17
18#define VALID_FLAGS (SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE| \
19 SYNC_FILE_RANGE_WAIT_AFTER)
20
cf9a2ae8
DH
21/*
22 * 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 */
9e3509e2 29 vfs_dq_sync(NULL);
5a3e5cb8 30 sync_inodes(wait); /* Mappings, inodes and blockdevs, again. */
cf9a2ae8
DH
31 sync_supers(); /* Write the superblocks */
32 sync_filesystems(0); /* Start syncing the filesystems */
33 sync_filesystems(wait); /* Waitingly sync the filesystems */
5a3e5cb8 34 sync_blockdevs();
cf9a2ae8
DH
35 if (!wait)
36 printk("Emergency Sync complete\n");
37 if (unlikely(laptop_mode))
38 laptop_sync_completion();
39}
40
a5f8fa9e 41SYSCALL_DEFINE0(sync)
cf9a2ae8
DH
42{
43 do_sync(1);
44 return 0;
45}
46
a2a9537a
JA
47static void do_sync_work(struct work_struct *work)
48{
49 do_sync(0);
50 kfree(work);
51}
52
cf9a2ae8
DH
53void emergency_sync(void)
54{
a2a9537a
JA
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 }
cf9a2ae8
DH
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 */
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);
762873c2 81 if (sb->s_dirt && sb->s_op->write_super)
cf9a2ae8
DH
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
4c728ef5
CH
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 */
105int vfs_fsync(struct file *file, struct dentry *dentry, int datasync)
cf9a2ae8 106{
4c728ef5
CH
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 }
cf9a2ae8 123
4c728ef5 124 if (!fop || !fop->fsync) {
cf9a2ae8
DH
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);
4c728ef5 136 err = fop->fsync(file, dentry, datasync);
cf9a2ae8
DH
137 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}
4c728ef5 146EXPORT_SYMBOL(vfs_fsync);
cf9a2ae8 147
4c728ef5 148static int do_fsync(unsigned int fd, int datasync)
cf9a2ae8
DH
149{
150 struct file *file;
151 int ret = -EBADF;
152
153 file = fget(fd);
154 if (file) {
4c728ef5 155 ret = vfs_fsync(file, file->f_path.dentry, datasync);
cf9a2ae8
DH
156 fput(file);
157 }
158 return ret;
159}
160
a5f8fa9e 161SYSCALL_DEFINE1(fsync, unsigned int, fd)
cf9a2ae8 162{
4c728ef5 163 return do_fsync(fd, 0);
cf9a2ae8
DH
164}
165
a5f8fa9e 166SYSCALL_DEFINE1(fdatasync, unsigned int, fd)
cf9a2ae8 167{
4c728ef5 168 return do_fsync(fd, 1);
cf9a2ae8
DH
169}
170
f79e2abb
AM
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
cce77081
PM
182 * range which are not presently under writeback. Note that this may block for
183 * significant periods due to exhaustion of disk request structures.
f79e2abb
AM
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 */
6673e0c3
HC
218SYSCALL_DEFINE(sync_file_range)(int fd, loff_t offset, loff_t nbytes,
219 unsigned int flags)
f79e2abb
AM
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)
111ebb6e 258 endbyte = LLONG_MAX;
f79e2abb
AM
259 else
260 endbyte--; /* inclusive */
261
262 ret = -EBADF;
263 file = fget_light(fd, &fput_needed);
264 if (!file)
265 goto out;
266
0f7fc9e4 267 i_mode = file->f_path.dentry->d_inode->i_mode;
f79e2abb
AM
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
ef51c976 273 ret = do_sync_mapping_range(file->f_mapping, offset, endbyte, flags);
f79e2abb
AM
274out_put:
275 fput_light(file, fput_needed);
276out:
277 return ret;
278}
6673e0c3
HC
279#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
f79e2abb 288
edd5cd4a
DW
289/* It would be nice if people remember that not all the world's an i386
290 when they introduce new system calls */
6673e0c3
HC
291SYSCALL_DEFINE(sync_file_range2)(int fd, unsigned int flags,
292 loff_t offset, loff_t nbytes)
edd5cd4a
DW
293{
294 return sys_sync_file_range(fd, offset, nbytes, flags);
295}
6673e0c3
HC
296#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
edd5cd4a 305
f79e2abb
AM
306/*
307 * `endbyte' is inclusive
308 */
5b04aa3a
MF
309int do_sync_mapping_range(struct address_space *mapping, loff_t offset,
310 loff_t endbyte, unsigned int flags)
f79e2abb
AM
311{
312 int ret;
f79e2abb 313
f79e2abb
AM
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,
ee53a891 330 WB_SYNC_ALL);
f79e2abb
AM
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 }
340out:
341 return ret;
342}
5b04aa3a 343EXPORT_SYMBOL_GPL(do_sync_mapping_range);