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 */
86 static int opendir(struct exfat* ef, const struct exfat_node* dir,
89 char buffer[EXFAT_UTF8_NAME_BUFFER_MAX];
91 if (!(dir->attrib & EXFAT_ATTRIB_DIR))
93 exfat_get_name(dir, buffer);
94 exfat_bug("'%s' is not a directory", buffer);
96 if (CLUSTER_INVALID(dir->start_cluster))
98 exfat_get_name(dir, buffer);
99 exfat_error("'%s' directory starts with invalid cluster %#x", buffer,
103 it->cluster = dir->start_cluster;
105 it->chunk = malloc(CLUSTER_SIZE(*ef->sb));
106 if (it->chunk == NULL)
108 exfat_error("failed to allocate memory for directory cluster");
111 if (exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb),
112 exfat_c2o(ef, it->cluster)) < 0)
115 exfat_get_name(dir, buffer);
116 exfat_error("failed to read '%s' directory cluster %#x", buffer,
123 static void closedir(struct iterator* it)
131 static bool fetch_next_entry(struct exfat* ef, const struct exfat_node* parent,
134 /* move iterator to the next entry in the directory */
135 it->offset += sizeof(struct exfat_entry);
136 /* fetch the next cluster if needed */
137 if ((it->offset & (CLUSTER_SIZE(*ef->sb) - 1)) == 0)
139 /* reached the end of directory; the caller should check this
141 if (it->offset >= parent->size)
143 it->cluster = exfat_next_cluster(ef, parent, it->cluster);
144 if (CLUSTER_INVALID(it->cluster))
146 exfat_error("invalid cluster 0x%x while reading directory",
150 if (exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb),
151 exfat_c2o(ef, it->cluster)) < 0)
153 exfat_error("failed to read the next directory cluster %#x",
161 static int read_entries(struct exfat* ef, struct exfat_node* dir,
162 struct exfat_entry* entries, int n, off_t offset)
166 if (!(dir->attrib & EXFAT_ATTRIB_DIR))
167 exfat_bug("attempted to read entries from a file");
169 size = exfat_generic_pread(ef, dir, entries,
170 sizeof(struct exfat_entry[n]), offset);
171 if (size == sizeof(struct exfat_entry[n]))
172 return 0; /* success */
177 exfat_error("read %zd bytes instead of %zu bytes", size,
178 sizeof(struct exfat_entry[n]));
182 static int write_entries(struct exfat* ef, struct exfat_node* dir,
183 const struct exfat_entry* entries, int n, off_t offset)
187 if (!(dir->attrib & EXFAT_ATTRIB_DIR))
188 exfat_bug("attempted to write entries into a file");
190 size = exfat_generic_pwrite(ef, dir, entries,
191 sizeof(struct exfat_entry[n]), offset);
192 if (size == sizeof(struct exfat_entry[n]))
193 return 0; /* success */
196 exfat_error("wrote %zd bytes instead of %zu bytes", size,
197 sizeof(struct exfat_entry[n]));
201 static struct exfat_node* allocate_node(void)
203 struct exfat_node* node = malloc(sizeof(struct exfat_node));
206 exfat_error("failed to allocate node");
209 memset(node, 0, sizeof(struct exfat_node));
213 static void init_node_meta1(struct exfat_node* node,
214 const struct exfat_entry_meta1* meta1)
216 node->attrib = le16_to_cpu(meta1->attrib);
217 node->continuations = meta1->continuations;
218 node->mtime = exfat_exfat2unix(meta1->mdate, meta1->mtime,
220 /* there is no centiseconds field for atime */
221 node->atime = exfat_exfat2unix(meta1->adate, meta1->atime, 0);
224 static void init_node_meta2(struct exfat_node* node,
225 const struct exfat_entry_meta2* meta2)
227 node->size = le64_to_cpu(meta2->size);
228 node->start_cluster = le32_to_cpu(meta2->start_cluster);
229 node->fptr_cluster = node->start_cluster;
230 node->is_contiguous = ((meta2->flags & EXFAT_FLAG_CONTIGUOUS) != 0);
233 static const struct exfat_entry* get_entry_ptr(const struct exfat* ef,
234 const struct iterator* it)
236 return (const struct exfat_entry*)
237 (it->chunk + it->offset % CLUSTER_SIZE(*ef->sb));
240 static bool check_node(const struct exfat_node* node, uint16_t actual_checksum,
241 uint16_t reference_checksum, uint64_t valid_size, int cluster_size)
243 char buffer[EXFAT_UTF8_NAME_BUFFER_MAX];
247 Validate checksum first. If it's invalid all other fields probably
248 contain just garbage.
250 if (actual_checksum != reference_checksum)
252 exfat_get_name(node, buffer);
253 exfat_error("'%s' has invalid checksum (%#hx != %#hx)", buffer,
254 actual_checksum, reference_checksum);
259 exFAT does not support sparse files but allows files with uninitialized
260 clusters. For such files valid_size means initialized data size and
261 cannot be greater than file size. See SetFileValidData() function
264 if (valid_size > node->size)
266 exfat_get_name(node, buffer);
267 exfat_error("'%s' has valid size (%"PRIu64") greater than size "
268 "(%"PRIu64")", buffer, valid_size, node->size);
273 Empty file must have zero start cluster. Non-empty file must start
274 with a valid cluster. Directories cannot be empty (i.e. must always
275 have a valid start cluster), but we will check this later in opendir()
276 to give user a chance to read current directory.
278 if (node->size == 0 && node->start_cluster != EXFAT_CLUSTER_FREE)
280 exfat_get_name(node, buffer);
281 exfat_error("'%s' is empty but start cluster is %#x", buffer,
282 node->start_cluster);
285 if (node->size > 0 && CLUSTER_INVALID(node->start_cluster))
287 exfat_get_name(node, buffer);
288 exfat_error("'%s' points to invalid cluster %#x", buffer,
289 node->start_cluster);
293 /* Empty file or directory must be marked as non-contiguous. */
294 if (node->size == 0 && node->is_contiguous)
296 exfat_get_name(node, buffer);
297 exfat_error("'%s' is empty but marked as contiguous (%#hx)", buffer,
302 /* Directory size must be aligned on at cluster boundary. */
303 if ((node->attrib & EXFAT_ATTRIB_DIR) && node->size % cluster_size != 0)
305 exfat_get_name(node, buffer);
306 exfat_error("'%s' directory size %"PRIu64" is not divisible by %d", buffer,
307 node->size, cluster_size);
314 static void decompress_upcase(uint16_t* output, const le16_t* source,
320 for (oi = 0; oi < EXFAT_UPCASE_CHARS; oi++)
323 for (si = 0, oi = 0; si < size && oi < EXFAT_UPCASE_CHARS; si++)
325 uint16_t ch = le16_to_cpu(source[si]);
327 if (ch == 0xffff && si + 1 < size) /* indicates a run */
328 oi += le16_to_cpu(source[++si]);
335 * Reads one entry in directory at position pointed by iterator and fills
338 static int readdir(struct exfat* ef, const struct exfat_node* parent,
339 struct exfat_node** node, struct iterator* it)
342 const struct exfat_entry* entry;
343 const struct exfat_entry_meta1* meta1;
344 const struct exfat_entry_meta2* meta2;
345 const struct exfat_entry_name* file_name;
346 const struct exfat_entry_upcase* upcase;
347 const struct exfat_entry_bitmap* bitmap;
348 const struct exfat_entry_label* label;
349 uint8_t continuations = 0;
350 le16_t* namep = NULL;
351 uint16_t reference_checksum = 0;
352 uint16_t actual_checksum = 0;
353 uint64_t valid_size = 0;
354 uint64_t upcase_size = 0;
355 le16_t* upcase_comp = NULL;
361 if (it->offset >= parent->size)
363 if (continuations != 0)
365 exfat_error("expected %hhu continuations", continuations);
368 return -ENOENT; /* that's OK, means end of directory */
371 entry = get_entry_ptr(ef, it);
374 case EXFAT_ENTRY_FILE:
375 if (continuations != 0)
377 exfat_error("expected %hhu continuations before new entry",
381 meta1 = (const struct exfat_entry_meta1*) entry;
382 continuations = meta1->continuations;
383 /* each file entry must have at least 2 continuations:
385 if (continuations < 2)
387 exfat_error("too few continuations (%hhu)", continuations);
390 if (continuations > 1 +
391 DIV_ROUND_UP(EXFAT_NAME_MAX, EXFAT_ENAME_MAX))
393 exfat_error("too many continuations (%hhu)", continuations);
396 reference_checksum = le16_to_cpu(meta1->checksum);
397 actual_checksum = exfat_start_checksum(meta1);
398 *node = allocate_node();
404 /* new node has zero reference counter */
405 (*node)->entry_cluster = it->cluster;
406 (*node)->entry_offset = it->offset;
407 init_node_meta1(*node, meta1);
408 namep = (*node)->name;
411 case EXFAT_ENTRY_FILE_INFO:
412 if (continuations < 2)
414 exfat_error("unexpected continuation (%hhu)",
418 meta2 = (const struct exfat_entry_meta2*) entry;
419 if (meta2->flags & ~(EXFAT_FLAG_ALWAYS1 | EXFAT_FLAG_CONTIGUOUS))
421 exfat_error("unknown flags in meta2 (0x%hhx)", meta2->flags);
424 init_node_meta2(*node, meta2);
425 actual_checksum = exfat_add_checksum(entry, actual_checksum);
426 valid_size = le64_to_cpu(meta2->valid_size);
430 case EXFAT_ENTRY_FILE_NAME:
431 if (continuations == 0)
433 exfat_error("unexpected continuation");
436 file_name = (const struct exfat_entry_name*) entry;
437 actual_checksum = exfat_add_checksum(entry, actual_checksum);
439 memcpy(namep, file_name->name,
441 ((*node)->name + EXFAT_NAME_MAX - namep)) *
443 namep += EXFAT_ENAME_MAX;
444 if (--continuations == 0)
446 if (!check_node(*node, actual_checksum, reference_checksum,
447 valid_size, CLUSTER_SIZE(*ef->sb)))
449 if (!fetch_next_entry(ef, parent, it))
451 return 0; /* entry completed */
455 case EXFAT_ENTRY_UPCASE:
456 if (ef->upcase != NULL)
458 upcase = (const struct exfat_entry_upcase*) entry;
459 if (CLUSTER_INVALID(le32_to_cpu(upcase->start_cluster)))
461 exfat_error("invalid cluster 0x%x in upcase table",
462 le32_to_cpu(upcase->start_cluster));
465 upcase_size = le64_to_cpu(upcase->size);
466 if (upcase_size == 0 ||
467 upcase_size > EXFAT_UPCASE_CHARS * sizeof(uint16_t) ||
468 upcase_size % sizeof(uint16_t) != 0)
470 exfat_error("bad upcase table size (%"PRIu64" bytes)",
474 upcase_comp = malloc(upcase_size);
475 if (upcase_comp == NULL)
477 exfat_error("failed to allocate upcase table (%"PRIu64" bytes)",
483 /* read compressed upcase table */
484 if (exfat_pread(ef->dev, upcase_comp, upcase_size,
485 exfat_c2o(ef, le32_to_cpu(upcase->start_cluster))) < 0)
488 exfat_error("failed to read upper case table "
489 "(%"PRIu64" bytes starting at cluster %#x)",
491 le32_to_cpu(upcase->start_cluster));
495 /* decompress upcase table */
496 ef->upcase = calloc(EXFAT_UPCASE_CHARS, sizeof(uint16_t));
497 if (ef->upcase == NULL)
500 exfat_error("failed to allocate decompressed upcase table");
504 decompress_upcase(ef->upcase, upcase_comp,
505 upcase_size / sizeof(uint16_t));
509 case EXFAT_ENTRY_BITMAP:
510 bitmap = (const struct exfat_entry_bitmap*) entry;
511 ef->cmap.start_cluster = le32_to_cpu(bitmap->start_cluster);
512 if (CLUSTER_INVALID(ef->cmap.start_cluster))
514 exfat_error("invalid cluster 0x%x in clusters bitmap",
515 ef->cmap.start_cluster);
518 ef->cmap.size = le32_to_cpu(ef->sb->cluster_count) -
519 EXFAT_FIRST_DATA_CLUSTER;
520 if (le64_to_cpu(bitmap->size) < DIV_ROUND_UP(ef->cmap.size, 8))
522 exfat_error("invalid clusters bitmap size: %"PRIu64
523 " (expected at least %u)",
524 le64_to_cpu(bitmap->size),
525 DIV_ROUND_UP(ef->cmap.size, 8));
528 /* FIXME bitmap can be rather big, up to 512 MB */
529 ef->cmap.chunk_size = ef->cmap.size;
530 ef->cmap.chunk = malloc(BMAP_SIZE(ef->cmap.chunk_size));
531 if (ef->cmap.chunk == NULL)
533 exfat_error("failed to allocate clusters bitmap chunk "
534 "(%"PRIu64" bytes)", le64_to_cpu(bitmap->size));
539 if (exfat_pread(ef->dev, ef->cmap.chunk,
540 BMAP_SIZE(ef->cmap.chunk_size),
541 exfat_c2o(ef, ef->cmap.start_cluster)) < 0)
543 exfat_error("failed to read clusters bitmap "
544 "(%"PRIu64" bytes starting at cluster %#x)",
545 le64_to_cpu(bitmap->size), ef->cmap.start_cluster);
550 case EXFAT_ENTRY_LABEL:
551 label = (const struct exfat_entry_label*) entry;
552 if (label->length > EXFAT_ENAME_MAX)
554 exfat_error("too long label (%hhu chars)", label->length);
557 if (utf16_to_utf8(ef->label, label->name,
558 sizeof(ef->label), EXFAT_ENAME_MAX) != 0)
563 if (!(entry->type & EXFAT_ENTRY_VALID))
564 break; /* deleted entry, ignore it */
565 if (!(entry->type & EXFAT_ENTRY_OPTIONAL))
567 exfat_error("unknown entry type %#hhx", entry->type);
570 /* optional entry, warn and skip */
571 exfat_warn("unknown entry type %#hhx", entry->type);
572 if (continuations == 0)
574 exfat_error("unexpected continuation");
581 if (!fetch_next_entry(ef, parent, it))
584 /* we never reach here */
592 int exfat_cache_directory(struct exfat* ef, struct exfat_node* dir)
596 struct exfat_node* node;
597 struct exfat_node* current = NULL;
600 return 0; /* already cached */
602 rc = opendir(ef, dir, &it);
605 while ((rc = readdir(ef, dir, &node, &it)) == 0)
610 current->next = node;
611 node->prev = current;
623 for (current = dir->child; current; current = node)
625 node = current->next;
632 dir->is_cached = true;
636 static void tree_attach(struct exfat_node* dir, struct exfat_node* node)
641 dir->child->prev = node;
642 node->next = dir->child;
647 static void tree_detach(struct exfat_node* node)
650 node->prev->next = node->next;
651 else /* this is the first node in the list */
652 node->parent->child = node->next;
654 node->next->prev = node->prev;
660 static void reset_cache(struct exfat* ef, struct exfat_node* node)
662 char buffer[EXFAT_UTF8_NAME_BUFFER_MAX];
666 struct exfat_node* p = node->child;
671 node->is_cached = false;
672 if (node->references != 0)
674 exfat_get_name(node, buffer);
675 exfat_warn("non-zero reference counter (%d) for '%s'",
676 node->references, buffer);
678 if (node != ef->root && node->is_dirty)
680 exfat_get_name(node, buffer);
681 exfat_bug("node '%s' is dirty", buffer);
683 while (node->references)
684 exfat_put_node(ef, node);
687 void exfat_reset_cache(struct exfat* ef)
689 reset_cache(ef, ef->root);
692 int exfat_flush_node(struct exfat* ef, struct exfat_node* node)
694 struct exfat_entry entries[2];
695 struct exfat_entry_meta1* meta1 = (struct exfat_entry_meta1*) &entries[0];
696 struct exfat_entry_meta2* meta2 = (struct exfat_entry_meta2*) &entries[1];
700 return 0; /* no need to flush */
703 exfat_bug("unable to flush node to read-only FS");
705 if (node->parent == NULL)
706 return 0; /* do not flush unlinked node */
708 rc = read_entries(ef, node->parent, entries, 2, node->entry_offset);
712 if (meta1->type != EXFAT_ENTRY_FILE)
714 exfat_error("invalid type of meta1: %#hhx", meta1->type);
717 meta1->attrib = cpu_to_le16(node->attrib);
718 exfat_unix2exfat(node->mtime, &meta1->mdate, &meta1->mtime,
720 exfat_unix2exfat(node->atime, &meta1->adate, &meta1->atime, NULL);
722 if (meta2->type != EXFAT_ENTRY_FILE_INFO)
724 exfat_error("invalid type of meta2: %#hhx", meta2->type);
727 meta2->size = meta2->valid_size = cpu_to_le64(node->size);
728 meta2->start_cluster = cpu_to_le32(node->start_cluster);
729 meta2->flags = EXFAT_FLAG_ALWAYS1;
730 /* empty files must not be marked as contiguous */
731 if (node->size != 0 && node->is_contiguous)
732 meta2->flags |= EXFAT_FLAG_CONTIGUOUS;
733 /* name hash remains unchanged, no need to recalculate it */
735 meta1->checksum = exfat_calc_checksum(meta1, meta2, node->name);
737 rc = write_entries(ef, node->parent, entries, 2, node->entry_offset);
741 node->is_dirty = false;
742 return exfat_flush(ef);
745 static int erase_entries(struct exfat* ef, struct exfat_node* dir, int n,
748 struct exfat_entry entries[n];
752 rc = read_entries(ef, dir, entries, n, offset);
755 for (i = 0; i < n; i++)
756 entries[i].type &= ~EXFAT_ENTRY_VALID;
757 return write_entries(ef, dir, entries, n, offset);
760 static int erase_node(struct exfat* ef, struct exfat_node* node)
764 exfat_get_node(node->parent);
765 rc = erase_entries(ef, node->parent, 1 + node->continuations,
769 exfat_put_node(ef, node->parent);
772 rc = exfat_flush_node(ef, node->parent);
773 exfat_put_node(ef, node->parent);
777 static int shrink_directory(struct exfat* ef, struct exfat_node* dir,
778 off_t deleted_offset)
780 const struct exfat_node* node;
781 const struct exfat_node* last_node;
782 uint64_t entries = 0;
785 if (!(dir->attrib & EXFAT_ATTRIB_DIR))
786 exfat_bug("attempted to shrink a file");
788 exfat_bug("attempted to shrink uncached directory");
790 for (last_node = node = dir->child; node; node = node->next)
792 if (deleted_offset < node->entry_offset)
794 /* there are other entries after the removed one, no way to shrink
798 if (last_node->entry_offset < node->entry_offset)
804 /* offset of the last entry */
805 entries += last_node->entry_offset / sizeof(struct exfat_entry);
806 /* two subentries with meta info */
808 /* subentries with file name */
809 entries += DIV_ROUND_UP(utf16_length(last_node->name),
813 new_size = DIV_ROUND_UP(entries * sizeof(struct exfat_entry),
814 CLUSTER_SIZE(*ef->sb)) * CLUSTER_SIZE(*ef->sb);
815 if (new_size == 0) /* directory always has at least 1 cluster */
816 new_size = CLUSTER_SIZE(*ef->sb);
817 if (new_size == dir->size)
819 return exfat_truncate(ef, dir, new_size, true);
822 static int delete(struct exfat* ef, struct exfat_node* node)
824 struct exfat_node* parent = node->parent;
825 off_t deleted_offset = node->entry_offset;
828 exfat_get_node(parent);
829 rc = erase_node(ef, node);
832 exfat_put_node(ef, parent);
836 rc = shrink_directory(ef, parent, deleted_offset);
837 node->is_unlinked = true;
840 exfat_flush_node(ef, parent);
841 exfat_put_node(ef, parent);
844 exfat_update_mtime(parent);
845 rc = exfat_flush_node(ef, parent);
846 exfat_put_node(ef, parent);
850 int exfat_unlink(struct exfat* ef, struct exfat_node* node)
852 if (node->attrib & EXFAT_ATTRIB_DIR)
854 return delete(ef, node);
857 int exfat_rmdir(struct exfat* ef, struct exfat_node* node)
861 if (!(node->attrib & EXFAT_ATTRIB_DIR))
863 /* check that directory is empty */
864 rc = exfat_cache_directory(ef, node);
869 return delete(ef, node);
872 static int check_slot(struct exfat* ef, struct exfat_node* dir, off_t offset,
875 struct exfat_entry entries[n];
879 /* Root directory contains entries, that don't have any nodes associated
880 with them (clusters bitmap, upper case table, label). We need to be
881 careful not to overwrite them. */
885 rc = read_entries(ef, dir, entries, n, offset);
888 for (i = 0; i < n; i++)
889 if (entries[i].type & EXFAT_ENTRY_VALID)
894 static int find_slot(struct exfat* ef, struct exfat_node* dir,
895 off_t* offset, int n)
898 struct exfat_node* p;
903 exfat_bug("directory is not cached");
905 /* build a bitmap of valid entries in the directory */
906 dmap = calloc(BMAP_SIZE(dir->size / sizeof(struct exfat_entry)),
910 exfat_error("failed to allocate directory bitmap (%"PRIu64")",
911 dir->size / sizeof(struct exfat_entry));
914 for (p = dir->child; p != NULL; p = p->next)
915 for (i = 0; i < 1 + p->continuations; i++)
916 BMAP_SET(dmap, p->entry_offset / sizeof(struct exfat_entry) + i);
918 /* find a slot in the directory entries bitmap */
919 for (i = 0; i < dir->size / sizeof(struct exfat_entry); i++)
921 if (BMAP_GET(dmap, i) == 0)
923 if (contiguous++ == 0)
924 *offset = (off_t) i * sizeof(struct exfat_entry);
926 /* suitable slot is found, check that it's not occupied */
927 switch (check_slot(ef, dir, *offset, n))
936 /* slot is occupied, continue searching */
946 /* no suitable slots found, extend the directory */
949 return exfat_truncate(ef, dir,
950 ROUND_UP(dir->size + sizeof(struct exfat_entry[n - contiguous]),
951 CLUSTER_SIZE(*ef->sb)),
955 static int commit_entry(struct exfat* ef, struct exfat_node* dir,
956 const le16_t* name, cluster_t cluster, off_t offset, uint16_t attrib)
958 struct exfat_node* node;
959 const size_t name_length = utf16_length(name);
960 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
961 struct exfat_entry entries[2 + name_entries];
962 struct exfat_entry_meta1* meta1 = (struct exfat_entry_meta1*) &entries[0];
963 struct exfat_entry_meta2* meta2 = (struct exfat_entry_meta2*) &entries[1];
967 memset(entries, 0, sizeof(struct exfat_entry[2]));
969 meta1->type = EXFAT_ENTRY_FILE;
970 meta1->continuations = 1 + name_entries;
971 meta1->attrib = cpu_to_le16(attrib);
972 exfat_unix2exfat(time(NULL), &meta1->crdate, &meta1->crtime,
974 meta1->adate = meta1->mdate = meta1->crdate;
975 meta1->atime = meta1->mtime = meta1->crtime;
976 meta1->mtime_cs = meta1->crtime_cs; /* there is no atime_cs */
978 meta2->type = EXFAT_ENTRY_FILE_INFO;
979 meta2->flags = EXFAT_FLAG_ALWAYS1;
980 meta2->name_length = name_length;
981 meta2->name_hash = exfat_calc_name_hash(ef, name, name_length);
982 meta2->start_cluster = cpu_to_le32(EXFAT_CLUSTER_FREE);
984 meta1->checksum = exfat_calc_checksum(meta1, meta2, name);
986 for (i = 0; i < name_entries; i++)
988 struct exfat_entry_name* name_entry;
990 name_entry = (struct exfat_entry_name*) &entries[2 + i];
991 name_entry->type = EXFAT_ENTRY_FILE_NAME;
992 name_entry->__unknown = 0;
993 memcpy(name_entry->name, name + i * EXFAT_ENAME_MAX,
994 EXFAT_ENAME_MAX * sizeof(le16_t));
997 rc = write_entries(ef, dir, entries, 2 + name_entries, offset);
1001 node = allocate_node();
1004 node->entry_cluster = cluster;
1005 node->entry_offset = offset;
1006 memcpy(node->name, name, name_length * sizeof(le16_t));
1007 init_node_meta1(node, meta1);
1008 init_node_meta2(node, meta2);
1010 tree_attach(dir, node);
1014 static int create(struct exfat* ef, const char* path, uint16_t attrib)
1016 struct exfat_node* dir;
1017 struct exfat_node* existing;
1018 cluster_t cluster = EXFAT_CLUSTER_BAD;
1020 le16_t name[EXFAT_NAME_MAX + 1];
1023 rc = exfat_split(ef, &dir, &existing, name, path);
1026 if (existing != NULL)
1028 exfat_put_node(ef, existing);
1029 exfat_put_node(ef, dir);
1033 rc = find_slot(ef, dir, &offset,
1034 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
1037 exfat_put_node(ef, dir);
1040 rc = commit_entry(ef, dir, name, cluster, offset, attrib);
1043 exfat_put_node(ef, dir);
1046 exfat_update_mtime(dir);
1047 rc = exfat_flush_node(ef, dir);
1048 exfat_put_node(ef, dir);
1052 int exfat_mknod(struct exfat* ef, const char* path)
1054 return create(ef, path, EXFAT_ATTRIB_ARCH);
1057 int exfat_mkdir(struct exfat* ef, const char* path)
1060 struct exfat_node* node;
1062 rc = create(ef, path, EXFAT_ATTRIB_DIR);
1065 rc = exfat_lookup(ef, &node, path);
1068 /* directories always have at least one cluster */
1069 rc = exfat_truncate(ef, node, CLUSTER_SIZE(*ef->sb), true);
1073 exfat_put_node(ef, node);
1076 rc = exfat_flush_node(ef, node);
1080 exfat_put_node(ef, node);
1083 exfat_put_node(ef, node);
1087 static int rename_entry(struct exfat* ef, struct exfat_node* dir,
1088 struct exfat_node* node, const le16_t* name, cluster_t new_cluster,
1091 const size_t name_length = utf16_length(name);
1092 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
1093 struct exfat_entry entries[2 + name_entries];
1094 struct exfat_entry_meta1* meta1 = (struct exfat_entry_meta1*) &entries[0];
1095 struct exfat_entry_meta2* meta2 = (struct exfat_entry_meta2*) &entries[1];
1099 rc = read_entries(ef, node->parent, entries, 2, node->entry_offset);
1103 meta1->continuations = 1 + name_entries;
1104 meta2->name_length = name_length;
1105 meta2->name_hash = exfat_calc_name_hash(ef, name, name_length);
1106 meta1->checksum = exfat_calc_checksum(meta1, meta2, name);
1108 rc = erase_node(ef, node);
1112 node->entry_cluster = new_cluster;
1113 node->entry_offset = new_offset;
1114 node->continuations = 1 + name_entries;
1116 for (i = 0; i < name_entries; i++)
1118 struct exfat_entry_name* name_entry;
1120 name_entry = (struct exfat_entry_name*) &entries[2 + i];
1121 name_entry->type = EXFAT_ENTRY_FILE_NAME;
1122 name_entry->__unknown = 0;
1123 memcpy(name_entry->name, name + i * EXFAT_ENAME_MAX,
1124 EXFAT_ENAME_MAX * sizeof(le16_t));
1127 rc = write_entries(ef, dir, entries, 2 + name_entries, new_offset);
1131 memcpy(node->name, name, (EXFAT_NAME_MAX + 1) * sizeof(le16_t));
1133 tree_attach(dir, node);
1137 int exfat_rename(struct exfat* ef, const char* old_path, const char* new_path)
1139 struct exfat_node* node;
1140 struct exfat_node* existing;
1141 struct exfat_node* dir;
1142 cluster_t cluster = EXFAT_CLUSTER_BAD;
1144 le16_t name[EXFAT_NAME_MAX + 1];
1147 rc = exfat_lookup(ef, &node, old_path);
1151 rc = exfat_split(ef, &dir, &existing, name, new_path);
1154 exfat_put_node(ef, node);
1158 /* check that target is not a subdirectory of the source */
1159 if (node->attrib & EXFAT_ATTRIB_DIR)
1161 struct exfat_node* p;
1163 for (p = dir; p; p = p->parent)
1166 if (existing != NULL)
1167 exfat_put_node(ef, existing);
1168 exfat_put_node(ef, dir);
1169 exfat_put_node(ef, node);
1174 if (existing != NULL)
1176 /* remove target if it's not the same node as source */
1177 if (existing != node)
1179 if (existing->attrib & EXFAT_ATTRIB_DIR)
1181 if (node->attrib & EXFAT_ATTRIB_DIR)
1182 rc = exfat_rmdir(ef, existing);
1188 if (!(node->attrib & EXFAT_ATTRIB_DIR))
1189 rc = exfat_unlink(ef, existing);
1193 exfat_put_node(ef, existing);
1196 /* free clusters even if something went wrong; overwise they
1197 will be just lost */
1198 exfat_cleanup_node(ef, existing);
1199 exfat_put_node(ef, dir);
1200 exfat_put_node(ef, node);
1203 rc = exfat_cleanup_node(ef, existing);
1206 exfat_put_node(ef, dir);
1207 exfat_put_node(ef, node);
1212 exfat_put_node(ef, existing);
1215 rc = find_slot(ef, dir, &offset,
1216 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
1219 exfat_put_node(ef, dir);
1220 exfat_put_node(ef, node);
1223 rc = rename_entry(ef, dir, node, name, cluster, offset);
1226 exfat_put_node(ef, dir);
1227 exfat_put_node(ef, node);
1230 rc = exfat_flush_node(ef, dir);
1231 exfat_put_node(ef, dir);
1232 exfat_put_node(ef, node);
1233 /* node itself is not marked as dirty, no need to flush it */
1237 void exfat_utimes(struct exfat_node* node, const struct timespec tv[2])
1239 node->atime = tv[0].tv_sec;
1240 node->mtime = tv[1].tv_sec;
1241 node->is_dirty = true;
1244 void exfat_update_atime(struct exfat_node* node)
1246 node->atime = time(NULL);
1247 node->is_dirty = true;
1250 void exfat_update_mtime(struct exfat_node* node)
1252 node->mtime = time(NULL);
1253 node->is_dirty = true;
1256 const char* exfat_get_label(struct exfat* ef)
1261 static int find_label(struct exfat* ef, off_t* offset)
1263 struct exfat_entry entry;
1266 for (*offset = 0; ; *offset += sizeof(entry))
1268 rc = read_entries(ef, ef->root, &entry, 1, *offset);
1272 if (entry.type == EXFAT_ENTRY_LABEL)
1277 int exfat_set_label(struct exfat* ef, const char* label)
1279 le16_t label_utf16[EXFAT_ENAME_MAX + 1];
1282 struct exfat_entry_label entry;
1284 memset(label_utf16, 0, sizeof(label_utf16));
1285 rc = utf8_to_utf16(label_utf16, label, EXFAT_ENAME_MAX + 1, strlen(label));
1289 rc = find_label(ef, &offset);
1291 rc = find_slot(ef, ef->root, &offset, 1);
1295 entry.type = EXFAT_ENTRY_LABEL;
1296 entry.length = utf16_length(label_utf16);
1297 memcpy(entry.name, label_utf16, sizeof(entry.name));
1298 if (entry.length == 0)
1299 entry.type ^= EXFAT_ENTRY_VALID;
1301 rc = write_entries(ef, ef->root, (struct exfat_entry*) &entry, 1, offset);
1305 strcpy(ef->label, label);