| 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_sync.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 | bn_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, bn_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 | bps = uint16_t_le2host(bs->bps);
|
|---|
| 151 | rscnt = uint16_t_le2host(bs->rscnt);
|
|---|
| 152 | rde = uint16_t_le2host(bs->root_ent_max);
|
|---|
| 153 | sf = uint16_t_le2host(bs->sec_per_fat);
|
|---|
| 154 |
|
|---|
| 155 | rds = (sizeof(fat_dentry_t) * rde) / bps;
|
|---|
| 156 | rds += ((sizeof(fat_dentry_t) * rde) % bps != 0);
|
|---|
| 157 | ssa = rscnt + bs->fatcnt * sf + rds;
|
|---|
| 158 |
|
|---|
| 159 | if (firstc == FAT_CLST_ROOT) {
|
|---|
| 160 | /* root directory special case */
|
|---|
| 161 | assert(bn < rds);
|
|---|
| 162 | rc = block_get(block, dev_handle, rscnt + bs->fatcnt * sf + bn,
|
|---|
| 163 | flags);
|
|---|
| 164 | return rc;
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | max_clusters = bn / bs->spc;
|
|---|
| 168 | rc = fat_cluster_walk(bs, dev_handle, firstc, &lastc, &clusters,
|
|---|
| 169 | max_clusters);
|
|---|
| 170 | if (rc != EOK)
|
|---|
| 171 | return rc;
|
|---|
| 172 | assert(clusters == max_clusters);
|
|---|
| 173 |
|
|---|
| 174 | rc = block_get(block, dev_handle,
|
|---|
| 175 | ssa + (lastc - FAT_CLST_FIRST) * bs->spc + bn % bs->spc, flags);
|
|---|
| 176 |
|
|---|
| 177 | return rc;
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | /** Fill the gap between EOF and a new file position.
|
|---|
| 181 | *
|
|---|
| 182 | * @param bs Buffer holding the boot sector for nodep.
|
|---|
| 183 | * @param nodep FAT node with the gap.
|
|---|
| 184 | * @param mcl First cluster in an independent cluster chain that will
|
|---|
| 185 | * be later appended to the end of the node's own cluster
|
|---|
| 186 | * chain. If pos is still in the last allocated cluster,
|
|---|
| 187 | * this argument is ignored.
|
|---|
| 188 | * @param pos Position in the last node block.
|
|---|
| 189 | *
|
|---|
| 190 | * @return EOK on success or a negative error code.
|
|---|
| 191 | */
|
|---|
| 192 | int fat_fill_gap(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, off_t pos)
|
|---|
| 193 | {
|
|---|
| 194 | uint16_t bps;
|
|---|
| 195 | unsigned spc;
|
|---|
| 196 | block_t *b;
|
|---|
| 197 | off_t o, boundary;
|
|---|
| 198 | int rc;
|
|---|
| 199 |
|
|---|
| 200 | bps = uint16_t_le2host(bs->bps);
|
|---|
| 201 | spc = bs->spc;
|
|---|
| 202 |
|
|---|
| 203 | boundary = ROUND_UP(nodep->size, bps * spc);
|
|---|
| 204 |
|
|---|
| 205 | /* zero out already allocated space */
|
|---|
| 206 | for (o = nodep->size; o < pos && o < boundary;
|
|---|
| 207 | o = ALIGN_DOWN(o + bps, bps)) {
|
|---|
| 208 | int flags = (o % bps == 0) ?
|
|---|
| 209 | BLOCK_FLAGS_NOREAD : BLOCK_FLAGS_NONE;
|
|---|
| 210 | rc = fat_block_get(&b, bs, nodep, o / bps, flags);
|
|---|
| 211 | if (rc != EOK)
|
|---|
| 212 | return rc;
|
|---|
| 213 | memset(b->data + o % bps, 0, bps - o % bps);
|
|---|
| 214 | b->dirty = true; /* need to sync node */
|
|---|
| 215 | rc = block_put(b);
|
|---|
| 216 | if (rc != EOK)
|
|---|
| 217 | return rc;
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | if (o >= pos)
|
|---|
| 221 | return EOK;
|
|---|
| 222 |
|
|---|
| 223 | /* zero out the initial part of the new cluster chain */
|
|---|
| 224 | for (o = boundary; o < pos; o += bps) {
|
|---|
| 225 | rc = _fat_block_get(&b, bs, nodep->idx->dev_handle, mcl,
|
|---|
| 226 | (o - boundary) / bps, BLOCK_FLAGS_NOREAD);
|
|---|
| 227 | if (rc != EOK)
|
|---|
| 228 | return rc;
|
|---|
| 229 | memset(b->data, 0, min(bps, pos - o));
|
|---|
| 230 | b->dirty = true; /* need to sync node */
|
|---|
| 231 | rc = block_put(b);
|
|---|
| 232 | if (rc != EOK)
|
|---|
| 233 | return rc;
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | return EOK;
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | /** Get cluster from the first FAT.
|
|---|
| 240 | *
|
|---|
| 241 | * @param bs Buffer holding the boot sector for the file system.
|
|---|
| 242 | * @param dev_handle Device handle for the file system.
|
|---|
| 243 | * @param clst Cluster which to get.
|
|---|
| 244 | * @param value Output argument holding the value of the cluster.
|
|---|
| 245 | *
|
|---|
| 246 | * @return EOK or a negative error code.
|
|---|
| 247 | */
|
|---|
| 248 | int
|
|---|
| 249 | fat_get_cluster(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t clst,
|
|---|
| 250 | fat_cluster_t *value)
|
|---|
| 251 | {
|
|---|
| 252 | block_t *b;
|
|---|
| 253 | uint16_t bps;
|
|---|
| 254 | uint16_t rscnt;
|
|---|
| 255 | fat_cluster_t *cp;
|
|---|
| 256 | int rc;
|
|---|
| 257 |
|
|---|
| 258 | bps = uint16_t_le2host(bs->bps);
|
|---|
| 259 | rscnt = uint16_t_le2host(bs->rscnt);
|
|---|
| 260 |
|
|---|
| 261 | rc = block_get(&b, dev_handle, rscnt +
|
|---|
| 262 | (clst * sizeof(fat_cluster_t)) / bps, BLOCK_FLAGS_NONE);
|
|---|
| 263 | if (rc != EOK)
|
|---|
| 264 | return rc;
|
|---|
| 265 | cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t));
|
|---|
| 266 | *value = uint16_t_le2host(*cp);
|
|---|
| 267 | rc = block_put(b);
|
|---|
| 268 |
|
|---|
| 269 | return rc;
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 | /** Set cluster in one instance of FAT.
|
|---|
| 273 | *
|
|---|
| 274 | * @param bs Buffer holding the boot sector for the file system.
|
|---|
| 275 | * @param dev_handle Device handle for the file system.
|
|---|
| 276 | * @param fatno Number of the FAT instance where to make the change.
|
|---|
| 277 | * @param clst Cluster which is to be set.
|
|---|
| 278 | * @param value Value to set the cluster with.
|
|---|
| 279 | *
|
|---|
| 280 | * @return EOK on success or a negative error code.
|
|---|
| 281 | */
|
|---|
| 282 | int
|
|---|
| 283 | fat_set_cluster(fat_bs_t *bs, dev_handle_t dev_handle, unsigned fatno,
|
|---|
| 284 | fat_cluster_t clst, fat_cluster_t value)
|
|---|
| 285 | {
|
|---|
| 286 | block_t *b;
|
|---|
| 287 | uint16_t bps;
|
|---|
| 288 | uint16_t rscnt;
|
|---|
| 289 | uint16_t sf;
|
|---|
| 290 | fat_cluster_t *cp;
|
|---|
| 291 | int rc;
|
|---|
| 292 |
|
|---|
| 293 | bps = uint16_t_le2host(bs->bps);
|
|---|
| 294 | rscnt = uint16_t_le2host(bs->rscnt);
|
|---|
| 295 | sf = uint16_t_le2host(bs->sec_per_fat);
|
|---|
| 296 |
|
|---|
| 297 | assert(fatno < bs->fatcnt);
|
|---|
| 298 | rc = block_get(&b, dev_handle, rscnt + sf * fatno +
|
|---|
| 299 | (clst * sizeof(fat_cluster_t)) / bps, BLOCK_FLAGS_NONE);
|
|---|
| 300 | if (rc != EOK)
|
|---|
| 301 | return rc;
|
|---|
| 302 | cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t));
|
|---|
| 303 | *cp = host2uint16_t_le(value);
|
|---|
| 304 | b->dirty = true; /* need to sync block */
|
|---|
| 305 | rc = block_put(b);
|
|---|
| 306 | return rc;
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | /** Replay the allocatoin of clusters in all shadow instances of FAT.
|
|---|
| 310 | *
|
|---|
| 311 | * @param bs Buffer holding the boot sector of the file system.
|
|---|
| 312 | * @param dev_handle Device handle of the file system.
|
|---|
| 313 | * @param lifo Chain of allocated clusters.
|
|---|
| 314 | * @param nclsts Number of clusters in the lifo chain.
|
|---|
| 315 | *
|
|---|
| 316 | * @return EOK on success or a negative error code.
|
|---|
| 317 | */
|
|---|
| 318 | int fat_alloc_shadow_clusters(fat_bs_t *bs, dev_handle_t dev_handle,
|
|---|
| 319 | fat_cluster_t *lifo, unsigned nclsts)
|
|---|
| 320 | {
|
|---|
| 321 | uint8_t fatno;
|
|---|
| 322 | unsigned c;
|
|---|
| 323 | int rc;
|
|---|
| 324 |
|
|---|
| 325 | for (fatno = FAT1 + 1; fatno < bs->fatcnt; fatno++) {
|
|---|
| 326 | for (c = 0; c < nclsts; c++) {
|
|---|
| 327 | rc = fat_set_cluster(bs, dev_handle, fatno, lifo[c],
|
|---|
| 328 | c == 0 ? FAT_CLST_LAST1 : lifo[c - 1]);
|
|---|
| 329 | if (rc != EOK)
|
|---|
| 330 | return rc;
|
|---|
| 331 | }
|
|---|
| 332 | }
|
|---|
| 333 |
|
|---|
| 334 | return EOK;
|
|---|
| 335 | }
|
|---|
| 336 |
|
|---|
| 337 | /** Allocate clusters in all copies of FAT.
|
|---|
| 338 | *
|
|---|
| 339 | * This function will attempt to allocate the requested number of clusters in
|
|---|
| 340 | * all instances of the FAT. The FAT will be altered so that the allocated
|
|---|
| 341 | * clusters form an independent chain (i.e. a chain which does not belong to any
|
|---|
| 342 | * file yet).
|
|---|
| 343 | *
|
|---|
| 344 | * @param bs Buffer holding the boot sector of the file system.
|
|---|
| 345 | * @param dev_handle Device handle of the file system.
|
|---|
| 346 | * @param nclsts Number of clusters to allocate.
|
|---|
| 347 | * @param mcl Output parameter where the first cluster in the chain
|
|---|
| 348 | * will be returned.
|
|---|
| 349 | * @param lcl Output parameter where the last cluster in the chain
|
|---|
| 350 | * will be returned.
|
|---|
| 351 | *
|
|---|
| 352 | * @return EOK on success, a negative error code otherwise.
|
|---|
| 353 | */
|
|---|
| 354 | int
|
|---|
| 355 | fat_alloc_clusters(fat_bs_t *bs, dev_handle_t dev_handle, unsigned nclsts,
|
|---|
| 356 | fat_cluster_t *mcl, fat_cluster_t *lcl)
|
|---|
| 357 | {
|
|---|
| 358 | uint16_t bps;
|
|---|
| 359 | uint16_t rscnt;
|
|---|
| 360 | uint16_t sf;
|
|---|
| 361 | block_t *blk;
|
|---|
| 362 | fat_cluster_t *lifo; /* stack for storing free cluster numbers */
|
|---|
| 363 | unsigned found = 0; /* top of the free cluster number stack */
|
|---|
| 364 | unsigned b, c, cl;
|
|---|
| 365 | int rc;
|
|---|
| 366 |
|
|---|
| 367 | lifo = (fat_cluster_t *) malloc(nclsts * sizeof(fat_cluster_t));
|
|---|
| 368 | if (!lifo)
|
|---|
| 369 | return ENOMEM;
|
|---|
| 370 |
|
|---|
| 371 | bps = uint16_t_le2host(bs->bps);
|
|---|
| 372 | rscnt = uint16_t_le2host(bs->rscnt);
|
|---|
| 373 | sf = uint16_t_le2host(bs->sec_per_fat);
|
|---|
| 374 |
|
|---|
| 375 | /*
|
|---|
| 376 | * Search FAT1 for unused clusters.
|
|---|
| 377 | */
|
|---|
| 378 | fibril_mutex_lock(&fat_alloc_lock);
|
|---|
| 379 | for (b = 0, cl = 0; b < sf; b++) {
|
|---|
| 380 | rc = block_get(&blk, dev_handle, rscnt + b, BLOCK_FLAGS_NONE);
|
|---|
| 381 | for (c = 0; c < bps / sizeof(fat_cluster_t); c++, cl++) {
|
|---|
| 382 | fat_cluster_t *clst = (fat_cluster_t *)blk->data + c;
|
|---|
| 383 | if (uint16_t_le2host(*clst) == FAT_CLST_RES0) {
|
|---|
| 384 | /*
|
|---|
| 385 | * The cluster is free. Put it into our stack
|
|---|
| 386 | * of found clusters and mark it as non-free.
|
|---|
| 387 | */
|
|---|
| 388 | lifo[found] = cl;
|
|---|
| 389 | *clst = (found == 0) ?
|
|---|
| 390 | host2uint16_t_le(FAT_CLST_LAST1) :
|
|---|
| 391 | host2uint16_t_le(lifo[found - 1]);
|
|---|
| 392 | blk->dirty = true; /* need to sync block */
|
|---|
| 393 | if (++found == nclsts) {
|
|---|
| 394 | /* we are almost done */
|
|---|
| 395 | rc = block_put(blk);
|
|---|
| 396 | assert(rc == EOK);
|
|---|
| 397 | /* update the shadow copies of FAT */
|
|---|
| 398 | rc = fat_alloc_shadow_clusters(bs,
|
|---|
| 399 | dev_handle, lifo, nclsts);
|
|---|
| 400 | assert(rc == EOK);
|
|---|
| 401 | *mcl = lifo[found - 1];
|
|---|
| 402 | *lcl = lifo[0];
|
|---|
| 403 | free(lifo);
|
|---|
| 404 | fibril_mutex_unlock(&fat_alloc_lock);
|
|---|
| 405 | return EOK;
|
|---|
| 406 | }
|
|---|
| 407 | }
|
|---|
| 408 | }
|
|---|
| 409 | rc = block_put(blk);
|
|---|
| 410 | assert(rc == EOK);
|
|---|
| 411 | }
|
|---|
| 412 | fibril_mutex_unlock(&fat_alloc_lock);
|
|---|
| 413 |
|
|---|
| 414 | /*
|
|---|
| 415 | * We could not find enough clusters. Now we need to free the clusters
|
|---|
| 416 | * we have allocated so far.
|
|---|
| 417 | */
|
|---|
| 418 | while (found--) {
|
|---|
| 419 | rc = fat_set_cluster(bs, dev_handle, FAT1, lifo[found],
|
|---|
| 420 | FAT_CLST_RES0);
|
|---|
| 421 | if (rc != EOK) {
|
|---|
| 422 | free(lifo);
|
|---|
| 423 | return rc;
|
|---|
| 424 | }
|
|---|
| 425 | }
|
|---|
| 426 |
|
|---|
| 427 | free(lifo);
|
|---|
| 428 | return ENOSPC;
|
|---|
| 429 | }
|
|---|
| 430 |
|
|---|
| 431 | /** Free clusters forming a cluster chain in all copies of FAT.
|
|---|
| 432 | *
|
|---|
| 433 | * @param bs Buffer hodling the boot sector of the file system.
|
|---|
| 434 | * @param dev_handle Device handle of the file system.
|
|---|
| 435 | * @param firstc First cluster in the chain which is to be freed.
|
|---|
| 436 | *
|
|---|
| 437 | * @return EOK on success or a negative return code.
|
|---|
| 438 | */
|
|---|
| 439 | int
|
|---|
| 440 | fat_free_clusters(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc)
|
|---|
| 441 | {
|
|---|
| 442 | unsigned fatno;
|
|---|
| 443 | fat_cluster_t nextc;
|
|---|
| 444 | int rc;
|
|---|
| 445 |
|
|---|
| 446 | /* Mark all clusters in the chain as free in all copies of FAT. */
|
|---|
| 447 | while (firstc < FAT_CLST_LAST1) {
|
|---|
| 448 | assert(firstc >= FAT_CLST_FIRST && firstc < FAT_CLST_BAD);
|
|---|
| 449 | rc = fat_get_cluster(bs, dev_handle, firstc, &nextc);
|
|---|
| 450 | if (rc != EOK)
|
|---|
| 451 | return rc;
|
|---|
| 452 | for (fatno = FAT1; fatno < bs->fatcnt; fatno++) {
|
|---|
| 453 | rc = fat_set_cluster(bs, dev_handle, fatno, firstc,
|
|---|
| 454 | FAT_CLST_RES0);
|
|---|
| 455 | if (rc != EOK)
|
|---|
| 456 | return rc;
|
|---|
| 457 | }
|
|---|
| 458 |
|
|---|
| 459 | firstc = nextc;
|
|---|
| 460 | }
|
|---|
| 461 |
|
|---|
| 462 | return EOK;
|
|---|
| 463 | }
|
|---|
| 464 |
|
|---|
| 465 | /** Append a cluster chain to the last file cluster in all FATs.
|
|---|
| 466 | *
|
|---|
| 467 | * @param bs Buffer holding the boot sector of the file system.
|
|---|
| 468 | * @param nodep Node representing the file.
|
|---|
| 469 | * @param mcl First cluster of the cluster chain to append.
|
|---|
| 470 | *
|
|---|
| 471 | * @return EOK on success or a negative error code.
|
|---|
| 472 | */
|
|---|
| 473 | int fat_append_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl)
|
|---|
| 474 | {
|
|---|
| 475 | dev_handle_t dev_handle = nodep->idx->dev_handle;
|
|---|
| 476 | fat_cluster_t lcl;
|
|---|
| 477 | uint16_t numc;
|
|---|
| 478 | uint8_t fatno;
|
|---|
| 479 | int rc;
|
|---|
| 480 |
|
|---|
| 481 | rc = fat_cluster_walk(bs, dev_handle, nodep->firstc, &lcl, &numc,
|
|---|
| 482 | (uint16_t) -1);
|
|---|
| 483 | if (rc != EOK)
|
|---|
| 484 | return rc;
|
|---|
| 485 |
|
|---|
| 486 | if (numc == 0) {
|
|---|
| 487 | /* No clusters allocated to the node yet. */
|
|---|
| 488 | nodep->firstc = mcl;
|
|---|
| 489 | nodep->dirty = true; /* need to sync node */
|
|---|
| 490 | return EOK;
|
|---|
| 491 | }
|
|---|
| 492 |
|
|---|
| 493 | for (fatno = FAT1; fatno < bs->fatcnt; fatno++) {
|
|---|
| 494 | rc = fat_set_cluster(bs, nodep->idx->dev_handle, fatno, lcl,
|
|---|
| 495 | mcl);
|
|---|
| 496 | if (rc != EOK)
|
|---|
| 497 | return rc;
|
|---|
| 498 | }
|
|---|
| 499 |
|
|---|
| 500 | return EOK;
|
|---|
| 501 | }
|
|---|
| 502 |
|
|---|
| 503 | /** Chop off node clusters in all copies of FAT.
|
|---|
| 504 | *
|
|---|
| 505 | * @param bs Buffer holding the boot sector of the file system.
|
|---|
| 506 | * @param nodep FAT node where the chopping will take place.
|
|---|
| 507 | * @param lastc Last cluster which will remain in the node. If this
|
|---|
| 508 | * argument is FAT_CLST_RES0, then all clusters will
|
|---|
| 509 | * be chopped off.
|
|---|
| 510 | *
|
|---|
| 511 | * @return EOK on success or a negative return code.
|
|---|
| 512 | */
|
|---|
| 513 | int fat_chop_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t lastc)
|
|---|
| 514 | {
|
|---|
| 515 | int rc;
|
|---|
| 516 |
|
|---|
| 517 | dev_handle_t dev_handle = nodep->idx->dev_handle;
|
|---|
| 518 | if (lastc == FAT_CLST_RES0) {
|
|---|
| 519 | /* The node will have zero size and no clusters allocated. */
|
|---|
| 520 | rc = fat_free_clusters(bs, dev_handle, nodep->firstc);
|
|---|
| 521 | if (rc != EOK)
|
|---|
| 522 | return rc;
|
|---|
| 523 | nodep->firstc = FAT_CLST_RES0;
|
|---|
| 524 | nodep->dirty = true; /* need to sync node */
|
|---|
| 525 | } else {
|
|---|
| 526 | fat_cluster_t nextc;
|
|---|
| 527 | unsigned fatno;
|
|---|
| 528 |
|
|---|
| 529 | rc = fat_get_cluster(bs, dev_handle, lastc, &nextc);
|
|---|
| 530 | if (rc != EOK)
|
|---|
| 531 | return rc;
|
|---|
| 532 |
|
|---|
| 533 | /* Terminate the cluster chain in all copies of FAT. */
|
|---|
| 534 | for (fatno = FAT1; fatno < bs->fatcnt; fatno++) {
|
|---|
| 535 | rc = fat_set_cluster(bs, dev_handle, fatno, lastc,
|
|---|
| 536 | FAT_CLST_LAST1);
|
|---|
| 537 | if (rc != EOK)
|
|---|
| 538 | return rc;
|
|---|
| 539 | }
|
|---|
| 540 |
|
|---|
| 541 | /* Free all following clusters. */
|
|---|
| 542 | rc = fat_free_clusters(bs, dev_handle, nextc);
|
|---|
| 543 | if (rc != EOK)
|
|---|
| 544 | return rc;
|
|---|
| 545 | }
|
|---|
| 546 |
|
|---|
| 547 | return EOK;
|
|---|
| 548 | }
|
|---|
| 549 |
|
|---|
| 550 | int
|
|---|
| 551 | fat_zero_cluster(struct fat_bs *bs, dev_handle_t dev_handle, fat_cluster_t c)
|
|---|
| 552 | {
|
|---|
| 553 | int i;
|
|---|
| 554 | block_t *b;
|
|---|
| 555 | unsigned bps;
|
|---|
| 556 | int rc;
|
|---|
| 557 |
|
|---|
| 558 | bps = uint16_t_le2host(bs->bps);
|
|---|
| 559 |
|
|---|
| 560 | for (i = 0; i < bs->spc; i++) {
|
|---|
| 561 | rc = _fat_block_get(&b, bs, dev_handle, c, i,
|
|---|
| 562 | BLOCK_FLAGS_NOREAD);
|
|---|
| 563 | if (rc != EOK)
|
|---|
| 564 | return rc;
|
|---|
| 565 | memset(b->data, 0, bps);
|
|---|
| 566 | b->dirty = true;
|
|---|
| 567 | rc = block_put(b);
|
|---|
| 568 | if (rc != EOK)
|
|---|
| 569 | return rc;
|
|---|
| 570 | }
|
|---|
| 571 |
|
|---|
| 572 | return EOK;
|
|---|
| 573 | }
|
|---|
| 574 |
|
|---|
| 575 | /**
|
|---|
| 576 | * @}
|
|---|
| 577 | */
|
|---|