| [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 | * @{
|
|---|
| [8dc72b64] | 31 | */
|
|---|
| [62da45a] | 32 |
|
|---|
| 33 | /**
|
|---|
| [8dc72b64] | 34 | * @file vfs_register.c
|
|---|
| [62da45a] | 35 | * @brief
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| 38 | #include <ipc/services.h>
|
|---|
| 39 | #include <async.h>
|
|---|
| 40 | #include <fibril.h>
|
|---|
| [df908b3] | 41 | #include <fibril_synch.h>
|
|---|
| [62da45a] | 42 | #include <errno.h>
|
|---|
| 43 | #include <stdio.h>
|
|---|
| 44 | #include <stdlib.h>
|
|---|
| [19f857a] | 45 | #include <str.h>
|
|---|
| [62da45a] | 46 | #include <ctype.h>
|
|---|
| 47 | #include <bool.h>
|
|---|
| [d9c8c81] | 48 | #include <adt/list.h>
|
|---|
| [62da45a] | 49 | #include <as.h>
|
|---|
| 50 | #include <assert.h>
|
|---|
| 51 | #include <atomic.h>
|
|---|
| 52 | #include "vfs.h"
|
|---|
| 53 |
|
|---|
| [b72efe8] | 54 | FIBRIL_CONDVAR_INITIALIZE(fs_list_cv);
|
|---|
| 55 | FIBRIL_MUTEX_INITIALIZE(fs_list_lock);
|
|---|
| 56 | LIST_INITIALIZE(fs_list);
|
|---|
| [62da45a] | 57 |
|
|---|
| 58 | atomic_t fs_handle_next = {
|
|---|
| 59 | .count = 1
|
|---|
| 60 | };
|
|---|
| 61 |
|
|---|
| 62 | /** Verify the VFS info structure.
|
|---|
| 63 | *
|
|---|
| [79ae36dd] | 64 | * @param info Info structure to be verified.
|
|---|
| 65 | *
|
|---|
| 66 | * @return Non-zero if the info structure is sane, zero otherwise.
|
|---|
| [62da45a] | 67 | *
|
|---|
| 68 | */
|
|---|
| 69 | static bool vfs_info_sane(vfs_info_t *info)
|
|---|
| 70 | {
|
|---|
| 71 | int i;
|
|---|
| [79ae36dd] | 72 |
|
|---|
| [62da45a] | 73 | /*
|
|---|
| 74 | * Check if the name is non-empty and is composed solely of ASCII
|
|---|
| 75 | * characters [a-z]+[a-z0-9_-]*.
|
|---|
| 76 | */
|
|---|
| 77 | if (!islower(info->name[0])) {
|
|---|
| 78 | dprintf("The name doesn't start with a lowercase character.\n");
|
|---|
| 79 | return false;
|
|---|
| 80 | }
|
|---|
| [79ae36dd] | 81 |
|
|---|
| [62da45a] | 82 | for (i = 1; i < FS_NAME_MAXLEN; i++) {
|
|---|
| 83 | if (!(islower(info->name[i]) || isdigit(info->name[i])) &&
|
|---|
| 84 | (info->name[i] != '-') && (info->name[i] != '_')) {
|
|---|
| 85 | if (info->name[i] == '\0') {
|
|---|
| 86 | break;
|
|---|
| 87 | } else {
|
|---|
| 88 | dprintf("The name contains illegal "
|
|---|
| 89 | "characters.\n");
|
|---|
| 90 | return false;
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|
| 93 | }
|
|---|
| [79ae36dd] | 94 |
|
|---|
| [62da45a] | 95 | /*
|
|---|
| 96 | * This check is not redundant. It ensures that the name is
|
|---|
| 97 | * NULL-terminated, even if FS_NAME_MAXLEN characters are used.
|
|---|
| 98 | */
|
|---|
| 99 | if (info->name[i] != '\0') {
|
|---|
| 100 | dprintf("The name is not properly NULL-terminated.\n");
|
|---|
| 101 | return false;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | return true;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | /** VFS_REGISTER protocol function.
|
|---|
| 108 | *
|
|---|
| [79ae36dd] | 109 | * @param rid Hash of the call with the request.
|
|---|
| 110 | * @param request Call structure with the request.
|
|---|
| 111 | *
|
|---|
| [62da45a] | 112 | */
|
|---|
| 113 | void vfs_register(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 114 | {
|
|---|
| 115 | dprintf("Processing VFS_REGISTER request received from %p.\n",
|
|---|
| 116 | request->in_phone_hash);
|
|---|
| 117 |
|
|---|
| [b4cbef1] | 118 | vfs_info_t *vfs_info;
|
|---|
| [eda925a] | 119 | int rc = async_data_write_accept((void **) &vfs_info, false,
|
|---|
| 120 | sizeof(vfs_info_t), sizeof(vfs_info_t), 0, NULL);
|
|---|
| [62da45a] | 121 |
|
|---|
| [b4cbef1] | 122 | if (rc != EOK) {
|
|---|
| 123 | dprintf("Failed to deliver the VFS info into our AS, rc=%d.\n",
|
|---|
| 124 | rc);
|
|---|
| [ffa2c8ef] | 125 | async_answer_0(rid, rc);
|
|---|
| [62da45a] | 126 | return;
|
|---|
| 127 | }
|
|---|
| [b4cbef1] | 128 |
|
|---|
| [62da45a] | 129 | /*
|
|---|
| 130 | * Allocate and initialize a buffer for the fs_info structure.
|
|---|
| 131 | */
|
|---|
| [b4cbef1] | 132 | fs_info_t *fs_info = (fs_info_t *) malloc(sizeof(fs_info_t));
|
|---|
| [62da45a] | 133 | if (!fs_info) {
|
|---|
| 134 | dprintf("Could not allocate memory for FS info.\n");
|
|---|
| [ffa2c8ef] | 135 | async_answer_0(rid, ENOMEM);
|
|---|
| [62da45a] | 136 | return;
|
|---|
| 137 | }
|
|---|
| [b4cbef1] | 138 |
|
|---|
| [62da45a] | 139 | link_initialize(&fs_info->fs_link);
|
|---|
| [b4cbef1] | 140 | fs_info->vfs_info = *vfs_info;
|
|---|
| 141 | free(vfs_info);
|
|---|
| 142 |
|
|---|
| [62da45a] | 143 | dprintf("VFS info delivered.\n");
|
|---|
| [b4cbef1] | 144 |
|
|---|
| [62da45a] | 145 | if (!vfs_info_sane(&fs_info->vfs_info)) {
|
|---|
| 146 | free(fs_info);
|
|---|
| [ffa2c8ef] | 147 | async_answer_0(rid, EINVAL);
|
|---|
| [62da45a] | 148 | return;
|
|---|
| 149 | }
|
|---|
| [b4cbef1] | 150 |
|
|---|
| [b72efe8] | 151 | fibril_mutex_lock(&fs_list_lock);
|
|---|
| [b4cbef1] | 152 |
|
|---|
| [62da45a] | 153 | /*
|
|---|
| 154 | * Check for duplicit registrations.
|
|---|
| 155 | */
|
|---|
| 156 | if (fs_name_to_handle(fs_info->vfs_info.name, false)) {
|
|---|
| 157 | /*
|
|---|
| 158 | * We already register a fs like this.
|
|---|
| 159 | */
|
|---|
| 160 | dprintf("FS is already registered.\n");
|
|---|
| [b72efe8] | 161 | fibril_mutex_unlock(&fs_list_lock);
|
|---|
| [62da45a] | 162 | free(fs_info);
|
|---|
| [ffa2c8ef] | 163 | async_answer_0(rid, EEXISTS);
|
|---|
| [62da45a] | 164 | return;
|
|---|
| 165 | }
|
|---|
| [b4cbef1] | 166 |
|
|---|
| [62da45a] | 167 | /*
|
|---|
| 168 | * Add fs_info to the list of registered FS's.
|
|---|
| 169 | */
|
|---|
| 170 | dprintf("Inserting FS into the list of registered file systems.\n");
|
|---|
| [b72efe8] | 171 | list_append(&fs_info->fs_link, &fs_list);
|
|---|
| [07deef5] | 172 |
|
|---|
| [62da45a] | 173 | /*
|
|---|
| 174 | * Now we want the client to send us the IPC_M_CONNECT_TO_ME call so
|
|---|
| 175 | * that a callback connection is created and we have a phone through
|
|---|
| 176 | * which to forward VFS requests to it.
|
|---|
| 177 | */
|
|---|
| [79ae36dd] | 178 | fs_info->sess = async_callback_receive(EXCHANGE_PARALLEL);
|
|---|
| 179 | if (!fs_info->sess) {
|
|---|
| 180 | dprintf("Callback connection expected\n");
|
|---|
| [62da45a] | 181 | list_remove(&fs_info->fs_link);
|
|---|
| [b72efe8] | 182 | fibril_mutex_unlock(&fs_list_lock);
|
|---|
| [62da45a] | 183 | free(fs_info);
|
|---|
| [ffa2c8ef] | 184 | async_answer_0(rid, EINVAL);
|
|---|
| [62da45a] | 185 | return;
|
|---|
| 186 | }
|
|---|
| [c1c0184] | 187 |
|
|---|
| [62da45a] | 188 | dprintf("Callback connection to FS created.\n");
|
|---|
| [b4cbef1] | 189 |
|
|---|
| [62da45a] | 190 | /*
|
|---|
| 191 | * The client will want us to send him the address space area with PLB.
|
|---|
| 192 | */
|
|---|
| [b4cbef1] | 193 |
|
|---|
| 194 | size_t size;
|
|---|
| [79ae36dd] | 195 | ipc_callid_t callid;
|
|---|
| [0da4e41] | 196 | if (!async_share_in_receive(&callid, &size)) {
|
|---|
| [228e490] | 197 | dprintf("Unexpected call, method = %d\n", IPC_GET_IMETHOD(call));
|
|---|
| [62da45a] | 198 | list_remove(&fs_info->fs_link);
|
|---|
| [b72efe8] | 199 | fibril_mutex_unlock(&fs_list_lock);
|
|---|
| [79ae36dd] | 200 | async_hangup(fs_info->sess);
|
|---|
| [62da45a] | 201 | free(fs_info);
|
|---|
| [ffa2c8ef] | 202 | async_answer_0(callid, EINVAL);
|
|---|
| 203 | async_answer_0(rid, EINVAL);
|
|---|
| [62da45a] | 204 | return;
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | /*
|
|---|
| 208 | * We can only send the client address space area PLB_SIZE bytes long.
|
|---|
| 209 | */
|
|---|
| 210 | if (size != PLB_SIZE) {
|
|---|
| 211 | dprintf("Client suggests wrong size of PFB, size = %d\n", size);
|
|---|
| 212 | list_remove(&fs_info->fs_link);
|
|---|
| [b72efe8] | 213 | fibril_mutex_unlock(&fs_list_lock);
|
|---|
| [79ae36dd] | 214 | async_hangup(fs_info->sess);
|
|---|
| [62da45a] | 215 | free(fs_info);
|
|---|
| [ffa2c8ef] | 216 | async_answer_0(callid, EINVAL);
|
|---|
| 217 | async_answer_0(rid, EINVAL);
|
|---|
| [62da45a] | 218 | return;
|
|---|
| 219 | }
|
|---|
| [b4cbef1] | 220 |
|
|---|
| [62da45a] | 221 | /*
|
|---|
| 222 | * Commit to read-only sharing the PLB with the client.
|
|---|
| 223 | */
|
|---|
| [0da4e41] | 224 | (void) async_share_in_finalize(callid, plb,
|
|---|
| [62da45a] | 225 | AS_AREA_READ | AS_AREA_CACHEABLE);
|
|---|
| [b4cbef1] | 226 |
|
|---|
| [62da45a] | 227 | dprintf("Sharing PLB.\n");
|
|---|
| [b4cbef1] | 228 |
|
|---|
| [62da45a] | 229 | /*
|
|---|
| 230 | * That was it. The FS has been registered.
|
|---|
| 231 | * In reply to the VFS_REGISTER request, we assign the client file
|
|---|
| 232 | * system a global file system handle.
|
|---|
| 233 | */
|
|---|
| [f2ec8c8] | 234 | fs_info->fs_handle = (fs_handle_t) atomic_postinc(&fs_handle_next);
|
|---|
| [ffa2c8ef] | 235 | async_answer_1(rid, EOK, (sysarg_t) fs_info->fs_handle);
|
|---|
| [62da45a] | 236 |
|
|---|
| [b72efe8] | 237 | fibril_condvar_broadcast(&fs_list_cv);
|
|---|
| 238 | fibril_mutex_unlock(&fs_list_lock);
|
|---|
| [62da45a] | 239 |
|
|---|
| 240 | dprintf("\"%.*s\" filesystem successfully registered, handle=%d.\n",
|
|---|
| 241 | FS_NAME_MAXLEN, fs_info->vfs_info.name, fs_info->fs_handle);
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| [79ae36dd] | 244 | /** Begin an exchange for a given file system handle
|
|---|
| [62da45a] | 245 | *
|
|---|
| [79ae36dd] | 246 | * @param handle File system handle.
|
|---|
| 247 | *
|
|---|
| 248 | * @return Exchange for a multi-call request.
|
|---|
| 249 | * @return NULL if no such file exists.
|
|---|
| [62da45a] | 250 | *
|
|---|
| 251 | */
|
|---|
| [79ae36dd] | 252 | async_exch_t *vfs_exchange_grab(fs_handle_t handle)
|
|---|
| [62da45a] | 253 | {
|
|---|
| 254 | /*
|
|---|
| [79ae36dd] | 255 | * For now, we don't try to be very clever and very fast.
|
|---|
| [b72efe8] | 256 | * We simply lookup the session in fs_list and
|
|---|
| [79ae36dd] | 257 | * begin an exchange.
|
|---|
| [62da45a] | 258 | */
|
|---|
| [b72efe8] | 259 | fibril_mutex_lock(&fs_list_lock);
|
|---|
| [79ae36dd] | 260 |
|
|---|
| [b72efe8] | 261 | list_foreach(fs_list, cur) {
|
|---|
| [79ae36dd] | 262 | fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link);
|
|---|
| 263 |
|
|---|
| [62da45a] | 264 | if (fs->fs_handle == handle) {
|
|---|
| [b72efe8] | 265 | fibril_mutex_unlock(&fs_list_lock);
|
|---|
| [79ae36dd] | 266 |
|
|---|
| 267 | assert(fs->sess);
|
|---|
| 268 | async_exch_t *exch = async_exchange_begin(fs->sess);
|
|---|
| 269 |
|
|---|
| 270 | assert(exch);
|
|---|
| 271 | return exch;
|
|---|
| [62da45a] | 272 | }
|
|---|
| 273 | }
|
|---|
| [79ae36dd] | 274 |
|
|---|
| [b72efe8] | 275 | fibril_mutex_unlock(&fs_list_lock);
|
|---|
| [79ae36dd] | 276 |
|
|---|
| 277 | return NULL;
|
|---|
| [62da45a] | 278 | }
|
|---|
| 279 |
|
|---|
| [79ae36dd] | 280 | /** End VFS server exchange.
|
|---|
| 281 | *
|
|---|
| 282 | * @param exch VFS server exchange.
|
|---|
| [62da45a] | 283 | *
|
|---|
| 284 | */
|
|---|
| [79ae36dd] | 285 | void vfs_exchange_release(async_exch_t *exch)
|
|---|
| [62da45a] | 286 | {
|
|---|
| [79ae36dd] | 287 | async_exchange_end(exch);
|
|---|
| [62da45a] | 288 | }
|
|---|
| 289 |
|
|---|
| 290 | /** Convert file system name to its handle.
|
|---|
| 291 | *
|
|---|
| [79ae36dd] | 292 | * @param name File system name.
|
|---|
| 293 | * @param lock If true, the function will lock and unlock the
|
|---|
| [b72efe8] | 294 | * fs_list_lock.
|
|---|
| [79ae36dd] | 295 | *
|
|---|
| 296 | * @return File system handle or zero if file system not found.
|
|---|
| [62da45a] | 297 | *
|
|---|
| 298 | */
|
|---|
| [f2ec8c8] | 299 | fs_handle_t fs_name_to_handle(char *name, bool lock)
|
|---|
| [62da45a] | 300 | {
|
|---|
| 301 | int handle = 0;
|
|---|
| 302 |
|
|---|
| 303 | if (lock)
|
|---|
| [b72efe8] | 304 | fibril_mutex_lock(&fs_list_lock);
|
|---|
| [79ae36dd] | 305 |
|
|---|
| [b72efe8] | 306 | list_foreach(fs_list, cur) {
|
|---|
| [62da45a] | 307 | fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link);
|
|---|
| [15f3c3f] | 308 | if (str_cmp(fs->vfs_info.name, name) == 0) {
|
|---|
| [62da45a] | 309 | handle = fs->fs_handle;
|
|---|
| 310 | break;
|
|---|
| 311 | }
|
|---|
| 312 | }
|
|---|
| [79ae36dd] | 313 |
|
|---|
| [62da45a] | 314 | if (lock)
|
|---|
| [b72efe8] | 315 | fibril_mutex_unlock(&fs_list_lock);
|
|---|
| [79ae36dd] | 316 |
|
|---|
| [62da45a] | 317 | return handle;
|
|---|
| 318 | }
|
|---|
| 319 |
|
|---|
| [f07f6b2] | 320 | /** Find the VFS info structure.
|
|---|
| 321 | *
|
|---|
| [79ae36dd] | 322 | * @param handle FS handle for which the VFS info structure is sought.
|
|---|
| 323 | *
|
|---|
| 324 | * @return VFS info structure on success or NULL otherwise.
|
|---|
| 325 | *
|
|---|
| [f07f6b2] | 326 | */
|
|---|
| 327 | vfs_info_t *fs_handle_to_info(fs_handle_t handle)
|
|---|
| 328 | {
|
|---|
| 329 | vfs_info_t *info = NULL;
|
|---|
| [79ae36dd] | 330 |
|
|---|
| [b72efe8] | 331 | fibril_mutex_lock(&fs_list_lock);
|
|---|
| 332 | list_foreach(fs_list, cur) {
|
|---|
| [f07f6b2] | 333 | fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link);
|
|---|
| 334 | if (fs->fs_handle == handle) {
|
|---|
| 335 | info = &fs->vfs_info;
|
|---|
| 336 | break;
|
|---|
| 337 | }
|
|---|
| 338 | }
|
|---|
| [b72efe8] | 339 | fibril_mutex_unlock(&fs_list_lock);
|
|---|
| [79ae36dd] | 340 |
|
|---|
| [f07f6b2] | 341 | return info;
|
|---|
| 342 | }
|
|---|
| 343 |
|
|---|
| [62da45a] | 344 | /**
|
|---|
| 345 | * @}
|
|---|
| [79ae36dd] | 346 | */
|
|---|