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