OSDN Git Service

Handle I/O error in erase_raw().
[android-x86/external-exfat.git] / libexfat / cluster.c
index d020d36..de73b55 100644 (file)
@@ -23,6 +23,7 @@
 #include "exfat.h"
 #include <errno.h>
 #include <string.h>
+#include <inttypes.h>
 
 /*
  * Sector to absolute offset.
@@ -82,7 +83,9 @@ cluster_t exfat_next_cluster(const struct exfat* ef,
                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);
 }
 
@@ -108,18 +111,22 @@ cluster_t exfat_advance_cluster(const struct exfat* ef,
        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);
@@ -129,11 +136,17 @@ static cluster_t find_bit_and_set(uint8_t* bitmap, size_t start, size_t end)
        return EXFAT_CLUSTER_END;
 }
 
-void exfat_flush_cmap(struct exfat* ef)
+void exfat_flush(struct exfat* ef)
 {
-       exfat_pwrite(ef->dev, ef->cmap.chunk, (ef->cmap.chunk_size + 7) / 8,
-                       exfat_c2o(ef, ef->cmap.start_cluster));
-       ef->cmap.dirty = false;
+       if (ef->cmap.dirty)
+       {
+               /* 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;
+       }
 }
 
 static void set_next_cluster(const struct exfat* ef, bool contiguous,
@@ -147,7 +160,9 @@ static void set_next_cluster(const struct exfat* ef, bool contiguous,
        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)
@@ -308,9 +323,14 @@ static int shrink_file(struct exfat* ef, struct exfat_node* node,
        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,
@@ -331,8 +351,9 @@ 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)
        {
@@ -340,7 +361,8 @@ static int erase_range(struct exfat* ef, struct exfat_node* node,
                /* 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;