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