HelenOS sources
This source file includes following definitions.
- exfat_cluster_walk
- exfat_block_get
- exfat_block_get_by_clst
- exfat_get_cluster
- exfat_set_cluster
- exfat_alloc_clusters
- exfat_free_clusters
- exfat_append_clusters
- exfat_chop_clusters
- exfat_zero_cluster
- exfat_read_uctable
- exfat_sanity_check
#include "exfat_fat.h"
#include "exfat_bitmap.h"
#include "exfat.h"
#include "../../vfs/vfs.h"
#include <libfs.h>
#include <block.h>
#include <errno.h>
#include <byteorder.h>
#include <align.h>
#include <assert.h>
#include <fibril_synch.h>
#include <mem.h>
#include <stdlib.h>
#include <str.h>
static FIBRIL_MUTEX_INITIALIZE(exfat_alloc_lock);
errno_t
exfat_cluster_walk(exfat_bs_t *bs, service_id_t service_id,
exfat_cluster_t firstc, exfat_cluster_t *lastc, uint32_t *numc,
uint32_t max_clusters)
{
uint32_t clusters = 0;
exfat_cluster_t clst = firstc;
errno_t rc;
if (firstc < EXFAT_CLST_FIRST) {
if (lastc)
*lastc = firstc;
if (numc)
*numc = 0;
return EOK;
}
while (clst != EXFAT_CLST_EOF && clusters < max_clusters) {
assert(clst >= EXFAT_CLST_FIRST);
if (lastc)
*lastc = clst;
rc = exfat_get_cluster(bs, service_id, clst, &clst);
if (rc != EOK)
return rc;
assert(clst != EXFAT_CLST_BAD);
clusters++;
}
if (lastc && clst != EXFAT_CLST_EOF)
*lastc = clst;
if (numc)
*numc = clusters;
return EOK;
}
errno_t
exfat_block_get(block_t **block, exfat_bs_t *bs, exfat_node_t *nodep,
aoff64_t bn, int flags)
{
exfat_cluster_t firstc = nodep->firstc;
exfat_cluster_t currc = 0;
aoff64_t relbn = bn;
errno_t rc;
if (!nodep->size)
return ELIMIT;
if (nodep->fragmented) {
if (((((nodep->size - 1) / BPS(bs)) / SPC(bs)) == bn / SPC(bs)) &&
nodep->lastc_cached_valid) {
return block_get(block, nodep->idx->service_id, DATA_FS(bs) +
(nodep->lastc_cached_value - EXFAT_CLST_FIRST) * SPC(bs) +
(bn % SPC(bs)), flags);
}
if (nodep->currc_cached_valid && bn >= nodep->currc_cached_bn) {
firstc = nodep->currc_cached_value;
relbn -= (nodep->currc_cached_bn / SPC(bs)) * SPC(bs);
}
}
rc = exfat_block_get_by_clst(block, bs, nodep->idx->service_id,
nodep->fragmented, firstc, &currc, relbn, flags);
if (rc != EOK)
return rc;
nodep->currc_cached_valid = true;
nodep->currc_cached_bn = bn;
nodep->currc_cached_value = currc;
return rc;
}
errno_t
exfat_block_get_by_clst(block_t **block, exfat_bs_t *bs,
service_id_t service_id, bool fragmented, exfat_cluster_t fcl,
exfat_cluster_t *clp, aoff64_t bn, int flags)
{
uint32_t clusters;
uint32_t max_clusters;
exfat_cluster_t c = EXFAT_CLST_FIRST;
errno_t rc;
if (fcl < EXFAT_CLST_FIRST || fcl > DATA_CNT(bs) + 2)
return ELIMIT;
if (!fragmented) {
rc = block_get(block, service_id, DATA_FS(bs) +
(fcl - EXFAT_CLST_FIRST) * SPC(bs) + bn, flags);
} else {
max_clusters = bn / SPC(bs);
rc = exfat_cluster_walk(bs, service_id, fcl, &c, &clusters, max_clusters);
if (rc != EOK)
return rc;
assert(clusters == max_clusters);
rc = block_get(block, service_id, DATA_FS(bs) +
(c - EXFAT_CLST_FIRST) * SPC(bs) + (bn % SPC(bs)), flags);
if (clp)
*clp = c;
}
return rc;
}
errno_t
exfat_get_cluster(exfat_bs_t *bs, service_id_t service_id,
exfat_cluster_t clst, exfat_cluster_t *value)
{
block_t *b;
aoff64_t offset;
errno_t rc;
offset = clst * sizeof(exfat_cluster_t);
rc = block_get(&b, service_id, FAT_FS(bs) + offset / BPS(bs), BLOCK_FLAGS_NONE);
if (rc != EOK)
return rc;
*value = uint32_t_le2host(*(uint32_t *)(b->data + offset % BPS(bs)));
rc = block_put(b);
return rc;
}
errno_t
exfat_set_cluster(exfat_bs_t *bs, service_id_t service_id,
exfat_cluster_t clst, exfat_cluster_t value)
{
block_t *b;
aoff64_t offset;
errno_t rc;
offset = clst * sizeof(exfat_cluster_t);
rc = block_get(&b, service_id, FAT_FS(bs) + offset / BPS(bs), BLOCK_FLAGS_NONE);
if (rc != EOK)
return rc;
*(uint32_t *)(b->data + offset % BPS(bs)) = host2uint32_t_le(value);
b->dirty = true;
rc = block_put(b);
return rc;
}
errno_t
exfat_alloc_clusters(exfat_bs_t *bs, service_id_t service_id, unsigned nclsts,
exfat_cluster_t *mcl, exfat_cluster_t *lcl)
{
exfat_cluster_t *lifo;
unsigned found = 0;
exfat_cluster_t clst;
errno_t rc = EOK;
lifo = (exfat_cluster_t *) malloc(nclsts * sizeof(exfat_cluster_t));
if (!lifo)
return ENOMEM;
fibril_mutex_lock(&exfat_alloc_lock);
for (clst = EXFAT_CLST_FIRST; clst < DATA_CNT(bs) + 2 && found < nclsts;
clst++) {
if (exfat_bitmap_is_free(bs, service_id, clst) == EOK) {
lifo[found] = clst;
rc = exfat_set_cluster(bs, service_id, clst,
(found == 0) ? EXFAT_CLST_EOF : lifo[found - 1]);
if (rc != EOK)
goto exit_error;
found++;
rc = exfat_bitmap_set_cluster(bs, service_id, clst);
if (rc != EOK)
goto exit_error;
}
}
if (rc == EOK && found == nclsts) {
*mcl = lifo[found - 1];
*lcl = lifo[0];
free(lifo);
fibril_mutex_unlock(&exfat_alloc_lock);
return EOK;
}
rc = ENOSPC;
exit_error:
while (found--) {
(void) exfat_bitmap_clear_cluster(bs, service_id, lifo[found]);
(void) exfat_set_cluster(bs, service_id, lifo[found], 0);
}
free(lifo);
fibril_mutex_unlock(&exfat_alloc_lock);
return rc;
}
errno_t
exfat_free_clusters(exfat_bs_t *bs, service_id_t service_id, exfat_cluster_t firstc)
{
exfat_cluster_t nextc;
errno_t rc;
while (firstc != EXFAT_CLST_EOF) {
assert(firstc >= EXFAT_CLST_FIRST && firstc < EXFAT_CLST_BAD);
rc = exfat_get_cluster(bs, service_id, firstc, &nextc);
if (rc != EOK)
return rc;
rc = exfat_set_cluster(bs, service_id, firstc, 0);
if (rc != EOK)
return rc;
rc = exfat_bitmap_clear_cluster(bs, service_id, firstc);
if (rc != EOK)
return rc;
firstc = nextc;
}
return EOK;
}
errno_t
exfat_append_clusters(exfat_bs_t *bs, exfat_node_t *nodep, exfat_cluster_t mcl,
exfat_cluster_t lcl)
{
service_id_t service_id = nodep->idx->service_id;
exfat_cluster_t lastc = 0;
errno_t rc;
if (nodep->firstc == 0) {
nodep->firstc = mcl;
nodep->dirty = true;
} else {
if (nodep->lastc_cached_valid) {
lastc = nodep->lastc_cached_value;
nodep->lastc_cached_valid = false;
} else {
rc = exfat_cluster_walk(bs, service_id, nodep->firstc,
&lastc, NULL, (uint16_t) -1);
if (rc != EOK)
return rc;
}
rc = exfat_set_cluster(bs, nodep->idx->service_id, lastc, mcl);
if (rc != EOK)
return rc;
}
nodep->lastc_cached_valid = true;
nodep->lastc_cached_value = lcl;
return EOK;
}
errno_t exfat_chop_clusters(exfat_bs_t *bs, exfat_node_t *nodep, exfat_cluster_t lcl)
{
errno_t rc;
service_id_t service_id = nodep->idx->service_id;
nodep->lastc_cached_valid = false;
if (nodep->currc_cached_value != lcl)
nodep->currc_cached_valid = false;
if (lcl == 0) {
rc = exfat_free_clusters(bs, service_id, nodep->firstc);
if (rc != EOK)
return rc;
nodep->firstc = 0;
nodep->dirty = true;
} else {
exfat_cluster_t nextc;
rc = exfat_get_cluster(bs, service_id, lcl, &nextc);
if (rc != EOK)
return rc;
rc = exfat_set_cluster(bs, service_id, lcl, EXFAT_CLST_EOF);
if (rc != EOK)
return rc;
rc = exfat_free_clusters(bs, service_id, nextc);
if (rc != EOK)
return rc;
}
nodep->lastc_cached_valid = true;
nodep->lastc_cached_value = lcl;
return EOK;
}
errno_t
exfat_zero_cluster(exfat_bs_t *bs, service_id_t service_id, exfat_cluster_t c)
{
size_t i;
block_t *b;
errno_t rc;
for (i = 0; i < SPC(bs); i++) {
rc = exfat_block_get_by_clst(&b, bs, service_id, false, c, NULL,
i, BLOCK_FLAGS_NOREAD);
if (rc != EOK)
return rc;
memset(b->data, 0, BPS(bs));
b->dirty = true;
rc = block_put(b);
if (rc != EOK)
return rc;
}
return EOK;
}
errno_t
exfat_read_uctable(exfat_bs_t *bs, exfat_node_t *nodep, uint8_t *uctable)
{
size_t i, blocks, count;
block_t *b;
errno_t rc;
blocks = ROUND_UP(nodep->size, BPS(bs)) / BPS(bs);
count = BPS(bs);
for (i = 0; i < blocks; i++) {
rc = exfat_block_get(&b, bs, nodep, i, BLOCK_FLAGS_NOREAD);
if (rc != EOK)
return rc;
if (i == blocks - 1)
count = nodep->size - i * BPS(bs);
memcpy(uctable, b->data, count);
uctable += count;
rc = block_put(b);
if (rc != EOK)
return rc;
}
return EOK;
}
errno_t exfat_sanity_check(exfat_bs_t *bs)
{
if (str_cmp((char const *)bs->oem_name, "EXFAT "))
return ENOTSUP;
else if (uint16_t_le2host(bs->signature) != 0xAA55)
return ENOTSUP;
else if (uint32_t_le2host(bs->fat_sector_count) == 0)
return ENOTSUP;
else if (uint32_t_le2host(bs->data_clusters) == 0)
return ENOTSUP;
else if (bs->fat_count != 1)
return ENOTSUP;
else if ((bs->bytes_per_sector + bs->sec_per_cluster) > 25) {
return ENOTSUP;
}
return EOK;
}
HelenOS homepage, sources at GitHub