| 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_file.c
|
|---|
| 35 | * @brief Various operations on files have their home in this file.
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| 38 | #include <errno.h>
|
|---|
| 39 | #include <stdlib.h>
|
|---|
| 40 | #include <str.h>
|
|---|
| 41 | #include <assert.h>
|
|---|
| 42 | #include <bool.h>
|
|---|
| 43 | #include <fibril.h>
|
|---|
| 44 | #include <fibril_synch.h>
|
|---|
| 45 | #include "vfs.h"
|
|---|
| 46 |
|
|---|
| 47 | #define VFS_DATA ((vfs_client_data_t *) async_client_data_get())
|
|---|
| 48 | #define FILES (VFS_DATA->files)
|
|---|
| 49 |
|
|---|
| 50 | typedef struct {
|
|---|
| 51 | fibril_mutex_t lock;
|
|---|
| 52 | vfs_file_t **files;
|
|---|
| 53 | } vfs_client_data_t;
|
|---|
| 54 |
|
|---|
| 55 | /** Initialize the table of open files. */
|
|---|
| 56 | static bool vfs_files_init(void)
|
|---|
| 57 | {
|
|---|
| 58 | fibril_mutex_lock(&VFS_DATA->lock);
|
|---|
| 59 | if (!FILES) {
|
|---|
| 60 | FILES = malloc(MAX_OPEN_FILES * sizeof(vfs_file_t *));
|
|---|
| 61 | if (!FILES) {
|
|---|
| 62 | fibril_mutex_unlock(&VFS_DATA->lock);
|
|---|
| 63 | return false;
|
|---|
| 64 | }
|
|---|
| 65 | memset(FILES, 0, MAX_OPEN_FILES * sizeof(vfs_file_t *));
|
|---|
| 66 | }
|
|---|
| 67 | fibril_mutex_unlock(&VFS_DATA->lock);
|
|---|
| 68 | return true;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | /** Cleanup the table of open files. */
|
|---|
| 72 | static void vfs_files_done(void)
|
|---|
| 73 | {
|
|---|
| 74 | int i;
|
|---|
| 75 |
|
|---|
| 76 | if (!FILES)
|
|---|
| 77 | return;
|
|---|
| 78 |
|
|---|
| 79 | for (i = 0; i < MAX_OPEN_FILES; i++) {
|
|---|
| 80 | if (FILES[i]) {
|
|---|
| 81 | (void) vfs_fd_free(i);
|
|---|
| 82 | }
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | free(FILES);
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | void *vfs_client_data_create(void)
|
|---|
| 89 | {
|
|---|
| 90 | vfs_client_data_t *vfs_data;
|
|---|
| 91 |
|
|---|
| 92 | vfs_data = malloc(sizeof(vfs_client_data_t));
|
|---|
| 93 | if (vfs_data) {
|
|---|
| 94 | fibril_mutex_initialize(&vfs_data->lock);
|
|---|
| 95 | vfs_data->files = NULL;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | return vfs_data;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | void vfs_client_data_destroy(void *data)
|
|---|
| 102 | {
|
|---|
| 103 | vfs_client_data_t *vfs_data = (vfs_client_data_t *) data;
|
|---|
| 104 |
|
|---|
| 105 | vfs_files_done();
|
|---|
| 106 | free(vfs_data);
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | /** Close the file in the endpoint FS server. */
|
|---|
| 110 | static int vfs_file_close_remote(vfs_file_t *file)
|
|---|
| 111 | {
|
|---|
| 112 | ipc_call_t answer;
|
|---|
| 113 | aid_t msg;
|
|---|
| 114 | sysarg_t rc;
|
|---|
| 115 | int phone;
|
|---|
| 116 |
|
|---|
| 117 | assert(!file->refcnt);
|
|---|
| 118 |
|
|---|
| 119 | phone = vfs_grab_phone(file->node->fs_handle);
|
|---|
| 120 | msg = async_send_2(phone, VFS_OUT_CLOSE, file->node->devmap_handle,
|
|---|
| 121 | file->node->index, &answer);
|
|---|
| 122 | async_wait_for(msg, &rc);
|
|---|
| 123 | vfs_release_phone(file->node->fs_handle, phone);
|
|---|
| 124 |
|
|---|
| 125 | return IPC_GET_ARG1(answer);
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 | /** Increment reference count of VFS file structure.
|
|---|
| 130 | *
|
|---|
| 131 | * @param file File structure that will have reference count
|
|---|
| 132 | * incremented.
|
|---|
| 133 | */
|
|---|
| 134 | static void vfs_file_addref(vfs_file_t *file)
|
|---|
| 135 | {
|
|---|
| 136 | assert(fibril_mutex_is_locked(&VFS_DATA->lock));
|
|---|
| 137 |
|
|---|
| 138 | file->refcnt++;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | /** Decrement reference count of VFS file structure.
|
|---|
| 142 | *
|
|---|
| 143 | * @param file File structure that will have reference count
|
|---|
| 144 | * decremented.
|
|---|
| 145 | */
|
|---|
| 146 | static int vfs_file_delref(vfs_file_t *file)
|
|---|
| 147 | {
|
|---|
| 148 | int rc = EOK;
|
|---|
| 149 |
|
|---|
| 150 | assert(fibril_mutex_is_locked(&VFS_DATA->lock));
|
|---|
| 151 |
|
|---|
| 152 | if (file->refcnt-- == 1) {
|
|---|
| 153 | /*
|
|---|
| 154 | * Lost the last reference to a file, need to close it in the
|
|---|
| 155 | * endpoint FS and drop our reference to the underlying VFS node.
|
|---|
| 156 | */
|
|---|
| 157 | rc = vfs_file_close_remote(file);
|
|---|
| 158 | vfs_node_delref(file->node);
|
|---|
| 159 | free(file);
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | return rc;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 | /** Allocate a file descriptor.
|
|---|
| 167 | *
|
|---|
| 168 | * @param desc If true, look for an available file descriptor
|
|---|
| 169 | * in a descending order.
|
|---|
| 170 | *
|
|---|
| 171 | * @return First available file descriptor or a negative error
|
|---|
| 172 | * code.
|
|---|
| 173 | */
|
|---|
| 174 | int vfs_fd_alloc(bool desc)
|
|---|
| 175 | {
|
|---|
| 176 | if (!vfs_files_init())
|
|---|
| 177 | return ENOMEM;
|
|---|
| 178 |
|
|---|
| 179 | unsigned int i;
|
|---|
| 180 | if (desc)
|
|---|
| 181 | i = MAX_OPEN_FILES - 1;
|
|---|
| 182 | else
|
|---|
| 183 | i = 0;
|
|---|
| 184 |
|
|---|
| 185 | fibril_mutex_lock(&VFS_DATA->lock);
|
|---|
| 186 | while (true) {
|
|---|
| 187 | if (!FILES[i]) {
|
|---|
| 188 | FILES[i] = (vfs_file_t *) malloc(sizeof(vfs_file_t));
|
|---|
| 189 | if (!FILES[i]) {
|
|---|
| 190 | fibril_mutex_unlock(&VFS_DATA->lock);
|
|---|
| 191 | return ENOMEM;
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | memset(FILES[i], 0, sizeof(vfs_file_t));
|
|---|
| 195 | fibril_mutex_initialize(&FILES[i]->lock);
|
|---|
| 196 | vfs_file_addref(FILES[i]);
|
|---|
| 197 | fibril_mutex_unlock(&VFS_DATA->lock);
|
|---|
| 198 | return (int) i;
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | if (desc) {
|
|---|
| 202 | if (i == 0)
|
|---|
| 203 | break;
|
|---|
| 204 |
|
|---|
| 205 | i--;
|
|---|
| 206 | } else {
|
|---|
| 207 | if (i == MAX_OPEN_FILES - 1)
|
|---|
| 208 | break;
|
|---|
| 209 |
|
|---|
| 210 | i++;
|
|---|
| 211 | }
|
|---|
| 212 | }
|
|---|
| 213 | fibril_mutex_unlock(&VFS_DATA->lock);
|
|---|
| 214 |
|
|---|
| 215 | return EMFILE;
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | /** Release file descriptor.
|
|---|
| 219 | *
|
|---|
| 220 | * @param fd File descriptor being released.
|
|---|
| 221 | *
|
|---|
| 222 | * @return EOK on success or EBADF if fd is an invalid file
|
|---|
| 223 | * descriptor.
|
|---|
| 224 | */
|
|---|
| 225 | int vfs_fd_free(int fd)
|
|---|
| 226 | {
|
|---|
| 227 | int rc;
|
|---|
| 228 |
|
|---|
| 229 | if (!vfs_files_init())
|
|---|
| 230 | return ENOMEM;
|
|---|
| 231 |
|
|---|
| 232 | fibril_mutex_lock(&VFS_DATA->lock);
|
|---|
| 233 | if ((fd < 0) || (fd >= MAX_OPEN_FILES) || (FILES[fd] == NULL)) {
|
|---|
| 234 | fibril_mutex_unlock(&VFS_DATA->lock);
|
|---|
| 235 | return EBADF;
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | rc = vfs_file_delref(FILES[fd]);
|
|---|
| 239 | FILES[fd] = NULL;
|
|---|
| 240 | fibril_mutex_unlock(&VFS_DATA->lock);
|
|---|
| 241 |
|
|---|
| 242 | return rc;
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | /** Assign a file to a file descriptor.
|
|---|
| 246 | *
|
|---|
| 247 | * @param file File to assign.
|
|---|
| 248 | * @param fd File descriptor to assign to.
|
|---|
| 249 | *
|
|---|
| 250 | * @return EOK on success or EINVAL if fd is an invalid or already
|
|---|
| 251 | * used file descriptor.
|
|---|
| 252 | *
|
|---|
| 253 | */
|
|---|
| 254 | int vfs_fd_assign(vfs_file_t *file, int fd)
|
|---|
| 255 | {
|
|---|
| 256 | if (!vfs_files_init())
|
|---|
| 257 | return ENOMEM;
|
|---|
| 258 |
|
|---|
| 259 | fibril_mutex_lock(&VFS_DATA->lock);
|
|---|
| 260 | if ((fd < 0) || (fd >= MAX_OPEN_FILES) || (FILES[fd] != NULL)) {
|
|---|
| 261 | fibril_mutex_unlock(&VFS_DATA->lock);
|
|---|
| 262 | return EINVAL;
|
|---|
| 263 | }
|
|---|
| 264 |
|
|---|
| 265 | FILES[fd] = file;
|
|---|
| 266 | vfs_file_addref(FILES[fd]);
|
|---|
| 267 | fibril_mutex_unlock(&VFS_DATA->lock);
|
|---|
| 268 |
|
|---|
| 269 | return EOK;
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 | /** Find VFS file structure for a given file descriptor.
|
|---|
| 273 | *
|
|---|
| 274 | * @param fd File descriptor.
|
|---|
| 275 | *
|
|---|
| 276 | * @return VFS file structure corresponding to fd.
|
|---|
| 277 | */
|
|---|
| 278 | vfs_file_t *vfs_file_get(int fd)
|
|---|
| 279 | {
|
|---|
| 280 | if (!vfs_files_init())
|
|---|
| 281 | return NULL;
|
|---|
| 282 |
|
|---|
| 283 | fibril_mutex_lock(&VFS_DATA->lock);
|
|---|
| 284 | if ((fd >= 0) && (fd < MAX_OPEN_FILES)) {
|
|---|
| 285 | vfs_file_t *file = FILES[fd];
|
|---|
| 286 | if (file != NULL) {
|
|---|
| 287 | vfs_file_addref(file);
|
|---|
| 288 | fibril_mutex_unlock(&VFS_DATA->lock);
|
|---|
| 289 | return file;
|
|---|
| 290 | }
|
|---|
| 291 | }
|
|---|
| 292 | fibril_mutex_unlock(&VFS_DATA->lock);
|
|---|
| 293 |
|
|---|
| 294 | return NULL;
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 | /** Stop using a file structure.
|
|---|
| 298 | *
|
|---|
| 299 | * @param file VFS file structure.
|
|---|
| 300 | */
|
|---|
| 301 | void vfs_file_put(vfs_file_t *file)
|
|---|
| 302 | {
|
|---|
| 303 | fibril_mutex_lock(&VFS_DATA->lock);
|
|---|
| 304 | vfs_file_delref(file);
|
|---|
| 305 | fibril_mutex_unlock(&VFS_DATA->lock);
|
|---|
| 306 | }
|
|---|
| 307 |
|
|---|
| 308 | /**
|
|---|
| 309 | * @}
|
|---|
| 310 | */
|
|---|