ARM: cpu topology: Add debugfs interface for cpu_power
[cmplus.git] / fs / sync.c
blobe2076b284fbafde21ff6aac5946bf65447d06d7d
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/slab.h>
9 #include <linux/module.h>
10 #include <linux/namei.h>
11 #include <linux/sched.h>
12 #include <linux/writeback.h>
13 #include <linux/syscalls.h>
14 #include <linux/linkage.h>
15 #include <linux/pagemap.h>
16 #include <linux/quotaops.h>
17 #include <linux/buffer_head.h>
18 #include <linux/backing-dev.h>
19 #include "internal.h"
21 #define VALID_FLAGS (SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE| \
22 SYNC_FILE_RANGE_WAIT_AFTER)
24 #ifdef CONFIG_FSYNC_CONTROL
25 extern bool fsynccontrol_fsync_enabled();
26 #endif
29 * Do the filesystem syncing work. For simple filesystems
30 * writeback_inodes_sb(sb) just dirties buffers with inodes so we have to
31 * submit IO for these buffers via __sync_blockdev(). This also speeds up the
32 * wait == 1 case since in that case write_inode() functions do
33 * sync_dirty_buffer() and thus effectively write one block at a time.
35 static int __sync_filesystem(struct super_block *sb, int wait)
38 * This should be safe, as we require bdi backing to actually
39 * write out data in the first place
41 if (sb->s_bdi == &noop_backing_dev_info)
42 return 0;
44 if (sb->s_qcop && sb->s_qcop->quota_sync)
45 sb->s_qcop->quota_sync(sb, -1, wait);
47 if (wait)
48 sync_inodes_sb(sb);
49 else
50 writeback_inodes_sb(sb);
52 if (sb->s_op->sync_fs)
53 sb->s_op->sync_fs(sb, wait);
54 return __sync_blockdev(sb->s_bdev, wait);
58 * Write out and wait upon all dirty data associated with this
59 * superblock. Filesystem data as well as the underlying block
60 * device. Takes the superblock lock.
62 int sync_filesystem(struct super_block *sb)
64 int ret;
67 * We need to be protected against the filesystem going from
68 * r/o to r/w or vice versa.
70 WARN_ON(!rwsem_is_locked(&sb->s_umount));
73 * No point in syncing out anything if the filesystem is read-only.
75 if (sb->s_flags & MS_RDONLY)
76 return 0;
78 ret = __sync_filesystem(sb, 0);
79 if (ret < 0)
80 return ret;
81 return __sync_filesystem(sb, 1);
83 EXPORT_SYMBOL_GPL(sync_filesystem);
85 static void sync_one_sb(struct super_block *sb, void *arg)
87 if (!(sb->s_flags & MS_RDONLY))
88 __sync_filesystem(sb, *(int *)arg);
91 * Sync all the data for all the filesystems (called by sys_sync() and
92 * emergency sync)
94 static void sync_filesystems(int wait)
96 iterate_supers(sync_one_sb, &wait);
100 * sync everything. Start out by waking pdflush, because that writes back
101 * all queues in parallel.
103 SYSCALL_DEFINE0(sync)
105 wakeup_flusher_threads(0);
106 sync_filesystems(0);
107 sync_filesystems(1);
108 if (unlikely(laptop_mode))
109 laptop_sync_completion();
110 return 0;
113 static void do_sync_work(struct work_struct *work)
116 * Sync twice to reduce the possibility we skipped some inodes / pages
117 * because they were temporarily locked
119 sync_filesystems(0);
120 sync_filesystems(0);
121 printk("Emergency Sync complete\n");
122 kfree(work);
125 void emergency_sync(void)
127 struct work_struct *work;
129 work = kmalloc(sizeof(*work), GFP_ATOMIC);
130 if (work) {
131 INIT_WORK(work, do_sync_work);
132 schedule_work(work);
137 * sync a single super
139 SYSCALL_DEFINE1(syncfs, int, fd)
141 struct file *file;
142 struct super_block *sb;
143 int ret;
144 int fput_needed;
146 #ifdef CONFIG_FSYNC_CONTROL
147 if (!fsynccontrol_fsync_enabled())
148 return 0;
149 #endif
151 file = fget_light(fd, &fput_needed);
152 if (!file)
153 return -EBADF;
154 sb = file->f_dentry->d_sb;
156 down_read(&sb->s_umount);
157 ret = sync_filesystem(sb);
158 up_read(&sb->s_umount);
160 fput_light(file, fput_needed);
161 return ret;
165 * vfs_fsync_range - helper to sync a range of data & metadata to disk
166 * @file: file to sync
167 * @start: offset in bytes of the beginning of data range to sync
168 * @end: offset in bytes of the end of data range (inclusive)
169 * @datasync: perform only datasync
171 * Write back data in range @start..@end and metadata for @file to disk. If
172 * @datasync is set only metadata needed to access modified file data is
173 * written.
175 int vfs_fsync_range(struct file *file, loff_t start, loff_t end, int datasync)
177 struct address_space *mapping = file->f_mapping;
178 int err, ret;
180 #ifdef CONFIG_FSYNC_CONTROL
181 if (!fsynccontrol_fsync_enabled())
182 return 0;
183 #endif
185 if (!file->f_op || !file->f_op->fsync) {
186 ret = -EINVAL;
187 goto out;
190 ret = filemap_write_and_wait_range(mapping, start, end);
193 * We need to protect against concurrent writers, which could cause
194 * livelocks in fsync_buffers_list().
196 mutex_lock(&mapping->host->i_mutex);
197 err = file->f_op->fsync(file, datasync);
198 if (!ret)
199 ret = err;
200 mutex_unlock(&mapping->host->i_mutex);
202 out:
203 return ret;
205 EXPORT_SYMBOL(vfs_fsync_range);
208 * vfs_fsync - perform a fsync or fdatasync on a file
209 * @file: file to sync
210 * @datasync: only perform a fdatasync operation
212 * Write back data and metadata for @file to disk. If @datasync is
213 * set only metadata needed to access modified file data is written.
215 int vfs_fsync(struct file *file, int datasync)
217 #ifdef CONFIG_FSYNC_CONTROL
218 if (!fsynccontrol_fsync_enabled())
219 return 0;
220 #endif
222 return vfs_fsync_range(file, 0, LLONG_MAX, datasync);
224 EXPORT_SYMBOL(vfs_fsync);
226 static int do_fsync(unsigned int fd, int datasync)
228 struct file *file;
229 int ret = -EBADF;
231 #ifdef CONFIG_FSYNC_CONTROL
232 if (!fsynccontrol_fsync_enabled())
233 return 0;
234 #endif
236 file = fget(fd);
237 if (file) {
238 ret = vfs_fsync(file, datasync);
239 fput(file);
241 return ret;
244 SYSCALL_DEFINE1(fsync, unsigned int, fd)
246 #ifdef CONFIG_FSYNC_CONTROL
247 if (!fsynccontrol_fsync_enabled())
248 return 0;
249 #endif
251 return do_fsync(fd, 0);
254 SYSCALL_DEFINE1(fdatasync, unsigned int, fd)
256 #ifdef CONFIG_FSYNC_CONTROL
257 if (!fsynccontrol_fsync_enabled())
258 return 0;
259 #endif
261 return do_fsync(fd, 1);
265 * generic_write_sync - perform syncing after a write if file / inode is sync
266 * @file: file to which the write happened
267 * @pos: offset where the write started
268 * @count: length of the write
270 * This is just a simple wrapper about our general syncing function.
272 int generic_write_sync(struct file *file, loff_t pos, loff_t count)
274 #ifdef CONFIG_FSYNC_CONTROL
275 if (!fsynccontrol_fsync_enabled())
276 return 0;
277 #endif
279 if (!(file->f_flags & O_DSYNC) && !IS_SYNC(file->f_mapping->host))
280 return 0;
281 return vfs_fsync_range(file, pos, pos + count - 1,
282 (file->f_flags & __O_SYNC) ? 0 : 1);
284 EXPORT_SYMBOL(generic_write_sync);
287 * sys_sync_file_range() permits finely controlled syncing over a segment of
288 * a file in the range offset .. (offset+nbytes-1) inclusive. If nbytes is
289 * zero then sys_sync_file_range() will operate from offset out to EOF.
291 * The flag bits are:
293 * SYNC_FILE_RANGE_WAIT_BEFORE: wait upon writeout of all pages in the range
294 * before performing the write.
296 * SYNC_FILE_RANGE_WRITE: initiate writeout of all those dirty pages in the
297 * range which are not presently under writeback. Note that this may block for
298 * significant periods due to exhaustion of disk request structures.
300 * SYNC_FILE_RANGE_WAIT_AFTER: wait upon writeout of all pages in the range
301 * after performing the write.
303 * Useful combinations of the flag bits are:
305 * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE: ensures that all pages
306 * in the range which were dirty on entry to sys_sync_file_range() are placed
307 * under writeout. This is a start-write-for-data-integrity operation.
309 * SYNC_FILE_RANGE_WRITE: start writeout of all dirty pages in the range which
310 * are not presently under writeout. This is an asynchronous flush-to-disk
311 * operation. Not suitable for data integrity operations.
313 * SYNC_FILE_RANGE_WAIT_BEFORE (or SYNC_FILE_RANGE_WAIT_AFTER): wait for
314 * completion of writeout of all pages in the range. This will be used after an
315 * earlier SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE operation to wait
316 * for that operation to complete and to return the result.
318 * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE|SYNC_FILE_RANGE_WAIT_AFTER:
319 * a traditional sync() operation. This is a write-for-data-integrity operation
320 * which will ensure that all pages in the range which were dirty on entry to
321 * sys_sync_file_range() are committed to disk.
324 * SYNC_FILE_RANGE_WAIT_BEFORE and SYNC_FILE_RANGE_WAIT_AFTER will detect any
325 * I/O errors or ENOSPC conditions and will return those to the caller, after
326 * clearing the EIO and ENOSPC flags in the address_space.
328 * It should be noted that none of these operations write out the file's
329 * metadata. So unless the application is strictly performing overwrites of
330 * already-instantiated disk blocks, there are no guarantees here that the data
331 * will be available after a crash.
333 SYSCALL_DEFINE(sync_file_range)(int fd, loff_t offset, loff_t nbytes,
334 unsigned int flags)
336 int ret;
337 struct file *file;
338 struct address_space *mapping;
339 loff_t endbyte; /* inclusive */
340 int fput_needed;
341 umode_t i_mode;
343 #ifdef CONFIG_FSYNC_CONTROL
344 if (!fsynccontrol_fsync_enabled())
345 return 0;
346 #endif
348 ret = -EINVAL;
349 if (flags & ~VALID_FLAGS)
350 goto out;
352 endbyte = offset + nbytes;
354 if ((s64)offset < 0)
355 goto out;
356 if ((s64)endbyte < 0)
357 goto out;
358 if (endbyte < offset)
359 goto out;
361 if (sizeof(pgoff_t) == 4) {
362 if (offset >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
364 * The range starts outside a 32 bit machine's
365 * pagecache addressing capabilities. Let it "succeed"
367 ret = 0;
368 goto out;
370 if (endbyte >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
372 * Out to EOF
374 nbytes = 0;
378 if (nbytes == 0)
379 endbyte = LLONG_MAX;
380 else
381 endbyte--; /* inclusive */
383 ret = -EBADF;
384 file = fget_light(fd, &fput_needed);
385 if (!file)
386 goto out;
388 i_mode = file->f_path.dentry->d_inode->i_mode;
389 ret = -ESPIPE;
390 if (!S_ISREG(i_mode) && !S_ISBLK(i_mode) && !S_ISDIR(i_mode) &&
391 !S_ISLNK(i_mode))
392 goto out_put;
394 mapping = file->f_mapping;
395 if (!mapping) {
396 ret = -EINVAL;
397 goto out_put;
400 ret = 0;
401 if (flags & SYNC_FILE_RANGE_WAIT_BEFORE) {
402 ret = filemap_fdatawait_range(mapping, offset, endbyte);
403 if (ret < 0)
404 goto out_put;
407 if (flags & SYNC_FILE_RANGE_WRITE) {
408 ret = filemap_fdatawrite_range(mapping, offset, endbyte);
409 if (ret < 0)
410 goto out_put;
413 if (flags & SYNC_FILE_RANGE_WAIT_AFTER)
414 ret = filemap_fdatawait_range(mapping, offset, endbyte);
416 out_put:
417 fput_light(file, fput_needed);
418 out:
419 return ret;
421 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
422 asmlinkage long SyS_sync_file_range(long fd, loff_t offset, loff_t nbytes,
423 long flags)
425 return SYSC_sync_file_range((int) fd, offset, nbytes,
426 (unsigned int) flags);
428 SYSCALL_ALIAS(sys_sync_file_range, SyS_sync_file_range);
429 #endif
431 /* It would be nice if people remember that not all the world's an i386
432 when they introduce new system calls */
433 SYSCALL_DEFINE(sync_file_range2)(int fd, unsigned int flags,
434 loff_t offset, loff_t nbytes)
436 return sys_sync_file_range(fd, offset, nbytes, flags);
438 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
439 asmlinkage long SyS_sync_file_range2(long fd, long flags,
440 loff_t offset, loff_t nbytes)
442 return SYSC_sync_file_range2((int) fd, (unsigned int) flags,
443 offset, nbytes);
445 SYSCALL_ALIAS(sys_sync_file_range2, SyS_sync_file_range2);
446 #endif