source: mainline/uspace/lib/fs/libfs.c@ b272c67a

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

unify interface API

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