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 if (CLUSTER_INVALID(dir->start_cluster))
106 exfat_get_name(dir, buffer, sizeof(buffer) - 1);
107 exfat_error("'%s' directory starts with invalid cluster %#x", buffer,
111 it->cluster = dir->start_cluster;
113 it->chunk = malloc(CLUSTER_SIZE(*ef->sb));
114 if (it->chunk == NULL)
116 exfat_error("failed to allocate memory for directory cluster");
119 if (exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb),
120 exfat_c2o(ef, it->cluster)) < 0)
123 exfat_get_name(dir, buffer, sizeof(buffer) - 1);
124 exfat_error("failed to read '%s' directory cluster %#x", buffer,
131 static void closedir(struct iterator* it)
139 static bool fetch_next_entry(struct exfat* ef, const struct exfat_node* parent,
142 /* move iterator to the next entry in the directory */
143 it->offset += sizeof(struct exfat_entry);
144 /* fetch the next cluster if needed */
145 if ((it->offset & (CLUSTER_SIZE(*ef->sb) - 1)) == 0)
147 /* reached the end of directory; the caller should check this
149 if (it->offset >= parent->size)
151 it->cluster = exfat_next_cluster(ef, parent, it->cluster);
152 if (CLUSTER_INVALID(it->cluster))
154 exfat_error("invalid cluster 0x%x while reading directory",
158 if (exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb),
159 exfat_c2o(ef, it->cluster)) < 0)
161 exfat_error("failed to read the next directory cluster %#x",
169 static struct exfat_node* allocate_node(void)
171 struct exfat_node* node = malloc(sizeof(struct exfat_node));
174 exfat_error("failed to allocate node");
177 memset(node, 0, sizeof(struct exfat_node));
181 static void init_node_meta1(struct exfat_node* node,
182 const struct exfat_entry_meta1* meta1)
184 node->flags = le16_to_cpu(meta1->attrib);
185 node->mtime = exfat_exfat2unix(meta1->mdate, meta1->mtime,
187 /* there is no centiseconds field for atime */
188 node->atime = exfat_exfat2unix(meta1->adate, meta1->atime, 0);
191 static void init_node_meta2(struct exfat_node* node,
192 const struct exfat_entry_meta2* meta2)
194 node->size = le64_to_cpu(meta2->size);
195 node->start_cluster = le32_to_cpu(meta2->start_cluster);
196 node->fptr_cluster = node->start_cluster;
197 if (meta2->flags & EXFAT_FLAG_CONTIGUOUS)
198 node->flags |= EXFAT_ATTRIB_CONTIGUOUS;
201 static const struct exfat_entry* get_entry_ptr(const struct exfat* ef,
202 const struct iterator* it)
204 return (const struct exfat_entry*)
205 (it->chunk + it->offset % CLUSTER_SIZE(*ef->sb));
208 static bool check_node(const struct exfat_node* node, uint16_t actual_checksum,
209 uint16_t reference_checksum, uint64_t valid_size)
211 char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
215 Validate checksum first. If it's invalid all other fields probably
216 contain just garbage.
218 if (actual_checksum != reference_checksum)
220 exfat_get_name(node, buffer, sizeof(buffer) - 1);
221 exfat_error("'%s' has invalid checksum (%#hx != %#hx)", buffer,
222 actual_checksum, reference_checksum);
227 exFAT does not support sparse files but allows files with uninitialized
228 clusters. For such files valid_size means initialized data size and
229 cannot be greater than file size. See SetFileValidData() function
232 if (valid_size > node->size)
234 exfat_get_name(node, buffer, sizeof(buffer) - 1);
235 exfat_error("'%s' has valid size (%"PRIu64") greater than size "
236 "(%"PRIu64")", buffer, valid_size, node->size);
241 Empty file must have zero start cluster. Non-empty file must start
242 with a valid cluster. Directories cannot be empty (i.e. must always
243 have a valid start cluster), but we will check this later in opendir()
244 to give user a chance to read current directory.
246 if (node->size == 0 && node->start_cluster != EXFAT_CLUSTER_FREE)
248 exfat_get_name(node, buffer, sizeof(buffer) - 1);
249 exfat_error("'%s' is empty but start cluster is %#x", buffer,
250 node->start_cluster);
253 if (node->size > 0 && CLUSTER_INVALID(node->start_cluster))
255 exfat_get_name(node, buffer, sizeof(buffer) - 1);
256 exfat_error("'%s' points to invalid cluster %#x", buffer,
257 node->start_cluster);
264 static void decompress_upcase(uint16_t* output, const le16_t* source,
270 for (oi = 0; oi < EXFAT_UPCASE_CHARS; oi++)
273 for (si = 0, oi = 0; si < size && oi < EXFAT_UPCASE_CHARS; si++)
275 uint16_t ch = le16_to_cpu(source[si]);
277 if (ch == 0xffff && si + 1 < size) /* indicates a run */
278 oi += le16_to_cpu(source[++si]);
285 * Reads one entry in directory at position pointed by iterator and fills
288 static int readdir(struct exfat* ef, const struct exfat_node* parent,
289 struct exfat_node** node, struct iterator* it)
292 const struct exfat_entry* entry;
293 const struct exfat_entry_meta1* meta1;
294 const struct exfat_entry_meta2* meta2;
295 const struct exfat_entry_name* file_name;
296 const struct exfat_entry_upcase* upcase;
297 const struct exfat_entry_bitmap* bitmap;
298 const struct exfat_entry_label* label;
299 uint8_t continuations = 0;
300 le16_t* namep = NULL;
301 uint16_t reference_checksum = 0;
302 uint16_t actual_checksum = 0;
303 uint64_t valid_size = 0;
304 uint64_t upcase_size = 0;
305 le16_t* upcase_comp = NULL;
311 if (it->offset >= parent->size)
313 if (continuations != 0)
315 exfat_error("expected %hhu continuations", continuations);
318 return -ENOENT; /* that's OK, means end of directory */
321 entry = get_entry_ptr(ef, it);
324 case EXFAT_ENTRY_FILE:
325 if (continuations != 0)
327 exfat_error("expected %hhu continuations before new entry",
331 meta1 = (const struct exfat_entry_meta1*) entry;
332 continuations = meta1->continuations;
333 /* each file entry must have at least 2 continuations:
335 if (continuations < 2)
337 exfat_error("too few continuations (%hhu)", continuations);
340 if (continuations > 1 +
341 DIV_ROUND_UP(EXFAT_NAME_MAX, EXFAT_ENAME_MAX))
343 exfat_error("too many continuations (%hhu)", continuations);
346 reference_checksum = le16_to_cpu(meta1->checksum);
347 actual_checksum = exfat_start_checksum(meta1);
348 *node = allocate_node();
354 /* new node has zero reference counter */
355 (*node)->entry_cluster = it->cluster;
356 (*node)->entry_offset = it->offset;
357 init_node_meta1(*node, meta1);
358 namep = (*node)->name;
361 case EXFAT_ENTRY_FILE_INFO:
362 if (continuations < 2)
364 exfat_error("unexpected continuation (%hhu)",
368 meta2 = (const struct exfat_entry_meta2*) entry;
369 if (meta2->flags & ~(EXFAT_FLAG_ALWAYS1 | EXFAT_FLAG_CONTIGUOUS))
371 exfat_error("unknown flags in meta2 (0x%hhx)", meta2->flags);
374 init_node_meta2(*node, meta2);
375 actual_checksum = exfat_add_checksum(entry, actual_checksum);
376 valid_size = le64_to_cpu(meta2->valid_size);
377 /* empty files must be marked as non-contiguous */
378 if ((*node)->size == 0 && (meta2->flags & EXFAT_FLAG_CONTIGUOUS))
380 exfat_error("empty file marked as contiguous (0x%hhx)",
384 /* directories must be aligned on at cluster boundary */
385 if (((*node)->flags & EXFAT_ATTRIB_DIR) &&
386 (*node)->size % CLUSTER_SIZE(*ef->sb) != 0)
388 exfat_error("directory has invalid size %"PRIu64" bytes",
395 case EXFAT_ENTRY_FILE_NAME:
396 if (continuations == 0)
398 exfat_error("unexpected continuation");
401 file_name = (const struct exfat_entry_name*) entry;
402 actual_checksum = exfat_add_checksum(entry, actual_checksum);
404 memcpy(namep, file_name->name,
406 ((*node)->name + EXFAT_NAME_MAX - namep)) *
408 namep += EXFAT_ENAME_MAX;
409 if (--continuations == 0)
411 if (!check_node(*node, actual_checksum, reference_checksum,
414 if (!fetch_next_entry(ef, parent, it))
416 return 0; /* entry completed */
420 case EXFAT_ENTRY_UPCASE:
421 if (ef->upcase != NULL)
423 upcase = (const struct exfat_entry_upcase*) entry;
424 if (CLUSTER_INVALID(le32_to_cpu(upcase->start_cluster)))
426 exfat_error("invalid cluster 0x%x in upcase table",
427 le32_to_cpu(upcase->start_cluster));
430 upcase_size = le64_to_cpu(upcase->size);
431 if (upcase_size == 0 ||
432 upcase_size > EXFAT_UPCASE_CHARS * sizeof(uint16_t) ||
433 upcase_size % sizeof(uint16_t) != 0)
435 exfat_error("bad upcase table size (%"PRIu64" bytes)",
439 upcase_comp = malloc(upcase_size);
440 if (upcase_comp == NULL)
442 exfat_error("failed to allocate upcase table (%"PRIu64" bytes)",
448 /* read compressed upcase table */
449 if (exfat_pread(ef->dev, upcase_comp, upcase_size,
450 exfat_c2o(ef, le32_to_cpu(upcase->start_cluster))) < 0)
453 exfat_error("failed to read upper case table "
454 "(%"PRIu64" bytes starting at cluster %#x)",
456 le32_to_cpu(upcase->start_cluster));
460 /* decompress upcase table */
461 ef->upcase = calloc(EXFAT_UPCASE_CHARS, sizeof(uint16_t));
462 if (ef->upcase == NULL)
465 exfat_error("failed to allocate decompressed upcase table");
469 decompress_upcase(ef->upcase, upcase_comp,
470 upcase_size / sizeof(uint16_t));
474 case EXFAT_ENTRY_BITMAP:
475 bitmap = (const struct exfat_entry_bitmap*) entry;
476 ef->cmap.start_cluster = le32_to_cpu(bitmap->start_cluster);
477 if (CLUSTER_INVALID(ef->cmap.start_cluster))
479 exfat_error("invalid cluster 0x%x in clusters bitmap",
480 ef->cmap.start_cluster);
483 ef->cmap.size = le32_to_cpu(ef->sb->cluster_count) -
484 EXFAT_FIRST_DATA_CLUSTER;
485 if (le64_to_cpu(bitmap->size) < DIV_ROUND_UP(ef->cmap.size, 8))
487 exfat_error("invalid clusters bitmap size: %"PRIu64
488 " (expected at least %u)",
489 le64_to_cpu(bitmap->size),
490 DIV_ROUND_UP(ef->cmap.size, 8));
493 /* FIXME bitmap can be rather big, up to 512 MB */
494 ef->cmap.chunk_size = ef->cmap.size;
495 ef->cmap.chunk = malloc(BMAP_SIZE(ef->cmap.chunk_size));
496 if (ef->cmap.chunk == NULL)
498 exfat_error("failed to allocate clusters bitmap chunk "
499 "(%"PRIu64" bytes)", le64_to_cpu(bitmap->size));
504 if (exfat_pread(ef->dev, ef->cmap.chunk,
505 BMAP_SIZE(ef->cmap.chunk_size),
506 exfat_c2o(ef, ef->cmap.start_cluster)) < 0)
508 exfat_error("failed to read clusters bitmap "
509 "(%"PRIu64" bytes starting at cluster %#x)",
510 le64_to_cpu(bitmap->size), ef->cmap.start_cluster);
515 case EXFAT_ENTRY_LABEL:
516 label = (const struct exfat_entry_label*) entry;
517 if (label->length > EXFAT_ENAME_MAX)
519 exfat_error("too long label (%hhu chars)", label->length);
522 if (utf16_to_utf8(ef->label, label->name,
523 sizeof(ef->label) - 1, EXFAT_ENAME_MAX) != 0)
528 if (!(entry->type & EXFAT_ENTRY_VALID))
529 break; /* deleted entry, ignore it */
530 if (!(entry->type & EXFAT_ENTRY_OPTIONAL))
532 exfat_error("unknown entry type %#hhx", entry->type);
535 /* optional entry, warn and skip */
536 exfat_warn("unknown entry type %#hhx", entry->type);
537 if (continuations == 0)
539 exfat_error("unexpected continuation");
546 if (!fetch_next_entry(ef, parent, it))
549 /* we never reach here */
557 int exfat_cache_directory(struct exfat* ef, struct exfat_node* dir)
561 struct exfat_node* node;
562 struct exfat_node* current = NULL;
564 if (dir->flags & EXFAT_ATTRIB_CACHED)
565 return 0; /* already cached */
567 rc = opendir(ef, dir, &it);
570 while ((rc = readdir(ef, dir, &node, &it)) == 0)
575 current->next = node;
576 node->prev = current;
588 for (current = dir->child; current; current = node)
590 node = current->next;
597 dir->flags |= EXFAT_ATTRIB_CACHED;
601 static void tree_attach(struct exfat_node* dir, struct exfat_node* node)
606 dir->child->prev = node;
607 node->next = dir->child;
612 static void tree_detach(struct exfat_node* node)
615 node->prev->next = node->next;
616 else /* this is the first node in the list */
617 node->parent->child = node->next;
619 node->next->prev = node->prev;
625 static void reset_cache(struct exfat* ef, struct exfat_node* node)
627 char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
631 struct exfat_node* p = node->child;
636 node->flags &= ~EXFAT_ATTRIB_CACHED;
637 if (node->references != 0)
639 exfat_get_name(node, buffer, sizeof(buffer) - 1);
640 exfat_warn("non-zero reference counter (%d) for '%s'",
641 node->references, buffer);
643 if (node != ef->root && (node->flags & EXFAT_ATTRIB_DIRTY))
645 exfat_get_name(node, buffer, sizeof(buffer) - 1);
646 exfat_bug("node '%s' is dirty", buffer);
648 while (node->references)
649 exfat_put_node(ef, node);
652 void exfat_reset_cache(struct exfat* ef)
654 reset_cache(ef, ef->root);
657 static bool next_entry(struct exfat* ef, const struct exfat_node* parent,
658 cluster_t* cluster, off_t* offset)
660 *offset += sizeof(struct exfat_entry);
661 if (*offset % CLUSTER_SIZE(*ef->sb) == 0)
663 *cluster = exfat_next_cluster(ef, parent, *cluster);
664 if (CLUSTER_INVALID(*cluster))
666 exfat_error("invalid cluster %#x while getting next entry",
674 int exfat_flush_node(struct exfat* ef, struct exfat_node* node)
678 off_t meta1_offset, meta2_offset;
679 struct exfat_entry_meta1 meta1;
680 struct exfat_entry_meta2 meta2;
682 if (!(node->flags & EXFAT_ATTRIB_DIRTY))
683 return 0; /* no need to flush */
686 exfat_bug("unable to flush node to read-only FS");
688 if (node->parent == NULL)
689 return 0; /* do not flush unlinked node */
691 cluster = node->entry_cluster;
692 offset = node->entry_offset;
693 meta1_offset = co2o(ef, cluster, offset);
694 if (!next_entry(ef, node->parent, &cluster, &offset))
696 meta2_offset = co2o(ef, cluster, offset);
698 if (exfat_pread(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
700 exfat_error("failed to read meta1 entry on flush");
703 if (meta1.type != EXFAT_ENTRY_FILE)
704 exfat_bug("invalid type of meta1: 0x%hhx", meta1.type);
705 meta1.attrib = cpu_to_le16(node->flags);
706 exfat_unix2exfat(node->mtime, &meta1.mdate, &meta1.mtime, &meta1.mtime_cs);
707 exfat_unix2exfat(node->atime, &meta1.adate, &meta1.atime, NULL);
709 if (exfat_pread(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
711 exfat_error("failed to read meta2 entry on flush");
714 if (meta2.type != EXFAT_ENTRY_FILE_INFO)
715 exfat_bug("invalid type of meta2: 0x%hhx", meta2.type);
716 meta2.size = meta2.valid_size = cpu_to_le64(node->size);
717 meta2.start_cluster = cpu_to_le32(node->start_cluster);
718 meta2.flags = EXFAT_FLAG_ALWAYS1;
719 /* empty files must not be marked as contiguous */
720 if (node->size != 0 && IS_CONTIGUOUS(*node))
721 meta2.flags |= EXFAT_FLAG_CONTIGUOUS;
722 /* name hash remains unchanged, no need to recalculate it */
724 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);
726 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
728 exfat_error("failed to write meta1 entry on flush");
731 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
733 exfat_error("failed to write meta2 entry on flush");
737 node->flags &= ~EXFAT_ATTRIB_DIRTY;
738 return exfat_flush(ef);
741 static bool erase_entry(struct exfat* ef, struct exfat_node* node)
743 cluster_t cluster = node->entry_cluster;
744 off_t offset = node->entry_offset;
745 int name_entries = DIV_ROUND_UP(utf16_length(node->name), EXFAT_ENAME_MAX);
748 entry_type = EXFAT_ENTRY_FILE & ~EXFAT_ENTRY_VALID;
749 if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
751 exfat_error("failed to erase meta1 entry");
755 if (!next_entry(ef, node->parent, &cluster, &offset))
757 entry_type = EXFAT_ENTRY_FILE_INFO & ~EXFAT_ENTRY_VALID;
758 if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
760 exfat_error("failed to erase meta2 entry");
764 while (name_entries--)
766 if (!next_entry(ef, node->parent, &cluster, &offset))
768 entry_type = EXFAT_ENTRY_FILE_NAME & ~EXFAT_ENTRY_VALID;
769 if (exfat_pwrite(ef->dev, &entry_type, 1,
770 co2o(ef, cluster, offset)) < 0)
772 exfat_error("failed to erase name entry");
779 static int shrink_directory(struct exfat* ef, struct exfat_node* dir,
780 off_t deleted_offset)
782 const struct exfat_node* node;
783 const struct exfat_node* last_node;
784 uint64_t entries = 0;
787 if (!(dir->flags & EXFAT_ATTRIB_DIR))
788 exfat_bug("attempted to shrink a file");
789 if (!(dir->flags & EXFAT_ATTRIB_CACHED))
790 exfat_bug("attempted to shrink uncached directory");
792 for (last_node = node = dir->child; node; node = node->next)
794 if (deleted_offset < node->entry_offset)
796 /* there are other entries after the removed one, no way to shrink
800 if (last_node->entry_offset < node->entry_offset)
806 /* offset of the last entry */
807 entries += last_node->entry_offset / sizeof(struct exfat_entry);
808 /* two subentries with meta info */
810 /* subentries with file name */
811 entries += DIV_ROUND_UP(utf16_length(last_node->name),
815 new_size = DIV_ROUND_UP(entries * sizeof(struct exfat_entry),
816 CLUSTER_SIZE(*ef->sb)) * CLUSTER_SIZE(*ef->sb);
817 if (new_size == 0) /* directory always has at least 1 cluster */
818 new_size = CLUSTER_SIZE(*ef->sb);
819 if (new_size == dir->size)
821 return exfat_truncate(ef, dir, new_size, true);
824 static int delete(struct exfat* ef, struct exfat_node* node)
826 struct exfat_node* parent = node->parent;
827 off_t deleted_offset = node->entry_offset;
830 exfat_get_node(parent);
831 if (!erase_entry(ef, node))
833 exfat_put_node(ef, parent);
836 exfat_update_mtime(parent);
838 rc = shrink_directory(ef, parent, deleted_offset);
839 node->flags |= EXFAT_ATTRIB_UNLINKED;
842 exfat_flush_node(ef, parent);
843 exfat_put_node(ef, parent);
846 rc = exfat_flush_node(ef, parent);
847 exfat_put_node(ef, parent);
851 int exfat_unlink(struct exfat* ef, struct exfat_node* node)
853 if (node->flags & EXFAT_ATTRIB_DIR)
855 return delete(ef, node);
858 int exfat_rmdir(struct exfat* ef, struct exfat_node* node)
862 if (!(node->flags & EXFAT_ATTRIB_DIR))
864 /* check that directory is empty */
865 rc = exfat_cache_directory(ef, node);
870 return delete(ef, node);
873 static int grow_directory(struct exfat* ef, struct exfat_node* dir,
874 uint64_t asize, uint32_t difference)
876 return exfat_truncate(ef, dir,
877 DIV_ROUND_UP(asize + difference, CLUSTER_SIZE(*ef->sb))
878 * CLUSTER_SIZE(*ef->sb), true);
881 static int find_slot(struct exfat* ef, struct exfat_node* dir,
882 cluster_t* cluster, off_t* offset, int subentries)
886 const struct exfat_entry* entry;
889 rc = opendir(ef, dir, &it);
896 *cluster = it.cluster;
899 entry = get_entry_ptr(ef, &it);
900 if (entry->type & EXFAT_ENTRY_VALID)
904 if (contiguous == subentries)
905 break; /* suitable slot is found */
906 if (it.offset + sizeof(struct exfat_entry) >= dir->size)
908 rc = grow_directory(ef, dir, dir->size,
909 (subentries - contiguous) * sizeof(struct exfat_entry));
916 if (!fetch_next_entry(ef, dir, &it))
926 static int write_entry(struct exfat* ef, struct exfat_node* dir,
927 const le16_t* name, cluster_t cluster, off_t offset, uint16_t attrib)
929 struct exfat_node* node;
930 struct exfat_entry_meta1 meta1;
931 struct exfat_entry_meta2 meta2;
932 const size_t name_length = utf16_length(name);
933 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
936 node = allocate_node();
939 node->entry_cluster = cluster;
940 node->entry_offset = offset;
941 memcpy(node->name, name, name_length * sizeof(le16_t));
943 memset(&meta1, 0, sizeof(meta1));
944 meta1.type = EXFAT_ENTRY_FILE;
945 meta1.continuations = 1 + name_entries;
946 meta1.attrib = cpu_to_le16(attrib);
947 exfat_unix2exfat(time(NULL), &meta1.crdate, &meta1.crtime,
949 meta1.adate = meta1.mdate = meta1.crdate;
950 meta1.atime = meta1.mtime = meta1.crtime;
951 meta1.mtime_cs = meta1.crtime_cs; /* there is no atime_cs */
953 memset(&meta2, 0, sizeof(meta2));
954 meta2.type = EXFAT_ENTRY_FILE_INFO;
955 meta2.flags = EXFAT_FLAG_ALWAYS1;
956 meta2.name_length = name_length;
957 meta2.name_hash = exfat_calc_name_hash(ef, node->name);
958 meta2.start_cluster = cpu_to_le32(EXFAT_CLUSTER_FREE);
960 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);
962 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
963 co2o(ef, cluster, offset)) < 0)
965 exfat_error("failed to write meta1 entry");
968 if (!next_entry(ef, dir, &cluster, &offset))
970 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
971 co2o(ef, cluster, offset)) < 0)
973 exfat_error("failed to write meta2 entry");
976 for (i = 0; i < name_entries; i++)
978 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
979 memcpy(name_entry.name, node->name + i * EXFAT_ENAME_MAX,
980 MIN(EXFAT_ENAME_MAX, EXFAT_NAME_MAX - i * EXFAT_ENAME_MAX) *
982 if (!next_entry(ef, dir, &cluster, &offset))
984 if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
985 co2o(ef, cluster, offset)) < 0)
987 exfat_error("failed to write name entry");
992 init_node_meta1(node, &meta1);
993 init_node_meta2(node, &meta2);
995 tree_attach(dir, node);
996 exfat_update_mtime(dir);
1000 static int create(struct exfat* ef, const char* path, uint16_t attrib)
1002 struct exfat_node* dir;
1003 struct exfat_node* existing;
1004 cluster_t cluster = EXFAT_CLUSTER_BAD;
1006 le16_t name[EXFAT_NAME_MAX + 1];
1009 rc = exfat_split(ef, &dir, &existing, name, path);
1012 if (existing != NULL)
1014 exfat_put_node(ef, existing);
1015 exfat_put_node(ef, dir);
1019 rc = find_slot(ef, dir, &cluster, &offset,
1020 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
1023 exfat_put_node(ef, dir);
1026 rc = write_entry(ef, dir, name, cluster, offset, attrib);
1029 exfat_put_node(ef, dir);
1032 rc = exfat_flush_node(ef, dir);
1033 exfat_put_node(ef, dir);
1037 int exfat_mknod(struct exfat* ef, const char* path)
1039 return create(ef, path, EXFAT_ATTRIB_ARCH);
1042 int exfat_mkdir(struct exfat* ef, const char* path)
1045 struct exfat_node* node;
1047 rc = create(ef, path, EXFAT_ATTRIB_DIR);
1050 rc = exfat_lookup(ef, &node, path);
1053 /* directories always have at least one cluster */
1054 rc = exfat_truncate(ef, node, CLUSTER_SIZE(*ef->sb), true);
1058 exfat_put_node(ef, node);
1061 rc = exfat_flush_node(ef, node);
1065 exfat_put_node(ef, node);
1068 exfat_put_node(ef, node);
1072 static int rename_entry(struct exfat* ef, struct exfat_node* dir,
1073 struct exfat_node* node, const le16_t* name, cluster_t new_cluster,
1076 struct exfat_entry_meta1 meta1;
1077 struct exfat_entry_meta2 meta2;
1078 cluster_t old_cluster = node->entry_cluster;
1079 off_t old_offset = node->entry_offset;
1080 const size_t name_length = utf16_length(name);
1081 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
1084 if (exfat_pread(ef->dev, &meta1, sizeof(meta1),
1085 co2o(ef, old_cluster, old_offset)) < 0)
1087 exfat_error("failed to read meta1 entry on rename");
1090 if (!next_entry(ef, node->parent, &old_cluster, &old_offset))
1092 if (exfat_pread(ef->dev, &meta2, sizeof(meta2),
1093 co2o(ef, old_cluster, old_offset)) < 0)
1095 exfat_error("failed to read meta2 entry on rename");
1098 meta1.continuations = 1 + name_entries;
1099 meta2.name_hash = exfat_calc_name_hash(ef, name);
1100 meta2.name_length = name_length;
1101 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, name);
1103 if (!erase_entry(ef, node))
1106 node->entry_cluster = new_cluster;
1107 node->entry_offset = new_offset;
1109 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
1110 co2o(ef, new_cluster, new_offset)) < 0)
1112 exfat_error("failed to write meta1 entry on rename");
1115 if (!next_entry(ef, dir, &new_cluster, &new_offset))
1117 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
1118 co2o(ef, new_cluster, new_offset)) < 0)
1120 exfat_error("failed to write meta2 entry on rename");
1124 for (i = 0; i < name_entries; i++)
1126 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
1127 memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX,
1128 EXFAT_ENAME_MAX * sizeof(le16_t));
1129 if (!next_entry(ef, dir, &new_cluster, &new_offset))
1131 if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
1132 co2o(ef, new_cluster, new_offset)) < 0)
1134 exfat_error("failed to write name entry on rename");
1139 memcpy(node->name, name, (EXFAT_NAME_MAX + 1) * sizeof(le16_t));
1141 tree_attach(dir, node);
1145 int exfat_rename(struct exfat* ef, const char* old_path, const char* new_path)
1147 struct exfat_node* node;
1148 struct exfat_node* existing;
1149 struct exfat_node* dir;
1150 cluster_t cluster = EXFAT_CLUSTER_BAD;
1152 le16_t name[EXFAT_NAME_MAX + 1];
1155 rc = exfat_lookup(ef, &node, old_path);
1159 rc = exfat_split(ef, &dir, &existing, name, new_path);
1162 exfat_put_node(ef, node);
1166 /* check that target is not a subdirectory of the source */
1167 if (node->flags & EXFAT_ATTRIB_DIR)
1169 struct exfat_node* p;
1171 for (p = dir; p; p = p->parent)
1174 if (existing != NULL)
1175 exfat_put_node(ef, existing);
1176 exfat_put_node(ef, dir);
1177 exfat_put_node(ef, node);
1182 if (existing != NULL)
1184 /* remove target if it's not the same node as source */
1185 if (existing != node)
1187 if (existing->flags & EXFAT_ATTRIB_DIR)
1189 if (node->flags & EXFAT_ATTRIB_DIR)
1190 rc = exfat_rmdir(ef, existing);
1196 if (!(node->flags & EXFAT_ATTRIB_DIR))
1197 rc = exfat_unlink(ef, existing);
1201 exfat_put_node(ef, existing);
1204 /* free clusters even if something went wrong; overwise they
1205 will be just lost */
1206 exfat_cleanup_node(ef, existing);
1207 exfat_put_node(ef, dir);
1208 exfat_put_node(ef, node);
1211 rc = exfat_cleanup_node(ef, existing);
1214 exfat_put_node(ef, dir);
1215 exfat_put_node(ef, node);
1220 exfat_put_node(ef, existing);
1223 rc = find_slot(ef, dir, &cluster, &offset,
1224 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
1227 exfat_put_node(ef, dir);
1228 exfat_put_node(ef, node);
1231 rc = rename_entry(ef, dir, node, name, cluster, offset);
1232 exfat_put_node(ef, dir);
1233 exfat_put_node(ef, node);
1237 void exfat_utimes(struct exfat_node* node, const struct timespec tv[2])
1239 node->atime = tv[0].tv_sec;
1240 node->mtime = tv[1].tv_sec;
1241 node->flags |= EXFAT_ATTRIB_DIRTY;
1244 void exfat_update_atime(struct exfat_node* node)
1246 node->atime = time(NULL);
1247 node->flags |= EXFAT_ATTRIB_DIRTY;
1250 void exfat_update_mtime(struct exfat_node* node)
1252 node->mtime = time(NULL);
1253 node->flags |= EXFAT_ATTRIB_DIRTY;
1256 const char* exfat_get_label(struct exfat* ef)
1261 static int find_label(struct exfat* ef, cluster_t* cluster, off_t* offset)
1266 rc = opendir(ef, ef->root, &it);
1272 if (it.offset >= ef->root->size)
1278 if (get_entry_ptr(ef, &it)->type == EXFAT_ENTRY_LABEL)
1280 *cluster = it.cluster;
1281 *offset = it.offset;
1286 if (!fetch_next_entry(ef, ef->root, &it))
1294 int exfat_set_label(struct exfat* ef, const char* label)
1296 le16_t label_utf16[EXFAT_ENAME_MAX + 1];
1300 struct exfat_entry_label entry;
1302 memset(label_utf16, 0, sizeof(label_utf16));
1303 rc = utf8_to_utf16(label_utf16, label, EXFAT_ENAME_MAX, strlen(label));
1307 rc = find_label(ef, &cluster, &offset);
1309 rc = find_slot(ef, ef->root, &cluster, &offset, 1);
1313 entry.type = EXFAT_ENTRY_LABEL;
1314 entry.length = utf16_length(label_utf16);
1315 memcpy(entry.name, label_utf16, sizeof(entry.name));
1316 if (entry.length == 0)
1317 entry.type ^= EXFAT_ENTRY_VALID;
1319 if (exfat_pwrite(ef->dev, &entry, sizeof(struct exfat_entry_label),
1320 co2o(ef, cluster, offset)) < 0)
1322 exfat_error("failed to write label entry");
1325 strcpy(ef->label, label);