source: mainline/uspace/lib/libfs/libfs.c@ e27cf669

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

improve naming conventions:
merge async_data_receive() and async_string_receive() into async_data_write_accept()
rename async_data_void() to async_data_write_void()
rename async_data_forward_fast() to async_data_write_forward_fast()
rename async_data_forward_n_m to async_data_write_forward_n_m

  • Property mode set to 100644
File size: 15.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 <errno.h>
40#include <async.h>
41#include <ipc/ipc.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 ipc_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 vfs_phone Open phone 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(int vfs_phone, 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 ipc_call_t answer;
89 aid_t req = async_send_0(vfs_phone, VFS_IN_REGISTER, &answer);
90
91 /*
92 * Send our VFS info structure to VFS.
93 */
94 int rc = async_data_write_start(vfs_phone, info, sizeof(*info));
95 if (rc != EOK) {
96 async_wait_for(req, NULL);
97 return rc;
98 }
99
100 /*
101 * Ask VFS for callback connection.
102 */
103 ipc_connect_to_me(vfs_phone, 0, 0, 0, &reg->vfs_phonehash);
104
105 /*
106 * Allocate piece of address space for PLB.
107 */
108 reg->plb_ro = as_get_mappable_page(PLB_SIZE);
109 if (!reg->plb_ro) {
110 async_wait_for(req, NULL);
111 return ENOMEM;
112 }
113
114 /*
115 * Request sharing the Path Lookup Buffer with VFS.
116 */
117 rc = async_share_in_start_0_0(vfs_phone, reg->plb_ro, PLB_SIZE);
118 if (rc) {
119 async_wait_for(req, NULL);
120 return rc;
121 }
122
123 /*
124 * Pick up the answer for the request to the VFS_IN_REQUEST call.
125 */
126 async_wait_for(req, NULL);
127 reg->fs_handle = (int) IPC_GET_ARG1(answer);
128
129 /*
130 * Create a connection fibril to handle the callback connection.
131 */
132 async_new_connection(reg->vfs_phonehash, 0, NULL, conn);
133
134 /*
135 * Tell the async framework that other connections are to be handled by
136 * the same connection fibril as well.
137 */
138 async_set_client_connection(conn);
139
140 return IPC_GET_RETVAL(answer);
141}
142
143void fs_node_initialize(fs_node_t *fn)
144{
145 memset(fn, 0, sizeof(fs_node_t));
146}
147
148void libfs_mount(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
149 ipc_call_t *request)
150{
151 dev_handle_t mp_dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
152 fs_index_t mp_fs_index = (fs_index_t) IPC_GET_ARG2(*request);
153 fs_handle_t mr_fs_handle = (fs_handle_t) IPC_GET_ARG3(*request);
154 dev_handle_t mr_dev_handle = (dev_handle_t) IPC_GET_ARG4(*request);
155 int res;
156 ipcarg_t rc;
157
158 ipc_call_t call;
159 ipc_callid_t callid;
160
161 /* Accept the phone */
162 callid = async_get_call(&call);
163 int mountee_phone = (int) IPC_GET_ARG1(call);
164 if ((IPC_GET_METHOD(call) != IPC_M_CONNECTION_CLONE) ||
165 (mountee_phone < 0)) {
166 ipc_answer_0(callid, EINVAL);
167 ipc_answer_0(rid, EINVAL);
168 return;
169 }
170
171 /* Acknowledge the mountee_phone */
172 ipc_answer_0(callid, EOK);
173
174 fs_node_t *fn;
175 res = ops->node_get(&fn, mp_dev_handle, mp_fs_index);
176 if ((res != EOK) || (!fn)) {
177 ipc_hangup(mountee_phone);
178 async_data_write_void(combine_rc(res, ENOENT));
179 ipc_answer_0(rid, combine_rc(res, ENOENT));
180 return;
181 }
182
183 if (fn->mp_data.mp_active) {
184 ipc_hangup(mountee_phone);
185 (void) ops->node_put(fn);
186 async_data_write_void(EBUSY);
187 ipc_answer_0(rid, EBUSY);
188 return;
189 }
190
191 rc = async_req_0_0(mountee_phone, IPC_M_CONNECT_ME);
192 if (rc != EOK) {
193 ipc_hangup(mountee_phone);
194 (void) ops->node_put(fn);
195 async_data_write_void(rc);
196 ipc_answer_0(rid, rc);
197 return;
198 }
199
200 ipc_call_t answer;
201 rc = async_data_write_forward_1_1(mountee_phone, VFS_OUT_MOUNTED,
202 mr_dev_handle, &answer);
203
204 if (rc == EOK) {
205 fn->mp_data.mp_active = true;
206 fn->mp_data.fs_handle = mr_fs_handle;
207 fn->mp_data.dev_handle = mr_dev_handle;
208 fn->mp_data.phone = mountee_phone;
209 }
210
211 /*
212 * Do not release the FS node so that it stays in memory.
213 */
214 ipc_answer_3(rid, rc, IPC_GET_ARG1(answer), IPC_GET_ARG2(answer),
215 IPC_GET_ARG3(answer));
216}
217
218void libfs_unmount(libfs_ops_t *ops, ipc_callid_t rid, ipc_call_t *request)
219{
220 dev_handle_t mp_dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
221 fs_index_t mp_fs_index = (fs_index_t) IPC_GET_ARG2(*request);
222 fs_node_t *fn;
223 int res;
224
225 res = ops->node_get(&fn, mp_dev_handle, mp_fs_index);
226 if ((res != EOK) || (!fn)) {
227 ipc_answer_0(rid, combine_rc(res, ENOENT));
228 return;
229 }
230
231 /*
232 * We are clearly expecting to find the mount point active.
233 */
234 if (!fn->mp_data.mp_active) {
235 (void) ops->node_put(fn);
236 ipc_answer_0(rid, EINVAL);
237 return;
238 }
239
240 /*
241 * Tell the mounted file system to unmount.
242 */
243 res = async_req_1_0(fn->mp_data.phone, VFS_OUT_UNMOUNTED,
244 fn->mp_data.dev_handle);
245
246 /*
247 * If everything went well, perform the clean-up on our side.
248 */
249 if (res == EOK) {
250 ipc_hangup(fn->mp_data.phone);
251 fn->mp_data.mp_active = false;
252 fn->mp_data.fs_handle = 0;
253 fn->mp_data.dev_handle = 0;
254 fn->mp_data.phone = 0;
255 /* Drop the reference created in libfs_mount(). */
256 (void) ops->node_put(fn);
257 }
258
259 (void) ops->node_put(fn);
260 ipc_answer_0(rid, res);
261}
262
263/** Lookup VFS triplet by name in the file system name space.
264 *
265 * The path passed in the PLB must be in the canonical file system path format
266 * as returned by the canonify() function.
267 *
268 * @param ops libfs operations structure with function pointers to
269 * file system implementation
270 * @param fs_handle File system handle of the file system where to perform
271 * the lookup.
272 * @param rid Request ID of the VFS_OUT_LOOKUP request.
273 * @param request VFS_OUT_LOOKUP request data itself.
274 *
275 */
276void libfs_lookup(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
277 ipc_call_t *request)
278{
279 unsigned int first = IPC_GET_ARG1(*request);
280 unsigned int last = IPC_GET_ARG2(*request);
281 unsigned int next = first;
282 dev_handle_t dev_handle = IPC_GET_ARG3(*request);
283 int lflag = IPC_GET_ARG4(*request);
284 fs_index_t index = IPC_GET_ARG5(*request);
285 char component[NAME_MAX + 1];
286 int len;
287 int rc;
288
289 if (last < next)
290 last += PLB_SIZE;
291
292 fs_node_t *par = NULL;
293 fs_node_t *cur = NULL;
294 fs_node_t *tmp = NULL;
295
296 rc = ops->root_get(&cur, dev_handle);
297 on_error(rc, goto out_with_answer);
298
299 if (cur->mp_data.mp_active) {
300 ipc_forward_slow(rid, cur->mp_data.phone, VFS_OUT_LOOKUP,
301 next, last, cur->mp_data.dev_handle, lflag, index,
302 IPC_FF_ROUTE_FROM_ME);
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 ipc_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 ipc_forward_slow(rid, tmp->mp_data.phone,
359 VFS_OUT_LOOKUP, next, last, tmp->mp_data.dev_handle,
360 lflag, index, IPC_FF_ROUTE_FROM_ME);
361 (void) ops->node_put(cur);
362 (void) ops->node_put(tmp);
363 if (par)
364 (void) ops->node_put(par);
365 return;
366 }
367
368 /* Handle miss: match amongst siblings */
369 if (!tmp) {
370 if (next <= last) {
371 /* There are unprocessed components */
372 ipc_answer_0(rid, ENOENT);
373 goto out;
374 }
375
376 /* Miss in the last component */
377 if (lflag & (L_CREATE | L_LINK)) {
378 /* Request to create a new link */
379 if (!ops->is_directory(cur)) {
380 ipc_answer_0(rid, ENOTDIR);
381 goto out;
382 }
383
384 fs_node_t *fn;
385 if (lflag & L_CREATE)
386 rc = ops->create(&fn, dev_handle,
387 lflag);
388 else
389 rc = ops->node_get(&fn, dev_handle,
390 index);
391 on_error(rc, goto out_with_answer);
392
393 if (fn) {
394 rc = ops->link(cur, fn, component);
395 if (rc != EOK) {
396 if (lflag & L_CREATE)
397 (void) ops->destroy(fn);
398 ipc_answer_0(rid, rc);
399 } else {
400 ipc_answer_5(rid, EOK,
401 fs_handle, dev_handle,
402 ops->index_get(fn),
403 ops->size_get(fn),
404 ops->lnkcnt_get(fn));
405 (void) ops->node_put(fn);
406 }
407 } else
408 ipc_answer_0(rid, ENOSPC);
409
410 goto out;
411 }
412
413 ipc_answer_0(rid, ENOENT);
414 goto out;
415 }
416
417 if (par) {
418 rc = ops->node_put(par);
419 on_error(rc, goto out_with_answer);
420 }
421
422 /* Descend one level */
423 par = cur;
424 cur = tmp;
425 tmp = NULL;
426 }
427
428 /* Handle miss: excessive components */
429 if (next <= last) {
430 bool has_children;
431 rc = ops->has_children(&has_children, cur);
432 on_error(rc, goto out_with_answer);
433
434 if (has_children)
435 goto skip_miss;
436
437 if (lflag & (L_CREATE | L_LINK)) {
438 if (!ops->is_directory(cur)) {
439 ipc_answer_0(rid, ENOTDIR);
440 goto out;
441 }
442
443 /* Collect next component */
444 len = 0;
445 while (next <= last) {
446 if (ops->plb_get_char(next) == '/') {
447 /* More than one component */
448 ipc_answer_0(rid, ENOENT);
449 goto out;
450 }
451
452 if (len + 1 == NAME_MAX) {
453 /* Component length overflow */
454 ipc_answer_0(rid, ENAMETOOLONG);
455 goto out;
456 }
457
458 component[len++] = ops->plb_get_char(next);
459 /* Process next character */
460 next++;
461 }
462
463 assert(len);
464 component[len] = '\0';
465
466 fs_node_t *fn;
467 if (lflag & L_CREATE)
468 rc = ops->create(&fn, dev_handle, lflag);
469 else
470 rc = ops->node_get(&fn, dev_handle, index);
471 on_error(rc, goto out_with_answer);
472
473 if (fn) {
474 rc = ops->link(cur, fn, component);
475 if (rc != EOK) {
476 if (lflag & L_CREATE)
477 (void) ops->destroy(fn);
478 ipc_answer_0(rid, rc);
479 } else {
480 ipc_answer_5(rid, EOK,
481 fs_handle, dev_handle,
482 ops->index_get(fn),
483 ops->size_get(fn),
484 ops->lnkcnt_get(fn));
485 (void) ops->node_put(fn);
486 }
487 } else
488 ipc_answer_0(rid, ENOSPC);
489
490 goto out;
491 }
492
493 ipc_answer_0(rid, ENOENT);
494 goto out;
495 }
496
497skip_miss:
498
499 /* Handle hit */
500 if (lflag & L_UNLINK) {
501 unsigned int old_lnkcnt = ops->lnkcnt_get(cur);
502 rc = ops->unlink(par, cur, component);
503 ipc_answer_5(rid, (ipcarg_t) rc, fs_handle, dev_handle,
504 ops->index_get(cur), ops->size_get(cur), old_lnkcnt);
505 goto out;
506 }
507
508 if (((lflag & (L_CREATE | L_EXCLUSIVE)) == (L_CREATE | L_EXCLUSIVE)) ||
509 (lflag & L_LINK)) {
510 ipc_answer_0(rid, EEXIST);
511 goto out;
512 }
513
514 if ((lflag & L_FILE) && (ops->is_directory(cur))) {
515 ipc_answer_0(rid, EISDIR);
516 goto out;
517 }
518
519 if ((lflag & L_DIRECTORY) && (ops->is_file(cur))) {
520 ipc_answer_0(rid, ENOTDIR);
521 goto out;
522 }
523
524 if ((lflag & L_ROOT) && par) {
525 ipc_answer_0(rid, EINVAL);
526 goto out;
527 }
528
529out_with_answer:
530
531 if (rc == EOK) {
532 if (lflag & L_OPEN)
533 rc = ops->node_open(cur);
534
535 ipc_answer_5(rid, rc, fs_handle, dev_handle,
536 ops->index_get(cur), ops->size_get(cur),
537 ops->lnkcnt_get(cur));
538 } else
539 ipc_answer_0(rid, rc);
540
541out:
542
543 if (par)
544 (void) ops->node_put(par);
545
546 if (cur)
547 (void) ops->node_put(cur);
548
549 if (tmp)
550 (void) ops->node_put(tmp);
551}
552
553void libfs_stat(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
554 ipc_call_t *request)
555{
556 dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
557 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
558
559 fs_node_t *fn;
560 int rc = ops->node_get(&fn, dev_handle, index);
561 on_error(rc, answer_and_return(rid, rc));
562
563 ipc_callid_t callid;
564 size_t size;
565 if ((!async_data_read_receive(&callid, &size)) ||
566 (size != sizeof(struct stat))) {
567 ops->node_put(fn);
568 ipc_answer_0(callid, EINVAL);
569 ipc_answer_0(rid, EINVAL);
570 return;
571 }
572
573 struct stat stat;
574 memset(&stat, 0, sizeof(struct stat));
575
576 stat.fs_handle = fs_handle;
577 stat.dev_handle = dev_handle;
578 stat.index = index;
579 stat.lnkcnt = ops->lnkcnt_get(fn);
580 stat.is_file = ops->is_file(fn);
581 stat.is_directory = ops->is_directory(fn);
582 stat.size = ops->size_get(fn);
583 stat.device = ops->device_get(fn);
584
585 ops->node_put(fn);
586
587 async_data_read_finalize(callid, &stat, sizeof(struct stat));
588 ipc_answer_0(rid, EOK);
589}
590
591/** Open VFS triplet.
592 *
593 * @param ops libfs operations structure with function pointers to
594 * file system implementation
595 * @param rid Request ID of the VFS_OUT_OPEN_NODE request.
596 * @param request VFS_OUT_OPEN_NODE request data itself.
597 *
598 */
599void libfs_open_node(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
600 ipc_call_t *request)
601{
602 dev_handle_t dev_handle = IPC_GET_ARG1(*request);
603 fs_index_t index = IPC_GET_ARG2(*request);
604 fs_node_t *fn;
605 int rc;
606
607 rc = ops->node_get(&fn, dev_handle, index);
608 on_error(rc, answer_and_return(rid, rc));
609
610 if (fn == NULL) {
611 ipc_answer_0(rid, ENOENT);
612 return;
613 }
614
615 rc = ops->node_open(fn);
616 ipc_answer_3(rid, rc, ops->size_get(fn), ops->lnkcnt_get(fn),
617 (ops->is_file(fn) ? L_FILE : 0) | (ops->is_directory(fn) ? L_DIRECTORY : 0));
618
619 (void) ops->node_put(fn);
620}
621
622/** @}
623 */
Note: See TracBrowser for help on using the repository browser.