Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
2 | /* |
3 | * File operations for Coda. | |
4 | * Original version: (C) 1996 Peter Braam | |
5 | * Rewritten for Linux 2.1: (C) 1997 Carnegie Mellon University | |
6 | * | |
7 | * Carnegie Mellon encourages users of this code to contribute improvements | |
8 | * to the Coda project. Contact Peter Braam <coda@cs.cmu.edu>. | |
9 | */ | |
10 | ||
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> | |
7596b27d | 17 | #include <linux/cred.h> |
1da177e4 | 18 | #include <linux/errno.h> |
b5ce1d83 | 19 | #include <linux/spinlock.h> |
1da177e4 | 20 | #include <linux/string.h> |
5a0e3ad6 | 21 | #include <linux/slab.h> |
834b46c3 | 22 | #include <linux/uaccess.h> |
1da177e4 LT |
23 | |
24 | #include <linux/coda.h> | |
1da177e4 | 25 | #include <linux/coda_psdev.h> |
1da177e4 | 26 | |
31a203df | 27 | #include "coda_linux.h" |
c98d8cfb AB |
28 | #include "coda_int.h" |
29 | ||
7fa0a1da JH |
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; | |
35 | }; | |
36 | ||
1da177e4 | 37 | static ssize_t |
c12c49e7 | 38 | coda_file_read_iter(struct kiocb *iocb, struct iov_iter *to) |
1da177e4 | 39 | { |
c12c49e7 AV |
40 | struct file *coda_file = iocb->ki_filp; |
41 | struct coda_file_info *cfi = CODA_FTOC(coda_file); | |
1da177e4 | 42 | |
1da177e4 | 43 | BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC); |
1da177e4 | 44 | |
18e9710e | 45 | return vfs_iter_read(cfi->cfi_container, to, &iocb->ki_pos, 0); |
1da177e4 LT |
46 | } |
47 | ||
1da177e4 | 48 | static ssize_t |
c12c49e7 | 49 | coda_file_write_iter(struct kiocb *iocb, struct iov_iter *to) |
1da177e4 | 50 | { |
c12c49e7 AV |
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); | |
1da177e4 LT |
54 | struct file *host_file; |
55 | ssize_t ret; | |
56 | ||
1da177e4 | 57 | BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC); |
1da177e4 | 58 | |
c12c49e7 | 59 | host_file = cfi->cfi_container; |
03d95eb2 | 60 | file_start_write(host_file); |
5955102c | 61 | inode_lock(coda_inode); |
abbb6589 | 62 | ret = vfs_iter_write(cfi->cfi_container, to, &iocb->ki_pos, 0); |
c12c49e7 | 63 | coda_inode->i_size = file_inode(host_file)->i_size; |
1da177e4 | 64 | coda_inode->i_blocks = (coda_inode->i_size + 511) >> 9; |
02027d42 | 65 | coda_inode->i_mtime = coda_inode->i_ctime = current_time(coda_inode); |
5955102c | 66 | inode_unlock(coda_inode); |
03d95eb2 | 67 | file_end_write(host_file); |
1da177e4 LT |
68 | return ret; |
69 | } | |
70 | ||
7fa0a1da JH |
71 | static void |
72 | coda_vm_open(struct vm_area_struct *vma) | |
73 | { | |
74 | struct coda_vm_ops *cvm_ops = | |
75 | container_of(vma->vm_ops, struct coda_vm_ops, vm_ops); | |
76 | ||
77 | atomic_inc(&cvm_ops->refcnt); | |
78 | ||
79 | if (cvm_ops->host_vm_ops && cvm_ops->host_vm_ops->open) | |
80 | cvm_ops->host_vm_ops->open(vma); | |
81 | } | |
82 | ||
83 | static void | |
84 | coda_vm_close(struct vm_area_struct *vma) | |
85 | { | |
86 | struct coda_vm_ops *cvm_ops = | |
87 | container_of(vma->vm_ops, struct coda_vm_ops, vm_ops); | |
88 | ||
89 | if (cvm_ops->host_vm_ops && cvm_ops->host_vm_ops->close) | |
90 | cvm_ops->host_vm_ops->close(vma); | |
91 | ||
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); | |
96 | } | |
97 | } | |
98 | ||
1da177e4 LT |
99 | static int |
100 | coda_file_mmap(struct file *coda_file, struct vm_area_struct *vma) | |
101 | { | |
102 | struct coda_file_info *cfi; | |
103 | struct coda_inode_info *cii; | |
104 | struct file *host_file; | |
105 | struct inode *coda_inode, *host_inode; | |
7fa0a1da JH |
106 | struct coda_vm_ops *cvm_ops; |
107 | int ret; | |
1da177e4 LT |
108 | |
109 | cfi = CODA_FTOC(coda_file); | |
110 | BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC); | |
111 | host_file = cfi->cfi_container; | |
112 | ||
72c2d531 | 113 | if (!host_file->f_op->mmap) |
1da177e4 LT |
114 | return -ENODEV; |
115 | ||
7fa0a1da JH |
116 | if (WARN_ON(coda_file != vma->vm_file)) |
117 | return -EIO; | |
118 | ||
119 | cvm_ops = kmalloc(sizeof(struct coda_vm_ops), GFP_KERNEL); | |
120 | if (!cvm_ops) | |
121 | return -ENOMEM; | |
122 | ||
496ad9aa AV |
123 | coda_inode = file_inode(coda_file); |
124 | host_inode = file_inode(host_file); | |
b5ce1d83 YA |
125 | |
126 | cii = ITOC(coda_inode); | |
127 | spin_lock(&cii->c_lock); | |
1da177e4 LT |
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; | |
131 | ||
132 | /* only allow additional mmaps as long as userspace isn't changing | |
133 | * the container file on us! */ | |
b5ce1d83 YA |
134 | else if (coda_inode->i_mapping != host_inode->i_mapping) { |
135 | spin_unlock(&cii->c_lock); | |
7fa0a1da | 136 | kfree(cvm_ops); |
1da177e4 | 137 | return -EBUSY; |
b5ce1d83 | 138 | } |
1da177e4 LT |
139 | |
140 | /* keep track of how often the coda_inode/host_file has been mmapped */ | |
1da177e4 LT |
141 | cii->c_mapcount++; |
142 | cfi->cfi_mapcount++; | |
b5ce1d83 | 143 | spin_unlock(&cii->c_lock); |
1da177e4 | 144 | |
7fa0a1da JH |
145 | vma->vm_file = get_file(host_file); |
146 | ret = call_mmap(vma->vm_file, vma); | |
147 | ||
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. | |
151 | */ | |
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; | |
159 | ||
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); | |
164 | ||
165 | vma->vm_ops = &cvm_ops->vm_ops; | |
166 | } | |
167 | return ret; | |
1da177e4 LT |
168 | } |
169 | ||
170 | int coda_open(struct inode *coda_inode, struct file *coda_file) | |
171 | { | |
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; | |
177 | ||
1da177e4 | 178 | cfi = kmalloc(sizeof(struct coda_file_info), GFP_KERNEL); |
6ecbc4e1 | 179 | if (!cfi) |
1da177e4 | 180 | return -ENOMEM; |
1da177e4 | 181 | |
1da177e4 | 182 | error = venus_open(coda_inode->i_sb, coda_i2f(coda_inode), coda_flags, |
38c2e437 JH |
183 | &host_file); |
184 | if (!host_file) | |
185 | error = -EIO; | |
186 | ||
187 | if (error) { | |
1da177e4 | 188 | kfree(cfi); |
1da177e4 LT |
189 | return error; |
190 | } | |
191 | ||
192 | host_file->f_flags |= coda_file->f_flags & (O_APPEND | O_SYNC); | |
193 | ||
194 | cfi->cfi_magic = CODA_MAGIC; | |
195 | cfi->cfi_mapcount = 0; | |
196 | cfi->cfi_container = host_file; | |
197 | ||
198 | BUG_ON(coda_file->private_data != NULL); | |
199 | coda_file->private_data = cfi; | |
1da177e4 LT |
200 | return 0; |
201 | } | |
202 | ||
1da177e4 LT |
203 | int coda_release(struct inode *coda_inode, struct file *coda_file) |
204 | { | |
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; | |
f7cc02b8 | 210 | int err; |
3cf01f28 | 211 | |
1da177e4 LT |
212 | cfi = CODA_FTOC(coda_file); |
213 | BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC); | |
214 | ||
d3fec424 | 215 | err = venus_close(coda_inode->i_sb, coda_i2f(coda_inode), |
d76b0d9b | 216 | coda_flags, coda_file->f_cred->fsuid); |
1da177e4 | 217 | |
496ad9aa | 218 | host_inode = file_inode(cfi->cfi_container); |
1da177e4 LT |
219 | cii = ITOC(coda_inode); |
220 | ||
221 | /* did we mmap this file? */ | |
b5ce1d83 | 222 | spin_lock(&cii->c_lock); |
1da177e4 LT |
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 | } | |
b5ce1d83 | 228 | spin_unlock(&cii->c_lock); |
1da177e4 LT |
229 | |
230 | fput(cfi->cfi_container); | |
231 | kfree(coda_file->private_data); | |
232 | coda_file->private_data = NULL; | |
233 | ||
d3fec424 JH |
234 | /* VFS fput ignores the return value from file_operations->release, so |
235 | * there is no use returning an error here */ | |
236 | return 0; | |
1da177e4 LT |
237 | } |
238 | ||
02c24a82 | 239 | int coda_fsync(struct file *coda_file, loff_t start, loff_t end, int datasync) |
1da177e4 LT |
240 | { |
241 | struct file *host_file; | |
496ad9aa | 242 | struct inode *coda_inode = file_inode(coda_file); |
1da177e4 | 243 | struct coda_file_info *cfi; |
f7cc02b8 | 244 | int err; |
1da177e4 LT |
245 | |
246 | if (!(S_ISREG(coda_inode->i_mode) || S_ISDIR(coda_inode->i_mode) || | |
247 | S_ISLNK(coda_inode->i_mode))) | |
248 | return -EINVAL; | |
249 | ||
02c24a82 JB |
250 | err = filemap_write_and_wait_range(coda_inode->i_mapping, start, end); |
251 | if (err) | |
252 | return err; | |
5955102c | 253 | inode_lock(coda_inode); |
02c24a82 | 254 | |
1da177e4 LT |
255 | cfi = CODA_FTOC(coda_file); |
256 | BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC); | |
257 | host_file = cfi->cfi_container; | |
258 | ||
8018ab05 | 259 | err = vfs_fsync(host_file, datasync); |
f7cc02b8 | 260 | if (!err && !datasync) |
1da177e4 | 261 | err = venus_fsync(coda_inode->i_sb, coda_i2f(coda_inode)); |
5955102c | 262 | inode_unlock(coda_inode); |
1da177e4 LT |
263 | |
264 | return err; | |
265 | } | |
266 | ||
4b6f5d20 | 267 | const struct file_operations coda_file_operations = { |
1da177e4 | 268 | .llseek = generic_file_llseek, |
c12c49e7 AV |
269 | .read_iter = coda_file_read_iter, |
270 | .write_iter = coda_file_write_iter, | |
1da177e4 LT |
271 | .mmap = coda_file_mmap, |
272 | .open = coda_open, | |
1da177e4 LT |
273 | .release = coda_release, |
274 | .fsync = coda_fsync, | |
82c156f8 | 275 | .splice_read = generic_file_splice_read, |
1da177e4 | 276 | }; |