dce729d428699c7653f18cdb0b6f5ff51ee40a7a
[safe/jmp/linux-2.6] / fs / 9p / vfs_inode.c
1 /*
2  *  linux/fs/9p/vfs_inode.c
3  *
4  * This file contains vfs inode ops for the 9P2000 protocol.
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/pagemap.h>
32 #include <linux/stat.h>
33 #include <linux/string.h>
34 #include <linux/smp_lock.h>
35 #include <linux/inet.h>
36 #include <linux/namei.h>
37 #include <linux/idr.h>
38
39 #include "debug.h"
40 #include "v9fs.h"
41 #include "9p.h"
42 #include "v9fs_vfs.h"
43 #include "fid.h"
44
45 static struct inode_operations v9fs_dir_inode_operations;
46 static struct inode_operations v9fs_dir_inode_operations_ext;
47 static struct inode_operations v9fs_file_inode_operations;
48 static struct inode_operations v9fs_symlink_inode_operations;
49
50 /**
51  * unixmode2p9mode - convert unix mode bits to plan 9
52  * @v9ses: v9fs session information
53  * @mode: mode to convert
54  *
55  */
56
57 static int unixmode2p9mode(struct v9fs_session_info *v9ses, int mode)
58 {
59         int res;
60         res = mode & 0777;
61         if (S_ISDIR(mode))
62                 res |= V9FS_DMDIR;
63         if (v9ses->extended) {
64                 if (S_ISLNK(mode))
65                         res |= V9FS_DMSYMLINK;
66                 if (v9ses->nodev == 0) {
67                         if (S_ISSOCK(mode))
68                                 res |= V9FS_DMSOCKET;
69                         if (S_ISFIFO(mode))
70                                 res |= V9FS_DMNAMEDPIPE;
71                         if (S_ISBLK(mode))
72                                 res |= V9FS_DMDEVICE;
73                         if (S_ISCHR(mode))
74                                 res |= V9FS_DMDEVICE;
75                 }
76
77                 if ((mode & S_ISUID) == S_ISUID)
78                         res |= V9FS_DMSETUID;
79                 if ((mode & S_ISGID) == S_ISGID)
80                         res |= V9FS_DMSETGID;
81                 if ((mode & V9FS_DMLINK))
82                         res |= V9FS_DMLINK;
83         }
84
85         return res;
86 }
87
88 /**
89  * p9mode2unixmode- convert plan9 mode bits to unix mode bits
90  * @v9ses: v9fs session information
91  * @mode: mode to convert
92  *
93  */
94
95 static int p9mode2unixmode(struct v9fs_session_info *v9ses, int mode)
96 {
97         int res;
98
99         res = mode & 0777;
100
101         if ((mode & V9FS_DMDIR) == V9FS_DMDIR)
102                 res |= S_IFDIR;
103         else if ((mode & V9FS_DMSYMLINK) && (v9ses->extended))
104                 res |= S_IFLNK;
105         else if ((mode & V9FS_DMSOCKET) && (v9ses->extended)
106                  && (v9ses->nodev == 0))
107                 res |= S_IFSOCK;
108         else if ((mode & V9FS_DMNAMEDPIPE) && (v9ses->extended)
109                  && (v9ses->nodev == 0))
110                 res |= S_IFIFO;
111         else if ((mode & V9FS_DMDEVICE) && (v9ses->extended)
112                  && (v9ses->nodev == 0))
113                 res |= S_IFBLK;
114         else
115                 res |= S_IFREG;
116
117         if (v9ses->extended) {
118                 if ((mode & V9FS_DMSETUID) == V9FS_DMSETUID)
119                         res |= S_ISUID;
120
121                 if ((mode & V9FS_DMSETGID) == V9FS_DMSETGID)
122                         res |= S_ISGID;
123         }
124
125         return res;
126 }
127
128 int v9fs_uflags2omode(int uflags)
129 {
130         int ret;
131
132         ret = 0;
133         switch (uflags&3) {
134         default:
135         case O_RDONLY:
136                 ret = V9FS_OREAD;
137                 break;
138
139         case O_WRONLY:
140                 ret = V9FS_OWRITE;
141                 break;
142
143         case O_RDWR:
144                 ret = V9FS_ORDWR;
145                 break;
146         }
147
148         if (uflags & O_EXCL)
149                 ret |= V9FS_OEXCL;
150
151         if (uflags & O_TRUNC)
152                 ret |= V9FS_OTRUNC;
153
154         if (uflags & O_APPEND)
155                 ret |= V9FS_OAPPEND;
156
157         return ret;
158 }
159
160 /**
161  * v9fs_blank_wstat - helper function to setup a 9P stat structure
162  * @v9ses: 9P session info (for determining extended mode)
163  * @wstat: structure to initialize
164  *
165  */
166
167 static void
168 v9fs_blank_wstat(struct v9fs_wstat *wstat)
169 {
170         wstat->type = ~0;
171         wstat->dev = ~0;
172         wstat->qid.type = ~0;
173         wstat->qid.version = ~0;
174         *((long long *)&wstat->qid.path) = ~0;
175         wstat->mode = ~0;
176         wstat->atime = ~0;
177         wstat->mtime = ~0;
178         wstat->length = ~0;
179         wstat->name = NULL;
180         wstat->uid = NULL;
181         wstat->gid = NULL;
182         wstat->muid = NULL;
183         wstat->n_uid = ~0;
184         wstat->n_gid = ~0;
185         wstat->n_muid = ~0;
186         wstat->extension = NULL;
187 }
188
189 /**
190  * v9fs_get_inode - helper function to setup an inode
191  * @sb: superblock
192  * @mode: mode to setup inode with
193  *
194  */
195
196 struct inode *v9fs_get_inode(struct super_block *sb, int mode)
197 {
198         struct inode *inode;
199         struct v9fs_session_info *v9ses = sb->s_fs_info;
200
201         dprintk(DEBUG_VFS, "super block: %p mode: %o\n", sb, mode);
202
203         inode = new_inode(sb);
204         if (inode) {
205                 inode->i_mode = mode;
206                 inode->i_uid = current->fsuid;
207                 inode->i_gid = current->fsgid;
208                 inode->i_blksize = sb->s_blocksize;
209                 inode->i_blocks = 0;
210                 inode->i_rdev = 0;
211                 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
212                 inode->i_mapping->a_ops = &v9fs_addr_operations;
213
214                 switch (mode & S_IFMT) {
215                 case S_IFIFO:
216                 case S_IFBLK:
217                 case S_IFCHR:
218                 case S_IFSOCK:
219                         if(!v9ses->extended) {
220                                 dprintk(DEBUG_ERROR, "special files without extended mode\n");
221                                 return ERR_PTR(-EINVAL);
222                         }
223                         init_special_inode(inode, inode->i_mode,
224                                            inode->i_rdev);
225                         break;
226                 case S_IFREG:
227                         inode->i_op = &v9fs_file_inode_operations;
228                         inode->i_fop = &v9fs_file_operations;
229                         break;
230                 case S_IFLNK:
231                         if(!v9ses->extended) {
232                                 dprintk(DEBUG_ERROR, "extended modes used w/o 9P2000.u\n");
233                                 return ERR_PTR(-EINVAL);
234                         }
235                         inode->i_op = &v9fs_symlink_inode_operations;
236                         break;
237                 case S_IFDIR:
238                         inode->i_nlink++;
239                         if(v9ses->extended)
240                                 inode->i_op = &v9fs_dir_inode_operations_ext;
241                         else
242                                 inode->i_op = &v9fs_dir_inode_operations;
243                         inode->i_fop = &v9fs_dir_operations;
244                         break;
245                 default:
246                         dprintk(DEBUG_ERROR, "BAD mode 0x%x S_IFMT 0x%x\n",
247                                 mode, mode & S_IFMT);
248                         return ERR_PTR(-EINVAL);
249                 }
250         } else {
251                 eprintk(KERN_WARNING, "Problem allocating inode\n");
252                 return ERR_PTR(-ENOMEM);
253         }
254         return inode;
255 }
256
257 static int
258 v9fs_create(struct v9fs_session_info *v9ses, u32 pfid, char *name,
259         u32 perm, u8 mode, u32 *fidp, struct v9fs_qid *qid, u32 *iounit)
260 {
261         u32 fid;
262         int err;
263         struct v9fs_fcall *fcall;
264
265         fid = v9fs_get_idpool(&v9ses->fidpool);
266         if (fid < 0) {
267                 eprintk(KERN_WARNING, "no free fids available\n");
268                 err = -ENOSPC;
269                 goto error;
270         }
271
272         err = v9fs_t_walk(v9ses, pfid, fid, NULL, &fcall);
273         if (err < 0) {
274                 PRINT_FCALL_ERROR("clone error", fcall);
275                 goto error;
276         }
277         kfree(fcall);
278
279         err = v9fs_t_create(v9ses, fid, name, perm, mode, &fcall);
280         if (err < 0) {
281                 PRINT_FCALL_ERROR("create fails", fcall);
282                 goto error;
283         }
284
285         if (iounit)
286                 *iounit = fcall->params.rcreate.iounit;
287
288         if (qid)
289                 *qid = fcall->params.rcreate.qid;
290
291         if (fidp)
292                 *fidp = fid;
293
294         kfree(fcall);
295         return 0;
296
297 error:
298         if (fid >= 0)
299                 v9fs_put_idpool(fid, &v9ses->fidpool);
300
301         kfree(fcall);
302         return err;
303 }
304
305 static struct v9fs_fid*
306 v9fs_clone_walk(struct v9fs_session_info *v9ses, u32 fid, struct dentry *dentry)
307 {
308         int err;
309         u32 nfid;
310         struct v9fs_fid *ret;
311         struct v9fs_fcall *fcall;
312
313         nfid = v9fs_get_idpool(&v9ses->fidpool);
314         if (nfid < 0) {
315                 eprintk(KERN_WARNING, "no free fids available\n");
316                 err = -ENOSPC;
317                 goto error;
318         }
319
320         err = v9fs_t_walk(v9ses, fid, nfid, (char *) dentry->d_name.name,
321                 &fcall);
322
323         if (err < 0) {
324                 PRINT_FCALL_ERROR("walk error", fcall);
325                 v9fs_put_idpool(nfid, &v9ses->fidpool);
326                 goto error;
327         }
328
329         kfree(fcall);
330         fcall = NULL;
331         ret = v9fs_fid_create(v9ses, nfid);
332         if (!ret) {
333                 err = -ENOMEM;
334                 goto clunk_fid;
335         }
336
337         err = v9fs_fid_insert(ret, dentry);
338         if (err < 0) {
339                 v9fs_fid_destroy(ret);
340                 goto clunk_fid;
341         }
342
343         return ret;
344
345 clunk_fid:
346         v9fs_t_clunk(v9ses, nfid);
347
348 error:
349         kfree(fcall);
350         return ERR_PTR(err);
351 }
352
353 struct inode *
354 v9fs_inode_from_fid(struct v9fs_session_info *v9ses, u32 fid,
355         struct super_block *sb)
356 {
357         int err, umode;
358         struct inode *ret;
359         struct v9fs_fcall *fcall;
360
361         ret = NULL;
362         err = v9fs_t_stat(v9ses, fid, &fcall);
363         if (err) {
364                 PRINT_FCALL_ERROR("stat error", fcall);
365                 goto error;
366         }
367
368         umode = p9mode2unixmode(v9ses, fcall->params.rstat.stat.mode);
369         ret = v9fs_get_inode(sb, umode);
370         if (IS_ERR(ret)) {
371                 err = PTR_ERR(ret);
372                 ret = NULL;
373                 goto error;
374         }
375
376         v9fs_stat2inode(&fcall->params.rstat.stat, ret, sb);
377         kfree(fcall);
378         return ret;
379
380 error:
381         kfree(fcall);
382         if (ret)
383                 iput(ret);
384
385         return ERR_PTR(err);
386 }
387
388 /**
389  * v9fs_remove - helper function to remove files and directories
390  * @dir: directory inode that is being deleted
391  * @file:  dentry that is being deleted
392  * @rmdir: removing a directory
393  *
394  */
395
396 static int v9fs_remove(struct inode *dir, struct dentry *file, int rmdir)
397 {
398         struct v9fs_fcall *fcall = NULL;
399         struct super_block *sb = NULL;
400         struct v9fs_session_info *v9ses = NULL;
401         struct v9fs_fid *v9fid = NULL;
402         struct inode *file_inode = NULL;
403         int fid = -1;
404         int result = 0;
405
406         dprintk(DEBUG_VFS, "inode: %p dentry: %p rmdir: %d\n", dir, file,
407                 rmdir);
408
409         file_inode = file->d_inode;
410         sb = file_inode->i_sb;
411         v9ses = v9fs_inode2v9ses(file_inode);
412         v9fid = v9fs_fid_lookup(file);
413
414         if (!v9fid) {
415                 dprintk(DEBUG_ERROR,
416                         "no v9fs_fid\n");
417                 return -EBADF;
418         }
419
420         fid = v9fid->fid;
421         if (fid < 0) {
422                 dprintk(DEBUG_ERROR, "inode #%lu, no fid!\n",
423                         file_inode->i_ino);
424                 return -EBADF;
425         }
426
427         result = v9fs_t_remove(v9ses, fid, &fcall);
428         if (result < 0) {
429                 PRINT_FCALL_ERROR("remove fails", fcall);
430         } else {
431                 v9fs_put_idpool(fid, &v9ses->fidpool);
432                 v9fs_fid_destroy(v9fid);
433         }
434
435         kfree(fcall);
436         return result;
437 }
438
439 static int
440 v9fs_open_created(struct inode *inode, struct file *file)
441 {
442         return 0;
443 }
444
445 /**
446  * v9fs_vfs_create - VFS hook to create files
447  * @inode: directory inode that is being deleted
448  * @dentry:  dentry that is being deleted
449  * @mode: create permissions
450  * @nd: path information
451  *
452  */
453
454 static int
455 v9fs_vfs_create(struct inode *dir, struct dentry *dentry, int mode,
456                 struct nameidata *nd)
457 {
458         int err;
459         u32 fid, perm, iounit;
460         int flags;
461         struct v9fs_session_info *v9ses;
462         struct v9fs_fid *dfid, *vfid, *ffid;
463         struct inode *inode;
464         struct v9fs_qid qid;
465         struct file *filp;
466
467         inode = NULL;
468         vfid = NULL;
469         v9ses = v9fs_inode2v9ses(dir);
470         dfid = v9fs_fid_lookup(dentry->d_parent);
471         perm = unixmode2p9mode(v9ses, mode);
472
473         if (nd && nd->flags & LOOKUP_OPEN)
474                 flags = nd->intent.open.flags - 1;
475         else
476                 flags = O_RDWR;
477
478         err = v9fs_create(v9ses, dfid->fid, (char *) dentry->d_name.name,
479                 perm, v9fs_uflags2omode(flags), &fid, &qid, &iounit);
480
481         if (err)
482                 goto error;
483
484         vfid = v9fs_clone_walk(v9ses, dfid->fid, dentry);
485         if (IS_ERR(vfid)) {
486                 err = PTR_ERR(vfid);
487                 vfid = NULL;
488                 goto error;
489         }
490
491         inode = v9fs_inode_from_fid(v9ses, vfid->fid, dir->i_sb);
492         if (IS_ERR(inode)) {
493                 err = PTR_ERR(inode);
494                 inode = NULL;
495                 goto error;
496         }
497
498         dentry->d_op = &v9fs_dentry_operations;
499         d_instantiate(dentry, inode);
500
501         if (nd && nd->flags & LOOKUP_OPEN) {
502                 ffid = v9fs_fid_create(v9ses, fid);
503                 if (!ffid)
504                         return -ENOMEM;
505
506                 filp = lookup_instantiate_filp(nd, dentry, v9fs_open_created);
507                 if (IS_ERR(filp)) {
508                         v9fs_fid_destroy(ffid);
509                         return PTR_ERR(filp);
510                 }
511
512                 ffid->rdir_pos = 0;
513                 ffid->rdir_fcall = NULL;
514                 ffid->fidopen = 1;
515                 ffid->iounit = iounit;
516                 ffid->filp = filp;
517                 filp->private_data = ffid;
518         }
519
520         return 0;
521
522 error:
523         if (vfid)
524                 v9fs_fid_destroy(vfid);
525
526         if (inode)
527                 iput(inode);
528
529         return err;
530 }
531
532 /**
533  * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
534  * @inode:  inode that is being unlinked
535  * @dentry: dentry that is being unlinked
536  * @mode: mode for new directory
537  *
538  */
539
540 static int v9fs_vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
541 {
542         int err;
543         u32 fid, perm;
544         struct v9fs_session_info *v9ses;
545         struct v9fs_fid *dfid, *vfid;
546         struct inode *inode;
547
548         inode = NULL;
549         vfid = NULL;
550         v9ses = v9fs_inode2v9ses(dir);
551         dfid = v9fs_fid_lookup(dentry->d_parent);
552         perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
553
554         err = v9fs_create(v9ses, dfid->fid, (char *) dentry->d_name.name,
555                 perm, V9FS_OREAD, &fid, NULL, NULL);
556
557         if (err) {
558                 dprintk(DEBUG_ERROR, "create error %d\n", err);
559                 goto error;
560         }
561
562         err = v9fs_t_clunk(v9ses, fid);
563         if (err) {
564                 dprintk(DEBUG_ERROR, "clunk error %d\n", err);
565                 goto error;
566         }
567
568         vfid = v9fs_clone_walk(v9ses, dfid->fid, dentry);
569         if (IS_ERR(vfid)) {
570                 err = PTR_ERR(vfid);
571                 vfid = NULL;
572                 goto error;
573         }
574
575         inode = v9fs_inode_from_fid(v9ses, vfid->fid, dir->i_sb);
576         if (IS_ERR(inode)) {
577                 err = PTR_ERR(inode);
578                 inode = NULL;
579                 goto error;
580         }
581
582         dentry->d_op = &v9fs_dentry_operations;
583         d_instantiate(dentry, inode);
584         return 0;
585
586 error:
587         if (vfid)
588                 v9fs_fid_destroy(vfid);
589
590         return err;
591 }
592
593 /**
594  * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
595  * @dir:  inode that is being walked from
596  * @dentry: dentry that is being walked to?
597  * @nameidata: path data
598  *
599  */
600
601 static struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
602                                       struct nameidata *nameidata)
603 {
604         struct super_block *sb;
605         struct v9fs_session_info *v9ses;
606         struct v9fs_fid *dirfid;
607         struct v9fs_fid *fid;
608         struct inode *inode;
609         struct v9fs_fcall *fcall = NULL;
610         int dirfidnum = -1;
611         int newfid = -1;
612         int result = 0;
613
614         dprintk(DEBUG_VFS, "dir: %p dentry: (%s) %p nameidata: %p\n",
615                 dir, dentry->d_iname, dentry, nameidata);
616
617         sb = dir->i_sb;
618         v9ses = v9fs_inode2v9ses(dir);
619         dirfid = v9fs_fid_lookup(dentry->d_parent);
620
621         if (!dirfid) {
622                 dprintk(DEBUG_ERROR, "no dirfid\n");
623                 return ERR_PTR(-EINVAL);
624         }
625
626         dirfidnum = dirfid->fid;
627
628         if (dirfidnum < 0) {
629                 dprintk(DEBUG_ERROR, "no dirfid for inode %p, #%lu\n",
630                         dir, dir->i_ino);
631                 return ERR_PTR(-EBADF);
632         }
633
634         newfid = v9fs_get_idpool(&v9ses->fidpool);
635         if (newfid < 0) {
636                 eprintk(KERN_WARNING, "newfid fails!\n");
637                 return ERR_PTR(-ENOSPC);
638         }
639
640         result = v9fs_t_walk(v9ses, dirfidnum, newfid,
641                 (char *)dentry->d_name.name, NULL);
642         if (result < 0) {
643                 v9fs_put_idpool(newfid, &v9ses->fidpool);
644                 if (result == -ENOENT) {
645                         d_add(dentry, NULL);
646                         dprintk(DEBUG_VFS,
647                                 "Return negative dentry %p count %d\n",
648                                 dentry, atomic_read(&dentry->d_count));
649                         return NULL;
650                 }
651                 dprintk(DEBUG_ERROR, "walk error:%d\n", result);
652                 goto FreeFcall;
653         }
654
655         result = v9fs_t_stat(v9ses, newfid, &fcall);
656         if (result < 0) {
657                 dprintk(DEBUG_ERROR, "stat error\n");
658                 goto FreeFcall;
659         }
660
661         inode = v9fs_get_inode(sb, p9mode2unixmode(v9ses,
662                 fcall->params.rstat.stat.mode));
663
664         if (IS_ERR(inode) && (PTR_ERR(inode) == -ENOSPC)) {
665                 eprintk(KERN_WARNING, "inode alloc failes, returns %ld\n",
666                         PTR_ERR(inode));
667
668                 result = -ENOSPC;
669                 goto FreeFcall;
670         }
671
672         inode->i_ino = v9fs_qid2ino(&fcall->params.rstat.stat.qid);
673
674         fid = v9fs_fid_create(v9ses, newfid);
675         if (fid == NULL) {
676                 dprintk(DEBUG_ERROR, "couldn't insert\n");
677                 result = -ENOMEM;
678                 goto FreeFcall;
679         }
680
681         result = v9fs_fid_insert(fid, dentry);
682         if (result < 0)
683                 goto FreeFcall;
684
685         fid->qid = fcall->params.rstat.stat.qid;
686
687         dentry->d_op = &v9fs_dentry_operations;
688         v9fs_stat2inode(&fcall->params.rstat.stat, inode, inode->i_sb);
689
690         d_add(dentry, inode);
691         kfree(fcall);
692
693         return NULL;
694
695       FreeFcall:
696         kfree(fcall);
697         return ERR_PTR(result);
698 }
699
700 /**
701  * v9fs_vfs_unlink - VFS unlink hook to delete an inode
702  * @i:  inode that is being unlinked
703  * @d: dentry that is being unlinked
704  *
705  */
706
707 static int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
708 {
709         return v9fs_remove(i, d, 0);
710 }
711
712 /**
713  * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
714  * @i:  inode that is being unlinked
715  * @d: dentry that is being unlinked
716  *
717  */
718
719 static int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
720 {
721         return v9fs_remove(i, d, 1);
722 }
723
724 /**
725  * v9fs_vfs_rename - VFS hook to rename an inode
726  * @old_dir:  old dir inode
727  * @old_dentry: old dentry
728  * @new_dir: new dir inode
729  * @new_dentry: new dentry
730  *
731  */
732
733 static int
734 v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
735                 struct inode *new_dir, struct dentry *new_dentry)
736 {
737         struct inode *old_inode = old_dentry->d_inode;
738         struct v9fs_session_info *v9ses = v9fs_inode2v9ses(old_inode);
739         struct v9fs_fid *oldfid = v9fs_fid_lookup(old_dentry);
740         struct v9fs_fid *olddirfid =
741             v9fs_fid_lookup(old_dentry->d_parent);
742         struct v9fs_fid *newdirfid =
743             v9fs_fid_lookup(new_dentry->d_parent);
744         struct v9fs_wstat wstat;
745         struct v9fs_fcall *fcall = NULL;
746         int fid = -1;
747         int olddirfidnum = -1;
748         int newdirfidnum = -1;
749         int retval = 0;
750
751         dprintk(DEBUG_VFS, "\n");
752
753         if ((!oldfid) || (!olddirfid) || (!newdirfid)) {
754                 dprintk(DEBUG_ERROR, "problem with arguments\n");
755                 return -EBADF;
756         }
757
758         /* 9P can only handle file rename in the same directory */
759         if (memcmp(&olddirfid->qid, &newdirfid->qid, sizeof(newdirfid->qid))) {
760                 dprintk(DEBUG_ERROR, "old dir and new dir are different\n");
761                 retval = -EPERM;
762                 goto FreeFcallnBail;
763         }
764
765         fid = oldfid->fid;
766         olddirfidnum = olddirfid->fid;
767         newdirfidnum = newdirfid->fid;
768
769         if (fid < 0) {
770                 dprintk(DEBUG_ERROR, "no fid for old file #%lu\n",
771                         old_inode->i_ino);
772                 retval = -EBADF;
773                 goto FreeFcallnBail;
774         }
775
776         v9fs_blank_wstat(&wstat);
777         wstat.muid = v9ses->name;
778         wstat.name = (char *) new_dentry->d_name.name;
779
780         retval = v9fs_t_wstat(v9ses, fid, &wstat, &fcall);
781
782       FreeFcallnBail:
783         if (retval < 0)
784                 PRINT_FCALL_ERROR("wstat error", fcall);
785
786         kfree(fcall);
787         return retval;
788 }
789
790 /**
791  * v9fs_vfs_getattr - retrieve file metadata
792  * @mnt - mount information
793  * @dentry - file to get attributes on
794  * @stat - metadata structure to populate
795  *
796  */
797
798 static int
799 v9fs_vfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
800                  struct kstat *stat)
801 {
802         struct v9fs_fcall *fcall = NULL;
803         struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dentry->d_inode);
804         struct v9fs_fid *fid = v9fs_fid_lookup(dentry);
805         int err = -EPERM;
806
807         dprintk(DEBUG_VFS, "dentry: %p\n", dentry);
808         if (!fid) {
809                 dprintk(DEBUG_ERROR,
810                         "couldn't find fid associated with dentry\n");
811                 return -EBADF;
812         }
813
814         err = v9fs_t_stat(v9ses, fid->fid, &fcall);
815
816         if (err < 0)
817                 dprintk(DEBUG_ERROR, "stat error\n");
818         else {
819                 v9fs_stat2inode(&fcall->params.rstat.stat, dentry->d_inode,
820                                   dentry->d_inode->i_sb);
821                 generic_fillattr(dentry->d_inode, stat);
822         }
823
824         kfree(fcall);
825         return err;
826 }
827
828 /**
829  * v9fs_vfs_setattr - set file metadata
830  * @dentry: file whose metadata to set
831  * @iattr: metadata assignment structure
832  *
833  */
834
835 static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
836 {
837         struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dentry->d_inode);
838         struct v9fs_fid *fid = v9fs_fid_lookup(dentry);
839         struct v9fs_fcall *fcall = NULL;
840         struct v9fs_wstat wstat;
841         int res = -EPERM;
842
843         dprintk(DEBUG_VFS, "\n");
844
845         if (!fid) {
846                 dprintk(DEBUG_ERROR,
847                         "Couldn't find fid associated with dentry\n");
848                 return -EBADF;
849         }
850
851         v9fs_blank_wstat(&wstat);
852         if (iattr->ia_valid & ATTR_MODE)
853                 wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
854
855         if (iattr->ia_valid & ATTR_MTIME)
856                 wstat.mtime = iattr->ia_mtime.tv_sec;
857
858         if (iattr->ia_valid & ATTR_ATIME)
859                 wstat.atime = iattr->ia_atime.tv_sec;
860
861         if (iattr->ia_valid & ATTR_SIZE)
862                 wstat.length = iattr->ia_size;
863
864         if (v9ses->extended) {
865                 if (iattr->ia_valid & ATTR_UID)
866                         wstat.n_uid = iattr->ia_uid;
867
868                 if (iattr->ia_valid & ATTR_GID)
869                         wstat.n_gid = iattr->ia_gid;
870         }
871
872         res = v9fs_t_wstat(v9ses, fid->fid, &wstat, &fcall);
873
874         if (res < 0)
875                 PRINT_FCALL_ERROR("wstat error", fcall);
876
877         kfree(fcall);
878         if (res >= 0)
879                 res = inode_setattr(dentry->d_inode, iattr);
880
881         return res;
882 }
883
884 /**
885  * v9fs_stat2inode - populate an inode structure with mistat info
886  * @stat: Plan 9 metadata (mistat) structure
887  * @inode: inode to populate
888  * @sb: superblock of filesystem
889  *
890  */
891
892 void
893 v9fs_stat2inode(struct v9fs_stat *stat, struct inode *inode,
894         struct super_block *sb)
895 {
896         int n;
897         char ext[32];
898         struct v9fs_session_info *v9ses = sb->s_fs_info;
899
900         inode->i_nlink = 1;
901
902         inode->i_atime.tv_sec = stat->atime;
903         inode->i_mtime.tv_sec = stat->mtime;
904         inode->i_ctime.tv_sec = stat->mtime;
905
906         inode->i_uid = v9ses->uid;
907         inode->i_gid = v9ses->gid;
908
909         if (v9ses->extended) {
910                 inode->i_uid = stat->n_uid;
911                 inode->i_gid = stat->n_gid;
912         }
913
914         inode->i_mode = p9mode2unixmode(v9ses, stat->mode);
915         if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode))) {
916                 char type = 0;
917                 int major = -1;
918                 int minor = -1;
919
920                 n = stat->extension.len;
921                 if (n > sizeof(ext)-1)
922                         n = sizeof(ext)-1;
923                 memmove(ext, stat->extension.str, n);
924                 ext[n] = 0;
925                 sscanf(ext, "%c %u %u", &type, &major, &minor);
926                 switch (type) {
927                 case 'c':
928                         inode->i_mode &= ~S_IFBLK;
929                         inode->i_mode |= S_IFCHR;
930                         break;
931                 case 'b':
932                         break;
933                 default:
934                         dprintk(DEBUG_ERROR, "Unknown special type %c (%.*s)\n",
935                                 type, stat->extension.len, stat->extension.str);
936                 };
937                 inode->i_rdev = MKDEV(major, minor);
938         } else
939                 inode->i_rdev = 0;
940
941         inode->i_size = stat->length;
942
943         inode->i_blksize = sb->s_blocksize;
944         inode->i_blocks =
945             (inode->i_size + inode->i_blksize - 1) >> sb->s_blocksize_bits;
946 }
947
948 /**
949  * v9fs_qid2ino - convert qid into inode number
950  * @qid: qid to hash
951  *
952  * BUG: potential for inode number collisions?
953  */
954
955 ino_t v9fs_qid2ino(struct v9fs_qid *qid)
956 {
957         u64 path = qid->path + 2;
958         ino_t i = 0;
959
960         if (sizeof(ino_t) == sizeof(path))
961                 memcpy(&i, &path, sizeof(ino_t));
962         else
963                 i = (ino_t) (path ^ (path >> 32));
964
965         return i;
966 }
967
968 /**
969  * v9fs_readlink - read a symlink's location (internal version)
970  * @dentry: dentry for symlink
971  * @buffer: buffer to load symlink location into
972  * @buflen: length of buffer
973  *
974  */
975
976 static int v9fs_readlink(struct dentry *dentry, char *buffer, int buflen)
977 {
978         int retval = -EPERM;
979
980         struct v9fs_fcall *fcall = NULL;
981         struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dentry->d_inode);
982         struct v9fs_fid *fid = v9fs_fid_lookup(dentry);
983
984         if (!fid) {
985                 dprintk(DEBUG_ERROR, "could not resolve fid from dentry\n");
986                 retval = -EBADF;
987                 goto FreeFcall;
988         }
989
990         if (!v9ses->extended) {
991                 retval = -EBADF;
992                 dprintk(DEBUG_ERROR, "not extended\n");
993                 goto FreeFcall;
994         }
995
996         dprintk(DEBUG_VFS, " %s\n", dentry->d_name.name);
997         retval = v9fs_t_stat(v9ses, fid->fid, &fcall);
998
999         if (retval < 0) {
1000                 dprintk(DEBUG_ERROR, "stat error\n");
1001                 goto FreeFcall;
1002         }
1003
1004         if (!fcall)
1005                 return -EIO;
1006
1007         if (!(fcall->params.rstat.stat.mode & V9FS_DMSYMLINK)) {
1008                 retval = -EINVAL;
1009                 goto FreeFcall;
1010         }
1011
1012         /* copy extension buffer into buffer */
1013         if (fcall->params.rstat.stat.extension.len < buflen)
1014                 buflen = fcall->params.rstat.stat.extension.len;
1015
1016         memcpy(buffer, fcall->params.rstat.stat.extension.str, buflen - 1);
1017         buffer[buflen-1] = 0;
1018
1019         retval = buflen;
1020
1021       FreeFcall:
1022         kfree(fcall);
1023
1024         return retval;
1025 }
1026
1027 /**
1028  * v9fs_vfs_readlink - read a symlink's location
1029  * @dentry: dentry for symlink
1030  * @buf: buffer to load symlink location into
1031  * @buflen: length of buffer
1032  *
1033  */
1034
1035 static int v9fs_vfs_readlink(struct dentry *dentry, char __user * buffer,
1036                              int buflen)
1037 {
1038         int retval;
1039         int ret;
1040         char *link = __getname();
1041
1042         if (buflen > PATH_MAX)
1043                 buflen = PATH_MAX;
1044
1045         dprintk(DEBUG_VFS, " dentry: %s (%p)\n", dentry->d_iname, dentry);
1046
1047         retval = v9fs_readlink(dentry, link, buflen);
1048
1049         if (retval > 0) {
1050                 if ((ret = copy_to_user(buffer, link, retval)) != 0) {
1051                         dprintk(DEBUG_ERROR, "problem copying to user: %d\n",
1052                                 ret);
1053                         retval = ret;
1054                 }
1055         }
1056
1057         __putname(link);
1058         return retval;
1059 }
1060
1061 /**
1062  * v9fs_vfs_follow_link - follow a symlink path
1063  * @dentry: dentry for symlink
1064  * @nd: nameidata
1065  *
1066  */
1067
1068 static void *v9fs_vfs_follow_link(struct dentry *dentry, struct nameidata *nd)
1069 {
1070         int len = 0;
1071         char *link = __getname();
1072
1073         dprintk(DEBUG_VFS, "%s n", dentry->d_name.name);
1074
1075         if (!link)
1076                 link = ERR_PTR(-ENOMEM);
1077         else {
1078                 len = v9fs_readlink(dentry, link, strlen(link));
1079
1080                 if (len < 0) {
1081                         __putname(link);
1082                         link = ERR_PTR(len);
1083                 } else
1084                         link[len] = 0;
1085         }
1086         nd_set_link(nd, link);
1087
1088         return NULL;
1089 }
1090
1091 /**
1092  * v9fs_vfs_put_link - release a symlink path
1093  * @dentry: dentry for symlink
1094  * @nd: nameidata
1095  *
1096  */
1097
1098 static void v9fs_vfs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
1099 {
1100         char *s = nd_get_link(nd);
1101
1102         dprintk(DEBUG_VFS, " %s %s\n", dentry->d_name.name, s);
1103         if (!IS_ERR(s))
1104                 __putname(s);
1105 }
1106
1107 static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
1108         int mode, const char *extension)
1109 {
1110         int err;
1111         u32 fid, perm;
1112         struct v9fs_session_info *v9ses;
1113         struct v9fs_fid *dfid, *vfid;
1114         struct inode *inode;
1115         struct v9fs_fcall *fcall;
1116         struct v9fs_wstat wstat;
1117
1118         fcall = NULL;
1119         inode = NULL;
1120         vfid = NULL;
1121         v9ses = v9fs_inode2v9ses(dir);
1122         dfid = v9fs_fid_lookup(dentry->d_parent);
1123         perm = unixmode2p9mode(v9ses, mode);
1124
1125         if (!v9ses->extended) {
1126                 dprintk(DEBUG_ERROR, "not extended\n");
1127                 return -EPERM;
1128         }
1129
1130         err = v9fs_create(v9ses, dfid->fid, (char *) dentry->d_name.name,
1131                 perm, V9FS_OREAD, &fid, NULL, NULL);
1132
1133         if (err)
1134                 goto error;
1135
1136         err = v9fs_t_clunk(v9ses, fid);
1137         if (err)
1138                 goto error;
1139
1140         vfid = v9fs_clone_walk(v9ses, dfid->fid, dentry);
1141         if (IS_ERR(vfid)) {
1142                 err = PTR_ERR(vfid);
1143                 vfid = NULL;
1144                 goto error;
1145         }
1146
1147         inode = v9fs_inode_from_fid(v9ses, vfid->fid, dir->i_sb);
1148         if (IS_ERR(inode)) {
1149                 err = PTR_ERR(inode);
1150                 inode = NULL;
1151                 goto error;
1152         }
1153
1154         /* issue a Twstat */
1155         v9fs_blank_wstat(&wstat);
1156         wstat.muid = v9ses->name;
1157         wstat.extension = (char *) extension;
1158         err = v9fs_t_wstat(v9ses, vfid->fid, &wstat, &fcall);
1159         if (err < 0) {
1160                 PRINT_FCALL_ERROR("wstat error", fcall);
1161                 goto error;
1162         }
1163
1164         kfree(fcall);
1165         dentry->d_op = &v9fs_dentry_operations;
1166         d_instantiate(dentry, inode);
1167         return 0;
1168
1169 error:
1170         kfree(fcall);
1171         if (vfid)
1172                 v9fs_fid_destroy(vfid);
1173
1174         if (inode)
1175                 iput(inode);
1176
1177         return err;
1178
1179 }
1180
1181 /**
1182  * v9fs_vfs_symlink - helper function to create symlinks
1183  * @dir: directory inode containing symlink
1184  * @dentry: dentry for symlink
1185  * @symname: symlink data
1186  *
1187  * See 9P2000.u RFC for more information
1188  *
1189  */
1190
1191 static int
1192 v9fs_vfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1193 {
1194         dprintk(DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
1195                 symname);
1196
1197         return v9fs_vfs_mkspecial(dir, dentry, S_IFLNK, symname);
1198 }
1199
1200 /**
1201  * v9fs_vfs_link - create a hardlink
1202  * @old_dentry: dentry for file to link to
1203  * @dir: inode destination for new link
1204  * @dentry: dentry for link
1205  *
1206  */
1207
1208 /* XXX - lots of code dup'd from symlink and creates,
1209  * figure out a better reuse strategy
1210  */
1211
1212 static int
1213 v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
1214               struct dentry *dentry)
1215 {
1216         int retval;
1217         struct v9fs_fid *oldfid;
1218         char *name;
1219
1220         dprintk(DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
1221                 old_dentry->d_name.name);
1222
1223         oldfid = v9fs_fid_lookup(old_dentry);
1224         if (!oldfid) {
1225                 dprintk(DEBUG_ERROR, "can't find oldfid\n");
1226                 return -EPERM;
1227         }
1228
1229         name = __getname();
1230         sprintf(name, "hardlink(%d)\n", oldfid->fid);
1231         retval = v9fs_vfs_mkspecial(dir, dentry, V9FS_DMLINK, name);
1232         __putname(name);
1233
1234         return retval;
1235 }
1236
1237 /**
1238  * v9fs_vfs_mknod - create a special file
1239  * @dir: inode destination for new link
1240  * @dentry: dentry for file
1241  * @mode: mode for creation
1242  * @dev_t: device associated with special file
1243  *
1244  */
1245
1246 static int
1247 v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1248 {
1249         int retval;
1250         char *name;
1251
1252         dprintk(DEBUG_VFS, " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
1253                 dentry->d_name.name, mode, MAJOR(rdev), MINOR(rdev));
1254
1255         if (!new_valid_dev(rdev))
1256                 return -EINVAL;
1257
1258         name = __getname();
1259         /* build extension */
1260         if (S_ISBLK(mode))
1261                 sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
1262         else if (S_ISCHR(mode))
1263                 sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
1264         else if (S_ISFIFO(mode))
1265                 *name = 0;
1266         else {
1267                 __putname(name);
1268                 return -EINVAL;
1269         }
1270
1271         retval = v9fs_vfs_mkspecial(dir, dentry, mode, name);
1272         __putname(name);
1273
1274         return retval;
1275 }
1276
1277 static struct inode_operations v9fs_dir_inode_operations_ext = {
1278         .create = v9fs_vfs_create,
1279         .lookup = v9fs_vfs_lookup,
1280         .symlink = v9fs_vfs_symlink,
1281         .link = v9fs_vfs_link,
1282         .unlink = v9fs_vfs_unlink,
1283         .mkdir = v9fs_vfs_mkdir,
1284         .rmdir = v9fs_vfs_rmdir,
1285         .mknod = v9fs_vfs_mknod,
1286         .rename = v9fs_vfs_rename,
1287         .readlink = v9fs_vfs_readlink,
1288         .getattr = v9fs_vfs_getattr,
1289         .setattr = v9fs_vfs_setattr,
1290 };
1291
1292 static struct inode_operations v9fs_dir_inode_operations = {
1293         .create = v9fs_vfs_create,
1294         .lookup = v9fs_vfs_lookup,
1295         .unlink = v9fs_vfs_unlink,
1296         .mkdir = v9fs_vfs_mkdir,
1297         .rmdir = v9fs_vfs_rmdir,
1298         .mknod = v9fs_vfs_mknod,
1299         .rename = v9fs_vfs_rename,
1300         .getattr = v9fs_vfs_getattr,
1301         .setattr = v9fs_vfs_setattr,
1302 };
1303
1304 static struct inode_operations v9fs_file_inode_operations = {
1305         .getattr = v9fs_vfs_getattr,
1306         .setattr = v9fs_vfs_setattr,
1307 };
1308
1309 static struct inode_operations v9fs_symlink_inode_operations = {
1310         .readlink = v9fs_vfs_readlink,
1311         .follow_link = v9fs_vfs_follow_link,
1312         .put_link = v9fs_vfs_put_link,
1313         .getattr = v9fs_vfs_getattr,
1314         .setattr = v9fs_vfs_setattr,
1315 };