3 exFAT file system implementation library.
5 Free exFAT implementation.
6 Copyright (C) 2010-2018 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 #define EXFAT_ENTRY_NONE (-1)
30 struct exfat_node* exfat_get_node(struct exfat_node* node)
32 /* if we switch to multi-threaded mode we will need atomic
33 increment here and atomic decrement in exfat_put_node() */
38 void exfat_put_node(struct exfat* ef, struct exfat_node* node)
40 char buffer[EXFAT_UTF8_NAME_BUFFER_MAX];
43 if (node->references < 0)
45 exfat_get_name(node, buffer);
46 exfat_bug("reference counter of '%s' is below zero", buffer);
48 else if (node->references == 0 && node != ef->root)
52 exfat_get_name(node, buffer);
53 exfat_warn("dirty node '%s' with zero references", buffer);
59 * This function must be called on rmdir and unlink (after the last
60 * exfat_put_node()) to free clusters.
62 int exfat_cleanup_node(struct exfat* ef, struct exfat_node* node)
66 if (node->references != 0)
67 exfat_bug("unable to cleanup a node with %d references",
70 if (node->is_unlinked)
72 /* free all clusters and node structure itself */
73 rc = exfat_truncate(ef, node, 0, true);
74 /* free the node even in case of error or its memory will be lost */
80 static int read_entries(struct exfat* ef, struct exfat_node* dir,
81 struct exfat_entry* entries, int n, off_t offset)
85 if (!(dir->attrib & EXFAT_ATTRIB_DIR))
86 exfat_bug("attempted to read entries from a file");
88 size = exfat_generic_pread(ef, dir, entries,
89 sizeof(struct exfat_entry[n]), offset);
90 if (size == sizeof(struct exfat_entry) * n)
91 return 0; /* success */
96 exfat_error("read %zd bytes instead of %zu bytes", size,
97 sizeof(struct exfat_entry[n]));
101 static int write_entries(struct exfat* ef, struct exfat_node* dir,
102 const struct exfat_entry* entries, int n, off_t offset)
106 if (!(dir->attrib & EXFAT_ATTRIB_DIR))
107 exfat_bug("attempted to write entries into a file");
109 size = exfat_generic_pwrite(ef, dir, entries,
110 sizeof(struct exfat_entry[n]), offset);
111 if (size == sizeof(struct exfat_entry) * n)
112 return 0; /* success */
115 exfat_error("wrote %zd bytes instead of %zu bytes", size,
116 sizeof(struct exfat_entry[n]));
120 static struct exfat_node* allocate_node(void)
122 struct exfat_node* node = malloc(sizeof(struct exfat_node));
125 exfat_error("failed to allocate node");
128 memset(node, 0, sizeof(struct exfat_node));
132 static void init_node_meta1(struct exfat_node* node,
133 const struct exfat_entry_meta1* meta1)
135 node->attrib = le16_to_cpu(meta1->attrib);
136 node->continuations = meta1->continuations;
137 node->mtime = exfat_exfat2unix(meta1->mdate, meta1->mtime,
138 meta1->mtime_cs, meta1->mtime_tzo);
139 /* there is no centiseconds field for atime */
140 node->atime = exfat_exfat2unix(meta1->adate, meta1->atime,
141 0, meta1->atime_tzo);
144 static void init_node_meta2(struct exfat_node* node,
145 const struct exfat_entry_meta2* meta2)
147 node->size = le64_to_cpu(meta2->size);
148 node->start_cluster = le32_to_cpu(meta2->start_cluster);
149 node->fptr_cluster = node->start_cluster;
150 node->is_contiguous = ((meta2->flags & EXFAT_FLAG_CONTIGUOUS) != 0);
153 static void init_node_name(struct exfat_node* node,
154 const struct exfat_entry* entries, int n)
158 for (i = 0; i < n; i++)
159 memcpy(node->name + i * EXFAT_ENAME_MAX,
160 ((const struct exfat_entry_name*) &entries[i])->name,
161 EXFAT_ENAME_MAX * sizeof(le16_t));
164 static bool check_entries(const struct exfat_entry* entry, int n)
166 int previous = EXFAT_ENTRY_NONE;
170 /* check transitions between entries types */
171 for (i = 0; i < n + 1; previous = current, i++)
175 current = (i < n) ? entry[i].type : EXFAT_ENTRY_NONE;
178 case EXFAT_ENTRY_NONE:
179 valid = (current == EXFAT_ENTRY_FILE);
181 case EXFAT_ENTRY_FILE:
182 valid = (current == EXFAT_ENTRY_FILE_INFO);
184 case EXFAT_ENTRY_FILE_INFO:
185 valid = (current == EXFAT_ENTRY_FILE_NAME);
187 case EXFAT_ENTRY_FILE_NAME:
188 valid = (current == EXFAT_ENTRY_FILE_NAME ||
189 current == EXFAT_ENTRY_NONE ||
190 current >= EXFAT_ENTRY_FILE_TAIL);
192 case EXFAT_ENTRY_FILE_TAIL ... 0xff:
193 valid = (current >= EXFAT_ENTRY_FILE_TAIL ||
194 current == EXFAT_ENTRY_NONE);
200 exfat_error("unexpected entry type %#x after %#x at %d/%d",
201 current, previous, i, n);
208 static bool check_node(const struct exfat* ef, struct exfat_node* node,
209 le16_t actual_checksum, const struct exfat_entry_meta1* meta1,
210 const struct exfat_entry_meta2* meta2)
212 int cluster_size = CLUSTER_SIZE(*ef->sb);
213 uint64_t clusters_heap_size =
214 (uint64_t) le32_to_cpu(ef->sb->cluster_count) * cluster_size;
215 char buffer[EXFAT_UTF8_NAME_BUFFER_MAX];
219 Validate checksum first. If it's invalid all other fields probably
220 contain just garbage.
222 if (le16_to_cpu(actual_checksum) != le16_to_cpu(meta1->checksum))
224 exfat_get_name(node, buffer);
225 exfat_error("'%s' has invalid checksum (%#hx != %#hx)", buffer,
226 le16_to_cpu(actual_checksum), le16_to_cpu(meta1->checksum));
227 if (!EXFAT_REPAIR(invalid_node_checksum, ef, node))
232 exFAT does not support sparse files but allows files with uninitialized
233 clusters. For such files valid_size means initialized data size and
234 cannot be greater than file size. See SetFileValidData() function
237 if (le64_to_cpu(meta2->valid_size) > node->size)
239 exfat_get_name(node, buffer);
240 exfat_error("'%s' has valid size (%"PRIu64") greater than size "
241 "(%"PRIu64")", buffer, le64_to_cpu(meta2->valid_size),
247 Empty file must have zero start cluster. Non-empty file must start
248 with a valid cluster. Directories cannot be empty (i.e. must always
249 have a valid start cluster), but we will check this later while
250 reading that directory to give user a chance to read this directory.
252 if (node->size == 0 && node->start_cluster != EXFAT_CLUSTER_FREE)
254 exfat_get_name(node, buffer);
255 exfat_error("'%s' is empty but start cluster is %#x", buffer,
256 node->start_cluster);
259 if (node->size > 0 && CLUSTER_INVALID(*ef->sb, node->start_cluster))
261 exfat_get_name(node, buffer);
262 exfat_error("'%s' points to invalid cluster %#x", buffer,
263 node->start_cluster);
267 /* File or directory cannot be larger than clusters heap. */
268 if (node->size > clusters_heap_size)
270 exfat_get_name(node, buffer);
271 exfat_error("'%s' is larger than clusters heap: %"PRIu64" > %"PRIu64,
272 buffer, node->size, clusters_heap_size);
276 /* Empty file or directory must be marked as non-contiguous. */
277 if (node->size == 0 && node->is_contiguous)
279 exfat_get_name(node, buffer);
280 exfat_error("'%s' is empty but marked as contiguous (%#hx)", buffer,
285 /* Directory size must be aligned on at cluster boundary. */
286 if ((node->attrib & EXFAT_ATTRIB_DIR) && node->size % cluster_size != 0)
288 exfat_get_name(node, buffer);
289 exfat_error("'%s' directory size %"PRIu64" is not divisible by %d", buffer,
290 node->size, cluster_size);
297 static int parse_file_entries(struct exfat* ef, struct exfat_node* node,
298 const struct exfat_entry* entries, int n)
300 const struct exfat_entry_meta1* meta1;
301 const struct exfat_entry_meta2* meta2;
302 int mandatory_entries;
304 if (!check_entries(entries, n))
307 meta1 = (const struct exfat_entry_meta1*) &entries[0];
308 if (meta1->continuations < 2)
310 exfat_error("too few continuations (%hhu)", meta1->continuations);
313 meta2 = (const struct exfat_entry_meta2*) &entries[1];
314 if (meta2->flags & ~(EXFAT_FLAG_ALWAYS1 | EXFAT_FLAG_CONTIGUOUS))
316 exfat_error("unknown flags in meta2 (%#hhx)", meta2->flags);
319 mandatory_entries = 2 + DIV_ROUND_UP(meta2->name_length, EXFAT_ENAME_MAX);
320 if (meta1->continuations < mandatory_entries - 1)
322 exfat_error("too few continuations (%hhu < %d)",
323 meta1->continuations, mandatory_entries - 1);
327 init_node_meta1(node, meta1);
328 init_node_meta2(node, meta2);
329 init_node_name(node, entries + 2, mandatory_entries - 2);
331 if (!check_node(ef, node, exfat_calc_checksum(entries, n), meta1, meta2))
337 static int parse_file_entry(struct exfat* ef, struct exfat_node* parent,
338 struct exfat_node** node, off_t* offset, int n)
340 struct exfat_entry entries[n];
343 rc = read_entries(ef, parent, entries, n, *offset);
347 /* a new node has zero references */
348 *node = allocate_node();
351 (*node)->entry_offset = *offset;
353 rc = parse_file_entries(ef, *node, entries, n);
360 *offset += sizeof(struct exfat_entry[n]);
364 static void decompress_upcase(uint16_t* output, const le16_t* source,
370 for (oi = 0; oi < EXFAT_UPCASE_CHARS; oi++)
373 for (si = 0, oi = 0; si < size && oi < EXFAT_UPCASE_CHARS; si++)
375 uint16_t ch = le16_to_cpu(source[si]);
377 if (ch == 0xffff && si + 1 < size) /* indicates a run */
378 oi += le16_to_cpu(source[++si]);
385 * Read one entry in a directory at offset position and build a new node
388 static int readdir(struct exfat* ef, struct exfat_node* parent,
389 struct exfat_node** node, off_t* offset)
392 struct exfat_entry entry;
393 const struct exfat_entry_meta1* meta1;
394 const struct exfat_entry_upcase* upcase;
395 const struct exfat_entry_bitmap* bitmap;
396 const struct exfat_entry_label* label;
397 uint64_t upcase_size = 0;
398 le16_t* upcase_comp = NULL;
399 le16_t label_name[EXFAT_ENAME_MAX];
403 rc = read_entries(ef, parent, &entry, 1, *offset);
409 case EXFAT_ENTRY_FILE:
410 meta1 = (const struct exfat_entry_meta1*) &entry;
411 return parse_file_entry(ef, parent, node, offset,
412 1 + meta1->continuations);
414 case EXFAT_ENTRY_UPCASE:
415 if (ef->upcase != NULL)
417 upcase = (const struct exfat_entry_upcase*) &entry;
418 if (CLUSTER_INVALID(*ef->sb, le32_to_cpu(upcase->start_cluster)))
420 exfat_error("invalid cluster 0x%x in upcase table",
421 le32_to_cpu(upcase->start_cluster));
424 upcase_size = le64_to_cpu(upcase->size);
425 if (upcase_size == 0 ||
426 upcase_size > EXFAT_UPCASE_CHARS * sizeof(uint16_t) ||
427 upcase_size % sizeof(uint16_t) != 0)
429 exfat_error("bad upcase table size (%"PRIu64" bytes)",
433 upcase_comp = malloc(upcase_size);
434 if (upcase_comp == NULL)
436 exfat_error("failed to allocate upcase table (%"PRIu64" bytes)",
441 /* read compressed upcase table */
442 if (exfat_pread(ef->dev, upcase_comp, upcase_size,
443 exfat_c2o(ef, le32_to_cpu(upcase->start_cluster))) < 0)
446 exfat_error("failed to read upper case table "
447 "(%"PRIu64" bytes starting at cluster %#x)",
449 le32_to_cpu(upcase->start_cluster));
453 /* decompress upcase table */
454 ef->upcase = calloc(EXFAT_UPCASE_CHARS, sizeof(uint16_t));
455 if (ef->upcase == NULL)
458 exfat_error("failed to allocate decompressed upcase table");
461 decompress_upcase(ef->upcase, upcase_comp,
462 upcase_size / sizeof(uint16_t));
466 case EXFAT_ENTRY_BITMAP:
467 bitmap = (const struct exfat_entry_bitmap*) &entry;
468 ef->cmap.start_cluster = le32_to_cpu(bitmap->start_cluster);
469 if (CLUSTER_INVALID(*ef->sb, ef->cmap.start_cluster))
471 exfat_error("invalid cluster 0x%x in clusters bitmap",
472 ef->cmap.start_cluster);
475 ef->cmap.size = le32_to_cpu(ef->sb->cluster_count);
476 if (le64_to_cpu(bitmap->size) < DIV_ROUND_UP(ef->cmap.size, 8))
478 exfat_error("invalid clusters bitmap size: %"PRIu64
479 " (expected at least %u)",
480 le64_to_cpu(bitmap->size),
481 DIV_ROUND_UP(ef->cmap.size, 8));
484 /* FIXME bitmap can be rather big, up to 512 MB */
485 ef->cmap.chunk_size = ef->cmap.size;
486 ef->cmap.chunk = malloc(BMAP_SIZE(ef->cmap.chunk_size));
487 if (ef->cmap.chunk == NULL)
489 exfat_error("failed to allocate clusters bitmap chunk "
490 "(%"PRIu64" bytes)", le64_to_cpu(bitmap->size));
494 if (exfat_pread(ef->dev, ef->cmap.chunk,
495 BMAP_SIZE(ef->cmap.chunk_size),
496 exfat_c2o(ef, ef->cmap.start_cluster)) < 0)
498 exfat_error("failed to read clusters bitmap "
499 "(%"PRIu64" bytes starting at cluster %#x)",
500 le64_to_cpu(bitmap->size), ef->cmap.start_cluster);
505 case EXFAT_ENTRY_LABEL:
506 label = (const struct exfat_entry_label*) &entry;
507 if (label->length > EXFAT_ENAME_MAX)
509 exfat_error("too long label (%hhu chars)", label->length);
512 /* copy to a temporary buffer to avoid unaligned access to a
514 memcpy(label_name, label->name, sizeof(label_name));
515 if (exfat_utf16_to_utf8(ef->label, label_name,
516 sizeof(ef->label), EXFAT_ENAME_MAX) != 0)
521 if (!(entry.type & EXFAT_ENTRY_VALID))
522 break; /* deleted entry, ignore it */
524 exfat_error("unknown entry type %#hhx", entry.type);
525 if (!EXFAT_REPAIR(unknown_entry, ef, parent, &entry, *offset))
528 *offset += sizeof(entry);
530 /* we never reach here */
533 int exfat_cache_directory(struct exfat* ef, struct exfat_node* dir)
537 struct exfat_node* node;
538 struct exfat_node* current = NULL;
541 return 0; /* already cached */
543 while ((rc = readdir(ef, dir, &node, &offset)) == 0)
548 current->next = node;
549 node->prev = current;
560 for (current = dir->child; current; current = node)
562 node = current->next;
569 dir->is_cached = true;
573 static void tree_attach(struct exfat_node* dir, struct exfat_node* node)
578 dir->child->prev = node;
579 node->next = dir->child;
584 static void tree_detach(struct exfat_node* node)
587 node->prev->next = node->next;
588 else /* this is the first node in the list */
589 node->parent->child = node->next;
591 node->next->prev = node->prev;
597 static void reset_cache(struct exfat* ef, struct exfat_node* node)
599 char buffer[EXFAT_UTF8_NAME_BUFFER_MAX];
603 struct exfat_node* p = node->child;
608 node->is_cached = false;
609 if (node->references != 0)
611 exfat_get_name(node, buffer);
612 exfat_warn("non-zero reference counter (%d) for '%s'",
613 node->references, buffer);
615 if (node != ef->root && node->is_dirty)
617 exfat_get_name(node, buffer);
618 exfat_bug("node '%s' is dirty", buffer);
620 while (node->references)
621 exfat_put_node(ef, node);
624 void exfat_reset_cache(struct exfat* ef)
626 reset_cache(ef, ef->root);
629 int exfat_flush_node(struct exfat* ef, struct exfat_node* node)
631 struct exfat_entry entries[1 + node->continuations];
632 struct exfat_entry_meta1* meta1 = (struct exfat_entry_meta1*) &entries[0];
633 struct exfat_entry_meta2* meta2 = (struct exfat_entry_meta2*) &entries[1];
638 return 0; /* no need to flush */
641 exfat_bug("unable to flush node to read-only FS");
643 if (node->parent == NULL)
644 return 0; /* do not flush unlinked node */
646 rc = read_entries(ef, node->parent, entries, 1 + node->continuations,
650 if (!check_entries(entries, 1 + node->continuations))
653 meta1->attrib = cpu_to_le16(node->attrib);
654 exfat_unix2exfat(node->mtime, &edate, &etime,
655 &meta1->mtime_cs, &meta1->mtime_tzo);
656 meta1->mdate = edate;
657 meta1->mtime = etime;
658 exfat_unix2exfat(node->atime, &edate, &etime,
659 NULL, &meta1->atime_tzo);
660 meta1->adate = edate;
661 meta1->atime = etime;
662 meta2->size = meta2->valid_size = cpu_to_le64(node->size);
663 meta2->start_cluster = cpu_to_le32(node->start_cluster);
664 meta2->flags = EXFAT_FLAG_ALWAYS1;
665 /* empty files must not be marked as contiguous */
666 if (node->size != 0 && node->is_contiguous)
667 meta2->flags |= EXFAT_FLAG_CONTIGUOUS;
668 /* name hash remains unchanged, no need to recalculate it */
670 meta1->checksum = exfat_calc_checksum(entries, 1 + node->continuations);
671 rc = write_entries(ef, node->parent, entries, 1 + node->continuations,
676 node->is_dirty = false;
677 return exfat_flush(ef);
680 static int erase_entries(struct exfat* ef, struct exfat_node* dir, int n,
683 struct exfat_entry entries[n];
687 rc = read_entries(ef, dir, entries, n, offset);
690 for (i = 0; i < n; i++)
691 entries[i].type &= ~EXFAT_ENTRY_VALID;
692 return write_entries(ef, dir, entries, n, offset);
695 static int erase_node(struct exfat* ef, struct exfat_node* node)
699 exfat_get_node(node->parent);
700 rc = erase_entries(ef, node->parent, 1 + node->continuations,
704 exfat_put_node(ef, node->parent);
707 rc = exfat_flush_node(ef, node->parent);
708 exfat_put_node(ef, node->parent);
712 static int shrink_directory(struct exfat* ef, struct exfat_node* dir,
713 off_t deleted_offset)
715 const struct exfat_node* node;
716 const struct exfat_node* last_node;
717 uint64_t entries = 0;
720 if (!(dir->attrib & EXFAT_ATTRIB_DIR))
721 exfat_bug("attempted to shrink a file");
723 exfat_bug("attempted to shrink uncached directory");
725 for (last_node = node = dir->child; node; node = node->next)
727 if (deleted_offset < node->entry_offset)
729 /* there are other entries after the removed one, no way to shrink
733 if (last_node->entry_offset < node->entry_offset)
739 /* offset of the last entry */
740 entries += last_node->entry_offset / sizeof(struct exfat_entry);
741 /* two subentries with meta info */
743 /* subentries with file name */
744 entries += DIV_ROUND_UP(exfat_utf16_length(last_node->name),
748 new_size = DIV_ROUND_UP(entries * sizeof(struct exfat_entry),
749 CLUSTER_SIZE(*ef->sb)) * CLUSTER_SIZE(*ef->sb);
750 if (new_size == 0) /* directory always has at least 1 cluster */
751 new_size = CLUSTER_SIZE(*ef->sb);
752 if (new_size == dir->size)
754 return exfat_truncate(ef, dir, new_size, true);
757 static int delete(struct exfat* ef, struct exfat_node* node)
759 struct exfat_node* parent = node->parent;
760 off_t deleted_offset = node->entry_offset;
763 exfat_get_node(parent);
764 rc = erase_node(ef, node);
767 exfat_put_node(ef, parent);
771 rc = shrink_directory(ef, parent, deleted_offset);
772 node->is_unlinked = true;
775 exfat_flush_node(ef, parent);
776 exfat_put_node(ef, parent);
779 exfat_update_mtime(parent);
780 rc = exfat_flush_node(ef, parent);
781 exfat_put_node(ef, parent);
785 int exfat_unlink(struct exfat* ef, struct exfat_node* node)
787 if (node->attrib & EXFAT_ATTRIB_DIR)
789 return delete(ef, node);
792 int exfat_rmdir(struct exfat* ef, struct exfat_node* node)
796 if (!(node->attrib & EXFAT_ATTRIB_DIR))
798 /* check that directory is empty */
799 rc = exfat_cache_directory(ef, node);
804 return delete(ef, node);
807 static int check_slot(struct exfat* ef, struct exfat_node* dir, off_t offset,
810 struct exfat_entry entries[n];
814 /* Root directory contains entries, that don't have any nodes associated
815 with them (clusters bitmap, upper case table, label). We need to be
816 careful not to overwrite them. */
820 rc = read_entries(ef, dir, entries, n, offset);
823 for (i = 0; i < n; i++)
824 if (entries[i].type & EXFAT_ENTRY_VALID)
829 static int find_slot(struct exfat* ef, struct exfat_node* dir,
830 off_t* offset, int n)
833 struct exfat_node* p;
838 exfat_bug("directory is not cached");
840 /* build a bitmap of valid entries in the directory */
841 dmap = calloc(BMAP_SIZE(dir->size / sizeof(struct exfat_entry)),
845 exfat_error("failed to allocate directory bitmap (%"PRIu64")",
846 dir->size / sizeof(struct exfat_entry));
849 for (p = dir->child; p != NULL; p = p->next)
850 for (i = 0; i < 1 + p->continuations; i++)
851 BMAP_SET(dmap, p->entry_offset / sizeof(struct exfat_entry) + i);
853 /* find a slot in the directory entries bitmap */
854 for (i = 0; i < dir->size / sizeof(struct exfat_entry); i++)
856 if (BMAP_GET(dmap, i) == 0)
858 if (contiguous++ == 0)
859 *offset = (off_t) i * sizeof(struct exfat_entry);
861 /* suitable slot is found, check that it's not occupied */
862 switch (check_slot(ef, dir, *offset, n))
871 /* slot at (i-n) is occupied, go back and check (i-n+1) */
882 /* no suitable slots found, extend the directory */
885 return exfat_truncate(ef, dir,
886 ROUND_UP(dir->size + sizeof(struct exfat_entry[n - contiguous]),
887 CLUSTER_SIZE(*ef->sb)),
891 static int commit_entry(struct exfat* ef, struct exfat_node* dir,
892 const le16_t* name, off_t offset, uint16_t attrib)
894 struct exfat_node* node;
895 const size_t name_length = exfat_utf16_length(name);
896 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
897 struct exfat_entry entries[2 + name_entries];
898 struct exfat_entry_meta1* meta1 = (struct exfat_entry_meta1*) &entries[0];
899 struct exfat_entry_meta2* meta2 = (struct exfat_entry_meta2*) &entries[1];
904 memset(entries, 0, sizeof(struct exfat_entry[2]));
906 meta1->type = EXFAT_ENTRY_FILE;
907 meta1->continuations = 1 + name_entries;
908 meta1->attrib = cpu_to_le16(attrib);
909 exfat_unix2exfat(time(NULL), &edate, &etime,
910 &meta1->crtime_cs, &meta1->crtime_tzo);
911 meta1->adate = meta1->mdate = meta1->crdate = edate;
912 meta1->atime = meta1->mtime = meta1->crtime = etime;
913 meta1->mtime_cs = meta1->crtime_cs; /* there is no atime_cs */
914 meta1->atime_tzo = meta1->mtime_tzo = meta1->crtime_tzo;
916 meta2->type = EXFAT_ENTRY_FILE_INFO;
917 meta2->flags = EXFAT_FLAG_ALWAYS1;
918 meta2->name_length = name_length;
919 meta2->name_hash = exfat_calc_name_hash(ef, name, name_length);
920 meta2->start_cluster = cpu_to_le32(EXFAT_CLUSTER_FREE);
922 for (i = 0; i < name_entries; i++)
924 struct exfat_entry_name* name_entry;
926 name_entry = (struct exfat_entry_name*) &entries[2 + i];
927 name_entry->type = EXFAT_ENTRY_FILE_NAME;
928 name_entry->__unknown = 0;
929 memcpy(name_entry->name, name + i * EXFAT_ENAME_MAX,
930 EXFAT_ENAME_MAX * sizeof(le16_t));
933 meta1->checksum = exfat_calc_checksum(entries, 2 + name_entries);
934 rc = write_entries(ef, dir, entries, 2 + name_entries, offset);
938 node = allocate_node();
941 node->entry_offset = offset;
942 memcpy(node->name, name, name_length * sizeof(le16_t));
943 init_node_meta1(node, meta1);
944 init_node_meta2(node, meta2);
946 tree_attach(dir, node);
950 static int create(struct exfat* ef, const char* path, uint16_t attrib)
952 struct exfat_node* dir;
953 struct exfat_node* existing;
955 le16_t name[EXFAT_NAME_MAX + 1];
958 rc = exfat_split(ef, &dir, &existing, name, path);
961 if (existing != NULL)
963 exfat_put_node(ef, existing);
964 exfat_put_node(ef, dir);
968 rc = find_slot(ef, dir, &offset,
969 2 + DIV_ROUND_UP(exfat_utf16_length(name), EXFAT_ENAME_MAX));
972 exfat_put_node(ef, dir);
975 rc = commit_entry(ef, dir, name, offset, attrib);
978 exfat_put_node(ef, dir);
981 exfat_update_mtime(dir);
982 rc = exfat_flush_node(ef, dir);
983 exfat_put_node(ef, dir);
987 int exfat_mknod(struct exfat* ef, const char* path)
989 return create(ef, path, EXFAT_ATTRIB_ARCH);
992 int exfat_mkdir(struct exfat* ef, const char* path)
995 struct exfat_node* node;
997 rc = create(ef, path, EXFAT_ATTRIB_DIR);
1000 rc = exfat_lookup(ef, &node, path);
1003 /* directories always have at least one cluster */
1004 rc = exfat_truncate(ef, node, CLUSTER_SIZE(*ef->sb), true);
1008 exfat_put_node(ef, node);
1011 rc = exfat_flush_node(ef, node);
1015 exfat_put_node(ef, node);
1018 exfat_put_node(ef, node);
1022 static int rename_entry(struct exfat* ef, struct exfat_node* dir,
1023 struct exfat_node* node, const le16_t* name, off_t new_offset)
1025 const size_t name_length = exfat_utf16_length(name);
1026 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
1027 struct exfat_entry entries[2 + name_entries];
1028 struct exfat_entry_meta1* meta1 = (struct exfat_entry_meta1*) &entries[0];
1029 struct exfat_entry_meta2* meta2 = (struct exfat_entry_meta2*) &entries[1];
1033 rc = read_entries(ef, node->parent, entries, 2, node->entry_offset);
1037 meta1->continuations = 1 + name_entries;
1038 meta2->name_length = name_length;
1039 meta2->name_hash = exfat_calc_name_hash(ef, name, name_length);
1041 rc = erase_node(ef, node);
1045 node->entry_offset = new_offset;
1046 node->continuations = 1 + name_entries;
1048 for (i = 0; i < name_entries; i++)
1050 struct exfat_entry_name* name_entry;
1052 name_entry = (struct exfat_entry_name*) &entries[2 + i];
1053 name_entry->type = EXFAT_ENTRY_FILE_NAME;
1054 name_entry->__unknown = 0;
1055 memcpy(name_entry->name, name + i * EXFAT_ENAME_MAX,
1056 EXFAT_ENAME_MAX * sizeof(le16_t));
1059 meta1->checksum = exfat_calc_checksum(entries, 2 + name_entries);
1060 rc = write_entries(ef, dir, entries, 2 + name_entries, new_offset);
1064 memcpy(node->name, name, (EXFAT_NAME_MAX + 1) * sizeof(le16_t));
1066 tree_attach(dir, node);
1070 int exfat_rename(struct exfat* ef, const char* old_path, const char* new_path)
1072 struct exfat_node* node;
1073 struct exfat_node* existing;
1074 struct exfat_node* dir;
1076 le16_t name[EXFAT_NAME_MAX + 1];
1079 rc = exfat_lookup(ef, &node, old_path);
1083 rc = exfat_split(ef, &dir, &existing, name, new_path);
1086 exfat_put_node(ef, node);
1090 /* check that target is not a subdirectory of the source */
1091 if (node->attrib & EXFAT_ATTRIB_DIR)
1093 struct exfat_node* p;
1095 for (p = dir; p; p = p->parent)
1098 if (existing != NULL)
1099 exfat_put_node(ef, existing);
1100 exfat_put_node(ef, dir);
1101 exfat_put_node(ef, node);
1106 if (existing != NULL)
1108 /* remove target if it's not the same node as source */
1109 if (existing != node)
1111 if (existing->attrib & EXFAT_ATTRIB_DIR)
1113 if (node->attrib & EXFAT_ATTRIB_DIR)
1114 rc = exfat_rmdir(ef, existing);
1120 if (!(node->attrib & EXFAT_ATTRIB_DIR))
1121 rc = exfat_unlink(ef, existing);
1125 exfat_put_node(ef, existing);
1128 /* free clusters even if something went wrong; overwise they
1129 will be just lost */
1130 exfat_cleanup_node(ef, existing);
1131 exfat_put_node(ef, dir);
1132 exfat_put_node(ef, node);
1135 rc = exfat_cleanup_node(ef, existing);
1138 exfat_put_node(ef, dir);
1139 exfat_put_node(ef, node);
1144 exfat_put_node(ef, existing);
1147 rc = find_slot(ef, dir, &offset,
1148 2 + DIV_ROUND_UP(exfat_utf16_length(name), EXFAT_ENAME_MAX));
1151 exfat_put_node(ef, dir);
1152 exfat_put_node(ef, node);
1155 rc = rename_entry(ef, dir, node, name, offset);
1158 exfat_put_node(ef, dir);
1159 exfat_put_node(ef, node);
1162 rc = exfat_flush_node(ef, dir);
1163 exfat_put_node(ef, dir);
1164 exfat_put_node(ef, node);
1165 /* node itself is not marked as dirty, no need to flush it */
1169 void exfat_utimes(struct exfat_node* node, const struct timespec tv[2])
1171 node->atime = tv[0].tv_sec;
1172 node->mtime = tv[1].tv_sec;
1173 node->is_dirty = true;
1176 void exfat_update_atime(struct exfat_node* node)
1178 node->atime = time(NULL);
1179 node->is_dirty = true;
1182 void exfat_update_mtime(struct exfat_node* node)
1184 node->mtime = time(NULL);
1185 node->is_dirty = true;
1188 const char* exfat_get_label(struct exfat* ef)
1193 static int find_label(struct exfat* ef, off_t* offset)
1195 struct exfat_entry entry;
1198 for (*offset = 0; ; *offset += sizeof(entry))
1200 rc = read_entries(ef, ef->root, &entry, 1, *offset);
1204 if (entry.type == EXFAT_ENTRY_LABEL)
1209 int exfat_set_label(struct exfat* ef, const char* label)
1211 le16_t label_utf16[EXFAT_ENAME_MAX + 1];
1214 struct exfat_entry_label entry;
1216 memset(label_utf16, 0, sizeof(label_utf16));
1217 rc = exfat_utf8_to_utf16(label_utf16, label, EXFAT_ENAME_MAX + 1,
1222 rc = find_label(ef, &offset);
1224 rc = find_slot(ef, ef->root, &offset, 1);
1228 entry.type = EXFAT_ENTRY_LABEL;
1229 entry.length = exfat_utf16_length(label_utf16);
1230 memcpy(entry.name, label_utf16, sizeof(entry.name));
1231 if (entry.length == 0)
1232 entry.type ^= EXFAT_ENTRY_VALID;
1234 rc = write_entries(ef, ef->root, (struct exfat_entry*) &entry, 1, offset);
1238 strcpy(ef->label, label);