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

[LibOS] Rename chroot_dentry_uri() into dentry_uri() #1817

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 8 additions & 8 deletions libos/include/libos_fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,14 @@ int dentry_abs_path(struct libos_dentry* dent, char** path, size_t* size);
*/
int dentry_rel_path(struct libos_dentry* dent, char** path, size_t* size);

/*
* Calculate the URI for a dentry. The URI scheme is determined by file type (`type` field). It
* needs to be passed separately (instead of using `dent->inode->type`) because the dentry might not
* have inode associated yet: we might be creating a new file, or looking up a file we don't know
* yet.
*/
int dentry_uri(struct libos_dentry* dent, mode_t type, char** out_uri);

ino_t dentry_ino(struct libos_dentry* dent);

/*!
Expand Down Expand Up @@ -969,13 +977,5 @@ int synthetic_setup_dentry(struct libos_dentry* dent, mode_t type, mode_t perm);

int fifo_setup_dentry(struct libos_dentry* dent, mode_t perm, int fd_read, int fd_write);

/*
* Calculate the URI for a dentry. The URI scheme is determined by file type (`type` field). It
* needs to be passed separately (instead of using `dent->inode->type`) because the dentry might not
* have inode associated yet: we might be creating a new file, or looking up a file we don't know
* yet.
*/
int chroot_dentry_uri(struct libos_dentry* dent, mode_t type, char** out_uri);

int chroot_readdir(struct libos_dentry* dent, readdir_callback_t callback, void* arg);
int chroot_unlink(struct libos_dentry* dent);
12 changes: 6 additions & 6 deletions libos/src/fs/chroot/encrypted.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ static int chroot_encrypted_lookup(struct libos_dentry* dent) {
* See also the comment in `fs.c:chroot_lookup` (but note that this case is simpler, because we
* don't allow "dev:" mounts).
*/
int ret = chroot_dentry_uri(dent, S_IFREG, &uri);
int ret = dentry_uri(dent, S_IFREG, &uri);
if (ret < 0)
goto out;

Expand Down Expand Up @@ -221,7 +221,7 @@ static int chroot_encrypted_creat(struct libos_handle* hdl, struct libos_dentry*
__UNUSED(flags);

char* uri;
int ret = chroot_dentry_uri(dent, S_IFREG, &uri);
int ret = dentry_uri(dent, S_IFREG, &uri);
if (ret < 0)
return ret;

Expand Down Expand Up @@ -266,7 +266,7 @@ static int chroot_encrypted_mkdir(struct libos_dentry* dent, mode_t perm) {

char* uri = NULL;

int ret = chroot_dentry_uri(dent, S_IFDIR, &uri);
int ret = dentry_uri(dent, S_IFDIR, &uri);
if (ret < 0)
goto out;

Expand Down Expand Up @@ -299,7 +299,7 @@ static int chroot_encrypted_unlink(struct libos_dentry* dent) {
assert(dent->inode);

char* uri;
int ret = chroot_dentry_uri(dent, dent->inode->type, &uri);
int ret = dentry_uri(dent, dent->inode->type, &uri);
if (ret < 0)
return ret;

Expand Down Expand Up @@ -331,7 +331,7 @@ static int chroot_encrypted_rename(struct libos_dentry* old, struct libos_dentry
int ret;
char* new_uri = NULL;

ret = chroot_dentry_uri(new, old->inode->type, &new_uri);
ret = dentry_uri(new, old->inode->type, &new_uri);
if (ret < 0)
return ret;

Expand Down Expand Up @@ -364,7 +364,7 @@ static int chroot_encrypted_chmod(struct libos_dentry* dent, mode_t perm) {

char* uri = NULL;

int ret = chroot_dentry_uri(dent, dent->inode->type, &uri);
int ret = dentry_uri(dent, dent->inode->type, &uri);
if (ret < 0)
goto out;

Expand Down
78 changes: 4 additions & 74 deletions libos/src/fs/chroot/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,76 +44,6 @@ static int chroot_mount(struct libos_mount_params* params, void** mount_data) {
return 0;
}

static const char* strip_prefix(const char* uri) {
const char* s = strchr(uri, ':');
assert(s);
return s + 1;
}

int chroot_dentry_uri(struct libos_dentry* dent, mode_t type, char** out_uri) {
assert(dent->mount);
assert(dent->mount->uri);

int ret;

const char* root = strip_prefix(dent->mount->uri);

const char* prefix;
size_t prefix_len;
switch (type) {
case S_IFREG:
prefix = URI_PREFIX_FILE;
prefix_len = static_strlen(URI_PREFIX_FILE);
break;
case S_IFDIR:
prefix = URI_PREFIX_DIR;
prefix_len = static_strlen(URI_PREFIX_DIR);
break;
case S_IFCHR:
prefix = URI_PREFIX_DEV;
prefix_len = static_strlen(URI_PREFIX_DEV);
break;
default:
BUG();
}

char* rel_path;
size_t rel_path_size;
ret = dentry_rel_path(dent, &rel_path, &rel_path_size);
if (ret < 0)
return ret;

/* Treat empty path as "." */
if (*root == '\0')
root = ".";

size_t root_len = strlen(root);

/* Allocate buffer for "<prefix:><root>/<rel_path>" (if `rel_path` is empty, we don't need the
* space for `/`, but overallocating 1 byte doesn't hurt us, and keeps the code simple) */
char* uri = malloc(prefix_len + root_len + 1 + rel_path_size);
if (!uri) {
ret = -ENOMEM;
goto out;
}
memcpy(uri, prefix, prefix_len);
memcpy(uri + prefix_len, root, root_len);
if (rel_path_size == 1) {
/* this is the mount root, the URI is "<prefix:><root>"*/
uri[prefix_len + root_len] = '\0';
} else {
/* this is not the mount root, the URI is "<prefix:><root>/<rel_path>" */
uri[prefix_len + root_len] = '/';
memcpy(uri + prefix_len + root_len + 1, rel_path, rel_path_size);
}
*out_uri = uri;
ret = 0;

out:
free(rel_path);
return ret;
}

static int chroot_setup_dentry(struct libos_dentry* dent, mode_t type, mode_t perm,
file_off_t size) {
assert(locked(&g_dcache_lock));
Expand All @@ -138,7 +68,7 @@ static int chroot_lookup(struct libos_dentry* dent) {
* and report the right file type.
*/
char* uri = NULL;
ret = chroot_dentry_uri(dent, S_IFREG, &uri);
ret = dentry_uri(dent, S_IFREG, &uri);
if (ret < 0)
goto out;

Expand Down Expand Up @@ -184,7 +114,7 @@ static int chroot_lookup(struct libos_dentry* dent) {
/* Open a temporary read-only PAL handle for a file (used by `unlink` etc.) */
static int chroot_temp_open(struct libos_dentry* dent, PAL_HANDLE* out_palhdl) {
char* uri;
int ret = chroot_dentry_uri(dent, dent->inode->type, &uri);
int ret = dentry_uri(dent, dent->inode->type, &uri);
if (ret < 0)
return ret;

Expand All @@ -202,7 +132,7 @@ static int chroot_do_open(struct libos_handle* hdl, struct libos_dentry* dent, m
int ret;

char* uri;
ret = chroot_dentry_uri(dent, type, &uri);
ret = dentry_uri(dent, type, &uri);
if (ret < 0)
return ret;

Expand Down Expand Up @@ -429,7 +359,7 @@ static int chroot_rename(struct libos_dentry* old, struct libos_dentry* new) {
int ret;
char* new_uri = NULL;

ret = chroot_dentry_uri(new, old->inode->type, &new_uri);
ret = dentry_uri(new, old->inode->type, &new_uri);
if (ret < 0)
goto out;

Expand Down
70 changes: 70 additions & 0 deletions libos/src/fs/libos_dcache.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,76 @@ int dentry_rel_path(struct libos_dentry* dent, char** path, size_t* size) {
return dentry_path(dent, /*relative=*/true, path, size);
}

static const char* strip_prefix(const char* uri) {
const char* s = strchr(uri, ':');
assert(s);
return s + 1;
}

int dentry_uri(struct libos_dentry* dent, mode_t type, char** out_uri) {
assert(dent->mount);
assert(dent->mount->uri);

int ret;

const char* root = strip_prefix(dent->mount->uri);

const char* prefix;
size_t prefix_len;
switch (type) {
case S_IFREG:
prefix = URI_PREFIX_FILE;
prefix_len = static_strlen(URI_PREFIX_FILE);
break;
case S_IFDIR:
prefix = URI_PREFIX_DIR;
prefix_len = static_strlen(URI_PREFIX_DIR);
break;
case S_IFCHR:
prefix = URI_PREFIX_DEV;
prefix_len = static_strlen(URI_PREFIX_DEV);
break;
default:
BUG();
}

char* rel_path;
size_t rel_path_size;
ret = dentry_rel_path(dent, &rel_path, &rel_path_size);
if (ret < 0)
return ret;

/* Treat empty path as "." */
if (*root == '\0')
root = ".";

size_t root_len = strlen(root);

/* Allocate buffer for "<prefix:><root>/<rel_path>" (if `rel_path` is empty, we don't need the
* space for `/`, but overallocating 1 byte doesn't hurt us, and keeps the code simple) */
char* uri = malloc(prefix_len + root_len + 1 + rel_path_size);
if (!uri) {
ret = -ENOMEM;
goto out;
}
memcpy(uri, prefix, prefix_len);
memcpy(uri + prefix_len, root, root_len);
if (rel_path_size == 1) {
/* this is the mount root, the URI is "<prefix:><root>"*/
uri[prefix_len + root_len] = '\0';
} else {
/* this is not the mount root, the URI is "<prefix:><root>/<rel_path>" */
uri[prefix_len + root_len] = '/';
memcpy(uri + prefix_len + root_len + 1, rel_path, rel_path_size);
}
*out_uri = uri;
ret = 0;

out:
free(rel_path);
return ret;
}

struct libos_inode* get_new_inode(struct libos_mount* mount, mode_t type, mode_t perm) {
assert(mount);

Expand Down
4 changes: 2 additions & 2 deletions libos/src/fs/shm/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ static int shm_do_open(struct libos_handle* hdl, struct libos_dentry* dent, mode
assert(locked(&g_dcache_lock));

char* uri;
int ret = chroot_dentry_uri(dent, type, &uri);
int ret = dentry_uri(dent, type, &uri);
if (ret < 0)
return ret;

Expand Down Expand Up @@ -109,7 +109,7 @@ static int shm_lookup(struct libos_dentry* dent) {
* However, "file:" prefix is good enough here: `PalStreamAttributesQuery` will access the file
* and report the right file type.
*/
int ret = chroot_dentry_uri(dent, S_IFREG, &uri);
int ret = dentry_uri(dent, S_IFREG, &uri);
if (ret < 0)
goto out;

Expand Down