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");
39 struct v_dnode* dnode;
40 struct v_inode* current_inode;
41 struct v_dnode* current_level = start;
43 struct hstr name = HSTR(fname_buffer, 0);
45 char current = path[i++], lookahead;
46 while (current && current_level) {
47 lookahead = path[i++];
48 if (current != VFS_PATH_DELIM) {
49 if (j >= VFS_NAME_MAXLEN - 1) {
52 if (!VFS_VALID_CHAR(current)) {
55 fname_buffer[j++] = current;
61 // handling cases like /^.*(\/+).*$/
62 if (lookahead == VFS_PATH_DELIM) {
68 hstr_rehash(&name, HSTR_FULL_HASH);
70 if (!lookahead && (walk_options & VFS_WALK_PARENT)) {
72 component->hash = name.hash;
74 strcpy(component->value, fname_buffer);
79 current_inode = current_level->inode;
81 if ((current_inode->itype & VFS_IFSYMLINK) &&
82 !(walk_options & VFS_WALK_NOFOLLOW)) {
85 lock_inode(current_inode);
87 current_inode->ops->read_symlink(current_inode, &link))) {
88 unlock_inode(current_inode);
91 unlock_inode(current_inode);
93 errno = __vfs_walk(current_level->parent,
99 fname_buffer + name.len + 1);
105 // reposition the resolved subtree pointed by symlink
106 vfs_dcache_rehash(current_level->parent, dnode);
107 current_level = dnode;
108 current_inode = dnode->inode;
111 lock_dnode(current_level);
113 dnode = vfs_dcache_lookup(current_level, &name);
116 dnode = vfs_d_alloc(current_level, &name);
123 lock_inode(current_inode);
125 errno = current_inode->ops->dir_lookup(current_inode, dnode);
127 if (errno == ENOENT && (walk_options & VFS_WALK_MKPARENT)) {
128 if (!current_inode->ops->mkdir) {
131 errno = current_inode->ops->mkdir(current_inode, dnode);
135 vfs_dcache_add(current_level, dnode);
136 unlock_inode(current_inode);
139 unlock_dnode(current_level);
144 unlock_dnode(current_level);
147 current_level = dnode;
152 *dentry = current_level;
163 vfs_walk(struct v_dnode* start,
165 struct v_dnode** dentry,
166 struct hstr* component,
169 // allocate a file name stack for path walking and recursion to resolve
171 char* name_buffer = valloc(2048);
174 __vfs_walk(start, path, dentry, component, options, 0, name_buffer);
181 vfs_walk_proc(const char* path,
182 struct v_dnode** dentry,
183 struct hstr* component,
186 return vfs_walk(__current->cwd, path, dentry, component, options);