Rev 3255 | Rev 3397 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 2689 | jermar | 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 | * @{ |
||
| 31 | */ |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @file vfs_ops.c |
||
| 35 | * @brief Operations that VFS offers to its clients. |
||
| 36 | */ |
||
| 37 | |||
| 2763 | jermar | 38 | #include "vfs.h" |
| 2689 | jermar | 39 | #include <ipc/ipc.h> |
| 40 | #include <async.h> |
||
| 41 | #include <errno.h> |
||
| 42 | #include <stdio.h> |
||
| 43 | #include <stdlib.h> |
||
| 44 | #include <string.h> |
||
| 45 | #include <bool.h> |
||
| 46 | #include <futex.h> |
||
| 47 | #include <rwlock.h> |
||
| 48 | #include <libadt/list.h> |
||
| 49 | #include <unistd.h> |
||
| 50 | #include <ctype.h> |
||
| 2708 | jermar | 51 | #include <fcntl.h> |
| 2689 | jermar | 52 | #include <assert.h> |
| 2763 | jermar | 53 | #include <vfs/canonify.h> |
| 2689 | jermar | 54 | |
| 2748 | jermar | 55 | /* Forward declarations of static functions. */ |
| 2770 | jermar | 56 | static int vfs_truncate_internal(fs_handle_t, dev_handle_t, fs_index_t, size_t); |
| 2748 | jermar | 57 | |
| 2689 | jermar | 58 | /** |
| 59 | * This rwlock prevents the race between a triplet-to-VFS-node resolution and a |
||
| 60 | * concurrent VFS operation which modifies the file system namespace. |
||
| 61 | */ |
||
| 62 | RWLOCK_INITIALIZE(namespace_rwlock); |
||
| 63 | |||
| 2766 | jermar | 64 | futex_t rootfs_futex = FUTEX_INITIALIZER; |
| 3109 | jermar | 65 | vfs_pair_t rootfs = { |
| 2689 | jermar | 66 | .fs_handle = 0, |
| 3109 | jermar | 67 | .dev_handle = 0 |
| 2689 | jermar | 68 | }; |
| 69 | |||
| 70 | void vfs_mount(ipc_callid_t rid, ipc_call_t *request) |
||
| 71 | { |
||
| 2770 | jermar | 72 | dev_handle_t dev_handle; |
| 2689 | jermar | 73 | vfs_node_t *mp_node = NULL; |
| 3255 | jermar | 74 | ipc_callid_t callid; |
| 75 | ipc_call_t data; |
||
| 2957 | jermar | 76 | int rc; |
| 2958 | jermar | 77 | int phone; |
| 3255 | jermar | 78 | size_t size; |
| 2689 | jermar | 79 | |
| 80 | /* |
||
| 81 | * We expect the library to do the device-name to device-handle |
||
| 82 | * translation for us, thus the device handle will arrive as ARG1 |
||
| 83 | * in the request. |
||
| 84 | */ |
||
| 2770 | jermar | 85 | dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
| 2689 | jermar | 86 | |
| 87 | /* |
||
| 88 | * For now, don't make use of ARG2 and ARG3, but they can be used to |
||
| 89 | * carry mount options in the future. |
||
| 90 | */ |
||
| 91 | |||
| 92 | /* |
||
| 93 | * Now, we expect the client to send us data with the name of the file |
||
| 94 | * system. |
||
| 95 | */ |
||
| 96 | if (!ipc_data_write_receive(&callid, &size)) { |
||
| 97 | ipc_answer_0(callid, EINVAL); |
||
| 98 | ipc_answer_0(rid, EINVAL); |
||
| 99 | return; |
||
| 100 | } |
||
| 101 | |||
| 102 | /* |
||
| 103 | * Don't receive more than is necessary for storing a full file system |
||
| 104 | * name. |
||
| 105 | */ |
||
| 106 | if (size < 1 || size > FS_NAME_MAXLEN) { |
||
| 107 | ipc_answer_0(callid, EINVAL); |
||
| 108 | ipc_answer_0(rid, EINVAL); |
||
| 109 | return; |
||
| 110 | } |
||
| 111 | |||
| 2707 | jermar | 112 | /* Deliver the file system name. */ |
| 2689 | jermar | 113 | char fs_name[FS_NAME_MAXLEN + 1]; |
| 114 | (void) ipc_data_write_finalize(callid, fs_name, size); |
||
| 115 | fs_name[size] = '\0'; |
||
| 116 | |||
| 117 | /* |
||
| 3255 | jermar | 118 | * Wait for IPC_M_PING so that we can return an error if we don't know |
| 119 | * fs_name. |
||
| 120 | */ |
||
| 121 | callid = async_get_call(&data); |
||
| 122 | if (IPC_GET_METHOD(data) != IPC_M_PING) { |
||
| 123 | ipc_answer_0(callid, ENOTSUP); |
||
| 124 | ipc_answer_0(rid, ENOTSUP); |
||
| 125 | return; |
||
| 126 | } |
||
| 127 | |||
| 128 | /* |
||
| 2689 | jermar | 129 | * Check if we know a file system with the same name as is in fs_name. |
| 130 | * This will also give us its file system handle. |
||
| 131 | */ |
||
| 2770 | jermar | 132 | fs_handle_t fs_handle = fs_name_to_handle(fs_name, true); |
| 2689 | jermar | 133 | if (!fs_handle) { |
| 3255 | jermar | 134 | ipc_answer_0(callid, ENOENT); |
| 2689 | jermar | 135 | ipc_answer_0(rid, ENOENT); |
| 136 | return; |
||
| 137 | } |
||
| 138 | |||
| 3255 | jermar | 139 | /* Acknowledge that we know fs_name. */ |
| 140 | ipc_answer_0(callid, EOK); |
||
| 141 | |||
| 2707 | jermar | 142 | /* Now, we want the client to send us the mount point. */ |
| 2689 | jermar | 143 | if (!ipc_data_write_receive(&callid, &size)) { |
| 144 | ipc_answer_0(callid, EINVAL); |
||
| 145 | ipc_answer_0(rid, EINVAL); |
||
| 146 | return; |
||
| 147 | } |
||
| 148 | |||
| 2707 | jermar | 149 | /* Check whether size is reasonable wrt. the mount point. */ |
| 2689 | jermar | 150 | if (size < 1 || size > MAX_PATH_LEN) { |
| 151 | ipc_answer_0(callid, EINVAL); |
||
| 152 | ipc_answer_0(rid, EINVAL); |
||
| 153 | return; |
||
| 154 | } |
||
| 2707 | jermar | 155 | /* Allocate buffer for the mount point data being received. */ |
| 2689 | jermar | 156 | uint8_t *buf; |
| 2752 | jermar | 157 | buf = malloc(size + 1); |
| 2689 | jermar | 158 | if (!buf) { |
| 159 | ipc_answer_0(callid, ENOMEM); |
||
| 160 | ipc_answer_0(rid, ENOMEM); |
||
| 161 | return; |
||
| 162 | } |
||
| 163 | |||
| 2707 | jermar | 164 | /* Deliver the mount point. */ |
| 2689 | jermar | 165 | (void) ipc_data_write_finalize(callid, buf, size); |
| 2752 | jermar | 166 | buf[size] = '\0'; |
| 2689 | jermar | 167 | |
| 3109 | jermar | 168 | /* Resolve the path to the mountpoint. */ |
| 2691 | jermar | 169 | vfs_lookup_res_t mp_res; |
| 2689 | jermar | 170 | futex_down(&rootfs_futex); |
| 171 | if (rootfs.fs_handle) { |
||
| 2707 | jermar | 172 | /* We already have the root FS. */ |
| 2689 | jermar | 173 | rwlock_write_lock(&namespace_rwlock); |
| 2788 | jermar | 174 | if ((size == 1) && (buf[0] == '/')) { |
| 175 | /* Trying to mount root FS over root FS */ |
||
| 176 | rwlock_write_unlock(&namespace_rwlock); |
||
| 177 | futex_up(&rootfs_futex); |
||
| 178 | free(buf); |
||
| 179 | ipc_answer_0(rid, EBUSY); |
||
| 180 | return; |
||
| 181 | } |
||
| 2752 | jermar | 182 | rc = vfs_lookup_internal(buf, L_DIRECTORY, &mp_res, NULL); |
| 2689 | jermar | 183 | if (rc != EOK) { |
| 2707 | jermar | 184 | /* The lookup failed for some reason. */ |
| 2689 | jermar | 185 | rwlock_write_unlock(&namespace_rwlock); |
| 186 | futex_up(&rootfs_futex); |
||
| 187 | free(buf); |
||
| 188 | ipc_answer_0(rid, rc); |
||
| 189 | return; |
||
| 190 | } |
||
| 2691 | jermar | 191 | mp_node = vfs_node_get(&mp_res); |
| 2689 | jermar | 192 | if (!mp_node) { |
| 193 | rwlock_write_unlock(&namespace_rwlock); |
||
| 194 | futex_up(&rootfs_futex); |
||
| 195 | free(buf); |
||
| 196 | ipc_answer_0(rid, ENOMEM); |
||
| 197 | return; |
||
| 198 | } |
||
| 199 | /* |
||
| 200 | * Now we hold a reference to mp_node. |
||
| 201 | * It will be dropped upon the corresponding VFS_UNMOUNT. |
||
| 202 | * This prevents the mount point from being deleted. |
||
| 203 | */ |
||
| 204 | rwlock_write_unlock(&namespace_rwlock); |
||
| 205 | } else { |
||
| 2707 | jermar | 206 | /* We still don't have the root file system mounted. */ |
| 2689 | jermar | 207 | if ((size == 1) && (buf[0] == '/')) { |
| 2958 | jermar | 208 | /* |
| 209 | * For this simple, but important case, |
||
| 210 | * we are almost done. |
||
| 211 | */ |
||
| 212 | free(buf); |
||
| 213 | |||
| 3109 | jermar | 214 | /* Tell the mountee that it is being mounted. */ |
| 215 | phone = vfs_grab_phone(fs_handle); |
||
| 216 | rc = async_req_1_0(phone, VFS_MOUNTED, |
||
| 217 | (ipcarg_t) dev_handle); |
||
| 2958 | jermar | 218 | vfs_release_phone(phone); |
| 219 | |||
| 3109 | jermar | 220 | if (rc == EOK) { |
| 221 | rootfs.fs_handle = fs_handle; |
||
| 222 | rootfs.dev_handle = dev_handle; |
||
| 223 | } |
||
| 2958 | jermar | 224 | |
| 2689 | jermar | 225 | futex_up(&rootfs_futex); |
| 2958 | jermar | 226 | ipc_answer_0(rid, rc); |
| 2689 | jermar | 227 | return; |
| 228 | } else { |
||
| 229 | /* |
||
| 230 | * We can't resolve this without the root filesystem |
||
| 231 | * being mounted first. |
||
| 232 | */ |
||
| 233 | futex_up(&rootfs_futex); |
||
| 234 | free(buf); |
||
| 235 | ipc_answer_0(rid, ENOENT); |
||
| 236 | return; |
||
| 237 | } |
||
| 238 | } |
||
| 239 | futex_up(&rootfs_futex); |
||
| 240 | |||
| 241 | free(buf); /* The buffer is not needed anymore. */ |
||
| 242 | |||
| 243 | /* |
||
| 244 | * At this point, we have all necessary pieces: file system and device |
||
| 3109 | jermar | 245 | * handles, and we know the mount point VFS node. |
| 2689 | jermar | 246 | */ |
| 247 | |||
| 2958 | jermar | 248 | phone = vfs_grab_phone(mp_res.triplet.fs_handle); |
| 3109 | jermar | 249 | rc = async_req_4_0(phone, VFS_MOUNT, |
| 2691 | jermar | 250 | (ipcarg_t) mp_res.triplet.dev_handle, |
| 2957 | jermar | 251 | (ipcarg_t) mp_res.triplet.index, |
| 3109 | jermar | 252 | (ipcarg_t) fs_handle, |
| 253 | (ipcarg_t) dev_handle); |
||
| 2689 | jermar | 254 | vfs_release_phone(phone); |
| 255 | |||
| 2957 | jermar | 256 | if (rc != EOK) { |
| 3109 | jermar | 257 | /* Mount failed, drop reference to mp_node. */ |
| 2689 | jermar | 258 | if (mp_node) |
| 259 | vfs_node_put(mp_node); |
||
| 260 | } |
||
| 261 | |||
| 2957 | jermar | 262 | ipc_answer_0(rid, rc); |
| 2689 | jermar | 263 | } |
| 264 | |||
| 265 | void vfs_open(ipc_callid_t rid, ipc_call_t *request) |
||
| 266 | { |
||
| 267 | if (!vfs_files_init()) { |
||
| 268 | ipc_answer_0(rid, ENOMEM); |
||
| 269 | return; |
||
| 270 | } |
||
| 271 | |||
| 272 | /* |
||
| 2700 | jermar | 273 | * The POSIX interface is open(path, oflag, mode). |
| 274 | * We can receive oflags and mode along with the VFS_OPEN call; the path |
||
| 2689 | jermar | 275 | * will need to arrive in another call. |
| 2700 | jermar | 276 | * |
| 277 | * We also receive one private, non-POSIX set of flags called lflag |
||
| 278 | * used to pass information to vfs_lookup_internal(). |
||
| 2689 | jermar | 279 | */ |
| 2700 | jermar | 280 | int lflag = IPC_GET_ARG1(*request); |
| 281 | int oflag = IPC_GET_ARG2(*request); |
||
| 282 | int mode = IPC_GET_ARG3(*request); |
||
| 2689 | jermar | 283 | size_t len; |
| 284 | |||
| 2708 | jermar | 285 | if (oflag & O_CREAT) |
| 286 | lflag |= L_CREATE; |
||
| 287 | if (oflag & O_EXCL) |
||
| 288 | lflag |= L_EXCLUSIVE; |
||
| 289 | |||
| 2689 | jermar | 290 | ipc_callid_t callid; |
| 291 | |||
| 292 | if (!ipc_data_write_receive(&callid, &len)) { |
||
| 293 | ipc_answer_0(callid, EINVAL); |
||
| 294 | ipc_answer_0(rid, EINVAL); |
||
| 295 | return; |
||
| 296 | } |
||
| 2752 | jermar | 297 | char *path = malloc(len + 1); |
| 2689 | jermar | 298 | if (!path) { |
| 299 | ipc_answer_0(callid, ENOMEM); |
||
| 300 | ipc_answer_0(rid, ENOMEM); |
||
| 301 | return; |
||
| 302 | } |
||
| 303 | int rc; |
||
| 304 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
||
| 305 | ipc_answer_0(rid, rc); |
||
| 306 | free(path); |
||
| 307 | return; |
||
| 308 | } |
||
| 2752 | jermar | 309 | path[len] = '\0'; |
| 2689 | jermar | 310 | |
| 311 | /* |
||
| 312 | * Avoid the race condition in which the file can be deleted before we |
||
| 313 | * find/create-and-lock the VFS node corresponding to the looked-up |
||
| 314 | * triplet. |
||
| 315 | */ |
||
| 2708 | jermar | 316 | if (lflag & L_CREATE) |
| 317 | rwlock_write_lock(&namespace_rwlock); |
||
| 318 | else |
||
| 319 | rwlock_read_lock(&namespace_rwlock); |
||
| 2689 | jermar | 320 | |
| 2707 | jermar | 321 | /* The path is now populated and we can call vfs_lookup_internal(). */ |
| 2691 | jermar | 322 | vfs_lookup_res_t lr; |
| 2752 | jermar | 323 | rc = vfs_lookup_internal(path, lflag, &lr, NULL); |
| 2689 | jermar | 324 | if (rc) { |
| 2708 | jermar | 325 | if (lflag & L_CREATE) |
| 326 | rwlock_write_unlock(&namespace_rwlock); |
||
| 327 | else |
||
| 328 | rwlock_read_unlock(&namespace_rwlock); |
||
| 2689 | jermar | 329 | ipc_answer_0(rid, rc); |
| 330 | free(path); |
||
| 331 | return; |
||
| 332 | } |
||
| 333 | |||
| 2748 | jermar | 334 | /* Path is no longer needed. */ |
| 2689 | jermar | 335 | free(path); |
| 336 | |||
| 2691 | jermar | 337 | vfs_node_t *node = vfs_node_get(&lr); |
| 2708 | jermar | 338 | if (lflag & L_CREATE) |
| 339 | rwlock_write_unlock(&namespace_rwlock); |
||
| 340 | else |
||
| 341 | rwlock_read_unlock(&namespace_rwlock); |
||
| 2689 | jermar | 342 | |
| 2748 | jermar | 343 | /* Truncate the file if requested and if necessary. */ |
| 344 | if (oflag & O_TRUNC) { |
||
| 2749 | jermar | 345 | rwlock_write_lock(&node->contents_rwlock); |
| 2748 | jermar | 346 | if (node->size) { |
| 347 | rc = vfs_truncate_internal(node->fs_handle, |
||
| 348 | node->dev_handle, node->index, 0); |
||
| 349 | if (rc) { |
||
| 2749 | jermar | 350 | rwlock_write_unlock(&node->contents_rwlock); |
| 2748 | jermar | 351 | vfs_node_put(node); |
| 352 | ipc_answer_0(rid, rc); |
||
| 353 | return; |
||
| 354 | } |
||
| 355 | node->size = 0; |
||
| 356 | } |
||
| 2749 | jermar | 357 | rwlock_write_unlock(&node->contents_rwlock); |
| 2748 | jermar | 358 | } |
| 359 | |||
| 2689 | jermar | 360 | /* |
| 361 | * Get ourselves a file descriptor and the corresponding vfs_file_t |
||
| 362 | * structure. |
||
| 363 | */ |
||
| 364 | int fd = vfs_fd_alloc(); |
||
| 365 | if (fd < 0) { |
||
| 366 | vfs_node_put(node); |
||
| 367 | ipc_answer_0(rid, fd); |
||
| 368 | return; |
||
| 369 | } |
||
| 370 | vfs_file_t *file = vfs_file_get(fd); |
||
| 371 | file->node = node; |
||
| 2710 | jermar | 372 | if (oflag & O_APPEND) |
| 2709 | jermar | 373 | file->append = true; |
| 2689 | jermar | 374 | |
| 375 | /* |
||
| 376 | * The following increase in reference count is for the fact that the |
||
| 377 | * file is being opened and that a file structure is pointing to it. |
||
| 378 | * It is necessary so that the file will not disappear when |
||
| 379 | * vfs_node_put() is called. The reference will be dropped by the |
||
| 380 | * respective VFS_CLOSE. |
||
| 381 | */ |
||
| 382 | vfs_node_addref(node); |
||
| 383 | vfs_node_put(node); |
||
| 384 | |||
| 2707 | jermar | 385 | /* Success! Return the new file descriptor to the client. */ |
| 2689 | jermar | 386 | ipc_answer_1(rid, EOK, fd); |
| 387 | } |
||
| 388 | |||
| 2734 | jermar | 389 | void vfs_close(ipc_callid_t rid, ipc_call_t *request) |
| 390 | { |
||
| 391 | int fd = IPC_GET_ARG1(*request); |
||
| 3215 | jermar | 392 | int rc = vfs_fd_free(fd); |
| 393 | ipc_answer_0(rid, rc); |
||
| 2734 | jermar | 394 | } |
| 395 | |||
| 2689 | jermar | 396 | static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read) |
| 397 | { |
||
| 398 | |||
| 399 | /* |
||
| 400 | * The following code strongly depends on the fact that the files data |
||
| 401 | * structure can be only accessed by a single fibril and all file |
||
| 402 | * operations are serialized (i.e. the reads and writes cannot |
||
| 403 | * interleave and a file cannot be closed while it is being read). |
||
| 404 | * |
||
| 405 | * Additional synchronization needs to be added once the table of |
||
| 406 | * open files supports parallel access! |
||
| 407 | */ |
||
| 408 | |||
| 409 | int fd = IPC_GET_ARG1(*request); |
||
| 3079 | decky | 410 | |
| 2707 | jermar | 411 | /* Lookup the file structure corresponding to the file descriptor. */ |
| 2689 | jermar | 412 | vfs_file_t *file = vfs_file_get(fd); |
| 413 | if (!file) { |
||
| 414 | ipc_answer_0(rid, ENOENT); |
||
| 415 | return; |
||
| 416 | } |
||
| 3079 | decky | 417 | |
| 2689 | jermar | 418 | /* |
| 419 | * Now we need to receive a call with client's |
||
| 420 | * IPC_M_DATA_READ/IPC_M_DATA_WRITE request. |
||
| 421 | */ |
||
| 422 | ipc_callid_t callid; |
||
| 423 | int res; |
||
| 424 | if (read) |
||
| 425 | res = ipc_data_read_receive(&callid, NULL); |
||
| 426 | else |
||
| 427 | res = ipc_data_write_receive(&callid, NULL); |
||
| 428 | if (!res) { |
||
| 429 | ipc_answer_0(callid, EINVAL); |
||
| 430 | ipc_answer_0(rid, EINVAL); |
||
| 431 | return; |
||
| 432 | } |
||
| 3079 | decky | 433 | |
| 2689 | jermar | 434 | /* |
| 435 | * Lock the open file structure so that no other thread can manipulate |
||
| 436 | * the same open file at a time. |
||
| 437 | */ |
||
| 438 | futex_down(&file->lock); |
||
| 3079 | decky | 439 | |
| 2689 | jermar | 440 | /* |
| 441 | * Lock the file's node so that no other client can read/write to it at |
||
| 442 | * the same time. |
||
| 443 | */ |
||
| 444 | if (read) |
||
| 445 | rwlock_read_lock(&file->node->contents_rwlock); |
||
| 446 | else |
||
| 447 | rwlock_write_lock(&file->node->contents_rwlock); |
||
| 3079 | decky | 448 | |
| 2689 | jermar | 449 | int fs_phone = vfs_grab_phone(file->node->fs_handle); |
| 450 | |||
| 2707 | jermar | 451 | /* Make a VFS_READ/VFS_WRITE request at the destination FS server. */ |
| 2689 | jermar | 452 | aid_t msg; |
| 453 | ipc_call_t answer; |
||
| 2709 | jermar | 454 | if (!read && file->append) |
| 455 | file->pos = file->node->size; |
||
| 2689 | jermar | 456 | msg = async_send_3(fs_phone, IPC_GET_METHOD(*request), |
| 457 | file->node->dev_handle, file->node->index, file->pos, &answer); |
||
| 458 | |||
| 459 | /* |
||
| 460 | * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the |
||
| 461 | * destination FS server. The call will be routed as if sent by |
||
| 462 | * ourselves. Note that call arguments are immutable in this case so we |
||
| 463 | * don't have to bother. |
||
| 464 | */ |
||
| 465 | ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
||
| 3079 | decky | 466 | |
| 2689 | jermar | 467 | vfs_release_phone(fs_phone); |
| 3079 | decky | 468 | |
| 2707 | jermar | 469 | /* Wait for reply from the FS server. */ |
| 2689 | jermar | 470 | ipcarg_t rc; |
| 471 | async_wait_for(msg, &rc); |
||
| 472 | size_t bytes = IPC_GET_ARG1(answer); |
||
| 3079 | decky | 473 | |
| 2707 | jermar | 474 | /* Unlock the VFS node. */ |
| 2689 | jermar | 475 | if (read) |
| 476 | rwlock_read_unlock(&file->node->contents_rwlock); |
||
| 477 | else { |
||
| 478 | /* Update the cached version of node's size. */ |
||
| 2710 | jermar | 479 | if (rc == EOK) |
| 480 | file->node->size = IPC_GET_ARG2(answer); |
||
| 2689 | jermar | 481 | rwlock_write_unlock(&file->node->contents_rwlock); |
| 482 | } |
||
| 3079 | decky | 483 | |
| 2707 | jermar | 484 | /* Update the position pointer and unlock the open file. */ |
| 2710 | jermar | 485 | if (rc == EOK) |
| 486 | file->pos += bytes; |
||
| 2689 | jermar | 487 | futex_up(&file->lock); |
| 3079 | decky | 488 | |
| 2689 | jermar | 489 | /* |
| 490 | * FS server's reply is the final result of the whole operation we |
||
| 491 | * return to the client. |
||
| 492 | */ |
||
| 493 | ipc_answer_1(rid, rc, bytes); |
||
| 494 | } |
||
| 495 | |||
| 496 | void vfs_read(ipc_callid_t rid, ipc_call_t *request) |
||
| 497 | { |
||
| 498 | vfs_rdwr(rid, request, true); |
||
| 499 | } |
||
| 500 | |||
| 501 | void vfs_write(ipc_callid_t rid, ipc_call_t *request) |
||
| 502 | { |
||
| 503 | vfs_rdwr(rid, request, false); |
||
| 504 | } |
||
| 505 | |||
| 506 | void vfs_seek(ipc_callid_t rid, ipc_call_t *request) |
||
| 507 | { |
||
| 508 | int fd = (int) IPC_GET_ARG1(*request); |
||
| 509 | off_t off = (off_t) IPC_GET_ARG2(*request); |
||
| 510 | int whence = (int) IPC_GET_ARG3(*request); |
||
| 511 | |||
| 512 | |||
| 2707 | jermar | 513 | /* Lookup the file structure corresponding to the file descriptor. */ |
| 2689 | jermar | 514 | vfs_file_t *file = vfs_file_get(fd); |
| 515 | if (!file) { |
||
| 516 | ipc_answer_0(rid, ENOENT); |
||
| 517 | return; |
||
| 518 | } |
||
| 519 | |||
| 520 | off_t newpos; |
||
| 521 | futex_down(&file->lock); |
||
| 522 | if (whence == SEEK_SET) { |
||
| 523 | file->pos = off; |
||
| 524 | futex_up(&file->lock); |
||
| 525 | ipc_answer_1(rid, EOK, off); |
||
| 526 | return; |
||
| 527 | } |
||
| 528 | if (whence == SEEK_CUR) { |
||
| 529 | if (file->pos + off < file->pos) { |
||
| 530 | futex_up(&file->lock); |
||
| 531 | ipc_answer_0(rid, EOVERFLOW); |
||
| 532 | return; |
||
| 533 | } |
||
| 534 | file->pos += off; |
||
| 535 | newpos = file->pos; |
||
| 536 | futex_up(&file->lock); |
||
| 537 | ipc_answer_1(rid, EOK, newpos); |
||
| 538 | return; |
||
| 539 | } |
||
| 540 | if (whence == SEEK_END) { |
||
| 541 | rwlock_read_lock(&file->node->contents_rwlock); |
||
| 542 | size_t size = file->node->size; |
||
| 543 | rwlock_read_unlock(&file->node->contents_rwlock); |
||
| 544 | if (size + off < size) { |
||
| 545 | futex_up(&file->lock); |
||
| 546 | ipc_answer_0(rid, EOVERFLOW); |
||
| 547 | return; |
||
| 548 | } |
||
| 549 | newpos = size + off; |
||
| 550 | futex_up(&file->lock); |
||
| 551 | ipc_answer_1(rid, EOK, newpos); |
||
| 552 | return; |
||
| 553 | } |
||
| 554 | futex_up(&file->lock); |
||
| 555 | ipc_answer_0(rid, EINVAL); |
||
| 556 | } |
||
| 557 | |||
| 2770 | jermar | 558 | int |
| 559 | vfs_truncate_internal(fs_handle_t fs_handle, dev_handle_t dev_handle, |
||
| 560 | fs_index_t index, size_t size) |
||
| 2748 | jermar | 561 | { |
| 562 | ipcarg_t rc; |
||
| 563 | int fs_phone; |
||
| 564 | |||
| 565 | fs_phone = vfs_grab_phone(fs_handle); |
||
| 566 | rc = async_req_3_0(fs_phone, VFS_TRUNCATE, (ipcarg_t)dev_handle, |
||
| 567 | (ipcarg_t)index, (ipcarg_t)size); |
||
| 568 | vfs_release_phone(fs_phone); |
||
| 569 | return (int)rc; |
||
| 570 | } |
||
| 571 | |||
| 2693 | jermar | 572 | void vfs_truncate(ipc_callid_t rid, ipc_call_t *request) |
| 573 | { |
||
| 574 | int fd = IPC_GET_ARG1(*request); |
||
| 575 | size_t size = IPC_GET_ARG2(*request); |
||
| 2748 | jermar | 576 | int rc; |
| 2693 | jermar | 577 | |
| 578 | vfs_file_t *file = vfs_file_get(fd); |
||
| 579 | if (!file) { |
||
| 580 | ipc_answer_0(rid, ENOENT); |
||
| 581 | return; |
||
| 582 | } |
||
| 583 | futex_down(&file->lock); |
||
| 584 | |||
| 585 | rwlock_write_lock(&file->node->contents_rwlock); |
||
| 2748 | jermar | 586 | rc = vfs_truncate_internal(file->node->fs_handle, |
| 587 | file->node->dev_handle, file->node->index, size); |
||
| 2693 | jermar | 588 | if (rc == EOK) |
| 589 | file->node->size = size; |
||
| 590 | rwlock_write_unlock(&file->node->contents_rwlock); |
||
| 591 | |||
| 592 | futex_up(&file->lock); |
||
| 2748 | jermar | 593 | ipc_answer_0(rid, (ipcarg_t)rc); |
| 2693 | jermar | 594 | } |
| 595 | |||
| 2707 | jermar | 596 | void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request) |
| 597 | { |
||
| 598 | int mode = IPC_GET_ARG1(*request); |
||
| 2735 | jermar | 599 | |
| 2707 | jermar | 600 | size_t len; |
| 601 | ipc_callid_t callid; |
||
| 602 | |||
| 603 | if (!ipc_data_write_receive(&callid, &len)) { |
||
| 604 | ipc_answer_0(callid, EINVAL); |
||
| 605 | ipc_answer_0(rid, EINVAL); |
||
| 606 | return; |
||
| 607 | } |
||
| 2752 | jermar | 608 | char *path = malloc(len + 1); |
| 2707 | jermar | 609 | if (!path) { |
| 610 | ipc_answer_0(callid, ENOMEM); |
||
| 611 | ipc_answer_0(rid, ENOMEM); |
||
| 612 | return; |
||
| 613 | } |
||
| 614 | int rc; |
||
| 615 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
||
| 616 | ipc_answer_0(rid, rc); |
||
| 617 | free(path); |
||
| 618 | return; |
||
| 619 | } |
||
| 2752 | jermar | 620 | path[len] = '\0'; |
| 2707 | jermar | 621 | |
| 622 | rwlock_write_lock(&namespace_rwlock); |
||
| 623 | int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE; |
||
| 2752 | jermar | 624 | rc = vfs_lookup_internal(path, lflag, NULL, NULL); |
| 2707 | jermar | 625 | rwlock_write_unlock(&namespace_rwlock); |
| 626 | free(path); |
||
| 627 | ipc_answer_0(rid, rc); |
||
| 628 | } |
||
| 629 | |||
| 2735 | jermar | 630 | void vfs_unlink(ipc_callid_t rid, ipc_call_t *request) |
| 631 | { |
||
| 632 | int lflag = IPC_GET_ARG1(*request); |
||
| 633 | |||
| 634 | size_t len; |
||
| 635 | ipc_callid_t callid; |
||
| 636 | |||
| 637 | if (!ipc_data_write_receive(&callid, &len)) { |
||
| 638 | ipc_answer_0(callid, EINVAL); |
||
| 639 | ipc_answer_0(rid, EINVAL); |
||
| 640 | return; |
||
| 641 | } |
||
| 2752 | jermar | 642 | char *path = malloc(len + 1); |
| 2735 | jermar | 643 | if (!path) { |
| 644 | ipc_answer_0(callid, ENOMEM); |
||
| 645 | ipc_answer_0(rid, ENOMEM); |
||
| 646 | return; |
||
| 647 | } |
||
| 648 | int rc; |
||
| 649 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
||
| 650 | ipc_answer_0(rid, rc); |
||
| 651 | free(path); |
||
| 652 | return; |
||
| 653 | } |
||
| 2752 | jermar | 654 | path[len] = '\0'; |
| 2735 | jermar | 655 | |
| 656 | rwlock_write_lock(&namespace_rwlock); |
||
| 657 | lflag &= L_DIRECTORY; /* sanitize lflag */ |
||
| 658 | vfs_lookup_res_t lr; |
||
| 2763 | jermar | 659 | rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL); |
| 2735 | jermar | 660 | free(path); |
| 661 | if (rc != EOK) { |
||
| 662 | rwlock_write_unlock(&namespace_rwlock); |
||
| 663 | ipc_answer_0(rid, rc); |
||
| 664 | return; |
||
| 665 | } |
||
| 666 | |||
| 667 | /* |
||
| 668 | * The name has already been unlinked by vfs_lookup_internal(). |
||
| 669 | * We have to get and put the VFS node to ensure that it is |
||
| 2742 | jermar | 670 | * VFS_DESTROY'ed after the last reference to it is dropped. |
| 2735 | jermar | 671 | */ |
| 672 | vfs_node_t *node = vfs_node_get(&lr); |
||
| 2766 | jermar | 673 | futex_down(&nodes_futex); |
| 2735 | jermar | 674 | node->lnkcnt--; |
| 2766 | jermar | 675 | futex_up(&nodes_futex); |
| 2735 | jermar | 676 | rwlock_write_unlock(&namespace_rwlock); |
| 677 | vfs_node_put(node); |
||
| 678 | ipc_answer_0(rid, EOK); |
||
| 679 | } |
||
| 680 | |||
| 2763 | jermar | 681 | void vfs_rename(ipc_callid_t rid, ipc_call_t *request) |
| 682 | { |
||
| 683 | size_t len; |
||
| 684 | ipc_callid_t callid; |
||
| 685 | int rc; |
||
| 686 | |||
| 687 | /* Retrieve the old path. */ |
||
| 688 | if (!ipc_data_write_receive(&callid, &len)) { |
||
| 689 | ipc_answer_0(callid, EINVAL); |
||
| 690 | ipc_answer_0(rid, EINVAL); |
||
| 691 | return; |
||
| 692 | } |
||
| 693 | char *old = malloc(len + 1); |
||
| 694 | if (!old) { |
||
| 695 | ipc_answer_0(callid, ENOMEM); |
||
| 696 | ipc_answer_0(rid, ENOMEM); |
||
| 697 | return; |
||
| 698 | } |
||
| 699 | if ((rc = ipc_data_write_finalize(callid, old, len))) { |
||
| 700 | ipc_answer_0(rid, rc); |
||
| 701 | free(old); |
||
| 702 | return; |
||
| 703 | } |
||
| 704 | old[len] = '\0'; |
||
| 705 | |||
| 706 | /* Retrieve the new path. */ |
||
| 707 | if (!ipc_data_write_receive(&callid, &len)) { |
||
| 708 | ipc_answer_0(callid, EINVAL); |
||
| 709 | ipc_answer_0(rid, EINVAL); |
||
| 710 | free(old); |
||
| 711 | return; |
||
| 712 | } |
||
| 713 | char *new = malloc(len + 1); |
||
| 714 | if (!new) { |
||
| 715 | ipc_answer_0(callid, ENOMEM); |
||
| 716 | ipc_answer_0(rid, ENOMEM); |
||
| 717 | free(old); |
||
| 718 | return; |
||
| 719 | } |
||
| 720 | if ((rc = ipc_data_write_finalize(callid, new, len))) { |
||
| 721 | ipc_answer_0(rid, rc); |
||
| 722 | free(old); |
||
| 723 | free(new); |
||
| 724 | return; |
||
| 725 | } |
||
| 726 | new[len] = '\0'; |
||
| 727 | |||
| 728 | char *oldc = canonify(old, &len); |
||
| 729 | char *newc = canonify(new, NULL); |
||
| 730 | if (!oldc || !newc) { |
||
| 731 | ipc_answer_0(rid, EINVAL); |
||
| 732 | free(old); |
||
| 733 | free(new); |
||
| 734 | return; |
||
| 735 | } |
||
| 736 | if (!strncmp(newc, oldc, len)) { |
||
| 737 | /* oldc is a prefix of newc */ |
||
| 738 | ipc_answer_0(rid, EINVAL); |
||
| 739 | free(old); |
||
| 740 | free(new); |
||
| 741 | return; |
||
| 742 | } |
||
| 743 | |||
| 744 | vfs_lookup_res_t old_lr; |
||
| 745 | vfs_lookup_res_t new_lr; |
||
| 746 | vfs_lookup_res_t new_par_lr; |
||
| 747 | rwlock_write_lock(&namespace_rwlock); |
||
| 748 | /* Lookup the node belonging to the old file name. */ |
||
| 749 | rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL); |
||
| 750 | if (rc != EOK) { |
||
| 751 | rwlock_write_unlock(&namespace_rwlock); |
||
| 752 | ipc_answer_0(rid, rc); |
||
| 753 | free(old); |
||
| 754 | free(new); |
||
| 755 | return; |
||
| 756 | } |
||
| 757 | vfs_node_t *old_node = vfs_node_get(&old_lr); |
||
| 758 | if (!old_node) { |
||
| 759 | rwlock_write_unlock(&namespace_rwlock); |
||
| 760 | ipc_answer_0(rid, ENOMEM); |
||
| 761 | free(old); |
||
| 762 | free(new); |
||
| 763 | return; |
||
| 764 | } |
||
| 765 | /* Lookup parent of the new file name. */ |
||
| 766 | rc = vfs_lookup_internal(newc, L_PARENT, &new_par_lr, NULL); |
||
| 767 | if (rc != EOK) { |
||
| 768 | rwlock_write_unlock(&namespace_rwlock); |
||
| 769 | ipc_answer_0(rid, rc); |
||
| 770 | free(old); |
||
| 771 | free(new); |
||
| 772 | return; |
||
| 773 | } |
||
| 774 | /* Check whether linking to the same file system instance. */ |
||
| 775 | if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) || |
||
| 776 | (old_node->dev_handle != new_par_lr.triplet.dev_handle)) { |
||
| 777 | rwlock_write_unlock(&namespace_rwlock); |
||
| 778 | ipc_answer_0(rid, EXDEV); /* different file systems */ |
||
| 779 | free(old); |
||
| 780 | free(new); |
||
| 781 | return; |
||
| 782 | } |
||
| 783 | /* Destroy the old link for the new name. */ |
||
| 784 | vfs_node_t *new_node = NULL; |
||
| 785 | rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL); |
||
| 786 | switch (rc) { |
||
| 787 | case ENOENT: |
||
| 788 | /* simply not in our way */ |
||
| 789 | break; |
||
| 790 | case EOK: |
||
| 791 | new_node = vfs_node_get(&new_lr); |
||
| 792 | if (!new_node) { |
||
| 793 | rwlock_write_unlock(&namespace_rwlock); |
||
| 794 | ipc_answer_0(rid, ENOMEM); |
||
| 795 | free(old); |
||
| 796 | free(new); |
||
| 797 | return; |
||
| 798 | } |
||
| 2766 | jermar | 799 | futex_down(&nodes_futex); |
| 2763 | jermar | 800 | new_node->lnkcnt--; |
| 2766 | jermar | 801 | futex_up(&nodes_futex); |
| 2763 | jermar | 802 | break; |
| 803 | default: |
||
| 804 | rwlock_write_unlock(&namespace_rwlock); |
||
| 805 | ipc_answer_0(rid, ENOTEMPTY); |
||
| 806 | free(old); |
||
| 807 | free(new); |
||
| 808 | return; |
||
| 809 | } |
||
| 810 | /* Create the new link for the new name. */ |
||
| 811 | rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index); |
||
| 812 | if (rc != EOK) { |
||
| 813 | rwlock_write_unlock(&namespace_rwlock); |
||
| 814 | if (new_node) |
||
| 815 | vfs_node_put(new_node); |
||
| 816 | ipc_answer_0(rid, rc); |
||
| 817 | free(old); |
||
| 818 | free(new); |
||
| 819 | return; |
||
| 820 | } |
||
| 2766 | jermar | 821 | futex_down(&nodes_futex); |
| 2763 | jermar | 822 | old_node->lnkcnt++; |
| 2766 | jermar | 823 | futex_up(&nodes_futex); |
| 2763 | jermar | 824 | /* Destroy the link for the old name. */ |
| 825 | rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL); |
||
| 826 | if (rc != EOK) { |
||
| 827 | rwlock_write_unlock(&namespace_rwlock); |
||
| 828 | vfs_node_put(old_node); |
||
| 829 | if (new_node) |
||
| 830 | vfs_node_put(new_node); |
||
| 831 | ipc_answer_0(rid, rc); |
||
| 832 | free(old); |
||
| 833 | free(new); |
||
| 834 | return; |
||
| 835 | } |
||
| 2766 | jermar | 836 | futex_down(&nodes_futex); |
| 2763 | jermar | 837 | old_node->lnkcnt--; |
| 2766 | jermar | 838 | futex_up(&nodes_futex); |
| 2763 | jermar | 839 | rwlock_write_unlock(&namespace_rwlock); |
| 840 | vfs_node_put(old_node); |
||
| 841 | if (new_node) |
||
| 842 | vfs_node_put(new_node); |
||
| 843 | free(old); |
||
| 844 | free(new); |
||
| 845 | ipc_answer_0(rid, EOK); |
||
| 846 | } |
||
| 847 | |||
| 2689 | jermar | 848 | /** |
| 849 | * @} |
||
| 850 | */ |