blob: 1144d59d6469ffd0db13a07198c34e8b1b97a067 [file] [log] [blame]
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -07001/*
2 * linux/fs/9p/vfs_file.c
3 *
4 * This file contians vfs file ops for 9P2000.
5 *
6 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
7 * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to:
21 * Free Software Foundation
22 * 51 Franklin Street, Fifth Floor
23 * Boston, MA 02111-1301 USA
24 *
25 */
26
27#include <linux/module.h>
28#include <linux/errno.h>
29#include <linux/fs.h>
30#include <linux/file.h>
31#include <linux/stat.h>
32#include <linux/string.h>
33#include <linux/smp_lock.h>
34#include <linux/inet.h>
Latchesar Ionkov531b1092006-01-08 01:05:00 -080035#include <linux/version.h>
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070036#include <linux/list.h>
37#include <asm/uaccess.h>
38#include <linux/idr.h>
39
40#include "debug.h"
41#include "v9fs.h"
42#include "9p.h"
43#include "v9fs_vfs.h"
44#include "fid.h"
45
46/**
47 * v9fs_file_open - open a file (or directory)
48 * @inode: inode to be opened
49 * @file: file being opened
50 *
51 */
52
53int v9fs_file_open(struct inode *inode, struct file *file)
54{
55 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -080056 struct v9fs_fid *vfid;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070057 struct v9fs_fcall *fcall = NULL;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -080058 int omode;
59 int fid = V9FS_NOFID;
60 int err;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070061
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -070062 dprintk(DEBUG_VFS, "inode: %p file: %p \n", inode, file);
63
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -080064 vfid = v9fs_fid_lookup(file->f_dentry);
65 if (!vfid) {
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070066 dprintk(DEBUG_ERROR, "Couldn't resolve fid from dentry\n");
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -070067 return -EBADF;
68 }
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070069
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -080070 fid = v9fs_get_idpool(&v9ses->fidpool);
71 if (fid < 0) {
Latchesar Ionkov16cce6d2006-03-25 03:07:26 -080072 eprintk(KERN_WARNING, "newfid fails!\n");
73 return -ENOSPC;
74 }
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070075
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -080076 err = v9fs_t_walk(v9ses, vfid->fid, fid, NULL, NULL);
77 if (err < 0) {
Latchesar Ionkov16cce6d2006-03-25 03:07:26 -080078 dprintk(DEBUG_ERROR, "rewalk didn't work\n");
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -080079 goto put_fid;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070080 }
81
Latchesar Ionkov16cce6d2006-03-25 03:07:26 -080082 /* TODO: do special things for O_EXCL, O_NOFOLLOW, O_SYNC */
83 /* translate open mode appropriately */
84 omode = v9fs_uflags2omode(file->f_flags);
85 err = v9fs_t_open(v9ses, fid, omode, &fcall);
86 if (err < 0) {
87 PRINT_FCALL_ERROR("open failed", fcall);
88 goto clunk_fid;
89 }
90
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -080091 vfid = kmalloc(sizeof(struct v9fs_fid), GFP_KERNEL);
92 if (vfid == NULL) {
93 dprintk(DEBUG_ERROR, "out of memory\n");
Latchesar Ionkov16cce6d2006-03-25 03:07:26 -080094 err = -ENOMEM;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -080095 goto clunk_fid;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -080096 }
97
98 file->private_data = vfid;
99 vfid->fid = fid;
100 vfid->fidopen = 1;
101 vfid->fidclunked = 0;
102 vfid->iounit = fcall->params.ropen.iounit;
103 vfid->rdir_pos = 0;
104 vfid->rdir_fcall = NULL;
105 vfid->filp = file;
106 kfree(fcall);
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700107
108 return 0;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800109
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800110clunk_fid:
111 v9fs_t_clunk(v9ses, fid);
112
113put_fid:
114 v9fs_put_idpool(fid, &v9ses->fidpool);
Latchesar Ionkov16cce6d2006-03-25 03:07:26 -0800115 kfree(fcall);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800116
117 return err;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700118}
119
120/**
121 * v9fs_file_lock - lock a file (or directory)
122 * @inode: inode to be opened
123 * @file: file being opened
124 *
125 * XXX - this looks like a local only lock, we should extend into 9P
126 * by using open exclusive
127 */
128
129static int v9fs_file_lock(struct file *filp, int cmd, struct file_lock *fl)
130{
131 int res = 0;
132 struct inode *inode = filp->f_dentry->d_inode;
133
134 dprintk(DEBUG_VFS, "filp: %p lock: %p\n", filp, fl);
135
136 /* No mandatory locks */
137 if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
138 return -ENOLCK;
139
140 if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800141 filemap_write_and_wait(inode->i_mapping);
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700142 invalidate_inode_pages(&inode->i_data);
143 }
144
145 return res;
146}
147
148/**
Latchesar Ionkov19cba8a2005-10-11 08:29:03 -0700149 * v9fs_file_read - read from a file
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700150 * @filep: file pointer to read
151 * @data: data buffer to read data into
152 * @count: size of buffer
153 * @offset: offset at which to read data
154 *
155 */
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700156static ssize_t
Latchesar Ionkov19cba8a2005-10-11 08:29:03 -0700157v9fs_file_read(struct file *filp, char __user * data, size_t count,
158 loff_t * offset)
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700159{
160 struct inode *inode = filp->f_dentry->d_inode;
161 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
162 struct v9fs_fid *v9f = filp->private_data;
163 struct v9fs_fcall *fcall = NULL;
164 int fid = v9f->fid;
165 int rsize = 0;
166 int result = 0;
167 int total = 0;
Latchesar Ionkov19cba8a2005-10-11 08:29:03 -0700168 int n;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700169
170 dprintk(DEBUG_VFS, "\n");
171
172 rsize = v9ses->maxdata - V9FS_IOHDRSZ;
173 if (v9f->iounit != 0 && rsize > v9f->iounit)
174 rsize = v9f->iounit;
175
176 do {
177 if (count < rsize)
178 rsize = count;
179
180 result = v9fs_t_read(v9ses, fid, *offset, rsize, &fcall);
181
182 if (result < 0) {
183 printk(KERN_ERR "9P2000: v9fs_t_read returned %d\n",
184 result);
185
186 kfree(fcall);
187 return total;
188 } else
189 *offset += result;
190
Latchesar Ionkov19cba8a2005-10-11 08:29:03 -0700191 n = copy_to_user(data, fcall->params.rread.data, result);
192 if (n) {
193 dprintk(DEBUG_ERROR, "Problem copying to user %d\n", n);
194 kfree(fcall);
195 return -EFAULT;
196 }
197
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700198 count -= result;
Latchesar Ionkov19cba8a2005-10-11 08:29:03 -0700199 data += result;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700200 total += result;
201
202 kfree(fcall);
203
204 if (result < rsize)
205 break;
206 } while (count);
207
208 return total;
209}
210
211/**
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700212 * v9fs_file_write - write to a file
213 * @filep: file pointer to write
214 * @data: data buffer to write data from
215 * @count: size of buffer
216 * @offset: offset at which to write data
217 *
218 */
219
220static ssize_t
221v9fs_file_write(struct file *filp, const char __user * data,
222 size_t count, loff_t * offset)
223{
Latchesar Ionkov19cba8a2005-10-11 08:29:03 -0700224 struct inode *inode = filp->f_dentry->d_inode;
225 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
226 struct v9fs_fid *v9fid = filp->private_data;
227 struct v9fs_fcall *fcall;
228 int fid = v9fid->fid;
229 int result = -EIO;
230 int rsize = 0;
231 int total = 0;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700232
Latchesar Ionkov19cba8a2005-10-11 08:29:03 -0700233 dprintk(DEBUG_VFS, "data %p count %d offset %x\n", data, (int)count,
234 (int)*offset);
235 rsize = v9ses->maxdata - V9FS_IOHDRSZ;
236 if (v9fid->iounit != 0 && rsize > v9fid->iounit)
237 rsize = v9fid->iounit;
238
Latchesar Ionkov19cba8a2005-10-11 08:29:03 -0700239 do {
240 if (count < rsize)
241 rsize = count;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700242
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800243 result = v9fs_t_write(v9ses, fid, *offset, rsize, data, &fcall);
Latchesar Ionkov19cba8a2005-10-11 08:29:03 -0700244 if (result < 0) {
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800245 PRINT_FCALL_ERROR("error while writing", fcall);
Latchesar Ionkov19cba8a2005-10-11 08:29:03 -0700246 kfree(fcall);
Latchesar Ionkov19cba8a2005-10-11 08:29:03 -0700247 return result;
248 } else
249 *offset += result;
250
251 kfree(fcall);
252 fcall = NULL;
253
254 if (result != rsize) {
255 eprintk(KERN_ERR,
256 "short write: v9fs_t_write returned %d\n",
257 result);
258 break;
259 }
260
261 count -= result;
262 data += result;
263 total += result;
264 } while (count);
265
Eric Van Hensbergen147b31c2006-01-18 17:43:02 -0800266 invalidate_inode_pages2(inode->i_mapping);
Latchesar Ionkov19cba8a2005-10-11 08:29:03 -0700267 return total;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700268}
269
270struct file_operations v9fs_file_operations = {
271 .llseek = generic_file_llseek,
272 .read = v9fs_file_read,
273 .write = v9fs_file_write,
274 .open = v9fs_file_open,
275 .release = v9fs_dir_release,
276 .lock = v9fs_file_lock,
Eric Van Hensbergen147b31c2006-01-18 17:43:02 -0800277 .mmap = generic_file_mmap,
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700278};