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