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);
127 error = venus_open(coda_inode->i_sb, coda_i2f(coda_inode), coda_flags,
129 if (error || !host_file) {
135 host_file->f_flags |= coda_file->f_flags & (O_APPEND | O_SYNC);
137 cfi->cfi_magic = CODA_MAGIC;
138 cfi->cfi_mapcount = 0;
139 cfi->cfi_container = host_file;
140 coda_load_creds(&cfi->cfi_cred);
142 host_inode = host_file->f_dentry->d_inode;
143 if (coda_inode->i_mapping == &coda_inode->i_data)
144 coda_inode->i_mapping = host_inode->i_mapping;
146 else if (coda_inode->i_mapping != host_inode->i_mapping) {
147 /* This is not a good thing, it doesn't happen with 'venus'
148 * Coda's own userspace daemon, but others might not provide
149 * the same guarantees. Still looking for the real fix. */
150 printk("coda_open: changed mapping\n");
151 coda_inode->i_mapping = host_inode->i_mapping;
154 if (coda_file->private_data != NULL) BUG();
155 coda_file->private_data = cfi;
161 int coda_flush(struct file *coda_file)
163 unsigned short flags = coda_file->f_flags & ~O_EXCL;
164 unsigned short coda_flags = coda_flags_to_cflags(flags);
165 struct coda_file_info *cfi;
166 struct inode *coda_inode;
169 coda_vfs_stat.flush++;
171 /* No need to make an upcall when we have not made any modifications
173 if ((coda_file->f_flags & O_ACCMODE) == O_RDONLY)
179 fcnt = file_count(coda_file);
180 if (fcnt > 1) return 0;
182 coda_inode = coda_file->f_dentry->d_inode;
184 cfi = CODA_FTOC(coda_file);
185 if (!cfi || cfi->cfi_magic != CODA_MAGIC) BUG();
187 err = venus_store(coda_inode->i_sb, coda_i2f(coda_inode), coda_flags,
190 if (err == -EOPNOTSUPP) {
198 int coda_release(struct inode *coda_inode, struct file *coda_file)
200 unsigned short flags = (coda_file->f_flags) & (~O_EXCL);
201 unsigned short coda_flags = coda_flags_to_cflags(flags);
202 struct coda_file_info *cfi;
203 struct coda_inode_info *cii;
204 struct inode *host_inode;
208 coda_vfs_stat.release++;
210 if (!use_coda_close) {
211 err = venus_release(coda_inode->i_sb, coda_i2f(coda_inode),
213 if (err == -EOPNOTSUPP) {
219 cfi = CODA_FTOC(coda_file);
220 if (!cfi || cfi->cfi_magic != CODA_MAGIC) BUG();
223 err = venus_close(coda_inode->i_sb, coda_i2f(coda_inode),
224 coda_flags, &cfi->cfi_cred);
226 host_inode = cfi->cfi_container->f_dentry->d_inode;
227 cii = ITOC(coda_inode);
229 /* did we mmap this file? */
230 if (coda_inode->i_mapping == &host_inode->i_data) {
231 cii->c_mapcount -= cfi->cfi_mapcount;
232 if (!cii->c_mapcount)
233 coda_inode->i_mapping = &coda_inode->i_data;
236 fput(cfi->cfi_container);
238 kfree(coda_file->private_data);
239 coda_file->private_data = NULL;
245 int coda_fsync(struct file *coda_file, struct dentry *coda_dentry, int datasync)
247 struct file *host_file;
248 struct dentry *host_dentry;
249 struct inode *host_inode, *coda_inode = coda_dentry->d_inode;
250 struct coda_file_info *cfi;
253 if (!(S_ISREG(coda_inode->i_mode) || S_ISDIR(coda_inode->i_mode) ||
254 S_ISLNK(coda_inode->i_mode)))
257 cfi = CODA_FTOC(coda_file);
258 if (!cfi || cfi->cfi_magic != CODA_MAGIC) BUG();
259 host_file = cfi->cfi_container;
261 coda_vfs_stat.fsync++;
263 if (host_file->f_op && host_file->f_op->fsync) {
264 host_dentry = host_file->f_dentry;
265 host_inode = host_dentry->d_inode;
266 down(&host_inode->i_sem);
267 err = host_file->f_op->fsync(host_file, host_dentry, datasync);
268 up(&host_inode->i_sem);
271 if ( !err && !datasync ) {
273 err = venus_fsync(coda_inode->i_sb, coda_i2f(coda_inode));
280 struct file_operations coda_file_operations = {
281 llseek: generic_file_llseek,
282 read: coda_file_read,
283 write: coda_file_write,
284 mmap: coda_file_mmap,
287 release: coda_release,