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)
113 exfat_error("failed to read directory cluster %#x", it->cluster);
119 static void closedir(struct iterator* it)
128 static bool fetch_next_entry(struct exfat* ef, const struct exfat_node* parent,
131 /* move iterator to the next entry in the directory */
132 it->offset += sizeof(struct exfat_entry);
133 /* fetch the next cluster if needed */
134 if ((it->offset & (CLUSTER_SIZE(*ef->sb) - 1)) == 0)
136 /* reached the end of directory; the caller should check this
138 if (it->offset >= parent->size)
140 it->cluster = exfat_next_cluster(ef, parent, it->cluster);
141 if (CLUSTER_INVALID(it->cluster))
143 exfat_error("invalid cluster 0x%x while reading directory",
147 if (exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb),
148 exfat_c2o(ef, it->cluster)) < 0)
150 exfat_error("failed to read the next directory cluster %#x",
158 static struct exfat_node* allocate_node(void)
160 struct exfat_node* node = malloc(sizeof(struct exfat_node));
163 exfat_error("failed to allocate node");
166 memset(node, 0, sizeof(struct exfat_node));
170 static void init_node_meta1(struct exfat_node* node,
171 const struct exfat_entry_meta1* meta1)
173 node->flags = le16_to_cpu(meta1->attrib);
174 node->mtime = exfat_exfat2unix(meta1->mdate, meta1->mtime,
176 /* there is no centiseconds field for atime */
177 node->atime = exfat_exfat2unix(meta1->adate, meta1->atime, 0);
180 static void init_node_meta2(struct exfat_node* node,
181 const struct exfat_entry_meta2* meta2)
183 node->size = le64_to_cpu(meta2->size);
184 node->start_cluster = le32_to_cpu(meta2->start_cluster);
185 node->fptr_cluster = node->start_cluster;
186 if (meta2->flags & EXFAT_FLAG_CONTIGUOUS)
187 node->flags |= EXFAT_ATTRIB_CONTIGUOUS;
190 static const struct exfat_entry* get_entry_ptr(const struct exfat* ef,
191 const struct iterator* it)
193 return (const struct exfat_entry*)
194 (it->chunk + it->offset % CLUSTER_SIZE(*ef->sb));
197 static bool check_node(const struct exfat_node* node, uint16_t actual_checksum,
198 uint16_t reference_checksum, uint64_t valid_size)
200 char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
203 Validate checksum first. If it's invalid all other fields probably
204 contain just garbage.
206 if (actual_checksum != reference_checksum)
208 exfat_get_name(node, buffer, sizeof(buffer) - 1);
209 exfat_error("'%s' has invalid checksum (%#hx != %#hx)", buffer,
210 actual_checksum, reference_checksum);
215 exFAT does not support sparse files but allows files with uninitialized
216 clusters. For such files valid_size means initialized data size and
217 cannot be greater than file size. See SetFileValidData() function
220 if (valid_size > node->size)
222 exfat_get_name(node, buffer, sizeof(buffer) - 1);
223 exfat_error("'%s' has valid size (%"PRIu64") greater than size "
224 "(%"PRIu64")", buffer, valid_size, node->size);
231 static void decompress_upcase(uint16_t* output, const le16_t* source,
237 for (oi = 0; oi < EXFAT_UPCASE_CHARS; oi++)
240 for (si = 0, oi = 0; si < size && oi < EXFAT_UPCASE_CHARS; si++)
242 uint16_t ch = le16_to_cpu(source[si]);
244 if (ch == 0xffff && si + 1 < size) /* indicates a run */
245 oi += le16_to_cpu(source[++si]);
252 * Reads one entry in directory at position pointed by iterator and fills
255 static int readdir(struct exfat* ef, const struct exfat_node* parent,
256 struct exfat_node** node, struct iterator* it)
259 const struct exfat_entry* entry;
260 const struct exfat_entry_meta1* meta1;
261 const struct exfat_entry_meta2* meta2;
262 const struct exfat_entry_name* file_name;
263 const struct exfat_entry_upcase* upcase;
264 const struct exfat_entry_bitmap* bitmap;
265 const struct exfat_entry_label* label;
266 uint8_t continuations = 0;
267 le16_t* namep = NULL;
268 uint16_t reference_checksum = 0;
269 uint16_t actual_checksum = 0;
270 uint64_t valid_size = 0;
271 uint64_t upcase_size = 0;
272 le16_t* upcase_comp = NULL;
278 if (it->offset >= parent->size)
280 if (continuations != 0)
282 exfat_error("expected %hhu continuations", continuations);
285 return -ENOENT; /* that's OK, means end of directory */
288 entry = get_entry_ptr(ef, it);
291 case EXFAT_ENTRY_FILE:
292 if (continuations != 0)
294 exfat_error("expected %hhu continuations before new entry",
298 meta1 = (const struct exfat_entry_meta1*) entry;
299 continuations = meta1->continuations;
300 /* each file entry must have at least 2 continuations:
302 if (continuations < 2)
304 exfat_error("too few continuations (%hhu)", continuations);
307 if (continuations > 1 +
308 DIV_ROUND_UP(EXFAT_NAME_MAX, EXFAT_ENAME_MAX))
310 exfat_error("too many continuations (%hhu)", continuations);
313 reference_checksum = le16_to_cpu(meta1->checksum);
314 actual_checksum = exfat_start_checksum(meta1);
315 *node = allocate_node();
321 /* new node has zero reference counter */
322 (*node)->entry_cluster = it->cluster;
323 (*node)->entry_offset = it->offset;
324 init_node_meta1(*node, meta1);
325 namep = (*node)->name;
328 case EXFAT_ENTRY_FILE_INFO:
329 if (continuations < 2)
331 exfat_error("unexpected continuation (%hhu)",
335 meta2 = (const struct exfat_entry_meta2*) entry;
336 if (meta2->flags & ~(EXFAT_FLAG_ALWAYS1 | EXFAT_FLAG_CONTIGUOUS))
338 exfat_error("unknown flags in meta2 (0x%hhx)", meta2->flags);
341 init_node_meta2(*node, meta2);
342 actual_checksum = exfat_add_checksum(entry, actual_checksum);
343 valid_size = le64_to_cpu(meta2->valid_size);
344 /* empty files must be marked as non-contiguous */
345 if ((*node)->size == 0 && (meta2->flags & EXFAT_FLAG_CONTIGUOUS))
347 exfat_error("empty file marked as contiguous (0x%hhx)",
351 /* directories must be aligned on at cluster boundary */
352 if (((*node)->flags & EXFAT_ATTRIB_DIR) &&
353 (*node)->size % CLUSTER_SIZE(*ef->sb) != 0)
355 exfat_error("directory has invalid size %"PRIu64" bytes",
362 case EXFAT_ENTRY_FILE_NAME:
363 if (continuations == 0)
365 exfat_error("unexpected continuation");
368 file_name = (const struct exfat_entry_name*) entry;
369 actual_checksum = exfat_add_checksum(entry, actual_checksum);
371 memcpy(namep, file_name->name,
373 ((*node)->name + EXFAT_NAME_MAX - namep)) *
375 namep += EXFAT_ENAME_MAX;
376 if (--continuations == 0)
378 if (!check_node(*node, actual_checksum, reference_checksum,
381 if (!fetch_next_entry(ef, parent, it))
383 return 0; /* entry completed */
387 case EXFAT_ENTRY_UPCASE:
388 if (ef->upcase != NULL)
390 upcase = (const struct exfat_entry_upcase*) entry;
391 if (CLUSTER_INVALID(le32_to_cpu(upcase->start_cluster)))
393 exfat_error("invalid cluster 0x%x in upcase table",
394 le32_to_cpu(upcase->start_cluster));
397 upcase_size = le64_to_cpu(upcase->size);
398 if (upcase_size == 0 ||
399 upcase_size > EXFAT_UPCASE_CHARS * sizeof(uint16_t) ||
400 upcase_size % sizeof(uint16_t) != 0)
402 exfat_error("bad upcase table size (%"PRIu64" bytes)",
406 upcase_comp = malloc(upcase_size);
407 if (upcase_comp == NULL)
409 exfat_error("failed to allocate upcase table (%"PRIu64" bytes)",
415 /* read compressed upcase table */
416 if (exfat_pread(ef->dev, upcase_comp, upcase_size,
417 exfat_c2o(ef, le32_to_cpu(upcase->start_cluster))) < 0)
420 exfat_error("failed to read upper case table "
421 "(%"PRIu64" bytes starting at cluster %#x)",
423 le32_to_cpu(upcase->start_cluster));
427 /* decompress upcase table */
428 ef->upcase = calloc(EXFAT_UPCASE_CHARS, sizeof(uint16_t));
429 if (ef->upcase == NULL)
432 exfat_error("failed to allocate decompressed upcase table");
436 decompress_upcase(ef->upcase, upcase_comp,
437 upcase_size / sizeof(uint16_t));
441 case EXFAT_ENTRY_BITMAP:
442 bitmap = (const struct exfat_entry_bitmap*) entry;
443 ef->cmap.start_cluster = le32_to_cpu(bitmap->start_cluster);
444 if (CLUSTER_INVALID(ef->cmap.start_cluster))
446 exfat_error("invalid cluster 0x%x in clusters bitmap",
447 ef->cmap.start_cluster);
450 ef->cmap.size = le32_to_cpu(ef->sb->cluster_count) -
451 EXFAT_FIRST_DATA_CLUSTER;
452 if (le64_to_cpu(bitmap->size) < DIV_ROUND_UP(ef->cmap.size, 8))
454 exfat_error("invalid clusters bitmap size: %"PRIu64
455 " (expected at least %u)",
456 le64_to_cpu(bitmap->size),
457 DIV_ROUND_UP(ef->cmap.size, 8));
460 /* FIXME bitmap can be rather big, up to 512 MB */
461 ef->cmap.chunk_size = ef->cmap.size;
462 ef->cmap.chunk = malloc(BMAP_SIZE(ef->cmap.chunk_size));
463 if (ef->cmap.chunk == NULL)
465 exfat_error("failed to allocate clusters bitmap chunk "
466 "(%"PRIu64" bytes)", le64_to_cpu(bitmap->size));
471 if (exfat_pread(ef->dev, ef->cmap.chunk,
472 BMAP_SIZE(ef->cmap.chunk_size),
473 exfat_c2o(ef, ef->cmap.start_cluster)) < 0)
475 exfat_error("failed to read clusters bitmap "
476 "(%"PRIu64" bytes starting at cluster %#x)",
477 le64_to_cpu(bitmap->size), ef->cmap.start_cluster);
482 case EXFAT_ENTRY_LABEL:
483 label = (const struct exfat_entry_label*) entry;
484 if (label->length > EXFAT_ENAME_MAX)
486 exfat_error("too long label (%hhu chars)", label->length);
489 if (utf16_to_utf8(ef->label, label->name,
490 sizeof(ef->label) - 1, EXFAT_ENAME_MAX) != 0)
495 if (!(entry->type & EXFAT_ENTRY_VALID))
496 break; /* deleted entry, ignore it */
497 if (!(entry->type & EXFAT_ENTRY_OPTIONAL))
499 exfat_error("unknown entry type %#hhx", entry->type);
502 /* optional entry, warn and skip */
503 exfat_warn("unknown entry type %#hhx", entry->type);
504 if (continuations == 0)
506 exfat_error("unexpected continuation");
513 if (!fetch_next_entry(ef, parent, it))
516 /* we never reach here */
524 int exfat_cache_directory(struct exfat* ef, struct exfat_node* dir)
528 struct exfat_node* node;
529 struct exfat_node* current = NULL;
531 if (dir->flags & EXFAT_ATTRIB_CACHED)
532 return 0; /* already cached */
534 rc = opendir(ef, dir, &it);
537 while ((rc = readdir(ef, dir, &node, &it)) == 0)
542 current->next = node;
543 node->prev = current;
555 for (current = dir->child; current; current = node)
557 node = current->next;
564 dir->flags |= EXFAT_ATTRIB_CACHED;
568 static void tree_attach(struct exfat_node* dir, struct exfat_node* node)
573 dir->child->prev = node;
574 node->next = dir->child;
579 static void tree_detach(struct exfat_node* node)
582 node->prev->next = node->next;
583 else /* this is the first node in the list */
584 node->parent->child = node->next;
586 node->next->prev = node->prev;
592 static void reset_cache(struct exfat* ef, struct exfat_node* node)
594 char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
598 struct exfat_node* p = node->child;
603 node->flags &= ~EXFAT_ATTRIB_CACHED;
604 if (node->references != 0)
606 exfat_get_name(node, buffer, sizeof(buffer) - 1);
607 exfat_warn("non-zero reference counter (%d) for '%s'",
608 node->references, buffer);
610 if (node != ef->root && (node->flags & EXFAT_ATTRIB_DIRTY))
612 exfat_get_name(node, buffer, sizeof(buffer) - 1);
613 exfat_bug("node '%s' is dirty", buffer);
615 while (node->references)
616 exfat_put_node(ef, node);
619 void exfat_reset_cache(struct exfat* ef)
621 reset_cache(ef, ef->root);
624 static bool next_entry(struct exfat* ef, const struct exfat_node* parent,
625 cluster_t* cluster, off_t* offset)
627 *offset += sizeof(struct exfat_entry);
628 if (*offset % CLUSTER_SIZE(*ef->sb) == 0)
630 *cluster = exfat_next_cluster(ef, parent, *cluster);
631 if (CLUSTER_INVALID(*cluster))
633 exfat_error("invalid cluster %#x while getting next entry",
641 int exfat_flush_node(struct exfat* ef, struct exfat_node* node)
645 off_t meta1_offset, meta2_offset;
646 struct exfat_entry_meta1 meta1;
647 struct exfat_entry_meta2 meta2;
649 if (!(node->flags & EXFAT_ATTRIB_DIRTY))
650 return 0; /* no need to flush */
653 exfat_bug("unable to flush node to read-only FS");
655 if (node->parent == NULL)
656 return 0; /* do not flush unlinked node */
658 cluster = node->entry_cluster;
659 offset = node->entry_offset;
660 meta1_offset = co2o(ef, cluster, offset);
661 if (!next_entry(ef, node->parent, &cluster, &offset))
663 meta2_offset = co2o(ef, cluster, offset);
665 if (exfat_pread(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
667 exfat_error("failed to read meta1 entry on flush");
670 if (meta1.type != EXFAT_ENTRY_FILE)
671 exfat_bug("invalid type of meta1: 0x%hhx", meta1.type);
672 meta1.attrib = cpu_to_le16(node->flags);
673 exfat_unix2exfat(node->mtime, &meta1.mdate, &meta1.mtime, &meta1.mtime_cs);
674 exfat_unix2exfat(node->atime, &meta1.adate, &meta1.atime, NULL);
676 if (exfat_pread(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
678 exfat_error("failed to read meta2 entry on flush");
681 if (meta2.type != EXFAT_ENTRY_FILE_INFO)
682 exfat_bug("invalid type of meta2: 0x%hhx", meta2.type);
683 meta2.size = meta2.valid_size = cpu_to_le64(node->size);
684 meta2.start_cluster = cpu_to_le32(node->start_cluster);
685 meta2.flags = EXFAT_FLAG_ALWAYS1;
686 /* empty files must not be marked as contiguous */
687 if (node->size != 0 && IS_CONTIGUOUS(*node))
688 meta2.flags |= EXFAT_FLAG_CONTIGUOUS;
689 /* name hash remains unchanged, no need to recalculate it */
691 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);
693 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
695 exfat_error("failed to write meta1 entry on flush");
698 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
700 exfat_error("failed to write meta2 entry on flush");
704 node->flags &= ~EXFAT_ATTRIB_DIRTY;
705 return exfat_flush(ef);
708 static bool erase_entry(struct exfat* ef, struct exfat_node* node)
710 cluster_t cluster = node->entry_cluster;
711 off_t offset = node->entry_offset;
712 int name_entries = DIV_ROUND_UP(utf16_length(node->name), EXFAT_ENAME_MAX);
715 entry_type = EXFAT_ENTRY_FILE & ~EXFAT_ENTRY_VALID;
716 if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
718 exfat_error("failed to erase meta1 entry");
722 if (!next_entry(ef, node->parent, &cluster, &offset))
724 entry_type = EXFAT_ENTRY_FILE_INFO & ~EXFAT_ENTRY_VALID;
725 if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
727 exfat_error("failed to erase meta2 entry");
731 while (name_entries--)
733 if (!next_entry(ef, node->parent, &cluster, &offset))
735 entry_type = EXFAT_ENTRY_FILE_NAME & ~EXFAT_ENTRY_VALID;
736 if (exfat_pwrite(ef->dev, &entry_type, 1,
737 co2o(ef, cluster, offset)) < 0)
739 exfat_error("failed to erase name entry");
746 static int shrink_directory(struct exfat* ef, struct exfat_node* dir,
747 off_t deleted_offset)
749 const struct exfat_node* node;
750 const struct exfat_node* last_node;
751 uint64_t entries = 0;
754 if (!(dir->flags & EXFAT_ATTRIB_DIR))
755 exfat_bug("attempted to shrink a file");
756 if (!(dir->flags & EXFAT_ATTRIB_CACHED))
757 exfat_bug("attempted to shrink uncached directory");
759 for (last_node = node = dir->child; node; node = node->next)
761 if (deleted_offset < node->entry_offset)
763 /* there are other entries after the removed one, no way to shrink
767 if (last_node->entry_offset < node->entry_offset)
773 /* offset of the last entry */
774 entries += last_node->entry_offset / sizeof(struct exfat_entry);
775 /* two subentries with meta info */
777 /* subentries with file name */
778 entries += DIV_ROUND_UP(utf16_length(last_node->name),
782 new_size = DIV_ROUND_UP(entries * sizeof(struct exfat_entry),
783 CLUSTER_SIZE(*ef->sb)) * CLUSTER_SIZE(*ef->sb);
784 if (new_size == 0) /* directory always has at least 1 cluster */
785 new_size = CLUSTER_SIZE(*ef->sb);
786 if (new_size == dir->size)
788 return exfat_truncate(ef, dir, new_size, true);
791 static int delete(struct exfat* ef, struct exfat_node* node)
793 struct exfat_node* parent = node->parent;
794 off_t deleted_offset = node->entry_offset;
797 exfat_get_node(parent);
798 if (!erase_entry(ef, node))
800 exfat_put_node(ef, parent);
803 exfat_update_mtime(parent);
805 rc = shrink_directory(ef, parent, deleted_offset);
806 node->flags |= EXFAT_ATTRIB_UNLINKED;
809 exfat_flush_node(ef, parent);
810 exfat_put_node(ef, parent);
813 rc = exfat_flush_node(ef, parent);
814 exfat_put_node(ef, parent);
818 int exfat_unlink(struct exfat* ef, struct exfat_node* node)
820 if (node->flags & EXFAT_ATTRIB_DIR)
822 return delete(ef, node);
825 int exfat_rmdir(struct exfat* ef, struct exfat_node* node)
829 if (!(node->flags & EXFAT_ATTRIB_DIR))
831 /* check that directory is empty */
832 rc = exfat_cache_directory(ef, node);
837 return delete(ef, node);
840 static int grow_directory(struct exfat* ef, struct exfat_node* dir,
841 uint64_t asize, uint32_t difference)
843 return exfat_truncate(ef, dir,
844 DIV_ROUND_UP(asize + difference, CLUSTER_SIZE(*ef->sb))
845 * CLUSTER_SIZE(*ef->sb), true);
848 static int find_slot(struct exfat* ef, struct exfat_node* dir,
849 cluster_t* cluster, off_t* offset, int subentries)
853 const struct exfat_entry* entry;
856 rc = opendir(ef, dir, &it);
863 *cluster = it.cluster;
866 entry = get_entry_ptr(ef, &it);
867 if (entry->type & EXFAT_ENTRY_VALID)
871 if (contiguous == subentries)
872 break; /* suitable slot is found */
873 if (it.offset + sizeof(struct exfat_entry) >= dir->size)
875 rc = grow_directory(ef, dir, dir->size,
876 (subentries - contiguous) * sizeof(struct exfat_entry));
883 if (!fetch_next_entry(ef, dir, &it))
893 static int write_entry(struct exfat* ef, struct exfat_node* dir,
894 const le16_t* name, cluster_t cluster, off_t offset, uint16_t attrib)
896 struct exfat_node* node;
897 struct exfat_entry_meta1 meta1;
898 struct exfat_entry_meta2 meta2;
899 const size_t name_length = utf16_length(name);
900 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
903 node = allocate_node();
906 node->entry_cluster = cluster;
907 node->entry_offset = offset;
908 memcpy(node->name, name, name_length * sizeof(le16_t));
910 memset(&meta1, 0, sizeof(meta1));
911 meta1.type = EXFAT_ENTRY_FILE;
912 meta1.continuations = 1 + name_entries;
913 meta1.attrib = cpu_to_le16(attrib);
914 exfat_unix2exfat(time(NULL), &meta1.crdate, &meta1.crtime,
916 meta1.adate = meta1.mdate = meta1.crdate;
917 meta1.atime = meta1.mtime = meta1.crtime;
918 meta1.mtime_cs = meta1.crtime_cs; /* there is no atime_cs */
920 memset(&meta2, 0, sizeof(meta2));
921 meta2.type = EXFAT_ENTRY_FILE_INFO;
922 meta2.flags = EXFAT_FLAG_ALWAYS1;
923 meta2.name_length = name_length;
924 meta2.name_hash = exfat_calc_name_hash(ef, node->name);
925 meta2.start_cluster = cpu_to_le32(EXFAT_CLUSTER_FREE);
927 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);
929 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
930 co2o(ef, cluster, offset)) < 0)
932 exfat_error("failed to write meta1 entry");
935 if (!next_entry(ef, dir, &cluster, &offset))
937 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
938 co2o(ef, cluster, offset)) < 0)
940 exfat_error("failed to write meta2 entry");
943 for (i = 0; i < name_entries; i++)
945 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
946 memcpy(name_entry.name, node->name + i * EXFAT_ENAME_MAX,
947 MIN(EXFAT_ENAME_MAX, EXFAT_NAME_MAX - i * EXFAT_ENAME_MAX) *
949 if (!next_entry(ef, dir, &cluster, &offset))
951 if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
952 co2o(ef, cluster, offset)) < 0)
954 exfat_error("failed to write name entry");
959 init_node_meta1(node, &meta1);
960 init_node_meta2(node, &meta2);
962 tree_attach(dir, node);
963 exfat_update_mtime(dir);
967 static int create(struct exfat* ef, const char* path, uint16_t attrib)
969 struct exfat_node* dir;
970 struct exfat_node* existing;
971 cluster_t cluster = EXFAT_CLUSTER_BAD;
973 le16_t name[EXFAT_NAME_MAX + 1];
976 rc = exfat_split(ef, &dir, &existing, name, path);
979 if (existing != NULL)
981 exfat_put_node(ef, existing);
982 exfat_put_node(ef, dir);
986 rc = find_slot(ef, dir, &cluster, &offset,
987 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
990 exfat_put_node(ef, dir);
993 rc = write_entry(ef, dir, name, cluster, offset, attrib);
996 exfat_put_node(ef, dir);
999 rc = exfat_flush_node(ef, dir);
1000 exfat_put_node(ef, dir);
1004 int exfat_mknod(struct exfat* ef, const char* path)
1006 return create(ef, path, EXFAT_ATTRIB_ARCH);
1009 int exfat_mkdir(struct exfat* ef, const char* path)
1012 struct exfat_node* node;
1014 rc = create(ef, path, EXFAT_ATTRIB_DIR);
1017 rc = exfat_lookup(ef, &node, path);
1020 /* directories always have at least one cluster */
1021 rc = exfat_truncate(ef, node, CLUSTER_SIZE(*ef->sb), true);
1025 exfat_put_node(ef, node);
1028 rc = exfat_flush_node(ef, node);
1032 exfat_put_node(ef, node);
1035 exfat_put_node(ef, node);
1039 static int rename_entry(struct exfat* ef, struct exfat_node* dir,
1040 struct exfat_node* node, const le16_t* name, cluster_t new_cluster,
1043 struct exfat_entry_meta1 meta1;
1044 struct exfat_entry_meta2 meta2;
1045 cluster_t old_cluster = node->entry_cluster;
1046 off_t old_offset = node->entry_offset;
1047 const size_t name_length = utf16_length(name);
1048 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
1051 if (exfat_pread(ef->dev, &meta1, sizeof(meta1),
1052 co2o(ef, old_cluster, old_offset)) < 0)
1054 exfat_error("failed to read meta1 entry on rename");
1057 if (!next_entry(ef, node->parent, &old_cluster, &old_offset))
1059 if (exfat_pread(ef->dev, &meta2, sizeof(meta2),
1060 co2o(ef, old_cluster, old_offset)) < 0)
1062 exfat_error("failed to read meta2 entry on rename");
1065 meta1.continuations = 1 + name_entries;
1066 meta2.name_hash = exfat_calc_name_hash(ef, name);
1067 meta2.name_length = name_length;
1068 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, name);
1070 if (!erase_entry(ef, node))
1073 node->entry_cluster = new_cluster;
1074 node->entry_offset = new_offset;
1076 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
1077 co2o(ef, new_cluster, new_offset)) < 0)
1079 exfat_error("failed to write meta1 entry on rename");
1082 if (!next_entry(ef, dir, &new_cluster, &new_offset))
1084 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
1085 co2o(ef, new_cluster, new_offset)) < 0)
1087 exfat_error("failed to write meta2 entry on rename");
1091 for (i = 0; i < name_entries; i++)
1093 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
1094 memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX,
1095 EXFAT_ENAME_MAX * sizeof(le16_t));
1096 if (!next_entry(ef, dir, &new_cluster, &new_offset))
1098 if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
1099 co2o(ef, new_cluster, new_offset)) < 0)
1101 exfat_error("failed to write name entry on rename");
1106 memcpy(node->name, name, (EXFAT_NAME_MAX + 1) * sizeof(le16_t));
1108 tree_attach(dir, node);
1112 int exfat_rename(struct exfat* ef, const char* old_path, const char* new_path)
1114 struct exfat_node* node;
1115 struct exfat_node* existing;
1116 struct exfat_node* dir;
1117 cluster_t cluster = EXFAT_CLUSTER_BAD;
1119 le16_t name[EXFAT_NAME_MAX + 1];
1122 rc = exfat_lookup(ef, &node, old_path);
1126 rc = exfat_split(ef, &dir, &existing, name, new_path);
1129 exfat_put_node(ef, node);
1133 /* check that target is not a subdirectory of the source */
1134 if (node->flags & EXFAT_ATTRIB_DIR)
1136 struct exfat_node* p;
1138 for (p = dir; p; p = p->parent)
1141 if (existing != NULL)
1142 exfat_put_node(ef, existing);
1143 exfat_put_node(ef, dir);
1144 exfat_put_node(ef, node);
1149 if (existing != NULL)
1151 /* remove target if it's not the same node as source */
1152 if (existing != node)
1154 if (existing->flags & EXFAT_ATTRIB_DIR)
1156 if (node->flags & EXFAT_ATTRIB_DIR)
1157 rc = exfat_rmdir(ef, existing);
1163 if (!(node->flags & EXFAT_ATTRIB_DIR))
1164 rc = exfat_unlink(ef, existing);
1168 exfat_put_node(ef, existing);
1171 /* free clusters even if something went wrong; overwise they
1172 will be just lost */
1173 exfat_cleanup_node(ef, existing);
1174 exfat_put_node(ef, dir);
1175 exfat_put_node(ef, node);
1178 rc = exfat_cleanup_node(ef, existing);
1181 exfat_put_node(ef, dir);
1182 exfat_put_node(ef, node);
1187 exfat_put_node(ef, existing);
1190 rc = find_slot(ef, dir, &cluster, &offset,
1191 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
1194 exfat_put_node(ef, dir);
1195 exfat_put_node(ef, node);
1198 rc = rename_entry(ef, dir, node, name, cluster, offset);
1199 exfat_put_node(ef, dir);
1200 exfat_put_node(ef, node);
1204 void exfat_utimes(struct exfat_node* node, const struct timespec tv[2])
1206 node->atime = tv[0].tv_sec;
1207 node->mtime = tv[1].tv_sec;
1208 node->flags |= EXFAT_ATTRIB_DIRTY;
1211 void exfat_update_atime(struct exfat_node* node)
1213 node->atime = time(NULL);
1214 node->flags |= EXFAT_ATTRIB_DIRTY;
1217 void exfat_update_mtime(struct exfat_node* node)
1219 node->mtime = time(NULL);
1220 node->flags |= EXFAT_ATTRIB_DIRTY;
1223 const char* exfat_get_label(struct exfat* ef)
1228 static int find_label(struct exfat* ef, cluster_t* cluster, off_t* offset)
1233 rc = opendir(ef, ef->root, &it);
1239 if (it.offset >= ef->root->size)
1245 if (get_entry_ptr(ef, &it)->type == EXFAT_ENTRY_LABEL)
1247 *cluster = it.cluster;
1248 *offset = it.offset;
1253 if (!fetch_next_entry(ef, ef->root, &it))
1261 int exfat_set_label(struct exfat* ef, const char* label)
1263 le16_t label_utf16[EXFAT_ENAME_MAX + 1];
1267 struct exfat_entry_label entry;
1269 memset(label_utf16, 0, sizeof(label_utf16));
1270 rc = utf8_to_utf16(label_utf16, label, EXFAT_ENAME_MAX, strlen(label));
1274 rc = find_label(ef, &cluster, &offset);
1276 rc = find_slot(ef, ef->root, &cluster, &offset, 1);
1280 entry.type = EXFAT_ENTRY_LABEL;
1281 entry.length = utf16_length(label_utf16);
1282 memcpy(entry.name, label_utf16, sizeof(entry.name));
1283 if (entry.length == 0)
1284 entry.type ^= EXFAT_ENTRY_VALID;
1286 if (exfat_pwrite(ef->dev, &entry, sizeof(struct exfat_entry_label),
1287 co2o(ef, cluster, offset)) < 0)
1289 exfat_error("failed to write label entry");
1292 strcpy(ef->label, label);