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_remove - helper function to remove files and directories
557 * @dir: directory inode that is being deleted
558 * @file: dentry that is being deleted
559 * @rmdir: removing a directory
563 static int v9fs_remove(struct inode
*dir
, struct dentry
*file
, int rmdir
)
566 struct p9_fid
*v9fid
;
567 struct inode
*file_inode
;
569 P9_DPRINTK(P9_DEBUG_VFS
, "inode: %p dentry: %p rmdir: %d\n", dir
, file
,
572 file_inode
= file
->d_inode
;
573 v9fid
= v9fs_fid_clone(file
);
575 return PTR_ERR(v9fid
);
577 retval
= p9_client_remove(v9fid
);
580 * directories on unlink should have zero
584 clear_nlink(file_inode
);
587 drop_nlink(file_inode
);
589 v9fs_invalidate_inode_attr(file_inode
);
590 v9fs_invalidate_inode_attr(dir
);
596 * v9fs_create - Create a file
597 * @v9ses: session information
598 * @dir: directory that dentry is being created in
599 * @dentry: dentry that is being created
600 * @extension: 9p2000.u extension string to support devices, etc.
601 * @perm: create permissions
605 static struct p9_fid
*
606 v9fs_create(struct v9fs_session_info
*v9ses
, struct inode
*dir
,
607 struct dentry
*dentry
, char *extension
, u32 perm
, u8 mode
)
611 struct p9_fid
*dfid
, *ofid
, *fid
;
614 P9_DPRINTK(P9_DEBUG_VFS
, "name %s\n", dentry
->d_name
.name
);
619 name
= (char *) dentry
->d_name
.name
;
620 dfid
= v9fs_fid_lookup(dentry
->d_parent
);
623 P9_DPRINTK(P9_DEBUG_VFS
, "fid lookup failed %d\n", err
);
627 /* clone a fid to use for creation */
628 ofid
= p9_client_walk(dfid
, 0, NULL
, 1);
631 P9_DPRINTK(P9_DEBUG_VFS
, "p9_client_walk failed %d\n", err
);
635 err
= p9_client_fcreate(ofid
, name
, perm
, mode
, extension
);
637 P9_DPRINTK(P9_DEBUG_VFS
, "p9_client_fcreate failed %d\n", err
);
641 /* now walk from the parent so we can get unopened fid */
642 fid
= p9_client_walk(dfid
, 1, &name
, 1);
645 P9_DPRINTK(P9_DEBUG_VFS
, "p9_client_walk failed %d\n", err
);
650 /* instantiate inode and assign the unopened fid to the dentry */
651 inode
= v9fs_get_new_inode_from_fid(v9ses
, fid
, dir
->i_sb
);
653 err
= PTR_ERR(inode
);
654 P9_DPRINTK(P9_DEBUG_VFS
, "inode creation failed %d\n", err
);
657 err
= v9fs_fid_add(dentry
, fid
);
660 d_instantiate(dentry
, inode
);
664 p9_client_clunk(ofid
);
667 p9_client_clunk(fid
);
673 * v9fs_vfs_create - VFS hook to create files
674 * @dir: directory inode that is being created
675 * @dentry: dentry that is being deleted
676 * @mode: create permissions
677 * @nd: path information
682 v9fs_vfs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
,
683 struct nameidata
*nd
)
689 struct v9fs_inode
*v9inode
;
690 struct v9fs_session_info
*v9ses
;
691 struct p9_fid
*fid
, *inode_fid
;
695 v9ses
= v9fs_inode2v9ses(dir
);
696 perm
= unixmode2p9mode(v9ses
, mode
);
697 if (nd
&& nd
->flags
& LOOKUP_OPEN
)
698 flags
= nd
->intent
.open
.flags
- 1;
702 fid
= v9fs_create(v9ses
, dir
, dentry
, NULL
, perm
,
703 v9fs_uflags2omode(flags
,
704 v9fs_proto_dotu(v9ses
)));
711 v9fs_invalidate_inode_attr(dir
);
712 /* if we are opening a file, assign the open fid to the file */
713 if (nd
&& nd
->flags
& LOOKUP_OPEN
) {
714 v9inode
= V9FS_I(dentry
->d_inode
);
715 mutex_lock(&v9inode
->v_mutex
);
716 if (v9ses
->cache
&& !v9inode
->writeback_fid
&&
717 ((flags
& O_ACCMODE
) != O_RDONLY
)) {
719 * clone a fid and add it to writeback_fid
720 * we do it during open time instead of
721 * page dirty time via write_begin/page_mkwrite
722 * because we want write after unlink usecase
725 inode_fid
= v9fs_writeback_fid(dentry
);
726 if (IS_ERR(inode_fid
)) {
727 err
= PTR_ERR(inode_fid
);
728 mutex_unlock(&v9inode
->v_mutex
);
731 v9inode
->writeback_fid
= (void *) inode_fid
;
733 mutex_unlock(&v9inode
->v_mutex
);
734 filp
= lookup_instantiate_filp(nd
, dentry
, generic_file_open
);
740 filp
->private_data
= fid
;
741 #ifdef CONFIG_9P_FSCACHE
743 v9fs_cache_inode_set_cookie(dentry
->d_inode
, filp
);
746 p9_client_clunk(fid
);
752 p9_client_clunk(fid
);
758 * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
759 * @dir: inode that is being unlinked
760 * @dentry: dentry that is being unlinked
761 * @mode: mode for new directory
765 static int v9fs_vfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
770 struct v9fs_session_info
*v9ses
;
772 P9_DPRINTK(P9_DEBUG_VFS
, "name %s\n", dentry
->d_name
.name
);
774 v9ses
= v9fs_inode2v9ses(dir
);
775 perm
= unixmode2p9mode(v9ses
, mode
| S_IFDIR
);
776 fid
= v9fs_create(v9ses
, dir
, dentry
, NULL
, perm
, P9_OREAD
);
782 v9fs_invalidate_inode_attr(dir
);
786 p9_client_clunk(fid
);
792 * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
793 * @dir: inode that is being walked from
794 * @dentry: dentry that is being walked to?
795 * @nameidata: path data
799 struct dentry
*v9fs_vfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
800 struct nameidata
*nameidata
)
803 struct super_block
*sb
;
804 struct v9fs_session_info
*v9ses
;
805 struct p9_fid
*dfid
, *fid
;
810 P9_DPRINTK(P9_DEBUG_VFS
, "dir: %p dentry: (%s) %p nameidata: %p\n",
811 dir
, dentry
->d_name
.name
, dentry
, nameidata
);
813 if (dentry
->d_name
.len
> NAME_MAX
)
814 return ERR_PTR(-ENAMETOOLONG
);
817 v9ses
= v9fs_inode2v9ses(dir
);
818 /* We can walk d_parent because we hold the dir->i_mutex */
819 dfid
= v9fs_fid_lookup(dentry
->d_parent
);
821 return ERR_CAST(dfid
);
823 name
= (char *) dentry
->d_name
.name
;
824 fid
= p9_client_walk(dfid
, 1, &name
, 1);
826 result
= PTR_ERR(fid
);
827 if (result
== -ENOENT
) {
832 return ERR_PTR(result
);
835 * Make sure we don't use a wrong inode due to parallel
836 * unlink. For cached mode create calls request for new
837 * inode. But with cache disabled, lookup should do this.
840 inode
= v9fs_get_inode_from_fid(v9ses
, fid
, dir
->i_sb
);
842 inode
= v9fs_get_new_inode_from_fid(v9ses
, fid
, dir
->i_sb
);
844 result
= PTR_ERR(inode
);
848 result
= v9fs_fid_add(dentry
, fid
);
853 * If we had a rename on the server and a parallel lookup
854 * for the new name, then make sure we instantiate with
855 * the new name. ie look up for a/b, while on server somebody
856 * moved b under k and client parallely did a lookup for
859 res
= d_materialise_unique(dentry
, inode
);
862 result
= PTR_ERR(res
);
866 p9_client_clunk(fid
);
868 return ERR_PTR(result
);
872 * v9fs_vfs_unlink - VFS unlink hook to delete an inode
873 * @i: inode that is being unlinked
874 * @d: dentry that is being unlinked
878 int v9fs_vfs_unlink(struct inode
*i
, struct dentry
*d
)
880 return v9fs_remove(i
, d
, 0);
884 * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
885 * @i: inode that is being unlinked
886 * @d: dentry that is being unlinked
890 int v9fs_vfs_rmdir(struct inode
*i
, struct dentry
*d
)
892 return v9fs_remove(i
, d
, 1);
896 * v9fs_vfs_rename - VFS hook to rename an inode
897 * @old_dir: old dir inode
898 * @old_dentry: old dentry
899 * @new_dir: new dir inode
900 * @new_dentry: new dentry
905 v9fs_vfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
906 struct inode
*new_dir
, struct dentry
*new_dentry
)
909 struct inode
*old_inode
;
910 struct inode
*new_inode
;
911 struct v9fs_session_info
*v9ses
;
912 struct p9_fid
*oldfid
;
913 struct p9_fid
*olddirfid
;
914 struct p9_fid
*newdirfid
;
915 struct p9_wstat wstat
;
917 P9_DPRINTK(P9_DEBUG_VFS
, "\n");
919 old_inode
= old_dentry
->d_inode
;
920 new_inode
= new_dentry
->d_inode
;
921 v9ses
= v9fs_inode2v9ses(old_inode
);
922 oldfid
= v9fs_fid_lookup(old_dentry
);
924 return PTR_ERR(oldfid
);
926 olddirfid
= v9fs_fid_clone(old_dentry
->d_parent
);
927 if (IS_ERR(olddirfid
)) {
928 retval
= PTR_ERR(olddirfid
);
932 newdirfid
= v9fs_fid_clone(new_dentry
->d_parent
);
933 if (IS_ERR(newdirfid
)) {
934 retval
= PTR_ERR(newdirfid
);
938 down_write(&v9ses
->rename_sem
);
939 if (v9fs_proto_dotl(v9ses
)) {
940 retval
= p9_client_rename(oldfid
, newdirfid
,
941 (char *) new_dentry
->d_name
.name
);
942 if (retval
!= -ENOSYS
)
945 if (old_dentry
->d_parent
!= new_dentry
->d_parent
) {
947 * 9P .u can only handle file rename in the same directory
950 P9_DPRINTK(P9_DEBUG_ERROR
,
951 "old dir and new dir are different\n");
955 v9fs_blank_wstat(&wstat
);
956 wstat
.muid
= v9ses
->uname
;
957 wstat
.name
= (char *) new_dentry
->d_name
.name
;
958 retval
= p9_client_wstat(oldfid
, &wstat
);
963 if (S_ISDIR(new_inode
->i_mode
))
964 clear_nlink(new_inode
);
966 drop_nlink(new_inode
);
968 * Work around vfs rename rehash bug with
969 * FS_RENAME_DOES_D_MOVE
971 v9fs_invalidate_inode_attr(new_inode
);
973 if (S_ISDIR(old_inode
->i_mode
)) {
978 v9fs_invalidate_inode_attr(old_inode
);
979 v9fs_invalidate_inode_attr(old_dir
);
980 v9fs_invalidate_inode_attr(new_dir
);
982 /* successful rename */
983 d_move(old_dentry
, new_dentry
);
985 up_write(&v9ses
->rename_sem
);
986 p9_client_clunk(newdirfid
);
989 p9_client_clunk(olddirfid
);
996 * v9fs_vfs_getattr - retrieve file metadata
997 * @mnt: mount information
998 * @dentry: file to get attributes on
999 * @stat: metadata structure to populate
1004 v9fs_vfs_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
1008 struct v9fs_session_info
*v9ses
;
1010 struct p9_wstat
*st
;
1012 P9_DPRINTK(P9_DEBUG_VFS
, "dentry: %p\n", dentry
);
1014 v9ses
= v9fs_dentry2v9ses(dentry
);
1015 if (v9ses
->cache
== CACHE_LOOSE
|| v9ses
->cache
== CACHE_FSCACHE
) {
1016 generic_fillattr(dentry
->d_inode
, stat
);
1019 fid
= v9fs_fid_lookup(dentry
);
1021 return PTR_ERR(fid
);
1023 st
= p9_client_stat(fid
);
1027 v9fs_stat2inode(st
, dentry
->d_inode
, dentry
->d_inode
->i_sb
);
1028 generic_fillattr(dentry
->d_inode
, stat
);
1036 * v9fs_vfs_setattr - set file metadata
1037 * @dentry: file whose metadata to set
1038 * @iattr: metadata assignment structure
1042 static int v9fs_vfs_setattr(struct dentry
*dentry
, struct iattr
*iattr
)
1045 struct v9fs_session_info
*v9ses
;
1047 struct p9_wstat wstat
;
1049 P9_DPRINTK(P9_DEBUG_VFS
, "\n");
1050 retval
= inode_change_ok(dentry
->d_inode
, iattr
);
1055 v9ses
= v9fs_dentry2v9ses(dentry
);
1056 fid
= v9fs_fid_lookup(dentry
);
1058 return PTR_ERR(fid
);
1060 v9fs_blank_wstat(&wstat
);
1061 if (iattr
->ia_valid
& ATTR_MODE
)
1062 wstat
.mode
= unixmode2p9mode(v9ses
, iattr
->ia_mode
);
1064 if (iattr
->ia_valid
& ATTR_MTIME
)
1065 wstat
.mtime
= iattr
->ia_mtime
.tv_sec
;
1067 if (iattr
->ia_valid
& ATTR_ATIME
)
1068 wstat
.atime
= iattr
->ia_atime
.tv_sec
;
1070 if (iattr
->ia_valid
& ATTR_SIZE
)
1071 wstat
.length
= iattr
->ia_size
;
1073 if (v9fs_proto_dotu(v9ses
)) {
1074 if (iattr
->ia_valid
& ATTR_UID
)
1075 wstat
.n_uid
= iattr
->ia_uid
;
1077 if (iattr
->ia_valid
& ATTR_GID
)
1078 wstat
.n_gid
= iattr
->ia_gid
;
1081 /* Write all dirty data */
1082 if (S_ISREG(dentry
->d_inode
->i_mode
))
1083 filemap_write_and_wait(dentry
->d_inode
->i_mapping
);
1085 retval
= p9_client_wstat(fid
, &wstat
);
1089 if ((iattr
->ia_valid
& ATTR_SIZE
) &&
1090 iattr
->ia_size
!= i_size_read(dentry
->d_inode
))
1091 truncate_setsize(dentry
->d_inode
, iattr
->ia_size
);
1093 v9fs_invalidate_inode_attr(dentry
->d_inode
);
1095 setattr_copy(dentry
->d_inode
, iattr
);
1096 mark_inode_dirty(dentry
->d_inode
);
1101 * v9fs_stat2inode - populate an inode structure with mistat info
1102 * @stat: Plan 9 metadata (mistat) structure
1103 * @inode: inode to populate
1104 * @sb: superblock of filesystem
1109 v9fs_stat2inode(struct p9_wstat
*stat
, struct inode
*inode
,
1110 struct super_block
*sb
)
1115 unsigned int i_nlink
;
1116 struct v9fs_session_info
*v9ses
= sb
->s_fs_info
;
1117 struct v9fs_inode
*v9inode
= V9FS_I(inode
);
1121 inode
->i_atime
.tv_sec
= stat
->atime
;
1122 inode
->i_mtime
.tv_sec
= stat
->mtime
;
1123 inode
->i_ctime
.tv_sec
= stat
->mtime
;
1125 inode
->i_uid
= v9ses
->dfltuid
;
1126 inode
->i_gid
= v9ses
->dfltgid
;
1128 if (v9fs_proto_dotu(v9ses
)) {
1129 inode
->i_uid
= stat
->n_uid
;
1130 inode
->i_gid
= stat
->n_gid
;
1132 if ((S_ISREG(inode
->i_mode
)) || (S_ISDIR(inode
->i_mode
))) {
1133 if (v9fs_proto_dotu(v9ses
) && (stat
->extension
[0] != '\0')) {
1135 * Hadlink support got added later to
1136 * to the .u extension. So there can be
1137 * server out there that doesn't support
1138 * this even with .u extension. So check
1139 * for non NULL stat->extension
1141 strncpy(ext
, stat
->extension
, sizeof(ext
));
1142 /* HARDLINKCOUNT %u */
1143 sscanf(ext
, "%13s %u", tag_name
, &i_nlink
);
1144 if (!strncmp(tag_name
, "HARDLINKCOUNT", 13))
1145 inode
->i_nlink
= i_nlink
;
1148 mode
= stat
->mode
& S_IALLUGO
;
1149 mode
|= inode
->i_mode
& ~S_IALLUGO
;
1150 inode
->i_mode
= mode
;
1151 i_size_write(inode
, stat
->length
);
1153 /* not real number of blocks, but 512 byte ones ... */
1154 inode
->i_blocks
= (i_size_read(inode
) + 512 - 1) >> 9;
1155 v9inode
->cache_validity
&= ~V9FS_INO_INVALID_ATTR
;
1159 * v9fs_qid2ino - convert qid into inode number
1162 * BUG: potential for inode number collisions?
1165 ino_t
v9fs_qid2ino(struct p9_qid
*qid
)
1167 u64 path
= qid
->path
+ 2;
1170 if (sizeof(ino_t
) == sizeof(path
))
1171 memcpy(&i
, &path
, sizeof(ino_t
));
1173 i
= (ino_t
) (path
^ (path
>> 32));
1179 * v9fs_readlink - read a symlink's location (internal version)
1180 * @dentry: dentry for symlink
1181 * @buffer: buffer to load symlink location into
1182 * @buflen: length of buffer
1186 static int v9fs_readlink(struct dentry
*dentry
, char *buffer
, int buflen
)
1190 struct v9fs_session_info
*v9ses
;
1192 struct p9_wstat
*st
;
1194 P9_DPRINTK(P9_DEBUG_VFS
, " %s\n", dentry
->d_name
.name
);
1196 v9ses
= v9fs_dentry2v9ses(dentry
);
1197 fid
= v9fs_fid_lookup(dentry
);
1199 return PTR_ERR(fid
);
1201 if (!v9fs_proto_dotu(v9ses
))
1204 st
= p9_client_stat(fid
);
1208 if (!(st
->mode
& P9_DMSYMLINK
)) {
1213 /* copy extension buffer into buffer */
1214 strncpy(buffer
, st
->extension
, buflen
);
1216 P9_DPRINTK(P9_DEBUG_VFS
,
1217 "%s -> %s (%s)\n", dentry
->d_name
.name
, st
->extension
, buffer
);
1219 retval
= strnlen(buffer
, buflen
);
1227 * v9fs_vfs_follow_link - follow a symlink path
1228 * @dentry: dentry for symlink
1233 static void *v9fs_vfs_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1236 char *link
= __getname();
1238 P9_DPRINTK(P9_DEBUG_VFS
, "%s n", dentry
->d_name
.name
);
1241 link
= ERR_PTR(-ENOMEM
);
1243 len
= v9fs_readlink(dentry
, link
, PATH_MAX
);
1247 link
= ERR_PTR(len
);
1249 link
[min(len
, PATH_MAX
-1)] = 0;
1251 nd_set_link(nd
, link
);
1257 * v9fs_vfs_put_link - release a symlink path
1258 * @dentry: dentry for symlink
1265 v9fs_vfs_put_link(struct dentry
*dentry
, struct nameidata
*nd
, void *p
)
1267 char *s
= nd_get_link(nd
);
1269 P9_DPRINTK(P9_DEBUG_VFS
, " %s %s\n", dentry
->d_name
.name
,
1270 IS_ERR(s
) ? "<error>" : s
);
1276 * v9fs_vfs_mkspecial - create a special file
1277 * @dir: inode to create special file in
1278 * @dentry: dentry to create
1279 * @mode: mode to create special file
1280 * @extension: 9p2000.u format extension string representing special file
1284 static int v9fs_vfs_mkspecial(struct inode
*dir
, struct dentry
*dentry
,
1285 int mode
, const char *extension
)
1289 struct v9fs_session_info
*v9ses
;
1291 v9ses
= v9fs_inode2v9ses(dir
);
1292 if (!v9fs_proto_dotu(v9ses
)) {
1293 P9_DPRINTK(P9_DEBUG_ERROR
, "not extended\n");
1297 perm
= unixmode2p9mode(v9ses
, mode
);
1298 fid
= v9fs_create(v9ses
, dir
, dentry
, (char *) extension
, perm
,
1301 return PTR_ERR(fid
);
1303 v9fs_invalidate_inode_attr(dir
);
1304 p9_client_clunk(fid
);
1309 * v9fs_vfs_symlink - helper function to create symlinks
1310 * @dir: directory inode containing symlink
1311 * @dentry: dentry for symlink
1312 * @symname: symlink data
1314 * See Also: 9P2000.u RFC for more information
1319 v9fs_vfs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *symname
)
1321 P9_DPRINTK(P9_DEBUG_VFS
, " %lu,%s,%s\n", dir
->i_ino
,
1322 dentry
->d_name
.name
, symname
);
1324 return v9fs_vfs_mkspecial(dir
, dentry
, S_IFLNK
, symname
);
1328 * v9fs_vfs_link - create a hardlink
1329 * @old_dentry: dentry for file to link to
1330 * @dir: inode destination for new link
1331 * @dentry: dentry for link
1336 v9fs_vfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
1337 struct dentry
*dentry
)
1341 struct p9_fid
*oldfid
;
1343 P9_DPRINTK(P9_DEBUG_VFS
,
1344 " %lu,%s,%s\n", dir
->i_ino
, dentry
->d_name
.name
,
1345 old_dentry
->d_name
.name
);
1347 oldfid
= v9fs_fid_clone(old_dentry
);
1349 return PTR_ERR(oldfid
);
1352 if (unlikely(!name
)) {
1357 sprintf(name
, "%d\n", oldfid
->fid
);
1358 retval
= v9fs_vfs_mkspecial(dir
, dentry
, P9_DMLINK
, name
);
1361 v9fs_refresh_inode(oldfid
, old_dentry
->d_inode
);
1362 v9fs_invalidate_inode_attr(dir
);
1365 p9_client_clunk(oldfid
);
1370 * v9fs_vfs_mknod - create a special file
1371 * @dir: inode destination for new link
1372 * @dentry: dentry for file
1373 * @mode: mode for creation
1374 * @rdev: device associated with special file
1379 v9fs_vfs_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
, dev_t rdev
)
1384 P9_DPRINTK(P9_DEBUG_VFS
,
1385 " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir
->i_ino
,
1386 dentry
->d_name
.name
, mode
, MAJOR(rdev
), MINOR(rdev
));
1388 if (!new_valid_dev(rdev
))
1394 /* build extension */
1396 sprintf(name
, "b %u %u", MAJOR(rdev
), MINOR(rdev
));
1397 else if (S_ISCHR(mode
))
1398 sprintf(name
, "c %u %u", MAJOR(rdev
), MINOR(rdev
));
1399 else if (S_ISFIFO(mode
))
1401 else if (S_ISSOCK(mode
))
1408 retval
= v9fs_vfs_mkspecial(dir
, dentry
, mode
, name
);
1414 int v9fs_refresh_inode(struct p9_fid
*fid
, struct inode
*inode
)
1419 struct p9_wstat
*st
;
1420 struct v9fs_session_info
*v9ses
;
1422 v9ses
= v9fs_inode2v9ses(inode
);
1423 st
= p9_client_stat(fid
);
1427 * Don't update inode if the file type is different
1429 umode
= p9mode2unixmode(v9ses
, st
, &rdev
);
1430 if ((inode
->i_mode
& S_IFMT
) != (umode
& S_IFMT
))
1433 spin_lock(&inode
->i_lock
);
1435 * We don't want to refresh inode->i_size,
1436 * because we may have cached data
1438 i_size
= inode
->i_size
;
1439 v9fs_stat2inode(st
, inode
, inode
->i_sb
);
1441 inode
->i_size
= i_size
;
1442 spin_unlock(&inode
->i_lock
);
1449 static const struct inode_operations v9fs_dir_inode_operations_dotu
= {
1450 .create
= v9fs_vfs_create
,
1451 .lookup
= v9fs_vfs_lookup
,
1452 .symlink
= v9fs_vfs_symlink
,
1453 .link
= v9fs_vfs_link
,
1454 .unlink
= v9fs_vfs_unlink
,
1455 .mkdir
= v9fs_vfs_mkdir
,
1456 .rmdir
= v9fs_vfs_rmdir
,
1457 .mknod
= v9fs_vfs_mknod
,
1458 .rename
= v9fs_vfs_rename
,
1459 .getattr
= v9fs_vfs_getattr
,
1460 .setattr
= v9fs_vfs_setattr
,
1463 static const struct inode_operations v9fs_dir_inode_operations
= {
1464 .create
= v9fs_vfs_create
,
1465 .lookup
= v9fs_vfs_lookup
,
1466 .unlink
= v9fs_vfs_unlink
,
1467 .mkdir
= v9fs_vfs_mkdir
,
1468 .rmdir
= v9fs_vfs_rmdir
,
1469 .mknod
= v9fs_vfs_mknod
,
1470 .rename
= v9fs_vfs_rename
,
1471 .getattr
= v9fs_vfs_getattr
,
1472 .setattr
= v9fs_vfs_setattr
,
1475 static const struct inode_operations v9fs_file_inode_operations
= {
1476 .getattr
= v9fs_vfs_getattr
,
1477 .setattr
= v9fs_vfs_setattr
,
1480 static const struct inode_operations v9fs_symlink_inode_operations
= {
1481 .readlink
= generic_readlink
,
1482 .follow_link
= v9fs_vfs_follow_link
,
1483 .put_link
= v9fs_vfs_put_link
,
1484 .getattr
= v9fs_vfs_getattr
,
1485 .setattr
= v9fs_vfs_setattr
,