| [be815bc] | 1 | /*
|
|---|
| [a2aa1dec] | 2 | * Copyright (c) 2008 Jakub Jermar
|
|---|
| [be815bc] | 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 fat_ops.c
|
|---|
| 35 | * @brief Implementation of VFS operations for the FAT file system server.
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| 38 | #include "fat.h"
|
|---|
| [033ef7d3] | 39 | #include "fat_dentry.h"
|
|---|
| 40 | #include "fat_fat.h"
|
|---|
| [6364d3c] | 41 | #include "../../vfs/vfs.h"
|
|---|
| [a2aa1dec] | 42 | #include <libfs.h>
|
|---|
| [fc840d9] | 43 | #include <libblock.h>
|
|---|
| [be815bc] | 44 | #include <ipc/ipc.h>
|
|---|
| [7a35204a] | 45 | #include <ipc/services.h>
|
|---|
| 46 | #include <ipc/devmap.h>
|
|---|
| [be815bc] | 47 | #include <async.h>
|
|---|
| 48 | #include <errno.h>
|
|---|
| [a2aa1dec] | 49 | #include <string.h>
|
|---|
| [776f2e6] | 50 | #include <byteorder.h>
|
|---|
| [d9c8c81] | 51 | #include <adt/hash_table.h>
|
|---|
| 52 | #include <adt/list.h>
|
|---|
| [e1e3b26] | 53 | #include <assert.h>
|
|---|
| [6ebe721] | 54 | #include <fibril_sync.h>
|
|---|
| [7a35204a] | 55 | #include <sys/mman.h>
|
|---|
| [8d32152] | 56 | #include <align.h>
|
|---|
| [e1e3b26] | 57 |
|
|---|
| [b6035ba] | 58 | #define FAT_NODE(node) ((node) ? (fat_node_t *) (node)->data : NULL)
|
|---|
| 59 | #define FS_NODE(node) ((node) ? (node)->bp : NULL)
|
|---|
| 60 |
|
|---|
| [6ebe721] | 61 | /** Mutex protecting the list of cached free FAT nodes. */
|
|---|
| 62 | static FIBRIL_MUTEX_INITIALIZE(ffn_mutex);
|
|---|
| [add5835] | 63 |
|
|---|
| 64 | /** List of cached free FAT nodes. */
|
|---|
| 65 | static LIST_INITIALIZE(ffn_head);
|
|---|
| [6364d3c] | 66 |
|
|---|
| [e1e3b26] | 67 | static void fat_node_initialize(fat_node_t *node)
|
|---|
| [a2aa1dec] | 68 | {
|
|---|
| [6ebe721] | 69 | fibril_mutex_initialize(&node->lock);
|
|---|
| [b6035ba] | 70 | node->bp = NULL;
|
|---|
| [869e546] | 71 | node->idx = NULL;
|
|---|
| [e1e3b26] | 72 | node->type = 0;
|
|---|
| 73 | link_initialize(&node->ffn_link);
|
|---|
| 74 | node->size = 0;
|
|---|
| 75 | node->lnkcnt = 0;
|
|---|
| 76 | node->refcnt = 0;
|
|---|
| 77 | node->dirty = false;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| [4098e38] | 80 | static int fat_node_sync(fat_node_t *node)
|
|---|
| [e1e3b26] | 81 | {
|
|---|
| [7858bc5f] | 82 | block_t *b;
|
|---|
| 83 | fat_bs_t *bs;
|
|---|
| [beb17734] | 84 | fat_dentry_t *d;
|
|---|
| 85 | uint16_t bps;
|
|---|
| 86 | unsigned dps;
|
|---|
| [c91f2d1b] | 87 | int rc;
|
|---|
| [beb17734] | 88 |
|
|---|
| 89 | assert(node->dirty);
|
|---|
| 90 |
|
|---|
| [7858bc5f] | 91 | bs = block_bb_get(node->idx->dev_handle);
|
|---|
| 92 | bps = uint16_t_le2host(bs->bps);
|
|---|
| [beb17734] | 93 | dps = bps / sizeof(fat_dentry_t);
|
|---|
| 94 |
|
|---|
| 95 | /* Read the block that contains the dentry of interest. */
|
|---|
| [684b655] | 96 | rc = _fat_block_get(&b, bs, node->idx->dev_handle, node->idx->pfc,
|
|---|
| [1d8cdb1] | 97 | (node->idx->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE);
|
|---|
| [4098e38] | 98 | if (rc != EOK)
|
|---|
| 99 | return rc;
|
|---|
| [beb17734] | 100 |
|
|---|
| 101 | d = ((fat_dentry_t *)b->data) + (node->idx->pdi % dps);
|
|---|
| 102 |
|
|---|
| 103 | d->firstc = host2uint16_t_le(node->firstc);
|
|---|
| [a5da446] | 104 | if (node->type == FAT_FILE) {
|
|---|
| [beb17734] | 105 | d->size = host2uint32_t_le(node->size);
|
|---|
| [a5da446] | 106 | } else if (node->type == FAT_DIRECTORY) {
|
|---|
| 107 | d->attr = FAT_ATTR_SUBDIR;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | /* TODO: update other fields? (e.g time fields) */
|
|---|
| [beb17734] | 111 |
|
|---|
| 112 | b->dirty = true; /* need to sync block */
|
|---|
| [c91f2d1b] | 113 | rc = block_put(b);
|
|---|
| [4098e38] | 114 | return rc;
|
|---|
| [e1e3b26] | 115 | }
|
|---|
| 116 |
|
|---|
| [9a3d5f0] | 117 | static fat_node_t *fat_node_get_new(void)
|
|---|
| 118 | {
|
|---|
| [b6035ba] | 119 | fs_node_t *fn;
|
|---|
| [9a3d5f0] | 120 | fat_node_t *nodep;
|
|---|
| [4098e38] | 121 | int rc;
|
|---|
| [9a3d5f0] | 122 |
|
|---|
| [6ebe721] | 123 | fibril_mutex_lock(&ffn_mutex);
|
|---|
| [9a3d5f0] | 124 | if (!list_empty(&ffn_head)) {
|
|---|
| 125 | /* Try to use a cached free node structure. */
|
|---|
| 126 | fat_idx_t *idxp_tmp;
|
|---|
| 127 | nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link);
|
|---|
| [6ebe721] | 128 | if (!fibril_mutex_trylock(&nodep->lock))
|
|---|
| [9a3d5f0] | 129 | goto skip_cache;
|
|---|
| 130 | idxp_tmp = nodep->idx;
|
|---|
| [6ebe721] | 131 | if (!fibril_mutex_trylock(&idxp_tmp->lock)) {
|
|---|
| 132 | fibril_mutex_unlock(&nodep->lock);
|
|---|
| [9a3d5f0] | 133 | goto skip_cache;
|
|---|
| 134 | }
|
|---|
| 135 | list_remove(&nodep->ffn_link);
|
|---|
| [6ebe721] | 136 | fibril_mutex_unlock(&ffn_mutex);
|
|---|
| [4098e38] | 137 | if (nodep->dirty) {
|
|---|
| 138 | rc = fat_node_sync(nodep);
|
|---|
| 139 | assert(rc == EOK);
|
|---|
| 140 | }
|
|---|
| [9a3d5f0] | 141 | idxp_tmp->nodep = NULL;
|
|---|
| [6ebe721] | 142 | fibril_mutex_unlock(&nodep->lock);
|
|---|
| 143 | fibril_mutex_unlock(&idxp_tmp->lock);
|
|---|
| [b6035ba] | 144 | fn = FS_NODE(nodep);
|
|---|
| [9a3d5f0] | 145 | } else {
|
|---|
| 146 | skip_cache:
|
|---|
| 147 | /* Try to allocate a new node structure. */
|
|---|
| [6ebe721] | 148 | fibril_mutex_unlock(&ffn_mutex);
|
|---|
| [b6035ba] | 149 | fn = (fs_node_t *)malloc(sizeof(fs_node_t));
|
|---|
| 150 | if (!fn)
|
|---|
| 151 | return NULL;
|
|---|
| [9a3d5f0] | 152 | nodep = (fat_node_t *)malloc(sizeof(fat_node_t));
|
|---|
| [b6035ba] | 153 | if (!nodep) {
|
|---|
| 154 | free(fn);
|
|---|
| [9a3d5f0] | 155 | return NULL;
|
|---|
| [b6035ba] | 156 | }
|
|---|
| [9a3d5f0] | 157 | }
|
|---|
| 158 | fat_node_initialize(nodep);
|
|---|
| [83937ccd] | 159 | fs_node_initialize(fn);
|
|---|
| [b6035ba] | 160 | fn->data = nodep;
|
|---|
| 161 | nodep->bp = fn;
|
|---|
| [9a3d5f0] | 162 |
|
|---|
| 163 | return nodep;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| [add5835] | 166 | /** Internal version of fat_node_get().
|
|---|
| 167 | *
|
|---|
| 168 | * @param idxp Locked index structure.
|
|---|
| 169 | */
|
|---|
| [b6035ba] | 170 | static fat_node_t *fat_node_get_core(fat_idx_t *idxp)
|
|---|
| [e1e3b26] | 171 | {
|
|---|
| [7858bc5f] | 172 | block_t *b;
|
|---|
| 173 | fat_bs_t *bs;
|
|---|
| [4573a79] | 174 | fat_dentry_t *d;
|
|---|
| [c06dbf9] | 175 | fat_node_t *nodep = NULL;
|
|---|
| [4573a79] | 176 | unsigned bps;
|
|---|
| [4f1c0b4] | 177 | unsigned spc;
|
|---|
| [4573a79] | 178 | unsigned dps;
|
|---|
| [c91f2d1b] | 179 | int rc;
|
|---|
| [4573a79] | 180 |
|
|---|
| [add5835] | 181 | if (idxp->nodep) {
|
|---|
| [4573a79] | 182 | /*
|
|---|
| 183 | * We are lucky.
|
|---|
| 184 | * The node is already instantiated in memory.
|
|---|
| 185 | */
|
|---|
| [6ebe721] | 186 | fibril_mutex_lock(&idxp->nodep->lock);
|
|---|
| [e6bc3a5] | 187 | if (!idxp->nodep->refcnt++) {
|
|---|
| 188 | fibril_mutex_lock(&ffn_mutex);
|
|---|
| [c06dbf9] | 189 | list_remove(&idxp->nodep->ffn_link);
|
|---|
| [e6bc3a5] | 190 | fibril_mutex_unlock(&ffn_mutex);
|
|---|
| 191 | }
|
|---|
| [6ebe721] | 192 | fibril_mutex_unlock(&idxp->nodep->lock);
|
|---|
| [add5835] | 193 | return idxp->nodep;
|
|---|
| [4573a79] | 194 | }
|
|---|
| 195 |
|
|---|
| 196 | /*
|
|---|
| 197 | * We must instantiate the node from the file system.
|
|---|
| 198 | */
|
|---|
| 199 |
|
|---|
| [add5835] | 200 | assert(idxp->pfc);
|
|---|
| [4573a79] | 201 |
|
|---|
| [9a3d5f0] | 202 | nodep = fat_node_get_new();
|
|---|
| 203 | if (!nodep)
|
|---|
| 204 | return NULL;
|
|---|
| [4573a79] | 205 |
|
|---|
| [7858bc5f] | 206 | bs = block_bb_get(idxp->dev_handle);
|
|---|
| 207 | bps = uint16_t_le2host(bs->bps);
|
|---|
| [4f1c0b4] | 208 | spc = bs->spc;
|
|---|
| [4573a79] | 209 | dps = bps / sizeof(fat_dentry_t);
|
|---|
| 210 |
|
|---|
| [2c4bbcde] | 211 | /* Read the block that contains the dentry of interest. */
|
|---|
| [684b655] | 212 | rc = _fat_block_get(&b, bs, idxp->dev_handle, idxp->pfc,
|
|---|
| [1d8cdb1] | 213 | (idxp->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE);
|
|---|
| [684b655] | 214 | assert(rc == EOK);
|
|---|
| [4573a79] | 215 |
|
|---|
| [add5835] | 216 | d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps);
|
|---|
| [2c4bbcde] | 217 | if (d->attr & FAT_ATTR_SUBDIR) {
|
|---|
| 218 | /*
|
|---|
| 219 | * The only directory which does not have this bit set is the
|
|---|
| 220 | * root directory itself. The root directory node is handled
|
|---|
| 221 | * and initialized elsewhere.
|
|---|
| 222 | */
|
|---|
| 223 | nodep->type = FAT_DIRECTORY;
|
|---|
| [2ab1023] | 224 | /*
|
|---|
| [e2115311] | 225 | * Unfortunately, the 'size' field of the FAT dentry is not
|
|---|
| 226 | * defined for the directory entry type. We must determine the
|
|---|
| 227 | * size of the directory by walking the FAT.
|
|---|
| [2ab1023] | 228 | */
|
|---|
| [e402382] | 229 | uint16_t clusters;
|
|---|
| 230 | rc = fat_clusters_get(&clusters, bs, idxp->dev_handle,
|
|---|
| [4f1c0b4] | 231 | uint16_t_le2host(d->firstc));
|
|---|
| [e402382] | 232 | assert(rc == EOK);
|
|---|
| 233 | nodep->size = bps * spc * clusters;
|
|---|
| [2c4bbcde] | 234 | } else {
|
|---|
| 235 | nodep->type = FAT_FILE;
|
|---|
| [2ab1023] | 236 | nodep->size = uint32_t_le2host(d->size);
|
|---|
| [2c4bbcde] | 237 | }
|
|---|
| 238 | nodep->firstc = uint16_t_le2host(d->firstc);
|
|---|
| 239 | nodep->lnkcnt = 1;
|
|---|
| 240 | nodep->refcnt = 1;
|
|---|
| 241 |
|
|---|
| [c91f2d1b] | 242 | rc = block_put(b);
|
|---|
| 243 | assert(rc == EOK);
|
|---|
| [2c4bbcde] | 244 |
|
|---|
| 245 | /* Link the idx structure with the node structure. */
|
|---|
| [add5835] | 246 | nodep->idx = idxp;
|
|---|
| 247 | idxp->nodep = nodep;
|
|---|
| [2c4bbcde] | 248 |
|
|---|
| 249 | return nodep;
|
|---|
| [a2aa1dec] | 250 | }
|
|---|
| 251 |
|
|---|
| [50e5b25] | 252 | /*
|
|---|
| 253 | * Forward declarations of FAT libfs operations.
|
|---|
| 254 | */
|
|---|
| [073f550] | 255 | static int fat_root_get(fs_node_t **, dev_handle_t);
|
|---|
| 256 | static int fat_match(fs_node_t **, fs_node_t *, const char *);
|
|---|
| 257 | static int fat_node_get(fs_node_t **, dev_handle_t, fs_index_t);
|
|---|
| 258 | static int fat_node_put(fs_node_t *);
|
|---|
| 259 | static int fat_create_node(fs_node_t **, dev_handle_t, int);
|
|---|
| [b6035ba] | 260 | static int fat_destroy_node(fs_node_t *);
|
|---|
| 261 | static int fat_link(fs_node_t *, fs_node_t *, const char *);
|
|---|
| [cf95bc0] | 262 | static int fat_unlink(fs_node_t *, fs_node_t *, const char *);
|
|---|
| [073f550] | 263 | static int fat_has_children(bool *, fs_node_t *);
|
|---|
| [b6035ba] | 264 | static fs_index_t fat_index_get(fs_node_t *);
|
|---|
| 265 | static size_t fat_size_get(fs_node_t *);
|
|---|
| 266 | static unsigned fat_lnkcnt_get(fs_node_t *);
|
|---|
| [50e5b25] | 267 | static char fat_plb_get_char(unsigned);
|
|---|
| [b6035ba] | 268 | static bool fat_is_directory(fs_node_t *);
|
|---|
| 269 | static bool fat_is_file(fs_node_t *node);
|
|---|
| [50e5b25] | 270 |
|
|---|
| 271 | /*
|
|---|
| 272 | * FAT libfs operations.
|
|---|
| 273 | */
|
|---|
| 274 |
|
|---|
| [073f550] | 275 | int fat_root_get(fs_node_t **rfn, dev_handle_t dev_handle)
|
|---|
| 276 | {
|
|---|
| 277 | return fat_node_get(rfn, dev_handle, 0);
|
|---|
| 278 | }
|
|---|
| 279 |
|
|---|
| 280 | int fat_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
|
|---|
| 281 | {
|
|---|
| 282 | fat_bs_t *bs;
|
|---|
| 283 | fat_node_t *parentp = FAT_NODE(pfn);
|
|---|
| 284 | char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
|
|---|
| 285 | unsigned i, j;
|
|---|
| 286 | unsigned bps; /* bytes per sector */
|
|---|
| 287 | unsigned dps; /* dentries per sector */
|
|---|
| 288 | unsigned blocks;
|
|---|
| 289 | fat_dentry_t *d;
|
|---|
| 290 | block_t *b;
|
|---|
| 291 | int rc;
|
|---|
| 292 |
|
|---|
| 293 | fibril_mutex_lock(&parentp->idx->lock);
|
|---|
| 294 | bs = block_bb_get(parentp->idx->dev_handle);
|
|---|
| 295 | bps = uint16_t_le2host(bs->bps);
|
|---|
| 296 | dps = bps / sizeof(fat_dentry_t);
|
|---|
| 297 | blocks = parentp->size / bps;
|
|---|
| 298 | for (i = 0; i < blocks; i++) {
|
|---|
| 299 | rc = fat_block_get(&b, bs, parentp, i, BLOCK_FLAGS_NONE);
|
|---|
| 300 | if (rc != EOK) {
|
|---|
| 301 | fibril_mutex_unlock(&parentp->idx->lock);
|
|---|
| 302 | return rc;
|
|---|
| 303 | }
|
|---|
| 304 | for (j = 0; j < dps; j++) {
|
|---|
| 305 | d = ((fat_dentry_t *)b->data) + j;
|
|---|
| 306 | switch (fat_classify_dentry(d)) {
|
|---|
| 307 | case FAT_DENTRY_SKIP:
|
|---|
| 308 | case FAT_DENTRY_FREE:
|
|---|
| 309 | continue;
|
|---|
| 310 | case FAT_DENTRY_LAST:
|
|---|
| 311 | rc = block_put(b);
|
|---|
| 312 | /* expect EOK as b was not dirty */
|
|---|
| 313 | assert(rc == EOK);
|
|---|
| 314 | fibril_mutex_unlock(&parentp->idx->lock);
|
|---|
| 315 | *rfn = NULL;
|
|---|
| 316 | return EOK;
|
|---|
| 317 | default:
|
|---|
| 318 | case FAT_DENTRY_VALID:
|
|---|
| 319 | fat_dentry_name_get(d, name);
|
|---|
| 320 | break;
|
|---|
| 321 | }
|
|---|
| 322 | if (fat_dentry_namecmp(name, component) == 0) {
|
|---|
| 323 | /* hit */
|
|---|
| 324 | fat_node_t *nodep;
|
|---|
| 325 | /*
|
|---|
| 326 | * Assume tree hierarchy for locking. We
|
|---|
| 327 | * already have the parent and now we are going
|
|---|
| 328 | * to lock the child. Never lock in the oposite
|
|---|
| 329 | * order.
|
|---|
| 330 | */
|
|---|
| 331 | fat_idx_t *idx = fat_idx_get_by_pos(
|
|---|
| 332 | parentp->idx->dev_handle, parentp->firstc,
|
|---|
| 333 | i * dps + j);
|
|---|
| 334 | fibril_mutex_unlock(&parentp->idx->lock);
|
|---|
| 335 | if (!idx) {
|
|---|
| 336 | /*
|
|---|
| 337 | * Can happen if memory is low or if we
|
|---|
| 338 | * run out of 32-bit indices.
|
|---|
| 339 | */
|
|---|
| 340 | rc = block_put(b);
|
|---|
| 341 | /* expect EOK as b was not dirty */
|
|---|
| 342 | assert(rc == EOK);
|
|---|
| 343 | return ENOMEM;
|
|---|
| 344 | }
|
|---|
| 345 | nodep = fat_node_get_core(idx);
|
|---|
| 346 | fibril_mutex_unlock(&idx->lock);
|
|---|
| 347 | rc = block_put(b);
|
|---|
| 348 | /* expect EOK as b was not dirty */
|
|---|
| 349 | assert(rc == EOK);
|
|---|
| 350 | *rfn = FS_NODE(nodep);
|
|---|
| 351 | return EOK;
|
|---|
| 352 | }
|
|---|
| 353 | }
|
|---|
| 354 | rc = block_put(b);
|
|---|
| 355 | assert(rc == EOK); /* expect EOK as b was not dirty */
|
|---|
| 356 | }
|
|---|
| 357 |
|
|---|
| 358 | fibril_mutex_unlock(&parentp->idx->lock);
|
|---|
| 359 | *rfn = NULL;
|
|---|
| 360 | return EOK;
|
|---|
| 361 | }
|
|---|
| 362 |
|
|---|
| [add5835] | 363 | /** Instantiate a FAT in-core node. */
|
|---|
| [073f550] | 364 | int fat_node_get(fs_node_t **rfn, dev_handle_t dev_handle, fs_index_t index)
|
|---|
| [add5835] | 365 | {
|
|---|
| [b6035ba] | 366 | fat_node_t *nodep;
|
|---|
| [add5835] | 367 | fat_idx_t *idxp;
|
|---|
| 368 |
|
|---|
| 369 | idxp = fat_idx_get_by_index(dev_handle, index);
|
|---|
| [073f550] | 370 | if (!idxp) {
|
|---|
| 371 | *rfn = NULL;
|
|---|
| 372 | return EOK;
|
|---|
| 373 | }
|
|---|
| [add5835] | 374 | /* idxp->lock held */
|
|---|
| [b6035ba] | 375 | nodep = fat_node_get_core(idxp);
|
|---|
| [6ebe721] | 376 | fibril_mutex_unlock(&idxp->lock);
|
|---|
| [073f550] | 377 | *rfn = FS_NODE(nodep);
|
|---|
| 378 | return EOK;
|
|---|
| [add5835] | 379 | }
|
|---|
| 380 |
|
|---|
| [073f550] | 381 | int fat_node_put(fs_node_t *fn)
|
|---|
| [06901c6b] | 382 | {
|
|---|
| [b6035ba] | 383 | fat_node_t *nodep = FAT_NODE(fn);
|
|---|
| [6571b78] | 384 | bool destroy = false;
|
|---|
| [34b3ce3] | 385 |
|
|---|
| [6ebe721] | 386 | fibril_mutex_lock(&nodep->lock);
|
|---|
| [34b3ce3] | 387 | if (!--nodep->refcnt) {
|
|---|
| [6571b78] | 388 | if (nodep->idx) {
|
|---|
| [6ebe721] | 389 | fibril_mutex_lock(&ffn_mutex);
|
|---|
| [6571b78] | 390 | list_append(&nodep->ffn_link, &ffn_head);
|
|---|
| [6ebe721] | 391 | fibril_mutex_unlock(&ffn_mutex);
|
|---|
| [6571b78] | 392 | } else {
|
|---|
| 393 | /*
|
|---|
| 394 | * The node does not have any index structure associated
|
|---|
| 395 | * with itself. This can only mean that we are releasing
|
|---|
| 396 | * the node after a failed attempt to allocate the index
|
|---|
| 397 | * structure for it.
|
|---|
| 398 | */
|
|---|
| 399 | destroy = true;
|
|---|
| 400 | }
|
|---|
| [34b3ce3] | 401 | }
|
|---|
| [6ebe721] | 402 | fibril_mutex_unlock(&nodep->lock);
|
|---|
| [b6035ba] | 403 | if (destroy) {
|
|---|
| 404 | free(nodep->bp);
|
|---|
| 405 | free(nodep);
|
|---|
| 406 | }
|
|---|
| [073f550] | 407 | return EOK;
|
|---|
| [06901c6b] | 408 | }
|
|---|
| 409 |
|
|---|
| [073f550] | 410 | int fat_create_node(fs_node_t **rfn, dev_handle_t dev_handle, int flags)
|
|---|
| [80e8482] | 411 | {
|
|---|
| [6571b78] | 412 | fat_idx_t *idxp;
|
|---|
| 413 | fat_node_t *nodep;
|
|---|
| [49df572] | 414 | fat_bs_t *bs;
|
|---|
| 415 | fat_cluster_t mcl, lcl;
|
|---|
| 416 | uint16_t bps;
|
|---|
| 417 | int rc;
|
|---|
| 418 |
|
|---|
| 419 | bs = block_bb_get(dev_handle);
|
|---|
| 420 | bps = uint16_t_le2host(bs->bps);
|
|---|
| 421 | if (flags & L_DIRECTORY) {
|
|---|
| 422 | /* allocate a cluster */
|
|---|
| 423 | rc = fat_alloc_clusters(bs, dev_handle, 1, &mcl, &lcl);
|
|---|
| [073f550] | 424 | if (rc != EOK)
|
|---|
| 425 | return rc;
|
|---|
| 426 | /* populate the new cluster with unused dentries */
|
|---|
| 427 | rc = fat_zero_cluster(bs, dev_handle, mcl);
|
|---|
| 428 | if (rc != EOK) {
|
|---|
| 429 | (void) fat_free_clusters(bs, dev_handle, mcl);
|
|---|
| 430 | return rc;
|
|---|
| 431 | }
|
|---|
| [49df572] | 432 | }
|
|---|
| [6571b78] | 433 |
|
|---|
| 434 | nodep = fat_node_get_new();
|
|---|
| [49df572] | 435 | if (!nodep) {
|
|---|
| [cca29e3c] | 436 | (void) fat_free_clusters(bs, dev_handle, mcl);
|
|---|
| [073f550] | 437 | return ENOMEM; /* FIXME: determine the true error code */
|
|---|
| [49df572] | 438 | }
|
|---|
| [6571b78] | 439 | idxp = fat_idx_get_new(dev_handle);
|
|---|
| 440 | if (!idxp) {
|
|---|
| [cca29e3c] | 441 | (void) fat_free_clusters(bs, dev_handle, mcl);
|
|---|
| [073f550] | 442 | (void) fat_node_put(FS_NODE(nodep));
|
|---|
| 443 | return ENOMEM; /* FIXME: determine the true error code */
|
|---|
| [6571b78] | 444 | }
|
|---|
| 445 | /* idxp->lock held */
|
|---|
| 446 | if (flags & L_DIRECTORY) {
|
|---|
| 447 | nodep->type = FAT_DIRECTORY;
|
|---|
| [49df572] | 448 | nodep->firstc = mcl;
|
|---|
| 449 | nodep->size = bps * bs->spc;
|
|---|
| [6571b78] | 450 | } else {
|
|---|
| 451 | nodep->type = FAT_FILE;
|
|---|
| [49df572] | 452 | nodep->firstc = FAT_CLST_RES0;
|
|---|
| 453 | nodep->size = 0;
|
|---|
| [6571b78] | 454 | }
|
|---|
| 455 | nodep->lnkcnt = 0; /* not linked anywhere */
|
|---|
| 456 | nodep->refcnt = 1;
|
|---|
| [49df572] | 457 | nodep->dirty = true;
|
|---|
| [6571b78] | 458 |
|
|---|
| 459 | nodep->idx = idxp;
|
|---|
| 460 | idxp->nodep = nodep;
|
|---|
| 461 |
|
|---|
| [6ebe721] | 462 | fibril_mutex_unlock(&idxp->lock);
|
|---|
| [073f550] | 463 | *rfn = FS_NODE(nodep);
|
|---|
| 464 | return EOK;
|
|---|
| [80e8482] | 465 | }
|
|---|
| 466 |
|
|---|
| [b6035ba] | 467 | int fat_destroy_node(fs_node_t *fn)
|
|---|
| [80e8482] | 468 | {
|
|---|
| [b6035ba] | 469 | fat_node_t *nodep = FAT_NODE(fn);
|
|---|
| [50e5b25] | 470 | fat_bs_t *bs;
|
|---|
| [073f550] | 471 | bool has_children;
|
|---|
| 472 | int rc;
|
|---|
| [50e5b25] | 473 |
|
|---|
| 474 | /*
|
|---|
| 475 | * The node is not reachable from the file system. This means that the
|
|---|
| 476 | * link count should be zero and that the index structure cannot be
|
|---|
| 477 | * found in the position hash. Obviously, we don't need to lock the node
|
|---|
| 478 | * nor its index structure.
|
|---|
| 479 | */
|
|---|
| 480 | assert(nodep->lnkcnt == 0);
|
|---|
| 481 |
|
|---|
| 482 | /*
|
|---|
| 483 | * The node may not have any children.
|
|---|
| 484 | */
|
|---|
| [073f550] | 485 | rc = fat_has_children(&has_children, fn);
|
|---|
| 486 | if (rc != EOK)
|
|---|
| 487 | return rc;
|
|---|
| 488 | assert(!has_children);
|
|---|
| [50e5b25] | 489 |
|
|---|
| 490 | bs = block_bb_get(nodep->idx->dev_handle);
|
|---|
| 491 | if (nodep->firstc != FAT_CLST_RES0) {
|
|---|
| 492 | assert(nodep->size);
|
|---|
| 493 | /* Free all clusters allocated to the node. */
|
|---|
| [cca29e3c] | 494 | rc = fat_free_clusters(bs, nodep->idx->dev_handle,
|
|---|
| 495 | nodep->firstc);
|
|---|
| [50e5b25] | 496 | }
|
|---|
| 497 |
|
|---|
| 498 | fat_idx_destroy(nodep->idx);
|
|---|
| [b6035ba] | 499 | free(nodep->bp);
|
|---|
| [50e5b25] | 500 | free(nodep);
|
|---|
| [cca29e3c] | 501 | return rc;
|
|---|
| [80e8482] | 502 | }
|
|---|
| 503 |
|
|---|
| [b6035ba] | 504 | int fat_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
|
|---|
| [80e8482] | 505 | {
|
|---|
| [b6035ba] | 506 | fat_node_t *parentp = FAT_NODE(pfn);
|
|---|
| 507 | fat_node_t *childp = FAT_NODE(cfn);
|
|---|
| [0fdd6bb] | 508 | fat_dentry_t *d;
|
|---|
| 509 | fat_bs_t *bs;
|
|---|
| 510 | block_t *b;
|
|---|
| [a405563] | 511 | unsigned i, j;
|
|---|
| [0fdd6bb] | 512 | uint16_t bps;
|
|---|
| 513 | unsigned dps;
|
|---|
| 514 | unsigned blocks;
|
|---|
| [e32b65a] | 515 | fat_cluster_t mcl, lcl;
|
|---|
| 516 | int rc;
|
|---|
| [0fdd6bb] | 517 |
|
|---|
| [6ebe721] | 518 | fibril_mutex_lock(&childp->lock);
|
|---|
| [0fdd6bb] | 519 | if (childp->lnkcnt == 1) {
|
|---|
| 520 | /*
|
|---|
| 521 | * On FAT, we don't support multiple hard links.
|
|---|
| 522 | */
|
|---|
| [6ebe721] | 523 | fibril_mutex_unlock(&childp->lock);
|
|---|
| [0fdd6bb] | 524 | return EMLINK;
|
|---|
| 525 | }
|
|---|
| 526 | assert(childp->lnkcnt == 0);
|
|---|
| [6ebe721] | 527 | fibril_mutex_unlock(&childp->lock);
|
|---|
| [0fdd6bb] | 528 |
|
|---|
| 529 | if (!fat_dentry_name_verify(name)) {
|
|---|
| 530 | /*
|
|---|
| 531 | * Attempt to create unsupported name.
|
|---|
| 532 | */
|
|---|
| 533 | return ENOTSUP;
|
|---|
| 534 | }
|
|---|
| 535 |
|
|---|
| 536 | /*
|
|---|
| 537 | * Get us an unused parent node's dentry or grow the parent and allocate
|
|---|
| 538 | * a new one.
|
|---|
| 539 | */
|
|---|
| 540 |
|
|---|
| [6ebe721] | 541 | fibril_mutex_lock(&parentp->idx->lock);
|
|---|
| [0fdd6bb] | 542 | bs = block_bb_get(parentp->idx->dev_handle);
|
|---|
| 543 | bps = uint16_t_le2host(bs->bps);
|
|---|
| 544 | dps = bps / sizeof(fat_dentry_t);
|
|---|
| 545 |
|
|---|
| 546 | blocks = parentp->size / bps;
|
|---|
| 547 |
|
|---|
| 548 | for (i = 0; i < blocks; i++) {
|
|---|
| [684b655] | 549 | rc = fat_block_get(&b, bs, parentp, i, BLOCK_FLAGS_NONE);
|
|---|
| [4b4668e] | 550 | if (rc != EOK) {
|
|---|
| 551 | fibril_mutex_unlock(&parentp->idx->lock);
|
|---|
| 552 | return rc;
|
|---|
| 553 | }
|
|---|
| [0fdd6bb] | 554 | for (j = 0; j < dps; j++) {
|
|---|
| 555 | d = ((fat_dentry_t *)b->data) + j;
|
|---|
| 556 | switch (fat_classify_dentry(d)) {
|
|---|
| 557 | case FAT_DENTRY_SKIP:
|
|---|
| 558 | case FAT_DENTRY_VALID:
|
|---|
| 559 | /* skipping used and meta entries */
|
|---|
| 560 | continue;
|
|---|
| 561 | case FAT_DENTRY_FREE:
|
|---|
| 562 | case FAT_DENTRY_LAST:
|
|---|
| 563 | /* found an empty slot */
|
|---|
| 564 | goto hit;
|
|---|
| 565 | }
|
|---|
| 566 | }
|
|---|
| [c91f2d1b] | 567 | rc = block_put(b);
|
|---|
| [4b4668e] | 568 | if (rc != EOK) {
|
|---|
| 569 | fibril_mutex_unlock(&parentp->idx->lock);
|
|---|
| 570 | return rc;
|
|---|
| 571 | }
|
|---|
| [0fdd6bb] | 572 | }
|
|---|
| [699743c] | 573 | j = 0;
|
|---|
| [0fdd6bb] | 574 |
|
|---|
| 575 | /*
|
|---|
| 576 | * We need to grow the parent in order to create a new unused dentry.
|
|---|
| 577 | */
|
|---|
| [b713492b] | 578 | if (parentp->firstc == FAT_CLST_ROOT) {
|
|---|
| [e32b65a] | 579 | /* Can't grow the root directory. */
|
|---|
| [6ebe721] | 580 | fibril_mutex_unlock(&parentp->idx->lock);
|
|---|
| [e32b65a] | 581 | return ENOSPC;
|
|---|
| 582 | }
|
|---|
| 583 | rc = fat_alloc_clusters(bs, parentp->idx->dev_handle, 1, &mcl, &lcl);
|
|---|
| 584 | if (rc != EOK) {
|
|---|
| [6ebe721] | 585 | fibril_mutex_unlock(&parentp->idx->lock);
|
|---|
| [e32b65a] | 586 | return rc;
|
|---|
| 587 | }
|
|---|
| [cca29e3c] | 588 | rc = fat_zero_cluster(bs, parentp->idx->dev_handle, mcl);
|
|---|
| [4b4668e] | 589 | if (rc != EOK) {
|
|---|
| [073f550] | 590 | (void) fat_free_clusters(bs, parentp->idx->dev_handle, mcl);
|
|---|
| [4b4668e] | 591 | fibril_mutex_unlock(&parentp->idx->lock);
|
|---|
| 592 | return rc;
|
|---|
| 593 | }
|
|---|
| [cca29e3c] | 594 | rc = fat_append_clusters(bs, parentp, mcl);
|
|---|
| [4b4668e] | 595 | if (rc != EOK) {
|
|---|
| [073f550] | 596 | (void) fat_free_clusters(bs, parentp->idx->dev_handle, mcl);
|
|---|
| [4b4668e] | 597 | fibril_mutex_unlock(&parentp->idx->lock);
|
|---|
| 598 | return rc;
|
|---|
| 599 | }
|
|---|
| [d44aabd] | 600 | parentp->size += bps * bs->spc;
|
|---|
| 601 | parentp->dirty = true; /* need to sync node */
|
|---|
| [684b655] | 602 | rc = fat_block_get(&b, bs, parentp, i, BLOCK_FLAGS_NONE);
|
|---|
| [4b4668e] | 603 | if (rc != EOK) {
|
|---|
| 604 | fibril_mutex_unlock(&parentp->idx->lock);
|
|---|
| 605 | return rc;
|
|---|
| 606 | }
|
|---|
| [e32b65a] | 607 | d = (fat_dentry_t *)b->data;
|
|---|
| [0fdd6bb] | 608 |
|
|---|
| 609 | hit:
|
|---|
| 610 | /*
|
|---|
| 611 | * At this point we only establish the link between the parent and the
|
|---|
| 612 | * child. The dentry, except of the name and the extension, will remain
|
|---|
| [e32b65a] | 613 | * uninitialized until the corresponding node is synced. Thus the valid
|
|---|
| 614 | * dentry data is kept in the child node structure.
|
|---|
| [0fdd6bb] | 615 | */
|
|---|
| 616 | memset(d, 0, sizeof(fat_dentry_t));
|
|---|
| 617 | fat_dentry_name_set(d, name);
|
|---|
| 618 | b->dirty = true; /* need to sync block */
|
|---|
| [c91f2d1b] | 619 | rc = block_put(b);
|
|---|
| [6ebe721] | 620 | fibril_mutex_unlock(&parentp->idx->lock);
|
|---|
| [4b4668e] | 621 | if (rc != EOK)
|
|---|
| 622 | return rc;
|
|---|
| [0fdd6bb] | 623 |
|
|---|
| [6ebe721] | 624 | fibril_mutex_lock(&childp->idx->lock);
|
|---|
| [1baec4b] | 625 |
|
|---|
| 626 | /*
|
|---|
| 627 | * If possible, create the Sub-directory Identifier Entry and the
|
|---|
| 628 | * Sub-directory Parent Pointer Entry (i.e. "." and ".."). These entries
|
|---|
| 629 | * are not mandatory according to Standard ECMA-107 and HelenOS VFS does
|
|---|
| 630 | * not use them anyway, so this is rather a sign of our good will.
|
|---|
| 631 | */
|
|---|
| [684b655] | 632 | rc = fat_block_get(&b, bs, childp, 0, BLOCK_FLAGS_NONE);
|
|---|
| [4b4668e] | 633 | if (rc != EOK) {
|
|---|
| 634 | /*
|
|---|
| 635 | * Rather than returning an error, simply skip the creation of
|
|---|
| 636 | * these two entries.
|
|---|
| 637 | */
|
|---|
| 638 | goto skip_dots;
|
|---|
| 639 | }
|
|---|
| [1baec4b] | 640 | d = (fat_dentry_t *)b->data;
|
|---|
| 641 | if (fat_classify_dentry(d) == FAT_DENTRY_LAST ||
|
|---|
| [92fd52d7] | 642 | str_cmp(d->name, FAT_NAME_DOT) == 0) {
|
|---|
| [1baec4b] | 643 | memset(d, 0, sizeof(fat_dentry_t));
|
|---|
| [6eb2e96] | 644 | str_cpy(d->name, 8, FAT_NAME_DOT);
|
|---|
| 645 | str_cpy(d->ext, 3, FAT_EXT_PAD);
|
|---|
| [1baec4b] | 646 | d->attr = FAT_ATTR_SUBDIR;
|
|---|
| 647 | d->firstc = host2uint16_t_le(childp->firstc);
|
|---|
| 648 | /* TODO: initialize also the date/time members. */
|
|---|
| 649 | }
|
|---|
| 650 | d++;
|
|---|
| 651 | if (fat_classify_dentry(d) == FAT_DENTRY_LAST ||
|
|---|
| [92fd52d7] | 652 | str_cmp(d->name, FAT_NAME_DOT_DOT) == 0) {
|
|---|
| [1baec4b] | 653 | memset(d, 0, sizeof(fat_dentry_t));
|
|---|
| [6eb2e96] | 654 | str_cpy(d->name, 8, FAT_NAME_DOT_DOT);
|
|---|
| 655 | str_cpy(d->ext, 3, FAT_EXT_PAD);
|
|---|
| [1baec4b] | 656 | d->attr = FAT_ATTR_SUBDIR;
|
|---|
| 657 | d->firstc = (parentp->firstc == FAT_CLST_ROOT) ?
|
|---|
| 658 | host2uint16_t_le(FAT_CLST_RES0) :
|
|---|
| 659 | host2uint16_t_le(parentp->firstc);
|
|---|
| 660 | /* TODO: initialize also the date/time members. */
|
|---|
| 661 | }
|
|---|
| 662 | b->dirty = true; /* need to sync block */
|
|---|
| [4b4668e] | 663 | /*
|
|---|
| 664 | * Ignore the return value as we would have fallen through on error
|
|---|
| 665 | * anyway.
|
|---|
| 666 | */
|
|---|
| 667 | (void) block_put(b);
|
|---|
| 668 | skip_dots:
|
|---|
| [1baec4b] | 669 |
|
|---|
| [0fdd6bb] | 670 | childp->idx->pfc = parentp->firstc;
|
|---|
| 671 | childp->idx->pdi = i * dps + j;
|
|---|
| [6ebe721] | 672 | fibril_mutex_unlock(&childp->idx->lock);
|
|---|
| [0fdd6bb] | 673 |
|
|---|
| [6ebe721] | 674 | fibril_mutex_lock(&childp->lock);
|
|---|
| [0fdd6bb] | 675 | childp->lnkcnt = 1;
|
|---|
| 676 | childp->dirty = true; /* need to sync node */
|
|---|
| [6ebe721] | 677 | fibril_mutex_unlock(&childp->lock);
|
|---|
| [0fdd6bb] | 678 |
|
|---|
| 679 | /*
|
|---|
| 680 | * Hash in the index structure into the position hash.
|
|---|
| 681 | */
|
|---|
| 682 | fat_idx_hashin(childp->idx);
|
|---|
| 683 |
|
|---|
| 684 | return EOK;
|
|---|
| [80e8482] | 685 | }
|
|---|
| 686 |
|
|---|
| [cf95bc0] | 687 | int fat_unlink(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
|
|---|
| [80e8482] | 688 | {
|
|---|
| [b6035ba] | 689 | fat_node_t *parentp = FAT_NODE(pfn);
|
|---|
| 690 | fat_node_t *childp = FAT_NODE(cfn);
|
|---|
| [a31c1ccf] | 691 | fat_bs_t *bs;
|
|---|
| 692 | fat_dentry_t *d;
|
|---|
| 693 | uint16_t bps;
|
|---|
| 694 | block_t *b;
|
|---|
| [073f550] | 695 | bool has_children;
|
|---|
| [c91f2d1b] | 696 | int rc;
|
|---|
| [a31c1ccf] | 697 |
|
|---|
| [770d281] | 698 | if (!parentp)
|
|---|
| 699 | return EBUSY;
|
|---|
| [0be3e8b] | 700 |
|
|---|
| [073f550] | 701 | rc = fat_has_children(&has_children, cfn);
|
|---|
| 702 | if (rc != EOK)
|
|---|
| 703 | return rc;
|
|---|
| 704 | if (has_children)
|
|---|
| [0be3e8b] | 705 | return ENOTEMPTY;
|
|---|
| [770d281] | 706 |
|
|---|
| [6ebe721] | 707 | fibril_mutex_lock(&parentp->lock);
|
|---|
| 708 | fibril_mutex_lock(&childp->lock);
|
|---|
| [a31c1ccf] | 709 | assert(childp->lnkcnt == 1);
|
|---|
| [6ebe721] | 710 | fibril_mutex_lock(&childp->idx->lock);
|
|---|
| [a31c1ccf] | 711 | bs = block_bb_get(childp->idx->dev_handle);
|
|---|
| 712 | bps = uint16_t_le2host(bs->bps);
|
|---|
| 713 |
|
|---|
| [684b655] | 714 | rc = _fat_block_get(&b, bs, childp->idx->dev_handle, childp->idx->pfc,
|
|---|
| [a31c1ccf] | 715 | (childp->idx->pdi * sizeof(fat_dentry_t)) / bps,
|
|---|
| 716 | BLOCK_FLAGS_NONE);
|
|---|
| [46c0498] | 717 | if (rc != EOK)
|
|---|
| 718 | goto error;
|
|---|
| [a31c1ccf] | 719 | d = (fat_dentry_t *)b->data +
|
|---|
| 720 | (childp->idx->pdi % (bps / sizeof(fat_dentry_t)));
|
|---|
| 721 | /* mark the dentry as not-currently-used */
|
|---|
| 722 | d->name[0] = FAT_DENTRY_ERASED;
|
|---|
| 723 | b->dirty = true; /* need to sync block */
|
|---|
| [c91f2d1b] | 724 | rc = block_put(b);
|
|---|
| [46c0498] | 725 | if (rc != EOK)
|
|---|
| 726 | goto error;
|
|---|
| [a31c1ccf] | 727 |
|
|---|
| 728 | /* remove the index structure from the position hash */
|
|---|
| 729 | fat_idx_hashout(childp->idx);
|
|---|
| 730 | /* clear position information */
|
|---|
| 731 | childp->idx->pfc = FAT_CLST_RES0;
|
|---|
| 732 | childp->idx->pdi = 0;
|
|---|
| [6ebe721] | 733 | fibril_mutex_unlock(&childp->idx->lock);
|
|---|
| [a31c1ccf] | 734 | childp->lnkcnt = 0;
|
|---|
| 735 | childp->dirty = true;
|
|---|
| [6ebe721] | 736 | fibril_mutex_unlock(&childp->lock);
|
|---|
| 737 | fibril_mutex_unlock(&parentp->lock);
|
|---|
| [a31c1ccf] | 738 |
|
|---|
| 739 | return EOK;
|
|---|
| [46c0498] | 740 |
|
|---|
| 741 | error:
|
|---|
| 742 | fibril_mutex_unlock(&parentp->idx->lock);
|
|---|
| 743 | fibril_mutex_unlock(&childp->lock);
|
|---|
| 744 | fibril_mutex_unlock(&childp->idx->lock);
|
|---|
| 745 | return rc;
|
|---|
| [80e8482] | 746 | }
|
|---|
| 747 |
|
|---|
| [073f550] | 748 | int fat_has_children(bool *has_children, fs_node_t *fn)
|
|---|
| [32fb10ed] | 749 | {
|
|---|
| [7858bc5f] | 750 | fat_bs_t *bs;
|
|---|
| [b6035ba] | 751 | fat_node_t *nodep = FAT_NODE(fn);
|
|---|
| [32fb10ed] | 752 | unsigned bps;
|
|---|
| 753 | unsigned dps;
|
|---|
| 754 | unsigned blocks;
|
|---|
| [7858bc5f] | 755 | block_t *b;
|
|---|
| [32fb10ed] | 756 | unsigned i, j;
|
|---|
| [c91f2d1b] | 757 | int rc;
|
|---|
| [32fb10ed] | 758 |
|
|---|
| [073f550] | 759 | if (nodep->type != FAT_DIRECTORY) {
|
|---|
| 760 | *has_children = false;
|
|---|
| 761 | return EOK;
|
|---|
| 762 | }
|
|---|
| [b0247bac] | 763 |
|
|---|
| [6ebe721] | 764 | fibril_mutex_lock(&nodep->idx->lock);
|
|---|
| [7858bc5f] | 765 | bs = block_bb_get(nodep->idx->dev_handle);
|
|---|
| 766 | bps = uint16_t_le2host(bs->bps);
|
|---|
| [32fb10ed] | 767 | dps = bps / sizeof(fat_dentry_t);
|
|---|
| 768 |
|
|---|
| [b0247bac] | 769 | blocks = nodep->size / bps;
|
|---|
| [32fb10ed] | 770 |
|
|---|
| 771 | for (i = 0; i < blocks; i++) {
|
|---|
| 772 | fat_dentry_t *d;
|
|---|
| 773 |
|
|---|
| [684b655] | 774 | rc = fat_block_get(&b, bs, nodep, i, BLOCK_FLAGS_NONE);
|
|---|
| [073f550] | 775 | if (rc != EOK) {
|
|---|
| 776 | fibril_mutex_unlock(&nodep->idx->lock);
|
|---|
| 777 | return rc;
|
|---|
| 778 | }
|
|---|
| [b0247bac] | 779 | for (j = 0; j < dps; j++) {
|
|---|
| [32fb10ed] | 780 | d = ((fat_dentry_t *)b->data) + j;
|
|---|
| 781 | switch (fat_classify_dentry(d)) {
|
|---|
| 782 | case FAT_DENTRY_SKIP:
|
|---|
| [0fdd6bb] | 783 | case FAT_DENTRY_FREE:
|
|---|
| [32fb10ed] | 784 | continue;
|
|---|
| 785 | case FAT_DENTRY_LAST:
|
|---|
| [c91f2d1b] | 786 | rc = block_put(b);
|
|---|
| [073f550] | 787 | /* expect EOK as b was not dirty */
|
|---|
| [c91f2d1b] | 788 | assert(rc == EOK);
|
|---|
| [6ebe721] | 789 | fibril_mutex_unlock(&nodep->idx->lock);
|
|---|
| [073f550] | 790 | *has_children = false;
|
|---|
| 791 | return EOK;
|
|---|
| [32fb10ed] | 792 | default:
|
|---|
| 793 | case FAT_DENTRY_VALID:
|
|---|
| [c91f2d1b] | 794 | rc = block_put(b);
|
|---|
| [073f550] | 795 | /* expect EOK as b was not dirty */
|
|---|
| [c91f2d1b] | 796 | assert(rc == EOK);
|
|---|
| [6ebe721] | 797 | fibril_mutex_unlock(&nodep->idx->lock);
|
|---|
| [073f550] | 798 | *has_children = true;
|
|---|
| 799 | return EOK;
|
|---|
| [32fb10ed] | 800 | }
|
|---|
| 801 | }
|
|---|
| [c91f2d1b] | 802 | rc = block_put(b);
|
|---|
| [073f550] | 803 | assert(rc == EOK); /* expect EOK as b was not dirty */
|
|---|
| [32fb10ed] | 804 | }
|
|---|
| 805 |
|
|---|
| [6ebe721] | 806 | fibril_mutex_unlock(&nodep->idx->lock);
|
|---|
| [073f550] | 807 | *has_children = false;
|
|---|
| 808 | return EOK;
|
|---|
| 809 | }
|
|---|
| 810 |
|
|---|
| 811 |
|
|---|
| 812 | fs_index_t fat_index_get(fs_node_t *fn)
|
|---|
| 813 | {
|
|---|
| 814 | return FAT_NODE(fn)->idx->index;
|
|---|
| 815 | }
|
|---|
| 816 |
|
|---|
| 817 | size_t fat_size_get(fs_node_t *fn)
|
|---|
| 818 | {
|
|---|
| 819 | return FAT_NODE(fn)->size;
|
|---|
| [32fb10ed] | 820 | }
|
|---|
| 821 |
|
|---|
| [073f550] | 822 | unsigned fat_lnkcnt_get(fs_node_t *fn)
|
|---|
| [74ea3c6] | 823 | {
|
|---|
| [073f550] | 824 | return FAT_NODE(fn)->lnkcnt;
|
|---|
| [74ea3c6] | 825 | }
|
|---|
| 826 |
|
|---|
| [50e5b25] | 827 | char fat_plb_get_char(unsigned pos)
|
|---|
| [74ea3c6] | 828 | {
|
|---|
| 829 | return fat_reg.plb_ro[pos % PLB_SIZE];
|
|---|
| 830 | }
|
|---|
| 831 |
|
|---|
| [b6035ba] | 832 | bool fat_is_directory(fs_node_t *fn)
|
|---|
| [e1e3b26] | 833 | {
|
|---|
| [b6035ba] | 834 | return FAT_NODE(fn)->type == FAT_DIRECTORY;
|
|---|
| [e1e3b26] | 835 | }
|
|---|
| 836 |
|
|---|
| [b6035ba] | 837 | bool fat_is_file(fs_node_t *fn)
|
|---|
| [e1e3b26] | 838 | {
|
|---|
| [b6035ba] | 839 | return FAT_NODE(fn)->type == FAT_FILE;
|
|---|
| [e1e3b26] | 840 | }
|
|---|
| 841 |
|
|---|
| [a2aa1dec] | 842 | /** libfs operations */
|
|---|
| 843 | libfs_ops_t fat_libfs_ops = {
|
|---|
| [073f550] | 844 | .root_get = fat_root_get,
|
|---|
| [a2aa1dec] | 845 | .match = fat_match,
|
|---|
| 846 | .node_get = fat_node_get,
|
|---|
| [06901c6b] | 847 | .node_put = fat_node_put,
|
|---|
| [6571b78] | 848 | .create = fat_create_node,
|
|---|
| 849 | .destroy = fat_destroy_node,
|
|---|
| [80e8482] | 850 | .link = fat_link,
|
|---|
| 851 | .unlink = fat_unlink,
|
|---|
| [073f550] | 852 | .has_children = fat_has_children,
|
|---|
| [e1e3b26] | 853 | .index_get = fat_index_get,
|
|---|
| 854 | .size_get = fat_size_get,
|
|---|
| 855 | .lnkcnt_get = fat_lnkcnt_get,
|
|---|
| [74ea3c6] | 856 | .plb_get_char = fat_plb_get_char,
|
|---|
| [e1e3b26] | 857 | .is_directory = fat_is_directory,
|
|---|
| 858 | .is_file = fat_is_file
|
|---|
| [a2aa1dec] | 859 | };
|
|---|
| 860 |
|
|---|
| [0013b9ce] | 861 | /*
|
|---|
| 862 | * VFS operations.
|
|---|
| 863 | */
|
|---|
| 864 |
|
|---|
| [cde485d] | 865 | void fat_mounted(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 866 | {
|
|---|
| 867 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
|
|---|
| [1fbe064b] | 868 | enum cache_mode cmode;
|
|---|
| [7858bc5f] | 869 | fat_bs_t *bs;
|
|---|
| [7a35204a] | 870 | uint16_t bps;
|
|---|
| [689f036] | 871 | uint16_t rde;
|
|---|
| [cde485d] | 872 | int rc;
|
|---|
| 873 |
|
|---|
| [594303b] | 874 | /* accept the mount options */
|
|---|
| 875 | ipc_callid_t callid;
|
|---|
| 876 | size_t size;
|
|---|
| 877 | if (!ipc_data_write_receive(&callid, &size)) {
|
|---|
| 878 | ipc_answer_0(callid, EINVAL);
|
|---|
| 879 | ipc_answer_0(rid, EINVAL);
|
|---|
| 880 | return;
|
|---|
| 881 | }
|
|---|
| 882 | char *opts = malloc(size + 1);
|
|---|
| 883 | if (!opts) {
|
|---|
| 884 | ipc_answer_0(callid, ENOMEM);
|
|---|
| 885 | ipc_answer_0(rid, ENOMEM);
|
|---|
| 886 | return;
|
|---|
| 887 | }
|
|---|
| 888 | ipcarg_t retval = ipc_data_write_finalize(callid, opts, size);
|
|---|
| 889 | if (retval != EOK) {
|
|---|
| 890 | ipc_answer_0(rid, retval);
|
|---|
| 891 | free(opts);
|
|---|
| 892 | return;
|
|---|
| 893 | }
|
|---|
| 894 | opts[size] = '\0';
|
|---|
| 895 |
|
|---|
| [1fbe064b] | 896 | /* Check for option enabling write through. */
|
|---|
| 897 | if (str_cmp(opts, "wtcache") == 0)
|
|---|
| 898 | cmode = CACHE_MODE_WT;
|
|---|
| 899 | else
|
|---|
| 900 | cmode = CACHE_MODE_WB;
|
|---|
| 901 |
|
|---|
| [7858bc5f] | 902 | /* initialize libblock */
|
|---|
| [6284978] | 903 | rc = block_init(dev_handle, BS_SIZE);
|
|---|
| [7a35204a] | 904 | if (rc != EOK) {
|
|---|
| [6284978] | 905 | ipc_answer_0(rid, rc);
|
|---|
| 906 | return;
|
|---|
| 907 | }
|
|---|
| 908 |
|
|---|
| 909 | /* prepare the boot block */
|
|---|
| [1ee00b7] | 910 | rc = block_bb_read(dev_handle, BS_BLOCK);
|
|---|
| [6284978] | 911 | if (rc != EOK) {
|
|---|
| 912 | block_fini(dev_handle);
|
|---|
| 913 | ipc_answer_0(rid, rc);
|
|---|
| [7a35204a] | 914 | return;
|
|---|
| 915 | }
|
|---|
| 916 |
|
|---|
| [7858bc5f] | 917 | /* get the buffer with the boot sector */
|
|---|
| 918 | bs = block_bb_get(dev_handle);
|
|---|
| 919 |
|
|---|
| [689f036] | 920 | /* Read the number of root directory entries. */
|
|---|
| [7858bc5f] | 921 | bps = uint16_t_le2host(bs->bps);
|
|---|
| 922 | rde = uint16_t_le2host(bs->root_ent_max);
|
|---|
| [689f036] | 923 |
|
|---|
| [7a35204a] | 924 | if (bps != BS_SIZE) {
|
|---|
| [7858bc5f] | 925 | block_fini(dev_handle);
|
|---|
| [7a35204a] | 926 | ipc_answer_0(rid, ENOTSUP);
|
|---|
| 927 | return;
|
|---|
| 928 | }
|
|---|
| 929 |
|
|---|
| [f1ba5d6] | 930 | /* Initialize the block cache */
|
|---|
| [1fbe064b] | 931 | rc = block_cache_init(dev_handle, bps, 0 /* XXX */, cmode);
|
|---|
| [f1ba5d6] | 932 | if (rc != EOK) {
|
|---|
| 933 | block_fini(dev_handle);
|
|---|
| 934 | ipc_answer_0(rid, rc);
|
|---|
| 935 | return;
|
|---|
| 936 | }
|
|---|
| 937 |
|
|---|
| [cde485d] | 938 | rc = fat_idx_init_by_dev_handle(dev_handle);
|
|---|
| 939 | if (rc != EOK) {
|
|---|
| [7858bc5f] | 940 | block_fini(dev_handle);
|
|---|
| [cde485d] | 941 | ipc_answer_0(rid, rc);
|
|---|
| 942 | return;
|
|---|
| 943 | }
|
|---|
| 944 |
|
|---|
| [689f036] | 945 | /* Initialize the root node. */
|
|---|
| [b6035ba] | 946 | fs_node_t *rfn = (fs_node_t *)malloc(sizeof(fs_node_t));
|
|---|
| 947 | if (!rfn) {
|
|---|
| 948 | block_fini(dev_handle);
|
|---|
| 949 | fat_idx_fini_by_dev_handle(dev_handle);
|
|---|
| 950 | ipc_answer_0(rid, ENOMEM);
|
|---|
| 951 | return;
|
|---|
| 952 | }
|
|---|
| [83937ccd] | 953 | fs_node_initialize(rfn);
|
|---|
| [689f036] | 954 | fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t));
|
|---|
| 955 | if (!rootp) {
|
|---|
| [b6035ba] | 956 | free(rfn);
|
|---|
| [7858bc5f] | 957 | block_fini(dev_handle);
|
|---|
| [689f036] | 958 | fat_idx_fini_by_dev_handle(dev_handle);
|
|---|
| 959 | ipc_answer_0(rid, ENOMEM);
|
|---|
| 960 | return;
|
|---|
| 961 | }
|
|---|
| 962 | fat_node_initialize(rootp);
|
|---|
| 963 |
|
|---|
| 964 | fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0);
|
|---|
| 965 | if (!ridxp) {
|
|---|
| [b6035ba] | 966 | free(rfn);
|
|---|
| [689f036] | 967 | free(rootp);
|
|---|
| [b6035ba] | 968 | block_fini(dev_handle);
|
|---|
| [689f036] | 969 | fat_idx_fini_by_dev_handle(dev_handle);
|
|---|
| 970 | ipc_answer_0(rid, ENOMEM);
|
|---|
| 971 | return;
|
|---|
| 972 | }
|
|---|
| 973 | assert(ridxp->index == 0);
|
|---|
| 974 | /* ridxp->lock held */
|
|---|
| 975 |
|
|---|
| 976 | rootp->type = FAT_DIRECTORY;
|
|---|
| 977 | rootp->firstc = FAT_CLST_ROOT;
|
|---|
| 978 | rootp->refcnt = 1;
|
|---|
| [5ab597d] | 979 | rootp->lnkcnt = 0; /* FS root is not linked */
|
|---|
| [689f036] | 980 | rootp->size = rde * sizeof(fat_dentry_t);
|
|---|
| 981 | rootp->idx = ridxp;
|
|---|
| 982 | ridxp->nodep = rootp;
|
|---|
| [b6035ba] | 983 | rootp->bp = rfn;
|
|---|
| 984 | rfn->data = rootp;
|
|---|
| [689f036] | 985 |
|
|---|
| [6ebe721] | 986 | fibril_mutex_unlock(&ridxp->lock);
|
|---|
| [689f036] | 987 |
|
|---|
| [5ab597d] | 988 | ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt);
|
|---|
| [cde485d] | 989 | }
|
|---|
| 990 |
|
|---|
| 991 | void fat_mount(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 992 | {
|
|---|
| [16d17ca] | 993 | libfs_mount(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
|
|---|
| [cde485d] | 994 | }
|
|---|
| 995 |
|
|---|
| [be815bc] | 996 | void fat_lookup(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 997 | {
|
|---|
| [a2aa1dec] | 998 | libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
|
|---|
| [be815bc] | 999 | }
|
|---|
| 1000 |
|
|---|
| [4bf40f6] | 1001 | void fat_read(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 1002 | {
|
|---|
| 1003 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
|
|---|
| 1004 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
|---|
| 1005 | off_t pos = (off_t)IPC_GET_ARG3(*request);
|
|---|
| [073f550] | 1006 | fs_node_t *fn;
|
|---|
| [b6035ba] | 1007 | fat_node_t *nodep;
|
|---|
| [7858bc5f] | 1008 | fat_bs_t *bs;
|
|---|
| [cb682eb] | 1009 | uint16_t bps;
|
|---|
| [79d031b] | 1010 | size_t bytes;
|
|---|
| [7858bc5f] | 1011 | block_t *b;
|
|---|
| [c91f2d1b] | 1012 | int rc;
|
|---|
| [79d031b] | 1013 |
|
|---|
| [073f550] | 1014 | rc = fat_node_get(&fn, dev_handle, index);
|
|---|
| 1015 | if (rc != EOK) {
|
|---|
| 1016 | ipc_answer_0(rid, rc);
|
|---|
| 1017 | return;
|
|---|
| 1018 | }
|
|---|
| [b6035ba] | 1019 | if (!fn) {
|
|---|
| [4bf40f6] | 1020 | ipc_answer_0(rid, ENOENT);
|
|---|
| 1021 | return;
|
|---|
| 1022 | }
|
|---|
| [b6035ba] | 1023 | nodep = FAT_NODE(fn);
|
|---|
| [4bf40f6] | 1024 |
|
|---|
| 1025 | ipc_callid_t callid;
|
|---|
| 1026 | size_t len;
|
|---|
| [6808614] | 1027 | if (!ipc_data_read_receive(&callid, &len)) {
|
|---|
| [b6035ba] | 1028 | fat_node_put(fn);
|
|---|
| [4bf40f6] | 1029 | ipc_answer_0(callid, EINVAL);
|
|---|
| 1030 | ipc_answer_0(rid, EINVAL);
|
|---|
| 1031 | return;
|
|---|
| 1032 | }
|
|---|
| 1033 |
|
|---|
| [7858bc5f] | 1034 | bs = block_bb_get(dev_handle);
|
|---|
| 1035 | bps = uint16_t_le2host(bs->bps);
|
|---|
| [cb682eb] | 1036 |
|
|---|
| [4bf40f6] | 1037 | if (nodep->type == FAT_FILE) {
|
|---|
| [ddd1219] | 1038 | /*
|
|---|
| 1039 | * Our strategy for regular file reads is to read one block at
|
|---|
| 1040 | * most and make use of the possibility to return less data than
|
|---|
| 1041 | * requested. This keeps the code very simple.
|
|---|
| 1042 | */
|
|---|
| [0d974d8] | 1043 | if (pos >= nodep->size) {
|
|---|
| [7d861950] | 1044 | /* reading beyond the EOF */
|
|---|
| 1045 | bytes = 0;
|
|---|
| [0d974d8] | 1046 | (void) ipc_data_read_finalize(callid, NULL, 0);
|
|---|
| 1047 | } else {
|
|---|
| 1048 | bytes = min(len, bps - pos % bps);
|
|---|
| 1049 | bytes = min(bytes, nodep->size - pos);
|
|---|
| [684b655] | 1050 | rc = fat_block_get(&b, bs, nodep, pos / bps,
|
|---|
| [1d8cdb1] | 1051 | BLOCK_FLAGS_NONE);
|
|---|
| [684b655] | 1052 | assert(rc == EOK);
|
|---|
| [0d974d8] | 1053 | (void) ipc_data_read_finalize(callid, b->data + pos % bps,
|
|---|
| 1054 | bytes);
|
|---|
| [c91f2d1b] | 1055 | rc = block_put(b);
|
|---|
| 1056 | assert(rc == EOK);
|
|---|
| [0d974d8] | 1057 | }
|
|---|
| [4bf40f6] | 1058 | } else {
|
|---|
| [ddd1219] | 1059 | unsigned bnum;
|
|---|
| 1060 | off_t spos = pos;
|
|---|
| 1061 | char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
|
|---|
| 1062 | fat_dentry_t *d;
|
|---|
| 1063 |
|
|---|
| [4bf40f6] | 1064 | assert(nodep->type == FAT_DIRECTORY);
|
|---|
| [ddd1219] | 1065 | assert(nodep->size % bps == 0);
|
|---|
| 1066 | assert(bps % sizeof(fat_dentry_t) == 0);
|
|---|
| 1067 |
|
|---|
| 1068 | /*
|
|---|
| 1069 | * Our strategy for readdir() is to use the position pointer as
|
|---|
| 1070 | * an index into the array of all dentries. On entry, it points
|
|---|
| 1071 | * to the first unread dentry. If we skip any dentries, we bump
|
|---|
| 1072 | * the position pointer accordingly.
|
|---|
| 1073 | */
|
|---|
| 1074 | bnum = (pos * sizeof(fat_dentry_t)) / bps;
|
|---|
| 1075 | while (bnum < nodep->size / bps) {
|
|---|
| 1076 | off_t o;
|
|---|
| 1077 |
|
|---|
| [684b655] | 1078 | rc = fat_block_get(&b, bs, nodep, bnum,
|
|---|
| 1079 | BLOCK_FLAGS_NONE);
|
|---|
| 1080 | assert(rc == EOK);
|
|---|
| [ddd1219] | 1081 | for (o = pos % (bps / sizeof(fat_dentry_t));
|
|---|
| 1082 | o < bps / sizeof(fat_dentry_t);
|
|---|
| 1083 | o++, pos++) {
|
|---|
| 1084 | d = ((fat_dentry_t *)b->data) + o;
|
|---|
| 1085 | switch (fat_classify_dentry(d)) {
|
|---|
| 1086 | case FAT_DENTRY_SKIP:
|
|---|
| [0fdd6bb] | 1087 | case FAT_DENTRY_FREE:
|
|---|
| [ddd1219] | 1088 | continue;
|
|---|
| 1089 | case FAT_DENTRY_LAST:
|
|---|
| [c91f2d1b] | 1090 | rc = block_put(b);
|
|---|
| 1091 | assert(rc == EOK);
|
|---|
| [ddd1219] | 1092 | goto miss;
|
|---|
| 1093 | default:
|
|---|
| 1094 | case FAT_DENTRY_VALID:
|
|---|
| [0fdd6bb] | 1095 | fat_dentry_name_get(d, name);
|
|---|
| [073f550] | 1096 | rc = block_put(b);
|
|---|
| [c91f2d1b] | 1097 | assert(rc == EOK);
|
|---|
| [ddd1219] | 1098 | goto hit;
|
|---|
| 1099 | }
|
|---|
| 1100 | }
|
|---|
| [c91f2d1b] | 1101 | rc = block_put(b);
|
|---|
| 1102 | assert(rc == EOK);
|
|---|
| [ddd1219] | 1103 | bnum++;
|
|---|
| 1104 | }
|
|---|
| 1105 | miss:
|
|---|
| [b6035ba] | 1106 | fat_node_put(fn);
|
|---|
| [ddd1219] | 1107 | ipc_answer_0(callid, ENOENT);
|
|---|
| 1108 | ipc_answer_1(rid, ENOENT, 0);
|
|---|
| [4bf40f6] | 1109 | return;
|
|---|
| [ddd1219] | 1110 | hit:
|
|---|
| [92fd52d7] | 1111 | (void) ipc_data_read_finalize(callid, name, str_size(name) + 1);
|
|---|
| [ddd1219] | 1112 | bytes = (pos - spos) + 1;
|
|---|
| [4bf40f6] | 1113 | }
|
|---|
| 1114 |
|
|---|
| [b6035ba] | 1115 | fat_node_put(fn);
|
|---|
| [79d031b] | 1116 | ipc_answer_1(rid, EOK, (ipcarg_t)bytes);
|
|---|
| [4bf40f6] | 1117 | }
|
|---|
| 1118 |
|
|---|
| [c947dda] | 1119 | void fat_write(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 1120 | {
|
|---|
| [8d32152] | 1121 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
|
|---|
| 1122 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
|---|
| 1123 | off_t pos = (off_t)IPC_GET_ARG3(*request);
|
|---|
| [073f550] | 1124 | fs_node_t *fn;
|
|---|
| [b6035ba] | 1125 | fat_node_t *nodep;
|
|---|
| [7858bc5f] | 1126 | fat_bs_t *bs;
|
|---|
| [8d32152] | 1127 | size_t bytes;
|
|---|
| [7858bc5f] | 1128 | block_t *b;
|
|---|
| [8d32152] | 1129 | uint16_t bps;
|
|---|
| 1130 | unsigned spc;
|
|---|
| [913a821c] | 1131 | unsigned bpc; /* bytes per cluster */
|
|---|
| [b4b7187] | 1132 | off_t boundary;
|
|---|
| [1d8cdb1] | 1133 | int flags = BLOCK_FLAGS_NONE;
|
|---|
| [c91f2d1b] | 1134 | int rc;
|
|---|
| [8d32152] | 1135 |
|
|---|
| [073f550] | 1136 | rc = fat_node_get(&fn, dev_handle, index);
|
|---|
| 1137 | if (rc != EOK) {
|
|---|
| 1138 | ipc_answer_0(rid, rc);
|
|---|
| 1139 | return;
|
|---|
| 1140 | }
|
|---|
| [b6035ba] | 1141 | if (!fn) {
|
|---|
| [8d32152] | 1142 | ipc_answer_0(rid, ENOENT);
|
|---|
| 1143 | return;
|
|---|
| 1144 | }
|
|---|
| [b6035ba] | 1145 | nodep = FAT_NODE(fn);
|
|---|
| [8d32152] | 1146 |
|
|---|
| 1147 | ipc_callid_t callid;
|
|---|
| 1148 | size_t len;
|
|---|
| 1149 | if (!ipc_data_write_receive(&callid, &len)) {
|
|---|
| [b6035ba] | 1150 | fat_node_put(fn);
|
|---|
| [8d32152] | 1151 | ipc_answer_0(callid, EINVAL);
|
|---|
| 1152 | ipc_answer_0(rid, EINVAL);
|
|---|
| 1153 | return;
|
|---|
| 1154 | }
|
|---|
| 1155 |
|
|---|
| [913a821c] | 1156 | bs = block_bb_get(dev_handle);
|
|---|
| 1157 | bps = uint16_t_le2host(bs->bps);
|
|---|
| 1158 | spc = bs->spc;
|
|---|
| 1159 | bpc = bps * spc;
|
|---|
| 1160 |
|
|---|
| [8d32152] | 1161 | /*
|
|---|
| 1162 | * In all scenarios, we will attempt to write out only one block worth
|
|---|
| 1163 | * of data at maximum. There might be some more efficient approaches,
|
|---|
| 1164 | * but this one greatly simplifies fat_write(). Note that we can afford
|
|---|
| 1165 | * to do this because the client must be ready to handle the return
|
|---|
| 1166 | * value signalizing a smaller number of bytes written.
|
|---|
| 1167 | */
|
|---|
| 1168 | bytes = min(len, bps - pos % bps);
|
|---|
| [1d8cdb1] | 1169 | if (bytes == bps)
|
|---|
| 1170 | flags |= BLOCK_FLAGS_NOREAD;
|
|---|
| [8d32152] | 1171 |
|
|---|
| [913a821c] | 1172 | boundary = ROUND_UP(nodep->size, bpc);
|
|---|
| [b4b7187] | 1173 | if (pos < boundary) {
|
|---|
| [8d32152] | 1174 | /*
|
|---|
| 1175 | * This is the easier case - we are either overwriting already
|
|---|
| 1176 | * existing contents or writing behind the EOF, but still within
|
|---|
| 1177 | * the limits of the last cluster. The node size may grow to the
|
|---|
| 1178 | * next block size boundary.
|
|---|
| 1179 | */
|
|---|
| [cca29e3c] | 1180 | rc = fat_fill_gap(bs, nodep, FAT_CLST_RES0, pos);
|
|---|
| 1181 | assert(rc == EOK);
|
|---|
| [684b655] | 1182 | rc = fat_block_get(&b, bs, nodep, pos / bps, flags);
|
|---|
| 1183 | assert(rc == EOK);
|
|---|
| [8d32152] | 1184 | (void) ipc_data_write_finalize(callid, b->data + pos % bps,
|
|---|
| 1185 | bytes);
|
|---|
| 1186 | b->dirty = true; /* need to sync block */
|
|---|
| [c91f2d1b] | 1187 | rc = block_put(b);
|
|---|
| 1188 | assert(rc == EOK);
|
|---|
| [8d32152] | 1189 | if (pos + bytes > nodep->size) {
|
|---|
| 1190 | nodep->size = pos + bytes;
|
|---|
| 1191 | nodep->dirty = true; /* need to sync node */
|
|---|
| 1192 | }
|
|---|
| [ac49f5d1] | 1193 | ipc_answer_2(rid, EOK, bytes, nodep->size);
|
|---|
| [b6035ba] | 1194 | fat_node_put(fn);
|
|---|
| [8d32152] | 1195 | return;
|
|---|
| 1196 | } else {
|
|---|
| 1197 | /*
|
|---|
| 1198 | * This is the more difficult case. We must allocate new
|
|---|
| 1199 | * clusters for the node and zero them out.
|
|---|
| 1200 | */
|
|---|
| [6f2dfd1] | 1201 | int status;
|
|---|
| [8d32152] | 1202 | unsigned nclsts;
|
|---|
| [8334a427] | 1203 | fat_cluster_t mcl, lcl;
|
|---|
| 1204 |
|
|---|
| [913a821c] | 1205 | nclsts = (ROUND_UP(pos + bytes, bpc) - boundary) / bpc;
|
|---|
| [6f2dfd1] | 1206 | /* create an independent chain of nclsts clusters in all FATs */
|
|---|
| [913a821c] | 1207 | status = fat_alloc_clusters(bs, dev_handle, nclsts, &mcl, &lcl);
|
|---|
| [6f2dfd1] | 1208 | if (status != EOK) {
|
|---|
| 1209 | /* could not allocate a chain of nclsts clusters */
|
|---|
| [b6035ba] | 1210 | fat_node_put(fn);
|
|---|
| [6f2dfd1] | 1211 | ipc_answer_0(callid, status);
|
|---|
| 1212 | ipc_answer_0(rid, status);
|
|---|
| 1213 | return;
|
|---|
| 1214 | }
|
|---|
| 1215 | /* zero fill any gaps */
|
|---|
| [cca29e3c] | 1216 | rc = fat_fill_gap(bs, nodep, mcl, pos);
|
|---|
| 1217 | assert(rc == EOK);
|
|---|
| [684b655] | 1218 | rc = _fat_block_get(&b, bs, dev_handle, lcl, (pos / bps) % spc,
|
|---|
| [1d8cdb1] | 1219 | flags);
|
|---|
| [684b655] | 1220 | assert(rc == EOK);
|
|---|
| [6f2dfd1] | 1221 | (void) ipc_data_write_finalize(callid, b->data + pos % bps,
|
|---|
| 1222 | bytes);
|
|---|
| [b4b7187] | 1223 | b->dirty = true; /* need to sync block */
|
|---|
| [c91f2d1b] | 1224 | rc = block_put(b);
|
|---|
| 1225 | assert(rc == EOK);
|
|---|
| [6f2dfd1] | 1226 | /*
|
|---|
| 1227 | * Append the cluster chain starting in mcl to the end of the
|
|---|
| 1228 | * node's cluster chain.
|
|---|
| 1229 | */
|
|---|
| [cca29e3c] | 1230 | rc = fat_append_clusters(bs, nodep, mcl);
|
|---|
| 1231 | assert(rc == EOK);
|
|---|
| [6f2dfd1] | 1232 | nodep->size = pos + bytes;
|
|---|
| [b4b7187] | 1233 | nodep->dirty = true; /* need to sync node */
|
|---|
| [ac49f5d1] | 1234 | ipc_answer_2(rid, EOK, bytes, nodep->size);
|
|---|
| [b6035ba] | 1235 | fat_node_put(fn);
|
|---|
| [6f2dfd1] | 1236 | return;
|
|---|
| [8d32152] | 1237 | }
|
|---|
| [c947dda] | 1238 | }
|
|---|
| 1239 |
|
|---|
| [6c71a1f] | 1240 | void fat_truncate(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 1241 | {
|
|---|
| [8334a427] | 1242 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
|
|---|
| 1243 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
|---|
| 1244 | size_t size = (off_t)IPC_GET_ARG3(*request);
|
|---|
| [073f550] | 1245 | fs_node_t *fn;
|
|---|
| [b6035ba] | 1246 | fat_node_t *nodep;
|
|---|
| [913a821c] | 1247 | fat_bs_t *bs;
|
|---|
| 1248 | uint16_t bps;
|
|---|
| 1249 | uint8_t spc;
|
|---|
| 1250 | unsigned bpc; /* bytes per cluster */
|
|---|
| [8334a427] | 1251 | int rc;
|
|---|
| 1252 |
|
|---|
| [073f550] | 1253 | rc = fat_node_get(&fn, dev_handle, index);
|
|---|
| 1254 | if (rc != EOK) {
|
|---|
| 1255 | ipc_answer_0(rid, rc);
|
|---|
| 1256 | return;
|
|---|
| 1257 | }
|
|---|
| [b6035ba] | 1258 | if (!fn) {
|
|---|
| [8334a427] | 1259 | ipc_answer_0(rid, ENOENT);
|
|---|
| 1260 | return;
|
|---|
| 1261 | }
|
|---|
| [b6035ba] | 1262 | nodep = FAT_NODE(fn);
|
|---|
| [8334a427] | 1263 |
|
|---|
| [913a821c] | 1264 | bs = block_bb_get(dev_handle);
|
|---|
| 1265 | bps = uint16_t_le2host(bs->bps);
|
|---|
| 1266 | spc = bs->spc;
|
|---|
| 1267 | bpc = bps * spc;
|
|---|
| 1268 |
|
|---|
| [8334a427] | 1269 | if (nodep->size == size) {
|
|---|
| 1270 | rc = EOK;
|
|---|
| 1271 | } else if (nodep->size < size) {
|
|---|
| 1272 | /*
|
|---|
| [913a821c] | 1273 | * The standard says we have the freedom to grow the node.
|
|---|
| [8334a427] | 1274 | * For now, we simply return an error.
|
|---|
| 1275 | */
|
|---|
| 1276 | rc = EINVAL;
|
|---|
| [913a821c] | 1277 | } else if (ROUND_UP(nodep->size, bpc) == ROUND_UP(size, bpc)) {
|
|---|
| 1278 | /*
|
|---|
| 1279 | * The node will be shrunk, but no clusters will be deallocated.
|
|---|
| 1280 | */
|
|---|
| 1281 | nodep->size = size;
|
|---|
| 1282 | nodep->dirty = true; /* need to sync node */
|
|---|
| 1283 | rc = EOK;
|
|---|
| [8334a427] | 1284 | } else {
|
|---|
| 1285 | /*
|
|---|
| [913a821c] | 1286 | * The node will be shrunk, clusters will be deallocated.
|
|---|
| [8334a427] | 1287 | */
|
|---|
| [913a821c] | 1288 | if (size == 0) {
|
|---|
| [cca29e3c] | 1289 | rc = fat_chop_clusters(bs, nodep, FAT_CLST_RES0);
|
|---|
| 1290 | if (rc != EOK)
|
|---|
| 1291 | goto out;
|
|---|
| [913a821c] | 1292 | } else {
|
|---|
| 1293 | fat_cluster_t lastc;
|
|---|
| [e402382] | 1294 | rc = fat_cluster_walk(bs, dev_handle, nodep->firstc,
|
|---|
| 1295 | &lastc, NULL, (size - 1) / bpc);
|
|---|
| 1296 | if (rc != EOK)
|
|---|
| 1297 | goto out;
|
|---|
| [cca29e3c] | 1298 | rc = fat_chop_clusters(bs, nodep, lastc);
|
|---|
| 1299 | if (rc != EOK)
|
|---|
| 1300 | goto out;
|
|---|
| [913a821c] | 1301 | }
|
|---|
| 1302 | nodep->size = size;
|
|---|
| 1303 | nodep->dirty = true; /* need to sync node */
|
|---|
| 1304 | rc = EOK;
|
|---|
| [8334a427] | 1305 | }
|
|---|
| [e402382] | 1306 | out:
|
|---|
| [b6035ba] | 1307 | fat_node_put(fn);
|
|---|
| [8334a427] | 1308 | ipc_answer_0(rid, rc);
|
|---|
| 1309 | return;
|
|---|
| [6c71a1f] | 1310 | }
|
|---|
| 1311 |
|
|---|
| [c20aa06] | 1312 | void fat_close(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 1313 | {
|
|---|
| 1314 | ipc_answer_0(rid, EOK);
|
|---|
| 1315 | }
|
|---|
| 1316 |
|
|---|
| [50e5b25] | 1317 | void fat_destroy(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 1318 | {
|
|---|
| 1319 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
|
|---|
| 1320 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
|---|
| [073f550] | 1321 | fs_node_t *fn;
|
|---|
| [50e5b25] | 1322 | int rc;
|
|---|
| 1323 |
|
|---|
| [073f550] | 1324 | rc = fat_node_get(&fn, dev_handle, index);
|
|---|
| 1325 | if (rc != EOK) {
|
|---|
| 1326 | ipc_answer_0(rid, rc);
|
|---|
| 1327 | return;
|
|---|
| 1328 | }
|
|---|
| [b6035ba] | 1329 | if (!fn) {
|
|---|
| [50e5b25] | 1330 | ipc_answer_0(rid, ENOENT);
|
|---|
| 1331 | return;
|
|---|
| 1332 | }
|
|---|
| 1333 |
|
|---|
| [b6035ba] | 1334 | rc = fat_destroy_node(fn);
|
|---|
| [50e5b25] | 1335 | ipc_answer_0(rid, rc);
|
|---|
| 1336 | }
|
|---|
| 1337 |
|
|---|
| [c20aa06] | 1338 | void fat_open_node(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 1339 | {
|
|---|
| 1340 | libfs_open_node(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
|
|---|
| 1341 | }
|
|---|
| 1342 |
|
|---|
| [852b801] | 1343 | void fat_stat(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| [c20aa06] | 1344 | {
|
|---|
| [75160a6] | 1345 | libfs_stat(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
|
|---|
| [c20aa06] | 1346 | }
|
|---|
| 1347 |
|
|---|
| 1348 | void fat_sync(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 1349 | {
|
|---|
| 1350 | /* Dummy implementation */
|
|---|
| 1351 | ipc_answer_0(rid, EOK);
|
|---|
| 1352 | }
|
|---|
| 1353 |
|
|---|
| [be815bc] | 1354 | /**
|
|---|
| 1355 | * @}
|
|---|
| [c20aa06] | 1356 | */
|
|---|