[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/ipc.h>
|
---|
| 39 | #include <ipc/services.h>
|
---|
| 40 | #include <async.h>
|
---|
| 41 | #include <fibril.h>
|
---|
| 42 | #include <errno.h>
|
---|
| 43 | #include <stdio.h>
|
---|
| 44 | #include <stdlib.h>
|
---|
| 45 | #include <string.h>
|
---|
| 46 | #include <ctype.h>
|
---|
| 47 | #include <bool.h>
|
---|
[1e4cada] | 48 | #include <fibril_synch.h>
|
---|
[d9c8c81] | 49 | #include <adt/list.h>
|
---|
[62da45a] | 50 | #include <as.h>
|
---|
| 51 | #include <assert.h>
|
---|
| 52 | #include <atomic.h>
|
---|
| 53 | #include "vfs.h"
|
---|
| 54 |
|
---|
[7b47fa2] | 55 | FIBRIL_CONDVAR_INITIALIZE(fs_head_cv);
|
---|
[230260ac] | 56 | FIBRIL_MUTEX_INITIALIZE(fs_head_lock);
|
---|
[7b47fa2] | 57 | LIST_INITIALIZE(fs_head);
|
---|
[62da45a] | 58 |
|
---|
| 59 | atomic_t fs_handle_next = {
|
---|
| 60 | .count = 1
|
---|
| 61 | };
|
---|
| 62 |
|
---|
| 63 | /** Verify the VFS info structure.
|
---|
| 64 | *
|
---|
| 65 | * @param info Info structure to be verified.
|
---|
| 66 | *
|
---|
| 67 | * @return Non-zero if the info structure is sane, zero otherwise.
|
---|
| 68 | */
|
---|
| 69 | static bool vfs_info_sane(vfs_info_t *info)
|
---|
| 70 | {
|
---|
| 71 | int i;
|
---|
| 72 |
|
---|
| 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 | }
|
---|
| 81 | for (i = 1; i < FS_NAME_MAXLEN; i++) {
|
---|
| 82 | if (!(islower(info->name[i]) || isdigit(info->name[i])) &&
|
---|
| 83 | (info->name[i] != '-') && (info->name[i] != '_')) {
|
---|
| 84 | if (info->name[i] == '\0') {
|
---|
| 85 | break;
|
---|
| 86 | } else {
|
---|
| 87 | dprintf("The name contains illegal "
|
---|
| 88 | "characters.\n");
|
---|
| 89 | return false;
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 | /*
|
---|
| 94 | * This check is not redundant. It ensures that the name is
|
---|
| 95 | * NULL-terminated, even if FS_NAME_MAXLEN characters are used.
|
---|
| 96 | */
|
---|
| 97 | if (info->name[i] != '\0') {
|
---|
| 98 | dprintf("The name is not properly NULL-terminated.\n");
|
---|
| 99 | return false;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | return true;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | /** VFS_REGISTER protocol function.
|
---|
| 106 | *
|
---|
| 107 | * @param rid Hash of the call with the request.
|
---|
| 108 | * @param request Call structure with the request.
|
---|
| 109 | */
|
---|
| 110 | void vfs_register(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 111 | {
|
---|
| 112 | dprintf("Processing VFS_REGISTER request received from %p.\n",
|
---|
| 113 | request->in_phone_hash);
|
---|
| 114 |
|
---|
[b4cbef1] | 115 | vfs_info_t *vfs_info;
|
---|
| 116 | int rc = async_data_receive(&vfs_info, sizeof(vfs_info_t),
|
---|
| 117 | sizeof(vfs_info_t), 0, NULL);
|
---|
[62da45a] | 118 |
|
---|
[b4cbef1] | 119 | if (rc != EOK) {
|
---|
| 120 | dprintf("Failed to deliver the VFS info into our AS, rc=%d.\n",
|
---|
| 121 | rc);
|
---|
| 122 | ipc_answer_0(rid, rc);
|
---|
[62da45a] | 123 | return;
|
---|
| 124 | }
|
---|
[b4cbef1] | 125 |
|
---|
[62da45a] | 126 | /*
|
---|
| 127 | * Allocate and initialize a buffer for the fs_info structure.
|
---|
| 128 | */
|
---|
[b4cbef1] | 129 | fs_info_t *fs_info = (fs_info_t *) malloc(sizeof(fs_info_t));
|
---|
[62da45a] | 130 | if (!fs_info) {
|
---|
| 131 | dprintf("Could not allocate memory for FS info.\n");
|
---|
| 132 | ipc_answer_0(rid, ENOMEM);
|
---|
| 133 | return;
|
---|
| 134 | }
|
---|
[b4cbef1] | 135 |
|
---|
[62da45a] | 136 | link_initialize(&fs_info->fs_link);
|
---|
[230260ac] | 137 | fibril_mutex_initialize(&fs_info->phone_lock);
|
---|
[b4cbef1] | 138 | fs_info->vfs_info = *vfs_info;
|
---|
| 139 | free(vfs_info);
|
---|
| 140 |
|
---|
[62da45a] | 141 | dprintf("VFS info delivered.\n");
|
---|
[b4cbef1] | 142 |
|
---|
[62da45a] | 143 | if (!vfs_info_sane(&fs_info->vfs_info)) {
|
---|
| 144 | free(fs_info);
|
---|
| 145 | ipc_answer_0(rid, EINVAL);
|
---|
| 146 | return;
|
---|
| 147 | }
|
---|
[b4cbef1] | 148 |
|
---|
[230260ac] | 149 | fibril_mutex_lock(&fs_head_lock);
|
---|
[b4cbef1] | 150 |
|
---|
[62da45a] | 151 | /*
|
---|
| 152 | * Check for duplicit registrations.
|
---|
| 153 | */
|
---|
| 154 | if (fs_name_to_handle(fs_info->vfs_info.name, false)) {
|
---|
| 155 | /*
|
---|
| 156 | * We already register a fs like this.
|
---|
| 157 | */
|
---|
| 158 | dprintf("FS is already registered.\n");
|
---|
[230260ac] | 159 | fibril_mutex_unlock(&fs_head_lock);
|
---|
[62da45a] | 160 | free(fs_info);
|
---|
| 161 | ipc_answer_0(rid, EEXISTS);
|
---|
| 162 | return;
|
---|
| 163 | }
|
---|
[b4cbef1] | 164 |
|
---|
[62da45a] | 165 | /*
|
---|
| 166 | * Add fs_info to the list of registered FS's.
|
---|
| 167 | */
|
---|
| 168 | dprintf("Inserting FS into the list of registered file systems.\n");
|
---|
| 169 | list_append(&fs_info->fs_link, &fs_head);
|
---|
[07deef5] | 170 |
|
---|
[62da45a] | 171 | /*
|
---|
| 172 | * Now we want the client to send us the IPC_M_CONNECT_TO_ME call so
|
---|
| 173 | * that a callback connection is created and we have a phone through
|
---|
| 174 | * which to forward VFS requests to it.
|
---|
| 175 | */
|
---|
[b4cbef1] | 176 | ipc_call_t call;
|
---|
| 177 | ipc_callid_t callid = async_get_call(&call);
|
---|
[62da45a] | 178 | if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) {
|
---|
| 179 | dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call));
|
---|
| 180 | list_remove(&fs_info->fs_link);
|
---|
[230260ac] | 181 | fibril_mutex_unlock(&fs_head_lock);
|
---|
[62da45a] | 182 | free(fs_info);
|
---|
| 183 | ipc_answer_0(callid, EINVAL);
|
---|
| 184 | ipc_answer_0(rid, EINVAL);
|
---|
| 185 | return;
|
---|
| 186 | }
|
---|
| 187 | fs_info->phone = IPC_GET_ARG5(call);
|
---|
| 188 | ipc_answer_0(callid, EOK);
|
---|
[b4cbef1] | 189 |
|
---|
[62da45a] | 190 | dprintf("Callback connection to FS created.\n");
|
---|
[b4cbef1] | 191 |
|
---|
[62da45a] | 192 | /*
|
---|
| 193 | * The client will want us to send him the address space area with PLB.
|
---|
| 194 | */
|
---|
[b4cbef1] | 195 |
|
---|
| 196 | size_t size;
|
---|
[0da4e41] | 197 | if (!async_share_in_receive(&callid, &size)) {
|
---|
[62da45a] | 198 | dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call));
|
---|
| 199 | list_remove(&fs_info->fs_link);
|
---|
[230260ac] | 200 | fibril_mutex_unlock(&fs_head_lock);
|
---|
[62da45a] | 201 | ipc_hangup(fs_info->phone);
|
---|
| 202 | free(fs_info);
|
---|
| 203 | ipc_answer_0(callid, EINVAL);
|
---|
| 204 | ipc_answer_0(rid, EINVAL);
|
---|
| 205 | return;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | /*
|
---|
| 209 | * We can only send the client address space area PLB_SIZE bytes long.
|
---|
| 210 | */
|
---|
| 211 | if (size != PLB_SIZE) {
|
---|
| 212 | dprintf("Client suggests wrong size of PFB, size = %d\n", size);
|
---|
| 213 | list_remove(&fs_info->fs_link);
|
---|
[230260ac] | 214 | fibril_mutex_unlock(&fs_head_lock);
|
---|
[62da45a] | 215 | ipc_hangup(fs_info->phone);
|
---|
| 216 | free(fs_info);
|
---|
| 217 | ipc_answer_0(callid, EINVAL);
|
---|
| 218 | ipc_answer_0(rid, EINVAL);
|
---|
| 219 | return;
|
---|
| 220 | }
|
---|
[b4cbef1] | 221 |
|
---|
[62da45a] | 222 | /*
|
---|
| 223 | * Commit to read-only sharing the PLB with the client.
|
---|
| 224 | */
|
---|
[0da4e41] | 225 | (void) async_share_in_finalize(callid, plb,
|
---|
[62da45a] | 226 | AS_AREA_READ | AS_AREA_CACHEABLE);
|
---|
[b4cbef1] | 227 |
|
---|
[62da45a] | 228 | dprintf("Sharing PLB.\n");
|
---|
[b4cbef1] | 229 |
|
---|
[62da45a] | 230 | /*
|
---|
| 231 | * That was it. The FS has been registered.
|
---|
| 232 | * In reply to the VFS_REGISTER request, we assign the client file
|
---|
| 233 | * system a global file system handle.
|
---|
| 234 | */
|
---|
[f2ec8c8] | 235 | fs_info->fs_handle = (fs_handle_t) atomic_postinc(&fs_handle_next);
|
---|
[62da45a] | 236 | ipc_answer_1(rid, EOK, (ipcarg_t) fs_info->fs_handle);
|
---|
| 237 |
|
---|
[9593bc8] | 238 | fibril_condvar_broadcast(&fs_head_cv);
|
---|
[230260ac] | 239 | fibril_mutex_unlock(&fs_head_lock);
|
---|
[62da45a] | 240 |
|
---|
| 241 | dprintf("\"%.*s\" filesystem successfully registered, handle=%d.\n",
|
---|
| 242 | FS_NAME_MAXLEN, fs_info->vfs_info.name, fs_info->fs_handle);
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | /** For a given file system handle, implement policy for allocating a phone.
|
---|
| 246 | *
|
---|
| 247 | * @param handle File system handle.
|
---|
| 248 | *
|
---|
| 249 | * @return Phone over which a multi-call request can be safely
|
---|
| 250 | * sent. Return 0 if no phone was found.
|
---|
| 251 | */
|
---|
[f2ec8c8] | 252 | int vfs_grab_phone(fs_handle_t handle)
|
---|
[62da45a] | 253 | {
|
---|
[34ca870] | 254 | int phone;
|
---|
| 255 |
|
---|
[62da45a] | 256 | /*
|
---|
[34ca870] | 257 | * For now, we don't try to be very clever and very fast. We simply
|
---|
| 258 | * lookup the phone in the fs_head list and duplicate it. The duplicate
|
---|
| 259 | * phone will be returned to the client and the client will use it for
|
---|
| 260 | * communication. In the future, we should cache the connections so
|
---|
| 261 | * that they do not have to be reestablished over and over again.
|
---|
[62da45a] | 262 | */
|
---|
[230260ac] | 263 | fibril_mutex_lock(&fs_head_lock);
|
---|
[62da45a] | 264 | link_t *cur;
|
---|
| 265 | fs_info_t *fs;
|
---|
| 266 | for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
|
---|
| 267 | fs = list_get_instance(cur, fs_info_t, fs_link);
|
---|
| 268 | if (fs->fs_handle == handle) {
|
---|
[230260ac] | 269 | fibril_mutex_unlock(&fs_head_lock);
|
---|
| 270 | fibril_mutex_lock(&fs->phone_lock);
|
---|
[34ca870] | 271 | phone = ipc_connect_me_to(fs->phone, 0, 0, 0);
|
---|
| 272 | fibril_mutex_unlock(&fs->phone_lock);
|
---|
| 273 |
|
---|
| 274 | assert(phone > 0);
|
---|
| 275 | return phone;
|
---|
[62da45a] | 276 | }
|
---|
| 277 | }
|
---|
[230260ac] | 278 | fibril_mutex_unlock(&fs_head_lock);
|
---|
[62da45a] | 279 | return 0;
|
---|
| 280 | }
|
---|
| 281 |
|
---|
[34ca870] | 282 | /** Tell VFS that the phone is not needed anymore.
|
---|
[62da45a] | 283 | *
|
---|
| 284 | * @param phone Phone to FS task.
|
---|
| 285 | */
|
---|
| 286 | void vfs_release_phone(int phone)
|
---|
| 287 | {
|
---|
[34ca870] | 288 | /* TODO: implement connection caching */
|
---|
| 289 | ipc_hangup(phone);
|
---|
[62da45a] | 290 | }
|
---|
| 291 |
|
---|
| 292 | /** Convert file system name to its handle.
|
---|
| 293 | *
|
---|
| 294 | * @param name File system name.
|
---|
[230260ac] | 295 | * @param lock If true, the function will lock and unlock the
|
---|
| 296 | * fs_head_lock.
|
---|
[62da45a] | 297 | *
|
---|
| 298 | * @return File system handle or zero if file system not found.
|
---|
| 299 | */
|
---|
[f2ec8c8] | 300 | fs_handle_t fs_name_to_handle(char *name, bool lock)
|
---|
[62da45a] | 301 | {
|
---|
| 302 | int handle = 0;
|
---|
| 303 |
|
---|
| 304 | if (lock)
|
---|
[230260ac] | 305 | fibril_mutex_lock(&fs_head_lock);
|
---|
[62da45a] | 306 | link_t *cur;
|
---|
| 307 | for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
|
---|
| 308 | fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link);
|
---|
[47a6708] | 309 | if (str_cmp(fs->vfs_info.name, name) == 0) {
|
---|
[62da45a] | 310 | handle = fs->fs_handle;
|
---|
| 311 | break;
|
---|
| 312 | }
|
---|
| 313 | }
|
---|
| 314 | if (lock)
|
---|
[230260ac] | 315 | fibril_mutex_unlock(&fs_head_lock);
|
---|
[62da45a] | 316 | return handle;
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | /**
|
---|
| 320 | * @}
|
---|
| 321 | */
|
---|