2225 lines · 51.6 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0/* *  linux/fs/read_write.c * *  Copyright (C) 1991, 1992  Linus Torvalds */ #include <linux/slab.h>#include <linux/stat.h>#include <linux/sched/xacct.h>#include <linux/fcntl.h>#include <linux/file.h>#include <linux/uio.h>#include <linux/fsnotify.h>#include <linux/security.h>#include <linux/export.h>#include <linux/syscalls.h>#include <linux/pagemap.h>#include <linux/splice.h>#include <linux/compat.h>#include <linux/mount.h>#include <linux/fs.h>#include "internal.h" #include <linux/uaccess.h>#include <asm/unistd.h> const struct file_operations generic_ro_fops = {	.llseek		= generic_file_llseek,	.read_iter	= generic_file_read_iter,	.mmap		= generic_file_readonly_mmap,	.splice_read	= generic_file_splice_read,}; EXPORT_SYMBOL(generic_ro_fops); static inline bool unsigned_offsets(struct file *file){	return file->f_mode & FMODE_UNSIGNED_OFFSET;} /** * 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){	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;}EXPORT_SYMBOL(vfs_setpos); /** * generic_file_llseek_size - generic llseek implementation for regular files * @file:	file structure to seek on * @offset:	file offset to seek to * @whence:	type of seek * @size:	max size of this file in file system * @eof:	offset used for SEEK_END position * * This is a variant of generic_file_llseek that allows passing in a custom * maximum file size and a custom EOF position, for e.g. hashed directories * * Synchronization: * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms) * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes. * read/writes behave like SEEK_SET against seeks. */loff_tgeneric_file_llseek_size(struct file *file, loff_t offset, int whence,		loff_t maxsize, loff_t eof){	switch (whence) {	case SEEK_END:		offset += eof;		break;	case SEEK_CUR:		/*		 * 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;		/*		 * 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);		offset = vfs_setpos(file, file->f_pos + offset, maxsize);		spin_unlock(&file->f_lock);		return offset;	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.		 */		if ((unsigned long long)offset >= eof)			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.		 */		if ((unsigned long long)offset >= eof)			return -ENXIO;		offset = eof;		break;	} 	return vfs_setpos(file, offset, maxsize);}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 * @whence:	type of seek * * This is a generic implemenation of ->llseek useable for all normal local * filesystems.  It just updates the file offset to the value specified by * @offset and @whence. */loff_t generic_file_llseek(struct file *file, loff_t offset, int whence){	struct inode *inode = file->f_mapping->host; 	return generic_file_llseek_size(file, offset, whence,					inode->i_sb->s_maxbytes,					i_size_read(inode));}EXPORT_SYMBOL(generic_file_llseek); /** * 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); /** * 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,						OFFSET_MAX, 0);	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); /** * noop_llseek - No Operation Performed llseek implementation * @file:	file structure to seek on * @offset:	file offset to seek to * @whence:	type of seek * * 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. */loff_t noop_llseek(struct file *file, loff_t offset, int whence){	return file->f_pos;}EXPORT_SYMBOL(noop_llseek); loff_t no_llseek(struct file *file, loff_t offset, int whence){	return -ESPIPE;}EXPORT_SYMBOL(no_llseek); loff_t default_llseek(struct file *file, loff_t offset, int whence){	struct inode *inode = file_inode(file);	loff_t retval; 	inode_lock(inode);	switch (whence) {		case SEEK_END:			offset += i_size_read(inode);			break;		case SEEK_CUR:			if (offset == 0) {				retval = file->f_pos;				goto out;			}			offset += file->f_pos;			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.			 */			if (offset >= inode->i_size) {				retval = -ENXIO;				goto out;			}			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.			 */			if (offset >= inode->i_size) {				retval = -ENXIO;				goto out;			}			offset = inode->i_size;			break;	}	retval = -EINVAL;	if (offset >= 0 || unsigned_offsets(file)) {		if (offset != file->f_pos) {			file->f_pos = offset;			file->f_version = 0;		}		retval = offset;	}out:	inode_unlock(inode);	return retval;}EXPORT_SYMBOL(default_llseek); loff_t vfs_llseek(struct file *file, loff_t offset, int whence){	loff_t (*fn)(struct file *, loff_t, int); 	fn = no_llseek;	if (file->f_mode & FMODE_LSEEK) {		if (file->f_op->llseek)			fn = file->f_op->llseek;	}	return fn(file, offset, whence);}EXPORT_SYMBOL(vfs_llseek); off_t ksys_lseek(unsigned int fd, off_t offset, unsigned int whence){	off_t retval;	struct fd f = fdget_pos(fd);	if (!f.file)		return -EBADF; 	retval = -EINVAL;	if (whence <= SEEK_MAX) {		loff_t res = vfs_llseek(f.file, offset, whence);		retval = res;		if (res != (loff_t)retval)			retval = -EOVERFLOW;	/* LFS: should only happen on 32 bit platforms */	}	fdput_pos(f);	return retval;} SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence){	return ksys_lseek(fd, offset, whence);} #ifdef CONFIG_COMPATCOMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence){	return ksys_lseek(fd, offset, whence);}#endif #if !defined(CONFIG_64BIT) || defined(CONFIG_COMPAT)SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,		unsigned long, offset_low, loff_t __user *, result,		unsigned int, whence){	int retval;	struct fd f = fdget_pos(fd);	loff_t offset; 	if (!f.file)		return -EBADF; 	retval = -EINVAL;	if (whence > SEEK_MAX)		goto out_putf; 	offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low,			whence); 	retval = (int)offset;	if (offset >= 0) {		retval = -EFAULT;		if (!copy_to_user(result, &offset, sizeof(offset)))			retval = 0;	}out_putf:	fdput_pos(f);	return retval;}#endif int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t count){	struct inode *inode;	int retval = -EINVAL; 	inode = file_inode(file);	if (unlikely((ssize_t) count < 0))		return retval; 	/*	 * 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;		} 		if (unlikely(inode->i_flctx && mandatory_lock(inode))) {			retval = locks_mandatory_area(inode, file, pos, pos + count - 1,					read_write == READ ? F_RDLCK : F_WRLCK);			if (retval < 0)				return retval;		}	} 	return security_file_permission(file,				read_write == READ ? MAY_READ : MAY_WRITE);} static ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos){	struct iovec iov = { .iov_base = buf, .iov_len = len };	struct kiocb kiocb;	struct iov_iter iter;	ssize_t ret; 	init_sync_kiocb(&kiocb, filp);	kiocb.ki_pos = (ppos ? *ppos : 0);	iov_iter_init(&iter, READ, &iov, 1, len); 	ret = call_read_iter(filp, &kiocb, &iter);	BUG_ON(ret == -EIOCBQUEUED);	if (ppos)		*ppos = kiocb.ki_pos;	return ret;} ssize_t __vfs_read(struct file *file, char __user *buf, size_t count,		   loff_t *pos){	if (file->f_op->read)		return file->f_op->read(file, buf, count, pos);	else if (file->f_op->read_iter)		return new_sync_read(file, buf, count, pos);	else		return -EINVAL;} ssize_t kernel_read(struct file *file, void *buf, size_t count, loff_t *pos){	mm_segment_t old_fs;	ssize_t result; 	old_fs = get_fs();	set_fs(KERNEL_DS);	/* The cast to a user pointer is valid due to the set_fs() */	result = vfs_read(file, (void __user *)buf, count, pos);	set_fs(old_fs);	return result;}EXPORT_SYMBOL(kernel_read); 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;	if (!(file->f_mode & FMODE_CAN_READ))		return -EINVAL;	if (unlikely(!access_ok(buf, count)))		return -EFAULT; 	ret = rw_verify_area(READ, file, pos, count);	if (!ret) {		if (count > MAX_RW_COUNT)			count =  MAX_RW_COUNT;		ret = __vfs_read(file, buf, count, pos);		if (ret > 0) {			fsnotify_access(file);			add_rchar(current, ret);		}		inc_syscr(current);	} 	return ret;} static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos){	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);	kiocb.ki_pos = (ppos ? *ppos : 0);	iov_iter_init(&iter, WRITE, &iov, 1, len); 	ret = call_write_iter(filp, &kiocb, &iter);	BUG_ON(ret == -EIOCBQUEUED);	if (ret > 0 && ppos)		*ppos = kiocb.ki_pos;	return ret;} static ssize_t __vfs_write(struct file *file, const char __user *p,			   size_t count, loff_t *pos){	if (file->f_op->write)		return file->f_op->write(file, p, count, pos);	else if (file->f_op->write_iter)		return new_sync_write(file, p, count, pos);	else		return -EINVAL;} 

Lines 1–500 of 2225.