2 #include <lunaix/mm/valloc.h>
3 #include <lunaix/spike.h>
5 #include <klibc/string.h>
7 #define VFS_SYMLINK_DEPTH 16
9 extern struct lru_zone *dnode_lru, *inode_lru;
12 __vfs_walk(struct v_dnode* start,
14 struct v_dnode** dentry,
15 struct hstr* component,
23 if (depth >= VFS_SYMLINK_DEPTH) {
27 if (path[0] == VFS_PATH_DELIM || !start) {
28 if ((walk_options & VFS_WALK_FSRELATIVE) && start) {
29 start = start->super_block->root;
32 if (!vfs_sysroot->mnt) {
33 panick("vfs: no root");
37 if (path[0] == VFS_PATH_DELIM) {
44 struct v_dnode* dnode;
45 struct v_inode* current_inode;
46 struct v_dnode* current_level = start;
48 struct hstr name = HSTR(fname_buffer, 0);
50 char current = path[i++], lookahead;
52 lookahead = path[i++];
53 if (current != VFS_PATH_DELIM) {
54 if (j >= VFS_NAME_MAXLEN - 1) {
57 if (!VFS_VALID_CHAR(current)) {
60 fname_buffer[j++] = current;
66 // handling cases like /^.*(\/+).*$/
67 if (lookahead == VFS_PATH_DELIM) {
73 hstr_rehash(&name, HSTR_FULL_HASH);
75 if (!lookahead && (walk_options & VFS_WALK_PARENT)) {
77 hstrcpy(component, &name);
82 current_inode = current_level->inode;
84 if ((current_inode->itype & VFS_IFSYMLINK) &&
85 !(walk_options & VFS_WALK_NOFOLLOW)) {
88 lock_inode(current_inode);
90 current_inode->ops->read_symlink(current_inode, &link))) {
91 unlock_inode(current_inode);
94 unlock_inode(current_inode);
96 errno = __vfs_walk(current_level->parent,
102 fname_buffer + name.len + 1);
108 // reposition the resolved subtree pointed by symlink
109 // vfs_dcache_rehash(current_level->parent, dnode);
110 current_level = dnode;
111 current_inode = dnode->inode;
114 lock_dnode(current_level);
116 dnode = vfs_dcache_lookup(current_level, &name);
119 dnode = vfs_d_alloc(current_level, &name);
126 lock_inode(current_inode);
128 errno = current_inode->ops->dir_lookup(current_inode, dnode);
130 if (errno == ENOENT && (walk_options & VFS_WALK_MKPARENT)) {
131 if (!current_inode->ops->mkdir) {
134 errno = current_inode->ops->mkdir(current_inode, dnode);
138 vfs_dcache_add(current_level, dnode);
139 unlock_inode(current_inode);
142 unlock_dnode(current_level);
147 unlock_dnode(current_level);
150 current_level = dnode;
155 *dentry = current_level;
166 vfs_walk(struct v_dnode* start,
168 struct v_dnode** dentry,
169 struct hstr* component,
172 // allocate a file name stack for path walking and recursion to resolve
174 char* name_buffer = valloc(2048);
177 __vfs_walk(start, path, dentry, component, options, 0, name_buffer);
184 vfs_walk_proc(const char* path,
185 struct v_dnode** dentry,
186 struct hstr* component,
189 return vfs_walk(__current->cwd, path, dentry, component, options);