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