static int dm_get_prefixed_uuid(const char *name, char *uuid) { struct dm_task *dmt; const char *uuidtmp; int r = 1; dmt = dm_task_create(DM_DEVICE_INFO); if (!dmt) return 1; if (!dm_task_set_name (dmt, name)) goto uuidout; if (!dm_task_run(dmt)) goto uuidout; uuidtmp = dm_task_get_uuid(dmt); if (uuidtmp) strcpy(uuid, uuidtmp); else uuid[0] = '\0'; r = 0; uuidout: dm_task_destroy(dmt); return r; }
int dm_event_unregister_handler(const struct dm_event_handler *dmevh) { int ret = 1, err; const char *uuid; struct dm_task *dmt; struct dm_event_daemon_message msg = { 0, 0, NULL }; if (!(dmt = _get_device_info(dmevh))) { stack; return 0; } uuid = dm_task_get_uuid(dmt); if ((err = _do_event(DM_EVENT_CMD_UNREGISTER_FOR_EVENT, dmevh->dmeventd_path, &msg, dmevh->dso, uuid, dmevh->mask, dmevh->timeout)) < 0) { log_error("%s: event deregistration failed: %s", dm_task_get_name(dmt), msg.data ? msg.data : strerror(-err)); ret = 0; } dm_free(msg.data); dm_task_destroy(dmt); return ret; }
extern int dm_is_mpath(const char * name) { int r = 0; struct dm_task *dmt; struct dm_info info; uint64_t start, length; char *target_type = NULL; char *params; const char *uuid; if (!(dmt = dm_task_create(DM_DEVICE_TABLE))) return 0; if (!dm_task_set_name(dmt, name)) goto out; dm_task_no_open_count(dmt); if (!dm_task_run(dmt)) goto out; if (!dm_task_get_info(dmt, &info) || !info.exists) goto out; uuid = dm_task_get_uuid(dmt); if (!uuid || strncmp(uuid, UUID_PREFIX, UUID_PREFIX_LEN) != 0) goto out; /* Fetch 1st target */ dm_get_next_target(dmt, NULL, &start, &length, &target_type, ¶ms); if (!target_type || strcmp(target_type, TGT_MPATH) != 0) goto out; r = 1; out: dm_task_destroy(dmt); return r; }
static int dm_map_present(char *str, char **uuid) { int r = 0; struct dm_task *dmt; const char *uuidtmp; struct dm_info info; if (uuid) *uuid = NULL; if (!(dmt = dm_task_create(DM_DEVICE_INFO))) return 0; if (!dm_task_set_name(dmt, str)) goto out; dm_task_no_open_count(dmt); if (!dm_task_run(dmt)) goto out; if (!dm_task_get_info(dmt, &info)) goto out; if (!info.exists) goto out; r = 1; if (uuid) { uuidtmp = dm_task_get_uuid(dmt); if (uuidtmp && strlen(uuidtmp)) *uuid = strdup(uuidtmp); } out: dm_task_destroy(dmt); return r; }
/* * Returns 0 if handler found; error (-ENOMEM, -ENOENT) otherwise. */ int dm_event_get_registered_device(struct dm_event_handler *dmevh, int next) { int ret = 0; const char *uuid = NULL; char *reply_dso = NULL, *reply_uuid = NULL; enum dm_event_mask reply_mask = 0; struct dm_task *dmt = NULL; struct dm_event_daemon_message msg = { 0, 0, NULL }; struct dm_info info; if (!(dmt = _get_device_info(dmevh))) { stack; return 0; } uuid = dm_task_get_uuid(dmt); if (!(ret = _do_event(next ? DM_EVENT_CMD_GET_NEXT_REGISTERED_DEVICE : DM_EVENT_CMD_GET_REGISTERED_DEVICE, dmevh->dmeventd_path, &msg, dmevh->dso, uuid, dmevh->mask, 0))) { /* FIXME this will probably horribly break if we get ill-formatted reply */ ret = _parse_message(&msg, &reply_dso, &reply_uuid, &reply_mask); } else { ret = -ENOENT; goto fail; } dm_task_destroy(dmt); dmt = NULL; dm_free(msg.data); msg.data = NULL; _dm_event_handler_clear_dev_info(dmevh); dmevh->uuid = dm_strdup(reply_uuid); if (!dmevh->uuid) { ret = -ENOMEM; goto fail; } if (!(dmt = _get_device_info(dmevh))) { ret = -ENXIO; /* dmeventd probably gave us bogus uuid back */ goto fail; } dm_event_handler_set_dso(dmevh, reply_dso); dm_event_handler_set_event_mask(dmevh, reply_mask); dm_free(reply_dso); reply_dso = NULL; dm_free(reply_uuid); reply_uuid = NULL; dmevh->dev_name = dm_strdup(dm_task_get_name(dmt)); if (!dmevh->dev_name) { ret = -ENOMEM; goto fail; } if (!dm_task_get_info(dmt, &info)) { ret = -1; goto fail; } dmevh->major = info.major; dmevh->minor = info.minor; dm_task_destroy(dmt); return ret; fail: dm_free(msg.data); dm_free(reply_dso); dm_free(reply_uuid); _dm_event_handler_clear_dev_info(dmevh); if (dmt) dm_task_destroy(dmt); return ret; }
int device_is_usable(struct device *dev) { struct dm_task *dmt; struct dm_info info; const char *name, *uuid; uint64_t start, length; char *target_type = NULL; char *params, *vgname = NULL, *lvname, *layer; void *next = NULL; int r = 0; if (!(dmt = dm_task_create(DM_DEVICE_STATUS))) { log_error("Failed to allocate dm_task struct to check dev status"); return 0; } if (!dm_task_set_major_minor(dmt, MAJOR(dev->dev), MINOR(dev->dev), 1)) goto_out; if (!dm_task_run(dmt)) { log_error("Failed to get state of mapped device"); goto out; } if (!dm_task_get_info(dmt, &info)) goto_out; if (!info.exists || info.suspended) goto out; name = dm_task_get_name(dmt); uuid = dm_task_get_uuid(dmt); /* FIXME Also check for mirror block_on_error and mpath no paths */ /* For now, we exclude all mirrors */ do { next = dm_get_next_target(dmt, next, &start, &length, &target_type, ¶ms); /* Skip if target type doesn't match */ if (target_type && !strcmp(target_type, "mirror")) { log_debug("%s: Mirror device not usable.", dev_name(dev)); goto out; } } while (next); /* FIXME Also check dependencies? */ /* Check internal lvm devices */ if (uuid && !strncmp(uuid, UUID_PREFIX, sizeof(UUID_PREFIX) - 1)) { if (!(vgname = dm_strdup(name)) || !dm_split_lvm_name(NULL, NULL, &vgname, &lvname, &layer)) goto_out; if (lvname && (is_reserved_lvname(lvname) || *layer)) { log_debug("%s: Reserved internal LV device %s/%s%s%s not usable.", dev_name(dev), vgname, lvname, *layer ? "-" : "", layer); goto out; } } r = 1; out: dm_free(vgname); dm_task_destroy(dmt); return r; }
int era_dm_info(const char *name, const char *uuid, struct era_dm_info *info, size_t name_size, char *name_ptr, size_t uuid_size, char *uuid_ptr) { struct dm_task *dmt; struct dm_info dmi; int rc = -1; if (!(dmt = dm_task_create(DM_DEVICE_INFO))) return -1; if (name && !dm_task_set_name(dmt, name)) goto out; if (uuid && !dm_task_set_uuid(dmt, uuid)) goto out; if (!dm_task_run(dmt)) goto out; if (!dm_task_get_info(dmt, &dmi)) goto out; if (info) { info->target_count = dmi.target_count; info->open_count = dmi.open_count; info->suspended = dmi.suspended; info->exists = dmi.exists; info->major = dmi.major; info->minor = dmi.minor; } if (dmi.exists && name_size > 0 && name_ptr) { const char *dm_name; size_t dm_name_len; dm_name = dm_task_get_name(dmt); if (!dm_name) goto out; dm_name_len = strlen(dm_name); if (dm_name_len >= name_size) { rc = dm_name_len + 1; goto out; } strcpy(name_ptr, dm_name); } if (dmi.exists && uuid_size > 0 && uuid_ptr) { const char *dm_uuid; size_t dm_uuid_len; dm_uuid = dm_task_get_uuid(dmt); if (!dm_uuid) goto out; dm_uuid_len = strlen(dm_uuid); if (dm_uuid_len >= uuid_size) { dm_task_destroy(dmt); return dm_uuid_len + 1; } strcpy(uuid_ptr, dm_uuid); } rc = 0; out: dm_task_destroy(dmt); return rc; }
int dm_query_device(const char *name, char **device, uint64_t *size, uint64_t *skip, uint64_t *offset, char **cipher, int *key_size, char **key, int *read_only, int *suspended, char **uuid) { struct dm_task *dmt; struct dm_info dmi; uint64_t start, length, val64; char *target_type, *params, *rcipher, *key_, *rdevice, *endp, buffer[3], *tmp_uuid; void *next = NULL; int i, r = -EINVAL; if (!(dmt = dm_task_create(DM_DEVICE_TABLE))) goto out; if (!dm_task_set_name(dmt, name)) goto out; r = -ENODEV; if (!dm_task_run(dmt)) goto out; r = -EINVAL; if (!dm_task_get_info(dmt, &dmi)) goto out; if (!dmi.exists) { r = -ENODEV; goto out; } next = dm_get_next_target(dmt, next, &start, &length, &target_type, ¶ms); if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 || start != 0 || next) goto out; if (size) *size = length; rcipher = strsep(¶ms, " "); /* cipher */ if (cipher) *cipher = strdup(rcipher); /* skip */ key_ = strsep(¶ms, " "); if (!params) goto out; val64 = strtoull(params, ¶ms, 10); if (*params != ' ') goto out; params++; if (skip) *skip = val64; /* device */ rdevice = strsep(¶ms, " "); if (device) *device = lookup_dev(rdevice); /*offset */ if (!params) goto out; val64 = strtoull(params, ¶ms, 10); if (*params) goto out; if (offset) *offset = val64; /* key_size */ if (key_size) *key_size = strlen(key_) / 2; /* key */ if (key_size && key) { *key = safe_alloc(*key_size); if (!*key) { r = -ENOMEM; goto out; } buffer[2] = '\0'; for(i = 0; i < *key_size; i++) { memcpy(buffer, &key_[i * 2], 2); (*key)[i] = strtoul(buffer, &endp, 16); if (endp != &buffer[2]) { safe_free(key); *key = NULL; goto out; } } } memset(key_, 0, strlen(key_)); if (read_only) *read_only = dmi.read_only; if (suspended) *suspended = dmi.suspended; if (uuid && (tmp_uuid = (char*)dm_task_get_uuid(dmt)) && !strncmp(tmp_uuid, DM_UUID_PREFIX, DM_UUID_PREFIX_LEN)) *uuid = strdup(tmp_uuid + DM_UUID_PREFIX_LEN); r = (dmi.open_count > 0); out: if (dmt) dm_task_destroy(dmt); return r; }