2 * Copyright (c) 2011, 2012, 2013, 2014 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 * Class definitions and supporting data strutures for the ZFS fault
41 * Header requirements:
43 * #include <sys/fs/zfs.h>
51 * #include <devdctl/guid.h>
52 * #include <devdctl/event.h>
53 * #include <devdctl/event_factory.h>
54 * #include <devdctl/consumer.h>
56 * #include "vdev_iterator.h"
61 /*=========================== Forward Declarations ===========================*/
65 typedef struct zpool_handle zpool_handle_t;
68 typedef struct libzfs_handle libzfs_handle_t;
71 typedef struct nvlist nvlist_t;
73 typedef int LeafIterFunc(zpool_handle_t *, nvlist_t *, void *);
75 /*================================ Global Data ===============================*/
77 extern libzfs_handle_t *g_zfsHandle;
79 /*============================= Class Definitions ============================*/
80 /*--------------------------------- ZfsDaemon --------------------------------*/
82 * Static singleton orchestrating the operations of the ZFS daemon program.
84 class ZfsDaemon : public DevdCtl::Consumer
87 /** Return the ZfsDaemon singleton. */
88 static ZfsDaemon &Get();
91 * Used by signal handlers to ensure, in a race free way, that
92 * the event loop will perform at least one more full loop
93 * before sleeping again.
95 static void WakeEventLoop();
98 * Schedules a rescan of devices in the system for potential
99 * candidates to replace a missing vdev. The scan is performed
100 * during the next run of the event loop.
102 static void RequestSystemRescan();
104 /** Daemonize and perform all functions of the ZFS daemon. */
111 static VdevCallback_t VdevAddCaseFile;
113 /** Purge our cache of outstanding ZFS issues in the system. */
114 void PurgeCaseFiles();
116 /** Build a cache of outstanding ZFS issues in the system. */
117 void BuildCaseFiles();
120 * Iterate over all known issues and attempt to solve them
121 * given resources currently available in the system.
126 * Interrogate the system looking for previously unknown
127 * faults that occurred either before ZFSD was started,
128 * or during a period of lost communication with Devd.
130 void DetectMissedEvents();
133 * Wait for and process event source activity.
138 * Signal handler for which our response is to
139 * log the current state of the daemon.
141 * \param sigNum The signal caught.
143 static void InfoSignalHandler(int sigNum);
146 * Signal handler for which our response is to
147 * request a case rescan.
149 * \param sigNum The signal caught.
151 static void RescanSignalHandler(int sigNum);
154 * Signal handler for which our response is to
155 * gracefully terminate.
157 * \param sigNum The signal caught.
159 static void QuitSignalHandler(int sigNum);
162 * Open and lock our PID file.
164 static void OpenPIDFile();
167 * Update our PID file with our PID.
169 static void UpdatePIDFile();
172 * Close and release the lock on our PID file.
174 static void ClosePIDFile();
177 * Perform syslog configuration.
179 static void InitializeSyslog();
181 static ZfsDaemon *s_theZfsDaemon;
184 * Set to true when our program is signaled to
187 static bool s_logCaseFiles;
190 * Set to true when our program is signaled to
193 static bool s_terminateEventLoop;
196 * The canonical path and file name of zfsd's PID file.
198 static char s_pidFilePath[];
201 * Control structure for PIDFILE(3) API.
203 static pidfh *s_pidFH;
206 * Pipe file descriptors used to close races with our
209 static int s_signalPipeFD[2];
212 * Flag controlling a rescan from ZFSD's event loop of all
213 * GEOM providers in the system to find candidates for solving
216 static bool s_systemRescanRequested;
219 * Flag controlling whether events can be queued. This boolean
220 * is set during event replay to ensure that events for pools or
221 * devices no longer in the system are not retained forever.
223 static bool s_consumingEvents;
225 static DevdCtl::EventFactory::Record s_registryEntries[];
228 #endif /* _ZFSD_H_ */