OSDN Git Service

Repairing: implement invalid node checksum fix.
[android-x86/external-exfat.git] / libexfat / node.c
index a0809da..9270ccf 100644 (file)
@@ -3,7 +3,7 @@
        exFAT file system implementation library.
 
        Free exFAT implementation.
-       Copyright (C) 2010-2016  Andrew Nayenko
+       Copyright (C) 2010-2018  Andrew Nayenko
 
        This program is free software; you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
 #include <string.h>
 #include <inttypes.h>
 
-/* on-disk nodes iterator */
-struct iterator
-{
-       cluster_t cluster;
-       off_t offset;
-       char* chunk;
-};
+#define EXFAT_ENTRY_NONE (-1)
 
 struct exfat_node* exfat_get_node(struct exfat_node* node)
 {
@@ -83,81 +77,6 @@ int exfat_cleanup_node(struct exfat* ef, struct exfat_node* node)
        return rc;
 }
 
-static int opendir(struct exfat* ef, const struct exfat_node* dir,
-               struct iterator* it)
-{
-       char buffer[EXFAT_UTF8_NAME_BUFFER_MAX];
-
-       if (!(dir->attrib & EXFAT_ATTRIB_DIR))
-       {
-               exfat_get_name(dir, buffer);
-               exfat_bug("'%s' is not a directory", buffer);
-       }
-       if (CLUSTER_INVALID(dir->start_cluster))
-       {
-               exfat_get_name(dir, buffer);
-               exfat_error("'%s' directory starts with invalid cluster %#x", buffer,
-                               dir->start_cluster);
-               return -EIO;
-       }
-       it->cluster = dir->start_cluster;
-       it->offset = 0;
-       it->chunk = malloc(CLUSTER_SIZE(*ef->sb));
-       if (it->chunk == NULL)
-       {
-               exfat_error("failed to allocate memory for directory cluster");
-               return -ENOMEM;
-       }
-       if (exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb),
-                       exfat_c2o(ef, it->cluster)) < 0)
-       {
-               free(it->chunk);
-               exfat_get_name(dir, buffer);
-               exfat_error("failed to read '%s' directory cluster %#x", buffer,
-                               it->cluster);
-               return -EIO;
-       }
-       return 0;
-}
-
-static void closedir(struct iterator* it)
-{
-       it->cluster = 0;
-       it->offset = 0;
-       free(it->chunk);
-       it->chunk = NULL;
-}
-
-static bool fetch_next_entry(struct exfat* ef, const struct exfat_node* parent,
-               struct iterator* it)
-{
-       /* move iterator to the next entry in the directory */
-       it->offset += sizeof(struct exfat_entry);
-       /* fetch the next cluster if needed */
-       if ((it->offset & (CLUSTER_SIZE(*ef->sb) - 1)) == 0)
-       {
-               /* reached the end of directory; the caller should check this
-                  condition too */
-               if (it->offset >= parent->size)
-                       return true;
-               it->cluster = exfat_next_cluster(ef, parent, it->cluster);
-               if (CLUSTER_INVALID(it->cluster))
-               {
-                       exfat_error("invalid cluster 0x%x while reading directory",
-                                       it->cluster);
-                       return false;
-               }
-               if (exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb),
-                               exfat_c2o(ef, it->cluster)) < 0)
-               {
-                       exfat_error("failed to read the next directory cluster %#x",
-                                       it->cluster);
-                       return false;
-               }
-       }
-       return true;
-}
-
 static int read_entries(struct exfat* ef, struct exfat_node* dir,
                struct exfat_entry* entries, int n, off_t offset)
 {
@@ -230,16 +149,68 @@ static void init_node_meta2(struct exfat_node* node,
        node->is_contiguous = ((meta2->flags & EXFAT_FLAG_CONTIGUOUS) != 0);
 }
 
-static const struct exfat_entry* get_entry_ptr(const struct exfat* ef,
-               const struct iterator* it)
+static void init_node_name(struct exfat_node* node,
+               const struct exfat_entry* entries, int n)
+{
+       int i;
+
+       for (i = 0; i < n; i++)
+               memcpy(node->name + i * EXFAT_ENAME_MAX,
+                               ((const struct exfat_entry_name*) &entries[i])->name,
+                               EXFAT_ENAME_MAX * sizeof(le16_t));
+}
+
+static bool check_entries(const struct exfat_entry* entry, int n)
 {
-       return (const struct exfat_entry*)
-                       (it->chunk + it->offset % CLUSTER_SIZE(*ef->sb));
+       int previous = EXFAT_ENTRY_NONE;
+       int current;
+       int i;
+
+       /* check transitions between entries types */
+       for (i = 0; i < n + 1; previous = current, i++)
+       {
+               bool valid = false;
+
+               current = (i < n) ? entry[i].type : EXFAT_ENTRY_NONE;
+               switch (previous)
+               {
+               case EXFAT_ENTRY_NONE:
+                       valid = (current == EXFAT_ENTRY_FILE);
+                       break;
+               case EXFAT_ENTRY_FILE:
+                       valid = (current == EXFAT_ENTRY_FILE_INFO);
+                       break;
+               case EXFAT_ENTRY_FILE_INFO:
+                       valid = (current == EXFAT_ENTRY_FILE_NAME);
+                       break;
+               case EXFAT_ENTRY_FILE_NAME:
+                       valid = (current == EXFAT_ENTRY_FILE_NAME ||
+                                current == EXFAT_ENTRY_NONE ||
+                                current >= EXFAT_ENTRY_FILE_TAIL);
+                       break;
+               case EXFAT_ENTRY_FILE_TAIL ... 0xff:
+                       valid = (current >= EXFAT_ENTRY_FILE_TAIL ||
+                                current == EXFAT_ENTRY_NONE);
+                       break;
+               }
+
+               if (!valid)
+               {
+                       exfat_error("unexpected entry type %#x after %#x at %d/%d",
+                                       current, previous, i, n);
+                       return false;
+               }
+       }
+       return true;
 }
 
-static bool check_node(const struct exfat_node* node, uint16_t actual_checksum,
-               uint16_t reference_checksum, uint64_t valid_size, int cluster_size)
+static bool check_node(const struct exfat* ef, struct exfat_node* node,
+               le16_t actual_checksum, const struct exfat_entry_meta1* meta1,
+               const struct exfat_entry_meta2* meta2)
 {
+       int cluster_size = CLUSTER_SIZE(*ef->sb);
+       uint64_t clusters_heap_size =
+                       (uint64_t) le32_to_cpu(ef->sb->cluster_count) * cluster_size;
        char buffer[EXFAT_UTF8_NAME_BUFFER_MAX];
        bool ret = true;
 
@@ -247,12 +218,13 @@ static bool check_node(const struct exfat_node* node, uint16_t actual_checksum,
           Validate checksum first. If it's invalid all other fields probably
           contain just garbage.
        */
-       if (actual_checksum != reference_checksum)
+       if (le16_to_cpu(actual_checksum) != le16_to_cpu(meta1->checksum))
        {
                exfat_get_name(node, buffer);
                exfat_error("'%s' has invalid checksum (%#hx != %#hx)", buffer,
-                               actual_checksum, reference_checksum);
-               ret = false;
+                               le16_to_cpu(actual_checksum), le16_to_cpu(meta1->checksum));
+               if (!EXFAT_REPAIR(invalid_node_checksum, ef, node))
+                       ret = false;
        }
 
        /*
@@ -261,19 +233,20 @@ static bool check_node(const struct exfat_node* node, uint16_t actual_checksum,
           cannot be greater than file size. See SetFileValidData() function
           description in MSDN.
        */
-       if (valid_size > node->size)
+       if (le64_to_cpu(meta2->valid_size) > node->size)
        {
                exfat_get_name(node, buffer);
                exfat_error("'%s' has valid size (%"PRIu64") greater than size "
-                               "(%"PRIu64")", buffer, valid_size, node->size);
+                               "(%"PRIu64")", buffer, le64_to_cpu(meta2->valid_size),
+                               node->size);
                ret = false;
        }
 
        /*
           Empty file must have zero start cluster. Non-empty file must start
           with a valid cluster. Directories cannot be empty (i.e. must always
-          have a valid start cluster), but we will check this later in opendir()
-          to give user a chance to read current directory.
+          have a valid start cluster), but we will check this later while
+          reading that directory to give user a chance to read this directory.
        */
        if (node->size == 0 && node->start_cluster != EXFAT_CLUSTER_FREE)
        {
@@ -282,7 +255,7 @@ static bool check_node(const struct exfat_node* node, uint16_t actual_checksum,
                                node->start_cluster);
                ret = false;
        }
-       if (node->size > 0 && CLUSTER_INVALID(node->start_cluster))
+       if (node->size > 0 && CLUSTER_INVALID(*ef->sb, node->start_cluster))
        {
                exfat_get_name(node, buffer);
                exfat_error("'%s' points to invalid cluster %#x", buffer,
@@ -290,6 +263,15 @@ static bool check_node(const struct exfat_node* node, uint16_t actual_checksum,
                ret = false;
        }
 
+       /* File or directory cannot be larger than clusters heap. */
+       if (node->size > clusters_heap_size)
+       {
+               exfat_get_name(node, buffer);
+               exfat_error("'%s' is larger than clusters heap: %"PRIu64" > %"PRIu64,
+                               buffer, node->size, clusters_heap_size);
+               ret = false;
+       }
+
        /* Empty file or directory must be marked as non-contiguous. */
        if (node->size == 0 && node->is_contiguous)
        {
@@ -311,6 +293,73 @@ static bool check_node(const struct exfat_node* node, uint16_t actual_checksum,
        return ret;
 }
 
+static int parse_file_entries(struct exfat* ef, struct exfat_node* node,
+               const struct exfat_entry* entries, int n)
+{
+       const struct exfat_entry_meta1* meta1;
+       const struct exfat_entry_meta2* meta2;
+       int mandatory_entries;
+
+       if (!check_entries(entries, n))
+               return -EIO;
+
+       meta1 = (const struct exfat_entry_meta1*) &entries[0];
+       if (meta1->continuations < 2)
+       {
+               exfat_error("too few continuations (%hhu)", meta1->continuations);
+               return -EIO;
+       }
+       meta2 = (const struct exfat_entry_meta2*) &entries[1];
+       if (meta2->flags & ~(EXFAT_FLAG_ALWAYS1 | EXFAT_FLAG_CONTIGUOUS))
+       {
+               exfat_error("unknown flags in meta2 (%#hhx)", meta2->flags);
+               return -EIO;
+       }
+       mandatory_entries = 2 + DIV_ROUND_UP(meta2->name_length, EXFAT_ENAME_MAX);
+       if (meta1->continuations < mandatory_entries - 1)
+       {
+               exfat_error("too few continuations (%hhu < %d)",
+                               meta1->continuations, mandatory_entries - 1);
+               return -EIO;
+       }
+
+       init_node_meta1(node, meta1);
+       init_node_meta2(node, meta2);
+       init_node_name(node, entries + 2, mandatory_entries - 2);
+
+       if (!check_node(ef, node, exfat_calc_checksum(entries, n), meta1, meta2))
+               return -EIO;
+
+       return 0;
+}
+
+static int parse_file_entry(struct exfat* ef, struct exfat_node* parent,
+               struct exfat_node** node, off_t* offset, int n)
+{
+       struct exfat_entry entries[n];
+       int rc;
+
+       rc = read_entries(ef, parent, entries, n, *offset);
+       if (rc != 0)
+               return rc;
+
+       /* a new node has zero references */
+       *node = allocate_node();
+       if (*node == NULL)
+               return -ENOMEM;
+       (*node)->entry_offset = *offset;
+
+       rc = parse_file_entries(ef, *node, entries, n);
+       if (rc != 0)
+       {
+               free(*node);
+               return rc;
+       }
+
+       *offset += sizeof(struct exfat_entry[n]);
+       return 0;
+}
+
 static void decompress_upcase(uint16_t* output, const le16_t* source,
                size_t size)
 {
@@ -332,135 +381,43 @@ static void decompress_upcase(uint16_t* output, const le16_t* source,
 }
 
 /*
- * Reads one entry in directory at position pointed by iterator and fills
- * node structure.
+ * Read one entry in a directory at offset position and build a new node
+ * structure.
  */
-static int readdir(struct exfat* ef, const struct exfat_node* parent,
-               struct exfat_node** node, struct iterator* it)
+static int readdir(struct exfat* ef, struct exfat_node* parent,
+               struct exfat_node** node, off_t* offset)
 {
-       int rc = -EIO;
-       const struct exfat_entry* entry;
+       int rc;
+       struct exfat_entry entry;
        const struct exfat_entry_meta1* meta1;
-       const struct exfat_entry_meta2* meta2;
-       const struct exfat_entry_name* file_name;
        const struct exfat_entry_upcase* upcase;
        const struct exfat_entry_bitmap* bitmap;
        const struct exfat_entry_label* label;
-       uint8_t continuations = 0;
-       le16_t* namep = NULL;
-       uint16_t reference_checksum = 0;
-       uint16_t actual_checksum = 0;
-       uint64_t valid_size = 0;
        uint64_t upcase_size = 0;
        le16_t* upcase_comp = NULL;
 
-       *node = NULL;
-
        for (;;)
        {
-               if (it->offset >= parent->size)
-               {
-                       if (continuations != 0)
-                       {
-                               exfat_error("expected %hhu continuations", continuations);
-                               goto error;
-                       }
-                       return -ENOENT; /* that's OK, means end of directory */
-               }
+               rc = read_entries(ef, parent, &entry, 1, *offset);
+               if (rc != 0)
+                       return rc;
 
-               entry = get_entry_ptr(ef, it);
-               switch (entry->type)
+               switch (entry.type)
                {
                case EXFAT_ENTRY_FILE:
-                       if (continuations != 0)
-                       {
-                               exfat_error("expected %hhu continuations before new entry",
-                                               continuations);
-                               goto error;
-                       }
-                       meta1 = (const struct exfat_entry_meta1*) entry;
-                       continuations = meta1->continuations;
-                       /* each file entry must have at least 2 continuations:
-                          info and name */
-                       if (continuations < 2)
-                       {
-                               exfat_error("too few continuations (%hhu)", continuations);
-                               goto error;
-                       }
-                       if (continuations > 1 +
-                                       DIV_ROUND_UP(EXFAT_NAME_MAX, EXFAT_ENAME_MAX))
-                       {
-                               exfat_error("too many continuations (%hhu)", continuations);
-                               goto error;
-                       }
-                       reference_checksum = le16_to_cpu(meta1->checksum);
-                       actual_checksum = exfat_start_checksum(meta1);
-                       *node = allocate_node();
-                       if (*node == NULL)
-                       {
-                               rc = -ENOMEM;
-                               goto error;
-                       }
-                       /* new node has zero reference counter */
-                       (*node)->entry_cluster = it->cluster;
-                       (*node)->entry_offset = it->offset;
-                       init_node_meta1(*node, meta1);
-                       namep = (*node)->name;
-                       break;
-
-               case EXFAT_ENTRY_FILE_INFO:
-                       if (continuations < 2)
-                       {
-                               exfat_error("unexpected continuation (%hhu)",
-                                               continuations);
-                               goto error;
-                       }
-                       meta2 = (const struct exfat_entry_meta2*) entry;
-                       if (meta2->flags & ~(EXFAT_FLAG_ALWAYS1 | EXFAT_FLAG_CONTIGUOUS))
-                       {
-                               exfat_error("unknown flags in meta2 (0x%hhx)", meta2->flags);
-                               goto error;
-                       }
-                       init_node_meta2(*node, meta2);
-                       actual_checksum = exfat_add_checksum(entry, actual_checksum);
-                       valid_size = le64_to_cpu(meta2->valid_size);
-                       --continuations;
-                       break;
-
-               case EXFAT_ENTRY_FILE_NAME:
-                       if (continuations == 0)
-                       {
-                               exfat_error("unexpected continuation");
-                               goto error;
-                       }
-                       file_name = (const struct exfat_entry_name*) entry;
-                       actual_checksum = exfat_add_checksum(entry, actual_checksum);
-
-                       memcpy(namep, file_name->name,
-                                       MIN(EXFAT_ENAME_MAX,
-                                               ((*node)->name + EXFAT_NAME_MAX - namep)) *
-                                       sizeof(le16_t));
-                       namep += EXFAT_ENAME_MAX;
-                       if (--continuations == 0)
-                       {
-                               if (!check_node(*node, actual_checksum, reference_checksum,
-                                               valid_size, CLUSTER_SIZE(*ef->sb)))
-                                       goto error;
-                               if (!fetch_next_entry(ef, parent, it))
-                                       goto error;
-                               return 0; /* entry completed */
-                       }
-                       break;
+                       meta1 = (const struct exfat_entry_meta1*) &entry;
+                       return parse_file_entry(ef, parent, node, offset,
+                                       1 + meta1->continuations);
 
                case EXFAT_ENTRY_UPCASE:
                        if (ef->upcase != NULL)
                                break;
-                       upcase = (const struct exfat_entry_upcase*) entry;
-                       if (CLUSTER_INVALID(le32_to_cpu(upcase->start_cluster)))
+                       upcase = (const struct exfat_entry_upcase*) &entry;
+                       if (CLUSTER_INVALID(*ef->sb, le32_to_cpu(upcase->start_cluster)))
                        {
                                exfat_error("invalid cluster 0x%x in upcase table",
                                                le32_to_cpu(upcase->start_cluster));
-                               goto error;
+                               return -EIO;
                        }
                        upcase_size = le64_to_cpu(upcase->size);
                        if (upcase_size == 0 ||
@@ -469,15 +426,14 @@ static int readdir(struct exfat* ef, const struct exfat_node* parent,
                        {
                                exfat_error("bad upcase table size (%"PRIu64" bytes)",
                                                upcase_size);
-                               goto error;
+                               return -EIO;
                        }
                        upcase_comp = malloc(upcase_size);
                        if (upcase_comp == NULL)
                        {
                                exfat_error("failed to allocate upcase table (%"PRIu64" bytes)",
                                                upcase_size);
-                               rc = -ENOMEM;
-                               goto error;
+                               return -ENOMEM;
                        }
 
                        /* read compressed upcase table */
@@ -489,7 +445,7 @@ static int readdir(struct exfat* ef, const struct exfat_node* parent,
                                                "(%"PRIu64" bytes starting at cluster %#x)",
                                                upcase_size,
                                                le32_to_cpu(upcase->start_cluster));
-                               goto error;
+                               return -EIO;
                        }
 
                        /* decompress upcase table */
@@ -498,8 +454,7 @@ static int readdir(struct exfat* ef, const struct exfat_node* parent,
                        {
                                free(upcase_comp);
                                exfat_error("failed to allocate decompressed upcase table");
-                               rc = -ENOMEM;
-                               goto error;
+                               return -ENOMEM;
                        }
                        decompress_upcase(ef->upcase, upcase_comp,
                                        upcase_size / sizeof(uint16_t));
@@ -507,23 +462,22 @@ static int readdir(struct exfat* ef, const struct exfat_node* parent,
                        break;
 
                case EXFAT_ENTRY_BITMAP:
-                       bitmap = (const struct exfat_entry_bitmap*) entry;
+                       bitmap = (const struct exfat_entry_bitmap*) &entry;
                        ef->cmap.start_cluster = le32_to_cpu(bitmap->start_cluster);
-                       if (CLUSTER_INVALID(ef->cmap.start_cluster))
+                       if (CLUSTER_INVALID(*ef->sb, ef->cmap.start_cluster))
                        {
                                exfat_error("invalid cluster 0x%x in clusters bitmap",
                                                ef->cmap.start_cluster);
-                               goto error;
+                               return -EIO;
                        }
-                       ef->cmap.size = le32_to_cpu(ef->sb->cluster_count) -
-                               EXFAT_FIRST_DATA_CLUSTER;
+                       ef->cmap.size = le32_to_cpu(ef->sb->cluster_count);
                        if (le64_to_cpu(bitmap->size) < DIV_ROUND_UP(ef->cmap.size, 8))
                        {
                                exfat_error("invalid clusters bitmap size: %"PRIu64
                                                " (expected at least %u)",
                                                le64_to_cpu(bitmap->size),
                                                DIV_ROUND_UP(ef->cmap.size, 8));
-                               goto error;
+                               return -EIO;
                        }
                        /* FIXME bitmap can be rather big, up to 512 MB */
                        ef->cmap.chunk_size = ef->cmap.size;
@@ -532,8 +486,7 @@ static int readdir(struct exfat* ef, const struct exfat_node* parent,
                        {
                                exfat_error("failed to allocate clusters bitmap chunk "
                                                "(%"PRIu64" bytes)", le64_to_cpu(bitmap->size));
-                               rc = -ENOMEM;
-                               goto error;
+                               return -ENOMEM;
                        }
 
                        if (exfat_pread(ef->dev, ef->cmap.chunk,
@@ -543,55 +496,37 @@ static int readdir(struct exfat* ef, const struct exfat_node* parent,
                                exfat_error("failed to read clusters bitmap "
                                                "(%"PRIu64" bytes starting at cluster %#x)",
                                                le64_to_cpu(bitmap->size), ef->cmap.start_cluster);
-                               goto error;
+                               return -EIO;
                        }
                        break;
 
                case EXFAT_ENTRY_LABEL:
-                       label = (const struct exfat_entry_label*) entry;
+                       label = (const struct exfat_entry_label*) &entry;
                        if (label->length > EXFAT_ENAME_MAX)
                        {
                                exfat_error("too long label (%hhu chars)", label->length);
-                               goto error;
+                               return -EIO;
                        }
                        if (utf16_to_utf8(ef->label, label->name,
                                                sizeof(ef->label), EXFAT_ENAME_MAX) != 0)
-                               goto error;
+                               return -EIO;
                        break;
 
                default:
-                       if (!(entry->type & EXFAT_ENTRY_VALID))
+                       if (!(entry.type & EXFAT_ENTRY_VALID))
                                break; /* deleted entry, ignore it */
-                       if (!(entry->type & EXFAT_ENTRY_OPTIONAL))
-                       {
-                               exfat_error("unknown entry type %#hhx", entry->type);
-                               goto error;
-                       }
-                       /* optional entry, warn and skip */
-                       exfat_warn("unknown entry type %#hhx", entry->type);
-                       if (continuations == 0)
-                       {
-                               exfat_error("unexpected continuation");
-                               goto error;
-                       }
-                       --continuations;
-                       break;
-               }
 
-               if (!fetch_next_entry(ef, parent, it))
-                       goto error;
+                       exfat_error("unknown entry type %#hhx", entry.type);
+                       return -EIO;
+               }
+               *offset += sizeof(entry);
        }
        /* we never reach here */
-
-error:
-       free(*node);
-       *node = NULL;
-       return rc;
 }
 
 int exfat_cache_directory(struct exfat* ef, struct exfat_node* dir)
 {
-       struct iterator it;
+       off_t offset = 0;
        int rc;
        struct exfat_node* node;
        struct exfat_node* current = NULL;
@@ -599,10 +534,7 @@ int exfat_cache_directory(struct exfat* ef, struct exfat_node* dir)
        if (dir->is_cached)
                return 0; /* already cached */
 
-       rc = opendir(ef, dir, &it);
-       if (rc != 0)
-               return rc;
-       while ((rc = readdir(ef, dir, &node, &it)) == 0)
+       while ((rc = readdir(ef, dir, &node, &offset)) == 0)
        {
                node->parent = dir;
                if (current != NULL)
@@ -615,7 +547,6 @@ int exfat_cache_directory(struct exfat* ef, struct exfat_node* dir)
 
                current = node;
        }
-       closedir(&it);
 
        if (rc != -ENOENT)
        {
@@ -691,7 +622,7 @@ void exfat_reset_cache(struct exfat* ef)
 
 int exfat_flush_node(struct exfat* ef, struct exfat_node* node)
 {
-       struct exfat_entry entries[2];
+       struct exfat_entry entries[1 + node->continuations];
        struct exfat_entry_meta1* meta1 = (struct exfat_entry_meta1*) &entries[0];
        struct exfat_entry_meta2* meta2 = (struct exfat_entry_meta2*) &entries[1];
        int rc;
@@ -705,25 +636,17 @@ int exfat_flush_node(struct exfat* ef, struct exfat_node* node)
        if (node->parent == NULL)
                return 0; /* do not flush unlinked node */
 
-       rc = read_entries(ef, node->parent, entries, 2, node->entry_offset);
+       rc = read_entries(ef, node->parent, entries, 1 + node->continuations,
+                       node->entry_offset);
        if (rc != 0)
                return rc;
-
-       if (meta1->type != EXFAT_ENTRY_FILE)
-       {
-               exfat_error("invalid type of meta1: %#hhx", meta1->type);
+       if (!check_entries(entries, 1 + node->continuations))
                return -EIO;
-       }
+
        meta1->attrib = cpu_to_le16(node->attrib);
        exfat_unix2exfat(node->mtime, &meta1->mdate, &meta1->mtime,
                        &meta1->mtime_cs);
        exfat_unix2exfat(node->atime, &meta1->adate, &meta1->atime, NULL);
-
-       if (meta2->type != EXFAT_ENTRY_FILE_INFO)
-       {
-               exfat_error("invalid type of meta2: %#hhx", meta2->type);
-               return -EIO;
-       }
        meta2->size = meta2->valid_size = cpu_to_le64(node->size);
        meta2->start_cluster = cpu_to_le32(node->start_cluster);
        meta2->flags = EXFAT_FLAG_ALWAYS1;
@@ -732,9 +655,9 @@ int exfat_flush_node(struct exfat* ef, struct exfat_node* node)
                meta2->flags |= EXFAT_FLAG_CONTIGUOUS;
        /* name hash remains unchanged, no need to recalculate it */
 
-       meta1->checksum = exfat_calc_checksum(meta1, meta2, node->name);
-
-       rc = write_entries(ef, node->parent, entries, 2, node->entry_offset);
+       meta1->checksum = exfat_calc_checksum(entries, 1 + node->continuations);
+       rc = write_entries(ef, node->parent, entries, 1 + node->continuations,
+                       node->entry_offset);
        if (rc != 0)
                return rc;
 
@@ -933,7 +856,8 @@ static int find_slot(struct exfat* ef, struct exfat_node* dir,
                                        free(dmap);
                                        return -EIO;
                                case -EINVAL:
-                                       /* slot is occupied, continue searching */
+                                       /* slot at (i-n) is occupied, go back and check (i-n+1) */
+                                       i -= contiguous - 1;
                                        contiguous = 0;
                                        break;
                                }
@@ -953,7 +877,7 @@ static int find_slot(struct exfat* ef, struct exfat_node* dir,
 }
 
 static int commit_entry(struct exfat* ef, struct exfat_node* dir,
-               const le16_t* name, cluster_t cluster, off_t offset, uint16_t attrib)
+               const le16_t* name, off_t offset, uint16_t attrib)
 {
        struct exfat_node* node;
        const size_t name_length = utf16_length(name);
@@ -981,8 +905,6 @@ static int commit_entry(struct exfat* ef, struct exfat_node* dir,
        meta2->name_hash = exfat_calc_name_hash(ef, name, name_length);
        meta2->start_cluster = cpu_to_le32(EXFAT_CLUSTER_FREE);
 
-       meta1->checksum = exfat_calc_checksum(meta1, meta2, name);
-
        for (i = 0; i < name_entries; i++)
        {
                struct exfat_entry_name* name_entry;
@@ -994,6 +916,7 @@ static int commit_entry(struct exfat* ef, struct exfat_node* dir,
                                EXFAT_ENAME_MAX * sizeof(le16_t));
        }
 
+       meta1->checksum = exfat_calc_checksum(entries, 2 + name_entries);
        rc = write_entries(ef, dir, entries, 2 + name_entries, offset);
        if (rc != 0)
                return rc;
@@ -1001,7 +924,6 @@ static int commit_entry(struct exfat* ef, struct exfat_node* dir,
        node = allocate_node();
        if (node == NULL)
                return -ENOMEM;
-       node->entry_cluster = cluster;
        node->entry_offset = offset;
        memcpy(node->name, name, name_length * sizeof(le16_t));
        init_node_meta1(node, meta1);
@@ -1015,7 +937,6 @@ static int create(struct exfat* ef, const char* path, uint16_t attrib)
 {
        struct exfat_node* dir;
        struct exfat_node* existing;
-       cluster_t cluster = EXFAT_CLUSTER_BAD;
        off_t offset = -1;
        le16_t name[EXFAT_NAME_MAX + 1];
        int rc;
@@ -1037,7 +958,7 @@ static int create(struct exfat* ef, const char* path, uint16_t attrib)
                exfat_put_node(ef, dir);
                return rc;
        }
-       rc = commit_entry(ef, dir, name, cluster, offset, attrib);
+       rc = commit_entry(ef, dir, name, offset, attrib);
        if (rc != 0)
        {
                exfat_put_node(ef, dir);
@@ -1085,8 +1006,7 @@ int exfat_mkdir(struct exfat* ef, const char* path)
 }
 
 static int rename_entry(struct exfat* ef, struct exfat_node* dir,
-               struct exfat_node* node, const le16_t* name, cluster_t new_cluster,
-               off_t new_offset)
+               struct exfat_node* node, const le16_t* name, off_t new_offset)
 {
        const size_t name_length = utf16_length(name);
        const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
@@ -1103,13 +1023,11 @@ static int rename_entry(struct exfat* ef, struct exfat_node* dir,
        meta1->continuations = 1 + name_entries;
        meta2->name_length = name_length;
        meta2->name_hash = exfat_calc_name_hash(ef, name, name_length);
-       meta1->checksum = exfat_calc_checksum(meta1, meta2, name);
 
        rc = erase_node(ef, node);
        if (rc != 0)
                return rc;
 
-       node->entry_cluster = new_cluster;
        node->entry_offset = new_offset;
        node->continuations = 1 + name_entries;
 
@@ -1124,6 +1042,7 @@ static int rename_entry(struct exfat* ef, struct exfat_node* dir,
                                EXFAT_ENAME_MAX * sizeof(le16_t));
        }
 
+       meta1->checksum = exfat_calc_checksum(entries, 2 + name_entries);
        rc = write_entries(ef, dir, entries, 2 + name_entries, new_offset);
        if (rc != 0)
                return rc;
@@ -1139,7 +1058,6 @@ int exfat_rename(struct exfat* ef, const char* old_path, const char* new_path)
        struct exfat_node* node;
        struct exfat_node* existing;
        struct exfat_node* dir;
-       cluster_t cluster = EXFAT_CLUSTER_BAD;
        off_t offset = -1;
        le16_t name[EXFAT_NAME_MAX + 1];
        int rc;
@@ -1220,7 +1138,7 @@ int exfat_rename(struct exfat* ef, const char* old_path, const char* new_path)
                exfat_put_node(ef, node);
                return rc;
        }
-       rc = rename_entry(ef, dir, node, name, cluster, offset);
+       rc = rename_entry(ef, dir, node, name, offset);
        if (rc != 0)
        {
                exfat_put_node(ef, dir);