OSDN Git Service

Set ctime to mtime to ensure we don't break programs that rely on ctime (e.g. rsync).
[android-x86/external-exfat.git] / libexfat / cluster.c
index 6d70c25..7251c31 100644 (file)
 #include <errno.h>
 #include <string.h>
 
-#define BMAP_GET(bitmap, index) ((bitmap)[(index) / 8] & (1u << ((index) % 8)))
-#define BMAP_SET(bitmap, index) (bitmap)[(index) / 8] |= (1u << ((index) % 8))
-#define BMAP_CLR(bitmap, index) (bitmap)[(index) / 8] &= ~(1u << ((index) % 8))
-
 /*
- * Block to absolute offset.
+ * Sector to absolute offset.
  */
-static off_t b2o(const struct exfat* ef, off_t block)
+static off_t s2o(const struct exfat* ef, off_t sector)
 {
-       return block << ef->sb->block_bits;
+       return sector << ef->sb->sector_bits;
 }
 
 /*
- * Cluster to block.
+ * Cluster to sector.
  */
-static off_t c2b(const struct exfat* ef, cluster_t cluster)
+static off_t c2s(const struct exfat* ef, cluster_t cluster)
 {
        if (cluster < EXFAT_FIRST_DATA_CLUSTER)
                exfat_bug("invalid cluster number %u", cluster);
-       return le32_to_cpu(ef->sb->cluster_block_start) +
-               ((off_t) (cluster - EXFAT_FIRST_DATA_CLUSTER) << ef->sb->bpc_bits);
+       return le32_to_cpu(ef->sb->cluster_sector_start) +
+               ((off_t) (cluster - EXFAT_FIRST_DATA_CLUSTER) << ef->sb->spc_bits);
 }
 
 /*
@@ -50,7 +46,16 @@ static off_t c2b(const struct exfat* ef, cluster_t cluster)
  */
 off_t exfat_c2o(const struct exfat* ef, cluster_t cluster)
 {
-       return b2o(ef, c2b(ef, cluster));
+       return s2o(ef, c2s(ef, cluster));
+}
+
+/*
+ * Sector to cluster.
+ */
+static cluster_t s2c(const struct exfat* ef, off_t sector)
+{
+       return ((sector - le32_to_cpu(ef->sb->cluster_sector_start)) >>
+                       ef->sb->spc_bits) + EXFAT_FIRST_DATA_CLUSTER;
 }
 
 /*
@@ -73,7 +78,7 @@ cluster_t exfat_next_cluster(const struct exfat* ef,
 
        if (IS_CONTIGUOUS(*node))
                return cluster + 1;
-       fat_offset = b2o(ef, le32_to_cpu(ef->sb->fat_block_start))
+       fat_offset = s2o(ef, le32_to_cpu(ef->sb->fat_sector_start))
                + cluster * sizeof(cluster_t);
        exfat_read_raw(&next, sizeof(next), fat_offset, ef->fd);
        return le32_to_cpu(next);
@@ -149,12 +154,14 @@ static void set_next_cluster(const struct exfat* ef, int contiguous,
                cluster_t current, cluster_t next)
 {
        off_t fat_offset;
+       le32_t next_le32;
 
        if (contiguous)
                return;
-       fat_offset = b2o(ef, le32_to_cpu(ef->sb->fat_block_start))
+       fat_offset = s2o(ef, le32_to_cpu(ef->sb->fat_sector_start))
                + current * sizeof(cluster_t);
-       exfat_write_raw(&next, sizeof(next), fat_offset, ef->fd);
+       next_le32 = cpu_to_le32(next);
+       exfat_write_raw(&next_le32, sizeof(next_le32), fat_offset, ef->fd);
 }
 
 static cluster_t allocate_cluster(struct exfat* ef, cluster_t hint)
@@ -318,37 +325,38 @@ static int shrink_file(struct exfat* ef, struct exfat_node* node,
 
 static void erase_raw(struct exfat* ef, size_t size, off_t offset)
 {
-       exfat_write_raw(ef->zero_block, size, offset, ef->fd);
+       exfat_write_raw(ef->zero_cluster, size, offset, ef->fd);
 }
 
 static int erase_range(struct exfat* ef, struct exfat_node* node,
                uint64_t begin, uint64_t end)
 {
-       uint64_t block_boundary;
+       uint64_t cluster_boundary;
        cluster_t cluster;
 
        if (begin >= end)
                return 0;
 
-       block_boundary = (node->size | (BLOCK_SIZE(*ef->sb) - 1)) + 1;
+       cluster_boundary = (begin | (CLUSTER_SIZE(*ef->sb) - 1)) + 1;
        cluster = exfat_advance_cluster(ef, node,
-                       node->size / CLUSTER_SIZE(*ef->sb));
+                       begin / CLUSTER_SIZE(*ef->sb));
        if (CLUSTER_INVALID(cluster))
        {
                exfat_error("invalid cluster in file");
                return -EIO;
        }
-       /* erase from the beginning to the closest block boundary */
-       erase_raw(ef, MIN(block_boundary, end) - node->size,
-                       exfat_c2o(ef, cluster) + node->size % CLUSTER_SIZE(*ef->sb));
-       /* erase whole blocks */
-       while (block_boundary < end)
+       /* 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));
+       /* erase whole clusters */
+       while (cluster_boundary < end)
        {
-               if (block_boundary % CLUSTER_SIZE(*ef->sb) == 0)
-                       cluster = exfat_next_cluster(ef, node, cluster);
-               erase_raw(ef, BLOCK_SIZE(*ef->sb),
-                       exfat_c2o(ef, cluster) + block_boundary % CLUSTER_SIZE(*ef->sb));
-               block_boundary += BLOCK_SIZE(*ef->sb);
+               cluster = exfat_next_cluster(ef, node, cluster);
+               /* the cluster cannot be invalid because we have just allocated it */
+               if (CLUSTER_INVALID(cluster))
+                       exfat_bug("invalid cluster in file");
+               erase_raw(ef, CLUSTER_SIZE(*ef->sb), exfat_c2o(ef, cluster));
+               cluster_boundary += CLUSTER_SIZE(*ef->sb);
        }
        return 0;
 }
@@ -359,6 +367,9 @@ int exfat_truncate(struct exfat* ef, struct exfat_node* node, uint64_t size)
        uint32_t c2 = bytes2clusters(ef, size);
        int rc = 0;
 
+       if (node->references == 0 && node->parent)
+               exfat_bug("no references, node changes can be lost");
+
        if (node->size == size)
                return 0;
 
@@ -380,7 +391,7 @@ int exfat_truncate(struct exfat* ef, struct exfat_node* node, uint64_t size)
        return 0;
 }
 
-uint32_t exfat_count_free_clusters(struct exfat* ef)
+uint32_t exfat_count_free_clusters(const struct exfat* ef)
 {
        uint32_t free_clusters = 0;
        uint32_t i;
@@ -390,3 +401,45 @@ uint32_t exfat_count_free_clusters(struct exfat* ef)
                        free_clusters++;
        return free_clusters;
 }
+
+static int find_used_clusters(const struct exfat* ef,
+               cluster_t* a, cluster_t* b)
+{
+       const cluster_t end = le32_to_cpu(ef->sb->cluster_count);
+
+       /* find first used cluster */
+       for (*a = *b + 1; *a < end; (*a)++)
+               if (BMAP_GET(ef->cmap.chunk, *a - EXFAT_FIRST_DATA_CLUSTER))
+                       break;
+       if (*a >= end)
+               return 1;
+
+       /* find last contiguous used cluster */
+       for (*b = *a; *b < end; (*b)++)
+               if (BMAP_GET(ef->cmap.chunk, *b - EXFAT_FIRST_DATA_CLUSTER) == 0)
+               {
+                       (*b)--;
+                       break;
+               }
+
+       return 0;
+}
+
+int exfat_find_used_sectors(const struct exfat* ef, off_t* a, off_t* b)
+{
+       cluster_t ca, cb;
+
+       if (*a == 0 && *b == 0)
+               ca = cb = EXFAT_FIRST_DATA_CLUSTER - 1;
+       else
+       {
+               ca = s2c(ef, *a);
+               cb = s2c(ef, *b);
+       }
+       if (find_used_clusters(ef, &ca, &cb) != 0)
+               return 1;
+       if (*a != 0 || *b != 0)
+               *a = c2s(ef, ca);
+       *b = c2s(ef, cb) + (CLUSTER_SIZE(*ef->sb) - 1) / SECTOR_SIZE(*ef->sb);
+       return 0;
+}