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