patch-2_6_7-vs1_9_1_12
[linux-2.6.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/time.h>
13 #include <linux/file.h>
14 #include <linux/fs.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>
20
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>
26
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 */
29 int use_coda_close;
30
31 static ssize_t
32 coda_file_read(struct file *coda_file, char __user *buf, size_t count, loff_t *ppos)
33 {
34         struct coda_file_info *cfi;
35         struct file *host_file;
36
37         cfi = CODA_FTOC(coda_file);
38         BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
39         host_file = cfi->cfi_container;
40
41         if (!host_file->f_op || !host_file->f_op->read)
42                 return -EINVAL;
43
44         return host_file->f_op->read(host_file, buf, count, ppos);
45 }
46
47 static ssize_t
48 coda_file_write(struct file *coda_file, const char __user *buf, size_t count, loff_t *ppos)
49 {
50         struct inode *host_inode, *coda_inode = coda_file->f_dentry->d_inode;
51         struct coda_file_info *cfi;
52         struct file *host_file;
53         ssize_t ret;
54
55         cfi = CODA_FTOC(coda_file);
56         BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
57         host_file = cfi->cfi_container;
58
59         if (!host_file->f_op || !host_file->f_op->write)
60                 return -EINVAL;
61
62         host_inode = host_file->f_dentry->d_inode;
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_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         BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
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         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;
95
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)
99                 return -EBUSY;
100
101         /* keep track of how often the coda_inode/host_file has been mmapped */
102         cii = ITOC(coda_inode);
103         cii->c_mapcount++;
104         cfi->cfi_mapcount++;
105
106         return host_file->f_op->mmap(host_file, vma);
107 }
108
109 int coda_open(struct inode *coda_inode, struct file *coda_file)
110 {
111         struct file *host_file = NULL;
112         int error;
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;
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
141         BUG_ON(coda_file->private_data != NULL);
142         coda_file->private_data = cfi;
143
144         unlock_kernel();
145         return 0;
146 }
147
148 int coda_flush(struct file *coda_file)
149 {
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;
154         int err = 0, fcnt;
155
156         lock_kernel();
157
158         coda_vfs_stat.flush++;
159
160         /* last close semantics */
161         fcnt = file_count(coda_file);
162         if (fcnt > 1)
163                 goto out;
164
165         /* No need to make an upcall when we have not made any modifications
166          * to the file */
167         if ((coda_file->f_flags & O_ACCMODE) == O_RDONLY)
168                 goto out;
169
170         if (use_coda_close)
171                 goto out;
172
173         cfi = CODA_FTOC(coda_file);
174         BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
175
176         coda_inode = coda_file->f_dentry->d_inode;
177
178         err = venus_store(coda_inode->i_sb, coda_i2f(coda_inode), coda_flags,
179                           coda_file->f_uid);
180
181         if (err == -EOPNOTSUPP) {
182                 use_coda_close = 1;
183                 err = 0;
184         }
185
186 out:
187         unlock_kernel();
188         return err;
189 }
190
191 int coda_release(struct inode *coda_inode, struct file *coda_file)
192 {
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;
198         int err = 0;
199
200         lock_kernel();
201         coda_vfs_stat.release++;
202  
203         if (!use_coda_close) {
204                 err = venus_release(coda_inode->i_sb, coda_i2f(coda_inode),
205                                     coda_flags);
206                 if (err == -EOPNOTSUPP) {
207                         use_coda_close = 1;
208                         err = 0;
209                 }
210         }
211
212         cfi = CODA_FTOC(coda_file);
213         BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
214
215         if (use_coda_close)
216                 err = venus_close(coda_inode->i_sb, coda_i2f(coda_inode),
217                                   coda_flags, coda_file->f_uid);
218
219         host_inode = cfi->cfi_container->f_dentry->d_inode;
220         cii = ITOC(coda_inode);
221
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;
227         }
228
229         fput(cfi->cfi_container);
230         kfree(coda_file->private_data);
231         coda_file->private_data = NULL;
232
233         unlock_kernel();
234         return err;
235 }
236
237 int coda_fsync(struct file *coda_file, struct dentry *coda_dentry, int datasync)
238 {
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;
243         int err = 0;
244
245         if (!(S_ISREG(coda_inode->i_mode) || S_ISDIR(coda_inode->i_mode) ||
246               S_ISLNK(coda_inode->i_mode)))
247                 return -EINVAL;
248
249         cfi = CODA_FTOC(coda_file);
250         BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
251         host_file = cfi->cfi_container;
252
253         coda_vfs_stat.fsync++;
254
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);
261         }
262
263         if ( !err && !datasync ) {
264                 lock_kernel();
265                 err = venus_fsync(coda_inode->i_sb, coda_i2f(coda_inode));
266                 unlock_kernel();
267         }
268
269         return err;
270 }
271
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,
277         .open           = coda_open,
278         .flush          = coda_flush,
279         .release        = coda_release,
280         .fsync          = coda_fsync,
281 };
282