Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * File operations for Coda. |
| 3 | * Original version: (C) 1996 Peter Braam |
| 4 | * Rewritten for Linux 2.1: (C) 1997 Carnegie Mellon University |
| 5 | * |
| 6 | * Carnegie Mellon encourages users of this code to contribute improvements |
| 7 | * to the Coda project. Contact Peter Braam <coda@cs.cmu.edu>. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/types.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/time.h> |
| 13 | #include <linux/file.h> |
| 14 | #include <linux/fs.h> |
| 15 | #include <linux/stat.h> |
Randy Dunlap | 7596b27 | 2008-11-19 11:30:27 -0800 | [diff] [blame] | 16 | #include <linux/cred.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/errno.h> |
| 18 | #include <linux/smp_lock.h> |
| 19 | #include <linux/string.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <asm/uaccess.h> |
| 22 | |
| 23 | #include <linux/coda.h> |
| 24 | #include <linux/coda_linux.h> |
| 25 | #include <linux/coda_fs_i.h> |
| 26 | #include <linux/coda_psdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Adrian Bunk | c98d8cf | 2006-03-24 03:15:53 -0800 | [diff] [blame] | 28 | #include "coda_int.h" |
| 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | static ssize_t |
| 31 | coda_file_read(struct file *coda_file, char __user *buf, size_t count, loff_t *ppos) |
| 32 | { |
| 33 | struct coda_file_info *cfi; |
| 34 | struct file *host_file; |
| 35 | |
| 36 | cfi = CODA_FTOC(coda_file); |
| 37 | BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC); |
| 38 | host_file = cfi->cfi_container; |
| 39 | |
| 40 | if (!host_file->f_op || !host_file->f_op->read) |
| 41 | return -EINVAL; |
| 42 | |
| 43 | return host_file->f_op->read(host_file, buf, count, ppos); |
| 44 | } |
| 45 | |
| 46 | static ssize_t |
Jens Axboe | 5ffc4ef | 2007-06-01 11:49:19 +0200 | [diff] [blame] | 47 | coda_file_splice_read(struct file *coda_file, loff_t *ppos, |
| 48 | struct pipe_inode_info *pipe, size_t count, |
| 49 | unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | { |
Miklos Szeredi | 6818173 | 2009-05-07 15:37:36 +0200 | [diff] [blame] | 51 | ssize_t (*splice_read)(struct file *, loff_t *, |
| 52 | struct pipe_inode_info *, size_t, unsigned int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | struct coda_file_info *cfi; |
| 54 | struct file *host_file; |
| 55 | |
| 56 | cfi = CODA_FTOC(coda_file); |
| 57 | BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC); |
| 58 | host_file = cfi->cfi_container; |
| 59 | |
Miklos Szeredi | 6818173 | 2009-05-07 15:37:36 +0200 | [diff] [blame] | 60 | splice_read = host_file->f_op->splice_read; |
| 61 | if (!splice_read) |
| 62 | splice_read = default_file_splice_read; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Miklos Szeredi | 6818173 | 2009-05-07 15:37:36 +0200 | [diff] [blame] | 64 | return splice_read(host_file, ppos, pipe, count, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | static ssize_t |
| 68 | coda_file_write(struct file *coda_file, const char __user *buf, size_t count, loff_t *ppos) |
| 69 | { |
Josef Sipek | d4176d3 | 2006-12-08 02:36:56 -0800 | [diff] [blame] | 70 | struct inode *host_inode, *coda_inode = coda_file->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | struct coda_file_info *cfi; |
| 72 | struct file *host_file; |
| 73 | ssize_t ret; |
| 74 | |
| 75 | cfi = CODA_FTOC(coda_file); |
| 76 | BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC); |
| 77 | host_file = cfi->cfi_container; |
| 78 | |
| 79 | if (!host_file->f_op || !host_file->f_op->write) |
| 80 | return -EINVAL; |
| 81 | |
Josef Sipek | d4176d3 | 2006-12-08 02:36:56 -0800 | [diff] [blame] | 82 | host_inode = host_file->f_path.dentry->d_inode; |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 83 | mutex_lock(&coda_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
| 85 | ret = host_file->f_op->write(host_file, buf, count, ppos); |
| 86 | |
| 87 | coda_inode->i_size = host_inode->i_size; |
| 88 | coda_inode->i_blocks = (coda_inode->i_size + 511) >> 9; |
| 89 | coda_inode->i_mtime = coda_inode->i_ctime = CURRENT_TIME_SEC; |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 90 | mutex_unlock(&coda_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
| 92 | return ret; |
| 93 | } |
| 94 | |
| 95 | static int |
| 96 | coda_file_mmap(struct file *coda_file, struct vm_area_struct *vma) |
| 97 | { |
| 98 | struct coda_file_info *cfi; |
| 99 | struct coda_inode_info *cii; |
| 100 | struct file *host_file; |
| 101 | struct inode *coda_inode, *host_inode; |
| 102 | |
| 103 | cfi = CODA_FTOC(coda_file); |
| 104 | BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC); |
| 105 | host_file = cfi->cfi_container; |
| 106 | |
| 107 | if (!host_file->f_op || !host_file->f_op->mmap) |
| 108 | return -ENODEV; |
| 109 | |
Josef Sipek | d4176d3 | 2006-12-08 02:36:56 -0800 | [diff] [blame] | 110 | coda_inode = coda_file->f_path.dentry->d_inode; |
| 111 | host_inode = host_file->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | coda_file->f_mapping = host_file->f_mapping; |
| 113 | if (coda_inode->i_mapping == &coda_inode->i_data) |
| 114 | coda_inode->i_mapping = host_inode->i_mapping; |
| 115 | |
| 116 | /* only allow additional mmaps as long as userspace isn't changing |
| 117 | * the container file on us! */ |
| 118 | else if (coda_inode->i_mapping != host_inode->i_mapping) |
| 119 | return -EBUSY; |
| 120 | |
| 121 | /* keep track of how often the coda_inode/host_file has been mmapped */ |
| 122 | cii = ITOC(coda_inode); |
| 123 | cii->c_mapcount++; |
| 124 | cfi->cfi_mapcount++; |
| 125 | |
| 126 | return host_file->f_op->mmap(host_file, vma); |
| 127 | } |
| 128 | |
| 129 | int coda_open(struct inode *coda_inode, struct file *coda_file) |
| 130 | { |
| 131 | struct file *host_file = NULL; |
| 132 | int error; |
| 133 | unsigned short flags = coda_file->f_flags & (~O_EXCL); |
| 134 | unsigned short coda_flags = coda_flags_to_cflags(flags); |
| 135 | struct coda_file_info *cfi; |
| 136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | cfi = kmalloc(sizeof(struct coda_file_info), GFP_KERNEL); |
Josh Triplett | 6ecbc4e | 2006-07-30 03:03:56 -0700 | [diff] [blame] | 138 | if (!cfi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
| 141 | lock_kernel(); |
| 142 | |
| 143 | error = venus_open(coda_inode->i_sb, coda_i2f(coda_inode), coda_flags, |
Jan Harkes | 38c2e43 | 2007-07-19 01:48:41 -0700 | [diff] [blame] | 144 | &host_file); |
| 145 | if (!host_file) |
| 146 | error = -EIO; |
| 147 | |
| 148 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | kfree(cfi); |
| 150 | unlock_kernel(); |
| 151 | return error; |
| 152 | } |
| 153 | |
| 154 | host_file->f_flags |= coda_file->f_flags & (O_APPEND | O_SYNC); |
| 155 | |
| 156 | cfi->cfi_magic = CODA_MAGIC; |
| 157 | cfi->cfi_mapcount = 0; |
| 158 | cfi->cfi_container = host_file; |
| 159 | |
| 160 | BUG_ON(coda_file->private_data != NULL); |
| 161 | coda_file->private_data = cfi; |
| 162 | |
| 163 | unlock_kernel(); |
| 164 | return 0; |
| 165 | } |
| 166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | int coda_release(struct inode *coda_inode, struct file *coda_file) |
| 168 | { |
| 169 | unsigned short flags = (coda_file->f_flags) & (~O_EXCL); |
| 170 | unsigned short coda_flags = coda_flags_to_cflags(flags); |
| 171 | struct coda_file_info *cfi; |
| 172 | struct coda_inode_info *cii; |
| 173 | struct inode *host_inode; |
| 174 | int err = 0; |
| 175 | |
| 176 | lock_kernel(); |
Jan Harkes | 3cf01f2 | 2007-07-19 01:48:51 -0700 | [diff] [blame] | 177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | cfi = CODA_FTOC(coda_file); |
| 179 | BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC); |
| 180 | |
Jan Harkes | d3fec42 | 2007-07-21 04:37:26 -0700 | [diff] [blame] | 181 | err = venus_close(coda_inode->i_sb, coda_i2f(coda_inode), |
David Howells | d76b0d9 | 2008-11-14 10:39:25 +1100 | [diff] [blame] | 182 | coda_flags, coda_file->f_cred->fsuid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
Josef Sipek | d4176d3 | 2006-12-08 02:36:56 -0800 | [diff] [blame] | 184 | host_inode = cfi->cfi_container->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | cii = ITOC(coda_inode); |
| 186 | |
| 187 | /* did we mmap this file? */ |
| 188 | if (coda_inode->i_mapping == &host_inode->i_data) { |
| 189 | cii->c_mapcount -= cfi->cfi_mapcount; |
| 190 | if (!cii->c_mapcount) |
| 191 | coda_inode->i_mapping = &coda_inode->i_data; |
| 192 | } |
| 193 | |
| 194 | fput(cfi->cfi_container); |
| 195 | kfree(coda_file->private_data); |
| 196 | coda_file->private_data = NULL; |
| 197 | |
| 198 | unlock_kernel(); |
Jan Harkes | d3fec42 | 2007-07-21 04:37:26 -0700 | [diff] [blame] | 199 | |
| 200 | /* VFS fput ignores the return value from file_operations->release, so |
| 201 | * there is no use returning an error here */ |
| 202 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | int coda_fsync(struct file *coda_file, struct dentry *coda_dentry, int datasync) |
| 206 | { |
| 207 | struct file *host_file; |
Christoph Hellwig | 4c728ef | 2008-12-22 21:11:15 +0100 | [diff] [blame] | 208 | struct inode *coda_inode = coda_dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | struct coda_file_info *cfi; |
| 210 | int err = 0; |
| 211 | |
| 212 | if (!(S_ISREG(coda_inode->i_mode) || S_ISDIR(coda_inode->i_mode) || |
| 213 | S_ISLNK(coda_inode->i_mode))) |
| 214 | return -EINVAL; |
| 215 | |
| 216 | cfi = CODA_FTOC(coda_file); |
| 217 | BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC); |
| 218 | host_file = cfi->cfi_container; |
| 219 | |
Christoph Hellwig | 8018ab0 | 2010-03-22 17:32:25 +0100 | [diff] [blame] | 220 | err = vfs_fsync(host_file, datasync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | if ( !err && !datasync ) { |
| 222 | lock_kernel(); |
| 223 | err = venus_fsync(coda_inode->i_sb, coda_i2f(coda_inode)); |
| 224 | unlock_kernel(); |
| 225 | } |
| 226 | |
| 227 | return err; |
| 228 | } |
| 229 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 230 | const struct file_operations coda_file_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | .llseek = generic_file_llseek, |
| 232 | .read = coda_file_read, |
| 233 | .write = coda_file_write, |
| 234 | .mmap = coda_file_mmap, |
| 235 | .open = coda_open, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | .release = coda_release, |
| 237 | .fsync = coda_fsync, |
Jens Axboe | 5ffc4ef | 2007-06-01 11:49:19 +0200 | [diff] [blame] | 238 | .splice_read = coda_file_splice_read, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | }; |
| 240 | |