1f8ae7d580abea733ecffd334403d400ed8f640d
[linux-2.6.git] / fs / 9p / vfs_file.c
1 /*
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>
35 #include <linux/version.h>
36 #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
53 int v9fs_file_open(struct inode *inode, struct file *file)
54 {
55         struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
56         struct v9fs_fid *v9fid = v9fs_fid_lookup(file->f_dentry, FID_WALK);
57         struct v9fs_fid *v9newfid = NULL;
58         struct v9fs_fcall *fcall = NULL;
59         int open_mode = 0;
60         unsigned int iounit = 0;
61         int newfid = -1;
62         long result = -1;
63
64         dprintk(DEBUG_VFS, "inode: %p file: %p v9fid= %p\n", inode, file,
65                 v9fid);
66
67         if (!v9fid) {
68                 struct dentry *dentry = file->f_dentry;
69                 dprintk(DEBUG_ERROR, "Couldn't resolve fid from dentry\n");
70
71                 /* XXX - some duplication from lookup, generalize later */
72                 /* basically vfs_lookup is too heavy weight */
73                 v9fid = v9fs_fid_lookup(file->f_dentry, FID_OP);
74                 if (!v9fid)
75                         return -EBADF;
76
77                 v9fid = v9fs_fid_lookup(dentry->d_parent, FID_WALK);
78                 if (!v9fid)
79                         return -EBADF;
80
81                 newfid = v9fs_get_idpool(&v9ses->fidpool);
82                 if (newfid < 0) {
83                         eprintk(KERN_WARNING, "newfid fails!\n");
84                         return -ENOSPC;
85                 }
86
87                 result =
88                     v9fs_t_walk(v9ses, v9fid->fid, newfid,
89                                 (char *)file->f_dentry->d_name.name, NULL);
90                 if (result < 0) {
91                         v9fs_put_idpool(newfid, &v9ses->fidpool);
92                         dprintk(DEBUG_ERROR, "rewalk didn't work\n");
93                         return -EBADF;
94                 }
95
96                 v9fid = v9fs_fid_create(dentry);
97                 if (v9fid == NULL) {
98                         dprintk(DEBUG_ERROR, "couldn't insert\n");
99                         return -ENOMEM;
100                 }
101                 v9fid->fid = newfid;
102         }
103
104         if (v9fid->fidcreate) {
105                 /* create case */
106                 newfid = v9fid->fid;
107                 iounit = v9fid->iounit;
108                 v9fid->fidcreate = 0;
109         } else {
110                 if (!S_ISDIR(inode->i_mode))
111                         newfid = v9fid->fid;
112                 else {
113                         newfid = v9fs_get_idpool(&v9ses->fidpool);
114                         if (newfid < 0) {
115                                 eprintk(KERN_WARNING, "allocation failed\n");
116                                 return -ENOSPC;
117                         }
118                         /* This would be a somewhat critical clone */
119                         result =
120                             v9fs_t_walk(v9ses, v9fid->fid, newfid, NULL,
121                                         &fcall);
122                         if (result < 0) {
123                                 dprintk(DEBUG_ERROR, "clone error: %s\n",
124                                         FCALL_ERROR(fcall));
125                                 kfree(fcall);
126                                 return result;
127                         }
128
129                         v9newfid = v9fs_fid_create(file->f_dentry);
130                         v9newfid->fid = newfid;
131                         v9newfid->qid = v9fid->qid;
132                         v9newfid->iounit = v9fid->iounit;
133                         v9newfid->fidopen = 0;
134                         v9newfid->fidclunked = 0;
135                         v9newfid->v9ses = v9ses;
136                         v9fid = v9newfid;
137                         kfree(fcall);
138                 }
139
140                 /* TODO: do special things for O_EXCL, O_NOFOLLOW, O_SYNC */
141                 /* translate open mode appropriately */
142                 open_mode = file->f_flags & 0x3;
143
144                 if (file->f_flags & O_EXCL)
145                         open_mode |= V9FS_OEXCL;
146
147                 if (v9ses->extended) {
148                         if (file->f_flags & O_TRUNC)
149                                 open_mode |= V9FS_OTRUNC;
150
151                         if (file->f_flags & O_APPEND)
152                                 open_mode |= V9FS_OAPPEND;
153                 }
154
155                 result = v9fs_t_open(v9ses, newfid, open_mode, &fcall);
156                 if (result < 0) {
157                         dprintk(DEBUG_ERROR,
158                                 "open failed, open_mode 0x%x: %s\n", open_mode,
159                                 FCALL_ERROR(fcall));
160                         kfree(fcall);
161                         return result;
162                 }
163
164                 iounit = fcall->params.ropen.iounit;
165                 kfree(fcall);
166         }
167
168
169         file->private_data = v9fid;
170
171         v9fid->rdir_pos = 0;
172         v9fid->rdir_fcall = NULL;
173         v9fid->fidopen = 1;
174         v9fid->filp = file;
175         v9fid->iounit = iounit;
176
177         return 0;
178 }
179
180 /**
181  * v9fs_file_lock - lock a file (or directory)
182  * @inode: inode to be opened
183  * @file: file being opened
184  *
185  * XXX - this looks like a local only lock, we should extend into 9P
186  *       by using open exclusive
187  */
188
189 static int v9fs_file_lock(struct file *filp, int cmd, struct file_lock *fl)
190 {
191         int res = 0;
192         struct inode *inode = filp->f_dentry->d_inode;
193
194         dprintk(DEBUG_VFS, "filp: %p lock: %p\n", filp, fl);
195
196         /* No mandatory locks */
197         if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
198                 return -ENOLCK;
199
200         if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
201                 filemap_fdatawrite(inode->i_mapping);
202                 filemap_fdatawait(inode->i_mapping);
203                 invalidate_inode_pages(&inode->i_data);
204         }
205
206         return res;
207 }
208
209 /**
210  * v9fs_read - read from a file (internal)
211  * @filep: file pointer to read
212  * @data: data buffer to read data into
213  * @count: size of buffer
214  * @offset: offset at which to read data
215  *
216  */
217
218 static ssize_t
219 v9fs_read(struct file *filp, char *buffer, size_t count, loff_t * offset)
220 {
221         struct inode *inode = filp->f_dentry->d_inode;
222         struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
223         struct v9fs_fid *v9f = filp->private_data;
224         struct v9fs_fcall *fcall = NULL;
225         int fid = v9f->fid;
226         int rsize = 0;
227         int result = 0;
228         int total = 0;
229
230         dprintk(DEBUG_VFS, "\n");
231
232         rsize = v9ses->maxdata - V9FS_IOHDRSZ;
233         if (v9f->iounit != 0 && rsize > v9f->iounit)
234                 rsize = v9f->iounit;
235
236         do {
237                 if (count < rsize)
238                         rsize = count;
239
240                 result = v9fs_t_read(v9ses, fid, *offset, rsize, &fcall);
241
242                 if (result < 0) {
243                         printk(KERN_ERR "9P2000: v9fs_t_read returned %d\n",
244                                result);
245
246                         kfree(fcall);
247                         return total;
248                 } else
249                         *offset += result;
250
251                 /* XXX - extra copy */
252                 memcpy(buffer, fcall->params.rread.data, result);
253                 count -= result;
254                 buffer += result;
255                 total += result;
256
257                 kfree(fcall);
258
259                 if (result < rsize)
260                         break;
261         } while (count);
262
263         return total;
264 }
265
266 /**
267  * v9fs_file_read - read from a file
268  * @filep: file pointer to read
269  * @data: data buffer to read data into
270  * @count: size of buffer
271  * @offset: offset at which to read data
272  *
273  */
274
275 static ssize_t
276 v9fs_file_read(struct file *filp, char __user * data, size_t count,
277                loff_t * offset)
278 {
279         int retval = -1;
280         int ret = 0;
281         char *buffer;
282
283         buffer = kmalloc(count, GFP_KERNEL);
284         if (!buffer)
285                 return -ENOMEM;
286
287         retval = v9fs_read(filp, buffer, count, offset);
288         if (retval > 0) {
289                 if ((ret = copy_to_user(data, buffer, retval)) != 0) {
290                         dprintk(DEBUG_ERROR, "Problem copying to user %d\n",
291                                 ret);
292                         retval = ret;
293                 }
294         }
295
296         kfree(buffer);
297
298         return retval;
299 }
300
301 /**
302  * v9fs_write - write to a file
303  * @filep: file pointer to write
304  * @data: data buffer to write data from
305  * @count: size of buffer
306  * @offset: offset at which to write data
307  *
308  */
309
310 static ssize_t
311 v9fs_write(struct file *filp, char *buffer, size_t count, loff_t * offset)
312 {
313         struct inode *inode = filp->f_dentry->d_inode;
314         struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
315         struct v9fs_fid *v9fid = filp->private_data;
316         struct v9fs_fcall *fcall;
317         int fid = v9fid->fid;
318         int result = -EIO;
319         int rsize = 0;
320         int total = 0;
321
322         dprintk(DEBUG_VFS, "data %p count %d offset %x\n", buffer, (int)count,
323                 (int)*offset);
324         rsize = v9ses->maxdata - V9FS_IOHDRSZ;
325         if (v9fid->iounit != 0 && rsize > v9fid->iounit)
326                 rsize = v9fid->iounit;
327
328         dump_data(buffer, count);
329
330         do {
331                 if (count < rsize)
332                         rsize = count;
333
334                 result =
335                     v9fs_t_write(v9ses, fid, *offset, rsize, buffer, &fcall);
336                 if (result < 0) {
337                         eprintk(KERN_ERR, "error while writing: %s(%d)\n",
338                                 FCALL_ERROR(fcall), result);
339                         kfree(fcall);
340                         return result;
341                 } else
342                         *offset += result;
343
344                 kfree(fcall);
345
346                 if (result != rsize) {
347                         eprintk(KERN_ERR,
348                                 "short write: v9fs_t_write returned %d\n",
349                                 result);
350                         break;
351                 }
352
353                 count -= result;
354                 buffer += result;
355                 total += result;
356         } while (count);
357
358         return total;
359 }
360
361 /**
362  * v9fs_file_write - write to a file
363  * @filep: file pointer to write
364  * @data: data buffer to write data from
365  * @count: size of buffer
366  * @offset: offset at which to write data
367  *
368  */
369
370 static ssize_t
371 v9fs_file_write(struct file *filp, const char __user * data,
372                 size_t count, loff_t * offset)
373 {
374         int ret = -1;
375         char *buffer;
376
377         buffer = kmalloc(count, GFP_KERNEL);
378         if (buffer == NULL)
379                 return -ENOMEM;
380
381         ret = copy_from_user(buffer, data, count);
382         if (ret) {
383                 dprintk(DEBUG_ERROR, "Problem copying from user\n");
384                 ret = -EFAULT;
385         } else {
386                 ret = v9fs_write(filp, buffer, count, offset);
387         }
388
389         kfree(buffer);
390
391         return ret;
392 }
393
394 struct file_operations v9fs_file_operations = {
395         .llseek = generic_file_llseek,
396         .read = v9fs_file_read,
397         .write = v9fs_file_write,
398         .open = v9fs_file_open,
399         .release = v9fs_dir_release,
400         .lock = v9fs_file_lock,
401 };