source: mainline/uspace/srv/vfs/vfs_ops.c@ f92b315

Last change on this file since f92b315 was 504d103, checked in by Matthieu Riolo <matthieu.riolo@…>, 6 years ago

sysman: Call starting by name appropriately
Conflicts:

uspace/srv/devman/driver.c

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