xref: /freebsd/cddl/usr.sbin/zfsd/vdev_iterator.h (revision 7a0c41d5d7d4e9770ef6f5d56f893efc8f18ab7c)
1*7a0c41d5SAlan Somers /*-
2*7a0c41d5SAlan Somers  * Copyright (c) 2011, 2012, 2013 Spectra Logic Corporation
3*7a0c41d5SAlan Somers  * All rights reserved.
4*7a0c41d5SAlan Somers  *
5*7a0c41d5SAlan Somers  * Redistribution and use in source and binary forms, with or without
6*7a0c41d5SAlan Somers  * modification, are permitted provided that the following conditions
7*7a0c41d5SAlan Somers  * are met:
8*7a0c41d5SAlan Somers  * 1. Redistributions of source code must retain the above copyright
9*7a0c41d5SAlan Somers  *    notice, this list of conditions, and the following disclaimer,
10*7a0c41d5SAlan Somers  *    without modification.
11*7a0c41d5SAlan Somers  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12*7a0c41d5SAlan Somers  *    substantially similar to the "NO WARRANTY" disclaimer below
13*7a0c41d5SAlan Somers  *    ("Disclaimer") and any redistribution must be conditioned upon
14*7a0c41d5SAlan Somers  *    including a substantially similar Disclaimer requirement for further
15*7a0c41d5SAlan Somers  *    binary redistribution.
16*7a0c41d5SAlan Somers  *
17*7a0c41d5SAlan Somers  * NO WARRANTY
18*7a0c41d5SAlan Somers  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19*7a0c41d5SAlan Somers  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20*7a0c41d5SAlan Somers  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21*7a0c41d5SAlan Somers  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22*7a0c41d5SAlan Somers  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23*7a0c41d5SAlan Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24*7a0c41d5SAlan Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25*7a0c41d5SAlan Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26*7a0c41d5SAlan Somers  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27*7a0c41d5SAlan Somers  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*7a0c41d5SAlan Somers  * POSSIBILITY OF SUCH DAMAGES.
29*7a0c41d5SAlan Somers  *
30*7a0c41d5SAlan Somers  * Authors: Justin T. Gibbs     (Spectra Logic Corporation)
31*7a0c41d5SAlan Somers  *
32*7a0c41d5SAlan Somers  * $FreeBSD$
33*7a0c41d5SAlan Somers  */
34*7a0c41d5SAlan Somers 
35*7a0c41d5SAlan Somers /**
36*7a0c41d5SAlan Somers  * \file vdev_iterator.h
37*7a0c41d5SAlan Somers  *
38*7a0c41d5SAlan Somers  * VdevIterator class definition.
39*7a0c41d5SAlan Somers  *
40*7a0c41d5SAlan Somers  * Header requirements:
41*7a0c41d5SAlan Somers  *
42*7a0c41d5SAlan Somers  *    #include <list>
43*7a0c41d5SAlan Somers  */
44*7a0c41d5SAlan Somers #ifndef	_VDEV_ITERATOR_H_
45*7a0c41d5SAlan Somers #define	_VDEV_ITERATOR_H_
46*7a0c41d5SAlan Somers 
47*7a0c41d5SAlan Somers /*=========================== Forward Declarations ===========================*/
48*7a0c41d5SAlan Somers struct zpool_handle;
49*7a0c41d5SAlan Somers typedef struct zpool_handle zpool_handle_t;
50*7a0c41d5SAlan Somers 
51*7a0c41d5SAlan Somers struct nvlist;
52*7a0c41d5SAlan Somers typedef struct nvlist nvlist_t;
53*7a0c41d5SAlan Somers 
54*7a0c41d5SAlan Somers class Vdev;
55*7a0c41d5SAlan Somers 
56*7a0c41d5SAlan Somers /*============================= Class Definitions ============================*/
57*7a0c41d5SAlan Somers /*------------------------------- VdevIterator -------------------------------*/
58*7a0c41d5SAlan Somers typedef bool VdevCallback_t(Vdev &vdev, void *cbArg);
59*7a0c41d5SAlan Somers 
60*7a0c41d5SAlan Somers /**
61*7a0c41d5SAlan Somers  * \brief VdevIterator provides mechanisms for traversing and searching
62*7a0c41d5SAlan Somers  *        the leaf vdevs contained in a ZFS pool configuration.
63*7a0c41d5SAlan Somers  */
64*7a0c41d5SAlan Somers class VdevIterator
65*7a0c41d5SAlan Somers {
66*7a0c41d5SAlan Somers public:
67*7a0c41d5SAlan Somers 	/**
68*7a0c41d5SAlan Somers 	 * \brief Instantiate a VdevIterator for the given ZFS pool.
69*7a0c41d5SAlan Somers 	 *
70*7a0c41d5SAlan Somers 	 * \param pool  The ZFS pool to traverse/search.
71*7a0c41d5SAlan Somers 	 */
72*7a0c41d5SAlan Somers 	VdevIterator(zpool_handle_t *pool);
73*7a0c41d5SAlan Somers 
74*7a0c41d5SAlan Somers 	/**
75*7a0c41d5SAlan Somers 	 * \brief Instantiate a VdevIterator for the given ZFS pool.
76*7a0c41d5SAlan Somers 	 *
77*7a0c41d5SAlan Somers 	 * \param poolConfig  The configuration data for the ZFS pool
78*7a0c41d5SAlan Somers 	 *                    to traverse/search.
79*7a0c41d5SAlan Somers 	 */
80*7a0c41d5SAlan Somers 	VdevIterator(nvlist_t *poolConfig);
81*7a0c41d5SAlan Somers 
82*7a0c41d5SAlan Somers 	/**
83*7a0c41d5SAlan Somers 	 * \brief Reset this iterator's cursor so that Next() will
84*7a0c41d5SAlan Somers 	 *        report the first member of the pool.
85*7a0c41d5SAlan Somers 	 */
86*7a0c41d5SAlan Somers 	void      Reset();
87*7a0c41d5SAlan Somers 
88*7a0c41d5SAlan Somers 	/**
89*7a0c41d5SAlan Somers 	 * \brief Report the leaf vdev at this iterator's cursor and increment
90*7a0c41d5SAlan Somers 	 *        the cursor to the next leaf pool member.
91*7a0c41d5SAlan Somers 	 */
92*7a0c41d5SAlan Somers 	nvlist_t *Next();
93*7a0c41d5SAlan Somers 
94*7a0c41d5SAlan Somers 	/**
95*7a0c41d5SAlan Somers 	 * \brief Traverse the entire pool configuration starting its
96*7a0c41d5SAlan Somers 	 *        first member, returning a vdev object with the given
97*7a0c41d5SAlan Somers 	 *        vdev GUID if found.
98*7a0c41d5SAlan Somers 	 *
99*7a0c41d5SAlan Somers 	 * \param vdevGUID  The vdev GUID of the vdev object to find.
100*7a0c41d5SAlan Somers 	 *
101*7a0c41d5SAlan Somers 	 * \return  A Vdev object for the matching vdev if found.  Otherwise
102*7a0c41d5SAlan Somers 	 *          NULL.
103*7a0c41d5SAlan Somers 	 *
104*7a0c41d5SAlan Somers 	 * Upon return, the VdevIterator's cursor points to the vdev just
105*7a0c41d5SAlan Somers 	 * past the returned vdev or end() if no matching vdev is found.
106*7a0c41d5SAlan Somers 	 */
107*7a0c41d5SAlan Somers 	nvlist_t *Find(DevdCtl::Guid vdevGUID);
108*7a0c41d5SAlan Somers 
109*7a0c41d5SAlan Somers 	/**
110*7a0c41d5SAlan Somers 	 * \brief Perform the specified operation on each leaf member of
111*7a0c41d5SAlan Somers 	 *        a pool's vdev membership.
112*7a0c41d5SAlan Somers 	 *
113*7a0c41d5SAlan Somers 	 * \param cb     Callback function to execute for each member.
114*7a0c41d5SAlan Somers 	 * \param cbArg  Argument to pass to cb.
115*7a0c41d5SAlan Somers 	 */
116*7a0c41d5SAlan Somers 	void	  Each(VdevCallback_t *cb, void *cbArg);
117*7a0c41d5SAlan Somers 
118*7a0c41d5SAlan Somers private:
119*7a0c41d5SAlan Somers 	nvlist_t                *m_poolConfig;
120*7a0c41d5SAlan Somers 	std::list<nvlist_t *>	 m_vdevQueue;
121*7a0c41d5SAlan Somers };
122*7a0c41d5SAlan Somers 
123*7a0c41d5SAlan Somers #endif	/* _VDEV_ITERATOR_H_ */
124