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 u32 unixmode2p9mode(struct v9fs_session_info *v9ses, umode_t mode)
68 if (v9fs_proto_dotu(v9ses)) {
69 if (v9ses->nodev == 0) {
73 res |= P9_DMNAMEDPIPE;
80 if ((mode & S_ISUID) == S_ISUID)
82 if ((mode & S_ISGID) == S_ISGID)
84 if ((mode & S_ISVTX) == S_ISVTX)
91 * p9mode2unixmode- convert plan9 mode bits to unix mode bits
92 * @v9ses: v9fs session information
93 * @stat: p9_wstat from which mode need to be derived
94 * @rdev: major number, minor number in case of device files.
97 static umode_t p9mode2unixmode(struct v9fs_session_info *v9ses,
98 struct p9_wstat *stat, dev_t *rdev)
101 u32 mode = stat->mode;
103 res = mode & S_IALLUGO;
106 if ((mode & P9_DMDIR) == P9_DMDIR)
108 else if ((mode & P9_DMSYMLINK) && (v9fs_proto_dotu(v9ses)))
110 else if ((mode & P9_DMSOCKET) && (v9fs_proto_dotu(v9ses))
111 && (v9ses->nodev == 0))
113 else if ((mode & P9_DMNAMEDPIPE) && (v9fs_proto_dotu(v9ses))
114 && (v9ses->nodev == 0))
116 else if ((mode & P9_DMDEVICE) && (v9fs_proto_dotu(v9ses))
117 && (v9ses->nodev == 0)) {
118 char type = 0, ext[32];
119 int major = -1, minor = -1;
121 strncpy(ext, stat->extension, sizeof(ext));
122 sscanf(ext, "%c %u %u", &type, &major, &minor);
131 P9_DPRINTK(P9_DEBUG_ERROR,
132 "Unknown special type %c %s\n", type,
135 *rdev = MKDEV(major, minor);
139 if (v9fs_proto_dotu(v9ses)) {
140 if ((mode & P9_DMSETUID) == P9_DMSETUID)
143 if ((mode & P9_DMSETGID) == P9_DMSETGID)
146 if ((mode & P9_DMSETVTX) == P9_DMSETVTX)
153 * v9fs_uflags2omode- convert posix open flags to plan 9 mode bits
154 * @uflags: flags to convert
155 * @extended: if .u extensions are active
158 int v9fs_uflags2omode(int uflags, int extended)
178 if (uflags & O_TRUNC)
185 if (uflags & O_APPEND)
193 * v9fs_blank_wstat - helper function to setup a 9P stat structure
194 * @wstat: structure to initialize
199 v9fs_blank_wstat(struct p9_wstat *wstat)
203 wstat->qid.type = ~0;
204 wstat->qid.version = ~0;
205 *((long long *)&wstat->qid.path) = ~0;
217 wstat->extension = NULL;
221 * v9fs_alloc_inode - helper function to allocate an inode
224 struct inode *v9fs_alloc_inode(struct super_block *sb)
226 struct v9fs_inode *v9inode;
227 v9inode = (struct v9fs_inode *)kmem_cache_alloc(v9fs_inode_cache,
231 #ifdef CONFIG_9P_FSCACHE
232 v9inode->fscache = NULL;
233 spin_lock_init(&v9inode->fscache_lock);
235 v9inode->writeback_fid = NULL;
236 v9inode->cache_validity = 0;
237 mutex_init(&v9inode->v_mutex);
238 return &v9inode->vfs_inode;
242 * v9fs_destroy_inode - destroy an inode
246 static void v9fs_i_callback(struct rcu_head *head)
248 struct inode *inode = container_of(head, struct inode, i_rcu);
249 kmem_cache_free(v9fs_inode_cache, V9FS_I(inode));
252 void v9fs_destroy_inode(struct inode *inode)
254 call_rcu(&inode->i_rcu, v9fs_i_callback);
257 int v9fs_init_inode(struct v9fs_session_info *v9ses,
258 struct inode *inode, umode_t mode, dev_t rdev)
262 inode_init_owner(inode, NULL, mode);
264 inode->i_rdev = rdev;
265 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
266 inode->i_mapping->a_ops = &v9fs_addr_operations;
268 switch (mode & S_IFMT) {
273 if (v9fs_proto_dotl(v9ses)) {
274 inode->i_op = &v9fs_file_inode_operations_dotl;
275 } else if (v9fs_proto_dotu(v9ses)) {
276 inode->i_op = &v9fs_file_inode_operations;
278 P9_DPRINTK(P9_DEBUG_ERROR,
279 "special files without extended mode\n");
283 init_special_inode(inode, inode->i_mode, inode->i_rdev);
286 if (v9fs_proto_dotl(v9ses)) {
287 inode->i_op = &v9fs_file_inode_operations_dotl;
290 &v9fs_cached_file_operations_dotl;
292 inode->i_fop = &v9fs_file_operations_dotl;
294 inode->i_op = &v9fs_file_inode_operations;
296 inode->i_fop = &v9fs_cached_file_operations;
298 inode->i_fop = &v9fs_file_operations;
303 if (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)) {
304 P9_DPRINTK(P9_DEBUG_ERROR, "extended modes used with "
305 "legacy protocol.\n");
310 if (v9fs_proto_dotl(v9ses))
311 inode->i_op = &v9fs_symlink_inode_operations_dotl;
313 inode->i_op = &v9fs_symlink_inode_operations;
318 if (v9fs_proto_dotl(v9ses))
319 inode->i_op = &v9fs_dir_inode_operations_dotl;
320 else if (v9fs_proto_dotu(v9ses))
321 inode->i_op = &v9fs_dir_inode_operations_dotu;
323 inode->i_op = &v9fs_dir_inode_operations;
325 if (v9fs_proto_dotl(v9ses))
326 inode->i_fop = &v9fs_dir_operations_dotl;
328 inode->i_fop = &v9fs_dir_operations;
332 P9_DPRINTK(P9_DEBUG_ERROR, "BAD mode 0x%hx S_IFMT 0x%x\n",
333 mode, mode & S_IFMT);
343 * v9fs_get_inode - helper function to setup an inode
345 * @mode: mode to setup inode with
349 struct inode *v9fs_get_inode(struct super_block *sb, umode_t mode, dev_t rdev)
353 struct v9fs_session_info *v9ses = sb->s_fs_info;
355 P9_DPRINTK(P9_DEBUG_VFS, "super block: %p mode: %ho\n", sb, mode);
357 inode = new_inode(sb);
359 P9_EPRINTK(KERN_WARNING, "Problem allocating inode\n");
360 return ERR_PTR(-ENOMEM);
362 err = v9fs_init_inode(v9ses, inode, mode, rdev);
371 static struct v9fs_fid*
372 v9fs_clone_walk(struct v9fs_session_info *v9ses, u32 fid, struct dentry *dentry)
376 struct v9fs_fid *ret;
377 struct v9fs_fcall *fcall;
379 nfid = v9fs_get_idpool(&v9ses->fidpool);
381 eprintk(KERN_WARNING, "no free fids available\n");
382 return ERR_PTR(-ENOSPC);
385 err = v9fs_t_walk(v9ses, fid, nfid, (char *) dentry->d_name.name,
389 if (fcall && fcall->id == RWALK)
392 PRINT_FCALL_ERROR("walk error", fcall);
393 v9fs_put_idpool(nfid, &v9ses->fidpool);
399 ret = v9fs_fid_create(v9ses, nfid);
405 err = v9fs_fid_insert(ret, dentry);
407 v9fs_fid_destroy(ret);
414 v9fs_t_clunk(v9ses, nfid);
424 * v9fs_clear_inode - release an inode
425 * @inode: inode to release
428 void v9fs_evict_inode(struct inode *inode)
430 struct v9fs_inode *v9inode = V9FS_I(inode);
432 truncate_inode_pages(inode->i_mapping, 0);
433 end_writeback(inode);
434 filemap_fdatawrite(inode->i_mapping);
436 #ifdef CONFIG_9P_FSCACHE
437 v9fs_cache_inode_put_cookie(inode);
439 /* clunk the fid stashed in writeback_fid */
440 if (v9inode->writeback_fid) {
441 p9_client_clunk(v9inode->writeback_fid);
442 v9inode->writeback_fid = NULL;
446 static int v9fs_test_inode(struct inode *inode, void *data)
450 struct v9fs_inode *v9inode = V9FS_I(inode);
451 struct p9_wstat *st = (struct p9_wstat *)data;
452 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
454 umode = p9mode2unixmode(v9ses, st, &rdev);
455 /* don't match inode of different type */
456 if ((inode->i_mode & S_IFMT) != (umode & S_IFMT))
459 /* compare qid details */
460 if (memcmp(&v9inode->qid.version,
461 &st->qid.version, sizeof(v9inode->qid.version)))
464 if (v9inode->qid.type != st->qid.type)
469 static int v9fs_test_new_inode(struct inode *inode, void *data)
474 static int v9fs_set_inode(struct inode *inode, void *data)
476 struct v9fs_inode *v9inode = V9FS_I(inode);
477 struct p9_wstat *st = (struct p9_wstat *)data;
479 memcpy(&v9inode->qid, &st->qid, sizeof(st->qid));
483 static struct inode *v9fs_qid_iget(struct super_block *sb,
493 struct v9fs_session_info *v9ses = sb->s_fs_info;
494 int (*test)(struct inode *, void *);
497 test = v9fs_test_new_inode;
499 test = v9fs_test_inode;
501 i_ino = v9fs_qid2ino(qid);
502 inode = iget5_locked(sb, i_ino, test, v9fs_set_inode, st);
504 return ERR_PTR(-ENOMEM);
505 if (!(inode->i_state & I_NEW))
508 * initialize the inode with the stat info
509 * FIXME!! we may need support for stale inodes
512 inode->i_ino = i_ino;
513 umode = p9mode2unixmode(v9ses, st, &rdev);
514 retval = v9fs_init_inode(v9ses, inode, umode, rdev);
518 v9fs_stat2inode(st, inode, sb);
519 #ifdef CONFIG_9P_FSCACHE
520 v9fs_cache_inode_get_cookie(inode);
522 unlock_new_inode(inode);
525 unlock_new_inode(inode);
527 return ERR_PTR(retval);
532 v9fs_inode_from_fid(struct v9fs_session_info *v9ses, struct p9_fid *fid,
533 struct super_block *sb, int new)
536 struct inode *inode = NULL;
538 st = p9_client_stat(fid);
542 inode = v9fs_qid_iget(sb, &st->qid, st, new);
549 * v9fs_at_to_dotl_flags- convert Linux specific AT flags to
551 * @flags: flags to convert
553 static int v9fs_at_to_dotl_flags(int flags)
556 if (flags & AT_REMOVEDIR)
557 rflags |= P9_DOTL_AT_REMOVEDIR;
562 * v9fs_remove - helper function to remove files and directories
563 * @dir: directory inode that is being deleted
564 * @dentry: dentry that is being deleted
565 * @rmdir: removing a directory
569 static int v9fs_remove(struct inode *dir, struct dentry *dentry, int flags)
572 int retval = -EOPNOTSUPP;
573 struct p9_fid *v9fid, *dfid;
574 struct v9fs_session_info *v9ses;
576 P9_DPRINTK(P9_DEBUG_VFS, "inode: %p dentry: %p rmdir: %x\n",
579 v9ses = v9fs_inode2v9ses(dir);
580 inode = dentry->d_inode;
581 dfid = v9fs_fid_lookup(dentry->d_parent);
583 retval = PTR_ERR(dfid);
584 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", retval);
587 if (v9fs_proto_dotl(v9ses))
588 retval = p9_client_unlinkat(dfid, dentry->d_name.name,
589 v9fs_at_to_dotl_flags(flags));
590 if (retval == -EOPNOTSUPP) {
591 /* Try the one based on path */
592 v9fid = v9fs_fid_clone(dentry);
594 return PTR_ERR(v9fid);
595 retval = p9_client_remove(v9fid);
599 * directories on unlink should have zero
602 if (flags & AT_REMOVEDIR) {
608 v9fs_invalidate_inode_attr(inode);
609 v9fs_invalidate_inode_attr(dir);
615 * v9fs_create - Create a file
616 * @v9ses: session information
617 * @dir: directory that dentry is being created in
618 * @dentry: dentry that is being created
619 * @extension: 9p2000.u extension string to support devices, etc.
620 * @perm: create permissions
624 static struct p9_fid *
625 v9fs_create(struct v9fs_session_info *v9ses, struct inode *dir,
626 struct dentry *dentry, char *extension, u32 perm, u8 mode)
630 struct p9_fid *dfid, *ofid, *fid;
633 P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
638 name = (char *) dentry->d_name.name;
639 dfid = v9fs_fid_lookup(dentry->d_parent);
642 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
646 /* clone a fid to use for creation */
647 ofid = p9_client_walk(dfid, 0, NULL, 1);
650 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
654 err = p9_client_fcreate(ofid, name, perm, mode, extension);
656 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_fcreate failed %d\n", err);
660 /* now walk from the parent so we can get unopened fid */
661 fid = p9_client_walk(dfid, 1, &name, 1);
664 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
669 /* instantiate inode and assign the unopened fid to the dentry */
670 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
672 err = PTR_ERR(inode);
673 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n", err);
676 err = v9fs_fid_add(dentry, fid);
679 d_instantiate(dentry, inode);
683 p9_client_clunk(ofid);
686 p9_client_clunk(fid);
692 * v9fs_vfs_create - VFS hook to create files
693 * @dir: directory inode that is being created
694 * @dentry: dentry that is being deleted
695 * @mode: create permissions
696 * @nd: path information
701 v9fs_vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
702 struct nameidata *nd)
708 struct v9fs_inode *v9inode;
709 struct v9fs_session_info *v9ses;
710 struct p9_fid *fid, *inode_fid;
714 v9ses = v9fs_inode2v9ses(dir);
715 perm = unixmode2p9mode(v9ses, mode);
717 flags = nd->intent.open.flags;
721 fid = v9fs_create(v9ses, dir, dentry, NULL, perm,
722 v9fs_uflags2omode(flags,
723 v9fs_proto_dotu(v9ses)));
730 v9fs_invalidate_inode_attr(dir);
731 /* if we are opening a file, assign the open fid to the file */
733 v9inode = V9FS_I(dentry->d_inode);
734 mutex_lock(&v9inode->v_mutex);
735 if (v9ses->cache && !v9inode->writeback_fid &&
736 ((flags & O_ACCMODE) != O_RDONLY)) {
738 * clone a fid and add it to writeback_fid
739 * we do it during open time instead of
740 * page dirty time via write_begin/page_mkwrite
741 * because we want write after unlink usecase
744 inode_fid = v9fs_writeback_fid(dentry);
745 if (IS_ERR(inode_fid)) {
746 err = PTR_ERR(inode_fid);
747 mutex_unlock(&v9inode->v_mutex);
750 v9inode->writeback_fid = (void *) inode_fid;
752 mutex_unlock(&v9inode->v_mutex);
753 filp = lookup_instantiate_filp(nd, dentry, generic_file_open);
759 filp->private_data = fid;
760 #ifdef CONFIG_9P_FSCACHE
762 v9fs_cache_inode_set_cookie(dentry->d_inode, filp);
765 p9_client_clunk(fid);
771 p9_client_clunk(fid);
777 * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
778 * @dir: inode that is being unlinked
779 * @dentry: dentry that is being unlinked
780 * @mode: mode for new directory
784 static int v9fs_vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
789 struct v9fs_session_info *v9ses;
791 P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
793 v9ses = v9fs_inode2v9ses(dir);
794 perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
795 fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_OREAD);
801 v9fs_invalidate_inode_attr(dir);
805 p9_client_clunk(fid);
811 * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
812 * @dir: inode that is being walked from
813 * @dentry: dentry that is being walked to?
814 * @nameidata: path data
818 struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
819 struct nameidata *nameidata)
822 struct super_block *sb;
823 struct v9fs_session_info *v9ses;
824 struct p9_fid *dfid, *fid;
829 P9_DPRINTK(P9_DEBUG_VFS, "dir: %p dentry: (%s) %p nameidata: %p\n",
830 dir, dentry->d_name.name, dentry, nameidata);
832 if (dentry->d_name.len > NAME_MAX)
833 return ERR_PTR(-ENAMETOOLONG);
836 v9ses = v9fs_inode2v9ses(dir);
837 /* We can walk d_parent because we hold the dir->i_mutex */
838 dfid = v9fs_fid_lookup(dentry->d_parent);
840 return ERR_CAST(dfid);
842 name = (char *) dentry->d_name.name;
843 fid = p9_client_walk(dfid, 1, &name, 1);
845 result = PTR_ERR(fid);
846 if (result == -ENOENT) {
851 return ERR_PTR(result);
854 * Make sure we don't use a wrong inode due to parallel
855 * unlink. For cached mode create calls request for new
856 * inode. But with cache disabled, lookup should do this.
859 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
861 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
863 result = PTR_ERR(inode);
867 result = v9fs_fid_add(dentry, fid);
872 * If we had a rename on the server and a parallel lookup
873 * for the new name, then make sure we instantiate with
874 * the new name. ie look up for a/b, while on server somebody
875 * moved b under k and client parallely did a lookup for
878 res = d_materialise_unique(dentry, inode);
881 result = PTR_ERR(res);
885 p9_client_clunk(fid);
887 return ERR_PTR(result);
891 * v9fs_vfs_unlink - VFS unlink hook to delete an inode
892 * @i: inode that is being unlinked
893 * @d: dentry that is being unlinked
897 int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
899 return v9fs_remove(i, d, 0);
903 * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
904 * @i: inode that is being unlinked
905 * @d: dentry that is being unlinked
909 int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
911 return v9fs_remove(i, d, AT_REMOVEDIR);
915 * v9fs_vfs_rename - VFS hook to rename an inode
916 * @old_dir: old dir inode
917 * @old_dentry: old dentry
918 * @new_dir: new dir inode
919 * @new_dentry: new dentry
924 v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
925 struct inode *new_dir, struct dentry *new_dentry)
928 struct inode *old_inode;
929 struct inode *new_inode;
930 struct v9fs_session_info *v9ses;
931 struct p9_fid *oldfid;
932 struct p9_fid *olddirfid;
933 struct p9_fid *newdirfid;
934 struct p9_wstat wstat;
936 P9_DPRINTK(P9_DEBUG_VFS, "\n");
938 old_inode = old_dentry->d_inode;
939 new_inode = new_dentry->d_inode;
940 v9ses = v9fs_inode2v9ses(old_inode);
941 oldfid = v9fs_fid_lookup(old_dentry);
943 return PTR_ERR(oldfid);
945 olddirfid = v9fs_fid_clone(old_dentry->d_parent);
946 if (IS_ERR(olddirfid)) {
947 retval = PTR_ERR(olddirfid);
951 newdirfid = v9fs_fid_clone(new_dentry->d_parent);
952 if (IS_ERR(newdirfid)) {
953 retval = PTR_ERR(newdirfid);
957 down_write(&v9ses->rename_sem);
958 if (v9fs_proto_dotl(v9ses)) {
959 retval = p9_client_renameat(olddirfid, old_dentry->d_name.name,
960 newdirfid, new_dentry->d_name.name);
961 if (retval == -EOPNOTSUPP)
962 retval = p9_client_rename(oldfid, newdirfid,
963 new_dentry->d_name.name);
964 if (retval != -EOPNOTSUPP)
967 if (old_dentry->d_parent != new_dentry->d_parent) {
969 * 9P .u can only handle file rename in the same directory
972 P9_DPRINTK(P9_DEBUG_ERROR,
973 "old dir and new dir are different\n");
977 v9fs_blank_wstat(&wstat);
978 wstat.muid = v9ses->uname;
979 wstat.name = (char *) new_dentry->d_name.name;
980 retval = p9_client_wstat(oldfid, &wstat);
985 if (S_ISDIR(new_inode->i_mode))
986 clear_nlink(new_inode);
988 drop_nlink(new_inode);
990 if (S_ISDIR(old_inode->i_mode)) {
995 v9fs_invalidate_inode_attr(old_inode);
996 v9fs_invalidate_inode_attr(old_dir);
997 v9fs_invalidate_inode_attr(new_dir);
999 /* successful rename */
1000 d_move(old_dentry, new_dentry);
1002 up_write(&v9ses->rename_sem);
1003 p9_client_clunk(newdirfid);
1006 p9_client_clunk(olddirfid);
1013 * v9fs_vfs_getattr - retrieve file metadata
1014 * @mnt: mount information
1015 * @dentry: file to get attributes on
1016 * @stat: metadata structure to populate
1021 v9fs_vfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
1025 struct v9fs_session_info *v9ses;
1027 struct p9_wstat *st;
1029 P9_DPRINTK(P9_DEBUG_VFS, "dentry: %p\n", dentry);
1031 v9ses = v9fs_dentry2v9ses(dentry);
1032 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
1033 generic_fillattr(dentry->d_inode, stat);
1036 fid = v9fs_fid_lookup(dentry);
1038 return PTR_ERR(fid);
1040 st = p9_client_stat(fid);
1044 v9fs_stat2inode(st, dentry->d_inode, dentry->d_inode->i_sb);
1045 generic_fillattr(dentry->d_inode, stat);
1053 * v9fs_vfs_setattr - set file metadata
1054 * @dentry: file whose metadata to set
1055 * @iattr: metadata assignment structure
1059 static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
1062 struct v9fs_session_info *v9ses;
1064 struct p9_wstat wstat;
1066 P9_DPRINTK(P9_DEBUG_VFS, "\n");
1067 retval = inode_change_ok(dentry->d_inode, iattr);
1072 v9ses = v9fs_dentry2v9ses(dentry);
1073 fid = v9fs_fid_lookup(dentry);
1075 return PTR_ERR(fid);
1077 v9fs_blank_wstat(&wstat);
1078 if (iattr->ia_valid & ATTR_MODE)
1079 wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
1081 if (iattr->ia_valid & ATTR_MTIME)
1082 wstat.mtime = iattr->ia_mtime.tv_sec;
1084 if (iattr->ia_valid & ATTR_ATIME)
1085 wstat.atime = iattr->ia_atime.tv_sec;
1087 if (iattr->ia_valid & ATTR_SIZE)
1088 wstat.length = iattr->ia_size;
1090 if (v9fs_proto_dotu(v9ses)) {
1091 if (iattr->ia_valid & ATTR_UID)
1092 wstat.n_uid = iattr->ia_uid;
1094 if (iattr->ia_valid & ATTR_GID)
1095 wstat.n_gid = iattr->ia_gid;
1098 /* Write all dirty data */
1099 if (S_ISREG(dentry->d_inode->i_mode))
1100 filemap_write_and_wait(dentry->d_inode->i_mapping);
1102 retval = p9_client_wstat(fid, &wstat);
1106 if ((iattr->ia_valid & ATTR_SIZE) &&
1107 iattr->ia_size != i_size_read(dentry->d_inode))
1108 truncate_setsize(dentry->d_inode, iattr->ia_size);
1110 v9fs_invalidate_inode_attr(dentry->d_inode);
1112 setattr_copy(dentry->d_inode, iattr);
1113 mark_inode_dirty(dentry->d_inode);
1118 * v9fs_stat2inode - populate an inode structure with mistat info
1119 * @stat: Plan 9 metadata (mistat) structure
1120 * @inode: inode to populate
1121 * @sb: superblock of filesystem
1126 v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode,
1127 struct super_block *sb)
1132 unsigned int i_nlink;
1133 struct v9fs_session_info *v9ses = sb->s_fs_info;
1134 struct v9fs_inode *v9inode = V9FS_I(inode);
1136 set_nlink(inode, 1);
1138 inode->i_atime.tv_sec = stat->atime;
1139 inode->i_mtime.tv_sec = stat->mtime;
1140 inode->i_ctime.tv_sec = stat->mtime;
1142 inode->i_uid = v9ses->dfltuid;
1143 inode->i_gid = v9ses->dfltgid;
1145 if (v9fs_proto_dotu(v9ses)) {
1146 inode->i_uid = stat->n_uid;
1147 inode->i_gid = stat->n_gid;
1149 if ((S_ISREG(inode->i_mode)) || (S_ISDIR(inode->i_mode))) {
1150 if (v9fs_proto_dotu(v9ses) && (stat->extension[0] != '\0')) {
1152 * Hadlink support got added later to
1153 * to the .u extension. So there can be
1154 * server out there that doesn't support
1155 * this even with .u extension. So check
1156 * for non NULL stat->extension
1158 strncpy(ext, stat->extension, sizeof(ext));
1159 /* HARDLINKCOUNT %u */
1160 sscanf(ext, "%13s %u", tag_name, &i_nlink);
1161 if (!strncmp(tag_name, "HARDLINKCOUNT", 13))
1162 set_nlink(inode, i_nlink);
1165 mode = stat->mode & S_IALLUGO;
1166 mode |= inode->i_mode & ~S_IALLUGO;
1167 inode->i_mode = mode;
1168 i_size_write(inode, stat->length);
1170 /* not real number of blocks, but 512 byte ones ... */
1171 inode->i_blocks = (i_size_read(inode) + 512 - 1) >> 9;
1172 v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR;
1176 * v9fs_qid2ino - convert qid into inode number
1179 * BUG: potential for inode number collisions?
1182 ino_t v9fs_qid2ino(struct p9_qid *qid)
1184 u64 path = qid->path + 2;
1187 if (sizeof(ino_t) == sizeof(path))
1188 memcpy(&i, &path, sizeof(ino_t));
1190 i = (ino_t) (path ^ (path >> 32));
1196 * v9fs_readlink - read a symlink's location (internal version)
1197 * @dentry: dentry for symlink
1198 * @buffer: buffer to load symlink location into
1199 * @buflen: length of buffer
1203 static int v9fs_readlink(struct dentry *dentry, char *buffer, int buflen)
1207 struct v9fs_session_info *v9ses;
1209 struct p9_wstat *st;
1211 P9_DPRINTK(P9_DEBUG_VFS, " %s\n", dentry->d_name.name);
1213 v9ses = v9fs_dentry2v9ses(dentry);
1214 fid = v9fs_fid_lookup(dentry);
1216 return PTR_ERR(fid);
1218 if (!v9fs_proto_dotu(v9ses))
1221 st = p9_client_stat(fid);
1225 if (!(st->mode & P9_DMSYMLINK)) {
1230 /* copy extension buffer into buffer */
1231 strncpy(buffer, st->extension, buflen);
1233 P9_DPRINTK(P9_DEBUG_VFS,
1234 "%s -> %s (%s)\n", dentry->d_name.name, st->extension, buffer);
1236 retval = strnlen(buffer, buflen);
1244 * v9fs_vfs_follow_link - follow a symlink path
1245 * @dentry: dentry for symlink
1250 static void *v9fs_vfs_follow_link(struct dentry *dentry, struct nameidata *nd)
1253 char *link = __getname();
1255 P9_DPRINTK(P9_DEBUG_VFS, "%s n", dentry->d_name.name);
1258 link = ERR_PTR(-ENOMEM);
1260 len = v9fs_readlink(dentry, link, PATH_MAX);
1264 link = ERR_PTR(len);
1266 link[min(len, PATH_MAX-1)] = 0;
1268 nd_set_link(nd, link);
1274 * v9fs_vfs_put_link - release a symlink path
1275 * @dentry: dentry for symlink
1282 v9fs_vfs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
1284 char *s = nd_get_link(nd);
1286 P9_DPRINTK(P9_DEBUG_VFS, " %s %s\n", dentry->d_name.name,
1287 IS_ERR(s) ? "<error>" : s);
1293 * v9fs_vfs_mkspecial - create a special file
1294 * @dir: inode to create special file in
1295 * @dentry: dentry to create
1296 * @mode: mode to create special file
1297 * @extension: 9p2000.u format extension string representing special file
1301 static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
1302 u32 perm, const char *extension)
1305 struct v9fs_session_info *v9ses;
1307 v9ses = v9fs_inode2v9ses(dir);
1308 if (!v9fs_proto_dotu(v9ses)) {
1309 P9_DPRINTK(P9_DEBUG_ERROR, "not extended\n");
1313 fid = v9fs_create(v9ses, dir, dentry, (char *) extension, perm,
1316 return PTR_ERR(fid);
1318 v9fs_invalidate_inode_attr(dir);
1319 p9_client_clunk(fid);
1324 * v9fs_vfs_symlink - helper function to create symlinks
1325 * @dir: directory inode containing symlink
1326 * @dentry: dentry for symlink
1327 * @symname: symlink data
1329 * See Also: 9P2000.u RFC for more information
1334 v9fs_vfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1336 P9_DPRINTK(P9_DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino,
1337 dentry->d_name.name, symname);
1339 return v9fs_vfs_mkspecial(dir, dentry, P9_DMSYMLINK, symname);
1343 * v9fs_vfs_link - create a hardlink
1344 * @old_dentry: dentry for file to link to
1345 * @dir: inode destination for new link
1346 * @dentry: dentry for link
1351 v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
1352 struct dentry *dentry)
1356 struct p9_fid *oldfid;
1358 P9_DPRINTK(P9_DEBUG_VFS,
1359 " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
1360 old_dentry->d_name.name);
1362 oldfid = v9fs_fid_clone(old_dentry);
1364 return PTR_ERR(oldfid);
1367 if (unlikely(!name)) {
1372 sprintf(name, "%d\n", oldfid->fid);
1373 retval = v9fs_vfs_mkspecial(dir, dentry, P9_DMLINK, name);
1376 v9fs_refresh_inode(oldfid, old_dentry->d_inode);
1377 v9fs_invalidate_inode_attr(dir);
1380 p9_client_clunk(oldfid);
1385 * v9fs_vfs_mknod - create a special file
1386 * @dir: inode destination for new link
1387 * @dentry: dentry for file
1388 * @mode: mode for creation
1389 * @rdev: device associated with special file
1394 v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
1396 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
1401 P9_DPRINTK(P9_DEBUG_VFS,
1402 " %lu,%s mode: %hx MAJOR: %u MINOR: %u\n", dir->i_ino,
1403 dentry->d_name.name, mode, MAJOR(rdev), MINOR(rdev));
1405 if (!new_valid_dev(rdev))
1411 /* build extension */
1413 sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
1414 else if (S_ISCHR(mode))
1415 sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
1416 else if (S_ISFIFO(mode))
1418 else if (S_ISSOCK(mode))
1425 perm = unixmode2p9mode(v9ses, mode);
1426 retval = v9fs_vfs_mkspecial(dir, dentry, perm, name);
1432 int v9fs_refresh_inode(struct p9_fid *fid, struct inode *inode)
1437 struct p9_wstat *st;
1438 struct v9fs_session_info *v9ses;
1440 v9ses = v9fs_inode2v9ses(inode);
1441 st = p9_client_stat(fid);
1445 * Don't update inode if the file type is different
1447 umode = p9mode2unixmode(v9ses, st, &rdev);
1448 if ((inode->i_mode & S_IFMT) != (umode & S_IFMT))
1451 spin_lock(&inode->i_lock);
1453 * We don't want to refresh inode->i_size,
1454 * because we may have cached data
1456 i_size = inode->i_size;
1457 v9fs_stat2inode(st, inode, inode->i_sb);
1459 inode->i_size = i_size;
1460 spin_unlock(&inode->i_lock);
1467 static const struct inode_operations v9fs_dir_inode_operations_dotu = {
1468 .create = v9fs_vfs_create,
1469 .lookup = v9fs_vfs_lookup,
1470 .symlink = v9fs_vfs_symlink,
1471 .link = v9fs_vfs_link,
1472 .unlink = v9fs_vfs_unlink,
1473 .mkdir = v9fs_vfs_mkdir,
1474 .rmdir = v9fs_vfs_rmdir,
1475 .mknod = v9fs_vfs_mknod,
1476 .rename = v9fs_vfs_rename,
1477 .getattr = v9fs_vfs_getattr,
1478 .setattr = v9fs_vfs_setattr,
1481 static const struct inode_operations v9fs_dir_inode_operations = {
1482 .create = v9fs_vfs_create,
1483 .lookup = v9fs_vfs_lookup,
1484 .unlink = v9fs_vfs_unlink,
1485 .mkdir = v9fs_vfs_mkdir,
1486 .rmdir = v9fs_vfs_rmdir,
1487 .mknod = v9fs_vfs_mknod,
1488 .rename = v9fs_vfs_rename,
1489 .getattr = v9fs_vfs_getattr,
1490 .setattr = v9fs_vfs_setattr,
1493 static const struct inode_operations v9fs_file_inode_operations = {
1494 .getattr = v9fs_vfs_getattr,
1495 .setattr = v9fs_vfs_setattr,
1498 static const struct inode_operations v9fs_symlink_inode_operations = {
1499 .readlink = generic_readlink,
1500 .follow_link = v9fs_vfs_follow_link,
1501 .put_link = v9fs_vfs_put_link,
1502 .getattr = v9fs_vfs_getattr,
1503 .setattr = v9fs_vfs_setattr,