[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 |
|
---|
| 47 | /**
|
---|
| 48 | * This is a per-connection table of open files.
|
---|
| 49 | * Our assumption is that each client opens only one connection and therefore
|
---|
| 50 | * there is one table of open files per task. However, this may not be the case
|
---|
| 51 | * and the client can open more connections to VFS. In that case, there will be
|
---|
| 52 | * several tables and several file handle name spaces per task. Besides of this,
|
---|
| 53 | * the functionality will stay unchanged. So unless the client knows what it is
|
---|
| 54 | * doing, it should open one connection to VFS only.
|
---|
| 55 | *
|
---|
| 56 | * Allocation of the open files table is deferred until the client makes the
|
---|
| 57 | * first VFS_OPEN operation.
|
---|
| 58 | *
|
---|
| 59 | * This resource being per-connection and, in the first place, per-fibril, we
|
---|
[553492be] | 60 | * don't need to protect it by a mutex.
|
---|
[320c884] | 61 | */
|
---|
[26360f7] | 62 | fibril_local vfs_file_t **files = NULL;
|
---|
[320c884] | 63 |
|
---|
| 64 | /** Initialize the table of open files. */
|
---|
| 65 | bool vfs_files_init(void)
|
---|
| 66 | {
|
---|
| 67 | if (!files) {
|
---|
| 68 | files = malloc(MAX_OPEN_FILES * sizeof(vfs_file_t *));
|
---|
| 69 | if (!files)
|
---|
| 70 | return false;
|
---|
| 71 | memset(files, 0, MAX_OPEN_FILES * sizeof(vfs_file_t *));
|
---|
| 72 | }
|
---|
| 73 | return true;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[f29a3a2] | 76 | /** Cleanup the table of open files. */
|
---|
| 77 | void vfs_files_done(void)
|
---|
| 78 | {
|
---|
| 79 | int i;
|
---|
| 80 |
|
---|
| 81 | if (!files)
|
---|
| 82 | return;
|
---|
| 83 |
|
---|
| 84 | for (i = 0; i < MAX_OPEN_FILES; i++) {
|
---|
| 85 | if (files[i]) {
|
---|
| 86 | (void) vfs_close_internal(files[i]);
|
---|
| 87 | (void) vfs_fd_free(i);
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | free(files);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[320c884] | 94 | /** Allocate a file descriptor.
|
---|
| 95 | *
|
---|
[2b88074b] | 96 | * @param desc If true, look for an available file descriptor
|
---|
| 97 | * in a descending order.
|
---|
| 98 | *
|
---|
| 99 | * @return First available file descriptor or a negative error
|
---|
| 100 | * code.
|
---|
[320c884] | 101 | */
|
---|
[2b88074b] | 102 | int vfs_fd_alloc(bool desc)
|
---|
[320c884] | 103 | {
|
---|
[ac23b9d3] | 104 | if (!vfs_files_init())
|
---|
| 105 | return ENOMEM;
|
---|
| 106 |
|
---|
| 107 | unsigned int i;
|
---|
[2b88074b] | 108 | if (desc)
|
---|
[42fa698] | 109 | i = MAX_OPEN_FILES - 1;
|
---|
[2b88074b] | 110 | else
|
---|
| 111 | i = 0;
|
---|
| 112 |
|
---|
| 113 | while (true) {
|
---|
[320c884] | 114 | if (!files[i]) {
|
---|
| 115 | files[i] = (vfs_file_t *) malloc(sizeof(vfs_file_t));
|
---|
| 116 | if (!files[i])
|
---|
| 117 | return ENOMEM;
|
---|
[ac23b9d3] | 118 |
|
---|
[320c884] | 119 | memset(files[i], 0, sizeof(vfs_file_t));
|
---|
[230260ac] | 120 | fibril_mutex_initialize(&files[i]->lock);
|
---|
[320c884] | 121 | vfs_file_addref(files[i]);
|
---|
[ac23b9d3] | 122 | return (int) i;
|
---|
[320c884] | 123 | }
|
---|
[2b88074b] | 124 |
|
---|
| 125 | if (desc) {
|
---|
| 126 | if (i == 0)
|
---|
| 127 | break;
|
---|
| 128 |
|
---|
| 129 | i--;
|
---|
| 130 | } else {
|
---|
[d8f92868] | 131 | if (i == MAX_OPEN_FILES - 1)
|
---|
[2b88074b] | 132 | break;
|
---|
| 133 |
|
---|
| 134 | i++;
|
---|
| 135 | }
|
---|
[320c884] | 136 | }
|
---|
[ac23b9d3] | 137 |
|
---|
[320c884] | 138 | return EMFILE;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | /** Release file descriptor.
|
---|
| 142 | *
|
---|
| 143 | * @param fd File descriptor being released.
|
---|
[b7f9087] | 144 | *
|
---|
| 145 | * @return EOK on success or EBADF if fd is an invalid file
|
---|
| 146 | * descriptor.
|
---|
[320c884] | 147 | */
|
---|
[b7f9087] | 148 | int vfs_fd_free(int fd)
|
---|
[320c884] | 149 | {
|
---|
[ac23b9d3] | 150 | if (!vfs_files_init())
|
---|
| 151 | return ENOMEM;
|
---|
| 152 |
|
---|
[386e276] | 153 | if ((fd < 0) || (fd >= MAX_OPEN_FILES) || (files[fd] == NULL))
|
---|
[b7f9087] | 154 | return EBADF;
|
---|
[ac23b9d3] | 155 |
|
---|
[320c884] | 156 | vfs_file_delref(files[fd]);
|
---|
| 157 | files[fd] = NULL;
|
---|
[ac23b9d3] | 158 |
|
---|
[b7f9087] | 159 | return EOK;
|
---|
[320c884] | 160 | }
|
---|
| 161 |
|
---|
[2b88074b] | 162 | /** Assign a file to a file descriptor.
|
---|
| 163 | *
|
---|
| 164 | * @param file File to assign.
|
---|
| 165 | * @param fd File descriptor to assign to.
|
---|
| 166 | *
|
---|
| 167 | * @return EOK on success or EINVAL if fd is an invalid or already
|
---|
| 168 | * used file descriptor.
|
---|
| 169 | *
|
---|
| 170 | */
|
---|
| 171 | int vfs_fd_assign(vfs_file_t *file, int fd)
|
---|
| 172 | {
|
---|
| 173 | if (!vfs_files_init())
|
---|
| 174 | return ENOMEM;
|
---|
| 175 |
|
---|
| 176 | if ((fd < 0) || (fd >= MAX_OPEN_FILES) || (files[fd] != NULL))
|
---|
| 177 | return EINVAL;
|
---|
| 178 |
|
---|
| 179 | files[fd] = file;
|
---|
| 180 | vfs_file_addref(files[fd]);
|
---|
| 181 |
|
---|
| 182 | return EOK;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
[320c884] | 185 | /** Increment reference count of VFS file structure.
|
---|
| 186 | *
|
---|
| 187 | * @param file File structure that will have reference count
|
---|
| 188 | * incremented.
|
---|
| 189 | */
|
---|
| 190 | void vfs_file_addref(vfs_file_t *file)
|
---|
| 191 | {
|
---|
| 192 | /*
|
---|
| 193 | * File structures are per-connection, so no-one, except the current
|
---|
| 194 | * fibril, should have a reference to them. This is the reason we don't
|
---|
| 195 | * do any synchronization here.
|
---|
| 196 | */
|
---|
| 197 | file->refcnt++;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | /** Decrement reference count of VFS file structure.
|
---|
| 201 | *
|
---|
| 202 | * @param file File structure that will have reference count
|
---|
| 203 | * decremented.
|
---|
| 204 | */
|
---|
| 205 | void vfs_file_delref(vfs_file_t *file)
|
---|
| 206 | {
|
---|
| 207 | if (file->refcnt-- == 1) {
|
---|
| 208 | /*
|
---|
[f17667a] | 209 | * Lost the last reference to a file, need to drop our reference
|
---|
[320c884] | 210 | * to the underlying VFS node.
|
---|
| 211 | */
|
---|
| 212 | vfs_node_delref(file->node);
|
---|
| 213 | free(file);
|
---|
| 214 | }
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | /** Find VFS file structure for a given file descriptor.
|
---|
| 218 | *
|
---|
| 219 | * @param fd File descriptor.
|
---|
| 220 | *
|
---|
| 221 | * @return VFS file structure corresponding to fd.
|
---|
| 222 | */
|
---|
| 223 | vfs_file_t *vfs_file_get(int fd)
|
---|
| 224 | {
|
---|
[ac23b9d3] | 225 | if (!vfs_files_init())
|
---|
| 226 | return NULL;
|
---|
| 227 |
|
---|
[386e276] | 228 | if ((fd >= 0) && (fd < MAX_OPEN_FILES))
|
---|
[ebd9392] | 229 | return files[fd];
|
---|
[ac23b9d3] | 230 |
|
---|
[ebd9392] | 231 | return NULL;
|
---|
[320c884] | 232 | }
|
---|
| 233 |
|
---|
| 234 | /**
|
---|
| 235 | * @}
|
---|
[ac23b9d3] | 236 | */
|
---|