static int inuse_zpool_common(char *slice, nvlist_t *attrs, int *errp, char *type) { int found = 0; char *name; int fd; pool_state_t state; boolean_t used; *errp = 0; if (slice == NULL) { return (found); } /* * Dynamically load libzfs */ if (!initialized) { if (!init_zpool()) { return (found); } initialized = B_TRUE; } if ((fd = open(slice, O_RDONLY)) > 0) { name = NULL; if (zfsdl_zpool_in_use(zfs_hdl, fd, &state, &name, &used) == 0 && used) { if (strcmp(type, DM_USE_ACTIVE_ZPOOL) == 0) { if (state == POOL_STATE_ACTIVE) { found = 1; } else if (state == POOL_STATE_SPARE) { found = 1; type = DM_USE_SPARE_ZPOOL; } else if (state == POOL_STATE_L2CACHE) { found = 1; type = DM_USE_L2CACHE_ZPOOL; } } else { found = 1; } if (found) { libdiskmgt_add_str(attrs, DM_USED_BY, type, errp); libdiskmgt_add_str(attrs, DM_USED_NAME, name, errp); } } if (name) free(name); (void) close(fd); } return (found); }
/* * Search the list of devices from /etc/mnttab to find the mount point * for the specified device. */ int inuse_mnt(char *slice, nvlist_t *attrs, int *errp) { struct mntpnt_list *listp; int found = 0; *errp = 0; if (slice == NULL) { return (found); } (void) mutex_lock(&init_lock); if (!initialized) { thread_t mnttab_thread; /* load the mntpnt cache */ *errp = load_mnttab(B_FALSE); if (*errp == 0) { /* start a thread to monitor the mnttab */ *errp = thr_create(NULL, NULL, (void *(*)(void *))watch_mnttab, NULL, THR_NEW_LWP | THR_DAEMON, &mnttab_thread); } if (*errp == 0) { initialized = 1; } } (void) mutex_unlock(&init_lock); (void) rw_rdlock(&mntpoint_lock); listp = mntpoint_listp; while (listp != NULL) { if (libdiskmgt_str_eq(slice, listp->special)) { libdiskmgt_add_str(attrs, DM_USED_BY, DM_USE_MOUNT, errp); libdiskmgt_add_str(attrs, DM_USED_NAME, listp->mountp, errp); found = 1; break; } listp = listp->next; } (void) rw_unlock(&mntpoint_lock); return (found); }
int inuse_mnt(char *slice, nvlist_t *attrs, int *errp) { struct statfs* mounts; /* Read the current set of mounts */ int num_mounts = getmntinfo(&mounts, MNT_WAIT); /* Check whether slice is presently in use */ for (int i = 0; i < num_mounts; i++) { int slice_found = (strcmp(mounts[i].f_mntfromname, slice) == 0); if (slice_found) { libdiskmgt_add_str(attrs, DM_USED_BY, DM_USE_MOUNT, errp); libdiskmgt_add_str(attrs, DM_USED_NAME, mounts[i].f_mntonname, errp); return 1; } } return 0; }
/* * Search the list of devices under SVM for the specified device. */ int inuse_svm(char *slice, nvlist_t *attrs, int *errp) { struct svm_list *listp; int found = 0; *errp = 0; if (slice == NULL) { return (found); } (void) mutex_lock(&init_lock); if (!initialized) { /* dynamically load libmeta */ if (init_svm()) { /* * need to initialize the cluster library to * avoid seg faults */ (mdl_sdssc_bind_library)(); /* load the SVM cache */ *errp = load_svm(); if (*errp == 0) { /* start a thread to monitor the svm config */ sysevent_handle_t *shp; const char *subclass_list[1]; /* * Only start the svmevent thread if * we are not doing an install */ if (getenv("_LIBDISKMGT_INSTALL") == NULL) { shp = sysevent_bind_handle( event_handler); if (shp != NULL) { subclass_list[0] = EC_SUB_ALL; if (sysevent_subscribe_event( shp, EC_SVM_CONFIG, subclass_list, 1) != 0) { *errp = errno; } } else { *errp = errno; } if (*errp) { /* * If the sysevent thread fails, * log the error but continue * on. This failing to start * is not catastrophic in * particular for short lived * consumers of libdiskmgt. */ syslog(LOG_WARNING, dgettext(TEXT_DOMAIN, "libdiskmgt: sysevent " "thread for SVM failed " "to start\n")); *errp = 0; } } } } if (*errp == 0) { initialized = 1; } } (void) mutex_unlock(&init_lock); (void) rw_rdlock(&svm_lock); listp = svm_listp; while (listp != NULL) { if (strcmp(slice, listp->slice) == 0) { libdiskmgt_add_str(attrs, DM_USED_BY, DM_USE_SVM, errp); if (strcmp(listp->type, "mdb") == 0 || strcmp(listp->type, "hs") == 0) { libdiskmgt_add_str(attrs, DM_USED_NAME, listp->type, errp); } else { char name[MAXPATHLEN]; (void) snprintf(name, MAXPATHLEN, "%s:%s", listp->type, listp->name); libdiskmgt_add_str(attrs, DM_USED_NAME, name, errp); } found = 1; break; } listp = listp->next; } (void) rw_unlock(&svm_lock); return (found); }
/* * Use the heuristics to check for a filesystem on the slice. */ int inuse_fs(char *slice, nvlist_t *attrs, int *errp) { struct heuristic *hp; time_t curr_time; int found = 0; *errp = 0; if (slice == NULL) { return (0); } /* * We get the list of heuristic programs one time. */ (void) mutex_lock(&init_lock); if (!initialized) { *errp = load_heuristics(); if (*errp == 0) { initialized = 1; } } (void) mutex_unlock(&init_lock); /* Run each of the heuristics. */ for (hp = hlist; hp; hp = hp->next) { if (has_fs(hp->prog, slice)) { libdiskmgt_add_str(attrs, DM_USED_BY, DM_USE_FS, errp); libdiskmgt_add_str(attrs, DM_USED_NAME, hp->type, errp); found = 1; } } if (*errp != 0) return (found); /* * Second heuristic used is the check for an entry in vfstab */ (void) mutex_lock(&vfstab_lock); curr_time = time(NULL); if (timestamp < curr_time && (curr_time - timestamp) > 60) { free_vfstab(vfstab_listp); *errp = load_vfstab(); timestamp = curr_time; } if (*errp == 0) { struct vfstab_list *listp; listp = vfstab_listp; while (listp != NULL) { if (strcmp(slice, listp->special) == 0) { char *mountp = ""; if (listp->mountp != NULL) mountp = listp->mountp; libdiskmgt_add_str(attrs, DM_USED_BY, DM_USE_VFSTAB, errp); libdiskmgt_add_str(attrs, DM_USED_NAME, mountp, errp); found = 1; } listp = listp->next; } } (void) mutex_unlock(&vfstab_lock); return (found); }
int inuse_corestorage(char *slice, nvlist_t *attrs, int *errp) { DU_Info info; int in_use = 0; init_diskutil_info(&info); get_diskutil_cs_info(slice, &info); if (diskutil_info_valid(info)) { if (is_cs_physical_volume(info)) { libdiskmgt_add_str(attrs, DM_USED_BY, DM_USE_CORESTORAGE_PV, errp); libdiskmgt_add_str(attrs, DM_USED_NAME, slice, errp); in_use = 1; } else if (is_cs_logical_volume(info)) { if (is_cs_locked(info)) { libdiskmgt_add_str(attrs, DM_USED_BY, DM_USE_CORESTORAGE_LOCKED_LV, errp); libdiskmgt_add_str(attrs, DM_USED_NAME, slice, errp); in_use = 1; } else if (!is_cs_converted(info)) { CFStringRef lv_status = get_cs_LV_status(info); char lv_status_str[128] = { 0 }; Boolean success = CFStringGetCString(lv_status, lv_status_str, 128, kCFStringEncodingMacRoman); libdiskmgt_add_str(attrs, DM_USED_BY, DM_USE_CORESTORAGE_CONVERTING_LV, errp); if (success) { libdiskmgt_add_str(attrs, DM_USED_NAME, lv_status_str, errp); } else { libdiskmgt_add_str(attrs, DM_USED_NAME, "Unknown", errp); } in_use = 1; } else if (!is_cs_online(info)) { CFStringRef lv_status = get_cs_LV_status(info); char lv_status_str[128] = { 0 }; Boolean success = CFStringGetCString(lv_status, lv_status_str, 128, kCFStringEncodingMacRoman); libdiskmgt_add_str(attrs, DM_USED_BY, DM_USE_CORESTORAGE_OFFLINE_LV, errp); if (success) { libdiskmgt_add_str(attrs, DM_USED_NAME, lv_status_str, errp); } else { libdiskmgt_add_str(attrs, DM_USED_NAME, "Unknown", errp); } in_use = 1; } } } return (in_use); }