9p: Creating files with names too long should fail with ENAMETOOLONG.
[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 version 2
11  *  as published by the Free Software Foundation.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to:
20  *  Free Software Foundation
21  *  51 Franklin Street, Fifth Floor
22  *  Boston, MA  02111-1301  USA
23  *
24  */
25
26 #include <linux/module.h>
27 #include <linux/errno.h>
28 #include <linux/fs.h>
29 #include <linux/file.h>
30 #include <linux/pagemap.h>
31 #include <linux/stat.h>
32 #include <linux/string.h>
33 #include <linux/inet.h>
34 #include <linux/namei.h>
35 #include <linux/idr.h>
36 #include <linux/sched.h>
37 #include <net/9p/9p.h>
38 #include <net/9p/client.h>
39
40 #include "v9fs.h"
41 #include "v9fs_vfs.h"
42 #include "fid.h"
43 #include "cache.h"
44
45 static const struct inode_operations v9fs_dir_inode_operations;
46 static const struct inode_operations v9fs_dir_inode_operations_ext;
47 static const struct inode_operations v9fs_file_inode_operations;
48 static const 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 |= P9_DMDIR;
63         if (v9fs_proto_dotu(v9ses)) {
64                 if (S_ISLNK(mode))
65                         res |= P9_DMSYMLINK;
66                 if (v9ses->nodev == 0) {
67                         if (S_ISSOCK(mode))
68                                 res |= P9_DMSOCKET;
69                         if (S_ISFIFO(mode))
70                                 res |= P9_DMNAMEDPIPE;
71                         if (S_ISBLK(mode))
72                                 res |= P9_DMDEVICE;
73                         if (S_ISCHR(mode))
74                                 res |= P9_DMDEVICE;
75                 }
76
77                 if ((mode & S_ISUID) == S_ISUID)
78                         res |= P9_DMSETUID;
79                 if ((mode & S_ISGID) == S_ISGID)
80                         res |= P9_DMSETGID;
81                 if ((mode & S_ISVTX) == S_ISVTX)
82                         res |= P9_DMSETVTX;
83                 if ((mode & P9_DMLINK))
84                         res |= P9_DMLINK;
85         }
86
87         return res;
88 }
89
90 /**
91  * p9mode2unixmode- convert plan9 mode bits to unix mode bits
92  * @v9ses: v9fs session information
93  * @mode: mode to convert
94  *
95  */
96
97 static int p9mode2unixmode(struct v9fs_session_info *v9ses, int mode)
98 {
99         int res;
100
101         res = mode & 0777;
102
103         if ((mode & P9_DMDIR) == P9_DMDIR)
104                 res |= S_IFDIR;
105         else if ((mode & P9_DMSYMLINK) && (v9fs_proto_dotu(v9ses)))
106                 res |= S_IFLNK;
107         else if ((mode & P9_DMSOCKET) && (v9fs_proto_dotu(v9ses))
108                  && (v9ses->nodev == 0))
109                 res |= S_IFSOCK;
110         else if ((mode & P9_DMNAMEDPIPE) && (v9fs_proto_dotu(v9ses))
111                  && (v9ses->nodev == 0))
112                 res |= S_IFIFO;
113         else if ((mode & P9_DMDEVICE) && (v9fs_proto_dotu(v9ses))
114                  && (v9ses->nodev == 0))
115                 res |= S_IFBLK;
116         else
117                 res |= S_IFREG;
118
119         if (v9fs_proto_dotu(v9ses)) {
120                 if ((mode & P9_DMSETUID) == P9_DMSETUID)
121                         res |= S_ISUID;
122
123                 if ((mode & P9_DMSETGID) == P9_DMSETGID)
124                         res |= S_ISGID;
125
126                 if ((mode & P9_DMSETVTX) == P9_DMSETVTX)
127                         res |= S_ISVTX;
128         }
129
130         return res;
131 }
132
133 /**
134  * v9fs_uflags2omode- convert posix open flags to plan 9 mode bits
135  * @uflags: flags to convert
136  * @extended: if .u extensions are active
137  */
138
139 int v9fs_uflags2omode(int uflags, int extended)
140 {
141         int ret;
142
143         ret = 0;
144         switch (uflags&3) {
145         default:
146         case O_RDONLY:
147                 ret = P9_OREAD;
148                 break;
149
150         case O_WRONLY:
151                 ret = P9_OWRITE;
152                 break;
153
154         case O_RDWR:
155                 ret = P9_ORDWR;
156                 break;
157         }
158
159         if (uflags & O_TRUNC)
160                 ret |= P9_OTRUNC;
161
162         if (extended) {
163                 if (uflags & O_EXCL)
164                         ret |= P9_OEXCL;
165
166                 if (uflags & O_APPEND)
167                         ret |= P9_OAPPEND;
168         }
169
170         return ret;
171 }
172
173 /**
174  * v9fs_blank_wstat - helper function to setup a 9P stat structure
175  * @wstat: structure to initialize
176  *
177  */
178
179 void
180 v9fs_blank_wstat(struct p9_wstat *wstat)
181 {
182         wstat->type = ~0;
183         wstat->dev = ~0;
184         wstat->qid.type = ~0;
185         wstat->qid.version = ~0;
186         *((long long *)&wstat->qid.path) = ~0;
187         wstat->mode = ~0;
188         wstat->atime = ~0;
189         wstat->mtime = ~0;
190         wstat->length = ~0;
191         wstat->name = NULL;
192         wstat->uid = NULL;
193         wstat->gid = NULL;
194         wstat->muid = NULL;
195         wstat->n_uid = ~0;
196         wstat->n_gid = ~0;
197         wstat->n_muid = ~0;
198         wstat->extension = NULL;
199 }
200
201 #ifdef CONFIG_9P_FSCACHE
202 /**
203  * v9fs_alloc_inode - helper function to allocate an inode
204  * This callback is executed before setting up the inode so that we
205  * can associate a vcookie with each inode.
206  *
207  */
208
209 struct inode *v9fs_alloc_inode(struct super_block *sb)
210 {
211         struct v9fs_cookie *vcookie;
212         vcookie = (struct v9fs_cookie *)kmem_cache_alloc(vcookie_cache,
213                                                          GFP_KERNEL);
214         if (!vcookie)
215                 return NULL;
216
217         vcookie->fscache = NULL;
218         vcookie->qid = NULL;
219         spin_lock_init(&vcookie->lock);
220         return &vcookie->inode;
221 }
222
223 /**
224  * v9fs_destroy_inode - destroy an inode
225  *
226  */
227
228 void v9fs_destroy_inode(struct inode *inode)
229 {
230         kmem_cache_free(vcookie_cache, v9fs_inode2cookie(inode));
231 }
232 #endif
233
234 /**
235  * v9fs_get_inode - helper function to setup an inode
236  * @sb: superblock
237  * @mode: mode to setup inode with
238  *
239  */
240
241 struct inode *v9fs_get_inode(struct super_block *sb, int mode)
242 {
243         int err;
244         struct inode *inode;
245         struct v9fs_session_info *v9ses = sb->s_fs_info;
246
247         P9_DPRINTK(P9_DEBUG_VFS, "super block: %p mode: %o\n", sb, mode);
248
249         inode = new_inode(sb);
250         if (!inode) {
251                 P9_EPRINTK(KERN_WARNING, "Problem allocating inode\n");
252                 return ERR_PTR(-ENOMEM);
253         }
254
255         inode->i_mode = mode;
256         inode->i_uid = current_fsuid();
257         inode->i_gid = current_fsgid();
258         inode->i_blocks = 0;
259         inode->i_rdev = 0;
260         inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
261         inode->i_mapping->a_ops = &v9fs_addr_operations;
262
263         switch (mode & S_IFMT) {
264         case S_IFIFO:
265         case S_IFBLK:
266         case S_IFCHR:
267         case S_IFSOCK:
268                 if (!v9fs_proto_dotu(v9ses)) {
269                         P9_DPRINTK(P9_DEBUG_ERROR,
270                                    "special files without extended mode\n");
271                         err = -EINVAL;
272                         goto error;
273                 }
274                 init_special_inode(inode, inode->i_mode, inode->i_rdev);
275                 break;
276         case S_IFREG:
277                 inode->i_op = &v9fs_file_inode_operations;
278                 inode->i_fop = &v9fs_file_operations;
279                 break;
280         case S_IFLNK:
281                 if (!v9fs_proto_dotu(v9ses)) {
282                         P9_DPRINTK(P9_DEBUG_ERROR,
283                                    "extended modes used w/o 9P2000.u\n");
284                         err = -EINVAL;
285                         goto error;
286                 }
287                 inode->i_op = &v9fs_symlink_inode_operations;
288                 break;
289         case S_IFDIR:
290                 inc_nlink(inode);
291                 if (v9fs_proto_dotu(v9ses))
292                         inode->i_op = &v9fs_dir_inode_operations_ext;
293                 else
294                         inode->i_op = &v9fs_dir_inode_operations;
295                 inode->i_fop = &v9fs_dir_operations;
296                 break;
297         default:
298                 P9_DPRINTK(P9_DEBUG_ERROR, "BAD mode 0x%x S_IFMT 0x%x\n",
299                            mode, mode & S_IFMT);
300                 err = -EINVAL;
301                 goto error;
302         }
303
304         return inode;
305
306 error:
307         iput(inode);
308         return ERR_PTR(err);
309 }
310
311 /*
312 static struct v9fs_fid*
313 v9fs_clone_walk(struct v9fs_session_info *v9ses, u32 fid, struct dentry *dentry)
314 {
315         int err;
316         int nfid;
317         struct v9fs_fid *ret;
318         struct v9fs_fcall *fcall;
319
320         nfid = v9fs_get_idpool(&v9ses->fidpool);
321         if (nfid < 0) {
322                 eprintk(KERN_WARNING, "no free fids available\n");
323                 return ERR_PTR(-ENOSPC);
324         }
325
326         err = v9fs_t_walk(v9ses, fid, nfid, (char *) dentry->d_name.name,
327                 &fcall);
328
329         if (err < 0) {
330                 if (fcall && fcall->id == RWALK)
331                         goto clunk_fid;
332
333                 PRINT_FCALL_ERROR("walk error", fcall);
334                 v9fs_put_idpool(nfid, &v9ses->fidpool);
335                 goto error;
336         }
337
338         kfree(fcall);
339         fcall = NULL;
340         ret = v9fs_fid_create(v9ses, nfid);
341         if (!ret) {
342                 err = -ENOMEM;
343                 goto clunk_fid;
344         }
345
346         err = v9fs_fid_insert(ret, dentry);
347         if (err < 0) {
348                 v9fs_fid_destroy(ret);
349                 goto clunk_fid;
350         }
351
352         return ret;
353
354 clunk_fid:
355         v9fs_t_clunk(v9ses, nfid);
356
357 error:
358         kfree(fcall);
359         return ERR_PTR(err);
360 }
361 */
362
363
364 /**
365  * v9fs_clear_inode - release an inode
366  * @inode: inode to release
367  *
368  */
369 void v9fs_clear_inode(struct inode *inode)
370 {
371         filemap_fdatawrite(inode->i_mapping);
372
373 #ifdef CONFIG_9P_FSCACHE
374         v9fs_cache_inode_put_cookie(inode);
375 #endif
376 }
377
378 /**
379  * v9fs_inode_from_fid - populate an inode by issuing a attribute request
380  * @v9ses: session information
381  * @fid: fid to issue attribute request for
382  * @sb: superblock on which to create inode
383  *
384  */
385
386 static struct inode *
387 v9fs_inode_from_fid(struct v9fs_session_info *v9ses, struct p9_fid *fid,
388         struct super_block *sb)
389 {
390         int err, umode;
391         struct inode *ret;
392         struct p9_wstat *st;
393
394         ret = NULL;
395         st = p9_client_stat(fid);
396         if (IS_ERR(st))
397                 return ERR_CAST(st);
398
399         umode = p9mode2unixmode(v9ses, st->mode);
400         ret = v9fs_get_inode(sb, umode);
401         if (IS_ERR(ret)) {
402                 err = PTR_ERR(ret);
403                 goto error;
404         }
405
406         v9fs_stat2inode(st, ret, sb);
407         ret->i_ino = v9fs_qid2ino(&st->qid);
408
409 #ifdef CONFIG_9P_FSCACHE
410         v9fs_vcookie_set_qid(ret, &st->qid);
411         v9fs_cache_inode_get_cookie(ret);
412 #endif
413         p9stat_free(st);
414         kfree(st);
415
416         return ret;
417
418 error:
419         p9stat_free(st);
420         kfree(st);
421         return ERR_PTR(err);
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         int retval;
435         struct inode *file_inode;
436         struct v9fs_session_info *v9ses;
437         struct p9_fid *v9fid;
438
439         P9_DPRINTK(P9_DEBUG_VFS, "inode: %p dentry: %p rmdir: %d\n", dir, file,
440                 rmdir);
441
442         file_inode = file->d_inode;
443         v9ses = v9fs_inode2v9ses(file_inode);
444         v9fid = v9fs_fid_clone(file);
445         if (IS_ERR(v9fid))
446                 return PTR_ERR(v9fid);
447
448         retval = p9_client_remove(v9fid);
449         if (!retval)
450                 drop_nlink(file_inode);
451         return retval;
452 }
453
454 static int
455 v9fs_open_created(struct inode *inode, struct file *file)
456 {
457         return 0;
458 }
459
460
461 /**
462  * v9fs_create - Create a file
463  * @v9ses: session information
464  * @dir: directory that dentry is being created in
465  * @dentry:  dentry that is being created
466  * @extension: 9p2000.u extension string to support devices, etc.
467  * @perm: create permissions
468  * @mode: open mode
469  *
470  */
471 static struct p9_fid *
472 v9fs_create(struct v9fs_session_info *v9ses, struct inode *dir,
473                 struct dentry *dentry, char *extension, u32 perm, u8 mode)
474 {
475         int err;
476         char *name;
477         struct p9_fid *dfid, *ofid, *fid;
478         struct inode *inode;
479
480         P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
481
482         err = 0;
483         ofid = NULL;
484         fid = NULL;
485         name = (char *) dentry->d_name.name;
486         dfid = v9fs_fid_clone(dentry->d_parent);
487         if (IS_ERR(dfid)) {
488                 err = PTR_ERR(dfid);
489                 P9_DPRINTK(P9_DEBUG_VFS, "fid clone failed %d\n", err);
490                 dfid = NULL;
491                 goto error;
492         }
493
494         /* clone a fid to use for creation */
495         ofid = p9_client_walk(dfid, 0, NULL, 1);
496         if (IS_ERR(ofid)) {
497                 err = PTR_ERR(ofid);
498                 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
499                 ofid = NULL;
500                 goto error;
501         }
502
503         err = p9_client_fcreate(ofid, name, perm, mode, extension);
504         if (err < 0) {
505                 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_fcreate failed %d\n", err);
506                 goto error;
507         }
508
509         /* now walk from the parent so we can get unopened fid */
510         fid = p9_client_walk(dfid, 1, &name, 0);
511         if (IS_ERR(fid)) {
512                 err = PTR_ERR(fid);
513                 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
514                 fid = NULL;
515                 goto error;
516         } else
517                 dfid = NULL;
518
519         /* instantiate inode and assign the unopened fid to the dentry */
520         inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
521         if (IS_ERR(inode)) {
522                 err = PTR_ERR(inode);
523                 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n", err);
524                 goto error;
525         }
526
527         if (v9ses->cache)
528                 dentry->d_op = &v9fs_cached_dentry_operations;
529         else
530                 dentry->d_op = &v9fs_dentry_operations;
531
532         d_instantiate(dentry, inode);
533         err = v9fs_fid_add(dentry, fid);
534         if (err < 0)
535                 goto error;
536
537         return ofid;
538
539 error:
540         if (dfid)
541                 p9_client_clunk(dfid);
542
543         if (ofid)
544                 p9_client_clunk(ofid);
545
546         if (fid)
547                 p9_client_clunk(fid);
548
549         return ERR_PTR(err);
550 }
551
552 /**
553  * v9fs_vfs_create - VFS hook to create files
554  * @dir: directory inode that is being created
555  * @dentry:  dentry that is being deleted
556  * @mode: create permissions
557  * @nd: path information
558  *
559  */
560
561 static int
562 v9fs_vfs_create(struct inode *dir, struct dentry *dentry, int mode,
563                 struct nameidata *nd)
564 {
565         int err;
566         u32 perm;
567         int flags;
568         struct v9fs_session_info *v9ses;
569         struct p9_fid *fid;
570         struct file *filp;
571
572         err = 0;
573         fid = NULL;
574         v9ses = v9fs_inode2v9ses(dir);
575         perm = unixmode2p9mode(v9ses, mode);
576         if (nd && nd->flags & LOOKUP_OPEN)
577                 flags = nd->intent.open.flags - 1;
578         else
579                 flags = O_RDWR;
580
581         fid = v9fs_create(v9ses, dir, dentry, NULL, perm,
582                                 v9fs_uflags2omode(flags,
583                                                 v9fs_proto_dotu(v9ses)));
584         if (IS_ERR(fid)) {
585                 err = PTR_ERR(fid);
586                 fid = NULL;
587                 goto error;
588         }
589
590         /* if we are opening a file, assign the open fid to the file */
591         if (nd && nd->flags & LOOKUP_OPEN) {
592                 filp = lookup_instantiate_filp(nd, dentry, v9fs_open_created);
593                 if (IS_ERR(filp)) {
594                         err = PTR_ERR(filp);
595                         goto error;
596                 }
597
598                 filp->private_data = fid;
599         } else
600                 p9_client_clunk(fid);
601
602         return 0;
603
604 error:
605         if (fid)
606                 p9_client_clunk(fid);
607
608         return err;
609 }
610
611 /**
612  * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
613  * @dir:  inode that is being unlinked
614  * @dentry: dentry that is being unlinked
615  * @mode: mode for new directory
616  *
617  */
618
619 static int v9fs_vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
620 {
621         int err;
622         u32 perm;
623         struct v9fs_session_info *v9ses;
624         struct p9_fid *fid;
625
626         P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
627         err = 0;
628         v9ses = v9fs_inode2v9ses(dir);
629         perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
630         fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_OREAD);
631         if (IS_ERR(fid)) {
632                 err = PTR_ERR(fid);
633                 fid = NULL;
634         }
635
636         if (fid)
637                 p9_client_clunk(fid);
638
639         return err;
640 }
641
642 /**
643  * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
644  * @dir:  inode that is being walked from
645  * @dentry: dentry that is being walked to?
646  * @nameidata: path data
647  *
648  */
649
650 static struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
651                                       struct nameidata *nameidata)
652 {
653         struct super_block *sb;
654         struct v9fs_session_info *v9ses;
655         struct p9_fid *dfid, *fid;
656         struct inode *inode;
657         char *name;
658         int result = 0;
659
660         P9_DPRINTK(P9_DEBUG_VFS, "dir: %p dentry: (%s) %p nameidata: %p\n",
661                 dir, dentry->d_name.name, dentry, nameidata);
662
663         if (dentry->d_name.len > NAME_MAX)
664                 return ERR_PTR(-ENAMETOOLONG);
665
666         sb = dir->i_sb;
667         v9ses = v9fs_inode2v9ses(dir);
668         dfid = v9fs_fid_lookup(dentry->d_parent);
669         if (IS_ERR(dfid))
670                 return ERR_CAST(dfid);
671
672         name = (char *) dentry->d_name.name;
673         fid = p9_client_walk(dfid, 1, &name, 1);
674         if (IS_ERR(fid)) {
675                 result = PTR_ERR(fid);
676                 if (result == -ENOENT) {
677                         d_add(dentry, NULL);
678                         return NULL;
679                 }
680
681                 return ERR_PTR(result);
682         }
683
684         inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
685         if (IS_ERR(inode)) {
686                 result = PTR_ERR(inode);
687                 inode = NULL;
688                 goto error;
689         }
690
691         result = v9fs_fid_add(dentry, fid);
692         if (result < 0)
693                 goto error;
694
695         if ((fid->qid.version) && (v9ses->cache))
696                 dentry->d_op = &v9fs_cached_dentry_operations;
697         else
698                 dentry->d_op = &v9fs_dentry_operations;
699
700         d_add(dentry, inode);
701         return NULL;
702
703 error:
704         p9_client_clunk(fid);
705
706         return ERR_PTR(result);
707 }
708
709 /**
710  * v9fs_vfs_unlink - VFS unlink hook to delete an inode
711  * @i:  inode that is being unlinked
712  * @d: dentry that is being unlinked
713  *
714  */
715
716 static int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
717 {
718         return v9fs_remove(i, d, 0);
719 }
720
721 /**
722  * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
723  * @i:  inode that is being unlinked
724  * @d: dentry that is being unlinked
725  *
726  */
727
728 static int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
729 {
730         return v9fs_remove(i, d, 1);
731 }
732
733 /**
734  * v9fs_vfs_rename - VFS hook to rename an inode
735  * @old_dir:  old dir inode
736  * @old_dentry: old dentry
737  * @new_dir: new dir inode
738  * @new_dentry: new dentry
739  *
740  */
741
742 static int
743 v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
744                 struct inode *new_dir, struct dentry *new_dentry)
745 {
746         struct inode *old_inode;
747         struct v9fs_session_info *v9ses;
748         struct p9_fid *oldfid;
749         struct p9_fid *olddirfid;
750         struct p9_fid *newdirfid;
751         struct p9_wstat wstat;
752         int retval;
753
754         P9_DPRINTK(P9_DEBUG_VFS, "\n");
755         retval = 0;
756         old_inode = old_dentry->d_inode;
757         v9ses = v9fs_inode2v9ses(old_inode);
758         oldfid = v9fs_fid_lookup(old_dentry);
759         if (IS_ERR(oldfid))
760                 return PTR_ERR(oldfid);
761
762         olddirfid = v9fs_fid_clone(old_dentry->d_parent);
763         if (IS_ERR(olddirfid)) {
764                 retval = PTR_ERR(olddirfid);
765                 goto done;
766         }
767
768         newdirfid = v9fs_fid_clone(new_dentry->d_parent);
769         if (IS_ERR(newdirfid)) {
770                 retval = PTR_ERR(newdirfid);
771                 goto clunk_olddir;
772         }
773
774         /* 9P can only handle file rename in the same directory */
775         if (memcmp(&olddirfid->qid, &newdirfid->qid, sizeof(newdirfid->qid))) {
776                 P9_DPRINTK(P9_DEBUG_ERROR,
777                                 "old dir and new dir are different\n");
778                 retval = -EXDEV;
779                 goto clunk_newdir;
780         }
781
782         v9fs_blank_wstat(&wstat);
783         wstat.muid = v9ses->uname;
784         wstat.name = (char *) new_dentry->d_name.name;
785         retval = p9_client_wstat(oldfid, &wstat);
786
787 clunk_newdir:
788         p9_client_clunk(newdirfid);
789
790 clunk_olddir:
791         p9_client_clunk(olddirfid);
792
793 done:
794         return retval;
795 }
796
797 /**
798  * v9fs_vfs_getattr - retrieve file metadata
799  * @mnt: mount information
800  * @dentry: file to get attributes on
801  * @stat: metadata structure to populate
802  *
803  */
804
805 static int
806 v9fs_vfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
807                  struct kstat *stat)
808 {
809         int err;
810         struct v9fs_session_info *v9ses;
811         struct p9_fid *fid;
812         struct p9_wstat *st;
813
814         P9_DPRINTK(P9_DEBUG_VFS, "dentry: %p\n", dentry);
815         err = -EPERM;
816         v9ses = v9fs_inode2v9ses(dentry->d_inode);
817         if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
818                 return simple_getattr(mnt, dentry, stat);
819
820         fid = v9fs_fid_lookup(dentry);
821         if (IS_ERR(fid))
822                 return PTR_ERR(fid);
823
824         st = p9_client_stat(fid);
825         if (IS_ERR(st))
826                 return PTR_ERR(st);
827
828         v9fs_stat2inode(st, dentry->d_inode, dentry->d_inode->i_sb);
829                 generic_fillattr(dentry->d_inode, stat);
830
831         kfree(st);
832         return 0;
833 }
834
835 /**
836  * v9fs_vfs_setattr - set file metadata
837  * @dentry: file whose metadata to set
838  * @iattr: metadata assignment structure
839  *
840  */
841
842 static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
843 {
844         int retval;
845         struct v9fs_session_info *v9ses;
846         struct p9_fid *fid;
847         struct p9_wstat wstat;
848
849         P9_DPRINTK(P9_DEBUG_VFS, "\n");
850         retval = -EPERM;
851         v9ses = v9fs_inode2v9ses(dentry->d_inode);
852         fid = v9fs_fid_lookup(dentry);
853         if(IS_ERR(fid))
854                 return PTR_ERR(fid);
855
856         v9fs_blank_wstat(&wstat);
857         if (iattr->ia_valid & ATTR_MODE)
858                 wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
859
860         if (iattr->ia_valid & ATTR_MTIME)
861                 wstat.mtime = iattr->ia_mtime.tv_sec;
862
863         if (iattr->ia_valid & ATTR_ATIME)
864                 wstat.atime = iattr->ia_atime.tv_sec;
865
866         if (iattr->ia_valid & ATTR_SIZE)
867                 wstat.length = iattr->ia_size;
868
869         if (v9fs_proto_dotu(v9ses)) {
870                 if (iattr->ia_valid & ATTR_UID)
871                         wstat.n_uid = iattr->ia_uid;
872
873                 if (iattr->ia_valid & ATTR_GID)
874                         wstat.n_gid = iattr->ia_gid;
875         }
876
877         retval = p9_client_wstat(fid, &wstat);
878         if (retval >= 0)
879                 retval = inode_setattr(dentry->d_inode, iattr);
880
881         return retval;
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 p9_wstat *stat, struct inode *inode,
894         struct super_block *sb)
895 {
896         char ext[32];
897         char tag_name[14];
898         unsigned int i_nlink;
899         struct v9fs_session_info *v9ses = sb->s_fs_info;
900
901         inode->i_nlink = 1;
902
903         inode->i_atime.tv_sec = stat->atime;
904         inode->i_mtime.tv_sec = stat->mtime;
905         inode->i_ctime.tv_sec = stat->mtime;
906
907         inode->i_uid = v9ses->dfltuid;
908         inode->i_gid = v9ses->dfltgid;
909
910         if (v9fs_proto_dotu(v9ses)) {
911                 inode->i_uid = stat->n_uid;
912                 inode->i_gid = stat->n_gid;
913         }
914         if ((S_ISREG(inode->i_mode)) || (S_ISDIR(inode->i_mode))) {
915                 if (v9fs_proto_dotu(v9ses) && (stat->extension[0] != '\0')) {
916                         /*
917                          * Hadlink support got added later to
918                          * to the .u extension. So there can be
919                          * server out there that doesn't support
920                          * this even with .u extension. So check
921                          * for non NULL stat->extension
922                          */
923                         strncpy(ext, stat->extension, sizeof(ext));
924                         /* HARDLINKCOUNT %u */
925                         sscanf(ext, "%13s %u", tag_name, &i_nlink);
926                         if (!strncmp(tag_name, "HARDLINKCOUNT", 13))
927                                 inode->i_nlink = i_nlink;
928                 }
929         }
930         inode->i_mode = p9mode2unixmode(v9ses, stat->mode);
931         if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode))) {
932                 char type = 0;
933                 int major = -1;
934                 int minor = -1;
935
936                 strncpy(ext, stat->extension, sizeof(ext));
937                 sscanf(ext, "%c %u %u", &type, &major, &minor);
938                 switch (type) {
939                 case 'c':
940                         inode->i_mode &= ~S_IFBLK;
941                         inode->i_mode |= S_IFCHR;
942                         break;
943                 case 'b':
944                         break;
945                 default:
946                         P9_DPRINTK(P9_DEBUG_ERROR,
947                                 "Unknown special type %c %s\n", type,
948                                 stat->extension);
949                 };
950                 inode->i_rdev = MKDEV(major, minor);
951                 init_special_inode(inode, inode->i_mode, inode->i_rdev);
952         } else
953                 inode->i_rdev = 0;
954
955         i_size_write(inode, stat->length);
956
957         /* not real number of blocks, but 512 byte ones ... */
958         inode->i_blocks = (i_size_read(inode) + 512 - 1) >> 9;
959 }
960
961 /**
962  * v9fs_qid2ino - convert qid into inode number
963  * @qid: qid to hash
964  *
965  * BUG: potential for inode number collisions?
966  */
967
968 ino_t v9fs_qid2ino(struct p9_qid *qid)
969 {
970         u64 path = qid->path + 2;
971         ino_t i = 0;
972
973         if (sizeof(ino_t) == sizeof(path))
974                 memcpy(&i, &path, sizeof(ino_t));
975         else
976                 i = (ino_t) (path ^ (path >> 32));
977
978         return i;
979 }
980
981 /**
982  * v9fs_readlink - read a symlink's location (internal version)
983  * @dentry: dentry for symlink
984  * @buffer: buffer to load symlink location into
985  * @buflen: length of buffer
986  *
987  */
988
989 static int v9fs_readlink(struct dentry *dentry, char *buffer, int buflen)
990 {
991         int retval;
992
993         struct v9fs_session_info *v9ses;
994         struct p9_fid *fid;
995         struct p9_wstat *st;
996
997         P9_DPRINTK(P9_DEBUG_VFS, " %s\n", dentry->d_name.name);
998         retval = -EPERM;
999         v9ses = v9fs_inode2v9ses(dentry->d_inode);
1000         fid = v9fs_fid_lookup(dentry);
1001         if (IS_ERR(fid))
1002                 return PTR_ERR(fid);
1003
1004         if (!v9fs_proto_dotu(v9ses))
1005                 return -EBADF;
1006
1007         st = p9_client_stat(fid);
1008         if (IS_ERR(st))
1009                 return PTR_ERR(st);
1010
1011         if (!(st->mode & P9_DMSYMLINK)) {
1012                 retval = -EINVAL;
1013                 goto done;
1014         }
1015
1016         /* copy extension buffer into buffer */
1017         strncpy(buffer, st->extension, buflen);
1018
1019         P9_DPRINTK(P9_DEBUG_VFS,
1020                 "%s -> %s (%s)\n", dentry->d_name.name, st->extension, buffer);
1021
1022         retval = strnlen(buffer, buflen);
1023 done:
1024         kfree(st);
1025         return retval;
1026 }
1027
1028 /**
1029  * v9fs_vfs_follow_link - follow a symlink path
1030  * @dentry: dentry for symlink
1031  * @nd: nameidata
1032  *
1033  */
1034
1035 static void *v9fs_vfs_follow_link(struct dentry *dentry, struct nameidata *nd)
1036 {
1037         int len = 0;
1038         char *link = __getname();
1039
1040         P9_DPRINTK(P9_DEBUG_VFS, "%s n", dentry->d_name.name);
1041
1042         if (!link)
1043                 link = ERR_PTR(-ENOMEM);
1044         else {
1045                 len = v9fs_readlink(dentry, link, PATH_MAX);
1046
1047                 if (len < 0) {
1048                         __putname(link);
1049                         link = ERR_PTR(len);
1050                 } else
1051                         link[min(len, PATH_MAX-1)] = 0;
1052         }
1053         nd_set_link(nd, link);
1054
1055         return NULL;
1056 }
1057
1058 /**
1059  * v9fs_vfs_put_link - release a symlink path
1060  * @dentry: dentry for symlink
1061  * @nd: nameidata
1062  * @p: unused
1063  *
1064  */
1065
1066 static void
1067 v9fs_vfs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
1068 {
1069         char *s = nd_get_link(nd);
1070
1071         P9_DPRINTK(P9_DEBUG_VFS, " %s %s\n", dentry->d_name.name,
1072                 IS_ERR(s) ? "<error>" : s);
1073         if (!IS_ERR(s))
1074                 __putname(s);
1075 }
1076
1077 /**
1078  * v9fs_vfs_mkspecial - create a special file
1079  * @dir: inode to create special file in
1080  * @dentry: dentry to create
1081  * @mode: mode to create special file
1082  * @extension: 9p2000.u format extension string representing special file
1083  *
1084  */
1085
1086 static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
1087         int mode, const char *extension)
1088 {
1089         u32 perm;
1090         struct v9fs_session_info *v9ses;
1091         struct p9_fid *fid;
1092
1093         v9ses = v9fs_inode2v9ses(dir);
1094         if (!v9fs_proto_dotu(v9ses)) {
1095                 P9_DPRINTK(P9_DEBUG_ERROR, "not extended\n");
1096                 return -EPERM;
1097         }
1098
1099         perm = unixmode2p9mode(v9ses, mode);
1100         fid = v9fs_create(v9ses, dir, dentry, (char *) extension, perm,
1101                                                                 P9_OREAD);
1102         if (IS_ERR(fid))
1103                 return PTR_ERR(fid);
1104
1105         p9_client_clunk(fid);
1106         return 0;
1107 }
1108
1109 /**
1110  * v9fs_vfs_symlink - helper function to create symlinks
1111  * @dir: directory inode containing symlink
1112  * @dentry: dentry for symlink
1113  * @symname: symlink data
1114  *
1115  * See Also: 9P2000.u RFC for more information
1116  *
1117  */
1118
1119 static int
1120 v9fs_vfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1121 {
1122         P9_DPRINTK(P9_DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino,
1123                                         dentry->d_name.name, symname);
1124
1125         return v9fs_vfs_mkspecial(dir, dentry, S_IFLNK, symname);
1126 }
1127
1128 /**
1129  * v9fs_vfs_link - create a hardlink
1130  * @old_dentry: dentry for file to link to
1131  * @dir: inode destination for new link
1132  * @dentry: dentry for link
1133  *
1134  */
1135
1136 static int
1137 v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
1138               struct dentry *dentry)
1139 {
1140         int retval;
1141         struct p9_fid *oldfid;
1142         char *name;
1143
1144         P9_DPRINTK(P9_DEBUG_VFS,
1145                 " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
1146                 old_dentry->d_name.name);
1147
1148         oldfid = v9fs_fid_clone(old_dentry);
1149         if (IS_ERR(oldfid))
1150                 return PTR_ERR(oldfid);
1151
1152         name = __getname();
1153         if (unlikely(!name)) {
1154                 retval = -ENOMEM;
1155                 goto clunk_fid;
1156         }
1157
1158         sprintf(name, "%d\n", oldfid->fid);
1159         retval = v9fs_vfs_mkspecial(dir, dentry, P9_DMLINK, name);
1160         __putname(name);
1161
1162 clunk_fid:
1163         p9_client_clunk(oldfid);
1164         return retval;
1165 }
1166
1167 /**
1168  * v9fs_vfs_mknod - create a special file
1169  * @dir: inode destination for new link
1170  * @dentry: dentry for file
1171  * @mode: mode for creation
1172  * @rdev: device associated with special file
1173  *
1174  */
1175
1176 static int
1177 v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1178 {
1179         int retval;
1180         char *name;
1181
1182         P9_DPRINTK(P9_DEBUG_VFS,
1183                 " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
1184                 dentry->d_name.name, mode, MAJOR(rdev), MINOR(rdev));
1185
1186         if (!new_valid_dev(rdev))
1187                 return -EINVAL;
1188
1189         name = __getname();
1190         if (!name)
1191                 return -ENOMEM;
1192         /* build extension */
1193         if (S_ISBLK(mode))
1194                 sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
1195         else if (S_ISCHR(mode))
1196                 sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
1197         else if (S_ISFIFO(mode))
1198                 *name = 0;
1199         else {
1200                 __putname(name);
1201                 return -EINVAL;
1202         }
1203
1204         retval = v9fs_vfs_mkspecial(dir, dentry, mode, name);
1205         __putname(name);
1206
1207         return retval;
1208 }
1209
1210 static const struct inode_operations v9fs_dir_inode_operations_ext = {
1211         .create = v9fs_vfs_create,
1212         .lookup = v9fs_vfs_lookup,
1213         .symlink = v9fs_vfs_symlink,
1214         .link = v9fs_vfs_link,
1215         .unlink = v9fs_vfs_unlink,
1216         .mkdir = v9fs_vfs_mkdir,
1217         .rmdir = v9fs_vfs_rmdir,
1218         .mknod = v9fs_vfs_mknod,
1219         .rename = v9fs_vfs_rename,
1220         .getattr = v9fs_vfs_getattr,
1221         .setattr = v9fs_vfs_setattr,
1222 };
1223
1224 static const struct inode_operations v9fs_dir_inode_operations = {
1225         .create = v9fs_vfs_create,
1226         .lookup = v9fs_vfs_lookup,
1227         .unlink = v9fs_vfs_unlink,
1228         .mkdir = v9fs_vfs_mkdir,
1229         .rmdir = v9fs_vfs_rmdir,
1230         .mknod = v9fs_vfs_mknod,
1231         .rename = v9fs_vfs_rename,
1232         .getattr = v9fs_vfs_getattr,
1233         .setattr = v9fs_vfs_setattr,
1234 };
1235
1236 static const struct inode_operations v9fs_file_inode_operations = {
1237         .getattr = v9fs_vfs_getattr,
1238         .setattr = v9fs_vfs_setattr,
1239 };
1240
1241 static const struct inode_operations v9fs_symlink_inode_operations = {
1242         .readlink = generic_readlink,
1243         .follow_link = v9fs_vfs_follow_link,
1244         .put_link = v9fs_vfs_put_link,
1245         .getattr = v9fs_vfs_getattr,
1246         .setattr = v9fs_vfs_setattr,
1247 };