2 * File operations for Coda.
3 * Original version: (C) 1996 Peter Braam
4 * Rewritten for Linux 2.1: (C) 1997 Carnegie Mellon University
6 * Carnegie Mellon encourages users of this code to contribute improvements
7 * to the Coda project. Contact Peter Braam <coda@cs.cmu.edu>.
10 #include <linux/types.h>
11 #include <linux/kernel.h>
12 #include <linux/sched.h>
13 #include <linux/file.h>
15 #include <linux/stat.h>
16 #include <linux/errno.h>
17 #include <linux/locks.h>
18 #include <linux/smp_lock.h>
19 #include <linux/string.h>
20 #include <asm/uaccess.h>
22 #include <linux/coda.h>
23 #include <linux/coda_linux.h>
24 #include <linux/coda_fs_i.h>
25 #include <linux/coda_psdev.h>
26 #include <linux/coda_proc.h>
28 /* if CODA_STORE fails with EOPNOTSUPP, venus clearly doesn't support
29 * CODA_STORE/CODA_RELEASE and we fall back on using the CODA_CLOSE upcall */
33 coda_file_read(struct file *coda_file, char *buf, size_t count, loff_t *ppos)
35 struct coda_file_info *cfi;
36 struct file *host_file;
38 cfi = CODA_FTOC(coda_file);
39 if (!cfi || cfi->cfi_magic != CODA_MAGIC) BUG();
40 host_file = cfi->cfi_container;
42 if (!host_file->f_op || !host_file->f_op->read)
45 return host_file->f_op->read(host_file, buf, count, ppos);
49 coda_file_write(struct file *coda_file, const char *buf, size_t count, loff_t *ppos)
51 struct inode *coda_inode = coda_file->f_dentry->d_inode;
52 struct coda_file_info *cfi;
53 struct file *host_file;
56 cfi = CODA_FTOC(coda_file);
57 if (!cfi || cfi->cfi_magic != CODA_MAGIC) BUG();
58 host_file = cfi->cfi_container;
60 if (!host_file->f_op || !host_file->f_op->write)
63 down(&coda_inode->i_sem);
65 ret = host_file->f_op->write(host_file, buf, count, ppos);
67 coda_inode->i_size = host_file->f_dentry->d_inode->i_size;
68 coda_inode->i_blocks = (coda_inode->i_size + 511) >> 9;
69 coda_inode->i_mtime = coda_inode->i_ctime = CURRENT_TIME;
70 up(&coda_inode->i_sem);
76 coda_file_mmap(struct file *coda_file, struct vm_area_struct *vma)
78 struct coda_file_info *cfi;
79 struct coda_inode_info *cii;
80 struct file *host_file;
81 struct inode *coda_inode, *host_inode;
83 cfi = CODA_FTOC(coda_file);
84 if (!cfi || cfi->cfi_magic != CODA_MAGIC) BUG();
85 host_file = cfi->cfi_container;
87 if (!host_file->f_op || !host_file->f_op->mmap)
90 coda_inode = coda_file->f_dentry->d_inode;
91 host_inode = host_file->f_dentry->d_inode;
92 if (coda_inode->i_mapping == &coda_inode->i_data)
93 coda_inode->i_mapping = host_inode->i_mapping;
95 /* only allow additional mmaps as long as userspace isn't changing
96 * the container file on us! */
97 else if (coda_inode->i_mapping != host_inode->i_mapping)
100 /* keep track of how often the coda_inode/host_file has been mmapped */
101 cii = ITOC(coda_inode);
105 return host_file->f_op->mmap(host_file, vma);
108 int coda_open(struct inode *coda_inode, struct file *coda_file)
110 struct file *host_file = NULL;
112 unsigned short flags = coda_file->f_flags & (~O_EXCL);
113 unsigned short coda_flags = coda_flags_to_cflags(flags);
114 struct coda_file_info *cfi;
115 struct inode *host_inode;
117 coda_vfs_stat.open++;
119 cfi = kmalloc(sizeof(struct coda_file_info), GFP_KERNEL);
125 error = venus_open(coda_inode->i_sb, coda_i2f(coda_inode), coda_flags,
128 if (!error && !host_file)
137 host_file->f_flags |= coda_file->f_flags & (O_APPEND | O_SYNC);
139 cfi->cfi_magic = CODA_MAGIC;
140 cfi->cfi_mapcount = 0;
141 cfi->cfi_container = host_file;
142 coda_load_creds(&cfi->cfi_cred);
144 host_inode = host_file->f_dentry->d_inode;
145 if (coda_inode->i_mapping == &coda_inode->i_data)
146 coda_inode->i_mapping = host_inode->i_mapping;
148 else if (coda_inode->i_mapping != host_inode->i_mapping) {
149 /* This is not a good thing, it doesn't happen with 'venus'
150 * Coda's own userspace daemon, but others might not provide
151 * the same guarantees. Still looking for the real fix. */
152 printk("coda_open: changed mapping\n");
153 coda_inode->i_mapping = host_inode->i_mapping;
156 if (coda_file->private_data != NULL) BUG();
157 coda_file->private_data = cfi;
163 int coda_flush(struct file *coda_file)
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 inode *coda_inode;
171 coda_vfs_stat.flush++;
173 /* No need to make an upcall when we have not made any modifications
175 if ((coda_file->f_flags & O_ACCMODE) == O_RDONLY)
181 fcnt = file_count(coda_file);
182 if (fcnt > 1) return 0;
184 coda_inode = coda_file->f_dentry->d_inode;
186 cfi = CODA_FTOC(coda_file);
187 if (!cfi || cfi->cfi_magic != CODA_MAGIC) BUG();
189 err = venus_store(coda_inode->i_sb, coda_i2f(coda_inode), coda_flags,
192 if (err == -EOPNOTSUPP) {
200 int coda_release(struct inode *coda_inode, struct file *coda_file)
202 unsigned short flags = (coda_file->f_flags) & (~O_EXCL);
203 unsigned short coda_flags = coda_flags_to_cflags(flags);
204 struct coda_file_info *cfi;
205 struct coda_inode_info *cii;
206 struct inode *host_inode;
210 coda_vfs_stat.release++;
212 if (!use_coda_close) {
213 err = venus_release(coda_inode->i_sb, coda_i2f(coda_inode),
215 if (err == -EOPNOTSUPP) {
221 cfi = CODA_FTOC(coda_file);
222 if (!cfi || cfi->cfi_magic != CODA_MAGIC) BUG();
225 err = venus_close(coda_inode->i_sb, coda_i2f(coda_inode),
226 coda_flags, &cfi->cfi_cred);
228 host_inode = cfi->cfi_container->f_dentry->d_inode;
229 cii = ITOC(coda_inode);
231 /* did we mmap this file? */
232 if (coda_inode->i_mapping == &host_inode->i_data) {
233 cii->c_mapcount -= cfi->cfi_mapcount;
234 if (!cii->c_mapcount)
235 coda_inode->i_mapping = &coda_inode->i_data;
238 fput(cfi->cfi_container);
240 kfree(coda_file->private_data);
241 coda_file->private_data = NULL;
247 int coda_fsync(struct file *coda_file, struct dentry *coda_dentry, int datasync)
249 struct file *host_file;
250 struct dentry *host_dentry;
251 struct inode *host_inode, *coda_inode = coda_dentry->d_inode;
252 struct coda_file_info *cfi;
255 if (!(S_ISREG(coda_inode->i_mode) || S_ISDIR(coda_inode->i_mode) ||
256 S_ISLNK(coda_inode->i_mode)))
259 cfi = CODA_FTOC(coda_file);
260 if (!cfi || cfi->cfi_magic != CODA_MAGIC) BUG();
261 host_file = cfi->cfi_container;
263 coda_vfs_stat.fsync++;
265 if (host_file->f_op && host_file->f_op->fsync) {
266 host_dentry = host_file->f_dentry;
267 host_inode = host_dentry->d_inode;
268 down(&host_inode->i_sem);
269 err = host_file->f_op->fsync(host_file, host_dentry, datasync);
270 up(&host_inode->i_sem);
273 if ( !err && !datasync ) {
275 err = venus_fsync(coda_inode->i_sb, coda_i2f(coda_inode));
282 struct file_operations coda_file_operations = {
283 llseek: generic_file_llseek,
284 read: coda_file_read,
285 write: coda_file_write,
286 mmap: coda_file_mmap,
289 release: coda_release,