162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * fs/epfs/file.c 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (c) 2022 Huawei Technologies Co., Ltd. 662306a36Sopenharmony_ci * Author: weilongping@huawei.com 762306a36Sopenharmony_ci * Create: 2022-06-10 862306a36Sopenharmony_ci */ 962306a36Sopenharmony_ci#include <linux/file.h> 1062306a36Sopenharmony_ci#include <linux/fs.h> 1162306a36Sopenharmony_ci#include <linux/fs_stack.h> 1262306a36Sopenharmony_ci#include <linux/slab.h> 1362306a36Sopenharmony_ci#include <linux/uaccess.h> 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_ci#include "internal.h" 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_cilong epfs_set_origin_fd(struct file *file, unsigned long arg) 1862306a36Sopenharmony_ci{ 1962306a36Sopenharmony_ci int fd = -1; 2062306a36Sopenharmony_ci struct file *origin_file; 2162306a36Sopenharmony_ci struct inode *inode = file->f_inode; 2262306a36Sopenharmony_ci struct epfs_inode_info *info = epfs_inode_to_private(inode); 2362306a36Sopenharmony_ci int ret = 0; 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_ci if (copy_from_user(&fd, (int *)arg, sizeof(fd))) 2662306a36Sopenharmony_ci return -EFAULT; 2762306a36Sopenharmony_ci if (IS_ENABLED(CONFIG_EPFS_DEBUG)) 2862306a36Sopenharmony_ci epfs_debug("original fd: %d", fd); 2962306a36Sopenharmony_ci origin_file = fget(fd); 3062306a36Sopenharmony_ci if (!origin_file) { 3162306a36Sopenharmony_ci epfs_err("Original file not exist!"); 3262306a36Sopenharmony_ci return -EBADF; 3362306a36Sopenharmony_ci } 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_ci mutex_lock(&info->lock); 3662306a36Sopenharmony_ci if (info->origin_file) { 3762306a36Sopenharmony_ci // origin_file had been set. 3862306a36Sopenharmony_ci ret = -EEXIST; 3962306a36Sopenharmony_ci fput(origin_file); 4062306a36Sopenharmony_ci } else if (file_inode(origin_file) == inode) { 4162306a36Sopenharmony_ci epfs_err("Could not set itself as origin_file!"); 4262306a36Sopenharmony_ci fput(origin_file); 4362306a36Sopenharmony_ci ret = -EINVAL; 4462306a36Sopenharmony_ci } else { 4562306a36Sopenharmony_ci info->origin_file = origin_file; 4662306a36Sopenharmony_ci fsstack_copy_attr_all(inode, file_inode(origin_file)); 4762306a36Sopenharmony_ci fsstack_copy_inode_size(inode, file_inode(origin_file)); 4862306a36Sopenharmony_ci } 4962306a36Sopenharmony_ci mutex_unlock(&info->lock); 5062306a36Sopenharmony_ci return ret; 5162306a36Sopenharmony_ci} 5262306a36Sopenharmony_ci 5362306a36Sopenharmony_ciint epfs_check_range(struct epfs_range *range) 5462306a36Sopenharmony_ci{ 5562306a36Sopenharmony_ci __u64 index; 5662306a36Sopenharmony_ci 5762306a36Sopenharmony_ci if (range->range[0].begin >= range->range[0].end) { 5862306a36Sopenharmony_ci epfs_err("Invalid range: [%llu, %llu)", range->range[0].begin, 5962306a36Sopenharmony_ci range->range[0].end); 6062306a36Sopenharmony_ci return -EINVAL; 6162306a36Sopenharmony_ci } 6262306a36Sopenharmony_ci 6362306a36Sopenharmony_ci for (index = 1; index < range->num; index++) { 6462306a36Sopenharmony_ci if ((range->range[index].begin >= range->range[index].end) || 6562306a36Sopenharmony_ci (range->range[index].begin < range->range[index - 1].end)) { 6662306a36Sopenharmony_ci epfs_err("Invalid range: [%llu, %llu), [%llu, %llu)", 6762306a36Sopenharmony_ci range->range[index - 1].begin, 6862306a36Sopenharmony_ci range->range[index - 1].end, 6962306a36Sopenharmony_ci range->range[index].begin, 7062306a36Sopenharmony_ci range->range[index].end); 7162306a36Sopenharmony_ci return -EINVAL; 7262306a36Sopenharmony_ci } 7362306a36Sopenharmony_ci } 7462306a36Sopenharmony_ci if (IS_ENABLED(CONFIG_EPFS_DEBUG)) { 7562306a36Sopenharmony_ci epfs_debug("epfs_range recv %llu ranges:", range->num); 7662306a36Sopenharmony_ci for (index = 0; index < range->num; index++) { 7762306a36Sopenharmony_ci epfs_debug("range:[%llu %llu)", 7862306a36Sopenharmony_ci range->range[index].begin, 7962306a36Sopenharmony_ci range->range[index].end); 8062306a36Sopenharmony_ci } 8162306a36Sopenharmony_ci epfs_debug("\n"); 8262306a36Sopenharmony_ci } 8362306a36Sopenharmony_ci return 0; 8462306a36Sopenharmony_ci} 8562306a36Sopenharmony_ci 8662306a36Sopenharmony_cilong epfs_set_range(struct file *file, unsigned long arg) 8762306a36Sopenharmony_ci{ 8862306a36Sopenharmony_ci struct inode *inode = file->f_inode; 8962306a36Sopenharmony_ci struct inode *origin_inode; 9062306a36Sopenharmony_ci struct epfs_inode_info *info = epfs_inode_to_private(inode); 9162306a36Sopenharmony_ci int ret = 0; 9262306a36Sopenharmony_ci __u64 num = 0; 9362306a36Sopenharmony_ci struct epfs_range *range; 9462306a36Sopenharmony_ci struct epfs_range header; 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ci mutex_lock(&info->lock); 9762306a36Sopenharmony_ci if (!info->origin_file) { 9862306a36Sopenharmony_ci epfs_err("origin file not exist!"); 9962306a36Sopenharmony_ci ret = -EBADF; 10062306a36Sopenharmony_ci goto out_set_range; 10162306a36Sopenharmony_ci } 10262306a36Sopenharmony_ci origin_inode = info->origin_file->f_inode; 10362306a36Sopenharmony_ci if (!in_group_p(origin_inode->i_gid)) { 10462306a36Sopenharmony_ci epfs_err("Only group member can set range: %u", 10562306a36Sopenharmony_ci i_gid_read(origin_inode)); 10662306a36Sopenharmony_ci ret = -EACCES; 10762306a36Sopenharmony_ci goto out_set_range; 10862306a36Sopenharmony_ci } 10962306a36Sopenharmony_ci 11062306a36Sopenharmony_ci if (copy_from_user(&header, (struct epfs_range *)arg, 11162306a36Sopenharmony_ci sizeof(header))) { 11262306a36Sopenharmony_ci ret = -EFAULT; 11362306a36Sopenharmony_ci epfs_err("get header failed!"); 11462306a36Sopenharmony_ci goto out_set_range; 11562306a36Sopenharmony_ci } 11662306a36Sopenharmony_ci num = header.num; 11762306a36Sopenharmony_ci 11862306a36Sopenharmony_ci if (num > EPFS_MAX_RANGES || num <= 0) { 11962306a36Sopenharmony_ci ret = -EINVAL; 12062306a36Sopenharmony_ci epfs_err("illegal num: %llu", num); 12162306a36Sopenharmony_ci goto out_set_range; 12262306a36Sopenharmony_ci } 12362306a36Sopenharmony_ci 12462306a36Sopenharmony_ci range = kzalloc(sizeof(header) + sizeof(header.range[0]) * num, 12562306a36Sopenharmony_ci GFP_KERNEL); 12662306a36Sopenharmony_ci if (!range) { 12762306a36Sopenharmony_ci ret = -ENOMEM; 12862306a36Sopenharmony_ci goto out_set_range; 12962306a36Sopenharmony_ci } 13062306a36Sopenharmony_ci 13162306a36Sopenharmony_ci if (copy_from_user(range, (struct epfs_range *)arg, 13262306a36Sopenharmony_ci sizeof(header) + sizeof(header.range[0]) * num)) { 13362306a36Sopenharmony_ci ret = -EFAULT; 13462306a36Sopenharmony_ci epfs_err("Failed to get range! num: %llu", num); 13562306a36Sopenharmony_ci kfree(range); 13662306a36Sopenharmony_ci goto out_set_range; 13762306a36Sopenharmony_ci } 13862306a36Sopenharmony_ci range->num = num; 13962306a36Sopenharmony_ci 14062306a36Sopenharmony_ci ret = epfs_check_range(range); 14162306a36Sopenharmony_ci if (ret) { 14262306a36Sopenharmony_ci kfree(range); 14362306a36Sopenharmony_ci goto out_set_range; 14462306a36Sopenharmony_ci } 14562306a36Sopenharmony_ci 14662306a36Sopenharmony_ci info->range = range; 14762306a36Sopenharmony_ciout_set_range: 14862306a36Sopenharmony_ci mutex_unlock(&info->lock); 14962306a36Sopenharmony_ci return ret; 15062306a36Sopenharmony_ci} 15162306a36Sopenharmony_ci 15262306a36Sopenharmony_cistatic long __epfs_ioctl(struct file *file, unsigned int cmd, 15362306a36Sopenharmony_ci unsigned long arg) 15462306a36Sopenharmony_ci{ 15562306a36Sopenharmony_ci long rc = -ENOTTY; 15662306a36Sopenharmony_ci 15762306a36Sopenharmony_ci if (unlikely(_IOC_TYPE(cmd) != EPFS_IOCTL_MAGIC)) { 15862306a36Sopenharmony_ci epfs_err("Failed to check epfs magic: %u", _IOC_TYPE(cmd)); 15962306a36Sopenharmony_ci return -ENOTTY; 16062306a36Sopenharmony_ci } 16162306a36Sopenharmony_ci if (unlikely(_IOC_NR(cmd) >= EPFS_IOCTL_MAXNR)) { 16262306a36Sopenharmony_ci epfs_err("Failed to check ioctl number: %u", _IOC_NR(cmd)); 16362306a36Sopenharmony_ci return -ENOTTY; 16462306a36Sopenharmony_ci } 16562306a36Sopenharmony_ci if (unlikely(!access_ok((void __user *)arg, _IOC_SIZE(cmd)))) { 16662306a36Sopenharmony_ci epfs_err("Failed to check user address space range!"); 16762306a36Sopenharmony_ci return -EFAULT; 16862306a36Sopenharmony_ci } 16962306a36Sopenharmony_ci 17062306a36Sopenharmony_ci switch (cmd) { 17162306a36Sopenharmony_ci case IOC_SET_ORIGIN_FD: 17262306a36Sopenharmony_ci return epfs_set_origin_fd(file, arg); 17362306a36Sopenharmony_ci case IOC_SET_EPFS_RANGE: 17462306a36Sopenharmony_ci return epfs_set_range(file, arg); 17562306a36Sopenharmony_ci default: 17662306a36Sopenharmony_ci epfs_info("Exit epfs unsupported ioctl, ret: %ld", rc); 17762306a36Sopenharmony_ci return rc; 17862306a36Sopenharmony_ci } 17962306a36Sopenharmony_ci} 18062306a36Sopenharmony_ci 18162306a36Sopenharmony_cistatic long epfs_compat_ioctl(struct file *file, unsigned int cmd, 18262306a36Sopenharmony_ci unsigned long arg) 18362306a36Sopenharmony_ci{ 18462306a36Sopenharmony_ci return __epfs_ioctl(file, cmd, arg); 18562306a36Sopenharmony_ci} 18662306a36Sopenharmony_ci 18762306a36Sopenharmony_cistatic long epfs_unlocked_ioctl(struct file *file, unsigned int cmd, 18862306a36Sopenharmony_ci unsigned long arg) 18962306a36Sopenharmony_ci{ 19062306a36Sopenharmony_ci return __epfs_ioctl(file, cmd, arg); 19162306a36Sopenharmony_ci} 19262306a36Sopenharmony_ci 19362306a36Sopenharmony_cistatic ssize_t epfs_read(struct file *file, char __user *buf, size_t count, 19462306a36Sopenharmony_ci loff_t *ppos) 19562306a36Sopenharmony_ci{ 19662306a36Sopenharmony_ci struct inode *inode = file_inode(file); 19762306a36Sopenharmony_ci struct epfs_inode_info *info = epfs_inode_to_private(inode); 19862306a36Sopenharmony_ci struct file *origin_file; 19962306a36Sopenharmony_ci struct epfs_range *range; 20062306a36Sopenharmony_ci ssize_t ret = 0; 20162306a36Sopenharmony_ci loff_t pos = *ppos; 20262306a36Sopenharmony_ci loff_t file_size; 20362306a36Sopenharmony_ci int current_range_index = 0; 20462306a36Sopenharmony_ci 20562306a36Sopenharmony_ci mutex_lock(&info->lock); 20662306a36Sopenharmony_ci range = info->range; 20762306a36Sopenharmony_ci if (!range) { 20862306a36Sopenharmony_ci ret = -EINVAL; 20962306a36Sopenharmony_ci epfs_err("Invalid inode range!"); 21062306a36Sopenharmony_ci goto out_read; 21162306a36Sopenharmony_ci } 21262306a36Sopenharmony_ci 21362306a36Sopenharmony_ci origin_file = info->origin_file; 21462306a36Sopenharmony_ci 21562306a36Sopenharmony_ci if (!origin_file) { 21662306a36Sopenharmony_ci ret = -ENOENT; 21762306a36Sopenharmony_ci epfs_err("origin file not exist!"); 21862306a36Sopenharmony_ci goto out_read; 21962306a36Sopenharmony_ci } 22062306a36Sopenharmony_ci 22162306a36Sopenharmony_ci // Reduce count when it will read over file size. 22262306a36Sopenharmony_ci file_size = i_size_read(file_inode(origin_file)); 22362306a36Sopenharmony_ci if (IS_ENABLED(CONFIG_EPFS_DEBUG)) 22462306a36Sopenharmony_ci if (count > (file_size - pos)) 22562306a36Sopenharmony_ci epfs_debug( 22662306a36Sopenharmony_ci "count will be truncated to %llu, as file_size=%llu, pos=%llu", 22762306a36Sopenharmony_ci file_size - pos, file_size, pos); 22862306a36Sopenharmony_ci count = count <= (file_size - pos) ? count : (file_size - pos); 22962306a36Sopenharmony_ci 23062306a36Sopenharmony_ci // Skip ranges before pos. 23162306a36Sopenharmony_ci while ((range->range[current_range_index].end <= pos) && 23262306a36Sopenharmony_ci (current_range_index < range->num)) 23362306a36Sopenharmony_ci current_range_index++; 23462306a36Sopenharmony_ci 23562306a36Sopenharmony_ci while (count > 0) { 23662306a36Sopenharmony_ci __u64 current_begin, current_end; 23762306a36Sopenharmony_ci 23862306a36Sopenharmony_ci if (current_range_index >= range->num) { 23962306a36Sopenharmony_ci // read directly when epfs range gone; 24062306a36Sopenharmony_ci if (IS_ENABLED(CONFIG_EPFS_DEBUG)) 24162306a36Sopenharmony_ci epfs_debug( 24262306a36Sopenharmony_ci "read from %llu with len %zu at the end.", 24362306a36Sopenharmony_ci pos, count); 24462306a36Sopenharmony_ci ret = vfs_read(origin_file, buf, count, &pos); 24562306a36Sopenharmony_ci break; 24662306a36Sopenharmony_ci } 24762306a36Sopenharmony_ci current_begin = range->range[current_range_index].begin; 24862306a36Sopenharmony_ci current_end = range->range[current_range_index].end; 24962306a36Sopenharmony_ci if (current_begin <= pos) { 25062306a36Sopenharmony_ci // Clear user memory 25162306a36Sopenharmony_ci unsigned long clear_len = current_end - pos; 25262306a36Sopenharmony_ci 25362306a36Sopenharmony_ci clear_len = clear_len < count ? clear_len : count; 25462306a36Sopenharmony_ci if (IS_ENABLED(CONFIG_EPFS_DEBUG)) 25562306a36Sopenharmony_ci epfs_debug( 25662306a36Sopenharmony_ci "clear user memory from %llu with len %lu", 25762306a36Sopenharmony_ci pos, clear_len); 25862306a36Sopenharmony_ci if (clear_user(buf, clear_len)) { 25962306a36Sopenharmony_ci ret = EFAULT; 26062306a36Sopenharmony_ci break; 26162306a36Sopenharmony_ci } 26262306a36Sopenharmony_ci buf += clear_len; 26362306a36Sopenharmony_ci pos += clear_len; 26462306a36Sopenharmony_ci count -= clear_len; 26562306a36Sopenharmony_ci current_range_index++; 26662306a36Sopenharmony_ci } else { 26762306a36Sopenharmony_ci // Read from pos to (next)current_begin 26862306a36Sopenharmony_ci unsigned long read_len = current_begin - pos; 26962306a36Sopenharmony_ci 27062306a36Sopenharmony_ci read_len = read_len < count ? read_len : count; 27162306a36Sopenharmony_ci if (IS_ENABLED(CONFIG_EPFS_DEBUG)) 27262306a36Sopenharmony_ci epfs_debug( 27362306a36Sopenharmony_ci "read from %llu with len %lu", 27462306a36Sopenharmony_ci pos, read_len); 27562306a36Sopenharmony_ci ret = vfs_read(origin_file, buf, read_len, &pos); 27662306a36Sopenharmony_ci if (ret < 0 || ret < read_len) { 27762306a36Sopenharmony_ci // Could not read enough bytes; 27862306a36Sopenharmony_ci break; 27962306a36Sopenharmony_ci } 28062306a36Sopenharmony_ci buf += ret; 28162306a36Sopenharmony_ci count -= ret; 28262306a36Sopenharmony_ci } 28362306a36Sopenharmony_ci } 28462306a36Sopenharmony_ci 28562306a36Sopenharmony_ci if (ret >= 0) { 28662306a36Sopenharmony_ci ret = pos - *ppos; 28762306a36Sopenharmony_ci *ppos = pos; 28862306a36Sopenharmony_ci } 28962306a36Sopenharmony_ciout_read: 29062306a36Sopenharmony_ci mutex_unlock(&info->lock); 29162306a36Sopenharmony_ci return ret; 29262306a36Sopenharmony_ci} 29362306a36Sopenharmony_ci 29462306a36Sopenharmony_ciconst struct file_operations epfs_file_fops = { 29562306a36Sopenharmony_ci .unlocked_ioctl = epfs_unlocked_ioctl, 29662306a36Sopenharmony_ci .compat_ioctl = epfs_compat_ioctl, 29762306a36Sopenharmony_ci .read = epfs_read, 29862306a36Sopenharmony_ci .llseek = generic_file_llseek, 29962306a36Sopenharmony_ci}; 300