diff --git a/libos/include/libos_process.h b/libos/include/libos_process.h index 6478d00370..4dabb98fa8 100644 --- a/libos/include/libos_process.h +++ b/libos/include/libos_process.h @@ -62,6 +62,8 @@ struct libos_process { LISTP_TYPE(libos_child_process) zombies; struct libos_lock children_lock; + + /* If g_dcache_lock is also required, acquire g_dcache_lock first and then fs_lock */ struct libos_lock fs_lock; /* Complete command line for the process, as reported by /proc/[pid]/cmdline; currently filled diff --git a/libos/src/bookkeep/libos_handle.c b/libos/src/bookkeep/libos_handle.c index 853d4a4c9e..bbb39acf8e 100644 --- a/libos/src/bookkeep/libos_handle.c +++ b/libos/src/bookkeep/libos_handle.c @@ -499,16 +499,6 @@ int set_new_fd_handle_above_fd(uint32_t fd, struct libos_handle* hdl, int fd_fla return __set_new_fd_handle(fd, hdl, fd_flags, handle_map, /*find_first=*/true); } -static inline __attribute__((unused)) const char* __handle_name(struct libos_handle* hdl) { - if (hdl->uri) - return hdl->uri; - if (hdl->dentry && hdl->dentry->name[0] != '\0') - return hdl->dentry->name; - if (hdl->fs) - return hdl->fs->name; - return "(unknown)"; -} - void get_handle(struct libos_handle* hdl) { refcount_inc(&hdl->ref_count); } diff --git a/libos/src/sys/libos_getcwd.c b/libos/src/sys/libos_getcwd.c index 2cd64fca7f..1368bfc85a 100644 --- a/libos/src/sys/libos_getcwd.c +++ b/libos/src/sys/libos_getcwd.c @@ -82,19 +82,23 @@ long libos_syscall_fchdir(int fd) { if (!hdl) return -EBADF; + int ret; + lock(&g_dcache_lock); + struct libos_dentry* dent = hdl->dentry; if (!dent) { log_debug("FD=%d has no path in the filesystem", fd); - return -ENOTDIR; + ret = -ENOTDIR; + goto out; } if (!dent->inode || dent->inode->type != S_IFDIR) { char* path = NULL; dentry_abs_path(dent, &path, /*size=*/NULL); log_debug("%s is not a directory", path); free(path); - put_handle(hdl); - return -ENOTDIR; + ret = -ENOTDIR; + goto out; } lock(&g_process.fs_lock); @@ -102,6 +106,9 @@ long libos_syscall_fchdir(int fd) { put_dentry(g_process.cwd); g_process.cwd = dent; unlock(&g_process.fs_lock); + ret = 0; +out: put_handle(hdl); - return 0; + unlock(&g_dcache_lock); + return ret; } diff --git a/libos/src/sys/libos_open.c b/libos/src/sys/libos_open.c index 83fde52c47..ce3afad908 100644 --- a/libos/src/sys/libos_open.c +++ b/libos/src/sys/libos_open.c @@ -373,12 +373,12 @@ static ssize_t do_getdents(int fd, uint8_t* buf, size_t buf_size, bool is_getden goto out_no_unlock; } + lock(&g_dcache_lock); if (!hdl->dentry->inode) { ret = -ENOENT; - goto out_no_unlock; + goto out_unlock_only_dcache_lock; } - lock(&g_dcache_lock); maybe_lock_pos_handle(hdl); lock(&hdl->lock); @@ -467,6 +467,7 @@ static ssize_t do_getdents(int fd, uint8_t* buf, size_t buf_size, bool is_getden out: unlock(&hdl->lock); maybe_unlock_pos_handle(hdl); +out_unlock_only_dcache_lock: unlock(&g_dcache_lock); out_no_unlock: put_handle(hdl);