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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28 #include <linux/module.h>
29 #include <linux/errno.h>
31 #include <linux/file.h>
32 #include <linux/pagemap.h>
33 #include <linux/stat.h>
34 #include <linux/string.h>
35 #include <linux/inet.h>
36 #include <linux/namei.h>
37 #include <linux/idr.h>
38 #include <linux/sched.h>
39 #include <linux/slab.h>
40 #include <linux/xattr.h>
41 #include <linux/posix_acl.h>
42 #include <net/9p/9p.h>
43 #include <net/9p/client.h>
52 static const struct inode_operations v9fs_dir_inode_operations;
53 static const struct inode_operations v9fs_dir_inode_operations_dotu;
54 static const struct inode_operations v9fs_file_inode_operations;
55 static const struct inode_operations v9fs_symlink_inode_operations;
58 * unixmode2p9mode - convert unix mode bits to plan 9
59 * @v9ses: v9fs session information
60 * @mode: mode to convert
64 static int unixmode2p9mode(struct v9fs_session_info *v9ses, int mode)
70 if (v9fs_proto_dotu(v9ses)) {
73 if (v9ses->nodev == 0) {
77 res |= P9_DMNAMEDPIPE;
84 if ((mode & S_ISUID) == S_ISUID)
86 if ((mode & S_ISGID) == S_ISGID)
88 if ((mode & S_ISVTX) == S_ISVTX)
90 if ((mode & P9_DMLINK))
98 * p9mode2unixmode- convert plan9 mode bits to unix mode bits
99 * @v9ses: v9fs session information
100 * @stat: p9_wstat from which mode need to be derived
101 * @rdev: major number, minor number in case of device files.
104 static int p9mode2unixmode(struct v9fs_session_info *v9ses,
105 struct p9_wstat *stat, dev_t *rdev)
108 int mode = stat->mode;
110 res = mode & S_IALLUGO;
113 if ((mode & P9_DMDIR) == P9_DMDIR)
115 else if ((mode & P9_DMSYMLINK) && (v9fs_proto_dotu(v9ses)))
117 else if ((mode & P9_DMSOCKET) && (v9fs_proto_dotu(v9ses))
118 && (v9ses->nodev == 0))
120 else if ((mode & P9_DMNAMEDPIPE) && (v9fs_proto_dotu(v9ses))
121 && (v9ses->nodev == 0))
123 else if ((mode & P9_DMDEVICE) && (v9fs_proto_dotu(v9ses))
124 && (v9ses->nodev == 0)) {
125 char type = 0, ext[32];
126 int major = -1, minor = -1;
128 strncpy(ext, stat->extension, sizeof(ext));
129 sscanf(ext, "%c %u %u", &type, &major, &minor);
138 p9_debug(P9_DEBUG_ERROR, "Unknown special type %c %s\n",
139 type, stat->extension);
141 *rdev = MKDEV(major, minor);
145 if (v9fs_proto_dotu(v9ses)) {
146 if ((mode & P9_DMSETUID) == P9_DMSETUID)
149 if ((mode & P9_DMSETGID) == P9_DMSETGID)
152 if ((mode & P9_DMSETVTX) == P9_DMSETVTX)
159 * v9fs_uflags2omode- convert posix open flags to plan 9 mode bits
160 * @uflags: flags to convert
161 * @extended: if .u extensions are active
164 int v9fs_uflags2omode(int uflags, int extended)
184 if (uflags & O_TRUNC)
191 if (uflags & O_APPEND)
199 * v9fs_blank_wstat - helper function to setup a 9P stat structure
200 * @wstat: structure to initialize
205 v9fs_blank_wstat(struct p9_wstat *wstat)
209 wstat->qid.type = ~0;
210 wstat->qid.version = ~0;
211 *((long long *)&wstat->qid.path) = ~0;
223 wstat->extension = NULL;
227 * v9fs_alloc_inode - helper function to allocate an inode
230 struct inode *v9fs_alloc_inode(struct super_block *sb)
232 struct v9fs_inode *v9inode;
233 v9inode = (struct v9fs_inode *)kmem_cache_alloc(v9fs_inode_cache,
237 #ifdef CONFIG_9P_FSCACHE
238 v9inode->fscache = NULL;
239 spin_lock_init(&v9inode->fscache_lock);
241 v9inode->writeback_fid = NULL;
242 v9inode->cache_validity = 0;
243 mutex_init(&v9inode->v_mutex);
244 return &v9inode->vfs_inode;
248 * v9fs_destroy_inode - destroy an inode
252 static void v9fs_i_callback(struct rcu_head *head)
254 struct inode *inode = container_of(head, struct inode, i_rcu);
255 INIT_LIST_HEAD(&inode->i_dentry);
256 kmem_cache_free(v9fs_inode_cache, V9FS_I(inode));
259 void v9fs_destroy_inode(struct inode *inode)
261 call_rcu(&inode->i_rcu, v9fs_i_callback);
264 int v9fs_init_inode(struct v9fs_session_info *v9ses,
265 struct inode *inode, int mode, dev_t rdev)
269 inode_init_owner(inode, NULL, mode);
271 inode->i_rdev = rdev;
272 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
273 inode->i_mapping->a_ops = &v9fs_addr_operations;
275 switch (mode & S_IFMT) {
280 if (v9fs_proto_dotl(v9ses)) {
281 inode->i_op = &v9fs_file_inode_operations_dotl;
282 } else if (v9fs_proto_dotu(v9ses)) {
283 inode->i_op = &v9fs_file_inode_operations;
285 p9_debug(P9_DEBUG_ERROR,
286 "special files without extended mode\n");
290 init_special_inode(inode, inode->i_mode, inode->i_rdev);
293 if (v9fs_proto_dotl(v9ses)) {
294 inode->i_op = &v9fs_file_inode_operations_dotl;
297 &v9fs_cached_file_operations_dotl;
299 inode->i_fop = &v9fs_file_operations_dotl;
301 inode->i_op = &v9fs_file_inode_operations;
303 inode->i_fop = &v9fs_cached_file_operations;
305 inode->i_fop = &v9fs_file_operations;
310 if (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)) {
311 p9_debug(P9_DEBUG_ERROR,
312 "extended modes used with legacy protocol\n");
317 if (v9fs_proto_dotl(v9ses))
318 inode->i_op = &v9fs_symlink_inode_operations_dotl;
320 inode->i_op = &v9fs_symlink_inode_operations;
325 if (v9fs_proto_dotl(v9ses))
326 inode->i_op = &v9fs_dir_inode_operations_dotl;
327 else if (v9fs_proto_dotu(v9ses))
328 inode->i_op = &v9fs_dir_inode_operations_dotu;
330 inode->i_op = &v9fs_dir_inode_operations;
332 if (v9fs_proto_dotl(v9ses))
333 inode->i_fop = &v9fs_dir_operations_dotl;
335 inode->i_fop = &v9fs_dir_operations;
339 p9_debug(P9_DEBUG_ERROR, "BAD mode 0x%x S_IFMT 0x%x\n",
340 mode, mode & S_IFMT);
350 * v9fs_get_inode - helper function to setup an inode
352 * @mode: mode to setup inode with
356 struct inode *v9fs_get_inode(struct super_block *sb, int mode, dev_t rdev)
360 struct v9fs_session_info *v9ses = sb->s_fs_info;
362 p9_debug(P9_DEBUG_VFS, "super block: %p mode: %o\n", sb, mode);
364 inode = new_inode(sb);
366 pr_warn("%s (%d): Problem allocating inode\n",
367 __func__, task_pid_nr(current));
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_debug(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_debug(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_debug(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_debug(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_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
661 err = p9_client_fcreate(ofid, name, perm, mode, extension);
663 p9_debug(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_debug(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_debug(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_debug(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_debug(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_debug(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_debug(P9_DEBUG_ERROR, "old dir and new dir are different\n");
983 v9fs_blank_wstat(&wstat);
984 wstat.muid = v9ses->uname;
985 wstat.name = (char *) new_dentry->d_name.name;
986 retval = p9_client_wstat(oldfid, &wstat);
991 if (S_ISDIR(new_inode->i_mode))
992 clear_nlink(new_inode);
994 drop_nlink(new_inode);
996 if (S_ISDIR(old_inode->i_mode)) {
1001 v9fs_invalidate_inode_attr(old_inode);
1002 v9fs_invalidate_inode_attr(old_dir);
1003 v9fs_invalidate_inode_attr(new_dir);
1005 /* successful rename */
1006 d_move(old_dentry, new_dentry);
1008 up_write(&v9ses->rename_sem);
1009 p9_client_clunk(newdirfid);
1012 p9_client_clunk(olddirfid);
1019 * v9fs_vfs_getattr - retrieve file metadata
1020 * @mnt: mount information
1021 * @dentry: file to get attributes on
1022 * @stat: metadata structure to populate
1027 v9fs_vfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
1031 struct v9fs_session_info *v9ses;
1033 struct p9_wstat *st;
1035 p9_debug(P9_DEBUG_VFS, "dentry: %p\n", dentry);
1037 v9ses = v9fs_dentry2v9ses(dentry);
1038 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
1039 generic_fillattr(dentry->d_inode, stat);
1042 fid = v9fs_fid_lookup(dentry);
1044 return PTR_ERR(fid);
1046 st = p9_client_stat(fid);
1050 v9fs_stat2inode(st, dentry->d_inode, dentry->d_inode->i_sb);
1051 generic_fillattr(dentry->d_inode, stat);
1059 * v9fs_vfs_setattr - set file metadata
1060 * @dentry: file whose metadata to set
1061 * @iattr: metadata assignment structure
1065 static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
1068 struct v9fs_session_info *v9ses;
1070 struct p9_wstat wstat;
1072 p9_debug(P9_DEBUG_VFS, "\n");
1073 retval = inode_change_ok(dentry->d_inode, iattr);
1078 v9ses = v9fs_dentry2v9ses(dentry);
1079 fid = v9fs_fid_lookup(dentry);
1081 return PTR_ERR(fid);
1083 v9fs_blank_wstat(&wstat);
1084 if (iattr->ia_valid & ATTR_MODE)
1085 wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
1087 if (iattr->ia_valid & ATTR_MTIME)
1088 wstat.mtime = iattr->ia_mtime.tv_sec;
1090 if (iattr->ia_valid & ATTR_ATIME)
1091 wstat.atime = iattr->ia_atime.tv_sec;
1093 if (iattr->ia_valid & ATTR_SIZE)
1094 wstat.length = iattr->ia_size;
1096 if (v9fs_proto_dotu(v9ses)) {
1097 if (iattr->ia_valid & ATTR_UID)
1098 wstat.n_uid = iattr->ia_uid;
1100 if (iattr->ia_valid & ATTR_GID)
1101 wstat.n_gid = iattr->ia_gid;
1104 /* Write all dirty data */
1105 if (S_ISREG(dentry->d_inode->i_mode))
1106 filemap_write_and_wait(dentry->d_inode->i_mapping);
1108 retval = p9_client_wstat(fid, &wstat);
1112 if ((iattr->ia_valid & ATTR_SIZE) &&
1113 iattr->ia_size != i_size_read(dentry->d_inode))
1114 truncate_setsize(dentry->d_inode, iattr->ia_size);
1116 v9fs_invalidate_inode_attr(dentry->d_inode);
1118 setattr_copy(dentry->d_inode, iattr);
1119 mark_inode_dirty(dentry->d_inode);
1124 * v9fs_stat2inode - populate an inode structure with mistat info
1125 * @stat: Plan 9 metadata (mistat) structure
1126 * @inode: inode to populate
1127 * @sb: superblock of filesystem
1132 v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode,
1133 struct super_block *sb)
1138 unsigned int i_nlink;
1139 struct v9fs_session_info *v9ses = sb->s_fs_info;
1140 struct v9fs_inode *v9inode = V9FS_I(inode);
1142 set_nlink(inode, 1);
1144 inode->i_atime.tv_sec = stat->atime;
1145 inode->i_mtime.tv_sec = stat->mtime;
1146 inode->i_ctime.tv_sec = stat->mtime;
1148 inode->i_uid = v9ses->dfltuid;
1149 inode->i_gid = v9ses->dfltgid;
1151 if (v9fs_proto_dotu(v9ses)) {
1152 inode->i_uid = stat->n_uid;
1153 inode->i_gid = stat->n_gid;
1155 if ((S_ISREG(inode->i_mode)) || (S_ISDIR(inode->i_mode))) {
1156 if (v9fs_proto_dotu(v9ses) && (stat->extension[0] != '\0')) {
1158 * Hadlink support got added later to
1159 * to the .u extension. So there can be
1160 * server out there that doesn't support
1161 * this even with .u extension. So check
1162 * for non NULL stat->extension
1164 strncpy(ext, stat->extension, sizeof(ext));
1165 /* HARDLINKCOUNT %u */
1166 sscanf(ext, "%13s %u", tag_name, &i_nlink);
1167 if (!strncmp(tag_name, "HARDLINKCOUNT", 13))
1168 set_nlink(inode, i_nlink);
1171 mode = stat->mode & S_IALLUGO;
1172 mode |= inode->i_mode & ~S_IALLUGO;
1173 inode->i_mode = mode;
1174 i_size_write(inode, stat->length);
1176 /* not real number of blocks, but 512 byte ones ... */
1177 inode->i_blocks = (i_size_read(inode) + 512 - 1) >> 9;
1178 v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR;
1182 * v9fs_qid2ino - convert qid into inode number
1185 * BUG: potential for inode number collisions?
1188 ino_t v9fs_qid2ino(struct p9_qid *qid)
1190 u64 path = qid->path + 2;
1193 if (sizeof(ino_t) == sizeof(path))
1194 memcpy(&i, &path, sizeof(ino_t));
1196 i = (ino_t) (path ^ (path >> 32));
1202 * v9fs_readlink - read a symlink's location (internal version)
1203 * @dentry: dentry for symlink
1204 * @buffer: buffer to load symlink location into
1205 * @buflen: length of buffer
1209 static int v9fs_readlink(struct dentry *dentry, char *buffer, int buflen)
1213 struct v9fs_session_info *v9ses;
1215 struct p9_wstat *st;
1217 p9_debug(P9_DEBUG_VFS, " %s\n", dentry->d_name.name);
1219 v9ses = v9fs_dentry2v9ses(dentry);
1220 fid = v9fs_fid_lookup(dentry);
1222 return PTR_ERR(fid);
1224 if (!v9fs_proto_dotu(v9ses))
1227 st = p9_client_stat(fid);
1231 if (!(st->mode & P9_DMSYMLINK)) {
1236 /* copy extension buffer into buffer */
1237 strncpy(buffer, st->extension, buflen);
1239 p9_debug(P9_DEBUG_VFS, "%s -> %s (%s)\n",
1240 dentry->d_name.name, st->extension, buffer);
1242 retval = strnlen(buffer, buflen);
1250 * v9fs_vfs_follow_link - follow a symlink path
1251 * @dentry: dentry for symlink
1256 static void *v9fs_vfs_follow_link(struct dentry *dentry, struct nameidata *nd)
1259 char *link = __getname();
1261 p9_debug(P9_DEBUG_VFS, "%s\n", dentry->d_name.name);
1264 link = ERR_PTR(-ENOMEM);
1266 len = v9fs_readlink(dentry, link, PATH_MAX);
1270 link = ERR_PTR(len);
1272 link[min(len, PATH_MAX-1)] = 0;
1274 nd_set_link(nd, link);
1280 * v9fs_vfs_put_link - release a symlink path
1281 * @dentry: dentry for symlink
1288 v9fs_vfs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
1290 char *s = nd_get_link(nd);
1292 p9_debug(P9_DEBUG_VFS, " %s %s\n",
1293 dentry->d_name.name, IS_ERR(s) ? "<error>" : s);
1299 * v9fs_vfs_mkspecial - create a special file
1300 * @dir: inode to create special file in
1301 * @dentry: dentry to create
1302 * @mode: mode to create special file
1303 * @extension: 9p2000.u format extension string representing special file
1307 static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
1308 int mode, const char *extension)
1312 struct v9fs_session_info *v9ses;
1314 v9ses = v9fs_inode2v9ses(dir);
1315 if (!v9fs_proto_dotu(v9ses)) {
1316 p9_debug(P9_DEBUG_ERROR, "not extended\n");
1320 perm = unixmode2p9mode(v9ses, mode);
1321 fid = v9fs_create(v9ses, dir, dentry, (char *) extension, perm,
1324 return PTR_ERR(fid);
1326 v9fs_invalidate_inode_attr(dir);
1327 p9_client_clunk(fid);
1332 * v9fs_vfs_symlink - helper function to create symlinks
1333 * @dir: directory inode containing symlink
1334 * @dentry: dentry for symlink
1335 * @symname: symlink data
1337 * See Also: 9P2000.u RFC for more information
1342 v9fs_vfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1344 p9_debug(P9_DEBUG_VFS, " %lu,%s,%s\n",
1345 dir->i_ino, dentry->d_name.name, symname);
1347 return v9fs_vfs_mkspecial(dir, dentry, S_IFLNK, symname);
1351 * v9fs_vfs_link - create a hardlink
1352 * @old_dentry: dentry for file to link to
1353 * @dir: inode destination for new link
1354 * @dentry: dentry for link
1359 v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
1360 struct dentry *dentry)
1364 struct p9_fid *oldfid;
1366 p9_debug(P9_DEBUG_VFS, " %lu,%s,%s\n",
1367 dir->i_ino, dentry->d_name.name, old_dentry->d_name.name);
1369 oldfid = v9fs_fid_clone(old_dentry);
1371 return PTR_ERR(oldfid);
1374 if (unlikely(!name)) {
1379 sprintf(name, "%d\n", oldfid->fid);
1380 retval = v9fs_vfs_mkspecial(dir, dentry, P9_DMLINK, name);
1383 v9fs_refresh_inode(oldfid, old_dentry->d_inode);
1384 v9fs_invalidate_inode_attr(dir);
1387 p9_client_clunk(oldfid);
1392 * v9fs_vfs_mknod - create a special file
1393 * @dir: inode destination for new link
1394 * @dentry: dentry for file
1395 * @mode: mode for creation
1396 * @rdev: device associated with special file
1401 v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1406 p9_debug(P9_DEBUG_VFS, " %lu,%s mode: %x MAJOR: %u MINOR: %u\n",
1407 dir->i_ino, dentry->d_name.name, mode,
1408 MAJOR(rdev), MINOR(rdev));
1410 if (!new_valid_dev(rdev))
1416 /* build extension */
1418 sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
1419 else if (S_ISCHR(mode))
1420 sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
1421 else if (S_ISFIFO(mode))
1423 else if (S_ISSOCK(mode))
1430 retval = v9fs_vfs_mkspecial(dir, dentry, mode, name);
1436 int v9fs_refresh_inode(struct p9_fid *fid, struct inode *inode)
1441 struct p9_wstat *st;
1442 struct v9fs_session_info *v9ses;
1444 v9ses = v9fs_inode2v9ses(inode);
1445 st = p9_client_stat(fid);
1449 * Don't update inode if the file type is different
1451 umode = p9mode2unixmode(v9ses, st, &rdev);
1452 if ((inode->i_mode & S_IFMT) != (umode & S_IFMT))
1455 spin_lock(&inode->i_lock);
1457 * We don't want to refresh inode->i_size,
1458 * because we may have cached data
1460 i_size = inode->i_size;
1461 v9fs_stat2inode(st, inode, inode->i_sb);
1463 inode->i_size = i_size;
1464 spin_unlock(&inode->i_lock);
1471 static const struct inode_operations v9fs_dir_inode_operations_dotu = {
1472 .create = v9fs_vfs_create,
1473 .lookup = v9fs_vfs_lookup,
1474 .symlink = v9fs_vfs_symlink,
1475 .link = v9fs_vfs_link,
1476 .unlink = v9fs_vfs_unlink,
1477 .mkdir = v9fs_vfs_mkdir,
1478 .rmdir = v9fs_vfs_rmdir,
1479 .mknod = v9fs_vfs_mknod,
1480 .rename = v9fs_vfs_rename,
1481 .getattr = v9fs_vfs_getattr,
1482 .setattr = v9fs_vfs_setattr,
1485 static const struct inode_operations v9fs_dir_inode_operations = {
1486 .create = v9fs_vfs_create,
1487 .lookup = v9fs_vfs_lookup,
1488 .unlink = v9fs_vfs_unlink,
1489 .mkdir = v9fs_vfs_mkdir,
1490 .rmdir = v9fs_vfs_rmdir,
1491 .mknod = v9fs_vfs_mknod,
1492 .rename = v9fs_vfs_rename,
1493 .getattr = v9fs_vfs_getattr,
1494 .setattr = v9fs_vfs_setattr,
1497 static const struct inode_operations v9fs_file_inode_operations = {
1498 .getattr = v9fs_vfs_getattr,
1499 .setattr = v9fs_vfs_setattr,
1502 static const struct inode_operations v9fs_symlink_inode_operations = {
1503 .readlink = generic_readlink,
1504 .follow_link = v9fs_vfs_follow_link,
1505 .put_link = v9fs_vfs_put_link,
1506 .getattr = v9fs_vfs_getattr,
1507 .setattr = v9fs_vfs_setattr,