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