source: mainline/uspace/srv/vfs/vfs_ops.c@ 582a0b8

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

Remove unistd.h

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