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