2 * Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016 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)
36 * The ZFS daemon consumes kernel devdctl(4) event data via devd(8)'s
37 * unix domain socket in order to react to system changes that impact
38 * the function of ZFS storage pools. The goal of this daemon is to
39 * provide similar functionality to the Solaris ZFS Diagnostic Engine
40 * (zfs-diagnosis), the Solaris ZFS fault handler (zfs-retire), and
41 * the Solaris ZFS vdev insertion agent (zfs-mod sysevent handler).
44 #include <sys/cdefs.h>
45 #include <sys/param.h>
46 #include <sys/fs/zfs.h>
60 #include <devdctl/guid.h>
61 #include <devdctl/event.h>
62 #include <devdctl/event_factory.h>
63 #include <devdctl/exception.h>
64 #include <devdctl/consumer.h>
67 #include "vdev_iterator.h"
68 #include "zfsd_event.h"
69 #include "case_file.h"
71 #include "vdev_iterator.h"
73 #include "zfsd_exception.h"
74 #include "zpool_list.h"
76 __FBSDID("$FreeBSD$");
78 /*================================== Macros ==================================*/
79 #define NUM_ELEMENTS(x) (sizeof(x) / sizeof(*x))
81 /*============================ Namespace Control =============================*/
83 using DevdCtl::EventFactory;
84 using DevdCtl::EventList;
86 /*================================ Global Data ===============================*/
88 libzfs_handle_t *g_zfsHandle;
90 /*--------------------------------- ZfsDaemon --------------------------------*/
91 //- ZfsDaemon Static Private Data ----------------------------------------------
92 ZfsDaemon *ZfsDaemon::s_theZfsDaemon;
93 bool ZfsDaemon::s_logCaseFiles;
94 bool ZfsDaemon::s_terminateEventLoop;
95 char ZfsDaemon::s_pidFilePath[] = "/var/run/zfsd.pid";
96 pidfh *ZfsDaemon::s_pidFH;
97 int ZfsDaemon::s_signalPipeFD[2];
98 bool ZfsDaemon::s_systemRescanRequested(false);
99 EventFactory::Record ZfsDaemon::s_registryEntries[] =
101 { Event::NOTIFY, "DEVFS", &DevfsEvent::Builder },
102 { Event::NOTIFY, "GEOM", &GeomEvent::Builder },
103 { Event::NOTIFY, "ZFS", &ZfsEvent::Builder }
106 //- ZfsDaemon Static Public Methods --------------------------------------------
110 return (*s_theZfsDaemon);
114 ZfsDaemon::WakeEventLoop()
116 write(s_signalPipeFD[1], "+", 1);
120 ZfsDaemon::RequestSystemRescan()
122 s_systemRescanRequested = true;
123 ZfsDaemon::WakeEventLoop();
131 while (s_terminateEventLoop == false) {
134 daemon.DisconnectFromDevd();
136 if (daemon.ConnectToDevd() == false) {
141 daemon.DetectMissedEvents();
145 } catch (const DevdCtl::Exception &exp) {
150 daemon.DisconnectFromDevd();
153 //- ZfsDaemon Private Methods --------------------------------------------------
154 ZfsDaemon::ZfsDaemon()
155 : Consumer(/*defBuilder*/NULL, s_registryEntries,
156 NUM_ELEMENTS(s_registryEntries))
158 if (s_theZfsDaemon != NULL)
159 errx(1, "Multiple ZfsDaemon instances created. Exiting");
161 s_theZfsDaemon = this;
163 if (pipe(s_signalPipeFD) != 0)
164 errx(1, "Unable to allocate signal pipe. Exiting");
166 if (fcntl(s_signalPipeFD[0], F_SETFL, O_NONBLOCK) == -1)
167 errx(1, "Unable to set pipe as non-blocking. Exiting");
169 if (fcntl(s_signalPipeFD[1], F_SETFL, O_NONBLOCK) == -1)
170 errx(1, "Unable to set pipe as non-blocking. Exiting");
172 signal(SIGHUP, ZfsDaemon::RescanSignalHandler);
173 signal(SIGINFO, ZfsDaemon::InfoSignalHandler);
174 signal(SIGINT, ZfsDaemon::QuitSignalHandler);
175 signal(SIGTERM, ZfsDaemon::QuitSignalHandler);
176 signal(SIGUSR1, ZfsDaemon::RescanSignalHandler);
178 g_zfsHandle = libzfs_init();
179 if (g_zfsHandle == NULL)
180 errx(1, "Unable to initialize ZFS library. Exiting");
192 ZfsDaemon::~ZfsDaemon()
199 ZfsDaemon::PurgeCaseFiles()
201 CaseFile::PurgeAll();
205 ZfsDaemon::VdevAddCaseFile(Vdev &vdev, void *cbArg)
207 if (vdev.State() != VDEV_STATE_HEALTHY)
208 CaseFile::Create(vdev);
210 return (/*break early*/false);
214 ZfsDaemon::BuildCaseFiles()
217 ZpoolList::iterator pool;
219 /* Add CaseFiles for vdevs with issues. */
220 for (pool = zpl.begin(); pool != zpl.end(); pool++)
221 VdevIterator(*pool).Each(VdevAddCaseFile, NULL);
223 /* De-serialize any saved cases. */
224 CaseFile::DeSerialize();
226 /* Simulate config_sync events to force CaseFile reevaluation */
227 for (pool = zpl.begin(); pool != zpl.end(); pool++) {
232 const char *poolname;
234 poolname = zpool_get_name(*pool);
235 config = zpool_get_config(*pool, NULL);
236 if (config == NULL) {
237 syslog(LOG_ERR, "ZFSDaemon::BuildCaseFiles: Could not "
238 "find pool config for pool %s", poolname);
241 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
243 syslog(LOG_ERR, "ZFSDaemon::BuildCaseFiles: Could not "
244 "find pool guid for pool %s", poolname);
249 snprintf(evString, 160, "!system=ZFS subsystem=ZFS "
250 "type=misc.fs.zfs.config_sync sub_type=synthesized "
251 "pool_name=%s pool_guid=%" PRIu64 "\n", poolname, poolGUID);
252 event = Event::CreateEvent(GetFactory(), string(evString));
261 ZfsDaemon::RescanSystem()
266 struct gprovider *pp;
270 * The devdctl system doesn't replay events for new consumers
271 * of the interface. Emit manufactured DEVFS arrival events
272 * for any devices that already before we started or during
273 * periods where we've lost our connection to devd.
275 result = geom_gettree(&mesh);
277 syslog(LOG_ERR, "ZfsDaemon::RescanSystem: "
278 "geom_gettree faild with error %d\n", result);
282 const string evStart("!system=DEVFS subsystem=CDEV type=CREATE "
283 "sub_type=synthesized cdev=");
284 LIST_FOREACH(mp, &mesh.lg_class, lg_class) {
285 LIST_FOREACH(gp, &mp->lg_geom, lg_geom) {
286 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
289 string evString(evStart + pp->lg_name + "\n");
290 event = Event::CreateEvent(GetFactory(),
293 if (event->Process())
300 geom_deletetree(&mesh);
304 ZfsDaemon::DetectMissedEvents()
310 * Discard any events waiting for us. We don't know
311 * if they still apply to the current state of the
319 * If the system state has changed during our
320 * interrogation, start over.
322 } while (s_terminateEventLoop == false && EventsPending());
328 ZfsDaemon::EventLoop()
330 while (s_terminateEventLoop == false) {
331 struct pollfd fds[2];
334 if (s_logCaseFiles == true) {
335 EventList::iterator event(m_unconsumedEvents.begin());
336 s_logCaseFiles = false;
338 while (event != m_unconsumedEvents.end())
339 (*event++)->Log(LOG_INFO);
342 Callout::ExpireCallouts();
345 fds[0].fd = m_devdSockFD;
346 fds[0].events = POLLIN;
348 fds[1].fd = s_signalPipeFD[0];
349 fds[1].events = POLLIN;
351 result = poll(fds, NUM_ELEMENTS(fds), /*timeout*/INFTIM);
356 err(1, "Polling for devd events failed");
357 } else if (result == 0) {
358 errx(1, "Unexpected result of 0 from poll. Exiting");
361 if ((fds[0].revents & POLLIN) != 0)
364 if ((fds[1].revents & POLLIN) != 0) {
365 static char discardBuf[128];
368 * This pipe exists just to close the signal
369 * race. Its contents are of no interest to
370 * us, but we must ensure that future signals
371 * have space in the pipe to write.
373 while (read(s_signalPipeFD[0], discardBuf,
374 sizeof(discardBuf)) > 0)
378 if (s_systemRescanRequested == true) {
379 s_systemRescanRequested = false;
380 syslog(LOG_INFO, "System Rescan request processed.");
384 if ((fds[0].revents & POLLERR) != 0) {
385 syslog(LOG_INFO, "POLLERROR detected on devd socket.");
389 if ((fds[0].revents & POLLHUP) != 0) {
390 syslog(LOG_INFO, "POLLHUP detected on devd socket.");
395 //- ZfsDaemon staic Private Methods --------------------------------------------
397 ZfsDaemon::InfoSignalHandler(int)
399 s_logCaseFiles = true;
400 ZfsDaemon::WakeEventLoop();
404 ZfsDaemon::RescanSignalHandler(int)
406 RequestSystemRescan();
410 ZfsDaemon::QuitSignalHandler(int)
412 s_terminateEventLoop = true;
413 ZfsDaemon::WakeEventLoop();
417 ZfsDaemon::OpenPIDFile()
421 s_pidFH = pidfile_open(s_pidFilePath, 0600, &otherPID);
422 if (s_pidFH == NULL) {
424 errx(1, "already running as PID %d. Exiting", otherPID);
425 warn("cannot open PID file");
430 ZfsDaemon::UpdatePIDFile()
433 pidfile_write(s_pidFH);
437 ZfsDaemon::ClosePIDFile()
440 pidfile_close(s_pidFH);
444 ZfsDaemon::InitializeSyslog()
446 openlog("zfsd", LOG_NDELAY, LOG_DAEMON);