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