mount: simplify mount_traverse
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
parent
a5c57b7893
commit
27c3ffa8b1
3 changed files with 26 additions and 112 deletions
110
fs/vfs/mount.c
110
fs/vfs/mount.c
|
|
@ -40,15 +40,6 @@ struct mount *mount_find_child(struct mount *parent, dirent_t *dent)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
static int load_inode(dirent_t *dent)
|
||||
{
|
||||
if (dent->inode)
|
||||
return 0;
|
||||
|
||||
int res = dent->fs->fs_ops.open_inode(dent->fs, dent->ino_num, dent, &dent->inode);
|
||||
return res;
|
||||
}
|
||||
|
||||
static int mount_traverse(const char *path, struct mount **out_mount, dirent_t **out_dirent)
|
||||
{
|
||||
if (!root_mount)
|
||||
|
|
@ -65,88 +56,8 @@ static int mount_traverse(const char *path, struct mount **out_mount, dirent_t *
|
|||
return 0;
|
||||
}
|
||||
|
||||
struct mount *cur = root_mount;
|
||||
dirent_t *cur_dent = root_mount->fs->root_dirent;
|
||||
|
||||
if (!cur_dent)
|
||||
return -ENOENT;
|
||||
|
||||
dirent_inc_ref(cur_dent);
|
||||
|
||||
const char *p = path;
|
||||
|
||||
while (*p != '\0') {
|
||||
const char *comp_start = p;
|
||||
while (*p != '/' && *p != '\0')
|
||||
p++;
|
||||
|
||||
size_t comp_len = p - comp_start;
|
||||
if (comp_len > FILE_NAME_MAX_LEN) {
|
||||
dirent_dec_ref(cur_dent);
|
||||
return -ENAMETOOLONG;
|
||||
}
|
||||
|
||||
char name[FILE_NAME_MAX_LEN + 1];
|
||||
memcpy(name, comp_start, comp_len);
|
||||
name[comp_len] = '\0';
|
||||
|
||||
while (*p == '/')
|
||||
p++;
|
||||
|
||||
bool is_last = (*p == '\0');
|
||||
|
||||
dirent_t *found = NULL;
|
||||
int res = dirent_lookup(cur_dent, name, &found);
|
||||
if (res < 0) {
|
||||
dirent_dec_ref(cur_dent);
|
||||
return res;
|
||||
}
|
||||
|
||||
res = load_inode(found);
|
||||
if (res < 0) {
|
||||
dirent_dec_ref(found);
|
||||
dirent_dec_ref(cur_dent);
|
||||
return res;
|
||||
}
|
||||
|
||||
if (is_last) {
|
||||
dirent_dec_ref(cur_dent);
|
||||
*out_mount = cur;
|
||||
*out_dirent = found;
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct mount *child = NULL;
|
||||
list_item_t *item = cur->children.head;
|
||||
while (item) {
|
||||
struct mount *m = item->ptr;
|
||||
if (m->mountpoint == found) {
|
||||
child = m;
|
||||
break;
|
||||
}
|
||||
item = item->next;
|
||||
}
|
||||
|
||||
dirent_dec_ref(cur_dent);
|
||||
|
||||
if (child) {
|
||||
dirent_dec_ref(found);
|
||||
cur = child;
|
||||
cur_dent = cur->fs->root_dirent;
|
||||
if (!cur_dent)
|
||||
return -ENOENT;
|
||||
dirent_inc_ref(cur_dent);
|
||||
} else {
|
||||
cur_dent = found;
|
||||
}
|
||||
}
|
||||
|
||||
dirent_dec_ref(cur_dent);
|
||||
*out_mount = cur;
|
||||
*out_dirent = cur->fs->root_dirent;
|
||||
if (*out_dirent)
|
||||
dirent_inc_ref(*out_dirent);
|
||||
return 0;
|
||||
return vfs_openat(root_mount, root_mount->fs->root_dirent, path,
|
||||
out_dirent, out_mount, false);
|
||||
}
|
||||
|
||||
int vfs_mount(const char *path, fs_t *fs, u64 flags)
|
||||
|
|
@ -154,9 +65,9 @@ int vfs_mount(const char *path, fs_t *fs, u64 flags)
|
|||
if (!fs)
|
||||
return -EINVAL;
|
||||
|
||||
if (!path) {
|
||||
spinlock_acquire(&mount_lock);
|
||||
|
||||
if (!path) {
|
||||
if (root_mount) {
|
||||
spinlock_release(&mount_lock);
|
||||
return -EBUSY;
|
||||
|
|
@ -183,17 +94,16 @@ int vfs_mount(const char *path, fs_t *fs, u64 flags)
|
|||
struct mount *parent = NULL;
|
||||
dirent_t *mp = NULL;
|
||||
int res = mount_traverse(path, &parent, &mp);
|
||||
if (res < 0) {
|
||||
spinlock_release(&mount_lock);
|
||||
if (res < 0)
|
||||
return res;
|
||||
}
|
||||
|
||||
if ((mp->inode->mode & S_IFMT) != S_IFDIR) {
|
||||
dirent_dec_ref(mp);
|
||||
spinlock_release(&mount_lock);
|
||||
return -ENOTDIR;
|
||||
}
|
||||
|
||||
spinlock_acquire(&mount_lock);
|
||||
|
||||
list_item_t *item = parent->children.head;
|
||||
while (item) {
|
||||
struct mount *m = item->ptr;
|
||||
|
|
@ -236,15 +146,13 @@ int vfs_umount(const char *path)
|
|||
if (!path)
|
||||
return -EINVAL;
|
||||
|
||||
spinlock_acquire(&mount_lock);
|
||||
|
||||
struct mount *parent = NULL;
|
||||
dirent_t *mp = NULL;
|
||||
int res = mount_traverse(path, &parent, &mp);
|
||||
if (res < 0) {
|
||||
spinlock_release(&mount_lock);
|
||||
if (res < 0)
|
||||
return res;
|
||||
}
|
||||
|
||||
spinlock_acquire(&mount_lock);
|
||||
|
||||
struct mount *target = NULL;
|
||||
list_item_t *item = parent->children.head;
|
||||
|
|
|
|||
24
fs/vfs/vfs.c
24
fs/vfs/vfs.c
|
|
@ -84,7 +84,7 @@ static int next_level(const char *path, dirent_t *dent, dirent_t **out_dirent)
|
|||
return -ENOENT;
|
||||
}
|
||||
|
||||
int vfs_openat(struct mount *mnt, dirent_t *in, const char *name, dirent_t **out)
|
||||
int vfs_openat(struct mount *mnt, dirent_t *in, const char *name, dirent_t **out, struct mount **out_mnt, bool follow)
|
||||
{
|
||||
if (!in || !name || !out)
|
||||
return -EINVAL;
|
||||
|
|
@ -99,6 +99,8 @@ int vfs_openat(struct mount *mnt, dirent_t *in, const char *name, dirent_t **out
|
|||
|
||||
if (*path_optr == '\0') {
|
||||
*out = current_dir;
|
||||
if (out_mnt)
|
||||
*out_mnt = mnt;
|
||||
kfree(path_optr);
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -119,8 +121,15 @@ int vfs_openat(struct mount *mnt, dirent_t *in, const char *name, dirent_t **out
|
|||
dirent_dec_ref(current_dir);
|
||||
current_dir = next_dir;
|
||||
|
||||
cur_path += res;
|
||||
|
||||
while (*cur_path == '/')
|
||||
cur_path++;
|
||||
|
||||
bool at_end = (*cur_path == '\0');
|
||||
|
||||
struct mount *child = mount_find_child(cur_mount, current_dir);
|
||||
if (child) {
|
||||
if (child && (!at_end || follow)) {
|
||||
dirent_dec_ref(current_dir);
|
||||
cur_mount = child;
|
||||
current_dir = child->fs->root_dirent;
|
||||
|
|
@ -131,13 +140,10 @@ int vfs_openat(struct mount *mnt, dirent_t *in, const char *name, dirent_t **out
|
|||
dirent_inc_ref(current_dir);
|
||||
}
|
||||
|
||||
cur_path += res;
|
||||
|
||||
while (*cur_path == '/')
|
||||
cur_path++;
|
||||
|
||||
if (*cur_path == '\0') {
|
||||
if (at_end) {
|
||||
*out = current_dir;
|
||||
if (out_mnt)
|
||||
*out_mnt = cur_mount;
|
||||
ret = 0;
|
||||
goto out;
|
||||
}
|
||||
|
|
@ -157,7 +163,7 @@ int vfs_open(const char *abs_path, dirent_t **out)
|
|||
if (!root || !root->fs || !root->fs->root_dirent)
|
||||
return -ENOENT;
|
||||
|
||||
return vfs_openat(root, root->fs->root_dirent, abs_path, out);
|
||||
return vfs_openat(root, root->fs->root_dirent, abs_path, out, NULL, true);
|
||||
}
|
||||
|
||||
void vfs_close(dirent_t *dent)
|
||||
|
|
|
|||
|
|
@ -107,7 +107,7 @@ void vfs_init(void);
|
|||
void vfs_register(struct fs_driver *fs);
|
||||
|
||||
int vfs_open(const char *abs_path, dirent_t **out);
|
||||
int vfs_openat(struct mount *mnt, dirent_t *in, const char *name, dirent_t **out);
|
||||
int vfs_openat(struct mount *mnt, dirent_t *in, const char *name, dirent_t **out, struct mount **out_mnt, bool follow);
|
||||
void vfs_close(dirent_t *dent);
|
||||
int vfs_readdir(dirent_t *dir, struct dir_context *ctx, dirent_t **out);
|
||||
int vfs_creat(dirent_t *parent, const char *name, u64 mode, int flags, dirent_t **out);
|
||||
|
|
|
|||
Loading…
Reference in a new issue