Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * File operations for Coda.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Original version: (C) 1996 Peter Braam 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Rewritten for Linux 2.1: (C) 1997 Carnegie Mellon University
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Carnegie Mellon encourages users of this code to contribute improvements
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * to the Coda project. Contact Peter Braam <coda@cs.cmu.edu>.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/cred.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/uio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <linux/coda.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include "coda_psdev.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include "coda_linux.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include "coda_int.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) struct coda_vm_ops {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	atomic_t refcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	struct file *coda_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	const struct vm_operations_struct *host_vm_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	struct vm_operations_struct vm_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) coda_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	struct file *coda_file = iocb->ki_filp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	struct inode *coda_inode = file_inode(coda_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	struct coda_file_info *cfi = coda_ftoc(coda_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	loff_t ki_pos = iocb->ki_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	size_t count = iov_iter_count(to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	ret = venus_access_intent(coda_inode->i_sb, coda_i2f(coda_inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 				  &cfi->cfi_access_intent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 				  count, ki_pos, CODA_ACCESS_TYPE_READ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		goto finish_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	ret = vfs_iter_read(cfi->cfi_container, to, &iocb->ki_pos, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) finish_read:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	venus_access_intent(coda_inode->i_sb, coda_i2f(coda_inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 			    &cfi->cfi_access_intent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 			    count, ki_pos, CODA_ACCESS_TYPE_READ_FINISH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) coda_file_write_iter(struct kiocb *iocb, struct iov_iter *to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	struct file *coda_file = iocb->ki_filp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	struct inode *coda_inode = file_inode(coda_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	struct coda_file_info *cfi = coda_ftoc(coda_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	struct file *host_file = cfi->cfi_container;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	loff_t ki_pos = iocb->ki_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	size_t count = iov_iter_count(to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	ret = venus_access_intent(coda_inode->i_sb, coda_i2f(coda_inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 				  &cfi->cfi_access_intent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 				  count, ki_pos, CODA_ACCESS_TYPE_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		goto finish_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	file_start_write(host_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	inode_lock(coda_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	ret = vfs_iter_write(cfi->cfi_container, to, &iocb->ki_pos, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	coda_inode->i_size = file_inode(host_file)->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	coda_inode->i_blocks = (coda_inode->i_size + 511) >> 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	coda_inode->i_mtime = coda_inode->i_ctime = current_time(coda_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	inode_unlock(coda_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	file_end_write(host_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) finish_write:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	venus_access_intent(coda_inode->i_sb, coda_i2f(coda_inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 			    &cfi->cfi_access_intent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 			    count, ki_pos, CODA_ACCESS_TYPE_WRITE_FINISH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) coda_vm_open(struct vm_area_struct *vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	struct coda_vm_ops *cvm_ops =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		container_of(vma->vm_ops, struct coda_vm_ops, vm_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	atomic_inc(&cvm_ops->refcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	if (cvm_ops->host_vm_ops && cvm_ops->host_vm_ops->open)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		cvm_ops->host_vm_ops->open(vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) coda_vm_close(struct vm_area_struct *vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	struct coda_vm_ops *cvm_ops =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		container_of(vma->vm_ops, struct coda_vm_ops, vm_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	if (cvm_ops->host_vm_ops && cvm_ops->host_vm_ops->close)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		cvm_ops->host_vm_ops->close(vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	if (atomic_dec_and_test(&cvm_ops->refcnt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		vma->vm_ops = cvm_ops->host_vm_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		fput(cvm_ops->coda_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		kfree(cvm_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) coda_file_mmap(struct file *coda_file, struct vm_area_struct *vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	struct inode *coda_inode = file_inode(coda_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	struct coda_file_info *cfi = coda_ftoc(coda_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	struct file *host_file = cfi->cfi_container;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	struct inode *host_inode = file_inode(host_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	struct coda_inode_info *cii;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	struct coda_vm_ops *cvm_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	loff_t ppos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	size_t count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	if (!host_file->f_op->mmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	if (WARN_ON(coda_file != vma->vm_file))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	count = vma->vm_end - vma->vm_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	ppos = vma->vm_pgoff * PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	ret = venus_access_intent(coda_inode->i_sb, coda_i2f(coda_inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 				  &cfi->cfi_access_intent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 				  count, ppos, CODA_ACCESS_TYPE_MMAP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	cvm_ops = kmalloc(sizeof(struct coda_vm_ops), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	if (!cvm_ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	cii = ITOC(coda_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	spin_lock(&cii->c_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	coda_file->f_mapping = host_file->f_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	if (coda_inode->i_mapping == &coda_inode->i_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		coda_inode->i_mapping = host_inode->i_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	/* only allow additional mmaps as long as userspace isn't changing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	 * the container file on us! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	else if (coda_inode->i_mapping != host_inode->i_mapping) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		spin_unlock(&cii->c_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		kfree(cvm_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	/* keep track of how often the coda_inode/host_file has been mmapped */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	cii->c_mapcount++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	cfi->cfi_mapcount++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	spin_unlock(&cii->c_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	vma->vm_file = get_file(host_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	ret = call_mmap(vma->vm_file, vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		/* if call_mmap fails, our caller will put coda_file so we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		 * should drop the reference to the host_file that we got.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		fput(host_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		kfree(cvm_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		/* here we add redirects for the open/close vm_operations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		cvm_ops->host_vm_ops = vma->vm_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		if (vma->vm_ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 			cvm_ops->vm_ops = *vma->vm_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		cvm_ops->vm_ops.open = coda_vm_open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		cvm_ops->vm_ops.close = coda_vm_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		cvm_ops->coda_file = coda_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		atomic_set(&cvm_ops->refcnt, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		vma->vm_ops = &cvm_ops->vm_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) int coda_open(struct inode *coda_inode, struct file *coda_file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	struct file *host_file = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	unsigned short flags = coda_file->f_flags & (~O_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	unsigned short coda_flags = coda_flags_to_cflags(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	struct coda_file_info *cfi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	cfi = kmalloc(sizeof(struct coda_file_info), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	if (!cfi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	error = venus_open(coda_inode->i_sb, coda_i2f(coda_inode), coda_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 			   &host_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	if (!host_file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		error = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		kfree(cfi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	host_file->f_flags |= coda_file->f_flags & (O_APPEND | O_SYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	cfi->cfi_magic = CODA_MAGIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	cfi->cfi_mapcount = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	cfi->cfi_container = host_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	/* assume access intents are supported unless we hear otherwise */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	cfi->cfi_access_intent = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	BUG_ON(coda_file->private_data != NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	coda_file->private_data = cfi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) int coda_release(struct inode *coda_inode, struct file *coda_file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	unsigned short flags = (coda_file->f_flags) & (~O_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	unsigned short coda_flags = coda_flags_to_cflags(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	struct coda_file_info *cfi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	struct coda_inode_info *cii;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	struct inode *host_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	cfi = coda_ftoc(coda_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	err = venus_close(coda_inode->i_sb, coda_i2f(coda_inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 			  coda_flags, coda_file->f_cred->fsuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	host_inode = file_inode(cfi->cfi_container);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	cii = ITOC(coda_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	/* did we mmap this file? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	spin_lock(&cii->c_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	if (coda_inode->i_mapping == &host_inode->i_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		cii->c_mapcount -= cfi->cfi_mapcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		if (!cii->c_mapcount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 			coda_inode->i_mapping = &coda_inode->i_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	spin_unlock(&cii->c_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	fput(cfi->cfi_container);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	kfree(coda_file->private_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	coda_file->private_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	/* VFS fput ignores the return value from file_operations->release, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	 * there is no use returning an error here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) int coda_fsync(struct file *coda_file, loff_t start, loff_t end, int datasync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	struct file *host_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	struct inode *coda_inode = file_inode(coda_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	struct coda_file_info *cfi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	if (!(S_ISREG(coda_inode->i_mode) || S_ISDIR(coda_inode->i_mode) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	      S_ISLNK(coda_inode->i_mode)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	err = filemap_write_and_wait_range(coda_inode->i_mapping, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	inode_lock(coda_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	cfi = coda_ftoc(coda_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	host_file = cfi->cfi_container;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	err = vfs_fsync(host_file, datasync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	if (!err && !datasync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		err = venus_fsync(coda_inode->i_sb, coda_i2f(coda_inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	inode_unlock(coda_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) const struct file_operations coda_file_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	.llseek		= generic_file_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	.read_iter	= coda_file_read_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	.write_iter	= coda_file_write_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	.mmap		= coda_file_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	.open		= coda_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	.release	= coda_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	.fsync		= coda_fsync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	.splice_read	= generic_file_splice_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) };