source: mainline/uspace/lib/fs/libfs.c@ 9bdfde73

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

Libfs now opens file even when creating new file (L_CREATE & L_OPEN).

  • Property mode set to 100644
File size: 22.6 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_forget(req);
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 == AS_MAP_FAILED) {
342 async_exchange_end(exch);
343 async_forget(req);
344 return ENOMEM;
345 }
346
347 async_exchange_end(exch);
348
349 if (rc) {
350 async_forget(req);
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_clone_establish(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 (void) ops->node_put(cur);
634 cur = fn;
635 goto out_with_answer;
636 }
637 } else
638 async_answer_0(rid, ENOSPC);
639
640 goto out;
641 }
642
643 async_answer_0(rid, ENOENT);
644 goto out;
645 }
646
647 if (par) {
648 rc = ops->node_put(par);
649 on_error(rc, goto out_with_answer);
650 }
651
652 /* Descend one level */
653 par = cur;
654 cur = tmp;
655 tmp = NULL;
656 }
657
658 /* Handle miss: excessive components */
659 if (next <= last) {
660 bool has_children;
661 rc = ops->has_children(&has_children, cur);
662 on_error(rc, goto out_with_answer);
663
664 if (has_children)
665 goto skip_miss;
666
667 if (lflag & (L_CREATE | L_LINK)) {
668 if (!ops->is_directory(cur)) {
669 async_answer_0(rid, ENOTDIR);
670 goto out;
671 }
672
673 /* Collect next component */
674 len = 0;
675 while (next <= last) {
676 if (plb_get_char(next) == '/') {
677 /* More than one component */
678 async_answer_0(rid, ENOENT);
679 goto out;
680 }
681
682 if (len + 1 == NAME_MAX) {
683 /* Component length overflow */
684 async_answer_0(rid, ENAMETOOLONG);
685 goto out;
686 }
687
688 component[len++] = plb_get_char(next);
689 /* Process next character */
690 next++;
691 }
692
693 assert(len);
694 component[len] = '\0';
695
696 fs_node_t *fn;
697 if (lflag & L_CREATE)
698 rc = ops->create(&fn, service_id, lflag);
699 else
700 rc = ops->node_get(&fn, service_id, index);
701 on_error(rc, goto out_with_answer);
702
703 if (fn) {
704 rc = ops->link(cur, fn, component);
705 if (rc != EOK) {
706 if (lflag & L_CREATE)
707 (void) ops->destroy(fn);
708 else
709 (void) ops->node_put(fn);
710 async_answer_0(rid, rc);
711 } else {
712 (void) ops->node_put(cur);
713 cur = fn;
714 goto out_with_answer;
715 }
716 } else
717 async_answer_0(rid, ENOSPC);
718
719 goto out;
720 }
721
722 async_answer_0(rid, ENOENT);
723 goto out;
724 }
725
726skip_miss:
727
728 /* Handle hit */
729 if (lflag & L_UNLINK) {
730 unsigned int old_lnkcnt = ops->lnkcnt_get(cur);
731 rc = ops->unlink(par, cur, component);
732
733 if (rc == EOK) {
734 aoff64_t size = ops->size_get(cur);
735 async_answer_5(rid, fs_handle, service_id,
736 ops->index_get(cur), LOWER32(size), UPPER32(size),
737 old_lnkcnt);
738 } else
739 async_answer_0(rid, rc);
740
741 goto out;
742 }
743
744 if (((lflag & (L_CREATE | L_EXCLUSIVE)) == (L_CREATE | L_EXCLUSIVE)) ||
745 (lflag & L_LINK)) {
746 async_answer_0(rid, EEXIST);
747 goto out;
748 }
749
750 if ((lflag & L_FILE) && (ops->is_directory(cur))) {
751 async_answer_0(rid, EISDIR);
752 goto out;
753 }
754
755 if ((lflag & L_DIRECTORY) && (ops->is_file(cur))) {
756 async_answer_0(rid, ENOTDIR);
757 goto out;
758 }
759
760 if ((lflag & L_ROOT) && par) {
761 async_answer_0(rid, EINVAL);
762 goto out;
763 }
764
765out_with_answer:
766
767 if (rc == EOK) {
768 if (lflag & L_OPEN)
769 rc = ops->node_open(cur);
770
771 if (rc == EOK) {
772 aoff64_t size = ops->size_get(cur);
773 async_answer_5(rid, fs_handle, service_id,
774 ops->index_get(cur), LOWER32(size), UPPER32(size),
775 ops->lnkcnt_get(cur));
776 } else
777 async_answer_0(rid, rc);
778
779 } else
780 async_answer_0(rid, rc);
781
782out:
783
784 if (par)
785 (void) ops->node_put(par);
786
787 if (cur)
788 (void) ops->node_put(cur);
789
790 if (tmp)
791 (void) ops->node_put(tmp);
792}
793
794void libfs_stat(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
795 ipc_call_t *request)
796{
797 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*request);
798 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
799
800 fs_node_t *fn;
801 int rc = ops->node_get(&fn, service_id, index);
802 on_error(rc, answer_and_return(rid, rc));
803
804 ipc_callid_t callid;
805 size_t size;
806 if ((!async_data_read_receive(&callid, &size)) ||
807 (size != sizeof(struct stat))) {
808 ops->node_put(fn);
809 async_answer_0(callid, EINVAL);
810 async_answer_0(rid, EINVAL);
811 return;
812 }
813
814 struct stat stat;
815 memset(&stat, 0, sizeof(struct stat));
816
817 stat.fs_handle = fs_handle;
818 stat.service_id = service_id;
819 stat.index = index;
820 stat.lnkcnt = ops->lnkcnt_get(fn);
821 stat.is_file = ops->is_file(fn);
822 stat.is_directory = ops->is_directory(fn);
823 stat.size = ops->size_get(fn);
824 stat.service = ops->service_get(fn);
825
826 ops->node_put(fn);
827
828 async_data_read_finalize(callid, &stat, sizeof(struct stat));
829 async_answer_0(rid, EOK);
830}
831
832/** Open VFS triplet.
833 *
834 * @param ops libfs operations structure with function pointers to
835 * file system implementation
836 * @param rid Request ID of the VFS_OUT_OPEN_NODE request.
837 * @param request VFS_OUT_OPEN_NODE request data itself.
838 *
839 */
840void libfs_open_node(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
841 ipc_call_t *request)
842{
843 service_id_t service_id = IPC_GET_ARG1(*request);
844 fs_index_t index = IPC_GET_ARG2(*request);
845
846 fs_node_t *fn;
847 int rc = ops->node_get(&fn, service_id, index);
848 on_error(rc, answer_and_return(rid, rc));
849
850 if (fn == NULL) {
851 async_answer_0(rid, ENOENT);
852 return;
853 }
854
855 rc = ops->node_open(fn);
856 aoff64_t size = ops->size_get(fn);
857 async_answer_4(rid, rc, LOWER32(size), UPPER32(size),
858 ops->lnkcnt_get(fn),
859 (ops->is_file(fn) ? L_FILE : 0) | (ops->is_directory(fn) ? L_DIRECTORY : 0));
860
861 (void) ops->node_put(fn);
862}
863
864static FIBRIL_MUTEX_INITIALIZE(instances_mutex);
865static LIST_INITIALIZE(instances_list);
866
867typedef struct {
868 service_id_t service_id;
869 link_t link;
870 void *data;
871} fs_instance_t;
872
873int fs_instance_create(service_id_t service_id, void *data)
874{
875 fs_instance_t *inst = malloc(sizeof(fs_instance_t));
876 if (!inst)
877 return ENOMEM;
878
879 link_initialize(&inst->link);
880 inst->service_id = service_id;
881 inst->data = data;
882
883 fibril_mutex_lock(&instances_mutex);
884 list_foreach(instances_list, link) {
885 fs_instance_t *cur = list_get_instance(link, fs_instance_t,
886 link);
887
888 if (cur->service_id == service_id) {
889 fibril_mutex_unlock(&instances_mutex);
890 free(inst);
891 return EEXIST;
892 }
893
894 /* keep the list sorted */
895 if (cur->service_id < service_id) {
896 list_insert_before(&inst->link, &cur->link);
897 fibril_mutex_unlock(&instances_mutex);
898 return EOK;
899 }
900 }
901 list_append(&inst->link, &instances_list);
902 fibril_mutex_unlock(&instances_mutex);
903
904 return EOK;
905}
906
907int fs_instance_get(service_id_t service_id, void **idp)
908{
909 fibril_mutex_lock(&instances_mutex);
910 list_foreach(instances_list, link) {
911 fs_instance_t *inst = list_get_instance(link, fs_instance_t,
912 link);
913
914 if (inst->service_id == service_id) {
915 *idp = inst->data;
916 fibril_mutex_unlock(&instances_mutex);
917 return EOK;
918 }
919 }
920 fibril_mutex_unlock(&instances_mutex);
921 return ENOENT;
922}
923
924int fs_instance_destroy(service_id_t service_id)
925{
926 fibril_mutex_lock(&instances_mutex);
927 list_foreach(instances_list, link) {
928 fs_instance_t *inst = list_get_instance(link, fs_instance_t,
929 link);
930
931 if (inst->service_id == service_id) {
932 list_remove(&inst->link);
933 fibril_mutex_unlock(&instances_mutex);
934 free(inst);
935 return EOK;
936 }
937 }
938 fibril_mutex_unlock(&instances_mutex);
939 return ENOENT;
940}
941
942/** @}
943 */
Note: See TracBrowser for help on using the repository browser.