source: mainline/uspace/srv/vfs/vfs_ops.c@ 55a7fee

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

Streamline vfs_op_fstat()

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