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
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 commonod 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 */
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;
88 aid_t req = async_send_0(vfs_phone, VFS_IN_REGISTER, &answer);
89
90 /*
91 * Send our VFS info structure to VFS.
92 */
93 int rc = async_data_write_start(vfs_phone, info, sizeof(*info));
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 */
116 rc = async_share_in_start_0_0(vfs_phone, reg->plb_ro, PLB_SIZE);
117 if (rc) {
118 async_wait_for(req, NULL);
119 return rc;
120 }
121
122 /*
123 * Pick up the answer for the request to the VFS_IN_REQUEST call.
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}
141
142void fs_node_initialize(fs_node_t *fn)
143{
144 memset(fn, 0, sizeof(fs_node_t));
145}
146
147void libfs_mount(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
148 ipc_call_t *request)
149{
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);
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
171 res = async_data_write_receive(&callid, NULL);
172 if (!res) {
173 ipc_hangup(mountee_phone);
174 ipc_answer_0(callid, EINVAL);
175 ipc_answer_0(rid, EINVAL);
176 return;
177 }
178
179 fs_node_t *fn;
180 res = ops->node_get(&fn, mp_dev_handle, mp_fs_index);
181 if (res != EOK || !fn) {
182 ipc_hangup(mountee_phone);
183 ipc_answer_0(callid, combine_rc(res, ENOENT));
184 ipc_answer_0(rid, combine_rc(res, ENOENT));
185 return;
186 }
187
188 if (fn->mp_data.mp_active) {
189 ipc_hangup(mountee_phone);
190 (void) ops->node_put(fn);
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);
197 if (rc != EOK) {
198 ipc_hangup(mountee_phone);
199 (void) ops->node_put(fn);
200 ipc_answer_0(callid, rc);
201 ipc_answer_0(rid, rc);
202 return;
203 }
204
205 ipc_call_t answer;
206 aid_t msg = async_send_1(mountee_phone, VFS_OUT_MOUNTED, mr_dev_handle,
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 */
220 ipc_answer_3(rid, rc, IPC_GET_ARG1(answer), IPC_GET_ARG2(answer),
221 IPC_GET_ARG3(answer));
222}
223
224/** Lookup VFS triplet by name in the file system name space.
225 *
226 * The path passed in the PLB must be in the canonical file system path format
227 * as returned by the canonify() function.
228 *
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.
233 * @param rid Request ID of the VFS_OUT_LOOKUP request.
234 * @param request VFS_OUT_LOOKUP request data itself.
235 *
236 */
237void libfs_lookup(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
238 ipc_call_t *request)
239{
240 unsigned first = IPC_GET_ARG1(*request);
241 unsigned last = IPC_GET_ARG2(*request);
242 unsigned next = first;
243 dev_handle_t dev_handle = IPC_GET_ARG3(*request);
244 int lflag = IPC_GET_ARG4(*request);
245 fs_index_t index = IPC_GET_ARG5(*request); /* when L_LINK specified */
246 char component[NAME_MAX + 1];
247 int len;
248 int rc;
249
250 if (last < next)
251 last += PLB_SIZE;
252
253 fs_node_t *par = NULL;
254 fs_node_t *cur = NULL;
255 fs_node_t *tmp = NULL;
256
257 rc = ops->root_get(&cur, dev_handle);
258 on_error(rc, goto out_with_answer);
259
260 if (cur->mp_data.mp_active) {
261 ipc_forward_slow(rid, cur->mp_data.phone, VFS_OUT_LOOKUP,
262 next, last, cur->mp_data.dev_handle, lflag, index,
263 IPC_FF_ROUTE_FROM_ME);
264 (void) ops->node_put(cur);
265 return;
266 }
267
268 if (ops->plb_get_char(next) == '/')
269 next++; /* eat slash */
270
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
279 /* collect the component */
280 len = 0;
281 while ((next <= last) && (ops->plb_get_char(next) != '/')) {
282 if (len + 1 == NAME_MAX) {
283 /* component length overflow */
284 ipc_answer_0(rid, ENAMETOOLONG);
285 goto out;
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 */
296 rc = ops->match(&tmp, cur, component);
297 on_error(rc, goto out_with_answer);
298
299 if (tmp && tmp->mp_data.mp_active) {
300 if (next > last)
301 next = last = first;
302 else
303 next--;
304
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);
308 (void) ops->node_put(cur);
309 (void) ops->node_put(tmp);
310 if (par)
311 (void) ops->node_put(par);
312 return;
313 }
314
315 /* handle miss: match amongst siblings */
316 if (!tmp) {
317 if (next <= last) {
318 /* there are unprocessed components */
319 ipc_answer_0(rid, ENOENT);
320 goto out;
321 }
322 /* miss in the last component */
323 if (lflag & (L_CREATE | L_LINK)) {
324 /* request to create a new link */
325 if (!ops->is_directory(cur)) {
326 ipc_answer_0(rid, ENOTDIR);
327 goto out;
328 }
329 fs_node_t *fn;
330 if (lflag & L_CREATE)
331 rc = ops->create(&fn, dev_handle,
332 lflag);
333 else
334 rc = ops->node_get(&fn, dev_handle,
335 index);
336 on_error(rc, goto out_with_answer);
337 if (fn) {
338 rc = ops->link(cur, fn, component);
339 if (rc != EOK) {
340 if (lflag & L_CREATE)
341 (void) ops->destroy(fn);
342 ipc_answer_0(rid, rc);
343 } else {
344 ipc_answer_5(rid, EOK,
345 fs_handle, dev_handle,
346 ops->index_get(fn),
347 ops->size_get(fn),
348 ops->lnkcnt_get(fn));
349 (void) ops->node_put(fn);
350 }
351 } else {
352 ipc_answer_0(rid, ENOSPC);
353 }
354 goto out;
355 }
356 ipc_answer_0(rid, ENOENT);
357 goto out;
358 }
359
360 if (par) {
361 rc = ops->node_put(par);
362 on_error(rc, goto out_with_answer);
363 }
364
365 /* descend one level */
366 par = cur;
367 cur = tmp;
368 tmp = NULL;
369 }
370
371 /* handle miss: excessive components */
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
380 if (lflag & (L_CREATE | L_LINK)) {
381 if (!ops->is_directory(cur)) {
382 ipc_answer_0(rid, ENOTDIR);
383 goto out;
384 }
385
386 /* collect next component */
387 len = 0;
388 while (next <= last) {
389 if (ops->plb_get_char(next) == '/') {
390 /* more than one component */
391 ipc_answer_0(rid, ENOENT);
392 goto out;
393 }
394 if (len + 1 == NAME_MAX) {
395 /* component length overflow */
396 ipc_answer_0(rid, ENAMETOOLONG);
397 goto out;
398 }
399 component[len++] = ops->plb_get_char(next);
400 next++; /* process next character */
401 }
402 assert(len);
403 component[len] = '\0';
404
405 fs_node_t *fn;
406 if (lflag & L_CREATE)
407 rc = ops->create(&fn, dev_handle, lflag);
408 else
409 rc = ops->node_get(&fn, dev_handle, index);
410 on_error(rc, goto out_with_answer);
411 if (fn) {
412 rc = ops->link(cur, fn, component);
413 if (rc != EOK) {
414 if (lflag & L_CREATE)
415 (void) ops->destroy(fn);
416 ipc_answer_0(rid, rc);
417 } else {
418 ipc_answer_5(rid, EOK,
419 fs_handle, dev_handle,
420 ops->index_get(fn),
421 ops->size_get(fn),
422 ops->lnkcnt_get(fn));
423 (void) ops->node_put(fn);
424 }
425 } else {
426 ipc_answer_0(rid, ENOSPC);
427 }
428 goto out;
429 }
430 ipc_answer_0(rid, ENOENT);
431 goto out;
432 }
433skip_miss:
434
435 /* handle hit */
436 if (lflag & L_UNLINK) {
437 unsigned old_lnkcnt = ops->lnkcnt_get(cur);
438 rc = ops->unlink(par, cur, component);
439 ipc_answer_5(rid, (ipcarg_t)rc, fs_handle, dev_handle,
440 ops->index_get(cur), ops->size_get(cur), old_lnkcnt);
441 goto out;
442 }
443 if (((lflag & (L_CREATE | L_EXCLUSIVE)) == (L_CREATE | L_EXCLUSIVE)) ||
444 (lflag & L_LINK)) {
445 ipc_answer_0(rid, EEXIST);
446 goto out;
447 }
448 if ((lflag & L_FILE) && (ops->is_directory(cur))) {
449 ipc_answer_0(rid, EISDIR);
450 goto out;
451 }
452 if ((lflag & L_DIRECTORY) && (ops->is_file(cur))) {
453 ipc_answer_0(rid, ENOTDIR);
454 goto out;
455 }
456
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 }
465
466out:
467 if (par)
468 (void) ops->node_put(par);
469 if (cur)
470 (void) ops->node_put(cur);
471 if (tmp)
472 (void) ops->node_put(tmp);
473}
474
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);
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));
485
486 ipc_callid_t callid;
487 size_t size;
488 if (!async_data_read_receive(&callid, &size) ||
489 size != sizeof(struct stat)) {
490 ipc_answer_0(callid, EINVAL);
491 ipc_answer_0(rid, EINVAL);
492 return;
493 }
494
495 struct stat stat;
496 memset(&stat, 0, sizeof(struct stat));
497
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
505 async_data_read_finalize(callid, &stat, sizeof(struct stat));
506 ipc_answer_0(rid, EOK);
507}
508
509/** Open VFS triplet.
510 *
511 * @param ops libfs operations structure with function pointers to
512 * file system implementation
513 * @param rid Request ID of the VFS_OUT_OPEN_NODE request.
514 * @param request VFS_OUT_OPEN_NODE request data itself.
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);
522 fs_node_t *fn;
523 int rc;
524
525 rc = ops->node_get(&fn, dev_handle, index);
526 on_error(rc, answer_and_return(rid, rc));
527
528 if (fn == NULL) {
529 ipc_answer_0(rid, ENOENT);
530 return;
531 }
532
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));
535
536 (void) ops->node_put(fn);
537}
538
539/** @}
540 */
Note: See TracBrowser for help on using the repository browser.