3 exFAT file system implementation library.
5 Free exFAT implementation.
6 Copyright (C) 2010-2013 Andrew Nayenko
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 /* on-disk nodes iterator */
37 struct exfat_node* exfat_get_node(struct exfat_node* node)
39 /* if we switch to multi-threaded mode we will need atomic
40 increment here and atomic decrement in exfat_put_node() */
45 void exfat_put_node(struct exfat* ef, struct exfat_node* node)
47 char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
50 if (node->references < 0)
52 exfat_get_name(node, buffer, sizeof(buffer) - 1);
53 exfat_bug("reference counter of `%s' is below zero", buffer);
55 else if (node->references == 0 && node != ef->root)
57 if (node->flags & EXFAT_ATTRIB_DIRTY)
59 exfat_get_name(node, buffer, sizeof(buffer) - 1);
60 exfat_warn("dirty node `%s' with zero references", buffer);
66 * This function must be called on rmdir and unlink (after the last
67 * exfat_put_node()) to free clusters.
69 int exfat_cleanup_node(struct exfat* ef, struct exfat_node* node)
73 if (node->references != 0)
74 exfat_bug("unable to cleanup a node with %d references",
77 if (node->flags & EXFAT_ATTRIB_UNLINKED)
79 /* free all clusters and node structure itself */
80 rc = exfat_truncate(ef, node, 0, true);
81 /* free the node even in case of error or its memory will be lost */
88 * Cluster + offset from the beginning of the directory to absolute offset.
90 static off_t co2o(struct exfat* ef, cluster_t cluster, off_t offset)
92 return exfat_c2o(ef, cluster) + offset % CLUSTER_SIZE(*ef->sb);
95 static int opendir(struct exfat* ef, const struct exfat_node* dir,
98 if (!(dir->flags & EXFAT_ATTRIB_DIR))
99 exfat_bug("not a directory");
100 it->cluster = dir->start_cluster;
102 it->contiguous = IS_CONTIGUOUS(*dir);
103 it->chunk = malloc(CLUSTER_SIZE(*ef->sb));
104 if (it->chunk == NULL)
106 exfat_error("out of memory");
109 if (exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb),
110 exfat_c2o(ef, it->cluster)) < 0)
112 exfat_error("failed to read directory cluster %#x", it->cluster);
118 static void closedir(struct iterator* it)
127 static int fetch_next_entry(struct exfat* ef, const struct exfat_node* parent,
130 /* move iterator to the next entry in the directory */
131 it->offset += sizeof(struct exfat_entry);
132 /* fetch the next cluster if needed */
133 if ((it->offset & (CLUSTER_SIZE(*ef->sb) - 1)) == 0)
135 /* reached the end of directory; the caller should check this
137 if (it->offset >= parent->size)
139 it->cluster = exfat_next_cluster(ef, parent, it->cluster);
140 if (CLUSTER_INVALID(it->cluster))
142 exfat_error("invalid cluster 0x%x while reading directory",
146 if (exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb),
147 exfat_c2o(ef, it->cluster)) < 0)
149 exfat_error("failed to read the next directory cluster %#x",
157 static struct exfat_node* allocate_node(void)
159 struct exfat_node* node = malloc(sizeof(struct exfat_node));
162 exfat_error("failed to allocate node");
165 memset(node, 0, sizeof(struct exfat_node));
169 static void init_node_meta1(struct exfat_node* node,
170 const struct exfat_entry_meta1* meta1)
172 node->flags = le16_to_cpu(meta1->attrib);
173 node->mtime = exfat_exfat2unix(meta1->mdate, meta1->mtime,
175 /* there is no centiseconds field for atime */
176 node->atime = exfat_exfat2unix(meta1->adate, meta1->atime, 0);
179 static void init_node_meta2(struct exfat_node* node,
180 const struct exfat_entry_meta2* meta2)
182 node->size = le64_to_cpu(meta2->size);
183 node->start_cluster = le32_to_cpu(meta2->start_cluster);
184 node->fptr_cluster = node->start_cluster;
185 if (meta2->flags & EXFAT_FLAG_CONTIGUOUS)
186 node->flags |= EXFAT_ATTRIB_CONTIGUOUS;
189 static const struct exfat_entry* get_entry_ptr(const struct exfat* ef,
190 const struct iterator* it)
192 return (const struct exfat_entry*)
193 (it->chunk + it->offset % CLUSTER_SIZE(*ef->sb));
196 static bool check_node(const struct exfat_node* node, uint16_t actual_checksum,
197 uint16_t reference_checksum, uint64_t real_size)
199 char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
202 Validate checksum first. If it's invalid all other fields probably
203 contain just garbage.
205 if (actual_checksum != reference_checksum)
207 exfat_get_name(node, buffer, sizeof(buffer) - 1);
208 exfat_error("`%s' has invalid checksum (%#hx != %#hx)", buffer,
209 actual_checksum, reference_checksum);
214 It's unclear what is real_size field needed for. It usually equals to
215 the size but may contain any value less than size (including 0).
217 if (real_size > node->size)
219 exfat_get_name(node, buffer, sizeof(buffer) - 1);
220 exfat_error("`%s' has real size (%"PRIu64") greater than size "
221 "(%"PRIu64")", buffer, real_size, node->size);
229 * Reads one entry in directory at position pointed by iterator and fills
232 static int readdir(struct exfat* ef, const struct exfat_node* parent,
233 struct exfat_node** node, struct iterator* it)
236 const struct exfat_entry* entry;
237 const struct exfat_entry_meta1* meta1;
238 const struct exfat_entry_meta2* meta2;
239 const struct exfat_entry_name* file_name;
240 const struct exfat_entry_upcase* upcase;
241 const struct exfat_entry_bitmap* bitmap;
242 const struct exfat_entry_label* label;
243 uint8_t continuations = 0;
244 le16_t* namep = NULL;
245 uint16_t reference_checksum = 0;
246 uint16_t actual_checksum = 0;
247 uint64_t real_size = 0;
253 if (it->offset >= parent->size)
255 if (continuations != 0)
257 exfat_error("expected %hhu continuations", continuations);
260 return -ENOENT; /* that's OK, means end of directory */
263 entry = get_entry_ptr(ef, it);
266 case EXFAT_ENTRY_FILE:
267 if (continuations != 0)
269 exfat_error("expected %hhu continuations before new entry",
273 meta1 = (const struct exfat_entry_meta1*) entry;
274 continuations = meta1->continuations;
275 /* each file entry must have at least 2 continuations:
277 if (continuations < 2)
279 exfat_error("too few continuations (%hhu)", continuations);
282 if (continuations > 1 +
283 DIV_ROUND_UP(EXFAT_NAME_MAX, EXFAT_ENAME_MAX))
285 exfat_error("too many continuations (%hhu)", continuations);
288 reference_checksum = le16_to_cpu(meta1->checksum);
289 actual_checksum = exfat_start_checksum(meta1);
290 *node = allocate_node();
296 /* new node has zero reference counter */
297 (*node)->entry_cluster = it->cluster;
298 (*node)->entry_offset = it->offset;
299 init_node_meta1(*node, meta1);
300 namep = (*node)->name;
303 case EXFAT_ENTRY_FILE_INFO:
304 if (continuations < 2)
306 exfat_error("unexpected continuation (%hhu)",
310 meta2 = (const struct exfat_entry_meta2*) entry;
311 if (meta2->flags & ~(EXFAT_FLAG_ALWAYS1 | EXFAT_FLAG_CONTIGUOUS))
313 exfat_error("unknown flags in meta2 (0x%hhx)", meta2->flags);
316 init_node_meta2(*node, meta2);
317 actual_checksum = exfat_add_checksum(entry, actual_checksum);
318 real_size = le64_to_cpu(meta2->real_size);
319 /* empty files must be marked as non-contiguous */
320 if ((*node)->size == 0 && (meta2->flags & EXFAT_FLAG_CONTIGUOUS))
322 exfat_error("empty file marked as contiguous (0x%hhx)",
326 /* directories must be aligned on at cluster boundary */
327 if (((*node)->flags & EXFAT_ATTRIB_DIR) &&
328 (*node)->size % CLUSTER_SIZE(*ef->sb) != 0)
330 exfat_error("directory has invalid size %"PRIu64" bytes",
337 case EXFAT_ENTRY_FILE_NAME:
338 if (continuations == 0)
340 exfat_error("unexpected continuation");
343 file_name = (const struct exfat_entry_name*) entry;
344 actual_checksum = exfat_add_checksum(entry, actual_checksum);
346 memcpy(namep, file_name->name,
348 ((*node)->name + EXFAT_NAME_MAX - namep)) *
350 namep += EXFAT_ENAME_MAX;
351 if (--continuations == 0)
353 if (!check_node(*node, actual_checksum, reference_checksum,
356 if (fetch_next_entry(ef, parent, it) != 0)
358 return 0; /* entry completed */
362 case EXFAT_ENTRY_UPCASE:
363 if (ef->upcase != NULL)
365 upcase = (const struct exfat_entry_upcase*) entry;
366 if (CLUSTER_INVALID(le32_to_cpu(upcase->start_cluster)))
368 exfat_error("invalid cluster 0x%x in upcase table",
369 le32_to_cpu(upcase->start_cluster));
372 if (le64_to_cpu(upcase->size) == 0 ||
373 le64_to_cpu(upcase->size) > 0xffff * sizeof(uint16_t) ||
374 le64_to_cpu(upcase->size) % sizeof(uint16_t) != 0)
376 exfat_error("bad upcase table size (%"PRIu64" bytes)",
377 le64_to_cpu(upcase->size));
380 ef->upcase = malloc(le64_to_cpu(upcase->size));
381 if (ef->upcase == NULL)
383 exfat_error("failed to allocate upcase table (%"PRIu64" bytes)",
384 le64_to_cpu(upcase->size));
388 ef->upcase_chars = le64_to_cpu(upcase->size) / sizeof(le16_t);
390 if (exfat_pread(ef->dev, ef->upcase, le64_to_cpu(upcase->size),
391 exfat_c2o(ef, le32_to_cpu(upcase->start_cluster))) < 0)
393 exfat_error("failed to read upper case table "
394 "(%"PRIu64" bytes starting at cluster %#x)",
395 le64_to_cpu(upcase->size),
396 le32_to_cpu(upcase->start_cluster));
401 case EXFAT_ENTRY_BITMAP:
402 bitmap = (const struct exfat_entry_bitmap*) entry;
403 ef->cmap.start_cluster = le32_to_cpu(bitmap->start_cluster);
404 if (CLUSTER_INVALID(ef->cmap.start_cluster))
406 exfat_error("invalid cluster 0x%x in clusters bitmap",
407 ef->cmap.start_cluster);
410 ef->cmap.size = le32_to_cpu(ef->sb->cluster_count) -
411 EXFAT_FIRST_DATA_CLUSTER;
412 if (le64_to_cpu(bitmap->size) < DIV_ROUND_UP(ef->cmap.size, 8))
414 exfat_error("invalid clusters bitmap size: %"PRIu64
415 " (expected at least %u)",
416 le64_to_cpu(bitmap->size),
417 DIV_ROUND_UP(ef->cmap.size, 8));
420 /* FIXME bitmap can be rather big, up to 512 MB */
421 ef->cmap.chunk_size = ef->cmap.size;
422 ef->cmap.chunk = malloc(BMAP_SIZE(ef->cmap.chunk_size));
423 if (ef->cmap.chunk == NULL)
425 exfat_error("failed to allocate clusters bitmap chunk "
426 "(%"PRIu64" bytes)", le64_to_cpu(bitmap->size));
431 if (exfat_pread(ef->dev, ef->cmap.chunk,
432 BMAP_SIZE(ef->cmap.chunk_size),
433 exfat_c2o(ef, ef->cmap.start_cluster)) < 0)
435 exfat_error("failed to read clusters bitmap "
436 "(%"PRIu64" bytes starting at cluster %#x)",
437 le64_to_cpu(bitmap->size), ef->cmap.start_cluster);
442 case EXFAT_ENTRY_LABEL:
443 label = (const struct exfat_entry_label*) entry;
444 if (label->length > EXFAT_ENAME_MAX)
446 exfat_error("too long label (%hhu chars)", label->length);
449 if (utf16_to_utf8(ef->label, label->name,
450 sizeof(ef->label) - 1, EXFAT_ENAME_MAX) != 0)
455 if (entry->type & EXFAT_ENTRY_VALID)
457 exfat_error("unknown entry type 0x%hhx", entry->type);
463 if (fetch_next_entry(ef, parent, it) != 0)
466 /* we never reach here */
474 int exfat_cache_directory(struct exfat* ef, struct exfat_node* dir)
478 struct exfat_node* node;
479 struct exfat_node* current = NULL;
481 if (dir->flags & EXFAT_ATTRIB_CACHED)
482 return 0; /* already cached */
484 rc = opendir(ef, dir, &it);
487 while ((rc = readdir(ef, dir, &node, &it)) == 0)
492 current->next = node;
493 node->prev = current;
505 for (current = dir->child; current; current = node)
507 node = current->next;
514 dir->flags |= EXFAT_ATTRIB_CACHED;
518 static void tree_attach(struct exfat_node* dir, struct exfat_node* node)
523 dir->child->prev = node;
524 node->next = dir->child;
529 static void tree_detach(struct exfat_node* node)
532 node->prev->next = node->next;
533 else /* this is the first node in the list */
534 node->parent->child = node->next;
536 node->next->prev = node->prev;
542 static void reset_cache(struct exfat* ef, struct exfat_node* node)
544 char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
548 struct exfat_node* p = node->child;
553 node->flags &= ~EXFAT_ATTRIB_CACHED;
554 if (node->references != 0)
556 exfat_get_name(node, buffer, sizeof(buffer) - 1);
557 exfat_warn("non-zero reference counter (%d) for `%s'",
558 node->references, buffer);
560 if (node != ef->root && (node->flags & EXFAT_ATTRIB_DIRTY))
562 exfat_get_name(node, buffer, sizeof(buffer) - 1);
563 exfat_bug("node `%s' is dirty", buffer);
565 while (node->references)
566 exfat_put_node(ef, node);
569 void exfat_reset_cache(struct exfat* ef)
571 reset_cache(ef, ef->root);
574 static bool next_entry(struct exfat* ef, const struct exfat_node* parent,
575 cluster_t* cluster, off_t* offset)
577 *offset += sizeof(struct exfat_entry);
578 if (*offset % CLUSTER_SIZE(*ef->sb) == 0)
580 *cluster = exfat_next_cluster(ef, parent, *cluster);
581 if (CLUSTER_INVALID(*cluster))
583 exfat_error("invalid cluster %#x while getting next entry",
591 int exfat_flush_node(struct exfat* ef, struct exfat_node* node)
595 off_t meta1_offset, meta2_offset;
596 struct exfat_entry_meta1 meta1;
597 struct exfat_entry_meta2 meta2;
599 if (!(node->flags & EXFAT_ATTRIB_DIRTY))
600 return 0; /* no need to flush */
603 exfat_bug("unable to flush node to read-only FS");
605 if (node->parent == NULL)
606 return 0; /* do not flush unlinked node */
608 cluster = node->entry_cluster;
609 offset = node->entry_offset;
610 meta1_offset = co2o(ef, cluster, offset);
611 if (!next_entry(ef, node->parent, &cluster, &offset))
613 meta2_offset = co2o(ef, cluster, offset);
615 if (exfat_pread(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
617 exfat_error("failed to read meta1 entry on flush");
620 if (meta1.type != EXFAT_ENTRY_FILE)
621 exfat_bug("invalid type of meta1: 0x%hhx", meta1.type);
622 meta1.attrib = cpu_to_le16(node->flags);
623 exfat_unix2exfat(node->mtime, &meta1.mdate, &meta1.mtime, &meta1.mtime_cs);
624 exfat_unix2exfat(node->atime, &meta1.adate, &meta1.atime, NULL);
626 if (exfat_pread(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
628 exfat_error("failed to read meta2 entry on flush");
631 if (meta2.type != EXFAT_ENTRY_FILE_INFO)
632 exfat_bug("invalid type of meta2: 0x%hhx", meta2.type);
633 meta2.size = meta2.real_size = cpu_to_le64(node->size);
634 meta2.start_cluster = cpu_to_le32(node->start_cluster);
635 meta2.flags = EXFAT_FLAG_ALWAYS1;
636 /* empty files must not be marked as contiguous */
637 if (node->size != 0 && IS_CONTIGUOUS(*node))
638 meta2.flags |= EXFAT_FLAG_CONTIGUOUS;
639 /* name hash remains unchanged, no need to recalculate it */
641 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);
643 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
645 exfat_error("failed to write meta1 entry on flush");
648 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
650 exfat_error("failed to write meta2 entry on flush");
654 node->flags &= ~EXFAT_ATTRIB_DIRTY;
658 static bool erase_entry(struct exfat* ef, struct exfat_node* node)
660 cluster_t cluster = node->entry_cluster;
661 off_t offset = node->entry_offset;
662 int name_entries = DIV_ROUND_UP(utf16_length(node->name), EXFAT_ENAME_MAX);
665 entry_type = EXFAT_ENTRY_FILE & ~EXFAT_ENTRY_VALID;
666 if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
668 exfat_error("failed to erase meta1 entry");
672 if (!next_entry(ef, node->parent, &cluster, &offset))
674 entry_type = EXFAT_ENTRY_FILE_INFO & ~EXFAT_ENTRY_VALID;
675 if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
677 exfat_error("failed to erase meta2 entry");
681 while (name_entries--)
683 if (!next_entry(ef, node->parent, &cluster, &offset))
685 entry_type = EXFAT_ENTRY_FILE_NAME & ~EXFAT_ENTRY_VALID;
686 if (exfat_pwrite(ef->dev, &entry_type, 1,
687 co2o(ef, cluster, offset)) < 0)
689 exfat_error("failed to erase name entry");
696 static int shrink_directory(struct exfat* ef, struct exfat_node* dir,
697 off_t deleted_offset)
699 const struct exfat_node* node;
700 const struct exfat_node* last_node;
701 uint64_t entries = 0;
704 if (!(dir->flags & EXFAT_ATTRIB_DIR))
705 exfat_bug("attempted to shrink a file");
706 if (!(dir->flags & EXFAT_ATTRIB_CACHED))
707 exfat_bug("attempted to shrink uncached directory");
709 for (last_node = node = dir->child; node; node = node->next)
711 if (deleted_offset < node->entry_offset)
713 /* there are other entries after the removed one, no way to shrink
717 if (last_node->entry_offset < node->entry_offset)
723 /* offset of the last entry */
724 entries += last_node->entry_offset / sizeof(struct exfat_entry);
725 /* two subentries with meta info */
727 /* subentries with file name */
728 entries += DIV_ROUND_UP(utf16_length(last_node->name),
732 new_size = DIV_ROUND_UP(entries * sizeof(struct exfat_entry),
733 CLUSTER_SIZE(*ef->sb)) * CLUSTER_SIZE(*ef->sb);
734 if (new_size == 0) /* directory always has at least 1 cluster */
735 new_size = CLUSTER_SIZE(*ef->sb);
736 if (new_size == dir->size)
738 return exfat_truncate(ef, dir, new_size, true);
741 static int delete(struct exfat* ef, struct exfat_node* node)
743 struct exfat_node* parent = node->parent;
744 off_t deleted_offset = node->entry_offset;
747 exfat_get_node(parent);
748 if (!erase_entry(ef, node))
750 exfat_put_node(ef, parent);
753 exfat_update_mtime(parent);
755 rc = shrink_directory(ef, parent, deleted_offset);
756 node->flags |= EXFAT_ATTRIB_UNLINKED;
759 exfat_flush_node(ef, parent);
760 exfat_put_node(ef, parent);
763 rc = exfat_flush_node(ef, parent);
764 exfat_put_node(ef, parent);
768 int exfat_unlink(struct exfat* ef, struct exfat_node* node)
770 if (node->flags & EXFAT_ATTRIB_DIR)
772 return delete(ef, node);
775 int exfat_rmdir(struct exfat* ef, struct exfat_node* node)
779 if (!(node->flags & EXFAT_ATTRIB_DIR))
781 /* check that directory is empty */
782 rc = exfat_cache_directory(ef, node);
787 return delete(ef, node);
790 static int grow_directory(struct exfat* ef, struct exfat_node* dir,
791 uint64_t asize, uint32_t difference)
793 return exfat_truncate(ef, dir,
794 DIV_ROUND_UP(asize + difference, CLUSTER_SIZE(*ef->sb))
795 * CLUSTER_SIZE(*ef->sb), true);
798 static int find_slot(struct exfat* ef, struct exfat_node* dir,
799 cluster_t* cluster, off_t* offset, int subentries)
803 const struct exfat_entry* entry;
806 rc = opendir(ef, dir, &it);
813 *cluster = it.cluster;
816 entry = get_entry_ptr(ef, &it);
817 if (entry->type & EXFAT_ENTRY_VALID)
821 if (contiguous == subentries)
822 break; /* suitable slot is found */
823 if (it.offset + sizeof(struct exfat_entry) >= dir->size)
825 rc = grow_directory(ef, dir, dir->size,
826 (subentries - contiguous) * sizeof(struct exfat_entry));
833 if (fetch_next_entry(ef, dir, &it) != 0)
843 static int write_entry(struct exfat* ef, struct exfat_node* dir,
844 const le16_t* name, cluster_t cluster, off_t offset, uint16_t attrib)
846 struct exfat_node* node;
847 struct exfat_entry_meta1 meta1;
848 struct exfat_entry_meta2 meta2;
849 const size_t name_length = utf16_length(name);
850 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
853 node = allocate_node();
856 node->entry_cluster = cluster;
857 node->entry_offset = offset;
858 memcpy(node->name, name, name_length * sizeof(le16_t));
860 memset(&meta1, 0, sizeof(meta1));
861 meta1.type = EXFAT_ENTRY_FILE;
862 meta1.continuations = 1 + name_entries;
863 meta1.attrib = cpu_to_le16(attrib);
864 exfat_unix2exfat(time(NULL), &meta1.crdate, &meta1.crtime,
866 meta1.adate = meta1.mdate = meta1.crdate;
867 meta1.atime = meta1.mtime = meta1.crtime;
868 meta1.mtime_cs = meta1.crtime_cs; /* there is no atime_cs */
870 memset(&meta2, 0, sizeof(meta2));
871 meta2.type = EXFAT_ENTRY_FILE_INFO;
872 meta2.flags = EXFAT_FLAG_ALWAYS1;
873 meta2.name_length = name_length;
874 meta2.name_hash = exfat_calc_name_hash(ef, node->name);
875 meta2.start_cluster = cpu_to_le32(EXFAT_CLUSTER_FREE);
877 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);
879 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
880 co2o(ef, cluster, offset)) < 0)
882 exfat_error("failed to write meta1 entry");
885 if (!next_entry(ef, dir, &cluster, &offset))
887 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
888 co2o(ef, cluster, offset)) < 0)
890 exfat_error("failed to write meta2 entry");
893 for (i = 0; i < name_entries; i++)
895 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
896 memcpy(name_entry.name, node->name + i * EXFAT_ENAME_MAX,
897 MIN(EXFAT_ENAME_MAX, EXFAT_NAME_MAX - i * EXFAT_ENAME_MAX) *
899 if (!next_entry(ef, dir, &cluster, &offset))
901 if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
902 co2o(ef, cluster, offset)) < 0)
904 exfat_error("failed to write name entry");
909 init_node_meta1(node, &meta1);
910 init_node_meta2(node, &meta2);
912 tree_attach(dir, node);
913 exfat_update_mtime(dir);
917 static int create(struct exfat* ef, const char* path, uint16_t attrib)
919 struct exfat_node* dir;
920 struct exfat_node* existing;
921 cluster_t cluster = EXFAT_CLUSTER_BAD;
923 le16_t name[EXFAT_NAME_MAX + 1];
926 rc = exfat_split(ef, &dir, &existing, name, path);
929 if (existing != NULL)
931 exfat_put_node(ef, existing);
932 exfat_put_node(ef, dir);
936 rc = find_slot(ef, dir, &cluster, &offset,
937 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
940 exfat_put_node(ef, dir);
943 rc = write_entry(ef, dir, name, cluster, offset, attrib);
946 exfat_put_node(ef, dir);
949 rc = exfat_flush_node(ef, dir);
950 exfat_put_node(ef, dir);
954 int exfat_mknod(struct exfat* ef, const char* path)
956 return create(ef, path, EXFAT_ATTRIB_ARCH);
959 int exfat_mkdir(struct exfat* ef, const char* path)
962 struct exfat_node* node;
964 rc = create(ef, path, EXFAT_ATTRIB_ARCH | EXFAT_ATTRIB_DIR);
967 rc = exfat_lookup(ef, &node, path);
970 /* directories always have at least one cluster */
971 rc = exfat_truncate(ef, node, CLUSTER_SIZE(*ef->sb), true);
975 exfat_put_node(ef, node);
978 rc = exfat_flush_node(ef, node);
982 exfat_put_node(ef, node);
985 exfat_put_node(ef, node);
989 static int rename_entry(struct exfat* ef, struct exfat_node* dir,
990 struct exfat_node* node, const le16_t* name, cluster_t new_cluster,
993 struct exfat_entry_meta1 meta1;
994 struct exfat_entry_meta2 meta2;
995 cluster_t old_cluster = node->entry_cluster;
996 off_t old_offset = node->entry_offset;
997 const size_t name_length = utf16_length(name);
998 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
1001 if (exfat_pread(ef->dev, &meta1, sizeof(meta1),
1002 co2o(ef, old_cluster, old_offset)) < 0)
1004 exfat_error("failed to read meta1 entry on rename");
1007 if (!next_entry(ef, node->parent, &old_cluster, &old_offset))
1009 if (exfat_pread(ef->dev, &meta2, sizeof(meta2),
1010 co2o(ef, old_cluster, old_offset)) < 0)
1012 exfat_error("failed to read meta2 entry on rename");
1015 meta1.continuations = 1 + name_entries;
1016 meta2.name_hash = exfat_calc_name_hash(ef, name);
1017 meta2.name_length = name_length;
1018 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, name);
1020 if (!erase_entry(ef, node))
1023 node->entry_cluster = new_cluster;
1024 node->entry_offset = new_offset;
1026 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
1027 co2o(ef, new_cluster, new_offset)) < 0)
1029 exfat_error("failed to write meta1 entry on rename");
1032 if (!next_entry(ef, dir, &new_cluster, &new_offset))
1034 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
1035 co2o(ef, new_cluster, new_offset)) < 0)
1037 exfat_error("failed to write meta2 entry on rename");
1041 for (i = 0; i < name_entries; i++)
1043 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
1044 memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX,
1045 EXFAT_ENAME_MAX * sizeof(le16_t));
1046 if (!next_entry(ef, dir, &new_cluster, &new_offset))
1048 if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
1049 co2o(ef, new_cluster, new_offset)) < 0)
1051 exfat_error("failed to write name entry on rename");
1056 memcpy(node->name, name, (EXFAT_NAME_MAX + 1) * sizeof(le16_t));
1058 tree_attach(dir, node);
1062 int exfat_rename(struct exfat* ef, const char* old_path, const char* new_path)
1064 struct exfat_node* node;
1065 struct exfat_node* existing;
1066 struct exfat_node* dir;
1067 cluster_t cluster = EXFAT_CLUSTER_BAD;
1069 le16_t name[EXFAT_NAME_MAX + 1];
1072 rc = exfat_lookup(ef, &node, old_path);
1076 rc = exfat_split(ef, &dir, &existing, name, new_path);
1079 exfat_put_node(ef, node);
1083 /* check that target is not a subdirectory of the source */
1084 if (node->flags & EXFAT_ATTRIB_DIR)
1086 struct exfat_node* p;
1088 for (p = dir; p; p = p->parent)
1091 if (existing != NULL)
1092 exfat_put_node(ef, existing);
1093 exfat_put_node(ef, dir);
1094 exfat_put_node(ef, node);
1099 if (existing != NULL)
1101 /* remove target if it's not the same node as source */
1102 if (existing != node)
1104 if (existing->flags & EXFAT_ATTRIB_DIR)
1106 if (node->flags & EXFAT_ATTRIB_DIR)
1107 rc = exfat_rmdir(ef, existing);
1113 if (!(node->flags & EXFAT_ATTRIB_DIR))
1114 rc = exfat_unlink(ef, existing);
1118 exfat_put_node(ef, existing);
1121 exfat_put_node(ef, dir);
1122 exfat_put_node(ef, node);
1127 exfat_put_node(ef, existing);
1130 rc = find_slot(ef, dir, &cluster, &offset,
1131 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
1134 exfat_put_node(ef, dir);
1135 exfat_put_node(ef, node);
1138 rc = rename_entry(ef, dir, node, name, cluster, offset);
1139 exfat_put_node(ef, dir);
1140 exfat_put_node(ef, node);
1144 void exfat_utimes(struct exfat_node* node, const struct timespec tv[2])
1146 node->atime = tv[0].tv_sec;
1147 node->mtime = tv[1].tv_sec;
1148 node->flags |= EXFAT_ATTRIB_DIRTY;
1151 void exfat_update_atime(struct exfat_node* node)
1153 node->atime = time(NULL);
1154 node->flags |= EXFAT_ATTRIB_DIRTY;
1157 void exfat_update_mtime(struct exfat_node* node)
1159 node->mtime = time(NULL);
1160 node->flags |= EXFAT_ATTRIB_DIRTY;
1163 const char* exfat_get_label(struct exfat* ef)
1168 static int find_label(struct exfat* ef, cluster_t* cluster, off_t* offset)
1173 rc = opendir(ef, ef->root, &it);
1179 if (it.offset >= ef->root->size)
1185 if (get_entry_ptr(ef, &it)->type == EXFAT_ENTRY_LABEL)
1187 *cluster = it.cluster;
1188 *offset = it.offset;
1193 if (fetch_next_entry(ef, ef->root, &it) != 0)
1201 int exfat_set_label(struct exfat* ef, const char* label)
1203 le16_t label_utf16[EXFAT_ENAME_MAX + 1];
1207 struct exfat_entry_label entry;
1209 memset(label_utf16, 0, sizeof(label_utf16));
1210 rc = utf8_to_utf16(label_utf16, label, EXFAT_ENAME_MAX, strlen(label));
1214 rc = find_label(ef, &cluster, &offset);
1216 rc = find_slot(ef, ef->root, &cluster, &offset, 1);
1220 entry.type = EXFAT_ENTRY_LABEL;
1221 entry.length = utf16_length(label_utf16);
1222 memcpy(entry.name, label_utf16, sizeof(entry.name));
1223 if (entry.length == 0)
1224 entry.type ^= EXFAT_ENTRY_VALID;
1226 if (exfat_pwrite(ef->dev, &entry, sizeof(struct exfat_entry_label),
1227 co2o(ef, cluster, offset)) < 0)
1229 exfat_error("failed to write label entry");
1232 strcpy(ef->label, label);