Skip to content

Commit

Permalink
feat(dentry): refactor dentry iterate code
Browse files Browse the repository at this point in the history
Signed-off-by: sundengyu <[email protected]>
  • Loading branch information
sundengyu committed Oct 28, 2024
1 parent 8fec50a commit 448601b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 51 deletions.
6 changes: 4 additions & 2 deletions include/libuzfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,10 @@ extern int libuzfs_dentry_delete(libuzfs_inode_handle_t *ihp,
extern int libuzfs_dentry_lookup(libuzfs_inode_handle_t *ihp,
const char *name, uint64_t *value);

extern int libuzfs_dentry_iterate(libuzfs_inode_handle_t *ihp,
uint64_t whence, uint32_t size, char *buf, uint32_t *num);
typedef int (*dir_emit_func_t)(void *arg, uint64_t whence,
const char *name, uint64_t value);
extern int libuzfs_dentry_iterate(libuzfs_inode_handle_t *dihp,
uint64_t whence, void *arg, dir_emit_func_t dir_emit);

extern int libuzfs_fs_create(const char *fsname);
extern void libuzfs_fs_destroy(const char *fsname);
Expand Down
45 changes: 2 additions & 43 deletions lib/libuzfs/libuzfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,6 @@ libuzfs_inode_handle_rele(libuzfs_inode_handle_t *ihp)
mutex_exit(mp);
}

typedef struct dir_emit_ctx {
char *buf;
char *cur;
uint32_t size;
} dir_emit_ctx_t;

static void
dump_debug_buffer(void)
{
Expand Down Expand Up @@ -2209,44 +2203,14 @@ libuzfs_dentry_lookup(libuzfs_inode_handle_t *dihp,
return (libuzfs_zap_lookup(dihp->dhp, dihp->ino, name, 8, 1, value));
}

static boolean_t
dir_emit(dir_emit_ctx_t *ctx, uint64_t whence, uint64_t value, char *name,
uint32_t name_len)
{
int size = offsetof(struct uzfs_dentry, name) + name_len + 1;

// ensure dentry is aligned to 8 bytes to make Rust happy
size = roundup(size, sizeof (uint64_t));
if (ctx->cur + size > ctx->buf + ctx->size) {
return (B_TRUE);
}

struct uzfs_dentry *dentry = (struct uzfs_dentry *)ctx->cur;
dentry->size = size;
dentry->whence = whence;
dentry->value = value;
memcpy(dentry->name, name, name_len + 1);

ctx->cur += size;

return (B_FALSE);
}

int
libuzfs_dentry_iterate(libuzfs_inode_handle_t *dihp,
uint64_t whence, uint32_t size, char *buf, uint32_t *num)
uint64_t whence, void *arg, dir_emit_func_t dir_emit)
{
int error = 0;
zap_cursor_t zc;
zap_attribute_t zap;
boolean_t done = B_FALSE;
dir_emit_ctx_t ctx;

*num = 0;
ctx.buf = buf;
ctx.cur = buf;
ctx.size = size;
memset(ctx.buf, 0, size);

libuzfs_dataset_handle_t *dhp = dihp->dhp;
uint64_t dino = dihp->ino;
Expand Down Expand Up @@ -2275,17 +2239,12 @@ libuzfs_dentry_iterate(libuzfs_inode_handle_t *dihp,
zap_cursor_advance(&zc);
whence = zap_cursor_serialize(&zc);

done = dir_emit(&ctx, whence, zap.za_first_integer, zap.za_name,
strlen(zap.za_name));
done = dir_emit(arg, whence, zap.za_name, zap.za_first_integer);
if (done)
break;
(*num)++;
}

zap_cursor_fini(&zc);
if (error == ENOENT) {
error = 0;
}
return (error);
}

Expand Down
7 changes: 1 addition & 6 deletions lib/libzpool/kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -294,13 +294,9 @@ kstat_seq_show_named(struct seq_file *f, kstat_named_t *knp)

switch (knp->data_type) {
case KSTAT_DATA_CHAR:
knp->value.c[15] = '\0'; /* NULL terminate */
knp->value.c[15] = '\0';
seq_printf(f, "%-16s", knp->value.c);
break;
/*
* NOTE - We need to be more careful able what tokens are
* used for each arch, for now this is correct for x86_64.
*/
case KSTAT_DATA_INT32:
seq_printf(f, "%d", knp->value.i32);
break;
Expand Down Expand Up @@ -344,7 +340,6 @@ kstat_seq_show_intr(struct seq_file *f, kstat_intr_t *kip)
static int
kstat_seq_show_io(struct seq_file *f, kstat_io_t *kip)
{
/* though wlentime & friends are signed, they will never be negative */
seq_printf(f,
"%-8llu %-8llu %-8u %-8u %-8llu %-8llu "
"%-8llu %-8llu %-8llu %-8llu %-8u %-8u\n",
Expand Down

0 comments on commit 448601b

Please sign in to comment.