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 * We keep case files for any leaf vdev that is not in the optimal state.
37 * However, we only serialize to disk those events that need to be preserved
38 * across reboots. For now, this is just a log of soft errors which we
39 * accumulate in order to mark a device as degraded.
41 #include <sys/cdefs.h>
44 #include <sys/fs/zfs.h>
60 #include <devdctl/guid.h>
61 #include <devdctl/event.h>
62 #include <devdctl/event_factory.h>
63 #include <devdctl/exception.h>
64 #include <devdctl/consumer.h>
67 #include "vdev_iterator.h"
68 #include "zfsd_event.h"
69 #include "case_file.h"
72 #include "zfsd_exception.h"
73 #include "zpool_list.h"
75 __FBSDID("$FreeBSD$");
77 /*============================ Namespace Control =============================*/
81 using std::stringstream;
86 using DevdCtl::EventFactory;
87 using DevdCtl::EventList;
89 using DevdCtl::ParseException;
91 /*--------------------------------- CaseFile ---------------------------------*/
92 //- CaseFile Static Data -------------------------------------------------------
94 CaseFileList CaseFile::s_activeCases;
95 const string CaseFile::s_caseFilePath = "/var/db/zfsd/cases";
96 const timeval CaseFile::s_removeGracePeriod = { 60 /*sec*/, 0 /*usec*/};
98 //- CaseFile Static Public Methods ---------------------------------------------
100 CaseFile::Find(Guid poolGUID, Guid vdevGUID)
102 for (CaseFileList::iterator curCase = s_activeCases.begin();
103 curCase != s_activeCases.end(); curCase++) {
105 if (((*curCase)->PoolGUID() != poolGUID
106 && Guid::InvalidGuid() != poolGUID)
107 || (*curCase)->VdevGUID() != vdevGUID)
111 * We only carry one active case per-vdev.
119 CaseFile::Find(const string &physPath)
121 CaseFile *result = NULL;
123 for (CaseFileList::iterator curCase = s_activeCases.begin();
124 curCase != s_activeCases.end(); curCase++) {
126 if ((*curCase)->PhysicalPath() != physPath)
129 if (result != NULL) {
130 syslog(LOG_WARNING, "Multiple casefiles found for "
132 "This is most likely a bug in zfsd",
142 CaseFile::ReEvaluateByGuid(Guid poolGUID, const ZfsEvent &event)
144 CaseFileList::iterator casefile;
145 for (casefile = s_activeCases.begin(); casefile != s_activeCases.end();){
146 CaseFileList::iterator next = casefile;
148 if (poolGUID == (*casefile)->PoolGUID())
149 (*casefile)->ReEvaluate(event);
155 CaseFile::Create(Vdev &vdev)
157 CaseFile *activeCase;
159 activeCase = Find(vdev.PoolGUID(), vdev.GUID());
160 if (activeCase == NULL)
161 activeCase = new CaseFile(vdev);
163 return (*activeCase);
167 CaseFile::DeSerialize()
169 struct dirent **caseFiles;
171 int numCaseFiles(scandir(s_caseFilePath.c_str(), &caseFiles,
172 DeSerializeSelector, /*compar*/NULL));
174 if (numCaseFiles == -1)
176 if (numCaseFiles == 0) {
181 for (int i = 0; i < numCaseFiles; i++) {
183 DeSerializeFile(caseFiles[i]->d_name);
192 for (CaseFileList::iterator curCase = s_activeCases.begin();
193 curCase != s_activeCases.end(); curCase++)
201 * Serialize casefiles before deleting them so that they can be reread
202 * and revalidated during BuildCaseFiles.
203 * CaseFiles remove themselves from this list on destruction.
205 while (s_activeCases.size() != 0) {
206 CaseFile *casefile = s_activeCases.front();
207 casefile->Serialize();
213 //- CaseFile Public Methods ----------------------------------------------------
215 CaseFile::RefreshVdevState()
217 ZpoolList zpl(ZpoolList::ZpoolByGUID, &m_poolGUID);
218 zpool_handle_t *casePool(zpl.empty() ? NULL : zpl.front());
219 if (casePool == NULL)
222 Vdev vd(casePool, CaseVdev(casePool));
223 if (vd.DoesNotExist())
226 m_vdevState = vd.State();
227 m_vdevPhysPath = vd.PhysicalPath();
232 CaseFile::ReEvaluate(const string &devPath, const string &physPath, Vdev *vdev)
234 ZpoolList zpl(ZpoolList::ZpoolByGUID, &m_poolGUID);
235 zpool_handle_t *pool(zpl.empty() ? NULL : zpl.front());
237 if (pool == NULL || !RefreshVdevState()) {
239 * The pool or vdev for this case file is no longer
240 * part of the configuration. This can happen
241 * if we process a device arrival notification
242 * before seeing the ZFS configuration change
246 "CaseFile::ReEvaluate(%s,%s) Pool/Vdev unconfigured. "
248 PoolGUIDString().c_str(),
249 VdevGUIDString().c_str());
253 * Since this event was not used to close this
254 * case, do not report it as consumed.
256 return (/*consumed*/false);
259 if (VdevState() > VDEV_STATE_CANT_OPEN) {
261 * For now, newly discovered devices only help for
262 * devices that are missing. In the future, we might
263 * use a newly inserted spare to replace a degraded
266 syslog(LOG_INFO, "CaseFile::ReEvaluate(%s,%s): Pool/Vdev ignored",
267 PoolGUIDString().c_str(), VdevGUIDString().c_str());
268 return (/*consumed*/false);
272 && ( vdev->PoolGUID() == m_poolGUID
273 || vdev->PoolGUID() == Guid::InvalidGuid())
274 && vdev->GUID() == m_vdevGUID) {
276 zpool_vdev_online(pool, vdev->GUIDString().c_str(),
277 ZFS_ONLINE_CHECKREMOVE | ZFS_ONLINE_UNSPARE,
279 syslog(LOG_INFO, "Onlined vdev(%s/%s:%s). State now %s.\n",
280 zpool_get_name(pool), vdev->GUIDString().c_str(),
282 zpool_state_to_name(VdevState(), VDEV_AUX_NONE));
285 * Check the vdev state post the online action to see
286 * if we can retire this case.
290 return (/*consumed*/true);
294 * If the auto-replace policy is enabled, and we have physical
295 * path information, try a physical path replacement.
297 if (zpool_get_prop_int(pool, ZPOOL_PROP_AUTOREPLACE, NULL) == 0) {
299 "CaseFile(%s:%s:%s): AutoReplace not set. "
300 "Ignoring device insertion.\n",
301 PoolGUIDString().c_str(),
302 VdevGUIDString().c_str(),
303 zpool_state_to_name(VdevState(), VDEV_AUX_NONE));
304 return (/*consumed*/false);
307 if (PhysicalPath().empty()) {
309 "CaseFile(%s:%s:%s): No physical path information. "
310 "Ignoring device insertion.\n",
311 PoolGUIDString().c_str(),
312 VdevGUIDString().c_str(),
313 zpool_state_to_name(VdevState(), VDEV_AUX_NONE));
314 return (/*consumed*/false);
317 if (physPath != PhysicalPath()) {
319 "CaseFile(%s:%s:%s): Physical path mismatch. "
320 "Ignoring device insertion.\n",
321 PoolGUIDString().c_str(),
322 VdevGUIDString().c_str(),
323 zpool_state_to_name(VdevState(), VDEV_AUX_NONE));
324 return (/*consumed*/false);
327 /* Write a label on the newly inserted disk. */
328 if (zpool_label_disk(g_zfsHandle, pool, devPath.c_str()) != 0) {
330 "Replace vdev(%s/%s) by physical path (label): %s: %s\n",
331 zpool_get_name(pool), VdevGUIDString().c_str(),
332 libzfs_error_action(g_zfsHandle),
333 libzfs_error_description(g_zfsHandle));
334 return (/*consumed*/false);
337 syslog(LOG_INFO, "CaseFile::ReEvaluate(%s/%s): Replacing with %s",
338 PoolGUIDString().c_str(), VdevGUIDString().c_str(),
340 return (Replace(VDEV_TYPE_DISK, devPath.c_str(), /*isspare*/false));
344 CaseFile::ReEvaluate(const ZfsEvent &event)
346 bool consumed(false);
348 if (event.Value("type") == "misc.fs.zfs.vdev_remove") {
350 * The Vdev we represent has been removed from the
351 * configuration. This case is no longer of value.
355 return (/*consumed*/true);
356 } else if (event.Value("type") == "misc.fs.zfs.pool_destroy") {
357 /* This Pool has been destroyed. Discard the case */
360 return (/*consumed*/true);
361 } else if (event.Value("type") == "misc.fs.zfs.config_sync") {
363 if (VdevState() < VDEV_STATE_HEALTHY)
364 consumed = ActivateSpare();
368 if (event.Value("class") == "resource.fs.zfs.removed") {
369 bool spare_activated;
371 if (!RefreshVdevState()) {
373 * The pool or vdev for this case file is no longer
374 * part of the configuration. This can happen
375 * if we process a device arrival notification
376 * before seeing the ZFS configuration change
380 "CaseFile::ReEvaluate(%s,%s) Pool/Vdev "
381 "unconfigured. Closing\n",
382 PoolGUIDString().c_str(),
383 VdevGUIDString().c_str());
385 * Close the case now so we won't waste cycles in the
391 * Since this event was not used to close this
392 * case, do not report it as consumed.
394 return (/*consumed*/false);
398 * Discard any tentative I/O error events for
399 * this case. They were most likely caused by the
400 * hot-unplug of this device.
402 PurgeTentativeEvents();
404 /* Try to activate spares if they are available */
405 spare_activated = ActivateSpare();
408 * Rescan the drives in the system to see if a recent
409 * drive arrival can be used to solve this case.
411 ZfsDaemon::RequestSystemRescan();
414 * Consume the event if we successfully activated a spare.
415 * Otherwise, leave it in the unconsumed events list so that the
416 * future addition of a spare to this pool might be able to
419 consumed = spare_activated;
420 } else if (event.Value("class") == "resource.fs.zfs.statechange") {
423 * If this vdev is DEGRADED, FAULTED, or UNAVAIL, try to
424 * activate a hotspare. Otherwise, ignore the event
426 if (VdevState() == VDEV_STATE_FAULTED ||
427 VdevState() == VDEV_STATE_DEGRADED ||
428 VdevState() == VDEV_STATE_CANT_OPEN)
429 (void) ActivateSpare();
432 else if (event.Value("class") == "ereport.fs.zfs.io" ||
433 event.Value("class") == "ereport.fs.zfs.checksum") {
435 m_tentativeEvents.push_front(event.DeepCopy());
436 RegisterCallout(event);
440 bool closed(CloseIfSolved());
442 return (consumed || closed);
447 CaseFile::ActivateSpare() {
448 nvlist_t *config, *nvroot;
450 char *devPath, *vdev_type;
451 const char *poolname;
455 ZpoolList zpl(ZpoolList::ZpoolByGUID, &m_poolGUID);
456 zpool_handle_t *zhp(zpl.empty() ? NULL : zpl.front());
458 syslog(LOG_ERR, "CaseFile::ActivateSpare: Could not find pool "
459 "for pool_guid %" PRIu64".", (uint64_t)m_poolGUID);
462 poolname = zpool_get_name(zhp);
463 config = zpool_get_config(zhp, NULL);
464 if (config == NULL) {
465 syslog(LOG_ERR, "CaseFile::ActivateSpare: Could not find pool "
466 "config for pool %s", poolname);
469 error = nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot);
471 syslog(LOG_ERR, "CaseFile::ActivateSpare: Could not find vdev "
472 "tree for pool %s", poolname);
476 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
479 /* The pool has no spares configured */
480 syslog(LOG_INFO, "CaseFile::ActivateSpare: "
481 "No spares available for pool %s", poolname);
484 for (i = 0; i < nspares; i++) {
485 uint64_t *nvlist_array;
489 if (nvlist_lookup_uint64_array(spares[i],
490 ZPOOL_CONFIG_VDEV_STATS, &nvlist_array, &nstats) != 0) {
491 syslog(LOG_ERR, "CaseFile::ActivateSpare: Could not "
492 "find vdev stats for pool %s, spare %d",
496 vs = reinterpret_cast<vdev_stat_t *>(nvlist_array);
498 if ((vs->vs_aux != VDEV_AUX_SPARED)
499 && (vs->vs_state == VDEV_STATE_HEALTHY)) {
500 /* We found a usable spare */
506 /* No available spares were found */
510 error = nvlist_lookup_string(spares[i], ZPOOL_CONFIG_PATH, &devPath);
512 syslog(LOG_ERR, "CaseFile::ActivateSpare: Cannot determine "
513 "the path of pool %s, spare %d. Error %d",
518 error = nvlist_lookup_string(spares[i], ZPOOL_CONFIG_TYPE, &vdev_type);
520 syslog(LOG_ERR, "CaseFile::ActivateSpare: Cannot determine "
521 "the vdev type of pool %s, spare %d. Error %d",
526 return (Replace(vdev_type, devPath, /*isspare*/true));
530 CaseFile::RegisterCallout(const Event &event)
532 timeval now, countdown, elapsed, timestamp, zero, remaining;
534 gettimeofday(&now, 0);
535 timestamp = event.GetTimestamp();
536 timersub(&now, ×tamp, &elapsed);
537 timersub(&s_removeGracePeriod, &elapsed, &countdown);
539 * If countdown is <= zero, Reset the timer to the
540 * smallest positive time value instead
543 if (timercmp(&countdown, &zero, <=)) {
544 timerclear(&countdown);
545 countdown.tv_usec = 1;
548 remaining = m_tentativeTimer.TimeRemaining();
550 if (!m_tentativeTimer.IsPending()
551 || timercmp(&countdown, &remaining, <))
552 m_tentativeTimer.Reset(countdown, OnGracePeriodEnded, this);
557 CaseFile::CloseIfSolved()
560 && m_tentativeEvents.empty()) {
563 * We currently do not track or take actions on
564 * devices in the degraded or faulted state.
565 * Once we have support for spare pools, we'll
566 * retain these cases so that any spares added in
567 * the future can be applied to them.
569 switch (VdevState()) {
570 case VDEV_STATE_HEALTHY:
571 /* No need to keep cases for healthy vdevs */
574 case VDEV_STATE_REMOVED:
575 case VDEV_STATE_CANT_OPEN:
577 * Keep open. We may solve it with a newly inserted
580 case VDEV_STATE_FAULTED:
581 case VDEV_STATE_DEGRADED:
583 * Keep open. We may solve it with the future
584 * addition of a spare to the pool
586 case VDEV_STATE_UNKNOWN:
587 case VDEV_STATE_CLOSED:
588 case VDEV_STATE_OFFLINE:
590 * Keep open? This may not be the correct behavior,
591 * but it's what we've always done
597 * Re-serialize the case in order to remove any
598 * previous event data.
609 syslog(LOG_INFO, "CaseFile(%s,%s,%s)\n", PoolGUIDString().c_str(),
610 VdevGUIDString().c_str(), PhysicalPath().c_str());
611 syslog(LOG_INFO, "\tVdev State = %s\n",
612 zpool_state_to_name(VdevState(), VDEV_AUX_NONE));
613 if (m_tentativeEvents.size() != 0) {
614 syslog(LOG_INFO, "\t=== Tentative Events ===\n");
615 for (EventList::iterator event(m_tentativeEvents.begin());
616 event != m_tentativeEvents.end(); event++)
617 (*event)->Log(LOG_INFO);
619 if (m_events.size() != 0) {
620 syslog(LOG_INFO, "\t=== Events ===\n");
621 for (EventList::iterator event(m_events.begin());
622 event != m_events.end(); event++)
623 (*event)->Log(LOG_INFO);
627 //- CaseFile Static Protected Methods ------------------------------------------
629 CaseFile::OnGracePeriodEnded(void *arg)
631 CaseFile &casefile(*static_cast<CaseFile *>(arg));
633 casefile.OnGracePeriodEnded();
637 CaseFile::DeSerializeSelector(const struct dirent *dirEntry)
642 if (dirEntry->d_type == DT_REG
643 && sscanf(dirEntry->d_name, "pool_%" PRIu64 "_vdev_%" PRIu64 ".case",
644 &poolGUID, &vdevGUID) == 2)
650 CaseFile::DeSerializeFile(const char *fileName)
652 string fullName(s_caseFilePath + '/' + fileName);
653 CaseFile *existingCaseFile(NULL);
654 CaseFile *caseFile(NULL);
661 if (sscanf(fileName, "pool_%" PRIu64 "_vdev_%" PRIu64 ".case",
662 &poolGUID, &vdevGUID) != 2) {
663 throw ZfsdException("CaseFile::DeSerialize: "
664 "Unintelligible CaseFile filename %s.\n", fileName);
666 existingCaseFile = Find(Guid(poolGUID), Guid(vdevGUID));
667 if (existingCaseFile != NULL) {
669 * If the vdev is already degraded or faulted,
670 * there's no point in keeping the state around
671 * that we use to put a drive into the degraded
672 * state. However, if the vdev is simply missing,
673 * preserve the case data in the hopes that it will
676 caseFile = existingCaseFile;
677 vdev_state curState(caseFile->VdevState());
678 if (curState > VDEV_STATE_CANT_OPEN
679 && curState < VDEV_STATE_HEALTHY) {
684 ZpoolList zpl(ZpoolList::ZpoolByGUID, &poolGUID);
686 || (vdevConf = VdevIterator(zpl.front())
687 .Find(vdevGUID)) == NULL) {
689 * Either the pool no longer exists
690 * or this vdev is no longer a member of
693 unlink(fullName.c_str());
698 * Any vdev we find that does not have a case file
699 * must be in the healthy state and thus worthy of
700 * continued SERD data tracking.
702 caseFile = new CaseFile(Vdev(zpl.front(), vdevConf));
705 ifstream caseStream(fullName.c_str());
707 throw ZfsdException("CaseFile::DeSerialize: Unable to "
708 "read %s.\n", fileName);
710 caseFile->DeSerialize(caseStream);
711 } catch (const ParseException &exp) {
714 if (caseFile != existingCaseFile)
718 * Since we can't parse the file, unlink it so we don't
719 * trip over it again.
722 } catch (const ZfsdException &zfsException) {
725 if (caseFile != existingCaseFile)
730 //- CaseFile Protected Methods -------------------------------------------------
731 CaseFile::CaseFile(const Vdev &vdev)
732 : m_poolGUID(vdev.PoolGUID()),
733 m_vdevGUID(vdev.GUID()),
734 m_vdevState(vdev.State()),
735 m_vdevPhysPath(vdev.PhysicalPath())
737 stringstream guidString;
739 guidString << m_vdevGUID;
740 m_vdevGUIDString = guidString.str();
742 guidString << m_poolGUID;
743 m_poolGUIDString = guidString.str();
745 s_activeCases.push_back(this);
747 syslog(LOG_INFO, "Creating new CaseFile:\n");
751 CaseFile::~CaseFile()
754 PurgeTentativeEvents();
755 m_tentativeTimer.Stop();
756 s_activeCases.remove(this);
760 CaseFile::PurgeEvents()
762 for (EventList::iterator event(m_events.begin());
763 event != m_events.end(); event++)
770 CaseFile::PurgeTentativeEvents()
772 for (EventList::iterator event(m_tentativeEvents.begin());
773 event != m_tentativeEvents.end(); event++)
776 m_tentativeEvents.clear();
780 CaseFile::SerializeEvList(const EventList events, int fd,
781 const char* prefix) const
785 for (EventList::const_iterator curEvent = events.begin();
786 curEvent != events.end(); curEvent++) {
787 const string &eventString((*curEvent)->GetEventString());
789 // TODO: replace many write(2) calls with a single writev(2)
791 write(fd, prefix, strlen(prefix));
792 write(fd, eventString.c_str(), eventString.length());
797 CaseFile::Serialize()
799 stringstream saveFile;
801 saveFile << setfill('0')
802 << s_caseFilePath << "/"
803 << "pool_" << PoolGUIDString()
804 << "_vdev_" << VdevGUIDString()
807 if (m_events.empty() && m_tentativeEvents.empty()) {
808 unlink(saveFile.str().c_str());
812 int fd(open(saveFile.str().c_str(), O_CREAT|O_TRUNC|O_WRONLY, 0644));
814 syslog(LOG_ERR, "CaseFile::Serialize: Unable to open %s.\n",
815 saveFile.str().c_str());
818 SerializeEvList(m_events, fd);
819 SerializeEvList(m_tentativeEvents, fd, "tentative ");
824 * XXX: This method assumes that events may not contain embedded newlines. If
825 * ever events can contain embedded newlines, then CaseFile must switch
826 * serialization formats
829 CaseFile::DeSerialize(ifstream &caseStream)
832 const EventFactory &factory(ZfsDaemon::Get().GetFactory());
834 caseStream >> std::noskipws >> std::ws;
835 while (caseStream.good()) {
838 * read the beginning of a line and check it for
839 * "tentative". If found, discard "tentative".
843 EventList* destEvents;
844 const string tentFlag("tentative ");
846 std::stringbuf lineBuf;
848 caseStream.get(lineBuf);
849 caseStream.ignore(); /*discard the newline character*/
850 line = lineBuf.str();
851 if (line.compare(0, tentFlag.size(), tentFlag) == 0) {
852 /* Discard "tentative" */
853 line.erase(0, tentFlag.size());
854 destEvents = &m_tentativeEvents;
856 destEvents = &m_events;
858 Event *event(Event::CreateEvent(factory, line));
860 destEvents->push_back(event);
861 RegisterCallout(*event);
870 * This case is no longer relevant. Clean up our
871 * serialization file, and delete the case.
873 syslog(LOG_INFO, "CaseFile(%s,%s) closed - State %s\n",
874 PoolGUIDString().c_str(), VdevGUIDString().c_str(),
875 zpool_state_to_name(VdevState(), VDEV_AUX_NONE));
878 * Serialization of a Case with no event data, clears the
879 * Serialization data for that event.
888 CaseFile::OnGracePeriodEnded()
890 bool should_fault, should_degrade;
891 ZpoolList zpl(ZpoolList::ZpoolByGUID, &m_poolGUID);
892 zpool_handle_t *zhp(zpl.empty() ? NULL : zpl.front());
894 m_events.splice(m_events.begin(), m_tentativeEvents);
895 should_fault = ShouldFault();
896 should_degrade = ShouldDegrade();
898 if (should_fault || should_degrade) {
900 || (VdevIterator(zhp).Find(m_vdevGUID)) == NULL) {
902 * Either the pool no longer exists
903 * or this vdev is no longer a member of
912 /* A fault condition has priority over a degrade condition */
914 /* Fault the vdev and close the case. */
915 if (zpool_vdev_fault(zhp, (uint64_t)m_vdevGUID,
916 VDEV_AUX_ERR_EXCEEDED) == 0) {
917 syslog(LOG_INFO, "Faulting vdev(%s/%s)",
918 PoolGUIDString().c_str(),
919 VdevGUIDString().c_str());
924 syslog(LOG_ERR, "Fault vdev(%s/%s): %s: %s\n",
925 PoolGUIDString().c_str(),
926 VdevGUIDString().c_str(),
927 libzfs_error_action(g_zfsHandle),
928 libzfs_error_description(g_zfsHandle));
931 else if (ShouldDegrade()) {
932 /* Degrade the vdev and close the case. */
933 if (zpool_vdev_degrade(zhp, (uint64_t)m_vdevGUID,
934 VDEV_AUX_ERR_EXCEEDED) == 0) {
935 syslog(LOG_INFO, "Degrading vdev(%s/%s)",
936 PoolGUIDString().c_str(),
937 VdevGUIDString().c_str());
942 syslog(LOG_ERR, "Degrade vdev(%s/%s): %s: %s\n",
943 PoolGUIDString().c_str(),
944 VdevGUIDString().c_str(),
945 libzfs_error_action(g_zfsHandle),
946 libzfs_error_description(g_zfsHandle));
953 CaseFile::BeingReplacedBy(zpool_handle_t *zhp) {
954 Vdev vd(zhp, CaseVdev(zhp));
955 std::list<Vdev> children;
956 std::list<Vdev>::iterator children_it;
958 Vdev parent(vd.Parent());
959 Vdev replacing(NonexistentVdev);
962 * To determine whether we are being replaced by another spare that
963 * is still working, then make sure that it is currently spared and
964 * that the spare is either resilvering or healthy. If any of these
965 * conditions fail, then we are not being replaced by a spare.
967 * If the spare is healthy, then the case file should be closed very
968 * soon after this check.
970 if (parent.DoesNotExist()
971 || parent.Name(zhp, /*verbose*/false) != "spare")
972 return (NonexistentVdev);
974 children = parent.Children();
975 children_it = children.begin();
976 for (;children_it != children.end(); children_it++) {
977 Vdev child = *children_it;
980 if (child.GUID() == VdevGUID())
983 * Accept the first child that doesn't match our GUID, or
984 * any resilvering/healthy device if one exists.
986 if (replacing.DoesNotExist() || child.IsResilvering()
987 || child.State() == VDEV_STATE_HEALTHY)
995 CaseFile::Replace(const char* vdev_type, const char* path, bool isspare) {
996 nvlist_t *nvroot, *newvd;
997 const char *poolname;
998 string oldstr(VdevGUIDString());
1001 /* Figure out what pool we're working on */
1002 ZpoolList zpl(ZpoolList::ZpoolByGUID, &m_poolGUID);
1003 zpool_handle_t *zhp(zpl.empty() ? NULL : zpl.front());
1005 syslog(LOG_ERR, "CaseFile::Replace: could not find pool for "
1006 "pool_guid %" PRIu64 ".", (uint64_t)m_poolGUID);
1009 poolname = zpool_get_name(zhp);
1010 Vdev vd(zhp, CaseVdev(zhp));
1011 Vdev replaced(BeingReplacedBy(zhp));
1013 if (isspare && !vd.IsSpare() && !replaced.DoesNotExist()) {
1014 /* If we are already being replaced by a working spare, pass. */
1015 if (replaced.IsResilvering()
1016 || replaced.State() == VDEV_STATE_HEALTHY) {
1017 syslog(LOG_INFO, "CaseFile::Replace(%s->%s): already "
1018 "replaced", VdevGUIDString().c_str(), path);
1019 return (/*consumed*/false);
1022 * If we have already been replaced by a spare, but that spare
1023 * is broken, we must spare the spare, not the original device.
1025 oldstr = replaced.GUIDString();
1026 syslog(LOG_INFO, "CaseFile::Replace(%s->%s): sparing "
1027 "broken spare %s instead", VdevGUIDString().c_str(),
1028 path, oldstr.c_str());
1032 * Build a root vdev/leaf vdev configuration suitable for
1033 * zpool_vdev_attach. Only enough data for the kernel to find
1034 * the device (i.e. type and disk device node path) are needed.
1039 if (nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) != 0
1040 || nvlist_alloc(&newvd, NV_UNIQUE_NAME, 0) != 0) {
1041 syslog(LOG_ERR, "Replace vdev(%s/%s): Unable to allocate "
1042 "configuration data.", poolname, oldstr.c_str());
1044 nvlist_free(nvroot);
1047 if (nvlist_add_string(newvd, ZPOOL_CONFIG_TYPE, vdev_type) != 0
1048 || nvlist_add_string(newvd, ZPOOL_CONFIG_PATH, path) != 0
1049 || nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) != 0
1050 || nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1052 syslog(LOG_ERR, "Replace vdev(%s/%s): Unable to initialize "
1053 "configuration data.", poolname, oldstr.c_str());
1055 nvlist_free(nvroot);
1059 /* Data was copied when added to the root vdev. */
1062 retval = (zpool_vdev_attach(zhp, oldstr.c_str(), path, nvroot,
1063 /*replace*/B_TRUE) == 0);
1065 syslog(LOG_INFO, "Replacing vdev(%s/%s) with %s\n",
1066 poolname, oldstr.c_str(), path);
1068 syslog(LOG_ERR, "Replace vdev(%s/%s): %s: %s\n",
1069 poolname, oldstr.c_str(), libzfs_error_action(g_zfsHandle),
1070 libzfs_error_description(g_zfsHandle));
1071 nvlist_free(nvroot);
1076 /* Does the argument event refer to a checksum error? */
1078 IsChecksumEvent(const Event* const event)
1080 return ("ereport.fs.zfs.checksum" == event->Value("type"));
1083 /* Does the argument event refer to an IO error? */
1085 IsIOEvent(const Event* const event)
1087 return ("ereport.fs.zfs.io" == event->Value("type"));
1091 CaseFile::ShouldDegrade() const
1093 return (std::count_if(m_events.begin(), m_events.end(),
1094 IsChecksumEvent) > ZFS_DEGRADE_IO_COUNT);
1098 CaseFile::ShouldFault() const
1100 return (std::count_if(m_events.begin(), m_events.end(),
1101 IsIOEvent) > ZFS_DEGRADE_IO_COUNT);
1105 CaseFile::CaseVdev(zpool_handle_t *zhp) const
1107 return (VdevIterator(zhp).Find(VdevGUID()));