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