xref: /freebsd/cddl/usr.sbin/zfsd/vdev.h (revision d8a0fe102c0cfdfcd5b818f850eff09d8536c9bc)
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.h
37  *
38  * Definition of the Vdev class.
39  *
40  * Header requirements:
41  *
42  *    #include <string>
43  *    #include <list>
44  *
45  *    #include <devdctl/guid.h>
46  */
47 #ifndef	_VDEV_H_
48 #define	_VDEV_H_
49 
50 /*=========================== Forward Declarations ===========================*/
51 struct zpool_handle;
52 typedef struct zpool_handle zpool_handle_t;
53 
54 struct nvlist;
55 typedef struct nvlist nvlist_t;
56 
57 /*============================= Class Definitions ============================*/
58 /*----------------------------------- Vdev -----------------------------------*/
59 /**
60  * \brief Wrapper class for a vdev's name/value configuration list
61  *        simplifying access to commonly used vdev attributes.
62  */
63 class Vdev
64 {
65 public:
66 	/**
67 	 * \brief Instantiate a vdev object for a vdev that is a member
68 	 *        of an imported pool.
69 	 *
70 	 * \param pool        The pool object containing the vdev with
71 	 *                    configuration data provided in vdevConfig.
72 	 * \param vdevConfig  Vdev configuration data.
73 	 *
74 	 * This method should be used whenever dealing with vdev's
75 	 * enumerated via the ZpoolList class.  The in-core configuration
76 	 * data for a vdev does not contain all of the items found in
77 	 * the on-disk label.  This requires the vdev class to augment
78 	 * the data in vdevConfig with data found in the pool object.
79 	 */
80 	Vdev(zpool_handle_t *pool, nvlist_t *vdevConfig);
81 
82 	/**
83 	 * \brief Instantiate a vdev object for a vdev that is a member
84 	 *        of a pool configuration.
85 	 *
86 	 * \param poolConfig  The pool configuration containing the vdev
87 	 *                    configuration data provided in vdevConfig.
88 	 * \param vdevConfig  Vdev configuration data.
89 	 *
90 	 * This method should be used whenever dealing with vdev's
91 	 * enumerated via the ZpoolList class.  The in-core configuration
92 	 * data for a vdev does not contain all of the items found in
93 	 * the on-disk label.  This requires the vdev class to augment
94 	 * the data in vdevConfig with data found in the pool object.
95 	 */
96 	Vdev(nvlist_t *poolConfig, nvlist_t *vdevConfig);
97 
98 	/**
99 	 * \brief Instantiate a vdev object from a ZFS label stored on
100 	 *        the device.
101 	 *
102 	 * \param vdevConfig  The name/value list retrieved by reading
103 	 *                    the label information on a leaf vdev.
104 	 */
105 	Vdev(nvlist_t *vdevConfig);
106 
107 	/**
108 	 * \brief No-op copy constructor for nonexistent vdevs.
109 	 */
110 	Vdev();
111 
112 	/**
113 	 * \brief No-op virtual destructor, since this class has virtual
114 	 *        functions.
115 	 */
116 	virtual ~Vdev();
117 	bool			DoesNotExist()	const;
118 
119 	/**
120 	 * \brief Return a list of the vdev's children.
121 	 */
122 	std::list<Vdev>		 Children();
123 
124 	virtual DevdCtl::Guid	 GUID()		const;
125 	bool			 IsSpare()	const;
126 	virtual DevdCtl::Guid	 PoolGUID()	const;
127 	virtual vdev_state	 State()	const;
128 	std::string		 Path()		const;
129 	virtual std::string	 PhysicalPath()	const;
130 	std::string		 GUIDString()	const;
131 	nvlist_t		*PoolConfig()	const;
132 	nvlist_t		*Config()	const;
133 	Vdev			 Parent();
134 	Vdev			 RootVdev();
135 	std::string		 Name(zpool_handle_t *, bool verbose)	const;
136 	bool			 IsSpare();
137 	bool			 IsAvailableSpare()	const;
138 	bool			 IsActiveSpare()	const;
139 	bool			 IsResilvering()	const;
140 
141 private:
142 	void			 VdevLookupGuid();
143 	bool			 VdevLookupPoolGuid();
144 	DevdCtl::Guid		 m_poolGUID;
145 	DevdCtl::Guid		 m_vdevGUID;
146 	nvlist_t		*m_poolConfig;
147 	nvlist_t		*m_config;
148 };
149 
150 //- Special objects -----------------------------------------------------------
151 extern Vdev NonexistentVdev;
152 
153 //- Vdev Inline Public Methods ------------------------------------------------
154 inline Vdev::~Vdev()
155 {
156 }
157 
158 inline DevdCtl::Guid
159 Vdev::PoolGUID() const
160 {
161 	return (m_poolGUID);
162 }
163 
164 inline DevdCtl::Guid
165 Vdev::GUID() const
166 {
167 	return (m_vdevGUID);
168 }
169 
170 inline nvlist_t *
171 Vdev::PoolConfig() const
172 {
173 	return (m_poolConfig);
174 }
175 
176 inline nvlist_t *
177 Vdev::Config() const
178 {
179 	return (m_config);
180 }
181 
182 inline bool
183 Vdev::DoesNotExist() const
184 {
185 	return (m_config == NULL);
186 }
187 
188 #endif /* _VDEV_H_ */
189