1823 lines · 43.8 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 <linux/filelock.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_prepare	= generic_file_readonly_mmap_prepare,	.splice_read	= filemap_splice_read,	.setlease	= generic_setlease,}; EXPORT_SYMBOL(generic_ro_fops); static inline bool unsigned_offsets(struct file *file){	return file->f_op->fop_flags & FOP_UNSIGNED_OFFSET;} /** * vfs_setpos_cookie - update the file offset for lseek and reset cookie * @file:	file structure in question * @offset:	file offset to seek to * @maxsize:	maximum file size * @cookie:	cookie to reset * * Update 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 and * reset the specified cookie to indicate that a seek happened. * * Return the specified offset on success and -EINVAL on invalid offset. */static loff_t vfs_setpos_cookie(struct file *file, loff_t offset,				loff_t maxsize, u64 *cookie){	if (offset < 0 && !unsigned_offsets(file))		return -EINVAL;	if (offset > maxsize)		return -EINVAL; 	if (offset != file->f_pos) {		file->f_pos = offset;		if (cookie)			*cookie = 0;	}	return 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){	return vfs_setpos_cookie(file, offset, maxsize, NULL);}EXPORT_SYMBOL(vfs_setpos); /** * must_set_pos - check whether f_pos has to be updated * @file: file to seek on * @offset: offset to use * @whence: type of seek operation * @eof: end of file * * Check whether f_pos needs to be updated and update @offset according * to @whence. * * Return: 0 if f_pos doesn't need to be updated, 1 if f_pos has to be * updated, and negative error code on failure. */static int must_set_pos(struct file *file, loff_t *offset, int whence, 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) {			*offset = file->f_pos;			return 0;		}		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 ((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 1;} /** * 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 * @maxsize:	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){	int ret; 	ret = must_set_pos(file, &offset, whence, eof);	if (ret < 0)		return ret;	if (ret == 0)		return offset; 	if (whence == SEEK_CUR) {		/*		 * If the file requires locking via f_pos_lock we know		 * that mutual exclusion for SEEK_CUR on the same file		 * is guaranteed. If the file isn't locked, we take		 * f_lock to protect against f_pos races with other		 * SEEK_CURs.		 */		if (file_seek_cur_needs_f_lock(file)) {			guard(spinlock)(&file->f_lock);			return vfs_setpos(file, file->f_pos + offset, maxsize);		}		return vfs_setpos(file, file->f_pos + offset, maxsize);	} 	return vfs_setpos(file, offset, maxsize);}EXPORT_SYMBOL(generic_file_llseek_size); /** * generic_llseek_cookie - versioned llseek implementation * @file:	file structure to seek on * @offset:	file offset to seek to * @whence:	type of seek * @cookie:	cookie to update * * See generic_file_llseek for a general description and locking assumptions. * * In contrast to generic_file_llseek, this function also resets a * specified cookie to indicate a seek took place. */loff_t generic_llseek_cookie(struct file *file, loff_t offset, int whence,			     u64 *cookie){	struct inode *inode = file->f_mapping->host;	loff_t maxsize = inode->i_sb->s_maxbytes;	loff_t eof = i_size_read(inode);	int ret; 	if (WARN_ON_ONCE(!cookie))		return -EINVAL; 	/*	 * Require that this is only used for directories that guarantee	 * synchronization between readdir and seek so that an update to	 * @cookie is correctly synchronized with concurrent readdir.	 */	if (WARN_ON_ONCE(!(file->f_mode & FMODE_ATOMIC_POS)))		return -EINVAL; 	ret = must_set_pos(file, &offset, whence, eof);	if (ret < 0)		return ret;	if (ret == 0)		return offset; 	/* No need to hold f_lock because we know that f_pos_lock is held. */	if (whence == SEEK_CUR)		return vfs_setpos_cookie(file, file->f_pos + offset, maxsize, cookie); 	return vfs_setpos_cookie(file, offset, maxsize, cookie);}EXPORT_SYMBOL(generic_llseek_cookie); /** * 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 implementation 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 default_llseek(struct file *file, loff_t offset, int whence){	struct inode *inode = file_inode(file);	loff_t retval; 	retval = inode_lock_killable(inode);	if (retval)		return retval;	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;		retval = offset;	}out:	inode_unlock(inode);	return retval;}EXPORT_SYMBOL(default_llseek); loff_t vfs_llseek(struct file *file, loff_t offset, int whence){	if (!(file->f_mode & FMODE_LSEEK))		return -ESPIPE;	return file->f_op->llseek(file, offset, whence);}EXPORT_SYMBOL(vfs_llseek); static off_t ksys_lseek(unsigned int fd, off_t offset, unsigned int whence){	off_t retval;	CLASS(fd_pos, f)(fd);	if (fd_empty(f))		return -EBADF; 	retval = -EINVAL;	if (whence <= SEEK_MAX) {		loff_t res = vfs_llseek(fd_file(f), offset, whence);		retval = res;		if (res != (loff_t)retval)			retval = -EOVERFLOW;	/* LFS: should only happen on 32 bit platforms */	}	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) || \	defined(__ARCH_WANT_SYS_LLSEEK)SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,		unsigned long, offset_low, loff_t __user *, result,		unsigned int, whence){	int retval;	CLASS(fd_pos, f)(fd);	loff_t offset; 	if (fd_empty(f))		return -EBADF; 	if (whence > SEEK_MAX)		return -EINVAL; 	offset = vfs_llseek(fd_file(f), ((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;	}	return retval;}#endif int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t count){	int mask = read_write == READ ? MAY_READ : MAY_WRITE;	int ret; 	if (unlikely((ssize_t) count < 0))		return -EINVAL; 	if (ppos) {		loff_t pos = *ppos; 		if (unlikely(pos < 0)) {			if (!unsigned_offsets(file))				return -EINVAL;			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 -EINVAL;		}	} 	ret = security_file_permission(file, mask);	if (ret)		return ret; 	return fsnotify_file_area_perm(file, mask, ppos, count);}EXPORT_SYMBOL(rw_verify_area); static ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos){	struct kiocb kiocb;	struct iov_iter iter;	ssize_t ret; 	init_sync_kiocb(&kiocb, filp);	kiocb.ki_pos = (ppos ? *ppos : 0);	iov_iter_ubuf(&iter, ITER_DEST, buf, len); 	ret = filp->f_op->read_iter(&kiocb, &iter);	BUG_ON(ret == -EIOCBQUEUED);	if (ppos)		*ppos = kiocb.ki_pos;	return ret;} static int warn_unsupported(struct file *file, const char *op)

Lines 1–500 of 1823.