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 |
|
---|
55 | /* Forward declarations of static functions. */
|
---|
56 | static int vfs_truncate_internal(fs_handle_t, service_id_t, fs_index_t,
|
---|
57 | aoff64_t);
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * This rwlock prevents the race between a triplet-to-VFS-node resolution and a
|
---|
61 | * concurrent VFS operation which modifies the file system namespace.
|
---|
62 | */
|
---|
63 | FIBRIL_RWLOCK_INITIALIZE(namespace_rwlock);
|
---|
64 |
|
---|
65 | static size_t shared_path(char *a, char *b)
|
---|
66 | {
|
---|
67 | size_t res = 0;
|
---|
68 |
|
---|
69 | while (a[res] == b[res] && a[res] != 0) {
|
---|
70 | res++;
|
---|
71 | }
|
---|
72 |
|
---|
73 | if (a[res] == b[res]) {
|
---|
74 | return res;
|
---|
75 | }
|
---|
76 |
|
---|
77 | res--;
|
---|
78 | while (a[res] != '/') {
|
---|
79 | res--;
|
---|
80 | }
|
---|
81 | return res;
|
---|
82 | }
|
---|
83 |
|
---|
84 | /* This call destroys the file if and only if there are no hard links left. */
|
---|
85 | static void out_destroy(vfs_triplet_t *file)
|
---|
86 | {
|
---|
87 | async_exch_t *exch = vfs_exchange_grab(file->fs_handle);
|
---|
88 | async_msg_2(exch, VFS_OUT_DESTROY,
|
---|
89 | (sysarg_t) file->service_id, (sysarg_t) file->index);
|
---|
90 | vfs_exchange_release(exch);
|
---|
91 | }
|
---|
92 |
|
---|
93 | int vfs_op_clone(int oldfd, bool desc)
|
---|
94 | {
|
---|
95 | /* Lookup the file structure corresponding to fd. */
|
---|
96 | vfs_file_t *oldfile = vfs_file_get(oldfd);
|
---|
97 | if (oldfile == NULL) {
|
---|
98 | return EBADF;
|
---|
99 | }
|
---|
100 | assert(oldfile->node != NULL);
|
---|
101 |
|
---|
102 | vfs_file_t *newfile;
|
---|
103 | int newfd = vfs_fd_alloc(&newfile, desc);
|
---|
104 | if (newfd >= 0) {
|
---|
105 | newfile->node = oldfile->node;
|
---|
106 | newfile->permissions = oldfile->permissions;
|
---|
107 | vfs_node_addref(newfile->node);
|
---|
108 |
|
---|
109 | vfs_file_put(newfile);
|
---|
110 | }
|
---|
111 | vfs_file_put(oldfile);
|
---|
112 |
|
---|
113 | return newfd;
|
---|
114 | }
|
---|
115 |
|
---|
116 | int vfs_op_close(int fd)
|
---|
117 | {
|
---|
118 | return vfs_fd_free(fd);
|
---|
119 | }
|
---|
120 |
|
---|
121 | int vfs_op_dup(int oldfd, int newfd)
|
---|
122 | {
|
---|
123 | /* If the file descriptors are the same, do nothing. */
|
---|
124 | if (oldfd == newfd) {
|
---|
125 | return EOK;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /* Lookup the file structure corresponding to oldfd. */
|
---|
129 | vfs_file_t *oldfile = vfs_file_get(oldfd);
|
---|
130 | if (!oldfile) {
|
---|
131 | return EBADF;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /* Make sure newfd is closed. */
|
---|
135 | (void) vfs_fd_free(newfd);
|
---|
136 |
|
---|
137 | /* Assign the old file to newfd. */
|
---|
138 | int ret = vfs_fd_assign(oldfile, newfd);
|
---|
139 | vfs_file_put(oldfile);
|
---|
140 |
|
---|
141 | return ret;
|
---|
142 | }
|
---|
143 |
|
---|
144 | int vfs_op_fstat_forward(int fd)
|
---|
145 | {
|
---|
146 | vfs_file_t *file = vfs_file_get(fd);
|
---|
147 | if (!file) {
|
---|
148 | return EBADF;
|
---|
149 | }
|
---|
150 | assert(file->node);
|
---|
151 |
|
---|
152 | ipc_callid_t callid;
|
---|
153 | if (!async_data_read_receive(&callid, NULL)) {
|
---|
154 | vfs_file_put(file);
|
---|
155 | async_answer_0(callid, EINVAL);
|
---|
156 | return EINVAL;
|
---|
157 | }
|
---|
158 |
|
---|
159 | async_exch_t *exch = vfs_exchange_grab(file->node->fs_handle);
|
---|
160 | assert(exch);
|
---|
161 |
|
---|
162 | aid_t msg;
|
---|
163 | msg = async_send_3(exch, VFS_OUT_STAT, file->node->service_id,
|
---|
164 | file->node->index, true, NULL);
|
---|
165 | assert(msg);
|
---|
166 | async_forward_fast(callid, exch, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
|
---|
167 |
|
---|
168 | vfs_exchange_release(exch);
|
---|
169 |
|
---|
170 | sysarg_t rc;
|
---|
171 | async_wait_for(msg, &rc);
|
---|
172 |
|
---|
173 | vfs_file_put(file);
|
---|
174 | return rc;
|
---|
175 | }
|
---|
176 |
|
---|
177 | static int vfs_connect_internal(service_id_t service_id, unsigned flags,
|
---|
178 | unsigned instance, const char *options, const char *fsname,
|
---|
179 | vfs_node_t **root)
|
---|
180 | {
|
---|
181 | fs_handle_t fs_handle = 0;
|
---|
182 |
|
---|
183 | fibril_mutex_lock(&fs_list_lock);
|
---|
184 | while (1) {
|
---|
185 | fs_handle = fs_name_to_handle(instance, fsname, false);
|
---|
186 |
|
---|
187 | if (fs_handle != 0 || !(flags & VFS_MOUNT_BLOCKING)) {
|
---|
188 | break;
|
---|
189 | }
|
---|
190 |
|
---|
191 | fibril_condvar_wait(&fs_list_cv, &fs_list_lock);
|
---|
192 | }
|
---|
193 | fibril_mutex_unlock(&fs_list_lock);
|
---|
194 |
|
---|
195 | if (fs_handle == 0) {
|
---|
196 | return ENOENT;
|
---|
197 | }
|
---|
198 |
|
---|
199 | /* Tell the mountee that it is being mounted. */
|
---|
200 | ipc_call_t answer;
|
---|
201 | async_exch_t *exch = vfs_exchange_grab(fs_handle);
|
---|
202 | aid_t msg = async_send_1(exch, VFS_OUT_MOUNTED, (sysarg_t) service_id,
|
---|
203 | &answer);
|
---|
204 | /* Send the mount options */
|
---|
205 | sysarg_t rc = async_data_write_start(exch, options, str_size(options));
|
---|
206 | if (rc != EOK) {
|
---|
207 | async_forget(msg);
|
---|
208 | vfs_exchange_release(exch);
|
---|
209 | return rc;
|
---|
210 | }
|
---|
211 | async_wait_for(msg, &rc);
|
---|
212 | vfs_exchange_release(exch);
|
---|
213 |
|
---|
214 | if (rc != EOK) {
|
---|
215 | return rc;
|
---|
216 | }
|
---|
217 |
|
---|
218 | vfs_lookup_res_t res;
|
---|
219 | res.triplet.fs_handle = fs_handle;
|
---|
220 | res.triplet.service_id = service_id;
|
---|
221 | res.triplet.index = (fs_index_t) IPC_GET_ARG1(answer);
|
---|
222 | res.size = (int64_t) MERGE_LOUP32(IPC_GET_ARG2(answer),
|
---|
223 | IPC_GET_ARG3(answer));
|
---|
224 | res.type = VFS_NODE_DIRECTORY;
|
---|
225 |
|
---|
226 | /* Add reference to the mounted root. */
|
---|
227 | *root = vfs_node_get(&res);
|
---|
228 | assert(*root);
|
---|
229 |
|
---|
230 | return EOK;
|
---|
231 | }
|
---|
232 |
|
---|
233 | int vfs_op_mount(int mpfd, unsigned service_id, unsigned flags,
|
---|
234 | unsigned instance, const char *opts, const char *fs_name, int *outfd)
|
---|
235 | {
|
---|
236 | int rc;
|
---|
237 | vfs_file_t *mp = NULL;
|
---|
238 | vfs_file_t *file = NULL;
|
---|
239 | int fd = -1;
|
---|
240 |
|
---|
241 | if (!(flags & VFS_MOUNT_CONNECT_ONLY)) {
|
---|
242 | mp = vfs_file_get(mpfd);
|
---|
243 | if (mp == NULL) {
|
---|
244 | rc = EBADF;
|
---|
245 | goto out;
|
---|
246 | }
|
---|
247 |
|
---|
248 | if (mp->node->mount != NULL) {
|
---|
249 | rc = EBUSY;
|
---|
250 | goto out;
|
---|
251 | }
|
---|
252 |
|
---|
253 | if (mp->node->type != VFS_NODE_DIRECTORY) {
|
---|
254 | rc = ENOTDIR;
|
---|
255 | goto out;
|
---|
256 | }
|
---|
257 |
|
---|
258 | if (vfs_node_has_children(mp->node)) {
|
---|
259 | rc = ENOTEMPTY;
|
---|
260 | goto out;
|
---|
261 | }
|
---|
262 | }
|
---|
263 |
|
---|
264 | if (!(flags & VFS_MOUNT_NO_REF)) {
|
---|
265 | fd = vfs_fd_alloc(&file, false);
|
---|
266 | if (fd < 0) {
|
---|
267 | rc = fd;
|
---|
268 | goto out;
|
---|
269 | }
|
---|
270 | }
|
---|
271 |
|
---|
272 | vfs_node_t *root = NULL;
|
---|
273 |
|
---|
274 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
275 |
|
---|
276 | rc = vfs_connect_internal(service_id, flags, instance, opts, fs_name,
|
---|
277 | &root);
|
---|
278 | if (rc == EOK && !(flags & VFS_MOUNT_CONNECT_ONLY)) {
|
---|
279 | vfs_node_addref(mp->node);
|
---|
280 | vfs_node_addref(root);
|
---|
281 | mp->node->mount = root;
|
---|
282 | }
|
---|
283 |
|
---|
284 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
285 |
|
---|
286 | if (rc != EOK) {
|
---|
287 | goto out;
|
---|
288 | }
|
---|
289 |
|
---|
290 | if (flags & VFS_MOUNT_NO_REF) {
|
---|
291 | vfs_node_delref(root);
|
---|
292 | } else {
|
---|
293 | assert(file != NULL);
|
---|
294 |
|
---|
295 | file->node = root;
|
---|
296 | file->permissions = MODE_READ | MODE_WRITE | MODE_APPEND;
|
---|
297 | file->open_read = false;
|
---|
298 | file->open_write = false;
|
---|
299 | }
|
---|
300 |
|
---|
301 | out:
|
---|
302 | if (mp) {
|
---|
303 | vfs_file_put(mp);
|
---|
304 | }
|
---|
305 | if (file) {
|
---|
306 | vfs_file_put(file);
|
---|
307 | }
|
---|
308 | if (rc != EOK && fd >= 0) {
|
---|
309 | vfs_fd_free(fd);
|
---|
310 | fd = 0;
|
---|
311 | }
|
---|
312 |
|
---|
313 | *outfd = fd;
|
---|
314 | return rc;
|
---|
315 | }
|
---|
316 |
|
---|
317 | int vfs_op_open2(int fd, int flags)
|
---|
318 | {
|
---|
319 | if (flags == 0) {
|
---|
320 | return EINVAL;
|
---|
321 | }
|
---|
322 |
|
---|
323 | vfs_file_t *file = vfs_file_get(fd);
|
---|
324 | if (!file) {
|
---|
325 | return EBADF;
|
---|
326 | }
|
---|
327 |
|
---|
328 | if ((flags & ~file->permissions) != 0) {
|
---|
329 | vfs_file_put(file);
|
---|
330 | return EPERM;
|
---|
331 | }
|
---|
332 |
|
---|
333 | if (file->open_read || file->open_write) {
|
---|
334 | vfs_file_put(file);
|
---|
335 | return EBUSY;
|
---|
336 | }
|
---|
337 |
|
---|
338 | file->open_read = (flags & MODE_READ) != 0;
|
---|
339 | file->open_write = (flags & (MODE_WRITE | MODE_APPEND)) != 0;
|
---|
340 | file->append = (flags & MODE_APPEND) != 0;
|
---|
341 |
|
---|
342 | if (!file->open_read && !file->open_write) {
|
---|
343 | vfs_file_put(file);
|
---|
344 | return EINVAL;
|
---|
345 | }
|
---|
346 |
|
---|
347 | if (file->node->type == VFS_NODE_DIRECTORY && file->open_write) {
|
---|
348 | file->open_read = file->open_write = false;
|
---|
349 | vfs_file_put(file);
|
---|
350 | return EINVAL;
|
---|
351 | }
|
---|
352 |
|
---|
353 | int rc = vfs_open_node_remote(file->node);
|
---|
354 | if (rc != EOK) {
|
---|
355 | file->open_read = file->open_write = false;
|
---|
356 | vfs_file_put(file);
|
---|
357 | return rc;
|
---|
358 | }
|
---|
359 |
|
---|
360 | vfs_file_put(file);
|
---|
361 | return EOK;
|
---|
362 | }
|
---|
363 |
|
---|
364 | typedef int (* rdwr_ipc_cb_t)(async_exch_t *, vfs_file_t *, ipc_call_t *,
|
---|
365 | bool, void *);
|
---|
366 |
|
---|
367 | static int rdwr_ipc_client(async_exch_t *exch, vfs_file_t *file,
|
---|
368 | ipc_call_t *answer, bool read, void *data)
|
---|
369 | {
|
---|
370 | size_t *bytes = (size_t *) data;
|
---|
371 | int rc;
|
---|
372 |
|
---|
373 | /*
|
---|
374 | * Make a VFS_READ/VFS_WRITE request at the destination FS server
|
---|
375 | * and forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
|
---|
376 | * destination FS server. The call will be routed as if sent by
|
---|
377 | * ourselves. Note that call arguments are immutable in this case so we
|
---|
378 | * don't have to bother.
|
---|
379 | */
|
---|
380 |
|
---|
381 | if (read) {
|
---|
382 | rc = async_data_read_forward_4_1(exch, VFS_OUT_READ,
|
---|
383 | file->node->service_id, file->node->index,
|
---|
384 | LOWER32(file->pos), UPPER32(file->pos), answer);
|
---|
385 | } else {
|
---|
386 | rc = async_data_write_forward_4_1(exch, VFS_OUT_WRITE,
|
---|
387 | file->node->service_id, file->node->index,
|
---|
388 | LOWER32(file->pos), UPPER32(file->pos), answer);
|
---|
389 | }
|
---|
390 |
|
---|
391 | *bytes = IPC_GET_ARG1(*answer);
|
---|
392 | return rc;
|
---|
393 | }
|
---|
394 |
|
---|
395 | static int rdwr_ipc_internal(async_exch_t *exch, vfs_file_t *file,
|
---|
396 | ipc_call_t *answer, bool read, void *data)
|
---|
397 | {
|
---|
398 | rdwr_io_chunk_t *chunk = (rdwr_io_chunk_t *) data;
|
---|
399 |
|
---|
400 | if (exch == NULL)
|
---|
401 | return ENOENT;
|
---|
402 |
|
---|
403 | aid_t msg = async_send_fast(exch, read ? VFS_OUT_READ : VFS_OUT_WRITE,
|
---|
404 | file->node->service_id, file->node->index, LOWER32(file->pos),
|
---|
405 | UPPER32(file->pos), answer);
|
---|
406 | if (msg == 0)
|
---|
407 | return EINVAL;
|
---|
408 |
|
---|
409 | int retval = async_data_read_start(exch, chunk->buffer, chunk->size);
|
---|
410 | if (retval != EOK) {
|
---|
411 | async_forget(msg);
|
---|
412 | return retval;
|
---|
413 | }
|
---|
414 |
|
---|
415 | sysarg_t rc;
|
---|
416 | async_wait_for(msg, &rc);
|
---|
417 |
|
---|
418 | chunk->size = IPC_GET_ARG1(*answer);
|
---|
419 |
|
---|
420 | return (int) rc;
|
---|
421 | }
|
---|
422 |
|
---|
423 | static int vfs_rdwr(int fd, bool read, rdwr_ipc_cb_t ipc_cb, void *ipc_cb_data)
|
---|
424 | {
|
---|
425 | /*
|
---|
426 | * The following code strongly depends on the fact that the files data
|
---|
427 | * structure can be only accessed by a single fibril and all file
|
---|
428 | * operations are serialized (i.e. the reads and writes cannot
|
---|
429 | * interleave and a file cannot be closed while it is being read).
|
---|
430 | *
|
---|
431 | * Additional synchronization needs to be added once the table of
|
---|
432 | * open files supports parallel access!
|
---|
433 | */
|
---|
434 |
|
---|
435 | /* Lookup the file structure corresponding to the file descriptor. */
|
---|
436 | vfs_file_t *file = vfs_file_get(fd);
|
---|
437 | if (!file)
|
---|
438 | return EBADF;
|
---|
439 |
|
---|
440 | if ((read && !file->open_read) || (!read && !file->open_write)) {
|
---|
441 | vfs_file_put(file);
|
---|
442 | return EINVAL;
|
---|
443 | }
|
---|
444 |
|
---|
445 | vfs_info_t *fs_info = fs_handle_to_info(file->node->fs_handle);
|
---|
446 | assert(fs_info);
|
---|
447 |
|
---|
448 | bool rlock = read ||
|
---|
449 | (fs_info->concurrent_read_write && fs_info->write_retains_size);
|
---|
450 |
|
---|
451 | /*
|
---|
452 | * Lock the file's node so that no other client can read/write to it at
|
---|
453 | * the same time unless the FS supports concurrent reads/writes and its
|
---|
454 | * write implementation does not modify the file size.
|
---|
455 | */
|
---|
456 | if (rlock) {
|
---|
457 | fibril_rwlock_read_lock(&file->node->contents_rwlock);
|
---|
458 | } else {
|
---|
459 | fibril_rwlock_write_lock(&file->node->contents_rwlock);
|
---|
460 | }
|
---|
461 |
|
---|
462 | if (file->node->type == VFS_NODE_DIRECTORY) {
|
---|
463 | /*
|
---|
464 | * Make sure that no one is modifying the namespace
|
---|
465 | * while we are in readdir().
|
---|
466 | */
|
---|
467 |
|
---|
468 | if (!read) {
|
---|
469 | if (rlock) {
|
---|
470 | fibril_rwlock_read_unlock(
|
---|
471 | &file->node->contents_rwlock);
|
---|
472 | } else {
|
---|
473 | fibril_rwlock_write_unlock(
|
---|
474 | &file->node->contents_rwlock);
|
---|
475 | }
|
---|
476 | vfs_file_put(file);
|
---|
477 | return EINVAL;
|
---|
478 | }
|
---|
479 |
|
---|
480 | fibril_rwlock_read_lock(&namespace_rwlock);
|
---|
481 | }
|
---|
482 |
|
---|
483 | async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
|
---|
484 |
|
---|
485 | if (!read && file->append)
|
---|
486 | file->pos = file->node->size;
|
---|
487 |
|
---|
488 | /*
|
---|
489 | * Handle communication with the endpoint FS.
|
---|
490 | */
|
---|
491 | ipc_call_t answer;
|
---|
492 | int rc = ipc_cb(fs_exch, file, &answer, read, ipc_cb_data);
|
---|
493 |
|
---|
494 | vfs_exchange_release(fs_exch);
|
---|
495 |
|
---|
496 | size_t bytes = IPC_GET_ARG1(answer);
|
---|
497 |
|
---|
498 | if (file->node->type == VFS_NODE_DIRECTORY) {
|
---|
499 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
500 | }
|
---|
501 |
|
---|
502 | /* Unlock the VFS node. */
|
---|
503 | if (rlock) {
|
---|
504 | fibril_rwlock_read_unlock(&file->node->contents_rwlock);
|
---|
505 | } else {
|
---|
506 | /* Update the cached version of node's size. */
|
---|
507 | if (rc == EOK) {
|
---|
508 | file->node->size = MERGE_LOUP32(IPC_GET_ARG2(answer),
|
---|
509 | IPC_GET_ARG3(answer));
|
---|
510 | }
|
---|
511 | fibril_rwlock_write_unlock(&file->node->contents_rwlock);
|
---|
512 | }
|
---|
513 |
|
---|
514 | /* Update the position pointer and unlock the open file. */
|
---|
515 | if (rc == EOK) {
|
---|
516 | file->pos += bytes;
|
---|
517 | }
|
---|
518 | vfs_file_put(file);
|
---|
519 |
|
---|
520 | return rc;
|
---|
521 | }
|
---|
522 |
|
---|
523 | int vfs_rdwr_internal(int fd, bool read, rdwr_io_chunk_t *chunk)
|
---|
524 | {
|
---|
525 | return vfs_rdwr(fd, read, rdwr_ipc_internal, chunk);
|
---|
526 | }
|
---|
527 |
|
---|
528 | int vfs_op_read(int fd, size_t *out_bytes)
|
---|
529 | {
|
---|
530 | return vfs_rdwr(fd, true, rdwr_ipc_client, out_bytes);
|
---|
531 | }
|
---|
532 |
|
---|
533 | int vfs_op_rename(int basefd, char *old, char *new)
|
---|
534 | {
|
---|
535 | vfs_file_t *base_file = vfs_file_get(basefd);
|
---|
536 | if (!base_file) {
|
---|
537 | return EBADF;
|
---|
538 | }
|
---|
539 | vfs_node_t *base = base_file->node;
|
---|
540 | vfs_node_addref(base);
|
---|
541 | vfs_file_put(base_file);
|
---|
542 |
|
---|
543 | vfs_lookup_res_t base_lr;
|
---|
544 | vfs_lookup_res_t old_lr;
|
---|
545 | vfs_lookup_res_t new_lr_orig;
|
---|
546 | bool orig_unlinked = false;
|
---|
547 |
|
---|
548 | int rc;
|
---|
549 |
|
---|
550 | size_t shared = shared_path(old, new);
|
---|
551 |
|
---|
552 | /* Do not allow one path to be a prefix of the other. */
|
---|
553 | if (old[shared] == 0 || new[shared] == 0) {
|
---|
554 | vfs_node_put(base);
|
---|
555 | return EINVAL;
|
---|
556 | }
|
---|
557 | assert(old[shared] == '/');
|
---|
558 | assert(new[shared] == '/');
|
---|
559 |
|
---|
560 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
561 |
|
---|
562 | /* Resolve the shared portion of the path first. */
|
---|
563 | if (shared != 0) {
|
---|
564 | old[shared] = 0;
|
---|
565 | rc = vfs_lookup_internal(base, old, L_DIRECTORY, &base_lr);
|
---|
566 | if (rc != EOK) {
|
---|
567 | vfs_node_put(base);
|
---|
568 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
569 | return rc;
|
---|
570 | }
|
---|
571 |
|
---|
572 | vfs_node_put(base);
|
---|
573 | base = vfs_node_get(&base_lr);
|
---|
574 | old[shared] = '/';
|
---|
575 | old += shared;
|
---|
576 | new += shared;
|
---|
577 | }
|
---|
578 |
|
---|
579 | rc = vfs_lookup_internal(base, old, L_DISABLE_MOUNTS,
|
---|
580 | &old_lr);
|
---|
581 | if (rc != EOK) {
|
---|
582 | vfs_node_put(base);
|
---|
583 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
584 | return rc;
|
---|
585 | }
|
---|
586 |
|
---|
587 | rc = vfs_lookup_internal(base, new, L_UNLINK | L_DISABLE_MOUNTS,
|
---|
588 | &new_lr_orig);
|
---|
589 | if (rc == EOK) {
|
---|
590 | orig_unlinked = true;
|
---|
591 | } else if (rc != ENOENT) {
|
---|
592 | vfs_node_put(base);
|
---|
593 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
594 | return rc;
|
---|
595 | }
|
---|
596 |
|
---|
597 | rc = vfs_link_internal(base, new, &old_lr.triplet);
|
---|
598 | if (rc != EOK) {
|
---|
599 | vfs_link_internal(base, old, &old_lr.triplet);
|
---|
600 | if (orig_unlinked) {
|
---|
601 | vfs_link_internal(base, new, &new_lr_orig.triplet);
|
---|
602 | }
|
---|
603 | vfs_node_put(base);
|
---|
604 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
605 | return rc;
|
---|
606 | }
|
---|
607 |
|
---|
608 | rc = vfs_lookup_internal(base, old, L_UNLINK | L_DISABLE_MOUNTS,
|
---|
609 | &old_lr);
|
---|
610 | if (rc != EOK) {
|
---|
611 | if (orig_unlinked) {
|
---|
612 | vfs_link_internal(base, new, &new_lr_orig.triplet);
|
---|
613 | }
|
---|
614 | vfs_node_put(base);
|
---|
615 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
616 | return rc;
|
---|
617 | }
|
---|
618 |
|
---|
619 | /* If the node is not held by anyone, try to destroy it. */
|
---|
620 | if (orig_unlinked) {
|
---|
621 | vfs_node_t *node = vfs_node_peek(&new_lr_orig);
|
---|
622 | if (!node)
|
---|
623 | out_destroy(&new_lr_orig.triplet);
|
---|
624 | else
|
---|
625 | vfs_node_put(node);
|
---|
626 | }
|
---|
627 |
|
---|
628 | vfs_node_put(base);
|
---|
629 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
630 | return EOK;
|
---|
631 | }
|
---|
632 |
|
---|
633 | int vfs_op_seek(int fd, int64_t offset, int whence, int64_t *out_offset)
|
---|
634 | {
|
---|
635 | vfs_file_t *file = vfs_file_get(fd);
|
---|
636 | if (!file) {
|
---|
637 | return EBADF;
|
---|
638 | }
|
---|
639 |
|
---|
640 | switch (whence) {
|
---|
641 | case SEEK_SET:
|
---|
642 | if (offset < 0) {
|
---|
643 | vfs_file_put(file);
|
---|
644 | return EINVAL;
|
---|
645 | }
|
---|
646 | file->pos = offset;
|
---|
647 | *out_offset = offset;
|
---|
648 | vfs_file_put(file);
|
---|
649 | return EOK;
|
---|
650 | case SEEK_CUR:
|
---|
651 | if (offset > 0 && file->pos > (INT64_MAX - offset)) {
|
---|
652 | vfs_file_put(file);
|
---|
653 | return EOVERFLOW;
|
---|
654 | }
|
---|
655 |
|
---|
656 | if (offset < 0 && -file->pos > offset) {
|
---|
657 | vfs_file_put(file);
|
---|
658 | return EOVERFLOW;
|
---|
659 | }
|
---|
660 |
|
---|
661 | file->pos += offset;
|
---|
662 | *out_offset = file->pos;
|
---|
663 | vfs_file_put(file);
|
---|
664 | return EOK;
|
---|
665 | case SEEK_END:
|
---|
666 | fibril_rwlock_read_lock(&file->node->contents_rwlock);
|
---|
667 | int64_t size = vfs_node_get_size(file->node);
|
---|
668 | fibril_rwlock_read_unlock(&file->node->contents_rwlock);
|
---|
669 |
|
---|
670 | if (offset > 0 && size > (INT64_MAX - offset)) {
|
---|
671 | vfs_file_put(file);
|
---|
672 | return EOVERFLOW;
|
---|
673 | }
|
---|
674 |
|
---|
675 | if (offset < 0 && -size > offset) {
|
---|
676 | vfs_file_put(file);
|
---|
677 | return EOVERFLOW;
|
---|
678 | }
|
---|
679 |
|
---|
680 | file->pos = size + offset;
|
---|
681 | *out_offset = file->pos;
|
---|
682 | vfs_file_put(file);
|
---|
683 | return EOK;
|
---|
684 | }
|
---|
685 |
|
---|
686 | vfs_file_put(file);
|
---|
687 | return EINVAL;
|
---|
688 | }
|
---|
689 |
|
---|
690 | int vfs_op_statfs(int fd)
|
---|
691 | {
|
---|
692 | ipc_callid_t callid;
|
---|
693 |
|
---|
694 | if (!async_data_read_receive(&callid, NULL)) {
|
---|
695 | async_answer_0(callid, EINVAL);
|
---|
696 | return EINVAL;
|
---|
697 | }
|
---|
698 |
|
---|
699 | vfs_file_t *file = vfs_file_get(fd);
|
---|
700 | if (!file) {
|
---|
701 | async_answer_0(callid, EBADF);
|
---|
702 | return EBADF;
|
---|
703 | }
|
---|
704 |
|
---|
705 | vfs_node_t *node = file->node;
|
---|
706 |
|
---|
707 | async_exch_t *exch = vfs_exchange_grab(node->fs_handle);
|
---|
708 |
|
---|
709 | aid_t msg;
|
---|
710 | msg = async_send_3(exch, VFS_OUT_STATFS, node->service_id,
|
---|
711 | node->index, false, NULL);
|
---|
712 | async_forward_fast(callid, exch, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
|
---|
713 |
|
---|
714 | vfs_exchange_release(exch);
|
---|
715 |
|
---|
716 | sysarg_t rv;
|
---|
717 | async_wait_for(msg, &rv);
|
---|
718 |
|
---|
719 | vfs_file_put(file);
|
---|
720 | return rv;
|
---|
721 | }
|
---|
722 |
|
---|
723 | int vfs_op_sync(int fd)
|
---|
724 | {
|
---|
725 | vfs_file_t *file = vfs_file_get(fd);
|
---|
726 | if (!file) {
|
---|
727 | return EBADF;
|
---|
728 | }
|
---|
729 |
|
---|
730 | async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
|
---|
731 |
|
---|
732 | aid_t msg;
|
---|
733 | ipc_call_t answer;
|
---|
734 | msg = async_send_2(fs_exch, VFS_OUT_SYNC, file->node->service_id,
|
---|
735 | file->node->index, &answer);
|
---|
736 |
|
---|
737 | vfs_exchange_release(fs_exch);
|
---|
738 |
|
---|
739 | sysarg_t rc;
|
---|
740 | async_wait_for(msg, &rc);
|
---|
741 |
|
---|
742 | vfs_file_put(file);
|
---|
743 | return rc;
|
---|
744 |
|
---|
745 | }
|
---|
746 |
|
---|
747 | static int vfs_truncate_internal(fs_handle_t fs_handle, service_id_t service_id,
|
---|
748 | fs_index_t index, aoff64_t size)
|
---|
749 | {
|
---|
750 | async_exch_t *exch = vfs_exchange_grab(fs_handle);
|
---|
751 | sysarg_t rc = async_req_4_0(exch, VFS_OUT_TRUNCATE,
|
---|
752 | (sysarg_t) service_id, (sysarg_t) index, LOWER32(size),
|
---|
753 | UPPER32(size));
|
---|
754 | vfs_exchange_release(exch);
|
---|
755 |
|
---|
756 | return (int) rc;
|
---|
757 | }
|
---|
758 |
|
---|
759 | int vfs_op_truncate(int fd, int64_t size)
|
---|
760 | {
|
---|
761 | vfs_file_t *file = vfs_file_get(fd);
|
---|
762 | if (!file) {
|
---|
763 | return EBADF;
|
---|
764 | }
|
---|
765 |
|
---|
766 | fibril_rwlock_write_lock(&file->node->contents_rwlock);
|
---|
767 |
|
---|
768 | int rc = vfs_truncate_internal(file->node->fs_handle,
|
---|
769 | file->node->service_id, file->node->index, size);
|
---|
770 | if (rc == EOK) {
|
---|
771 | file->node->size = size;
|
---|
772 | }
|
---|
773 |
|
---|
774 | fibril_rwlock_write_unlock(&file->node->contents_rwlock);
|
---|
775 | vfs_file_put(file);
|
---|
776 | return rc;
|
---|
777 | }
|
---|
778 |
|
---|
779 | int vfs_op_unlink2(int parentfd, int expectfd, int wflag, char *path)
|
---|
780 | {
|
---|
781 | int rc = EOK;
|
---|
782 | vfs_file_t *parent = NULL;
|
---|
783 | vfs_file_t *expect = NULL;
|
---|
784 |
|
---|
785 | if (parentfd == expectfd) {
|
---|
786 | return EINVAL;
|
---|
787 | }
|
---|
788 |
|
---|
789 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
790 |
|
---|
791 | int lflag = (wflag&WALK_DIRECTORY) ? L_DIRECTORY: 0;
|
---|
792 |
|
---|
793 | /*
|
---|
794 | * Files are retrieved in order of file descriptors, to prevent
|
---|
795 | * deadlock.
|
---|
796 | */
|
---|
797 | if (parentfd < expectfd) {
|
---|
798 | parent = vfs_file_get(parentfd);
|
---|
799 | if (!parent) {
|
---|
800 | rc = EBADF;
|
---|
801 | goto exit;
|
---|
802 | }
|
---|
803 | }
|
---|
804 |
|
---|
805 | if (expectfd >= 0) {
|
---|
806 | expect = vfs_file_get(expectfd);
|
---|
807 | if (!expect) {
|
---|
808 | rc = EBADF;
|
---|
809 | goto exit;
|
---|
810 | }
|
---|
811 | }
|
---|
812 |
|
---|
813 | if (parentfd > expectfd) {
|
---|
814 | parent = vfs_file_get(parentfd);
|
---|
815 | if (!parent) {
|
---|
816 | rc = EBADF;
|
---|
817 | goto exit;
|
---|
818 | }
|
---|
819 | }
|
---|
820 |
|
---|
821 | assert(parent != NULL);
|
---|
822 |
|
---|
823 | if (expectfd >= 0) {
|
---|
824 | vfs_lookup_res_t lr;
|
---|
825 | rc = vfs_lookup_internal(parent->node, path, lflag, &lr);
|
---|
826 | if (rc != EOK) {
|
---|
827 | goto exit;
|
---|
828 | }
|
---|
829 |
|
---|
830 | vfs_node_t *found_node = vfs_node_peek(&lr);
|
---|
831 | vfs_node_put(found_node);
|
---|
832 | if (expect->node != found_node) {
|
---|
833 | rc = ENOENT;
|
---|
834 | goto exit;
|
---|
835 | }
|
---|
836 |
|
---|
837 | vfs_file_put(expect);
|
---|
838 | expect = NULL;
|
---|
839 | }
|
---|
840 |
|
---|
841 | vfs_lookup_res_t lr;
|
---|
842 | rc = vfs_lookup_internal(parent->node, path, lflag | L_UNLINK, &lr);
|
---|
843 | if (rc != EOK) {
|
---|
844 | goto exit;
|
---|
845 | }
|
---|
846 |
|
---|
847 | /* If the node is not held by anyone, try to destroy it. */
|
---|
848 | vfs_node_t *node = vfs_node_peek(&lr);
|
---|
849 | if (!node)
|
---|
850 | out_destroy(&lr.triplet);
|
---|
851 | else
|
---|
852 | vfs_node_put(node);
|
---|
853 |
|
---|
854 | exit:
|
---|
855 | if (path) {
|
---|
856 | free(path);
|
---|
857 | }
|
---|
858 | if (parent) {
|
---|
859 | vfs_file_put(parent);
|
---|
860 | }
|
---|
861 | if (expect) {
|
---|
862 | vfs_file_put(expect);
|
---|
863 | }
|
---|
864 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
865 | return rc;
|
---|
866 | }
|
---|
867 |
|
---|
868 | int vfs_op_unmount(int mpfd)
|
---|
869 | {
|
---|
870 | vfs_file_t *mp = vfs_file_get(mpfd);
|
---|
871 | if (mp == NULL) {
|
---|
872 | return EBADF;
|
---|
873 | }
|
---|
874 |
|
---|
875 | if (mp->node->mount == NULL) {
|
---|
876 | vfs_file_put(mp);
|
---|
877 | return ENOENT;
|
---|
878 | }
|
---|
879 |
|
---|
880 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
881 |
|
---|
882 | /*
|
---|
883 | * Count the total number of references for the mounted file system. We
|
---|
884 | * are expecting at least one, which is held by the mount point.
|
---|
885 | * If we find more, it means that
|
---|
886 | * the file system cannot be gracefully unmounted at the moment because
|
---|
887 | * someone is working with it.
|
---|
888 | */
|
---|
889 | if (vfs_nodes_refcount_sum_get(mp->node->mount->fs_handle,
|
---|
890 | mp->node->mount->service_id) != 1) {
|
---|
891 | vfs_file_put(mp);
|
---|
892 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
893 | return EBUSY;
|
---|
894 | }
|
---|
895 |
|
---|
896 | async_exch_t *exch = vfs_exchange_grab(mp->node->mount->fs_handle);
|
---|
897 | int rc = async_req_1_0(exch, VFS_OUT_UNMOUNTED,
|
---|
898 | mp->node->mount->service_id);
|
---|
899 | vfs_exchange_release(exch);
|
---|
900 |
|
---|
901 | if (rc != EOK) {
|
---|
902 | vfs_file_put(mp);
|
---|
903 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
904 | return rc;
|
---|
905 | }
|
---|
906 |
|
---|
907 | vfs_node_forget(mp->node->mount);
|
---|
908 | vfs_node_put(mp->node);
|
---|
909 | mp->node->mount = NULL;
|
---|
910 |
|
---|
911 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
912 |
|
---|
913 | vfs_file_put(mp);
|
---|
914 | return EOK;
|
---|
915 | }
|
---|
916 |
|
---|
917 | int vfs_op_wait_handle(bool high_fd)
|
---|
918 | {
|
---|
919 | return vfs_wait_handle_internal(high_fd);
|
---|
920 | }
|
---|
921 |
|
---|
922 | static inline bool walk_flags_valid(int flags)
|
---|
923 | {
|
---|
924 | if ((flags & ~WALK_ALL_FLAGS) != 0) {
|
---|
925 | return false;
|
---|
926 | }
|
---|
927 | if ((flags & WALK_MAY_CREATE) && (flags & WALK_MUST_CREATE)) {
|
---|
928 | return false;
|
---|
929 | }
|
---|
930 | if ((flags & WALK_REGULAR) && (flags & WALK_DIRECTORY)) {
|
---|
931 | return false;
|
---|
932 | }
|
---|
933 | if ((flags & WALK_MAY_CREATE) || (flags & WALK_MUST_CREATE)) {
|
---|
934 | if (!(flags & WALK_DIRECTORY) && !(flags & WALK_REGULAR)) {
|
---|
935 | return false;
|
---|
936 | }
|
---|
937 | }
|
---|
938 | return true;
|
---|
939 | }
|
---|
940 |
|
---|
941 | static inline int walk_lookup_flags(int flags)
|
---|
942 | {
|
---|
943 | int lflags = 0;
|
---|
944 | if ((flags & WALK_MAY_CREATE) || (flags & WALK_MUST_CREATE)) {
|
---|
945 | lflags |= L_CREATE;
|
---|
946 | }
|
---|
947 | if (flags & WALK_MUST_CREATE) {
|
---|
948 | lflags |= L_EXCLUSIVE;
|
---|
949 | }
|
---|
950 | if (flags & WALK_REGULAR) {
|
---|
951 | lflags |= L_FILE;
|
---|
952 | }
|
---|
953 | if (flags & WALK_DIRECTORY) {
|
---|
954 | lflags |= L_DIRECTORY;
|
---|
955 | }
|
---|
956 | if (flags & WALK_MOUNT_POINT) {
|
---|
957 | lflags |= L_MP;
|
---|
958 | }
|
---|
959 | return lflags;
|
---|
960 | }
|
---|
961 |
|
---|
962 | int vfs_op_walk(int parentfd, int flags, char *path, int *out_fd)
|
---|
963 | {
|
---|
964 | if (!walk_flags_valid(flags)) {
|
---|
965 | return EINVAL;
|
---|
966 | }
|
---|
967 |
|
---|
968 | vfs_file_t *parent = vfs_file_get(parentfd);
|
---|
969 | if (!parent) {
|
---|
970 | return EBADF;
|
---|
971 | }
|
---|
972 |
|
---|
973 | fibril_rwlock_read_lock(&namespace_rwlock);
|
---|
974 |
|
---|
975 | vfs_lookup_res_t lr;
|
---|
976 | int rc = vfs_lookup_internal(parent->node, path,
|
---|
977 | walk_lookup_flags(flags), &lr);
|
---|
978 |
|
---|
979 | if (rc != EOK) {
|
---|
980 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
981 | vfs_file_put(parent);
|
---|
982 | return rc;
|
---|
983 | }
|
---|
984 |
|
---|
985 | vfs_node_t *node = vfs_node_get(&lr);
|
---|
986 |
|
---|
987 | vfs_file_t *file;
|
---|
988 | int fd = vfs_fd_alloc(&file, false);
|
---|
989 | if (fd < 0) {
|
---|
990 | vfs_node_put(node);
|
---|
991 | vfs_file_put(parent);
|
---|
992 | return fd;
|
---|
993 | }
|
---|
994 | assert(file != NULL);
|
---|
995 |
|
---|
996 | file->node = node;
|
---|
997 | file->permissions = parent->permissions;
|
---|
998 | file->open_read = false;
|
---|
999 | file->open_write = false;
|
---|
1000 |
|
---|
1001 | vfs_file_put(file);
|
---|
1002 | vfs_file_put(parent);
|
---|
1003 |
|
---|
1004 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
1005 |
|
---|
1006 | *out_fd = fd;
|
---|
1007 | return EOK;
|
---|
1008 | }
|
---|
1009 |
|
---|
1010 | int vfs_op_write(int fd, size_t *out_bytes)
|
---|
1011 | {
|
---|
1012 | return vfs_rdwr(fd, false, rdwr_ipc_client, out_bytes);
|
---|
1013 | }
|
---|
1014 |
|
---|
1015 | /**
|
---|
1016 | * @}
|
---|
1017 | */
|
---|