/* * util_pool_create -- create a new memory pool (set or a single file) * * On success returns 0 and a pointer to a newly allocated structure * containing the info of all the parts of the pool set and replicas. */ int util_pool_create(struct pool_set **setp, const char *path, size_t poolsize, size_t minsize, size_t hdrsize, const char *sig, uint32_t major, uint32_t compat, uint32_t incompat, uint32_t ro_compat) { LOG(3, "setp %p path %s poolsize %zu minsize %zu " "hdrsize %zu sig %s major %u " "compat %#x incompat %#x ro_comapt %#x", setp, path, poolsize, minsize, hdrsize, sig, major, compat, incompat, ro_compat); int flags = MAP_SHARED; int ret = util_poolset_create(setp, path, poolsize, minsize); if (ret < 0) { LOG(2, "cannot create pool set"); return -1; } struct pool_set *set = *setp; ASSERT(set->nreplicas > 0); set->zeroed = 1; set->poolsize = SIZE_MAX; /* generate pool set UUID */ uuid_generate(set->uuid); /* generate UUID's for all the parts */ for (unsigned r = 0; r < set->nreplicas; r++) { struct pool_replica *rep = set->replica[r]; for (unsigned i = 0; i < rep->nparts; i++) uuid_generate(rep->part[i].uuid); } for (unsigned r = 0; r < set->nreplicas; r++) { if (util_replica_create(set, r, flags, hdrsize, sig, major, compat, incompat, ro_compat) != 0) { LOG(2, "replica creation failed"); goto err; } } return 0; err: LOG(4, "error clean up"); int oerrno = errno; for (unsigned r = 0; r < set->nreplicas; r++) { struct pool_replica *rep = set->replica[r]; VALGRIND_REMOVE_PMEM_MAPPING(rep->part[0].addr, rep->part[0].size); util_unmap(rep->part[0].addr, rep->part[0].size); } util_poolset_close(set, 1); errno = oerrno; return -1; }
/* * util_pool_create_uuids -- create a new memory pool (set or a single file) * with given uuids * * On success returns 0 and a pointer to a newly allocated structure * containing the info of all the parts of the pool set and replicas. */ int util_pool_create_uuids(struct pool_set **setp, const char *path, size_t poolsize, size_t minsize, const char *sig, uint32_t major, uint32_t compat, uint32_t incompat, uint32_t ro_compat, const unsigned char *poolset_uuid, const unsigned char *first_part_uuid, const unsigned char *prev_repl_uuid, const unsigned char *next_repl_uuid, const unsigned char *arch_flags) { LOG(3, "setp %p path %s poolsize %zu minsize %zu " "sig %.8s major %u compat %#x incompat %#x ro_comapt %#x " "poolset_uuid %p first_part_uuid %p" "prev_repl_uuid %p next_repl_uuid %p arch_flags %p", setp, path, poolsize, minsize, sig, major, compat, incompat, ro_compat, poolset_uuid, first_part_uuid, prev_repl_uuid, next_repl_uuid, arch_flags); int flags = MAP_SHARED; int ret = util_poolset_create(setp, path, poolsize, minsize); if (ret < 0) { LOG(2, "cannot create pool set"); return -1; } struct pool_set *set = *setp; ASSERT(set->nreplicas > 0); set->zeroed = 1; if (poolset_uuid) { memcpy(set->uuid, poolset_uuid, POOL_HDR_UUID_LEN); } else { /* generate pool set UUID */ ret = util_uuid_generate(set->uuid); if (ret < 0) { LOG(2, "cannot generate pool set UUID"); util_poolset_close(*setp, 1); return -1; } } /* generate UUID's for all the parts */ for (unsigned r = 0; r < set->nreplicas; r++) { struct pool_replica *rep = set->replica[r]; for (unsigned i = 0; i < rep->nparts; i++) { ret = util_uuid_generate(rep->part[i].uuid); if (ret < 0) { LOG(2, "cannot generate pool set part UUID"); util_poolset_close(*setp, 1); return -1; } } } /* overwrite UUID of the first part if given */ if (first_part_uuid) { memcpy(set->replica[0]->part[0].uuid, first_part_uuid, POOL_HDR_UUID_LEN); } if (set->nreplicas == 1 && prev_repl_uuid && next_repl_uuid) { if (util_replica_create(set, 0, flags, sig, major, compat, incompat, ro_compat, prev_repl_uuid, next_repl_uuid, arch_flags) != 0) { LOG(2, "replica creation failed"); goto err; } } else { for (unsigned r = 0; r < set->nreplicas; r++) { if (util_replica_create(set, r, flags, sig, major, compat, incompat, ro_compat, NULL, NULL, NULL) != 0) { LOG(2, "replica creation failed"); goto err; } } } return 0; err: LOG(4, "error clean up"); int oerrno = errno; for (unsigned r = 0; r < set->nreplicas; r++) util_replica_close(set, r); util_poolset_close(set, 1); errno = oerrno; return -1; }