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

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

Merge dup2() into vfs_clone()

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