Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 14 Oct 2013 22:58:48 +0000 (UTC)
From:      Alan Somers <asomers@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-projects@freebsd.org
Subject:   svn commit: r256469 - in projects/zfsd/head: cddl/sbin/zfsd etc/mtree lib lib/libdevctl share/mk
Message-ID:  <201310142258.r9EMwmYP015385@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: asomers
Date: Mon Oct 14 22:58:48 2013
New Revision: 256469
URL: http://svnweb.freebsd.org/changeset/base/256469

Log:
  Extract devctl event processing support out of zfsd and into a new library:
  libdevctl.
  
  	etc/mtree/BSD.include.dist:
  		Add the /usr/include/devctl directory to the base system.
  
  	lib/Makefile:
  	lib/libdevctl/Makefile:
  		Build support for the new library.
  
  	lib/libdevctl/consumer.cc:
  	lib/libdevctl/consumer.h:
  	cddl/sbin/zfsd/zfsd.cc:
  	cddl/sbin/zfsd/zfsd.h:
  		New class, DevCtl::Consumer, from functionality extracted
  		from ZfsDaemon, which can connect to devd, parse an event
  		stream, and invoke event handlers.
  
  	lib/libdevctl/event.cc:
  	lib/libdevctl/event.h:
  	cddl/sbin/zfsd/dev_ctl_event.cc:
  	cddl/sbin/zfsd/dev_ctl_event.h:
  		Strip Zfsd specific event handling from Zfsd's event classes
  		to create DevCtl::Event, DevCtl::DevfsEvent, and
  		DevCtl::ZfsEvent.
  
  	lib/libdevctl/event_buffer.cc:
  	lib/libdevctl/event_buffer.h:
  	cddl/sbin/zfsd/zfsd.cc:
  	cddl/sbin/zfsd/zfsd.h:
  		DevCtl::EventBuffer: buffer for incoming devctl stream used
  		to parse/extract individual event strings.
  
  	lib/libdevctl/event_factory.cc:
  	lib/libdevctl/event_factory.h:
  	cddl/sbin/zfsd/zfsd.cc:
  	cddl/sbin/zfsd/zfsd.h:
  		DevCtl::EventFactory: Method map for converting event
  		strings to Devctl::Event objects.
  
  	lib/libdevctl/exception.cc:
  	lib/libdevctl/exception.h:
  	cddl/sbin/zfsd/zfsd_exception.cc:
  	cddl/sbin/zfsd/zfsd_exception.h:
  		DevCtl::Exception and DevCtl::ParseException, the exceptions
  		which are thrown by this library.
  
  	lib/libdevctl/guid.cc:
  	lib/libdevctl/guid.h:
  	cddl/sbin/zfsd/guid.cc:
  	cddl/sbin/zfsd/guid.h:
  		DevCtl::Guid: Helper routines for dealing with 64bit GUIDs
  		such as found in Zfs pools and vdevs.
  
  	lib/libdevctl/reader.cc:
  	lib/libdevctl/reader.h:
  	cddl/sbin/zfsd/zfsd.cc:
  	cddl/sbin/zfsd/zfsd.h:
  		DevCtl::Reader class hierarchy.  Used to direct a Consumer
  		to an event stream (e.g. from devd or a local/saved file).
  
  	cddl/sbin/zfsd/zfsd_event.cc:
  	cddl/sbin/zfsd/zfsd_event.h:
  		Zfsd specialization of DevCtl::Event types.
  
  	cddl/sbin/zfsd/zfsd_exception.cc:
  	cddl/sbin/zfsd/zfsd_exception.h:
  		Zfsd specific exception types, now derived from
  		DevCtl::Excpetion.
  
  	cddl/sbin/zfsd/Makefile:
  	cddl/sbin/zfsd/callout.cc:
  	cddl/sbin/zfsd/callout.h:
  	cddl/sbin/zfsd/case_file.cc:
  	cddl/sbin/zfsd/case_file.h:
  	cddl/sbin/zfsd/vdev.cc:
  	cddl/sbin/zfsd/vdev.h:
  	cddl/sbin/zfsd/vdev_iterator.cc:
  	cddl/sbin/zfsd/vdev_iterator.h:
  	cddl/sbin/zfsd/zfsd.cc:
  	cddl/sbin/zfsd/zfsd.h:
  	cddl/sbin/zfsd/zfsd_event.cc:
  	cddl/sbin/zfsd/zfsd_event.h:
  	cddl/sbin/zfsd/zfsd_exception.cc:
  	cddl/sbin/zfsd/zfsd_exception.h:
  	cddl/sbin/zfsd/zfsd_main.cc:
  	cddl/sbin/zfsd/zpool_list.cc:
  	cddl/sbin/zfsd/zpool_list.h:
  		Miscelaneuous refactoring to support using libdevctl instead
  		of local implementation.
  
  	cddl/sbin/zfsd/Makefile:
  	cddl/sbin/zfsd/Makefile.common:
  	tools/regression/zfsd/Makefile:
  		Export common build information for zfsd and its unit tests
  		to a "Makefile.common".
  
  Submitted by:	gibbs
  Approved by:	ken (mentor)
  Sponsored by:	Spectra Logic Corporation

Added:
  projects/zfsd/head/cddl/sbin/zfsd/Makefile.common
  projects/zfsd/head/cddl/sbin/zfsd/zfsd_event.cc
  projects/zfsd/head/cddl/sbin/zfsd/zfsd_event.h
  projects/zfsd/head/lib/libdevctl/
  projects/zfsd/head/lib/libdevctl/Makefile
  projects/zfsd/head/lib/libdevctl/consumer.cc
  projects/zfsd/head/lib/libdevctl/consumer.h
  projects/zfsd/head/lib/libdevctl/event.cc
  projects/zfsd/head/lib/libdevctl/event.h
  projects/zfsd/head/lib/libdevctl/event_buffer.cc
  projects/zfsd/head/lib/libdevctl/event_buffer.h
  projects/zfsd/head/lib/libdevctl/event_factory.cc
  projects/zfsd/head/lib/libdevctl/event_factory.h
  projects/zfsd/head/lib/libdevctl/exception.cc
  projects/zfsd/head/lib/libdevctl/exception.h
  projects/zfsd/head/lib/libdevctl/guid.cc
  projects/zfsd/head/lib/libdevctl/guid.h
  projects/zfsd/head/lib/libdevctl/reader.cc
  projects/zfsd/head/lib/libdevctl/reader.h
Deleted:
  projects/zfsd/head/cddl/sbin/zfsd/dev_ctl_event.cc
  projects/zfsd/head/cddl/sbin/zfsd/dev_ctl_event.h
  projects/zfsd/head/cddl/sbin/zfsd/guid.cc
  projects/zfsd/head/cddl/sbin/zfsd/guid.h
Modified:
  projects/zfsd/head/cddl/sbin/zfsd/Makefile
  projects/zfsd/head/cddl/sbin/zfsd/callout.cc
  projects/zfsd/head/cddl/sbin/zfsd/callout.h
  projects/zfsd/head/cddl/sbin/zfsd/case_file.cc
  projects/zfsd/head/cddl/sbin/zfsd/case_file.h
  projects/zfsd/head/cddl/sbin/zfsd/vdev.cc
  projects/zfsd/head/cddl/sbin/zfsd/vdev.h
  projects/zfsd/head/cddl/sbin/zfsd/vdev_iterator.cc
  projects/zfsd/head/cddl/sbin/zfsd/vdev_iterator.h
  projects/zfsd/head/cddl/sbin/zfsd/zfsd.cc
  projects/zfsd/head/cddl/sbin/zfsd/zfsd.h
  projects/zfsd/head/cddl/sbin/zfsd/zfsd_exception.cc
  projects/zfsd/head/cddl/sbin/zfsd/zfsd_exception.h
  projects/zfsd/head/cddl/sbin/zfsd/zfsd_main.cc
  projects/zfsd/head/cddl/sbin/zfsd/zpool_list.cc
  projects/zfsd/head/cddl/sbin/zfsd/zpool_list.h
  projects/zfsd/head/etc/mtree/BSD.include.dist
  projects/zfsd/head/lib/Makefile
  projects/zfsd/head/share/mk/bsd.libnames.mk

Modified: projects/zfsd/head/cddl/sbin/zfsd/Makefile
==============================================================================
--- projects/zfsd/head/cddl/sbin/zfsd/Makefile	Mon Oct 14 22:42:13 2013	(r256468)
+++ projects/zfsd/head/cddl/sbin/zfsd/Makefile	Mon Oct 14 22:58:48 2013	(r256469)
@@ -1,44 +1,7 @@
 # $FreeBSD$
 
-PROG_CXX=	zfsd
-SRCS=		callout.cc		\
-		case_file.cc		\
-		dev_ctl_event.cc	\
-		guid.cc			\
-		vdev.cc			\
-		vdev_iterator.cc	\
-		zfsd.cc			\
-		zfsd_exception.cc	\
-		zpool_list.cc		\
-		zfsd_main.cc
-
-NO_MAN=		YES
-
-WARNS?=		1
-
-INCFLAGS+= -I${.CURDIR}/../../../cddl/contrib/opensolaris/lib/libzpool/common
-INCFLAGS+= -I${.CURDIR}/../../../cddl/compat/opensolaris/include
-INCFLAGS+= -I${.CURDIR}/../../../cddl/compat/opensolaris/lib/libumem
-INCFLAGS+= -I${.CURDIR}/../../../sys/cddl/compat/opensolaris
-INCFLAGS+= -I${.CURDIR}/../../../cddl/contrib/opensolaris/head
-INCFLAGS+= -I${.CURDIR}/../../../cddl/contrib/opensolaris/lib/libuutil/common
-INCFLAGS+= -I${.CURDIR}/../../../cddl/contrib/opensolaris/lib/libumem/common
-INCFLAGS+= -I${.CURDIR}/../../../cddl/contrib/opensolaris/lib/libzfs/common
-INCFLAGS+= -I${.CURDIR}/../../../cddl/contrib/opensolaris/lib/libnvpair
-INCFLAGS+= -I${.CURDIR}/../../../sys/cddl/contrib/opensolaris/common/zfs
-INCFLAGS+= -I${.CURDIR}/../../../sys/cddl/contrib/opensolaris/uts/common
-INCFLAGS+= -I${.CURDIR}/../../../sys/cddl/contrib/opensolaris/uts/common/fs/zfs
-INCFLAGS+= -I${.CURDIR}/../../../sys/cddl/contrib/opensolaris/uts/common/sys
+.include "Makefile.common"
 
-CFLAGS= -Wall -g -DNEED_SOLARIS_BOOLEAN ${INCFLAGS}
-
-DPADD=  ${LIBZFS} ${LIBUTIL} ${LIBGEOM} ${LIBBSDXML} ${LIBSBUF} \
-	${LIBNVPAIR} ${LIBUUTIL}
-LDADD=  -lzfs -lutil -lgeom -lbsdxml -lsbuf -lnvpair -luutil
-
-cscope:
-	find ${.CURDIR} -type f -a \( -name "*.[ch]" -o -name "*.cc" \) \
-	     > ${.CURDIR}/cscope.files
-	cd ${.CURDIR} && cscope -buq ${INCFLAGS}
+PROG_CXX=	zfsd
 
 .include <bsd.prog.mk>

Added: projects/zfsd/head/cddl/sbin/zfsd/Makefile.common
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ projects/zfsd/head/cddl/sbin/zfsd/Makefile.common	Mon Oct 14 22:58:48 2013	(r256469)
@@ -0,0 +1,43 @@
+# $FreeBSD$
+
+SRCS=		callout.cc		\
+		case_file.cc		\
+		zfsd_event.cc		\
+		vdev.cc			\
+		vdev_iterator.cc	\
+		zfsd.cc			\
+		zfsd_exception.cc	\
+		zpool_list.cc		\
+		zfsd_main.cc
+
+NO_MAN=		YES
+
+WARNS?=		3
+
+# Ignore warnings about Solaris specific pragmas.
+IGNORE_PRAGMA=  YES
+
+INCFLAGS+= -I${.CURDIR}/../../../cddl/contrib/opensolaris/lib/libzpool/common
+INCFLAGS+= -I${.CURDIR}/../../../cddl/compat/opensolaris/include
+INCFLAGS+= -I${.CURDIR}/../../../cddl/compat/opensolaris/lib/libumem
+INCFLAGS+= -I${.CURDIR}/../../../sys/cddl/compat/opensolaris
+INCFLAGS+= -I${.CURDIR}/../../../cddl/contrib/opensolaris/head
+INCFLAGS+= -I${.CURDIR}/../../../cddl/contrib/opensolaris/lib/libuutil/common
+INCFLAGS+= -I${.CURDIR}/../../../cddl/contrib/opensolaris/lib/libumem/common
+INCFLAGS+= -I${.CURDIR}/../../../cddl/contrib/opensolaris/lib/libzfs/common
+INCFLAGS+= -I${.CURDIR}/../../../cddl/contrib/opensolaris/lib/libnvpair
+INCFLAGS+= -I${.CURDIR}/../../../sys/cddl/contrib/opensolaris/common/zfs
+INCFLAGS+= -I${.CURDIR}/../../../sys/cddl/contrib/opensolaris/uts/common
+INCFLAGS+= -I${.CURDIR}/../../../sys/cddl/contrib/opensolaris/uts/common/fs/zfs
+INCFLAGS+= -I${.CURDIR}/../../../sys/cddl/contrib/opensolaris/uts/common/sys
+
+CFLAGS= -g -DNEED_SOLARIS_BOOLEAN ${INCFLAGS}
+
+DPADD=  ${LIBDEVCTL} ${LIBZFS} ${LIBUTIL} ${LIBGEOM} ${LIBBSDXML} ${LIBSBUF} \
+	${LIBNVPAIR} ${LIBUUTIL}
+LDADD=  -ldevctl -lzfs -lutil -lgeom -lbsdxml -lsbuf -lnvpair -luutil
+
+cscope:
+	find ${.CURDIR} -type f -a \( -name "*.[ch]" -o -name "*.cc" \) \
+	     > ${.CURDIR}/cscope.files
+	cd ${.CURDIR} && cscope -buq ${INCFLAGS}

Modified: projects/zfsd/head/cddl/sbin/zfsd/callout.cc
==============================================================================
--- projects/zfsd/head/cddl/sbin/zfsd/callout.cc	Mon Oct 14 22:42:13 2013	(r256468)
+++ projects/zfsd/head/cddl/sbin/zfsd/callout.cc	Mon Oct 14 22:58:48 2013	(r256469)
@@ -37,10 +37,23 @@
  *        timer services built on top of the POSIX interval timer.
  */
 
+#include <sys/time.h>
+
 #include <signal.h>
 #include <syslog.h>
 
+#include <list>
+#include <map>
+#include <string>
+
+#include <devctl/guid.h>
+#include <devctl/event.h>
+#include <devctl/event_factory.h>
+#include <devctl/consumer.h>
+#include <devctl/exception.h>
+
 #include "callout.h"
+#include "vdev_iterator.h"
 #include "zfsd.h"
 #include "zfsd_exception.h"
 

Modified: projects/zfsd/head/cddl/sbin/zfsd/callout.h
==============================================================================
--- projects/zfsd/head/cddl/sbin/zfsd/callout.h	Mon Oct 14 22:42:13 2013	(r256468)
+++ projects/zfsd/head/cddl/sbin/zfsd/callout.h	Mon Oct 14 22:58:48 2013	(r256469)
@@ -34,15 +34,17 @@
  * \file callout.h
  *
  * \brief Interface for timer based callback services.
+ *
+ * Header requirements:
+ *
+ *     #include <sys/time.h>
+ *
+ *     #include <list>
  */
 
 #ifndef _CALLOUT_H_
 #define _CALLOUT_H_
 
-#include <sys/time.h>
-
-#include <list>
-
 /**
  * \brief Type of the function callback from a Callout.
  */

Modified: projects/zfsd/head/cddl/sbin/zfsd/case_file.cc
==============================================================================
--- projects/zfsd/head/cddl/sbin/zfsd/case_file.cc	Mon Oct 14 22:42:13 2013	(r256468)
+++ projects/zfsd/head/cddl/sbin/zfsd/case_file.cc	Mon Oct 14 22:58:48 2013	(r256469)
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 2011 Spectra Logic Corporation
+ * Copyright (c) 2011, 2012, 2013 Spectra Logic Corporation
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -38,6 +38,11 @@
  * across reboots.  For now, this is just a log of soft errors which we
  * accumulate in order to mark a device as degraded.
  */
+#include <sys/cdefs.h>
+#include <sys/time.h>
+
+#include <sys/fs/zfs.h>
+
 #include <dirent.h>
 #include <iomanip>
 #include <fstream>
@@ -46,12 +51,31 @@
 #include <syslog.h>
 #include <unistd.h>
 
+#include <libzfs.h>
+
+#include <list>
+#include <map>
+#include <string>
+
+#include <devctl/guid.h>
+#include <devctl/event.h>
+#include <devctl/event_buffer.h>
+#include <devctl/event_factory.h>
+#include <devctl/exception.h>
+#include <devctl/consumer.h>
+#include <devctl/reader.h>
+
+#include "callout.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$");
+
 /*============================ Namespace Control =============================*/
 using std::auto_ptr;
 using std::hex;
@@ -60,26 +84,43 @@ using std::stringstream;
 using std::setfill;
 using std::setw;
 
+using DevCtl::Event;
+using DevCtl::EventBuffer;
+using DevCtl::EventFactory;
+using DevCtl::EventList;
+using DevCtl::Guid;
+using DevCtl::IstreamReader;
+using DevCtl::ParseException;
+
 /*-------------------------- File-scoped classes ----------------------------*/
 /**
  * \brief Functor that operators on STL collections of CaseFiles
+ * 
  * Selectively calls ReEvaluate on the casefile, based on its pool GUID.
  */
 class CaseFileReEvaluator : public std::unary_function<CaseFile, bool>
 {
 public:
-	CaseFileReEvaluator(Guid guid, const ZfsEvent &event) :
-		m_poolGUID(guid), m_event(event) {};
-	void operator() (CaseFile *casefile) {
-		if (m_poolGUID == casefile->PoolGUID())
-			casefile->ReEvaluate(m_event);
-	}
+	CaseFileReEvaluator(Guid guid, const ZfsEvent &event);
+
+	void operator() (CaseFile *casefile);
+
 private:
 	Guid		m_poolGUID;
-	const ZfsEvent	&m_event;
+	const ZfsEvent &m_event;
 };
 
+CaseFileReEvaluator::CaseFileReEvaluator(Guid guid, const ZfsEvent &event)
+ : m_poolGUID(guid), m_event(event)
+{
+}
 
+void
+CaseFileReEvaluator::operator() (CaseFile *casefile)
+{
+	if (m_poolGUID == casefile->PoolGUID())
+		casefile->ReEvaluate(m_event);
+}
 
 /*--------------------------------- CaseFile ---------------------------------*/
 //- CaseFile Static Data -------------------------------------------------------
@@ -88,7 +129,6 @@ const string  CaseFile::s_caseFilePath =
 const timeval CaseFile::s_removeGracePeriod = { 60 /*sec*/, 0 /*usec*/};
 
 //- CaseFile Static Public Methods ---------------------------------------------
-
 CaseFile *
 CaseFile::Find(Guid poolGUID, Guid vdevGUID)
 {
@@ -509,7 +549,7 @@ CaseFile::ActivateSpare() {
 }
 
 void
-CaseFile::RegisterCallout(const DevCtlEvent &event)
+CaseFile::RegisterCallout(const Event &event)
 {
 	timeval now, countdown, elapsed, timestamp, zero, remaining;
 
@@ -594,13 +634,13 @@ CaseFile::Log()
 	       zpool_state_to_name(VdevState(), VDEV_AUX_NONE));
 	if (m_tentativeEvents.size() != 0) {
 		syslog(LOG_INFO, "\t=== Tentative Events ===\n");
-		for (DevCtlEventList::iterator event(m_tentativeEvents.begin());
+		for (EventList::iterator event(m_tentativeEvents.begin());
 		     event != m_tentativeEvents.end(); event++)
 			(*event)->Log(LOG_INFO);
 	}
 	if (m_events.size() != 0) {
 		syslog(LOG_INFO, "\t=== Events ===\n");
-		for (DevCtlEventList::iterator event(m_events.begin());
+		for (EventList::iterator event(m_events.begin());
 		     event != m_events.end(); event++)
 			(*event)->Log(LOG_INFO);
 	}
@@ -738,7 +778,7 @@ CaseFile::~CaseFile()
 void
 CaseFile::PurgeEvents()
 {
-	for (DevCtlEventList::iterator event(m_events.begin());
+	for (EventList::iterator event(m_events.begin());
 	     event != m_events.end(); event++)
 		delete *event;
 
@@ -748,7 +788,7 @@ CaseFile::PurgeEvents()
 void
 CaseFile::PurgeTentativeEvents()
 {
-	for (DevCtlEventList::iterator event(m_tentativeEvents.begin());
+	for (EventList::iterator event(m_tentativeEvents.begin());
 	     event != m_tentativeEvents.end(); event++)
 		delete *event;
 
@@ -756,12 +796,12 @@ CaseFile::PurgeTentativeEvents()
 }
 
 void
-CaseFile::SerializeEvList(const DevCtlEventList events, int fd,
+CaseFile::SerializeEvList(const EventList events, int fd,
 		const char* prefix) const
 {
 	if (events.empty())
 		return;
-	for (DevCtlEventList::const_iterator curEvent = events.begin();
+	for (EventList::const_iterator curEvent = events.begin();
 	     curEvent != events.end(); curEvent++) {
 		const string &eventString((*curEvent)->GetEventString());
 
@@ -816,7 +856,7 @@ CaseFile::DeSerialize(ifstream &caseStre
 		 * call ExtractEvent
 		 * continue
 		 */
-		DevCtlEventList* destEvents;
+		EventList* destEvents;
 		string tentFlag("tentative ");
 		string line;
 		std::stringbuf lineBuf;
@@ -832,8 +872,9 @@ CaseFile::DeSerialize(ifstream &caseStre
 		}
 		fakeDevdSocket << line;
 		fakeDevdSocket << '\n';
+		const EventFactory &factory(ZfsDaemon::Get().GetFactory());
 		while (eventBuffer.ExtractEvent(evString)) {
-			DevCtlEvent *event(DevCtlEvent::CreateEvent(evString));
+			Event *event(Event::CreateEvent(factory, evString));
 			if (event != NULL) {
 				destEvents->push_back(event);
 				RegisterCallout(*event);
@@ -993,23 +1034,29 @@ CaseFile::Replace(const char* vdev_type,
 }
 
 /* Does the argument event refer to a checksum error? */
-static bool IsChecksumEvent(const DevCtlEvent* const event){
+static bool
+IsChecksumEvent(const Event* const event)
+{
 	return ("ereport.fs.zfs.checksum" == event->Value("type"));
 }
 
 /* Does the argument event refer to an IO error? */
-static bool IsIOEvent(const DevCtlEvent* const event){
+static bool
+IsIOEvent(const Event* const event)
+{
 	return ("ereport.fs.zfs.io" == event->Value("type"));
 }
 
 bool
-CaseFile::ShouldDegrade() const {
+CaseFile::ShouldDegrade() const
+{
 	return (std::count_if(m_events.begin(), m_events.end(),
 	    		      IsChecksumEvent) > ZFS_DEGRADE_IO_COUNT);
 }
 
 bool
-CaseFile::ShouldFault() const {
+CaseFile::ShouldFault() const
+{
 	return (std::count_if(m_events.begin(), m_events.end(),
 	    		      IsIOEvent) > ZFS_DEGRADE_IO_COUNT);
 }

Modified: projects/zfsd/head/cddl/sbin/zfsd/case_file.h
==============================================================================
--- projects/zfsd/head/cddl/sbin/zfsd/case_file.h	Mon Oct 14 22:42:13 2013	(r256468)
+++ projects/zfsd/head/cddl/sbin/zfsd/case_file.h	Mon Oct 14 22:58:48 2013	(r256469)
@@ -37,18 +37,17 @@
  *
  * CaseFile objects aggregate vdev faults that may require ZFSD action
  * in order to maintain the health of a ZFS pool.
+ *
+ * Header requirements:
+ *
+ *    #include <list>
+ *
+ *    #include "callout.h"
+ *    #include "zfsd_event.h"
  */
 #ifndef _CASE_FILE_H_
 #define	_CASE_FILE_H_
 
-#include <list>
-#include <utility>
-
-#include <sys/fs/zfs.h>
-
-#include "callout.h"
-#include "dev_ctl_event.h"
-
 /*=========================== Forward Declarations ===========================*/
 class CaseFile;
 class Vdev;
@@ -95,7 +94,7 @@ public:
 	 * \return  If found, a pointer to a valid CaseFile object.
 	 *          Otherwise NULL.
 	 */
-	static CaseFile *Find(Guid poolGUID, Guid vdevGUID);
+	static CaseFile *Find(DevCtl::Guid poolGUID, DevCtl::Guid vdevGUID);
 
 	/**
 	 * \brief Find a CaseFile object by a vdev's current/last known
@@ -114,7 +113,8 @@ public:
 	 * \param poolGUID	Only reevaluate cases for this pool
 	 * \param event		Try to consume this event with the casefile
 	 */
-	static void ReEvaluateByGuid(Guid poolGUID, const ZfsEvent &event);
+	static void ReEvaluateByGuid(DevCtl::Guid poolGUID,
+				     const ZfsEvent &event);
 
 	/**
 	 * \brief Create or return an existing active CaseFile for the
@@ -145,8 +145,8 @@ public:
 	 */
 	static void      PurgeAll();
 
-	Guid	      PoolGUID()       const;
-	Guid	      VdevGUID()       const;
+	DevCtl::Guid  PoolGUID()       const;
+	DevCtl::Guid  VdevGUID()       const;
 	vdev_state    VdevState()      const;
 	const string &PoolGUIDString() const;
 	const string &VdevGUIDString() const;
@@ -171,6 +171,7 @@ public:
 
 	/**
 	 * \brief Update this CaseFile in light of the provided ZfsEvent.
+	 *
 	 * Must be virtual so it can be overridden in the unit tests
 	 *
 	 * \param event  The ZfsEvent to evaluate.
@@ -182,7 +183,7 @@ public:
 	/**
 	 * \brief Register an itimer callout for the given event, if necessary
 	 */
-	virtual void RegisterCallout(const DevCtlEvent &event);
+	virtual void RegisterCallout(const DevCtl::Event &event);
 
 	/**
 	 * \brief Close a case if it is no longer relevant.
@@ -289,7 +290,7 @@ protected:
 	 * \param prefix  If not NULL, this prefix will be prepended to
 	 *                every event in the file.
 	 */
-	void SerializeEvList(const DevCtlEventList events, int fd,
+	void SerializeEvList(const DevCtl::EventList events, int fd,
 			     const char* prefix=NULL) const;
 
 	/**
@@ -327,7 +328,6 @@ protected:
 	 * \param path        The file system path to the new vdev
 	 *
 	 * \return            true iff the replacement was successful
-	 *
 	 */
 	bool Replace(const char* vdev_type, const char* path);
 
@@ -351,35 +351,35 @@ protected:
 	 * \brief A list of soft error events counted against the health of
 	 *        a vdev.
 	 */
-	DevCtlEventList m_events;
+	DevCtl::EventList m_events;
 
 	/**
 	 * \brief A list of soft error events waiting for a grace period
 	 *        expiration before being counted against the health of
 	 *        a vdev.
 	 */
-	DevCtlEventList m_tentativeEvents;
+	DevCtl::EventList m_tentativeEvents;
 
-	Guid		m_poolGUID;
-	Guid		m_vdevGUID;
-	vdev_state	m_vdevState;
-	string		m_poolGUIDString;
-	string		m_vdevGUIDString;
-	string		m_vdevPhysPath;
+	DevCtl::Guid	  m_poolGUID;
+	DevCtl::Guid	  m_vdevGUID;
+	vdev_state	  m_vdevState;
+	string		  m_poolGUIDString;
+	string		  m_vdevGUIDString;
+	string		  m_vdevPhysPath;
 
 	/**
 	 * \brief Callout activated when a grace period
 	 */
-	Callout		m_tentativeTimer;
+	Callout		  m_tentativeTimer;
 };
 
-inline Guid
+inline DevCtl::Guid
 CaseFile::PoolGUID() const
 {
 	return (m_poolGUID);
 }
 
-inline Guid
+inline DevCtl::Guid
 CaseFile::VdevGUID() const
 {
 	return (m_vdevGUID);

Modified: projects/zfsd/head/cddl/sbin/zfsd/vdev.cc
==============================================================================
--- projects/zfsd/head/cddl/sbin/zfsd/vdev.cc	Mon Oct 14 22:42:13 2013	(r256468)
+++ projects/zfsd/head/cddl/sbin/zfsd/vdev.cc	Mon Oct 14 22:58:48 2013	(r256469)
@@ -38,15 +38,30 @@
  * Implementation of the Vdev class.
  */
 #include <sys/cdefs.h>
+#include <sys/fs/zfs.h>
 
+#include <libzfs.h>
+
+#include <list>
+#include <map>
+#include <string>
 #include <sstream>
 
+#include <devctl/guid.h>
+#include <devctl/event.h>
+#include <devctl/event_buffer.h>
+#include <devctl/event_factory.h>
+#include <devctl/exception.h>
+#include <devctl/consumer.h>
+
 #include "vdev.h"
+#include "vdev_iterator.h"
 #include "zfsd.h"
 #include "zfsd_exception.h"
 
 __FBSDID("$FreeBSD$");
 /*============================ Namespace Control =============================*/
+using std::string;
 using std::stringstream;
 
 /*=========================== Class Implementations ==========================*/

Modified: projects/zfsd/head/cddl/sbin/zfsd/vdev.h
==============================================================================
--- projects/zfsd/head/cddl/sbin/zfsd/vdev.h	Mon Oct 14 22:42:13 2013	(r256468)
+++ projects/zfsd/head/cddl/sbin/zfsd/vdev.h	Mon Oct 14 22:58:48 2013	(r256469)
@@ -36,18 +36,25 @@
  * \file vdev.h
  *
  * Definition of the Vdev class.
+ *
+ * Header requirements:
+ *
+ *    #include <string>
+ *
+ *    #include <devctl/guid.h>
  */
 #ifndef	_VDEV_H_
 #define	_VDEV_H_
 
-#include <ostream>
-#include <string>
-
-#include <sys/fs/zfs.h>
-#include <libzfs.h>
+/*=========================== Forward Declarations ===========================*/
+struct zpool_handle;
+typedef struct zpool_handle zpool_handle_t;
 
-#include "guid.h"
+struct nvlist;
+typedef struct nvlist nvlist_t;
 
+/*============================= Class Definitions ============================*/
+/*----------------------------------- Vdev -----------------------------------*/
 /**
  * \brief Wrapper class for a vdev's name/value configuration list
  *        simplifying access to commonly used vdev attributes.
@@ -96,30 +103,30 @@ public:
 	 */
 	Vdev(nvlist_t *vdevConfig);
 
-	virtual Guid	 	GUID()		const;
-	virtual Guid	 	PoolGUID()	const;
-	virtual vdev_state	State()		const;
-	std::string	 	Path()		const;
-	virtual std::string	PhysicalPath()	const;
-	std::string	 GUIDString()	const;
-	nvlist_t	*PoolConfig()	const;
-	nvlist_t	*Config()	const;
+	virtual DevCtl::Guid	 GUID()		const;
+	virtual DevCtl::Guid	 PoolGUID()	const;
+	virtual vdev_state	 State()	const;
+	std::string	 	 Path()		const;
+	virtual std::string	 PhysicalPath()	const;
+	std::string		 GUIDString()	const;
+	nvlist_t		*PoolConfig()	const;
+	nvlist_t		*Config()	const;
 
 private:
-	Guid	  m_poolGUID;
-	Guid	  m_vdevGUID;
-	nvlist_t *m_poolConfig;
-	nvlist_t *m_config;
+	DevCtl::Guid m_poolGUID;
+	DevCtl::Guid m_vdevGUID;
+	nvlist_t    *m_poolConfig;
+	nvlist_t    *m_config;
 };
 
 //- Vdev Inline Public Methods ------------------------------------------------
-inline Guid
+inline DevCtl::Guid
 Vdev::PoolGUID() const
 {
 	return (m_poolGUID);
 }
 
-inline Guid
+inline DevCtl::Guid
 Vdev::GUID() const
 {
 	return (m_vdevGUID);

Modified: projects/zfsd/head/cddl/sbin/zfsd/vdev_iterator.cc
==============================================================================
--- projects/zfsd/head/cddl/sbin/zfsd/vdev_iterator.cc	Mon Oct 14 22:42:13 2013	(r256468)
+++ projects/zfsd/head/cddl/sbin/zfsd/vdev_iterator.cc	Mon Oct 14 22:58:48 2013	(r256469)
@@ -35,17 +35,27 @@
  *
  * Implementation of the VdevIterator class.
  */
+#include <sys/cdefs.h>
+#include <sys/fs/zfs.h>
+
 #include <stdint.h>
 #include <syslog.h>
 
-#include <sys/fs/zfs.h>
 #include <libzfs.h>
 
+#include <list>
+#include <string>
+
+#include <devctl/exception.h>
+#include <devctl/guid.h>
+
 #include "vdev.h"
 #include "vdev_iterator.h"
-#include "zfsd.h"
 #include "zfsd_exception.h"
 
+/*============================ Namespace Control =============================*/
+using DevCtl::Guid;
+
 /*=========================== Class Implementations ==========================*/
 /*------------------------------- VdevIterator -------------------------------*/
 VdevIterator::VdevIterator(zpool_handle_t *pool)

Modified: projects/zfsd/head/cddl/sbin/zfsd/vdev_iterator.h
==============================================================================
--- projects/zfsd/head/cddl/sbin/zfsd/vdev_iterator.h	Mon Oct 14 22:42:13 2013	(r256468)
+++ projects/zfsd/head/cddl/sbin/zfsd/vdev_iterator.h	Mon Oct 14 22:58:48 2013	(r256469)
@@ -34,16 +34,21 @@
  * \file vdev_iterator.h
  *
  * VdevIterator class definition.
+ *
+ * Header requirements:
+ *
+ *    #include <list>
  */
 #ifndef	_VDEV_ITERATOR_H_
 #define	_VDEV_ITERATOR_H_
 
-#include <list>
+/*=========================== Forward Declarations ===========================*/
+struct zpool_handle;
+typedef struct zpool_handle zpool_handle_t;
 
-#include <sys/fs/zfs.h>
-#include <libzfs.h>
+struct nvlist;
+typedef struct nvlist nvlist_t;
 
-/*=========================== Forward Declarations ===========================*/
 class Vdev;
 
 /*============================= Class Definitions ============================*/
@@ -97,7 +102,7 @@ public:
 	 * Upon return, the VdevIterator's cursor points to the vdev just
 	 * past the returned vdev or end() if no matching vdev is found.
 	 */
-	nvlist_t *Find(Guid vdevGUID);
+	nvlist_t *Find(DevCtl::Guid vdevGUID);
 
 	/**
 	 * \brief Perform the specified operation on each leaf member of

Modified: projects/zfsd/head/cddl/sbin/zfsd/zfsd.cc
==============================================================================
--- projects/zfsd/head/cddl/sbin/zfsd/zfsd.cc	Mon Oct 14 22:42:13 2013	(r256468)
+++ projects/zfsd/head/cddl/sbin/zfsd/zfsd.cc	Mon Oct 14 22:58:48 2013	(r256469)
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 2011 Spectra Logic Corporation
+ * Copyright (c) 2011, 2012, 2013 Spectra Logic Corporation
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -42,242 +42,77 @@
  */
 
 #include <sys/cdefs.h>
-#include <sys/disk.h>
-#include <sys/filio.h>
 #include <sys/param.h>
-#include <sys/poll.h>
-#include <sys/socket.h>
-#include <sys/types.h>
-#include <sys/un.h>
-
-#include <cstdio>
-#include <cstdlib>
-#include <csignal>
+#include <sys/fs/zfs.h>
+
 #include <err.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <iostream>
+#include <libgeom.h>
 #include <libutil.h>
-#include <sstream>
-#include <string>
+#include <poll.h>
 #include <syslog.h>
 
-#include <sys/fs/zfs.h>
 #include <libzfs.h>
-#include <libgeom.h>
+
+#include <list>
+#include <map>
+#include <string>
+
+#include <devctl/guid.h>
+#include <devctl/event.h>
+#include <devctl/event_buffer.h>
+#include <devctl/event_factory.h>
+#include <devctl/exception.h>
+#include <devctl/consumer.h>
+#include <devctl/reader.h>
 
 #include "callout.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 std::string;
-using std::stringstream;
-using std::cerr;
-using std::cout;
-using std::endl;
+using DevCtl::Event;
+using DevCtl::EventBuffer;
+using DevCtl::EventFactory;
+using DevCtl::EventList;
+using DevCtl::Guid;
+using DevCtl::NVPairMap;
 
 /*================================ Global Data ===============================*/
-const char       g_devdSock[] = "/var/run/devd.pipe";
 int              g_debug = 0;
 libzfs_handle_t *g_zfsHandle;
 
-/*-------------------------------- FDReader    -------------------------------*/
-//- FDReader Public Methods ----------------------------------------------------
-size_t
-FDReader::in_avail() const
-{
-	int bytes;
-	if (ioctl(m_fd, FIONREAD, &bytes)) {
-		syslog(LOG_ERR, "ioctl FIONREAD: %s", strerror(errno));
-		return (0);
-	}
-	return (bytes);
-}
-
-
-/*-------------------------------- IstreamReader   ---------------------------*/
-//- IstreamReader Public Methods ----------------------------------------------
-ssize_t
-IstreamReader::read(char* buf, size_t count)
-{
-	m_stream->read(buf, count);
-	if (m_stream->fail())
-		return (-1);
-	return (m_stream->gcount());
-}
-
-
-/*-------------------------------- EventBuffer -------------------------------*/
-//- EventBuffer Static Data ----------------------------------------------------
-/**
- * NOTIFY, NOMATCH, ATTACH, DETACH.  See DevCtlEvent::Type.
- */
-const char EventBuffer::s_eventStartTokens[] = "!?+-";
-
-/**
- * Events are terminated by a newline.
- */
-const char EventBuffer::s_eventEndTokens[] = "\n";
-
-/**
- * Key=Value pairs are terminated by whitespace.
- */
-const char EventBuffer::s_keyPairSepTokens[] = " \t\n";
-
-//- EventBuffer Public Methods -------------------------------------------------
-EventBuffer::EventBuffer(Reader& reader)
- : m_reader(reader),
-   m_validLen(0),
-   m_parsedLen(0),
-   m_nextEventOffset(0),
-   m_synchronized(true)
-{
-}
-
-bool
-EventBuffer::ExtractEvent(string &eventString)
-{
-	stringstream tsField;
-	timeval now;
-
-	gettimeofday(&now, NULL);
-	tsField << " timestamp=" << now.tv_sec;
-
-	while (UnParsed() > 0 || Fill()) {
-
-		/*
-		 * If the valid data in the buffer isn't enough to hold
-		 * a full event, try reading more.
-		 */
-		if (NextEventMaxLen() < MIN_EVENT_SIZE) {
-			m_parsedLen += UnParsed();
-			continue;
-		}
-
-		char  *nextEvent(m_buf + m_nextEventOffset);
-		bool   truncated(true);
-		size_t eventLen(strcspn(nextEvent, s_eventEndTokens));
-
-		if (!m_synchronized) {
-			/* Discard data until an end token is read. */
-			if (nextEvent[eventLen] != '\0')
-				m_synchronized = true;
-			m_nextEventOffset += eventLen;
-			m_parsedLen = m_nextEventOffset;
-			continue;
-		} else if (nextEvent[eventLen] == '\0') {
-
-			m_parsedLen += eventLen;
-			if (m_parsedLen < MAX_EVENT_SIZE) {
-				/*
-				 * Ran out of buffer before hitting
-				 * a full event. Fill() and try again.
-				 */
-				continue;
-			}
-			syslog(LOG_WARNING, "Overran event buffer\n\tm_nextEventOffset"
-			       "=%zd\n\tm_parsedLen=%zd\n\tm_validLen=%zd",
-			       m_nextEventOffset, m_parsedLen, m_validLen);
-		} else {
-			/*
-			 * Include the normal terminator in the extracted
-			 * event data.
-			 */
-			eventLen += 1;
-			truncated = false;
-		}
-
-		m_nextEventOffset += eventLen;
-		m_parsedLen = m_nextEventOffset;
-		eventString.assign(nextEvent, eventLen);
-
-		if (truncated) {
-			size_t fieldEnd;
-
-			/* Break cleanly at the end of a key<=>value pair. */
-			fieldEnd = eventString.find_last_of(s_keyPairSepTokens);
-			if (fieldEnd != string::npos)
-				eventString.erase(fieldEnd);
-			eventString += '\n';
-
-			m_synchronized = false;
-			syslog(LOG_WARNING,
-			       "Truncated %zd characters from event.",
-			       eventLen - fieldEnd);
-		}
-
-		/*
-		 * Add a timestamp as the final field of the event if it is
-		 * not already present.
-		 */
-		if (eventString.find("timestamp=") == string::npos) {
-			size_t eventEnd(eventString.find_last_not_of('\n') + 1);
-
-			eventString.insert(eventEnd, tsField.str());
-		}
-
-		return (true);
-	}
-	return (false);
-}
-
-//- EventBuffer Private Methods ------------------------------------------------
-bool
-EventBuffer::Fill()
-{
-	size_t avail;
-	ssize_t consumed(0);
-
-	/* Compact the buffer. */
-	if (m_nextEventOffset != 0) {
-		memmove(m_buf, m_buf + m_nextEventOffset,
-			m_validLen - m_nextEventOffset);
-		m_validLen -= m_nextEventOffset;
-		m_parsedLen -= m_nextEventOffset;
-		m_nextEventOffset = 0;
-	}
-
-	/* Fill any empty space. */
-	avail = m_reader.in_avail();
-	if (avail) {
-		size_t want;
-
-		want = std::min(avail, MAX_READ_SIZE - m_validLen);
-		consumed = m_reader.read(m_buf + m_validLen, want);
-		if (consumed == -1) {
-			if (errno == EINTR)
-				return (false);
-			else
-				err(1, "EventBuffer::Fill(): Read failed");
-		}
-	}
-
-	m_validLen += consumed;
-	/* Guarantee our buffer is always NUL terminated. */
-	m_buf[m_validLen] = '\0';
-
-	return (consumed > 0);
-}
-
 /*--------------------------------- ZfsDaemon --------------------------------*/
 //- ZfsDaemon Static Private Data ----------------------------------------------
-bool             ZfsDaemon::s_logCaseFiles;
-bool             ZfsDaemon::s_terminateEventLoop;
-char             ZfsDaemon::s_pidFilePath[] = "/var/run/zfsd.pid";
-pidfh           *ZfsDaemon::s_pidFH;
-FDReader*        ZfsDaemon::s_reader;
-int              ZfsDaemon::s_devdSockFD = -1;
-int              ZfsDaemon::s_signalPipeFD[2];
-bool		 ZfsDaemon::s_systemRescanRequested(false);
-bool		 ZfsDaemon::s_consumingEvents(false);
-DevCtlEventList	 ZfsDaemon::s_unconsumedEvents;
+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::DevfsEventBuilder },
+	{ Event::NOTIFY, "ZFS",   &ZfsEvent::ZfsEventBuilder }
+};
 
 //- ZfsDaemon Static Public Methods --------------------------------------------
+ZfsDaemon &
+ZfsDaemon::Get()
+{
+	return (*s_theZfsDaemon);
+}
+
 void
 ZfsDaemon::WakeEventLoop()
 {
@@ -294,36 +129,40 @@ ZfsDaemon::RequestSystemRescan()
 void
 ZfsDaemon::Run()
 {
-	Init();
+	ZfsDaemon daemon;
 
 	while (s_terminateEventLoop == false) {
 
 		try {
-			DisconnectFromDevd();
+			daemon.DisconnectFromDevd();
 

*** DIFF OUTPUT TRUNCATED AT 1000 LINES ***



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201310142258.r9EMwmYP015385>