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> |
| 8 | #include <linux/module.h> |
Al Viro | 914e263 | 2006-10-18 13:55:46 -0400 | [diff] [blame] | 9 | #include <linux/sched.h> |
Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 10 | #include <linux/writeback.h> |
| 11 | #include <linux/syscalls.h> |
| 12 | #include <linux/linkage.h> |
| 13 | #include <linux/pagemap.h> |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 14 | #include <linux/quotaops.h> |
| 15 | #include <linux/buffer_head.h> |
Jan Kara | 5a3e5cb | 2009-04-27 16:43:48 +0200 | [diff] [blame^] | 16 | #include "internal.h" |
Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 17 | |
| 18 | #define VALID_FLAGS (SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE| \ |
| 19 | SYNC_FILE_RANGE_WAIT_AFTER) |
| 20 | |
| 21 | /* |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 22 | * sync everything. Start out by waking pdflush, because that writes back |
| 23 | * all queues in parallel. |
| 24 | */ |
| 25 | static void do_sync(unsigned long wait) |
| 26 | { |
| 27 | wakeup_pdflush(0); |
| 28 | sync_inodes(0); /* All mappings, inodes and their blockdevs */ |
Jan Kara | 9e3509e | 2009-01-26 16:45:12 +0100 | [diff] [blame] | 29 | vfs_dq_sync(NULL); |
Jan Kara | 5a3e5cb | 2009-04-27 16:43:48 +0200 | [diff] [blame^] | 30 | sync_inodes(wait); /* Mappings, inodes and blockdevs, again. */ |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 31 | sync_supers(); /* Write the superblocks */ |
| 32 | sync_filesystems(0); /* Start syncing the filesystems */ |
| 33 | sync_filesystems(wait); /* Waitingly sync the filesystems */ |
Jan Kara | 5a3e5cb | 2009-04-27 16:43:48 +0200 | [diff] [blame^] | 34 | sync_blockdevs(); |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 35 | if (!wait) |
| 36 | printk("Emergency Sync complete\n"); |
| 37 | if (unlikely(laptop_mode)) |
| 38 | laptop_sync_completion(); |
| 39 | } |
| 40 | |
Heiko Carstens | a5f8fa9 | 2009-01-14 14:14:11 +0100 | [diff] [blame] | 41 | SYSCALL_DEFINE0(sync) |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 42 | { |
| 43 | do_sync(1); |
| 44 | return 0; |
| 45 | } |
| 46 | |
Jens Axboe | a2a9537 | 2009-03-17 09:38:40 +0100 | [diff] [blame] | 47 | static void do_sync_work(struct work_struct *work) |
| 48 | { |
| 49 | do_sync(0); |
| 50 | kfree(work); |
| 51 | } |
| 52 | |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 53 | void emergency_sync(void) |
| 54 | { |
Jens Axboe | a2a9537 | 2009-03-17 09:38:40 +0100 | [diff] [blame] | 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 | } |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 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 | */ |
| 69 | int 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); |
OGAWA Hirofumi | 762873c | 2008-04-29 00:59:42 -0700 | [diff] [blame] | 81 | if (sb->s_dirt && sb->s_op->write_super) |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 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 | |
Christoph Hellwig | 4c728ef | 2008-12-22 21:11:15 +0100 | [diff] [blame] | 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 | */ |
| 105 | int vfs_fsync(struct file *file, struct dentry *dentry, int datasync) |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 106 | { |
Christoph Hellwig | 4c728ef | 2008-12-22 21:11:15 +0100 | [diff] [blame] | 107 | const struct file_operations *fop; |
| 108 | struct address_space *mapping; |
| 109 | int err, ret; |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 110 | |
Christoph Hellwig | 4c728ef | 2008-12-22 21:11:15 +0100 | [diff] [blame] | 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 | } |
| 123 | |
| 124 | if (!fop || !fop->fsync) { |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 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); |
Christoph Hellwig | 4c728ef | 2008-12-22 21:11:15 +0100 | [diff] [blame] | 136 | err = fop->fsync(file, dentry, datasync); |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 137 | if (!ret) |
| 138 | ret = err; |
| 139 | mutex_unlock(&mapping->host->i_mutex); |
| 140 | err = filemap_fdatawait(mapping); |
| 141 | if (!ret) |
| 142 | ret = err; |
| 143 | out: |
| 144 | return ret; |
| 145 | } |
Christoph Hellwig | 4c728ef | 2008-12-22 21:11:15 +0100 | [diff] [blame] | 146 | EXPORT_SYMBOL(vfs_fsync); |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 147 | |
Christoph Hellwig | 4c728ef | 2008-12-22 21:11:15 +0100 | [diff] [blame] | 148 | static int do_fsync(unsigned int fd, int datasync) |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 149 | { |
| 150 | struct file *file; |
| 151 | int ret = -EBADF; |
| 152 | |
| 153 | file = fget(fd); |
| 154 | if (file) { |
Christoph Hellwig | 4c728ef | 2008-12-22 21:11:15 +0100 | [diff] [blame] | 155 | ret = vfs_fsync(file, file->f_path.dentry, datasync); |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 156 | fput(file); |
| 157 | } |
| 158 | return ret; |
| 159 | } |
| 160 | |
Heiko Carstens | a5f8fa9 | 2009-01-14 14:14:11 +0100 | [diff] [blame] | 161 | SYSCALL_DEFINE1(fsync, unsigned int, fd) |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 162 | { |
Christoph Hellwig | 4c728ef | 2008-12-22 21:11:15 +0100 | [diff] [blame] | 163 | return do_fsync(fd, 0); |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 164 | } |
| 165 | |
Heiko Carstens | a5f8fa9 | 2009-01-14 14:14:11 +0100 | [diff] [blame] | 166 | SYSCALL_DEFINE1(fdatasync, unsigned int, fd) |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 167 | { |
Christoph Hellwig | 4c728ef | 2008-12-22 21:11:15 +0100 | [diff] [blame] | 168 | return do_fsync(fd, 1); |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /* |
Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 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 |
Pavel Machek | cce7708 | 2008-07-23 21:27:36 -0700 | [diff] [blame] | 182 | * range which are not presently under writeback. Note that this may block for |
| 183 | * significant periods due to exhaustion of disk request structures. |
Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 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 | */ |
Heiko Carstens | 6673e0c | 2009-01-14 14:14:02 +0100 | [diff] [blame] | 218 | SYSCALL_DEFINE(sync_file_range)(int fd, loff_t offset, loff_t nbytes, |
| 219 | unsigned int flags) |
Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 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) |
OGAWA Hirofumi | 111ebb6 | 2006-06-23 02:03:26 -0700 | [diff] [blame] | 258 | endbyte = LLONG_MAX; |
Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 259 | else |
| 260 | endbyte--; /* inclusive */ |
| 261 | |
| 262 | ret = -EBADF; |
| 263 | file = fget_light(fd, &fput_needed); |
| 264 | if (!file) |
| 265 | goto out; |
| 266 | |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 267 | i_mode = file->f_path.dentry->d_inode->i_mode; |
Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 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 | |
Mark Fasheh | ef51c97 | 2007-05-08 00:27:10 -0700 | [diff] [blame] | 273 | ret = do_sync_mapping_range(file->f_mapping, offset, endbyte, flags); |
Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 274 | out_put: |
| 275 | fput_light(file, fput_needed); |
| 276 | out: |
| 277 | return ret; |
| 278 | } |
Heiko Carstens | 6673e0c | 2009-01-14 14:14:02 +0100 | [diff] [blame] | 279 | #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS |
| 280 | asmlinkage 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 | } |
| 286 | SYSCALL_ALIAS(sys_sync_file_range, SyS_sync_file_range); |
| 287 | #endif |
Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 288 | |
David Woodhouse | edd5cd4 | 2007-06-27 14:10:09 -0700 | [diff] [blame] | 289 | /* It would be nice if people remember that not all the world's an i386 |
| 290 | when they introduce new system calls */ |
Heiko Carstens | 6673e0c | 2009-01-14 14:14:02 +0100 | [diff] [blame] | 291 | SYSCALL_DEFINE(sync_file_range2)(int fd, unsigned int flags, |
| 292 | loff_t offset, loff_t nbytes) |
David Woodhouse | edd5cd4 | 2007-06-27 14:10:09 -0700 | [diff] [blame] | 293 | { |
| 294 | return sys_sync_file_range(fd, offset, nbytes, flags); |
| 295 | } |
Heiko Carstens | 6673e0c | 2009-01-14 14:14:02 +0100 | [diff] [blame] | 296 | #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS |
| 297 | asmlinkage 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 | } |
| 303 | SYSCALL_ALIAS(sys_sync_file_range2, SyS_sync_file_range2); |
| 304 | #endif |
David Woodhouse | edd5cd4 | 2007-06-27 14:10:09 -0700 | [diff] [blame] | 305 | |
Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 306 | /* |
| 307 | * `endbyte' is inclusive |
| 308 | */ |
Mark Fasheh | 5b04aa3 | 2007-03-01 11:01:55 -0800 | [diff] [blame] | 309 | int do_sync_mapping_range(struct address_space *mapping, loff_t offset, |
| 310 | loff_t endbyte, unsigned int flags) |
Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 311 | { |
| 312 | int ret; |
Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 313 | |
Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 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, |
Nick Piggin | ee53a89 | 2009-01-06 14:39:12 -0800 | [diff] [blame] | 330 | WB_SYNC_ALL); |
Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 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 | } |
| 340 | out: |
| 341 | return ret; |
| 342 | } |
Mark Fasheh | 5b04aa3 | 2007-03-01 11:01:55 -0800 | [diff] [blame] | 343 | EXPORT_SYMBOL_GPL(do_sync_mapping_range); |