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 return (s_activeCases.empty());
198 for (CaseFileList::iterator curCase = s_activeCases.begin();
199 curCase != s_activeCases.end(); curCase++)
207 * Serialize casefiles before deleting them so that they can be reread
208 * and revalidated during BuildCaseFiles.
209 * CaseFiles remove themselves from this list on destruction.
211 while (s_activeCases.size() != 0) {
212 CaseFile *casefile = s_activeCases.front();
213 casefile->Serialize();
219 //- CaseFile Public Methods ----------------------------------------------------
221 CaseFile::RefreshVdevState()
223 ZpoolList zpl(ZpoolList::ZpoolByGUID, &m_poolGUID);
224 zpool_handle_t *casePool(zpl.empty() ? NULL : zpl.front());
225 if (casePool == NULL)
228 Vdev vd(casePool, CaseVdev(casePool));
229 if (vd.DoesNotExist())
232 m_vdevState = vd.State();
233 m_vdevPhysPath = vd.PhysicalPath();
238 CaseFile::ReEvaluate(const string &devPath, const string &physPath, Vdev *vdev)
240 ZpoolList zpl(ZpoolList::ZpoolByGUID, &m_poolGUID);
241 zpool_handle_t *pool(zpl.empty() ? NULL : zpl.front());
242 zpool_boot_label_t boot_type;
245 if (pool == NULL || !RefreshVdevState()) {
247 * The pool or vdev for this case file is no longer
248 * part of the configuration. This can happen
249 * if we process a device arrival notification
250 * before seeing the ZFS configuration change
254 "CaseFile::ReEvaluate(%s,%s) Pool/Vdev unconfigured. "
256 PoolGUIDString().c_str(),
257 VdevGUIDString().c_str());
261 * Since this event was not used to close this
262 * case, do not report it as consumed.
264 return (/*consumed*/false);
267 if (VdevState() > VDEV_STATE_CANT_OPEN) {
269 * For now, newly discovered devices only help for
270 * devices that are missing. In the future, we might
271 * use a newly inserted spare to replace a degraded
274 syslog(LOG_INFO, "CaseFile::ReEvaluate(%s,%s): Pool/Vdev ignored",
275 PoolGUIDString().c_str(), VdevGUIDString().c_str());
276 return (/*consumed*/false);
280 && ( vdev->PoolGUID() == m_poolGUID
281 || vdev->PoolGUID() == Guid::InvalidGuid())
282 && vdev->GUID() == m_vdevGUID) {
284 zpool_vdev_online(pool, vdev->GUIDString().c_str(),
285 ZFS_ONLINE_CHECKREMOVE | ZFS_ONLINE_UNSPARE,
287 syslog(LOG_INFO, "Onlined vdev(%s/%s:%s). State now %s.\n",
288 zpool_get_name(pool), vdev->GUIDString().c_str(),
290 zpool_state_to_name(VdevState(), VDEV_AUX_NONE));
293 * Check the vdev state post the online action to see
294 * if we can retire this case.
298 return (/*consumed*/true);
302 * If the auto-replace policy is enabled, and we have physical
303 * path information, try a physical path replacement.
305 if (zpool_get_prop_int(pool, ZPOOL_PROP_AUTOREPLACE, NULL) == 0) {
307 "CaseFile(%s:%s:%s): AutoReplace not set. "
308 "Ignoring device insertion.\n",
309 PoolGUIDString().c_str(),
310 VdevGUIDString().c_str(),
311 zpool_state_to_name(VdevState(), VDEV_AUX_NONE));
312 return (/*consumed*/false);
315 if (PhysicalPath().empty()) {
317 "CaseFile(%s:%s:%s): No physical path information. "
318 "Ignoring device insertion.\n",
319 PoolGUIDString().c_str(),
320 VdevGUIDString().c_str(),
321 zpool_state_to_name(VdevState(), VDEV_AUX_NONE));
322 return (/*consumed*/false);
325 if (physPath != PhysicalPath()) {
327 "CaseFile(%s:%s:%s): Physical path mismatch. "
328 "Ignoring device insertion.\n",
329 PoolGUIDString().c_str(),
330 VdevGUIDString().c_str(),
331 zpool_state_to_name(VdevState(), VDEV_AUX_NONE));
332 return (/*consumed*/false);
335 /* Write a label on the newly inserted disk. */
336 if (zpool_is_bootable(pool))
337 boot_type = ZPOOL_COPY_BOOT_LABEL;
339 boot_type = ZPOOL_NO_BOOT_LABEL;
340 boot_size = zpool_get_prop_int(pool, ZPOOL_PROP_BOOTSIZE, NULL);
341 if (zpool_label_disk(g_zfsHandle, pool, devPath.c_str(),
342 boot_type, boot_size, NULL) != 0) {
344 "Replace vdev(%s/%s) by physical path (label): %s: %s\n",
345 zpool_get_name(pool), VdevGUIDString().c_str(),
346 libzfs_error_action(g_zfsHandle),
347 libzfs_error_description(g_zfsHandle));
348 return (/*consumed*/false);
351 syslog(LOG_INFO, "CaseFile::ReEvaluate(%s/%s): Replacing with %s",
352 PoolGUIDString().c_str(), VdevGUIDString().c_str(),
354 return (Replace(VDEV_TYPE_DISK, devPath.c_str(), /*isspare*/false));
358 CaseFile::ReEvaluate(const ZfsEvent &event)
360 bool consumed(false);
362 if (event.Value("type") == "misc.fs.zfs.vdev_remove") {
364 * The Vdev we represent has been removed from the
365 * configuration. This case is no longer of value.
369 return (/*consumed*/true);
370 } else if (event.Value("type") == "misc.fs.zfs.pool_destroy") {
371 /* This Pool has been destroyed. Discard the case */
374 return (/*consumed*/true);
375 } else if (event.Value("type") == "misc.fs.zfs.config_sync") {
377 if (VdevState() < VDEV_STATE_HEALTHY)
378 consumed = ActivateSpare();
382 if (event.Value("class") == "resource.fs.zfs.removed") {
383 bool spare_activated;
385 if (!RefreshVdevState()) {
387 * The pool or vdev for this case file is no longer
388 * part of the configuration. This can happen
389 * if we process a device arrival notification
390 * before seeing the ZFS configuration change
394 "CaseFile::ReEvaluate(%s,%s) Pool/Vdev "
395 "unconfigured. Closing\n",
396 PoolGUIDString().c_str(),
397 VdevGUIDString().c_str());
399 * Close the case now so we won't waste cycles in the
405 * Since this event was not used to close this
406 * case, do not report it as consumed.
408 return (/*consumed*/false);
412 * Discard any tentative I/O error events for
413 * this case. They were most likely caused by the
414 * hot-unplug of this device.
416 PurgeTentativeEvents();
418 /* Try to activate spares if they are available */
419 spare_activated = ActivateSpare();
422 * Rescan the drives in the system to see if a recent
423 * drive arrival can be used to solve this case.
425 ZfsDaemon::RequestSystemRescan();
428 * Consume the event if we successfully activated a spare.
429 * Otherwise, leave it in the unconsumed events list so that the
430 * future addition of a spare to this pool might be able to
433 consumed = spare_activated;
434 } else if (event.Value("class") == "resource.fs.zfs.statechange") {
437 * If this vdev is DEGRADED, FAULTED, or UNAVAIL, try to
438 * activate a hotspare. Otherwise, ignore the event
440 if (VdevState() == VDEV_STATE_FAULTED ||
441 VdevState() == VDEV_STATE_DEGRADED ||
442 VdevState() == VDEV_STATE_CANT_OPEN)
443 (void) ActivateSpare();
446 else if (event.Value("class") == "ereport.fs.zfs.io" ||
447 event.Value("class") == "ereport.fs.zfs.checksum") {
449 m_tentativeEvents.push_front(event.DeepCopy());
450 RegisterCallout(event);
454 bool closed(CloseIfSolved());
456 return (consumed || closed);
459 /* Find a Vdev containing the vdev with the given GUID */
461 find_parent(nvlist_t *pool_config, nvlist_t *config, DevdCtl::Guid child_guid)
463 nvlist_t **vdevChildren;
465 unsigned ch, numChildren;
467 error = nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
468 &vdevChildren, &numChildren);
470 if (error != 0 || numChildren == 0)
473 for (ch = 0; ch < numChildren; ch++) {
475 Vdev vdev(pool_config, vdevChildren[ch]);
477 if (vdev.GUID() == child_guid)
480 result = find_parent(pool_config, vdevChildren[ch], child_guid);
489 CaseFile::ActivateSpare() {
490 nvlist_t *config, *nvroot, *parent_config;
492 char *devPath, *vdev_type;
493 const char *poolname;
497 ZpoolList zpl(ZpoolList::ZpoolByGUID, &m_poolGUID);
498 zpool_handle_t *zhp(zpl.empty() ? NULL : zpl.front());
500 syslog(LOG_ERR, "CaseFile::ActivateSpare: Could not find pool "
501 "for pool_guid %" PRIu64".", (uint64_t)m_poolGUID);
504 poolname = zpool_get_name(zhp);
505 config = zpool_get_config(zhp, NULL);
506 if (config == NULL) {
507 syslog(LOG_ERR, "CaseFile::ActivateSpare: Could not find pool "
508 "config for pool %s", poolname);
511 error = nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot);
513 syslog(LOG_ERR, "CaseFile::ActivateSpare: Could not find vdev "
514 "tree for pool %s", poolname);
518 parent_config = find_parent(config, nvroot, m_vdevGUID);
519 if (parent_config != NULL) {
523 * Don't activate spares for members of a "replacing" vdev.
524 * They're already dealt with. Sparing them will just drag out
525 * the resilver process.
527 error = nvlist_lookup_string(parent_config,
528 ZPOOL_CONFIG_TYPE, &parent_type);
529 if (error == 0 && strcmp(parent_type, VDEV_TYPE_REPLACING) == 0)
534 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
537 /* The pool has no spares configured */
538 syslog(LOG_INFO, "CaseFile::ActivateSpare: "
539 "No spares available for pool %s", poolname);
542 for (i = 0; i < nspares; i++) {
543 uint64_t *nvlist_array;
547 if (nvlist_lookup_uint64_array(spares[i],
548 ZPOOL_CONFIG_VDEV_STATS, &nvlist_array, &nstats) != 0) {
549 syslog(LOG_ERR, "CaseFile::ActivateSpare: Could not "
550 "find vdev stats for pool %s, spare %d",
554 vs = reinterpret_cast<vdev_stat_t *>(nvlist_array);
556 if ((vs->vs_aux != VDEV_AUX_SPARED)
557 && (vs->vs_state == VDEV_STATE_HEALTHY)) {
558 /* We found a usable spare */
564 /* No available spares were found */
568 error = nvlist_lookup_string(spares[i], ZPOOL_CONFIG_PATH, &devPath);
570 syslog(LOG_ERR, "CaseFile::ActivateSpare: Cannot determine "
571 "the path of pool %s, spare %d. Error %d",
576 error = nvlist_lookup_string(spares[i], ZPOOL_CONFIG_TYPE, &vdev_type);
578 syslog(LOG_ERR, "CaseFile::ActivateSpare: Cannot determine "
579 "the vdev type of pool %s, spare %d. Error %d",
584 return (Replace(vdev_type, devPath, /*isspare*/true));
588 CaseFile::RegisterCallout(const Event &event)
590 timeval now, countdown, elapsed, timestamp, zero, remaining;
592 gettimeofday(&now, 0);
593 timestamp = event.GetTimestamp();
594 timersub(&now, ×tamp, &elapsed);
595 timersub(&s_removeGracePeriod, &elapsed, &countdown);
597 * If countdown is <= zero, Reset the timer to the
598 * smallest positive time value instead
601 if (timercmp(&countdown, &zero, <=)) {
602 timerclear(&countdown);
603 countdown.tv_usec = 1;
606 remaining = m_tentativeTimer.TimeRemaining();
608 if (!m_tentativeTimer.IsPending()
609 || timercmp(&countdown, &remaining, <))
610 m_tentativeTimer.Reset(countdown, OnGracePeriodEnded, this);
615 CaseFile::CloseIfSolved()
618 && m_tentativeEvents.empty()) {
621 * We currently do not track or take actions on
622 * devices in the degraded or faulted state.
623 * Once we have support for spare pools, we'll
624 * retain these cases so that any spares added in
625 * the future can be applied to them.
627 switch (VdevState()) {
628 case VDEV_STATE_HEALTHY:
629 /* No need to keep cases for healthy vdevs */
632 case VDEV_STATE_REMOVED:
633 case VDEV_STATE_CANT_OPEN:
635 * Keep open. We may solve it with a newly inserted
638 case VDEV_STATE_FAULTED:
639 case VDEV_STATE_DEGRADED:
641 * Keep open. We may solve it with the future
642 * addition of a spare to the pool
644 case VDEV_STATE_UNKNOWN:
645 case VDEV_STATE_CLOSED:
646 case VDEV_STATE_OFFLINE:
648 * Keep open? This may not be the correct behavior,
649 * but it's what we've always done
655 * Re-serialize the case in order to remove any
656 * previous event data.
667 syslog(LOG_INFO, "CaseFile(%s,%s,%s)\n", PoolGUIDString().c_str(),
668 VdevGUIDString().c_str(), PhysicalPath().c_str());
669 syslog(LOG_INFO, "\tVdev State = %s\n",
670 zpool_state_to_name(VdevState(), VDEV_AUX_NONE));
671 if (m_tentativeEvents.size() != 0) {
672 syslog(LOG_INFO, "\t=== Tentative Events ===\n");
673 for (EventList::iterator event(m_tentativeEvents.begin());
674 event != m_tentativeEvents.end(); event++)
675 (*event)->Log(LOG_INFO);
677 if (m_events.size() != 0) {
678 syslog(LOG_INFO, "\t=== Events ===\n");
679 for (EventList::iterator event(m_events.begin());
680 event != m_events.end(); event++)
681 (*event)->Log(LOG_INFO);
685 //- CaseFile Static Protected Methods ------------------------------------------
687 CaseFile::OnGracePeriodEnded(void *arg)
689 CaseFile &casefile(*static_cast<CaseFile *>(arg));
691 casefile.OnGracePeriodEnded();
695 CaseFile::DeSerializeSelector(const struct dirent *dirEntry)
700 if (dirEntry->d_type == DT_REG
701 && sscanf(dirEntry->d_name, "pool_%" PRIu64 "_vdev_%" PRIu64 ".case",
702 &poolGUID, &vdevGUID) == 2)
708 CaseFile::DeSerializeFile(const char *fileName)
710 string fullName(s_caseFilePath + '/' + fileName);
711 CaseFile *existingCaseFile(NULL);
712 CaseFile *caseFile(NULL);
719 if (sscanf(fileName, "pool_%" PRIu64 "_vdev_%" PRIu64 ".case",
720 &poolGUID, &vdevGUID) != 2) {
721 throw ZfsdException("CaseFile::DeSerialize: "
722 "Unintelligible CaseFile filename %s.\n", fileName);
724 existingCaseFile = Find(Guid(poolGUID), Guid(vdevGUID));
725 if (existingCaseFile != NULL) {
727 * If the vdev is already degraded or faulted,
728 * there's no point in keeping the state around
729 * that we use to put a drive into the degraded
730 * state. However, if the vdev is simply missing,
731 * preserve the case data in the hopes that it will
734 caseFile = existingCaseFile;
735 vdev_state curState(caseFile->VdevState());
736 if (curState > VDEV_STATE_CANT_OPEN
737 && curState < VDEV_STATE_HEALTHY) {
742 ZpoolList zpl(ZpoolList::ZpoolByGUID, &poolGUID);
744 || (vdevConf = VdevIterator(zpl.front())
745 .Find(vdevGUID)) == NULL) {
747 * Either the pool no longer exists
748 * or this vdev is no longer a member of
751 unlink(fullName.c_str());
756 * Any vdev we find that does not have a case file
757 * must be in the healthy state and thus worthy of
758 * continued SERD data tracking.
760 caseFile = new CaseFile(Vdev(zpl.front(), vdevConf));
763 ifstream caseStream(fullName.c_str());
765 throw ZfsdException("CaseFile::DeSerialize: Unable to "
766 "read %s.\n", fileName);
768 caseFile->DeSerialize(caseStream);
769 } catch (const ParseException &exp) {
772 if (caseFile != existingCaseFile)
776 * Since we can't parse the file, unlink it so we don't
777 * trip over it again.
780 } catch (const ZfsdException &zfsException) {
783 if (caseFile != existingCaseFile)
788 //- CaseFile Protected Methods -------------------------------------------------
789 CaseFile::CaseFile(const Vdev &vdev)
790 : m_poolGUID(vdev.PoolGUID()),
791 m_vdevGUID(vdev.GUID()),
792 m_vdevState(vdev.State()),
793 m_vdevPhysPath(vdev.PhysicalPath())
795 stringstream guidString;
797 guidString << m_vdevGUID;
798 m_vdevGUIDString = guidString.str();
800 guidString << m_poolGUID;
801 m_poolGUIDString = guidString.str();
803 s_activeCases.push_back(this);
805 syslog(LOG_INFO, "Creating new CaseFile:\n");
809 CaseFile::~CaseFile()
812 PurgeTentativeEvents();
813 m_tentativeTimer.Stop();
814 s_activeCases.remove(this);
818 CaseFile::PurgeEvents()
820 for (EventList::iterator event(m_events.begin());
821 event != m_events.end(); event++)
828 CaseFile::PurgeTentativeEvents()
830 for (EventList::iterator event(m_tentativeEvents.begin());
831 event != m_tentativeEvents.end(); event++)
834 m_tentativeEvents.clear();
838 CaseFile::SerializeEvList(const EventList events, int fd,
839 const char* prefix) const
843 for (EventList::const_iterator curEvent = events.begin();
844 curEvent != events.end(); curEvent++) {
845 const string &eventString((*curEvent)->GetEventString());
847 // TODO: replace many write(2) calls with a single writev(2)
849 write(fd, prefix, strlen(prefix));
850 write(fd, eventString.c_str(), eventString.length());
855 CaseFile::Serialize()
857 stringstream saveFile;
859 saveFile << setfill('0')
860 << s_caseFilePath << "/"
861 << "pool_" << PoolGUIDString()
862 << "_vdev_" << VdevGUIDString()
865 if (m_events.empty() && m_tentativeEvents.empty()) {
866 unlink(saveFile.str().c_str());
870 int fd(open(saveFile.str().c_str(), O_CREAT|O_TRUNC|O_WRONLY, 0644));
872 syslog(LOG_ERR, "CaseFile::Serialize: Unable to open %s.\n",
873 saveFile.str().c_str());
876 SerializeEvList(m_events, fd);
877 SerializeEvList(m_tentativeEvents, fd, "tentative ");
882 * XXX: This method assumes that events may not contain embedded newlines. If
883 * ever events can contain embedded newlines, then CaseFile must switch
884 * serialization formats
887 CaseFile::DeSerialize(ifstream &caseStream)
890 const EventFactory &factory(ZfsDaemon::Get().GetFactory());
892 caseStream >> std::noskipws >> std::ws;
893 while (caseStream.good()) {
896 * read the beginning of a line and check it for
897 * "tentative". If found, discard "tentative".
901 EventList* destEvents;
902 const string tentFlag("tentative ");
904 std::stringbuf lineBuf;
906 caseStream.get(lineBuf);
907 caseStream.ignore(); /*discard the newline character*/
908 line = lineBuf.str();
909 if (line.compare(0, tentFlag.size(), tentFlag) == 0) {
910 /* Discard "tentative" */
911 line.erase(0, tentFlag.size());
912 destEvents = &m_tentativeEvents;
914 destEvents = &m_events;
916 Event *event(Event::CreateEvent(factory, line));
918 destEvents->push_back(event);
919 RegisterCallout(*event);
928 * This case is no longer relevant. Clean up our
929 * serialization file, and delete the case.
931 syslog(LOG_INFO, "CaseFile(%s,%s) closed - State %s\n",
932 PoolGUIDString().c_str(), VdevGUIDString().c_str(),
933 zpool_state_to_name(VdevState(), VDEV_AUX_NONE));
936 * Serialization of a Case with no event data, clears the
937 * Serialization data for that event.
946 CaseFile::OnGracePeriodEnded()
948 bool should_fault, should_degrade;
949 ZpoolList zpl(ZpoolList::ZpoolByGUID, &m_poolGUID);
950 zpool_handle_t *zhp(zpl.empty() ? NULL : zpl.front());
952 m_events.splice(m_events.begin(), m_tentativeEvents);
953 should_fault = ShouldFault();
954 should_degrade = ShouldDegrade();
956 if (should_fault || should_degrade) {
958 || (VdevIterator(zhp).Find(m_vdevGUID)) == NULL) {
960 * Either the pool no longer exists
961 * or this vdev is no longer a member of
970 /* A fault condition has priority over a degrade condition */
972 /* Fault the vdev and close the case. */
973 if (zpool_vdev_fault(zhp, (uint64_t)m_vdevGUID,
974 VDEV_AUX_ERR_EXCEEDED) == 0) {
975 syslog(LOG_INFO, "Faulting vdev(%s/%s)",
976 PoolGUIDString().c_str(),
977 VdevGUIDString().c_str());
982 syslog(LOG_ERR, "Fault vdev(%s/%s): %s: %s\n",
983 PoolGUIDString().c_str(),
984 VdevGUIDString().c_str(),
985 libzfs_error_action(g_zfsHandle),
986 libzfs_error_description(g_zfsHandle));
989 else if (ShouldDegrade()) {
990 /* Degrade the vdev and close the case. */
991 if (zpool_vdev_degrade(zhp, (uint64_t)m_vdevGUID,
992 VDEV_AUX_ERR_EXCEEDED) == 0) {
993 syslog(LOG_INFO, "Degrading vdev(%s/%s)",
994 PoolGUIDString().c_str(),
995 VdevGUIDString().c_str());
1000 syslog(LOG_ERR, "Degrade vdev(%s/%s): %s: %s\n",
1001 PoolGUIDString().c_str(),
1002 VdevGUIDString().c_str(),
1003 libzfs_error_action(g_zfsHandle),
1004 libzfs_error_description(g_zfsHandle));
1011 CaseFile::BeingReplacedBy(zpool_handle_t *zhp) {
1012 Vdev vd(zhp, CaseVdev(zhp));
1013 std::list<Vdev> children;
1014 std::list<Vdev>::iterator children_it;
1016 Vdev parent(vd.Parent());
1017 Vdev replacing(NonexistentVdev);
1020 * To determine whether we are being replaced by another spare that
1021 * is still working, then make sure that it is currently spared and
1022 * that the spare is either resilvering or healthy. If any of these
1023 * conditions fail, then we are not being replaced by a spare.
1025 * If the spare is healthy, then the case file should be closed very
1026 * soon after this check.
1028 if (parent.DoesNotExist()
1029 || parent.Name(zhp, /*verbose*/false) != "spare")
1030 return (NonexistentVdev);
1032 children = parent.Children();
1033 children_it = children.begin();
1034 for (;children_it != children.end(); children_it++) {
1035 Vdev child = *children_it;
1037 /* Skip our vdev. */
1038 if (child.GUID() == VdevGUID())
1041 * Accept the first child that doesn't match our GUID, or
1042 * any resilvering/healthy device if one exists.
1044 if (replacing.DoesNotExist() || child.IsResilvering()
1045 || child.State() == VDEV_STATE_HEALTHY)
1053 CaseFile::Replace(const char* vdev_type, const char* path, bool isspare) {
1054 nvlist_t *nvroot, *newvd;
1055 const char *poolname;
1056 string oldstr(VdevGUIDString());
1059 /* Figure out what pool we're working on */
1060 ZpoolList zpl(ZpoolList::ZpoolByGUID, &m_poolGUID);
1061 zpool_handle_t *zhp(zpl.empty() ? NULL : zpl.front());
1063 syslog(LOG_ERR, "CaseFile::Replace: could not find pool for "
1064 "pool_guid %" PRIu64 ".", (uint64_t)m_poolGUID);
1067 poolname = zpool_get_name(zhp);
1068 Vdev vd(zhp, CaseVdev(zhp));
1069 Vdev replaced(BeingReplacedBy(zhp));
1071 if (isspare && !vd.IsSpare() && !replaced.DoesNotExist()) {
1072 /* If we are already being replaced by a working spare, pass. */
1073 if (replaced.IsResilvering()
1074 || replaced.State() == VDEV_STATE_HEALTHY) {
1075 syslog(LOG_INFO, "CaseFile::Replace(%s->%s): already "
1076 "replaced", VdevGUIDString().c_str(), path);
1077 return (/*consumed*/false);
1080 * If we have already been replaced by a spare, but that spare
1081 * is broken, we must spare the spare, not the original device.
1083 oldstr = replaced.GUIDString();
1084 syslog(LOG_INFO, "CaseFile::Replace(%s->%s): sparing "
1085 "broken spare %s instead", VdevGUIDString().c_str(),
1086 path, oldstr.c_str());
1090 * Build a root vdev/leaf vdev configuration suitable for
1091 * zpool_vdev_attach. Only enough data for the kernel to find
1092 * the device (i.e. type and disk device node path) are needed.
1097 if (nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) != 0
1098 || nvlist_alloc(&newvd, NV_UNIQUE_NAME, 0) != 0) {
1099 syslog(LOG_ERR, "Replace vdev(%s/%s): Unable to allocate "
1100 "configuration data.", poolname, oldstr.c_str());
1102 nvlist_free(nvroot);
1105 if (nvlist_add_string(newvd, ZPOOL_CONFIG_TYPE, vdev_type) != 0
1106 || nvlist_add_string(newvd, ZPOOL_CONFIG_PATH, path) != 0
1107 || nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) != 0
1108 || nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1110 syslog(LOG_ERR, "Replace vdev(%s/%s): Unable to initialize "
1111 "configuration data.", poolname, oldstr.c_str());
1113 nvlist_free(nvroot);
1117 /* Data was copied when added to the root vdev. */
1120 retval = (zpool_vdev_attach(zhp, oldstr.c_str(), path, nvroot,
1121 /*replace*/B_TRUE) == 0);
1123 syslog(LOG_INFO, "Replacing vdev(%s/%s) with %s\n",
1124 poolname, oldstr.c_str(), path);
1126 syslog(LOG_ERR, "Replace vdev(%s/%s): %s: %s\n",
1127 poolname, oldstr.c_str(), libzfs_error_action(g_zfsHandle),
1128 libzfs_error_description(g_zfsHandle));
1129 nvlist_free(nvroot);
1134 /* Does the argument event refer to a checksum error? */
1136 IsChecksumEvent(const Event* const event)
1138 return ("ereport.fs.zfs.checksum" == event->Value("type"));
1141 /* Does the argument event refer to an IO error? */
1143 IsIOEvent(const Event* const event)
1145 return ("ereport.fs.zfs.io" == event->Value("type"));
1149 CaseFile::ShouldDegrade() const
1151 return (std::count_if(m_events.begin(), m_events.end(),
1152 IsChecksumEvent) > ZFS_DEGRADE_IO_COUNT);
1156 CaseFile::ShouldFault() const
1158 return (std::count_if(m_events.begin(), m_events.end(),
1159 IsIOEvent) > ZFS_DEGRADE_IO_COUNT);
1163 CaseFile::CaseVdev(zpool_handle_t *zhp) const
1165 return (VdevIterator(zhp).Find(VdevGUID()));