1 // SPDX-License-Identifier: GPL-2.0
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>.
11 #include <linux/types.h>
12 #include <linux/kernel.h>
13 #include <linux/time.h>
14 #include <linux/file.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"
32 struct file
*coda_file
;
33 const struct vm_operations_struct
*host_vm_ops
;
34 struct vm_operations_struct vm_ops
;
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);
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
;
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
);
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
);
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
);
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
;
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
)
116 if (WARN_ON(coda_file
!= vma
->vm_file
))
119 cvm_ops
= kmalloc(sizeof(struct coda_vm_ops
), GFP_KERNEL
);
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
);
140 /* keep track of how often the coda_inode/host_file has been mmapped */
143 spin_unlock(&cii
->c_lock
);
145 vma
->vm_file
= get_file(host_file
);
146 ret
= call_mmap(vma
->vm_file
, vma
);
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.
155 /* here we add redirects for the open/close vm_operations */
156 cvm_ops
->host_vm_ops
= 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
;
170 int coda_open(struct inode
*coda_inode
, struct file
*coda_file
)
172 struct file
*host_file
= NULL
;
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
);
182 error
= venus_open(coda_inode
->i_sb
, coda_i2f(coda_inode
), coda_flags
,
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
;
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
;
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 */
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
;
246 if (!(S_ISREG(coda_inode
->i_mode
) || S_ISDIR(coda_inode
->i_mode
) ||
247 S_ISLNK(coda_inode
->i_mode
)))
250 err
= filemap_write_and_wait_range(coda_inode
->i_mapping
, start
, end
);
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
);
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
,
273 .release
= coda_release
,
275 .splice_read
= generic_file_splice_read
,