3 exFAT file system implementation library.
5 Free exFAT implementation.
6 Copyright (C) 2010-2014 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)
459 exfat_error("unknown entry type 0x%hhx", entry->type);
465 if (!fetch_next_entry(ef, parent, it))
468 /* we never reach here */
476 int exfat_cache_directory(struct exfat* ef, struct exfat_node* dir)
480 struct exfat_node* node;
481 struct exfat_node* current = NULL;
483 if (dir->flags & EXFAT_ATTRIB_CACHED)
484 return 0; /* already cached */
486 rc = opendir(ef, dir, &it);
489 while ((rc = readdir(ef, dir, &node, &it)) == 0)
494 current->next = node;
495 node->prev = current;
507 for (current = dir->child; current; current = node)
509 node = current->next;
516 dir->flags |= EXFAT_ATTRIB_CACHED;
520 static void tree_attach(struct exfat_node* dir, struct exfat_node* node)
525 dir->child->prev = node;
526 node->next = dir->child;
531 static void tree_detach(struct exfat_node* node)
534 node->prev->next = node->next;
535 else /* this is the first node in the list */
536 node->parent->child = node->next;
538 node->next->prev = node->prev;
544 static void reset_cache(struct exfat* ef, struct exfat_node* node)
546 char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
550 struct exfat_node* p = node->child;
555 node->flags &= ~EXFAT_ATTRIB_CACHED;
556 if (node->references != 0)
558 exfat_get_name(node, buffer, sizeof(buffer) - 1);
559 exfat_warn("non-zero reference counter (%d) for '%s'",
560 node->references, buffer);
562 if (node != ef->root && (node->flags & EXFAT_ATTRIB_DIRTY))
564 exfat_get_name(node, buffer, sizeof(buffer) - 1);
565 exfat_bug("node '%s' is dirty", buffer);
567 while (node->references)
568 exfat_put_node(ef, node);
571 void exfat_reset_cache(struct exfat* ef)
573 reset_cache(ef, ef->root);
576 static bool next_entry(struct exfat* ef, const struct exfat_node* parent,
577 cluster_t* cluster, off_t* offset)
579 *offset += sizeof(struct exfat_entry);
580 if (*offset % CLUSTER_SIZE(*ef->sb) == 0)
582 *cluster = exfat_next_cluster(ef, parent, *cluster);
583 if (CLUSTER_INVALID(*cluster))
585 exfat_error("invalid cluster %#x while getting next entry",
593 int exfat_flush_node(struct exfat* ef, struct exfat_node* node)
597 off_t meta1_offset, meta2_offset;
598 struct exfat_entry_meta1 meta1;
599 struct exfat_entry_meta2 meta2;
601 if (!(node->flags & EXFAT_ATTRIB_DIRTY))
602 return 0; /* no need to flush */
605 exfat_bug("unable to flush node to read-only FS");
607 if (node->parent == NULL)
608 return 0; /* do not flush unlinked node */
610 cluster = node->entry_cluster;
611 offset = node->entry_offset;
612 meta1_offset = co2o(ef, cluster, offset);
613 if (!next_entry(ef, node->parent, &cluster, &offset))
615 meta2_offset = co2o(ef, cluster, offset);
617 if (exfat_pread(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
619 exfat_error("failed to read meta1 entry on flush");
622 if (meta1.type != EXFAT_ENTRY_FILE)
623 exfat_bug("invalid type of meta1: 0x%hhx", meta1.type);
624 meta1.attrib = cpu_to_le16(node->flags);
625 exfat_unix2exfat(node->mtime, &meta1.mdate, &meta1.mtime, &meta1.mtime_cs);
626 exfat_unix2exfat(node->atime, &meta1.adate, &meta1.atime, NULL);
628 if (exfat_pread(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
630 exfat_error("failed to read meta2 entry on flush");
633 if (meta2.type != EXFAT_ENTRY_FILE_INFO)
634 exfat_bug("invalid type of meta2: 0x%hhx", meta2.type);
635 meta2.size = meta2.valid_size = cpu_to_le64(node->size);
636 meta2.start_cluster = cpu_to_le32(node->start_cluster);
637 meta2.flags = EXFAT_FLAG_ALWAYS1;
638 /* empty files must not be marked as contiguous */
639 if (node->size != 0 && IS_CONTIGUOUS(*node))
640 meta2.flags |= EXFAT_FLAG_CONTIGUOUS;
641 /* name hash remains unchanged, no need to recalculate it */
643 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);
645 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
647 exfat_error("failed to write meta1 entry on flush");
650 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
652 exfat_error("failed to write meta2 entry on flush");
656 node->flags &= ~EXFAT_ATTRIB_DIRTY;
660 static bool erase_entry(struct exfat* ef, struct exfat_node* node)
662 cluster_t cluster = node->entry_cluster;
663 off_t offset = node->entry_offset;
664 int name_entries = DIV_ROUND_UP(utf16_length(node->name), EXFAT_ENAME_MAX);
667 entry_type = EXFAT_ENTRY_FILE & ~EXFAT_ENTRY_VALID;
668 if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
670 exfat_error("failed to erase meta1 entry");
674 if (!next_entry(ef, node->parent, &cluster, &offset))
676 entry_type = EXFAT_ENTRY_FILE_INFO & ~EXFAT_ENTRY_VALID;
677 if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
679 exfat_error("failed to erase meta2 entry");
683 while (name_entries--)
685 if (!next_entry(ef, node->parent, &cluster, &offset))
687 entry_type = EXFAT_ENTRY_FILE_NAME & ~EXFAT_ENTRY_VALID;
688 if (exfat_pwrite(ef->dev, &entry_type, 1,
689 co2o(ef, cluster, offset)) < 0)
691 exfat_error("failed to erase name entry");
698 static int shrink_directory(struct exfat* ef, struct exfat_node* dir,
699 off_t deleted_offset)
701 const struct exfat_node* node;
702 const struct exfat_node* last_node;
703 uint64_t entries = 0;
706 if (!(dir->flags & EXFAT_ATTRIB_DIR))
707 exfat_bug("attempted to shrink a file");
708 if (!(dir->flags & EXFAT_ATTRIB_CACHED))
709 exfat_bug("attempted to shrink uncached directory");
711 for (last_node = node = dir->child; node; node = node->next)
713 if (deleted_offset < node->entry_offset)
715 /* there are other entries after the removed one, no way to shrink
719 if (last_node->entry_offset < node->entry_offset)
725 /* offset of the last entry */
726 entries += last_node->entry_offset / sizeof(struct exfat_entry);
727 /* two subentries with meta info */
729 /* subentries with file name */
730 entries += DIV_ROUND_UP(utf16_length(last_node->name),
734 new_size = DIV_ROUND_UP(entries * sizeof(struct exfat_entry),
735 CLUSTER_SIZE(*ef->sb)) * CLUSTER_SIZE(*ef->sb);
736 if (new_size == 0) /* directory always has at least 1 cluster */
737 new_size = CLUSTER_SIZE(*ef->sb);
738 if (new_size == dir->size)
740 return exfat_truncate(ef, dir, new_size, true);
743 static int delete(struct exfat* ef, struct exfat_node* node)
745 struct exfat_node* parent = node->parent;
746 off_t deleted_offset = node->entry_offset;
749 exfat_get_node(parent);
750 if (!erase_entry(ef, node))
752 exfat_put_node(ef, parent);
755 exfat_update_mtime(parent);
757 rc = shrink_directory(ef, parent, deleted_offset);
758 node->flags |= EXFAT_ATTRIB_UNLINKED;
761 exfat_flush_node(ef, parent);
762 exfat_put_node(ef, parent);
765 rc = exfat_flush_node(ef, parent);
766 exfat_put_node(ef, parent);
770 int exfat_unlink(struct exfat* ef, struct exfat_node* node)
772 if (node->flags & EXFAT_ATTRIB_DIR)
774 return delete(ef, node);
777 int exfat_rmdir(struct exfat* ef, struct exfat_node* node)
781 if (!(node->flags & EXFAT_ATTRIB_DIR))
783 /* check that directory is empty */
784 rc = exfat_cache_directory(ef, node);
789 return delete(ef, node);
792 static int grow_directory(struct exfat* ef, struct exfat_node* dir,
793 uint64_t asize, uint32_t difference)
795 return exfat_truncate(ef, dir,
796 DIV_ROUND_UP(asize + difference, CLUSTER_SIZE(*ef->sb))
797 * CLUSTER_SIZE(*ef->sb), true);
800 static int find_slot(struct exfat* ef, struct exfat_node* dir,
801 cluster_t* cluster, off_t* offset, int subentries)
805 const struct exfat_entry* entry;
808 rc = opendir(ef, dir, &it);
815 *cluster = it.cluster;
818 entry = get_entry_ptr(ef, &it);
819 if (entry->type & EXFAT_ENTRY_VALID)
823 if (contiguous == subentries)
824 break; /* suitable slot is found */
825 if (it.offset + sizeof(struct exfat_entry) >= dir->size)
827 rc = grow_directory(ef, dir, dir->size,
828 (subentries - contiguous) * sizeof(struct exfat_entry));
835 if (!fetch_next_entry(ef, dir, &it))
845 static int write_entry(struct exfat* ef, struct exfat_node* dir,
846 const le16_t* name, cluster_t cluster, off_t offset, uint16_t attrib)
848 struct exfat_node* node;
849 struct exfat_entry_meta1 meta1;
850 struct exfat_entry_meta2 meta2;
851 const size_t name_length = utf16_length(name);
852 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
855 node = allocate_node();
858 node->entry_cluster = cluster;
859 node->entry_offset = offset;
860 memcpy(node->name, name, name_length * sizeof(le16_t));
862 memset(&meta1, 0, sizeof(meta1));
863 meta1.type = EXFAT_ENTRY_FILE;
864 meta1.continuations = 1 + name_entries;
865 meta1.attrib = cpu_to_le16(attrib);
866 exfat_unix2exfat(time(NULL), &meta1.crdate, &meta1.crtime,
868 meta1.adate = meta1.mdate = meta1.crdate;
869 meta1.atime = meta1.mtime = meta1.crtime;
870 meta1.mtime_cs = meta1.crtime_cs; /* there is no atime_cs */
872 memset(&meta2, 0, sizeof(meta2));
873 meta2.type = EXFAT_ENTRY_FILE_INFO;
874 meta2.flags = EXFAT_FLAG_ALWAYS1;
875 meta2.name_length = name_length;
876 meta2.name_hash = exfat_calc_name_hash(ef, node->name);
877 meta2.start_cluster = cpu_to_le32(EXFAT_CLUSTER_FREE);
879 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);
881 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
882 co2o(ef, cluster, offset)) < 0)
884 exfat_error("failed to write meta1 entry");
887 if (!next_entry(ef, dir, &cluster, &offset))
889 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
890 co2o(ef, cluster, offset)) < 0)
892 exfat_error("failed to write meta2 entry");
895 for (i = 0; i < name_entries; i++)
897 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
898 memcpy(name_entry.name, node->name + i * EXFAT_ENAME_MAX,
899 MIN(EXFAT_ENAME_MAX, EXFAT_NAME_MAX - i * EXFAT_ENAME_MAX) *
901 if (!next_entry(ef, dir, &cluster, &offset))
903 if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
904 co2o(ef, cluster, offset)) < 0)
906 exfat_error("failed to write name entry");
911 init_node_meta1(node, &meta1);
912 init_node_meta2(node, &meta2);
914 tree_attach(dir, node);
915 exfat_update_mtime(dir);
919 static int create(struct exfat* ef, const char* path, uint16_t attrib)
921 struct exfat_node* dir;
922 struct exfat_node* existing;
923 cluster_t cluster = EXFAT_CLUSTER_BAD;
925 le16_t name[EXFAT_NAME_MAX + 1];
928 rc = exfat_split(ef, &dir, &existing, name, path);
931 if (existing != NULL)
933 exfat_put_node(ef, existing);
934 exfat_put_node(ef, dir);
938 rc = find_slot(ef, dir, &cluster, &offset,
939 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
942 exfat_put_node(ef, dir);
945 rc = write_entry(ef, dir, name, cluster, offset, attrib);
948 exfat_put_node(ef, dir);
951 rc = exfat_flush_node(ef, dir);
952 exfat_put_node(ef, dir);
956 int exfat_mknod(struct exfat* ef, const char* path)
958 return create(ef, path, EXFAT_ATTRIB_ARCH);
961 int exfat_mkdir(struct exfat* ef, const char* path)
964 struct exfat_node* node;
966 rc = create(ef, path, EXFAT_ATTRIB_ARCH | EXFAT_ATTRIB_DIR);
969 rc = exfat_lookup(ef, &node, path);
972 /* directories always have at least one cluster */
973 rc = exfat_truncate(ef, node, CLUSTER_SIZE(*ef->sb), true);
977 exfat_put_node(ef, node);
980 rc = exfat_flush_node(ef, node);
984 exfat_put_node(ef, node);
987 exfat_put_node(ef, node);
991 static int rename_entry(struct exfat* ef, struct exfat_node* dir,
992 struct exfat_node* node, const le16_t* name, cluster_t new_cluster,
995 struct exfat_entry_meta1 meta1;
996 struct exfat_entry_meta2 meta2;
997 cluster_t old_cluster = node->entry_cluster;
998 off_t old_offset = node->entry_offset;
999 const size_t name_length = utf16_length(name);
1000 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
1003 if (exfat_pread(ef->dev, &meta1, sizeof(meta1),
1004 co2o(ef, old_cluster, old_offset)) < 0)
1006 exfat_error("failed to read meta1 entry on rename");
1009 if (!next_entry(ef, node->parent, &old_cluster, &old_offset))
1011 if (exfat_pread(ef->dev, &meta2, sizeof(meta2),
1012 co2o(ef, old_cluster, old_offset)) < 0)
1014 exfat_error("failed to read meta2 entry on rename");
1017 meta1.continuations = 1 + name_entries;
1018 meta2.name_hash = exfat_calc_name_hash(ef, name);
1019 meta2.name_length = name_length;
1020 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, name);
1022 if (!erase_entry(ef, node))
1025 node->entry_cluster = new_cluster;
1026 node->entry_offset = new_offset;
1028 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
1029 co2o(ef, new_cluster, new_offset)) < 0)
1031 exfat_error("failed to write meta1 entry on rename");
1034 if (!next_entry(ef, dir, &new_cluster, &new_offset))
1036 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
1037 co2o(ef, new_cluster, new_offset)) < 0)
1039 exfat_error("failed to write meta2 entry on rename");
1043 for (i = 0; i < name_entries; i++)
1045 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
1046 memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX,
1047 EXFAT_ENAME_MAX * sizeof(le16_t));
1048 if (!next_entry(ef, dir, &new_cluster, &new_offset))
1050 if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
1051 co2o(ef, new_cluster, new_offset)) < 0)
1053 exfat_error("failed to write name entry on rename");
1058 memcpy(node->name, name, (EXFAT_NAME_MAX + 1) * sizeof(le16_t));
1060 tree_attach(dir, node);
1064 int exfat_rename(struct exfat* ef, const char* old_path, const char* new_path)
1066 struct exfat_node* node;
1067 struct exfat_node* existing;
1068 struct exfat_node* dir;
1069 cluster_t cluster = EXFAT_CLUSTER_BAD;
1071 le16_t name[EXFAT_NAME_MAX + 1];
1074 rc = exfat_lookup(ef, &node, old_path);
1078 rc = exfat_split(ef, &dir, &existing, name, new_path);
1081 exfat_put_node(ef, node);
1085 /* check that target is not a subdirectory of the source */
1086 if (node->flags & EXFAT_ATTRIB_DIR)
1088 struct exfat_node* p;
1090 for (p = dir; p; p = p->parent)
1093 if (existing != NULL)
1094 exfat_put_node(ef, existing);
1095 exfat_put_node(ef, dir);
1096 exfat_put_node(ef, node);
1101 if (existing != NULL)
1103 /* remove target if it's not the same node as source */
1104 if (existing != node)
1106 if (existing->flags & EXFAT_ATTRIB_DIR)
1108 if (node->flags & EXFAT_ATTRIB_DIR)
1109 rc = exfat_rmdir(ef, existing);
1115 if (!(node->flags & EXFAT_ATTRIB_DIR))
1116 rc = exfat_unlink(ef, existing);
1120 exfat_put_node(ef, existing);
1123 exfat_put_node(ef, dir);
1124 exfat_put_node(ef, node);
1129 exfat_put_node(ef, existing);
1132 rc = find_slot(ef, dir, &cluster, &offset,
1133 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
1136 exfat_put_node(ef, dir);
1137 exfat_put_node(ef, node);
1140 rc = rename_entry(ef, dir, node, name, cluster, offset);
1141 exfat_put_node(ef, dir);
1142 exfat_put_node(ef, node);
1146 void exfat_utimes(struct exfat_node* node, const struct timespec tv[2])
1148 node->atime = tv[0].tv_sec;
1149 node->mtime = tv[1].tv_sec;
1150 node->flags |= EXFAT_ATTRIB_DIRTY;
1153 void exfat_update_atime(struct exfat_node* node)
1155 node->atime = time(NULL);
1156 node->flags |= EXFAT_ATTRIB_DIRTY;
1159 void exfat_update_mtime(struct exfat_node* node)
1161 node->mtime = time(NULL);
1162 node->flags |= EXFAT_ATTRIB_DIRTY;
1165 const char* exfat_get_label(struct exfat* ef)
1170 static int find_label(struct exfat* ef, cluster_t* cluster, off_t* offset)
1175 rc = opendir(ef, ef->root, &it);
1181 if (it.offset >= ef->root->size)
1187 if (get_entry_ptr(ef, &it)->type == EXFAT_ENTRY_LABEL)
1189 *cluster = it.cluster;
1190 *offset = it.offset;
1195 if (!fetch_next_entry(ef, ef->root, &it))
1203 int exfat_set_label(struct exfat* ef, const char* label)
1205 le16_t label_utf16[EXFAT_ENAME_MAX + 1];
1209 struct exfat_entry_label entry;
1211 memset(label_utf16, 0, sizeof(label_utf16));
1212 rc = utf8_to_utf16(label_utf16, label, EXFAT_ENAME_MAX, strlen(label));
1216 rc = find_label(ef, &cluster, &offset);
1218 rc = find_slot(ef, ef->root, &cluster, &offset, 1);
1222 entry.type = EXFAT_ENTRY_LABEL;
1223 entry.length = utf16_length(label_utf16);
1224 memcpy(entry.name, label_utf16, sizeof(entry.name));
1225 if (entry.length == 0)
1226 entry.type ^= EXFAT_ENTRY_VALID;
1228 if (exfat_pwrite(ef->dev, &entry, sizeof(struct exfat_entry_label),
1229 co2o(ef, cluster, offset)) < 0)
1231 exfat_error("failed to write label entry");
1234 strcpy(ef->label, label);