3 exFAT file system implementation library.
5 Free exFAT implementation.
6 Copyright (C) 2010-2016 Andrew Nayenko
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 /* on-disk nodes iterator */
36 struct exfat_node* exfat_get_node(struct exfat_node* node)
38 /* if we switch to multi-threaded mode we will need atomic
39 increment here and atomic decrement in exfat_put_node() */
44 void exfat_put_node(struct exfat* ef, struct exfat_node* node)
46 char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
49 if (node->references < 0)
51 exfat_get_name(node, buffer, sizeof(buffer) - 1);
52 exfat_bug("reference counter of '%s' is below zero", buffer);
54 else if (node->references == 0 && node != ef->root)
56 if (node->flags & EXFAT_ATTRIB_DIRTY)
58 exfat_get_name(node, buffer, sizeof(buffer) - 1);
59 exfat_warn("dirty node '%s' with zero references", buffer);
65 * This function must be called on rmdir and unlink (after the last
66 * exfat_put_node()) to free clusters.
68 int exfat_cleanup_node(struct exfat* ef, struct exfat_node* node)
72 if (node->references != 0)
73 exfat_bug("unable to cleanup a node with %d references",
76 if (node->flags & EXFAT_ATTRIB_UNLINKED)
78 /* free all clusters and node structure itself */
79 rc = exfat_truncate(ef, node, 0, true);
80 /* free the node even in case of error or its memory will be lost */
87 * Cluster + offset from the beginning of the directory to absolute offset.
89 static off_t co2o(struct exfat* ef, cluster_t cluster, off_t offset)
91 return exfat_c2o(ef, cluster) + offset % CLUSTER_SIZE(*ef->sb);
94 static int opendir(struct exfat* ef, const struct exfat_node* dir,
97 char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
99 if (!(dir->flags & EXFAT_ATTRIB_DIR))
101 exfat_get_name(dir, buffer, sizeof(buffer) - 1);
102 exfat_bug("'%s' is not a directory", buffer);
104 it->cluster = dir->start_cluster;
106 it->chunk = malloc(CLUSTER_SIZE(*ef->sb));
107 if (it->chunk == NULL)
109 exfat_error("failed to allocate memory for directory cluster");
112 if (exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb),
113 exfat_c2o(ef, it->cluster)) < 0)
116 exfat_get_name(dir, buffer, sizeof(buffer) - 1);
117 exfat_error("failed to read '%s' directory cluster %#x", buffer,
124 static void closedir(struct iterator* it)
132 static bool fetch_next_entry(struct exfat* ef, const struct exfat_node* parent,
135 /* move iterator to the next entry in the directory */
136 it->offset += sizeof(struct exfat_entry);
137 /* fetch the next cluster if needed */
138 if ((it->offset & (CLUSTER_SIZE(*ef->sb) - 1)) == 0)
140 /* reached the end of directory; the caller should check this
142 if (it->offset >= parent->size)
144 it->cluster = exfat_next_cluster(ef, parent, it->cluster);
145 if (CLUSTER_INVALID(it->cluster))
147 exfat_error("invalid cluster 0x%x while reading directory",
151 if (exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb),
152 exfat_c2o(ef, it->cluster)) < 0)
154 exfat_error("failed to read the next directory cluster %#x",
162 static struct exfat_node* allocate_node(void)
164 struct exfat_node* node = malloc(sizeof(struct exfat_node));
167 exfat_error("failed to allocate node");
170 memset(node, 0, sizeof(struct exfat_node));
174 static void init_node_meta1(struct exfat_node* node,
175 const struct exfat_entry_meta1* meta1)
177 node->flags = le16_to_cpu(meta1->attrib);
178 node->mtime = exfat_exfat2unix(meta1->mdate, meta1->mtime,
180 /* there is no centiseconds field for atime */
181 node->atime = exfat_exfat2unix(meta1->adate, meta1->atime, 0);
184 static void init_node_meta2(struct exfat_node* node,
185 const struct exfat_entry_meta2* meta2)
187 node->size = le64_to_cpu(meta2->size);
188 node->start_cluster = le32_to_cpu(meta2->start_cluster);
189 node->fptr_cluster = node->start_cluster;
190 if (meta2->flags & EXFAT_FLAG_CONTIGUOUS)
191 node->flags |= EXFAT_ATTRIB_CONTIGUOUS;
194 static const struct exfat_entry* get_entry_ptr(const struct exfat* ef,
195 const struct iterator* it)
197 return (const struct exfat_entry*)
198 (it->chunk + it->offset % CLUSTER_SIZE(*ef->sb));
201 static bool check_node(const struct exfat_node* node, uint16_t actual_checksum,
202 uint16_t reference_checksum, uint64_t valid_size)
204 char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
208 Validate checksum first. If it's invalid all other fields probably
209 contain just garbage.
211 if (actual_checksum != reference_checksum)
213 exfat_get_name(node, buffer, sizeof(buffer) - 1);
214 exfat_error("'%s' has invalid checksum (%#hx != %#hx)", buffer,
215 actual_checksum, reference_checksum);
220 exFAT does not support sparse files but allows files with uninitialized
221 clusters. For such files valid_size means initialized data size and
222 cannot be greater than file size. See SetFileValidData() function
225 if (valid_size > node->size)
227 exfat_get_name(node, buffer, sizeof(buffer) - 1);
228 exfat_error("'%s' has valid size (%"PRIu64") greater than size "
229 "(%"PRIu64")", buffer, valid_size, node->size);
236 static void decompress_upcase(uint16_t* output, const le16_t* source,
242 for (oi = 0; oi < EXFAT_UPCASE_CHARS; oi++)
245 for (si = 0, oi = 0; si < size && oi < EXFAT_UPCASE_CHARS; si++)
247 uint16_t ch = le16_to_cpu(source[si]);
249 if (ch == 0xffff && si + 1 < size) /* indicates a run */
250 oi += le16_to_cpu(source[++si]);
257 * Reads one entry in directory at position pointed by iterator and fills
260 static int readdir(struct exfat* ef, const struct exfat_node* parent,
261 struct exfat_node** node, struct iterator* it)
264 const struct exfat_entry* entry;
265 const struct exfat_entry_meta1* meta1;
266 const struct exfat_entry_meta2* meta2;
267 const struct exfat_entry_name* file_name;
268 const struct exfat_entry_upcase* upcase;
269 const struct exfat_entry_bitmap* bitmap;
270 const struct exfat_entry_label* label;
271 uint8_t continuations = 0;
272 le16_t* namep = NULL;
273 uint16_t reference_checksum = 0;
274 uint16_t actual_checksum = 0;
275 uint64_t valid_size = 0;
276 uint64_t upcase_size = 0;
277 le16_t* upcase_comp = NULL;
283 if (it->offset >= parent->size)
285 if (continuations != 0)
287 exfat_error("expected %hhu continuations", continuations);
290 return -ENOENT; /* that's OK, means end of directory */
293 entry = get_entry_ptr(ef, it);
296 case EXFAT_ENTRY_FILE:
297 if (continuations != 0)
299 exfat_error("expected %hhu continuations before new entry",
303 meta1 = (const struct exfat_entry_meta1*) entry;
304 continuations = meta1->continuations;
305 /* each file entry must have at least 2 continuations:
307 if (continuations < 2)
309 exfat_error("too few continuations (%hhu)", continuations);
312 if (continuations > 1 +
313 DIV_ROUND_UP(EXFAT_NAME_MAX, EXFAT_ENAME_MAX))
315 exfat_error("too many continuations (%hhu)", continuations);
318 reference_checksum = le16_to_cpu(meta1->checksum);
319 actual_checksum = exfat_start_checksum(meta1);
320 *node = allocate_node();
326 /* new node has zero reference counter */
327 (*node)->entry_cluster = it->cluster;
328 (*node)->entry_offset = it->offset;
329 init_node_meta1(*node, meta1);
330 namep = (*node)->name;
333 case EXFAT_ENTRY_FILE_INFO:
334 if (continuations < 2)
336 exfat_error("unexpected continuation (%hhu)",
340 meta2 = (const struct exfat_entry_meta2*) entry;
341 if (meta2->flags & ~(EXFAT_FLAG_ALWAYS1 | EXFAT_FLAG_CONTIGUOUS))
343 exfat_error("unknown flags in meta2 (0x%hhx)", meta2->flags);
346 init_node_meta2(*node, meta2);
347 actual_checksum = exfat_add_checksum(entry, actual_checksum);
348 valid_size = le64_to_cpu(meta2->valid_size);
349 /* empty files must be marked as non-contiguous */
350 if ((*node)->size == 0 && (meta2->flags & EXFAT_FLAG_CONTIGUOUS))
352 exfat_error("empty file marked as contiguous (0x%hhx)",
356 /* directories must be aligned on at cluster boundary */
357 if (((*node)->flags & EXFAT_ATTRIB_DIR) &&
358 (*node)->size % CLUSTER_SIZE(*ef->sb) != 0)
360 exfat_error("directory has invalid size %"PRIu64" bytes",
367 case EXFAT_ENTRY_FILE_NAME:
368 if (continuations == 0)
370 exfat_error("unexpected continuation");
373 file_name = (const struct exfat_entry_name*) entry;
374 actual_checksum = exfat_add_checksum(entry, actual_checksum);
376 memcpy(namep, file_name->name,
378 ((*node)->name + EXFAT_NAME_MAX - namep)) *
380 namep += EXFAT_ENAME_MAX;
381 if (--continuations == 0)
383 if (!check_node(*node, actual_checksum, reference_checksum,
386 if (!fetch_next_entry(ef, parent, it))
388 return 0; /* entry completed */
392 case EXFAT_ENTRY_UPCASE:
393 if (ef->upcase != NULL)
395 upcase = (const struct exfat_entry_upcase*) entry;
396 if (CLUSTER_INVALID(le32_to_cpu(upcase->start_cluster)))
398 exfat_error("invalid cluster 0x%x in upcase table",
399 le32_to_cpu(upcase->start_cluster));
402 upcase_size = le64_to_cpu(upcase->size);
403 if (upcase_size == 0 ||
404 upcase_size > EXFAT_UPCASE_CHARS * sizeof(uint16_t) ||
405 upcase_size % sizeof(uint16_t) != 0)
407 exfat_error("bad upcase table size (%"PRIu64" bytes)",
411 upcase_comp = malloc(upcase_size);
412 if (upcase_comp == NULL)
414 exfat_error("failed to allocate upcase table (%"PRIu64" bytes)",
420 /* read compressed upcase table */
421 if (exfat_pread(ef->dev, upcase_comp, upcase_size,
422 exfat_c2o(ef, le32_to_cpu(upcase->start_cluster))) < 0)
425 exfat_error("failed to read upper case table "
426 "(%"PRIu64" bytes starting at cluster %#x)",
428 le32_to_cpu(upcase->start_cluster));
432 /* decompress upcase table */
433 ef->upcase = calloc(EXFAT_UPCASE_CHARS, sizeof(uint16_t));
434 if (ef->upcase == NULL)
437 exfat_error("failed to allocate decompressed upcase table");
441 decompress_upcase(ef->upcase, upcase_comp,
442 upcase_size / sizeof(uint16_t));
446 case EXFAT_ENTRY_BITMAP:
447 bitmap = (const struct exfat_entry_bitmap*) entry;
448 ef->cmap.start_cluster = le32_to_cpu(bitmap->start_cluster);
449 if (CLUSTER_INVALID(ef->cmap.start_cluster))
451 exfat_error("invalid cluster 0x%x in clusters bitmap",
452 ef->cmap.start_cluster);
455 ef->cmap.size = le32_to_cpu(ef->sb->cluster_count) -
456 EXFAT_FIRST_DATA_CLUSTER;
457 if (le64_to_cpu(bitmap->size) < DIV_ROUND_UP(ef->cmap.size, 8))
459 exfat_error("invalid clusters bitmap size: %"PRIu64
460 " (expected at least %u)",
461 le64_to_cpu(bitmap->size),
462 DIV_ROUND_UP(ef->cmap.size, 8));
465 /* FIXME bitmap can be rather big, up to 512 MB */
466 ef->cmap.chunk_size = ef->cmap.size;
467 ef->cmap.chunk = malloc(BMAP_SIZE(ef->cmap.chunk_size));
468 if (ef->cmap.chunk == NULL)
470 exfat_error("failed to allocate clusters bitmap chunk "
471 "(%"PRIu64" bytes)", le64_to_cpu(bitmap->size));
476 if (exfat_pread(ef->dev, ef->cmap.chunk,
477 BMAP_SIZE(ef->cmap.chunk_size),
478 exfat_c2o(ef, ef->cmap.start_cluster)) < 0)
480 exfat_error("failed to read clusters bitmap "
481 "(%"PRIu64" bytes starting at cluster %#x)",
482 le64_to_cpu(bitmap->size), ef->cmap.start_cluster);
487 case EXFAT_ENTRY_LABEL:
488 label = (const struct exfat_entry_label*) entry;
489 if (label->length > EXFAT_ENAME_MAX)
491 exfat_error("too long label (%hhu chars)", label->length);
494 if (utf16_to_utf8(ef->label, label->name,
495 sizeof(ef->label) - 1, EXFAT_ENAME_MAX) != 0)
500 if (!(entry->type & EXFAT_ENTRY_VALID))
501 break; /* deleted entry, ignore it */
502 if (!(entry->type & EXFAT_ENTRY_OPTIONAL))
504 exfat_error("unknown entry type %#hhx", entry->type);
507 /* optional entry, warn and skip */
508 exfat_warn("unknown entry type %#hhx", entry->type);
509 if (continuations == 0)
511 exfat_error("unexpected continuation");
518 if (!fetch_next_entry(ef, parent, it))
521 /* we never reach here */
529 int exfat_cache_directory(struct exfat* ef, struct exfat_node* dir)
533 struct exfat_node* node;
534 struct exfat_node* current = NULL;
536 if (dir->flags & EXFAT_ATTRIB_CACHED)
537 return 0; /* already cached */
539 rc = opendir(ef, dir, &it);
542 while ((rc = readdir(ef, dir, &node, &it)) == 0)
547 current->next = node;
548 node->prev = current;
560 for (current = dir->child; current; current = node)
562 node = current->next;
569 dir->flags |= EXFAT_ATTRIB_CACHED;
573 static void tree_attach(struct exfat_node* dir, struct exfat_node* node)
578 dir->child->prev = node;
579 node->next = dir->child;
584 static void tree_detach(struct exfat_node* node)
587 node->prev->next = node->next;
588 else /* this is the first node in the list */
589 node->parent->child = node->next;
591 node->next->prev = node->prev;
597 static void reset_cache(struct exfat* ef, struct exfat_node* node)
599 char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
603 struct exfat_node* p = node->child;
608 node->flags &= ~EXFAT_ATTRIB_CACHED;
609 if (node->references != 0)
611 exfat_get_name(node, buffer, sizeof(buffer) - 1);
612 exfat_warn("non-zero reference counter (%d) for '%s'",
613 node->references, buffer);
615 if (node != ef->root && (node->flags & EXFAT_ATTRIB_DIRTY))
617 exfat_get_name(node, buffer, sizeof(buffer) - 1);
618 exfat_bug("node '%s' is dirty", buffer);
620 while (node->references)
621 exfat_put_node(ef, node);
624 void exfat_reset_cache(struct exfat* ef)
626 reset_cache(ef, ef->root);
629 static bool next_entry(struct exfat* ef, const struct exfat_node* parent,
630 cluster_t* cluster, off_t* offset)
632 *offset += sizeof(struct exfat_entry);
633 if (*offset % CLUSTER_SIZE(*ef->sb) == 0)
635 *cluster = exfat_next_cluster(ef, parent, *cluster);
636 if (CLUSTER_INVALID(*cluster))
638 exfat_error("invalid cluster %#x while getting next entry",
646 int exfat_flush_node(struct exfat* ef, struct exfat_node* node)
650 off_t meta1_offset, meta2_offset;
651 struct exfat_entry_meta1 meta1;
652 struct exfat_entry_meta2 meta2;
654 if (!(node->flags & EXFAT_ATTRIB_DIRTY))
655 return 0; /* no need to flush */
658 exfat_bug("unable to flush node to read-only FS");
660 if (node->parent == NULL)
661 return 0; /* do not flush unlinked node */
663 cluster = node->entry_cluster;
664 offset = node->entry_offset;
665 meta1_offset = co2o(ef, cluster, offset);
666 if (!next_entry(ef, node->parent, &cluster, &offset))
668 meta2_offset = co2o(ef, cluster, offset);
670 if (exfat_pread(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
672 exfat_error("failed to read meta1 entry on flush");
675 if (meta1.type != EXFAT_ENTRY_FILE)
676 exfat_bug("invalid type of meta1: 0x%hhx", meta1.type);
677 meta1.attrib = cpu_to_le16(node->flags);
678 exfat_unix2exfat(node->mtime, &meta1.mdate, &meta1.mtime, &meta1.mtime_cs);
679 exfat_unix2exfat(node->atime, &meta1.adate, &meta1.atime, NULL);
681 if (exfat_pread(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
683 exfat_error("failed to read meta2 entry on flush");
686 if (meta2.type != EXFAT_ENTRY_FILE_INFO)
687 exfat_bug("invalid type of meta2: 0x%hhx", meta2.type);
688 meta2.size = meta2.valid_size = cpu_to_le64(node->size);
689 meta2.start_cluster = cpu_to_le32(node->start_cluster);
690 meta2.flags = EXFAT_FLAG_ALWAYS1;
691 /* empty files must not be marked as contiguous */
692 if (node->size != 0 && IS_CONTIGUOUS(*node))
693 meta2.flags |= EXFAT_FLAG_CONTIGUOUS;
694 /* name hash remains unchanged, no need to recalculate it */
696 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);
698 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
700 exfat_error("failed to write meta1 entry on flush");
703 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
705 exfat_error("failed to write meta2 entry on flush");
709 node->flags &= ~EXFAT_ATTRIB_DIRTY;
710 return exfat_flush(ef);
713 static bool erase_entry(struct exfat* ef, struct exfat_node* node)
715 cluster_t cluster = node->entry_cluster;
716 off_t offset = node->entry_offset;
717 int name_entries = DIV_ROUND_UP(utf16_length(node->name), EXFAT_ENAME_MAX);
720 entry_type = EXFAT_ENTRY_FILE & ~EXFAT_ENTRY_VALID;
721 if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
723 exfat_error("failed to erase meta1 entry");
727 if (!next_entry(ef, node->parent, &cluster, &offset))
729 entry_type = EXFAT_ENTRY_FILE_INFO & ~EXFAT_ENTRY_VALID;
730 if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
732 exfat_error("failed to erase meta2 entry");
736 while (name_entries--)
738 if (!next_entry(ef, node->parent, &cluster, &offset))
740 entry_type = EXFAT_ENTRY_FILE_NAME & ~EXFAT_ENTRY_VALID;
741 if (exfat_pwrite(ef->dev, &entry_type, 1,
742 co2o(ef, cluster, offset)) < 0)
744 exfat_error("failed to erase name entry");
751 static int shrink_directory(struct exfat* ef, struct exfat_node* dir,
752 off_t deleted_offset)
754 const struct exfat_node* node;
755 const struct exfat_node* last_node;
756 uint64_t entries = 0;
759 if (!(dir->flags & EXFAT_ATTRIB_DIR))
760 exfat_bug("attempted to shrink a file");
761 if (!(dir->flags & EXFAT_ATTRIB_CACHED))
762 exfat_bug("attempted to shrink uncached directory");
764 for (last_node = node = dir->child; node; node = node->next)
766 if (deleted_offset < node->entry_offset)
768 /* there are other entries after the removed one, no way to shrink
772 if (last_node->entry_offset < node->entry_offset)
778 /* offset of the last entry */
779 entries += last_node->entry_offset / sizeof(struct exfat_entry);
780 /* two subentries with meta info */
782 /* subentries with file name */
783 entries += DIV_ROUND_UP(utf16_length(last_node->name),
787 new_size = DIV_ROUND_UP(entries * sizeof(struct exfat_entry),
788 CLUSTER_SIZE(*ef->sb)) * CLUSTER_SIZE(*ef->sb);
789 if (new_size == 0) /* directory always has at least 1 cluster */
790 new_size = CLUSTER_SIZE(*ef->sb);
791 if (new_size == dir->size)
793 return exfat_truncate(ef, dir, new_size, true);
796 static int delete(struct exfat* ef, struct exfat_node* node)
798 struct exfat_node* parent = node->parent;
799 off_t deleted_offset = node->entry_offset;
802 exfat_get_node(parent);
803 if (!erase_entry(ef, node))
805 exfat_put_node(ef, parent);
808 exfat_update_mtime(parent);
810 rc = shrink_directory(ef, parent, deleted_offset);
811 node->flags |= EXFAT_ATTRIB_UNLINKED;
814 exfat_flush_node(ef, parent);
815 exfat_put_node(ef, parent);
818 rc = exfat_flush_node(ef, parent);
819 exfat_put_node(ef, parent);
823 int exfat_unlink(struct exfat* ef, struct exfat_node* node)
825 if (node->flags & EXFAT_ATTRIB_DIR)
827 return delete(ef, node);
830 int exfat_rmdir(struct exfat* ef, struct exfat_node* node)
834 if (!(node->flags & EXFAT_ATTRIB_DIR))
836 /* check that directory is empty */
837 rc = exfat_cache_directory(ef, node);
842 return delete(ef, node);
845 static int grow_directory(struct exfat* ef, struct exfat_node* dir,
846 uint64_t asize, uint32_t difference)
848 return exfat_truncate(ef, dir,
849 DIV_ROUND_UP(asize + difference, CLUSTER_SIZE(*ef->sb))
850 * CLUSTER_SIZE(*ef->sb), true);
853 static int find_slot(struct exfat* ef, struct exfat_node* dir,
854 cluster_t* cluster, off_t* offset, int subentries)
858 const struct exfat_entry* entry;
861 rc = opendir(ef, dir, &it);
868 *cluster = it.cluster;
871 entry = get_entry_ptr(ef, &it);
872 if (entry->type & EXFAT_ENTRY_VALID)
876 if (contiguous == subentries)
877 break; /* suitable slot is found */
878 if (it.offset + sizeof(struct exfat_entry) >= dir->size)
880 rc = grow_directory(ef, dir, dir->size,
881 (subentries - contiguous) * sizeof(struct exfat_entry));
888 if (!fetch_next_entry(ef, dir, &it))
898 static int write_entry(struct exfat* ef, struct exfat_node* dir,
899 const le16_t* name, cluster_t cluster, off_t offset, uint16_t attrib)
901 struct exfat_node* node;
902 struct exfat_entry_meta1 meta1;
903 struct exfat_entry_meta2 meta2;
904 const size_t name_length = utf16_length(name);
905 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
908 node = allocate_node();
911 node->entry_cluster = cluster;
912 node->entry_offset = offset;
913 memcpy(node->name, name, name_length * sizeof(le16_t));
915 memset(&meta1, 0, sizeof(meta1));
916 meta1.type = EXFAT_ENTRY_FILE;
917 meta1.continuations = 1 + name_entries;
918 meta1.attrib = cpu_to_le16(attrib);
919 exfat_unix2exfat(time(NULL), &meta1.crdate, &meta1.crtime,
921 meta1.adate = meta1.mdate = meta1.crdate;
922 meta1.atime = meta1.mtime = meta1.crtime;
923 meta1.mtime_cs = meta1.crtime_cs; /* there is no atime_cs */
925 memset(&meta2, 0, sizeof(meta2));
926 meta2.type = EXFAT_ENTRY_FILE_INFO;
927 meta2.flags = EXFAT_FLAG_ALWAYS1;
928 meta2.name_length = name_length;
929 meta2.name_hash = exfat_calc_name_hash(ef, node->name);
930 meta2.start_cluster = cpu_to_le32(EXFAT_CLUSTER_FREE);
932 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);
934 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
935 co2o(ef, cluster, offset)) < 0)
937 exfat_error("failed to write meta1 entry");
940 if (!next_entry(ef, dir, &cluster, &offset))
942 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
943 co2o(ef, cluster, offset)) < 0)
945 exfat_error("failed to write meta2 entry");
948 for (i = 0; i < name_entries; i++)
950 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
951 memcpy(name_entry.name, node->name + i * EXFAT_ENAME_MAX,
952 MIN(EXFAT_ENAME_MAX, EXFAT_NAME_MAX - i * EXFAT_ENAME_MAX) *
954 if (!next_entry(ef, dir, &cluster, &offset))
956 if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
957 co2o(ef, cluster, offset)) < 0)
959 exfat_error("failed to write name entry");
964 init_node_meta1(node, &meta1);
965 init_node_meta2(node, &meta2);
967 tree_attach(dir, node);
968 exfat_update_mtime(dir);
972 static int create(struct exfat* ef, const char* path, uint16_t attrib)
974 struct exfat_node* dir;
975 struct exfat_node* existing;
976 cluster_t cluster = EXFAT_CLUSTER_BAD;
978 le16_t name[EXFAT_NAME_MAX + 1];
981 rc = exfat_split(ef, &dir, &existing, name, path);
984 if (existing != NULL)
986 exfat_put_node(ef, existing);
987 exfat_put_node(ef, dir);
991 rc = find_slot(ef, dir, &cluster, &offset,
992 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
995 exfat_put_node(ef, dir);
998 rc = write_entry(ef, dir, name, cluster, offset, attrib);
1001 exfat_put_node(ef, dir);
1004 rc = exfat_flush_node(ef, dir);
1005 exfat_put_node(ef, dir);
1009 int exfat_mknod(struct exfat* ef, const char* path)
1011 return create(ef, path, EXFAT_ATTRIB_ARCH);
1014 int exfat_mkdir(struct exfat* ef, const char* path)
1017 struct exfat_node* node;
1019 rc = create(ef, path, EXFAT_ATTRIB_DIR);
1022 rc = exfat_lookup(ef, &node, path);
1025 /* directories always have at least one cluster */
1026 rc = exfat_truncate(ef, node, CLUSTER_SIZE(*ef->sb), true);
1030 exfat_put_node(ef, node);
1033 rc = exfat_flush_node(ef, node);
1037 exfat_put_node(ef, node);
1040 exfat_put_node(ef, node);
1044 static int rename_entry(struct exfat* ef, struct exfat_node* dir,
1045 struct exfat_node* node, const le16_t* name, cluster_t new_cluster,
1048 struct exfat_entry_meta1 meta1;
1049 struct exfat_entry_meta2 meta2;
1050 cluster_t old_cluster = node->entry_cluster;
1051 off_t old_offset = node->entry_offset;
1052 const size_t name_length = utf16_length(name);
1053 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
1056 if (exfat_pread(ef->dev, &meta1, sizeof(meta1),
1057 co2o(ef, old_cluster, old_offset)) < 0)
1059 exfat_error("failed to read meta1 entry on rename");
1062 if (!next_entry(ef, node->parent, &old_cluster, &old_offset))
1064 if (exfat_pread(ef->dev, &meta2, sizeof(meta2),
1065 co2o(ef, old_cluster, old_offset)) < 0)
1067 exfat_error("failed to read meta2 entry on rename");
1070 meta1.continuations = 1 + name_entries;
1071 meta2.name_hash = exfat_calc_name_hash(ef, name);
1072 meta2.name_length = name_length;
1073 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, name);
1075 if (!erase_entry(ef, node))
1078 node->entry_cluster = new_cluster;
1079 node->entry_offset = new_offset;
1081 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
1082 co2o(ef, new_cluster, new_offset)) < 0)
1084 exfat_error("failed to write meta1 entry on rename");
1087 if (!next_entry(ef, dir, &new_cluster, &new_offset))
1089 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
1090 co2o(ef, new_cluster, new_offset)) < 0)
1092 exfat_error("failed to write meta2 entry on rename");
1096 for (i = 0; i < name_entries; i++)
1098 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
1099 memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX,
1100 EXFAT_ENAME_MAX * sizeof(le16_t));
1101 if (!next_entry(ef, dir, &new_cluster, &new_offset))
1103 if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
1104 co2o(ef, new_cluster, new_offset)) < 0)
1106 exfat_error("failed to write name entry on rename");
1111 memcpy(node->name, name, (EXFAT_NAME_MAX + 1) * sizeof(le16_t));
1113 tree_attach(dir, node);
1117 int exfat_rename(struct exfat* ef, const char* old_path, const char* new_path)
1119 struct exfat_node* node;
1120 struct exfat_node* existing;
1121 struct exfat_node* dir;
1122 cluster_t cluster = EXFAT_CLUSTER_BAD;
1124 le16_t name[EXFAT_NAME_MAX + 1];
1127 rc = exfat_lookup(ef, &node, old_path);
1131 rc = exfat_split(ef, &dir, &existing, name, new_path);
1134 exfat_put_node(ef, node);
1138 /* check that target is not a subdirectory of the source */
1139 if (node->flags & EXFAT_ATTRIB_DIR)
1141 struct exfat_node* p;
1143 for (p = dir; p; p = p->parent)
1146 if (existing != NULL)
1147 exfat_put_node(ef, existing);
1148 exfat_put_node(ef, dir);
1149 exfat_put_node(ef, node);
1154 if (existing != NULL)
1156 /* remove target if it's not the same node as source */
1157 if (existing != node)
1159 if (existing->flags & EXFAT_ATTRIB_DIR)
1161 if (node->flags & EXFAT_ATTRIB_DIR)
1162 rc = exfat_rmdir(ef, existing);
1168 if (!(node->flags & EXFAT_ATTRIB_DIR))
1169 rc = exfat_unlink(ef, existing);
1173 exfat_put_node(ef, existing);
1176 /* free clusters even if something went wrong; overwise they
1177 will be just lost */
1178 exfat_cleanup_node(ef, existing);
1179 exfat_put_node(ef, dir);
1180 exfat_put_node(ef, node);
1183 rc = exfat_cleanup_node(ef, existing);
1186 exfat_put_node(ef, dir);
1187 exfat_put_node(ef, node);
1192 exfat_put_node(ef, existing);
1195 rc = find_slot(ef, dir, &cluster, &offset,
1196 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
1199 exfat_put_node(ef, dir);
1200 exfat_put_node(ef, node);
1203 rc = rename_entry(ef, dir, node, name, cluster, offset);
1204 exfat_put_node(ef, dir);
1205 exfat_put_node(ef, node);
1209 void exfat_utimes(struct exfat_node* node, const struct timespec tv[2])
1211 node->atime = tv[0].tv_sec;
1212 node->mtime = tv[1].tv_sec;
1213 node->flags |= EXFAT_ATTRIB_DIRTY;
1216 void exfat_update_atime(struct exfat_node* node)
1218 node->atime = time(NULL);
1219 node->flags |= EXFAT_ATTRIB_DIRTY;
1222 void exfat_update_mtime(struct exfat_node* node)
1224 node->mtime = time(NULL);
1225 node->flags |= EXFAT_ATTRIB_DIRTY;
1228 const char* exfat_get_label(struct exfat* ef)
1233 static int find_label(struct exfat* ef, cluster_t* cluster, off_t* offset)
1238 rc = opendir(ef, ef->root, &it);
1244 if (it.offset >= ef->root->size)
1250 if (get_entry_ptr(ef, &it)->type == EXFAT_ENTRY_LABEL)
1252 *cluster = it.cluster;
1253 *offset = it.offset;
1258 if (!fetch_next_entry(ef, ef->root, &it))
1266 int exfat_set_label(struct exfat* ef, const char* label)
1268 le16_t label_utf16[EXFAT_ENAME_MAX + 1];
1272 struct exfat_entry_label entry;
1274 memset(label_utf16, 0, sizeof(label_utf16));
1275 rc = utf8_to_utf16(label_utf16, label, EXFAT_ENAME_MAX, strlen(label));
1279 rc = find_label(ef, &cluster, &offset);
1281 rc = find_slot(ef, ef->root, &cluster, &offset, 1);
1285 entry.type = EXFAT_ENTRY_LABEL;
1286 entry.length = utf16_length(label_utf16);
1287 memcpy(entry.name, label_utf16, sizeof(entry.name));
1288 if (entry.length == 0)
1289 entry.type ^= EXFAT_ENTRY_VALID;
1291 if (exfat_pwrite(ef->dev, &entry, sizeof(struct exfat_entry_label),
1292 co2o(ef, cluster, offset)) < 0)
1294 exfat_error("failed to write label entry");
1297 strcpy(ef->label, label);