[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 |
|
---|
[c9957b6] | 82 | ipc_callid_t callid;
|
---|
| 83 | size_t size;
|
---|
| 84 |
|
---|
[7313e7a] | 85 | /*
|
---|
| 86 | * Now, we expect the client to send us data with the name of the file
|
---|
[c9957b6] | 87 | * system.
|
---|
[7313e7a] | 88 | */
|
---|
[36d852c] | 89 | if (!ipc_data_write_receive(&callid, NULL, &size)) {
|
---|
[b74959bd] | 90 | ipc_answer_0(callid, EINVAL);
|
---|
| 91 | ipc_answer_0(rid, EINVAL);
|
---|
[7313e7a] | 92 | return;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | /*
|
---|
[c9957b6] | 96 | * Don't receive more than is necessary for storing a full file system
|
---|
| 97 | * name.
|
---|
| 98 | */
|
---|
| 99 | if (size < 1 || size > FS_NAME_MAXLEN) {
|
---|
| 100 | ipc_answer_0(callid, EINVAL);
|
---|
| 101 | ipc_answer_0(rid, EINVAL);
|
---|
| 102 | return;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | /*
|
---|
| 106 | * Deliver the file system name.
|
---|
| 107 | */
|
---|
| 108 | char fs_name[FS_NAME_MAXLEN + 1];
|
---|
[36d852c] | 109 | (void) ipc_data_write_deliver(callid, fs_name, size);
|
---|
[c9957b6] | 110 | fs_name[size] = '\0';
|
---|
| 111 |
|
---|
| 112 | /*
|
---|
| 113 | * Check if we know a file system with the same name as is in fs_name.
|
---|
| 114 | * This will also give us its file system handle.
|
---|
| 115 | */
|
---|
| 116 | int fs_handle = fs_name_to_handle(fs_name, true);
|
---|
| 117 | if (!fs_handle) {
|
---|
| 118 | ipc_answer_0(rid, ENOENT);
|
---|
| 119 | return;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | /*
|
---|
| 123 | * Now, we want the client to send us the mount point.
|
---|
[7313e7a] | 124 | */
|
---|
[36d852c] | 125 | if (!ipc_data_write_receive(&callid, NULL, &size)) {
|
---|
[b74959bd] | 126 | ipc_answer_0(callid, EINVAL);
|
---|
| 127 | ipc_answer_0(rid, EINVAL);
|
---|
[7313e7a] | 128 | return;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | /*
|
---|
[c9957b6] | 132 | * Check whether size is reasonable wrt. the mount point.
|
---|
| 133 | */
|
---|
| 134 | if (size < 1 || size > MAX_PATH_LEN) {
|
---|
| 135 | ipc_answer_0(callid, EINVAL);
|
---|
| 136 | ipc_answer_0(rid, EINVAL);
|
---|
| 137 | return;
|
---|
| 138 | }
|
---|
| 139 | /*
|
---|
| 140 | * Allocate buffer for the mount point data being received.
|
---|
[7313e7a] | 141 | */
|
---|
| 142 | uint8_t *buf;
|
---|
| 143 | buf = malloc(size);
|
---|
| 144 | if (!buf) {
|
---|
[b74959bd] | 145 | ipc_answer_0(callid, ENOMEM);
|
---|
| 146 | ipc_answer_0(rid, ENOMEM);
|
---|
[7313e7a] | 147 | return;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | /*
|
---|
[c9957b6] | 151 | * Deliver the mount point.
|
---|
[7313e7a] | 152 | */
|
---|
[36d852c] | 153 | (void) ipc_data_write_deliver(callid, buf, size);
|
---|
[7313e7a] | 154 |
|
---|
| 155 | /*
|
---|
| 156 | * Lookup the root node of the filesystem being mounted.
|
---|
[44358c1] | 157 | * In this case, we don't need to take the unlink_futex as the root node
|
---|
| 158 | * cannot be removed. However, we do take a reference to it so that
|
---|
| 159 | * we can track how many times it has been mounted.
|
---|
[7313e7a] | 160 | */
|
---|
| 161 | int rc;
|
---|
[4d21cf8] | 162 | vfs_triplet_t mounted_root;
|
---|
[7313e7a] | 163 | rc = lookup_root(fs_handle, dev_handle, &mounted_root);
|
---|
| 164 | if (rc != EOK) {
|
---|
| 165 | free(buf);
|
---|
[b74959bd] | 166 | ipc_answer_0(rid, rc);
|
---|
[7313e7a] | 167 | return;
|
---|
| 168 | }
|
---|
[44358c1] | 169 | vfs_node_t *mr_node = vfs_node_get(&mounted_root);
|
---|
| 170 | if (!mr_node) {
|
---|
| 171 | free(buf);
|
---|
[b74959bd] | 172 | ipc_answer_0(rid, ENOMEM);
|
---|
[44358c1] | 173 | return;
|
---|
| 174 | }
|
---|
[7313e7a] | 175 |
|
---|
| 176 | /*
|
---|
| 177 | * Finally, we need to resolve the path to the mountpoint.
|
---|
| 178 | */
|
---|
[4d21cf8] | 179 | vfs_triplet_t mp;
|
---|
[2c2e0c6] | 180 | futex_down(&rootfs_futex);
|
---|
[7313e7a] | 181 | if (rootfs.fs_handle) {
|
---|
| 182 | /*
|
---|
| 183 | * We already have the root FS.
|
---|
| 184 | */
|
---|
[44358c1] | 185 | futex_down(&unlink_futex);
|
---|
[c9957b6] | 186 | rc = vfs_lookup_internal(buf, size, &mp, NULL);
|
---|
[7313e7a] | 187 | if (rc != EOK) {
|
---|
| 188 | /*
|
---|
| 189 | * The lookup failed for some reason.
|
---|
| 190 | */
|
---|
[44358c1] | 191 | futex_up(&unlink_futex);
|
---|
[2c2e0c6] | 192 | futex_up(&rootfs_futex);
|
---|
[44358c1] | 193 | vfs_node_put(mr_node); /* failed -> drop reference */
|
---|
[7313e7a] | 194 | free(buf);
|
---|
[b74959bd] | 195 | ipc_answer_0(rid, rc);
|
---|
[7313e7a] | 196 | return;
|
---|
| 197 | }
|
---|
[44358c1] | 198 | mp_node = vfs_node_get(&mp);
|
---|
| 199 | if (!mp_node) {
|
---|
| 200 | futex_up(&unlink_futex);
|
---|
| 201 | futex_up(&rootfs_futex);
|
---|
| 202 | vfs_node_put(mr_node); /* failed -> drop reference */
|
---|
| 203 | free(buf);
|
---|
[b74959bd] | 204 | ipc_answer_0(rid, ENOMEM);
|
---|
[44358c1] | 205 | return;
|
---|
| 206 | }
|
---|
| 207 | /*
|
---|
| 208 | * Now we hold a reference to mp_node.
|
---|
| 209 | * It will be dropped upon the corresponding VFS_UNMOUNT.
|
---|
| 210 | * This prevents the mount point from being deleted.
|
---|
| 211 | */
|
---|
| 212 | futex_up(&unlink_futex);
|
---|
[7313e7a] | 213 | } else {
|
---|
| 214 | /*
|
---|
| 215 | * We still don't have the root file system mounted.
|
---|
| 216 | */
|
---|
[c9957b6] | 217 | if ((size == 1) && (buf[0] == '/')) {
|
---|
[7313e7a] | 218 | /*
|
---|
| 219 | * For this simple, but important case, we are done.
|
---|
| 220 | */
|
---|
| 221 | rootfs = mounted_root;
|
---|
[2c2e0c6] | 222 | futex_up(&rootfs_futex);
|
---|
[7313e7a] | 223 | free(buf);
|
---|
[b74959bd] | 224 | ipc_answer_0(rid, EOK);
|
---|
[7313e7a] | 225 | return;
|
---|
| 226 | } else {
|
---|
| 227 | /*
|
---|
| 228 | * We can't resolve this without the root filesystem
|
---|
| 229 | * being mounted first.
|
---|
| 230 | */
|
---|
[2c2e0c6] | 231 | futex_up(&rootfs_futex);
|
---|
[7313e7a] | 232 | free(buf);
|
---|
[44358c1] | 233 | vfs_node_put(mr_node); /* failed -> drop reference */
|
---|
[b74959bd] | 234 | ipc_answer_0(rid, ENOENT);
|
---|
[7313e7a] | 235 | return;
|
---|
| 236 | }
|
---|
| 237 | }
|
---|
[2c2e0c6] | 238 | futex_up(&rootfs_futex);
|
---|
| 239 |
|
---|
[af0ff9b2] | 240 | free(buf); /* The buffer is not needed anymore. */
|
---|
| 241 |
|
---|
[7313e7a] | 242 | /*
|
---|
| 243 | * At this point, we have all necessary pieces: file system and device
|
---|
| 244 | * handles, and we know the mount point VFS node and also the root node
|
---|
| 245 | * of the file system being mounted.
|
---|
| 246 | */
|
---|
[af0ff9b2] | 247 |
|
---|
| 248 | int phone = vfs_grab_phone(mp.fs_handle);
|
---|
| 249 | /* Later we can use ARG3 to pass mode/flags. */
|
---|
| 250 | aid_t req1 = async_send_3(phone, VFS_MOUNT, (ipcarg_t) mp.dev_handle,
|
---|
| 251 | (ipcarg_t) mp.index, 0, NULL);
|
---|
| 252 | /* The second call uses the same method. */
|
---|
| 253 | aid_t req2 = async_send_3(phone, VFS_MOUNT,
|
---|
| 254 | (ipcarg_t) mounted_root.fs_handle,
|
---|
| 255 | (ipcarg_t) mounted_root.dev_handle, (ipcarg_t) mounted_root.index,
|
---|
| 256 | NULL);
|
---|
[45ee9ed] | 257 | vfs_release_phone(phone);
|
---|
[af0ff9b2] | 258 |
|
---|
| 259 | ipcarg_t rc1;
|
---|
| 260 | ipcarg_t rc2;
|
---|
| 261 | async_wait_for(req1, &rc1);
|
---|
| 262 | async_wait_for(req2, &rc2);
|
---|
| 263 |
|
---|
[44358c1] | 264 | if ((rc1 != EOK) || (rc2 != EOK)) {
|
---|
| 265 | /* Mount failed, drop references to mr_node and mp_node. */
|
---|
| 266 | vfs_node_put(mr_node);
|
---|
| 267 | if (mp_node)
|
---|
| 268 | vfs_node_put(mp_node);
|
---|
| 269 | }
|
---|
| 270 |
|
---|
[18525c5] | 271 | if (rc2 == EOK)
|
---|
[b74959bd] | 272 | ipc_answer_0(rid, rc1);
|
---|
[18525c5] | 273 | else if (rc1 == EOK)
|
---|
[b74959bd] | 274 | ipc_answer_0(rid, rc2);
|
---|
[18525c5] | 275 | else
|
---|
[b74959bd] | 276 | ipc_answer_0(rid, rc1);
|
---|
[bcf23cf] | 277 | }
|
---|
| 278 |
|
---|
| 279 | /**
|
---|
| 280 | * @}
|
---|
| 281 | */
|
---|