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/time.h>
13 #include <linux/file.h>
15 #include <linux/stat.h>
16 #include <linux/errno.h>
17 #include <linux/smp_lock.h>
18 #include <linux/string.h>
19 #include <asm/uaccess.h>
21 #include <linux/coda.h>
22 #include <linux/coda_linux.h>
23 #include <linux/coda_fs_i.h>
24 #include <linux/coda_psdev.h>
25 #include <linux/coda_proc.h>
27 /* if CODA_STORE fails with EOPNOTSUPP, venus clearly doesn't support
28 * CODA_STORE/CODA_RELEASE and we fall back on using the CODA_CLOSE upcall */
32 coda_file_read(struct file *coda_file, char __user *buf, size_t count, loff_t *ppos)
34 struct coda_file_info *cfi;
35 struct file *host_file;
37 cfi = CODA_FTOC(coda_file);
38 BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
39 host_file = cfi->cfi_container;
41 if (!host_file->f_op || !host_file->f_op->read)
44 return host_file->f_op->read(host_file, buf, count, ppos);
48 coda_file_write(struct file *coda_file, const char __user *buf, size_t count, loff_t *ppos)
50 struct inode *host_inode, *coda_inode = coda_file->f_dentry->d_inode;
51 struct coda_file_info *cfi;
52 struct file *host_file;
55 cfi = CODA_FTOC(coda_file);
56 BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
57 host_file = cfi->cfi_container;
59 if (!host_file->f_op || !host_file->f_op->write)
62 host_inode = host_file->f_dentry->d_inode;
63 down(&coda_inode->i_sem);
65 ret = host_file->f_op->write(host_file, buf, count, ppos);
67 coda_inode->i_size = host_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 BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
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 coda_file->f_mapping = host_file->f_mapping;
93 if (coda_inode->i_mapping == &coda_inode->i_data)
94 coda_inode->i_mapping = host_inode->i_mapping;
96 /* only allow additional mmaps as long as userspace isn't changing
97 * the container file on us! */
98 else if (coda_inode->i_mapping != host_inode->i_mapping)
101 /* keep track of how often the coda_inode/host_file has been mmapped */
102 cii = ITOC(coda_inode);
106 return host_file->f_op->mmap(host_file, vma);
109 int coda_open(struct inode *coda_inode, struct file *coda_file)
111 struct file *host_file = NULL;
113 unsigned short flags = coda_file->f_flags & (~O_EXCL);
114 unsigned short coda_flags = coda_flags_to_cflags(flags);
115 struct coda_file_info *cfi;
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;
141 BUG_ON(coda_file->private_data != NULL);
142 coda_file->private_data = cfi;
148 int coda_flush(struct file *coda_file)
150 unsigned short flags = coda_file->f_flags & ~O_EXCL;
151 unsigned short coda_flags = coda_flags_to_cflags(flags);
152 struct coda_file_info *cfi;
153 struct inode *coda_inode;
158 coda_vfs_stat.flush++;
160 /* last close semantics */
161 fcnt = file_count(coda_file);
165 /* No need to make an upcall when we have not made any modifications
167 if ((coda_file->f_flags & O_ACCMODE) == O_RDONLY)
173 cfi = CODA_FTOC(coda_file);
174 BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
176 coda_inode = coda_file->f_dentry->d_inode;
178 err = venus_store(coda_inode->i_sb, coda_i2f(coda_inode), coda_flags,
181 if (err == -EOPNOTSUPP) {
191 int coda_release(struct inode *coda_inode, struct file *coda_file)
193 unsigned short flags = (coda_file->f_flags) & (~O_EXCL);
194 unsigned short coda_flags = coda_flags_to_cflags(flags);
195 struct coda_file_info *cfi;
196 struct coda_inode_info *cii;
197 struct inode *host_inode;
201 coda_vfs_stat.release++;
203 if (!use_coda_close) {
204 err = venus_release(coda_inode->i_sb, coda_i2f(coda_inode),
206 if (err == -EOPNOTSUPP) {
212 cfi = CODA_FTOC(coda_file);
213 BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
216 err = venus_close(coda_inode->i_sb, coda_i2f(coda_inode),
217 coda_flags, coda_file->f_uid);
219 host_inode = cfi->cfi_container->f_dentry->d_inode;
220 cii = ITOC(coda_inode);
222 /* did we mmap this file? */
223 if (coda_inode->i_mapping == &host_inode->i_data) {
224 cii->c_mapcount -= cfi->cfi_mapcount;
225 if (!cii->c_mapcount)
226 coda_inode->i_mapping = &coda_inode->i_data;
229 fput(cfi->cfi_container);
230 kfree(coda_file->private_data);
231 coda_file->private_data = NULL;
237 int coda_fsync(struct file *coda_file, struct dentry *coda_dentry, int datasync)
239 struct file *host_file;
240 struct dentry *host_dentry;
241 struct inode *host_inode, *coda_inode = coda_dentry->d_inode;
242 struct coda_file_info *cfi;
245 if (!(S_ISREG(coda_inode->i_mode) || S_ISDIR(coda_inode->i_mode) ||
246 S_ISLNK(coda_inode->i_mode)))
249 cfi = CODA_FTOC(coda_file);
250 BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
251 host_file = cfi->cfi_container;
253 coda_vfs_stat.fsync++;
255 if (host_file->f_op && host_file->f_op->fsync) {
256 host_dentry = host_file->f_dentry;
257 host_inode = host_dentry->d_inode;
258 down(&host_inode->i_sem);
259 err = host_file->f_op->fsync(host_file, host_dentry, datasync);
260 up(&host_inode->i_sem);
263 if ( !err && !datasync ) {
265 err = venus_fsync(coda_inode->i_sb, coda_i2f(coda_inode));
272 struct file_operations coda_file_operations = {
273 .llseek = generic_file_llseek,
274 .read = coda_file_read,
275 .write = coda_file_write,
276 .mmap = coda_file_mmap,
279 .release = coda_release,