#include "exfat.h"
#include <errno.h>
#include <string.h>
+#include <inttypes.h>
/*
* Sector to absolute offset.
return cluster + 1;
fat_offset = s2o(ef, le32_to_cpu(ef->sb->fat_sector_start))
+ cluster * sizeof(cluster_t);
- exfat_pread(ef->dev, &next, sizeof(next), fat_offset);
+ /* FIXME handle I/O error */
+ if (exfat_pread(ef->dev, &next, sizeof(next), fat_offset) < 0)
+ exfat_bug("failed to read the next cluster after %#x", cluster);
return le32_to_cpu(next);
}
return node->fptr_cluster;
}
-static cluster_t find_bit_and_set(uint8_t* bitmap, size_t start, size_t end)
+static cluster_t find_bit_and_set(bitmap_t* bitmap, size_t start, size_t end)
{
- const size_t start_index = start / 8;
- const size_t end_index = DIV_ROUND_UP(end, 8);
+ const size_t start_index = start / sizeof(bitmap_t) / 8;
+ const size_t end_index = DIV_ROUND_UP(end, sizeof(bitmap_t) * 8);
size_t i;
+ size_t start_bitindex;
+ size_t end_bitindex;
size_t c;
for (i = start_index; i < end_index; i++)
{
- if (bitmap[i] == 0xff)
+ if (bitmap[i] == ~((bitmap_t) 0))
continue;
- for (c = MAX(i * 8, start); c < MIN((i + 1) * 8, end); c++)
+ start_bitindex = MAX(i * sizeof(bitmap_t) * 8, start);
+ end_bitindex = MIN((i + 1) * sizeof(bitmap_t) * 8, end);
+ for (c = start_bitindex; c < end_bitindex; c++)
if (BMAP_GET(bitmap, c) == 0)
{
BMAP_SET(bitmap, c);
{
if (ef->cmap.dirty)
{
- exfat_pwrite(ef->dev, ef->cmap.chunk, (ef->cmap.chunk_size + 7) / 8,
- exfat_c2o(ef, ef->cmap.start_cluster));
+ /* FIXME handle I/O error */
+ if (exfat_pwrite(ef->dev, ef->cmap.chunk,
+ BMAP_SIZE(ef->cmap.chunk_size),
+ exfat_c2o(ef, ef->cmap.start_cluster)) < 0)
+ exfat_bug("failed to write clusters bitmap");
ef->cmap.dirty = false;
}
}
fat_offset = s2o(ef, le32_to_cpu(ef->sb->fat_sector_start))
+ current * sizeof(cluster_t);
next_le32 = cpu_to_le32(next);
- exfat_pwrite(ef->dev, &next_le32, sizeof(next_le32), fat_offset);
+ /* FIXME handle I/O error */
+ if (exfat_pwrite(ef->dev, &next_le32, sizeof(next_le32), fat_offset) < 0)
+ exfat_bug("failed to write the next cluster");
}
static cluster_t allocate_cluster(struct exfat* ef, cluster_t hint)
return 0;
}
-static void erase_raw(struct exfat* ef, size_t size, off_t offset)
+static bool erase_raw(struct exfat* ef, size_t size, off_t offset)
{
- exfat_pwrite(ef->dev, ef->zero_cluster, size, offset);
+ if (exfat_pwrite(ef->dev, ef->zero_cluster, size, offset) < 0)
+ {
+ exfat_error("failed to erase %zu bytes at %"PRId64, size, offset);
+ return false;
+ }
+ return true;
}
static int erase_range(struct exfat* ef, struct exfat_node* node,
return -EIO;
}
/* erase from the beginning to the closest cluster boundary */
- erase_raw(ef, MIN(cluster_boundary, end) - begin,
- exfat_c2o(ef, cluster) + begin % CLUSTER_SIZE(*ef->sb));
+ if (!erase_raw(ef, MIN(cluster_boundary, end) - begin,
+ exfat_c2o(ef, cluster) + begin % CLUSTER_SIZE(*ef->sb)))
+ return -EIO;
/* erase whole clusters */
while (cluster_boundary < end)
{
/* the cluster cannot be invalid because we have just allocated it */
if (CLUSTER_INVALID(cluster))
exfat_bug("invalid cluster 0x%x after allocation", cluster);
- erase_raw(ef, CLUSTER_SIZE(*ef->sb), exfat_c2o(ef, cluster));
+ if (!erase_raw(ef, CLUSTER_SIZE(*ef->sb), exfat_c2o(ef, cluster)))
+ return -EIO;
cluster_boundary += CLUSTER_SIZE(*ef->sb);
}
return 0;