no need for list_for_each_entry_safe()/resetting with superblock list
[cascardo/linux.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_init_owner(inode, NULL, mode);
260         inode->i_blocks = 0;
261         inode->i_rdev = 0;
262         inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
263         inode->i_mapping->a_ops = &v9fs_addr_operations;
264
265         switch (mode & S_IFMT) {
266         case S_IFIFO:
267         case S_IFBLK:
268         case S_IFCHR:
269         case S_IFSOCK:
270                 if (!v9fs_proto_dotu(v9ses)) {
271                         P9_DPRINTK(P9_DEBUG_ERROR,
272                                    "special files without extended mode\n");
273                         err = -EINVAL;
274                         goto error;
275                 }
276                 init_special_inode(inode, inode->i_mode, inode->i_rdev);
277                 break;
278         case S_IFREG:
279                 if (v9fs_proto_dotl(v9ses)) {
280                         inode->i_op = &v9fs_file_inode_operations_dotl;
281                         inode->i_fop = &v9fs_file_operations_dotl;
282                 } else {
283                         inode->i_op = &v9fs_file_inode_operations;
284                         inode->i_fop = &v9fs_file_operations;
285                 }
286
287                 break;
288
289         case S_IFLNK:
290                 if (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)) {
291                         P9_DPRINTK(P9_DEBUG_ERROR, "extended modes used with "
292                                                 "legacy protocol.\n");
293                         err = -EINVAL;
294                         goto error;
295                 }
296
297                 if (v9fs_proto_dotl(v9ses))
298                         inode->i_op = &v9fs_symlink_inode_operations_dotl;
299                 else
300                         inode->i_op = &v9fs_symlink_inode_operations;
301
302                 break;
303         case S_IFDIR:
304                 inc_nlink(inode);
305                 if (v9fs_proto_dotl(v9ses))
306                         inode->i_op = &v9fs_dir_inode_operations_dotl;
307                 else if (v9fs_proto_dotu(v9ses))
308                         inode->i_op = &v9fs_dir_inode_operations_dotu;
309                 else
310                         inode->i_op = &v9fs_dir_inode_operations;
311
312                 if (v9fs_proto_dotl(v9ses))
313                         inode->i_fop = &v9fs_dir_operations_dotl;
314                 else
315                         inode->i_fop = &v9fs_dir_operations;
316
317                 break;
318         default:
319                 P9_DPRINTK(P9_DEBUG_ERROR, "BAD mode 0x%x S_IFMT 0x%x\n",
320                            mode, mode & S_IFMT);
321                 err = -EINVAL;
322                 goto error;
323         }
324
325         return inode;
326
327 error:
328         iput(inode);
329         return ERR_PTR(err);
330 }
331
332 /*
333 static struct v9fs_fid*
334 v9fs_clone_walk(struct v9fs_session_info *v9ses, u32 fid, struct dentry *dentry)
335 {
336         int err;
337         int nfid;
338         struct v9fs_fid *ret;
339         struct v9fs_fcall *fcall;
340
341         nfid = v9fs_get_idpool(&v9ses->fidpool);
342         if (nfid < 0) {
343                 eprintk(KERN_WARNING, "no free fids available\n");
344                 return ERR_PTR(-ENOSPC);
345         }
346
347         err = v9fs_t_walk(v9ses, fid, nfid, (char *) dentry->d_name.name,
348                 &fcall);
349
350         if (err < 0) {
351                 if (fcall && fcall->id == RWALK)
352                         goto clunk_fid;
353
354                 PRINT_FCALL_ERROR("walk error", fcall);
355                 v9fs_put_idpool(nfid, &v9ses->fidpool);
356                 goto error;
357         }
358
359         kfree(fcall);
360         fcall = NULL;
361         ret = v9fs_fid_create(v9ses, nfid);
362         if (!ret) {
363                 err = -ENOMEM;
364                 goto clunk_fid;
365         }
366
367         err = v9fs_fid_insert(ret, dentry);
368         if (err < 0) {
369                 v9fs_fid_destroy(ret);
370                 goto clunk_fid;
371         }
372
373         return ret;
374
375 clunk_fid:
376         v9fs_t_clunk(v9ses, nfid);
377
378 error:
379         kfree(fcall);
380         return ERR_PTR(err);
381 }
382 */
383
384
385 /**
386  * v9fs_clear_inode - release an inode
387  * @inode: inode to release
388  *
389  */
390 void v9fs_evict_inode(struct inode *inode)
391 {
392         truncate_inode_pages(inode->i_mapping, 0);
393         end_writeback(inode);
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 p9_fid *v9fid;
460
461         P9_DPRINTK(P9_DEBUG_VFS, "inode: %p dentry: %p rmdir: %d\n", dir, file,
462                 rmdir);
463
464         file_inode = file->d_inode;
465         v9fid = v9fs_fid_clone(file);
466         if (IS_ERR(v9fid))
467                 return PTR_ERR(v9fid);
468
469         retval = p9_client_remove(v9fid);
470         if (!retval)
471                 drop_nlink(file_inode);
472         return retval;
473 }
474
475 static int
476 v9fs_open_created(struct inode *inode, struct file *file)
477 {
478         return 0;
479 }
480
481
482 /**
483  * v9fs_create - Create a file
484  * @v9ses: session information
485  * @dir: directory that dentry is being created in
486  * @dentry:  dentry that is being created
487  * @extension: 9p2000.u extension string to support devices, etc.
488  * @perm: create permissions
489  * @mode: open mode
490  *
491  */
492 static struct p9_fid *
493 v9fs_create(struct v9fs_session_info *v9ses, struct inode *dir,
494                 struct dentry *dentry, char *extension, u32 perm, u8 mode)
495 {
496         int err;
497         char *name;
498         struct p9_fid *dfid, *ofid, *fid;
499         struct inode *inode;
500
501         P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
502
503         err = 0;
504         ofid = NULL;
505         fid = NULL;
506         name = (char *) dentry->d_name.name;
507         dfid = v9fs_fid_lookup(dentry->d_parent);
508         if (IS_ERR(dfid)) {
509                 err = PTR_ERR(dfid);
510                 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
511                 return ERR_PTR(err);
512         }
513
514         /* clone a fid to use for creation */
515         ofid = p9_client_walk(dfid, 0, NULL, 1);
516         if (IS_ERR(ofid)) {
517                 err = PTR_ERR(ofid);
518                 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
519                 return ERR_PTR(err);
520         }
521
522         err = p9_client_fcreate(ofid, name, perm, mode, extension);
523         if (err < 0) {
524                 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_fcreate failed %d\n", err);
525                 goto error;
526         }
527
528         /* now walk from the parent so we can get unopened fid */
529         fid = p9_client_walk(dfid, 1, &name, 1);
530         if (IS_ERR(fid)) {
531                 err = PTR_ERR(fid);
532                 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
533                 fid = NULL;
534                 goto error;
535         }
536
537         /* instantiate inode and assign the unopened fid to the dentry */
538         inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
539         if (IS_ERR(inode)) {
540                 err = PTR_ERR(inode);
541                 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n", err);
542                 goto error;
543         }
544
545         if (v9ses->cache)
546                 dentry->d_op = &v9fs_cached_dentry_operations;
547         else
548                 dentry->d_op = &v9fs_dentry_operations;
549
550         d_instantiate(dentry, inode);
551         err = v9fs_fid_add(dentry, fid);
552         if (err < 0)
553                 goto error;
554
555         return ofid;
556
557 error:
558         if (ofid)
559                 p9_client_clunk(ofid);
560
561         if (fid)
562                 p9_client_clunk(fid);
563
564         return ERR_PTR(err);
565 }
566
567 /**
568  * v9fs_vfs_create - VFS hook to create files
569  * @dir: directory inode that is being created
570  * @dentry:  dentry that is being deleted
571  * @mode: create permissions
572  * @nd: path information
573  *
574  */
575
576 static int
577 v9fs_vfs_create(struct inode *dir, struct dentry *dentry, int mode,
578                 struct nameidata *nd)
579 {
580         int err;
581         u32 perm;
582         int flags;
583         struct v9fs_session_info *v9ses;
584         struct p9_fid *fid;
585         struct file *filp;
586
587         err = 0;
588         fid = NULL;
589         v9ses = v9fs_inode2v9ses(dir);
590         perm = unixmode2p9mode(v9ses, mode);
591         if (nd && nd->flags & LOOKUP_OPEN)
592                 flags = nd->intent.open.flags - 1;
593         else
594                 flags = O_RDWR;
595
596         fid = v9fs_create(v9ses, dir, dentry, NULL, perm,
597                                 v9fs_uflags2omode(flags,
598                                                 v9fs_proto_dotu(v9ses)));
599         if (IS_ERR(fid)) {
600                 err = PTR_ERR(fid);
601                 fid = NULL;
602                 goto error;
603         }
604
605         /* if we are opening a file, assign the open fid to the file */
606         if (nd && nd->flags & LOOKUP_OPEN) {
607                 filp = lookup_instantiate_filp(nd, dentry, v9fs_open_created);
608                 if (IS_ERR(filp)) {
609                         err = PTR_ERR(filp);
610                         goto error;
611                 }
612
613                 filp->private_data = fid;
614         } else
615                 p9_client_clunk(fid);
616
617         return 0;
618
619 error:
620         if (fid)
621                 p9_client_clunk(fid);
622
623         return err;
624 }
625
626 /**
627  * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
628  * @dir:  inode that is being unlinked
629  * @dentry: dentry that is being unlinked
630  * @mode: mode for new directory
631  *
632  */
633
634 static int v9fs_vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
635 {
636         int err;
637         u32 perm;
638         struct v9fs_session_info *v9ses;
639         struct p9_fid *fid;
640
641         P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
642         err = 0;
643         v9ses = v9fs_inode2v9ses(dir);
644         perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
645         fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_OREAD);
646         if (IS_ERR(fid)) {
647                 err = PTR_ERR(fid);
648                 fid = NULL;
649         }
650
651         if (fid)
652                 p9_client_clunk(fid);
653
654         return err;
655 }
656
657 /**
658  * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
659  * @dir:  inode that is being walked from
660  * @dentry: dentry that is being walked to?
661  * @nameidata: path data
662  *
663  */
664
665 static struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
666                                       struct nameidata *nameidata)
667 {
668         struct super_block *sb;
669         struct v9fs_session_info *v9ses;
670         struct p9_fid *dfid, *fid;
671         struct inode *inode;
672         char *name;
673         int result = 0;
674
675         P9_DPRINTK(P9_DEBUG_VFS, "dir: %p dentry: (%s) %p nameidata: %p\n",
676                 dir, dentry->d_name.name, dentry, nameidata);
677
678         if (dentry->d_name.len > NAME_MAX)
679                 return ERR_PTR(-ENAMETOOLONG);
680
681         sb = dir->i_sb;
682         v9ses = v9fs_inode2v9ses(dir);
683         dfid = v9fs_fid_lookup(dentry->d_parent);
684         if (IS_ERR(dfid))
685                 return ERR_CAST(dfid);
686
687         name = (char *) dentry->d_name.name;
688         fid = p9_client_walk(dfid, 1, &name, 1);
689         if (IS_ERR(fid)) {
690                 result = PTR_ERR(fid);
691                 if (result == -ENOENT) {
692                         inode = NULL;
693                         goto inst_out;
694                 }
695
696                 return ERR_PTR(result);
697         }
698
699         inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
700         if (IS_ERR(inode)) {
701                 result = PTR_ERR(inode);
702                 inode = NULL;
703                 goto error;
704         }
705
706         result = v9fs_fid_add(dentry, fid);
707         if (result < 0)
708                 goto error;
709
710 inst_out:
711         if (v9ses->cache)
712                 dentry->d_op = &v9fs_cached_dentry_operations;
713         else
714                 dentry->d_op = &v9fs_dentry_operations;
715
716         d_add(dentry, inode);
717         return NULL;
718
719 error:
720         p9_client_clunk(fid);
721
722         return ERR_PTR(result);
723 }
724
725 /**
726  * v9fs_vfs_unlink - VFS unlink hook to delete an inode
727  * @i:  inode that is being unlinked
728  * @d: dentry that is being unlinked
729  *
730  */
731
732 static int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
733 {
734         return v9fs_remove(i, d, 0);
735 }
736
737 /**
738  * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
739  * @i:  inode that is being unlinked
740  * @d: dentry that is being unlinked
741  *
742  */
743
744 static int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
745 {
746         return v9fs_remove(i, d, 1);
747 }
748
749 /**
750  * v9fs_vfs_rename - VFS hook to rename an inode
751  * @old_dir:  old dir inode
752  * @old_dentry: old dentry
753  * @new_dir: new dir inode
754  * @new_dentry: new dentry
755  *
756  */
757
758 static int
759 v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
760                 struct inode *new_dir, struct dentry *new_dentry)
761 {
762         struct inode *old_inode;
763         struct v9fs_session_info *v9ses;
764         struct p9_fid *oldfid;
765         struct p9_fid *olddirfid;
766         struct p9_fid *newdirfid;
767         struct p9_wstat wstat;
768         int retval;
769
770         P9_DPRINTK(P9_DEBUG_VFS, "\n");
771         retval = 0;
772         old_inode = old_dentry->d_inode;
773         v9ses = v9fs_inode2v9ses(old_inode);
774         oldfid = v9fs_fid_lookup(old_dentry);
775         if (IS_ERR(oldfid))
776                 return PTR_ERR(oldfid);
777
778         olddirfid = v9fs_fid_clone(old_dentry->d_parent);
779         if (IS_ERR(olddirfid)) {
780                 retval = PTR_ERR(olddirfid);
781                 goto done;
782         }
783
784         newdirfid = v9fs_fid_clone(new_dentry->d_parent);
785         if (IS_ERR(newdirfid)) {
786                 retval = PTR_ERR(newdirfid);
787                 goto clunk_olddir;
788         }
789
790         if (v9fs_proto_dotl(v9ses)) {
791                 retval = p9_client_rename(oldfid, newdirfid,
792                                         (char *) new_dentry->d_name.name);
793                 if (retval != -ENOSYS)
794                         goto clunk_newdir;
795         }
796
797         /* 9P can only handle file rename in the same directory */
798         if (memcmp(&olddirfid->qid, &newdirfid->qid, sizeof(newdirfid->qid))) {
799                 P9_DPRINTK(P9_DEBUG_ERROR,
800                                 "old dir and new dir are different\n");
801                 retval = -EXDEV;
802                 goto clunk_newdir;
803         }
804
805         v9fs_blank_wstat(&wstat);
806         wstat.muid = v9ses->uname;
807         wstat.name = (char *) new_dentry->d_name.name;
808         retval = p9_client_wstat(oldfid, &wstat);
809
810 clunk_newdir:
811         p9_client_clunk(newdirfid);
812
813 clunk_olddir:
814         p9_client_clunk(olddirfid);
815
816 done:
817         return retval;
818 }
819
820 /**
821  * v9fs_vfs_getattr - retrieve file metadata
822  * @mnt: mount information
823  * @dentry: file to get attributes on
824  * @stat: metadata structure to populate
825  *
826  */
827
828 static int
829 v9fs_vfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
830                  struct kstat *stat)
831 {
832         int err;
833         struct v9fs_session_info *v9ses;
834         struct p9_fid *fid;
835         struct p9_wstat *st;
836
837         P9_DPRINTK(P9_DEBUG_VFS, "dentry: %p\n", dentry);
838         err = -EPERM;
839         v9ses = v9fs_inode2v9ses(dentry->d_inode);
840         if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
841                 return simple_getattr(mnt, dentry, stat);
842
843         fid = v9fs_fid_lookup(dentry);
844         if (IS_ERR(fid))
845                 return PTR_ERR(fid);
846
847         st = p9_client_stat(fid);
848         if (IS_ERR(st))
849                 return PTR_ERR(st);
850
851         v9fs_stat2inode(st, dentry->d_inode, dentry->d_inode->i_sb);
852                 generic_fillattr(dentry->d_inode, stat);
853
854         kfree(st);
855         return 0;
856 }
857
858 /**
859  * v9fs_vfs_setattr - set file metadata
860  * @dentry: file whose metadata to set
861  * @iattr: metadata assignment structure
862  *
863  */
864
865 static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
866 {
867         int retval;
868         struct v9fs_session_info *v9ses;
869         struct p9_fid *fid;
870         struct p9_wstat wstat;
871
872         P9_DPRINTK(P9_DEBUG_VFS, "\n");
873         retval = -EPERM;
874         v9ses = v9fs_inode2v9ses(dentry->d_inode);
875         fid = v9fs_fid_lookup(dentry);
876         if(IS_ERR(fid))
877                 return PTR_ERR(fid);
878
879         v9fs_blank_wstat(&wstat);
880         if (iattr->ia_valid & ATTR_MODE)
881                 wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
882
883         if (iattr->ia_valid & ATTR_MTIME)
884                 wstat.mtime = iattr->ia_mtime.tv_sec;
885
886         if (iattr->ia_valid & ATTR_ATIME)
887                 wstat.atime = iattr->ia_atime.tv_sec;
888
889         if (iattr->ia_valid & ATTR_SIZE)
890                 wstat.length = iattr->ia_size;
891
892         if (v9fs_proto_dotu(v9ses)) {
893                 if (iattr->ia_valid & ATTR_UID)
894                         wstat.n_uid = iattr->ia_uid;
895
896                 if (iattr->ia_valid & ATTR_GID)
897                         wstat.n_gid = iattr->ia_gid;
898         }
899
900         retval = p9_client_wstat(fid, &wstat);
901         if (retval < 0)
902                 return retval;
903
904         if ((iattr->ia_valid & ATTR_SIZE) &&
905             iattr->ia_size != i_size_read(dentry->d_inode)) {
906                 retval = vmtruncate(dentry->d_inode, iattr->ia_size);
907                 if (retval)
908                         return retval;
909         }
910
911         setattr_copy(dentry->d_inode, iattr);
912         mark_inode_dirty(dentry->d_inode);
913         return 0;
914 }
915
916 /**
917  * v9fs_stat2inode - populate an inode structure with mistat info
918  * @stat: Plan 9 metadata (mistat) structure
919  * @inode: inode to populate
920  * @sb: superblock of filesystem
921  *
922  */
923
924 void
925 v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode,
926         struct super_block *sb)
927 {
928         char ext[32];
929         char tag_name[14];
930         unsigned int i_nlink;
931         struct v9fs_session_info *v9ses = sb->s_fs_info;
932
933         inode->i_nlink = 1;
934
935         inode->i_atime.tv_sec = stat->atime;
936         inode->i_mtime.tv_sec = stat->mtime;
937         inode->i_ctime.tv_sec = stat->mtime;
938
939         inode->i_uid = v9ses->dfltuid;
940         inode->i_gid = v9ses->dfltgid;
941
942         if (v9fs_proto_dotu(v9ses)) {
943                 inode->i_uid = stat->n_uid;
944                 inode->i_gid = stat->n_gid;
945         }
946         if ((S_ISREG(inode->i_mode)) || (S_ISDIR(inode->i_mode))) {
947                 if (v9fs_proto_dotu(v9ses) && (stat->extension[0] != '\0')) {
948                         /*
949                          * Hadlink support got added later to
950                          * to the .u extension. So there can be
951                          * server out there that doesn't support
952                          * this even with .u extension. So check
953                          * for non NULL stat->extension
954                          */
955                         strncpy(ext, stat->extension, sizeof(ext));
956                         /* HARDLINKCOUNT %u */
957                         sscanf(ext, "%13s %u", tag_name, &i_nlink);
958                         if (!strncmp(tag_name, "HARDLINKCOUNT", 13))
959                                 inode->i_nlink = i_nlink;
960                 }
961         }
962         inode->i_mode = p9mode2unixmode(v9ses, stat->mode);
963         if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode))) {
964                 char type = 0;
965                 int major = -1;
966                 int minor = -1;
967
968                 strncpy(ext, stat->extension, sizeof(ext));
969                 sscanf(ext, "%c %u %u", &type, &major, &minor);
970                 switch (type) {
971                 case 'c':
972                         inode->i_mode &= ~S_IFBLK;
973                         inode->i_mode |= S_IFCHR;
974                         break;
975                 case 'b':
976                         break;
977                 default:
978                         P9_DPRINTK(P9_DEBUG_ERROR,
979                                 "Unknown special type %c %s\n", type,
980                                 stat->extension);
981                 };
982                 inode->i_rdev = MKDEV(major, minor);
983                 init_special_inode(inode, inode->i_mode, inode->i_rdev);
984         } else
985                 inode->i_rdev = 0;
986
987         i_size_write(inode, stat->length);
988
989         /* not real number of blocks, but 512 byte ones ... */
990         inode->i_blocks = (i_size_read(inode) + 512 - 1) >> 9;
991 }
992
993 /**
994  * v9fs_qid2ino - convert qid into inode number
995  * @qid: qid to hash
996  *
997  * BUG: potential for inode number collisions?
998  */
999
1000 ino_t v9fs_qid2ino(struct p9_qid *qid)
1001 {
1002         u64 path = qid->path + 2;
1003         ino_t i = 0;
1004
1005         if (sizeof(ino_t) == sizeof(path))
1006                 memcpy(&i, &path, sizeof(ino_t));
1007         else
1008                 i = (ino_t) (path ^ (path >> 32));
1009
1010         return i;
1011 }
1012
1013 /**
1014  * v9fs_readlink - read a symlink's location (internal version)
1015  * @dentry: dentry for symlink
1016  * @buffer: buffer to load symlink location into
1017  * @buflen: length of buffer
1018  *
1019  */
1020
1021 static int v9fs_readlink(struct dentry *dentry, char *buffer, int buflen)
1022 {
1023         int retval;
1024
1025         struct v9fs_session_info *v9ses;
1026         struct p9_fid *fid;
1027         struct p9_wstat *st;
1028
1029         P9_DPRINTK(P9_DEBUG_VFS, " %s\n", dentry->d_name.name);
1030         retval = -EPERM;
1031         v9ses = v9fs_inode2v9ses(dentry->d_inode);
1032         fid = v9fs_fid_lookup(dentry);
1033         if (IS_ERR(fid))
1034                 return PTR_ERR(fid);
1035
1036         if (!v9fs_proto_dotu(v9ses))
1037                 return -EBADF;
1038
1039         st = p9_client_stat(fid);
1040         if (IS_ERR(st))
1041                 return PTR_ERR(st);
1042
1043         if (!(st->mode & P9_DMSYMLINK)) {
1044                 retval = -EINVAL;
1045                 goto done;
1046         }
1047
1048         /* copy extension buffer into buffer */
1049         strncpy(buffer, st->extension, buflen);
1050
1051         P9_DPRINTK(P9_DEBUG_VFS,
1052                 "%s -> %s (%s)\n", dentry->d_name.name, st->extension, buffer);
1053
1054         retval = strnlen(buffer, buflen);
1055 done:
1056         kfree(st);
1057         return retval;
1058 }
1059
1060 /**
1061  * v9fs_vfs_follow_link - follow a symlink path
1062  * @dentry: dentry for symlink
1063  * @nd: nameidata
1064  *
1065  */
1066
1067 static void *v9fs_vfs_follow_link(struct dentry *dentry, struct nameidata *nd)
1068 {
1069         int len = 0;
1070         char *link = __getname();
1071
1072         P9_DPRINTK(P9_DEBUG_VFS, "%s n", dentry->d_name.name);
1073
1074         if (!link)
1075                 link = ERR_PTR(-ENOMEM);
1076         else {
1077                 len = v9fs_readlink(dentry, link, PATH_MAX);
1078
1079                 if (len < 0) {
1080                         __putname(link);
1081                         link = ERR_PTR(len);
1082                 } else
1083                         link[min(len, PATH_MAX-1)] = 0;
1084         }
1085         nd_set_link(nd, link);
1086
1087         return NULL;
1088 }
1089
1090 /**
1091  * v9fs_vfs_put_link - release a symlink path
1092  * @dentry: dentry for symlink
1093  * @nd: nameidata
1094  * @p: unused
1095  *
1096  */
1097
1098 static void
1099 v9fs_vfs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
1100 {
1101         char *s = nd_get_link(nd);
1102
1103         P9_DPRINTK(P9_DEBUG_VFS, " %s %s\n", dentry->d_name.name,
1104                 IS_ERR(s) ? "<error>" : s);
1105         if (!IS_ERR(s))
1106                 __putname(s);
1107 }
1108
1109 /**
1110  * v9fs_vfs_mkspecial - create a special file
1111  * @dir: inode to create special file in
1112  * @dentry: dentry to create
1113  * @mode: mode to create special file
1114  * @extension: 9p2000.u format extension string representing special file
1115  *
1116  */
1117
1118 static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
1119         int mode, const char *extension)
1120 {
1121         u32 perm;
1122         struct v9fs_session_info *v9ses;
1123         struct p9_fid *fid;
1124
1125         v9ses = v9fs_inode2v9ses(dir);
1126         if (!v9fs_proto_dotu(v9ses)) {
1127                 P9_DPRINTK(P9_DEBUG_ERROR, "not extended\n");
1128                 return -EPERM;
1129         }
1130
1131         perm = unixmode2p9mode(v9ses, mode);
1132         fid = v9fs_create(v9ses, dir, dentry, (char *) extension, perm,
1133                                                                 P9_OREAD);
1134         if (IS_ERR(fid))
1135                 return PTR_ERR(fid);
1136
1137         p9_client_clunk(fid);
1138         return 0;
1139 }
1140
1141 /**
1142  * v9fs_vfs_symlink - helper function to create symlinks
1143  * @dir: directory inode containing symlink
1144  * @dentry: dentry for symlink
1145  * @symname: symlink data
1146  *
1147  * See Also: 9P2000.u RFC for more information
1148  *
1149  */
1150
1151 static int
1152 v9fs_vfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1153 {
1154         P9_DPRINTK(P9_DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino,
1155                                         dentry->d_name.name, symname);
1156
1157         return v9fs_vfs_mkspecial(dir, dentry, S_IFLNK, symname);
1158 }
1159
1160 /**
1161  * v9fs_vfs_link - create a hardlink
1162  * @old_dentry: dentry for file to link to
1163  * @dir: inode destination for new link
1164  * @dentry: dentry for link
1165  *
1166  */
1167
1168 static int
1169 v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
1170               struct dentry *dentry)
1171 {
1172         int retval;
1173         struct p9_fid *oldfid;
1174         char *name;
1175
1176         P9_DPRINTK(P9_DEBUG_VFS,
1177                 " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
1178                 old_dentry->d_name.name);
1179
1180         oldfid = v9fs_fid_clone(old_dentry);
1181         if (IS_ERR(oldfid))
1182                 return PTR_ERR(oldfid);
1183
1184         name = __getname();
1185         if (unlikely(!name)) {
1186                 retval = -ENOMEM;
1187                 goto clunk_fid;
1188         }
1189
1190         sprintf(name, "%d\n", oldfid->fid);
1191         retval = v9fs_vfs_mkspecial(dir, dentry, P9_DMLINK, name);
1192         __putname(name);
1193
1194 clunk_fid:
1195         p9_client_clunk(oldfid);
1196         return retval;
1197 }
1198
1199 /**
1200  * v9fs_vfs_mknod - create a special file
1201  * @dir: inode destination for new link
1202  * @dentry: dentry for file
1203  * @mode: mode for creation
1204  * @rdev: device associated with special file
1205  *
1206  */
1207
1208 static int
1209 v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1210 {
1211         int retval;
1212         char *name;
1213
1214         P9_DPRINTK(P9_DEBUG_VFS,
1215                 " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
1216                 dentry->d_name.name, mode, MAJOR(rdev), MINOR(rdev));
1217
1218         if (!new_valid_dev(rdev))
1219                 return -EINVAL;
1220
1221         name = __getname();
1222         if (!name)
1223                 return -ENOMEM;
1224         /* build extension */
1225         if (S_ISBLK(mode))
1226                 sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
1227         else if (S_ISCHR(mode))
1228                 sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
1229         else if (S_ISFIFO(mode))
1230                 *name = 0;
1231         else if (S_ISSOCK(mode))
1232                 *name = 0;
1233         else {
1234                 __putname(name);
1235                 return -EINVAL;
1236         }
1237
1238         retval = v9fs_vfs_mkspecial(dir, dentry, mode, name);
1239         __putname(name);
1240
1241         return retval;
1242 }
1243
1244 static const struct inode_operations v9fs_dir_inode_operations_dotu = {
1245         .create = v9fs_vfs_create,
1246         .lookup = v9fs_vfs_lookup,
1247         .symlink = v9fs_vfs_symlink,
1248         .link = v9fs_vfs_link,
1249         .unlink = v9fs_vfs_unlink,
1250         .mkdir = v9fs_vfs_mkdir,
1251         .rmdir = v9fs_vfs_rmdir,
1252         .mknod = v9fs_vfs_mknod,
1253         .rename = v9fs_vfs_rename,
1254         .getattr = v9fs_vfs_getattr,
1255         .setattr = v9fs_vfs_setattr,
1256 };
1257
1258 static const struct inode_operations v9fs_dir_inode_operations_dotl = {
1259         .create = v9fs_vfs_create,
1260         .lookup = v9fs_vfs_lookup,
1261         .symlink = v9fs_vfs_symlink,
1262         .link = v9fs_vfs_link,
1263         .unlink = v9fs_vfs_unlink,
1264         .mkdir = v9fs_vfs_mkdir,
1265         .rmdir = v9fs_vfs_rmdir,
1266         .mknod = v9fs_vfs_mknod,
1267         .rename = v9fs_vfs_rename,
1268         .getattr = v9fs_vfs_getattr,
1269         .setattr = v9fs_vfs_setattr,
1270 };
1271
1272 static const struct inode_operations v9fs_dir_inode_operations = {
1273         .create = v9fs_vfs_create,
1274         .lookup = v9fs_vfs_lookup,
1275         .unlink = v9fs_vfs_unlink,
1276         .mkdir = v9fs_vfs_mkdir,
1277         .rmdir = v9fs_vfs_rmdir,
1278         .mknod = v9fs_vfs_mknod,
1279         .rename = v9fs_vfs_rename,
1280         .getattr = v9fs_vfs_getattr,
1281         .setattr = v9fs_vfs_setattr,
1282 };
1283
1284 static const struct inode_operations v9fs_file_inode_operations = {
1285         .getattr = v9fs_vfs_getattr,
1286         .setattr = v9fs_vfs_setattr,
1287 };
1288
1289 static const struct inode_operations v9fs_file_inode_operations_dotl = {
1290         .getattr = v9fs_vfs_getattr,
1291         .setattr = v9fs_vfs_setattr,
1292 };
1293
1294 static const struct inode_operations v9fs_symlink_inode_operations = {
1295         .readlink = generic_readlink,
1296         .follow_link = v9fs_vfs_follow_link,
1297         .put_link = v9fs_vfs_put_link,
1298         .getattr = v9fs_vfs_getattr,
1299         .setattr = v9fs_vfs_setattr,
1300 };
1301
1302 static const struct inode_operations v9fs_symlink_inode_operations_dotl = {
1303         .readlink = generic_readlink,
1304         .follow_link = v9fs_vfs_follow_link,
1305         .put_link = v9fs_vfs_put_link,
1306         .getattr = v9fs_vfs_getattr,
1307         .setattr = v9fs_vfs_setattr,
1308 };