Index: projects/zfsd/head/cddl/sbin/zfsd/zfsd.cc =================================================================== --- projects/zfsd/head/cddl/sbin/zfsd/zfsd.cc (revision 298703) +++ projects/zfsd/head/cddl/sbin/zfsd/zfsd.cc (revision 298704) @@ -1,447 +1,448 @@ /*- * Copyright (c) 2011, 2012, 2013 Spectra Logic Corporation * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions, and the following disclaimer, * without modification. * 2. Redistributions in binary form must reproduce at minimum a disclaimer * substantially similar to the "NO WARRANTY" disclaimer below * ("Disclaimer") and any redistribution must be conditioned upon * including a substantially similar Disclaimer requirement for further * binary redistribution. * * NO WARRANTY * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * Authors: Justin T. Gibbs (Spectra Logic Corporation) */ /** * \file zfsd.cc * * The ZFS daemon consumes kernel devdctl(4) event data via devd(8)'s * unix domain socket in order to react to system changes that impact * the function of ZFS storage pools. The goal of this daemon is to * provide similar functionality to the Solaris ZFS Diagnostic Engine * (zfs-diagnosis), the Solaris ZFS fault handler (zfs-retire), and * the Solaris ZFS vdev insertion agent (zfs-mod sysevent handler). */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "callout.h" #include "vdev_iterator.h" #include "zfsd_event.h" #include "case_file.h" #include "vdev.h" #include "vdev_iterator.h" #include "zfsd.h" #include "zfsd_exception.h" #include "zpool_list.h" __FBSDID("$FreeBSD$"); /*================================== Macros ==================================*/ #define NUM_ELEMENTS(x) (sizeof(x) / sizeof(*x)) /*============================ Namespace Control =============================*/ using DevdCtl::Event; using DevdCtl::EventFactory; using DevdCtl::EventList; /*================================ Global Data ===============================*/ int g_debug = 0; libzfs_handle_t *g_zfsHandle; /*--------------------------------- ZfsDaemon --------------------------------*/ //- ZfsDaemon Static Private Data ---------------------------------------------- ZfsDaemon *ZfsDaemon::s_theZfsDaemon; bool ZfsDaemon::s_logCaseFiles; bool ZfsDaemon::s_terminateEventLoop; char ZfsDaemon::s_pidFilePath[] = "/var/run/zfsd.pid"; pidfh *ZfsDaemon::s_pidFH; int ZfsDaemon::s_signalPipeFD[2]; bool ZfsDaemon::s_systemRescanRequested(false); EventFactory::Record ZfsDaemon::s_registryEntries[] = { { Event::NOTIFY, "DEVFS", &DevfsEvent::Builder }, + { Event::NOTIFY, "GEOM", &GeomEvent::Builder }, { Event::NOTIFY, "ZFS", &ZfsEvent::Builder } }; //- ZfsDaemon Static Public Methods -------------------------------------------- ZfsDaemon & ZfsDaemon::Get() { return (*s_theZfsDaemon); } void ZfsDaemon::WakeEventLoop() { write(s_signalPipeFD[1], "+", 1); } void ZfsDaemon::RequestSystemRescan() { s_systemRescanRequested = true; ZfsDaemon::WakeEventLoop(); } void ZfsDaemon::Run() { ZfsDaemon daemon; while (s_terminateEventLoop == false) { try { daemon.DisconnectFromDevd(); if (daemon.ConnectToDevd() == false) { sleep(30); continue; } daemon.DetectMissedEvents(); daemon.EventLoop(); } catch (const DevdCtl::Exception &exp) { exp.Log(); } } daemon.DisconnectFromDevd(); } //- ZfsDaemon Private Methods -------------------------------------------------- ZfsDaemon::ZfsDaemon() : Consumer(/*defBuilder*/NULL, s_registryEntries, NUM_ELEMENTS(s_registryEntries)) { if (s_theZfsDaemon != NULL) errx(1, "Multiple ZfsDaemon instances created. Exiting"); s_theZfsDaemon = this; if (pipe(s_signalPipeFD) != 0) errx(1, "Unable to allocate signal pipe. Exiting"); if (fcntl(s_signalPipeFD[0], F_SETFL, O_NONBLOCK) == -1) errx(1, "Unable to set pipe as non-blocking. Exiting"); if (fcntl(s_signalPipeFD[1], F_SETFL, O_NONBLOCK) == -1) errx(1, "Unable to set pipe as non-blocking. Exiting"); signal(SIGHUP, ZfsDaemon::RescanSignalHandler); signal(SIGINFO, ZfsDaemon::InfoSignalHandler); signal(SIGINT, ZfsDaemon::QuitSignalHandler); signal(SIGTERM, ZfsDaemon::QuitSignalHandler); signal(SIGUSR1, ZfsDaemon::RescanSignalHandler); g_zfsHandle = libzfs_init(); if (g_zfsHandle == NULL) errx(1, "Unable to initialize ZFS library. Exiting"); Callout::Init(); InitializeSyslog(); OpenPIDFile(); if (g_debug == 0) daemon(0, 0); UpdatePIDFile(); } ZfsDaemon::~ZfsDaemon() { PurgeCaseFiles(); ClosePIDFile(); } void ZfsDaemon::PurgeCaseFiles() { CaseFile::PurgeAll(); } bool ZfsDaemon::VdevAddCaseFile(Vdev &vdev, void *cbArg) { if (vdev.State() != VDEV_STATE_HEALTHY) CaseFile::Create(vdev); return (/*break early*/false); } void ZfsDaemon::BuildCaseFiles() { ZpoolList zpl; ZpoolList::iterator pool; /* Add CaseFiles for vdevs with issues. */ for (pool = zpl.begin(); pool != zpl.end(); pool++) VdevIterator(*pool).Each(VdevAddCaseFile, NULL); /* De-serialize any saved cases. */ CaseFile::DeSerialize(); /* Simulate config_sync events to force CaseFile reevaluation */ for (pool = zpl.begin(); pool != zpl.end(); pool++) { char evString[160]; Event *event; nvlist_t *config; uint64_t poolGUID; const char *poolname; poolname = zpool_get_name(*pool); config = zpool_get_config(*pool, NULL); if (config == NULL) { syslog(LOG_ERR, "ZFSDaemon::BuildCaseFiles: Could not " "find pool config for pool %s", poolname); continue; } if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &poolGUID) != 0) { syslog(LOG_ERR, "ZFSDaemon::BuildCaseFiles: Could not " "find pool guid for pool %s", poolname); continue; } snprintf(evString, 160, "!system=ZFS subsystem=ZFS " "type=misc.fs.zfs.config_sync sub_type=synthesized " "pool_name=%s pool_guid=%lu\n", poolname, poolGUID); event = Event::CreateEvent(GetFactory(), string(evString)); if (event != NULL) { event->Process(); delete event; } } } void ZfsDaemon::RescanSystem() { struct gmesh mesh; struct gclass *mp; struct ggeom *gp; struct gprovider *pp; int result; /* * The devdctl system doesn't replay events for new consumers * of the interface. Emit manufactured DEVFS arrival events * for any devices that already before we started or during * periods where we've lost our connection to devd. */ result = geom_gettree(&mesh); if (result != 0) { syslog(LOG_ERR, "ZfsDaemon::RescanSystem: " "geom_gettree faild with error %d\n", result); return; } const string evStart("!system=DEVFS subsystem=CDEV type=CREATE " "sub_type=synthesized cdev="); LIST_FOREACH(mp, &mesh.lg_class, lg_class) { LIST_FOREACH(gp, &mp->lg_geom, lg_geom) { LIST_FOREACH(pp, &gp->lg_provider, lg_provider) { Event *event; string evString(evStart + pp->lg_name + "\n"); event = Event::CreateEvent(GetFactory(), evString); if (event != NULL) { if (event->Process()) SaveEvent(*event); delete event; } } } } geom_deletetree(&mesh); } void ZfsDaemon::DetectMissedEvents() { do { PurgeCaseFiles(); /* * Discard any events waiting for us. We don't know * if they still apply to the current state of the * system. */ FlushEvents(); BuildCaseFiles(); /* * If the system state has changed durring our * interrogation, start over. */ } while (s_terminateEventLoop == false && EventsPending()); RescanSystem(); } void ZfsDaemon::EventLoop() { while (s_terminateEventLoop == false) { struct pollfd fds[2]; int result; if (s_logCaseFiles == true) { EventList::iterator event(m_unconsumedEvents.begin()); s_logCaseFiles = false; CaseFile::LogAll(); while (event != m_unconsumedEvents.end()) (*event++)->Log(LOG_INFO); } Callout::ExpireCallouts(); /* Wait for data. */ fds[0].fd = m_devdSockFD; fds[0].events = POLLIN; fds[0].revents = 0; fds[1].fd = s_signalPipeFD[0]; fds[1].events = POLLIN; fds[1].revents = 0; result = poll(fds, NUM_ELEMENTS(fds), /*timeout*/INFTIM); if (result == -1) { if (errno == EINTR) continue; else err(1, "Polling for devd events failed"); } else if (result == 0) { errx(1, "Unexpected result of 0 from poll. Exiting"); } if ((fds[0].revents & POLLIN) != 0) ProcessEvents(); if ((fds[1].revents & POLLIN) != 0) { static char discardBuf[128]; /* * This pipe exists just to close the signal * race. Its contents are of no interest to * us, but we must ensure that future signals * have space in the pipe to write. */ while (read(s_signalPipeFD[0], discardBuf, sizeof(discardBuf)) > 0) ; } if (s_systemRescanRequested == true) { s_systemRescanRequested = false; syslog(LOG_INFO, "System Rescan request processed."); RescanSystem(); } if ((fds[0].revents & POLLERR) != 0) { syslog(LOG_INFO, "POLLERROR detected on devd socket."); break; } if ((fds[0].revents & POLLHUP) != 0) { syslog(LOG_INFO, "POLLHUP detected on devd socket."); break; } } } //- ZfsDaemon staic Private Methods -------------------------------------------- void ZfsDaemon::InfoSignalHandler(int) { s_logCaseFiles = true; ZfsDaemon::WakeEventLoop(); } void ZfsDaemon::RescanSignalHandler(int) { RequestSystemRescan(); } void ZfsDaemon::QuitSignalHandler(int) { s_terminateEventLoop = true; ZfsDaemon::WakeEventLoop(); } void ZfsDaemon::OpenPIDFile() { pid_t otherPID; s_pidFH = pidfile_open(s_pidFilePath, 0600, &otherPID); if (s_pidFH == NULL) { if (errno == EEXIST) errx(1, "already running as PID %d. Exiting", otherPID); warn("cannot open PID file"); } } void ZfsDaemon::UpdatePIDFile() { if (s_pidFH != NULL) pidfile_write(s_pidFH); } void ZfsDaemon::ClosePIDFile() { if (s_pidFH != NULL) pidfile_close(s_pidFH); } void ZfsDaemon::InitializeSyslog() { openlog("zfsd", LOG_NDELAY, LOG_DAEMON); } Index: projects/zfsd/head/cddl/sbin/zfsd/zfsd_event.cc =================================================================== --- projects/zfsd/head/cddl/sbin/zfsd/zfsd_event.cc (revision 298703) +++ projects/zfsd/head/cddl/sbin/zfsd/zfsd_event.cc (revision 298704) @@ -1,457 +1,530 @@ /*- * Copyright (c) 2011 Spectra Logic Corporation * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions, and the following disclaimer, * without modification. * 2. Redistributions in binary form must reproduce at minimum a disclaimer * substantially similar to the "NO WARRANTY" disclaimer below * ("Disclaimer") and any redistribution must be conditioned upon * including a substantially similar Disclaimer requirement for further * binary redistribution. * * NO WARRANTY * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * Authors: Justin T. Gibbs (Spectra Logic Corporation) */ /** * \file zfsd_event.cc */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "callout.h" #include "vdev_iterator.h" #include "zfsd_event.h" #include "case_file.h" #include "vdev.h" #include "zfsd.h" #include "zfsd_exception.h" #include "zpool_list.h" __FBSDID("$FreeBSD$"); /*============================ Namespace Control =============================*/ using DevdCtl::Event; using DevdCtl::Guid; using DevdCtl::NVPairMap; using std::stringstream; /*=========================== Class Implementations ==========================*/ /*-------------------------------- DevfsEvent --------------------------------*/ //- DevfsEvent Static Public Methods ------------------------------------------- Event * DevfsEvent::Builder(Event::Type type, NVPairMap &nvPairs, const string &eventString) { return (new DevfsEvent(type, nvPairs, eventString)); } //- DevfsEvent Static Protected Methods ---------------------------------------- nvlist_t * DevfsEvent::ReadLabel(int devFd, bool &inUse, bool °raded) { pool_state_t poolState; char *poolName; boolean_t b_inuse; inUse = false; degraded = false; poolName = NULL; if (zpool_in_use(g_zfsHandle, devFd, &poolState, &poolName, &b_inuse) == 0) { nvlist_t *devLabel; inUse = b_inuse == B_TRUE; if (poolName != NULL) free(poolName); if (zpool_read_label(devFd, &devLabel) != 0 || devLabel == NULL) return (NULL); try { Vdev vdev(devLabel); degraded = vdev.State() != VDEV_STATE_HEALTHY; return (devLabel); } catch (ZfsdException &exp) { string devName = fdevname(devFd); string devPath = _PATH_DEV + devName; string context("DevfsEvent::ReadLabel: " + devPath + ": "); exp.GetString().insert(0, context); exp.Log(); } } return (NULL); } bool DevfsEvent::OnlineByLabel(const string &devPath, const string& physPath, nvlist_t *devConfig) { try { /* * A device with ZFS label information has been * inserted. If it matches a device for which we * have a case, see if we can solve that case. */ syslog(LOG_INFO, "Interrogating VDEV label for %s\n", devPath.c_str()); Vdev vdev(devConfig); CaseFile *caseFile(CaseFile::Find(vdev.PoolGUID(), vdev.GUID())); if (caseFile != NULL) return (caseFile->ReEvaluate(devPath, physPath, &vdev)); } catch (ZfsdException &exp) { string context("DevfsEvent::OnlineByLabel: " + devPath + ": "); exp.GetString().insert(0, context); exp.Log(); } return (false); } //- DevfsEvent Virtual Public Methods ------------------------------------------ Event * DevfsEvent::DeepCopy() const { return (new DevfsEvent(*this)); } bool DevfsEvent::Process() const { /* * We are only concerned with newly discovered * devices that can be ZFS vdevs. */ if (Value("type") != "CREATE" || !IsDiskDev()) return (false); /* Log the event since it is of interest. */ Log(LOG_INFO); string devPath; if (!DevPath(devPath)) return (false); int devFd(open(devPath.c_str(), O_RDONLY)); if (devFd == -1) return (false); bool inUse; bool degraded; nvlist_t *devLabel(ReadLabel(devFd, inUse, degraded)); string physPath; bool havePhysPath(PhysicalPath(physPath)); string devName; DevName(devName); close(devFd); if (inUse && devLabel != NULL) { OnlineByLabel(devPath, physPath, devLabel); } else if (degraded) { syslog(LOG_INFO, "%s is marked degraded. Ignoring " "as a replace by physical path candidate.\n", devName.c_str()); } else if (havePhysPath && IsWholeDev()) { /* * TODO: attempt to resolve events using every casefile * that matches this physpath */ CaseFile *caseFile(CaseFile::Find(physPath)); if (caseFile != NULL) { syslog(LOG_INFO, "Found CaseFile(%s:%s:%s) - ReEvaluating\n", caseFile->PoolGUIDString().c_str(), caseFile->VdevGUIDString().c_str(), zpool_state_to_name(caseFile->VdevState(), VDEV_AUX_NONE)); caseFile->ReEvaluate(devPath, physPath, /*vdev*/NULL); } } if (devLabel != NULL) nvlist_free(devLabel); return (false); } //- DevfsEvent Protected Methods ----------------------------------------------- DevfsEvent::DevfsEvent(Event::Type type, NVPairMap &nvpairs, const string &eventString) : DevdCtl::DevfsEvent(type, nvpairs, eventString) { } DevfsEvent::DevfsEvent(const DevfsEvent &src) : DevdCtl::DevfsEvent::DevfsEvent(src) { } +/*-------------------------------- GeomEvent --------------------------------*/ + +//- GeomEvent Static Public Methods ------------------------------------------- +Event * +GeomEvent::Builder(Event::Type type, + NVPairMap &nvPairs, + const string &eventString) +{ + return (new GeomEvent(type, nvPairs, eventString)); +} + +//- GeomEvent Virtual Public Methods ------------------------------------------ +Event * +GeomEvent::DeepCopy() const +{ + return (new GeomEvent(*this)); +} + +bool +GeomEvent::Process() const +{ + /* + * We are only concerned with physical path changes, because those can + * be used to satisfy autoreplace operations + */ + if (Value("type") != "GEOM::physpath" || !IsDiskDev()) + return (false); + + /* Log the event since it is of interest. */ + Log(LOG_INFO); + + string devPath; + if (!DevPath(devPath)) + return (false); + + string physPath; + bool havePhysPath(PhysicalPath(physPath)); + + string devName; + DevName(devName); + + if (havePhysPath) { + /* + * TODO: attempt to resolve events using every casefile + * that matches this physpath + */ + CaseFile *caseFile(CaseFile::Find(physPath)); + if (caseFile != NULL) { + syslog(LOG_INFO, + "Found CaseFile(%s:%s:%s) - ReEvaluating\n", + caseFile->PoolGUIDString().c_str(), + caseFile->VdevGUIDString().c_str(), + zpool_state_to_name(caseFile->VdevState(), + VDEV_AUX_NONE)); + caseFile->ReEvaluate(devPath, physPath, /*vdev*/NULL); + } + } + return (false); +} + +//- GeomEvent Protected Methods ----------------------------------------------- +GeomEvent::GeomEvent(Event::Type type, NVPairMap &nvpairs, + const string &eventString) + : DevdCtl::GeomEvent(type, nvpairs, eventString) +{ +} + +GeomEvent::GeomEvent(const GeomEvent &src) + : DevdCtl::GeomEvent::GeomEvent(src) +{ +} + + /*--------------------------------- ZfsEvent ---------------------------------*/ //- ZfsEvent Static Public Methods --------------------------------------------- DevdCtl::Event * ZfsEvent::Builder(Event::Type type, NVPairMap &nvpairs, const string &eventString) { return (new ZfsEvent(type, nvpairs, eventString)); } //- ZfsEvent Virtual Public Methods -------------------------------------------- Event * ZfsEvent::DeepCopy() const { return (new ZfsEvent(*this)); } bool ZfsEvent::Process() const { string logstr(""); if (!Contains("class") && !Contains("type")) { syslog(LOG_ERR, "ZfsEvent::Process: Missing class or type data."); return (false); } /* On config syncs, replay any queued events first. */ if (Value("type").find("misc.fs.zfs.config_sync") == 0) { /* * Even if saved events are unconsumed the second time * around, drop them. Any events that still can't be * consumed are probably referring to vdevs or pools that * no longer exist. */ ZfsDaemon::Get().ReplayUnconsumedEvents(/*discard*/true); CaseFile::ReEvaluateByGuid(PoolGUID(), *this); } if (Value("type").find("misc.fs.zfs.") == 0) { /* Configuration changes, resilver events, etc. */ ProcessPoolEvent(); return (false); } if (!Contains("pool_guid") || !Contains("vdev_guid")) { /* Only currently interested in Vdev related events. */ return (false); } CaseFile *caseFile(CaseFile::Find(PoolGUID(), VdevGUID())); if (caseFile != NULL) { Log(LOG_INFO); syslog(LOG_INFO, "Evaluating existing case file\n"); caseFile->ReEvaluate(*this); return (false); } /* Skip events that can't be handled. */ Guid poolGUID(PoolGUID()); /* If there are no replicas for a pool, then it's not manageable. */ if (Value("class").find("fs.zfs.vdev.no_replicas") == 0) { stringstream msg; msg << "No replicas available for pool " << poolGUID; msg << ", ignoring"; Log(LOG_INFO); syslog(LOG_INFO, "%s", msg.str().c_str()); return (false); } /* * Create a case file for this vdev, and have it * evaluate the event. */ ZpoolList zpl(ZpoolList::ZpoolByGUID, &poolGUID); if (zpl.empty()) { stringstream msg; int priority = LOG_INFO; msg << "ZfsEvent::Process: Event for unknown pool "; msg << poolGUID << " "; msg << "queued"; Log(LOG_INFO); syslog(priority, "%s", msg.str().c_str()); return (true); } nvlist_t *vdevConfig = VdevIterator(zpl.front()).Find(VdevGUID()); if (vdevConfig == NULL) { stringstream msg; int priority = LOG_INFO; msg << "ZfsEvent::Process: Event for unknown vdev "; msg << VdevGUID() << " "; msg << "queued"; Log(LOG_INFO); syslog(priority, "%s", msg.str().c_str()); return (true); } Vdev vdev(zpl.front(), vdevConfig); caseFile = &CaseFile::Create(vdev); if (caseFile->ReEvaluate(*this) == false) { stringstream msg; int priority = LOG_INFO; msg << "ZfsEvent::Process: Unconsumed event for vdev("; msg << zpool_get_name(zpl.front()) << ","; msg << vdev.GUID() << ") "; msg << "queued"; Log(LOG_INFO); syslog(priority, "%s", msg.str().c_str()); return (true); } return (false); } //- ZfsEvent Protected Methods ------------------------------------------------- ZfsEvent::ZfsEvent(Event::Type type, NVPairMap &nvpairs, const string &eventString) : DevdCtl::ZfsEvent(type, nvpairs, eventString) { } ZfsEvent::ZfsEvent(const ZfsEvent &src) : DevdCtl::ZfsEvent(src) { } /* * Sometimes the kernel won't detach a spare when it is no longer needed. This * can happen for example if a drive is removed, then either the pool is * exported or the machine is powered off, then the drive is reinserted, then * the machine is powered on or the pool is imported. ZFSD must detach these * spares itself. */ void ZfsEvent::CleanupSpares() const { Guid poolGUID(PoolGUID()); ZpoolList zpl(ZpoolList::ZpoolByGUID, &poolGUID); if (!zpl.empty()) { zpool_handle_t* hdl; hdl = zpl.front(); VdevIterator(hdl).Each(TryDetach, (void*)hdl); } } void ZfsEvent::ProcessPoolEvent() const { bool degradedDevice(false); /* The pool is destroyed. Discard any open cases */ if (Value("type") == "misc.fs.zfs.pool_destroy") { Log(LOG_INFO); CaseFile::ReEvaluateByGuid(PoolGUID(), *this); return; } CaseFile *caseFile(CaseFile::Find(PoolGUID(), VdevGUID())); if (caseFile != NULL) { if (caseFile->VdevState() != VDEV_STATE_UNKNOWN && caseFile->VdevState() < VDEV_STATE_HEALTHY) degradedDevice = true; Log(LOG_INFO); caseFile->ReEvaluate(*this); } else if (Value("type") == "misc.fs.zfs.resilver_finish") { /* * It's possible to get a resilver_finish event with no * corresponding casefile. For example, if a damaged pool were * exported, repaired, then reimported. */ Log(LOG_INFO); CleanupSpares(); } if (Value("type") == "misc.fs.zfs.vdev_remove" && degradedDevice == false) { /* See if any other cases can make use of this device. */ Log(LOG_INFO); ZfsDaemon::RequestSystemRescan(); } } bool ZfsEvent::TryDetach(Vdev &vdev, void *cbArg) { /* * Outline: * if this device is a spare, and its parent includes one healthy, * non-spare child, then detach this device. */ zpool_handle_t *hdl(static_cast(cbArg)); if (vdev.IsSpare()) { std::list siblings; std::list::iterator siblings_it; boolean_t cleanup = B_FALSE; Vdev parent = vdev.Parent(); siblings = parent.Children(); /* Determine whether the parent should be cleaned up */ for (siblings_it = siblings.begin(); siblings_it != siblings.end(); siblings_it++) { Vdev sibling = *siblings_it; if (!sibling.IsSpare() && sibling.State() == VDEV_STATE_HEALTHY) { cleanup = B_TRUE; break; } } if (cleanup) { syslog(LOG_INFO, "Detaching spare vdev %s from pool %s", vdev.Path().c_str(), zpool_get_name(hdl)); zpool_vdev_detach(hdl, vdev.Path().c_str()); } } /* Always return false, because there may be other spares to detach */ return (false); } Index: projects/zfsd/head/cddl/sbin/zfsd/zfsd_event.h =================================================================== --- projects/zfsd/head/cddl/sbin/zfsd/zfsd_event.h (revision 298703) +++ projects/zfsd/head/cddl/sbin/zfsd/zfsd_event.h (revision 298704) @@ -1,152 +1,168 @@ /*- * Copyright (c) 2011 Spectra Logic Corporation * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions, and the following disclaimer, * without modification. * 2. Redistributions in binary form must reproduce at minimum a disclaimer * substantially similar to the "NO WARRANTY" disclaimer below * ("Disclaimer") and any redistribution must be conditioned upon * including a substantially similar Disclaimer requirement for further * binary redistribution. * * NO WARRANTY * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * Authors: Justin T. Gibbs (Spectra Logic Corporation) * * $FreeBSD$ */ /** * \file dev_ctl_event.h * * \brief Class hierarchy used to express events received via * the devdctl API. * * Header requirements: * #include * #include * #include * * #include * #include */ #ifndef _ZFSD_EVENT_H_ #define _ZFSD_EVENT_H_ /*============================ Namespace Control =============================*/ using std::string; /*=========================== Forward Declarations ===========================*/ struct zpool_handle; typedef struct zpool_handle zpool_handle_t; struct nvlist; typedef struct nvlist nvlist_t; /*============================= Class Definitions ============================*/ /*-------------------------------- DevfsEvent --------------------------------*/ class DevfsEvent : public DevdCtl::DevfsEvent { public: /** Specialized DevdCtlEvent object factory for Devfs events. */ static BuildMethod Builder; virtual DevdCtl::Event *DeepCopy() const; /** * Interpret and perform any actions necessary to * consume the event. * \return True if this event should be queued for later reevaluation */ virtual bool Process() const; protected: /** * \brief Read and return label information for a device. * * \param devFd The device from which to read ZFS label information. * \param inUse The device is part of an active or potentially * active configuration. * \param degraded The device label indicates the vdev is not healthy. * * \return If label information is available, an nvlist describing * the vdev configuraiton found on the device specified by * devFd. Otherwise NULL. */ static nvlist_t *ReadLabel(int devFd, bool &inUse, bool °raded); /** * Attempt to match the ZFS labeled device at devPath with an active * CaseFile for a missing vdev. If a CaseFile is found, attempt * to re-integrate the device with its pool. * * \param devPath The devfs path to the potential leaf vdev. * \param physPath The physical path string reported by the device * at devPath. * \param devConfig The ZFS label information found on the device * at devPath. * * \return true if the event that caused the online action can * be considered consumed. */ static bool OnlineByLabel(const string &devPath, const string& physPath, nvlist_t *devConfig); /** DeepCopy Constructor. */ DevfsEvent(const DevfsEvent &src); /** Constructor */ DevfsEvent(Type, DevdCtl::NVPairMap &, const string &); }; /*--------------------------------- ZfsEvent ---------------------------------*/ class ZfsEvent : public DevdCtl::ZfsEvent { public: /** Specialized DevdCtlEvent object factory for ZFS events. */ static BuildMethod Builder; virtual DevdCtl::Event *DeepCopy() const; /** * Interpret and perform any actions necessary to * consume the event. * \return True if this event should be queued for later reevaluation */ virtual bool Process() const; protected: /** DeepCopy Constructor. */ ZfsEvent(const ZfsEvent &src); /** Constructor */ ZfsEvent(Type, DevdCtl::NVPairMap &, const string &); /** * Detach any spares that are no longer needed, but were not * automatically detached by the kernel */ virtual void CleanupSpares() const; virtual void ProcessPoolEvent() const; static VdevCallback_t TryDetach; }; +class GeomEvent : public DevdCtl::GeomEvent +{ +public: + static BuildMethod Builder; + + virtual DevdCtl::Event *DeepCopy() const; + + virtual bool Process() const; + +protected: + /** DeepCopy Constructor. */ + GeomEvent(const GeomEvent &src); + + /** Constructor */ + GeomEvent(Type, DevdCtl::NVPairMap &, const string &); +}; #endif /*_ZFSD_EVENT_H_ */ Index: projects/zfsd/head/lib/libdevdctl/event.cc =================================================================== --- projects/zfsd/head/lib/libdevdctl/event.cc (revision 298703) +++ projects/zfsd/head/lib/libdevdctl/event.cc (revision 298704) @@ -1,557 +1,603 @@ /*- - * Copyright (c) 2011, 2012, 2013 Spectra Logic Corporation + * Copyright (c) 2011, 2012, 2013, 2016 Spectra Logic Corporation * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions, and the following disclaimer, * without modification. * 2. Redistributions in binary form must reproduce at minimum a disclaimer * substantially similar to the "NO WARRANTY" disclaimer below * ("Disclaimer") and any redistribution must be conditioned upon * including a substantially similar Disclaimer requirement for further * binary redistribution. * * NO WARRANTY * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * Authors: Justin T. Gibbs (Spectra Logic Corporation) */ /** * \file event.cc * * Implementation of the class hierarchy used to express events * received via the devdctl API. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "guid.h" #include "event.h" #include "event_factory.h" #include "exception.h" __FBSDID("$FreeBSD$"); /*================================== Macros ==================================*/ #define NUM_ELEMENTS(x) (sizeof(x) / sizeof(*x)) /*============================ Namespace Control =============================*/ +using std::begin; using std::cout; +using std::end; using std::endl; using std::string; using std::stringstream; namespace DevdCtl { /*=========================== Class Implementations ==========================*/ /*----------------------------------- Event ----------------------------------*/ //- Event Static Protected Data ------------------------------------------------ const string Event::s_theEmptyString; Event::EventTypeRecord Event::s_typeTable[] = { { Event::NOTIFY, "Notify" }, { Event::NOMATCH, "No Driver Match" }, { Event::ATTACH, "Attach" }, { Event::DETACH, "Detach" } }; //- Event Static Public Methods ------------------------------------------------ Event * Event::Builder(Event::Type type, NVPairMap &nvPairs, const string &eventString) { return (new Event(type, nvPairs, eventString)); } Event * Event::CreateEvent(const EventFactory &factory, const string &eventString) { NVPairMap &nvpairs(*new NVPairMap); Type type(static_cast(eventString[0])); try { ParseEventString(type, eventString, nvpairs); } catch (const ParseException &exp) { if (exp.GetType() == ParseException::INVALID_FORMAT) exp.Log(); return (NULL); } /* * Allow entries in our table for events with no system specified. * These entries should specify the string "none". */ NVPairMap::iterator system_item(nvpairs.find("system")); if (system_item == nvpairs.end()) nvpairs["system"] = "none"; return (factory.Build(type, nvpairs, eventString)); } +bool +Event::DevName(std::string &name) const +{ + return (false); +} + +/* TODO: simplify this function with C++-11 methods */ +bool +Event::IsDiskDev() const +{ + static const char *diskDevNames[] = + { + "da", + "ada" + }; + const char **dName; + string devName; + + if (! DevName(devName)) + return false; + + size_t find_start = devName.rfind('/'); + if (find_start == string::npos) { + find_start = 0; + } else { + /* Just after the last '/'. */ + find_start++; + } + + for (dName = begin(diskDevNames); dName <= end(diskDevNames); dName++) { + + size_t loc(devName.find(*dName, find_start)); + if (loc == find_start) { + size_t prefixLen(strlen(*dName)); + + if (devName.length() - find_start >= prefixLen + && isdigit(devName[find_start + prefixLen])) + return (true); + } + } + + return (false); +} + const char * Event::TypeToString(Event::Type type) { EventTypeRecord *rec(s_typeTable); EventTypeRecord *lastRec(s_typeTable + NUM_ELEMENTS(s_typeTable) - 1); for (; rec <= lastRec; rec++) { if (rec->m_type == type) return (rec->m_typeName); } return ("Unknown"); } //- Event Public Methods ------------------------------------------------------- const string & Event::Value(const string &varName) const { NVPairMap::const_iterator item(m_nvPairs.find(varName)); if (item == m_nvPairs.end()) return (s_theEmptyString); return (item->second); } bool Event::Contains(const string &varName) const { return (m_nvPairs.find(varName) != m_nvPairs.end()); } string Event::ToString() const { stringstream result; NVPairMap::const_iterator devName(m_nvPairs.find("device-name")); if (devName != m_nvPairs.end()) result << devName->second << ": "; NVPairMap::const_iterator systemName(m_nvPairs.find("system")); if (systemName != m_nvPairs.end() && systemName->second != "none") result << systemName->second << ": "; result << TypeToString(GetType()) << ' '; for (NVPairMap::const_iterator curVar = m_nvPairs.begin(); curVar != m_nvPairs.end(); curVar++) { if (curVar == devName || curVar == systemName) continue; result << ' ' << curVar->first << "=" << curVar->second; } result << endl; return (result.str()); } void Event::Print() const { cout << ToString() << std::flush; } void Event::Log(int priority) const { syslog(priority, "%s", ToString().c_str()); } //- Event Virtual Public Methods ----------------------------------------------- Event::~Event() { delete &m_nvPairs; } Event * Event::DeepCopy() const { return (new Event(*this)); } bool Event::Process() const { return (false); } timeval Event::GetTimestamp() const { timeval tv_timestamp; struct tm tm_timestamp; if (!Contains("timestamp")) { throw Exception("Event contains no timestamp: %s", m_eventString.c_str()); } strptime(Value(string("timestamp")).c_str(), "%s", &tm_timestamp); tv_timestamp.tv_sec = mktime(&tm_timestamp); tv_timestamp.tv_usec = 0; return (tv_timestamp); } +bool +Event::DevPath(std::string &path) const +{ + string devName; + if (!DevName(devName)) + return (false); + + string devPath(_PATH_DEV + devName); + int devFd(open(devPath.c_str(), O_RDONLY)); + if (devFd == -1) + return (false); + + /* Normalize the device name in case the DEVFS event is for a link. */ + devName = fdevname(devFd); + path = _PATH_DEV + devName; + + close(devFd); + + return (true); +} + +bool +Event::PhysicalPath(std::string &path) const +{ + string devPath; + + if (!DevPath(devPath)) + return (false); + + int devFd(open(devPath.c_str(), O_RDONLY)); + if (devFd == -1) + return (false); + + char physPath[MAXPATHLEN]; + physPath[0] = '\0'; + bool result(ioctl(devFd, DIOCGPHYSPATH, physPath) == 0); + close(devFd); + if (result) + path = physPath; + return (result); +} + //- Event Protected Methods ---------------------------------------------------- Event::Event(Type type, NVPairMap &map, const string &eventString) : m_type(type), m_nvPairs(map), m_eventString(eventString) { } Event::Event(const Event &src) : m_type(src.m_type), m_nvPairs(*new NVPairMap(src.m_nvPairs)), m_eventString(src.m_eventString) { } void Event::ParseEventString(Event::Type type, const string &eventString, NVPairMap& nvpairs) { size_t start; size_t end; switch (type) { case ATTACH: case DETACH: /* * \ * at \ * on * * Handle all data that doesn't conform to the * "name=value" format, and let the generic parser * below handle the rest. * * Type is a single char. Skip it. */ start = 1; end = eventString.find_first_of(" \t\n", start); if (end == string::npos) throw ParseException(ParseException::INVALID_FORMAT, eventString, start); nvpairs["device-name"] = eventString.substr(start, end - start); start = eventString.find(" on ", end); if (end == string::npos) throw ParseException(ParseException::INVALID_FORMAT, eventString, start); start += 4; end = eventString.find_first_of(" \t\n", start); nvpairs["parent"] = eventString.substr(start, end); break; case NOTIFY: break; case NOMATCH: throw ParseException(ParseException::DISCARDED_EVENT_TYPE, eventString); default: throw ParseException(ParseException::UNKNOWN_EVENT_TYPE, eventString); } /* Process common "key=value" format. */ for (start = 1; start < eventString.length(); start = end + 1) { /* Find the '=' in the middle of the key/value pair. */ end = eventString.find('=', start); if (end == string::npos) break; /* * Find the start of the key by backing up until * we hit whitespace or '!' (event type "notice"). * Due to the devdctl format, all key/value pair must * start with one of these two characters. */ start = eventString.find_last_of("! \t\n", end); if (start == string::npos) throw ParseException(ParseException::INVALID_FORMAT, eventString, end); start++; string key(eventString.substr(start, end - start)); /* * Walk forward from the '=' until either we exhaust * the buffer or we hit whitespace. */ start = end + 1; if (start >= eventString.length()) throw ParseException(ParseException::INVALID_FORMAT, eventString, end); end = eventString.find_first_of(" \t\n", start); if (end == string::npos) end = eventString.length() - 1; string value(eventString.substr(start, end - start)); nvpairs[key] = value; } } void Event::TimestampEventString(std::string &eventString) { if (eventString.size() > 0) { /* * Add a timestamp as the final field of the event if it is * not already present. */ if (eventString.find("timestamp=") == string::npos) { const size_t bufsize = 32; // Long enough for a 64-bit int timeval now; struct tm* time_s; char timebuf[bufsize]; size_t eventEnd(eventString.find_last_not_of('\n') + 1); if (gettimeofday(&now, NULL) != 0) err(1, "gettimeofday"); time_s = gmtime(&now.tv_sec); strftime(timebuf, bufsize, " timestamp=%s", time_s); eventString.insert(eventEnd, timebuf); } } } /*-------------------------------- DevfsEvent --------------------------------*/ //- DevfsEvent Static Public Methods ------------------------------------------- Event * DevfsEvent::Builder(Event::Type type, NVPairMap &nvPairs, const string &eventString) { return (new DevfsEvent(type, nvPairs, eventString)); } //- DevfsEvent Static Protected Methods ---------------------------------------- bool -DevfsEvent::IsDiskDev(const string &devName) -{ - static const char *diskDevNames[] = - { - "da", - "ada" - }; - - const char **diskName(diskDevNames); - const char **lastDiskName(diskDevNames - + NUM_ELEMENTS(diskDevNames) - 1); - size_t find_start = devName.rfind('/'); - if (find_start == string::npos) { - find_start = 0; - } else { - /* Just after the last '/'. */ - find_start++; - } - - for (; diskName <= lastDiskName; diskName++) { - - size_t loc(devName.find(*diskName, find_start)); - if (loc == find_start) { - size_t prefixLen(strlen(*diskName)); - - if (devName.length() - find_start >= prefixLen - && isdigit(devName[find_start + prefixLen])) - return (true); - } - } - - return (false); -} - -bool DevfsEvent::IsWholeDev(const string &devName) { string::const_iterator i(devName.begin()); size_t start = devName.rfind('/'); if (start == string::npos) { start = 0; } else { /* Just after the last '/'. */ start++; } i += start; /* alpha prefix followed only by digits. */ for (; i < devName.end() && !isdigit(*i); i++) ; if (i == devName.end()) return (false); for (; i < devName.end() && isdigit(*i); i++) ; return (i == devName.end()); } //- DevfsEvent Virtual Public Methods ------------------------------------------ Event * DevfsEvent::DeepCopy() const { return (new DevfsEvent(*this)); } bool DevfsEvent::Process() const { return (true); } //- DevfsEvent Public Methods -------------------------------------------------- bool -DevfsEvent::IsDiskDev() const -{ - string devName; - - return (DevName(devName) && IsDiskDev(devName)); -} - -bool DevfsEvent::IsWholeDev() const { string devName; return (DevName(devName) && IsDiskDev() && IsWholeDev(devName)); } bool DevfsEvent::DevName(std::string &name) const { if (Value("subsystem") != "CDEV") return (false); name = Value("cdev"); return (!name.empty()); } -bool -DevfsEvent::DevPath(std::string &path) const +//- DevfsEvent Protected Methods ----------------------------------------------- +DevfsEvent::DevfsEvent(Event::Type type, NVPairMap &nvpairs, + const string &eventString) + : Event(type, nvpairs, eventString) { - string devName; +} - if (!DevName(devName)) - return (false); +DevfsEvent::DevfsEvent(const DevfsEvent &src) + : Event(src) +{ +} - string devPath(_PATH_DEV + devName); - int devFd(open(devPath.c_str(), O_RDONLY)); - if (devFd == -1) - return (false); +/*--------------------------------- GeomEvent --------------------------------*/ +//- GeomEvent Static Public Methods -------------------------------------------- +Event * +GeomEvent::Builder(Event::Type type, NVPairMap &nvpairs, + const string &eventString) +{ + return (new GeomEvent(type, nvpairs, eventString)); +} - /* Normalize the device name in case the DEVFS event is for a link. */ - devName = fdevname(devFd); - path = _PATH_DEV + devName; - - close(devFd); - - return (true); +//- GeomEvent Virtual Public Methods ------------------------------------------- +Event * +GeomEvent::DeepCopy() const +{ + return (new GeomEvent(*this)); } bool -DevfsEvent::PhysicalPath(std::string &path) const +GeomEvent::DevName(std::string &name) const { - string devPath; - - if (!DevPath(devPath)) - return (false); - - int devFd(open(devPath.c_str(), O_RDONLY)); - if (devFd == -1) - return (false); - - char physPath[MAXPATHLEN]; - physPath[0] = '\0'; - bool result(ioctl(devFd, DIOCGPHYSPATH, physPath) == 0); - close(devFd); - if (result) - path = physPath; - return (result); + name = Value("devname"); + return (!name.empty()); } -//- DevfsEvent Protected Methods ----------------------------------------------- -DevfsEvent::DevfsEvent(Event::Type type, NVPairMap &nvpairs, - const string &eventString) - : Event(type, nvpairs, eventString) + +//- GeomEvent Protected Methods ------------------------------------------------ +GeomEvent::GeomEvent(Event::Type type, NVPairMap &nvpairs, + const string &eventString) + : Event(type, nvpairs, eventString), + m_devname(Value("devname")) { } -DevfsEvent::DevfsEvent(const DevfsEvent &src) - : Event(src) +GeomEvent::GeomEvent(const GeomEvent &src) + : Event(src), + m_devname(src.m_devname) { } /*--------------------------------- ZfsEvent ---------------------------------*/ //- ZfsEvent Static Public Methods --------------------------------------------- Event * ZfsEvent::Builder(Event::Type type, NVPairMap &nvpairs, const string &eventString) { return (new ZfsEvent(type, nvpairs, eventString)); } //- ZfsEvent Virtual Public Methods -------------------------------------------- Event * ZfsEvent::DeepCopy() const { return (new ZfsEvent(*this)); +} + +bool +ZfsEvent::DevName(std::string &name) const +{ + return (false); } //- ZfsEvent Protected Methods ------------------------------------------------- ZfsEvent::ZfsEvent(Event::Type type, NVPairMap &nvpairs, const string &eventString) : Event(type, nvpairs, eventString), m_poolGUID(Guid(Value("pool_guid"))), m_vdevGUID(Guid(Value("vdev_guid"))) { } ZfsEvent::ZfsEvent(const ZfsEvent &src) : Event(src), m_poolGUID(src.m_poolGUID), m_vdevGUID(src.m_vdevGUID) { } } // namespace DevdCtl Index: projects/zfsd/head/lib/libdevdctl/event.h =================================================================== --- projects/zfsd/head/lib/libdevdctl/event.h (revision 298703) +++ projects/zfsd/head/lib/libdevdctl/event.h (revision 298704) @@ -1,379 +1,423 @@ /*- - * Copyright (c) 2011, 2012, 2013 Spectra Logic Corporation + * Copyright (c) 2011, 2012, 2013, 2016 Spectra Logic Corporation * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions, and the following disclaimer, * without modification. * 2. Redistributions in binary form must reproduce at minimum a disclaimer * substantially similar to the "NO WARRANTY" disclaimer below * ("Disclaimer") and any redistribution must be conditioned upon * including a substantially similar Disclaimer requirement for further * binary redistribution. * * NO WARRANTY * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * Authors: Justin T. Gibbs (Spectra Logic Corporation) * * $FreeBSD$ */ /** * \file devdctl_event.h * * \brief Class hierarchy used to express events received via * the devdctl API. */ #ifndef _DEVDCTL_EVENT_H_ #define _DEVDCTL_EVENT_H_ /*============================ Namespace Control =============================*/ namespace DevdCtl { /*=========================== Forward Declarations ===========================*/ class EventFactory; /*============================= Class Definitions ============================*/ /*-------------------------------- NVPairMap ---------------------------------*/ /** * NVPairMap is a specialization of the standard map STL container. */ typedef std::map NVPairMap; /*----------------------------------- Event ----------------------------------*/ /** * \brief Container for the name => value pairs that comprise the content of * a device control event. * * All name => value data for events can be accessed via the Contains() * and Value() methods. name => value pairs for data not explicitly * received as a name => value pair are synthesized during parsing. For * example, ATTACH and DETACH events have "device-name" and "parent" * name => value pairs added. */ class Event { friend class EventFactory; public: /** Event type */ enum Type { /** Generic event notification. */ NOTIFY = '!', /** A driver was not found for this device. */ NOMATCH = '?', /** A bus device instance has been added. */ ATTACH = '+', /** A bus device instance has been removed. */ DETACH = '-' }; /** * Factory method type to construct an Event given * the type of event and an NVPairMap populated from * the event string received from devd. */ typedef Event* (BuildMethod)(Type, NVPairMap &, const std::string &); /** Generic Event object factory. */ static BuildMethod Builder; static Event *CreateEvent(const EventFactory &factory, const std::string &eventString); /** + * Returns the devname, if any, associated with the event + * + * \param name Devname, returned by reference + * \return True iff the event contained a devname + */ + virtual bool DevName(std::string &name) const; + + /** + * Returns the absolute pathname of the device associated with this + * event. + * + * \param name Devname, returned by reference + * \return True iff the event contained a devname + */ + bool DevPath(std::string &path) const; + + /** + * Returns true iff this event refers to a disk device + */ + bool IsDiskDev() const; + + /** Returns the physical path of the device, if any + * + * \param path Physical path, returned by reference + * \return True iff the event contains a device with a physical + * path + */ + bool PhysicalPath(std::string &path) const; + + /** * Provide a user friendly string representation of an * event type. * * \param type The type of event to map to a string. * * \return A user friendly string representing the input type. */ static const char *TypeToString(Type type); /** * Determine the availability of a name => value pair by name. * * \param name The key name to search for in this event instance. * * \return true if the specified key is available in this * event, otherwise false. */ bool Contains(const std::string &name) const; /** * \param key The name of the key for which to retrieve its * associated value. * * \return A const reference to the string representing the * value associated with key. * * \note For key's with no registered value, the empty string * is returned. */ const std::string &Value(const std::string &key) const; /** * Get the type of this event instance. * * \return The type of this event instance. */ Type GetType() const; /** * Get the orginal DevdCtl event string for this event. * * \return The DevdCtl event string. */ const std::string &GetEventString() const; /** * Convert the event instance into a string suitable for * printing to the console or emitting to syslog. * * \return A string of formatted event data. */ std::string ToString() const; /** * Pretty-print this event instance to cout. */ void Print() const; /** * Pretty-print this event instance to syslog. * * \param priority The logging priority/facility. * See syslog(3). */ void Log(int priority) const; /** * Create and return a fully independent clone * of this event. */ virtual Event *DeepCopy() const; /** Destructor */ virtual ~Event(); /** * Interpret and perform any actions necessary to * consume the event. * * \return True if this event should be queued for later reevaluation */ virtual bool Process() const; /** * Get the time that the event was created */ timeval GetTimestamp() const; /** * Add a timestamp to the event string, if one does not already exist * TODO: make this an instance method that operates on the std::map * instead of the string. We must fix zfsd's CaseFile serialization * routines first, so that they don't need the raw event string. * * \param[in,out] eventString The devd event string to modify */ static void TimestampEventString(std::string &eventString); /** * Access all parsed key => value pairs. */ const NVPairMap &GetMap() const; protected: /** Table entries used to map a type to a user friendly string. */ struct EventTypeRecord { Type m_type; const char *m_typeName; }; /** * Constructor * * \param type The type of event to create. */ Event(Type type, NVPairMap &map, const std::string &eventString); /** Deep copy constructor. */ Event(const Event &src); /** Always empty string returned when NVPairMap lookups fail. */ static const std::string s_theEmptyString; /** Unsorted table of event types. */ static EventTypeRecord s_typeTable[]; /** The type of this event. */ const Type m_type; /** * Event attribute storage. * * \note Although stored by reference (since m_nvPairs can * never be NULL), the NVPairMap referenced by this field * is dynamically allocated and owned by this event object. * m_nvPairs must be deleted at event desctruction. */ NVPairMap &m_nvPairs; /** * The unaltered event string, as received from devd, used to * create this event object. */ std::string m_eventString; private: /** * Ingest event data from the supplied string. * * \param[in] eventString The string of devd event data to parse. * \param[out] nvpairs Returns the parsed data */ static void ParseEventString(Type type, const std::string &eventString, NVPairMap &nvpairs); }; inline Event::Type Event::GetType() const { return (m_type); } inline const std::string & Event::GetEventString() const { return (m_eventString); } inline const NVPairMap & Event::GetMap() const { return (m_nvPairs); } /*--------------------------------- EventList --------------------------------*/ /** * EventList is a specialization of the standard list STL container. */ typedef std::list EventList; /*-------------------------------- DevfsEvent --------------------------------*/ class DevfsEvent : public Event { public: /** Specialized Event object factory for Devfs events. */ static BuildMethod Builder; virtual Event *DeepCopy() const; /** * Interpret and perform any actions necessary to * consume the event. * \return True if this event should be queued for later reevaluation */ virtual bool Process() const; - bool IsDiskDev() const; bool IsWholeDev() const; - bool DevName(std::string &name) const; - bool DevPath(std::string &path) const; - bool PhysicalPath(std::string &path) const; + virtual bool DevName(std::string &name) const; protected: /** - * Determine if the given device name references a potential - * disk device. - * - * \param devName The device name to test. - */ - static bool IsDiskDev(const std::string &devName); - - /** * Given the device name of a disk, determine if the device * represents the whole device, not just a partition. * * \param devName Device name of disk device to test. * * \return True if the device name represents the whole device. * Otherwise false. */ static bool IsWholeDev(const std::string &devName); /** DeepCopy Constructor. */ DevfsEvent(const DevfsEvent &src); /** Constructor */ DevfsEvent(Type, NVPairMap &, const std::string &); }; +/*--------------------------------- GeomEvent --------------------------------*/ +class GeomEvent : public Event +{ +public: + /** Specialized Event object factory for GEOM events. */ + static BuildMethod Builder; + + virtual Event *DeepCopy() const; + + virtual bool DevName(std::string &name) const; + + const std::string &DeviceName() const; + +protected: + /** Constructor */ + GeomEvent(Type, NVPairMap &, const std::string &); + + /** Deep copy constructor. */ + GeomEvent(const GeomEvent &src); + + std::string m_devname; +}; + /*--------------------------------- ZfsEvent ---------------------------------*/ class ZfsEvent : public Event { public: /** Specialized Event object factory for ZFS events. */ static BuildMethod Builder; virtual Event *DeepCopy() const; + + virtual bool DevName(std::string &name) const; const std::string &PoolName() const; Guid PoolGUID() const; Guid VdevGUID() const; protected: /** Constructor */ ZfsEvent(Type, NVPairMap &, const std::string &); /** Deep copy constructor. */ ZfsEvent(const ZfsEvent &src); Guid m_poolGUID; Guid m_vdevGUID; }; //- ZfsEvent Inline Public Methods -------------------------------------------- inline const std::string& ZfsEvent::PoolName() const { /* The pool name is reported as the subsystem of ZFS events. */ return (Value("subsystem")); } inline Guid ZfsEvent::PoolGUID() const { return (m_poolGUID); } inline Guid ZfsEvent::VdevGUID() const { return (m_vdevGUID); } } // namespace DevdCtl #endif /*_DEVDCTL_EVENT_H_ */ Index: projects/zfsd/head/lib/libdevdctl/tests/libdevdctl_unittest.cc =================================================================== --- projects/zfsd/head/lib/libdevdctl/tests/libdevdctl_unittest.cc (revision 298703) +++ projects/zfsd/head/lib/libdevdctl/tests/libdevdctl_unittest.cc (revision 298704) @@ -1,113 +1,136 @@ /*- * Copyright (c) 2016 Spectra Logic Corporation * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions, and the following disclaimer, * without modification. * 2. Redistributions in binary form must reproduce at minimum a disclaimer * substantially similar to the "NO WARRANTY" disclaimer below * ("Disclaimer") and any redistribution must be conditioned upon * including a substantially similar Disclaimer requirement for further * binary redistribution. * * NO WARRANTY * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * Authors: Alan Somers (Spectra Logic Corporation) */ #include #include #include #include #include #include #include using namespace DevdCtl; using namespace std; using namespace testing; -#define NUM_ELEMENTS(x) (sizeof(x) / sizeof(*x)) +#define REGISTRY_SIZE 2 -class IsDiskDevTest : public TestWithParam >{ +struct DevNameTestParams +{ + const char* evs; + bool is_disk; + const char* devname; +}; + +class DevNameTest : public TestWithParam{ protected: virtual void SetUp() { m_factory = new EventFactory(); + m_factory->UpdateRegistry(s_registry, REGISTRY_SIZE); } virtual void TearDown() { if (m_ev) delete m_ev; if (m_factory) delete m_factory; } EventFactory *m_factory; Event *m_ev; - static EventFactory::Record s_registry[]; + static EventFactory::Record s_registry[REGISTRY_SIZE]; }; -DevdCtl::EventFactory::Record IsDiskDevTest::s_registry[] = { - { Event::NOTIFY, "DEVFS", &DevfsEvent::Builder } +DevdCtl::EventFactory::Record DevNameTest::s_registry[REGISTRY_SIZE] = { + { Event::NOTIFY, "DEVFS", &DevfsEvent::Builder }, + { Event::NOTIFY, "GEOM", &GeomEvent::Builder } }; -TEST_P(IsDiskDevTest, TestIsDiskDev) { - pair param = GetParam(); - DevfsEvent *devfs_ev; +TEST_P(DevNameTest, TestDevname) { + std::string devname; + DevNameTestParams param = GetParam(); + + string evString(param.evs); + m_ev = Event::CreateEvent(*m_factory, evString); + m_ev->DevName(devname); + EXPECT_STREQ(param.devname, devname.c_str()); +} - m_factory->UpdateRegistry(s_registry, NUM_ELEMENTS(s_registry)); - string evString(param.second); +TEST_P(DevNameTest, TestIsDiskDev) { + DevNameTestParams param = GetParam(); + + string evString(param.evs); m_ev = Event::CreateEvent(*m_factory, evString); - devfs_ev = dynamic_cast(m_ev); - ASSERT_NE(nullptr, devfs_ev); - EXPECT_EQ(param.first, devfs_ev->IsDiskDev()); + EXPECT_EQ(param.is_disk, m_ev->IsDiskDev()); } -INSTANTIATE_TEST_CASE_P(IsDiskDevTestInstantiation, IsDiskDevTest, Values( - pair(true, - "!system=DEVFS subsystem=CDEV type=CREATE cdev=da6\n"), - pair(false, - "!system=DEVFS subsystem=CDEV type=CREATE cdev=cuau0\n"), - pair(true, - "!system=DEVFS subsystem=CDEV type=CREATE cdev=ada6\n"), - pair(true, - "!system=DEVFS subsystem=CDEV type=CREATE cdev=da6p1\n"), - pair(true, - "!system=DEVFS subsystem=CDEV type=CREATE cdev=ada6p1\n"), - pair(true, - "!system=DEVFS subsystem=CDEV type=CREATE cdev=da6s0p1\n"), - pair(true, - "!system=DEVFS subsystem=CDEV type=CREATE cdev=ada6s0p1\n"), +/* TODO: clean this up using C++-11 uniform initializers */ +INSTANTIATE_TEST_CASE_P(IsDiskDevTestInstantiation, DevNameTest, Values( + (DevNameTestParams){ + .evs = "!system=DEVFS subsystem=CDEV type=CREATE cdev=da6\n", + .is_disk = true, .devname = "da6"}, + (DevNameTestParams){.is_disk = false, .devname = "cuau0", + .evs = "!system=DEVFS subsystem=CDEV type=CREATE cdev=cuau0\n"}, + (DevNameTestParams){.is_disk = true, .devname = "ada6", + .evs = "!system=DEVFS subsystem=CDEV type=CREATE cdev=ada6\n"}, + (DevNameTestParams){.is_disk = true, .devname = "da6p1", + .evs = "!system=DEVFS subsystem=CDEV type=CREATE cdev=da6p1\n"}, + (DevNameTestParams){.is_disk = true, .devname = "ada6p1", + .evs = "!system=DEVFS subsystem=CDEV type=CREATE cdev=ada6p1\n"}, + (DevNameTestParams){.is_disk = true, .devname = "da6s0p1", + .evs = "!system=DEVFS subsystem=CDEV type=CREATE cdev=da6s0p1\n"}, + (DevNameTestParams){.is_disk = true, .devname = "ada6s0p1", + .evs = "!system=DEVFS subsystem=CDEV type=CREATE cdev=ada6s0p1\n"}, /* * Test physical path nodes. These are currently all set to false since * physical path nodes are implemented with symlinks, and most CAM and * ZFS operations can't use symlinked device nodes */ /* A SpectraBSD-style physical path node*/ - pair(false, - "!system=DEVFS subsystem=CDEV type=CREATE cdev=enc@50030480019f53fd/elmtype@array_device/slot@18/da\n"), - pair(false, - "!system=DEVFS subsystem=CDEV type=CREATE cdev=enc@50030480019f53fd/elmtype@array_device/slot@18/pass\n"), + (DevNameTestParams){.is_disk = false, .devname = "enc@50030480019f53fd/elmtype@array_device/slot@18/da", + .evs = "!system=DEVFS subsystem=CDEV type=CREATE cdev=enc@50030480019f53fd/elmtype@array_device/slot@18/da\n"}, + (DevNameTestParams){.is_disk = false, .devname = "enc@50030480019f53fd/elmtype@array_device/slot@18/pass", + .evs = "!system=DEVFS subsystem=CDEV type=CREATE cdev=enc@50030480019f53fd/elmtype@array_device/slot@18/pass\n"}, /* A FreeBSD-style physical path node */ - pair(true, - "!system=DEVFS subsystem=CDEV type=CREATE cdev=enc@n50030480019f53fd/type@0/slot@18/elmdesc@ArrayDevice18/da6\n"), - pair(false, - "!system=DEVFS subsystem=CDEV type=CREATE cdev=enc@n50030480019f53fd/type@0/slot@18/elmdesc@ArrayDevice18/pass6\n")); + (DevNameTestParams){.is_disk = true, .devname = "enc@n50030480019f53fd/type@0/slot@18/elmdesc@ArrayDevice18/da6", + .evs = "!system=DEVFS subsystem=CDEV type=CREATE cdev=enc@n50030480019f53fd/type@0/slot@18/elmdesc@ArrayDevice18/da6\n"}, + (DevNameTestParams){.is_disk = false, .devname = "enc@n50030480019f53fd/type@0/slot@18/elmdesc@ArrayDevice18/pass6", + .evs = "!system=DEVFS subsystem=CDEV type=CREATE cdev=enc@n50030480019f53fd/type@0/slot@18/elmdesc@ArrayDevice18/pass6\n"}, + + /* + * Test some GEOM events + */ + (DevNameTestParams){.is_disk = true, .devname = "da5", + .evs = "!system=GEOM subsystem=disk type=GEOM::physpath devname=da5\n"}) );