1 /*-
2 * Copyright (c) 2011, 2012, 2013 Spectra Logic Corporation
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
16 *
17 * NO WARRANTY
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.
29 *
30 * Authors: Justin T. Gibbs (Spectra Logic Corporation)
31 *
32 * $FreeBSD$
33 */
34
35 /**
36 * \file vdev_iterator.cc
37 *
38 * Implementation of the VdevIterator class.
39 */
40 #include <sys/cdefs.h>
41 #include <sys/byteorder.h>
42 #include <sys/fs/zfs.h>
43
44 #include <stdint.h>
45 #include <syslog.h>
46
47 #include <libzfs.h>
48
49 #include <list>
50 #include <string>
51
52 #include <devdctl/exception.h>
53 #include <devdctl/guid.h>
54
55 #include "vdev.h"
56 #include "vdev_iterator.h"
57 #include "zfsd_exception.h"
58
59 /*============================ Namespace Control =============================*/
60 using DevdCtl::Guid;
61
62 /*=========================== Class Implementations ==========================*/
63 /*------------------------------- VdevIterator -------------------------------*/
VdevIterator(zpool_handle_t * pool)64 VdevIterator::VdevIterator(zpool_handle_t *pool)
65 : m_poolConfig(zpool_get_config(pool, NULL))
66 {
67 Reset();
68 }
69
VdevIterator(nvlist_t * poolConfig)70 VdevIterator::VdevIterator(nvlist_t *poolConfig)
71 : m_poolConfig(poolConfig)
72 {
73 Reset();
74 }
75
76 void
Reset()77 VdevIterator::Reset()
78 {
79 nvlist_t *rootVdev;
80 nvlist **cache_child;
81 int result;
82 uint_t cache_children;
83
84 result = nvlist_lookup_nvlist(m_poolConfig,
85 ZPOOL_CONFIG_VDEV_TREE,
86 &rootVdev);
87 if (result != 0)
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,
92 ZPOOL_CONFIG_L2CACHE,
93 &cache_child,
94 &cache_children);
95 if (result == 0)
96 for (uint_t c = 0; c < cache_children; c++)
97 m_vdevQueue.push_back(cache_child[c]);
98 }
99
100 nvlist_t *
Next()101 VdevIterator::Next()
102 {
103 nvlist_t *vdevConfig;
104
105 if (m_vdevQueue.empty())
106 return (NULL);
107
108 for (;;) {
109 nvlist_t **vdevChildren;
110 int result;
111 u_int numChildren;
112
113 vdevConfig = m_vdevQueue.front();
114 m_vdevQueue.pop_front();
115
116 /* Expand non-leaf vdevs. */
117 result = nvlist_lookup_nvlist_array(vdevConfig,
118 ZPOOL_CONFIG_CHILDREN,
119 &vdevChildren, &numChildren);
120 if (result != 0) {
121 /* leaf vdev */
122 break;
123 }
124
125 /*
126 * Insert children at the head of the queue to effect a
127 * depth first traversal of the tree.
128 */
129 m_vdevQueue.insert(m_vdevQueue.begin(), vdevChildren,
130 vdevChildren + numChildren);
131 }
132
133 return (vdevConfig);
134 }
135
136 void
Each(VdevCallback_t * callBack,void * callBackArg)137 VdevIterator::Each(VdevCallback_t *callBack, void *callBackArg)
138 {
139 nvlist_t *vdevConfig;
140
141 Reset();
142 while ((vdevConfig = Next()) != NULL) {
143 Vdev vdev(m_poolConfig, vdevConfig);
144
145 if (callBack(vdev, callBackArg))
146 break;
147 }
148 }
149
150 nvlist_t *
Find(Guid vdevGUID)151 VdevIterator::Find(Guid vdevGUID)
152 {
153 nvlist_t *vdevConfig;
154
155 Reset();
156 while ((vdevConfig = Next()) != NULL) {
157 Vdev vdev(m_poolConfig, vdevConfig);
158
159 if (vdev.GUID() == vdevGUID)
160 return (vdevConfig);
161 }
162 return (NULL);
163 }
164