[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 | /**
|
---|
| 34 | * @file vfs.c
|
---|
| 35 | * @brief VFS_LOOKUP method and Path Lookup Buffer code.
|
---|
| 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 |
|
---|
[6c117bb] | 48 | #define min(a, b) ((a) < (b) ? (a) : (b))
|
---|
| 49 |
|
---|
[bcf23cf] | 50 | atomic_t plb_futex = FUTEX_INITIALIZER;
|
---|
[6c117bb] | 51 | link_t plb_head; /**< PLB entry ring buffer. */
|
---|
[bcf23cf] | 52 | uint8_t *plb = NULL;
|
---|
| 53 |
|
---|
[12a56fa] | 54 | /** Perform a path lookup.
|
---|
| 55 | *
|
---|
| 56 | * @param path Path to be resolved; it needn't be an ASCIIZ string.
|
---|
| 57 | * @param len Number of path characters pointed by path.
|
---|
| 58 | * @param result Empty node structure where the result will be stored.
|
---|
| 59 | * @param altroot If non-empty, will be used instead of rootfs as the root
|
---|
| 60 | * of the whole VFS tree.
|
---|
| 61 | *
|
---|
| 62 | * @return EOK on success or an error code from errno.h.
|
---|
| 63 | */
|
---|
[4d21cf8] | 64 | int vfs_lookup_internal(char *path, size_t len, vfs_triplet_t *result,
|
---|
| 65 | vfs_pair_t *altroot)
|
---|
[bcf23cf] | 66 | {
|
---|
[4d21cf8] | 67 | vfs_pair_t *root;
|
---|
[12a56fa] | 68 |
|
---|
[6c117bb] | 69 | if (!len)
|
---|
| 70 | return EINVAL;
|
---|
| 71 |
|
---|
[12a56fa] | 72 | if (altroot)
|
---|
| 73 | root = altroot;
|
---|
| 74 | else
|
---|
[4d21cf8] | 75 | root = (vfs_pair_t *) &rootfs;
|
---|
[12a56fa] | 76 |
|
---|
[7313e7a] | 77 | if (!root->fs_handle)
|
---|
[6c117bb] | 78 | return ENOENT;
|
---|
| 79 |
|
---|
| 80 | futex_down(&plb_futex);
|
---|
| 81 |
|
---|
| 82 | plb_entry_t entry;
|
---|
| 83 | link_initialize(&entry.plb_link);
|
---|
| 84 | entry.len = len;
|
---|
| 85 |
|
---|
| 86 | off_t first; /* the first free index */
|
---|
| 87 | off_t last; /* the last free index */
|
---|
| 88 |
|
---|
| 89 | if (list_empty(&plb_head)) {
|
---|
| 90 | first = 0;
|
---|
| 91 | last = PLB_SIZE - 1;
|
---|
| 92 | } else {
|
---|
| 93 | plb_entry_t *oldest = list_get_instance(plb_head.next,
|
---|
| 94 | plb_entry_t, plb_link);
|
---|
| 95 | plb_entry_t *newest = list_get_instance(plb_head.prev,
|
---|
| 96 | plb_entry_t, plb_link);
|
---|
| 97 |
|
---|
| 98 | first = (newest->index + newest->len) % PLB_SIZE;
|
---|
| 99 | last = (oldest->index - 1) % PLB_SIZE;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | if (first <= last) {
|
---|
| 103 | if ((last - first) + 1 < len) {
|
---|
| 104 | /*
|
---|
| 105 | * The buffer cannot absorb the path.
|
---|
| 106 | */
|
---|
| 107 | futex_up(&plb_futex);
|
---|
| 108 | return ELIMIT;
|
---|
| 109 | }
|
---|
| 110 | } else {
|
---|
| 111 | if (PLB_SIZE - ((first - last) + 1) < len) {
|
---|
| 112 | /*
|
---|
| 113 | * The buffer cannot absorb the path.
|
---|
| 114 | */
|
---|
| 115 | futex_up(&plb_futex);
|
---|
| 116 | return ELIMIT;
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | /*
|
---|
| 121 | * We know the first free index in PLB and we also know that there is
|
---|
| 122 | * enough space in the buffer to hold our path.
|
---|
| 123 | */
|
---|
| 124 |
|
---|
| 125 | entry.index = first;
|
---|
| 126 | entry.len = len;
|
---|
| 127 |
|
---|
| 128 | /*
|
---|
| 129 | * Claim PLB space by inserting the entry into the PLB entry ring
|
---|
| 130 | * buffer.
|
---|
| 131 | */
|
---|
| 132 | list_append(&entry.plb_link, &plb_head);
|
---|
| 133 |
|
---|
| 134 | futex_up(&plb_futex);
|
---|
| 135 |
|
---|
| 136 | /*
|
---|
| 137 | * Copy the path into PLB.
|
---|
| 138 | */
|
---|
| 139 | size_t cnt1 = min(len, (PLB_SIZE - first) + 1);
|
---|
| 140 | size_t cnt2 = len - cnt1;
|
---|
| 141 |
|
---|
| 142 | memcpy(&plb[first], path, cnt1);
|
---|
| 143 | memcpy(plb, &path[cnt1], cnt2);
|
---|
| 144 |
|
---|
| 145 | ipc_call_t answer;
|
---|
[12a56fa] | 146 | int phone = vfs_grab_phone(root->fs_handle);
|
---|
[5f4cfb1e] | 147 | aid_t req = async_send_3(phone, VFS_LOOKUP, (ipcarg_t) first,
|
---|
[12a56fa] | 148 | (ipcarg_t) last, (ipcarg_t) root->dev_handle, &answer);
|
---|
[5f4cfb1e] | 149 | vfs_release_phone(phone);
|
---|
[6c117bb] | 150 |
|
---|
| 151 | ipcarg_t rc;
|
---|
| 152 | async_wait_for(req, &rc);
|
---|
| 153 |
|
---|
| 154 | futex_down(&plb_futex);
|
---|
| 155 | list_remove(&entry.plb_link);
|
---|
[12a56fa] | 156 | /*
|
---|
| 157 | * Erasing the path from PLB will come handy for debugging purposes.
|
---|
| 158 | */
|
---|
| 159 | memset(&plb[first], 0, cnt1);
|
---|
| 160 | memset(plb, 0, cnt2);
|
---|
[6c117bb] | 161 | futex_up(&plb_futex);
|
---|
| 162 |
|
---|
| 163 | if (rc == EOK) {
|
---|
| 164 | result->fs_handle = (int) IPC_GET_ARG1(answer);
|
---|
| 165 | result->dev_handle = (int) IPC_GET_ARG2(answer);
|
---|
| 166 | result->index = (int) IPC_GET_ARG3(answer);
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | return rc;
|
---|
[bcf23cf] | 170 | }
|
---|
| 171 |
|
---|
| 172 | /**
|
---|
| 173 | * @}
|
---|
| 174 | */
|
---|