static struct json_object *list_namespaces(struct ndctl_region *region, struct json_object *container, struct json_object *jnamespaces, bool continue_array) { struct ndctl_namespace *ndns; ndctl_namespace_foreach(region, ndns) { struct json_object *jndns; /* are we emitting namespaces? */ if (!list.namespaces) break; if (!list.idle && !util_namespace_active(ndns)) continue; if (!jnamespaces) { jnamespaces = json_object_new_array(); if (!jnamespaces) { fail("\n"); continue; } if (container) json_object_object_add(container, "namespaces", jnamespaces); } jndns = util_namespace_to_json(ndns); if (!jndns) { fail("\n"); continue; } json_object_array_add(jnamespaces, jndns); } /* * We we are collecting namespaces anonymously across the * platform / bus */ if (continue_array) return jnamespaces; return NULL; }
struct json_object *util_namespace_to_json(struct ndctl_namespace *ndns) { struct json_object *jndns = json_object_new_object(); unsigned long long size = ULLONG_MAX; enum ndctl_namespace_mode mode; struct json_object *jobj; const char *bdev = NULL; struct ndctl_btt *btt; struct ndctl_pfn *pfn; struct ndctl_dax *dax; char buf[40]; uuid_t uuid; if (!jndns) return NULL; jobj = json_object_new_string(ndctl_namespace_get_devname(ndns)); if (!jobj) goto err; json_object_object_add(jndns, "dev", jobj); btt = ndctl_namespace_get_btt(ndns); dax = ndctl_namespace_get_dax(ndns); pfn = ndctl_namespace_get_pfn(ndns); mode = ndctl_namespace_get_mode(ndns); switch (mode) { case NDCTL_NS_MODE_MEMORY: if (pfn) /* dynamic memory mode */ size = ndctl_pfn_get_size(pfn); else /* native/static memory mode */ size = ndctl_namespace_get_size(ndns); jobj = json_object_new_string("memory"); break; case NDCTL_NS_MODE_DAX: size = ndctl_dax_get_size(dax); jobj = json_object_new_string("dax"); break; case NDCTL_NS_MODE_SAFE: jobj = json_object_new_string("sector"); break; case NDCTL_NS_MODE_RAW: size = ndctl_namespace_get_size(ndns); jobj = json_object_new_string("raw"); break; default: jobj = NULL; } if (jobj) json_object_object_add(jndns, "mode", jobj); if (size < ULLONG_MAX) { jobj = json_object_new_int64(size); if (jobj) json_object_object_add(jndns, "size", jobj); } if (btt) { ndctl_btt_get_uuid(btt, uuid); uuid_unparse(uuid, buf); jobj = json_object_new_string(buf); if (!jobj) goto err; json_object_object_add(jndns, "uuid", jobj); jobj = json_object_new_int(ndctl_btt_get_sector_size(btt)); if (!jobj) goto err; json_object_object_add(jndns, "sector_size", jobj); bdev = ndctl_btt_get_block_device(btt); } else if (pfn) { ndctl_pfn_get_uuid(pfn, uuid); uuid_unparse(uuid, buf); jobj = json_object_new_string(buf); if (!jobj) goto err; json_object_object_add(jndns, "uuid", jobj); bdev = ndctl_pfn_get_block_device(pfn); } else if (dax) { ndctl_dax_get_uuid(dax, uuid); uuid_unparse(uuid, buf); jobj = json_object_new_string(buf); if (!jobj) goto err; json_object_object_add(jndns, "uuid", jobj); } else if (ndctl_namespace_get_type(ndns) != ND_DEVICE_NAMESPACE_IO) { const char *name; ndctl_namespace_get_uuid(ndns, uuid); uuid_unparse(uuid, buf); jobj = json_object_new_string(buf); if (!jobj) goto err; json_object_object_add(jndns, "uuid", jobj); name = ndctl_namespace_get_alt_name(ndns); if (name[0]) { jobj = json_object_new_string(name); if (!jobj) goto err; json_object_object_add(jndns, "name", jobj); } bdev = ndctl_namespace_get_block_device(ndns); } else bdev = ndctl_namespace_get_block_device(ndns); if (bdev && bdev[0]) { jobj = json_object_new_string(bdev); if (!jobj) goto err; json_object_object_add(jndns, "blockdev", jobj); } if (!util_namespace_active(ndns)) { jobj = json_object_new_string("disabled"); if (!jobj) goto err; json_object_object_add(jndns, "state", jobj); } return jndns; err: json_object_put(jndns); return NULL; }