struct json_object *util_mapping_to_json(struct ndctl_mapping *mapping) { struct json_object *jmapping = json_object_new_object(); struct ndctl_dimm *dimm = ndctl_mapping_get_dimm(mapping); struct json_object *jobj; if (!jmapping) return NULL; jobj = json_object_new_string(ndctl_dimm_get_devname(dimm)); if (!jobj) goto err; json_object_object_add(jmapping, "dimm", jobj); jobj = json_object_new_int64(ndctl_mapping_get_offset(mapping)); if (!jobj) goto err; json_object_object_add(jmapping, "offset", jobj); jobj = json_object_new_int64(ndctl_mapping_get_length(mapping)); if (!jobj) goto err; json_object_object_add(jmapping, "length", jobj); return jmapping; err: json_object_put(jmapping); return NULL; }
static struct json_object *region_to_json(struct ndctl_region *region) { struct json_object *jregion = json_object_new_object(); struct json_object *jobj, *jmappings = NULL; struct ndctl_interleave_set *iset; struct ndctl_mapping *mapping; if (!jregion) return NULL; jobj = json_object_new_string(ndctl_region_get_devname(region)); if (!jobj) goto err; json_object_object_add(jregion, "dev", jobj); jobj = json_object_new_int64(ndctl_region_get_size(region)); if (!jobj) goto err; json_object_object_add(jregion, "size", jobj); jobj = json_object_new_int64(ndctl_region_get_available_size(region)); if (!jobj) goto err; json_object_object_add(jregion, "available_size", jobj); switch (ndctl_region_get_type(region)) { case ND_DEVICE_REGION_PMEM: jobj = json_object_new_string("pmem"); break; case ND_DEVICE_REGION_BLK: jobj = json_object_new_string("blk"); break; default: jobj = NULL; } if (!jobj) goto err; json_object_object_add(jregion, "type", jobj); iset = ndctl_region_get_interleave_set(region); if (iset) { jobj = json_object_new_int64( ndctl_interleave_set_get_cookie(iset)); if (!jobj) fail("\n"); else json_object_object_add(jregion, "iset_id", jobj); } ndctl_mapping_foreach(region, mapping) { struct ndctl_dimm *dimm = ndctl_mapping_get_dimm(mapping); struct json_object *jmapping; if (!list.dimms) break; if (!list.idle && !ndctl_dimm_is_enabled(dimm)) continue; if (!jmappings) { jmappings = json_object_new_array(); if (!jmappings) { fail("\n"); continue; } json_object_object_add(jregion, "mappings", jmappings); } jmapping = util_mapping_to_json(mapping); if (!jmapping) { fail("\n"); continue; } json_object_array_add(jmappings, jmapping); } if (!ndctl_region_is_enabled(region)) { jobj = json_object_new_string("disabled"); if (!jobj) goto err; json_object_object_add(jregion, "state", jobj); } list_namespaces(region, jregion, NULL, false); return jregion; err: fail("\n"); json_object_put(jregion); return NULL; }