Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * File operations for Coda.
0004  * Original version: (C) 1996 Peter Braam 
0005  * Rewritten for Linux 2.1: (C) 1997 Carnegie Mellon University
0006  *
0007  * Carnegie Mellon encourages users of this code to contribute improvements
0008  * to the Coda project. Contact Peter Braam <coda@cs.cmu.edu>.
0009  */
0010 
0011 #include <linux/refcount.h>
0012 #include <linux/types.h>
0013 #include <linux/kernel.h>
0014 #include <linux/time.h>
0015 #include <linux/file.h>
0016 #include <linux/fs.h>
0017 #include <linux/pagemap.h>
0018 #include <linux/stat.h>
0019 #include <linux/cred.h>
0020 #include <linux/errno.h>
0021 #include <linux/spinlock.h>
0022 #include <linux/string.h>
0023 #include <linux/slab.h>
0024 #include <linux/uaccess.h>
0025 #include <linux/uio.h>
0026 
0027 #include <linux/coda.h>
0028 #include "coda_psdev.h"
0029 #include "coda_linux.h"
0030 #include "coda_int.h"
0031 
0032 struct coda_vm_ops {
0033     refcount_t refcnt;
0034     struct file *coda_file;
0035     const struct vm_operations_struct *host_vm_ops;
0036     struct vm_operations_struct vm_ops;
0037 };
0038 
0039 static ssize_t
0040 coda_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
0041 {
0042     struct file *coda_file = iocb->ki_filp;
0043     struct inode *coda_inode = file_inode(coda_file);
0044     struct coda_file_info *cfi = coda_ftoc(coda_file);
0045     loff_t ki_pos = iocb->ki_pos;
0046     size_t count = iov_iter_count(to);
0047     ssize_t ret;
0048 
0049     ret = venus_access_intent(coda_inode->i_sb, coda_i2f(coda_inode),
0050                   &cfi->cfi_access_intent,
0051                   count, ki_pos, CODA_ACCESS_TYPE_READ);
0052     if (ret)
0053         goto finish_read;
0054 
0055     ret = vfs_iter_read(cfi->cfi_container, to, &iocb->ki_pos, 0);
0056 
0057 finish_read:
0058     venus_access_intent(coda_inode->i_sb, coda_i2f(coda_inode),
0059                 &cfi->cfi_access_intent,
0060                 count, ki_pos, CODA_ACCESS_TYPE_READ_FINISH);
0061     return ret;
0062 }
0063 
0064 static ssize_t
0065 coda_file_write_iter(struct kiocb *iocb, struct iov_iter *to)
0066 {
0067     struct file *coda_file = iocb->ki_filp;
0068     struct inode *coda_inode = file_inode(coda_file);
0069     struct coda_file_info *cfi = coda_ftoc(coda_file);
0070     struct file *host_file = cfi->cfi_container;
0071     loff_t ki_pos = iocb->ki_pos;
0072     size_t count = iov_iter_count(to);
0073     ssize_t ret;
0074 
0075     ret = venus_access_intent(coda_inode->i_sb, coda_i2f(coda_inode),
0076                   &cfi->cfi_access_intent,
0077                   count, ki_pos, CODA_ACCESS_TYPE_WRITE);
0078     if (ret)
0079         goto finish_write;
0080 
0081     file_start_write(host_file);
0082     inode_lock(coda_inode);
0083     ret = vfs_iter_write(cfi->cfi_container, to, &iocb->ki_pos, 0);
0084     coda_inode->i_size = file_inode(host_file)->i_size;
0085     coda_inode->i_blocks = (coda_inode->i_size + 511) >> 9;
0086     coda_inode->i_mtime = coda_inode->i_ctime = current_time(coda_inode);
0087     inode_unlock(coda_inode);
0088     file_end_write(host_file);
0089 
0090 finish_write:
0091     venus_access_intent(coda_inode->i_sb, coda_i2f(coda_inode),
0092                 &cfi->cfi_access_intent,
0093                 count, ki_pos, CODA_ACCESS_TYPE_WRITE_FINISH);
0094     return ret;
0095 }
0096 
0097 static void
0098 coda_vm_open(struct vm_area_struct *vma)
0099 {
0100     struct coda_vm_ops *cvm_ops =
0101         container_of(vma->vm_ops, struct coda_vm_ops, vm_ops);
0102 
0103     refcount_inc(&cvm_ops->refcnt);
0104 
0105     if (cvm_ops->host_vm_ops && cvm_ops->host_vm_ops->open)
0106         cvm_ops->host_vm_ops->open(vma);
0107 }
0108 
0109 static void
0110 coda_vm_close(struct vm_area_struct *vma)
0111 {
0112     struct coda_vm_ops *cvm_ops =
0113         container_of(vma->vm_ops, struct coda_vm_ops, vm_ops);
0114 
0115     if (cvm_ops->host_vm_ops && cvm_ops->host_vm_ops->close)
0116         cvm_ops->host_vm_ops->close(vma);
0117 
0118     if (refcount_dec_and_test(&cvm_ops->refcnt)) {
0119         vma->vm_ops = cvm_ops->host_vm_ops;
0120         fput(cvm_ops->coda_file);
0121         kfree(cvm_ops);
0122     }
0123 }
0124 
0125 static int
0126 coda_file_mmap(struct file *coda_file, struct vm_area_struct *vma)
0127 {
0128     struct inode *coda_inode = file_inode(coda_file);
0129     struct coda_file_info *cfi = coda_ftoc(coda_file);
0130     struct file *host_file = cfi->cfi_container;
0131     struct inode *host_inode = file_inode(host_file);
0132     struct coda_inode_info *cii;
0133     struct coda_vm_ops *cvm_ops;
0134     loff_t ppos;
0135     size_t count;
0136     int ret;
0137 
0138     if (!host_file->f_op->mmap)
0139         return -ENODEV;
0140 
0141     if (WARN_ON(coda_file != vma->vm_file))
0142         return -EIO;
0143 
0144     count = vma->vm_end - vma->vm_start;
0145     ppos = vma->vm_pgoff * PAGE_SIZE;
0146 
0147     ret = venus_access_intent(coda_inode->i_sb, coda_i2f(coda_inode),
0148                   &cfi->cfi_access_intent,
0149                   count, ppos, CODA_ACCESS_TYPE_MMAP);
0150     if (ret)
0151         return ret;
0152 
0153     cvm_ops = kmalloc(sizeof(struct coda_vm_ops), GFP_KERNEL);
0154     if (!cvm_ops)
0155         return -ENOMEM;
0156 
0157     cii = ITOC(coda_inode);
0158     spin_lock(&cii->c_lock);
0159     coda_file->f_mapping = host_file->f_mapping;
0160     if (coda_inode->i_mapping == &coda_inode->i_data)
0161         coda_inode->i_mapping = host_inode->i_mapping;
0162 
0163     /* only allow additional mmaps as long as userspace isn't changing
0164      * the container file on us! */
0165     else if (coda_inode->i_mapping != host_inode->i_mapping) {
0166         spin_unlock(&cii->c_lock);
0167         kfree(cvm_ops);
0168         return -EBUSY;
0169     }
0170 
0171     /* keep track of how often the coda_inode/host_file has been mmapped */
0172     cii->c_mapcount++;
0173     cfi->cfi_mapcount++;
0174     spin_unlock(&cii->c_lock);
0175 
0176     vma->vm_file = get_file(host_file);
0177     ret = call_mmap(vma->vm_file, vma);
0178 
0179     if (ret) {
0180         /* if call_mmap fails, our caller will put host_file so we
0181          * should drop the reference to the coda_file that we got.
0182          */
0183         fput(coda_file);
0184         kfree(cvm_ops);
0185     } else {
0186         /* here we add redirects for the open/close vm_operations */
0187         cvm_ops->host_vm_ops = vma->vm_ops;
0188         if (vma->vm_ops)
0189             cvm_ops->vm_ops = *vma->vm_ops;
0190 
0191         cvm_ops->vm_ops.open = coda_vm_open;
0192         cvm_ops->vm_ops.close = coda_vm_close;
0193         cvm_ops->coda_file = coda_file;
0194         refcount_set(&cvm_ops->refcnt, 1);
0195 
0196         vma->vm_ops = &cvm_ops->vm_ops;
0197     }
0198     return ret;
0199 }
0200 
0201 int coda_open(struct inode *coda_inode, struct file *coda_file)
0202 {
0203     struct file *host_file = NULL;
0204     int error;
0205     unsigned short flags = coda_file->f_flags & (~O_EXCL);
0206     unsigned short coda_flags = coda_flags_to_cflags(flags);
0207     struct coda_file_info *cfi;
0208 
0209     cfi = kmalloc(sizeof(struct coda_file_info), GFP_KERNEL);
0210     if (!cfi)
0211         return -ENOMEM;
0212 
0213     error = venus_open(coda_inode->i_sb, coda_i2f(coda_inode), coda_flags,
0214                &host_file);
0215     if (!host_file)
0216         error = -EIO;
0217 
0218     if (error) {
0219         kfree(cfi);
0220         return error;
0221     }
0222 
0223     host_file->f_flags |= coda_file->f_flags & (O_APPEND | O_SYNC);
0224 
0225     cfi->cfi_magic = CODA_MAGIC;
0226     cfi->cfi_mapcount = 0;
0227     cfi->cfi_container = host_file;
0228     /* assume access intents are supported unless we hear otherwise */
0229     cfi->cfi_access_intent = true;
0230 
0231     BUG_ON(coda_file->private_data != NULL);
0232     coda_file->private_data = cfi;
0233     return 0;
0234 }
0235 
0236 int coda_release(struct inode *coda_inode, struct file *coda_file)
0237 {
0238     unsigned short flags = (coda_file->f_flags) & (~O_EXCL);
0239     unsigned short coda_flags = coda_flags_to_cflags(flags);
0240     struct coda_file_info *cfi;
0241     struct coda_inode_info *cii;
0242     struct inode *host_inode;
0243 
0244     cfi = coda_ftoc(coda_file);
0245 
0246     venus_close(coda_inode->i_sb, coda_i2f(coda_inode),
0247               coda_flags, coda_file->f_cred->fsuid);
0248 
0249     host_inode = file_inode(cfi->cfi_container);
0250     cii = ITOC(coda_inode);
0251 
0252     /* did we mmap this file? */
0253     spin_lock(&cii->c_lock);
0254     if (coda_inode->i_mapping == &host_inode->i_data) {
0255         cii->c_mapcount -= cfi->cfi_mapcount;
0256         if (!cii->c_mapcount)
0257             coda_inode->i_mapping = &coda_inode->i_data;
0258     }
0259     spin_unlock(&cii->c_lock);
0260 
0261     fput(cfi->cfi_container);
0262     kfree(coda_file->private_data);
0263     coda_file->private_data = NULL;
0264 
0265     /* VFS fput ignores the return value from file_operations->release, so
0266      * there is no use returning an error here */
0267     return 0;
0268 }
0269 
0270 int coda_fsync(struct file *coda_file, loff_t start, loff_t end, int datasync)
0271 {
0272     struct file *host_file;
0273     struct inode *coda_inode = file_inode(coda_file);
0274     struct coda_file_info *cfi;
0275     int err;
0276 
0277     if (!(S_ISREG(coda_inode->i_mode) || S_ISDIR(coda_inode->i_mode) ||
0278           S_ISLNK(coda_inode->i_mode)))
0279         return -EINVAL;
0280 
0281     err = filemap_write_and_wait_range(coda_inode->i_mapping, start, end);
0282     if (err)
0283         return err;
0284     inode_lock(coda_inode);
0285 
0286     cfi = coda_ftoc(coda_file);
0287     host_file = cfi->cfi_container;
0288 
0289     err = vfs_fsync(host_file, datasync);
0290     if (!err && !datasync)
0291         err = venus_fsync(coda_inode->i_sb, coda_i2f(coda_inode));
0292     inode_unlock(coda_inode);
0293 
0294     return err;
0295 }
0296 
0297 const struct file_operations coda_file_operations = {
0298     .llseek     = generic_file_llseek,
0299     .read_iter  = coda_file_read_iter,
0300     .write_iter = coda_file_write_iter,
0301     .mmap       = coda_file_mmap,
0302     .open       = coda_open,
0303     .release    = coda_release,
0304     .fsync      = coda_fsync,
0305     .splice_read    = generic_file_splice_read,
0306 };