[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 | * @{
|
---|
| 31 | */
|
---|
| 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>
|
---|
[6ebe721] | 48 | #include <fibril_sync.h>
|
---|
[c20aa06] | 49 | #include <mem.h>
|
---|
[0ec862d] | 50 |
|
---|
| 51 | /**
|
---|
[6ebe721] | 52 | * The fat_alloc_lock mutex protects all copies of the File Allocation Table
|
---|
[0ec862d] | 53 | * during allocation of clusters. The lock does not have to be held durring
|
---|
| 54 | * deallocation of clusters.
|
---|
| 55 | */
|
---|
[6ebe721] | 56 | static FIBRIL_MUTEX_INITIALIZE(fat_alloc_lock);
|
---|
[0f57d0e] | 57 |
|
---|
[4f1c0b4] | 58 | /** Walk the cluster chain.
|
---|
[9f95a80] | 59 | *
|
---|
[4f1c0b4] | 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.
|
---|
[e402382] | 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.
|
---|
[4f1c0b4] | 67 | * @param max_clusters Maximum number of clusters to visit.
|
---|
[9f95a80] | 68 | *
|
---|
[e402382] | 69 | * @return EOK on success or a negative error code.
|
---|
[9f95a80] | 70 | */
|
---|
[e402382] | 71 | int
|
---|
[4f1c0b4] | 72 | fat_cluster_walk(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc,
|
---|
[e402382] | 73 | fat_cluster_t *lastc, uint16_t *numc, uint16_t max_clusters)
|
---|
[0f57d0e] | 74 | {
|
---|
| 75 | block_t *b;
|
---|
| 76 | unsigned bps;
|
---|
| 77 | unsigned rscnt; /* block address of the first FAT */
|
---|
[4f1c0b4] | 78 | uint16_t clusters = 0;
|
---|
[0f57d0e] | 79 | fat_cluster_t clst = firstc;
|
---|
[c91f2d1b] | 80 | int rc;
|
---|
[0f57d0e] | 81 |
|
---|
[cb682eb] | 82 | bps = uint16_t_le2host(bs->bps);
|
---|
| 83 | rscnt = uint16_t_le2host(bs->rscnt);
|
---|
[0f57d0e] | 84 |
|
---|
[4f1c0b4] | 85 | if (firstc == FAT_CLST_RES0) {
|
---|
| 86 | /* No space allocated to the file. */
|
---|
[6c8d267] | 87 | if (lastc)
|
---|
| 88 | *lastc = firstc;
|
---|
[e402382] | 89 | if (numc)
|
---|
| 90 | *numc = 0;
|
---|
| 91 | return EOK;
|
---|
[0f57d0e] | 92 | }
|
---|
| 93 |
|
---|
[4f1c0b4] | 94 | while (clst < FAT_CLST_LAST1 && clusters < max_clusters) {
|
---|
[6c8d267] | 95 | bn_t fsec; /* sector offset relative to FAT1 */
|
---|
[0f57d0e] | 96 | unsigned fidx; /* FAT1 entry index */
|
---|
| 97 |
|
---|
[4f1c0b4] | 98 | assert(clst >= FAT_CLST_FIRST);
|
---|
[6c8d267] | 99 | if (lastc)
|
---|
| 100 | *lastc = clst; /* remember the last cluster number */
|
---|
[0f57d0e] | 101 | fsec = (clst * sizeof(fat_cluster_t)) / bps;
|
---|
| 102 | fidx = clst % (bps / sizeof(fat_cluster_t));
|
---|
| 103 | /* read FAT1 */
|
---|
[c91f2d1b] | 104 | rc = block_get(&b, dev_handle, rscnt + fsec, BLOCK_FLAGS_NONE);
|
---|
[e402382] | 105 | if (rc != EOK)
|
---|
| 106 | return rc;
|
---|
[0f57d0e] | 107 | clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]);
|
---|
| 108 | assert(clst != FAT_CLST_BAD);
|
---|
[c91f2d1b] | 109 | rc = block_put(b);
|
---|
[e402382] | 110 | if (rc != EOK)
|
---|
| 111 | return rc;
|
---|
[4f1c0b4] | 112 | clusters++;
|
---|
[0f57d0e] | 113 | }
|
---|
| 114 |
|
---|
[6c8d267] | 115 | if (lastc && clst < FAT_CLST_LAST1)
|
---|
| 116 | *lastc = clst;
|
---|
[e402382] | 117 | if (numc)
|
---|
| 118 | *numc = clusters;
|
---|
[0f57d0e] | 119 |
|
---|
[e402382] | 120 | return EOK;
|
---|
[0f57d0e] | 121 | }
|
---|
| 122 |
|
---|
[4f1c0b4] | 123 | /** Read block from file located on a FAT file system.
|
---|
[0f57d0e] | 124 | *
|
---|
[684b655] | 125 | * @param block Pointer to a block pointer for storing result.
|
---|
[4f1c0b4] | 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.
|
---|
[6c8d267] | 130 | * @param bn Block number.
|
---|
[1d8cdb1] | 131 | * @param flags Flags passed to libblock.
|
---|
[0f57d0e] | 132 | *
|
---|
[684b655] | 133 | * @return EOK on success or a negative error code.
|
---|
[0f57d0e] | 134 | */
|
---|
[684b655] | 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)
|
---|
[0f57d0e] | 138 | {
|
---|
| 139 | unsigned bps;
|
---|
| 140 | unsigned rscnt; /* block address of the first FAT */
|
---|
[4f1c0b4] | 141 | unsigned rde;
|
---|
| 142 | unsigned rds; /* root directory size */
|
---|
| 143 | unsigned sf;
|
---|
| 144 | unsigned ssa; /* size of the system area */
|
---|
[e402382] | 145 | uint16_t clusters;
|
---|
| 146 | unsigned max_clusters;
|
---|
[913a821c] | 147 | fat_cluster_t lastc;
|
---|
[c91f2d1b] | 148 | int rc;
|
---|
[0f57d0e] | 149 |
|
---|
[cb682eb] | 150 | bps = uint16_t_le2host(bs->bps);
|
---|
| 151 | rscnt = uint16_t_le2host(bs->rscnt);
|
---|
[4f1c0b4] | 152 | rde = uint16_t_le2host(bs->root_ent_max);
|
---|
| 153 | sf = uint16_t_le2host(bs->sec_per_fat);
|
---|
[0f57d0e] | 154 |
|
---|
[4f1c0b4] | 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 */
|
---|
[6c8d267] | 161 | assert(bn < rds);
|
---|
[684b655] | 162 | rc = block_get(block, dev_handle, rscnt + bs->fatcnt * sf + bn,
|
---|
[c91f2d1b] | 163 | flags);
|
---|
[684b655] | 164 | return rc;
|
---|
[0f57d0e] | 165 | }
|
---|
| 166 |
|
---|
[6c8d267] | 167 | max_clusters = bn / bs->spc;
|
---|
[e402382] | 168 | rc = fat_cluster_walk(bs, dev_handle, firstc, &lastc, &clusters,
|
---|
[4f1c0b4] | 169 | max_clusters);
|
---|
[e402382] | 170 | if (rc != EOK)
|
---|
| 171 | return rc;
|
---|
[4f1c0b4] | 172 | assert(clusters == max_clusters);
|
---|
[0f57d0e] | 173 |
|
---|
[684b655] | 174 | rc = block_get(block, dev_handle,
|
---|
| 175 | ssa + (lastc - FAT_CLST_FIRST) * bs->spc + bn % bs->spc, flags);
|
---|
[0f57d0e] | 176 |
|
---|
[684b655] | 177 | return rc;
|
---|
[0f57d0e] | 178 | }
|
---|
| 179 |
|
---|
| 180 | /** Fill the gap between EOF and a new file position.
|
---|
| 181 | *
|
---|
[cb682eb] | 182 | * @param bs Buffer holding the boot sector for nodep.
|
---|
[0f57d0e] | 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.
|
---|
[cca29e3c] | 189 | *
|
---|
| 190 | * @return EOK on success or a negative error code.
|
---|
[0f57d0e] | 191 | */
|
---|
[cca29e3c] | 192 | int fat_fill_gap(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, off_t pos)
|
---|
[0f57d0e] | 193 | {
|
---|
| 194 | uint16_t bps;
|
---|
| 195 | unsigned spc;
|
---|
[cb682eb] | 196 | block_t *b;
|
---|
[0f57d0e] | 197 | off_t o, boundary;
|
---|
[c91f2d1b] | 198 | int rc;
|
---|
[0f57d0e] | 199 |
|
---|
[cb682eb] | 200 | bps = uint16_t_le2host(bs->bps);
|
---|
| 201 | spc = bs->spc;
|
---|
[0f57d0e] | 202 |
|
---|
| 203 | boundary = ROUND_UP(nodep->size, bps * spc);
|
---|
| 204 |
|
---|
| 205 | /* zero out already allocated space */
|
---|
[abd36f7] | 206 | for (o = nodep->size; o < pos && o < boundary;
|
---|
[0f57d0e] | 207 | o = ALIGN_DOWN(o + bps, bps)) {
|
---|
[1d8cdb1] | 208 | int flags = (o % bps == 0) ?
|
---|
| 209 | BLOCK_FLAGS_NOREAD : BLOCK_FLAGS_NONE;
|
---|
[684b655] | 210 | rc = fat_block_get(&b, bs, nodep, o / bps, flags);
|
---|
[cca29e3c] | 211 | if (rc != EOK)
|
---|
| 212 | return rc;
|
---|
[0f57d0e] | 213 | memset(b->data + o % bps, 0, bps - o % bps);
|
---|
| 214 | b->dirty = true; /* need to sync node */
|
---|
[c91f2d1b] | 215 | rc = block_put(b);
|
---|
[cca29e3c] | 216 | if (rc != EOK)
|
---|
| 217 | return rc;
|
---|
[0f57d0e] | 218 | }
|
---|
| 219 |
|
---|
| 220 | if (o >= pos)
|
---|
[cca29e3c] | 221 | return EOK;
|
---|
[0f57d0e] | 222 |
|
---|
| 223 | /* zero out the initial part of the new cluster chain */
|
---|
| 224 | for (o = boundary; o < pos; o += bps) {
|
---|
[684b655] | 225 | rc = _fat_block_get(&b, bs, nodep->idx->dev_handle, mcl,
|
---|
[1d8cdb1] | 226 | (o - boundary) / bps, BLOCK_FLAGS_NOREAD);
|
---|
[cca29e3c] | 227 | if (rc != EOK)
|
---|
| 228 | return rc;
|
---|
[0f57d0e] | 229 | memset(b->data, 0, min(bps, pos - o));
|
---|
| 230 | b->dirty = true; /* need to sync node */
|
---|
[c91f2d1b] | 231 | rc = block_put(b);
|
---|
[cca29e3c] | 232 | if (rc != EOK)
|
---|
| 233 | return rc;
|
---|
[0f57d0e] | 234 | }
|
---|
[cca29e3c] | 235 |
|
---|
| 236 | return EOK;
|
---|
[0f57d0e] | 237 | }
|
---|
| 238 |
|
---|
[913a821c] | 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.
|
---|
[dc87ad11] | 244 | * @param value Output argument holding the value of the cluster.
|
---|
[913a821c] | 245 | *
|
---|
[dc87ad11] | 246 | * @return EOK or a negative error code.
|
---|
[913a821c] | 247 | */
|
---|
[dc87ad11] | 248 | int
|
---|
| 249 | fat_get_cluster(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t clst,
|
---|
| 250 | fat_cluster_t *value)
|
---|
[913a821c] | 251 | {
|
---|
| 252 | block_t *b;
|
---|
| 253 | uint16_t bps;
|
---|
| 254 | uint16_t rscnt;
|
---|
[dc87ad11] | 255 | fat_cluster_t *cp;
|
---|
[c91f2d1b] | 256 | int rc;
|
---|
[913a821c] | 257 |
|
---|
| 258 | bps = uint16_t_le2host(bs->bps);
|
---|
| 259 | rscnt = uint16_t_le2host(bs->rscnt);
|
---|
| 260 |
|
---|
[c91f2d1b] | 261 | rc = block_get(&b, dev_handle, rscnt +
|
---|
| 262 | (clst * sizeof(fat_cluster_t)) / bps, BLOCK_FLAGS_NONE);
|
---|
[dc87ad11] | 263 | if (rc != EOK)
|
---|
| 264 | return rc;
|
---|
[913a821c] | 265 | cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t));
|
---|
[dc87ad11] | 266 | *value = uint16_t_le2host(*cp);
|
---|
[c91f2d1b] | 267 | rc = block_put(b);
|
---|
[913a821c] | 268 |
|
---|
[dc87ad11] | 269 | return rc;
|
---|
[913a821c] | 270 | }
|
---|
| 271 |
|
---|
| 272 | /** Set cluster in one instance of FAT.
|
---|
[9f95a80] | 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.
|
---|
[913a821c] | 277 | * @param clst Cluster which is to be set.
|
---|
| 278 | * @param value Value to set the cluster with.
|
---|
[cca29e3c] | 279 | *
|
---|
| 280 | * @return EOK on success or a negative error code.
|
---|
[9f95a80] | 281 | */
|
---|
[cca29e3c] | 282 | int
|
---|
[913a821c] | 283 | fat_set_cluster(fat_bs_t *bs, dev_handle_t dev_handle, unsigned fatno,
|
---|
[cb682eb] | 284 | fat_cluster_t clst, fat_cluster_t value)
|
---|
[0f57d0e] | 285 | {
|
---|
[cb682eb] | 286 | block_t *b;
|
---|
[23b56ca] | 287 | uint16_t bps;
|
---|
| 288 | uint16_t rscnt;
|
---|
| 289 | uint16_t sf;
|
---|
| 290 | fat_cluster_t *cp;
|
---|
[c91f2d1b] | 291 | int rc;
|
---|
[23b56ca] | 292 |
|
---|
[cb682eb] | 293 | bps = uint16_t_le2host(bs->bps);
|
---|
| 294 | rscnt = uint16_t_le2host(bs->rscnt);
|
---|
| 295 | sf = uint16_t_le2host(bs->sec_per_fat);
|
---|
[23b56ca] | 296 |
|
---|
[cb682eb] | 297 | assert(fatno < bs->fatcnt);
|
---|
[c91f2d1b] | 298 | rc = block_get(&b, dev_handle, rscnt + sf * fatno +
|
---|
[1d8cdb1] | 299 | (clst * sizeof(fat_cluster_t)) / bps, BLOCK_FLAGS_NONE);
|
---|
[cca29e3c] | 300 | if (rc != EOK)
|
---|
| 301 | return rc;
|
---|
[cb682eb] | 302 | cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t));
|
---|
[23b56ca] | 303 | *cp = host2uint16_t_le(value);
|
---|
[cb682eb] | 304 | b->dirty = true; /* need to sync block */
|
---|
[c91f2d1b] | 305 | rc = block_put(b);
|
---|
[cca29e3c] | 306 | return rc;
|
---|
[0f57d0e] | 307 | }
|
---|
| 308 |
|
---|
[9f95a80] | 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.
|
---|
[cca29e3c] | 315 | *
|
---|
| 316 | * @return EOK on success or a negative error code.
|
---|
[9f95a80] | 317 | */
|
---|
[cca29e3c] | 318 | int fat_alloc_shadow_clusters(fat_bs_t *bs, dev_handle_t dev_handle,
|
---|
[cb682eb] | 319 | fat_cluster_t *lifo, unsigned nclsts)
|
---|
[0f57d0e] | 320 | {
|
---|
[b1178d0] | 321 | uint8_t fatno;
|
---|
| 322 | unsigned c;
|
---|
[cca29e3c] | 323 | int rc;
|
---|
[b1178d0] | 324 |
|
---|
[cb682eb] | 325 | for (fatno = FAT1 + 1; fatno < bs->fatcnt; fatno++) {
|
---|
[b1178d0] | 326 | for (c = 0; c < nclsts; c++) {
|
---|
[cca29e3c] | 327 | rc = fat_set_cluster(bs, dev_handle, fatno, lifo[c],
|
---|
[b1178d0] | 328 | c == 0 ? FAT_CLST_LAST1 : lifo[c - 1]);
|
---|
[cca29e3c] | 329 | if (rc != EOK)
|
---|
| 330 | return rc;
|
---|
[b1178d0] | 331 | }
|
---|
| 332 | }
|
---|
[cca29e3c] | 333 |
|
---|
| 334 | return EOK;
|
---|
[0f57d0e] | 335 | }
|
---|
| 336 |
|
---|
[e478b2a4] | 337 | /** Allocate clusters in all copies of FAT.
|
---|
[9f95a80] | 338 | *
|
---|
| 339 | * This function will attempt to allocate the requested number of clusters in
|
---|
[e478b2a4] | 340 | * all instances of the FAT. The FAT will be altered so that the allocated
|
---|
[9f95a80] | 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 | */
|
---|
[0f57d0e] | 354 | int
|
---|
[cb682eb] | 355 | fat_alloc_clusters(fat_bs_t *bs, dev_handle_t dev_handle, unsigned nclsts,
|
---|
| 356 | fat_cluster_t *mcl, fat_cluster_t *lcl)
|
---|
[0f57d0e] | 357 | {
|
---|
| 358 | uint16_t bps;
|
---|
| 359 | uint16_t rscnt;
|
---|
| 360 | uint16_t sf;
|
---|
[3a8faba] | 361 | uint16_t ts;
|
---|
| 362 | unsigned rde;
|
---|
| 363 | unsigned rds;
|
---|
| 364 | unsigned ssa;
|
---|
[cb682eb] | 365 | block_t *blk;
|
---|
[0f57d0e] | 366 | fat_cluster_t *lifo; /* stack for storing free cluster numbers */
|
---|
| 367 | unsigned found = 0; /* top of the free cluster number stack */
|
---|
| 368 | unsigned b, c, cl;
|
---|
[c91f2d1b] | 369 | int rc;
|
---|
[0f57d0e] | 370 |
|
---|
| 371 | lifo = (fat_cluster_t *) malloc(nclsts * sizeof(fat_cluster_t));
|
---|
[e478b2a4] | 372 | if (!lifo)
|
---|
[0f57d0e] | 373 | return ENOMEM;
|
---|
| 374 |
|
---|
[cb682eb] | 375 | bps = uint16_t_le2host(bs->bps);
|
---|
| 376 | rscnt = uint16_t_le2host(bs->rscnt);
|
---|
| 377 | sf = uint16_t_le2host(bs->sec_per_fat);
|
---|
[3a8faba] | 378 | rde = uint16_t_le2host(bs->root_ent_max);
|
---|
| 379 | ts = uint16_t_le2host(bs->totsec16);
|
---|
| 380 |
|
---|
| 381 | rds = (sizeof(fat_dentry_t) * rde) / bps;
|
---|
| 382 | rds += ((sizeof(fat_dentry_t) * rde) % bps != 0);
|
---|
| 383 | ssa = rscnt + bs->fatcnt * sf + rds;
|
---|
[0f57d0e] | 384 |
|
---|
| 385 | /*
|
---|
| 386 | * Search FAT1 for unused clusters.
|
---|
| 387 | */
|
---|
[6ebe721] | 388 | fibril_mutex_lock(&fat_alloc_lock);
|
---|
[e478b2a4] | 389 | for (b = 0, cl = 0; b < sf; b++) {
|
---|
[c91f2d1b] | 390 | rc = block_get(&blk, dev_handle, rscnt + b, BLOCK_FLAGS_NONE);
|
---|
[2f636b6] | 391 | if (rc != EOK)
|
---|
| 392 | goto error;
|
---|
[0f57d0e] | 393 | for (c = 0; c < bps / sizeof(fat_cluster_t); c++, cl++) {
|
---|
[3a8faba] | 394 | /*
|
---|
| 395 | * Check if the cluster is physically there. This check
|
---|
| 396 | * becomes necessary when the file system is created
|
---|
| 397 | * with fewer total sectors than how many is inferred
|
---|
| 398 | * from the size of the file allocation table.
|
---|
| 399 | */
|
---|
| 400 | if ((cl - 2) * bs->spc + ssa >= ts) {
|
---|
| 401 | rc = block_put(blk);
|
---|
| 402 | if (rc != EOK)
|
---|
| 403 | goto error;
|
---|
| 404 | goto out;
|
---|
| 405 | }
|
---|
| 406 |
|
---|
[0f57d0e] | 407 | fat_cluster_t *clst = (fat_cluster_t *)blk->data + c;
|
---|
[a429bfb] | 408 | if (uint16_t_le2host(*clst) == FAT_CLST_RES0) {
|
---|
[0f57d0e] | 409 | /*
|
---|
| 410 | * The cluster is free. Put it into our stack
|
---|
| 411 | * of found clusters and mark it as non-free.
|
---|
| 412 | */
|
---|
| 413 | lifo[found] = cl;
|
---|
[a429bfb] | 414 | *clst = (found == 0) ?
|
---|
| 415 | host2uint16_t_le(FAT_CLST_LAST1) :
|
---|
| 416 | host2uint16_t_le(lifo[found - 1]);
|
---|
[0f57d0e] | 417 | blk->dirty = true; /* need to sync block */
|
---|
| 418 | if (++found == nclsts) {
|
---|
| 419 | /* we are almost done */
|
---|
[c91f2d1b] | 420 | rc = block_put(blk);
|
---|
[2f636b6] | 421 | if (rc != EOK)
|
---|
| 422 | goto error;
|
---|
[0f57d0e] | 423 | /* update the shadow copies of FAT */
|
---|
[cca29e3c] | 424 | rc = fat_alloc_shadow_clusters(bs,
|
---|
[cb682eb] | 425 | dev_handle, lifo, nclsts);
|
---|
[2f636b6] | 426 | if (rc != EOK)
|
---|
| 427 | goto error;
|
---|
[0f57d0e] | 428 | *mcl = lifo[found - 1];
|
---|
| 429 | *lcl = lifo[0];
|
---|
| 430 | free(lifo);
|
---|
[6ebe721] | 431 | fibril_mutex_unlock(&fat_alloc_lock);
|
---|
[0f57d0e] | 432 | return EOK;
|
---|
| 433 | }
|
---|
| 434 | }
|
---|
| 435 | }
|
---|
[c91f2d1b] | 436 | rc = block_put(blk);
|
---|
[2f636b6] | 437 | if (rc != EOK) {
|
---|
| 438 | error:
|
---|
| 439 | fibril_mutex_unlock(&fat_alloc_lock);
|
---|
| 440 | free(lifo);
|
---|
| 441 | return rc;
|
---|
| 442 | }
|
---|
[0f57d0e] | 443 | }
|
---|
[3a8faba] | 444 | out:
|
---|
[6ebe721] | 445 | fibril_mutex_unlock(&fat_alloc_lock);
|
---|
[0f57d0e] | 446 |
|
---|
| 447 | /*
|
---|
| 448 | * We could not find enough clusters. Now we need to free the clusters
|
---|
| 449 | * we have allocated so far.
|
---|
| 450 | */
|
---|
[cb682eb] | 451 | while (found--) {
|
---|
[cca29e3c] | 452 | rc = fat_set_cluster(bs, dev_handle, FAT1, lifo[found],
|
---|
[cb682eb] | 453 | FAT_CLST_RES0);
|
---|
[cca29e3c] | 454 | if (rc != EOK) {
|
---|
| 455 | free(lifo);
|
---|
| 456 | return rc;
|
---|
| 457 | }
|
---|
[cb682eb] | 458 | }
|
---|
[0f57d0e] | 459 |
|
---|
| 460 | free(lifo);
|
---|
| 461 | return ENOSPC;
|
---|
| 462 | }
|
---|
| 463 |
|
---|
[913a821c] | 464 | /** Free clusters forming a cluster chain in all copies of FAT.
|
---|
| 465 | *
|
---|
| 466 | * @param bs Buffer hodling the boot sector of the file system.
|
---|
| 467 | * @param dev_handle Device handle of the file system.
|
---|
| 468 | * @param firstc First cluster in the chain which is to be freed.
|
---|
[cca29e3c] | 469 | *
|
---|
| 470 | * @return EOK on success or a negative return code.
|
---|
[913a821c] | 471 | */
|
---|
[cca29e3c] | 472 | int
|
---|
[913a821c] | 473 | fat_free_clusters(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc)
|
---|
| 474 | {
|
---|
| 475 | unsigned fatno;
|
---|
| 476 | fat_cluster_t nextc;
|
---|
[dc87ad11] | 477 | int rc;
|
---|
[913a821c] | 478 |
|
---|
| 479 | /* Mark all clusters in the chain as free in all copies of FAT. */
|
---|
| 480 | while (firstc < FAT_CLST_LAST1) {
|
---|
[d1b625b] | 481 | assert(firstc >= FAT_CLST_FIRST && firstc < FAT_CLST_BAD);
|
---|
[dc87ad11] | 482 | rc = fat_get_cluster(bs, dev_handle, firstc, &nextc);
|
---|
[cca29e3c] | 483 | if (rc != EOK)
|
---|
| 484 | return rc;
|
---|
| 485 | for (fatno = FAT1; fatno < bs->fatcnt; fatno++) {
|
---|
| 486 | rc = fat_set_cluster(bs, dev_handle, fatno, firstc,
|
---|
[913a821c] | 487 | FAT_CLST_RES0);
|
---|
[cca29e3c] | 488 | if (rc != EOK)
|
---|
| 489 | return rc;
|
---|
| 490 | }
|
---|
| 491 |
|
---|
[913a821c] | 492 | firstc = nextc;
|
---|
| 493 | }
|
---|
[cca29e3c] | 494 |
|
---|
| 495 | return EOK;
|
---|
[913a821c] | 496 | }
|
---|
| 497 |
|
---|
[9f95a80] | 498 | /** Append a cluster chain to the last file cluster in all FATs.
|
---|
| 499 | *
|
---|
[913a821c] | 500 | * @param bs Buffer holding the boot sector of the file system.
|
---|
[9f95a80] | 501 | * @param nodep Node representing the file.
|
---|
| 502 | * @param mcl First cluster of the cluster chain to append.
|
---|
[cca29e3c] | 503 | *
|
---|
| 504 | * @return EOK on success or a negative error code.
|
---|
[9f95a80] | 505 | */
|
---|
[cca29e3c] | 506 | int fat_append_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl)
|
---|
[0f57d0e] | 507 | {
|
---|
[cb682eb] | 508 | dev_handle_t dev_handle = nodep->idx->dev_handle;
|
---|
[e17d986] | 509 | fat_cluster_t lcl;
|
---|
[e402382] | 510 | uint16_t numc;
|
---|
[cb682eb] | 511 | uint8_t fatno;
|
---|
[e402382] | 512 | int rc;
|
---|
| 513 |
|
---|
| 514 | rc = fat_cluster_walk(bs, dev_handle, nodep->firstc, &lcl, &numc,
|
---|
| 515 | (uint16_t) -1);
|
---|
[cca29e3c] | 516 | if (rc != EOK)
|
---|
| 517 | return rc;
|
---|
[e17d986] | 518 |
|
---|
[e402382] | 519 | if (numc == 0) {
|
---|
[4f1c0b4] | 520 | /* No clusters allocated to the node yet. */
|
---|
[57e76cb] | 521 | nodep->firstc = mcl;
|
---|
[e17d986] | 522 | nodep->dirty = true; /* need to sync node */
|
---|
[cca29e3c] | 523 | return EOK;
|
---|
| 524 | }
|
---|
| 525 |
|
---|
| 526 | for (fatno = FAT1; fatno < bs->fatcnt; fatno++) {
|
---|
| 527 | rc = fat_set_cluster(bs, nodep->idx->dev_handle, fatno, lcl,
|
---|
| 528 | mcl);
|
---|
| 529 | if (rc != EOK)
|
---|
| 530 | return rc;
|
---|
[e17d986] | 531 | }
|
---|
| 532 |
|
---|
[cca29e3c] | 533 | return EOK;
|
---|
[913a821c] | 534 | }
|
---|
| 535 |
|
---|
| 536 | /** Chop off node clusters in all copies of FAT.
|
---|
| 537 | *
|
---|
| 538 | * @param bs Buffer holding the boot sector of the file system.
|
---|
| 539 | * @param nodep FAT node where the chopping will take place.
|
---|
| 540 | * @param lastc Last cluster which will remain in the node. If this
|
---|
| 541 | * argument is FAT_CLST_RES0, then all clusters will
|
---|
[ac49f5d1] | 542 | * be chopped off.
|
---|
[cca29e3c] | 543 | *
|
---|
| 544 | * @return EOK on success or a negative return code.
|
---|
[913a821c] | 545 | */
|
---|
[cca29e3c] | 546 | int fat_chop_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t lastc)
|
---|
[913a821c] | 547 | {
|
---|
[dc87ad11] | 548 | int rc;
|
---|
| 549 |
|
---|
[913a821c] | 550 | dev_handle_t dev_handle = nodep->idx->dev_handle;
|
---|
| 551 | if (lastc == FAT_CLST_RES0) {
|
---|
| 552 | /* The node will have zero size and no clusters allocated. */
|
---|
[cca29e3c] | 553 | rc = fat_free_clusters(bs, dev_handle, nodep->firstc);
|
---|
| 554 | if (rc != EOK)
|
---|
| 555 | return rc;
|
---|
[913a821c] | 556 | nodep->firstc = FAT_CLST_RES0;
|
---|
| 557 | nodep->dirty = true; /* need to sync node */
|
---|
| 558 | } else {
|
---|
| 559 | fat_cluster_t nextc;
|
---|
| 560 | unsigned fatno;
|
---|
| 561 |
|
---|
[dc87ad11] | 562 | rc = fat_get_cluster(bs, dev_handle, lastc, &nextc);
|
---|
[cca29e3c] | 563 | if (rc != EOK)
|
---|
| 564 | return rc;
|
---|
[913a821c] | 565 |
|
---|
| 566 | /* Terminate the cluster chain in all copies of FAT. */
|
---|
[cca29e3c] | 567 | for (fatno = FAT1; fatno < bs->fatcnt; fatno++) {
|
---|
| 568 | rc = fat_set_cluster(bs, dev_handle, fatno, lastc,
|
---|
| 569 | FAT_CLST_LAST1);
|
---|
| 570 | if (rc != EOK)
|
---|
| 571 | return rc;
|
---|
| 572 | }
|
---|
[913a821c] | 573 |
|
---|
| 574 | /* Free all following clusters. */
|
---|
[cca29e3c] | 575 | rc = fat_free_clusters(bs, dev_handle, nextc);
|
---|
| 576 | if (rc != EOK)
|
---|
| 577 | return rc;
|
---|
[913a821c] | 578 | }
|
---|
[cca29e3c] | 579 |
|
---|
| 580 | return EOK;
|
---|
[0f57d0e] | 581 | }
|
---|
[6ebaff9] | 582 |
|
---|
[cca29e3c] | 583 | int
|
---|
[5f116e7] | 584 | fat_zero_cluster(struct fat_bs *bs, dev_handle_t dev_handle, fat_cluster_t c)
|
---|
| 585 | {
|
---|
| 586 | int i;
|
---|
| 587 | block_t *b;
|
---|
| 588 | unsigned bps;
|
---|
[c91f2d1b] | 589 | int rc;
|
---|
[5f116e7] | 590 |
|
---|
| 591 | bps = uint16_t_le2host(bs->bps);
|
---|
| 592 |
|
---|
| 593 | for (i = 0; i < bs->spc; i++) {
|
---|
[684b655] | 594 | rc = _fat_block_get(&b, bs, dev_handle, c, i,
|
---|
| 595 | BLOCK_FLAGS_NOREAD);
|
---|
[cca29e3c] | 596 | if (rc != EOK)
|
---|
| 597 | return rc;
|
---|
[5f116e7] | 598 | memset(b->data, 0, bps);
|
---|
| 599 | b->dirty = true;
|
---|
[c91f2d1b] | 600 | rc = block_put(b);
|
---|
[cca29e3c] | 601 | if (rc != EOK)
|
---|
| 602 | return rc;
|
---|
[5f116e7] | 603 | }
|
---|
[cca29e3c] | 604 |
|
---|
| 605 | return EOK;
|
---|
[5f116e7] | 606 | }
|
---|
| 607 |
|
---|
[6ebaff9] | 608 | /**
|
---|
| 609 | * @}
|
---|
| 610 | */
|
---|