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