xref: /freebsd/cddl/usr.sbin/zfsd/vdev_iterator.h (revision e6bfd18d21b225af6a0ed67ceeaf1293b7b9eba5)
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.h
37  *
38  * VdevIterator class definition.
39  *
40  * Header requirements:
41  *
42  *    #include <list>
43  */
44 #ifndef	_VDEV_ITERATOR_H_
45 #define	_VDEV_ITERATOR_H_
46 
47 /*=========================== Forward Declarations ===========================*/
48 struct zpool_handle;
49 typedef struct zpool_handle zpool_handle_t;
50 
51 struct nvlist;
52 typedef struct nvlist nvlist_t;
53 
54 class Vdev;
55 
56 /*============================= Class Definitions ============================*/
57 /*------------------------------- VdevIterator -------------------------------*/
58 typedef bool VdevCallback_t(Vdev &vdev, void *cbArg);
59 
60 /**
61  * \brief VdevIterator provides mechanisms for traversing and searching
62  *        the leaf vdevs contained in a ZFS pool configuration.
63  */
64 class VdevIterator
65 {
66 public:
67 	/**
68 	 * \brief Instantiate a VdevIterator for the given ZFS pool.
69 	 *
70 	 * \param pool  The ZFS pool to traverse/search.
71 	 */
72 	VdevIterator(zpool_handle_t *pool);
73 
74 	/**
75 	 * \brief Instantiate a VdevIterator for the given ZFS pool.
76 	 *
77 	 * \param poolConfig  The configuration data for the ZFS pool
78 	 *                    to traverse/search.
79 	 */
80 	VdevIterator(nvlist_t *poolConfig);
81 
82 	/**
83 	 * \brief Reset this iterator's cursor so that Next() will
84 	 *        report the first member of the pool.
85 	 */
86 	void      Reset();
87 
88 	/**
89 	 * \brief Report the leaf vdev at this iterator's cursor and increment
90 	 *        the cursor to the next leaf pool member.
91 	 */
92 	nvlist_t *Next();
93 
94 	/**
95 	 * \brief Traverse the entire pool configuration starting its
96 	 *        first member, returning a vdev object with the given
97 	 *        vdev GUID if found.
98 	 *
99 	 * \param vdevGUID  The vdev GUID of the vdev object to find.
100 	 *
101 	 * \return  A Vdev object for the matching vdev if found.  Otherwise
102 	 *          NULL.
103 	 *
104 	 * Upon return, the VdevIterator's cursor points to the vdev just
105 	 * past the returned vdev or end() if no matching vdev is found.
106 	 */
107 	nvlist_t *Find(DevdCtl::Guid vdevGUID);
108 
109 	/**
110 	 * \brief Perform the specified operation on each leaf member of
111 	 *        a pool's vdev membership.
112 	 *
113 	 * \param cb     Callback function to execute for each member.
114 	 * \param cbArg  Argument to pass to cb.
115 	 */
116 	void	  Each(VdevCallback_t *cb, void *cbArg);
117 
118 private:
119 	nvlist_t                *m_poolConfig;
120 	std::list<nvlist_t *>	 m_vdevQueue;
121 };
122 
123 #endif	/* _VDEV_ITERATOR_H_ */
124