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>
39 #include <sys/vdev_impl.h>
45 * Undefine flush, defined by cpufunc.h on sparc64, because it conflicts with
55 #include <devdctl/guid.h>
56 #include <devdctl/event.h>
57 #include <devdctl/event_factory.h>
58 #include <devdctl/exception.h>
59 #include <devdctl/consumer.h>
62 #include "vdev_iterator.h"
63 #include "zfsd_event.h"
64 #include "case_file.h"
67 #include "zfsd_exception.h"
68 #include "zpool_list.h"
70 __FBSDID("$FreeBSD$");
71 /*============================ Namespace Control =============================*/
74 using DevdCtl::NVPairMap;
75 using std::stringstream;
77 /*=========================== Class Implementations ==========================*/
79 /*-------------------------------- DevfsEvent --------------------------------*/
81 //- DevfsEvent Static Public Methods -------------------------------------------
83 DevfsEvent::Builder(Event::Type type,
85 const string &eventString)
87 return (new DevfsEvent(type, nvPairs, eventString));
90 //- DevfsEvent Static Protected Methods ----------------------------------------
92 DevfsEvent::ReadLabel(int devFd, bool &inUse, bool °raded)
94 pool_state_t poolState;
102 if (zpool_in_use(g_zfsHandle, devFd, &poolState,
103 &poolName, &b_inuse) == 0) {
106 inUse = b_inuse == B_TRUE;
107 if (poolName != NULL)
110 nlabels = zpool_read_all_labels(devFd, &devLabel);
112 * If we find a disk with fewer than the maximum number of
113 * labels, it might be the whole disk of a partitioned disk
114 * where ZFS resides on a partition. In that case, we should do
115 * nothing and wait for the partition to appear. Or, the disk
116 * might be damaged. In that case, zfsd should do nothing and
117 * wait for the sysadmin to decide.
119 if (nlabels != VDEV_LABELS || devLabel == NULL)
124 degraded = vdev.State() != VDEV_STATE_HEALTHY;
126 } catch (ZfsdException &exp) {
127 string devName = fdevname(devFd);
128 string devPath = _PATH_DEV + devName;
129 string context("DevfsEvent::ReadLabel: "
132 exp.GetString().insert(0, context);
140 DevfsEvent::OnlineByLabel(const string &devPath, const string& physPath,
145 * A device with ZFS label information has been
146 * inserted. If it matches a device for which we
147 * have a case, see if we can solve that case.
149 syslog(LOG_INFO, "Interrogating VDEV label for %s\n",
151 Vdev vdev(devConfig);
152 CaseFile *caseFile(CaseFile::Find(vdev.PoolGUID(),
154 if (caseFile != NULL)
155 return (caseFile->ReEvaluate(devPath, physPath, &vdev));
157 } catch (ZfsdException &exp) {
158 string context("DevfsEvent::OnlineByLabel: " + devPath + ": ");
160 exp.GetString().insert(0, context);
166 //- DevfsEvent Virtual Public Methods ------------------------------------------
168 DevfsEvent::DeepCopy() const
170 return (new DevfsEvent(*this));
174 DevfsEvent::Process() const
177 * We are only concerned with newly discovered
178 * devices that can be ZFS vdevs.
180 if (Value("type") != "CREATE" || !IsDiskDev())
183 /* Log the event since it is of interest. */
187 if (!DevPath(devPath))
190 int devFd(open(devPath.c_str(), O_RDONLY));
196 nvlist_t *devLabel(ReadLabel(devFd, inUse, degraded));
199 bool havePhysPath(PhysicalPath(physPath));
205 if (inUse && devLabel != NULL) {
206 OnlineByLabel(devPath, physPath, devLabel);
207 } else if (degraded) {
208 syslog(LOG_INFO, "%s is marked degraded. Ignoring "
209 "as a replace by physical path candidate.\n",
211 } else if (havePhysPath && IsWholeDev()) {
213 * TODO: attempt to resolve events using every casefile
214 * that matches this physpath
216 CaseFile *caseFile(CaseFile::Find(physPath));
217 if (caseFile != NULL) {
219 "Found CaseFile(%s:%s:%s) - ReEvaluating\n",
220 caseFile->PoolGUIDString().c_str(),
221 caseFile->VdevGUIDString().c_str(),
222 zpool_state_to_name(caseFile->VdevState(),
224 caseFile->ReEvaluate(devPath, physPath, /*vdev*/NULL);
227 if (devLabel != NULL)
228 nvlist_free(devLabel);
232 //- DevfsEvent Protected Methods -----------------------------------------------
233 DevfsEvent::DevfsEvent(Event::Type type, NVPairMap &nvpairs,
234 const string &eventString)
235 : DevdCtl::DevfsEvent(type, nvpairs, eventString)
239 DevfsEvent::DevfsEvent(const DevfsEvent &src)
240 : DevdCtl::DevfsEvent::DevfsEvent(src)
244 /*-------------------------------- GeomEvent --------------------------------*/
246 //- GeomEvent Static Public Methods -------------------------------------------
248 GeomEvent::Builder(Event::Type type,
250 const string &eventString)
252 return (new GeomEvent(type, nvPairs, eventString));
255 //- GeomEvent Virtual Public Methods ------------------------------------------
257 GeomEvent::DeepCopy() const
259 return (new GeomEvent(*this));
263 GeomEvent::Process() const
266 * We are only concerned with physical path changes, because those can
267 * be used to satisfy autoreplace operations
269 if (Value("type") != "GEOM::physpath" || !IsDiskDev())
272 /* Log the event since it is of interest. */
276 if (!DevPath(devPath))
280 bool havePhysPath(PhysicalPath(physPath));
287 * TODO: attempt to resolve events using every casefile
288 * that matches this physpath
290 CaseFile *caseFile(CaseFile::Find(physPath));
291 if (caseFile != NULL) {
293 "Found CaseFile(%s:%s:%s) - ReEvaluating\n",
294 caseFile->PoolGUIDString().c_str(),
295 caseFile->VdevGUIDString().c_str(),
296 zpool_state_to_name(caseFile->VdevState(),
298 caseFile->ReEvaluate(devPath, physPath, /*vdev*/NULL);
304 //- GeomEvent Protected Methods -----------------------------------------------
305 GeomEvent::GeomEvent(Event::Type type, NVPairMap &nvpairs,
306 const string &eventString)
307 : DevdCtl::GeomEvent(type, nvpairs, eventString)
311 GeomEvent::GeomEvent(const GeomEvent &src)
312 : DevdCtl::GeomEvent::GeomEvent(src)
317 /*--------------------------------- ZfsEvent ---------------------------------*/
318 //- ZfsEvent Static Public Methods ---------------------------------------------
320 ZfsEvent::Builder(Event::Type type, NVPairMap &nvpairs,
321 const string &eventString)
323 return (new ZfsEvent(type, nvpairs, eventString));
326 //- ZfsEvent Virtual Public Methods --------------------------------------------
328 ZfsEvent::DeepCopy() const
330 return (new ZfsEvent(*this));
334 ZfsEvent::Process() const
338 if (!Contains("class") && !Contains("type")) {
340 "ZfsEvent::Process: Missing class or type data.");
344 /* On config syncs, replay any queued events first. */
345 if (Value("type").find("misc.fs.zfs.config_sync") == 0) {
347 * Even if saved events are unconsumed the second time
348 * around, drop them. Any events that still can't be
349 * consumed are probably referring to vdevs or pools that
352 ZfsDaemon::Get().ReplayUnconsumedEvents(/*discard*/true);
353 CaseFile::ReEvaluateByGuid(PoolGUID(), *this);
356 if (Value("type").find("misc.fs.zfs.") == 0) {
357 /* Configuration changes, resilver events, etc. */
362 if (!Contains("pool_guid") || !Contains("vdev_guid")) {
363 /* Only currently interested in Vdev related events. */
367 CaseFile *caseFile(CaseFile::Find(PoolGUID(), VdevGUID()));
368 if (caseFile != NULL) {
370 syslog(LOG_INFO, "Evaluating existing case file\n");
371 caseFile->ReEvaluate(*this);
375 /* Skip events that can't be handled. */
376 Guid poolGUID(PoolGUID());
377 /* If there are no replicas for a pool, then it's not manageable. */
378 if (Value("class").find("fs.zfs.vdev.no_replicas") == 0) {
380 msg << "No replicas available for pool " << poolGUID;
383 syslog(LOG_INFO, "%s", msg.str().c_str());
388 * Create a case file for this vdev, and have it
389 * evaluate the event.
391 ZpoolList zpl(ZpoolList::ZpoolByGUID, &poolGUID);
394 int priority = LOG_INFO;
395 msg << "ZfsEvent::Process: Event for unknown pool ";
396 msg << poolGUID << " ";
399 syslog(priority, "%s", msg.str().c_str());
403 nvlist_t *vdevConfig = VdevIterator(zpl.front()).Find(VdevGUID());
404 if (vdevConfig == NULL) {
406 int priority = LOG_INFO;
407 msg << "ZfsEvent::Process: Event for unknown vdev ";
408 msg << VdevGUID() << " ";
411 syslog(priority, "%s", msg.str().c_str());
415 Vdev vdev(zpl.front(), vdevConfig);
416 caseFile = &CaseFile::Create(vdev);
417 if (caseFile->ReEvaluate(*this) == false) {
419 int priority = LOG_INFO;
420 msg << "ZfsEvent::Process: Unconsumed event for vdev(";
421 msg << zpool_get_name(zpl.front()) << ",";
422 msg << vdev.GUID() << ") ";
425 syslog(priority, "%s", msg.str().c_str());
431 //- ZfsEvent Protected Methods -------------------------------------------------
432 ZfsEvent::ZfsEvent(Event::Type type, NVPairMap &nvpairs,
433 const string &eventString)
434 : DevdCtl::ZfsEvent(type, nvpairs, eventString)
438 ZfsEvent::ZfsEvent(const ZfsEvent &src)
439 : DevdCtl::ZfsEvent(src)
444 * Sometimes the kernel won't detach a spare when it is no longer needed. This
445 * can happen for example if a drive is removed, then either the pool is
446 * exported or the machine is powered off, then the drive is reinserted, then
447 * the machine is powered on or the pool is imported. ZFSD must detach these
451 ZfsEvent::CleanupSpares() const
453 Guid poolGUID(PoolGUID());
454 ZpoolList zpl(ZpoolList::ZpoolByGUID, &poolGUID);
459 VdevIterator(hdl).Each(TryDetach, (void*)hdl);
464 ZfsEvent::ProcessPoolEvent() const
466 bool degradedDevice(false);
468 /* The pool is destroyed. Discard any open cases */
469 if (Value("type") == "misc.fs.zfs.pool_destroy") {
471 CaseFile::ReEvaluateByGuid(PoolGUID(), *this);
475 CaseFile *caseFile(CaseFile::Find(PoolGUID(), VdevGUID()));
476 if (caseFile != NULL) {
477 if (caseFile->VdevState() != VDEV_STATE_UNKNOWN
478 && caseFile->VdevState() < VDEV_STATE_HEALTHY)
479 degradedDevice = true;
482 caseFile->ReEvaluate(*this);
484 else if (Value("type") == "misc.fs.zfs.resilver_finish")
487 * It's possible to get a resilver_finish event with no
488 * corresponding casefile. For example, if a damaged pool were
489 * exported, repaired, then reimported.
495 if (Value("type") == "misc.fs.zfs.vdev_remove"
496 && degradedDevice == false) {
498 /* See if any other cases can make use of this device. */
500 ZfsDaemon::RequestSystemRescan();
505 ZfsEvent::TryDetach(Vdev &vdev, void *cbArg)
509 * if this device is a spare, and its parent includes one healthy,
510 * non-spare child, then detach this device.
512 zpool_handle_t *hdl(static_cast<zpool_handle_t*>(cbArg));
514 if (vdev.IsSpare()) {
515 std::list<Vdev> siblings;
516 std::list<Vdev>::iterator siblings_it;
517 boolean_t cleanup = B_FALSE;
519 Vdev parent = vdev.Parent();
520 siblings = parent.Children();
522 /* Determine whether the parent should be cleaned up */
523 for (siblings_it = siblings.begin();
524 siblings_it != siblings.end();
526 Vdev sibling = *siblings_it;
528 if (!sibling.IsSpare() &&
529 sibling.State() == VDEV_STATE_HEALTHY) {
536 syslog(LOG_INFO, "Detaching spare vdev %s from pool %s",
537 vdev.Path().c_str(), zpool_get_name(hdl));
538 zpool_vdev_detach(hdl, vdev.Path().c_str());
543 /* Always return false, because there may be other spares to detach */