Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/read_write.c |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 5 | */ |
| 6 | |
| 7 | #include <linux/slab.h> |
| 8 | #include <linux/stat.h> |
| 9 | #include <linux/fcntl.h> |
| 10 | #include <linux/file.h> |
| 11 | #include <linux/uio.h> |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 12 | #include <linux/fsnotify.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/security.h> |
Paul Gortmaker | 630d9c4 | 2011-11-16 23:57:37 -0500 | [diff] [blame] | 14 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/syscalls.h> |
Linus Torvalds | e28cc71 | 2006-01-04 16:20:40 -0800 | [diff] [blame] | 16 | #include <linux/pagemap.h> |
Jens Axboe | d6b29d7 | 2007-06-04 09:59:47 +0200 | [diff] [blame] | 17 | #include <linux/splice.h> |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 18 | #include "read_write.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
| 20 | #include <asm/uaccess.h> |
| 21 | #include <asm/unistd.h> |
| 22 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 23 | const struct file_operations generic_ro_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | .llseek = generic_file_llseek, |
Badari Pulavarty | 543ade1 | 2006-09-30 23:28:48 -0700 | [diff] [blame] | 25 | .read = do_sync_read, |
| 26 | .aio_read = generic_file_aio_read, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | .mmap = generic_file_readonly_mmap, |
Jens Axboe | 534f2aa | 2007-06-01 14:52:37 +0200 | [diff] [blame] | 28 | .splice_read = generic_file_splice_read, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | EXPORT_SYMBOL(generic_ro_fops); |
| 32 | |
Al Viro | cccb5a1 | 2010-12-17 07:44:05 -0500 | [diff] [blame] | 33 | static inline int unsigned_offsets(struct file *file) |
KAMEZAWA Hiroyuki | 4a3956c | 2010-10-01 14:20:22 -0700 | [diff] [blame] | 34 | { |
Al Viro | cccb5a1 | 2010-12-17 07:44:05 -0500 | [diff] [blame] | 35 | return file->f_mode & FMODE_UNSIGNED_OFFSET; |
KAMEZAWA Hiroyuki | 4a3956c | 2010-10-01 14:20:22 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Andi Kleen | ef3d0fd | 2011-09-15 16:06:48 -0700 | [diff] [blame] | 38 | static loff_t lseek_execute(struct file *file, struct inode *inode, |
| 39 | loff_t offset, loff_t maxsize) |
| 40 | { |
| 41 | if (offset < 0 && !unsigned_offsets(file)) |
| 42 | return -EINVAL; |
| 43 | if (offset > maxsize) |
| 44 | return -EINVAL; |
| 45 | |
| 46 | if (offset != file->f_pos) { |
| 47 | file->f_pos = offset; |
| 48 | file->f_version = 0; |
| 49 | } |
| 50 | return offset; |
| 51 | } |
| 52 | |
Christoph Hellwig | 3a8cff4 | 2008-08-11 15:37:17 +0200 | [diff] [blame] | 53 | /** |
Andi Kleen | 5760495 | 2011-09-15 16:06:50 -0700 | [diff] [blame] | 54 | * generic_file_llseek_size - generic llseek implementation for regular files |
Christoph Hellwig | 3a8cff4 | 2008-08-11 15:37:17 +0200 | [diff] [blame] | 55 | * @file: file structure to seek on |
| 56 | * @offset: file offset to seek to |
| 57 | * @origin: type of seek |
Eric Sandeen | e8b96eb | 2012-04-30 13:11:29 -0500 | [diff] [blame] | 58 | * @size: max size of this file in file system |
| 59 | * @eof: offset used for SEEK_END position |
Christoph Hellwig | 3a8cff4 | 2008-08-11 15:37:17 +0200 | [diff] [blame] | 60 | * |
Andi Kleen | 5760495 | 2011-09-15 16:06:50 -0700 | [diff] [blame] | 61 | * This is a variant of generic_file_llseek that allows passing in a custom |
Eric Sandeen | e8b96eb | 2012-04-30 13:11:29 -0500 | [diff] [blame] | 62 | * maximum file size and a custom EOF position, for e.g. hashed directories |
Andi Kleen | ef3d0fd | 2011-09-15 16:06:48 -0700 | [diff] [blame] | 63 | * |
| 64 | * Synchronization: |
Andi Kleen | 5760495 | 2011-09-15 16:06:50 -0700 | [diff] [blame] | 65 | * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms) |
Andi Kleen | ef3d0fd | 2011-09-15 16:06:48 -0700 | [diff] [blame] | 66 | * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes. |
| 67 | * read/writes behave like SEEK_SET against seeks. |
Christoph Hellwig | 3a8cff4 | 2008-08-11 15:37:17 +0200 | [diff] [blame] | 68 | */ |
Andi Kleen | 9465efc | 2008-06-27 11:05:24 +0200 | [diff] [blame] | 69 | loff_t |
Andi Kleen | 5760495 | 2011-09-15 16:06:50 -0700 | [diff] [blame] | 70 | generic_file_llseek_size(struct file *file, loff_t offset, int origin, |
Eric Sandeen | e8b96eb | 2012-04-30 13:11:29 -0500 | [diff] [blame] | 71 | loff_t maxsize, loff_t eof) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | struct inode *inode = file->f_mapping->host; |
| 74 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | switch (origin) { |
Christoph Hellwig | 3a8cff4 | 2008-08-11 15:37:17 +0200 | [diff] [blame] | 76 | case SEEK_END: |
Eric Sandeen | e8b96eb | 2012-04-30 13:11:29 -0500 | [diff] [blame] | 77 | offset += eof; |
Christoph Hellwig | 3a8cff4 | 2008-08-11 15:37:17 +0200 | [diff] [blame] | 78 | break; |
| 79 | case SEEK_CUR: |
Alain Knaff | 5b6f1eb | 2008-11-10 17:08:08 -0800 | [diff] [blame] | 80 | /* |
| 81 | * Here we special-case the lseek(fd, 0, SEEK_CUR) |
| 82 | * position-querying operation. Avoid rewriting the "same" |
| 83 | * f_pos value back to the file because a concurrent read(), |
| 84 | * write() or lseek() might have altered it |
| 85 | */ |
| 86 | if (offset == 0) |
| 87 | return file->f_pos; |
Andi Kleen | ef3d0fd | 2011-09-15 16:06:48 -0700 | [diff] [blame] | 88 | /* |
| 89 | * f_lock protects against read/modify/write race with other |
| 90 | * SEEK_CURs. Note that parallel writes and reads behave |
| 91 | * like SEEK_SET. |
| 92 | */ |
| 93 | spin_lock(&file->f_lock); |
| 94 | offset = lseek_execute(file, inode, file->f_pos + offset, |
Andi Kleen | 5760495 | 2011-09-15 16:06:50 -0700 | [diff] [blame] | 95 | maxsize); |
Andi Kleen | ef3d0fd | 2011-09-15 16:06:48 -0700 | [diff] [blame] | 96 | spin_unlock(&file->f_lock); |
| 97 | return offset; |
Josef Bacik | 982d816 | 2011-07-18 13:21:35 -0400 | [diff] [blame] | 98 | case SEEK_DATA: |
| 99 | /* |
| 100 | * In the generic case the entire file is data, so as long as |
| 101 | * offset isn't at the end of the file then the offset is data. |
| 102 | */ |
Eric Sandeen | e8b96eb | 2012-04-30 13:11:29 -0500 | [diff] [blame] | 103 | if (offset >= eof) |
Josef Bacik | 982d816 | 2011-07-18 13:21:35 -0400 | [diff] [blame] | 104 | return -ENXIO; |
| 105 | break; |
| 106 | case SEEK_HOLE: |
| 107 | /* |
| 108 | * There is a virtual hole at the end of the file, so as long as |
| 109 | * offset isn't i_size or larger, return i_size. |
| 110 | */ |
Eric Sandeen | e8b96eb | 2012-04-30 13:11:29 -0500 | [diff] [blame] | 111 | if (offset >= eof) |
Josef Bacik | 982d816 | 2011-07-18 13:21:35 -0400 | [diff] [blame] | 112 | return -ENXIO; |
Eric Sandeen | e8b96eb | 2012-04-30 13:11:29 -0500 | [diff] [blame] | 113 | offset = eof; |
Josef Bacik | 982d816 | 2011-07-18 13:21:35 -0400 | [diff] [blame] | 114 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } |
Christoph Hellwig | 3a8cff4 | 2008-08-11 15:37:17 +0200 | [diff] [blame] | 116 | |
Andi Kleen | 5760495 | 2011-09-15 16:06:50 -0700 | [diff] [blame] | 117 | return lseek_execute(file, inode, offset, maxsize); |
| 118 | } |
| 119 | EXPORT_SYMBOL(generic_file_llseek_size); |
| 120 | |
| 121 | /** |
| 122 | * generic_file_llseek - generic llseek implementation for regular files |
| 123 | * @file: file structure to seek on |
| 124 | * @offset: file offset to seek to |
| 125 | * @origin: type of seek |
| 126 | * |
| 127 | * This is a generic implemenation of ->llseek useable for all normal local |
| 128 | * filesystems. It just updates the file offset to the value specified by |
| 129 | * @offset and @origin under i_mutex. |
| 130 | */ |
| 131 | loff_t generic_file_llseek(struct file *file, loff_t offset, int origin) |
| 132 | { |
| 133 | struct inode *inode = file->f_mapping->host; |
| 134 | |
| 135 | return generic_file_llseek_size(file, offset, origin, |
Eric Sandeen | e8b96eb | 2012-04-30 13:11:29 -0500 | [diff] [blame] | 136 | inode->i_sb->s_maxbytes, |
| 137 | i_size_read(inode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | } |
Andi Kleen | 9465efc | 2008-06-27 11:05:24 +0200 | [diff] [blame] | 139 | EXPORT_SYMBOL(generic_file_llseek); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
jan Blunck | ae6afc3 | 2010-05-26 14:44:48 -0700 | [diff] [blame] | 141 | /** |
| 142 | * noop_llseek - No Operation Performed llseek implementation |
| 143 | * @file: file structure to seek on |
| 144 | * @offset: file offset to seek to |
| 145 | * @origin: type of seek |
| 146 | * |
| 147 | * This is an implementation of ->llseek useable for the rare special case when |
| 148 | * userspace expects the seek to succeed but the (device) file is actually not |
| 149 | * able to perform the seek. In this case you use noop_llseek() instead of |
| 150 | * falling back to the default implementation of ->llseek. |
| 151 | */ |
| 152 | loff_t noop_llseek(struct file *file, loff_t offset, int origin) |
| 153 | { |
| 154 | return file->f_pos; |
| 155 | } |
| 156 | EXPORT_SYMBOL(noop_llseek); |
| 157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | loff_t no_llseek(struct file *file, loff_t offset, int origin) |
| 159 | { |
| 160 | return -ESPIPE; |
| 161 | } |
| 162 | EXPORT_SYMBOL(no_llseek); |
| 163 | |
| 164 | loff_t default_llseek(struct file *file, loff_t offset, int origin) |
| 165 | { |
Josef Bacik | 982d816 | 2011-07-18 13:21:35 -0400 | [diff] [blame] | 166 | struct inode *inode = file->f_path.dentry->d_inode; |
David Sterba | 16abef0 | 2008-04-22 15:09:22 +0200 | [diff] [blame] | 167 | loff_t retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
Josef Bacik | 982d816 | 2011-07-18 13:21:35 -0400 | [diff] [blame] | 169 | mutex_lock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | switch (origin) { |
Chris Snook | 7b8e892 | 2007-05-08 00:24:13 -0700 | [diff] [blame] | 171 | case SEEK_END: |
Josef Bacik | 982d816 | 2011-07-18 13:21:35 -0400 | [diff] [blame] | 172 | offset += i_size_read(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | break; |
Chris Snook | 7b8e892 | 2007-05-08 00:24:13 -0700 | [diff] [blame] | 174 | case SEEK_CUR: |
Alain Knaff | 5b6f1eb | 2008-11-10 17:08:08 -0800 | [diff] [blame] | 175 | if (offset == 0) { |
| 176 | retval = file->f_pos; |
| 177 | goto out; |
| 178 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | offset += file->f_pos; |
Josef Bacik | 982d816 | 2011-07-18 13:21:35 -0400 | [diff] [blame] | 180 | break; |
| 181 | case SEEK_DATA: |
| 182 | /* |
| 183 | * In the generic case the entire file is data, so as |
| 184 | * long as offset isn't at the end of the file then the |
| 185 | * offset is data. |
| 186 | */ |
Dan Carpenter | bacb2d8 | 2011-07-26 17:25:20 +0300 | [diff] [blame] | 187 | if (offset >= inode->i_size) { |
| 188 | retval = -ENXIO; |
| 189 | goto out; |
| 190 | } |
Josef Bacik | 982d816 | 2011-07-18 13:21:35 -0400 | [diff] [blame] | 191 | break; |
| 192 | case SEEK_HOLE: |
| 193 | /* |
| 194 | * There is a virtual hole at the end of the file, so |
| 195 | * as long as offset isn't i_size or larger, return |
| 196 | * i_size. |
| 197 | */ |
Dan Carpenter | bacb2d8 | 2011-07-26 17:25:20 +0300 | [diff] [blame] | 198 | if (offset >= inode->i_size) { |
| 199 | retval = -ENXIO; |
| 200 | goto out; |
| 201 | } |
Josef Bacik | 982d816 | 2011-07-18 13:21:35 -0400 | [diff] [blame] | 202 | offset = inode->i_size; |
| 203 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | } |
| 205 | retval = -EINVAL; |
Al Viro | cccb5a1 | 2010-12-17 07:44:05 -0500 | [diff] [blame] | 206 | if (offset >= 0 || unsigned_offsets(file)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | if (offset != file->f_pos) { |
| 208 | file->f_pos = offset; |
| 209 | file->f_version = 0; |
| 210 | } |
| 211 | retval = offset; |
| 212 | } |
Alain Knaff | 5b6f1eb | 2008-11-10 17:08:08 -0800 | [diff] [blame] | 213 | out: |
Josef Bacik | 982d816 | 2011-07-18 13:21:35 -0400 | [diff] [blame] | 214 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | return retval; |
| 216 | } |
| 217 | EXPORT_SYMBOL(default_llseek); |
| 218 | |
| 219 | loff_t vfs_llseek(struct file *file, loff_t offset, int origin) |
| 220 | { |
| 221 | loff_t (*fn)(struct file *, loff_t, int); |
| 222 | |
| 223 | fn = no_llseek; |
| 224 | if (file->f_mode & FMODE_LSEEK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | if (file->f_op && file->f_op->llseek) |
| 226 | fn = file->f_op->llseek; |
| 227 | } |
| 228 | return fn(file, offset, origin); |
| 229 | } |
| 230 | EXPORT_SYMBOL(vfs_llseek); |
| 231 | |
Heiko Carstens | 003d7ab | 2009-01-14 14:14:21 +0100 | [diff] [blame] | 232 | SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, origin) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | { |
| 234 | off_t retval; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 235 | struct fd f = fdget(fd); |
| 236 | if (!f.file) |
| 237 | return -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
| 239 | retval = -EINVAL; |
Chris Snook | 1ae7075 | 2007-05-08 00:24:15 -0700 | [diff] [blame] | 240 | if (origin <= SEEK_MAX) { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 241 | loff_t res = vfs_llseek(f.file, offset, origin); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | retval = res; |
| 243 | if (res != (loff_t)retval) |
| 244 | retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */ |
| 245 | } |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 246 | fdput(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | return retval; |
| 248 | } |
| 249 | |
| 250 | #ifdef __ARCH_WANT_SYS_LLSEEK |
Heiko Carstens | 003d7ab | 2009-01-14 14:14:21 +0100 | [diff] [blame] | 251 | SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high, |
| 252 | unsigned long, offset_low, loff_t __user *, result, |
| 253 | unsigned int, origin) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | { |
| 255 | int retval; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 256 | struct fd f = fdget(fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | loff_t offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 259 | if (!f.file) |
| 260 | return -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
| 262 | retval = -EINVAL; |
Chris Snook | 1ae7075 | 2007-05-08 00:24:15 -0700 | [diff] [blame] | 263 | if (origin > SEEK_MAX) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | goto out_putf; |
| 265 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 266 | offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | origin); |
| 268 | |
| 269 | retval = (int)offset; |
| 270 | if (offset >= 0) { |
| 271 | retval = -EFAULT; |
| 272 | if (!copy_to_user(result, &offset, sizeof(offset))) |
| 273 | retval = 0; |
| 274 | } |
| 275 | out_putf: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 276 | fdput(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | return retval; |
| 278 | } |
| 279 | #endif |
| 280 | |
KAMEZAWA Hiroyuki | 4a3956c | 2010-10-01 14:20:22 -0700 | [diff] [blame] | 281 | |
Linus Torvalds | e28cc71 | 2006-01-04 16:20:40 -0800 | [diff] [blame] | 282 | /* |
| 283 | * rw_verify_area doesn't like huge counts. We limit |
| 284 | * them to something that fits in "int" so that others |
| 285 | * won't have to do range checks all the time. |
| 286 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | int rw_verify_area(int read_write, struct file *file, loff_t *ppos, size_t count) |
| 288 | { |
| 289 | struct inode *inode; |
| 290 | loff_t pos; |
James Morris | c43e259 | 2008-01-12 22:05:48 +1100 | [diff] [blame] | 291 | int retval = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
Eric Dumazet | 163da95 | 2007-02-12 00:52:24 -0800 | [diff] [blame] | 293 | inode = file->f_path.dentry->d_inode; |
Linus Torvalds | e28cc71 | 2006-01-04 16:20:40 -0800 | [diff] [blame] | 294 | if (unlikely((ssize_t) count < 0)) |
James Morris | c43e259 | 2008-01-12 22:05:48 +1100 | [diff] [blame] | 295 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | pos = *ppos; |
Al Viro | cccb5a1 | 2010-12-17 07:44:05 -0500 | [diff] [blame] | 297 | if (unlikely(pos < 0)) { |
| 298 | if (!unsigned_offsets(file)) |
| 299 | return retval; |
| 300 | if (count >= -pos) /* both values are in 0..LLONG_MAX */ |
| 301 | return -EOVERFLOW; |
| 302 | } else if (unlikely((loff_t) (pos + count) < 0)) { |
| 303 | if (!unsigned_offsets(file)) |
KAMEZAWA Hiroyuki | 4a3956c | 2010-10-01 14:20:22 -0700 | [diff] [blame] | 304 | return retval; |
| 305 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | |
Pavel Emelyanov | a16877c | 2007-10-01 14:41:11 -0700 | [diff] [blame] | 307 | if (unlikely(inode->i_flock && mandatory_lock(inode))) { |
James Morris | c43e259 | 2008-01-12 22:05:48 +1100 | [diff] [blame] | 308 | retval = locks_mandatory_area( |
Linus Torvalds | e28cc71 | 2006-01-04 16:20:40 -0800 | [diff] [blame] | 309 | read_write == READ ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE, |
| 310 | inode, file, pos, count); |
| 311 | if (retval < 0) |
| 312 | return retval; |
| 313 | } |
James Morris | c43e259 | 2008-01-12 22:05:48 +1100 | [diff] [blame] | 314 | retval = security_file_permission(file, |
| 315 | read_write == READ ? MAY_READ : MAY_WRITE); |
| 316 | if (retval) |
| 317 | return retval; |
Linus Torvalds | e28cc71 | 2006-01-04 16:20:40 -0800 | [diff] [blame] | 318 | return count > MAX_RW_COUNT ? MAX_RW_COUNT : count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Benjamin LaHaise | 63e6880 | 2005-06-23 00:10:27 -0700 | [diff] [blame] | 321 | static void wait_on_retry_sync_kiocb(struct kiocb *iocb) |
| 322 | { |
| 323 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 324 | if (!kiocbIsKicked(iocb)) |
| 325 | schedule(); |
| 326 | else |
| 327 | kiocbClearKicked(iocb); |
| 328 | __set_current_state(TASK_RUNNING); |
| 329 | } |
| 330 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | ssize_t do_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos) |
| 332 | { |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 333 | struct iovec iov = { .iov_base = buf, .iov_len = len }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | struct kiocb kiocb; |
| 335 | ssize_t ret; |
| 336 | |
| 337 | init_sync_kiocb(&kiocb, filp); |
| 338 | kiocb.ki_pos = *ppos; |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 339 | kiocb.ki_left = len; |
David Howells | 61964eb | 2010-03-24 17:09:19 +0000 | [diff] [blame] | 340 | kiocb.ki_nbytes = len; |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 341 | |
| 342 | for (;;) { |
| 343 | ret = filp->f_op->aio_read(&kiocb, &iov, 1, kiocb.ki_pos); |
| 344 | if (ret != -EIOCBRETRY) |
| 345 | break; |
Benjamin LaHaise | 63e6880 | 2005-06-23 00:10:27 -0700 | [diff] [blame] | 346 | wait_on_retry_sync_kiocb(&kiocb); |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 347 | } |
Benjamin LaHaise | 63e6880 | 2005-06-23 00:10:27 -0700 | [diff] [blame] | 348 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | if (-EIOCBQUEUED == ret) |
| 350 | ret = wait_on_sync_kiocb(&kiocb); |
| 351 | *ppos = kiocb.ki_pos; |
| 352 | return ret; |
| 353 | } |
| 354 | |
| 355 | EXPORT_SYMBOL(do_sync_read); |
| 356 | |
| 357 | ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos) |
| 358 | { |
| 359 | ssize_t ret; |
| 360 | |
| 361 | if (!(file->f_mode & FMODE_READ)) |
| 362 | return -EBADF; |
| 363 | if (!file->f_op || (!file->f_op->read && !file->f_op->aio_read)) |
| 364 | return -EINVAL; |
| 365 | if (unlikely(!access_ok(VERIFY_WRITE, buf, count))) |
| 366 | return -EFAULT; |
| 367 | |
| 368 | ret = rw_verify_area(READ, file, pos, count); |
Linus Torvalds | e28cc71 | 2006-01-04 16:20:40 -0800 | [diff] [blame] | 369 | if (ret >= 0) { |
| 370 | count = ret; |
James Morris | c43e259 | 2008-01-12 22:05:48 +1100 | [diff] [blame] | 371 | if (file->f_op->read) |
| 372 | ret = file->f_op->read(file, buf, count, pos); |
| 373 | else |
| 374 | ret = do_sync_read(file, buf, count, pos); |
| 375 | if (ret > 0) { |
Eric Paris | 2a12a9d | 2009-12-17 21:24:21 -0500 | [diff] [blame] | 376 | fsnotify_access(file); |
James Morris | c43e259 | 2008-01-12 22:05:48 +1100 | [diff] [blame] | 377 | add_rchar(current, ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | } |
James Morris | c43e259 | 2008-01-12 22:05:48 +1100 | [diff] [blame] | 379 | inc_syscr(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | return ret; |
| 383 | } |
| 384 | |
| 385 | EXPORT_SYMBOL(vfs_read); |
| 386 | |
| 387 | ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos) |
| 388 | { |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 389 | struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | struct kiocb kiocb; |
| 391 | ssize_t ret; |
| 392 | |
| 393 | init_sync_kiocb(&kiocb, filp); |
| 394 | kiocb.ki_pos = *ppos; |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 395 | kiocb.ki_left = len; |
David Howells | 61964eb | 2010-03-24 17:09:19 +0000 | [diff] [blame] | 396 | kiocb.ki_nbytes = len; |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 397 | |
| 398 | for (;;) { |
| 399 | ret = filp->f_op->aio_write(&kiocb, &iov, 1, kiocb.ki_pos); |
| 400 | if (ret != -EIOCBRETRY) |
| 401 | break; |
Benjamin LaHaise | 63e6880 | 2005-06-23 00:10:27 -0700 | [diff] [blame] | 402 | wait_on_retry_sync_kiocb(&kiocb); |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 403 | } |
Benjamin LaHaise | 63e6880 | 2005-06-23 00:10:27 -0700 | [diff] [blame] | 404 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | if (-EIOCBQUEUED == ret) |
| 406 | ret = wait_on_sync_kiocb(&kiocb); |
| 407 | *ppos = kiocb.ki_pos; |
| 408 | return ret; |
| 409 | } |
| 410 | |
| 411 | EXPORT_SYMBOL(do_sync_write); |
| 412 | |
| 413 | ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos) |
| 414 | { |
| 415 | ssize_t ret; |
| 416 | |
| 417 | if (!(file->f_mode & FMODE_WRITE)) |
| 418 | return -EBADF; |
| 419 | if (!file->f_op || (!file->f_op->write && !file->f_op->aio_write)) |
| 420 | return -EINVAL; |
| 421 | if (unlikely(!access_ok(VERIFY_READ, buf, count))) |
| 422 | return -EFAULT; |
| 423 | |
| 424 | ret = rw_verify_area(WRITE, file, pos, count); |
Linus Torvalds | e28cc71 | 2006-01-04 16:20:40 -0800 | [diff] [blame] | 425 | if (ret >= 0) { |
| 426 | count = ret; |
James Morris | c43e259 | 2008-01-12 22:05:48 +1100 | [diff] [blame] | 427 | if (file->f_op->write) |
| 428 | ret = file->f_op->write(file, buf, count, pos); |
| 429 | else |
| 430 | ret = do_sync_write(file, buf, count, pos); |
| 431 | if (ret > 0) { |
Eric Paris | 2a12a9d | 2009-12-17 21:24:21 -0500 | [diff] [blame] | 432 | fsnotify_modify(file); |
James Morris | c43e259 | 2008-01-12 22:05:48 +1100 | [diff] [blame] | 433 | add_wchar(current, ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | } |
James Morris | c43e259 | 2008-01-12 22:05:48 +1100 | [diff] [blame] | 435 | inc_syscw(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | return ret; |
| 439 | } |
| 440 | |
| 441 | EXPORT_SYMBOL(vfs_write); |
| 442 | |
| 443 | static inline loff_t file_pos_read(struct file *file) |
| 444 | { |
| 445 | return file->f_pos; |
| 446 | } |
| 447 | |
| 448 | static inline void file_pos_write(struct file *file, loff_t pos) |
| 449 | { |
| 450 | file->f_pos = pos; |
| 451 | } |
| 452 | |
Heiko Carstens | 3cdad42 | 2009-01-14 14:14:22 +0100 | [diff] [blame] | 453 | SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 455 | struct fd f = fdget(fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | ssize_t ret = -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 458 | if (f.file) { |
| 459 | loff_t pos = file_pos_read(f.file); |
| 460 | ret = vfs_read(f.file, buf, count, &pos); |
| 461 | file_pos_write(f.file, pos); |
| 462 | fdput(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | return ret; |
| 465 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | |
Heiko Carstens | 3cdad42 | 2009-01-14 14:14:22 +0100 | [diff] [blame] | 467 | SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf, |
| 468 | size_t, count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 470 | struct fd f = fdget(fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | ssize_t ret = -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 473 | if (f.file) { |
| 474 | loff_t pos = file_pos_read(f.file); |
| 475 | ret = vfs_write(f.file, buf, count, &pos); |
| 476 | file_pos_write(f.file, pos); |
| 477 | fdput(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | return ret; |
| 481 | } |
| 482 | |
Heiko Carstens | 6673e0c | 2009-01-14 14:14:02 +0100 | [diff] [blame] | 483 | SYSCALL_DEFINE(pread64)(unsigned int fd, char __user *buf, |
| 484 | size_t count, loff_t pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 486 | struct fd f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | ssize_t ret = -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | |
| 489 | if (pos < 0) |
| 490 | return -EINVAL; |
| 491 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 492 | f = fdget(fd); |
| 493 | if (f.file) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | ret = -ESPIPE; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 495 | if (f.file->f_mode & FMODE_PREAD) |
| 496 | ret = vfs_read(f.file, buf, count, &pos); |
| 497 | fdput(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | return ret; |
| 501 | } |
Heiko Carstens | 6673e0c | 2009-01-14 14:14:02 +0100 | [diff] [blame] | 502 | #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS |
| 503 | asmlinkage long SyS_pread64(long fd, long buf, long count, loff_t pos) |
| 504 | { |
| 505 | return SYSC_pread64((unsigned int) fd, (char __user *) buf, |
| 506 | (size_t) count, pos); |
| 507 | } |
| 508 | SYSCALL_ALIAS(sys_pread64, SyS_pread64); |
| 509 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | |
Heiko Carstens | 6673e0c | 2009-01-14 14:14:02 +0100 | [diff] [blame] | 511 | SYSCALL_DEFINE(pwrite64)(unsigned int fd, const char __user *buf, |
| 512 | size_t count, loff_t pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 514 | struct fd f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | ssize_t ret = -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | |
| 517 | if (pos < 0) |
| 518 | return -EINVAL; |
| 519 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 520 | f = fdget(fd); |
| 521 | if (f.file) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | ret = -ESPIPE; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 523 | if (f.file->f_mode & FMODE_PWRITE) |
| 524 | ret = vfs_write(f.file, buf, count, &pos); |
| 525 | fdput(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | return ret; |
| 529 | } |
Heiko Carstens | 6673e0c | 2009-01-14 14:14:02 +0100 | [diff] [blame] | 530 | #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS |
| 531 | asmlinkage long SyS_pwrite64(long fd, long buf, long count, loff_t pos) |
| 532 | { |
| 533 | return SYSC_pwrite64((unsigned int) fd, (const char __user *) buf, |
| 534 | (size_t) count, pos); |
| 535 | } |
| 536 | SYSCALL_ALIAS(sys_pwrite64, SyS_pwrite64); |
| 537 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | |
| 539 | /* |
| 540 | * Reduce an iovec's length in-place. Return the resulting number of segments |
| 541 | */ |
| 542 | unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to) |
| 543 | { |
| 544 | unsigned long seg = 0; |
| 545 | size_t len = 0; |
| 546 | |
| 547 | while (seg < nr_segs) { |
| 548 | seg++; |
| 549 | if (len + iov->iov_len >= to) { |
| 550 | iov->iov_len = to - len; |
| 551 | break; |
| 552 | } |
| 553 | len += iov->iov_len; |
| 554 | iov++; |
| 555 | } |
| 556 | return seg; |
| 557 | } |
Eric Sandeen | 1929552 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 558 | EXPORT_SYMBOL(iov_shorten); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 560 | ssize_t do_sync_readv_writev(struct file *filp, const struct iovec *iov, |
| 561 | unsigned long nr_segs, size_t len, loff_t *ppos, iov_fn_t fn) |
| 562 | { |
| 563 | struct kiocb kiocb; |
| 564 | ssize_t ret; |
| 565 | |
| 566 | init_sync_kiocb(&kiocb, filp); |
| 567 | kiocb.ki_pos = *ppos; |
| 568 | kiocb.ki_left = len; |
| 569 | kiocb.ki_nbytes = len; |
| 570 | |
| 571 | for (;;) { |
| 572 | ret = fn(&kiocb, iov, nr_segs, kiocb.ki_pos); |
| 573 | if (ret != -EIOCBRETRY) |
| 574 | break; |
| 575 | wait_on_retry_sync_kiocb(&kiocb); |
| 576 | } |
| 577 | |
| 578 | if (ret == -EIOCBQUEUED) |
| 579 | ret = wait_on_sync_kiocb(&kiocb); |
| 580 | *ppos = kiocb.ki_pos; |
| 581 | return ret; |
| 582 | } |
| 583 | |
| 584 | /* Do it by hand, with file-ops */ |
| 585 | ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov, |
| 586 | unsigned long nr_segs, loff_t *ppos, io_fn_t fn) |
| 587 | { |
| 588 | struct iovec *vector = iov; |
| 589 | ssize_t ret = 0; |
| 590 | |
| 591 | while (nr_segs > 0) { |
| 592 | void __user *base; |
| 593 | size_t len; |
| 594 | ssize_t nr; |
| 595 | |
| 596 | base = vector->iov_base; |
| 597 | len = vector->iov_len; |
| 598 | vector++; |
| 599 | nr_segs--; |
| 600 | |
| 601 | nr = fn(filp, base, len, ppos); |
| 602 | |
| 603 | if (nr < 0) { |
| 604 | if (!ret) |
| 605 | ret = nr; |
| 606 | break; |
| 607 | } |
| 608 | ret += nr; |
| 609 | if (nr != len) |
| 610 | break; |
| 611 | } |
| 612 | |
| 613 | return ret; |
| 614 | } |
| 615 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | /* A write operation does a read from user space and vice versa */ |
| 617 | #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ) |
| 618 | |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 619 | ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector, |
| 620 | unsigned long nr_segs, unsigned long fast_segs, |
| 621 | struct iovec *fast_pointer, |
Christopher Yeoh | ac34ebb | 2012-05-31 16:26:42 -0700 | [diff] [blame] | 622 | struct iovec **ret_pointer) |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 623 | { |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 624 | unsigned long seg; |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 625 | ssize_t ret; |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 626 | struct iovec *iov = fast_pointer; |
| 627 | |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 628 | /* |
| 629 | * SuS says "The readv() function *may* fail if the iovcnt argument |
| 630 | * was less than or equal to 0, or greater than {IOV_MAX}. Linux has |
| 631 | * traditionally returned zero for zero segments, so... |
| 632 | */ |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 633 | if (nr_segs == 0) { |
| 634 | ret = 0; |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 635 | goto out; |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 636 | } |
| 637 | |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 638 | /* |
| 639 | * First get the "struct iovec" from user memory and |
| 640 | * verify all the pointers |
| 641 | */ |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 642 | if (nr_segs > UIO_MAXIOV) { |
| 643 | ret = -EINVAL; |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 644 | goto out; |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 645 | } |
| 646 | if (nr_segs > fast_segs) { |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 647 | iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL); |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 648 | if (iov == NULL) { |
| 649 | ret = -ENOMEM; |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 650 | goto out; |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 651 | } |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 652 | } |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 653 | if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) { |
| 654 | ret = -EFAULT; |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 655 | goto out; |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 656 | } |
| 657 | |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 658 | /* |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 659 | * According to the Single Unix Specification we should return EINVAL |
| 660 | * if an element length is < 0 when cast to ssize_t or if the |
| 661 | * total length would overflow the ssize_t return value of the |
| 662 | * system call. |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 663 | * |
| 664 | * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the |
| 665 | * overflow case. |
| 666 | */ |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 667 | ret = 0; |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 668 | for (seg = 0; seg < nr_segs; seg++) { |
| 669 | void __user *buf = iov[seg].iov_base; |
| 670 | ssize_t len = (ssize_t)iov[seg].iov_len; |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 671 | |
| 672 | /* see if we we're about to use an invalid len or if |
| 673 | * it's about to overflow ssize_t */ |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 674 | if (len < 0) { |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 675 | ret = -EINVAL; |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 676 | goto out; |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 677 | } |
Christopher Yeoh | ac34ebb | 2012-05-31 16:26:42 -0700 | [diff] [blame] | 678 | if (type >= 0 |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 679 | && unlikely(!access_ok(vrfy_dir(type), buf, len))) { |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 680 | ret = -EFAULT; |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 681 | goto out; |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 682 | } |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 683 | if (len > MAX_RW_COUNT - ret) { |
| 684 | len = MAX_RW_COUNT - ret; |
| 685 | iov[seg].iov_len = len; |
| 686 | } |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 687 | ret += len; |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 688 | } |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 689 | out: |
| 690 | *ret_pointer = iov; |
| 691 | return ret; |
| 692 | } |
| 693 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | static ssize_t do_readv_writev(int type, struct file *file, |
| 695 | const struct iovec __user * uvector, |
| 696 | unsigned long nr_segs, loff_t *pos) |
| 697 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | size_t tot_len; |
| 699 | struct iovec iovstack[UIO_FASTIOV]; |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 700 | struct iovec *iov = iovstack; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | ssize_t ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | io_fn_t fn; |
| 703 | iov_fn_t fnv; |
| 704 | |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 705 | if (!file->f_op) { |
| 706 | ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | goto out; |
| 708 | } |
| 709 | |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 710 | ret = rw_copy_check_uvector(type, uvector, nr_segs, |
Christopher Yeoh | ac34ebb | 2012-05-31 16:26:42 -0700 | [diff] [blame] | 711 | ARRAY_SIZE(iovstack), iovstack, &iov); |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 712 | if (ret <= 0) |
| 713 | goto out; |
| 714 | |
| 715 | tot_len = ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | ret = rw_verify_area(type, file, pos, tot_len); |
Linus Torvalds | e28cc71 | 2006-01-04 16:20:40 -0800 | [diff] [blame] | 717 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | goto out; |
| 719 | |
| 720 | fnv = NULL; |
| 721 | if (type == READ) { |
| 722 | fn = file->f_op->read; |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 723 | fnv = file->f_op->aio_read; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | } else { |
| 725 | fn = (io_fn_t)file->f_op->write; |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 726 | fnv = file->f_op->aio_write; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | } |
| 728 | |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 729 | if (fnv) |
| 730 | ret = do_sync_readv_writev(file, iov, nr_segs, tot_len, |
| 731 | pos, fnv); |
| 732 | else |
| 733 | ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | out: |
| 736 | if (iov != iovstack) |
| 737 | kfree(iov); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 738 | if ((ret + (type == READ)) > 0) { |
| 739 | if (type == READ) |
Eric Paris | 2a12a9d | 2009-12-17 21:24:21 -0500 | [diff] [blame] | 740 | fsnotify_access(file); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 741 | else |
Eric Paris | 2a12a9d | 2009-12-17 21:24:21 -0500 | [diff] [blame] | 742 | fsnotify_modify(file); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 743 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | ssize_t vfs_readv(struct file *file, const struct iovec __user *vec, |
| 748 | unsigned long vlen, loff_t *pos) |
| 749 | { |
| 750 | if (!(file->f_mode & FMODE_READ)) |
| 751 | return -EBADF; |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 752 | if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | return -EINVAL; |
| 754 | |
| 755 | return do_readv_writev(READ, file, vec, vlen, pos); |
| 756 | } |
| 757 | |
| 758 | EXPORT_SYMBOL(vfs_readv); |
| 759 | |
| 760 | ssize_t vfs_writev(struct file *file, const struct iovec __user *vec, |
| 761 | unsigned long vlen, loff_t *pos) |
| 762 | { |
| 763 | if (!(file->f_mode & FMODE_WRITE)) |
| 764 | return -EBADF; |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 765 | if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | return -EINVAL; |
| 767 | |
| 768 | return do_readv_writev(WRITE, file, vec, vlen, pos); |
| 769 | } |
| 770 | |
| 771 | EXPORT_SYMBOL(vfs_writev); |
| 772 | |
Heiko Carstens | 3cdad42 | 2009-01-14 14:14:22 +0100 | [diff] [blame] | 773 | SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec, |
| 774 | unsigned long, vlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 776 | struct fd f = fdget(fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | ssize_t ret = -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 779 | if (f.file) { |
| 780 | loff_t pos = file_pos_read(f.file); |
| 781 | ret = vfs_readv(f.file, vec, vlen, &pos); |
| 782 | file_pos_write(f.file, pos); |
| 783 | fdput(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | if (ret > 0) |
Alexey Dobriyan | 4b98d11 | 2007-02-10 01:46:45 -0800 | [diff] [blame] | 787 | add_rchar(current, ret); |
| 788 | inc_syscr(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | return ret; |
| 790 | } |
| 791 | |
Heiko Carstens | 3cdad42 | 2009-01-14 14:14:22 +0100 | [diff] [blame] | 792 | SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec, |
| 793 | unsigned long, vlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 795 | struct fd f = fdget(fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | ssize_t ret = -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 798 | if (f.file) { |
| 799 | loff_t pos = file_pos_read(f.file); |
| 800 | ret = vfs_writev(f.file, vec, vlen, &pos); |
| 801 | file_pos_write(f.file, pos); |
| 802 | fdput(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | if (ret > 0) |
Alexey Dobriyan | 4b98d11 | 2007-02-10 01:46:45 -0800 | [diff] [blame] | 806 | add_wchar(current, ret); |
| 807 | inc_syscw(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | return ret; |
| 809 | } |
| 810 | |
Linus Torvalds | 601cc11 | 2009-04-03 08:03:22 -0700 | [diff] [blame] | 811 | static inline loff_t pos_from_hilo(unsigned long high, unsigned long low) |
Gerd Hoffmann | f3554f4 | 2009-04-02 16:59:23 -0700 | [diff] [blame] | 812 | { |
Linus Torvalds | 601cc11 | 2009-04-03 08:03:22 -0700 | [diff] [blame] | 813 | #define HALF_LONG_BITS (BITS_PER_LONG / 2) |
| 814 | return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low; |
| 815 | } |
| 816 | |
| 817 | SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec, |
| 818 | unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h) |
| 819 | { |
| 820 | loff_t pos = pos_from_hilo(pos_h, pos_l); |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 821 | struct fd f; |
Gerd Hoffmann | f3554f4 | 2009-04-02 16:59:23 -0700 | [diff] [blame] | 822 | ssize_t ret = -EBADF; |
Gerd Hoffmann | f3554f4 | 2009-04-02 16:59:23 -0700 | [diff] [blame] | 823 | |
| 824 | if (pos < 0) |
| 825 | return -EINVAL; |
| 826 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 827 | f = fdget(fd); |
| 828 | if (f.file) { |
Gerd Hoffmann | f3554f4 | 2009-04-02 16:59:23 -0700 | [diff] [blame] | 829 | ret = -ESPIPE; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 830 | if (f.file->f_mode & FMODE_PREAD) |
| 831 | ret = vfs_readv(f.file, vec, vlen, &pos); |
| 832 | fdput(f); |
Gerd Hoffmann | f3554f4 | 2009-04-02 16:59:23 -0700 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | if (ret > 0) |
| 836 | add_rchar(current, ret); |
| 837 | inc_syscr(current); |
| 838 | return ret; |
| 839 | } |
| 840 | |
| 841 | SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec, |
Linus Torvalds | 601cc11 | 2009-04-03 08:03:22 -0700 | [diff] [blame] | 842 | unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h) |
Gerd Hoffmann | f3554f4 | 2009-04-02 16:59:23 -0700 | [diff] [blame] | 843 | { |
Linus Torvalds | 601cc11 | 2009-04-03 08:03:22 -0700 | [diff] [blame] | 844 | loff_t pos = pos_from_hilo(pos_h, pos_l); |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 845 | struct fd f; |
Gerd Hoffmann | f3554f4 | 2009-04-02 16:59:23 -0700 | [diff] [blame] | 846 | ssize_t ret = -EBADF; |
Gerd Hoffmann | f3554f4 | 2009-04-02 16:59:23 -0700 | [diff] [blame] | 847 | |
| 848 | if (pos < 0) |
| 849 | return -EINVAL; |
| 850 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 851 | f = fdget(fd); |
| 852 | if (f.file) { |
Gerd Hoffmann | f3554f4 | 2009-04-02 16:59:23 -0700 | [diff] [blame] | 853 | ret = -ESPIPE; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 854 | if (f.file->f_mode & FMODE_PWRITE) |
| 855 | ret = vfs_writev(f.file, vec, vlen, &pos); |
| 856 | fdput(f); |
Gerd Hoffmann | f3554f4 | 2009-04-02 16:59:23 -0700 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | if (ret > 0) |
| 860 | add_wchar(current, ret); |
| 861 | inc_syscw(current); |
| 862 | return ret; |
| 863 | } |
| 864 | |
Catalin Marinas | 8f9c011 | 2012-09-19 12:01:52 +0100 | [diff] [blame] | 865 | ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos, size_t count, |
| 866 | loff_t max) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 868 | struct fd in, out; |
| 869 | struct inode *in_inode, *out_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | loff_t pos; |
| 871 | ssize_t retval; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 872 | int fl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | |
| 874 | /* |
| 875 | * Get input file, and verify that it is ok.. |
| 876 | */ |
| 877 | retval = -EBADF; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 878 | in = fdget(in_fd); |
| 879 | if (!in.file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | goto out; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 881 | if (!(in.file->f_mode & FMODE_READ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | goto fput_in; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | retval = -ESPIPE; |
| 884 | if (!ppos) |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 885 | ppos = &in.file->f_pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | else |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 887 | if (!(in.file->f_mode & FMODE_PREAD)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | goto fput_in; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 889 | retval = rw_verify_area(READ, in.file, ppos, count); |
Linus Torvalds | e28cc71 | 2006-01-04 16:20:40 -0800 | [diff] [blame] | 890 | if (retval < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | goto fput_in; |
Linus Torvalds | e28cc71 | 2006-01-04 16:20:40 -0800 | [diff] [blame] | 892 | count = retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | /* |
| 895 | * Get output file, and verify that it is ok.. |
| 896 | */ |
| 897 | retval = -EBADF; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 898 | out = fdget(out_fd); |
| 899 | if (!out.file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | goto fput_in; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 901 | if (!(out.file->f_mode & FMODE_WRITE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | goto fput_out; |
| 903 | retval = -EINVAL; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 904 | in_inode = in.file->f_path.dentry->d_inode; |
| 905 | out_inode = out.file->f_path.dentry->d_inode; |
| 906 | retval = rw_verify_area(WRITE, out.file, &out.file->f_pos, count); |
Linus Torvalds | e28cc71 | 2006-01-04 16:20:40 -0800 | [diff] [blame] | 907 | if (retval < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | goto fput_out; |
Linus Torvalds | e28cc71 | 2006-01-04 16:20:40 -0800 | [diff] [blame] | 909 | count = retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | if (!max) |
| 912 | max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes); |
| 913 | |
| 914 | pos = *ppos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | if (unlikely(pos + count > max)) { |
| 916 | retval = -EOVERFLOW; |
| 917 | if (pos >= max) |
| 918 | goto fput_out; |
| 919 | count = max - pos; |
| 920 | } |
| 921 | |
Jens Axboe | d96e6e7 | 2007-06-11 12:18:52 +0200 | [diff] [blame] | 922 | fl = 0; |
Jens Axboe | 534f2aa | 2007-06-01 14:52:37 +0200 | [diff] [blame] | 923 | #if 0 |
Jens Axboe | d96e6e7 | 2007-06-11 12:18:52 +0200 | [diff] [blame] | 924 | /* |
| 925 | * We need to debate whether we can enable this or not. The |
| 926 | * man page documents EAGAIN return for the output at least, |
| 927 | * and the application is arguably buggy if it doesn't expect |
| 928 | * EAGAIN on a non-blocking file descriptor. |
| 929 | */ |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 930 | if (in.file->f_flags & O_NONBLOCK) |
Jens Axboe | d96e6e7 | 2007-06-11 12:18:52 +0200 | [diff] [blame] | 931 | fl = SPLICE_F_NONBLOCK; |
Jens Axboe | 534f2aa | 2007-06-01 14:52:37 +0200 | [diff] [blame] | 932 | #endif |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 933 | retval = do_splice_direct(in.file, ppos, out.file, count, fl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | |
| 935 | if (retval > 0) { |
Alexey Dobriyan | 4b98d11 | 2007-02-10 01:46:45 -0800 | [diff] [blame] | 936 | add_rchar(current, retval); |
| 937 | add_wchar(current, retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | |
Alexey Dobriyan | 4b98d11 | 2007-02-10 01:46:45 -0800 | [diff] [blame] | 940 | inc_syscr(current); |
| 941 | inc_syscw(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | if (*ppos > max) |
| 943 | retval = -EOVERFLOW; |
| 944 | |
| 945 | fput_out: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 946 | fdput(out); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | fput_in: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 948 | fdput(in); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | out: |
| 950 | return retval; |
| 951 | } |
| 952 | |
Heiko Carstens | 002c897 | 2009-01-14 14:14:18 +0100 | [diff] [blame] | 953 | SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | { |
| 955 | loff_t pos; |
| 956 | off_t off; |
| 957 | ssize_t ret; |
| 958 | |
| 959 | if (offset) { |
| 960 | if (unlikely(get_user(off, offset))) |
| 961 | return -EFAULT; |
| 962 | pos = off; |
| 963 | ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS); |
| 964 | if (unlikely(put_user(pos, offset))) |
| 965 | return -EFAULT; |
| 966 | return ret; |
| 967 | } |
| 968 | |
| 969 | return do_sendfile(out_fd, in_fd, NULL, count, 0); |
| 970 | } |
| 971 | |
Heiko Carstens | 002c897 | 2009-01-14 14:14:18 +0100 | [diff] [blame] | 972 | SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | { |
| 974 | loff_t pos; |
| 975 | ssize_t ret; |
| 976 | |
| 977 | if (offset) { |
| 978 | if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t)))) |
| 979 | return -EFAULT; |
| 980 | ret = do_sendfile(out_fd, in_fd, &pos, count, 0); |
| 981 | if (unlikely(put_user(pos, offset))) |
| 982 | return -EFAULT; |
| 983 | return ret; |
| 984 | } |
| 985 | |
| 986 | return do_sendfile(out_fd, in_fd, NULL, count, 0); |
| 987 | } |