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 } else if (v9fs_proto_dotu(v9ses
)) {
282 inode
->i_op
= &v9fs_file_inode_operations
;
284 P9_DPRINTK(P9_DEBUG_ERROR
,
285 "special files without extended mode\n");
289 init_special_inode(inode
, inode
->i_mode
, inode
->i_rdev
);
292 if (v9fs_proto_dotl(v9ses
)) {
293 inode
->i_op
= &v9fs_file_inode_operations_dotl
;
296 &v9fs_cached_file_operations_dotl
;
298 inode
->i_fop
= &v9fs_file_operations_dotl
;
300 inode
->i_op
= &v9fs_file_inode_operations
;
302 inode
->i_fop
= &v9fs_cached_file_operations
;
304 inode
->i_fop
= &v9fs_file_operations
;
309 if (!v9fs_proto_dotu(v9ses
) && !v9fs_proto_dotl(v9ses
)) {
310 P9_DPRINTK(P9_DEBUG_ERROR
, "extended modes used with "
311 "legacy protocol.\n");
316 if (v9fs_proto_dotl(v9ses
))
317 inode
->i_op
= &v9fs_symlink_inode_operations_dotl
;
319 inode
->i_op
= &v9fs_symlink_inode_operations
;
324 if (v9fs_proto_dotl(v9ses
))
325 inode
->i_op
= &v9fs_dir_inode_operations_dotl
;
326 else if (v9fs_proto_dotu(v9ses
))
327 inode
->i_op
= &v9fs_dir_inode_operations_dotu
;
329 inode
->i_op
= &v9fs_dir_inode_operations
;
331 if (v9fs_proto_dotl(v9ses
))
332 inode
->i_fop
= &v9fs_dir_operations_dotl
;
334 inode
->i_fop
= &v9fs_dir_operations
;
338 P9_DPRINTK(P9_DEBUG_ERROR
, "BAD mode 0x%x S_IFMT 0x%x\n",
339 mode
, mode
& S_IFMT
);
349 * v9fs_get_inode - helper function to setup an inode
351 * @mode: mode to setup inode with
355 struct inode
*v9fs_get_inode(struct super_block
*sb
, int mode
, dev_t rdev
)
359 struct v9fs_session_info
*v9ses
= sb
->s_fs_info
;
361 P9_DPRINTK(P9_DEBUG_VFS
, "super block: %p mode: %o\n", sb
, mode
);
363 inode
= new_inode(sb
);
365 P9_EPRINTK(KERN_WARNING
, "Problem allocating inode\n");
366 return ERR_PTR(-ENOMEM
);
368 err
= v9fs_init_inode(v9ses
, inode
, mode
, rdev
);
377 static struct v9fs_fid*
378 v9fs_clone_walk(struct v9fs_session_info *v9ses, u32 fid, struct dentry *dentry)
382 struct v9fs_fid *ret;
383 struct v9fs_fcall *fcall;
385 nfid = v9fs_get_idpool(&v9ses->fidpool);
387 eprintk(KERN_WARNING, "no free fids available\n");
388 return ERR_PTR(-ENOSPC);
391 err = v9fs_t_walk(v9ses, fid, nfid, (char *) dentry->d_name.name,
395 if (fcall && fcall->id == RWALK)
398 PRINT_FCALL_ERROR("walk error", fcall);
399 v9fs_put_idpool(nfid, &v9ses->fidpool);
405 ret = v9fs_fid_create(v9ses, nfid);
411 err = v9fs_fid_insert(ret, dentry);
413 v9fs_fid_destroy(ret);
420 v9fs_t_clunk(v9ses, nfid);
430 * v9fs_clear_inode - release an inode
431 * @inode: inode to release
434 void v9fs_evict_inode(struct inode
*inode
)
436 struct v9fs_inode
*v9inode
= V9FS_I(inode
);
438 truncate_inode_pages(inode
->i_mapping
, 0);
439 end_writeback(inode
);
440 filemap_fdatawrite(inode
->i_mapping
);
442 #ifdef CONFIG_9P_FSCACHE
443 v9fs_cache_inode_put_cookie(inode
);
445 /* clunk the fid stashed in writeback_fid */
446 if (v9inode
->writeback_fid
) {
447 p9_client_clunk(v9inode
->writeback_fid
);
448 v9inode
->writeback_fid
= NULL
;
452 static int v9fs_test_inode(struct inode
*inode
, void *data
)
456 struct v9fs_inode
*v9inode
= V9FS_I(inode
);
457 struct p9_wstat
*st
= (struct p9_wstat
*)data
;
458 struct v9fs_session_info
*v9ses
= v9fs_inode2v9ses(inode
);
460 umode
= p9mode2unixmode(v9ses
, st
, &rdev
);
461 /* don't match inode of different type */
462 if ((inode
->i_mode
& S_IFMT
) != (umode
& S_IFMT
))
465 /* compare qid details */
466 if (memcmp(&v9inode
->qid
.version
,
467 &st
->qid
.version
, sizeof(v9inode
->qid
.version
)))
470 if (v9inode
->qid
.type
!= st
->qid
.type
)
475 static int v9fs_test_new_inode(struct inode
*inode
, void *data
)
480 static int v9fs_set_inode(struct inode
*inode
, void *data
)
482 struct v9fs_inode
*v9inode
= V9FS_I(inode
);
483 struct p9_wstat
*st
= (struct p9_wstat
*)data
;
485 memcpy(&v9inode
->qid
, &st
->qid
, sizeof(st
->qid
));
489 static struct inode
*v9fs_qid_iget(struct super_block
*sb
,
498 struct v9fs_session_info
*v9ses
= sb
->s_fs_info
;
499 int (*test
)(struct inode
*, void *);
502 test
= v9fs_test_new_inode
;
504 test
= v9fs_test_inode
;
506 i_ino
= v9fs_qid2ino(qid
);
507 inode
= iget5_locked(sb
, i_ino
, test
, v9fs_set_inode
, st
);
509 return ERR_PTR(-ENOMEM
);
510 if (!(inode
->i_state
& I_NEW
))
513 * initialize the inode with the stat info
514 * FIXME!! we may need support for stale inodes
517 inode
->i_ino
= i_ino
;
518 umode
= p9mode2unixmode(v9ses
, st
, &rdev
);
519 retval
= v9fs_init_inode(v9ses
, inode
, umode
, rdev
);
523 v9fs_stat2inode(st
, inode
, sb
);
524 #ifdef CONFIG_9P_FSCACHE
525 v9fs_cache_inode_get_cookie(inode
);
527 unlock_new_inode(inode
);
530 unlock_new_inode(inode
);
532 return ERR_PTR(retval
);
537 v9fs_inode_from_fid(struct v9fs_session_info
*v9ses
, struct p9_fid
*fid
,
538 struct super_block
*sb
, int new)
541 struct inode
*inode
= NULL
;
543 st
= p9_client_stat(fid
);
547 inode
= v9fs_qid_iget(sb
, &st
->qid
, st
, new);
554 * v9fs_at_to_dotl_flags- convert Linux specific AT flags to
556 * @flags: flags to convert
558 static int v9fs_at_to_dotl_flags(int flags
)
561 if (flags
& AT_REMOVEDIR
)
562 rflags
|= P9_DOTL_AT_REMOVEDIR
;
567 * v9fs_remove - helper function to remove files and directories
568 * @dir: directory inode that is being deleted
569 * @dentry: dentry that is being deleted
570 * @rmdir: removing a directory
574 static int v9fs_remove(struct inode
*dir
, struct dentry
*dentry
, int flags
)
577 int retval
= -EOPNOTSUPP
;
578 struct p9_fid
*v9fid
, *dfid
;
579 struct v9fs_session_info
*v9ses
;
581 P9_DPRINTK(P9_DEBUG_VFS
, "inode: %p dentry: %p rmdir: %x\n",
584 v9ses
= v9fs_inode2v9ses(dir
);
585 inode
= dentry
->d_inode
;
586 dfid
= v9fs_fid_lookup(dentry
->d_parent
);
588 retval
= PTR_ERR(dfid
);
589 P9_DPRINTK(P9_DEBUG_VFS
, "fid lookup failed %d\n", retval
);
592 if (v9fs_proto_dotl(v9ses
))
593 retval
= p9_client_unlinkat(dfid
, dentry
->d_name
.name
,
594 v9fs_at_to_dotl_flags(flags
));
595 if (retval
== -EOPNOTSUPP
) {
596 /* Try the one based on path */
597 v9fid
= v9fs_fid_clone(dentry
);
599 return PTR_ERR(v9fid
);
600 retval
= p9_client_remove(v9fid
);
604 * directories on unlink should have zero
607 if (flags
& AT_REMOVEDIR
) {
613 v9fs_invalidate_inode_attr(inode
);
614 v9fs_invalidate_inode_attr(dir
);
620 * v9fs_create - Create a file
621 * @v9ses: session information
622 * @dir: directory that dentry is being created in
623 * @dentry: dentry that is being created
624 * @extension: 9p2000.u extension string to support devices, etc.
625 * @perm: create permissions
629 static struct p9_fid
*
630 v9fs_create(struct v9fs_session_info
*v9ses
, struct inode
*dir
,
631 struct dentry
*dentry
, char *extension
, u32 perm
, u8 mode
)
635 struct p9_fid
*dfid
, *ofid
, *fid
;
638 P9_DPRINTK(P9_DEBUG_VFS
, "name %s\n", dentry
->d_name
.name
);
643 name
= (char *) dentry
->d_name
.name
;
644 dfid
= v9fs_fid_lookup(dentry
->d_parent
);
647 P9_DPRINTK(P9_DEBUG_VFS
, "fid lookup failed %d\n", err
);
651 /* clone a fid to use for creation */
652 ofid
= p9_client_walk(dfid
, 0, NULL
, 1);
655 P9_DPRINTK(P9_DEBUG_VFS
, "p9_client_walk failed %d\n", err
);
659 err
= p9_client_fcreate(ofid
, name
, perm
, mode
, extension
);
661 P9_DPRINTK(P9_DEBUG_VFS
, "p9_client_fcreate failed %d\n", err
);
665 /* now walk from the parent so we can get unopened fid */
666 fid
= p9_client_walk(dfid
, 1, &name
, 1);
669 P9_DPRINTK(P9_DEBUG_VFS
, "p9_client_walk failed %d\n", err
);
674 /* instantiate inode and assign the unopened fid to the dentry */
675 inode
= v9fs_get_new_inode_from_fid(v9ses
, fid
, dir
->i_sb
);
677 err
= PTR_ERR(inode
);
678 P9_DPRINTK(P9_DEBUG_VFS
, "inode creation failed %d\n", err
);
681 err
= v9fs_fid_add(dentry
, fid
);
684 d_instantiate(dentry
, inode
);
688 p9_client_clunk(ofid
);
691 p9_client_clunk(fid
);
697 * v9fs_vfs_create - VFS hook to create files
698 * @dir: directory inode that is being created
699 * @dentry: dentry that is being deleted
700 * @mode: create permissions
701 * @nd: path information
706 v9fs_vfs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
,
707 struct nameidata
*nd
)
713 struct v9fs_inode
*v9inode
;
714 struct v9fs_session_info
*v9ses
;
715 struct p9_fid
*fid
, *inode_fid
;
719 v9ses
= v9fs_inode2v9ses(dir
);
720 perm
= unixmode2p9mode(v9ses
, mode
);
722 flags
= nd
->intent
.open
.flags
;
726 fid
= v9fs_create(v9ses
, dir
, dentry
, NULL
, perm
,
727 v9fs_uflags2omode(flags
,
728 v9fs_proto_dotu(v9ses
)));
735 v9fs_invalidate_inode_attr(dir
);
736 /* if we are opening a file, assign the open fid to the file */
738 v9inode
= V9FS_I(dentry
->d_inode
);
739 mutex_lock(&v9inode
->v_mutex
);
740 if (v9ses
->cache
&& !v9inode
->writeback_fid
&&
741 ((flags
& O_ACCMODE
) != O_RDONLY
)) {
743 * clone a fid and add it to writeback_fid
744 * we do it during open time instead of
745 * page dirty time via write_begin/page_mkwrite
746 * because we want write after unlink usecase
749 inode_fid
= v9fs_writeback_fid(dentry
);
750 if (IS_ERR(inode_fid
)) {
751 err
= PTR_ERR(inode_fid
);
752 mutex_unlock(&v9inode
->v_mutex
);
755 v9inode
->writeback_fid
= (void *) inode_fid
;
757 mutex_unlock(&v9inode
->v_mutex
);
758 filp
= lookup_instantiate_filp(nd
, dentry
, generic_file_open
);
764 filp
->private_data
= fid
;
765 #ifdef CONFIG_9P_FSCACHE
767 v9fs_cache_inode_set_cookie(dentry
->d_inode
, filp
);
770 p9_client_clunk(fid
);
776 p9_client_clunk(fid
);
782 * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
783 * @dir: inode that is being unlinked
784 * @dentry: dentry that is being unlinked
785 * @mode: mode for new directory
789 static int v9fs_vfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
794 struct v9fs_session_info
*v9ses
;
796 P9_DPRINTK(P9_DEBUG_VFS
, "name %s\n", dentry
->d_name
.name
);
798 v9ses
= v9fs_inode2v9ses(dir
);
799 perm
= unixmode2p9mode(v9ses
, mode
| S_IFDIR
);
800 fid
= v9fs_create(v9ses
, dir
, dentry
, NULL
, perm
, P9_OREAD
);
806 v9fs_invalidate_inode_attr(dir
);
810 p9_client_clunk(fid
);
816 * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
817 * @dir: inode that is being walked from
818 * @dentry: dentry that is being walked to?
819 * @nameidata: path data
823 struct dentry
*v9fs_vfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
824 struct nameidata
*nameidata
)
827 struct super_block
*sb
;
828 struct v9fs_session_info
*v9ses
;
829 struct p9_fid
*dfid
, *fid
;
834 P9_DPRINTK(P9_DEBUG_VFS
, "dir: %p dentry: (%s) %p nameidata: %p\n",
835 dir
, dentry
->d_name
.name
, dentry
, nameidata
);
837 if (dentry
->d_name
.len
> NAME_MAX
)
838 return ERR_PTR(-ENAMETOOLONG
);
841 v9ses
= v9fs_inode2v9ses(dir
);
842 /* We can walk d_parent because we hold the dir->i_mutex */
843 dfid
= v9fs_fid_lookup(dentry
->d_parent
);
845 return ERR_CAST(dfid
);
847 name
= (char *) dentry
->d_name
.name
;
848 fid
= p9_client_walk(dfid
, 1, &name
, 1);
850 result
= PTR_ERR(fid
);
851 if (result
== -ENOENT
) {
856 return ERR_PTR(result
);
859 * Make sure we don't use a wrong inode due to parallel
860 * unlink. For cached mode create calls request for new
861 * inode. But with cache disabled, lookup should do this.
864 inode
= v9fs_get_inode_from_fid(v9ses
, fid
, dir
->i_sb
);
866 inode
= v9fs_get_new_inode_from_fid(v9ses
, fid
, dir
->i_sb
);
868 result
= PTR_ERR(inode
);
872 result
= v9fs_fid_add(dentry
, fid
);
877 * If we had a rename on the server and a parallel lookup
878 * for the new name, then make sure we instantiate with
879 * the new name. ie look up for a/b, while on server somebody
880 * moved b under k and client parallely did a lookup for
883 res
= d_materialise_unique(dentry
, inode
);
886 result
= PTR_ERR(res
);
890 p9_client_clunk(fid
);
892 return ERR_PTR(result
);
896 * v9fs_vfs_unlink - VFS unlink hook to delete an inode
897 * @i: inode that is being unlinked
898 * @d: dentry that is being unlinked
902 int v9fs_vfs_unlink(struct inode
*i
, struct dentry
*d
)
904 return v9fs_remove(i
, d
, 0);
908 * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
909 * @i: inode that is being unlinked
910 * @d: dentry that is being unlinked
914 int v9fs_vfs_rmdir(struct inode
*i
, struct dentry
*d
)
916 return v9fs_remove(i
, d
, AT_REMOVEDIR
);
920 * v9fs_vfs_rename - VFS hook to rename an inode
921 * @old_dir: old dir inode
922 * @old_dentry: old dentry
923 * @new_dir: new dir inode
924 * @new_dentry: new dentry
929 v9fs_vfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
930 struct inode
*new_dir
, struct dentry
*new_dentry
)
933 struct inode
*old_inode
;
934 struct inode
*new_inode
;
935 struct v9fs_session_info
*v9ses
;
936 struct p9_fid
*oldfid
;
937 struct p9_fid
*olddirfid
;
938 struct p9_fid
*newdirfid
;
939 struct p9_wstat wstat
;
941 P9_DPRINTK(P9_DEBUG_VFS
, "\n");
943 old_inode
= old_dentry
->d_inode
;
944 new_inode
= new_dentry
->d_inode
;
945 v9ses
= v9fs_inode2v9ses(old_inode
);
946 oldfid
= v9fs_fid_lookup(old_dentry
);
948 return PTR_ERR(oldfid
);
950 olddirfid
= v9fs_fid_clone(old_dentry
->d_parent
);
951 if (IS_ERR(olddirfid
)) {
952 retval
= PTR_ERR(olddirfid
);
956 newdirfid
= v9fs_fid_clone(new_dentry
->d_parent
);
957 if (IS_ERR(newdirfid
)) {
958 retval
= PTR_ERR(newdirfid
);
962 down_write(&v9ses
->rename_sem
);
963 if (v9fs_proto_dotl(v9ses
)) {
964 retval
= p9_client_renameat(olddirfid
, old_dentry
->d_name
.name
,
965 newdirfid
, new_dentry
->d_name
.name
);
966 if (retval
== -EOPNOTSUPP
)
967 retval
= p9_client_rename(oldfid
, newdirfid
,
968 new_dentry
->d_name
.name
);
969 if (retval
!= -EOPNOTSUPP
)
972 if (old_dentry
->d_parent
!= new_dentry
->d_parent
) {
974 * 9P .u can only handle file rename in the same directory
977 P9_DPRINTK(P9_DEBUG_ERROR
,
978 "old dir and new dir are different\n");
982 v9fs_blank_wstat(&wstat
);
983 wstat
.muid
= v9ses
->uname
;
984 wstat
.name
= (char *) new_dentry
->d_name
.name
;
985 retval
= p9_client_wstat(oldfid
, &wstat
);
990 if (S_ISDIR(new_inode
->i_mode
))
991 clear_nlink(new_inode
);
993 drop_nlink(new_inode
);
995 if (S_ISDIR(old_inode
->i_mode
)) {
1000 v9fs_invalidate_inode_attr(old_inode
);
1001 v9fs_invalidate_inode_attr(old_dir
);
1002 v9fs_invalidate_inode_attr(new_dir
);
1004 /* successful rename */
1005 d_move(old_dentry
, new_dentry
);
1007 up_write(&v9ses
->rename_sem
);
1008 p9_client_clunk(newdirfid
);
1011 p9_client_clunk(olddirfid
);
1018 * v9fs_vfs_getattr - retrieve file metadata
1019 * @mnt: mount information
1020 * @dentry: file to get attributes on
1021 * @stat: metadata structure to populate
1026 v9fs_vfs_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
1030 struct v9fs_session_info
*v9ses
;
1032 struct p9_wstat
*st
;
1034 P9_DPRINTK(P9_DEBUG_VFS
, "dentry: %p\n", dentry
);
1036 v9ses
= v9fs_dentry2v9ses(dentry
);
1037 if (v9ses
->cache
== CACHE_LOOSE
|| v9ses
->cache
== CACHE_FSCACHE
) {
1038 generic_fillattr(dentry
->d_inode
, stat
);
1041 fid
= v9fs_fid_lookup(dentry
);
1043 return PTR_ERR(fid
);
1045 st
= p9_client_stat(fid
);
1049 v9fs_stat2inode(st
, dentry
->d_inode
, dentry
->d_inode
->i_sb
);
1050 generic_fillattr(dentry
->d_inode
, stat
);
1058 * v9fs_vfs_setattr - set file metadata
1059 * @dentry: file whose metadata to set
1060 * @iattr: metadata assignment structure
1064 static int v9fs_vfs_setattr(struct dentry
*dentry
, struct iattr
*iattr
)
1067 struct v9fs_session_info
*v9ses
;
1069 struct p9_wstat wstat
;
1071 P9_DPRINTK(P9_DEBUG_VFS
, "\n");
1072 retval
= inode_change_ok(dentry
->d_inode
, iattr
);
1077 v9ses
= v9fs_dentry2v9ses(dentry
);
1078 fid
= v9fs_fid_lookup(dentry
);
1080 return PTR_ERR(fid
);
1082 v9fs_blank_wstat(&wstat
);
1083 if (iattr
->ia_valid
& ATTR_MODE
)
1084 wstat
.mode
= unixmode2p9mode(v9ses
, iattr
->ia_mode
);
1086 if (iattr
->ia_valid
& ATTR_MTIME
)
1087 wstat
.mtime
= iattr
->ia_mtime
.tv_sec
;
1089 if (iattr
->ia_valid
& ATTR_ATIME
)
1090 wstat
.atime
= iattr
->ia_atime
.tv_sec
;
1092 if (iattr
->ia_valid
& ATTR_SIZE
)
1093 wstat
.length
= iattr
->ia_size
;
1095 if (v9fs_proto_dotu(v9ses
)) {
1096 if (iattr
->ia_valid
& ATTR_UID
)
1097 wstat
.n_uid
= iattr
->ia_uid
;
1099 if (iattr
->ia_valid
& ATTR_GID
)
1100 wstat
.n_gid
= iattr
->ia_gid
;
1103 /* Write all dirty data */
1104 if (S_ISREG(dentry
->d_inode
->i_mode
))
1105 filemap_write_and_wait(dentry
->d_inode
->i_mapping
);
1107 retval
= p9_client_wstat(fid
, &wstat
);
1111 if ((iattr
->ia_valid
& ATTR_SIZE
) &&
1112 iattr
->ia_size
!= i_size_read(dentry
->d_inode
))
1113 truncate_setsize(dentry
->d_inode
, iattr
->ia_size
);
1115 v9fs_invalidate_inode_attr(dentry
->d_inode
);
1117 setattr_copy(dentry
->d_inode
, iattr
);
1118 mark_inode_dirty(dentry
->d_inode
);
1123 * v9fs_stat2inode - populate an inode structure with mistat info
1124 * @stat: Plan 9 metadata (mistat) structure
1125 * @inode: inode to populate
1126 * @sb: superblock of filesystem
1131 v9fs_stat2inode(struct p9_wstat
*stat
, struct inode
*inode
,
1132 struct super_block
*sb
)
1137 unsigned int i_nlink
;
1138 struct v9fs_session_info
*v9ses
= sb
->s_fs_info
;
1139 struct v9fs_inode
*v9inode
= V9FS_I(inode
);
1143 inode
->i_atime
.tv_sec
= stat
->atime
;
1144 inode
->i_mtime
.tv_sec
= stat
->mtime
;
1145 inode
->i_ctime
.tv_sec
= stat
->mtime
;
1147 inode
->i_uid
= v9ses
->dfltuid
;
1148 inode
->i_gid
= v9ses
->dfltgid
;
1150 if (v9fs_proto_dotu(v9ses
)) {
1151 inode
->i_uid
= stat
->n_uid
;
1152 inode
->i_gid
= stat
->n_gid
;
1154 if ((S_ISREG(inode
->i_mode
)) || (S_ISDIR(inode
->i_mode
))) {
1155 if (v9fs_proto_dotu(v9ses
) && (stat
->extension
[0] != '\0')) {
1157 * Hadlink support got added later to
1158 * to the .u extension. So there can be
1159 * server out there that doesn't support
1160 * this even with .u extension. So check
1161 * for non NULL stat->extension
1163 strncpy(ext
, stat
->extension
, sizeof(ext
));
1164 /* HARDLINKCOUNT %u */
1165 sscanf(ext
, "%13s %u", tag_name
, &i_nlink
);
1166 if (!strncmp(tag_name
, "HARDLINKCOUNT", 13))
1167 inode
->i_nlink
= i_nlink
;
1170 mode
= stat
->mode
& S_IALLUGO
;
1171 mode
|= inode
->i_mode
& ~S_IALLUGO
;
1172 inode
->i_mode
= mode
;
1173 i_size_write(inode
, stat
->length
);
1175 /* not real number of blocks, but 512 byte ones ... */
1176 inode
->i_blocks
= (i_size_read(inode
) + 512 - 1) >> 9;
1177 v9inode
->cache_validity
&= ~V9FS_INO_INVALID_ATTR
;
1181 * v9fs_qid2ino - convert qid into inode number
1184 * BUG: potential for inode number collisions?
1187 ino_t
v9fs_qid2ino(struct p9_qid
*qid
)
1189 u64 path
= qid
->path
+ 2;
1192 if (sizeof(ino_t
) == sizeof(path
))
1193 memcpy(&i
, &path
, sizeof(ino_t
));
1195 i
= (ino_t
) (path
^ (path
>> 32));
1201 * v9fs_readlink - read a symlink's location (internal version)
1202 * @dentry: dentry for symlink
1203 * @buffer: buffer to load symlink location into
1204 * @buflen: length of buffer
1208 static int v9fs_readlink(struct dentry
*dentry
, char *buffer
, int buflen
)
1212 struct v9fs_session_info
*v9ses
;
1214 struct p9_wstat
*st
;
1216 P9_DPRINTK(P9_DEBUG_VFS
, " %s\n", dentry
->d_name
.name
);
1218 v9ses
= v9fs_dentry2v9ses(dentry
);
1219 fid
= v9fs_fid_lookup(dentry
);
1221 return PTR_ERR(fid
);
1223 if (!v9fs_proto_dotu(v9ses
))
1226 st
= p9_client_stat(fid
);
1230 if (!(st
->mode
& P9_DMSYMLINK
)) {
1235 /* copy extension buffer into buffer */
1236 strncpy(buffer
, st
->extension
, buflen
);
1238 P9_DPRINTK(P9_DEBUG_VFS
,
1239 "%s -> %s (%s)\n", dentry
->d_name
.name
, st
->extension
, buffer
);
1241 retval
= strnlen(buffer
, buflen
);
1249 * v9fs_vfs_follow_link - follow a symlink path
1250 * @dentry: dentry for symlink
1255 static void *v9fs_vfs_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1258 char *link
= __getname();
1260 P9_DPRINTK(P9_DEBUG_VFS
, "%s n", dentry
->d_name
.name
);
1263 link
= ERR_PTR(-ENOMEM
);
1265 len
= v9fs_readlink(dentry
, link
, PATH_MAX
);
1269 link
= ERR_PTR(len
);
1271 link
[min(len
, PATH_MAX
-1)] = 0;
1273 nd_set_link(nd
, link
);
1279 * v9fs_vfs_put_link - release a symlink path
1280 * @dentry: dentry for symlink
1287 v9fs_vfs_put_link(struct dentry
*dentry
, struct nameidata
*nd
, void *p
)
1289 char *s
= nd_get_link(nd
);
1291 P9_DPRINTK(P9_DEBUG_VFS
, " %s %s\n", dentry
->d_name
.name
,
1292 IS_ERR(s
) ? "<error>" : s
);
1298 * v9fs_vfs_mkspecial - create a special file
1299 * @dir: inode to create special file in
1300 * @dentry: dentry to create
1301 * @mode: mode to create special file
1302 * @extension: 9p2000.u format extension string representing special file
1306 static int v9fs_vfs_mkspecial(struct inode
*dir
, struct dentry
*dentry
,
1307 int mode
, const char *extension
)
1311 struct v9fs_session_info
*v9ses
;
1313 v9ses
= v9fs_inode2v9ses(dir
);
1314 if (!v9fs_proto_dotu(v9ses
)) {
1315 P9_DPRINTK(P9_DEBUG_ERROR
, "not extended\n");
1319 perm
= unixmode2p9mode(v9ses
, mode
);
1320 fid
= v9fs_create(v9ses
, dir
, dentry
, (char *) extension
, perm
,
1323 return PTR_ERR(fid
);
1325 v9fs_invalidate_inode_attr(dir
);
1326 p9_client_clunk(fid
);
1331 * v9fs_vfs_symlink - helper function to create symlinks
1332 * @dir: directory inode containing symlink
1333 * @dentry: dentry for symlink
1334 * @symname: symlink data
1336 * See Also: 9P2000.u RFC for more information
1341 v9fs_vfs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *symname
)
1343 P9_DPRINTK(P9_DEBUG_VFS
, " %lu,%s,%s\n", dir
->i_ino
,
1344 dentry
->d_name
.name
, symname
);
1346 return v9fs_vfs_mkspecial(dir
, dentry
, S_IFLNK
, symname
);
1350 * v9fs_vfs_link - create a hardlink
1351 * @old_dentry: dentry for file to link to
1352 * @dir: inode destination for new link
1353 * @dentry: dentry for link
1358 v9fs_vfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
1359 struct dentry
*dentry
)
1363 struct p9_fid
*oldfid
;
1365 P9_DPRINTK(P9_DEBUG_VFS
,
1366 " %lu,%s,%s\n", dir
->i_ino
, dentry
->d_name
.name
,
1367 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_DPRINTK(P9_DEBUG_VFS
,
1407 " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir
->i_ino
,
1408 dentry
->d_name
.name
, mode
, 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
,