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