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 <linux/xattr.h>
39 #include <linux/posix_acl.h>
40 #include <net/9p/9p.h>
41 #include <net/9p/client.h>
50 static const struct inode_operations v9fs_dir_inode_operations;
51 static const struct inode_operations v9fs_dir_inode_operations_dotu;
52 static const struct inode_operations v9fs_file_inode_operations;
53 static const struct inode_operations v9fs_symlink_inode_operations;
56 * unixmode2p9mode - convert unix mode bits to plan 9
57 * @v9ses: v9fs session information
58 * @mode: mode to convert
62 static int unixmode2p9mode(struct v9fs_session_info *v9ses, int mode)
68 if (v9fs_proto_dotu(v9ses)) {
71 if (v9ses->nodev == 0) {
75 res |= P9_DMNAMEDPIPE;
82 if ((mode & S_ISUID) == S_ISUID)
84 if ((mode & S_ISGID) == S_ISGID)
86 if ((mode & S_ISVTX) == S_ISVTX)
88 if ((mode & P9_DMLINK))
96 * p9mode2unixmode- convert plan9 mode bits to unix mode bits
97 * @v9ses: v9fs session information
98 * @stat: p9_wstat from which mode need to be derived
99 * @rdev: major number, minor number in case of device files.
102 static int p9mode2unixmode(struct v9fs_session_info *v9ses,
103 struct p9_wstat *stat, dev_t *rdev)
106 int mode = stat->mode;
108 res = mode & S_IALLUGO;
111 if ((mode & P9_DMDIR) == P9_DMDIR)
113 else if ((mode & P9_DMSYMLINK) && (v9fs_proto_dotu(v9ses)))
115 else if ((mode & P9_DMSOCKET) && (v9fs_proto_dotu(v9ses))
116 && (v9ses->nodev == 0))
118 else if ((mode & P9_DMNAMEDPIPE) && (v9fs_proto_dotu(v9ses))
119 && (v9ses->nodev == 0))
121 else if ((mode & P9_DMDEVICE) && (v9fs_proto_dotu(v9ses))
122 && (v9ses->nodev == 0)) {
123 char type = 0, ext[32];
124 int major = -1, minor = -1;
126 strncpy(ext, stat->extension, sizeof(ext));
127 sscanf(ext, "%c %u %u", &type, &major, &minor);
136 P9_DPRINTK(P9_DEBUG_ERROR,
137 "Unknown special type %c %s\n", type,
140 *rdev = MKDEV(major, minor);
144 if (v9fs_proto_dotu(v9ses)) {
145 if ((mode & P9_DMSETUID) == P9_DMSETUID)
148 if ((mode & P9_DMSETGID) == P9_DMSETGID)
151 if ((mode & P9_DMSETVTX) == P9_DMSETVTX)
158 * v9fs_uflags2omode- convert posix open flags to plan 9 mode bits
159 * @uflags: flags to convert
160 * @extended: if .u extensions are active
163 int v9fs_uflags2omode(int uflags, int extended)
183 if (uflags & O_TRUNC)
190 if (uflags & O_APPEND)
198 * v9fs_blank_wstat - helper function to setup a 9P stat structure
199 * @wstat: structure to initialize
204 v9fs_blank_wstat(struct p9_wstat *wstat)
208 wstat->qid.type = ~0;
209 wstat->qid.version = ~0;
210 *((long long *)&wstat->qid.path) = ~0;
222 wstat->extension = NULL;
226 * v9fs_alloc_inode - helper function to allocate an inode
229 struct inode *v9fs_alloc_inode(struct super_block *sb)
231 struct v9fs_inode *v9inode;
232 v9inode = (struct v9fs_inode *)kmem_cache_alloc(v9fs_inode_cache,
236 #ifdef CONFIG_9P_FSCACHE
237 v9inode->fscache = NULL;
238 spin_lock_init(&v9inode->fscache_lock);
240 v9inode->writeback_fid = NULL;
241 v9inode->cache_validity = 0;
242 mutex_init(&v9inode->v_mutex);
243 return &v9inode->vfs_inode;
247 * v9fs_destroy_inode - destroy an inode
251 static void v9fs_i_callback(struct rcu_head *head)
253 struct inode *inode = container_of(head, struct inode, i_rcu);
254 INIT_LIST_HEAD(&inode->i_dentry);
255 kmem_cache_free(v9fs_inode_cache, V9FS_I(inode));
258 void v9fs_destroy_inode(struct inode *inode)
260 call_rcu(&inode->i_rcu, v9fs_i_callback);
263 int v9fs_init_inode(struct v9fs_session_info *v9ses,
264 struct inode *inode, int mode, dev_t rdev)
268 inode_init_owner(inode, NULL, mode);
270 inode->i_rdev = rdev;
271 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
272 inode->i_mapping->a_ops = &v9fs_addr_operations;
274 switch (mode & S_IFMT) {
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 if (v9fs_proto_dotu(v9ses)) {
283 inode->i_op = &v9fs_file_inode_operations;
284 inode->i_fop = &v9fs_file_operations;
286 P9_DPRINTK(P9_DEBUG_ERROR,
287 "special files without extended mode\n");
291 init_special_inode(inode, inode->i_mode, inode->i_rdev);
294 if (v9fs_proto_dotl(v9ses)) {
295 inode->i_op = &v9fs_file_inode_operations_dotl;
298 &v9fs_cached_file_operations_dotl;
300 inode->i_fop = &v9fs_file_operations_dotl;
302 inode->i_op = &v9fs_file_inode_operations;
304 inode->i_fop = &v9fs_cached_file_operations;
306 inode->i_fop = &v9fs_file_operations;
311 if (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)) {
312 P9_DPRINTK(P9_DEBUG_ERROR, "extended modes used with "
313 "legacy protocol.\n");
318 if (v9fs_proto_dotl(v9ses))
319 inode->i_op = &v9fs_symlink_inode_operations_dotl;
321 inode->i_op = &v9fs_symlink_inode_operations;
326 if (v9fs_proto_dotl(v9ses))
327 inode->i_op = &v9fs_dir_inode_operations_dotl;
328 else if (v9fs_proto_dotu(v9ses))
329 inode->i_op = &v9fs_dir_inode_operations_dotu;
331 inode->i_op = &v9fs_dir_inode_operations;
333 if (v9fs_proto_dotl(v9ses))
334 inode->i_fop = &v9fs_dir_operations_dotl;
336 inode->i_fop = &v9fs_dir_operations;
340 P9_DPRINTK(P9_DEBUG_ERROR, "BAD mode 0x%x S_IFMT 0x%x\n",
341 mode, mode & S_IFMT);
351 * v9fs_get_inode - helper function to setup an inode
353 * @mode: mode to setup inode with
357 struct inode *v9fs_get_inode(struct super_block *sb, int mode, dev_t rdev)
361 struct v9fs_session_info *v9ses = sb->s_fs_info;
363 P9_DPRINTK(P9_DEBUG_VFS, "super block: %p mode: %o\n", sb, mode);
365 inode = new_inode(sb);
367 P9_EPRINTK(KERN_WARNING, "Problem allocating inode\n");
368 return ERR_PTR(-ENOMEM);
370 err = v9fs_init_inode(v9ses, inode, mode, rdev);
379 static struct v9fs_fid*
380 v9fs_clone_walk(struct v9fs_session_info *v9ses, u32 fid, struct dentry *dentry)
384 struct v9fs_fid *ret;
385 struct v9fs_fcall *fcall;
387 nfid = v9fs_get_idpool(&v9ses->fidpool);
389 eprintk(KERN_WARNING, "no free fids available\n");
390 return ERR_PTR(-ENOSPC);
393 err = v9fs_t_walk(v9ses, fid, nfid, (char *) dentry->d_name.name,
397 if (fcall && fcall->id == RWALK)
400 PRINT_FCALL_ERROR("walk error", fcall);
401 v9fs_put_idpool(nfid, &v9ses->fidpool);
407 ret = v9fs_fid_create(v9ses, nfid);
413 err = v9fs_fid_insert(ret, dentry);
415 v9fs_fid_destroy(ret);
422 v9fs_t_clunk(v9ses, nfid);
432 * v9fs_clear_inode - release an inode
433 * @inode: inode to release
436 void v9fs_evict_inode(struct inode *inode)
438 struct v9fs_inode *v9inode = V9FS_I(inode);
440 truncate_inode_pages(inode->i_mapping, 0);
441 end_writeback(inode);
442 filemap_fdatawrite(inode->i_mapping);
444 #ifdef CONFIG_9P_FSCACHE
445 v9fs_cache_inode_put_cookie(inode);
447 /* clunk the fid stashed in writeback_fid */
448 if (v9inode->writeback_fid) {
449 p9_client_clunk(v9inode->writeback_fid);
450 v9inode->writeback_fid = NULL;
454 static int v9fs_test_inode(struct inode *inode, void *data)
458 struct v9fs_inode *v9inode = V9FS_I(inode);
459 struct p9_wstat *st = (struct p9_wstat *)data;
460 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
462 umode = p9mode2unixmode(v9ses, st, &rdev);
463 /* don't match inode of different type */
464 if ((inode->i_mode & S_IFMT) != (umode & S_IFMT))
467 /* compare qid details */
468 if (memcmp(&v9inode->qid.version,
469 &st->qid.version, sizeof(v9inode->qid.version)))
472 if (v9inode->qid.type != st->qid.type)
477 static int v9fs_test_new_inode(struct inode *inode, void *data)
482 static int v9fs_set_inode(struct inode *inode, void *data)
484 struct v9fs_inode *v9inode = V9FS_I(inode);
485 struct p9_wstat *st = (struct p9_wstat *)data;
487 memcpy(&v9inode->qid, &st->qid, sizeof(st->qid));
491 static struct inode *v9fs_qid_iget(struct super_block *sb,
500 struct v9fs_session_info *v9ses = sb->s_fs_info;
501 int (*test)(struct inode *, void *);
504 test = v9fs_test_new_inode;
506 test = v9fs_test_inode;
508 i_ino = v9fs_qid2ino(qid);
509 inode = iget5_locked(sb, i_ino, test, v9fs_set_inode, st);
511 return ERR_PTR(-ENOMEM);
512 if (!(inode->i_state & I_NEW))
515 * initialize the inode with the stat info
516 * FIXME!! we may need support for stale inodes
519 inode->i_ino = i_ino;
520 umode = p9mode2unixmode(v9ses, st, &rdev);
521 retval = v9fs_init_inode(v9ses, inode, umode, rdev);
525 v9fs_stat2inode(st, inode, sb);
526 #ifdef CONFIG_9P_FSCACHE
527 v9fs_cache_inode_get_cookie(inode);
529 unlock_new_inode(inode);
532 unlock_new_inode(inode);
534 return ERR_PTR(retval);
539 v9fs_inode_from_fid(struct v9fs_session_info *v9ses, struct p9_fid *fid,
540 struct super_block *sb, int new)
543 struct inode *inode = NULL;
545 st = p9_client_stat(fid);
549 inode = v9fs_qid_iget(sb, &st->qid, st, new);
556 * v9fs_at_to_dotl_flags- convert Linux specific AT flags to
558 * @flags: flags to convert
560 static int v9fs_at_to_dotl_flags(int flags)
563 if (flags & AT_REMOVEDIR)
564 rflags |= P9_DOTL_AT_REMOVEDIR;
569 * v9fs_remove - helper function to remove files and directories
570 * @dir: directory inode that is being deleted
571 * @dentry: dentry that is being deleted
572 * @rmdir: removing a directory
576 static int v9fs_remove(struct inode *dir, struct dentry *dentry, int flags)
579 int retval = -EOPNOTSUPP;
580 struct p9_fid *v9fid, *dfid;
581 struct v9fs_session_info *v9ses;
583 P9_DPRINTK(P9_DEBUG_VFS, "inode: %p dentry: %p rmdir: %x\n",
586 v9ses = v9fs_inode2v9ses(dir);
587 inode = dentry->d_inode;
588 dfid = v9fs_fid_lookup(dentry->d_parent);
590 retval = PTR_ERR(dfid);
591 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", retval);
594 if (v9fs_proto_dotl(v9ses))
595 retval = p9_client_unlinkat(dfid, dentry->d_name.name,
596 v9fs_at_to_dotl_flags(flags));
597 if (retval == -EOPNOTSUPP) {
598 /* Try the one based on path */
599 v9fid = v9fs_fid_clone(dentry);
601 return PTR_ERR(v9fid);
602 retval = p9_client_remove(v9fid);
606 * directories on unlink should have zero
609 if (flags & AT_REMOVEDIR) {
615 v9fs_invalidate_inode_attr(inode);
616 v9fs_invalidate_inode_attr(dir);
622 * v9fs_create - Create a file
623 * @v9ses: session information
624 * @dir: directory that dentry is being created in
625 * @dentry: dentry that is being created
626 * @extension: 9p2000.u extension string to support devices, etc.
627 * @perm: create permissions
631 static struct p9_fid *
632 v9fs_create(struct v9fs_session_info *v9ses, struct inode *dir,
633 struct dentry *dentry, char *extension, u32 perm, u8 mode)
637 struct p9_fid *dfid, *ofid, *fid;
640 P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
645 name = (char *) dentry->d_name.name;
646 dfid = v9fs_fid_lookup(dentry->d_parent);
649 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
653 /* clone a fid to use for creation */
654 ofid = p9_client_walk(dfid, 0, NULL, 1);
657 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
661 err = p9_client_fcreate(ofid, name, perm, mode, extension);
663 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_fcreate failed %d\n", err);
667 /* now walk from the parent so we can get unopened fid */
668 fid = p9_client_walk(dfid, 1, &name, 1);
671 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
676 /* instantiate inode and assign the unopened fid to the dentry */
677 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
679 err = PTR_ERR(inode);
680 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n", err);
683 err = v9fs_fid_add(dentry, fid);
686 d_instantiate(dentry, inode);
690 p9_client_clunk(ofid);
693 p9_client_clunk(fid);
699 * v9fs_vfs_create - VFS hook to create files
700 * @dir: directory inode that is being created
701 * @dentry: dentry that is being deleted
702 * @mode: create permissions
703 * @nd: path information
708 v9fs_vfs_create(struct inode *dir, struct dentry *dentry, int mode,
709 struct nameidata *nd)
715 struct v9fs_inode *v9inode;
716 struct v9fs_session_info *v9ses;
717 struct p9_fid *fid, *inode_fid;
721 v9ses = v9fs_inode2v9ses(dir);
722 perm = unixmode2p9mode(v9ses, mode);
724 flags = nd->intent.open.flags;
728 fid = v9fs_create(v9ses, dir, dentry, NULL, perm,
729 v9fs_uflags2omode(flags,
730 v9fs_proto_dotu(v9ses)));
737 v9fs_invalidate_inode_attr(dir);
738 /* if we are opening a file, assign the open fid to the file */
740 v9inode = V9FS_I(dentry->d_inode);
741 mutex_lock(&v9inode->v_mutex);
742 if (v9ses->cache && !v9inode->writeback_fid &&
743 ((flags & O_ACCMODE) != O_RDONLY)) {
745 * clone a fid and add it to writeback_fid
746 * we do it during open time instead of
747 * page dirty time via write_begin/page_mkwrite
748 * because we want write after unlink usecase
751 inode_fid = v9fs_writeback_fid(dentry);
752 if (IS_ERR(inode_fid)) {
753 err = PTR_ERR(inode_fid);
754 mutex_unlock(&v9inode->v_mutex);
757 v9inode->writeback_fid = (void *) inode_fid;
759 mutex_unlock(&v9inode->v_mutex);
760 filp = lookup_instantiate_filp(nd, dentry, generic_file_open);
766 filp->private_data = fid;
767 #ifdef CONFIG_9P_FSCACHE
769 v9fs_cache_inode_set_cookie(dentry->d_inode, filp);
772 p9_client_clunk(fid);
778 p9_client_clunk(fid);
784 * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
785 * @dir: inode that is being unlinked
786 * @dentry: dentry that is being unlinked
787 * @mode: mode for new directory
791 static int v9fs_vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
796 struct v9fs_session_info *v9ses;
798 P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
800 v9ses = v9fs_inode2v9ses(dir);
801 perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
802 fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_OREAD);
808 v9fs_invalidate_inode_attr(dir);
812 p9_client_clunk(fid);
818 * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
819 * @dir: inode that is being walked from
820 * @dentry: dentry that is being walked to?
821 * @nameidata: path data
825 struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
826 struct nameidata *nameidata)
829 struct super_block *sb;
830 struct v9fs_session_info *v9ses;
831 struct p9_fid *dfid, *fid;
836 P9_DPRINTK(P9_DEBUG_VFS, "dir: %p dentry: (%s) %p nameidata: %p\n",
837 dir, dentry->d_name.name, dentry, nameidata);
839 if (dentry->d_name.len > NAME_MAX)
840 return ERR_PTR(-ENAMETOOLONG);
843 v9ses = v9fs_inode2v9ses(dir);
844 /* We can walk d_parent because we hold the dir->i_mutex */
845 dfid = v9fs_fid_lookup(dentry->d_parent);
847 return ERR_CAST(dfid);
849 name = (char *) dentry->d_name.name;
850 fid = p9_client_walk(dfid, 1, &name, 1);
852 result = PTR_ERR(fid);
853 if (result == -ENOENT) {
858 return ERR_PTR(result);
861 * Make sure we don't use a wrong inode due to parallel
862 * unlink. For cached mode create calls request for new
863 * inode. But with cache disabled, lookup should do this.
866 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
868 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
870 result = PTR_ERR(inode);
874 result = v9fs_fid_add(dentry, fid);
879 * If we had a rename on the server and a parallel lookup
880 * for the new name, then make sure we instantiate with
881 * the new name. ie look up for a/b, while on server somebody
882 * moved b under k and client parallely did a lookup for
885 res = d_materialise_unique(dentry, inode);
888 result = PTR_ERR(res);
892 p9_client_clunk(fid);
894 return ERR_PTR(result);
898 * v9fs_vfs_unlink - VFS unlink hook to delete an inode
899 * @i: inode that is being unlinked
900 * @d: dentry that is being unlinked
904 int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
906 return v9fs_remove(i, d, 0);
910 * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
911 * @i: inode that is being unlinked
912 * @d: dentry that is being unlinked
916 int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
918 return v9fs_remove(i, d, AT_REMOVEDIR);
922 * v9fs_vfs_rename - VFS hook to rename an inode
923 * @old_dir: old dir inode
924 * @old_dentry: old dentry
925 * @new_dir: new dir inode
926 * @new_dentry: new dentry
931 v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
932 struct inode *new_dir, struct dentry *new_dentry)
935 struct inode *old_inode;
936 struct inode *new_inode;
937 struct v9fs_session_info *v9ses;
938 struct p9_fid *oldfid;
939 struct p9_fid *olddirfid;
940 struct p9_fid *newdirfid;
941 struct p9_wstat wstat;
943 P9_DPRINTK(P9_DEBUG_VFS, "\n");
945 old_inode = old_dentry->d_inode;
946 new_inode = new_dentry->d_inode;
947 v9ses = v9fs_inode2v9ses(old_inode);
948 oldfid = v9fs_fid_lookup(old_dentry);
950 return PTR_ERR(oldfid);
952 olddirfid = v9fs_fid_clone(old_dentry->d_parent);
953 if (IS_ERR(olddirfid)) {
954 retval = PTR_ERR(olddirfid);
958 newdirfid = v9fs_fid_clone(new_dentry->d_parent);
959 if (IS_ERR(newdirfid)) {
960 retval = PTR_ERR(newdirfid);
964 down_write(&v9ses->rename_sem);
965 if (v9fs_proto_dotl(v9ses)) {
966 retval = p9_client_renameat(olddirfid, old_dentry->d_name.name,
967 newdirfid, new_dentry->d_name.name);
968 if (retval == -EOPNOTSUPP)
969 retval = p9_client_rename(oldfid, newdirfid,
970 new_dentry->d_name.name);
971 if (retval != -EOPNOTSUPP)
974 if (old_dentry->d_parent != new_dentry->d_parent) {
976 * 9P .u can only handle file rename in the same directory
979 P9_DPRINTK(P9_DEBUG_ERROR,
980 "old dir and new dir are different\n");
984 v9fs_blank_wstat(&wstat);
985 wstat.muid = v9ses->uname;
986 wstat.name = (char *) new_dentry->d_name.name;
987 retval = p9_client_wstat(oldfid, &wstat);
992 if (S_ISDIR(new_inode->i_mode))
993 clear_nlink(new_inode);
995 drop_nlink(new_inode);
997 if (S_ISDIR(old_inode->i_mode)) {
1000 drop_nlink(old_dir);
1002 v9fs_invalidate_inode_attr(old_inode);
1003 v9fs_invalidate_inode_attr(old_dir);
1004 v9fs_invalidate_inode_attr(new_dir);
1006 /* successful rename */
1007 d_move(old_dentry, new_dentry);
1009 up_write(&v9ses->rename_sem);
1010 p9_client_clunk(newdirfid);
1013 p9_client_clunk(olddirfid);
1020 * v9fs_vfs_getattr - retrieve file metadata
1021 * @mnt: mount information
1022 * @dentry: file to get attributes on
1023 * @stat: metadata structure to populate
1028 v9fs_vfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
1032 struct v9fs_session_info *v9ses;
1034 struct p9_wstat *st;
1036 P9_DPRINTK(P9_DEBUG_VFS, "dentry: %p\n", dentry);
1038 v9ses = v9fs_dentry2v9ses(dentry);
1039 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
1040 generic_fillattr(dentry->d_inode, stat);
1043 fid = v9fs_fid_lookup(dentry);
1045 return PTR_ERR(fid);
1047 st = p9_client_stat(fid);
1051 v9fs_stat2inode(st, dentry->d_inode, dentry->d_inode->i_sb);
1052 generic_fillattr(dentry->d_inode, stat);
1060 * v9fs_vfs_setattr - set file metadata
1061 * @dentry: file whose metadata to set
1062 * @iattr: metadata assignment structure
1066 static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
1069 struct v9fs_session_info *v9ses;
1071 struct p9_wstat wstat;
1073 P9_DPRINTK(P9_DEBUG_VFS, "\n");
1074 retval = inode_change_ok(dentry->d_inode, iattr);
1079 v9ses = v9fs_dentry2v9ses(dentry);
1080 fid = v9fs_fid_lookup(dentry);
1082 return PTR_ERR(fid);
1084 v9fs_blank_wstat(&wstat);
1085 if (iattr->ia_valid & ATTR_MODE)
1086 wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
1088 if (iattr->ia_valid & ATTR_MTIME)
1089 wstat.mtime = iattr->ia_mtime.tv_sec;
1091 if (iattr->ia_valid & ATTR_ATIME)
1092 wstat.atime = iattr->ia_atime.tv_sec;
1094 if (iattr->ia_valid & ATTR_SIZE)
1095 wstat.length = iattr->ia_size;
1097 if (v9fs_proto_dotu(v9ses)) {
1098 if (iattr->ia_valid & ATTR_UID)
1099 wstat.n_uid = iattr->ia_uid;
1101 if (iattr->ia_valid & ATTR_GID)
1102 wstat.n_gid = iattr->ia_gid;
1105 /* Write all dirty data */
1106 if (S_ISREG(dentry->d_inode->i_mode))
1107 filemap_write_and_wait(dentry->d_inode->i_mapping);
1109 retval = p9_client_wstat(fid, &wstat);
1113 if ((iattr->ia_valid & ATTR_SIZE) &&
1114 iattr->ia_size != i_size_read(dentry->d_inode))
1115 truncate_setsize(dentry->d_inode, iattr->ia_size);
1117 v9fs_invalidate_inode_attr(dentry->d_inode);
1119 setattr_copy(dentry->d_inode, iattr);
1120 mark_inode_dirty(dentry->d_inode);
1125 * v9fs_stat2inode - populate an inode structure with mistat info
1126 * @stat: Plan 9 metadata (mistat) structure
1127 * @inode: inode to populate
1128 * @sb: superblock of filesystem
1133 v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode,
1134 struct super_block *sb)
1139 unsigned int i_nlink;
1140 struct v9fs_session_info *v9ses = sb->s_fs_info;
1141 struct v9fs_inode *v9inode = V9FS_I(inode);
1145 inode->i_atime.tv_sec = stat->atime;
1146 inode->i_mtime.tv_sec = stat->mtime;
1147 inode->i_ctime.tv_sec = stat->mtime;
1149 inode->i_uid = v9ses->dfltuid;
1150 inode->i_gid = v9ses->dfltgid;
1152 if (v9fs_proto_dotu(v9ses)) {
1153 inode->i_uid = stat->n_uid;
1154 inode->i_gid = stat->n_gid;
1156 if ((S_ISREG(inode->i_mode)) || (S_ISDIR(inode->i_mode))) {
1157 if (v9fs_proto_dotu(v9ses) && (stat->extension[0] != '\0')) {
1159 * Hadlink support got added later to
1160 * to the .u extension. So there can be
1161 * server out there that doesn't support
1162 * this even with .u extension. So check
1163 * for non NULL stat->extension
1165 strncpy(ext, stat->extension, sizeof(ext));
1166 /* HARDLINKCOUNT %u */
1167 sscanf(ext, "%13s %u", tag_name, &i_nlink);
1168 if (!strncmp(tag_name, "HARDLINKCOUNT", 13))
1169 inode->i_nlink = i_nlink;
1172 mode = stat->mode & S_IALLUGO;
1173 mode |= inode->i_mode & ~S_IALLUGO;
1174 inode->i_mode = mode;
1175 i_size_write(inode, stat->length);
1177 /* not real number of blocks, but 512 byte ones ... */
1178 inode->i_blocks = (i_size_read(inode) + 512 - 1) >> 9;
1179 v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR;
1183 * v9fs_qid2ino - convert qid into inode number
1186 * BUG: potential for inode number collisions?
1189 ino_t v9fs_qid2ino(struct p9_qid *qid)
1191 u64 path = qid->path + 2;
1194 if (sizeof(ino_t) == sizeof(path))
1195 memcpy(&i, &path, sizeof(ino_t));
1197 i = (ino_t) (path ^ (path >> 32));
1203 * v9fs_readlink - read a symlink's location (internal version)
1204 * @dentry: dentry for symlink
1205 * @buffer: buffer to load symlink location into
1206 * @buflen: length of buffer
1210 static int v9fs_readlink(struct dentry *dentry, char *buffer, int buflen)
1214 struct v9fs_session_info *v9ses;
1216 struct p9_wstat *st;
1218 P9_DPRINTK(P9_DEBUG_VFS, " %s\n", dentry->d_name.name);
1220 v9ses = v9fs_dentry2v9ses(dentry);
1221 fid = v9fs_fid_lookup(dentry);
1223 return PTR_ERR(fid);
1225 if (!v9fs_proto_dotu(v9ses))
1228 st = p9_client_stat(fid);
1232 if (!(st->mode & P9_DMSYMLINK)) {
1237 /* copy extension buffer into buffer */
1238 strncpy(buffer, st->extension, buflen);
1240 P9_DPRINTK(P9_DEBUG_VFS,
1241 "%s -> %s (%s)\n", dentry->d_name.name, st->extension, buffer);
1243 retval = strnlen(buffer, buflen);
1251 * v9fs_vfs_follow_link - follow a symlink path
1252 * @dentry: dentry for symlink
1257 static void *v9fs_vfs_follow_link(struct dentry *dentry, struct nameidata *nd)
1260 char *link = __getname();
1262 P9_DPRINTK(P9_DEBUG_VFS, "%s n", dentry->d_name.name);
1265 link = ERR_PTR(-ENOMEM);
1267 len = v9fs_readlink(dentry, link, PATH_MAX);
1271 link = ERR_PTR(len);
1273 link[min(len, PATH_MAX-1)] = 0;
1275 nd_set_link(nd, link);
1281 * v9fs_vfs_put_link - release a symlink path
1282 * @dentry: dentry for symlink
1289 v9fs_vfs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
1291 char *s = nd_get_link(nd);
1293 P9_DPRINTK(P9_DEBUG_VFS, " %s %s\n", dentry->d_name.name,
1294 IS_ERR(s) ? "<error>" : s);
1300 * v9fs_vfs_mkspecial - create a special file
1301 * @dir: inode to create special file in
1302 * @dentry: dentry to create
1303 * @mode: mode to create special file
1304 * @extension: 9p2000.u format extension string representing special file
1308 static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
1309 int mode, const char *extension)
1313 struct v9fs_session_info *v9ses;
1315 v9ses = v9fs_inode2v9ses(dir);
1316 if (!v9fs_proto_dotu(v9ses)) {
1317 P9_DPRINTK(P9_DEBUG_ERROR, "not extended\n");
1321 perm = unixmode2p9mode(v9ses, mode);
1322 fid = v9fs_create(v9ses, dir, dentry, (char *) extension, perm,
1325 return PTR_ERR(fid);
1327 v9fs_invalidate_inode_attr(dir);
1328 p9_client_clunk(fid);
1333 * v9fs_vfs_symlink - helper function to create symlinks
1334 * @dir: directory inode containing symlink
1335 * @dentry: dentry for symlink
1336 * @symname: symlink data
1338 * See Also: 9P2000.u RFC for more information
1343 v9fs_vfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1345 P9_DPRINTK(P9_DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino,
1346 dentry->d_name.name, symname);
1348 return v9fs_vfs_mkspecial(dir, dentry, S_IFLNK, symname);
1352 * v9fs_vfs_link - create a hardlink
1353 * @old_dentry: dentry for file to link to
1354 * @dir: inode destination for new link
1355 * @dentry: dentry for link
1360 v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
1361 struct dentry *dentry)
1365 struct p9_fid *oldfid;
1367 P9_DPRINTK(P9_DEBUG_VFS,
1368 " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
1369 old_dentry->d_name.name);
1371 oldfid = v9fs_fid_clone(old_dentry);
1373 return PTR_ERR(oldfid);
1376 if (unlikely(!name)) {
1381 sprintf(name, "%d\n", oldfid->fid);
1382 retval = v9fs_vfs_mkspecial(dir, dentry, P9_DMLINK, name);
1385 v9fs_refresh_inode(oldfid, old_dentry->d_inode);
1386 v9fs_invalidate_inode_attr(dir);
1389 p9_client_clunk(oldfid);
1394 * v9fs_vfs_mknod - create a special file
1395 * @dir: inode destination for new link
1396 * @dentry: dentry for file
1397 * @mode: mode for creation
1398 * @rdev: device associated with special file
1403 v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1408 P9_DPRINTK(P9_DEBUG_VFS,
1409 " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
1410 dentry->d_name.name, mode, MAJOR(rdev), MINOR(rdev));
1412 if (!new_valid_dev(rdev))
1418 /* build extension */
1420 sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
1421 else if (S_ISCHR(mode))
1422 sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
1423 else if (S_ISFIFO(mode))
1425 else if (S_ISSOCK(mode))
1432 retval = v9fs_vfs_mkspecial(dir, dentry, mode, name);
1438 int v9fs_refresh_inode(struct p9_fid *fid, struct inode *inode)
1443 struct p9_wstat *st;
1444 struct v9fs_session_info *v9ses;
1446 v9ses = v9fs_inode2v9ses(inode);
1447 st = p9_client_stat(fid);
1451 * Don't update inode if the file type is different
1453 umode = p9mode2unixmode(v9ses, st, &rdev);
1454 if ((inode->i_mode & S_IFMT) != (umode & S_IFMT))
1457 spin_lock(&inode->i_lock);
1459 * We don't want to refresh inode->i_size,
1460 * because we may have cached data
1462 i_size = inode->i_size;
1463 v9fs_stat2inode(st, inode, inode->i_sb);
1465 inode->i_size = i_size;
1466 spin_unlock(&inode->i_lock);
1473 static const struct inode_operations v9fs_dir_inode_operations_dotu = {
1474 .create = v9fs_vfs_create,
1475 .lookup = v9fs_vfs_lookup,
1476 .symlink = v9fs_vfs_symlink,
1477 .link = v9fs_vfs_link,
1478 .unlink = v9fs_vfs_unlink,
1479 .mkdir = v9fs_vfs_mkdir,
1480 .rmdir = v9fs_vfs_rmdir,
1481 .mknod = v9fs_vfs_mknod,
1482 .rename = v9fs_vfs_rename,
1483 .getattr = v9fs_vfs_getattr,
1484 .setattr = v9fs_vfs_setattr,
1487 static const struct inode_operations v9fs_dir_inode_operations = {
1488 .create = v9fs_vfs_create,
1489 .lookup = v9fs_vfs_lookup,
1490 .unlink = v9fs_vfs_unlink,
1491 .mkdir = v9fs_vfs_mkdir,
1492 .rmdir = v9fs_vfs_rmdir,
1493 .mknod = v9fs_vfs_mknod,
1494 .rename = v9fs_vfs_rename,
1495 .getattr = v9fs_vfs_getattr,
1496 .setattr = v9fs_vfs_setattr,
1499 static const struct inode_operations v9fs_file_inode_operations = {
1500 .getattr = v9fs_vfs_getattr,
1501 .setattr = v9fs_vfs_setattr,
1504 static const struct inode_operations v9fs_symlink_inode_operations = {
1505 .readlink = generic_readlink,
1506 .follow_link = v9fs_vfs_follow_link,
1507 .put_link = v9fs_vfs_put_link,
1508 .getattr = v9fs_vfs_getattr,
1509 .setattr = v9fs_vfs_setattr,