xref: /freebsd/cddl/usr.sbin/zfsd/vdev_iterator.cc (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
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 
33 /**
34  * \file vdev_iterator.cc
35  *
36  * Implementation of the VdevIterator class.
37  */
38 #include <sys/cdefs.h>
39 #include <sys/byteorder.h>
40 #include <sys/fs/zfs.h>
41 
42 #include <stdint.h>
43 #include <syslog.h>
44 
45 #include <libzfs.h>
46 
47 #include <list>
48 #include <string>
49 
50 #include <devdctl/exception.h>
51 #include <devdctl/guid.h>
52 
53 #include "vdev.h"
54 #include "vdev_iterator.h"
55 #include "zfsd_exception.h"
56 
57 /*============================ Namespace Control =============================*/
58 using DevdCtl::Guid;
59 
60 /*=========================== Class Implementations ==========================*/
61 /*------------------------------- VdevIterator -------------------------------*/
62 VdevIterator::VdevIterator(zpool_handle_t *pool)
63  : m_poolConfig(zpool_get_config(pool, NULL))
64 {
65 	Reset();
66 }
67 
68 VdevIterator::VdevIterator(nvlist_t *poolConfig)
69  : m_poolConfig(poolConfig)
70 {
71 	Reset();
72 }
73 
74 void
75 VdevIterator::Reset()
76 {
77 	nvlist_t  *rootVdev;
78 	nvlist	  **cache_child;
79 	nvlist	  **spare_child;
80 	int	   result;
81 	uint_t   cache_children;
82 	uint_t	 spare_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 	result = nvlist_lookup_nvlist_array(rootVdev,
99 					    ZPOOL_CONFIG_SPARES,
100 					    &spare_child,
101 					    &spare_children);
102 	if (result == 0)
103 		for (uint_t c = 0; c < spare_children; c++)
104 			m_vdevQueue.push_back(spare_child[c]);
105 }
106 
107 nvlist_t *
108 VdevIterator::Next()
109 {
110 	nvlist_t *vdevConfig;
111 
112 	if (m_vdevQueue.empty())
113 		return (NULL);
114 
115 	for (;;) {
116 		nvlist_t **vdevChildren;
117 		int        result;
118 		u_int      numChildren;
119 
120 		vdevConfig = m_vdevQueue.front();
121 		m_vdevQueue.pop_front();
122 
123 		/* Expand non-leaf vdevs. */
124 		result = nvlist_lookup_nvlist_array(vdevConfig,
125 						    ZPOOL_CONFIG_CHILDREN,
126 						   &vdevChildren, &numChildren);
127 		if (result != 0) {
128 			/* leaf vdev */
129 			break;
130 		}
131 
132 		/*
133 		 * Insert children at the head of the queue to effect a
134 		 * depth first traversal of the tree.
135 		 */
136 		m_vdevQueue.insert(m_vdevQueue.begin(), vdevChildren,
137 				   vdevChildren + numChildren);
138 	}
139 
140 	return (vdevConfig);
141 }
142 
143 void
144 VdevIterator::Each(VdevCallback_t *callBack, void *callBackArg)
145 {
146 	nvlist_t *vdevConfig;
147 
148 	Reset();
149 	while ((vdevConfig = Next()) != NULL) {
150 		Vdev vdev(m_poolConfig, vdevConfig);
151 
152 		if (callBack(vdev, callBackArg))
153 			break;
154 	}
155 }
156 
157 nvlist_t *
158 VdevIterator::Find(Guid vdevGUID)
159 {
160 	nvlist_t *vdevConfig;
161 
162 	Reset();
163 	while ((vdevConfig = Next()) != NULL) {
164 		Vdev vdev(m_poolConfig, vdevConfig);
165 
166 		if (vdev.GUID() == vdevGUID)
167 			return (vdevConfig);
168 	}
169 	return (NULL);
170 }
171