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 */
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);
230 static void decompress_upcase(uint16_t* output, const le16_t* source,
236 for (oi = 0; oi < EXFAT_UPCASE_CHARS; oi++)
239 for (si = 0, oi = 0; si < size && oi < EXFAT_UPCASE_CHARS; si++)
241 uint16_t ch = le16_to_cpu(source[si]);
243 if (ch == 0xffff && si + 1 < size) /* indicates a run */
244 oi += le16_to_cpu(source[++si]);
251 * Reads one entry in directory at position pointed by iterator and fills
254 static int readdir(struct exfat* ef, const struct exfat_node* parent,
255 struct exfat_node** node, struct iterator* it)
258 const struct exfat_entry* entry;
259 const struct exfat_entry_meta1* meta1;
260 const struct exfat_entry_meta2* meta2;
261 const struct exfat_entry_name* file_name;
262 const struct exfat_entry_upcase* upcase;
263 const struct exfat_entry_bitmap* bitmap;
264 const struct exfat_entry_label* label;
265 uint8_t continuations = 0;
266 le16_t* namep = NULL;
267 uint16_t reference_checksum = 0;
268 uint16_t actual_checksum = 0;
269 uint64_t valid_size = 0;
270 uint64_t upcase_size = 0;
271 le16_t* upcase_comp = NULL;
277 if (it->offset >= parent->size)
279 if (continuations != 0)
281 exfat_error("expected %hhu continuations", continuations);
284 return -ENOENT; /* that's OK, means end of directory */
287 entry = get_entry_ptr(ef, it);
290 case EXFAT_ENTRY_FILE:
291 if (continuations != 0)
293 exfat_error("expected %hhu continuations before new entry",
297 meta1 = (const struct exfat_entry_meta1*) entry;
298 continuations = meta1->continuations;
299 /* each file entry must have at least 2 continuations:
301 if (continuations < 2)
303 exfat_error("too few continuations (%hhu)", continuations);
306 if (continuations > 1 +
307 DIV_ROUND_UP(EXFAT_NAME_MAX, EXFAT_ENAME_MAX))
309 exfat_error("too many continuations (%hhu)", continuations);
312 reference_checksum = le16_to_cpu(meta1->checksum);
313 actual_checksum = exfat_start_checksum(meta1);
314 *node = allocate_node();
320 /* new node has zero reference counter */
321 (*node)->entry_cluster = it->cluster;
322 (*node)->entry_offset = it->offset;
323 init_node_meta1(*node, meta1);
324 namep = (*node)->name;
327 case EXFAT_ENTRY_FILE_INFO:
328 if (continuations < 2)
330 exfat_error("unexpected continuation (%hhu)",
334 meta2 = (const struct exfat_entry_meta2*) entry;
335 if (meta2->flags & ~(EXFAT_FLAG_ALWAYS1 | EXFAT_FLAG_CONTIGUOUS))
337 exfat_error("unknown flags in meta2 (0x%hhx)", meta2->flags);
340 init_node_meta2(*node, meta2);
341 actual_checksum = exfat_add_checksum(entry, actual_checksum);
342 valid_size = le64_to_cpu(meta2->valid_size);
343 /* empty files must be marked as non-contiguous */
344 if ((*node)->size == 0 && (meta2->flags & EXFAT_FLAG_CONTIGUOUS))
346 exfat_error("empty file marked as contiguous (0x%hhx)",
350 /* directories must be aligned on at cluster boundary */
351 if (((*node)->flags & EXFAT_ATTRIB_DIR) &&
352 (*node)->size % CLUSTER_SIZE(*ef->sb) != 0)
354 exfat_error("directory has invalid size %"PRIu64" bytes",
361 case EXFAT_ENTRY_FILE_NAME:
362 if (continuations == 0)
364 exfat_error("unexpected continuation");
367 file_name = (const struct exfat_entry_name*) entry;
368 actual_checksum = exfat_add_checksum(entry, actual_checksum);
370 memcpy(namep, file_name->name,
372 ((*node)->name + EXFAT_NAME_MAX - namep)) *
374 namep += EXFAT_ENAME_MAX;
375 if (--continuations == 0)
377 if (!check_node(*node, actual_checksum, reference_checksum,
380 if (!fetch_next_entry(ef, parent, it))
382 return 0; /* entry completed */
386 case EXFAT_ENTRY_UPCASE:
387 if (ef->upcase != NULL)
389 upcase = (const struct exfat_entry_upcase*) entry;
390 if (CLUSTER_INVALID(le32_to_cpu(upcase->start_cluster)))
392 exfat_error("invalid cluster 0x%x in upcase table",
393 le32_to_cpu(upcase->start_cluster));
396 upcase_size = le64_to_cpu(upcase->size);
397 if (upcase_size == 0 ||
398 upcase_size > EXFAT_UPCASE_CHARS * sizeof(uint16_t) ||
399 upcase_size % sizeof(uint16_t) != 0)
401 exfat_error("bad upcase table size (%"PRIu64" bytes)",
405 upcase_comp = malloc(upcase_size);
406 if (upcase_comp == NULL)
408 exfat_error("failed to allocate upcase table (%"PRIu64" bytes)",
414 /* read compressed upcase table */
415 if (exfat_pread(ef->dev, upcase_comp, upcase_size,
416 exfat_c2o(ef, le32_to_cpu(upcase->start_cluster))) < 0)
419 exfat_error("failed to read upper case table "
420 "(%"PRIu64" bytes starting at cluster %#x)",
422 le32_to_cpu(upcase->start_cluster));
426 /* decompress upcase table */
427 ef->upcase = calloc(EXFAT_UPCASE_CHARS, sizeof(uint16_t));
428 if (ef->upcase == NULL)
431 exfat_error("failed to allocate decompressed upcase table");
435 decompress_upcase(ef->upcase, upcase_comp,
436 upcase_size / sizeof(uint16_t));
440 case EXFAT_ENTRY_BITMAP:
441 bitmap = (const struct exfat_entry_bitmap*) entry;
442 ef->cmap.start_cluster = le32_to_cpu(bitmap->start_cluster);
443 if (CLUSTER_INVALID(ef->cmap.start_cluster))
445 exfat_error("invalid cluster 0x%x in clusters bitmap",
446 ef->cmap.start_cluster);
449 ef->cmap.size = le32_to_cpu(ef->sb->cluster_count) -
450 EXFAT_FIRST_DATA_CLUSTER;
451 if (le64_to_cpu(bitmap->size) < DIV_ROUND_UP(ef->cmap.size, 8))
453 exfat_error("invalid clusters bitmap size: %"PRIu64
454 " (expected at least %u)",
455 le64_to_cpu(bitmap->size),
456 DIV_ROUND_UP(ef->cmap.size, 8));
459 /* FIXME bitmap can be rather big, up to 512 MB */
460 ef->cmap.chunk_size = ef->cmap.size;
461 ef->cmap.chunk = malloc(BMAP_SIZE(ef->cmap.chunk_size));
462 if (ef->cmap.chunk == NULL)
464 exfat_error("failed to allocate clusters bitmap chunk "
465 "(%"PRIu64" bytes)", le64_to_cpu(bitmap->size));
470 if (exfat_pread(ef->dev, ef->cmap.chunk,
471 BMAP_SIZE(ef->cmap.chunk_size),
472 exfat_c2o(ef, ef->cmap.start_cluster)) < 0)
474 exfat_error("failed to read clusters bitmap "
475 "(%"PRIu64" bytes starting at cluster %#x)",
476 le64_to_cpu(bitmap->size), ef->cmap.start_cluster);
481 case EXFAT_ENTRY_LABEL:
482 label = (const struct exfat_entry_label*) entry;
483 if (label->length > EXFAT_ENAME_MAX)
485 exfat_error("too long label (%hhu chars)", label->length);
488 if (utf16_to_utf8(ef->label, label->name,
489 sizeof(ef->label) - 1, EXFAT_ENAME_MAX) != 0)
494 if (!(entry->type & EXFAT_ENTRY_VALID))
495 break; /* deleted entry, ignore it */
496 if (!(entry->type & EXFAT_ENTRY_OPTIONAL))
498 exfat_error("unknown entry type %#hhx", entry->type);
501 /* optional entry, warn and skip */
502 exfat_warn("unknown entry type %#hhx", entry->type);
503 if (continuations == 0)
505 exfat_error("unexpected continuation");
512 if (!fetch_next_entry(ef, parent, it))
515 /* we never reach here */
523 int exfat_cache_directory(struct exfat* ef, struct exfat_node* dir)
527 struct exfat_node* node;
528 struct exfat_node* current = NULL;
530 if (dir->flags & EXFAT_ATTRIB_CACHED)
531 return 0; /* already cached */
533 rc = opendir(ef, dir, &it);
536 while ((rc = readdir(ef, dir, &node, &it)) == 0)
541 current->next = node;
542 node->prev = current;
554 for (current = dir->child; current; current = node)
556 node = current->next;
563 dir->flags |= EXFAT_ATTRIB_CACHED;
567 static void tree_attach(struct exfat_node* dir, struct exfat_node* node)
572 dir->child->prev = node;
573 node->next = dir->child;
578 static void tree_detach(struct exfat_node* node)
581 node->prev->next = node->next;
582 else /* this is the first node in the list */
583 node->parent->child = node->next;
585 node->next->prev = node->prev;
591 static void reset_cache(struct exfat* ef, struct exfat_node* node)
593 char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
597 struct exfat_node* p = node->child;
602 node->flags &= ~EXFAT_ATTRIB_CACHED;
603 if (node->references != 0)
605 exfat_get_name(node, buffer, sizeof(buffer) - 1);
606 exfat_warn("non-zero reference counter (%d) for '%s'",
607 node->references, buffer);
609 if (node != ef->root && (node->flags & EXFAT_ATTRIB_DIRTY))
611 exfat_get_name(node, buffer, sizeof(buffer) - 1);
612 exfat_bug("node '%s' is dirty", buffer);
614 while (node->references)
615 exfat_put_node(ef, node);
618 void exfat_reset_cache(struct exfat* ef)
620 reset_cache(ef, ef->root);
623 static bool next_entry(struct exfat* ef, const struct exfat_node* parent,
624 cluster_t* cluster, off_t* offset)
626 *offset += sizeof(struct exfat_entry);
627 if (*offset % CLUSTER_SIZE(*ef->sb) == 0)
629 *cluster = exfat_next_cluster(ef, parent, *cluster);
630 if (CLUSTER_INVALID(*cluster))
632 exfat_error("invalid cluster %#x while getting next entry",
640 int exfat_flush_node(struct exfat* ef, struct exfat_node* node)
644 off_t meta1_offset, meta2_offset;
645 struct exfat_entry_meta1 meta1;
646 struct exfat_entry_meta2 meta2;
648 if (!(node->flags & EXFAT_ATTRIB_DIRTY))
649 return 0; /* no need to flush */
652 exfat_bug("unable to flush node to read-only FS");
654 if (node->parent == NULL)
655 return 0; /* do not flush unlinked node */
657 cluster = node->entry_cluster;
658 offset = node->entry_offset;
659 meta1_offset = co2o(ef, cluster, offset);
660 if (!next_entry(ef, node->parent, &cluster, &offset))
662 meta2_offset = co2o(ef, cluster, offset);
664 if (exfat_pread(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
666 exfat_error("failed to read meta1 entry on flush");
669 if (meta1.type != EXFAT_ENTRY_FILE)
670 exfat_bug("invalid type of meta1: 0x%hhx", meta1.type);
671 meta1.attrib = cpu_to_le16(node->flags);
672 exfat_unix2exfat(node->mtime, &meta1.mdate, &meta1.mtime, &meta1.mtime_cs);
673 exfat_unix2exfat(node->atime, &meta1.adate, &meta1.atime, NULL);
675 if (exfat_pread(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
677 exfat_error("failed to read meta2 entry on flush");
680 if (meta2.type != EXFAT_ENTRY_FILE_INFO)
681 exfat_bug("invalid type of meta2: 0x%hhx", meta2.type);
682 meta2.size = meta2.valid_size = cpu_to_le64(node->size);
683 meta2.start_cluster = cpu_to_le32(node->start_cluster);
684 meta2.flags = EXFAT_FLAG_ALWAYS1;
685 /* empty files must not be marked as contiguous */
686 if (node->size != 0 && IS_CONTIGUOUS(*node))
687 meta2.flags |= EXFAT_FLAG_CONTIGUOUS;
688 /* name hash remains unchanged, no need to recalculate it */
690 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);
692 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
694 exfat_error("failed to write meta1 entry on flush");
697 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
699 exfat_error("failed to write meta2 entry on flush");
703 node->flags &= ~EXFAT_ATTRIB_DIRTY;
704 return exfat_flush(ef);
707 static bool erase_entry(struct exfat* ef, struct exfat_node* node)
709 cluster_t cluster = node->entry_cluster;
710 off_t offset = node->entry_offset;
711 int name_entries = DIV_ROUND_UP(utf16_length(node->name), EXFAT_ENAME_MAX);
714 entry_type = EXFAT_ENTRY_FILE & ~EXFAT_ENTRY_VALID;
715 if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
717 exfat_error("failed to erase meta1 entry");
721 if (!next_entry(ef, node->parent, &cluster, &offset))
723 entry_type = EXFAT_ENTRY_FILE_INFO & ~EXFAT_ENTRY_VALID;
724 if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
726 exfat_error("failed to erase meta2 entry");
730 while (name_entries--)
732 if (!next_entry(ef, node->parent, &cluster, &offset))
734 entry_type = EXFAT_ENTRY_FILE_NAME & ~EXFAT_ENTRY_VALID;
735 if (exfat_pwrite(ef->dev, &entry_type, 1,
736 co2o(ef, cluster, offset)) < 0)
738 exfat_error("failed to erase name entry");
745 static int shrink_directory(struct exfat* ef, struct exfat_node* dir,
746 off_t deleted_offset)
748 const struct exfat_node* node;
749 const struct exfat_node* last_node;
750 uint64_t entries = 0;
753 if (!(dir->flags & EXFAT_ATTRIB_DIR))
754 exfat_bug("attempted to shrink a file");
755 if (!(dir->flags & EXFAT_ATTRIB_CACHED))
756 exfat_bug("attempted to shrink uncached directory");
758 for (last_node = node = dir->child; node; node = node->next)
760 if (deleted_offset < node->entry_offset)
762 /* there are other entries after the removed one, no way to shrink
766 if (last_node->entry_offset < node->entry_offset)
772 /* offset of the last entry */
773 entries += last_node->entry_offset / sizeof(struct exfat_entry);
774 /* two subentries with meta info */
776 /* subentries with file name */
777 entries += DIV_ROUND_UP(utf16_length(last_node->name),
781 new_size = DIV_ROUND_UP(entries * sizeof(struct exfat_entry),
782 CLUSTER_SIZE(*ef->sb)) * CLUSTER_SIZE(*ef->sb);
783 if (new_size == 0) /* directory always has at least 1 cluster */
784 new_size = CLUSTER_SIZE(*ef->sb);
785 if (new_size == dir->size)
787 return exfat_truncate(ef, dir, new_size, true);
790 static int delete(struct exfat* ef, struct exfat_node* node)
792 struct exfat_node* parent = node->parent;
793 off_t deleted_offset = node->entry_offset;
796 exfat_get_node(parent);
797 if (!erase_entry(ef, node))
799 exfat_put_node(ef, parent);
802 exfat_update_mtime(parent);
804 rc = shrink_directory(ef, parent, deleted_offset);
805 node->flags |= EXFAT_ATTRIB_UNLINKED;
808 exfat_flush_node(ef, parent);
809 exfat_put_node(ef, parent);
812 rc = exfat_flush_node(ef, parent);
813 exfat_put_node(ef, parent);
817 int exfat_unlink(struct exfat* ef, struct exfat_node* node)
819 if (node->flags & EXFAT_ATTRIB_DIR)
821 return delete(ef, node);
824 int exfat_rmdir(struct exfat* ef, struct exfat_node* node)
828 if (!(node->flags & EXFAT_ATTRIB_DIR))
830 /* check that directory is empty */
831 rc = exfat_cache_directory(ef, node);
836 return delete(ef, node);
839 static int grow_directory(struct exfat* ef, struct exfat_node* dir,
840 uint64_t asize, uint32_t difference)
842 return exfat_truncate(ef, dir,
843 DIV_ROUND_UP(asize + difference, CLUSTER_SIZE(*ef->sb))
844 * CLUSTER_SIZE(*ef->sb), true);
847 static int find_slot(struct exfat* ef, struct exfat_node* dir,
848 cluster_t* cluster, off_t* offset, int subentries)
852 const struct exfat_entry* entry;
855 rc = opendir(ef, dir, &it);
862 *cluster = it.cluster;
865 entry = get_entry_ptr(ef, &it);
866 if (entry->type & EXFAT_ENTRY_VALID)
870 if (contiguous == subentries)
871 break; /* suitable slot is found */
872 if (it.offset + sizeof(struct exfat_entry) >= dir->size)
874 rc = grow_directory(ef, dir, dir->size,
875 (subentries - contiguous) * sizeof(struct exfat_entry));
882 if (!fetch_next_entry(ef, dir, &it))
892 static int write_entry(struct exfat* ef, struct exfat_node* dir,
893 const le16_t* name, cluster_t cluster, off_t offset, uint16_t attrib)
895 struct exfat_node* node;
896 struct exfat_entry_meta1 meta1;
897 struct exfat_entry_meta2 meta2;
898 const size_t name_length = utf16_length(name);
899 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
902 node = allocate_node();
905 node->entry_cluster = cluster;
906 node->entry_offset = offset;
907 memcpy(node->name, name, name_length * sizeof(le16_t));
909 memset(&meta1, 0, sizeof(meta1));
910 meta1.type = EXFAT_ENTRY_FILE;
911 meta1.continuations = 1 + name_entries;
912 meta1.attrib = cpu_to_le16(attrib);
913 exfat_unix2exfat(time(NULL), &meta1.crdate, &meta1.crtime,
915 meta1.adate = meta1.mdate = meta1.crdate;
916 meta1.atime = meta1.mtime = meta1.crtime;
917 meta1.mtime_cs = meta1.crtime_cs; /* there is no atime_cs */
919 memset(&meta2, 0, sizeof(meta2));
920 meta2.type = EXFAT_ENTRY_FILE_INFO;
921 meta2.flags = EXFAT_FLAG_ALWAYS1;
922 meta2.name_length = name_length;
923 meta2.name_hash = exfat_calc_name_hash(ef, node->name);
924 meta2.start_cluster = cpu_to_le32(EXFAT_CLUSTER_FREE);
926 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);
928 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
929 co2o(ef, cluster, offset)) < 0)
931 exfat_error("failed to write meta1 entry");
934 if (!next_entry(ef, dir, &cluster, &offset))
936 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
937 co2o(ef, cluster, offset)) < 0)
939 exfat_error("failed to write meta2 entry");
942 for (i = 0; i < name_entries; i++)
944 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
945 memcpy(name_entry.name, node->name + i * EXFAT_ENAME_MAX,
946 MIN(EXFAT_ENAME_MAX, EXFAT_NAME_MAX - i * EXFAT_ENAME_MAX) *
948 if (!next_entry(ef, dir, &cluster, &offset))
950 if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
951 co2o(ef, cluster, offset)) < 0)
953 exfat_error("failed to write name entry");
958 init_node_meta1(node, &meta1);
959 init_node_meta2(node, &meta2);
961 tree_attach(dir, node);
962 exfat_update_mtime(dir);
966 static int create(struct exfat* ef, const char* path, uint16_t attrib)
968 struct exfat_node* dir;
969 struct exfat_node* existing;
970 cluster_t cluster = EXFAT_CLUSTER_BAD;
972 le16_t name[EXFAT_NAME_MAX + 1];
975 rc = exfat_split(ef, &dir, &existing, name, path);
978 if (existing != NULL)
980 exfat_put_node(ef, existing);
981 exfat_put_node(ef, dir);
985 rc = find_slot(ef, dir, &cluster, &offset,
986 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
989 exfat_put_node(ef, dir);
992 rc = write_entry(ef, dir, name, cluster, offset, attrib);
995 exfat_put_node(ef, dir);
998 rc = exfat_flush_node(ef, dir);
999 exfat_put_node(ef, dir);
1003 int exfat_mknod(struct exfat* ef, const char* path)
1005 return create(ef, path, EXFAT_ATTRIB_ARCH);
1008 int exfat_mkdir(struct exfat* ef, const char* path)
1011 struct exfat_node* node;
1013 rc = create(ef, path, EXFAT_ATTRIB_DIR);
1016 rc = exfat_lookup(ef, &node, path);
1019 /* directories always have at least one cluster */
1020 rc = exfat_truncate(ef, node, CLUSTER_SIZE(*ef->sb), true);
1024 exfat_put_node(ef, node);
1027 rc = exfat_flush_node(ef, node);
1031 exfat_put_node(ef, node);
1034 exfat_put_node(ef, node);
1038 static int rename_entry(struct exfat* ef, struct exfat_node* dir,
1039 struct exfat_node* node, const le16_t* name, cluster_t new_cluster,
1042 struct exfat_entry_meta1 meta1;
1043 struct exfat_entry_meta2 meta2;
1044 cluster_t old_cluster = node->entry_cluster;
1045 off_t old_offset = node->entry_offset;
1046 const size_t name_length = utf16_length(name);
1047 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
1050 if (exfat_pread(ef->dev, &meta1, sizeof(meta1),
1051 co2o(ef, old_cluster, old_offset)) < 0)
1053 exfat_error("failed to read meta1 entry on rename");
1056 if (!next_entry(ef, node->parent, &old_cluster, &old_offset))
1058 if (exfat_pread(ef->dev, &meta2, sizeof(meta2),
1059 co2o(ef, old_cluster, old_offset)) < 0)
1061 exfat_error("failed to read meta2 entry on rename");
1064 meta1.continuations = 1 + name_entries;
1065 meta2.name_hash = exfat_calc_name_hash(ef, name);
1066 meta2.name_length = name_length;
1067 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, name);
1069 if (!erase_entry(ef, node))
1072 node->entry_cluster = new_cluster;
1073 node->entry_offset = new_offset;
1075 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
1076 co2o(ef, new_cluster, new_offset)) < 0)
1078 exfat_error("failed to write meta1 entry on rename");
1081 if (!next_entry(ef, dir, &new_cluster, &new_offset))
1083 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
1084 co2o(ef, new_cluster, new_offset)) < 0)
1086 exfat_error("failed to write meta2 entry on rename");
1090 for (i = 0; i < name_entries; i++)
1092 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
1093 memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX,
1094 EXFAT_ENAME_MAX * sizeof(le16_t));
1095 if (!next_entry(ef, dir, &new_cluster, &new_offset))
1097 if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
1098 co2o(ef, new_cluster, new_offset)) < 0)
1100 exfat_error("failed to write name entry on rename");
1105 memcpy(node->name, name, (EXFAT_NAME_MAX + 1) * sizeof(le16_t));
1107 tree_attach(dir, node);
1111 int exfat_rename(struct exfat* ef, const char* old_path, const char* new_path)
1113 struct exfat_node* node;
1114 struct exfat_node* existing;
1115 struct exfat_node* dir;
1116 cluster_t cluster = EXFAT_CLUSTER_BAD;
1118 le16_t name[EXFAT_NAME_MAX + 1];
1121 rc = exfat_lookup(ef, &node, old_path);
1125 rc = exfat_split(ef, &dir, &existing, name, new_path);
1128 exfat_put_node(ef, node);
1132 /* check that target is not a subdirectory of the source */
1133 if (node->flags & EXFAT_ATTRIB_DIR)
1135 struct exfat_node* p;
1137 for (p = dir; p; p = p->parent)
1140 if (existing != NULL)
1141 exfat_put_node(ef, existing);
1142 exfat_put_node(ef, dir);
1143 exfat_put_node(ef, node);
1148 if (existing != NULL)
1150 /* remove target if it's not the same node as source */
1151 if (existing != node)
1153 if (existing->flags & EXFAT_ATTRIB_DIR)
1155 if (node->flags & EXFAT_ATTRIB_DIR)
1156 rc = exfat_rmdir(ef, existing);
1162 if (!(node->flags & EXFAT_ATTRIB_DIR))
1163 rc = exfat_unlink(ef, existing);
1167 exfat_put_node(ef, existing);
1170 /* free clusters even if something went wrong; overwise they
1171 will be just lost */
1172 exfat_cleanup_node(ef, existing);
1173 exfat_put_node(ef, dir);
1174 exfat_put_node(ef, node);
1177 rc = exfat_cleanup_node(ef, existing);
1180 exfat_put_node(ef, dir);
1181 exfat_put_node(ef, node);
1186 exfat_put_node(ef, existing);
1189 rc = find_slot(ef, dir, &cluster, &offset,
1190 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
1193 exfat_put_node(ef, dir);
1194 exfat_put_node(ef, node);
1197 rc = rename_entry(ef, dir, node, name, cluster, offset);
1198 exfat_put_node(ef, dir);
1199 exfat_put_node(ef, node);
1203 void exfat_utimes(struct exfat_node* node, const struct timespec tv[2])
1205 node->atime = tv[0].tv_sec;
1206 node->mtime = tv[1].tv_sec;
1207 node->flags |= EXFAT_ATTRIB_DIRTY;
1210 void exfat_update_atime(struct exfat_node* node)
1212 node->atime = time(NULL);
1213 node->flags |= EXFAT_ATTRIB_DIRTY;
1216 void exfat_update_mtime(struct exfat_node* node)
1218 node->mtime = time(NULL);
1219 node->flags |= EXFAT_ATTRIB_DIRTY;
1222 const char* exfat_get_label(struct exfat* ef)
1227 static int find_label(struct exfat* ef, cluster_t* cluster, off_t* offset)
1232 rc = opendir(ef, ef->root, &it);
1238 if (it.offset >= ef->root->size)
1244 if (get_entry_ptr(ef, &it)->type == EXFAT_ENTRY_LABEL)
1246 *cluster = it.cluster;
1247 *offset = it.offset;
1252 if (!fetch_next_entry(ef, ef->root, &it))
1260 int exfat_set_label(struct exfat* ef, const char* label)
1262 le16_t label_utf16[EXFAT_ENAME_MAX + 1];
1266 struct exfat_entry_label entry;
1268 memset(label_utf16, 0, sizeof(label_utf16));
1269 rc = utf8_to_utf16(label_utf16, label, EXFAT_ENAME_MAX, strlen(label));
1273 rc = find_label(ef, &cluster, &offset);
1275 rc = find_slot(ef, ef->root, &cluster, &offset, 1);
1279 entry.type = EXFAT_ENTRY_LABEL;
1280 entry.length = utf16_length(label_utf16);
1281 memcpy(entry.name, label_utf16, sizeof(entry.name));
1282 if (entry.length == 0)
1283 entry.type ^= EXFAT_ENTRY_VALID;
1285 if (exfat_pwrite(ef->dev, &entry, sizeof(struct exfat_entry_label),
1286 co2o(ef, cluster, offset)) < 0)
1288 exfat_error("failed to write label entry");
1291 strcpy(ef->label, label);