Skip to content

Commit

Permalink
Allocate zap_attribute_t from kmem instead of stack
Browse files Browse the repository at this point in the history
This patch is preparatory work for long name feature. It changes all
users of zap_attribute_t to allocate it from kmem instead of stack. It
also make zap_attribute_t and zap_name_t structure variable length.

Reviewed-by: Tony Hutter <[email protected]>
Reviewed-by: Brian Behlendorf <[email protected]>
Reviewed-by: Alexander Motin <[email protected]>
Signed-off-by: Chunwei Chen <[email protected]>
Closes #15921
  • Loading branch information
sanjeevbagewadinutanix authored and behlendorf committed Oct 1, 2024
1 parent 141368a commit 3cf2bfa
Show file tree
Hide file tree
Showing 35 changed files with 513 additions and 365 deletions.
130 changes: 71 additions & 59 deletions cmd/zdb/zdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -1117,61 +1117,62 @@ dump_zap(objset_t *os, uint64_t object, void *data, size_t size)
{
(void) data, (void) size;
zap_cursor_t zc;
zap_attribute_t attr;
zap_attribute_t *attrp = zap_attribute_alloc();
void *prop;
unsigned i;

dump_zap_stats(os, object);
(void) printf("\n");

for (zap_cursor_init(&zc, os, object);
zap_cursor_retrieve(&zc, &attr) == 0;
zap_cursor_retrieve(&zc, attrp) == 0;
zap_cursor_advance(&zc)) {
boolean_t key64 =
!!(zap_getflags(zc.zc_zap) & ZAP_FLAG_UINT64_KEY);

if (key64)
(void) printf("\t\t0x%010lx = ",
*(uint64_t *)attr.za_name);
*(uint64_t *)attrp->za_name);
else
(void) printf("\t\t%s = ", attr.za_name);
(void) printf("\t\t%s = ", attrp->za_name);

if (attr.za_num_integers == 0) {
if (attrp->za_num_integers == 0) {
(void) printf("\n");
continue;
}
prop = umem_zalloc(attr.za_num_integers *
attr.za_integer_length, UMEM_NOFAIL);
prop = umem_zalloc(attrp->za_num_integers *
attrp->za_integer_length, UMEM_NOFAIL);

if (key64)
(void) zap_lookup_uint64(os, object,
(const uint64_t *)attr.za_name, 1,
attr.za_integer_length, attr.za_num_integers,
(const uint64_t *)attrp->za_name, 1,
attrp->za_integer_length, attrp->za_num_integers,
prop);
else
(void) zap_lookup(os, object, attr.za_name,
attr.za_integer_length, attr.za_num_integers,
(void) zap_lookup(os, object, attrp->za_name,
attrp->za_integer_length, attrp->za_num_integers,
prop);

if (attr.za_integer_length == 1 && !key64) {
if (strcmp(attr.za_name,
if (attrp->za_integer_length == 1 && !key64) {
if (strcmp(attrp->za_name,
DSL_CRYPTO_KEY_MASTER_KEY) == 0 ||
strcmp(attr.za_name,
strcmp(attrp->za_name,
DSL_CRYPTO_KEY_HMAC_KEY) == 0 ||
strcmp(attr.za_name, DSL_CRYPTO_KEY_IV) == 0 ||
strcmp(attr.za_name, DSL_CRYPTO_KEY_MAC) == 0 ||
strcmp(attr.za_name, DMU_POOL_CHECKSUM_SALT) == 0) {
strcmp(attrp->za_name, DSL_CRYPTO_KEY_IV) == 0 ||
strcmp(attrp->za_name, DSL_CRYPTO_KEY_MAC) == 0 ||
strcmp(attrp->za_name,
DMU_POOL_CHECKSUM_SALT) == 0) {
uint8_t *u8 = prop;

for (i = 0; i < attr.za_num_integers; i++) {
for (i = 0; i < attrp->za_num_integers; i++) {
(void) printf("%02x", u8[i]);
}
} else {
(void) printf("%s", (char *)prop);
}
} else {
for (i = 0; i < attr.za_num_integers; i++) {
switch (attr.za_integer_length) {
for (i = 0; i < attrp->za_num_integers; i++) {
switch (attrp->za_integer_length) {
case 1:
(void) printf("%u ",
((uint8_t *)prop)[i]);
Expand All @@ -1192,9 +1193,11 @@ dump_zap(objset_t *os, uint64_t object, void *data, size_t size)
}
}
(void) printf("\n");
umem_free(prop, attr.za_num_integers * attr.za_integer_length);
umem_free(prop,
attrp->za_num_integers * attrp->za_integer_length);
}
zap_cursor_fini(&zc);
zap_attribute_free(attrp);
}

static void
Expand Down Expand Up @@ -1295,72 +1298,74 @@ dump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size)
{
(void) data, (void) size;
zap_cursor_t zc;
zap_attribute_t attr;
zap_attribute_t *attrp = zap_attribute_alloc();

dump_zap_stats(os, object);
(void) printf("\n");

for (zap_cursor_init(&zc, os, object);
zap_cursor_retrieve(&zc, &attr) == 0;
zap_cursor_retrieve(&zc, attrp) == 0;
zap_cursor_advance(&zc)) {
(void) printf("\t\t%s = ", attr.za_name);
if (attr.za_num_integers == 0) {
(void) printf("\t\t%s = ", attrp->za_name);
if (attrp->za_num_integers == 0) {
(void) printf("\n");
continue;
}
(void) printf(" %llx : [%d:%d:%d]\n",
(u_longlong_t)attr.za_first_integer,
(int)ATTR_LENGTH(attr.za_first_integer),
(int)ATTR_BSWAP(attr.za_first_integer),
(int)ATTR_NUM(attr.za_first_integer));
(u_longlong_t)attrp->za_first_integer,
(int)ATTR_LENGTH(attrp->za_first_integer),
(int)ATTR_BSWAP(attrp->za_first_integer),
(int)ATTR_NUM(attrp->za_first_integer));
}
zap_cursor_fini(&zc);
zap_attribute_free(attrp);
}

static void
dump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size)
{
(void) data, (void) size;
zap_cursor_t zc;
zap_attribute_t attr;
zap_attribute_t *attrp = zap_attribute_alloc();
uint16_t *layout_attrs;
unsigned i;

dump_zap_stats(os, object);
(void) printf("\n");

for (zap_cursor_init(&zc, os, object);
zap_cursor_retrieve(&zc, &attr) == 0;
zap_cursor_retrieve(&zc, attrp) == 0;
zap_cursor_advance(&zc)) {
(void) printf("\t\t%s = [", attr.za_name);
if (attr.za_num_integers == 0) {
(void) printf("\t\t%s = [", attrp->za_name);
if (attrp->za_num_integers == 0) {
(void) printf("\n");
continue;
}

VERIFY(attr.za_integer_length == 2);
layout_attrs = umem_zalloc(attr.za_num_integers *
attr.za_integer_length, UMEM_NOFAIL);
VERIFY(attrp->za_integer_length == 2);
layout_attrs = umem_zalloc(attrp->za_num_integers *
attrp->za_integer_length, UMEM_NOFAIL);

VERIFY(zap_lookup(os, object, attr.za_name,
attr.za_integer_length,
attr.za_num_integers, layout_attrs) == 0);
VERIFY(zap_lookup(os, object, attrp->za_name,
attrp->za_integer_length,
attrp->za_num_integers, layout_attrs) == 0);

for (i = 0; i != attr.za_num_integers; i++)
for (i = 0; i != attrp->za_num_integers; i++)
(void) printf(" %d ", (int)layout_attrs[i]);
(void) printf("]\n");
umem_free(layout_attrs,
attr.za_num_integers * attr.za_integer_length);
attrp->za_num_integers * attrp->za_integer_length);
}
zap_cursor_fini(&zc);
zap_attribute_free(attrp);
}

static void
dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
{
(void) data, (void) size;
zap_cursor_t zc;
zap_attribute_t attr;
zap_attribute_t *attrp = zap_attribute_alloc();
const char *typenames[] = {
/* 0 */ "not specified",
/* 1 */ "FIFO",
Expand All @@ -1384,13 +1389,14 @@ dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
(void) printf("\n");

for (zap_cursor_init(&zc, os, object);
zap_cursor_retrieve(&zc, &attr) == 0;
zap_cursor_retrieve(&zc, attrp) == 0;
zap_cursor_advance(&zc)) {
(void) printf("\t\t%s = %lld (type: %s)\n",
attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
attrp->za_name, ZFS_DIRENT_OBJ(attrp->za_first_integer),
typenames[ZFS_DIRENT_TYPE(attrp->za_first_integer)]);
}
zap_cursor_fini(&zc);
zap_attribute_free(attrp);
}

static int
Expand Down Expand Up @@ -2155,23 +2161,25 @@ dump_brt(spa_t *spa)
continue;

zap_cursor_t zc;
zap_attribute_t za;
zap_attribute_t *za = zap_attribute_alloc();
for (zap_cursor_init(&zc, brt->brt_mos, brtvd->bv_mos_entries);
zap_cursor_retrieve(&zc, &za) == 0;
zap_cursor_retrieve(&zc, za) == 0;
zap_cursor_advance(&zc)) {
uint64_t refcnt;
VERIFY0(zap_lookup_uint64(brt->brt_mos,
brtvd->bv_mos_entries,
(const uint64_t *)za.za_name, 1,
za.za_integer_length, za.za_num_integers, &refcnt));
(const uint64_t *)za->za_name, 1,
za->za_integer_length, za->za_num_integers,
&refcnt));

uint64_t offset = *(const uint64_t *)za.za_name;
uint64_t offset = *(const uint64_t *)za->za_name;

snprintf(dva, sizeof (dva), "%" PRIu64 ":%llx", vdevid,
(u_longlong_t)offset);
printf("%-16s %-10llu\n", dva, (u_longlong_t)refcnt);
}
zap_cursor_fini(&zc);
zap_attribute_free(za);
}
}

Expand Down Expand Up @@ -2953,28 +2961,30 @@ static void
dump_bookmarks(objset_t *os, int verbosity)
{
zap_cursor_t zc;
zap_attribute_t attr;
zap_attribute_t *attrp;
dsl_dataset_t *ds = dmu_objset_ds(os);
dsl_pool_t *dp = spa_get_dsl(os->os_spa);
objset_t *mos = os->os_spa->spa_meta_objset;
if (verbosity < 4)
return;
attrp = zap_attribute_alloc();
dsl_pool_config_enter(dp, FTAG);

for (zap_cursor_init(&zc, mos, ds->ds_bookmarks_obj);
zap_cursor_retrieve(&zc, &attr) == 0;
zap_cursor_retrieve(&zc, attrp) == 0;
zap_cursor_advance(&zc)) {
char osname[ZFS_MAX_DATASET_NAME_LEN];
char buf[ZFS_MAX_DATASET_NAME_LEN];
int len;
dmu_objset_name(os, osname);
len = snprintf(buf, sizeof (buf), "%s#%s", osname,
attr.za_name);
attrp->za_name);
VERIFY3S(len, <, ZFS_MAX_DATASET_NAME_LEN);
(void) dump_bookmark(dp, buf, verbosity >= 5, verbosity >= 6);
}
zap_cursor_fini(&zc);
dsl_pool_config_exit(dp, FTAG);
zap_attribute_free(attrp);
}

static void
Expand Down Expand Up @@ -6857,18 +6867,19 @@ iterate_deleted_livelists(spa_t *spa, ll_iter_t func, void *arg)
ASSERT0(err);

zap_cursor_t zc;
zap_attribute_t attr;
zap_attribute_t *attrp = zap_attribute_alloc();
dsl_deadlist_t ll;
/* NULL out os prior to dsl_deadlist_open in case it's garbage */
ll.dl_os = NULL;
for (zap_cursor_init(&zc, mos, zap_obj);
zap_cursor_retrieve(&zc, &attr) == 0;
zap_cursor_retrieve(&zc, attrp) == 0;
(void) zap_cursor_advance(&zc)) {
dsl_deadlist_open(&ll, mos, attr.za_first_integer);
dsl_deadlist_open(&ll, mos, attrp->za_first_integer);
func(&ll, arg);
dsl_deadlist_close(&ll);
}
zap_cursor_fini(&zc);
zap_attribute_free(attrp);
}

static int
Expand Down Expand Up @@ -8082,13 +8093,14 @@ static void
errorlog_count_refd(objset_t *mos, uint64_t errlog)
{
zap_cursor_t zc;
zap_attribute_t za;
zap_attribute_t *za = zap_attribute_alloc();
for (zap_cursor_init(&zc, mos, errlog);
zap_cursor_retrieve(&zc, &za) == 0;
zap_cursor_retrieve(&zc, za) == 0;
zap_cursor_advance(&zc)) {
mos_obj_refd(za.za_first_integer);
mos_obj_refd(za->za_first_integer);
}
zap_cursor_fini(&zc);
zap_attribute_free(za);
}

static int
Expand Down
17 changes: 9 additions & 8 deletions cmd/zhack.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,26 +203,27 @@ static void
dump_obj(objset_t *os, uint64_t obj, const char *name)
{
zap_cursor_t zc;
zap_attribute_t za;
zap_attribute_t *za = zap_attribute_alloc();

(void) printf("%s_obj:\n", name);

for (zap_cursor_init(&zc, os, obj);
zap_cursor_retrieve(&zc, &za) == 0;
zap_cursor_retrieve(&zc, za) == 0;
zap_cursor_advance(&zc)) {
if (za.za_integer_length == 8) {
ASSERT(za.za_num_integers == 1);
if (za->za_integer_length == 8) {
ASSERT(za->za_num_integers == 1);
(void) printf("\t%s = %llu\n",
za.za_name, (u_longlong_t)za.za_first_integer);
za->za_name, (u_longlong_t)za->za_first_integer);
} else {
ASSERT(za.za_integer_length == 1);
ASSERT(za->za_integer_length == 1);
char val[1024];
VERIFY(zap_lookup(os, obj, za.za_name,
VERIFY(zap_lookup(os, obj, za->za_name,
1, sizeof (val), val) == 0);
(void) printf("\t%s = %s\n", za.za_name, val);
(void) printf("\t%s = %s\n", za->za_name, val);
}
}
zap_cursor_fini(&zc);
zap_attribute_free(za);
}

static void
Expand Down
12 changes: 11 additions & 1 deletion include/sys/zap.h
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,19 @@ typedef struct {
boolean_t za_normalization_conflict;
uint64_t za_num_integers;
uint64_t za_first_integer; /* no sign extension for <8byte ints */
char za_name[ZAP_MAXNAMELEN];
uint32_t za_name_len;
char za_name[];
} zap_attribute_t;

void zap_init(void);
void zap_fini(void);

/*
* Alloc and free zap_attribute_t.
*/
zap_attribute_t *zap_attribute_alloc(void);
void zap_attribute_free(zap_attribute_t *attrp);

/*
* The interface for listing all the attributes of a zapobj can be
* thought of as cursor moving down a list of the attributes one by
Expand Down
3 changes: 2 additions & 1 deletion include/sys/zap_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ typedef struct zap_name {
uint64_t zn_hash;
matchtype_t zn_matchtype;
int zn_normflags;
char zn_normbuf[ZAP_MAXNAMELEN];
int zn_normbuf_len;
char zn_normbuf[];
} zap_name_t;

#define zap_f zap_u.zap_fat
Expand Down
Loading

0 comments on commit 3cf2bfa

Please sign in to comment.