xref: /illumos-gate/usr/src/lib/libzfs/common/libzfs_fru.c (revision 6d5f0164c96478925502004890b8c98a4eaf1c55)
1069f55e2SEric Schrock /*
2069f55e2SEric Schrock  * CDDL HEADER START
3069f55e2SEric Schrock  *
4069f55e2SEric Schrock  * The contents of this file are subject to the terms of the
5069f55e2SEric Schrock  * Common Development and Distribution License (the "License").
6069f55e2SEric Schrock  * You may not use this file except in compliance with the License.
7069f55e2SEric Schrock  *
8069f55e2SEric Schrock  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9069f55e2SEric Schrock  * or http://www.opensolaris.org/os/licensing.
10069f55e2SEric Schrock  * See the License for the specific language governing permissions
11069f55e2SEric Schrock  * and limitations under the License.
12069f55e2SEric Schrock  *
13069f55e2SEric Schrock  * When distributing Covered Code, include this CDDL HEADER in each
14069f55e2SEric Schrock  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15069f55e2SEric Schrock  * If applicable, add the following below this CDDL HEADER, with the
16069f55e2SEric Schrock  * fields enclosed by brackets "[]" replaced with your own identifying
17069f55e2SEric Schrock  * information: Portions Copyright [yyyy] [name of copyright owner]
18069f55e2SEric Schrock  *
19069f55e2SEric Schrock  * CDDL HEADER END
20069f55e2SEric Schrock  */
21069f55e2SEric Schrock 
22069f55e2SEric Schrock /*
23069f55e2SEric Schrock  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24069f55e2SEric Schrock  * Use is subject to license terms.
25069f55e2SEric Schrock  */
26*6d5f0164SAndy Fiddaman /*
27*6d5f0164SAndy Fiddaman  * Copyright 2024 OmniOS Community Edition (OmniOSce) Association.
28*6d5f0164SAndy Fiddaman  */
29069f55e2SEric Schrock 
30069f55e2SEric Schrock #include <dlfcn.h>
31069f55e2SEric Schrock #include <errno.h>
32069f55e2SEric Schrock #include <libintl.h>
33069f55e2SEric Schrock #include <link.h>
34069f55e2SEric Schrock #include <pthread.h>
35069f55e2SEric Schrock #include <strings.h>
36069f55e2SEric Schrock #include <unistd.h>
37*6d5f0164SAndy Fiddaman #include <zone.h>
38069f55e2SEric Schrock 
39069f55e2SEric Schrock #include <libzfs.h>
40069f55e2SEric Schrock 
41069f55e2SEric Schrock #include <fm/libtopo.h>
42069f55e2SEric Schrock #include <sys/fm/protocol.h>
43069f55e2SEric Schrock #include <sys/systeminfo.h>
44069f55e2SEric Schrock 
45069f55e2SEric Schrock #include "libzfs_impl.h"
46069f55e2SEric Schrock 
47069f55e2SEric Schrock /*
48069f55e2SEric Schrock  * This file is responsible for determining the relationship between I/O
49069f55e2SEric Schrock  * devices paths and physical locations.  In the world of MPxIO and external
50069f55e2SEric Schrock  * enclosures, the device path is not synonymous with the physical location.
51069f55e2SEric Schrock  * If you remove a drive and insert it into a different slot, it will end up
52069f55e2SEric Schrock  * with the same path under MPxIO.  If you recable storage enclosures, the
53069f55e2SEric Schrock  * device paths may change.  All of this makes it difficult to implement the
54069f55e2SEric Schrock  * 'autoreplace' property, which is supposed to automatically manage disk
55069f55e2SEric Schrock  * replacement based on physical slot.
56069f55e2SEric Schrock  *
57069f55e2SEric Schrock  * In order to work around these limitations, we have a per-vdev FRU property
58069f55e2SEric Schrock  * that is the libtopo path (minus disk-specific authority information) to the
59069f55e2SEric Schrock  * physical location of the device on the system.  This is an optional
60069f55e2SEric Schrock  * property, and is only needed when using the 'autoreplace' property or when
61069f55e2SEric Schrock  * generating FMA faults against vdevs.
62069f55e2SEric Schrock  */
63069f55e2SEric Schrock 
64069f55e2SEric Schrock /*
65069f55e2SEric Schrock  * Because the FMA packages depend on ZFS, we have to dlopen() libtopo in case
66069f55e2SEric Schrock  * it is not present.  We only need this once per library instance, so it is
67069f55e2SEric Schrock  * not part of the libzfs handle.
68069f55e2SEric Schrock  */
69069f55e2SEric Schrock static void *_topo_dlhandle;
70069f55e2SEric Schrock static topo_hdl_t *(*_topo_open)(int, const char *, int *);
71069f55e2SEric Schrock static void (*_topo_close)(topo_hdl_t *);
72069f55e2SEric Schrock static char *(*_topo_snap_hold)(topo_hdl_t *, const char *, int *);
73069f55e2SEric Schrock static void (*_topo_snap_release)(topo_hdl_t *);
74069f55e2SEric Schrock static topo_walk_t *(*_topo_walk_init)(topo_hdl_t *, const char *,
75069f55e2SEric Schrock     topo_walk_cb_t, void *, int *);
76069f55e2SEric Schrock static int (*_topo_walk_step)(topo_walk_t *, int);
77069f55e2SEric Schrock static void (*_topo_walk_fini)(topo_walk_t *);
78069f55e2SEric Schrock static void (*_topo_hdl_strfree)(topo_hdl_t *, char *);
79069f55e2SEric Schrock static char *(*_topo_node_name)(tnode_t *);
80069f55e2SEric Schrock static int (*_topo_prop_get_string)(tnode_t *, const char *, const char *,
81069f55e2SEric Schrock     char **, int *);
82069f55e2SEric Schrock static int (*_topo_node_fru)(tnode_t *, nvlist_t **, nvlist_t *, int *);
83069f55e2SEric Schrock static int (*_topo_fmri_nvl2str)(topo_hdl_t *, nvlist_t *, char **, int *);
84069f55e2SEric Schrock static int (*_topo_fmri_strcmp_noauth)(topo_hdl_t *, const char *,
85069f55e2SEric Schrock     const char *);
86069f55e2SEric Schrock 
87069f55e2SEric Schrock #define	ZFS_FRU_HASH_SIZE	257
88069f55e2SEric Schrock 
89069f55e2SEric Schrock static size_t
fru_strhash(const char * key)90069f55e2SEric Schrock fru_strhash(const char *key)
91069f55e2SEric Schrock {
92069f55e2SEric Schrock 	ulong_t g, h = 0;
93069f55e2SEric Schrock 	const char *p;
94069f55e2SEric Schrock 
95069f55e2SEric Schrock 	for (p = key; *p != '\0'; p++) {
96069f55e2SEric Schrock 		h = (h << 4) + *p;
97069f55e2SEric Schrock 
98069f55e2SEric Schrock 		if ((g = (h & 0xf0000000)) != 0) {
99069f55e2SEric Schrock 			h ^= (g >> 24);
100069f55e2SEric Schrock 			h ^= g;
101069f55e2SEric Schrock 		}
102069f55e2SEric Schrock 	}
103069f55e2SEric Schrock 
104069f55e2SEric Schrock 	return (h % ZFS_FRU_HASH_SIZE);
105069f55e2SEric Schrock }
106069f55e2SEric Schrock 
107069f55e2SEric Schrock static int
libzfs_fru_gather(topo_hdl_t * thp,tnode_t * tn,void * arg)108069f55e2SEric Schrock libzfs_fru_gather(topo_hdl_t *thp, tnode_t *tn, void *arg)
109069f55e2SEric Schrock {
110069f55e2SEric Schrock 	libzfs_handle_t *hdl = arg;
111069f55e2SEric Schrock 	nvlist_t *fru;
112069f55e2SEric Schrock 	char *devpath, *frustr;
113069f55e2SEric Schrock 	int err;
114069f55e2SEric Schrock 	libzfs_fru_t *frup;
115069f55e2SEric Schrock 	size_t idx;
116069f55e2SEric Schrock 
117069f55e2SEric Schrock 	/*
118069f55e2SEric Schrock 	 * If this is the chassis node, and we don't yet have the system
119069f55e2SEric Schrock 	 * chassis ID, then fill in this value now.
120069f55e2SEric Schrock 	 */
121069f55e2SEric Schrock 	if (hdl->libzfs_chassis_id[0] == '\0' &&
122069f55e2SEric Schrock 	    strcmp(_topo_node_name(tn), "chassis") == 0) {
123069f55e2SEric Schrock 		if (_topo_prop_get_string(tn, FM_FMRI_AUTHORITY,
124069f55e2SEric Schrock 		    FM_FMRI_AUTH_CHASSIS, &devpath, &err) == 0)
125069f55e2SEric Schrock 			(void) strlcpy(hdl->libzfs_chassis_id, devpath,
126069f55e2SEric Schrock 			    sizeof (hdl->libzfs_chassis_id));
127069f55e2SEric Schrock 	}
128069f55e2SEric Schrock 
129069f55e2SEric Schrock 	/*
130069f55e2SEric Schrock 	 * Skip non-disk nodes.
131069f55e2SEric Schrock 	 */
132069f55e2SEric Schrock 	if (strcmp(_topo_node_name(tn), "disk") != 0)
133069f55e2SEric Schrock 		return (TOPO_WALK_NEXT);
134069f55e2SEric Schrock 
135069f55e2SEric Schrock 	/*
136069f55e2SEric Schrock 	 * Get the devfs path and FRU.
137069f55e2SEric Schrock 	 */
138069f55e2SEric Schrock 	if (_topo_prop_get_string(tn, "io", "devfs-path", &devpath, &err) != 0)
139069f55e2SEric Schrock 		return (TOPO_WALK_NEXT);
140069f55e2SEric Schrock 
141069f55e2SEric Schrock 	if (libzfs_fru_lookup(hdl, devpath) != NULL) {
142069f55e2SEric Schrock 		_topo_hdl_strfree(thp, devpath);
143069f55e2SEric Schrock 		return (TOPO_WALK_NEXT);
144069f55e2SEric Schrock 	}
145069f55e2SEric Schrock 
146069f55e2SEric Schrock 	if (_topo_node_fru(tn, &fru, NULL, &err) != 0) {
147069f55e2SEric Schrock 		_topo_hdl_strfree(thp, devpath);
148069f55e2SEric Schrock 		return (TOPO_WALK_NEXT);
149069f55e2SEric Schrock 	}
150069f55e2SEric Schrock 
151069f55e2SEric Schrock 	/*
152069f55e2SEric Schrock 	 * Convert the FRU into a string.
153069f55e2SEric Schrock 	 */
154069f55e2SEric Schrock 	if (_topo_fmri_nvl2str(thp, fru, &frustr, &err) != 0) {
155069f55e2SEric Schrock 		nvlist_free(fru);
156069f55e2SEric Schrock 		_topo_hdl_strfree(thp, devpath);
157069f55e2SEric Schrock 		return (TOPO_WALK_NEXT);
158069f55e2SEric Schrock 	}
159069f55e2SEric Schrock 
160069f55e2SEric Schrock 	nvlist_free(fru);
161069f55e2SEric Schrock 
162069f55e2SEric Schrock 	/*
163069f55e2SEric Schrock 	 * Finally, we have a FRU string and device path.  Add it to the hash.
164069f55e2SEric Schrock 	 */
165069f55e2SEric Schrock 	if ((frup = calloc(sizeof (libzfs_fru_t), 1)) == NULL) {
166069f55e2SEric Schrock 		_topo_hdl_strfree(thp, devpath);
167069f55e2SEric Schrock 		_topo_hdl_strfree(thp, frustr);
168069f55e2SEric Schrock 		return (TOPO_WALK_NEXT);
169069f55e2SEric Schrock 	}
170069f55e2SEric Schrock 
171069f55e2SEric Schrock 	if ((frup->zf_device = strdup(devpath)) == NULL ||
172069f55e2SEric Schrock 	    (frup->zf_fru = strdup(frustr)) == NULL) {
173069f55e2SEric Schrock 		free(frup->zf_device);
174069f55e2SEric Schrock 		free(frup);
175069f55e2SEric Schrock 		_topo_hdl_strfree(thp, devpath);
176069f55e2SEric Schrock 		_topo_hdl_strfree(thp, frustr);
177069f55e2SEric Schrock 		return (TOPO_WALK_NEXT);
178069f55e2SEric Schrock 	}
179069f55e2SEric Schrock 
180069f55e2SEric Schrock 	_topo_hdl_strfree(thp, devpath);
181069f55e2SEric Schrock 	_topo_hdl_strfree(thp, frustr);
182069f55e2SEric Schrock 
183069f55e2SEric Schrock 	idx = fru_strhash(frup->zf_device);
184069f55e2SEric Schrock 	frup->zf_chain = hdl->libzfs_fru_hash[idx];
185069f55e2SEric Schrock 	hdl->libzfs_fru_hash[idx] = frup;
186069f55e2SEric Schrock 	frup->zf_next = hdl->libzfs_fru_list;
187069f55e2SEric Schrock 	hdl->libzfs_fru_list = frup;
188069f55e2SEric Schrock 
189069f55e2SEric Schrock 	return (TOPO_WALK_NEXT);
190069f55e2SEric Schrock }
191069f55e2SEric Schrock 
192069f55e2SEric Schrock /*
193069f55e2SEric Schrock  * Called during initialization to setup the dynamic libtopo connection.
194069f55e2SEric Schrock  */
195069f55e2SEric Schrock #pragma init(libzfs_init_fru)
196069f55e2SEric Schrock static void
libzfs_init_fru(void)197069f55e2SEric Schrock libzfs_init_fru(void)
198069f55e2SEric Schrock {
199069f55e2SEric Schrock 	char path[MAXPATHLEN];
200069f55e2SEric Schrock 	char isa[257];
201069f55e2SEric Schrock 
202*6d5f0164SAndy Fiddaman 	if (getzoneid() != GLOBAL_ZONEID)
203*6d5f0164SAndy Fiddaman 		return;
204*6d5f0164SAndy Fiddaman 
205069f55e2SEric Schrock #if defined(_LP64)
206069f55e2SEric Schrock 	if (sysinfo(SI_ARCHITECTURE_64, isa, sizeof (isa)) < 0)
207069f55e2SEric Schrock 		isa[0] = '\0';
208069f55e2SEric Schrock #else
209069f55e2SEric Schrock 	isa[0] = '\0';
210069f55e2SEric Schrock #endif
211069f55e2SEric Schrock 	(void) snprintf(path, sizeof (path),
212069f55e2SEric Schrock 	    "/usr/lib/fm/%s/libtopo.so", isa);
213069f55e2SEric Schrock 
214069f55e2SEric Schrock 	if ((_topo_dlhandle = dlopen(path, RTLD_LAZY)) == NULL)
215069f55e2SEric Schrock 		return;
216069f55e2SEric Schrock 
217069f55e2SEric Schrock 	_topo_open = (topo_hdl_t *(*)())
218069f55e2SEric Schrock 	    dlsym(_topo_dlhandle, "topo_open");
219069f55e2SEric Schrock 	_topo_close = (void (*)())
220069f55e2SEric Schrock 	    dlsym(_topo_dlhandle, "topo_close");
221069f55e2SEric Schrock 	_topo_snap_hold = (char *(*)())
222069f55e2SEric Schrock 	    dlsym(_topo_dlhandle, "topo_snap_hold");
223069f55e2SEric Schrock 	_topo_snap_release = (void (*)())
224069f55e2SEric Schrock 	    dlsym(_topo_dlhandle, "topo_snap_release");
225069f55e2SEric Schrock 	_topo_walk_init = (topo_walk_t *(*)())
226069f55e2SEric Schrock 	    dlsym(_topo_dlhandle, "topo_walk_init");
227069f55e2SEric Schrock 	_topo_walk_step = (int (*)())
228069f55e2SEric Schrock 	    dlsym(_topo_dlhandle, "topo_walk_step");
229069f55e2SEric Schrock 	_topo_walk_fini = (void (*)())
230069f55e2SEric Schrock 	    dlsym(_topo_dlhandle, "topo_walk_fini");
231069f55e2SEric Schrock 	_topo_hdl_strfree = (void (*)())
232069f55e2SEric Schrock 	    dlsym(_topo_dlhandle, "topo_hdl_strfree");
233069f55e2SEric Schrock 	_topo_node_name = (char *(*)())
234069f55e2SEric Schrock 	    dlsym(_topo_dlhandle, "topo_node_name");
235069f55e2SEric Schrock 	_topo_prop_get_string = (int (*)())
236069f55e2SEric Schrock 	    dlsym(_topo_dlhandle, "topo_prop_get_string");
237069f55e2SEric Schrock 	_topo_node_fru = (int (*)())
238069f55e2SEric Schrock 	    dlsym(_topo_dlhandle, "topo_node_fru");
239069f55e2SEric Schrock 	_topo_fmri_nvl2str = (int (*)())
240069f55e2SEric Schrock 	    dlsym(_topo_dlhandle, "topo_fmri_nvl2str");
241069f55e2SEric Schrock 	_topo_fmri_strcmp_noauth = (int (*)())
242069f55e2SEric Schrock 	    dlsym(_topo_dlhandle, "topo_fmri_strcmp_noauth");
243069f55e2SEric Schrock 
244069f55e2SEric Schrock 	if (_topo_open == NULL || _topo_close == NULL ||
245069f55e2SEric Schrock 	    _topo_snap_hold == NULL || _topo_snap_release == NULL ||
246069f55e2SEric Schrock 	    _topo_walk_init == NULL || _topo_walk_step == NULL ||
247069f55e2SEric Schrock 	    _topo_walk_fini == NULL || _topo_hdl_strfree == NULL ||
248069f55e2SEric Schrock 	    _topo_node_name == NULL || _topo_prop_get_string == NULL ||
249069f55e2SEric Schrock 	    _topo_node_fru == NULL || _topo_fmri_nvl2str == NULL ||
250069f55e2SEric Schrock 	    _topo_fmri_strcmp_noauth == NULL) {
251069f55e2SEric Schrock 		(void) dlclose(_topo_dlhandle);
252069f55e2SEric Schrock 		_topo_dlhandle = NULL;
253069f55e2SEric Schrock 	}
254069f55e2SEric Schrock }
255069f55e2SEric Schrock 
256069f55e2SEric Schrock /*
257069f55e2SEric Schrock  * Refresh the mappings from device path -> FMRI.  We do this by walking the
258069f55e2SEric Schrock  * hc topology looking for disk nodes, and recording the io/devfs-path and FRU.
259069f55e2SEric Schrock  * Note that we strip out the disk-specific authority information (serial,
260069f55e2SEric Schrock  * part, revision, etc) so that we are left with only the identifying
261069f55e2SEric Schrock  * characteristics of the slot (hc path and chassis-id).
262069f55e2SEric Schrock  */
263069f55e2SEric Schrock void
libzfs_fru_refresh(libzfs_handle_t * hdl)264069f55e2SEric Schrock libzfs_fru_refresh(libzfs_handle_t *hdl)
265069f55e2SEric Schrock {
266069f55e2SEric Schrock 	int err;
267069f55e2SEric Schrock 	char *uuid;
268069f55e2SEric Schrock 	topo_hdl_t *thp;
269069f55e2SEric Schrock 	topo_walk_t *twp;
270069f55e2SEric Schrock 
271069f55e2SEric Schrock 	if (_topo_dlhandle == NULL)
272069f55e2SEric Schrock 		return;
273069f55e2SEric Schrock 
274069f55e2SEric Schrock 	/*
275069f55e2SEric Schrock 	 * Clear the FRU hash and initialize our basic structures.
276069f55e2SEric Schrock 	 */
277069f55e2SEric Schrock 	libzfs_fru_clear(hdl, B_FALSE);
278069f55e2SEric Schrock 
279069f55e2SEric Schrock 	if ((hdl->libzfs_topo_hdl = _topo_open(TOPO_VERSION,
280069f55e2SEric Schrock 	    NULL, &err)) == NULL)
281069f55e2SEric Schrock 		return;
282069f55e2SEric Schrock 
283069f55e2SEric Schrock 	thp = hdl->libzfs_topo_hdl;
284069f55e2SEric Schrock 
285069f55e2SEric Schrock 	if ((uuid = _topo_snap_hold(thp, NULL, &err)) == NULL)
286069f55e2SEric Schrock 		return;
287069f55e2SEric Schrock 
288069f55e2SEric Schrock 	_topo_hdl_strfree(thp, uuid);
289069f55e2SEric Schrock 
290069f55e2SEric Schrock 	if (hdl->libzfs_fru_hash == NULL &&
291069f55e2SEric Schrock 	    (hdl->libzfs_fru_hash =
2925697e03eSPedro Giffuni 	    calloc(ZFS_FRU_HASH_SIZE, sizeof (void *))) == NULL)
293069f55e2SEric Schrock 		return;
294069f55e2SEric Schrock 
295069f55e2SEric Schrock 	/*
296069f55e2SEric Schrock 	 * We now have a topo snapshot, so iterate over the hc topology looking
297069f55e2SEric Schrock 	 * for disks to add to the hash.
298069f55e2SEric Schrock 	 */
299069f55e2SEric Schrock 	twp = _topo_walk_init(thp, FM_FMRI_SCHEME_HC,
300069f55e2SEric Schrock 	    libzfs_fru_gather, hdl, &err);
301069f55e2SEric Schrock 	if (twp != NULL) {
302069f55e2SEric Schrock 		(void) _topo_walk_step(twp, TOPO_WALK_CHILD);
303069f55e2SEric Schrock 		_topo_walk_fini(twp);
304069f55e2SEric Schrock 	}
305069f55e2SEric Schrock }
306069f55e2SEric Schrock 
307069f55e2SEric Schrock /*
308069f55e2SEric Schrock  * Given a devfs path, return the FRU for the device, if known.  This will
309069f55e2SEric Schrock  * automatically call libzfs_fru_refresh() if it hasn't already been called by
310069f55e2SEric Schrock  * the consumer.  The string returned is valid until the next call to
311069f55e2SEric Schrock  * libzfs_fru_refresh().
312069f55e2SEric Schrock  */
313069f55e2SEric Schrock const char *
libzfs_fru_lookup(libzfs_handle_t * hdl,const char * devpath)314069f55e2SEric Schrock libzfs_fru_lookup(libzfs_handle_t *hdl, const char *devpath)
315069f55e2SEric Schrock {
316069f55e2SEric Schrock 	size_t idx = fru_strhash(devpath);
317069f55e2SEric Schrock 	libzfs_fru_t *frup;
318069f55e2SEric Schrock 
319069f55e2SEric Schrock 	if (hdl->libzfs_fru_hash == NULL)
320069f55e2SEric Schrock 		libzfs_fru_refresh(hdl);
321069f55e2SEric Schrock 
322069f55e2SEric Schrock 	if (hdl->libzfs_fru_hash == NULL)
323069f55e2SEric Schrock 		return (NULL);
324069f55e2SEric Schrock 
325069f55e2SEric Schrock 	for (frup = hdl->libzfs_fru_hash[idx]; frup != NULL;
326069f55e2SEric Schrock 	    frup = frup->zf_chain) {
327069f55e2SEric Schrock 		if (strcmp(devpath, frup->zf_device) == 0)
328069f55e2SEric Schrock 			return (frup->zf_fru);
329069f55e2SEric Schrock 	}
330069f55e2SEric Schrock 
331069f55e2SEric Schrock 	return (NULL);
332069f55e2SEric Schrock }
333069f55e2SEric Schrock 
334069f55e2SEric Schrock /*
335069f55e2SEric Schrock  * Given a fru path, return the device path.  This will automatically call
336069f55e2SEric Schrock  * libzfs_fru_refresh() if it hasn't already been called by the consumer.  The
337069f55e2SEric Schrock  * string returned is valid until the next call to libzfs_fru_refresh().
338069f55e2SEric Schrock  */
339069f55e2SEric Schrock const char *
libzfs_fru_devpath(libzfs_handle_t * hdl,const char * fru)340069f55e2SEric Schrock libzfs_fru_devpath(libzfs_handle_t *hdl, const char *fru)
341069f55e2SEric Schrock {
342069f55e2SEric Schrock 	libzfs_fru_t *frup;
343069f55e2SEric Schrock 	size_t idx;
344069f55e2SEric Schrock 
345069f55e2SEric Schrock 	if (hdl->libzfs_fru_hash == NULL)
346069f55e2SEric Schrock 		libzfs_fru_refresh(hdl);
347069f55e2SEric Schrock 
348069f55e2SEric Schrock 	if (hdl->libzfs_fru_hash == NULL)
349069f55e2SEric Schrock 		return (NULL);
350069f55e2SEric Schrock 
351069f55e2SEric Schrock 	for (idx = 0; idx < ZFS_FRU_HASH_SIZE; idx++) {
352069f55e2SEric Schrock 		for (frup = hdl->libzfs_fru_hash[idx]; frup != NULL;
353069f55e2SEric Schrock 		    frup = frup->zf_next) {
354069f55e2SEric Schrock 			if (_topo_fmri_strcmp_noauth(hdl->libzfs_topo_hdl,
355069f55e2SEric Schrock 			    fru, frup->zf_fru))
356069f55e2SEric Schrock 				return (frup->zf_device);
357069f55e2SEric Schrock 		}
358069f55e2SEric Schrock 	}
359069f55e2SEric Schrock 
360069f55e2SEric Schrock 	return (NULL);
361069f55e2SEric Schrock }
362069f55e2SEric Schrock 
363069f55e2SEric Schrock /*
364069f55e2SEric Schrock  * Change the stored FRU for the given vdev.
365069f55e2SEric Schrock  */
366069f55e2SEric Schrock int
zpool_fru_set(zpool_handle_t * zhp,uint64_t vdev_guid,const char * fru)367069f55e2SEric Schrock zpool_fru_set(zpool_handle_t *zhp, uint64_t vdev_guid, const char *fru)
368069f55e2SEric Schrock {
369069f55e2SEric Schrock 	zfs_cmd_t zc = { 0 };
370069f55e2SEric Schrock 
371069f55e2SEric Schrock 	(void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
372069f55e2SEric Schrock 	(void) strncpy(zc.zc_value, fru, sizeof (zc.zc_value));
373069f55e2SEric Schrock 	zc.zc_guid = vdev_guid;
374069f55e2SEric Schrock 
375069f55e2SEric Schrock 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SETFRU, &zc) != 0)
376069f55e2SEric Schrock 		return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
377069f55e2SEric Schrock 		    dgettext(TEXT_DOMAIN, "cannot set FRU")));
378069f55e2SEric Schrock 
379069f55e2SEric Schrock 	return (0);
380069f55e2SEric Schrock }
381069f55e2SEric Schrock 
382069f55e2SEric Schrock /*
383069f55e2SEric Schrock  * Compare to two FRUs, ignoring any authority information.
384069f55e2SEric Schrock  */
385069f55e2SEric Schrock boolean_t
libzfs_fru_compare(libzfs_handle_t * hdl,const char * a,const char * b)386069f55e2SEric Schrock libzfs_fru_compare(libzfs_handle_t *hdl, const char *a, const char *b)
387069f55e2SEric Schrock {
388069f55e2SEric Schrock 	if (hdl->libzfs_fru_hash == NULL)
389069f55e2SEric Schrock 		libzfs_fru_refresh(hdl);
390069f55e2SEric Schrock 
391069f55e2SEric Schrock 	if (hdl->libzfs_fru_hash == NULL)
392069f55e2SEric Schrock 		return (strcmp(a, b) == 0);
393069f55e2SEric Schrock 
394069f55e2SEric Schrock 	return (_topo_fmri_strcmp_noauth(hdl->libzfs_topo_hdl, a, b));
395069f55e2SEric Schrock }
396069f55e2SEric Schrock 
397069f55e2SEric Schrock /*
398069f55e2SEric Schrock  * This special function checks to see whether the FRU indicates it's supposed
399069f55e2SEric Schrock  * to be in the system chassis, but the chassis-id doesn't match.  This can
400069f55e2SEric Schrock  * happen in a clustered case, where both head nodes have the same logical
401069f55e2SEric Schrock  * disk, but opening the device on the other head node is meaningless.
402069f55e2SEric Schrock  */
403069f55e2SEric Schrock boolean_t
libzfs_fru_notself(libzfs_handle_t * hdl,const char * fru)404069f55e2SEric Schrock libzfs_fru_notself(libzfs_handle_t *hdl, const char *fru)
405069f55e2SEric Schrock {
406069f55e2SEric Schrock 	const char *chassisid;
407069f55e2SEric Schrock 	size_t len;
408069f55e2SEric Schrock 
409069f55e2SEric Schrock 	if (hdl->libzfs_fru_hash == NULL)
410069f55e2SEric Schrock 		libzfs_fru_refresh(hdl);
411069f55e2SEric Schrock 
412069f55e2SEric Schrock 	if (hdl->libzfs_chassis_id[0] == '\0')
413069f55e2SEric Schrock 		return (B_FALSE);
414069f55e2SEric Schrock 
415069f55e2SEric Schrock 	if (strstr(fru, "/chassis=0/") == NULL)
416069f55e2SEric Schrock 		return (B_FALSE);
417069f55e2SEric Schrock 
418069f55e2SEric Schrock 	if ((chassisid = strstr(fru, ":chassis-id=")) == NULL)
419069f55e2SEric Schrock 		return (B_FALSE);
420069f55e2SEric Schrock 
421069f55e2SEric Schrock 	chassisid += 12;
422069f55e2SEric Schrock 	len = strlen(hdl->libzfs_chassis_id);
423069f55e2SEric Schrock 	if (strncmp(chassisid, hdl->libzfs_chassis_id, len) == 0 &&
424069f55e2SEric Schrock 	    (chassisid[len] == '/' || chassisid[len] == ':'))
425069f55e2SEric Schrock 		return (B_FALSE);
426069f55e2SEric Schrock 
427069f55e2SEric Schrock 	return (B_TRUE);
428069f55e2SEric Schrock }
429069f55e2SEric Schrock 
430069f55e2SEric Schrock /*
431069f55e2SEric Schrock  * Clear memory associated with the FRU hash.
432069f55e2SEric Schrock  */
433069f55e2SEric Schrock void
libzfs_fru_clear(libzfs_handle_t * hdl,boolean_t final)434069f55e2SEric Schrock libzfs_fru_clear(libzfs_handle_t *hdl, boolean_t final)
435069f55e2SEric Schrock {
436069f55e2SEric Schrock 	libzfs_fru_t *frup;
437069f55e2SEric Schrock 
438069f55e2SEric Schrock 	while ((frup = hdl->libzfs_fru_list) != NULL) {
439069f55e2SEric Schrock 		hdl->libzfs_fru_list = frup->zf_next;
440069f55e2SEric Schrock 		free(frup->zf_device);
441069f55e2SEric Schrock 		free(frup->zf_fru);
442069f55e2SEric Schrock 		free(frup);
443069f55e2SEric Schrock 	}
444069f55e2SEric Schrock 
445069f55e2SEric Schrock 	hdl->libzfs_fru_list = NULL;
446069f55e2SEric Schrock 
447069f55e2SEric Schrock 	if (hdl->libzfs_topo_hdl != NULL) {
448069f55e2SEric Schrock 		_topo_snap_release(hdl->libzfs_topo_hdl);
449069f55e2SEric Schrock 		_topo_close(hdl->libzfs_topo_hdl);
450069f55e2SEric Schrock 		hdl->libzfs_topo_hdl = NULL;
451069f55e2SEric Schrock 	}
452069f55e2SEric Schrock 
453069f55e2SEric Schrock 	if (final) {
454069f55e2SEric Schrock 		free(hdl->libzfs_fru_hash);
455069f55e2SEric Schrock 	} else if (hdl->libzfs_fru_hash != NULL) {
456069f55e2SEric Schrock 		bzero(hdl->libzfs_fru_hash,
457069f55e2SEric Schrock 		    ZFS_FRU_HASH_SIZE * sizeof (void *));
458069f55e2SEric Schrock 	}
459069f55e2SEric Schrock }
460