[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 <ipc/ipc.h>
|
---|
| 43 | #include <as.h>
|
---|
[2c448fb] | 44 | #include <assert.h>
|
---|
| 45 | #include <dirent.h>
|
---|
[595edf5] | 46 | #include <mem.h>
|
---|
[75160a6] | 47 | #include <sys/stat.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 { \
|
---|
| 60 | ipc_answer_0((rid), (rc)); \
|
---|
| 61 | return; \
|
---|
| 62 | } while (0)
|
---|
| 63 |
|
---|
[efd4a72] | 64 | /** Register file system server.
|
---|
| 65 | *
|
---|
| 66 | * This function abstracts away the tedious registration protocol from
|
---|
| 67 | * file system implementations and lets them to reuse this registration glue
|
---|
| 68 | * code.
|
---|
| 69 | *
|
---|
[1313ee9] | 70 | * @param vfs_phone Open phone for communication with VFS.
|
---|
| 71 | * @param reg File system registration structure. It will be
|
---|
| 72 | * initialized by this function.
|
---|
| 73 | * @param info VFS info structure supplied by the file system
|
---|
| 74 | * implementation.
|
---|
| 75 | * @param conn Connection fibril for handling all calls originating in
|
---|
| 76 | * VFS.
|
---|
| 77 | *
|
---|
| 78 | * @return EOK on success or a non-zero error code on errror.
|
---|
[efd4a72] | 79 | *
|
---|
| 80 | */
|
---|
| 81 | int fs_register(int vfs_phone, fs_reg_t *reg, vfs_info_t *info,
|
---|
| 82 | async_client_conn_t conn)
|
---|
| 83 | {
|
---|
| 84 | /*
|
---|
| 85 | * Tell VFS that we are here and want to get registered.
|
---|
| 86 | * We use the async framework because VFS will answer the request
|
---|
| 87 | * out-of-order, when it knows that the operation succeeded or failed.
|
---|
| 88 | */
|
---|
| 89 | ipc_call_t answer;
|
---|
[4198f9c3] | 90 | aid_t req = async_send_0(vfs_phone, VFS_IN_REGISTER, &answer);
|
---|
[1313ee9] | 91 |
|
---|
[efd4a72] | 92 | /*
|
---|
| 93 | * Send our VFS info structure to VFS.
|
---|
| 94 | */
|
---|
[0da4e41] | 95 | int rc = async_data_write_start(vfs_phone, info, sizeof(*info));
|
---|
[efd4a72] | 96 | if (rc != EOK) {
|
---|
| 97 | async_wait_for(req, NULL);
|
---|
| 98 | return rc;
|
---|
| 99 | }
|
---|
[1313ee9] | 100 |
|
---|
[efd4a72] | 101 | /*
|
---|
| 102 | * Ask VFS for callback connection.
|
---|
| 103 | */
|
---|
[3c22f70] | 104 | sysarg_t taskhash;
|
---|
| 105 | ipc_connect_to_me(vfs_phone, 0, 0, 0, &taskhash, ®->vfs_phonehash);
|
---|
[1313ee9] | 106 |
|
---|
[efd4a72] | 107 | /*
|
---|
| 108 | * Allocate piece of address space for PLB.
|
---|
| 109 | */
|
---|
| 110 | reg->plb_ro = as_get_mappable_page(PLB_SIZE);
|
---|
| 111 | if (!reg->plb_ro) {
|
---|
| 112 | async_wait_for(req, NULL);
|
---|
| 113 | return ENOMEM;
|
---|
| 114 | }
|
---|
[1313ee9] | 115 |
|
---|
[efd4a72] | 116 | /*
|
---|
| 117 | * Request sharing the Path Lookup Buffer with VFS.
|
---|
| 118 | */
|
---|
[0da4e41] | 119 | rc = async_share_in_start_0_0(vfs_phone, reg->plb_ro, PLB_SIZE);
|
---|
[efd4a72] | 120 | if (rc) {
|
---|
| 121 | async_wait_for(req, NULL);
|
---|
| 122 | return rc;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | /*
|
---|
[4198f9c3] | 126 | * Pick up the answer for the request to the VFS_IN_REQUEST call.
|
---|
[efd4a72] | 127 | */
|
---|
| 128 | async_wait_for(req, NULL);
|
---|
| 129 | reg->fs_handle = (int) IPC_GET_ARG1(answer);
|
---|
| 130 |
|
---|
| 131 | /*
|
---|
| 132 | * Create a connection fibril to handle the callback connection.
|
---|
| 133 | */
|
---|
[3c22f70] | 134 | async_new_connection(taskhash, reg->vfs_phonehash, 0, NULL, conn);
|
---|
[efd4a72] | 135 |
|
---|
| 136 | /*
|
---|
| 137 | * Tell the async framework that other connections are to be handled by
|
---|
| 138 | * the same connection fibril as well.
|
---|
| 139 | */
|
---|
| 140 | async_set_client_connection(conn);
|
---|
[1313ee9] | 141 |
|
---|
[efd4a72] | 142 | return IPC_GET_RETVAL(answer);
|
---|
| 143 | }
|
---|
[74303b6] | 144 |
|
---|
[83937ccd] | 145 | void fs_node_initialize(fs_node_t *fn)
|
---|
| 146 | {
|
---|
| 147 | memset(fn, 0, sizeof(fs_node_t));
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[16d17ca] | 150 | void libfs_mount(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
|
---|
| 151 | ipc_call_t *request)
|
---|
[83937ccd] | 152 | {
|
---|
[991f645] | 153 | devmap_handle_t mp_devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
---|
[55982d6] | 154 | fs_index_t mp_fs_index = (fs_index_t) IPC_GET_ARG2(*request);
|
---|
| 155 | fs_handle_t mr_fs_handle = (fs_handle_t) IPC_GET_ARG3(*request);
|
---|
[991f645] | 156 | devmap_handle_t mr_devmap_handle = (devmap_handle_t) IPC_GET_ARG4(*request);
|
---|
[83937ccd] | 157 | int res;
|
---|
[96b02eb9] | 158 | sysarg_t rc;
|
---|
[1313ee9] | 159 |
|
---|
[83937ccd] | 160 | ipc_call_t call;
|
---|
| 161 | ipc_callid_t callid;
|
---|
[1313ee9] | 162 |
|
---|
| 163 | /* Accept the phone */
|
---|
[83937ccd] | 164 | callid = async_get_call(&call);
|
---|
[b4cbef1] | 165 | int mountee_phone = (int) IPC_GET_ARG1(call);
|
---|
[228e490] | 166 | if ((IPC_GET_IMETHOD(call) != IPC_M_CONNECTION_CLONE) ||
|
---|
[1313ee9] | 167 | (mountee_phone < 0)) {
|
---|
[83937ccd] | 168 | ipc_answer_0(callid, EINVAL);
|
---|
| 169 | ipc_answer_0(rid, EINVAL);
|
---|
| 170 | return;
|
---|
| 171 | }
|
---|
[1313ee9] | 172 |
|
---|
| 173 | /* Acknowledge the mountee_phone */
|
---|
| 174 | ipc_answer_0(callid, EOK);
|
---|
[83937ccd] | 175 |
|
---|
[0daba212] | 176 | fs_node_t *fn;
|
---|
[991f645] | 177 | res = ops->node_get(&fn, mp_devmap_handle, mp_fs_index);
|
---|
[1313ee9] | 178 | if ((res != EOK) || (!fn)) {
|
---|
[83937ccd] | 179 | ipc_hangup(mountee_phone);
|
---|
[eda925a] | 180 | async_data_write_void(combine_rc(res, ENOENT));
|
---|
[0daba212] | 181 | ipc_answer_0(rid, combine_rc(res, ENOENT));
|
---|
[83937ccd] | 182 | return;
|
---|
| 183 | }
|
---|
[1313ee9] | 184 |
|
---|
[83937ccd] | 185 | if (fn->mp_data.mp_active) {
|
---|
| 186 | ipc_hangup(mountee_phone);
|
---|
[0daba212] | 187 | (void) ops->node_put(fn);
|
---|
[eda925a] | 188 | async_data_write_void(EBUSY);
|
---|
[83937ccd] | 189 | ipc_answer_0(rid, EBUSY);
|
---|
| 190 | return;
|
---|
| 191 | }
|
---|
[1313ee9] | 192 |
|
---|
[83937ccd] | 193 | rc = async_req_0_0(mountee_phone, IPC_M_CONNECT_ME);
|
---|
[0daba212] | 194 | if (rc != EOK) {
|
---|
[83937ccd] | 195 | ipc_hangup(mountee_phone);
|
---|
[0daba212] | 196 | (void) ops->node_put(fn);
|
---|
[eda925a] | 197 | async_data_write_void(rc);
|
---|
[83937ccd] | 198 | ipc_answer_0(rid, rc);
|
---|
| 199 | return;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | ipc_call_t answer;
|
---|
[eda925a] | 203 | rc = async_data_write_forward_1_1(mountee_phone, VFS_OUT_MOUNTED,
|
---|
[991f645] | 204 | mr_devmap_handle, &answer);
|
---|
[83937ccd] | 205 |
|
---|
| 206 | if (rc == EOK) {
|
---|
| 207 | fn->mp_data.mp_active = true;
|
---|
| 208 | fn->mp_data.fs_handle = mr_fs_handle;
|
---|
[991f645] | 209 | fn->mp_data.devmap_handle = mr_devmap_handle;
|
---|
[83937ccd] | 210 | fn->mp_data.phone = mountee_phone;
|
---|
| 211 | }
|
---|
[1313ee9] | 212 |
|
---|
[83937ccd] | 213 | /*
|
---|
| 214 | * Do not release the FS node so that it stays in memory.
|
---|
| 215 | */
|
---|
[0442c02] | 216 | ipc_answer_3(rid, rc, IPC_GET_ARG1(answer), IPC_GET_ARG2(answer),
|
---|
| 217 | IPC_GET_ARG3(answer));
|
---|
[83937ccd] | 218 | }
|
---|
| 219 |
|
---|
[3c11713] | 220 | void libfs_unmount(libfs_ops_t *ops, ipc_callid_t rid, ipc_call_t *request)
|
---|
| 221 | {
|
---|
[991f645] | 222 | devmap_handle_t mp_devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
---|
[c888102] | 223 | fs_index_t mp_fs_index = (fs_index_t) IPC_GET_ARG2(*request);
|
---|
| 224 | fs_node_t *fn;
|
---|
| 225 | int res;
|
---|
| 226 |
|
---|
[991f645] | 227 | res = ops->node_get(&fn, mp_devmap_handle, mp_fs_index);
|
---|
[c888102] | 228 | if ((res != EOK) || (!fn)) {
|
---|
| 229 | ipc_answer_0(rid, combine_rc(res, ENOENT));
|
---|
| 230 | return;
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | /*
|
---|
| 234 | * We are clearly expecting to find the mount point active.
|
---|
| 235 | */
|
---|
| 236 | if (!fn->mp_data.mp_active) {
|
---|
| 237 | (void) ops->node_put(fn);
|
---|
| 238 | ipc_answer_0(rid, EINVAL);
|
---|
| 239 | return;
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | /*
|
---|
| 243 | * Tell the mounted file system to unmount.
|
---|
| 244 | */
|
---|
| 245 | res = async_req_1_0(fn->mp_data.phone, VFS_OUT_UNMOUNTED,
|
---|
[991f645] | 246 | fn->mp_data.devmap_handle);
|
---|
[c888102] | 247 |
|
---|
| 248 | /*
|
---|
| 249 | * If everything went well, perform the clean-up on our side.
|
---|
| 250 | */
|
---|
| 251 | if (res == EOK) {
|
---|
| 252 | ipc_hangup(fn->mp_data.phone);
|
---|
| 253 | fn->mp_data.mp_active = false;
|
---|
| 254 | fn->mp_data.fs_handle = 0;
|
---|
[991f645] | 255 | fn->mp_data.devmap_handle = 0;
|
---|
[c888102] | 256 | fn->mp_data.phone = 0;
|
---|
| 257 | /* Drop the reference created in libfs_mount(). */
|
---|
| 258 | (void) ops->node_put(fn);
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | (void) ops->node_put(fn);
|
---|
| 262 | ipc_answer_0(rid, res);
|
---|
[3c11713] | 263 | }
|
---|
| 264 |
|
---|
[1e50f81] | 265 | /** Lookup VFS triplet by name in the file system name space.
|
---|
[9bb85f3] | 266 | *
|
---|
| 267 | * The path passed in the PLB must be in the canonical file system path format
|
---|
| 268 | * as returned by the canonify() function.
|
---|
[1e50f81] | 269 | *
|
---|
[595edf5] | 270 | * @param ops libfs operations structure with function pointers to
|
---|
| 271 | * file system implementation
|
---|
| 272 | * @param fs_handle File system handle of the file system where to perform
|
---|
| 273 | * the lookup.
|
---|
[4198f9c3] | 274 | * @param rid Request ID of the VFS_OUT_LOOKUP request.
|
---|
| 275 | * @param request VFS_OUT_LOOKUP request data itself.
|
---|
[595edf5] | 276 | *
|
---|
[1e50f81] | 277 | */
|
---|
[f2ec8c8] | 278 | void libfs_lookup(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
|
---|
[2c448fb] | 279 | ipc_call_t *request)
|
---|
| 280 | {
|
---|
[1313ee9] | 281 | unsigned int first = IPC_GET_ARG1(*request);
|
---|
| 282 | unsigned int last = IPC_GET_ARG2(*request);
|
---|
| 283 | unsigned int next = first;
|
---|
[991f645] | 284 | devmap_handle_t devmap_handle = IPC_GET_ARG3(*request);
|
---|
[2c448fb] | 285 | int lflag = IPC_GET_ARG4(*request);
|
---|
[1313ee9] | 286 | fs_index_t index = IPC_GET_ARG5(*request);
|
---|
[c9f6e49f] | 287 | char component[NAME_MAX + 1];
|
---|
| 288 | int len;
|
---|
[0daba212] | 289 | int rc;
|
---|
[1313ee9] | 290 |
|
---|
[2c448fb] | 291 | if (last < next)
|
---|
| 292 | last += PLB_SIZE;
|
---|
[1313ee9] | 293 |
|
---|
[b6035ba] | 294 | fs_node_t *par = NULL;
|
---|
[0daba212] | 295 | fs_node_t *cur = NULL;
|
---|
[b6035ba] | 296 | fs_node_t *tmp = NULL;
|
---|
[1313ee9] | 297 |
|
---|
[991f645] | 298 | rc = ops->root_get(&cur, devmap_handle);
|
---|
[0daba212] | 299 | on_error(rc, goto out_with_answer);
|
---|
[1313ee9] | 300 |
|
---|
[83937ccd] | 301 | if (cur->mp_data.mp_active) {
|
---|
[4198f9c3] | 302 | ipc_forward_slow(rid, cur->mp_data.phone, VFS_OUT_LOOKUP,
|
---|
[991f645] | 303 | next, last, cur->mp_data.devmap_handle, lflag, index,
|
---|
[83937ccd] | 304 | IPC_FF_ROUTE_FROM_ME);
|
---|
[0daba212] | 305 | (void) ops->node_put(cur);
|
---|
[83937ccd] | 306 | return;
|
---|
| 307 | }
|
---|
[1313ee9] | 308 |
|
---|
| 309 | /* Eat slash */
|
---|
[2c448fb] | 310 | if (ops->plb_get_char(next) == '/')
|
---|
[1313ee9] | 311 | next++;
|
---|
[2c448fb] | 312 |
|
---|
[0daba212] | 313 | while (next <= last) {
|
---|
| 314 | bool has_children;
|
---|
[1313ee9] | 315 |
|
---|
[0daba212] | 316 | rc = ops->has_children(&has_children, cur);
|
---|
| 317 | on_error(rc, goto out_with_answer);
|
---|
| 318 | if (!has_children)
|
---|
| 319 | break;
|
---|
[1313ee9] | 320 |
|
---|
| 321 | /* Collect the component */
|
---|
[c9f6e49f] | 322 | len = 0;
|
---|
[1313ee9] | 323 | while ((next <= last) && (ops->plb_get_char(next) != '/')) {
|
---|
[2c448fb] | 324 | if (len + 1 == NAME_MAX) {
|
---|
[1313ee9] | 325 | /* Component length overflow */
|
---|
[2c448fb] | 326 | ipc_answer_0(rid, ENAMETOOLONG);
|
---|
[06901c6b] | 327 | goto out;
|
---|
[2c448fb] | 328 | }
|
---|
| 329 | component[len++] = ops->plb_get_char(next);
|
---|
[1313ee9] | 330 | /* Process next character */
|
---|
| 331 | next++;
|
---|
[2c448fb] | 332 | }
|
---|
[1313ee9] | 333 |
|
---|
[2c448fb] | 334 | assert(len);
|
---|
| 335 | component[len] = '\0';
|
---|
[1313ee9] | 336 | /* Eat slash */
|
---|
| 337 | next++;
|
---|
| 338 |
|
---|
| 339 | /* Match the component */
|
---|
[0daba212] | 340 | rc = ops->match(&tmp, cur, component);
|
---|
| 341 | on_error(rc, goto out_with_answer);
|
---|
[1313ee9] | 342 |
|
---|
[4b995b92] | 343 | /*
|
---|
| 344 | * If the matching component is a mount point, there are two
|
---|
| 345 | * legitimate semantics of the lookup operation. The first is
|
---|
| 346 | * the commonly used one in which the lookup crosses each mount
|
---|
| 347 | * point into the mounted file system. The second semantics is
|
---|
| 348 | * used mostly during unmount() and differs from the first one
|
---|
| 349 | * only in that the last mount point in the looked up path,
|
---|
| 350 | * which is also its last component, is not crossed.
|
---|
| 351 | */
|
---|
| 352 |
|
---|
| 353 | if ((tmp) && (tmp->mp_data.mp_active) &&
|
---|
[f7376cbf] | 354 | (!(lflag & L_MP) || (next <= last))) {
|
---|
[83937ccd] | 355 | if (next > last)
|
---|
| 356 | next = last = first;
|
---|
| 357 | else
|
---|
| 358 | next--;
|
---|
[1313ee9] | 359 |
|
---|
[4198f9c3] | 360 | ipc_forward_slow(rid, tmp->mp_data.phone,
|
---|
[991f645] | 361 | VFS_OUT_LOOKUP, next, last, tmp->mp_data.devmap_handle,
|
---|
[4198f9c3] | 362 | lflag, index, IPC_FF_ROUTE_FROM_ME);
|
---|
[0daba212] | 363 | (void) ops->node_put(cur);
|
---|
| 364 | (void) ops->node_put(tmp);
|
---|
[83937ccd] | 365 | if (par)
|
---|
[0daba212] | 366 | (void) ops->node_put(par);
|
---|
[83937ccd] | 367 | return;
|
---|
| 368 | }
|
---|
[1313ee9] | 369 |
|
---|
| 370 | /* Handle miss: match amongst siblings */
|
---|
[2c448fb] | 371 | if (!tmp) {
|
---|
[a8e9ab8d] | 372 | if (next <= last) {
|
---|
[1313ee9] | 373 | /* There are unprocessed components */
|
---|
[a8e9ab8d] | 374 | ipc_answer_0(rid, ENOENT);
|
---|
[06901c6b] | 375 | goto out;
|
---|
[a8e9ab8d] | 376 | }
|
---|
[1313ee9] | 377 |
|
---|
| 378 | /* Miss in the last component */
|
---|
| 379 | if (lflag & (L_CREATE | L_LINK)) {
|
---|
| 380 | /* Request to create a new link */
|
---|
[2c448fb] | 381 | if (!ops->is_directory(cur)) {
|
---|
| 382 | ipc_answer_0(rid, ENOTDIR);
|
---|
[06901c6b] | 383 | goto out;
|
---|
[a8e9ab8d] | 384 | }
|
---|
[1313ee9] | 385 |
|
---|
[b6035ba] | 386 | fs_node_t *fn;
|
---|
[a8e9ab8d] | 387 | if (lflag & L_CREATE)
|
---|
[991f645] | 388 | rc = ops->create(&fn, devmap_handle,
|
---|
[0daba212] | 389 | lflag);
|
---|
[a8e9ab8d] | 390 | else
|
---|
[991f645] | 391 | rc = ops->node_get(&fn, devmap_handle,
|
---|
[34f62f8] | 392 | index);
|
---|
[0daba212] | 393 | on_error(rc, goto out_with_answer);
|
---|
[1313ee9] | 394 |
|
---|
[b6035ba] | 395 | if (fn) {
|
---|
| 396 | rc = ops->link(cur, fn, component);
|
---|
[0013b9ce] | 397 | if (rc != EOK) {
|
---|
[0daba212] | 398 | if (lflag & L_CREATE)
|
---|
| 399 | (void) ops->destroy(fn);
|
---|
[0013b9ce] | 400 | ipc_answer_0(rid, rc);
|
---|
[2c448fb] | 401 | } else {
|
---|
[ed903174] | 402 | aoff64_t size = ops->size_get(fn);
|
---|
| 403 | ipc_answer_5(rid, fs_handle,
|
---|
[991f645] | 404 | devmap_handle,
|
---|
[b6035ba] | 405 | ops->index_get(fn),
|
---|
[ed903174] | 406 | LOWER32(size),
|
---|
| 407 | UPPER32(size),
|
---|
[b6035ba] | 408 | ops->lnkcnt_get(fn));
|
---|
[0daba212] | 409 | (void) ops->node_put(fn);
|
---|
[2c448fb] | 410 | }
|
---|
[1313ee9] | 411 | } else
|
---|
[2c448fb] | 412 | ipc_answer_0(rid, ENOSPC);
|
---|
[1313ee9] | 413 |
|
---|
[06901c6b] | 414 | goto out;
|
---|
[1313ee9] | 415 | }
|
---|
| 416 |
|
---|
[2c448fb] | 417 | ipc_answer_0(rid, ENOENT);
|
---|
[06901c6b] | 418 | goto out;
|
---|
[2c448fb] | 419 | }
|
---|
[1313ee9] | 420 |
|
---|
[0daba212] | 421 | if (par) {
|
---|
| 422 | rc = ops->node_put(par);
|
---|
| 423 | on_error(rc, goto out_with_answer);
|
---|
| 424 | }
|
---|
[1313ee9] | 425 |
|
---|
| 426 | /* Descend one level */
|
---|
[7b6d98b] | 427 | par = cur;
|
---|
[2c448fb] | 428 | cur = tmp;
|
---|
[06901c6b] | 429 | tmp = NULL;
|
---|
[2c448fb] | 430 | }
|
---|
[1313ee9] | 431 |
|
---|
| 432 | /* Handle miss: excessive components */
|
---|
[0daba212] | 433 | if (next <= last) {
|
---|
| 434 | bool has_children;
|
---|
| 435 | rc = ops->has_children(&has_children, cur);
|
---|
| 436 | on_error(rc, goto out_with_answer);
|
---|
[1313ee9] | 437 |
|
---|
[0daba212] | 438 | if (has_children)
|
---|
| 439 | goto skip_miss;
|
---|
[1313ee9] | 440 |
|
---|
[a8e9ab8d] | 441 | if (lflag & (L_CREATE | L_LINK)) {
|
---|
[2c448fb] | 442 | if (!ops->is_directory(cur)) {
|
---|
| 443 | ipc_answer_0(rid, ENOTDIR);
|
---|
[06901c6b] | 444 | goto out;
|
---|
[2c448fb] | 445 | }
|
---|
[1313ee9] | 446 |
|
---|
| 447 | /* Collect next component */
|
---|
[c9f6e49f] | 448 | len = 0;
|
---|
[2c448fb] | 449 | while (next <= last) {
|
---|
| 450 | if (ops->plb_get_char(next) == '/') {
|
---|
[1313ee9] | 451 | /* More than one component */
|
---|
[2c448fb] | 452 | ipc_answer_0(rid, ENOENT);
|
---|
[06901c6b] | 453 | goto out;
|
---|
[2c448fb] | 454 | }
|
---|
[1313ee9] | 455 |
|
---|
[2c448fb] | 456 | if (len + 1 == NAME_MAX) {
|
---|
[1313ee9] | 457 | /* Component length overflow */
|
---|
[2c448fb] | 458 | ipc_answer_0(rid, ENAMETOOLONG);
|
---|
[06901c6b] | 459 | goto out;
|
---|
[2c448fb] | 460 | }
|
---|
[1313ee9] | 461 |
|
---|
[2c448fb] | 462 | component[len++] = ops->plb_get_char(next);
|
---|
[1313ee9] | 463 | /* Process next character */
|
---|
| 464 | next++;
|
---|
[2c448fb] | 465 | }
|
---|
[1313ee9] | 466 |
|
---|
[2c448fb] | 467 | assert(len);
|
---|
| 468 | component[len] = '\0';
|
---|
[1313ee9] | 469 |
|
---|
[b6035ba] | 470 | fs_node_t *fn;
|
---|
[a8e9ab8d] | 471 | if (lflag & L_CREATE)
|
---|
[991f645] | 472 | rc = ops->create(&fn, devmap_handle, lflag);
|
---|
[a8e9ab8d] | 473 | else
|
---|
[991f645] | 474 | rc = ops->node_get(&fn, devmap_handle, index);
|
---|
[0daba212] | 475 | on_error(rc, goto out_with_answer);
|
---|
[1313ee9] | 476 |
|
---|
[b6035ba] | 477 | if (fn) {
|
---|
| 478 | rc = ops->link(cur, fn, component);
|
---|
[0013b9ce] | 479 | if (rc != EOK) {
|
---|
[a8e9ab8d] | 480 | if (lflag & L_CREATE)
|
---|
[0daba212] | 481 | (void) ops->destroy(fn);
|
---|
[0013b9ce] | 482 | ipc_answer_0(rid, rc);
|
---|
[2c448fb] | 483 | } else {
|
---|
[ed903174] | 484 | aoff64_t size = ops->size_get(fn);
|
---|
| 485 | ipc_answer_5(rid, fs_handle,
|
---|
[991f645] | 486 | devmap_handle,
|
---|
[b6035ba] | 487 | ops->index_get(fn),
|
---|
[ed903174] | 488 | LOWER32(size),
|
---|
| 489 | UPPER32(size),
|
---|
[b6035ba] | 490 | ops->lnkcnt_get(fn));
|
---|
[0daba212] | 491 | (void) ops->node_put(fn);
|
---|
[2c448fb] | 492 | }
|
---|
[1313ee9] | 493 | } else
|
---|
[2c448fb] | 494 | ipc_answer_0(rid, ENOSPC);
|
---|
[1313ee9] | 495 |
|
---|
[06901c6b] | 496 | goto out;
|
---|
[2c448fb] | 497 | }
|
---|
[1313ee9] | 498 |
|
---|
[2c448fb] | 499 | ipc_answer_0(rid, ENOENT);
|
---|
[06901c6b] | 500 | goto out;
|
---|
[2c448fb] | 501 | }
|
---|
[1313ee9] | 502 |
|
---|
[0daba212] | 503 | skip_miss:
|
---|
[1313ee9] | 504 |
|
---|
| 505 | /* Handle hit */
|
---|
[a8e9ab8d] | 506 | if (lflag & L_UNLINK) {
|
---|
[1313ee9] | 507 | unsigned int old_lnkcnt = ops->lnkcnt_get(cur);
|
---|
[0daba212] | 508 | rc = ops->unlink(par, cur, component);
|
---|
[ed903174] | 509 |
|
---|
| 510 | if (rc == EOK) {
|
---|
| 511 | aoff64_t size = ops->size_get(cur);
|
---|
[991f645] | 512 | ipc_answer_5(rid, fs_handle, devmap_handle,
|
---|
[ed903174] | 513 | ops->index_get(cur), LOWER32(size), UPPER32(size),
|
---|
| 514 | old_lnkcnt);
|
---|
| 515 | } else
|
---|
| 516 | ipc_answer_0(rid, rc);
|
---|
| 517 |
|
---|
[06901c6b] | 518 | goto out;
|
---|
[2c448fb] | 519 | }
|
---|
[1313ee9] | 520 |
|
---|
[a8e9ab8d] | 521 | if (((lflag & (L_CREATE | L_EXCLUSIVE)) == (L_CREATE | L_EXCLUSIVE)) ||
|
---|
| 522 | (lflag & L_LINK)) {
|
---|
[2c448fb] | 523 | ipc_answer_0(rid, EEXIST);
|
---|
[06901c6b] | 524 | goto out;
|
---|
[2c448fb] | 525 | }
|
---|
[1313ee9] | 526 |
|
---|
[2c448fb] | 527 | if ((lflag & L_FILE) && (ops->is_directory(cur))) {
|
---|
| 528 | ipc_answer_0(rid, EISDIR);
|
---|
[06901c6b] | 529 | goto out;
|
---|
[2c448fb] | 530 | }
|
---|
[1313ee9] | 531 |
|
---|
[2c448fb] | 532 | if ((lflag & L_DIRECTORY) && (ops->is_file(cur))) {
|
---|
| 533 | ipc_answer_0(rid, ENOTDIR);
|
---|
[06901c6b] | 534 | goto out;
|
---|
[2c448fb] | 535 | }
|
---|
[f7376cbf] | 536 |
|
---|
| 537 | if ((lflag & L_ROOT) && par) {
|
---|
| 538 | ipc_answer_0(rid, EINVAL);
|
---|
| 539 | goto out;
|
---|
| 540 | }
|
---|
[1313ee9] | 541 |
|
---|
[0daba212] | 542 | out_with_answer:
|
---|
[1313ee9] | 543 |
|
---|
[0daba212] | 544 | if (rc == EOK) {
|
---|
[1313ee9] | 545 | if (lflag & L_OPEN)
|
---|
| 546 | rc = ops->node_open(cur);
|
---|
| 547 |
|
---|
[ed903174] | 548 | if (rc == EOK) {
|
---|
| 549 | aoff64_t size = ops->size_get(cur);
|
---|
[991f645] | 550 | ipc_answer_5(rid, fs_handle, devmap_handle,
|
---|
[ed903174] | 551 | ops->index_get(cur), LOWER32(size), UPPER32(size),
|
---|
| 552 | ops->lnkcnt_get(cur));
|
---|
| 553 | } else
|
---|
| 554 | ipc_answer_0(rid, rc);
|
---|
| 555 |
|
---|
[1313ee9] | 556 | } else
|
---|
[0daba212] | 557 | ipc_answer_0(rid, rc);
|
---|
[1313ee9] | 558 |
|
---|
[06901c6b] | 559 | out:
|
---|
[1313ee9] | 560 |
|
---|
[06901c6b] | 561 | if (par)
|
---|
[0daba212] | 562 | (void) ops->node_put(par);
|
---|
[1313ee9] | 563 |
|
---|
[06901c6b] | 564 | if (cur)
|
---|
[0daba212] | 565 | (void) ops->node_put(cur);
|
---|
[1313ee9] | 566 |
|
---|
[06901c6b] | 567 | if (tmp)
|
---|
[0daba212] | 568 | (void) ops->node_put(tmp);
|
---|
[2c448fb] | 569 | }
|
---|
| 570 |
|
---|
[75160a6] | 571 | void libfs_stat(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
|
---|
| 572 | ipc_call_t *request)
|
---|
| 573 | {
|
---|
[991f645] | 574 | devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
---|
[75160a6] | 575 | fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
|
---|
[1313ee9] | 576 |
|
---|
[0daba212] | 577 | fs_node_t *fn;
|
---|
[991f645] | 578 | int rc = ops->node_get(&fn, devmap_handle, index);
|
---|
[0daba212] | 579 | on_error(rc, answer_and_return(rid, rc));
|
---|
[1313ee9] | 580 |
|
---|
[75160a6] | 581 | ipc_callid_t callid;
|
---|
| 582 | size_t size;
|
---|
[1313ee9] | 583 | if ((!async_data_read_receive(&callid, &size)) ||
|
---|
| 584 | (size != sizeof(struct stat))) {
|
---|
| 585 | ops->node_put(fn);
|
---|
[75160a6] | 586 | ipc_answer_0(callid, EINVAL);
|
---|
| 587 | ipc_answer_0(rid, EINVAL);
|
---|
| 588 | return;
|
---|
| 589 | }
|
---|
[1313ee9] | 590 |
|
---|
[0143f72] | 591 | struct stat stat;
|
---|
| 592 | memset(&stat, 0, sizeof(struct stat));
|
---|
[75160a6] | 593 |
|
---|
[0143f72] | 594 | stat.fs_handle = fs_handle;
|
---|
[991f645] | 595 | stat.devmap_handle = devmap_handle;
|
---|
[0143f72] | 596 | stat.index = index;
|
---|
[1313ee9] | 597 | stat.lnkcnt = ops->lnkcnt_get(fn);
|
---|
[0143f72] | 598 | stat.is_file = ops->is_file(fn);
|
---|
[1313ee9] | 599 | stat.is_directory = ops->is_directory(fn);
|
---|
[0143f72] | 600 | stat.size = ops->size_get(fn);
|
---|
[1313ee9] | 601 | stat.device = ops->device_get(fn);
|
---|
| 602 |
|
---|
| 603 | ops->node_put(fn);
|
---|
| 604 |
|
---|
[0da4e41] | 605 | async_data_read_finalize(callid, &stat, sizeof(struct stat));
|
---|
[75160a6] | 606 | ipc_answer_0(rid, EOK);
|
---|
| 607 | }
|
---|
| 608 |
|
---|
[595edf5] | 609 | /** Open VFS triplet.
|
---|
| 610 | *
|
---|
[1313ee9] | 611 | * @param ops libfs operations structure with function pointers to
|
---|
| 612 | * file system implementation
|
---|
| 613 | * @param rid Request ID of the VFS_OUT_OPEN_NODE request.
|
---|
| 614 | * @param request VFS_OUT_OPEN_NODE request data itself.
|
---|
[595edf5] | 615 | *
|
---|
| 616 | */
|
---|
| 617 | void libfs_open_node(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
|
---|
| 618 | ipc_call_t *request)
|
---|
| 619 | {
|
---|
[991f645] | 620 | devmap_handle_t devmap_handle = IPC_GET_ARG1(*request);
|
---|
[595edf5] | 621 | fs_index_t index = IPC_GET_ARG2(*request);
|
---|
| 622 |
|
---|
[ed903174] | 623 | fs_node_t *fn;
|
---|
[991f645] | 624 | int rc = ops->node_get(&fn, devmap_handle, index);
|
---|
[0daba212] | 625 | on_error(rc, answer_and_return(rid, rc));
|
---|
[595edf5] | 626 |
|
---|
[0daba212] | 627 | if (fn == NULL) {
|
---|
[595edf5] | 628 | ipc_answer_0(rid, ENOENT);
|
---|
| 629 | return;
|
---|
| 630 | }
|
---|
| 631 |
|
---|
[1313ee9] | 632 | rc = ops->node_open(fn);
|
---|
[ed903174] | 633 | aoff64_t size = ops->size_get(fn);
|
---|
| 634 | ipc_answer_4(rid, rc, LOWER32(size), UPPER32(size), ops->lnkcnt_get(fn),
|
---|
[2e983c7] | 635 | (ops->is_file(fn) ? L_FILE : 0) | (ops->is_directory(fn) ? L_DIRECTORY : 0));
|
---|
[595edf5] | 636 |
|
---|
[0daba212] | 637 | (void) ops->node_put(fn);
|
---|
[595edf5] | 638 | }
|
---|
| 639 |
|
---|
[74303b6] | 640 | /** @}
|
---|
| 641 | */
|
---|