| 1 | /*
|
|---|
| 2 | * Copyright (c) 2008 Jakub Jermar
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup fs
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * @file fat_fat.c
|
|---|
| 35 | * @brief Functions that manipulate the File Allocation Tables.
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| 38 | #include "fat_fat.h"
|
|---|
| 39 | #include "fat_dentry.h"
|
|---|
| 40 | #include "fat.h"
|
|---|
| 41 | #include "../../vfs/vfs.h"
|
|---|
| 42 | #include <libfs.h>
|
|---|
| 43 | #include <libblock.h>
|
|---|
| 44 | #include <errno.h>
|
|---|
| 45 | #include <byteorder.h>
|
|---|
| 46 | #include <align.h>
|
|---|
| 47 | #include <assert.h>
|
|---|
| 48 | #include <fibril_synch.h>
|
|---|
| 49 | #include <mem.h>
|
|---|
| 50 |
|
|---|
| 51 | /**
|
|---|
| 52 | * The fat_alloc_lock mutex protects all copies of the File Allocation Table
|
|---|
| 53 | * during allocation of clusters. The lock does not have to be held durring
|
|---|
| 54 | * deallocation of clusters.
|
|---|
| 55 | */
|
|---|
| 56 | static FIBRIL_MUTEX_INITIALIZE(fat_alloc_lock);
|
|---|
| 57 |
|
|---|
| 58 | /** Walk the cluster chain.
|
|---|
| 59 | *
|
|---|
| 60 | * @param bs Buffer holding the boot sector for the file.
|
|---|
| 61 | * @param dev_handle Device handle of the device with the file.
|
|---|
| 62 | * @param firstc First cluster to start the walk with.
|
|---|
| 63 | * @param lastc If non-NULL, output argument hodling the last cluster
|
|---|
| 64 | * number visited.
|
|---|
| 65 | * @param numc If non-NULL, output argument holding the number of
|
|---|
| 66 | * clusters seen during the walk.
|
|---|
| 67 | * @param max_clusters Maximum number of clusters to visit.
|
|---|
| 68 | *
|
|---|
| 69 | * @return EOK on success or a negative error code.
|
|---|
| 70 | */
|
|---|
| 71 | int
|
|---|
| 72 | fat_cluster_walk(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc,
|
|---|
| 73 | fat_cluster_t *lastc, uint16_t *numc, uint16_t max_clusters)
|
|---|
| 74 | {
|
|---|
| 75 | block_t *b;
|
|---|
| 76 | unsigned bps;
|
|---|
| 77 | unsigned rscnt; /* block address of the first FAT */
|
|---|
| 78 | uint16_t clusters = 0;
|
|---|
| 79 | fat_cluster_t clst = firstc;
|
|---|
| 80 | int rc;
|
|---|
| 81 |
|
|---|
| 82 | bps = uint16_t_le2host(bs->bps);
|
|---|
| 83 | rscnt = uint16_t_le2host(bs->rscnt);
|
|---|
| 84 |
|
|---|
| 85 | if (firstc == FAT_CLST_RES0) {
|
|---|
| 86 | /* No space allocated to the file. */
|
|---|
| 87 | if (lastc)
|
|---|
| 88 | *lastc = firstc;
|
|---|
| 89 | if (numc)
|
|---|
| 90 | *numc = 0;
|
|---|
| 91 | return EOK;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | while (clst < FAT_CLST_LAST1 && clusters < max_clusters) {
|
|---|
| 95 | aoff64_t fsec; /* sector offset relative to FAT1 */
|
|---|
| 96 | unsigned fidx; /* FAT1 entry index */
|
|---|
| 97 |
|
|---|
| 98 | assert(clst >= FAT_CLST_FIRST);
|
|---|
| 99 | if (lastc)
|
|---|
| 100 | *lastc = clst; /* remember the last cluster number */
|
|---|
| 101 | fsec = (clst * sizeof(fat_cluster_t)) / bps;
|
|---|
| 102 | fidx = clst % (bps / sizeof(fat_cluster_t));
|
|---|
| 103 | /* read FAT1 */
|
|---|
| 104 | rc = block_get(&b, dev_handle, rscnt + fsec, BLOCK_FLAGS_NONE);
|
|---|
| 105 | if (rc != EOK)
|
|---|
| 106 | return rc;
|
|---|
| 107 | clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]);
|
|---|
| 108 | assert(clst != FAT_CLST_BAD);
|
|---|
| 109 | rc = block_put(b);
|
|---|
| 110 | if (rc != EOK)
|
|---|
| 111 | return rc;
|
|---|
| 112 | clusters++;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | if (lastc && clst < FAT_CLST_LAST1)
|
|---|
| 116 | *lastc = clst;
|
|---|
| 117 | if (numc)
|
|---|
| 118 | *numc = clusters;
|
|---|
| 119 |
|
|---|
| 120 | return EOK;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | /** Read block from file located on a FAT file system.
|
|---|
| 124 | *
|
|---|
| 125 | * @param block Pointer to a block pointer for storing result.
|
|---|
| 126 | * @param bs Buffer holding the boot sector of the file system.
|
|---|
| 127 | * @param dev_handle Device handle of the file system.
|
|---|
| 128 | * @param firstc First cluster used by the file. Can be zero if the file
|
|---|
| 129 | * is empty.
|
|---|
| 130 | * @param bn Block number.
|
|---|
| 131 | * @param flags Flags passed to libblock.
|
|---|
| 132 | *
|
|---|
| 133 | * @return EOK on success or a negative error code.
|
|---|
| 134 | */
|
|---|
| 135 | int
|
|---|
| 136 | _fat_block_get(block_t **block, fat_bs_t *bs, dev_handle_t dev_handle,
|
|---|
| 137 | fat_cluster_t firstc, aoff64_t bn, int flags)
|
|---|
| 138 | {
|
|---|
| 139 | unsigned bps;
|
|---|
| 140 | unsigned rscnt; /* block address of the first FAT */
|
|---|
| 141 | unsigned rde;
|
|---|
| 142 | unsigned rds; /* root directory size */
|
|---|
| 143 | unsigned sf;
|
|---|
| 144 | unsigned ssa; /* size of the system area */
|
|---|
| 145 | uint16_t clusters;
|
|---|
| 146 | unsigned max_clusters;
|
|---|
| 147 | fat_cluster_t lastc;
|
|---|
| 148 | int rc;
|
|---|
| 149 |
|
|---|
| 150 | /*
|
|---|
| 151 | * This function can only operate on non-zero length files.
|
|---|
| 152 | */
|
|---|
| 153 | if (firstc == FAT_CLST_RES0)
|
|---|
| 154 | return ELIMIT;
|
|---|
| 155 |
|
|---|
| 156 | bps = uint16_t_le2host(bs->bps);
|
|---|
| 157 | rscnt = uint16_t_le2host(bs->rscnt);
|
|---|
| 158 | rde = uint16_t_le2host(bs->root_ent_max);
|
|---|
| 159 | sf = uint16_t_le2host(bs->sec_per_fat);
|
|---|
| 160 |
|
|---|
| 161 | rds = (sizeof(fat_dentry_t) * rde) / bps;
|
|---|
| 162 | rds += ((sizeof(fat_dentry_t) * rde) % bps != 0);
|
|---|
| 163 | ssa = rscnt + bs->fatcnt * sf + rds;
|
|---|
| 164 |
|
|---|
| 165 | if (firstc == FAT_CLST_ROOT) {
|
|---|
| 166 | /* root directory special case */
|
|---|
| 167 | assert(bn < rds);
|
|---|
| 168 | rc = block_get(block, dev_handle, rscnt + bs->fatcnt * sf + bn,
|
|---|
| 169 | flags);
|
|---|
| 170 | return rc;
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | max_clusters = bn / bs->spc;
|
|---|
| 174 | rc = fat_cluster_walk(bs, dev_handle, firstc, &lastc, &clusters,
|
|---|
| 175 | max_clusters);
|
|---|
| 176 | if (rc != EOK)
|
|---|
| 177 | return rc;
|
|---|
| 178 | assert(clusters == max_clusters);
|
|---|
| 179 |
|
|---|
| 180 | rc = block_get(block, dev_handle,
|
|---|
| 181 | ssa + (lastc - FAT_CLST_FIRST) * bs->spc + bn % bs->spc, flags);
|
|---|
| 182 |
|
|---|
| 183 | return rc;
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | /** Fill the gap between EOF and a new file position.
|
|---|
| 187 | *
|
|---|
| 188 | * @param bs Buffer holding the boot sector for nodep.
|
|---|
| 189 | * @param nodep FAT node with the gap.
|
|---|
| 190 | * @param mcl First cluster in an independent cluster chain that will
|
|---|
| 191 | * be later appended to the end of the node's own cluster
|
|---|
| 192 | * chain. If pos is still in the last allocated cluster,
|
|---|
| 193 | * this argument is ignored.
|
|---|
| 194 | * @param pos Position in the last node block.
|
|---|
| 195 | *
|
|---|
| 196 | * @return EOK on success or a negative error code.
|
|---|
| 197 | */
|
|---|
| 198 | int fat_fill_gap(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, aoff64_t pos)
|
|---|
| 199 | {
|
|---|
| 200 | uint16_t bps;
|
|---|
| 201 | unsigned spc;
|
|---|
| 202 | block_t *b;
|
|---|
| 203 | aoff64_t o, boundary;
|
|---|
| 204 | int rc;
|
|---|
| 205 |
|
|---|
| 206 | bps = uint16_t_le2host(bs->bps);
|
|---|
| 207 | spc = bs->spc;
|
|---|
| 208 |
|
|---|
| 209 | boundary = ROUND_UP(nodep->size, bps * spc);
|
|---|
| 210 |
|
|---|
| 211 | /* zero out already allocated space */
|
|---|
| 212 | for (o = nodep->size; o < pos && o < boundary;
|
|---|
| 213 | o = ALIGN_DOWN(o + bps, bps)) {
|
|---|
| 214 | int flags = (o % bps == 0) ?
|
|---|
| 215 | BLOCK_FLAGS_NOREAD : BLOCK_FLAGS_NONE;
|
|---|
| 216 | rc = fat_block_get(&b, bs, nodep, o / bps, flags);
|
|---|
| 217 | if (rc != EOK)
|
|---|
| 218 | return rc;
|
|---|
| 219 | memset(b->data + o % bps, 0, bps - o % bps);
|
|---|
| 220 | b->dirty = true; /* need to sync node */
|
|---|
| 221 | rc = block_put(b);
|
|---|
| 222 | if (rc != EOK)
|
|---|
| 223 | return rc;
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | if (o >= pos)
|
|---|
| 227 | return EOK;
|
|---|
| 228 |
|
|---|
| 229 | /* zero out the initial part of the new cluster chain */
|
|---|
| 230 | for (o = boundary; o < pos; o += bps) {
|
|---|
| 231 | rc = _fat_block_get(&b, bs, nodep->idx->dev_handle, mcl,
|
|---|
| 232 | (o - boundary) / bps, BLOCK_FLAGS_NOREAD);
|
|---|
| 233 | if (rc != EOK)
|
|---|
| 234 | return rc;
|
|---|
| 235 | memset(b->data, 0, min(bps, pos - o));
|
|---|
| 236 | b->dirty = true; /* need to sync node */
|
|---|
| 237 | rc = block_put(b);
|
|---|
| 238 | if (rc != EOK)
|
|---|
| 239 | return rc;
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | return EOK;
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | /** Get cluster from the first FAT.
|
|---|
| 246 | *
|
|---|
| 247 | * @param bs Buffer holding the boot sector for the file system.
|
|---|
| 248 | * @param dev_handle Device handle for the file system.
|
|---|
| 249 | * @param clst Cluster which to get.
|
|---|
| 250 | * @param value Output argument holding the value of the cluster.
|
|---|
| 251 | *
|
|---|
| 252 | * @return EOK or a negative error code.
|
|---|
| 253 | */
|
|---|
| 254 | int
|
|---|
| 255 | fat_get_cluster(fat_bs_t *bs, dev_handle_t dev_handle, unsigned fatno,
|
|---|
| 256 | fat_cluster_t clst, fat_cluster_t *value)
|
|---|
| 257 | {
|
|---|
| 258 | block_t *b;
|
|---|
| 259 | uint16_t bps;
|
|---|
| 260 | uint16_t rscnt;
|
|---|
| 261 | uint16_t sf;
|
|---|
| 262 | fat_cluster_t *cp;
|
|---|
| 263 | int rc;
|
|---|
| 264 |
|
|---|
| 265 | bps = uint16_t_le2host(bs->bps);
|
|---|
| 266 | rscnt = uint16_t_le2host(bs->rscnt);
|
|---|
| 267 | sf = uint16_t_le2host(bs->sec_per_fat);
|
|---|
| 268 |
|
|---|
| 269 | rc = block_get(&b, dev_handle, rscnt + sf * fatno +
|
|---|
| 270 | (clst * sizeof(fat_cluster_t)) / bps, BLOCK_FLAGS_NONE);
|
|---|
| 271 | if (rc != EOK)
|
|---|
| 272 | return rc;
|
|---|
| 273 | cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t));
|
|---|
| 274 | *value = uint16_t_le2host(*cp);
|
|---|
| 275 | rc = block_put(b);
|
|---|
| 276 |
|
|---|
| 277 | return rc;
|
|---|
| 278 | }
|
|---|
| 279 |
|
|---|
| 280 | /** Set cluster in one instance of FAT.
|
|---|
| 281 | *
|
|---|
| 282 | * @param bs Buffer holding the boot sector for the file system.
|
|---|
| 283 | * @param dev_handle Device handle for the file system.
|
|---|
| 284 | * @param fatno Number of the FAT instance where to make the change.
|
|---|
| 285 | * @param clst Cluster which is to be set.
|
|---|
| 286 | * @param value Value to set the cluster with.
|
|---|
| 287 | *
|
|---|
| 288 | * @return EOK on success or a negative error code.
|
|---|
| 289 | */
|
|---|
| 290 | int
|
|---|
| 291 | fat_set_cluster(fat_bs_t *bs, dev_handle_t dev_handle, unsigned fatno,
|
|---|
| 292 | fat_cluster_t clst, fat_cluster_t value)
|
|---|
| 293 | {
|
|---|
| 294 | block_t *b;
|
|---|
| 295 | uint16_t bps;
|
|---|
| 296 | uint16_t rscnt;
|
|---|
| 297 | uint16_t sf;
|
|---|
| 298 | fat_cluster_t *cp;
|
|---|
| 299 | int rc;
|
|---|
| 300 |
|
|---|
| 301 | bps = uint16_t_le2host(bs->bps);
|
|---|
| 302 | rscnt = uint16_t_le2host(bs->rscnt);
|
|---|
| 303 | sf = uint16_t_le2host(bs->sec_per_fat);
|
|---|
| 304 |
|
|---|
| 305 | assert(fatno < bs->fatcnt);
|
|---|
| 306 | rc = block_get(&b, dev_handle, rscnt + sf * fatno +
|
|---|
| 307 | (clst * sizeof(fat_cluster_t)) / bps, BLOCK_FLAGS_NONE);
|
|---|
| 308 | if (rc != EOK)
|
|---|
| 309 | return rc;
|
|---|
| 310 | cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t));
|
|---|
| 311 | *cp = host2uint16_t_le(value);
|
|---|
| 312 | b->dirty = true; /* need to sync block */
|
|---|
| 313 | rc = block_put(b);
|
|---|
| 314 | return rc;
|
|---|
| 315 | }
|
|---|
| 316 |
|
|---|
| 317 | /** Replay the allocatoin of clusters in all shadow instances of FAT.
|
|---|
| 318 | *
|
|---|
| 319 | * @param bs Buffer holding the boot sector of the file system.
|
|---|
| 320 | * @param dev_handle Device handle of the file system.
|
|---|
| 321 | * @param lifo Chain of allocated clusters.
|
|---|
| 322 | * @param nclsts Number of clusters in the lifo chain.
|
|---|
| 323 | *
|
|---|
| 324 | * @return EOK on success or a negative error code.
|
|---|
| 325 | */
|
|---|
| 326 | int fat_alloc_shadow_clusters(fat_bs_t *bs, dev_handle_t dev_handle,
|
|---|
| 327 | fat_cluster_t *lifo, unsigned nclsts)
|
|---|
| 328 | {
|
|---|
| 329 | uint8_t fatno;
|
|---|
| 330 | unsigned c;
|
|---|
| 331 | int rc;
|
|---|
| 332 |
|
|---|
| 333 | for (fatno = FAT1 + 1; fatno < bs->fatcnt; fatno++) {
|
|---|
| 334 | for (c = 0; c < nclsts; c++) {
|
|---|
| 335 | rc = fat_set_cluster(bs, dev_handle, fatno, lifo[c],
|
|---|
| 336 | c == 0 ? FAT_CLST_LAST1 : lifo[c - 1]);
|
|---|
| 337 | if (rc != EOK)
|
|---|
| 338 | return rc;
|
|---|
| 339 | }
|
|---|
| 340 | }
|
|---|
| 341 |
|
|---|
| 342 | return EOK;
|
|---|
| 343 | }
|
|---|
| 344 |
|
|---|
| 345 | /** Allocate clusters in all copies of FAT.
|
|---|
| 346 | *
|
|---|
| 347 | * This function will attempt to allocate the requested number of clusters in
|
|---|
| 348 | * all instances of the FAT. The FAT will be altered so that the allocated
|
|---|
| 349 | * clusters form an independent chain (i.e. a chain which does not belong to any
|
|---|
| 350 | * file yet).
|
|---|
| 351 | *
|
|---|
| 352 | * @param bs Buffer holding the boot sector of the file system.
|
|---|
| 353 | * @param dev_handle Device handle of the file system.
|
|---|
| 354 | * @param nclsts Number of clusters to allocate.
|
|---|
| 355 | * @param mcl Output parameter where the first cluster in the chain
|
|---|
| 356 | * will be returned.
|
|---|
| 357 | * @param lcl Output parameter where the last cluster in the chain
|
|---|
| 358 | * will be returned.
|
|---|
| 359 | *
|
|---|
| 360 | * @return EOK on success, a negative error code otherwise.
|
|---|
| 361 | */
|
|---|
| 362 | int
|
|---|
| 363 | fat_alloc_clusters(fat_bs_t *bs, dev_handle_t dev_handle, unsigned nclsts,
|
|---|
| 364 | fat_cluster_t *mcl, fat_cluster_t *lcl)
|
|---|
| 365 | {
|
|---|
| 366 | uint16_t bps;
|
|---|
| 367 | uint16_t rscnt;
|
|---|
| 368 | uint16_t sf;
|
|---|
| 369 | uint32_t ts;
|
|---|
| 370 | unsigned rde;
|
|---|
| 371 | unsigned rds;
|
|---|
| 372 | unsigned ssa;
|
|---|
| 373 | block_t *blk;
|
|---|
| 374 | fat_cluster_t *lifo; /* stack for storing free cluster numbers */
|
|---|
| 375 | unsigned found = 0; /* top of the free cluster number stack */
|
|---|
| 376 | unsigned b, c, cl;
|
|---|
| 377 | int rc;
|
|---|
| 378 |
|
|---|
| 379 | lifo = (fat_cluster_t *) malloc(nclsts * sizeof(fat_cluster_t));
|
|---|
| 380 | if (!lifo)
|
|---|
| 381 | return ENOMEM;
|
|---|
| 382 |
|
|---|
| 383 | bps = uint16_t_le2host(bs->bps);
|
|---|
| 384 | rscnt = uint16_t_le2host(bs->rscnt);
|
|---|
| 385 | sf = uint16_t_le2host(bs->sec_per_fat);
|
|---|
| 386 | rde = uint16_t_le2host(bs->root_ent_max);
|
|---|
| 387 | ts = (uint32_t) uint16_t_le2host(bs->totsec16);
|
|---|
| 388 | if (ts == 0)
|
|---|
| 389 | ts = uint32_t_le2host(bs->totsec32);
|
|---|
| 390 |
|
|---|
| 391 | rds = (sizeof(fat_dentry_t) * rde) / bps;
|
|---|
| 392 | rds += ((sizeof(fat_dentry_t) * rde) % bps != 0);
|
|---|
| 393 | ssa = rscnt + bs->fatcnt * sf + rds;
|
|---|
| 394 |
|
|---|
| 395 | /*
|
|---|
| 396 | * Search FAT1 for unused clusters.
|
|---|
| 397 | */
|
|---|
| 398 | fibril_mutex_lock(&fat_alloc_lock);
|
|---|
| 399 | for (b = 0, cl = 0; b < sf; b++) {
|
|---|
| 400 | rc = block_get(&blk, dev_handle, rscnt + b, BLOCK_FLAGS_NONE);
|
|---|
| 401 | if (rc != EOK)
|
|---|
| 402 | goto error;
|
|---|
| 403 | for (c = 0; c < bps / sizeof(fat_cluster_t); c++, cl++) {
|
|---|
| 404 | /*
|
|---|
| 405 | * Check if the cluster is physically there. This check
|
|---|
| 406 | * becomes necessary when the file system is created
|
|---|
| 407 | * with fewer total sectors than how many is inferred
|
|---|
| 408 | * from the size of the file allocation table.
|
|---|
| 409 | */
|
|---|
| 410 | if ((cl >= 2) && ((cl - 2) * bs->spc + ssa >= ts)) {
|
|---|
| 411 | rc = block_put(blk);
|
|---|
| 412 | if (rc != EOK)
|
|---|
| 413 | goto error;
|
|---|
| 414 | goto out;
|
|---|
| 415 | }
|
|---|
| 416 |
|
|---|
| 417 | fat_cluster_t *clst = (fat_cluster_t *)blk->data + c;
|
|---|
| 418 | if (uint16_t_le2host(*clst) == FAT_CLST_RES0) {
|
|---|
| 419 | /*
|
|---|
| 420 | * The cluster is free. Put it into our stack
|
|---|
| 421 | * of found clusters and mark it as non-free.
|
|---|
| 422 | */
|
|---|
| 423 | lifo[found] = cl;
|
|---|
| 424 | *clst = (found == 0) ?
|
|---|
| 425 | host2uint16_t_le(FAT_CLST_LAST1) :
|
|---|
| 426 | host2uint16_t_le(lifo[found - 1]);
|
|---|
| 427 | blk->dirty = true; /* need to sync block */
|
|---|
| 428 | if (++found == nclsts) {
|
|---|
| 429 | /* we are almost done */
|
|---|
| 430 | rc = block_put(blk);
|
|---|
| 431 | if (rc != EOK)
|
|---|
| 432 | goto error;
|
|---|
| 433 | /* update the shadow copies of FAT */
|
|---|
| 434 | rc = fat_alloc_shadow_clusters(bs,
|
|---|
| 435 | dev_handle, lifo, nclsts);
|
|---|
| 436 | if (rc != EOK)
|
|---|
| 437 | goto error;
|
|---|
| 438 | *mcl = lifo[found - 1];
|
|---|
| 439 | *lcl = lifo[0];
|
|---|
| 440 | free(lifo);
|
|---|
| 441 | fibril_mutex_unlock(&fat_alloc_lock);
|
|---|
| 442 | return EOK;
|
|---|
| 443 | }
|
|---|
| 444 | }
|
|---|
| 445 | }
|
|---|
| 446 | rc = block_put(blk);
|
|---|
| 447 | if (rc != EOK) {
|
|---|
| 448 | error:
|
|---|
| 449 | fibril_mutex_unlock(&fat_alloc_lock);
|
|---|
| 450 | free(lifo);
|
|---|
| 451 | return rc;
|
|---|
| 452 | }
|
|---|
| 453 | }
|
|---|
| 454 | out:
|
|---|
| 455 | fibril_mutex_unlock(&fat_alloc_lock);
|
|---|
| 456 |
|
|---|
| 457 | /*
|
|---|
| 458 | * We could not find enough clusters. Now we need to free the clusters
|
|---|
| 459 | * we have allocated so far.
|
|---|
| 460 | */
|
|---|
| 461 | while (found--) {
|
|---|
| 462 | rc = fat_set_cluster(bs, dev_handle, FAT1, lifo[found],
|
|---|
| 463 | FAT_CLST_RES0);
|
|---|
| 464 | if (rc != EOK) {
|
|---|
| 465 | free(lifo);
|
|---|
| 466 | return rc;
|
|---|
| 467 | }
|
|---|
| 468 | }
|
|---|
| 469 |
|
|---|
| 470 | free(lifo);
|
|---|
| 471 | return ENOSPC;
|
|---|
| 472 | }
|
|---|
| 473 |
|
|---|
| 474 | /** Free clusters forming a cluster chain in all copies of FAT.
|
|---|
| 475 | *
|
|---|
| 476 | * @param bs Buffer hodling the boot sector of the file system.
|
|---|
| 477 | * @param dev_handle Device handle of the file system.
|
|---|
| 478 | * @param firstc First cluster in the chain which is to be freed.
|
|---|
| 479 | *
|
|---|
| 480 | * @return EOK on success or a negative return code.
|
|---|
| 481 | */
|
|---|
| 482 | int
|
|---|
| 483 | fat_free_clusters(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc)
|
|---|
| 484 | {
|
|---|
| 485 | unsigned fatno;
|
|---|
| 486 | fat_cluster_t nextc;
|
|---|
| 487 | int rc;
|
|---|
| 488 |
|
|---|
| 489 | /* Mark all clusters in the chain as free in all copies of FAT. */
|
|---|
| 490 | while (firstc < FAT_CLST_LAST1) {
|
|---|
| 491 | assert(firstc >= FAT_CLST_FIRST && firstc < FAT_CLST_BAD);
|
|---|
| 492 | rc = fat_get_cluster(bs, dev_handle, FAT1, firstc, &nextc);
|
|---|
| 493 | if (rc != EOK)
|
|---|
| 494 | return rc;
|
|---|
| 495 | for (fatno = FAT1; fatno < bs->fatcnt; fatno++) {
|
|---|
| 496 | rc = fat_set_cluster(bs, dev_handle, fatno, firstc,
|
|---|
| 497 | FAT_CLST_RES0);
|
|---|
| 498 | if (rc != EOK)
|
|---|
| 499 | return rc;
|
|---|
| 500 | }
|
|---|
| 501 |
|
|---|
| 502 | firstc = nextc;
|
|---|
| 503 | }
|
|---|
| 504 |
|
|---|
| 505 | return EOK;
|
|---|
| 506 | }
|
|---|
| 507 |
|
|---|
| 508 | /** Append a cluster chain to the last file cluster in all FATs.
|
|---|
| 509 | *
|
|---|
| 510 | * @param bs Buffer holding the boot sector of the file system.
|
|---|
| 511 | * @param nodep Node representing the file.
|
|---|
| 512 | * @param mcl First cluster of the cluster chain to append.
|
|---|
| 513 | *
|
|---|
| 514 | * @return EOK on success or a negative error code.
|
|---|
| 515 | */
|
|---|
| 516 | int fat_append_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl)
|
|---|
| 517 | {
|
|---|
| 518 | dev_handle_t dev_handle = nodep->idx->dev_handle;
|
|---|
| 519 | fat_cluster_t lcl;
|
|---|
| 520 | uint16_t numc;
|
|---|
| 521 | uint8_t fatno;
|
|---|
| 522 | int rc;
|
|---|
| 523 |
|
|---|
| 524 | rc = fat_cluster_walk(bs, dev_handle, nodep->firstc, &lcl, &numc,
|
|---|
| 525 | (uint16_t) -1);
|
|---|
| 526 | if (rc != EOK)
|
|---|
| 527 | return rc;
|
|---|
| 528 |
|
|---|
| 529 | if (numc == 0) {
|
|---|
| 530 | /* No clusters allocated to the node yet. */
|
|---|
| 531 | nodep->firstc = mcl;
|
|---|
| 532 | nodep->dirty = true; /* need to sync node */
|
|---|
| 533 | return EOK;
|
|---|
| 534 | }
|
|---|
| 535 |
|
|---|
| 536 | for (fatno = FAT1; fatno < bs->fatcnt; fatno++) {
|
|---|
| 537 | rc = fat_set_cluster(bs, nodep->idx->dev_handle, fatno, lcl,
|
|---|
| 538 | mcl);
|
|---|
| 539 | if (rc != EOK)
|
|---|
| 540 | return rc;
|
|---|
| 541 | }
|
|---|
| 542 |
|
|---|
| 543 | return EOK;
|
|---|
| 544 | }
|
|---|
| 545 |
|
|---|
| 546 | /** Chop off node clusters in all copies of FAT.
|
|---|
| 547 | *
|
|---|
| 548 | * @param bs Buffer holding the boot sector of the file system.
|
|---|
| 549 | * @param nodep FAT node where the chopping will take place.
|
|---|
| 550 | * @param lastc Last cluster which will remain in the node. If this
|
|---|
| 551 | * argument is FAT_CLST_RES0, then all clusters will
|
|---|
| 552 | * be chopped off.
|
|---|
| 553 | *
|
|---|
| 554 | * @return EOK on success or a negative return code.
|
|---|
| 555 | */
|
|---|
| 556 | int fat_chop_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t lastc)
|
|---|
| 557 | {
|
|---|
| 558 | int rc;
|
|---|
| 559 |
|
|---|
| 560 | dev_handle_t dev_handle = nodep->idx->dev_handle;
|
|---|
| 561 | if (lastc == FAT_CLST_RES0) {
|
|---|
| 562 | /* The node will have zero size and no clusters allocated. */
|
|---|
| 563 | rc = fat_free_clusters(bs, dev_handle, nodep->firstc);
|
|---|
| 564 | if (rc != EOK)
|
|---|
| 565 | return rc;
|
|---|
| 566 | nodep->firstc = FAT_CLST_RES0;
|
|---|
| 567 | nodep->dirty = true; /* need to sync node */
|
|---|
| 568 | } else {
|
|---|
| 569 | fat_cluster_t nextc;
|
|---|
| 570 | unsigned fatno;
|
|---|
| 571 |
|
|---|
| 572 | rc = fat_get_cluster(bs, dev_handle, FAT1, lastc, &nextc);
|
|---|
| 573 | if (rc != EOK)
|
|---|
| 574 | return rc;
|
|---|
| 575 |
|
|---|
| 576 | /* Terminate the cluster chain in all copies of FAT. */
|
|---|
| 577 | for (fatno = FAT1; fatno < bs->fatcnt; fatno++) {
|
|---|
| 578 | rc = fat_set_cluster(bs, dev_handle, fatno, lastc,
|
|---|
| 579 | FAT_CLST_LAST1);
|
|---|
| 580 | if (rc != EOK)
|
|---|
| 581 | return rc;
|
|---|
| 582 | }
|
|---|
| 583 |
|
|---|
| 584 | /* Free all following clusters. */
|
|---|
| 585 | rc = fat_free_clusters(bs, dev_handle, nextc);
|
|---|
| 586 | if (rc != EOK)
|
|---|
| 587 | return rc;
|
|---|
| 588 | }
|
|---|
| 589 |
|
|---|
| 590 | return EOK;
|
|---|
| 591 | }
|
|---|
| 592 |
|
|---|
| 593 | int
|
|---|
| 594 | fat_zero_cluster(struct fat_bs *bs, dev_handle_t dev_handle, fat_cluster_t c)
|
|---|
| 595 | {
|
|---|
| 596 | int i;
|
|---|
| 597 | block_t *b;
|
|---|
| 598 | unsigned bps;
|
|---|
| 599 | int rc;
|
|---|
| 600 |
|
|---|
| 601 | bps = uint16_t_le2host(bs->bps);
|
|---|
| 602 |
|
|---|
| 603 | for (i = 0; i < bs->spc; i++) {
|
|---|
| 604 | rc = _fat_block_get(&b, bs, dev_handle, c, i,
|
|---|
| 605 | BLOCK_FLAGS_NOREAD);
|
|---|
| 606 | if (rc != EOK)
|
|---|
| 607 | return rc;
|
|---|
| 608 | memset(b->data, 0, bps);
|
|---|
| 609 | b->dirty = true;
|
|---|
| 610 | rc = block_put(b);
|
|---|
| 611 | if (rc != EOK)
|
|---|
| 612 | return rc;
|
|---|
| 613 | }
|
|---|
| 614 |
|
|---|
| 615 | return EOK;
|
|---|
| 616 | }
|
|---|
| 617 |
|
|---|
| 618 | /** Perform basic sanity checks on the file system.
|
|---|
| 619 | *
|
|---|
| 620 | * Verify if values of boot sector fields are sane. Also verify media
|
|---|
| 621 | * descriptor. This is used to rule out cases when a device obviously
|
|---|
| 622 | * does not contain a fat file system.
|
|---|
| 623 | */
|
|---|
| 624 | int fat_sanity_check(fat_bs_t *bs, dev_handle_t dev_handle)
|
|---|
| 625 | {
|
|---|
| 626 | fat_cluster_t e0, e1;
|
|---|
| 627 | unsigned fat_no;
|
|---|
| 628 | int rc;
|
|---|
| 629 |
|
|---|
| 630 | /* Check number of FATs. */
|
|---|
| 631 | if (bs->fatcnt == 0)
|
|---|
| 632 | return ENOTSUP;
|
|---|
| 633 |
|
|---|
| 634 | /* Check total number of sectors. */
|
|---|
| 635 |
|
|---|
| 636 | if (bs->totsec16 == 0 && bs->totsec32 == 0)
|
|---|
| 637 | return ENOTSUP;
|
|---|
| 638 |
|
|---|
| 639 | if (bs->totsec16 != 0 && bs->totsec32 != 0 &&
|
|---|
| 640 | bs->totsec16 != bs->totsec32)
|
|---|
| 641 | return ENOTSUP;
|
|---|
| 642 |
|
|---|
| 643 | /* Check media descriptor. Must be between 0xf0 and 0xff. */
|
|---|
| 644 | if ((bs->mdesc & 0xf0) != 0xf0)
|
|---|
| 645 | return ENOTSUP;
|
|---|
| 646 |
|
|---|
| 647 | /* Check number of sectors per FAT. */
|
|---|
| 648 | if (bs->sec_per_fat == 0)
|
|---|
| 649 | return ENOTSUP;
|
|---|
| 650 |
|
|---|
| 651 | /*
|
|---|
| 652 | * Check that the root directory entries take up whole blocks.
|
|---|
| 653 | * This check is rather strict, but it allows us to treat the root
|
|---|
| 654 | * directory and non-root directories uniformly in some places.
|
|---|
| 655 | * It can be removed provided that functions such as fat_read() are
|
|---|
| 656 | * sanitized to support file systems with this property.
|
|---|
| 657 | */
|
|---|
| 658 | if ((uint16_t_le2host(bs->root_ent_max) * sizeof(fat_dentry_t)) %
|
|---|
| 659 | uint16_t_le2host(bs->bps) != 0)
|
|---|
| 660 | return ENOTSUP;
|
|---|
| 661 |
|
|---|
| 662 | /* Check signature of each FAT. */
|
|---|
| 663 |
|
|---|
| 664 | for (fat_no = 0; fat_no < bs->fatcnt; fat_no++) {
|
|---|
| 665 | rc = fat_get_cluster(bs, dev_handle, fat_no, 0, &e0);
|
|---|
| 666 | if (rc != EOK)
|
|---|
| 667 | return EIO;
|
|---|
| 668 |
|
|---|
| 669 | rc = fat_get_cluster(bs, dev_handle, fat_no, 1, &e1);
|
|---|
| 670 | if (rc != EOK)
|
|---|
| 671 | return EIO;
|
|---|
| 672 |
|
|---|
| 673 | /* Check that first byte of FAT contains the media descriptor. */
|
|---|
| 674 | if ((e0 & 0xff) != bs->mdesc)
|
|---|
| 675 | return ENOTSUP;
|
|---|
| 676 |
|
|---|
| 677 | /*
|
|---|
| 678 | * Check that remaining bits of the first two entries are
|
|---|
| 679 | * set to one.
|
|---|
| 680 | */
|
|---|
| 681 | if ((e0 >> 8) != 0xff || e1 != 0xffff)
|
|---|
| 682 | return ENOTSUP;
|
|---|
| 683 | }
|
|---|
| 684 |
|
|---|
| 685 | return EOK;
|
|---|
| 686 | }
|
|---|
| 687 |
|
|---|
| 688 | /**
|
|---|
| 689 | * @}
|
|---|
| 690 | */
|
|---|