source: mainline/uspace/lib/fs/libfs.c@ 9934f7d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9934f7d was 9934f7d, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Add extra argument to async connection handlers that can be used for passing
information from async_connect_to_me() to the handler.

  • Property mode set to 100644
File size: 16.1 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
48#define on_error(rc, action) \
49 do { \
50 if ((rc) != EOK) \
51 action; \
52 } while (0)
53
54#define combine_rc(rc1, rc2) \
55 ((rc1) == EOK ? (rc2) : (rc1))
56
57#define answer_and_return(rid, rc) \
58 do { \
59 async_answer_0((rid), (rc)); \
60 return; \
61 } while (0)
62
63/** Register file system server.
64 *
65 * This function abstracts away the tedious registration protocol from
66 * file system implementations and lets them to reuse this registration glue
67 * code.
68 *
69 * @param sess Session for communication with VFS.
70 * @param reg File system registration structure. It will be
71 * initialized by this function.
72 * @param info VFS info structure supplied by the file system
73 * implementation.
74 * @param conn Connection fibril for handling all calls originating in
75 * VFS.
76 *
77 * @return EOK on success or a non-zero error code on errror.
78 *
79 */
80int fs_register(async_sess_t *sess, fs_reg_t *reg, vfs_info_t *info,
81 async_client_conn_t conn)
82{
83 /*
84 * Tell VFS that we are here and want to get registered.
85 * We use the async framework because VFS will answer the request
86 * out-of-order, when it knows that the operation succeeded or failed.
87 */
88
89 async_exch_t *exch = async_exchange_begin(sess);
90
91 ipc_call_t answer;
92 aid_t req = async_send_0(exch, VFS_IN_REGISTER, &answer);
93
94 /*
95 * Send our VFS info structure to VFS.
96 */
97 int rc = async_data_write_start(exch, info, sizeof(*info));
98
99 if (rc != EOK) {
100 async_exchange_end(exch);
101 async_wait_for(req, NULL);
102 return rc;
103 }
104
105 /*
106 * Ask VFS for callback connection.
107 */
108 async_connect_to_me(exch, 0, 0, 0, conn, NULL);
109
110 /*
111 * Allocate piece of address space for PLB.
112 */
113 reg->plb_ro = as_get_mappable_page(PLB_SIZE);
114 if (!reg->plb_ro) {
115 async_exchange_end(exch);
116 async_wait_for(req, NULL);
117 return ENOMEM;
118 }
119
120 /*
121 * Request sharing the Path Lookup Buffer with VFS.
122 */
123 rc = async_share_in_start_0_0(exch, reg->plb_ro, PLB_SIZE);
124
125 async_exchange_end(exch);
126
127 if (rc) {
128 async_wait_for(req, NULL);
129 return rc;
130 }
131
132 /*
133 * Pick up the answer for the request to the VFS_IN_REQUEST call.
134 */
135 async_wait_for(req, NULL);
136 reg->fs_handle = (int) IPC_GET_ARG1(answer);
137
138 /*
139 * Tell the async framework that other connections are to be handled by
140 * the same connection fibril as well.
141 */
142 async_set_client_connection(conn);
143
144 return IPC_GET_RETVAL(answer);
145}
146
147void fs_node_initialize(fs_node_t *fn)
148{
149 memset(fn, 0, sizeof(fs_node_t));
150}
151
152void libfs_mount(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
153 ipc_call_t *request)
154{
155 devmap_handle_t mp_devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
156 fs_index_t mp_fs_index = (fs_index_t) IPC_GET_ARG2(*request);
157 fs_handle_t mr_fs_handle = (fs_handle_t) IPC_GET_ARG3(*request);
158 devmap_handle_t mr_devmap_handle = (devmap_handle_t) IPC_GET_ARG4(*request);
159
160 async_sess_t *mountee_sess = async_clone_receive(EXCHANGE_PARALLEL);
161 if (mountee_sess == NULL) {
162 async_answer_0(rid, EINVAL);
163 return;
164 }
165
166 fs_node_t *fn;
167 int res = ops->node_get(&fn, mp_devmap_handle, mp_fs_index);
168 if ((res != EOK) || (!fn)) {
169 async_hangup(mountee_sess);
170 async_data_write_void(combine_rc(res, ENOENT));
171 async_answer_0(rid, combine_rc(res, ENOENT));
172 return;
173 }
174
175 if (fn->mp_data.mp_active) {
176 async_hangup(mountee_sess);
177 (void) ops->node_put(fn);
178 async_data_write_void(EBUSY);
179 async_answer_0(rid, EBUSY);
180 return;
181 }
182
183 async_exch_t *exch = async_exchange_begin(mountee_sess);
184 async_sess_t *sess = async_connect_me(EXCHANGE_PARALLEL, exch);
185
186 if (!sess) {
187 async_exchange_end(exch);
188 async_hangup(mountee_sess);
189 (void) ops->node_put(fn);
190 async_data_write_void(errno);
191 async_answer_0(rid, errno);
192 return;
193 }
194
195 ipc_call_t answer;
196 int rc = async_data_write_forward_1_1(exch, VFS_OUT_MOUNTED,
197 mr_devmap_handle, &answer);
198 async_exchange_end(exch);
199
200 if (rc == EOK) {
201 fn->mp_data.mp_active = true;
202 fn->mp_data.fs_handle = mr_fs_handle;
203 fn->mp_data.devmap_handle = mr_devmap_handle;
204 fn->mp_data.sess = mountee_sess;
205 }
206
207 /*
208 * Do not release the FS node so that it stays in memory.
209 */
210 async_answer_3(rid, rc, IPC_GET_ARG1(answer), IPC_GET_ARG2(answer),
211 IPC_GET_ARG3(answer));
212}
213
214void libfs_unmount(libfs_ops_t *ops, ipc_callid_t rid, ipc_call_t *request)
215{
216 devmap_handle_t mp_devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
217 fs_index_t mp_fs_index = (fs_index_t) IPC_GET_ARG2(*request);
218 fs_node_t *fn;
219 int res;
220
221 res = ops->node_get(&fn, mp_devmap_handle, mp_fs_index);
222 if ((res != EOK) || (!fn)) {
223 async_answer_0(rid, combine_rc(res, ENOENT));
224 return;
225 }
226
227 /*
228 * We are clearly expecting to find the mount point active.
229 */
230 if (!fn->mp_data.mp_active) {
231 (void) ops->node_put(fn);
232 async_answer_0(rid, EINVAL);
233 return;
234 }
235
236 /*
237 * Tell the mounted file system to unmount.
238 */
239 async_exch_t *exch = async_exchange_begin(fn->mp_data.sess);
240 res = async_req_1_0(exch, VFS_OUT_UNMOUNTED, fn->mp_data.devmap_handle);
241 async_exchange_end(exch);
242
243 /*
244 * If everything went well, perform the clean-up on our side.
245 */
246 if (res == EOK) {
247 async_hangup(fn->mp_data.sess);
248 fn->mp_data.mp_active = false;
249 fn->mp_data.fs_handle = 0;
250 fn->mp_data.devmap_handle = 0;
251 fn->mp_data.sess = NULL;
252
253 /* Drop the reference created in libfs_mount(). */
254 (void) ops->node_put(fn);
255 }
256
257 (void) ops->node_put(fn);
258 async_answer_0(rid, res);
259}
260
261/** Lookup VFS triplet by name in the file system name space.
262 *
263 * The path passed in the PLB must be in the canonical file system path format
264 * as returned by the canonify() function.
265 *
266 * @param ops libfs operations structure with function pointers to
267 * file system implementation
268 * @param fs_handle File system handle of the file system where to perform
269 * the lookup.
270 * @param rid Request ID of the VFS_OUT_LOOKUP request.
271 * @param request VFS_OUT_LOOKUP request data itself.
272 *
273 */
274void libfs_lookup(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
275 ipc_call_t *request)
276{
277 unsigned int first = IPC_GET_ARG1(*request);
278 unsigned int last = IPC_GET_ARG2(*request);
279 unsigned int next = first;
280 devmap_handle_t devmap_handle = IPC_GET_ARG3(*request);
281 int lflag = IPC_GET_ARG4(*request);
282 fs_index_t index = IPC_GET_ARG5(*request);
283 char component[NAME_MAX + 1];
284 int len;
285 int rc;
286
287 if (last < next)
288 last += PLB_SIZE;
289
290 fs_node_t *par = NULL;
291 fs_node_t *cur = NULL;
292 fs_node_t *tmp = NULL;
293
294 rc = ops->root_get(&cur, devmap_handle);
295 on_error(rc, goto out_with_answer);
296
297 if (cur->mp_data.mp_active) {
298 async_exch_t *exch = async_exchange_begin(cur->mp_data.sess);
299 async_forward_slow(rid, exch, VFS_OUT_LOOKUP, next, last,
300 cur->mp_data.devmap_handle, lflag, index, IPC_FF_ROUTE_FROM_ME);
301 async_exchange_end(exch);
302
303 (void) ops->node_put(cur);
304 return;
305 }
306
307 /* Eat slash */
308 if (ops->plb_get_char(next) == '/')
309 next++;
310
311 while (next <= last) {
312 bool has_children;
313
314 rc = ops->has_children(&has_children, cur);
315 on_error(rc, goto out_with_answer);
316 if (!has_children)
317 break;
318
319 /* Collect the component */
320 len = 0;
321 while ((next <= last) && (ops->plb_get_char(next) != '/')) {
322 if (len + 1 == NAME_MAX) {
323 /* Component length overflow */
324 async_answer_0(rid, ENAMETOOLONG);
325 goto out;
326 }
327 component[len++] = ops->plb_get_char(next);
328 /* Process next character */
329 next++;
330 }
331
332 assert(len);
333 component[len] = '\0';
334 /* Eat slash */
335 next++;
336
337 /* Match the component */
338 rc = ops->match(&tmp, cur, component);
339 on_error(rc, goto out_with_answer);
340
341 /*
342 * If the matching component is a mount point, there are two
343 * legitimate semantics of the lookup operation. The first is
344 * the commonly used one in which the lookup crosses each mount
345 * point into the mounted file system. The second semantics is
346 * used mostly during unmount() and differs from the first one
347 * only in that the last mount point in the looked up path,
348 * which is also its last component, is not crossed.
349 */
350
351 if ((tmp) && (tmp->mp_data.mp_active) &&
352 (!(lflag & L_MP) || (next <= last))) {
353 if (next > last)
354 next = last = first;
355 else
356 next--;
357
358 async_exch_t *exch = async_exchange_begin(tmp->mp_data.sess);
359 async_forward_slow(rid, exch, VFS_OUT_LOOKUP, next, last,
360 tmp->mp_data.devmap_handle, lflag, index,
361 IPC_FF_ROUTE_FROM_ME);
362 async_exchange_end(exch);
363
364 (void) ops->node_put(cur);
365 (void) ops->node_put(tmp);
366 if (par)
367 (void) ops->node_put(par);
368 return;
369 }
370
371 /* Handle miss: match amongst siblings */
372 if (!tmp) {
373 if (next <= last) {
374 /* There are unprocessed components */
375 async_answer_0(rid, ENOENT);
376 goto out;
377 }
378
379 /* Miss in the last component */
380 if (lflag & (L_CREATE | L_LINK)) {
381 /* Request to create a new link */
382 if (!ops->is_directory(cur)) {
383 async_answer_0(rid, ENOTDIR);
384 goto out;
385 }
386
387 fs_node_t *fn;
388 if (lflag & L_CREATE)
389 rc = ops->create(&fn, devmap_handle,
390 lflag);
391 else
392 rc = ops->node_get(&fn, devmap_handle,
393 index);
394 on_error(rc, goto out_with_answer);
395
396 if (fn) {
397 rc = ops->link(cur, fn, component);
398 if (rc != EOK) {
399 if (lflag & L_CREATE)
400 (void) ops->destroy(fn);
401 else
402 (void) ops->node_put(fn);
403 async_answer_0(rid, rc);
404 } else {
405 aoff64_t size = ops->size_get(fn);
406 async_answer_5(rid, fs_handle,
407 devmap_handle,
408 ops->index_get(fn),
409 LOWER32(size),
410 UPPER32(size),
411 ops->lnkcnt_get(fn));
412 (void) ops->node_put(fn);
413 }
414 } else
415 async_answer_0(rid, ENOSPC);
416
417 goto out;
418 }
419
420 async_answer_0(rid, ENOENT);
421 goto out;
422 }
423
424 if (par) {
425 rc = ops->node_put(par);
426 on_error(rc, goto out_with_answer);
427 }
428
429 /* Descend one level */
430 par = cur;
431 cur = tmp;
432 tmp = NULL;
433 }
434
435 /* Handle miss: excessive components */
436 if (next <= last) {
437 bool has_children;
438 rc = ops->has_children(&has_children, cur);
439 on_error(rc, goto out_with_answer);
440
441 if (has_children)
442 goto skip_miss;
443
444 if (lflag & (L_CREATE | L_LINK)) {
445 if (!ops->is_directory(cur)) {
446 async_answer_0(rid, ENOTDIR);
447 goto out;
448 }
449
450 /* Collect next component */
451 len = 0;
452 while (next <= last) {
453 if (ops->plb_get_char(next) == '/') {
454 /* More than one component */
455 async_answer_0(rid, ENOENT);
456 goto out;
457 }
458
459 if (len + 1 == NAME_MAX) {
460 /* Component length overflow */
461 async_answer_0(rid, ENAMETOOLONG);
462 goto out;
463 }
464
465 component[len++] = ops->plb_get_char(next);
466 /* Process next character */
467 next++;
468 }
469
470 assert(len);
471 component[len] = '\0';
472
473 fs_node_t *fn;
474 if (lflag & L_CREATE)
475 rc = ops->create(&fn, devmap_handle, lflag);
476 else
477 rc = ops->node_get(&fn, devmap_handle, index);
478 on_error(rc, goto out_with_answer);
479
480 if (fn) {
481 rc = ops->link(cur, fn, component);
482 if (rc != EOK) {
483 if (lflag & L_CREATE)
484 (void) ops->destroy(fn);
485 else
486 (void) ops->node_put(fn);
487 async_answer_0(rid, rc);
488 } else {
489 aoff64_t size = ops->size_get(fn);
490 async_answer_5(rid, fs_handle,
491 devmap_handle,
492 ops->index_get(fn),
493 LOWER32(size),
494 UPPER32(size),
495 ops->lnkcnt_get(fn));
496 (void) ops->node_put(fn);
497 }
498 } else
499 async_answer_0(rid, ENOSPC);
500
501 goto out;
502 }
503
504 async_answer_0(rid, ENOENT);
505 goto out;
506 }
507
508skip_miss:
509
510 /* Handle hit */
511 if (lflag & L_UNLINK) {
512 unsigned int old_lnkcnt = ops->lnkcnt_get(cur);
513 rc = ops->unlink(par, cur, component);
514
515 if (rc == EOK) {
516 aoff64_t size = ops->size_get(cur);
517 async_answer_5(rid, fs_handle, devmap_handle,
518 ops->index_get(cur), LOWER32(size), UPPER32(size),
519 old_lnkcnt);
520 } else
521 async_answer_0(rid, rc);
522
523 goto out;
524 }
525
526 if (((lflag & (L_CREATE | L_EXCLUSIVE)) == (L_CREATE | L_EXCLUSIVE)) ||
527 (lflag & L_LINK)) {
528 async_answer_0(rid, EEXIST);
529 goto out;
530 }
531
532 if ((lflag & L_FILE) && (ops->is_directory(cur))) {
533 async_answer_0(rid, EISDIR);
534 goto out;
535 }
536
537 if ((lflag & L_DIRECTORY) && (ops->is_file(cur))) {
538 async_answer_0(rid, ENOTDIR);
539 goto out;
540 }
541
542 if ((lflag & L_ROOT) && par) {
543 async_answer_0(rid, EINVAL);
544 goto out;
545 }
546
547out_with_answer:
548
549 if (rc == EOK) {
550 if (lflag & L_OPEN)
551 rc = ops->node_open(cur);
552
553 if (rc == EOK) {
554 aoff64_t size = ops->size_get(cur);
555 async_answer_5(rid, fs_handle, devmap_handle,
556 ops->index_get(cur), LOWER32(size), UPPER32(size),
557 ops->lnkcnt_get(cur));
558 } else
559 async_answer_0(rid, rc);
560
561 } else
562 async_answer_0(rid, rc);
563
564out:
565
566 if (par)
567 (void) ops->node_put(par);
568
569 if (cur)
570 (void) ops->node_put(cur);
571
572 if (tmp)
573 (void) ops->node_put(tmp);
574}
575
576void libfs_stat(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
577 ipc_call_t *request)
578{
579 devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
580 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
581
582 fs_node_t *fn;
583 int rc = ops->node_get(&fn, devmap_handle, index);
584 on_error(rc, answer_and_return(rid, rc));
585
586 ipc_callid_t callid;
587 size_t size;
588 if ((!async_data_read_receive(&callid, &size)) ||
589 (size != sizeof(struct stat))) {
590 ops->node_put(fn);
591 async_answer_0(callid, EINVAL);
592 async_answer_0(rid, EINVAL);
593 return;
594 }
595
596 struct stat stat;
597 memset(&stat, 0, sizeof(struct stat));
598
599 stat.fs_handle = fs_handle;
600 stat.devmap_handle = devmap_handle;
601 stat.index = index;
602 stat.lnkcnt = ops->lnkcnt_get(fn);
603 stat.is_file = ops->is_file(fn);
604 stat.is_directory = ops->is_directory(fn);
605 stat.size = ops->size_get(fn);
606 stat.device = ops->device_get(fn);
607
608 ops->node_put(fn);
609
610 async_data_read_finalize(callid, &stat, sizeof(struct stat));
611 async_answer_0(rid, EOK);
612}
613
614/** Open VFS triplet.
615 *
616 * @param ops libfs operations structure with function pointers to
617 * file system implementation
618 * @param rid Request ID of the VFS_OUT_OPEN_NODE request.
619 * @param request VFS_OUT_OPEN_NODE request data itself.
620 *
621 */
622void libfs_open_node(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
623 ipc_call_t *request)
624{
625 devmap_handle_t devmap_handle = IPC_GET_ARG1(*request);
626 fs_index_t index = IPC_GET_ARG2(*request);
627
628 fs_node_t *fn;
629 int rc = ops->node_get(&fn, devmap_handle, index);
630 on_error(rc, answer_and_return(rid, rc));
631
632 if (fn == NULL) {
633 async_answer_0(rid, ENOENT);
634 return;
635 }
636
637 rc = ops->node_open(fn);
638 aoff64_t size = ops->size_get(fn);
639 async_answer_4(rid, rc, LOWER32(size), UPPER32(size), ops->lnkcnt_get(fn),
640 (ops->is_file(fn) ? L_FILE : 0) | (ops->is_directory(fn) ? L_DIRECTORY : 0));
641
642 (void) ops->node_put(fn);
643}
644
645/** @}
646 */
Note: See TracBrowser for help on using the repository browser.