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/cmn_err.h>
44
45 /*
46 * CTFS routines for the /system/contract/all/<ctid> vnode.
47 */
48
49 /*
50 * ctfs_create_symnode
51 *
52 * Creates and returns a symnode for the specified contract.
53 */
54 vnode_t *
ctfs_create_symnode(vnode_t * pvp,contract_t * ct)55 ctfs_create_symnode(vnode_t *pvp, contract_t *ct)
56 {
57 ctfs_symnode_t *symnode;
58 vnode_t *vp;
59 size_t len;
60
61 vp = gfs_file_create(sizeof (ctfs_symnode_t), pvp, ctfs_ops_sym);
62 vp->v_type = VLNK;
63 symnode = vp->v_data;
64
65 symnode->ctfs_sn_contract = ct;
66 symnode->ctfs_sn_size = len = snprintf(NULL, 0, "../%s/%ld",
67 ct->ct_type->ct_type_name, (long)ct->ct_id) + 1;
68 symnode->ctfs_sn_string = kmem_alloc(len, KM_SLEEP);
69 VERIFY(snprintf(symnode->ctfs_sn_string, len, "../%s/%ld",
70 ct->ct_type->ct_type_name, (long)ct->ct_id) < len);
71
72 contract_hold(ct);
73
74 return (vp);
75 }
76
77 /*
78 * ctfs_sym_getattr - VOP_GETATTR entry point
79 */
80 /* ARGSUSED */
81 static int
ctfs_sym_getattr(vnode_t * vp,vattr_t * vap,int flags,cred_t * cr,caller_context_t * ct)82 ctfs_sym_getattr(
83 vnode_t *vp,
84 vattr_t *vap,
85 int flags,
86 cred_t *cr,
87 caller_context_t *ct)
88 {
89 ctfs_symnode_t *symnode = vp->v_data;
90
91 vap->va_type = VLNK;
92 vap->va_mode = 0444;
93 vap->va_nlink = 1;
94 vap->va_size = symnode->ctfs_sn_size - 1;
95 vap->va_mtime = vap->va_atime = vap->va_ctime =
96 symnode->ctfs_sn_contract->ct_ctime;
97 ctfs_common_getattr(vp, vap);
98
99 return (0);
100 }
101
102 /*
103 * ctfs_sym_readlink - VOP_READLINK entry point
104 *
105 * Since we built the symlink string in ctfs_create_symnode, this is
106 * just a uiomove.
107 */
108 /* ARGSUSED */
109 int
ctfs_sym_readlink(vnode_t * vp,uio_t * uiop,cred_t * cr,caller_context_t * ct)110 ctfs_sym_readlink(vnode_t *vp, uio_t *uiop, cred_t *cr, caller_context_t *ct)
111 {
112 ctfs_symnode_t *symnode = vp->v_data;
113
114 return (uiomove(symnode->ctfs_sn_string, symnode->ctfs_sn_size - 1,
115 UIO_READ, uiop));
116 }
117
118 /*
119 * ctfs_sym_inactive - VOP_INACTIVE entry point
120 */
121 /* ARGSUSED */
122 static void
ctfs_sym_inactive(vnode_t * vp,cred_t * cr,caller_context_t * ct)123 ctfs_sym_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
124 {
125 ctfs_symnode_t *symnode;
126
127 if ((symnode = gfs_file_inactive(vp)) != NULL) {
128 contract_rele(symnode->ctfs_sn_contract);
129 kmem_free(symnode->ctfs_sn_string, symnode->ctfs_sn_size);
130 kmem_free(symnode, sizeof (ctfs_symnode_t));
131 }
132 }
133
134 const fs_operation_def_t ctfs_tops_sym[] = {
135 { VOPNAME_OPEN, { .vop_open = ctfs_open } },
136 { VOPNAME_CLOSE, { .vop_close = ctfs_close } },
137 { VOPNAME_IOCTL, { .error = fs_inval } },
138 { VOPNAME_GETATTR, { .vop_getattr = ctfs_sym_getattr } },
139 { VOPNAME_READLINK, { .vop_readlink = ctfs_sym_readlink } },
140 { VOPNAME_ACCESS, { .vop_access = ctfs_access_readonly } },
141 { VOPNAME_READDIR, { .error = fs_notdir } },
142 { VOPNAME_LOOKUP, { .error = fs_notdir } },
143 { VOPNAME_INACTIVE, { .vop_inactive = ctfs_sym_inactive } },
144 { NULL, NULL }
145 };
146