source: mainline/uspace/lib/fs/libfs.c@ 45aa22c

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

Remove the two-phase way of creating virtual memory areas (first asking for a mappable address and then mapping it) which was prone to race conditions when two or more calls to as_get_mappable_page() and as_area_create() were interleaved. This for example caused the e1k driver to randomly fail.

The memory area related syscalls and IPC calls have all been altered to accept a special value (void *) -1, representing a demand to atomically search for a mappable address space "hole" and map to it.

Individual changes:

  • IPC_M_SHARE_OUT: the destination address space area is supplied by the kernel, the callee only specifies the lower bound

(the address is returned to the callee via a pointer in an IPC reply argument)

  • IPC_M_SHARE_IN: the destination address space ares is supplied by the kernel, the callee only specifies the lower bound

(the address is returned to the caller as usual via an IPC argument)

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