source: mainline/uspace/srv/vfs/vfs_ops.c@ 6ad454f

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

Pass file handles separately from error codes.

  • Property mode set to 100644
File size: 21.0 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
[f77c1c9]88int vfs_op_clone(int oldfd, int newfd, bool desc, int *out_fd)
[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 /* 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);
[0d35511]114
[fcab7ef]115 vfs_file_put(newfile);
116 }
[0d35511]117 }
118 vfs_file_put(oldfile);
119
[6ad454f]120 return rc;
[0d35511]121}
122
[9c4cf0d]123int vfs_op_put(int fd)
[0d35511]124{
125 return vfs_fd_free(fd);
126}
127
128static int vfs_connect_internal(service_id_t service_id, unsigned flags,
129 unsigned instance, const char *options, const char *fsname,
130 vfs_node_t **root)
[861e7d1]131{
[4636a60]132 fs_handle_t fs_handle = 0;
[05b9912]133
[4636a60]134 fibril_mutex_lock(&fs_list_lock);
[61042de]135 while (true) {
[4636a60]136 fs_handle = fs_name_to_handle(instance, fsname, false);
[8dc72b64]137
[61042de]138 if (fs_handle != 0 || !(flags & VFS_MOUNT_BLOCKING))
[4636a60]139 break;
[8dc72b64]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;
[5bcd5b7]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 */
154 sysarg_t rc = async_data_write_start(exch, options, str_size(options));
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 }
[5bcd5b7]166
[4636a60]167 vfs_lookup_res_t res;
168 res.triplet.fs_handle = fs_handle;
169 res.triplet.service_id = service_id;
170 res.triplet.index = (fs_index_t) IPC_GET_ARG1(answer);
[0d35511]171 res.size = (int64_t) MERGE_LOUP32(IPC_GET_ARG2(answer),
172 IPC_GET_ARG3(answer));
[4636a60]173 res.type = VFS_NODE_DIRECTORY;
[5bcd5b7]174
[4636a60]175 /* Add reference to the mounted root. */
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 }
[8dc72b64]184
[c990ee6]185 vfs_exchange_release(exch);
186
[4636a60]187 return EOK;
[5bcd5b7]188}
189
[d2c8533]190int vfs_op_fsprobe(const char *fs_name, service_id_t sid,
191 vfs_fs_probe_info_t *info)
192{
193 fs_handle_t fs_handle = 0;
194 sysarg_t rc;
195 int retval;
196
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);
200
201 if (fs_handle == 0)
202 return ENOFS;
203
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;
211
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 }
218
219 async_wait_for(msg, &rc);
220 vfs_exchange_release(exch);
221 return rc;
222}
223
[0d35511]224int 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{
[0d35511]227 int rc;
[5126f80]228 vfs_file_t *mp = NULL;
229 vfs_file_t *file = NULL;
[6ad454f]230 *out_fd = -1;
[5126f80]231
232 if (!(flags & VFS_MOUNT_CONNECT_ONLY)) {
233 mp = vfs_file_get(mpfd);
234 if (mp == NULL) {
235 rc = EBADF;
236 goto out;
237 }
238
239 if (mp->node->mount != NULL) {
240 rc = EBUSY;
241 goto out;
242 }
243
244 if (mp->node->type != VFS_NODE_DIRECTORY) {
245 rc = ENOTDIR;
246 goto out;
247 }
248
249 if (vfs_node_has_children(mp->node)) {
250 rc = ENOTEMPTY;
251 goto out;
252 }
253 }
254
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 }
[4636a60]261
[5126f80]262 vfs_node_t *root = NULL;
263
[4636a60]264 fibril_rwlock_write_lock(&namespace_rwlock);
[6f9ef87a]265
[0d35511]266 rc = vfs_connect_internal(service_id, flags, instance, opts, fs_name,
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 }
273
274 fibril_rwlock_write_unlock(&namespace_rwlock);
275
[61042de]276 if (rc != EOK)
[5126f80]277 goto out;
278
279 if (flags & VFS_MOUNT_NO_REF) {
280 vfs_node_delref(root);
281 } else {
282 assert(file != NULL);
283
284 file->node = root;
285 file->permissions = MODE_READ | MODE_WRITE | MODE_APPEND;
286 file->open_read = false;
287 file->open_write = false;
288 }
289
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 }
[0d35511]300
301 return rc;
[8dc72b64]302}
303
[b19e892]304int 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;
312
[b19e892]313 if ((mode & ~file->permissions) != 0) {
[0d35511]314 vfs_file_put(file);
315 return EPERM;
[0b18364]316 }
[0d35511]317
318 if (file->open_read || file->open_write) {
319 vfs_file_put(file);
320 return EBUSY;
[cb65bbe]321 }
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;
[05b9912]326
[cb65bbe]327 if (!file->open_read && !file->open_write) {
328 vfs_file_put(file);
[0d35511]329 return EINVAL;
[6f2c1ff]330 }
[05b9912]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 }
[05b9912]337
[cb65bbe]338 int rc = vfs_open_node_remote(file->node);
339 if (rc != EOK) {
340 file->open_read = file->open_write = false;
341 vfs_file_put(file);
[0d35511]342 return rc;
[05b9912]343 }
344
[4fe94c66]345 vfs_file_put(file);
[0d35511]346 return EOK;
[05b9912]347}
348
[58898d1d]349typedef int (* rdwr_ipc_cb_t)(async_exch_t *, vfs_file_t *, aoff64_t,
350 ipc_call_t *, bool, void *);
[42d08592]351
[58898d1d]352static int 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;
356 int rc;
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
376 *bytes = IPC_GET_ARG1(*answer);
377 return rc;
[42d08592]378}
[e503517a]379
[58898d1d]380static int 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;
[42d08592]387
[e503517a]388 aid_t msg = async_send_fast(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
394 int retval = async_data_read_start(exch, chunk->buffer, chunk->size);
395 if (retval != EOK) {
396 async_forget(msg);
397 return retval;
398 }
399
400 sysarg_t rc;
401 async_wait_for(msg, &rc);
402
403 chunk->size = IPC_GET_ARG1(*answer);
404
405 return (int) rc;
406}
407
[58898d1d]408static int vfs_rdwr(int fd, aoff64_t pos, bool read, rdwr_ipc_cb_t ipc_cb,
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 */
[79ae36dd]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;
[6c89f20]425
[cb65bbe]426 if ((read && !file->open_read) || (!read && !file->open_write)) {
[c577a9a]427 vfs_file_put(file);
[1dff985]428 return EINVAL;
[cb65bbe]429 }
430
[79ae36dd]431 vfs_info_t *fs_info = fs_handle_to_info(file->node->fs_handle);
432 assert(fs_info);
433
[0d35511]434 bool rlock = read ||
435 (fs_info->concurrent_read_write && fs_info->write_retains_size);
[354b642]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);
[79ae36dd]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 */
[354b642]452
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 }
464
[230260ac]465 fibril_rwlock_read_lock(&namespace_rwlock);
[b17186d]466 }
[6c89f20]467
[79ae36dd]468 async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
[861e7d1]469
[51774cd]470 if (!read && file->append)
[58898d1d]471 pos = file->node->size;
[42d08592]472
[861e7d1]473 /*
[42d08592]474 * Handle communication with the endpoint FS.
[861e7d1]475 */
[b4cbef1]476 ipc_call_t answer;
[58898d1d]477 int rc = ipc_cb(fs_exch, file, pos, &answer, read, ipc_cb_data);
[05b9912]478
[79ae36dd]479 vfs_exchange_release(fs_exch);
[34ca870]480
[61042de]481 if (file->node->type == VFS_NODE_DIRECTORY)
[230260ac]482 fibril_rwlock_read_unlock(&namespace_rwlock);
[6c89f20]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) {
[5bb9907]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 }
[6c89f20]495
[4fe94c66]496 vfs_file_put(file);
497
[e503517a]498 return rc;
499}
[861e7d1]500
[58898d1d]501int 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
[58898d1d]506int 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
[0d35511]511int 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);
520
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;
525
526 int rc;
527
528 size_t shared = shared_path(old, new);
529
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] == '/');
537
538 fibril_rwlock_write_lock(&namespace_rwlock);
539
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 }
549
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 }
[0d35511]567
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 }
597
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 }
606
607 vfs_node_put(base);
608 fibril_rwlock_write_unlock(&namespace_rwlock);
609 return EOK;
[861e7d1]610}
611
[67e881c]612int vfs_op_resize(int fd, int64_t size)
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);
619
620 int rc = vfs_truncate_internal(file->node->fs_handle,
621 file->node->service_id, file->node->index, size);
622 if (rc == EOK)
623 file->node->size = size;
624
625 fibril_rwlock_write_unlock(&file->node->contents_rwlock);
626 vfs_file_put(file);
627 return rc;
628}
629
[fe91f66]630int vfs_op_stat(int fd)
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);
639 int rc = async_data_read_forward_fast(exch, VFS_OUT_STAT,
640 node->service_id, node->index, true, 0, NULL);
641 vfs_exchange_release(exch);
642
643 vfs_file_put(file);
644 return rc;
645}
646
[0d35511]647int 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);
[35e81e2]656 int rc = async_data_read_forward_fast(exch, VFS_OUT_STATFS,
657 node->service_id, node->index, false, 0, NULL);
[0d35511]658 vfs_exchange_release(exch);
659
[4fe94c66]660 vfs_file_put(file);
[35e81e2]661 return rc;
[861e7d1]662}
663
[0d35511]664int vfs_op_sync(int fd)
[852b801]665{
666 vfs_file_t *file = vfs_file_get(fd);
[61042de]667 if (!file)
[0d35511]668 return EBADF;
669
670 async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
[852b801]671
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);
[79ae36dd]676
[0d35511]677 vfs_exchange_release(fs_exch);
[79ae36dd]678
[0d35511]679 sysarg_t rc;
[852b801]680 async_wait_for(msg, &rc);
[79ae36dd]681
[4fe94c66]682 vfs_file_put(file);
[0d35511]683 return rc;
684
[852b801]685}
686
[0d35511]687static int vfs_truncate_internal(fs_handle_t fs_handle, service_id_t service_id,
688 fs_index_t index, aoff64_t size)
[5bcd5b7]689{
[0d35511]690 async_exch_t *exch = vfs_exchange_grab(fs_handle);
691 sysarg_t rc = async_req_4_0(exch, VFS_OUT_TRUNCATE,
692 (sysarg_t) service_id, (sysarg_t) index, LOWER32(size),
693 UPPER32(size));
694 vfs_exchange_release(exch);
695
696 return (int) rc;
[5bcd5b7]697}
698
[79ea5af]699int vfs_op_unlink(int parentfd, int expectfd, char *path)
[0d35511]700{
701 int rc = EOK;
[20c071d]702 vfs_file_t *parent = NULL;
703 vfs_file_t *expect = NULL;
704
[61042de]705 if (parentfd == expectfd)
[0d35511]706 return EINVAL;
[472c09d]707
[20c071d]708 fibril_rwlock_write_lock(&namespace_rwlock);
709
[0d35511]710 /*
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 }
721
722 if (expectfd >= 0) {
723 expect = vfs_file_get(expectfd);
724 if (!expect) {
[0d35511]725 rc = EBADF;
[20c071d]726 goto exit;
727 }
[c577a9a]728 }
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 }
737
[5126f80]738 assert(parent != NULL);
[c577a9a]739
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;
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 }
752
753 vfs_file_put(expect);
754 expect = NULL;
755 }
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
[0d35511]780int 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;
[472c09d]785
[0d35511]786 if (mp->node->mount == NULL) {
787 vfs_file_put(mp);
788 return ENOENT;
[a8e9ab8d]789 }
[72bde81]790
[230260ac]791 fibril_rwlock_write_lock(&namespace_rwlock);
[472c09d]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 }
[472c09d]806
[0d35511]807 async_exch_t *exch = vfs_exchange_grab(mp->node->mount->fs_handle);
808 int rc = async_req_1_0(exch, VFS_OUT_UNMOUNTED,
809 mp->node->mount->service_id);
810 vfs_exchange_release(exch);
[f15cf1a6]811
812 if (rc != EOK) {
[0d35511]813 vfs_file_put(mp);
[230260ac]814 fibril_rwlock_write_unlock(&namespace_rwlock);
[778d26d]815 return rc;
[f15cf1a6]816 }
[472c09d]817
[0d35511]818 vfs_node_forget(mp->node->mount);
819 vfs_node_put(mp->node);
820 mp->node->mount = NULL;
[472c09d]821
[230260ac]822 fibril_rwlock_write_unlock(&namespace_rwlock);
[0d35511]823
824 vfs_file_put(mp);
[778d26d]825 return EOK;
[f15cf1a6]826}
827
[6ad454f]828int 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
[0d35511]864int 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;
[4fe94c66]868
[0d35511]869 vfs_file_t *parent = vfs_file_get(parentfd);
[61042de]870 if (!parent)
[0d35511]871 return EBADF;
[2b88074b]872
[0d35511]873 fibril_rwlock_read_lock(&namespace_rwlock);
[2b88074b]874
[0d35511]875 vfs_lookup_res_t lr;
876 int rc = vfs_lookup_internal(parent->node, path,
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 }
[9dc6083]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 }
[9dc6083]890
[0d35511]891 vfs_file_t *file;
[6ad454f]892 rc = vfs_fd_alloc(&file, false, out_fd);
893 if (rc != EOK) {
[0d35511]894 vfs_node_put(node);
895 vfs_file_put(parent);
[6ad454f]896 return rc;
[0d35511]897 }
898 assert(file != NULL);
[9dc6083]899
[0d35511]900 file->node = node;
901 file->permissions = parent->permissions;
902 file->open_read = false;
903 file->open_write = false;
[9dc6083]904
[a737667e]905 vfs_file_put(file);
[0d35511]906 vfs_file_put(parent);
907
908 fibril_rwlock_read_unlock(&namespace_rwlock);
[9dc6083]909
[0d35511]910 return EOK;
[66366470]911}
912
[58898d1d]913int vfs_op_write(int fd, aoff64_t pos, size_t *out_bytes)
[354b642]914{
[58898d1d]915 return vfs_rdwr(fd, pos, false, rdwr_ipc_client, out_bytes);
[354b642]916}
917
[861e7d1]918/**
919 * @}
[05b9912]920 */
Note: See TracBrowser for help on using the repository browser.