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/fs/zfs.h>
51 #include <devdctl/exception.h>
52 #include <devdctl/guid.h>
55 #include "vdev_iterator.h"
56 #include "zfsd_exception.h"
58 /*============================ Namespace Control =============================*/
61 /*=========================== Class Implementations ==========================*/
62 /*------------------------------- VdevIterator -------------------------------*/
63 VdevIterator::VdevIterator(zpool_handle_t *pool)
64 : m_poolConfig(zpool_get_config(pool, NULL))
69 VdevIterator::VdevIterator(nvlist_t *poolConfig)
70 : m_poolConfig(poolConfig)
81 uint_t cache_children;
83 result = nvlist_lookup_nvlist(m_poolConfig,
84 ZPOOL_CONFIG_VDEV_TREE,
87 throw ZfsdException(m_poolConfig, "Unable to extract "
88 "ZPOOL_CONFIG_VDEV_TREE from pool.");
89 m_vdevQueue.assign(1, rootVdev);
90 result = nvlist_lookup_nvlist_array(rootVdev,
95 for (uint_t c = 0; c < cache_children; c++)
96 m_vdevQueue.push_back(cache_child[c]);
102 nvlist_t *vdevConfig;
104 if (m_vdevQueue.empty())
108 nvlist_t **vdevChildren;
112 vdevConfig = m_vdevQueue.front();
113 m_vdevQueue.pop_front();
115 /* Expand non-leaf vdevs. */
116 result = nvlist_lookup_nvlist_array(vdevConfig,
117 ZPOOL_CONFIG_CHILDREN,
118 &vdevChildren, &numChildren);
125 * Insert children at the head of the queue to effect a
126 * depth first traversal of the tree.
128 m_vdevQueue.insert(m_vdevQueue.begin(), vdevChildren,
129 vdevChildren + numChildren);
136 VdevIterator::Each(VdevCallback_t *callBack, void *callBackArg)
138 nvlist_t *vdevConfig;
141 while ((vdevConfig = Next()) != NULL) {
142 Vdev vdev(m_poolConfig, vdevConfig);
144 if (callBack(vdev, callBackArg))
150 VdevIterator::Find(Guid vdevGUID)
152 nvlist_t *vdevConfig;
155 while ((vdevConfig = Next()) != NULL) {
156 Vdev vdev(m_poolConfig, vdevConfig);
158 if (vdev.GUID() == vdevGUID)