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 <net/9p/9p.h>
39 #include <net/9p/client.h>
46 static const struct inode_operations v9fs_dir_inode_operations
;
47 static const struct inode_operations v9fs_dir_inode_operations_dotu
;
48 static const struct inode_operations v9fs_dir_inode_operations_dotl
;
49 static const struct inode_operations v9fs_file_inode_operations
;
50 static const struct inode_operations v9fs_file_inode_operations_dotl
;
51 static const struct inode_operations v9fs_symlink_inode_operations
;
52 static const struct inode_operations v9fs_symlink_inode_operations_dotl
;
55 * unixmode2p9mode - convert unix mode bits to plan 9
56 * @v9ses: v9fs session information
57 * @mode: mode to convert
61 static int unixmode2p9mode(struct v9fs_session_info
*v9ses
, int mode
)
67 if (v9fs_proto_dotu(v9ses
)) {
70 if (v9ses
->nodev
== 0) {
74 res
|= P9_DMNAMEDPIPE
;
81 if ((mode
& S_ISUID
) == S_ISUID
)
83 if ((mode
& S_ISGID
) == S_ISGID
)
85 if ((mode
& S_ISVTX
) == S_ISVTX
)
87 if ((mode
& P9_DMLINK
))
95 * p9mode2unixmode- convert plan9 mode bits to unix mode bits
96 * @v9ses: v9fs session information
97 * @mode: mode to convert
101 static int p9mode2unixmode(struct v9fs_session_info
*v9ses
, int mode
)
107 if ((mode
& P9_DMDIR
) == P9_DMDIR
)
109 else if ((mode
& P9_DMSYMLINK
) && (v9fs_proto_dotu(v9ses
)))
111 else if ((mode
& P9_DMSOCKET
) && (v9fs_proto_dotu(v9ses
))
112 && (v9ses
->nodev
== 0))
114 else if ((mode
& P9_DMNAMEDPIPE
) && (v9fs_proto_dotu(v9ses
))
115 && (v9ses
->nodev
== 0))
117 else if ((mode
& P9_DMDEVICE
) && (v9fs_proto_dotu(v9ses
))
118 && (v9ses
->nodev
== 0))
123 if (v9fs_proto_dotu(v9ses
)) {
124 if ((mode
& P9_DMSETUID
) == P9_DMSETUID
)
127 if ((mode
& P9_DMSETGID
) == P9_DMSETGID
)
130 if ((mode
& P9_DMSETVTX
) == P9_DMSETVTX
)
138 * v9fs_uflags2omode- convert posix open flags to plan 9 mode bits
139 * @uflags: flags to convert
140 * @extended: if .u extensions are active
143 int v9fs_uflags2omode(int uflags
, int extended
)
163 if (uflags
& O_TRUNC
)
170 if (uflags
& O_APPEND
)
178 * v9fs_blank_wstat - helper function to setup a 9P stat structure
179 * @wstat: structure to initialize
184 v9fs_blank_wstat(struct p9_wstat
*wstat
)
188 wstat
->qid
.type
= ~0;
189 wstat
->qid
.version
= ~0;
190 *((long long *)&wstat
->qid
.path
) = ~0;
202 wstat
->extension
= NULL
;
205 #ifdef CONFIG_9P_FSCACHE
207 * v9fs_alloc_inode - helper function to allocate an inode
208 * This callback is executed before setting up the inode so that we
209 * can associate a vcookie with each inode.
213 struct inode
*v9fs_alloc_inode(struct super_block
*sb
)
215 struct v9fs_cookie
*vcookie
;
216 vcookie
= (struct v9fs_cookie
*)kmem_cache_alloc(vcookie_cache
,
221 vcookie
->fscache
= NULL
;
223 spin_lock_init(&vcookie
->lock
);
224 return &vcookie
->inode
;
228 * v9fs_destroy_inode - destroy an inode
232 void v9fs_destroy_inode(struct inode
*inode
)
234 kmem_cache_free(vcookie_cache
, v9fs_inode2cookie(inode
));
239 * v9fs_get_inode - helper function to setup an inode
241 * @mode: mode to setup inode with
245 struct inode
*v9fs_get_inode(struct super_block
*sb
, int mode
)
249 struct v9fs_session_info
*v9ses
= sb
->s_fs_info
;
251 P9_DPRINTK(P9_DEBUG_VFS
, "super block: %p mode: %o\n", sb
, mode
);
253 inode
= new_inode(sb
);
255 P9_EPRINTK(KERN_WARNING
, "Problem allocating inode\n");
256 return ERR_PTR(-ENOMEM
);
259 inode_init_owner(inode
, NULL
, mode
);
262 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
263 inode
->i_mapping
->a_ops
= &v9fs_addr_operations
;
265 switch (mode
& S_IFMT
) {
270 if (!v9fs_proto_dotu(v9ses
)) {
271 P9_DPRINTK(P9_DEBUG_ERROR
,
272 "special files without extended mode\n");
276 init_special_inode(inode
, inode
->i_mode
, inode
->i_rdev
);
279 if (v9fs_proto_dotl(v9ses
)) {
280 inode
->i_op
= &v9fs_file_inode_operations_dotl
;
281 inode
->i_fop
= &v9fs_file_operations_dotl
;
283 inode
->i_op
= &v9fs_file_inode_operations
;
284 inode
->i_fop
= &v9fs_file_operations
;
290 if (!v9fs_proto_dotu(v9ses
) && !v9fs_proto_dotl(v9ses
)) {
291 P9_DPRINTK(P9_DEBUG_ERROR
, "extended modes used with "
292 "legacy protocol.\n");
297 if (v9fs_proto_dotl(v9ses
))
298 inode
->i_op
= &v9fs_symlink_inode_operations_dotl
;
300 inode
->i_op
= &v9fs_symlink_inode_operations
;
305 if (v9fs_proto_dotl(v9ses
))
306 inode
->i_op
= &v9fs_dir_inode_operations_dotl
;
307 else if (v9fs_proto_dotu(v9ses
))
308 inode
->i_op
= &v9fs_dir_inode_operations_dotu
;
310 inode
->i_op
= &v9fs_dir_inode_operations
;
312 if (v9fs_proto_dotl(v9ses
))
313 inode
->i_fop
= &v9fs_dir_operations_dotl
;
315 inode
->i_fop
= &v9fs_dir_operations
;
319 P9_DPRINTK(P9_DEBUG_ERROR
, "BAD mode 0x%x S_IFMT 0x%x\n",
320 mode
, mode
& S_IFMT
);
333 static struct v9fs_fid*
334 v9fs_clone_walk(struct v9fs_session_info *v9ses, u32 fid, struct dentry *dentry)
338 struct v9fs_fid *ret;
339 struct v9fs_fcall *fcall;
341 nfid = v9fs_get_idpool(&v9ses->fidpool);
343 eprintk(KERN_WARNING, "no free fids available\n");
344 return ERR_PTR(-ENOSPC);
347 err = v9fs_t_walk(v9ses, fid, nfid, (char *) dentry->d_name.name,
351 if (fcall && fcall->id == RWALK)
354 PRINT_FCALL_ERROR("walk error", fcall);
355 v9fs_put_idpool(nfid, &v9ses->fidpool);
361 ret = v9fs_fid_create(v9ses, nfid);
367 err = v9fs_fid_insert(ret, dentry);
369 v9fs_fid_destroy(ret);
376 v9fs_t_clunk(v9ses, nfid);
386 * v9fs_clear_inode - release an inode
387 * @inode: inode to release
390 void v9fs_clear_inode(struct inode
*inode
)
392 filemap_fdatawrite(inode
->i_mapping
);
394 #ifdef CONFIG_9P_FSCACHE
395 v9fs_cache_inode_put_cookie(inode
);
399 static struct inode
*
400 v9fs_inode(struct v9fs_session_info
*v9ses
, struct p9_fid
*fid
,
401 struct super_block
*sb
)
404 struct inode
*ret
= NULL
;
407 st
= p9_client_stat(fid
);
411 umode
= p9mode2unixmode(v9ses
, st
->mode
);
412 ret
= v9fs_get_inode(sb
, umode
);
418 v9fs_stat2inode(st
, ret
, sb
);
419 ret
->i_ino
= v9fs_qid2ino(&st
->qid
);
421 #ifdef CONFIG_9P_FSCACHE
422 v9fs_vcookie_set_qid(ret
, &st
->qid
);
423 v9fs_cache_inode_get_cookie(ret
);
434 static struct inode
*
435 v9fs_inode_dotl(struct v9fs_session_info
*v9ses
, struct p9_fid
*fid
,
436 struct super_block
*sb
)
438 struct inode
*ret
= NULL
;
440 struct p9_stat_dotl
*st
;
442 st
= p9_client_getattr_dotl(fid
, P9_STATS_BASIC
);
446 ret
= v9fs_get_inode(sb
, st
->st_mode
);
452 v9fs_stat2inode_dotl(st
, ret
);
453 ret
->i_ino
= v9fs_qid2ino(&st
->qid
);
454 #ifdef CONFIG_9P_FSCACHE
455 v9fs_vcookie_set_qid(ret
, &st
->qid
);
456 v9fs_cache_inode_get_cookie(ret
);
466 * v9fs_inode_from_fid - Helper routine to populate an inode by
467 * issuing a attribute request
468 * @v9ses: session information
469 * @fid: fid to issue attribute request for
470 * @sb: superblock on which to create inode
473 static inline struct inode
*
474 v9fs_inode_from_fid(struct v9fs_session_info
*v9ses
, struct p9_fid
*fid
,
475 struct super_block
*sb
)
477 if (v9fs_proto_dotl(v9ses
))
478 return v9fs_inode_dotl(v9ses
, fid
, sb
);
480 return v9fs_inode(v9ses
, fid
, sb
);
484 * v9fs_remove - helper function to remove files and directories
485 * @dir: directory inode that is being deleted
486 * @file: dentry that is being deleted
487 * @rmdir: removing a directory
491 static int v9fs_remove(struct inode
*dir
, struct dentry
*file
, int rmdir
)
494 struct inode
*file_inode
;
495 struct p9_fid
*v9fid
;
497 P9_DPRINTK(P9_DEBUG_VFS
, "inode: %p dentry: %p rmdir: %d\n", dir
, file
,
500 file_inode
= file
->d_inode
;
501 v9fid
= v9fs_fid_clone(file
);
503 return PTR_ERR(v9fid
);
505 retval
= p9_client_remove(v9fid
);
507 drop_nlink(file_inode
);
512 v9fs_open_created(struct inode
*inode
, struct file
*file
)
519 * v9fs_create - Create a file
520 * @v9ses: session information
521 * @dir: directory that dentry is being created in
522 * @dentry: dentry that is being created
523 * @extension: 9p2000.u extension string to support devices, etc.
524 * @perm: create permissions
528 static struct p9_fid
*
529 v9fs_create(struct v9fs_session_info
*v9ses
, struct inode
*dir
,
530 struct dentry
*dentry
, char *extension
, u32 perm
, u8 mode
)
534 struct p9_fid
*dfid
, *ofid
, *fid
;
537 P9_DPRINTK(P9_DEBUG_VFS
, "name %s\n", dentry
->d_name
.name
);
542 name
= (char *) dentry
->d_name
.name
;
543 dfid
= v9fs_fid_lookup(dentry
->d_parent
);
546 P9_DPRINTK(P9_DEBUG_VFS
, "fid lookup failed %d\n", err
);
550 /* clone a fid to use for creation */
551 ofid
= p9_client_walk(dfid
, 0, NULL
, 1);
554 P9_DPRINTK(P9_DEBUG_VFS
, "p9_client_walk failed %d\n", err
);
558 err
= p9_client_fcreate(ofid
, name
, perm
, mode
, extension
);
560 P9_DPRINTK(P9_DEBUG_VFS
, "p9_client_fcreate failed %d\n", err
);
564 /* now walk from the parent so we can get unopened fid */
565 fid
= p9_client_walk(dfid
, 1, &name
, 1);
568 P9_DPRINTK(P9_DEBUG_VFS
, "p9_client_walk failed %d\n", err
);
573 /* instantiate inode and assign the unopened fid to the dentry */
574 inode
= v9fs_inode_from_fid(v9ses
, fid
, dir
->i_sb
);
576 err
= PTR_ERR(inode
);
577 P9_DPRINTK(P9_DEBUG_VFS
, "inode creation failed %d\n", err
);
582 dentry
->d_op
= &v9fs_cached_dentry_operations
;
584 dentry
->d_op
= &v9fs_dentry_operations
;
586 d_instantiate(dentry
, inode
);
587 err
= v9fs_fid_add(dentry
, fid
);
595 p9_client_clunk(ofid
);
598 p9_client_clunk(fid
);
604 * v9fs_vfs_create - VFS hook to create files
605 * @dir: directory inode that is being created
606 * @dentry: dentry that is being deleted
607 * @mode: create permissions
608 * @nd: path information
613 v9fs_vfs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
,
614 struct nameidata
*nd
)
619 struct v9fs_session_info
*v9ses
;
625 v9ses
= v9fs_inode2v9ses(dir
);
626 perm
= unixmode2p9mode(v9ses
, mode
);
627 if (nd
&& nd
->flags
& LOOKUP_OPEN
)
628 flags
= nd
->intent
.open
.flags
- 1;
632 fid
= v9fs_create(v9ses
, dir
, dentry
, NULL
, perm
,
633 v9fs_uflags2omode(flags
,
634 v9fs_proto_dotu(v9ses
)));
641 /* if we are opening a file, assign the open fid to the file */
642 if (nd
&& nd
->flags
& LOOKUP_OPEN
) {
643 filp
= lookup_instantiate_filp(nd
, dentry
, v9fs_open_created
);
649 filp
->private_data
= fid
;
651 p9_client_clunk(fid
);
657 p9_client_clunk(fid
);
663 * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
664 * @dir: inode that is being unlinked
665 * @dentry: dentry that is being unlinked
666 * @mode: mode for new directory
670 static int v9fs_vfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
674 struct v9fs_session_info
*v9ses
;
677 P9_DPRINTK(P9_DEBUG_VFS
, "name %s\n", dentry
->d_name
.name
);
679 v9ses
= v9fs_inode2v9ses(dir
);
680 perm
= unixmode2p9mode(v9ses
, mode
| S_IFDIR
);
681 fid
= v9fs_create(v9ses
, dir
, dentry
, NULL
, perm
, P9_OREAD
);
688 p9_client_clunk(fid
);
694 * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
695 * @dir: inode that is being walked from
696 * @dentry: dentry that is being walked to?
697 * @nameidata: path data
701 static struct dentry
*v9fs_vfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
702 struct nameidata
*nameidata
)
704 struct super_block
*sb
;
705 struct v9fs_session_info
*v9ses
;
706 struct p9_fid
*dfid
, *fid
;
711 P9_DPRINTK(P9_DEBUG_VFS
, "dir: %p dentry: (%s) %p nameidata: %p\n",
712 dir
, dentry
->d_name
.name
, dentry
, nameidata
);
714 if (dentry
->d_name
.len
> NAME_MAX
)
715 return ERR_PTR(-ENAMETOOLONG
);
718 v9ses
= v9fs_inode2v9ses(dir
);
719 dfid
= v9fs_fid_lookup(dentry
->d_parent
);
721 return ERR_CAST(dfid
);
723 name
= (char *) dentry
->d_name
.name
;
724 fid
= p9_client_walk(dfid
, 1, &name
, 1);
726 result
= PTR_ERR(fid
);
727 if (result
== -ENOENT
) {
732 return ERR_PTR(result
);
735 inode
= v9fs_inode_from_fid(v9ses
, fid
, dir
->i_sb
);
737 result
= PTR_ERR(inode
);
742 result
= v9fs_fid_add(dentry
, fid
);
748 dentry
->d_op
= &v9fs_cached_dentry_operations
;
750 dentry
->d_op
= &v9fs_dentry_operations
;
752 d_add(dentry
, inode
);
756 p9_client_clunk(fid
);
758 return ERR_PTR(result
);
762 * v9fs_vfs_unlink - VFS unlink hook to delete an inode
763 * @i: inode that is being unlinked
764 * @d: dentry that is being unlinked
768 static int v9fs_vfs_unlink(struct inode
*i
, struct dentry
*d
)
770 return v9fs_remove(i
, d
, 0);
774 * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
775 * @i: inode that is being unlinked
776 * @d: dentry that is being unlinked
780 static int v9fs_vfs_rmdir(struct inode
*i
, struct dentry
*d
)
782 return v9fs_remove(i
, d
, 1);
786 * v9fs_vfs_rename - VFS hook to rename an inode
787 * @old_dir: old dir inode
788 * @old_dentry: old dentry
789 * @new_dir: new dir inode
790 * @new_dentry: new dentry
795 v9fs_vfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
796 struct inode
*new_dir
, struct dentry
*new_dentry
)
798 struct inode
*old_inode
;
799 struct v9fs_session_info
*v9ses
;
800 struct p9_fid
*oldfid
;
801 struct p9_fid
*olddirfid
;
802 struct p9_fid
*newdirfid
;
803 struct p9_wstat wstat
;
806 P9_DPRINTK(P9_DEBUG_VFS
, "\n");
808 old_inode
= old_dentry
->d_inode
;
809 v9ses
= v9fs_inode2v9ses(old_inode
);
810 oldfid
= v9fs_fid_lookup(old_dentry
);
812 return PTR_ERR(oldfid
);
814 olddirfid
= v9fs_fid_clone(old_dentry
->d_parent
);
815 if (IS_ERR(olddirfid
)) {
816 retval
= PTR_ERR(olddirfid
);
820 newdirfid
= v9fs_fid_clone(new_dentry
->d_parent
);
821 if (IS_ERR(newdirfid
)) {
822 retval
= PTR_ERR(newdirfid
);
826 if (v9fs_proto_dotl(v9ses
)) {
827 retval
= p9_client_rename(oldfid
, newdirfid
,
828 (char *) new_dentry
->d_name
.name
);
829 if (retval
!= -ENOSYS
)
833 /* 9P can only handle file rename in the same directory */
834 if (memcmp(&olddirfid
->qid
, &newdirfid
->qid
, sizeof(newdirfid
->qid
))) {
835 P9_DPRINTK(P9_DEBUG_ERROR
,
836 "old dir and new dir are different\n");
841 v9fs_blank_wstat(&wstat
);
842 wstat
.muid
= v9ses
->uname
;
843 wstat
.name
= (char *) new_dentry
->d_name
.name
;
844 retval
= p9_client_wstat(oldfid
, &wstat
);
847 p9_client_clunk(newdirfid
);
850 p9_client_clunk(olddirfid
);
857 * v9fs_vfs_getattr - retrieve file metadata
858 * @mnt: mount information
859 * @dentry: file to get attributes on
860 * @stat: metadata structure to populate
865 v9fs_vfs_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
869 struct v9fs_session_info
*v9ses
;
873 P9_DPRINTK(P9_DEBUG_VFS
, "dentry: %p\n", dentry
);
875 v9ses
= v9fs_inode2v9ses(dentry
->d_inode
);
876 if (v9ses
->cache
== CACHE_LOOSE
|| v9ses
->cache
== CACHE_FSCACHE
)
877 return simple_getattr(mnt
, dentry
, stat
);
879 fid
= v9fs_fid_lookup(dentry
);
883 st
= p9_client_stat(fid
);
887 v9fs_stat2inode(st
, dentry
->d_inode
, dentry
->d_inode
->i_sb
);
888 generic_fillattr(dentry
->d_inode
, stat
);
895 v9fs_vfs_getattr_dotl(struct vfsmount
*mnt
, struct dentry
*dentry
,
899 struct v9fs_session_info
*v9ses
;
901 struct p9_stat_dotl
*st
;
903 P9_DPRINTK(P9_DEBUG_VFS
, "dentry: %p\n", dentry
);
905 v9ses
= v9fs_inode2v9ses(dentry
->d_inode
);
906 if (v9ses
->cache
== CACHE_LOOSE
|| v9ses
->cache
== CACHE_FSCACHE
)
907 return simple_getattr(mnt
, dentry
, stat
);
909 fid
= v9fs_fid_lookup(dentry
);
913 /* Ask for all the fields in stat structure. Server will return
914 * whatever it supports
917 st
= p9_client_getattr_dotl(fid
, P9_STATS_ALL
);
921 v9fs_stat2inode_dotl(st
, dentry
->d_inode
);
922 generic_fillattr(dentry
->d_inode
, stat
);
923 /* Change block size to what the server returned */
924 stat
->blksize
= st
->st_blksize
;
931 * v9fs_vfs_setattr - set file metadata
932 * @dentry: file whose metadata to set
933 * @iattr: metadata assignment structure
937 static int v9fs_vfs_setattr(struct dentry
*dentry
, struct iattr
*iattr
)
940 struct v9fs_session_info
*v9ses
;
942 struct p9_wstat wstat
;
944 P9_DPRINTK(P9_DEBUG_VFS
, "\n");
946 v9ses
= v9fs_inode2v9ses(dentry
->d_inode
);
947 fid
= v9fs_fid_lookup(dentry
);
951 v9fs_blank_wstat(&wstat
);
952 if (iattr
->ia_valid
& ATTR_MODE
)
953 wstat
.mode
= unixmode2p9mode(v9ses
, iattr
->ia_mode
);
955 if (iattr
->ia_valid
& ATTR_MTIME
)
956 wstat
.mtime
= iattr
->ia_mtime
.tv_sec
;
958 if (iattr
->ia_valid
& ATTR_ATIME
)
959 wstat
.atime
= iattr
->ia_atime
.tv_sec
;
961 if (iattr
->ia_valid
& ATTR_SIZE
)
962 wstat
.length
= iattr
->ia_size
;
964 if (v9fs_proto_dotu(v9ses
)) {
965 if (iattr
->ia_valid
& ATTR_UID
)
966 wstat
.n_uid
= iattr
->ia_uid
;
968 if (iattr
->ia_valid
& ATTR_GID
)
969 wstat
.n_gid
= iattr
->ia_gid
;
972 retval
= p9_client_wstat(fid
, &wstat
);
974 retval
= inode_setattr(dentry
->d_inode
, iattr
);
980 * v9fs_vfs_setattr_dotl - set file metadata
981 * @dentry: file whose metadata to set
982 * @iattr: metadata assignment structure
986 static int v9fs_vfs_setattr_dotl(struct dentry
*dentry
, struct iattr
*iattr
)
989 struct v9fs_session_info
*v9ses
;
991 struct p9_iattr_dotl p9attr
;
993 P9_DPRINTK(P9_DEBUG_VFS
, "\n");
995 retval
= inode_change_ok(dentry
->d_inode
, iattr
);
999 p9attr
.valid
= iattr
->ia_valid
;
1000 p9attr
.mode
= iattr
->ia_mode
;
1001 p9attr
.uid
= iattr
->ia_uid
;
1002 p9attr
.gid
= iattr
->ia_gid
;
1003 p9attr
.size
= iattr
->ia_size
;
1004 p9attr
.atime_sec
= iattr
->ia_atime
.tv_sec
;
1005 p9attr
.atime_nsec
= iattr
->ia_atime
.tv_nsec
;
1006 p9attr
.mtime_sec
= iattr
->ia_mtime
.tv_sec
;
1007 p9attr
.mtime_nsec
= iattr
->ia_mtime
.tv_nsec
;
1010 v9ses
= v9fs_inode2v9ses(dentry
->d_inode
);
1011 fid
= v9fs_fid_lookup(dentry
);
1013 return PTR_ERR(fid
);
1015 retval
= p9_client_setattr(fid
, &p9attr
);
1017 retval
= inode_setattr(dentry
->d_inode
, iattr
);
1023 * v9fs_stat2inode - populate an inode structure with mistat info
1024 * @stat: Plan 9 metadata (mistat) structure
1025 * @inode: inode to populate
1026 * @sb: superblock of filesystem
1031 v9fs_stat2inode(struct p9_wstat
*stat
, struct inode
*inode
,
1032 struct super_block
*sb
)
1036 unsigned int i_nlink
;
1037 struct v9fs_session_info
*v9ses
= sb
->s_fs_info
;
1041 inode
->i_atime
.tv_sec
= stat
->atime
;
1042 inode
->i_mtime
.tv_sec
= stat
->mtime
;
1043 inode
->i_ctime
.tv_sec
= stat
->mtime
;
1045 inode
->i_uid
= v9ses
->dfltuid
;
1046 inode
->i_gid
= v9ses
->dfltgid
;
1048 if (v9fs_proto_dotu(v9ses
)) {
1049 inode
->i_uid
= stat
->n_uid
;
1050 inode
->i_gid
= stat
->n_gid
;
1052 if ((S_ISREG(inode
->i_mode
)) || (S_ISDIR(inode
->i_mode
))) {
1053 if (v9fs_proto_dotu(v9ses
) && (stat
->extension
[0] != '\0')) {
1055 * Hadlink support got added later to
1056 * to the .u extension. So there can be
1057 * server out there that doesn't support
1058 * this even with .u extension. So check
1059 * for non NULL stat->extension
1061 strncpy(ext
, stat
->extension
, sizeof(ext
));
1062 /* HARDLINKCOUNT %u */
1063 sscanf(ext
, "%13s %u", tag_name
, &i_nlink
);
1064 if (!strncmp(tag_name
, "HARDLINKCOUNT", 13))
1065 inode
->i_nlink
= i_nlink
;
1068 inode
->i_mode
= p9mode2unixmode(v9ses
, stat
->mode
);
1069 if ((S_ISBLK(inode
->i_mode
)) || (S_ISCHR(inode
->i_mode
))) {
1074 strncpy(ext
, stat
->extension
, sizeof(ext
));
1075 sscanf(ext
, "%c %u %u", &type
, &major
, &minor
);
1078 inode
->i_mode
&= ~S_IFBLK
;
1079 inode
->i_mode
|= S_IFCHR
;
1084 P9_DPRINTK(P9_DEBUG_ERROR
,
1085 "Unknown special type %c %s\n", type
,
1088 inode
->i_rdev
= MKDEV(major
, minor
);
1089 init_special_inode(inode
, inode
->i_mode
, inode
->i_rdev
);
1093 i_size_write(inode
, stat
->length
);
1095 /* not real number of blocks, but 512 byte ones ... */
1096 inode
->i_blocks
= (i_size_read(inode
) + 512 - 1) >> 9;
1100 * v9fs_stat2inode_dotl - populate an inode structure with stat info
1101 * @stat: stat structure
1102 * @inode: inode to populate
1103 * @sb: superblock of filesystem
1108 v9fs_stat2inode_dotl(struct p9_stat_dotl
*stat
, struct inode
*inode
)
1111 if ((stat
->st_result_mask
& P9_STATS_BASIC
) == P9_STATS_BASIC
) {
1112 inode
->i_atime
.tv_sec
= stat
->st_atime_sec
;
1113 inode
->i_atime
.tv_nsec
= stat
->st_atime_nsec
;
1114 inode
->i_mtime
.tv_sec
= stat
->st_mtime_sec
;
1115 inode
->i_mtime
.tv_nsec
= stat
->st_mtime_nsec
;
1116 inode
->i_ctime
.tv_sec
= stat
->st_ctime_sec
;
1117 inode
->i_ctime
.tv_nsec
= stat
->st_ctime_nsec
;
1118 inode
->i_uid
= stat
->st_uid
;
1119 inode
->i_gid
= stat
->st_gid
;
1120 inode
->i_nlink
= stat
->st_nlink
;
1121 inode
->i_mode
= stat
->st_mode
;
1122 inode
->i_rdev
= new_decode_dev(stat
->st_rdev
);
1124 if ((S_ISBLK(inode
->i_mode
)) || (S_ISCHR(inode
->i_mode
)))
1125 init_special_inode(inode
, inode
->i_mode
, inode
->i_rdev
);
1127 i_size_write(inode
, stat
->st_size
);
1128 inode
->i_blocks
= stat
->st_blocks
;
1130 if (stat
->st_result_mask
& P9_STATS_ATIME
) {
1131 inode
->i_atime
.tv_sec
= stat
->st_atime_sec
;
1132 inode
->i_atime
.tv_nsec
= stat
->st_atime_nsec
;
1134 if (stat
->st_result_mask
& P9_STATS_MTIME
) {
1135 inode
->i_mtime
.tv_sec
= stat
->st_mtime_sec
;
1136 inode
->i_mtime
.tv_nsec
= stat
->st_mtime_nsec
;
1138 if (stat
->st_result_mask
& P9_STATS_CTIME
) {
1139 inode
->i_ctime
.tv_sec
= stat
->st_ctime_sec
;
1140 inode
->i_ctime
.tv_nsec
= stat
->st_ctime_nsec
;
1142 if (stat
->st_result_mask
& P9_STATS_UID
)
1143 inode
->i_uid
= stat
->st_uid
;
1144 if (stat
->st_result_mask
& P9_STATS_GID
)
1145 inode
->i_gid
= stat
->st_gid
;
1146 if (stat
->st_result_mask
& P9_STATS_NLINK
)
1147 inode
->i_nlink
= stat
->st_nlink
;
1148 if (stat
->st_result_mask
& P9_STATS_MODE
) {
1149 inode
->i_mode
= stat
->st_mode
;
1150 if ((S_ISBLK(inode
->i_mode
)) ||
1151 (S_ISCHR(inode
->i_mode
)))
1152 init_special_inode(inode
, inode
->i_mode
,
1155 if (stat
->st_result_mask
& P9_STATS_RDEV
)
1156 inode
->i_rdev
= new_decode_dev(stat
->st_rdev
);
1157 if (stat
->st_result_mask
& P9_STATS_SIZE
)
1158 i_size_write(inode
, stat
->st_size
);
1159 if (stat
->st_result_mask
& P9_STATS_BLOCKS
)
1160 inode
->i_blocks
= stat
->st_blocks
;
1162 if (stat
->st_result_mask
& P9_STATS_GEN
)
1163 inode
->i_generation
= stat
->st_gen
;
1165 /* Currently we don't support P9_STATS_BTIME and P9_STATS_DATA_VERSION
1166 * because the inode structure does not have fields for them.
1171 * v9fs_qid2ino - convert qid into inode number
1174 * BUG: potential for inode number collisions?
1177 ino_t
v9fs_qid2ino(struct p9_qid
*qid
)
1179 u64 path
= qid
->path
+ 2;
1182 if (sizeof(ino_t
) == sizeof(path
))
1183 memcpy(&i
, &path
, sizeof(ino_t
));
1185 i
= (ino_t
) (path
^ (path
>> 32));
1191 * v9fs_readlink - read a symlink's location (internal version)
1192 * @dentry: dentry for symlink
1193 * @buffer: buffer to load symlink location into
1194 * @buflen: length of buffer
1198 static int v9fs_readlink(struct dentry
*dentry
, char *buffer
, int buflen
)
1202 struct v9fs_session_info
*v9ses
;
1204 struct p9_wstat
*st
;
1206 P9_DPRINTK(P9_DEBUG_VFS
, " %s\n", dentry
->d_name
.name
);
1208 v9ses
= v9fs_inode2v9ses(dentry
->d_inode
);
1209 fid
= v9fs_fid_lookup(dentry
);
1211 return PTR_ERR(fid
);
1213 if (!v9fs_proto_dotu(v9ses
))
1216 st
= p9_client_stat(fid
);
1220 if (!(st
->mode
& P9_DMSYMLINK
)) {
1225 /* copy extension buffer into buffer */
1226 strncpy(buffer
, st
->extension
, buflen
);
1228 P9_DPRINTK(P9_DEBUG_VFS
,
1229 "%s -> %s (%s)\n", dentry
->d_name
.name
, st
->extension
, buffer
);
1231 retval
= strnlen(buffer
, buflen
);
1238 * v9fs_vfs_follow_link - follow a symlink path
1239 * @dentry: dentry for symlink
1244 static void *v9fs_vfs_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1247 char *link
= __getname();
1249 P9_DPRINTK(P9_DEBUG_VFS
, "%s n", dentry
->d_name
.name
);
1252 link
= ERR_PTR(-ENOMEM
);
1254 len
= v9fs_readlink(dentry
, link
, PATH_MAX
);
1258 link
= ERR_PTR(len
);
1260 link
[min(len
, PATH_MAX
-1)] = 0;
1262 nd_set_link(nd
, link
);
1268 * v9fs_vfs_put_link - release a symlink path
1269 * @dentry: dentry for symlink
1276 v9fs_vfs_put_link(struct dentry
*dentry
, struct nameidata
*nd
, void *p
)
1278 char *s
= nd_get_link(nd
);
1280 P9_DPRINTK(P9_DEBUG_VFS
, " %s %s\n", dentry
->d_name
.name
,
1281 IS_ERR(s
) ? "<error>" : s
);
1287 * v9fs_vfs_mkspecial - create a special file
1288 * @dir: inode to create special file in
1289 * @dentry: dentry to create
1290 * @mode: mode to create special file
1291 * @extension: 9p2000.u format extension string representing special file
1295 static int v9fs_vfs_mkspecial(struct inode
*dir
, struct dentry
*dentry
,
1296 int mode
, const char *extension
)
1299 struct v9fs_session_info
*v9ses
;
1302 v9ses
= v9fs_inode2v9ses(dir
);
1303 if (!v9fs_proto_dotu(v9ses
)) {
1304 P9_DPRINTK(P9_DEBUG_ERROR
, "not extended\n");
1308 perm
= unixmode2p9mode(v9ses
, mode
);
1309 fid
= v9fs_create(v9ses
, dir
, dentry
, (char *) extension
, perm
,
1312 return PTR_ERR(fid
);
1314 p9_client_clunk(fid
);
1319 * v9fs_vfs_symlink - helper function to create symlinks
1320 * @dir: directory inode containing symlink
1321 * @dentry: dentry for symlink
1322 * @symname: symlink data
1324 * See Also: 9P2000.u RFC for more information
1329 v9fs_vfs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *symname
)
1331 P9_DPRINTK(P9_DEBUG_VFS
, " %lu,%s,%s\n", dir
->i_ino
,
1332 dentry
->d_name
.name
, symname
);
1334 return v9fs_vfs_mkspecial(dir
, dentry
, S_IFLNK
, symname
);
1338 * v9fs_vfs_link - create a hardlink
1339 * @old_dentry: dentry for file to link to
1340 * @dir: inode destination for new link
1341 * @dentry: dentry for link
1346 v9fs_vfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
1347 struct dentry
*dentry
)
1350 struct p9_fid
*oldfid
;
1353 P9_DPRINTK(P9_DEBUG_VFS
,
1354 " %lu,%s,%s\n", dir
->i_ino
, dentry
->d_name
.name
,
1355 old_dentry
->d_name
.name
);
1357 oldfid
= v9fs_fid_clone(old_dentry
);
1359 return PTR_ERR(oldfid
);
1362 if (unlikely(!name
)) {
1367 sprintf(name
, "%d\n", oldfid
->fid
);
1368 retval
= v9fs_vfs_mkspecial(dir
, dentry
, P9_DMLINK
, name
);
1372 p9_client_clunk(oldfid
);
1377 * v9fs_vfs_mknod - create a special file
1378 * @dir: inode destination for new link
1379 * @dentry: dentry for file
1380 * @mode: mode for creation
1381 * @rdev: device associated with special file
1386 v9fs_vfs_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
, dev_t rdev
)
1391 P9_DPRINTK(P9_DEBUG_VFS
,
1392 " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir
->i_ino
,
1393 dentry
->d_name
.name
, mode
, MAJOR(rdev
), MINOR(rdev
));
1395 if (!new_valid_dev(rdev
))
1401 /* build extension */
1403 sprintf(name
, "b %u %u", MAJOR(rdev
), MINOR(rdev
));
1404 else if (S_ISCHR(mode
))
1405 sprintf(name
, "c %u %u", MAJOR(rdev
), MINOR(rdev
));
1406 else if (S_ISFIFO(mode
))
1408 else if (S_ISSOCK(mode
))
1415 retval
= v9fs_vfs_mkspecial(dir
, dentry
, mode
, name
);
1421 static const struct inode_operations v9fs_dir_inode_operations_dotu
= {
1422 .create
= v9fs_vfs_create
,
1423 .lookup
= v9fs_vfs_lookup
,
1424 .symlink
= v9fs_vfs_symlink
,
1425 .link
= v9fs_vfs_link
,
1426 .unlink
= v9fs_vfs_unlink
,
1427 .mkdir
= v9fs_vfs_mkdir
,
1428 .rmdir
= v9fs_vfs_rmdir
,
1429 .mknod
= v9fs_vfs_mknod
,
1430 .rename
= v9fs_vfs_rename
,
1431 .getattr
= v9fs_vfs_getattr
,
1432 .setattr
= v9fs_vfs_setattr
,
1435 static const struct inode_operations v9fs_dir_inode_operations_dotl
= {
1436 .create
= v9fs_vfs_create
,
1437 .lookup
= v9fs_vfs_lookup
,
1438 .symlink
= v9fs_vfs_symlink
,
1439 .link
= v9fs_vfs_link
,
1440 .unlink
= v9fs_vfs_unlink
,
1441 .mkdir
= v9fs_vfs_mkdir
,
1442 .rmdir
= v9fs_vfs_rmdir
,
1443 .mknod
= v9fs_vfs_mknod
,
1444 .rename
= v9fs_vfs_rename
,
1445 .getattr
= v9fs_vfs_getattr_dotl
,
1446 .setattr
= v9fs_vfs_setattr_dotl
,
1449 static const struct inode_operations v9fs_dir_inode_operations
= {
1450 .create
= v9fs_vfs_create
,
1451 .lookup
= v9fs_vfs_lookup
,
1452 .unlink
= v9fs_vfs_unlink
,
1453 .mkdir
= v9fs_vfs_mkdir
,
1454 .rmdir
= v9fs_vfs_rmdir
,
1455 .mknod
= v9fs_vfs_mknod
,
1456 .rename
= v9fs_vfs_rename
,
1457 .getattr
= v9fs_vfs_getattr
,
1458 .setattr
= v9fs_vfs_setattr
,
1461 static const struct inode_operations v9fs_file_inode_operations
= {
1462 .getattr
= v9fs_vfs_getattr
,
1463 .setattr
= v9fs_vfs_setattr
,
1466 static const struct inode_operations v9fs_file_inode_operations_dotl
= {
1467 .getattr
= v9fs_vfs_getattr_dotl
,
1468 .setattr
= v9fs_vfs_setattr_dotl
,
1471 static const struct inode_operations v9fs_symlink_inode_operations
= {
1472 .readlink
= generic_readlink
,
1473 .follow_link
= v9fs_vfs_follow_link
,
1474 .put_link
= v9fs_vfs_put_link
,
1475 .getattr
= v9fs_vfs_getattr
,
1476 .setattr
= v9fs_vfs_setattr
,
1479 static const struct inode_operations v9fs_symlink_inode_operations_dotl
= {
1480 .readlink
= generic_readlink
,
1481 .follow_link
= v9fs_vfs_follow_link
,
1482 .put_link
= v9fs_vfs_put_link
,
1483 .getattr
= v9fs_vfs_getattr_dotl
,
1484 .setattr
= v9fs_vfs_setattr_dotl
,