Skip to content

Commit

Permalink
add mkdirat & unlinkat & renameat
Browse files Browse the repository at this point in the history
  • Loading branch information
KoyamaSohei committed Oct 2, 2024
1 parent f5a170b commit e9faea3
Show file tree
Hide file tree
Showing 6 changed files with 273 additions and 80 deletions.
47 changes: 33 additions & 14 deletions finchfsd/fs_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,15 @@ ucs_status_t
fs_rpc_mkdir_recv(void *arg, const void *header, size_t header_length,
void *data, size_t length, const ucp_am_recv_param_t *param)
{
char *path = (char *)data;
mode_t mode = *(mode_t *)UCS_PTR_BYTE_OFFSET(data, strlen(path) + 1);
uint64_t base;
mode_t mode;
char *path;
char *p = (char *)data;
base = *(uint64_t *)p;
p += sizeof(base);
mode = *(mode_t *)p;
p += sizeof(mode);
path = (char *)p;

log_debug("fs_rpc_mkdir_recv() called path=%s", path);

Expand All @@ -262,7 +269,7 @@ fs_rpc_mkdir_recv(void *arg, const void *header, size_t header_length,
user_data->iov[0].length = sizeof(int);

char dirname[128];
entry_t *parent = get_parent_and_filename(0, dirname, path, &ctx);
entry_t *parent = get_parent_and_filename(base, dirname, path, &ctx);

if (parent == NULL) {
log_debug("fs_rpc_mkdir_recv() parent path=%s does not exist",
Expand Down Expand Up @@ -393,22 +400,25 @@ fs_rpc_inode_unlink_recv(void *arg, const void *header, size_t header_length,
void *data, size_t length,
const ucp_am_recv_param_t *param)
{
char *path = (char *)data;
uint64_t base;
char *path;
char *p = (char *)data;
base = *(uint64_t *)p;
p += sizeof(base);
path = (char *)p;

log_debug("fs_rpc_inode_unlink_recv() called path=%s", path);

iov_req_t *user_data =
malloc(sizeof(iov_req_t) + sizeof(ucp_dt_iov_t) * 2);
user_data->header = malloc(header_length);
memcpy(user_data->header, header, header_length);
user_data->n = 2;
user_data->n = 1;
user_data->iov[0].buffer = malloc(sizeof(int));
user_data->iov[0].length = sizeof(int);
user_data->iov[1].buffer = malloc(sizeof(uint64_t));
user_data->iov[1].length = sizeof(uint64_t);

char name[128];
entry_t *parent = get_parent_and_filename(0, name, path, &ctx);
entry_t *parent = get_parent_and_filename(base, name, path, &ctx);
if (parent == NULL) {
log_debug("fs_rpc_inode_unlink_recv() path=%s does not exist",
path);
Expand All @@ -424,7 +434,6 @@ fs_rpc_inode_unlink_recv(void *arg, const void *header, size_t header_length,
path);
*(int *)(user_data->iov[0].buffer) = FINCH_ENOENT;
} else {
*(uint64_t *)user_data->iov[1].buffer = ent->i_ino;
*(int *)(user_data->iov[0].buffer) = FINCH_OK;
free_meta_tree(ent);
RB_REMOVE(entrytree, &parent->entries, ent);
Expand All @@ -439,7 +448,7 @@ fs_rpc_inode_unlink_recv(void *arg, const void *header, size_t header_length,
}

ucs_status_t status;
status = post_iov_req(param->reply_ep, RPC_INODE_REP, user_data,
status = post_iov_req(param->reply_ep, RPC_RET_REP, user_data,
header_length);
return (status);
}
Expand Down Expand Up @@ -863,8 +872,18 @@ ucs_status_t
fs_rpc_rename_recv(void *arg, const void *header, size_t header_length,
void *data, size_t length, const ucp_am_recv_param_t *param)
{
char *opath = (char *)data;
char *npath = (char *)UCS_PTR_BYTE_OFFSET(data, strlen(opath) + 1);
uint64_t oldbase;
char *opath;
uint64_t newbase;
char *npath;
char *p = (char *)data;
oldbase = *(uint64_t *)p;
p += sizeof(uint64_t);
opath = (char *)p;
p += strlen(opath) + 1;
newbase = *(uint64_t *)p;
p += sizeof(uint64_t);
npath = (char *)p;

log_debug("fs_rpc_dir_move_recv() called opath=%s npath=%s", opath,
npath);
Expand All @@ -878,8 +897,8 @@ fs_rpc_rename_recv(void *arg, const void *header, size_t header_length,

char oname[128];
char nname[128];
entry_t *oparent = get_parent_and_filename(0, oname, opath, &ctx);
entry_t *nparent = get_parent_and_filename(0, nname, npath, &ctx);
entry_t *oparent = get_parent_and_filename(oldbase, oname, opath, &ctx);
entry_t *nparent = get_parent_and_filename(newbase, nname, npath, &ctx);

if (oparent == NULL) {
log_debug("fs_rpc_dir_move_recv() opath=%s does not exist",
Expand Down
92 changes: 86 additions & 6 deletions lib/finchfs.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#define _ATFILE_SOURCE
#include <stdint.h>
#include <ucp/api/ucp.h>
#include <fcntl.h>
Expand Down Expand Up @@ -636,9 +637,8 @@ finchfs_unlink(const char *path)
{
log_debug("finchfs_unlink() called path=%s", path);
int ret;
uint64_t i_ino;
char *p = canonical_path(path);
ret = fs_rpc_inode_unlink(p, &i_ino);
ret = fs_rpc_inode_unlink(NULL, p);
free(p);
return (ret);
}
Expand All @@ -650,7 +650,7 @@ finchfs_mkdir(const char *path, mode_t mode)
int ret;
char *p = canonical_path(path);
mode |= S_IFDIR;
ret = fs_rpc_mkdir(p, mode);
ret = fs_rpc_mkdir(NULL, p, mode);
free(p);
return (ret);
}
Expand All @@ -661,7 +661,7 @@ finchfs_rmdir(const char *path)
log_debug("finchfs_rmdir() called path=%s", path);
int ret;
char *p = canonical_path(path);
ret = fs_rpc_inode_unlink_all(p);
ret = fs_rpc_inode_unlink_all(NULL, p);
free(p);
return (ret);
}
Expand Down Expand Up @@ -741,12 +741,12 @@ finchfs_rename(const char *oldpath, const char *newpath)
return (-1);
}
if (S_ISDIR(st.mode)) {
ret = fs_rpc_dir_rename(oldp, newp);
ret = fs_rpc_dir_rename(NULL, oldp, NULL, newp);
free(oldp);
free(newp);
return (ret);
} else {
ret = fs_rpc_file_rename(oldp, newp);
ret = fs_rpc_file_rename(NULL, oldp, NULL, newp);
free(oldp);
free(newp);
return (ret);
Expand Down Expand Up @@ -912,6 +912,86 @@ finchfs_fstatat(int dirfd, const char *pathname, struct stat *st, int flags)
return (0);
}

int
finchfs_mkdirat(int dirfd, const char *pathname, mode_t mode)
{
log_debug("finchfs_mkdirat() called dirfd=%d path=%s", dirfd, pathname);
if (dirfd < 0 || dirfd >= fd_table_size ||
fd_table[dirfd].path == NULL) {
errno = EBADF;
return (-1);
}
int ret;
char *p = canonical_path(pathname);
mode |= S_IFDIR;
ret = fs_rpc_mkdir(fd_table[dirfd].eid, p, mode);
free(p);
return (ret);
}

int
finchfs_unlinkat(int dirfd, const char *pathname, int flags)
{
log_debug("finchfs_unlinkat() called dirfd=%d path=%s", dirfd,
pathname);
if (dirfd < 0 || dirfd >= fd_table_size ||
fd_table[dirfd].path == NULL) {
errno = EBADF;
return (-1);
}
int ret;
char *p = canonical_path(pathname);
if (flags & AT_REMOVEDIR) {
ret = fs_rpc_inode_unlink_all(fd_table[dirfd].eid, p);
} else {
ret = fs_rpc_inode_unlink(fd_table[dirfd].eid, p);
}
free(p);
return (ret);
}

int
finchfs_renameat(int olddirfd, const char *oldpath, int newdirfd,
const char *newpath)
{
log_debug("finchfs_renameat() called olddirfd=%d oldpath=%s "
"newdirfd=%d newpath=%s",
olddirfd, oldpath, newdirfd, newpath);
if (olddirfd < 0 || olddirfd >= fd_table_size ||
fd_table[olddirfd].path == NULL) {
errno = EBADF;
return (-1);
}
if (newdirfd < 0 || newdirfd >= fd_table_size ||
fd_table[newdirfd].path == NULL) {
errno = EBADF;
return (-1);
}
int ret;
char *oldp = canonical_path(oldpath);
char *newp = canonical_path(newpath);
fs_stat_t st;
ret = fs_rpc_inode_stat(fd_table[olddirfd].eid, oldp, &st, 0);
if (ret) {
free(oldp);
free(newp);
return (-1);
}
if (S_ISDIR(st.mode)) {
ret = fs_rpc_dir_rename(fd_table[olddirfd].eid, oldp,
fd_table[newdirfd].eid, newp);
free(oldp);
free(newp);
return (ret);
} else {
ret = fs_rpc_file_rename(fd_table[olddirfd].eid, oldp,
fd_table[newdirfd].eid, newp);
free(oldp);
free(newp);
return (ret);
}
}

ssize_t
finchfs_getdents(int fd, void *dirp, size_t count)
{
Expand Down
4 changes: 4 additions & 0 deletions lib/finchfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ int finchfs_createat_chunk_size(int dirfd, const char *pathname, int flags,
int finchfs_openat(int dirfd, const char *pathname, int flags);
int finchfs_fstatat(int dirfd, const char *pathname, struct stat *buf,
int flags);
int finchfs_mkdirat(int dirfd, const char *pathname, mode_t mode);
int finchfs_unlinkat(int dirfd, const char *pathname, int flags);
int finchfs_renameat(int olddirfd, const char *oldpath, int newdirfd,
const char *newpath);
ssize_t finchfs_getdents(int fd, void *dirp, size_t count);
void *finchfs_mmap(void *addr, size_t length, int prot, int flags, int fd,
off_t offset);
Expand Down
Loading

0 comments on commit e9faea3

Please sign in to comment.