source: mainline/uspace/lib/fs/libfs.c@ 8c95dff

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8c95dff was 2d884ab, checked in by Jakub Jermar <jakub@…>, 13 years ago

Libfs now opens file even when creating new file (L_CREATE & L_OPEN).

  • Property mode set to 100644
File size: 22.6 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);
[faba839]341 if (reg.plb_ro == AS_MAP_FAILED) {
[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 {
[2d884ab]633 (void) ops->node_put(cur);
634 cur = fn;
635 goto out_with_answer;
[2c448fb]636 }
[1313ee9]637 } else
[ffa2c8ef]638 async_answer_0(rid, ENOSPC);
[1313ee9]639
[06901c6b]640 goto out;
[1313ee9]641 }
642
[ffa2c8ef]643 async_answer_0(rid, ENOENT);
[06901c6b]644 goto out;
[2c448fb]645 }
[1313ee9]646
[0daba212]647 if (par) {
648 rc = ops->node_put(par);
649 on_error(rc, goto out_with_answer);
650 }
[1313ee9]651
652 /* Descend one level */
[7b6d98b]653 par = cur;
[2c448fb]654 cur = tmp;
[06901c6b]655 tmp = NULL;
[2c448fb]656 }
[1313ee9]657
658 /* Handle miss: excessive components */
[0daba212]659 if (next <= last) {
660 bool has_children;
661 rc = ops->has_children(&has_children, cur);
662 on_error(rc, goto out_with_answer);
[1313ee9]663
[0daba212]664 if (has_children)
665 goto skip_miss;
[1313ee9]666
[a8e9ab8d]667 if (lflag & (L_CREATE | L_LINK)) {
[2c448fb]668 if (!ops->is_directory(cur)) {
[ffa2c8ef]669 async_answer_0(rid, ENOTDIR);
[06901c6b]670 goto out;
[2c448fb]671 }
[1313ee9]672
673 /* Collect next component */
[c9f6e49f]674 len = 0;
[2c448fb]675 while (next <= last) {
[efcebe1]676 if (plb_get_char(next) == '/') {
[1313ee9]677 /* More than one component */
[ffa2c8ef]678 async_answer_0(rid, ENOENT);
[06901c6b]679 goto out;
[2c448fb]680 }
[1313ee9]681
[2c448fb]682 if (len + 1 == NAME_MAX) {
[1313ee9]683 /* Component length overflow */
[ffa2c8ef]684 async_answer_0(rid, ENAMETOOLONG);
[06901c6b]685 goto out;
[2c448fb]686 }
[1313ee9]687
[efcebe1]688 component[len++] = plb_get_char(next);
[1313ee9]689 /* Process next character */
690 next++;
[2c448fb]691 }
[1313ee9]692
[2c448fb]693 assert(len);
694 component[len] = '\0';
[1313ee9]695
[b6035ba]696 fs_node_t *fn;
[a8e9ab8d]697 if (lflag & L_CREATE)
[15f3c3f]698 rc = ops->create(&fn, service_id, lflag);
[a8e9ab8d]699 else
[15f3c3f]700 rc = ops->node_get(&fn, service_id, index);
[0daba212]701 on_error(rc, goto out_with_answer);
[1313ee9]702
[b6035ba]703 if (fn) {
704 rc = ops->link(cur, fn, component);
[0013b9ce]705 if (rc != EOK) {
[a8e9ab8d]706 if (lflag & L_CREATE)
[0daba212]707 (void) ops->destroy(fn);
[b946bf83]708 else
709 (void) ops->node_put(fn);
[ffa2c8ef]710 async_answer_0(rid, rc);
[2c448fb]711 } else {
[2d884ab]712 (void) ops->node_put(cur);
713 cur = fn;
714 goto out_with_answer;
[2c448fb]715 }
[1313ee9]716 } else
[ffa2c8ef]717 async_answer_0(rid, ENOSPC);
[1313ee9]718
[06901c6b]719 goto out;
[2c448fb]720 }
[1313ee9]721
[ffa2c8ef]722 async_answer_0(rid, ENOENT);
[06901c6b]723 goto out;
[2c448fb]724 }
[1313ee9]725
[0daba212]726skip_miss:
[1313ee9]727
728 /* Handle hit */
[a8e9ab8d]729 if (lflag & L_UNLINK) {
[1313ee9]730 unsigned int old_lnkcnt = ops->lnkcnt_get(cur);
[0daba212]731 rc = ops->unlink(par, cur, component);
[ed903174]732
733 if (rc == EOK) {
734 aoff64_t size = ops->size_get(cur);
[15f3c3f]735 async_answer_5(rid, fs_handle, service_id,
[ed903174]736 ops->index_get(cur), LOWER32(size), UPPER32(size),
737 old_lnkcnt);
738 } else
[ffa2c8ef]739 async_answer_0(rid, rc);
[ed903174]740
[06901c6b]741 goto out;
[2c448fb]742 }
[1313ee9]743
[a8e9ab8d]744 if (((lflag & (L_CREATE | L_EXCLUSIVE)) == (L_CREATE | L_EXCLUSIVE)) ||
745 (lflag & L_LINK)) {
[ffa2c8ef]746 async_answer_0(rid, EEXIST);
[06901c6b]747 goto out;
[2c448fb]748 }
[1313ee9]749
[2c448fb]750 if ((lflag & L_FILE) && (ops->is_directory(cur))) {
[ffa2c8ef]751 async_answer_0(rid, EISDIR);
[06901c6b]752 goto out;
[2c448fb]753 }
[1313ee9]754
[2c448fb]755 if ((lflag & L_DIRECTORY) && (ops->is_file(cur))) {
[ffa2c8ef]756 async_answer_0(rid, ENOTDIR);
[06901c6b]757 goto out;
[2c448fb]758 }
[f7376cbf]759
760 if ((lflag & L_ROOT) && par) {
[ffa2c8ef]761 async_answer_0(rid, EINVAL);
[f7376cbf]762 goto out;
763 }
[1313ee9]764
[0daba212]765out_with_answer:
[1313ee9]766
[0daba212]767 if (rc == EOK) {
[1313ee9]768 if (lflag & L_OPEN)
769 rc = ops->node_open(cur);
770
[ed903174]771 if (rc == EOK) {
772 aoff64_t size = ops->size_get(cur);
[15f3c3f]773 async_answer_5(rid, fs_handle, service_id,
[ed903174]774 ops->index_get(cur), LOWER32(size), UPPER32(size),
775 ops->lnkcnt_get(cur));
776 } else
[ffa2c8ef]777 async_answer_0(rid, rc);
[ed903174]778
[1313ee9]779 } else
[ffa2c8ef]780 async_answer_0(rid, rc);
[1313ee9]781
[06901c6b]782out:
[1313ee9]783
[06901c6b]784 if (par)
[0daba212]785 (void) ops->node_put(par);
[1313ee9]786
[06901c6b]787 if (cur)
[0daba212]788 (void) ops->node_put(cur);
[1313ee9]789
[06901c6b]790 if (tmp)
[0daba212]791 (void) ops->node_put(tmp);
[2c448fb]792}
793
[75160a6]794void libfs_stat(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
795 ipc_call_t *request)
796{
[15f3c3f]797 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*request);
[75160a6]798 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
[1313ee9]799
[0daba212]800 fs_node_t *fn;
[15f3c3f]801 int rc = ops->node_get(&fn, service_id, index);
[0daba212]802 on_error(rc, answer_and_return(rid, rc));
[1313ee9]803
[75160a6]804 ipc_callid_t callid;
805 size_t size;
[1313ee9]806 if ((!async_data_read_receive(&callid, &size)) ||
807 (size != sizeof(struct stat))) {
808 ops->node_put(fn);
[ffa2c8ef]809 async_answer_0(callid, EINVAL);
810 async_answer_0(rid, EINVAL);
[75160a6]811 return;
812 }
[1313ee9]813
[0143f72]814 struct stat stat;
815 memset(&stat, 0, sizeof(struct stat));
[75160a6]816
[0143f72]817 stat.fs_handle = fs_handle;
[15f3c3f]818 stat.service_id = service_id;
[0143f72]819 stat.index = index;
[1313ee9]820 stat.lnkcnt = ops->lnkcnt_get(fn);
[0143f72]821 stat.is_file = ops->is_file(fn);
[1313ee9]822 stat.is_directory = ops->is_directory(fn);
[0143f72]823 stat.size = ops->size_get(fn);
[b33870b]824 stat.service = ops->service_get(fn);
[1313ee9]825
826 ops->node_put(fn);
827
[0da4e41]828 async_data_read_finalize(callid, &stat, sizeof(struct stat));
[ffa2c8ef]829 async_answer_0(rid, EOK);
[75160a6]830}
831
[595edf5]832/** Open VFS triplet.
833 *
[1313ee9]834 * @param ops libfs operations structure with function pointers to
835 * file system implementation
836 * @param rid Request ID of the VFS_OUT_OPEN_NODE request.
837 * @param request VFS_OUT_OPEN_NODE request data itself.
[595edf5]838 *
839 */
840void libfs_open_node(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
841 ipc_call_t *request)
842{
[15f3c3f]843 service_id_t service_id = IPC_GET_ARG1(*request);
[595edf5]844 fs_index_t index = IPC_GET_ARG2(*request);
845
[ed903174]846 fs_node_t *fn;
[15f3c3f]847 int rc = ops->node_get(&fn, service_id, index);
[0daba212]848 on_error(rc, answer_and_return(rid, rc));
[595edf5]849
[0daba212]850 if (fn == NULL) {
[ffa2c8ef]851 async_answer_0(rid, ENOENT);
[595edf5]852 return;
853 }
854
[1313ee9]855 rc = ops->node_open(fn);
[ed903174]856 aoff64_t size = ops->size_get(fn);
[efcebe1]857 async_answer_4(rid, rc, LOWER32(size), UPPER32(size),
858 ops->lnkcnt_get(fn),
[2e983c7]859 (ops->is_file(fn) ? L_FILE : 0) | (ops->is_directory(fn) ? L_DIRECTORY : 0));
[595edf5]860
[0daba212]861 (void) ops->node_put(fn);
[595edf5]862}
863
[5bf76c1]864static FIBRIL_MUTEX_INITIALIZE(instances_mutex);
865static LIST_INITIALIZE(instances_list);
866
867typedef struct {
868 service_id_t service_id;
869 link_t link;
870 void *data;
871} fs_instance_t;
872
873int fs_instance_create(service_id_t service_id, void *data)
874{
875 fs_instance_t *inst = malloc(sizeof(fs_instance_t));
876 if (!inst)
877 return ENOMEM;
878
879 link_initialize(&inst->link);
880 inst->service_id = service_id;
881 inst->data = data;
882
883 fibril_mutex_lock(&instances_mutex);
884 list_foreach(instances_list, link) {
885 fs_instance_t *cur = list_get_instance(link, fs_instance_t,
886 link);
887
888 if (cur->service_id == service_id) {
889 fibril_mutex_unlock(&instances_mutex);
890 free(inst);
891 return EEXIST;
892 }
893
894 /* keep the list sorted */
895 if (cur->service_id < service_id) {
896 list_insert_before(&inst->link, &cur->link);
897 fibril_mutex_unlock(&instances_mutex);
898 return EOK;
899 }
900 }
901 list_append(&inst->link, &instances_list);
902 fibril_mutex_unlock(&instances_mutex);
903
904 return EOK;
905}
906
907int fs_instance_get(service_id_t service_id, void **idp)
908{
909 fibril_mutex_lock(&instances_mutex);
910 list_foreach(instances_list, link) {
911 fs_instance_t *inst = list_get_instance(link, fs_instance_t,
912 link);
913
914 if (inst->service_id == service_id) {
915 *idp = inst->data;
916 fibril_mutex_unlock(&instances_mutex);
917 return EOK;
918 }
919 }
920 fibril_mutex_unlock(&instances_mutex);
921 return ENOENT;
922}
923
924int fs_instance_destroy(service_id_t service_id)
925{
926 fibril_mutex_lock(&instances_mutex);
927 list_foreach(instances_list, link) {
928 fs_instance_t *inst = list_get_instance(link, fs_instance_t,
929 link);
930
931 if (inst->service_id == service_id) {
932 list_remove(&inst->link);
933 fibril_mutex_unlock(&instances_mutex);
934 free(inst);
935 return EOK;
936 }
937 }
938 fibril_mutex_unlock(&instances_mutex);
939 return ENOENT;
940}
941
[74303b6]942/** @}
943 */
Note: See TracBrowser for help on using the repository browser.