3 exFAT file system implementation library.
5 Free exFAT implementation.
6 Copyright (C) 2010-2015 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 */
37 struct exfat_node* exfat_get_node(struct exfat_node* node)
39 /* if we switch to multi-threaded mode we will need atomic
40 increment here and atomic decrement in exfat_put_node() */
45 void exfat_put_node(struct exfat* ef, struct exfat_node* node)
47 char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
50 if (node->references < 0)
52 exfat_get_name(node, buffer, sizeof(buffer) - 1);
53 exfat_bug("reference counter of '%s' is below zero", buffer);
55 else if (node->references == 0 && node != ef->root)
57 if (node->flags & EXFAT_ATTRIB_DIRTY)
59 exfat_get_name(node, buffer, sizeof(buffer) - 1);
60 exfat_warn("dirty node '%s' with zero references", buffer);
66 * This function must be called on rmdir and unlink (after the last
67 * exfat_put_node()) to free clusters.
69 int exfat_cleanup_node(struct exfat* ef, struct exfat_node* node)
73 if (node->references != 0)
74 exfat_bug("unable to cleanup a node with %d references",
77 if (node->flags & EXFAT_ATTRIB_UNLINKED)
79 /* free all clusters and node structure itself */
80 rc = exfat_truncate(ef, node, 0, true);
81 /* free the node even in case of error or its memory will be lost */
88 * Cluster + offset from the beginning of the directory to absolute offset.
90 static off_t co2o(struct exfat* ef, cluster_t cluster, off_t offset)
92 return exfat_c2o(ef, cluster) + offset % CLUSTER_SIZE(*ef->sb);
95 static int opendir(struct exfat* ef, const struct exfat_node* dir,
98 if (!(dir->flags & EXFAT_ATTRIB_DIR))
99 exfat_bug("not a directory");
100 it->cluster = dir->start_cluster;
102 it->contiguous = IS_CONTIGUOUS(*dir);
103 it->chunk = malloc(CLUSTER_SIZE(*ef->sb));
104 if (it->chunk == NULL)
106 exfat_error("out of memory");
109 if (exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb),
110 exfat_c2o(ef, it->cluster)) < 0)
112 exfat_error("failed to read directory cluster %#x", it->cluster);
118 static void closedir(struct iterator* it)
127 static bool fetch_next_entry(struct exfat* ef, const struct exfat_node* parent,
130 /* move iterator to the next entry in the directory */
131 it->offset += sizeof(struct exfat_entry);
132 /* fetch the next cluster if needed */
133 if ((it->offset & (CLUSTER_SIZE(*ef->sb) - 1)) == 0)
135 /* reached the end of directory; the caller should check this
137 if (it->offset >= parent->size)
139 it->cluster = exfat_next_cluster(ef, parent, it->cluster);
140 if (CLUSTER_INVALID(it->cluster))
142 exfat_error("invalid cluster 0x%x while reading directory",
146 if (exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb),
147 exfat_c2o(ef, it->cluster)) < 0)
149 exfat_error("failed to read the next directory cluster %#x",
157 static struct exfat_node* allocate_node(void)
159 struct exfat_node* node = malloc(sizeof(struct exfat_node));
162 exfat_error("failed to allocate node");
165 memset(node, 0, sizeof(struct exfat_node));
169 static void init_node_meta1(struct exfat_node* node,
170 const struct exfat_entry_meta1* meta1)
172 node->flags = le16_to_cpu(meta1->attrib);
173 node->mtime = exfat_exfat2unix(meta1->mdate, meta1->mtime,
175 /* there is no centiseconds field for atime */
176 node->atime = exfat_exfat2unix(meta1->adate, meta1->atime, 0);
179 static void init_node_meta2(struct exfat_node* node,
180 const struct exfat_entry_meta2* meta2)
182 node->size = le64_to_cpu(meta2->size);
183 node->start_cluster = le32_to_cpu(meta2->start_cluster);
184 node->fptr_cluster = node->start_cluster;
185 if (meta2->flags & EXFAT_FLAG_CONTIGUOUS)
186 node->flags |= EXFAT_ATTRIB_CONTIGUOUS;
189 static const struct exfat_entry* get_entry_ptr(const struct exfat* ef,
190 const struct iterator* it)
192 return (const struct exfat_entry*)
193 (it->chunk + it->offset % CLUSTER_SIZE(*ef->sb));
196 static bool check_node(const struct exfat_node* node, uint16_t actual_checksum,
197 uint16_t reference_checksum, uint64_t valid_size)
199 char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
202 Validate checksum first. If it's invalid all other fields probably
203 contain just garbage.
205 if (actual_checksum != reference_checksum)
207 exfat_get_name(node, buffer, sizeof(buffer) - 1);
208 exfat_error("'%s' has invalid checksum (%#hx != %#hx)", buffer,
209 actual_checksum, reference_checksum);
214 exFAT does not support sparse files but allows files with uninitialized
215 clusters. For such files valid_size means initialized data size and
216 cannot be greater than file size. See SetFileValidData() function
219 if (valid_size > node->size)
221 exfat_get_name(node, buffer, sizeof(buffer) - 1);
222 exfat_error("'%s' has valid size (%"PRIu64") greater than size "
223 "(%"PRIu64")", buffer, valid_size, node->size);
231 * Reads one entry in directory at position pointed by iterator and fills
234 static int readdir(struct exfat* ef, const struct exfat_node* parent,
235 struct exfat_node** node, struct iterator* it)
238 const struct exfat_entry* entry;
239 const struct exfat_entry_meta1* meta1;
240 const struct exfat_entry_meta2* meta2;
241 const struct exfat_entry_name* file_name;
242 const struct exfat_entry_upcase* upcase;
243 const struct exfat_entry_bitmap* bitmap;
244 const struct exfat_entry_label* label;
245 uint8_t continuations = 0;
246 le16_t* namep = NULL;
247 uint16_t reference_checksum = 0;
248 uint16_t actual_checksum = 0;
249 uint64_t valid_size = 0;
255 if (it->offset >= parent->size)
257 if (continuations != 0)
259 exfat_error("expected %hhu continuations", continuations);
262 return -ENOENT; /* that's OK, means end of directory */
265 entry = get_entry_ptr(ef, it);
268 case EXFAT_ENTRY_FILE:
269 if (continuations != 0)
271 exfat_error("expected %hhu continuations before new entry",
275 meta1 = (const struct exfat_entry_meta1*) entry;
276 continuations = meta1->continuations;
277 /* each file entry must have at least 2 continuations:
279 if (continuations < 2)
281 exfat_error("too few continuations (%hhu)", continuations);
284 if (continuations > 1 +
285 DIV_ROUND_UP(EXFAT_NAME_MAX, EXFAT_ENAME_MAX))
287 exfat_error("too many continuations (%hhu)", continuations);
290 reference_checksum = le16_to_cpu(meta1->checksum);
291 actual_checksum = exfat_start_checksum(meta1);
292 *node = allocate_node();
298 /* new node has zero reference counter */
299 (*node)->entry_cluster = it->cluster;
300 (*node)->entry_offset = it->offset;
301 init_node_meta1(*node, meta1);
302 namep = (*node)->name;
305 case EXFAT_ENTRY_FILE_INFO:
306 if (continuations < 2)
308 exfat_error("unexpected continuation (%hhu)",
312 meta2 = (const struct exfat_entry_meta2*) entry;
313 if (meta2->flags & ~(EXFAT_FLAG_ALWAYS1 | EXFAT_FLAG_CONTIGUOUS))
315 exfat_error("unknown flags in meta2 (0x%hhx)", meta2->flags);
318 init_node_meta2(*node, meta2);
319 actual_checksum = exfat_add_checksum(entry, actual_checksum);
320 valid_size = le64_to_cpu(meta2->valid_size);
321 /* empty files must be marked as non-contiguous */
322 if ((*node)->size == 0 && (meta2->flags & EXFAT_FLAG_CONTIGUOUS))
324 exfat_error("empty file marked as contiguous (0x%hhx)",
328 /* directories must be aligned on at cluster boundary */
329 if (((*node)->flags & EXFAT_ATTRIB_DIR) &&
330 (*node)->size % CLUSTER_SIZE(*ef->sb) != 0)
332 exfat_error("directory has invalid size %"PRIu64" bytes",
339 case EXFAT_ENTRY_FILE_NAME:
340 if (continuations == 0)
342 exfat_error("unexpected continuation");
345 file_name = (const struct exfat_entry_name*) entry;
346 actual_checksum = exfat_add_checksum(entry, actual_checksum);
348 memcpy(namep, file_name->name,
350 ((*node)->name + EXFAT_NAME_MAX - namep)) *
352 namep += EXFAT_ENAME_MAX;
353 if (--continuations == 0)
355 if (!check_node(*node, actual_checksum, reference_checksum,
358 if (!fetch_next_entry(ef, parent, it))
360 return 0; /* entry completed */
364 case EXFAT_ENTRY_UPCASE:
365 if (ef->upcase != NULL)
367 upcase = (const struct exfat_entry_upcase*) entry;
368 if (CLUSTER_INVALID(le32_to_cpu(upcase->start_cluster)))
370 exfat_error("invalid cluster 0x%x in upcase table",
371 le32_to_cpu(upcase->start_cluster));
374 if (le64_to_cpu(upcase->size) == 0 ||
375 le64_to_cpu(upcase->size) > 0xffff * sizeof(uint16_t) ||
376 le64_to_cpu(upcase->size) % sizeof(uint16_t) != 0)
378 exfat_error("bad upcase table size (%"PRIu64" bytes)",
379 le64_to_cpu(upcase->size));
382 ef->upcase = malloc(le64_to_cpu(upcase->size));
383 if (ef->upcase == NULL)
385 exfat_error("failed to allocate upcase table (%"PRIu64" bytes)",
386 le64_to_cpu(upcase->size));
390 ef->upcase_chars = le64_to_cpu(upcase->size) / sizeof(le16_t);
392 if (exfat_pread(ef->dev, ef->upcase, le64_to_cpu(upcase->size),
393 exfat_c2o(ef, le32_to_cpu(upcase->start_cluster))) < 0)
395 exfat_error("failed to read upper case table "
396 "(%"PRIu64" bytes starting at cluster %#x)",
397 le64_to_cpu(upcase->size),
398 le32_to_cpu(upcase->start_cluster));
403 case EXFAT_ENTRY_BITMAP:
404 bitmap = (const struct exfat_entry_bitmap*) entry;
405 ef->cmap.start_cluster = le32_to_cpu(bitmap->start_cluster);
406 if (CLUSTER_INVALID(ef->cmap.start_cluster))
408 exfat_error("invalid cluster 0x%x in clusters bitmap",
409 ef->cmap.start_cluster);
412 ef->cmap.size = le32_to_cpu(ef->sb->cluster_count) -
413 EXFAT_FIRST_DATA_CLUSTER;
414 if (le64_to_cpu(bitmap->size) < DIV_ROUND_UP(ef->cmap.size, 8))
416 exfat_error("invalid clusters bitmap size: %"PRIu64
417 " (expected at least %u)",
418 le64_to_cpu(bitmap->size),
419 DIV_ROUND_UP(ef->cmap.size, 8));
422 /* FIXME bitmap can be rather big, up to 512 MB */
423 ef->cmap.chunk_size = ef->cmap.size;
424 ef->cmap.chunk = malloc(BMAP_SIZE(ef->cmap.chunk_size));
425 if (ef->cmap.chunk == NULL)
427 exfat_error("failed to allocate clusters bitmap chunk "
428 "(%"PRIu64" bytes)", le64_to_cpu(bitmap->size));
433 if (exfat_pread(ef->dev, ef->cmap.chunk,
434 BMAP_SIZE(ef->cmap.chunk_size),
435 exfat_c2o(ef, ef->cmap.start_cluster)) < 0)
437 exfat_error("failed to read clusters bitmap "
438 "(%"PRIu64" bytes starting at cluster %#x)",
439 le64_to_cpu(bitmap->size), ef->cmap.start_cluster);
444 case EXFAT_ENTRY_LABEL:
445 label = (const struct exfat_entry_label*) entry;
446 if (label->length > EXFAT_ENAME_MAX)
448 exfat_error("too long label (%hhu chars)", label->length);
451 if (utf16_to_utf8(ef->label, label->name,
452 sizeof(ef->label) - 1, EXFAT_ENAME_MAX) != 0)
457 if (!(entry->type & EXFAT_ENTRY_VALID))
458 break; /* deleted entry, ignore it */
459 if (!(entry->type & EXFAT_ENTRY_OPTIONAL))
461 exfat_error("unknown entry type %#hhx", entry->type);
464 /* optional entry, warn and skip */
465 exfat_warn("unknown entry type %#hhx", entry->type);
466 if (continuations == 0)
468 exfat_error("unexpected continuation");
475 if (!fetch_next_entry(ef, parent, it))
478 /* we never reach here */
486 int exfat_cache_directory(struct exfat* ef, struct exfat_node* dir)
490 struct exfat_node* node;
491 struct exfat_node* current = NULL;
493 if (dir->flags & EXFAT_ATTRIB_CACHED)
494 return 0; /* already cached */
496 rc = opendir(ef, dir, &it);
499 while ((rc = readdir(ef, dir, &node, &it)) == 0)
504 current->next = node;
505 node->prev = current;
517 for (current = dir->child; current; current = node)
519 node = current->next;
526 dir->flags |= EXFAT_ATTRIB_CACHED;
530 static void tree_attach(struct exfat_node* dir, struct exfat_node* node)
535 dir->child->prev = node;
536 node->next = dir->child;
541 static void tree_detach(struct exfat_node* node)
544 node->prev->next = node->next;
545 else /* this is the first node in the list */
546 node->parent->child = node->next;
548 node->next->prev = node->prev;
554 static void reset_cache(struct exfat* ef, struct exfat_node* node)
556 char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
560 struct exfat_node* p = node->child;
565 node->flags &= ~EXFAT_ATTRIB_CACHED;
566 if (node->references != 0)
568 exfat_get_name(node, buffer, sizeof(buffer) - 1);
569 exfat_warn("non-zero reference counter (%d) for '%s'",
570 node->references, buffer);
572 if (node != ef->root && (node->flags & EXFAT_ATTRIB_DIRTY))
574 exfat_get_name(node, buffer, sizeof(buffer) - 1);
575 exfat_bug("node '%s' is dirty", buffer);
577 while (node->references)
578 exfat_put_node(ef, node);
581 void exfat_reset_cache(struct exfat* ef)
583 reset_cache(ef, ef->root);
586 static bool next_entry(struct exfat* ef, const struct exfat_node* parent,
587 cluster_t* cluster, off_t* offset)
589 *offset += sizeof(struct exfat_entry);
590 if (*offset % CLUSTER_SIZE(*ef->sb) == 0)
592 *cluster = exfat_next_cluster(ef, parent, *cluster);
593 if (CLUSTER_INVALID(*cluster))
595 exfat_error("invalid cluster %#x while getting next entry",
603 int exfat_flush_node(struct exfat* ef, struct exfat_node* node)
607 off_t meta1_offset, meta2_offset;
608 struct exfat_entry_meta1 meta1;
609 struct exfat_entry_meta2 meta2;
611 if (!(node->flags & EXFAT_ATTRIB_DIRTY))
612 return 0; /* no need to flush */
615 exfat_bug("unable to flush node to read-only FS");
617 if (node->parent == NULL)
618 return 0; /* do not flush unlinked node */
620 cluster = node->entry_cluster;
621 offset = node->entry_offset;
622 meta1_offset = co2o(ef, cluster, offset);
623 if (!next_entry(ef, node->parent, &cluster, &offset))
625 meta2_offset = co2o(ef, cluster, offset);
627 if (exfat_pread(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
629 exfat_error("failed to read meta1 entry on flush");
632 if (meta1.type != EXFAT_ENTRY_FILE)
633 exfat_bug("invalid type of meta1: 0x%hhx", meta1.type);
634 meta1.attrib = cpu_to_le16(node->flags);
635 exfat_unix2exfat(node->mtime, &meta1.mdate, &meta1.mtime, &meta1.mtime_cs);
636 exfat_unix2exfat(node->atime, &meta1.adate, &meta1.atime, NULL);
638 if (exfat_pread(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
640 exfat_error("failed to read meta2 entry on flush");
643 if (meta2.type != EXFAT_ENTRY_FILE_INFO)
644 exfat_bug("invalid type of meta2: 0x%hhx", meta2.type);
645 meta2.size = meta2.valid_size = cpu_to_le64(node->size);
646 meta2.start_cluster = cpu_to_le32(node->start_cluster);
647 meta2.flags = EXFAT_FLAG_ALWAYS1;
648 /* empty files must not be marked as contiguous */
649 if (node->size != 0 && IS_CONTIGUOUS(*node))
650 meta2.flags |= EXFAT_FLAG_CONTIGUOUS;
651 /* name hash remains unchanged, no need to recalculate it */
653 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);
655 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
657 exfat_error("failed to write meta1 entry on flush");
660 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
662 exfat_error("failed to write meta2 entry on flush");
666 node->flags &= ~EXFAT_ATTRIB_DIRTY;
670 static bool erase_entry(struct exfat* ef, struct exfat_node* node)
672 cluster_t cluster = node->entry_cluster;
673 off_t offset = node->entry_offset;
674 int name_entries = DIV_ROUND_UP(utf16_length(node->name), EXFAT_ENAME_MAX);
677 entry_type = EXFAT_ENTRY_FILE & ~EXFAT_ENTRY_VALID;
678 if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
680 exfat_error("failed to erase meta1 entry");
684 if (!next_entry(ef, node->parent, &cluster, &offset))
686 entry_type = EXFAT_ENTRY_FILE_INFO & ~EXFAT_ENTRY_VALID;
687 if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
689 exfat_error("failed to erase meta2 entry");
693 while (name_entries--)
695 if (!next_entry(ef, node->parent, &cluster, &offset))
697 entry_type = EXFAT_ENTRY_FILE_NAME & ~EXFAT_ENTRY_VALID;
698 if (exfat_pwrite(ef->dev, &entry_type, 1,
699 co2o(ef, cluster, offset)) < 0)
701 exfat_error("failed to erase name entry");
708 static int shrink_directory(struct exfat* ef, struct exfat_node* dir,
709 off_t deleted_offset)
711 const struct exfat_node* node;
712 const struct exfat_node* last_node;
713 uint64_t entries = 0;
716 if (!(dir->flags & EXFAT_ATTRIB_DIR))
717 exfat_bug("attempted to shrink a file");
718 if (!(dir->flags & EXFAT_ATTRIB_CACHED))
719 exfat_bug("attempted to shrink uncached directory");
721 for (last_node = node = dir->child; node; node = node->next)
723 if (deleted_offset < node->entry_offset)
725 /* there are other entries after the removed one, no way to shrink
729 if (last_node->entry_offset < node->entry_offset)
735 /* offset of the last entry */
736 entries += last_node->entry_offset / sizeof(struct exfat_entry);
737 /* two subentries with meta info */
739 /* subentries with file name */
740 entries += DIV_ROUND_UP(utf16_length(last_node->name),
744 new_size = DIV_ROUND_UP(entries * sizeof(struct exfat_entry),
745 CLUSTER_SIZE(*ef->sb)) * CLUSTER_SIZE(*ef->sb);
746 if (new_size == 0) /* directory always has at least 1 cluster */
747 new_size = CLUSTER_SIZE(*ef->sb);
748 if (new_size == dir->size)
750 return exfat_truncate(ef, dir, new_size, true);
753 static int delete(struct exfat* ef, struct exfat_node* node)
755 struct exfat_node* parent = node->parent;
756 off_t deleted_offset = node->entry_offset;
759 exfat_get_node(parent);
760 if (!erase_entry(ef, node))
762 exfat_put_node(ef, parent);
765 exfat_update_mtime(parent);
767 rc = shrink_directory(ef, parent, deleted_offset);
768 node->flags |= EXFAT_ATTRIB_UNLINKED;
771 exfat_flush_node(ef, parent);
772 exfat_put_node(ef, parent);
775 rc = exfat_flush_node(ef, parent);
776 exfat_put_node(ef, parent);
780 int exfat_unlink(struct exfat* ef, struct exfat_node* node)
782 if (node->flags & EXFAT_ATTRIB_DIR)
784 return delete(ef, node);
787 int exfat_rmdir(struct exfat* ef, struct exfat_node* node)
791 if (!(node->flags & EXFAT_ATTRIB_DIR))
793 /* check that directory is empty */
794 rc = exfat_cache_directory(ef, node);
799 return delete(ef, node);
802 static int grow_directory(struct exfat* ef, struct exfat_node* dir,
803 uint64_t asize, uint32_t difference)
805 return exfat_truncate(ef, dir,
806 DIV_ROUND_UP(asize + difference, CLUSTER_SIZE(*ef->sb))
807 * CLUSTER_SIZE(*ef->sb), true);
810 static int find_slot(struct exfat* ef, struct exfat_node* dir,
811 cluster_t* cluster, off_t* offset, int subentries)
815 const struct exfat_entry* entry;
818 rc = opendir(ef, dir, &it);
825 *cluster = it.cluster;
828 entry = get_entry_ptr(ef, &it);
829 if (entry->type & EXFAT_ENTRY_VALID)
833 if (contiguous == subentries)
834 break; /* suitable slot is found */
835 if (it.offset + sizeof(struct exfat_entry) >= dir->size)
837 rc = grow_directory(ef, dir, dir->size,
838 (subentries - contiguous) * sizeof(struct exfat_entry));
845 if (!fetch_next_entry(ef, dir, &it))
855 static int write_entry(struct exfat* ef, struct exfat_node* dir,
856 const le16_t* name, cluster_t cluster, off_t offset, uint16_t attrib)
858 struct exfat_node* node;
859 struct exfat_entry_meta1 meta1;
860 struct exfat_entry_meta2 meta2;
861 const size_t name_length = utf16_length(name);
862 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
865 node = allocate_node();
868 node->entry_cluster = cluster;
869 node->entry_offset = offset;
870 memcpy(node->name, name, name_length * sizeof(le16_t));
872 memset(&meta1, 0, sizeof(meta1));
873 meta1.type = EXFAT_ENTRY_FILE;
874 meta1.continuations = 1 + name_entries;
875 meta1.attrib = cpu_to_le16(attrib);
876 exfat_unix2exfat(time(NULL), &meta1.crdate, &meta1.crtime,
878 meta1.adate = meta1.mdate = meta1.crdate;
879 meta1.atime = meta1.mtime = meta1.crtime;
880 meta1.mtime_cs = meta1.crtime_cs; /* there is no atime_cs */
882 memset(&meta2, 0, sizeof(meta2));
883 meta2.type = EXFAT_ENTRY_FILE_INFO;
884 meta2.flags = EXFAT_FLAG_ALWAYS1;
885 meta2.name_length = name_length;
886 meta2.name_hash = exfat_calc_name_hash(ef, node->name);
887 meta2.start_cluster = cpu_to_le32(EXFAT_CLUSTER_FREE);
889 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);
891 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
892 co2o(ef, cluster, offset)) < 0)
894 exfat_error("failed to write meta1 entry");
897 if (!next_entry(ef, dir, &cluster, &offset))
899 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
900 co2o(ef, cluster, offset)) < 0)
902 exfat_error("failed to write meta2 entry");
905 for (i = 0; i < name_entries; i++)
907 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
908 memcpy(name_entry.name, node->name + i * EXFAT_ENAME_MAX,
909 MIN(EXFAT_ENAME_MAX, EXFAT_NAME_MAX - i * EXFAT_ENAME_MAX) *
911 if (!next_entry(ef, dir, &cluster, &offset))
913 if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
914 co2o(ef, cluster, offset)) < 0)
916 exfat_error("failed to write name entry");
921 init_node_meta1(node, &meta1);
922 init_node_meta2(node, &meta2);
924 tree_attach(dir, node);
925 exfat_update_mtime(dir);
929 static int create(struct exfat* ef, const char* path, uint16_t attrib)
931 struct exfat_node* dir;
932 struct exfat_node* existing;
933 cluster_t cluster = EXFAT_CLUSTER_BAD;
935 le16_t name[EXFAT_NAME_MAX + 1];
938 rc = exfat_split(ef, &dir, &existing, name, path);
941 if (existing != NULL)
943 exfat_put_node(ef, existing);
944 exfat_put_node(ef, dir);
948 rc = find_slot(ef, dir, &cluster, &offset,
949 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
952 exfat_put_node(ef, dir);
955 rc = write_entry(ef, dir, name, cluster, offset, attrib);
958 exfat_put_node(ef, dir);
961 rc = exfat_flush_node(ef, dir);
962 exfat_put_node(ef, dir);
966 int exfat_mknod(struct exfat* ef, const char* path)
968 return create(ef, path, EXFAT_ATTRIB_ARCH);
971 int exfat_mkdir(struct exfat* ef, const char* path)
974 struct exfat_node* node;
976 rc = create(ef, path, EXFAT_ATTRIB_DIR);
979 rc = exfat_lookup(ef, &node, path);
982 /* directories always have at least one cluster */
983 rc = exfat_truncate(ef, node, CLUSTER_SIZE(*ef->sb), true);
987 exfat_put_node(ef, node);
990 rc = exfat_flush_node(ef, node);
994 exfat_put_node(ef, node);
997 exfat_put_node(ef, node);
1001 static int rename_entry(struct exfat* ef, struct exfat_node* dir,
1002 struct exfat_node* node, const le16_t* name, cluster_t new_cluster,
1005 struct exfat_entry_meta1 meta1;
1006 struct exfat_entry_meta2 meta2;
1007 cluster_t old_cluster = node->entry_cluster;
1008 off_t old_offset = node->entry_offset;
1009 const size_t name_length = utf16_length(name);
1010 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
1013 if (exfat_pread(ef->dev, &meta1, sizeof(meta1),
1014 co2o(ef, old_cluster, old_offset)) < 0)
1016 exfat_error("failed to read meta1 entry on rename");
1019 if (!next_entry(ef, node->parent, &old_cluster, &old_offset))
1021 if (exfat_pread(ef->dev, &meta2, sizeof(meta2),
1022 co2o(ef, old_cluster, old_offset)) < 0)
1024 exfat_error("failed to read meta2 entry on rename");
1027 meta1.continuations = 1 + name_entries;
1028 meta2.name_hash = exfat_calc_name_hash(ef, name);
1029 meta2.name_length = name_length;
1030 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, name);
1032 if (!erase_entry(ef, node))
1035 node->entry_cluster = new_cluster;
1036 node->entry_offset = new_offset;
1038 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
1039 co2o(ef, new_cluster, new_offset)) < 0)
1041 exfat_error("failed to write meta1 entry on rename");
1044 if (!next_entry(ef, dir, &new_cluster, &new_offset))
1046 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
1047 co2o(ef, new_cluster, new_offset)) < 0)
1049 exfat_error("failed to write meta2 entry on rename");
1053 for (i = 0; i < name_entries; i++)
1055 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
1056 memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX,
1057 EXFAT_ENAME_MAX * sizeof(le16_t));
1058 if (!next_entry(ef, dir, &new_cluster, &new_offset))
1060 if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
1061 co2o(ef, new_cluster, new_offset)) < 0)
1063 exfat_error("failed to write name entry on rename");
1068 memcpy(node->name, name, (EXFAT_NAME_MAX + 1) * sizeof(le16_t));
1070 tree_attach(dir, node);
1074 int exfat_rename(struct exfat* ef, const char* old_path, const char* new_path)
1076 struct exfat_node* node;
1077 struct exfat_node* existing;
1078 struct exfat_node* dir;
1079 cluster_t cluster = EXFAT_CLUSTER_BAD;
1081 le16_t name[EXFAT_NAME_MAX + 1];
1084 rc = exfat_lookup(ef, &node, old_path);
1088 rc = exfat_split(ef, &dir, &existing, name, new_path);
1091 exfat_put_node(ef, node);
1095 /* check that target is not a subdirectory of the source */
1096 if (node->flags & EXFAT_ATTRIB_DIR)
1098 struct exfat_node* p;
1100 for (p = dir; p; p = p->parent)
1103 if (existing != NULL)
1104 exfat_put_node(ef, existing);
1105 exfat_put_node(ef, dir);
1106 exfat_put_node(ef, node);
1111 if (existing != NULL)
1113 /* remove target if it's not the same node as source */
1114 if (existing != node)
1116 if (existing->flags & EXFAT_ATTRIB_DIR)
1118 if (node->flags & EXFAT_ATTRIB_DIR)
1119 rc = exfat_rmdir(ef, existing);
1125 if (!(node->flags & EXFAT_ATTRIB_DIR))
1126 rc = exfat_unlink(ef, existing);
1130 exfat_put_node(ef, existing);
1133 exfat_put_node(ef, dir);
1134 exfat_put_node(ef, node);
1139 exfat_put_node(ef, existing);
1142 rc = find_slot(ef, dir, &cluster, &offset,
1143 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
1146 exfat_put_node(ef, dir);
1147 exfat_put_node(ef, node);
1150 rc = rename_entry(ef, dir, node, name, cluster, offset);
1151 exfat_put_node(ef, dir);
1152 exfat_put_node(ef, node);
1156 void exfat_utimes(struct exfat_node* node, const struct timespec tv[2])
1158 node->atime = tv[0].tv_sec;
1159 node->mtime = tv[1].tv_sec;
1160 node->flags |= EXFAT_ATTRIB_DIRTY;
1163 void exfat_update_atime(struct exfat_node* node)
1165 node->atime = time(NULL);
1166 node->flags |= EXFAT_ATTRIB_DIRTY;
1169 void exfat_update_mtime(struct exfat_node* node)
1171 node->mtime = time(NULL);
1172 node->flags |= EXFAT_ATTRIB_DIRTY;
1175 const char* exfat_get_label(struct exfat* ef)
1180 static int find_label(struct exfat* ef, cluster_t* cluster, off_t* offset)
1185 rc = opendir(ef, ef->root, &it);
1191 if (it.offset >= ef->root->size)
1197 if (get_entry_ptr(ef, &it)->type == EXFAT_ENTRY_LABEL)
1199 *cluster = it.cluster;
1200 *offset = it.offset;
1205 if (!fetch_next_entry(ef, ef->root, &it))
1213 int exfat_set_label(struct exfat* ef, const char* label)
1215 le16_t label_utf16[EXFAT_ENAME_MAX + 1];
1219 struct exfat_entry_label entry;
1221 memset(label_utf16, 0, sizeof(label_utf16));
1222 rc = utf8_to_utf16(label_utf16, label, EXFAT_ENAME_MAX, strlen(label));
1226 rc = find_label(ef, &cluster, &offset);
1228 rc = find_slot(ef, ef->root, &cluster, &offset, 1);
1232 entry.type = EXFAT_ENTRY_LABEL;
1233 entry.length = utf16_length(label_utf16);
1234 memcpy(entry.name, label_utf16, sizeof(entry.name));
1235 if (entry.length == 0)
1236 entry.type ^= EXFAT_ENTRY_VALID;
1238 if (exfat_pwrite(ef->dev, &entry, sizeof(struct exfat_entry_label),
1239 co2o(ef, cluster, offset)) < 0)
1241 exfat_error("failed to write label entry");
1244 strcpy(ef->label, label);