xref: /titanic_51/usr/src/lib/fm/topo/libtopo/common/zfs.c (revision 5d460eafffba936e81c4dd5ebe0f59b238f09121)
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_release(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_release };
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 	g_zfs = libzfs_init();
85 
86 	return (0);
87 }
88 
89 void
90 zfs_fini(topo_mod_t *mod)
91 {
92 	if (g_zfs) {
93 		libzfs_fini(g_zfs);
94 		g_zfs = NULL;
95 	}
96 	topo_mod_unregister(mod);
97 }
98 
99 
100 /*ARGSUSED*/
101 int
102 zfs_enum(topo_mod_t *mod, tnode_t *pnode, const char *name, topo_instance_t min,
103     topo_instance_t max, void *notused1, void *notused2)
104 {
105 	(void) topo_method_register(mod, pnode, zfs_methods);
106 	return (0);
107 }
108 
109 /*ARGSUSED*/
110 static void
111 zfs_release(topo_mod_t *mp, tnode_t *node)
112 {
113 	topo_method_unregister_all(mp, node);
114 }
115 
116 typedef struct cbdata {
117 	uint64_t	cb_guid;
118 	zpool_handle_t	*cb_pool;
119 } cbdata_t;
120 
121 static int
122 find_pool(zpool_handle_t *zhp, void *data)
123 {
124 	cbdata_t *cbp = data;
125 
126 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_GUID, NULL) == cbp->cb_guid) {
127 		cbp->cb_pool = zhp;
128 		return (1);
129 	}
130 
131 	zpool_close(zhp);
132 
133 	return (0);
134 }
135 
136 static ssize_t
137 fmri_nvl2str(nvlist_t *nvl, char *buf, size_t buflen)
138 {
139 	uint64_t pool_guid, vdev_guid;
140 	cbdata_t cb;
141 	ssize_t len;
142 	const char *name;
143 	char guidbuf[64];
144 
145 	(void) nvlist_lookup_uint64(nvl, FM_FMRI_ZFS_POOL, &pool_guid);
146 
147 	/*
148 	 * Attempt to convert the pool guid to a name.
149 	 */
150 	cb.cb_guid = pool_guid;
151 	cb.cb_pool = NULL;
152 
153 	if (g_zfs != NULL && zpool_iter(g_zfs, find_pool, &cb) == 1) {
154 		name = zpool_get_name(cb.cb_pool);
155 	} else {
156 		(void) snprintf(guidbuf, sizeof (guidbuf), "%llx", pool_guid);
157 		name = guidbuf;
158 	}
159 
160 	if (nvlist_lookup_uint64(nvl, FM_FMRI_ZFS_VDEV, &vdev_guid) == 0)
161 		len = snprintf(buf, buflen, "%s://pool=%s/vdev=%llx",
162 		    FM_FMRI_SCHEME_ZFS, name, vdev_guid);
163 	else
164 		len = snprintf(buf, buflen, "%s://pool=%s",
165 		    FM_FMRI_SCHEME_ZFS, name);
166 
167 	if (cb.cb_pool)
168 		zpool_close(cb.cb_pool);
169 
170 	return (len);
171 }
172 
173 /*ARGSUSED*/
174 static int
175 zfs_fmri_nvl2str(topo_mod_t *mod, tnode_t *node, topo_version_t version,
176     nvlist_t *nvl, nvlist_t **out)
177 {
178 	ssize_t len;
179 	char *name = NULL;
180 	nvlist_t *fmristr;
181 
182 	if (version > TOPO_METH_NVL2STR_VERSION)
183 		return (topo_mod_seterrno(mod, EMOD_VER_NEW));
184 
185 	if ((len = fmri_nvl2str(nvl, NULL, 0)) == 0 ||
186 	    (name = topo_mod_alloc(mod, len + 1)) == NULL ||
187 	    fmri_nvl2str(nvl, name, len + 1) == 0) {
188 		if (name != NULL)
189 			topo_mod_free(mod, name, len + 1);
190 		return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
191 	}
192 
193 	if (topo_mod_nvalloc(mod, &fmristr, NV_UNIQUE_NAME) != 0) {
194 		topo_mod_free(mod, name, len + 1);
195 		return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
196 	}
197 	if (nvlist_add_string(fmristr, "fmri-string", name) != 0) {
198 		topo_mod_free(mod, name, len + 1);
199 		nvlist_free(fmristr);
200 		return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
201 	}
202 	topo_mod_free(mod, name, len + 1);
203 	*out = fmristr;
204 
205 	return (0);
206 }
207