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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ce04ea44 was 6e5562a, checked in by Jakub Jermar <jakub@…>, 9 years ago

Introduce vfs_link_path() and replace mkdir() with it

  • Property mode set to 100644
File size: 21.2 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"
[ed903174]38#include <macros.h>
[efd4a72]39#include <errno.h>
40#include <async.h>
41#include <as.h>
[2c448fb]42#include <assert.h>
43#include <dirent.h>
[595edf5]44#include <mem.h>
[5a2b765]45#include <str.h>
[efcebe1]46#include <stdlib.h>
[b8dbe2f]47#include <fibril_synch.h>
[5a2b765]48#include <ipc/vfs.h>
[b5b5d84]49#include <vfs/vfs.h>
[efd4a72]50
[0daba212]51#define on_error(rc, action) \
52 do { \
53 if ((rc) != EOK) \
54 action; \
55 } while (0)
56
57#define combine_rc(rc1, rc2) \
58 ((rc1) == EOK ? (rc2) : (rc1))
59
60#define answer_and_return(rid, rc) \
61 do { \
[ffa2c8ef]62 async_answer_0((rid), (rc)); \
[0daba212]63 return; \
64 } while (0)
65
[efcebe1]66static fs_reg_t reg;
67
68static vfs_out_ops_t *vfs_out_ops = NULL;
69static libfs_ops_t *libfs_ops = NULL;
70
[5a2b765]71static char fs_name[FS_NAME_MAXLEN + 1];
72
[bf9dc4e2]73static void libfs_link(libfs_ops_t *, fs_handle_t, ipc_callid_t,
74 ipc_call_t *);
[efcebe1]75static void libfs_lookup(libfs_ops_t *, fs_handle_t, ipc_callid_t,
76 ipc_call_t *);
77static void libfs_stat(libfs_ops_t *, fs_handle_t, ipc_callid_t, ipc_call_t *);
78static void libfs_open_node(libfs_ops_t *, fs_handle_t, ipc_callid_t,
79 ipc_call_t *);
[61042de]80static void libfs_statfs(libfs_ops_t *, fs_handle_t, ipc_callid_t,
81 ipc_call_t *);
[efcebe1]82
83static void vfs_out_mounted(ipc_callid_t rid, ipc_call_t *req)
84{
[86ffa27f]85 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*req);
[efcebe1]86 char *opts;
87 int rc;
88
89 /* Accept the mount options. */
90 rc = async_data_write_accept((void **) &opts, true, 0, 0, 0, NULL);
91 if (rc != EOK) {
92 async_answer_0(rid, rc);
93 return;
94 }
95
96 fs_index_t index;
97 aoff64_t size;
98 unsigned lnkcnt;
[86ffa27f]99 rc = vfs_out_ops->mounted(service_id, opts, &index, &size, &lnkcnt);
[efcebe1]100
[7eb0fed8]101 if (rc == EOK)
102 async_answer_4(rid, EOK, index, LOWER32(size), UPPER32(size),
103 lnkcnt);
[efcebe1]104 else
105 async_answer_0(rid, rc);
106
107 free(opts);
108}
109
110static void vfs_out_unmounted(ipc_callid_t rid, ipc_call_t *req)
111{
[86ffa27f]112 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*req);
[efcebe1]113 int rc;
114
[86ffa27f]115 rc = vfs_out_ops->unmounted(service_id);
[efcebe1]116
117 async_answer_0(rid, rc);
118}
119
[bf9dc4e2]120static void vfs_out_link(ipc_callid_t rid, ipc_call_t *req)
[efcebe1]121{
[bf9dc4e2]122 libfs_link(libfs_ops, reg.fs_handle, rid, req);
[efcebe1]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
[61042de]159 if (rc == EOK) {
160 async_answer_3(rid, EOK, wbytes, LOWER32(nsize),
161 UPPER32(nsize));
162 } else
[efcebe1]163 async_answer_0(rid, rc);
164}
165
166static void vfs_out_truncate(ipc_callid_t rid, ipc_call_t *req)
167{
[86ffa27f]168 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*req);
[efcebe1]169 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*req);
170 aoff64_t size = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*req),
171 IPC_GET_ARG4(*req));
172 int rc;
173
[86ffa27f]174 rc = vfs_out_ops->truncate(service_id, index, size);
[efcebe1]175
176 async_answer_0(rid, rc);
177}
178
179static void vfs_out_close(ipc_callid_t rid, ipc_call_t *req)
180{
[86ffa27f]181 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*req);
[efcebe1]182 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*req);
183 int rc;
184
[86ffa27f]185 rc = vfs_out_ops->close(service_id, index);
[efcebe1]186
187 async_answer_0(rid, rc);
188}
189
190static void vfs_out_destroy(ipc_callid_t rid, ipc_call_t *req)
191{
[86ffa27f]192 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*req);
[efcebe1]193 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*req);
194
[b7c62a9]195 int rc;
196 fs_node_t *node = NULL;
197 rc = libfs_ops->node_get(&node, service_id, index);
198 if (rc == EOK && node != NULL) {
199 bool destroy = (libfs_ops->lnkcnt_get(node) == 0);
200 libfs_ops->node_put(node);
[61042de]201 if (destroy)
[b7c62a9]202 rc = vfs_out_ops->destroy(service_id, index);
203 }
[efcebe1]204 async_answer_0(rid, rc);
205}
206
207static void vfs_out_open_node(ipc_callid_t rid, ipc_call_t *req)
208{
209 libfs_open_node(libfs_ops, reg.fs_handle, rid, req);
210}
211
212static void vfs_out_stat(ipc_callid_t rid, ipc_call_t *req)
213{
214 libfs_stat(libfs_ops, reg.fs_handle, rid, req);
215}
216
217static void vfs_out_sync(ipc_callid_t rid, ipc_call_t *req)
218{
[86ffa27f]219 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*req);
[efcebe1]220 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*req);
221 int rc;
222
[86ffa27f]223 rc = vfs_out_ops->sync(service_id, index);
[efcebe1]224
225 async_answer_0(rid, rc);
226}
227
[5930e3f]228static void vfs_out_statfs(ipc_callid_t rid, ipc_call_t *req)
229{
230 libfs_statfs(libfs_ops, reg.fs_handle, rid, req);
[9dc6083]231}
[1dff985]232
[677745a]233static void vfs_out_get_size(ipc_callid_t rid, ipc_call_t *req)
234{
235 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*req);
236 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*req);
237 int rc;
238
239 fs_node_t *node = NULL;
240 rc = libfs_ops->node_get(&node, service_id, index);
[61042de]241 if (rc != EOK)
[677745a]242 async_answer_0(rid, rc);
[61042de]243 if (node == NULL)
[677745a]244 async_answer_0(rid, EINVAL);
245
246 uint64_t size = libfs_ops->size_get(node);
247 libfs_ops->node_put(node);
248
249 async_answer_2(rid, EOK, LOWER32(size), UPPER32(size));
250}
251
[5bcd5b7]252static void vfs_out_is_empty(ipc_callid_t rid, ipc_call_t *req)
253{
254 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*req);
255 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*req);
256 int rc;
257
258 fs_node_t *node = NULL;
259 rc = libfs_ops->node_get(&node, service_id, index);
[61042de]260 if (rc != EOK)
[5bcd5b7]261 async_answer_0(rid, rc);
[61042de]262 if (node == NULL)
[5bcd5b7]263 async_answer_0(rid, EINVAL);
264
265 bool children = false;
266 rc = libfs_ops->has_children(&children, node);
267 libfs_ops->node_put(node);
268
[61042de]269 if (rc != EOK)
[5bcd5b7]270 async_answer_0(rid, rc);
271 async_answer_0(rid, children ? ENOTEMPTY : EOK);
272}
273
[efcebe1]274static void vfs_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
275{
276 if (iid) {
277 /*
278 * This only happens for connections opened by
279 * IPC_M_CONNECT_ME_TO calls as opposed to callback connections
280 * created by IPC_M_CONNECT_TO_ME.
281 */
282 async_answer_0(iid, EOK);
283 }
284
285 while (true) {
286 ipc_call_t call;
287 ipc_callid_t callid = async_get_call(&call);
288
289 if (!IPC_GET_IMETHOD(call))
290 return;
291
292 switch (IPC_GET_IMETHOD(call)) {
293 case VFS_OUT_MOUNTED:
294 vfs_out_mounted(callid, &call);
295 break;
296 case VFS_OUT_UNMOUNTED:
297 vfs_out_unmounted(callid, &call);
298 break;
[bf9dc4e2]299 case VFS_OUT_LINK:
300 vfs_out_link(callid, &call);
[efcebe1]301 break;
302 case VFS_OUT_LOOKUP:
303 vfs_out_lookup(callid, &call);
304 break;
305 case VFS_OUT_READ:
306 vfs_out_read(callid, &call);
307 break;
308 case VFS_OUT_WRITE:
309 vfs_out_write(callid, &call);
310 break;
311 case VFS_OUT_TRUNCATE:
312 vfs_out_truncate(callid, &call);
313 break;
314 case VFS_OUT_CLOSE:
315 vfs_out_close(callid, &call);
316 break;
317 case VFS_OUT_DESTROY:
318 vfs_out_destroy(callid, &call);
319 break;
320 case VFS_OUT_OPEN_NODE:
321 vfs_out_open_node(callid, &call);
322 break;
323 case VFS_OUT_STAT:
324 vfs_out_stat(callid, &call);
325 break;
326 case VFS_OUT_SYNC:
327 vfs_out_sync(callid, &call);
328 break;
[9dc6083]329 case VFS_OUT_STATFS:
330 vfs_out_statfs(callid, &call);
331 break;
[677745a]332 case VFS_OUT_GET_SIZE:
333 vfs_out_get_size(callid, &call);
334 break;
[5bcd5b7]335 case VFS_OUT_IS_EMPTY:
336 vfs_out_is_empty(callid, &call);
337 break;
[efcebe1]338 default:
339 async_answer_0(callid, ENOTSUP);
340 break;
341 }
342 }
343}
344
[efd4a72]345/** Register file system server.
346 *
347 * This function abstracts away the tedious registration protocol from
348 * file system implementations and lets them to reuse this registration glue
349 * code.
350 *
[79ae36dd]351 * @param sess Session for communication with VFS.
352 * @param info VFS info structure supplied by the file system
353 * implementation.
[efcebe1]354 * @param vops Address of the vfs_out_ops_t structure.
355 * @param lops Address of the libfs_ops_t structure.
[1313ee9]356 *
357 * @return EOK on success or a non-zero error code on errror.
[efd4a72]358 *
359 */
[efcebe1]360int fs_register(async_sess_t *sess, vfs_info_t *info, vfs_out_ops_t *vops,
361 libfs_ops_t *lops)
[efd4a72]362{
363 /*
364 * Tell VFS that we are here and want to get registered.
365 * We use the async framework because VFS will answer the request
366 * out-of-order, when it knows that the operation succeeded or failed.
367 */
[79ae36dd]368
369 async_exch_t *exch = async_exchange_begin(sess);
370
[efd4a72]371 ipc_call_t answer;
[79ae36dd]372 aid_t req = async_send_0(exch, VFS_IN_REGISTER, &answer);
[1313ee9]373
[efd4a72]374 /*
375 * Send our VFS info structure to VFS.
376 */
[79ae36dd]377 int rc = async_data_write_start(exch, info, sizeof(*info));
378
[efd4a72]379 if (rc != EOK) {
[79ae36dd]380 async_exchange_end(exch);
[50b581d]381 async_forget(req);
[efd4a72]382 return rc;
383 }
[1313ee9]384
[efcebe1]385 /*
386 * Set VFS_OUT and libfs operations.
387 */
388 vfs_out_ops = vops;
389 libfs_ops = lops;
390
[5a2b765]391 str_cpy(fs_name, sizeof(fs_name), info->name);
392
[efd4a72]393 /*
394 * Ask VFS for callback connection.
395 */
[f9b2cb4c]396 port_id_t port;
397 rc = async_create_callback_port(exch, INTERFACE_VFS_DRIVER_CB, 0, 0,
398 vfs_connection, NULL, &port);
[1313ee9]399
[efd4a72]400 /*
[fbcdeb8]401 * Request sharing the Path Lookup Buffer with VFS.
[efd4a72]402 */
[fbcdeb8]403 rc = async_share_in_start_0_0(exch, PLB_SIZE, (void *) &reg.plb_ro);
[faba839]404 if (reg.plb_ro == AS_MAP_FAILED) {
[79ae36dd]405 async_exchange_end(exch);
[50b581d]406 async_forget(req);
[efd4a72]407 return ENOMEM;
408 }
[1313ee9]409
[79ae36dd]410 async_exchange_end(exch);
411
[efd4a72]412 if (rc) {
[50b581d]413 async_forget(req);
[efd4a72]414 return rc;
415 }
416
417 /*
[4198f9c3]418 * Pick up the answer for the request to the VFS_IN_REQUEST call.
[efd4a72]419 */
420 async_wait_for(req, NULL);
[efcebe1]421 reg.fs_handle = (int) IPC_GET_ARG1(answer);
[efd4a72]422
423 /*
424 * Tell the async framework that other connections are to be handled by
425 * the same connection fibril as well.
426 */
[b688fd8]427 async_set_fallback_port_handler(vfs_connection, NULL);
[1313ee9]428
[efd4a72]429 return IPC_GET_RETVAL(answer);
430}
[74303b6]431
[83937ccd]432void fs_node_initialize(fs_node_t *fn)
433{
434 memset(fn, 0, sizeof(fs_node_t));
435}
436
[efcebe1]437static char plb_get_char(unsigned pos)
[83937ccd]438{
[efcebe1]439 return reg.plb_ro[pos % PLB_SIZE];
440}
441
[61042de]442static int plb_get_component(char *dest, unsigned *sz, unsigned *ppos,
443 unsigned last)
[bf9dc4e2]444{
445 unsigned pos = *ppos;
446 unsigned size = 0;
[1313ee9]447
[bf9dc4e2]448 if (pos == last) {
449 *sz = 0;
450 return ERANGE;
[83937ccd]451 }
[bf9dc4e2]452
453 char c = plb_get_char(pos);
[61042de]454 if (c == '/')
[bf9dc4e2]455 pos++;
[1313ee9]456
[bf9dc4e2]457 for (int i = 0; i <= NAME_MAX; i++) {
458 c = plb_get_char(pos);
459 if (pos == last || c == '/') {
460 dest[i] = 0;
461 *ppos = pos;
462 *sz = size;
463 return EOK;
464 }
465 dest[i] = c;
466 pos++;
467 size++;
[83937ccd]468 }
[bf9dc4e2]469 return ENAMETOOLONG;
470}
471
472static int receive_fname(char *buffer)
473{
474 size_t size;
475 ipc_callid_t wcall;
[1313ee9]476
[61042de]477 if (!async_data_write_receive(&wcall, &size))
[bf9dc4e2]478 return ENOENT;
479 if (size > NAME_MAX + 1) {
480 async_answer_0(wcall, ERANGE);
481 return ERANGE;
[83937ccd]482 }
[bf9dc4e2]483 return async_data_write_finalize(wcall, buffer, size);
[83937ccd]484}
485
[bf9dc4e2]486/** Link a file at a path.
487 */
[61042de]488void libfs_link(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
489 ipc_call_t *req)
[3c11713]490{
[bf9dc4e2]491 service_id_t parent_sid = IPC_GET_ARG1(*req);
492 fs_index_t parent_index = IPC_GET_ARG2(*req);
493 fs_index_t child_index = IPC_GET_ARG3(*req);
494
495 char component[NAME_MAX + 1];
496 int rc = receive_fname(component);
497 if (rc != EOK) {
498 async_answer_0(rid, rc);
[c888102]499 return;
500 }
501
[bf9dc4e2]502 fs_node_t *parent = NULL;
503 rc = ops->node_get(&parent, parent_sid, parent_index);
504 if (parent == NULL) {
505 async_answer_0(rid, rc == EOK ? EBADF : rc);
[c888102]506 return;
507 }
[bf9dc4e2]508
509 fs_node_t *child = NULL;
510 rc = ops->node_get(&child, parent_sid, child_index);
511 if (child == NULL) {
512 async_answer_0(rid, rc == EOK ? EBADF : rc);
513 ops->node_put(parent);
514 return;
[c888102]515 }
[bf9dc4e2]516
517 rc = ops->link(parent, child, component);
518 ops->node_put(parent);
519 ops->node_put(child);
520 async_answer_0(rid, rc);
[efcebe1]521}
522
[1e50f81]523/** Lookup VFS triplet by name in the file system name space.
[9bb85f3]524 *
525 * The path passed in the PLB must be in the canonical file system path format
526 * as returned by the canonify() function.
[1e50f81]527 *
[595edf5]528 * @param ops libfs operations structure with function pointers to
529 * file system implementation
530 * @param fs_handle File system handle of the file system where to perform
531 * the lookup.
[4198f9c3]532 * @param rid Request ID of the VFS_OUT_LOOKUP request.
533 * @param request VFS_OUT_LOOKUP request data itself.
[595edf5]534 *
[1e50f81]535 */
[f2ec8c8]536void libfs_lookup(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
[efcebe1]537 ipc_call_t *req)
[2c448fb]538{
[bf9dc4e2]539 unsigned first = IPC_GET_ARG1(*req);
540 unsigned len = IPC_GET_ARG2(*req);
[86ffa27f]541 service_id_t service_id = IPC_GET_ARG3(*req);
[bf9dc4e2]542 fs_index_t index = IPC_GET_ARG4(*req);
543 int lflag = IPC_GET_ARG5(*req);
544
[06256b0]545 assert((int) index != -1);
546
[bf9dc4e2]547 // TODO: Validate flags.
548
549 unsigned next = first;
550 unsigned last = first + len;
551
[c9f6e49f]552 char component[NAME_MAX + 1];
[0daba212]553 int rc;
[1313ee9]554
[b6035ba]555 fs_node_t *par = NULL;
[0daba212]556 fs_node_t *cur = NULL;
[b6035ba]557 fs_node_t *tmp = NULL;
[bf9dc4e2]558 unsigned clen = 0;
[1313ee9]559
[06256b0]560 rc = ops->node_get(&cur, service_id, index);
[bf9dc4e2]561 if (rc != EOK) {
562 async_answer_0(rid, rc);
563 goto out;
[83937ccd]564 }
[1313ee9]565
[bf9dc4e2]566 assert(cur != NULL);
[2c448fb]567
[bf9dc4e2]568 /* Find the file and its parent. */
[2c448fb]569
[677745a]570 unsigned last_next = 0;
571
[bf9dc4e2]572 while (next != last) {
573 if (cur == NULL) {
[677745a]574 assert(par != NULL);
575 goto out1;
[bf9dc4e2]576 }
[677745a]577
[bf9dc4e2]578 if (!ops->is_directory(cur)) {
579 async_answer_0(rid, ENOTDIR);
580 goto out;
581 }
[1313ee9]582
[677745a]583 last_next = next;
[1313ee9]584 /* Collect the component */
[bf9dc4e2]585 rc = plb_get_component(component, &clen, &next, last);
586 assert(rc != ERANGE);
587 if (rc != EOK) {
588 async_answer_0(rid, rc);
589 goto out;
[2c448fb]590 }
[1313ee9]591
[bf9dc4e2]592 if (clen == 0) {
593 /* The path is just "/". */
594 break;
[2c448fb]595 }
[1313ee9]596
[bf9dc4e2]597 assert(component[clen] == 0);
[1313ee9]598
599 /* Match the component */
[0daba212]600 rc = ops->match(&tmp, cur, component);
[bf9dc4e2]601 if (rc != EOK) {
602 async_answer_0(rid, rc);
[06901c6b]603 goto out;
[2c448fb]604 }
[1313ee9]605
[bf9dc4e2]606 /* Descend one level */
[0daba212]607 if (par) {
608 rc = ops->node_put(par);
[bf9dc4e2]609 if (rc != EOK) {
610 async_answer_0(rid, rc);
611 goto out;
612 }
[0daba212]613 }
[1313ee9]614
[7b6d98b]615 par = cur;
[2c448fb]616 cur = tmp;
[06901c6b]617 tmp = NULL;
[2c448fb]618 }
[1313ee9]619
[bf9dc4e2]620 /* At this point, par is either NULL or a directory.
621 * If cur is NULL, the looked up file does not exist yet.
622 */
623
624 assert(par == NULL || ops->is_directory(par));
625 assert(par != NULL || cur != NULL);
626
627 /* Check for some error conditions. */
628
629 if (cur && (lflag & L_FILE) && (ops->is_directory(cur))) {
630 async_answer_0(rid, EISDIR);
631 goto out;
632 }
633
634 if (cur && (lflag & L_DIRECTORY) && (ops->is_file(cur))) {
635 async_answer_0(rid, ENOTDIR);
[06901c6b]636 goto out;
[2c448fb]637 }
[1313ee9]638
[bf9dc4e2]639 /* Unlink. */
[1313ee9]640
[a8e9ab8d]641 if (lflag & L_UNLINK) {
[bf9dc4e2]642 if (!cur) {
643 async_answer_0(rid, ENOENT);
644 goto out;
645 }
646 if (!par) {
647 async_answer_0(rid, EINVAL);
648 goto out;
649 }
[ed903174]650
[0daba212]651 rc = ops->unlink(par, cur, component);
[ed903174]652 if (rc == EOK) {
[677745a]653 int64_t size = ops->size_get(cur);
654 int32_t lsize = LOWER32(size);
[61042de]655 if (lsize != size)
[677745a]656 lsize = -1;
657
[15f3c3f]658 async_answer_5(rid, fs_handle, service_id,
[677745a]659 ops->index_get(cur), last, lsize,
[b8dbe2f]660 ops->is_directory(cur));
[bf9dc4e2]661 } else {
[ffa2c8ef]662 async_answer_0(rid, rc);
[bf9dc4e2]663 }
[06901c6b]664 goto out;
[2c448fb]665 }
[1313ee9]666
[bf9dc4e2]667 /* Create. */
[1313ee9]668
[bf9dc4e2]669 if (lflag & L_CREATE) {
670 if (cur && (lflag & L_EXCLUSIVE)) {
671 async_answer_0(rid, EEXIST);
672 goto out;
673 }
[1313ee9]674
[bf9dc4e2]675 if (!cur) {
[61042de]676 rc = ops->create(&cur, service_id,
677 lflag & (L_FILE | L_DIRECTORY));
[bf9dc4e2]678 if (rc != EOK) {
679 async_answer_0(rid, rc);
680 goto out;
681 }
682 if (!cur) {
683 async_answer_0(rid, ENOSPC);
684 goto out;
685 }
686
687 rc = ops->link(par, cur, component);
688 if (rc != EOK) {
689 (void) ops->destroy(cur);
690 cur = NULL;
691 async_answer_0(rid, rc);
692 goto out;
693 }
694 }
[2c448fb]695 }
[1313ee9]696
[bf9dc4e2]697 /* Return. */
[677745a]698out1:
[bf9dc4e2]699 if (!cur) {
[61042de]700 async_answer_5(rid, fs_handle, service_id, ops->index_get(par),
701 last_next, -1, true);
[06901c6b]702 goto out;
[2c448fb]703 }
[1313ee9]704
[677745a]705 int64_t size = ops->size_get(cur);
706 int32_t lsize = LOWER32(size);
[61042de]707 if (lsize != size)
[677745a]708 lsize = -1;
[1313ee9]709
[1dff985]710 async_answer_5(rid, fs_handle, service_id, ops->index_get(cur), last,
711 lsize, ops->is_directory(cur));
[1313ee9]712
[06901c6b]713out:
[61042de]714 if (par)
[0daba212]715 (void) ops->node_put(par);
[1313ee9]716
[61042de]717 if (cur)
[0daba212]718 (void) ops->node_put(cur);
[1313ee9]719
[61042de]720 if (tmp)
[0daba212]721 (void) ops->node_put(tmp);
[2c448fb]722}
723
[75160a6]724void libfs_stat(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
725 ipc_call_t *request)
726{
[15f3c3f]727 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*request);
[75160a6]728 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
[5ed8b72]729
[0daba212]730 fs_node_t *fn;
[15f3c3f]731 int rc = ops->node_get(&fn, service_id, index);
[0daba212]732 on_error(rc, answer_and_return(rid, rc));
[5ed8b72]733
[75160a6]734 ipc_callid_t callid;
735 size_t size;
[1313ee9]736 if ((!async_data_read_receive(&callid, &size)) ||
737 (size != sizeof(struct stat))) {
738 ops->node_put(fn);
[ffa2c8ef]739 async_answer_0(callid, EINVAL);
740 async_answer_0(rid, EINVAL);
[75160a6]741 return;
742 }
[5ed8b72]743
[0143f72]744 struct stat stat;
745 memset(&stat, 0, sizeof(struct stat));
[5ed8b72]746
[0143f72]747 stat.fs_handle = fs_handle;
[15f3c3f]748 stat.service_id = service_id;
[0143f72]749 stat.index = index;
[1313ee9]750 stat.lnkcnt = ops->lnkcnt_get(fn);
[0143f72]751 stat.is_file = ops->is_file(fn);
[1313ee9]752 stat.is_directory = ops->is_directory(fn);
[0143f72]753 stat.size = ops->size_get(fn);
[b33870b]754 stat.service = ops->service_get(fn);
[5ed8b72]755
[1313ee9]756 ops->node_put(fn);
[3dd148d]757
758
[0da4e41]759 async_data_read_finalize(callid, &stat, sizeof(struct stat));
[ffa2c8ef]760 async_answer_0(rid, EOK);
[75160a6]761}
762
[5930e3f]763void libfs_statfs(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
764 ipc_call_t *request)
765{
766 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*request);
767 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
[5ed8b72]768
[5930e3f]769 fs_node_t *fn;
770 int rc = ops->node_get(&fn, service_id, index);
771 on_error(rc, answer_and_return(rid, rc));
[5ed8b72]772
[5930e3f]773 ipc_callid_t callid;
774 size_t size;
775 if ((!async_data_read_receive(&callid, &size)) ||
776 (size != sizeof(struct statfs))) {
[5ed8b72]777 goto error;
[5930e3f]778 }
779
[5ed8b72]780 struct statfs st;
781 memset(&st, 0, sizeof(struct statfs));
782
[5a2b765]783 str_cpy(st.fs_name, sizeof(st.fs_name), fs_name);
784
[5ed8b72]785 if (ops->size_block != NULL) {
786 rc = ops->size_block(service_id, &st.f_bsize);
787 if (rc != EOK)
788 goto error;
[3dd148d]789 }
790
[5ed8b72]791 if (ops->total_block_count != NULL) {
792 rc = ops->total_block_count(service_id, &st.f_blocks);
793 if (rc != EOK)
794 goto error;
[3dd148d]795 }
796
[5ed8b72]797 if (ops->free_block_count != NULL) {
798 rc = ops->free_block_count(service_id, &st.f_bfree);
799 if (rc != EOK)
800 goto error;
[3dd148d]801 }
802
[5930e3f]803 ops->node_put(fn);
[5ed8b72]804 async_data_read_finalize(callid, &st, sizeof(struct statfs));
[5930e3f]805 async_answer_0(rid, EOK);
[3dd148d]806 return;
807
808error:
809 ops->node_put(fn);
810 async_answer_0(callid, EINVAL);
811 async_answer_0(rid, EINVAL);
[5930e3f]812}
813
814
[595edf5]815/** Open VFS triplet.
816 *
[1313ee9]817 * @param ops libfs operations structure with function pointers to
818 * file system implementation
819 * @param rid Request ID of the VFS_OUT_OPEN_NODE request.
820 * @param request VFS_OUT_OPEN_NODE request data itself.
[595edf5]821 *
822 */
823void libfs_open_node(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
824 ipc_call_t *request)
825{
[15f3c3f]826 service_id_t service_id = IPC_GET_ARG1(*request);
[595edf5]827 fs_index_t index = IPC_GET_ARG2(*request);
828
[ed903174]829 fs_node_t *fn;
[15f3c3f]830 int rc = ops->node_get(&fn, service_id, index);
[0daba212]831 on_error(rc, answer_and_return(rid, rc));
[595edf5]832
[0daba212]833 if (fn == NULL) {
[ffa2c8ef]834 async_answer_0(rid, ENOENT);
[595edf5]835 return;
836 }
837
[1313ee9]838 rc = ops->node_open(fn);
[ed903174]839 aoff64_t size = ops->size_get(fn);
[efcebe1]840 async_answer_4(rid, rc, LOWER32(size), UPPER32(size),
841 ops->lnkcnt_get(fn),
[61042de]842 (ops->is_file(fn) ? L_FILE : 0) |
843 (ops->is_directory(fn) ? L_DIRECTORY : 0));
[595edf5]844
[0daba212]845 (void) ops->node_put(fn);
[595edf5]846}
847
[5bf76c1]848static FIBRIL_MUTEX_INITIALIZE(instances_mutex);
849static LIST_INITIALIZE(instances_list);
850
851typedef struct {
852 service_id_t service_id;
853 link_t link;
854 void *data;
855} fs_instance_t;
856
857int fs_instance_create(service_id_t service_id, void *data)
858{
859 fs_instance_t *inst = malloc(sizeof(fs_instance_t));
860 if (!inst)
861 return ENOMEM;
862
863 link_initialize(&inst->link);
864 inst->service_id = service_id;
865 inst->data = data;
866
867 fibril_mutex_lock(&instances_mutex);
[feeac0d]868 list_foreach(instances_list, link, fs_instance_t, cur) {
[5bf76c1]869 if (cur->service_id == service_id) {
870 fibril_mutex_unlock(&instances_mutex);
871 free(inst);
872 return EEXIST;
873 }
874
875 /* keep the list sorted */
876 if (cur->service_id < service_id) {
877 list_insert_before(&inst->link, &cur->link);
878 fibril_mutex_unlock(&instances_mutex);
879 return EOK;
880 }
881 }
882 list_append(&inst->link, &instances_list);
883 fibril_mutex_unlock(&instances_mutex);
884
885 return EOK;
886}
887
888int fs_instance_get(service_id_t service_id, void **idp)
889{
890 fibril_mutex_lock(&instances_mutex);
[feeac0d]891 list_foreach(instances_list, link, fs_instance_t, inst) {
[5bf76c1]892 if (inst->service_id == service_id) {
893 *idp = inst->data;
894 fibril_mutex_unlock(&instances_mutex);
895 return EOK;
896 }
897 }
898 fibril_mutex_unlock(&instances_mutex);
899 return ENOENT;
900}
901
902int fs_instance_destroy(service_id_t service_id)
903{
904 fibril_mutex_lock(&instances_mutex);
[feeac0d]905 list_foreach(instances_list, link, fs_instance_t, inst) {
[5bf76c1]906 if (inst->service_id == service_id) {
907 list_remove(&inst->link);
908 fibril_mutex_unlock(&instances_mutex);
909 free(inst);
910 return EOK;
911 }
912 }
913 fibril_mutex_unlock(&instances_mutex);
914 return ENOENT;
915}
916
[74303b6]917/** @}
918 */
Note: See TracBrowser for help on using the repository browser.