xref: /titanic_41/usr/src/lib/fm/topo/libtopo/common/zfs.c (revision a799b1e741b6f59012a469e6b57c40cb8061127b)
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 2009 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <ctype.h>
33 #include <alloca.h>
34 #include <limits.h>
35 #include <fm/topo_mod.h>
36 #include <sys/param.h>
37 #include <sys/systeminfo.h>
38 #include <sys/fm/protocol.h>
39 #include <sys/stat.h>
40 
41 #include <topo_method.h>
42 #include <topo_subr.h>
43 #include <libzfs.h>
44 #include <zfs.h>
45 
46 static int zfs_enum(topo_mod_t *, tnode_t *, const char *, topo_instance_t,
47     topo_instance_t, void *, void *);
48 static void zfs_rele(topo_mod_t *, tnode_t *);
49 static int zfs_fmri_nvl2str(topo_mod_t *, tnode_t *, topo_version_t,
50     nvlist_t *, nvlist_t **);
51 
52 const topo_method_t zfs_methods[] = {
53 	{ TOPO_METH_NVL2STR, TOPO_METH_NVL2STR_DESC, TOPO_METH_NVL2STR_VERSION,
54 	    TOPO_STABILITY_INTERNAL, zfs_fmri_nvl2str },
55 	{ NULL }
56 };
57 
58 static const topo_modops_t zfs_ops =
59 	{ zfs_enum, zfs_rele };
60 static const topo_modinfo_t zfs_info =
61 	{ ZFS, FM_FMRI_SCHEME_ZFS, ZFS_VERSION, &zfs_ops };
62 
63 static libzfs_handle_t *g_zfs;
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 	if (!g_zfs)
85 		g_zfs = libzfs_init();
86 
87 	return (0);
88 }
89 
90 void
91 zfs_fini(topo_mod_t *mod)
92 {
93 	if (g_zfs) {
94 		libzfs_fini(g_zfs);
95 		g_zfs = NULL;
96 	}
97 	topo_mod_unregister(mod);
98 }
99 
100 
101 /*ARGSUSED*/
102 int
103 zfs_enum(topo_mod_t *mod, tnode_t *pnode, const char *name, topo_instance_t min,
104     topo_instance_t max, void *notused1, void *notused2)
105 {
106 	(void) topo_method_register(mod, pnode, zfs_methods);
107 	return (0);
108 }
109 
110 /*ARGSUSED*/
111 static void
112 zfs_rele(topo_mod_t *mp, tnode_t *node)
113 {
114 	topo_method_unregister_all(mp, node);
115 }
116 
117 typedef struct cbdata {
118 	uint64_t	cb_guid;
119 	zpool_handle_t	*cb_pool;
120 } cbdata_t;
121 
122 static int
123 find_pool(zpool_handle_t *zhp, void *data)
124 {
125 	cbdata_t *cbp = data;
126 
127 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_GUID, NULL) == cbp->cb_guid) {
128 		cbp->cb_pool = zhp;
129 		return (1);
130 	}
131 
132 	zpool_close(zhp);
133 
134 	return (0);
135 }
136 
137 static ssize_t
138 fmri_nvl2str(nvlist_t *nvl, char *buf, size_t buflen)
139 {
140 	uint64_t pool_guid, vdev_guid;
141 	cbdata_t cb;
142 	ssize_t len;
143 	const char *name;
144 	char guidbuf[64];
145 
146 	(void) nvlist_lookup_uint64(nvl, FM_FMRI_ZFS_POOL, &pool_guid);
147 
148 	/*
149 	 * Attempt to convert the pool guid to a name.
150 	 */
151 	cb.cb_guid = pool_guid;
152 	cb.cb_pool = NULL;
153 
154 	if (g_zfs != NULL && zpool_iter(g_zfs, find_pool, &cb) == 1) {
155 		name = zpool_get_name(cb.cb_pool);
156 	} else {
157 		(void) snprintf(guidbuf, sizeof (guidbuf), "%llx", pool_guid);
158 		name = guidbuf;
159 	}
160 
161 	if (nvlist_lookup_uint64(nvl, FM_FMRI_ZFS_VDEV, &vdev_guid) == 0)
162 		len = snprintf(buf, buflen, "%s://pool=%s/vdev=%llx",
163 		    FM_FMRI_SCHEME_ZFS, name, vdev_guid);
164 	else
165 		len = snprintf(buf, buflen, "%s://pool=%s",
166 		    FM_FMRI_SCHEME_ZFS, name);
167 
168 	if (cb.cb_pool)
169 		zpool_close(cb.cb_pool);
170 
171 	return (len);
172 }
173 
174 /*ARGSUSED*/
175 static int
176 zfs_fmri_nvl2str(topo_mod_t *mod, tnode_t *node, topo_version_t version,
177     nvlist_t *nvl, nvlist_t **out)
178 {
179 	ssize_t len;
180 	char *name = NULL;
181 	nvlist_t *fmristr;
182 
183 	if (version > TOPO_METH_NVL2STR_VERSION)
184 		return (topo_mod_seterrno(mod, EMOD_VER_NEW));
185 
186 	if ((len = fmri_nvl2str(nvl, NULL, 0)) == 0 ||
187 	    (name = topo_mod_alloc(mod, len + 1)) == NULL ||
188 	    fmri_nvl2str(nvl, name, len + 1) == 0) {
189 		if (name != NULL)
190 			topo_mod_free(mod, name, len + 1);
191 		return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
192 	}
193 
194 	if (topo_mod_nvalloc(mod, &fmristr, NV_UNIQUE_NAME) != 0) {
195 		topo_mod_free(mod, name, len + 1);
196 		return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
197 	}
198 	if (nvlist_add_string(fmristr, "fmri-string", name) != 0) {
199 		topo_mod_free(mod, name, len + 1);
200 		nvlist_free(fmristr);
201 		return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
202 	}
203 	topo_mod_free(mod, name, len + 1);
204 	*out = fmristr;
205 
206 	return (0);
207 }
208