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