| 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 exfat_fat.c
|
|---|
| 36 | * @brief Functions that manipulate the File Allocation Table.
|
|---|
| 37 | */
|
|---|
| 38 |
|
|---|
| 39 | #include "exfat_fat.h"
|
|---|
| 40 | #include "exfat_bitmap.h"
|
|---|
| 41 | #include "exfat.h"
|
|---|
| 42 | #include "../../vfs/vfs.h"
|
|---|
| 43 | #include <libfs.h>
|
|---|
| 44 | #include <libblock.h>
|
|---|
| 45 | #include <errno.h>
|
|---|
| 46 | #include <byteorder.h>
|
|---|
| 47 | #include <align.h>
|
|---|
| 48 | #include <assert.h>
|
|---|
| 49 | #include <fibril_synch.h>
|
|---|
| 50 | #include <malloc.h>
|
|---|
| 51 | #include <mem.h>
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 | /**
|
|---|
| 55 | * The fat_alloc_lock mutex protects all copies of the File Allocation Table
|
|---|
| 56 | * during allocation of clusters. The lock does not have to be held durring
|
|---|
| 57 | * deallocation of clusters.
|
|---|
| 58 | */
|
|---|
| 59 | static FIBRIL_MUTEX_INITIALIZE(exfat_alloc_lock);
|
|---|
| 60 |
|
|---|
| 61 | /** Walk the cluster chain.
|
|---|
| 62 | *
|
|---|
| 63 | * @param bs Buffer holding the boot sector for the file.
|
|---|
| 64 | * @param service_id Service ID of the device with the file.
|
|---|
| 65 | * @param firstc First cluster to start the walk with.
|
|---|
| 66 | * @param lastc If non-NULL, output argument hodling the last cluster
|
|---|
| 67 | * number visited.
|
|---|
| 68 | * @param numc If non-NULL, output argument holding the number of
|
|---|
| 69 | * clusters seen during the walk.
|
|---|
| 70 | * @param max_clusters Maximum number of clusters to visit.
|
|---|
| 71 | *
|
|---|
| 72 | * @return EOK on success or a negative error code.
|
|---|
| 73 | */
|
|---|
| 74 | int
|
|---|
| 75 | exfat_cluster_walk(exfat_bs_t *bs, service_id_t service_id,
|
|---|
| 76 | exfat_cluster_t firstc, exfat_cluster_t *lastc, uint32_t *numc,
|
|---|
| 77 | uint32_t max_clusters)
|
|---|
| 78 | {
|
|---|
| 79 | uint32_t clusters = 0;
|
|---|
| 80 | exfat_cluster_t clst = firstc;
|
|---|
| 81 | int rc;
|
|---|
| 82 |
|
|---|
| 83 | if (firstc < EXFAT_CLST_FIRST) {
|
|---|
| 84 | /* No space allocated to the file. */
|
|---|
| 85 | if (lastc)
|
|---|
| 86 | *lastc = firstc;
|
|---|
| 87 | if (numc)
|
|---|
| 88 | *numc = 0;
|
|---|
| 89 | return EOK;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | while (clst != EXFAT_CLST_EOF && clusters < max_clusters) {
|
|---|
| 93 | assert(clst >= EXFAT_CLST_FIRST);
|
|---|
| 94 | if (lastc)
|
|---|
| 95 | *lastc = clst; /* remember the last cluster number */
|
|---|
| 96 |
|
|---|
| 97 | rc = exfat_get_cluster(bs, service_id, clst, &clst);
|
|---|
| 98 | if (rc != EOK)
|
|---|
| 99 | return rc;
|
|---|
| 100 |
|
|---|
| 101 | assert(clst != EXFAT_CLST_BAD);
|
|---|
| 102 | clusters++;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | if (lastc && clst != EXFAT_CLST_EOF)
|
|---|
| 106 | *lastc = clst;
|
|---|
| 107 | if (numc)
|
|---|
| 108 | *numc = clusters;
|
|---|
| 109 |
|
|---|
| 110 | return EOK;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | /** Read block from file located on a exFAT file system.
|
|---|
| 114 | *
|
|---|
| 115 | * @param block Pointer to a block pointer for storing result.
|
|---|
| 116 | * @param bs Buffer holding the boot sector of the file system.
|
|---|
| 117 | * @param nodep FAT node.
|
|---|
| 118 | * @param bn Block number.
|
|---|
| 119 | * @param flags Flags passed to libblock.
|
|---|
| 120 | *
|
|---|
| 121 | * @return EOK on success or a negative error code.
|
|---|
| 122 | */
|
|---|
| 123 | int
|
|---|
| 124 | exfat_block_get(block_t **block, exfat_bs_t *bs, exfat_node_t *nodep,
|
|---|
| 125 | aoff64_t bn, int flags)
|
|---|
| 126 | {
|
|---|
| 127 | exfat_cluster_t firstc = nodep->firstc;
|
|---|
| 128 | exfat_cluster_t currc;
|
|---|
| 129 | aoff64_t relbn = bn;
|
|---|
| 130 | int rc;
|
|---|
| 131 |
|
|---|
| 132 | if (!nodep->size)
|
|---|
| 133 | return ELIMIT;
|
|---|
| 134 |
|
|---|
| 135 | if (nodep->fragmented) {
|
|---|
| 136 | if (((((nodep->size - 1) / BPS(bs)) / SPC(bs)) == bn / SPC(bs)) &&
|
|---|
| 137 | nodep->lastc_cached_valid) {
|
|---|
| 138 | /*
|
|---|
| 139 | * This is a request to read a block within the last cluster
|
|---|
| 140 | * when fortunately we have the last cluster number cached.
|
|---|
| 141 | */
|
|---|
| 142 | return block_get(block, nodep->idx->service_id, DATA_FS(bs) +
|
|---|
| 143 | (nodep->lastc_cached_value-EXFAT_CLST_FIRST)*SPC(bs) +
|
|---|
| 144 | (bn % SPC(bs)), flags);
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | if (nodep->currc_cached_valid && bn >= nodep->currc_cached_bn) {
|
|---|
| 148 | /*
|
|---|
| 149 | * We can start with the cluster cached by the previous call to
|
|---|
| 150 | * fat_block_get().
|
|---|
| 151 | */
|
|---|
| 152 | firstc = nodep->currc_cached_value;
|
|---|
| 153 | relbn -= (nodep->currc_cached_bn / SPC(bs)) * SPC(bs);
|
|---|
| 154 | }
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | rc = exfat_block_get_by_clst(block, bs, nodep->idx->service_id,
|
|---|
| 158 | nodep->fragmented, firstc, &currc, relbn, flags);
|
|---|
| 159 | if (rc != EOK)
|
|---|
| 160 | return rc;
|
|---|
| 161 |
|
|---|
| 162 | /*
|
|---|
| 163 | * Update the "current" cluster cache.
|
|---|
| 164 | */
|
|---|
| 165 | nodep->currc_cached_valid = true;
|
|---|
| 166 | nodep->currc_cached_bn = bn;
|
|---|
| 167 | nodep->currc_cached_value = currc;
|
|---|
| 168 |
|
|---|
| 169 | return rc;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | /** Read block from file located on a exFAT file system.
|
|---|
| 173 | *
|
|---|
| 174 | * @param block Pointer to a block pointer for storing result.
|
|---|
| 175 | * @param bs Buffer holding the boot sector of the file system.
|
|---|
| 176 | * @param service_id Service ID of the file system.
|
|---|
| 177 | * @param fcl First cluster used by the file. Can be zero if the file
|
|---|
| 178 | * is empty.
|
|---|
| 179 | * @param clp If not NULL, address where the cluster containing bn
|
|---|
| 180 | * will be stored.
|
|---|
| 181 | * stored
|
|---|
| 182 | * @param bn Block number.
|
|---|
| 183 | * @param flags Flags passed to libblock.
|
|---|
| 184 | *
|
|---|
| 185 | * @return EOK on success or a negative error code.
|
|---|
| 186 | */
|
|---|
| 187 | int
|
|---|
| 188 | exfat_block_get_by_clst(block_t **block, exfat_bs_t *bs,
|
|---|
| 189 | service_id_t service_id, bool fragmented, exfat_cluster_t fcl,
|
|---|
| 190 | exfat_cluster_t *clp, aoff64_t bn, int flags)
|
|---|
| 191 | {
|
|---|
| 192 | uint32_t clusters;
|
|---|
| 193 | uint32_t max_clusters;
|
|---|
| 194 | exfat_cluster_t c;
|
|---|
| 195 | int rc;
|
|---|
| 196 |
|
|---|
| 197 | if (fcl < EXFAT_CLST_FIRST || fcl > DATA_CNT(bs) + 2)
|
|---|
| 198 | return ELIMIT;
|
|---|
| 199 |
|
|---|
| 200 | if (!fragmented) {
|
|---|
| 201 | rc = block_get(block, service_id, DATA_FS(bs) +
|
|---|
| 202 | (fcl - EXFAT_CLST_FIRST)*SPC(bs) + bn, flags);
|
|---|
| 203 | } else {
|
|---|
| 204 | max_clusters = bn / SPC(bs);
|
|---|
| 205 | rc = exfat_cluster_walk(bs, service_id, fcl, &c, &clusters, max_clusters);
|
|---|
| 206 | if (rc != EOK)
|
|---|
| 207 | return rc;
|
|---|
| 208 | assert(clusters == max_clusters);
|
|---|
| 209 |
|
|---|
| 210 | rc = block_get(block, service_id, DATA_FS(bs) +
|
|---|
| 211 | (c - EXFAT_CLST_FIRST) * SPC(bs) + (bn % SPC(bs)), flags);
|
|---|
| 212 |
|
|---|
| 213 | if (clp)
|
|---|
| 214 | *clp = c;
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | return rc;
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 | /** Get cluster from the FAT.
|
|---|
| 222 | *
|
|---|
| 223 | * @param bs Buffer holding the boot sector for the file system.
|
|---|
| 224 | * @param service_id Service ID for the file system.
|
|---|
| 225 | * @param clst Cluster which to get.
|
|---|
| 226 | * @param value Output argument holding the value of the cluster.
|
|---|
| 227 | *
|
|---|
| 228 | * @return EOK or a negative error code.
|
|---|
| 229 | */
|
|---|
| 230 | int
|
|---|
| 231 | exfat_get_cluster(exfat_bs_t *bs, service_id_t service_id,
|
|---|
| 232 | exfat_cluster_t clst, exfat_cluster_t *value)
|
|---|
| 233 | {
|
|---|
| 234 | block_t *b;
|
|---|
| 235 | aoff64_t offset;
|
|---|
| 236 | int rc;
|
|---|
| 237 |
|
|---|
| 238 | offset = clst * sizeof(exfat_cluster_t);
|
|---|
| 239 |
|
|---|
| 240 | rc = block_get(&b, service_id, FAT_FS(bs) + offset / BPS(bs), BLOCK_FLAGS_NONE);
|
|---|
| 241 | if (rc != EOK)
|
|---|
| 242 | return rc;
|
|---|
| 243 |
|
|---|
| 244 | *value = uint32_t_le2host(*(uint32_t *)(b->data + offset % BPS(bs)));
|
|---|
| 245 |
|
|---|
| 246 | rc = block_put(b);
|
|---|
| 247 |
|
|---|
| 248 | return rc;
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | /** Set cluster in FAT.
|
|---|
| 252 | *
|
|---|
| 253 | * @param bs Buffer holding the boot sector for the file system.
|
|---|
| 254 | * @param service_id Service ID for the file system.
|
|---|
| 255 | * @param clst Cluster which is to be set.
|
|---|
| 256 | * @param value Value to set the cluster with.
|
|---|
| 257 | *
|
|---|
| 258 | * @return EOK on success or a negative error code.
|
|---|
| 259 | */
|
|---|
| 260 | int
|
|---|
| 261 | exfat_set_cluster(exfat_bs_t *bs, service_id_t service_id,
|
|---|
| 262 | exfat_cluster_t clst, exfat_cluster_t value)
|
|---|
| 263 | {
|
|---|
| 264 | block_t *b;
|
|---|
| 265 | aoff64_t offset;
|
|---|
| 266 | int rc;
|
|---|
| 267 |
|
|---|
| 268 | offset = clst * sizeof(exfat_cluster_t);
|
|---|
| 269 |
|
|---|
| 270 | rc = block_get(&b, service_id, FAT_FS(bs) + offset / BPS(bs), BLOCK_FLAGS_NONE);
|
|---|
| 271 | if (rc != EOK)
|
|---|
| 272 | return rc;
|
|---|
| 273 |
|
|---|
| 274 | *(uint32_t *)(b->data + offset % BPS(bs)) = host2uint32_t_le(value);
|
|---|
| 275 |
|
|---|
| 276 | b->dirty = true; /* need to sync block */
|
|---|
| 277 | rc = block_put(b);
|
|---|
| 278 | return rc;
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | /** Allocate clusters in FAT.
|
|---|
| 282 | *
|
|---|
| 283 | * This function will attempt to allocate the requested number of clusters in
|
|---|
| 284 | * the FAT. The FAT will be altered so that the allocated
|
|---|
| 285 | * clusters form an independent chain (i.e. a chain which does not belong to any
|
|---|
| 286 | * file yet).
|
|---|
| 287 | *
|
|---|
| 288 | * @param bs Buffer holding the boot sector of the file system.
|
|---|
| 289 | * @param service_id Service ID of the file system.
|
|---|
| 290 | * @param nclsts Number of clusters to allocate.
|
|---|
| 291 | * @param mcl Output parameter where the first cluster in the chain
|
|---|
| 292 | * will be returned.
|
|---|
| 293 | * @param lcl Output parameter where the last cluster in the chain
|
|---|
| 294 | * will be returned.
|
|---|
| 295 | *
|
|---|
| 296 | * @return EOK on success, a negative error code otherwise.
|
|---|
| 297 | */
|
|---|
| 298 | int
|
|---|
| 299 | exfat_alloc_clusters(exfat_bs_t *bs, service_id_t service_id, unsigned nclsts,
|
|---|
| 300 | exfat_cluster_t *mcl, exfat_cluster_t *lcl)
|
|---|
| 301 | {
|
|---|
| 302 | exfat_cluster_t *lifo; /* stack for storing free cluster numbers */
|
|---|
| 303 | unsigned found = 0; /* top of the free cluster number stack */
|
|---|
| 304 | exfat_cluster_t clst;
|
|---|
| 305 | int rc = EOK;
|
|---|
| 306 |
|
|---|
| 307 | lifo = (exfat_cluster_t *) malloc(nclsts * sizeof(exfat_cluster_t));
|
|---|
| 308 | if (!lifo)
|
|---|
| 309 | return ENOMEM;
|
|---|
| 310 |
|
|---|
| 311 | fibril_mutex_lock(&exfat_alloc_lock);
|
|---|
| 312 | for (clst = EXFAT_CLST_FIRST; clst < DATA_CNT(bs) + 2 && found < nclsts;
|
|---|
| 313 | clst++) {
|
|---|
| 314 | /* Need to rewrite because of multiple exfat_bitmap_get calls */
|
|---|
| 315 | if (bitmap_is_free(bs, service_id, clst) == EOK) {
|
|---|
| 316 | /*
|
|---|
| 317 | * The cluster is free. Put it into our stack
|
|---|
| 318 | * of found clusters and mark it as non-free.
|
|---|
| 319 | */
|
|---|
| 320 | lifo[found] = clst;
|
|---|
| 321 | rc = exfat_set_cluster(bs, service_id, clst,
|
|---|
| 322 | (found == 0) ? EXFAT_CLST_EOF : lifo[found - 1]);
|
|---|
| 323 | if (rc != EOK)
|
|---|
| 324 | break;
|
|---|
| 325 | found++;
|
|---|
| 326 | rc = bitmap_set_cluster(bs, service_id, clst);
|
|---|
| 327 | if (rc != EOK)
|
|---|
| 328 | break;
|
|---|
| 329 |
|
|---|
| 330 | }
|
|---|
| 331 | }
|
|---|
| 332 |
|
|---|
| 333 | if (rc == EOK && found == nclsts) {
|
|---|
| 334 | *mcl = lifo[found - 1];
|
|---|
| 335 | *lcl = lifo[0];
|
|---|
| 336 | free(lifo);
|
|---|
| 337 | fibril_mutex_unlock(&exfat_alloc_lock);
|
|---|
| 338 | return EOK;
|
|---|
| 339 | }
|
|---|
| 340 |
|
|---|
| 341 | /* If something wrong - free the clusters */
|
|---|
| 342 | while (found--) {
|
|---|
| 343 | (void) bitmap_clear_cluster(bs, service_id, lifo[found]);
|
|---|
| 344 | (void) exfat_set_cluster(bs, service_id, lifo[found], 0);
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | free(lifo);
|
|---|
| 348 | fibril_mutex_unlock(&exfat_alloc_lock);
|
|---|
| 349 | return ENOSPC;
|
|---|
| 350 | }
|
|---|
| 351 |
|
|---|
| 352 | /** Free clusters forming a cluster chain in FAT.
|
|---|
| 353 | *
|
|---|
| 354 | * @param bs Buffer hodling the boot sector of the file system.
|
|---|
| 355 | * @param service_id Service ID of the file system.
|
|---|
| 356 | * @param firstc First cluster in the chain which is to be freed.
|
|---|
| 357 | *
|
|---|
| 358 | * @return EOK on success or a negative return code.
|
|---|
| 359 | */
|
|---|
| 360 | int
|
|---|
| 361 | exfat_free_clusters(exfat_bs_t *bs, service_id_t service_id, exfat_cluster_t firstc)
|
|---|
| 362 | {
|
|---|
| 363 | exfat_cluster_t nextc;
|
|---|
| 364 | int rc;
|
|---|
| 365 |
|
|---|
| 366 | /* Mark all clusters in the chain as free */
|
|---|
| 367 | while (firstc != EXFAT_CLST_EOF) {
|
|---|
| 368 | assert(firstc >= EXFAT_CLST_FIRST && firstc < EXFAT_CLST_BAD);
|
|---|
| 369 | rc = exfat_get_cluster(bs, service_id, firstc, &nextc);
|
|---|
| 370 | if (rc != EOK)
|
|---|
| 371 | return rc;
|
|---|
| 372 | rc = exfat_set_cluster(bs, service_id, firstc, 0);
|
|---|
| 373 | if (rc != EOK)
|
|---|
| 374 | return rc;
|
|---|
| 375 | rc = bitmap_clear_cluster(bs, service_id, firstc);
|
|---|
| 376 | if (rc != EOK)
|
|---|
| 377 | return rc;
|
|---|
| 378 | firstc = nextc;
|
|---|
| 379 | }
|
|---|
| 380 |
|
|---|
| 381 | return EOK;
|
|---|
| 382 | }
|
|---|
| 383 |
|
|---|
| 384 | /** Append a cluster chain to the last file cluster in FAT.
|
|---|
| 385 | *
|
|---|
| 386 | * @param bs Buffer holding the boot sector of the file system.
|
|---|
| 387 | * @param nodep Node representing the file.
|
|---|
| 388 | * @param mcl First cluster of the cluster chain to append.
|
|---|
| 389 | * @param lcl Last cluster of the cluster chain to append.
|
|---|
| 390 | *
|
|---|
| 391 | * @return EOK on success or a negative error code.
|
|---|
| 392 | */
|
|---|
| 393 | int
|
|---|
| 394 | exfat_append_clusters(exfat_bs_t *bs, exfat_node_t *nodep, exfat_cluster_t mcl,
|
|---|
| 395 | exfat_cluster_t lcl)
|
|---|
| 396 | {
|
|---|
| 397 | service_id_t service_id = nodep->idx->service_id;
|
|---|
| 398 | exfat_cluster_t lastc;
|
|---|
| 399 | int rc;
|
|---|
| 400 |
|
|---|
| 401 | if (nodep->firstc == 0) {
|
|---|
| 402 | /* No clusters allocated to the node yet. */
|
|---|
| 403 | nodep->firstc = mcl;
|
|---|
| 404 | nodep->dirty = true; /* need to sync node */
|
|---|
| 405 | } else {
|
|---|
| 406 | if (nodep->lastc_cached_valid) {
|
|---|
| 407 | lastc = nodep->lastc_cached_value;
|
|---|
| 408 | nodep->lastc_cached_valid = false;
|
|---|
| 409 | } else {
|
|---|
| 410 | rc = exfat_cluster_walk(bs, service_id, nodep->firstc,
|
|---|
| 411 | &lastc, NULL, (uint16_t) -1);
|
|---|
| 412 | if (rc != EOK)
|
|---|
| 413 | return rc;
|
|---|
| 414 | }
|
|---|
| 415 |
|
|---|
| 416 | rc = exfat_set_cluster(bs, nodep->idx->service_id, lastc, mcl);
|
|---|
| 417 | if (rc != EOK)
|
|---|
| 418 | return rc;
|
|---|
| 419 | }
|
|---|
| 420 |
|
|---|
| 421 | nodep->lastc_cached_valid = true;
|
|---|
| 422 | nodep->lastc_cached_value = lcl;
|
|---|
| 423 |
|
|---|
| 424 | return EOK;
|
|---|
| 425 | }
|
|---|
| 426 |
|
|---|
| 427 | /** Chop off node clusters in FAT.
|
|---|
| 428 | *
|
|---|
| 429 | * @param bs Buffer holding the boot sector of the file system.
|
|---|
| 430 | * @param nodep FAT node where the chopping will take place.
|
|---|
| 431 | * @param lcl Last cluster which will remain in the node. If this
|
|---|
| 432 | * argument is FAT_CLST_RES0, then all clusters will
|
|---|
| 433 | * be chopped off.
|
|---|
| 434 | *
|
|---|
| 435 | * @return EOK on success or a negative return code.
|
|---|
| 436 | */
|
|---|
| 437 | int exfat_chop_clusters(exfat_bs_t *bs, exfat_node_t *nodep, exfat_cluster_t lcl)
|
|---|
| 438 | {
|
|---|
| 439 | int rc;
|
|---|
| 440 | service_id_t service_id = nodep->idx->service_id;
|
|---|
| 441 |
|
|---|
| 442 | /*
|
|---|
| 443 | * Invalidate cached cluster numbers.
|
|---|
| 444 | */
|
|---|
| 445 | nodep->lastc_cached_valid = false;
|
|---|
| 446 | if (nodep->currc_cached_value != lcl)
|
|---|
| 447 | nodep->currc_cached_valid = false;
|
|---|
| 448 |
|
|---|
| 449 | if (lcl == 0) {
|
|---|
| 450 | /* The node will have zero size and no clusters allocated. */
|
|---|
| 451 | rc = exfat_free_clusters(bs, service_id, nodep->firstc);
|
|---|
| 452 | if (rc != EOK)
|
|---|
| 453 | return rc;
|
|---|
| 454 | nodep->firstc = 0;
|
|---|
| 455 | nodep->dirty = true; /* need to sync node */
|
|---|
| 456 | } else {
|
|---|
| 457 | exfat_cluster_t nextc;
|
|---|
| 458 |
|
|---|
| 459 | rc = exfat_get_cluster(bs, service_id, lcl, &nextc);
|
|---|
| 460 | if (rc != EOK)
|
|---|
| 461 | return rc;
|
|---|
| 462 |
|
|---|
| 463 | /* Terminate the cluster chain */
|
|---|
| 464 | rc = exfat_set_cluster(bs, service_id, lcl, EXFAT_CLST_EOF);
|
|---|
| 465 | if (rc != EOK)
|
|---|
| 466 | return rc;
|
|---|
| 467 |
|
|---|
| 468 | /* Free all following clusters. */
|
|---|
| 469 | rc = exfat_free_clusters(bs, service_id, nextc);
|
|---|
| 470 | if (rc != EOK)
|
|---|
| 471 | return rc;
|
|---|
| 472 | }
|
|---|
| 473 |
|
|---|
| 474 | /*
|
|---|
| 475 | * Update and re-enable the last cluster cache.
|
|---|
| 476 | */
|
|---|
| 477 | nodep->lastc_cached_valid = true;
|
|---|
| 478 | nodep->lastc_cached_value = lcl;
|
|---|
| 479 |
|
|---|
| 480 | return EOK;
|
|---|
| 481 | }
|
|---|
| 482 |
|
|---|
| 483 | int
|
|---|
| 484 | exfat_zero_cluster(exfat_bs_t *bs, service_id_t service_id, exfat_cluster_t c)
|
|---|
| 485 | {
|
|---|
| 486 | size_t i;
|
|---|
| 487 | block_t *b;
|
|---|
| 488 | int rc;
|
|---|
| 489 |
|
|---|
| 490 | for (i = 0; i < SPC(bs); i++) {
|
|---|
| 491 | rc = exfat_block_get_by_clst(&b, bs, service_id, false, c, NULL,
|
|---|
| 492 | i, BLOCK_FLAGS_NOREAD);
|
|---|
| 493 | if (rc != EOK)
|
|---|
| 494 | return rc;
|
|---|
| 495 | memset(b->data, 0, BPS(bs));
|
|---|
| 496 | b->dirty = true;
|
|---|
| 497 | rc = block_put(b);
|
|---|
| 498 | if (rc != EOK)
|
|---|
| 499 | return rc;
|
|---|
| 500 | }
|
|---|
| 501 |
|
|---|
| 502 | return EOK;
|
|---|
| 503 | }
|
|---|
| 504 |
|
|---|
| 505 | int
|
|---|
| 506 | exfat_read_uctable(exfat_bs_t *bs, exfat_node_t *nodep, uint8_t *uctable)
|
|---|
| 507 | {
|
|---|
| 508 | size_t i, blocks, count;
|
|---|
| 509 | block_t *b;
|
|---|
| 510 | int rc;
|
|---|
| 511 | blocks = ROUND_UP(nodep->size, BPS(bs))/BPS(bs);
|
|---|
| 512 | count = BPS(bs);
|
|---|
| 513 |
|
|---|
| 514 | for (i = 0; i < blocks; i++) {
|
|---|
| 515 | rc = exfat_block_get(&b, bs, nodep, i, BLOCK_FLAGS_NOREAD);
|
|---|
| 516 | if (rc != EOK)
|
|---|
| 517 | return rc;
|
|---|
| 518 | if (i == blocks - 1)
|
|---|
| 519 | count = nodep->size - i * BPS(bs);
|
|---|
| 520 | memcpy(uctable, b->data, count);
|
|---|
| 521 | uctable += count;
|
|---|
| 522 | rc = block_put(b);
|
|---|
| 523 | if (rc != EOK)
|
|---|
| 524 | return rc;
|
|---|
| 525 | }
|
|---|
| 526 |
|
|---|
| 527 | return EOK;
|
|---|
| 528 | }
|
|---|
| 529 |
|
|---|
| 530 |
|
|---|
| 531 | /** Perform basic sanity checks on the file system.
|
|---|
| 532 | *
|
|---|
| 533 | * Verify if values of boot sector fields are sane. Also verify media
|
|---|
| 534 | * descriptor. This is used to rule out cases when a device obviously
|
|---|
| 535 | * does not contain a exfat file system.
|
|---|
| 536 | */
|
|---|
| 537 | int exfat_sanity_check(exfat_bs_t *bs, service_id_t service_id)
|
|---|
| 538 | {
|
|---|
| 539 | /* TODO */
|
|---|
| 540 | return EOK;
|
|---|
| 541 | }
|
|---|
| 542 |
|
|---|
| 543 | /**
|
|---|
| 544 | * @}
|
|---|
| 545 | */
|
|---|