2 * Copyright (c) 2011, 2012, 2013, 2014, 2016 Spectra Logic Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
30 * Authors: Justin T. Gibbs (Spectra Logic Corporation)
36 #include <sys/cdefs.h>
38 #include <sys/fs/zfs.h>
44 * Undefine flush, defined by cpufunc.h on sparc64, because it conflicts with
54 #include <devdctl/guid.h>
55 #include <devdctl/event.h>
56 #include <devdctl/event_factory.h>
57 #include <devdctl/exception.h>
58 #include <devdctl/consumer.h>
61 #include "vdev_iterator.h"
62 #include "zfsd_event.h"
63 #include "case_file.h"
66 #include "zfsd_exception.h"
67 #include "zpool_list.h"
69 __FBSDID("$FreeBSD$");
70 /*============================ Namespace Control =============================*/
73 using DevdCtl::NVPairMap;
74 using std::stringstream;
76 /*=========================== Class Implementations ==========================*/
78 /*-------------------------------- DevfsEvent --------------------------------*/
80 //- DevfsEvent Static Public Methods -------------------------------------------
82 DevfsEvent::Builder(Event::Type type,
84 const string &eventString)
86 return (new DevfsEvent(type, nvPairs, eventString));
89 //- DevfsEvent Static Protected Methods ----------------------------------------
91 DevfsEvent::ReadLabel(int devFd, bool &inUse, bool °raded)
93 pool_state_t poolState;
100 if (zpool_in_use(g_zfsHandle, devFd, &poolState,
101 &poolName, &b_inuse) == 0) {
104 inUse = b_inuse == B_TRUE;
105 if (poolName != NULL)
108 if (zpool_read_label(devFd, &devLabel) != 0
114 degraded = vdev.State() != VDEV_STATE_HEALTHY;
116 } catch (ZfsdException &exp) {
117 string devName = fdevname(devFd);
118 string devPath = _PATH_DEV + devName;
119 string context("DevfsEvent::ReadLabel: "
122 exp.GetString().insert(0, context);
130 DevfsEvent::OnlineByLabel(const string &devPath, const string& physPath,
135 * A device with ZFS label information has been
136 * inserted. If it matches a device for which we
137 * have a case, see if we can solve that case.
139 syslog(LOG_INFO, "Interrogating VDEV label for %s\n",
141 Vdev vdev(devConfig);
142 CaseFile *caseFile(CaseFile::Find(vdev.PoolGUID(),
144 if (caseFile != NULL)
145 return (caseFile->ReEvaluate(devPath, physPath, &vdev));
147 } catch (ZfsdException &exp) {
148 string context("DevfsEvent::OnlineByLabel: " + devPath + ": ");
150 exp.GetString().insert(0, context);
156 //- DevfsEvent Virtual Public Methods ------------------------------------------
158 DevfsEvent::DeepCopy() const
160 return (new DevfsEvent(*this));
164 DevfsEvent::Process() const
167 * We are only concerned with newly discovered
168 * devices that can be ZFS vdevs.
170 if (Value("type") != "CREATE" || !IsDiskDev())
173 /* Log the event since it is of interest. */
177 if (!DevPath(devPath))
180 int devFd(open(devPath.c_str(), O_RDONLY));
186 nvlist_t *devLabel(ReadLabel(devFd, inUse, degraded));
189 bool havePhysPath(PhysicalPath(physPath));
195 if (inUse && devLabel != NULL) {
196 OnlineByLabel(devPath, physPath, devLabel);
197 } else if (degraded) {
198 syslog(LOG_INFO, "%s is marked degraded. Ignoring "
199 "as a replace by physical path candidate.\n",
201 } else if (havePhysPath && IsWholeDev()) {
203 * TODO: attempt to resolve events using every casefile
204 * that matches this physpath
206 CaseFile *caseFile(CaseFile::Find(physPath));
207 if (caseFile != NULL) {
209 "Found CaseFile(%s:%s:%s) - ReEvaluating\n",
210 caseFile->PoolGUIDString().c_str(),
211 caseFile->VdevGUIDString().c_str(),
212 zpool_state_to_name(caseFile->VdevState(),
214 caseFile->ReEvaluate(devPath, physPath, /*vdev*/NULL);
217 if (devLabel != NULL)
218 nvlist_free(devLabel);
222 //- DevfsEvent Protected Methods -----------------------------------------------
223 DevfsEvent::DevfsEvent(Event::Type type, NVPairMap &nvpairs,
224 const string &eventString)
225 : DevdCtl::DevfsEvent(type, nvpairs, eventString)
229 DevfsEvent::DevfsEvent(const DevfsEvent &src)
230 : DevdCtl::DevfsEvent::DevfsEvent(src)
234 /*-------------------------------- GeomEvent --------------------------------*/
236 //- GeomEvent Static Public Methods -------------------------------------------
238 GeomEvent::Builder(Event::Type type,
240 const string &eventString)
242 return (new GeomEvent(type, nvPairs, eventString));
245 //- GeomEvent Virtual Public Methods ------------------------------------------
247 GeomEvent::DeepCopy() const
249 return (new GeomEvent(*this));
253 GeomEvent::Process() const
256 * We are only concerned with physical path changes, because those can
257 * be used to satisfy autoreplace operations
259 if (Value("type") != "GEOM::physpath" || !IsDiskDev())
262 /* Log the event since it is of interest. */
266 if (!DevPath(devPath))
270 bool havePhysPath(PhysicalPath(physPath));
277 * TODO: attempt to resolve events using every casefile
278 * that matches this physpath
280 CaseFile *caseFile(CaseFile::Find(physPath));
281 if (caseFile != NULL) {
283 "Found CaseFile(%s:%s:%s) - ReEvaluating\n",
284 caseFile->PoolGUIDString().c_str(),
285 caseFile->VdevGUIDString().c_str(),
286 zpool_state_to_name(caseFile->VdevState(),
288 caseFile->ReEvaluate(devPath, physPath, /*vdev*/NULL);
294 //- GeomEvent Protected Methods -----------------------------------------------
295 GeomEvent::GeomEvent(Event::Type type, NVPairMap &nvpairs,
296 const string &eventString)
297 : DevdCtl::GeomEvent(type, nvpairs, eventString)
301 GeomEvent::GeomEvent(const GeomEvent &src)
302 : DevdCtl::GeomEvent::GeomEvent(src)
307 /*--------------------------------- ZfsEvent ---------------------------------*/
308 //- ZfsEvent Static Public Methods ---------------------------------------------
310 ZfsEvent::Builder(Event::Type type, NVPairMap &nvpairs,
311 const string &eventString)
313 return (new ZfsEvent(type, nvpairs, eventString));
316 //- ZfsEvent Virtual Public Methods --------------------------------------------
318 ZfsEvent::DeepCopy() const
320 return (new ZfsEvent(*this));
324 ZfsEvent::Process() const
328 if (!Contains("class") && !Contains("type")) {
330 "ZfsEvent::Process: Missing class or type data.");
334 /* On config syncs, replay any queued events first. */
335 if (Value("type").find("misc.fs.zfs.config_sync") == 0) {
337 * Even if saved events are unconsumed the second time
338 * around, drop them. Any events that still can't be
339 * consumed are probably referring to vdevs or pools that
342 ZfsDaemon::Get().ReplayUnconsumedEvents(/*discard*/true);
343 CaseFile::ReEvaluateByGuid(PoolGUID(), *this);
346 if (Value("type").find("misc.fs.zfs.") == 0) {
347 /* Configuration changes, resilver events, etc. */
352 if (!Contains("pool_guid") || !Contains("vdev_guid")) {
353 /* Only currently interested in Vdev related events. */
357 CaseFile *caseFile(CaseFile::Find(PoolGUID(), VdevGUID()));
358 if (caseFile != NULL) {
360 syslog(LOG_INFO, "Evaluating existing case file\n");
361 caseFile->ReEvaluate(*this);
365 /* Skip events that can't be handled. */
366 Guid poolGUID(PoolGUID());
367 /* If there are no replicas for a pool, then it's not manageable. */
368 if (Value("class").find("fs.zfs.vdev.no_replicas") == 0) {
370 msg << "No replicas available for pool " << poolGUID;
373 syslog(LOG_INFO, "%s", msg.str().c_str());
378 * Create a case file for this vdev, and have it
379 * evaluate the event.
381 ZpoolList zpl(ZpoolList::ZpoolByGUID, &poolGUID);
384 int priority = LOG_INFO;
385 msg << "ZfsEvent::Process: Event for unknown pool ";
386 msg << poolGUID << " ";
389 syslog(priority, "%s", msg.str().c_str());
393 nvlist_t *vdevConfig = VdevIterator(zpl.front()).Find(VdevGUID());
394 if (vdevConfig == NULL) {
396 int priority = LOG_INFO;
397 msg << "ZfsEvent::Process: Event for unknown vdev ";
398 msg << VdevGUID() << " ";
401 syslog(priority, "%s", msg.str().c_str());
405 Vdev vdev(zpl.front(), vdevConfig);
406 caseFile = &CaseFile::Create(vdev);
407 if (caseFile->ReEvaluate(*this) == false) {
409 int priority = LOG_INFO;
410 msg << "ZfsEvent::Process: Unconsumed event for vdev(";
411 msg << zpool_get_name(zpl.front()) << ",";
412 msg << vdev.GUID() << ") ";
415 syslog(priority, "%s", msg.str().c_str());
421 //- ZfsEvent Protected Methods -------------------------------------------------
422 ZfsEvent::ZfsEvent(Event::Type type, NVPairMap &nvpairs,
423 const string &eventString)
424 : DevdCtl::ZfsEvent(type, nvpairs, eventString)
428 ZfsEvent::ZfsEvent(const ZfsEvent &src)
429 : DevdCtl::ZfsEvent(src)
434 * Sometimes the kernel won't detach a spare when it is no longer needed. This
435 * can happen for example if a drive is removed, then either the pool is
436 * exported or the machine is powered off, then the drive is reinserted, then
437 * the machine is powered on or the pool is imported. ZFSD must detach these
441 ZfsEvent::CleanupSpares() const
443 Guid poolGUID(PoolGUID());
444 ZpoolList zpl(ZpoolList::ZpoolByGUID, &poolGUID);
449 VdevIterator(hdl).Each(TryDetach, (void*)hdl);
454 ZfsEvent::ProcessPoolEvent() const
456 bool degradedDevice(false);
458 /* The pool is destroyed. Discard any open cases */
459 if (Value("type") == "misc.fs.zfs.pool_destroy") {
461 CaseFile::ReEvaluateByGuid(PoolGUID(), *this);
465 CaseFile *caseFile(CaseFile::Find(PoolGUID(), VdevGUID()));
466 if (caseFile != NULL) {
467 if (caseFile->VdevState() != VDEV_STATE_UNKNOWN
468 && caseFile->VdevState() < VDEV_STATE_HEALTHY)
469 degradedDevice = true;
472 caseFile->ReEvaluate(*this);
474 else if (Value("type") == "misc.fs.zfs.resilver_finish")
477 * It's possible to get a resilver_finish event with no
478 * corresponding casefile. For example, if a damaged pool were
479 * exported, repaired, then reimported.
485 if (Value("type") == "misc.fs.zfs.vdev_remove"
486 && degradedDevice == false) {
488 /* See if any other cases can make use of this device. */
490 ZfsDaemon::RequestSystemRescan();
495 ZfsEvent::TryDetach(Vdev &vdev, void *cbArg)
499 * if this device is a spare, and its parent includes one healthy,
500 * non-spare child, then detach this device.
502 zpool_handle_t *hdl(static_cast<zpool_handle_t*>(cbArg));
504 if (vdev.IsSpare()) {
505 std::list<Vdev> siblings;
506 std::list<Vdev>::iterator siblings_it;
507 boolean_t cleanup = B_FALSE;
509 Vdev parent = vdev.Parent();
510 siblings = parent.Children();
512 /* Determine whether the parent should be cleaned up */
513 for (siblings_it = siblings.begin();
514 siblings_it != siblings.end();
516 Vdev sibling = *siblings_it;
518 if (!sibling.IsSpare() &&
519 sibling.State() == VDEV_STATE_HEALTHY) {
526 syslog(LOG_INFO, "Detaching spare vdev %s from pool %s",
527 vdev.Path().c_str(), zpool_get_name(hdl));
528 zpool_vdev_detach(hdl, vdev.Path().c_str());
533 /* Always return false, because there may be other spares to detach */