aoe: remove unused NARGS enum
[linux-2.6.22.y-op.git] / fs / sync.c
blob1de747b5ddb9dcaf12ac0dc164f9a614db7b6d13
1 /*
2 * High-level sync()-related operations
3 */
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>
13 #include <linux/quotaops.h>
14 #include <linux/buffer_head.h>
16 #define VALID_FLAGS (SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE| \
17 SYNC_FILE_RANGE_WAIT_AFTER)
20 * sync everything. Start out by waking pdflush, because that writes back
21 * all queues in parallel.
23 static void do_sync(unsigned long wait)
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();
38 asmlinkage long sys_sync(void)
40 do_sync(1);
41 return 0;
44 void emergency_sync(void)
46 pdflush_operation(do_sync, 0);
50 * Generic function to fsync a file.
52 * filp may be NULL if called via the msync of a vma.
54 int file_fsync(struct file *filp, struct dentry *dentry, int datasync)
56 struct inode * inode = dentry->d_inode;
57 struct super_block * sb;
58 int ret, err;
60 /* sync the inode to buffers */
61 ret = write_inode_now(inode, 0);
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);
70 /* .. finally sync the buffers to disk */
71 err = sync_blockdev(sb->s_bdev);
72 if (!ret)
73 ret = err;
74 return ret;
77 long do_fsync(struct file *file, int datasync)
79 int ret;
80 int err;
81 struct address_space *mapping = file->f_mapping;
83 if (!file->f_op || !file->f_op->fsync) {
84 /* Why? We can still call filemap_fdatawrite */
85 ret = -EINVAL;
86 goto out;
89 ret = filemap_fdatawrite(mapping);
92 * We need to protect against concurrent writers, which could cause
93 * livelocks in fsync_buffers_list().
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;
103 out:
104 return ret;
107 static long __do_fsync(unsigned int fd, int datasync)
109 struct file *file;
110 int ret = -EBADF;
112 file = fget(fd);
113 if (file) {
114 ret = do_fsync(file, datasync);
115 fput(file);
117 return ret;
120 asmlinkage long sys_fsync(unsigned int fd)
122 return __do_fsync(fd, 0);
125 asmlinkage long sys_fdatasync(unsigned int fd)
127 return __do_fsync(fd, 1);
131 * 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.
135 * The flag bits are:
137 * SYNC_FILE_RANGE_WAIT_BEFORE: wait upon writeout of all pages in the range
138 * before performing the write.
140 * SYNC_FILE_RANGE_WRITE: initiate writeout of all those dirty pages in the
141 * range which are not presently under writeback.
143 * SYNC_FILE_RANGE_WAIT_AFTER: wait upon writeout of all pages in the range
144 * after performing the write.
146 * Useful combinations of the flag bits are:
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.
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.
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.
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.
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.
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.
176 asmlinkage long sys_sync_file_range(int fd, loff_t offset, loff_t nbytes,
177 unsigned int flags)
179 int ret;
180 struct file *file;
181 loff_t endbyte; /* inclusive */
182 int fput_needed;
183 umode_t i_mode;
185 ret = -EINVAL;
186 if (flags & ~VALID_FLAGS)
187 goto out;
189 endbyte = offset + nbytes;
191 if ((s64)offset < 0)
192 goto out;
193 if ((s64)endbyte < 0)
194 goto out;
195 if (endbyte < offset)
196 goto out;
198 if (sizeof(pgoff_t) == 4) {
199 if (offset >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
201 * The range starts outside a 32 bit machine's
202 * pagecache addressing capabilities. Let it "succeed"
204 ret = 0;
205 goto out;
207 if (endbyte >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
209 * Out to EOF
211 nbytes = 0;
215 if (nbytes == 0)
216 endbyte = LLONG_MAX;
217 else
218 endbyte--; /* inclusive */
220 ret = -EBADF;
221 file = fget_light(fd, &fput_needed);
222 if (!file)
223 goto out;
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;
231 ret = do_sync_file_range(file, offset, endbyte, flags);
232 out_put:
233 fput_light(file, fput_needed);
234 out:
235 return ret;
239 * `endbyte' is inclusive
241 int do_sync_file_range(struct file *file, loff_t offset, loff_t endbyte,
242 unsigned int flags)
244 int ret;
245 struct address_space *mapping;
247 mapping = file->f_mapping;
248 if (!mapping) {
249 ret = -EINVAL;
250 goto out;
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;
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;
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);
274 out:
275 return ret;
277 EXPORT_SYMBOL_GPL(do_sync_file_range);