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

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

set correct IPC return values in libfs_open_node()

  • Property mode set to 100644
File size: 13.8 KB
RevLine 
[74303b6]1/*
[83937ccd]2 * Copyright (c) 2009 Jakub Jermar
[74303b6]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 commonod to all FS implementations.
35 */
36
37#include "libfs.h"
[efd4a72]38#include "../../srv/vfs/vfs.h"
39#include <errno.h>
40#include <async.h>
41#include <ipc/ipc.h>
42#include <as.h>
[2c448fb]43#include <assert.h>
44#include <dirent.h>
[595edf5]45#include <mem.h>
[75160a6]46#include <sys/stat.h>
[efd4a72]47
[0daba212]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
[efd4a72]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 */
79int fs_register(int vfs_phone, fs_reg_t *reg, vfs_info_t *info,
80 async_client_conn_t conn)
81{
82 /*
83 * Tell VFS that we are here and want to get registered.
84 * We use the async framework because VFS will answer the request
85 * out-of-order, when it knows that the operation succeeded or failed.
86 */
87 ipc_call_t answer;
[4198f9c3]88 aid_t req = async_send_0(vfs_phone, VFS_IN_REGISTER, &answer);
[efd4a72]89
90 /*
91 * Send our VFS info structure to VFS.
92 */
[0da4e41]93 int rc = async_data_write_start(vfs_phone, info, sizeof(*info));
[efd4a72]94 if (rc != EOK) {
95 async_wait_for(req, NULL);
96 return rc;
97 }
98
99 /*
100 * Ask VFS for callback connection.
101 */
102 ipc_connect_to_me(vfs_phone, 0, 0, 0, &reg->vfs_phonehash);
103
104 /*
105 * Allocate piece of address space for PLB.
106 */
107 reg->plb_ro = as_get_mappable_page(PLB_SIZE);
108 if (!reg->plb_ro) {
109 async_wait_for(req, NULL);
110 return ENOMEM;
111 }
112
113 /*
114 * Request sharing the Path Lookup Buffer with VFS.
115 */
[0da4e41]116 rc = async_share_in_start_0_0(vfs_phone, reg->plb_ro, PLB_SIZE);
[efd4a72]117 if (rc) {
118 async_wait_for(req, NULL);
119 return rc;
120 }
121
122 /*
[4198f9c3]123 * Pick up the answer for the request to the VFS_IN_REQUEST call.
[efd4a72]124 */
125 async_wait_for(req, NULL);
126 reg->fs_handle = (int) IPC_GET_ARG1(answer);
127
128 /*
129 * Create a connection fibril to handle the callback connection.
130 */
131 async_new_connection(reg->vfs_phonehash, 0, NULL, conn);
132
133 /*
134 * Tell the async framework that other connections are to be handled by
135 * the same connection fibril as well.
136 */
137 async_set_client_connection(conn);
138
139 return IPC_GET_RETVAL(answer);
140}
[74303b6]141
[83937ccd]142void fs_node_initialize(fs_node_t *fn)
143{
144 memset(fn, 0, sizeof(fs_node_t));
145}
146
[16d17ca]147void libfs_mount(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
148 ipc_call_t *request)
[83937ccd]149{
[55982d6]150 dev_handle_t mp_dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
151 fs_index_t mp_fs_index = (fs_index_t) IPC_GET_ARG2(*request);
152 fs_handle_t mr_fs_handle = (fs_handle_t) IPC_GET_ARG3(*request);
153 dev_handle_t mr_dev_handle = (dev_handle_t) IPC_GET_ARG4(*request);
[83937ccd]154 int res;
155 ipcarg_t rc;
156
157 ipc_call_t call;
158 ipc_callid_t callid;
159
160 /* accept the phone */
161 callid = async_get_call(&call);
162 int mountee_phone = (int)IPC_GET_ARG1(call);
163 if ((IPC_GET_METHOD(call) != IPC_M_CONNECTION_CLONE) ||
164 mountee_phone < 0) {
165 ipc_answer_0(callid, EINVAL);
166 ipc_answer_0(rid, EINVAL);
167 return;
168 }
169 ipc_answer_0(callid, EOK); /* acknowledge the mountee_phone */
170
[0da4e41]171 res = async_data_write_receive(&callid, NULL);
[83937ccd]172 if (!res) {
173 ipc_hangup(mountee_phone);
174 ipc_answer_0(callid, EINVAL);
175 ipc_answer_0(rid, EINVAL);
176 return;
177 }
178
[0daba212]179 fs_node_t *fn;
180 res = ops->node_get(&fn, mp_dev_handle, mp_fs_index);
181 if (res != EOK || !fn) {
[83937ccd]182 ipc_hangup(mountee_phone);
[0daba212]183 ipc_answer_0(callid, combine_rc(res, ENOENT));
184 ipc_answer_0(rid, combine_rc(res, ENOENT));
[83937ccd]185 return;
186 }
187
188 if (fn->mp_data.mp_active) {
189 ipc_hangup(mountee_phone);
[0daba212]190 (void) ops->node_put(fn);
[83937ccd]191 ipc_answer_0(callid, EBUSY);
192 ipc_answer_0(rid, EBUSY);
193 return;
194 }
195
196 rc = async_req_0_0(mountee_phone, IPC_M_CONNECT_ME);
[0daba212]197 if (rc != EOK) {
[83937ccd]198 ipc_hangup(mountee_phone);
[0daba212]199 (void) ops->node_put(fn);
[83937ccd]200 ipc_answer_0(callid, rc);
201 ipc_answer_0(rid, rc);
202 return;
203 }
204
205 ipc_call_t answer;
[4198f9c3]206 aid_t msg = async_send_1(mountee_phone, VFS_OUT_MOUNTED, mr_dev_handle,
[83937ccd]207 &answer);
208 ipc_forward_fast(callid, mountee_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
209 async_wait_for(msg, &rc);
210
211 if (rc == EOK) {
212 fn->mp_data.mp_active = true;
213 fn->mp_data.fs_handle = mr_fs_handle;
214 fn->mp_data.dev_handle = mr_dev_handle;
215 fn->mp_data.phone = mountee_phone;
216 }
217 /*
218 * Do not release the FS node so that it stays in memory.
219 */
[0442c02]220 ipc_answer_3(rid, rc, IPC_GET_ARG1(answer), IPC_GET_ARG2(answer),
221 IPC_GET_ARG3(answer));
[83937ccd]222}
223
[1e50f81]224/** Lookup VFS triplet by name in the file system name space.
[9bb85f3]225 *
226 * The path passed in the PLB must be in the canonical file system path format
227 * as returned by the canonify() function.
[1e50f81]228 *
[595edf5]229 * @param ops libfs operations structure with function pointers to
230 * file system implementation
231 * @param fs_handle File system handle of the file system where to perform
232 * the lookup.
[4198f9c3]233 * @param rid Request ID of the VFS_OUT_LOOKUP request.
234 * @param request VFS_OUT_LOOKUP request data itself.
[595edf5]235 *
[1e50f81]236 */
[f2ec8c8]237void libfs_lookup(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
[2c448fb]238 ipc_call_t *request)
239{
[83937ccd]240 unsigned first = IPC_GET_ARG1(*request);
[2c448fb]241 unsigned last = IPC_GET_ARG2(*request);
[83937ccd]242 unsigned next = first;
[f2ec8c8]243 dev_handle_t dev_handle = IPC_GET_ARG3(*request);
[2c448fb]244 int lflag = IPC_GET_ARG4(*request);
[f2ec8c8]245 fs_index_t index = IPC_GET_ARG5(*request); /* when L_LINK specified */
[c9f6e49f]246 char component[NAME_MAX + 1];
247 int len;
[0daba212]248 int rc;
[2c448fb]249
250 if (last < next)
251 last += PLB_SIZE;
252
[b6035ba]253 fs_node_t *par = NULL;
[0daba212]254 fs_node_t *cur = NULL;
[b6035ba]255 fs_node_t *tmp = NULL;
[2c448fb]256
[0daba212]257 rc = ops->root_get(&cur, dev_handle);
258 on_error(rc, goto out_with_answer);
259
[83937ccd]260 if (cur->mp_data.mp_active) {
[4198f9c3]261 ipc_forward_slow(rid, cur->mp_data.phone, VFS_OUT_LOOKUP,
[83937ccd]262 next, last, cur->mp_data.dev_handle, lflag, index,
263 IPC_FF_ROUTE_FROM_ME);
[0daba212]264 (void) ops->node_put(cur);
[83937ccd]265 return;
266 }
267
[2c448fb]268 if (ops->plb_get_char(next) == '/')
269 next++; /* eat slash */
270
[0daba212]271 while (next <= last) {
272 bool has_children;
273
274 rc = ops->has_children(&has_children, cur);
275 on_error(rc, goto out_with_answer);
276 if (!has_children)
277 break;
278
[2c448fb]279 /* collect the component */
[c9f6e49f]280 len = 0;
[fa832eb]281 while ((next <= last) && (ops->plb_get_char(next) != '/')) {
[2c448fb]282 if (len + 1 == NAME_MAX) {
[abb2865]283 /* component length overflow */
[2c448fb]284 ipc_answer_0(rid, ENAMETOOLONG);
[06901c6b]285 goto out;
[2c448fb]286 }
287 component[len++] = ops->plb_get_char(next);
288 next++; /* process next character */
289 }
290
291 assert(len);
292 component[len] = '\0';
293 next++; /* eat slash */
294
295 /* match the component */
[0daba212]296 rc = ops->match(&tmp, cur, component);
297 on_error(rc, goto out_with_answer);
298
[83937ccd]299 if (tmp && tmp->mp_data.mp_active) {
300 if (next > last)
301 next = last = first;
302 else
303 next--;
304
[4198f9c3]305 ipc_forward_slow(rid, tmp->mp_data.phone,
306 VFS_OUT_LOOKUP, next, last, tmp->mp_data.dev_handle,
307 lflag, index, IPC_FF_ROUTE_FROM_ME);
[0daba212]308 (void) ops->node_put(cur);
309 (void) ops->node_put(tmp);
[83937ccd]310 if (par)
[0daba212]311 (void) ops->node_put(par);
[83937ccd]312 return;
313 }
[2c448fb]314
315 /* handle miss: match amongst siblings */
316 if (!tmp) {
[a8e9ab8d]317 if (next <= last) {
318 /* there are unprocessed components */
319 ipc_answer_0(rid, ENOENT);
[06901c6b]320 goto out;
[a8e9ab8d]321 }
322 /* miss in the last component */
323 if (lflag & (L_CREATE | L_LINK)) {
324 /* request to create a new link */
[2c448fb]325 if (!ops->is_directory(cur)) {
326 ipc_answer_0(rid, ENOTDIR);
[06901c6b]327 goto out;
[a8e9ab8d]328 }
[b6035ba]329 fs_node_t *fn;
[a8e9ab8d]330 if (lflag & L_CREATE)
[0daba212]331 rc = ops->create(&fn, dev_handle,
332 lflag);
[a8e9ab8d]333 else
[0daba212]334 rc = ops->node_get(&fn, dev_handle,
[34f62f8]335 index);
[0daba212]336 on_error(rc, goto out_with_answer);
[b6035ba]337 if (fn) {
338 rc = ops->link(cur, fn, component);
[0013b9ce]339 if (rc != EOK) {
[0daba212]340 if (lflag & L_CREATE)
341 (void) ops->destroy(fn);
[0013b9ce]342 ipc_answer_0(rid, rc);
[2c448fb]343 } else {
344 ipc_answer_5(rid, EOK,
345 fs_handle, dev_handle,
[b6035ba]346 ops->index_get(fn),
347 ops->size_get(fn),
348 ops->lnkcnt_get(fn));
[0daba212]349 (void) ops->node_put(fn);
[2c448fb]350 }
351 } else {
352 ipc_answer_0(rid, ENOSPC);
353 }
[06901c6b]354 goto out;
[a8e9ab8d]355 }
[2c448fb]356 ipc_answer_0(rid, ENOENT);
[06901c6b]357 goto out;
[2c448fb]358 }
359
[0daba212]360 if (par) {
361 rc = ops->node_put(par);
362 on_error(rc, goto out_with_answer);
363 }
[06901c6b]364
[2c448fb]365 /* descend one level */
[7b6d98b]366 par = cur;
[2c448fb]367 cur = tmp;
[06901c6b]368 tmp = NULL;
[2c448fb]369 }
370
371 /* handle miss: excessive components */
[0daba212]372 if (next <= last) {
373 bool has_children;
374
375 rc = ops->has_children(&has_children, cur);
376 on_error(rc, goto out_with_answer);
377 if (has_children)
378 goto skip_miss;
379
[a8e9ab8d]380 if (lflag & (L_CREATE | L_LINK)) {
[2c448fb]381 if (!ops->is_directory(cur)) {
382 ipc_answer_0(rid, ENOTDIR);
[06901c6b]383 goto out;
[2c448fb]384 }
385
386 /* collect next component */
[c9f6e49f]387 len = 0;
[2c448fb]388 while (next <= last) {
389 if (ops->plb_get_char(next) == '/') {
390 /* more than one component */
391 ipc_answer_0(rid, ENOENT);
[06901c6b]392 goto out;
[2c448fb]393 }
394 if (len + 1 == NAME_MAX) {
395 /* component length overflow */
396 ipc_answer_0(rid, ENAMETOOLONG);
[06901c6b]397 goto out;
[2c448fb]398 }
399 component[len++] = ops->plb_get_char(next);
400 next++; /* process next character */
401 }
402 assert(len);
403 component[len] = '\0';
404
[b6035ba]405 fs_node_t *fn;
[a8e9ab8d]406 if (lflag & L_CREATE)
[0daba212]407 rc = ops->create(&fn, dev_handle, lflag);
[a8e9ab8d]408 else
[0daba212]409 rc = ops->node_get(&fn, dev_handle, index);
410 on_error(rc, goto out_with_answer);
[b6035ba]411 if (fn) {
412 rc = ops->link(cur, fn, component);
[0013b9ce]413 if (rc != EOK) {
[a8e9ab8d]414 if (lflag & L_CREATE)
[0daba212]415 (void) ops->destroy(fn);
[0013b9ce]416 ipc_answer_0(rid, rc);
[2c448fb]417 } else {
418 ipc_answer_5(rid, EOK,
419 fs_handle, dev_handle,
[b6035ba]420 ops->index_get(fn),
421 ops->size_get(fn),
422 ops->lnkcnt_get(fn));
[0daba212]423 (void) ops->node_put(fn);
[2c448fb]424 }
425 } else {
426 ipc_answer_0(rid, ENOSPC);
427 }
[06901c6b]428 goto out;
[2c448fb]429 }
430 ipc_answer_0(rid, ENOENT);
[06901c6b]431 goto out;
[2c448fb]432 }
[0daba212]433skip_miss:
[2c448fb]434
435 /* handle hit */
[a8e9ab8d]436 if (lflag & L_UNLINK) {
[2c448fb]437 unsigned old_lnkcnt = ops->lnkcnt_get(cur);
[0daba212]438 rc = ops->unlink(par, cur, component);
439 ipc_answer_5(rid, (ipcarg_t)rc, fs_handle, dev_handle,
[2c448fb]440 ops->index_get(cur), ops->size_get(cur), old_lnkcnt);
[06901c6b]441 goto out;
[2c448fb]442 }
[a8e9ab8d]443 if (((lflag & (L_CREATE | L_EXCLUSIVE)) == (L_CREATE | L_EXCLUSIVE)) ||
444 (lflag & L_LINK)) {
[2c448fb]445 ipc_answer_0(rid, EEXIST);
[06901c6b]446 goto out;
[2c448fb]447 }
448 if ((lflag & L_FILE) && (ops->is_directory(cur))) {
449 ipc_answer_0(rid, EISDIR);
[06901c6b]450 goto out;
[2c448fb]451 }
452 if ((lflag & L_DIRECTORY) && (ops->is_file(cur))) {
453 ipc_answer_0(rid, ENOTDIR);
[06901c6b]454 goto out;
[2c448fb]455 }
456
[0daba212]457out_with_answer:
458 if (rc == EOK) {
459 ipc_answer_5(rid, EOK, fs_handle, dev_handle,
460 ops->index_get(cur), ops->size_get(cur),
461 ops->lnkcnt_get(cur));
462 } else {
463 ipc_answer_0(rid, rc);
464 }
[06901c6b]465
466out:
467 if (par)
[0daba212]468 (void) ops->node_put(par);
[06901c6b]469 if (cur)
[0daba212]470 (void) ops->node_put(cur);
[06901c6b]471 if (tmp)
[0daba212]472 (void) ops->node_put(tmp);
[2c448fb]473}
474
[75160a6]475void libfs_stat(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
476 ipc_call_t *request)
477{
478 dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
479 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
[0daba212]480 fs_node_t *fn;
481 int rc;
482
483 rc = ops->node_get(&fn, dev_handle, index);
484 on_error(rc, answer_and_return(rid, rc));
[75160a6]485
486 ipc_callid_t callid;
487 size_t size;
[0da4e41]488 if (!async_data_read_receive(&callid, &size) ||
[0143f72]489 size != sizeof(struct stat)) {
[75160a6]490 ipc_answer_0(callid, EINVAL);
491 ipc_answer_0(rid, EINVAL);
492 return;
493 }
494
[0143f72]495 struct stat stat;
496 memset(&stat, 0, sizeof(struct stat));
[75160a6]497
[0143f72]498 stat.fs_handle = fs_handle;
499 stat.dev_handle = dev_handle;
500 stat.index = index;
501 stat.lnkcnt = ops->lnkcnt_get(fn);
502 stat.is_file = ops->is_file(fn);
503 stat.size = ops->size_get(fn);
504
[0da4e41]505 async_data_read_finalize(callid, &stat, sizeof(struct stat));
[75160a6]506 ipc_answer_0(rid, EOK);
507}
508
[595edf5]509/** Open VFS triplet.
510 *
511 * @param ops libfs operations structure with function pointers to
512 * file system implementation
[4198f9c3]513 * @param rid Request ID of the VFS_OUT_OPEN_NODE request.
514 * @param request VFS_OUT_OPEN_NODE request data itself.
[595edf5]515 *
516 */
517void libfs_open_node(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
518 ipc_call_t *request)
519{
520 dev_handle_t dev_handle = IPC_GET_ARG1(*request);
521 fs_index_t index = IPC_GET_ARG2(*request);
[0daba212]522 fs_node_t *fn;
523 int rc;
[595edf5]524
[0daba212]525 rc = ops->node_get(&fn, dev_handle, index);
526 on_error(rc, answer_and_return(rid, rc));
[595edf5]527
[0daba212]528 if (fn == NULL) {
[595edf5]529 ipc_answer_0(rid, ENOENT);
530 return;
531 }
532
[2e983c7]533 ipc_answer_3(rid, EOK, ops->size_get(fn), ops->lnkcnt_get(fn),
534 (ops->is_file(fn) ? L_FILE : 0) | (ops->is_directory(fn) ? L_DIRECTORY : 0));
[595edf5]535
[0daba212]536 (void) ops->node_put(fn);
[595edf5]537}
538
[74303b6]539/** @}
540 */
Note: See TracBrowser for help on using the repository browser.