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>
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>
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;
55 * unixmode2p9mode - convert unix mode bits to plan 9
56 * @v9ses: v9fs session information
57 * @mode: mode to convert
61 static int unixmode2p9mode(struct v9fs_session_info *v9ses, int mode)
67 if (v9fs_proto_dotu(v9ses)) {
70 if (v9ses->nodev == 0) {
74 res |= P9_DMNAMEDPIPE;
81 if ((mode & S_ISUID) == S_ISUID)
83 if ((mode & S_ISGID) == S_ISGID)
85 if ((mode & S_ISVTX) == S_ISVTX)
87 if ((mode & P9_DMLINK))
95 * p9mode2unixmode- convert plan9 mode bits to unix mode bits
96 * @v9ses: v9fs session information
97 * @mode: mode to convert
101 static int p9mode2unixmode(struct v9fs_session_info *v9ses, int mode)
107 if ((mode & P9_DMDIR) == P9_DMDIR)
109 else if ((mode & P9_DMSYMLINK) && (v9fs_proto_dotu(v9ses)))
111 else if ((mode & P9_DMSOCKET) && (v9fs_proto_dotu(v9ses))
112 && (v9ses->nodev == 0))
114 else if ((mode & P9_DMNAMEDPIPE) && (v9fs_proto_dotu(v9ses))
115 && (v9ses->nodev == 0))
117 else if ((mode & P9_DMDEVICE) && (v9fs_proto_dotu(v9ses))
118 && (v9ses->nodev == 0))
123 if (v9fs_proto_dotu(v9ses)) {
124 if ((mode & P9_DMSETUID) == P9_DMSETUID)
127 if ((mode & P9_DMSETGID) == P9_DMSETGID)
130 if ((mode & P9_DMSETVTX) == P9_DMSETVTX)
138 * v9fs_uflags2omode- convert posix open flags to plan 9 mode bits
139 * @uflags: flags to convert
140 * @extended: if .u extensions are active
143 int v9fs_uflags2omode(int uflags, int extended)
163 if (uflags & O_TRUNC)
170 if (uflags & O_APPEND)
178 * v9fs_blank_wstat - helper function to setup a 9P stat structure
179 * @wstat: structure to initialize
184 v9fs_blank_wstat(struct p9_wstat *wstat)
188 wstat->qid.type = ~0;
189 wstat->qid.version = ~0;
190 *((long long *)&wstat->qid.path) = ~0;
202 wstat->extension = NULL;
205 #ifdef CONFIG_9P_FSCACHE
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.
213 struct inode *v9fs_alloc_inode(struct super_block *sb)
215 struct v9fs_cookie *vcookie;
216 vcookie = (struct v9fs_cookie *)kmem_cache_alloc(vcookie_cache,
221 vcookie->fscache = NULL;
223 spin_lock_init(&vcookie->lock);
224 return &vcookie->inode;
228 * v9fs_destroy_inode - destroy an inode
232 void v9fs_destroy_inode(struct inode *inode)
234 kmem_cache_free(vcookie_cache, v9fs_inode2cookie(inode));
239 * v9fs_get_fsgid_for_create - Helper function to get the gid for creating a
240 * new file system object. This checks the S_ISGID to determine the owning
241 * group of the new file system object.
244 static gid_t v9fs_get_fsgid_for_create(struct inode *dir_inode)
246 BUG_ON(dir_inode == NULL);
248 if (dir_inode->i_mode & S_ISGID) {
249 /* set_gid bit is set.*/
250 return dir_inode->i_gid;
252 return current_fsgid();
256 * v9fs_dentry_from_dir_inode - helper function to get the dentry from
261 struct dentry *v9fs_dentry_from_dir_inode(struct inode *inode)
263 struct dentry *dentry;
265 spin_lock(&dcache_lock);
266 /* Directory should have only one entry. */
267 BUG_ON(S_ISDIR(inode->i_mode) && !list_is_singular(&inode->i_dentry));
268 dentry = list_entry(inode->i_dentry.next, struct dentry, d_alias);
269 spin_unlock(&dcache_lock);
274 * v9fs_get_inode - helper function to setup an inode
276 * @mode: mode to setup inode with
280 struct inode *v9fs_get_inode(struct super_block *sb, int mode)
284 struct v9fs_session_info *v9ses = sb->s_fs_info;
286 P9_DPRINTK(P9_DEBUG_VFS, "super block: %p mode: %o\n", sb, mode);
288 inode = new_inode(sb);
290 P9_EPRINTK(KERN_WARNING, "Problem allocating inode\n");
291 return ERR_PTR(-ENOMEM);
294 inode_init_owner(inode, NULL, mode);
297 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
298 inode->i_mapping->a_ops = &v9fs_addr_operations;
300 switch (mode & S_IFMT) {
305 if (!v9fs_proto_dotu(v9ses)) {
306 P9_DPRINTK(P9_DEBUG_ERROR,
307 "special files without extended mode\n");
311 init_special_inode(inode, inode->i_mode, inode->i_rdev);
314 if (v9fs_proto_dotl(v9ses)) {
315 inode->i_op = &v9fs_file_inode_operations_dotl;
316 inode->i_fop = &v9fs_file_operations_dotl;
318 inode->i_op = &v9fs_file_inode_operations;
319 inode->i_fop = &v9fs_file_operations;
325 if (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)) {
326 P9_DPRINTK(P9_DEBUG_ERROR, "extended modes used with "
327 "legacy protocol.\n");
332 if (v9fs_proto_dotl(v9ses))
333 inode->i_op = &v9fs_symlink_inode_operations_dotl;
335 inode->i_op = &v9fs_symlink_inode_operations;
340 if (v9fs_proto_dotl(v9ses))
341 inode->i_op = &v9fs_dir_inode_operations_dotl;
342 else if (v9fs_proto_dotu(v9ses))
343 inode->i_op = &v9fs_dir_inode_operations_dotu;
345 inode->i_op = &v9fs_dir_inode_operations;
347 if (v9fs_proto_dotl(v9ses))
348 inode->i_fop = &v9fs_dir_operations_dotl;
350 inode->i_fop = &v9fs_dir_operations;
354 P9_DPRINTK(P9_DEBUG_ERROR, "BAD mode 0x%x S_IFMT 0x%x\n",
355 mode, mode & S_IFMT);
368 static struct v9fs_fid*
369 v9fs_clone_walk(struct v9fs_session_info *v9ses, u32 fid, struct dentry *dentry)
373 struct v9fs_fid *ret;
374 struct v9fs_fcall *fcall;
376 nfid = v9fs_get_idpool(&v9ses->fidpool);
378 eprintk(KERN_WARNING, "no free fids available\n");
379 return ERR_PTR(-ENOSPC);
382 err = v9fs_t_walk(v9ses, fid, nfid, (char *) dentry->d_name.name,
386 if (fcall && fcall->id == RWALK)
389 PRINT_FCALL_ERROR("walk error", fcall);
390 v9fs_put_idpool(nfid, &v9ses->fidpool);
396 ret = v9fs_fid_create(v9ses, nfid);
402 err = v9fs_fid_insert(ret, dentry);
404 v9fs_fid_destroy(ret);
411 v9fs_t_clunk(v9ses, nfid);
421 * v9fs_clear_inode - release an inode
422 * @inode: inode to release
425 void v9fs_clear_inode(struct inode *inode)
427 filemap_fdatawrite(inode->i_mapping);
429 #ifdef CONFIG_9P_FSCACHE
430 v9fs_cache_inode_put_cookie(inode);
434 static struct inode *
435 v9fs_inode(struct v9fs_session_info *v9ses, struct p9_fid *fid,
436 struct super_block *sb)
439 struct inode *ret = NULL;
442 st = p9_client_stat(fid);
446 umode = p9mode2unixmode(v9ses, st->mode);
447 ret = v9fs_get_inode(sb, umode);
453 v9fs_stat2inode(st, ret, sb);
454 ret->i_ino = v9fs_qid2ino(&st->qid);
456 #ifdef CONFIG_9P_FSCACHE
457 v9fs_vcookie_set_qid(ret, &st->qid);
458 v9fs_cache_inode_get_cookie(ret);
469 static struct inode *
470 v9fs_inode_dotl(struct v9fs_session_info *v9ses, struct p9_fid *fid,
471 struct super_block *sb)
473 struct inode *ret = NULL;
475 struct p9_stat_dotl *st;
477 st = p9_client_getattr_dotl(fid, P9_STATS_BASIC);
481 ret = v9fs_get_inode(sb, st->st_mode);
487 v9fs_stat2inode_dotl(st, ret);
488 ret->i_ino = v9fs_qid2ino(&st->qid);
489 #ifdef CONFIG_9P_FSCACHE
490 v9fs_vcookie_set_qid(ret, &st->qid);
491 v9fs_cache_inode_get_cookie(ret);
501 * v9fs_inode_from_fid - Helper routine to populate an inode by
502 * issuing a attribute request
503 * @v9ses: session information
504 * @fid: fid to issue attribute request for
505 * @sb: superblock on which to create inode
508 static inline struct inode *
509 v9fs_inode_from_fid(struct v9fs_session_info *v9ses, struct p9_fid *fid,
510 struct super_block *sb)
512 if (v9fs_proto_dotl(v9ses))
513 return v9fs_inode_dotl(v9ses, fid, sb);
515 return v9fs_inode(v9ses, fid, sb);
519 * v9fs_remove - helper function to remove files and directories
520 * @dir: directory inode that is being deleted
521 * @file: dentry that is being deleted
522 * @rmdir: removing a directory
526 static int v9fs_remove(struct inode *dir, struct dentry *file, int rmdir)
529 struct inode *file_inode;
530 struct p9_fid *v9fid;
532 P9_DPRINTK(P9_DEBUG_VFS, "inode: %p dentry: %p rmdir: %d\n", dir, file,
535 file_inode = file->d_inode;
536 v9fid = v9fs_fid_clone(file);
538 return PTR_ERR(v9fid);
540 retval = p9_client_remove(v9fid);
542 drop_nlink(file_inode);
547 v9fs_open_created(struct inode *inode, struct file *file)
554 * v9fs_create - Create a file
555 * @v9ses: session information
556 * @dir: directory that dentry is being created in
557 * @dentry: dentry that is being created
558 * @extension: 9p2000.u extension string to support devices, etc.
559 * @perm: create permissions
563 static struct p9_fid *
564 v9fs_create(struct v9fs_session_info *v9ses, struct inode *dir,
565 struct dentry *dentry, char *extension, u32 perm, u8 mode)
569 struct p9_fid *dfid, *ofid, *fid;
572 P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
577 name = (char *) dentry->d_name.name;
578 dfid = v9fs_fid_lookup(dentry->d_parent);
581 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
585 /* clone a fid to use for creation */
586 ofid = p9_client_walk(dfid, 0, NULL, 1);
589 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
593 err = p9_client_fcreate(ofid, name, perm, mode, extension);
595 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_fcreate failed %d\n", err);
599 /* now walk from the parent so we can get unopened fid */
600 fid = p9_client_walk(dfid, 1, &name, 1);
603 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
608 /* instantiate inode and assign the unopened fid to the dentry */
609 inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
611 err = PTR_ERR(inode);
612 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n", err);
617 dentry->d_op = &v9fs_cached_dentry_operations;
619 dentry->d_op = &v9fs_dentry_operations;
621 d_instantiate(dentry, inode);
622 err = v9fs_fid_add(dentry, fid);
630 p9_client_clunk(ofid);
633 p9_client_clunk(fid);
639 * v9fs_vfs_create - VFS hook to create files
640 * @dir: directory inode that is being created
641 * @dentry: dentry that is being deleted
642 * @mode: create permissions
643 * @nd: path information
648 v9fs_vfs_create(struct inode *dir, struct dentry *dentry, int mode,
649 struct nameidata *nd)
654 struct v9fs_session_info *v9ses;
660 v9ses = v9fs_inode2v9ses(dir);
661 perm = unixmode2p9mode(v9ses, mode);
662 if (nd && nd->flags & LOOKUP_OPEN)
663 flags = nd->intent.open.flags - 1;
667 fid = v9fs_create(v9ses, dir, dentry, NULL, perm,
668 v9fs_uflags2omode(flags,
669 v9fs_proto_dotu(v9ses)));
676 /* if we are opening a file, assign the open fid to the file */
677 if (nd && nd->flags & LOOKUP_OPEN) {
678 filp = lookup_instantiate_filp(nd, dentry, v9fs_open_created);
684 filp->private_data = fid;
686 p9_client_clunk(fid);
692 p9_client_clunk(fid);
698 * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
699 * @dir: inode that is being unlinked
700 * @dentry: dentry that is being unlinked
701 * @mode: mode for new directory
705 static int v9fs_vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
709 struct v9fs_session_info *v9ses;
712 P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
714 v9ses = v9fs_inode2v9ses(dir);
715 perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
716 fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_OREAD);
723 p9_client_clunk(fid);
729 * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
730 * @dir: inode that is being walked from
731 * @dentry: dentry that is being walked to?
732 * @nameidata: path data
736 static struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
737 struct nameidata *nameidata)
739 struct super_block *sb;
740 struct v9fs_session_info *v9ses;
741 struct p9_fid *dfid, *fid;
746 P9_DPRINTK(P9_DEBUG_VFS, "dir: %p dentry: (%s) %p nameidata: %p\n",
747 dir, dentry->d_name.name, dentry, nameidata);
749 if (dentry->d_name.len > NAME_MAX)
750 return ERR_PTR(-ENAMETOOLONG);
753 v9ses = v9fs_inode2v9ses(dir);
754 dfid = v9fs_fid_lookup(dentry->d_parent);
756 return ERR_CAST(dfid);
758 name = (char *) dentry->d_name.name;
759 fid = p9_client_walk(dfid, 1, &name, 1);
761 result = PTR_ERR(fid);
762 if (result == -ENOENT) {
767 return ERR_PTR(result);
770 inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
772 result = PTR_ERR(inode);
777 result = v9fs_fid_add(dentry, fid);
783 dentry->d_op = &v9fs_cached_dentry_operations;
785 dentry->d_op = &v9fs_dentry_operations;
787 d_add(dentry, inode);
791 p9_client_clunk(fid);
793 return ERR_PTR(result);
797 * v9fs_vfs_unlink - VFS unlink hook to delete an inode
798 * @i: inode that is being unlinked
799 * @d: dentry that is being unlinked
803 static int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
805 return v9fs_remove(i, d, 0);
809 * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
810 * @i: inode that is being unlinked
811 * @d: dentry that is being unlinked
815 static int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
817 return v9fs_remove(i, d, 1);
821 * v9fs_vfs_rename - VFS hook to rename an inode
822 * @old_dir: old dir inode
823 * @old_dentry: old dentry
824 * @new_dir: new dir inode
825 * @new_dentry: new dentry
830 v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
831 struct inode *new_dir, struct dentry *new_dentry)
833 struct inode *old_inode;
834 struct v9fs_session_info *v9ses;
835 struct p9_fid *oldfid;
836 struct p9_fid *olddirfid;
837 struct p9_fid *newdirfid;
838 struct p9_wstat wstat;
841 P9_DPRINTK(P9_DEBUG_VFS, "\n");
843 old_inode = old_dentry->d_inode;
844 v9ses = v9fs_inode2v9ses(old_inode);
845 oldfid = v9fs_fid_lookup(old_dentry);
847 return PTR_ERR(oldfid);
849 olddirfid = v9fs_fid_clone(old_dentry->d_parent);
850 if (IS_ERR(olddirfid)) {
851 retval = PTR_ERR(olddirfid);
855 newdirfid = v9fs_fid_clone(new_dentry->d_parent);
856 if (IS_ERR(newdirfid)) {
857 retval = PTR_ERR(newdirfid);
861 if (v9fs_proto_dotl(v9ses)) {
862 retval = p9_client_rename(oldfid, newdirfid,
863 (char *) new_dentry->d_name.name);
864 if (retval != -ENOSYS)
868 /* 9P can only handle file rename in the same directory */
869 if (memcmp(&olddirfid->qid, &newdirfid->qid, sizeof(newdirfid->qid))) {
870 P9_DPRINTK(P9_DEBUG_ERROR,
871 "old dir and new dir are different\n");
876 v9fs_blank_wstat(&wstat);
877 wstat.muid = v9ses->uname;
878 wstat.name = (char *) new_dentry->d_name.name;
879 retval = p9_client_wstat(oldfid, &wstat);
882 p9_client_clunk(newdirfid);
885 p9_client_clunk(olddirfid);
892 * v9fs_vfs_getattr - retrieve file metadata
893 * @mnt: mount information
894 * @dentry: file to get attributes on
895 * @stat: metadata structure to populate
900 v9fs_vfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
904 struct v9fs_session_info *v9ses;
908 P9_DPRINTK(P9_DEBUG_VFS, "dentry: %p\n", dentry);
910 v9ses = v9fs_inode2v9ses(dentry->d_inode);
911 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
912 return simple_getattr(mnt, dentry, stat);
914 fid = v9fs_fid_lookup(dentry);
918 st = p9_client_stat(fid);
922 v9fs_stat2inode(st, dentry->d_inode, dentry->d_inode->i_sb);
923 generic_fillattr(dentry->d_inode, stat);
930 v9fs_vfs_getattr_dotl(struct vfsmount *mnt, struct dentry *dentry,
934 struct v9fs_session_info *v9ses;
936 struct p9_stat_dotl *st;
938 P9_DPRINTK(P9_DEBUG_VFS, "dentry: %p\n", dentry);
940 v9ses = v9fs_inode2v9ses(dentry->d_inode);
941 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
942 return simple_getattr(mnt, dentry, stat);
944 fid = v9fs_fid_lookup(dentry);
948 /* Ask for all the fields in stat structure. Server will return
949 * whatever it supports
952 st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
956 v9fs_stat2inode_dotl(st, dentry->d_inode);
957 generic_fillattr(dentry->d_inode, stat);
958 /* Change block size to what the server returned */
959 stat->blksize = st->st_blksize;
966 * v9fs_vfs_setattr - set file metadata
967 * @dentry: file whose metadata to set
968 * @iattr: metadata assignment structure
972 static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
975 struct v9fs_session_info *v9ses;
977 struct p9_wstat wstat;
979 P9_DPRINTK(P9_DEBUG_VFS, "\n");
981 v9ses = v9fs_inode2v9ses(dentry->d_inode);
982 fid = v9fs_fid_lookup(dentry);
986 v9fs_blank_wstat(&wstat);
987 if (iattr->ia_valid & ATTR_MODE)
988 wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
990 if (iattr->ia_valid & ATTR_MTIME)
991 wstat.mtime = iattr->ia_mtime.tv_sec;
993 if (iattr->ia_valid & ATTR_ATIME)
994 wstat.atime = iattr->ia_atime.tv_sec;
996 if (iattr->ia_valid & ATTR_SIZE)
997 wstat.length = iattr->ia_size;
999 if (v9fs_proto_dotu(v9ses)) {
1000 if (iattr->ia_valid & ATTR_UID)
1001 wstat.n_uid = iattr->ia_uid;
1003 if (iattr->ia_valid & ATTR_GID)
1004 wstat.n_gid = iattr->ia_gid;
1007 retval = p9_client_wstat(fid, &wstat);
1009 retval = inode_setattr(dentry->d_inode, iattr);
1015 * v9fs_vfs_setattr_dotl - set file metadata
1016 * @dentry: file whose metadata to set
1017 * @iattr: metadata assignment structure
1021 static int v9fs_vfs_setattr_dotl(struct dentry *dentry, struct iattr *iattr)
1024 struct v9fs_session_info *v9ses;
1026 struct p9_iattr_dotl p9attr;
1028 P9_DPRINTK(P9_DEBUG_VFS, "\n");
1030 retval = inode_change_ok(dentry->d_inode, iattr);
1034 p9attr.valid = iattr->ia_valid;
1035 p9attr.mode = iattr->ia_mode;
1036 p9attr.uid = iattr->ia_uid;
1037 p9attr.gid = iattr->ia_gid;
1038 p9attr.size = iattr->ia_size;
1039 p9attr.atime_sec = iattr->ia_atime.tv_sec;
1040 p9attr.atime_nsec = iattr->ia_atime.tv_nsec;
1041 p9attr.mtime_sec = iattr->ia_mtime.tv_sec;
1042 p9attr.mtime_nsec = iattr->ia_mtime.tv_nsec;
1045 v9ses = v9fs_inode2v9ses(dentry->d_inode);
1046 fid = v9fs_fid_lookup(dentry);
1048 return PTR_ERR(fid);
1050 retval = p9_client_setattr(fid, &p9attr);
1052 retval = inode_setattr(dentry->d_inode, iattr);
1058 * v9fs_stat2inode - populate an inode structure with mistat info
1059 * @stat: Plan 9 metadata (mistat) structure
1060 * @inode: inode to populate
1061 * @sb: superblock of filesystem
1066 v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode,
1067 struct super_block *sb)
1071 unsigned int i_nlink;
1072 struct v9fs_session_info *v9ses = sb->s_fs_info;
1076 inode->i_atime.tv_sec = stat->atime;
1077 inode->i_mtime.tv_sec = stat->mtime;
1078 inode->i_ctime.tv_sec = stat->mtime;
1080 inode->i_uid = v9ses->dfltuid;
1081 inode->i_gid = v9ses->dfltgid;
1083 if (v9fs_proto_dotu(v9ses)) {
1084 inode->i_uid = stat->n_uid;
1085 inode->i_gid = stat->n_gid;
1087 if ((S_ISREG(inode->i_mode)) || (S_ISDIR(inode->i_mode))) {
1088 if (v9fs_proto_dotu(v9ses) && (stat->extension[0] != '\0')) {
1090 * Hadlink support got added later to
1091 * to the .u extension. So there can be
1092 * server out there that doesn't support
1093 * this even with .u extension. So check
1094 * for non NULL stat->extension
1096 strncpy(ext, stat->extension, sizeof(ext));
1097 /* HARDLINKCOUNT %u */
1098 sscanf(ext, "%13s %u", tag_name, &i_nlink);
1099 if (!strncmp(tag_name, "HARDLINKCOUNT", 13))
1100 inode->i_nlink = i_nlink;
1103 inode->i_mode = p9mode2unixmode(v9ses, stat->mode);
1104 if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode))) {
1109 strncpy(ext, stat->extension, sizeof(ext));
1110 sscanf(ext, "%c %u %u", &type, &major, &minor);
1113 inode->i_mode &= ~S_IFBLK;
1114 inode->i_mode |= S_IFCHR;
1119 P9_DPRINTK(P9_DEBUG_ERROR,
1120 "Unknown special type %c %s\n", type,
1123 inode->i_rdev = MKDEV(major, minor);
1124 init_special_inode(inode, inode->i_mode, inode->i_rdev);
1128 i_size_write(inode, stat->length);
1130 /* not real number of blocks, but 512 byte ones ... */
1131 inode->i_blocks = (i_size_read(inode) + 512 - 1) >> 9;
1135 * v9fs_stat2inode_dotl - populate an inode structure with stat info
1136 * @stat: stat structure
1137 * @inode: inode to populate
1138 * @sb: superblock of filesystem
1143 v9fs_stat2inode_dotl(struct p9_stat_dotl *stat, struct inode *inode)
1146 if ((stat->st_result_mask & P9_STATS_BASIC) == P9_STATS_BASIC) {
1147 inode->i_atime.tv_sec = stat->st_atime_sec;
1148 inode->i_atime.tv_nsec = stat->st_atime_nsec;
1149 inode->i_mtime.tv_sec = stat->st_mtime_sec;
1150 inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
1151 inode->i_ctime.tv_sec = stat->st_ctime_sec;
1152 inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
1153 inode->i_uid = stat->st_uid;
1154 inode->i_gid = stat->st_gid;
1155 inode->i_nlink = stat->st_nlink;
1156 inode->i_mode = stat->st_mode;
1157 inode->i_rdev = new_decode_dev(stat->st_rdev);
1159 if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode)))
1160 init_special_inode(inode, inode->i_mode, inode->i_rdev);
1162 i_size_write(inode, stat->st_size);
1163 inode->i_blocks = stat->st_blocks;
1165 if (stat->st_result_mask & P9_STATS_ATIME) {
1166 inode->i_atime.tv_sec = stat->st_atime_sec;
1167 inode->i_atime.tv_nsec = stat->st_atime_nsec;
1169 if (stat->st_result_mask & P9_STATS_MTIME) {
1170 inode->i_mtime.tv_sec = stat->st_mtime_sec;
1171 inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
1173 if (stat->st_result_mask & P9_STATS_CTIME) {
1174 inode->i_ctime.tv_sec = stat->st_ctime_sec;
1175 inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
1177 if (stat->st_result_mask & P9_STATS_UID)
1178 inode->i_uid = stat->st_uid;
1179 if (stat->st_result_mask & P9_STATS_GID)
1180 inode->i_gid = stat->st_gid;
1181 if (stat->st_result_mask & P9_STATS_NLINK)
1182 inode->i_nlink = stat->st_nlink;
1183 if (stat->st_result_mask & P9_STATS_MODE) {
1184 inode->i_mode = stat->st_mode;
1185 if ((S_ISBLK(inode->i_mode)) ||
1186 (S_ISCHR(inode->i_mode)))
1187 init_special_inode(inode, inode->i_mode,
1190 if (stat->st_result_mask & P9_STATS_RDEV)
1191 inode->i_rdev = new_decode_dev(stat->st_rdev);
1192 if (stat->st_result_mask & P9_STATS_SIZE)
1193 i_size_write(inode, stat->st_size);
1194 if (stat->st_result_mask & P9_STATS_BLOCKS)
1195 inode->i_blocks = stat->st_blocks;
1197 if (stat->st_result_mask & P9_STATS_GEN)
1198 inode->i_generation = stat->st_gen;
1200 /* Currently we don't support P9_STATS_BTIME and P9_STATS_DATA_VERSION
1201 * because the inode structure does not have fields for them.
1206 * v9fs_qid2ino - convert qid into inode number
1209 * BUG: potential for inode number collisions?
1212 ino_t v9fs_qid2ino(struct p9_qid *qid)
1214 u64 path = qid->path + 2;
1217 if (sizeof(ino_t) == sizeof(path))
1218 memcpy(&i, &path, sizeof(ino_t));
1220 i = (ino_t) (path ^ (path >> 32));
1226 * v9fs_readlink - read a symlink's location (internal version)
1227 * @dentry: dentry for symlink
1228 * @buffer: buffer to load symlink location into
1229 * @buflen: length of buffer
1233 static int v9fs_readlink(struct dentry *dentry, char *buffer, int buflen)
1237 struct v9fs_session_info *v9ses;
1239 struct p9_wstat *st;
1241 P9_DPRINTK(P9_DEBUG_VFS, " %s\n", dentry->d_name.name);
1243 v9ses = v9fs_inode2v9ses(dentry->d_inode);
1244 fid = v9fs_fid_lookup(dentry);
1246 return PTR_ERR(fid);
1248 if (!v9fs_proto_dotu(v9ses))
1251 st = p9_client_stat(fid);
1255 if (!(st->mode & P9_DMSYMLINK)) {
1260 /* copy extension buffer into buffer */
1261 strncpy(buffer, st->extension, buflen);
1263 P9_DPRINTK(P9_DEBUG_VFS,
1264 "%s -> %s (%s)\n", dentry->d_name.name, st->extension, buffer);
1266 retval = strnlen(buffer, buflen);
1273 * v9fs_vfs_follow_link - follow a symlink path
1274 * @dentry: dentry for symlink
1279 static void *v9fs_vfs_follow_link(struct dentry *dentry, struct nameidata *nd)
1282 char *link = __getname();
1284 P9_DPRINTK(P9_DEBUG_VFS, "%s n", dentry->d_name.name);
1287 link = ERR_PTR(-ENOMEM);
1289 len = v9fs_readlink(dentry, link, PATH_MAX);
1293 link = ERR_PTR(len);
1295 link[min(len, PATH_MAX-1)] = 0;
1297 nd_set_link(nd, link);
1303 * v9fs_vfs_put_link - release a symlink path
1304 * @dentry: dentry for symlink
1311 v9fs_vfs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
1313 char *s = nd_get_link(nd);
1315 P9_DPRINTK(P9_DEBUG_VFS, " %s %s\n", dentry->d_name.name,
1316 IS_ERR(s) ? "<error>" : s);
1322 * v9fs_vfs_mkspecial - create a special file
1323 * @dir: inode to create special file in
1324 * @dentry: dentry to create
1325 * @mode: mode to create special file
1326 * @extension: 9p2000.u format extension string representing special file
1330 static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
1331 int mode, const char *extension)
1334 struct v9fs_session_info *v9ses;
1337 v9ses = v9fs_inode2v9ses(dir);
1338 if (!v9fs_proto_dotu(v9ses)) {
1339 P9_DPRINTK(P9_DEBUG_ERROR, "not extended\n");
1343 perm = unixmode2p9mode(v9ses, mode);
1344 fid = v9fs_create(v9ses, dir, dentry, (char *) extension, perm,
1347 return PTR_ERR(fid);
1349 p9_client_clunk(fid);
1354 * v9fs_vfs_symlink - helper function to create symlinks
1355 * @dir: directory inode containing symlink
1356 * @dentry: dentry for symlink
1357 * @symname: symlink data
1359 * See Also: 9P2000.u RFC for more information
1364 v9fs_vfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1366 P9_DPRINTK(P9_DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino,
1367 dentry->d_name.name, symname);
1369 return v9fs_vfs_mkspecial(dir, dentry, S_IFLNK, symname);
1373 * v9fs_vfs_link - create a hardlink
1374 * @old_dentry: dentry for file to link to
1375 * @dir: inode destination for new link
1376 * @dentry: dentry for link
1381 v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
1382 struct dentry *dentry)
1385 struct p9_fid *oldfid;
1388 P9_DPRINTK(P9_DEBUG_VFS,
1389 " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
1390 old_dentry->d_name.name);
1392 oldfid = v9fs_fid_clone(old_dentry);
1394 return PTR_ERR(oldfid);
1397 if (unlikely(!name)) {
1402 sprintf(name, "%d\n", oldfid->fid);
1403 retval = v9fs_vfs_mkspecial(dir, dentry, P9_DMLINK, name);
1407 p9_client_clunk(oldfid);
1412 * v9fs_vfs_link_dotl - create a hardlink for dotl
1413 * @old_dentry: dentry for file to link to
1414 * @dir: inode destination for new link
1415 * @dentry: dentry for link
1420 v9fs_vfs_link_dotl(struct dentry *old_dentry, struct inode *dir,
1421 struct dentry *dentry)
1424 struct p9_fid *dfid, *oldfid;
1426 struct v9fs_session_info *v9ses;
1427 struct dentry *dir_dentry;
1429 P9_DPRINTK(P9_DEBUG_VFS, "dir ino: %lu, old_name: %s, new_name: %s\n",
1430 dir->i_ino, old_dentry->d_name.name,
1431 dentry->d_name.name);
1433 v9ses = v9fs_inode2v9ses(dir);
1434 dir_dentry = v9fs_dentry_from_dir_inode(dir);
1435 dfid = v9fs_fid_lookup(dir_dentry);
1437 return PTR_ERR(dfid);
1439 oldfid = v9fs_fid_lookup(old_dentry);
1441 return PTR_ERR(oldfid);
1443 name = (char *) dentry->d_name.name;
1445 err = p9_client_link(dfid, oldfid, (char *)dentry->d_name.name);
1448 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_link failed %d\n", err);
1452 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
1453 /* Get the latest stat info from server. */
1455 struct p9_stat_dotl *st;
1457 fid = v9fs_fid_lookup(old_dentry);
1459 return PTR_ERR(fid);
1461 st = p9_client_getattr_dotl(fid, P9_STATS_BASIC);
1465 v9fs_stat2inode_dotl(st, old_dentry->d_inode);
1469 /* Caching disabled. No need to get upto date stat info.
1470 * This dentry will be released immediately. So, just i_count++
1472 atomic_inc(&old_dentry->d_inode->i_count);
1475 dentry->d_op = old_dentry->d_op;
1476 d_instantiate(dentry, old_dentry->d_inode);
1482 * v9fs_vfs_mknod - create a special file
1483 * @dir: inode destination for new link
1484 * @dentry: dentry for file
1485 * @mode: mode for creation
1486 * @rdev: device associated with special file
1491 v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1496 P9_DPRINTK(P9_DEBUG_VFS,
1497 " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
1498 dentry->d_name.name, mode, MAJOR(rdev), MINOR(rdev));
1500 if (!new_valid_dev(rdev))
1506 /* build extension */
1508 sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
1509 else if (S_ISCHR(mode))
1510 sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
1511 else if (S_ISFIFO(mode))
1513 else if (S_ISSOCK(mode))
1520 retval = v9fs_vfs_mkspecial(dir, dentry, mode, name);
1526 static const struct inode_operations v9fs_dir_inode_operations_dotu = {
1527 .create = v9fs_vfs_create,
1528 .lookup = v9fs_vfs_lookup,
1529 .symlink = v9fs_vfs_symlink,
1530 .link = v9fs_vfs_link_dotl,
1531 .unlink = v9fs_vfs_unlink,
1532 .mkdir = v9fs_vfs_mkdir,
1533 .rmdir = v9fs_vfs_rmdir,
1534 .mknod = v9fs_vfs_mknod,
1535 .rename = v9fs_vfs_rename,
1536 .getattr = v9fs_vfs_getattr,
1537 .setattr = v9fs_vfs_setattr,
1540 static const struct inode_operations v9fs_dir_inode_operations_dotl = {
1541 .create = v9fs_vfs_create,
1542 .lookup = v9fs_vfs_lookup,
1543 .symlink = v9fs_vfs_symlink,
1544 .link = v9fs_vfs_link,
1545 .unlink = v9fs_vfs_unlink,
1546 .mkdir = v9fs_vfs_mkdir,
1547 .rmdir = v9fs_vfs_rmdir,
1548 .mknod = v9fs_vfs_mknod,
1549 .rename = v9fs_vfs_rename,
1550 .getattr = v9fs_vfs_getattr_dotl,
1551 .setattr = v9fs_vfs_setattr_dotl,
1554 static const struct inode_operations v9fs_dir_inode_operations = {
1555 .create = v9fs_vfs_create,
1556 .lookup = v9fs_vfs_lookup,
1557 .unlink = v9fs_vfs_unlink,
1558 .mkdir = v9fs_vfs_mkdir,
1559 .rmdir = v9fs_vfs_rmdir,
1560 .mknod = v9fs_vfs_mknod,
1561 .rename = v9fs_vfs_rename,
1562 .getattr = v9fs_vfs_getattr,
1563 .setattr = v9fs_vfs_setattr,
1566 static const struct inode_operations v9fs_file_inode_operations = {
1567 .getattr = v9fs_vfs_getattr,
1568 .setattr = v9fs_vfs_setattr,
1571 static const struct inode_operations v9fs_file_inode_operations_dotl = {
1572 .getattr = v9fs_vfs_getattr_dotl,
1573 .setattr = v9fs_vfs_setattr_dotl,
1576 static const struct inode_operations v9fs_symlink_inode_operations = {
1577 .readlink = generic_readlink,
1578 .follow_link = v9fs_vfs_follow_link,
1579 .put_link = v9fs_vfs_put_link,
1580 .getattr = v9fs_vfs_getattr,
1581 .setattr = v9fs_vfs_setattr,
1584 static const struct inode_operations v9fs_symlink_inode_operations_dotl = {
1585 .readlink = generic_readlink,
1586 .follow_link = v9fs_vfs_follow_link,
1587 .put_link = v9fs_vfs_put_link,
1588 .getattr = v9fs_vfs_getattr_dotl,
1589 .setattr = v9fs_vfs_setattr_dotl,