[62da45a] | 1 | /*
|
---|
| 2 | * Copyright (c) 2008 Jakub Jermar
|
---|
| 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 |
|
---|
| 29 | /** @addtogroup fs
|
---|
| 30 | * @{
|
---|
[05b9912] | 31 | */
|
---|
[62da45a] | 32 |
|
---|
| 33 | /**
|
---|
[05b9912] | 34 | * @file vfs_lookup.c
|
---|
[62da45a] | 35 | * @brief
|
---|
| 36 | */
|
---|
| 37 |
|
---|
[dadcec1] | 38 | #include "vfs.h"
|
---|
[ed903174] | 39 | #include <macros.h>
|
---|
[62da45a] | 40 | #include <async.h>
|
---|
| 41 | #include <errno.h>
|
---|
[19f857a] | 42 | #include <str.h>
|
---|
[a8e9ab8d] | 43 | #include <stdarg.h>
|
---|
[62da45a] | 44 | #include <bool.h>
|
---|
[1e4cada] | 45 | #include <fibril_synch.h>
|
---|
[d9c8c81] | 46 | #include <adt/list.h>
|
---|
[dadcec1] | 47 | #include <vfs/canonify.h>
|
---|
[62da45a] | 48 |
|
---|
[05b9912] | 49 | #define min(a, b) ((a) < (b) ? (a) : (b))
|
---|
[62da45a] | 50 |
|
---|
[553492be] | 51 | FIBRIL_MUTEX_INITIALIZE(plb_mutex);
|
---|
[b72efe8] | 52 | LIST_INITIALIZE(plb_entries); /**< PLB entry ring buffer. */
|
---|
[62da45a] | 53 | uint8_t *plb = NULL;
|
---|
| 54 |
|
---|
| 55 | /** Perform a path lookup.
|
---|
| 56 | *
|
---|
[05b9912] | 57 | * @param path Path to be resolved; it must be a NULL-terminated
|
---|
| 58 | * string.
|
---|
| 59 | * @param lflag Flags to be used during lookup.
|
---|
| 60 | * @param result Empty structure where the lookup result will be stored.
|
---|
| 61 | * Can be NULL.
|
---|
| 62 | * @param altroot If non-empty, will be used instead of rootfs as the root
|
---|
| 63 | * of the whole VFS tree.
|
---|
| 64 | *
|
---|
| 65 | * @return EOK on success or an error code from errno.h.
|
---|
[62da45a] | 66 | *
|
---|
| 67 | */
|
---|
[d6084ef] | 68 | int vfs_lookup_internal(char *path, int lflag, vfs_lookup_res_t *result,
|
---|
[a8e9ab8d] | 69 | vfs_pair_t *altroot, ...)
|
---|
[62da45a] | 70 | {
|
---|
| 71 | vfs_pair_t *root;
|
---|
| 72 |
|
---|
| 73 | if (altroot)
|
---|
| 74 | root = altroot;
|
---|
| 75 | else
|
---|
[f49b0ea] | 76 | root = &rootfs;
|
---|
[62da45a] | 77 |
|
---|
| 78 | if (!root->fs_handle)
|
---|
| 79 | return ENOENT;
|
---|
| 80 |
|
---|
[d6084ef] | 81 | size_t len;
|
---|
| 82 | path = canonify(path, &len);
|
---|
| 83 | if (!path)
|
---|
| 84 | return EINVAL;
|
---|
| 85 |
|
---|
[f2ec8c8] | 86 | fs_index_t index = 0;
|
---|
[a8e9ab8d] | 87 | if (lflag & L_LINK) {
|
---|
| 88 | va_list ap;
|
---|
| 89 |
|
---|
| 90 | va_start(ap, altroot);
|
---|
[f2ec8c8] | 91 | index = va_arg(ap, fs_index_t);
|
---|
[a8e9ab8d] | 92 | va_end(ap);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[553492be] | 95 | fibril_mutex_lock(&plb_mutex);
|
---|
[62da45a] | 96 |
|
---|
| 97 | plb_entry_t entry;
|
---|
| 98 | link_initialize(&entry.plb_link);
|
---|
| 99 | entry.len = len;
|
---|
| 100 |
|
---|
[ed903174] | 101 | size_t first; /* the first free index */
|
---|
| 102 | size_t last; /* the last free index */
|
---|
[62da45a] | 103 |
|
---|
[b72efe8] | 104 | if (list_empty(&plb_entries)) {
|
---|
[62da45a] | 105 | first = 0;
|
---|
| 106 | last = PLB_SIZE - 1;
|
---|
| 107 | } else {
|
---|
[b72efe8] | 108 | plb_entry_t *oldest = list_get_instance(
|
---|
| 109 | list_first(&plb_entries), plb_entry_t, plb_link);
|
---|
| 110 | plb_entry_t *newest = list_get_instance(
|
---|
| 111 | list_last(&plb_entries), plb_entry_t, plb_link);
|
---|
[62da45a] | 112 |
|
---|
| 113 | first = (newest->index + newest->len) % PLB_SIZE;
|
---|
| 114 | last = (oldest->index - 1) % PLB_SIZE;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | if (first <= last) {
|
---|
| 118 | if ((last - first) + 1 < len) {
|
---|
| 119 | /*
|
---|
| 120 | * The buffer cannot absorb the path.
|
---|
| 121 | */
|
---|
[553492be] | 122 | fibril_mutex_unlock(&plb_mutex);
|
---|
[62da45a] | 123 | return ELIMIT;
|
---|
| 124 | }
|
---|
| 125 | } else {
|
---|
| 126 | if (PLB_SIZE - ((first - last) + 1) < len) {
|
---|
| 127 | /*
|
---|
| 128 | * The buffer cannot absorb the path.
|
---|
| 129 | */
|
---|
[553492be] | 130 | fibril_mutex_unlock(&plb_mutex);
|
---|
[62da45a] | 131 | return ELIMIT;
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | /*
|
---|
| 136 | * We know the first free index in PLB and we also know that there is
|
---|
| 137 | * enough space in the buffer to hold our path.
|
---|
| 138 | */
|
---|
| 139 |
|
---|
| 140 | entry.index = first;
|
---|
| 141 | entry.len = len;
|
---|
| 142 |
|
---|
| 143 | /*
|
---|
| 144 | * Claim PLB space by inserting the entry into the PLB entry ring
|
---|
| 145 | * buffer.
|
---|
| 146 | */
|
---|
[b72efe8] | 147 | list_append(&entry.plb_link, &plb_entries);
|
---|
[62da45a] | 148 |
|
---|
[553492be] | 149 | fibril_mutex_unlock(&plb_mutex);
|
---|
[62da45a] | 150 |
|
---|
| 151 | /*
|
---|
| 152 | * Copy the path into PLB.
|
---|
| 153 | */
|
---|
| 154 | size_t cnt1 = min(len, (PLB_SIZE - first) + 1);
|
---|
| 155 | size_t cnt2 = len - cnt1;
|
---|
| 156 |
|
---|
| 157 | memcpy(&plb[first], path, cnt1);
|
---|
| 158 | memcpy(plb, &path[cnt1], cnt2);
|
---|
| 159 |
|
---|
| 160 | ipc_call_t answer;
|
---|
[79ae36dd] | 161 | async_exch_t *exch = vfs_exchange_grab(root->fs_handle);
|
---|
| 162 | aid_t req = async_send_5(exch, VFS_OUT_LOOKUP, (sysarg_t) first,
|
---|
[96b02eb9] | 163 | (sysarg_t) (first + len - 1) % PLB_SIZE,
|
---|
[15f3c3f] | 164 | (sysarg_t) root->service_id, (sysarg_t) lflag, (sysarg_t) index,
|
---|
[a8e9ab8d] | 165 | &answer);
|
---|
[50461f2] | 166 |
|
---|
[96b02eb9] | 167 | sysarg_t rc;
|
---|
[62da45a] | 168 | async_wait_for(req, &rc);
|
---|
[79ae36dd] | 169 | vfs_exchange_release(exch);
|
---|
[50461f2] | 170 |
|
---|
[553492be] | 171 | fibril_mutex_lock(&plb_mutex);
|
---|
[62da45a] | 172 | list_remove(&entry.plb_link);
|
---|
| 173 | /*
|
---|
| 174 | * Erasing the path from PLB will come handy for debugging purposes.
|
---|
| 175 | */
|
---|
| 176 | memset(&plb[first], 0, cnt1);
|
---|
| 177 | memset(plb, 0, cnt2);
|
---|
[553492be] | 178 | fibril_mutex_unlock(&plb_mutex);
|
---|
[ed903174] | 179 |
|
---|
[cead2aa] | 180 | if ((int) rc < EOK)
|
---|
[ed903174] | 181 | return (int) rc;
|
---|
[cead2aa] | 182 |
|
---|
| 183 | if (!result)
|
---|
| 184 | return EOK;
|
---|
[ed903174] | 185 |
|
---|
| 186 | result->triplet.fs_handle = (fs_handle_t) rc;
|
---|
[15f3c3f] | 187 | result->triplet.service_id = (service_id_t) IPC_GET_ARG1(answer);
|
---|
[ed903174] | 188 | result->triplet.index = (fs_index_t) IPC_GET_ARG2(answer);
|
---|
| 189 | result->size =
|
---|
| 190 | (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(answer), IPC_GET_ARG4(answer));
|
---|
| 191 | result->lnkcnt = (unsigned int) IPC_GET_ARG5(answer);
|
---|
| 192 |
|
---|
| 193 | if (lflag & L_FILE)
|
---|
| 194 | result->type = VFS_NODE_FILE;
|
---|
| 195 | else if (lflag & L_DIRECTORY)
|
---|
| 196 | result->type = VFS_NODE_DIRECTORY;
|
---|
| 197 | else
|
---|
| 198 | result->type = VFS_NODE_UNKNOWN;
|
---|
| 199 |
|
---|
| 200 | return EOK;
|
---|
[62da45a] | 201 | }
|
---|
| 202 |
|
---|
[05b9912] | 203 | /** Perform a node open operation.
|
---|
| 204 | *
|
---|
| 205 | * @return EOK on success or an error code from errno.h.
|
---|
| 206 | *
|
---|
| 207 | */
|
---|
| 208 | int vfs_open_node_internal(vfs_lookup_res_t *result)
|
---|
| 209 | {
|
---|
[79ae36dd] | 210 | async_exch_t *exch = vfs_exchange_grab(result->triplet.fs_handle);
|
---|
[05b9912] | 211 |
|
---|
| 212 | ipc_call_t answer;
|
---|
[79ae36dd] | 213 | aid_t req = async_send_2(exch, VFS_OUT_OPEN_NODE,
|
---|
[15f3c3f] | 214 | (sysarg_t) result->triplet.service_id,
|
---|
[96b02eb9] | 215 | (sysarg_t) result->triplet.index, &answer);
|
---|
[05b9912] | 216 |
|
---|
[96b02eb9] | 217 | sysarg_t rc;
|
---|
[05b9912] | 218 | async_wait_for(req, &rc);
|
---|
[79ae36dd] | 219 | vfs_exchange_release(exch);
|
---|
[05b9912] | 220 |
|
---|
| 221 | if (rc == EOK) {
|
---|
[ed903174] | 222 | result->size =
|
---|
| 223 | MERGE_LOUP32(IPC_GET_ARG1(answer), IPC_GET_ARG2(answer));
|
---|
| 224 | result->lnkcnt = (unsigned int) IPC_GET_ARG3(answer);
|
---|
| 225 | if (IPC_GET_ARG4(answer) & L_FILE)
|
---|
[05b9912] | 226 | result->type = VFS_NODE_FILE;
|
---|
[ed903174] | 227 | else if (IPC_GET_ARG4(answer) & L_DIRECTORY)
|
---|
[05b9912] | 228 | result->type = VFS_NODE_DIRECTORY;
|
---|
| 229 | else
|
---|
| 230 | result->type = VFS_NODE_UNKNOWN;
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | return rc;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
[62da45a] | 236 | /**
|
---|
| 237 | * @}
|
---|
[9bb85f3] | 238 | */
|
---|