read_write.c 39.1 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
L
Linus Torvalds 已提交
2 3 4 5 6 7
/*
 *  linux/fs/read_write.c
 *
 *  Copyright (C) 1991, 1992  Linus Torvalds
 */

8
#include <linux/slab.h>
L
Linus Torvalds 已提交
9
#include <linux/stat.h>
10
#include <linux/sched/xacct.h>
L
Linus Torvalds 已提交
11 12 13
#include <linux/fcntl.h>
#include <linux/file.h>
#include <linux/uio.h>
R
Robert Love 已提交
14
#include <linux/fsnotify.h>
L
Linus Torvalds 已提交
15
#include <linux/security.h>
16
#include <linux/export.h>
L
Linus Torvalds 已提交
17
#include <linux/syscalls.h>
18
#include <linux/pagemap.h>
19
#include <linux/splice.h>
A
Al Viro 已提交
20
#include <linux/compat.h>
21
#include <linux/mount.h>
22
#include <linux/fs.h>
23
#include "internal.h"
L
Linus Torvalds 已提交
24

25
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
26 27
#include <asm/unistd.h>

28
const struct file_operations generic_ro_fops = {
L
Linus Torvalds 已提交
29
	.llseek		= generic_file_llseek,
30
	.read_iter	= generic_file_read_iter,
L
Linus Torvalds 已提交
31
	.mmap		= generic_file_readonly_mmap,
32
	.splice_read	= generic_file_splice_read,
L
Linus Torvalds 已提交
33 34 35 36
};

EXPORT_SYMBOL(generic_ro_fops);

C
Christoph Hellwig 已提交
37
static inline bool unsigned_offsets(struct file *file)
38
{
39
	return file->f_mode & FMODE_UNSIGNED_OFFSET;
40 41
}

J
Jie Liu 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54
/**
 * vfs_setpos - update the file offset for lseek
 * @file:	file structure in question
 * @offset:	file offset to seek to
 * @maxsize:	maximum file size
 *
 * This is a low-level filesystem helper for updating the file offset to
 * the value specified by @offset if the given offset is valid and it is
 * not equal to the current file offset.
 *
 * Return the specified offset on success and -EINVAL on invalid offset.
 */
loff_t vfs_setpos(struct file *file, loff_t offset, loff_t maxsize)
55 56 57 58 59 60 61 62 63 64 65 66
{
	if (offset < 0 && !unsigned_offsets(file))
		return -EINVAL;
	if (offset > maxsize)
		return -EINVAL;

	if (offset != file->f_pos) {
		file->f_pos = offset;
		file->f_version = 0;
	}
	return offset;
}
J
Jie Liu 已提交
67
EXPORT_SYMBOL(vfs_setpos);
68

69
/**
A
Andi Kleen 已提交
70
 * generic_file_llseek_size - generic llseek implementation for regular files
71 72
 * @file:	file structure to seek on
 * @offset:	file offset to seek to
73
 * @whence:	type of seek
74 75
 * @size:	max size of this file in file system
 * @eof:	offset used for SEEK_END position
76
 *
A
Andi Kleen 已提交
77
 * This is a variant of generic_file_llseek that allows passing in a custom
78
 * maximum file size and a custom EOF position, for e.g. hashed directories
79 80
 *
 * Synchronization:
A
Andi Kleen 已提交
81
 * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms)
82 83
 * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes.
 * read/writes behave like SEEK_SET against seeks.
84
 */
A
Andi Kleen 已提交
85
loff_t
86
generic_file_llseek_size(struct file *file, loff_t offset, int whence,
87
		loff_t maxsize, loff_t eof)
L
Linus Torvalds 已提交
88
{
89
	switch (whence) {
90
	case SEEK_END:
91
		offset += eof;
92 93
		break;
	case SEEK_CUR:
94 95 96 97 98 99 100 101
		/*
		 * Here we special-case the lseek(fd, 0, SEEK_CUR)
		 * position-querying operation.  Avoid rewriting the "same"
		 * f_pos value back to the file because a concurrent read(),
		 * write() or lseek() might have altered it
		 */
		if (offset == 0)
			return file->f_pos;
102 103 104 105 106 107
		/*
		 * f_lock protects against read/modify/write race with other
		 * SEEK_CURs. Note that parallel writes and reads behave
		 * like SEEK_SET.
		 */
		spin_lock(&file->f_lock);
J
Jie Liu 已提交
108
		offset = vfs_setpos(file, file->f_pos + offset, maxsize);
109 110
		spin_unlock(&file->f_lock);
		return offset;
111 112 113 114 115
	case SEEK_DATA:
		/*
		 * In the generic case the entire file is data, so as long as
		 * offset isn't at the end of the file then the offset is data.
		 */
116
		if ((unsigned long long)offset >= eof)
117 118 119 120 121 122 123
			return -ENXIO;
		break;
	case SEEK_HOLE:
		/*
		 * There is a virtual hole at the end of the file, so as long as
		 * offset isn't i_size or larger, return i_size.
		 */
124
		if ((unsigned long long)offset >= eof)
125
			return -ENXIO;
126
		offset = eof;
127
		break;
L
Linus Torvalds 已提交
128
	}
129

J
Jie Liu 已提交
130
	return vfs_setpos(file, offset, maxsize);
A
Andi Kleen 已提交
131 132 133 134 135 136 137
}
EXPORT_SYMBOL(generic_file_llseek_size);

/**
 * generic_file_llseek - generic llseek implementation for regular files
 * @file:	file structure to seek on
 * @offset:	file offset to seek to
138
 * @whence:	type of seek
A
Andi Kleen 已提交
139 140 141
 *
 * This is a generic implemenation of ->llseek useable for all normal local
 * filesystems.  It just updates the file offset to the value specified by
142
 * @offset and @whence.
A
Andi Kleen 已提交
143
 */
144
loff_t generic_file_llseek(struct file *file, loff_t offset, int whence)
A
Andi Kleen 已提交
145 146 147
{
	struct inode *inode = file->f_mapping->host;

148
	return generic_file_llseek_size(file, offset, whence,
149 150
					inode->i_sb->s_maxbytes,
					i_size_read(inode));
L
Linus Torvalds 已提交
151
}
A
Andi Kleen 已提交
152
EXPORT_SYMBOL(generic_file_llseek);
L
Linus Torvalds 已提交
153

A
Al Viro 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
/**
 * fixed_size_llseek - llseek implementation for fixed-sized devices
 * @file:	file structure to seek on
 * @offset:	file offset to seek to
 * @whence:	type of seek
 * @size:	size of the file
 *
 */
loff_t fixed_size_llseek(struct file *file, loff_t offset, int whence, loff_t size)
{
	switch (whence) {
	case SEEK_SET: case SEEK_CUR: case SEEK_END:
		return generic_file_llseek_size(file, offset, whence,
						size, size);
	default:
		return -EINVAL;
	}
}
EXPORT_SYMBOL(fixed_size_llseek);

174 175 176 177 178 179 180 181 182 183 184 185
/**
 * no_seek_end_llseek - llseek implementation for fixed-sized devices
 * @file:	file structure to seek on
 * @offset:	file offset to seek to
 * @whence:	type of seek
 *
 */
loff_t no_seek_end_llseek(struct file *file, loff_t offset, int whence)
{
	switch (whence) {
	case SEEK_SET: case SEEK_CUR:
		return generic_file_llseek_size(file, offset, whence,
186
						OFFSET_MAX, 0);
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
	default:
		return -EINVAL;
	}
}
EXPORT_SYMBOL(no_seek_end_llseek);

/**
 * no_seek_end_llseek_size - llseek implementation for fixed-sized devices
 * @file:	file structure to seek on
 * @offset:	file offset to seek to
 * @whence:	type of seek
 * @size:	maximal offset allowed
 *
 */
loff_t no_seek_end_llseek_size(struct file *file, loff_t offset, int whence, loff_t size)
{
	switch (whence) {
	case SEEK_SET: case SEEK_CUR:
		return generic_file_llseek_size(file, offset, whence,
						size, 0);
	default:
		return -EINVAL;
	}
}
EXPORT_SYMBOL(no_seek_end_llseek_size);

J
jan Blunck 已提交
213 214 215 216
/**
 * noop_llseek - No Operation Performed llseek implementation
 * @file:	file structure to seek on
 * @offset:	file offset to seek to
217
 * @whence:	type of seek
J
jan Blunck 已提交
218 219 220 221 222 223
 *
 * This is an implementation of ->llseek useable for the rare special case when
 * userspace expects the seek to succeed but the (device) file is actually not
 * able to perform the seek. In this case you use noop_llseek() instead of
 * falling back to the default implementation of ->llseek.
 */
224
loff_t noop_llseek(struct file *file, loff_t offset, int whence)
J
jan Blunck 已提交
225 226 227 228 229
{
	return file->f_pos;
}
EXPORT_SYMBOL(noop_llseek);

230
loff_t no_llseek(struct file *file, loff_t offset, int whence)
L
Linus Torvalds 已提交
231 232 233 234 235
{
	return -ESPIPE;
}
EXPORT_SYMBOL(no_llseek);

236
loff_t default_llseek(struct file *file, loff_t offset, int whence)
L
Linus Torvalds 已提交
237
{
A
Al Viro 已提交
238
	struct inode *inode = file_inode(file);
239
	loff_t retval;
L
Linus Torvalds 已提交
240

A
Al Viro 已提交
241
	inode_lock(inode);
242
	switch (whence) {
243
		case SEEK_END:
244
			offset += i_size_read(inode);
L
Linus Torvalds 已提交
245
			break;
246
		case SEEK_CUR:
247 248 249 250
			if (offset == 0) {
				retval = file->f_pos;
				goto out;
			}
L
Linus Torvalds 已提交
251
			offset += file->f_pos;
252 253 254 255 256 257 258
			break;
		case SEEK_DATA:
			/*
			 * In the generic case the entire file is data, so as
			 * long as offset isn't at the end of the file then the
			 * offset is data.
			 */
259 260 261 262
			if (offset >= inode->i_size) {
				retval = -ENXIO;
				goto out;
			}
263 264 265 266 267 268 269
			break;
		case SEEK_HOLE:
			/*
			 * There is a virtual hole at the end of the file, so
			 * as long as offset isn't i_size or larger, return
			 * i_size.
			 */
270 271 272 273
			if (offset >= inode->i_size) {
				retval = -ENXIO;
				goto out;
			}
274 275
			offset = inode->i_size;
			break;
L
Linus Torvalds 已提交
276 277
	}
	retval = -EINVAL;
278
	if (offset >= 0 || unsigned_offsets(file)) {
L
Linus Torvalds 已提交
279 280 281 282 283 284
		if (offset != file->f_pos) {
			file->f_pos = offset;
			file->f_version = 0;
		}
		retval = offset;
	}
285
out:
A
Al Viro 已提交
286
	inode_unlock(inode);
L
Linus Torvalds 已提交
287 288 289 290
	return retval;
}
EXPORT_SYMBOL(default_llseek);

291
loff_t vfs_llseek(struct file *file, loff_t offset, int whence)
L
Linus Torvalds 已提交
292 293 294 295 296
{
	loff_t (*fn)(struct file *, loff_t, int);

	fn = no_llseek;
	if (file->f_mode & FMODE_LSEEK) {
A
Al Viro 已提交
297
		if (file->f_op->llseek)
L
Linus Torvalds 已提交
298 299
			fn = file->f_op->llseek;
	}
300
	return fn(file, offset, whence);
L
Linus Torvalds 已提交
301 302 303
}
EXPORT_SYMBOL(vfs_llseek);

304
static off_t ksys_lseek(unsigned int fd, off_t offset, unsigned int whence)
L
Linus Torvalds 已提交
305 306
{
	off_t retval;
307
	struct fd f = fdget_pos(fd);
308 309
	if (!f.file)
		return -EBADF;
L
Linus Torvalds 已提交
310 311

	retval = -EINVAL;
312 313
	if (whence <= SEEK_MAX) {
		loff_t res = vfs_llseek(f.file, offset, whence);
L
Linus Torvalds 已提交
314 315 316 317
		retval = res;
		if (res != (loff_t)retval)
			retval = -EOVERFLOW;	/* LFS: should only happen on 32 bit platforms */
	}
318
	fdput_pos(f);
L
Linus Torvalds 已提交
319 320 321
	return retval;
}

322 323 324 325 326
SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence)
{
	return ksys_lseek(fd, offset, whence);
}

A
Al Viro 已提交
327 328 329
#ifdef CONFIG_COMPAT
COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence)
{
330
	return ksys_lseek(fd, offset, whence);
A
Al Viro 已提交
331 332 333
}
#endif

334 335
#if !defined(CONFIG_64BIT) || defined(CONFIG_COMPAT) || \
	defined(__ARCH_WANT_SYS_LLSEEK)
336 337
SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
		unsigned long, offset_low, loff_t __user *, result,
338
		unsigned int, whence)
L
Linus Torvalds 已提交
339 340
{
	int retval;
341
	struct fd f = fdget_pos(fd);
L
Linus Torvalds 已提交
342 343
	loff_t offset;

344 345
	if (!f.file)
		return -EBADF;
L
Linus Torvalds 已提交
346 347

	retval = -EINVAL;
348
	if (whence > SEEK_MAX)
L
Linus Torvalds 已提交
349 350
		goto out_putf;

351
	offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low,
352
			whence);
L
Linus Torvalds 已提交
353 354 355 356 357 358 359 360

	retval = (int)offset;
	if (offset >= 0) {
		retval = -EFAULT;
		if (!copy_to_user(result, &offset, sizeof(offset)))
			retval = 0;
	}
out_putf:
361
	fdput_pos(f);
L
Linus Torvalds 已提交
362 363 364 365
	return retval;
}
#endif

A
Al Viro 已提交
366
int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t count)
L
Linus Torvalds 已提交
367 368
{
	struct inode *inode;
369
	int retval = -EINVAL;
L
Linus Torvalds 已提交
370

A
Al Viro 已提交
371
	inode = file_inode(file);
372
	if (unlikely((ssize_t) count < 0))
373
		return retval;
L
Linus Torvalds 已提交
374

375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
	/*
	 * ranged mandatory locking does not apply to streams - it makes sense
	 * only for files where position has a meaning.
	 */
	if (ppos) {
		loff_t pos = *ppos;

		if (unlikely(pos < 0)) {
			if (!unsigned_offsets(file))
				return retval;
			if (count >= -pos) /* both values are in 0..LLONG_MAX */
				return -EOVERFLOW;
		} else if (unlikely((loff_t) (pos + count) < 0)) {
			if (!unsigned_offsets(file))
				return retval;
		}
391
	}
392

393
	return security_file_permission(file,
394
				read_write == READ ? MAY_READ : MAY_WRITE);
L
Linus Torvalds 已提交
395 396
}

A
Al Viro 已提交
397
static ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
398 399 400 401 402 403 404
{
	struct iovec iov = { .iov_base = buf, .iov_len = len };
	struct kiocb kiocb;
	struct iov_iter iter;
	ssize_t ret;

	init_sync_kiocb(&kiocb, filp);
405
	kiocb.ki_pos = (ppos ? *ppos : 0);
406 407
	iov_iter_init(&iter, READ, &iov, 1, len);

408
	ret = call_read_iter(filp, &kiocb, &iter);
409
	BUG_ON(ret == -EIOCBQUEUED);
410 411
	if (ppos)
		*ppos = kiocb.ki_pos;
412 413 414
	return ret;
}

415 416 417 418 419 420 421 422
static int warn_unsupported(struct file *file, const char *op)
{
	pr_warn_ratelimited(
		"kernel %s not supported for file %pD4 (pid: %d comm: %.20s)\n",
		op, file, current->pid, current->comm);
	return -EINVAL;
}

423 424
ssize_t __kernel_read(struct file *file, void *buf, size_t count, loff_t *pos)
{
425 426 427 428 429 430
	struct kvec iov = {
		.iov_base	= buf,
		.iov_len	= min_t(size_t, count, MAX_RW_COUNT),
	};
	struct kiocb kiocb;
	struct iov_iter iter;
431 432 433 434 435 436
	ssize_t ret;

	if (WARN_ON_ONCE(!(file->f_mode & FMODE_READ)))
		return -EINVAL;
	if (!(file->f_mode & FMODE_CAN_READ))
		return -EINVAL;
437 438 439 440 441 442
	/*
	 * Also fail if ->read_iter and ->read are both wired up as that
	 * implies very convoluted semantics.
	 */
	if (unlikely(!file->f_op->read_iter || file->f_op->read))
		return warn_unsupported(file, "read");
443

444
	init_sync_kiocb(&kiocb, file);
445
	kiocb.ki_pos = pos ? *pos : 0;
446 447
	iov_iter_kvec(&iter, READ, &iov, 1, iov.iov_len);
	ret = file->f_op->read_iter(&kiocb, &iter);
448
	if (ret > 0) {
449 450
		if (pos)
			*pos = kiocb.ki_pos;
451 452 453 454 455 456 457
		fsnotify_access(file);
		add_rchar(current, ret);
	}
	inc_syscr(current);
	return ret;
}

458
ssize_t kernel_read(struct file *file, void *buf, size_t count, loff_t *pos)
459
{
460
	ssize_t ret;
461

462 463 464 465
	ret = rw_verify_area(READ, file, pos, count);
	if (ret)
		return ret;
	return __kernel_read(file, buf, count, pos);
466 467
}
EXPORT_SYMBOL(kernel_read);
D
Dmitry Kasatkin 已提交
468

L
Linus Torvalds 已提交
469 470 471 472 473 474
ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
{
	ssize_t ret;

	if (!(file->f_mode & FMODE_READ))
		return -EBADF;
475
	if (!(file->f_mode & FMODE_CAN_READ))
L
Linus Torvalds 已提交
476
		return -EINVAL;
477
	if (unlikely(!access_ok(buf, count)))
L
Linus Torvalds 已提交
478 479 480
		return -EFAULT;

	ret = rw_verify_area(READ, file, pos, count);
C
Christoph Hellwig 已提交
481 482 483 484
	if (ret)
		return ret;
	if (count > MAX_RW_COUNT)
		count =  MAX_RW_COUNT;
L
Linus Torvalds 已提交
485

C
Christoph Hellwig 已提交
486 487 488 489 490 491 492 493 494 495 496
	if (file->f_op->read)
		ret = file->f_op->read(file, buf, count, pos);
	else if (file->f_op->read_iter)
		ret = new_sync_read(file, buf, count, pos);
	else
		ret = -EINVAL;
	if (ret > 0) {
		fsnotify_access(file);
		add_rchar(current, ret);
	}
	inc_syscr(current);
L
Linus Torvalds 已提交
497 498 499
	return ret;
}

A
Al Viro 已提交
500
static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
501 502 503 504 505 506 507
{
	struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
	struct kiocb kiocb;
	struct iov_iter iter;
	ssize_t ret;

	init_sync_kiocb(&kiocb, filp);
508
	kiocb.ki_pos = (ppos ? *ppos : 0);
509 510
	iov_iter_init(&iter, WRITE, &iov, 1, len);

511
	ret = call_write_iter(filp, &kiocb, &iter);
512
	BUG_ON(ret == -EIOCBQUEUED);
513
	if (ret > 0 && ppos)
514
		*ppos = kiocb.ki_pos;
515 516 517
	return ret;
}

518
/* caller is responsible for file_start_write/file_end_write */
519
ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos)
520
{
521 522 523 524 525 526
	struct kvec iov = {
		.iov_base	= (void *)buf,
		.iov_len	= min_t(size_t, count, MAX_RW_COUNT),
	};
	struct kiocb kiocb;
	struct iov_iter iter;
527 528
	ssize_t ret;

529 530
	if (WARN_ON_ONCE(!(file->f_mode & FMODE_WRITE)))
		return -EBADF;
531
	if (!(file->f_mode & FMODE_CAN_WRITE))
532
		return -EINVAL;
533 534 535 536 537 538
	/*
	 * Also fail if ->write_iter and ->write are both wired up as that
	 * implies very convoluted semantics.
	 */
	if (unlikely(!file->f_op->write_iter || file->f_op->write))
		return warn_unsupported(file, "write");
539

540
	init_sync_kiocb(&kiocb, file);
541
	kiocb.ki_pos = pos ? *pos : 0;
542 543
	iov_iter_kvec(&iter, WRITE, &iov, 1, iov.iov_len);
	ret = file->f_op->write_iter(&kiocb, &iter);
544
	if (ret > 0) {
545 546
		if (pos)
			*pos = kiocb.ki_pos;
547 548 549 550 551 552
		fsnotify_modify(file);
		add_wchar(current, ret);
	}
	inc_syscw(current);
	return ret;
}
553 554 555 556 557 558 559 560
/*
 * This "EXPORT_SYMBOL_GPL()" is more of a "EXPORT_SYMBOL_DONTUSE()",
 * but autofs is one of the few internal kernel users that actually
 * wants this _and_ can be built as a module. So we need to export
 * this symbol for autofs, even though it really isn't appropriate
 * for any other kernel modules.
 */
EXPORT_SYMBOL_GPL(__kernel_write);
561

562 563
ssize_t kernel_write(struct file *file, const void *buf, size_t count,
			    loff_t *pos)
564
{
565
	ssize_t ret;
566

567 568 569
	ret = rw_verify_area(WRITE, file, pos, count);
	if (ret)
		return ret;
570

571 572 573 574
	file_start_write(file);
	ret =  __kernel_write(file, buf, count, pos);
	file_end_write(file);
	return ret;
575 576 577
}
EXPORT_SYMBOL(kernel_write);

L
Linus Torvalds 已提交
578 579 580 581 582 583
ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
{
	ssize_t ret;

	if (!(file->f_mode & FMODE_WRITE))
		return -EBADF;
584
	if (!(file->f_mode & FMODE_CAN_WRITE))
L
Linus Torvalds 已提交
585
		return -EINVAL;
586
	if (unlikely(!access_ok(buf, count)))
L
Linus Torvalds 已提交
587 588 589
		return -EFAULT;

	ret = rw_verify_area(WRITE, file, pos, count);
C
Christoph Hellwig 已提交
590 591 592 593 594 595 596 597 598 599 600 601 602 603
	if (ret)
		return ret;
	if (count > MAX_RW_COUNT)
		count =  MAX_RW_COUNT;
	file_start_write(file);
	if (file->f_op->write)
		ret = file->f_op->write(file, buf, count, pos);
	else if (file->f_op->write_iter)
		ret = new_sync_write(file, buf, count, pos);
	else
		ret = -EINVAL;
	if (ret > 0) {
		fsnotify_modify(file);
		add_wchar(current, ret);
L
Linus Torvalds 已提交
604
	}
C
Christoph Hellwig 已提交
605 606
	inc_syscw(current);
	file_end_write(file);
L
Linus Torvalds 已提交
607 608 609
	return ret;
}

610 611
/* file_ppos returns &file->f_pos or NULL if file is stream */
static inline loff_t *file_ppos(struct file *file)
L
Linus Torvalds 已提交
612
{
613
	return file->f_mode & FMODE_STREAM ? NULL : &file->f_pos;
L
Linus Torvalds 已提交
614 615
}

616
ssize_t ksys_read(unsigned int fd, char __user *buf, size_t count)
L
Linus Torvalds 已提交
617
{
618
	struct fd f = fdget_pos(fd);
L
Linus Torvalds 已提交
619 620
	ssize_t ret = -EBADF;

621
	if (f.file) {
622 623 624 625 626 627 628 629
		loff_t pos, *ppos = file_ppos(f.file);
		if (ppos) {
			pos = *ppos;
			ppos = &pos;
		}
		ret = vfs_read(f.file, buf, count, ppos);
		if (ret >= 0 && ppos)
			f.file->f_pos = pos;
630
		fdput_pos(f);
L
Linus Torvalds 已提交
631 632 633 634
	}
	return ret;
}

635 636 637 638 639
SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
{
	return ksys_read(fd, buf, count);
}

640
ssize_t ksys_write(unsigned int fd, const char __user *buf, size_t count)
L
Linus Torvalds 已提交
641
{
642
	struct fd f = fdget_pos(fd);
L
Linus Torvalds 已提交
643 644
	ssize_t ret = -EBADF;

645
	if (f.file) {
646 647 648 649 650 651 652 653
		loff_t pos, *ppos = file_ppos(f.file);
		if (ppos) {
			pos = *ppos;
			ppos = &pos;
		}
		ret = vfs_write(f.file, buf, count, ppos);
		if (ret >= 0 && ppos)
			f.file->f_pos = pos;
654
		fdput_pos(f);
L
Linus Torvalds 已提交
655 656 657 658 659
	}

	return ret;
}

660 661 662 663 664 665
SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
		size_t, count)
{
	return ksys_write(fd, buf, count);
}

666 667
ssize_t ksys_pread64(unsigned int fd, char __user *buf, size_t count,
		     loff_t pos)
L
Linus Torvalds 已提交
668
{
669
	struct fd f;
L
Linus Torvalds 已提交
670 671 672 673 674
	ssize_t ret = -EBADF;

	if (pos < 0)
		return -EINVAL;

675 676
	f = fdget(fd);
	if (f.file) {
L
Linus Torvalds 已提交
677
		ret = -ESPIPE;
678 679 680
		if (f.file->f_mode & FMODE_PREAD)
			ret = vfs_read(f.file, buf, count, &pos);
		fdput(f);
L
Linus Torvalds 已提交
681 682 683 684 685
	}

	return ret;
}

686 687 688 689 690 691 692 693
SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf,
			size_t, count, loff_t, pos)
{
	return ksys_pread64(fd, buf, count, pos);
}

ssize_t ksys_pwrite64(unsigned int fd, const char __user *buf,
		      size_t count, loff_t pos)
L
Linus Torvalds 已提交
694
{
695
	struct fd f;
L
Linus Torvalds 已提交
696 697 698 699 700
	ssize_t ret = -EBADF;

	if (pos < 0)
		return -EINVAL;

701 702
	f = fdget(fd);
	if (f.file) {
L
Linus Torvalds 已提交
703
		ret = -ESPIPE;
704 705 706
		if (f.file->f_mode & FMODE_PWRITE)  
			ret = vfs_write(f.file, buf, count, &pos);
		fdput(f);
L
Linus Torvalds 已提交
707 708 709 710 711
	}

	return ret;
}

712 713 714 715 716 717
SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf,
			 size_t, count, loff_t, pos)
{
	return ksys_pwrite64(fd, buf, count, pos);
}

718
static ssize_t do_iter_readv_writev(struct file *filp, struct iov_iter *iter,
C
Christoph Hellwig 已提交
719
		loff_t *ppos, int type, rwf_t flags)
720 721 722 723 724
{
	struct kiocb kiocb;
	ssize_t ret;

	init_sync_kiocb(&kiocb, filp);
725 726 727
	ret = kiocb_set_rw_flags(&kiocb, flags);
	if (ret)
		return ret;
728
	kiocb.ki_pos = (ppos ? *ppos : 0);
729

730
	if (type == READ)
731
		ret = call_read_iter(filp, &kiocb, iter);
732
	else
733
		ret = call_write_iter(filp, &kiocb, iter);
734
	BUG_ON(ret == -EIOCBQUEUED);
735 736
	if (ppos)
		*ppos = kiocb.ki_pos;
737 738 739
	return ret;
}

740
/* Do it by hand, with file-ops */
741
static ssize_t do_loop_readv_writev(struct file *filp, struct iov_iter *iter,
C
Christoph Hellwig 已提交
742
		loff_t *ppos, int type, rwf_t flags)
743 744 745
{
	ssize_t ret = 0;

746
	if (flags & ~RWF_HIPRI)
747 748
		return -EOPNOTSUPP;

749 750
	while (iov_iter_count(iter)) {
		struct iovec iovec = iov_iter_iovec(iter);
751 752
		ssize_t nr;

753 754 755 756 757 758 759
		if (type == READ) {
			nr = filp->f_op->read(filp, iovec.iov_base,
					      iovec.iov_len, ppos);
		} else {
			nr = filp->f_op->write(filp, iovec.iov_base,
					       iovec.iov_len, ppos);
		}
760 761 762 763 764 765 766

		if (nr < 0) {
			if (!ret)
				ret = nr;
			break;
		}
		ret += nr;
767
		if (nr != iovec.iov_len)
768
			break;
769
		iov_iter_advance(iter, nr);
770 771 772 773 774
	}

	return ret;
}

C
Christoph Hellwig 已提交
775
static ssize_t do_iter_read(struct file *file, struct iov_iter *iter,
C
Christoph Hellwig 已提交
776
		loff_t *pos, rwf_t flags)
L
Linus Torvalds 已提交
777 778
{
	size_t tot_len;
779
	ssize_t ret = 0;
L
Linus Torvalds 已提交
780

781 782 783 784 785
	if (!(file->f_mode & FMODE_READ))
		return -EBADF;
	if (!(file->f_mode & FMODE_CAN_READ))
		return -EINVAL;

786
	tot_len = iov_iter_count(iter);
787 788
	if (!tot_len)
		goto out;
C
Christoph Hellwig 已提交
789
	ret = rw_verify_area(READ, file, pos, tot_len);
790
	if (ret < 0)
C
Christoph Hellwig 已提交
791
		return ret;
L
Linus Torvalds 已提交
792

C
Christoph Hellwig 已提交
793 794
	if (file->f_op->read_iter)
		ret = do_iter_readv_writev(file, iter, pos, READ, flags);
795
	else
C
Christoph Hellwig 已提交
796
		ret = do_loop_readv_writev(file, iter, pos, READ, flags);
L
Linus Torvalds 已提交
797
out:
C
Christoph Hellwig 已提交
798 799
	if (ret >= 0)
		fsnotify_access(file);
L
Linus Torvalds 已提交
800 801 802
	return ret;
}

803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
ssize_t vfs_iocb_iter_read(struct file *file, struct kiocb *iocb,
			   struct iov_iter *iter)
{
	size_t tot_len;
	ssize_t ret = 0;

	if (!file->f_op->read_iter)
		return -EINVAL;
	if (!(file->f_mode & FMODE_READ))
		return -EBADF;
	if (!(file->f_mode & FMODE_CAN_READ))
		return -EINVAL;

	tot_len = iov_iter_count(iter);
	if (!tot_len)
		goto out;
	ret = rw_verify_area(READ, file, &iocb->ki_pos, tot_len);
	if (ret < 0)
		return ret;

	ret = call_read_iter(file, iocb, iter);
out:
	if (ret >= 0)
		fsnotify_access(file);
	return ret;
}
EXPORT_SYMBOL(vfs_iocb_iter_read);

831
ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos,
C
Christoph Hellwig 已提交
832
		rwf_t flags)
833
{
834 835 836 837 838
	if (!file->f_op->read_iter)
		return -EINVAL;
	return do_iter_read(file, iter, ppos, flags);
}
EXPORT_SYMBOL(vfs_iter_read);
839

C
Christoph Hellwig 已提交
840
static ssize_t do_iter_write(struct file *file, struct iov_iter *iter,
C
Christoph Hellwig 已提交
841
		loff_t *pos, rwf_t flags)
C
Christoph Hellwig 已提交
842 843 844
{
	size_t tot_len;
	ssize_t ret = 0;
A
Al Viro 已提交
845

846 847 848 849 850
	if (!(file->f_mode & FMODE_WRITE))
		return -EBADF;
	if (!(file->f_mode & FMODE_CAN_WRITE))
		return -EINVAL;

C
Christoph Hellwig 已提交
851 852 853 854
	tot_len = iov_iter_count(iter);
	if (!tot_len)
		return 0;
	ret = rw_verify_area(WRITE, file, pos, tot_len);
855 856 857
	if (ret < 0)
		return ret;

C
Christoph Hellwig 已提交
858 859 860 861 862 863
	if (file->f_op->write_iter)
		ret = do_iter_readv_writev(file, iter, pos, WRITE, flags);
	else
		ret = do_loop_readv_writev(file, iter, pos, WRITE, flags);
	if (ret > 0)
		fsnotify_modify(file);
864 865 866
	return ret;
}

867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
ssize_t vfs_iocb_iter_write(struct file *file, struct kiocb *iocb,
			    struct iov_iter *iter)
{
	size_t tot_len;
	ssize_t ret = 0;

	if (!file->f_op->write_iter)
		return -EINVAL;
	if (!(file->f_mode & FMODE_WRITE))
		return -EBADF;
	if (!(file->f_mode & FMODE_CAN_WRITE))
		return -EINVAL;

	tot_len = iov_iter_count(iter);
	if (!tot_len)
		return 0;
	ret = rw_verify_area(WRITE, file, &iocb->ki_pos, tot_len);
	if (ret < 0)
		return ret;

	ret = call_write_iter(file, iocb, iter);
	if (ret > 0)
		fsnotify_modify(file);

	return ret;
}
EXPORT_SYMBOL(vfs_iocb_iter_write);

895
ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos,
C
Christoph Hellwig 已提交
896
		rwf_t flags)
897 898 899 900 901 902 903
{
	if (!file->f_op->write_iter)
		return -EINVAL;
	return do_iter_write(file, iter, ppos, flags);
}
EXPORT_SYMBOL(vfs_iter_write);

904
static ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
C
Christoph Hellwig 已提交
905
		  unsigned long vlen, loff_t *pos, rwf_t flags)
L
Linus Torvalds 已提交
906
{
907 908 909 910
	struct iovec iovstack[UIO_FASTIOV];
	struct iovec *iov = iovstack;
	struct iov_iter iter;
	ssize_t ret;
L
Linus Torvalds 已提交
911

C
Christoph Hellwig 已提交
912
	ret = import_iovec(READ, vec, vlen, ARRAY_SIZE(iovstack), &iov, &iter);
913 914 915 916
	if (ret >= 0) {
		ret = do_iter_read(file, &iter, pos, flags);
		kfree(iov);
	}
L
Linus Torvalds 已提交
917

C
Christoph Hellwig 已提交
918 919
	return ret;
}
L
Linus Torvalds 已提交
920

921
static ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
C
Christoph Hellwig 已提交
922
		   unsigned long vlen, loff_t *pos, rwf_t flags)
L
Linus Torvalds 已提交
923
{
C
Christoph Hellwig 已提交
924 925 926 927
	struct iovec iovstack[UIO_FASTIOV];
	struct iovec *iov = iovstack;
	struct iov_iter iter;
	ssize_t ret;
L
Linus Torvalds 已提交
928

C
Christoph Hellwig 已提交
929
	ret = import_iovec(WRITE, vec, vlen, ARRAY_SIZE(iovstack), &iov, &iter);
930
	if (ret >= 0) {
931
		file_start_write(file);
932
		ret = do_iter_write(file, &iter, pos, flags);
933
		file_end_write(file);
934 935
		kfree(iov);
	}
C
Christoph Hellwig 已提交
936
	return ret;
L
Linus Torvalds 已提交
937 938
}

939
static ssize_t do_readv(unsigned long fd, const struct iovec __user *vec,
C
Christoph Hellwig 已提交
940
			unsigned long vlen, rwf_t flags)
L
Linus Torvalds 已提交
941
{
942
	struct fd f = fdget_pos(fd);
L
Linus Torvalds 已提交
943 944
	ssize_t ret = -EBADF;

945
	if (f.file) {
946 947 948 949 950 951 952 953
		loff_t pos, *ppos = file_ppos(f.file);
		if (ppos) {
			pos = *ppos;
			ppos = &pos;
		}
		ret = vfs_readv(f.file, vec, vlen, ppos, flags);
		if (ret >= 0 && ppos)
			f.file->f_pos = pos;
954
		fdput_pos(f);
L
Linus Torvalds 已提交
955 956 957
	}

	if (ret > 0)
958 959
		add_rchar(current, ret);
	inc_syscr(current);
L
Linus Torvalds 已提交
960 961 962
	return ret;
}

963
static ssize_t do_writev(unsigned long fd, const struct iovec __user *vec,
C
Christoph Hellwig 已提交
964
			 unsigned long vlen, rwf_t flags)
L
Linus Torvalds 已提交
965
{
966
	struct fd f = fdget_pos(fd);
L
Linus Torvalds 已提交
967 968
	ssize_t ret = -EBADF;

969
	if (f.file) {
970 971 972 973 974 975 976 977
		loff_t pos, *ppos = file_ppos(f.file);
		if (ppos) {
			pos = *ppos;
			ppos = &pos;
		}
		ret = vfs_writev(f.file, vec, vlen, ppos, flags);
		if (ret >= 0 && ppos)
			f.file->f_pos = pos;
978
		fdput_pos(f);
L
Linus Torvalds 已提交
979 980 981
	}

	if (ret > 0)
982 983
		add_wchar(current, ret);
	inc_syscw(current);
L
Linus Torvalds 已提交
984 985 986
	return ret;
}

987 988 989 990 991 992
static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
{
#define HALF_LONG_BITS (BITS_PER_LONG / 2)
	return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
}

993
static ssize_t do_preadv(unsigned long fd, const struct iovec __user *vec,
C
Christoph Hellwig 已提交
994
			 unsigned long vlen, loff_t pos, rwf_t flags)
995
{
996
	struct fd f;
997 998 999 1000 1001
	ssize_t ret = -EBADF;

	if (pos < 0)
		return -EINVAL;

1002 1003
	f = fdget(fd);
	if (f.file) {
1004
		ret = -ESPIPE;
1005
		if (f.file->f_mode & FMODE_PREAD)
1006
			ret = vfs_readv(f.file, vec, vlen, &pos, flags);
1007
		fdput(f);
1008 1009 1010 1011 1012 1013 1014 1015
	}

	if (ret > 0)
		add_rchar(current, ret);
	inc_syscr(current);
	return ret;
}

1016
static ssize_t do_pwritev(unsigned long fd, const struct iovec __user *vec,
C
Christoph Hellwig 已提交
1017
			  unsigned long vlen, loff_t pos, rwf_t flags)
1018
{
1019
	struct fd f;
1020 1021 1022 1023 1024
	ssize_t ret = -EBADF;

	if (pos < 0)
		return -EINVAL;

1025 1026
	f = fdget(fd);
	if (f.file) {
1027
		ret = -ESPIPE;
1028
		if (f.file->f_mode & FMODE_PWRITE)
1029
			ret = vfs_writev(f.file, vec, vlen, &pos, flags);
1030
		fdput(f);
1031 1032 1033 1034 1035 1036 1037 1038
	}

	if (ret > 0)
		add_wchar(current, ret);
	inc_syscw(current);
	return ret;
}

1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
		unsigned long, vlen)
{
	return do_readv(fd, vec, vlen, 0);
}

SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
		unsigned long, vlen)
{
	return do_writev(fd, vec, vlen, 0);
}

SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
		unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
{
	loff_t pos = pos_from_hilo(pos_h, pos_l);

	return do_preadv(fd, vec, vlen, pos, 0);
}

SYSCALL_DEFINE6(preadv2, unsigned long, fd, const struct iovec __user *, vec,
		unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h,
C
Christoph Hellwig 已提交
1061
		rwf_t, flags)
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
{
	loff_t pos = pos_from_hilo(pos_h, pos_l);

	if (pos == -1)
		return do_readv(fd, vec, vlen, flags);

	return do_preadv(fd, vec, vlen, pos, flags);
}

SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
		unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
{
	loff_t pos = pos_from_hilo(pos_h, pos_l);

	return do_pwritev(fd, vec, vlen, pos, 0);
}

SYSCALL_DEFINE6(pwritev2, unsigned long, fd, const struct iovec __user *, vec,
		unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h,
C
Christoph Hellwig 已提交
1081
		rwf_t, flags)
1082 1083 1084 1085 1086 1087 1088 1089 1090
{
	loff_t pos = pos_from_hilo(pos_h, pos_l);

	if (pos == -1)
		return do_writev(fd, vec, vlen, flags);

	return do_pwritev(fd, vec, vlen, pos, flags);
}

1091 1092 1093 1094 1095
/*
 * Various compat syscalls.  Note that they all pretend to take a native
 * iovec - import_iovec will properly treat those as compat_iovecs based on
 * in_compat_syscall().
 */
1096
#ifdef CONFIG_COMPAT
1097 1098
#ifdef __ARCH_WANT_COMPAT_SYS_PREADV64
COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd,
1099
		const struct iovec __user *, vec,
1100 1101
		unsigned long, vlen, loff_t, pos)
{
1102
	return do_preadv(fd, vec, vlen, pos, 0);
1103 1104 1105
}
#endif

1106
COMPAT_SYSCALL_DEFINE5(preadv, compat_ulong_t, fd,
1107
		const struct iovec __user *, vec,
1108
		compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
1109 1110
{
	loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1111

1112
	return do_preadv(fd, vec, vlen, pos, 0);
1113 1114
}

1115 1116
#ifdef __ARCH_WANT_COMPAT_SYS_PREADV64V2
COMPAT_SYSCALL_DEFINE5(preadv64v2, unsigned long, fd,
1117
		const struct iovec __user *, vec,
C
Christoph Hellwig 已提交
1118
		unsigned long, vlen, loff_t, pos, rwf_t, flags)
1119
{
1120
	if (pos == -1)
1121 1122
		return do_readv(fd, vec, vlen, flags);
	return do_preadv(fd, vec, vlen, pos, flags);
1123 1124 1125
}
#endif

1126
COMPAT_SYSCALL_DEFINE6(preadv2, compat_ulong_t, fd,
1127
		const struct iovec __user *, vec,
1128
		compat_ulong_t, vlen, u32, pos_low, u32, pos_high,
C
Christoph Hellwig 已提交
1129
		rwf_t, flags)
1130 1131 1132 1133
{
	loff_t pos = ((loff_t)pos_high << 32) | pos_low;

	if (pos == -1)
1134 1135
		return do_readv(fd, vec, vlen, flags);
	return do_preadv(fd, vec, vlen, pos, flags);
1136 1137
}

1138 1139
#ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64
COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd,
1140
		const struct iovec __user *, vec,
1141 1142
		unsigned long, vlen, loff_t, pos)
{
1143
	return do_pwritev(fd, vec, vlen, pos, 0);
1144 1145 1146
}
#endif

1147
COMPAT_SYSCALL_DEFINE5(pwritev, compat_ulong_t, fd,
1148
		const struct iovec __user *,vec,
1149
		compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
1150 1151
{
	loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1152

1153
	return do_pwritev(fd, vec, vlen, pos, 0);
1154
}
1155

1156 1157
#ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64V2
COMPAT_SYSCALL_DEFINE5(pwritev64v2, unsigned long, fd,
1158
		const struct iovec __user *, vec,
C
Christoph Hellwig 已提交
1159
		unsigned long, vlen, loff_t, pos, rwf_t, flags)
1160
{
1161
	if (pos == -1)
1162 1163
		return do_writev(fd, vec, vlen, flags);
	return do_pwritev(fd, vec, vlen, pos, flags);
1164 1165 1166
}
#endif

1167
COMPAT_SYSCALL_DEFINE6(pwritev2, compat_ulong_t, fd,
1168
		const struct iovec __user *,vec,
C
Christoph Hellwig 已提交
1169
		compat_ulong_t, vlen, u32, pos_low, u32, pos_high, rwf_t, flags)
1170 1171 1172 1173
{
	loff_t pos = ((loff_t)pos_high << 32) | pos_low;

	if (pos == -1)
1174 1175
		return do_writev(fd, vec, vlen, flags);
	return do_pwritev(fd, vec, vlen, pos, flags);
1176
}
1177
#endif /* CONFIG_COMPAT */
1178

1179 1180
static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
		  	   size_t count, loff_t max)
L
Linus Torvalds 已提交
1181
{
1182 1183
	struct fd in, out;
	struct inode *in_inode, *out_inode;
1184
	struct pipe_inode_info *opipe;
L
Linus Torvalds 已提交
1185
	loff_t pos;
1186
	loff_t out_pos;
L
Linus Torvalds 已提交
1187
	ssize_t retval;
1188
	int fl;
L
Linus Torvalds 已提交
1189 1190 1191 1192 1193

	/*
	 * Get input file, and verify that it is ok..
	 */
	retval = -EBADF;
1194 1195
	in = fdget(in_fd);
	if (!in.file)
L
Linus Torvalds 已提交
1196
		goto out;
1197
	if (!(in.file->f_mode & FMODE_READ))
L
Linus Torvalds 已提交
1198 1199
		goto fput_in;
	retval = -ESPIPE;
1200 1201 1202 1203
	if (!ppos) {
		pos = in.file->f_pos;
	} else {
		pos = *ppos;
1204
		if (!(in.file->f_mode & FMODE_PREAD))
L
Linus Torvalds 已提交
1205
			goto fput_in;
1206 1207
	}
	retval = rw_verify_area(READ, in.file, &pos, count);
1208
	if (retval < 0)
L
Linus Torvalds 已提交
1209
		goto fput_in;
1210 1211
	if (count > MAX_RW_COUNT)
		count =  MAX_RW_COUNT;
L
Linus Torvalds 已提交
1212 1213 1214 1215 1216

	/*
	 * Get output file, and verify that it is ok..
	 */
	retval = -EBADF;
1217 1218
	out = fdget(out_fd);
	if (!out.file)
L
Linus Torvalds 已提交
1219
		goto fput_in;
1220
	if (!(out.file->f_mode & FMODE_WRITE))
L
Linus Torvalds 已提交
1221
		goto fput_out;
A
Al Viro 已提交
1222 1223
	in_inode = file_inode(in.file);
	out_inode = file_inode(out.file);
1224
	out_pos = out.file->f_pos;
L
Linus Torvalds 已提交
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235

	if (!max)
		max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);

	if (unlikely(pos + count > max)) {
		retval = -EOVERFLOW;
		if (pos >= max)
			goto fput_out;
		count = max - pos;
	}

J
Jens Axboe 已提交
1236
	fl = 0;
1237
#if 0
J
Jens Axboe 已提交
1238 1239 1240 1241 1242 1243
	/*
	 * We need to debate whether we can enable this or not. The
	 * man page documents EAGAIN return for the output at least,
	 * and the application is arguably buggy if it doesn't expect
	 * EAGAIN on a non-blocking file descriptor.
	 */
1244
	if (in.file->f_flags & O_NONBLOCK)
J
Jens Axboe 已提交
1245
		fl = SPLICE_F_NONBLOCK;
1246
#endif
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
	opipe = get_pipe_info(out.file, true);
	if (!opipe) {
		retval = rw_verify_area(WRITE, out.file, &out_pos, count);
		if (retval < 0)
			goto fput_out;
		file_start_write(out.file);
		retval = do_splice_direct(in.file, &pos, out.file, &out_pos,
					  count, fl);
		file_end_write(out.file);
	} else {
		retval = splice_file_to_pipe(in.file, opipe, &pos, count, fl);
	}
L
Linus Torvalds 已提交
1259 1260

	if (retval > 0) {
1261 1262
		add_rchar(current, retval);
		add_wchar(current, retval);
1263 1264
		fsnotify_access(in.file);
		fsnotify_modify(out.file);
1265 1266 1267 1268 1269
		out.file->f_pos = out_pos;
		if (ppos)
			*ppos = pos;
		else
			in.file->f_pos = pos;
L
Linus Torvalds 已提交
1270 1271
	}

1272 1273
	inc_syscr(current);
	inc_syscw(current);
1274
	if (pos > max)
L
Linus Torvalds 已提交
1275 1276 1277
		retval = -EOVERFLOW;

fput_out:
1278
	fdput(out);
L
Linus Torvalds 已提交
1279
fput_in:
1280
	fdput(in);
L
Linus Torvalds 已提交
1281 1282 1283 1284
out:
	return retval;
}

1285
SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
L
Linus Torvalds 已提交
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
{
	loff_t pos;
	off_t off;
	ssize_t ret;

	if (offset) {
		if (unlikely(get_user(off, offset)))
			return -EFAULT;
		pos = off;
		ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
		if (unlikely(put_user(pos, offset)))
			return -EFAULT;
		return ret;
	}

	return do_sendfile(out_fd, in_fd, NULL, count, 0);
}

1304
SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
L
Linus Torvalds 已提交
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
{
	loff_t pos;
	ssize_t ret;

	if (offset) {
		if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
			return -EFAULT;
		ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
		if (unlikely(put_user(pos, offset)))
			return -EFAULT;
		return ret;
	}

	return do_sendfile(out_fd, in_fd, NULL, count, 0);
}
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359

#ifdef CONFIG_COMPAT
COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd,
		compat_off_t __user *, offset, compat_size_t, count)
{
	loff_t pos;
	off_t off;
	ssize_t ret;

	if (offset) {
		if (unlikely(get_user(off, offset)))
			return -EFAULT;
		pos = off;
		ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
		if (unlikely(put_user(pos, offset)))
			return -EFAULT;
		return ret;
	}

	return do_sendfile(out_fd, in_fd, NULL, count, 0);
}

COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd,
		compat_loff_t __user *, offset, compat_size_t, count)
{
	loff_t pos;
	ssize_t ret;

	if (offset) {
		if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
			return -EFAULT;
		ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
		if (unlikely(put_user(pos, offset)))
			return -EFAULT;
		return ret;
	}

	return do_sendfile(out_fd, in_fd, NULL, count, 0);
}
#endif
1360

1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
/**
 * generic_copy_file_range - copy data between two files
 * @file_in:	file structure to read from
 * @pos_in:	file offset to read from
 * @file_out:	file structure to write data to
 * @pos_out:	file offset to write data to
 * @len:	amount of data to copy
 * @flags:	copy flags
 *
 * This is a generic filesystem helper to copy data from one file to another.
 * It has no constraints on the source or destination file owners - the files
 * can belong to different superblocks and different filesystem types. Short
 * copies are allowed.
 *
 * This should be called from the @file_out filesystem, as per the
 * ->copy_file_range() method.
 *
 * Returns the number of bytes copied or a negative error indicating the
 * failure.
 */

ssize_t generic_copy_file_range(struct file *file_in, loff_t pos_in,
				struct file *file_out, loff_t pos_out,
				size_t len, unsigned int flags)
{
	return do_splice_direct(file_in, &pos_in, file_out, &pos_out,
				len > MAX_RW_COUNT ? MAX_RW_COUNT : len, 0);
}
EXPORT_SYMBOL(generic_copy_file_range);

1391 1392 1393 1394
static ssize_t do_copy_file_range(struct file *file_in, loff_t pos_in,
				  struct file *file_out, loff_t pos_out,
				  size_t len, unsigned int flags)
{
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404
	/*
	 * Although we now allow filesystems to handle cross sb copy, passing
	 * a file of the wrong filesystem type to filesystem driver can result
	 * in an attempt to dereference the wrong type of ->private_data, so
	 * avoid doing that until we really have a good reason.  NFS defines
	 * several different file_system_type structures, but they all end up
	 * using the same ->copy_file_range() function pointer.
	 */
	if (file_out->f_op->copy_file_range &&
	    file_out->f_op->copy_file_range == file_in->f_op->copy_file_range)
1405 1406 1407 1408 1409 1410 1411 1412
		return file_out->f_op->copy_file_range(file_in, pos_in,
						       file_out, pos_out,
						       len, flags);

	return generic_copy_file_range(file_in, pos_in, file_out, pos_out, len,
				       flags);
}

1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
/*
 * Performs necessary checks before doing a file copy
 *
 * Can adjust amount of bytes to copy via @req_count argument.
 * Returns appropriate error code that caller should return or
 * zero in case the copy should be allowed.
 */
static int generic_copy_file_checks(struct file *file_in, loff_t pos_in,
				    struct file *file_out, loff_t pos_out,
				    size_t *req_count, unsigned int flags)
{
	struct inode *inode_in = file_inode(file_in);
	struct inode *inode_out = file_inode(file_out);
	uint64_t count = *req_count;
	loff_t size_in;
	int ret;

	ret = generic_file_rw_checks(file_in, file_out);
	if (ret)
		return ret;

	/* Don't touch certain kinds of inodes */
	if (IS_IMMUTABLE(inode_out))
		return -EPERM;

	if (IS_SWAPFILE(inode_in) || IS_SWAPFILE(inode_out))
		return -ETXTBSY;

	/* Ensure offsets don't wrap. */
	if (pos_in + count < pos_in || pos_out + count < pos_out)
		return -EOVERFLOW;

	/* Shorten the copy to EOF */
	size_in = i_size_read(inode_in);
	if (pos_in >= size_in)
		count = 0;
	else
		count = min(count, size_in - (uint64_t)pos_in);

	ret = generic_write_check_limits(file_out, pos_out, &count);
	if (ret)
		return ret;

	/* Don't allow overlapped copying within the same file. */
	if (inode_in == inode_out &&
	    pos_out + count > pos_in &&
	    pos_out < pos_in + count)
		return -EINVAL;

	*req_count = count;
	return 0;
}

1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479
/*
 * copy_file_range() differs from regular file read and write in that it
 * specifically allows return partial success.  When it does so is up to
 * the copy_file_range method.
 */
ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
			    struct file *file_out, loff_t pos_out,
			    size_t len, unsigned int flags)
{
	ssize_t ret;

	if (flags != 0)
		return -EINVAL;

1480 1481
	ret = generic_copy_file_checks(file_in, pos_in, file_out, pos_out, &len,
				       flags);
1482 1483
	if (unlikely(ret))
		return ret;
1484

1485
	ret = rw_verify_area(READ, file_in, &pos_in, len);
1486 1487 1488 1489 1490
	if (unlikely(ret))
		return ret;

	ret = rw_verify_area(WRITE, file_out, &pos_out, len);
	if (unlikely(ret))
1491 1492 1493 1494 1495
		return ret;

	if (len == 0)
		return 0;

1496
	file_start_write(file_out);
1497

1498 1499 1500 1501
	/*
	 * Try cloning first, this is supported by more file systems, and
	 * more efficient if both clone and copy are supported (e.g. NFS).
	 */
1502 1503
	if (file_in->f_op->remap_file_range &&
	    file_inode(file_in)->i_sb == file_inode(file_out)->i_sb) {
1504 1505 1506 1507
		loff_t cloned;

		cloned = file_in->f_op->remap_file_range(file_in, pos_in,
				file_out, pos_out,
1508 1509
				min_t(loff_t, MAX_RW_COUNT, len),
				REMAP_FILE_CAN_SHORTEN);
1510 1511
		if (cloned > 0) {
			ret = cloned;
1512 1513 1514 1515
			goto done;
		}
	}

1516 1517 1518
	ret = do_copy_file_range(file_in, pos_in, file_out, pos_out, len,
				flags);
	WARN_ON_ONCE(ret == -EOPNOTSUPP);
1519
done:
1520 1521 1522 1523 1524 1525
	if (ret > 0) {
		fsnotify_access(file_in);
		add_rchar(current, ret);
		fsnotify_modify(file_out);
		add_wchar(current, ret);
	}
1526

1527 1528 1529
	inc_syscr(current);
	inc_syscw(current);

1530
	file_end_write(file_out);
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596

	return ret;
}
EXPORT_SYMBOL(vfs_copy_file_range);

SYSCALL_DEFINE6(copy_file_range, int, fd_in, loff_t __user *, off_in,
		int, fd_out, loff_t __user *, off_out,
		size_t, len, unsigned int, flags)
{
	loff_t pos_in;
	loff_t pos_out;
	struct fd f_in;
	struct fd f_out;
	ssize_t ret = -EBADF;

	f_in = fdget(fd_in);
	if (!f_in.file)
		goto out2;

	f_out = fdget(fd_out);
	if (!f_out.file)
		goto out1;

	ret = -EFAULT;
	if (off_in) {
		if (copy_from_user(&pos_in, off_in, sizeof(loff_t)))
			goto out;
	} else {
		pos_in = f_in.file->f_pos;
	}

	if (off_out) {
		if (copy_from_user(&pos_out, off_out, sizeof(loff_t)))
			goto out;
	} else {
		pos_out = f_out.file->f_pos;
	}

	ret = vfs_copy_file_range(f_in.file, pos_in, f_out.file, pos_out, len,
				  flags);
	if (ret > 0) {
		pos_in += ret;
		pos_out += ret;

		if (off_in) {
			if (copy_to_user(off_in, &pos_in, sizeof(loff_t)))
				ret = -EFAULT;
		} else {
			f_in.file->f_pos = pos_in;
		}

		if (off_out) {
			if (copy_to_user(off_out, &pos_out, sizeof(loff_t)))
				ret = -EFAULT;
		} else {
			f_out.file->f_pos = pos_out;
		}
	}

out:
	fdput(f_out);
out1:
	fdput(f_in);
out2:
	return ret;
}
1597

1598
/*
1599 1600 1601
 * Don't operate on ranges the page cache doesn't support, and don't exceed the
 * LFS limits.  If pos is under the limit it becomes a short access.  If it
 * exceeds the limit we return -EFBIG.
1602
 */
1603
int generic_write_check_limits(struct file *file, loff_t pos, loff_t *count)
1604
{
1605 1606 1607
	struct inode *inode = file->f_mapping->host;
	loff_t max_size = inode->i_sb->s_maxbytes;
	loff_t limit = rlimit(RLIMIT_FSIZE);
1608

1609 1610 1611 1612
	if (limit != RLIM_INFINITY) {
		if (pos >= limit) {
			send_sig(SIGXFSZ, current, 0);
			return -EFBIG;
1613
		}
1614 1615
		*count = min(*count, limit - pos);
	}
1616

1617 1618
	if (!(file->f_flags & O_LARGEFILE))
		max_size = MAX_NON_LFS;
1619

1620 1621
	if (unlikely(pos >= max_size))
		return -EFBIG;
1622

1623
	*count = min(*count, max_size - pos);
1624 1625 1626

	return 0;
}
1627

1628
/*
1629
 * Performs necessary checks before doing a write
1630
 *
1631 1632 1633
 * Can adjust writing position or amount of bytes to write.
 * Returns appropriate error code that caller should return or
 * zero in case that write should be allowed.
1634
 */
1635
ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)
1636
{
1637 1638 1639
	struct file *file = iocb->ki_filp;
	struct inode *inode = file->f_mapping->host;
	loff_t count;
1640 1641
	int ret;

1642
	if (IS_SWAPFILE(inode))
1643 1644
		return -ETXTBSY;

1645 1646
	if (!iov_iter_count(from))
		return 0;
1647

1648 1649 1650
	/* FIXME: this is for backwards compatibility with 2.4 */
	if (iocb->ki_flags & IOCB_APPEND)
		iocb->ki_pos = i_size_read(inode);
1651

1652 1653
	if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
		return -EINVAL;
1654

1655 1656
	count = iov_iter_count(from);
	ret = generic_write_check_limits(file, iocb->ki_pos, &count);
1657 1658 1659
	if (ret)
		return ret;

1660 1661
	iov_iter_truncate(from, count);
	return iov_iter_count(from);
1662
}
1663
EXPORT_SYMBOL(generic_write_checks);
1664

1665 1666 1667 1668 1669
/*
 * Performs common checks before doing a file copy/clone
 * from @file_in to @file_out.
 */
int generic_file_rw_checks(struct file *file_in, struct file *file_out)
1670
{
1671 1672
	struct inode *inode_in = file_inode(file_in);
	struct inode *inode_out = file_inode(file_out);
1673

1674 1675
	/* Don't copy dirs, pipes, sockets... */
	if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
1676
		return -EISDIR;
1677
	if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
1678 1679
		return -EINVAL;

1680 1681 1682 1683
	if (!(file_in->f_mode & FMODE_READ) ||
	    !(file_out->f_mode & FMODE_WRITE) ||
	    (file_out->f_flags & O_APPEND))
		return -EBADF;
1684

1685
	return 0;
1686
}