Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * High-level sync()-related operations |
| 3 | */ |
| 4 | |
| 5 | #include <linux/kernel.h> |
| 6 | #include <linux/file.h> |
| 7 | #include <linux/fs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame^] | 8 | #include <linux/slab.h> |
Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 9 | #include <linux/module.h> |
Al Viro | 914e263 | 2006-10-18 13:55:46 -0400 | [diff] [blame] | 10 | #include <linux/sched.h> |
Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 11 | #include <linux/writeback.h> |
| 12 | #include <linux/syscalls.h> |
| 13 | #include <linux/linkage.h> |
| 14 | #include <linux/pagemap.h> |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 15 | #include <linux/quotaops.h> |
| 16 | #include <linux/buffer_head.h> |
Jan Kara | 5a3e5cb | 2009-04-27 16:43:48 +0200 | [diff] [blame] | 17 | #include "internal.h" |
Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 18 | |
| 19 | #define VALID_FLAGS (SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE| \ |
| 20 | SYNC_FILE_RANGE_WAIT_AFTER) |
| 21 | |
Jan Kara | c15c54f | 2009-04-27 16:43:52 +0200 | [diff] [blame] | 22 | /* |
Jens Axboe | d8a8559 | 2009-09-02 12:34:32 +0200 | [diff] [blame] | 23 | * Do the filesystem syncing work. For simple filesystems |
| 24 | * writeback_inodes_sb(sb) just dirties buffers with inodes so we have to |
| 25 | * submit IO for these buffers via __sync_blockdev(). This also speeds up the |
| 26 | * wait == 1 case since in that case write_inode() functions do |
| 27 | * sync_dirty_buffer() and thus effectively write one block at a time. |
Jan Kara | c15c54f | 2009-04-27 16:43:52 +0200 | [diff] [blame] | 28 | */ |
Jan Kara | 60b0680 | 2009-04-27 16:43:53 +0200 | [diff] [blame] | 29 | static int __sync_filesystem(struct super_block *sb, int wait) |
Jan Kara | c15c54f | 2009-04-27 16:43:52 +0200 | [diff] [blame] | 30 | { |
Jens Axboe | 32a88aa | 2009-09-16 15:02:33 +0200 | [diff] [blame] | 31 | /* |
| 32 | * This should be safe, as we require bdi backing to actually |
| 33 | * write out data in the first place |
| 34 | */ |
| 35 | if (!sb->s_bdi) |
| 36 | return 0; |
| 37 | |
Christoph Hellwig | 5fb324a | 2010-02-16 03:44:52 -0500 | [diff] [blame] | 38 | if (sb->s_qcop && sb->s_qcop->quota_sync) |
| 39 | sb->s_qcop->quota_sync(sb, -1, wait); |
| 40 | |
| 41 | if (wait) |
Jens Axboe | d8a8559 | 2009-09-02 12:34:32 +0200 | [diff] [blame] | 42 | sync_inodes_sb(sb); |
Christoph Hellwig | 5fb324a | 2010-02-16 03:44:52 -0500 | [diff] [blame] | 43 | else |
| 44 | writeback_inodes_sb(sb); |
| 45 | |
Jan Kara | c15c54f | 2009-04-27 16:43:52 +0200 | [diff] [blame] | 46 | if (sb->s_op->sync_fs) |
| 47 | sb->s_op->sync_fs(sb, wait); |
| 48 | return __sync_blockdev(sb->s_bdev, wait); |
| 49 | } |
| 50 | |
| 51 | /* |
| 52 | * Write out and wait upon all dirty data associated with this |
| 53 | * superblock. Filesystem data as well as the underlying block |
| 54 | * device. Takes the superblock lock. |
| 55 | */ |
Jan Kara | 60b0680 | 2009-04-27 16:43:53 +0200 | [diff] [blame] | 56 | int sync_filesystem(struct super_block *sb) |
Jan Kara | c15c54f | 2009-04-27 16:43:52 +0200 | [diff] [blame] | 57 | { |
| 58 | int ret; |
| 59 | |
Christoph Hellwig | 5af7926 | 2009-05-05 15:41:25 +0200 | [diff] [blame] | 60 | /* |
| 61 | * We need to be protected against the filesystem going from |
| 62 | * r/o to r/w or vice versa. |
| 63 | */ |
| 64 | WARN_ON(!rwsem_is_locked(&sb->s_umount)); |
| 65 | |
| 66 | /* |
| 67 | * No point in syncing out anything if the filesystem is read-only. |
| 68 | */ |
| 69 | if (sb->s_flags & MS_RDONLY) |
| 70 | return 0; |
| 71 | |
Jan Kara | 60b0680 | 2009-04-27 16:43:53 +0200 | [diff] [blame] | 72 | ret = __sync_filesystem(sb, 0); |
Jan Kara | c15c54f | 2009-04-27 16:43:52 +0200 | [diff] [blame] | 73 | if (ret < 0) |
| 74 | return ret; |
Jan Kara | 60b0680 | 2009-04-27 16:43:53 +0200 | [diff] [blame] | 75 | return __sync_filesystem(sb, 1); |
Jan Kara | c15c54f | 2009-04-27 16:43:52 +0200 | [diff] [blame] | 76 | } |
Jan Kara | 60b0680 | 2009-04-27 16:43:53 +0200 | [diff] [blame] | 77 | EXPORT_SYMBOL_GPL(sync_filesystem); |
Jan Kara | c15c54f | 2009-04-27 16:43:52 +0200 | [diff] [blame] | 78 | |
| 79 | /* |
| 80 | * Sync all the data for all the filesystems (called by sys_sync() and |
| 81 | * emergency sync) |
| 82 | * |
| 83 | * This operation is careful to avoid the livelock which could easily happen |
| 84 | * if two or more filesystems are being continuously dirtied. s_need_sync |
| 85 | * is used only here. We set it against all filesystems and then clear it as |
| 86 | * we sync them. So redirtied filesystems are skipped. |
| 87 | * |
| 88 | * But if process A is currently running sync_filesystems and then process B |
| 89 | * calls sync_filesystems as well, process B will set all the s_need_sync |
| 90 | * flags again, which will cause process A to resync everything. Fix that with |
| 91 | * a local mutex. |
| 92 | */ |
| 93 | static void sync_filesystems(int wait) |
| 94 | { |
| 95 | struct super_block *sb; |
| 96 | static DEFINE_MUTEX(mutex); |
| 97 | |
| 98 | mutex_lock(&mutex); /* Could be down_interruptible */ |
| 99 | spin_lock(&sb_lock); |
Christoph Hellwig | 5af7926 | 2009-05-05 15:41:25 +0200 | [diff] [blame] | 100 | list_for_each_entry(sb, &super_blocks, s_list) |
Jan Kara | c15c54f | 2009-04-27 16:43:52 +0200 | [diff] [blame] | 101 | sb->s_need_sync = 1; |
Jan Kara | c15c54f | 2009-04-27 16:43:52 +0200 | [diff] [blame] | 102 | |
| 103 | restart: |
| 104 | list_for_each_entry(sb, &super_blocks, s_list) { |
| 105 | if (!sb->s_need_sync) |
| 106 | continue; |
| 107 | sb->s_need_sync = 0; |
Jan Kara | c15c54f | 2009-04-27 16:43:52 +0200 | [diff] [blame] | 108 | sb->s_count++; |
| 109 | spin_unlock(&sb_lock); |
Christoph Hellwig | 5af7926 | 2009-05-05 15:41:25 +0200 | [diff] [blame] | 110 | |
Jan Kara | c15c54f | 2009-04-27 16:43:52 +0200 | [diff] [blame] | 111 | down_read(&sb->s_umount); |
Jens Axboe | 32a88aa | 2009-09-16 15:02:33 +0200 | [diff] [blame] | 112 | if (!(sb->s_flags & MS_RDONLY) && sb->s_root && sb->s_bdi) |
Jan Kara | 60b0680 | 2009-04-27 16:43:53 +0200 | [diff] [blame] | 113 | __sync_filesystem(sb, wait); |
Jan Kara | c15c54f | 2009-04-27 16:43:52 +0200 | [diff] [blame] | 114 | up_read(&sb->s_umount); |
Christoph Hellwig | 5af7926 | 2009-05-05 15:41:25 +0200 | [diff] [blame] | 115 | |
Jan Kara | c15c54f | 2009-04-27 16:43:52 +0200 | [diff] [blame] | 116 | /* restart only when sb is no longer on the list */ |
| 117 | spin_lock(&sb_lock); |
| 118 | if (__put_super_and_need_restart(sb)) |
| 119 | goto restart; |
| 120 | } |
| 121 | spin_unlock(&sb_lock); |
| 122 | mutex_unlock(&mutex); |
| 123 | } |
| 124 | |
Zhang, Yanmin | 3beab0b | 2009-07-05 12:08:08 -0700 | [diff] [blame] | 125 | /* |
| 126 | * sync everything. Start out by waking pdflush, because that writes back |
| 127 | * all queues in parallel. |
| 128 | */ |
Heiko Carstens | a5f8fa9 | 2009-01-14 14:14:11 +0100 | [diff] [blame] | 129 | SYSCALL_DEFINE0(sync) |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 130 | { |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 131 | wakeup_flusher_threads(0); |
Jan Kara | 5cee581 | 2009-04-27 16:43:51 +0200 | [diff] [blame] | 132 | sync_filesystems(0); |
| 133 | sync_filesystems(1); |
| 134 | if (unlikely(laptop_mode)) |
| 135 | laptop_sync_completion(); |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 136 | return 0; |
| 137 | } |
| 138 | |
Jens Axboe | a2a9537 | 2009-03-17 09:38:40 +0100 | [diff] [blame] | 139 | static void do_sync_work(struct work_struct *work) |
| 140 | { |
Jan Kara | 5cee581 | 2009-04-27 16:43:51 +0200 | [diff] [blame] | 141 | /* |
| 142 | * Sync twice to reduce the possibility we skipped some inodes / pages |
| 143 | * because they were temporarily locked |
| 144 | */ |
| 145 | sync_filesystems(0); |
| 146 | sync_filesystems(0); |
| 147 | printk("Emergency Sync complete\n"); |
Jens Axboe | a2a9537 | 2009-03-17 09:38:40 +0100 | [diff] [blame] | 148 | kfree(work); |
| 149 | } |
| 150 | |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 151 | void emergency_sync(void) |
| 152 | { |
Jens Axboe | a2a9537 | 2009-03-17 09:38:40 +0100 | [diff] [blame] | 153 | struct work_struct *work; |
| 154 | |
| 155 | work = kmalloc(sizeof(*work), GFP_ATOMIC); |
| 156 | if (work) { |
| 157 | INIT_WORK(work, do_sync_work); |
| 158 | schedule_work(work); |
| 159 | } |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /* |
| 163 | * Generic function to fsync a file. |
| 164 | * |
| 165 | * filp may be NULL if called via the msync of a vma. |
| 166 | */ |
| 167 | int file_fsync(struct file *filp, struct dentry *dentry, int datasync) |
| 168 | { |
| 169 | struct inode * inode = dentry->d_inode; |
| 170 | struct super_block * sb; |
| 171 | int ret, err; |
| 172 | |
| 173 | /* sync the inode to buffers */ |
| 174 | ret = write_inode_now(inode, 0); |
| 175 | |
| 176 | /* sync the superblock to buffers */ |
| 177 | sb = inode->i_sb; |
OGAWA Hirofumi | 762873c | 2008-04-29 00:59:42 -0700 | [diff] [blame] | 178 | if (sb->s_dirt && sb->s_op->write_super) |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 179 | sb->s_op->write_super(sb); |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 180 | |
| 181 | /* .. finally sync the buffers to disk */ |
| 182 | err = sync_blockdev(sb->s_bdev); |
| 183 | if (!ret) |
| 184 | ret = err; |
| 185 | return ret; |
| 186 | } |
H Hartley Sweeten | 1fe72ea | 2009-09-22 16:43:51 -0700 | [diff] [blame] | 187 | EXPORT_SYMBOL(file_fsync); |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 188 | |
Christoph Hellwig | 4c728ef | 2008-12-22 21:11:15 +0100 | [diff] [blame] | 189 | /** |
Jan Kara | 148f948 | 2009-08-17 19:52:36 +0200 | [diff] [blame] | 190 | * vfs_fsync_range - helper to sync a range of data & metadata to disk |
Christoph Hellwig | 4c728ef | 2008-12-22 21:11:15 +0100 | [diff] [blame] | 191 | * @file: file to sync |
| 192 | * @dentry: dentry of @file |
Jan Kara | 148f948 | 2009-08-17 19:52:36 +0200 | [diff] [blame] | 193 | * @start: offset in bytes of the beginning of data range to sync |
| 194 | * @end: offset in bytes of the end of data range (inclusive) |
| 195 | * @datasync: perform only datasync |
Christoph Hellwig | 4c728ef | 2008-12-22 21:11:15 +0100 | [diff] [blame] | 196 | * |
Jan Kara | 148f948 | 2009-08-17 19:52:36 +0200 | [diff] [blame] | 197 | * Write back data in range @start..@end and metadata for @file to disk. If |
| 198 | * @datasync is set only metadata needed to access modified file data is |
| 199 | * written. |
Christoph Hellwig | 4c728ef | 2008-12-22 21:11:15 +0100 | [diff] [blame] | 200 | * |
| 201 | * In case this function is called from nfsd @file may be %NULL and |
| 202 | * only @dentry is set. This can only happen when the filesystem |
| 203 | * implements the export_operations API. |
| 204 | */ |
Jan Kara | 148f948 | 2009-08-17 19:52:36 +0200 | [diff] [blame] | 205 | int vfs_fsync_range(struct file *file, struct dentry *dentry, loff_t start, |
| 206 | loff_t end, int datasync) |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 207 | { |
Christoph Hellwig | 4c728ef | 2008-12-22 21:11:15 +0100 | [diff] [blame] | 208 | const struct file_operations *fop; |
| 209 | struct address_space *mapping; |
| 210 | int err, ret; |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 211 | |
Christoph Hellwig | 4c728ef | 2008-12-22 21:11:15 +0100 | [diff] [blame] | 212 | /* |
| 213 | * Get mapping and operations from the file in case we have |
| 214 | * as file, or get the default values for them in case we |
| 215 | * don't have a struct file available. Damn nfsd.. |
| 216 | */ |
| 217 | if (file) { |
| 218 | mapping = file->f_mapping; |
| 219 | fop = file->f_op; |
| 220 | } else { |
| 221 | mapping = dentry->d_inode->i_mapping; |
| 222 | fop = dentry->d_inode->i_fop; |
| 223 | } |
| 224 | |
| 225 | if (!fop || !fop->fsync) { |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 226 | ret = -EINVAL; |
| 227 | goto out; |
| 228 | } |
| 229 | |
Christoph Hellwig | 2daea67 | 2009-09-03 12:39:39 +0200 | [diff] [blame] | 230 | ret = filemap_write_and_wait_range(mapping, start, end); |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 231 | |
| 232 | /* |
| 233 | * We need to protect against concurrent writers, which could cause |
| 234 | * livelocks in fsync_buffers_list(). |
| 235 | */ |
| 236 | mutex_lock(&mapping->host->i_mutex); |
Christoph Hellwig | 4c728ef | 2008-12-22 21:11:15 +0100 | [diff] [blame] | 237 | err = fop->fsync(file, dentry, datasync); |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 238 | if (!ret) |
| 239 | ret = err; |
| 240 | mutex_unlock(&mapping->host->i_mutex); |
Jan Kara | 148f948 | 2009-08-17 19:52:36 +0200 | [diff] [blame] | 241 | |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 242 | out: |
| 243 | return ret; |
| 244 | } |
Jan Kara | 148f948 | 2009-08-17 19:52:36 +0200 | [diff] [blame] | 245 | EXPORT_SYMBOL(vfs_fsync_range); |
| 246 | |
| 247 | /** |
| 248 | * vfs_fsync - perform a fsync or fdatasync on a file |
| 249 | * @file: file to sync |
| 250 | * @dentry: dentry of @file |
| 251 | * @datasync: only perform a fdatasync operation |
| 252 | * |
| 253 | * Write back data and metadata for @file to disk. If @datasync is |
| 254 | * set only metadata needed to access modified file data is written. |
| 255 | * |
| 256 | * In case this function is called from nfsd @file may be %NULL and |
| 257 | * only @dentry is set. This can only happen when the filesystem |
| 258 | * implements the export_operations API. |
| 259 | */ |
| 260 | int vfs_fsync(struct file *file, struct dentry *dentry, int datasync) |
| 261 | { |
| 262 | return vfs_fsync_range(file, dentry, 0, LLONG_MAX, datasync); |
| 263 | } |
Christoph Hellwig | 4c728ef | 2008-12-22 21:11:15 +0100 | [diff] [blame] | 264 | EXPORT_SYMBOL(vfs_fsync); |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 265 | |
Christoph Hellwig | 4c728ef | 2008-12-22 21:11:15 +0100 | [diff] [blame] | 266 | static int do_fsync(unsigned int fd, int datasync) |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 267 | { |
| 268 | struct file *file; |
| 269 | int ret = -EBADF; |
| 270 | |
| 271 | file = fget(fd); |
| 272 | if (file) { |
Christoph Hellwig | 4c728ef | 2008-12-22 21:11:15 +0100 | [diff] [blame] | 273 | ret = vfs_fsync(file, file->f_path.dentry, datasync); |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 274 | fput(file); |
| 275 | } |
| 276 | return ret; |
| 277 | } |
| 278 | |
Heiko Carstens | a5f8fa9 | 2009-01-14 14:14:11 +0100 | [diff] [blame] | 279 | SYSCALL_DEFINE1(fsync, unsigned int, fd) |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 280 | { |
Christoph Hellwig | 4c728ef | 2008-12-22 21:11:15 +0100 | [diff] [blame] | 281 | return do_fsync(fd, 0); |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 282 | } |
| 283 | |
Heiko Carstens | a5f8fa9 | 2009-01-14 14:14:11 +0100 | [diff] [blame] | 284 | SYSCALL_DEFINE1(fdatasync, unsigned int, fd) |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 285 | { |
Christoph Hellwig | 4c728ef | 2008-12-22 21:11:15 +0100 | [diff] [blame] | 286 | return do_fsync(fd, 1); |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 287 | } |
| 288 | |
Jan Kara | 148f948 | 2009-08-17 19:52:36 +0200 | [diff] [blame] | 289 | /** |
| 290 | * generic_write_sync - perform syncing after a write if file / inode is sync |
| 291 | * @file: file to which the write happened |
| 292 | * @pos: offset where the write started |
| 293 | * @count: length of the write |
| 294 | * |
| 295 | * This is just a simple wrapper about our general syncing function. |
| 296 | */ |
| 297 | int generic_write_sync(struct file *file, loff_t pos, loff_t count) |
| 298 | { |
Christoph Hellwig | 6b2f3d1 | 2009-10-27 11:05:28 +0100 | [diff] [blame] | 299 | if (!(file->f_flags & O_DSYNC) && !IS_SYNC(file->f_mapping->host)) |
Jan Kara | 148f948 | 2009-08-17 19:52:36 +0200 | [diff] [blame] | 300 | return 0; |
| 301 | return vfs_fsync_range(file, file->f_path.dentry, pos, |
Christoph Hellwig | 6b2f3d1 | 2009-10-27 11:05:28 +0100 | [diff] [blame] | 302 | pos + count - 1, |
| 303 | (file->f_flags & __O_SYNC) ? 0 : 1); |
Jan Kara | 148f948 | 2009-08-17 19:52:36 +0200 | [diff] [blame] | 304 | } |
| 305 | EXPORT_SYMBOL(generic_write_sync); |
| 306 | |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 307 | /* |
Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 308 | * sys_sync_file_range() permits finely controlled syncing over a segment of |
| 309 | * a file in the range offset .. (offset+nbytes-1) inclusive. If nbytes is |
| 310 | * zero then sys_sync_file_range() will operate from offset out to EOF. |
| 311 | * |
| 312 | * The flag bits are: |
| 313 | * |
| 314 | * SYNC_FILE_RANGE_WAIT_BEFORE: wait upon writeout of all pages in the range |
| 315 | * before performing the write. |
| 316 | * |
| 317 | * SYNC_FILE_RANGE_WRITE: initiate writeout of all those dirty pages in the |
Pavel Machek | cce7708 | 2008-07-23 21:27:36 -0700 | [diff] [blame] | 318 | * range which are not presently under writeback. Note that this may block for |
| 319 | * significant periods due to exhaustion of disk request structures. |
Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 320 | * |
| 321 | * SYNC_FILE_RANGE_WAIT_AFTER: wait upon writeout of all pages in the range |
| 322 | * after performing the write. |
| 323 | * |
| 324 | * Useful combinations of the flag bits are: |
| 325 | * |
| 326 | * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE: ensures that all pages |
| 327 | * in the range which were dirty on entry to sys_sync_file_range() are placed |
| 328 | * under writeout. This is a start-write-for-data-integrity operation. |
| 329 | * |
| 330 | * SYNC_FILE_RANGE_WRITE: start writeout of all dirty pages in the range which |
| 331 | * are not presently under writeout. This is an asynchronous flush-to-disk |
| 332 | * operation. Not suitable for data integrity operations. |
| 333 | * |
| 334 | * SYNC_FILE_RANGE_WAIT_BEFORE (or SYNC_FILE_RANGE_WAIT_AFTER): wait for |
| 335 | * completion of writeout of all pages in the range. This will be used after an |
| 336 | * earlier SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE operation to wait |
| 337 | * for that operation to complete and to return the result. |
| 338 | * |
| 339 | * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE|SYNC_FILE_RANGE_WAIT_AFTER: |
| 340 | * a traditional sync() operation. This is a write-for-data-integrity operation |
| 341 | * which will ensure that all pages in the range which were dirty on entry to |
| 342 | * sys_sync_file_range() are committed to disk. |
| 343 | * |
| 344 | * |
| 345 | * SYNC_FILE_RANGE_WAIT_BEFORE and SYNC_FILE_RANGE_WAIT_AFTER will detect any |
| 346 | * I/O errors or ENOSPC conditions and will return those to the caller, after |
| 347 | * clearing the EIO and ENOSPC flags in the address_space. |
| 348 | * |
| 349 | * It should be noted that none of these operations write out the file's |
| 350 | * metadata. So unless the application is strictly performing overwrites of |
| 351 | * already-instantiated disk blocks, there are no guarantees here that the data |
| 352 | * will be available after a crash. |
| 353 | */ |
Heiko Carstens | 6673e0c | 2009-01-14 14:14:02 +0100 | [diff] [blame] | 354 | SYSCALL_DEFINE(sync_file_range)(int fd, loff_t offset, loff_t nbytes, |
| 355 | unsigned int flags) |
Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 356 | { |
| 357 | int ret; |
| 358 | struct file *file; |
Christoph Hellwig | 7a0ad10 | 2009-12-17 14:24:40 +0100 | [diff] [blame] | 359 | struct address_space *mapping; |
Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 360 | loff_t endbyte; /* inclusive */ |
| 361 | int fput_needed; |
| 362 | umode_t i_mode; |
| 363 | |
| 364 | ret = -EINVAL; |
| 365 | if (flags & ~VALID_FLAGS) |
| 366 | goto out; |
| 367 | |
| 368 | endbyte = offset + nbytes; |
| 369 | |
| 370 | if ((s64)offset < 0) |
| 371 | goto out; |
| 372 | if ((s64)endbyte < 0) |
| 373 | goto out; |
| 374 | if (endbyte < offset) |
| 375 | goto out; |
| 376 | |
| 377 | if (sizeof(pgoff_t) == 4) { |
| 378 | if (offset >= (0x100000000ULL << PAGE_CACHE_SHIFT)) { |
| 379 | /* |
| 380 | * The range starts outside a 32 bit machine's |
| 381 | * pagecache addressing capabilities. Let it "succeed" |
| 382 | */ |
| 383 | ret = 0; |
| 384 | goto out; |
| 385 | } |
| 386 | if (endbyte >= (0x100000000ULL << PAGE_CACHE_SHIFT)) { |
| 387 | /* |
| 388 | * Out to EOF |
| 389 | */ |
| 390 | nbytes = 0; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | if (nbytes == 0) |
OGAWA Hirofumi | 111ebb6 | 2006-06-23 02:03:26 -0700 | [diff] [blame] | 395 | endbyte = LLONG_MAX; |
Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 396 | else |
| 397 | endbyte--; /* inclusive */ |
| 398 | |
| 399 | ret = -EBADF; |
| 400 | file = fget_light(fd, &fput_needed); |
| 401 | if (!file) |
| 402 | goto out; |
| 403 | |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 404 | i_mode = file->f_path.dentry->d_inode->i_mode; |
Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 405 | ret = -ESPIPE; |
| 406 | if (!S_ISREG(i_mode) && !S_ISBLK(i_mode) && !S_ISDIR(i_mode) && |
| 407 | !S_ISLNK(i_mode)) |
| 408 | goto out_put; |
| 409 | |
Christoph Hellwig | 7a0ad10 | 2009-12-17 14:24:40 +0100 | [diff] [blame] | 410 | mapping = file->f_mapping; |
| 411 | if (!mapping) { |
| 412 | ret = -EINVAL; |
| 413 | goto out_put; |
| 414 | } |
| 415 | |
| 416 | ret = 0; |
| 417 | if (flags & SYNC_FILE_RANGE_WAIT_BEFORE) { |
| 418 | ret = filemap_fdatawait_range(mapping, offset, endbyte); |
| 419 | if (ret < 0) |
| 420 | goto out_put; |
| 421 | } |
| 422 | |
| 423 | if (flags & SYNC_FILE_RANGE_WRITE) { |
| 424 | ret = filemap_fdatawrite_range(mapping, offset, endbyte); |
| 425 | if (ret < 0) |
| 426 | goto out_put; |
| 427 | } |
| 428 | |
| 429 | if (flags & SYNC_FILE_RANGE_WAIT_AFTER) |
| 430 | ret = filemap_fdatawait_range(mapping, offset, endbyte); |
| 431 | |
Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 432 | out_put: |
| 433 | fput_light(file, fput_needed); |
| 434 | out: |
| 435 | return ret; |
| 436 | } |
Heiko Carstens | 6673e0c | 2009-01-14 14:14:02 +0100 | [diff] [blame] | 437 | #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS |
| 438 | asmlinkage long SyS_sync_file_range(long fd, loff_t offset, loff_t nbytes, |
| 439 | long flags) |
| 440 | { |
| 441 | return SYSC_sync_file_range((int) fd, offset, nbytes, |
| 442 | (unsigned int) flags); |
| 443 | } |
| 444 | SYSCALL_ALIAS(sys_sync_file_range, SyS_sync_file_range); |
| 445 | #endif |
Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 446 | |
David Woodhouse | edd5cd4 | 2007-06-27 14:10:09 -0700 | [diff] [blame] | 447 | /* It would be nice if people remember that not all the world's an i386 |
| 448 | when they introduce new system calls */ |
Heiko Carstens | 6673e0c | 2009-01-14 14:14:02 +0100 | [diff] [blame] | 449 | SYSCALL_DEFINE(sync_file_range2)(int fd, unsigned int flags, |
| 450 | loff_t offset, loff_t nbytes) |
David Woodhouse | edd5cd4 | 2007-06-27 14:10:09 -0700 | [diff] [blame] | 451 | { |
| 452 | return sys_sync_file_range(fd, offset, nbytes, flags); |
| 453 | } |
Heiko Carstens | 6673e0c | 2009-01-14 14:14:02 +0100 | [diff] [blame] | 454 | #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS |
| 455 | asmlinkage long SyS_sync_file_range2(long fd, long flags, |
| 456 | loff_t offset, loff_t nbytes) |
| 457 | { |
| 458 | return SYSC_sync_file_range2((int) fd, (unsigned int) flags, |
| 459 | offset, nbytes); |
| 460 | } |
| 461 | SYSCALL_ALIAS(sys_sync_file_range2, SyS_sync_file_range2); |
| 462 | #endif |