xref: /titanic_51/usr/src/lib/fm/topo/libtopo/common/zfs.c (revision 0080280bd4437b3f383eea051f4b1cefb0714b95)
1 /*
2  *
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance 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 /*
24  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <ctype.h>
35 #include <alloca.h>
36 #include <limits.h>
37 #include <fm/topo_mod.h>
38 #include <sys/param.h>
39 #include <sys/systeminfo.h>
40 #include <sys/fm/protocol.h>
41 #include <sys/stat.h>
42 
43 #include <topo_method.h>
44 #include <topo_subr.h>
45 #include <libzfs.h>
46 #include <zfs.h>
47 
48 static int zfs_enum(topo_mod_t *, tnode_t *, const char *, topo_instance_t,
49     topo_instance_t, void *, void *);
50 static void zfs_release(topo_mod_t *, tnode_t *);
51 static int zfs_fmri_nvl2str(topo_mod_t *, tnode_t *, topo_version_t,
52     nvlist_t *, nvlist_t **);
53 
54 const topo_method_t zfs_methods[] = {
55 	{ TOPO_METH_NVL2STR, TOPO_METH_NVL2STR_DESC, TOPO_METH_NVL2STR_VERSION,
56 	    TOPO_STABILITY_INTERNAL, zfs_fmri_nvl2str },
57 	{ NULL }
58 };
59 
60 static const topo_modops_t zfs_ops =
61 	{ zfs_enum, zfs_release };
62 static const topo_modinfo_t zfs_info =
63 	{ ZFS, FM_FMRI_SCHEME_ZFS, ZFS_VERSION, &zfs_ops };
64 
65 int
66 zfs_init(topo_mod_t *mod, topo_version_t version)
67 {
68 	/*
69 	 * Turn on module debugging output
70 	 */
71 	if (getenv("TOPOZFSDEBUG"))
72 		topo_mod_setdebug(mod);
73 
74 	topo_mod_dprintf(mod, "initializing zfs builtin\n");
75 
76 	if (version != ZFS_VERSION)
77 		return (topo_mod_seterrno(mod, EMOD_VER_NEW));
78 
79 	if (topo_mod_register(mod, &zfs_info, TOPO_VERSION) != 0) {
80 		topo_mod_dprintf(mod, "failed to register zfs: "
81 		    "%s\n", topo_mod_errmsg(mod));
82 		return (-1); /* mod errno already set */
83 	}
84 
85 	return (0);
86 }
87 
88 void
89 zfs_fini(topo_mod_t *mod)
90 {
91 	topo_mod_unregister(mod);
92 }
93 
94 
95 /*ARGSUSED*/
96 int
97 zfs_enum(topo_mod_t *mod, tnode_t *pnode, const char *name, topo_instance_t min,
98     topo_instance_t max, void *notused1, void *notused2)
99 {
100 	(void) topo_method_register(mod, pnode, zfs_methods);
101 	return (0);
102 }
103 
104 /*ARGSUSED*/
105 static void
106 zfs_release(topo_mod_t *mp, tnode_t *node)
107 {
108 	topo_method_unregister_all(mp, node);
109 }
110 
111 typedef struct cbdata {
112 	uint64_t	cb_guid;
113 	zpool_handle_t	*cb_pool;
114 } cbdata_t;
115 
116 libzfs_handle_t *g_zfs;
117 
118 static int
119 find_pool(zpool_handle_t *zhp, void *data)
120 {
121 	cbdata_t *cbp = data;
122 
123 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_GUID, NULL) == cbp->cb_guid) {
124 		cbp->cb_pool = zhp;
125 		return (1);
126 	}
127 
128 	zpool_close(zhp);
129 
130 	return (0);
131 }
132 
133 static ssize_t
134 fmri_nvl2str(nvlist_t *nvl, char *buf, size_t buflen)
135 {
136 	uint64_t pool_guid, vdev_guid;
137 	cbdata_t cb;
138 	ssize_t len;
139 	const char *name;
140 	char guidbuf[64];
141 
142 	(void) nvlist_lookup_uint64(nvl, FM_FMRI_ZFS_POOL, &pool_guid);
143 
144 	/*
145 	 * Attempt to convert the pool guid to a name.
146 	 */
147 	cb.cb_guid = pool_guid;
148 	cb.cb_pool = NULL;
149 
150 	if (zpool_iter(g_zfs, find_pool, &cb) == 1) {
151 		name = zpool_get_name(cb.cb_pool);
152 	} else {
153 		(void) snprintf(guidbuf, sizeof (guidbuf), "%llx", pool_guid);
154 		name = guidbuf;
155 	}
156 
157 	if (nvlist_lookup_uint64(nvl, FM_FMRI_ZFS_VDEV, &vdev_guid) == 0)
158 		len = snprintf(buf, buflen, "%s://pool=%s/vdev=%llx",
159 		    FM_FMRI_SCHEME_ZFS, name, vdev_guid);
160 	else
161 		len = snprintf(buf, buflen, "%s://pool=%s",
162 		    FM_FMRI_SCHEME_ZFS, name);
163 
164 	if (cb.cb_pool)
165 		zpool_close(cb.cb_pool);
166 
167 	return (len);
168 }
169 
170 /*ARGSUSED*/
171 static int
172 zfs_fmri_nvl2str(topo_mod_t *mod, tnode_t *node, topo_version_t version,
173     nvlist_t *nvl, nvlist_t **out)
174 {
175 	ssize_t len;
176 	char *name = NULL;
177 	nvlist_t *fmristr;
178 
179 	if (version > TOPO_METH_NVL2STR_VERSION)
180 		return (topo_mod_seterrno(mod, EMOD_VER_NEW));
181 
182 	if ((len = fmri_nvl2str(nvl, NULL, 0)) == 0 ||
183 	    (name = topo_mod_alloc(mod, len + 1)) == NULL ||
184 	    fmri_nvl2str(nvl, name, len + 1) == 0) {
185 		if (name != NULL)
186 			topo_mod_free(mod, name, len + 1);
187 		return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
188 	}
189 
190 	if (topo_mod_nvalloc(mod, &fmristr, NV_UNIQUE_NAME) != 0) {
191 		topo_mod_free(mod, name, len + 1);
192 		return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
193 	}
194 	if (nvlist_add_string(fmristr, "fmri-string", name) != 0) {
195 		topo_mod_free(mod, name, len + 1);
196 		nvlist_free(fmristr);
197 		return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
198 	}
199 	topo_mod_free(mod, name, len + 1);
200 	*out = fmristr;
201 
202 	return (0);
203 }
204