Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ngnfs-tests #9

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ cscope.*
shared/generated-trace-inlines.h
cli/ngnfs-cli
devd/ngnfs-devd
mapd/ngnfs-mapd
lib/libngnfs.a
17 changes: 14 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,29 @@ LDFLAGS := -Wl,--gc-sections -lurcu-common -lurcu -lurcu-cds
LDFLAGS += -rdynamic

# build all c files in source directories
DIR := cli devd shared shared/lk
DIR := mapd cli devd shared shared/lk
SRC := $(foreach d,$(DIR),$(wildcard $(d)/*.c))
OBJ := $(patsubst %.c,%.o,$(SRC))
DEP := $(foreach d,$(DIR),$(wildcard $(d)/*.d))

# source with main() is linked as a binary
BIN := $(patsubst %.c,%,$(shell grep -l "^int main" $(SRC)))

# make a static library out of everything in shared
LIB := lib/libngnfs.a
LIB_DIR := shared shared/lk
LIB_SRC := $(foreach d,$(LIB_DIR),$(wildcard $(d)/*.c))
LIB_OBJ := $(patsubst %.c,%.o,$(LIB_SRC))
LIB_DEP := $(foreach d,$(LIB_DIR),$(wildcard $(d)/*.d))

# binary names have ngnfs- prefixed
#binname = $(dir $1)ngnfs-$(notdir $1)

# check exposed format structs by building compiled headers with -Wpadded
GCH := $(patsubst %,%.gch,$(wildcard shared/format-*.h))

.PHONY: all
all: shared/generated-trace-inlines.h $(GCH) $(BIN)
all: shared/generated-trace-inlines.h $(GCH) $(BIN) $(LIB)

ifneq ($(DEP),)
-include $(DEP)
Expand All @@ -49,6 +56,10 @@ $(BIN): %: %.o $$(filter $$(dir %)$$(PERCENT),$$(OBJ)) \
gcc $(CFLAGS) -MD -MP -MF $*.d -c $< -o $*.o
./scripts/sparse.sh -Wbitwise -D__CHECKER__ $(CFLAGS) $<

$(LIB): $(LIB_OBJ) Makefile
ar rcs $@ $(LIB_OBJ)
ranlib $@

$(GCH): %.h.gch: %.h Makefile
gcc $(CFLAGS) -Wpadded -c $< -o $@

Expand All @@ -58,7 +69,7 @@ shared/generated-trace-inlines.h: scripts/generate-trace-events.awk \

.PHONY: clean
clean:
@rm -f $(BIN) $(OBJ) $(DEP) $(GCH) \
@rm -f $(BIN) $(LIB) $(OBJ) $(DEP) $(GCH) \
$(foreach d,$(DIR),$(wildcard $(d)/*.[is])) \
shared/generated-trace-inlines.h \
.sparse.gcc-defines.h .sparse.output
Expand Down
93 changes: 79 additions & 14 deletions cli/debugfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,31 @@
#include <inttypes.h>

#include "shared/lk/byteorder.h"
#include "shared/lk/err.h"
#include "shared/lk/kernel.h"
#include "shared/lk/timekeeping.h"
#include "shared/lk/types.h"
#include "shared/lk/wait.h"

#include "shared/format-block.h"
#include "shared/log.h"
#include "shared/mount.h"
#include "shared/pfs.h"
#include "shared/shutdown.h"
#include "shared/thread.h"
#include "shared/txn.h"

#include "cli/cli.h"

struct debugfs_context {
struct ngnfs_fs_info *nfi;
struct wait_queue_head *waitq;
u64 cwd_ino;
};

#define LINE_SIZE (PATH_MAX * 5)
#define MAX_ARGC ((LINE_SIZE + 1) / 2)

static void cmd_mkfs(struct debugfs_context *ctx, int argc, char **argv)
static int cmd_mkfs(struct debugfs_context *ctx, int argc, char **argv)
{
struct ngnfs_transaction txn = INIT_NGNFS_TXN(txn);
int ret;
Expand All @@ -40,22 +42,27 @@ static void cmd_mkfs(struct debugfs_context *ctx, int argc, char **argv)
ngnfs_txn_destroy(ctx->nfi, &txn);
if (ret < 0) {
printf("mkfs error: "ENOF"\n", ENOA(-ret));
return;
return ret;
}

ret = ngnfs_block_sync(ctx->nfi);
if (ret < 0)
printf("final sync error: "ENOF"\n", ENOA(-ret));
return ret;
}

static void cmd_stat(struct debugfs_context *ctx, int argc, char **argv)
static int cmd_quit(struct debugfs_context *ctx, int argc, char **argv)
{
return 1;
}

static int cmd_stat(struct debugfs_context *ctx, int argc, char **argv)
{
struct ngnfs_transaction txn = INIT_NGNFS_TXN(txn);
struct ngnfs_inode ninode;
int ret;

ret = ngnfs_pfs_read_inode(ctx->nfi, &txn, NGNFS_ROOT_INO, &ninode, sizeof(ninode));
ngnfs_txn_destroy(ctx->nfi, &txn);

if (ret < 0) {
log("stat error: %d", ret);
Expand All @@ -80,13 +87,15 @@ static void cmd_stat(struct debugfs_context *ctx, int argc, char **argv)
le64_to_cpu(ninode.mtime_nsec),
le64_to_cpu(ninode.crtime_nsec));
}
return ret < 0 ? ret : 0;
}

static struct command {
char *name;
void (*func)(struct debugfs_context *ctx, int argc, char **argv);
int (*func)(struct debugfs_context *ctx, int argc, char **argv);
} commands[] = {
{ "mkfs", cmd_mkfs, },
{ "quit", cmd_quit, },
{ "stat", cmd_stat, },
};

Expand All @@ -106,7 +115,55 @@ static int compar_key_cmd_name(const void *key, const void *ele)
return strcmp(name, cmd->name);
}

static void parse_command(struct debugfs_context *ctx, char *buf, char **argv)
struct cmd_thread_args {
struct command *cmd;
struct debugfs_context *ctx;
int argc;
char **argv;
bool cmd_done;
int ret;
};

static void run_command(struct thread *thr, void *arg)
{
struct cmd_thread_args *cargs = arg;

cargs->ret = cargs->cmd->func(cargs->ctx, cargs->argc, cargs->argv);

cargs->cmd_done = true;
wake_up(cargs->ctx->waitq);
}

static int start_command_thread(struct debugfs_context *ctx, struct command *cmd, char **argv,
int argc)
{
struct cmd_thread_args cargs = { };
struct thread thr;
int ret;

cargs.cmd = cmd;
cargs.ctx = ctx;
cargs.argc = argc;
cargs.argv = argv;
cargs.cmd_done = false;

thread_init(&thr);
ret = thread_start(&thr, run_command, &cargs);
if (ret < 0)
return ret;

wait_event(ctx->waitq, cargs.cmd_done || ngnfs_should_shutdown(ctx->nfi));
ret = cargs.ret;

if (cargs.cmd_done != true) {
thread_stop_indicate(&thr);
thread_stop_wait(&thr);
ret = ctx->nfi->global_errno;
}
return ret;
}

static int parse_run_command(struct debugfs_context *ctx, char *buf, char **argv)
{
struct command *cmd;
char *delim = "\t \n\r";
Expand All @@ -122,22 +179,23 @@ static void parse_command(struct debugfs_context *ctx, char *buf, char **argv)

if (argc == 0) {
printf("no command");
return;
return -EINVAL;
}

cmd = bsearch(argv[0], commands, ARRAY_SIZE(commands), sizeof(commands[0]),
compar_key_cmd_name);
if (!cmd) {
printf("unknown command: '%s'\n", argv[0]);
return;
return -EINVAL;
}

cmd->func(ctx, argc, argv);
return start_command_thread(ctx, cmd, argv, argc);
}

struct debugfs_thread_args {
int argc;
char **argv;
struct wait_queue_head waitq;
int ret;
};

Expand All @@ -148,6 +206,7 @@ static void debugfs_thread(struct thread *thr, void *arg)
struct debugfs_context _ctx = {
.nfi = &nfi,
.cwd_ino = NGNFS_ROOT_INO,
.waitq = &dargs->waitq,
}, *ctx = &_ctx;
char **line_argv = NULL;
char *line = NULL;
Expand All @@ -173,15 +232,19 @@ static void debugfs_thread(struct thread *thr, void *arg)
if (!fgets(line, LINE_SIZE, stdin))
break;

parse_command(ctx, line, line_argv);
ret = parse_run_command(ctx, line, line_argv);
if (ret == 1) { /* quit requested */
ret = 0;
break;
}
}

dargs->ret = ret;
ngnfs_shutdown(&nfi, dargs->ret);
ngnfs_unmount(&nfi);
ret = 0;
out:
free(line);
free(line_argv);
dargs->ret = ret;
}

/*
Expand All @@ -200,6 +263,7 @@ static int debugfs_func(int argc, char **argv)
struct thread thr;
int ret;

init_waitqueue_head(&dargs.waitq);
thread_init(&thr);

ret = thread_prepare_main();
Expand All @@ -209,7 +273,8 @@ static int debugfs_func(int argc, char **argv)
ret = thread_start(&thr, debugfs_thread, &dargs) ?:
thread_sigwait();

thread_stop_indicate(&thr);
fclose(stdin);
wake_up(&dargs.waitq);
thread_stop_wait(&thr);

out:
Expand Down
Loading