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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b272c67a was f9b2cb4c, checked in by Martin Decky <martin@…>, 10 years ago

unify interface API

  • introduce new interfaces
  • unify location service clients to always expect service ID as the second argument
  • remove obsolete methods that take explicit exchange management arguments (first phase)
  • use interfaces in device drivers, devman, location service, logger, inet
  • Property mode set to 100644
File size: 23.9 KB
Line 
1/*
2 * Copyright (c) 2009 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 libfs
30 * @{
31 */
32/**
33 * @file
34 * Glue code which is common to all FS implementations.
35 */
36
37#include "libfs.h"
38#include "../../srv/vfs/vfs.h"
39#include <macros.h>
40#include <errno.h>
41#include <async.h>
42#include <as.h>
43#include <assert.h>
44#include <dirent.h>
45#include <mem.h>
46#include <sys/stat.h>
47#include <sys/statfs.h>
48#include <stdlib.h>
49
50#define on_error(rc, action) \
51 do { \
52 if ((rc) != EOK) \
53 action; \
54 } while (0)
55
56#define combine_rc(rc1, rc2) \
57 ((rc1) == EOK ? (rc2) : (rc1))
58
59#define answer_and_return(rid, rc) \
60 do { \
61 async_answer_0((rid), (rc)); \
62 return; \
63 } while (0)
64
65static fs_reg_t reg;
66
67static vfs_out_ops_t *vfs_out_ops = NULL;
68static libfs_ops_t *libfs_ops = NULL;
69
70static void libfs_mount(libfs_ops_t *, fs_handle_t, ipc_callid_t, ipc_call_t *);
71static void libfs_unmount(libfs_ops_t *, ipc_callid_t, ipc_call_t *);
72static void libfs_lookup(libfs_ops_t *, fs_handle_t, ipc_callid_t,
73 ipc_call_t *);
74static void libfs_stat(libfs_ops_t *, fs_handle_t, ipc_callid_t, ipc_call_t *);
75static void libfs_open_node(libfs_ops_t *, fs_handle_t, ipc_callid_t,
76 ipc_call_t *);
77static void libfs_statfs(libfs_ops_t *, fs_handle_t, ipc_callid_t, ipc_call_t *);
78
79static void vfs_out_mounted(ipc_callid_t rid, ipc_call_t *req)
80{
81 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*req);
82 char *opts;
83 int rc;
84
85 /* Accept the mount options. */
86 rc = async_data_write_accept((void **) &opts, true, 0, 0, 0, NULL);
87 if (rc != EOK) {
88 async_answer_0(rid, rc);
89 return;
90 }
91
92 fs_index_t index;
93 aoff64_t size;
94 unsigned lnkcnt;
95 rc = vfs_out_ops->mounted(service_id, opts, &index, &size, &lnkcnt);
96
97 if (rc == EOK)
98 async_answer_4(rid, EOK, index, LOWER32(size), UPPER32(size),
99 lnkcnt);
100 else
101 async_answer_0(rid, rc);
102
103 free(opts);
104}
105
106static void vfs_out_mount(ipc_callid_t rid, ipc_call_t *req)
107{
108 libfs_mount(libfs_ops, reg.fs_handle, rid, req);
109}
110
111static void vfs_out_unmounted(ipc_callid_t rid, ipc_call_t *req)
112{
113 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*req);
114 int rc;
115
116 rc = vfs_out_ops->unmounted(service_id);
117
118 async_answer_0(rid, rc);
119}
120
121static void vfs_out_unmount(ipc_callid_t rid, ipc_call_t *req)
122{
123
124 libfs_unmount(libfs_ops, rid, req);
125}
126
127static void vfs_out_lookup(ipc_callid_t rid, ipc_call_t *req)
128{
129 libfs_lookup(libfs_ops, reg.fs_handle, rid, req);
130}
131
132static void vfs_out_read(ipc_callid_t rid, ipc_call_t *req)
133{
134 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*req);
135 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*req);
136 aoff64_t pos = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*req),
137 IPC_GET_ARG4(*req));
138 size_t rbytes;
139 int rc;
140
141 rc = vfs_out_ops->read(service_id, index, pos, &rbytes);
142
143 if (rc == EOK)
144 async_answer_1(rid, EOK, rbytes);
145 else
146 async_answer_0(rid, rc);
147}
148
149static void vfs_out_write(ipc_callid_t rid, ipc_call_t *req)
150{
151 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*req);
152 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*req);
153 aoff64_t pos = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*req),
154 IPC_GET_ARG4(*req));
155 size_t wbytes;
156 aoff64_t nsize;
157 int rc;
158
159 rc = vfs_out_ops->write(service_id, index, pos, &wbytes, &nsize);
160
161 if (rc == EOK)
162 async_answer_3(rid, EOK, wbytes, LOWER32(nsize), UPPER32(nsize));
163 else
164 async_answer_0(rid, rc);
165}
166
167static void vfs_out_truncate(ipc_callid_t rid, ipc_call_t *req)
168{
169 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*req);
170 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*req);
171 aoff64_t size = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*req),
172 IPC_GET_ARG4(*req));
173 int rc;
174
175 rc = vfs_out_ops->truncate(service_id, index, size);
176
177 async_answer_0(rid, rc);
178}
179
180static void vfs_out_close(ipc_callid_t rid, ipc_call_t *req)
181{
182 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*req);
183 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*req);
184 int rc;
185
186 rc = vfs_out_ops->close(service_id, index);
187
188 async_answer_0(rid, rc);
189}
190
191static void vfs_out_destroy(ipc_callid_t rid, ipc_call_t *req)
192{
193 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*req);
194 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*req);
195 int rc;
196
197 rc = vfs_out_ops->destroy(service_id, index);
198
199 async_answer_0(rid, rc);
200}
201
202static void vfs_out_open_node(ipc_callid_t rid, ipc_call_t *req)
203{
204 libfs_open_node(libfs_ops, reg.fs_handle, rid, req);
205}
206
207static void vfs_out_stat(ipc_callid_t rid, ipc_call_t *req)
208{
209 libfs_stat(libfs_ops, reg.fs_handle, rid, req);
210}
211
212static void vfs_out_sync(ipc_callid_t rid, ipc_call_t *req)
213{
214 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*req);
215 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*req);
216 int rc;
217
218 rc = vfs_out_ops->sync(service_id, index);
219
220 async_answer_0(rid, rc);
221}
222
223static void vfs_out_statfs(ipc_callid_t rid, ipc_call_t *req)
224{
225 libfs_statfs(libfs_ops, reg.fs_handle, rid, req);
226}
227static void vfs_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
228{
229 if (iid) {
230 /*
231 * This only happens for connections opened by
232 * IPC_M_CONNECT_ME_TO calls as opposed to callback connections
233 * created by IPC_M_CONNECT_TO_ME.
234 */
235 async_answer_0(iid, EOK);
236 }
237
238 while (true) {
239 ipc_call_t call;
240 ipc_callid_t callid = async_get_call(&call);
241
242 if (!IPC_GET_IMETHOD(call))
243 return;
244
245 switch (IPC_GET_IMETHOD(call)) {
246 case VFS_OUT_MOUNTED:
247 vfs_out_mounted(callid, &call);
248 break;
249 case VFS_OUT_MOUNT:
250 vfs_out_mount(callid, &call);
251 break;
252 case VFS_OUT_UNMOUNTED:
253 vfs_out_unmounted(callid, &call);
254 break;
255 case VFS_OUT_UNMOUNT:
256 vfs_out_unmount(callid, &call);
257 break;
258 case VFS_OUT_LOOKUP:
259 vfs_out_lookup(callid, &call);
260 break;
261 case VFS_OUT_READ:
262 vfs_out_read(callid, &call);
263 break;
264 case VFS_OUT_WRITE:
265 vfs_out_write(callid, &call);
266 break;
267 case VFS_OUT_TRUNCATE:
268 vfs_out_truncate(callid, &call);
269 break;
270 case VFS_OUT_CLOSE:
271 vfs_out_close(callid, &call);
272 break;
273 case VFS_OUT_DESTROY:
274 vfs_out_destroy(callid, &call);
275 break;
276 case VFS_OUT_OPEN_NODE:
277 vfs_out_open_node(callid, &call);
278 break;
279 case VFS_OUT_STAT:
280 vfs_out_stat(callid, &call);
281 break;
282 case VFS_OUT_SYNC:
283 vfs_out_sync(callid, &call);
284 break;
285 case VFS_OUT_STATFS:
286 vfs_out_statfs(callid, &call);
287 break;
288 default:
289 async_answer_0(callid, ENOTSUP);
290 break;
291 }
292 }
293}
294
295/** Register file system server.
296 *
297 * This function abstracts away the tedious registration protocol from
298 * file system implementations and lets them to reuse this registration glue
299 * code.
300 *
301 * @param sess Session for communication with VFS.
302 * @param info VFS info structure supplied by the file system
303 * implementation.
304 * @param vops Address of the vfs_out_ops_t structure.
305 * @param lops Address of the libfs_ops_t structure.
306 *
307 * @return EOK on success or a non-zero error code on errror.
308 *
309 */
310int fs_register(async_sess_t *sess, vfs_info_t *info, vfs_out_ops_t *vops,
311 libfs_ops_t *lops)
312{
313 /*
314 * Tell VFS that we are here and want to get registered.
315 * We use the async framework because VFS will answer the request
316 * out-of-order, when it knows that the operation succeeded or failed.
317 */
318
319 async_exch_t *exch = async_exchange_begin(sess);
320
321 ipc_call_t answer;
322 aid_t req = async_send_0(exch, VFS_IN_REGISTER, &answer);
323
324 /*
325 * Send our VFS info structure to VFS.
326 */
327 int rc = async_data_write_start(exch, info, sizeof(*info));
328
329 if (rc != EOK) {
330 async_exchange_end(exch);
331 async_forget(req);
332 return rc;
333 }
334
335 /*
336 * Set VFS_OUT and libfs operations.
337 */
338 vfs_out_ops = vops;
339 libfs_ops = lops;
340
341 /*
342 * Ask VFS for callback connection.
343 */
344 port_id_t port;
345 rc = async_create_callback_port(exch, INTERFACE_VFS_DRIVER_CB, 0, 0,
346 vfs_connection, NULL, &port);
347
348 /*
349 * Request sharing the Path Lookup Buffer with VFS.
350 */
351 rc = async_share_in_start_0_0(exch, PLB_SIZE, (void *) &reg.plb_ro);
352 if (reg.plb_ro == AS_MAP_FAILED) {
353 async_exchange_end(exch);
354 async_forget(req);
355 return ENOMEM;
356 }
357
358 async_exchange_end(exch);
359
360 if (rc) {
361 async_forget(req);
362 return rc;
363 }
364
365 /*
366 * Pick up the answer for the request to the VFS_IN_REQUEST call.
367 */
368 async_wait_for(req, NULL);
369 reg.fs_handle = (int) IPC_GET_ARG1(answer);
370
371 /*
372 * Tell the async framework that other connections are to be handled by
373 * the same connection fibril as well.
374 */
375 async_set_fallback_port_handler(vfs_connection, NULL);
376
377 return IPC_GET_RETVAL(answer);
378}
379
380void fs_node_initialize(fs_node_t *fn)
381{
382 memset(fn, 0, sizeof(fs_node_t));
383}
384
385void libfs_mount(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
386 ipc_call_t *req)
387{
388 service_id_t mp_service_id = (service_id_t) IPC_GET_ARG1(*req);
389 fs_index_t mp_fs_index = (fs_index_t) IPC_GET_ARG2(*req);
390 fs_handle_t mr_fs_handle = (fs_handle_t) IPC_GET_ARG3(*req);
391 service_id_t mr_service_id = (service_id_t) IPC_GET_ARG4(*req);
392
393 async_sess_t *mountee_sess = async_clone_receive(EXCHANGE_PARALLEL);
394 if (mountee_sess == NULL) {
395 async_answer_0(rid, EINVAL);
396 return;
397 }
398
399 fs_node_t *fn;
400 int res = ops->node_get(&fn, mp_service_id, mp_fs_index);
401 if ((res != EOK) || (!fn)) {
402 async_hangup(mountee_sess);
403 async_data_write_void(combine_rc(res, ENOENT));
404 async_answer_0(rid, combine_rc(res, ENOENT));
405 return;
406 }
407
408 if (fn->mp_data.mp_active) {
409 async_hangup(mountee_sess);
410 (void) ops->node_put(fn);
411 async_data_write_void(EBUSY);
412 async_answer_0(rid, EBUSY);
413 return;
414 }
415
416 async_exch_t *exch = async_exchange_begin(mountee_sess);
417 async_sess_t *sess = async_clone_establish(EXCHANGE_PARALLEL, exch);
418
419 if (!sess) {
420 async_exchange_end(exch);
421 async_hangup(mountee_sess);
422 (void) ops->node_put(fn);
423 async_data_write_void(errno);
424 async_answer_0(rid, errno);
425 return;
426 }
427
428 ipc_call_t answer;
429 int rc = async_data_write_forward_1_1(exch, VFS_OUT_MOUNTED,
430 mr_service_id, &answer);
431 async_exchange_end(exch);
432
433 if (rc == EOK) {
434 fn->mp_data.mp_active = true;
435 fn->mp_data.fs_handle = mr_fs_handle;
436 fn->mp_data.service_id = mr_service_id;
437 fn->mp_data.sess = mountee_sess;
438 }
439
440 /*
441 * Do not release the FS node so that it stays in memory.
442 */
443 async_answer_4(rid, rc, IPC_GET_ARG1(answer), IPC_GET_ARG2(answer),
444 IPC_GET_ARG3(answer), IPC_GET_ARG4(answer));
445}
446
447void libfs_unmount(libfs_ops_t *ops, ipc_callid_t rid, ipc_call_t *req)
448{
449 service_id_t mp_service_id = (service_id_t) IPC_GET_ARG1(*req);
450 fs_index_t mp_fs_index = (fs_index_t) IPC_GET_ARG2(*req);
451 fs_node_t *fn;
452 int res;
453
454 res = ops->node_get(&fn, mp_service_id, mp_fs_index);
455 if ((res != EOK) || (!fn)) {
456 async_answer_0(rid, combine_rc(res, ENOENT));
457 return;
458 }
459
460 /*
461 * We are clearly expecting to find the mount point active.
462 */
463 if (!fn->mp_data.mp_active) {
464 (void) ops->node_put(fn);
465 async_answer_0(rid, EINVAL);
466 return;
467 }
468
469 /*
470 * Tell the mounted file system to unmount.
471 */
472 async_exch_t *exch = async_exchange_begin(fn->mp_data.sess);
473 res = async_req_1_0(exch, VFS_OUT_UNMOUNTED, fn->mp_data.service_id);
474 async_exchange_end(exch);
475
476 /*
477 * If everything went well, perform the clean-up on our side.
478 */
479 if (res == EOK) {
480 async_hangup(fn->mp_data.sess);
481 fn->mp_data.mp_active = false;
482 fn->mp_data.fs_handle = 0;
483 fn->mp_data.service_id = 0;
484 fn->mp_data.sess = NULL;
485
486 /* Drop the reference created in libfs_mount(). */
487 (void) ops->node_put(fn);
488 }
489
490 (void) ops->node_put(fn);
491 async_answer_0(rid, res);
492}
493
494static char plb_get_char(unsigned pos)
495{
496 return reg.plb_ro[pos % PLB_SIZE];
497}
498
499/** Lookup VFS triplet by name in the file system name space.
500 *
501 * The path passed in the PLB must be in the canonical file system path format
502 * as returned by the canonify() function.
503 *
504 * @param ops libfs operations structure with function pointers to
505 * file system implementation
506 * @param fs_handle File system handle of the file system where to perform
507 * the lookup.
508 * @param rid Request ID of the VFS_OUT_LOOKUP request.
509 * @param request VFS_OUT_LOOKUP request data itself.
510 *
511 */
512void libfs_lookup(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
513 ipc_call_t *req)
514{
515 unsigned int first = IPC_GET_ARG1(*req);
516 unsigned int last = IPC_GET_ARG2(*req);
517 unsigned int next = first;
518 service_id_t service_id = IPC_GET_ARG3(*req);
519 int lflag = IPC_GET_ARG4(*req);
520 fs_index_t index = IPC_GET_ARG5(*req);
521 char component[NAME_MAX + 1];
522 int len;
523 int rc;
524
525 if (last < next)
526 last += PLB_SIZE;
527
528 fs_node_t *par = NULL;
529 fs_node_t *cur = NULL;
530 fs_node_t *tmp = NULL;
531
532 rc = ops->root_get(&cur, service_id);
533 on_error(rc, goto out_with_answer);
534
535 if (cur->mp_data.mp_active) {
536 async_exch_t *exch = async_exchange_begin(cur->mp_data.sess);
537 async_forward_slow(rid, exch, VFS_OUT_LOOKUP, next, last,
538 cur->mp_data.service_id, lflag, index,
539 IPC_FF_ROUTE_FROM_ME);
540 async_exchange_end(exch);
541
542 (void) ops->node_put(cur);
543 return;
544 }
545
546 /* Eat slash */
547 if (plb_get_char(next) == '/')
548 next++;
549
550 while (next <= last) {
551 bool has_children;
552
553 rc = ops->has_children(&has_children, cur);
554 on_error(rc, goto out_with_answer);
555 if (!has_children)
556 break;
557
558 /* Collect the component */
559 len = 0;
560 while ((next <= last) && (plb_get_char(next) != '/')) {
561 if (len + 1 == NAME_MAX) {
562 /* Component length overflow */
563 async_answer_0(rid, ENAMETOOLONG);
564 goto out;
565 }
566 component[len++] = plb_get_char(next);
567 /* Process next character */
568 next++;
569 }
570
571 assert(len);
572 component[len] = '\0';
573 /* Eat slash */
574 next++;
575
576 /* Match the component */
577 rc = ops->match(&tmp, cur, component);
578 on_error(rc, goto out_with_answer);
579
580 /*
581 * If the matching component is a mount point, there are two
582 * legitimate semantics of the lookup operation. The first is
583 * the commonly used one in which the lookup crosses each mount
584 * point into the mounted file system. The second semantics is
585 * used mostly during unmount() and differs from the first one
586 * only in that the last mount point in the looked up path,
587 * which is also its last component, is not crossed.
588 */
589
590 if ((tmp) && (tmp->mp_data.mp_active) &&
591 (!(lflag & L_MP) || (next <= last))) {
592 if (next > last)
593 next = last = first;
594 else
595 next--;
596
597 async_exch_t *exch = async_exchange_begin(tmp->mp_data.sess);
598 async_forward_slow(rid, exch, VFS_OUT_LOOKUP, next,
599 last, tmp->mp_data.service_id, lflag, index,
600 IPC_FF_ROUTE_FROM_ME);
601 async_exchange_end(exch);
602
603 (void) ops->node_put(cur);
604 (void) ops->node_put(tmp);
605 if (par)
606 (void) ops->node_put(par);
607 return;
608 }
609
610 /* Handle miss: match amongst siblings */
611 if (!tmp) {
612 if (next <= last) {
613 /* There are unprocessed components */
614 async_answer_0(rid, ENOENT);
615 goto out;
616 }
617
618 /* Miss in the last component */
619 if (lflag & (L_CREATE | L_LINK)) {
620 /* Request to create a new link */
621 if (!ops->is_directory(cur)) {
622 async_answer_0(rid, ENOTDIR);
623 goto out;
624 }
625
626 fs_node_t *fn;
627 if (lflag & L_CREATE)
628 rc = ops->create(&fn, service_id,
629 lflag);
630 else
631 rc = ops->node_get(&fn, service_id,
632 index);
633 on_error(rc, goto out_with_answer);
634
635 if (fn) {
636 rc = ops->link(cur, fn, component);
637 if (rc != EOK) {
638 if (lflag & L_CREATE)
639 (void) ops->destroy(fn);
640 else
641 (void) ops->node_put(fn);
642 async_answer_0(rid, rc);
643 } else {
644 (void) ops->node_put(cur);
645 cur = fn;
646 goto out_with_answer;
647 }
648 } else
649 async_answer_0(rid, ENOSPC);
650
651 goto out;
652 }
653
654 async_answer_0(rid, ENOENT);
655 goto out;
656 }
657
658 if (par) {
659 rc = ops->node_put(par);
660 on_error(rc, goto out_with_answer);
661 }
662
663 /* Descend one level */
664 par = cur;
665 cur = tmp;
666 tmp = NULL;
667 }
668
669 /* Handle miss: excessive components */
670 if (next <= last) {
671 bool has_children;
672 rc = ops->has_children(&has_children, cur);
673 on_error(rc, goto out_with_answer);
674
675 if (has_children)
676 goto skip_miss;
677
678 if (lflag & (L_CREATE | L_LINK)) {
679 if (!ops->is_directory(cur)) {
680 async_answer_0(rid, ENOTDIR);
681 goto out;
682 }
683
684 /* Collect next component */
685 len = 0;
686 while (next <= last) {
687 if (plb_get_char(next) == '/') {
688 /* More than one component */
689 async_answer_0(rid, ENOENT);
690 goto out;
691 }
692
693 if (len + 1 == NAME_MAX) {
694 /* Component length overflow */
695 async_answer_0(rid, ENAMETOOLONG);
696 goto out;
697 }
698
699 component[len++] = plb_get_char(next);
700 /* Process next character */
701 next++;
702 }
703
704 assert(len);
705 component[len] = '\0';
706
707 fs_node_t *fn;
708 if (lflag & L_CREATE)
709 rc = ops->create(&fn, service_id, lflag);
710 else
711 rc = ops->node_get(&fn, service_id, index);
712 on_error(rc, goto out_with_answer);
713
714 if (fn) {
715 rc = ops->link(cur, fn, component);
716 if (rc != EOK) {
717 if (lflag & L_CREATE)
718 (void) ops->destroy(fn);
719 else
720 (void) ops->node_put(fn);
721 async_answer_0(rid, rc);
722 } else {
723 (void) ops->node_put(cur);
724 cur = fn;
725 goto out_with_answer;
726 }
727 } else
728 async_answer_0(rid, ENOSPC);
729
730 goto out;
731 }
732
733 async_answer_0(rid, ENOENT);
734 goto out;
735 }
736
737skip_miss:
738
739 /* Handle hit */
740 if (lflag & L_UNLINK) {
741 unsigned int old_lnkcnt = ops->lnkcnt_get(cur);
742 rc = ops->unlink(par, cur, component);
743
744 if (rc == EOK) {
745 aoff64_t size = ops->size_get(cur);
746 async_answer_5(rid, fs_handle, service_id,
747 ops->index_get(cur), LOWER32(size), UPPER32(size),
748 old_lnkcnt);
749 } else
750 async_answer_0(rid, rc);
751
752 goto out;
753 }
754
755 if (((lflag & (L_CREATE | L_EXCLUSIVE)) == (L_CREATE | L_EXCLUSIVE)) ||
756 (lflag & L_LINK)) {
757 async_answer_0(rid, EEXIST);
758 goto out;
759 }
760
761 if ((lflag & L_FILE) && (ops->is_directory(cur))) {
762 async_answer_0(rid, EISDIR);
763 goto out;
764 }
765
766 if ((lflag & L_DIRECTORY) && (ops->is_file(cur))) {
767 async_answer_0(rid, ENOTDIR);
768 goto out;
769 }
770
771 if ((lflag & L_ROOT) && par) {
772 async_answer_0(rid, EINVAL);
773 goto out;
774 }
775
776out_with_answer:
777
778 if (rc == EOK) {
779 if (lflag & L_OPEN)
780 rc = ops->node_open(cur);
781
782 if (rc == EOK) {
783 aoff64_t size = ops->size_get(cur);
784 async_answer_5(rid, fs_handle, service_id,
785 ops->index_get(cur), LOWER32(size), UPPER32(size),
786 ops->lnkcnt_get(cur));
787 } else
788 async_answer_0(rid, rc);
789
790 } else
791 async_answer_0(rid, rc);
792
793out:
794
795 if (par)
796 (void) ops->node_put(par);
797
798 if (cur)
799 (void) ops->node_put(cur);
800
801 if (tmp)
802 (void) ops->node_put(tmp);
803}
804
805void libfs_stat(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
806 ipc_call_t *request)
807{
808 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*request);
809 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
810
811 fs_node_t *fn;
812 int rc = ops->node_get(&fn, service_id, index);
813 on_error(rc, answer_and_return(rid, rc));
814
815 ipc_callid_t callid;
816 size_t size;
817 if ((!async_data_read_receive(&callid, &size)) ||
818 (size != sizeof(struct stat))) {
819 ops->node_put(fn);
820 async_answer_0(callid, EINVAL);
821 async_answer_0(rid, EINVAL);
822 return;
823 }
824
825 struct stat stat;
826 memset(&stat, 0, sizeof(struct stat));
827
828 stat.fs_handle = fs_handle;
829 stat.service_id = service_id;
830 stat.index = index;
831 stat.lnkcnt = ops->lnkcnt_get(fn);
832 stat.is_file = ops->is_file(fn);
833 stat.is_directory = ops->is_directory(fn);
834 stat.size = ops->size_get(fn);
835 stat.service = ops->service_get(fn);
836
837 ops->node_put(fn);
838
839
840 async_data_read_finalize(callid, &stat, sizeof(struct stat));
841 async_answer_0(rid, EOK);
842}
843
844void libfs_statfs(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
845 ipc_call_t *request)
846{
847 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*request);
848 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
849
850 fs_node_t *fn;
851 int rc = ops->node_get(&fn, service_id, index);
852 on_error(rc, answer_and_return(rid, rc));
853
854 ipc_callid_t callid;
855 size_t size;
856 if ((!async_data_read_receive(&callid, &size)) ||
857 (size != sizeof(struct statfs))) {
858 goto error;
859 }
860
861 struct statfs st;
862 memset(&st, 0, sizeof(struct statfs));
863
864 if (ops->size_block != NULL) {
865 rc = ops->size_block(service_id, &st.f_bsize);
866 if (rc != EOK)
867 goto error;
868 }
869
870 if (ops->total_block_count != NULL) {
871 rc = ops->total_block_count(service_id, &st.f_blocks);
872 if (rc != EOK)
873 goto error;
874 }
875
876 if (ops->free_block_count != NULL) {
877 rc = ops->free_block_count(service_id, &st.f_bfree);
878 if (rc != EOK)
879 goto error;
880 }
881
882 ops->node_put(fn);
883 async_data_read_finalize(callid, &st, sizeof(struct statfs));
884 async_answer_0(rid, EOK);
885 return;
886
887error:
888 ops->node_put(fn);
889 async_answer_0(callid, EINVAL);
890 async_answer_0(rid, EINVAL);
891}
892
893
894/** Open VFS triplet.
895 *
896 * @param ops libfs operations structure with function pointers to
897 * file system implementation
898 * @param rid Request ID of the VFS_OUT_OPEN_NODE request.
899 * @param request VFS_OUT_OPEN_NODE request data itself.
900 *
901 */
902void libfs_open_node(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
903 ipc_call_t *request)
904{
905 service_id_t service_id = IPC_GET_ARG1(*request);
906 fs_index_t index = IPC_GET_ARG2(*request);
907
908 fs_node_t *fn;
909 int rc = ops->node_get(&fn, service_id, index);
910 on_error(rc, answer_and_return(rid, rc));
911
912 if (fn == NULL) {
913 async_answer_0(rid, ENOENT);
914 return;
915 }
916
917 rc = ops->node_open(fn);
918 aoff64_t size = ops->size_get(fn);
919 async_answer_4(rid, rc, LOWER32(size), UPPER32(size),
920 ops->lnkcnt_get(fn),
921 (ops->is_file(fn) ? L_FILE : 0) | (ops->is_directory(fn) ? L_DIRECTORY : 0));
922
923 (void) ops->node_put(fn);
924}
925
926static FIBRIL_MUTEX_INITIALIZE(instances_mutex);
927static LIST_INITIALIZE(instances_list);
928
929typedef struct {
930 service_id_t service_id;
931 link_t link;
932 void *data;
933} fs_instance_t;
934
935int fs_instance_create(service_id_t service_id, void *data)
936{
937 fs_instance_t *inst = malloc(sizeof(fs_instance_t));
938 if (!inst)
939 return ENOMEM;
940
941 link_initialize(&inst->link);
942 inst->service_id = service_id;
943 inst->data = data;
944
945 fibril_mutex_lock(&instances_mutex);
946 list_foreach(instances_list, link, fs_instance_t, cur) {
947 if (cur->service_id == service_id) {
948 fibril_mutex_unlock(&instances_mutex);
949 free(inst);
950 return EEXIST;
951 }
952
953 /* keep the list sorted */
954 if (cur->service_id < service_id) {
955 list_insert_before(&inst->link, &cur->link);
956 fibril_mutex_unlock(&instances_mutex);
957 return EOK;
958 }
959 }
960 list_append(&inst->link, &instances_list);
961 fibril_mutex_unlock(&instances_mutex);
962
963 return EOK;
964}
965
966int fs_instance_get(service_id_t service_id, void **idp)
967{
968 fibril_mutex_lock(&instances_mutex);
969 list_foreach(instances_list, link, fs_instance_t, inst) {
970 if (inst->service_id == service_id) {
971 *idp = inst->data;
972 fibril_mutex_unlock(&instances_mutex);
973 return EOK;
974 }
975 }
976 fibril_mutex_unlock(&instances_mutex);
977 return ENOENT;
978}
979
980int fs_instance_destroy(service_id_t service_id)
981{
982 fibril_mutex_lock(&instances_mutex);
983 list_foreach(instances_list, link, fs_instance_t, inst) {
984 if (inst->service_id == service_id) {
985 list_remove(&inst->link);
986 fibril_mutex_unlock(&instances_mutex);
987 free(inst);
988 return EOK;
989 }
990 }
991 fibril_mutex_unlock(&instances_mutex);
992 return ENOENT;
993}
994
995/** @}
996 */
Note: See TracBrowser for help on using the repository browser.