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, int cluster_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);
261 /* Empty file or directory must be marked as non-contiguous. */
262 if (node->size == 0 && (node->flags & EXFAT_ATTRIB_CONTIGUOUS))
264 exfat_get_name(node, buffer, sizeof(buffer) - 1);
265 exfat_error("'%s' is empty but marked as contiguous (%#x)", buffer,
270 /* Directory size must be aligned on at cluster boundary. */
271 if ((node->flags & EXFAT_ATTRIB_DIR) && node->size % cluster_size != 0)
273 exfat_get_name(node, buffer, sizeof(buffer) - 1);
274 exfat_error("'%s' directory size %"PRIu64" is not divisible by %d", buffer,
275 node->size, cluster_size);
282 static void decompress_upcase(uint16_t* output, const le16_t* source,
288 for (oi = 0; oi < EXFAT_UPCASE_CHARS; oi++)
291 for (si = 0, oi = 0; si < size && oi < EXFAT_UPCASE_CHARS; si++)
293 uint16_t ch = le16_to_cpu(source[si]);
295 if (ch == 0xffff && si + 1 < size) /* indicates a run */
296 oi += le16_to_cpu(source[++si]);
303 * Reads one entry in directory at position pointed by iterator and fills
306 static int readdir(struct exfat* ef, const struct exfat_node* parent,
307 struct exfat_node** node, struct iterator* it)
310 const struct exfat_entry* entry;
311 const struct exfat_entry_meta1* meta1;
312 const struct exfat_entry_meta2* meta2;
313 const struct exfat_entry_name* file_name;
314 const struct exfat_entry_upcase* upcase;
315 const struct exfat_entry_bitmap* bitmap;
316 const struct exfat_entry_label* label;
317 uint8_t continuations = 0;
318 le16_t* namep = NULL;
319 uint16_t reference_checksum = 0;
320 uint16_t actual_checksum = 0;
321 uint64_t valid_size = 0;
322 uint64_t upcase_size = 0;
323 le16_t* upcase_comp = NULL;
329 if (it->offset >= parent->size)
331 if (continuations != 0)
333 exfat_error("expected %hhu continuations", continuations);
336 return -ENOENT; /* that's OK, means end of directory */
339 entry = get_entry_ptr(ef, it);
342 case EXFAT_ENTRY_FILE:
343 if (continuations != 0)
345 exfat_error("expected %hhu continuations before new entry",
349 meta1 = (const struct exfat_entry_meta1*) entry;
350 continuations = meta1->continuations;
351 /* each file entry must have at least 2 continuations:
353 if (continuations < 2)
355 exfat_error("too few continuations (%hhu)", continuations);
358 if (continuations > 1 +
359 DIV_ROUND_UP(EXFAT_NAME_MAX, EXFAT_ENAME_MAX))
361 exfat_error("too many continuations (%hhu)", continuations);
364 reference_checksum = le16_to_cpu(meta1->checksum);
365 actual_checksum = exfat_start_checksum(meta1);
366 *node = allocate_node();
372 /* new node has zero reference counter */
373 (*node)->entry_cluster = it->cluster;
374 (*node)->entry_offset = it->offset;
375 init_node_meta1(*node, meta1);
376 namep = (*node)->name;
379 case EXFAT_ENTRY_FILE_INFO:
380 if (continuations < 2)
382 exfat_error("unexpected continuation (%hhu)",
386 meta2 = (const struct exfat_entry_meta2*) entry;
387 if (meta2->flags & ~(EXFAT_FLAG_ALWAYS1 | EXFAT_FLAG_CONTIGUOUS))
389 exfat_error("unknown flags in meta2 (0x%hhx)", meta2->flags);
392 init_node_meta2(*node, meta2);
393 actual_checksum = exfat_add_checksum(entry, actual_checksum);
394 valid_size = le64_to_cpu(meta2->valid_size);
398 case EXFAT_ENTRY_FILE_NAME:
399 if (continuations == 0)
401 exfat_error("unexpected continuation");
404 file_name = (const struct exfat_entry_name*) entry;
405 actual_checksum = exfat_add_checksum(entry, actual_checksum);
407 memcpy(namep, file_name->name,
409 ((*node)->name + EXFAT_NAME_MAX - namep)) *
411 namep += EXFAT_ENAME_MAX;
412 if (--continuations == 0)
414 if (!check_node(*node, actual_checksum, reference_checksum,
415 valid_size, CLUSTER_SIZE(*ef->sb)))
417 if (!fetch_next_entry(ef, parent, it))
419 return 0; /* entry completed */
423 case EXFAT_ENTRY_UPCASE:
424 if (ef->upcase != NULL)
426 upcase = (const struct exfat_entry_upcase*) entry;
427 if (CLUSTER_INVALID(le32_to_cpu(upcase->start_cluster)))
429 exfat_error("invalid cluster 0x%x in upcase table",
430 le32_to_cpu(upcase->start_cluster));
433 upcase_size = le64_to_cpu(upcase->size);
434 if (upcase_size == 0 ||
435 upcase_size > EXFAT_UPCASE_CHARS * sizeof(uint16_t) ||
436 upcase_size % sizeof(uint16_t) != 0)
438 exfat_error("bad upcase table size (%"PRIu64" bytes)",
442 upcase_comp = malloc(upcase_size);
443 if (upcase_comp == NULL)
445 exfat_error("failed to allocate upcase table (%"PRIu64" bytes)",
451 /* read compressed upcase table */
452 if (exfat_pread(ef->dev, upcase_comp, upcase_size,
453 exfat_c2o(ef, le32_to_cpu(upcase->start_cluster))) < 0)
456 exfat_error("failed to read upper case table "
457 "(%"PRIu64" bytes starting at cluster %#x)",
459 le32_to_cpu(upcase->start_cluster));
463 /* decompress upcase table */
464 ef->upcase = calloc(EXFAT_UPCASE_CHARS, sizeof(uint16_t));
465 if (ef->upcase == NULL)
468 exfat_error("failed to allocate decompressed upcase table");
472 decompress_upcase(ef->upcase, upcase_comp,
473 upcase_size / sizeof(uint16_t));
477 case EXFAT_ENTRY_BITMAP:
478 bitmap = (const struct exfat_entry_bitmap*) entry;
479 ef->cmap.start_cluster = le32_to_cpu(bitmap->start_cluster);
480 if (CLUSTER_INVALID(ef->cmap.start_cluster))
482 exfat_error("invalid cluster 0x%x in clusters bitmap",
483 ef->cmap.start_cluster);
486 ef->cmap.size = le32_to_cpu(ef->sb->cluster_count) -
487 EXFAT_FIRST_DATA_CLUSTER;
488 if (le64_to_cpu(bitmap->size) < DIV_ROUND_UP(ef->cmap.size, 8))
490 exfat_error("invalid clusters bitmap size: %"PRIu64
491 " (expected at least %u)",
492 le64_to_cpu(bitmap->size),
493 DIV_ROUND_UP(ef->cmap.size, 8));
496 /* FIXME bitmap can be rather big, up to 512 MB */
497 ef->cmap.chunk_size = ef->cmap.size;
498 ef->cmap.chunk = malloc(BMAP_SIZE(ef->cmap.chunk_size));
499 if (ef->cmap.chunk == NULL)
501 exfat_error("failed to allocate clusters bitmap chunk "
502 "(%"PRIu64" bytes)", le64_to_cpu(bitmap->size));
507 if (exfat_pread(ef->dev, ef->cmap.chunk,
508 BMAP_SIZE(ef->cmap.chunk_size),
509 exfat_c2o(ef, ef->cmap.start_cluster)) < 0)
511 exfat_error("failed to read clusters bitmap "
512 "(%"PRIu64" bytes starting at cluster %#x)",
513 le64_to_cpu(bitmap->size), ef->cmap.start_cluster);
518 case EXFAT_ENTRY_LABEL:
519 label = (const struct exfat_entry_label*) entry;
520 if (label->length > EXFAT_ENAME_MAX)
522 exfat_error("too long label (%hhu chars)", label->length);
525 if (utf16_to_utf8(ef->label, label->name,
526 sizeof(ef->label) - 1, EXFAT_ENAME_MAX) != 0)
531 if (!(entry->type & EXFAT_ENTRY_VALID))
532 break; /* deleted entry, ignore it */
533 if (!(entry->type & EXFAT_ENTRY_OPTIONAL))
535 exfat_error("unknown entry type %#hhx", entry->type);
538 /* optional entry, warn and skip */
539 exfat_warn("unknown entry type %#hhx", entry->type);
540 if (continuations == 0)
542 exfat_error("unexpected continuation");
549 if (!fetch_next_entry(ef, parent, it))
552 /* we never reach here */
560 int exfat_cache_directory(struct exfat* ef, struct exfat_node* dir)
564 struct exfat_node* node;
565 struct exfat_node* current = NULL;
567 if (dir->flags & EXFAT_ATTRIB_CACHED)
568 return 0; /* already cached */
570 rc = opendir(ef, dir, &it);
573 while ((rc = readdir(ef, dir, &node, &it)) == 0)
578 current->next = node;
579 node->prev = current;
591 for (current = dir->child; current; current = node)
593 node = current->next;
600 dir->flags |= EXFAT_ATTRIB_CACHED;
604 static void tree_attach(struct exfat_node* dir, struct exfat_node* node)
609 dir->child->prev = node;
610 node->next = dir->child;
615 static void tree_detach(struct exfat_node* node)
618 node->prev->next = node->next;
619 else /* this is the first node in the list */
620 node->parent->child = node->next;
622 node->next->prev = node->prev;
628 static void reset_cache(struct exfat* ef, struct exfat_node* node)
630 char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
634 struct exfat_node* p = node->child;
639 node->flags &= ~EXFAT_ATTRIB_CACHED;
640 if (node->references != 0)
642 exfat_get_name(node, buffer, sizeof(buffer) - 1);
643 exfat_warn("non-zero reference counter (%d) for '%s'",
644 node->references, buffer);
646 if (node != ef->root && (node->flags & EXFAT_ATTRIB_DIRTY))
648 exfat_get_name(node, buffer, sizeof(buffer) - 1);
649 exfat_bug("node '%s' is dirty", buffer);
651 while (node->references)
652 exfat_put_node(ef, node);
655 void exfat_reset_cache(struct exfat* ef)
657 reset_cache(ef, ef->root);
660 static bool next_entry(struct exfat* ef, const struct exfat_node* parent,
661 cluster_t* cluster, off_t* offset)
663 *offset += sizeof(struct exfat_entry);
664 if (*offset % CLUSTER_SIZE(*ef->sb) == 0)
666 *cluster = exfat_next_cluster(ef, parent, *cluster);
667 if (CLUSTER_INVALID(*cluster))
669 exfat_error("invalid cluster %#x while getting next entry",
677 int exfat_flush_node(struct exfat* ef, struct exfat_node* node)
681 off_t meta1_offset, meta2_offset;
682 struct exfat_entry_meta1 meta1;
683 struct exfat_entry_meta2 meta2;
685 if (!(node->flags & EXFAT_ATTRIB_DIRTY))
686 return 0; /* no need to flush */
689 exfat_bug("unable to flush node to read-only FS");
691 if (node->parent == NULL)
692 return 0; /* do not flush unlinked node */
694 cluster = node->entry_cluster;
695 offset = node->entry_offset;
696 meta1_offset = co2o(ef, cluster, offset);
697 if (!next_entry(ef, node->parent, &cluster, &offset))
699 meta2_offset = co2o(ef, cluster, offset);
701 if (exfat_pread(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
703 exfat_error("failed to read meta1 entry on flush");
706 if (meta1.type != EXFAT_ENTRY_FILE)
707 exfat_bug("invalid type of meta1: 0x%hhx", meta1.type);
708 meta1.attrib = cpu_to_le16(node->flags);
709 exfat_unix2exfat(node->mtime, &meta1.mdate, &meta1.mtime, &meta1.mtime_cs);
710 exfat_unix2exfat(node->atime, &meta1.adate, &meta1.atime, NULL);
712 if (exfat_pread(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
714 exfat_error("failed to read meta2 entry on flush");
717 if (meta2.type != EXFAT_ENTRY_FILE_INFO)
718 exfat_bug("invalid type of meta2: 0x%hhx", meta2.type);
719 meta2.size = meta2.valid_size = cpu_to_le64(node->size);
720 meta2.start_cluster = cpu_to_le32(node->start_cluster);
721 meta2.flags = EXFAT_FLAG_ALWAYS1;
722 /* empty files must not be marked as contiguous */
723 if (node->size != 0 && IS_CONTIGUOUS(*node))
724 meta2.flags |= EXFAT_FLAG_CONTIGUOUS;
725 /* name hash remains unchanged, no need to recalculate it */
727 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);
729 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
731 exfat_error("failed to write meta1 entry on flush");
734 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
736 exfat_error("failed to write meta2 entry on flush");
740 node->flags &= ~EXFAT_ATTRIB_DIRTY;
741 return exfat_flush(ef);
744 static bool erase_entry(struct exfat* ef, struct exfat_node* node)
746 cluster_t cluster = node->entry_cluster;
747 off_t offset = node->entry_offset;
748 int name_entries = DIV_ROUND_UP(utf16_length(node->name), EXFAT_ENAME_MAX);
751 entry_type = EXFAT_ENTRY_FILE & ~EXFAT_ENTRY_VALID;
752 if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
754 exfat_error("failed to erase meta1 entry");
758 if (!next_entry(ef, node->parent, &cluster, &offset))
760 entry_type = EXFAT_ENTRY_FILE_INFO & ~EXFAT_ENTRY_VALID;
761 if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
763 exfat_error("failed to erase meta2 entry");
767 while (name_entries--)
769 if (!next_entry(ef, node->parent, &cluster, &offset))
771 entry_type = EXFAT_ENTRY_FILE_NAME & ~EXFAT_ENTRY_VALID;
772 if (exfat_pwrite(ef->dev, &entry_type, 1,
773 co2o(ef, cluster, offset)) < 0)
775 exfat_error("failed to erase name entry");
782 static int shrink_directory(struct exfat* ef, struct exfat_node* dir,
783 off_t deleted_offset)
785 const struct exfat_node* node;
786 const struct exfat_node* last_node;
787 uint64_t entries = 0;
790 if (!(dir->flags & EXFAT_ATTRIB_DIR))
791 exfat_bug("attempted to shrink a file");
792 if (!(dir->flags & EXFAT_ATTRIB_CACHED))
793 exfat_bug("attempted to shrink uncached directory");
795 for (last_node = node = dir->child; node; node = node->next)
797 if (deleted_offset < node->entry_offset)
799 /* there are other entries after the removed one, no way to shrink
803 if (last_node->entry_offset < node->entry_offset)
809 /* offset of the last entry */
810 entries += last_node->entry_offset / sizeof(struct exfat_entry);
811 /* two subentries with meta info */
813 /* subentries with file name */
814 entries += DIV_ROUND_UP(utf16_length(last_node->name),
818 new_size = DIV_ROUND_UP(entries * sizeof(struct exfat_entry),
819 CLUSTER_SIZE(*ef->sb)) * CLUSTER_SIZE(*ef->sb);
820 if (new_size == 0) /* directory always has at least 1 cluster */
821 new_size = CLUSTER_SIZE(*ef->sb);
822 if (new_size == dir->size)
824 return exfat_truncate(ef, dir, new_size, true);
827 static int delete(struct exfat* ef, struct exfat_node* node)
829 struct exfat_node* parent = node->parent;
830 off_t deleted_offset = node->entry_offset;
833 exfat_get_node(parent);
834 if (!erase_entry(ef, node))
836 exfat_put_node(ef, parent);
839 exfat_update_mtime(parent);
841 rc = shrink_directory(ef, parent, deleted_offset);
842 node->flags |= EXFAT_ATTRIB_UNLINKED;
845 exfat_flush_node(ef, parent);
846 exfat_put_node(ef, parent);
849 rc = exfat_flush_node(ef, parent);
850 exfat_put_node(ef, parent);
854 int exfat_unlink(struct exfat* ef, struct exfat_node* node)
856 if (node->flags & EXFAT_ATTRIB_DIR)
858 return delete(ef, node);
861 int exfat_rmdir(struct exfat* ef, struct exfat_node* node)
865 if (!(node->flags & EXFAT_ATTRIB_DIR))
867 /* check that directory is empty */
868 rc = exfat_cache_directory(ef, node);
873 return delete(ef, node);
876 static int grow_directory(struct exfat* ef, struct exfat_node* dir,
877 uint64_t asize, uint32_t difference)
879 return exfat_truncate(ef, dir,
880 DIV_ROUND_UP(asize + difference, CLUSTER_SIZE(*ef->sb))
881 * CLUSTER_SIZE(*ef->sb), true);
884 static int find_slot(struct exfat* ef, struct exfat_node* dir,
885 cluster_t* cluster, off_t* offset, int subentries)
889 const struct exfat_entry* entry;
892 rc = opendir(ef, dir, &it);
899 *cluster = it.cluster;
902 entry = get_entry_ptr(ef, &it);
903 if (entry->type & EXFAT_ENTRY_VALID)
907 if (contiguous == subentries)
908 break; /* suitable slot is found */
909 if (it.offset + sizeof(struct exfat_entry) >= dir->size)
911 rc = grow_directory(ef, dir, dir->size,
912 (subentries - contiguous) * sizeof(struct exfat_entry));
919 if (!fetch_next_entry(ef, dir, &it))
929 static int write_entry(struct exfat* ef, struct exfat_node* dir,
930 const le16_t* name, cluster_t cluster, off_t offset, uint16_t attrib)
932 struct exfat_node* node;
933 struct exfat_entry_meta1 meta1;
934 struct exfat_entry_meta2 meta2;
935 const size_t name_length = utf16_length(name);
936 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
939 node = allocate_node();
942 node->entry_cluster = cluster;
943 node->entry_offset = offset;
944 memcpy(node->name, name, name_length * sizeof(le16_t));
946 memset(&meta1, 0, sizeof(meta1));
947 meta1.type = EXFAT_ENTRY_FILE;
948 meta1.continuations = 1 + name_entries;
949 meta1.attrib = cpu_to_le16(attrib);
950 exfat_unix2exfat(time(NULL), &meta1.crdate, &meta1.crtime,
952 meta1.adate = meta1.mdate = meta1.crdate;
953 meta1.atime = meta1.mtime = meta1.crtime;
954 meta1.mtime_cs = meta1.crtime_cs; /* there is no atime_cs */
956 memset(&meta2, 0, sizeof(meta2));
957 meta2.type = EXFAT_ENTRY_FILE_INFO;
958 meta2.flags = EXFAT_FLAG_ALWAYS1;
959 meta2.name_length = name_length;
960 meta2.name_hash = exfat_calc_name_hash(ef, node->name);
961 meta2.start_cluster = cpu_to_le32(EXFAT_CLUSTER_FREE);
963 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);
965 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
966 co2o(ef, cluster, offset)) < 0)
968 exfat_error("failed to write meta1 entry");
971 if (!next_entry(ef, dir, &cluster, &offset))
973 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
974 co2o(ef, cluster, offset)) < 0)
976 exfat_error("failed to write meta2 entry");
979 for (i = 0; i < name_entries; i++)
981 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
982 memcpy(name_entry.name, node->name + i * EXFAT_ENAME_MAX,
983 MIN(EXFAT_ENAME_MAX, EXFAT_NAME_MAX - i * EXFAT_ENAME_MAX) *
985 if (!next_entry(ef, dir, &cluster, &offset))
987 if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
988 co2o(ef, cluster, offset)) < 0)
990 exfat_error("failed to write name entry");
995 init_node_meta1(node, &meta1);
996 init_node_meta2(node, &meta2);
998 tree_attach(dir, node);
999 exfat_update_mtime(dir);
1003 static int create(struct exfat* ef, const char* path, uint16_t attrib)
1005 struct exfat_node* dir;
1006 struct exfat_node* existing;
1007 cluster_t cluster = EXFAT_CLUSTER_BAD;
1009 le16_t name[EXFAT_NAME_MAX + 1];
1012 rc = exfat_split(ef, &dir, &existing, name, path);
1015 if (existing != NULL)
1017 exfat_put_node(ef, existing);
1018 exfat_put_node(ef, dir);
1022 rc = find_slot(ef, dir, &cluster, &offset,
1023 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
1026 exfat_put_node(ef, dir);
1029 rc = write_entry(ef, dir, name, cluster, offset, attrib);
1032 exfat_put_node(ef, dir);
1035 rc = exfat_flush_node(ef, dir);
1036 exfat_put_node(ef, dir);
1040 int exfat_mknod(struct exfat* ef, const char* path)
1042 return create(ef, path, EXFAT_ATTRIB_ARCH);
1045 int exfat_mkdir(struct exfat* ef, const char* path)
1048 struct exfat_node* node;
1050 rc = create(ef, path, EXFAT_ATTRIB_DIR);
1053 rc = exfat_lookup(ef, &node, path);
1056 /* directories always have at least one cluster */
1057 rc = exfat_truncate(ef, node, CLUSTER_SIZE(*ef->sb), true);
1061 exfat_put_node(ef, node);
1064 rc = exfat_flush_node(ef, node);
1068 exfat_put_node(ef, node);
1071 exfat_put_node(ef, node);
1075 static int rename_entry(struct exfat* ef, struct exfat_node* dir,
1076 struct exfat_node* node, const le16_t* name, cluster_t new_cluster,
1079 struct exfat_entry_meta1 meta1;
1080 struct exfat_entry_meta2 meta2;
1081 cluster_t old_cluster = node->entry_cluster;
1082 off_t old_offset = node->entry_offset;
1083 const size_t name_length = utf16_length(name);
1084 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
1087 if (exfat_pread(ef->dev, &meta1, sizeof(meta1),
1088 co2o(ef, old_cluster, old_offset)) < 0)
1090 exfat_error("failed to read meta1 entry on rename");
1093 if (!next_entry(ef, node->parent, &old_cluster, &old_offset))
1095 if (exfat_pread(ef->dev, &meta2, sizeof(meta2),
1096 co2o(ef, old_cluster, old_offset)) < 0)
1098 exfat_error("failed to read meta2 entry on rename");
1101 meta1.continuations = 1 + name_entries;
1102 meta2.name_hash = exfat_calc_name_hash(ef, name);
1103 meta2.name_length = name_length;
1104 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, name);
1106 if (!erase_entry(ef, node))
1109 node->entry_cluster = new_cluster;
1110 node->entry_offset = new_offset;
1112 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
1113 co2o(ef, new_cluster, new_offset)) < 0)
1115 exfat_error("failed to write meta1 entry on rename");
1118 if (!next_entry(ef, dir, &new_cluster, &new_offset))
1120 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
1121 co2o(ef, new_cluster, new_offset)) < 0)
1123 exfat_error("failed to write meta2 entry on rename");
1127 for (i = 0; i < name_entries; i++)
1129 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
1130 memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX,
1131 EXFAT_ENAME_MAX * sizeof(le16_t));
1132 if (!next_entry(ef, dir, &new_cluster, &new_offset))
1134 if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
1135 co2o(ef, new_cluster, new_offset)) < 0)
1137 exfat_error("failed to write name entry on rename");
1142 memcpy(node->name, name, (EXFAT_NAME_MAX + 1) * sizeof(le16_t));
1144 tree_attach(dir, node);
1148 int exfat_rename(struct exfat* ef, const char* old_path, const char* new_path)
1150 struct exfat_node* node;
1151 struct exfat_node* existing;
1152 struct exfat_node* dir;
1153 cluster_t cluster = EXFAT_CLUSTER_BAD;
1155 le16_t name[EXFAT_NAME_MAX + 1];
1158 rc = exfat_lookup(ef, &node, old_path);
1162 rc = exfat_split(ef, &dir, &existing, name, new_path);
1165 exfat_put_node(ef, node);
1169 /* check that target is not a subdirectory of the source */
1170 if (node->flags & EXFAT_ATTRIB_DIR)
1172 struct exfat_node* p;
1174 for (p = dir; p; p = p->parent)
1177 if (existing != NULL)
1178 exfat_put_node(ef, existing);
1179 exfat_put_node(ef, dir);
1180 exfat_put_node(ef, node);
1185 if (existing != NULL)
1187 /* remove target if it's not the same node as source */
1188 if (existing != node)
1190 if (existing->flags & EXFAT_ATTRIB_DIR)
1192 if (node->flags & EXFAT_ATTRIB_DIR)
1193 rc = exfat_rmdir(ef, existing);
1199 if (!(node->flags & EXFAT_ATTRIB_DIR))
1200 rc = exfat_unlink(ef, existing);
1204 exfat_put_node(ef, existing);
1207 /* free clusters even if something went wrong; overwise they
1208 will be just lost */
1209 exfat_cleanup_node(ef, existing);
1210 exfat_put_node(ef, dir);
1211 exfat_put_node(ef, node);
1214 rc = exfat_cleanup_node(ef, existing);
1217 exfat_put_node(ef, dir);
1218 exfat_put_node(ef, node);
1223 exfat_put_node(ef, existing);
1226 rc = find_slot(ef, dir, &cluster, &offset,
1227 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
1230 exfat_put_node(ef, dir);
1231 exfat_put_node(ef, node);
1234 rc = rename_entry(ef, dir, node, name, cluster, offset);
1235 exfat_put_node(ef, dir);
1236 exfat_put_node(ef, node);
1240 void exfat_utimes(struct exfat_node* node, const struct timespec tv[2])
1242 node->atime = tv[0].tv_sec;
1243 node->mtime = tv[1].tv_sec;
1244 node->flags |= EXFAT_ATTRIB_DIRTY;
1247 void exfat_update_atime(struct exfat_node* node)
1249 node->atime = time(NULL);
1250 node->flags |= EXFAT_ATTRIB_DIRTY;
1253 void exfat_update_mtime(struct exfat_node* node)
1255 node->mtime = time(NULL);
1256 node->flags |= EXFAT_ATTRIB_DIRTY;
1259 const char* exfat_get_label(struct exfat* ef)
1264 static int find_label(struct exfat* ef, cluster_t* cluster, off_t* offset)
1269 rc = opendir(ef, ef->root, &it);
1275 if (it.offset >= ef->root->size)
1281 if (get_entry_ptr(ef, &it)->type == EXFAT_ENTRY_LABEL)
1283 *cluster = it.cluster;
1284 *offset = it.offset;
1289 if (!fetch_next_entry(ef, ef->root, &it))
1297 int exfat_set_label(struct exfat* ef, const char* label)
1299 le16_t label_utf16[EXFAT_ENAME_MAX + 1];
1303 struct exfat_entry_label entry;
1305 memset(label_utf16, 0, sizeof(label_utf16));
1306 rc = utf8_to_utf16(label_utf16, label, EXFAT_ENAME_MAX, strlen(label));
1310 rc = find_label(ef, &cluster, &offset);
1312 rc = find_slot(ef, ef->root, &cluster, &offset, 1);
1316 entry.type = EXFAT_ENTRY_LABEL;
1317 entry.length = utf16_length(label_utf16);
1318 memcpy(entry.name, label_utf16, sizeof(entry.name));
1319 if (entry.length == 0)
1320 entry.type ^= EXFAT_ENTRY_VALID;
1322 if (exfat_pwrite(ef->dev, &entry, sizeof(struct exfat_entry_label),
1323 co2o(ef, cluster, offset)) < 0)
1325 exfat_error("failed to write label entry");
1328 strcpy(ef->label, label);