xref: /titanic_50/usr/src/uts/common/fs/ctfs/ctfs_cdir.c (revision 24da5b34f49324ed742a340010ed5bd3d4e06625)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/time.h>
31 #include <sys/cred.h>
32 #include <sys/vfs.h>
33 #include <sys/vfs_opreg.h>
34 #include <sys/gfs.h>
35 #include <sys/vnode.h>
36 #include <sys/systm.h>
37 #include <sys/errno.h>
38 #include <sys/sysmacros.h>
39 #include <fs/fs_subr.h>
40 #include <sys/contract.h>
41 #include <sys/contract_impl.h>
42 #include <sys/ctfs.h>
43 #include <sys/ctfs_impl.h>
44 #include <sys/file.h>
45 #include <sys/pathname.h>
46 
47 /*
48  * Entries in a /system/contract/<type>/<ctid> directory.
49  */
50 static gfs_dirent_t ctfs_ctls[] = {
51 	{ "ctl", ctfs_create_ctlnode, GFS_CACHE_VNODE, },
52 	{ "status", ctfs_create_statnode, GFS_CACHE_VNODE },
53 	{ "events", ctfs_create_evnode },
54 	{ NULL }
55 };
56 #define	CTFS_NCTLS	((sizeof ctfs_ctls / sizeof (gfs_dirent_t)) - 1)
57 
58 static ino64_t ctfs_cdir_do_inode(vnode_t *, int);
59 
60 /*
61  * ctfs_create_cdirnode
62  *
63  * If necessary, creates a cdirnode for the specified contract and
64  * inserts it into the contract's list of vnodes.  Returns either the
65  * existing vnode or the new one.
66  */
67 vnode_t *
68 ctfs_create_cdirnode(vnode_t *pvp, contract_t *ct)
69 {
70 	vnode_t *vp;
71 	ctfs_cdirnode_t *cdir;
72 
73 	if ((vp = contract_vnode_get(ct, pvp->v_vfsp)) != NULL)
74 		return (vp);
75 
76 	vp = gfs_dir_create(sizeof (ctfs_cdirnode_t), pvp, ctfs_ops_cdir,
77 	    ctfs_ctls, ctfs_cdir_do_inode, CTFS_NAME_MAX, NULL, NULL);
78 	cdir = vp->v_data;
79 
80 	/*
81 	 * We must set the inode because this is called explicitly rather than
82 	 * through GFS callbacks.
83 	 */
84 	gfs_file_set_inode(vp, CTFS_INO_CT_DIR(ct->ct_id));
85 
86 	cdir->ctfs_cn_contract	= ct;
87 	contract_hold(ct);
88 	contract_vnode_set(ct, &cdir->ctfs_cn_linkage, vp);
89 
90 	return (vp);
91 }
92 
93 /*
94  * ctfs_cdir_getattr - VOP_GETATTR entry point
95  */
96 /* ARGSUSED */
97 static int
98 ctfs_cdir_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr)
99 {
100 	ctfs_cdirnode_t *cdirnode = vp->v_data;
101 
102 	vap->va_type = VDIR;
103 	vap->va_mode = 0555;
104 	vap->va_nlink = 2 + CTFS_NCTLS;
105 	vap->va_size = vap->va_nlink;
106 	vap->va_ctime = cdirnode->ctfs_cn_contract->ct_ctime;
107 	mutex_enter(&cdirnode->ctfs_cn_contract->ct_events.ctq_lock);
108 	vap->va_atime = vap->va_mtime =
109 	    cdirnode->ctfs_cn_contract->ct_events.ctq_atime;
110 	mutex_exit(&cdirnode->ctfs_cn_contract->ct_events.ctq_lock);
111 	ctfs_common_getattr(vp, vap);
112 
113 	return (0);
114 }
115 
116 /*
117  * ctfs_cdir_do_inode - return inode number based on static index
118  */
119 static ino64_t
120 ctfs_cdir_do_inode(vnode_t *vp, int index)
121 {
122 	ctfs_cdirnode_t *cdirnode = vp->v_data;
123 
124 	return (CTFS_INO_CT_FILE(cdirnode->ctfs_cn_contract->ct_id, index));
125 }
126 
127 /*
128  * ctfs_cdir_inactive - VOP_INACTIVE entry point
129  */
130 /* ARGSUSED */
131 static void
132 ctfs_cdir_inactive(vnode_t *vp, cred_t *cr)
133 {
134 	ctfs_cdirnode_t *cdirnode = vp->v_data;
135 	contract_t *ct = cdirnode->ctfs_cn_contract;
136 
137 	mutex_enter(&ct->ct_lock);
138 	if (gfs_dir_inactive(vp) == NULL) {
139 		mutex_exit(&ct->ct_lock);
140 		return;
141 	}
142 
143 	list_remove(&ct->ct_vnodes, &cdirnode->ctfs_cn_linkage);
144 	mutex_exit(&ct->ct_lock);
145 
146 	contract_rele(ct);
147 	kmem_free(cdirnode, sizeof (ctfs_cdirnode_t));
148 }
149 
150 
151 const fs_operation_def_t ctfs_tops_cdir[] = {
152 	{ VOPNAME_OPEN,		{ .vop_open = ctfs_open } },
153 	{ VOPNAME_CLOSE,	{ .vop_close = ctfs_close } },
154 	{ VOPNAME_IOCTL,	{ .error = fs_inval } },
155 	{ VOPNAME_GETATTR,	{ .vop_getattr = ctfs_cdir_getattr } },
156 	{ VOPNAME_ACCESS,	{ .vop_access = ctfs_access_dir } },
157 	{ VOPNAME_READDIR,	{ .vop_readdir = gfs_vop_readdir } },
158 	{ VOPNAME_LOOKUP,	{ .vop_lookup = gfs_vop_lookup } },
159 	{ VOPNAME_SEEK,		{ .vop_seek = fs_seek } },
160 	{ VOPNAME_INACTIVE,	{ .vop_inactive = ctfs_cdir_inactive } },
161 	{ NULL, NULL }
162 };
163