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