2 * Copyright (c) 2011, 2012, 2013 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)
36 * \file vdev_iterator.cc
38 * Implementation of the VdevIterator class.
40 #include <sys/cdefs.h>
41 #include <sys/byteorder.h>
42 #include <sys/fs/zfs.h>
52 #include <devdctl/exception.h>
53 #include <devdctl/guid.h>
56 #include "vdev_iterator.h"
57 #include "zfsd_exception.h"
59 /*============================ Namespace Control =============================*/
62 /*=========================== Class Implementations ==========================*/
63 /*------------------------------- VdevIterator -------------------------------*/
64 VdevIterator::VdevIterator(zpool_handle_t *pool)
65 : m_poolConfig(zpool_get_config(pool, NULL))
70 VdevIterator::VdevIterator(nvlist_t *poolConfig)
71 : m_poolConfig(poolConfig)
82 uint_t cache_children;
84 result = nvlist_lookup_nvlist(m_poolConfig,
85 ZPOOL_CONFIG_VDEV_TREE,
88 throw ZfsdException(m_poolConfig, "Unable to extract "
89 "ZPOOL_CONFIG_VDEV_TREE from pool.");
90 m_vdevQueue.assign(1, rootVdev);
91 result = nvlist_lookup_nvlist_array(rootVdev,
96 for (uint_t c = 0; c < cache_children; c++)
97 m_vdevQueue.push_back(cache_child[c]);
103 nvlist_t *vdevConfig;
105 if (m_vdevQueue.empty())
109 nvlist_t **vdevChildren;
113 vdevConfig = m_vdevQueue.front();
114 m_vdevQueue.pop_front();
116 /* Expand non-leaf vdevs. */
117 result = nvlist_lookup_nvlist_array(vdevConfig,
118 ZPOOL_CONFIG_CHILDREN,
119 &vdevChildren, &numChildren);
126 * Insert children at the head of the queue to effect a
127 * depth first traversal of the tree.
129 m_vdevQueue.insert(m_vdevQueue.begin(), vdevChildren,
130 vdevChildren + numChildren);
137 VdevIterator::Each(VdevCallback_t *callBack, void *callBackArg)
139 nvlist_t *vdevConfig;
142 while ((vdevConfig = Next()) != NULL) {
143 Vdev vdev(m_poolConfig, vdevConfig);
145 if (callBack(vdev, callBackArg))
151 VdevIterator::Find(Guid vdevGUID)
153 nvlist_t *vdevConfig;
156 while ((vdevConfig = Next()) != NULL) {
157 Vdev vdev(m_poolConfig, vdevConfig);
159 if (vdev.GUID() == vdevGUID)