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/sched.h>
13 #include <linux/file.h>
14 #include <linux/fs.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>
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>
26 #include <linux/coda_proc.h>
27
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 */
30 int use_coda_close;
31
32 static ssize_t
coda_file_read(struct file * coda_file,char * buf,size_t count,loff_t * ppos)33 coda_file_read(struct file *coda_file, char *buf, size_t count, loff_t *ppos)
34 {
35 struct coda_file_info *cfi;
36 struct file *host_file;
37
38 cfi = CODA_FTOC(coda_file);
39 if (!cfi || cfi->cfi_magic != CODA_MAGIC) BUG();
40 host_file = cfi->cfi_container;
41
42 if (!host_file->f_op || !host_file->f_op->read)
43 return -EINVAL;
44
45 return host_file->f_op->read(host_file, buf, count, ppos);
46 }
47
48 static ssize_t
coda_file_write(struct file * coda_file,const char * buf,size_t count,loff_t * ppos)49 coda_file_write(struct file *coda_file, const char *buf, size_t count, loff_t *ppos)
50 {
51 struct inode *coda_inode = coda_file->f_dentry->d_inode;
52 struct coda_file_info *cfi;
53 struct file *host_file;
54 ssize_t ret;
55
56 cfi = CODA_FTOC(coda_file);
57 if (!cfi || cfi->cfi_magic != CODA_MAGIC) BUG();
58 host_file = cfi->cfi_container;
59
60 if (!host_file->f_op || !host_file->f_op->write)
61 return -EINVAL;
62
63 down(&coda_inode->i_sem);
64
65 ret = host_file->f_op->write(host_file, buf, count, ppos);
66
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);
71
72 return ret;
73 }
74
75 static int
coda_file_mmap(struct file * coda_file,struct vm_area_struct * vma)76 coda_file_mmap(struct file *coda_file, struct vm_area_struct *vma)
77 {
78 struct coda_file_info *cfi;
79 struct coda_inode_info *cii;
80 struct file *host_file;
81 struct inode *coda_inode, *host_inode;
82
83 cfi = CODA_FTOC(coda_file);
84 if (!cfi || cfi->cfi_magic != CODA_MAGIC) BUG();
85 host_file = cfi->cfi_container;
86
87 if (!host_file->f_op || !host_file->f_op->mmap)
88 return -ENODEV;
89
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;
94
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)
98 return -EBUSY;
99
100 /* keep track of how often the coda_inode/host_file has been mmapped */
101 cii = ITOC(coda_inode);
102 cii->c_mapcount++;
103 cfi->cfi_mapcount++;
104
105 return host_file->f_op->mmap(host_file, vma);
106 }
107
coda_open(struct inode * coda_inode,struct file * coda_file)108 int coda_open(struct inode *coda_inode, struct file *coda_file)
109 {
110 struct file *host_file = NULL;
111 int error;
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;
116
117 coda_vfs_stat.open++;
118
119 cfi = kmalloc(sizeof(struct coda_file_info), GFP_KERNEL);
120 if (!cfi)
121 return -ENOMEM;
122
123 lock_kernel();
124
125 error = venus_open(coda_inode->i_sb, coda_i2f(coda_inode), coda_flags,
126 &host_file);
127
128 if (!error && !host_file)
129 error = EBADF;
130
131 if (error) {
132 kfree(cfi);
133 unlock_kernel();
134 return error;
135 }
136
137 host_file->f_flags |= coda_file->f_flags & (O_APPEND | O_SYNC);
138
139 cfi->cfi_magic = CODA_MAGIC;
140 cfi->cfi_mapcount = 0;
141 cfi->cfi_container = host_file;
142 coda_load_creds(&cfi->cfi_cred);
143
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;
147
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;
154 }
155
156 if (coda_file->private_data != NULL) BUG();
157 coda_file->private_data = cfi;
158
159 unlock_kernel();
160 return 0;
161 }
162
coda_flush(struct file * coda_file)163 int coda_flush(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 inode *coda_inode;
169 int err = 0, fcnt;
170
171 coda_vfs_stat.flush++;
172
173 /* No need to make an upcall when we have not made any modifications
174 * to the file */
175 if ((coda_file->f_flags & O_ACCMODE) == O_RDONLY)
176 return 0;
177
178 if (use_coda_close)
179 return 0;
180
181 fcnt = file_count(coda_file);
182 if (fcnt > 1) return 0;
183
184 coda_inode = coda_file->f_dentry->d_inode;
185
186 cfi = CODA_FTOC(coda_file);
187 if (!cfi || cfi->cfi_magic != CODA_MAGIC) BUG();
188
189 err = venus_store(coda_inode->i_sb, coda_i2f(coda_inode), coda_flags,
190 &cfi->cfi_cred);
191
192 if (err == -EOPNOTSUPP) {
193 use_coda_close = 1;
194 err = 0;
195 }
196
197 return err;
198 }
199
coda_release(struct inode * coda_inode,struct file * coda_file)200 int coda_release(struct inode *coda_inode, struct file *coda_file)
201 {
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;
207 int err = 0;
208
209 lock_kernel();
210 coda_vfs_stat.release++;
211
212 if (!use_coda_close) {
213 err = venus_release(coda_inode->i_sb, coda_i2f(coda_inode),
214 coda_flags);
215 if (err == -EOPNOTSUPP) {
216 use_coda_close = 1;
217 err = 0;
218 }
219 }
220
221 cfi = CODA_FTOC(coda_file);
222 if (!cfi || cfi->cfi_magic != CODA_MAGIC) BUG();
223
224 if (use_coda_close)
225 err = venus_close(coda_inode->i_sb, coda_i2f(coda_inode),
226 coda_flags, &cfi->cfi_cred);
227
228 host_inode = cfi->cfi_container->f_dentry->d_inode;
229 cii = ITOC(coda_inode);
230
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;
236 }
237
238 fput(cfi->cfi_container);
239
240 kfree(coda_file->private_data);
241 coda_file->private_data = NULL;
242
243 unlock_kernel();
244 return err;
245 }
246
coda_fsync(struct file * coda_file,struct dentry * coda_dentry,int datasync)247 int coda_fsync(struct file *coda_file, struct dentry *coda_dentry, int datasync)
248 {
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;
253 int err = 0;
254
255 if (!(S_ISREG(coda_inode->i_mode) || S_ISDIR(coda_inode->i_mode) ||
256 S_ISLNK(coda_inode->i_mode)))
257 return -EINVAL;
258
259 cfi = CODA_FTOC(coda_file);
260 if (!cfi || cfi->cfi_magic != CODA_MAGIC) BUG();
261 host_file = cfi->cfi_container;
262
263 coda_vfs_stat.fsync++;
264
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);
271 }
272
273 if ( !err && !datasync ) {
274 lock_kernel();
275 err = venus_fsync(coda_inode->i_sb, coda_i2f(coda_inode));
276 unlock_kernel();
277 }
278
279 return err;
280 }
281
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,
287 open: coda_open,
288 flush: coda_flush,
289 release: coda_release,
290 fsync: coda_fsync,
291 };
292
293