2 #include <lunaix/mm/valloc.h>
3 #include <lunaix/process.h>
4 #include <lunaix/spike.h>
6 #include <klibc/string.h>
8 #define VFS_SYMLINK_DEPTH 16
10 extern struct lru_zone *dnode_lru, *inode_lru;
13 __vfs_walk(struct v_dnode* start,
15 struct v_dnode** dentry,
16 struct hstr* component,
24 if (depth >= VFS_SYMLINK_DEPTH) {
28 if (path[0] == VFS_PATH_DELIM || !start) {
29 if ((walk_options & VFS_WALK_FSRELATIVE) && start) {
30 start = start->super_block->root;
33 if (!vfs_sysroot->mnt) {
34 panick("vfs: no root");
38 if (path[0] == VFS_PATH_DELIM) {
45 struct v_dnode* dnode;
46 struct v_inode* current_inode;
47 struct v_dnode* current_level = start;
49 struct hstr name = HSTR(fname_buffer, 0);
51 char current = path[i++], lookahead;
53 lookahead = path[i++];
54 if (current != VFS_PATH_DELIM) {
55 if (j >= VFS_NAME_MAXLEN - 1) {
58 if (!VFS_VALID_CHAR(current)) {
61 fname_buffer[j++] = current;
67 // handling cases like /^.*(\/+).*$/
68 if (lookahead == VFS_PATH_DELIM) {
74 hstr_rehash(&name, HSTR_FULL_HASH);
76 if (!lookahead && (walk_options & VFS_WALK_PARENT)) {
78 hstrcpy(component, &name);
83 current_inode = current_level->inode;
85 if ((current_inode->itype & VFS_IFSYMLINK) &&
86 !(walk_options & VFS_WALK_NOFOLLOW)) {
89 lock_inode(current_inode);
91 current_inode->ops->read_symlink(current_inode, &link))) {
92 unlock_inode(current_inode);
95 unlock_inode(current_inode);
97 errno = __vfs_walk(current_level->parent,
103 fname_buffer + name.len + 1);
109 // reposition the resolved subtree pointed by symlink
110 // vfs_dcache_rehash(current_level->parent, dnode);
111 current_level = dnode;
112 current_inode = dnode->inode;
115 lock_dnode(current_level);
117 dnode = vfs_dcache_lookup(current_level, &name);
120 dnode = vfs_d_alloc(current_level, &name);
127 lock_inode(current_inode);
129 errno = current_inode->ops->dir_lookup(current_inode, dnode);
131 if (errno == ENOENT && (walk_options & VFS_WALK_MKPARENT)) {
132 if (!current_inode->ops->mkdir) {
135 errno = current_inode->ops->mkdir(current_inode, dnode);
139 vfs_dcache_add(current_level, dnode);
140 unlock_inode(current_inode);
143 unlock_dnode(current_level);
148 unlock_dnode(current_level);
151 current_level = dnode;
156 *dentry = current_level;
167 vfs_walk(struct v_dnode* start,
169 struct v_dnode** dentry,
170 struct hstr* component,
173 // allocate a file name stack for path walking and recursion to resolve
175 char* name_buffer = valloc(2048);
178 __vfs_walk(start, path, dentry, component, options, 0, name_buffer);
185 vfs_walk_proc(const char* path,
186 struct v_dnode** dentry,
187 struct hstr* component,
190 return vfs_walk(__current->cwd, path, dentry, component, options);