3 exFAT file system implementation library.
5 Free exFAT implementation.
6 Copyright (C) 2010-2016 Andrew Nayenko
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 /* on-disk nodes iterator */
36 struct exfat_node* exfat_get_node(struct exfat_node* node)
38 /* if we switch to multi-threaded mode we will need atomic
39 increment here and atomic decrement in exfat_put_node() */
44 void exfat_put_node(struct exfat* ef, struct exfat_node* node)
46 char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
49 if (node->references < 0)
51 exfat_get_name(node, buffer, sizeof(buffer) - 1);
52 exfat_bug("reference counter of '%s' is below zero", buffer);
54 else if (node->references == 0 && node != ef->root)
56 if (node->flags & EXFAT_ATTRIB_DIRTY)
58 exfat_get_name(node, buffer, sizeof(buffer) - 1);
59 exfat_warn("dirty node '%s' with zero references", buffer);
65 * This function must be called on rmdir and unlink (after the last
66 * exfat_put_node()) to free clusters.
68 int exfat_cleanup_node(struct exfat* ef, struct exfat_node* node)
72 if (node->references != 0)
73 exfat_bug("unable to cleanup a node with %d references",
76 if (node->flags & EXFAT_ATTRIB_UNLINKED)
78 /* free all clusters and node structure itself */
79 rc = exfat_truncate(ef, node, 0, true);
80 /* free the node even in case of error or its memory will be lost */
87 * Cluster + offset from the beginning of the directory to absolute offset.
89 static off_t co2o(struct exfat* ef, cluster_t cluster, off_t offset)
91 return exfat_c2o(ef, cluster) + offset % CLUSTER_SIZE(*ef->sb);
94 static int opendir(struct exfat* ef, const struct exfat_node* dir,
97 char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
99 if (!(dir->flags & EXFAT_ATTRIB_DIR))
101 exfat_get_name(dir, buffer, sizeof(buffer) - 1);
102 exfat_bug("'%s' is not a directory", buffer);
104 it->cluster = dir->start_cluster;
106 it->chunk = malloc(CLUSTER_SIZE(*ef->sb));
107 if (it->chunk == NULL)
109 exfat_error("failed to allocate memory for directory cluster");
112 if (exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb),
113 exfat_c2o(ef, it->cluster)) < 0)
116 exfat_get_name(dir, buffer, sizeof(buffer) - 1);
117 exfat_error("failed to read '%s' directory cluster %#x", buffer,
124 static void closedir(struct iterator* it)
132 static bool fetch_next_entry(struct exfat* ef, const struct exfat_node* parent,
135 /* move iterator to the next entry in the directory */
136 it->offset += sizeof(struct exfat_entry);
137 /* fetch the next cluster if needed */
138 if ((it->offset & (CLUSTER_SIZE(*ef->sb) - 1)) == 0)
140 /* reached the end of directory; the caller should check this
142 if (it->offset >= parent->size)
144 it->cluster = exfat_next_cluster(ef, parent, it->cluster);
145 if (CLUSTER_INVALID(it->cluster))
147 exfat_error("invalid cluster 0x%x while reading directory",
151 if (exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb),
152 exfat_c2o(ef, it->cluster)) < 0)
154 exfat_error("failed to read the next directory cluster %#x",
162 static struct exfat_node* allocate_node(void)
164 struct exfat_node* node = malloc(sizeof(struct exfat_node));
167 exfat_error("failed to allocate node");
170 memset(node, 0, sizeof(struct exfat_node));
174 static void init_node_meta1(struct exfat_node* node,
175 const struct exfat_entry_meta1* meta1)
177 node->flags = le16_to_cpu(meta1->attrib);
178 node->mtime = exfat_exfat2unix(meta1->mdate, meta1->mtime,
180 /* there is no centiseconds field for atime */
181 node->atime = exfat_exfat2unix(meta1->adate, meta1->atime, 0);
184 static void init_node_meta2(struct exfat_node* node,
185 const struct exfat_entry_meta2* meta2)
187 node->size = le64_to_cpu(meta2->size);
188 node->start_cluster = le32_to_cpu(meta2->start_cluster);
189 node->fptr_cluster = node->start_cluster;
190 if (meta2->flags & EXFAT_FLAG_CONTIGUOUS)
191 node->flags |= EXFAT_ATTRIB_CONTIGUOUS;
194 static const struct exfat_entry* get_entry_ptr(const struct exfat* ef,
195 const struct iterator* it)
197 return (const struct exfat_entry*)
198 (it->chunk + it->offset % CLUSTER_SIZE(*ef->sb));
201 static bool check_node(const struct exfat_node* node, uint16_t actual_checksum,
202 uint16_t reference_checksum, uint64_t valid_size)
204 char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
207 Validate checksum first. If it's invalid all other fields probably
208 contain just garbage.
210 if (actual_checksum != reference_checksum)
212 exfat_get_name(node, buffer, sizeof(buffer) - 1);
213 exfat_error("'%s' has invalid checksum (%#hx != %#hx)", buffer,
214 actual_checksum, reference_checksum);
219 exFAT does not support sparse files but allows files with uninitialized
220 clusters. For such files valid_size means initialized data size and
221 cannot be greater than file size. See SetFileValidData() function
224 if (valid_size > node->size)
226 exfat_get_name(node, buffer, sizeof(buffer) - 1);
227 exfat_error("'%s' has valid size (%"PRIu64") greater than size "
228 "(%"PRIu64")", buffer, valid_size, node->size);
235 static void decompress_upcase(uint16_t* output, const le16_t* source,
241 for (oi = 0; oi < EXFAT_UPCASE_CHARS; oi++)
244 for (si = 0, oi = 0; si < size && oi < EXFAT_UPCASE_CHARS; si++)
246 uint16_t ch = le16_to_cpu(source[si]);
248 if (ch == 0xffff && si + 1 < size) /* indicates a run */
249 oi += le16_to_cpu(source[++si]);
256 * Reads one entry in directory at position pointed by iterator and fills
259 static int readdir(struct exfat* ef, const struct exfat_node* parent,
260 struct exfat_node** node, struct iterator* it)
263 const struct exfat_entry* entry;
264 const struct exfat_entry_meta1* meta1;
265 const struct exfat_entry_meta2* meta2;
266 const struct exfat_entry_name* file_name;
267 const struct exfat_entry_upcase* upcase;
268 const struct exfat_entry_bitmap* bitmap;
269 const struct exfat_entry_label* label;
270 uint8_t continuations = 0;
271 le16_t* namep = NULL;
272 uint16_t reference_checksum = 0;
273 uint16_t actual_checksum = 0;
274 uint64_t valid_size = 0;
275 uint64_t upcase_size = 0;
276 le16_t* upcase_comp = NULL;
282 if (it->offset >= parent->size)
284 if (continuations != 0)
286 exfat_error("expected %hhu continuations", continuations);
289 return -ENOENT; /* that's OK, means end of directory */
292 entry = get_entry_ptr(ef, it);
295 case EXFAT_ENTRY_FILE:
296 if (continuations != 0)
298 exfat_error("expected %hhu continuations before new entry",
302 meta1 = (const struct exfat_entry_meta1*) entry;
303 continuations = meta1->continuations;
304 /* each file entry must have at least 2 continuations:
306 if (continuations < 2)
308 exfat_error("too few continuations (%hhu)", continuations);
311 if (continuations > 1 +
312 DIV_ROUND_UP(EXFAT_NAME_MAX, EXFAT_ENAME_MAX))
314 exfat_error("too many continuations (%hhu)", continuations);
317 reference_checksum = le16_to_cpu(meta1->checksum);
318 actual_checksum = exfat_start_checksum(meta1);
319 *node = allocate_node();
325 /* new node has zero reference counter */
326 (*node)->entry_cluster = it->cluster;
327 (*node)->entry_offset = it->offset;
328 init_node_meta1(*node, meta1);
329 namep = (*node)->name;
332 case EXFAT_ENTRY_FILE_INFO:
333 if (continuations < 2)
335 exfat_error("unexpected continuation (%hhu)",
339 meta2 = (const struct exfat_entry_meta2*) entry;
340 if (meta2->flags & ~(EXFAT_FLAG_ALWAYS1 | EXFAT_FLAG_CONTIGUOUS))
342 exfat_error("unknown flags in meta2 (0x%hhx)", meta2->flags);
345 init_node_meta2(*node, meta2);
346 actual_checksum = exfat_add_checksum(entry, actual_checksum);
347 valid_size = le64_to_cpu(meta2->valid_size);
348 /* empty files must be marked as non-contiguous */
349 if ((*node)->size == 0 && (meta2->flags & EXFAT_FLAG_CONTIGUOUS))
351 exfat_error("empty file marked as contiguous (0x%hhx)",
355 /* directories must be aligned on at cluster boundary */
356 if (((*node)->flags & EXFAT_ATTRIB_DIR) &&
357 (*node)->size % CLUSTER_SIZE(*ef->sb) != 0)
359 exfat_error("directory has invalid size %"PRIu64" bytes",
366 case EXFAT_ENTRY_FILE_NAME:
367 if (continuations == 0)
369 exfat_error("unexpected continuation");
372 file_name = (const struct exfat_entry_name*) entry;
373 actual_checksum = exfat_add_checksum(entry, actual_checksum);
375 memcpy(namep, file_name->name,
377 ((*node)->name + EXFAT_NAME_MAX - namep)) *
379 namep += EXFAT_ENAME_MAX;
380 if (--continuations == 0)
382 if (!check_node(*node, actual_checksum, reference_checksum,
385 if (!fetch_next_entry(ef, parent, it))
387 return 0; /* entry completed */
391 case EXFAT_ENTRY_UPCASE:
392 if (ef->upcase != NULL)
394 upcase = (const struct exfat_entry_upcase*) entry;
395 if (CLUSTER_INVALID(le32_to_cpu(upcase->start_cluster)))
397 exfat_error("invalid cluster 0x%x in upcase table",
398 le32_to_cpu(upcase->start_cluster));
401 upcase_size = le64_to_cpu(upcase->size);
402 if (upcase_size == 0 ||
403 upcase_size > EXFAT_UPCASE_CHARS * sizeof(uint16_t) ||
404 upcase_size % sizeof(uint16_t) != 0)
406 exfat_error("bad upcase table size (%"PRIu64" bytes)",
410 upcase_comp = malloc(upcase_size);
411 if (upcase_comp == NULL)
413 exfat_error("failed to allocate upcase table (%"PRIu64" bytes)",
419 /* read compressed upcase table */
420 if (exfat_pread(ef->dev, upcase_comp, upcase_size,
421 exfat_c2o(ef, le32_to_cpu(upcase->start_cluster))) < 0)
424 exfat_error("failed to read upper case table "
425 "(%"PRIu64" bytes starting at cluster %#x)",
427 le32_to_cpu(upcase->start_cluster));
431 /* decompress upcase table */
432 ef->upcase = calloc(EXFAT_UPCASE_CHARS, sizeof(uint16_t));
433 if (ef->upcase == NULL)
436 exfat_error("failed to allocate decompressed upcase table");
440 decompress_upcase(ef->upcase, upcase_comp,
441 upcase_size / sizeof(uint16_t));
445 case EXFAT_ENTRY_BITMAP:
446 bitmap = (const struct exfat_entry_bitmap*) entry;
447 ef->cmap.start_cluster = le32_to_cpu(bitmap->start_cluster);
448 if (CLUSTER_INVALID(ef->cmap.start_cluster))
450 exfat_error("invalid cluster 0x%x in clusters bitmap",
451 ef->cmap.start_cluster);
454 ef->cmap.size = le32_to_cpu(ef->sb->cluster_count) -
455 EXFAT_FIRST_DATA_CLUSTER;
456 if (le64_to_cpu(bitmap->size) < DIV_ROUND_UP(ef->cmap.size, 8))
458 exfat_error("invalid clusters bitmap size: %"PRIu64
459 " (expected at least %u)",
460 le64_to_cpu(bitmap->size),
461 DIV_ROUND_UP(ef->cmap.size, 8));
464 /* FIXME bitmap can be rather big, up to 512 MB */
465 ef->cmap.chunk_size = ef->cmap.size;
466 ef->cmap.chunk = malloc(BMAP_SIZE(ef->cmap.chunk_size));
467 if (ef->cmap.chunk == NULL)
469 exfat_error("failed to allocate clusters bitmap chunk "
470 "(%"PRIu64" bytes)", le64_to_cpu(bitmap->size));
475 if (exfat_pread(ef->dev, ef->cmap.chunk,
476 BMAP_SIZE(ef->cmap.chunk_size),
477 exfat_c2o(ef, ef->cmap.start_cluster)) < 0)
479 exfat_error("failed to read clusters bitmap "
480 "(%"PRIu64" bytes starting at cluster %#x)",
481 le64_to_cpu(bitmap->size), ef->cmap.start_cluster);
486 case EXFAT_ENTRY_LABEL:
487 label = (const struct exfat_entry_label*) entry;
488 if (label->length > EXFAT_ENAME_MAX)
490 exfat_error("too long label (%hhu chars)", label->length);
493 if (utf16_to_utf8(ef->label, label->name,
494 sizeof(ef->label) - 1, EXFAT_ENAME_MAX) != 0)
499 if (!(entry->type & EXFAT_ENTRY_VALID))
500 break; /* deleted entry, ignore it */
501 if (!(entry->type & EXFAT_ENTRY_OPTIONAL))
503 exfat_error("unknown entry type %#hhx", entry->type);
506 /* optional entry, warn and skip */
507 exfat_warn("unknown entry type %#hhx", entry->type);
508 if (continuations == 0)
510 exfat_error("unexpected continuation");
517 if (!fetch_next_entry(ef, parent, it))
520 /* we never reach here */
528 int exfat_cache_directory(struct exfat* ef, struct exfat_node* dir)
532 struct exfat_node* node;
533 struct exfat_node* current = NULL;
535 if (dir->flags & EXFAT_ATTRIB_CACHED)
536 return 0; /* already cached */
538 rc = opendir(ef, dir, &it);
541 while ((rc = readdir(ef, dir, &node, &it)) == 0)
546 current->next = node;
547 node->prev = current;
559 for (current = dir->child; current; current = node)
561 node = current->next;
568 dir->flags |= EXFAT_ATTRIB_CACHED;
572 static void tree_attach(struct exfat_node* dir, struct exfat_node* node)
577 dir->child->prev = node;
578 node->next = dir->child;
583 static void tree_detach(struct exfat_node* node)
586 node->prev->next = node->next;
587 else /* this is the first node in the list */
588 node->parent->child = node->next;
590 node->next->prev = node->prev;
596 static void reset_cache(struct exfat* ef, struct exfat_node* node)
598 char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
602 struct exfat_node* p = node->child;
607 node->flags &= ~EXFAT_ATTRIB_CACHED;
608 if (node->references != 0)
610 exfat_get_name(node, buffer, sizeof(buffer) - 1);
611 exfat_warn("non-zero reference counter (%d) for '%s'",
612 node->references, buffer);
614 if (node != ef->root && (node->flags & EXFAT_ATTRIB_DIRTY))
616 exfat_get_name(node, buffer, sizeof(buffer) - 1);
617 exfat_bug("node '%s' is dirty", buffer);
619 while (node->references)
620 exfat_put_node(ef, node);
623 void exfat_reset_cache(struct exfat* ef)
625 reset_cache(ef, ef->root);
628 static bool next_entry(struct exfat* ef, const struct exfat_node* parent,
629 cluster_t* cluster, off_t* offset)
631 *offset += sizeof(struct exfat_entry);
632 if (*offset % CLUSTER_SIZE(*ef->sb) == 0)
634 *cluster = exfat_next_cluster(ef, parent, *cluster);
635 if (CLUSTER_INVALID(*cluster))
637 exfat_error("invalid cluster %#x while getting next entry",
645 int exfat_flush_node(struct exfat* ef, struct exfat_node* node)
649 off_t meta1_offset, meta2_offset;
650 struct exfat_entry_meta1 meta1;
651 struct exfat_entry_meta2 meta2;
653 if (!(node->flags & EXFAT_ATTRIB_DIRTY))
654 return 0; /* no need to flush */
657 exfat_bug("unable to flush node to read-only FS");
659 if (node->parent == NULL)
660 return 0; /* do not flush unlinked node */
662 cluster = node->entry_cluster;
663 offset = node->entry_offset;
664 meta1_offset = co2o(ef, cluster, offset);
665 if (!next_entry(ef, node->parent, &cluster, &offset))
667 meta2_offset = co2o(ef, cluster, offset);
669 if (exfat_pread(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
671 exfat_error("failed to read meta1 entry on flush");
674 if (meta1.type != EXFAT_ENTRY_FILE)
675 exfat_bug("invalid type of meta1: 0x%hhx", meta1.type);
676 meta1.attrib = cpu_to_le16(node->flags);
677 exfat_unix2exfat(node->mtime, &meta1.mdate, &meta1.mtime, &meta1.mtime_cs);
678 exfat_unix2exfat(node->atime, &meta1.adate, &meta1.atime, NULL);
680 if (exfat_pread(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
682 exfat_error("failed to read meta2 entry on flush");
685 if (meta2.type != EXFAT_ENTRY_FILE_INFO)
686 exfat_bug("invalid type of meta2: 0x%hhx", meta2.type);
687 meta2.size = meta2.valid_size = cpu_to_le64(node->size);
688 meta2.start_cluster = cpu_to_le32(node->start_cluster);
689 meta2.flags = EXFAT_FLAG_ALWAYS1;
690 /* empty files must not be marked as contiguous */
691 if (node->size != 0 && IS_CONTIGUOUS(*node))
692 meta2.flags |= EXFAT_FLAG_CONTIGUOUS;
693 /* name hash remains unchanged, no need to recalculate it */
695 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);
697 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
699 exfat_error("failed to write meta1 entry on flush");
702 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
704 exfat_error("failed to write meta2 entry on flush");
708 node->flags &= ~EXFAT_ATTRIB_DIRTY;
709 return exfat_flush(ef);
712 static bool erase_entry(struct exfat* ef, struct exfat_node* node)
714 cluster_t cluster = node->entry_cluster;
715 off_t offset = node->entry_offset;
716 int name_entries = DIV_ROUND_UP(utf16_length(node->name), EXFAT_ENAME_MAX);
719 entry_type = EXFAT_ENTRY_FILE & ~EXFAT_ENTRY_VALID;
720 if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
722 exfat_error("failed to erase meta1 entry");
726 if (!next_entry(ef, node->parent, &cluster, &offset))
728 entry_type = EXFAT_ENTRY_FILE_INFO & ~EXFAT_ENTRY_VALID;
729 if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
731 exfat_error("failed to erase meta2 entry");
735 while (name_entries--)
737 if (!next_entry(ef, node->parent, &cluster, &offset))
739 entry_type = EXFAT_ENTRY_FILE_NAME & ~EXFAT_ENTRY_VALID;
740 if (exfat_pwrite(ef->dev, &entry_type, 1,
741 co2o(ef, cluster, offset)) < 0)
743 exfat_error("failed to erase name entry");
750 static int shrink_directory(struct exfat* ef, struct exfat_node* dir,
751 off_t deleted_offset)
753 const struct exfat_node* node;
754 const struct exfat_node* last_node;
755 uint64_t entries = 0;
758 if (!(dir->flags & EXFAT_ATTRIB_DIR))
759 exfat_bug("attempted to shrink a file");
760 if (!(dir->flags & EXFAT_ATTRIB_CACHED))
761 exfat_bug("attempted to shrink uncached directory");
763 for (last_node = node = dir->child; node; node = node->next)
765 if (deleted_offset < node->entry_offset)
767 /* there are other entries after the removed one, no way to shrink
771 if (last_node->entry_offset < node->entry_offset)
777 /* offset of the last entry */
778 entries += last_node->entry_offset / sizeof(struct exfat_entry);
779 /* two subentries with meta info */
781 /* subentries with file name */
782 entries += DIV_ROUND_UP(utf16_length(last_node->name),
786 new_size = DIV_ROUND_UP(entries * sizeof(struct exfat_entry),
787 CLUSTER_SIZE(*ef->sb)) * CLUSTER_SIZE(*ef->sb);
788 if (new_size == 0) /* directory always has at least 1 cluster */
789 new_size = CLUSTER_SIZE(*ef->sb);
790 if (new_size == dir->size)
792 return exfat_truncate(ef, dir, new_size, true);
795 static int delete(struct exfat* ef, struct exfat_node* node)
797 struct exfat_node* parent = node->parent;
798 off_t deleted_offset = node->entry_offset;
801 exfat_get_node(parent);
802 if (!erase_entry(ef, node))
804 exfat_put_node(ef, parent);
807 exfat_update_mtime(parent);
809 rc = shrink_directory(ef, parent, deleted_offset);
810 node->flags |= EXFAT_ATTRIB_UNLINKED;
813 exfat_flush_node(ef, parent);
814 exfat_put_node(ef, parent);
817 rc = exfat_flush_node(ef, parent);
818 exfat_put_node(ef, parent);
822 int exfat_unlink(struct exfat* ef, struct exfat_node* node)
824 if (node->flags & EXFAT_ATTRIB_DIR)
826 return delete(ef, node);
829 int exfat_rmdir(struct exfat* ef, struct exfat_node* node)
833 if (!(node->flags & EXFAT_ATTRIB_DIR))
835 /* check that directory is empty */
836 rc = exfat_cache_directory(ef, node);
841 return delete(ef, node);
844 static int grow_directory(struct exfat* ef, struct exfat_node* dir,
845 uint64_t asize, uint32_t difference)
847 return exfat_truncate(ef, dir,
848 DIV_ROUND_UP(asize + difference, CLUSTER_SIZE(*ef->sb))
849 * CLUSTER_SIZE(*ef->sb), true);
852 static int find_slot(struct exfat* ef, struct exfat_node* dir,
853 cluster_t* cluster, off_t* offset, int subentries)
857 const struct exfat_entry* entry;
860 rc = opendir(ef, dir, &it);
867 *cluster = it.cluster;
870 entry = get_entry_ptr(ef, &it);
871 if (entry->type & EXFAT_ENTRY_VALID)
875 if (contiguous == subentries)
876 break; /* suitable slot is found */
877 if (it.offset + sizeof(struct exfat_entry) >= dir->size)
879 rc = grow_directory(ef, dir, dir->size,
880 (subentries - contiguous) * sizeof(struct exfat_entry));
887 if (!fetch_next_entry(ef, dir, &it))
897 static int write_entry(struct exfat* ef, struct exfat_node* dir,
898 const le16_t* name, cluster_t cluster, off_t offset, uint16_t attrib)
900 struct exfat_node* node;
901 struct exfat_entry_meta1 meta1;
902 struct exfat_entry_meta2 meta2;
903 const size_t name_length = utf16_length(name);
904 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
907 node = allocate_node();
910 node->entry_cluster = cluster;
911 node->entry_offset = offset;
912 memcpy(node->name, name, name_length * sizeof(le16_t));
914 memset(&meta1, 0, sizeof(meta1));
915 meta1.type = EXFAT_ENTRY_FILE;
916 meta1.continuations = 1 + name_entries;
917 meta1.attrib = cpu_to_le16(attrib);
918 exfat_unix2exfat(time(NULL), &meta1.crdate, &meta1.crtime,
920 meta1.adate = meta1.mdate = meta1.crdate;
921 meta1.atime = meta1.mtime = meta1.crtime;
922 meta1.mtime_cs = meta1.crtime_cs; /* there is no atime_cs */
924 memset(&meta2, 0, sizeof(meta2));
925 meta2.type = EXFAT_ENTRY_FILE_INFO;
926 meta2.flags = EXFAT_FLAG_ALWAYS1;
927 meta2.name_length = name_length;
928 meta2.name_hash = exfat_calc_name_hash(ef, node->name);
929 meta2.start_cluster = cpu_to_le32(EXFAT_CLUSTER_FREE);
931 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);
933 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
934 co2o(ef, cluster, offset)) < 0)
936 exfat_error("failed to write meta1 entry");
939 if (!next_entry(ef, dir, &cluster, &offset))
941 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
942 co2o(ef, cluster, offset)) < 0)
944 exfat_error("failed to write meta2 entry");
947 for (i = 0; i < name_entries; i++)
949 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
950 memcpy(name_entry.name, node->name + i * EXFAT_ENAME_MAX,
951 MIN(EXFAT_ENAME_MAX, EXFAT_NAME_MAX - i * EXFAT_ENAME_MAX) *
953 if (!next_entry(ef, dir, &cluster, &offset))
955 if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
956 co2o(ef, cluster, offset)) < 0)
958 exfat_error("failed to write name entry");
963 init_node_meta1(node, &meta1);
964 init_node_meta2(node, &meta2);
966 tree_attach(dir, node);
967 exfat_update_mtime(dir);
971 static int create(struct exfat* ef, const char* path, uint16_t attrib)
973 struct exfat_node* dir;
974 struct exfat_node* existing;
975 cluster_t cluster = EXFAT_CLUSTER_BAD;
977 le16_t name[EXFAT_NAME_MAX + 1];
980 rc = exfat_split(ef, &dir, &existing, name, path);
983 if (existing != NULL)
985 exfat_put_node(ef, existing);
986 exfat_put_node(ef, dir);
990 rc = find_slot(ef, dir, &cluster, &offset,
991 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
994 exfat_put_node(ef, dir);
997 rc = write_entry(ef, dir, name, cluster, offset, attrib);
1000 exfat_put_node(ef, dir);
1003 rc = exfat_flush_node(ef, dir);
1004 exfat_put_node(ef, dir);
1008 int exfat_mknod(struct exfat* ef, const char* path)
1010 return create(ef, path, EXFAT_ATTRIB_ARCH);
1013 int exfat_mkdir(struct exfat* ef, const char* path)
1016 struct exfat_node* node;
1018 rc = create(ef, path, EXFAT_ATTRIB_DIR);
1021 rc = exfat_lookup(ef, &node, path);
1024 /* directories always have at least one cluster */
1025 rc = exfat_truncate(ef, node, CLUSTER_SIZE(*ef->sb), true);
1029 exfat_put_node(ef, node);
1032 rc = exfat_flush_node(ef, node);
1036 exfat_put_node(ef, node);
1039 exfat_put_node(ef, node);
1043 static int rename_entry(struct exfat* ef, struct exfat_node* dir,
1044 struct exfat_node* node, const le16_t* name, cluster_t new_cluster,
1047 struct exfat_entry_meta1 meta1;
1048 struct exfat_entry_meta2 meta2;
1049 cluster_t old_cluster = node->entry_cluster;
1050 off_t old_offset = node->entry_offset;
1051 const size_t name_length = utf16_length(name);
1052 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
1055 if (exfat_pread(ef->dev, &meta1, sizeof(meta1),
1056 co2o(ef, old_cluster, old_offset)) < 0)
1058 exfat_error("failed to read meta1 entry on rename");
1061 if (!next_entry(ef, node->parent, &old_cluster, &old_offset))
1063 if (exfat_pread(ef->dev, &meta2, sizeof(meta2),
1064 co2o(ef, old_cluster, old_offset)) < 0)
1066 exfat_error("failed to read meta2 entry on rename");
1069 meta1.continuations = 1 + name_entries;
1070 meta2.name_hash = exfat_calc_name_hash(ef, name);
1071 meta2.name_length = name_length;
1072 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, name);
1074 if (!erase_entry(ef, node))
1077 node->entry_cluster = new_cluster;
1078 node->entry_offset = new_offset;
1080 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
1081 co2o(ef, new_cluster, new_offset)) < 0)
1083 exfat_error("failed to write meta1 entry on rename");
1086 if (!next_entry(ef, dir, &new_cluster, &new_offset))
1088 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
1089 co2o(ef, new_cluster, new_offset)) < 0)
1091 exfat_error("failed to write meta2 entry on rename");
1095 for (i = 0; i < name_entries; i++)
1097 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
1098 memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX,
1099 EXFAT_ENAME_MAX * sizeof(le16_t));
1100 if (!next_entry(ef, dir, &new_cluster, &new_offset))
1102 if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
1103 co2o(ef, new_cluster, new_offset)) < 0)
1105 exfat_error("failed to write name entry on rename");
1110 memcpy(node->name, name, (EXFAT_NAME_MAX + 1) * sizeof(le16_t));
1112 tree_attach(dir, node);
1116 int exfat_rename(struct exfat* ef, const char* old_path, const char* new_path)
1118 struct exfat_node* node;
1119 struct exfat_node* existing;
1120 struct exfat_node* dir;
1121 cluster_t cluster = EXFAT_CLUSTER_BAD;
1123 le16_t name[EXFAT_NAME_MAX + 1];
1126 rc = exfat_lookup(ef, &node, old_path);
1130 rc = exfat_split(ef, &dir, &existing, name, new_path);
1133 exfat_put_node(ef, node);
1137 /* check that target is not a subdirectory of the source */
1138 if (node->flags & EXFAT_ATTRIB_DIR)
1140 struct exfat_node* p;
1142 for (p = dir; p; p = p->parent)
1145 if (existing != NULL)
1146 exfat_put_node(ef, existing);
1147 exfat_put_node(ef, dir);
1148 exfat_put_node(ef, node);
1153 if (existing != NULL)
1155 /* remove target if it's not the same node as source */
1156 if (existing != node)
1158 if (existing->flags & EXFAT_ATTRIB_DIR)
1160 if (node->flags & EXFAT_ATTRIB_DIR)
1161 rc = exfat_rmdir(ef, existing);
1167 if (!(node->flags & EXFAT_ATTRIB_DIR))
1168 rc = exfat_unlink(ef, existing);
1172 exfat_put_node(ef, existing);
1175 /* free clusters even if something went wrong; overwise they
1176 will be just lost */
1177 exfat_cleanup_node(ef, existing);
1178 exfat_put_node(ef, dir);
1179 exfat_put_node(ef, node);
1182 rc = exfat_cleanup_node(ef, existing);
1185 exfat_put_node(ef, dir);
1186 exfat_put_node(ef, node);
1191 exfat_put_node(ef, existing);
1194 rc = find_slot(ef, dir, &cluster, &offset,
1195 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
1198 exfat_put_node(ef, dir);
1199 exfat_put_node(ef, node);
1202 rc = rename_entry(ef, dir, node, name, cluster, offset);
1203 exfat_put_node(ef, dir);
1204 exfat_put_node(ef, node);
1208 void exfat_utimes(struct exfat_node* node, const struct timespec tv[2])
1210 node->atime = tv[0].tv_sec;
1211 node->mtime = tv[1].tv_sec;
1212 node->flags |= EXFAT_ATTRIB_DIRTY;
1215 void exfat_update_atime(struct exfat_node* node)
1217 node->atime = time(NULL);
1218 node->flags |= EXFAT_ATTRIB_DIRTY;
1221 void exfat_update_mtime(struct exfat_node* node)
1223 node->mtime = time(NULL);
1224 node->flags |= EXFAT_ATTRIB_DIRTY;
1227 const char* exfat_get_label(struct exfat* ef)
1232 static int find_label(struct exfat* ef, cluster_t* cluster, off_t* offset)
1237 rc = opendir(ef, ef->root, &it);
1243 if (it.offset >= ef->root->size)
1249 if (get_entry_ptr(ef, &it)->type == EXFAT_ENTRY_LABEL)
1251 *cluster = it.cluster;
1252 *offset = it.offset;
1257 if (!fetch_next_entry(ef, ef->root, &it))
1265 int exfat_set_label(struct exfat* ef, const char* label)
1267 le16_t label_utf16[EXFAT_ENAME_MAX + 1];
1271 struct exfat_entry_label entry;
1273 memset(label_utf16, 0, sizeof(label_utf16));
1274 rc = utf8_to_utf16(label_utf16, label, EXFAT_ENAME_MAX, strlen(label));
1278 rc = find_label(ef, &cluster, &offset);
1280 rc = find_slot(ef, ef->root, &cluster, &offset, 1);
1284 entry.type = EXFAT_ENTRY_LABEL;
1285 entry.length = utf16_length(label_utf16);
1286 memcpy(entry.name, label_utf16, sizeof(entry.name));
1287 if (entry.length == 0)
1288 entry.type ^= EXFAT_ENTRY_VALID;
1290 if (exfat_pwrite(ef->dev, &entry, sizeof(struct exfat_entry_label),
1291 co2o(ef, cluster, offset)) < 0)
1293 exfat_error("failed to write label entry");
1296 strcpy(ef->label, label);