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 /*-------------------------------- GeomEvent --------------------------------*/
81 //- GeomEvent Static Public Methods -------------------------------------------
83 GeomEvent::Builder(Event::Type type,
85 const string &eventString)
87 return (new GeomEvent(type, nvPairs, eventString));
90 //- GeomEvent Virtual Public Methods ------------------------------------------
92 GeomEvent::DeepCopy() const
94 return (new GeomEvent(*this));
98 GeomEvent::Process() const
101 * We only use GEOM events to repair damaged pools. So return early if
102 * there are no damaged pools
104 if (CaseFile::Empty())
108 * We are only concerned with arrivals and physical path changes,
109 * because those can be used to satisfy online and autoreplace
112 if (Value("type") != "GEOM::physpath" && Value("type") != "CREATE")
115 /* Log the event since it is of interest. */
119 if (!DevPath(devPath))
122 int devFd(open(devPath.c_str(), O_RDONLY));
128 nvlist_t *devLabel(ReadLabel(devFd, inUse, degraded));
131 bool havePhysPath(PhysicalPath(physPath));
137 if (inUse && devLabel != NULL) {
138 OnlineByLabel(devPath, physPath, devLabel);
139 } else if (degraded) {
140 syslog(LOG_INFO, "%s is marked degraded. Ignoring "
141 "as a replace by physical path candidate.\n",
143 } else if (havePhysPath) {
145 * TODO: attempt to resolve events using every casefile
146 * that matches this physpath
148 CaseFile *caseFile(CaseFile::Find(physPath));
149 if (caseFile != NULL) {
151 "Found CaseFile(%s:%s:%s) - ReEvaluating\n",
152 caseFile->PoolGUIDString().c_str(),
153 caseFile->VdevGUIDString().c_str(),
154 zpool_state_to_name(caseFile->VdevState(),
156 caseFile->ReEvaluate(devPath, physPath, /*vdev*/NULL);
162 //- GeomEvent Protected Methods -----------------------------------------------
163 GeomEvent::GeomEvent(Event::Type type, NVPairMap &nvpairs,
164 const string &eventString)
165 : DevdCtl::GeomEvent(type, nvpairs, eventString)
169 GeomEvent::GeomEvent(const GeomEvent &src)
170 : DevdCtl::GeomEvent::GeomEvent(src)
175 GeomEvent::ReadLabel(int devFd, bool &inUse, bool °raded)
177 pool_state_t poolState;
185 if (zpool_in_use(g_zfsHandle, devFd, &poolState,
186 &poolName, &b_inuse) == 0) {
187 nvlist_t *devLabel = NULL;
189 inUse = b_inuse == B_TRUE;
190 if (poolName != NULL)
193 nlabels = zpool_read_all_labels(devFd, &devLabel);
195 * If we find a disk with fewer than the maximum number of
196 * labels, it might be the whole disk of a partitioned disk
197 * where ZFS resides on a partition. In that case, we should do
198 * nothing and wait for the partition to appear. Or, the disk
199 * might be damaged. In that case, zfsd should do nothing and
200 * wait for the sysadmin to decide.
202 if (nlabels != VDEV_LABELS || devLabel == NULL) {
203 nvlist_free(devLabel);
209 degraded = vdev.State() != VDEV_STATE_HEALTHY;
211 } catch (ZfsdException &exp) {
212 string devName = fdevname(devFd);
213 string devPath = _PATH_DEV + devName;
214 string context("GeomEvent::ReadLabel: "
217 exp.GetString().insert(0, context);
219 nvlist_free(devLabel);
226 GeomEvent::OnlineByLabel(const string &devPath, const string& physPath,
231 * A device with ZFS label information has been
232 * inserted. If it matches a device for which we
233 * have a case, see if we can solve that case.
235 syslog(LOG_INFO, "Interrogating VDEV label for %s\n",
237 Vdev vdev(devConfig);
238 CaseFile *caseFile(CaseFile::Find(vdev.PoolGUID(),
240 if (caseFile != NULL)
241 return (caseFile->ReEvaluate(devPath, physPath, &vdev));
243 } catch (ZfsdException &exp) {
244 string context("GeomEvent::OnlineByLabel: " + devPath + ": ");
246 exp.GetString().insert(0, context);
253 /*--------------------------------- ZfsEvent ---------------------------------*/
254 //- ZfsEvent Static Public Methods ---------------------------------------------
256 ZfsEvent::Builder(Event::Type type, NVPairMap &nvpairs,
257 const string &eventString)
259 return (new ZfsEvent(type, nvpairs, eventString));
262 //- ZfsEvent Virtual Public Methods --------------------------------------------
264 ZfsEvent::DeepCopy() const
266 return (new ZfsEvent(*this));
270 ZfsEvent::Process() const
274 if (!Contains("class") && !Contains("type")) {
276 "ZfsEvent::Process: Missing class or type data.");
280 /* On config syncs, replay any queued events first. */
281 if (Value("type").find("misc.fs.zfs.config_sync") == 0) {
283 * Even if saved events are unconsumed the second time
284 * around, drop them. Any events that still can't be
285 * consumed are probably referring to vdevs or pools that
288 ZfsDaemon::Get().ReplayUnconsumedEvents(/*discard*/true);
289 CaseFile::ReEvaluateByGuid(PoolGUID(), *this);
292 if (Value("type").find("misc.fs.zfs.") == 0) {
293 /* Configuration changes, resilver events, etc. */
298 if (!Contains("pool_guid") || !Contains("vdev_guid")) {
299 /* Only currently interested in Vdev related events. */
303 CaseFile *caseFile(CaseFile::Find(PoolGUID(), VdevGUID()));
304 if (caseFile != NULL) {
306 syslog(LOG_INFO, "Evaluating existing case file\n");
307 caseFile->ReEvaluate(*this);
311 /* Skip events that can't be handled. */
312 Guid poolGUID(PoolGUID());
313 /* If there are no replicas for a pool, then it's not manageable. */
314 if (Value("class").find("fs.zfs.vdev.no_replicas") == 0) {
316 msg << "No replicas available for pool " << poolGUID;
319 syslog(LOG_INFO, "%s", msg.str().c_str());
324 * Create a case file for this vdev, and have it
325 * evaluate the event.
327 ZpoolList zpl(ZpoolList::ZpoolByGUID, &poolGUID);
330 int priority = LOG_INFO;
331 msg << "ZfsEvent::Process: Event for unknown pool ";
332 msg << poolGUID << " ";
335 syslog(priority, "%s", msg.str().c_str());
339 nvlist_t *vdevConfig = VdevIterator(zpl.front()).Find(VdevGUID());
340 if (vdevConfig == NULL) {
342 int priority = LOG_INFO;
343 msg << "ZfsEvent::Process: Event for unknown vdev ";
344 msg << VdevGUID() << " ";
347 syslog(priority, "%s", msg.str().c_str());
351 Vdev vdev(zpl.front(), vdevConfig);
352 caseFile = &CaseFile::Create(vdev);
353 if (caseFile->ReEvaluate(*this) == false) {
355 int priority = LOG_INFO;
356 msg << "ZfsEvent::Process: Unconsumed event for vdev(";
357 msg << zpool_get_name(zpl.front()) << ",";
358 msg << vdev.GUID() << ") ";
361 syslog(priority, "%s", msg.str().c_str());
367 //- ZfsEvent Protected Methods -------------------------------------------------
368 ZfsEvent::ZfsEvent(Event::Type type, NVPairMap &nvpairs,
369 const string &eventString)
370 : DevdCtl::ZfsEvent(type, nvpairs, eventString)
374 ZfsEvent::ZfsEvent(const ZfsEvent &src)
375 : DevdCtl::ZfsEvent(src)
380 * Sometimes the kernel won't detach a spare when it is no longer needed. This
381 * can happen for example if a drive is removed, then either the pool is
382 * exported or the machine is powered off, then the drive is reinserted, then
383 * the machine is powered on or the pool is imported. ZFSD must detach these
387 ZfsEvent::CleanupSpares() const
389 Guid poolGUID(PoolGUID());
390 ZpoolList zpl(ZpoolList::ZpoolByGUID, &poolGUID);
395 VdevIterator(hdl).Each(TryDetach, (void*)hdl);
400 ZfsEvent::ProcessPoolEvent() const
402 bool degradedDevice(false);
404 /* The pool is destroyed. Discard any open cases */
405 if (Value("type") == "misc.fs.zfs.pool_destroy") {
407 CaseFile::ReEvaluateByGuid(PoolGUID(), *this);
411 CaseFile *caseFile(CaseFile::Find(PoolGUID(), VdevGUID()));
412 if (caseFile != NULL) {
413 if (caseFile->VdevState() != VDEV_STATE_UNKNOWN
414 && caseFile->VdevState() < VDEV_STATE_HEALTHY)
415 degradedDevice = true;
418 caseFile->ReEvaluate(*this);
420 else if (Value("type") == "misc.fs.zfs.resilver_finish")
423 * It's possible to get a resilver_finish event with no
424 * corresponding casefile. For example, if a damaged pool were
425 * exported, repaired, then reimported.
431 if (Value("type") == "misc.fs.zfs.vdev_remove"
432 && degradedDevice == false) {
434 /* See if any other cases can make use of this device. */
436 ZfsDaemon::RequestSystemRescan();
441 ZfsEvent::TryDetach(Vdev &vdev, void *cbArg)
445 * if this device is a spare, and its parent includes one healthy,
446 * non-spare child, then detach this device.
448 zpool_handle_t *hdl(static_cast<zpool_handle_t*>(cbArg));
450 if (vdev.IsSpare()) {
451 std::list<Vdev> siblings;
452 std::list<Vdev>::iterator siblings_it;
453 boolean_t cleanup = B_FALSE;
455 Vdev parent = vdev.Parent();
456 siblings = parent.Children();
458 /* Determine whether the parent should be cleaned up */
459 for (siblings_it = siblings.begin();
460 siblings_it != siblings.end();
462 Vdev sibling = *siblings_it;
464 if (!sibling.IsSpare() &&
465 sibling.State() == VDEV_STATE_HEALTHY) {
472 syslog(LOG_INFO, "Detaching spare vdev %s from pool %s",
473 vdev.Path().c_str(), zpool_get_name(hdl));
474 zpool_vdev_detach(hdl, vdev.Path().c_str());
479 /* Always return false, because there may be other spares to detach */