1 // SPDX-License-Identifier: GPL-2.0 << 2 /* 1 /* 3 * linux/fs/read_write.c 2 * linux/fs/read_write.c 4 * 3 * 5 * Copyright (C) 1991, 1992 Linus Torvalds 4 * Copyright (C) 1991, 1992 Linus Torvalds 6 */ 5 */ 7 6 8 #include <linux/slab.h> !! 7 #include <linux/slab.h> 9 #include <linux/stat.h> 8 #include <linux/stat.h> 10 #include <linux/sched/xacct.h> << 11 #include <linux/fcntl.h> 9 #include <linux/fcntl.h> 12 #include <linux/file.h> 10 #include <linux/file.h> 13 #include <linux/uio.h> 11 #include <linux/uio.h> 14 #include <linux/fsnotify.h> 12 #include <linux/fsnotify.h> 15 #include <linux/security.h> 13 #include <linux/security.h> 16 #include <linux/export.h> 14 #include <linux/export.h> 17 #include <linux/syscalls.h> 15 #include <linux/syscalls.h> 18 #include <linux/pagemap.h> 16 #include <linux/pagemap.h> 19 #include <linux/splice.h> 17 #include <linux/splice.h> 20 #include <linux/compat.h> 18 #include <linux/compat.h> 21 #include <linux/mount.h> 19 #include <linux/mount.h> 22 #include <linux/fs.h> 20 #include <linux/fs.h> 23 #include "internal.h" 21 #include "internal.h" 24 22 25 #include <linux/uaccess.h> 23 #include <linux/uaccess.h> 26 #include <asm/unistd.h> 24 #include <asm/unistd.h> 27 25 >> 26 typedef ssize_t (*io_fn_t)(struct file *, char __user *, size_t, loff_t *); >> 27 typedef ssize_t (*iter_fn_t)(struct kiocb *, struct iov_iter *); >> 28 28 const struct file_operations generic_ro_fops = 29 const struct file_operations generic_ro_fops = { 29 .llseek = generic_file_llseek, 30 .llseek = generic_file_llseek, 30 .read_iter = generic_file_read_it 31 .read_iter = generic_file_read_iter, 31 .mmap = generic_file_readonl 32 .mmap = generic_file_readonly_mmap, 32 .splice_read = filemap_splice_read, !! 33 .splice_read = generic_file_splice_read, 33 }; 34 }; 34 35 35 EXPORT_SYMBOL(generic_ro_fops); 36 EXPORT_SYMBOL(generic_ro_fops); 36 37 37 static inline bool unsigned_offsets(struct fil !! 38 static inline int unsigned_offsets(struct file *file) 38 { 39 { 39 return file->f_op->fop_flags & FOP_UNS !! 40 return file->f_mode & FMODE_UNSIGNED_OFFSET; 40 } 41 } 41 42 42 /** 43 /** 43 * vfs_setpos_cookie - update the file offset !! 44 * vfs_setpos - update the file offset for lseek 44 * @file: file structure in question 45 * @file: file structure in question 45 * @offset: file offset to seek to 46 * @offset: file offset to seek to 46 * @maxsize: maximum file size 47 * @maxsize: maximum file size 47 * @cookie: cookie to reset << 48 * 48 * 49 * Update the file offset to the value specifi !! 49 * This is a low-level filesystem helper for updating the file offset to 50 * offset is valid and it is not equal to the !! 50 * the value specified by @offset if the given offset is valid and it is 51 * reset the specified cookie to indicate that !! 51 * not equal to the current file offset. 52 * 52 * 53 * Return the specified offset on success and 53 * Return the specified offset on success and -EINVAL on invalid offset. 54 */ 54 */ 55 static loff_t vfs_setpos_cookie(struct file *f !! 55 loff_t vfs_setpos(struct file *file, loff_t offset, loff_t maxsize) 56 loff_t maxsize << 57 { 56 { 58 if (offset < 0 && !unsigned_offsets(fi 57 if (offset < 0 && !unsigned_offsets(file)) 59 return -EINVAL; 58 return -EINVAL; 60 if (offset > maxsize) 59 if (offset > maxsize) 61 return -EINVAL; 60 return -EINVAL; 62 61 63 if (offset != file->f_pos) { 62 if (offset != file->f_pos) { 64 file->f_pos = offset; 63 file->f_pos = offset; 65 if (cookie) !! 64 file->f_version = 0; 66 *cookie = 0; << 67 } 65 } 68 return offset; 66 return offset; 69 } 67 } 70 << 71 /** << 72 * vfs_setpos - update the file offset for lse << 73 * @file: file structure in question << 74 * @offset: file offset to seek to << 75 * @maxsize: maximum file size << 76 * << 77 * This is a low-level filesystem helper for u << 78 * the value specified by @offset if the given << 79 * not equal to the current file offset. << 80 * << 81 * Return the specified offset on success and << 82 */ << 83 loff_t vfs_setpos(struct file *file, loff_t of << 84 { << 85 return vfs_setpos_cookie(file, offset, << 86 } << 87 EXPORT_SYMBOL(vfs_setpos); 68 EXPORT_SYMBOL(vfs_setpos); 88 69 89 /** 70 /** 90 * must_set_pos - check whether f_pos has to b !! 71 * generic_file_llseek_size - generic llseek implementation for regular files 91 * @file: file to seek on !! 72 * @file: file structure to seek on 92 * @offset: offset to use !! 73 * @offset: file offset to seek to 93 * @whence: type of seek operation !! 74 * @whence: type of seek 94 * @eof: end of file !! 75 * @size: max size of this file in file system >> 76 * @eof: offset used for SEEK_END position 95 * 77 * 96 * Check whether f_pos needs to be updated and !! 78 * This is a variant of generic_file_llseek that allows passing in a custom 97 * to @whence. !! 79 * maximum file size and a custom EOF position, for e.g. hashed directories 98 * 80 * 99 * Return: 0 if f_pos doesn't need to be updat !! 81 * Synchronization: 100 * updated, and negative error code on failure !! 82 * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms) >> 83 * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes. >> 84 * read/writes behave like SEEK_SET against seeks. 101 */ 85 */ 102 static int must_set_pos(struct file *file, lof !! 86 loff_t >> 87 generic_file_llseek_size(struct file *file, loff_t offset, int whence, >> 88 loff_t maxsize, loff_t eof) 103 { 89 { 104 switch (whence) { 90 switch (whence) { 105 case SEEK_END: 91 case SEEK_END: 106 *offset += eof; !! 92 offset += eof; 107 break; 93 break; 108 case SEEK_CUR: 94 case SEEK_CUR: 109 /* 95 /* 110 * Here we special-case the ls 96 * Here we special-case the lseek(fd, 0, SEEK_CUR) 111 * position-querying operation 97 * position-querying operation. Avoid rewriting the "same" 112 * f_pos value back to the fil 98 * f_pos value back to the file because a concurrent read(), 113 * write() or lseek() might ha 99 * write() or lseek() might have altered it 114 */ 100 */ 115 if (*offset == 0) { !! 101 if (offset == 0) 116 *offset = file->f_pos; !! 102 return file->f_pos; 117 return 0; !! 103 /* 118 } !! 104 * f_lock protects against read/modify/write race with other 119 break; !! 105 * SEEK_CURs. Note that parallel writes and reads behave >> 106 * like SEEK_SET. >> 107 */ >> 108 spin_lock(&file->f_lock); >> 109 offset = vfs_setpos(file, file->f_pos + offset, maxsize); >> 110 spin_unlock(&file->f_lock); >> 111 return offset; 120 case SEEK_DATA: 112 case SEEK_DATA: 121 /* 113 /* 122 * In the generic case the ent 114 * In the generic case the entire file is data, so as long as 123 * offset isn't at the end of 115 * offset isn't at the end of the file then the offset is data. 124 */ 116 */ 125 if ((unsigned long long)*offse !! 117 if (offset >= eof) 126 return -ENXIO; 118 return -ENXIO; 127 break; 119 break; 128 case SEEK_HOLE: 120 case SEEK_HOLE: 129 /* 121 /* 130 * There is a virtual hole at 122 * There is a virtual hole at the end of the file, so as long as 131 * offset isn't i_size or larg 123 * offset isn't i_size or larger, return i_size. 132 */ 124 */ 133 if ((unsigned long long)*offse !! 125 if (offset >= eof) 134 return -ENXIO; 126 return -ENXIO; 135 *offset = eof; !! 127 offset = eof; 136 break; 128 break; 137 } 129 } 138 130 139 return 1; << 140 } << 141 << 142 /** << 143 * generic_file_llseek_size - generic llseek i << 144 * @file: file structure to seek on << 145 * @offset: file offset to seek to << 146 * @whence: type of seek << 147 * @maxsize: max size of this file in file << 148 * @eof: offset used for SEEK_END posit << 149 * << 150 * This is a variant of generic_file_llseek th << 151 * maximum file size and a custom EOF position << 152 * << 153 * Synchronization: << 154 * SEEK_SET and SEEK_END are unsynchronized (b << 155 * SEEK_CUR is synchronized against other SEEK << 156 * read/writes behave like SEEK_SET against se << 157 */ << 158 loff_t << 159 generic_file_llseek_size(struct file *file, lo << 160 loff_t maxsize, loff_t eof) << 161 { << 162 int ret; << 163 << 164 ret = must_set_pos(file, &offset, when << 165 if (ret < 0) << 166 return ret; << 167 if (ret == 0) << 168 return offset; << 169 << 170 if (whence == SEEK_CUR) { << 171 /* << 172 * f_lock protects against rea << 173 * other SEEK_CURs. Note that << 174 * behave like SEEK_SET. << 175 */ << 176 guard(spinlock)(&file->f_lock) << 177 return vfs_setpos(file, file-> << 178 } << 179 << 180 return vfs_setpos(file, offset, maxsiz 131 return vfs_setpos(file, offset, maxsize); 181 } 132 } 182 EXPORT_SYMBOL(generic_file_llseek_size); 133 EXPORT_SYMBOL(generic_file_llseek_size); 183 134 184 /** 135 /** 185 * generic_llseek_cookie - versioned llseek im << 186 * @file: file structure to seek on << 187 * @offset: file offset to seek to << 188 * @whence: type of seek << 189 * @cookie: cookie to update << 190 * << 191 * See generic_file_llseek for a general descr << 192 * << 193 * In contrast to generic_file_llseek, this fu << 194 * specified cookie to indicate a seek took pl << 195 */ << 196 loff_t generic_llseek_cookie(struct file *file << 197 u64 *cookie) << 198 { << 199 struct inode *inode = file->f_mapping- << 200 loff_t maxsize = inode->i_sb->s_maxbyt << 201 loff_t eof = i_size_read(inode); << 202 int ret; << 203 << 204 if (WARN_ON_ONCE(!cookie)) << 205 return -EINVAL; << 206 << 207 /* << 208 * Require that this is only used for << 209 * synchronization between readdir and << 210 * @cookie is correctly synchronized w << 211 */ << 212 if (WARN_ON_ONCE(!(file->f_mode & FMOD << 213 return -EINVAL; << 214 << 215 ret = must_set_pos(file, &offset, when << 216 if (ret < 0) << 217 return ret; << 218 if (ret == 0) << 219 return offset; << 220 << 221 /* No need to hold f_lock because we k << 222 if (whence == SEEK_CUR) << 223 return vfs_setpos_cookie(file, << 224 << 225 return vfs_setpos_cookie(file, offset, << 226 } << 227 EXPORT_SYMBOL(generic_llseek_cookie); << 228 << 229 /** << 230 * generic_file_llseek - generic llseek implem 136 * generic_file_llseek - generic llseek implementation for regular files 231 * @file: file structure to seek on 137 * @file: file structure to seek on 232 * @offset: file offset to seek to 138 * @offset: file offset to seek to 233 * @whence: type of seek 139 * @whence: type of seek 234 * 140 * 235 * This is a generic implemenation of ->llseek 141 * This is a generic implemenation of ->llseek useable for all normal local 236 * filesystems. It just updates the file offs 142 * filesystems. It just updates the file offset to the value specified by 237 * @offset and @whence. 143 * @offset and @whence. 238 */ 144 */ 239 loff_t generic_file_llseek(struct file *file, 145 loff_t generic_file_llseek(struct file *file, loff_t offset, int whence) 240 { 146 { 241 struct inode *inode = file->f_mapping- 147 struct inode *inode = file->f_mapping->host; 242 148 243 return generic_file_llseek_size(file, 149 return generic_file_llseek_size(file, offset, whence, 244 inode- 150 inode->i_sb->s_maxbytes, 245 i_size 151 i_size_read(inode)); 246 } 152 } 247 EXPORT_SYMBOL(generic_file_llseek); 153 EXPORT_SYMBOL(generic_file_llseek); 248 154 249 /** 155 /** 250 * fixed_size_llseek - llseek implementation f 156 * fixed_size_llseek - llseek implementation for fixed-sized devices 251 * @file: file structure to seek on 157 * @file: file structure to seek on 252 * @offset: file offset to seek to 158 * @offset: file offset to seek to 253 * @whence: type of seek 159 * @whence: type of seek 254 * @size: size of the file 160 * @size: size of the file 255 * 161 * 256 */ 162 */ 257 loff_t fixed_size_llseek(struct file *file, lo 163 loff_t fixed_size_llseek(struct file *file, loff_t offset, int whence, loff_t size) 258 { 164 { 259 switch (whence) { 165 switch (whence) { 260 case SEEK_SET: case SEEK_CUR: case SEE 166 case SEEK_SET: case SEEK_CUR: case SEEK_END: 261 return generic_file_llseek_siz 167 return generic_file_llseek_size(file, offset, whence, 262 168 size, size); 263 default: 169 default: 264 return -EINVAL; 170 return -EINVAL; 265 } 171 } 266 } 172 } 267 EXPORT_SYMBOL(fixed_size_llseek); 173 EXPORT_SYMBOL(fixed_size_llseek); 268 174 269 /** 175 /** 270 * no_seek_end_llseek - llseek implementation 176 * no_seek_end_llseek - llseek implementation for fixed-sized devices 271 * @file: file structure to seek on 177 * @file: file structure to seek on 272 * @offset: file offset to seek to 178 * @offset: file offset to seek to 273 * @whence: type of seek 179 * @whence: type of seek 274 * 180 * 275 */ 181 */ 276 loff_t no_seek_end_llseek(struct file *file, l 182 loff_t no_seek_end_llseek(struct file *file, loff_t offset, int whence) 277 { 183 { 278 switch (whence) { 184 switch (whence) { 279 case SEEK_SET: case SEEK_CUR: 185 case SEEK_SET: case SEEK_CUR: 280 return generic_file_llseek_siz 186 return generic_file_llseek_size(file, offset, whence, 281 187 OFFSET_MAX, 0); 282 default: 188 default: 283 return -EINVAL; 189 return -EINVAL; 284 } 190 } 285 } 191 } 286 EXPORT_SYMBOL(no_seek_end_llseek); 192 EXPORT_SYMBOL(no_seek_end_llseek); 287 193 288 /** 194 /** 289 * no_seek_end_llseek_size - llseek implementa 195 * no_seek_end_llseek_size - llseek implementation for fixed-sized devices 290 * @file: file structure to seek on 196 * @file: file structure to seek on 291 * @offset: file offset to seek to 197 * @offset: file offset to seek to 292 * @whence: type of seek 198 * @whence: type of seek 293 * @size: maximal offset allowed 199 * @size: maximal offset allowed 294 * 200 * 295 */ 201 */ 296 loff_t no_seek_end_llseek_size(struct file *fi 202 loff_t no_seek_end_llseek_size(struct file *file, loff_t offset, int whence, loff_t size) 297 { 203 { 298 switch (whence) { 204 switch (whence) { 299 case SEEK_SET: case SEEK_CUR: 205 case SEEK_SET: case SEEK_CUR: 300 return generic_file_llseek_siz 206 return generic_file_llseek_size(file, offset, whence, 301 207 size, 0); 302 default: 208 default: 303 return -EINVAL; 209 return -EINVAL; 304 } 210 } 305 } 211 } 306 EXPORT_SYMBOL(no_seek_end_llseek_size); 212 EXPORT_SYMBOL(no_seek_end_llseek_size); 307 213 308 /** 214 /** 309 * noop_llseek - No Operation Performed llseek 215 * noop_llseek - No Operation Performed llseek implementation 310 * @file: file structure to seek on 216 * @file: file structure to seek on 311 * @offset: file offset to seek to 217 * @offset: file offset to seek to 312 * @whence: type of seek 218 * @whence: type of seek 313 * 219 * 314 * This is an implementation of ->llseek useab 220 * This is an implementation of ->llseek useable for the rare special case when 315 * userspace expects the seek to succeed but t 221 * userspace expects the seek to succeed but the (device) file is actually not 316 * able to perform the seek. In this case you 222 * able to perform the seek. In this case you use noop_llseek() instead of 317 * falling back to the default implementation 223 * falling back to the default implementation of ->llseek. 318 */ 224 */ 319 loff_t noop_llseek(struct file *file, loff_t o 225 loff_t noop_llseek(struct file *file, loff_t offset, int whence) 320 { 226 { 321 return file->f_pos; 227 return file->f_pos; 322 } 228 } 323 EXPORT_SYMBOL(noop_llseek); 229 EXPORT_SYMBOL(noop_llseek); 324 230 >> 231 loff_t no_llseek(struct file *file, loff_t offset, int whence) >> 232 { >> 233 return -ESPIPE; >> 234 } >> 235 EXPORT_SYMBOL(no_llseek); >> 236 325 loff_t default_llseek(struct file *file, loff_ 237 loff_t default_llseek(struct file *file, loff_t offset, int whence) 326 { 238 { 327 struct inode *inode = file_inode(file) 239 struct inode *inode = file_inode(file); 328 loff_t retval; 240 loff_t retval; 329 241 330 inode_lock(inode); 242 inode_lock(inode); 331 switch (whence) { 243 switch (whence) { 332 case SEEK_END: 244 case SEEK_END: 333 offset += i_size_read( 245 offset += i_size_read(inode); 334 break; 246 break; 335 case SEEK_CUR: 247 case SEEK_CUR: 336 if (offset == 0) { 248 if (offset == 0) { 337 retval = file- 249 retval = file->f_pos; 338 goto out; 250 goto out; 339 } 251 } 340 offset += file->f_pos; 252 offset += file->f_pos; 341 break; 253 break; 342 case SEEK_DATA: 254 case SEEK_DATA: 343 /* 255 /* 344 * In the generic case 256 * In the generic case the entire file is data, so as 345 * long as offset isn' 257 * long as offset isn't at the end of the file then the 346 * offset is data. 258 * offset is data. 347 */ 259 */ 348 if (offset >= inode->i 260 if (offset >= inode->i_size) { 349 retval = -ENXI 261 retval = -ENXIO; 350 goto out; 262 goto out; 351 } 263 } 352 break; 264 break; 353 case SEEK_HOLE: 265 case SEEK_HOLE: 354 /* 266 /* 355 * There is a virtual 267 * There is a virtual hole at the end of the file, so 356 * as long as offset i 268 * as long as offset isn't i_size or larger, return 357 * i_size. 269 * i_size. 358 */ 270 */ 359 if (offset >= inode->i 271 if (offset >= inode->i_size) { 360 retval = -ENXI 272 retval = -ENXIO; 361 goto out; 273 goto out; 362 } 274 } 363 offset = inode->i_size 275 offset = inode->i_size; 364 break; 276 break; 365 } 277 } 366 retval = -EINVAL; 278 retval = -EINVAL; 367 if (offset >= 0 || unsigned_offsets(fi 279 if (offset >= 0 || unsigned_offsets(file)) { 368 if (offset != file->f_pos) !! 280 if (offset != file->f_pos) { 369 file->f_pos = offset; 281 file->f_pos = offset; >> 282 file->f_version = 0; >> 283 } 370 retval = offset; 284 retval = offset; 371 } 285 } 372 out: 286 out: 373 inode_unlock(inode); 287 inode_unlock(inode); 374 return retval; 288 return retval; 375 } 289 } 376 EXPORT_SYMBOL(default_llseek); 290 EXPORT_SYMBOL(default_llseek); 377 291 378 loff_t vfs_llseek(struct file *file, loff_t of 292 loff_t vfs_llseek(struct file *file, loff_t offset, int whence) 379 { 293 { 380 if (!(file->f_mode & FMODE_LSEEK)) !! 294 loff_t (*fn)(struct file *, loff_t, int); 381 return -ESPIPE; !! 295 382 return file->f_op->llseek(file, offset !! 296 fn = no_llseek; >> 297 if (file->f_mode & FMODE_LSEEK) { >> 298 if (file->f_op->llseek) >> 299 fn = file->f_op->llseek; >> 300 } >> 301 return fn(file, offset, whence); 383 } 302 } 384 EXPORT_SYMBOL(vfs_llseek); 303 EXPORT_SYMBOL(vfs_llseek); 385 304 386 static off_t ksys_lseek(unsigned int fd, off_t !! 305 SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence) 387 { 306 { 388 off_t retval; 307 off_t retval; 389 struct fd f = fdget_pos(fd); 308 struct fd f = fdget_pos(fd); 390 if (!fd_file(f)) !! 309 if (!f.file) 391 return -EBADF; 310 return -EBADF; 392 311 393 retval = -EINVAL; 312 retval = -EINVAL; 394 if (whence <= SEEK_MAX) { 313 if (whence <= SEEK_MAX) { 395 loff_t res = vfs_llseek(fd_fil !! 314 loff_t res = vfs_llseek(f.file, offset, whence); 396 retval = res; 315 retval = res; 397 if (res != (loff_t)retval) 316 if (res != (loff_t)retval) 398 retval = -EOVERFLOW; 317 retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */ 399 } 318 } 400 fdput_pos(f); 319 fdput_pos(f); 401 return retval; 320 return retval; 402 } 321 } 403 322 404 SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t << 405 { << 406 return ksys_lseek(fd, offset, whence); << 407 } << 408 << 409 #ifdef CONFIG_COMPAT 323 #ifdef CONFIG_COMPAT 410 COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd 324 COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence) 411 { 325 { 412 return ksys_lseek(fd, offset, whence); !! 326 return sys_lseek(fd, offset, whence); 413 } 327 } 414 #endif 328 #endif 415 329 416 #if !defined(CONFIG_64BIT) || defined(CONFIG_C !! 330 #ifdef __ARCH_WANT_SYS_LLSEEK 417 defined(__ARCH_WANT_SYS_LLSEEK) << 418 SYSCALL_DEFINE5(llseek, unsigned int, fd, unsi 331 SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high, 419 unsigned long, offset_low, lof 332 unsigned long, offset_low, loff_t __user *, result, 420 unsigned int, whence) 333 unsigned int, whence) 421 { 334 { 422 int retval; 335 int retval; 423 struct fd f = fdget_pos(fd); 336 struct fd f = fdget_pos(fd); 424 loff_t offset; 337 loff_t offset; 425 338 426 if (!fd_file(f)) !! 339 if (!f.file) 427 return -EBADF; 340 return -EBADF; 428 341 429 retval = -EINVAL; 342 retval = -EINVAL; 430 if (whence > SEEK_MAX) 343 if (whence > SEEK_MAX) 431 goto out_putf; 344 goto out_putf; 432 345 433 offset = vfs_llseek(fd_file(f), ((loff !! 346 offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low, 434 whence); 347 whence); 435 348 436 retval = (int)offset; 349 retval = (int)offset; 437 if (offset >= 0) { 350 if (offset >= 0) { 438 retval = -EFAULT; 351 retval = -EFAULT; 439 if (!copy_to_user(result, &off 352 if (!copy_to_user(result, &offset, sizeof(offset))) 440 retval = 0; 353 retval = 0; 441 } 354 } 442 out_putf: 355 out_putf: 443 fdput_pos(f); 356 fdput_pos(f); 444 return retval; 357 return retval; 445 } 358 } 446 #endif 359 #endif 447 360 448 int rw_verify_area(int read_write, struct file !! 361 ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos) 449 { 362 { 450 int mask = read_write == READ ? MAY_RE !! 363 struct kiocb kiocb; 451 int ret; !! 364 ssize_t ret; 452 365 453 if (unlikely((ssize_t) count < 0)) !! 366 if (!file->f_op->read_iter) 454 return -EINVAL; 367 return -EINVAL; 455 368 456 if (ppos) { !! 369 init_sync_kiocb(&kiocb, file); 457 loff_t pos = *ppos; !! 370 kiocb.ki_pos = *ppos; 458 << 459 if (unlikely(pos < 0)) { << 460 if (!unsigned_offsets( << 461 return -EINVAL << 462 if (count >= -pos) /* << 463 return -EOVERF << 464 } else if (unlikely((loff_t) ( << 465 if (!unsigned_offsets( << 466 return -EINVAL << 467 } << 468 } << 469 << 470 ret = security_file_permission(file, m << 471 if (ret) << 472 return ret; << 473 371 474 return fsnotify_file_area_perm(file, m !! 372 iter->type |= READ; >> 373 ret = file->f_op->read_iter(&kiocb, iter); >> 374 BUG_ON(ret == -EIOCBQUEUED); >> 375 if (ret > 0) >> 376 *ppos = kiocb.ki_pos; >> 377 return ret; 475 } 378 } 476 EXPORT_SYMBOL(rw_verify_area); !! 379 EXPORT_SYMBOL(vfs_iter_read); 477 380 478 static ssize_t new_sync_read(struct file *filp !! 381 ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos) 479 { 382 { 480 struct kiocb kiocb; 383 struct kiocb kiocb; 481 struct iov_iter iter; << 482 ssize_t ret; 384 ssize_t ret; 483 385 484 init_sync_kiocb(&kiocb, filp); !! 386 if (!file->f_op->write_iter) 485 kiocb.ki_pos = (ppos ? *ppos : 0); !! 387 return -EINVAL; 486 iov_iter_ubuf(&iter, ITER_DEST, buf, l << 487 388 488 ret = filp->f_op->read_iter(&kiocb, &i !! 389 init_sync_kiocb(&kiocb, file); >> 390 kiocb.ki_pos = *ppos; >> 391 >> 392 iter->type |= WRITE; >> 393 ret = file->f_op->write_iter(&kiocb, iter); 489 BUG_ON(ret == -EIOCBQUEUED); 394 BUG_ON(ret == -EIOCBQUEUED); 490 if (ppos) !! 395 if (ret > 0) 491 *ppos = kiocb.ki_pos; 396 *ppos = kiocb.ki_pos; 492 return ret; 397 return ret; 493 } 398 } >> 399 EXPORT_SYMBOL(vfs_iter_write); 494 400 495 static int warn_unsupported(struct file *file, !! 401 int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t count) 496 { 402 { 497 pr_warn_ratelimited( !! 403 struct inode *inode; 498 "kernel %s not supported for f !! 404 loff_t pos; 499 op, file, current->pid, curren !! 405 int retval = -EINVAL; 500 return -EINVAL; !! 406 >> 407 inode = file_inode(file); >> 408 if (unlikely((ssize_t) count < 0)) >> 409 return retval; >> 410 pos = *ppos; >> 411 if (unlikely(pos < 0)) { >> 412 if (!unsigned_offsets(file)) >> 413 return retval; >> 414 if (count >= -pos) /* both values are in 0..LLONG_MAX */ >> 415 return -EOVERFLOW; >> 416 } else if (unlikely((loff_t) (pos + count) < 0)) { >> 417 if (!unsigned_offsets(file)) >> 418 return retval; >> 419 } >> 420 >> 421 if (unlikely(inode->i_flctx && mandatory_lock(inode))) { >> 422 retval = locks_mandatory_area(inode, file, pos, pos + count - 1, >> 423 read_write == READ ? F_RDLCK : F_WRLCK); >> 424 if (retval < 0) >> 425 return retval; >> 426 } >> 427 return security_file_permission(file, >> 428 read_write == READ ? MAY_READ : MAY_WRITE); 501 } 429 } 502 430 503 ssize_t __kernel_read(struct file *file, void !! 431 static ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos) 504 { 432 { 505 struct kvec iov = { !! 433 struct iovec iov = { .iov_base = buf, .iov_len = len }; 506 .iov_base = buf, << 507 .iov_len = min_t(size_t << 508 }; << 509 struct kiocb kiocb; 434 struct kiocb kiocb; 510 struct iov_iter iter; 435 struct iov_iter iter; 511 ssize_t ret; 436 ssize_t ret; 512 437 513 if (WARN_ON_ONCE(!(file->f_mode & FMOD !! 438 init_sync_kiocb(&kiocb, filp); 514 return -EINVAL; !! 439 kiocb.ki_pos = *ppos; 515 if (!(file->f_mode & FMODE_CAN_READ)) !! 440 iov_iter_init(&iter, READ, &iov, 1, len); 516 return -EINVAL; << 517 /* << 518 * Also fail if ->read_iter and ->read << 519 * implies very convoluted semantics. << 520 */ << 521 if (unlikely(!file->f_op->read_iter || << 522 return warn_unsupported(file, << 523 441 524 init_sync_kiocb(&kiocb, file); !! 442 ret = filp->f_op->read_iter(&kiocb, &iter); 525 kiocb.ki_pos = pos ? *pos : 0; !! 443 BUG_ON(ret == -EIOCBQUEUED); 526 iov_iter_kvec(&iter, ITER_DEST, &iov, !! 444 *ppos = kiocb.ki_pos; 527 ret = file->f_op->read_iter(&kiocb, &i << 528 if (ret > 0) { << 529 if (pos) << 530 *pos = kiocb.ki_pos; << 531 fsnotify_access(file); << 532 add_rchar(current, ret); << 533 } << 534 inc_syscr(current); << 535 return ret; 445 return ret; 536 } 446 } 537 447 538 ssize_t kernel_read(struct file *file, void *b !! 448 ssize_t __vfs_read(struct file *file, char __user *buf, size_t count, >> 449 loff_t *pos) 539 { 450 { 540 ssize_t ret; !! 451 if (file->f_op->read) 541 !! 452 return file->f_op->read(file, buf, count, pos); 542 ret = rw_verify_area(READ, file, pos, !! 453 else if (file->f_op->read_iter) 543 if (ret) !! 454 return new_sync_read(file, buf, count, pos); 544 return ret; !! 455 else 545 return __kernel_read(file, buf, count, !! 456 return -EINVAL; 546 } 457 } 547 EXPORT_SYMBOL(kernel_read); !! 458 EXPORT_SYMBOL(__vfs_read); 548 459 549 ssize_t vfs_read(struct file *file, char __use 460 ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos) 550 { 461 { 551 ssize_t ret; 462 ssize_t ret; 552 463 553 if (!(file->f_mode & FMODE_READ)) 464 if (!(file->f_mode & FMODE_READ)) 554 return -EBADF; 465 return -EBADF; 555 if (!(file->f_mode & FMODE_CAN_READ)) 466 if (!(file->f_mode & FMODE_CAN_READ)) 556 return -EINVAL; 467 return -EINVAL; 557 if (unlikely(!access_ok(buf, count))) !! 468 if (unlikely(!access_ok(VERIFY_WRITE, buf, count))) 558 return -EFAULT; 469 return -EFAULT; 559 470 560 ret = rw_verify_area(READ, file, pos, 471 ret = rw_verify_area(READ, file, pos, count); 561 if (ret) !! 472 if (!ret) { 562 return ret; !! 473 if (count > MAX_RW_COUNT) 563 if (count > MAX_RW_COUNT) !! 474 count = MAX_RW_COUNT; 564 count = MAX_RW_COUNT; !! 475 ret = __vfs_read(file, buf, count, pos); 565 !! 476 if (ret > 0) { 566 if (file->f_op->read) !! 477 fsnotify_access(file); 567 ret = file->f_op->read(file, b !! 478 add_rchar(current, ret); 568 else if (file->f_op->read_iter) !! 479 } 569 ret = new_sync_read(file, buf, !! 480 inc_syscr(current); 570 else << 571 ret = -EINVAL; << 572 if (ret > 0) { << 573 fsnotify_access(file); << 574 add_rchar(current, ret); << 575 } 481 } 576 inc_syscr(current); !! 482 577 return ret; 483 return ret; 578 } 484 } 579 485 >> 486 EXPORT_SYMBOL(vfs_read); >> 487 580 static ssize_t new_sync_write(struct file *fil 488 static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos) 581 { 489 { >> 490 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len }; 582 struct kiocb kiocb; 491 struct kiocb kiocb; 583 struct iov_iter iter; 492 struct iov_iter iter; 584 ssize_t ret; 493 ssize_t ret; 585 494 586 init_sync_kiocb(&kiocb, filp); 495 init_sync_kiocb(&kiocb, filp); 587 kiocb.ki_pos = (ppos ? *ppos : 0); !! 496 kiocb.ki_pos = *ppos; 588 iov_iter_ubuf(&iter, ITER_SOURCE, (voi !! 497 iov_iter_init(&iter, WRITE, &iov, 1, len); 589 498 590 ret = filp->f_op->write_iter(&kiocb, & 499 ret = filp->f_op->write_iter(&kiocb, &iter); 591 BUG_ON(ret == -EIOCBQUEUED); 500 BUG_ON(ret == -EIOCBQUEUED); 592 if (ret > 0 && ppos) !! 501 if (ret > 0) 593 *ppos = kiocb.ki_pos; 502 *ppos = kiocb.ki_pos; 594 return ret; 503 return ret; 595 } 504 } 596 505 597 /* caller is responsible for file_start_write/ !! 506 ssize_t __vfs_write(struct file *file, const char __user *p, size_t count, 598 ssize_t __kernel_write_iter(struct file *file, !! 507 loff_t *pos) 599 { 508 { 600 struct kiocb kiocb; !! 509 if (file->f_op->write) >> 510 return file->f_op->write(file, p, count, pos); >> 511 else if (file->f_op->write_iter) >> 512 return new_sync_write(file, p, count, pos); >> 513 else >> 514 return -EINVAL; >> 515 } >> 516 EXPORT_SYMBOL(__vfs_write); >> 517 >> 518 ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos) >> 519 { >> 520 mm_segment_t old_fs; >> 521 const char __user *p; 601 ssize_t ret; 522 ssize_t ret; 602 523 603 if (WARN_ON_ONCE(!(file->f_mode & FMOD << 604 return -EBADF; << 605 if (!(file->f_mode & FMODE_CAN_WRITE)) 524 if (!(file->f_mode & FMODE_CAN_WRITE)) 606 return -EINVAL; 525 return -EINVAL; 607 /* << 608 * Also fail if ->write_iter and ->wri << 609 * implies very convoluted semantics. << 610 */ << 611 if (unlikely(!file->f_op->write_iter | << 612 return warn_unsupported(file, << 613 526 614 init_sync_kiocb(&kiocb, file); !! 527 old_fs = get_fs(); 615 kiocb.ki_pos = pos ? *pos : 0; !! 528 set_fs(get_ds()); 616 ret = file->f_op->write_iter(&kiocb, f !! 529 p = (__force const char __user *)buf; >> 530 if (count > MAX_RW_COUNT) >> 531 count = MAX_RW_COUNT; >> 532 ret = __vfs_write(file, p, count, pos); >> 533 set_fs(old_fs); 617 if (ret > 0) { 534 if (ret > 0) { 618 if (pos) << 619 *pos = kiocb.ki_pos; << 620 fsnotify_modify(file); 535 fsnotify_modify(file); 621 add_wchar(current, ret); 536 add_wchar(current, ret); 622 } 537 } 623 inc_syscw(current); 538 inc_syscw(current); 624 return ret; 539 return ret; 625 } 540 } 626 541 627 /* caller is responsible for file_start_write/ !! 542 EXPORT_SYMBOL(__kernel_write); 628 ssize_t __kernel_write(struct file *file, cons << 629 { << 630 struct kvec iov = { << 631 .iov_base = (void *)buf, << 632 .iov_len = min_t(size_t << 633 }; << 634 struct iov_iter iter; << 635 iov_iter_kvec(&iter, ITER_SOURCE, &iov << 636 return __kernel_write_iter(file, &iter << 637 } << 638 /* << 639 * This "EXPORT_SYMBOL_GPL()" is more of a "EX << 640 * but autofs is one of the few internal kerne << 641 * wants this _and_ can be built as a module. << 642 * this symbol for autofs, even though it real << 643 * for any other kernel modules. << 644 */ << 645 EXPORT_SYMBOL_GPL(__kernel_write); << 646 << 647 ssize_t kernel_write(struct file *file, const << 648 loff_t *pos) << 649 { << 650 ssize_t ret; << 651 << 652 ret = rw_verify_area(WRITE, file, pos, << 653 if (ret) << 654 return ret; << 655 << 656 file_start_write(file); << 657 ret = __kernel_write(file, buf, count << 658 file_end_write(file); << 659 return ret; << 660 } << 661 EXPORT_SYMBOL(kernel_write); << 662 543 663 ssize_t vfs_write(struct file *file, const cha 544 ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos) 664 { 545 { 665 ssize_t ret; 546 ssize_t ret; 666 547 667 if (!(file->f_mode & FMODE_WRITE)) 548 if (!(file->f_mode & FMODE_WRITE)) 668 return -EBADF; 549 return -EBADF; 669 if (!(file->f_mode & FMODE_CAN_WRITE)) 550 if (!(file->f_mode & FMODE_CAN_WRITE)) 670 return -EINVAL; 551 return -EINVAL; 671 if (unlikely(!access_ok(buf, count))) !! 552 if (unlikely(!access_ok(VERIFY_READ, buf, count))) 672 return -EFAULT; 553 return -EFAULT; 673 554 674 ret = rw_verify_area(WRITE, file, pos, 555 ret = rw_verify_area(WRITE, file, pos, count); 675 if (ret) !! 556 if (!ret) { 676 return ret; !! 557 if (count > MAX_RW_COUNT) 677 if (count > MAX_RW_COUNT) !! 558 count = MAX_RW_COUNT; 678 count = MAX_RW_COUNT; !! 559 file_start_write(file); 679 file_start_write(file); !! 560 ret = __vfs_write(file, buf, count, pos); 680 if (file->f_op->write) !! 561 if (ret > 0) { 681 ret = file->f_op->write(file, !! 562 fsnotify_modify(file); 682 else if (file->f_op->write_iter) !! 563 add_wchar(current, ret); 683 ret = new_sync_write(file, buf !! 564 } 684 else !! 565 inc_syscw(current); 685 ret = -EINVAL; !! 566 file_end_write(file); 686 if (ret > 0) { << 687 fsnotify_modify(file); << 688 add_wchar(current, ret); << 689 } 567 } 690 inc_syscw(current); !! 568 691 file_end_write(file); << 692 return ret; 569 return ret; 693 } 570 } 694 571 695 /* file_ppos returns &file->f_pos or NULL if f !! 572 EXPORT_SYMBOL(vfs_write); 696 static inline loff_t *file_ppos(struct file *f !! 573 >> 574 static inline loff_t file_pos_read(struct file *file) >> 575 { >> 576 return file->f_pos; >> 577 } >> 578 >> 579 static inline void file_pos_write(struct file *file, loff_t pos) 697 { 580 { 698 return file->f_mode & FMODE_STREAM ? N !! 581 file->f_pos = pos; 699 } 582 } 700 583 701 ssize_t ksys_read(unsigned int fd, char __user !! 584 SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count) 702 { 585 { 703 struct fd f = fdget_pos(fd); 586 struct fd f = fdget_pos(fd); 704 ssize_t ret = -EBADF; 587 ssize_t ret = -EBADF; 705 588 706 if (fd_file(f)) { !! 589 if (f.file) { 707 loff_t pos, *ppos = file_ppos( !! 590 loff_t pos = file_pos_read(f.file); 708 if (ppos) { !! 591 ret = vfs_read(f.file, buf, count, &pos); 709 pos = *ppos; !! 592 if (ret >= 0) 710 ppos = &pos; !! 593 file_pos_write(f.file, pos); 711 } << 712 ret = vfs_read(fd_file(f), buf << 713 if (ret >= 0 && ppos) << 714 fd_file(f)->f_pos = po << 715 fdput_pos(f); 594 fdput_pos(f); 716 } 595 } 717 return ret; 596 return ret; 718 } 597 } 719 598 720 SYSCALL_DEFINE3(read, unsigned int, fd, char _ !! 599 SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf, 721 { !! 600 size_t, count) 722 return ksys_read(fd, buf, count); << 723 } << 724 << 725 ssize_t ksys_write(unsigned int fd, const char << 726 { 601 { 727 struct fd f = fdget_pos(fd); 602 struct fd f = fdget_pos(fd); 728 ssize_t ret = -EBADF; 603 ssize_t ret = -EBADF; 729 604 730 if (fd_file(f)) { !! 605 if (f.file) { 731 loff_t pos, *ppos = file_ppos( !! 606 loff_t pos = file_pos_read(f.file); 732 if (ppos) { !! 607 ret = vfs_write(f.file, buf, count, &pos); 733 pos = *ppos; !! 608 if (ret >= 0) 734 ppos = &pos; !! 609 file_pos_write(f.file, pos); 735 } << 736 ret = vfs_write(fd_file(f), bu << 737 if (ret >= 0 && ppos) << 738 fd_file(f)->f_pos = po << 739 fdput_pos(f); 610 fdput_pos(f); 740 } 611 } 741 612 742 return ret; 613 return ret; 743 } 614 } 744 615 745 SYSCALL_DEFINE3(write, unsigned int, fd, const !! 616 SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf, 746 size_t, count) !! 617 size_t, count, loff_t, pos) 747 { << 748 return ksys_write(fd, buf, count); << 749 } << 750 << 751 ssize_t ksys_pread64(unsigned int fd, char __u << 752 loff_t pos) << 753 { 618 { 754 struct fd f; 619 struct fd f; 755 ssize_t ret = -EBADF; 620 ssize_t ret = -EBADF; 756 621 757 if (pos < 0) 622 if (pos < 0) 758 return -EINVAL; 623 return -EINVAL; 759 624 760 f = fdget(fd); 625 f = fdget(fd); 761 if (fd_file(f)) { !! 626 if (f.file) { 762 ret = -ESPIPE; 627 ret = -ESPIPE; 763 if (fd_file(f)->f_mode & FMODE !! 628 if (f.file->f_mode & FMODE_PREAD) 764 ret = vfs_read(fd_file !! 629 ret = vfs_read(f.file, buf, count, &pos); 765 fdput(f); 630 fdput(f); 766 } 631 } 767 632 768 return ret; 633 return ret; 769 } 634 } 770 635 771 SYSCALL_DEFINE4(pread64, unsigned int, fd, cha !! 636 SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf, 772 size_t, count, loff_t, !! 637 size_t, count, loff_t, pos) 773 { << 774 return ksys_pread64(fd, buf, count, po << 775 } << 776 << 777 #if defined(CONFIG_COMPAT) && defined(__ARCH_W << 778 COMPAT_SYSCALL_DEFINE5(pread64, unsigned int, << 779 size_t, count, compat_a << 780 { << 781 return ksys_pread64(fd, buf, count, co << 782 } << 783 #endif << 784 << 785 ssize_t ksys_pwrite64(unsigned int fd, const c << 786 size_t count, loff_t pos << 787 { 638 { 788 struct fd f; 639 struct fd f; 789 ssize_t ret = -EBADF; 640 ssize_t ret = -EBADF; 790 641 791 if (pos < 0) 642 if (pos < 0) 792 return -EINVAL; 643 return -EINVAL; 793 644 794 f = fdget(fd); 645 f = fdget(fd); 795 if (fd_file(f)) { !! 646 if (f.file) { 796 ret = -ESPIPE; 647 ret = -ESPIPE; 797 if (fd_file(f)->f_mode & FMODE !! 648 if (f.file->f_mode & FMODE_PWRITE) 798 ret = vfs_write(fd_fil !! 649 ret = vfs_write(f.file, buf, count, &pos); 799 fdput(f); 650 fdput(f); 800 } 651 } 801 652 802 return ret; 653 return ret; 803 } 654 } 804 655 805 SYSCALL_DEFINE4(pwrite64, unsigned int, fd, co !! 656 /* 806 size_t, count, loff_t !! 657 * Reduce an iovec's length in-place. Return the resulting number of segments >> 658 */ >> 659 unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to) 807 { 660 { 808 return ksys_pwrite64(fd, buf, count, p !! 661 unsigned long seg = 0; 809 } !! 662 size_t len = 0; 810 663 811 #if defined(CONFIG_COMPAT) && defined(__ARCH_W !! 664 while (seg < nr_segs) { 812 COMPAT_SYSCALL_DEFINE5(pwrite64, unsigned int, !! 665 seg++; 813 size_t, count, compat_a !! 666 if (len + iov->iov_len >= to) { 814 { !! 667 iov->iov_len = to - len; 815 return ksys_pwrite64(fd, buf, count, c !! 668 break; >> 669 } >> 670 len += iov->iov_len; >> 671 iov++; >> 672 } >> 673 return seg; 816 } 674 } 817 #endif !! 675 EXPORT_SYMBOL(iov_shorten); 818 676 819 static ssize_t do_iter_readv_writev(struct fil 677 static ssize_t do_iter_readv_writev(struct file *filp, struct iov_iter *iter, 820 loff_t *ppos, int type, rwf_t !! 678 loff_t *ppos, iter_fn_t fn, int flags) 821 { 679 { 822 struct kiocb kiocb; 680 struct kiocb kiocb; 823 ssize_t ret; 681 ssize_t ret; 824 682 >> 683 if (flags & ~(RWF_HIPRI | RWF_DSYNC | RWF_SYNC)) >> 684 return -EOPNOTSUPP; >> 685 825 init_sync_kiocb(&kiocb, filp); 686 init_sync_kiocb(&kiocb, filp); 826 ret = kiocb_set_rw_flags(&kiocb, flags !! 687 if (flags & RWF_HIPRI) 827 if (ret) !! 688 kiocb.ki_flags |= IOCB_HIPRI; 828 return ret; !! 689 if (flags & RWF_DSYNC) 829 kiocb.ki_pos = (ppos ? *ppos : 0); !! 690 kiocb.ki_flags |= IOCB_DSYNC; >> 691 if (flags & RWF_SYNC) >> 692 kiocb.ki_flags |= (IOCB_DSYNC | IOCB_SYNC); >> 693 kiocb.ki_pos = *ppos; 830 694 831 if (type == READ) !! 695 ret = fn(&kiocb, iter); 832 ret = filp->f_op->read_iter(&k << 833 else << 834 ret = filp->f_op->write_iter(& << 835 BUG_ON(ret == -EIOCBQUEUED); 696 BUG_ON(ret == -EIOCBQUEUED); 836 if (ppos) !! 697 *ppos = kiocb.ki_pos; 837 *ppos = kiocb.ki_pos; << 838 return ret; 698 return ret; 839 } 699 } 840 700 841 /* Do it by hand, with file-ops */ 701 /* Do it by hand, with file-ops */ 842 static ssize_t do_loop_readv_writev(struct fil 702 static ssize_t do_loop_readv_writev(struct file *filp, struct iov_iter *iter, 843 loff_t *ppos, int type, rwf_t !! 703 loff_t *ppos, io_fn_t fn, int flags) 844 { 704 { 845 ssize_t ret = 0; 705 ssize_t ret = 0; 846 706 847 if (flags & ~RWF_HIPRI) 707 if (flags & ~RWF_HIPRI) 848 return -EOPNOTSUPP; 708 return -EOPNOTSUPP; 849 709 850 while (iov_iter_count(iter)) { 710 while (iov_iter_count(iter)) { >> 711 struct iovec iovec = iov_iter_iovec(iter); 851 ssize_t nr; 712 ssize_t nr; 852 713 853 if (type == READ) { !! 714 nr = fn(filp, iovec.iov_base, iovec.iov_len, ppos); 854 nr = filp->f_op->read( << 855 << 856 } else { << 857 nr = filp->f_op->write << 858 << 859 } << 860 715 861 if (nr < 0) { 716 if (nr < 0) { 862 if (!ret) 717 if (!ret) 863 ret = nr; 718 ret = nr; 864 break; 719 break; 865 } 720 } 866 ret += nr; 721 ret += nr; 867 if (nr != iter_iov_len(iter)) !! 722 if (nr != iovec.iov_len) 868 break; 723 break; 869 iov_iter_advance(iter, nr); 724 iov_iter_advance(iter, nr); 870 } 725 } 871 726 872 return ret; 727 return ret; 873 } 728 } 874 729 875 ssize_t vfs_iocb_iter_read(struct file *file, !! 730 /* A write operation does a read from user space and vice versa */ 876 struct iov_iter *it !! 731 #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ) 877 { << 878 size_t tot_len; << 879 ssize_t ret = 0; << 880 << 881 if (!file->f_op->read_iter) << 882 return -EINVAL; << 883 if (!(file->f_mode & FMODE_READ)) << 884 return -EBADF; << 885 if (!(file->f_mode & FMODE_CAN_READ)) << 886 return -EINVAL; << 887 << 888 tot_len = iov_iter_count(iter); << 889 if (!tot_len) << 890 goto out; << 891 ret = rw_verify_area(READ, file, &iocb << 892 if (ret < 0) << 893 return ret; << 894 << 895 ret = file->f_op->read_iter(iocb, iter << 896 out: << 897 if (ret >= 0) << 898 fsnotify_access(file); << 899 return ret; << 900 } << 901 EXPORT_SYMBOL(vfs_iocb_iter_read); << 902 732 903 ssize_t vfs_iter_read(struct file *file, struc !! 733 /** 904 rwf_t flags) !! 734 * rw_copy_check_uvector() - Copy an array of &struct iovec from userspace >> 735 * into the kernel and check that it is valid. >> 736 * >> 737 * @type: One of %CHECK_IOVEC_ONLY, %READ, or %WRITE. >> 738 * @uvector: Pointer to the userspace array. >> 739 * @nr_segs: Number of elements in userspace array. >> 740 * @fast_segs: Number of elements in @fast_pointer. >> 741 * @fast_pointer: Pointer to (usually small on-stack) kernel array. >> 742 * @ret_pointer: (output parameter) Pointer to a variable that will point to >> 743 * either @fast_pointer, a newly allocated kernel array, or NULL, >> 744 * depending on which array was used. >> 745 * >> 746 * This function copies an array of &struct iovec of @nr_segs from >> 747 * userspace into the kernel and checks that each element is valid (e.g. >> 748 * it does not point to a kernel address or cause overflow by being too >> 749 * large, etc.). >> 750 * >> 751 * As an optimization, the caller may provide a pointer to a small >> 752 * on-stack array in @fast_pointer, typically %UIO_FASTIOV elements long >> 753 * (the size of this array, or 0 if unused, should be given in @fast_segs). >> 754 * >> 755 * @ret_pointer will always point to the array that was used, so the >> 756 * caller must take care not to call kfree() on it e.g. in case the >> 757 * @fast_pointer array was used and it was allocated on the stack. >> 758 * >> 759 * Return: The total number of bytes covered by the iovec array on success >> 760 * or a negative error code on error. >> 761 */ >> 762 ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector, >> 763 unsigned long nr_segs, unsigned long fast_segs, >> 764 struct iovec *fast_pointer, >> 765 struct iovec **ret_pointer) 905 { 766 { 906 size_t tot_len; !! 767 unsigned long seg; 907 ssize_t ret = 0; !! 768 ssize_t ret; >> 769 struct iovec *iov = fast_pointer; 908 770 909 if (!file->f_op->read_iter) !! 771 /* 910 return -EINVAL; !! 772 * SuS says "The readv() function *may* fail if the iovcnt argument 911 if (!(file->f_mode & FMODE_READ)) !! 773 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has 912 return -EBADF; !! 774 * traditionally returned zero for zero segments, so... 913 if (!(file->f_mode & FMODE_CAN_READ)) !! 775 */ 914 return -EINVAL; !! 776 if (nr_segs == 0) { >> 777 ret = 0; >> 778 goto out; >> 779 } 915 780 916 tot_len = iov_iter_count(iter); !! 781 /* 917 if (!tot_len) !! 782 * First get the "struct iovec" from user memory and >> 783 * verify all the pointers >> 784 */ >> 785 if (nr_segs > UIO_MAXIOV) { >> 786 ret = -EINVAL; 918 goto out; 787 goto out; 919 ret = rw_verify_area(READ, file, ppos, !! 788 } 920 if (ret < 0) !! 789 if (nr_segs > fast_segs) { 921 return ret; !! 790 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL); >> 791 if (iov == NULL) { >> 792 ret = -ENOMEM; >> 793 goto out; >> 794 } >> 795 } >> 796 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) { >> 797 ret = -EFAULT; >> 798 goto out; >> 799 } 922 800 923 ret = do_iter_readv_writev(file, iter, !! 801 /* >> 802 * According to the Single Unix Specification we should return EINVAL >> 803 * if an element length is < 0 when cast to ssize_t or if the >> 804 * total length would overflow the ssize_t return value of the >> 805 * system call. >> 806 * >> 807 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the >> 808 * overflow case. >> 809 */ >> 810 ret = 0; >> 811 for (seg = 0; seg < nr_segs; seg++) { >> 812 void __user *buf = iov[seg].iov_base; >> 813 ssize_t len = (ssize_t)iov[seg].iov_len; >> 814 >> 815 /* see if we we're about to use an invalid len or if >> 816 * it's about to overflow ssize_t */ >> 817 if (len < 0) { >> 818 ret = -EINVAL; >> 819 goto out; >> 820 } >> 821 if (type >= 0 >> 822 && unlikely(!access_ok(vrfy_dir(type), buf, len))) { >> 823 ret = -EFAULT; >> 824 goto out; >> 825 } >> 826 if (len > MAX_RW_COUNT - ret) { >> 827 len = MAX_RW_COUNT - ret; >> 828 iov[seg].iov_len = len; >> 829 } >> 830 ret += len; >> 831 } 924 out: 832 out: 925 if (ret >= 0) !! 833 *ret_pointer = iov; 926 fsnotify_access(file); << 927 return ret; << 928 } << 929 EXPORT_SYMBOL(vfs_iter_read); << 930 << 931 /* << 932 * Caller is responsible for calling kiocb_end << 933 * if async iocb was queued. << 934 */ << 935 ssize_t vfs_iocb_iter_write(struct file *file, << 936 struct iov_iter *i << 937 { << 938 size_t tot_len; << 939 ssize_t ret = 0; << 940 << 941 if (!file->f_op->write_iter) << 942 return -EINVAL; << 943 if (!(file->f_mode & FMODE_WRITE)) << 944 return -EBADF; << 945 if (!(file->f_mode & FMODE_CAN_WRITE)) << 946 return -EINVAL; << 947 << 948 tot_len = iov_iter_count(iter); << 949 if (!tot_len) << 950 return 0; << 951 ret = rw_verify_area(WRITE, file, &ioc << 952 if (ret < 0) << 953 return ret; << 954 << 955 kiocb_start_write(iocb); << 956 ret = file->f_op->write_iter(iocb, ite << 957 if (ret != -EIOCBQUEUED) << 958 kiocb_end_write(iocb); << 959 if (ret > 0) << 960 fsnotify_modify(file); << 961 << 962 return ret; 834 return ret; 963 } 835 } 964 EXPORT_SYMBOL(vfs_iocb_iter_write); << 965 836 966 ssize_t vfs_iter_write(struct file *file, stru !! 837 static ssize_t do_readv_writev(int type, struct file *file, 967 rwf_t flags) !! 838 const struct iovec __user * uvector, >> 839 unsigned long nr_segs, loff_t *pos, >> 840 int flags) 968 { 841 { 969 size_t tot_len; 842 size_t tot_len; 970 ssize_t ret; << 971 << 972 if (!(file->f_mode & FMODE_WRITE)) << 973 return -EBADF; << 974 if (!(file->f_mode & FMODE_CAN_WRITE)) << 975 return -EINVAL; << 976 if (!file->f_op->write_iter) << 977 return -EINVAL; << 978 << 979 tot_len = iov_iter_count(iter); << 980 if (!tot_len) << 981 return 0; << 982 << 983 ret = rw_verify_area(WRITE, file, ppos << 984 if (ret < 0) << 985 return ret; << 986 << 987 file_start_write(file); << 988 ret = do_iter_readv_writev(file, iter, << 989 if (ret > 0) << 990 fsnotify_modify(file); << 991 file_end_write(file); << 992 << 993 return ret; << 994 } << 995 EXPORT_SYMBOL(vfs_iter_write); << 996 << 997 static ssize_t vfs_readv(struct file *file, co << 998 unsigned long vlen, l << 999 { << 1000 struct iovec iovstack[UIO_FASTIOV]; 843 struct iovec iovstack[UIO_FASTIOV]; 1001 struct iovec *iov = iovstack; 844 struct iovec *iov = iovstack; 1002 struct iov_iter iter; 845 struct iov_iter iter; 1003 size_t tot_len; !! 846 ssize_t ret; 1004 ssize_t ret = 0; !! 847 io_fn_t fn; 1005 !! 848 iter_fn_t iter_fn; 1006 if (!(file->f_mode & FMODE_READ)) << 1007 return -EBADF; << 1008 if (!(file->f_mode & FMODE_CAN_READ)) << 1009 return -EINVAL; << 1010 849 1011 ret = import_iovec(ITER_DEST, vec, vl !! 850 ret = import_iovec(type, uvector, nr_segs, 1012 &iter); !! 851 ARRAY_SIZE(iovstack), &iov, &iter); 1013 if (ret < 0) 852 if (ret < 0) 1014 return ret; 853 return ret; 1015 854 1016 tot_len = iov_iter_count(&iter); 855 tot_len = iov_iter_count(&iter); 1017 if (!tot_len) 856 if (!tot_len) 1018 goto out; 857 goto out; 1019 !! 858 ret = rw_verify_area(type, file, pos, tot_len); 1020 ret = rw_verify_area(READ, file, pos, << 1021 if (ret < 0) 859 if (ret < 0) 1022 goto out; 860 goto out; 1023 861 1024 if (file->f_op->read_iter) !! 862 if (type == READ) { 1025 ret = do_iter_readv_writev(fi !! 863 fn = file->f_op->read; >> 864 iter_fn = file->f_op->read_iter; >> 865 } else { >> 866 fn = (io_fn_t)file->f_op->write; >> 867 iter_fn = file->f_op->write_iter; >> 868 file_start_write(file); >> 869 } >> 870 >> 871 if (iter_fn) >> 872 ret = do_iter_readv_writev(file, &iter, pos, iter_fn, flags); 1026 else 873 else 1027 ret = do_loop_readv_writev(fi !! 874 ret = do_loop_readv_writev(file, &iter, pos, fn, flags); >> 875 >> 876 if (type != READ) >> 877 file_end_write(file); >> 878 1028 out: 879 out: 1029 if (ret >= 0) << 1030 fsnotify_access(file); << 1031 kfree(iov); 880 kfree(iov); >> 881 if ((ret + (type == READ)) > 0) { >> 882 if (type == READ) >> 883 fsnotify_access(file); >> 884 else >> 885 fsnotify_modify(file); >> 886 } 1032 return ret; 887 return ret; 1033 } 888 } 1034 889 1035 static ssize_t vfs_writev(struct file *file, !! 890 ssize_t vfs_readv(struct file *file, const struct iovec __user *vec, 1036 unsigned long vlen, !! 891 unsigned long vlen, loff_t *pos, int flags) 1037 { 892 { 1038 struct iovec iovstack[UIO_FASTIOV]; !! 893 if (!(file->f_mode & FMODE_READ)) 1039 struct iovec *iov = iovstack; << 1040 struct iov_iter iter; << 1041 size_t tot_len; << 1042 ssize_t ret = 0; << 1043 << 1044 if (!(file->f_mode & FMODE_WRITE)) << 1045 return -EBADF; 894 return -EBADF; 1046 if (!(file->f_mode & FMODE_CAN_WRITE) !! 895 if (!(file->f_mode & FMODE_CAN_READ)) 1047 return -EINVAL; 896 return -EINVAL; 1048 897 1049 ret = import_iovec(ITER_SOURCE, vec, !! 898 return do_readv_writev(READ, file, vec, vlen, pos, flags); 1050 &iter); !! 899 } 1051 if (ret < 0) << 1052 return ret; << 1053 900 1054 tot_len = iov_iter_count(&iter); !! 901 EXPORT_SYMBOL(vfs_readv); 1055 if (!tot_len) << 1056 goto out; << 1057 902 1058 ret = rw_verify_area(WRITE, file, pos !! 903 ssize_t vfs_writev(struct file *file, const struct iovec __user *vec, 1059 if (ret < 0) !! 904 unsigned long vlen, loff_t *pos, int flags) 1060 goto out; !! 905 { >> 906 if (!(file->f_mode & FMODE_WRITE)) >> 907 return -EBADF; >> 908 if (!(file->f_mode & FMODE_CAN_WRITE)) >> 909 return -EINVAL; 1061 910 1062 file_start_write(file); !! 911 return do_readv_writev(WRITE, file, vec, vlen, pos, flags); 1063 if (file->f_op->write_iter) << 1064 ret = do_iter_readv_writev(fi << 1065 else << 1066 ret = do_loop_readv_writev(fi << 1067 if (ret > 0) << 1068 fsnotify_modify(file); << 1069 file_end_write(file); << 1070 out: << 1071 kfree(iov); << 1072 return ret; << 1073 } 912 } 1074 913 >> 914 EXPORT_SYMBOL(vfs_writev); >> 915 1075 static ssize_t do_readv(unsigned long fd, con 916 static ssize_t do_readv(unsigned long fd, const struct iovec __user *vec, 1076 unsigned long vlen, r !! 917 unsigned long vlen, int flags) 1077 { 918 { 1078 struct fd f = fdget_pos(fd); 919 struct fd f = fdget_pos(fd); 1079 ssize_t ret = -EBADF; 920 ssize_t ret = -EBADF; 1080 921 1081 if (fd_file(f)) { !! 922 if (f.file) { 1082 loff_t pos, *ppos = file_ppos !! 923 loff_t pos = file_pos_read(f.file); 1083 if (ppos) { !! 924 ret = vfs_readv(f.file, vec, vlen, &pos, flags); 1084 pos = *ppos; !! 925 if (ret >= 0) 1085 ppos = &pos; !! 926 file_pos_write(f.file, pos); 1086 } << 1087 ret = vfs_readv(fd_file(f), v << 1088 if (ret >= 0 && ppos) << 1089 fd_file(f)->f_pos = p << 1090 fdput_pos(f); 927 fdput_pos(f); 1091 } 928 } 1092 929 1093 if (ret > 0) 930 if (ret > 0) 1094 add_rchar(current, ret); 931 add_rchar(current, ret); 1095 inc_syscr(current); 932 inc_syscr(current); 1096 return ret; 933 return ret; 1097 } 934 } 1098 935 1099 static ssize_t do_writev(unsigned long fd, co 936 static ssize_t do_writev(unsigned long fd, const struct iovec __user *vec, 1100 unsigned long vlen, !! 937 unsigned long vlen, int flags) 1101 { 938 { 1102 struct fd f = fdget_pos(fd); 939 struct fd f = fdget_pos(fd); 1103 ssize_t ret = -EBADF; 940 ssize_t ret = -EBADF; 1104 941 1105 if (fd_file(f)) { !! 942 if (f.file) { 1106 loff_t pos, *ppos = file_ppos !! 943 loff_t pos = file_pos_read(f.file); 1107 if (ppos) { !! 944 ret = vfs_writev(f.file, vec, vlen, &pos, flags); 1108 pos = *ppos; !! 945 if (ret >= 0) 1109 ppos = &pos; !! 946 file_pos_write(f.file, pos); 1110 } << 1111 ret = vfs_writev(fd_file(f), << 1112 if (ret >= 0 && ppos) << 1113 fd_file(f)->f_pos = p << 1114 fdput_pos(f); 947 fdput_pos(f); 1115 } 948 } 1116 949 1117 if (ret > 0) 950 if (ret > 0) 1118 add_wchar(current, ret); 951 add_wchar(current, ret); 1119 inc_syscw(current); 952 inc_syscw(current); 1120 return ret; 953 return ret; 1121 } 954 } 1122 955 1123 static inline loff_t pos_from_hilo(unsigned l 956 static inline loff_t pos_from_hilo(unsigned long high, unsigned long low) 1124 { 957 { 1125 #define HALF_LONG_BITS (BITS_PER_LONG / 2) 958 #define HALF_LONG_BITS (BITS_PER_LONG / 2) 1126 return (((loff_t)high << HALF_LONG_BI 959 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low; 1127 } 960 } 1128 961 1129 static ssize_t do_preadv(unsigned long fd, co 962 static ssize_t do_preadv(unsigned long fd, const struct iovec __user *vec, 1130 unsigned long vlen, !! 963 unsigned long vlen, loff_t pos, int flags) 1131 { 964 { 1132 struct fd f; 965 struct fd f; 1133 ssize_t ret = -EBADF; 966 ssize_t ret = -EBADF; 1134 967 1135 if (pos < 0) 968 if (pos < 0) 1136 return -EINVAL; 969 return -EINVAL; 1137 970 1138 f = fdget(fd); 971 f = fdget(fd); 1139 if (fd_file(f)) { !! 972 if (f.file) { 1140 ret = -ESPIPE; 973 ret = -ESPIPE; 1141 if (fd_file(f)->f_mode & FMOD !! 974 if (f.file->f_mode & FMODE_PREAD) 1142 ret = vfs_readv(fd_fi !! 975 ret = vfs_readv(f.file, vec, vlen, &pos, flags); 1143 fdput(f); 976 fdput(f); 1144 } 977 } 1145 978 1146 if (ret > 0) 979 if (ret > 0) 1147 add_rchar(current, ret); 980 add_rchar(current, ret); 1148 inc_syscr(current); 981 inc_syscr(current); 1149 return ret; 982 return ret; 1150 } 983 } 1151 984 1152 static ssize_t do_pwritev(unsigned long fd, c 985 static ssize_t do_pwritev(unsigned long fd, const struct iovec __user *vec, 1153 unsigned long vlen, !! 986 unsigned long vlen, loff_t pos, int flags) 1154 { 987 { 1155 struct fd f; 988 struct fd f; 1156 ssize_t ret = -EBADF; 989 ssize_t ret = -EBADF; 1157 990 1158 if (pos < 0) 991 if (pos < 0) 1159 return -EINVAL; 992 return -EINVAL; 1160 993 1161 f = fdget(fd); 994 f = fdget(fd); 1162 if (fd_file(f)) { !! 995 if (f.file) { 1163 ret = -ESPIPE; 996 ret = -ESPIPE; 1164 if (fd_file(f)->f_mode & FMOD !! 997 if (f.file->f_mode & FMODE_PWRITE) 1165 ret = vfs_writev(fd_f !! 998 ret = vfs_writev(f.file, vec, vlen, &pos, flags); 1166 fdput(f); 999 fdput(f); 1167 } 1000 } 1168 1001 1169 if (ret > 0) 1002 if (ret > 0) 1170 add_wchar(current, ret); 1003 add_wchar(current, ret); 1171 inc_syscw(current); 1004 inc_syscw(current); 1172 return ret; 1005 return ret; 1173 } 1006 } 1174 1007 1175 SYSCALL_DEFINE3(readv, unsigned long, fd, con 1008 SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec, 1176 unsigned long, vlen) 1009 unsigned long, vlen) 1177 { 1010 { 1178 return do_readv(fd, vec, vlen, 0); 1011 return do_readv(fd, vec, vlen, 0); 1179 } 1012 } 1180 1013 1181 SYSCALL_DEFINE3(writev, unsigned long, fd, co 1014 SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec, 1182 unsigned long, vlen) 1015 unsigned long, vlen) 1183 { 1016 { 1184 return do_writev(fd, vec, vlen, 0); 1017 return do_writev(fd, vec, vlen, 0); 1185 } 1018 } 1186 1019 1187 SYSCALL_DEFINE5(preadv, unsigned long, fd, co 1020 SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec, 1188 unsigned long, vlen, unsigned 1021 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h) 1189 { 1022 { 1190 loff_t pos = pos_from_hilo(pos_h, pos 1023 loff_t pos = pos_from_hilo(pos_h, pos_l); 1191 1024 1192 return do_preadv(fd, vec, vlen, pos, 1025 return do_preadv(fd, vec, vlen, pos, 0); 1193 } 1026 } 1194 1027 1195 SYSCALL_DEFINE6(preadv2, unsigned long, fd, c 1028 SYSCALL_DEFINE6(preadv2, unsigned long, fd, const struct iovec __user *, vec, 1196 unsigned long, vlen, unsigned 1029 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h, 1197 rwf_t, flags) !! 1030 int, flags) 1198 { 1031 { 1199 loff_t pos = pos_from_hilo(pos_h, pos 1032 loff_t pos = pos_from_hilo(pos_h, pos_l); 1200 1033 1201 if (pos == -1) 1034 if (pos == -1) 1202 return do_readv(fd, vec, vlen 1035 return do_readv(fd, vec, vlen, flags); 1203 1036 1204 return do_preadv(fd, vec, vlen, pos, 1037 return do_preadv(fd, vec, vlen, pos, flags); 1205 } 1038 } 1206 1039 1207 SYSCALL_DEFINE5(pwritev, unsigned long, fd, c 1040 SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec, 1208 unsigned long, vlen, unsigned 1041 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h) 1209 { 1042 { 1210 loff_t pos = pos_from_hilo(pos_h, pos 1043 loff_t pos = pos_from_hilo(pos_h, pos_l); 1211 1044 1212 return do_pwritev(fd, vec, vlen, pos, 1045 return do_pwritev(fd, vec, vlen, pos, 0); 1213 } 1046 } 1214 1047 1215 SYSCALL_DEFINE6(pwritev2, unsigned long, fd, 1048 SYSCALL_DEFINE6(pwritev2, unsigned long, fd, const struct iovec __user *, vec, 1216 unsigned long, vlen, unsigned 1049 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h, 1217 rwf_t, flags) !! 1050 int, flags) 1218 { 1051 { 1219 loff_t pos = pos_from_hilo(pos_h, pos 1052 loff_t pos = pos_from_hilo(pos_h, pos_l); 1220 1053 1221 if (pos == -1) 1054 if (pos == -1) 1222 return do_writev(fd, vec, vle 1055 return do_writev(fd, vec, vlen, flags); 1223 1056 1224 return do_pwritev(fd, vec, vlen, pos, 1057 return do_pwritev(fd, vec, vlen, pos, flags); 1225 } 1058 } 1226 1059 1227 /* << 1228 * Various compat syscalls. Note that they a << 1229 * iovec - import_iovec will properly treat t << 1230 * in_compat_syscall(). << 1231 */ << 1232 #ifdef CONFIG_COMPAT 1060 #ifdef CONFIG_COMPAT >> 1061 >> 1062 static ssize_t compat_do_readv_writev(int type, struct file *file, >> 1063 const struct compat_iovec __user *uvector, >> 1064 unsigned long nr_segs, loff_t *pos, >> 1065 int flags) >> 1066 { >> 1067 compat_ssize_t tot_len; >> 1068 struct iovec iovstack[UIO_FASTIOV]; >> 1069 struct iovec *iov = iovstack; >> 1070 struct iov_iter iter; >> 1071 ssize_t ret; >> 1072 io_fn_t fn; >> 1073 iter_fn_t iter_fn; >> 1074 >> 1075 ret = compat_import_iovec(type, uvector, nr_segs, >> 1076 UIO_FASTIOV, &iov, &iter); >> 1077 if (ret < 0) >> 1078 return ret; >> 1079 >> 1080 tot_len = iov_iter_count(&iter); >> 1081 if (!tot_len) >> 1082 goto out; >> 1083 ret = rw_verify_area(type, file, pos, tot_len); >> 1084 if (ret < 0) >> 1085 goto out; >> 1086 >> 1087 if (type == READ) { >> 1088 fn = file->f_op->read; >> 1089 iter_fn = file->f_op->read_iter; >> 1090 } else { >> 1091 fn = (io_fn_t)file->f_op->write; >> 1092 iter_fn = file->f_op->write_iter; >> 1093 file_start_write(file); >> 1094 } >> 1095 >> 1096 if (iter_fn) >> 1097 ret = do_iter_readv_writev(file, &iter, pos, iter_fn, flags); >> 1098 else >> 1099 ret = do_loop_readv_writev(file, &iter, pos, fn, flags); >> 1100 >> 1101 if (type != READ) >> 1102 file_end_write(file); >> 1103 >> 1104 out: >> 1105 kfree(iov); >> 1106 if ((ret + (type == READ)) > 0) { >> 1107 if (type == READ) >> 1108 fsnotify_access(file); >> 1109 else >> 1110 fsnotify_modify(file); >> 1111 } >> 1112 return ret; >> 1113 } >> 1114 >> 1115 static size_t compat_readv(struct file *file, >> 1116 const struct compat_iovec __user *vec, >> 1117 unsigned long vlen, loff_t *pos, int flags) >> 1118 { >> 1119 ssize_t ret = -EBADF; >> 1120 >> 1121 if (!(file->f_mode & FMODE_READ)) >> 1122 goto out; >> 1123 >> 1124 ret = -EINVAL; >> 1125 if (!(file->f_mode & FMODE_CAN_READ)) >> 1126 goto out; >> 1127 >> 1128 ret = compat_do_readv_writev(READ, file, vec, vlen, pos, flags); >> 1129 >> 1130 out: >> 1131 if (ret > 0) >> 1132 add_rchar(current, ret); >> 1133 inc_syscr(current); >> 1134 return ret; >> 1135 } >> 1136 >> 1137 static size_t do_compat_readv(compat_ulong_t fd, >> 1138 const struct compat_iovec __user *vec, >> 1139 compat_ulong_t vlen, int flags) >> 1140 { >> 1141 struct fd f = fdget_pos(fd); >> 1142 ssize_t ret; >> 1143 loff_t pos; >> 1144 >> 1145 if (!f.file) >> 1146 return -EBADF; >> 1147 pos = f.file->f_pos; >> 1148 ret = compat_readv(f.file, vec, vlen, &pos, flags); >> 1149 if (ret >= 0) >> 1150 f.file->f_pos = pos; >> 1151 fdput_pos(f); >> 1152 return ret; >> 1153 >> 1154 } >> 1155 >> 1156 COMPAT_SYSCALL_DEFINE3(readv, compat_ulong_t, fd, >> 1157 const struct compat_iovec __user *,vec, >> 1158 compat_ulong_t, vlen) >> 1159 { >> 1160 return do_compat_readv(fd, vec, vlen, 0); >> 1161 } >> 1162 >> 1163 static long do_compat_preadv64(unsigned long fd, >> 1164 const struct compat_iovec __user *vec, >> 1165 unsigned long vlen, loff_t pos, int flags) >> 1166 { >> 1167 struct fd f; >> 1168 ssize_t ret; >> 1169 >> 1170 if (pos < 0) >> 1171 return -EINVAL; >> 1172 f = fdget(fd); >> 1173 if (!f.file) >> 1174 return -EBADF; >> 1175 ret = -ESPIPE; >> 1176 if (f.file->f_mode & FMODE_PREAD) >> 1177 ret = compat_readv(f.file, vec, vlen, &pos, flags); >> 1178 fdput(f); >> 1179 return ret; >> 1180 } >> 1181 1233 #ifdef __ARCH_WANT_COMPAT_SYS_PREADV64 1182 #ifdef __ARCH_WANT_COMPAT_SYS_PREADV64 1234 COMPAT_SYSCALL_DEFINE4(preadv64, unsigned lon 1183 COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd, 1235 const struct iovec __user *, !! 1184 const struct compat_iovec __user *,vec, 1236 unsigned long, vlen, loff_t, 1185 unsigned long, vlen, loff_t, pos) 1237 { 1186 { 1238 return do_preadv(fd, vec, vlen, pos, !! 1187 return do_compat_preadv64(fd, vec, vlen, pos, 0); 1239 } 1188 } 1240 #endif 1189 #endif 1241 1190 1242 COMPAT_SYSCALL_DEFINE5(preadv, compat_ulong_t 1191 COMPAT_SYSCALL_DEFINE5(preadv, compat_ulong_t, fd, 1243 const struct iovec __user *, !! 1192 const struct compat_iovec __user *,vec, 1244 compat_ulong_t, vlen, u32, po 1193 compat_ulong_t, vlen, u32, pos_low, u32, pos_high) 1245 { 1194 { 1246 loff_t pos = ((loff_t)pos_high << 32) 1195 loff_t pos = ((loff_t)pos_high << 32) | pos_low; 1247 1196 1248 return do_preadv(fd, vec, vlen, pos, !! 1197 return do_compat_preadv64(fd, vec, vlen, pos, 0); 1249 } 1198 } 1250 1199 1251 #ifdef __ARCH_WANT_COMPAT_SYS_PREADV64V2 1200 #ifdef __ARCH_WANT_COMPAT_SYS_PREADV64V2 1252 COMPAT_SYSCALL_DEFINE5(preadv64v2, unsigned l 1201 COMPAT_SYSCALL_DEFINE5(preadv64v2, unsigned long, fd, 1253 const struct iovec __user *, !! 1202 const struct compat_iovec __user *,vec, 1254 unsigned long, vlen, loff_t, !! 1203 unsigned long, vlen, loff_t, pos, int, flags) 1255 { 1204 { 1256 if (pos == -1) !! 1205 return do_compat_preadv64(fd, vec, vlen, pos, flags); 1257 return do_readv(fd, vec, vlen << 1258 return do_preadv(fd, vec, vlen, pos, << 1259 } 1206 } 1260 #endif 1207 #endif 1261 1208 1262 COMPAT_SYSCALL_DEFINE6(preadv2, compat_ulong_ 1209 COMPAT_SYSCALL_DEFINE6(preadv2, compat_ulong_t, fd, 1263 const struct iovec __user *, !! 1210 const struct compat_iovec __user *,vec, 1264 compat_ulong_t, vlen, u32, po 1211 compat_ulong_t, vlen, u32, pos_low, u32, pos_high, 1265 rwf_t, flags) !! 1212 int, flags) 1266 { 1213 { 1267 loff_t pos = ((loff_t)pos_high << 32) 1214 loff_t pos = ((loff_t)pos_high << 32) | pos_low; 1268 1215 1269 if (pos == -1) 1216 if (pos == -1) 1270 return do_readv(fd, vec, vlen !! 1217 return do_compat_readv(fd, vec, vlen, flags); 1271 return do_preadv(fd, vec, vlen, pos, !! 1218 >> 1219 return do_compat_preadv64(fd, vec, vlen, pos, flags); >> 1220 } >> 1221 >> 1222 static size_t compat_writev(struct file *file, >> 1223 const struct compat_iovec __user *vec, >> 1224 unsigned long vlen, loff_t *pos, int flags) >> 1225 { >> 1226 ssize_t ret = -EBADF; >> 1227 >> 1228 if (!(file->f_mode & FMODE_WRITE)) >> 1229 goto out; >> 1230 >> 1231 ret = -EINVAL; >> 1232 if (!(file->f_mode & FMODE_CAN_WRITE)) >> 1233 goto out; >> 1234 >> 1235 ret = compat_do_readv_writev(WRITE, file, vec, vlen, pos, 0); >> 1236 >> 1237 out: >> 1238 if (ret > 0) >> 1239 add_wchar(current, ret); >> 1240 inc_syscw(current); >> 1241 return ret; >> 1242 } >> 1243 >> 1244 static size_t do_compat_writev(compat_ulong_t fd, >> 1245 const struct compat_iovec __user* vec, >> 1246 compat_ulong_t vlen, int flags) >> 1247 { >> 1248 struct fd f = fdget_pos(fd); >> 1249 ssize_t ret; >> 1250 loff_t pos; >> 1251 >> 1252 if (!f.file) >> 1253 return -EBADF; >> 1254 pos = f.file->f_pos; >> 1255 ret = compat_writev(f.file, vec, vlen, &pos, flags); >> 1256 if (ret >= 0) >> 1257 f.file->f_pos = pos; >> 1258 fdput_pos(f); >> 1259 return ret; >> 1260 } >> 1261 >> 1262 COMPAT_SYSCALL_DEFINE3(writev, compat_ulong_t, fd, >> 1263 const struct compat_iovec __user *, vec, >> 1264 compat_ulong_t, vlen) >> 1265 { >> 1266 return do_compat_writev(fd, vec, vlen, 0); >> 1267 } >> 1268 >> 1269 static long do_compat_pwritev64(unsigned long fd, >> 1270 const struct compat_iovec __user *vec, >> 1271 unsigned long vlen, loff_t pos, int flags) >> 1272 { >> 1273 struct fd f; >> 1274 ssize_t ret; >> 1275 >> 1276 if (pos < 0) >> 1277 return -EINVAL; >> 1278 f = fdget(fd); >> 1279 if (!f.file) >> 1280 return -EBADF; >> 1281 ret = -ESPIPE; >> 1282 if (f.file->f_mode & FMODE_PWRITE) >> 1283 ret = compat_writev(f.file, vec, vlen, &pos, flags); >> 1284 fdput(f); >> 1285 return ret; 1272 } 1286 } 1273 1287 1274 #ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64 1288 #ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64 1275 COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned lo 1289 COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd, 1276 const struct iovec __user *, !! 1290 const struct compat_iovec __user *,vec, 1277 unsigned long, vlen, loff_t, 1291 unsigned long, vlen, loff_t, pos) 1278 { 1292 { 1279 return do_pwritev(fd, vec, vlen, pos, !! 1293 return do_compat_pwritev64(fd, vec, vlen, pos, 0); 1280 } 1294 } 1281 #endif 1295 #endif 1282 1296 1283 COMPAT_SYSCALL_DEFINE5(pwritev, compat_ulong_ 1297 COMPAT_SYSCALL_DEFINE5(pwritev, compat_ulong_t, fd, 1284 const struct iovec __user *,v !! 1298 const struct compat_iovec __user *,vec, 1285 compat_ulong_t, vlen, u32, po 1299 compat_ulong_t, vlen, u32, pos_low, u32, pos_high) 1286 { 1300 { 1287 loff_t pos = ((loff_t)pos_high << 32) 1301 loff_t pos = ((loff_t)pos_high << 32) | pos_low; 1288 1302 1289 return do_pwritev(fd, vec, vlen, pos, !! 1303 return do_compat_pwritev64(fd, vec, vlen, pos, 0); 1290 } 1304 } 1291 1305 1292 #ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64V2 1306 #ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64V2 1293 COMPAT_SYSCALL_DEFINE5(pwritev64v2, unsigned 1307 COMPAT_SYSCALL_DEFINE5(pwritev64v2, unsigned long, fd, 1294 const struct iovec __user *, !! 1308 const struct compat_iovec __user *,vec, 1295 unsigned long, vlen, loff_t, !! 1309 unsigned long, vlen, loff_t, pos, int, flags) 1296 { 1310 { 1297 if (pos == -1) !! 1311 return do_compat_pwritev64(fd, vec, vlen, pos, flags); 1298 return do_writev(fd, vec, vle << 1299 return do_pwritev(fd, vec, vlen, pos, << 1300 } 1312 } 1301 #endif 1313 #endif 1302 1314 1303 COMPAT_SYSCALL_DEFINE6(pwritev2, compat_ulong 1315 COMPAT_SYSCALL_DEFINE6(pwritev2, compat_ulong_t, fd, 1304 const struct iovec __user *,v !! 1316 const struct compat_iovec __user *,vec, 1305 compat_ulong_t, vlen, u32, po !! 1317 compat_ulong_t, vlen, u32, pos_low, u32, pos_high, int, flags) 1306 { 1318 { 1307 loff_t pos = ((loff_t)pos_high << 32) 1319 loff_t pos = ((loff_t)pos_high << 32) | pos_low; 1308 1320 1309 if (pos == -1) 1321 if (pos == -1) 1310 return do_writev(fd, vec, vle !! 1322 return do_compat_writev(fd, vec, vlen, flags); 1311 return do_pwritev(fd, vec, vlen, pos, !! 1323 >> 1324 return do_compat_pwritev64(fd, vec, vlen, pos, flags); 1312 } 1325 } 1313 #endif /* CONFIG_COMPAT */ !! 1326 >> 1327 #endif 1314 1328 1315 static ssize_t do_sendfile(int out_fd, int in 1329 static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos, 1316 size_t count, loff !! 1330 size_t count, loff_t max) 1317 { 1331 { 1318 struct fd in, out; 1332 struct fd in, out; 1319 struct inode *in_inode, *out_inode; 1333 struct inode *in_inode, *out_inode; 1320 struct pipe_inode_info *opipe; << 1321 loff_t pos; 1334 loff_t pos; 1322 loff_t out_pos; 1335 loff_t out_pos; 1323 ssize_t retval; 1336 ssize_t retval; 1324 int fl; 1337 int fl; 1325 1338 1326 /* 1339 /* 1327 * Get input file, and verify that it 1340 * Get input file, and verify that it is ok.. 1328 */ 1341 */ 1329 retval = -EBADF; 1342 retval = -EBADF; 1330 in = fdget(in_fd); 1343 in = fdget(in_fd); 1331 if (!fd_file(in)) !! 1344 if (!in.file) 1332 goto out; 1345 goto out; 1333 if (!(fd_file(in)->f_mode & FMODE_REA !! 1346 if (!(in.file->f_mode & FMODE_READ)) 1334 goto fput_in; 1347 goto fput_in; 1335 retval = -ESPIPE; 1348 retval = -ESPIPE; 1336 if (!ppos) { 1349 if (!ppos) { 1337 pos = fd_file(in)->f_pos; !! 1350 pos = in.file->f_pos; 1338 } else { 1351 } else { 1339 pos = *ppos; 1352 pos = *ppos; 1340 if (!(fd_file(in)->f_mode & F !! 1353 if (!(in.file->f_mode & FMODE_PREAD)) 1341 goto fput_in; 1354 goto fput_in; 1342 } 1355 } 1343 retval = rw_verify_area(READ, fd_file !! 1356 retval = rw_verify_area(READ, in.file, &pos, count); 1344 if (retval < 0) 1357 if (retval < 0) 1345 goto fput_in; 1358 goto fput_in; 1346 if (count > MAX_RW_COUNT) 1359 if (count > MAX_RW_COUNT) 1347 count = MAX_RW_COUNT; 1360 count = MAX_RW_COUNT; 1348 1361 1349 /* 1362 /* 1350 * Get output file, and verify that i 1363 * Get output file, and verify that it is ok.. 1351 */ 1364 */ 1352 retval = -EBADF; 1365 retval = -EBADF; 1353 out = fdget(out_fd); 1366 out = fdget(out_fd); 1354 if (!fd_file(out)) !! 1367 if (!out.file) 1355 goto fput_in; 1368 goto fput_in; 1356 if (!(fd_file(out)->f_mode & FMODE_WR !! 1369 if (!(out.file->f_mode & FMODE_WRITE)) >> 1370 goto fput_out; >> 1371 retval = -EINVAL; >> 1372 in_inode = file_inode(in.file); >> 1373 out_inode = file_inode(out.file); >> 1374 out_pos = out.file->f_pos; >> 1375 retval = rw_verify_area(WRITE, out.file, &out_pos, count); >> 1376 if (retval < 0) 1357 goto fput_out; 1377 goto fput_out; 1358 in_inode = file_inode(fd_file(in)); << 1359 out_inode = file_inode(fd_file(out)); << 1360 out_pos = fd_file(out)->f_pos; << 1361 1378 1362 if (!max) 1379 if (!max) 1363 max = min(in_inode->i_sb->s_m 1380 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes); 1364 1381 1365 if (unlikely(pos + count > max)) { 1382 if (unlikely(pos + count > max)) { 1366 retval = -EOVERFLOW; 1383 retval = -EOVERFLOW; 1367 if (pos >= max) 1384 if (pos >= max) 1368 goto fput_out; 1385 goto fput_out; 1369 count = max - pos; 1386 count = max - pos; 1370 } 1387 } 1371 1388 1372 fl = 0; 1389 fl = 0; 1373 #if 0 1390 #if 0 1374 /* 1391 /* 1375 * We need to debate whether we can e 1392 * We need to debate whether we can enable this or not. The 1376 * man page documents EAGAIN return f 1393 * man page documents EAGAIN return for the output at least, 1377 * and the application is arguably bu 1394 * and the application is arguably buggy if it doesn't expect 1378 * EAGAIN on a non-blocking file desc 1395 * EAGAIN on a non-blocking file descriptor. 1379 */ 1396 */ 1380 if (fd_file(in)->f_flags & O_NONBLOCK !! 1397 if (in.file->f_flags & O_NONBLOCK) 1381 fl = SPLICE_F_NONBLOCK; 1398 fl = SPLICE_F_NONBLOCK; 1382 #endif 1399 #endif 1383 opipe = get_pipe_info(fd_file(out), t !! 1400 file_start_write(out.file); 1384 if (!opipe) { !! 1401 retval = do_splice_direct(in.file, &pos, out.file, &out_pos, count, fl); 1385 retval = rw_verify_area(WRITE !! 1402 file_end_write(out.file); 1386 if (retval < 0) << 1387 goto fput_out; << 1388 retval = do_splice_direct(fd_ << 1389 cou << 1390 } else { << 1391 if (fd_file(out)->f_flags & O << 1392 fl |= SPLICE_F_NONBLO << 1393 << 1394 retval = splice_file_to_pipe( << 1395 } << 1396 1403 1397 if (retval > 0) { 1404 if (retval > 0) { 1398 add_rchar(current, retval); 1405 add_rchar(current, retval); 1399 add_wchar(current, retval); 1406 add_wchar(current, retval); 1400 fsnotify_access(fd_file(in)); !! 1407 fsnotify_access(in.file); 1401 fsnotify_modify(fd_file(out)) !! 1408 fsnotify_modify(out.file); 1402 fd_file(out)->f_pos = out_pos !! 1409 out.file->f_pos = out_pos; 1403 if (ppos) 1410 if (ppos) 1404 *ppos = pos; 1411 *ppos = pos; 1405 else 1412 else 1406 fd_file(in)->f_pos = !! 1413 in.file->f_pos = pos; 1407 } 1414 } 1408 1415 1409 inc_syscr(current); 1416 inc_syscr(current); 1410 inc_syscw(current); 1417 inc_syscw(current); 1411 if (pos > max) 1418 if (pos > max) 1412 retval = -EOVERFLOW; 1419 retval = -EOVERFLOW; 1413 1420 1414 fput_out: 1421 fput_out: 1415 fdput(out); 1422 fdput(out); 1416 fput_in: 1423 fput_in: 1417 fdput(in); 1424 fdput(in); 1418 out: 1425 out: 1419 return retval; 1426 return retval; 1420 } 1427 } 1421 1428 1422 SYSCALL_DEFINE4(sendfile, int, out_fd, int, i 1429 SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count) 1423 { 1430 { 1424 loff_t pos; 1431 loff_t pos; 1425 off_t off; 1432 off_t off; 1426 ssize_t ret; 1433 ssize_t ret; 1427 1434 1428 if (offset) { 1435 if (offset) { 1429 if (unlikely(get_user(off, of 1436 if (unlikely(get_user(off, offset))) 1430 return -EFAULT; 1437 return -EFAULT; 1431 pos = off; 1438 pos = off; 1432 ret = do_sendfile(out_fd, in_ 1439 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS); 1433 if (unlikely(put_user(pos, of 1440 if (unlikely(put_user(pos, offset))) 1434 return -EFAULT; 1441 return -EFAULT; 1435 return ret; 1442 return ret; 1436 } 1443 } 1437 1444 1438 return do_sendfile(out_fd, in_fd, NUL 1445 return do_sendfile(out_fd, in_fd, NULL, count, 0); 1439 } 1446 } 1440 1447 1441 SYSCALL_DEFINE4(sendfile64, int, out_fd, int, 1448 SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count) 1442 { 1449 { 1443 loff_t pos; 1450 loff_t pos; 1444 ssize_t ret; 1451 ssize_t ret; 1445 1452 1446 if (offset) { 1453 if (offset) { 1447 if (unlikely(copy_from_user(& 1454 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t)))) 1448 return -EFAULT; 1455 return -EFAULT; 1449 ret = do_sendfile(out_fd, in_ 1456 ret = do_sendfile(out_fd, in_fd, &pos, count, 0); 1450 if (unlikely(put_user(pos, of 1457 if (unlikely(put_user(pos, offset))) 1451 return -EFAULT; 1458 return -EFAULT; 1452 return ret; 1459 return ret; 1453 } 1460 } 1454 1461 1455 return do_sendfile(out_fd, in_fd, NUL 1462 return do_sendfile(out_fd, in_fd, NULL, count, 0); 1456 } 1463 } 1457 1464 1458 #ifdef CONFIG_COMPAT 1465 #ifdef CONFIG_COMPAT 1459 COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, 1466 COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, 1460 compat_off_t __user *, offset 1467 compat_off_t __user *, offset, compat_size_t, count) 1461 { 1468 { 1462 loff_t pos; 1469 loff_t pos; 1463 off_t off; 1470 off_t off; 1464 ssize_t ret; 1471 ssize_t ret; 1465 1472 1466 if (offset) { 1473 if (offset) { 1467 if (unlikely(get_user(off, of 1474 if (unlikely(get_user(off, offset))) 1468 return -EFAULT; 1475 return -EFAULT; 1469 pos = off; 1476 pos = off; 1470 ret = do_sendfile(out_fd, in_ 1477 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS); 1471 if (unlikely(put_user(pos, of 1478 if (unlikely(put_user(pos, offset))) 1472 return -EFAULT; 1479 return -EFAULT; 1473 return ret; 1480 return ret; 1474 } 1481 } 1475 1482 1476 return do_sendfile(out_fd, in_fd, NUL 1483 return do_sendfile(out_fd, in_fd, NULL, count, 0); 1477 } 1484 } 1478 1485 1479 COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_f 1486 COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, 1480 compat_loff_t __user *, offse 1487 compat_loff_t __user *, offset, compat_size_t, count) 1481 { 1488 { 1482 loff_t pos; 1489 loff_t pos; 1483 ssize_t ret; 1490 ssize_t ret; 1484 1491 1485 if (offset) { 1492 if (offset) { 1486 if (unlikely(copy_from_user(& 1493 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t)))) 1487 return -EFAULT; 1494 return -EFAULT; 1488 ret = do_sendfile(out_fd, in_ 1495 ret = do_sendfile(out_fd, in_fd, &pos, count, 0); 1489 if (unlikely(put_user(pos, of 1496 if (unlikely(put_user(pos, offset))) 1490 return -EFAULT; 1497 return -EFAULT; 1491 return ret; 1498 return ret; 1492 } 1499 } 1493 1500 1494 return do_sendfile(out_fd, in_fd, NUL 1501 return do_sendfile(out_fd, in_fd, NULL, count, 0); 1495 } 1502 } 1496 #endif 1503 #endif 1497 1504 1498 /* 1505 /* 1499 * Performs necessary checks before doing a f << 1500 * << 1501 * Can adjust amount of bytes to copy via @re << 1502 * Returns appropriate error code that caller << 1503 * zero in case the copy should be allowed. << 1504 */ << 1505 static int generic_copy_file_checks(struct fi << 1506 struct fi << 1507 size_t *r << 1508 { << 1509 struct inode *inode_in = file_inode(f << 1510 struct inode *inode_out = file_inode( << 1511 uint64_t count = *req_count; << 1512 loff_t size_in; << 1513 int ret; << 1514 << 1515 ret = generic_file_rw_checks(file_in, << 1516 if (ret) << 1517 return ret; << 1518 << 1519 /* << 1520 * We allow some filesystems to handl << 1521 * a file of the wrong filesystem typ << 1522 * in an attempt to dereference the w << 1523 * avoid doing that until we really h << 1524 * << 1525 * nfs and cifs define several differ << 1526 * and several different sets of file << 1527 * using the same ->copy_file_range() << 1528 */ << 1529 if (flags & COPY_FILE_SPLICE) { << 1530 /* cross sb splice is allowed << 1531 } else if (file_out->f_op->copy_file_ << 1532 if (file_in->f_op->copy_file_ << 1533 file_out->f_op->copy_file << 1534 return -EXDEV; << 1535 } else if (file_inode(file_in)->i_sb << 1536 return -EXDEV; << 1537 } << 1538 << 1539 /* Don't touch certain kinds of inode << 1540 if (IS_IMMUTABLE(inode_out)) << 1541 return -EPERM; << 1542 << 1543 if (IS_SWAPFILE(inode_in) || IS_SWAPF << 1544 return -ETXTBSY; << 1545 << 1546 /* Ensure offsets don't wrap. */ << 1547 if (pos_in + count < pos_in || pos_ou << 1548 return -EOVERFLOW; << 1549 << 1550 /* Shorten the copy to EOF */ << 1551 size_in = i_size_read(inode_in); << 1552 if (pos_in >= size_in) << 1553 count = 0; << 1554 else << 1555 count = min(count, size_in - << 1556 << 1557 ret = generic_write_check_limits(file << 1558 if (ret) << 1559 return ret; << 1560 << 1561 /* Don't allow overlapped copying wit << 1562 if (inode_in == inode_out && << 1563 pos_out + count > pos_in && << 1564 pos_out < pos_in + count) << 1565 return -EINVAL; << 1566 << 1567 *req_count = count; << 1568 return 0; << 1569 } << 1570 << 1571 /* << 1572 * copy_file_range() differs from regular fil 1506 * copy_file_range() differs from regular file read and write in that it 1573 * specifically allows return partial success 1507 * specifically allows return partial success. When it does so is up to 1574 * the copy_file_range method. 1508 * the copy_file_range method. 1575 */ 1509 */ 1576 ssize_t vfs_copy_file_range(struct file *file 1510 ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in, 1577 struct file *file 1511 struct file *file_out, loff_t pos_out, 1578 size_t len, unsig 1512 size_t len, unsigned int flags) 1579 { 1513 { >> 1514 struct inode *inode_in = file_inode(file_in); >> 1515 struct inode *inode_out = file_inode(file_out); 1580 ssize_t ret; 1516 ssize_t ret; 1581 bool splice = flags & COPY_FILE_SPLIC << 1582 bool samesb = file_inode(file_in)->i_ << 1583 1517 1584 if (flags & ~COPY_FILE_SPLICE) !! 1518 if (flags != 0) 1585 return -EINVAL; 1519 return -EINVAL; 1586 1520 1587 ret = generic_copy_file_checks(file_i << 1588 flags) << 1589 if (unlikely(ret)) << 1590 return ret; << 1591 << 1592 ret = rw_verify_area(READ, file_in, & 1521 ret = rw_verify_area(READ, file_in, &pos_in, len); 1593 if (unlikely(ret)) 1522 if (unlikely(ret)) 1594 return ret; 1523 return ret; 1595 1524 1596 ret = rw_verify_area(WRITE, file_out, 1525 ret = rw_verify_area(WRITE, file_out, &pos_out, len); 1597 if (unlikely(ret)) 1526 if (unlikely(ret)) 1598 return ret; 1527 return ret; 1599 1528 >> 1529 if (!(file_in->f_mode & FMODE_READ) || >> 1530 !(file_out->f_mode & FMODE_WRITE) || >> 1531 (file_out->f_flags & O_APPEND)) >> 1532 return -EBADF; >> 1533 >> 1534 /* this could be relaxed once a method supports cross-fs copies */ >> 1535 if (inode_in->i_sb != inode_out->i_sb) >> 1536 return -EXDEV; >> 1537 1600 if (len == 0) 1538 if (len == 0) 1601 return 0; 1539 return 0; 1602 1540 1603 file_start_write(file_out); !! 1541 sb_start_write(inode_out->i_sb); 1604 1542 1605 /* 1543 /* 1606 * Cloning is supported by more file !! 1544 * Try cloning first, this is supported by more file systems, and 1607 * same sb using clone, but for files !! 1545 * more efficient if both clone and copy are supported (e.g. NFS). 1608 * are supported (e.g. nfs,cifs), we << 1609 */ 1546 */ 1610 if (!splice && file_out->f_op->copy_f !! 1547 if (file_in->f_op->clone_file_range) { 1611 ret = file_out->f_op->copy_fi !! 1548 ret = file_in->f_op->clone_file_range(file_in, pos_in, 1612 !! 1549 file_out, pos_out, len); 1613 !! 1550 if (ret == 0) { 1614 } else if (!splice && file_in->f_op-> !! 1551 ret = len; 1615 ret = file_in->f_op->remap_fi !! 1552 goto done; 1616 file_out, pos !! 1553 } 1617 min_t(loff_t, << 1618 REMAP_FILE_CA << 1619 /* fallback to splice */ << 1620 if (ret <= 0) << 1621 splice = true; << 1622 } else if (samesb) { << 1623 /* Fallback to splice for sam << 1624 splice = true; << 1625 } 1554 } 1626 1555 1627 file_end_write(file_out); !! 1556 if (file_out->f_op->copy_file_range) { 1628 !! 1557 ret = file_out->f_op->copy_file_range(file_in, pos_in, file_out, 1629 if (!splice) !! 1558 pos_out, len, flags); 1630 goto done; !! 1559 if (ret != -EOPNOTSUPP) >> 1560 goto done; >> 1561 } 1631 1562 1632 /* << 1633 * We can get here for same sb copy o << 1634 * ->copy_file_range() in case filesy << 1635 * case filesystem supports clone but << 1636 * because it was not block aligned). << 1637 * << 1638 * In both cases, fall back to kernel << 1639 * consistent story about which files << 1640 * and which filesystems do not, that << 1641 * make consistent desicions w.r.t us << 1642 * << 1643 * We also get here if caller (e.g. n << 1644 * for server-side-copy between any t << 1645 * << 1646 * In any case, we call do_splice_dir << 1647 * without file_start_write() held, t << 1648 * to splicing from input file, while << 1649 * the output file on a different sb. << 1650 */ << 1651 ret = do_splice_direct(file_in, &pos_ 1563 ret = do_splice_direct(file_in, &pos_in, file_out, &pos_out, 1652 min_t(size_t, !! 1564 len > MAX_RW_COUNT ? MAX_RW_COUNT : len, 0); >> 1565 1653 done: 1566 done: 1654 if (ret > 0) { 1567 if (ret > 0) { 1655 fsnotify_access(file_in); 1568 fsnotify_access(file_in); 1656 add_rchar(current, ret); 1569 add_rchar(current, ret); 1657 fsnotify_modify(file_out); 1570 fsnotify_modify(file_out); 1658 add_wchar(current, ret); 1571 add_wchar(current, ret); 1659 } 1572 } 1660 1573 1661 inc_syscr(current); 1574 inc_syscr(current); 1662 inc_syscw(current); 1575 inc_syscw(current); 1663 1576 >> 1577 sb_end_write(inode_out->i_sb); >> 1578 1664 return ret; 1579 return ret; 1665 } 1580 } 1666 EXPORT_SYMBOL(vfs_copy_file_range); 1581 EXPORT_SYMBOL(vfs_copy_file_range); 1667 1582 1668 SYSCALL_DEFINE6(copy_file_range, int, fd_in, 1583 SYSCALL_DEFINE6(copy_file_range, int, fd_in, loff_t __user *, off_in, 1669 int, fd_out, loff_t __user *, 1584 int, fd_out, loff_t __user *, off_out, 1670 size_t, len, unsigned int, fl 1585 size_t, len, unsigned int, flags) 1671 { 1586 { 1672 loff_t pos_in; 1587 loff_t pos_in; 1673 loff_t pos_out; 1588 loff_t pos_out; 1674 struct fd f_in; 1589 struct fd f_in; 1675 struct fd f_out; 1590 struct fd f_out; 1676 ssize_t ret = -EBADF; 1591 ssize_t ret = -EBADF; 1677 1592 1678 f_in = fdget(fd_in); 1593 f_in = fdget(fd_in); 1679 if (!fd_file(f_in)) !! 1594 if (!f_in.file) 1680 goto out2; 1595 goto out2; 1681 1596 1682 f_out = fdget(fd_out); 1597 f_out = fdget(fd_out); 1683 if (!fd_file(f_out)) !! 1598 if (!f_out.file) 1684 goto out1; 1599 goto out1; 1685 1600 1686 ret = -EFAULT; 1601 ret = -EFAULT; 1687 if (off_in) { 1602 if (off_in) { 1688 if (copy_from_user(&pos_in, o 1603 if (copy_from_user(&pos_in, off_in, sizeof(loff_t))) 1689 goto out; 1604 goto out; 1690 } else { 1605 } else { 1691 pos_in = fd_file(f_in)->f_pos !! 1606 pos_in = f_in.file->f_pos; 1692 } 1607 } 1693 1608 1694 if (off_out) { 1609 if (off_out) { 1695 if (copy_from_user(&pos_out, 1610 if (copy_from_user(&pos_out, off_out, sizeof(loff_t))) 1696 goto out; 1611 goto out; 1697 } else { 1612 } else { 1698 pos_out = fd_file(f_out)->f_p !! 1613 pos_out = f_out.file->f_pos; 1699 } 1614 } 1700 1615 1701 ret = -EINVAL; !! 1616 ret = vfs_copy_file_range(f_in.file, pos_in, f_out.file, pos_out, len, 1702 if (flags != 0) << 1703 goto out; << 1704 << 1705 ret = vfs_copy_file_range(fd_file(f_i << 1706 flags); 1617 flags); 1707 if (ret > 0) { 1618 if (ret > 0) { 1708 pos_in += ret; 1619 pos_in += ret; 1709 pos_out += ret; 1620 pos_out += ret; 1710 1621 1711 if (off_in) { 1622 if (off_in) { 1712 if (copy_to_user(off_ 1623 if (copy_to_user(off_in, &pos_in, sizeof(loff_t))) 1713 ret = -EFAULT 1624 ret = -EFAULT; 1714 } else { 1625 } else { 1715 fd_file(f_in)->f_pos !! 1626 f_in.file->f_pos = pos_in; 1716 } 1627 } 1717 1628 1718 if (off_out) { 1629 if (off_out) { 1719 if (copy_to_user(off_ 1630 if (copy_to_user(off_out, &pos_out, sizeof(loff_t))) 1720 ret = -EFAULT 1631 ret = -EFAULT; 1721 } else { 1632 } else { 1722 fd_file(f_out)->f_pos !! 1633 f_out.file->f_pos = pos_out; 1723 } 1634 } 1724 } 1635 } 1725 1636 1726 out: 1637 out: 1727 fdput(f_out); 1638 fdput(f_out); 1728 out1: 1639 out1: 1729 fdput(f_in); 1640 fdput(f_in); 1730 out2: 1641 out2: 1731 return ret; 1642 return ret; 1732 } 1643 } 1733 1644 1734 /* !! 1645 static int clone_verify_area(struct file *file, loff_t pos, u64 len, bool write) 1735 * Don't operate on ranges the page cache doe << 1736 * LFS limits. If pos is under the limit it << 1737 * exceeds the limit we return -EFBIG. << 1738 */ << 1739 int generic_write_check_limits(struct file *f << 1740 { 1646 { 1741 struct inode *inode = file->f_mapping !! 1647 struct inode *inode = file_inode(file); 1742 loff_t max_size = inode->i_sb->s_maxb << 1743 loff_t limit = rlimit(RLIMIT_FSIZE); << 1744 1648 1745 if (limit != RLIM_INFINITY) { !! 1649 if (unlikely(pos < 0)) 1746 if (pos >= limit) { !! 1650 return -EINVAL; 1747 send_sig(SIGXFSZ, cur << 1748 return -EFBIG; << 1749 } << 1750 *count = min(*count, limit - << 1751 } << 1752 1651 1753 if (!(file->f_flags & O_LARGEFILE)) !! 1652 if (unlikely((loff_t) (pos + len) < 0)) 1754 max_size = MAX_NON_LFS; !! 1653 return -EINVAL; 1755 1654 1756 if (unlikely(pos >= max_size)) !! 1655 if (unlikely(inode->i_flctx && mandatory_lock(inode))) { 1757 return -EFBIG; !! 1656 loff_t end = len ? pos + len - 1 : OFFSET_MAX; >> 1657 int retval; 1758 1658 1759 *count = min(*count, max_size - pos); !! 1659 retval = locks_mandatory_area(inode, file, pos, end, >> 1660 write ? F_WRLCK : F_RDLCK); >> 1661 if (retval < 0) >> 1662 return retval; >> 1663 } 1760 1664 1761 return 0; !! 1665 return security_file_permission(file, write ? MAY_WRITE : MAY_READ); 1762 } 1666 } 1763 EXPORT_SYMBOL_GPL(generic_write_check_limits) << 1764 1667 1765 /* Like generic_write_checks(), but takes siz !! 1668 /* 1766 int generic_write_checks_count(struct kiocb * !! 1669 * Check that the two inodes are eligible for cloning, the ranges make 1767 { !! 1670 * sense, and then flush all dirty data. Caller must ensure that the 1768 struct file *file = iocb->ki_filp; !! 1671 * inodes have been locked against any other modifications. 1769 struct inode *inode = file->f_mapping !! 1672 * >> 1673 * Returns: 0 for "nothing to clone", 1 for "something to clone", or >> 1674 * the usual negative error code. >> 1675 */ >> 1676 int vfs_clone_file_prep_inodes(struct inode *inode_in, loff_t pos_in, >> 1677 struct inode *inode_out, loff_t pos_out, >> 1678 u64 *len, bool is_dedupe) >> 1679 { >> 1680 loff_t bs = inode_out->i_sb->s_blocksize; >> 1681 loff_t blen; >> 1682 loff_t isize; >> 1683 bool same_inode = (inode_in == inode_out); >> 1684 int ret; >> 1685 >> 1686 /* Don't touch certain kinds of inodes */ >> 1687 if (IS_IMMUTABLE(inode_out)) >> 1688 return -EPERM; 1770 1689 1771 if (IS_SWAPFILE(inode)) !! 1690 if (IS_SWAPFILE(inode_in) || IS_SWAPFILE(inode_out)) 1772 return -ETXTBSY; 1691 return -ETXTBSY; 1773 1692 1774 if (!*count) !! 1693 /* Don't reflink dirs, pipes, sockets... */ >> 1694 if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode)) >> 1695 return -EISDIR; >> 1696 if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode)) >> 1697 return -EINVAL; >> 1698 >> 1699 /* Are we going all the way to the end? */ >> 1700 isize = i_size_read(inode_in); >> 1701 if (isize == 0) 1775 return 0; 1702 return 0; 1776 1703 1777 if (iocb->ki_flags & IOCB_APPEND) !! 1704 /* Zero length dedupe exits immediately; reflink goes to EOF. */ 1778 iocb->ki_pos = i_size_read(in !! 1705 if (*len == 0) { >> 1706 if (is_dedupe || pos_in == isize) >> 1707 return 0; >> 1708 if (pos_in > isize) >> 1709 return -EINVAL; >> 1710 *len = isize - pos_in; >> 1711 } 1779 1712 1780 if ((iocb->ki_flags & IOCB_NOWAIT) && !! 1713 /* Ensure offsets don't wrap and the input is inside i_size */ 1781 !((iocb->ki_flags & IOCB_DIRECT) !! 1714 if (pos_in + *len < pos_in || pos_out + *len < pos_out || 1782 (file->f_op->fop_flags & FOP_BU !! 1715 pos_in + *len > isize) 1783 return -EINVAL; 1716 return -EINVAL; 1784 1717 1785 return generic_write_check_limits(ioc !! 1718 /* Don't allow dedupe past EOF in the dest file */ 1786 } !! 1719 if (is_dedupe) { 1787 EXPORT_SYMBOL(generic_write_checks_count); !! 1720 loff_t disize; 1788 1721 1789 /* !! 1722 disize = i_size_read(inode_out); 1790 * Performs necessary checks before doing a w !! 1723 if (pos_out >= disize || pos_out + *len > disize) 1791 * !! 1724 return -EINVAL; 1792 * Can adjust writing position or amount of b !! 1725 } 1793 * Returns appropriate error code that caller !! 1726 1794 * zero in case that write should be allowed. !! 1727 /* If we're linking to EOF, continue to the block boundary. */ 1795 */ !! 1728 if (pos_in + *len == isize) 1796 ssize_t generic_write_checks(struct kiocb *io !! 1729 blen = ALIGN(isize, bs) - pos_in; 1797 { !! 1730 else 1798 loff_t count = iov_iter_count(from); !! 1731 blen = *len; 1799 int ret; !! 1732 >> 1733 /* Only reflink if we're aligned to block boundaries */ >> 1734 if (!IS_ALIGNED(pos_in, bs) || !IS_ALIGNED(pos_in + blen, bs) || >> 1735 !IS_ALIGNED(pos_out, bs) || !IS_ALIGNED(pos_out + blen, bs)) >> 1736 return -EINVAL; >> 1737 >> 1738 /* Don't allow overlapped reflink within the same file */ >> 1739 if (same_inode) { >> 1740 if (pos_out + blen > pos_in && pos_out < pos_in + blen) >> 1741 return -EINVAL; >> 1742 } >> 1743 >> 1744 /* Wait for the completion of any pending IOs on both files */ >> 1745 inode_dio_wait(inode_in); >> 1746 if (!same_inode) >> 1747 inode_dio_wait(inode_out); 1800 1748 1801 ret = generic_write_checks_count(iocb !! 1749 ret = filemap_write_and_wait_range(inode_in->i_mapping, >> 1750 pos_in, pos_in + *len - 1); 1802 if (ret) 1751 if (ret) 1803 return ret; 1752 return ret; 1804 1753 1805 iov_iter_truncate(from, count); !! 1754 ret = filemap_write_and_wait_range(inode_out->i_mapping, 1806 return iov_iter_count(from); !! 1755 pos_out, pos_out + *len - 1); >> 1756 if (ret) >> 1757 return ret; >> 1758 >> 1759 /* >> 1760 * Check that the extents are the same. >> 1761 */ >> 1762 if (is_dedupe) { >> 1763 bool is_same = false; >> 1764 >> 1765 ret = vfs_dedupe_file_range_compare(inode_in, pos_in, >> 1766 inode_out, pos_out, *len, &is_same); >> 1767 if (ret) >> 1768 return ret; >> 1769 if (!is_same) >> 1770 return -EBADE; >> 1771 } >> 1772 >> 1773 return 1; 1807 } 1774 } 1808 EXPORT_SYMBOL(generic_write_checks); !! 1775 EXPORT_SYMBOL(vfs_clone_file_prep_inodes); 1809 1776 1810 /* !! 1777 int vfs_clone_file_range(struct file *file_in, loff_t pos_in, 1811 * Performs common checks before doing a file !! 1778 struct file *file_out, loff_t pos_out, u64 len) 1812 * from @file_in to @file_out. << 1813 */ << 1814 int generic_file_rw_checks(struct file *file_ << 1815 { 1779 { 1816 struct inode *inode_in = file_inode(f 1780 struct inode *inode_in = file_inode(file_in); 1817 struct inode *inode_out = file_inode( 1781 struct inode *inode_out = file_inode(file_out); >> 1782 int ret; 1818 1783 1819 /* Don't copy dirs, pipes, sockets... << 1820 if (S_ISDIR(inode_in->i_mode) || S_IS 1784 if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode)) 1821 return -EISDIR; 1785 return -EISDIR; 1822 if (!S_ISREG(inode_in->i_mode) || !S_ 1786 if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode)) 1823 return -EINVAL; 1787 return -EINVAL; 1824 1788 >> 1789 /* >> 1790 * FICLONE/FICLONERANGE ioctls enforce that src and dest files are on >> 1791 * the same mount. Practically, they only need to be on the same file >> 1792 * system. >> 1793 */ >> 1794 if (inode_in->i_sb != inode_out->i_sb) >> 1795 return -EXDEV; >> 1796 1825 if (!(file_in->f_mode & FMODE_READ) | 1797 if (!(file_in->f_mode & FMODE_READ) || 1826 !(file_out->f_mode & FMODE_WRITE) 1798 !(file_out->f_mode & FMODE_WRITE) || 1827 (file_out->f_flags & O_APPEND)) 1799 (file_out->f_flags & O_APPEND)) 1828 return -EBADF; 1800 return -EBADF; 1829 1801 >> 1802 if (!file_in->f_op->clone_file_range) >> 1803 return -EOPNOTSUPP; >> 1804 >> 1805 ret = clone_verify_area(file_in, pos_in, len, false); >> 1806 if (ret) >> 1807 return ret; >> 1808 >> 1809 ret = clone_verify_area(file_out, pos_out, len, true); >> 1810 if (ret) >> 1811 return ret; >> 1812 >> 1813 if (pos_in + len > i_size_read(inode_in)) >> 1814 return -EINVAL; >> 1815 >> 1816 ret = file_in->f_op->clone_file_range(file_in, pos_in, >> 1817 file_out, pos_out, len); >> 1818 if (!ret) { >> 1819 fsnotify_access(file_in); >> 1820 fsnotify_modify(file_out); >> 1821 } >> 1822 >> 1823 return ret; >> 1824 } >> 1825 EXPORT_SYMBOL(vfs_clone_file_range); >> 1826 >> 1827 /* >> 1828 * Read a page's worth of file data into the page cache. Return the page >> 1829 * locked. >> 1830 */ >> 1831 static struct page *vfs_dedupe_get_page(struct inode *inode, loff_t offset) >> 1832 { >> 1833 struct address_space *mapping; >> 1834 struct page *page; >> 1835 pgoff_t n; >> 1836 >> 1837 n = offset >> PAGE_SHIFT; >> 1838 mapping = inode->i_mapping; >> 1839 page = read_mapping_page(mapping, n, NULL); >> 1840 if (IS_ERR(page)) >> 1841 return page; >> 1842 if (!PageUptodate(page)) { >> 1843 put_page(page); >> 1844 return ERR_PTR(-EIO); >> 1845 } >> 1846 lock_page(page); >> 1847 return page; >> 1848 } >> 1849 >> 1850 /* >> 1851 * Compare extents of two files to see if they are the same. >> 1852 * Caller must have locked both inodes to prevent write races. >> 1853 */ >> 1854 int vfs_dedupe_file_range_compare(struct inode *src, loff_t srcoff, >> 1855 struct inode *dest, loff_t destoff, >> 1856 loff_t len, bool *is_same) >> 1857 { >> 1858 loff_t src_poff; >> 1859 loff_t dest_poff; >> 1860 void *src_addr; >> 1861 void *dest_addr; >> 1862 struct page *src_page; >> 1863 struct page *dest_page; >> 1864 loff_t cmp_len; >> 1865 bool same; >> 1866 int error; >> 1867 >> 1868 error = -EINVAL; >> 1869 same = true; >> 1870 while (len) { >> 1871 src_poff = srcoff & (PAGE_SIZE - 1); >> 1872 dest_poff = destoff & (PAGE_SIZE - 1); >> 1873 cmp_len = min(PAGE_SIZE - src_poff, >> 1874 PAGE_SIZE - dest_poff); >> 1875 cmp_len = min(cmp_len, len); >> 1876 if (cmp_len <= 0) >> 1877 goto out_error; >> 1878 >> 1879 src_page = vfs_dedupe_get_page(src, srcoff); >> 1880 if (IS_ERR(src_page)) { >> 1881 error = PTR_ERR(src_page); >> 1882 goto out_error; >> 1883 } >> 1884 dest_page = vfs_dedupe_get_page(dest, destoff); >> 1885 if (IS_ERR(dest_page)) { >> 1886 error = PTR_ERR(dest_page); >> 1887 unlock_page(src_page); >> 1888 put_page(src_page); >> 1889 goto out_error; >> 1890 } >> 1891 src_addr = kmap_atomic(src_page); >> 1892 dest_addr = kmap_atomic(dest_page); >> 1893 >> 1894 flush_dcache_page(src_page); >> 1895 flush_dcache_page(dest_page); >> 1896 >> 1897 if (memcmp(src_addr + src_poff, dest_addr + dest_poff, cmp_len)) >> 1898 same = false; >> 1899 >> 1900 kunmap_atomic(dest_addr); >> 1901 kunmap_atomic(src_addr); >> 1902 unlock_page(dest_page); >> 1903 unlock_page(src_page); >> 1904 put_page(dest_page); >> 1905 put_page(src_page); >> 1906 >> 1907 if (!same) >> 1908 break; >> 1909 >> 1910 srcoff += cmp_len; >> 1911 destoff += cmp_len; >> 1912 len -= cmp_len; >> 1913 } >> 1914 >> 1915 *is_same = same; 1830 return 0; 1916 return 0; >> 1917 >> 1918 out_error: >> 1919 return error; 1831 } 1920 } >> 1921 EXPORT_SYMBOL(vfs_dedupe_file_range_compare); 1832 1922 1833 bool generic_atomic_write_valid(struct iov_it !! 1923 int vfs_dedupe_file_range(struct file *file, struct file_dedupe_range *same) 1834 { 1924 { 1835 size_t len = iov_iter_count(iter); !! 1925 struct file_dedupe_range_info *info; >> 1926 struct inode *src = file_inode(file); >> 1927 u64 off; >> 1928 u64 len; >> 1929 int i; >> 1930 int ret; >> 1931 bool is_admin = capable(CAP_SYS_ADMIN); >> 1932 u16 count = same->dest_count; >> 1933 struct file *dst_file; >> 1934 loff_t dst_off; >> 1935 ssize_t deduped; >> 1936 >> 1937 if (!(file->f_mode & FMODE_READ)) >> 1938 return -EINVAL; >> 1939 >> 1940 if (same->reserved1 || same->reserved2) >> 1941 return -EINVAL; >> 1942 >> 1943 off = same->src_offset; >> 1944 len = same->src_length; >> 1945 >> 1946 ret = -EISDIR; >> 1947 if (S_ISDIR(src->i_mode)) >> 1948 goto out; >> 1949 >> 1950 ret = -EINVAL; >> 1951 if (!S_ISREG(src->i_mode)) >> 1952 goto out; >> 1953 >> 1954 ret = clone_verify_area(file, off, len, false); >> 1955 if (ret < 0) >> 1956 goto out; >> 1957 ret = 0; >> 1958 >> 1959 if (off + len > i_size_read(src)) >> 1960 return -EINVAL; >> 1961 >> 1962 /* pre-format output fields to sane values */ >> 1963 for (i = 0; i < count; i++) { >> 1964 same->info[i].bytes_deduped = 0ULL; >> 1965 same->info[i].status = FILE_DEDUPE_RANGE_SAME; >> 1966 } >> 1967 >> 1968 for (i = 0, info = same->info; i < count; i++, info++) { >> 1969 struct inode *dst; >> 1970 struct fd dst_fd = fdget(info->dest_fd); 1836 1971 1837 if (!iter_is_ubuf(iter)) !! 1972 dst_file = dst_fd.file; 1838 return false; !! 1973 if (!dst_file) { >> 1974 info->status = -EBADF; >> 1975 goto next_loop; >> 1976 } >> 1977 dst = file_inode(dst_file); >> 1978 >> 1979 ret = mnt_want_write_file(dst_file); >> 1980 if (ret) { >> 1981 info->status = ret; >> 1982 goto next_loop; >> 1983 } >> 1984 >> 1985 dst_off = info->dest_offset; >> 1986 ret = clone_verify_area(dst_file, dst_off, len, true); >> 1987 if (ret < 0) { >> 1988 info->status = ret; >> 1989 goto next_file; >> 1990 } >> 1991 ret = 0; >> 1992 >> 1993 if (info->reserved) { >> 1994 info->status = -EINVAL; >> 1995 } else if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) { >> 1996 info->status = -EINVAL; >> 1997 } else if (file->f_path.mnt != dst_file->f_path.mnt) { >> 1998 info->status = -EXDEV; >> 1999 } else if (S_ISDIR(dst->i_mode)) { >> 2000 info->status = -EISDIR; >> 2001 } else if (dst_file->f_op->dedupe_file_range == NULL) { >> 2002 info->status = -EINVAL; >> 2003 } else { >> 2004 deduped = dst_file->f_op->dedupe_file_range(file, off, >> 2005 len, dst_file, >> 2006 info->dest_offset); >> 2007 if (deduped == -EBADE) >> 2008 info->status = FILE_DEDUPE_RANGE_DIFFERS; >> 2009 else if (deduped < 0) >> 2010 info->status = deduped; >> 2011 else >> 2012 info->bytes_deduped += deduped; >> 2013 } 1839 2014 1840 if (!is_power_of_2(len)) !! 2015 next_file: 1841 return false; !! 2016 mnt_drop_write_file(dst_file); >> 2017 next_loop: >> 2018 fdput(dst_fd); 1842 2019 1843 if (!IS_ALIGNED(pos, len)) !! 2020 if (fatal_signal_pending(current)) 1844 return false; !! 2021 goto out; >> 2022 } 1845 2023 1846 return true; !! 2024 out: >> 2025 return ret; 1847 } 2026 } >> 2027 EXPORT_SYMBOL(vfs_dedupe_file_range); 1848 2028
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.