xref: /freebsd/cddl/usr.sbin/zfsd/zfsd_exception.cc (revision a7623790fb345e6dc986dfd31df0ace115e6f2e4)
1 /*-
2  * Copyright (c) 2011, 2012, 2013 Spectra Logic Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions, and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    substantially similar to the "NO WARRANTY" disclaimer below
13  *    ("Disclaimer") and any redistribution must be conditioned upon
14  *    including a substantially similar Disclaimer requirement for further
15  *    binary redistribution.
16  *
17  * NO WARRANTY
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGES.
29  *
30  * Authors: Justin T. Gibbs     (Spectra Logic Corporation)
31  */
32 
33 /**
34  * \file zfsd_exception
35  *
36  * Implementation of the ZfsdException class.
37  */
38 #include <sys/cdefs.h>
39 #include <sys/byteorder.h>
40 #include <sys/fs/zfs.h>
41 
42 #include <syslog.h>
43 
44 #include <string>
45 #include <list>
46 #include <sstream>
47 
48 #include <devdctl/exception.h>
49 #include <devdctl/guid.h>
50 
51 #include <libzfs.h>
52 
53 #include "vdev.h"
54 #include "zfsd_exception.h"
55 
56 __FBSDID("$FreeBSD$");
57 /*============================ Namespace Control =============================*/
58 using std::endl;
59 using std::string;
60 using std::stringstream;
61 
62 /*=========================== Class Implementations ==========================*/
63 /*------------------------------- ZfsdException ------------------------------*/
64 ZfsdException::ZfsdException(const char *fmt, ...)
65  : DevdCtl::Exception(),
66    m_poolConfig(NULL),
67    m_vdevConfig(NULL)
68 {
69 	va_list ap;
70 
71 	va_start(ap, fmt);
72 	FormatLog(fmt, ap);
73 	va_end(ap);
74 }
75 
76 ZfsdException::ZfsdException(zpool_handle_t *pool, const char *fmt, ...)
77  : DevdCtl::Exception(),
78    m_poolConfig(zpool_get_config(pool, NULL)),
79    m_vdevConfig(NULL)
80 {
81 	va_list ap;
82 
83 	va_start(ap, fmt);
84 	FormatLog(fmt, ap);
85 	va_end(ap);
86 }
87 
88 ZfsdException::ZfsdException(nvlist_t *poolConfig, const char *fmt, ...)
89  : DevdCtl::Exception(),
90    m_poolConfig(poolConfig),
91    m_vdevConfig(NULL)
92 {
93 	va_list ap;
94 
95 	va_start(ap, fmt);
96 	FormatLog(fmt, ap);
97 	va_end(ap);
98 }
99 
100 void
101 ZfsdException::Log() const
102 {
103 	stringstream output;
104 
105 	if (m_poolConfig != NULL) {
106 
107 		output << "Pool ";
108 
109 		char *poolName;
110 		if (nvlist_lookup_string(m_poolConfig, ZPOOL_CONFIG_POOL_NAME,
111 				     &poolName) == 0)
112 			output << poolName;
113 		else
114 			output << "Unknown";
115 		output << ": ";
116 	}
117 
118 	if (m_vdevConfig != NULL) {
119 
120 		if (m_poolConfig != NULL) {
121 			Vdev vdev(m_poolConfig, m_vdevConfig);
122 
123 			output << "Vdev " <<  vdev.GUID() << ": ";
124 		} else {
125 			Vdev vdev(m_vdevConfig);
126 
127 			output << "Pool " <<  vdev.PoolGUID() << ": ";
128 			output << "Vdev " <<  vdev.GUID() << ": ";
129 		}
130 	}
131 
132 	output << m_log << endl;
133 	syslog(LOG_ERR, "%s", output.str().c_str());
134 }
135 
136