#include <stdio.h>
#include <libgen.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <getopt.h>
#include <stdbool.h>
#include <sys/mman.h>
#include <endian.h>
#include "common.h"
#include "set.h"
#include "libpmem.h"
#include "convert.h"
#include "util_pmem.h"
static const char *help_str = "";
static void
print_usage(char *appname)
{
printf("Usage: %s convert <file>\n", appname);
}
static void
print_version(char *appname)
{
printf("%s %s\n", appname, SRCVERSION);
}
void
pmempool_convert_help(char *appname)
{
print_usage(appname);
print_version(appname);
printf(help_str, appname);
}
typedef int (*convert_func)(void *poolset, void *addr);
static int
convert_v2_v3(void *poolset, void *addr)
{
printf("The conversion can only be made automatically if the\n"
"PMEMmutex, PMEMrwlock and PMEMcond types are not used in the\n"
"pool or all of the variables of those three types are aligned "
"to 8 bytes.\nProceed only if you are sure that the above is "
"true for this pool.\n");
char ans = ask_Yn('?', "convert the pool ?");
if (ans == INV_ANS)
return -1;
if (ans == 'y')
return 0;
return -1;
}
static convert_func version_convert[] = {
NULL,
convert_v1_v2,
convert_v2_v3,
convert_v3_v4,
};
void
pmempool_convert_persist(void *poolset, const void *addr, size_t len)
{
pool_set_file_persist(poolset, addr, len);
}
int
pmempool_convert_func(char *appname, int argc, char *argv[])
{
if (argc != 2) {
print_usage(appname);
return -1;
}
int ret = 0;
const char *f = argv[1];
struct pmem_pool_params params;
if (pmem_pool_parse_params(f, ¶ms, 1)) {
fprintf(stderr, "Cannot determine type of pool.\n");
return -1;
}
if (params.is_part) {
fprintf(stderr, "Conversion cannot be performed on "
"a poolset part.\n");
return -1;
}
if (params.type != PMEM_POOL_TYPE_OBJ) {
fprintf(stderr, "Conversion is currently supported only for "
"pmemobj pools.\n");
return -1;
}
struct pool_set_file *psf = pool_set_file_open(f, 0, 1);
if (psf == NULL) {
perror(f);
return -1;
}
if (psf->poolset->remote) {
fprintf(stderr, "Conversion of remotely replicated pools is "
"currently not supported. Remove the replica first\n");
pool_set_file_close(psf);
return -1;
}
void *addr = pool_set_file_map(psf, 0);
if (addr == NULL) {
perror(f);
ret = -1;
goto out;
}
struct pool_hdr *phdr = addr;
uint32_t m = le32toh(phdr->major);
if (m >= COUNT_OF(version_convert) || !version_convert[m]) {
fprintf(stderr, "There's no conversion method for the pool.\n"
"Please make sure the pmempool utility "
"is up-to-date.\n");
ret = -1;
goto out;
}
printf("This tool will update the pool to the latest available "
"layout version.\nThis process is NOT fail-safe.\n"
"Proceed only if the pool has been backed up or\n"
"the risks are fully understood and acceptable.\n");
char ans = ask_Yn('?', "convert the pool '%s' ?", f);
if (ans == INV_ANS) {
fprintf(stderr, "invalid answer");
ret = -1;
goto out;
}
if (ans != 'y') {
ret = 0;
goto out;
}
PMEMobjpool *pop = addr;
for (unsigned r = 0; r < psf->poolset->nreplicas; ++r) {
struct pool_replica *rep = psf->poolset->replica[r];
for (unsigned p = 0; p < rep->nparts; ++p) {
struct pool_set_part *part = &rep->part[p];
if (util_map_hdr(part, MAP_SHARED, 0) != 0) {
fprintf(stderr, "Failed to map headers.\n"
"Conversion did not start.\n");
ret = -1;
goto out;
}
}
}
uint32_t i;
for (i = m; i < COUNT_OF(version_convert); ++i) {
if (version_convert[i](psf, pop) != 0) {
fprintf(stderr, "Failed to convert the pool\n");
break;
} else {
uint32_t target_m = i + 1;
for (unsigned r = 0; r < psf->poolset->nreplicas; ++r) {
struct pool_replica *rep =
psf->poolset->replica[r];
for (unsigned p = 0; p < rep->nparts; ++p) {
struct pool_set_part *part =
&rep->part[p];
struct pool_hdr *hdr = part->hdr;
hdr->major = htole32(target_m);
util_checksum(hdr, sizeof(*hdr),
&hdr->checksum, 1);
util_persist_auto(part->is_dev_dax, hdr,
sizeof(struct pool_hdr));
}
}
}
}
if (i != m)
printf("The pool has been converted to version %d\n", i);
util_persist_auto(psf->poolset->replica[0]->part[0].is_dev_dax, pop,
psf->size);
out:
for (unsigned r = 0; r < psf->poolset->nreplicas; ++r) {
struct pool_replica *rep = psf->poolset->replica[r];
for (unsigned p = 0; p < rep->nparts; ++p) {
struct pool_set_part *part = &rep->part[p];
if (part->hdr != NULL)
util_unmap_hdr(part);
}
}
pool_set_file_close(psf);
return ret;
}