xref: /illumos-gate/usr/src/uts/common/fs/ctfs/ctfs_all.c (revision 9b9d39d2a32ff806d2431dbcc50968ef1e6d46b2)
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 2008 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  * CTFS routines for the /system/contract/all vnode.
47  */
48 
49 static int ctfs_adir_do_readdir(vnode_t *, void *, int *, offset_t *,
50     offset_t *, void *, int);
51 static int ctfs_adir_do_lookup(vnode_t *, const char *, vnode_t **, ino64_t *,
52     cred_t *, int, int *, pathname_t *);
53 
54 /*
55  * ctfs_create_adirnode
56  */
57 /* ARGSUSED */
58 vnode_t *
59 ctfs_create_adirnode(vnode_t *pvp)
60 {
61 	vnode_t *vp = gfs_dir_create(sizeof (ctfs_adirnode_t), pvp,
62 	    ctfs_ops_adir, NULL, NULL, CTFS_NAME_MAX, ctfs_adir_do_readdir,
63 	    ctfs_adir_do_lookup);
64 
65 	return (vp);
66 }
67 
68 /*
69  * ctfs_adir_getattr - VOP_GETATTR entry point
70  */
71 /* ARGSUSED */
72 static int
73 ctfs_adir_getattr(
74 	vnode_t *vp,
75 	vattr_t *vap,
76 	int flags,
77 	cred_t *cr,
78 	caller_context_t *ct)
79 {
80 	int i, total;
81 
82 	vap->va_type = VDIR;
83 	vap->va_mode = 0555;
84 	for (i = 0, total = 0; i < ct_ntypes; i++)
85 		total += contract_type_count(ct_types[i]);
86 	vap->va_nlink = 2;
87 	vap->va_size = 2 + total;
88 	vap->va_ctime.tv_sec = vp->v_vfsp->vfs_mtime;
89 	vap->va_ctime.tv_nsec = 0;
90 	vap->va_mtime = vap->va_atime = vap->va_ctime;
91 	ctfs_common_getattr(vp, vap);
92 
93 	return (0);
94 }
95 
96 /* ARGSUSED */
97 static int
98 ctfs_adir_do_lookup(vnode_t *vp, const char *nm, vnode_t **vpp, ino64_t *inop,
99     cred_t *cr, int flags, int *deflags, pathname_t *rpnp)
100 {
101 	int i;
102 	contract_t *ct;
103 
104 	i = stoi((char **)&nm);
105 	if (*nm != '\0')
106 		return (ENOENT);
107 
108 	ct = contract_ptr(i, VTOZONE(vp)->zone_uniqid);
109 	if (ct == NULL)
110 		return (ENOENT);
111 
112 	*vpp = ctfs_create_symnode(vp, ct);
113 	*inop = CTFS_INO_CT_LINK(ct->ct_id);
114 	contract_rele(ct);
115 
116 	return (0);
117 }
118 
119 /* ARGSUSED */
120 static int
121 ctfs_adir_do_readdir(vnode_t *vp, void *dp, int *eofp,
122     offset_t *offp, offset_t *nextp, void *unused, int flags)
123 {
124 	uint64_t zuniqid;
125 	ctid_t next;
126 	struct dirent64 *odp = dp;
127 
128 	ASSERT(!(flags & V_RDDIR_ENTFLAGS));
129 
130 	zuniqid = VTOZONE(vp)->zone_uniqid;
131 	next = contract_lookup(zuniqid, *offp);
132 
133 	if (next == -1) {
134 		*eofp = 1;
135 		return (0);
136 	}
137 
138 	odp->d_ino = CTFS_INO_CT_LINK(next);
139 	numtos(next, odp->d_name);
140 	*offp = next;
141 	*nextp = next + 1;
142 
143 	return (0);
144 }
145 
146 const fs_operation_def_t ctfs_tops_adir[] = {
147 	{ VOPNAME_OPEN,		{ .vop_open = ctfs_open } },
148 	{ VOPNAME_CLOSE,	{ .vop_close = ctfs_close } },
149 	{ VOPNAME_IOCTL,	{ .error = fs_inval } },
150 	{ VOPNAME_GETATTR,	{ .vop_getattr = ctfs_adir_getattr } },
151 	{ VOPNAME_ACCESS,	{ .vop_access = ctfs_access_dir } },
152 	{ VOPNAME_READDIR,	{ .vop_readdir = gfs_vop_readdir } },
153 	{ VOPNAME_LOOKUP,	{ .vop_lookup = gfs_vop_lookup } },
154 	{ VOPNAME_SEEK,		{ .vop_seek = fs_seek } },
155 	{ VOPNAME_INACTIVE,	{ .vop_inactive = gfs_vop_inactive } },
156 	{ NULL, NULL }
157 };
158