void enumerate_images(struct rbd_image **head) { char *ibuf = NULL; size_t ibuf_len = 0; struct rbd_image *im, *next; char *ip; int ret; if (*head != NULL) { for (im = *head; im != NULL;) { next = im->next; free(im); im = next; } *head = NULL; } ret = rbd_list(ioctx, ibuf, &ibuf_len); if (ret == -ERANGE) { assert(ibuf_len > 0); ibuf = malloc(ibuf_len); if (!ibuf) { simple_err("Failed to get ibuf", -ENOMEM); return; } } else if (ret < 0) { simple_err("Failed to get ibuf_len", ret); return; } ret = rbd_list(ioctx, ibuf, &ibuf_len); if (ret < 0) { simple_err("Failed to populate ibuf", ret); free(ibuf); return; } assert(ret == (int)ibuf_len); fprintf(stderr, "pool %s: ", pool_name); for (ip = ibuf; ip < &ibuf[ibuf_len]; ip += strlen(ip) + 1) { if ((mount_image_name == NULL) || ((strlen(mount_image_name) > 0) && (strcmp(ip, mount_image_name) == 0))) { fprintf(stderr, "%s, ", ip); im = malloc(sizeof(*im)); im->image_name = ip; im->next = *head; *head = im; } } fprintf(stderr, "\n"); }
void test_ls(rados_ioctx_t io_ctx, size_t num_expected, ...) { int num_images, i, j; char *expected, *names, *cur_name; va_list ap; size_t max_size = 1024; names = (char *) malloc(sizeof(char *) * 1024); num_images = rbd_list(io_ctx, names, &max_size); printf("num images is: %d\nexpected: %d\n", num_images, (int)num_expected); assert(num_images >= 0); assert(num_images == (int)num_expected); for (i = 0, cur_name = names; i < num_images; i++) { printf("image: %s\n", cur_name); cur_name += strlen(cur_name) + 1; } va_start(ap, num_expected); for (i = num_expected; i > 0; i--) { expected = va_arg(ap, char *); printf("expected = %s\n", expected); int found = 0; for (j = 0, cur_name = names; j < num_images; j++) { if (cur_name[0] == '_') { cur_name += strlen(cur_name) + 1; continue; } if (strcmp(cur_name, expected) == 0) { printf("found %s\n", cur_name); cur_name[0] = '_'; found = 1; break; } } assert(found); } for (i = 0, cur_name = names; i < num_images; i++) { assert(cur_name[0] == '_'); cur_name += strlen(cur_name) + 1; } free(names); }
void enumerate_images(struct rbd_image **head) { char *ibuf; size_t ibuf_len; struct rbd_image *im, *next; char *ip; int actual_len; if (*head != NULL) { for (im = *head; im != NULL;) { next = im->next; free(im); im = next; } *head = NULL; } ibuf_len = 1024; ibuf = malloc(ibuf_len); actual_len = rbd_list(ioctx, ibuf, &ibuf_len); if (actual_len < 0) { simple_err("rbd_list: error %d\n", actual_len); return; } fprintf(stderr, "pool %s: ", pool_name); for (ip = ibuf; *ip != '\0' && ip < &ibuf[actual_len]; ip += strlen(ip) + 1) { fprintf(stderr, "%s, ", ip); im = malloc(sizeof(*im)); im->image_name = ip; im->next = *head; *head = im; } fprintf(stderr, "\n"); return; }
static int virStorageBackendRBDRefreshPool(virConnectPtr conn ATTRIBUTE_UNUSED, virStoragePoolObjPtr pool) { size_t max_size = 1024; int ret = -1; int len = -1; int i; char *name, *names = NULL; virStorageBackendRBDStatePtr ptr; ptr.cluster = NULL; ptr.ioctx = NULL; if (virStorageBackendRBDOpenRADOSConn(&ptr, conn, pool) < 0) { goto cleanup; } if (rados_ioctx_create(ptr.cluster, pool->def->source.name, &ptr.ioctx) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, _("failed to create the RBD IoCTX. Does the pool '%s' exist?"), pool->def->source.name); goto cleanup; } struct rados_cluster_stat_t stat; if (rados_cluster_stat(ptr.cluster, &stat) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("failed to stat the RADOS cluster")); goto cleanup; } struct rados_pool_stat_t poolstat; if (rados_ioctx_pool_stat(ptr.ioctx, &poolstat) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, _("failed to stat the RADOS pool '%s'"), pool->def->source.name); goto cleanup; } pool->def->capacity = stat.kb * 1024; pool->def->available = stat.kb_avail * 1024; pool->def->allocation = poolstat.num_bytes; VIR_DEBUG("Utilization of RBD pool %s: (kb: %llu kb_avail: %llu num_bytes: %llu)", pool->def->source.name, (unsigned long long)stat.kb, (unsigned long long)stat.kb_avail, (unsigned long long)poolstat.num_bytes); while (true) { if (VIR_ALLOC_N(names, max_size) < 0) goto out_of_memory; len = rbd_list(ptr.ioctx, names, &max_size); if (len >= 0) break; if (len != -ERANGE) { VIR_WARN("%s", _("A problem occurred while listing RBD images")); goto cleanup; } VIR_FREE(names); } for (i = 0, name = names; name < names + max_size; i++) { virStorageVolDefPtr vol; if (VIR_REALLOC_N(pool->volumes.objs, pool->volumes.count + 1) < 0) { virStoragePoolObjClearVols(pool); goto out_of_memory; } if (STREQ(name, "")) break; if (VIR_ALLOC(vol) < 0) goto out_of_memory; vol->name = strdup(name); if (vol->name == NULL) { VIR_FREE(vol); goto out_of_memory; } name += strlen(name) + 1; if (volStorageBackendRBDRefreshVolInfo(vol, pool, ptr) < 0) { virStorageVolDefFree(vol); goto cleanup; } pool->volumes.objs[pool->volumes.count++] = vol; } VIR_DEBUG("Found %d images in RBD pool %s", pool->volumes.count, pool->def->source.name); ret = 0; cleanup: VIR_FREE(names); virStorageBackendRBDCloseRADOSConn(ptr); return ret; out_of_memory: virReportOOMError(); goto cleanup; }
static int virStorageBackendRBDRefreshPool(virConnectPtr conn, virStoragePoolObjPtr pool) { size_t max_size = 1024; int ret = -1; int len = -1; int r = 0; char *name, *names = NULL; virStorageBackendRBDState ptr; ptr.cluster = NULL; ptr.ioctx = NULL; if (virStorageBackendRBDOpenRADOSConn(&ptr, conn, pool) < 0) { goto cleanup; } if (virStorageBackendRBDOpenIoCTX(&ptr, pool) < 0) { goto cleanup; } struct rados_cluster_stat_t clusterstat; r = rados_cluster_stat(ptr.cluster, &clusterstat); if (r < 0) { virReportSystemError(-r, "%s", _("failed to stat the RADOS cluster")); goto cleanup; } struct rados_pool_stat_t poolstat; r = rados_ioctx_pool_stat(ptr.ioctx, &poolstat); if (r < 0) { virReportSystemError(-r, _("failed to stat the RADOS pool '%s'"), pool->def->source.name); goto cleanup; } pool->def->capacity = clusterstat.kb * 1024; pool->def->available = clusterstat.kb_avail * 1024; pool->def->allocation = poolstat.num_bytes; VIR_DEBUG("Utilization of RBD pool %s: (kb: %llu kb_avail: %llu num_bytes: %llu)", pool->def->source.name, (unsigned long long)clusterstat.kb, (unsigned long long)clusterstat.kb_avail, (unsigned long long)poolstat.num_bytes); while (true) { if (VIR_ALLOC_N(names, max_size) < 0) goto cleanup; len = rbd_list(ptr.ioctx, names, &max_size); if (len >= 0) break; if (len != -ERANGE) { VIR_WARN("%s", _("A problem occurred while listing RBD images")); goto cleanup; } VIR_FREE(names); } for (name = names; name < names + max_size;) { virStorageVolDefPtr vol; if (VIR_REALLOC_N(pool->volumes.objs, pool->volumes.count + 1) < 0) { virStoragePoolObjClearVols(pool); goto cleanup; } if (STREQ(name, "")) break; if (VIR_ALLOC(vol) < 0) goto cleanup; if (VIR_STRDUP(vol->name, name) < 0) { VIR_FREE(vol); goto cleanup; } name += strlen(name) + 1; if (volStorageBackendRBDRefreshVolInfo(vol, pool, &ptr) < 0) { virStorageVolDefFree(vol); goto cleanup; } pool->volumes.objs[pool->volumes.count++] = vol; } VIR_DEBUG("Found %zu images in RBD pool %s", pool->volumes.count, pool->def->source.name); ret = 0; cleanup: VIR_FREE(names); virStorageBackendRBDCloseRADOSConn(&ptr); return ret; }