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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c84146d3 was c84146d3, checked in by Manuele Conti <conti.ma@…>, 12 years ago

Replace name operations from total_block and free_block to total_block_count and
free_block_count

  • 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 async_connect_to_me(exch, 0, 0, 0, vfs_connection, NULL);
345
346 /*
347 * Request sharing the Path Lookup Buffer with VFS.
348 */
349 rc = async_share_in_start_0_0(exch, PLB_SIZE, (void *) &reg.plb_ro);
350 if (reg.plb_ro == AS_MAP_FAILED) {
351 async_exchange_end(exch);
352 async_forget(req);
353 return ENOMEM;
354 }
355
356 async_exchange_end(exch);
357
358 if (rc) {
359 async_forget(req);
360 return rc;
361 }
362
363 /*
364 * Pick up the answer for the request to the VFS_IN_REQUEST call.
365 */
366 async_wait_for(req, NULL);
367 reg.fs_handle = (int) IPC_GET_ARG1(answer);
368
369 /*
370 * Tell the async framework that other connections are to be handled by
371 * the same connection fibril as well.
372 */
373 async_set_client_connection(vfs_connection);
374
375 return IPC_GET_RETVAL(answer);
376}
377
378void fs_node_initialize(fs_node_t *fn)
379{
380 memset(fn, 0, sizeof(fs_node_t));
381}
382
383void libfs_mount(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
384 ipc_call_t *req)
385{
386 service_id_t mp_service_id = (service_id_t) IPC_GET_ARG1(*req);
387 fs_index_t mp_fs_index = (fs_index_t) IPC_GET_ARG2(*req);
388 fs_handle_t mr_fs_handle = (fs_handle_t) IPC_GET_ARG3(*req);
389 service_id_t mr_service_id = (service_id_t) IPC_GET_ARG4(*req);
390
391 async_sess_t *mountee_sess = async_clone_receive(EXCHANGE_PARALLEL);
392 if (mountee_sess == NULL) {
393 async_answer_0(rid, EINVAL);
394 return;
395 }
396
397 fs_node_t *fn;
398 int res = ops->node_get(&fn, mp_service_id, mp_fs_index);
399 if ((res != EOK) || (!fn)) {
400 async_hangup(mountee_sess);
401 async_data_write_void(combine_rc(res, ENOENT));
402 async_answer_0(rid, combine_rc(res, ENOENT));
403 return;
404 }
405
406 if (fn->mp_data.mp_active) {
407 async_hangup(mountee_sess);
408 (void) ops->node_put(fn);
409 async_data_write_void(EBUSY);
410 async_answer_0(rid, EBUSY);
411 return;
412 }
413
414 async_exch_t *exch = async_exchange_begin(mountee_sess);
415 async_sess_t *sess = async_clone_establish(EXCHANGE_PARALLEL, exch);
416
417 if (!sess) {
418 async_exchange_end(exch);
419 async_hangup(mountee_sess);
420 (void) ops->node_put(fn);
421 async_data_write_void(errno);
422 async_answer_0(rid, errno);
423 return;
424 }
425
426 ipc_call_t answer;
427 int rc = async_data_write_forward_1_1(exch, VFS_OUT_MOUNTED,
428 mr_service_id, &answer);
429 async_exchange_end(exch);
430
431 if (rc == EOK) {
432 fn->mp_data.mp_active = true;
433 fn->mp_data.fs_handle = mr_fs_handle;
434 fn->mp_data.service_id = mr_service_id;
435 fn->mp_data.sess = mountee_sess;
436 }
437
438 /*
439 * Do not release the FS node so that it stays in memory.
440 */
441 async_answer_4(rid, rc, IPC_GET_ARG1(answer), IPC_GET_ARG2(answer),
442 IPC_GET_ARG3(answer), IPC_GET_ARG4(answer));
443}
444
445void libfs_unmount(libfs_ops_t *ops, ipc_callid_t rid, ipc_call_t *req)
446{
447 service_id_t mp_service_id = (service_id_t) IPC_GET_ARG1(*req);
448 fs_index_t mp_fs_index = (fs_index_t) IPC_GET_ARG2(*req);
449 fs_node_t *fn;
450 int res;
451
452 res = ops->node_get(&fn, mp_service_id, mp_fs_index);
453 if ((res != EOK) || (!fn)) {
454 async_answer_0(rid, combine_rc(res, ENOENT));
455 return;
456 }
457
458 /*
459 * We are clearly expecting to find the mount point active.
460 */
461 if (!fn->mp_data.mp_active) {
462 (void) ops->node_put(fn);
463 async_answer_0(rid, EINVAL);
464 return;
465 }
466
467 /*
468 * Tell the mounted file system to unmount.
469 */
470 async_exch_t *exch = async_exchange_begin(fn->mp_data.sess);
471 res = async_req_1_0(exch, VFS_OUT_UNMOUNTED, fn->mp_data.service_id);
472 async_exchange_end(exch);
473
474 /*
475 * If everything went well, perform the clean-up on our side.
476 */
477 if (res == EOK) {
478 async_hangup(fn->mp_data.sess);
479 fn->mp_data.mp_active = false;
480 fn->mp_data.fs_handle = 0;
481 fn->mp_data.service_id = 0;
482 fn->mp_data.sess = NULL;
483
484 /* Drop the reference created in libfs_mount(). */
485 (void) ops->node_put(fn);
486 }
487
488 (void) ops->node_put(fn);
489 async_answer_0(rid, res);
490}
491
492static char plb_get_char(unsigned pos)
493{
494 return reg.plb_ro[pos % PLB_SIZE];
495}
496
497/** Lookup VFS triplet by name in the file system name space.
498 *
499 * The path passed in the PLB must be in the canonical file system path format
500 * as returned by the canonify() function.
501 *
502 * @param ops libfs operations structure with function pointers to
503 * file system implementation
504 * @param fs_handle File system handle of the file system where to perform
505 * the lookup.
506 * @param rid Request ID of the VFS_OUT_LOOKUP request.
507 * @param request VFS_OUT_LOOKUP request data itself.
508 *
509 */
510void libfs_lookup(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
511 ipc_call_t *req)
512{
513 unsigned int first = IPC_GET_ARG1(*req);
514 unsigned int last = IPC_GET_ARG2(*req);
515 unsigned int next = first;
516 service_id_t service_id = IPC_GET_ARG3(*req);
517 int lflag = IPC_GET_ARG4(*req);
518 fs_index_t index = IPC_GET_ARG5(*req);
519 char component[NAME_MAX + 1];
520 int len;
521 int rc;
522
523 if (last < next)
524 last += PLB_SIZE;
525
526 fs_node_t *par = NULL;
527 fs_node_t *cur = NULL;
528 fs_node_t *tmp = NULL;
529
530 rc = ops->root_get(&cur, service_id);
531 on_error(rc, goto out_with_answer);
532
533 if (cur->mp_data.mp_active) {
534 async_exch_t *exch = async_exchange_begin(cur->mp_data.sess);
535 async_forward_slow(rid, exch, VFS_OUT_LOOKUP, next, last,
536 cur->mp_data.service_id, lflag, index,
537 IPC_FF_ROUTE_FROM_ME);
538 async_exchange_end(exch);
539
540 (void) ops->node_put(cur);
541 return;
542 }
543
544 /* Eat slash */
545 if (plb_get_char(next) == '/')
546 next++;
547
548 while (next <= last) {
549 bool has_children;
550
551 rc = ops->has_children(&has_children, cur);
552 on_error(rc, goto out_with_answer);
553 if (!has_children)
554 break;
555
556 /* Collect the component */
557 len = 0;
558 while ((next <= last) && (plb_get_char(next) != '/')) {
559 if (len + 1 == NAME_MAX) {
560 /* Component length overflow */
561 async_answer_0(rid, ENAMETOOLONG);
562 goto out;
563 }
564 component[len++] = plb_get_char(next);
565 /* Process next character */
566 next++;
567 }
568
569 assert(len);
570 component[len] = '\0';
571 /* Eat slash */
572 next++;
573
574 /* Match the component */
575 rc = ops->match(&tmp, cur, component);
576 on_error(rc, goto out_with_answer);
577
578 /*
579 * If the matching component is a mount point, there are two
580 * legitimate semantics of the lookup operation. The first is
581 * the commonly used one in which the lookup crosses each mount
582 * point into the mounted file system. The second semantics is
583 * used mostly during unmount() and differs from the first one
584 * only in that the last mount point in the looked up path,
585 * which is also its last component, is not crossed.
586 */
587
588 if ((tmp) && (tmp->mp_data.mp_active) &&
589 (!(lflag & L_MP) || (next <= last))) {
590 if (next > last)
591 next = last = first;
592 else
593 next--;
594
595 async_exch_t *exch = async_exchange_begin(tmp->mp_data.sess);
596 async_forward_slow(rid, exch, VFS_OUT_LOOKUP, next,
597 last, tmp->mp_data.service_id, lflag, index,
598 IPC_FF_ROUTE_FROM_ME);
599 async_exchange_end(exch);
600
601 (void) ops->node_put(cur);
602 (void) ops->node_put(tmp);
603 if (par)
604 (void) ops->node_put(par);
605 return;
606 }
607
608 /* Handle miss: match amongst siblings */
609 if (!tmp) {
610 if (next <= last) {
611 /* There are unprocessed components */
612 async_answer_0(rid, ENOENT);
613 goto out;
614 }
615
616 /* Miss in the last component */
617 if (lflag & (L_CREATE | L_LINK)) {
618 /* Request to create a new link */
619 if (!ops->is_directory(cur)) {
620 async_answer_0(rid, ENOTDIR);
621 goto out;
622 }
623
624 fs_node_t *fn;
625 if (lflag & L_CREATE)
626 rc = ops->create(&fn, service_id,
627 lflag);
628 else
629 rc = ops->node_get(&fn, service_id,
630 index);
631 on_error(rc, goto out_with_answer);
632
633 if (fn) {
634 rc = ops->link(cur, fn, component);
635 if (rc != EOK) {
636 if (lflag & L_CREATE)
637 (void) ops->destroy(fn);
638 else
639 (void) ops->node_put(fn);
640 async_answer_0(rid, rc);
641 } else {
642 (void) ops->node_put(cur);
643 cur = fn;
644 goto out_with_answer;
645 }
646 } else
647 async_answer_0(rid, ENOSPC);
648
649 goto out;
650 }
651
652 async_answer_0(rid, ENOENT);
653 goto out;
654 }
655
656 if (par) {
657 rc = ops->node_put(par);
658 on_error(rc, goto out_with_answer);
659 }
660
661 /* Descend one level */
662 par = cur;
663 cur = tmp;
664 tmp = NULL;
665 }
666
667 /* Handle miss: excessive components */
668 if (next <= last) {
669 bool has_children;
670 rc = ops->has_children(&has_children, cur);
671 on_error(rc, goto out_with_answer);
672
673 if (has_children)
674 goto skip_miss;
675
676 if (lflag & (L_CREATE | L_LINK)) {
677 if (!ops->is_directory(cur)) {
678 async_answer_0(rid, ENOTDIR);
679 goto out;
680 }
681
682 /* Collect next component */
683 len = 0;
684 while (next <= last) {
685 if (plb_get_char(next) == '/') {
686 /* More than one component */
687 async_answer_0(rid, ENOENT);
688 goto out;
689 }
690
691 if (len + 1 == NAME_MAX) {
692 /* Component length overflow */
693 async_answer_0(rid, ENAMETOOLONG);
694 goto out;
695 }
696
697 component[len++] = plb_get_char(next);
698 /* Process next character */
699 next++;
700 }
701
702 assert(len);
703 component[len] = '\0';
704
705 fs_node_t *fn;
706 if (lflag & L_CREATE)
707 rc = ops->create(&fn, service_id, lflag);
708 else
709 rc = ops->node_get(&fn, service_id, index);
710 on_error(rc, goto out_with_answer);
711
712 if (fn) {
713 rc = ops->link(cur, fn, component);
714 if (rc != EOK) {
715 if (lflag & L_CREATE)
716 (void) ops->destroy(fn);
717 else
718 (void) ops->node_put(fn);
719 async_answer_0(rid, rc);
720 } else {
721 (void) ops->node_put(cur);
722 cur = fn;
723 goto out_with_answer;
724 }
725 } else
726 async_answer_0(rid, ENOSPC);
727
728 goto out;
729 }
730
731 async_answer_0(rid, ENOENT);
732 goto out;
733 }
734
735skip_miss:
736
737 /* Handle hit */
738 if (lflag & L_UNLINK) {
739 unsigned int old_lnkcnt = ops->lnkcnt_get(cur);
740 rc = ops->unlink(par, cur, component);
741
742 if (rc == EOK) {
743 aoff64_t size = ops->size_get(cur);
744 async_answer_5(rid, fs_handle, service_id,
745 ops->index_get(cur), LOWER32(size), UPPER32(size),
746 old_lnkcnt);
747 } else
748 async_answer_0(rid, rc);
749
750 goto out;
751 }
752
753 if (((lflag & (L_CREATE | L_EXCLUSIVE)) == (L_CREATE | L_EXCLUSIVE)) ||
754 (lflag & L_LINK)) {
755 async_answer_0(rid, EEXIST);
756 goto out;
757 }
758
759 if ((lflag & L_FILE) && (ops->is_directory(cur))) {
760 async_answer_0(rid, EISDIR);
761 goto out;
762 }
763
764 if ((lflag & L_DIRECTORY) && (ops->is_file(cur))) {
765 async_answer_0(rid, ENOTDIR);
766 goto out;
767 }
768
769 if ((lflag & L_ROOT) && par) {
770 async_answer_0(rid, EINVAL);
771 goto out;
772 }
773
774out_with_answer:
775
776 if (rc == EOK) {
777 if (lflag & L_OPEN)
778 rc = ops->node_open(cur);
779
780 if (rc == EOK) {
781 aoff64_t size = ops->size_get(cur);
782 async_answer_5(rid, fs_handle, service_id,
783 ops->index_get(cur), LOWER32(size), UPPER32(size),
784 ops->lnkcnt_get(cur));
785 } else
786 async_answer_0(rid, rc);
787
788 } else
789 async_answer_0(rid, rc);
790
791out:
792
793 if (par)
794 (void) ops->node_put(par);
795
796 if (cur)
797 (void) ops->node_put(cur);
798
799 if (tmp)
800 (void) ops->node_put(tmp);
801}
802
803void libfs_stat(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
804 ipc_call_t *request)
805{
806 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*request);
807 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
808
809 fs_node_t *fn;
810 int rc = ops->node_get(&fn, service_id, index);
811 on_error(rc, answer_and_return(rid, rc));
812
813 ipc_callid_t callid;
814 size_t size;
815 if ((!async_data_read_receive(&callid, &size)) ||
816 (size != sizeof(struct stat))) {
817 ops->node_put(fn);
818 async_answer_0(callid, EINVAL);
819 async_answer_0(rid, EINVAL);
820 return;
821 }
822
823 struct stat stat;
824 memset(&stat, 0, sizeof(struct stat));
825
826 stat.fs_handle = fs_handle;
827 stat.service_id = service_id;
828 stat.index = index;
829 stat.lnkcnt = ops->lnkcnt_get(fn);
830 stat.is_file = ops->is_file(fn);
831 stat.is_directory = ops->is_directory(fn);
832 stat.size = ops->size_get(fn);
833 stat.service = ops->service_get(fn);
834
835 ops->node_put(fn);
836
837 async_data_read_finalize(callid, &stat, sizeof(struct stat));
838 async_answer_0(rid, EOK);
839}
840
841void libfs_statfs(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
842 ipc_call_t *request)
843{
844 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*request);
845 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
846
847 fs_node_t *fn;
848 int rc = ops->node_get(&fn, service_id, index);
849 on_error(rc, answer_and_return(rid, rc));
850
851 ipc_callid_t callid;
852 size_t size;
853 if ((!async_data_read_receive(&callid, &size)) ||
854 (size != sizeof(struct statfs))) {
855 ops->node_put(fn);
856 async_answer_0(callid, EINVAL);
857 async_answer_0(rid, EINVAL);
858 return;
859 }
860
861 struct statfs statfs;
862 memset(&statfs, 0, sizeof(struct statfs));
863
864 if (NULL != ops->size_block)
865 statfs.f_bsize = ops->size_block(service_id);
866 if (NULL != ops->total_block_count)
867 statfs.f_blocks = ops->total_block_count(service_id);
868 if (NULL != ops->free_block_count)
869 statfs.f_bfree = ops->free_block_count(service_id);
870
871 ops->node_put(fn);
872
873 async_data_read_finalize(callid, &statfs, sizeof(struct statfs));
874 async_answer_0(rid, EOK);
875}
876
877
878/** Open VFS triplet.
879 *
880 * @param ops libfs operations structure with function pointers to
881 * file system implementation
882 * @param rid Request ID of the VFS_OUT_OPEN_NODE request.
883 * @param request VFS_OUT_OPEN_NODE request data itself.
884 *
885 */
886void libfs_open_node(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
887 ipc_call_t *request)
888{
889 service_id_t service_id = IPC_GET_ARG1(*request);
890 fs_index_t index = IPC_GET_ARG2(*request);
891
892 fs_node_t *fn;
893 int rc = ops->node_get(&fn, service_id, index);
894 on_error(rc, answer_and_return(rid, rc));
895
896 if (fn == NULL) {
897 async_answer_0(rid, ENOENT);
898 return;
899 }
900
901 rc = ops->node_open(fn);
902 aoff64_t size = ops->size_get(fn);
903 async_answer_4(rid, rc, LOWER32(size), UPPER32(size),
904 ops->lnkcnt_get(fn),
905 (ops->is_file(fn) ? L_FILE : 0) | (ops->is_directory(fn) ? L_DIRECTORY : 0));
906
907 (void) ops->node_put(fn);
908}
909
910static FIBRIL_MUTEX_INITIALIZE(instances_mutex);
911static LIST_INITIALIZE(instances_list);
912
913typedef struct {
914 service_id_t service_id;
915 link_t link;
916 void *data;
917} fs_instance_t;
918
919int fs_instance_create(service_id_t service_id, void *data)
920{
921 fs_instance_t *inst = malloc(sizeof(fs_instance_t));
922 if (!inst)
923 return ENOMEM;
924
925 link_initialize(&inst->link);
926 inst->service_id = service_id;
927 inst->data = data;
928
929 fibril_mutex_lock(&instances_mutex);
930 list_foreach(instances_list, link) {
931 fs_instance_t *cur = list_get_instance(link, fs_instance_t,
932 link);
933
934 if (cur->service_id == service_id) {
935 fibril_mutex_unlock(&instances_mutex);
936 free(inst);
937 return EEXIST;
938 }
939
940 /* keep the list sorted */
941 if (cur->service_id < service_id) {
942 list_insert_before(&inst->link, &cur->link);
943 fibril_mutex_unlock(&instances_mutex);
944 return EOK;
945 }
946 }
947 list_append(&inst->link, &instances_list);
948 fibril_mutex_unlock(&instances_mutex);
949
950 return EOK;
951}
952
953int fs_instance_get(service_id_t service_id, void **idp)
954{
955 fibril_mutex_lock(&instances_mutex);
956 list_foreach(instances_list, link) {
957 fs_instance_t *inst = list_get_instance(link, fs_instance_t,
958 link);
959
960 if (inst->service_id == service_id) {
961 *idp = inst->data;
962 fibril_mutex_unlock(&instances_mutex);
963 return EOK;
964 }
965 }
966 fibril_mutex_unlock(&instances_mutex);
967 return ENOENT;
968}
969
970int fs_instance_destroy(service_id_t service_id)
971{
972 fibril_mutex_lock(&instances_mutex);
973 list_foreach(instances_list, link) {
974 fs_instance_t *inst = list_get_instance(link, fs_instance_t,
975 link);
976
977 if (inst->service_id == service_id) {
978 list_remove(&inst->link);
979 fibril_mutex_unlock(&instances_mutex);
980 free(inst);
981 return EOK;
982 }
983 }
984 fibril_mutex_unlock(&instances_mutex);
985 return ENOENT;
986}
987
988/** @}
989 */
Note: See TracBrowser for help on using the repository browser.