netns: Teach network device kobjects which namespace they are in.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / 9p / vfs_inode.c
blobf2434fc9d2c44f620e2e579a19318321e12d6e71
1 /*
2 * linux/fs/9p/vfs_inode.c
4 * This file contains vfs inode ops for the 9P2000 protocol.
6 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
7 * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
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.
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.
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
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>
41 #include "v9fs.h"
42 #include "v9fs_vfs.h"
43 #include "fid.h"
44 #include "cache.h"
46 static const struct inode_operations v9fs_dir_inode_operations;
47 static const struct inode_operations v9fs_dir_inode_operations_ext;
48 static const struct inode_operations v9fs_file_inode_operations;
49 static const struct inode_operations v9fs_symlink_inode_operations;
51 /**
52 * unixmode2p9mode - convert unix mode bits to plan 9
53 * @v9ses: v9fs session information
54 * @mode: mode to convert
58 static int unixmode2p9mode(struct v9fs_session_info *v9ses, int mode)
60 int res;
61 res = mode & 0777;
62 if (S_ISDIR(mode))
63 res |= P9_DMDIR;
64 if (v9fs_proto_dotu(v9ses)) {
65 if (S_ISLNK(mode))
66 res |= P9_DMSYMLINK;
67 if (v9ses->nodev == 0) {
68 if (S_ISSOCK(mode))
69 res |= P9_DMSOCKET;
70 if (S_ISFIFO(mode))
71 res |= P9_DMNAMEDPIPE;
72 if (S_ISBLK(mode))
73 res |= P9_DMDEVICE;
74 if (S_ISCHR(mode))
75 res |= P9_DMDEVICE;
78 if ((mode & S_ISUID) == S_ISUID)
79 res |= P9_DMSETUID;
80 if ((mode & S_ISGID) == S_ISGID)
81 res |= P9_DMSETGID;
82 if ((mode & S_ISVTX) == S_ISVTX)
83 res |= P9_DMSETVTX;
84 if ((mode & P9_DMLINK))
85 res |= P9_DMLINK;
88 return res;
91 /**
92 * p9mode2unixmode- convert plan9 mode bits to unix mode bits
93 * @v9ses: v9fs session information
94 * @mode: mode to convert
98 static int p9mode2unixmode(struct v9fs_session_info *v9ses, int mode)
100 int res;
102 res = mode & 0777;
104 if ((mode & P9_DMDIR) == P9_DMDIR)
105 res |= S_IFDIR;
106 else if ((mode & P9_DMSYMLINK) && (v9fs_proto_dotu(v9ses)))
107 res |= S_IFLNK;
108 else if ((mode & P9_DMSOCKET) && (v9fs_proto_dotu(v9ses))
109 && (v9ses->nodev == 0))
110 res |= S_IFSOCK;
111 else if ((mode & P9_DMNAMEDPIPE) && (v9fs_proto_dotu(v9ses))
112 && (v9ses->nodev == 0))
113 res |= S_IFIFO;
114 else if ((mode & P9_DMDEVICE) && (v9fs_proto_dotu(v9ses))
115 && (v9ses->nodev == 0))
116 res |= S_IFBLK;
117 else
118 res |= S_IFREG;
120 if (v9fs_proto_dotu(v9ses)) {
121 if ((mode & P9_DMSETUID) == P9_DMSETUID)
122 res |= S_ISUID;
124 if ((mode & P9_DMSETGID) == P9_DMSETGID)
125 res |= S_ISGID;
127 if ((mode & P9_DMSETVTX) == P9_DMSETVTX)
128 res |= S_ISVTX;
131 return res;
135 * v9fs_uflags2omode- convert posix open flags to plan 9 mode bits
136 * @uflags: flags to convert
137 * @extended: if .u extensions are active
140 int v9fs_uflags2omode(int uflags, int extended)
142 int ret;
144 ret = 0;
145 switch (uflags&3) {
146 default:
147 case O_RDONLY:
148 ret = P9_OREAD;
149 break;
151 case O_WRONLY:
152 ret = P9_OWRITE;
153 break;
155 case O_RDWR:
156 ret = P9_ORDWR;
157 break;
160 if (uflags & O_TRUNC)
161 ret |= P9_OTRUNC;
163 if (extended) {
164 if (uflags & O_EXCL)
165 ret |= P9_OEXCL;
167 if (uflags & O_APPEND)
168 ret |= P9_OAPPEND;
171 return ret;
175 * v9fs_blank_wstat - helper function to setup a 9P stat structure
176 * @wstat: structure to initialize
180 void
181 v9fs_blank_wstat(struct p9_wstat *wstat)
183 wstat->type = ~0;
184 wstat->dev = ~0;
185 wstat->qid.type = ~0;
186 wstat->qid.version = ~0;
187 *((long long *)&wstat->qid.path) = ~0;
188 wstat->mode = ~0;
189 wstat->atime = ~0;
190 wstat->mtime = ~0;
191 wstat->length = ~0;
192 wstat->name = NULL;
193 wstat->uid = NULL;
194 wstat->gid = NULL;
195 wstat->muid = NULL;
196 wstat->n_uid = ~0;
197 wstat->n_gid = ~0;
198 wstat->n_muid = ~0;
199 wstat->extension = NULL;
202 #ifdef CONFIG_9P_FSCACHE
204 * v9fs_alloc_inode - helper function to allocate an inode
205 * This callback is executed before setting up the inode so that we
206 * can associate a vcookie with each inode.
210 struct inode *v9fs_alloc_inode(struct super_block *sb)
212 struct v9fs_cookie *vcookie;
213 vcookie = (struct v9fs_cookie *)kmem_cache_alloc(vcookie_cache,
214 GFP_KERNEL);
215 if (!vcookie)
216 return NULL;
218 vcookie->fscache = NULL;
219 vcookie->qid = NULL;
220 spin_lock_init(&vcookie->lock);
221 return &vcookie->inode;
225 * v9fs_destroy_inode - destroy an inode
229 void v9fs_destroy_inode(struct inode *inode)
231 kmem_cache_free(vcookie_cache, v9fs_inode2cookie(inode));
233 #endif
236 * v9fs_get_inode - helper function to setup an inode
237 * @sb: superblock
238 * @mode: mode to setup inode with
242 struct inode *v9fs_get_inode(struct super_block *sb, int mode)
244 int err;
245 struct inode *inode;
246 struct v9fs_session_info *v9ses = sb->s_fs_info;
248 P9_DPRINTK(P9_DEBUG_VFS, "super block: %p mode: %o\n", sb, mode);
250 inode = new_inode(sb);
251 if (!inode) {
252 P9_EPRINTK(KERN_WARNING, "Problem allocating inode\n");
253 return ERR_PTR(-ENOMEM);
256 inode->i_mode = mode;
257 inode->i_uid = current_fsuid();
258 inode->i_gid = current_fsgid();
259 inode->i_blocks = 0;
260 inode->i_rdev = 0;
261 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
262 inode->i_mapping->a_ops = &v9fs_addr_operations;
264 switch (mode & S_IFMT) {
265 case S_IFIFO:
266 case S_IFBLK:
267 case S_IFCHR:
268 case S_IFSOCK:
269 if (!v9fs_proto_dotu(v9ses)) {
270 P9_DPRINTK(P9_DEBUG_ERROR,
271 "special files without extended mode\n");
272 err = -EINVAL;
273 goto error;
275 init_special_inode(inode, inode->i_mode, inode->i_rdev);
276 break;
277 case S_IFREG:
278 inode->i_op = &v9fs_file_inode_operations;
279 inode->i_fop = &v9fs_file_operations;
280 break;
281 case S_IFLNK:
282 if (!v9fs_proto_dotu(v9ses)) {
283 P9_DPRINTK(P9_DEBUG_ERROR,
284 "extended modes used w/o 9P2000.u\n");
285 err = -EINVAL;
286 goto error;
288 inode->i_op = &v9fs_symlink_inode_operations;
289 break;
290 case S_IFDIR:
291 inc_nlink(inode);
292 if (v9fs_proto_dotu(v9ses))
293 inode->i_op = &v9fs_dir_inode_operations_ext;
294 else
295 inode->i_op = &v9fs_dir_inode_operations;
296 inode->i_fop = &v9fs_dir_operations;
297 break;
298 default:
299 P9_DPRINTK(P9_DEBUG_ERROR, "BAD mode 0x%x S_IFMT 0x%x\n",
300 mode, mode & S_IFMT);
301 err = -EINVAL;
302 goto error;
305 return inode;
307 error:
308 iput(inode);
309 return ERR_PTR(err);
313 static struct v9fs_fid*
314 v9fs_clone_walk(struct v9fs_session_info *v9ses, u32 fid, struct dentry *dentry)
316 int err;
317 int nfid;
318 struct v9fs_fid *ret;
319 struct v9fs_fcall *fcall;
321 nfid = v9fs_get_idpool(&v9ses->fidpool);
322 if (nfid < 0) {
323 eprintk(KERN_WARNING, "no free fids available\n");
324 return ERR_PTR(-ENOSPC);
327 err = v9fs_t_walk(v9ses, fid, nfid, (char *) dentry->d_name.name,
328 &fcall);
330 if (err < 0) {
331 if (fcall && fcall->id == RWALK)
332 goto clunk_fid;
334 PRINT_FCALL_ERROR("walk error", fcall);
335 v9fs_put_idpool(nfid, &v9ses->fidpool);
336 goto error;
339 kfree(fcall);
340 fcall = NULL;
341 ret = v9fs_fid_create(v9ses, nfid);
342 if (!ret) {
343 err = -ENOMEM;
344 goto clunk_fid;
347 err = v9fs_fid_insert(ret, dentry);
348 if (err < 0) {
349 v9fs_fid_destroy(ret);
350 goto clunk_fid;
353 return ret;
355 clunk_fid:
356 v9fs_t_clunk(v9ses, nfid);
358 error:
359 kfree(fcall);
360 return ERR_PTR(err);
366 * v9fs_clear_inode - release an inode
367 * @inode: inode to release
370 void v9fs_clear_inode(struct inode *inode)
372 filemap_fdatawrite(inode->i_mapping);
374 #ifdef CONFIG_9P_FSCACHE
375 v9fs_cache_inode_put_cookie(inode);
376 #endif
380 * v9fs_inode_from_fid - populate an inode by issuing a attribute request
381 * @v9ses: session information
382 * @fid: fid to issue attribute request for
383 * @sb: superblock on which to create inode
387 static struct inode *
388 v9fs_inode_from_fid(struct v9fs_session_info *v9ses, struct p9_fid *fid,
389 struct super_block *sb)
391 int err, umode;
392 struct inode *ret;
393 struct p9_wstat *st;
395 ret = NULL;
396 st = p9_client_stat(fid);
397 if (IS_ERR(st))
398 return ERR_CAST(st);
400 umode = p9mode2unixmode(v9ses, st->mode);
401 ret = v9fs_get_inode(sb, umode);
402 if (IS_ERR(ret)) {
403 err = PTR_ERR(ret);
404 goto error;
407 v9fs_stat2inode(st, ret, sb);
408 ret->i_ino = v9fs_qid2ino(&st->qid);
410 #ifdef CONFIG_9P_FSCACHE
411 v9fs_vcookie_set_qid(ret, &st->qid);
412 v9fs_cache_inode_get_cookie(ret);
413 #endif
414 p9stat_free(st);
415 kfree(st);
417 return ret;
419 error:
420 p9stat_free(st);
421 kfree(st);
422 return ERR_PTR(err);
426 * v9fs_remove - helper function to remove files and directories
427 * @dir: directory inode that is being deleted
428 * @file: dentry that is being deleted
429 * @rmdir: removing a directory
433 static int v9fs_remove(struct inode *dir, struct dentry *file, int rmdir)
435 int retval;
436 struct inode *file_inode;
437 struct v9fs_session_info *v9ses;
438 struct p9_fid *v9fid;
440 P9_DPRINTK(P9_DEBUG_VFS, "inode: %p dentry: %p rmdir: %d\n", dir, file,
441 rmdir);
443 file_inode = file->d_inode;
444 v9ses = v9fs_inode2v9ses(file_inode);
445 v9fid = v9fs_fid_clone(file);
446 if (IS_ERR(v9fid))
447 return PTR_ERR(v9fid);
449 retval = p9_client_remove(v9fid);
450 if (!retval)
451 drop_nlink(file_inode);
452 return retval;
455 static int
456 v9fs_open_created(struct inode *inode, struct file *file)
458 return 0;
463 * v9fs_create - Create a file
464 * @v9ses: session information
465 * @dir: directory that dentry is being created in
466 * @dentry: dentry that is being created
467 * @extension: 9p2000.u extension string to support devices, etc.
468 * @perm: create permissions
469 * @mode: open mode
472 static struct p9_fid *
473 v9fs_create(struct v9fs_session_info *v9ses, struct inode *dir,
474 struct dentry *dentry, char *extension, u32 perm, u8 mode)
476 int err;
477 char *name;
478 struct p9_fid *dfid, *ofid, *fid;
479 struct inode *inode;
481 P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
483 err = 0;
484 ofid = NULL;
485 fid = NULL;
486 name = (char *) dentry->d_name.name;
487 dfid = v9fs_fid_clone(dentry->d_parent);
488 if (IS_ERR(dfid)) {
489 err = PTR_ERR(dfid);
490 P9_DPRINTK(P9_DEBUG_VFS, "fid clone failed %d\n", err);
491 dfid = NULL;
492 goto error;
495 /* clone a fid to use for creation */
496 ofid = p9_client_walk(dfid, 0, NULL, 1);
497 if (IS_ERR(ofid)) {
498 err = PTR_ERR(ofid);
499 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
500 ofid = NULL;
501 goto error;
504 err = p9_client_fcreate(ofid, name, perm, mode, extension);
505 if (err < 0) {
506 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_fcreate failed %d\n", err);
507 goto error;
510 /* now walk from the parent so we can get unopened fid */
511 fid = p9_client_walk(dfid, 1, &name, 0);
512 if (IS_ERR(fid)) {
513 err = PTR_ERR(fid);
514 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
515 fid = NULL;
516 goto error;
517 } else
518 dfid = NULL;
520 /* instantiate inode and assign the unopened fid to the dentry */
521 inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
522 if (IS_ERR(inode)) {
523 err = PTR_ERR(inode);
524 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n", err);
525 goto error;
528 if (v9ses->cache)
529 dentry->d_op = &v9fs_cached_dentry_operations;
530 else
531 dentry->d_op = &v9fs_dentry_operations;
533 d_instantiate(dentry, inode);
534 err = v9fs_fid_add(dentry, fid);
535 if (err < 0)
536 goto error;
538 return ofid;
540 error:
541 if (dfid)
542 p9_client_clunk(dfid);
544 if (ofid)
545 p9_client_clunk(ofid);
547 if (fid)
548 p9_client_clunk(fid);
550 return ERR_PTR(err);
554 * v9fs_vfs_create - VFS hook to create files
555 * @dir: directory inode that is being created
556 * @dentry: dentry that is being deleted
557 * @mode: create permissions
558 * @nd: path information
562 static int
563 v9fs_vfs_create(struct inode *dir, struct dentry *dentry, int mode,
564 struct nameidata *nd)
566 int err;
567 u32 perm;
568 int flags;
569 struct v9fs_session_info *v9ses;
570 struct p9_fid *fid;
571 struct file *filp;
573 err = 0;
574 fid = NULL;
575 v9ses = v9fs_inode2v9ses(dir);
576 perm = unixmode2p9mode(v9ses, mode);
577 if (nd && nd->flags & LOOKUP_OPEN)
578 flags = nd->intent.open.flags - 1;
579 else
580 flags = O_RDWR;
582 fid = v9fs_create(v9ses, dir, dentry, NULL, perm,
583 v9fs_uflags2omode(flags,
584 v9fs_proto_dotu(v9ses)));
585 if (IS_ERR(fid)) {
586 err = PTR_ERR(fid);
587 fid = NULL;
588 goto error;
591 /* if we are opening a file, assign the open fid to the file */
592 if (nd && nd->flags & LOOKUP_OPEN) {
593 filp = lookup_instantiate_filp(nd, dentry, v9fs_open_created);
594 if (IS_ERR(filp)) {
595 err = PTR_ERR(filp);
596 goto error;
599 filp->private_data = fid;
600 } else
601 p9_client_clunk(fid);
603 return 0;
605 error:
606 if (fid)
607 p9_client_clunk(fid);
609 return err;
613 * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
614 * @dir: inode that is being unlinked
615 * @dentry: dentry that is being unlinked
616 * @mode: mode for new directory
620 static int v9fs_vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
622 int err;
623 u32 perm;
624 struct v9fs_session_info *v9ses;
625 struct p9_fid *fid;
627 P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
628 err = 0;
629 v9ses = v9fs_inode2v9ses(dir);
630 perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
631 fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_OREAD);
632 if (IS_ERR(fid)) {
633 err = PTR_ERR(fid);
634 fid = NULL;
637 if (fid)
638 p9_client_clunk(fid);
640 return err;
644 * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
645 * @dir: inode that is being walked from
646 * @dentry: dentry that is being walked to?
647 * @nameidata: path data
651 static struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
652 struct nameidata *nameidata)
654 struct super_block *sb;
655 struct v9fs_session_info *v9ses;
656 struct p9_fid *dfid, *fid;
657 struct inode *inode;
658 char *name;
659 int result = 0;
661 P9_DPRINTK(P9_DEBUG_VFS, "dir: %p dentry: (%s) %p nameidata: %p\n",
662 dir, dentry->d_name.name, dentry, nameidata);
664 if (dentry->d_name.len > NAME_MAX)
665 return ERR_PTR(-ENAMETOOLONG);
667 sb = dir->i_sb;
668 v9ses = v9fs_inode2v9ses(dir);
669 dfid = v9fs_fid_lookup(dentry->d_parent);
670 if (IS_ERR(dfid))
671 return ERR_CAST(dfid);
673 name = (char *) dentry->d_name.name;
674 fid = p9_client_walk(dfid, 1, &name, 1);
675 if (IS_ERR(fid)) {
676 result = PTR_ERR(fid);
677 if (result == -ENOENT) {
678 d_add(dentry, NULL);
679 return NULL;
682 return ERR_PTR(result);
685 inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
686 if (IS_ERR(inode)) {
687 result = PTR_ERR(inode);
688 inode = NULL;
689 goto error;
692 result = v9fs_fid_add(dentry, fid);
693 if (result < 0)
694 goto error;
696 if ((fid->qid.version) && (v9ses->cache))
697 dentry->d_op = &v9fs_cached_dentry_operations;
698 else
699 dentry->d_op = &v9fs_dentry_operations;
701 d_add(dentry, inode);
702 return NULL;
704 error:
705 p9_client_clunk(fid);
707 return ERR_PTR(result);
711 * v9fs_vfs_unlink - VFS unlink hook to delete an inode
712 * @i: inode that is being unlinked
713 * @d: dentry that is being unlinked
717 static int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
719 return v9fs_remove(i, d, 0);
723 * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
724 * @i: inode that is being unlinked
725 * @d: dentry that is being unlinked
729 static int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
731 return v9fs_remove(i, d, 1);
735 * v9fs_vfs_rename - VFS hook to rename an inode
736 * @old_dir: old dir inode
737 * @old_dentry: old dentry
738 * @new_dir: new dir inode
739 * @new_dentry: new dentry
743 static int
744 v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
745 struct inode *new_dir, struct dentry *new_dentry)
747 struct inode *old_inode;
748 struct v9fs_session_info *v9ses;
749 struct p9_fid *oldfid;
750 struct p9_fid *olddirfid;
751 struct p9_fid *newdirfid;
752 struct p9_wstat wstat;
753 int retval;
755 P9_DPRINTK(P9_DEBUG_VFS, "\n");
756 retval = 0;
757 old_inode = old_dentry->d_inode;
758 v9ses = v9fs_inode2v9ses(old_inode);
759 oldfid = v9fs_fid_lookup(old_dentry);
760 if (IS_ERR(oldfid))
761 return PTR_ERR(oldfid);
763 olddirfid = v9fs_fid_clone(old_dentry->d_parent);
764 if (IS_ERR(olddirfid)) {
765 retval = PTR_ERR(olddirfid);
766 goto done;
769 newdirfid = v9fs_fid_clone(new_dentry->d_parent);
770 if (IS_ERR(newdirfid)) {
771 retval = PTR_ERR(newdirfid);
772 goto clunk_olddir;
775 /* 9P can only handle file rename in the same directory */
776 if (memcmp(&olddirfid->qid, &newdirfid->qid, sizeof(newdirfid->qid))) {
777 P9_DPRINTK(P9_DEBUG_ERROR,
778 "old dir and new dir are different\n");
779 retval = -EXDEV;
780 goto clunk_newdir;
783 v9fs_blank_wstat(&wstat);
784 wstat.muid = v9ses->uname;
785 wstat.name = (char *) new_dentry->d_name.name;
786 retval = p9_client_wstat(oldfid, &wstat);
788 clunk_newdir:
789 p9_client_clunk(newdirfid);
791 clunk_olddir:
792 p9_client_clunk(olddirfid);
794 done:
795 return retval;
799 * v9fs_vfs_getattr - retrieve file metadata
800 * @mnt: mount information
801 * @dentry: file to get attributes on
802 * @stat: metadata structure to populate
806 static int
807 v9fs_vfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
808 struct kstat *stat)
810 int err;
811 struct v9fs_session_info *v9ses;
812 struct p9_fid *fid;
813 struct p9_wstat *st;
815 P9_DPRINTK(P9_DEBUG_VFS, "dentry: %p\n", dentry);
816 err = -EPERM;
817 v9ses = v9fs_inode2v9ses(dentry->d_inode);
818 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
819 return simple_getattr(mnt, dentry, stat);
821 fid = v9fs_fid_lookup(dentry);
822 if (IS_ERR(fid))
823 return PTR_ERR(fid);
825 st = p9_client_stat(fid);
826 if (IS_ERR(st))
827 return PTR_ERR(st);
829 v9fs_stat2inode(st, dentry->d_inode, dentry->d_inode->i_sb);
830 generic_fillattr(dentry->d_inode, stat);
832 kfree(st);
833 return 0;
837 * v9fs_vfs_setattr - set file metadata
838 * @dentry: file whose metadata to set
839 * @iattr: metadata assignment structure
843 static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
845 int retval;
846 struct v9fs_session_info *v9ses;
847 struct p9_fid *fid;
848 struct p9_wstat wstat;
850 P9_DPRINTK(P9_DEBUG_VFS, "\n");
851 retval = -EPERM;
852 v9ses = v9fs_inode2v9ses(dentry->d_inode);
853 fid = v9fs_fid_lookup(dentry);
854 if(IS_ERR(fid))
855 return PTR_ERR(fid);
857 v9fs_blank_wstat(&wstat);
858 if (iattr->ia_valid & ATTR_MODE)
859 wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
861 if (iattr->ia_valid & ATTR_MTIME)
862 wstat.mtime = iattr->ia_mtime.tv_sec;
864 if (iattr->ia_valid & ATTR_ATIME)
865 wstat.atime = iattr->ia_atime.tv_sec;
867 if (iattr->ia_valid & ATTR_SIZE)
868 wstat.length = iattr->ia_size;
870 if (v9fs_proto_dotu(v9ses)) {
871 if (iattr->ia_valid & ATTR_UID)
872 wstat.n_uid = iattr->ia_uid;
874 if (iattr->ia_valid & ATTR_GID)
875 wstat.n_gid = iattr->ia_gid;
878 retval = p9_client_wstat(fid, &wstat);
879 if (retval >= 0)
880 retval = inode_setattr(dentry->d_inode, iattr);
882 return retval;
886 * v9fs_stat2inode - populate an inode structure with mistat info
887 * @stat: Plan 9 metadata (mistat) structure
888 * @inode: inode to populate
889 * @sb: superblock of filesystem
893 void
894 v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode,
895 struct super_block *sb)
897 char ext[32];
898 char tag_name[14];
899 unsigned int i_nlink;
900 struct v9fs_session_info *v9ses = sb->s_fs_info;
902 inode->i_nlink = 1;
904 inode->i_atime.tv_sec = stat->atime;
905 inode->i_mtime.tv_sec = stat->mtime;
906 inode->i_ctime.tv_sec = stat->mtime;
908 inode->i_uid = v9ses->dfltuid;
909 inode->i_gid = v9ses->dfltgid;
911 if (v9fs_proto_dotu(v9ses)) {
912 inode->i_uid = stat->n_uid;
913 inode->i_gid = stat->n_gid;
915 if ((S_ISREG(inode->i_mode)) || (S_ISDIR(inode->i_mode))) {
916 if (v9fs_proto_dotu(v9ses) && (stat->extension[0] != '\0')) {
918 * Hadlink support got added later to
919 * to the .u extension. So there can be
920 * server out there that doesn't support
921 * this even with .u extension. So check
922 * for non NULL stat->extension
924 strncpy(ext, stat->extension, sizeof(ext));
925 /* HARDLINKCOUNT %u */
926 sscanf(ext, "%13s %u", tag_name, &i_nlink);
927 if (!strncmp(tag_name, "HARDLINKCOUNT", 13))
928 inode->i_nlink = i_nlink;
931 inode->i_mode = p9mode2unixmode(v9ses, stat->mode);
932 if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode))) {
933 char type = 0;
934 int major = -1;
935 int minor = -1;
937 strncpy(ext, stat->extension, sizeof(ext));
938 sscanf(ext, "%c %u %u", &type, &major, &minor);
939 switch (type) {
940 case 'c':
941 inode->i_mode &= ~S_IFBLK;
942 inode->i_mode |= S_IFCHR;
943 break;
944 case 'b':
945 break;
946 default:
947 P9_DPRINTK(P9_DEBUG_ERROR,
948 "Unknown special type %c %s\n", type,
949 stat->extension);
951 inode->i_rdev = MKDEV(major, minor);
952 init_special_inode(inode, inode->i_mode, inode->i_rdev);
953 } else
954 inode->i_rdev = 0;
956 i_size_write(inode, stat->length);
958 /* not real number of blocks, but 512 byte ones ... */
959 inode->i_blocks = (i_size_read(inode) + 512 - 1) >> 9;
963 * v9fs_qid2ino - convert qid into inode number
964 * @qid: qid to hash
966 * BUG: potential for inode number collisions?
969 ino_t v9fs_qid2ino(struct p9_qid *qid)
971 u64 path = qid->path + 2;
972 ino_t i = 0;
974 if (sizeof(ino_t) == sizeof(path))
975 memcpy(&i, &path, sizeof(ino_t));
976 else
977 i = (ino_t) (path ^ (path >> 32));
979 return i;
983 * v9fs_readlink - read a symlink's location (internal version)
984 * @dentry: dentry for symlink
985 * @buffer: buffer to load symlink location into
986 * @buflen: length of buffer
990 static int v9fs_readlink(struct dentry *dentry, char *buffer, int buflen)
992 int retval;
994 struct v9fs_session_info *v9ses;
995 struct p9_fid *fid;
996 struct p9_wstat *st;
998 P9_DPRINTK(P9_DEBUG_VFS, " %s\n", dentry->d_name.name);
999 retval = -EPERM;
1000 v9ses = v9fs_inode2v9ses(dentry->d_inode);
1001 fid = v9fs_fid_lookup(dentry);
1002 if (IS_ERR(fid))
1003 return PTR_ERR(fid);
1005 if (!v9fs_proto_dotu(v9ses))
1006 return -EBADF;
1008 st = p9_client_stat(fid);
1009 if (IS_ERR(st))
1010 return PTR_ERR(st);
1012 if (!(st->mode & P9_DMSYMLINK)) {
1013 retval = -EINVAL;
1014 goto done;
1017 /* copy extension buffer into buffer */
1018 strncpy(buffer, st->extension, buflen);
1020 P9_DPRINTK(P9_DEBUG_VFS,
1021 "%s -> %s (%s)\n", dentry->d_name.name, st->extension, buffer);
1023 retval = strnlen(buffer, buflen);
1024 done:
1025 kfree(st);
1026 return retval;
1030 * v9fs_vfs_follow_link - follow a symlink path
1031 * @dentry: dentry for symlink
1032 * @nd: nameidata
1036 static void *v9fs_vfs_follow_link(struct dentry *dentry, struct nameidata *nd)
1038 int len = 0;
1039 char *link = __getname();
1041 P9_DPRINTK(P9_DEBUG_VFS, "%s n", dentry->d_name.name);
1043 if (!link)
1044 link = ERR_PTR(-ENOMEM);
1045 else {
1046 len = v9fs_readlink(dentry, link, PATH_MAX);
1048 if (len < 0) {
1049 __putname(link);
1050 link = ERR_PTR(len);
1051 } else
1052 link[min(len, PATH_MAX-1)] = 0;
1054 nd_set_link(nd, link);
1056 return NULL;
1060 * v9fs_vfs_put_link - release a symlink path
1061 * @dentry: dentry for symlink
1062 * @nd: nameidata
1063 * @p: unused
1067 static void
1068 v9fs_vfs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
1070 char *s = nd_get_link(nd);
1072 P9_DPRINTK(P9_DEBUG_VFS, " %s %s\n", dentry->d_name.name,
1073 IS_ERR(s) ? "<error>" : s);
1074 if (!IS_ERR(s))
1075 __putname(s);
1079 * v9fs_vfs_mkspecial - create a special file
1080 * @dir: inode to create special file in
1081 * @dentry: dentry to create
1082 * @mode: mode to create special file
1083 * @extension: 9p2000.u format extension string representing special file
1087 static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
1088 int mode, const char *extension)
1090 u32 perm;
1091 struct v9fs_session_info *v9ses;
1092 struct p9_fid *fid;
1094 v9ses = v9fs_inode2v9ses(dir);
1095 if (!v9fs_proto_dotu(v9ses)) {
1096 P9_DPRINTK(P9_DEBUG_ERROR, "not extended\n");
1097 return -EPERM;
1100 perm = unixmode2p9mode(v9ses, mode);
1101 fid = v9fs_create(v9ses, dir, dentry, (char *) extension, perm,
1102 P9_OREAD);
1103 if (IS_ERR(fid))
1104 return PTR_ERR(fid);
1106 p9_client_clunk(fid);
1107 return 0;
1111 * v9fs_vfs_symlink - helper function to create symlinks
1112 * @dir: directory inode containing symlink
1113 * @dentry: dentry for symlink
1114 * @symname: symlink data
1116 * See Also: 9P2000.u RFC for more information
1120 static int
1121 v9fs_vfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1123 P9_DPRINTK(P9_DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino,
1124 dentry->d_name.name, symname);
1126 return v9fs_vfs_mkspecial(dir, dentry, S_IFLNK, symname);
1130 * v9fs_vfs_link - create a hardlink
1131 * @old_dentry: dentry for file to link to
1132 * @dir: inode destination for new link
1133 * @dentry: dentry for link
1137 static int
1138 v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
1139 struct dentry *dentry)
1141 int retval;
1142 struct p9_fid *oldfid;
1143 char *name;
1145 P9_DPRINTK(P9_DEBUG_VFS,
1146 " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
1147 old_dentry->d_name.name);
1149 oldfid = v9fs_fid_clone(old_dentry);
1150 if (IS_ERR(oldfid))
1151 return PTR_ERR(oldfid);
1153 name = __getname();
1154 if (unlikely(!name)) {
1155 retval = -ENOMEM;
1156 goto clunk_fid;
1159 sprintf(name, "%d\n", oldfid->fid);
1160 retval = v9fs_vfs_mkspecial(dir, dentry, P9_DMLINK, name);
1161 __putname(name);
1163 clunk_fid:
1164 p9_client_clunk(oldfid);
1165 return retval;
1169 * v9fs_vfs_mknod - create a special file
1170 * @dir: inode destination for new link
1171 * @dentry: dentry for file
1172 * @mode: mode for creation
1173 * @rdev: device associated with special file
1177 static int
1178 v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1180 int retval;
1181 char *name;
1183 P9_DPRINTK(P9_DEBUG_VFS,
1184 " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
1185 dentry->d_name.name, mode, MAJOR(rdev), MINOR(rdev));
1187 if (!new_valid_dev(rdev))
1188 return -EINVAL;
1190 name = __getname();
1191 if (!name)
1192 return -ENOMEM;
1193 /* build extension */
1194 if (S_ISBLK(mode))
1195 sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
1196 else if (S_ISCHR(mode))
1197 sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
1198 else if (S_ISFIFO(mode))
1199 *name = 0;
1200 else {
1201 __putname(name);
1202 return -EINVAL;
1205 retval = v9fs_vfs_mkspecial(dir, dentry, mode, name);
1206 __putname(name);
1208 return retval;
1211 static const struct inode_operations v9fs_dir_inode_operations_ext = {
1212 .create = v9fs_vfs_create,
1213 .lookup = v9fs_vfs_lookup,
1214 .symlink = v9fs_vfs_symlink,
1215 .link = v9fs_vfs_link,
1216 .unlink = v9fs_vfs_unlink,
1217 .mkdir = v9fs_vfs_mkdir,
1218 .rmdir = v9fs_vfs_rmdir,
1219 .mknod = v9fs_vfs_mknod,
1220 .rename = v9fs_vfs_rename,
1221 .getattr = v9fs_vfs_getattr,
1222 .setattr = v9fs_vfs_setattr,
1225 static const struct inode_operations v9fs_dir_inode_operations = {
1226 .create = v9fs_vfs_create,
1227 .lookup = v9fs_vfs_lookup,
1228 .unlink = v9fs_vfs_unlink,
1229 .mkdir = v9fs_vfs_mkdir,
1230 .rmdir = v9fs_vfs_rmdir,
1231 .mknod = v9fs_vfs_mknod,
1232 .rename = v9fs_vfs_rename,
1233 .getattr = v9fs_vfs_getattr,
1234 .setattr = v9fs_vfs_setattr,
1237 static const struct inode_operations v9fs_file_inode_operations = {
1238 .getattr = v9fs_vfs_getattr,
1239 .setattr = v9fs_vfs_setattr,
1242 static const struct inode_operations v9fs_symlink_inode_operations = {
1243 .readlink = generic_readlink,
1244 .follow_link = v9fs_vfs_follow_link,
1245 .put_link = v9fs_vfs_put_link,
1246 .getattr = v9fs_vfs_getattr,
1247 .setattr = v9fs_vfs_setattr,