Skip to content
This repository has been archived by the owner on Nov 17, 2022. It is now read-only.

Commit

Permalink
Merge updates/NFSE-4208-merr-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nabeelmmd committed May 15, 2020
2 parents bb04940 + 825f0cb commit fe867cb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
51 changes: 36 additions & 15 deletions src/mpool/mpool_err.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,14 @@
*/
#define MERR_BASE_SZ (MERR_ALIGN * 64 * 2)

char mpool_merr_base[MERR_BASE_SZ] _merr_attributes = "mpool_merr_bug0u";
char mpool_merr_base[MERR_BASE_SZ] _merr_attributes = "mpool_merr_baseu";
char mpool_merr_bug0[] _merr_attributes = "mpool_merr_bug0u";
char mpool_merr_bug1[] _merr_attributes = "mpool_merr_bug1u";
char mpool_merr_bug2[] _merr_attributes = "mpool_merr_bug2u";
char mpool_merr_bug3[] _merr_attributes = "mpool_merr_bug3u";

extern uint8_t __start_mpool_merr;
extern uint8_t __stop_mpool_merr;

/**
* mpool_merr_lineno() - Return the line number from given merr_t
Expand All @@ -56,6 +61,12 @@ mpool_merr_pack(int errnum, const char *file, int line)
if (errnum < 0)
errnum = -errnum;

/* Ignore files not from our section.
*/
if (file < (char *)&__start_mpool_merr ||
file >= (char *)&__stop_mpool_merr)
goto finish;

if (!file || !IS_ALIGNED((ulong)file, MERR_ALIGN))
file = mpool_merr_bug1;

Expand All @@ -64,6 +75,8 @@ mpool_merr_pack(int errnum, const char *file, int line)
if (((s64)((u64)off << MERR_FILE_SHIFT) >> MERR_FILE_SHIFT) == off)
err = (u64)off << MERR_FILE_SHIFT;

finish:
err |= (line & MERR_LINE_MASK) << MERR_LINE_SHIFT;
err |= ((u64)line << MERR_LINE_SHIFT) & MERR_LINE_MASK;
err |= errnum & MERR_ERRNO_MASK;

Expand All @@ -82,9 +95,15 @@ mpool_merr_file(merr_t err)
return NULL;

off = (s64)(err & MERR_FILE_MASK) >> MERR_FILE_SHIFT;
off *= MERR_ALIGN;
if (off == 0)
return NULL;

file = mpool_merr_base + (off * MERR_ALIGN);

if (file < (char *)&__start_mpool_merr ||
file >= (char *)&__stop_mpool_merr)
return mpool_merr_bug3;

file = mpool_merr_base + off;
len = strnlen(file, PATH_MAX);
file += len;

Expand All @@ -108,7 +127,7 @@ mpool_strerror(
int errnum = mpool_errno(err);

if (errnum == EBUG)
strlcpy(buf, "Software bug", bufsz);
strlcpy(buf, "mpool software bug", bufsz);
else
strerror_r(errnum, buf, bufsz);

Expand All @@ -121,19 +140,21 @@ mpool_strinfo(
char *buf,
size_t bufsz)
{
int off = 0;

if (err) {
if (mpool_merr_file(err))
off = snprintf(buf, bufsz, "%s:%d: ",
mpool_merr_file(err),
mpool_merr_lineno(err));
if (off >= 0 && off < bufsz)
mpool_strerror(err, buf + off, bufsz - off);
} else {
snprintf(buf, bufsz, "Success");
int n = 0;

if (!err) {
strlcpy(buf, "Success", bufsz);
return buf;
}

if (mpool_merr_file(err))
n = snprintf(buf, bufsz, "%s:%d: ",
mpool_merr_file(err),
mpool_merr_lineno(err));

if (n >= 0 && n < bufsz)
mpool_strerror(err, buf + n, bufsz - n);

return buf;
}

Expand Down
6 changes: 3 additions & 3 deletions src/mpool/mpool_err.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@

#define EBUG (666)

/* MERR_ALIGN Alignment of _mpool_merr_file in section ".merr"
/* MERR_ALIGN Alignment of _mpool_merr_file in section "mpool_merr"
*/
#define MERR_ALIGN (1u << 6)
#define MERR_ALIGN (1 << 6)

#define _merr_section __attribute__((__section__(".merr")))
#define _merr_section __attribute__((__section__("mpool_merr")))
#define _merr_attributes _merr_section __aligned(MERR_ALIGN) __maybe_unused

static char _mpool_merr_file[] _merr_attributes = __BASE_FILE__;
Expand Down
2 changes: 1 addition & 1 deletion test/smoke/tests/mptest
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ X=$($sudo mptest list -H $mp1 $mp2 floobie |wc -l)
[ $X -eq 2 ] || err

mptest testerr $mp1 0 |grep Success || err
mptest testerr $mp2 1 |egrep '/mpctl.c:[0-9]+:' || err
mptest testerr $mp2 1 |egrep 'mpctl[.c]{0,2}:[0-9]+:' || err
mptest testerr $mp3 1 |grep 'Operation not permitted' || err

cmd $sudo ${MPOOL_BIN}/mpool destroy "$mp1"
Expand Down

0 comments on commit fe867cb

Please sign in to comment.