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