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