source: mainline/uspace/lib/fs/libfs.c@ 2166728

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

Remove superfluous assert

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