/* * pmemlog_check -- log memory pool consistency check * * Returns true if consistent, zero if inconsistent, -1/error if checking * cannot happen due to other errors. */ int pmemlog_check(const char *path) { LOG(3, "path \"%s\"", path); int fd = open(path, O_RDWR); if (fd < 0) { LOG(1, "!open"); return -1; } /* open the pool read-only */ PMEMlog *plp = pmemlog_map_common(fd, 1); int oerrno = errno; close(fd); errno = oerrno; if (plp == NULL) return -1; /* errno set by pmemlog_map_common() */ int consistent = 1; /* validate pool descriptor */ uint64_t hdr_start = le64toh(plp->start_offset); uint64_t hdr_end = le64toh(plp->end_offset); uint64_t hdr_write = le64toh(plp->write_offset); if (hdr_start != roundup(sizeof (*plp), LOG_FORMAT_DATA_ALIGN)) { LOG(1, "wrong value of start_offset"); consistent = 0; } if (hdr_end != plp->size) { LOG(1, "wrong value of end_offset"); consistent = 0; } if (hdr_start > hdr_end) { LOG(1, "start_offset greater than end_offset"); consistent = 0; } if (hdr_start > hdr_write) { LOG(1, "start_offset greater than write_offset"); consistent = 0; } if (hdr_write > hdr_end) { LOG(1, "write_offset greater than end_offset"); consistent = 0; } pmemlog_unmap(plp); if (consistent) LOG(4, "pool consistency check OK"); return consistent; }
/* * pmemlog_map -- map a log memory pool */ PMEMlog * pmemlog_map(int fd) { LOG(3, "fd %d", fd); return pmemlog_map_common(fd, 0); }
/* * pmemlog_check -- log memory pool consistency check * * Returns true if consistent, zero if inconsistent, -1/error if checking * cannot happen due to other errors. */ int pmemlog_check(const char *path) { LOG(3, "path \"%s\"", path); size_t poolsize = 0; int fd; if ((fd = util_pool_open(path, &poolsize, PMEMLOG_MIN_POOL)) == -1) return -1; /* errno set by util_pool_open() */ /* map the pool read-only */ PMEMlogpool *plp = pmemlog_map_common(fd, poolsize, 1, 0); if (plp == NULL) return -1; /* errno set by pmemlog_map_common() */ int consistent = 1; /* validate pool descriptor */ uint64_t hdr_start = le64toh(plp->start_offset); uint64_t hdr_end = le64toh(plp->end_offset); uint64_t hdr_write = le64toh(plp->write_offset); if (hdr_start != roundup(sizeof (*plp), LOG_FORMAT_DATA_ALIGN)) { ERR("wrong value of start_offset"); consistent = 0; } if (hdr_end != plp->size) { ERR("wrong value of end_offset"); consistent = 0; } if (hdr_start > hdr_end) { ERR("start_offset greater than end_offset"); consistent = 0; } if (hdr_start > hdr_write) { ERR("start_offset greater than write_offset"); consistent = 0; } if (hdr_write > hdr_end) { ERR("write_offset greater than end_offset"); consistent = 0; } pmemlog_close(plp); if (consistent) LOG(4, "pool consistency check OK"); return consistent; }
/* * pmemlog_open -- open an existing log memory pool */ PMEMlogpool * pmemlog_open(const char *path) { LOG(3, "path %s", path); size_t poolsize = 0; int fd; if ((fd = util_pool_open(path, &poolsize, PMEMLOG_MIN_POOL)) == -1) return NULL; /* errno set by util_pool_open() */ return pmemlog_map_common(fd, poolsize, 0, 0); }
/* * pmemlog_create -- create a log memory pool */ PMEMlogpool * pmemlog_create(const char *path, size_t poolsize, mode_t mode) { LOG(3, "path %s poolsize %zu mode %d", path, poolsize, mode); int created = 0; int fd; if (poolsize != 0) { /* create a new memory pool file */ fd = util_pool_create(path, poolsize, PMEMLOG_MIN_POOL, mode); created = 1; } else { /* open an existing file */ fd = util_pool_open(path, &poolsize, PMEMLOG_MIN_POOL); } if (fd == -1) return NULL; /* errno set by util_pool_create/open() */ PMEMlogpool *plp = pmemlog_map_common(fd, poolsize, 0, 1); if (plp == NULL && created) unlink(path); /* delete file if pool creation failed */ return plp; }