blob: 672b187def6296a1de19bba537047a1e1010f9ae [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Love0eeca282005-07-12 17:06:03 -040012#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/security.h>
14#include <linux/module.h>
15#include <linux/syscalls.h>
Linus Torvaldse28cc712006-01-04 16:20:40 -080016#include <linux/pagemap.h>
Jens Axboed6b29d72007-06-04 09:59:47 +020017#include <linux/splice.h>
Badari Pulavartyee0b3e62006-09-30 23:28:47 -070018#include "read_write.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#include <asm/uaccess.h>
21#include <asm/unistd.h>
22
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080023const struct file_operations generic_ro_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 .llseek = generic_file_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -070025 .read = do_sync_read,
26 .aio_read = generic_file_aio_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 .mmap = generic_file_readonly_mmap,
Jens Axboe534f2aa2007-06-01 14:52:37 +020028 .splice_read = generic_file_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -070029};
30
31EXPORT_SYMBOL(generic_ro_fops);
32
Al Virocccb5a12010-12-17 07:44:05 -050033static inline int unsigned_offsets(struct file *file)
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -070034{
Al Virocccb5a12010-12-17 07:44:05 -050035 return file->f_mode & FMODE_UNSIGNED_OFFSET;
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -070036}
37
Andi Kleenef3d0fd2011-09-15 16:06:48 -070038static loff_t lseek_execute(struct file *file, struct inode *inode,
39 loff_t offset, loff_t maxsize)
40{
41 if (offset < 0 && !unsigned_offsets(file))
42 return -EINVAL;
43 if (offset > maxsize)
44 return -EINVAL;
45
46 if (offset != file->f_pos) {
47 file->f_pos = offset;
48 file->f_version = 0;
49 }
50 return offset;
51}
52
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020053/**
Andi Kleenef3d0fd2011-09-15 16:06:48 -070054 * generic_file_llseek - generic llseek implementation for regular files
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020055 * @file: file structure to seek on
56 * @offset: file offset to seek to
57 * @origin: type of seek
58 *
Andi Kleenef3d0fd2011-09-15 16:06:48 -070059 * This is a generic implemenation of ->llseek usable for all normal local
60 * filesystems. It just updates the file offset to the value specified by
61 * @offset and @origin under i_mutex.
62 *
63 * Synchronization:
64 * SEEK_SET is unsynchronized (but atomic on 64bit platforms)
65 * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes.
66 * read/writes behave like SEEK_SET against seeks.
67 * SEEK_END
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020068 */
Andi Kleen9465efc2008-06-27 11:05:24 +020069loff_t
Andi Kleenef3d0fd2011-09-15 16:06:48 -070070generic_file_llseek(struct file *file, loff_t offset, int origin)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 struct inode *inode = file->f_mapping->host;
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 switch (origin) {
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020075 case SEEK_END:
Andi Kleenef3d0fd2011-09-15 16:06:48 -070076 offset += i_size_read(inode);
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020077 break;
78 case SEEK_CUR:
Alain Knaff5b6f1eb2008-11-10 17:08:08 -080079 /*
80 * Here we special-case the lseek(fd, 0, SEEK_CUR)
81 * position-querying operation. Avoid rewriting the "same"
82 * f_pos value back to the file because a concurrent read(),
83 * write() or lseek() might have altered it
84 */
85 if (offset == 0)
86 return file->f_pos;
Andi Kleenef3d0fd2011-09-15 16:06:48 -070087 /*
88 * f_lock protects against read/modify/write race with other
89 * SEEK_CURs. Note that parallel writes and reads behave
90 * like SEEK_SET.
91 */
92 spin_lock(&file->f_lock);
93 offset = lseek_execute(file, inode, file->f_pos + offset,
94 inode->i_sb->s_maxbytes);
95 spin_unlock(&file->f_lock);
96 return offset;
Josef Bacik982d8162011-07-18 13:21:35 -040097 case SEEK_DATA:
98 /*
99 * In the generic case the entire file is data, so as long as
100 * offset isn't at the end of the file then the offset is data.
101 */
Andi Kleenef3d0fd2011-09-15 16:06:48 -0700102 if (offset >= i_size_read(inode))
Josef Bacik982d8162011-07-18 13:21:35 -0400103 return -ENXIO;
104 break;
105 case SEEK_HOLE:
106 /*
107 * There is a virtual hole at the end of the file, so as long as
108 * offset isn't i_size or larger, return i_size.
109 */
Andi Kleenef3d0fd2011-09-15 16:06:48 -0700110 if (offset >= i_size_read(inode))
Josef Bacik982d8162011-07-18 13:21:35 -0400111 return -ENXIO;
Andi Kleenef3d0fd2011-09-15 16:06:48 -0700112 offset = i_size_read(inode);
Josef Bacik982d8162011-07-18 13:21:35 -0400113 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 }
Christoph Hellwig3a8cff42008-08-11 15:37:17 +0200115
Andi Kleenef3d0fd2011-09-15 16:06:48 -0700116 return lseek_execute(file, inode, offset, inode->i_sb->s_maxbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
Andi Kleen9465efc2008-06-27 11:05:24 +0200118EXPORT_SYMBOL(generic_file_llseek);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
jan Blunckae6afc32010-05-26 14:44:48 -0700120/**
121 * noop_llseek - No Operation Performed llseek implementation
122 * @file: file structure to seek on
123 * @offset: file offset to seek to
124 * @origin: type of seek
125 *
126 * This is an implementation of ->llseek useable for the rare special case when
127 * userspace expects the seek to succeed but the (device) file is actually not
128 * able to perform the seek. In this case you use noop_llseek() instead of
129 * falling back to the default implementation of ->llseek.
130 */
131loff_t noop_llseek(struct file *file, loff_t offset, int origin)
132{
133 return file->f_pos;
134}
135EXPORT_SYMBOL(noop_llseek);
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137loff_t no_llseek(struct file *file, loff_t offset, int origin)
138{
139 return -ESPIPE;
140}
141EXPORT_SYMBOL(no_llseek);
142
143loff_t default_llseek(struct file *file, loff_t offset, int origin)
144{
Josef Bacik982d8162011-07-18 13:21:35 -0400145 struct inode *inode = file->f_path.dentry->d_inode;
David Sterba16abef02008-04-22 15:09:22 +0200146 loff_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Josef Bacik982d8162011-07-18 13:21:35 -0400148 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 switch (origin) {
Chris Snook7b8e8922007-05-08 00:24:13 -0700150 case SEEK_END:
Josef Bacik982d8162011-07-18 13:21:35 -0400151 offset += i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 break;
Chris Snook7b8e8922007-05-08 00:24:13 -0700153 case SEEK_CUR:
Alain Knaff5b6f1eb2008-11-10 17:08:08 -0800154 if (offset == 0) {
155 retval = file->f_pos;
156 goto out;
157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 offset += file->f_pos;
Josef Bacik982d8162011-07-18 13:21:35 -0400159 break;
160 case SEEK_DATA:
161 /*
162 * In the generic case the entire file is data, so as
163 * long as offset isn't at the end of the file then the
164 * offset is data.
165 */
Dan Carpenterbacb2d82011-07-26 17:25:20 +0300166 if (offset >= inode->i_size) {
167 retval = -ENXIO;
168 goto out;
169 }
Josef Bacik982d8162011-07-18 13:21:35 -0400170 break;
171 case SEEK_HOLE:
172 /*
173 * There is a virtual hole at the end of the file, so
174 * as long as offset isn't i_size or larger, return
175 * i_size.
176 */
Dan Carpenterbacb2d82011-07-26 17:25:20 +0300177 if (offset >= inode->i_size) {
178 retval = -ENXIO;
179 goto out;
180 }
Josef Bacik982d8162011-07-18 13:21:35 -0400181 offset = inode->i_size;
182 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
184 retval = -EINVAL;
Al Virocccb5a12010-12-17 07:44:05 -0500185 if (offset >= 0 || unsigned_offsets(file)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 if (offset != file->f_pos) {
187 file->f_pos = offset;
188 file->f_version = 0;
189 }
190 retval = offset;
191 }
Alain Knaff5b6f1eb2008-11-10 17:08:08 -0800192out:
Josef Bacik982d8162011-07-18 13:21:35 -0400193 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return retval;
195}
196EXPORT_SYMBOL(default_llseek);
197
198loff_t vfs_llseek(struct file *file, loff_t offset, int origin)
199{
200 loff_t (*fn)(struct file *, loff_t, int);
201
202 fn = no_llseek;
203 if (file->f_mode & FMODE_LSEEK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 if (file->f_op && file->f_op->llseek)
205 fn = file->f_op->llseek;
206 }
207 return fn(file, offset, origin);
208}
209EXPORT_SYMBOL(vfs_llseek);
210
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100211SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, origin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 off_t retval;
214 struct file * file;
215 int fput_needed;
216
217 retval = -EBADF;
218 file = fget_light(fd, &fput_needed);
219 if (!file)
220 goto bad;
221
222 retval = -EINVAL;
Chris Snook1ae70752007-05-08 00:24:15 -0700223 if (origin <= SEEK_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 loff_t res = vfs_llseek(file, offset, origin);
225 retval = res;
226 if (res != (loff_t)retval)
227 retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
228 }
229 fput_light(file, fput_needed);
230bad:
231 return retval;
232}
233
234#ifdef __ARCH_WANT_SYS_LLSEEK
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100235SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
236 unsigned long, offset_low, loff_t __user *, result,
237 unsigned int, origin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
239 int retval;
240 struct file * file;
241 loff_t offset;
242 int fput_needed;
243
244 retval = -EBADF;
245 file = fget_light(fd, &fput_needed);
246 if (!file)
247 goto bad;
248
249 retval = -EINVAL;
Chris Snook1ae70752007-05-08 00:24:15 -0700250 if (origin > SEEK_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 goto out_putf;
252
253 offset = vfs_llseek(file, ((loff_t) offset_high << 32) | offset_low,
254 origin);
255
256 retval = (int)offset;
257 if (offset >= 0) {
258 retval = -EFAULT;
259 if (!copy_to_user(result, &offset, sizeof(offset)))
260 retval = 0;
261 }
262out_putf:
263 fput_light(file, fput_needed);
264bad:
265 return retval;
266}
267#endif
268
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700269
Linus Torvaldse28cc712006-01-04 16:20:40 -0800270/*
271 * rw_verify_area doesn't like huge counts. We limit
272 * them to something that fits in "int" so that others
273 * won't have to do range checks all the time.
274 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275int rw_verify_area(int read_write, struct file *file, loff_t *ppos, size_t count)
276{
277 struct inode *inode;
278 loff_t pos;
James Morrisc43e2592008-01-12 22:05:48 +1100279 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Eric Dumazet163da952007-02-12 00:52:24 -0800281 inode = file->f_path.dentry->d_inode;
Linus Torvaldse28cc712006-01-04 16:20:40 -0800282 if (unlikely((ssize_t) count < 0))
James Morrisc43e2592008-01-12 22:05:48 +1100283 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 pos = *ppos;
Al Virocccb5a12010-12-17 07:44:05 -0500285 if (unlikely(pos < 0)) {
286 if (!unsigned_offsets(file))
287 return retval;
288 if (count >= -pos) /* both values are in 0..LLONG_MAX */
289 return -EOVERFLOW;
290 } else if (unlikely((loff_t) (pos + count) < 0)) {
291 if (!unsigned_offsets(file))
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700292 return retval;
293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Pavel Emelyanova16877c2007-10-01 14:41:11 -0700295 if (unlikely(inode->i_flock && mandatory_lock(inode))) {
James Morrisc43e2592008-01-12 22:05:48 +1100296 retval = locks_mandatory_area(
Linus Torvaldse28cc712006-01-04 16:20:40 -0800297 read_write == READ ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE,
298 inode, file, pos, count);
299 if (retval < 0)
300 return retval;
301 }
James Morrisc43e2592008-01-12 22:05:48 +1100302 retval = security_file_permission(file,
303 read_write == READ ? MAY_READ : MAY_WRITE);
304 if (retval)
305 return retval;
Linus Torvaldse28cc712006-01-04 16:20:40 -0800306 return count > MAX_RW_COUNT ? MAX_RW_COUNT : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
Benjamin LaHaise63e68802005-06-23 00:10:27 -0700309static void wait_on_retry_sync_kiocb(struct kiocb *iocb)
310{
311 set_current_state(TASK_UNINTERRUPTIBLE);
312 if (!kiocbIsKicked(iocb))
313 schedule();
314 else
315 kiocbClearKicked(iocb);
316 __set_current_state(TASK_RUNNING);
317}
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319ssize_t do_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
320{
Badari Pulavarty027445c2006-09-30 23:28:46 -0700321 struct iovec iov = { .iov_base = buf, .iov_len = len };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 struct kiocb kiocb;
323 ssize_t ret;
324
325 init_sync_kiocb(&kiocb, filp);
326 kiocb.ki_pos = *ppos;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700327 kiocb.ki_left = len;
David Howells61964eb2010-03-24 17:09:19 +0000328 kiocb.ki_nbytes = len;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700329
330 for (;;) {
331 ret = filp->f_op->aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
332 if (ret != -EIOCBRETRY)
333 break;
Benjamin LaHaise63e68802005-06-23 00:10:27 -0700334 wait_on_retry_sync_kiocb(&kiocb);
Badari Pulavarty027445c2006-09-30 23:28:46 -0700335 }
Benjamin LaHaise63e68802005-06-23 00:10:27 -0700336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (-EIOCBQUEUED == ret)
338 ret = wait_on_sync_kiocb(&kiocb);
339 *ppos = kiocb.ki_pos;
340 return ret;
341}
342
343EXPORT_SYMBOL(do_sync_read);
344
345ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
346{
347 ssize_t ret;
348
349 if (!(file->f_mode & FMODE_READ))
350 return -EBADF;
351 if (!file->f_op || (!file->f_op->read && !file->f_op->aio_read))
352 return -EINVAL;
353 if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
354 return -EFAULT;
355
356 ret = rw_verify_area(READ, file, pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800357 if (ret >= 0) {
358 count = ret;
James Morrisc43e2592008-01-12 22:05:48 +1100359 if (file->f_op->read)
360 ret = file->f_op->read(file, buf, count, pos);
361 else
362 ret = do_sync_read(file, buf, count, pos);
363 if (ret > 0) {
Eric Paris2a12a9d2009-12-17 21:24:21 -0500364 fsnotify_access(file);
James Morrisc43e2592008-01-12 22:05:48 +1100365 add_rchar(current, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
James Morrisc43e2592008-01-12 22:05:48 +1100367 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
369
370 return ret;
371}
372
373EXPORT_SYMBOL(vfs_read);
374
375ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
376{
Badari Pulavarty027445c2006-09-30 23:28:46 -0700377 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 struct kiocb kiocb;
379 ssize_t ret;
380
381 init_sync_kiocb(&kiocb, filp);
382 kiocb.ki_pos = *ppos;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700383 kiocb.ki_left = len;
David Howells61964eb2010-03-24 17:09:19 +0000384 kiocb.ki_nbytes = len;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700385
386 for (;;) {
387 ret = filp->f_op->aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
388 if (ret != -EIOCBRETRY)
389 break;
Benjamin LaHaise63e68802005-06-23 00:10:27 -0700390 wait_on_retry_sync_kiocb(&kiocb);
Badari Pulavarty027445c2006-09-30 23:28:46 -0700391 }
Benjamin LaHaise63e68802005-06-23 00:10:27 -0700392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (-EIOCBQUEUED == ret)
394 ret = wait_on_sync_kiocb(&kiocb);
395 *ppos = kiocb.ki_pos;
396 return ret;
397}
398
399EXPORT_SYMBOL(do_sync_write);
400
401ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
402{
403 ssize_t ret;
404
405 if (!(file->f_mode & FMODE_WRITE))
406 return -EBADF;
407 if (!file->f_op || (!file->f_op->write && !file->f_op->aio_write))
408 return -EINVAL;
409 if (unlikely(!access_ok(VERIFY_READ, buf, count)))
410 return -EFAULT;
411
412 ret = rw_verify_area(WRITE, file, pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800413 if (ret >= 0) {
414 count = ret;
James Morrisc43e2592008-01-12 22:05:48 +1100415 if (file->f_op->write)
416 ret = file->f_op->write(file, buf, count, pos);
417 else
418 ret = do_sync_write(file, buf, count, pos);
419 if (ret > 0) {
Eric Paris2a12a9d2009-12-17 21:24:21 -0500420 fsnotify_modify(file);
James Morrisc43e2592008-01-12 22:05:48 +1100421 add_wchar(current, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
James Morrisc43e2592008-01-12 22:05:48 +1100423 inc_syscw(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425
426 return ret;
427}
428
429EXPORT_SYMBOL(vfs_write);
430
431static inline loff_t file_pos_read(struct file *file)
432{
433 return file->f_pos;
434}
435
436static inline void file_pos_write(struct file *file, loff_t pos)
437{
438 file->f_pos = pos;
439}
440
Heiko Carstens3cdad422009-01-14 14:14:22 +0100441SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
443 struct file *file;
444 ssize_t ret = -EBADF;
445 int fput_needed;
446
447 file = fget_light(fd, &fput_needed);
448 if (file) {
449 loff_t pos = file_pos_read(file);
450 ret = vfs_read(file, buf, count, &pos);
451 file_pos_write(file, pos);
452 fput_light(file, fput_needed);
453 }
454
455 return ret;
456}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Heiko Carstens3cdad422009-01-14 14:14:22 +0100458SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
459 size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
461 struct file *file;
462 ssize_t ret = -EBADF;
463 int fput_needed;
464
465 file = fget_light(fd, &fput_needed);
466 if (file) {
467 loff_t pos = file_pos_read(file);
468 ret = vfs_write(file, buf, count, &pos);
469 file_pos_write(file, pos);
470 fput_light(file, fput_needed);
471 }
472
473 return ret;
474}
475
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100476SYSCALL_DEFINE(pread64)(unsigned int fd, char __user *buf,
477 size_t count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 struct file *file;
480 ssize_t ret = -EBADF;
481 int fput_needed;
482
483 if (pos < 0)
484 return -EINVAL;
485
486 file = fget_light(fd, &fput_needed);
487 if (file) {
488 ret = -ESPIPE;
489 if (file->f_mode & FMODE_PREAD)
490 ret = vfs_read(file, buf, count, &pos);
491 fput_light(file, fput_needed);
492 }
493
494 return ret;
495}
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100496#ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
497asmlinkage long SyS_pread64(long fd, long buf, long count, loff_t pos)
498{
499 return SYSC_pread64((unsigned int) fd, (char __user *) buf,
500 (size_t) count, pos);
501}
502SYSCALL_ALIAS(sys_pread64, SyS_pread64);
503#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100505SYSCALL_DEFINE(pwrite64)(unsigned int fd, const char __user *buf,
506 size_t count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
508 struct file *file;
509 ssize_t ret = -EBADF;
510 int fput_needed;
511
512 if (pos < 0)
513 return -EINVAL;
514
515 file = fget_light(fd, &fput_needed);
516 if (file) {
517 ret = -ESPIPE;
518 if (file->f_mode & FMODE_PWRITE)
519 ret = vfs_write(file, buf, count, &pos);
520 fput_light(file, fput_needed);
521 }
522
523 return ret;
524}
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100525#ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
526asmlinkage long SyS_pwrite64(long fd, long buf, long count, loff_t pos)
527{
528 return SYSC_pwrite64((unsigned int) fd, (const char __user *) buf,
529 (size_t) count, pos);
530}
531SYSCALL_ALIAS(sys_pwrite64, SyS_pwrite64);
532#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534/*
535 * Reduce an iovec's length in-place. Return the resulting number of segments
536 */
537unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
538{
539 unsigned long seg = 0;
540 size_t len = 0;
541
542 while (seg < nr_segs) {
543 seg++;
544 if (len + iov->iov_len >= to) {
545 iov->iov_len = to - len;
546 break;
547 }
548 len += iov->iov_len;
549 iov++;
550 }
551 return seg;
552}
Eric Sandeen19295522008-01-28 23:58:27 -0500553EXPORT_SYMBOL(iov_shorten);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700555ssize_t do_sync_readv_writev(struct file *filp, const struct iovec *iov,
556 unsigned long nr_segs, size_t len, loff_t *ppos, iov_fn_t fn)
557{
558 struct kiocb kiocb;
559 ssize_t ret;
560
561 init_sync_kiocb(&kiocb, filp);
562 kiocb.ki_pos = *ppos;
563 kiocb.ki_left = len;
564 kiocb.ki_nbytes = len;
565
566 for (;;) {
567 ret = fn(&kiocb, iov, nr_segs, kiocb.ki_pos);
568 if (ret != -EIOCBRETRY)
569 break;
570 wait_on_retry_sync_kiocb(&kiocb);
571 }
572
573 if (ret == -EIOCBQUEUED)
574 ret = wait_on_sync_kiocb(&kiocb);
575 *ppos = kiocb.ki_pos;
576 return ret;
577}
578
579/* Do it by hand, with file-ops */
580ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov,
581 unsigned long nr_segs, loff_t *ppos, io_fn_t fn)
582{
583 struct iovec *vector = iov;
584 ssize_t ret = 0;
585
586 while (nr_segs > 0) {
587 void __user *base;
588 size_t len;
589 ssize_t nr;
590
591 base = vector->iov_base;
592 len = vector->iov_len;
593 vector++;
594 nr_segs--;
595
596 nr = fn(filp, base, len, ppos);
597
598 if (nr < 0) {
599 if (!ret)
600 ret = nr;
601 break;
602 }
603 ret += nr;
604 if (nr != len)
605 break;
606 }
607
608 return ret;
609}
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611/* A write operation does a read from user space and vice versa */
612#define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
613
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700614ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
615 unsigned long nr_segs, unsigned long fast_segs,
616 struct iovec *fast_pointer,
617 struct iovec **ret_pointer)
Linus Torvalds435f49a2010-10-29 10:36:49 -0700618{
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700619 unsigned long seg;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700620 ssize_t ret;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700621 struct iovec *iov = fast_pointer;
622
Linus Torvalds435f49a2010-10-29 10:36:49 -0700623 /*
624 * SuS says "The readv() function *may* fail if the iovcnt argument
625 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
626 * traditionally returned zero for zero segments, so...
627 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700628 if (nr_segs == 0) {
629 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700630 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700631 }
632
Linus Torvalds435f49a2010-10-29 10:36:49 -0700633 /*
634 * First get the "struct iovec" from user memory and
635 * verify all the pointers
636 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700637 if (nr_segs > UIO_MAXIOV) {
638 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700639 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700640 }
641 if (nr_segs > fast_segs) {
Linus Torvalds435f49a2010-10-29 10:36:49 -0700642 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700643 if (iov == NULL) {
644 ret = -ENOMEM;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700645 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700646 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700647 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700648 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
649 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700650 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700651 }
652
Linus Torvalds435f49a2010-10-29 10:36:49 -0700653 /*
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700654 * According to the Single Unix Specification we should return EINVAL
655 * if an element length is < 0 when cast to ssize_t or if the
656 * total length would overflow the ssize_t return value of the
657 * system call.
Linus Torvalds435f49a2010-10-29 10:36:49 -0700658 *
659 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
660 * overflow case.
661 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700662 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700663 for (seg = 0; seg < nr_segs; seg++) {
664 void __user *buf = iov[seg].iov_base;
665 ssize_t len = (ssize_t)iov[seg].iov_len;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700666
667 /* see if we we're about to use an invalid len or if
668 * it's about to overflow ssize_t */
Linus Torvalds435f49a2010-10-29 10:36:49 -0700669 if (len < 0) {
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700670 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700671 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700672 }
673 if (unlikely(!access_ok(vrfy_dir(type), buf, len))) {
674 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700675 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700676 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700677 if (len > MAX_RW_COUNT - ret) {
678 len = MAX_RW_COUNT - ret;
679 iov[seg].iov_len = len;
680 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700681 ret += len;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700682 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700683out:
684 *ret_pointer = iov;
685 return ret;
686}
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688static ssize_t do_readv_writev(int type, struct file *file,
689 const struct iovec __user * uvector,
690 unsigned long nr_segs, loff_t *pos)
691{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 size_t tot_len;
693 struct iovec iovstack[UIO_FASTIOV];
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700694 struct iovec *iov = iovstack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 io_fn_t fn;
697 iov_fn_t fnv;
698
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700699 if (!file->f_op) {
700 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 goto out;
702 }
703
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700704 ret = rw_copy_check_uvector(type, uvector, nr_segs,
705 ARRAY_SIZE(iovstack), iovstack, &iov);
706 if (ret <= 0)
707 goto out;
708
709 tot_len = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 ret = rw_verify_area(type, file, pos, tot_len);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800711 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 goto out;
713
714 fnv = NULL;
715 if (type == READ) {
716 fn = file->f_op->read;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700717 fnv = file->f_op->aio_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 } else {
719 fn = (io_fn_t)file->f_op->write;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700720 fnv = file->f_op->aio_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 }
722
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700723 if (fnv)
724 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
725 pos, fnv);
726 else
727 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729out:
730 if (iov != iovstack)
731 kfree(iov);
Robert Love0eeca282005-07-12 17:06:03 -0400732 if ((ret + (type == READ)) > 0) {
733 if (type == READ)
Eric Paris2a12a9d2009-12-17 21:24:21 -0500734 fsnotify_access(file);
Robert Love0eeca282005-07-12 17:06:03 -0400735 else
Eric Paris2a12a9d2009-12-17 21:24:21 -0500736 fsnotify_modify(file);
Robert Love0eeca282005-07-12 17:06:03 -0400737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
741ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
742 unsigned long vlen, loff_t *pos)
743{
744 if (!(file->f_mode & FMODE_READ))
745 return -EBADF;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700746 if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return -EINVAL;
748
749 return do_readv_writev(READ, file, vec, vlen, pos);
750}
751
752EXPORT_SYMBOL(vfs_readv);
753
754ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
755 unsigned long vlen, loff_t *pos)
756{
757 if (!(file->f_mode & FMODE_WRITE))
758 return -EBADF;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700759 if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return -EINVAL;
761
762 return do_readv_writev(WRITE, file, vec, vlen, pos);
763}
764
765EXPORT_SYMBOL(vfs_writev);
766
Heiko Carstens3cdad422009-01-14 14:14:22 +0100767SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
768 unsigned long, vlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
770 struct file *file;
771 ssize_t ret = -EBADF;
772 int fput_needed;
773
774 file = fget_light(fd, &fput_needed);
775 if (file) {
776 loff_t pos = file_pos_read(file);
777 ret = vfs_readv(file, vec, vlen, &pos);
778 file_pos_write(file, pos);
779 fput_light(file, fput_needed);
780 }
781
782 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800783 add_rchar(current, ret);
784 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return ret;
786}
787
Heiko Carstens3cdad422009-01-14 14:14:22 +0100788SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
789 unsigned long, vlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
791 struct file *file;
792 ssize_t ret = -EBADF;
793 int fput_needed;
794
795 file = fget_light(fd, &fput_needed);
796 if (file) {
797 loff_t pos = file_pos_read(file);
798 ret = vfs_writev(file, vec, vlen, &pos);
799 file_pos_write(file, pos);
800 fput_light(file, fput_needed);
801 }
802
803 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800804 add_wchar(current, ret);
805 inc_syscw(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return ret;
807}
808
Linus Torvalds601cc112009-04-03 08:03:22 -0700809static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700810{
Linus Torvalds601cc112009-04-03 08:03:22 -0700811#define HALF_LONG_BITS (BITS_PER_LONG / 2)
812 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
813}
814
815SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
816 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
817{
818 loff_t pos = pos_from_hilo(pos_h, pos_l);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700819 struct file *file;
820 ssize_t ret = -EBADF;
821 int fput_needed;
822
823 if (pos < 0)
824 return -EINVAL;
825
826 file = fget_light(fd, &fput_needed);
827 if (file) {
828 ret = -ESPIPE;
829 if (file->f_mode & FMODE_PREAD)
830 ret = vfs_readv(file, vec, vlen, &pos);
831 fput_light(file, fput_needed);
832 }
833
834 if (ret > 0)
835 add_rchar(current, ret);
836 inc_syscr(current);
837 return ret;
838}
839
840SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
Linus Torvalds601cc112009-04-03 08:03:22 -0700841 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700842{
Linus Torvalds601cc112009-04-03 08:03:22 -0700843 loff_t pos = pos_from_hilo(pos_h, pos_l);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700844 struct file *file;
845 ssize_t ret = -EBADF;
846 int fput_needed;
847
848 if (pos < 0)
849 return -EINVAL;
850
851 file = fget_light(fd, &fput_needed);
852 if (file) {
853 ret = -ESPIPE;
854 if (file->f_mode & FMODE_PWRITE)
855 ret = vfs_writev(file, vec, vlen, &pos);
856 fput_light(file, fput_needed);
857 }
858
859 if (ret > 0)
860 add_wchar(current, ret);
861 inc_syscw(current);
862 return ret;
863}
864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
866 size_t count, loff_t max)
867{
868 struct file * in_file, * out_file;
869 struct inode * in_inode, * out_inode;
870 loff_t pos;
871 ssize_t retval;
Jens Axboe534f2aa2007-06-01 14:52:37 +0200872 int fput_needed_in, fput_needed_out, fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 /*
875 * Get input file, and verify that it is ok..
876 */
877 retval = -EBADF;
878 in_file = fget_light(in_fd, &fput_needed_in);
879 if (!in_file)
880 goto out;
881 if (!(in_file->f_mode & FMODE_READ))
882 goto fput_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 retval = -ESPIPE;
884 if (!ppos)
885 ppos = &in_file->f_pos;
886 else
887 if (!(in_file->f_mode & FMODE_PREAD))
888 goto fput_in;
889 retval = rw_verify_area(READ, in_file, ppos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800890 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 goto fput_in;
Linus Torvaldse28cc712006-01-04 16:20:40 -0800892 count = retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 /*
895 * Get output file, and verify that it is ok..
896 */
897 retval = -EBADF;
898 out_file = fget_light(out_fd, &fput_needed_out);
899 if (!out_file)
900 goto fput_in;
901 if (!(out_file->f_mode & FMODE_WRITE))
902 goto fput_out;
903 retval = -EINVAL;
Miklos Szeredi68181732009-05-07 15:37:36 +0200904 in_inode = in_file->f_path.dentry->d_inode;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800905 out_inode = out_file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 retval = rw_verify_area(WRITE, out_file, &out_file->f_pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800907 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 goto fput_out;
Linus Torvaldse28cc712006-01-04 16:20:40 -0800909 count = retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (!max)
912 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
913
914 pos = *ppos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 if (unlikely(pos + count > max)) {
916 retval = -EOVERFLOW;
917 if (pos >= max)
918 goto fput_out;
919 count = max - pos;
920 }
921
Jens Axboed96e6e72007-06-11 12:18:52 +0200922 fl = 0;
Jens Axboe534f2aa2007-06-01 14:52:37 +0200923#if 0
Jens Axboed96e6e72007-06-11 12:18:52 +0200924 /*
925 * We need to debate whether we can enable this or not. The
926 * man page documents EAGAIN return for the output at least,
927 * and the application is arguably buggy if it doesn't expect
928 * EAGAIN on a non-blocking file descriptor.
929 */
930 if (in_file->f_flags & O_NONBLOCK)
931 fl = SPLICE_F_NONBLOCK;
Jens Axboe534f2aa2007-06-01 14:52:37 +0200932#endif
Jens Axboed96e6e72007-06-11 12:18:52 +0200933 retval = do_splice_direct(in_file, ppos, out_file, count, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 if (retval > 0) {
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800936 add_rchar(current, retval);
937 add_wchar(current, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800940 inc_syscr(current);
941 inc_syscw(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (*ppos > max)
943 retval = -EOVERFLOW;
944
945fput_out:
946 fput_light(out_file, fput_needed_out);
947fput_in:
948 fput_light(in_file, fput_needed_in);
949out:
950 return retval;
951}
952
Heiko Carstens002c8972009-01-14 14:14:18 +0100953SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
955 loff_t pos;
956 off_t off;
957 ssize_t ret;
958
959 if (offset) {
960 if (unlikely(get_user(off, offset)))
961 return -EFAULT;
962 pos = off;
963 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
964 if (unlikely(put_user(pos, offset)))
965 return -EFAULT;
966 return ret;
967 }
968
969 return do_sendfile(out_fd, in_fd, NULL, count, 0);
970}
971
Heiko Carstens002c8972009-01-14 14:14:18 +0100972SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
974 loff_t pos;
975 ssize_t ret;
976
977 if (offset) {
978 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
979 return -EFAULT;
980 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
981 if (unlikely(put_user(pos, offset)))
982 return -EFAULT;
983 return ret;
984 }
985
986 return do_sendfile(out_fd, in_fd, NULL, count, 0);
987}