| 1 | /*
|
|---|
| 2 | * Copyright (c) 2008 Jakub Jermar
|
|---|
| 3 | * Copyright (c) 2011 Oleg Romanenko
|
|---|
| 4 | * All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 7 | * modification, are permitted provided that the following conditions
|
|---|
| 8 | * are met:
|
|---|
| 9 | *
|
|---|
| 10 | * - Redistributions of source code must retain the above copyright
|
|---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 14 | * documentation and/or other materials provided with the distribution.
|
|---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 16 | * derived from this software without specific prior written permission.
|
|---|
| 17 | *
|
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | /** @addtogroup fs
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 |
|
|---|
| 34 | /**
|
|---|
| 35 | * @file fat_fat.c
|
|---|
| 36 | * @brief Functions that manipulate the File Allocation Tables.
|
|---|
| 37 | */
|
|---|
| 38 |
|
|---|
| 39 | #include "fat_fat.h"
|
|---|
| 40 | #include "fat_dentry.h"
|
|---|
| 41 | #include "fat.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 | * Convenience macros for computing some frequently used values from the
|
|---|
| 55 | * primitive boot sector members.
|
|---|
| 56 | */
|
|---|
| 57 | #define CLBN2PBN(bs, cl, bn) \
|
|---|
| 58 | (SSA((bs)) + ((cl) - FAT_CLST_FIRST) * SPC((bs)) + (bn) % SPC((bs)))
|
|---|
| 59 |
|
|---|
| 60 | #define IS_ODD(number) (number & 0x1)
|
|---|
| 61 |
|
|---|
| 62 | /**
|
|---|
| 63 | * The fat_alloc_lock mutex protects all copies of the File Allocation Table
|
|---|
| 64 | * during allocation of clusters. The lock does not have to be held durring
|
|---|
| 65 | * deallocation of clusters.
|
|---|
| 66 | */
|
|---|
| 67 | static FIBRIL_MUTEX_INITIALIZE(fat_alloc_lock);
|
|---|
| 68 |
|
|---|
| 69 | /** Walk the cluster chain.
|
|---|
| 70 | *
|
|---|
| 71 | * @param bs Buffer holding the boot sector for the file.
|
|---|
| 72 | * @param devmap_handle Device handle of the device with the file.
|
|---|
| 73 | * @param firstc First cluster to start the walk with.
|
|---|
| 74 | * @param lastc If non-NULL, output argument hodling the last cluster
|
|---|
| 75 | * number visited.
|
|---|
| 76 | * @param numc If non-NULL, output argument holding the number of
|
|---|
| 77 | * clusters seen during the walk.
|
|---|
| 78 | * @param max_clusters Maximum number of clusters to visit.
|
|---|
| 79 | *
|
|---|
| 80 | * @return EOK on success or a negative error code.
|
|---|
| 81 | */
|
|---|
| 82 | int
|
|---|
| 83 | fat_cluster_walk(fat_bs_t *bs, devmap_handle_t devmap_handle, fat_cluster_t firstc,
|
|---|
| 84 | fat_cluster_t *lastc, uint16_t *numc, uint16_t max_clusters)
|
|---|
| 85 | {
|
|---|
| 86 | uint16_t clusters = 0;
|
|---|
| 87 | fat_cluster_t clst = firstc, clst_last1 = FAT_CLST_LAST1(bs);
|
|---|
| 88 | fat_cluster_t clst_bad = FAT_CLST_BAD(bs);
|
|---|
| 89 | int rc;
|
|---|
| 90 |
|
|---|
| 91 | if (firstc == FAT_CLST_RES0) {
|
|---|
| 92 | /* No space allocated to the file. */
|
|---|
| 93 | if (lastc)
|
|---|
| 94 | *lastc = firstc;
|
|---|
| 95 | if (numc)
|
|---|
| 96 | *numc = 0;
|
|---|
| 97 | return EOK;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | while (clst < clst_last1 && clusters < max_clusters) {
|
|---|
| 101 | assert(clst >= FAT_CLST_FIRST);
|
|---|
| 102 | if (lastc)
|
|---|
| 103 | *lastc = clst; /* remember the last cluster number */
|
|---|
| 104 |
|
|---|
| 105 | /* read FAT1 */
|
|---|
| 106 | rc = fat_get_cluster(bs, devmap_handle, FAT1, clst, &clst);
|
|---|
| 107 | if (rc != EOK)
|
|---|
| 108 | return rc;
|
|---|
| 109 |
|
|---|
| 110 | assert(clst != clst_bad);
|
|---|
| 111 | clusters++;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | if (lastc && clst < clst_last1)
|
|---|
| 115 | *lastc = clst;
|
|---|
| 116 | if (numc)
|
|---|
| 117 | *numc = clusters;
|
|---|
| 118 |
|
|---|
| 119 | return EOK;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | /** Read block from file located on a FAT file system.
|
|---|
| 123 | *
|
|---|
| 124 | * @param block Pointer to a block pointer for storing result.
|
|---|
| 125 | * @param bs Buffer holding the boot sector of the file system.
|
|---|
| 126 | * @param nodep FAT node.
|
|---|
| 127 | * @param bn Block number.
|
|---|
| 128 | * @param flags Flags passed to libblock.
|
|---|
| 129 | *
|
|---|
| 130 | * @return EOK on success or a negative error code.
|
|---|
| 131 | */
|
|---|
| 132 | int
|
|---|
| 133 | fat_block_get(block_t **block, struct fat_bs *bs, fat_node_t *nodep,
|
|---|
| 134 | aoff64_t bn, int flags)
|
|---|
| 135 | {
|
|---|
| 136 | fat_cluster_t firstc = nodep->firstc;
|
|---|
| 137 | fat_cluster_t currc;
|
|---|
| 138 | aoff64_t relbn = bn;
|
|---|
| 139 | int rc;
|
|---|
| 140 |
|
|---|
| 141 | if (!nodep->size)
|
|---|
| 142 | return ELIMIT;
|
|---|
| 143 |
|
|---|
| 144 | if (!FAT_IS_FAT32(bs) && nodep->firstc == FAT_CLST_ROOT)
|
|---|
| 145 | goto fall_through;
|
|---|
| 146 |
|
|---|
| 147 | if (((((nodep->size - 1) / BPS(bs)) / SPC(bs)) == bn / SPC(bs)) &&
|
|---|
| 148 | nodep->lastc_cached_valid) {
|
|---|
| 149 | /*
|
|---|
| 150 | * This is a request to read a block within the last cluster
|
|---|
| 151 | * when fortunately we have the last cluster number cached.
|
|---|
| 152 | */
|
|---|
| 153 | return block_get(block, nodep->idx->devmap_handle,
|
|---|
| 154 | CLBN2PBN(bs, nodep->lastc_cached_value, bn), flags);
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | if (nodep->currc_cached_valid && bn >= nodep->currc_cached_bn) {
|
|---|
| 158 | /*
|
|---|
| 159 | * We can start with the cluster cached by the previous call to
|
|---|
| 160 | * fat_block_get().
|
|---|
| 161 | */
|
|---|
| 162 | firstc = nodep->currc_cached_value;
|
|---|
| 163 | relbn -= (nodep->currc_cached_bn / SPC(bs)) * SPC(bs);
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | fall_through:
|
|---|
| 167 | rc = _fat_block_get(block, bs, nodep->idx->devmap_handle, firstc,
|
|---|
| 168 | &currc, relbn, flags);
|
|---|
| 169 | if (rc != EOK)
|
|---|
| 170 | return rc;
|
|---|
| 171 |
|
|---|
| 172 | /*
|
|---|
| 173 | * Update the "current" cluster cache.
|
|---|
| 174 | */
|
|---|
| 175 | nodep->currc_cached_valid = true;
|
|---|
| 176 | nodep->currc_cached_bn = bn;
|
|---|
| 177 | nodep->currc_cached_value = currc;
|
|---|
| 178 |
|
|---|
| 179 | return rc;
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | /** Read block from file located on a FAT file system.
|
|---|
| 183 | *
|
|---|
| 184 | * @param block Pointer to a block pointer for storing result.
|
|---|
| 185 | * @param bs Buffer holding the boot sector of the file system.
|
|---|
| 186 | * @param devmap_handle Device handle of the file system.
|
|---|
| 187 | * @param fcl First cluster used by the file. Can be zero if the file
|
|---|
| 188 | * is empty.
|
|---|
| 189 | * @param clp If not NULL, address where the cluster containing bn
|
|---|
| 190 | * will be stored.
|
|---|
| 191 | * stored
|
|---|
| 192 | * @param bn Block number.
|
|---|
| 193 | * @param flags Flags passed to libblock.
|
|---|
| 194 | *
|
|---|
| 195 | * @return EOK on success or a negative error code.
|
|---|
| 196 | */
|
|---|
| 197 | int
|
|---|
| 198 | _fat_block_get(block_t **block, fat_bs_t *bs, devmap_handle_t devmap_handle,
|
|---|
| 199 | fat_cluster_t fcl, fat_cluster_t *clp, aoff64_t bn, int flags)
|
|---|
| 200 | {
|
|---|
| 201 | uint16_t clusters;
|
|---|
| 202 | unsigned max_clusters;
|
|---|
| 203 | fat_cluster_t c;
|
|---|
| 204 | int rc;
|
|---|
| 205 |
|
|---|
| 206 | /*
|
|---|
| 207 | * This function can only operate on non-zero length files.
|
|---|
| 208 | */
|
|---|
| 209 | if (fcl == FAT_CLST_RES0)
|
|---|
| 210 | return ELIMIT;
|
|---|
| 211 |
|
|---|
| 212 | if (!FAT_IS_FAT32(bs) && fcl == FAT_CLST_ROOT) {
|
|---|
| 213 | /* root directory special case */
|
|---|
| 214 | assert(bn < RDS(bs));
|
|---|
| 215 | rc = block_get(block, devmap_handle,
|
|---|
| 216 | RSCNT(bs) + FATCNT(bs) * SF(bs) + bn, flags);
|
|---|
| 217 | return rc;
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | max_clusters = bn / SPC(bs);
|
|---|
| 221 | rc = fat_cluster_walk(bs, devmap_handle, fcl, &c, &clusters, max_clusters);
|
|---|
| 222 | if (rc != EOK)
|
|---|
| 223 | return rc;
|
|---|
| 224 | assert(clusters == max_clusters);
|
|---|
| 225 |
|
|---|
| 226 | rc = block_get(block, devmap_handle, CLBN2PBN(bs, c, bn), flags);
|
|---|
| 227 |
|
|---|
| 228 | if (clp)
|
|---|
| 229 | *clp = c;
|
|---|
| 230 |
|
|---|
| 231 | return rc;
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | /** Fill the gap between EOF and a new file position.
|
|---|
| 235 | *
|
|---|
| 236 | * @param bs Buffer holding the boot sector for nodep.
|
|---|
| 237 | * @param nodep FAT node with the gap.
|
|---|
| 238 | * @param mcl First cluster in an independent cluster chain that will
|
|---|
| 239 | * be later appended to the end of the node's own cluster
|
|---|
| 240 | * chain. If pos is still in the last allocated cluster,
|
|---|
| 241 | * this argument is ignored.
|
|---|
| 242 | * @param pos Position in the last node block.
|
|---|
| 243 | *
|
|---|
| 244 | * @return EOK on success or a negative error code.
|
|---|
| 245 | */
|
|---|
| 246 | int fat_fill_gap(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, aoff64_t pos)
|
|---|
| 247 | {
|
|---|
| 248 | block_t *b;
|
|---|
| 249 | aoff64_t o, boundary;
|
|---|
| 250 | int rc;
|
|---|
| 251 |
|
|---|
| 252 | boundary = ROUND_UP(nodep->size, BPS(bs) * SPC(bs));
|
|---|
| 253 |
|
|---|
| 254 | /* zero out already allocated space */
|
|---|
| 255 | for (o = nodep->size; o < pos && o < boundary;
|
|---|
| 256 | o = ALIGN_DOWN(o + BPS(bs), BPS(bs))) {
|
|---|
| 257 | int flags = (o % BPS(bs) == 0) ?
|
|---|
| 258 | BLOCK_FLAGS_NOREAD : BLOCK_FLAGS_NONE;
|
|---|
| 259 | rc = fat_block_get(&b, bs, nodep, o / BPS(bs), flags);
|
|---|
| 260 | if (rc != EOK)
|
|---|
| 261 | return rc;
|
|---|
| 262 | memset(b->data + o % BPS(bs), 0, BPS(bs) - o % BPS(bs));
|
|---|
| 263 | b->dirty = true; /* need to sync node */
|
|---|
| 264 | rc = block_put(b);
|
|---|
| 265 | if (rc != EOK)
|
|---|
| 266 | return rc;
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | if (o >= pos)
|
|---|
| 270 | return EOK;
|
|---|
| 271 |
|
|---|
| 272 | /* zero out the initial part of the new cluster chain */
|
|---|
| 273 | for (o = boundary; o < pos; o += BPS(bs)) {
|
|---|
| 274 | rc = _fat_block_get(&b, bs, nodep->idx->devmap_handle, mcl,
|
|---|
| 275 | NULL, (o - boundary) / BPS(bs), BLOCK_FLAGS_NOREAD);
|
|---|
| 276 | if (rc != EOK)
|
|---|
| 277 | return rc;
|
|---|
| 278 | memset(b->data, 0, min(BPS(bs), pos - o));
|
|---|
| 279 | b->dirty = true; /* need to sync node */
|
|---|
| 280 | rc = block_put(b);
|
|---|
| 281 | if (rc != EOK)
|
|---|
| 282 | return rc;
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | return EOK;
|
|---|
| 286 | }
|
|---|
| 287 |
|
|---|
| 288 | /** Get cluster from the first FAT. FAT12 version
|
|---|
| 289 | *
|
|---|
| 290 | * @param bs Buffer holding the boot sector for the file system.
|
|---|
| 291 | * @param devmap_handle Device handle for the file system.
|
|---|
| 292 | * @param clst Cluster which to get.
|
|---|
| 293 | * @param value Output argument holding the value of the cluster.
|
|---|
| 294 | *
|
|---|
| 295 | * @return EOK or a negative error code.
|
|---|
| 296 | */
|
|---|
| 297 | int
|
|---|
| 298 | fat_get_cluster_fat12(fat_bs_t *bs, devmap_handle_t devmap_handle, unsigned fatno,
|
|---|
| 299 | fat_cluster_t clst, fat_cluster_t *value)
|
|---|
| 300 | {
|
|---|
| 301 | block_t *b, *b1;
|
|---|
| 302 | uint16_t byte1, byte2;
|
|---|
| 303 | aoff64_t offset;
|
|---|
| 304 | int rc;
|
|---|
| 305 |
|
|---|
| 306 | offset = (clst + clst/2);
|
|---|
| 307 | if (offset / BPS(bs) >= SF(bs))
|
|---|
| 308 | return ERANGE;
|
|---|
| 309 |
|
|---|
| 310 | rc = block_get(&b, devmap_handle, RSCNT(bs) + SF(bs) * fatno +
|
|---|
| 311 | offset / BPS(bs), BLOCK_FLAGS_NONE);
|
|---|
| 312 | if (rc != EOK)
|
|---|
| 313 | return rc;
|
|---|
| 314 |
|
|---|
| 315 | byte1 = ((uint8_t*) b->data)[offset % BPS(bs)];
|
|---|
| 316 | /* This cluster access spans a sector boundary. Check only for FAT12 */
|
|---|
| 317 | if ((offset % BPS(bs)) + 1 == BPS(bs)) {
|
|---|
| 318 | /* Is it last sector of FAT? */
|
|---|
| 319 | if (offset / BPS(bs) < SF(bs)) {
|
|---|
| 320 | /* No. Reading next sector */
|
|---|
| 321 | rc = block_get(&b1, devmap_handle, 1 + RSCNT(bs) +
|
|---|
| 322 | SF(bs)*fatno + offset / BPS(bs), BLOCK_FLAGS_NONE);
|
|---|
| 323 | if (rc != EOK) {
|
|---|
| 324 | block_put(b);
|
|---|
| 325 | return rc;
|
|---|
| 326 | }
|
|---|
| 327 | /*
|
|---|
| 328 | * Combining value with last byte of current sector and
|
|---|
| 329 | * first byte of next sector
|
|---|
| 330 | */
|
|---|
| 331 | byte2 = ((uint8_t*) b1->data)[0];
|
|---|
| 332 |
|
|---|
| 333 | rc = block_put(b1);
|
|---|
| 334 | if (rc != EOK) {
|
|---|
| 335 | block_put(b);
|
|---|
| 336 | return rc;
|
|---|
| 337 | }
|
|---|
| 338 | }
|
|---|
| 339 | else {
|
|---|
| 340 | /* Yes. It is last sector of FAT */
|
|---|
| 341 | block_put(b);
|
|---|
| 342 | return ERANGE;
|
|---|
| 343 | }
|
|---|
| 344 | }
|
|---|
| 345 | else
|
|---|
| 346 | byte2 = ((uint8_t*) b->data)[(offset % BPS(bs))+1];
|
|---|
| 347 |
|
|---|
| 348 | #ifdef __BE__
|
|---|
| 349 | *value = byte2 | (byte1 << 8);
|
|---|
| 350 | #else
|
|---|
| 351 | *value = byte1 | (byte2 << 8);
|
|---|
| 352 | #endif
|
|---|
| 353 |
|
|---|
| 354 | *value = uint16_t_le2host(*value);
|
|---|
| 355 | if (IS_ODD(clst))
|
|---|
| 356 | *value = (*value) >> 4;
|
|---|
| 357 | else
|
|---|
| 358 | *value = (*value) & FAT12_MASK;
|
|---|
| 359 |
|
|---|
| 360 | rc = block_put(b);
|
|---|
| 361 | return rc;
|
|---|
| 362 | }
|
|---|
| 363 |
|
|---|
| 364 | /** Get cluster from the first FAT. FAT16 version
|
|---|
| 365 | *
|
|---|
| 366 | * @param bs Buffer holding the boot sector for the file system.
|
|---|
| 367 | * @param devmap_handle Device handle for the file system.
|
|---|
| 368 | * @param clst Cluster which to get.
|
|---|
| 369 | * @param value Output argument holding the value of the cluster.
|
|---|
| 370 | *
|
|---|
| 371 | * @return EOK or a negative error code.
|
|---|
| 372 | */
|
|---|
| 373 | int
|
|---|
| 374 | fat_get_cluster_fat16(fat_bs_t *bs, devmap_handle_t devmap_handle, unsigned fatno,
|
|---|
| 375 | fat_cluster_t clst, fat_cluster_t *value)
|
|---|
| 376 | {
|
|---|
| 377 | block_t *b;
|
|---|
| 378 | aoff64_t offset;
|
|---|
| 379 | int rc;
|
|---|
| 380 |
|
|---|
| 381 | offset = (clst * FAT16_CLST_SIZE);
|
|---|
| 382 |
|
|---|
| 383 | rc = block_get(&b, devmap_handle, RSCNT(bs) + SF(bs) * fatno +
|
|---|
| 384 | offset / BPS(bs), BLOCK_FLAGS_NONE);
|
|---|
| 385 | if (rc != EOK)
|
|---|
| 386 | return rc;
|
|---|
| 387 |
|
|---|
| 388 | *value = uint16_t_le2host(*(uint16_t *)(b->data + offset % BPS(bs)));
|
|---|
| 389 |
|
|---|
| 390 | rc = block_put(b);
|
|---|
| 391 |
|
|---|
| 392 | return rc;
|
|---|
| 393 | }
|
|---|
| 394 |
|
|---|
| 395 | /** Get cluster from the first FAT. FAT32 version
|
|---|
| 396 | *
|
|---|
| 397 | * @param bs Buffer holding the boot sector for the file system.
|
|---|
| 398 | * @param devmap_handle Device handle for the file system.
|
|---|
| 399 | * @param clst Cluster which to get.
|
|---|
| 400 | * @param value Output argument holding the value of the cluster.
|
|---|
| 401 | *
|
|---|
| 402 | * @return EOK or a negative error code.
|
|---|
| 403 | */
|
|---|
| 404 | int
|
|---|
| 405 | fat_get_cluster_fat32(fat_bs_t *bs, devmap_handle_t devmap_handle, unsigned fatno,
|
|---|
| 406 | fat_cluster_t clst, fat_cluster_t *value)
|
|---|
| 407 | {
|
|---|
| 408 | block_t *b;
|
|---|
| 409 | aoff64_t offset;
|
|---|
| 410 | int rc;
|
|---|
| 411 |
|
|---|
| 412 | offset = (clst * FAT32_CLST_SIZE);
|
|---|
| 413 |
|
|---|
| 414 | rc = block_get(&b, devmap_handle, RSCNT(bs) + SF(bs) * fatno +
|
|---|
| 415 | offset / BPS(bs), BLOCK_FLAGS_NONE);
|
|---|
| 416 | if (rc != EOK)
|
|---|
| 417 | return rc;
|
|---|
| 418 |
|
|---|
| 419 | *value = uint32_t_le2host(*(uint32_t *)(b->data + offset % BPS(bs))) & FAT32_MASK;
|
|---|
| 420 |
|
|---|
| 421 | rc = block_put(b);
|
|---|
| 422 |
|
|---|
| 423 | return rc;
|
|---|
| 424 | }
|
|---|
| 425 |
|
|---|
| 426 |
|
|---|
| 427 | /** Get cluster from the first FAT.
|
|---|
| 428 | *
|
|---|
| 429 | * @param bs Buffer holding the boot sector for the file system.
|
|---|
| 430 | * @param devmap_handle Device handle for the file system.
|
|---|
| 431 | * @param clst Cluster which to get.
|
|---|
| 432 | * @param value Output argument holding the value of the cluster.
|
|---|
| 433 | *
|
|---|
| 434 | * @return EOK or a negative error code.
|
|---|
| 435 | */
|
|---|
| 436 | int
|
|---|
| 437 | fat_get_cluster(fat_bs_t *bs, devmap_handle_t devmap_handle, unsigned fatno,
|
|---|
| 438 | fat_cluster_t clst, fat_cluster_t *value)
|
|---|
| 439 | {
|
|---|
| 440 | int rc;
|
|---|
| 441 |
|
|---|
| 442 | assert(fatno < FATCNT(bs));
|
|---|
| 443 |
|
|---|
| 444 | if (FAT_IS_FAT12(bs)) {
|
|---|
| 445 | rc = fat_get_cluster_fat12(bs, devmap_handle, fatno, clst, value);
|
|---|
| 446 | }
|
|---|
| 447 | else {
|
|---|
| 448 | if (FAT_IS_FAT32(bs))
|
|---|
| 449 | rc = fat_get_cluster_fat32(bs, devmap_handle, fatno, clst, value);
|
|---|
| 450 | else
|
|---|
| 451 | rc = fat_get_cluster_fat16(bs, devmap_handle, fatno, clst, value);
|
|---|
| 452 | }
|
|---|
| 453 |
|
|---|
| 454 | return rc;
|
|---|
| 455 | }
|
|---|
| 456 |
|
|---|
| 457 | /** Set cluster in one instance of FAT. FAT12 version.
|
|---|
| 458 | *
|
|---|
| 459 | * @param bs Buffer holding the boot sector for the file system.
|
|---|
| 460 | * @param devmap_handle Device handle for the file system.
|
|---|
| 461 | * @param fatno Number of the FAT instance where to make the change.
|
|---|
| 462 | * @param clst Cluster which is to be set.
|
|---|
| 463 | * @param value Value to set the cluster with.
|
|---|
| 464 | *
|
|---|
| 465 | * @return EOK on success or a negative error code.
|
|---|
| 466 | */
|
|---|
| 467 | int
|
|---|
| 468 | fat_set_cluster_fat12(fat_bs_t *bs, devmap_handle_t devmap_handle, unsigned fatno,
|
|---|
| 469 | fat_cluster_t clst, fat_cluster_t value)
|
|---|
| 470 | {
|
|---|
| 471 | block_t *b, *b1=NULL;
|
|---|
| 472 | aoff64_t offset;
|
|---|
| 473 | uint16_t byte1, byte2;
|
|---|
| 474 | int rc;
|
|---|
| 475 |
|
|---|
| 476 | offset = (clst + clst/2);
|
|---|
| 477 | if (offset / BPS(bs) >= SF(bs))
|
|---|
| 478 | return ERANGE;
|
|---|
| 479 |
|
|---|
| 480 | rc = block_get(&b, devmap_handle, RSCNT(bs) + SF(bs) * fatno +
|
|---|
| 481 | offset / BPS(bs), BLOCK_FLAGS_NONE);
|
|---|
| 482 | if (rc != EOK)
|
|---|
| 483 | return rc;
|
|---|
| 484 |
|
|---|
| 485 | byte1 = ((uint8_t*) b->data)[offset % BPS(bs)];
|
|---|
| 486 | bool border = false;
|
|---|
| 487 | /* This cluster access spans a sector boundary. Check only for FAT12 */
|
|---|
| 488 | if ((offset % BPS(bs))+1 == BPS(bs)) {
|
|---|
| 489 | /* Is it last sector of FAT? */
|
|---|
| 490 | if (offset / BPS(bs) < SF(bs)) {
|
|---|
| 491 | /* No. Reading next sector */
|
|---|
| 492 | rc = block_get(&b1, devmap_handle, 1 + RSCNT(bs) +
|
|---|
| 493 | SF(bs)*fatno + offset / BPS(bs), BLOCK_FLAGS_NONE);
|
|---|
| 494 | if (rc != EOK) {
|
|---|
| 495 | block_put(b);
|
|---|
| 496 | return rc;
|
|---|
| 497 | }
|
|---|
| 498 | /*
|
|---|
| 499 | * Combining value with last byte of current sector and
|
|---|
| 500 | * first byte of next sector
|
|---|
| 501 | */
|
|---|
| 502 | byte2 = ((uint8_t*) b1->data)[0];
|
|---|
| 503 | border = true;
|
|---|
| 504 | }
|
|---|
| 505 | else {
|
|---|
| 506 | /* Yes. It is last sector of fat */
|
|---|
| 507 | block_put(b);
|
|---|
| 508 | return ERANGE;
|
|---|
| 509 | }
|
|---|
| 510 | }
|
|---|
| 511 | else
|
|---|
| 512 | byte2 = ((uint8_t*) b->data)[(offset % BPS(bs))+1];
|
|---|
| 513 |
|
|---|
| 514 | if (IS_ODD(clst)) {
|
|---|
| 515 | byte1 &= 0x0f;
|
|---|
| 516 | byte2 = 0;
|
|---|
| 517 | value = (value << 4);
|
|---|
| 518 | } else {
|
|---|
| 519 | byte1 = 0;
|
|---|
| 520 | byte2 &= 0xf0;
|
|---|
| 521 | value &= FAT12_MASK;
|
|---|
| 522 | }
|
|---|
| 523 |
|
|---|
| 524 | byte1 = byte1 | (value & 0xff);
|
|---|
| 525 | byte2 = byte2 | (value >> 8);
|
|---|
| 526 |
|
|---|
| 527 | ((uint8_t*) b->data)[(offset % BPS(bs))] = byte1;
|
|---|
| 528 | if (border) {
|
|---|
| 529 | ((uint8_t*) b1->data)[0] = byte2;
|
|---|
| 530 |
|
|---|
| 531 | b1->dirty = true;
|
|---|
| 532 | rc = block_put(b1);
|
|---|
| 533 | if (rc != EOK) {
|
|---|
| 534 | block_put(b);
|
|---|
| 535 | return rc;
|
|---|
| 536 | }
|
|---|
| 537 | } else
|
|---|
| 538 | ((uint8_t*) b->data)[(offset % BPS(bs))+1] = byte2;
|
|---|
| 539 |
|
|---|
| 540 | b->dirty = true; /* need to sync block */
|
|---|
| 541 | rc = block_put(b);
|
|---|
| 542 | return rc;
|
|---|
| 543 | }
|
|---|
| 544 |
|
|---|
| 545 | /** Set cluster in one instance of FAT. FAT16 version.
|
|---|
| 546 | *
|
|---|
| 547 | * @param bs Buffer holding the boot sector for the file system.
|
|---|
| 548 | * @param devmap_handle Device handle for the file system.
|
|---|
| 549 | * @param fatno Number of the FAT instance where to make the change.
|
|---|
| 550 | * @param clst Cluster which is to be set.
|
|---|
| 551 | * @param value Value to set the cluster with.
|
|---|
| 552 | *
|
|---|
| 553 | * @return EOK on success or a negative error code.
|
|---|
| 554 | */
|
|---|
| 555 | int
|
|---|
| 556 | fat_set_cluster_fat16(fat_bs_t *bs, devmap_handle_t devmap_handle, unsigned fatno,
|
|---|
| 557 | fat_cluster_t clst, fat_cluster_t value)
|
|---|
| 558 | {
|
|---|
| 559 | block_t *b;
|
|---|
| 560 | aoff64_t offset;
|
|---|
| 561 | int rc;
|
|---|
| 562 |
|
|---|
| 563 | offset = (clst * FAT16_CLST_SIZE);
|
|---|
| 564 |
|
|---|
| 565 | rc = block_get(&b, devmap_handle, RSCNT(bs) + SF(bs) * fatno +
|
|---|
| 566 | offset / BPS(bs), BLOCK_FLAGS_NONE);
|
|---|
| 567 | if (rc != EOK)
|
|---|
| 568 | return rc;
|
|---|
| 569 |
|
|---|
| 570 | *(uint16_t *)(b->data + offset % BPS(bs)) = host2uint16_t_le(value);
|
|---|
| 571 |
|
|---|
| 572 | b->dirty = true; /* need to sync block */
|
|---|
| 573 | rc = block_put(b);
|
|---|
| 574 | return rc;
|
|---|
| 575 | }
|
|---|
| 576 |
|
|---|
| 577 | /** Set cluster in one instance of FAT. FAT32 version.
|
|---|
| 578 | *
|
|---|
| 579 | * @param bs Buffer holding the boot sector for the file system.
|
|---|
| 580 | * @param devmap_handle Device handle for the file system.
|
|---|
| 581 | * @param fatno Number of the FAT instance where to make the change.
|
|---|
| 582 | * @param clst Cluster which is to be set.
|
|---|
| 583 | * @param value Value to set the cluster with.
|
|---|
| 584 | *
|
|---|
| 585 | * @return EOK on success or a negative error code.
|
|---|
| 586 | */
|
|---|
| 587 | int
|
|---|
| 588 | fat_set_cluster_fat32(fat_bs_t *bs, devmap_handle_t devmap_handle, unsigned fatno,
|
|---|
| 589 | fat_cluster_t clst, fat_cluster_t value)
|
|---|
| 590 | {
|
|---|
| 591 | block_t *b;
|
|---|
| 592 | aoff64_t offset;
|
|---|
| 593 | int rc;
|
|---|
| 594 | fat_cluster_t temp;
|
|---|
| 595 |
|
|---|
| 596 | offset = (clst * FAT32_CLST_SIZE);
|
|---|
| 597 |
|
|---|
| 598 | rc = block_get(&b, devmap_handle, RSCNT(bs) + SF(bs) * fatno +
|
|---|
| 599 | offset / BPS(bs), BLOCK_FLAGS_NONE);
|
|---|
| 600 | if (rc != EOK)
|
|---|
| 601 | return rc;
|
|---|
| 602 |
|
|---|
| 603 | temp = uint32_t_le2host(*(uint32_t *)(b->data + offset % BPS(bs)));
|
|---|
| 604 | temp &= 0xf0000000;
|
|---|
| 605 | temp |= (value & FAT32_MASK);
|
|---|
| 606 | *(uint32_t *)(b->data + offset % BPS(bs)) = host2uint32_t_le(temp);
|
|---|
| 607 |
|
|---|
| 608 | b->dirty = true; /* need to sync block */
|
|---|
| 609 | rc = block_put(b);
|
|---|
| 610 | return rc;
|
|---|
| 611 | }
|
|---|
| 612 |
|
|---|
| 613 | /** Set cluster in one instance of FAT.
|
|---|
| 614 | *
|
|---|
| 615 | * @param bs Buffer holding the boot sector for the file system.
|
|---|
| 616 | * @param devmap_handle Device handle for the file system.
|
|---|
| 617 | * @param fatno Number of the FAT instance where to make the change.
|
|---|
| 618 | * @param clst Cluster which is to be set.
|
|---|
| 619 | * @param value Value to set the cluster with.
|
|---|
| 620 | *
|
|---|
| 621 | * @return EOK on success or a negative error code.
|
|---|
| 622 | */
|
|---|
| 623 | int
|
|---|
| 624 | fat_set_cluster(fat_bs_t *bs, devmap_handle_t devmap_handle, unsigned fatno,
|
|---|
| 625 | fat_cluster_t clst, fat_cluster_t value)
|
|---|
| 626 | {
|
|---|
| 627 | int rc;
|
|---|
| 628 |
|
|---|
| 629 | assert(fatno < FATCNT(bs));
|
|---|
| 630 |
|
|---|
| 631 | if (FAT_IS_FAT12(bs))
|
|---|
| 632 | rc = fat_set_cluster_fat12(bs, devmap_handle, fatno, clst, value);
|
|---|
| 633 | else if (FAT_IS_FAT32(bs))
|
|---|
| 634 | rc = fat_set_cluster_fat32(bs, devmap_handle, fatno, clst, value);
|
|---|
| 635 | else
|
|---|
| 636 | rc = fat_set_cluster_fat16(bs, devmap_handle, fatno, clst, value);
|
|---|
| 637 |
|
|---|
| 638 | return rc;
|
|---|
| 639 | }
|
|---|
| 640 |
|
|---|
| 641 | /** Replay the allocatoin of clusters in all shadow instances of FAT.
|
|---|
| 642 | *
|
|---|
| 643 | * @param bs Buffer holding the boot sector of the file system.
|
|---|
| 644 | * @param devmap_handle Device handle of the file system.
|
|---|
| 645 | * @param lifo Chain of allocated clusters.
|
|---|
| 646 | * @param nclsts Number of clusters in the lifo chain.
|
|---|
| 647 | *
|
|---|
| 648 | * @return EOK on success or a negative error code.
|
|---|
| 649 | */
|
|---|
| 650 | int fat_alloc_shadow_clusters(fat_bs_t *bs, devmap_handle_t devmap_handle,
|
|---|
| 651 | fat_cluster_t *lifo, unsigned nclsts)
|
|---|
| 652 | {
|
|---|
| 653 | uint8_t fatno;
|
|---|
| 654 | unsigned c;
|
|---|
| 655 | fat_cluster_t clst_last1 = FAT_CLST_LAST1(bs);
|
|---|
| 656 | int rc;
|
|---|
| 657 |
|
|---|
| 658 | for (fatno = FAT1 + 1; fatno < FATCNT(bs); fatno++) {
|
|---|
| 659 | for (c = 0; c < nclsts; c++) {
|
|---|
| 660 | rc = fat_set_cluster(bs, devmap_handle, fatno, lifo[c],
|
|---|
| 661 | c == 0 ? clst_last1 : lifo[c - 1]);
|
|---|
| 662 | if (rc != EOK)
|
|---|
| 663 | return rc;
|
|---|
| 664 | }
|
|---|
| 665 | }
|
|---|
| 666 |
|
|---|
| 667 | return EOK;
|
|---|
| 668 | }
|
|---|
| 669 |
|
|---|
| 670 | /** Allocate clusters in all copies of FAT.
|
|---|
| 671 | *
|
|---|
| 672 | * This function will attempt to allocate the requested number of clusters in
|
|---|
| 673 | * all instances of the FAT. The FAT will be altered so that the allocated
|
|---|
| 674 | * clusters form an independent chain (i.e. a chain which does not belong to any
|
|---|
| 675 | * file yet).
|
|---|
| 676 | *
|
|---|
| 677 | * @param bs Buffer holding the boot sector of the file system.
|
|---|
| 678 | * @param devmap_handle Device handle of the file system.
|
|---|
| 679 | * @param nclsts Number of clusters to allocate.
|
|---|
| 680 | * @param mcl Output parameter where the first cluster in the chain
|
|---|
| 681 | * will be returned.
|
|---|
| 682 | * @param lcl Output parameter where the last cluster in the chain
|
|---|
| 683 | * will be returned.
|
|---|
| 684 | *
|
|---|
| 685 | * @return EOK on success, a negative error code otherwise.
|
|---|
| 686 | */
|
|---|
| 687 | int
|
|---|
| 688 | fat_alloc_clusters(fat_bs_t *bs, devmap_handle_t devmap_handle, unsigned nclsts,
|
|---|
| 689 | fat_cluster_t *mcl, fat_cluster_t *lcl)
|
|---|
| 690 | {
|
|---|
| 691 | fat_cluster_t *lifo; /* stack for storing free cluster numbers */
|
|---|
| 692 | unsigned found = 0; /* top of the free cluster number stack */
|
|---|
| 693 | fat_cluster_t clst, value, clst_last1 = FAT_CLST_LAST1(bs);
|
|---|
| 694 | int rc = EOK;
|
|---|
| 695 |
|
|---|
| 696 | lifo = (fat_cluster_t *) malloc(nclsts * sizeof(fat_cluster_t));
|
|---|
| 697 | if (!lifo)
|
|---|
| 698 | return ENOMEM;
|
|---|
| 699 | /*
|
|---|
| 700 | * Search FAT1 for unused clusters.
|
|---|
| 701 | */
|
|---|
| 702 | fibril_mutex_lock(&fat_alloc_lock);
|
|---|
| 703 | for (clst=FAT_CLST_FIRST; clst < CC(bs)+2 && found < nclsts; clst++) {
|
|---|
| 704 | rc = fat_get_cluster(bs, devmap_handle, FAT1, clst, &value);
|
|---|
| 705 | if (rc != EOK)
|
|---|
| 706 | break;
|
|---|
| 707 |
|
|---|
| 708 | if (value == FAT_CLST_RES0) {
|
|---|
| 709 | /*
|
|---|
| 710 | * The cluster is free. Put it into our stack
|
|---|
| 711 | * of found clusters and mark it as non-free.
|
|---|
| 712 | */
|
|---|
| 713 | lifo[found] = clst;
|
|---|
| 714 | rc = fat_set_cluster(bs, devmap_handle, FAT1, clst,
|
|---|
| 715 | (found == 0) ? clst_last1 : lifo[found - 1]);
|
|---|
| 716 | if (rc != EOK)
|
|---|
| 717 | break;
|
|---|
| 718 |
|
|---|
| 719 | found++;
|
|---|
| 720 | }
|
|---|
| 721 | }
|
|---|
| 722 |
|
|---|
| 723 | if (rc == EOK && found == nclsts) {
|
|---|
| 724 | rc = fat_alloc_shadow_clusters(bs, devmap_handle, lifo, nclsts);
|
|---|
| 725 | if (rc == EOK) {
|
|---|
| 726 | *mcl = lifo[found - 1];
|
|---|
| 727 | *lcl = lifo[0];
|
|---|
| 728 | free(lifo);
|
|---|
| 729 | fibril_mutex_unlock(&fat_alloc_lock);
|
|---|
| 730 | return EOK;
|
|---|
| 731 | }
|
|---|
| 732 | }
|
|---|
| 733 |
|
|---|
| 734 | /* If something wrong - free the clusters */
|
|---|
| 735 | if (found > 0) {
|
|---|
| 736 | while (found--) {
|
|---|
| 737 | rc = fat_set_cluster(bs, devmap_handle, FAT1, lifo[found],
|
|---|
| 738 | FAT_CLST_RES0);
|
|---|
| 739 | }
|
|---|
| 740 | }
|
|---|
| 741 |
|
|---|
| 742 | free(lifo);
|
|---|
| 743 | fibril_mutex_unlock(&fat_alloc_lock);
|
|---|
| 744 | return ENOSPC;
|
|---|
| 745 | }
|
|---|
| 746 |
|
|---|
| 747 | /** Free clusters forming a cluster chain in all copies of FAT.
|
|---|
| 748 | *
|
|---|
| 749 | * @param bs Buffer hodling the boot sector of the file system.
|
|---|
| 750 | * @param devmap_handle Device handle of the file system.
|
|---|
| 751 | * @param firstc First cluster in the chain which is to be freed.
|
|---|
| 752 | *
|
|---|
| 753 | * @return EOK on success or a negative return code.
|
|---|
| 754 | */
|
|---|
| 755 | int
|
|---|
| 756 | fat_free_clusters(fat_bs_t *bs, devmap_handle_t devmap_handle, fat_cluster_t firstc)
|
|---|
| 757 | {
|
|---|
| 758 | unsigned fatno;
|
|---|
| 759 | fat_cluster_t nextc, clst_bad = FAT_CLST_BAD(bs);
|
|---|
| 760 | int rc;
|
|---|
| 761 |
|
|---|
| 762 | /* Mark all clusters in the chain as free in all copies of FAT. */
|
|---|
| 763 | while (firstc < FAT_CLST_LAST1(bs)) {
|
|---|
| 764 | assert(firstc >= FAT_CLST_FIRST && firstc < clst_bad);
|
|---|
| 765 | rc = fat_get_cluster(bs, devmap_handle, FAT1, firstc, &nextc);
|
|---|
| 766 | if (rc != EOK)
|
|---|
| 767 | return rc;
|
|---|
| 768 | for (fatno = FAT1; fatno < FATCNT(bs); fatno++) {
|
|---|
| 769 | rc = fat_set_cluster(bs, devmap_handle, fatno, firstc,
|
|---|
| 770 | FAT_CLST_RES0);
|
|---|
| 771 | if (rc != EOK)
|
|---|
| 772 | return rc;
|
|---|
| 773 | }
|
|---|
| 774 |
|
|---|
| 775 | firstc = nextc;
|
|---|
| 776 | }
|
|---|
| 777 |
|
|---|
| 778 | return EOK;
|
|---|
| 779 | }
|
|---|
| 780 |
|
|---|
| 781 | /** Append a cluster chain to the last file cluster in all FATs.
|
|---|
| 782 | *
|
|---|
| 783 | * @param bs Buffer holding the boot sector of the file system.
|
|---|
| 784 | * @param nodep Node representing the file.
|
|---|
| 785 | * @param mcl First cluster of the cluster chain to append.
|
|---|
| 786 | * @param lcl Last cluster of the cluster chain to append.
|
|---|
| 787 | *
|
|---|
| 788 | * @return EOK on success or a negative error code.
|
|---|
| 789 | */
|
|---|
| 790 | int
|
|---|
| 791 | fat_append_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl,
|
|---|
| 792 | fat_cluster_t lcl)
|
|---|
| 793 | {
|
|---|
| 794 | devmap_handle_t devmap_handle = nodep->idx->devmap_handle;
|
|---|
| 795 | fat_cluster_t lastc;
|
|---|
| 796 | uint8_t fatno;
|
|---|
| 797 | int rc;
|
|---|
| 798 |
|
|---|
| 799 | if (nodep->firstc == FAT_CLST_RES0) {
|
|---|
| 800 | /* No clusters allocated to the node yet. */
|
|---|
| 801 | nodep->firstc = mcl;
|
|---|
| 802 | nodep->dirty = true; /* need to sync node */
|
|---|
| 803 | } else {
|
|---|
| 804 | if (nodep->lastc_cached_valid) {
|
|---|
| 805 | lastc = nodep->lastc_cached_value;
|
|---|
| 806 | nodep->lastc_cached_valid = false;
|
|---|
| 807 | } else {
|
|---|
| 808 | rc = fat_cluster_walk(bs, devmap_handle, nodep->firstc,
|
|---|
| 809 | &lastc, NULL, (uint16_t) -1);
|
|---|
| 810 | if (rc != EOK)
|
|---|
| 811 | return rc;
|
|---|
| 812 | }
|
|---|
| 813 |
|
|---|
| 814 | for (fatno = FAT1; fatno < FATCNT(bs); fatno++) {
|
|---|
| 815 | rc = fat_set_cluster(bs, nodep->idx->devmap_handle,
|
|---|
| 816 | fatno, lastc, mcl);
|
|---|
| 817 | if (rc != EOK)
|
|---|
| 818 | return rc;
|
|---|
| 819 | }
|
|---|
| 820 | }
|
|---|
| 821 |
|
|---|
| 822 | nodep->lastc_cached_valid = true;
|
|---|
| 823 | nodep->lastc_cached_value = lcl;
|
|---|
| 824 |
|
|---|
| 825 | return EOK;
|
|---|
| 826 | }
|
|---|
| 827 |
|
|---|
| 828 | /** Chop off node clusters in all copies of FAT.
|
|---|
| 829 | *
|
|---|
| 830 | * @param bs Buffer holding the boot sector of the file system.
|
|---|
| 831 | * @param nodep FAT node where the chopping will take place.
|
|---|
| 832 | * @param lcl Last cluster which will remain in the node. If this
|
|---|
| 833 | * argument is FAT_CLST_RES0, then all clusters will
|
|---|
| 834 | * be chopped off.
|
|---|
| 835 | *
|
|---|
| 836 | * @return EOK on success or a negative return code.
|
|---|
| 837 | */
|
|---|
| 838 | int fat_chop_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t lcl)
|
|---|
| 839 | {
|
|---|
| 840 | fat_cluster_t clst_last1 = FAT_CLST_LAST1(bs);
|
|---|
| 841 | int rc;
|
|---|
| 842 | devmap_handle_t devmap_handle = nodep->idx->devmap_handle;
|
|---|
| 843 |
|
|---|
| 844 | /*
|
|---|
| 845 | * Invalidate cached cluster numbers.
|
|---|
| 846 | */
|
|---|
| 847 | nodep->lastc_cached_valid = false;
|
|---|
| 848 | if (nodep->currc_cached_value != lcl)
|
|---|
| 849 | nodep->currc_cached_valid = false;
|
|---|
| 850 |
|
|---|
| 851 | if (lcl == FAT_CLST_RES0) {
|
|---|
| 852 | /* The node will have zero size and no clusters allocated. */
|
|---|
| 853 | rc = fat_free_clusters(bs, devmap_handle, nodep->firstc);
|
|---|
| 854 | if (rc != EOK)
|
|---|
| 855 | return rc;
|
|---|
| 856 | nodep->firstc = FAT_CLST_RES0;
|
|---|
| 857 | nodep->dirty = true; /* need to sync node */
|
|---|
| 858 | } else {
|
|---|
| 859 | fat_cluster_t nextc;
|
|---|
| 860 | unsigned fatno;
|
|---|
| 861 |
|
|---|
| 862 | rc = fat_get_cluster(bs, devmap_handle, FAT1, lcl, &nextc);
|
|---|
| 863 | if (rc != EOK)
|
|---|
| 864 | return rc;
|
|---|
| 865 |
|
|---|
| 866 | /* Terminate the cluster chain in all copies of FAT. */
|
|---|
| 867 | for (fatno = FAT1; fatno < FATCNT(bs); fatno++) {
|
|---|
| 868 | rc = fat_set_cluster(bs, devmap_handle, fatno, lcl,
|
|---|
| 869 | clst_last1);
|
|---|
| 870 | if (rc != EOK)
|
|---|
| 871 | return rc;
|
|---|
| 872 | }
|
|---|
| 873 |
|
|---|
| 874 | /* Free all following clusters. */
|
|---|
| 875 | rc = fat_free_clusters(bs, devmap_handle, nextc);
|
|---|
| 876 | if (rc != EOK)
|
|---|
| 877 | return rc;
|
|---|
| 878 | }
|
|---|
| 879 |
|
|---|
| 880 | /*
|
|---|
| 881 | * Update and re-enable the last cluster cache.
|
|---|
| 882 | */
|
|---|
| 883 | nodep->lastc_cached_valid = true;
|
|---|
| 884 | nodep->lastc_cached_value = lcl;
|
|---|
| 885 |
|
|---|
| 886 | return EOK;
|
|---|
| 887 | }
|
|---|
| 888 |
|
|---|
| 889 | int
|
|---|
| 890 | fat_zero_cluster(struct fat_bs *bs, devmap_handle_t devmap_handle, fat_cluster_t c)
|
|---|
| 891 | {
|
|---|
| 892 | int i;
|
|---|
| 893 | block_t *b;
|
|---|
| 894 | int rc;
|
|---|
| 895 |
|
|---|
| 896 | for (i = 0; i < SPC(bs); i++) {
|
|---|
| 897 | rc = _fat_block_get(&b, bs, devmap_handle, c, NULL, i,
|
|---|
| 898 | BLOCK_FLAGS_NOREAD);
|
|---|
| 899 | if (rc != EOK)
|
|---|
| 900 | return rc;
|
|---|
| 901 | memset(b->data, 0, BPS(bs));
|
|---|
| 902 | b->dirty = true;
|
|---|
| 903 | rc = block_put(b);
|
|---|
| 904 | if (rc != EOK)
|
|---|
| 905 | return rc;
|
|---|
| 906 | }
|
|---|
| 907 |
|
|---|
| 908 | return EOK;
|
|---|
| 909 | }
|
|---|
| 910 |
|
|---|
| 911 | /** Perform basic sanity checks on the file system.
|
|---|
| 912 | *
|
|---|
| 913 | * Verify if values of boot sector fields are sane. Also verify media
|
|---|
| 914 | * descriptor. This is used to rule out cases when a device obviously
|
|---|
| 915 | * does not contain a fat file system.
|
|---|
| 916 | */
|
|---|
| 917 | int fat_sanity_check(fat_bs_t *bs, devmap_handle_t devmap_handle)
|
|---|
| 918 | {
|
|---|
| 919 | fat_cluster_t e0, e1;
|
|---|
| 920 | unsigned fat_no;
|
|---|
| 921 | int rc;
|
|---|
| 922 |
|
|---|
| 923 | /* Check number of FATs. */
|
|---|
| 924 | if (FATCNT(bs) == 0)
|
|---|
| 925 | return ENOTSUP;
|
|---|
| 926 |
|
|---|
| 927 | /* Check total number of sectors. */
|
|---|
| 928 | if (TS(bs) == 0)
|
|---|
| 929 | return ENOTSUP;
|
|---|
| 930 |
|
|---|
| 931 | if (bs->totsec16 != 0 && bs->totsec32 != 0 &&
|
|---|
| 932 | bs->totsec16 != bs->totsec32)
|
|---|
| 933 | return ENOTSUP;
|
|---|
| 934 |
|
|---|
| 935 | /* Check media descriptor. Must be between 0xf0 and 0xff. */
|
|---|
| 936 | if ((bs->mdesc & 0xf0) != 0xf0)
|
|---|
| 937 | return ENOTSUP;
|
|---|
| 938 |
|
|---|
| 939 | /* Check number of sectors per FAT. */
|
|---|
| 940 | if (SF(bs) == 0)
|
|---|
| 941 | return ENOTSUP;
|
|---|
| 942 |
|
|---|
| 943 | /*
|
|---|
| 944 | * Check that the root directory entries take up whole blocks.
|
|---|
| 945 | * This check is rather strict, but it allows us to treat the root
|
|---|
| 946 | * directory and non-root directories uniformly in some places.
|
|---|
| 947 | * It can be removed provided that functions such as fat_read() are
|
|---|
| 948 | * sanitized to support file systems with this property.
|
|---|
| 949 | */
|
|---|
| 950 | if (!FAT_IS_FAT32(bs) && (RDE(bs) * sizeof(fat_dentry_t)) % BPS(bs) != 0)
|
|---|
| 951 | return ENOTSUP;
|
|---|
| 952 |
|
|---|
| 953 | /* Check signature of each FAT. */
|
|---|
| 954 | for (fat_no = 0; fat_no < FATCNT(bs); fat_no++) {
|
|---|
| 955 | rc = fat_get_cluster(bs, devmap_handle, fat_no, 0, &e0);
|
|---|
| 956 | if (rc != EOK)
|
|---|
| 957 | return EIO;
|
|---|
| 958 |
|
|---|
| 959 | rc = fat_get_cluster(bs, devmap_handle, fat_no, 1, &e1);
|
|---|
| 960 | if (rc != EOK)
|
|---|
| 961 | return EIO;
|
|---|
| 962 |
|
|---|
| 963 | /* Check that first byte of FAT contains the media descriptor. */
|
|---|
| 964 | if ((e0 & 0xff) != bs->mdesc)
|
|---|
| 965 | return ENOTSUP;
|
|---|
| 966 |
|
|---|
| 967 | /*
|
|---|
| 968 | * Check that remaining bits of the first two entries are
|
|---|
| 969 | * set to one.
|
|---|
| 970 | */
|
|---|
| 971 | if (!FAT_IS_FAT12(bs) &&
|
|---|
| 972 | ((e0 >> 8) != (FAT_MASK(bs) >> 8) || e1 != FAT_MASK(bs)))
|
|---|
| 973 | return ENOTSUP;
|
|---|
| 974 | }
|
|---|
| 975 |
|
|---|
| 976 | return EOK;
|
|---|
| 977 | }
|
|---|
| 978 |
|
|---|
| 979 | /**
|
|---|
| 980 | * @}
|
|---|
| 981 | */
|
|---|