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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*
27  * Copyright 2024 OmniOS Community Edition (OmniOSce) Association.
28  */
29 
30 #include <dlfcn.h>
31 #include <errno.h>
32 #include <libintl.h>
33 #include <link.h>
34 #include <pthread.h>
35 #include <strings.h>
36 #include <unistd.h>
37 #include <zone.h>
38 
39 #include <libzfs.h>
40 
41 #include <fm/libtopo.h>
42 #include <sys/fm/protocol.h>
43 #include <sys/systeminfo.h>
44 
45 #include "libzfs_impl.h"
46 
47 /*
48  * This file is responsible for determining the relationship between I/O
49  * devices paths and physical locations.  In the world of MPxIO and external
50  * enclosures, the device path is not synonymous with the physical location.
51  * If you remove a drive and insert it into a different slot, it will end up
52  * with the same path under MPxIO.  If you recable storage enclosures, the
53  * device paths may change.  All of this makes it difficult to implement the
54  * 'autoreplace' property, which is supposed to automatically manage disk
55  * replacement based on physical slot.
56  *
57  * In order to work around these limitations, we have a per-vdev FRU property
58  * that is the libtopo path (minus disk-specific authority information) to the
59  * physical location of the device on the system.  This is an optional
60  * property, and is only needed when using the 'autoreplace' property or when
61  * generating FMA faults against vdevs.
62  */
63 
64 /*
65  * Because the FMA packages depend on ZFS, we have to dlopen() libtopo in case
66  * it is not present.  We only need this once per library instance, so it is
67  * not part of the libzfs handle.
68  */
69 static void *_topo_dlhandle;
70 static topo_hdl_t *(*_topo_open)(int, const char *, int *);
71 static void (*_topo_close)(topo_hdl_t *);
72 static char *(*_topo_snap_hold)(topo_hdl_t *, const char *, int *);
73 static void (*_topo_snap_release)(topo_hdl_t *);
74 static topo_walk_t *(*_topo_walk_init)(topo_hdl_t *, const char *,
75     topo_walk_cb_t, void *, int *);
76 static int (*_topo_walk_step)(topo_walk_t *, int);
77 static void (*_topo_walk_fini)(topo_walk_t *);
78 static void (*_topo_hdl_strfree)(topo_hdl_t *, char *);
79 static char *(*_topo_node_name)(tnode_t *);
80 static int (*_topo_prop_get_string)(tnode_t *, const char *, const char *,
81     char **, int *);
82 static int (*_topo_node_fru)(tnode_t *, nvlist_t **, nvlist_t *, int *);
83 static int (*_topo_fmri_nvl2str)(topo_hdl_t *, nvlist_t *, char **, int *);
84 static int (*_topo_fmri_strcmp_noauth)(topo_hdl_t *, const char *,
85     const char *);
86 
87 #define	ZFS_FRU_HASH_SIZE	257
88 
89 static size_t
fru_strhash(const char * key)90 fru_strhash(const char *key)
91 {
92 	ulong_t g, h = 0;
93 	const char *p;
94 
95 	for (p = key; *p != '\0'; p++) {
96 		h = (h << 4) + *p;
97 
98 		if ((g = (h & 0xf0000000)) != 0) {
99 			h ^= (g >> 24);
100 			h ^= g;
101 		}
102 	}
103 
104 	return (h % ZFS_FRU_HASH_SIZE);
105 }
106 
107 static int
libzfs_fru_gather(topo_hdl_t * thp,tnode_t * tn,void * arg)108 libzfs_fru_gather(topo_hdl_t *thp, tnode_t *tn, void *arg)
109 {
110 	libzfs_handle_t *hdl = arg;
111 	nvlist_t *fru;
112 	char *devpath, *frustr;
113 	int err;
114 	libzfs_fru_t *frup;
115 	size_t idx;
116 
117 	/*
118 	 * If this is the chassis node, and we don't yet have the system
119 	 * chassis ID, then fill in this value now.
120 	 */
121 	if (hdl->libzfs_chassis_id[0] == '\0' &&
122 	    strcmp(_topo_node_name(tn), "chassis") == 0) {
123 		if (_topo_prop_get_string(tn, FM_FMRI_AUTHORITY,
124 		    FM_FMRI_AUTH_CHASSIS, &devpath, &err) == 0)
125 			(void) strlcpy(hdl->libzfs_chassis_id, devpath,
126 			    sizeof (hdl->libzfs_chassis_id));
127 	}
128 
129 	/*
130 	 * Skip non-disk nodes.
131 	 */
132 	if (strcmp(_topo_node_name(tn), "disk") != 0)
133 		return (TOPO_WALK_NEXT);
134 
135 	/*
136 	 * Get the devfs path and FRU.
137 	 */
138 	if (_topo_prop_get_string(tn, "io", "devfs-path", &devpath, &err) != 0)
139 		return (TOPO_WALK_NEXT);
140 
141 	if (libzfs_fru_lookup(hdl, devpath) != NULL) {
142 		_topo_hdl_strfree(thp, devpath);
143 		return (TOPO_WALK_NEXT);
144 	}
145 
146 	if (_topo_node_fru(tn, &fru, NULL, &err) != 0) {
147 		_topo_hdl_strfree(thp, devpath);
148 		return (TOPO_WALK_NEXT);
149 	}
150 
151 	/*
152 	 * Convert the FRU into a string.
153 	 */
154 	if (_topo_fmri_nvl2str(thp, fru, &frustr, &err) != 0) {
155 		nvlist_free(fru);
156 		_topo_hdl_strfree(thp, devpath);
157 		return (TOPO_WALK_NEXT);
158 	}
159 
160 	nvlist_free(fru);
161 
162 	/*
163 	 * Finally, we have a FRU string and device path.  Add it to the hash.
164 	 */
165 	if ((frup = calloc(sizeof (libzfs_fru_t), 1)) == NULL) {
166 		_topo_hdl_strfree(thp, devpath);
167 		_topo_hdl_strfree(thp, frustr);
168 		return (TOPO_WALK_NEXT);
169 	}
170 
171 	if ((frup->zf_device = strdup(devpath)) == NULL ||
172 	    (frup->zf_fru = strdup(frustr)) == NULL) {
173 		free(frup->zf_device);
174 		free(frup);
175 		_topo_hdl_strfree(thp, devpath);
176 		_topo_hdl_strfree(thp, frustr);
177 		return (TOPO_WALK_NEXT);
178 	}
179 
180 	_topo_hdl_strfree(thp, devpath);
181 	_topo_hdl_strfree(thp, frustr);
182 
183 	idx = fru_strhash(frup->zf_device);
184 	frup->zf_chain = hdl->libzfs_fru_hash[idx];
185 	hdl->libzfs_fru_hash[idx] = frup;
186 	frup->zf_next = hdl->libzfs_fru_list;
187 	hdl->libzfs_fru_list = frup;
188 
189 	return (TOPO_WALK_NEXT);
190 }
191 
192 /*
193  * Called during initialization to setup the dynamic libtopo connection.
194  */
195 #pragma init(libzfs_init_fru)
196 static void
libzfs_init_fru(void)197 libzfs_init_fru(void)
198 {
199 	char path[MAXPATHLEN];
200 	char isa[257];
201 
202 	if (getzoneid() != GLOBAL_ZONEID)
203 		return;
204 
205 #if defined(_LP64)
206 	if (sysinfo(SI_ARCHITECTURE_64, isa, sizeof (isa)) < 0)
207 		isa[0] = '\0';
208 #else
209 	isa[0] = '\0';
210 #endif
211 	(void) snprintf(path, sizeof (path),
212 	    "/usr/lib/fm/%s/libtopo.so", isa);
213 
214 	if ((_topo_dlhandle = dlopen(path, RTLD_LAZY)) == NULL)
215 		return;
216 
217 	_topo_open = (topo_hdl_t *(*)())
218 	    dlsym(_topo_dlhandle, "topo_open");
219 	_topo_close = (void (*)())
220 	    dlsym(_topo_dlhandle, "topo_close");
221 	_topo_snap_hold = (char *(*)())
222 	    dlsym(_topo_dlhandle, "topo_snap_hold");
223 	_topo_snap_release = (void (*)())
224 	    dlsym(_topo_dlhandle, "topo_snap_release");
225 	_topo_walk_init = (topo_walk_t *(*)())
226 	    dlsym(_topo_dlhandle, "topo_walk_init");
227 	_topo_walk_step = (int (*)())
228 	    dlsym(_topo_dlhandle, "topo_walk_step");
229 	_topo_walk_fini = (void (*)())
230 	    dlsym(_topo_dlhandle, "topo_walk_fini");
231 	_topo_hdl_strfree = (void (*)())
232 	    dlsym(_topo_dlhandle, "topo_hdl_strfree");
233 	_topo_node_name = (char *(*)())
234 	    dlsym(_topo_dlhandle, "topo_node_name");
235 	_topo_prop_get_string = (int (*)())
236 	    dlsym(_topo_dlhandle, "topo_prop_get_string");
237 	_topo_node_fru = (int (*)())
238 	    dlsym(_topo_dlhandle, "topo_node_fru");
239 	_topo_fmri_nvl2str = (int (*)())
240 	    dlsym(_topo_dlhandle, "topo_fmri_nvl2str");
241 	_topo_fmri_strcmp_noauth = (int (*)())
242 	    dlsym(_topo_dlhandle, "topo_fmri_strcmp_noauth");
243 
244 	if (_topo_open == NULL || _topo_close == NULL ||
245 	    _topo_snap_hold == NULL || _topo_snap_release == NULL ||
246 	    _topo_walk_init == NULL || _topo_walk_step == NULL ||
247 	    _topo_walk_fini == NULL || _topo_hdl_strfree == NULL ||
248 	    _topo_node_name == NULL || _topo_prop_get_string == NULL ||
249 	    _topo_node_fru == NULL || _topo_fmri_nvl2str == NULL ||
250 	    _topo_fmri_strcmp_noauth == NULL) {
251 		(void) dlclose(_topo_dlhandle);
252 		_topo_dlhandle = NULL;
253 	}
254 }
255 
256 /*
257  * Refresh the mappings from device path -> FMRI.  We do this by walking the
258  * hc topology looking for disk nodes, and recording the io/devfs-path and FRU.
259  * Note that we strip out the disk-specific authority information (serial,
260  * part, revision, etc) so that we are left with only the identifying
261  * characteristics of the slot (hc path and chassis-id).
262  */
263 void
libzfs_fru_refresh(libzfs_handle_t * hdl)264 libzfs_fru_refresh(libzfs_handle_t *hdl)
265 {
266 	int err;
267 	char *uuid;
268 	topo_hdl_t *thp;
269 	topo_walk_t *twp;
270 
271 	if (_topo_dlhandle == NULL)
272 		return;
273 
274 	/*
275 	 * Clear the FRU hash and initialize our basic structures.
276 	 */
277 	libzfs_fru_clear(hdl, B_FALSE);
278 
279 	if ((hdl->libzfs_topo_hdl = _topo_open(TOPO_VERSION,
280 	    NULL, &err)) == NULL)
281 		return;
282 
283 	thp = hdl->libzfs_topo_hdl;
284 
285 	if ((uuid = _topo_snap_hold(thp, NULL, &err)) == NULL)
286 		return;
287 
288 	_topo_hdl_strfree(thp, uuid);
289 
290 	if (hdl->libzfs_fru_hash == NULL &&
291 	    (hdl->libzfs_fru_hash =
292 	    calloc(ZFS_FRU_HASH_SIZE, sizeof (void *))) == NULL)
293 		return;
294 
295 	/*
296 	 * We now have a topo snapshot, so iterate over the hc topology looking
297 	 * for disks to add to the hash.
298 	 */
299 	twp = _topo_walk_init(thp, FM_FMRI_SCHEME_HC,
300 	    libzfs_fru_gather, hdl, &err);
301 	if (twp != NULL) {
302 		(void) _topo_walk_step(twp, TOPO_WALK_CHILD);
303 		_topo_walk_fini(twp);
304 	}
305 }
306 
307 /*
308  * Given a devfs path, return the FRU for the device, if known.  This will
309  * automatically call libzfs_fru_refresh() if it hasn't already been called by
310  * the consumer.  The string returned is valid until the next call to
311  * libzfs_fru_refresh().
312  */
313 const char *
libzfs_fru_lookup(libzfs_handle_t * hdl,const char * devpath)314 libzfs_fru_lookup(libzfs_handle_t *hdl, const char *devpath)
315 {
316 	size_t idx = fru_strhash(devpath);
317 	libzfs_fru_t *frup;
318 
319 	if (hdl->libzfs_fru_hash == NULL)
320 		libzfs_fru_refresh(hdl);
321 
322 	if (hdl->libzfs_fru_hash == NULL)
323 		return (NULL);
324 
325 	for (frup = hdl->libzfs_fru_hash[idx]; frup != NULL;
326 	    frup = frup->zf_chain) {
327 		if (strcmp(devpath, frup->zf_device) == 0)
328 			return (frup->zf_fru);
329 	}
330 
331 	return (NULL);
332 }
333 
334 /*
335  * Given a fru path, return the device path.  This will automatically call
336  * libzfs_fru_refresh() if it hasn't already been called by the consumer.  The
337  * string returned is valid until the next call to libzfs_fru_refresh().
338  */
339 const char *
libzfs_fru_devpath(libzfs_handle_t * hdl,const char * fru)340 libzfs_fru_devpath(libzfs_handle_t *hdl, const char *fru)
341 {
342 	libzfs_fru_t *frup;
343 	size_t idx;
344 
345 	if (hdl->libzfs_fru_hash == NULL)
346 		libzfs_fru_refresh(hdl);
347 
348 	if (hdl->libzfs_fru_hash == NULL)
349 		return (NULL);
350 
351 	for (idx = 0; idx < ZFS_FRU_HASH_SIZE; idx++) {
352 		for (frup = hdl->libzfs_fru_hash[idx]; frup != NULL;
353 		    frup = frup->zf_next) {
354 			if (_topo_fmri_strcmp_noauth(hdl->libzfs_topo_hdl,
355 			    fru, frup->zf_fru))
356 				return (frup->zf_device);
357 		}
358 	}
359 
360 	return (NULL);
361 }
362 
363 /*
364  * Change the stored FRU for the given vdev.
365  */
366 int
zpool_fru_set(zpool_handle_t * zhp,uint64_t vdev_guid,const char * fru)367 zpool_fru_set(zpool_handle_t *zhp, uint64_t vdev_guid, const char *fru)
368 {
369 	zfs_cmd_t zc = { 0 };
370 
371 	(void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
372 	(void) strncpy(zc.zc_value, fru, sizeof (zc.zc_value));
373 	zc.zc_guid = vdev_guid;
374 
375 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SETFRU, &zc) != 0)
376 		return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
377 		    dgettext(TEXT_DOMAIN, "cannot set FRU")));
378 
379 	return (0);
380 }
381 
382 /*
383  * Compare to two FRUs, ignoring any authority information.
384  */
385 boolean_t
libzfs_fru_compare(libzfs_handle_t * hdl,const char * a,const char * b)386 libzfs_fru_compare(libzfs_handle_t *hdl, const char *a, const char *b)
387 {
388 	if (hdl->libzfs_fru_hash == NULL)
389 		libzfs_fru_refresh(hdl);
390 
391 	if (hdl->libzfs_fru_hash == NULL)
392 		return (strcmp(a, b) == 0);
393 
394 	return (_topo_fmri_strcmp_noauth(hdl->libzfs_topo_hdl, a, b));
395 }
396 
397 /*
398  * This special function checks to see whether the FRU indicates it's supposed
399  * to be in the system chassis, but the chassis-id doesn't match.  This can
400  * happen in a clustered case, where both head nodes have the same logical
401  * disk, but opening the device on the other head node is meaningless.
402  */
403 boolean_t
libzfs_fru_notself(libzfs_handle_t * hdl,const char * fru)404 libzfs_fru_notself(libzfs_handle_t *hdl, const char *fru)
405 {
406 	const char *chassisid;
407 	size_t len;
408 
409 	if (hdl->libzfs_fru_hash == NULL)
410 		libzfs_fru_refresh(hdl);
411 
412 	if (hdl->libzfs_chassis_id[0] == '\0')
413 		return (B_FALSE);
414 
415 	if (strstr(fru, "/chassis=0/") == NULL)
416 		return (B_FALSE);
417 
418 	if ((chassisid = strstr(fru, ":chassis-id=")) == NULL)
419 		return (B_FALSE);
420 
421 	chassisid += 12;
422 	len = strlen(hdl->libzfs_chassis_id);
423 	if (strncmp(chassisid, hdl->libzfs_chassis_id, len) == 0 &&
424 	    (chassisid[len] == '/' || chassisid[len] == ':'))
425 		return (B_FALSE);
426 
427 	return (B_TRUE);
428 }
429 
430 /*
431  * Clear memory associated with the FRU hash.
432  */
433 void
libzfs_fru_clear(libzfs_handle_t * hdl,boolean_t final)434 libzfs_fru_clear(libzfs_handle_t *hdl, boolean_t final)
435 {
436 	libzfs_fru_t *frup;
437 
438 	while ((frup = hdl->libzfs_fru_list) != NULL) {
439 		hdl->libzfs_fru_list = frup->zf_next;
440 		free(frup->zf_device);
441 		free(frup->zf_fru);
442 		free(frup);
443 	}
444 
445 	hdl->libzfs_fru_list = NULL;
446 
447 	if (hdl->libzfs_topo_hdl != NULL) {
448 		_topo_snap_release(hdl->libzfs_topo_hdl);
449 		_topo_close(hdl->libzfs_topo_hdl);
450 		hdl->libzfs_topo_hdl = NULL;
451 	}
452 
453 	if (final) {
454 		free(hdl->libzfs_fru_hash);
455 	} else if (hdl->libzfs_fru_hash != NULL) {
456 		bzero(hdl->libzfs_fru_hash,
457 		    ZFS_FRU_HASH_SIZE * sizeof (void *));
458 	}
459 }
460