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 */
50static 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
58static 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 */
67vnode_t *
68ctfs_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 */
97static int
98ctfs_cdir_getattr(
99	vnode_t *vp,
100	vattr_t *vap,
101	int flags,
102	cred_t *cr,
103	caller_context_t *ct)
104{
105	ctfs_cdirnode_t *cdirnode = vp->v_data;
106
107	vap->va_type = VDIR;
108	vap->va_mode = 0555;
109	vap->va_nlink = 2 + CTFS_NCTLS;
110	vap->va_size = vap->va_nlink;
111	vap->va_ctime = cdirnode->ctfs_cn_contract->ct_ctime;
112	mutex_enter(&cdirnode->ctfs_cn_contract->ct_events.ctq_lock);
113	vap->va_atime = vap->va_mtime =
114	    cdirnode->ctfs_cn_contract->ct_events.ctq_atime;
115	mutex_exit(&cdirnode->ctfs_cn_contract->ct_events.ctq_lock);
116	ctfs_common_getattr(vp, vap);
117
118	return (0);
119}
120
121/*
122 * ctfs_cdir_do_inode - return inode number based on static index
123 */
124static ino64_t
125ctfs_cdir_do_inode(vnode_t *vp, int index)
126{
127	ctfs_cdirnode_t *cdirnode = vp->v_data;
128
129	return (CTFS_INO_CT_FILE(cdirnode->ctfs_cn_contract->ct_id, index));
130}
131
132/*
133 * ctfs_cdir_inactive - VOP_INACTIVE entry point
134 */
135/* ARGSUSED */
136static void
137ctfs_cdir_inactive(vnode_t *vp, cred_t *cr, caller_context_t *cct)
138{
139	ctfs_cdirnode_t *cdirnode = vp->v_data;
140	contract_t *ct = cdirnode->ctfs_cn_contract;
141
142	mutex_enter(&ct->ct_lock);
143	if (gfs_dir_inactive(vp) == NULL) {
144		mutex_exit(&ct->ct_lock);
145		return;
146	}
147
148	list_remove(&ct->ct_vnodes, &cdirnode->ctfs_cn_linkage);
149	mutex_exit(&ct->ct_lock);
150
151	contract_rele(ct);
152	kmem_free(cdirnode, sizeof (ctfs_cdirnode_t));
153}
154
155
156const fs_operation_def_t ctfs_tops_cdir[] = {
157	{ VOPNAME_OPEN,		{ .vop_open = ctfs_open } },
158	{ VOPNAME_CLOSE,	{ .vop_close = ctfs_close } },
159	{ VOPNAME_IOCTL,	{ .error = fs_inval } },
160	{ VOPNAME_GETATTR,	{ .vop_getattr = ctfs_cdir_getattr } },
161	{ VOPNAME_ACCESS,	{ .vop_access = ctfs_access_dir } },
162	{ VOPNAME_READDIR,	{ .vop_readdir = gfs_vop_readdir } },
163	{ VOPNAME_LOOKUP,	{ .vop_lookup = gfs_vop_lookup } },
164	{ VOPNAME_SEEK,		{ .vop_seek = fs_seek } },
165	{ VOPNAME_INACTIVE,	{ .vop_inactive = ctfs_cdir_inactive } },
166	{ NULL, NULL }
167};
168