Skip to content

Commit

Permalink
Merge pull request #5987 from grom72/core-log-error
Browse files Browse the repository at this point in the history
Core log error
  • Loading branch information
janekmi authored Jan 31, 2024
2 parents 4f341b7 + a201bbd commit e67b654
Show file tree
Hide file tree
Showing 31 changed files with 262 additions and 169 deletions.
13 changes: 7 additions & 6 deletions src/common/bad_blocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ badblocks_clear(const char *file, struct badblocks *bbs)

ret = pmem2_badblock_context_new(&bbctx, src);
if (ret) {
LOG(1, "pmem2_badblock_context_new failed -- %s", file);
CORE_LOG_ERROR("pmem2_badblock_context_new failed -- %s", file);
goto exit_delete_source;
}

Expand All @@ -157,7 +157,7 @@ badblocks_clear(const char *file, struct badblocks *bbs)
bb.length = bbs->bbv[b].length;
ret = pmem2_badblock_clear(bbctx, &bb);
if (ret) {
LOG(1, "pmem2_badblock_clear -- %s", file);
CORE_LOG_ERROR("pmem2_badblock_clear -- %s", file);
goto exit_delete_ctx;
}
}
Expand Down Expand Up @@ -206,14 +206,14 @@ badblocks_clear_all(const char *file)

ret = pmem2_badblock_context_new(&bbctx, src);
if (ret) {
LOG(1, "pmem2_badblock_context_new failed -- %s", file);
CORE_LOG_ERROR("pmem2_badblock_context_new failed -- %s", file);
goto exit_delete_source;
}

while ((pmem2_badblock_next(bbctx, &bb)) == 0) {
ret = pmem2_badblock_clear(bbctx, &bb);
if (ret) {
LOG(1, "pmem2_badblock_clear -- %s", file);
CORE_LOG_ERROR("pmem2_badblock_clear -- %s", file);
goto exit_delete_ctx;
}
};
Expand Down Expand Up @@ -251,12 +251,13 @@ badblocks_check_file(const char *file)

long bbsc = badblocks_count(file);
if (bbsc < 0) {
LOG(1, "counting bad blocks failed -- '%s'", file);
CORE_LOG_ERROR("counting bad blocks failed -- '%s'", file);
return -1;
}

if (bbsc > 0) {
LOG(1, "pool file '%s' contains %li bad block(s)", file, bbsc);
CORE_LOG_ERROR("pool file '%s' contains %li bad block(s)", file,
bbsc);
return 1;
}

Expand Down
2 changes: 1 addition & 1 deletion src/common/ctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ ctl_exec_query_write(void *ctx, const struct ctl_node *n,

void *real_arg = ctl_query_get_real_args(n, arg, source);
if (real_arg == NULL) {
LOG(1, "Invalid arguments");
CORE_LOG_ERROR("Invalid arguments");
return -1;
}

Expand Down
2 changes: 1 addition & 1 deletion src/common/file_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ device_dax_alignment(const char *path)

int fd = os_open(path, O_RDONLY);
if (fd == -1) {
LOG(1, "Cannot open file %s", path);
CORE_LOG_ERROR("Cannot open file %s", path);
return size;
}

Expand Down
2 changes: 1 addition & 1 deletion src/common/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ util_map(int fd, os_off_t off, size_t len, int flags, int rdonly,
void *base;
void *addr = util_map_hint(len, req_align);
if (addr == MAP_FAILED) {
LOG(1, "cannot find a contiguous region of given size");
CORE_LOG_ERROR("cannot find a contiguous region of given size");
return NULL;
}

Expand Down
18 changes: 10 additions & 8 deletions src/common/os_deep_linux.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2017-2023, Intel Corporation */
/* Copyright 2017-2024, Intel Corporation */

/*
* os_deep_linux.c -- Linux abstraction layer
Expand Down Expand Up @@ -35,7 +35,8 @@ os_deep_type(const struct map_tracker *mt, void *addr, size_t len)
if (ret < 0) {
if (ret == PMEM2_E_NOSUPP) {
errno = ENOTSUP;
LOG(1, "!deep_flush not supported");
CORE_LOG_ERROR_WITH_ERRNO(
"deep_flush not supported");
} else {
errno = pmem2_err_to_errno(ret);
LOG(2, "cannot write to deep_flush"
Expand Down Expand Up @@ -122,7 +123,7 @@ os_part_deep_common(struct pool_replica *rep, unsigned partidx, void *addr,
return 0;

if (pmem_msync(addr, len)) {
LOG(1, "pmem_msync(%p, %lu)", addr, len);
CORE_LOG_ERROR("pmem_msync(%p, %lu)", addr, len);
return -1;
}
return 0;
Expand Down Expand Up @@ -151,16 +152,17 @@ os_part_deep_common(struct pool_replica *rep, unsigned partidx, void *addr,
if (ret < 0) {
if (errno == ENOENT) {
errno = ENOTSUP;
LOG(1, "!deep_flush not supported");
CORE_LOG_ERROR_WITH_ERRNO(
"deep_flush not supported");
} else {
LOG(1, "invalid dax_region id %u", region_id);
CORE_LOG_ERROR("invalid dax_region id %u",
region_id);
}
return -1;
}

if (pmem2_deep_flush_write(region_id)) {
LOG(1, "pmem2_deep_flush_write(%u)",
region_id);
CORE_LOG_ERROR("pmem2_deep_flush_write(%u)", region_id);
return -1;
}
} else {
Expand All @@ -169,7 +171,7 @@ os_part_deep_common(struct pool_replica *rep, unsigned partidx, void *addr,
* call msync on one page.
*/
if (pmem_msync(addr, MIN(Pagesize, len))) {
LOG(1, "pmem_msync(%p, %lu)", addr, len);
CORE_LOG_ERROR("pmem_msync(%p, %lu)", addr, len);
return -1;
}
}
Expand Down
42 changes: 24 additions & 18 deletions src/common/set.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ util_map_hdr(struct pool_set_part *part, int flags, int rdonly)
/* this is required only for Device DAX & memcheck */
addr = util_map_hint(hdrsize, hdrsize);
if (addr == MAP_FAILED) {
LOG(1, "cannot find a contiguous region of given size");
CORE_LOG_ERROR(
"cannot find a contiguous region of given size");
/* there's nothing we can do */
return -1;
}
Expand Down Expand Up @@ -424,8 +425,8 @@ util_poolset_chmod(struct pool_set *set, mode_t mode)
}

if (stbuf.st_mode & ~(unsigned)S_IFMT) {
LOG(1, "file permissions changed during pool "
"initialization, file: %s (%o)",
CORE_LOG_WARNING(
"file permissions changed during pool initialization, file: %s (%o)",
part->path,
stbuf.st_mode & ~(unsigned)S_IFMT);
}
Expand Down Expand Up @@ -1959,7 +1960,8 @@ util_replica_map_local(struct pool_set *set, unsigned repidx, int flags)
/* determine a hint address for mmap() */
addr = util_map_hint(rep->resvsize, 0);
if (addr == MAP_FAILED) {
LOG(1, "cannot find a contiguous region of given size");
CORE_LOG_ERROR(
"cannot find a contiguous region of given size");
return -1;
}

Expand Down Expand Up @@ -2434,7 +2436,7 @@ util_pool_create_uuids(struct pool_set **setp, const char *path,
(attr->features.compat & POOL_FEAT_CHECK_BAD_BLOCKS)) {
int bbs = badblocks_check_poolset(set, 1 /* create */);
if (bbs < 0) {
LOG(1,
CORE_LOG_ERROR(
"failed to check pool set for bad blocks -- '%s'",
path);
goto err_poolset_free;
Expand Down Expand Up @@ -2579,7 +2581,8 @@ util_replica_open_local(struct pool_set *set, unsigned repidx, int flags)
if (addr == NULL)
addr = util_map_hint(rep->resvsize, 0);
if (addr == MAP_FAILED) {
LOG(1, "cannot find a contiguous region of given size");
CORE_LOG_ERROR(
"cannot find a contiguous region of given size");
return -1;
}

Expand Down Expand Up @@ -2865,21 +2868,22 @@ util_pool_open_nocheck(struct pool_set *set, unsigned flags)
return -1;
}
if (bfe < 0) {
LOG(1,
CORE_LOG_ERROR(
"an error occurred when checking whether recovery file exists.");
return -1;
}

int bbs = badblocks_check_poolset(set, 0 /* not create */);
if (bbs < 0) {
LOG(1, "failed to check pool set for bad blocks");
CORE_LOG_ERROR(
"failed to check pool set for bad blocks");
return -1;
}

if (bbs > 0) {
if (flags & POOL_OPEN_IGNORE_BAD_BLOCKS) {
LOG(1,
"WARNING: pool set contains bad blocks, ignoring");
CORE_LOG_WARNING(
"pool set contains bad blocks, ignoring");
} else {
ERR_WO_ERRNO(
"pool set contains bad blocks and cannot be opened, run 'pmempool sync --bad-blocks' utility to try to recover the pool");
Expand Down Expand Up @@ -2936,14 +2940,16 @@ util_read_compat_features(struct pool_set *set, uint32_t *compat_features)
struct pool_set_part *part = &rep->part[p];

if (util_part_open(part, 0, 0 /* create */)) {
LOG(1, "!cannot open the part -- \"%s\"",
CORE_LOG_WARNING_W_ERRNO(
"cannot open the part -- \"%s\"",
part->path);
/* try to open the next part */
continue;
}

if (util_map_hdr(part, MAP_SHARED, 0) != 0) {
LOG(1, "header mapping failed -- \"%s\"",
CORE_LOG_ERROR(
"header mapping failed -- \"%s\"",
part->path);
util_part_fdclose(part);
return -1;
Expand Down Expand Up @@ -3009,7 +3015,7 @@ util_pool_open(struct pool_set **setp, const char *path, size_t minpartsize,
uint32_t compat_features;

if (util_read_compat_features(set, &compat_features)) {
LOG(1, "reading compat features failed");
CORE_LOG_ERROR("reading compat features failed");
goto err_poolset_free;
}

Expand All @@ -3024,23 +3030,23 @@ util_pool_open(struct pool_set **setp, const char *path, size_t minpartsize,
}

if (bfe < 0) {
LOG(1,
CORE_LOG_ERROR(
"an error occurred when checking whether recovery file exists.");
goto err_poolset_free;
}

int bbs = badblocks_check_poolset(set, 0 /* not create */);
if (bbs < 0) {
LOG(1,
CORE_LOG_ERROR(
"failed to check pool set for bad blocks -- '%s'",
path);
goto err_poolset_free;
}

if (bbs > 0) {
if (flags & POOL_OPEN_IGNORE_BAD_BLOCKS) {
LOG(1,
"WARNING: pool set contains bad blocks, ignoring -- '%s'",
CORE_LOG_WARNING(
"pool set contains bad blocks, ignoring -- '%s'",
path);
} else {
ERR_WO_ERRNO(
Expand Down Expand Up @@ -3295,7 +3301,7 @@ util_replica_deep_common(const void *addr, size_t len, struct pool_set *set,
replica_id, part, (void *)range_start, range_len);
if (os_part_deep_common(rep, p, (void *)range_start,
range_len, flush)) {
LOG(1, "os_part_deep_common(%p, %p, %lu)",
CORE_LOG_ERROR("os_part_deep_common(%p, %p, %lu)",
part, (void *)range_start, range_len);
return -1;
}
Expand Down
5 changes: 3 additions & 2 deletions src/common/set_badblocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ badblocks_check_poolset(struct pool_set *set, int create)
}

if (cfcb.n_files_bbs) {
LOG(1, "%i pool file(s) contain bad blocks", cfcb.n_files_bbs);
CORE_LOG_ERROR("%i pool file(s) contain bad blocks",
cfcb.n_files_bbs);
set->has_bad_blocks = 1;
}

Expand Down Expand Up @@ -209,7 +210,7 @@ badblocks_recovery_file_exists(struct pool_set *set)
char *rec_file =
badblocks_recovery_file_alloc(set->path, r, p);
if (rec_file == NULL) {
LOG(1,
CORE_LOG_ERROR(
"allocating name of bad block recovery file failed");
return -1;
}
Expand Down
57 changes: 51 additions & 6 deletions src/core/log_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@
#ifndef CORE_LOG_INTERNAL_H
#define CORE_LOG_INTERNAL_H

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>

#ifdef ATOMIC_OPERATIONS_SUPPORTED
#include <stdatomic.h>
#endif /* ATOMIC_OPERATIONS_SUPPORTED */
Expand Down Expand Up @@ -147,13 +144,26 @@ void core_log_default_function(void *context, enum core_log_level level,
} while (0)

#define CORE_LOG_MAX_ERR_MSG 128
#ifdef _GNU_SOURCE
#define CORE_LOG_FATAL_W_ERRNO(format, ...) \
do { \
char buff[CORE_LOG_MAX_ERR_MSG]; \
CORE_LOG(CORE_LOG_LEVEL_FATAL, format ": %s", ##__VA_ARGS__, \
strerror_r(errno, buff, CORE_LOG_MAX_ERR_MSG)); \
abort(); \
} while (0)
#else
#define CORE_LOG_FATAL_W_ERRNO(format, ...) \
do { \
char buff[CORE_LOG_MAX_ERR_MSG]; \
uint64_t ret = (uint64_t)strerror_r(errno, buff, \
CORE_LOG_MAX_ERR_MSG); \
ret = ret; \
CORE_LOG(CORE_LOG_LEVEL_FATAL, format ": %s", ##__VA_ARGS__, \
buff); \
abort(); \
} while (0)
#endif

#define CORE_LOG_ALWAYS(format, ...) \
CORE_LOG(CORE_LOG_LEVEL_ALWAYS, format, ##__VA_ARGS__)
Expand All @@ -163,8 +173,43 @@ void core_log_default_function(void *context, enum core_log_level level,
* 'f' stands here for 'function' or 'format' where the latter may accept
* additional arguments.
*/
#define CORE_LOG_ERROR_WITH_ERRNO(f, ...) \
CORE_LOG_ERROR(f ": %s", ##__VA_ARGS__, strerror(errno))
#ifdef _GNU_SOURCE
#define CORE_LOG_ERROR_WITH_ERRNO(format, ...) \
do { \
char buff[CORE_LOG_MAX_ERR_MSG]; \
CORE_LOG(CORE_LOG_LEVEL_ERROR, format ": %s", ##__VA_ARGS__, \
strerror_r(errno, buff, CORE_LOG_MAX_ERR_MSG)); \
} while (0)
#else
#define CORE_LOG_ERROR_WITH_ERRNO(format, ...) \
do { \
char buff[CORE_LOG_MAX_ERR_MSG]; \
uint64_t ret = (uint64_t)strerror_r(errno, buff, \
CORE_LOG_MAX_ERR_MSG); \
ret = ret; \
CORE_LOG(CORE_LOG_LEVEL_ERROR, format ": %s", ##__VA_ARGS__, \
buff); \
} while (0)
#endif

#ifdef _GNU_SOURCE
#define CORE_LOG_WARNING_W_ERRNO(format, ...) \
do { \
char buff[CORE_LOG_MAX_ERR_MSG]; \
CORE_LOG(CORE_LOG_LEVEL_WARNING, format ": %s", ##__VA_ARGS__, \
strerror_r(errno, buff, CORE_LOG_MAX_ERR_MSG)); \
} while (0)
#else
#define CORE_LOG_WARNING_W_ERRNO(format, ...) \
do { \
char buff[CORE_LOG_MAX_ERR_MSG]; \
uint64_t ret = (uint64_t)strerror_r(errno, buff, \
CORE_LOG_MAX_ERR_MSG); \
ret = ret; \
CORE_LOG(CORE_LOG_LEVEL_WARNING, format ": %s", ##__VA_ARGS__, \
buff); \
} while (0)
#endif

static inline int
core_log_error_translate(int ret)
Expand Down
Loading

0 comments on commit e67b654

Please sign in to comment.