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