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