read_write.c 38.9 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
	if (unlikely((ssize_t) count < 0))
369
		return -EINVAL;
L
Linus Torvalds 已提交
370

371 372 373 374 375
	if (ppos) {
		loff_t pos = *ppos;

		if (unlikely(pos < 0)) {
			if (!unsigned_offsets(file))
376
				return -EINVAL;
377 378 379 380
			if (count >= -pos) /* both values are in 0..LLONG_MAX */
				return -EOVERFLOW;
		} else if (unlikely((loff_t) (pos + count) < 0)) {
			if (!unsigned_offsets(file))
381
				return -EINVAL;
382
		}
383
	}
384

385
	return security_file_permission(file,
386
				read_write == READ ? MAY_READ : MAY_WRITE);
L
Linus Torvalds 已提交
387 388
}

A
Al Viro 已提交
389
static ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
390 391 392 393 394 395 396
{
	struct iovec iov = { .iov_base = buf, .iov_len = len };
	struct kiocb kiocb;
	struct iov_iter iter;
	ssize_t ret;

	init_sync_kiocb(&kiocb, filp);
397
	kiocb.ki_pos = (ppos ? *ppos : 0);
398 399
	iov_iter_init(&iter, READ, &iov, 1, len);

400
	ret = call_read_iter(filp, &kiocb, &iter);
401
	BUG_ON(ret == -EIOCBQUEUED);
402 403
	if (ppos)
		*ppos = kiocb.ki_pos;
404 405 406
	return ret;
}

407 408 409 410 411 412 413 414
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;
}

415 416
ssize_t __kernel_read(struct file *file, void *buf, size_t count, loff_t *pos)
{
417 418 419 420 421 422
	struct kvec iov = {
		.iov_base	= buf,
		.iov_len	= min_t(size_t, count, MAX_RW_COUNT),
	};
	struct kiocb kiocb;
	struct iov_iter iter;
423 424 425 426 427 428
	ssize_t ret;

	if (WARN_ON_ONCE(!(file->f_mode & FMODE_READ)))
		return -EINVAL;
	if (!(file->f_mode & FMODE_CAN_READ))
		return -EINVAL;
429 430 431 432 433 434
	/*
	 * 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");
435

436
	init_sync_kiocb(&kiocb, file);
437
	kiocb.ki_pos = pos ? *pos : 0;
438 439
	iov_iter_kvec(&iter, READ, &iov, 1, iov.iov_len);
	ret = file->f_op->read_iter(&kiocb, &iter);
440
	if (ret > 0) {
441 442
		if (pos)
			*pos = kiocb.ki_pos;
443 444 445 446 447 448 449
		fsnotify_access(file);
		add_rchar(current, ret);
	}
	inc_syscr(current);
	return ret;
}

450
ssize_t kernel_read(struct file *file, void *buf, size_t count, loff_t *pos)
451
{
452
	ssize_t ret;
453

454 455 456 457
	ret = rw_verify_area(READ, file, pos, count);
	if (ret)
		return ret;
	return __kernel_read(file, buf, count, pos);
458 459
}
EXPORT_SYMBOL(kernel_read);
D
Dmitry Kasatkin 已提交
460

L
Linus Torvalds 已提交
461 462 463 464 465 466
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;
467
	if (!(file->f_mode & FMODE_CAN_READ))
L
Linus Torvalds 已提交
468
		return -EINVAL;
469
	if (unlikely(!access_ok(buf, count)))
L
Linus Torvalds 已提交
470 471 472
		return -EFAULT;

	ret = rw_verify_area(READ, file, pos, count);
C
Christoph Hellwig 已提交
473 474 475 476
	if (ret)
		return ret;
	if (count > MAX_RW_COUNT)
		count =  MAX_RW_COUNT;
L
Linus Torvalds 已提交
477

C
Christoph Hellwig 已提交
478 479 480 481 482 483 484 485 486 487 488
	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 已提交
489 490 491
	return ret;
}

A
Al Viro 已提交
492
static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
493 494 495 496 497 498 499
{
	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);
500
	kiocb.ki_pos = (ppos ? *ppos : 0);
501 502
	iov_iter_init(&iter, WRITE, &iov, 1, len);

503
	ret = call_write_iter(filp, &kiocb, &iter);
504
	BUG_ON(ret == -EIOCBQUEUED);
505
	if (ret > 0 && ppos)
506
		*ppos = kiocb.ki_pos;
507 508 509
	return ret;
}

510
/* caller is responsible for file_start_write/file_end_write */
511
ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos)
512
{
513 514 515 516 517 518
	struct kvec iov = {
		.iov_base	= (void *)buf,
		.iov_len	= min_t(size_t, count, MAX_RW_COUNT),
	};
	struct kiocb kiocb;
	struct iov_iter iter;
519 520
	ssize_t ret;

521 522
	if (WARN_ON_ONCE(!(file->f_mode & FMODE_WRITE)))
		return -EBADF;
523
	if (!(file->f_mode & FMODE_CAN_WRITE))
524
		return -EINVAL;
525 526 527 528 529 530
	/*
	 * 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");
531

532
	init_sync_kiocb(&kiocb, file);
533
	kiocb.ki_pos = pos ? *pos : 0;
534 535
	iov_iter_kvec(&iter, WRITE, &iov, 1, iov.iov_len);
	ret = file->f_op->write_iter(&kiocb, &iter);
536
	if (ret > 0) {
537 538
		if (pos)
			*pos = kiocb.ki_pos;
539 540 541 542 543 544
		fsnotify_modify(file);
		add_wchar(current, ret);
	}
	inc_syscw(current);
	return ret;
}
545 546 547 548 549 550 551 552
/*
 * 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);
553

554 555
ssize_t kernel_write(struct file *file, const void *buf, size_t count,
			    loff_t *pos)
556
{
557
	ssize_t ret;
558

559 560 561
	ret = rw_verify_area(WRITE, file, pos, count);
	if (ret)
		return ret;
562

563 564 565 566
	file_start_write(file);
	ret =  __kernel_write(file, buf, count, pos);
	file_end_write(file);
	return ret;
567 568 569
}
EXPORT_SYMBOL(kernel_write);

L
Linus Torvalds 已提交
570 571 572 573 574 575
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;
576
	if (!(file->f_mode & FMODE_CAN_WRITE))
L
Linus Torvalds 已提交
577
		return -EINVAL;
578
	if (unlikely(!access_ok(buf, count)))
L
Linus Torvalds 已提交
579 580 581
		return -EFAULT;

	ret = rw_verify_area(WRITE, file, pos, count);
C
Christoph Hellwig 已提交
582 583 584 585 586 587 588 589 590 591 592 593 594 595
	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 已提交
596
	}
C
Christoph Hellwig 已提交
597 598
	inc_syscw(current);
	file_end_write(file);
L
Linus Torvalds 已提交
599 600 601
	return ret;
}

602 603
/* file_ppos returns &file->f_pos or NULL if file is stream */
static inline loff_t *file_ppos(struct file *file)
L
Linus Torvalds 已提交
604
{
605
	return file->f_mode & FMODE_STREAM ? NULL : &file->f_pos;
L
Linus Torvalds 已提交
606 607
}

608
ssize_t ksys_read(unsigned int fd, char __user *buf, size_t count)
L
Linus Torvalds 已提交
609
{
610
	struct fd f = fdget_pos(fd);
L
Linus Torvalds 已提交
611 612
	ssize_t ret = -EBADF;

613
	if (f.file) {
614 615 616 617 618 619 620 621
		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;
622
		fdput_pos(f);
L
Linus Torvalds 已提交
623 624 625 626
	}
	return ret;
}

627 628 629 630 631
SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
{
	return ksys_read(fd, buf, count);
}

632
ssize_t ksys_write(unsigned int fd, const char __user *buf, size_t count)
L
Linus Torvalds 已提交
633
{
634
	struct fd f = fdget_pos(fd);
L
Linus Torvalds 已提交
635 636
	ssize_t ret = -EBADF;

637
	if (f.file) {
638 639 640 641 642 643 644 645
		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;
646
		fdput_pos(f);
L
Linus Torvalds 已提交
647 648 649 650 651
	}

	return ret;
}

652 653 654 655 656 657
SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
		size_t, count)
{
	return ksys_write(fd, buf, count);
}

658 659
ssize_t ksys_pread64(unsigned int fd, char __user *buf, size_t count,
		     loff_t pos)
L
Linus Torvalds 已提交
660
{
661
	struct fd f;
L
Linus Torvalds 已提交
662 663 664 665 666
	ssize_t ret = -EBADF;

	if (pos < 0)
		return -EINVAL;

667 668
	f = fdget(fd);
	if (f.file) {
L
Linus Torvalds 已提交
669
		ret = -ESPIPE;
670 671 672
		if (f.file->f_mode & FMODE_PREAD)
			ret = vfs_read(f.file, buf, count, &pos);
		fdput(f);
L
Linus Torvalds 已提交
673 674 675 676 677
	}

	return ret;
}

678 679 680 681 682 683 684 685
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 已提交
686
{
687
	struct fd f;
L
Linus Torvalds 已提交
688 689 690 691 692
	ssize_t ret = -EBADF;

	if (pos < 0)
		return -EINVAL;

693 694
	f = fdget(fd);
	if (f.file) {
L
Linus Torvalds 已提交
695
		ret = -ESPIPE;
696 697 698
		if (f.file->f_mode & FMODE_PWRITE)  
			ret = vfs_write(f.file, buf, count, &pos);
		fdput(f);
L
Linus Torvalds 已提交
699 700 701 702 703
	}

	return ret;
}

704 705 706 707 708 709
SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf,
			 size_t, count, loff_t, pos)
{
	return ksys_pwrite64(fd, buf, count, pos);
}

710
static ssize_t do_iter_readv_writev(struct file *filp, struct iov_iter *iter,
C
Christoph Hellwig 已提交
711
		loff_t *ppos, int type, rwf_t flags)
712 713 714 715 716
{
	struct kiocb kiocb;
	ssize_t ret;

	init_sync_kiocb(&kiocb, filp);
717 718 719
	ret = kiocb_set_rw_flags(&kiocb, flags);
	if (ret)
		return ret;
720
	kiocb.ki_pos = (ppos ? *ppos : 0);
721

722
	if (type == READ)
723
		ret = call_read_iter(filp, &kiocb, iter);
724
	else
725
		ret = call_write_iter(filp, &kiocb, iter);
726
	BUG_ON(ret == -EIOCBQUEUED);
727 728
	if (ppos)
		*ppos = kiocb.ki_pos;
729 730 731
	return ret;
}

732
/* Do it by hand, with file-ops */
733
static ssize_t do_loop_readv_writev(struct file *filp, struct iov_iter *iter,
C
Christoph Hellwig 已提交
734
		loff_t *ppos, int type, rwf_t flags)
735 736 737
{
	ssize_t ret = 0;

738
	if (flags & ~RWF_HIPRI)
739 740
		return -EOPNOTSUPP;

741 742
	while (iov_iter_count(iter)) {
		struct iovec iovec = iov_iter_iovec(iter);
743 744
		ssize_t nr;

745 746 747 748 749 750 751
		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);
		}
752 753 754 755 756 757 758

		if (nr < 0) {
			if (!ret)
				ret = nr;
			break;
		}
		ret += nr;
759
		if (nr != iovec.iov_len)
760
			break;
761
		iov_iter_advance(iter, nr);
762 763 764 765 766
	}

	return ret;
}

C
Christoph Hellwig 已提交
767
static ssize_t do_iter_read(struct file *file, struct iov_iter *iter,
C
Christoph Hellwig 已提交
768
		loff_t *pos, rwf_t flags)
L
Linus Torvalds 已提交
769 770
{
	size_t tot_len;
771
	ssize_t ret = 0;
L
Linus Torvalds 已提交
772

773 774 775 776 777
	if (!(file->f_mode & FMODE_READ))
		return -EBADF;
	if (!(file->f_mode & FMODE_CAN_READ))
		return -EINVAL;

778
	tot_len = iov_iter_count(iter);
779 780
	if (!tot_len)
		goto out;
C
Christoph Hellwig 已提交
781
	ret = rw_verify_area(READ, file, pos, tot_len);
782
	if (ret < 0)
C
Christoph Hellwig 已提交
783
		return ret;
L
Linus Torvalds 已提交
784

C
Christoph Hellwig 已提交
785 786
	if (file->f_op->read_iter)
		ret = do_iter_readv_writev(file, iter, pos, READ, flags);
787
	else
C
Christoph Hellwig 已提交
788
		ret = do_loop_readv_writev(file, iter, pos, READ, flags);
L
Linus Torvalds 已提交
789
out:
C
Christoph Hellwig 已提交
790 791
	if (ret >= 0)
		fsnotify_access(file);
L
Linus Torvalds 已提交
792 793 794
	return ret;
}

795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
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);

823
ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos,
C
Christoph Hellwig 已提交
824
		rwf_t flags)
825
{
826 827 828 829 830
	if (!file->f_op->read_iter)
		return -EINVAL;
	return do_iter_read(file, iter, ppos, flags);
}
EXPORT_SYMBOL(vfs_iter_read);
831

C
Christoph Hellwig 已提交
832
static ssize_t do_iter_write(struct file *file, struct iov_iter *iter,
C
Christoph Hellwig 已提交
833
		loff_t *pos, rwf_t flags)
C
Christoph Hellwig 已提交
834 835 836
{
	size_t tot_len;
	ssize_t ret = 0;
A
Al Viro 已提交
837

838 839 840 841 842
	if (!(file->f_mode & FMODE_WRITE))
		return -EBADF;
	if (!(file->f_mode & FMODE_CAN_WRITE))
		return -EINVAL;

C
Christoph Hellwig 已提交
843 844 845 846
	tot_len = iov_iter_count(iter);
	if (!tot_len)
		return 0;
	ret = rw_verify_area(WRITE, file, pos, tot_len);
847 848 849
	if (ret < 0)
		return ret;

C
Christoph Hellwig 已提交
850 851 852 853 854 855
	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);
856 857 858
	return ret;
}

859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
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);

887
ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos,
C
Christoph Hellwig 已提交
888
		rwf_t flags)
889 890 891 892 893 894 895
{
	if (!file->f_op->write_iter)
		return -EINVAL;
	return do_iter_write(file, iter, ppos, flags);
}
EXPORT_SYMBOL(vfs_iter_write);

896
static ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
C
Christoph Hellwig 已提交
897
		  unsigned long vlen, loff_t *pos, rwf_t flags)
L
Linus Torvalds 已提交
898
{
899 900 901 902
	struct iovec iovstack[UIO_FASTIOV];
	struct iovec *iov = iovstack;
	struct iov_iter iter;
	ssize_t ret;
L
Linus Torvalds 已提交
903

C
Christoph Hellwig 已提交
904
	ret = import_iovec(READ, vec, vlen, ARRAY_SIZE(iovstack), &iov, &iter);
905 906 907 908
	if (ret >= 0) {
		ret = do_iter_read(file, &iter, pos, flags);
		kfree(iov);
	}
L
Linus Torvalds 已提交
909

C
Christoph Hellwig 已提交
910 911
	return ret;
}
L
Linus Torvalds 已提交
912

913
static ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
C
Christoph Hellwig 已提交
914
		   unsigned long vlen, loff_t *pos, rwf_t flags)
L
Linus Torvalds 已提交
915
{
C
Christoph Hellwig 已提交
916 917 918 919
	struct iovec iovstack[UIO_FASTIOV];
	struct iovec *iov = iovstack;
	struct iov_iter iter;
	ssize_t ret;
L
Linus Torvalds 已提交
920

C
Christoph Hellwig 已提交
921
	ret = import_iovec(WRITE, vec, vlen, ARRAY_SIZE(iovstack), &iov, &iter);
922
	if (ret >= 0) {
923
		file_start_write(file);
924
		ret = do_iter_write(file, &iter, pos, flags);
925
		file_end_write(file);
926 927
		kfree(iov);
	}
C
Christoph Hellwig 已提交
928
	return ret;
L
Linus Torvalds 已提交
929 930
}

931
static ssize_t do_readv(unsigned long fd, const struct iovec __user *vec,
C
Christoph Hellwig 已提交
932
			unsigned long vlen, rwf_t flags)
L
Linus Torvalds 已提交
933
{
934
	struct fd f = fdget_pos(fd);
L
Linus Torvalds 已提交
935 936
	ssize_t ret = -EBADF;

937
	if (f.file) {
938 939 940 941 942 943 944 945
		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;
946
		fdput_pos(f);
L
Linus Torvalds 已提交
947 948 949
	}

	if (ret > 0)
950 951
		add_rchar(current, ret);
	inc_syscr(current);
L
Linus Torvalds 已提交
952 953 954
	return ret;
}

955
static ssize_t do_writev(unsigned long fd, const struct iovec __user *vec,
C
Christoph Hellwig 已提交
956
			 unsigned long vlen, rwf_t flags)
L
Linus Torvalds 已提交
957
{
958
	struct fd f = fdget_pos(fd);
L
Linus Torvalds 已提交
959 960
	ssize_t ret = -EBADF;

961
	if (f.file) {
962 963 964 965 966 967 968 969
		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;
970
		fdput_pos(f);
L
Linus Torvalds 已提交
971 972 973
	}

	if (ret > 0)
974 975
		add_wchar(current, ret);
	inc_syscw(current);
L
Linus Torvalds 已提交
976 977 978
	return ret;
}

979 980 981 982 983 984
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;
}

985
static ssize_t do_preadv(unsigned long fd, const struct iovec __user *vec,
C
Christoph Hellwig 已提交
986
			 unsigned long vlen, loff_t pos, rwf_t flags)
987
{
988
	struct fd f;
989 990 991 992 993
	ssize_t ret = -EBADF;

	if (pos < 0)
		return -EINVAL;

994 995
	f = fdget(fd);
	if (f.file) {
996
		ret = -ESPIPE;
997
		if (f.file->f_mode & FMODE_PREAD)
998
			ret = vfs_readv(f.file, vec, vlen, &pos, flags);
999
		fdput(f);
1000 1001 1002 1003 1004 1005 1006 1007
	}

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

1008
static ssize_t do_pwritev(unsigned long fd, const struct iovec __user *vec,
C
Christoph Hellwig 已提交
1009
			  unsigned long vlen, loff_t pos, rwf_t flags)
1010
{
1011
	struct fd f;
1012 1013 1014 1015 1016
	ssize_t ret = -EBADF;

	if (pos < 0)
		return -EINVAL;

1017 1018
	f = fdget(fd);
	if (f.file) {
1019
		ret = -ESPIPE;
1020
		if (f.file->f_mode & FMODE_PWRITE)
1021
			ret = vfs_writev(f.file, vec, vlen, &pos, flags);
1022
		fdput(f);
1023 1024 1025 1026 1027 1028 1029 1030
	}

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

1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
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 已提交
1053
		rwf_t, flags)
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
{
	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 已提交
1073
		rwf_t, flags)
1074 1075 1076 1077 1078 1079 1080 1081 1082
{
	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);
}

1083 1084 1085 1086 1087
/*
 * 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().
 */
1088
#ifdef CONFIG_COMPAT
1089 1090
#ifdef __ARCH_WANT_COMPAT_SYS_PREADV64
COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd,
1091
		const struct iovec __user *, vec,
1092 1093
		unsigned long, vlen, loff_t, pos)
{
1094
	return do_preadv(fd, vec, vlen, pos, 0);
1095 1096 1097
}
#endif

1098
COMPAT_SYSCALL_DEFINE5(preadv, compat_ulong_t, fd,
1099
		const struct iovec __user *, vec,
1100
		compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
1101 1102
{
	loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1103

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

1107 1108
#ifdef __ARCH_WANT_COMPAT_SYS_PREADV64V2
COMPAT_SYSCALL_DEFINE5(preadv64v2, unsigned long, fd,
1109
		const struct iovec __user *, vec,
C
Christoph Hellwig 已提交
1110
		unsigned long, vlen, loff_t, pos, rwf_t, flags)
1111
{
1112
	if (pos == -1)
1113 1114
		return do_readv(fd, vec, vlen, flags);
	return do_preadv(fd, vec, vlen, pos, flags);
1115 1116 1117
}
#endif

1118
COMPAT_SYSCALL_DEFINE6(preadv2, compat_ulong_t, fd,
1119
		const struct iovec __user *, vec,
1120
		compat_ulong_t, vlen, u32, pos_low, u32, pos_high,
C
Christoph Hellwig 已提交
1121
		rwf_t, flags)
1122 1123 1124 1125
{
	loff_t pos = ((loff_t)pos_high << 32) | pos_low;

	if (pos == -1)
1126 1127
		return do_readv(fd, vec, vlen, flags);
	return do_preadv(fd, vec, vlen, pos, flags);
1128 1129
}

1130 1131
#ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64
COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd,
1132
		const struct iovec __user *, vec,
1133 1134
		unsigned long, vlen, loff_t, pos)
{
1135
	return do_pwritev(fd, vec, vlen, pos, 0);
1136 1137 1138
}
#endif

1139
COMPAT_SYSCALL_DEFINE5(pwritev, compat_ulong_t, fd,
1140
		const struct iovec __user *,vec,
1141
		compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
1142 1143
{
	loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1144

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

1148 1149
#ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64V2
COMPAT_SYSCALL_DEFINE5(pwritev64v2, unsigned long, fd,
1150
		const struct iovec __user *, vec,
C
Christoph Hellwig 已提交
1151
		unsigned long, vlen, loff_t, pos, rwf_t, flags)
1152
{
1153
	if (pos == -1)
1154 1155
		return do_writev(fd, vec, vlen, flags);
	return do_pwritev(fd, vec, vlen, pos, flags);
1156 1157 1158
}
#endif

1159
COMPAT_SYSCALL_DEFINE6(pwritev2, compat_ulong_t, fd,
1160
		const struct iovec __user *,vec,
C
Christoph Hellwig 已提交
1161
		compat_ulong_t, vlen, u32, pos_low, u32, pos_high, rwf_t, flags)
1162 1163 1164 1165
{
	loff_t pos = ((loff_t)pos_high << 32) | pos_low;

	if (pos == -1)
1166 1167
		return do_writev(fd, vec, vlen, flags);
	return do_pwritev(fd, vec, vlen, pos, flags);
1168
}
1169
#endif /* CONFIG_COMPAT */
1170

1171 1172
static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
		  	   size_t count, loff_t max)
L
Linus Torvalds 已提交
1173
{
1174 1175
	struct fd in, out;
	struct inode *in_inode, *out_inode;
1176
	struct pipe_inode_info *opipe;
L
Linus Torvalds 已提交
1177
	loff_t pos;
1178
	loff_t out_pos;
L
Linus Torvalds 已提交
1179
	ssize_t retval;
1180
	int fl;
L
Linus Torvalds 已提交
1181 1182 1183 1184 1185

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

	/*
	 * Get output file, and verify that it is ok..
	 */
	retval = -EBADF;
1209 1210
	out = fdget(out_fd);
	if (!out.file)
L
Linus Torvalds 已提交
1211
		goto fput_in;
1212
	if (!(out.file->f_mode & FMODE_WRITE))
L
Linus Torvalds 已提交
1213
		goto fput_out;
A
Al Viro 已提交
1214 1215
	in_inode = file_inode(in.file);
	out_inode = file_inode(out.file);
1216
	out_pos = out.file->f_pos;
L
Linus Torvalds 已提交
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227

	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 已提交
1228
	fl = 0;
1229
#if 0
J
Jens Axboe 已提交
1230 1231 1232 1233 1234 1235
	/*
	 * 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.
	 */
1236
	if (in.file->f_flags & O_NONBLOCK)
J
Jens Axboe 已提交
1237
		fl = SPLICE_F_NONBLOCK;
1238
#endif
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
	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 已提交
1251 1252

	if (retval > 0) {
1253 1254
		add_rchar(current, retval);
		add_wchar(current, retval);
1255 1256
		fsnotify_access(in.file);
		fsnotify_modify(out.file);
1257 1258 1259 1260 1261
		out.file->f_pos = out_pos;
		if (ppos)
			*ppos = pos;
		else
			in.file->f_pos = pos;
L
Linus Torvalds 已提交
1262 1263
	}

1264 1265
	inc_syscr(current);
	inc_syscw(current);
1266
	if (pos > max)
L
Linus Torvalds 已提交
1267 1268 1269
		retval = -EOVERFLOW;

fput_out:
1270
	fdput(out);
L
Linus Torvalds 已提交
1271
fput_in:
1272
	fdput(in);
L
Linus Torvalds 已提交
1273 1274 1275 1276
out:
	return retval;
}

1277
SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
L
Linus Torvalds 已提交
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
{
	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);
}

1296
SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
L
Linus Torvalds 已提交
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
{
	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);
}
1312 1313 1314 1315 1316 1317 1318 1319 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

#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
1352

1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
/**
 * 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);

1383 1384 1385 1386
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)
{
1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
	/*
	 * 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)
1397 1398 1399 1400 1401 1402 1403 1404
		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);
}

1405 1406 1407 1408 1409 1410 1411 1412 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
/*
 * 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;
}

1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
/*
 * 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;

1472 1473
	ret = generic_copy_file_checks(file_in, pos_in, file_out, pos_out, &len,
				       flags);
1474 1475
	if (unlikely(ret))
		return ret;
1476

1477
	ret = rw_verify_area(READ, file_in, &pos_in, len);
1478 1479 1480 1481 1482
	if (unlikely(ret))
		return ret;

	ret = rw_verify_area(WRITE, file_out, &pos_out, len);
	if (unlikely(ret))
1483 1484 1485 1486 1487
		return ret;

	if (len == 0)
		return 0;

1488
	file_start_write(file_out);
1489

1490 1491 1492 1493
	/*
	 * Try cloning first, this is supported by more file systems, and
	 * more efficient if both clone and copy are supported (e.g. NFS).
	 */
1494 1495
	if (file_in->f_op->remap_file_range &&
	    file_inode(file_in)->i_sb == file_inode(file_out)->i_sb) {
1496 1497 1498 1499
		loff_t cloned;

		cloned = file_in->f_op->remap_file_range(file_in, pos_in,
				file_out, pos_out,
1500 1501
				min_t(loff_t, MAX_RW_COUNT, len),
				REMAP_FILE_CAN_SHORTEN);
1502 1503
		if (cloned > 0) {
			ret = cloned;
1504 1505 1506 1507
			goto done;
		}
	}

1508 1509 1510
	ret = do_copy_file_range(file_in, pos_in, file_out, pos_out, len,
				flags);
	WARN_ON_ONCE(ret == -EOPNOTSUPP);
1511
done:
1512 1513 1514 1515 1516 1517
	if (ret > 0) {
		fsnotify_access(file_in);
		add_rchar(current, ret);
		fsnotify_modify(file_out);
		add_wchar(current, ret);
	}
1518

1519 1520 1521
	inc_syscr(current);
	inc_syscw(current);

1522
	file_end_write(file_out);
1523 1524 1525 1526 1527 1528 1529 1530 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

	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;
}
1589

1590
/*
1591 1592 1593
 * 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.
1594
 */
1595
int generic_write_check_limits(struct file *file, loff_t pos, loff_t *count)
1596
{
1597 1598 1599
	struct inode *inode = file->f_mapping->host;
	loff_t max_size = inode->i_sb->s_maxbytes;
	loff_t limit = rlimit(RLIMIT_FSIZE);
1600

1601 1602 1603 1604
	if (limit != RLIM_INFINITY) {
		if (pos >= limit) {
			send_sig(SIGXFSZ, current, 0);
			return -EFBIG;
1605
		}
1606 1607
		*count = min(*count, limit - pos);
	}
1608

1609 1610
	if (!(file->f_flags & O_LARGEFILE))
		max_size = MAX_NON_LFS;
1611

1612 1613
	if (unlikely(pos >= max_size))
		return -EFBIG;
1614

1615
	*count = min(*count, max_size - pos);
1616 1617 1618

	return 0;
}
1619

1620
/*
1621
 * Performs necessary checks before doing a write
1622
 *
1623 1624 1625
 * 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.
1626
 */
1627
ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)
1628
{
1629 1630 1631
	struct file *file = iocb->ki_filp;
	struct inode *inode = file->f_mapping->host;
	loff_t count;
1632 1633
	int ret;

1634
	if (IS_SWAPFILE(inode))
1635 1636
		return -ETXTBSY;

1637 1638
	if (!iov_iter_count(from))
		return 0;
1639

1640 1641 1642
	/* FIXME: this is for backwards compatibility with 2.4 */
	if (iocb->ki_flags & IOCB_APPEND)
		iocb->ki_pos = i_size_read(inode);
1643

1644 1645
	if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
		return -EINVAL;
1646

1647 1648
	count = iov_iter_count(from);
	ret = generic_write_check_limits(file, iocb->ki_pos, &count);
1649 1650 1651
	if (ret)
		return ret;

1652 1653
	iov_iter_truncate(from, count);
	return iov_iter_count(from);
1654
}
1655
EXPORT_SYMBOL(generic_write_checks);
1656

1657 1658 1659 1660 1661
/*
 * 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)
1662
{
1663 1664
	struct inode *inode_in = file_inode(file_in);
	struct inode *inode_out = file_inode(file_out);
1665

1666 1667
	/* Don't copy dirs, pipes, sockets... */
	if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
1668
		return -EISDIR;
1669
	if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
1670 1671
		return -EINVAL;

1672 1673 1674 1675
	if (!(file_in->f_mode & FMODE_READ) ||
	    !(file_out->f_mode & FMODE_WRITE) ||
	    (file_out->f_flags & O_APPEND))
		return -EBADF;
1676

1677
	return 0;
1678
}