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

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

Get rid of per filesystem VFS_OUT method switch and IPC unmarshalling.

  • libfs now understands the notion of VFS_OUT operations and provides the single version of the switch
  • libfs now automatically takes care of some libfs provided operations, such as lookup and stat; filesystem need not be even aware of these
  • one filesystem time per libfs instance
  • plb_get_char() no longer a libfs operation
  • filesystem implemenations need not worry about IPC with the exception of VFS_OUT_READ/WRITE methods and filesystems that depend on doing extra IPC in these and similar methods, such as devfs
  • Property mode set to 100644
File size: 21.5 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 devmap_handle_t devmap_handle = (devmap_handle_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(devmap_handle, opts, &index, &size, &lnkcnt);
94
95 if (rc == EOK) // FIXME: size is 64-bit
96 async_answer_3(rid, EOK, index, size, lnkcnt);
97 else
98 async_answer_0(rid, rc);
99
100 free(opts);
101}
102
103static void vfs_out_mount(ipc_callid_t rid, ipc_call_t *req)
104{
105 libfs_mount(libfs_ops, reg.fs_handle, rid, req);
106}
107
108static void vfs_out_unmounted(ipc_callid_t rid, ipc_call_t *req)
109{
110 devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*req);
111 int rc;
112
113 rc = vfs_out_ops->unmounted(devmap_handle);
114
115 async_answer_0(rid, rc);
116}
117
118static void vfs_out_unmount(ipc_callid_t rid, ipc_call_t *req)
119{
120
121 libfs_unmount(libfs_ops, rid, req);
122}
123
124static void vfs_out_lookup(ipc_callid_t rid, ipc_call_t *req)
125{
126 libfs_lookup(libfs_ops, reg.fs_handle, rid, req);
127}
128
129static void vfs_out_read(ipc_callid_t rid, ipc_call_t *req)
130{
131 devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*req);
132 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*req);
133 aoff64_t pos = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*req),
134 IPC_GET_ARG4(*req));
135 size_t rbytes;
136 int rc;
137
138 rc = vfs_out_ops->read(devmap_handle, index, pos, &rbytes);
139
140 if (rc == EOK)
141 async_answer_1(rid, EOK, rbytes);
142 else
143 async_answer_0(rid, rc);
144}
145
146static void vfs_out_write(ipc_callid_t rid, ipc_call_t *req)
147{
148 devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*req);
149 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*req);
150 aoff64_t pos = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*req),
151 IPC_GET_ARG4(*req));
152 size_t wbytes;
153 aoff64_t nsize;
154 int rc;
155
156 rc = vfs_out_ops->write(devmap_handle, index, pos, &wbytes, &nsize);
157
158 if (rc == EOK) // FIXME: nsize is 64-bit
159 async_answer_2(rid, EOK, wbytes, nsize);
160 else
161 async_answer_0(rid, rc);
162}
163
164static void vfs_out_truncate(ipc_callid_t rid, ipc_call_t *req)
165{
166 devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*req);
167 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*req);
168 aoff64_t size = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*req),
169 IPC_GET_ARG4(*req));
170 int rc;
171
172 rc = vfs_out_ops->truncate(devmap_handle, index, size);
173
174 async_answer_0(rid, rc);
175}
176
177static void vfs_out_close(ipc_callid_t rid, ipc_call_t *req)
178{
179 devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*req);
180 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*req);
181 int rc;
182
183 rc = vfs_out_ops->close(devmap_handle, index);
184
185 async_answer_0(rid, rc);
186}
187
188static void vfs_out_destroy(ipc_callid_t rid, ipc_call_t *req)
189{
190 devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*req);
191 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*req);
192 int rc;
193
194 rc = vfs_out_ops->destroy(devmap_handle, index);
195
196 async_answer_0(rid, rc);
197}
198
199static void vfs_out_open_node(ipc_callid_t rid, ipc_call_t *req)
200{
201 libfs_open_node(libfs_ops, reg.fs_handle, rid, req);
202}
203
204static void vfs_out_stat(ipc_callid_t rid, ipc_call_t *req)
205{
206 libfs_stat(libfs_ops, reg.fs_handle, rid, req);
207}
208
209static void vfs_out_sync(ipc_callid_t rid, ipc_call_t *req)
210{
211 devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*req);
212 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*req);
213 int rc;
214
215 rc = vfs_out_ops->sync(devmap_handle, index);
216
217 async_answer_0(rid, rc);
218}
219
220static void vfs_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
221{
222 if (iid) {
223 /*
224 * This only happens for connections opened by
225 * IPC_M_CONNECT_ME_TO calls as opposed to callback connections
226 * created by IPC_M_CONNECT_TO_ME.
227 */
228 async_answer_0(iid, EOK);
229 }
230
231 while (true) {
232 ipc_call_t call;
233 ipc_callid_t callid = async_get_call(&call);
234
235 if (!IPC_GET_IMETHOD(call))
236 return;
237
238 switch (IPC_GET_IMETHOD(call)) {
239 case VFS_OUT_MOUNTED:
240 vfs_out_mounted(callid, &call);
241 break;
242 case VFS_OUT_MOUNT:
243 vfs_out_mount(callid, &call);
244 break;
245 case VFS_OUT_UNMOUNTED:
246 vfs_out_unmounted(callid, &call);
247 break;
248 case VFS_OUT_UNMOUNT:
249 vfs_out_unmount(callid, &call);
250 break;
251 case VFS_OUT_LOOKUP:
252 vfs_out_lookup(callid, &call);
253 break;
254 case VFS_OUT_READ:
255 vfs_out_read(callid, &call);
256 break;
257 case VFS_OUT_WRITE:
258 vfs_out_write(callid, &call);
259 break;
260 case VFS_OUT_TRUNCATE:
261 vfs_out_truncate(callid, &call);
262 break;
263 case VFS_OUT_CLOSE:
264 vfs_out_close(callid, &call);
265 break;
266 case VFS_OUT_DESTROY:
267 vfs_out_destroy(callid, &call);
268 break;
269 case VFS_OUT_OPEN_NODE:
270 vfs_out_open_node(callid, &call);
271 break;
272 case VFS_OUT_STAT:
273 vfs_out_stat(callid, &call);
274 break;
275 case VFS_OUT_SYNC:
276 vfs_out_sync(callid, &call);
277 break;
278 default:
279 async_answer_0(callid, ENOTSUP);
280 break;
281 }
282 }
283}
284
285/** Register file system server.
286 *
287 * This function abstracts away the tedious registration protocol from
288 * file system implementations and lets them to reuse this registration glue
289 * code.
290 *
291 * @param sess Session for communication with VFS.
292 * @param info VFS info structure supplied by the file system
293 * implementation.
294 * @param vops Address of the vfs_out_ops_t structure.
295 * @param lops Address of the libfs_ops_t structure.
296 *
297 * @return EOK on success or a non-zero error code on errror.
298 *
299 */
300int fs_register(async_sess_t *sess, vfs_info_t *info, vfs_out_ops_t *vops,
301 libfs_ops_t *lops)
302{
303 /*
304 * Tell VFS that we are here and want to get registered.
305 * We use the async framework because VFS will answer the request
306 * out-of-order, when it knows that the operation succeeded or failed.
307 */
308
309 async_exch_t *exch = async_exchange_begin(sess);
310
311 ipc_call_t answer;
312 aid_t req = async_send_0(exch, VFS_IN_REGISTER, &answer);
313
314 /*
315 * Send our VFS info structure to VFS.
316 */
317 int rc = async_data_write_start(exch, info, sizeof(*info));
318
319 if (rc != EOK) {
320 async_exchange_end(exch);
321 async_wait_for(req, NULL);
322 return rc;
323 }
324
325 /*
326 * Set VFS_OUT and libfs operations.
327 */
328 vfs_out_ops = vops;
329 libfs_ops = lops;
330
331 /*
332 * Ask VFS for callback connection.
333 */
334 async_connect_to_me(exch, 0, 0, 0, vfs_connection, NULL);
335
336 /*
337 * Allocate piece of address space for PLB.
338 */
339 reg.plb_ro = as_get_mappable_page(PLB_SIZE);
340 if (!reg.plb_ro) {
341 async_exchange_end(exch);
342 async_wait_for(req, NULL);
343 return ENOMEM;
344 }
345
346 /*
347 * Request sharing the Path Lookup Buffer with VFS.
348 */
349 rc = async_share_in_start_0_0(exch, reg.plb_ro, PLB_SIZE);
350
351 async_exchange_end(exch);
352
353 if (rc) {
354 async_wait_for(req, NULL);
355 return rc;
356 }
357
358 /*
359 * Pick up the answer for the request to the VFS_IN_REQUEST call.
360 */
361 async_wait_for(req, NULL);
362 reg.fs_handle = (int) IPC_GET_ARG1(answer);
363
364 /*
365 * Tell the async framework that other connections are to be handled by
366 * the same connection fibril as well.
367 */
368 async_set_client_connection(vfs_connection);
369
370 return IPC_GET_RETVAL(answer);
371}
372
373void fs_node_initialize(fs_node_t *fn)
374{
375 memset(fn, 0, sizeof(fs_node_t));
376}
377
378void libfs_mount(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
379 ipc_call_t *req)
380{
381 devmap_handle_t mp_devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*req);
382 fs_index_t mp_fs_index = (fs_index_t) IPC_GET_ARG2(*req);
383 fs_handle_t mr_fs_handle = (fs_handle_t) IPC_GET_ARG3(*req);
384 devmap_handle_t mr_devmap_handle = (devmap_handle_t) IPC_GET_ARG4(*req);
385
386 async_sess_t *mountee_sess = async_clone_receive(EXCHANGE_PARALLEL);
387 if (mountee_sess == NULL) {
388 async_answer_0(rid, EINVAL);
389 return;
390 }
391
392 fs_node_t *fn;
393 int res = ops->node_get(&fn, mp_devmap_handle, mp_fs_index);
394 if ((res != EOK) || (!fn)) {
395 async_hangup(mountee_sess);
396 async_data_write_void(combine_rc(res, ENOENT));
397 async_answer_0(rid, combine_rc(res, ENOENT));
398 return;
399 }
400
401 if (fn->mp_data.mp_active) {
402 async_hangup(mountee_sess);
403 (void) ops->node_put(fn);
404 async_data_write_void(EBUSY);
405 async_answer_0(rid, EBUSY);
406 return;
407 }
408
409 async_exch_t *exch = async_exchange_begin(mountee_sess);
410 async_sess_t *sess = async_connect_me(EXCHANGE_PARALLEL, exch);
411
412 if (!sess) {
413 async_exchange_end(exch);
414 async_hangup(mountee_sess);
415 (void) ops->node_put(fn);
416 async_data_write_void(errno);
417 async_answer_0(rid, errno);
418 return;
419 }
420
421 ipc_call_t answer;
422 int rc = async_data_write_forward_1_1(exch, VFS_OUT_MOUNTED,
423 mr_devmap_handle, &answer);
424 async_exchange_end(exch);
425
426 if (rc == EOK) {
427 fn->mp_data.mp_active = true;
428 fn->mp_data.fs_handle = mr_fs_handle;
429 fn->mp_data.devmap_handle = mr_devmap_handle;
430 fn->mp_data.sess = mountee_sess;
431 }
432
433 /*
434 * Do not release the FS node so that it stays in memory.
435 */
436 async_answer_3(rid, rc, IPC_GET_ARG1(answer), IPC_GET_ARG2(answer),
437 IPC_GET_ARG3(answer));
438}
439
440void libfs_unmount(libfs_ops_t *ops, ipc_callid_t rid, ipc_call_t *req)
441{
442 devmap_handle_t mp_devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*req);
443 fs_index_t mp_fs_index = (fs_index_t) IPC_GET_ARG2(*req);
444 fs_node_t *fn;
445 int res;
446
447 res = ops->node_get(&fn, mp_devmap_handle, mp_fs_index);
448 if ((res != EOK) || (!fn)) {
449 async_answer_0(rid, combine_rc(res, ENOENT));
450 return;
451 }
452
453 /*
454 * We are clearly expecting to find the mount point active.
455 */
456 if (!fn->mp_data.mp_active) {
457 (void) ops->node_put(fn);
458 async_answer_0(rid, EINVAL);
459 return;
460 }
461
462 /*
463 * Tell the mounted file system to unmount.
464 */
465 async_exch_t *exch = async_exchange_begin(fn->mp_data.sess);
466 res = async_req_1_0(exch, VFS_OUT_UNMOUNTED, fn->mp_data.devmap_handle);
467 async_exchange_end(exch);
468
469 /*
470 * If everything went well, perform the clean-up on our side.
471 */
472 if (res == EOK) {
473 async_hangup(fn->mp_data.sess);
474 fn->mp_data.mp_active = false;
475 fn->mp_data.fs_handle = 0;
476 fn->mp_data.devmap_handle = 0;
477 fn->mp_data.sess = NULL;
478
479 /* Drop the reference created in libfs_mount(). */
480 (void) ops->node_put(fn);
481 }
482
483 (void) ops->node_put(fn);
484 async_answer_0(rid, res);
485}
486
487static char plb_get_char(unsigned pos)
488{
489 return reg.plb_ro[pos % PLB_SIZE];
490}
491
492/** Lookup VFS triplet by name in the file system name space.
493 *
494 * The path passed in the PLB must be in the canonical file system path format
495 * as returned by the canonify() function.
496 *
497 * @param ops libfs operations structure with function pointers to
498 * file system implementation
499 * @param fs_handle File system handle of the file system where to perform
500 * the lookup.
501 * @param rid Request ID of the VFS_OUT_LOOKUP request.
502 * @param request VFS_OUT_LOOKUP request data itself.
503 *
504 */
505void libfs_lookup(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
506 ipc_call_t *req)
507{
508 unsigned int first = IPC_GET_ARG1(*req);
509 unsigned int last = IPC_GET_ARG2(*req);
510 unsigned int next = first;
511 devmap_handle_t devmap_handle = IPC_GET_ARG3(*req);
512 int lflag = IPC_GET_ARG4(*req);
513 fs_index_t index = IPC_GET_ARG5(*req);
514 char component[NAME_MAX + 1];
515 int len;
516 int rc;
517
518 if (last < next)
519 last += PLB_SIZE;
520
521 fs_node_t *par = NULL;
522 fs_node_t *cur = NULL;
523 fs_node_t *tmp = NULL;
524
525 rc = ops->root_get(&cur, devmap_handle);
526 on_error(rc, goto out_with_answer);
527
528 if (cur->mp_data.mp_active) {
529 async_exch_t *exch = async_exchange_begin(cur->mp_data.sess);
530 async_forward_slow(rid, exch, VFS_OUT_LOOKUP, next, last,
531 cur->mp_data.devmap_handle, lflag, index,
532 IPC_FF_ROUTE_FROM_ME);
533 async_exchange_end(exch);
534
535 (void) ops->node_put(cur);
536 return;
537 }
538
539 /* Eat slash */
540 if (plb_get_char(next) == '/')
541 next++;
542
543 while (next <= last) {
544 bool has_children;
545
546 rc = ops->has_children(&has_children, cur);
547 on_error(rc, goto out_with_answer);
548 if (!has_children)
549 break;
550
551 /* Collect the component */
552 len = 0;
553 while ((next <= last) && (plb_get_char(next) != '/')) {
554 if (len + 1 == NAME_MAX) {
555 /* Component length overflow */
556 async_answer_0(rid, ENAMETOOLONG);
557 goto out;
558 }
559 component[len++] = plb_get_char(next);
560 /* Process next character */
561 next++;
562 }
563
564 assert(len);
565 component[len] = '\0';
566 /* Eat slash */
567 next++;
568
569 /* Match the component */
570 rc = ops->match(&tmp, cur, component);
571 on_error(rc, goto out_with_answer);
572
573 /*
574 * If the matching component is a mount point, there are two
575 * legitimate semantics of the lookup operation. The first is
576 * the commonly used one in which the lookup crosses each mount
577 * point into the mounted file system. The second semantics is
578 * used mostly during unmount() and differs from the first one
579 * only in that the last mount point in the looked up path,
580 * which is also its last component, is not crossed.
581 */
582
583 if ((tmp) && (tmp->mp_data.mp_active) &&
584 (!(lflag & L_MP) || (next <= last))) {
585 if (next > last)
586 next = last = first;
587 else
588 next--;
589
590 async_exch_t *exch = async_exchange_begin(tmp->mp_data.sess);
591 async_forward_slow(rid, exch, VFS_OUT_LOOKUP, next,
592 last, tmp->mp_data.devmap_handle, lflag, index,
593 IPC_FF_ROUTE_FROM_ME);
594 async_exchange_end(exch);
595
596 (void) ops->node_put(cur);
597 (void) ops->node_put(tmp);
598 if (par)
599 (void) ops->node_put(par);
600 return;
601 }
602
603 /* Handle miss: match amongst siblings */
604 if (!tmp) {
605 if (next <= last) {
606 /* There are unprocessed components */
607 async_answer_0(rid, ENOENT);
608 goto out;
609 }
610
611 /* Miss in the last component */
612 if (lflag & (L_CREATE | L_LINK)) {
613 /* Request to create a new link */
614 if (!ops->is_directory(cur)) {
615 async_answer_0(rid, ENOTDIR);
616 goto out;
617 }
618
619 fs_node_t *fn;
620 if (lflag & L_CREATE)
621 rc = ops->create(&fn, devmap_handle,
622 lflag);
623 else
624 rc = ops->node_get(&fn, devmap_handle,
625 index);
626 on_error(rc, goto out_with_answer);
627
628 if (fn) {
629 rc = ops->link(cur, fn, component);
630 if (rc != EOK) {
631 if (lflag & L_CREATE)
632 (void) ops->destroy(fn);
633 else
634 (void) ops->node_put(fn);
635 async_answer_0(rid, rc);
636 } else {
637 aoff64_t size = ops->size_get(fn);
638 async_answer_5(rid, fs_handle,
639 devmap_handle,
640 ops->index_get(fn),
641 LOWER32(size),
642 UPPER32(size),
643 ops->lnkcnt_get(fn));
644 (void) ops->node_put(fn);
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, devmap_handle, lflag);
708 else
709 rc = ops->node_get(&fn, devmap_handle, 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 aoff64_t size = ops->size_get(fn);
722 async_answer_5(rid, fs_handle,
723 devmap_handle,
724 ops->index_get(fn),
725 LOWER32(size),
726 UPPER32(size),
727 ops->lnkcnt_get(fn));
728 (void) ops->node_put(fn);
729 }
730 } else
731 async_answer_0(rid, ENOSPC);
732
733 goto out;
734 }
735
736 async_answer_0(rid, ENOENT);
737 goto out;
738 }
739
740skip_miss:
741
742 /* Handle hit */
743 if (lflag & L_UNLINK) {
744 unsigned int old_lnkcnt = ops->lnkcnt_get(cur);
745 rc = ops->unlink(par, cur, component);
746
747 if (rc == EOK) {
748 aoff64_t size = ops->size_get(cur);
749 async_answer_5(rid, fs_handle, devmap_handle,
750 ops->index_get(cur), LOWER32(size), UPPER32(size),
751 old_lnkcnt);
752 } else
753 async_answer_0(rid, rc);
754
755 goto out;
756 }
757
758 if (((lflag & (L_CREATE | L_EXCLUSIVE)) == (L_CREATE | L_EXCLUSIVE)) ||
759 (lflag & L_LINK)) {
760 async_answer_0(rid, EEXIST);
761 goto out;
762 }
763
764 if ((lflag & L_FILE) && (ops->is_directory(cur))) {
765 async_answer_0(rid, EISDIR);
766 goto out;
767 }
768
769 if ((lflag & L_DIRECTORY) && (ops->is_file(cur))) {
770 async_answer_0(rid, ENOTDIR);
771 goto out;
772 }
773
774 if ((lflag & L_ROOT) && par) {
775 async_answer_0(rid, EINVAL);
776 goto out;
777 }
778
779out_with_answer:
780
781 if (rc == EOK) {
782 if (lflag & L_OPEN)
783 rc = ops->node_open(cur);
784
785 if (rc == EOK) {
786 aoff64_t size = ops->size_get(cur);
787 async_answer_5(rid, fs_handle, devmap_handle,
788 ops->index_get(cur), LOWER32(size), UPPER32(size),
789 ops->lnkcnt_get(cur));
790 } else
791 async_answer_0(rid, rc);
792
793 } else
794 async_answer_0(rid, rc);
795
796out:
797
798 if (par)
799 (void) ops->node_put(par);
800
801 if (cur)
802 (void) ops->node_put(cur);
803
804 if (tmp)
805 (void) ops->node_put(tmp);
806}
807
808void libfs_stat(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
809 ipc_call_t *request)
810{
811 devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
812 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
813
814 fs_node_t *fn;
815 int rc = ops->node_get(&fn, devmap_handle, index);
816 on_error(rc, answer_and_return(rid, rc));
817
818 ipc_callid_t callid;
819 size_t size;
820 if ((!async_data_read_receive(&callid, &size)) ||
821 (size != sizeof(struct stat))) {
822 ops->node_put(fn);
823 async_answer_0(callid, EINVAL);
824 async_answer_0(rid, EINVAL);
825 return;
826 }
827
828 struct stat stat;
829 memset(&stat, 0, sizeof(struct stat));
830
831 stat.fs_handle = fs_handle;
832 stat.devmap_handle = devmap_handle;
833 stat.index = index;
834 stat.lnkcnt = ops->lnkcnt_get(fn);
835 stat.is_file = ops->is_file(fn);
836 stat.is_directory = ops->is_directory(fn);
837 stat.size = ops->size_get(fn);
838 stat.device = ops->device_get(fn);
839
840 ops->node_put(fn);
841
842 async_data_read_finalize(callid, &stat, sizeof(struct stat));
843 async_answer_0(rid, EOK);
844}
845
846/** Open VFS triplet.
847 *
848 * @param ops libfs operations structure with function pointers to
849 * file system implementation
850 * @param rid Request ID of the VFS_OUT_OPEN_NODE request.
851 * @param request VFS_OUT_OPEN_NODE request data itself.
852 *
853 */
854void libfs_open_node(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
855 ipc_call_t *request)
856{
857 devmap_handle_t devmap_handle = IPC_GET_ARG1(*request);
858 fs_index_t index = IPC_GET_ARG2(*request);
859
860 fs_node_t *fn;
861 int rc = ops->node_get(&fn, devmap_handle, index);
862 on_error(rc, answer_and_return(rid, rc));
863
864 if (fn == NULL) {
865 async_answer_0(rid, ENOENT);
866 return;
867 }
868
869 rc = ops->node_open(fn);
870 aoff64_t size = ops->size_get(fn);
871 async_answer_4(rid, rc, LOWER32(size), UPPER32(size),
872 ops->lnkcnt_get(fn),
873 (ops->is_file(fn) ? L_FILE : 0) | (ops->is_directory(fn) ? L_DIRECTORY : 0));
874
875 (void) ops->node_put(fn);
876}
877
878/** @}
879 */
Note: See TracBrowser for help on using the repository browser.