xref: /illumos-gate/usr/src/uts/common/fs/ctfs/ctfs_tdir.c (revision 2d6eb4a5e0a47d30189497241345dc5466bb68ab)
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  * Entries in a /system/contract/<type> directory.
47  */
48 static gfs_dirent_t ctfs_tdir_dirents[] = {
49 	{ "bundle", ctfs_create_bundle },
50 	{ "pbundle", ctfs_create_pbundle },
51 	{ "template", ctfs_create_tmplnode },
52 	{ "latest", ctfs_create_latenode, GFS_CACHE_VNODE },
53 	{ NULL }
54 };
55 #define	CTFS_NSPECIALS	((sizeof ctfs_tdir_dirents / sizeof (gfs_dirent_t)) - 1)
56 
57 static int ctfs_tdir_do_readdir(vnode_t *, void *, int *, offset_t *,
58     offset_t *, void *, int);
59 static int ctfs_tdir_do_lookup(vnode_t *, const char *, vnode_t **, ino64_t *,
60     cred_t *, int, int *, pathname_t *);
61 static ino64_t ctfs_tdir_do_inode(vnode_t *, int);
62 
63 /*
64  * ctfs_create_tdirnode
65  */
66 vnode_t *
ctfs_create_tdirnode(vnode_t * pvp)67 ctfs_create_tdirnode(vnode_t *pvp)
68 {
69 	return (gfs_dir_create(sizeof (ctfs_tdirnode_t), pvp, ctfs_ops_tdir,
70 	    ctfs_tdir_dirents, ctfs_tdir_do_inode, CTFS_NAME_MAX,
71 	    ctfs_tdir_do_readdir, ctfs_tdir_do_lookup));
72 }
73 
74 /*
75  * ctfs_tdir_getattr - VOP_GETATTR entry point
76  */
77 /* ARGSUSED */
78 static int
ctfs_tdir_getattr(vnode_t * vp,vattr_t * vap,int flags,cred_t * cr,caller_context_t * ct)79 ctfs_tdir_getattr(
80 	vnode_t *vp,
81 	vattr_t *vap,
82 	int flags,
83 	cred_t *cr,
84 	caller_context_t *ct)
85 {
86 	vap->va_type = VDIR;
87 	vap->va_mode = 0555;
88 	vap->va_nlink = 2 + CTFS_NSPECIALS;
89 	vap->va_size = vap->va_nlink +
90 	    contract_type_count(ct_types[gfs_file_index(vp)]);
91 	vap->va_ctime.tv_sec = vp->v_vfsp->vfs_mtime;
92 	vap->va_ctime.tv_nsec = 0;
93 	contract_type_time(ct_types[gfs_file_index(vp)], &vap->va_atime);
94 	vap->va_mtime = vap->va_atime;
95 	ctfs_common_getattr(vp, vap);
96 
97 	return (0);
98 }
99 
100 static ino64_t
ctfs_tdir_do_inode(vnode_t * vp,int index)101 ctfs_tdir_do_inode(vnode_t *vp, int index)
102 {
103 	return (CTFS_INO_TYPE_FILE(gfs_file_index(vp), index));
104 }
105 
106 /* ARGSUSED */
107 static int
ctfs_tdir_do_readdir(vnode_t * vp,void * dp,int * eofp,offset_t * offp,offset_t * nextp,void * data,int flags)108 ctfs_tdir_do_readdir(vnode_t *vp, void *dp, int *eofp,
109     offset_t *offp, offset_t *nextp, void *data, int flags)
110 {
111 	uint64_t zuniqid;
112 	ctid_t next;
113 	ct_type_t *ty = ct_types[gfs_file_index(vp)];
114 	struct dirent64 *odp = dp;
115 
116 	ASSERT(!(flags & V_RDDIR_ENTFLAGS));
117 
118 	zuniqid = VTOZONE(vp)->zone_uniqid;
119 	next = contract_type_lookup(ty, zuniqid, *offp);
120 
121 	if (next == -1) {
122 		*eofp = 1;
123 		return (0);
124 	}
125 
126 	odp->d_ino = CTFS_INO_CT_DIR(next);
127 	numtos(next, odp->d_name);
128 	*offp = next;
129 	*nextp = next + 1;
130 
131 	return (0);
132 }
133 
134 /* ARGSUSED */
135 static int
ctfs_tdir_do_lookup(vnode_t * vp,const char * nm,vnode_t ** vpp,ino64_t * inop,cred_t * cr,int flags,int * deflags,pathname_t * rpnp)136 ctfs_tdir_do_lookup(vnode_t *vp, const char *nm, vnode_t **vpp, ino64_t *inop,
137     cred_t *cr, int flags, int *deflags, pathname_t *rpnp)
138 {
139 	int i;
140 	contract_t *ct;
141 
142 	i = stoi((char **)&nm);
143 	if (*nm != '\0')
144 		return (ENOENT);
145 
146 	ct = contract_type_ptr(ct_types[gfs_file_index(vp)], i,
147 	    VTOZONE(vp)->zone_uniqid);
148 	if (ct == NULL)
149 		return (ENOENT);
150 
151 	*vpp = ctfs_create_cdirnode(vp, ct);
152 	*inop = gfs_file_inode(*vpp);
153 	contract_rele(ct);
154 	return (0);
155 }
156 
157 const fs_operation_def_t ctfs_tops_tdir[] = {
158 	{ VOPNAME_OPEN,		{ .vop_open = ctfs_open } },
159 	{ VOPNAME_CLOSE,	{ .vop_close = ctfs_close } },
160 	{ VOPNAME_IOCTL,	{ .error = fs_inval } },
161 	{ VOPNAME_GETATTR,	{ .vop_getattr = ctfs_tdir_getattr } },
162 	{ VOPNAME_ACCESS,	{ .vop_access = ctfs_access_dir } },
163 	{ VOPNAME_READDIR,	{ .vop_readdir = gfs_vop_readdir } },
164 	{ VOPNAME_LOOKUP,	{ .vop_lookup = gfs_vop_lookup } },
165 	{ VOPNAME_SEEK,		{ .vop_seek = fs_seek } },
166 	{ VOPNAME_INACTIVE,	{ .vop_inactive = gfs_vop_inactive } },
167 	{ NULL, NULL }
168 };
169