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

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

Fix cstyle

  • Property mode set to 100644
File size: 24.2 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>
[10e4cd7]54#include <vfs/vfs_mtab.h>
55
56FIBRIL_MUTEX_INITIALIZE(mtab_list_lock);
57LIST_INITIALIZE(mtab_list);
58static size_t mtab_size = 0;
[861e7d1]59
[7fe1f75]60/* Forward declarations of static functions. */
[15f3c3f]61static int vfs_truncate_internal(fs_handle_t, service_id_t, fs_index_t,
[8df8415]62 aoff64_t);
[7fe1f75]63
[861e7d1]64/**
65 * This rwlock prevents the race between a triplet-to-VFS-node resolution and a
66 * concurrent VFS operation which modifies the file system namespace.
67 */
[230260ac]68FIBRIL_RWLOCK_INITIALIZE(namespace_rwlock);
[861e7d1]69
[0d35511]70static size_t shared_path(char *a, char *b)
71{
72 size_t res = 0;
73
74 while (a[res] == b[res] && a[res] != 0) {
75 res++;
76 }
77
78 if (a[res] == b[res]) {
79 return res;
80 }
81
82 res--;
83 while (a[res] != '/') {
84 res--;
85 }
86 return res;
87}
88
89/* This call destroys the file if and only if there are no hard links left. */
90static void out_destroy(vfs_triplet_t *file)
91{
92 async_exch_t *exch = vfs_exchange_grab(file->fs_handle);
93 async_msg_2(exch, VFS_OUT_DESTROY,
94 (sysarg_t) file->service_id, (sysarg_t) file->index);
95 vfs_exchange_release(exch);
96}
97
98int vfs_op_clone(int oldfd, bool desc)
99{
100 /* Lookup the file structure corresponding to fd. */
101 vfs_file_t *oldfile = vfs_file_get(oldfd);
102 if (oldfile == NULL) {
103 return EBADF;
104 }
105 assert(oldfile->node != NULL);
106
107 vfs_file_t *newfile;
108 int newfd = vfs_fd_alloc(&newfile, desc);
109 if (newfd >= 0) {
110 newfile->node = oldfile->node;
111 newfile->permissions = oldfile->permissions;
112 vfs_node_addref(newfile->node);
113
114 vfs_file_put(newfile);
115 }
116 vfs_file_put(oldfile);
117
118 return newfd;
119}
120
121int vfs_op_close(int fd)
122{
123 return vfs_fd_free(fd);
124}
125
126int vfs_op_dup(int oldfd, int newfd)
127{
128 /* If the file descriptors are the same, do nothing. */
129 if (oldfd == newfd) {
130 return EOK;
131 }
132
133 /* Lookup the file structure corresponding to oldfd. */
134 vfs_file_t *oldfile = vfs_file_get(oldfd);
135 if (!oldfile) {
136 return EBADF;
137 }
138
139 /* Make sure newfd is closed. */
140 (void) vfs_fd_free(newfd);
141
142 /* Assign the old file to newfd. */
143 int ret = vfs_fd_assign(oldfile, newfd);
144 vfs_file_put(oldfile);
145
146 return ret;
147}
148
149int vfs_op_fstat_forward(int fd)
150{
151 vfs_file_t *file = vfs_file_get(fd);
152 if (!file) {
153 return EBADF;
154 }
155 assert(file->node);
156
157 ipc_callid_t callid;
158 if (!async_data_read_receive(&callid, NULL)) {
159 vfs_file_put(file);
160 async_answer_0(callid, EINVAL);
161 return EINVAL;
162 }
163
164 async_exch_t *exch = vfs_exchange_grab(file->node->fs_handle);
165 assert(exch);
166
167 aid_t msg;
168 msg = async_send_3(exch, VFS_OUT_STAT, file->node->service_id,
169 file->node->index, true, NULL);
170 assert(msg);
171 async_forward_fast(callid, exch, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
172
173 vfs_exchange_release(exch);
174
175 sysarg_t rc;
176 async_wait_for(msg, &rc);
177
178 vfs_file_put(file);
179 return rc;
180}
181
182static int vfs_connect_internal(service_id_t service_id, unsigned flags,
183 unsigned instance, const char *options, const char *fsname,
184 vfs_node_t **root)
[861e7d1]185{
[4636a60]186 fs_handle_t fs_handle = 0;
[05b9912]187
[4636a60]188 fibril_mutex_lock(&fs_list_lock);
189 while (1) {
190 fs_handle = fs_name_to_handle(instance, fsname, false);
[8dc72b64]191
[5126f80]192 if (fs_handle != 0 || !(flags & VFS_MOUNT_BLOCKING)) {
[4636a60]193 break;
[861e7d1]194 }
[8dc72b64]195
[4636a60]196 fibril_condvar_wait(&fs_list_cv, &fs_list_lock);
197 }
198 fibril_mutex_unlock(&fs_list_lock);
199
200 if (fs_handle == 0) {
201 return ENOENT;
[5bcd5b7]202 }
203
[4636a60]204 /* Tell the mountee that it is being mounted. */
205 ipc_call_t answer;
206 async_exch_t *exch = vfs_exchange_grab(fs_handle);
[0d35511]207 aid_t msg = async_send_1(exch, VFS_OUT_MOUNTED, (sysarg_t) service_id,
208 &answer);
[4636a60]209 /* Send the mount options */
210 sysarg_t rc = async_data_write_start(exch, options, str_size(options));
211 if (rc != EOK) {
212 async_forget(msg);
213 vfs_exchange_release(exch);
214 return rc;
[5bcd5b7]215 }
[4636a60]216 async_wait_for(msg, &rc);
217 vfs_exchange_release(exch);
[5bcd5b7]218
[4636a60]219 if (rc != EOK) {
220 return rc;
[5bcd5b7]221 }
222
[4636a60]223 vfs_lookup_res_t res;
224 res.triplet.fs_handle = fs_handle;
225 res.triplet.service_id = service_id;
226 res.triplet.index = (fs_index_t) IPC_GET_ARG1(answer);
[0d35511]227 res.size = (int64_t) MERGE_LOUP32(IPC_GET_ARG2(answer),
228 IPC_GET_ARG3(answer));
[4636a60]229 res.type = VFS_NODE_DIRECTORY;
[5bcd5b7]230
[4636a60]231 /* Add reference to the mounted root. */
232 *root = vfs_node_get(&res);
233 assert(*root);
[8dc72b64]234
[4636a60]235 return EOK;
[5bcd5b7]236}
237
[0d35511]238int vfs_op_mount(int mpfd, unsigned service_id, unsigned flags,
239 unsigned instance, const char *opts, const char *fs_name, int *outfd)
[8dc72b64]240{
[0d35511]241 int rc;
[5126f80]242 vfs_file_t *mp = NULL;
243 vfs_file_t *file = NULL;
244 int fd = -1;
245 mtab_ent_t *mtab_ent = NULL;
246
247 if (!(flags & VFS_MOUNT_CONNECT_ONLY)) {
248 mp = vfs_file_get(mpfd);
249 if (mp == NULL) {
250 rc = EBADF;
251 goto out;
252 }
253
254 if (mp->node->mount != NULL) {
255 rc = EBUSY;
256 goto out;
257 }
258
259 if (mp->node->type != VFS_NODE_DIRECTORY) {
260 rc = ENOTDIR;
261 goto out;
262 }
263
264 if (vfs_node_has_children(mp->node)) {
265 rc = ENOTEMPTY;
266 goto out;
267 }
268 }
269
270 if (!(flags & VFS_MOUNT_NO_REF)) {
271 fd = vfs_fd_alloc(&file, false);
272 if (fd < 0) {
273 rc = fd;
274 goto out;
275 }
[c08c355]276 }
[4636a60]277
[460514d]278 /* Add the filesystem info to the list of mounted filesystems */
[5126f80]279 mtab_ent = malloc(sizeof(mtab_ent_t));
[460514d]280 if (!mtab_ent) {
[5126f80]281 rc = ENOMEM;
282 goto out;
[460514d]283 }
[4636a60]284
[5126f80]285 vfs_node_t *root = NULL;
286
[4636a60]287 fibril_rwlock_write_lock(&namespace_rwlock);
[6f9ef87a]288
[0d35511]289 rc = vfs_connect_internal(service_id, flags, instance, opts, fs_name,
290 &root);
[5126f80]291 if (rc == EOK && !(flags & VFS_MOUNT_CONNECT_ONLY)) {
292 vfs_node_addref(mp->node);
293 vfs_node_addref(root);
294 mp->node->mount = root;
295 }
296
297 fibril_rwlock_write_unlock(&namespace_rwlock);
298
299 if (rc != EOK) {
300 goto out;
301 }
302
303
304 if (flags & VFS_MOUNT_NO_REF) {
305 vfs_node_delref(root);
306 } else {
307 assert(file != NULL);
308
309 file->node = root;
310 file->permissions = MODE_READ | MODE_WRITE | MODE_APPEND;
311 file->open_read = false;
312 file->open_write = false;
313 }
314
[10e4cd7]315 /* Add the filesystem info to the list of mounted filesystems */
[0d35511]316 str_cpy(mtab_ent->mp, MAX_PATH_LEN, "fixme");
317 str_cpy(mtab_ent->fs_name, FS_NAME_MAXLEN, fs_name);
318 str_cpy(mtab_ent->opts, MAX_MNTOPTS_LEN, opts);
319 mtab_ent->instance = instance;
320 mtab_ent->service_id = service_id;
[10e4cd7]321
[0d35511]322 link_initialize(&mtab_ent->link);
[10e4cd7]323
[0d35511]324 fibril_mutex_lock(&mtab_list_lock);
325 list_append(&mtab_ent->link, &mtab_list);
326 mtab_size++;
327 fibril_mutex_unlock(&mtab_list_lock);
[10e4cd7]328
[5126f80]329out:
330 if (mp) {
331 vfs_file_put(mp);
332 }
333 if (file) {
334 vfs_file_put(file);
335 }
336 if (rc != EOK && fd >= 0) {
337 vfs_fd_free(fd);
[0d35511]338 fd = 0;
[5126f80]339 }
[0d35511]340
341 *outfd = fd;
342 return rc;
[8dc72b64]343}
344
[0d35511]345int vfs_op_mtab_get(void)
[7f5e070]346{
[0d35511]347 ipc_callid_t callid;
348 ipc_call_t data;
349 sysarg_t rc = EOK;
350 size_t len;
351
[76a67ce]352 fibril_mutex_lock(&mtab_list_lock);
[0d35511]353
354 /* Send to the caller the number of mounted filesystems */
355 callid = async_get_call(&data);
356 if (IPC_GET_IMETHOD(data) != VFS_IN_PING) {
357 rc = ENOTSUP;
358 async_answer_0(callid, rc);
359 goto exit;
360 }
361 async_answer_1(callid, EOK, mtab_size);
[76a67ce]362
[feeac0d]363 list_foreach(mtab_list, link, mtab_ent_t, mtab_ent) {
[0d35511]364 rc = ENOTSUP;
365
366 if (!async_data_read_receive(&callid, &len)) {
367 async_answer_0(callid, rc);
368 goto exit;
369 }
370
371 (void) async_data_read_finalize(callid, mtab_ent->mp,
372 str_size(mtab_ent->mp));
373
374 if (!async_data_read_receive(&callid, &len)) {
375 async_answer_0(callid, rc);
376 goto exit;
377 }
378
379 (void) async_data_read_finalize(callid, mtab_ent->opts,
380 str_size(mtab_ent->opts));
381
382 if (!async_data_read_receive(&callid, &len)) {
383 async_answer_0(callid, rc);
384 goto exit;
385 }
386
387 (void) async_data_read_finalize(callid, mtab_ent->fs_name,
388 str_size(mtab_ent->fs_name));
389
390 callid = async_get_call(&data);
391
392 if (IPC_GET_IMETHOD(data) != VFS_IN_PING) {
393 async_answer_0(callid, rc);
394 goto exit;
[76a67ce]395 }
[0d35511]396
397 rc = EOK;
398 async_answer_2(callid, rc, mtab_ent->instance,
399 mtab_ent->service_id);
[76a67ce]400 }
[0d35511]401
402exit:
[4af2f36]403 fibril_mutex_unlock(&mtab_list_lock);
[0d35511]404 return rc;
[7f5e070]405}
406
[0d35511]407int vfs_op_open2(int fd, int flags)
[861e7d1]408{
[0d35511]409 if (flags == 0) {
410 return EINVAL;
[0b18364]411 }
[0d35511]412
413 vfs_file_t *file = vfs_file_get(fd);
414 if (!file) {
415 return EBADF;
[0b18364]416 }
[0d35511]417
418 if ((flags & ~file->permissions) != 0) {
419 vfs_file_put(file);
420 return EPERM;
[0b18364]421 }
[0d35511]422
423 if (file->open_read || file->open_write) {
424 vfs_file_put(file);
425 return EBUSY;
[cb65bbe]426 }
427
428 file->open_read = (flags & MODE_READ) != 0;
429 file->open_write = (flags & (MODE_WRITE | MODE_APPEND)) != 0;
430 file->append = (flags & MODE_APPEND) != 0;
[05b9912]431
[cb65bbe]432 if (!file->open_read && !file->open_write) {
433 vfs_file_put(file);
[0d35511]434 return EINVAL;
[6f2c1ff]435 }
[05b9912]436
[cb65bbe]437 if (file->node->type == VFS_NODE_DIRECTORY && file->open_write) {
438 file->open_read = file->open_write = false;
439 vfs_file_put(file);
[0d35511]440 return EINVAL;
[7fe1f75]441 }
[05b9912]442
[cb65bbe]443 int rc = vfs_open_node_remote(file->node);
444 if (rc != EOK) {
445 file->open_read = file->open_write = false;
446 vfs_file_put(file);
[0d35511]447 return rc;
[05b9912]448 }
449
[4fe94c66]450 vfs_file_put(file);
[0d35511]451 return EOK;
[05b9912]452}
453
[e503517a]454typedef int (* rdwr_ipc_cb_t)(async_exch_t *, vfs_file_t *, ipc_call_t *,
[42d08592]455 bool, void *);
456
[e503517a]457static int rdwr_ipc_client(async_exch_t *exch, vfs_file_t *file,
[42d08592]458 ipc_call_t *answer, bool read, void *data)
459{
[e503517a]460 size_t *bytes = (size_t *) data;
461 int rc;
462
[42d08592]463 /*
464 * Make a VFS_READ/VFS_WRITE request at the destination FS server
465 * and forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
466 * destination FS server. The call will be routed as if sent by
467 * ourselves. Note that call arguments are immutable in this case so we
468 * don't have to bother.
469 */
470
471 if (read) {
[e503517a]472 rc = async_data_read_forward_4_1(exch, VFS_OUT_READ,
[42d08592]473 file->node->service_id, file->node->index,
474 LOWER32(file->pos), UPPER32(file->pos), answer);
475 } else {
[e503517a]476 rc = async_data_write_forward_4_1(exch, VFS_OUT_WRITE,
[42d08592]477 file->node->service_id, file->node->index,
478 LOWER32(file->pos), UPPER32(file->pos), answer);
[e503517a]479 }
480
481 *bytes = IPC_GET_ARG1(*answer);
482 return rc;
[42d08592]483}
[e503517a]484
485static int rdwr_ipc_internal(async_exch_t *exch, vfs_file_t *file,
486 ipc_call_t *answer, bool read, void *data)
487{
488 rdwr_io_chunk_t *chunk = (rdwr_io_chunk_t *) data;
489
490 if (exch == NULL)
491 return ENOENT;
[42d08592]492
[e503517a]493 aid_t msg = async_send_fast(exch, read ? VFS_OUT_READ : VFS_OUT_WRITE,
494 file->node->service_id, file->node->index, LOWER32(file->pos),
495 UPPER32(file->pos), answer);
496 if (msg == 0)
497 return EINVAL;
498
499 int retval = async_data_read_start(exch, chunk->buffer, chunk->size);
500 if (retval != EOK) {
501 async_forget(msg);
502 return retval;
503 }
504
505 sysarg_t rc;
506 async_wait_for(msg, &rc);
507
508 chunk->size = IPC_GET_ARG1(*answer);
509
510 return (int) rc;
511}
512
513static int vfs_rdwr(int fd, bool read, rdwr_ipc_cb_t ipc_cb, void *ipc_cb_data)
[861e7d1]514{
515 /*
516 * The following code strongly depends on the fact that the files data
517 * structure can be only accessed by a single fibril and all file
518 * operations are serialized (i.e. the reads and writes cannot
519 * interleave and a file cannot be closed while it is being read).
520 *
521 * Additional synchronization needs to be added once the table of
522 * open files supports parallel access!
523 */
[79ae36dd]524
[72bde81]525 /* Lookup the file structure corresponding to the file descriptor. */
[861e7d1]526 vfs_file_t *file = vfs_file_get(fd);
[e503517a]527 if (!file)
[354b642]528 return EBADF;
[6c89f20]529
[cb65bbe]530 if ((read && !file->open_read) || (!read && !file->open_write)) {
[c577a9a]531 vfs_file_put(file);
[1dff985]532 return EINVAL;
[cb65bbe]533 }
534
[79ae36dd]535 vfs_info_t *fs_info = fs_handle_to_info(file->node->fs_handle);
536 assert(fs_info);
537
[0d35511]538 bool rlock = read ||
539 (fs_info->concurrent_read_write && fs_info->write_retains_size);
[354b642]540
[861e7d1]541 /*
542 * Lock the file's node so that no other client can read/write to it at
[c2f4b6b]543 * the same time unless the FS supports concurrent reads/writes and its
544 * write implementation does not modify the file size.
[861e7d1]545 */
[354b642]546 if (rlock) {
[230260ac]547 fibril_rwlock_read_lock(&file->node->contents_rwlock);
[354b642]548 } else {
[230260ac]549 fibril_rwlock_write_lock(&file->node->contents_rwlock);
[354b642]550 }
[79ae36dd]551
[b17186d]552 if (file->node->type == VFS_NODE_DIRECTORY) {
553 /*
554 * Make sure that no one is modifying the namespace
555 * while we are in readdir().
556 */
[354b642]557
558 if (!read) {
559 if (rlock) {
[0d35511]560 fibril_rwlock_read_unlock(
561 &file->node->contents_rwlock);
[354b642]562 } else {
[0d35511]563 fibril_rwlock_write_unlock(
564 &file->node->contents_rwlock);
[354b642]565 }
566 vfs_file_put(file);
567 return EINVAL;
568 }
569
[230260ac]570 fibril_rwlock_read_lock(&namespace_rwlock);
[b17186d]571 }
[6c89f20]572
[79ae36dd]573 async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
[861e7d1]574
[42d08592]575 if (!read && file->append)
576 file->pos = file->node->size;
577
[861e7d1]578 /*
[42d08592]579 * Handle communication with the endpoint FS.
[861e7d1]580 */
[b4cbef1]581 ipc_call_t answer;
[e503517a]582 int rc = ipc_cb(fs_exch, file, &answer, read, ipc_cb_data);
[05b9912]583
[79ae36dd]584 vfs_exchange_release(fs_exch);
[34ca870]585
[861e7d1]586 size_t bytes = IPC_GET_ARG1(answer);
[b4cbef1]587
[b7c62a9]588 if (file->node->type == VFS_NODE_DIRECTORY) {
[230260ac]589 fibril_rwlock_read_unlock(&namespace_rwlock);
[b7c62a9]590 }
[6c89f20]591
[72bde81]592 /* Unlock the VFS node. */
[354b642]593 if (rlock) {
[230260ac]594 fibril_rwlock_read_unlock(&file->node->contents_rwlock);
[354b642]595 } else {
[861e7d1]596 /* Update the cached version of node's size. */
[354b642]597 if (rc == EOK) {
[5bb9907]598 file->node->size = MERGE_LOUP32(IPC_GET_ARG2(answer),
599 IPC_GET_ARG3(answer));
[354b642]600 }
[230260ac]601 fibril_rwlock_write_unlock(&file->node->contents_rwlock);
[861e7d1]602 }
[6c89f20]603
[72bde81]604 /* Update the position pointer and unlock the open file. */
[354b642]605 if (rc == EOK) {
[f7017572]606 file->pos += bytes;
[354b642]607 }
[4fe94c66]608 vfs_file_put(file);
609
[e503517a]610 return rc;
611}
[861e7d1]612
[e503517a]613int vfs_rdwr_internal(int fd, bool read, rdwr_io_chunk_t *chunk)
614{
615 return vfs_rdwr(fd, read, rdwr_ipc_internal, chunk);
616}
617
[0d35511]618int vfs_op_read(int fd, size_t *out_bytes)
[861e7d1]619{
[0d35511]620 return vfs_rdwr(fd, true, rdwr_ipc_client, out_bytes);
[861e7d1]621}
622
[0d35511]623int vfs_op_rename(int basefd, char *old, char *new)
[861e7d1]624{
[0d35511]625 vfs_file_t *base_file = vfs_file_get(basefd);
626 if (!base_file) {
627 return EBADF;
628 }
629 vfs_node_t *base = base_file->node;
630 vfs_node_addref(base);
631 vfs_file_put(base_file);
632
633 vfs_lookup_res_t base_lr;
634 vfs_lookup_res_t old_lr;
635 vfs_lookup_res_t new_lr_orig;
636 bool orig_unlinked = false;
637
638 int rc;
639
640 size_t shared = shared_path(old, new);
641
642 /* Do not allow one path to be a prefix of the other. */
643 if (old[shared] == 0 || new[shared] == 0) {
644 return EINVAL;
645 }
646 assert(old[shared] == '/');
647 assert(new[shared] == '/');
648
649 fibril_rwlock_write_lock(&namespace_rwlock);
650
651 /* Resolve the shared portion of the path first. */
652 if (shared != 0) {
653 old[shared] = 0;
654 rc = vfs_lookup_internal(base, old, L_DIRECTORY, &base_lr);
655 if (rc != EOK) {
656 fibril_rwlock_write_unlock(&namespace_rwlock);
657 return rc;
658 }
659
660 vfs_node_put(base);
661 base = vfs_node_get(&base_lr);
662 old[shared] = '/';
663 old += shared;
664 new += shared;
665 }
666
667 rc = vfs_lookup_internal(base, new, L_UNLINK | L_DISABLE_MOUNTS,
668 &new_lr_orig);
669 if (rc == EOK) {
670 orig_unlinked = true;
671 } else if (rc != ENOENT) {
672 vfs_node_put(base);
673 fibril_rwlock_write_unlock(&namespace_rwlock);
674 return rc;
675 }
676
677 rc = vfs_lookup_internal(base, old, L_UNLINK | L_DISABLE_MOUNTS,
678 &old_lr);
679 if (rc != EOK) {
680 if (orig_unlinked) {
681 vfs_link_internal(base, new, &new_lr_orig.triplet);
682 }
683 vfs_node_put(base);
684 fibril_rwlock_write_unlock(&namespace_rwlock);
685 return rc;
686 }
687
688 rc = vfs_link_internal(base, new, &old_lr.triplet);
689 if (rc != EOK) {
690 vfs_link_internal(base, old, &old_lr.triplet);
691 if (orig_unlinked) {
692 vfs_link_internal(base, new, &new_lr_orig.triplet);
693 }
694 vfs_node_put(base);
695 fibril_rwlock_write_unlock(&namespace_rwlock);
696 return rc;
697 }
698
699 /* If the node is not held by anyone, try to destroy it. */
700 if (orig_unlinked && vfs_node_peek(&new_lr_orig) == NULL) {
701 out_destroy(&new_lr_orig.triplet);
702 }
703
704 vfs_node_put(base);
705 fibril_rwlock_write_unlock(&namespace_rwlock);
706 return EOK;
[861e7d1]707}
708
[0d35511]709int vfs_op_seek(int fd, int64_t offset, int whence, int64_t *out_offset)
[861e7d1]710{
711 vfs_file_t *file = vfs_file_get(fd);
712 if (!file) {
[0d35511]713 return EBADF;
[861e7d1]714 }
[ed903174]715
716 switch (whence) {
[8df8415]717 case SEEK_SET:
[0d35511]718 if (offset < 0) {
[4fe94c66]719 vfs_file_put(file);
[0d35511]720 return EINVAL;
[8df8415]721 }
[0d35511]722 file->pos = offset;
723 *out_offset = offset;
724 vfs_file_put(file);
725 return EOK;
[8df8415]726 case SEEK_CUR:
[0d35511]727 if (offset > 0 && file->pos > (INT64_MAX - offset)) {
[4fe94c66]728 vfs_file_put(file);
[0d35511]729 return EOVERFLOW;
[8df8415]730 }
731
[0d35511]732 if (offset < 0 && -file->pos > offset) {
[4fe94c66]733 vfs_file_put(file);
[0d35511]734 return EOVERFLOW;
[8df8415]735 }
736
[0d35511]737 file->pos += offset;
738 *out_offset = file->pos;
[4fe94c66]739 vfs_file_put(file);
[0d35511]740 return EOK;
[8df8415]741 case SEEK_END:
742 fibril_rwlock_read_lock(&file->node->contents_rwlock);
[0d35511]743 int64_t size = vfs_node_get_size(file->node);
744 fibril_rwlock_read_unlock(&file->node->contents_rwlock);
[8df8415]745
[0d35511]746 if (offset > 0 && size > (INT64_MAX - offset)) {
[4fe94c66]747 vfs_file_put(file);
[0d35511]748 return EOVERFLOW;
[8df8415]749 }
750
[0d35511]751 if (offset < 0 && -size > offset) {
[4fe94c66]752 vfs_file_put(file);
[0d35511]753 return EOVERFLOW;
[8df8415]754 }
755
[0d35511]756 file->pos = size + offset;
757 *out_offset = file->pos;
[4fe94c66]758 vfs_file_put(file);
[0d35511]759 return EOK;
[861e7d1]760 }
[ed903174]761
[4fe94c66]762 vfs_file_put(file);
[0d35511]763 return EINVAL;
[861e7d1]764}
765
[0d35511]766int vfs_op_statfs(int fd)
[7fe1f75]767{
[0d35511]768 ipc_callid_t callid;
[7fe1f75]769
[0d35511]770 if (!async_data_read_receive(&callid, NULL)) {
771 async_answer_0(callid, EINVAL);
772 return EINVAL;
773 }
[0ee4322]774
775 vfs_file_t *file = vfs_file_get(fd);
776 if (!file) {
[0d35511]777 async_answer_0(callid, EBADF);
778 return EBADF;
[0ee4322]779 }
780
[0d35511]781 vfs_node_t *node = file->node;
782
783 async_exch_t *exch = vfs_exchange_grab(node->fs_handle);
784
785 aid_t msg;
786 msg = async_send_3(exch, VFS_OUT_STATFS, node->service_id,
787 node->index, false, NULL);
788 async_forward_fast(callid, exch, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
789
790 vfs_exchange_release(exch);
791
792 sysarg_t rv;
793 async_wait_for(msg, &rv);
[0ee4322]794
[4fe94c66]795 vfs_file_put(file);
[0d35511]796 return rv;
[861e7d1]797}
798
[0d35511]799int vfs_op_sync(int fd)
[852b801]800{
801 vfs_file_t *file = vfs_file_get(fd);
802 if (!file) {
[0d35511]803 return EBADF;
[852b801]804 }
[0d35511]805
806 async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
[852b801]807
808 aid_t msg;
[0d35511]809 ipc_call_t answer;
810 msg = async_send_2(fs_exch, VFS_OUT_SYNC, file->node->service_id,
811 file->node->index, &answer);
[79ae36dd]812
[0d35511]813 vfs_exchange_release(fs_exch);
[79ae36dd]814
[0d35511]815 sysarg_t rc;
[852b801]816 async_wait_for(msg, &rc);
[79ae36dd]817
[4fe94c66]818 vfs_file_put(file);
[0d35511]819 return rc;
820
[852b801]821}
822
[0d35511]823static int vfs_truncate_internal(fs_handle_t fs_handle, service_id_t service_id,
824 fs_index_t index, aoff64_t size)
[5bcd5b7]825{
[0d35511]826 async_exch_t *exch = vfs_exchange_grab(fs_handle);
827 sysarg_t rc = async_req_4_0(exch, VFS_OUT_TRUNCATE,
828 (sysarg_t) service_id, (sysarg_t) index, LOWER32(size),
829 UPPER32(size));
830 vfs_exchange_release(exch);
831
832 return (int) rc;
[5bcd5b7]833}
834
[0d35511]835int vfs_op_truncate(int fd, int64_t size)
[852b801]836{
[0d35511]837 vfs_file_t *file = vfs_file_get(fd);
838 if (!file) {
839 return EBADF;
840 }
841
842 fibril_rwlock_write_lock(&file->node->contents_rwlock);
843
844 int rc = vfs_truncate_internal(file->node->fs_handle,
845 file->node->service_id, file->node->index, size);
846 if (rc == EOK) {
847 file->node->size = size;
848 }
849
850 fibril_rwlock_write_unlock(&file->node->contents_rwlock);
851 vfs_file_put(file);
852 return rc;
853}
854
855int vfs_op_unlink2(int parentfd, int expectfd, int wflag, char *path)
856{
857 int rc = EOK;
[20c071d]858 vfs_file_t *parent = NULL;
859 vfs_file_t *expect = NULL;
860
[5126f80]861 if (parentfd == expectfd) {
[0d35511]862 return EINVAL;
[5126f80]863 }
[472c09d]864
[20c071d]865 fibril_rwlock_write_lock(&namespace_rwlock);
866
867 int lflag = (wflag&WALK_DIRECTORY) ? L_DIRECTORY: 0;
[415c7e0d]868
[0d35511]869 /*
870 * Files are retrieved in order of file descriptors, to prevent
871 * deadlock.
872 */
[5126f80]873 if (parentfd < expectfd) {
[20c071d]874 parent = vfs_file_get(parentfd);
875 if (!parent) {
[5126f80]876 rc = EBADF;
[20c071d]877 goto exit;
878 }
879 }
880
881 if (expectfd >= 0) {
882 expect = vfs_file_get(expectfd);
883 if (!expect) {
[0d35511]884 rc = EBADF;
[20c071d]885 goto exit;
886 }
[c577a9a]887 }
888
[5126f80]889 if (parentfd > expectfd) {
[c577a9a]890 parent = vfs_file_get(parentfd);
891 if (!parent) {
[5126f80]892 rc = EBADF;
[c577a9a]893 goto exit;
894 }
895 }
896
[5126f80]897 assert(parent != NULL);
[c577a9a]898
899 if (expectfd >= 0) {
[20c071d]900 vfs_lookup_res_t lr;
[5126f80]901 rc = vfs_lookup_internal(parent->node, path, lflag, &lr);
[20c071d]902 if (rc != EOK) {
903 goto exit;
904 }
905
[a274a5f]906 vfs_node_t *found_node = vfs_node_peek(&lr);
907 if (expect->node != found_node) {
[20c071d]908 rc = ENOENT;
909 goto exit;
910 }
911
912 vfs_file_put(expect);
913 expect = NULL;
914 }
915
[415c7e0d]916 vfs_lookup_res_t lr;
[5126f80]917 rc = vfs_lookup_internal(parent->node, path, lflag | L_UNLINK, &lr);
[415c7e0d]918 if (rc != EOK) {
[20c071d]919 goto exit;
[415c7e0d]920 }
[20c071d]921
[5bcd5b7]922 /* If the node is not held by anyone, try to destroy it. */
923 if (vfs_node_peek(&lr) == NULL) {
924 out_destroy(&lr.triplet);
[415c7e0d]925 }
926
[20c071d]927exit:
928 if (path) {
929 free(path);
930 }
931 if (parent) {
932 vfs_file_put(parent);
933 }
934 if (expect) {
935 vfs_file_put(expect);
936 }
937 fibril_rwlock_write_unlock(&namespace_rwlock);
[0d35511]938 return rc;
[20c071d]939}
[415c7e0d]940
[0d35511]941int vfs_op_unmount(int mpfd)
[a8e9ab8d]942{
[0d35511]943 vfs_file_t *mp = vfs_file_get(mpfd);
944 if (mp == NULL) {
945 return EBADF;
[a8e9ab8d]946 }
[472c09d]947
[0d35511]948 if (mp->node->mount == NULL) {
949 vfs_file_put(mp);
950 return ENOENT;
[a8e9ab8d]951 }
[72bde81]952
[230260ac]953 fibril_rwlock_write_lock(&namespace_rwlock);
[472c09d]954
[0d35511]955 /*
956 * Count the total number of references for the mounted file system. We
957 * are expecting at least one, which is held by the mount point.
958 * If we find more, it means that
959 * the file system cannot be gracefully unmounted at the moment because
960 * someone is working with it.
961 */
962 if (vfs_nodes_refcount_sum_get(mp->node->mount->fs_handle,
963 mp->node->mount->service_id) != 1) {
964 vfs_file_put(mp);
[230260ac]965 fibril_rwlock_write_unlock(&namespace_rwlock);
[0d35511]966 return EBUSY;
[a8e9ab8d]967 }
[472c09d]968
[0d35511]969 async_exch_t *exch = vfs_exchange_grab(mp->node->mount->fs_handle);
970 int rc = async_req_1_0(exch, VFS_OUT_UNMOUNTED,
971 mp->node->mount->service_id);
972 vfs_exchange_release(exch);
[f15cf1a6]973
974 if (rc != EOK) {
[0d35511]975 vfs_file_put(mp);
[230260ac]976 fibril_rwlock_write_unlock(&namespace_rwlock);
[778d26d]977 return rc;
[f15cf1a6]978 }
[472c09d]979
[0d35511]980 vfs_node_forget(mp->node->mount);
981 vfs_node_put(mp->node);
982 mp->node->mount = NULL;
[472c09d]983
[230260ac]984 fibril_rwlock_write_unlock(&namespace_rwlock);
[0d35511]985
986 fibril_mutex_lock(&mtab_list_lock);
987 int found = 0;
988
989 list_foreach(mtab_list, link, mtab_ent_t, mtab_ent) {
990 // FIXME: mp name
991 if (str_cmp(mtab_ent->mp, "fixme") == 0) {
992 list_remove(&mtab_ent->link);
993 mtab_size--;
994 free(mtab_ent);
995 found = 1;
996 break;
997 }
998 }
999 assert(found);
1000 fibril_mutex_unlock(&mtab_list_lock);
1001
1002 vfs_file_put(mp);
[778d26d]1003 return EOK;
[f15cf1a6]1004}
1005
[0d35511]1006int vfs_op_wait_handle(bool high_fd)
[a8e9ab8d]1007{
[0d35511]1008 return vfs_wait_handle_internal(high_fd);
1009}
1010
1011static inline bool walk_flags_valid(int flags)
1012{
[4809715]1013 if ((flags & ~WALK_ALL_FLAGS) != 0) {
[0d35511]1014 return false;
[a8e9ab8d]1015 }
[4809715]1016 if ((flags & WALK_MAY_CREATE) && (flags & WALK_MUST_CREATE)) {
[0d35511]1017 return false;
[a8e9ab8d]1018 }
[4809715]1019 if ((flags & WALK_REGULAR) && (flags & WALK_DIRECTORY)) {
[0d35511]1020 return false;
[a8e9ab8d]1021 }
[4809715]1022 if ((flags & WALK_MAY_CREATE) || (flags & WALK_MUST_CREATE)) {
1023 if (!(flags & WALK_DIRECTORY) && !(flags & WALK_REGULAR)) {
[0d35511]1024 return false;
1025 }
[a8e9ab8d]1026 }
[0d35511]1027 return true;
1028}
[778d26d]1029
[0d35511]1030static inline int walk_lookup_flags(int flags)
1031{
1032 int lflags = 0;
[4809715]1033 if ((flags & WALK_MAY_CREATE) || (flags & WALK_MUST_CREATE)) {
[0d35511]1034 lflags |= L_CREATE;
1035 }
[4809715]1036 if (flags & WALK_MUST_CREATE) {
[0d35511]1037 lflags |= L_EXCLUSIVE;
[a8e9ab8d]1038 }
[4809715]1039 if (flags & WALK_REGULAR) {
[0d35511]1040 lflags |= L_FILE;
[a8e9ab8d]1041 }
[4809715]1042 if (flags & WALK_DIRECTORY) {
[0d35511]1043 lflags |= L_DIRECTORY;
[778d26d]1044 }
[4809715]1045 if (flags & WALK_MOUNT_POINT) {
[0d35511]1046 lflags |= L_MP;
1047 }
1048 return lflags;
[a8e9ab8d]1049}
1050
[0d35511]1051int vfs_op_walk(int parentfd, int flags, char *path, int *out_fd)
[2b88074b]1052{
[0d35511]1053 if (!walk_flags_valid(flags)) {
1054 return EINVAL;
[4fe94c66]1055 }
1056
[0d35511]1057 vfs_file_t *parent = vfs_file_get(parentfd);
1058 if (!parent) {
1059 return EBADF;
[2b88074b]1060 }
1061
[0d35511]1062 fibril_rwlock_read_lock(&namespace_rwlock);
[2b88074b]1063
[0d35511]1064 vfs_lookup_res_t lr;
1065 int rc = vfs_lookup_internal(parent->node, path,
1066 walk_lookup_flags(flags), &lr);
[76a67ce]1067
[0d35511]1068 if (rc != EOK) {
1069 fibril_rwlock_read_unlock(&namespace_rwlock);
1070 vfs_file_put(parent);
1071 return rc;
[76a67ce]1072 }
[9dc6083]1073
[0d35511]1074 vfs_node_t *node = vfs_node_get(&lr);
[9dc6083]1075
[0d35511]1076 vfs_file_t *file;
1077 int fd = vfs_fd_alloc(&file, false);
1078 if (fd < 0) {
1079 vfs_node_put(node);
1080 vfs_file_put(parent);
1081 return fd;
1082 }
1083 assert(file != NULL);
[9dc6083]1084
[0d35511]1085 file->node = node;
1086 file->permissions = parent->permissions;
1087 file->open_read = false;
1088 file->open_write = false;
[9dc6083]1089
[a737667e]1090 vfs_file_put(file);
[0d35511]1091 vfs_file_put(parent);
1092
1093 fibril_rwlock_read_unlock(&namespace_rwlock);
[9dc6083]1094
[0d35511]1095 *out_fd = fd;
1096 return EOK;
[66366470]1097}
1098
[0d35511]1099int vfs_op_write(int fd, size_t *out_bytes)
[354b642]1100{
[0d35511]1101 return vfs_rdwr(fd, false, rdwr_ipc_client, out_bytes);
[354b642]1102}
1103
[861e7d1]1104/**
1105 * @}
[05b9912]1106 */
Note: See TracBrowser for help on using the repository browser.