[320c884] | 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>
|
---|
[19f857a] | 40 | #include <str.h>
|
---|
[320c884] | 41 | #include <assert.h>
|
---|
| 42 | #include <bool.h>
|
---|
[26360f7] | 43 | #include <fibril.h>
|
---|
[1e4cada] | 44 | #include <fibril_synch.h>
|
---|
[320c884] | 45 | #include "vfs.h"
|
---|
| 46 |
|
---|
[b75e929] | 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;
|
---|
[320c884] | 54 |
|
---|
| 55 | /** Initialize the table of open files. */
|
---|
[b75e929] | 56 | static bool vfs_files_init(void)
|
---|
[320c884] | 57 | {
|
---|
[b75e929] | 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);
|
---|
[320c884] | 63 | return false;
|
---|
[b75e929] | 64 | }
|
---|
| 65 | memset(FILES, 0, MAX_OPEN_FILES * sizeof(vfs_file_t *));
|
---|
[320c884] | 66 | }
|
---|
[b75e929] | 67 | fibril_mutex_unlock(&VFS_DATA->lock);
|
---|
[320c884] | 68 | return true;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[f29a3a2] | 71 | /** Cleanup the table of open files. */
|
---|
[b75e929] | 72 | static void vfs_files_done(void)
|
---|
[f29a3a2] | 73 | {
|
---|
| 74 | int i;
|
---|
| 75 |
|
---|
[b75e929] | 76 | if (!FILES)
|
---|
[f29a3a2] | 77 | return;
|
---|
| 78 |
|
---|
| 79 | for (i = 0; i < MAX_OPEN_FILES; i++) {
|
---|
[b75e929] | 80 | if (FILES[i]) {
|
---|
| 81 | (void) vfs_close_internal(FILES[i]);
|
---|
[f29a3a2] | 82 | (void) vfs_fd_free(i);
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[b75e929] | 86 | free(FILES);
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | void *vfs_client_data_create(void)
|
---|
| 90 | {
|
---|
| 91 | vfs_client_data_t *vfs_data;
|
---|
| 92 |
|
---|
| 93 | vfs_data = malloc(sizeof(vfs_client_data_t));
|
---|
| 94 | if (vfs_data) {
|
---|
| 95 | fibril_mutex_initialize(&vfs_data->lock);
|
---|
| 96 | vfs_data->files = NULL;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | return vfs_data;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | void vfs_client_data_destroy(void *data)
|
---|
| 103 | {
|
---|
| 104 | vfs_client_data_t *vfs_data = (vfs_client_data_t *) data;
|
---|
| 105 |
|
---|
| 106 | vfs_files_done();
|
---|
| 107 | free(vfs_data);
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | /** Increment reference count of VFS file structure.
|
---|
| 111 | *
|
---|
| 112 | * @param file File structure that will have reference count
|
---|
| 113 | * incremented.
|
---|
| 114 | */
|
---|
| 115 | static void vfs_file_addref(vfs_file_t *file)
|
---|
| 116 | {
|
---|
| 117 | assert(fibril_mutex_is_locked(&VFS_DATA->lock));
|
---|
| 118 |
|
---|
| 119 | file->refcnt++;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | /** Decrement reference count of VFS file structure.
|
---|
| 123 | *
|
---|
| 124 | * @param file File structure that will have reference count
|
---|
| 125 | * decremented.
|
---|
| 126 | */
|
---|
| 127 | static void vfs_file_delref(vfs_file_t *file)
|
---|
| 128 | {
|
---|
| 129 | assert(fibril_mutex_is_locked(&VFS_DATA->lock));
|
---|
| 130 |
|
---|
| 131 | if (file->refcnt-- == 1) {
|
---|
| 132 | /*
|
---|
| 133 | * Lost the last reference to a file, need to drop our reference
|
---|
| 134 | * to the underlying VFS node.
|
---|
| 135 | */
|
---|
| 136 | vfs_node_delref(file->node);
|
---|
| 137 | free(file);
|
---|
| 138 | }
|
---|
[f29a3a2] | 139 | }
|
---|
| 140 |
|
---|
[b75e929] | 141 |
|
---|
[320c884] | 142 | /** Allocate a file descriptor.
|
---|
| 143 | *
|
---|
[2b88074b] | 144 | * @param desc If true, look for an available file descriptor
|
---|
| 145 | * in a descending order.
|
---|
| 146 | *
|
---|
| 147 | * @return First available file descriptor or a negative error
|
---|
| 148 | * code.
|
---|
[320c884] | 149 | */
|
---|
[2b88074b] | 150 | int vfs_fd_alloc(bool desc)
|
---|
[320c884] | 151 | {
|
---|
[ac23b9d3] | 152 | if (!vfs_files_init())
|
---|
| 153 | return ENOMEM;
|
---|
| 154 |
|
---|
| 155 | unsigned int i;
|
---|
[2b88074b] | 156 | if (desc)
|
---|
[42fa698] | 157 | i = MAX_OPEN_FILES - 1;
|
---|
[2b88074b] | 158 | else
|
---|
| 159 | i = 0;
|
---|
| 160 |
|
---|
[b75e929] | 161 | fibril_mutex_lock(&VFS_DATA->lock);
|
---|
[2b88074b] | 162 | while (true) {
|
---|
[b75e929] | 163 | if (!FILES[i]) {
|
---|
| 164 | FILES[i] = (vfs_file_t *) malloc(sizeof(vfs_file_t));
|
---|
| 165 | if (!FILES[i]) {
|
---|
| 166 | fibril_mutex_unlock(&VFS_DATA->lock);
|
---|
[320c884] | 167 | return ENOMEM;
|
---|
[b75e929] | 168 | }
|
---|
[ac23b9d3] | 169 |
|
---|
[b75e929] | 170 | memset(FILES[i], 0, sizeof(vfs_file_t));
|
---|
| 171 | fibril_mutex_initialize(&FILES[i]->lock);
|
---|
| 172 | vfs_file_addref(FILES[i]);
|
---|
| 173 | fibril_mutex_unlock(&VFS_DATA->lock);
|
---|
[ac23b9d3] | 174 | return (int) i;
|
---|
[320c884] | 175 | }
|
---|
[2b88074b] | 176 |
|
---|
| 177 | if (desc) {
|
---|
| 178 | if (i == 0)
|
---|
| 179 | break;
|
---|
| 180 |
|
---|
| 181 | i--;
|
---|
| 182 | } else {
|
---|
[d8f92868] | 183 | if (i == MAX_OPEN_FILES - 1)
|
---|
[2b88074b] | 184 | break;
|
---|
| 185 |
|
---|
| 186 | i++;
|
---|
| 187 | }
|
---|
[320c884] | 188 | }
|
---|
[b75e929] | 189 | fibril_mutex_unlock(&VFS_DATA->lock);
|
---|
[ac23b9d3] | 190 |
|
---|
[320c884] | 191 | return EMFILE;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | /** Release file descriptor.
|
---|
| 195 | *
|
---|
| 196 | * @param fd File descriptor being released.
|
---|
[b7f9087] | 197 | *
|
---|
| 198 | * @return EOK on success or EBADF if fd is an invalid file
|
---|
| 199 | * descriptor.
|
---|
[320c884] | 200 | */
|
---|
[b7f9087] | 201 | int vfs_fd_free(int fd)
|
---|
[320c884] | 202 | {
|
---|
[ac23b9d3] | 203 | if (!vfs_files_init())
|
---|
| 204 | return ENOMEM;
|
---|
[b75e929] | 205 |
|
---|
| 206 | fibril_mutex_lock(&VFS_DATA->lock);
|
---|
| 207 | if ((fd < 0) || (fd >= MAX_OPEN_FILES) || (FILES[fd] == NULL)) {
|
---|
| 208 | fibril_mutex_unlock(&VFS_DATA->lock);
|
---|
[b7f9087] | 209 | return EBADF;
|
---|
[b75e929] | 210 | }
|
---|
[ac23b9d3] | 211 |
|
---|
[b75e929] | 212 | vfs_file_delref(FILES[fd]);
|
---|
| 213 | FILES[fd] = NULL;
|
---|
| 214 | fibril_mutex_unlock(&VFS_DATA->lock);
|
---|
[ac23b9d3] | 215 |
|
---|
[b7f9087] | 216 | return EOK;
|
---|
[320c884] | 217 | }
|
---|
| 218 |
|
---|
[2b88074b] | 219 | /** Assign a file to a file descriptor.
|
---|
| 220 | *
|
---|
| 221 | * @param file File to assign.
|
---|
| 222 | * @param fd File descriptor to assign to.
|
---|
| 223 | *
|
---|
| 224 | * @return EOK on success or EINVAL if fd is an invalid or already
|
---|
| 225 | * used file descriptor.
|
---|
| 226 | *
|
---|
| 227 | */
|
---|
| 228 | int vfs_fd_assign(vfs_file_t *file, int fd)
|
---|
| 229 | {
|
---|
| 230 | if (!vfs_files_init())
|
---|
| 231 | return ENOMEM;
|
---|
[b75e929] | 232 |
|
---|
| 233 | fibril_mutex_lock(&VFS_DATA->lock);
|
---|
| 234 | if ((fd < 0) || (fd >= MAX_OPEN_FILES) || (FILES[fd] != NULL)) {
|
---|
| 235 | fibril_mutex_unlock(&VFS_DATA->lock);
|
---|
[2b88074b] | 236 | return EINVAL;
|
---|
[b75e929] | 237 | }
|
---|
[2b88074b] | 238 |
|
---|
[b75e929] | 239 | FILES[fd] = file;
|
---|
| 240 | vfs_file_addref(FILES[fd]);
|
---|
| 241 | fibril_mutex_unlock(&VFS_DATA->lock);
|
---|
[2b88074b] | 242 |
|
---|
| 243 | return EOK;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
[320c884] | 246 | /** Find VFS file structure for a given file descriptor.
|
---|
| 247 | *
|
---|
| 248 | * @param fd File descriptor.
|
---|
| 249 | *
|
---|
| 250 | * @return VFS file structure corresponding to fd.
|
---|
| 251 | */
|
---|
| 252 | vfs_file_t *vfs_file_get(int fd)
|
---|
| 253 | {
|
---|
[ac23b9d3] | 254 | if (!vfs_files_init())
|
---|
| 255 | return NULL;
|
---|
| 256 |
|
---|
[b75e929] | 257 | fibril_mutex_lock(&VFS_DATA->lock);
|
---|
| 258 | if ((fd >= 0) && (fd < MAX_OPEN_FILES)) {
|
---|
| 259 | vfs_file_t *file = FILES[fd];
|
---|
[6639ae1] | 260 | if (file != NULL) {
|
---|
| 261 | vfs_file_addref(file);
|
---|
| 262 | fibril_mutex_unlock(&VFS_DATA->lock);
|
---|
| 263 | return file;
|
---|
| 264 | }
|
---|
[b75e929] | 265 | }
|
---|
| 266 | fibril_mutex_unlock(&VFS_DATA->lock);
|
---|
[ac23b9d3] | 267 |
|
---|
[ebd9392] | 268 | return NULL;
|
---|
[320c884] | 269 | }
|
---|
| 270 |
|
---|
[4fe94c66] | 271 | /** Stop using a file structure.
|
---|
| 272 | *
|
---|
| 273 | * @param file VFS file structure.
|
---|
| 274 | */
|
---|
| 275 | void vfs_file_put(vfs_file_t *file)
|
---|
| 276 | {
|
---|
| 277 | fibril_mutex_lock(&VFS_DATA->lock);
|
---|
| 278 | vfs_file_delref(file);
|
---|
| 279 | fibril_mutex_unlock(&VFS_DATA->lock);
|
---|
| 280 | }
|
---|
| 281 |
|
---|
[320c884] | 282 | /**
|
---|
| 283 | * @}
|
---|
[ac23b9d3] | 284 | */
|
---|