1 | /*
|
---|
2 | * Copyright (c) 2008 Jakub Jermar
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup fs
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file vfs_ops.c
|
---|
35 | * @brief Operations that VFS offers to its clients.
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include "vfs.h"
|
---|
39 | #include <macros.h>
|
---|
40 | #include <stdint.h>
|
---|
41 | #include <async.h>
|
---|
42 | #include <errno.h>
|
---|
43 | #include <stdio.h>
|
---|
44 | #include <stdlib.h>
|
---|
45 | #include <str.h>
|
---|
46 | #include <stdbool.h>
|
---|
47 | #include <fibril_synch.h>
|
---|
48 | #include <adt/list.h>
|
---|
49 | #include <unistd.h>
|
---|
50 | #include <ctype.h>
|
---|
51 | #include <fcntl.h>
|
---|
52 | #include <assert.h>
|
---|
53 | #include <vfs/canonify.h>
|
---|
54 | #include <vfs/vfs_mtab.h>
|
---|
55 |
|
---|
56 | FIBRIL_MUTEX_INITIALIZE(mtab_list_lock);
|
---|
57 | LIST_INITIALIZE(mtab_list);
|
---|
58 | static size_t mtab_size = 0;
|
---|
59 |
|
---|
60 | /* Forward declarations of static functions. */
|
---|
61 | static int vfs_truncate_internal(fs_handle_t, service_id_t, fs_index_t,
|
---|
62 | aoff64_t);
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * This rwlock prevents the race between a triplet-to-VFS-node resolution and a
|
---|
66 | * concurrent VFS operation which modifies the file system namespace.
|
---|
67 | */
|
---|
68 | FIBRIL_RWLOCK_INITIALIZE(namespace_rwlock);
|
---|
69 |
|
---|
70 | vfs_node_t *root = NULL;
|
---|
71 |
|
---|
72 | static int vfs_connect_internal(service_id_t service_id, unsigned flags, unsigned instance,
|
---|
73 | char *options, char *fsname, vfs_node_t **root)
|
---|
74 | {
|
---|
75 | fs_handle_t fs_handle = 0;
|
---|
76 |
|
---|
77 | fibril_mutex_lock(&fs_list_lock);
|
---|
78 | while (1) {
|
---|
79 | fs_handle = fs_name_to_handle(instance, fsname, false);
|
---|
80 |
|
---|
81 | if (fs_handle != 0 || !(flags & IPC_FLAG_BLOCKING)) {
|
---|
82 | break;
|
---|
83 | }
|
---|
84 |
|
---|
85 | fibril_condvar_wait(&fs_list_cv, &fs_list_lock);
|
---|
86 | }
|
---|
87 | fibril_mutex_unlock(&fs_list_lock);
|
---|
88 |
|
---|
89 | if (fs_handle == 0) {
|
---|
90 | return ENOENT;
|
---|
91 | }
|
---|
92 |
|
---|
93 | /* Tell the mountee that it is being mounted. */
|
---|
94 | ipc_call_t answer;
|
---|
95 | async_exch_t *exch = vfs_exchange_grab(fs_handle);
|
---|
96 | aid_t msg = async_send_1(exch, VFS_OUT_MOUNTED, (sysarg_t) service_id, &answer);
|
---|
97 | /* Send the mount options */
|
---|
98 | sysarg_t rc = async_data_write_start(exch, options, str_size(options));
|
---|
99 | if (rc != EOK) {
|
---|
100 | async_forget(msg);
|
---|
101 | vfs_exchange_release(exch);
|
---|
102 | return rc;
|
---|
103 | }
|
---|
104 | async_wait_for(msg, &rc);
|
---|
105 | vfs_exchange_release(exch);
|
---|
106 |
|
---|
107 | if (rc != EOK) {
|
---|
108 | return rc;
|
---|
109 | }
|
---|
110 |
|
---|
111 | vfs_lookup_res_t res;
|
---|
112 | res.triplet.fs_handle = fs_handle;
|
---|
113 | res.triplet.service_id = service_id;
|
---|
114 | res.triplet.index = (fs_index_t) IPC_GET_ARG1(answer);
|
---|
115 | res.size = (int64_t) MERGE_LOUP32(IPC_GET_ARG2(answer), IPC_GET_ARG3(answer));
|
---|
116 | res.type = VFS_NODE_DIRECTORY;
|
---|
117 |
|
---|
118 | /* Add reference to the mounted root. */
|
---|
119 | *root = vfs_node_get(&res);
|
---|
120 | assert(*root);
|
---|
121 |
|
---|
122 | return EOK;
|
---|
123 | }
|
---|
124 |
|
---|
125 | static int vfs_mount_internal(service_id_t service_id, unsigned flags, unsigned instance,
|
---|
126 | char *opts, char *fs_name, char *mp)
|
---|
127 | {
|
---|
128 | /* Resolve the path to the mountpoint. */
|
---|
129 |
|
---|
130 | if (root == NULL) {
|
---|
131 | /* We still don't have the root file system mounted. */
|
---|
132 | if (str_cmp(mp, "/") != 0) {
|
---|
133 | /*
|
---|
134 | * We can't resolve this without the root filesystem
|
---|
135 | * being mounted first.
|
---|
136 | */
|
---|
137 | return ENOENT;
|
---|
138 | }
|
---|
139 |
|
---|
140 | return vfs_connect_internal(service_id, flags, instance, opts, fs_name, &root);
|
---|
141 | }
|
---|
142 |
|
---|
143 | /* We already have the root FS. */
|
---|
144 | if (str_cmp(mp, "/") == 0) {
|
---|
145 | /* Trying to mount root FS over root FS */
|
---|
146 | return EBUSY;
|
---|
147 | }
|
---|
148 |
|
---|
149 | vfs_lookup_res_t mp_res;
|
---|
150 | int rc = vfs_lookup_internal(root, mp, L_DIRECTORY, &mp_res);
|
---|
151 | if (rc != EOK) {
|
---|
152 | /* The lookup failed. */
|
---|
153 | return rc;
|
---|
154 | }
|
---|
155 |
|
---|
156 | vfs_node_t *mp_node;
|
---|
157 | mp_node = vfs_node_get(&mp_res);
|
---|
158 | if (!mp_node) {
|
---|
159 | return ENOMEM;
|
---|
160 | }
|
---|
161 |
|
---|
162 | if (mp_node->mount != NULL) {
|
---|
163 | return EBUSY;
|
---|
164 | }
|
---|
165 |
|
---|
166 | if (mp_node->type != VFS_NODE_DIRECTORY) {
|
---|
167 | printf("%s node not a directory, type=%d\n", mp, mp_node->type);
|
---|
168 | return ENOTDIR;
|
---|
169 | }
|
---|
170 |
|
---|
171 | if (vfs_node_has_children(mp_node)) {
|
---|
172 | return ENOTEMPTY;
|
---|
173 | }
|
---|
174 |
|
---|
175 | vfs_node_t *mountee;
|
---|
176 |
|
---|
177 | rc = vfs_connect_internal(service_id, flags, instance, opts, fs_name, &mountee);
|
---|
178 | if (rc != EOK) {
|
---|
179 | vfs_node_put(mp_node);
|
---|
180 | return ENOMEM;
|
---|
181 | }
|
---|
182 |
|
---|
183 | mp_node->mount = mountee;
|
---|
184 | /* The two references to nodes are held by the mount so that they cannot be freed.
|
---|
185 | * They are removed in detach_internal().
|
---|
186 | */
|
---|
187 | return EOK;
|
---|
188 | }
|
---|
189 |
|
---|
190 | void vfs_mount_srv(ipc_callid_t rid, ipc_call_t *request)
|
---|
191 | {
|
---|
192 | /*
|
---|
193 | * We expect the library to do the device-name to device-handle
|
---|
194 | * translation for us, thus the device handle will arrive as ARG1
|
---|
195 | * in the request.
|
---|
196 | */
|
---|
197 | service_id_t service_id = (service_id_t) IPC_GET_ARG1(*request);
|
---|
198 |
|
---|
199 | /*
|
---|
200 | * Mount flags are passed as ARG2.
|
---|
201 | */
|
---|
202 | unsigned int flags = (unsigned int) IPC_GET_ARG2(*request);
|
---|
203 |
|
---|
204 | /*
|
---|
205 | * Instance number is passed as ARG3.
|
---|
206 | */
|
---|
207 | unsigned int instance = IPC_GET_ARG3(*request);
|
---|
208 |
|
---|
209 | /* We want the client to send us the mount point. */
|
---|
210 | char *mp;
|
---|
211 | int rc = async_data_write_accept((void **) &mp, true, 0, MAX_PATH_LEN,
|
---|
212 | 0, NULL);
|
---|
213 | if (rc != EOK) {
|
---|
214 | async_answer_0(rid, rc);
|
---|
215 | return;
|
---|
216 | }
|
---|
217 |
|
---|
218 | /* Now we expect to receive the mount options. */
|
---|
219 | char *opts;
|
---|
220 | rc = async_data_write_accept((void **) &opts, true, 0, MAX_MNTOPTS_LEN,
|
---|
221 | 0, NULL);
|
---|
222 | if (rc != EOK) {
|
---|
223 | async_answer_0(rid, rc);
|
---|
224 | free(mp);
|
---|
225 | return;
|
---|
226 | }
|
---|
227 |
|
---|
228 | /*
|
---|
229 | * Now, we expect the client to send us data with the name of the file
|
---|
230 | * system.
|
---|
231 | */
|
---|
232 | char *fs_name;
|
---|
233 | rc = async_data_write_accept((void **) &fs_name, true, 0,
|
---|
234 | FS_NAME_MAXLEN, 0, NULL);
|
---|
235 | if (rc != EOK) {
|
---|
236 | async_answer_0(rid, rc);
|
---|
237 | free(mp);
|
---|
238 | free(opts);
|
---|
239 | return;
|
---|
240 | }
|
---|
241 |
|
---|
242 | /* Add the filesystem info to the list of mounted filesystems */
|
---|
243 | mtab_ent_t *mtab_ent = malloc(sizeof(mtab_ent_t));
|
---|
244 | if (!mtab_ent) {
|
---|
245 | async_answer_0(rid, ENOMEM);
|
---|
246 | free(mp);
|
---|
247 | free(fs_name);
|
---|
248 | free(opts);
|
---|
249 | return;
|
---|
250 | }
|
---|
251 |
|
---|
252 | /* Mount the filesystem. */
|
---|
253 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
254 | rc = vfs_mount_internal(service_id, flags, instance, opts, fs_name, mp);
|
---|
255 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
256 |
|
---|
257 | /* Add the filesystem info to the list of mounted filesystems */
|
---|
258 | if (rc == EOK) {
|
---|
259 | str_cpy(mtab_ent->mp, MAX_PATH_LEN, mp);
|
---|
260 | str_cpy(mtab_ent->fs_name, FS_NAME_MAXLEN, fs_name);
|
---|
261 | str_cpy(mtab_ent->opts, MAX_MNTOPTS_LEN, opts);
|
---|
262 | mtab_ent->instance = instance;
|
---|
263 | mtab_ent->service_id = service_id;
|
---|
264 |
|
---|
265 | link_initialize(&mtab_ent->link);
|
---|
266 |
|
---|
267 | fibril_mutex_lock(&mtab_list_lock);
|
---|
268 | list_append(&mtab_ent->link, &mtab_list);
|
---|
269 | mtab_size++;
|
---|
270 | fibril_mutex_unlock(&mtab_list_lock);
|
---|
271 | }
|
---|
272 |
|
---|
273 | async_answer_0(rid, rc);
|
---|
274 |
|
---|
275 | free(mp);
|
---|
276 | free(fs_name);
|
---|
277 | free(opts);
|
---|
278 | }
|
---|
279 |
|
---|
280 | void vfs_unmount_srv(ipc_callid_t rid, ipc_call_t *request)
|
---|
281 | {
|
---|
282 | /*
|
---|
283 | * Receive the mount point path.
|
---|
284 | */
|
---|
285 | char *mp;
|
---|
286 | int rc = async_data_write_accept((void **) &mp, true, 0, MAX_PATH_LEN,
|
---|
287 | 0, NULL);
|
---|
288 | if (rc != EOK)
|
---|
289 | async_answer_0(rid, rc);
|
---|
290 |
|
---|
291 | /*
|
---|
292 | * Taking the namespace lock will do two things for us. First, it will
|
---|
293 | * prevent races with other lookup operations. Second, it will stop new
|
---|
294 | * references to already existing VFS nodes and creation of new VFS
|
---|
295 | * nodes. This is because new references are added as a result of some
|
---|
296 | * lookup operation or at least of some operation which is protected by
|
---|
297 | * the namespace lock.
|
---|
298 | */
|
---|
299 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
300 |
|
---|
301 | if (str_cmp(mp, "/") == 0) {
|
---|
302 | free(mp);
|
---|
303 |
|
---|
304 | /*
|
---|
305 | * Unmounting the root file system.
|
---|
306 | *
|
---|
307 | * In this case, there is no mount point node and we send
|
---|
308 | * VFS_OUT_UNMOUNTED directly to the mounted file system.
|
---|
309 | */
|
---|
310 |
|
---|
311 | if (!root) {
|
---|
312 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
313 | async_answer_0(rid, ENOENT);
|
---|
314 | return;
|
---|
315 | }
|
---|
316 |
|
---|
317 | /*
|
---|
318 | * Count the total number of references for the mounted file system. We
|
---|
319 | * are expecting at least one, which we got when the file system was mounted.
|
---|
320 | * If we find more, it means that
|
---|
321 | * the file system cannot be gracefully unmounted at the moment because
|
---|
322 | * someone is working with it.
|
---|
323 | */
|
---|
324 | if (vfs_nodes_refcount_sum_get(root->fs_handle, root->service_id) != 1) {
|
---|
325 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
326 | async_answer_0(rid, EBUSY);
|
---|
327 | return;
|
---|
328 | }
|
---|
329 |
|
---|
330 | async_exch_t *exch = vfs_exchange_grab(root->fs_handle);
|
---|
331 | rc = async_req_1_0(exch, VFS_OUT_UNMOUNTED, root->service_id);
|
---|
332 | vfs_exchange_release(exch);
|
---|
333 |
|
---|
334 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
335 | if (rc == EOK) {
|
---|
336 | vfs_node_forget(root);
|
---|
337 | root = NULL;
|
---|
338 | }
|
---|
339 | async_answer_0(rid, rc);
|
---|
340 | return;
|
---|
341 | }
|
---|
342 |
|
---|
343 | /*
|
---|
344 | * Lookup the mounted root and instantiate it.
|
---|
345 | */
|
---|
346 | vfs_lookup_res_t mp_res;
|
---|
347 | rc = vfs_lookup_internal(root, mp, L_MP, &mp_res);
|
---|
348 | if (rc != EOK) {
|
---|
349 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
350 | free(mp);
|
---|
351 | async_answer_0(rid, rc);
|
---|
352 | return;
|
---|
353 | }
|
---|
354 | vfs_node_t *mp_node = vfs_node_get(&mp_res);
|
---|
355 | if (!mp_node) {
|
---|
356 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
357 | free(mp);
|
---|
358 | async_answer_0(rid, ENOMEM);
|
---|
359 | return;
|
---|
360 | }
|
---|
361 |
|
---|
362 | if (mp_node->mount == NULL) {
|
---|
363 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
364 | vfs_node_put(mp_node);
|
---|
365 | free(mp);
|
---|
366 | async_answer_0(rid, ENOENT);
|
---|
367 | return;
|
---|
368 | }
|
---|
369 |
|
---|
370 | /*
|
---|
371 | * Count the total number of references for the mounted file system. We
|
---|
372 | * are expecting at least one, which we got when the file system was mounted.
|
---|
373 | * If we find more, it means that
|
---|
374 | * the file system cannot be gracefully unmounted at the moment because
|
---|
375 | * someone is working with it.
|
---|
376 | */
|
---|
377 | if (vfs_nodes_refcount_sum_get(mp_node->mount->fs_handle, mp_node->mount->service_id) != 1) {
|
---|
378 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
379 | vfs_node_put(mp_node);
|
---|
380 | free(mp);
|
---|
381 | async_answer_0(rid, EBUSY);
|
---|
382 | return;
|
---|
383 | }
|
---|
384 |
|
---|
385 | /* Unmount the filesystem. */
|
---|
386 | async_exch_t *exch = vfs_exchange_grab(mp_node->mount->fs_handle);
|
---|
387 | rc = async_req_1_0(exch, VFS_OUT_UNMOUNTED, mp_node->mount->service_id);
|
---|
388 | vfs_exchange_release(exch);
|
---|
389 |
|
---|
390 | vfs_node_forget(mp_node->mount);
|
---|
391 | mp_node->mount = NULL;
|
---|
392 |
|
---|
393 | vfs_node_put(mp_node);
|
---|
394 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
395 |
|
---|
396 | fibril_mutex_lock(&mtab_list_lock);
|
---|
397 | int found = 0;
|
---|
398 |
|
---|
399 | list_foreach(mtab_list, link, mtab_ent_t, mtab_ent) {
|
---|
400 | if (str_cmp(mtab_ent->mp, mp) == 0) {
|
---|
401 | list_remove(&mtab_ent->link);
|
---|
402 | mtab_size--;
|
---|
403 | free(mtab_ent);
|
---|
404 | found = 1;
|
---|
405 | break;
|
---|
406 | }
|
---|
407 | }
|
---|
408 | assert(found);
|
---|
409 | fibril_mutex_unlock(&mtab_list_lock);
|
---|
410 |
|
---|
411 | free(mp);
|
---|
412 |
|
---|
413 | async_answer_0(rid, EOK);
|
---|
414 | return;
|
---|
415 | }
|
---|
416 |
|
---|
417 | static inline bool walk_flags_valid(int flags)
|
---|
418 | {
|
---|
419 | if ((flags&~WALK_ALL_FLAGS) != 0) {
|
---|
420 | return false;
|
---|
421 | }
|
---|
422 | if ((flags&WALK_MAY_CREATE) && (flags&WALK_MUST_CREATE)) {
|
---|
423 | return false;
|
---|
424 | }
|
---|
425 | if ((flags&WALK_REGULAR) && (flags&WALK_DIRECTORY)) {
|
---|
426 | return false;
|
---|
427 | }
|
---|
428 | if ((flags&WALK_MAY_CREATE) || (flags&WALK_MUST_CREATE)) {
|
---|
429 | if (!(flags&WALK_DIRECTORY) && !(flags&WALK_REGULAR)) {
|
---|
430 | return false;
|
---|
431 | }
|
---|
432 | }
|
---|
433 | return true;
|
---|
434 | }
|
---|
435 |
|
---|
436 | static inline int walk_lookup_flags(int flags)
|
---|
437 | {
|
---|
438 | int lflags = 0;
|
---|
439 | if (flags&WALK_MAY_CREATE || flags&WALK_MUST_CREATE) {
|
---|
440 | lflags |= L_CREATE;
|
---|
441 | }
|
---|
442 | if (flags&WALK_MUST_CREATE) {
|
---|
443 | lflags |= L_EXCLUSIVE;
|
---|
444 | }
|
---|
445 | if (flags&WALK_REGULAR) {
|
---|
446 | lflags |= L_FILE;
|
---|
447 | }
|
---|
448 | if (flags&WALK_DIRECTORY) {
|
---|
449 | lflags |= L_DIRECTORY;
|
---|
450 | }
|
---|
451 | return lflags;
|
---|
452 | }
|
---|
453 |
|
---|
454 | void vfs_walk(ipc_callid_t rid, ipc_call_t *request)
|
---|
455 | {
|
---|
456 | /*
|
---|
457 | * Parent is our relative root for file lookup.
|
---|
458 | * For defined flags, see <ipc/vfs.h>.
|
---|
459 | */
|
---|
460 | int parentfd = IPC_GET_ARG1(*request);
|
---|
461 | int flags = IPC_GET_ARG2(*request);
|
---|
462 |
|
---|
463 | if (!walk_flags_valid(flags)) {
|
---|
464 | async_answer_0(rid, EINVAL);
|
---|
465 | return;
|
---|
466 | }
|
---|
467 |
|
---|
468 | char *path;
|
---|
469 | int rc = async_data_write_accept((void **)&path, true, 0, 0, 0, NULL);
|
---|
470 |
|
---|
471 | /* Lookup the file structure corresponding to the file descriptor. */
|
---|
472 | vfs_file_t *parent = NULL;
|
---|
473 | vfs_node_t *parent_node = root;
|
---|
474 | // TODO: Client-side root.
|
---|
475 | if (parentfd != -1) {
|
---|
476 | parent = vfs_file_get(parentfd);
|
---|
477 | if (!parent) {
|
---|
478 | free(path);
|
---|
479 | async_answer_0(rid, EBADF);
|
---|
480 | return;
|
---|
481 | }
|
---|
482 | parent_node = parent->node;
|
---|
483 | }
|
---|
484 |
|
---|
485 | fibril_rwlock_read_lock(&namespace_rwlock);
|
---|
486 |
|
---|
487 | vfs_lookup_res_t lr;
|
---|
488 | rc = vfs_lookup_internal(parent_node, path, walk_lookup_flags(flags), &lr);
|
---|
489 | free(path);
|
---|
490 |
|
---|
491 | if (rc != EOK) {
|
---|
492 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
493 | if (parent) {
|
---|
494 | vfs_file_put(parent);
|
---|
495 | }
|
---|
496 | async_answer_0(rid, rc);
|
---|
497 | return;
|
---|
498 | }
|
---|
499 |
|
---|
500 | vfs_node_t *node = vfs_node_get(&lr);
|
---|
501 |
|
---|
502 | vfs_file_t *file;
|
---|
503 | int fd = vfs_fd_alloc(&file, false);
|
---|
504 | if (fd < 0) {
|
---|
505 | vfs_node_put(node);
|
---|
506 | if (parent) {
|
---|
507 | vfs_file_put(parent);
|
---|
508 | }
|
---|
509 | async_answer_0(rid, fd);
|
---|
510 | return;
|
---|
511 | }
|
---|
512 | assert(file != NULL);
|
---|
513 |
|
---|
514 | file->node = node;
|
---|
515 | if (parent) {
|
---|
516 | file->permissions = parent->permissions;
|
---|
517 | } else {
|
---|
518 | file->permissions = MODE_READ | MODE_WRITE | MODE_APPEND;
|
---|
519 | }
|
---|
520 | file->open_read = false;
|
---|
521 | file->open_write = false;
|
---|
522 |
|
---|
523 | vfs_file_put(file);
|
---|
524 | if (parent) {
|
---|
525 | vfs_file_put(parent);
|
---|
526 | }
|
---|
527 |
|
---|
528 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
529 |
|
---|
530 | async_answer_1(rid, EOK, fd);
|
---|
531 | }
|
---|
532 |
|
---|
533 | void vfs_open2(ipc_callid_t rid, ipc_call_t *request)
|
---|
534 | {
|
---|
535 | int fd = IPC_GET_ARG1(*request);
|
---|
536 | int flags = IPC_GET_ARG2(*request);
|
---|
537 |
|
---|
538 | if (flags == 0) {
|
---|
539 | async_answer_0(rid, EINVAL);
|
---|
540 | return;
|
---|
541 | }
|
---|
542 |
|
---|
543 | vfs_file_t *file = vfs_file_get(fd);
|
---|
544 | if (!file) {
|
---|
545 | async_answer_0(rid, EBADF);
|
---|
546 | return;
|
---|
547 | }
|
---|
548 |
|
---|
549 | if ((flags & ~file->permissions) != 0) {
|
---|
550 | vfs_file_put(file);
|
---|
551 | async_answer_0(rid, EPERM);
|
---|
552 | return;
|
---|
553 | }
|
---|
554 |
|
---|
555 | file->open_read = (flags & MODE_READ) != 0;
|
---|
556 | file->open_write = (flags & (MODE_WRITE | MODE_APPEND)) != 0;
|
---|
557 | file->append = (flags & MODE_APPEND) != 0;
|
---|
558 |
|
---|
559 | if (!file->open_read && !file->open_write) {
|
---|
560 | vfs_file_put(file);
|
---|
561 | async_answer_0(rid, EINVAL);
|
---|
562 | return;
|
---|
563 | }
|
---|
564 |
|
---|
565 | if (file->node->type == VFS_NODE_DIRECTORY && file->open_write) {
|
---|
566 | file->open_read = file->open_write = false;
|
---|
567 | vfs_file_put(file);
|
---|
568 | async_answer_0(rid, EINVAL);
|
---|
569 | return;
|
---|
570 | }
|
---|
571 |
|
---|
572 | int rc = vfs_open_node_remote(file->node);
|
---|
573 | if (rc != EOK) {
|
---|
574 | file->open_read = file->open_write = false;
|
---|
575 | vfs_file_put(file);
|
---|
576 | async_answer_0(rid, rc);
|
---|
577 | return;
|
---|
578 | }
|
---|
579 |
|
---|
580 | vfs_file_put(file);
|
---|
581 | async_answer_0(rid, EOK);
|
---|
582 | }
|
---|
583 |
|
---|
584 | void vfs_sync(ipc_callid_t rid, ipc_call_t *request)
|
---|
585 | {
|
---|
586 | int fd = IPC_GET_ARG1(*request);
|
---|
587 |
|
---|
588 | /* Lookup the file structure corresponding to the file descriptor. */
|
---|
589 | vfs_file_t *file = vfs_file_get(fd);
|
---|
590 | if (!file) {
|
---|
591 | async_answer_0(rid, ENOENT);
|
---|
592 | return;
|
---|
593 | }
|
---|
594 |
|
---|
595 | /*
|
---|
596 | * Lock the open file structure so that no other thread can manipulate
|
---|
597 | * the same open file at a time.
|
---|
598 | */
|
---|
599 | async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
|
---|
600 |
|
---|
601 | /* Make a VFS_OUT_SYMC request at the destination FS server. */
|
---|
602 | aid_t msg;
|
---|
603 | ipc_call_t answer;
|
---|
604 | msg = async_send_2(fs_exch, VFS_OUT_SYNC, file->node->service_id,
|
---|
605 | file->node->index, &answer);
|
---|
606 |
|
---|
607 | vfs_exchange_release(fs_exch);
|
---|
608 |
|
---|
609 | /* Wait for reply from the FS server. */
|
---|
610 | sysarg_t rc;
|
---|
611 | async_wait_for(msg, &rc);
|
---|
612 |
|
---|
613 | vfs_file_put(file);
|
---|
614 | async_answer_0(rid, rc);
|
---|
615 | }
|
---|
616 |
|
---|
617 | void vfs_close(ipc_callid_t rid, ipc_call_t *request)
|
---|
618 | {
|
---|
619 | int fd = IPC_GET_ARG1(*request);
|
---|
620 | int ret = vfs_fd_free(fd);
|
---|
621 | async_answer_0(rid, ret);
|
---|
622 | }
|
---|
623 |
|
---|
624 | typedef int (* rdwr_ipc_cb_t)(async_exch_t *, vfs_file_t *, ipc_call_t *,
|
---|
625 | bool, void *);
|
---|
626 |
|
---|
627 | static int rdwr_ipc_client(async_exch_t *exch, vfs_file_t *file,
|
---|
628 | ipc_call_t *answer, bool read, void *data)
|
---|
629 | {
|
---|
630 | size_t *bytes = (size_t *) data;
|
---|
631 | int rc;
|
---|
632 |
|
---|
633 | /*
|
---|
634 | * Make a VFS_READ/VFS_WRITE request at the destination FS server
|
---|
635 | * and forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
|
---|
636 | * destination FS server. The call will be routed as if sent by
|
---|
637 | * ourselves. Note that call arguments are immutable in this case so we
|
---|
638 | * don't have to bother.
|
---|
639 | */
|
---|
640 |
|
---|
641 | if (read) {
|
---|
642 | rc = async_data_read_forward_4_1(exch, VFS_OUT_READ,
|
---|
643 | file->node->service_id, file->node->index,
|
---|
644 | LOWER32(file->pos), UPPER32(file->pos), answer);
|
---|
645 | } else {
|
---|
646 | rc = async_data_write_forward_4_1(exch, VFS_OUT_WRITE,
|
---|
647 | file->node->service_id, file->node->index,
|
---|
648 | LOWER32(file->pos), UPPER32(file->pos), answer);
|
---|
649 | }
|
---|
650 |
|
---|
651 | *bytes = IPC_GET_ARG1(*answer);
|
---|
652 | return rc;
|
---|
653 | }
|
---|
654 |
|
---|
655 | static int rdwr_ipc_internal(async_exch_t *exch, vfs_file_t *file,
|
---|
656 | ipc_call_t *answer, bool read, void *data)
|
---|
657 | {
|
---|
658 | rdwr_io_chunk_t *chunk = (rdwr_io_chunk_t *) data;
|
---|
659 |
|
---|
660 | if (exch == NULL)
|
---|
661 | return ENOENT;
|
---|
662 |
|
---|
663 | aid_t msg = async_send_fast(exch, read ? VFS_OUT_READ : VFS_OUT_WRITE,
|
---|
664 | file->node->service_id, file->node->index, LOWER32(file->pos),
|
---|
665 | UPPER32(file->pos), answer);
|
---|
666 | if (msg == 0)
|
---|
667 | return EINVAL;
|
---|
668 |
|
---|
669 | int retval = async_data_read_start(exch, chunk->buffer, chunk->size);
|
---|
670 | if (retval != EOK) {
|
---|
671 | async_forget(msg);
|
---|
672 | return retval;
|
---|
673 | }
|
---|
674 |
|
---|
675 | sysarg_t rc;
|
---|
676 | async_wait_for(msg, &rc);
|
---|
677 |
|
---|
678 | chunk->size = IPC_GET_ARG1(*answer);
|
---|
679 |
|
---|
680 | return (int) rc;
|
---|
681 | }
|
---|
682 |
|
---|
683 | static int vfs_rdwr(int fd, bool read, rdwr_ipc_cb_t ipc_cb, void *ipc_cb_data)
|
---|
684 | {
|
---|
685 | /*
|
---|
686 | * The following code strongly depends on the fact that the files data
|
---|
687 | * structure can be only accessed by a single fibril and all file
|
---|
688 | * operations are serialized (i.e. the reads and writes cannot
|
---|
689 | * interleave and a file cannot be closed while it is being read).
|
---|
690 | *
|
---|
691 | * Additional synchronization needs to be added once the table of
|
---|
692 | * open files supports parallel access!
|
---|
693 | */
|
---|
694 |
|
---|
695 | /* Lookup the file structure corresponding to the file descriptor. */
|
---|
696 | vfs_file_t *file = vfs_file_get(fd);
|
---|
697 | if (!file)
|
---|
698 | return EBADF;
|
---|
699 |
|
---|
700 | if ((read && !file->open_read) || (!read && !file->open_write)) {
|
---|
701 | vfs_file_put(file);
|
---|
702 | return EINVAL;
|
---|
703 | }
|
---|
704 |
|
---|
705 | vfs_info_t *fs_info = fs_handle_to_info(file->node->fs_handle);
|
---|
706 | assert(fs_info);
|
---|
707 |
|
---|
708 | bool rlock = read || ((fs_info->concurrent_read_write) && (fs_info->write_retains_size));
|
---|
709 |
|
---|
710 | /*
|
---|
711 | * Lock the file's node so that no other client can read/write to it at
|
---|
712 | * the same time unless the FS supports concurrent reads/writes and its
|
---|
713 | * write implementation does not modify the file size.
|
---|
714 | */
|
---|
715 | if (rlock) {
|
---|
716 | fibril_rwlock_read_lock(&file->node->contents_rwlock);
|
---|
717 | } else {
|
---|
718 | fibril_rwlock_write_lock(&file->node->contents_rwlock);
|
---|
719 | }
|
---|
720 |
|
---|
721 | if (file->node->type == VFS_NODE_DIRECTORY) {
|
---|
722 | /*
|
---|
723 | * Make sure that no one is modifying the namespace
|
---|
724 | * while we are in readdir().
|
---|
725 | */
|
---|
726 |
|
---|
727 | if (!read) {
|
---|
728 | if (rlock) {
|
---|
729 | fibril_rwlock_read_unlock(&file->node->contents_rwlock);
|
---|
730 | } else {
|
---|
731 | fibril_rwlock_write_unlock(&file->node->contents_rwlock);
|
---|
732 | }
|
---|
733 | vfs_file_put(file);
|
---|
734 | return EINVAL;
|
---|
735 | }
|
---|
736 |
|
---|
737 | fibril_rwlock_read_lock(&namespace_rwlock);
|
---|
738 | }
|
---|
739 |
|
---|
740 | async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
|
---|
741 |
|
---|
742 | if (!read && file->append)
|
---|
743 | file->pos = file->node->size;
|
---|
744 |
|
---|
745 | /*
|
---|
746 | * Handle communication with the endpoint FS.
|
---|
747 | */
|
---|
748 | ipc_call_t answer;
|
---|
749 | int rc = ipc_cb(fs_exch, file, &answer, read, ipc_cb_data);
|
---|
750 |
|
---|
751 | vfs_exchange_release(fs_exch);
|
---|
752 |
|
---|
753 | size_t bytes = IPC_GET_ARG1(answer);
|
---|
754 |
|
---|
755 | if (file->node->type == VFS_NODE_DIRECTORY) {
|
---|
756 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
757 | }
|
---|
758 |
|
---|
759 | /* Unlock the VFS node. */
|
---|
760 | if (rlock) {
|
---|
761 | fibril_rwlock_read_unlock(&file->node->contents_rwlock);
|
---|
762 | } else {
|
---|
763 | /* Update the cached version of node's size. */
|
---|
764 | if (rc == EOK) {
|
---|
765 | file->node->size = MERGE_LOUP32(IPC_GET_ARG2(answer),
|
---|
766 | IPC_GET_ARG3(answer));
|
---|
767 | }
|
---|
768 | fibril_rwlock_write_unlock(&file->node->contents_rwlock);
|
---|
769 | }
|
---|
770 |
|
---|
771 | /* Update the position pointer and unlock the open file. */
|
---|
772 | if (rc == EOK) {
|
---|
773 | file->pos += bytes;
|
---|
774 | }
|
---|
775 | vfs_file_put(file);
|
---|
776 |
|
---|
777 | return rc;
|
---|
778 | }
|
---|
779 |
|
---|
780 | static void vfs_rdwr_client(ipc_callid_t rid, ipc_call_t *request, bool read)
|
---|
781 | {
|
---|
782 | size_t bytes = 0;
|
---|
783 | int rc = vfs_rdwr(IPC_GET_ARG1(*request), read, rdwr_ipc_client,
|
---|
784 | &bytes);
|
---|
785 | async_answer_1(rid, rc, bytes);
|
---|
786 | }
|
---|
787 |
|
---|
788 | int vfs_rdwr_internal(int fd, bool read, rdwr_io_chunk_t *chunk)
|
---|
789 | {
|
---|
790 | return vfs_rdwr(fd, read, rdwr_ipc_internal, chunk);
|
---|
791 | }
|
---|
792 |
|
---|
793 | void vfs_read(ipc_callid_t rid, ipc_call_t *request)
|
---|
794 | {
|
---|
795 | vfs_rdwr_client(rid, request, true);
|
---|
796 | }
|
---|
797 |
|
---|
798 | void vfs_write(ipc_callid_t rid, ipc_call_t *request)
|
---|
799 | {
|
---|
800 | vfs_rdwr_client(rid, request, false);
|
---|
801 | }
|
---|
802 |
|
---|
803 | void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
|
---|
804 | {
|
---|
805 | int fd = (int) IPC_GET_ARG1(*request);
|
---|
806 | off64_t off = (off64_t) MERGE_LOUP32(IPC_GET_ARG2(*request),
|
---|
807 | IPC_GET_ARG3(*request));
|
---|
808 | int whence = (int) IPC_GET_ARG4(*request);
|
---|
809 |
|
---|
810 | /* Lookup the file structure corresponding to the file descriptor. */
|
---|
811 | vfs_file_t *file = vfs_file_get(fd);
|
---|
812 | if (!file) {
|
---|
813 | async_answer_0(rid, ENOENT);
|
---|
814 | return;
|
---|
815 | }
|
---|
816 |
|
---|
817 | off64_t newoff;
|
---|
818 | switch (whence) {
|
---|
819 | case SEEK_SET:
|
---|
820 | if (off >= 0) {
|
---|
821 | file->pos = (aoff64_t) off;
|
---|
822 | vfs_file_put(file);
|
---|
823 | async_answer_1(rid, EOK, off);
|
---|
824 | return;
|
---|
825 | }
|
---|
826 | break;
|
---|
827 | case SEEK_CUR:
|
---|
828 | if ((off >= 0) && (file->pos + off < file->pos)) {
|
---|
829 | vfs_file_put(file);
|
---|
830 | async_answer_0(rid, EOVERFLOW);
|
---|
831 | return;
|
---|
832 | }
|
---|
833 |
|
---|
834 | if ((off < 0) && (file->pos < (aoff64_t) -off)) {
|
---|
835 | vfs_file_put(file);
|
---|
836 | async_answer_0(rid, EOVERFLOW);
|
---|
837 | return;
|
---|
838 | }
|
---|
839 |
|
---|
840 | file->pos += off;
|
---|
841 | newoff = (file->pos > OFF64_MAX) ? OFF64_MAX : file->pos;
|
---|
842 |
|
---|
843 | vfs_file_put(file);
|
---|
844 | async_answer_2(rid, EOK, LOWER32(newoff),
|
---|
845 | UPPER32(newoff));
|
---|
846 | return;
|
---|
847 | case SEEK_END:
|
---|
848 | fibril_rwlock_read_lock(&file->node->contents_rwlock);
|
---|
849 | aoff64_t size = vfs_node_get_size(file->node);
|
---|
850 |
|
---|
851 | if ((off >= 0) && (size + off < size)) {
|
---|
852 | fibril_rwlock_read_unlock(&file->node->contents_rwlock);
|
---|
853 | vfs_file_put(file);
|
---|
854 | async_answer_0(rid, EOVERFLOW);
|
---|
855 | return;
|
---|
856 | }
|
---|
857 |
|
---|
858 | if ((off < 0) && (size < (aoff64_t) -off)) {
|
---|
859 | fibril_rwlock_read_unlock(&file->node->contents_rwlock);
|
---|
860 | vfs_file_put(file);
|
---|
861 | async_answer_0(rid, EOVERFLOW);
|
---|
862 | return;
|
---|
863 | }
|
---|
864 |
|
---|
865 | file->pos = size + off;
|
---|
866 | newoff = (file->pos > OFF64_MAX) ? OFF64_MAX : file->pos;
|
---|
867 |
|
---|
868 | fibril_rwlock_read_unlock(&file->node->contents_rwlock);
|
---|
869 | vfs_file_put(file);
|
---|
870 | async_answer_2(rid, EOK, LOWER32(newoff), UPPER32(newoff));
|
---|
871 | return;
|
---|
872 | }
|
---|
873 |
|
---|
874 | vfs_file_put(file);
|
---|
875 | async_answer_0(rid, EINVAL);
|
---|
876 | }
|
---|
877 |
|
---|
878 | int vfs_truncate_internal(fs_handle_t fs_handle, service_id_t service_id,
|
---|
879 | fs_index_t index, aoff64_t size)
|
---|
880 | {
|
---|
881 | async_exch_t *exch = vfs_exchange_grab(fs_handle);
|
---|
882 | sysarg_t rc = async_req_4_0(exch, VFS_OUT_TRUNCATE,
|
---|
883 | (sysarg_t) service_id, (sysarg_t) index, LOWER32(size),
|
---|
884 | UPPER32(size));
|
---|
885 | vfs_exchange_release(exch);
|
---|
886 |
|
---|
887 | return (int) rc;
|
---|
888 | }
|
---|
889 |
|
---|
890 | void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
|
---|
891 | {
|
---|
892 | int fd = IPC_GET_ARG1(*request);
|
---|
893 | aoff64_t size = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG2(*request),
|
---|
894 | IPC_GET_ARG3(*request));
|
---|
895 | int rc;
|
---|
896 |
|
---|
897 | vfs_file_t *file = vfs_file_get(fd);
|
---|
898 | if (!file) {
|
---|
899 | async_answer_0(rid, ENOENT);
|
---|
900 | return;
|
---|
901 | }
|
---|
902 |
|
---|
903 | fibril_rwlock_write_lock(&file->node->contents_rwlock);
|
---|
904 | rc = vfs_truncate_internal(file->node->fs_handle,
|
---|
905 | file->node->service_id, file->node->index, size);
|
---|
906 | if (rc == EOK)
|
---|
907 | file->node->size = size;
|
---|
908 | fibril_rwlock_write_unlock(&file->node->contents_rwlock);
|
---|
909 |
|
---|
910 | vfs_file_put(file);
|
---|
911 | async_answer_0(rid, (sysarg_t)rc);
|
---|
912 | }
|
---|
913 |
|
---|
914 | void vfs_fstat(ipc_callid_t rid, ipc_call_t *request)
|
---|
915 | {
|
---|
916 | int fd = IPC_GET_ARG1(*request);
|
---|
917 | sysarg_t rc;
|
---|
918 |
|
---|
919 | vfs_file_t *file = vfs_file_get(fd);
|
---|
920 | if (!file) {
|
---|
921 | async_answer_0(rid, ENOENT);
|
---|
922 | return;
|
---|
923 | }
|
---|
924 | assert(file->node);
|
---|
925 |
|
---|
926 | ipc_callid_t callid;
|
---|
927 | if (!async_data_read_receive(&callid, NULL)) {
|
---|
928 | vfs_file_put(file);
|
---|
929 | async_answer_0(callid, EINVAL);
|
---|
930 | async_answer_0(rid, EINVAL);
|
---|
931 | return;
|
---|
932 | }
|
---|
933 |
|
---|
934 | async_exch_t *exch = vfs_exchange_grab(file->node->fs_handle);
|
---|
935 | assert(exch);
|
---|
936 |
|
---|
937 | aid_t msg;
|
---|
938 | msg = async_send_3(exch, VFS_OUT_STAT, file->node->service_id,
|
---|
939 | file->node->index, true, NULL);
|
---|
940 | assert(msg);
|
---|
941 | async_forward_fast(callid, exch, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
|
---|
942 |
|
---|
943 | vfs_exchange_release(exch);
|
---|
944 |
|
---|
945 | async_wait_for(msg, &rc);
|
---|
946 |
|
---|
947 | vfs_file_put(file);
|
---|
948 | async_answer_0(rid, rc);
|
---|
949 | }
|
---|
950 |
|
---|
951 | static void out_destroy(vfs_triplet_t *file)
|
---|
952 | {
|
---|
953 | async_exch_t *exch = vfs_exchange_grab(file->fs_handle);
|
---|
954 | async_msg_2(exch, VFS_OUT_DESTROY,
|
---|
955 | (sysarg_t) file->service_id, (sysarg_t) file->index);
|
---|
956 | vfs_exchange_release(exch);
|
---|
957 | }
|
---|
958 |
|
---|
959 | void vfs_unlink2(ipc_callid_t rid, ipc_call_t *request)
|
---|
960 | {
|
---|
961 | int rc;
|
---|
962 | char *path;
|
---|
963 | vfs_file_t *parent = NULL;
|
---|
964 | vfs_file_t *expect = NULL;
|
---|
965 | vfs_node_t *parent_node = root;
|
---|
966 |
|
---|
967 | int parentfd = IPC_GET_ARG1(*request);
|
---|
968 | int expectfd = IPC_GET_ARG2(*request);
|
---|
969 | int wflag = IPC_GET_ARG3(*request);
|
---|
970 |
|
---|
971 | rc = async_data_write_accept((void **) &path, true, 0, 0, 0, NULL);
|
---|
972 | if (rc != EOK) {
|
---|
973 | async_answer_0(rid, rc);
|
---|
974 | return;
|
---|
975 | }
|
---|
976 |
|
---|
977 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
978 |
|
---|
979 | int lflag = (wflag&WALK_DIRECTORY) ? L_DIRECTORY: 0;
|
---|
980 |
|
---|
981 | /* Files are retrieved in order of file descriptors, to prevent deadlock. */
|
---|
982 | if (parentfd >= 0 && parentfd < expectfd) {
|
---|
983 | parent = vfs_file_get(parentfd);
|
---|
984 | if (!parent) {
|
---|
985 | rc = ENOENT;
|
---|
986 | goto exit;
|
---|
987 | }
|
---|
988 | }
|
---|
989 |
|
---|
990 | if (expectfd >= 0) {
|
---|
991 | expect = vfs_file_get(expectfd);
|
---|
992 | if (!expect) {
|
---|
993 | rc = ENOENT;
|
---|
994 | goto exit;
|
---|
995 | }
|
---|
996 | }
|
---|
997 |
|
---|
998 | if (parentfd >= 0 && parentfd >= expectfd) {
|
---|
999 | parent = vfs_file_get(parentfd);
|
---|
1000 | if (!parent) {
|
---|
1001 | rc = ENOENT;
|
---|
1002 | goto exit;
|
---|
1003 | }
|
---|
1004 | }
|
---|
1005 |
|
---|
1006 | if (parent) {
|
---|
1007 | parent_node = parent->node;
|
---|
1008 | }
|
---|
1009 |
|
---|
1010 | if (expectfd >= 0) {
|
---|
1011 | vfs_lookup_res_t lr;
|
---|
1012 | rc = vfs_lookup_internal(parent_node, path, lflag, &lr);
|
---|
1013 | if (rc != EOK) {
|
---|
1014 | goto exit;
|
---|
1015 | }
|
---|
1016 |
|
---|
1017 | if (__builtin_memcmp(&lr.triplet, expect->node, sizeof(vfs_triplet_t)) != 0) {
|
---|
1018 | rc = ENOENT;
|
---|
1019 | goto exit;
|
---|
1020 | }
|
---|
1021 |
|
---|
1022 | vfs_file_put(expect);
|
---|
1023 | expect = NULL;
|
---|
1024 | }
|
---|
1025 |
|
---|
1026 | vfs_lookup_res_t lr;
|
---|
1027 | rc = vfs_lookup_internal(parent_node, path, lflag | L_UNLINK, &lr);
|
---|
1028 | if (rc != EOK) {
|
---|
1029 | goto exit;
|
---|
1030 | }
|
---|
1031 |
|
---|
1032 | /* If the node is not held by anyone, try to destroy it. */
|
---|
1033 | if (vfs_node_peek(&lr) == NULL) {
|
---|
1034 | out_destroy(&lr.triplet);
|
---|
1035 | }
|
---|
1036 |
|
---|
1037 | exit:
|
---|
1038 | if (path) {
|
---|
1039 | free(path);
|
---|
1040 | }
|
---|
1041 | if (parent) {
|
---|
1042 | vfs_file_put(parent);
|
---|
1043 | }
|
---|
1044 | if (expect) {
|
---|
1045 | vfs_file_put(expect);
|
---|
1046 | }
|
---|
1047 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1048 | async_answer_0(rid, rc);
|
---|
1049 | }
|
---|
1050 |
|
---|
1051 | static size_t shared_path(char *a, char *b)
|
---|
1052 | {
|
---|
1053 | size_t res = 0;
|
---|
1054 |
|
---|
1055 | while (a[res] == b[res] && a[res] != 0) {
|
---|
1056 | res++;
|
---|
1057 | }
|
---|
1058 |
|
---|
1059 | if (a[res] == b[res]) {
|
---|
1060 | return res;
|
---|
1061 | }
|
---|
1062 |
|
---|
1063 | res--;
|
---|
1064 | while (a[res] != '/') {
|
---|
1065 | res--;
|
---|
1066 | }
|
---|
1067 | return res;
|
---|
1068 | }
|
---|
1069 |
|
---|
1070 | static int vfs_rename_internal(vfs_node_t *base, char *old, char *new)
|
---|
1071 | {
|
---|
1072 | assert(base != NULL);
|
---|
1073 | assert(old != NULL);
|
---|
1074 | assert(new != NULL);
|
---|
1075 |
|
---|
1076 | vfs_lookup_res_t base_lr;
|
---|
1077 | vfs_lookup_res_t old_lr;
|
---|
1078 | vfs_lookup_res_t new_lr_orig;
|
---|
1079 | bool orig_unlinked = false;
|
---|
1080 |
|
---|
1081 | int rc;
|
---|
1082 |
|
---|
1083 | size_t shared = shared_path(old, new);
|
---|
1084 |
|
---|
1085 | /* Do not allow one path to be a prefix of the other. */
|
---|
1086 | if (old[shared] == 0 || new[shared] == 0) {
|
---|
1087 | return EINVAL;
|
---|
1088 | }
|
---|
1089 | assert(old[shared] == '/');
|
---|
1090 | assert(new[shared] == '/');
|
---|
1091 |
|
---|
1092 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
1093 |
|
---|
1094 | /* Resolve the shared portion of the path first. */
|
---|
1095 | if (shared != 0) {
|
---|
1096 | old[shared] = 0;
|
---|
1097 | rc = vfs_lookup_internal(base, old, L_DIRECTORY, &base_lr);
|
---|
1098 | if (rc != EOK) {
|
---|
1099 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1100 | return rc;
|
---|
1101 | }
|
---|
1102 |
|
---|
1103 | base = vfs_node_get(&base_lr);
|
---|
1104 | old[shared] = '/';
|
---|
1105 | old += shared;
|
---|
1106 | new += shared;
|
---|
1107 | } else {
|
---|
1108 | vfs_node_addref(base);
|
---|
1109 | }
|
---|
1110 |
|
---|
1111 |
|
---|
1112 | rc = vfs_lookup_internal(base, new, L_UNLINK | L_DISABLE_MOUNTS, &new_lr_orig);
|
---|
1113 | if (rc == EOK) {
|
---|
1114 | orig_unlinked = true;
|
---|
1115 | } else if (rc != ENOENT) {
|
---|
1116 | vfs_node_put(base);
|
---|
1117 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1118 | return rc;
|
---|
1119 | }
|
---|
1120 |
|
---|
1121 | rc = vfs_lookup_internal(base, old, L_UNLINK | L_DISABLE_MOUNTS, &old_lr);
|
---|
1122 | if (rc != EOK) {
|
---|
1123 | if (orig_unlinked) {
|
---|
1124 | vfs_link_internal(base, new, &new_lr_orig.triplet);
|
---|
1125 | }
|
---|
1126 | vfs_node_put(base);
|
---|
1127 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1128 | return rc;
|
---|
1129 | }
|
---|
1130 |
|
---|
1131 | rc = vfs_link_internal(base, new, &old_lr.triplet);
|
---|
1132 | if (rc != EOK) {
|
---|
1133 | vfs_link_internal(base, old, &old_lr.triplet);
|
---|
1134 | if (orig_unlinked) {
|
---|
1135 | vfs_link_internal(base, new, &new_lr_orig.triplet);
|
---|
1136 | }
|
---|
1137 | vfs_node_put(base);
|
---|
1138 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1139 | return rc;
|
---|
1140 | }
|
---|
1141 |
|
---|
1142 | /* If the node is not held by anyone, try to destroy it. */
|
---|
1143 | if (orig_unlinked && vfs_node_peek(&new_lr_orig) == NULL) {
|
---|
1144 | out_destroy(&new_lr_orig.triplet);
|
---|
1145 | }
|
---|
1146 |
|
---|
1147 | vfs_node_put(base);
|
---|
1148 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1149 | return EOK;
|
---|
1150 | }
|
---|
1151 |
|
---|
1152 | void vfs_rename(ipc_callid_t rid, ipc_call_t *request)
|
---|
1153 | {
|
---|
1154 | /* The common base directory. */
|
---|
1155 | int basefd;
|
---|
1156 | char *old = NULL;
|
---|
1157 | char *new = NULL;
|
---|
1158 | vfs_file_t *base = NULL;
|
---|
1159 | int rc;
|
---|
1160 |
|
---|
1161 | basefd = IPC_GET_ARG1(*request);
|
---|
1162 |
|
---|
1163 | /* Retrieve the old path. */
|
---|
1164 | rc = async_data_write_accept((void **) &old, true, 0, 0, 0, NULL);
|
---|
1165 | if (rc != EOK) {
|
---|
1166 | goto out;
|
---|
1167 | }
|
---|
1168 |
|
---|
1169 | /* Retrieve the new path. */
|
---|
1170 | rc = async_data_write_accept((void **) &new, true, 0, 0, 0, NULL);
|
---|
1171 | if (rc != EOK) {
|
---|
1172 | goto out;
|
---|
1173 | }
|
---|
1174 |
|
---|
1175 | size_t olen;
|
---|
1176 | size_t nlen;
|
---|
1177 | char *oldc = canonify(old, &olen);
|
---|
1178 | char *newc = canonify(new, &nlen);
|
---|
1179 |
|
---|
1180 | if ((!oldc) || (!newc)) {
|
---|
1181 | rc = EINVAL;
|
---|
1182 | goto out;
|
---|
1183 | }
|
---|
1184 |
|
---|
1185 | assert(oldc[olen] == '\0');
|
---|
1186 | assert(newc[nlen] == '\0');
|
---|
1187 |
|
---|
1188 | /* Lookup the file structure corresponding to the file descriptor. */
|
---|
1189 | vfs_node_t *base_node = root;
|
---|
1190 | // TODO: Client-side root.
|
---|
1191 | if (basefd != -1) {
|
---|
1192 | base = vfs_file_get(basefd);
|
---|
1193 | if (!base) {
|
---|
1194 | rc = EBADF;
|
---|
1195 | goto out;
|
---|
1196 | }
|
---|
1197 | base_node = base->node;
|
---|
1198 | }
|
---|
1199 |
|
---|
1200 | rc = vfs_rename_internal(base_node, oldc, newc);
|
---|
1201 |
|
---|
1202 | out:
|
---|
1203 | async_answer_0(rid, rc);
|
---|
1204 |
|
---|
1205 | if (old) {
|
---|
1206 | free(old);
|
---|
1207 | }
|
---|
1208 | if (new) {
|
---|
1209 | free(new);
|
---|
1210 | }
|
---|
1211 | if (base) {
|
---|
1212 | vfs_file_put(base);
|
---|
1213 | }
|
---|
1214 | }
|
---|
1215 |
|
---|
1216 | void vfs_dup(ipc_callid_t rid, ipc_call_t *request)
|
---|
1217 | {
|
---|
1218 | int oldfd = IPC_GET_ARG1(*request);
|
---|
1219 | int newfd = IPC_GET_ARG2(*request);
|
---|
1220 |
|
---|
1221 | /* If the file descriptors are the same, do nothing. */
|
---|
1222 | if (oldfd == newfd) {
|
---|
1223 | async_answer_1(rid, EOK, newfd);
|
---|
1224 | return;
|
---|
1225 | }
|
---|
1226 |
|
---|
1227 | /* Lookup the file structure corresponding to oldfd. */
|
---|
1228 | vfs_file_t *oldfile = vfs_file_get(oldfd);
|
---|
1229 | if (!oldfile) {
|
---|
1230 | async_answer_0(rid, EBADF);
|
---|
1231 | return;
|
---|
1232 | }
|
---|
1233 |
|
---|
1234 | /* Make sure newfd is closed. */
|
---|
1235 | (void) vfs_fd_free(newfd);
|
---|
1236 |
|
---|
1237 | /* Assign the old file to newfd. */
|
---|
1238 | int ret = vfs_fd_assign(oldfile, newfd);
|
---|
1239 | vfs_file_put(oldfile);
|
---|
1240 |
|
---|
1241 | if (ret != EOK)
|
---|
1242 | async_answer_0(rid, ret);
|
---|
1243 | else
|
---|
1244 | async_answer_1(rid, EOK, newfd);
|
---|
1245 | }
|
---|
1246 |
|
---|
1247 | void vfs_wait_handle(ipc_callid_t rid, ipc_call_t *request)
|
---|
1248 | {
|
---|
1249 | int fd = vfs_wait_handle_internal();
|
---|
1250 | async_answer_1(rid, EOK, fd);
|
---|
1251 | }
|
---|
1252 |
|
---|
1253 | void vfs_get_mtab(ipc_callid_t rid, ipc_call_t *request)
|
---|
1254 | {
|
---|
1255 | ipc_callid_t callid;
|
---|
1256 | ipc_call_t data;
|
---|
1257 | sysarg_t rc = EOK;
|
---|
1258 | size_t len;
|
---|
1259 |
|
---|
1260 | fibril_mutex_lock(&mtab_list_lock);
|
---|
1261 |
|
---|
1262 | /* Send to the caller the number of mounted filesystems */
|
---|
1263 | callid = async_get_call(&data);
|
---|
1264 | if (IPC_GET_IMETHOD(data) != VFS_IN_PING) {
|
---|
1265 | rc = ENOTSUP;
|
---|
1266 | async_answer_0(callid, rc);
|
---|
1267 | goto exit;
|
---|
1268 | }
|
---|
1269 | async_answer_1(callid, EOK, mtab_size);
|
---|
1270 |
|
---|
1271 | list_foreach(mtab_list, link, mtab_ent_t, mtab_ent) {
|
---|
1272 | rc = ENOTSUP;
|
---|
1273 |
|
---|
1274 | if (!async_data_read_receive(&callid, &len)) {
|
---|
1275 | async_answer_0(callid, rc);
|
---|
1276 | goto exit;
|
---|
1277 | }
|
---|
1278 |
|
---|
1279 | (void) async_data_read_finalize(callid, mtab_ent->mp,
|
---|
1280 | str_size(mtab_ent->mp));
|
---|
1281 |
|
---|
1282 | if (!async_data_read_receive(&callid, &len)) {
|
---|
1283 | async_answer_0(callid, rc);
|
---|
1284 | goto exit;
|
---|
1285 | }
|
---|
1286 |
|
---|
1287 | (void) async_data_read_finalize(callid, mtab_ent->opts,
|
---|
1288 | str_size(mtab_ent->opts));
|
---|
1289 |
|
---|
1290 | if (!async_data_read_receive(&callid, &len)) {
|
---|
1291 | async_answer_0(callid, rc);
|
---|
1292 | goto exit;
|
---|
1293 | }
|
---|
1294 |
|
---|
1295 | (void) async_data_read_finalize(callid, mtab_ent->fs_name,
|
---|
1296 | str_size(mtab_ent->fs_name));
|
---|
1297 |
|
---|
1298 | callid = async_get_call(&data);
|
---|
1299 |
|
---|
1300 | if (IPC_GET_IMETHOD(data) != VFS_IN_PING) {
|
---|
1301 | async_answer_0(callid, rc);
|
---|
1302 | goto exit;
|
---|
1303 | }
|
---|
1304 |
|
---|
1305 | rc = EOK;
|
---|
1306 | async_answer_2(callid, rc, mtab_ent->instance,
|
---|
1307 | mtab_ent->service_id);
|
---|
1308 | }
|
---|
1309 |
|
---|
1310 | exit:
|
---|
1311 | fibril_mutex_unlock(&mtab_list_lock);
|
---|
1312 | async_answer_0(rid, rc);
|
---|
1313 | }
|
---|
1314 |
|
---|
1315 | void vfs_statfs(ipc_callid_t rid, ipc_call_t *request)
|
---|
1316 | {
|
---|
1317 | char *path;
|
---|
1318 | int rc = async_data_write_accept((void **) &path, true, 0, 0, 0, NULL);
|
---|
1319 | if (rc != EOK) {
|
---|
1320 | async_answer_0(rid, rc);
|
---|
1321 | return;
|
---|
1322 | }
|
---|
1323 |
|
---|
1324 | ipc_callid_t callid;
|
---|
1325 | if (!async_data_read_receive(&callid, NULL)) {
|
---|
1326 | free(path);
|
---|
1327 | async_answer_0(callid, EINVAL);
|
---|
1328 | async_answer_0(rid, EINVAL);
|
---|
1329 | return;
|
---|
1330 | }
|
---|
1331 |
|
---|
1332 | vfs_lookup_res_t lr;
|
---|
1333 | fibril_rwlock_read_lock(&namespace_rwlock);
|
---|
1334 | rc = vfs_lookup_internal(root, path, L_NONE, &lr);
|
---|
1335 | free(path);
|
---|
1336 | if (rc != EOK) {
|
---|
1337 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
1338 | async_answer_0(callid, rc);
|
---|
1339 | async_answer_0(rid, rc);
|
---|
1340 | return;
|
---|
1341 | }
|
---|
1342 | vfs_node_t *node = vfs_node_get(&lr);
|
---|
1343 | if (!node) {
|
---|
1344 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
1345 | async_answer_0(callid, ENOMEM);
|
---|
1346 | async_answer_0(rid, ENOMEM);
|
---|
1347 | return;
|
---|
1348 | }
|
---|
1349 |
|
---|
1350 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
1351 |
|
---|
1352 | async_exch_t *exch = vfs_exchange_grab(node->fs_handle);
|
---|
1353 |
|
---|
1354 | aid_t msg;
|
---|
1355 | msg = async_send_3(exch, VFS_OUT_STATFS, node->service_id,
|
---|
1356 | node->index, false, NULL);
|
---|
1357 | async_forward_fast(callid, exch, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
|
---|
1358 |
|
---|
1359 | vfs_exchange_release(exch);
|
---|
1360 |
|
---|
1361 | sysarg_t rv;
|
---|
1362 | async_wait_for(msg, &rv);
|
---|
1363 |
|
---|
1364 | async_answer_0(rid, rv);
|
---|
1365 |
|
---|
1366 | vfs_node_put(node);
|
---|
1367 | }
|
---|
1368 |
|
---|
1369 | void vfs_op_clone(ipc_callid_t rid, ipc_call_t *request)
|
---|
1370 | {
|
---|
1371 | int oldfd = IPC_GET_ARG1(*request);
|
---|
1372 | bool desc = IPC_GET_ARG2(*request);
|
---|
1373 |
|
---|
1374 | /* Lookup the file structure corresponding to fd. */
|
---|
1375 | vfs_file_t *oldfile = vfs_file_get(oldfd);
|
---|
1376 | if (!oldfile) {
|
---|
1377 | async_answer_0(rid, EBADF);
|
---|
1378 | return;
|
---|
1379 | }
|
---|
1380 |
|
---|
1381 | vfs_file_t *newfile;
|
---|
1382 | int newfd = vfs_fd_alloc(&newfile, desc);
|
---|
1383 | if (newfd < 0) {
|
---|
1384 | async_answer_0(rid, newfd);
|
---|
1385 | vfs_file_put(oldfile);
|
---|
1386 | return;
|
---|
1387 | }
|
---|
1388 |
|
---|
1389 | assert(oldfile->node != NULL);
|
---|
1390 |
|
---|
1391 | newfile->node = oldfile->node;
|
---|
1392 | newfile->permissions = oldfile->permissions;
|
---|
1393 |
|
---|
1394 | vfs_file_put(oldfile);
|
---|
1395 | vfs_file_put(newfile);
|
---|
1396 |
|
---|
1397 | async_answer_0(rid, newfd);
|
---|
1398 | }
|
---|
1399 |
|
---|
1400 | /**
|
---|
1401 | * @}
|
---|
1402 | */
|
---|