Linux-libre 4.4.228-gnu
[librecmc/linux-libre.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/cred.h>
17 #include <linux/errno.h>
18 #include <linux/spinlock.h>
19 #include <linux/string.h>
20 #include <linux/slab.h>
21 #include <linux/uaccess.h>
22
23 #include <linux/coda.h>
24 #include <linux/coda_psdev.h>
25
26 #include "coda_linux.h"
27 #include "coda_int.h"
28
29 static ssize_t
30 coda_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
31 {
32         struct file *coda_file = iocb->ki_filp;
33         struct coda_file_info *cfi = CODA_FTOC(coda_file);
34
35         BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
36
37         return vfs_iter_read(cfi->cfi_container, to, &iocb->ki_pos);
38 }
39
40 static ssize_t
41 coda_file_splice_read(struct file *coda_file, loff_t *ppos,
42                       struct pipe_inode_info *pipe, size_t count,
43                       unsigned int flags)
44 {
45         ssize_t (*splice_read)(struct file *, loff_t *,
46                                struct pipe_inode_info *, size_t, unsigned int);
47         struct coda_file_info *cfi;
48         struct file *host_file;
49
50         cfi = CODA_FTOC(coda_file);
51         BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
52         host_file = cfi->cfi_container;
53
54         splice_read = host_file->f_op->splice_read;
55         if (!splice_read)
56                 splice_read = default_file_splice_read;
57
58         return splice_read(host_file, ppos, pipe, count, flags);
59 }
60
61 static ssize_t
62 coda_file_write_iter(struct kiocb *iocb, struct iov_iter *to)
63 {
64         struct file *coda_file = iocb->ki_filp;
65         struct inode *coda_inode = file_inode(coda_file);
66         struct coda_file_info *cfi = CODA_FTOC(coda_file);
67         struct file *host_file;
68         ssize_t ret;
69
70         BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
71
72         host_file = cfi->cfi_container;
73         file_start_write(host_file);
74         mutex_lock(&coda_inode->i_mutex);
75         ret = vfs_iter_write(cfi->cfi_container, to, &iocb->ki_pos);
76         coda_inode->i_size = file_inode(host_file)->i_size;
77         coda_inode->i_blocks = (coda_inode->i_size + 511) >> 9;
78         coda_inode->i_mtime = coda_inode->i_ctime = CURRENT_TIME_SEC;
79         mutex_unlock(&coda_inode->i_mutex);
80         file_end_write(host_file);
81         return ret;
82 }
83
84 struct coda_vm_ops {
85         atomic_t refcnt;
86         struct file *coda_file;
87         const struct vm_operations_struct *host_vm_ops;
88         struct vm_operations_struct vm_ops;
89 };
90
91 static void
92 coda_vm_open(struct vm_area_struct *vma)
93 {
94         struct coda_vm_ops *cvm_ops =
95                 container_of(vma->vm_ops, struct coda_vm_ops, vm_ops);
96
97         atomic_inc(&cvm_ops->refcnt);
98
99         if (cvm_ops->host_vm_ops && cvm_ops->host_vm_ops->open)
100                 cvm_ops->host_vm_ops->open(vma);
101 }
102
103 static void
104 coda_vm_close(struct vm_area_struct *vma)
105 {
106         struct coda_vm_ops *cvm_ops =
107                 container_of(vma->vm_ops, struct coda_vm_ops, vm_ops);
108
109         if (cvm_ops->host_vm_ops && cvm_ops->host_vm_ops->close)
110                 cvm_ops->host_vm_ops->close(vma);
111
112         if (atomic_dec_and_test(&cvm_ops->refcnt)) {
113                 vma->vm_ops = cvm_ops->host_vm_ops;
114                 fput(cvm_ops->coda_file);
115                 kfree(cvm_ops);
116         }
117 }
118
119 static int
120 coda_file_mmap(struct file *coda_file, struct vm_area_struct *vma)
121 {
122         struct coda_file_info *cfi;
123         struct coda_inode_info *cii;
124         struct file *host_file;
125         struct inode *coda_inode, *host_inode;
126         struct coda_vm_ops *cvm_ops;
127         int ret;
128
129         cfi = CODA_FTOC(coda_file);
130         BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
131         host_file = cfi->cfi_container;
132
133         if (!host_file->f_op->mmap)
134                 return -ENODEV;
135
136         if (WARN_ON(coda_file != vma->vm_file))
137                 return -EIO;
138
139         cvm_ops = kmalloc(sizeof(struct coda_vm_ops), GFP_KERNEL);
140         if (!cvm_ops)
141                 return -ENOMEM;
142
143         coda_inode = file_inode(coda_file);
144         host_inode = file_inode(host_file);
145
146         cii = ITOC(coda_inode);
147         spin_lock(&cii->c_lock);
148         coda_file->f_mapping = host_file->f_mapping;
149         if (coda_inode->i_mapping == &coda_inode->i_data)
150                 coda_inode->i_mapping = host_inode->i_mapping;
151
152         /* only allow additional mmaps as long as userspace isn't changing
153          * the container file on us! */
154         else if (coda_inode->i_mapping != host_inode->i_mapping) {
155                 spin_unlock(&cii->c_lock);
156                 kfree(cvm_ops);
157                 return -EBUSY;
158         }
159
160         /* keep track of how often the coda_inode/host_file has been mmapped */
161         cii->c_mapcount++;
162         cfi->cfi_mapcount++;
163         spin_unlock(&cii->c_lock);
164
165         vma->vm_file = get_file(host_file);
166         ret = host_file->f_op->mmap(host_file, vma);
167
168         if (ret) {
169                 /* if call_mmap fails, our caller will put coda_file so we
170                  * should drop the reference to the host_file that we got.
171                  */
172                 fput(host_file);
173                 kfree(cvm_ops);
174         } else {
175                 /* here we add redirects for the open/close vm_operations */
176                 cvm_ops->host_vm_ops = vma->vm_ops;
177                 if (vma->vm_ops)
178                         cvm_ops->vm_ops = *vma->vm_ops;
179
180                 cvm_ops->vm_ops.open = coda_vm_open;
181                 cvm_ops->vm_ops.close = coda_vm_close;
182                 cvm_ops->coda_file = coda_file;
183                 atomic_set(&cvm_ops->refcnt, 1);
184
185                 vma->vm_ops = &cvm_ops->vm_ops;
186         }
187         return ret;
188 }
189
190 int coda_open(struct inode *coda_inode, struct file *coda_file)
191 {
192         struct file *host_file = NULL;
193         int error;
194         unsigned short flags = coda_file->f_flags & (~O_EXCL);
195         unsigned short coda_flags = coda_flags_to_cflags(flags);
196         struct coda_file_info *cfi;
197
198         cfi = kmalloc(sizeof(struct coda_file_info), GFP_KERNEL);
199         if (!cfi)
200                 return -ENOMEM;
201
202         error = venus_open(coda_inode->i_sb, coda_i2f(coda_inode), coda_flags,
203                            &host_file);
204         if (!host_file)
205                 error = -EIO;
206
207         if (error) {
208                 kfree(cfi);
209                 return error;
210         }
211
212         host_file->f_flags |= coda_file->f_flags & (O_APPEND | O_SYNC);
213
214         cfi->cfi_magic = CODA_MAGIC;
215         cfi->cfi_mapcount = 0;
216         cfi->cfi_container = host_file;
217
218         BUG_ON(coda_file->private_data != NULL);
219         coda_file->private_data = cfi;
220         return 0;
221 }
222
223 int coda_release(struct inode *coda_inode, struct file *coda_file)
224 {
225         unsigned short flags = (coda_file->f_flags) & (~O_EXCL);
226         unsigned short coda_flags = coda_flags_to_cflags(flags);
227         struct coda_file_info *cfi;
228         struct coda_inode_info *cii;
229         struct inode *host_inode;
230         int err;
231
232         cfi = CODA_FTOC(coda_file);
233         BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
234
235         err = venus_close(coda_inode->i_sb, coda_i2f(coda_inode),
236                           coda_flags, coda_file->f_cred->fsuid);
237
238         host_inode = file_inode(cfi->cfi_container);
239         cii = ITOC(coda_inode);
240
241         /* did we mmap this file? */
242         spin_lock(&cii->c_lock);
243         if (coda_inode->i_mapping == &host_inode->i_data) {
244                 cii->c_mapcount -= cfi->cfi_mapcount;
245                 if (!cii->c_mapcount)
246                         coda_inode->i_mapping = &coda_inode->i_data;
247         }
248         spin_unlock(&cii->c_lock);
249
250         fput(cfi->cfi_container);
251         kfree(coda_file->private_data);
252         coda_file->private_data = NULL;
253
254         /* VFS fput ignores the return value from file_operations->release, so
255          * there is no use returning an error here */
256         return 0;
257 }
258
259 int coda_fsync(struct file *coda_file, loff_t start, loff_t end, int datasync)
260 {
261         struct file *host_file;
262         struct inode *coda_inode = file_inode(coda_file);
263         struct coda_file_info *cfi;
264         int err;
265
266         if (!(S_ISREG(coda_inode->i_mode) || S_ISDIR(coda_inode->i_mode) ||
267               S_ISLNK(coda_inode->i_mode)))
268                 return -EINVAL;
269
270         err = filemap_write_and_wait_range(coda_inode->i_mapping, start, end);
271         if (err)
272                 return err;
273         mutex_lock(&coda_inode->i_mutex);
274
275         cfi = CODA_FTOC(coda_file);
276         BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
277         host_file = cfi->cfi_container;
278
279         err = vfs_fsync(host_file, datasync);
280         if (!err && !datasync)
281                 err = venus_fsync(coda_inode->i_sb, coda_i2f(coda_inode));
282         mutex_unlock(&coda_inode->i_mutex);
283
284         return err;
285 }
286
287 const struct file_operations coda_file_operations = {
288         .llseek         = generic_file_llseek,
289         .read_iter      = coda_file_read_iter,
290         .write_iter     = coda_file_write_iter,
291         .mmap           = coda_file_mmap,
292         .open           = coda_open,
293         .release        = coda_release,
294         .fsync          = coda_fsync,
295         .splice_read    = coda_file_splice_read,
296 };
297