source: mainline/uspace/lib/fs/libfs.c@ 6aae539d

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

rename IPC_M_CONNECT_ME to IPC_M_CLONE_ESTABLISH

  • the term itself should be more understandable and give more hint with respect to what is actually does
  • IPC_M_CONNECT_ME was a prefix for IPC_M_CONNECT_ME_TO which was rather inconvenient
  • small documentation and cstyle improvements
  • Property mode set to 100644
File size: 23.0 KB
RevLine 
[74303b6]1/*
[1313ee9]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
[1313ee9]29/** @addtogroup libfs
[74303b6]30 * @{
[1313ee9]31 */
[74303b6]32/**
33 * @file
[1313ee9]34 * Glue code which is common to all FS implementations.
[74303b6]35 */
36
[1313ee9]37#include "libfs.h"
[efd4a72]38#include "../../srv/vfs/vfs.h"
[ed903174]39#include <macros.h>
[efd4a72]40#include <errno.h>
41#include <async.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>
[efcebe1]47#include <stdlib.h>
[efd4a72]48
[0daba212]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 { \
[ffa2c8ef]60 async_answer_0((rid), (rc)); \
[0daba212]61 return; \
62 } while (0)
63
[efcebe1]64static fs_reg_t reg;
65
66static vfs_out_ops_t *vfs_out_ops = NULL;
67static libfs_ops_t *libfs_ops = NULL;
68
69static void libfs_mount(libfs_ops_t *, fs_handle_t, ipc_callid_t, ipc_call_t *);
70static void libfs_unmount(libfs_ops_t *, ipc_callid_t, ipc_call_t *);
71static void libfs_lookup(libfs_ops_t *, fs_handle_t, ipc_callid_t,
72 ipc_call_t *);
73static void libfs_stat(libfs_ops_t *, fs_handle_t, ipc_callid_t, ipc_call_t *);
74static void libfs_open_node(libfs_ops_t *, fs_handle_t, ipc_callid_t,
75 ipc_call_t *);
76
77static void vfs_out_mounted(ipc_callid_t rid, ipc_call_t *req)
78{
[86ffa27f]79 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*req);
[efcebe1]80 char *opts;
81 int rc;
82
83 /* Accept the mount options. */
84 rc = async_data_write_accept((void **) &opts, true, 0, 0, 0, NULL);
85 if (rc != EOK) {
86 async_answer_0(rid, rc);
87 return;
88 }
89
90 fs_index_t index;
91 aoff64_t size;
92 unsigned lnkcnt;
[86ffa27f]93 rc = vfs_out_ops->mounted(service_id, opts, &index, &size, &lnkcnt);
[efcebe1]94
[7eb0fed8]95 if (rc == EOK)
96 async_answer_4(rid, EOK, index, LOWER32(size), UPPER32(size),
97 lnkcnt);
[efcebe1]98 else
99 async_answer_0(rid, rc);
100
101 free(opts);
102}
103
104static void vfs_out_mount(ipc_callid_t rid, ipc_call_t *req)
105{
106 libfs_mount(libfs_ops, reg.fs_handle, rid, req);
107}
108
109static void vfs_out_unmounted(ipc_callid_t rid, ipc_call_t *req)
110{
[86ffa27f]111 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*req);
[efcebe1]112 int rc;
113
[86ffa27f]114 rc = vfs_out_ops->unmounted(service_id);
[efcebe1]115
116 async_answer_0(rid, rc);
117}
118
119static void vfs_out_unmount(ipc_callid_t rid, ipc_call_t *req)
120{
121
122 libfs_unmount(libfs_ops, rid, req);
123}
124
125static void vfs_out_lookup(ipc_callid_t rid, ipc_call_t *req)
126{
127 libfs_lookup(libfs_ops, reg.fs_handle, rid, req);
128}
129
130static void vfs_out_read(ipc_callid_t rid, ipc_call_t *req)
131{
[86ffa27f]132 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*req);
[efcebe1]133 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*req);
134 aoff64_t pos = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*req),
135 IPC_GET_ARG4(*req));
136 size_t rbytes;
137 int rc;
138
[86ffa27f]139 rc = vfs_out_ops->read(service_id, index, pos, &rbytes);
[efcebe1]140
141 if (rc == EOK)
142 async_answer_1(rid, EOK, rbytes);
143 else
144 async_answer_0(rid, rc);
145}
146
147static void vfs_out_write(ipc_callid_t rid, ipc_call_t *req)
148{
[86ffa27f]149 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*req);
[efcebe1]150 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*req);
151 aoff64_t pos = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*req),
152 IPC_GET_ARG4(*req));
153 size_t wbytes;
154 aoff64_t nsize;
155 int rc;
156
[86ffa27f]157 rc = vfs_out_ops->write(service_id, index, pos, &wbytes, &nsize);
[efcebe1]158
[5bb9907]159 if (rc == EOK)
160 async_answer_3(rid, EOK, wbytes, LOWER32(nsize), UPPER32(nsize));
[efcebe1]161 else
162 async_answer_0(rid, rc);
163}
164
165static void vfs_out_truncate(ipc_callid_t rid, ipc_call_t *req)
166{
[86ffa27f]167 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*req);
[efcebe1]168 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*req);
169 aoff64_t size = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*req),
170 IPC_GET_ARG4(*req));
171 int rc;
172
[86ffa27f]173 rc = vfs_out_ops->truncate(service_id, index, size);
[efcebe1]174
175 async_answer_0(rid, rc);
176}
177
178static void vfs_out_close(ipc_callid_t rid, ipc_call_t *req)
179{
[86ffa27f]180 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*req);
[efcebe1]181 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*req);
182 int rc;
183
[86ffa27f]184 rc = vfs_out_ops->close(service_id, index);
[efcebe1]185
186 async_answer_0(rid, rc);
187}
188
189static void vfs_out_destroy(ipc_callid_t rid, ipc_call_t *req)
190{
[86ffa27f]191 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*req);
[efcebe1]192 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*req);
193 int rc;
194
[86ffa27f]195 rc = vfs_out_ops->destroy(service_id, index);
[efcebe1]196
197 async_answer_0(rid, rc);
198}
199
200static void vfs_out_open_node(ipc_callid_t rid, ipc_call_t *req)
201{
202 libfs_open_node(libfs_ops, reg.fs_handle, rid, req);
203}
204
205static void vfs_out_stat(ipc_callid_t rid, ipc_call_t *req)
206{
207 libfs_stat(libfs_ops, reg.fs_handle, rid, req);
208}
209
210static void vfs_out_sync(ipc_callid_t rid, ipc_call_t *req)
211{
[86ffa27f]212 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*req);
[efcebe1]213 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*req);
214 int rc;
215
[86ffa27f]216 rc = vfs_out_ops->sync(service_id, index);
[efcebe1]217
218 async_answer_0(rid, rc);
219}
220
221static void vfs_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
222{
223 if (iid) {
224 /*
225 * This only happens for connections opened by
226 * IPC_M_CONNECT_ME_TO calls as opposed to callback connections
227 * created by IPC_M_CONNECT_TO_ME.
228 */
229 async_answer_0(iid, EOK);
230 }
231
232 while (true) {
233 ipc_call_t call;
234 ipc_callid_t callid = async_get_call(&call);
235
236 if (!IPC_GET_IMETHOD(call))
237 return;
238
239 switch (IPC_GET_IMETHOD(call)) {
240 case VFS_OUT_MOUNTED:
241 vfs_out_mounted(callid, &call);
242 break;
243 case VFS_OUT_MOUNT:
244 vfs_out_mount(callid, &call);
245 break;
246 case VFS_OUT_UNMOUNTED:
247 vfs_out_unmounted(callid, &call);
248 break;
249 case VFS_OUT_UNMOUNT:
250 vfs_out_unmount(callid, &call);
251 break;
252 case VFS_OUT_LOOKUP:
253 vfs_out_lookup(callid, &call);
254 break;
255 case VFS_OUT_READ:
256 vfs_out_read(callid, &call);
257 break;
258 case VFS_OUT_WRITE:
259 vfs_out_write(callid, &call);
260 break;
261 case VFS_OUT_TRUNCATE:
262 vfs_out_truncate(callid, &call);
263 break;
264 case VFS_OUT_CLOSE:
265 vfs_out_close(callid, &call);
266 break;
267 case VFS_OUT_DESTROY:
268 vfs_out_destroy(callid, &call);
269 break;
270 case VFS_OUT_OPEN_NODE:
271 vfs_out_open_node(callid, &call);
272 break;
273 case VFS_OUT_STAT:
274 vfs_out_stat(callid, &call);
275 break;
276 case VFS_OUT_SYNC:
277 vfs_out_sync(callid, &call);
278 break;
279 default:
280 async_answer_0(callid, ENOTSUP);
281 break;
282 }
283 }
284}
285
[efd4a72]286/** Register file system server.
287 *
288 * This function abstracts away the tedious registration protocol from
289 * file system implementations and lets them to reuse this registration glue
290 * code.
291 *
[79ae36dd]292 * @param sess Session for communication with VFS.
293 * @param info VFS info structure supplied by the file system
294 * implementation.
[efcebe1]295 * @param vops Address of the vfs_out_ops_t structure.
296 * @param lops Address of the libfs_ops_t structure.
[1313ee9]297 *
298 * @return EOK on success or a non-zero error code on errror.
[efd4a72]299 *
300 */
[efcebe1]301int fs_register(async_sess_t *sess, vfs_info_t *info, vfs_out_ops_t *vops,
302 libfs_ops_t *lops)
[efd4a72]303{
304 /*
305 * Tell VFS that we are here and want to get registered.
306 * We use the async framework because VFS will answer the request
307 * out-of-order, when it knows that the operation succeeded or failed.
308 */
[79ae36dd]309
310 async_exch_t *exch = async_exchange_begin(sess);
311
[efd4a72]312 ipc_call_t answer;
[79ae36dd]313 aid_t req = async_send_0(exch, VFS_IN_REGISTER, &answer);
[1313ee9]314
[efd4a72]315 /*
316 * Send our VFS info structure to VFS.
317 */
[79ae36dd]318 int rc = async_data_write_start(exch, info, sizeof(*info));
319
[efd4a72]320 if (rc != EOK) {
[79ae36dd]321 async_exchange_end(exch);
[50b581d]322 async_forget(req);
[efd4a72]323 return rc;
324 }
[1313ee9]325
[efcebe1]326 /*
327 * Set VFS_OUT and libfs operations.
328 */
329 vfs_out_ops = vops;
330 libfs_ops = lops;
331
[efd4a72]332 /*
333 * Ask VFS for callback connection.
334 */
[efcebe1]335 async_connect_to_me(exch, 0, 0, 0, vfs_connection, NULL);
[1313ee9]336
[efd4a72]337 /*
[fbcdeb8]338 * Request sharing the Path Lookup Buffer with VFS.
[efd4a72]339 */
[fbcdeb8]340 rc = async_share_in_start_0_0(exch, PLB_SIZE, (void *) &reg.plb_ro);
341 if (reg.plb_ro == (void *) -1) {
[79ae36dd]342 async_exchange_end(exch);
[50b581d]343 async_forget(req);
[efd4a72]344 return ENOMEM;
345 }
[1313ee9]346
[79ae36dd]347 async_exchange_end(exch);
348
[efd4a72]349 if (rc) {
[50b581d]350 async_forget(req);
[efd4a72]351 return rc;
352 }
353
354 /*
[4198f9c3]355 * Pick up the answer for the request to the VFS_IN_REQUEST call.
[efd4a72]356 */
357 async_wait_for(req, NULL);
[efcebe1]358 reg.fs_handle = (int) IPC_GET_ARG1(answer);
[efd4a72]359
360 /*
361 * Tell the async framework that other connections are to be handled by
362 * the same connection fibril as well.
363 */
[efcebe1]364 async_set_client_connection(vfs_connection);
[1313ee9]365
[efd4a72]366 return IPC_GET_RETVAL(answer);
367}
[74303b6]368
[83937ccd]369void fs_node_initialize(fs_node_t *fn)
370{
371 memset(fn, 0, sizeof(fs_node_t));
372}
373
[16d17ca]374void libfs_mount(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
[efcebe1]375 ipc_call_t *req)
[83937ccd]376{
[86ffa27f]377 service_id_t mp_service_id = (service_id_t) IPC_GET_ARG1(*req);
[efcebe1]378 fs_index_t mp_fs_index = (fs_index_t) IPC_GET_ARG2(*req);
379 fs_handle_t mr_fs_handle = (fs_handle_t) IPC_GET_ARG3(*req);
[86ffa27f]380 service_id_t mr_service_id = (service_id_t) IPC_GET_ARG4(*req);
[1313ee9]381
[79ae36dd]382 async_sess_t *mountee_sess = async_clone_receive(EXCHANGE_PARALLEL);
383 if (mountee_sess == NULL) {
[ffa2c8ef]384 async_answer_0(rid, EINVAL);
[83937ccd]385 return;
386 }
[1313ee9]387
[0daba212]388 fs_node_t *fn;
[15f3c3f]389 int res = ops->node_get(&fn, mp_service_id, mp_fs_index);
[1313ee9]390 if ((res != EOK) || (!fn)) {
[79ae36dd]391 async_hangup(mountee_sess);
[eda925a]392 async_data_write_void(combine_rc(res, ENOENT));
[ffa2c8ef]393 async_answer_0(rid, combine_rc(res, ENOENT));
[83937ccd]394 return;
395 }
[1313ee9]396
[83937ccd]397 if (fn->mp_data.mp_active) {
[79ae36dd]398 async_hangup(mountee_sess);
[0daba212]399 (void) ops->node_put(fn);
[eda925a]400 async_data_write_void(EBUSY);
[ffa2c8ef]401 async_answer_0(rid, EBUSY);
[83937ccd]402 return;
403 }
[1313ee9]404
[79ae36dd]405 async_exch_t *exch = async_exchange_begin(mountee_sess);
[6aae539d]406 async_sess_t *sess = async_clone_establish(EXCHANGE_PARALLEL, exch);
[79ae36dd]407
408 if (!sess) {
409 async_exchange_end(exch);
410 async_hangup(mountee_sess);
[0daba212]411 (void) ops->node_put(fn);
[79ae36dd]412 async_data_write_void(errno);
413 async_answer_0(rid, errno);
[83937ccd]414 return;
415 }
416
417 ipc_call_t answer;
[79ae36dd]418 int rc = async_data_write_forward_1_1(exch, VFS_OUT_MOUNTED,
[15f3c3f]419 mr_service_id, &answer);
[79ae36dd]420 async_exchange_end(exch);
[83937ccd]421
422 if (rc == EOK) {
423 fn->mp_data.mp_active = true;
424 fn->mp_data.fs_handle = mr_fs_handle;
[15f3c3f]425 fn->mp_data.service_id = mr_service_id;
[79ae36dd]426 fn->mp_data.sess = mountee_sess;
[83937ccd]427 }
[1313ee9]428
[83937ccd]429 /*
430 * Do not release the FS node so that it stays in memory.
431 */
[7eb0fed8]432 async_answer_4(rid, rc, IPC_GET_ARG1(answer), IPC_GET_ARG2(answer),
433 IPC_GET_ARG3(answer), IPC_GET_ARG4(answer));
[83937ccd]434}
435
[efcebe1]436void libfs_unmount(libfs_ops_t *ops, ipc_callid_t rid, ipc_call_t *req)
[3c11713]437{
[86ffa27f]438 service_id_t mp_service_id = (service_id_t) IPC_GET_ARG1(*req);
[efcebe1]439 fs_index_t mp_fs_index = (fs_index_t) IPC_GET_ARG2(*req);
[c888102]440 fs_node_t *fn;
441 int res;
442
[15f3c3f]443 res = ops->node_get(&fn, mp_service_id, mp_fs_index);
[c888102]444 if ((res != EOK) || (!fn)) {
[ffa2c8ef]445 async_answer_0(rid, combine_rc(res, ENOENT));
[c888102]446 return;
447 }
448
449 /*
450 * We are clearly expecting to find the mount point active.
451 */
452 if (!fn->mp_data.mp_active) {
453 (void) ops->node_put(fn);
[ffa2c8ef]454 async_answer_0(rid, EINVAL);
[c888102]455 return;
456 }
457
458 /*
459 * Tell the mounted file system to unmount.
460 */
[79ae36dd]461 async_exch_t *exch = async_exchange_begin(fn->mp_data.sess);
[15f3c3f]462 res = async_req_1_0(exch, VFS_OUT_UNMOUNTED, fn->mp_data.service_id);
[79ae36dd]463 async_exchange_end(exch);
[c888102]464
465 /*
466 * If everything went well, perform the clean-up on our side.
467 */
468 if (res == EOK) {
[79ae36dd]469 async_hangup(fn->mp_data.sess);
[c888102]470 fn->mp_data.mp_active = false;
471 fn->mp_data.fs_handle = 0;
[15f3c3f]472 fn->mp_data.service_id = 0;
[79ae36dd]473 fn->mp_data.sess = NULL;
474
[c888102]475 /* Drop the reference created in libfs_mount(). */
476 (void) ops->node_put(fn);
477 }
478
479 (void) ops->node_put(fn);
[ffa2c8ef]480 async_answer_0(rid, res);
[3c11713]481}
482
[efcebe1]483static char plb_get_char(unsigned pos)
484{
485 return reg.plb_ro[pos % PLB_SIZE];
486}
487
[1e50f81]488/** Lookup VFS triplet by name in the file system name space.
[9bb85f3]489 *
490 * The path passed in the PLB must be in the canonical file system path format
491 * as returned by the canonify() function.
[1e50f81]492 *
[595edf5]493 * @param ops libfs operations structure with function pointers to
494 * file system implementation
495 * @param fs_handle File system handle of the file system where to perform
496 * the lookup.
[4198f9c3]497 * @param rid Request ID of the VFS_OUT_LOOKUP request.
498 * @param request VFS_OUT_LOOKUP request data itself.
[595edf5]499 *
[1e50f81]500 */
[f2ec8c8]501void libfs_lookup(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
[efcebe1]502 ipc_call_t *req)
[2c448fb]503{
[efcebe1]504 unsigned int first = IPC_GET_ARG1(*req);
505 unsigned int last = IPC_GET_ARG2(*req);
[1313ee9]506 unsigned int next = first;
[86ffa27f]507 service_id_t service_id = IPC_GET_ARG3(*req);
[efcebe1]508 int lflag = IPC_GET_ARG4(*req);
509 fs_index_t index = IPC_GET_ARG5(*req);
[c9f6e49f]510 char component[NAME_MAX + 1];
511 int len;
[0daba212]512 int rc;
[1313ee9]513
[2c448fb]514 if (last < next)
515 last += PLB_SIZE;
[1313ee9]516
[b6035ba]517 fs_node_t *par = NULL;
[0daba212]518 fs_node_t *cur = NULL;
[b6035ba]519 fs_node_t *tmp = NULL;
[1313ee9]520
[15f3c3f]521 rc = ops->root_get(&cur, service_id);
[0daba212]522 on_error(rc, goto out_with_answer);
[1313ee9]523
[83937ccd]524 if (cur->mp_data.mp_active) {
[79ae36dd]525 async_exch_t *exch = async_exchange_begin(cur->mp_data.sess);
526 async_forward_slow(rid, exch, VFS_OUT_LOOKUP, next, last,
[86ffa27f]527 cur->mp_data.service_id, lflag, index,
[efcebe1]528 IPC_FF_ROUTE_FROM_ME);
[79ae36dd]529 async_exchange_end(exch);
530
[0daba212]531 (void) ops->node_put(cur);
[83937ccd]532 return;
533 }
[1313ee9]534
535 /* Eat slash */
[efcebe1]536 if (plb_get_char(next) == '/')
[1313ee9]537 next++;
[2c448fb]538
[0daba212]539 while (next <= last) {
540 bool has_children;
[1313ee9]541
[0daba212]542 rc = ops->has_children(&has_children, cur);
543 on_error(rc, goto out_with_answer);
544 if (!has_children)
545 break;
[1313ee9]546
547 /* Collect the component */
[c9f6e49f]548 len = 0;
[efcebe1]549 while ((next <= last) && (plb_get_char(next) != '/')) {
[2c448fb]550 if (len + 1 == NAME_MAX) {
[1313ee9]551 /* Component length overflow */
[ffa2c8ef]552 async_answer_0(rid, ENAMETOOLONG);
[06901c6b]553 goto out;
[2c448fb]554 }
[efcebe1]555 component[len++] = plb_get_char(next);
[1313ee9]556 /* Process next character */
557 next++;
[2c448fb]558 }
[1313ee9]559
[2c448fb]560 assert(len);
561 component[len] = '\0';
[1313ee9]562 /* Eat slash */
563 next++;
564
565 /* Match the component */
[0daba212]566 rc = ops->match(&tmp, cur, component);
567 on_error(rc, goto out_with_answer);
[1313ee9]568
[4b995b92]569 /*
570 * If the matching component is a mount point, there are two
571 * legitimate semantics of the lookup operation. The first is
572 * the commonly used one in which the lookup crosses each mount
573 * point into the mounted file system. The second semantics is
574 * used mostly during unmount() and differs from the first one
575 * only in that the last mount point in the looked up path,
576 * which is also its last component, is not crossed.
577 */
578
579 if ((tmp) && (tmp->mp_data.mp_active) &&
[f7376cbf]580 (!(lflag & L_MP) || (next <= last))) {
[83937ccd]581 if (next > last)
582 next = last = first;
583 else
584 next--;
[1313ee9]585
[79ae36dd]586 async_exch_t *exch = async_exchange_begin(tmp->mp_data.sess);
[efcebe1]587 async_forward_slow(rid, exch, VFS_OUT_LOOKUP, next,
[86ffa27f]588 last, tmp->mp_data.service_id, lflag, index,
[79ae36dd]589 IPC_FF_ROUTE_FROM_ME);
590 async_exchange_end(exch);
591
[0daba212]592 (void) ops->node_put(cur);
593 (void) ops->node_put(tmp);
[83937ccd]594 if (par)
[0daba212]595 (void) ops->node_put(par);
[83937ccd]596 return;
597 }
[1313ee9]598
599 /* Handle miss: match amongst siblings */
[2c448fb]600 if (!tmp) {
[a8e9ab8d]601 if (next <= last) {
[1313ee9]602 /* There are unprocessed components */
[ffa2c8ef]603 async_answer_0(rid, ENOENT);
[06901c6b]604 goto out;
[a8e9ab8d]605 }
[1313ee9]606
607 /* Miss in the last component */
608 if (lflag & (L_CREATE | L_LINK)) {
609 /* Request to create a new link */
[2c448fb]610 if (!ops->is_directory(cur)) {
[ffa2c8ef]611 async_answer_0(rid, ENOTDIR);
[06901c6b]612 goto out;
[a8e9ab8d]613 }
[1313ee9]614
[b6035ba]615 fs_node_t *fn;
[a8e9ab8d]616 if (lflag & L_CREATE)
[15f3c3f]617 rc = ops->create(&fn, service_id,
[0daba212]618 lflag);
[a8e9ab8d]619 else
[15f3c3f]620 rc = ops->node_get(&fn, service_id,
[34f62f8]621 index);
[0daba212]622 on_error(rc, goto out_with_answer);
[1313ee9]623
[b6035ba]624 if (fn) {
625 rc = ops->link(cur, fn, component);
[0013b9ce]626 if (rc != EOK) {
[0daba212]627 if (lflag & L_CREATE)
628 (void) ops->destroy(fn);
[b946bf83]629 else
630 (void) ops->node_put(fn);
[ffa2c8ef]631 async_answer_0(rid, rc);
[2c448fb]632 } else {
[ed903174]633 aoff64_t size = ops->size_get(fn);
[ffa2c8ef]634 async_answer_5(rid, fs_handle,
[15f3c3f]635 service_id,
[b6035ba]636 ops->index_get(fn),
[ed903174]637 LOWER32(size),
638 UPPER32(size),
[b6035ba]639 ops->lnkcnt_get(fn));
[0daba212]640 (void) ops->node_put(fn);
[2c448fb]641 }
[1313ee9]642 } else
[ffa2c8ef]643 async_answer_0(rid, ENOSPC);
[1313ee9]644
[06901c6b]645 goto out;
[1313ee9]646 }
647
[ffa2c8ef]648 async_answer_0(rid, ENOENT);
[06901c6b]649 goto out;
[2c448fb]650 }
[1313ee9]651
[0daba212]652 if (par) {
653 rc = ops->node_put(par);
654 on_error(rc, goto out_with_answer);
655 }
[1313ee9]656
657 /* Descend one level */
[7b6d98b]658 par = cur;
[2c448fb]659 cur = tmp;
[06901c6b]660 tmp = NULL;
[2c448fb]661 }
[1313ee9]662
663 /* Handle miss: excessive components */
[0daba212]664 if (next <= last) {
665 bool has_children;
666 rc = ops->has_children(&has_children, cur);
667 on_error(rc, goto out_with_answer);
[1313ee9]668
[0daba212]669 if (has_children)
670 goto skip_miss;
[1313ee9]671
[a8e9ab8d]672 if (lflag & (L_CREATE | L_LINK)) {
[2c448fb]673 if (!ops->is_directory(cur)) {
[ffa2c8ef]674 async_answer_0(rid, ENOTDIR);
[06901c6b]675 goto out;
[2c448fb]676 }
[1313ee9]677
678 /* Collect next component */
[c9f6e49f]679 len = 0;
[2c448fb]680 while (next <= last) {
[efcebe1]681 if (plb_get_char(next) == '/') {
[1313ee9]682 /* More than one component */
[ffa2c8ef]683 async_answer_0(rid, ENOENT);
[06901c6b]684 goto out;
[2c448fb]685 }
[1313ee9]686
[2c448fb]687 if (len + 1 == NAME_MAX) {
[1313ee9]688 /* Component length overflow */
[ffa2c8ef]689 async_answer_0(rid, ENAMETOOLONG);
[06901c6b]690 goto out;
[2c448fb]691 }
[1313ee9]692
[efcebe1]693 component[len++] = plb_get_char(next);
[1313ee9]694 /* Process next character */
695 next++;
[2c448fb]696 }
[1313ee9]697
[2c448fb]698 assert(len);
699 component[len] = '\0';
[1313ee9]700
[b6035ba]701 fs_node_t *fn;
[a8e9ab8d]702 if (lflag & L_CREATE)
[15f3c3f]703 rc = ops->create(&fn, service_id, lflag);
[a8e9ab8d]704 else
[15f3c3f]705 rc = ops->node_get(&fn, service_id, index);
[0daba212]706 on_error(rc, goto out_with_answer);
[1313ee9]707
[b6035ba]708 if (fn) {
709 rc = ops->link(cur, fn, component);
[0013b9ce]710 if (rc != EOK) {
[a8e9ab8d]711 if (lflag & L_CREATE)
[0daba212]712 (void) ops->destroy(fn);
[b946bf83]713 else
714 (void) ops->node_put(fn);
[ffa2c8ef]715 async_answer_0(rid, rc);
[2c448fb]716 } else {
[ed903174]717 aoff64_t size = ops->size_get(fn);
[ffa2c8ef]718 async_answer_5(rid, fs_handle,
[15f3c3f]719 service_id,
[b6035ba]720 ops->index_get(fn),
[ed903174]721 LOWER32(size),
722 UPPER32(size),
[b6035ba]723 ops->lnkcnt_get(fn));
[0daba212]724 (void) ops->node_put(fn);
[2c448fb]725 }
[1313ee9]726 } else
[ffa2c8ef]727 async_answer_0(rid, ENOSPC);
[1313ee9]728
[06901c6b]729 goto out;
[2c448fb]730 }
[1313ee9]731
[ffa2c8ef]732 async_answer_0(rid, ENOENT);
[06901c6b]733 goto out;
[2c448fb]734 }
[1313ee9]735
[0daba212]736skip_miss:
[1313ee9]737
738 /* Handle hit */
[a8e9ab8d]739 if (lflag & L_UNLINK) {
[1313ee9]740 unsigned int old_lnkcnt = ops->lnkcnt_get(cur);
[0daba212]741 rc = ops->unlink(par, cur, component);
[ed903174]742
743 if (rc == EOK) {
744 aoff64_t size = ops->size_get(cur);
[15f3c3f]745 async_answer_5(rid, fs_handle, service_id,
[ed903174]746 ops->index_get(cur), LOWER32(size), UPPER32(size),
747 old_lnkcnt);
748 } else
[ffa2c8ef]749 async_answer_0(rid, rc);
[ed903174]750
[06901c6b]751 goto out;
[2c448fb]752 }
[1313ee9]753
[a8e9ab8d]754 if (((lflag & (L_CREATE | L_EXCLUSIVE)) == (L_CREATE | L_EXCLUSIVE)) ||
755 (lflag & L_LINK)) {
[ffa2c8ef]756 async_answer_0(rid, EEXIST);
[06901c6b]757 goto out;
[2c448fb]758 }
[1313ee9]759
[2c448fb]760 if ((lflag & L_FILE) && (ops->is_directory(cur))) {
[ffa2c8ef]761 async_answer_0(rid, EISDIR);
[06901c6b]762 goto out;
[2c448fb]763 }
[1313ee9]764
[2c448fb]765 if ((lflag & L_DIRECTORY) && (ops->is_file(cur))) {
[ffa2c8ef]766 async_answer_0(rid, ENOTDIR);
[06901c6b]767 goto out;
[2c448fb]768 }
[f7376cbf]769
770 if ((lflag & L_ROOT) && par) {
[ffa2c8ef]771 async_answer_0(rid, EINVAL);
[f7376cbf]772 goto out;
773 }
[1313ee9]774
[0daba212]775out_with_answer:
[1313ee9]776
[0daba212]777 if (rc == EOK) {
[1313ee9]778 if (lflag & L_OPEN)
779 rc = ops->node_open(cur);
780
[ed903174]781 if (rc == EOK) {
782 aoff64_t size = ops->size_get(cur);
[15f3c3f]783 async_answer_5(rid, fs_handle, service_id,
[ed903174]784 ops->index_get(cur), LOWER32(size), UPPER32(size),
785 ops->lnkcnt_get(cur));
786 } else
[ffa2c8ef]787 async_answer_0(rid, rc);
[ed903174]788
[1313ee9]789 } else
[ffa2c8ef]790 async_answer_0(rid, rc);
[1313ee9]791
[06901c6b]792out:
[1313ee9]793
[06901c6b]794 if (par)
[0daba212]795 (void) ops->node_put(par);
[1313ee9]796
[06901c6b]797 if (cur)
[0daba212]798 (void) ops->node_put(cur);
[1313ee9]799
[06901c6b]800 if (tmp)
[0daba212]801 (void) ops->node_put(tmp);
[2c448fb]802}
803
[75160a6]804void libfs_stat(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
805 ipc_call_t *request)
806{
[15f3c3f]807 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*request);
[75160a6]808 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
[1313ee9]809
[0daba212]810 fs_node_t *fn;
[15f3c3f]811 int rc = ops->node_get(&fn, service_id, index);
[0daba212]812 on_error(rc, answer_and_return(rid, rc));
[1313ee9]813
[75160a6]814 ipc_callid_t callid;
815 size_t size;
[1313ee9]816 if ((!async_data_read_receive(&callid, &size)) ||
817 (size != sizeof(struct stat))) {
818 ops->node_put(fn);
[ffa2c8ef]819 async_answer_0(callid, EINVAL);
820 async_answer_0(rid, EINVAL);
[75160a6]821 return;
822 }
[1313ee9]823
[0143f72]824 struct stat stat;
825 memset(&stat, 0, sizeof(struct stat));
[75160a6]826
[0143f72]827 stat.fs_handle = fs_handle;
[15f3c3f]828 stat.service_id = service_id;
[0143f72]829 stat.index = index;
[1313ee9]830 stat.lnkcnt = ops->lnkcnt_get(fn);
[0143f72]831 stat.is_file = ops->is_file(fn);
[1313ee9]832 stat.is_directory = ops->is_directory(fn);
[0143f72]833 stat.size = ops->size_get(fn);
[b33870b]834 stat.service = ops->service_get(fn);
[1313ee9]835
836 ops->node_put(fn);
837
[0da4e41]838 async_data_read_finalize(callid, &stat, sizeof(struct stat));
[ffa2c8ef]839 async_answer_0(rid, EOK);
[75160a6]840}
841
[595edf5]842/** Open VFS triplet.
843 *
[1313ee9]844 * @param ops libfs operations structure with function pointers to
845 * file system implementation
846 * @param rid Request ID of the VFS_OUT_OPEN_NODE request.
847 * @param request VFS_OUT_OPEN_NODE request data itself.
[595edf5]848 *
849 */
850void libfs_open_node(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
851 ipc_call_t *request)
852{
[15f3c3f]853 service_id_t service_id = IPC_GET_ARG1(*request);
[595edf5]854 fs_index_t index = IPC_GET_ARG2(*request);
855
[ed903174]856 fs_node_t *fn;
[15f3c3f]857 int rc = ops->node_get(&fn, service_id, index);
[0daba212]858 on_error(rc, answer_and_return(rid, rc));
[595edf5]859
[0daba212]860 if (fn == NULL) {
[ffa2c8ef]861 async_answer_0(rid, ENOENT);
[595edf5]862 return;
863 }
864
[1313ee9]865 rc = ops->node_open(fn);
[ed903174]866 aoff64_t size = ops->size_get(fn);
[efcebe1]867 async_answer_4(rid, rc, LOWER32(size), UPPER32(size),
868 ops->lnkcnt_get(fn),
[2e983c7]869 (ops->is_file(fn) ? L_FILE : 0) | (ops->is_directory(fn) ? L_DIRECTORY : 0));
[595edf5]870
[0daba212]871 (void) ops->node_put(fn);
[595edf5]872}
873
[5bf76c1]874static FIBRIL_MUTEX_INITIALIZE(instances_mutex);
875static LIST_INITIALIZE(instances_list);
876
877typedef struct {
878 service_id_t service_id;
879 link_t link;
880 void *data;
881} fs_instance_t;
882
883int fs_instance_create(service_id_t service_id, void *data)
884{
885 fs_instance_t *inst = malloc(sizeof(fs_instance_t));
886 if (!inst)
887 return ENOMEM;
888
889 link_initialize(&inst->link);
890 inst->service_id = service_id;
891 inst->data = data;
892
893 fibril_mutex_lock(&instances_mutex);
894 list_foreach(instances_list, link) {
895 fs_instance_t *cur = list_get_instance(link, fs_instance_t,
896 link);
897
898 if (cur->service_id == service_id) {
899 fibril_mutex_unlock(&instances_mutex);
900 free(inst);
901 return EEXIST;
902 }
903
904 /* keep the list sorted */
905 if (cur->service_id < service_id) {
906 list_insert_before(&inst->link, &cur->link);
907 fibril_mutex_unlock(&instances_mutex);
908 return EOK;
909 }
910 }
911 list_append(&inst->link, &instances_list);
912 fibril_mutex_unlock(&instances_mutex);
913
914 return EOK;
915}
916
917int fs_instance_get(service_id_t service_id, void **idp)
918{
919 fibril_mutex_lock(&instances_mutex);
920 list_foreach(instances_list, link) {
921 fs_instance_t *inst = list_get_instance(link, fs_instance_t,
922 link);
923
924 if (inst->service_id == service_id) {
925 *idp = inst->data;
926 fibril_mutex_unlock(&instances_mutex);
927 return EOK;
928 }
929 }
930 fibril_mutex_unlock(&instances_mutex);
931 return ENOENT;
932}
933
934int fs_instance_destroy(service_id_t service_id)
935{
936 fibril_mutex_lock(&instances_mutex);
937 list_foreach(instances_list, link) {
938 fs_instance_t *inst = list_get_instance(link, fs_instance_t,
939 link);
940
941 if (inst->service_id == service_id) {
942 list_remove(&inst->link);
943 fibril_mutex_unlock(&instances_mutex);
944 free(inst);
945 return EOK;
946 }
947 }
948 fibril_mutex_unlock(&instances_mutex);
949 return ENOENT;
950}
951
[74303b6]952/** @}
953 */
Note: See TracBrowser for help on using the repository browser.