source: mainline/uspace/srv/vfs/vfs_ops.c@ 35b7d86e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 35b7d86e was 35b7d86e, checked in by Jakub Jermar <jakub@…>, 8 years ago

Remove VFS_IN_MTAB_GET

The mountpoints in VFS don't know their path anymore and it does not
make much sense to maintain this global mount table when tasks can have
different roots.

  • Property mode set to 100644
File size: 21.8 KB
RevLine 
[861e7d1]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 * @{
[8dc72b64]31 */
[861e7d1]32
33/**
[8dc72b64]34 * @file vfs_ops.c
35 * @brief Operations that VFS offers to its clients.
[861e7d1]36 */
37
[a8e9ab8d]38#include "vfs.h"
[ed903174]39#include <macros.h>
[9539be6]40#include <stdint.h>
[861e7d1]41#include <async.h>
42#include <errno.h>
43#include <stdio.h>
44#include <stdlib.h>
[19f857a]45#include <str.h>
[3e6a98c5]46#include <stdbool.h>
[1e4cada]47#include <fibril_synch.h>
[d9c8c81]48#include <adt/list.h>
[861e7d1]49#include <unistd.h>
50#include <ctype.h>
[2db4ac8]51#include <fcntl.h>
[861e7d1]52#include <assert.h>
[a8e9ab8d]53#include <vfs/canonify.h>
[861e7d1]54
[7fe1f75]55/* Forward declarations of static functions. */
[15f3c3f]56static int vfs_truncate_internal(fs_handle_t, service_id_t, fs_index_t,
[8df8415]57 aoff64_t);
[7fe1f75]58
[861e7d1]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 */
[230260ac]63FIBRIL_RWLOCK_INITIALIZE(namespace_rwlock);
[861e7d1]64
[0d35511]65static 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. */
85static 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
93int 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
116int vfs_op_close(int fd)
117{
118 return vfs_fd_free(fd);
119}
120
121int 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
144int 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
177static 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)
[861e7d1]180{
[4636a60]181 fs_handle_t fs_handle = 0;
[05b9912]182
[4636a60]183 fibril_mutex_lock(&fs_list_lock);
184 while (1) {
185 fs_handle = fs_name_to_handle(instance, fsname, false);
[8dc72b64]186
[5126f80]187 if (fs_handle != 0 || !(flags & VFS_MOUNT_BLOCKING)) {
[4636a60]188 break;
[861e7d1]189 }
[8dc72b64]190
[4636a60]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;
[5bcd5b7]197 }
198
[4636a60]199 /* Tell the mountee that it is being mounted. */
200 ipc_call_t answer;
201 async_exch_t *exch = vfs_exchange_grab(fs_handle);
[0d35511]202 aid_t msg = async_send_1(exch, VFS_OUT_MOUNTED, (sysarg_t) service_id,
203 &answer);
[4636a60]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;
[5bcd5b7]210 }
[4636a60]211 async_wait_for(msg, &rc);
212 vfs_exchange_release(exch);
[5bcd5b7]213
[4636a60]214 if (rc != EOK) {
215 return rc;
[5bcd5b7]216 }
217
[4636a60]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);
[0d35511]222 res.size = (int64_t) MERGE_LOUP32(IPC_GET_ARG2(answer),
223 IPC_GET_ARG3(answer));
[4636a60]224 res.type = VFS_NODE_DIRECTORY;
[5bcd5b7]225
[4636a60]226 /* Add reference to the mounted root. */
227 *root = vfs_node_get(&res);
228 assert(*root);
[8dc72b64]229
[4636a60]230 return EOK;
[5bcd5b7]231}
232
[0d35511]233int vfs_op_mount(int mpfd, unsigned service_id, unsigned flags,
234 unsigned instance, const char *opts, const char *fs_name, int *outfd)
[8dc72b64]235{
[0d35511]236 int rc;
[5126f80]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 }
[c08c355]270 }
[4636a60]271
[5126f80]272 vfs_node_t *root = NULL;
273
[4636a60]274 fibril_rwlock_write_lock(&namespace_rwlock);
[6f9ef87a]275
[0d35511]276 rc = vfs_connect_internal(service_id, flags, instance, opts, fs_name,
277 &root);
[5126f80]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
301out:
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);
[0d35511]310 fd = 0;
[5126f80]311 }
[0d35511]312
313 *outfd = fd;
314 return rc;
[8dc72b64]315}
316
[0d35511]317int vfs_op_open2(int fd, int flags)
[861e7d1]318{
[0d35511]319 if (flags == 0) {
320 return EINVAL;
[0b18364]321 }
[0d35511]322
323 vfs_file_t *file = vfs_file_get(fd);
324 if (!file) {
325 return EBADF;
[0b18364]326 }
[0d35511]327
328 if ((flags & ~file->permissions) != 0) {
329 vfs_file_put(file);
330 return EPERM;
[0b18364]331 }
[0d35511]332
333 if (file->open_read || file->open_write) {
334 vfs_file_put(file);
335 return EBUSY;
[cb65bbe]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;
[05b9912]341
[cb65bbe]342 if (!file->open_read && !file->open_write) {
343 vfs_file_put(file);
[0d35511]344 return EINVAL;
[6f2c1ff]345 }
[05b9912]346
[cb65bbe]347 if (file->node->type == VFS_NODE_DIRECTORY && file->open_write) {
348 file->open_read = file->open_write = false;
349 vfs_file_put(file);
[0d35511]350 return EINVAL;
[7fe1f75]351 }
[05b9912]352
[cb65bbe]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);
[0d35511]357 return rc;
[05b9912]358 }
359
[4fe94c66]360 vfs_file_put(file);
[0d35511]361 return EOK;
[05b9912]362}
363
[e503517a]364typedef int (* rdwr_ipc_cb_t)(async_exch_t *, vfs_file_t *, ipc_call_t *,
[42d08592]365 bool, void *);
366
[e503517a]367static int rdwr_ipc_client(async_exch_t *exch, vfs_file_t *file,
[42d08592]368 ipc_call_t *answer, bool read, void *data)
369{
[e503517a]370 size_t *bytes = (size_t *) data;
371 int rc;
372
[42d08592]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) {
[e503517a]382 rc = async_data_read_forward_4_1(exch, VFS_OUT_READ,
[42d08592]383 file->node->service_id, file->node->index,
384 LOWER32(file->pos), UPPER32(file->pos), answer);
385 } else {
[e503517a]386 rc = async_data_write_forward_4_1(exch, VFS_OUT_WRITE,
[42d08592]387 file->node->service_id, file->node->index,
388 LOWER32(file->pos), UPPER32(file->pos), answer);
[e503517a]389 }
390
391 *bytes = IPC_GET_ARG1(*answer);
392 return rc;
[42d08592]393}
[e503517a]394
395static 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;
[42d08592]402
[e503517a]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
423static int vfs_rdwr(int fd, bool read, rdwr_ipc_cb_t ipc_cb, void *ipc_cb_data)
[861e7d1]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 */
[79ae36dd]434
[72bde81]435 /* Lookup the file structure corresponding to the file descriptor. */
[861e7d1]436 vfs_file_t *file = vfs_file_get(fd);
[e503517a]437 if (!file)
[354b642]438 return EBADF;
[6c89f20]439
[cb65bbe]440 if ((read && !file->open_read) || (!read && !file->open_write)) {
[c577a9a]441 vfs_file_put(file);
[1dff985]442 return EINVAL;
[cb65bbe]443 }
444
[79ae36dd]445 vfs_info_t *fs_info = fs_handle_to_info(file->node->fs_handle);
446 assert(fs_info);
447
[0d35511]448 bool rlock = read ||
449 (fs_info->concurrent_read_write && fs_info->write_retains_size);
[354b642]450
[861e7d1]451 /*
452 * Lock the file's node so that no other client can read/write to it at
[c2f4b6b]453 * the same time unless the FS supports concurrent reads/writes and its
454 * write implementation does not modify the file size.
[861e7d1]455 */
[354b642]456 if (rlock) {
[230260ac]457 fibril_rwlock_read_lock(&file->node->contents_rwlock);
[354b642]458 } else {
[230260ac]459 fibril_rwlock_write_lock(&file->node->contents_rwlock);
[354b642]460 }
[79ae36dd]461
[b17186d]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 */
[354b642]467
468 if (!read) {
469 if (rlock) {
[0d35511]470 fibril_rwlock_read_unlock(
471 &file->node->contents_rwlock);
[354b642]472 } else {
[0d35511]473 fibril_rwlock_write_unlock(
474 &file->node->contents_rwlock);
[354b642]475 }
476 vfs_file_put(file);
477 return EINVAL;
478 }
479
[230260ac]480 fibril_rwlock_read_lock(&namespace_rwlock);
[b17186d]481 }
[6c89f20]482
[79ae36dd]483 async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
[861e7d1]484
[42d08592]485 if (!read && file->append)
486 file->pos = file->node->size;
487
[861e7d1]488 /*
[42d08592]489 * Handle communication with the endpoint FS.
[861e7d1]490 */
[b4cbef1]491 ipc_call_t answer;
[e503517a]492 int rc = ipc_cb(fs_exch, file, &answer, read, ipc_cb_data);
[05b9912]493
[79ae36dd]494 vfs_exchange_release(fs_exch);
[34ca870]495
[861e7d1]496 size_t bytes = IPC_GET_ARG1(answer);
[b4cbef1]497
[b7c62a9]498 if (file->node->type == VFS_NODE_DIRECTORY) {
[230260ac]499 fibril_rwlock_read_unlock(&namespace_rwlock);
[b7c62a9]500 }
[6c89f20]501
[72bde81]502 /* Unlock the VFS node. */
[354b642]503 if (rlock) {
[230260ac]504 fibril_rwlock_read_unlock(&file->node->contents_rwlock);
[354b642]505 } else {
[861e7d1]506 /* Update the cached version of node's size. */
[354b642]507 if (rc == EOK) {
[5bb9907]508 file->node->size = MERGE_LOUP32(IPC_GET_ARG2(answer),
509 IPC_GET_ARG3(answer));
[354b642]510 }
[230260ac]511 fibril_rwlock_write_unlock(&file->node->contents_rwlock);
[861e7d1]512 }
[6c89f20]513
[72bde81]514 /* Update the position pointer and unlock the open file. */
[354b642]515 if (rc == EOK) {
[f7017572]516 file->pos += bytes;
[354b642]517 }
[4fe94c66]518 vfs_file_put(file);
519
[e503517a]520 return rc;
521}
[861e7d1]522
[e503517a]523int 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
[0d35511]528int vfs_op_read(int fd, size_t *out_bytes)
[861e7d1]529{
[0d35511]530 return vfs_rdwr(fd, true, rdwr_ipc_client, out_bytes);
[861e7d1]531}
532
[0d35511]533int vfs_op_rename(int basefd, char *old, char *new)
[861e7d1]534{
[0d35511]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 return EINVAL;
555 }
556 assert(old[shared] == '/');
557 assert(new[shared] == '/');
558
559 fibril_rwlock_write_lock(&namespace_rwlock);
560
561 /* Resolve the shared portion of the path first. */
562 if (shared != 0) {
563 old[shared] = 0;
564 rc = vfs_lookup_internal(base, old, L_DIRECTORY, &base_lr);
565 if (rc != EOK) {
566 fibril_rwlock_write_unlock(&namespace_rwlock);
567 return rc;
568 }
569
570 vfs_node_put(base);
571 base = vfs_node_get(&base_lr);
572 old[shared] = '/';
573 old += shared;
574 new += shared;
575 }
576
577 rc = vfs_lookup_internal(base, new, L_UNLINK | L_DISABLE_MOUNTS,
578 &new_lr_orig);
579 if (rc == EOK) {
580 orig_unlinked = true;
581 } else if (rc != ENOENT) {
582 vfs_node_put(base);
583 fibril_rwlock_write_unlock(&namespace_rwlock);
584 return rc;
585 }
586
587 rc = vfs_lookup_internal(base, old, L_UNLINK | L_DISABLE_MOUNTS,
588 &old_lr);
589 if (rc != EOK) {
590 if (orig_unlinked) {
591 vfs_link_internal(base, new, &new_lr_orig.triplet);
592 }
593 vfs_node_put(base);
594 fibril_rwlock_write_unlock(&namespace_rwlock);
595 return rc;
596 }
597
598 rc = vfs_link_internal(base, new, &old_lr.triplet);
599 if (rc != EOK) {
600 vfs_link_internal(base, old, &old_lr.triplet);
601 if (orig_unlinked) {
602 vfs_link_internal(base, new, &new_lr_orig.triplet);
603 }
604 vfs_node_put(base);
605 fibril_rwlock_write_unlock(&namespace_rwlock);
606 return rc;
607 }
608
609 /* If the node is not held by anyone, try to destroy it. */
610 if (orig_unlinked && vfs_node_peek(&new_lr_orig) == NULL) {
611 out_destroy(&new_lr_orig.triplet);
612 }
613
614 vfs_node_put(base);
615 fibril_rwlock_write_unlock(&namespace_rwlock);
616 return EOK;
[861e7d1]617}
618
[0d35511]619int vfs_op_seek(int fd, int64_t offset, int whence, int64_t *out_offset)
[861e7d1]620{
621 vfs_file_t *file = vfs_file_get(fd);
622 if (!file) {
[0d35511]623 return EBADF;
[861e7d1]624 }
[ed903174]625
626 switch (whence) {
[8df8415]627 case SEEK_SET:
[0d35511]628 if (offset < 0) {
[4fe94c66]629 vfs_file_put(file);
[0d35511]630 return EINVAL;
[8df8415]631 }
[0d35511]632 file->pos = offset;
633 *out_offset = offset;
634 vfs_file_put(file);
635 return EOK;
[8df8415]636 case SEEK_CUR:
[0d35511]637 if (offset > 0 && file->pos > (INT64_MAX - offset)) {
[4fe94c66]638 vfs_file_put(file);
[0d35511]639 return EOVERFLOW;
[8df8415]640 }
641
[0d35511]642 if (offset < 0 && -file->pos > offset) {
[4fe94c66]643 vfs_file_put(file);
[0d35511]644 return EOVERFLOW;
[8df8415]645 }
646
[0d35511]647 file->pos += offset;
648 *out_offset = file->pos;
[4fe94c66]649 vfs_file_put(file);
[0d35511]650 return EOK;
[8df8415]651 case SEEK_END:
652 fibril_rwlock_read_lock(&file->node->contents_rwlock);
[0d35511]653 int64_t size = vfs_node_get_size(file->node);
654 fibril_rwlock_read_unlock(&file->node->contents_rwlock);
[8df8415]655
[0d35511]656 if (offset > 0 && size > (INT64_MAX - offset)) {
[4fe94c66]657 vfs_file_put(file);
[0d35511]658 return EOVERFLOW;
[8df8415]659 }
660
[0d35511]661 if (offset < 0 && -size > offset) {
[4fe94c66]662 vfs_file_put(file);
[0d35511]663 return EOVERFLOW;
[8df8415]664 }
665
[0d35511]666 file->pos = size + offset;
667 *out_offset = file->pos;
[4fe94c66]668 vfs_file_put(file);
[0d35511]669 return EOK;
[861e7d1]670 }
[ed903174]671
[4fe94c66]672 vfs_file_put(file);
[0d35511]673 return EINVAL;
[861e7d1]674}
675
[0d35511]676int vfs_op_statfs(int fd)
[7fe1f75]677{
[0d35511]678 ipc_callid_t callid;
[7fe1f75]679
[0d35511]680 if (!async_data_read_receive(&callid, NULL)) {
681 async_answer_0(callid, EINVAL);
682 return EINVAL;
683 }
[0ee4322]684
685 vfs_file_t *file = vfs_file_get(fd);
686 if (!file) {
[0d35511]687 async_answer_0(callid, EBADF);
688 return EBADF;
[0ee4322]689 }
690
[0d35511]691 vfs_node_t *node = file->node;
692
693 async_exch_t *exch = vfs_exchange_grab(node->fs_handle);
694
695 aid_t msg;
696 msg = async_send_3(exch, VFS_OUT_STATFS, node->service_id,
697 node->index, false, NULL);
698 async_forward_fast(callid, exch, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
699
700 vfs_exchange_release(exch);
701
702 sysarg_t rv;
703 async_wait_for(msg, &rv);
[0ee4322]704
[4fe94c66]705 vfs_file_put(file);
[0d35511]706 return rv;
[861e7d1]707}
708
[0d35511]709int vfs_op_sync(int fd)
[852b801]710{
711 vfs_file_t *file = vfs_file_get(fd);
712 if (!file) {
[0d35511]713 return EBADF;
[852b801]714 }
[0d35511]715
716 async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
[852b801]717
718 aid_t msg;
[0d35511]719 ipc_call_t answer;
720 msg = async_send_2(fs_exch, VFS_OUT_SYNC, file->node->service_id,
721 file->node->index, &answer);
[79ae36dd]722
[0d35511]723 vfs_exchange_release(fs_exch);
[79ae36dd]724
[0d35511]725 sysarg_t rc;
[852b801]726 async_wait_for(msg, &rc);
[79ae36dd]727
[4fe94c66]728 vfs_file_put(file);
[0d35511]729 return rc;
730
[852b801]731}
732
[0d35511]733static int vfs_truncate_internal(fs_handle_t fs_handle, service_id_t service_id,
734 fs_index_t index, aoff64_t size)
[5bcd5b7]735{
[0d35511]736 async_exch_t *exch = vfs_exchange_grab(fs_handle);
737 sysarg_t rc = async_req_4_0(exch, VFS_OUT_TRUNCATE,
738 (sysarg_t) service_id, (sysarg_t) index, LOWER32(size),
739 UPPER32(size));
740 vfs_exchange_release(exch);
741
742 return (int) rc;
[5bcd5b7]743}
744
[0d35511]745int vfs_op_truncate(int fd, int64_t size)
[852b801]746{
[0d35511]747 vfs_file_t *file = vfs_file_get(fd);
748 if (!file) {
749 return EBADF;
750 }
751
752 fibril_rwlock_write_lock(&file->node->contents_rwlock);
753
754 int rc = vfs_truncate_internal(file->node->fs_handle,
755 file->node->service_id, file->node->index, size);
756 if (rc == EOK) {
757 file->node->size = size;
758 }
759
760 fibril_rwlock_write_unlock(&file->node->contents_rwlock);
761 vfs_file_put(file);
762 return rc;
763}
764
765int vfs_op_unlink2(int parentfd, int expectfd, int wflag, char *path)
766{
767 int rc = EOK;
[20c071d]768 vfs_file_t *parent = NULL;
769 vfs_file_t *expect = NULL;
770
[5126f80]771 if (parentfd == expectfd) {
[0d35511]772 return EINVAL;
[5126f80]773 }
[472c09d]774
[20c071d]775 fibril_rwlock_write_lock(&namespace_rwlock);
776
777 int lflag = (wflag&WALK_DIRECTORY) ? L_DIRECTORY: 0;
[415c7e0d]778
[0d35511]779 /*
780 * Files are retrieved in order of file descriptors, to prevent
781 * deadlock.
782 */
[5126f80]783 if (parentfd < expectfd) {
[20c071d]784 parent = vfs_file_get(parentfd);
785 if (!parent) {
[5126f80]786 rc = EBADF;
[20c071d]787 goto exit;
788 }
789 }
790
791 if (expectfd >= 0) {
792 expect = vfs_file_get(expectfd);
793 if (!expect) {
[0d35511]794 rc = EBADF;
[20c071d]795 goto exit;
796 }
[c577a9a]797 }
798
[5126f80]799 if (parentfd > expectfd) {
[c577a9a]800 parent = vfs_file_get(parentfd);
801 if (!parent) {
[5126f80]802 rc = EBADF;
[c577a9a]803 goto exit;
804 }
805 }
806
[5126f80]807 assert(parent != NULL);
[c577a9a]808
809 if (expectfd >= 0) {
[20c071d]810 vfs_lookup_res_t lr;
[5126f80]811 rc = vfs_lookup_internal(parent->node, path, lflag, &lr);
[20c071d]812 if (rc != EOK) {
813 goto exit;
814 }
815
[a274a5f]816 vfs_node_t *found_node = vfs_node_peek(&lr);
817 if (expect->node != found_node) {
[20c071d]818 rc = ENOENT;
819 goto exit;
820 }
821
822 vfs_file_put(expect);
823 expect = NULL;
824 }
825
[415c7e0d]826 vfs_lookup_res_t lr;
[5126f80]827 rc = vfs_lookup_internal(parent->node, path, lflag | L_UNLINK, &lr);
[415c7e0d]828 if (rc != EOK) {
[20c071d]829 goto exit;
[415c7e0d]830 }
[20c071d]831
[5bcd5b7]832 /* If the node is not held by anyone, try to destroy it. */
833 if (vfs_node_peek(&lr) == NULL) {
834 out_destroy(&lr.triplet);
[415c7e0d]835 }
836
[20c071d]837exit:
838 if (path) {
839 free(path);
840 }
841 if (parent) {
842 vfs_file_put(parent);
843 }
844 if (expect) {
845 vfs_file_put(expect);
846 }
847 fibril_rwlock_write_unlock(&namespace_rwlock);
[0d35511]848 return rc;
[20c071d]849}
[415c7e0d]850
[0d35511]851int vfs_op_unmount(int mpfd)
[a8e9ab8d]852{
[0d35511]853 vfs_file_t *mp = vfs_file_get(mpfd);
854 if (mp == NULL) {
855 return EBADF;
[a8e9ab8d]856 }
[472c09d]857
[0d35511]858 if (mp->node->mount == NULL) {
859 vfs_file_put(mp);
860 return ENOENT;
[a8e9ab8d]861 }
[72bde81]862
[230260ac]863 fibril_rwlock_write_lock(&namespace_rwlock);
[472c09d]864
[0d35511]865 /*
866 * Count the total number of references for the mounted file system. We
867 * are expecting at least one, which is held by the mount point.
868 * If we find more, it means that
869 * the file system cannot be gracefully unmounted at the moment because
870 * someone is working with it.
871 */
872 if (vfs_nodes_refcount_sum_get(mp->node->mount->fs_handle,
873 mp->node->mount->service_id) != 1) {
874 vfs_file_put(mp);
[230260ac]875 fibril_rwlock_write_unlock(&namespace_rwlock);
[0d35511]876 return EBUSY;
[a8e9ab8d]877 }
[472c09d]878
[0d35511]879 async_exch_t *exch = vfs_exchange_grab(mp->node->mount->fs_handle);
880 int rc = async_req_1_0(exch, VFS_OUT_UNMOUNTED,
881 mp->node->mount->service_id);
882 vfs_exchange_release(exch);
[f15cf1a6]883
884 if (rc != EOK) {
[0d35511]885 vfs_file_put(mp);
[230260ac]886 fibril_rwlock_write_unlock(&namespace_rwlock);
[778d26d]887 return rc;
[f15cf1a6]888 }
[472c09d]889
[0d35511]890 vfs_node_forget(mp->node->mount);
891 vfs_node_put(mp->node);
892 mp->node->mount = NULL;
[472c09d]893
[230260ac]894 fibril_rwlock_write_unlock(&namespace_rwlock);
[0d35511]895
896 vfs_file_put(mp);
[778d26d]897 return EOK;
[f15cf1a6]898}
899
[0d35511]900int vfs_op_wait_handle(bool high_fd)
[a8e9ab8d]901{
[0d35511]902 return vfs_wait_handle_internal(high_fd);
903}
904
905static inline bool walk_flags_valid(int flags)
906{
[4809715]907 if ((flags & ~WALK_ALL_FLAGS) != 0) {
[0d35511]908 return false;
[a8e9ab8d]909 }
[4809715]910 if ((flags & WALK_MAY_CREATE) && (flags & WALK_MUST_CREATE)) {
[0d35511]911 return false;
[a8e9ab8d]912 }
[4809715]913 if ((flags & WALK_REGULAR) && (flags & WALK_DIRECTORY)) {
[0d35511]914 return false;
[a8e9ab8d]915 }
[4809715]916 if ((flags & WALK_MAY_CREATE) || (flags & WALK_MUST_CREATE)) {
917 if (!(flags & WALK_DIRECTORY) && !(flags & WALK_REGULAR)) {
[0d35511]918 return false;
919 }
[a8e9ab8d]920 }
[0d35511]921 return true;
922}
[778d26d]923
[0d35511]924static inline int walk_lookup_flags(int flags)
925{
926 int lflags = 0;
[4809715]927 if ((flags & WALK_MAY_CREATE) || (flags & WALK_MUST_CREATE)) {
[0d35511]928 lflags |= L_CREATE;
929 }
[4809715]930 if (flags & WALK_MUST_CREATE) {
[0d35511]931 lflags |= L_EXCLUSIVE;
[a8e9ab8d]932 }
[4809715]933 if (flags & WALK_REGULAR) {
[0d35511]934 lflags |= L_FILE;
[a8e9ab8d]935 }
[4809715]936 if (flags & WALK_DIRECTORY) {
[0d35511]937 lflags |= L_DIRECTORY;
[778d26d]938 }
[4809715]939 if (flags & WALK_MOUNT_POINT) {
[0d35511]940 lflags |= L_MP;
941 }
942 return lflags;
[a8e9ab8d]943}
944
[0d35511]945int vfs_op_walk(int parentfd, int flags, char *path, int *out_fd)
[2b88074b]946{
[0d35511]947 if (!walk_flags_valid(flags)) {
948 return EINVAL;
[4fe94c66]949 }
950
[0d35511]951 vfs_file_t *parent = vfs_file_get(parentfd);
952 if (!parent) {
953 return EBADF;
[2b88074b]954 }
955
[0d35511]956 fibril_rwlock_read_lock(&namespace_rwlock);
[2b88074b]957
[0d35511]958 vfs_lookup_res_t lr;
959 int rc = vfs_lookup_internal(parent->node, path,
960 walk_lookup_flags(flags), &lr);
[76a67ce]961
[0d35511]962 if (rc != EOK) {
963 fibril_rwlock_read_unlock(&namespace_rwlock);
964 vfs_file_put(parent);
965 return rc;
[76a67ce]966 }
[9dc6083]967
[0d35511]968 vfs_node_t *node = vfs_node_get(&lr);
[9dc6083]969
[0d35511]970 vfs_file_t *file;
971 int fd = vfs_fd_alloc(&file, false);
972 if (fd < 0) {
973 vfs_node_put(node);
974 vfs_file_put(parent);
975 return fd;
976 }
977 assert(file != NULL);
[9dc6083]978
[0d35511]979 file->node = node;
980 file->permissions = parent->permissions;
981 file->open_read = false;
982 file->open_write = false;
[9dc6083]983
[a737667e]984 vfs_file_put(file);
[0d35511]985 vfs_file_put(parent);
986
987 fibril_rwlock_read_unlock(&namespace_rwlock);
[9dc6083]988
[0d35511]989 *out_fd = fd;
990 return EOK;
[66366470]991}
992
[0d35511]993int vfs_op_write(int fd, size_t *out_bytes)
[354b642]994{
[0d35511]995 return vfs_rdwr(fd, false, rdwr_ipc_client, out_bytes);
[354b642]996}
997
[861e7d1]998/**
999 * @}
[05b9912]1000 */
Note: See TracBrowser for help on using the repository browser.