source: mainline/uspace/lib/fs/libfs.c@ 09ab0a9a

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 09ab0a9a was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

  • Property mode set to 100644
File size: 20.2 KB
RevLine 
[74303b6]1/*
[1313ee9]2 * Copyright (c) 2009 Jakub Jermar
[74303b6]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
[1313ee9]29/** @addtogroup libfs
[74303b6]30 * @{
[1313ee9]31 */
[74303b6]32/**
33 * @file
[1313ee9]34 * Glue code which is common to all FS implementations.
[74303b6]35 */
36
[1313ee9]37#include "libfs.h"
[ed903174]38#include <macros.h>
[efd4a72]39#include <errno.h>
40#include <async.h>
41#include <as.h>
[2c448fb]42#include <assert.h>
43#include <dirent.h>
[595edf5]44#include <mem.h>
[5a2b765]45#include <str.h>
[efcebe1]46#include <stdlib.h>
[b8dbe2f]47#include <fibril_synch.h>
[5a2b765]48#include <ipc/vfs.h>
[b5b5d84]49#include <vfs/vfs.h>
[efd4a72]50
[0daba212]51#define on_error(rc, action) \
52 do { \
53 if ((rc) != EOK) \
54 action; \
55 } while (0)
56
57#define combine_rc(rc1, rc2) \
58 ((rc1) == EOK ? (rc2) : (rc1))
59
[984a9ba]60#define answer_and_return(call, rc) \
[0daba212]61 do { \
[984a9ba]62 async_answer_0((call), (rc)); \
[0daba212]63 return; \
64 } while (0)
65
[efcebe1]66static fs_reg_t reg;
67
68static vfs_out_ops_t *vfs_out_ops = NULL;
69static libfs_ops_t *libfs_ops = NULL;
70
[5a2b765]71static char fs_name[FS_NAME_MAXLEN + 1];
72
[984a9ba]73static void libfs_link(libfs_ops_t *, fs_handle_t, ipc_call_t *);
74static void libfs_lookup(libfs_ops_t *, fs_handle_t, ipc_call_t *);
75static void libfs_stat(libfs_ops_t *, fs_handle_t, ipc_call_t *);
76static void libfs_open_node(libfs_ops_t *, fs_handle_t, ipc_call_t *);
77static void libfs_statfs(libfs_ops_t *, fs_handle_t, ipc_call_t *);
78
79static void vfs_out_fsprobe(ipc_call_t *req)
[d2c8533]80{
81 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*req);
[b7fd2a0]82 errno_t rc;
[d2c8533]83 vfs_fs_probe_info_t info;
[a35b458]84
[984a9ba]85 ipc_call_t call;
[d2c8533]86 size_t size;
[984a9ba]87 if ((!async_data_read_receive(&call, &size)) ||
[d2c8533]88 (size != sizeof(info))) {
[984a9ba]89 async_answer_0(&call, EIO);
90 async_answer_0(req, EIO);
[d2c8533]91 return;
92 }
[a35b458]93
[d2c8533]94 memset(&info, 0, sizeof(info));
95 rc = vfs_out_ops->fsprobe(service_id, &info);
96 if (rc != EOK) {
[984a9ba]97 async_answer_0(&call, EIO);
98 async_answer_0(req, rc);
[d2c8533]99 return;
100 }
[a35b458]101
[984a9ba]102 async_data_read_finalize(&call, &info, sizeof(info));
103 async_answer_0(req, EOK);
[d2c8533]104}
105
[984a9ba]106static void vfs_out_mounted(ipc_call_t *req)
[efcebe1]107{
[86ffa27f]108 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*req);
[efcebe1]109 char *opts;
[b7fd2a0]110 errno_t rc;
[a35b458]111
[efcebe1]112 /* Accept the mount options. */
113 rc = async_data_write_accept((void **) &opts, true, 0, 0, 0, NULL);
114 if (rc != EOK) {
[984a9ba]115 async_answer_0(req, rc);
[efcebe1]116 return;
117 }
118
119 fs_index_t index;
120 aoff64_t size;
[4f30222]121 rc = vfs_out_ops->mounted(service_id, opts, &index, &size);
[efcebe1]122
[77f0a1d]123 if (rc == EOK) {
[984a9ba]124 async_answer_3(req, EOK, index, LOWER32(size),
[77f0a1d]125 UPPER32(size));
126 } else
[984a9ba]127 async_answer_0(req, rc);
[efcebe1]128
129 free(opts);
130}
131
[984a9ba]132static void vfs_out_unmounted(ipc_call_t *req)
[efcebe1]133{
[86ffa27f]134 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*req);
[1b20da0]135 errno_t rc;
[efcebe1]136
[86ffa27f]137 rc = vfs_out_ops->unmounted(service_id);
[efcebe1]138
[984a9ba]139 async_answer_0(req, rc);
[efcebe1]140}
141
[984a9ba]142static void vfs_out_link(ipc_call_t *req)
[efcebe1]143{
[984a9ba]144 libfs_link(libfs_ops, reg.fs_handle, req);
[efcebe1]145}
146
[984a9ba]147static void vfs_out_lookup(ipc_call_t *req)
[efcebe1]148{
[984a9ba]149 libfs_lookup(libfs_ops, reg.fs_handle, req);
[efcebe1]150}
151
[984a9ba]152static void vfs_out_read(ipc_call_t *req)
[efcebe1]153{
[86ffa27f]154 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*req);
[efcebe1]155 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*req);
156 aoff64_t pos = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*req),
157 IPC_GET_ARG4(*req));
158 size_t rbytes;
[b7fd2a0]159 errno_t rc;
[efcebe1]160
[86ffa27f]161 rc = vfs_out_ops->read(service_id, index, pos, &rbytes);
[efcebe1]162
163 if (rc == EOK)
[984a9ba]164 async_answer_1(req, EOK, rbytes);
[efcebe1]165 else
[984a9ba]166 async_answer_0(req, rc);
[efcebe1]167}
168
[984a9ba]169static void vfs_out_write(ipc_call_t *req)
[efcebe1]170{
[86ffa27f]171 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*req);
[efcebe1]172 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*req);
173 aoff64_t pos = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*req),
174 IPC_GET_ARG4(*req));
175 size_t wbytes;
176 aoff64_t nsize;
[b7fd2a0]177 errno_t rc;
[efcebe1]178
[86ffa27f]179 rc = vfs_out_ops->write(service_id, index, pos, &wbytes, &nsize);
[efcebe1]180
[61042de]181 if (rc == EOK) {
[984a9ba]182 async_answer_3(req, EOK, wbytes, LOWER32(nsize),
[61042de]183 UPPER32(nsize));
184 } else
[984a9ba]185 async_answer_0(req, rc);
[efcebe1]186}
187
[984a9ba]188static void vfs_out_truncate(ipc_call_t *req)
[efcebe1]189{
[86ffa27f]190 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*req);
[efcebe1]191 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*req);
192 aoff64_t size = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*req),
193 IPC_GET_ARG4(*req));
[b7fd2a0]194 errno_t rc;
[efcebe1]195
[86ffa27f]196 rc = vfs_out_ops->truncate(service_id, index, size);
[efcebe1]197
[984a9ba]198 async_answer_0(req, rc);
[efcebe1]199}
200
[984a9ba]201static void vfs_out_close(ipc_call_t *req)
[efcebe1]202{
[86ffa27f]203 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*req);
[efcebe1]204 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*req);
[b7fd2a0]205 errno_t rc;
[efcebe1]206
[86ffa27f]207 rc = vfs_out_ops->close(service_id, index);
[efcebe1]208
[984a9ba]209 async_answer_0(req, rc);
[efcebe1]210}
211
[984a9ba]212static void vfs_out_destroy(ipc_call_t *req)
[efcebe1]213{
[86ffa27f]214 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*req);
[efcebe1]215 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*req);
216
[b7fd2a0]217 errno_t rc;
[b7c62a9]218 fs_node_t *node = NULL;
219 rc = libfs_ops->node_get(&node, service_id, index);
220 if (rc == EOK && node != NULL) {
221 bool destroy = (libfs_ops->lnkcnt_get(node) == 0);
222 libfs_ops->node_put(node);
[61042de]223 if (destroy)
[b7c62a9]224 rc = vfs_out_ops->destroy(service_id, index);
225 }
[984a9ba]226 async_answer_0(req, rc);
[efcebe1]227}
228
[984a9ba]229static void vfs_out_open_node(ipc_call_t *req)
[efcebe1]230{
[984a9ba]231 libfs_open_node(libfs_ops, reg.fs_handle, req);
[efcebe1]232}
233
[984a9ba]234static void vfs_out_stat(ipc_call_t *req)
[efcebe1]235{
[984a9ba]236 libfs_stat(libfs_ops, reg.fs_handle, req);
[efcebe1]237}
238
[984a9ba]239static void vfs_out_sync(ipc_call_t *req)
[efcebe1]240{
[86ffa27f]241 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*req);
[efcebe1]242 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*req);
[b7fd2a0]243 errno_t rc;
[efcebe1]244
[86ffa27f]245 rc = vfs_out_ops->sync(service_id, index);
[efcebe1]246
[984a9ba]247 async_answer_0(req, rc);
[efcebe1]248}
249
[984a9ba]250static void vfs_out_statfs(ipc_call_t *req)
[5930e3f]251{
[984a9ba]252 libfs_statfs(libfs_ops, reg.fs_handle, req);
[9dc6083]253}
[1dff985]254
[984a9ba]255static void vfs_out_is_empty(ipc_call_t *req)
[5bcd5b7]256{
257 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*req);
258 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*req);
[b7fd2a0]259 errno_t rc;
[5bcd5b7]260
261 fs_node_t *node = NULL;
262 rc = libfs_ops->node_get(&node, service_id, index);
[61042de]263 if (rc != EOK)
[984a9ba]264 async_answer_0(req, rc);
[61042de]265 if (node == NULL)
[984a9ba]266 async_answer_0(req, EINVAL);
[a35b458]267
[5bcd5b7]268 bool children = false;
269 rc = libfs_ops->has_children(&children, node);
270 libfs_ops->node_put(node);
[a35b458]271
[61042de]272 if (rc != EOK)
[984a9ba]273 async_answer_0(req, rc);
274 async_answer_0(req, children ? ENOTEMPTY : EOK);
[5bcd5b7]275}
276
[984a9ba]277static void vfs_connection(ipc_call_t *icall, void *arg)
[efcebe1]278{
[984a9ba]279 if (icall->cap_handle) {
[efcebe1]280 /*
281 * This only happens for connections opened by
282 * IPC_M_CONNECT_ME_TO calls as opposed to callback connections
283 * created by IPC_M_CONNECT_TO_ME.
284 */
[984a9ba]285 async_answer_0(icall, EOK);
[efcebe1]286 }
[a35b458]287
[efcebe1]288 while (true) {
289 ipc_call_t call;
[984a9ba]290 async_get_call(&call);
[a35b458]291
[efcebe1]292 if (!IPC_GET_IMETHOD(call))
293 return;
[a35b458]294
[efcebe1]295 switch (IPC_GET_IMETHOD(call)) {
[d2c8533]296 case VFS_OUT_FSPROBE:
[984a9ba]297 vfs_out_fsprobe(&call);
[d2c8533]298 break;
[efcebe1]299 case VFS_OUT_MOUNTED:
[984a9ba]300 vfs_out_mounted(&call);
[efcebe1]301 break;
302 case VFS_OUT_UNMOUNTED:
[984a9ba]303 vfs_out_unmounted(&call);
[efcebe1]304 break;
[bf9dc4e2]305 case VFS_OUT_LINK:
[984a9ba]306 vfs_out_link(&call);
[efcebe1]307 break;
308 case VFS_OUT_LOOKUP:
[984a9ba]309 vfs_out_lookup(&call);
[efcebe1]310 break;
311 case VFS_OUT_READ:
[984a9ba]312 vfs_out_read(&call);
[efcebe1]313 break;
314 case VFS_OUT_WRITE:
[984a9ba]315 vfs_out_write(&call);
[efcebe1]316 break;
317 case VFS_OUT_TRUNCATE:
[984a9ba]318 vfs_out_truncate(&call);
[efcebe1]319 break;
320 case VFS_OUT_CLOSE:
[984a9ba]321 vfs_out_close(&call);
[efcebe1]322 break;
323 case VFS_OUT_DESTROY:
[984a9ba]324 vfs_out_destroy(&call);
[efcebe1]325 break;
326 case VFS_OUT_OPEN_NODE:
[984a9ba]327 vfs_out_open_node(&call);
[efcebe1]328 break;
329 case VFS_OUT_STAT:
[984a9ba]330 vfs_out_stat(&call);
[efcebe1]331 break;
332 case VFS_OUT_SYNC:
[984a9ba]333 vfs_out_sync(&call);
[efcebe1]334 break;
[9dc6083]335 case VFS_OUT_STATFS:
[984a9ba]336 vfs_out_statfs(&call);
[9dc6083]337 break;
[5bcd5b7]338 case VFS_OUT_IS_EMPTY:
[984a9ba]339 vfs_out_is_empty(&call);
[5bcd5b7]340 break;
[efcebe1]341 default:
[984a9ba]342 async_answer_0(&call, ENOTSUP);
[efcebe1]343 break;
344 }
345 }
346}
347
[efd4a72]348/** Register file system server.
349 *
350 * This function abstracts away the tedious registration protocol from
351 * file system implementations and lets them to reuse this registration glue
352 * code.
353 *
[79ae36dd]354 * @param sess Session for communication with VFS.
355 * @param info VFS info structure supplied by the file system
356 * implementation.
[efcebe1]357 * @param vops Address of the vfs_out_ops_t structure.
358 * @param lops Address of the libfs_ops_t structure.
[1313ee9]359 *
360 * @return EOK on success or a non-zero error code on errror.
[efd4a72]361 *
362 */
[b7fd2a0]363errno_t fs_register(async_sess_t *sess, vfs_info_t *info, vfs_out_ops_t *vops,
[efcebe1]364 libfs_ops_t *lops)
[efd4a72]365{
366 /*
367 * Tell VFS that we are here and want to get registered.
368 * We use the async framework because VFS will answer the request
369 * out-of-order, when it knows that the operation succeeded or failed.
370 */
[a35b458]371
[79ae36dd]372 async_exch_t *exch = async_exchange_begin(sess);
[a35b458]373
[efd4a72]374 ipc_call_t answer;
[79ae36dd]375 aid_t req = async_send_0(exch, VFS_IN_REGISTER, &answer);
[a35b458]376
[efd4a72]377 /*
378 * Send our VFS info structure to VFS.
379 */
[b7fd2a0]380 errno_t rc = async_data_write_start(exch, info, sizeof(*info));
[a35b458]381
[efd4a72]382 if (rc != EOK) {
[79ae36dd]383 async_exchange_end(exch);
[50b581d]384 async_forget(req);
[efd4a72]385 return rc;
386 }
[a35b458]387
[efcebe1]388 /*
389 * Set VFS_OUT and libfs operations.
390 */
391 vfs_out_ops = vops;
392 libfs_ops = lops;
393
[5a2b765]394 str_cpy(fs_name, sizeof(fs_name), info->name);
395
[efd4a72]396 /*
397 * Ask VFS for callback connection.
398 */
[f9b2cb4c]399 port_id_t port;
400 rc = async_create_callback_port(exch, INTERFACE_VFS_DRIVER_CB, 0, 0,
401 vfs_connection, NULL, &port);
[a35b458]402
[efd4a72]403 /*
[fbcdeb8]404 * Request sharing the Path Lookup Buffer with VFS.
[efd4a72]405 */
[fbcdeb8]406 rc = async_share_in_start_0_0(exch, PLB_SIZE, (void *) &reg.plb_ro);
[faba839]407 if (reg.plb_ro == AS_MAP_FAILED) {
[79ae36dd]408 async_exchange_end(exch);
[50b581d]409 async_forget(req);
[efd4a72]410 return ENOMEM;
411 }
[a35b458]412
[79ae36dd]413 async_exchange_end(exch);
[a35b458]414
[efd4a72]415 if (rc) {
[50b581d]416 async_forget(req);
[efd4a72]417 return rc;
418 }
[a35b458]419
[efd4a72]420 /*
[4198f9c3]421 * Pick up the answer for the request to the VFS_IN_REQUEST call.
[efd4a72]422 */
423 async_wait_for(req, NULL);
[efcebe1]424 reg.fs_handle = (int) IPC_GET_ARG1(answer);
[a35b458]425
[efd4a72]426 return IPC_GET_RETVAL(answer);
427}
[74303b6]428
[83937ccd]429void fs_node_initialize(fs_node_t *fn)
430{
431 memset(fn, 0, sizeof(fs_node_t));
432}
433
[efcebe1]434static char plb_get_char(unsigned pos)
[83937ccd]435{
[efcebe1]436 return reg.plb_ro[pos % PLB_SIZE];
437}
438
[b7fd2a0]439static errno_t plb_get_component(char *dest, unsigned *sz, unsigned *ppos,
[61042de]440 unsigned last)
[bf9dc4e2]441{
442 unsigned pos = *ppos;
443 unsigned size = 0;
[a35b458]444
[bf9dc4e2]445 if (pos == last) {
446 *sz = 0;
447 return ERANGE;
[83937ccd]448 }
[bf9dc4e2]449
[1b20da0]450 char c = plb_get_char(pos);
[61042de]451 if (c == '/')
[bf9dc4e2]452 pos++;
[a35b458]453
[bf9dc4e2]454 for (int i = 0; i <= NAME_MAX; i++) {
455 c = plb_get_char(pos);
456 if (pos == last || c == '/') {
457 dest[i] = 0;
458 *ppos = pos;
459 *sz = size;
460 return EOK;
461 }
462 dest[i] = c;
463 pos++;
464 size++;
[83937ccd]465 }
[bf9dc4e2]466 return ENAMETOOLONG;
467}
468
[b7fd2a0]469static errno_t receive_fname(char *buffer)
[bf9dc4e2]470{
[984a9ba]471 ipc_call_t call;
[bf9dc4e2]472 size_t size;
[a35b458]473
[984a9ba]474 if (!async_data_write_receive(&call, &size))
[bf9dc4e2]475 return ENOENT;
[984a9ba]476
[bf9dc4e2]477 if (size > NAME_MAX + 1) {
[984a9ba]478 async_answer_0(&call, ERANGE);
[bf9dc4e2]479 return ERANGE;
[83937ccd]480 }
[984a9ba]481
482 return async_data_write_finalize(&call, buffer, size);
[83937ccd]483}
484
[bf9dc4e2]485/** Link a file at a path.
[984a9ba]486 *
[bf9dc4e2]487 */
[984a9ba]488void libfs_link(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_call_t *req)
[3c11713]489{
[bf9dc4e2]490 service_id_t parent_sid = IPC_GET_ARG1(*req);
491 fs_index_t parent_index = IPC_GET_ARG2(*req);
492 fs_index_t child_index = IPC_GET_ARG3(*req);
[a35b458]493
[bf9dc4e2]494 char component[NAME_MAX + 1];
[b7fd2a0]495 errno_t rc = receive_fname(component);
[bf9dc4e2]496 if (rc != EOK) {
[984a9ba]497 async_answer_0(req, rc);
[c888102]498 return;
499 }
500
[bf9dc4e2]501 fs_node_t *parent = NULL;
502 rc = ops->node_get(&parent, parent_sid, parent_index);
503 if (parent == NULL) {
[984a9ba]504 async_answer_0(req, rc == EOK ? EBADF : rc);
[c888102]505 return;
506 }
[a35b458]507
[bf9dc4e2]508 fs_node_t *child = NULL;
509 rc = ops->node_get(&child, parent_sid, child_index);
510 if (child == NULL) {
[984a9ba]511 async_answer_0(req, rc == EOK ? EBADF : rc);
[bf9dc4e2]512 ops->node_put(parent);
513 return;
[c888102]514 }
[a35b458]515
[bf9dc4e2]516 rc = ops->link(parent, child, component);
517 ops->node_put(parent);
518 ops->node_put(child);
[984a9ba]519 async_answer_0(req, rc);
[efcebe1]520}
521
[1e50f81]522/** Lookup VFS triplet by name in the file system name space.
[9bb85f3]523 *
524 * The path passed in the PLB must be in the canonical file system path format
525 * as returned by the canonify() function.
[1e50f81]526 *
[984a9ba]527 * @param ops libfs operations structure with function pointers to
528 * file system implementation
529 * @param fs_handle File system handle of the file system where to perform
530 * the lookup.
531 * @param req VFS_OUT_LOOKUP request data itself.
[595edf5]532 *
[1e50f81]533 */
[984a9ba]534void libfs_lookup(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_call_t *req)
[2c448fb]535{
[bf9dc4e2]536 unsigned first = IPC_GET_ARG1(*req);
537 unsigned len = IPC_GET_ARG2(*req);
[86ffa27f]538 service_id_t service_id = IPC_GET_ARG3(*req);
[bf9dc4e2]539 fs_index_t index = IPC_GET_ARG4(*req);
540 int lflag = IPC_GET_ARG5(*req);
[a35b458]541
[bf9dc4e2]542 // TODO: Validate flags.
[a35b458]543
[bf9dc4e2]544 unsigned next = first;
545 unsigned last = first + len;
[a35b458]546
[c9f6e49f]547 char component[NAME_MAX + 1];
[b7fd2a0]548 errno_t rc;
[a35b458]549
[b6035ba]550 fs_node_t *par = NULL;
[0daba212]551 fs_node_t *cur = NULL;
[b6035ba]552 fs_node_t *tmp = NULL;
[bf9dc4e2]553 unsigned clen = 0;
[a35b458]554
[06256b0]555 rc = ops->node_get(&cur, service_id, index);
[bf9dc4e2]556 if (rc != EOK) {
[984a9ba]557 async_answer_0(req, rc);
[bf9dc4e2]558 goto out;
[83937ccd]559 }
[a35b458]560
[bf9dc4e2]561 assert(cur != NULL);
[a35b458]562
[bf9dc4e2]563 /* Find the file and its parent. */
[a35b458]564
[677745a]565 unsigned last_next = 0;
[a35b458]566
[bf9dc4e2]567 while (next != last) {
568 if (cur == NULL) {
[677745a]569 assert(par != NULL);
570 goto out1;
[bf9dc4e2]571 }
[677745a]572
[bf9dc4e2]573 if (!ops->is_directory(cur)) {
[984a9ba]574 async_answer_0(req, ENOTDIR);
[bf9dc4e2]575 goto out;
576 }
[a35b458]577
[677745a]578 last_next = next;
[1313ee9]579 /* Collect the component */
[bf9dc4e2]580 rc = plb_get_component(component, &clen, &next, last);
581 assert(rc != ERANGE);
582 if (rc != EOK) {
[984a9ba]583 async_answer_0(req, rc);
[bf9dc4e2]584 goto out;
[2c448fb]585 }
[a35b458]586
[bf9dc4e2]587 if (clen == 0) {
588 /* The path is just "/". */
589 break;
[2c448fb]590 }
[a35b458]591
[bf9dc4e2]592 assert(component[clen] == 0);
[a35b458]593
[1313ee9]594 /* Match the component */
[0daba212]595 rc = ops->match(&tmp, cur, component);
[bf9dc4e2]596 if (rc != EOK) {
[984a9ba]597 async_answer_0(req, rc);
[06901c6b]598 goto out;
[2c448fb]599 }
[a35b458]600
[bf9dc4e2]601 /* Descend one level */
[0daba212]602 if (par) {
603 rc = ops->node_put(par);
[bf9dc4e2]604 if (rc != EOK) {
[984a9ba]605 async_answer_0(req, rc);
[bf9dc4e2]606 goto out;
607 }
[0daba212]608 }
[a35b458]609
[7b6d98b]610 par = cur;
[2c448fb]611 cur = tmp;
[06901c6b]612 tmp = NULL;
[2c448fb]613 }
[a35b458]614
[7c3fb9b]615 /*
616 * At this point, par is either NULL or a directory.
[bf9dc4e2]617 * If cur is NULL, the looked up file does not exist yet.
618 */
[a35b458]619
[bf9dc4e2]620 assert(par == NULL || ops->is_directory(par));
621 assert(par != NULL || cur != NULL);
[a35b458]622
[bf9dc4e2]623 /* Check for some error conditions. */
[a35b458]624
[bf9dc4e2]625 if (cur && (lflag & L_FILE) && (ops->is_directory(cur))) {
[984a9ba]626 async_answer_0(req, EISDIR);
[bf9dc4e2]627 goto out;
628 }
[a35b458]629
[bf9dc4e2]630 if (cur && (lflag & L_DIRECTORY) && (ops->is_file(cur))) {
[984a9ba]631 async_answer_0(req, ENOTDIR);
[06901c6b]632 goto out;
[2c448fb]633 }
[a35b458]634
[bf9dc4e2]635 /* Unlink. */
[a35b458]636
[a8e9ab8d]637 if (lflag & L_UNLINK) {
[bf9dc4e2]638 if (!cur) {
[984a9ba]639 async_answer_0(req, ENOENT);
[bf9dc4e2]640 goto out;
641 }
642 if (!par) {
[984a9ba]643 async_answer_0(req, EINVAL);
[bf9dc4e2]644 goto out;
645 }
[a35b458]646
[0daba212]647 rc = ops->unlink(par, cur, component);
[ed903174]648 if (rc == EOK) {
[51774cd]649 aoff64_t size = ops->size_get(cur);
[984a9ba]650 async_answer_5(req, EOK, fs_handle,
[51774cd]651 ops->index_get(cur),
652 (ops->is_directory(cur) << 16) | last,
653 LOWER32(size), UPPER32(size));
[bf9dc4e2]654 } else {
[984a9ba]655 async_answer_0(req, rc);
[bf9dc4e2]656 }
[06901c6b]657 goto out;
[2c448fb]658 }
[a35b458]659
[bf9dc4e2]660 /* Create. */
[a35b458]661
[bf9dc4e2]662 if (lflag & L_CREATE) {
663 if (cur && (lflag & L_EXCLUSIVE)) {
[984a9ba]664 async_answer_0(req, EEXIST);
[bf9dc4e2]665 goto out;
666 }
[a35b458]667
[bf9dc4e2]668 if (!cur) {
[61042de]669 rc = ops->create(&cur, service_id,
670 lflag & (L_FILE | L_DIRECTORY));
[bf9dc4e2]671 if (rc != EOK) {
[984a9ba]672 async_answer_0(req, rc);
[bf9dc4e2]673 goto out;
674 }
675 if (!cur) {
[984a9ba]676 async_answer_0(req, ENOSPC);
[bf9dc4e2]677 goto out;
678 }
[a35b458]679
[bf9dc4e2]680 rc = ops->link(par, cur, component);
681 if (rc != EOK) {
682 (void) ops->destroy(cur);
683 cur = NULL;
[984a9ba]684 async_answer_0(req, rc);
[bf9dc4e2]685 goto out;
686 }
687 }
[2c448fb]688 }
[a35b458]689
[bf9dc4e2]690 /* Return. */
[677745a]691out1:
[bf9dc4e2]692 if (!cur) {
[984a9ba]693 async_answer_5(req, EOK, fs_handle, ops->index_get(par),
[dde4689]694 (ops->is_directory(par) << 16) | last_next,
695 LOWER32(ops->size_get(par)), UPPER32(ops->size_get(par)));
[06901c6b]696 goto out;
[2c448fb]697 }
[a35b458]698
[984a9ba]699 async_answer_5(req, EOK, fs_handle, ops->index_get(cur),
[dde4689]700 (ops->is_directory(cur) << 16) | last, LOWER32(ops->size_get(cur)),
701 UPPER32(ops->size_get(cur)));
[a35b458]702
[06901c6b]703out:
[61042de]704 if (par)
[0daba212]705 (void) ops->node_put(par);
[a35b458]706
[61042de]707 if (cur)
[0daba212]708 (void) ops->node_put(cur);
[a35b458]709
[61042de]710 if (tmp)
[0daba212]711 (void) ops->node_put(tmp);
[2c448fb]712}
713
[984a9ba]714void libfs_stat(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_call_t *req)
[75160a6]715{
[984a9ba]716 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*req);
717 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*req);
[5ed8b72]718
[0daba212]719 fs_node_t *fn;
[b7fd2a0]720 errno_t rc = ops->node_get(&fn, service_id, index);
[984a9ba]721 on_error(rc, answer_and_return(req, rc));
[5ed8b72]722
[984a9ba]723 ipc_call_t call;
[75160a6]724 size_t size;
[984a9ba]725 if ((!async_data_read_receive(&call, &size)) ||
[39330200]726 (size != sizeof(vfs_stat_t))) {
[1313ee9]727 ops->node_put(fn);
[984a9ba]728 async_answer_0(&call, EINVAL);
729 async_answer_0(req, EINVAL);
[75160a6]730 return;
731 }
[5ed8b72]732
[39330200]733 vfs_stat_t stat;
734 memset(&stat, 0, sizeof(vfs_stat_t));
[5ed8b72]735
[0143f72]736 stat.fs_handle = fs_handle;
[15f3c3f]737 stat.service_id = service_id;
[0143f72]738 stat.index = index;
[1313ee9]739 stat.lnkcnt = ops->lnkcnt_get(fn);
[0143f72]740 stat.is_file = ops->is_file(fn);
[1313ee9]741 stat.is_directory = ops->is_directory(fn);
[0143f72]742 stat.size = ops->size_get(fn);
[b33870b]743 stat.service = ops->service_get(fn);
[5ed8b72]744
[1313ee9]745 ops->node_put(fn);
[3dd148d]746
[984a9ba]747 async_data_read_finalize(&call, &stat, sizeof(vfs_stat_t));
748 async_answer_0(req, EOK);
[75160a6]749}
750
[984a9ba]751void libfs_statfs(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_call_t *req)
[5930e3f]752{
[984a9ba]753 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*req);
754 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*req);
[5ed8b72]755
[5930e3f]756 fs_node_t *fn;
[b7fd2a0]757 errno_t rc = ops->node_get(&fn, service_id, index);
[984a9ba]758 on_error(rc, answer_and_return(req, rc));
[5ed8b72]759
[984a9ba]760 ipc_call_t call;
[5930e3f]761 size_t size;
[984a9ba]762 if ((!async_data_read_receive(&call, &size)) ||
[39330200]763 (size != sizeof(vfs_statfs_t))) {
[5ed8b72]764 goto error;
[5930e3f]765 }
766
[39330200]767 vfs_statfs_t st;
768 memset(&st, 0, sizeof(vfs_statfs_t));
[5ed8b72]769
[5a2b765]770 str_cpy(st.fs_name, sizeof(st.fs_name), fs_name);
771
[5ed8b72]772 if (ops->size_block != NULL) {
773 rc = ops->size_block(service_id, &st.f_bsize);
774 if (rc != EOK)
775 goto error;
[3dd148d]776 }
777
[5ed8b72]778 if (ops->total_block_count != NULL) {
779 rc = ops->total_block_count(service_id, &st.f_blocks);
780 if (rc != EOK)
781 goto error;
[3dd148d]782 }
783
[5ed8b72]784 if (ops->free_block_count != NULL) {
785 rc = ops->free_block_count(service_id, &st.f_bfree);
786 if (rc != EOK)
787 goto error;
[3dd148d]788 }
789
[5930e3f]790 ops->node_put(fn);
[984a9ba]791 async_data_read_finalize(&call, &st, sizeof(vfs_statfs_t));
792 async_answer_0(req, EOK);
[3dd148d]793 return;
794
795error:
796 ops->node_put(fn);
[984a9ba]797 async_answer_0(&call, EINVAL);
798 async_answer_0(req, EINVAL);
[5930e3f]799}
800
[595edf5]801/** Open VFS triplet.
802 *
[984a9ba]803 * @param ops libfs operations structure with function pointers to
804 * file system implementation
805 * @param req VFS_OUT_OPEN_NODE request data itself.
[595edf5]806 *
807 */
[984a9ba]808void libfs_open_node(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_call_t *req)
[595edf5]809{
[984a9ba]810 service_id_t service_id = IPC_GET_ARG1(*req);
811 fs_index_t index = IPC_GET_ARG2(*req);
[a35b458]812
[ed903174]813 fs_node_t *fn;
[b7fd2a0]814 errno_t rc = ops->node_get(&fn, service_id, index);
[984a9ba]815 on_error(rc, answer_and_return(req, rc));
[a35b458]816
[0daba212]817 if (fn == NULL) {
[984a9ba]818 async_answer_0(req, ENOENT);
[595edf5]819 return;
820 }
[a35b458]821
[1313ee9]822 rc = ops->node_open(fn);
[ed903174]823 aoff64_t size = ops->size_get(fn);
[984a9ba]824 async_answer_4(req, rc, LOWER32(size), UPPER32(size),
[efcebe1]825 ops->lnkcnt_get(fn),
[61042de]826 (ops->is_file(fn) ? L_FILE : 0) |
827 (ops->is_directory(fn) ? L_DIRECTORY : 0));
[a35b458]828
[0daba212]829 (void) ops->node_put(fn);
[595edf5]830}
831
[5bf76c1]832static FIBRIL_MUTEX_INITIALIZE(instances_mutex);
833static LIST_INITIALIZE(instances_list);
834
835typedef struct {
836 service_id_t service_id;
837 link_t link;
838 void *data;
839} fs_instance_t;
840
[b7fd2a0]841errno_t fs_instance_create(service_id_t service_id, void *data)
[5bf76c1]842{
843 fs_instance_t *inst = malloc(sizeof(fs_instance_t));
844 if (!inst)
845 return ENOMEM;
846
847 link_initialize(&inst->link);
848 inst->service_id = service_id;
849 inst->data = data;
850
851 fibril_mutex_lock(&instances_mutex);
[feeac0d]852 list_foreach(instances_list, link, fs_instance_t, cur) {
[5bf76c1]853 if (cur->service_id == service_id) {
854 fibril_mutex_unlock(&instances_mutex);
855 free(inst);
856 return EEXIST;
857 }
858
859 /* keep the list sorted */
860 if (cur->service_id < service_id) {
861 list_insert_before(&inst->link, &cur->link);
862 fibril_mutex_unlock(&instances_mutex);
863 return EOK;
864 }
865 }
866 list_append(&inst->link, &instances_list);
867 fibril_mutex_unlock(&instances_mutex);
868
869 return EOK;
870}
871
[b7fd2a0]872errno_t fs_instance_get(service_id_t service_id, void **idp)
[5bf76c1]873{
874 fibril_mutex_lock(&instances_mutex);
[984a9ba]875
[feeac0d]876 list_foreach(instances_list, link, fs_instance_t, inst) {
[5bf76c1]877 if (inst->service_id == service_id) {
878 *idp = inst->data;
879 fibril_mutex_unlock(&instances_mutex);
880 return EOK;
881 }
882 }
[984a9ba]883
[5bf76c1]884 fibril_mutex_unlock(&instances_mutex);
885 return ENOENT;
886}
887
[b7fd2a0]888errno_t fs_instance_destroy(service_id_t service_id)
[5bf76c1]889{
890 fibril_mutex_lock(&instances_mutex);
[984a9ba]891
[feeac0d]892 list_foreach(instances_list, link, fs_instance_t, inst) {
[5bf76c1]893 if (inst->service_id == service_id) {
894 list_remove(&inst->link);
895 fibril_mutex_unlock(&instances_mutex);
896 free(inst);
897 return EOK;
898 }
899 }
[984a9ba]900
[5bf76c1]901 fibril_mutex_unlock(&instances_mutex);
902 return ENOENT;
903}
904
[74303b6]905/** @}
906 */
Note: See TracBrowser for help on using the repository browser.