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

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

Finish implementation of libfs_unmount().

  • Property mode set to 100644
File size: 15.7 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 res = async_data_write_receive(&callid, NULL);
175 if (!res) {
176 ipc_hangup(mountee_phone);
177 ipc_answer_0(callid, EINVAL);
178 ipc_answer_0(rid, EINVAL);
179 return;
180 }
181
182 fs_node_t *fn;
183 res = ops->node_get(&fn, mp_dev_handle, mp_fs_index);
184 if ((res != EOK) || (!fn)) {
185 ipc_hangup(mountee_phone);
186 ipc_answer_0(callid, combine_rc(res, ENOENT));
187 ipc_answer_0(rid, combine_rc(res, ENOENT));
188 return;
189 }
190
191 if (fn->mp_data.mp_active) {
192 ipc_hangup(mountee_phone);
193 (void) ops->node_put(fn);
194 ipc_answer_0(callid, EBUSY);
195 ipc_answer_0(rid, EBUSY);
196 return;
197 }
198
199 rc = async_req_0_0(mountee_phone, IPC_M_CONNECT_ME);
200 if (rc != EOK) {
201 ipc_hangup(mountee_phone);
202 (void) ops->node_put(fn);
203 ipc_answer_0(callid, rc);
204 ipc_answer_0(rid, rc);
205 return;
206 }
207
208 ipc_call_t answer;
209 aid_t msg = async_send_1(mountee_phone, VFS_OUT_MOUNTED, mr_dev_handle,
210 &answer);
211 ipc_forward_fast(callid, mountee_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
212 async_wait_for(msg, &rc);
213
214 if (rc == EOK) {
215 fn->mp_data.mp_active = true;
216 fn->mp_data.fs_handle = mr_fs_handle;
217 fn->mp_data.dev_handle = mr_dev_handle;
218 fn->mp_data.phone = mountee_phone;
219 }
220
221 /*
222 * Do not release the FS node so that it stays in memory.
223 */
224 ipc_answer_3(rid, rc, IPC_GET_ARG1(answer), IPC_GET_ARG2(answer),
225 IPC_GET_ARG3(answer));
226}
227
228void libfs_unmount(libfs_ops_t *ops, ipc_callid_t rid, ipc_call_t *request)
229{
230 dev_handle_t mp_dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
231 fs_index_t mp_fs_index = (fs_index_t) IPC_GET_ARG2(*request);
232 fs_node_t *fn;
233 int res;
234
235 res = ops->node_get(&fn, mp_dev_handle, mp_fs_index);
236 if ((res != EOK) || (!fn)) {
237 ipc_answer_0(rid, combine_rc(res, ENOENT));
238 return;
239 }
240
241 /*
242 * We are clearly expecting to find the mount point active.
243 */
244 if (!fn->mp_data.mp_active) {
245 (void) ops->node_put(fn);
246 ipc_answer_0(rid, EINVAL);
247 return;
248 }
249
250 /*
251 * Tell the mounted file system to unmount.
252 */
253 res = async_req_1_0(fn->mp_data.phone, VFS_OUT_UNMOUNTED,
254 fn->mp_data.dev_handle);
255
256 /*
257 * If everything went well, perform the clean-up on our side.
258 */
259 if (res == EOK) {
260 ipc_hangup(fn->mp_data.phone);
261 fn->mp_data.mp_active = false;
262 fn->mp_data.fs_handle = 0;
263 fn->mp_data.dev_handle = 0;
264 fn->mp_data.phone = 0;
265 /* Drop the reference created in libfs_mount(). */
266 (void) ops->node_put(fn);
267 }
268
269 (void) ops->node_put(fn);
270 ipc_answer_0(rid, res);
271}
272
273/** Lookup VFS triplet by name in the file system name space.
274 *
275 * The path passed in the PLB must be in the canonical file system path format
276 * as returned by the canonify() function.
277 *
278 * @param ops libfs operations structure with function pointers to
279 * file system implementation
280 * @param fs_handle File system handle of the file system where to perform
281 * the lookup.
282 * @param rid Request ID of the VFS_OUT_LOOKUP request.
283 * @param request VFS_OUT_LOOKUP request data itself.
284 *
285 */
286void libfs_lookup(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
287 ipc_call_t *request)
288{
289 unsigned int first = IPC_GET_ARG1(*request);
290 unsigned int last = IPC_GET_ARG2(*request);
291 unsigned int next = first;
292 dev_handle_t dev_handle = IPC_GET_ARG3(*request);
293 int lflag = IPC_GET_ARG4(*request);
294 fs_index_t index = IPC_GET_ARG5(*request);
295 char component[NAME_MAX + 1];
296 int len;
297 int rc;
298
299 if (last < next)
300 last += PLB_SIZE;
301
302 fs_node_t *par = NULL;
303 fs_node_t *cur = NULL;
304 fs_node_t *tmp = NULL;
305
306 rc = ops->root_get(&cur, dev_handle);
307 on_error(rc, goto out_with_answer);
308
309 if (cur->mp_data.mp_active) {
310 ipc_forward_slow(rid, cur->mp_data.phone, VFS_OUT_LOOKUP,
311 next, last, cur->mp_data.dev_handle, lflag, index,
312 IPC_FF_ROUTE_FROM_ME);
313 (void) ops->node_put(cur);
314 return;
315 }
316
317 /* Eat slash */
318 if (ops->plb_get_char(next) == '/')
319 next++;
320
321 while (next <= last) {
322 bool has_children;
323
324 rc = ops->has_children(&has_children, cur);
325 on_error(rc, goto out_with_answer);
326 if (!has_children)
327 break;
328
329 /* Collect the component */
330 len = 0;
331 while ((next <= last) && (ops->plb_get_char(next) != '/')) {
332 if (len + 1 == NAME_MAX) {
333 /* Component length overflow */
334 ipc_answer_0(rid, ENAMETOOLONG);
335 goto out;
336 }
337 component[len++] = ops->plb_get_char(next);
338 /* Process next character */
339 next++;
340 }
341
342 assert(len);
343 component[len] = '\0';
344 /* Eat slash */
345 next++;
346
347 /* Match the component */
348 rc = ops->match(&tmp, cur, component);
349 on_error(rc, goto out_with_answer);
350
351 /*
352 * If the matching component is a mount point, there are two
353 * legitimate semantics of the lookup operation. The first is
354 * the commonly used one in which the lookup crosses each mount
355 * point into the mounted file system. The second semantics is
356 * used mostly during unmount() and differs from the first one
357 * only in that the last mount point in the looked up path,
358 * which is also its last component, is not crossed.
359 */
360
361 if ((tmp) && (tmp->mp_data.mp_active) &&
362 (!(lflag & L_MP) || (next <= last))) {
363 if (next > last)
364 next = last = first;
365 else
366 next--;
367
368 ipc_forward_slow(rid, tmp->mp_data.phone,
369 VFS_OUT_LOOKUP, next, last, tmp->mp_data.dev_handle,
370 lflag, index, IPC_FF_ROUTE_FROM_ME);
371 (void) ops->node_put(cur);
372 (void) ops->node_put(tmp);
373 if (par)
374 (void) ops->node_put(par);
375 return;
376 }
377
378 /* Handle miss: match amongst siblings */
379 if (!tmp) {
380 if (next <= last) {
381 /* There are unprocessed components */
382 ipc_answer_0(rid, ENOENT);
383 goto out;
384 }
385
386 /* Miss in the last component */
387 if (lflag & (L_CREATE | L_LINK)) {
388 /* Request to create a new link */
389 if (!ops->is_directory(cur)) {
390 ipc_answer_0(rid, ENOTDIR);
391 goto out;
392 }
393
394 fs_node_t *fn;
395 if (lflag & L_CREATE)
396 rc = ops->create(&fn, dev_handle,
397 lflag);
398 else
399 rc = ops->node_get(&fn, dev_handle,
400 index);
401 on_error(rc, goto out_with_answer);
402
403 if (fn) {
404 rc = ops->link(cur, fn, component);
405 if (rc != EOK) {
406 if (lflag & L_CREATE)
407 (void) ops->destroy(fn);
408 ipc_answer_0(rid, rc);
409 } else {
410 ipc_answer_5(rid, EOK,
411 fs_handle, dev_handle,
412 ops->index_get(fn),
413 ops->size_get(fn),
414 ops->lnkcnt_get(fn));
415 (void) ops->node_put(fn);
416 }
417 } else
418 ipc_answer_0(rid, ENOSPC);
419
420 goto out;
421 }
422
423 ipc_answer_0(rid, ENOENT);
424 goto out;
425 }
426
427 if (par) {
428 rc = ops->node_put(par);
429 on_error(rc, goto out_with_answer);
430 }
431
432 /* Descend one level */
433 par = cur;
434 cur = tmp;
435 tmp = NULL;
436 }
437
438 /* Handle miss: excessive components */
439 if (next <= last) {
440 bool has_children;
441 rc = ops->has_children(&has_children, cur);
442 on_error(rc, goto out_with_answer);
443
444 if (has_children)
445 goto skip_miss;
446
447 if (lflag & (L_CREATE | L_LINK)) {
448 if (!ops->is_directory(cur)) {
449 ipc_answer_0(rid, ENOTDIR);
450 goto out;
451 }
452
453 /* Collect next component */
454 len = 0;
455 while (next <= last) {
456 if (ops->plb_get_char(next) == '/') {
457 /* More than one component */
458 ipc_answer_0(rid, ENOENT);
459 goto out;
460 }
461
462 if (len + 1 == NAME_MAX) {
463 /* Component length overflow */
464 ipc_answer_0(rid, ENAMETOOLONG);
465 goto out;
466 }
467
468 component[len++] = ops->plb_get_char(next);
469 /* Process next character */
470 next++;
471 }
472
473 assert(len);
474 component[len] = '\0';
475
476 fs_node_t *fn;
477 if (lflag & L_CREATE)
478 rc = ops->create(&fn, dev_handle, lflag);
479 else
480 rc = ops->node_get(&fn, dev_handle, index);
481 on_error(rc, goto out_with_answer);
482
483 if (fn) {
484 rc = ops->link(cur, fn, component);
485 if (rc != EOK) {
486 if (lflag & L_CREATE)
487 (void) ops->destroy(fn);
488 ipc_answer_0(rid, rc);
489 } else {
490 ipc_answer_5(rid, EOK,
491 fs_handle, dev_handle,
492 ops->index_get(fn),
493 ops->size_get(fn),
494 ops->lnkcnt_get(fn));
495 (void) ops->node_put(fn);
496 }
497 } else
498 ipc_answer_0(rid, ENOSPC);
499
500 goto out;
501 }
502
503 ipc_answer_0(rid, ENOENT);
504 goto out;
505 }
506
507skip_miss:
508
509 /* Handle hit */
510 if (lflag & L_UNLINK) {
511 unsigned int old_lnkcnt = ops->lnkcnt_get(cur);
512 rc = ops->unlink(par, cur, component);
513 ipc_answer_5(rid, (ipcarg_t) rc, fs_handle, dev_handle,
514 ops->index_get(cur), ops->size_get(cur), old_lnkcnt);
515 goto out;
516 }
517
518 if (((lflag & (L_CREATE | L_EXCLUSIVE)) == (L_CREATE | L_EXCLUSIVE)) ||
519 (lflag & L_LINK)) {
520 ipc_answer_0(rid, EEXIST);
521 goto out;
522 }
523
524 if ((lflag & L_FILE) && (ops->is_directory(cur))) {
525 ipc_answer_0(rid, EISDIR);
526 goto out;
527 }
528
529 if ((lflag & L_DIRECTORY) && (ops->is_file(cur))) {
530 ipc_answer_0(rid, ENOTDIR);
531 goto out;
532 }
533
534 if ((lflag & L_ROOT) && par) {
535 ipc_answer_0(rid, EINVAL);
536 goto out;
537 }
538
539out_with_answer:
540
541 if (rc == EOK) {
542 if (lflag & L_OPEN)
543 rc = ops->node_open(cur);
544
545 ipc_answer_5(rid, rc, fs_handle, dev_handle,
546 ops->index_get(cur), ops->size_get(cur),
547 ops->lnkcnt_get(cur));
548 } else
549 ipc_answer_0(rid, rc);
550
551out:
552
553 if (par)
554 (void) ops->node_put(par);
555
556 if (cur)
557 (void) ops->node_put(cur);
558
559 if (tmp)
560 (void) ops->node_put(tmp);
561}
562
563void libfs_stat(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
564 ipc_call_t *request)
565{
566 dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
567 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
568
569 fs_node_t *fn;
570 int rc = ops->node_get(&fn, dev_handle, index);
571 on_error(rc, answer_and_return(rid, rc));
572
573 ipc_callid_t callid;
574 size_t size;
575 if ((!async_data_read_receive(&callid, &size)) ||
576 (size != sizeof(struct stat))) {
577 ops->node_put(fn);
578 ipc_answer_0(callid, EINVAL);
579 ipc_answer_0(rid, EINVAL);
580 return;
581 }
582
583 struct stat stat;
584 memset(&stat, 0, sizeof(struct stat));
585
586 stat.fs_handle = fs_handle;
587 stat.dev_handle = dev_handle;
588 stat.index = index;
589 stat.lnkcnt = ops->lnkcnt_get(fn);
590 stat.is_file = ops->is_file(fn);
591 stat.is_directory = ops->is_directory(fn);
592 stat.size = ops->size_get(fn);
593 stat.device = ops->device_get(fn);
594
595 ops->node_put(fn);
596
597 async_data_read_finalize(callid, &stat, sizeof(struct stat));
598 ipc_answer_0(rid, EOK);
599}
600
601/** Open VFS triplet.
602 *
603 * @param ops libfs operations structure with function pointers to
604 * file system implementation
605 * @param rid Request ID of the VFS_OUT_OPEN_NODE request.
606 * @param request VFS_OUT_OPEN_NODE request data itself.
607 *
608 */
609void libfs_open_node(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
610 ipc_call_t *request)
611{
612 dev_handle_t dev_handle = IPC_GET_ARG1(*request);
613 fs_index_t index = IPC_GET_ARG2(*request);
614 fs_node_t *fn;
615 int rc;
616
617 rc = ops->node_get(&fn, dev_handle, index);
618 on_error(rc, answer_and_return(rid, rc));
619
620 if (fn == NULL) {
621 ipc_answer_0(rid, ENOENT);
622 return;
623 }
624
625 rc = ops->node_open(fn);
626 ipc_answer_3(rid, rc, ops->size_get(fn), ops->lnkcnt_get(fn),
627 (ops->is_file(fn) ? L_FILE : 0) | (ops->is_directory(fn) ? L_DIRECTORY : 0));
628
629 (void) ops->node_put(fn);
630}
631
632/** @}
633 */
Note: See TracBrowser for help on using the repository browser.