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 <ipc/ipc.h>
|
---|
40 | #include <async.h>
|
---|
41 | #include <errno.h>
|
---|
42 | #include <stdio.h>
|
---|
43 | #include <stdlib.h>
|
---|
44 | #include <string.h>
|
---|
45 | #include <bool.h>
|
---|
46 | #include <fibril_synch.h>
|
---|
47 | #include <adt/list.h>
|
---|
48 | #include <unistd.h>
|
---|
49 | #include <ctype.h>
|
---|
50 | #include <fcntl.h>
|
---|
51 | #include <assert.h>
|
---|
52 | #include <vfs/canonify.h>
|
---|
53 |
|
---|
54 | /* Forward declarations of static functions. */
|
---|
55 | static int vfs_truncate_internal(fs_handle_t, dev_handle_t, fs_index_t, size_t);
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * This rwlock prevents the race between a triplet-to-VFS-node resolution and a
|
---|
59 | * concurrent VFS operation which modifies the file system namespace.
|
---|
60 | */
|
---|
61 | FIBRIL_RWLOCK_INITIALIZE(namespace_rwlock);
|
---|
62 |
|
---|
63 | vfs_pair_t rootfs = {
|
---|
64 | .fs_handle = 0,
|
---|
65 | .dev_handle = 0
|
---|
66 | };
|
---|
67 |
|
---|
68 | static void vfs_mount_internal(ipc_callid_t rid, dev_handle_t dev_handle,
|
---|
69 | fs_handle_t fs_handle, char *mp, char *opts)
|
---|
70 | {
|
---|
71 | vfs_lookup_res_t mp_res;
|
---|
72 | vfs_lookup_res_t mr_res;
|
---|
73 | vfs_node_t *mp_node = NULL;
|
---|
74 | vfs_node_t *mr_node;
|
---|
75 | fs_index_t rindex;
|
---|
76 | size_t rsize;
|
---|
77 | unsigned rlnkcnt;
|
---|
78 | ipcarg_t rc;
|
---|
79 | int phone;
|
---|
80 | aid_t msg;
|
---|
81 | ipc_call_t answer;
|
---|
82 |
|
---|
83 | /* Resolve the path to the mountpoint. */
|
---|
84 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
85 | if (rootfs.fs_handle) {
|
---|
86 | /* We already have the root FS. */
|
---|
87 | if (str_cmp(mp, "/") == 0) {
|
---|
88 | /* Trying to mount root FS over root FS */
|
---|
89 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
90 | ipc_answer_0(rid, EBUSY);
|
---|
91 | return;
|
---|
92 | }
|
---|
93 |
|
---|
94 | rc = vfs_lookup_internal(mp, L_MP, &mp_res, NULL);
|
---|
95 | if (rc != EOK) {
|
---|
96 | /* The lookup failed for some reason. */
|
---|
97 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
98 | ipc_answer_0(rid, rc);
|
---|
99 | return;
|
---|
100 | }
|
---|
101 |
|
---|
102 | mp_node = vfs_node_get(&mp_res);
|
---|
103 | if (!mp_node) {
|
---|
104 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
105 | ipc_answer_0(rid, ENOMEM);
|
---|
106 | return;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /*
|
---|
110 | * Now we hold a reference to mp_node.
|
---|
111 | * It will be dropped upon the corresponding VFS_IN_UNMOUNT.
|
---|
112 | * This prevents the mount point from being deleted.
|
---|
113 | */
|
---|
114 | } else {
|
---|
115 | /* We still don't have the root file system mounted. */
|
---|
116 | if (str_cmp(mp, "/") == 0) {
|
---|
117 | /*
|
---|
118 | * For this simple, but important case,
|
---|
119 | * we are almost done.
|
---|
120 | */
|
---|
121 |
|
---|
122 | /* Tell the mountee that it is being mounted. */
|
---|
123 | phone = vfs_grab_phone(fs_handle);
|
---|
124 | msg = async_send_1(phone, VFS_OUT_MOUNTED,
|
---|
125 | (ipcarg_t) dev_handle, &answer);
|
---|
126 | /* send the mount options */
|
---|
127 | rc = async_data_write_start(phone, (void *)opts,
|
---|
128 | str_size(opts));
|
---|
129 | if (rc != EOK) {
|
---|
130 | async_wait_for(msg, NULL);
|
---|
131 | vfs_release_phone(phone);
|
---|
132 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
133 | ipc_answer_0(rid, rc);
|
---|
134 | return;
|
---|
135 | }
|
---|
136 | async_wait_for(msg, &rc);
|
---|
137 | vfs_release_phone(phone);
|
---|
138 |
|
---|
139 | if (rc != EOK) {
|
---|
140 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
141 | ipc_answer_0(rid, rc);
|
---|
142 | return;
|
---|
143 | }
|
---|
144 |
|
---|
145 | rindex = (fs_index_t) IPC_GET_ARG1(answer);
|
---|
146 | rsize = (size_t) IPC_GET_ARG2(answer);
|
---|
147 | rlnkcnt = (unsigned) IPC_GET_ARG3(answer);
|
---|
148 |
|
---|
149 | mr_res.triplet.fs_handle = fs_handle;
|
---|
150 | mr_res.triplet.dev_handle = dev_handle;
|
---|
151 | mr_res.triplet.index = rindex;
|
---|
152 | mr_res.size = rsize;
|
---|
153 | mr_res.lnkcnt = rlnkcnt;
|
---|
154 | mr_res.type = VFS_NODE_DIRECTORY;
|
---|
155 |
|
---|
156 | rootfs.fs_handle = fs_handle;
|
---|
157 | rootfs.dev_handle = dev_handle;
|
---|
158 |
|
---|
159 | /* Add reference to the mounted root. */
|
---|
160 | mr_node = vfs_node_get(&mr_res);
|
---|
161 | assert(mr_node);
|
---|
162 |
|
---|
163 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
164 | ipc_answer_0(rid, rc);
|
---|
165 | return;
|
---|
166 | } else {
|
---|
167 | /*
|
---|
168 | * We can't resolve this without the root filesystem
|
---|
169 | * being mounted first.
|
---|
170 | */
|
---|
171 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
172 | ipc_answer_0(rid, ENOENT);
|
---|
173 | return;
|
---|
174 | }
|
---|
175 | }
|
---|
176 |
|
---|
177 | /*
|
---|
178 | * At this point, we have all necessary pieces: file system and device
|
---|
179 | * handles, and we know the mount point VFS node.
|
---|
180 | */
|
---|
181 |
|
---|
182 | int mountee_phone = vfs_grab_phone(fs_handle);
|
---|
183 | assert(mountee_phone >= 0);
|
---|
184 |
|
---|
185 | phone = vfs_grab_phone(mp_res.triplet.fs_handle);
|
---|
186 | msg = async_send_4(phone, VFS_OUT_MOUNT,
|
---|
187 | (ipcarg_t) mp_res.triplet.dev_handle,
|
---|
188 | (ipcarg_t) mp_res.triplet.index,
|
---|
189 | (ipcarg_t) fs_handle,
|
---|
190 | (ipcarg_t) dev_handle, &answer);
|
---|
191 |
|
---|
192 | /* send connection */
|
---|
193 | rc = async_req_1_0(phone, IPC_M_CONNECTION_CLONE, mountee_phone);
|
---|
194 | if (rc != EOK) {
|
---|
195 | async_wait_for(msg, NULL);
|
---|
196 | vfs_release_phone(mountee_phone);
|
---|
197 | vfs_release_phone(phone);
|
---|
198 | /* Mount failed, drop reference to mp_node. */
|
---|
199 | if (mp_node)
|
---|
200 | vfs_node_put(mp_node);
|
---|
201 | ipc_answer_0(rid, rc);
|
---|
202 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
203 | return;
|
---|
204 | }
|
---|
205 |
|
---|
206 | vfs_release_phone(mountee_phone);
|
---|
207 |
|
---|
208 | /* send the mount options */
|
---|
209 | rc = async_data_write_start(phone, (void *)opts, str_size(opts));
|
---|
210 | if (rc != EOK) {
|
---|
211 | async_wait_for(msg, NULL);
|
---|
212 | vfs_release_phone(phone);
|
---|
213 | /* Mount failed, drop reference to mp_node. */
|
---|
214 | if (mp_node)
|
---|
215 | vfs_node_put(mp_node);
|
---|
216 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
217 | ipc_answer_0(rid, rc);
|
---|
218 | return;
|
---|
219 | }
|
---|
220 | async_wait_for(msg, &rc);
|
---|
221 | vfs_release_phone(phone);
|
---|
222 |
|
---|
223 | if (rc == EOK) {
|
---|
224 | rindex = (fs_index_t) IPC_GET_ARG1(answer);
|
---|
225 | rsize = (size_t) IPC_GET_ARG2(answer);
|
---|
226 | rlnkcnt = (unsigned) IPC_GET_ARG3(answer);
|
---|
227 |
|
---|
228 | mr_res.triplet.fs_handle = fs_handle;
|
---|
229 | mr_res.triplet.dev_handle = dev_handle;
|
---|
230 | mr_res.triplet.index = rindex;
|
---|
231 | mr_res.size = rsize;
|
---|
232 | mr_res.lnkcnt = rlnkcnt;
|
---|
233 | mr_res.type = VFS_NODE_DIRECTORY;
|
---|
234 |
|
---|
235 | /* Add reference to the mounted root. */
|
---|
236 | mr_node = vfs_node_get(&mr_res);
|
---|
237 | assert(mr_node);
|
---|
238 | } else {
|
---|
239 | /* Mount failed, drop reference to mp_node. */
|
---|
240 | if (mp_node)
|
---|
241 | vfs_node_put(mp_node);
|
---|
242 | }
|
---|
243 |
|
---|
244 | ipc_answer_0(rid, rc);
|
---|
245 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
246 | }
|
---|
247 |
|
---|
248 | void vfs_mount(ipc_callid_t rid, ipc_call_t *request)
|
---|
249 | {
|
---|
250 | /*
|
---|
251 | * We expect the library to do the device-name to device-handle
|
---|
252 | * translation for us, thus the device handle will arrive as ARG1
|
---|
253 | * in the request.
|
---|
254 | */
|
---|
255 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
|
---|
256 |
|
---|
257 | /*
|
---|
258 | * Mount flags are passed as ARG2.
|
---|
259 | */
|
---|
260 | unsigned int flags = (unsigned int) IPC_GET_ARG2(*request);
|
---|
261 |
|
---|
262 | /*
|
---|
263 | * For now, don't make use of ARG3, but it can be used to
|
---|
264 | * carry mount options in the future.
|
---|
265 | */
|
---|
266 |
|
---|
267 | /* We want the client to send us the mount point. */
|
---|
268 | char *mp;
|
---|
269 | int rc = async_string_receive(&mp, MAX_PATH_LEN, NULL);
|
---|
270 | if (rc != EOK) {
|
---|
271 | ipc_answer_0(rid, rc);
|
---|
272 | return;
|
---|
273 | }
|
---|
274 |
|
---|
275 | /* Now we expect to receive the mount options. */
|
---|
276 | char *opts;
|
---|
277 | rc = async_string_receive(&opts, MAX_MNTOPTS_LEN, NULL);
|
---|
278 | if (rc != EOK) {
|
---|
279 | free(mp);
|
---|
280 | ipc_answer_0(rid, rc);
|
---|
281 | return;
|
---|
282 | }
|
---|
283 |
|
---|
284 | /*
|
---|
285 | * Now, we expect the client to send us data with the name of the file
|
---|
286 | * system.
|
---|
287 | */
|
---|
288 | char *fs_name;
|
---|
289 | rc = async_string_receive(&fs_name, FS_NAME_MAXLEN, NULL);
|
---|
290 | if (rc != EOK) {
|
---|
291 | free(mp);
|
---|
292 | free(opts);
|
---|
293 | ipc_answer_0(rid, rc);
|
---|
294 | return;
|
---|
295 | }
|
---|
296 |
|
---|
297 | /*
|
---|
298 | * Wait for IPC_M_PING so that we can return an error if we don't know
|
---|
299 | * fs_name.
|
---|
300 | */
|
---|
301 | ipc_call_t data;
|
---|
302 | ipc_callid_t callid = async_get_call(&data);
|
---|
303 | if (IPC_GET_METHOD(data) != IPC_M_PING) {
|
---|
304 | ipc_answer_0(callid, ENOTSUP);
|
---|
305 | ipc_answer_0(rid, ENOTSUP);
|
---|
306 | free(mp);
|
---|
307 | free(opts);
|
---|
308 | free(fs_name);
|
---|
309 | return;
|
---|
310 | }
|
---|
311 |
|
---|
312 | /*
|
---|
313 | * Check if we know a file system with the same name as is in fs_name.
|
---|
314 | * This will also give us its file system handle.
|
---|
315 | */
|
---|
316 | fibril_mutex_lock(&fs_head_lock);
|
---|
317 | fs_handle_t fs_handle;
|
---|
318 | recheck:
|
---|
319 | fs_handle = fs_name_to_handle(fs_name, false);
|
---|
320 | if (!fs_handle) {
|
---|
321 | if (flags & IPC_FLAG_BLOCKING) {
|
---|
322 | fibril_condvar_wait(&fs_head_cv, &fs_head_lock);
|
---|
323 | goto recheck;
|
---|
324 | }
|
---|
325 |
|
---|
326 | fibril_mutex_unlock(&fs_head_lock);
|
---|
327 | ipc_answer_0(callid, ENOENT);
|
---|
328 | ipc_answer_0(rid, ENOENT);
|
---|
329 | free(mp);
|
---|
330 | free(fs_name);
|
---|
331 | free(opts);
|
---|
332 | return;
|
---|
333 | }
|
---|
334 | fibril_mutex_unlock(&fs_head_lock);
|
---|
335 |
|
---|
336 | /* Acknowledge that we know fs_name. */
|
---|
337 | ipc_answer_0(callid, EOK);
|
---|
338 |
|
---|
339 | /* Do the mount */
|
---|
340 | vfs_mount_internal(rid, dev_handle, fs_handle, mp, opts);
|
---|
341 | free(mp);
|
---|
342 | free(fs_name);
|
---|
343 | free(opts);
|
---|
344 | }
|
---|
345 |
|
---|
346 | void vfs_unmount(ipc_callid_t rid, ipc_call_t *request)
|
---|
347 | {
|
---|
348 | int rc;
|
---|
349 | char *mp;
|
---|
350 | vfs_lookup_res_t mp_res;
|
---|
351 | vfs_lookup_res_t mr_res;
|
---|
352 | vfs_node_t *mp_node;
|
---|
353 | vfs_node_t *mr_node;
|
---|
354 | int phone;
|
---|
355 |
|
---|
356 | /*
|
---|
357 | * Receive the mount point path.
|
---|
358 | */
|
---|
359 | rc = async_string_receive(&mp, MAX_PATH_LEN, NULL);
|
---|
360 | if (rc != EOK)
|
---|
361 | ipc_answer_0(rid, rc);
|
---|
362 |
|
---|
363 | /*
|
---|
364 | * Taking the namespace lock will do two things for us. First, it will
|
---|
365 | * prevent races with other lookup operations. Second, it will stop new
|
---|
366 | * references to already existing VFS nodes and creation of new VFS
|
---|
367 | * nodes. This is because new references are added as a result of some
|
---|
368 | * lookup operation or at least of some operation which is protected by
|
---|
369 | * the namespace lock.
|
---|
370 | */
|
---|
371 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
372 |
|
---|
373 | /*
|
---|
374 | * Lookup the mounted root and instantiate it.
|
---|
375 | */
|
---|
376 | rc = vfs_lookup_internal(mp, L_ROOT, &mr_res, NULL);
|
---|
377 | if (rc != EOK) {
|
---|
378 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
379 | free(mp);
|
---|
380 | ipc_answer_0(rid, rc);
|
---|
381 | return;
|
---|
382 | }
|
---|
383 | mr_node = vfs_node_get(&mr_res);
|
---|
384 | if (!mr_node) {
|
---|
385 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
386 | free(mp);
|
---|
387 | ipc_answer_0(rid, ENOMEM);
|
---|
388 | return;
|
---|
389 | }
|
---|
390 |
|
---|
391 | /*
|
---|
392 | * Count the total number of references for the mounted file system. We
|
---|
393 | * are expecting at least two. One which we got above and one which we
|
---|
394 | * got when the file system was mounted. If we find more, it means that
|
---|
395 | * the file system cannot be gracefully unmounted at the moment because
|
---|
396 | * someone is working with it.
|
---|
397 | */
|
---|
398 | if (vfs_nodes_refcount_sum_get(mr_node->fs_handle,
|
---|
399 | mr_node->dev_handle) != 2) {
|
---|
400 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
401 | vfs_node_put(mr_node);
|
---|
402 | free(mp);
|
---|
403 | ipc_answer_0(rid, EBUSY);
|
---|
404 | return;
|
---|
405 | }
|
---|
406 |
|
---|
407 | if (str_cmp(mp, "/") == 0) {
|
---|
408 |
|
---|
409 | /*
|
---|
410 | * Unmounting the root file system.
|
---|
411 | *
|
---|
412 | * In this case, there is no mount point node and we send
|
---|
413 | * VFS_OUT_UNMOUNTED directly to the mounted file system.
|
---|
414 | */
|
---|
415 |
|
---|
416 | free(mp);
|
---|
417 | phone = vfs_grab_phone(mr_node->fs_handle);
|
---|
418 | rc = async_req_1_0(phone, VFS_OUT_UNMOUNTED,
|
---|
419 | mr_node->dev_handle);
|
---|
420 | vfs_release_phone(phone);
|
---|
421 | if (rc != EOK) {
|
---|
422 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
423 | vfs_node_put(mr_node);
|
---|
424 | ipc_answer_0(rid, rc);
|
---|
425 | return;
|
---|
426 | }
|
---|
427 | rootfs.fs_handle = 0;
|
---|
428 | rootfs.dev_handle = 0;
|
---|
429 | } else {
|
---|
430 |
|
---|
431 | /*
|
---|
432 | * Unmounting a non-root file system.
|
---|
433 | *
|
---|
434 | * We have a regular mount point node representing the parent
|
---|
435 | * file system, so we delegate the operation to it.
|
---|
436 | */
|
---|
437 |
|
---|
438 | rc = vfs_lookup_internal(mp, L_MP, &mp_res, NULL);
|
---|
439 | free(mp);
|
---|
440 | if (rc != EOK) {
|
---|
441 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
442 | vfs_node_put(mr_node);
|
---|
443 | ipc_answer_0(rid, rc);
|
---|
444 | return;
|
---|
445 | }
|
---|
446 | vfs_node_t *mp_node = vfs_node_get(&mp_res);
|
---|
447 | if (!mp_node) {
|
---|
448 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
449 | vfs_node_put(mr_node);
|
---|
450 | ipc_answer_0(rid, ENOMEM);
|
---|
451 | return;
|
---|
452 | }
|
---|
453 |
|
---|
454 | phone = vfs_grab_phone(mp_node->fs_handle);
|
---|
455 | rc = async_req_2_0(phone, VFS_OUT_UNMOUNT, mp_node->dev_handle,
|
---|
456 | mp_node->index);
|
---|
457 | vfs_release_phone(phone);
|
---|
458 | if (rc != EOK) {
|
---|
459 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
460 | vfs_node_put(mp_node);
|
---|
461 | vfs_node_put(mr_node);
|
---|
462 | ipc_answer_0(rid, rc);
|
---|
463 | return;
|
---|
464 | }
|
---|
465 |
|
---|
466 | /* Drop the reference we got above. */
|
---|
467 | vfs_node_put(mp_node);
|
---|
468 | /* Drop the reference from when the file system was mounted. */
|
---|
469 | vfs_node_put(mp_node);
|
---|
470 | }
|
---|
471 |
|
---|
472 |
|
---|
473 | /*
|
---|
474 | * All went well, the mounted file system was successfully unmounted.
|
---|
475 | * The only thing left is to forget the unmounted root VFS node.
|
---|
476 | */
|
---|
477 | vfs_node_forget(mr_node);
|
---|
478 |
|
---|
479 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
480 | ipc_answer_0(rid, EOK);
|
---|
481 | }
|
---|
482 |
|
---|
483 | void vfs_open(ipc_callid_t rid, ipc_call_t *request)
|
---|
484 | {
|
---|
485 | if (!vfs_files_init()) {
|
---|
486 | ipc_answer_0(rid, ENOMEM);
|
---|
487 | return;
|
---|
488 | }
|
---|
489 |
|
---|
490 | /*
|
---|
491 | * The POSIX interface is open(path, oflag, mode).
|
---|
492 | * We can receive oflags and mode along with the VFS_IN_OPEN call;
|
---|
493 | * the path will need to arrive in another call.
|
---|
494 | *
|
---|
495 | * We also receive one private, non-POSIX set of flags called lflag
|
---|
496 | * used to pass information to vfs_lookup_internal().
|
---|
497 | */
|
---|
498 | int lflag = IPC_GET_ARG1(*request);
|
---|
499 | int oflag = IPC_GET_ARG2(*request);
|
---|
500 | int mode = IPC_GET_ARG3(*request);
|
---|
501 | size_t len;
|
---|
502 |
|
---|
503 | /* Ignore mode for now. */
|
---|
504 | (void) mode;
|
---|
505 |
|
---|
506 | /*
|
---|
507 | * Make sure that we are called with exactly one of L_FILE and
|
---|
508 | * L_DIRECTORY. Make sure that the user does not pass L_OPEN,
|
---|
509 | * L_ROOT or L_MP.
|
---|
510 | */
|
---|
511 | if (((lflag & (L_FILE | L_DIRECTORY)) == 0) ||
|
---|
512 | ((lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY)) ||
|
---|
513 | (lflag & (L_OPEN | L_ROOT | L_MP))) {
|
---|
514 | ipc_answer_0(rid, EINVAL);
|
---|
515 | return;
|
---|
516 | }
|
---|
517 |
|
---|
518 | if (oflag & O_CREAT)
|
---|
519 | lflag |= L_CREATE;
|
---|
520 | if (oflag & O_EXCL)
|
---|
521 | lflag |= L_EXCLUSIVE;
|
---|
522 |
|
---|
523 | char *path;
|
---|
524 | int rc = async_string_receive(&path, 0, NULL);
|
---|
525 | if (rc != EOK) {
|
---|
526 | ipc_answer_0(rid, rc);
|
---|
527 | return;
|
---|
528 | }
|
---|
529 |
|
---|
530 | /*
|
---|
531 | * Avoid the race condition in which the file can be deleted before we
|
---|
532 | * find/create-and-lock the VFS node corresponding to the looked-up
|
---|
533 | * triplet.
|
---|
534 | */
|
---|
535 | if (lflag & L_CREATE)
|
---|
536 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
537 | else
|
---|
538 | fibril_rwlock_read_lock(&namespace_rwlock);
|
---|
539 |
|
---|
540 | /* The path is now populated and we can call vfs_lookup_internal(). */
|
---|
541 | vfs_lookup_res_t lr;
|
---|
542 | rc = vfs_lookup_internal(path, lflag | L_OPEN, &lr, NULL);
|
---|
543 | if (rc != EOK) {
|
---|
544 | if (lflag & L_CREATE)
|
---|
545 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
546 | else
|
---|
547 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
548 | ipc_answer_0(rid, rc);
|
---|
549 | free(path);
|
---|
550 | return;
|
---|
551 | }
|
---|
552 |
|
---|
553 | /* Path is no longer needed. */
|
---|
554 | free(path);
|
---|
555 |
|
---|
556 | vfs_node_t *node = vfs_node_get(&lr);
|
---|
557 | if (lflag & L_CREATE)
|
---|
558 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
559 | else
|
---|
560 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
561 |
|
---|
562 | /* Truncate the file if requested and if necessary. */
|
---|
563 | if (oflag & O_TRUNC) {
|
---|
564 | fibril_rwlock_write_lock(&node->contents_rwlock);
|
---|
565 | if (node->size) {
|
---|
566 | rc = vfs_truncate_internal(node->fs_handle,
|
---|
567 | node->dev_handle, node->index, 0);
|
---|
568 | if (rc) {
|
---|
569 | fibril_rwlock_write_unlock(&node->contents_rwlock);
|
---|
570 | vfs_node_put(node);
|
---|
571 | ipc_answer_0(rid, rc);
|
---|
572 | return;
|
---|
573 | }
|
---|
574 | node->size = 0;
|
---|
575 | }
|
---|
576 | fibril_rwlock_write_unlock(&node->contents_rwlock);
|
---|
577 | }
|
---|
578 |
|
---|
579 | /*
|
---|
580 | * Get ourselves a file descriptor and the corresponding vfs_file_t
|
---|
581 | * structure.
|
---|
582 | */
|
---|
583 | int fd = vfs_fd_alloc((oflag & O_DESC) != 0);
|
---|
584 | if (fd < 0) {
|
---|
585 | vfs_node_put(node);
|
---|
586 | ipc_answer_0(rid, fd);
|
---|
587 | return;
|
---|
588 | }
|
---|
589 | vfs_file_t *file = vfs_file_get(fd);
|
---|
590 | file->node = node;
|
---|
591 | if (oflag & O_APPEND)
|
---|
592 | file->append = true;
|
---|
593 |
|
---|
594 | /*
|
---|
595 | * The following increase in reference count is for the fact that the
|
---|
596 | * file is being opened and that a file structure is pointing to it.
|
---|
597 | * It is necessary so that the file will not disappear when
|
---|
598 | * vfs_node_put() is called. The reference will be dropped by the
|
---|
599 | * respective VFS_IN_CLOSE.
|
---|
600 | */
|
---|
601 | vfs_node_addref(node);
|
---|
602 | vfs_node_put(node);
|
---|
603 |
|
---|
604 | /* Success! Return the new file descriptor to the client. */
|
---|
605 | ipc_answer_1(rid, EOK, fd);
|
---|
606 | }
|
---|
607 |
|
---|
608 | void vfs_open_node(ipc_callid_t rid, ipc_call_t *request)
|
---|
609 | {
|
---|
610 | // FIXME: check for sanity of the supplied fs, dev and index
|
---|
611 |
|
---|
612 | if (!vfs_files_init()) {
|
---|
613 | ipc_answer_0(rid, ENOMEM);
|
---|
614 | return;
|
---|
615 | }
|
---|
616 |
|
---|
617 | /*
|
---|
618 | * The interface is open_node(fs, dev, index, oflag).
|
---|
619 | */
|
---|
620 | vfs_lookup_res_t lr;
|
---|
621 |
|
---|
622 | lr.triplet.fs_handle = IPC_GET_ARG1(*request);
|
---|
623 | lr.triplet.dev_handle = IPC_GET_ARG2(*request);
|
---|
624 | lr.triplet.index = IPC_GET_ARG3(*request);
|
---|
625 | int oflag = IPC_GET_ARG4(*request);
|
---|
626 |
|
---|
627 | fibril_rwlock_read_lock(&namespace_rwlock);
|
---|
628 |
|
---|
629 | int rc = vfs_open_node_internal(&lr);
|
---|
630 | if (rc != EOK) {
|
---|
631 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
632 | ipc_answer_0(rid, rc);
|
---|
633 | return;
|
---|
634 | }
|
---|
635 |
|
---|
636 | vfs_node_t *node = vfs_node_get(&lr);
|
---|
637 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
638 |
|
---|
639 | /* Truncate the file if requested and if necessary. */
|
---|
640 | if (oflag & O_TRUNC) {
|
---|
641 | fibril_rwlock_write_lock(&node->contents_rwlock);
|
---|
642 | if (node->size) {
|
---|
643 | rc = vfs_truncate_internal(node->fs_handle,
|
---|
644 | node->dev_handle, node->index, 0);
|
---|
645 | if (rc) {
|
---|
646 | fibril_rwlock_write_unlock(&node->contents_rwlock);
|
---|
647 | vfs_node_put(node);
|
---|
648 | ipc_answer_0(rid, rc);
|
---|
649 | return;
|
---|
650 | }
|
---|
651 | node->size = 0;
|
---|
652 | }
|
---|
653 | fibril_rwlock_write_unlock(&node->contents_rwlock);
|
---|
654 | }
|
---|
655 |
|
---|
656 | /*
|
---|
657 | * Get ourselves a file descriptor and the corresponding vfs_file_t
|
---|
658 | * structure.
|
---|
659 | */
|
---|
660 | int fd = vfs_fd_alloc((oflag & O_DESC) != 0);
|
---|
661 | if (fd < 0) {
|
---|
662 | vfs_node_put(node);
|
---|
663 | ipc_answer_0(rid, fd);
|
---|
664 | return;
|
---|
665 | }
|
---|
666 | vfs_file_t *file = vfs_file_get(fd);
|
---|
667 | file->node = node;
|
---|
668 | if (oflag & O_APPEND)
|
---|
669 | file->append = true;
|
---|
670 |
|
---|
671 | /*
|
---|
672 | * The following increase in reference count is for the fact that the
|
---|
673 | * file is being opened and that a file structure is pointing to it.
|
---|
674 | * It is necessary so that the file will not disappear when
|
---|
675 | * vfs_node_put() is called. The reference will be dropped by the
|
---|
676 | * respective VFS_IN_CLOSE.
|
---|
677 | */
|
---|
678 | vfs_node_addref(node);
|
---|
679 | vfs_node_put(node);
|
---|
680 |
|
---|
681 | /* Success! Return the new file descriptor to the client. */
|
---|
682 | ipc_answer_1(rid, EOK, fd);
|
---|
683 | }
|
---|
684 |
|
---|
685 | void vfs_sync(ipc_callid_t rid, ipc_call_t *request)
|
---|
686 | {
|
---|
687 | int fd = IPC_GET_ARG1(*request);
|
---|
688 |
|
---|
689 | /* Lookup the file structure corresponding to the file descriptor. */
|
---|
690 | vfs_file_t *file = vfs_file_get(fd);
|
---|
691 | if (!file) {
|
---|
692 | ipc_answer_0(rid, ENOENT);
|
---|
693 | return;
|
---|
694 | }
|
---|
695 |
|
---|
696 | /*
|
---|
697 | * Lock the open file structure so that no other thread can manipulate
|
---|
698 | * the same open file at a time.
|
---|
699 | */
|
---|
700 | fibril_mutex_lock(&file->lock);
|
---|
701 | int fs_phone = vfs_grab_phone(file->node->fs_handle);
|
---|
702 |
|
---|
703 | /* Make a VFS_OUT_SYMC request at the destination FS server. */
|
---|
704 | aid_t msg;
|
---|
705 | ipc_call_t answer;
|
---|
706 | msg = async_send_2(fs_phone, VFS_OUT_SYNC, file->node->dev_handle,
|
---|
707 | file->node->index, &answer);
|
---|
708 |
|
---|
709 | /* Wait for reply from the FS server. */
|
---|
710 | ipcarg_t rc;
|
---|
711 | async_wait_for(msg, &rc);
|
---|
712 |
|
---|
713 | vfs_release_phone(fs_phone);
|
---|
714 | fibril_mutex_unlock(&file->lock);
|
---|
715 |
|
---|
716 | ipc_answer_0(rid, rc);
|
---|
717 | }
|
---|
718 |
|
---|
719 | int vfs_close_internal(vfs_file_t *file)
|
---|
720 | {
|
---|
721 | /*
|
---|
722 | * Lock the open file structure so that no other thread can manipulate
|
---|
723 | * the same open file at a time.
|
---|
724 | */
|
---|
725 | fibril_mutex_lock(&file->lock);
|
---|
726 |
|
---|
727 | if (file->refcnt <= 1) {
|
---|
728 | /* Only close the file on the destination FS server
|
---|
729 | if there are no more file descriptors (except the
|
---|
730 | present one) pointing to this file. */
|
---|
731 |
|
---|
732 | int fs_phone = vfs_grab_phone(file->node->fs_handle);
|
---|
733 |
|
---|
734 | /* Make a VFS_OUT_CLOSE request at the destination FS server. */
|
---|
735 | aid_t msg;
|
---|
736 | ipc_call_t answer;
|
---|
737 | msg = async_send_2(fs_phone, VFS_OUT_CLOSE, file->node->dev_handle,
|
---|
738 | file->node->index, &answer);
|
---|
739 |
|
---|
740 | /* Wait for reply from the FS server. */
|
---|
741 | ipcarg_t rc;
|
---|
742 | async_wait_for(msg, &rc);
|
---|
743 |
|
---|
744 | vfs_release_phone(fs_phone);
|
---|
745 | fibril_mutex_unlock(&file->lock);
|
---|
746 |
|
---|
747 | return IPC_GET_ARG1(answer);
|
---|
748 | }
|
---|
749 |
|
---|
750 | fibril_mutex_unlock(&file->lock);
|
---|
751 | return EOK;
|
---|
752 | }
|
---|
753 |
|
---|
754 | void vfs_close(ipc_callid_t rid, ipc_call_t *request)
|
---|
755 | {
|
---|
756 | int fd = IPC_GET_ARG1(*request);
|
---|
757 |
|
---|
758 | /* Lookup the file structure corresponding to the file descriptor. */
|
---|
759 | vfs_file_t *file = vfs_file_get(fd);
|
---|
760 | if (!file) {
|
---|
761 | ipc_answer_0(rid, ENOENT);
|
---|
762 | return;
|
---|
763 | }
|
---|
764 |
|
---|
765 | int ret = vfs_close_internal(file);
|
---|
766 | if (ret != EOK)
|
---|
767 | ipc_answer_0(rid, ret);
|
---|
768 |
|
---|
769 | ret = vfs_fd_free(fd);
|
---|
770 | ipc_answer_0(rid, ret);
|
---|
771 | }
|
---|
772 |
|
---|
773 | static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
|
---|
774 | {
|
---|
775 |
|
---|
776 | /*
|
---|
777 | * The following code strongly depends on the fact that the files data
|
---|
778 | * structure can be only accessed by a single fibril and all file
|
---|
779 | * operations are serialized (i.e. the reads and writes cannot
|
---|
780 | * interleave and a file cannot be closed while it is being read).
|
---|
781 | *
|
---|
782 | * Additional synchronization needs to be added once the table of
|
---|
783 | * open files supports parallel access!
|
---|
784 | */
|
---|
785 |
|
---|
786 | int fd = IPC_GET_ARG1(*request);
|
---|
787 |
|
---|
788 | /* Lookup the file structure corresponding to the file descriptor. */
|
---|
789 | vfs_file_t *file = vfs_file_get(fd);
|
---|
790 | if (!file) {
|
---|
791 | ipc_answer_0(rid, ENOENT);
|
---|
792 | return;
|
---|
793 | }
|
---|
794 |
|
---|
795 | /*
|
---|
796 | * Lock the open file structure so that no other thread can manipulate
|
---|
797 | * the same open file at a time.
|
---|
798 | */
|
---|
799 | fibril_mutex_lock(&file->lock);
|
---|
800 |
|
---|
801 | /*
|
---|
802 | * Lock the file's node so that no other client can read/write to it at
|
---|
803 | * the same time.
|
---|
804 | */
|
---|
805 | if (read)
|
---|
806 | fibril_rwlock_read_lock(&file->node->contents_rwlock);
|
---|
807 | else
|
---|
808 | fibril_rwlock_write_lock(&file->node->contents_rwlock);
|
---|
809 |
|
---|
810 | if (file->node->type == VFS_NODE_DIRECTORY) {
|
---|
811 | /*
|
---|
812 | * Make sure that no one is modifying the namespace
|
---|
813 | * while we are in readdir().
|
---|
814 | */
|
---|
815 | assert(read);
|
---|
816 | fibril_rwlock_read_lock(&namespace_rwlock);
|
---|
817 | }
|
---|
818 |
|
---|
819 | int fs_phone = vfs_grab_phone(file->node->fs_handle);
|
---|
820 |
|
---|
821 | /*
|
---|
822 | * Make a VFS_READ/VFS_WRITE request at the destination FS server
|
---|
823 | * and forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
|
---|
824 | * destination FS server. The call will be routed as if sent by
|
---|
825 | * ourselves. Note that call arguments are immutable in this case so we
|
---|
826 | * don't have to bother.
|
---|
827 | */
|
---|
828 | ipcarg_t rc;
|
---|
829 | ipc_call_t answer;
|
---|
830 | if (read) {
|
---|
831 | if (file->append)
|
---|
832 | file->pos = file->node->size;
|
---|
833 |
|
---|
834 | rc = async_data_read_forward_3_1(fs_phone, VFS_OUT_READ,
|
---|
835 | file->node->dev_handle, file->node->index, file->pos,
|
---|
836 | &answer);
|
---|
837 | } else {
|
---|
838 | rc = async_data_forward_3_1(fs_phone, VFS_OUT_WRITE,
|
---|
839 | file->node->dev_handle, file->node->index, file->pos,
|
---|
840 | &answer);
|
---|
841 | }
|
---|
842 |
|
---|
843 | vfs_release_phone(fs_phone);
|
---|
844 |
|
---|
845 | size_t bytes = IPC_GET_ARG1(answer);
|
---|
846 |
|
---|
847 | if (file->node->type == VFS_NODE_DIRECTORY)
|
---|
848 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
849 |
|
---|
850 | /* Unlock the VFS node. */
|
---|
851 | if (read)
|
---|
852 | fibril_rwlock_read_unlock(&file->node->contents_rwlock);
|
---|
853 | else {
|
---|
854 | /* Update the cached version of node's size. */
|
---|
855 | if (rc == EOK)
|
---|
856 | file->node->size = IPC_GET_ARG2(answer);
|
---|
857 | fibril_rwlock_write_unlock(&file->node->contents_rwlock);
|
---|
858 | }
|
---|
859 |
|
---|
860 | /* Update the position pointer and unlock the open file. */
|
---|
861 | if (rc == EOK)
|
---|
862 | file->pos += bytes;
|
---|
863 | fibril_mutex_unlock(&file->lock);
|
---|
864 |
|
---|
865 | /*
|
---|
866 | * FS server's reply is the final result of the whole operation we
|
---|
867 | * return to the client.
|
---|
868 | */
|
---|
869 | ipc_answer_1(rid, rc, bytes);
|
---|
870 | }
|
---|
871 |
|
---|
872 | void vfs_read(ipc_callid_t rid, ipc_call_t *request)
|
---|
873 | {
|
---|
874 | vfs_rdwr(rid, request, true);
|
---|
875 | }
|
---|
876 |
|
---|
877 | void vfs_write(ipc_callid_t rid, ipc_call_t *request)
|
---|
878 | {
|
---|
879 | vfs_rdwr(rid, request, false);
|
---|
880 | }
|
---|
881 |
|
---|
882 | void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
|
---|
883 | {
|
---|
884 | int fd = (int) IPC_GET_ARG1(*request);
|
---|
885 | off_t off = (off_t) IPC_GET_ARG2(*request);
|
---|
886 | int whence = (int) IPC_GET_ARG3(*request);
|
---|
887 |
|
---|
888 |
|
---|
889 | /* Lookup the file structure corresponding to the file descriptor. */
|
---|
890 | vfs_file_t *file = vfs_file_get(fd);
|
---|
891 | if (!file) {
|
---|
892 | ipc_answer_0(rid, ENOENT);
|
---|
893 | return;
|
---|
894 | }
|
---|
895 |
|
---|
896 | off_t newpos;
|
---|
897 | fibril_mutex_lock(&file->lock);
|
---|
898 | if (whence == SEEK_SET) {
|
---|
899 | file->pos = off;
|
---|
900 | fibril_mutex_unlock(&file->lock);
|
---|
901 | ipc_answer_1(rid, EOK, off);
|
---|
902 | return;
|
---|
903 | }
|
---|
904 | if (whence == SEEK_CUR) {
|
---|
905 | if (file->pos + off < file->pos) {
|
---|
906 | fibril_mutex_unlock(&file->lock);
|
---|
907 | ipc_answer_0(rid, EOVERFLOW);
|
---|
908 | return;
|
---|
909 | }
|
---|
910 | file->pos += off;
|
---|
911 | newpos = file->pos;
|
---|
912 | fibril_mutex_unlock(&file->lock);
|
---|
913 | ipc_answer_1(rid, EOK, newpos);
|
---|
914 | return;
|
---|
915 | }
|
---|
916 | if (whence == SEEK_END) {
|
---|
917 | fibril_rwlock_read_lock(&file->node->contents_rwlock);
|
---|
918 | size_t size = file->node->size;
|
---|
919 | fibril_rwlock_read_unlock(&file->node->contents_rwlock);
|
---|
920 | if (size + off < size) {
|
---|
921 | fibril_mutex_unlock(&file->lock);
|
---|
922 | ipc_answer_0(rid, EOVERFLOW);
|
---|
923 | return;
|
---|
924 | }
|
---|
925 | newpos = size + off;
|
---|
926 | file->pos = newpos;
|
---|
927 | fibril_mutex_unlock(&file->lock);
|
---|
928 | ipc_answer_1(rid, EOK, newpos);
|
---|
929 | return;
|
---|
930 | }
|
---|
931 | fibril_mutex_unlock(&file->lock);
|
---|
932 | ipc_answer_0(rid, EINVAL);
|
---|
933 | }
|
---|
934 |
|
---|
935 | int
|
---|
936 | vfs_truncate_internal(fs_handle_t fs_handle, dev_handle_t dev_handle,
|
---|
937 | fs_index_t index, size_t size)
|
---|
938 | {
|
---|
939 | ipcarg_t rc;
|
---|
940 | int fs_phone;
|
---|
941 |
|
---|
942 | fs_phone = vfs_grab_phone(fs_handle);
|
---|
943 | rc = async_req_3_0(fs_phone, VFS_OUT_TRUNCATE, (ipcarg_t)dev_handle,
|
---|
944 | (ipcarg_t)index, (ipcarg_t)size);
|
---|
945 | vfs_release_phone(fs_phone);
|
---|
946 | return (int)rc;
|
---|
947 | }
|
---|
948 |
|
---|
949 | void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
|
---|
950 | {
|
---|
951 | int fd = IPC_GET_ARG1(*request);
|
---|
952 | size_t size = IPC_GET_ARG2(*request);
|
---|
953 | int rc;
|
---|
954 |
|
---|
955 | vfs_file_t *file = vfs_file_get(fd);
|
---|
956 | if (!file) {
|
---|
957 | ipc_answer_0(rid, ENOENT);
|
---|
958 | return;
|
---|
959 | }
|
---|
960 | fibril_mutex_lock(&file->lock);
|
---|
961 |
|
---|
962 | fibril_rwlock_write_lock(&file->node->contents_rwlock);
|
---|
963 | rc = vfs_truncate_internal(file->node->fs_handle,
|
---|
964 | file->node->dev_handle, file->node->index, size);
|
---|
965 | if (rc == EOK)
|
---|
966 | file->node->size = size;
|
---|
967 | fibril_rwlock_write_unlock(&file->node->contents_rwlock);
|
---|
968 |
|
---|
969 | fibril_mutex_unlock(&file->lock);
|
---|
970 | ipc_answer_0(rid, (ipcarg_t)rc);
|
---|
971 | }
|
---|
972 |
|
---|
973 | void vfs_fstat(ipc_callid_t rid, ipc_call_t *request)
|
---|
974 | {
|
---|
975 | int fd = IPC_GET_ARG1(*request);
|
---|
976 | ipcarg_t rc;
|
---|
977 |
|
---|
978 | vfs_file_t *file = vfs_file_get(fd);
|
---|
979 | if (!file) {
|
---|
980 | ipc_answer_0(rid, ENOENT);
|
---|
981 | return;
|
---|
982 | }
|
---|
983 |
|
---|
984 | ipc_callid_t callid;
|
---|
985 | if (!async_data_read_receive(&callid, NULL)) {
|
---|
986 | ipc_answer_0(callid, EINVAL);
|
---|
987 | ipc_answer_0(rid, EINVAL);
|
---|
988 | return;
|
---|
989 | }
|
---|
990 |
|
---|
991 | fibril_mutex_lock(&file->lock);
|
---|
992 |
|
---|
993 | int fs_phone = vfs_grab_phone(file->node->fs_handle);
|
---|
994 |
|
---|
995 | aid_t msg;
|
---|
996 | msg = async_send_3(fs_phone, VFS_OUT_STAT, file->node->dev_handle,
|
---|
997 | file->node->index, true, NULL);
|
---|
998 | ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
|
---|
999 | async_wait_for(msg, &rc);
|
---|
1000 | vfs_release_phone(fs_phone);
|
---|
1001 |
|
---|
1002 | fibril_mutex_unlock(&file->lock);
|
---|
1003 | ipc_answer_0(rid, rc);
|
---|
1004 | }
|
---|
1005 |
|
---|
1006 | void vfs_stat(ipc_callid_t rid, ipc_call_t *request)
|
---|
1007 | {
|
---|
1008 | char *path;
|
---|
1009 | int rc = async_string_receive(&path, 0, NULL);
|
---|
1010 | if (rc != EOK) {
|
---|
1011 | ipc_answer_0(rid, rc);
|
---|
1012 | return;
|
---|
1013 | }
|
---|
1014 |
|
---|
1015 | ipc_callid_t callid;
|
---|
1016 | if (!async_data_read_receive(&callid, NULL)) {
|
---|
1017 | free(path);
|
---|
1018 | ipc_answer_0(callid, EINVAL);
|
---|
1019 | ipc_answer_0(rid, EINVAL);
|
---|
1020 | return;
|
---|
1021 | }
|
---|
1022 |
|
---|
1023 | vfs_lookup_res_t lr;
|
---|
1024 | fibril_rwlock_read_lock(&namespace_rwlock);
|
---|
1025 | rc = vfs_lookup_internal(path, L_NONE, &lr, NULL);
|
---|
1026 | free(path);
|
---|
1027 | if (rc != EOK) {
|
---|
1028 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
1029 | ipc_answer_0(callid, rc);
|
---|
1030 | ipc_answer_0(rid, rc);
|
---|
1031 | return;
|
---|
1032 | }
|
---|
1033 | vfs_node_t *node = vfs_node_get(&lr);
|
---|
1034 | if (!node) {
|
---|
1035 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
1036 | ipc_answer_0(callid, ENOMEM);
|
---|
1037 | ipc_answer_0(rid, ENOMEM);
|
---|
1038 | return;
|
---|
1039 | }
|
---|
1040 |
|
---|
1041 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
1042 |
|
---|
1043 | int fs_phone = vfs_grab_phone(node->fs_handle);
|
---|
1044 | aid_t msg;
|
---|
1045 | msg = async_send_3(fs_phone, VFS_OUT_STAT, node->dev_handle,
|
---|
1046 | node->index, false, NULL);
|
---|
1047 | ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
|
---|
1048 |
|
---|
1049 | ipcarg_t rv;
|
---|
1050 | async_wait_for(msg, &rv);
|
---|
1051 | vfs_release_phone(fs_phone);
|
---|
1052 |
|
---|
1053 | ipc_answer_0(rid, rv);
|
---|
1054 |
|
---|
1055 | vfs_node_put(node);
|
---|
1056 | }
|
---|
1057 |
|
---|
1058 | void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request)
|
---|
1059 | {
|
---|
1060 | int mode = IPC_GET_ARG1(*request);
|
---|
1061 |
|
---|
1062 | char *path;
|
---|
1063 | int rc = async_string_receive(&path, 0, NULL);
|
---|
1064 | if (rc != EOK) {
|
---|
1065 | ipc_answer_0(rid, rc);
|
---|
1066 | return;
|
---|
1067 | }
|
---|
1068 |
|
---|
1069 | /* Ignore mode for now. */
|
---|
1070 | (void) mode;
|
---|
1071 |
|
---|
1072 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
1073 | int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE;
|
---|
1074 | rc = vfs_lookup_internal(path, lflag, NULL, NULL);
|
---|
1075 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1076 | free(path);
|
---|
1077 | ipc_answer_0(rid, rc);
|
---|
1078 | }
|
---|
1079 |
|
---|
1080 | void vfs_unlink(ipc_callid_t rid, ipc_call_t *request)
|
---|
1081 | {
|
---|
1082 | int lflag = IPC_GET_ARG1(*request);
|
---|
1083 |
|
---|
1084 | char *path;
|
---|
1085 | int rc = async_string_receive(&path, 0, NULL);
|
---|
1086 | if (rc != EOK) {
|
---|
1087 | ipc_answer_0(rid, rc);
|
---|
1088 | return;
|
---|
1089 | }
|
---|
1090 |
|
---|
1091 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
1092 | lflag &= L_DIRECTORY; /* sanitize lflag */
|
---|
1093 | vfs_lookup_res_t lr;
|
---|
1094 | rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL);
|
---|
1095 | free(path);
|
---|
1096 | if (rc != EOK) {
|
---|
1097 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1098 | ipc_answer_0(rid, rc);
|
---|
1099 | return;
|
---|
1100 | }
|
---|
1101 |
|
---|
1102 | /*
|
---|
1103 | * The name has already been unlinked by vfs_lookup_internal().
|
---|
1104 | * We have to get and put the VFS node to ensure that it is
|
---|
1105 | * VFS_OUT_DESTROY'ed after the last reference to it is dropped.
|
---|
1106 | */
|
---|
1107 | vfs_node_t *node = vfs_node_get(&lr);
|
---|
1108 | fibril_mutex_lock(&nodes_mutex);
|
---|
1109 | node->lnkcnt--;
|
---|
1110 | fibril_mutex_unlock(&nodes_mutex);
|
---|
1111 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1112 | vfs_node_put(node);
|
---|
1113 | ipc_answer_0(rid, EOK);
|
---|
1114 | }
|
---|
1115 |
|
---|
1116 | void vfs_rename(ipc_callid_t rid, ipc_call_t *request)
|
---|
1117 | {
|
---|
1118 | /* Retrieve the old path. */
|
---|
1119 | char *old;
|
---|
1120 | int rc = async_string_receive(&old, 0, NULL);
|
---|
1121 | if (rc != EOK) {
|
---|
1122 | ipc_answer_0(rid, rc);
|
---|
1123 | return;
|
---|
1124 | }
|
---|
1125 |
|
---|
1126 | /* Retrieve the new path. */
|
---|
1127 | char *new;
|
---|
1128 | rc = async_string_receive(&new, 0, NULL);
|
---|
1129 | if (rc != EOK) {
|
---|
1130 | free(old);
|
---|
1131 | ipc_answer_0(rid, rc);
|
---|
1132 | return;
|
---|
1133 | }
|
---|
1134 |
|
---|
1135 | size_t olen;
|
---|
1136 | size_t nlen;
|
---|
1137 | char *oldc = canonify(old, &olen);
|
---|
1138 | char *newc = canonify(new, &nlen);
|
---|
1139 |
|
---|
1140 | if ((!oldc) || (!newc)) {
|
---|
1141 | ipc_answer_0(rid, EINVAL);
|
---|
1142 | free(old);
|
---|
1143 | free(new);
|
---|
1144 | return;
|
---|
1145 | }
|
---|
1146 |
|
---|
1147 | oldc[olen] = '\0';
|
---|
1148 | newc[nlen] = '\0';
|
---|
1149 |
|
---|
1150 | if ((!str_lcmp(newc, oldc, str_length(oldc))) &&
|
---|
1151 | ((newc[str_length(oldc)] == '/') ||
|
---|
1152 | (str_length(oldc) == 1) ||
|
---|
1153 | (str_length(oldc) == str_length(newc)))) {
|
---|
1154 | /*
|
---|
1155 | * oldc is a prefix of newc and either
|
---|
1156 | * - newc continues with a / where oldc ends, or
|
---|
1157 | * - oldc was / itself, or
|
---|
1158 | * - oldc and newc are equal.
|
---|
1159 | */
|
---|
1160 | ipc_answer_0(rid, EINVAL);
|
---|
1161 | free(old);
|
---|
1162 | free(new);
|
---|
1163 | return;
|
---|
1164 | }
|
---|
1165 |
|
---|
1166 | vfs_lookup_res_t old_lr;
|
---|
1167 | vfs_lookup_res_t new_lr;
|
---|
1168 | vfs_lookup_res_t new_par_lr;
|
---|
1169 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
1170 |
|
---|
1171 | /* Lookup the node belonging to the old file name. */
|
---|
1172 | rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL);
|
---|
1173 | if (rc != EOK) {
|
---|
1174 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1175 | ipc_answer_0(rid, rc);
|
---|
1176 | free(old);
|
---|
1177 | free(new);
|
---|
1178 | return;
|
---|
1179 | }
|
---|
1180 |
|
---|
1181 | vfs_node_t *old_node = vfs_node_get(&old_lr);
|
---|
1182 | if (!old_node) {
|
---|
1183 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1184 | ipc_answer_0(rid, ENOMEM);
|
---|
1185 | free(old);
|
---|
1186 | free(new);
|
---|
1187 | return;
|
---|
1188 | }
|
---|
1189 |
|
---|
1190 | /* Determine the path to the parent of the node with the new name. */
|
---|
1191 | char *parentc = str_dup(newc);
|
---|
1192 | if (!parentc) {
|
---|
1193 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1194 | ipc_answer_0(rid, rc);
|
---|
1195 | free(old);
|
---|
1196 | free(new);
|
---|
1197 | return;
|
---|
1198 | }
|
---|
1199 |
|
---|
1200 | char *lastsl = str_rchr(parentc + 1, '/');
|
---|
1201 | if (lastsl)
|
---|
1202 | *lastsl = '\0';
|
---|
1203 | else
|
---|
1204 | parentc[1] = '\0';
|
---|
1205 |
|
---|
1206 | /* Lookup parent of the new file name. */
|
---|
1207 | rc = vfs_lookup_internal(parentc, L_NONE, &new_par_lr, NULL);
|
---|
1208 | free(parentc); /* not needed anymore */
|
---|
1209 | if (rc != EOK) {
|
---|
1210 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1211 | ipc_answer_0(rid, rc);
|
---|
1212 | free(old);
|
---|
1213 | free(new);
|
---|
1214 | return;
|
---|
1215 | }
|
---|
1216 |
|
---|
1217 | /* Check whether linking to the same file system instance. */
|
---|
1218 | if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) ||
|
---|
1219 | (old_node->dev_handle != new_par_lr.triplet.dev_handle)) {
|
---|
1220 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1221 | ipc_answer_0(rid, EXDEV); /* different file systems */
|
---|
1222 | free(old);
|
---|
1223 | free(new);
|
---|
1224 | return;
|
---|
1225 | }
|
---|
1226 |
|
---|
1227 | /* Destroy the old link for the new name. */
|
---|
1228 | vfs_node_t *new_node = NULL;
|
---|
1229 | rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL);
|
---|
1230 |
|
---|
1231 | switch (rc) {
|
---|
1232 | case ENOENT:
|
---|
1233 | /* simply not in our way */
|
---|
1234 | break;
|
---|
1235 | case EOK:
|
---|
1236 | new_node = vfs_node_get(&new_lr);
|
---|
1237 | if (!new_node) {
|
---|
1238 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1239 | ipc_answer_0(rid, ENOMEM);
|
---|
1240 | free(old);
|
---|
1241 | free(new);
|
---|
1242 | return;
|
---|
1243 | }
|
---|
1244 | fibril_mutex_lock(&nodes_mutex);
|
---|
1245 | new_node->lnkcnt--;
|
---|
1246 | fibril_mutex_unlock(&nodes_mutex);
|
---|
1247 | break;
|
---|
1248 | default:
|
---|
1249 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1250 | ipc_answer_0(rid, ENOTEMPTY);
|
---|
1251 | free(old);
|
---|
1252 | free(new);
|
---|
1253 | return;
|
---|
1254 | }
|
---|
1255 |
|
---|
1256 | /* Create the new link for the new name. */
|
---|
1257 | rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index);
|
---|
1258 | if (rc != EOK) {
|
---|
1259 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1260 | if (new_node)
|
---|
1261 | vfs_node_put(new_node);
|
---|
1262 | ipc_answer_0(rid, rc);
|
---|
1263 | free(old);
|
---|
1264 | free(new);
|
---|
1265 | return;
|
---|
1266 | }
|
---|
1267 |
|
---|
1268 | fibril_mutex_lock(&nodes_mutex);
|
---|
1269 | old_node->lnkcnt++;
|
---|
1270 | fibril_mutex_unlock(&nodes_mutex);
|
---|
1271 |
|
---|
1272 | /* Destroy the link for the old name. */
|
---|
1273 | rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL);
|
---|
1274 | if (rc != EOK) {
|
---|
1275 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1276 | vfs_node_put(old_node);
|
---|
1277 | if (new_node)
|
---|
1278 | vfs_node_put(new_node);
|
---|
1279 | ipc_answer_0(rid, rc);
|
---|
1280 | free(old);
|
---|
1281 | free(new);
|
---|
1282 | return;
|
---|
1283 | }
|
---|
1284 |
|
---|
1285 | fibril_mutex_lock(&nodes_mutex);
|
---|
1286 | old_node->lnkcnt--;
|
---|
1287 | fibril_mutex_unlock(&nodes_mutex);
|
---|
1288 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1289 | vfs_node_put(old_node);
|
---|
1290 |
|
---|
1291 | if (new_node)
|
---|
1292 | vfs_node_put(new_node);
|
---|
1293 |
|
---|
1294 | free(old);
|
---|
1295 | free(new);
|
---|
1296 | ipc_answer_0(rid, EOK);
|
---|
1297 | }
|
---|
1298 |
|
---|
1299 | void vfs_dup(ipc_callid_t rid, ipc_call_t *request)
|
---|
1300 | {
|
---|
1301 | int oldfd = IPC_GET_ARG1(*request);
|
---|
1302 | int newfd = IPC_GET_ARG2(*request);
|
---|
1303 |
|
---|
1304 | /* Lookup the file structure corresponding to oldfd. */
|
---|
1305 | vfs_file_t *oldfile = vfs_file_get(oldfd);
|
---|
1306 | if (!oldfile) {
|
---|
1307 | ipc_answer_0(rid, EBADF);
|
---|
1308 | return;
|
---|
1309 | }
|
---|
1310 |
|
---|
1311 | /* If the file descriptors are the same, do nothing. */
|
---|
1312 | if (oldfd == newfd) {
|
---|
1313 | ipc_answer_1(rid, EOK, newfd);
|
---|
1314 | return;
|
---|
1315 | }
|
---|
1316 |
|
---|
1317 | /*
|
---|
1318 | * Lock the open file structure so that no other thread can manipulate
|
---|
1319 | * the same open file at a time.
|
---|
1320 | */
|
---|
1321 | fibril_mutex_lock(&oldfile->lock);
|
---|
1322 |
|
---|
1323 | /* Lookup an open file structure possibly corresponding to newfd. */
|
---|
1324 | vfs_file_t *newfile = vfs_file_get(newfd);
|
---|
1325 | if (newfile) {
|
---|
1326 | /* Close the originally opened file. */
|
---|
1327 | int ret = vfs_close_internal(newfile);
|
---|
1328 | if (ret != EOK) {
|
---|
1329 | ipc_answer_0(rid, ret);
|
---|
1330 | return;
|
---|
1331 | }
|
---|
1332 |
|
---|
1333 | ret = vfs_fd_free(newfd);
|
---|
1334 | if (ret != EOK) {
|
---|
1335 | ipc_answer_0(rid, ret);
|
---|
1336 | return;
|
---|
1337 | }
|
---|
1338 | }
|
---|
1339 |
|
---|
1340 | /* Assign the old file to newfd. */
|
---|
1341 | int ret = vfs_fd_assign(oldfile, newfd);
|
---|
1342 | fibril_mutex_unlock(&oldfile->lock);
|
---|
1343 |
|
---|
1344 | if (ret != EOK)
|
---|
1345 | ipc_answer_0(rid, ret);
|
---|
1346 | else
|
---|
1347 | ipc_answer_1(rid, EOK, newfd);
|
---|
1348 | }
|
---|
1349 |
|
---|
1350 | /**
|
---|
1351 | * @}
|
---|
1352 | */
|
---|