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 567a0c41d5SAlan Somers __FBSDID("$FreeBSD$"); 577a0c41d5SAlan Somers /*============================ Namespace Control =============================*/ 587a0c41d5SAlan Somers using std::endl; 597a0c41d5SAlan Somers using std::string; 607a0c41d5SAlan Somers using std::stringstream; 617a0c41d5SAlan Somers 627a0c41d5SAlan Somers /*=========================== Class Implementations ==========================*/ 637a0c41d5SAlan Somers /*------------------------------- ZfsdException ------------------------------*/ 647a0c41d5SAlan Somers ZfsdException::ZfsdException(const char *fmt, ...) 657a0c41d5SAlan Somers : DevdCtl::Exception(), 667a0c41d5SAlan Somers m_poolConfig(NULL), 677a0c41d5SAlan Somers m_vdevConfig(NULL) 687a0c41d5SAlan Somers { 697a0c41d5SAlan Somers va_list ap; 707a0c41d5SAlan Somers 717a0c41d5SAlan Somers va_start(ap, fmt); 727a0c41d5SAlan Somers FormatLog(fmt, ap); 737a0c41d5SAlan Somers va_end(ap); 747a0c41d5SAlan Somers } 757a0c41d5SAlan Somers 767a0c41d5SAlan Somers ZfsdException::ZfsdException(zpool_handle_t *pool, const char *fmt, ...) 777a0c41d5SAlan Somers : DevdCtl::Exception(), 787a0c41d5SAlan Somers m_poolConfig(zpool_get_config(pool, NULL)), 797a0c41d5SAlan Somers m_vdevConfig(NULL) 807a0c41d5SAlan Somers { 817a0c41d5SAlan Somers va_list ap; 827a0c41d5SAlan Somers 837a0c41d5SAlan Somers va_start(ap, fmt); 847a0c41d5SAlan Somers FormatLog(fmt, ap); 857a0c41d5SAlan Somers va_end(ap); 867a0c41d5SAlan Somers } 877a0c41d5SAlan Somers 887a0c41d5SAlan Somers ZfsdException::ZfsdException(nvlist_t *poolConfig, const char *fmt, ...) 897a0c41d5SAlan Somers : DevdCtl::Exception(), 907a0c41d5SAlan Somers m_poolConfig(poolConfig), 917a0c41d5SAlan Somers m_vdevConfig(NULL) 927a0c41d5SAlan Somers { 937a0c41d5SAlan Somers va_list ap; 947a0c41d5SAlan Somers 957a0c41d5SAlan Somers va_start(ap, fmt); 967a0c41d5SAlan Somers FormatLog(fmt, ap); 977a0c41d5SAlan Somers va_end(ap); 987a0c41d5SAlan Somers } 997a0c41d5SAlan Somers 1007a0c41d5SAlan Somers void 1017a0c41d5SAlan Somers ZfsdException::Log() const 1027a0c41d5SAlan Somers { 1037a0c41d5SAlan Somers stringstream output; 1047a0c41d5SAlan Somers 1057a0c41d5SAlan Somers if (m_poolConfig != NULL) { 1067a0c41d5SAlan Somers 1077a0c41d5SAlan Somers output << "Pool "; 1087a0c41d5SAlan Somers 109*2a58b312SMartin Matuska const char *poolName; 1107a0c41d5SAlan Somers if (nvlist_lookup_string(m_poolConfig, ZPOOL_CONFIG_POOL_NAME, 1117a0c41d5SAlan Somers &poolName) == 0) 1127a0c41d5SAlan Somers output << poolName; 1137a0c41d5SAlan Somers else 1147a0c41d5SAlan Somers output << "Unknown"; 1157a0c41d5SAlan Somers output << ": "; 1167a0c41d5SAlan Somers } 1177a0c41d5SAlan Somers 1187a0c41d5SAlan Somers if (m_vdevConfig != NULL) { 1197a0c41d5SAlan Somers 1207a0c41d5SAlan Somers if (m_poolConfig != NULL) { 1217a0c41d5SAlan Somers Vdev vdev(m_poolConfig, m_vdevConfig); 1227a0c41d5SAlan Somers 1237a0c41d5SAlan Somers output << "Vdev " << vdev.GUID() << ": "; 1247a0c41d5SAlan Somers } else { 1257a0c41d5SAlan Somers Vdev vdev(m_vdevConfig); 1267a0c41d5SAlan Somers 1277a0c41d5SAlan Somers output << "Pool " << vdev.PoolGUID() << ": "; 1287a0c41d5SAlan Somers output << "Vdev " << vdev.GUID() << ": "; 1297a0c41d5SAlan Somers } 1307a0c41d5SAlan Somers } 1317a0c41d5SAlan Somers 1327a0c41d5SAlan Somers output << m_log << endl; 1337a0c41d5SAlan Somers syslog(LOG_ERR, "%s", output.str().c_str()); 1347a0c41d5SAlan Somers } 1357a0c41d5SAlan Somers 136