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_pair_t rootfs = {
|
---|
71 | .fs_handle = 0,
|
---|
72 | .service_id = 0
|
---|
73 | };
|
---|
74 |
|
---|
75 | static int vfs_mount_internal(ipc_callid_t rid, service_id_t service_id,
|
---|
76 | fs_handle_t fs_handle, char *mp, char *opts)
|
---|
77 | {
|
---|
78 | vfs_lookup_res_t mp_res;
|
---|
79 | vfs_lookup_res_t mr_res;
|
---|
80 | vfs_node_t *mp_node = NULL;
|
---|
81 | vfs_node_t *mr_node;
|
---|
82 | fs_index_t rindex;
|
---|
83 | aoff64_t rsize;
|
---|
84 | unsigned rlnkcnt;
|
---|
85 | async_exch_t *exch;
|
---|
86 | sysarg_t rc;
|
---|
87 | aid_t msg;
|
---|
88 | ipc_call_t answer;
|
---|
89 |
|
---|
90 | /* Resolve the path to the mountpoint. */
|
---|
91 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
92 | if (rootfs.fs_handle) {
|
---|
93 | /* We already have the root FS. */
|
---|
94 | if (str_cmp(mp, "/") == 0) {
|
---|
95 | /* Trying to mount root FS over root FS */
|
---|
96 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
97 | async_answer_0(rid, EBUSY);
|
---|
98 | return EBUSY;
|
---|
99 | }
|
---|
100 |
|
---|
101 | rc = vfs_lookup_internal(mp, L_MP, &mp_res, NULL);
|
---|
102 | if (rc != EOK) {
|
---|
103 | /* The lookup failed for some reason. */
|
---|
104 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
105 | async_answer_0(rid, rc);
|
---|
106 | return rc;
|
---|
107 | }
|
---|
108 |
|
---|
109 | mp_node = vfs_node_get(&mp_res);
|
---|
110 | if (!mp_node) {
|
---|
111 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
112 | async_answer_0(rid, ENOMEM);
|
---|
113 | return ENOMEM;
|
---|
114 | }
|
---|
115 |
|
---|
116 | /*
|
---|
117 | * Now we hold a reference to mp_node.
|
---|
118 | * It will be dropped upon the corresponding VFS_IN_UNMOUNT.
|
---|
119 | * This prevents the mount point from being deleted.
|
---|
120 | */
|
---|
121 | } else {
|
---|
122 | /* We still don't have the root file system mounted. */
|
---|
123 | if (str_cmp(mp, "/") == 0) {
|
---|
124 | /*
|
---|
125 | * For this simple, but important case,
|
---|
126 | * we are almost done.
|
---|
127 | */
|
---|
128 |
|
---|
129 | /* Tell the mountee that it is being mounted. */
|
---|
130 | exch = vfs_exchange_grab(fs_handle);
|
---|
131 | msg = async_send_1(exch, VFS_OUT_MOUNTED,
|
---|
132 | (sysarg_t) service_id, &answer);
|
---|
133 | /* Send the mount options */
|
---|
134 | rc = async_data_write_start(exch, (void *)opts,
|
---|
135 | str_size(opts));
|
---|
136 | vfs_exchange_release(exch);
|
---|
137 |
|
---|
138 | if (rc != EOK) {
|
---|
139 | async_forget(msg);
|
---|
140 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
141 | async_answer_0(rid, rc);
|
---|
142 | return rc;
|
---|
143 | }
|
---|
144 | async_wait_for(msg, &rc);
|
---|
145 |
|
---|
146 | if (rc != EOK) {
|
---|
147 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
148 | async_answer_0(rid, rc);
|
---|
149 | return rc;
|
---|
150 | }
|
---|
151 |
|
---|
152 | rindex = (fs_index_t) IPC_GET_ARG1(answer);
|
---|
153 | rsize = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG2(answer),
|
---|
154 | IPC_GET_ARG3(answer));
|
---|
155 | rlnkcnt = (unsigned) IPC_GET_ARG4(answer);
|
---|
156 |
|
---|
157 | mr_res.triplet.fs_handle = fs_handle;
|
---|
158 | mr_res.triplet.service_id = service_id;
|
---|
159 | mr_res.triplet.index = rindex;
|
---|
160 | mr_res.size = rsize;
|
---|
161 | mr_res.lnkcnt = rlnkcnt;
|
---|
162 | mr_res.type = VFS_NODE_DIRECTORY;
|
---|
163 |
|
---|
164 | rootfs.fs_handle = fs_handle;
|
---|
165 | rootfs.service_id = service_id;
|
---|
166 |
|
---|
167 | /* Add reference to the mounted root. */
|
---|
168 | mr_node = vfs_node_get(&mr_res);
|
---|
169 | assert(mr_node);
|
---|
170 |
|
---|
171 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
172 | async_answer_0(rid, rc);
|
---|
173 | return rc;
|
---|
174 | } else {
|
---|
175 | /*
|
---|
176 | * We can't resolve this without the root filesystem
|
---|
177 | * being mounted first.
|
---|
178 | */
|
---|
179 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
180 | async_answer_0(rid, ENOENT);
|
---|
181 | return ENOENT;
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | /*
|
---|
186 | * At this point, we have all necessary pieces: file system handle
|
---|
187 | * and service ID, and we know the mount point VFS node.
|
---|
188 | */
|
---|
189 |
|
---|
190 | async_exch_t *mountee_exch = vfs_exchange_grab(fs_handle);
|
---|
191 | assert(mountee_exch);
|
---|
192 |
|
---|
193 | exch = vfs_exchange_grab(mp_res.triplet.fs_handle);
|
---|
194 | msg = async_send_4(exch, VFS_OUT_MOUNT,
|
---|
195 | (sysarg_t) mp_res.triplet.service_id,
|
---|
196 | (sysarg_t) mp_res.triplet.index,
|
---|
197 | (sysarg_t) fs_handle,
|
---|
198 | (sysarg_t) service_id, &answer);
|
---|
199 |
|
---|
200 | /* Send connection */
|
---|
201 | rc = async_exchange_clone(exch, mountee_exch);
|
---|
202 | vfs_exchange_release(mountee_exch);
|
---|
203 |
|
---|
204 | if (rc != EOK) {
|
---|
205 | vfs_exchange_release(exch);
|
---|
206 | async_forget(msg);
|
---|
207 |
|
---|
208 | /* Mount failed, drop reference to mp_node. */
|
---|
209 | if (mp_node)
|
---|
210 | vfs_node_put(mp_node);
|
---|
211 |
|
---|
212 | async_answer_0(rid, rc);
|
---|
213 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
214 | return rc;
|
---|
215 | }
|
---|
216 |
|
---|
217 | /* send the mount options */
|
---|
218 | rc = async_data_write_start(exch, (void *) opts, str_size(opts));
|
---|
219 | if (rc != EOK) {
|
---|
220 | vfs_exchange_release(exch);
|
---|
221 | async_forget(msg);
|
---|
222 |
|
---|
223 | /* Mount failed, drop reference to mp_node. */
|
---|
224 | if (mp_node)
|
---|
225 | vfs_node_put(mp_node);
|
---|
226 |
|
---|
227 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
228 | async_answer_0(rid, rc);
|
---|
229 | return rc;
|
---|
230 | }
|
---|
231 |
|
---|
232 | /*
|
---|
233 | * Wait for the answer before releasing the exchange to avoid deadlock
|
---|
234 | * in case the answer depends on further calls to the same file system.
|
---|
235 | * Think of a case when mounting a FS on a file_bd backed by a file on
|
---|
236 | * the same FS.
|
---|
237 | */
|
---|
238 | async_wait_for(msg, &rc);
|
---|
239 | vfs_exchange_release(exch);
|
---|
240 |
|
---|
241 | if (rc == EOK) {
|
---|
242 | rindex = (fs_index_t) IPC_GET_ARG1(answer);
|
---|
243 | rsize = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG2(answer),
|
---|
244 | IPC_GET_ARG3(answer));
|
---|
245 | rlnkcnt = (unsigned) IPC_GET_ARG4(answer);
|
---|
246 |
|
---|
247 | mr_res.triplet.fs_handle = fs_handle;
|
---|
248 | mr_res.triplet.service_id = service_id;
|
---|
249 | mr_res.triplet.index = rindex;
|
---|
250 | mr_res.size = rsize;
|
---|
251 | mr_res.lnkcnt = rlnkcnt;
|
---|
252 | mr_res.type = VFS_NODE_DIRECTORY;
|
---|
253 |
|
---|
254 | /* Add reference to the mounted root. */
|
---|
255 | mr_node = vfs_node_get(&mr_res);
|
---|
256 | assert(mr_node);
|
---|
257 | } else {
|
---|
258 | /* Mount failed, drop reference to mp_node. */
|
---|
259 | if (mp_node)
|
---|
260 | vfs_node_put(mp_node);
|
---|
261 | }
|
---|
262 |
|
---|
263 | async_answer_0(rid, rc);
|
---|
264 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
265 | return rc;
|
---|
266 | }
|
---|
267 |
|
---|
268 | void vfs_mount(ipc_callid_t rid, ipc_call_t *request)
|
---|
269 | {
|
---|
270 | service_id_t service_id;
|
---|
271 |
|
---|
272 | /*
|
---|
273 | * We expect the library to do the device-name to device-handle
|
---|
274 | * translation for us, thus the device handle will arrive as ARG1
|
---|
275 | * in the request.
|
---|
276 | */
|
---|
277 | service_id = (service_id_t) IPC_GET_ARG1(*request);
|
---|
278 |
|
---|
279 | /*
|
---|
280 | * Mount flags are passed as ARG2.
|
---|
281 | */
|
---|
282 | unsigned int flags = (unsigned int) IPC_GET_ARG2(*request);
|
---|
283 |
|
---|
284 | /*
|
---|
285 | * Instance number is passed as ARG3.
|
---|
286 | */
|
---|
287 | unsigned int instance = IPC_GET_ARG3(*request);
|
---|
288 |
|
---|
289 | /* We want the client to send us the mount point. */
|
---|
290 | char *mp;
|
---|
291 | int rc = async_data_write_accept((void **) &mp, true, 0, MAX_PATH_LEN,
|
---|
292 | 0, NULL);
|
---|
293 | if (rc != EOK) {
|
---|
294 | async_answer_0(rid, rc);
|
---|
295 | return;
|
---|
296 | }
|
---|
297 |
|
---|
298 | /* Now we expect to receive the mount options. */
|
---|
299 | char *opts;
|
---|
300 | rc = async_data_write_accept((void **) &opts, true, 0, MAX_MNTOPTS_LEN,
|
---|
301 | 0, NULL);
|
---|
302 | if (rc != EOK) {
|
---|
303 | free(mp);
|
---|
304 | async_answer_0(rid, rc);
|
---|
305 | return;
|
---|
306 | }
|
---|
307 |
|
---|
308 | /*
|
---|
309 | * Now, we expect the client to send us data with the name of the file
|
---|
310 | * system.
|
---|
311 | */
|
---|
312 | char *fs_name;
|
---|
313 | rc = async_data_write_accept((void **) &fs_name, true, 0,
|
---|
314 | FS_NAME_MAXLEN, 0, NULL);
|
---|
315 | if (rc != EOK) {
|
---|
316 | free(mp);
|
---|
317 | free(opts);
|
---|
318 | async_answer_0(rid, rc);
|
---|
319 | return;
|
---|
320 | }
|
---|
321 |
|
---|
322 | /*
|
---|
323 | * Wait for VFS_IN_PING so that we can return an error if we don't know
|
---|
324 | * fs_name.
|
---|
325 | */
|
---|
326 | ipc_call_t data;
|
---|
327 | ipc_callid_t callid = async_get_call(&data);
|
---|
328 | if (IPC_GET_IMETHOD(data) != VFS_IN_PING) {
|
---|
329 | async_answer_0(callid, ENOTSUP);
|
---|
330 | async_answer_0(rid, ENOTSUP);
|
---|
331 | free(mp);
|
---|
332 | free(opts);
|
---|
333 | free(fs_name);
|
---|
334 | return;
|
---|
335 | }
|
---|
336 |
|
---|
337 | /*
|
---|
338 | * Check if we know a file system with the same name as is in fs_name.
|
---|
339 | * This will also give us its file system handle.
|
---|
340 | */
|
---|
341 | fibril_mutex_lock(&fs_list_lock);
|
---|
342 | fs_handle_t fs_handle;
|
---|
343 | recheck:
|
---|
344 | fs_handle = fs_name_to_handle(instance, fs_name, false);
|
---|
345 | if (!fs_handle) {
|
---|
346 | if (flags & IPC_FLAG_BLOCKING) {
|
---|
347 | fibril_condvar_wait(&fs_list_cv, &fs_list_lock);
|
---|
348 | goto recheck;
|
---|
349 | }
|
---|
350 |
|
---|
351 | fibril_mutex_unlock(&fs_list_lock);
|
---|
352 | async_answer_0(callid, ENOENT);
|
---|
353 | async_answer_0(rid, ENOENT);
|
---|
354 | free(mp);
|
---|
355 | free(fs_name);
|
---|
356 | free(opts);
|
---|
357 | return;
|
---|
358 | }
|
---|
359 | fibril_mutex_unlock(&fs_list_lock);
|
---|
360 |
|
---|
361 | /* Add the filesystem info to the list of mounted filesystems */
|
---|
362 | mtab_ent_t *mtab_ent = malloc(sizeof(mtab_ent_t));
|
---|
363 | if (!mtab_ent) {
|
---|
364 | async_answer_0(callid, ENOMEM);
|
---|
365 | async_answer_0(rid, ENOMEM);
|
---|
366 | free(mp);
|
---|
367 | free(fs_name);
|
---|
368 | free(opts);
|
---|
369 | return;
|
---|
370 | }
|
---|
371 |
|
---|
372 | /* Do the mount */
|
---|
373 | rc = vfs_mount_internal(rid, service_id, fs_handle, mp, opts);
|
---|
374 | if (rc != EOK) {
|
---|
375 | async_answer_0(callid, ENOTSUP);
|
---|
376 | async_answer_0(rid, ENOTSUP);
|
---|
377 | free(mtab_ent);
|
---|
378 | free(mp);
|
---|
379 | free(opts);
|
---|
380 | free(fs_name);
|
---|
381 | return;
|
---|
382 | }
|
---|
383 |
|
---|
384 | /* Add the filesystem info to the list of mounted filesystems */
|
---|
385 |
|
---|
386 | str_cpy(mtab_ent->mp, MAX_PATH_LEN, mp);
|
---|
387 | str_cpy(mtab_ent->fs_name, FS_NAME_MAXLEN, fs_name);
|
---|
388 | str_cpy(mtab_ent->opts, MAX_MNTOPTS_LEN, opts);
|
---|
389 | mtab_ent->instance = instance;
|
---|
390 | mtab_ent->service_id = service_id;
|
---|
391 |
|
---|
392 | link_initialize(&mtab_ent->link);
|
---|
393 |
|
---|
394 | fibril_mutex_lock(&mtab_list_lock);
|
---|
395 | list_append(&mtab_ent->link, &mtab_list);
|
---|
396 | mtab_size++;
|
---|
397 | fibril_mutex_unlock(&mtab_list_lock);
|
---|
398 |
|
---|
399 | free(mp);
|
---|
400 | free(fs_name);
|
---|
401 | free(opts);
|
---|
402 |
|
---|
403 | /* Acknowledge that we know fs_name. */
|
---|
404 | async_answer_0(callid, EOK);
|
---|
405 | }
|
---|
406 |
|
---|
407 | void vfs_unmount(ipc_callid_t rid, ipc_call_t *request)
|
---|
408 | {
|
---|
409 | int rc;
|
---|
410 | char *mp;
|
---|
411 | vfs_lookup_res_t mp_res;
|
---|
412 | vfs_lookup_res_t mr_res;
|
---|
413 | vfs_node_t *mr_node;
|
---|
414 | async_exch_t *exch;
|
---|
415 |
|
---|
416 | /*
|
---|
417 | * Receive the mount point path.
|
---|
418 | */
|
---|
419 | rc = async_data_write_accept((void **) &mp, true, 0, MAX_PATH_LEN,
|
---|
420 | 0, NULL);
|
---|
421 | if (rc != EOK)
|
---|
422 | async_answer_0(rid, rc);
|
---|
423 |
|
---|
424 | /*
|
---|
425 | * Taking the namespace lock will do two things for us. First, it will
|
---|
426 | * prevent races with other lookup operations. Second, it will stop new
|
---|
427 | * references to already existing VFS nodes and creation of new VFS
|
---|
428 | * nodes. This is because new references are added as a result of some
|
---|
429 | * lookup operation or at least of some operation which is protected by
|
---|
430 | * the namespace lock.
|
---|
431 | */
|
---|
432 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
433 |
|
---|
434 | /*
|
---|
435 | * Lookup the mounted root and instantiate it.
|
---|
436 | */
|
---|
437 | rc = vfs_lookup_internal(mp, L_ROOT, &mr_res, NULL);
|
---|
438 | if (rc != EOK) {
|
---|
439 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
440 | free(mp);
|
---|
441 | async_answer_0(rid, rc);
|
---|
442 | return;
|
---|
443 | }
|
---|
444 | mr_node = vfs_node_get(&mr_res);
|
---|
445 | if (!mr_node) {
|
---|
446 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
447 | free(mp);
|
---|
448 | async_answer_0(rid, ENOMEM);
|
---|
449 | return;
|
---|
450 | }
|
---|
451 |
|
---|
452 | /*
|
---|
453 | * Count the total number of references for the mounted file system. We
|
---|
454 | * are expecting at least two. One which we got above and one which we
|
---|
455 | * got when the file system was mounted. If we find more, it means that
|
---|
456 | * the file system cannot be gracefully unmounted at the moment because
|
---|
457 | * someone is working with it.
|
---|
458 | */
|
---|
459 | if (vfs_nodes_refcount_sum_get(mr_node->fs_handle,
|
---|
460 | mr_node->service_id) != 2) {
|
---|
461 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
462 | vfs_node_put(mr_node);
|
---|
463 | free(mp);
|
---|
464 | async_answer_0(rid, EBUSY);
|
---|
465 | return;
|
---|
466 | }
|
---|
467 |
|
---|
468 | if (str_cmp(mp, "/") == 0) {
|
---|
469 |
|
---|
470 | /*
|
---|
471 | * Unmounting the root file system.
|
---|
472 | *
|
---|
473 | * In this case, there is no mount point node and we send
|
---|
474 | * VFS_OUT_UNMOUNTED directly to the mounted file system.
|
---|
475 | */
|
---|
476 |
|
---|
477 | exch = vfs_exchange_grab(mr_node->fs_handle);
|
---|
478 | rc = async_req_1_0(exch, VFS_OUT_UNMOUNTED,
|
---|
479 | mr_node->service_id);
|
---|
480 | vfs_exchange_release(exch);
|
---|
481 |
|
---|
482 | if (rc != EOK) {
|
---|
483 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
484 | free(mp);
|
---|
485 | vfs_node_put(mr_node);
|
---|
486 | async_answer_0(rid, rc);
|
---|
487 | return;
|
---|
488 | }
|
---|
489 |
|
---|
490 | rootfs.fs_handle = 0;
|
---|
491 | rootfs.service_id = 0;
|
---|
492 | } else {
|
---|
493 |
|
---|
494 | /*
|
---|
495 | * Unmounting a non-root file system.
|
---|
496 | *
|
---|
497 | * We have a regular mount point node representing the parent
|
---|
498 | * file system, so we delegate the operation to it.
|
---|
499 | */
|
---|
500 |
|
---|
501 | rc = vfs_lookup_internal(mp, L_MP, &mp_res, NULL);
|
---|
502 | if (rc != EOK) {
|
---|
503 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
504 | free(mp);
|
---|
505 | vfs_node_put(mr_node);
|
---|
506 | async_answer_0(rid, rc);
|
---|
507 | return;
|
---|
508 | }
|
---|
509 |
|
---|
510 | vfs_node_t *mp_node = vfs_node_get(&mp_res);
|
---|
511 | if (!mp_node) {
|
---|
512 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
513 | free(mp);
|
---|
514 | vfs_node_put(mr_node);
|
---|
515 | async_answer_0(rid, ENOMEM);
|
---|
516 | return;
|
---|
517 | }
|
---|
518 |
|
---|
519 | exch = vfs_exchange_grab(mp_node->fs_handle);
|
---|
520 | rc = async_req_2_0(exch, VFS_OUT_UNMOUNT,
|
---|
521 | mp_node->service_id, mp_node->index);
|
---|
522 | vfs_exchange_release(exch);
|
---|
523 |
|
---|
524 | if (rc != EOK) {
|
---|
525 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
526 | free(mp);
|
---|
527 | vfs_node_put(mp_node);
|
---|
528 | vfs_node_put(mr_node);
|
---|
529 | async_answer_0(rid, rc);
|
---|
530 | return;
|
---|
531 | }
|
---|
532 |
|
---|
533 | /* Drop the reference we got above. */
|
---|
534 | vfs_node_put(mp_node);
|
---|
535 | /* Drop the reference from when the file system was mounted. */
|
---|
536 | vfs_node_put(mp_node);
|
---|
537 | }
|
---|
538 |
|
---|
539 | /*
|
---|
540 | * All went well, the mounted file system was successfully unmounted.
|
---|
541 | * The only thing left is to forget the unmounted root VFS node.
|
---|
542 | */
|
---|
543 | vfs_node_forget(mr_node);
|
---|
544 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
545 |
|
---|
546 | fibril_mutex_lock(&mtab_list_lock);
|
---|
547 |
|
---|
548 | int found = 0;
|
---|
549 |
|
---|
550 | list_foreach(mtab_list, cur) {
|
---|
551 | mtab_ent_t *mtab_ent = list_get_instance(cur, mtab_ent_t,
|
---|
552 | link);
|
---|
553 |
|
---|
554 | if (str_cmp(mtab_ent->mp, mp) == 0) {
|
---|
555 | list_remove(&mtab_ent->link);
|
---|
556 | mtab_size--;
|
---|
557 | free(mtab_ent);
|
---|
558 | found = 1;
|
---|
559 | break;
|
---|
560 | }
|
---|
561 | }
|
---|
562 | assert(found);
|
---|
563 | fibril_mutex_unlock(&mtab_list_lock);
|
---|
564 |
|
---|
565 | free(mp);
|
---|
566 |
|
---|
567 | async_answer_0(rid, EOK);
|
---|
568 | }
|
---|
569 |
|
---|
570 | static inline bool walk_flags_valid(int flags)
|
---|
571 | {
|
---|
572 | if ((flags&~WALK_ALL_FLAGS) != 0) {
|
---|
573 | return false;
|
---|
574 | }
|
---|
575 | if ((flags&WALK_MAY_CREATE) && (flags&WALK_MUST_CREATE)) {
|
---|
576 | return false;
|
---|
577 | }
|
---|
578 | if ((flags&WALK_REGULAR) && (flags&WALK_DIRECTORY)) {
|
---|
579 | return false;
|
---|
580 | }
|
---|
581 | if ((flags&WALK_MAY_CREATE) || (flags&WALK_MUST_CREATE)) {
|
---|
582 | if (!(flags&WALK_DIRECTORY) && !(flags&WALK_REGULAR)) {
|
---|
583 | return false;
|
---|
584 | }
|
---|
585 | }
|
---|
586 | return true;
|
---|
587 | }
|
---|
588 |
|
---|
589 | static inline int walk_lookup_flags(int flags)
|
---|
590 | {
|
---|
591 | int lflags = 0;
|
---|
592 | if (flags&WALK_MAY_CREATE || flags&WALK_MUST_CREATE) {
|
---|
593 | lflags |= L_CREATE;
|
---|
594 | }
|
---|
595 | if (flags&WALK_MUST_CREATE) {
|
---|
596 | lflags |= L_EXCLUSIVE;
|
---|
597 | }
|
---|
598 | if (flags&WALK_REGULAR) {
|
---|
599 | lflags |= L_FILE;
|
---|
600 | }
|
---|
601 | if (flags&WALK_DIRECTORY) {
|
---|
602 | lflags |= L_DIRECTORY;
|
---|
603 | }
|
---|
604 | return lflags;
|
---|
605 | }
|
---|
606 |
|
---|
607 | void vfs_walk(ipc_callid_t rid, ipc_call_t *request)
|
---|
608 | {
|
---|
609 | /*
|
---|
610 | * Parent is our relative root for file lookup.
|
---|
611 | * For defined flags, see <ipc/vfs.h>.
|
---|
612 | */
|
---|
613 | int parentfd = IPC_GET_ARG1(*request);
|
---|
614 | int flags = IPC_GET_ARG2(*request);
|
---|
615 |
|
---|
616 | if (!walk_flags_valid(flags)) {
|
---|
617 | async_answer_0(rid, EINVAL);
|
---|
618 | return;
|
---|
619 | }
|
---|
620 |
|
---|
621 | char *path;
|
---|
622 | int rc = async_data_write_accept((void **)&path, true, 0, 0, 0, NULL);
|
---|
623 |
|
---|
624 | /* Lookup the file structure corresponding to the file descriptor. */
|
---|
625 | vfs_file_t *parent = NULL;
|
---|
626 | vfs_pair_t *parent_node = NULL;
|
---|
627 | // TODO: Client-side root.
|
---|
628 | if (parentfd != -1) {
|
---|
629 | parent = vfs_file_get(parentfd);
|
---|
630 | if (!parent) {
|
---|
631 | free(path);
|
---|
632 | async_answer_0(rid, EBADF);
|
---|
633 | return;
|
---|
634 | }
|
---|
635 | parent_node = (vfs_pair_t *)parent->node;
|
---|
636 | }
|
---|
637 |
|
---|
638 | fibril_rwlock_read_lock(&namespace_rwlock);
|
---|
639 |
|
---|
640 | vfs_lookup_res_t lr;
|
---|
641 | rc = vfs_lookup_internal(path, walk_lookup_flags(flags), &lr, parent_node);
|
---|
642 | free(path);
|
---|
643 |
|
---|
644 | if (rc != EOK) {
|
---|
645 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
646 | if (parent) {
|
---|
647 | vfs_file_put(parent);
|
---|
648 | }
|
---|
649 | async_answer_0(rid, rc);
|
---|
650 | return;
|
---|
651 | }
|
---|
652 |
|
---|
653 | vfs_node_t *node = vfs_node_get(&lr);
|
---|
654 |
|
---|
655 | int fd = vfs_fd_alloc(false);
|
---|
656 | if (fd < 0) {
|
---|
657 | vfs_node_put(node);
|
---|
658 | if (parent) {
|
---|
659 | vfs_file_put(parent);
|
---|
660 | }
|
---|
661 | async_answer_0(rid, fd);
|
---|
662 | return;
|
---|
663 | }
|
---|
664 |
|
---|
665 | vfs_file_t *file = vfs_file_get(fd);
|
---|
666 | assert(file != NULL);
|
---|
667 |
|
---|
668 | file->node = node;
|
---|
669 | if (parent) {
|
---|
670 | file->permissions = parent->permissions;
|
---|
671 | } else {
|
---|
672 | file->permissions = MODE_READ | MODE_WRITE | MODE_APPEND;
|
---|
673 | }
|
---|
674 | file->open_read = false;
|
---|
675 | file->open_write = false;
|
---|
676 |
|
---|
677 | vfs_node_addref(node);
|
---|
678 | vfs_node_put(node);
|
---|
679 | vfs_file_put(file);
|
---|
680 | if (parent) {
|
---|
681 | vfs_file_put(parent);
|
---|
682 | }
|
---|
683 |
|
---|
684 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
685 |
|
---|
686 | async_answer_1(rid, EOK, fd);
|
---|
687 | }
|
---|
688 |
|
---|
689 | void vfs_open2(ipc_callid_t rid, ipc_call_t *request)
|
---|
690 | {
|
---|
691 | int fd = IPC_GET_ARG1(*request);
|
---|
692 | int flags = IPC_GET_ARG2(*request);
|
---|
693 |
|
---|
694 | if (flags == 0) {
|
---|
695 | async_answer_0(rid, EINVAL);
|
---|
696 | return;
|
---|
697 | }
|
---|
698 |
|
---|
699 | vfs_file_t *file = vfs_file_get(fd);
|
---|
700 | if (!file) {
|
---|
701 | async_answer_0(rid, EBADF);
|
---|
702 | return;
|
---|
703 | }
|
---|
704 |
|
---|
705 | if ((flags & ~file->permissions) != 0) {
|
---|
706 | vfs_file_put(file);
|
---|
707 | async_answer_0(rid, EPERM);
|
---|
708 | return;
|
---|
709 | }
|
---|
710 |
|
---|
711 | file->open_read = (flags & MODE_READ) != 0;
|
---|
712 | file->open_write = (flags & (MODE_WRITE | MODE_APPEND)) != 0;
|
---|
713 | file->append = (flags & MODE_APPEND) != 0;
|
---|
714 |
|
---|
715 | if (!file->open_read && !file->open_write) {
|
---|
716 | vfs_file_put(file);
|
---|
717 | async_answer_0(rid, EINVAL);
|
---|
718 | return;
|
---|
719 | }
|
---|
720 |
|
---|
721 | if (file->node->type == VFS_NODE_DIRECTORY && file->open_write) {
|
---|
722 | file->open_read = file->open_write = false;
|
---|
723 | vfs_file_put(file);
|
---|
724 | async_answer_0(rid, EINVAL);
|
---|
725 | return;
|
---|
726 | }
|
---|
727 |
|
---|
728 | int rc = vfs_open_node_remote(file->node);
|
---|
729 | if (rc != EOK) {
|
---|
730 | file->open_read = file->open_write = false;
|
---|
731 | vfs_file_put(file);
|
---|
732 | async_answer_0(rid, rc);
|
---|
733 | return;
|
---|
734 | }
|
---|
735 |
|
---|
736 | vfs_file_put(file);
|
---|
737 | async_answer_0(rid, EOK);
|
---|
738 | }
|
---|
739 |
|
---|
740 | void vfs_open(ipc_callid_t rid, ipc_call_t *request)
|
---|
741 | {
|
---|
742 | /*
|
---|
743 | * The POSIX interface is open(path, oflag, mode).
|
---|
744 | * We can receive oflags and mode along with the VFS_IN_OPEN call;
|
---|
745 | * the path will need to arrive in another call.
|
---|
746 | *
|
---|
747 | * We also receive one private, non-POSIX set of flags called lflag
|
---|
748 | * used to pass information to vfs_lookup_internal().
|
---|
749 | */
|
---|
750 | int lflag = IPC_GET_ARG1(*request);
|
---|
751 | int oflag = IPC_GET_ARG2(*request);
|
---|
752 | int mode = IPC_GET_ARG3(*request);
|
---|
753 |
|
---|
754 | /* Ignore mode for now. */
|
---|
755 | (void) mode;
|
---|
756 |
|
---|
757 | /*
|
---|
758 | * Make sure that we are called with exactly one of L_FILE and
|
---|
759 | * L_DIRECTORY. Make sure that the user does not pass L_OPEN,
|
---|
760 | * L_ROOT or L_MP.
|
---|
761 | */
|
---|
762 | if (((lflag & (L_FILE | L_DIRECTORY)) == 0) ||
|
---|
763 | ((lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY)) ||
|
---|
764 | (lflag & (L_OPEN | L_ROOT | L_MP))) {
|
---|
765 | async_answer_0(rid, EINVAL);
|
---|
766 | return;
|
---|
767 | }
|
---|
768 |
|
---|
769 | if (oflag & O_CREAT)
|
---|
770 | lflag |= L_CREATE;
|
---|
771 | if (oflag & O_EXCL)
|
---|
772 | lflag |= L_EXCLUSIVE;
|
---|
773 |
|
---|
774 | char *path;
|
---|
775 | int rc = async_data_write_accept((void **) &path, true, 0, 0, 0, NULL);
|
---|
776 | if (rc != EOK) {
|
---|
777 | async_answer_0(rid, rc);
|
---|
778 | return;
|
---|
779 | }
|
---|
780 |
|
---|
781 | /*
|
---|
782 | * Avoid the race condition in which the file can be deleted before we
|
---|
783 | * find/create-and-lock the VFS node corresponding to the looked-up
|
---|
784 | * triplet.
|
---|
785 | */
|
---|
786 | if (lflag & L_CREATE)
|
---|
787 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
788 | else
|
---|
789 | fibril_rwlock_read_lock(&namespace_rwlock);
|
---|
790 |
|
---|
791 | /* The path is now populated and we can call vfs_lookup_internal(). */
|
---|
792 | vfs_lookup_res_t lr;
|
---|
793 | rc = vfs_lookup_internal(path, lflag | L_OPEN, &lr, NULL);
|
---|
794 | if (rc != EOK) {
|
---|
795 | if (lflag & L_CREATE)
|
---|
796 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
797 | else
|
---|
798 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
799 | async_answer_0(rid, rc);
|
---|
800 | free(path);
|
---|
801 | return;
|
---|
802 | }
|
---|
803 |
|
---|
804 | /* Path is no longer needed. */
|
---|
805 | free(path);
|
---|
806 |
|
---|
807 | vfs_node_t *node = vfs_node_get(&lr);
|
---|
808 | if (lflag & L_CREATE)
|
---|
809 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
810 | else
|
---|
811 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
812 |
|
---|
813 | /* Truncate the file if requested and if necessary. */
|
---|
814 | if (oflag & O_TRUNC) {
|
---|
815 | fibril_rwlock_write_lock(&node->contents_rwlock);
|
---|
816 | if (node->size) {
|
---|
817 | rc = vfs_truncate_internal(node->fs_handle,
|
---|
818 | node->service_id, node->index, 0);
|
---|
819 | if (rc) {
|
---|
820 | fibril_rwlock_write_unlock(&node->contents_rwlock);
|
---|
821 | vfs_node_put(node);
|
---|
822 | async_answer_0(rid, rc);
|
---|
823 | return;
|
---|
824 | }
|
---|
825 | node->size = 0;
|
---|
826 | }
|
---|
827 | fibril_rwlock_write_unlock(&node->contents_rwlock);
|
---|
828 | }
|
---|
829 |
|
---|
830 | /*
|
---|
831 | * Get ourselves a file descriptor and the corresponding vfs_file_t
|
---|
832 | * structure.
|
---|
833 | */
|
---|
834 | int fd = vfs_fd_alloc((oflag & O_DESC) != 0);
|
---|
835 | if (fd < 0) {
|
---|
836 | vfs_node_put(node);
|
---|
837 | async_answer_0(rid, fd);
|
---|
838 | return;
|
---|
839 | }
|
---|
840 | vfs_file_t *file = vfs_file_get(fd);
|
---|
841 |
|
---|
842 | /* There is a potential race with another fibril of a malicious client. */
|
---|
843 | if (!file) {
|
---|
844 | vfs_node_put(node);
|
---|
845 | async_answer_0(rid, EBUSY);
|
---|
846 | return;
|
---|
847 | }
|
---|
848 |
|
---|
849 | file->node = node;
|
---|
850 | if (oflag & O_RDONLY)
|
---|
851 | file->open_read = true;
|
---|
852 | if (oflag & O_WRONLY)
|
---|
853 | file->open_write = true;
|
---|
854 | if (oflag & O_RDWR)
|
---|
855 | file->open_read = file->open_write = true;
|
---|
856 | if (oflag & O_APPEND)
|
---|
857 | file->append = true;
|
---|
858 |
|
---|
859 | /*
|
---|
860 | * The following increase in reference count is for the fact that the
|
---|
861 | * file is being opened and that a file structure is pointing to it.
|
---|
862 | * It is necessary so that the file will not disappear when
|
---|
863 | * vfs_node_put() is called. The reference will be dropped by the
|
---|
864 | * respective VFS_IN_CLOSE.
|
---|
865 | */
|
---|
866 | vfs_node_addref(node);
|
---|
867 | vfs_node_put(node);
|
---|
868 | vfs_file_put(file);
|
---|
869 |
|
---|
870 | /* Success! Return the new file descriptor to the client. */
|
---|
871 | async_answer_1(rid, EOK, fd);
|
---|
872 | }
|
---|
873 |
|
---|
874 | void vfs_sync(ipc_callid_t rid, ipc_call_t *request)
|
---|
875 | {
|
---|
876 | int fd = IPC_GET_ARG1(*request);
|
---|
877 |
|
---|
878 | /* Lookup the file structure corresponding to the file descriptor. */
|
---|
879 | vfs_file_t *file = vfs_file_get(fd);
|
---|
880 | if (!file) {
|
---|
881 | async_answer_0(rid, ENOENT);
|
---|
882 | return;
|
---|
883 | }
|
---|
884 |
|
---|
885 | /*
|
---|
886 | * Lock the open file structure so that no other thread can manipulate
|
---|
887 | * the same open file at a time.
|
---|
888 | */
|
---|
889 | fibril_mutex_lock(&file->lock);
|
---|
890 | async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
|
---|
891 |
|
---|
892 | /* Make a VFS_OUT_SYMC request at the destination FS server. */
|
---|
893 | aid_t msg;
|
---|
894 | ipc_call_t answer;
|
---|
895 | msg = async_send_2(fs_exch, VFS_OUT_SYNC, file->node->service_id,
|
---|
896 | file->node->index, &answer);
|
---|
897 |
|
---|
898 | vfs_exchange_release(fs_exch);
|
---|
899 |
|
---|
900 | /* Wait for reply from the FS server. */
|
---|
901 | sysarg_t rc;
|
---|
902 | async_wait_for(msg, &rc);
|
---|
903 |
|
---|
904 | fibril_mutex_unlock(&file->lock);
|
---|
905 |
|
---|
906 | vfs_file_put(file);
|
---|
907 | async_answer_0(rid, rc);
|
---|
908 | }
|
---|
909 |
|
---|
910 | void vfs_close(ipc_callid_t rid, ipc_call_t *request)
|
---|
911 | {
|
---|
912 | int fd = IPC_GET_ARG1(*request);
|
---|
913 | int ret = vfs_fd_free(fd);
|
---|
914 | async_answer_0(rid, ret);
|
---|
915 | }
|
---|
916 |
|
---|
917 | static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
|
---|
918 | {
|
---|
919 | /*
|
---|
920 | * The following code strongly depends on the fact that the files data
|
---|
921 | * structure can be only accessed by a single fibril and all file
|
---|
922 | * operations are serialized (i.e. the reads and writes cannot
|
---|
923 | * interleave and a file cannot be closed while it is being read).
|
---|
924 | *
|
---|
925 | * Additional synchronization needs to be added once the table of
|
---|
926 | * open files supports parallel access!
|
---|
927 | */
|
---|
928 |
|
---|
929 | int fd = IPC_GET_ARG1(*request);
|
---|
930 |
|
---|
931 | /* Lookup the file structure corresponding to the file descriptor. */
|
---|
932 | vfs_file_t *file = vfs_file_get(fd);
|
---|
933 | if (!file) {
|
---|
934 | async_answer_0(rid, ENOENT);
|
---|
935 | return;
|
---|
936 | }
|
---|
937 |
|
---|
938 | /*
|
---|
939 | * Lock the open file structure so that no other thread can manipulate
|
---|
940 | * the same open file at a time.
|
---|
941 | */
|
---|
942 | fibril_mutex_lock(&file->lock);
|
---|
943 |
|
---|
944 | if ((read && !file->open_read) || (!read && !file->open_write)) {
|
---|
945 | fibril_mutex_unlock(&file->lock);
|
---|
946 | async_answer_0(rid, EINVAL);
|
---|
947 | return;
|
---|
948 | }
|
---|
949 |
|
---|
950 | vfs_info_t *fs_info = fs_handle_to_info(file->node->fs_handle);
|
---|
951 | assert(fs_info);
|
---|
952 |
|
---|
953 | /*
|
---|
954 | * Lock the file's node so that no other client can read/write to it at
|
---|
955 | * the same time unless the FS supports concurrent reads/writes and its
|
---|
956 | * write implementation does not modify the file size.
|
---|
957 | */
|
---|
958 | if ((read) ||
|
---|
959 | ((fs_info->concurrent_read_write) && (fs_info->write_retains_size)))
|
---|
960 | fibril_rwlock_read_lock(&file->node->contents_rwlock);
|
---|
961 | else
|
---|
962 | fibril_rwlock_write_lock(&file->node->contents_rwlock);
|
---|
963 |
|
---|
964 | if (file->node->type == VFS_NODE_DIRECTORY) {
|
---|
965 | /*
|
---|
966 | * Make sure that no one is modifying the namespace
|
---|
967 | * while we are in readdir().
|
---|
968 | */
|
---|
969 | assert(read);
|
---|
970 | fibril_rwlock_read_lock(&namespace_rwlock);
|
---|
971 | }
|
---|
972 |
|
---|
973 | async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
|
---|
974 |
|
---|
975 | /*
|
---|
976 | * Make a VFS_READ/VFS_WRITE request at the destination FS server
|
---|
977 | * and forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
|
---|
978 | * destination FS server. The call will be routed as if sent by
|
---|
979 | * ourselves. Note that call arguments are immutable in this case so we
|
---|
980 | * don't have to bother.
|
---|
981 | */
|
---|
982 | sysarg_t rc;
|
---|
983 | ipc_call_t answer;
|
---|
984 | if (read) {
|
---|
985 | rc = async_data_read_forward_4_1(fs_exch, VFS_OUT_READ,
|
---|
986 | file->node->service_id, file->node->index,
|
---|
987 | LOWER32(file->pos), UPPER32(file->pos), &answer);
|
---|
988 | } else {
|
---|
989 | if (file->append)
|
---|
990 | file->pos = file->node->size;
|
---|
991 |
|
---|
992 | rc = async_data_write_forward_4_1(fs_exch, VFS_OUT_WRITE,
|
---|
993 | file->node->service_id, file->node->index,
|
---|
994 | LOWER32(file->pos), UPPER32(file->pos), &answer);
|
---|
995 | }
|
---|
996 |
|
---|
997 | vfs_exchange_release(fs_exch);
|
---|
998 |
|
---|
999 | size_t bytes = IPC_GET_ARG1(answer);
|
---|
1000 |
|
---|
1001 | if (file->node->type == VFS_NODE_DIRECTORY)
|
---|
1002 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
1003 |
|
---|
1004 | /* Unlock the VFS node. */
|
---|
1005 | if ((read) ||
|
---|
1006 | ((fs_info->concurrent_read_write) && (fs_info->write_retains_size)))
|
---|
1007 | fibril_rwlock_read_unlock(&file->node->contents_rwlock);
|
---|
1008 | else {
|
---|
1009 | /* Update the cached version of node's size. */
|
---|
1010 | if (rc == EOK)
|
---|
1011 | file->node->size = MERGE_LOUP32(IPC_GET_ARG2(answer),
|
---|
1012 | IPC_GET_ARG3(answer));
|
---|
1013 | fibril_rwlock_write_unlock(&file->node->contents_rwlock);
|
---|
1014 | }
|
---|
1015 |
|
---|
1016 | /* Update the position pointer and unlock the open file. */
|
---|
1017 | if (rc == EOK)
|
---|
1018 | file->pos += bytes;
|
---|
1019 | fibril_mutex_unlock(&file->lock);
|
---|
1020 | vfs_file_put(file);
|
---|
1021 |
|
---|
1022 | /*
|
---|
1023 | * FS server's reply is the final result of the whole operation we
|
---|
1024 | * return to the client.
|
---|
1025 | */
|
---|
1026 | async_answer_1(rid, rc, bytes);
|
---|
1027 | }
|
---|
1028 |
|
---|
1029 | void vfs_read(ipc_callid_t rid, ipc_call_t *request)
|
---|
1030 | {
|
---|
1031 | vfs_rdwr(rid, request, true);
|
---|
1032 | }
|
---|
1033 |
|
---|
1034 | void vfs_write(ipc_callid_t rid, ipc_call_t *request)
|
---|
1035 | {
|
---|
1036 | vfs_rdwr(rid, request, false);
|
---|
1037 | }
|
---|
1038 |
|
---|
1039 | void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
|
---|
1040 | {
|
---|
1041 | int fd = (int) IPC_GET_ARG1(*request);
|
---|
1042 | off64_t off = (off64_t) MERGE_LOUP32(IPC_GET_ARG2(*request),
|
---|
1043 | IPC_GET_ARG3(*request));
|
---|
1044 | int whence = (int) IPC_GET_ARG4(*request);
|
---|
1045 |
|
---|
1046 | /* Lookup the file structure corresponding to the file descriptor. */
|
---|
1047 | vfs_file_t *file = vfs_file_get(fd);
|
---|
1048 | if (!file) {
|
---|
1049 | async_answer_0(rid, ENOENT);
|
---|
1050 | return;
|
---|
1051 | }
|
---|
1052 |
|
---|
1053 | fibril_mutex_lock(&file->lock);
|
---|
1054 |
|
---|
1055 | off64_t newoff;
|
---|
1056 | switch (whence) {
|
---|
1057 | case SEEK_SET:
|
---|
1058 | if (off >= 0) {
|
---|
1059 | file->pos = (aoff64_t) off;
|
---|
1060 | fibril_mutex_unlock(&file->lock);
|
---|
1061 | vfs_file_put(file);
|
---|
1062 | async_answer_1(rid, EOK, off);
|
---|
1063 | return;
|
---|
1064 | }
|
---|
1065 | break;
|
---|
1066 | case SEEK_CUR:
|
---|
1067 | if ((off >= 0) && (file->pos + off < file->pos)) {
|
---|
1068 | fibril_mutex_unlock(&file->lock);
|
---|
1069 | vfs_file_put(file);
|
---|
1070 | async_answer_0(rid, EOVERFLOW);
|
---|
1071 | return;
|
---|
1072 | }
|
---|
1073 |
|
---|
1074 | if ((off < 0) && (file->pos < (aoff64_t) -off)) {
|
---|
1075 | fibril_mutex_unlock(&file->lock);
|
---|
1076 | vfs_file_put(file);
|
---|
1077 | async_answer_0(rid, EOVERFLOW);
|
---|
1078 | return;
|
---|
1079 | }
|
---|
1080 |
|
---|
1081 | file->pos += off;
|
---|
1082 | newoff = (file->pos > OFF64_MAX) ? OFF64_MAX : file->pos;
|
---|
1083 |
|
---|
1084 | fibril_mutex_unlock(&file->lock);
|
---|
1085 | vfs_file_put(file);
|
---|
1086 | async_answer_2(rid, EOK, LOWER32(newoff),
|
---|
1087 | UPPER32(newoff));
|
---|
1088 | return;
|
---|
1089 | case SEEK_END:
|
---|
1090 | fibril_rwlock_read_lock(&file->node->contents_rwlock);
|
---|
1091 | aoff64_t size = file->node->size;
|
---|
1092 |
|
---|
1093 | if ((off >= 0) && (size + off < size)) {
|
---|
1094 | fibril_rwlock_read_unlock(&file->node->contents_rwlock);
|
---|
1095 | fibril_mutex_unlock(&file->lock);
|
---|
1096 | vfs_file_put(file);
|
---|
1097 | async_answer_0(rid, EOVERFLOW);
|
---|
1098 | return;
|
---|
1099 | }
|
---|
1100 |
|
---|
1101 | if ((off < 0) && (size < (aoff64_t) -off)) {
|
---|
1102 | fibril_rwlock_read_unlock(&file->node->contents_rwlock);
|
---|
1103 | fibril_mutex_unlock(&file->lock);
|
---|
1104 | vfs_file_put(file);
|
---|
1105 | async_answer_0(rid, EOVERFLOW);
|
---|
1106 | return;
|
---|
1107 | }
|
---|
1108 |
|
---|
1109 | file->pos = size + off;
|
---|
1110 | newoff = (file->pos > OFF64_MAX) ? OFF64_MAX : file->pos;
|
---|
1111 |
|
---|
1112 | fibril_rwlock_read_unlock(&file->node->contents_rwlock);
|
---|
1113 | fibril_mutex_unlock(&file->lock);
|
---|
1114 | vfs_file_put(file);
|
---|
1115 | async_answer_2(rid, EOK, LOWER32(newoff), UPPER32(newoff));
|
---|
1116 | return;
|
---|
1117 | }
|
---|
1118 |
|
---|
1119 | fibril_mutex_unlock(&file->lock);
|
---|
1120 | vfs_file_put(file);
|
---|
1121 | async_answer_0(rid, EINVAL);
|
---|
1122 | }
|
---|
1123 |
|
---|
1124 | int vfs_truncate_internal(fs_handle_t fs_handle, service_id_t service_id,
|
---|
1125 | fs_index_t index, aoff64_t size)
|
---|
1126 | {
|
---|
1127 | async_exch_t *exch = vfs_exchange_grab(fs_handle);
|
---|
1128 | sysarg_t rc = async_req_4_0(exch, VFS_OUT_TRUNCATE,
|
---|
1129 | (sysarg_t) service_id, (sysarg_t) index, LOWER32(size),
|
---|
1130 | UPPER32(size));
|
---|
1131 | vfs_exchange_release(exch);
|
---|
1132 |
|
---|
1133 | return (int) rc;
|
---|
1134 | }
|
---|
1135 |
|
---|
1136 | void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
|
---|
1137 | {
|
---|
1138 | int fd = IPC_GET_ARG1(*request);
|
---|
1139 | aoff64_t size = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG2(*request),
|
---|
1140 | IPC_GET_ARG3(*request));
|
---|
1141 | int rc;
|
---|
1142 |
|
---|
1143 | vfs_file_t *file = vfs_file_get(fd);
|
---|
1144 | if (!file) {
|
---|
1145 | async_answer_0(rid, ENOENT);
|
---|
1146 | return;
|
---|
1147 | }
|
---|
1148 | fibril_mutex_lock(&file->lock);
|
---|
1149 |
|
---|
1150 | fibril_rwlock_write_lock(&file->node->contents_rwlock);
|
---|
1151 | rc = vfs_truncate_internal(file->node->fs_handle,
|
---|
1152 | file->node->service_id, file->node->index, size);
|
---|
1153 | if (rc == EOK)
|
---|
1154 | file->node->size = size;
|
---|
1155 | fibril_rwlock_write_unlock(&file->node->contents_rwlock);
|
---|
1156 |
|
---|
1157 | fibril_mutex_unlock(&file->lock);
|
---|
1158 | vfs_file_put(file);
|
---|
1159 | async_answer_0(rid, (sysarg_t)rc);
|
---|
1160 | }
|
---|
1161 |
|
---|
1162 | void vfs_fstat(ipc_callid_t rid, ipc_call_t *request)
|
---|
1163 | {
|
---|
1164 | int fd = IPC_GET_ARG1(*request);
|
---|
1165 | sysarg_t rc;
|
---|
1166 |
|
---|
1167 | vfs_file_t *file = vfs_file_get(fd);
|
---|
1168 | if (!file) {
|
---|
1169 | async_answer_0(rid, ENOENT);
|
---|
1170 | return;
|
---|
1171 | }
|
---|
1172 |
|
---|
1173 | ipc_callid_t callid;
|
---|
1174 | if (!async_data_read_receive(&callid, NULL)) {
|
---|
1175 | vfs_file_put(file);
|
---|
1176 | async_answer_0(callid, EINVAL);
|
---|
1177 | async_answer_0(rid, EINVAL);
|
---|
1178 | return;
|
---|
1179 | }
|
---|
1180 |
|
---|
1181 | fibril_mutex_lock(&file->lock);
|
---|
1182 |
|
---|
1183 | async_exch_t *exch = vfs_exchange_grab(file->node->fs_handle);
|
---|
1184 |
|
---|
1185 | aid_t msg;
|
---|
1186 | msg = async_send_3(exch, VFS_OUT_STAT, file->node->service_id,
|
---|
1187 | file->node->index, true, NULL);
|
---|
1188 | async_forward_fast(callid, exch, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
|
---|
1189 |
|
---|
1190 | vfs_exchange_release(exch);
|
---|
1191 |
|
---|
1192 | async_wait_for(msg, &rc);
|
---|
1193 |
|
---|
1194 | fibril_mutex_unlock(&file->lock);
|
---|
1195 | vfs_file_put(file);
|
---|
1196 | async_answer_0(rid, rc);
|
---|
1197 | }
|
---|
1198 |
|
---|
1199 | void vfs_stat(ipc_callid_t rid, ipc_call_t *request)
|
---|
1200 | {
|
---|
1201 | char *path;
|
---|
1202 | int rc = async_data_write_accept((void **) &path, true, 0, 0, 0, NULL);
|
---|
1203 | if (rc != EOK) {
|
---|
1204 | async_answer_0(rid, rc);
|
---|
1205 | return;
|
---|
1206 | }
|
---|
1207 |
|
---|
1208 | ipc_callid_t callid;
|
---|
1209 | if (!async_data_read_receive(&callid, NULL)) {
|
---|
1210 | free(path);
|
---|
1211 | async_answer_0(callid, EINVAL);
|
---|
1212 | async_answer_0(rid, EINVAL);
|
---|
1213 | return;
|
---|
1214 | }
|
---|
1215 |
|
---|
1216 | vfs_lookup_res_t lr;
|
---|
1217 | fibril_rwlock_read_lock(&namespace_rwlock);
|
---|
1218 | rc = vfs_lookup_internal(path, L_NONE, &lr, NULL);
|
---|
1219 | free(path);
|
---|
1220 | if (rc != EOK) {
|
---|
1221 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
1222 | async_answer_0(callid, rc);
|
---|
1223 | async_answer_0(rid, rc);
|
---|
1224 | return;
|
---|
1225 | }
|
---|
1226 | vfs_node_t *node = vfs_node_get(&lr);
|
---|
1227 | if (!node) {
|
---|
1228 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
1229 | async_answer_0(callid, ENOMEM);
|
---|
1230 | async_answer_0(rid, ENOMEM);
|
---|
1231 | return;
|
---|
1232 | }
|
---|
1233 |
|
---|
1234 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
1235 |
|
---|
1236 | async_exch_t *exch = vfs_exchange_grab(node->fs_handle);
|
---|
1237 |
|
---|
1238 | aid_t msg;
|
---|
1239 | msg = async_send_3(exch, VFS_OUT_STAT, node->service_id,
|
---|
1240 | node->index, false, NULL);
|
---|
1241 | async_forward_fast(callid, exch, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
|
---|
1242 |
|
---|
1243 | vfs_exchange_release(exch);
|
---|
1244 |
|
---|
1245 | sysarg_t rv;
|
---|
1246 | async_wait_for(msg, &rv);
|
---|
1247 |
|
---|
1248 | async_answer_0(rid, rv);
|
---|
1249 |
|
---|
1250 | vfs_node_put(node);
|
---|
1251 | }
|
---|
1252 |
|
---|
1253 | void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request)
|
---|
1254 | {
|
---|
1255 | int mode = IPC_GET_ARG1(*request);
|
---|
1256 |
|
---|
1257 | char *path;
|
---|
1258 | int rc = async_data_write_accept((void **) &path, true, 0, 0, 0, NULL);
|
---|
1259 | if (rc != EOK) {
|
---|
1260 | async_answer_0(rid, rc);
|
---|
1261 | return;
|
---|
1262 | }
|
---|
1263 |
|
---|
1264 | /* Ignore mode for now. */
|
---|
1265 | (void) mode;
|
---|
1266 |
|
---|
1267 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
1268 | int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE;
|
---|
1269 | rc = vfs_lookup_internal(path, lflag, NULL, NULL);
|
---|
1270 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1271 | free(path);
|
---|
1272 | async_answer_0(rid, rc);
|
---|
1273 | }
|
---|
1274 |
|
---|
1275 | void vfs_unlink(ipc_callid_t rid, ipc_call_t *request)
|
---|
1276 | {
|
---|
1277 | int lflag = IPC_GET_ARG1(*request);
|
---|
1278 |
|
---|
1279 | char *path;
|
---|
1280 | int rc = async_data_write_accept((void **) &path, true, 0, 0, 0, NULL);
|
---|
1281 | if (rc != EOK) {
|
---|
1282 | async_answer_0(rid, rc);
|
---|
1283 | return;
|
---|
1284 | }
|
---|
1285 |
|
---|
1286 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
1287 | lflag &= L_DIRECTORY; /* sanitize lflag */
|
---|
1288 | vfs_lookup_res_t lr;
|
---|
1289 | rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL);
|
---|
1290 | free(path);
|
---|
1291 | if (rc != EOK) {
|
---|
1292 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1293 | async_answer_0(rid, rc);
|
---|
1294 | return;
|
---|
1295 | }
|
---|
1296 |
|
---|
1297 | /*
|
---|
1298 | * The name has already been unlinked by vfs_lookup_internal().
|
---|
1299 | * We have to get and put the VFS node to ensure that it is
|
---|
1300 | * VFS_OUT_DESTROY'ed after the last reference to it is dropped.
|
---|
1301 | */
|
---|
1302 | vfs_node_t *node = vfs_node_get(&lr);
|
---|
1303 | fibril_mutex_lock(&nodes_mutex);
|
---|
1304 | node->lnkcnt--;
|
---|
1305 | fibril_mutex_unlock(&nodes_mutex);
|
---|
1306 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1307 | vfs_node_put(node);
|
---|
1308 | async_answer_0(rid, EOK);
|
---|
1309 | }
|
---|
1310 |
|
---|
1311 | void vfs_rename(ipc_callid_t rid, ipc_call_t *request)
|
---|
1312 | {
|
---|
1313 | /* Retrieve the old path. */
|
---|
1314 | char *old;
|
---|
1315 | int rc = async_data_write_accept((void **) &old, true, 0, 0, 0, NULL);
|
---|
1316 | if (rc != EOK) {
|
---|
1317 | async_answer_0(rid, rc);
|
---|
1318 | return;
|
---|
1319 | }
|
---|
1320 |
|
---|
1321 | /* Retrieve the new path. */
|
---|
1322 | char *new;
|
---|
1323 | rc = async_data_write_accept((void **) &new, true, 0, 0, 0, NULL);
|
---|
1324 | if (rc != EOK) {
|
---|
1325 | free(old);
|
---|
1326 | async_answer_0(rid, rc);
|
---|
1327 | return;
|
---|
1328 | }
|
---|
1329 |
|
---|
1330 | size_t olen;
|
---|
1331 | size_t nlen;
|
---|
1332 | char *oldc = canonify(old, &olen);
|
---|
1333 | char *newc = canonify(new, &nlen);
|
---|
1334 |
|
---|
1335 | if ((!oldc) || (!newc)) {
|
---|
1336 | async_answer_0(rid, EINVAL);
|
---|
1337 | free(old);
|
---|
1338 | free(new);
|
---|
1339 | return;
|
---|
1340 | }
|
---|
1341 |
|
---|
1342 | oldc[olen] = '\0';
|
---|
1343 | newc[nlen] = '\0';
|
---|
1344 |
|
---|
1345 | if ((!str_lcmp(newc, oldc, str_length(oldc))) &&
|
---|
1346 | ((newc[str_length(oldc)] == '/') ||
|
---|
1347 | (str_length(oldc) == 1) ||
|
---|
1348 | (str_length(oldc) == str_length(newc)))) {
|
---|
1349 | /*
|
---|
1350 | * oldc is a prefix of newc and either
|
---|
1351 | * - newc continues with a / where oldc ends, or
|
---|
1352 | * - oldc was / itself, or
|
---|
1353 | * - oldc and newc are equal.
|
---|
1354 | */
|
---|
1355 | async_answer_0(rid, EINVAL);
|
---|
1356 | free(old);
|
---|
1357 | free(new);
|
---|
1358 | return;
|
---|
1359 | }
|
---|
1360 |
|
---|
1361 | vfs_lookup_res_t old_lr;
|
---|
1362 | vfs_lookup_res_t new_lr;
|
---|
1363 | vfs_lookup_res_t new_par_lr;
|
---|
1364 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
1365 |
|
---|
1366 | /* Lookup the node belonging to the old file name. */
|
---|
1367 | rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL);
|
---|
1368 | if (rc != EOK) {
|
---|
1369 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1370 | async_answer_0(rid, rc);
|
---|
1371 | free(old);
|
---|
1372 | free(new);
|
---|
1373 | return;
|
---|
1374 | }
|
---|
1375 |
|
---|
1376 | vfs_node_t *old_node = vfs_node_get(&old_lr);
|
---|
1377 | if (!old_node) {
|
---|
1378 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1379 | async_answer_0(rid, ENOMEM);
|
---|
1380 | free(old);
|
---|
1381 | free(new);
|
---|
1382 | return;
|
---|
1383 | }
|
---|
1384 |
|
---|
1385 | /* Determine the path to the parent of the node with the new name. */
|
---|
1386 | char *parentc = str_dup(newc);
|
---|
1387 | if (!parentc) {
|
---|
1388 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1389 | vfs_node_put(old_node);
|
---|
1390 | async_answer_0(rid, rc);
|
---|
1391 | free(old);
|
---|
1392 | free(new);
|
---|
1393 | return;
|
---|
1394 | }
|
---|
1395 |
|
---|
1396 | char *lastsl = str_rchr(parentc + 1, '/');
|
---|
1397 | if (lastsl)
|
---|
1398 | *lastsl = '\0';
|
---|
1399 | else
|
---|
1400 | parentc[1] = '\0';
|
---|
1401 |
|
---|
1402 | /* Lookup parent of the new file name. */
|
---|
1403 | rc = vfs_lookup_internal(parentc, L_NONE, &new_par_lr, NULL);
|
---|
1404 | free(parentc); /* not needed anymore */
|
---|
1405 | if (rc != EOK) {
|
---|
1406 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1407 | vfs_node_put(old_node);
|
---|
1408 | async_answer_0(rid, rc);
|
---|
1409 | free(old);
|
---|
1410 | free(new);
|
---|
1411 | return;
|
---|
1412 | }
|
---|
1413 |
|
---|
1414 | /* Check whether linking to the same file system instance. */
|
---|
1415 | if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) ||
|
---|
1416 | (old_node->service_id != new_par_lr.triplet.service_id)) {
|
---|
1417 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1418 | vfs_node_put(old_node);
|
---|
1419 | async_answer_0(rid, EXDEV); /* different file systems */
|
---|
1420 | free(old);
|
---|
1421 | free(new);
|
---|
1422 | return;
|
---|
1423 | }
|
---|
1424 |
|
---|
1425 | /* Destroy the old link for the new name. */
|
---|
1426 | vfs_node_t *new_node = NULL;
|
---|
1427 | rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL);
|
---|
1428 |
|
---|
1429 | switch (rc) {
|
---|
1430 | case ENOENT:
|
---|
1431 | /* simply not in our way */
|
---|
1432 | break;
|
---|
1433 | case EOK:
|
---|
1434 | new_node = vfs_node_get(&new_lr);
|
---|
1435 | if (!new_node) {
|
---|
1436 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1437 | vfs_node_put(old_node);
|
---|
1438 | async_answer_0(rid, ENOMEM);
|
---|
1439 | free(old);
|
---|
1440 | free(new);
|
---|
1441 | return;
|
---|
1442 | }
|
---|
1443 | fibril_mutex_lock(&nodes_mutex);
|
---|
1444 | new_node->lnkcnt--;
|
---|
1445 | fibril_mutex_unlock(&nodes_mutex);
|
---|
1446 | break;
|
---|
1447 | default:
|
---|
1448 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1449 | vfs_node_put(old_node);
|
---|
1450 | async_answer_0(rid, ENOTEMPTY);
|
---|
1451 | free(old);
|
---|
1452 | free(new);
|
---|
1453 | return;
|
---|
1454 | }
|
---|
1455 |
|
---|
1456 | /* Create the new link for the new name. */
|
---|
1457 | rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index);
|
---|
1458 | if (rc != EOK) {
|
---|
1459 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1460 | vfs_node_put(old_node);
|
---|
1461 | if (new_node)
|
---|
1462 | vfs_node_put(new_node);
|
---|
1463 | async_answer_0(rid, rc);
|
---|
1464 | free(old);
|
---|
1465 | free(new);
|
---|
1466 | return;
|
---|
1467 | }
|
---|
1468 |
|
---|
1469 | fibril_mutex_lock(&nodes_mutex);
|
---|
1470 | old_node->lnkcnt++;
|
---|
1471 | fibril_mutex_unlock(&nodes_mutex);
|
---|
1472 |
|
---|
1473 | /* Destroy the link for the old name. */
|
---|
1474 | rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL);
|
---|
1475 | if (rc != EOK) {
|
---|
1476 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1477 | vfs_node_put(old_node);
|
---|
1478 | if (new_node)
|
---|
1479 | vfs_node_put(new_node);
|
---|
1480 | async_answer_0(rid, rc);
|
---|
1481 | free(old);
|
---|
1482 | free(new);
|
---|
1483 | return;
|
---|
1484 | }
|
---|
1485 |
|
---|
1486 | fibril_mutex_lock(&nodes_mutex);
|
---|
1487 | old_node->lnkcnt--;
|
---|
1488 | fibril_mutex_unlock(&nodes_mutex);
|
---|
1489 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1490 | vfs_node_put(old_node);
|
---|
1491 |
|
---|
1492 | if (new_node)
|
---|
1493 | vfs_node_put(new_node);
|
---|
1494 |
|
---|
1495 | free(old);
|
---|
1496 | free(new);
|
---|
1497 | async_answer_0(rid, EOK);
|
---|
1498 | }
|
---|
1499 |
|
---|
1500 | void vfs_dup(ipc_callid_t rid, ipc_call_t *request)
|
---|
1501 | {
|
---|
1502 | int oldfd = IPC_GET_ARG1(*request);
|
---|
1503 | int newfd = IPC_GET_ARG2(*request);
|
---|
1504 |
|
---|
1505 | /* If the file descriptors are the same, do nothing. */
|
---|
1506 | if (oldfd == newfd) {
|
---|
1507 | async_answer_1(rid, EOK, newfd);
|
---|
1508 | return;
|
---|
1509 | }
|
---|
1510 |
|
---|
1511 | /* Lookup the file structure corresponding to oldfd. */
|
---|
1512 | vfs_file_t *oldfile = vfs_file_get(oldfd);
|
---|
1513 | if (!oldfile) {
|
---|
1514 | async_answer_0(rid, EBADF);
|
---|
1515 | return;
|
---|
1516 | }
|
---|
1517 |
|
---|
1518 | /*
|
---|
1519 | * Lock the open file structure so that no other thread can manipulate
|
---|
1520 | * the same open file at a time.
|
---|
1521 | */
|
---|
1522 | fibril_mutex_lock(&oldfile->lock);
|
---|
1523 |
|
---|
1524 | /* Make sure newfd is closed. */
|
---|
1525 | (void) vfs_fd_free(newfd);
|
---|
1526 |
|
---|
1527 | /* Assign the old file to newfd. */
|
---|
1528 | int ret = vfs_fd_assign(oldfile, newfd);
|
---|
1529 | fibril_mutex_unlock(&oldfile->lock);
|
---|
1530 | vfs_file_put(oldfile);
|
---|
1531 |
|
---|
1532 | if (ret != EOK)
|
---|
1533 | async_answer_0(rid, ret);
|
---|
1534 | else
|
---|
1535 | async_answer_1(rid, EOK, newfd);
|
---|
1536 | }
|
---|
1537 |
|
---|
1538 | void vfs_wait_handle(ipc_callid_t rid, ipc_call_t *request)
|
---|
1539 | {
|
---|
1540 | int fd = vfs_wait_handle_internal();
|
---|
1541 | async_answer_1(rid, EOK, fd);
|
---|
1542 | }
|
---|
1543 |
|
---|
1544 | void vfs_get_mtab(ipc_callid_t rid, ipc_call_t *request)
|
---|
1545 | {
|
---|
1546 | ipc_callid_t callid;
|
---|
1547 | ipc_call_t data;
|
---|
1548 | sysarg_t rc = EOK;
|
---|
1549 | size_t len;
|
---|
1550 |
|
---|
1551 | fibril_mutex_lock(&mtab_list_lock);
|
---|
1552 |
|
---|
1553 | /* Send to the caller the number of mounted filesystems */
|
---|
1554 | callid = async_get_call(&data);
|
---|
1555 | if (IPC_GET_IMETHOD(data) != VFS_IN_PING) {
|
---|
1556 | rc = ENOTSUP;
|
---|
1557 | async_answer_0(callid, rc);
|
---|
1558 | goto exit;
|
---|
1559 | }
|
---|
1560 | async_answer_1(callid, EOK, mtab_size);
|
---|
1561 |
|
---|
1562 | list_foreach(mtab_list, cur) {
|
---|
1563 | mtab_ent_t *mtab_ent = list_get_instance(cur, mtab_ent_t,
|
---|
1564 | link);
|
---|
1565 |
|
---|
1566 | rc = ENOTSUP;
|
---|
1567 |
|
---|
1568 | if (!async_data_read_receive(&callid, &len)) {
|
---|
1569 | async_answer_0(callid, rc);
|
---|
1570 | goto exit;
|
---|
1571 | }
|
---|
1572 |
|
---|
1573 | (void) async_data_read_finalize(callid, mtab_ent->mp,
|
---|
1574 | str_size(mtab_ent->mp));
|
---|
1575 |
|
---|
1576 | if (!async_data_read_receive(&callid, &len)) {
|
---|
1577 | async_answer_0(callid, rc);
|
---|
1578 | goto exit;
|
---|
1579 | }
|
---|
1580 |
|
---|
1581 | (void) async_data_read_finalize(callid, mtab_ent->opts,
|
---|
1582 | str_size(mtab_ent->opts));
|
---|
1583 |
|
---|
1584 | if (!async_data_read_receive(&callid, &len)) {
|
---|
1585 | async_answer_0(callid, rc);
|
---|
1586 | goto exit;
|
---|
1587 | }
|
---|
1588 |
|
---|
1589 | (void) async_data_read_finalize(callid, mtab_ent->fs_name,
|
---|
1590 | str_size(mtab_ent->fs_name));
|
---|
1591 |
|
---|
1592 | callid = async_get_call(&data);
|
---|
1593 |
|
---|
1594 | if (IPC_GET_IMETHOD(data) != VFS_IN_PING) {
|
---|
1595 | async_answer_0(callid, rc);
|
---|
1596 | goto exit;
|
---|
1597 | }
|
---|
1598 |
|
---|
1599 | rc = EOK;
|
---|
1600 | async_answer_2(callid, rc, mtab_ent->instance,
|
---|
1601 | mtab_ent->service_id);
|
---|
1602 | }
|
---|
1603 |
|
---|
1604 | exit:
|
---|
1605 | fibril_mutex_unlock(&mtab_list_lock);
|
---|
1606 | async_answer_0(rid, rc);
|
---|
1607 | }
|
---|
1608 |
|
---|
1609 | /**
|
---|
1610 | * @}
|
---|
1611 | */
|
---|