| 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> | 
| Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 16 |  | 
 | 17 | #define VALID_FLAGS (SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE| \ | 
 | 18 | 			SYNC_FILE_RANGE_WAIT_AFTER) | 
 | 19 |  | 
 | 20 | /* | 
| David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 21 |  * sync everything.  Start out by waking pdflush, because that writes back | 
 | 22 |  * all queues in parallel. | 
 | 23 |  */ | 
 | 24 | static void do_sync(unsigned long wait) | 
 | 25 | { | 
 | 26 | 	wakeup_pdflush(0); | 
 | 27 | 	sync_inodes(0);		/* All mappings, inodes and their blockdevs */ | 
| Jan Kara | 9e3509e | 2009-01-26 16:45:12 +0100 | [diff] [blame] | 28 | 	vfs_dq_sync(NULL); | 
| David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 29 | 	sync_supers();		/* Write the superblocks */ | 
 | 30 | 	sync_filesystems(0);	/* Start syncing the filesystems */ | 
 | 31 | 	sync_filesystems(wait);	/* Waitingly sync the filesystems */ | 
 | 32 | 	sync_inodes(wait);	/* Mappings, inodes and blockdevs, again. */ | 
 | 33 | 	if (!wait) | 
 | 34 | 		printk("Emergency Sync complete\n"); | 
 | 35 | 	if (unlikely(laptop_mode)) | 
 | 36 | 		laptop_sync_completion(); | 
 | 37 | } | 
 | 38 |  | 
| Heiko Carstens | a5f8fa9 | 2009-01-14 14:14:11 +0100 | [diff] [blame] | 39 | SYSCALL_DEFINE0(sync) | 
| David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 40 | { | 
 | 41 | 	do_sync(1); | 
 | 42 | 	return 0; | 
 | 43 | } | 
 | 44 |  | 
| Jens Axboe | a2a9537 | 2009-03-17 09:38:40 +0100 | [diff] [blame] | 45 | static void do_sync_work(struct work_struct *work) | 
 | 46 | { | 
 | 47 | 	do_sync(0); | 
 | 48 | 	kfree(work); | 
 | 49 | } | 
 | 50 |  | 
| David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 51 | void emergency_sync(void) | 
 | 52 | { | 
| Jens Axboe | a2a9537 | 2009-03-17 09:38:40 +0100 | [diff] [blame] | 53 | 	struct work_struct *work; | 
 | 54 |  | 
 | 55 | 	work = kmalloc(sizeof(*work), GFP_ATOMIC); | 
 | 56 | 	if (work) { | 
 | 57 | 		INIT_WORK(work, do_sync_work); | 
 | 58 | 		schedule_work(work); | 
 | 59 | 	} | 
| David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 60 | } | 
 | 61 |  | 
 | 62 | /* | 
 | 63 |  * Generic function to fsync a file. | 
 | 64 |  * | 
 | 65 |  * filp may be NULL if called via the msync of a vma. | 
 | 66 |  */ | 
 | 67 | int file_fsync(struct file *filp, struct dentry *dentry, int datasync) | 
 | 68 | { | 
 | 69 | 	struct inode * inode = dentry->d_inode; | 
 | 70 | 	struct super_block * sb; | 
 | 71 | 	int ret, err; | 
 | 72 |  | 
 | 73 | 	/* sync the inode to buffers */ | 
 | 74 | 	ret = write_inode_now(inode, 0); | 
 | 75 |  | 
 | 76 | 	/* sync the superblock to buffers */ | 
 | 77 | 	sb = inode->i_sb; | 
 | 78 | 	lock_super(sb); | 
| OGAWA Hirofumi | 762873c | 2008-04-29 00:59:42 -0700 | [diff] [blame] | 79 | 	if (sb->s_dirt && sb->s_op->write_super) | 
| David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 80 | 		sb->s_op->write_super(sb); | 
 | 81 | 	unlock_super(sb); | 
 | 82 |  | 
 | 83 | 	/* .. finally sync the buffers to disk */ | 
 | 84 | 	err = sync_blockdev(sb->s_bdev); | 
 | 85 | 	if (!ret) | 
 | 86 | 		ret = err; | 
 | 87 | 	return ret; | 
 | 88 | } | 
 | 89 |  | 
| Christoph Hellwig | 4c728ef | 2008-12-22 21:11:15 +0100 | [diff] [blame] | 90 | /** | 
 | 91 |  * vfs_fsync - perform a fsync or fdatasync on a file | 
 | 92 |  * @file:		file to sync | 
 | 93 |  * @dentry:		dentry of @file | 
 | 94 |  * @data:		only perform a fdatasync operation | 
 | 95 |  * | 
 | 96 |  * Write back data and metadata for @file to disk.  If @datasync is | 
 | 97 |  * set only metadata needed to access modified file data is written. | 
 | 98 |  * | 
 | 99 |  * In case this function is called from nfsd @file may be %NULL and | 
 | 100 |  * only @dentry is set.  This can only happen when the filesystem | 
 | 101 |  * implements the export_operations API. | 
 | 102 |  */ | 
 | 103 | int vfs_fsync(struct file *file, struct dentry *dentry, int datasync) | 
| David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 104 | { | 
| Christoph Hellwig | 4c728ef | 2008-12-22 21:11:15 +0100 | [diff] [blame] | 105 | 	const struct file_operations *fop; | 
 | 106 | 	struct address_space *mapping; | 
 | 107 | 	int err, ret; | 
| David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 108 |  | 
| Christoph Hellwig | 4c728ef | 2008-12-22 21:11:15 +0100 | [diff] [blame] | 109 | 	/* | 
 | 110 | 	 * Get mapping and operations from the file in case we have | 
 | 111 | 	 * as file, or get the default values for them in case we | 
 | 112 | 	 * don't have a struct file available.  Damn nfsd.. | 
 | 113 | 	 */ | 
 | 114 | 	if (file) { | 
 | 115 | 		mapping = file->f_mapping; | 
 | 116 | 		fop = file->f_op; | 
 | 117 | 	} else { | 
 | 118 | 		mapping = dentry->d_inode->i_mapping; | 
 | 119 | 		fop = dentry->d_inode->i_fop; | 
 | 120 | 	} | 
 | 121 |  | 
 | 122 | 	if (!fop || !fop->fsync) { | 
| David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 123 | 		ret = -EINVAL; | 
 | 124 | 		goto out; | 
 | 125 | 	} | 
 | 126 |  | 
 | 127 | 	ret = filemap_fdatawrite(mapping); | 
 | 128 |  | 
 | 129 | 	/* | 
 | 130 | 	 * We need to protect against concurrent writers, which could cause | 
 | 131 | 	 * livelocks in fsync_buffers_list(). | 
 | 132 | 	 */ | 
 | 133 | 	mutex_lock(&mapping->host->i_mutex); | 
| Christoph Hellwig | 4c728ef | 2008-12-22 21:11:15 +0100 | [diff] [blame] | 134 | 	err = fop->fsync(file, dentry, datasync); | 
| David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 135 | 	if (!ret) | 
 | 136 | 		ret = err; | 
 | 137 | 	mutex_unlock(&mapping->host->i_mutex); | 
 | 138 | 	err = filemap_fdatawait(mapping); | 
 | 139 | 	if (!ret) | 
 | 140 | 		ret = err; | 
 | 141 | out: | 
 | 142 | 	return ret; | 
 | 143 | } | 
| Christoph Hellwig | 4c728ef | 2008-12-22 21:11:15 +0100 | [diff] [blame] | 144 | EXPORT_SYMBOL(vfs_fsync); | 
| David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 145 |  | 
| Christoph Hellwig | 4c728ef | 2008-12-22 21:11:15 +0100 | [diff] [blame] | 146 | static int do_fsync(unsigned int fd, int datasync) | 
| David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 147 | { | 
 | 148 | 	struct file *file; | 
 | 149 | 	int ret = -EBADF; | 
 | 150 |  | 
 | 151 | 	file = fget(fd); | 
 | 152 | 	if (file) { | 
| Christoph Hellwig | 4c728ef | 2008-12-22 21:11:15 +0100 | [diff] [blame] | 153 | 		ret = vfs_fsync(file, file->f_path.dentry, datasync); | 
| David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 154 | 		fput(file); | 
 | 155 | 	} | 
 | 156 | 	return ret; | 
 | 157 | } | 
 | 158 |  | 
| Heiko Carstens | a5f8fa9 | 2009-01-14 14:14:11 +0100 | [diff] [blame] | 159 | SYSCALL_DEFINE1(fsync, unsigned int, fd) | 
| David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 160 | { | 
| Christoph Hellwig | 4c728ef | 2008-12-22 21:11:15 +0100 | [diff] [blame] | 161 | 	return do_fsync(fd, 0); | 
| David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 162 | } | 
 | 163 |  | 
| Heiko Carstens | a5f8fa9 | 2009-01-14 14:14:11 +0100 | [diff] [blame] | 164 | SYSCALL_DEFINE1(fdatasync, unsigned int, fd) | 
| David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 165 | { | 
| Christoph Hellwig | 4c728ef | 2008-12-22 21:11:15 +0100 | [diff] [blame] | 166 | 	return do_fsync(fd, 1); | 
| David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 167 | } | 
 | 168 |  | 
 | 169 | /* | 
| Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 170 |  * sys_sync_file_range() permits finely controlled syncing over a segment of | 
 | 171 |  * a file in the range offset .. (offset+nbytes-1) inclusive.  If nbytes is | 
 | 172 |  * zero then sys_sync_file_range() will operate from offset out to EOF. | 
 | 173 |  * | 
 | 174 |  * The flag bits are: | 
 | 175 |  * | 
 | 176 |  * SYNC_FILE_RANGE_WAIT_BEFORE: wait upon writeout of all pages in the range | 
 | 177 |  * before performing the write. | 
 | 178 |  * | 
 | 179 |  * 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] | 180 |  * range which are not presently under writeback. Note that this may block for | 
 | 181 |  * significant periods due to exhaustion of disk request structures. | 
| Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 182 |  * | 
 | 183 |  * SYNC_FILE_RANGE_WAIT_AFTER: wait upon writeout of all pages in the range | 
 | 184 |  * after performing the write. | 
 | 185 |  * | 
 | 186 |  * Useful combinations of the flag bits are: | 
 | 187 |  * | 
 | 188 |  * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE: ensures that all pages | 
 | 189 |  * in the range which were dirty on entry to sys_sync_file_range() are placed | 
 | 190 |  * under writeout.  This is a start-write-for-data-integrity operation. | 
 | 191 |  * | 
 | 192 |  * SYNC_FILE_RANGE_WRITE: start writeout of all dirty pages in the range which | 
 | 193 |  * are not presently under writeout.  This is an asynchronous flush-to-disk | 
 | 194 |  * operation.  Not suitable for data integrity operations. | 
 | 195 |  * | 
 | 196 |  * SYNC_FILE_RANGE_WAIT_BEFORE (or SYNC_FILE_RANGE_WAIT_AFTER): wait for | 
 | 197 |  * completion of writeout of all pages in the range.  This will be used after an | 
 | 198 |  * earlier SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE operation to wait | 
 | 199 |  * for that operation to complete and to return the result. | 
 | 200 |  * | 
 | 201 |  * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE|SYNC_FILE_RANGE_WAIT_AFTER: | 
 | 202 |  * a traditional sync() operation.  This is a write-for-data-integrity operation | 
 | 203 |  * which will ensure that all pages in the range which were dirty on entry to | 
 | 204 |  * sys_sync_file_range() are committed to disk. | 
 | 205 |  * | 
 | 206 |  * | 
 | 207 |  * SYNC_FILE_RANGE_WAIT_BEFORE and SYNC_FILE_RANGE_WAIT_AFTER will detect any | 
 | 208 |  * I/O errors or ENOSPC conditions and will return those to the caller, after | 
 | 209 |  * clearing the EIO and ENOSPC flags in the address_space. | 
 | 210 |  * | 
 | 211 |  * It should be noted that none of these operations write out the file's | 
 | 212 |  * metadata.  So unless the application is strictly performing overwrites of | 
 | 213 |  * already-instantiated disk blocks, there are no guarantees here that the data | 
 | 214 |  * will be available after a crash. | 
 | 215 |  */ | 
| Heiko Carstens | 6673e0c | 2009-01-14 14:14:02 +0100 | [diff] [blame] | 216 | SYSCALL_DEFINE(sync_file_range)(int fd, loff_t offset, loff_t nbytes, | 
 | 217 | 				unsigned int flags) | 
| Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 218 | { | 
 | 219 | 	int ret; | 
 | 220 | 	struct file *file; | 
 | 221 | 	loff_t endbyte;			/* inclusive */ | 
 | 222 | 	int fput_needed; | 
 | 223 | 	umode_t i_mode; | 
 | 224 |  | 
 | 225 | 	ret = -EINVAL; | 
 | 226 | 	if (flags & ~VALID_FLAGS) | 
 | 227 | 		goto out; | 
 | 228 |  | 
 | 229 | 	endbyte = offset + nbytes; | 
 | 230 |  | 
 | 231 | 	if ((s64)offset < 0) | 
 | 232 | 		goto out; | 
 | 233 | 	if ((s64)endbyte < 0) | 
 | 234 | 		goto out; | 
 | 235 | 	if (endbyte < offset) | 
 | 236 | 		goto out; | 
 | 237 |  | 
 | 238 | 	if (sizeof(pgoff_t) == 4) { | 
 | 239 | 		if (offset >= (0x100000000ULL << PAGE_CACHE_SHIFT)) { | 
 | 240 | 			/* | 
 | 241 | 			 * The range starts outside a 32 bit machine's | 
 | 242 | 			 * pagecache addressing capabilities.  Let it "succeed" | 
 | 243 | 			 */ | 
 | 244 | 			ret = 0; | 
 | 245 | 			goto out; | 
 | 246 | 		} | 
 | 247 | 		if (endbyte >= (0x100000000ULL << PAGE_CACHE_SHIFT)) { | 
 | 248 | 			/* | 
 | 249 | 			 * Out to EOF | 
 | 250 | 			 */ | 
 | 251 | 			nbytes = 0; | 
 | 252 | 		} | 
 | 253 | 	} | 
 | 254 |  | 
 | 255 | 	if (nbytes == 0) | 
| OGAWA Hirofumi | 111ebb6 | 2006-06-23 02:03:26 -0700 | [diff] [blame] | 256 | 		endbyte = LLONG_MAX; | 
| Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 257 | 	else | 
 | 258 | 		endbyte--;		/* inclusive */ | 
 | 259 |  | 
 | 260 | 	ret = -EBADF; | 
 | 261 | 	file = fget_light(fd, &fput_needed); | 
 | 262 | 	if (!file) | 
 | 263 | 		goto out; | 
 | 264 |  | 
| Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 265 | 	i_mode = file->f_path.dentry->d_inode->i_mode; | 
| Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 266 | 	ret = -ESPIPE; | 
 | 267 | 	if (!S_ISREG(i_mode) && !S_ISBLK(i_mode) && !S_ISDIR(i_mode) && | 
 | 268 | 			!S_ISLNK(i_mode)) | 
 | 269 | 		goto out_put; | 
 | 270 |  | 
| Mark Fasheh | ef51c97 | 2007-05-08 00:27:10 -0700 | [diff] [blame] | 271 | 	ret = do_sync_mapping_range(file->f_mapping, offset, endbyte, flags); | 
| Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 272 | out_put: | 
 | 273 | 	fput_light(file, fput_needed); | 
 | 274 | out: | 
 | 275 | 	return ret; | 
 | 276 | } | 
| Heiko Carstens | 6673e0c | 2009-01-14 14:14:02 +0100 | [diff] [blame] | 277 | #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS | 
 | 278 | asmlinkage long SyS_sync_file_range(long fd, loff_t offset, loff_t nbytes, | 
 | 279 | 				    long flags) | 
 | 280 | { | 
 | 281 | 	return SYSC_sync_file_range((int) fd, offset, nbytes, | 
 | 282 | 				    (unsigned int) flags); | 
 | 283 | } | 
 | 284 | SYSCALL_ALIAS(sys_sync_file_range, SyS_sync_file_range); | 
 | 285 | #endif | 
| Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 286 |  | 
| David Woodhouse | edd5cd4 | 2007-06-27 14:10:09 -0700 | [diff] [blame] | 287 | /* It would be nice if people remember that not all the world's an i386 | 
 | 288 |    when they introduce new system calls */ | 
| Heiko Carstens | 6673e0c | 2009-01-14 14:14:02 +0100 | [diff] [blame] | 289 | SYSCALL_DEFINE(sync_file_range2)(int fd, unsigned int flags, | 
 | 290 | 				 loff_t offset, loff_t nbytes) | 
| David Woodhouse | edd5cd4 | 2007-06-27 14:10:09 -0700 | [diff] [blame] | 291 | { | 
 | 292 | 	return sys_sync_file_range(fd, offset, nbytes, flags); | 
 | 293 | } | 
| Heiko Carstens | 6673e0c | 2009-01-14 14:14:02 +0100 | [diff] [blame] | 294 | #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS | 
 | 295 | asmlinkage long SyS_sync_file_range2(long fd, long flags, | 
 | 296 | 				     loff_t offset, loff_t nbytes) | 
 | 297 | { | 
 | 298 | 	return SYSC_sync_file_range2((int) fd, (unsigned int) flags, | 
 | 299 | 				     offset, nbytes); | 
 | 300 | } | 
 | 301 | SYSCALL_ALIAS(sys_sync_file_range2, SyS_sync_file_range2); | 
 | 302 | #endif | 
| David Woodhouse | edd5cd4 | 2007-06-27 14:10:09 -0700 | [diff] [blame] | 303 |  | 
| Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 304 | /* | 
 | 305 |  * `endbyte' is inclusive | 
 | 306 |  */ | 
| Mark Fasheh | 5b04aa3 | 2007-03-01 11:01:55 -0800 | [diff] [blame] | 307 | int do_sync_mapping_range(struct address_space *mapping, loff_t offset, | 
 | 308 | 			  loff_t endbyte, unsigned int flags) | 
| Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 309 | { | 
 | 310 | 	int ret; | 
| Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 311 |  | 
| Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 312 | 	if (!mapping) { | 
 | 313 | 		ret = -EINVAL; | 
 | 314 | 		goto out; | 
 | 315 | 	} | 
 | 316 |  | 
 | 317 | 	ret = 0; | 
 | 318 | 	if (flags & SYNC_FILE_RANGE_WAIT_BEFORE) { | 
 | 319 | 		ret = wait_on_page_writeback_range(mapping, | 
 | 320 | 					offset >> PAGE_CACHE_SHIFT, | 
 | 321 | 					endbyte >> PAGE_CACHE_SHIFT); | 
 | 322 | 		if (ret < 0) | 
 | 323 | 			goto out; | 
 | 324 | 	} | 
 | 325 |  | 
 | 326 | 	if (flags & SYNC_FILE_RANGE_WRITE) { | 
 | 327 | 		ret = __filemap_fdatawrite_range(mapping, offset, endbyte, | 
| Nick Piggin | ee53a89 | 2009-01-06 14:39:12 -0800 | [diff] [blame] | 328 | 						WB_SYNC_ALL); | 
| Andrew Morton | f79e2ab | 2006-03-31 02:30:42 -0800 | [diff] [blame] | 329 | 		if (ret < 0) | 
 | 330 | 			goto out; | 
 | 331 | 	} | 
 | 332 |  | 
 | 333 | 	if (flags & SYNC_FILE_RANGE_WAIT_AFTER) { | 
 | 334 | 		ret = wait_on_page_writeback_range(mapping, | 
 | 335 | 					offset >> PAGE_CACHE_SHIFT, | 
 | 336 | 					endbyte >> PAGE_CACHE_SHIFT); | 
 | 337 | 	} | 
 | 338 | out: | 
 | 339 | 	return ret; | 
 | 340 | } | 
| Mark Fasheh | 5b04aa3 | 2007-03-01 11:01:55 -0800 | [diff] [blame] | 341 | EXPORT_SYMBOL_GPL(do_sync_mapping_range); |