2 * Copyright (c) 2011, 2012, 2013, 2014 Spectra Logic Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions, and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 * substantially similar to the "NO WARRANTY" disclaimer below
13 * ("Disclaimer") and any redistribution must be conditioned upon
14 * including a substantially similar Disclaimer requirement for further
15 * binary redistribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGES.
30 * Authors: Justin T. Gibbs (Spectra Logic Corporation)
38 * Implementation of the Vdev class.
41 #include <sys/cdefs.h>
42 #include <sys/fs/zfs.h>
46 * Undefine flush, defined by cpufunc.h on sparc64, because it conflicts with
56 #include <devdctl/guid.h>
57 #include <devdctl/event.h>
58 #include <devdctl/event_factory.h>
59 #include <devdctl/exception.h>
60 #include <devdctl/consumer.h>
63 #include "vdev_iterator.h"
65 #include "zfsd_exception.h"
66 #include "zpool_list.h"
68 __FBSDID("$FreeBSD$");
69 /*============================ Namespace Control =============================*/
71 using std::stringstream;
73 //- Special objects -----------------------------------------------------------
76 //- Vdev Inline Public Methods ------------------------------------------------
77 /*=========================== Class Implementations ==========================*/
78 /*----------------------------------- Vdev -----------------------------------*/
80 /* Special constructor for NonexistentVdev. */
87 Vdev::VdevLookupPoolGuid()
90 if (nvlist_lookup_uint64(m_poolConfig, ZPOOL_CONFIG_POOL_GUID, &guid))
97 Vdev::VdevLookupGuid()
100 if (nvlist_lookup_uint64(m_config, ZPOOL_CONFIG_GUID, &guid) != 0)
101 throw ZfsdException("Unable to extract vdev GUID "
102 "from vdev config data.");
106 Vdev::Vdev(zpool_handle_t *pool, nvlist_t *config)
107 : m_poolConfig(zpool_get_config(pool, NULL)),
110 if (!VdevLookupPoolGuid())
111 throw ZfsdException("Can't extract pool GUID from handle.");
115 Vdev::Vdev(nvlist_t *poolConfig, nvlist_t *config)
116 : m_poolConfig(poolConfig),
119 if (!VdevLookupPoolGuid())
120 throw ZfsdException("Can't extract pool GUID from config.");
124 Vdev::Vdev(nvlist_t *labelConfig)
125 : m_poolConfig(labelConfig),
126 m_config(labelConfig)
129 * Spares do not have a Pool GUID. Tolerate its absence.
130 * Code accessing this Vdev in a context where the Pool GUID is
131 * required will find it invalid (as it is upon Vdev construction)
132 * and act accordingly.
134 (void) VdevLookupPoolGuid();
138 m_config = VdevIterator(labelConfig).Find(m_vdevGUID);
139 } catch (const ZfsdException &exp) {
141 * When reading a spare's label, it is normal not to find
149 Vdev::IsSpare() const
151 uint64_t is_spare(0);
153 if (m_config == NULL)
156 (void)nvlist_lookup_uint64(m_config, ZPOOL_CONFIG_IS_SPARE, &is_spare);
157 return (bool(is_spare));
163 uint64_t *nvlist_array;
167 if (m_config == NULL) {
169 * If we couldn't find the list of vdevs, that normally means
170 * that this is an available hotspare. In that case, we will
171 * presume it to be healthy. Even if this spare had formerly
172 * been in use, been degraded, and been replaced, the act of
173 * replacement wipes the degraded bit from the label. So we
174 * have no choice but to presume that it is healthy.
176 return (VDEV_STATE_HEALTHY);
179 if (nvlist_lookup_uint64_array(m_config, ZPOOL_CONFIG_VDEV_STATS,
180 &nvlist_array, &vsc) == 0) {
181 vs = reinterpret_cast<vdev_stat_t *>(nvlist_array);
182 return (static_cast<vdev_state>(vs->vs_state));
186 * Stats are not available. This vdev was created from a label.
187 * Synthesize a state based on available data.
190 uint64_t degraded(0);
191 (void)nvlist_lookup_uint64(m_config, ZPOOL_CONFIG_FAULTED, &faulted);
192 (void)nvlist_lookup_uint64(m_config, ZPOOL_CONFIG_DEGRADED, °raded);
194 return (VDEV_STATE_FAULTED);
196 return (VDEV_STATE_DEGRADED);
197 return (VDEV_STATE_HEALTHY);
203 nvlist_t **vdevChildren;
206 std::list<Vdev> children;
208 if (m_poolConfig == NULL || m_config == NULL)
211 result = nvlist_lookup_nvlist_array(m_config,
212 ZPOOL_CONFIG_CHILDREN, &vdevChildren, &numChildren);
216 for (u_int c = 0;c < numChildren; c++)
217 children.push_back(Vdev(m_poolConfig, vdevChildren[c]));
227 if (m_poolConfig == NULL)
228 return (NonexistentVdev);
230 if (nvlist_lookup_nvlist(m_poolConfig, ZPOOL_CONFIG_VDEV_TREE,
232 return (NonexistentVdev);
233 return (Vdev(m_poolConfig, rootVdev));
237 * Find our parent. This requires doing a traversal of the config; we can't
238 * cache it as leaf vdevs may change their pool config location (spare,
239 * replacing, mirror, etc).
244 std::list<Vdev> to_examine;
245 std::list<Vdev> children;
246 std::list<Vdev>::iterator children_it;
248 to_examine.push_back(RootVdev());
250 if (to_examine.empty())
251 return (NonexistentVdev);
252 Vdev vd = to_examine.front();
253 if (vd.DoesNotExist())
254 return (NonexistentVdev);
255 to_examine.pop_front();
256 children = vd.Children();
257 children_it = children.begin();
258 for (;children_it != children.end(); children_it++) {
259 Vdev child = *children_it;
261 if (child.GUID() == GUID())
263 to_examine.push_front(child);
269 Vdev::IsAvailableSpare() const
271 /* If we have a pool guid, we cannot be an available spare. */
282 if (nvlist_lookup_uint64(m_config, ZPOOL_CONFIG_IS_SPARE, &spare) != 0)
288 Vdev::IsActiveSpare() const
293 if (m_poolConfig == NULL)
296 (void) nvlist_lookup_uint64_array(m_config, ZPOOL_CONFIG_VDEV_STATS,
297 reinterpret_cast<uint64_t **>(&vs), &c);
298 if (vs == NULL || vs->vs_aux != VDEV_AUX_SPARED)
304 Vdev::IsResilvering() const
306 pool_scan_stat_t *ps = NULL;
309 if (State() != VDEV_STATE_HEALTHY)
312 (void) nvlist_lookup_uint64_array(m_config, ZPOOL_CONFIG_SCAN_STATS,
313 reinterpret_cast<uint64_t **>(&ps), &c);
314 if (ps == NULL || ps->pss_func != POOL_SCAN_RESILVER)
320 Vdev::GUIDString() const
322 stringstream vdevGUIDString;
324 vdevGUIDString << GUID();
325 return (vdevGUIDString.str());
329 Vdev::Name(zpool_handle_t *zhp, bool verbose) const
331 return (zpool_vdev_name(g_zfsHandle, zhp, m_config,
332 verbose ? B_TRUE : B_FALSE));
340 if ((m_config != NULL)
341 && (nvlist_lookup_string(m_config, ZPOOL_CONFIG_PATH, &path) == 0))
348 Vdev::PhysicalPath() const
352 if ((m_config != NULL) && (nvlist_lookup_string(m_config,
353 ZPOOL_CONFIG_PHYS_PATH, &path) == 0))