xref: /freebsd/cddl/usr.sbin/zfsd/vdev_iterator.cc (revision ccfd87fe2ac0e2e6aeb1911a7d7cce6712a8564f)
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 -------------------------------*/
64 VdevIterator::VdevIterator(zpool_handle_t *pool)
65  : m_poolConfig(zpool_get_config(pool, NULL))
66 {
67 	Reset();
68 }
69 
70 VdevIterator::VdevIterator(nvlist_t *poolConfig)
71  : m_poolConfig(poolConfig)
72 {
73 	Reset();
74 }
75 
76 void
77 VdevIterator::Reset()
78 {
79 	nvlist_t  *rootVdev;
80 	nvlist	  **cache_child;
81 	nvlist	  **spare_child;
82 	int	   result;
83 	uint_t   cache_children;
84 	uint_t	 spare_children;
85 
86 	result = nvlist_lookup_nvlist(m_poolConfig,
87 				      ZPOOL_CONFIG_VDEV_TREE,
88 				      &rootVdev);
89 	if (result != 0)
90 		throw ZfsdException(m_poolConfig, "Unable to extract "
91 				    "ZPOOL_CONFIG_VDEV_TREE from pool.");
92 	m_vdevQueue.assign(1, rootVdev);
93 	result = nvlist_lookup_nvlist_array(rootVdev,
94 				      	    ZPOOL_CONFIG_L2CACHE,
95 				      	    &cache_child,
96 					    &cache_children);
97 	if (result == 0)
98 		for (uint_t c = 0; c < cache_children; c++)
99 			m_vdevQueue.push_back(cache_child[c]);
100 	result = nvlist_lookup_nvlist_array(rootVdev,
101 					    ZPOOL_CONFIG_SPARES,
102 					    &spare_child,
103 					    &spare_children);
104 	if (result == 0)
105 		for (uint_t c = 0; c < spare_children; c++)
106 			m_vdevQueue.push_back(spare_child[c]);
107 }
108 
109 nvlist_t *
110 VdevIterator::Next()
111 {
112 	nvlist_t *vdevConfig;
113 
114 	if (m_vdevQueue.empty())
115 		return (NULL);
116 
117 	for (;;) {
118 		nvlist_t **vdevChildren;
119 		int        result;
120 		u_int      numChildren;
121 
122 		vdevConfig = m_vdevQueue.front();
123 		m_vdevQueue.pop_front();
124 
125 		/* Expand non-leaf vdevs. */
126 		result = nvlist_lookup_nvlist_array(vdevConfig,
127 						    ZPOOL_CONFIG_CHILDREN,
128 						   &vdevChildren, &numChildren);
129 		if (result != 0) {
130 			/* leaf vdev */
131 			break;
132 		}
133 
134 		/*
135 		 * Insert children at the head of the queue to effect a
136 		 * depth first traversal of the tree.
137 		 */
138 		m_vdevQueue.insert(m_vdevQueue.begin(), vdevChildren,
139 				   vdevChildren + numChildren);
140 	}
141 
142 	return (vdevConfig);
143 }
144 
145 void
146 VdevIterator::Each(VdevCallback_t *callBack, void *callBackArg)
147 {
148 	nvlist_t *vdevConfig;
149 
150 	Reset();
151 	while ((vdevConfig = Next()) != NULL) {
152 		Vdev vdev(m_poolConfig, vdevConfig);
153 
154 		if (callBack(vdev, callBackArg))
155 			break;
156 	}
157 }
158 
159 nvlist_t *
160 VdevIterator::Find(Guid vdevGUID)
161 {
162 	nvlist_t *vdevConfig;
163 
164 	Reset();
165 	while ((vdevConfig = Next()) != NULL) {
166 		Vdev vdev(m_poolConfig, vdevConfig);
167 
168 		if (vdev.GUID() == vdevGUID)
169 			return (vdevConfig);
170 	}
171 	return (NULL);
172 }
173