Linux 4.19.133
[linux/fpc-iii.git] / fs / coda / file.c
blob43d371551d2b1f571e0a1a87f9ac340d45e0d189
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * File operations for Coda.
4 * Original version: (C) 1996 Peter Braam
5 * Rewritten for Linux 2.1: (C) 1997 Carnegie Mellon University
7 * Carnegie Mellon encourages users of this code to contribute improvements
8 * to the Coda project. Contact Peter Braam <coda@cs.cmu.edu>.
9 */
11 #include <linux/types.h>
12 #include <linux/kernel.h>
13 #include <linux/time.h>
14 #include <linux/file.h>
15 #include <linux/fs.h>
16 #include <linux/stat.h>
17 #include <linux/cred.h>
18 #include <linux/errno.h>
19 #include <linux/spinlock.h>
20 #include <linux/string.h>
21 #include <linux/slab.h>
22 #include <linux/uaccess.h>
24 #include <linux/coda.h>
25 #include <linux/coda_psdev.h>
27 #include "coda_linux.h"
28 #include "coda_int.h"
30 struct coda_vm_ops {
31 atomic_t refcnt;
32 struct file *coda_file;
33 const struct vm_operations_struct *host_vm_ops;
34 struct vm_operations_struct vm_ops;
37 static ssize_t
38 coda_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
40 struct file *coda_file = iocb->ki_filp;
41 struct coda_file_info *cfi = CODA_FTOC(coda_file);
43 BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
45 return vfs_iter_read(cfi->cfi_container, to, &iocb->ki_pos, 0);
48 static ssize_t
49 coda_file_write_iter(struct kiocb *iocb, struct iov_iter *to)
51 struct file *coda_file = iocb->ki_filp;
52 struct inode *coda_inode = file_inode(coda_file);
53 struct coda_file_info *cfi = CODA_FTOC(coda_file);
54 struct file *host_file;
55 ssize_t ret;
57 BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
59 host_file = cfi->cfi_container;
60 file_start_write(host_file);
61 inode_lock(coda_inode);
62 ret = vfs_iter_write(cfi->cfi_container, to, &iocb->ki_pos, 0);
63 coda_inode->i_size = file_inode(host_file)->i_size;
64 coda_inode->i_blocks = (coda_inode->i_size + 511) >> 9;
65 coda_inode->i_mtime = coda_inode->i_ctime = current_time(coda_inode);
66 inode_unlock(coda_inode);
67 file_end_write(host_file);
68 return ret;
71 static void
72 coda_vm_open(struct vm_area_struct *vma)
74 struct coda_vm_ops *cvm_ops =
75 container_of(vma->vm_ops, struct coda_vm_ops, vm_ops);
77 atomic_inc(&cvm_ops->refcnt);
79 if (cvm_ops->host_vm_ops && cvm_ops->host_vm_ops->open)
80 cvm_ops->host_vm_ops->open(vma);
83 static void
84 coda_vm_close(struct vm_area_struct *vma)
86 struct coda_vm_ops *cvm_ops =
87 container_of(vma->vm_ops, struct coda_vm_ops, vm_ops);
89 if (cvm_ops->host_vm_ops && cvm_ops->host_vm_ops->close)
90 cvm_ops->host_vm_ops->close(vma);
92 if (atomic_dec_and_test(&cvm_ops->refcnt)) {
93 vma->vm_ops = cvm_ops->host_vm_ops;
94 fput(cvm_ops->coda_file);
95 kfree(cvm_ops);
99 static int
100 coda_file_mmap(struct file *coda_file, struct vm_area_struct *vma)
102 struct coda_file_info *cfi;
103 struct coda_inode_info *cii;
104 struct file *host_file;
105 struct inode *coda_inode, *host_inode;
106 struct coda_vm_ops *cvm_ops;
107 int ret;
109 cfi = CODA_FTOC(coda_file);
110 BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
111 host_file = cfi->cfi_container;
113 if (!host_file->f_op->mmap)
114 return -ENODEV;
116 if (WARN_ON(coda_file != vma->vm_file))
117 return -EIO;
119 cvm_ops = kmalloc(sizeof(struct coda_vm_ops), GFP_KERNEL);
120 if (!cvm_ops)
121 return -ENOMEM;
123 coda_inode = file_inode(coda_file);
124 host_inode = file_inode(host_file);
126 cii = ITOC(coda_inode);
127 spin_lock(&cii->c_lock);
128 coda_file->f_mapping = host_file->f_mapping;
129 if (coda_inode->i_mapping == &coda_inode->i_data)
130 coda_inode->i_mapping = host_inode->i_mapping;
132 /* only allow additional mmaps as long as userspace isn't changing
133 * the container file on us! */
134 else if (coda_inode->i_mapping != host_inode->i_mapping) {
135 spin_unlock(&cii->c_lock);
136 kfree(cvm_ops);
137 return -EBUSY;
140 /* keep track of how often the coda_inode/host_file has been mmapped */
141 cii->c_mapcount++;
142 cfi->cfi_mapcount++;
143 spin_unlock(&cii->c_lock);
145 vma->vm_file = get_file(host_file);
146 ret = call_mmap(vma->vm_file, vma);
148 if (ret) {
149 /* if call_mmap fails, our caller will put coda_file so we
150 * should drop the reference to the host_file that we got.
152 fput(host_file);
153 kfree(cvm_ops);
154 } else {
155 /* here we add redirects for the open/close vm_operations */
156 cvm_ops->host_vm_ops = vma->vm_ops;
157 if (vma->vm_ops)
158 cvm_ops->vm_ops = *vma->vm_ops;
160 cvm_ops->vm_ops.open = coda_vm_open;
161 cvm_ops->vm_ops.close = coda_vm_close;
162 cvm_ops->coda_file = coda_file;
163 atomic_set(&cvm_ops->refcnt, 1);
165 vma->vm_ops = &cvm_ops->vm_ops;
167 return ret;
170 int coda_open(struct inode *coda_inode, struct file *coda_file)
172 struct file *host_file = NULL;
173 int error;
174 unsigned short flags = coda_file->f_flags & (~O_EXCL);
175 unsigned short coda_flags = coda_flags_to_cflags(flags);
176 struct coda_file_info *cfi;
178 cfi = kmalloc(sizeof(struct coda_file_info), GFP_KERNEL);
179 if (!cfi)
180 return -ENOMEM;
182 error = venus_open(coda_inode->i_sb, coda_i2f(coda_inode), coda_flags,
183 &host_file);
184 if (!host_file)
185 error = -EIO;
187 if (error) {
188 kfree(cfi);
189 return error;
192 host_file->f_flags |= coda_file->f_flags & (O_APPEND | O_SYNC);
194 cfi->cfi_magic = CODA_MAGIC;
195 cfi->cfi_mapcount = 0;
196 cfi->cfi_container = host_file;
198 BUG_ON(coda_file->private_data != NULL);
199 coda_file->private_data = cfi;
200 return 0;
203 int coda_release(struct inode *coda_inode, struct file *coda_file)
205 unsigned short flags = (coda_file->f_flags) & (~O_EXCL);
206 unsigned short coda_flags = coda_flags_to_cflags(flags);
207 struct coda_file_info *cfi;
208 struct coda_inode_info *cii;
209 struct inode *host_inode;
210 int err;
212 cfi = CODA_FTOC(coda_file);
213 BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
215 err = venus_close(coda_inode->i_sb, coda_i2f(coda_inode),
216 coda_flags, coda_file->f_cred->fsuid);
218 host_inode = file_inode(cfi->cfi_container);
219 cii = ITOC(coda_inode);
221 /* did we mmap this file? */
222 spin_lock(&cii->c_lock);
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;
228 spin_unlock(&cii->c_lock);
230 fput(cfi->cfi_container);
231 kfree(coda_file->private_data);
232 coda_file->private_data = NULL;
234 /* VFS fput ignores the return value from file_operations->release, so
235 * there is no use returning an error here */
236 return 0;
239 int coda_fsync(struct file *coda_file, loff_t start, loff_t end, int datasync)
241 struct file *host_file;
242 struct inode *coda_inode = file_inode(coda_file);
243 struct coda_file_info *cfi;
244 int err;
246 if (!(S_ISREG(coda_inode->i_mode) || S_ISDIR(coda_inode->i_mode) ||
247 S_ISLNK(coda_inode->i_mode)))
248 return -EINVAL;
250 err = filemap_write_and_wait_range(coda_inode->i_mapping, start, end);
251 if (err)
252 return err;
253 inode_lock(coda_inode);
255 cfi = CODA_FTOC(coda_file);
256 BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
257 host_file = cfi->cfi_container;
259 err = vfs_fsync(host_file, datasync);
260 if (!err && !datasync)
261 err = venus_fsync(coda_inode->i_sb, coda_i2f(coda_inode));
262 inode_unlock(coda_inode);
264 return err;
267 const struct file_operations coda_file_operations = {
268 .llseek = generic_file_llseek,
269 .read_iter = coda_file_read_iter,
270 .write_iter = coda_file_write_iter,
271 .mmap = coda_file_mmap,
272 .open = coda_open,
273 .release = coda_release,
274 .fsync = coda_fsync,
275 .splice_read = generic_file_splice_read,