clean
[linux-2.4.21-pre4.git] / fs / coda / file.c
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
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
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
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
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                 unlock_kernel();
122                 return -ENOMEM;
123         }
124
125         lock_kernel();
126
127         error = venus_open(coda_inode->i_sb, coda_i2f(coda_inode), coda_flags,
128                            &host_file); 
129         if (error || !host_file) {
130                 kfree(cfi);
131                 unlock_kernel();
132                 return error;
133         }
134
135         host_file->f_flags |= coda_file->f_flags & (O_APPEND | O_SYNC);
136
137         cfi->cfi_magic = CODA_MAGIC;
138         cfi->cfi_mapcount = 0;
139         cfi->cfi_container = host_file;
140         coda_load_creds(&cfi->cfi_cred);
141
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;
145
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;
152         }
153
154         if (coda_file->private_data != NULL) BUG();
155         coda_file->private_data = cfi;
156
157         unlock_kernel();
158         return 0;
159 }
160
161 int coda_flush(struct file *coda_file)
162 {
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;
167         int err = 0, fcnt;
168
169         coda_vfs_stat.flush++;
170
171         /* No need to make an upcall when we have not made any modifications
172          * to the file */
173         if ((coda_file->f_flags & O_ACCMODE) == O_RDONLY)
174                 return 0;
175
176         if (use_coda_close)
177                 return 0;
178
179         fcnt = file_count(coda_file);
180         if (fcnt > 1) return 0;
181
182         coda_inode = coda_file->f_dentry->d_inode;
183
184         cfi = CODA_FTOC(coda_file);
185         if (!cfi || cfi->cfi_magic != CODA_MAGIC) BUG();
186
187         err = venus_store(coda_inode->i_sb, coda_i2f(coda_inode), coda_flags,
188                           &cfi->cfi_cred);
189
190         if (err == -EOPNOTSUPP) {
191                 use_coda_close = 1;
192                 err = 0;
193         }
194
195         return err;
196 }
197
198 int coda_release(struct inode *coda_inode, struct file *coda_file)
199 {
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;
205         int err = 0;
206
207         lock_kernel();
208         coda_vfs_stat.release++;
209  
210         if (!use_coda_close) {
211                 err = venus_release(coda_inode->i_sb, coda_i2f(coda_inode),
212                                     coda_flags);
213                 if (err == -EOPNOTSUPP) {
214                         use_coda_close = 1;
215                         err = 0;
216                 }
217         }
218
219         cfi = CODA_FTOC(coda_file);
220         if (!cfi || cfi->cfi_magic != CODA_MAGIC) BUG();
221
222         if (use_coda_close)
223                 err = venus_close(coda_inode->i_sb, coda_i2f(coda_inode),
224                                   coda_flags, &cfi->cfi_cred);
225
226         host_inode = cfi->cfi_container->f_dentry->d_inode;
227         cii = ITOC(coda_inode);
228
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;
234         }
235
236         fput(cfi->cfi_container);
237
238         kfree(coda_file->private_data);
239         coda_file->private_data = NULL;
240
241         unlock_kernel();
242         return err;
243 }
244
245 int coda_fsync(struct file *coda_file, struct dentry *coda_dentry, int datasync)
246 {
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;
251         int err = 0;
252
253         if (!(S_ISREG(coda_inode->i_mode) || S_ISDIR(coda_inode->i_mode) ||
254               S_ISLNK(coda_inode->i_mode)))
255                 return -EINVAL;
256
257         cfi = CODA_FTOC(coda_file);
258         if (!cfi || cfi->cfi_magic != CODA_MAGIC) BUG();
259         host_file = cfi->cfi_container;
260
261         coda_vfs_stat.fsync++;
262
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);
269         }
270
271         if ( !err && !datasync ) {
272                 lock_kernel();
273                 err = venus_fsync(coda_inode->i_sb, coda_i2f(coda_inode));
274                 unlock_kernel();
275         }
276
277         return err;
278 }
279
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,
285         open:           coda_open,
286         flush:          coda_flush,
287         release:        coda_release,
288         fsync:          coda_fsync,
289 };
290