| 1 | /*
|
|---|
| 2 | * Copyright (c) 2008 Jakub Jermar
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup fs
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * @file fat_ops.c
|
|---|
| 35 | * @brief Implementation of VFS operations for the FAT file system server.
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| 38 | #include "fat.h"
|
|---|
| 39 | #include "../../vfs/vfs.h"
|
|---|
| 40 | #include <libfs.h>
|
|---|
| 41 | #include <ipc/ipc.h>
|
|---|
| 42 | #include <async.h>
|
|---|
| 43 | #include <errno.h>
|
|---|
| 44 | #include <string.h>
|
|---|
| 45 | #include <byteorder.h>
|
|---|
| 46 | #include <libadt/hash_table.h>
|
|---|
| 47 | #include <libadt/list.h>
|
|---|
| 48 | #include <assert.h>
|
|---|
| 49 | #include <futex.h>
|
|---|
| 50 |
|
|---|
| 51 | #define BS_BLOCK 0
|
|---|
| 52 |
|
|---|
| 53 | #define FIN_KEY_DEV_HANDLE 0
|
|---|
| 54 | #define FIN_KEY_INDEX 1
|
|---|
| 55 |
|
|---|
| 56 | /** Futex protecting both fin_hash and ffn_head. */
|
|---|
| 57 | futex_t fin_futex = FUTEX_INITIALIZER;
|
|---|
| 58 |
|
|---|
| 59 | /** Hash table of FAT in-core nodes. */
|
|---|
| 60 | hash_table_t fin_hash;
|
|---|
| 61 |
|
|---|
| 62 | /** List of free FAT in-core nodes. */
|
|---|
| 63 | link_t ffn_head;
|
|---|
| 64 |
|
|---|
| 65 | #define FAT_NAME_LEN 8
|
|---|
| 66 | #define FAT_EXT_LEN 3
|
|---|
| 67 |
|
|---|
| 68 | #define FAT_PAD ' '
|
|---|
| 69 |
|
|---|
| 70 | #define FAT_DENTRY_UNUSED 0x00
|
|---|
| 71 | #define FAT_DENTRY_E5_ESC 0x05
|
|---|
| 72 | #define FAT_DENTRY_DOT 0x2e
|
|---|
| 73 | #define FAT_DENTRY_ERASED 0xe5
|
|---|
| 74 |
|
|---|
| 75 | static void dentry_name_canonify(fat_dentry_t *d, char *buf)
|
|---|
| 76 | {
|
|---|
| 77 | int i;
|
|---|
| 78 |
|
|---|
| 79 | for (i = 0; i < FAT_NAME_LEN; i++) {
|
|---|
| 80 | if (d->name[i] == FAT_PAD) {
|
|---|
| 81 | buf++;
|
|---|
| 82 | break;
|
|---|
| 83 | }
|
|---|
| 84 | if (d->name[i] == FAT_DENTRY_E5_ESC)
|
|---|
| 85 | *buf++ = 0xe5;
|
|---|
| 86 | else
|
|---|
| 87 | *buf++ = d->name[i];
|
|---|
| 88 | }
|
|---|
| 89 | if (d->ext[0] != FAT_PAD)
|
|---|
| 90 | *buf++ = '.';
|
|---|
| 91 | for (i = 0; i < FAT_EXT_LEN; i++) {
|
|---|
| 92 | if (d->ext[i] == FAT_PAD) {
|
|---|
| 93 | *buf = '\0';
|
|---|
| 94 | return;
|
|---|
| 95 | }
|
|---|
| 96 | if (d->ext[i] == FAT_DENTRY_E5_ESC)
|
|---|
| 97 | *buf++ = 0xe5;
|
|---|
| 98 | else
|
|---|
| 99 | *buf++ = d->ext[i];
|
|---|
| 100 | }
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | /* TODO and also move somewhere else */
|
|---|
| 104 | typedef struct {
|
|---|
| 105 | void *data;
|
|---|
| 106 | } block_t;
|
|---|
| 107 |
|
|---|
| 108 | static block_t *block_get(dev_handle_t dev_handle, off_t offset)
|
|---|
| 109 | {
|
|---|
| 110 | return NULL; /* TODO */
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | static void block_put(block_t *block)
|
|---|
| 114 | {
|
|---|
| 115 | /* TODO */
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 | #define FAT_BS(b) ((fat_bs_t *)((b)->data))
|
|---|
| 120 |
|
|---|
| 121 | #define FAT_CLST_FIRST 0x0002
|
|---|
| 122 | #define FAT_CLST_BAD 0xfff7
|
|---|
| 123 | #define FAT_CLST_LAST1 0xfff8
|
|---|
| 124 | #define FAT_CLST_LAST8 0xffff
|
|---|
| 125 |
|
|---|
| 126 | /** Convert cluster number to an index within a FAT.
|
|---|
| 127 | *
|
|---|
| 128 | * Format Identifier and cluster numbering is considered.
|
|---|
| 129 | */
|
|---|
| 130 | #define C2FAT_IDX(c) (1 + (c) - FAT_CLST_FIRST)
|
|---|
| 131 |
|
|---|
| 132 | static block_t *fat_block_get(dev_handle_t dev_handle, fs_index_t index,
|
|---|
| 133 | off_t offset)
|
|---|
| 134 | {
|
|---|
| 135 | block_t *bb;
|
|---|
| 136 | block_t *b;
|
|---|
| 137 | unsigned bps;
|
|---|
| 138 | unsigned spc;
|
|---|
| 139 | unsigned rscnt; /* block address of the first FAT */
|
|---|
| 140 | unsigned fatcnt;
|
|---|
| 141 | unsigned rde;
|
|---|
| 142 | unsigned rds; /* root directory size */
|
|---|
| 143 | unsigned sf;
|
|---|
| 144 | unsigned ssa; /* size of the system area */
|
|---|
| 145 | unsigned clusters;
|
|---|
| 146 | unsigned clst = index;
|
|---|
| 147 | unsigned i;
|
|---|
| 148 |
|
|---|
| 149 | bb = block_get(dev_handle, BS_BLOCK);
|
|---|
| 150 | bps = uint16_t_le2host(FAT_BS(bb)->bps);
|
|---|
| 151 | spc = FAT_BS(bb)->spc;
|
|---|
| 152 | rscnt = uint16_t_le2host(FAT_BS(bb)->rscnt);
|
|---|
| 153 | fatcnt = FAT_BS(bb)->fatcnt;
|
|---|
| 154 | rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max);
|
|---|
| 155 | sf = uint16_t_le2host(FAT_BS(bb)->sec_per_fat);
|
|---|
| 156 | block_put(bb);
|
|---|
| 157 |
|
|---|
| 158 | rds = (sizeof(fat_dentry_t) * rde) / bps;
|
|---|
| 159 | rds += ((sizeof(fat_dentry_t) * rde) % bps != 0);
|
|---|
| 160 | ssa = rscnt + fatcnt * sf + rds;
|
|---|
| 161 |
|
|---|
| 162 | if (!index) {
|
|---|
| 163 | /* root directory special case */
|
|---|
| 164 | assert(offset < rds);
|
|---|
| 165 | b = block_get(dev_handle, rscnt + fatcnt * sf + offset);
|
|---|
| 166 | return b;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | clusters = offset / spc;
|
|---|
| 170 | for (i = 0; i < clusters; i++) {
|
|---|
| 171 | unsigned fsec; /* sector offset relative to FAT1 */
|
|---|
| 172 | unsigned fidx; /* FAT1 entry index */
|
|---|
| 173 |
|
|---|
| 174 | assert(clst >= FAT_CLST_FIRST && clst < FAT_CLST_BAD);
|
|---|
| 175 | fsec = (C2FAT_IDX(clst) * sizeof(uint16_t)) / bps;
|
|---|
| 176 | fidx = C2FAT_IDX(clst) % (bps / sizeof(uint16_t));
|
|---|
| 177 | /* read FAT1 */
|
|---|
| 178 | b = block_get(dev_handle, rscnt + fsec);
|
|---|
| 179 | clst = uint16_t_le2host(((uint16_t *)b->data)[fidx]);
|
|---|
| 180 | assert(clst != FAT_CLST_BAD);
|
|---|
| 181 | assert(clst < FAT_CLST_LAST1);
|
|---|
| 182 | block_put(b);
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | b = block_get(dev_handle, ssa + (clst - FAT_CLST_FIRST) * spc +
|
|---|
| 186 | offset % spc);
|
|---|
| 187 |
|
|---|
| 188 | return b;
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | static void fat_node_initialize(fat_node_t *node)
|
|---|
| 192 | {
|
|---|
| 193 | futex_initialize(&node->lock, 1);
|
|---|
| 194 | node->type = 0;
|
|---|
| 195 | node->index = 0;
|
|---|
| 196 | node->pindex = 0;
|
|---|
| 197 | node->dev_handle = 0;
|
|---|
| 198 | link_initialize(&node->fin_link);
|
|---|
| 199 | link_initialize(&node->ffn_link);
|
|---|
| 200 | node->size = 0;
|
|---|
| 201 | node->lnkcnt = 0;
|
|---|
| 202 | node->refcnt = 0;
|
|---|
| 203 | node->dirty = false;
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | static uint16_t fat_bps_get(dev_handle_t dev_handle)
|
|---|
| 207 | {
|
|---|
| 208 | block_t *bb;
|
|---|
| 209 | uint16_t bps;
|
|---|
| 210 |
|
|---|
| 211 | bb = block_get(dev_handle, BS_BLOCK);
|
|---|
| 212 | assert(bb != NULL);
|
|---|
| 213 | bps = uint16_t_le2host(FAT_BS(bb)->bps);
|
|---|
| 214 | block_put(bb);
|
|---|
| 215 |
|
|---|
| 216 | return bps;
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | typedef enum {
|
|---|
| 220 | FAT_DENTRY_SKIP,
|
|---|
| 221 | FAT_DENTRY_LAST,
|
|---|
| 222 | FAT_DENTRY_VALID
|
|---|
| 223 | } fat_dentry_clsf_t;
|
|---|
| 224 |
|
|---|
| 225 | static fat_dentry_clsf_t fat_classify_dentry(fat_dentry_t *d)
|
|---|
| 226 | {
|
|---|
| 227 | if (d->attr & FAT_ATTR_VOLLABEL) {
|
|---|
| 228 | /* volume label entry */
|
|---|
| 229 | return FAT_DENTRY_SKIP;
|
|---|
| 230 | }
|
|---|
| 231 | if (d->name[0] == FAT_DENTRY_ERASED) {
|
|---|
| 232 | /* not-currently-used entry */
|
|---|
| 233 | return FAT_DENTRY_SKIP;
|
|---|
| 234 | }
|
|---|
| 235 | if (d->name[0] == FAT_DENTRY_UNUSED) {
|
|---|
| 236 | /* never used entry */
|
|---|
| 237 | return FAT_DENTRY_LAST;
|
|---|
| 238 | }
|
|---|
| 239 | if (d->name[0] == FAT_DENTRY_DOT) {
|
|---|
| 240 | /*
|
|---|
| 241 | * Most likely '.' or '..'.
|
|---|
| 242 | * It cannot occur in a regular file name.
|
|---|
| 243 | */
|
|---|
| 244 | return FAT_DENTRY_SKIP;
|
|---|
| 245 | }
|
|---|
| 246 | return FAT_DENTRY_VALID;
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | static void fat_sync_node(fat_node_t *node)
|
|---|
| 250 | {
|
|---|
| 251 | /* TODO */
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | /** Instantiate a FAT in-core node.
|
|---|
| 255 | *
|
|---|
| 256 | * FAT stores the info necessary for instantiation of a node in the parent of
|
|---|
| 257 | * that node. This design necessitated the addition of the parent node index
|
|---|
| 258 | * parameter to this otherwise generic libfs API.
|
|---|
| 259 | */
|
|---|
| 260 | static void *
|
|---|
| 261 | fat_node_get(dev_handle_t dev_handle, fs_index_t index, fs_index_t pindex)
|
|---|
| 262 | {
|
|---|
| 263 | link_t *lnk;
|
|---|
| 264 | fat_node_t *node = NULL;
|
|---|
| 265 | block_t *b;
|
|---|
| 266 | unsigned bps;
|
|---|
| 267 | unsigned dps;
|
|---|
| 268 | fat_dentry_t *d;
|
|---|
| 269 | unsigned i, j;
|
|---|
| 270 |
|
|---|
| 271 | unsigned long key[] = {
|
|---|
| 272 | [FIN_KEY_DEV_HANDLE] = dev_handle,
|
|---|
| 273 | [FIN_KEY_INDEX] = index
|
|---|
| 274 | };
|
|---|
| 275 |
|
|---|
| 276 | futex_down(&fin_futex);
|
|---|
| 277 | lnk = hash_table_find(&fin_hash, key);
|
|---|
| 278 | if (lnk) {
|
|---|
| 279 | /*
|
|---|
| 280 | * The in-core node was found in the hash table.
|
|---|
| 281 | */
|
|---|
| 282 | node = hash_table_get_instance(lnk, fat_node_t, fin_link);
|
|---|
| 283 | if (!node->refcnt++)
|
|---|
| 284 | list_remove(&node->ffn_link);
|
|---|
| 285 | futex_up(&fin_futex);
|
|---|
| 286 |
|
|---|
| 287 | /* Make sure that the node is fully instantiated. */
|
|---|
| 288 | futex_down(&node->lock);
|
|---|
| 289 | futex_up(&node->lock);
|
|---|
| 290 |
|
|---|
| 291 | return (void *) node;
|
|---|
| 292 | }
|
|---|
| 293 |
|
|---|
| 294 | bps = fat_bps_get(dev_handle);
|
|---|
| 295 | dps = bps / sizeof(fat_dentry_t);
|
|---|
| 296 |
|
|---|
| 297 | if (!list_empty(&ffn_head)) {
|
|---|
| 298 | /*
|
|---|
| 299 | * We are going to reuse a node from the free list.
|
|---|
| 300 | */
|
|---|
| 301 | lnk = ffn_head.next;
|
|---|
| 302 | list_remove(lnk);
|
|---|
| 303 | node = list_get_instance(lnk, fat_node_t, ffn_link);
|
|---|
| 304 | assert(!node->refcnt);
|
|---|
| 305 | if (node->dirty)
|
|---|
| 306 | fat_sync_node(node);
|
|---|
| 307 | key[FIN_KEY_DEV_HANDLE] = node->dev_handle;
|
|---|
| 308 | key[FIN_KEY_INDEX] = node->index;
|
|---|
| 309 | hash_table_remove(&fin_hash, key, sizeof(key)/sizeof(*key));
|
|---|
| 310 | } else {
|
|---|
| 311 | /*
|
|---|
| 312 | * We need to allocate a new node.
|
|---|
| 313 | */
|
|---|
| 314 | node = malloc(sizeof(fat_node_t));
|
|---|
| 315 | if (!node)
|
|---|
| 316 | return NULL;
|
|---|
| 317 | }
|
|---|
| 318 | fat_node_initialize(node);
|
|---|
| 319 | node->refcnt++;
|
|---|
| 320 | node->lnkcnt++;
|
|---|
| 321 | node->dev_handle = dev_handle;
|
|---|
| 322 | node->index = index;
|
|---|
| 323 | node->pindex = pindex;
|
|---|
| 324 | key[FIN_KEY_DEV_HANDLE] = node->dev_handle;
|
|---|
| 325 | key[FIN_KEY_INDEX] = node->index;
|
|---|
| 326 | hash_table_insert(&fin_hash, key, &node->fin_link);
|
|---|
| 327 |
|
|---|
| 328 | /*
|
|---|
| 329 | * We have already put the node back to fin_hash.
|
|---|
| 330 | * The node is not yet fully instantiated so we lock it prior to
|
|---|
| 331 | * unlocking fin_hash.
|
|---|
| 332 | */
|
|---|
| 333 | futex_down(&node->lock);
|
|---|
| 334 | futex_up(&fin_futex);
|
|---|
| 335 |
|
|---|
| 336 | /*
|
|---|
| 337 | * Because of the design of the FAT file system, we have no clue about
|
|---|
| 338 | * how big (i.e. how many directory entries it contains) is the parent
|
|---|
| 339 | * of the node we are trying to instantiate. However, we know that it
|
|---|
| 340 | * must contain a directory entry for our node of interest. We simply
|
|---|
| 341 | * scan the parent until we find it.
|
|---|
| 342 | */
|
|---|
| 343 | for (i = 0; ; i++) {
|
|---|
| 344 | b = fat_block_get(node->dev_handle, node->pindex, i);
|
|---|
| 345 | for (j = 0; j < dps; j++) {
|
|---|
| 346 | d = ((fat_dentry_t *)b->data) + j;
|
|---|
| 347 | if (d->firstc == node->index)
|
|---|
| 348 | goto found;
|
|---|
| 349 | }
|
|---|
| 350 | block_put(b);
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|
| 353 | found:
|
|---|
| 354 | if (!(d->attr & (FAT_ATTR_SUBDIR | FAT_ATTR_VOLLABEL)))
|
|---|
| 355 | node->type = FAT_FILE;
|
|---|
| 356 | if ((d->attr & FAT_ATTR_SUBDIR) || !index)
|
|---|
| 357 | node->type = FAT_DIRECTORY;
|
|---|
| 358 | assert((node->type == FAT_FILE) || (node->type == FAT_DIRECTORY));
|
|---|
| 359 |
|
|---|
| 360 | node->size = uint32_t_le2host(d->size);
|
|---|
| 361 | block_put(b);
|
|---|
| 362 |
|
|---|
| 363 | futex_up(&node->lock);
|
|---|
| 364 | return node;
|
|---|
| 365 | }
|
|---|
| 366 |
|
|---|
| 367 | static void fat_node_put(void *node)
|
|---|
| 368 | {
|
|---|
| 369 | fat_node_t *nodep = (fat_node_t *)node;
|
|---|
| 370 |
|
|---|
| 371 | futex_down(&fin_futex);
|
|---|
| 372 | if (!--nodep->refcnt)
|
|---|
| 373 | list_append(&nodep->ffn_link, &ffn_head);
|
|---|
| 374 | futex_up(&fin_futex);
|
|---|
| 375 | }
|
|---|
| 376 |
|
|---|
| 377 | static void *fat_create(int flags)
|
|---|
| 378 | {
|
|---|
| 379 | return NULL; /* not supported at the moment */
|
|---|
| 380 | }
|
|---|
| 381 |
|
|---|
| 382 | static int fat_destroy(void *node)
|
|---|
| 383 | {
|
|---|
| 384 | return ENOTSUP; /* not supported at the moment */
|
|---|
| 385 | }
|
|---|
| 386 |
|
|---|
| 387 | static bool fat_link(void *prnt, void *chld, const char *name)
|
|---|
| 388 | {
|
|---|
| 389 | return false; /* not supported at the moment */
|
|---|
| 390 | }
|
|---|
| 391 |
|
|---|
| 392 | static int fat_unlink(void *prnt, void *chld)
|
|---|
| 393 | {
|
|---|
| 394 | return ENOTSUP; /* not supported at the moment */
|
|---|
| 395 | }
|
|---|
| 396 |
|
|---|
| 397 | static void *fat_match(void *prnt, const char *component)
|
|---|
| 398 | {
|
|---|
| 399 | fat_node_t *parentp = (fat_node_t *)prnt;
|
|---|
| 400 | char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
|
|---|
| 401 | unsigned i, j;
|
|---|
| 402 | unsigned bps; /* bytes per sector */
|
|---|
| 403 | unsigned dps; /* dentries per sector */
|
|---|
| 404 | unsigned blocks;
|
|---|
| 405 | fat_dentry_t *d;
|
|---|
| 406 | block_t *b;
|
|---|
| 407 |
|
|---|
| 408 | bps = fat_bps_get(parentp->dev_handle);
|
|---|
| 409 | dps = bps / sizeof(fat_dentry_t);
|
|---|
| 410 | blocks = parentp->size / bps + (parentp->size % bps != 0);
|
|---|
| 411 | for (i = 0; i < blocks; i++) {
|
|---|
| 412 | unsigned dentries;
|
|---|
| 413 |
|
|---|
| 414 | b = fat_block_get(parentp->dev_handle, parentp->index, i);
|
|---|
| 415 | dentries = (i == blocks - 1) ?
|
|---|
| 416 | parentp->size % sizeof(fat_dentry_t) :
|
|---|
| 417 | dps;
|
|---|
| 418 | for (j = 0; j < dentries; j++) {
|
|---|
| 419 | d = ((fat_dentry_t *)b->data) + j;
|
|---|
| 420 | switch (fat_classify_dentry(d)) {
|
|---|
| 421 | case FAT_DENTRY_SKIP:
|
|---|
| 422 | continue;
|
|---|
| 423 | case FAT_DENTRY_LAST:
|
|---|
| 424 | block_put(b);
|
|---|
| 425 | return NULL;
|
|---|
| 426 | default:
|
|---|
| 427 | case FAT_DENTRY_VALID:
|
|---|
| 428 | dentry_name_canonify(d, name);
|
|---|
| 429 | break;
|
|---|
| 430 | }
|
|---|
| 431 | if (strcmp(name, component) == 0) {
|
|---|
| 432 | /* hit */
|
|---|
| 433 | void *node = fat_node_get(parentp->dev_handle,
|
|---|
| 434 | (fs_index_t)uint16_t_le2host(d->firstc),
|
|---|
| 435 | parentp->index);
|
|---|
| 436 | block_put(b);
|
|---|
| 437 | return node;
|
|---|
| 438 | }
|
|---|
| 439 | }
|
|---|
| 440 | block_put(b);
|
|---|
| 441 | }
|
|---|
| 442 |
|
|---|
| 443 | return NULL;
|
|---|
| 444 | }
|
|---|
| 445 |
|
|---|
| 446 | static fs_index_t fat_index_get(void *node)
|
|---|
| 447 | {
|
|---|
| 448 | fat_node_t *fnodep = (fat_node_t *)node;
|
|---|
| 449 | if (!fnodep)
|
|---|
| 450 | return 0;
|
|---|
| 451 | return fnodep->index;
|
|---|
| 452 | }
|
|---|
| 453 |
|
|---|
| 454 | static size_t fat_size_get(void *node)
|
|---|
| 455 | {
|
|---|
| 456 | return ((fat_node_t *)node)->size;
|
|---|
| 457 | }
|
|---|
| 458 |
|
|---|
| 459 | static unsigned fat_lnkcnt_get(void *node)
|
|---|
| 460 | {
|
|---|
| 461 | return ((fat_node_t *)node)->lnkcnt;
|
|---|
| 462 | }
|
|---|
| 463 |
|
|---|
| 464 | static bool fat_has_children(void *node)
|
|---|
| 465 | {
|
|---|
| 466 | fat_node_t *nodep = (fat_node_t *)node;
|
|---|
| 467 | unsigned bps;
|
|---|
| 468 | unsigned dps;
|
|---|
| 469 | unsigned blocks;
|
|---|
| 470 | block_t *b;
|
|---|
| 471 | unsigned i, j;
|
|---|
| 472 |
|
|---|
| 473 | if (nodep->type != FAT_DIRECTORY)
|
|---|
| 474 | return false;
|
|---|
| 475 |
|
|---|
| 476 | bps = fat_bps_get(nodep->dev_handle);
|
|---|
| 477 | dps = bps / sizeof(fat_dentry_t);
|
|---|
| 478 |
|
|---|
| 479 | blocks = nodep->size / bps + (nodep->size % bps != 0);
|
|---|
| 480 |
|
|---|
| 481 | for (i = 0; i < blocks; i++) {
|
|---|
| 482 | unsigned dentries;
|
|---|
| 483 | fat_dentry_t *d;
|
|---|
| 484 |
|
|---|
| 485 | b = fat_block_get(nodep->dev_handle, nodep->index, i);
|
|---|
| 486 | dentries = (i == blocks - 1) ?
|
|---|
| 487 | nodep->size % sizeof(fat_dentry_t) :
|
|---|
| 488 | dps;
|
|---|
| 489 | for (j = 0; j < dentries; j++) {
|
|---|
| 490 | d = ((fat_dentry_t *)b->data) + j;
|
|---|
| 491 | switch (fat_classify_dentry(d)) {
|
|---|
| 492 | case FAT_DENTRY_SKIP:
|
|---|
| 493 | continue;
|
|---|
| 494 | case FAT_DENTRY_LAST:
|
|---|
| 495 | block_put(b);
|
|---|
| 496 | return false;
|
|---|
| 497 | default:
|
|---|
| 498 | case FAT_DENTRY_VALID:
|
|---|
| 499 | block_put(b);
|
|---|
| 500 | return true;
|
|---|
| 501 | }
|
|---|
| 502 | block_put(b);
|
|---|
| 503 | return true;
|
|---|
| 504 | }
|
|---|
| 505 | block_put(b);
|
|---|
| 506 | }
|
|---|
| 507 |
|
|---|
| 508 | return false;
|
|---|
| 509 | }
|
|---|
| 510 |
|
|---|
| 511 | static void *fat_root_get(dev_handle_t dev_handle)
|
|---|
| 512 | {
|
|---|
| 513 | return fat_node_get(dev_handle, 0, 0);
|
|---|
| 514 | }
|
|---|
| 515 |
|
|---|
| 516 | static char fat_plb_get_char(unsigned pos)
|
|---|
| 517 | {
|
|---|
| 518 | return fat_reg.plb_ro[pos % PLB_SIZE];
|
|---|
| 519 | }
|
|---|
| 520 |
|
|---|
| 521 | static bool fat_is_directory(void *node)
|
|---|
| 522 | {
|
|---|
| 523 | return ((fat_node_t *)node)->type == FAT_DIRECTORY;
|
|---|
| 524 | }
|
|---|
| 525 |
|
|---|
| 526 | static bool fat_is_file(void *node)
|
|---|
| 527 | {
|
|---|
| 528 | return ((fat_node_t *)node)->type == FAT_FILE;
|
|---|
| 529 | }
|
|---|
| 530 |
|
|---|
| 531 | /** libfs operations */
|
|---|
| 532 | libfs_ops_t fat_libfs_ops = {
|
|---|
| 533 | .match = fat_match,
|
|---|
| 534 | .node_get = fat_node_get,
|
|---|
| 535 | .node_put = fat_node_put,
|
|---|
| 536 | .create = fat_create,
|
|---|
| 537 | .destroy = fat_destroy,
|
|---|
| 538 | .link = fat_link,
|
|---|
| 539 | .unlink = fat_unlink,
|
|---|
| 540 | .index_get = fat_index_get,
|
|---|
| 541 | .size_get = fat_size_get,
|
|---|
| 542 | .lnkcnt_get = fat_lnkcnt_get,
|
|---|
| 543 | .has_children = fat_has_children,
|
|---|
| 544 | .root_get = fat_root_get,
|
|---|
| 545 | .plb_get_char = fat_plb_get_char,
|
|---|
| 546 | .is_directory = fat_is_directory,
|
|---|
| 547 | .is_file = fat_is_file
|
|---|
| 548 | };
|
|---|
| 549 |
|
|---|
| 550 | void fat_lookup(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 551 | {
|
|---|
| 552 | libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
|
|---|
| 553 | }
|
|---|
| 554 |
|
|---|
| 555 | /**
|
|---|
| 556 | * @}
|
|---|
| 557 | */
|
|---|