2 * File operations for Coda.
3 * Original version: (C) 1996 Peter Braam
4 * Rewritten for Linux 2.1: (C) 1997 Carnegie Mellon University
6 * Carnegie Mellon encourages users of this code to contribute improvements
7 * to the Coda project. Contact Peter Braam <coda@cs.cmu.edu>.
10 #include <linux/types.h>
11 #include <linux/kernel.h>
12 #include <linux/time.h>
13 #include <linux/file.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>
23 #include <linux/coda.h>
24 #include <linux/coda_psdev.h>
26 #include "coda_linux.h"
30 coda_file_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
32 struct file
*coda_file
= iocb
->ki_filp
;
33 struct coda_file_info
*cfi
= CODA_FTOC(coda_file
);
35 BUG_ON(!cfi
|| cfi
->cfi_magic
!= CODA_MAGIC
);
37 return vfs_iter_read(cfi
->cfi_container
, to
, &iocb
->ki_pos
);
41 coda_file_splice_read(struct file
*coda_file
, loff_t
*ppos
,
42 struct pipe_inode_info
*pipe
, size_t count
,
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
;
50 cfi
= CODA_FTOC(coda_file
);
51 BUG_ON(!cfi
|| cfi
->cfi_magic
!= CODA_MAGIC
);
52 host_file
= cfi
->cfi_container
;
54 splice_read
= host_file
->f_op
->splice_read
;
56 splice_read
= default_file_splice_read
;
58 return splice_read(host_file
, ppos
, pipe
, count
, flags
);
62 coda_file_write_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
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
;
70 BUG_ON(!cfi
|| cfi
->cfi_magic
!= CODA_MAGIC
);
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
);
86 struct file
*coda_file
;
87 const struct vm_operations_struct
*host_vm_ops
;
88 struct vm_operations_struct vm_ops
;
92 coda_vm_open(struct vm_area_struct
*vma
)
94 struct coda_vm_ops
*cvm_ops
=
95 container_of(vma
->vm_ops
, struct coda_vm_ops
, vm_ops
);
97 atomic_inc(&cvm_ops
->refcnt
);
99 if (cvm_ops
->host_vm_ops
&& cvm_ops
->host_vm_ops
->open
)
100 cvm_ops
->host_vm_ops
->open(vma
);
104 coda_vm_close(struct vm_area_struct
*vma
)
106 struct coda_vm_ops
*cvm_ops
=
107 container_of(vma
->vm_ops
, struct coda_vm_ops
, vm_ops
);
109 if (cvm_ops
->host_vm_ops
&& cvm_ops
->host_vm_ops
->close
)
110 cvm_ops
->host_vm_ops
->close(vma
);
112 if (atomic_dec_and_test(&cvm_ops
->refcnt
)) {
113 vma
->vm_ops
= cvm_ops
->host_vm_ops
;
114 fput(cvm_ops
->coda_file
);
120 coda_file_mmap(struct file
*coda_file
, struct vm_area_struct
*vma
)
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
;
129 cfi
= CODA_FTOC(coda_file
);
130 BUG_ON(!cfi
|| cfi
->cfi_magic
!= CODA_MAGIC
);
131 host_file
= cfi
->cfi_container
;
133 if (!host_file
->f_op
->mmap
)
136 if (WARN_ON(coda_file
!= vma
->vm_file
))
139 cvm_ops
= kmalloc(sizeof(struct coda_vm_ops
), GFP_KERNEL
);
143 coda_inode
= file_inode(coda_file
);
144 host_inode
= file_inode(host_file
);
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
;
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
);
160 /* keep track of how often the coda_inode/host_file has been mmapped */
163 spin_unlock(&cii
->c_lock
);
165 vma
->vm_file
= get_file(host_file
);
166 ret
= host_file
->f_op
->mmap(host_file
, vma
);
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.
175 /* here we add redirects for the open/close vm_operations */
176 cvm_ops
->host_vm_ops
= vma
->vm_ops
;
178 cvm_ops
->vm_ops
= *vma
->vm_ops
;
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);
185 vma
->vm_ops
= &cvm_ops
->vm_ops
;
190 int coda_open(struct inode
*coda_inode
, struct file
*coda_file
)
192 struct file
*host_file
= NULL
;
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
;
198 cfi
= kmalloc(sizeof(struct coda_file_info
), GFP_KERNEL
);
202 error
= venus_open(coda_inode
->i_sb
, coda_i2f(coda_inode
), coda_flags
,
212 host_file
->f_flags
|= coda_file
->f_flags
& (O_APPEND
| O_SYNC
);
214 cfi
->cfi_magic
= CODA_MAGIC
;
215 cfi
->cfi_mapcount
= 0;
216 cfi
->cfi_container
= host_file
;
218 BUG_ON(coda_file
->private_data
!= NULL
);
219 coda_file
->private_data
= cfi
;
223 int coda_release(struct inode
*coda_inode
, struct file
*coda_file
)
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
;
232 cfi
= CODA_FTOC(coda_file
);
233 BUG_ON(!cfi
|| cfi
->cfi_magic
!= CODA_MAGIC
);
235 err
= venus_close(coda_inode
->i_sb
, coda_i2f(coda_inode
),
236 coda_flags
, coda_file
->f_cred
->fsuid
);
238 host_inode
= file_inode(cfi
->cfi_container
);
239 cii
= ITOC(coda_inode
);
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
;
248 spin_unlock(&cii
->c_lock
);
250 fput(cfi
->cfi_container
);
251 kfree(coda_file
->private_data
);
252 coda_file
->private_data
= NULL
;
254 /* VFS fput ignores the return value from file_operations->release, so
255 * there is no use returning an error here */
259 int coda_fsync(struct file
*coda_file
, loff_t start
, loff_t end
, int datasync
)
261 struct file
*host_file
;
262 struct inode
*coda_inode
= file_inode(coda_file
);
263 struct coda_file_info
*cfi
;
266 if (!(S_ISREG(coda_inode
->i_mode
) || S_ISDIR(coda_inode
->i_mode
) ||
267 S_ISLNK(coda_inode
->i_mode
)))
270 err
= filemap_write_and_wait_range(coda_inode
->i_mapping
, start
, end
);
273 mutex_lock(&coda_inode
->i_mutex
);
275 cfi
= CODA_FTOC(coda_file
);
276 BUG_ON(!cfi
|| cfi
->cfi_magic
!= CODA_MAGIC
);
277 host_file
= cfi
->cfi_container
;
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
);
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
,
293 .release
= coda_release
,
295 .splice_read
= coda_file_splice_read
,