[PATCH 16/23] Add ngnfs_dir_unlink(), ngnfs_dir_rmdir(), and debugfs commands
Valerie Aurora
val at versity.com
Fri Apr 4 11:45:32 PDT 2025
Allow removal of links to files and directories. Also simplify calling
convention for update_dir() and add ngnfs_inode_update() to change
link counts for the target of a directory entry.
Signed-off-by: Valerie Aurora <val at versity.com>
---
cli/debugfs.c | 35 +++++++++++
shared/dir.c | 154 +++++++++++++++++++++++++++++++++++++++++++++----
shared/dir.h | 4 ++
shared/inode.c | 20 +++++++
shared/inode.h | 1 +
5 files changed, 203 insertions(+), 11 deletions(-)
diff --git a/cli/debugfs.c b/cli/debugfs.c
index 4a13795..96f9040 100644
--- a/cli/debugfs.c
+++ b/cli/debugfs.c
@@ -222,6 +222,23 @@ static void cmd_readdir(struct debugfs_context *ctx, int argc, char **argv)
free(buf);
}
+static void cmd_rmdir(struct debugfs_context *ctx, int argc, char **argv)
+{
+ char *name;
+ int ret;
+
+ if (argc != 2) {
+ printf("usage: rmdir <pathname>\n");
+ return;
+ }
+
+ name = argv[1];
+
+ ret = ngnfs_dir_rmdir(ctx->nfi, &ctx->cwd_ig, name, strlen(name));
+ if (ret < 0)
+ print_err("rmdir", ret);
+}
+
static void cmd_stat(struct debugfs_context *ctx, int argc, char **argv)
{
struct ngnfs_inode_ino_gen ig = ctx->cwd_ig;
@@ -287,6 +304,22 @@ static void cmd_sync(struct debugfs_context *ctx, int argc, char **argv)
print_err("sync", ret);
}
+static void cmd_unlink(struct debugfs_context *ctx, int argc, char **argv)
+{
+ char *name;
+ int ret;
+
+ if (argc != 2) {
+ printf("usage: unlink <pathname>\n");
+ return;
+ }
+
+ name = argv[1];
+
+ ret = ngnfs_dir_unlink(ctx->nfi, &ctx->cwd_ig, name, strlen(name));
+ if (ret < 0)
+ print_err("unlink", ret);
+}
static struct command {
char *name;
@@ -299,8 +332,10 @@ static struct command {
{ "mkfs", cmd_mkfs, },
{ "quit", cmd_quit, },
{ "readdir", cmd_readdir, },
+ { "rmdir", cmd_rmdir, },
{ "stat", cmd_stat, },
{ "sync", cmd_sync, },
+ { "unlink", cmd_unlink, },
};
static int compar_cmd_names(const void *A, const void *B)
diff --git a/shared/dir.c b/shared/dir.c
index 0dc69cf..2fc2db9 100644
--- a/shared/dir.c
+++ b/shared/dir.c
@@ -175,27 +175,26 @@ static inline int check_ifmt(struct ngnfs_inode *ninode, u32 ifmt, int err)
}
/*
- * Update a directory's inode to reflect creation. We can return errors
- * if the create should fail.
+ * Update a parent directory's inode to reflect creation or deletion of
+ * an entry. We can return errors if the create should fail.
*/
static int update_dir(struct ngnfs_txn_block *tblk, struct ngnfs_inode *dir,
- struct dirent_args *da, int posneg)
+ struct dirent_args *da, s32 nlink_delta)
{
- s32 delta;
+ s32 dent_bytes;
+ s32 posneg;
int ret;
if (da->dent.pers_dtype == NGNFS_DT_DIR) {
- delta = posneg * 1;
- if ((le32_to_cpu(dir->nlink) + delta >= NGNFS_LINK_MAX)) {
- ret = -EMLINK;
+ ret = ngnfs_inode_update(tblk, dir, nlink_delta);
+ if (ret < 0)
goto out;
- }
- ngnfs_tblk_assign(tblk, dir->nlink, cpu_to_le32(le32_to_cpu(dir->nlink) + delta));
}
+ posneg = nlink_delta >= 0 ? 1 : -1;
/* dir i_size includes null termed names */
- delta = posneg * ((s32)da->dent.name_len + 1);
- ngnfs_tblk_assign(tblk, dir->size, cpu_to_le64(le64_to_cpu(dir->size) + delta));
+ dent_bytes = posneg * ((s32)da->dent.name_len + 1);
+ ngnfs_tblk_assign(tblk, dir->size, cpu_to_le64(le64_to_cpu(dir->size) + dent_bytes));
ret = 0;
out:
return ret;
@@ -595,3 +594,136 @@ int ngnfs_dir_lookup(struct ngnfs_fs_info *nfi, struct ngnfs_inode_ino_gen *dir_
out:
return ret;
}
+
+static int remove_dirent_wr(struct ngnfs_btree_key *key, void *val, size_t size, void *arg,
+ struct ngnfs_btree_op *op)
+{
+ struct ngnfs_dirent *dent = val;
+ struct dirent_args *da = arg;
+
+ if (!dent)
+ return -ENOENT;
+
+ if (!names_equal(dent->name, dent->name_len, da->dent.name, da->dent.name_len))
+ return NGNFS_BTREE_ITER_CONTINUE;
+
+ /* ino says we found it, dent tells us how to update parent dir */
+ da->ig.ino = le64_to_cpu(dent->ig.ino);
+ da->ig.gen = le64_to_cpu(dent->ig.gen);
+ memcpy(&da->dent, dent, size);
+
+ op->delete = 1;
+
+ return 0;
+}
+
+static int remove_dirent(struct ngnfs_fs_info *nfi, struct ngnfs_transaction *txn,
+ struct ngnfs_inode_txn_ref *dir, struct dirent_args *da)
+{
+ struct ngnfs_btree_key key;
+ struct ngnfs_btree_key last;
+ int ret;
+
+ init_dirent_key(&key, da->hash);
+ init_dirent_key(&last, da->hash | NGNFS_DIRENT_COLL_BIT);
+
+ ret = ngnfs_btree_write_iter(nfi, txn, dir->tblk, &dir->ninode->dirents, &key, &last,
+ remove_dirent_wr, da);
+ if (ret < 0)
+ goto out;
+
+ if (da->ig.ino == 0)
+ ret = -ENOENT;
+out:
+ return ret;
+}
+
+static int check_empty_dir(struct ngnfs_fs_info *nfi, struct ngnfs_transaction *txn,
+ struct ngnfs_inode_ino_gen *ig)
+{
+ struct ngnfs_inode_txn_ref new;
+ int ret;
+
+ ret = ngnfs_inode_get(nfi, txn, NBF_READ, ig, &new);
+ if (ret < 0)
+ return ret;
+
+ if (le32_to_cpu(new.ninode->nlink) != 2)
+ return -ENOTEMPTY;
+
+ return 0;
+}
+
+static int check_remove_dirent(struct ngnfs_fs_info *nfi, struct ngnfs_transaction *txn,
+ struct ngnfs_inode_txn_ref *dir, struct dirent_args *da,
+ int want_non_dir)
+{
+ if (want_non_dir) {
+ if (da->dent.pers_dtype == NGNFS_DT_DIR)
+ return -EISDIR;
+ else
+ return 0;
+ }
+
+ if (da->dent.pers_dtype != NGNFS_DT_DIR)
+ return -ENOTDIR;
+
+ return check_empty_dir(nfi, txn, &da->ig);
+}
+
+static int do_unlink(struct ngnfs_fs_info *nfi, struct ngnfs_inode_ino_gen *dir_ig, char *name,
+ size_t name_len, int flags)
+{
+ struct {
+ struct ngnfs_transaction txn;
+ struct ngnfs_inode_txn_ref dir;
+ struct ngnfs_inode_txn_ref target;
+ struct dirent_args da;
+ u64 ino;
+ u64 nsec;
+ } *op;
+ int ret;
+
+ if (name_len > NGNFS_NAME_MAX) {
+ ret = -ENAMETOOLONG;
+ goto out;
+ }
+
+ op = kmalloc(sizeof(*op), GFP_NOFS);
+ if (!op) {
+ ret = -ENOMEM;
+ goto out;
+ }
+
+ ngnfs_txn_init(&op->txn);
+ op->nsec = ktime_to_ns(ktime_get_real());
+ init_dirent_args(&op->da, name, name_len, 0);
+
+ do {
+ ret = ngnfs_inode_get(nfi, &op->txn, NBF_WRITE, dir_ig, &op->dir) ?:
+ check_ifmt(op->dir.ninode, S_IFDIR, -ENOTDIR) ?:
+ remove_dirent(nfi, &op->txn, &op->dir, &op->da) ?:
+ ngnfs_inode_get(nfi, &op->txn, NBF_WRITE, &op->da.ig, &op->target) ?:
+ check_remove_dirent(nfi, &op->txn, &op->dir, &op->da, flags) ?:
+ ngnfs_inode_update(op->target.tblk, op->target.ninode, -1) ?:
+ update_dir(op->dir.tblk, op->dir.ninode, &op->da, -1);
+
+ } while (ngnfs_txn_retry(nfi, &op->txn, &ret));
+
+ ngnfs_txn_teardown(nfi, &op->txn);
+ kfree(op);
+out:
+ return ret;
+}
+
+int ngnfs_dir_unlink(struct ngnfs_fs_info *nfi, struct ngnfs_inode_ino_gen *dir, char *name,
+ size_t name_len)
+{
+ return do_unlink(nfi, dir, name, name_len, 1);
+}
+
+int ngnfs_dir_rmdir(struct ngnfs_fs_info *nfi, struct ngnfs_inode_ino_gen *dir, char *name,
+ size_t name_len)
+{
+ return do_unlink(nfi, dir, name, name_len, 0);
+}
diff --git a/shared/dir.h b/shared/dir.h
index b3ef067..dd83556 100644
--- a/shared/dir.h
+++ b/shared/dir.h
@@ -10,6 +10,10 @@ int ngnfs_dir_create(struct ngnfs_fs_info *nfi, struct ngnfs_inode_ino_gen *dir,
char *name, size_t name_len);
int ngnfs_dir_mkdir(struct ngnfs_fs_info *nfi, struct ngnfs_inode_ino_gen *dir, umode_t mode,
char *name, size_t name_len);
+int ngnfs_dir_unlink(struct ngnfs_fs_info *nfi, struct ngnfs_inode_ino_gen *dir, char *name,
+ size_t name_len);
+int ngnfs_dir_rmdir(struct ngnfs_fs_info *nfi, struct ngnfs_inode_ino_gen *dir, char *name,
+ size_t name_len);
/*
* Readdir fills the buffer with entries. The start of the buffer must
diff --git a/shared/inode.c b/shared/inode.c
index 3698920..5a4d14b 100644
--- a/shared/inode.c
+++ b/shared/inode.c
@@ -73,6 +73,8 @@ int ngnfs_inode_init(struct ngnfs_inode_txn_ref *itref, struct ngnfs_inode_ino_g
int ngnfs_inode_get(struct ngnfs_fs_info *nfi, struct ngnfs_transaction *txn, nbf_t nbf,
struct ngnfs_inode_ino_gen *ig, struct ngnfs_inode_txn_ref *itref)
{
+ BUG_ON(ig->ino == 0);
+
return ngnfs_txn_get_block(nfi, txn, ig->ino, nbf, &itref->tblk, (void **)&itref->ninode);
}
@@ -122,3 +124,21 @@ int ngnfs_inode_read_copy(struct ngnfs_fs_info *nfi, struct ngnfs_inode_ino_gen
out:
return ret;
}
+
+/*
+ * Update an inode to reflect the addition or removal of one or more links to it.
+ */
+int ngnfs_inode_update(struct ngnfs_txn_block *tblk, struct ngnfs_inode *inode, s32 delta)
+{
+ s32 nlink = le32_to_cpu(inode->nlink);
+
+ if ((delta > 0) && (nlink > NGNFS_LINK_MAX - delta))
+ return -EMLINK;
+
+ /* nlink < 0 is a bug */
+ BUG_ON((delta < 0) && (nlink + delta < 0));
+
+ ngnfs_tblk_assign(tblk, inode->nlink, cpu_to_le32(nlink + delta));
+
+ return 0;
+}
diff --git a/shared/inode.h b/shared/inode.h
index 92b5114..0d063ea 100644
--- a/shared/inode.h
+++ b/shared/inode.h
@@ -27,5 +27,6 @@ int ngnfs_inode_alloc(struct ngnfs_fs_info *nfi, struct ngnfs_transaction *txn,
struct ngnfs_inode_ino_gen *ig, struct ngnfs_inode_txn_ref *itref);
int ngnfs_inode_read_copy(struct ngnfs_fs_info *nfi, struct ngnfs_inode_ino_gen *ig,
void *buf, int size);
+int ngnfs_inode_update(struct ngnfs_txn_block *tblk, struct ngnfs_inode *inode, s32 delta);
#endif
--
2.48.1
More information about the ngnfs-devel
mailing list