[bcf23cf] | 1 | /*
|
---|
| 2 | * Copyright (c) 2007 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 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
[ec01adf] | 34 | * @file vfs_mount.c
|
---|
[bcf23cf] | 35 | * @brief VFS_MOUNT method.
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | #include <ipc/ipc.h>
|
---|
| 39 | #include <async.h>
|
---|
| 40 | #include <errno.h>
|
---|
| 41 | #include <stdlib.h>
|
---|
| 42 | #include <string.h>
|
---|
| 43 | #include <bool.h>
|
---|
| 44 | #include <futex.h>
|
---|
| 45 | #include <libadt/list.h>
|
---|
| 46 | #include "vfs.h"
|
---|
| 47 |
|
---|
[2c2e0c6] | 48 | atomic_t rootfs_futex = FUTEX_INITIALIZER;
|
---|
[4d21cf8] | 49 | vfs_triplet_t rootfs = {
|
---|
| 50 | .fs_handle = 0,
|
---|
| 51 | .dev_handle = 0,
|
---|
| 52 | .index = 0,
|
---|
| 53 | };
|
---|
[7313e7a] | 54 |
|
---|
[4d21cf8] | 55 | static int lookup_root(int fs_handle, int dev_handle, vfs_triplet_t *root)
|
---|
[7313e7a] | 56 | {
|
---|
[4d21cf8] | 57 | vfs_pair_t altroot = {
|
---|
[7313e7a] | 58 | .fs_handle = fs_handle,
|
---|
| 59 | .dev_handle = dev_handle,
|
---|
| 60 | };
|
---|
| 61 |
|
---|
[2c2e0c6] | 62 | return vfs_lookup_internal("/", strlen("/"), root, &altroot);
|
---|
[7313e7a] | 63 | }
|
---|
[bcf23cf] | 64 |
|
---|
| 65 | void vfs_mount(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 66 | {
|
---|
[7313e7a] | 67 | int dev_handle;
|
---|
[44358c1] | 68 | vfs_node_t *mp_node = NULL;
|
---|
[7313e7a] | 69 |
|
---|
| 70 | /*
|
---|
| 71 | * We expect the library to do the device-name to device-handle
|
---|
| 72 | * translation for us, thus the device handle will arrive as ARG1
|
---|
| 73 | * in the request.
|
---|
| 74 | */
|
---|
| 75 | dev_handle = IPC_GET_ARG1(*request);
|
---|
| 76 |
|
---|
| 77 | /*
|
---|
| 78 | * For now, don't make use of ARG2 and ARG3, but they can be used to
|
---|
| 79 | * carry mount options in the future.
|
---|
| 80 | */
|
---|
| 81 |
|
---|
| 82 | /*
|
---|
| 83 | * Now, we expect the client to send us data with the name of the file
|
---|
| 84 | * system and the path of the mountpoint.
|
---|
| 85 | */
|
---|
| 86 | ipc_callid_t callid;
|
---|
| 87 | size_t size;
|
---|
[b74959bd] | 88 | if (!ipc_data_receive(&callid, NULL, &size)) {
|
---|
| 89 | ipc_answer_0(callid, EINVAL);
|
---|
| 90 | ipc_answer_0(rid, EINVAL);
|
---|
[7313e7a] | 91 | return;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | /*
|
---|
| 95 | * There is no sense in receiving data that can't hold a single
|
---|
| 96 | * character of path. We won't accept data that exceed certain limits
|
---|
| 97 | * either.
|
---|
| 98 | */
|
---|
| 99 | if ((size < FS_NAME_MAXLEN + 1) ||
|
---|
| 100 | (size > FS_NAME_MAXLEN + MAX_PATH_LEN)) {
|
---|
[b74959bd] | 101 | ipc_answer_0(callid, EINVAL);
|
---|
| 102 | ipc_answer_0(rid, EINVAL);
|
---|
[7313e7a] | 103 | return;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | /*
|
---|
| 107 | * Allocate buffer for the data being received.
|
---|
| 108 | */
|
---|
| 109 | uint8_t *buf;
|
---|
| 110 | buf = malloc(size);
|
---|
| 111 | if (!buf) {
|
---|
[b74959bd] | 112 | ipc_answer_0(callid, ENOMEM);
|
---|
| 113 | ipc_answer_0(rid, ENOMEM);
|
---|
[7313e7a] | 114 | return;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | /*
|
---|
| 118 | * Deliver the data.
|
---|
| 119 | */
|
---|
[b74959bd] | 120 | (void) ipc_data_deliver(callid, buf, size);
|
---|
[7313e7a] | 121 |
|
---|
| 122 | char fs_name[FS_NAME_MAXLEN + 1];
|
---|
| 123 | memcpy(fs_name, buf, FS_NAME_MAXLEN);
|
---|
| 124 | fs_name[FS_NAME_MAXLEN] = '\0';
|
---|
| 125 |
|
---|
| 126 | /*
|
---|
| 127 | * Check if we know a file system with the same name as is in fs_name.
|
---|
| 128 | * This will also give us its file system handle.
|
---|
| 129 | */
|
---|
| 130 | int fs_handle = fs_name_to_handle(fs_name, true);
|
---|
| 131 | if (!fs_handle) {
|
---|
| 132 | free(buf);
|
---|
[b74959bd] | 133 | ipc_answer_0(rid, ENOENT);
|
---|
[7313e7a] | 134 | return;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | /*
|
---|
| 138 | * Lookup the root node of the filesystem being mounted.
|
---|
[44358c1] | 139 | * In this case, we don't need to take the unlink_futex as the root node
|
---|
| 140 | * cannot be removed. However, we do take a reference to it so that
|
---|
| 141 | * we can track how many times it has been mounted.
|
---|
[7313e7a] | 142 | */
|
---|
| 143 | int rc;
|
---|
[4d21cf8] | 144 | vfs_triplet_t mounted_root;
|
---|
[7313e7a] | 145 | rc = lookup_root(fs_handle, dev_handle, &mounted_root);
|
---|
| 146 | if (rc != EOK) {
|
---|
| 147 | free(buf);
|
---|
[b74959bd] | 148 | ipc_answer_0(rid, rc);
|
---|
[7313e7a] | 149 | return;
|
---|
| 150 | }
|
---|
[44358c1] | 151 | vfs_node_t *mr_node = vfs_node_get(&mounted_root);
|
---|
| 152 | if (!mr_node) {
|
---|
| 153 | free(buf);
|
---|
[b74959bd] | 154 | ipc_answer_0(rid, ENOMEM);
|
---|
[44358c1] | 155 | return;
|
---|
| 156 | }
|
---|
[7313e7a] | 157 |
|
---|
| 158 | /*
|
---|
| 159 | * Finally, we need to resolve the path to the mountpoint.
|
---|
| 160 | */
|
---|
[4d21cf8] | 161 | vfs_triplet_t mp;
|
---|
[2c2e0c6] | 162 | futex_down(&rootfs_futex);
|
---|
[7313e7a] | 163 | if (rootfs.fs_handle) {
|
---|
| 164 | /*
|
---|
| 165 | * We already have the root FS.
|
---|
| 166 | */
|
---|
[44358c1] | 167 | futex_down(&unlink_futex);
|
---|
[7313e7a] | 168 | rc = vfs_lookup_internal((char *) (buf + FS_NAME_MAXLEN),
|
---|
| 169 | size - FS_NAME_MAXLEN, &mp, NULL);
|
---|
| 170 | if (rc != EOK) {
|
---|
| 171 | /*
|
---|
| 172 | * The lookup failed for some reason.
|
---|
| 173 | */
|
---|
[44358c1] | 174 | futex_up(&unlink_futex);
|
---|
[2c2e0c6] | 175 | futex_up(&rootfs_futex);
|
---|
[44358c1] | 176 | vfs_node_put(mr_node); /* failed -> drop reference */
|
---|
[7313e7a] | 177 | free(buf);
|
---|
[b74959bd] | 178 | ipc_answer_0(rid, rc);
|
---|
[7313e7a] | 179 | return;
|
---|
| 180 | }
|
---|
[44358c1] | 181 | mp_node = vfs_node_get(&mp);
|
---|
| 182 | if (!mp_node) {
|
---|
| 183 | futex_up(&unlink_futex);
|
---|
| 184 | futex_up(&rootfs_futex);
|
---|
| 185 | vfs_node_put(mr_node); /* failed -> drop reference */
|
---|
| 186 | free(buf);
|
---|
[b74959bd] | 187 | ipc_answer_0(rid, ENOMEM);
|
---|
[44358c1] | 188 | return;
|
---|
| 189 | }
|
---|
| 190 | /*
|
---|
| 191 | * Now we hold a reference to mp_node.
|
---|
| 192 | * It will be dropped upon the corresponding VFS_UNMOUNT.
|
---|
| 193 | * This prevents the mount point from being deleted.
|
---|
| 194 | */
|
---|
| 195 | futex_up(&unlink_futex);
|
---|
[7313e7a] | 196 | } else {
|
---|
| 197 | /*
|
---|
| 198 | * We still don't have the root file system mounted.
|
---|
| 199 | */
|
---|
[2c2e0c6] | 200 | if ((size - FS_NAME_MAXLEN == strlen("/")) &&
|
---|
[7313e7a] | 201 | (buf[FS_NAME_MAXLEN] == '/')) {
|
---|
| 202 | /*
|
---|
| 203 | * For this simple, but important case, we are done.
|
---|
| 204 | */
|
---|
| 205 | rootfs = mounted_root;
|
---|
[2c2e0c6] | 206 | futex_up(&rootfs_futex);
|
---|
[7313e7a] | 207 | free(buf);
|
---|
[b74959bd] | 208 | ipc_answer_0(rid, EOK);
|
---|
[7313e7a] | 209 | return;
|
---|
| 210 | } else {
|
---|
| 211 | /*
|
---|
| 212 | * We can't resolve this without the root filesystem
|
---|
| 213 | * being mounted first.
|
---|
| 214 | */
|
---|
[2c2e0c6] | 215 | futex_up(&rootfs_futex);
|
---|
[7313e7a] | 216 | free(buf);
|
---|
[44358c1] | 217 | vfs_node_put(mr_node); /* failed -> drop reference */
|
---|
[b74959bd] | 218 | ipc_answer_0(rid, ENOENT);
|
---|
[7313e7a] | 219 | return;
|
---|
| 220 | }
|
---|
| 221 | }
|
---|
[2c2e0c6] | 222 | futex_up(&rootfs_futex);
|
---|
| 223 |
|
---|
[af0ff9b2] | 224 | free(buf); /* The buffer is not needed anymore. */
|
---|
| 225 |
|
---|
[7313e7a] | 226 | /*
|
---|
| 227 | * At this point, we have all necessary pieces: file system and device
|
---|
| 228 | * handles, and we know the mount point VFS node and also the root node
|
---|
| 229 | * of the file system being mounted.
|
---|
| 230 | */
|
---|
[af0ff9b2] | 231 |
|
---|
| 232 | int phone = vfs_grab_phone(mp.fs_handle);
|
---|
| 233 | /* Later we can use ARG3 to pass mode/flags. */
|
---|
| 234 | aid_t req1 = async_send_3(phone, VFS_MOUNT, (ipcarg_t) mp.dev_handle,
|
---|
| 235 | (ipcarg_t) mp.index, 0, NULL);
|
---|
| 236 | /* The second call uses the same method. */
|
---|
| 237 | aid_t req2 = async_send_3(phone, VFS_MOUNT,
|
---|
| 238 | (ipcarg_t) mounted_root.fs_handle,
|
---|
| 239 | (ipcarg_t) mounted_root.dev_handle, (ipcarg_t) mounted_root.index,
|
---|
| 240 | NULL);
|
---|
[45ee9ed] | 241 | vfs_release_phone(phone);
|
---|
[af0ff9b2] | 242 |
|
---|
| 243 | ipcarg_t rc1;
|
---|
| 244 | ipcarg_t rc2;
|
---|
| 245 | async_wait_for(req1, &rc1);
|
---|
| 246 | async_wait_for(req2, &rc2);
|
---|
| 247 |
|
---|
[44358c1] | 248 | if ((rc1 != EOK) || (rc2 != EOK)) {
|
---|
| 249 | /* Mount failed, drop references to mr_node and mp_node. */
|
---|
| 250 | vfs_node_put(mr_node);
|
---|
| 251 | if (mp_node)
|
---|
| 252 | vfs_node_put(mp_node);
|
---|
| 253 | }
|
---|
| 254 |
|
---|
[18525c5] | 255 | if (rc2 == EOK)
|
---|
[b74959bd] | 256 | ipc_answer_0(rid, rc1);
|
---|
[18525c5] | 257 | else if (rc1 == EOK)
|
---|
[b74959bd] | 258 | ipc_answer_0(rid, rc2);
|
---|
[18525c5] | 259 | else
|
---|
[b74959bd] | 260 | ipc_answer_0(rid, rc1);
|
---|
[bcf23cf] | 261 | }
|
---|
| 262 |
|
---|
| 263 | /**
|
---|
| 264 | * @}
|
---|
| 265 | */
|
---|