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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since eb13ef8 was eb13ef8, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 6 years ago

Change IPC_GET_* and IPC_SET_* to accept pointer instead of lvalue

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