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[EXFAT_UTF8_NAME_BUFFER_MAX];
49 if (node->references < 0)
51 exfat_get_name(node, buffer);
52 exfat_bug("reference counter of '%s' is below zero", buffer);
54 else if (node->references == 0 && node != ef->root)
58 exfat_get_name(node, buffer);
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->is_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[EXFAT_UTF8_NAME_BUFFER_MAX];
99 if (!(dir->flags & EXFAT_ATTRIB_DIR))
101 exfat_get_name(dir, buffer);
102 exfat_bug("'%s' is not a directory", buffer);
104 if (CLUSTER_INVALID(dir->start_cluster))
106 exfat_get_name(dir, buffer);
107 exfat_error("'%s' directory starts with invalid cluster %#x", buffer,
111 it->cluster = dir->start_cluster;
113 it->chunk = malloc(CLUSTER_SIZE(*ef->sb));
114 if (it->chunk == NULL)
116 exfat_error("failed to allocate memory for directory cluster");
119 if (exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb),
120 exfat_c2o(ef, it->cluster)) < 0)
123 exfat_get_name(dir, buffer);
124 exfat_error("failed to read '%s' directory cluster %#x", buffer,
131 static void closedir(struct iterator* it)
139 static bool fetch_next_entry(struct exfat* ef, const struct exfat_node* parent,
142 /* move iterator to the next entry in the directory */
143 it->offset += sizeof(struct exfat_entry);
144 /* fetch the next cluster if needed */
145 if ((it->offset & (CLUSTER_SIZE(*ef->sb) - 1)) == 0)
147 /* reached the end of directory; the caller should check this
149 if (it->offset >= parent->size)
151 it->cluster = exfat_next_cluster(ef, parent, it->cluster);
152 if (CLUSTER_INVALID(it->cluster))
154 exfat_error("invalid cluster 0x%x while reading directory",
158 if (exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb),
159 exfat_c2o(ef, it->cluster)) < 0)
161 exfat_error("failed to read the next directory cluster %#x",
169 static struct exfat_node* allocate_node(void)
171 struct exfat_node* node = malloc(sizeof(struct exfat_node));
174 exfat_error("failed to allocate node");
177 memset(node, 0, sizeof(struct exfat_node));
181 static void init_node_meta1(struct exfat_node* node,
182 const struct exfat_entry_meta1* meta1)
184 node->flags = le16_to_cpu(meta1->attrib);
185 node->mtime = exfat_exfat2unix(meta1->mdate, meta1->mtime,
187 /* there is no centiseconds field for atime */
188 node->atime = exfat_exfat2unix(meta1->adate, meta1->atime, 0);
191 static void init_node_meta2(struct exfat_node* node,
192 const struct exfat_entry_meta2* meta2)
194 node->size = le64_to_cpu(meta2->size);
195 node->start_cluster = le32_to_cpu(meta2->start_cluster);
196 node->fptr_cluster = node->start_cluster;
197 node->is_contiguous = ((meta2->flags & EXFAT_FLAG_CONTIGUOUS) != 0);
200 static const struct exfat_entry* get_entry_ptr(const struct exfat* ef,
201 const struct iterator* it)
203 return (const struct exfat_entry*)
204 (it->chunk + it->offset % CLUSTER_SIZE(*ef->sb));
207 static bool check_node(const struct exfat_node* node, uint16_t actual_checksum,
208 uint16_t reference_checksum, uint64_t valid_size, int cluster_size)
210 char buffer[EXFAT_UTF8_NAME_BUFFER_MAX];
214 Validate checksum first. If it's invalid all other fields probably
215 contain just garbage.
217 if (actual_checksum != reference_checksum)
219 exfat_get_name(node, buffer);
220 exfat_error("'%s' has invalid checksum (%#hx != %#hx)", buffer,
221 actual_checksum, reference_checksum);
226 exFAT does not support sparse files but allows files with uninitialized
227 clusters. For such files valid_size means initialized data size and
228 cannot be greater than file size. See SetFileValidData() function
231 if (valid_size > node->size)
233 exfat_get_name(node, buffer);
234 exfat_error("'%s' has valid size (%"PRIu64") greater than size "
235 "(%"PRIu64")", buffer, valid_size, node->size);
240 Empty file must have zero start cluster. Non-empty file must start
241 with a valid cluster. Directories cannot be empty (i.e. must always
242 have a valid start cluster), but we will check this later in opendir()
243 to give user a chance to read current directory.
245 if (node->size == 0 && node->start_cluster != EXFAT_CLUSTER_FREE)
247 exfat_get_name(node, buffer);
248 exfat_error("'%s' is empty but start cluster is %#x", buffer,
249 node->start_cluster);
252 if (node->size > 0 && CLUSTER_INVALID(node->start_cluster))
254 exfat_get_name(node, buffer);
255 exfat_error("'%s' points to invalid cluster %#x", buffer,
256 node->start_cluster);
260 /* Empty file or directory must be marked as non-contiguous. */
261 if (node->size == 0 && node->is_contiguous)
263 exfat_get_name(node, buffer);
264 exfat_error("'%s' is empty but marked as contiguous (%#x)", buffer,
269 /* Directory size must be aligned on at cluster boundary. */
270 if ((node->flags & EXFAT_ATTRIB_DIR) && node->size % cluster_size != 0)
272 exfat_get_name(node, buffer);
273 exfat_error("'%s' directory size %"PRIu64" is not divisible by %d", buffer,
274 node->size, cluster_size);
281 static void decompress_upcase(uint16_t* output, const le16_t* source,
287 for (oi = 0; oi < EXFAT_UPCASE_CHARS; oi++)
290 for (si = 0, oi = 0; si < size && oi < EXFAT_UPCASE_CHARS; si++)
292 uint16_t ch = le16_to_cpu(source[si]);
294 if (ch == 0xffff && si + 1 < size) /* indicates a run */
295 oi += le16_to_cpu(source[++si]);
302 * Reads one entry in directory at position pointed by iterator and fills
305 static int readdir(struct exfat* ef, const struct exfat_node* parent,
306 struct exfat_node** node, struct iterator* it)
309 const struct exfat_entry* entry;
310 const struct exfat_entry_meta1* meta1;
311 const struct exfat_entry_meta2* meta2;
312 const struct exfat_entry_name* file_name;
313 const struct exfat_entry_upcase* upcase;
314 const struct exfat_entry_bitmap* bitmap;
315 const struct exfat_entry_label* label;
316 uint8_t continuations = 0;
317 le16_t* namep = NULL;
318 uint16_t reference_checksum = 0;
319 uint16_t actual_checksum = 0;
320 uint64_t valid_size = 0;
321 uint64_t upcase_size = 0;
322 le16_t* upcase_comp = NULL;
328 if (it->offset >= parent->size)
330 if (continuations != 0)
332 exfat_error("expected %hhu continuations", continuations);
335 return -ENOENT; /* that's OK, means end of directory */
338 entry = get_entry_ptr(ef, it);
341 case EXFAT_ENTRY_FILE:
342 if (continuations != 0)
344 exfat_error("expected %hhu continuations before new entry",
348 meta1 = (const struct exfat_entry_meta1*) entry;
349 continuations = meta1->continuations;
350 /* each file entry must have at least 2 continuations:
352 if (continuations < 2)
354 exfat_error("too few continuations (%hhu)", continuations);
357 if (continuations > 1 +
358 DIV_ROUND_UP(EXFAT_NAME_MAX, EXFAT_ENAME_MAX))
360 exfat_error("too many continuations (%hhu)", continuations);
363 reference_checksum = le16_to_cpu(meta1->checksum);
364 actual_checksum = exfat_start_checksum(meta1);
365 *node = allocate_node();
371 /* new node has zero reference counter */
372 (*node)->entry_cluster = it->cluster;
373 (*node)->entry_offset = it->offset;
374 init_node_meta1(*node, meta1);
375 namep = (*node)->name;
378 case EXFAT_ENTRY_FILE_INFO:
379 if (continuations < 2)
381 exfat_error("unexpected continuation (%hhu)",
385 meta2 = (const struct exfat_entry_meta2*) entry;
386 if (meta2->flags & ~(EXFAT_FLAG_ALWAYS1 | EXFAT_FLAG_CONTIGUOUS))
388 exfat_error("unknown flags in meta2 (0x%hhx)", meta2->flags);
391 init_node_meta2(*node, meta2);
392 actual_checksum = exfat_add_checksum(entry, actual_checksum);
393 valid_size = le64_to_cpu(meta2->valid_size);
397 case EXFAT_ENTRY_FILE_NAME:
398 if (continuations == 0)
400 exfat_error("unexpected continuation");
403 file_name = (const struct exfat_entry_name*) entry;
404 actual_checksum = exfat_add_checksum(entry, actual_checksum);
406 memcpy(namep, file_name->name,
408 ((*node)->name + EXFAT_NAME_MAX - namep)) *
410 namep += EXFAT_ENAME_MAX;
411 if (--continuations == 0)
413 if (!check_node(*node, actual_checksum, reference_checksum,
414 valid_size, CLUSTER_SIZE(*ef->sb)))
416 if (!fetch_next_entry(ef, parent, it))
418 return 0; /* entry completed */
422 case EXFAT_ENTRY_UPCASE:
423 if (ef->upcase != NULL)
425 upcase = (const struct exfat_entry_upcase*) entry;
426 if (CLUSTER_INVALID(le32_to_cpu(upcase->start_cluster)))
428 exfat_error("invalid cluster 0x%x in upcase table",
429 le32_to_cpu(upcase->start_cluster));
432 upcase_size = le64_to_cpu(upcase->size);
433 if (upcase_size == 0 ||
434 upcase_size > EXFAT_UPCASE_CHARS * sizeof(uint16_t) ||
435 upcase_size % sizeof(uint16_t) != 0)
437 exfat_error("bad upcase table size (%"PRIu64" bytes)",
441 upcase_comp = malloc(upcase_size);
442 if (upcase_comp == NULL)
444 exfat_error("failed to allocate upcase table (%"PRIu64" bytes)",
450 /* read compressed upcase table */
451 if (exfat_pread(ef->dev, upcase_comp, upcase_size,
452 exfat_c2o(ef, le32_to_cpu(upcase->start_cluster))) < 0)
455 exfat_error("failed to read upper case table "
456 "(%"PRIu64" bytes starting at cluster %#x)",
458 le32_to_cpu(upcase->start_cluster));
462 /* decompress upcase table */
463 ef->upcase = calloc(EXFAT_UPCASE_CHARS, sizeof(uint16_t));
464 if (ef->upcase == NULL)
467 exfat_error("failed to allocate decompressed upcase table");
471 decompress_upcase(ef->upcase, upcase_comp,
472 upcase_size / sizeof(uint16_t));
476 case EXFAT_ENTRY_BITMAP:
477 bitmap = (const struct exfat_entry_bitmap*) entry;
478 ef->cmap.start_cluster = le32_to_cpu(bitmap->start_cluster);
479 if (CLUSTER_INVALID(ef->cmap.start_cluster))
481 exfat_error("invalid cluster 0x%x in clusters bitmap",
482 ef->cmap.start_cluster);
485 ef->cmap.size = le32_to_cpu(ef->sb->cluster_count) -
486 EXFAT_FIRST_DATA_CLUSTER;
487 if (le64_to_cpu(bitmap->size) < DIV_ROUND_UP(ef->cmap.size, 8))
489 exfat_error("invalid clusters bitmap size: %"PRIu64
490 " (expected at least %u)",
491 le64_to_cpu(bitmap->size),
492 DIV_ROUND_UP(ef->cmap.size, 8));
495 /* FIXME bitmap can be rather big, up to 512 MB */
496 ef->cmap.chunk_size = ef->cmap.size;
497 ef->cmap.chunk = malloc(BMAP_SIZE(ef->cmap.chunk_size));
498 if (ef->cmap.chunk == NULL)
500 exfat_error("failed to allocate clusters bitmap chunk "
501 "(%"PRIu64" bytes)", le64_to_cpu(bitmap->size));
506 if (exfat_pread(ef->dev, ef->cmap.chunk,
507 BMAP_SIZE(ef->cmap.chunk_size),
508 exfat_c2o(ef, ef->cmap.start_cluster)) < 0)
510 exfat_error("failed to read clusters bitmap "
511 "(%"PRIu64" bytes starting at cluster %#x)",
512 le64_to_cpu(bitmap->size), ef->cmap.start_cluster);
517 case EXFAT_ENTRY_LABEL:
518 label = (const struct exfat_entry_label*) entry;
519 if (label->length > EXFAT_ENAME_MAX)
521 exfat_error("too long label (%hhu chars)", label->length);
524 if (utf16_to_utf8(ef->label, label->name,
525 sizeof(ef->label), EXFAT_ENAME_MAX) != 0)
530 if (!(entry->type & EXFAT_ENTRY_VALID))
531 break; /* deleted entry, ignore it */
532 if (!(entry->type & EXFAT_ENTRY_OPTIONAL))
534 exfat_error("unknown entry type %#hhx", entry->type);
537 /* optional entry, warn and skip */
538 exfat_warn("unknown entry type %#hhx", entry->type);
539 if (continuations == 0)
541 exfat_error("unexpected continuation");
548 if (!fetch_next_entry(ef, parent, it))
551 /* we never reach here */
559 int exfat_cache_directory(struct exfat* ef, struct exfat_node* dir)
563 struct exfat_node* node;
564 struct exfat_node* current = NULL;
567 return 0; /* already cached */
569 rc = opendir(ef, dir, &it);
572 while ((rc = readdir(ef, dir, &node, &it)) == 0)
577 current->next = node;
578 node->prev = current;
590 for (current = dir->child; current; current = node)
592 node = current->next;
599 dir->is_cached = true;
603 static void tree_attach(struct exfat_node* dir, struct exfat_node* node)
608 dir->child->prev = node;
609 node->next = dir->child;
614 static void tree_detach(struct exfat_node* node)
617 node->prev->next = node->next;
618 else /* this is the first node in the list */
619 node->parent->child = node->next;
621 node->next->prev = node->prev;
627 static void reset_cache(struct exfat* ef, struct exfat_node* node)
629 char buffer[EXFAT_UTF8_NAME_BUFFER_MAX];
633 struct exfat_node* p = node->child;
638 node->is_cached = false;
639 if (node->references != 0)
641 exfat_get_name(node, buffer);
642 exfat_warn("non-zero reference counter (%d) for '%s'",
643 node->references, buffer);
645 if (node != ef->root && node->is_dirty)
647 exfat_get_name(node, buffer);
648 exfat_bug("node '%s' is dirty", buffer);
650 while (node->references)
651 exfat_put_node(ef, node);
654 void exfat_reset_cache(struct exfat* ef)
656 reset_cache(ef, ef->root);
659 static bool next_entry(struct exfat* ef, const struct exfat_node* parent,
660 cluster_t* cluster, off_t* offset)
662 *offset += sizeof(struct exfat_entry);
663 if (*offset % CLUSTER_SIZE(*ef->sb) == 0)
665 *cluster = exfat_next_cluster(ef, parent, *cluster);
666 if (CLUSTER_INVALID(*cluster))
668 exfat_error("invalid cluster %#x while getting next entry",
676 int exfat_flush_node(struct exfat* ef, struct exfat_node* node)
680 off_t meta1_offset, meta2_offset;
681 struct exfat_entry_meta1 meta1;
682 struct exfat_entry_meta2 meta2;
685 return 0; /* no need to flush */
688 exfat_bug("unable to flush node to read-only FS");
690 if (node->parent == NULL)
691 return 0; /* do not flush unlinked node */
693 cluster = node->entry_cluster;
694 offset = node->entry_offset;
695 meta1_offset = co2o(ef, cluster, offset);
696 if (!next_entry(ef, node->parent, &cluster, &offset))
698 meta2_offset = co2o(ef, cluster, offset);
700 if (exfat_pread(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
702 exfat_error("failed to read meta1 entry on flush");
705 if (meta1.type != EXFAT_ENTRY_FILE)
706 exfat_bug("invalid type of meta1: 0x%hhx", meta1.type);
707 meta1.attrib = cpu_to_le16(node->flags);
708 exfat_unix2exfat(node->mtime, &meta1.mdate, &meta1.mtime, &meta1.mtime_cs);
709 exfat_unix2exfat(node->atime, &meta1.adate, &meta1.atime, NULL);
711 if (exfat_pread(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
713 exfat_error("failed to read meta2 entry on flush");
716 if (meta2.type != EXFAT_ENTRY_FILE_INFO)
717 exfat_bug("invalid type of meta2: 0x%hhx", meta2.type);
718 meta2.size = meta2.valid_size = cpu_to_le64(node->size);
719 meta2.start_cluster = cpu_to_le32(node->start_cluster);
720 meta2.flags = EXFAT_FLAG_ALWAYS1;
721 /* empty files must not be marked as contiguous */
722 if (node->size != 0 && node->is_contiguous)
723 meta2.flags |= EXFAT_FLAG_CONTIGUOUS;
724 /* name hash remains unchanged, no need to recalculate it */
726 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);
728 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
730 exfat_error("failed to write meta1 entry on flush");
733 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
735 exfat_error("failed to write meta2 entry on flush");
739 node->is_dirty = false;
740 return exfat_flush(ef);
743 static bool erase_entry(struct exfat* ef, struct exfat_node* node)
745 cluster_t cluster = node->entry_cluster;
746 off_t offset = node->entry_offset;
747 int name_entries = DIV_ROUND_UP(utf16_length(node->name), EXFAT_ENAME_MAX);
750 entry_type = EXFAT_ENTRY_FILE & ~EXFAT_ENTRY_VALID;
751 if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
753 exfat_error("failed to erase meta1 entry");
757 if (!next_entry(ef, node->parent, &cluster, &offset))
759 entry_type = EXFAT_ENTRY_FILE_INFO & ~EXFAT_ENTRY_VALID;
760 if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
762 exfat_error("failed to erase meta2 entry");
766 while (name_entries--)
768 if (!next_entry(ef, node->parent, &cluster, &offset))
770 entry_type = EXFAT_ENTRY_FILE_NAME & ~EXFAT_ENTRY_VALID;
771 if (exfat_pwrite(ef->dev, &entry_type, 1,
772 co2o(ef, cluster, offset)) < 0)
774 exfat_error("failed to erase name entry");
781 static int shrink_directory(struct exfat* ef, struct exfat_node* dir,
782 off_t deleted_offset)
784 const struct exfat_node* node;
785 const struct exfat_node* last_node;
786 uint64_t entries = 0;
789 if (!(dir->flags & EXFAT_ATTRIB_DIR))
790 exfat_bug("attempted to shrink a file");
792 exfat_bug("attempted to shrink uncached directory");
794 for (last_node = node = dir->child; node; node = node->next)
796 if (deleted_offset < node->entry_offset)
798 /* there are other entries after the removed one, no way to shrink
802 if (last_node->entry_offset < node->entry_offset)
808 /* offset of the last entry */
809 entries += last_node->entry_offset / sizeof(struct exfat_entry);
810 /* two subentries with meta info */
812 /* subentries with file name */
813 entries += DIV_ROUND_UP(utf16_length(last_node->name),
817 new_size = DIV_ROUND_UP(entries * sizeof(struct exfat_entry),
818 CLUSTER_SIZE(*ef->sb)) * CLUSTER_SIZE(*ef->sb);
819 if (new_size == 0) /* directory always has at least 1 cluster */
820 new_size = CLUSTER_SIZE(*ef->sb);
821 if (new_size == dir->size)
823 return exfat_truncate(ef, dir, new_size, true);
826 static int delete(struct exfat* ef, struct exfat_node* node)
828 struct exfat_node* parent = node->parent;
829 off_t deleted_offset = node->entry_offset;
832 exfat_get_node(parent);
833 if (!erase_entry(ef, node))
835 exfat_put_node(ef, parent);
838 exfat_update_mtime(parent);
840 rc = shrink_directory(ef, parent, deleted_offset);
841 node->is_unlinked = true;
844 exfat_flush_node(ef, parent);
845 exfat_put_node(ef, parent);
848 rc = exfat_flush_node(ef, parent);
849 exfat_put_node(ef, parent);
853 int exfat_unlink(struct exfat* ef, struct exfat_node* node)
855 if (node->flags & EXFAT_ATTRIB_DIR)
857 return delete(ef, node);
860 int exfat_rmdir(struct exfat* ef, struct exfat_node* node)
864 if (!(node->flags & EXFAT_ATTRIB_DIR))
866 /* check that directory is empty */
867 rc = exfat_cache_directory(ef, node);
872 return delete(ef, node);
875 static int grow_directory(struct exfat* ef, struct exfat_node* dir,
876 uint64_t asize, uint32_t difference)
878 return exfat_truncate(ef, dir,
879 DIV_ROUND_UP(asize + difference, CLUSTER_SIZE(*ef->sb))
880 * CLUSTER_SIZE(*ef->sb), true);
883 static int find_slot(struct exfat* ef, struct exfat_node* dir,
884 cluster_t* cluster, off_t* offset, int subentries)
888 const struct exfat_entry* entry;
891 rc = opendir(ef, dir, &it);
898 *cluster = it.cluster;
901 entry = get_entry_ptr(ef, &it);
902 if (entry->type & EXFAT_ENTRY_VALID)
906 if (contiguous == subentries)
907 break; /* suitable slot is found */
908 if (it.offset + sizeof(struct exfat_entry) >= dir->size)
910 rc = grow_directory(ef, dir, dir->size,
911 (subentries - contiguous) * sizeof(struct exfat_entry));
918 if (!fetch_next_entry(ef, dir, &it))
928 static int commit_entry(struct exfat* ef, struct exfat_node* dir,
929 const le16_t* name, cluster_t cluster, off_t offset, uint16_t attrib)
931 struct exfat_node* node;
932 struct exfat_entry_meta1 meta1;
933 struct exfat_entry_meta2 meta2;
934 const size_t name_length = utf16_length(name);
935 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
938 node = allocate_node();
941 node->entry_cluster = cluster;
942 node->entry_offset = offset;
943 memcpy(node->name, name, name_length * sizeof(le16_t));
945 memset(&meta1, 0, sizeof(meta1));
946 meta1.type = EXFAT_ENTRY_FILE;
947 meta1.continuations = 1 + name_entries;
948 meta1.attrib = cpu_to_le16(attrib);
949 exfat_unix2exfat(time(NULL), &meta1.crdate, &meta1.crtime,
951 meta1.adate = meta1.mdate = meta1.crdate;
952 meta1.atime = meta1.mtime = meta1.crtime;
953 meta1.mtime_cs = meta1.crtime_cs; /* there is no atime_cs */
955 memset(&meta2, 0, sizeof(meta2));
956 meta2.type = EXFAT_ENTRY_FILE_INFO;
957 meta2.flags = EXFAT_FLAG_ALWAYS1;
958 meta2.name_length = name_length;
959 meta2.name_hash = exfat_calc_name_hash(ef, node->name, name_length);
960 meta2.start_cluster = cpu_to_le32(EXFAT_CLUSTER_FREE);
962 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);
964 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
965 co2o(ef, cluster, offset)) < 0)
967 exfat_error("failed to write meta1 entry");
970 if (!next_entry(ef, dir, &cluster, &offset))
972 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
973 co2o(ef, cluster, offset)) < 0)
975 exfat_error("failed to write meta2 entry");
978 for (i = 0; i < name_entries; i++)
980 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
981 memcpy(name_entry.name, node->name + i * EXFAT_ENAME_MAX,
982 MIN(EXFAT_ENAME_MAX, EXFAT_NAME_MAX - i * EXFAT_ENAME_MAX) *
984 if (!next_entry(ef, dir, &cluster, &offset))
986 if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
987 co2o(ef, cluster, offset)) < 0)
989 exfat_error("failed to write name entry");
994 init_node_meta1(node, &meta1);
995 init_node_meta2(node, &meta2);
997 tree_attach(dir, node);
998 exfat_update_mtime(dir);
1002 static int create(struct exfat* ef, const char* path, uint16_t attrib)
1004 struct exfat_node* dir;
1005 struct exfat_node* existing;
1006 cluster_t cluster = EXFAT_CLUSTER_BAD;
1008 le16_t name[EXFAT_NAME_MAX + 1];
1011 rc = exfat_split(ef, &dir, &existing, name, path);
1014 if (existing != NULL)
1016 exfat_put_node(ef, existing);
1017 exfat_put_node(ef, dir);
1021 rc = find_slot(ef, dir, &cluster, &offset,
1022 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
1025 exfat_put_node(ef, dir);
1028 rc = commit_entry(ef, dir, name, cluster, offset, attrib);
1031 exfat_put_node(ef, dir);
1034 rc = exfat_flush_node(ef, dir);
1035 exfat_put_node(ef, dir);
1039 int exfat_mknod(struct exfat* ef, const char* path)
1041 return create(ef, path, EXFAT_ATTRIB_ARCH);
1044 int exfat_mkdir(struct exfat* ef, const char* path)
1047 struct exfat_node* node;
1049 rc = create(ef, path, EXFAT_ATTRIB_DIR);
1052 rc = exfat_lookup(ef, &node, path);
1055 /* directories always have at least one cluster */
1056 rc = exfat_truncate(ef, node, CLUSTER_SIZE(*ef->sb), true);
1060 exfat_put_node(ef, node);
1063 rc = exfat_flush_node(ef, node);
1067 exfat_put_node(ef, node);
1070 exfat_put_node(ef, node);
1074 static int rename_entry(struct exfat* ef, struct exfat_node* dir,
1075 struct exfat_node* node, const le16_t* name, cluster_t new_cluster,
1078 struct exfat_entry_meta1 meta1;
1079 struct exfat_entry_meta2 meta2;
1080 cluster_t old_cluster = node->entry_cluster;
1081 off_t old_offset = node->entry_offset;
1082 const size_t name_length = utf16_length(name);
1083 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
1086 if (exfat_pread(ef->dev, &meta1, sizeof(meta1),
1087 co2o(ef, old_cluster, old_offset)) < 0)
1089 exfat_error("failed to read meta1 entry on rename");
1092 if (!next_entry(ef, node->parent, &old_cluster, &old_offset))
1094 if (exfat_pread(ef->dev, &meta2, sizeof(meta2),
1095 co2o(ef, old_cluster, old_offset)) < 0)
1097 exfat_error("failed to read meta2 entry on rename");
1100 meta1.continuations = 1 + name_entries;
1101 meta2.name_hash = exfat_calc_name_hash(ef, name, name_length);
1102 meta2.name_length = name_length;
1103 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, name);
1105 if (!erase_entry(ef, node))
1108 node->entry_cluster = new_cluster;
1109 node->entry_offset = new_offset;
1111 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
1112 co2o(ef, new_cluster, new_offset)) < 0)
1114 exfat_error("failed to write meta1 entry on rename");
1117 if (!next_entry(ef, dir, &new_cluster, &new_offset))
1119 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
1120 co2o(ef, new_cluster, new_offset)) < 0)
1122 exfat_error("failed to write meta2 entry on rename");
1126 for (i = 0; i < name_entries; i++)
1128 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
1129 memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX,
1130 EXFAT_ENAME_MAX * sizeof(le16_t));
1131 if (!next_entry(ef, dir, &new_cluster, &new_offset))
1133 if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
1134 co2o(ef, new_cluster, new_offset)) < 0)
1136 exfat_error("failed to write name entry on rename");
1141 memcpy(node->name, name, (EXFAT_NAME_MAX + 1) * sizeof(le16_t));
1143 tree_attach(dir, node);
1147 int exfat_rename(struct exfat* ef, const char* old_path, const char* new_path)
1149 struct exfat_node* node;
1150 struct exfat_node* existing;
1151 struct exfat_node* dir;
1152 cluster_t cluster = EXFAT_CLUSTER_BAD;
1154 le16_t name[EXFAT_NAME_MAX + 1];
1157 rc = exfat_lookup(ef, &node, old_path);
1161 rc = exfat_split(ef, &dir, &existing, name, new_path);
1164 exfat_put_node(ef, node);
1168 /* check that target is not a subdirectory of the source */
1169 if (node->flags & EXFAT_ATTRIB_DIR)
1171 struct exfat_node* p;
1173 for (p = dir; p; p = p->parent)
1176 if (existing != NULL)
1177 exfat_put_node(ef, existing);
1178 exfat_put_node(ef, dir);
1179 exfat_put_node(ef, node);
1184 if (existing != NULL)
1186 /* remove target if it's not the same node as source */
1187 if (existing != node)
1189 if (existing->flags & EXFAT_ATTRIB_DIR)
1191 if (node->flags & EXFAT_ATTRIB_DIR)
1192 rc = exfat_rmdir(ef, existing);
1198 if (!(node->flags & EXFAT_ATTRIB_DIR))
1199 rc = exfat_unlink(ef, existing);
1203 exfat_put_node(ef, existing);
1206 /* free clusters even if something went wrong; overwise they
1207 will be just lost */
1208 exfat_cleanup_node(ef, existing);
1209 exfat_put_node(ef, dir);
1210 exfat_put_node(ef, node);
1213 rc = exfat_cleanup_node(ef, existing);
1216 exfat_put_node(ef, dir);
1217 exfat_put_node(ef, node);
1222 exfat_put_node(ef, existing);
1225 rc = find_slot(ef, dir, &cluster, &offset,
1226 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
1229 exfat_put_node(ef, dir);
1230 exfat_put_node(ef, node);
1233 rc = rename_entry(ef, dir, node, name, cluster, offset);
1234 exfat_put_node(ef, dir);
1235 exfat_put_node(ef, node);
1239 void exfat_utimes(struct exfat_node* node, const struct timespec tv[2])
1241 node->atime = tv[0].tv_sec;
1242 node->mtime = tv[1].tv_sec;
1243 node->is_dirty = true;
1246 void exfat_update_atime(struct exfat_node* node)
1248 node->atime = time(NULL);
1249 node->is_dirty = true;
1252 void exfat_update_mtime(struct exfat_node* node)
1254 node->mtime = time(NULL);
1255 node->is_dirty = true;
1258 const char* exfat_get_label(struct exfat* ef)
1263 static int find_label(struct exfat* ef, cluster_t* cluster, off_t* offset)
1268 rc = opendir(ef, ef->root, &it);
1274 if (it.offset >= ef->root->size)
1280 if (get_entry_ptr(ef, &it)->type == EXFAT_ENTRY_LABEL)
1282 *cluster = it.cluster;
1283 *offset = it.offset;
1288 if (!fetch_next_entry(ef, ef->root, &it))
1296 int exfat_set_label(struct exfat* ef, const char* label)
1298 le16_t label_utf16[EXFAT_ENAME_MAX + 1];
1302 struct exfat_entry_label entry;
1304 memset(label_utf16, 0, sizeof(label_utf16));
1305 rc = utf8_to_utf16(label_utf16, label, EXFAT_ENAME_MAX + 1, strlen(label));
1309 rc = find_label(ef, &cluster, &offset);
1311 rc = find_slot(ef, ef->root, &cluster, &offset, 1);
1315 entry.type = EXFAT_ENTRY_LABEL;
1316 entry.length = utf16_length(label_utf16);
1317 memcpy(entry.name, label_utf16, sizeof(entry.name));
1318 if (entry.length == 0)
1319 entry.type ^= EXFAT_ENTRY_VALID;
1321 if (exfat_pwrite(ef->dev, &entry, sizeof(struct exfat_entry_label),
1322 co2o(ef, cluster, offset)) < 0)
1324 exfat_error("failed to write label entry");
1327 strcpy(ef->label, label);