source: mainline/uspace/lib/fs/libfs.c@ 70452dd4

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

Put the libfs node upon its failure to link.

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