source: mainline/uspace/srv/vfs/vfs_ops.c@ 79ea5af

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

Rename unlink() to vfs_unlink_path() and _vfs_unlink() to vfs_unlink()

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