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