2 * Copyright (c) 2011, 2012, 2013 Spectra Logic Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
30 * Authors: Justin T. Gibbs (Spectra Logic Corporation)
38 * CaseFile objects aggregate vdev faults that may require ZFSD action
39 * in order to maintain the health of a ZFS pool.
41 * Header requirements:
45 * #include "callout.h"
46 * #include "zfsd_event.h"
51 /*=========================== Forward Declarations ===========================*/
55 /*============================= Class Definitions ============================*/
56 /*------------------------------- CaseFileList -------------------------------*/
58 * CaseFileList is a specialization of the standard list STL container.
60 typedef std::list< CaseFile *> CaseFileList;
62 /*--------------------------------- CaseFile ---------------------------------*/
64 * A CaseFile object is instantiated anytime a vdev for an active pool
65 * experiences an I/O error, is faulted by ZFS, or is determined to be
68 * A vdev may have at most one CaseFile.
70 * CaseFiles are retired when a vdev leaves an active pool configuration
71 * or an action is taken to resolve the issues recorded in the CaseFile.
73 * Logging a case against a vdev does not imply that an immediate action
74 * to resolve a fault is required or even desired. For example, a CaseFile
75 * must accumulate a number of I/O errors in order to flag a device as
78 * Vdev I/O errors are not recorded in ZFS label inforamation. For this
79 * reasons, CaseFile%%s with accumulated I/O error events are serialized
80 * to the file system so that they survive across boots. Currently all
81 * other fault types can be reconstructed from ZFS label information, so
82 * CaseFile%%s for missing, faulted, or degradded members are just recreated
83 * at ZFSD startup instead of being deserialized from the file system.
89 * \brief Find a CaseFile object by a vdev's pool/vdev GUID tuple.
91 * \param poolGUID Pool GUID for the vdev of the CaseFile to find.
92 * If InvalidGuid, then only match the vdev GUID
93 * instead of both pool and vdev GUIDs.
94 * \param vdevGUID Vdev GUID for the vdev of the CaseFile to find.
96 * \return If found, a pointer to a valid CaseFile object.
99 static CaseFile *Find(DevdCtl::Guid poolGUID, DevdCtl::Guid vdevGUID);
102 * \brief Find a CaseFile object by a vdev's current/last known
105 * \param physPath Physical path of the vdev of the CaseFile to find.
107 * \return If found, a pointer to a valid CaseFile object.
110 static CaseFile *Find(const string &physPath);
113 * \brief ReEvaluate all open cases whose pool guid matches the argument
115 * \param poolGUID Only reevaluate cases for this pool
116 * \param event Try to consume this event with the casefile
118 static void ReEvaluateByGuid(DevdCtl::Guid poolGUID,
119 const ZfsEvent &event);
122 * \brief Create or return an existing active CaseFile for the
125 * \param vdev The vdev object for which to find/create a CaseFile.
127 * \return A reference to a valid CaseFile object.
129 static CaseFile &Create(Vdev &vdev);
132 * \brief Deserialize all serialized CaseFile objects found in
135 static void DeSerialize();
138 * \brief Emit syslog data on all active CaseFile%%s in the system.
140 static void LogAll();
143 * \brief Destroy the in-core cache of CaseFile data.
145 * This routine does not disturb the on disk, serialized, CaseFile
148 static void PurgeAll();
150 DevdCtl::Guid PoolGUID() const;
151 DevdCtl::Guid VdevGUID() const;
152 vdev_state VdevState() const;
153 const string &PoolGUIDString() const;
154 const string &VdevGUIDString() const;
155 const string &PhysicalPath() const;
158 * \brief Attempt to resolve this CaseFile using the disk
159 * resource at the given device/physical path/vdev object
162 * \param devPath The devfs path for the disk resource.
163 * \param physPath The physical path information reported by
165 * \param vdev If the disk contains ZFS label information,
166 * a pointer to the disk label's vdev object
167 * data. Otherwise NULL.
169 * \return True if this event was consumed by this CaseFile.
171 bool ReEvaluate(const string &devPath, const string &physPath,
175 * \brief Update this CaseFile in light of the provided ZfsEvent.
177 * Must be virtual so it can be overridden in the unit tests
179 * \param event The ZfsEvent to evaluate.
181 * \return True if this event was consumed by this CaseFile.
183 virtual bool ReEvaluate(const ZfsEvent &event);
186 * \brief Register an itimer callout for the given event, if necessary
188 virtual void RegisterCallout(const DevdCtl::Event &event);
191 * \brief Close a case if it is no longer relevant.
193 * This method deals with cases tracking soft errors. Soft errors
194 * will be discarded should a remove event occur within a short period
195 * of the soft errors being reported. We also discard the events
196 * if the vdev is marked degraded or failed.
198 * \return True if the case is closed. False otherwise.
200 bool CloseIfSolved();
203 * \brief Emit data about this CaseFile via syslog(3).
208 * \brief Whether we should degrade this vdev
210 bool ShouldDegrade() const;
213 * \brief Whether we should fault this vdev
215 bool ShouldFault() const;
220 * The number of soft errors on a vdev required
221 * to transition a vdev from healthy to degraded
224 ZFS_DEGRADE_IO_COUNT = 50
227 static CalloutFunc_t OnGracePeriodEnded;
230 * \brief scandir(3) filter function used to find files containing
231 * serialized CaseFile data.
233 * \param dirEntry Directory entry for the file to filter.
235 * \return Non-zero for a file to include in the selection,
238 static int DeSerializeSelector(const struct dirent *dirEntry);
241 * \brief Given the name of a file containing serialized events from a
242 * CaseFile object, create/update an in-core CaseFile object
243 * representing the serialized data.
245 * \param fileName The name of a file containing serialized events
246 * from a CaseFile object.
248 static void DeSerializeFile(const char *fileName);
251 CaseFile(const Vdev &vdev);
255 * Must be virtual so it can be subclassed in the unit tests
260 * \brief Reload state for the vdev associated with this CaseFile.
262 * \return True if the refresh was successful. False if the system
263 * has no record of the pool or vdev for this CaseFile.
265 virtual bool RefreshVdevState();
268 * \brief Free all events in the m_events list.
273 * \brief Free all events in the m_tentativeEvents list.
275 void PurgeTentativeEvents();
278 * \brief Commit to file system storage.
283 * \brief Retrieve event data from a serialization stream.
285 * \param caseStream The serializtion stream to parse.
287 void DeSerialize(std::ifstream &caseStream);
290 * \brief Serializes the supplied event list and writes it to fd
292 * \param prefix If not NULL, this prefix will be prepended to
293 * every event in the file.
295 void SerializeEvList(const DevdCtl::EventList events, int fd,
296 const char* prefix=NULL) const;
299 * \brief Unconditionally close a CaseFile.
301 virtual void Close();
304 * \brief Callout callback invoked when the remove timer grace
307 * If no remove events are received prior to the grace period
308 * firing, then any tentative events are promoted and counted
309 * against the health of the vdev.
311 void OnGracePeriodEnded();
314 * \brief Attempt to activate a spare on this case's pool.
316 * Call this whenever a pool becomes degraded. It will look for any
317 * spare devices and activate one to replace the casefile's vdev. It
318 * will _not_ close the casefile; that should only happen when the
319 * missing drive is replaced or the user promotes the spare.
321 * \return True if a spare was activated
323 bool ActivateSpare();
326 * \brief replace a pool's vdev with another
328 * \param vdev_type The type of the new vdev. Usually either
329 * VDEV_TYPE_DISK or VDEV_TYPE_FILE
330 * \param path The file system path to the new vdev
331 * \param isspare Whether the new vdev is a spare
333 * \return true iff the replacement was successful
335 bool Replace(const char* vdev_type, const char* path, bool isspare);
338 * \brief Which vdev, if any, is replacing ours.
340 * \param zhp Pool handle state from the caller context
342 * \return the vdev that is currently replacing ours,
343 * or NonexistentVdev if there isn't one.
345 Vdev BeingReplacedBy(zpool_handle_t *zhp);
348 * \brief All CaseFiles being tracked by ZFSD.
350 static CaseFileList s_activeCases;
353 * \brief The file system path to serialized CaseFile data.
355 static const string s_caseFilePath;
358 * \brief The time ZFSD waits before promoting a tentative event
359 * into a permanent event.
361 static const timeval s_removeGracePeriod;
364 * \brief A list of soft error events counted against the health of
367 DevdCtl::EventList m_events;
370 * \brief A list of soft error events waiting for a grace period
371 * expiration before being counted against the health of
374 DevdCtl::EventList m_tentativeEvents;
376 DevdCtl::Guid m_poolGUID;
377 DevdCtl::Guid m_vdevGUID;
378 vdev_state m_vdevState;
379 string m_poolGUIDString;
380 string m_vdevGUIDString;
381 string m_vdevPhysPath;
384 * \brief Callout activated when a grace period
386 Callout m_tentativeTimer;
389 nvlist_t *CaseVdev(zpool_handle_t *zhp) const;
393 CaseFile::PoolGUID() const
399 CaseFile::VdevGUID() const
405 CaseFile::VdevState() const
407 return (m_vdevState);
410 inline const string &
411 CaseFile::PoolGUIDString() const
413 return (m_poolGUIDString);
416 inline const string &
417 CaseFile::VdevGUIDString() const
419 return (m_vdevGUIDString);
422 inline const string &
423 CaseFile::PhysicalPath() const
425 return (m_vdevPhysPath);
428 #endif /* _CASE_FILE_H_ */