blob: 6e0154eb6fcc1c0c7163c18990a4e2e0a0e74368 [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>
Fabian Frederick834b46c2014-08-08 14:20:33 -070021#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include <linux/coda.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/coda_psdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Al Viro31a203d2011-01-12 16:36:09 -050026#include "coda_linux.h"
Adrian Bunkc98d8cf2006-03-24 03:15:53 -080027#include "coda_int.h"
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029static ssize_t
Al Viroc12c49e2015-04-03 10:58:11 -040030coda_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
Al Viroc12c49e2015-04-03 10:58:11 -040032 struct file *coda_file = iocb->ki_filp;
33 struct coda_file_info *cfi = CODA_FTOC(coda_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Al Viroc12c49e2015-04-03 10:58:11 -040037 return vfs_iter_read(cfi->cfi_container, to, &iocb->ki_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038}
39
40static ssize_t
Al Viroc12c49e2015-04-03 10:58:11 -040041coda_file_write_iter(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Al Viroc12c49e2015-04-03 10:58:11 -040043 struct file *coda_file = iocb->ki_filp;
44 struct inode *coda_inode = file_inode(coda_file);
45 struct coda_file_info *cfi = CODA_FTOC(coda_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 struct file *host_file;
47 ssize_t ret;
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
Al Viroc12c49e2015-04-03 10:58:11 -040050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 host_file = cfi->cfi_container;
Al Viro03d95eb2013-03-20 13:04:20 -040052 file_start_write(host_file);
Al Viro59551022016-01-22 15:40:57 -050053 inode_lock(coda_inode);
Al Viroc12c49e2015-04-03 10:58:11 -040054 ret = vfs_iter_write(cfi->cfi_container, to, &iocb->ki_pos);
55 coda_inode->i_size = file_inode(host_file)->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 coda_inode->i_blocks = (coda_inode->i_size + 511) >> 9;
Deepa Dinamani02027d42016-09-14 07:48:05 -070057 coda_inode->i_mtime = coda_inode->i_ctime = current_time(coda_inode);
Al Viro59551022016-01-22 15:40:57 -050058 inode_unlock(coda_inode);
Al Viro03d95eb2013-03-20 13:04:20 -040059 file_end_write(host_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 return ret;
61}
62
63static int
64coda_file_mmap(struct file *coda_file, struct vm_area_struct *vma)
65{
66 struct coda_file_info *cfi;
67 struct coda_inode_info *cii;
68 struct file *host_file;
69 struct inode *coda_inode, *host_inode;
70
71 cfi = CODA_FTOC(coda_file);
72 BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
73 host_file = cfi->cfi_container;
74
Al Viro72c2d532013-09-22 16:27:52 -040075 if (!host_file->f_op->mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 return -ENODEV;
77
Al Viro496ad9a2013-01-23 17:07:38 -050078 coda_inode = file_inode(coda_file);
79 host_inode = file_inode(host_file);
Yoshihisa Abeb5ce1d82010-10-25 02:03:44 -040080
81 cii = ITOC(coda_inode);
82 spin_lock(&cii->c_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 coda_file->f_mapping = host_file->f_mapping;
84 if (coda_inode->i_mapping == &coda_inode->i_data)
85 coda_inode->i_mapping = host_inode->i_mapping;
86
87 /* only allow additional mmaps as long as userspace isn't changing
88 * the container file on us! */
Yoshihisa Abeb5ce1d82010-10-25 02:03:44 -040089 else if (coda_inode->i_mapping != host_inode->i_mapping) {
90 spin_unlock(&cii->c_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return -EBUSY;
Yoshihisa Abeb5ce1d82010-10-25 02:03:44 -040092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 /* keep track of how often the coda_inode/host_file has been mmapped */
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 cii->c_mapcount++;
96 cfi->cfi_mapcount++;
Yoshihisa Abeb5ce1d82010-10-25 02:03:44 -040097 spin_unlock(&cii->c_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 return host_file->f_op->mmap(host_file, vma);
100}
101
102int coda_open(struct inode *coda_inode, struct file *coda_file)
103{
104 struct file *host_file = NULL;
105 int error;
106 unsigned short flags = coda_file->f_flags & (~O_EXCL);
107 unsigned short coda_flags = coda_flags_to_cflags(flags);
108 struct coda_file_info *cfi;
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 cfi = kmalloc(sizeof(struct coda_file_info), GFP_KERNEL);
Josh Triplett6ecbc4e2006-07-30 03:03:56 -0700111 if (!cfi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 error = venus_open(coda_inode->i_sb, coda_i2f(coda_inode), coda_flags,
Jan Harkes38c2e432007-07-19 01:48:41 -0700115 &host_file);
116 if (!host_file)
117 error = -EIO;
118
119 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 kfree(cfi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 return error;
122 }
123
124 host_file->f_flags |= coda_file->f_flags & (O_APPEND | O_SYNC);
125
126 cfi->cfi_magic = CODA_MAGIC;
127 cfi->cfi_mapcount = 0;
128 cfi->cfi_container = host_file;
129
130 BUG_ON(coda_file->private_data != NULL);
131 coda_file->private_data = cfi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return 0;
133}
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135int coda_release(struct inode *coda_inode, struct file *coda_file)
136{
137 unsigned short flags = (coda_file->f_flags) & (~O_EXCL);
138 unsigned short coda_flags = coda_flags_to_cflags(flags);
139 struct coda_file_info *cfi;
140 struct coda_inode_info *cii;
141 struct inode *host_inode;
Yoshihisa Abef7cc02b82010-10-25 02:03:45 -0400142 int err;
Jan Harkes3cf01f22007-07-19 01:48:51 -0700143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 cfi = CODA_FTOC(coda_file);
145 BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
146
Jan Harkesd3fec422007-07-21 04:37:26 -0700147 err = venus_close(coda_inode->i_sb, coda_i2f(coda_inode),
David Howellsd76b0d92008-11-14 10:39:25 +1100148 coda_flags, coda_file->f_cred->fsuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Al Viro496ad9a2013-01-23 17:07:38 -0500150 host_inode = file_inode(cfi->cfi_container);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 cii = ITOC(coda_inode);
152
153 /* did we mmap this file? */
Yoshihisa Abeb5ce1d82010-10-25 02:03:44 -0400154 spin_lock(&cii->c_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if (coda_inode->i_mapping == &host_inode->i_data) {
156 cii->c_mapcount -= cfi->cfi_mapcount;
157 if (!cii->c_mapcount)
158 coda_inode->i_mapping = &coda_inode->i_data;
159 }
Yoshihisa Abeb5ce1d82010-10-25 02:03:44 -0400160 spin_unlock(&cii->c_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 fput(cfi->cfi_container);
163 kfree(coda_file->private_data);
164 coda_file->private_data = NULL;
165
Jan Harkesd3fec422007-07-21 04:37:26 -0700166 /* VFS fput ignores the return value from file_operations->release, so
167 * there is no use returning an error here */
168 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
Josef Bacik02c24a82011-07-16 20:44:56 -0400171int coda_fsync(struct file *coda_file, loff_t start, loff_t end, int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 struct file *host_file;
Al Viro496ad9a2013-01-23 17:07:38 -0500174 struct inode *coda_inode = file_inode(coda_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 struct coda_file_info *cfi;
Yoshihisa Abef7cc02b82010-10-25 02:03:45 -0400176 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 if (!(S_ISREG(coda_inode->i_mode) || S_ISDIR(coda_inode->i_mode) ||
179 S_ISLNK(coda_inode->i_mode)))
180 return -EINVAL;
181
Josef Bacik02c24a82011-07-16 20:44:56 -0400182 err = filemap_write_and_wait_range(coda_inode->i_mapping, start, end);
183 if (err)
184 return err;
Al Viro59551022016-01-22 15:40:57 -0500185 inode_lock(coda_inode);
Josef Bacik02c24a82011-07-16 20:44:56 -0400186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 cfi = CODA_FTOC(coda_file);
188 BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
189 host_file = cfi->cfi_container;
190
Christoph Hellwig8018ab02010-03-22 17:32:25 +0100191 err = vfs_fsync(host_file, datasync);
Yoshihisa Abef7cc02b82010-10-25 02:03:45 -0400192 if (!err && !datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 err = venus_fsync(coda_inode->i_sb, coda_i2f(coda_inode));
Al Viro59551022016-01-22 15:40:57 -0500194 inode_unlock(coda_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 return err;
197}
198
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800199const struct file_operations coda_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 .llseek = generic_file_llseek,
Al Viroc12c49e2015-04-03 10:58:11 -0400201 .read_iter = coda_file_read_iter,
202 .write_iter = coda_file_write_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 .mmap = coda_file_mmap,
204 .open = coda_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 .release = coda_release,
206 .fsync = coda_fsync,
Al Viro82c156f2016-09-22 23:35:42 -0400207 .splice_read = generic_file_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208};
209