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");
41 struct v_dnode* dnode;
42 struct v_inode* current_inode;
43 struct v_dnode* current_level = start;
45 struct hstr name = HSTR(fname_buffer, 0);
47 char current = path[i++], lookahead;
49 lookahead = path[i++];
50 if (current != VFS_PATH_DELIM) {
51 if (j >= VFS_NAME_MAXLEN - 1) {
54 if (!VFS_VALID_CHAR(current)) {
57 fname_buffer[j++] = current;
63 // handling cases like /^.*(\/+).*$/
64 if (lookahead == VFS_PATH_DELIM) {
70 hstr_rehash(&name, HSTR_FULL_HASH);
72 if (!lookahead && (walk_options & VFS_WALK_PARENT)) {
74 component->hash = name.hash;
76 strcpy(component->value, fname_buffer);
81 current_inode = current_level->inode;
83 if ((current_inode->itype & VFS_IFSYMLINK) &&
84 !(walk_options & VFS_WALK_NOFOLLOW)) {
87 lock_inode(current_inode);
89 current_inode->ops->read_symlink(current_inode, &link))) {
90 unlock_inode(current_inode);
93 unlock_inode(current_inode);
95 errno = __vfs_walk(current_level->parent,
101 fname_buffer + name.len + 1);
107 // reposition the resolved subtree pointed by symlink
108 vfs_dcache_rehash(current_level->parent, dnode);
109 current_level = dnode;
110 current_inode = dnode->inode;
113 lock_dnode(current_level);
115 dnode = vfs_dcache_lookup(current_level, &name);
118 dnode = vfs_d_alloc(current_level, &name);
125 lock_inode(current_inode);
127 errno = current_inode->ops->dir_lookup(current_inode, dnode);
129 if (errno == ENOENT && (walk_options & VFS_WALK_MKPARENT)) {
130 if (!current_inode->ops->mkdir) {
133 errno = current_inode->ops->mkdir(current_inode, dnode);
137 vfs_dcache_add(current_level, dnode);
138 unlock_inode(current_inode);
141 unlock_dnode(current_level);
146 unlock_dnode(current_level);
149 current_level = dnode;
154 *dentry = current_level;
165 vfs_walk(struct v_dnode* start,
167 struct v_dnode** dentry,
168 struct hstr* component,
171 // allocate a file name stack for path walking and recursion to resolve
173 char* name_buffer = valloc(2048);
176 __vfs_walk(start, path, dentry, component, options, 0, name_buffer);
183 vfs_walk_proc(const char* path,
184 struct v_dnode** dentry,
185 struct hstr* component,
188 return vfs_walk(__current->cwd, path, dentry, component, options);