xref: /illumos-gate/usr/src/uts/common/fs/ctfs/ctfs_tdir.c (revision f00e6aa6)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/time.h>
32 #include <sys/cred.h>
33 #include <sys/vfs.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> directory.
49  */
50 static gfs_dirent_t ctfs_tdir_dirents[] = {
51 	{ "bundle", ctfs_create_bundle },
52 	{ "pbundle", ctfs_create_pbundle },
53 	{ "template", ctfs_create_tmplnode },
54 	{ "latest", ctfs_create_latenode, GFS_CACHE_VNODE },
55 	{ NULL }
56 };
57 #define	CTFS_NSPECIALS	((sizeof ctfs_tdir_dirents / sizeof (gfs_dirent_t)) - 1)
58 
59 static int ctfs_tdir_do_readdir(vnode_t *, struct dirent64 *, int *, offset_t *,
60     offset_t *, void *);
61 static int ctfs_tdir_do_lookup(vnode_t *, const char *, vnode_t **, ino64_t *);
62 static ino64_t ctfs_tdir_do_inode(vnode_t *, int);
63 
64 /*
65  * ctfs_create_tdirnode
66  */
67 vnode_t *
68 ctfs_create_tdirnode(vnode_t *pvp)
69 {
70 	return (gfs_dir_create(sizeof (ctfs_tdirnode_t), pvp, ctfs_ops_tdir,
71 	    ctfs_tdir_dirents, ctfs_tdir_do_inode, CTFS_NAME_MAX,
72 	    ctfs_tdir_do_readdir, ctfs_tdir_do_lookup));
73 }
74 
75 /*
76  * ctfs_tdir_getattr - VOP_GETATTR entry point
77  */
78 /* ARGSUSED */
79 static int
80 ctfs_tdir_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr)
81 {
82 	vap->va_type = VDIR;
83 	vap->va_mode = 0555;
84 	vap->va_nlink = 2 + CTFS_NSPECIALS;
85 	vap->va_size = vap->va_nlink +
86 	    contract_type_count(ct_types[gfs_file_index(vp)]);
87 	vap->va_ctime.tv_sec = vp->v_vfsp->vfs_mtime;
88 	vap->va_ctime.tv_nsec = 0;
89 	contract_type_time(ct_types[gfs_file_index(vp)], &vap->va_atime);
90 	vap->va_mtime = vap->va_atime;
91 	ctfs_common_getattr(vp, vap);
92 
93 	return (0);
94 }
95 
96 static ino64_t
97 ctfs_tdir_do_inode(vnode_t *vp, int index)
98 {
99 	return (CTFS_INO_TYPE_FILE(gfs_file_index(vp), index));
100 }
101 
102 /* ARGSUSED */
103 static int
104 ctfs_tdir_do_readdir(vnode_t *vp, struct dirent64 *dp, int *eofp,
105     offset_t *offp, offset_t *nextp, void *data)
106 {
107 	uint64_t zuniqid;
108 	ctid_t next;
109 	ct_type_t *ty = ct_types[gfs_file_index(vp)];
110 
111 	zuniqid = VTOZONE(vp)->zone_uniqid;
112 	next = contract_type_lookup(ty, zuniqid, *offp);
113 
114 	if (next == -1) {
115 		*eofp = 1;
116 		return (0);
117 	}
118 
119 	dp->d_ino = CTFS_INO_CT_DIR(next);
120 	numtos(next, dp->d_name);
121 	*offp = next;
122 	*nextp = next + 1;
123 
124 	return (0);
125 }
126 
127 static int
128 ctfs_tdir_do_lookup(vnode_t *vp, const char *nm, vnode_t **vpp, ino64_t *inop)
129 {
130 	int i;
131 	contract_t *ct;
132 
133 	i = stoi((char **)&nm);
134 	if (*nm != '\0')
135 		return (ENOENT);
136 
137 	ct = contract_type_ptr(ct_types[gfs_file_index(vp)], i,
138 	    VTOZONE(vp)->zone_uniqid);
139 	if (ct == NULL)
140 		return (ENOENT);
141 
142 	*vpp = ctfs_create_cdirnode(vp, ct);
143 	*inop = gfs_file_inode(*vpp);
144 	contract_rele(ct);
145 	return (0);
146 }
147 
148 const fs_operation_def_t ctfs_tops_tdir[] = {
149 	{ VOPNAME_OPEN,		ctfs_open },
150 	{ VOPNAME_CLOSE,	ctfs_close },
151 	{ VOPNAME_IOCTL,	fs_inval },
152 	{ VOPNAME_GETATTR,	ctfs_tdir_getattr },
153 	{ VOPNAME_ACCESS,	ctfs_access_dir },
154 	{ VOPNAME_READDIR,	gfs_vop_readdir },
155 	{ VOPNAME_LOOKUP,	gfs_vop_lookup },
156 	{ VOPNAME_SEEK,		fs_seek },
157 	{ VOPNAME_INACTIVE,	(fs_generic_func_p) gfs_vop_inactive },
158 	{ NULL, NULL }
159 };
160