blob: c8b50ba4366a3a2aa0531ea61037ac9615edaaa0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * File operations for Coda.
3 * Original version: (C) 1996 Peter Braam
4 * Rewritten for Linux 2.1: (C) 1997 Carnegie Mellon University
5 *
6 * Carnegie Mellon encourages users of this code to contribute improvements
7 * to the Coda project. Contact Peter Braam <coda@cs.cmu.edu>.
8 */
9
10#include <linux/types.h>
11#include <linux/kernel.h>
12#include <linux/time.h>
13#include <linux/file.h>
14#include <linux/fs.h>
15#include <linux/stat.h>
Randy Dunlap7596b272008-11-19 11:30:27 -080016#include <linux/cred.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/errno.h>
Yoshihisa Abeb5ce1d82010-10-25 02:03:44 -040018#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/string.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/uaccess.h>
22
23#include <linux/coda.h>
24#include <linux/coda_linux.h>
25#include <linux/coda_fs_i.h>
26#include <linux/coda_psdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Adrian Bunkc98d8cf2006-03-24 03:15:53 -080028#include "coda_int.h"
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030static ssize_t
31coda_file_read(struct file *coda_file, char __user *buf, size_t count, loff_t *ppos)
32{
33 struct coda_file_info *cfi;
34 struct file *host_file;
35
36 cfi = CODA_FTOC(coda_file);
37 BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
38 host_file = cfi->cfi_container;
39
40 if (!host_file->f_op || !host_file->f_op->read)
41 return -EINVAL;
42
43 return host_file->f_op->read(host_file, buf, count, ppos);
44}
45
46static ssize_t
Jens Axboe5ffc4ef2007-06-01 11:49:19 +020047coda_file_splice_read(struct file *coda_file, loff_t *ppos,
48 struct pipe_inode_info *pipe, size_t count,
49 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Miklos Szeredi68181732009-05-07 15:37:36 +020051 ssize_t (*splice_read)(struct file *, loff_t *,
52 struct pipe_inode_info *, size_t, unsigned int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 struct coda_file_info *cfi;
54 struct file *host_file;
55
56 cfi = CODA_FTOC(coda_file);
57 BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
58 host_file = cfi->cfi_container;
59
Miklos Szeredi68181732009-05-07 15:37:36 +020060 splice_read = host_file->f_op->splice_read;
61 if (!splice_read)
62 splice_read = default_file_splice_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Miklos Szeredi68181732009-05-07 15:37:36 +020064 return splice_read(host_file, ppos, pipe, count, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
67static ssize_t
68coda_file_write(struct file *coda_file, const char __user *buf, size_t count, loff_t *ppos)
69{
Josef Sipekd4176d32006-12-08 02:36:56 -080070 struct inode *host_inode, *coda_inode = coda_file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 struct coda_file_info *cfi;
72 struct file *host_file;
73 ssize_t ret;
74
75 cfi = CODA_FTOC(coda_file);
76 BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
77 host_file = cfi->cfi_container;
78
79 if (!host_file->f_op || !host_file->f_op->write)
80 return -EINVAL;
81
Josef Sipekd4176d32006-12-08 02:36:56 -080082 host_inode = host_file->f_path.dentry->d_inode;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080083 mutex_lock(&coda_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 ret = host_file->f_op->write(host_file, buf, count, ppos);
86
87 coda_inode->i_size = host_inode->i_size;
88 coda_inode->i_blocks = (coda_inode->i_size + 511) >> 9;
89 coda_inode->i_mtime = coda_inode->i_ctime = CURRENT_TIME_SEC;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080090 mutex_unlock(&coda_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 return ret;
93}
94
95static int
96coda_file_mmap(struct file *coda_file, struct vm_area_struct *vma)
97{
98 struct coda_file_info *cfi;
99 struct coda_inode_info *cii;
100 struct file *host_file;
101 struct inode *coda_inode, *host_inode;
102
103 cfi = CODA_FTOC(coda_file);
104 BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
105 host_file = cfi->cfi_container;
106
107 if (!host_file->f_op || !host_file->f_op->mmap)
108 return -ENODEV;
109
Josef Sipekd4176d32006-12-08 02:36:56 -0800110 coda_inode = coda_file->f_path.dentry->d_inode;
111 host_inode = host_file->f_path.dentry->d_inode;
Yoshihisa Abeb5ce1d82010-10-25 02:03:44 -0400112
113 cii = ITOC(coda_inode);
114 spin_lock(&cii->c_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 coda_file->f_mapping = host_file->f_mapping;
116 if (coda_inode->i_mapping == &coda_inode->i_data)
117 coda_inode->i_mapping = host_inode->i_mapping;
118
119 /* only allow additional mmaps as long as userspace isn't changing
120 * the container file on us! */
Yoshihisa Abeb5ce1d82010-10-25 02:03:44 -0400121 else if (coda_inode->i_mapping != host_inode->i_mapping) {
122 spin_unlock(&cii->c_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return -EBUSY;
Yoshihisa Abeb5ce1d82010-10-25 02:03:44 -0400124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 /* keep track of how often the coda_inode/host_file has been mmapped */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 cii->c_mapcount++;
128 cfi->cfi_mapcount++;
Yoshihisa Abeb5ce1d82010-10-25 02:03:44 -0400129 spin_unlock(&cii->c_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 return host_file->f_op->mmap(host_file, vma);
132}
133
134int coda_open(struct inode *coda_inode, struct file *coda_file)
135{
136 struct file *host_file = NULL;
137 int error;
138 unsigned short flags = coda_file->f_flags & (~O_EXCL);
139 unsigned short coda_flags = coda_flags_to_cflags(flags);
140 struct coda_file_info *cfi;
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 cfi = kmalloc(sizeof(struct coda_file_info), GFP_KERNEL);
Josh Triplett6ecbc4e2006-07-30 03:03:56 -0700143 if (!cfi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 error = venus_open(coda_inode->i_sb, coda_i2f(coda_inode), coda_flags,
Jan Harkes38c2e432007-07-19 01:48:41 -0700147 &host_file);
148 if (!host_file)
149 error = -EIO;
150
151 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 kfree(cfi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return error;
154 }
155
156 host_file->f_flags |= coda_file->f_flags & (O_APPEND | O_SYNC);
157
158 cfi->cfi_magic = CODA_MAGIC;
159 cfi->cfi_mapcount = 0;
160 cfi->cfi_container = host_file;
161
162 BUG_ON(coda_file->private_data != NULL);
163 coda_file->private_data = cfi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return 0;
165}
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167int coda_release(struct inode *coda_inode, struct file *coda_file)
168{
169 unsigned short flags = (coda_file->f_flags) & (~O_EXCL);
170 unsigned short coda_flags = coda_flags_to_cflags(flags);
171 struct coda_file_info *cfi;
172 struct coda_inode_info *cii;
173 struct inode *host_inode;
Yoshihisa Abef7cc02b82010-10-25 02:03:45 -0400174 int err;
Jan Harkes3cf01f22007-07-19 01:48:51 -0700175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 cfi = CODA_FTOC(coda_file);
177 BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
178
Jan Harkesd3fec422007-07-21 04:37:26 -0700179 err = venus_close(coda_inode->i_sb, coda_i2f(coda_inode),
David Howellsd76b0d92008-11-14 10:39:25 +1100180 coda_flags, coda_file->f_cred->fsuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Josef Sipekd4176d32006-12-08 02:36:56 -0800182 host_inode = cfi->cfi_container->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 cii = ITOC(coda_inode);
184
185 /* did we mmap this file? */
Yoshihisa Abeb5ce1d82010-10-25 02:03:44 -0400186 spin_lock(&cii->c_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (coda_inode->i_mapping == &host_inode->i_data) {
188 cii->c_mapcount -= cfi->cfi_mapcount;
189 if (!cii->c_mapcount)
190 coda_inode->i_mapping = &coda_inode->i_data;
191 }
Yoshihisa Abeb5ce1d82010-10-25 02:03:44 -0400192 spin_unlock(&cii->c_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 fput(cfi->cfi_container);
195 kfree(coda_file->private_data);
196 coda_file->private_data = NULL;
197
Jan Harkesd3fec422007-07-21 04:37:26 -0700198 /* VFS fput ignores the return value from file_operations->release, so
199 * there is no use returning an error here */
200 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200203int coda_fsync(struct file *coda_file, int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 struct file *host_file;
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200206 struct inode *coda_inode = coda_file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 struct coda_file_info *cfi;
Yoshihisa Abef7cc02b82010-10-25 02:03:45 -0400208 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 if (!(S_ISREG(coda_inode->i_mode) || S_ISDIR(coda_inode->i_mode) ||
211 S_ISLNK(coda_inode->i_mode)))
212 return -EINVAL;
213
214 cfi = CODA_FTOC(coda_file);
215 BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
216 host_file = cfi->cfi_container;
217
Christoph Hellwig8018ab02010-03-22 17:32:25 +0100218 err = vfs_fsync(host_file, datasync);
Yoshihisa Abef7cc02b82010-10-25 02:03:45 -0400219 if (!err && !datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 err = venus_fsync(coda_inode->i_sb, coda_i2f(coda_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 return err;
223}
224
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800225const struct file_operations coda_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 .llseek = generic_file_llseek,
227 .read = coda_file_read,
228 .write = coda_file_write,
229 .mmap = coda_file_mmap,
230 .open = coda_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 .release = coda_release,
232 .fsync = coda_fsync,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +0200233 .splice_read = coda_file_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234};
235