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

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

Rename dev_handle_t to devmap_handle_t and make it explicitly clear that
dev_handle_t is a handle understood by devmap.

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