17a0c41d5SAlan Somers /*-
27a0c41d5SAlan Somers * Copyright (c) 2011, 2012, 2013 Spectra Logic Corporation
37a0c41d5SAlan Somers * All rights reserved.
47a0c41d5SAlan Somers *
57a0c41d5SAlan Somers * Redistribution and use in source and binary forms, with or without
67a0c41d5SAlan Somers * modification, are permitted provided that the following conditions
77a0c41d5SAlan Somers * are met:
87a0c41d5SAlan Somers * 1. Redistributions of source code must retain the above copyright
97a0c41d5SAlan Somers * notice, this list of conditions, and the following disclaimer,
107a0c41d5SAlan Somers * without modification.
117a0c41d5SAlan Somers * 2. Redistributions in binary form must reproduce at minimum a disclaimer
127a0c41d5SAlan Somers * substantially similar to the "NO WARRANTY" disclaimer below
137a0c41d5SAlan Somers * ("Disclaimer") and any redistribution must be conditioned upon
147a0c41d5SAlan Somers * including a substantially similar Disclaimer requirement for further
157a0c41d5SAlan Somers * binary redistribution.
167a0c41d5SAlan Somers *
177a0c41d5SAlan Somers * NO WARRANTY
187a0c41d5SAlan Somers * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
197a0c41d5SAlan Somers * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
207a0c41d5SAlan Somers * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
217a0c41d5SAlan Somers * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
227a0c41d5SAlan Somers * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
237a0c41d5SAlan Somers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
247a0c41d5SAlan Somers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
257a0c41d5SAlan Somers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
267a0c41d5SAlan Somers * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
277a0c41d5SAlan Somers * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
287a0c41d5SAlan Somers * POSSIBILITY OF SUCH DAMAGES.
297a0c41d5SAlan Somers *
307a0c41d5SAlan Somers * Authors: Justin T. Gibbs (Spectra Logic Corporation)
317a0c41d5SAlan Somers */
327a0c41d5SAlan Somers
337a0c41d5SAlan Somers /**
347a0c41d5SAlan Somers * \file zfsd_exception
357a0c41d5SAlan Somers *
367a0c41d5SAlan Somers * Implementation of the ZfsdException class.
377a0c41d5SAlan Somers */
387a0c41d5SAlan Somers #include <sys/cdefs.h>
399e5787d2SMatt Macy #include <sys/byteorder.h>
407a0c41d5SAlan Somers #include <sys/fs/zfs.h>
417a0c41d5SAlan Somers
427a0c41d5SAlan Somers #include <syslog.h>
437a0c41d5SAlan Somers
447a0c41d5SAlan Somers #include <string>
457a0c41d5SAlan Somers #include <list>
467a0c41d5SAlan Somers #include <sstream>
477a0c41d5SAlan Somers
487a0c41d5SAlan Somers #include <devdctl/exception.h>
497a0c41d5SAlan Somers #include <devdctl/guid.h>
507a0c41d5SAlan Somers
517a0c41d5SAlan Somers #include <libzfs.h>
527a0c41d5SAlan Somers
537a0c41d5SAlan Somers #include "vdev.h"
547a0c41d5SAlan Somers #include "zfsd_exception.h"
557a0c41d5SAlan Somers /*============================ Namespace Control =============================*/
567a0c41d5SAlan Somers using std::endl;
577a0c41d5SAlan Somers using std::string;
587a0c41d5SAlan Somers using std::stringstream;
597a0c41d5SAlan Somers
607a0c41d5SAlan Somers /*=========================== Class Implementations ==========================*/
617a0c41d5SAlan Somers /*------------------------------- ZfsdException ------------------------------*/
ZfsdException(const char * fmt,...)627a0c41d5SAlan Somers ZfsdException::ZfsdException(const char *fmt, ...)
637a0c41d5SAlan Somers : DevdCtl::Exception(),
647a0c41d5SAlan Somers m_poolConfig(NULL),
657a0c41d5SAlan Somers m_vdevConfig(NULL)
667a0c41d5SAlan Somers {
677a0c41d5SAlan Somers va_list ap;
687a0c41d5SAlan Somers
697a0c41d5SAlan Somers va_start(ap, fmt);
707a0c41d5SAlan Somers FormatLog(fmt, ap);
717a0c41d5SAlan Somers va_end(ap);
727a0c41d5SAlan Somers }
737a0c41d5SAlan Somers
ZfsdException(zpool_handle_t * pool,const char * fmt,...)747a0c41d5SAlan Somers ZfsdException::ZfsdException(zpool_handle_t *pool, const char *fmt, ...)
757a0c41d5SAlan Somers : DevdCtl::Exception(),
767a0c41d5SAlan Somers m_poolConfig(zpool_get_config(pool, NULL)),
777a0c41d5SAlan Somers m_vdevConfig(NULL)
787a0c41d5SAlan Somers {
797a0c41d5SAlan Somers va_list ap;
807a0c41d5SAlan Somers
817a0c41d5SAlan Somers va_start(ap, fmt);
827a0c41d5SAlan Somers FormatLog(fmt, ap);
837a0c41d5SAlan Somers va_end(ap);
847a0c41d5SAlan Somers }
857a0c41d5SAlan Somers
ZfsdException(nvlist_t * poolConfig,const char * fmt,...)867a0c41d5SAlan Somers ZfsdException::ZfsdException(nvlist_t *poolConfig, const char *fmt, ...)
877a0c41d5SAlan Somers : DevdCtl::Exception(),
887a0c41d5SAlan Somers m_poolConfig(poolConfig),
897a0c41d5SAlan Somers m_vdevConfig(NULL)
907a0c41d5SAlan Somers {
917a0c41d5SAlan Somers va_list ap;
927a0c41d5SAlan Somers
937a0c41d5SAlan Somers va_start(ap, fmt);
947a0c41d5SAlan Somers FormatLog(fmt, ap);
957a0c41d5SAlan Somers va_end(ap);
967a0c41d5SAlan Somers }
977a0c41d5SAlan Somers
987a0c41d5SAlan Somers void
Log() const997a0c41d5SAlan Somers ZfsdException::Log() const
1007a0c41d5SAlan Somers {
1017a0c41d5SAlan Somers stringstream output;
1027a0c41d5SAlan Somers
1037a0c41d5SAlan Somers if (m_poolConfig != NULL) {
1047a0c41d5SAlan Somers
1057a0c41d5SAlan Somers output << "Pool ";
1067a0c41d5SAlan Somers
107*2a58b312SMartin Matuska const char *poolName;
1087a0c41d5SAlan Somers if (nvlist_lookup_string(m_poolConfig, ZPOOL_CONFIG_POOL_NAME,
1097a0c41d5SAlan Somers &poolName) == 0)
1107a0c41d5SAlan Somers output << poolName;
1117a0c41d5SAlan Somers else
1127a0c41d5SAlan Somers output << "Unknown";
1137a0c41d5SAlan Somers output << ": ";
1147a0c41d5SAlan Somers }
1157a0c41d5SAlan Somers
1167a0c41d5SAlan Somers if (m_vdevConfig != NULL) {
1177a0c41d5SAlan Somers
1187a0c41d5SAlan Somers if (m_poolConfig != NULL) {
1197a0c41d5SAlan Somers Vdev vdev(m_poolConfig, m_vdevConfig);
1207a0c41d5SAlan Somers
1217a0c41d5SAlan Somers output << "Vdev " << vdev.GUID() << ": ";
1227a0c41d5SAlan Somers } else {
1237a0c41d5SAlan Somers Vdev vdev(m_vdevConfig);
1247a0c41d5SAlan Somers
1257a0c41d5SAlan Somers output << "Pool " << vdev.PoolGUID() << ": ";
1267a0c41d5SAlan Somers output << "Vdev " << vdev.GUID() << ": ";
1277a0c41d5SAlan Somers }
1287a0c41d5SAlan Somers }
1297a0c41d5SAlan Somers
1307a0c41d5SAlan Somers output << m_log << endl;
1317a0c41d5SAlan Somers syslog(LOG_ERR, "%s", output.str().c_str());
1327a0c41d5SAlan Somers }
1337a0c41d5SAlan Somers
134