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