syscalls: chroot, fchmodat, fchownat, faccessat
[lunaix-os.git] / lunaix-os / kernel / fs / path_walk.c
1 #include <lunaix/fs.h>
2 #include <lunaix/mm/valloc.h>
3 #include <lunaix/process.h>
4 #include <lunaix/spike.h>
5
6 #include <usr/lunaix/fcntl_defs.h>
7
8 #include <klibc/string.h>
9
10 #define VFS_SYMLINK_DEPTH 16
11 #define VFS_SYMLINK_MAXLEN 512
12
13 extern struct lru_zone *dnode_lru, *inode_lru;
14
15 int
16 __vfs_walk(struct v_dnode* start,
17            const char* path,
18            struct v_dnode** dentry,
19            struct hstr* component,
20            int walk_options,
21            size_t depth,
22            char* fname_buffer)
23 {
24     int errno = 0;
25     int i = 0, j = 0;
26
27     if (depth >= VFS_SYMLINK_DEPTH) {
28         return ELOOP;
29     }
30
31     if (path[0] == VFS_PATH_DELIM || !start) {
32         if ((walk_options & VFS_WALK_FSRELATIVE) && start) {
33             start = start->super_block->root;
34         } 
35         else if (unlikely(!__current)) {
36             start = vfs_sysroot;
37         }
38         else {
39             start = __current->root ?: vfs_sysroot;
40         }
41
42         if (unlikely(!start || !start->mnt)) {
43             fail("vfs: no root");
44         }
45
46         if (path[0] == VFS_PATH_DELIM) {
47             i++;
48         }
49     }
50
51     assert(start);
52
53     struct v_dnode* dnode;
54     struct v_dnode* current_level = start;
55     struct v_inode* current_inode = current_level->inode;
56
57     struct hstr name = HSTR(fname_buffer, 0);
58
59     char current = path[i++], lookahead;
60     while (current) 
61     {
62         lookahead = path[i++];
63
64         if (current != VFS_PATH_DELIM) 
65         {
66             if (j >= VFS_NAME_MAXLEN - 1) {
67                 return ENAMETOOLONG;
68             }
69             if (!VFS_VALID_CHAR(current)) {
70                 return EINVAL;
71             }
72             fname_buffer[j++] = current;
73             if (lookahead) {
74                 goto cont;
75             }
76         }
77
78         // handling cases like /^.*(\/+).*$/
79         if (lookahead == VFS_PATH_DELIM) {
80             goto cont;
81         }
82
83         fname_buffer[j] = 0;
84         name.len = j;
85         hstr_rehash(&name, HSTR_FULL_HASH);
86
87         if (!lookahead && (walk_options & VFS_WALK_PARENT)) {
88             if (component) {
89                 hstrcpy(component, &name);
90             }
91             break;
92         }
93
94         lock_dnode(current_level);
95
96         if (!check_allow_execute(current_inode)) {
97             errno = EACCESS;
98             goto error;
99         }
100
101         dnode = vfs_dcache_lookup(current_level, &name);
102
103         if (!dnode) 
104         {
105             dnode = vfs_d_alloc(current_level, &name);
106
107             if (!dnode) {
108                 errno = ENOMEM;
109                 goto error;
110             }
111
112             lock_inode(current_inode);
113
114             errno = current_inode->ops->dir_lookup(current_inode, dnode);
115
116             if (errno == ENOENT && (walk_options & VFS_WALK_MKPARENT)) {
117                 if (!current_inode->ops->mkdir) {
118                     errno = ENOTSUP;
119                 } else {
120                     errno = current_inode->ops->mkdir(current_inode, dnode);
121                 }
122             }
123
124             vfs_dcache_add(current_level, dnode);
125             unlock_inode(current_inode);
126
127             if (errno) {
128                 unlock_dnode(current_level);
129                 goto cleanup;
130             }
131         }
132
133         unlock_dnode(current_level);
134
135         j = 0;
136         current_level = dnode;
137         current_inode = current_level->inode;
138
139         assert(current_inode);
140         
141         if (check_symlink_node(current_inode) &&
142             !(walk_options & VFS_WALK_NOFOLLOW)) 
143         {
144             const char* link;
145             struct v_inode_ops* iops;
146
147             iops = current_inode->ops;
148
149             if (!iops->read_symlink) {
150                 errno = ENOTSUP;
151                 goto error;
152             }
153
154             lock_inode(current_inode);
155
156             errno = iops->read_symlink(current_inode, &link);
157             if ((errno < 0)) {
158                 unlock_inode(current_inode);
159                 goto error;
160             }
161
162             unlock_inode(current_inode);
163
164             errno = __vfs_walk(current_level->parent,
165                                link,
166                                &dnode,
167                                NULL,
168                                0,
169                                depth + 1,
170                                fname_buffer + name.len + 1);
171
172             if (errno) {
173                 goto error;
174             }
175
176             current_level = dnode;
177             current_inode = dnode->inode;
178         }
179
180     cont:
181         current = lookahead;
182     };
183
184     *dentry = current_level;
185     return 0;
186
187 cleanup:
188     vfs_d_free(dnode);
189
190 error:
191     *dentry = NULL;
192     return errno;
193 }
194
195 int
196 vfs_walk(struct v_dnode* start,
197          const char* path,
198          struct v_dnode** dentry,
199          struct hstr* component,
200          int options)
201 {
202     if (!path) {
203         *dentry = NULL;
204         return 0;
205     }
206
207     // allocate a file name stack for path walking and recursion to resolve
208     // symlink
209     char* name_buffer = valloc(2048);
210
211     int errno =
212       __vfs_walk(start, path, dentry, component, options, 0, name_buffer);
213
214     vfree(name_buffer);
215     return errno;
216 }
217
218 int
219 vfs_walk_proc(const char* path,
220               struct v_dnode** dentry,
221               struct hstr* component,
222               int options)
223 {
224     return vfs_walk(__current->cwd, path, dentry, component, options);
225 }
226
227 int
228 vfs_walkat(int fd, const char* path, int at_opts, struct v_dnode** dnode_out)
229 {
230     int errno, options = 0;
231     struct v_dnode *root_dnode;
232     struct v_fd* _fd;
233
234     if ((at_opts & AT_FDCWD)) {
235         root_dnode = __current->cwd;
236     }
237     else 
238     {
239         errno = vfs_getfd(fd, &_fd);
240         if (errno) {
241             return errno;
242         }
243
244         root_dnode = _fd->file->dnode;
245     }
246
247     if ((at_opts & AT_SYMLINK_NOFOLLOW)) {
248         options |= VFS_WALK_NOFOLLOW;
249     }
250
251     errno = vfs_walk(root_dnode, path, dnode_out, NULL, options);
252     if (errno) {
253         return errno;
254     }
255
256     return 0;
257 }