OSDN Git Service

Improve clusters allocation algorithm.
authorrelan <relan@users.noreply.github.com>
Sat, 7 Nov 2009 09:54:36 +0000 (09:54 +0000)
committerrelan <relan@users.noreply.github.com>
Mon, 24 Aug 2015 05:26:10 +0000 (08:26 +0300)
Now it attempts to allocate cluster right after the previous one to keep
a file contiguous.

libexfat/cluster.c

index 9a25c00..668acea 100644 (file)
@@ -149,11 +149,17 @@ static void erase_cluster(struct exfat* ef, cluster_t cluster)
                                exfat_c2o(ef, cluster) + i * block_size, ef->fd);
 }
 
-static cluster_t allocate_cluster(struct exfat* ef)
+static cluster_t allocate_cluster(struct exfat* ef, cluster_t hint)
 {
-       cluster_t cluster = find_bit_and_set(ef->cmap.chunk, 0,
-                       ef->cmap.chunk_size);
+       cluster_t cluster;
 
+       hint -= EXFAT_FIRST_DATA_CLUSTER;
+       if (hint >= ef->cmap.chunk_size)
+               hint = 0;
+
+       cluster = find_bit_and_set(ef->cmap.chunk, hint, ef->cmap.chunk_size);
+       if (cluster == EXFAT_CLUSTER_END)
+               cluster = find_bit_and_set(ef->cmap.chunk, 0, hint);
        if (cluster == EXFAT_CLUSTER_END)
        {
                exfat_error("no free space left");
@@ -212,7 +218,7 @@ static int grow_file(struct exfat* ef, struct exfat_node* node,
        {
                /* file does not have clusters (i.e. is empty), allocate
                   the first one for it */
-               previous = allocate_cluster(ef);
+               previous = allocate_cluster(ef, 0);
                if (CLUSTER_INVALID(previous))
                        return -ENOSPC;
                node->start_cluster = previous;
@@ -223,7 +229,7 @@ static int grow_file(struct exfat* ef, struct exfat_node* node,
 
        while (difference--)
        {
-               next = allocate_cluster(ef);
+               next = allocate_cluster(ef, previous + 1);
                if (CLUSTER_INVALID(next))
                        return -ENOSPC;
                if (next != previous - 1 && IS_CONTIGUOUS(*node))