xref: /freebsd/cddl/usr.sbin/zfsd/vdev.h (revision b5864e6de2f3aa8eb9bb269ec86282598b5201b1)
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 	bool			DoesNotExist()	const;
112 
113 	/**
114 	 * \brief Return a list of the vdev's children.
115 	 */
116 	std::list<Vdev>		 Children();
117 
118 	virtual DevdCtl::Guid	 GUID()		const;
119 	bool			 IsSpare()	const;
120 	virtual DevdCtl::Guid	 PoolGUID()	const;
121 	virtual vdev_state	 State()	const;
122 	std::string		 Path()		const;
123 	virtual std::string	 PhysicalPath()	const;
124 	std::string		 GUIDString()	const;
125 	nvlist_t		*PoolConfig()	const;
126 	nvlist_t		*Config()	const;
127 	Vdev			 Parent();
128 	Vdev			 RootVdev();
129 	std::string		 Name(zpool_handle_t *, bool verbose)	const;
130 	bool			 IsSpare();
131 	bool			 IsAvailableSpare()	const;
132 	bool			 IsActiveSpare()	const;
133 	bool			 IsResilvering()	const;
134 
135 private:
136 	void			 VdevLookupGuid();
137 	bool			 VdevLookupPoolGuid();
138 	DevdCtl::Guid		 m_poolGUID;
139 	DevdCtl::Guid		 m_vdevGUID;
140 	nvlist_t		*m_poolConfig;
141 	nvlist_t		*m_config;
142 };
143 
144 //- Special objects -----------------------------------------------------------
145 extern Vdev NonexistentVdev;
146 
147 //- Vdev Inline Public Methods ------------------------------------------------
148 inline DevdCtl::Guid
149 Vdev::PoolGUID() const
150 {
151 	return (m_poolGUID);
152 }
153 
154 inline DevdCtl::Guid
155 Vdev::GUID() const
156 {
157 	return (m_vdevGUID);
158 }
159 
160 inline nvlist_t *
161 Vdev::PoolConfig() const
162 {
163 	return (m_poolConfig);
164 }
165 
166 inline nvlist_t *
167 Vdev::Config() const
168 {
169 	return (m_config);
170 }
171 
172 inline bool
173 Vdev::DoesNotExist() const
174 {
175 	return (m_config == NULL);
176 }
177 
178 #endif /* _VDEV_H_ */
179