xref: /illumos-gate/usr/src/uts/common/fs/ctfs/ctfs_cdir.c (revision dd72704bd9e794056c558153663c739e2012d721)
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 #include <sys/types.h>
27 #include <sys/param.h>
28 #include <sys/time.h>
29 #include <sys/cred.h>
30 #include <sys/vfs.h>
31 #include <sys/vfs_opreg.h>
32 #include <sys/gfs.h>
33 #include <sys/vnode.h>
34 #include <sys/systm.h>
35 #include <sys/errno.h>
36 #include <sys/sysmacros.h>
37 #include <fs/fs_subr.h>
38 #include <sys/contract.h>
39 #include <sys/contract_impl.h>
40 #include <sys/ctfs.h>
41 #include <sys/ctfs_impl.h>
42 #include <sys/file.h>
43 #include <sys/pathname.h>
44 
45 /*
46  * Entries in a /system/contract/<type>/<ctid> directory.
47  */
48 static gfs_dirent_t ctfs_ctls[] = {
49 	{ "ctl", ctfs_create_ctlnode, GFS_CACHE_VNODE, },
50 	{ "status", ctfs_create_statnode, GFS_CACHE_VNODE },
51 	{ "events", ctfs_create_evnode },
52 	{ NULL }
53 };
54 #define	CTFS_NCTLS	((sizeof ctfs_ctls / sizeof (gfs_dirent_t)) - 1)
55 
56 static ino64_t ctfs_cdir_do_inode(vnode_t *, int);
57 
58 /*
59  * ctfs_create_cdirnode
60  *
61  * If necessary, creates a cdirnode for the specified contract and
62  * inserts it into the contract's list of vnodes.  Returns either the
63  * existing vnode or the new one.
64  */
65 vnode_t *
66 ctfs_create_cdirnode(vnode_t *pvp, contract_t *ct)
67 {
68 	vnode_t *vp;
69 	ctfs_cdirnode_t *cdir;
70 
71 	if ((vp = contract_vnode_get(ct, pvp->v_vfsp)) != NULL)
72 		return (vp);
73 
74 	vp = gfs_dir_create(sizeof (ctfs_cdirnode_t), pvp, ctfs_ops_cdir,
75 	    ctfs_ctls, ctfs_cdir_do_inode, CTFS_NAME_MAX, NULL, NULL);
76 	cdir = vp->v_data;
77 
78 	/*
79 	 * We must set the inode because this is called explicitly rather than
80 	 * through GFS callbacks.
81 	 */
82 	gfs_file_set_inode(vp, CTFS_INO_CT_DIR(ct->ct_id));
83 
84 	cdir->ctfs_cn_contract	= ct;
85 	contract_hold(ct);
86 	contract_vnode_set(ct, &cdir->ctfs_cn_linkage, vp);
87 
88 	return (vp);
89 }
90 
91 /*
92  * ctfs_cdir_getattr - VOP_GETATTR entry point
93  */
94 /* ARGSUSED */
95 static int
96 ctfs_cdir_getattr(
97 	vnode_t *vp,
98 	vattr_t *vap,
99 	int flags,
100 	cred_t *cr,
101 	caller_context_t *ct)
102 {
103 	ctfs_cdirnode_t *cdirnode = vp->v_data;
104 
105 	vap->va_type = VDIR;
106 	vap->va_mode = 0555;
107 	vap->va_nlink = 2 + CTFS_NCTLS;
108 	vap->va_size = vap->va_nlink;
109 	vap->va_ctime = cdirnode->ctfs_cn_contract->ct_ctime;
110 	mutex_enter(&cdirnode->ctfs_cn_contract->ct_events.ctq_lock);
111 	vap->va_atime = vap->va_mtime =
112 	    cdirnode->ctfs_cn_contract->ct_events.ctq_atime;
113 	mutex_exit(&cdirnode->ctfs_cn_contract->ct_events.ctq_lock);
114 	ctfs_common_getattr(vp, vap);
115 
116 	return (0);
117 }
118 
119 /*
120  * ctfs_cdir_do_inode - return inode number based on static index
121  */
122 static ino64_t
123 ctfs_cdir_do_inode(vnode_t *vp, int index)
124 {
125 	ctfs_cdirnode_t *cdirnode = vp->v_data;
126 
127 	return (CTFS_INO_CT_FILE(cdirnode->ctfs_cn_contract->ct_id, index));
128 }
129 
130 /*
131  * ctfs_cdir_inactive - VOP_INACTIVE entry point
132  */
133 /* ARGSUSED */
134 static void
135 ctfs_cdir_inactive(vnode_t *vp, cred_t *cr, caller_context_t *cct)
136 {
137 	ctfs_cdirnode_t *cdirnode = vp->v_data;
138 	contract_t *ct = cdirnode->ctfs_cn_contract;
139 
140 	mutex_enter(&ct->ct_lock);
141 	if (gfs_dir_inactive(vp) == NULL) {
142 		mutex_exit(&ct->ct_lock);
143 		return;
144 	}
145 
146 	list_remove(&ct->ct_vnodes, &cdirnode->ctfs_cn_linkage);
147 	mutex_exit(&ct->ct_lock);
148 
149 	contract_rele(ct);
150 	kmem_free(cdirnode, sizeof (ctfs_cdirnode_t));
151 }
152 
153 
154 const fs_operation_def_t ctfs_tops_cdir[] = {
155 	{ VOPNAME_OPEN,		{ .vop_open = ctfs_open } },
156 	{ VOPNAME_CLOSE,	{ .vop_close = ctfs_close } },
157 	{ VOPNAME_IOCTL,	{ .error = fs_inval } },
158 	{ VOPNAME_GETATTR,	{ .vop_getattr = ctfs_cdir_getattr } },
159 	{ VOPNAME_ACCESS,	{ .vop_access = ctfs_access_dir } },
160 	{ VOPNAME_READDIR,	{ .vop_readdir = gfs_vop_readdir } },
161 	{ VOPNAME_LOOKUP,	{ .vop_lookup = gfs_vop_lookup } },
162 	{ VOPNAME_SEEK,		{ .vop_seek = fs_seek } },
163 	{ VOPNAME_INACTIVE,	{ .vop_inactive = ctfs_cdir_inactive } },
164 	{ NULL, NULL }
165 };
166