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 if (!(dir->flags & EXFAT_ATTRIB_DIR))
98 exfat_bug("not a directory");
99 it->cluster = dir->start_cluster;
101 it->chunk = malloc(CLUSTER_SIZE(*ef->sb));
102 if (it->chunk == NULL)
104 exfat_error("out of memory");
107 if (exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb),
108 exfat_c2o(ef, it->cluster)) < 0)
111 exfat_error("failed to read directory cluster %#x", it->cluster);
117 static void closedir(struct iterator* it)
125 static bool fetch_next_entry(struct exfat* ef, const struct exfat_node* parent,
128 /* move iterator to the next entry in the directory */
129 it->offset += sizeof(struct exfat_entry);
130 /* fetch the next cluster if needed */
131 if ((it->offset & (CLUSTER_SIZE(*ef->sb) - 1)) == 0)
133 /* reached the end of directory; the caller should check this
135 if (it->offset >= parent->size)
137 it->cluster = exfat_next_cluster(ef, parent, it->cluster);
138 if (CLUSTER_INVALID(it->cluster))
140 exfat_error("invalid cluster 0x%x while reading directory",
144 if (exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb),
145 exfat_c2o(ef, it->cluster)) < 0)
147 exfat_error("failed to read the next directory cluster %#x",
155 static struct exfat_node* allocate_node(void)
157 struct exfat_node* node = malloc(sizeof(struct exfat_node));
160 exfat_error("failed to allocate node");
163 memset(node, 0, sizeof(struct exfat_node));
167 static void init_node_meta1(struct exfat_node* node,
168 const struct exfat_entry_meta1* meta1)
170 node->flags = le16_to_cpu(meta1->attrib);
171 node->mtime = exfat_exfat2unix(meta1->mdate, meta1->mtime,
173 /* there is no centiseconds field for atime */
174 node->atime = exfat_exfat2unix(meta1->adate, meta1->atime, 0);
177 static void init_node_meta2(struct exfat_node* node,
178 const struct exfat_entry_meta2* meta2)
180 node->size = le64_to_cpu(meta2->size);
181 node->start_cluster = le32_to_cpu(meta2->start_cluster);
182 node->fptr_cluster = node->start_cluster;
183 if (meta2->flags & EXFAT_FLAG_CONTIGUOUS)
184 node->flags |= EXFAT_ATTRIB_CONTIGUOUS;
187 static const struct exfat_entry* get_entry_ptr(const struct exfat* ef,
188 const struct iterator* it)
190 return (const struct exfat_entry*)
191 (it->chunk + it->offset % CLUSTER_SIZE(*ef->sb));
194 static bool check_node(const struct exfat_node* node, uint16_t actual_checksum,
195 uint16_t reference_checksum, uint64_t valid_size)
197 char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
200 Validate checksum first. If it's invalid all other fields probably
201 contain just garbage.
203 if (actual_checksum != reference_checksum)
205 exfat_get_name(node, buffer, sizeof(buffer) - 1);
206 exfat_error("'%s' has invalid checksum (%#hx != %#hx)", buffer,
207 actual_checksum, reference_checksum);
212 exFAT does not support sparse files but allows files with uninitialized
213 clusters. For such files valid_size means initialized data size and
214 cannot be greater than file size. See SetFileValidData() function
217 if (valid_size > node->size)
219 exfat_get_name(node, buffer, sizeof(buffer) - 1);
220 exfat_error("'%s' has valid size (%"PRIu64") greater than size "
221 "(%"PRIu64")", buffer, valid_size, node->size);
228 static void decompress_upcase(uint16_t* output, const le16_t* source,
234 for (oi = 0; oi < EXFAT_UPCASE_CHARS; oi++)
237 for (si = 0, oi = 0; si < size && oi < EXFAT_UPCASE_CHARS; si++)
239 uint16_t ch = le16_to_cpu(source[si]);
241 if (ch == 0xffff && si + 1 < size) /* indicates a run */
242 oi += le16_to_cpu(source[++si]);
249 * Reads one entry in directory at position pointed by iterator and fills
252 static int readdir(struct exfat* ef, const struct exfat_node* parent,
253 struct exfat_node** node, struct iterator* it)
256 const struct exfat_entry* entry;
257 const struct exfat_entry_meta1* meta1;
258 const struct exfat_entry_meta2* meta2;
259 const struct exfat_entry_name* file_name;
260 const struct exfat_entry_upcase* upcase;
261 const struct exfat_entry_bitmap* bitmap;
262 const struct exfat_entry_label* label;
263 uint8_t continuations = 0;
264 le16_t* namep = NULL;
265 uint16_t reference_checksum = 0;
266 uint16_t actual_checksum = 0;
267 uint64_t valid_size = 0;
268 uint64_t upcase_size = 0;
269 le16_t* upcase_comp = NULL;
275 if (it->offset >= parent->size)
277 if (continuations != 0)
279 exfat_error("expected %hhu continuations", continuations);
282 return -ENOENT; /* that's OK, means end of directory */
285 entry = get_entry_ptr(ef, it);
288 case EXFAT_ENTRY_FILE:
289 if (continuations != 0)
291 exfat_error("expected %hhu continuations before new entry",
295 meta1 = (const struct exfat_entry_meta1*) entry;
296 continuations = meta1->continuations;
297 /* each file entry must have at least 2 continuations:
299 if (continuations < 2)
301 exfat_error("too few continuations (%hhu)", continuations);
304 if (continuations > 1 +
305 DIV_ROUND_UP(EXFAT_NAME_MAX, EXFAT_ENAME_MAX))
307 exfat_error("too many continuations (%hhu)", continuations);
310 reference_checksum = le16_to_cpu(meta1->checksum);
311 actual_checksum = exfat_start_checksum(meta1);
312 *node = allocate_node();
318 /* new node has zero reference counter */
319 (*node)->entry_cluster = it->cluster;
320 (*node)->entry_offset = it->offset;
321 init_node_meta1(*node, meta1);
322 namep = (*node)->name;
325 case EXFAT_ENTRY_FILE_INFO:
326 if (continuations < 2)
328 exfat_error("unexpected continuation (%hhu)",
332 meta2 = (const struct exfat_entry_meta2*) entry;
333 if (meta2->flags & ~(EXFAT_FLAG_ALWAYS1 | EXFAT_FLAG_CONTIGUOUS))
335 exfat_error("unknown flags in meta2 (0x%hhx)", meta2->flags);
338 init_node_meta2(*node, meta2);
339 actual_checksum = exfat_add_checksum(entry, actual_checksum);
340 valid_size = le64_to_cpu(meta2->valid_size);
341 /* empty files must be marked as non-contiguous */
342 if ((*node)->size == 0 && (meta2->flags & EXFAT_FLAG_CONTIGUOUS))
344 exfat_error("empty file marked as contiguous (0x%hhx)",
348 /* directories must be aligned on at cluster boundary */
349 if (((*node)->flags & EXFAT_ATTRIB_DIR) &&
350 (*node)->size % CLUSTER_SIZE(*ef->sb) != 0)
352 exfat_error("directory has invalid size %"PRIu64" bytes",
359 case EXFAT_ENTRY_FILE_NAME:
360 if (continuations == 0)
362 exfat_error("unexpected continuation");
365 file_name = (const struct exfat_entry_name*) entry;
366 actual_checksum = exfat_add_checksum(entry, actual_checksum);
368 memcpy(namep, file_name->name,
370 ((*node)->name + EXFAT_NAME_MAX - namep)) *
372 namep += EXFAT_ENAME_MAX;
373 if (--continuations == 0)
375 if (!check_node(*node, actual_checksum, reference_checksum,
378 if (!fetch_next_entry(ef, parent, it))
380 return 0; /* entry completed */
384 case EXFAT_ENTRY_UPCASE:
385 if (ef->upcase != NULL)
387 upcase = (const struct exfat_entry_upcase*) entry;
388 if (CLUSTER_INVALID(le32_to_cpu(upcase->start_cluster)))
390 exfat_error("invalid cluster 0x%x in upcase table",
391 le32_to_cpu(upcase->start_cluster));
394 upcase_size = le64_to_cpu(upcase->size);
395 if (upcase_size == 0 ||
396 upcase_size > EXFAT_UPCASE_CHARS * sizeof(uint16_t) ||
397 upcase_size % sizeof(uint16_t) != 0)
399 exfat_error("bad upcase table size (%"PRIu64" bytes)",
403 upcase_comp = malloc(upcase_size);
404 if (upcase_comp == NULL)
406 exfat_error("failed to allocate upcase table (%"PRIu64" bytes)",
412 /* read compressed upcase table */
413 if (exfat_pread(ef->dev, upcase_comp, upcase_size,
414 exfat_c2o(ef, le32_to_cpu(upcase->start_cluster))) < 0)
417 exfat_error("failed to read upper case table "
418 "(%"PRIu64" bytes starting at cluster %#x)",
420 le32_to_cpu(upcase->start_cluster));
424 /* decompress upcase table */
425 ef->upcase = calloc(EXFAT_UPCASE_CHARS, sizeof(uint16_t));
426 if (ef->upcase == NULL)
429 exfat_error("failed to allocate decompressed upcase table");
433 decompress_upcase(ef->upcase, upcase_comp,
434 upcase_size / sizeof(uint16_t));
438 case EXFAT_ENTRY_BITMAP:
439 bitmap = (const struct exfat_entry_bitmap*) entry;
440 ef->cmap.start_cluster = le32_to_cpu(bitmap->start_cluster);
441 if (CLUSTER_INVALID(ef->cmap.start_cluster))
443 exfat_error("invalid cluster 0x%x in clusters bitmap",
444 ef->cmap.start_cluster);
447 ef->cmap.size = le32_to_cpu(ef->sb->cluster_count) -
448 EXFAT_FIRST_DATA_CLUSTER;
449 if (le64_to_cpu(bitmap->size) < DIV_ROUND_UP(ef->cmap.size, 8))
451 exfat_error("invalid clusters bitmap size: %"PRIu64
452 " (expected at least %u)",
453 le64_to_cpu(bitmap->size),
454 DIV_ROUND_UP(ef->cmap.size, 8));
457 /* FIXME bitmap can be rather big, up to 512 MB */
458 ef->cmap.chunk_size = ef->cmap.size;
459 ef->cmap.chunk = malloc(BMAP_SIZE(ef->cmap.chunk_size));
460 if (ef->cmap.chunk == NULL)
462 exfat_error("failed to allocate clusters bitmap chunk "
463 "(%"PRIu64" bytes)", le64_to_cpu(bitmap->size));
468 if (exfat_pread(ef->dev, ef->cmap.chunk,
469 BMAP_SIZE(ef->cmap.chunk_size),
470 exfat_c2o(ef, ef->cmap.start_cluster)) < 0)
472 exfat_error("failed to read clusters bitmap "
473 "(%"PRIu64" bytes starting at cluster %#x)",
474 le64_to_cpu(bitmap->size), ef->cmap.start_cluster);
479 case EXFAT_ENTRY_LABEL:
480 label = (const struct exfat_entry_label*) entry;
481 if (label->length > EXFAT_ENAME_MAX)
483 exfat_error("too long label (%hhu chars)", label->length);
486 if (utf16_to_utf8(ef->label, label->name,
487 sizeof(ef->label) - 1, EXFAT_ENAME_MAX) != 0)
492 if (!(entry->type & EXFAT_ENTRY_VALID))
493 break; /* deleted entry, ignore it */
494 if (!(entry->type & EXFAT_ENTRY_OPTIONAL))
496 exfat_error("unknown entry type %#hhx", entry->type);
499 /* optional entry, warn and skip */
500 exfat_warn("unknown entry type %#hhx", entry->type);
501 if (continuations == 0)
503 exfat_error("unexpected continuation");
510 if (!fetch_next_entry(ef, parent, it))
513 /* we never reach here */
521 int exfat_cache_directory(struct exfat* ef, struct exfat_node* dir)
525 struct exfat_node* node;
526 struct exfat_node* current = NULL;
528 if (dir->flags & EXFAT_ATTRIB_CACHED)
529 return 0; /* already cached */
531 rc = opendir(ef, dir, &it);
534 while ((rc = readdir(ef, dir, &node, &it)) == 0)
539 current->next = node;
540 node->prev = current;
552 for (current = dir->child; current; current = node)
554 node = current->next;
561 dir->flags |= EXFAT_ATTRIB_CACHED;
565 static void tree_attach(struct exfat_node* dir, struct exfat_node* node)
570 dir->child->prev = node;
571 node->next = dir->child;
576 static void tree_detach(struct exfat_node* node)
579 node->prev->next = node->next;
580 else /* this is the first node in the list */
581 node->parent->child = node->next;
583 node->next->prev = node->prev;
589 static void reset_cache(struct exfat* ef, struct exfat_node* node)
591 char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
595 struct exfat_node* p = node->child;
600 node->flags &= ~EXFAT_ATTRIB_CACHED;
601 if (node->references != 0)
603 exfat_get_name(node, buffer, sizeof(buffer) - 1);
604 exfat_warn("non-zero reference counter (%d) for '%s'",
605 node->references, buffer);
607 if (node != ef->root && (node->flags & EXFAT_ATTRIB_DIRTY))
609 exfat_get_name(node, buffer, sizeof(buffer) - 1);
610 exfat_bug("node '%s' is dirty", buffer);
612 while (node->references)
613 exfat_put_node(ef, node);
616 void exfat_reset_cache(struct exfat* ef)
618 reset_cache(ef, ef->root);
621 static bool next_entry(struct exfat* ef, const struct exfat_node* parent,
622 cluster_t* cluster, off_t* offset)
624 *offset += sizeof(struct exfat_entry);
625 if (*offset % CLUSTER_SIZE(*ef->sb) == 0)
627 *cluster = exfat_next_cluster(ef, parent, *cluster);
628 if (CLUSTER_INVALID(*cluster))
630 exfat_error("invalid cluster %#x while getting next entry",
638 int exfat_flush_node(struct exfat* ef, struct exfat_node* node)
642 off_t meta1_offset, meta2_offset;
643 struct exfat_entry_meta1 meta1;
644 struct exfat_entry_meta2 meta2;
646 if (!(node->flags & EXFAT_ATTRIB_DIRTY))
647 return 0; /* no need to flush */
650 exfat_bug("unable to flush node to read-only FS");
652 if (node->parent == NULL)
653 return 0; /* do not flush unlinked node */
655 cluster = node->entry_cluster;
656 offset = node->entry_offset;
657 meta1_offset = co2o(ef, cluster, offset);
658 if (!next_entry(ef, node->parent, &cluster, &offset))
660 meta2_offset = co2o(ef, cluster, offset);
662 if (exfat_pread(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
664 exfat_error("failed to read meta1 entry on flush");
667 if (meta1.type != EXFAT_ENTRY_FILE)
668 exfat_bug("invalid type of meta1: 0x%hhx", meta1.type);
669 meta1.attrib = cpu_to_le16(node->flags);
670 exfat_unix2exfat(node->mtime, &meta1.mdate, &meta1.mtime, &meta1.mtime_cs);
671 exfat_unix2exfat(node->atime, &meta1.adate, &meta1.atime, NULL);
673 if (exfat_pread(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
675 exfat_error("failed to read meta2 entry on flush");
678 if (meta2.type != EXFAT_ENTRY_FILE_INFO)
679 exfat_bug("invalid type of meta2: 0x%hhx", meta2.type);
680 meta2.size = meta2.valid_size = cpu_to_le64(node->size);
681 meta2.start_cluster = cpu_to_le32(node->start_cluster);
682 meta2.flags = EXFAT_FLAG_ALWAYS1;
683 /* empty files must not be marked as contiguous */
684 if (node->size != 0 && IS_CONTIGUOUS(*node))
685 meta2.flags |= EXFAT_FLAG_CONTIGUOUS;
686 /* name hash remains unchanged, no need to recalculate it */
688 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);
690 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
692 exfat_error("failed to write meta1 entry on flush");
695 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
697 exfat_error("failed to write meta2 entry on flush");
701 node->flags &= ~EXFAT_ATTRIB_DIRTY;
702 return exfat_flush(ef);
705 static bool erase_entry(struct exfat* ef, struct exfat_node* node)
707 cluster_t cluster = node->entry_cluster;
708 off_t offset = node->entry_offset;
709 int name_entries = DIV_ROUND_UP(utf16_length(node->name), EXFAT_ENAME_MAX);
712 entry_type = EXFAT_ENTRY_FILE & ~EXFAT_ENTRY_VALID;
713 if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
715 exfat_error("failed to erase meta1 entry");
719 if (!next_entry(ef, node->parent, &cluster, &offset))
721 entry_type = EXFAT_ENTRY_FILE_INFO & ~EXFAT_ENTRY_VALID;
722 if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
724 exfat_error("failed to erase meta2 entry");
728 while (name_entries--)
730 if (!next_entry(ef, node->parent, &cluster, &offset))
732 entry_type = EXFAT_ENTRY_FILE_NAME & ~EXFAT_ENTRY_VALID;
733 if (exfat_pwrite(ef->dev, &entry_type, 1,
734 co2o(ef, cluster, offset)) < 0)
736 exfat_error("failed to erase name entry");
743 static int shrink_directory(struct exfat* ef, struct exfat_node* dir,
744 off_t deleted_offset)
746 const struct exfat_node* node;
747 const struct exfat_node* last_node;
748 uint64_t entries = 0;
751 if (!(dir->flags & EXFAT_ATTRIB_DIR))
752 exfat_bug("attempted to shrink a file");
753 if (!(dir->flags & EXFAT_ATTRIB_CACHED))
754 exfat_bug("attempted to shrink uncached directory");
756 for (last_node = node = dir->child; node; node = node->next)
758 if (deleted_offset < node->entry_offset)
760 /* there are other entries after the removed one, no way to shrink
764 if (last_node->entry_offset < node->entry_offset)
770 /* offset of the last entry */
771 entries += last_node->entry_offset / sizeof(struct exfat_entry);
772 /* two subentries with meta info */
774 /* subentries with file name */
775 entries += DIV_ROUND_UP(utf16_length(last_node->name),
779 new_size = DIV_ROUND_UP(entries * sizeof(struct exfat_entry),
780 CLUSTER_SIZE(*ef->sb)) * CLUSTER_SIZE(*ef->sb);
781 if (new_size == 0) /* directory always has at least 1 cluster */
782 new_size = CLUSTER_SIZE(*ef->sb);
783 if (new_size == dir->size)
785 return exfat_truncate(ef, dir, new_size, true);
788 static int delete(struct exfat* ef, struct exfat_node* node)
790 struct exfat_node* parent = node->parent;
791 off_t deleted_offset = node->entry_offset;
794 exfat_get_node(parent);
795 if (!erase_entry(ef, node))
797 exfat_put_node(ef, parent);
800 exfat_update_mtime(parent);
802 rc = shrink_directory(ef, parent, deleted_offset);
803 node->flags |= EXFAT_ATTRIB_UNLINKED;
806 exfat_flush_node(ef, parent);
807 exfat_put_node(ef, parent);
810 rc = exfat_flush_node(ef, parent);
811 exfat_put_node(ef, parent);
815 int exfat_unlink(struct exfat* ef, struct exfat_node* node)
817 if (node->flags & EXFAT_ATTRIB_DIR)
819 return delete(ef, node);
822 int exfat_rmdir(struct exfat* ef, struct exfat_node* node)
826 if (!(node->flags & EXFAT_ATTRIB_DIR))
828 /* check that directory is empty */
829 rc = exfat_cache_directory(ef, node);
834 return delete(ef, node);
837 static int grow_directory(struct exfat* ef, struct exfat_node* dir,
838 uint64_t asize, uint32_t difference)
840 return exfat_truncate(ef, dir,
841 DIV_ROUND_UP(asize + difference, CLUSTER_SIZE(*ef->sb))
842 * CLUSTER_SIZE(*ef->sb), true);
845 static int find_slot(struct exfat* ef, struct exfat_node* dir,
846 cluster_t* cluster, off_t* offset, int subentries)
850 const struct exfat_entry* entry;
853 rc = opendir(ef, dir, &it);
860 *cluster = it.cluster;
863 entry = get_entry_ptr(ef, &it);
864 if (entry->type & EXFAT_ENTRY_VALID)
868 if (contiguous == subentries)
869 break; /* suitable slot is found */
870 if (it.offset + sizeof(struct exfat_entry) >= dir->size)
872 rc = grow_directory(ef, dir, dir->size,
873 (subentries - contiguous) * sizeof(struct exfat_entry));
880 if (!fetch_next_entry(ef, dir, &it))
890 static int write_entry(struct exfat* ef, struct exfat_node* dir,
891 const le16_t* name, cluster_t cluster, off_t offset, uint16_t attrib)
893 struct exfat_node* node;
894 struct exfat_entry_meta1 meta1;
895 struct exfat_entry_meta2 meta2;
896 const size_t name_length = utf16_length(name);
897 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
900 node = allocate_node();
903 node->entry_cluster = cluster;
904 node->entry_offset = offset;
905 memcpy(node->name, name, name_length * sizeof(le16_t));
907 memset(&meta1, 0, sizeof(meta1));
908 meta1.type = EXFAT_ENTRY_FILE;
909 meta1.continuations = 1 + name_entries;
910 meta1.attrib = cpu_to_le16(attrib);
911 exfat_unix2exfat(time(NULL), &meta1.crdate, &meta1.crtime,
913 meta1.adate = meta1.mdate = meta1.crdate;
914 meta1.atime = meta1.mtime = meta1.crtime;
915 meta1.mtime_cs = meta1.crtime_cs; /* there is no atime_cs */
917 memset(&meta2, 0, sizeof(meta2));
918 meta2.type = EXFAT_ENTRY_FILE_INFO;
919 meta2.flags = EXFAT_FLAG_ALWAYS1;
920 meta2.name_length = name_length;
921 meta2.name_hash = exfat_calc_name_hash(ef, node->name);
922 meta2.start_cluster = cpu_to_le32(EXFAT_CLUSTER_FREE);
924 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);
926 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
927 co2o(ef, cluster, offset)) < 0)
929 exfat_error("failed to write meta1 entry");
932 if (!next_entry(ef, dir, &cluster, &offset))
934 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
935 co2o(ef, cluster, offset)) < 0)
937 exfat_error("failed to write meta2 entry");
940 for (i = 0; i < name_entries; i++)
942 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
943 memcpy(name_entry.name, node->name + i * EXFAT_ENAME_MAX,
944 MIN(EXFAT_ENAME_MAX, EXFAT_NAME_MAX - i * EXFAT_ENAME_MAX) *
946 if (!next_entry(ef, dir, &cluster, &offset))
948 if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
949 co2o(ef, cluster, offset)) < 0)
951 exfat_error("failed to write name entry");
956 init_node_meta1(node, &meta1);
957 init_node_meta2(node, &meta2);
959 tree_attach(dir, node);
960 exfat_update_mtime(dir);
964 static int create(struct exfat* ef, const char* path, uint16_t attrib)
966 struct exfat_node* dir;
967 struct exfat_node* existing;
968 cluster_t cluster = EXFAT_CLUSTER_BAD;
970 le16_t name[EXFAT_NAME_MAX + 1];
973 rc = exfat_split(ef, &dir, &existing, name, path);
976 if (existing != NULL)
978 exfat_put_node(ef, existing);
979 exfat_put_node(ef, dir);
983 rc = find_slot(ef, dir, &cluster, &offset,
984 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
987 exfat_put_node(ef, dir);
990 rc = write_entry(ef, dir, name, cluster, offset, attrib);
993 exfat_put_node(ef, dir);
996 rc = exfat_flush_node(ef, dir);
997 exfat_put_node(ef, dir);
1001 int exfat_mknod(struct exfat* ef, const char* path)
1003 return create(ef, path, EXFAT_ATTRIB_ARCH);
1006 int exfat_mkdir(struct exfat* ef, const char* path)
1009 struct exfat_node* node;
1011 rc = create(ef, path, EXFAT_ATTRIB_DIR);
1014 rc = exfat_lookup(ef, &node, path);
1017 /* directories always have at least one cluster */
1018 rc = exfat_truncate(ef, node, CLUSTER_SIZE(*ef->sb), true);
1022 exfat_put_node(ef, node);
1025 rc = exfat_flush_node(ef, node);
1029 exfat_put_node(ef, node);
1032 exfat_put_node(ef, node);
1036 static int rename_entry(struct exfat* ef, struct exfat_node* dir,
1037 struct exfat_node* node, const le16_t* name, cluster_t new_cluster,
1040 struct exfat_entry_meta1 meta1;
1041 struct exfat_entry_meta2 meta2;
1042 cluster_t old_cluster = node->entry_cluster;
1043 off_t old_offset = node->entry_offset;
1044 const size_t name_length = utf16_length(name);
1045 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
1048 if (exfat_pread(ef->dev, &meta1, sizeof(meta1),
1049 co2o(ef, old_cluster, old_offset)) < 0)
1051 exfat_error("failed to read meta1 entry on rename");
1054 if (!next_entry(ef, node->parent, &old_cluster, &old_offset))
1056 if (exfat_pread(ef->dev, &meta2, sizeof(meta2),
1057 co2o(ef, old_cluster, old_offset)) < 0)
1059 exfat_error("failed to read meta2 entry on rename");
1062 meta1.continuations = 1 + name_entries;
1063 meta2.name_hash = exfat_calc_name_hash(ef, name);
1064 meta2.name_length = name_length;
1065 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, name);
1067 if (!erase_entry(ef, node))
1070 node->entry_cluster = new_cluster;
1071 node->entry_offset = new_offset;
1073 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
1074 co2o(ef, new_cluster, new_offset)) < 0)
1076 exfat_error("failed to write meta1 entry on rename");
1079 if (!next_entry(ef, dir, &new_cluster, &new_offset))
1081 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
1082 co2o(ef, new_cluster, new_offset)) < 0)
1084 exfat_error("failed to write meta2 entry on rename");
1088 for (i = 0; i < name_entries; i++)
1090 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
1091 memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX,
1092 EXFAT_ENAME_MAX * sizeof(le16_t));
1093 if (!next_entry(ef, dir, &new_cluster, &new_offset))
1095 if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
1096 co2o(ef, new_cluster, new_offset)) < 0)
1098 exfat_error("failed to write name entry on rename");
1103 memcpy(node->name, name, (EXFAT_NAME_MAX + 1) * sizeof(le16_t));
1105 tree_attach(dir, node);
1109 int exfat_rename(struct exfat* ef, const char* old_path, const char* new_path)
1111 struct exfat_node* node;
1112 struct exfat_node* existing;
1113 struct exfat_node* dir;
1114 cluster_t cluster = EXFAT_CLUSTER_BAD;
1116 le16_t name[EXFAT_NAME_MAX + 1];
1119 rc = exfat_lookup(ef, &node, old_path);
1123 rc = exfat_split(ef, &dir, &existing, name, new_path);
1126 exfat_put_node(ef, node);
1130 /* check that target is not a subdirectory of the source */
1131 if (node->flags & EXFAT_ATTRIB_DIR)
1133 struct exfat_node* p;
1135 for (p = dir; p; p = p->parent)
1138 if (existing != NULL)
1139 exfat_put_node(ef, existing);
1140 exfat_put_node(ef, dir);
1141 exfat_put_node(ef, node);
1146 if (existing != NULL)
1148 /* remove target if it's not the same node as source */
1149 if (existing != node)
1151 if (existing->flags & EXFAT_ATTRIB_DIR)
1153 if (node->flags & EXFAT_ATTRIB_DIR)
1154 rc = exfat_rmdir(ef, existing);
1160 if (!(node->flags & EXFAT_ATTRIB_DIR))
1161 rc = exfat_unlink(ef, existing);
1165 exfat_put_node(ef, existing);
1168 /* free clusters even if something went wrong; overwise they
1169 will be just lost */
1170 exfat_cleanup_node(ef, existing);
1171 exfat_put_node(ef, dir);
1172 exfat_put_node(ef, node);
1175 rc = exfat_cleanup_node(ef, existing);
1178 exfat_put_node(ef, dir);
1179 exfat_put_node(ef, node);
1184 exfat_put_node(ef, existing);
1187 rc = find_slot(ef, dir, &cluster, &offset,
1188 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
1191 exfat_put_node(ef, dir);
1192 exfat_put_node(ef, node);
1195 rc = rename_entry(ef, dir, node, name, cluster, offset);
1196 exfat_put_node(ef, dir);
1197 exfat_put_node(ef, node);
1201 void exfat_utimes(struct exfat_node* node, const struct timespec tv[2])
1203 node->atime = tv[0].tv_sec;
1204 node->mtime = tv[1].tv_sec;
1205 node->flags |= EXFAT_ATTRIB_DIRTY;
1208 void exfat_update_atime(struct exfat_node* node)
1210 node->atime = time(NULL);
1211 node->flags |= EXFAT_ATTRIB_DIRTY;
1214 void exfat_update_mtime(struct exfat_node* node)
1216 node->mtime = time(NULL);
1217 node->flags |= EXFAT_ATTRIB_DIRTY;
1220 const char* exfat_get_label(struct exfat* ef)
1225 static int find_label(struct exfat* ef, cluster_t* cluster, off_t* offset)
1230 rc = opendir(ef, ef->root, &it);
1236 if (it.offset >= ef->root->size)
1242 if (get_entry_ptr(ef, &it)->type == EXFAT_ENTRY_LABEL)
1244 *cluster = it.cluster;
1245 *offset = it.offset;
1250 if (!fetch_next_entry(ef, ef->root, &it))
1258 int exfat_set_label(struct exfat* ef, const char* label)
1260 le16_t label_utf16[EXFAT_ENAME_MAX + 1];
1264 struct exfat_entry_label entry;
1266 memset(label_utf16, 0, sizeof(label_utf16));
1267 rc = utf8_to_utf16(label_utf16, label, EXFAT_ENAME_MAX, strlen(label));
1271 rc = find_label(ef, &cluster, &offset);
1273 rc = find_slot(ef, ef->root, &cluster, &offset, 1);
1277 entry.type = EXFAT_ENTRY_LABEL;
1278 entry.length = utf16_length(label_utf16);
1279 memcpy(entry.name, label_utf16, sizeof(entry.name));
1280 if (entry.length == 0)
1281 entry.type ^= EXFAT_ENTRY_VALID;
1283 if (exfat_pwrite(ef->dev, &entry, sizeof(struct exfat_entry_label),
1284 co2o(ef, cluster, offset)) < 0)
1286 exfat_error("failed to write label entry");
1289 strcpy(ef->label, label);