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 if (--node->references < 0)
49 char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
50 exfat_get_name(node, buffer, sizeof(buffer) - 1);
51 exfat_bug("reference counter of `%s' is below zero", buffer);
54 if (node->references == 0)
56 /* FIXME handle I/O error */
57 if (exfat_flush_node(ef, node) != 0)
58 exfat_bug("node flush failed");
59 if (node->flags & EXFAT_ATTRIB_UNLINKED)
61 /* free all clusters and node structure itself */
62 exfat_truncate(ef, node, 0, true);
65 /* FIXME handle I/O error */
66 if (exfat_flush(ef) != 0)
67 exfat_bug("flush failed");
72 * Cluster + offset from the beginning of the directory to absolute offset.
74 static off_t co2o(struct exfat* ef, cluster_t cluster, off_t offset)
76 return exfat_c2o(ef, cluster) + offset % CLUSTER_SIZE(*ef->sb);
79 static int opendir(struct exfat* ef, const struct exfat_node* dir,
82 if (!(dir->flags & EXFAT_ATTRIB_DIR))
83 exfat_bug("not a directory");
84 it->cluster = dir->start_cluster;
86 it->contiguous = IS_CONTIGUOUS(*dir);
87 it->chunk = malloc(CLUSTER_SIZE(*ef->sb));
88 if (it->chunk == NULL)
90 exfat_error("out of memory");
93 if (exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb),
94 exfat_c2o(ef, it->cluster)) < 0)
96 exfat_error("failed to read directory cluster %#x", it->cluster);
102 static void closedir(struct iterator* it)
111 static int fetch_next_entry(struct exfat* ef, const struct exfat_node* parent,
114 /* move iterator to the next entry in the directory */
115 it->offset += sizeof(struct exfat_entry);
116 /* fetch the next cluster if needed */
117 if ((it->offset & (CLUSTER_SIZE(*ef->sb) - 1)) == 0)
119 /* reached the end of directory; the caller should check this
121 if (it->offset >= parent->size)
123 it->cluster = exfat_next_cluster(ef, parent, it->cluster);
124 if (CLUSTER_INVALID(it->cluster))
126 exfat_error("invalid cluster 0x%x while reading directory",
130 if (exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb),
131 exfat_c2o(ef, it->cluster)) < 0)
133 exfat_error("failed to read the next directory cluster %#x",
141 static struct exfat_node* allocate_node(void)
143 struct exfat_node* node = malloc(sizeof(struct exfat_node));
146 exfat_error("failed to allocate node");
149 memset(node, 0, sizeof(struct exfat_node));
153 static void init_node_meta1(struct exfat_node* node,
154 const struct exfat_entry_meta1* meta1)
156 node->flags = le16_to_cpu(meta1->attrib);
157 node->mtime = exfat_exfat2unix(meta1->mdate, meta1->mtime,
159 /* there is no centiseconds field for atime */
160 node->atime = exfat_exfat2unix(meta1->adate, meta1->atime, 0);
163 static void init_node_meta2(struct exfat_node* node,
164 const struct exfat_entry_meta2* meta2)
166 node->size = le64_to_cpu(meta2->size);
167 node->start_cluster = le32_to_cpu(meta2->start_cluster);
168 node->fptr_cluster = node->start_cluster;
169 if (meta2->flags & EXFAT_FLAG_CONTIGUOUS)
170 node->flags |= EXFAT_ATTRIB_CONTIGUOUS;
173 static const struct exfat_entry* get_entry_ptr(const struct exfat* ef,
174 const struct iterator* it)
176 return (const struct exfat_entry*)
177 (it->chunk + it->offset % CLUSTER_SIZE(*ef->sb));
181 * Reads one entry in directory at position pointed by iterator and fills
184 static int readdir(struct exfat* ef, const struct exfat_node* parent,
185 struct exfat_node** node, struct iterator* it)
188 const struct exfat_entry* entry;
189 const struct exfat_entry_meta1* meta1;
190 const struct exfat_entry_meta2* meta2;
191 const struct exfat_entry_name* file_name;
192 const struct exfat_entry_upcase* upcase;
193 const struct exfat_entry_bitmap* bitmap;
194 const struct exfat_entry_label* label;
195 uint8_t continuations = 0;
196 le16_t* namep = NULL;
197 uint16_t reference_checksum = 0;
198 uint16_t actual_checksum = 0;
199 uint64_t real_size = 0;
205 if (it->offset >= parent->size)
207 if (continuations != 0)
209 exfat_error("expected %hhu continuations", continuations);
212 return -ENOENT; /* that's OK, means end of directory */
215 entry = get_entry_ptr(ef, it);
218 case EXFAT_ENTRY_FILE:
219 if (continuations != 0)
221 exfat_error("expected %hhu continuations before new entry",
225 meta1 = (const struct exfat_entry_meta1*) entry;
226 continuations = meta1->continuations;
227 /* each file entry must have at least 2 continuations:
229 if (continuations < 2)
231 exfat_error("too few continuations (%hhu)", continuations);
234 if (continuations > 1 +
235 DIV_ROUND_UP(EXFAT_NAME_MAX, EXFAT_ENAME_MAX))
237 exfat_error("too many continuations (%hhu)", continuations);
240 reference_checksum = le16_to_cpu(meta1->checksum);
241 actual_checksum = exfat_start_checksum(meta1);
242 *node = allocate_node();
248 /* new node has zero reference counter */
249 (*node)->entry_cluster = it->cluster;
250 (*node)->entry_offset = it->offset;
251 init_node_meta1(*node, meta1);
252 namep = (*node)->name;
255 case EXFAT_ENTRY_FILE_INFO:
256 if (continuations < 2)
258 exfat_error("unexpected continuation (%hhu)",
262 meta2 = (const struct exfat_entry_meta2*) entry;
263 if (meta2->flags & ~(EXFAT_FLAG_ALWAYS1 | EXFAT_FLAG_CONTIGUOUS))
265 exfat_error("unknown flags in meta2 (0x%hhx)", meta2->flags);
268 init_node_meta2(*node, meta2);
269 actual_checksum = exfat_add_checksum(entry, actual_checksum);
270 real_size = le64_to_cpu(meta2->real_size);
271 /* empty files must be marked as non-contiguous */
272 if ((*node)->size == 0 && (meta2->flags & EXFAT_FLAG_CONTIGUOUS))
274 exfat_error("empty file marked as contiguous (0x%hhx)",
278 /* directories must be aligned on at cluster boundary */
279 if (((*node)->flags & EXFAT_ATTRIB_DIR) &&
280 (*node)->size % CLUSTER_SIZE(*ef->sb) != 0)
282 exfat_error("directory has invalid size %"PRIu64" bytes",
289 case EXFAT_ENTRY_FILE_NAME:
290 if (continuations == 0)
292 exfat_error("unexpected continuation");
295 file_name = (const struct exfat_entry_name*) entry;
296 actual_checksum = exfat_add_checksum(entry, actual_checksum);
298 memcpy(namep, file_name->name,
300 ((*node)->name + EXFAT_NAME_MAX - namep)) *
302 namep += EXFAT_ENAME_MAX;
303 if (--continuations == 0)
306 There are two fields that contain file size. Maybe they
307 plan to add compression support in the future and one of
308 those fields is visible (uncompressed) size and the other
309 is real (compressed) size. Anyway, currently it looks like
310 exFAT does not support compression and both fields must be
313 There is an exception though: pagefile.sys (its real_size
316 if (real_size != (*node)->size)
318 char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
320 exfat_get_name(*node, buffer, sizeof(buffer) - 1);
321 exfat_error("`%s' real size does not equal to size "
322 "(%"PRIu64" != %"PRIu64")", buffer,
323 real_size, (*node)->size);
326 if (actual_checksum != reference_checksum)
328 char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
330 exfat_get_name(*node, buffer, sizeof(buffer) - 1);
331 exfat_error("`%s' has invalid checksum (0x%hx != 0x%hx)",
332 buffer, actual_checksum, reference_checksum);
335 if (fetch_next_entry(ef, parent, it) != 0)
337 return 0; /* entry completed */
341 case EXFAT_ENTRY_UPCASE:
342 if (ef->upcase != NULL)
344 upcase = (const struct exfat_entry_upcase*) entry;
345 if (CLUSTER_INVALID(le32_to_cpu(upcase->start_cluster)))
347 exfat_error("invalid cluster 0x%x in upcase table",
348 le32_to_cpu(upcase->start_cluster));
351 if (le64_to_cpu(upcase->size) == 0 ||
352 le64_to_cpu(upcase->size) > 0xffff * sizeof(uint16_t) ||
353 le64_to_cpu(upcase->size) % sizeof(uint16_t) != 0)
355 exfat_error("bad upcase table size (%"PRIu64" bytes)",
356 le64_to_cpu(upcase->size));
359 ef->upcase = malloc(le64_to_cpu(upcase->size));
360 if (ef->upcase == NULL)
362 exfat_error("failed to allocate upcase table (%"PRIu64" bytes)",
363 le64_to_cpu(upcase->size));
367 ef->upcase_chars = le64_to_cpu(upcase->size) / sizeof(le16_t);
369 if (exfat_pread(ef->dev, ef->upcase, le64_to_cpu(upcase->size),
370 exfat_c2o(ef, le32_to_cpu(upcase->start_cluster))) < 0)
372 exfat_error("failed to read upper case table "
373 "(%"PRIu64" bytes starting at cluster %#x)",
374 le64_to_cpu(upcase->size),
375 le32_to_cpu(upcase->start_cluster));
380 case EXFAT_ENTRY_BITMAP:
381 bitmap = (const struct exfat_entry_bitmap*) entry;
382 ef->cmap.start_cluster = le32_to_cpu(bitmap->start_cluster);
383 if (CLUSTER_INVALID(ef->cmap.start_cluster))
385 exfat_error("invalid cluster 0x%x in clusters bitmap",
386 ef->cmap.start_cluster);
389 ef->cmap.size = le32_to_cpu(ef->sb->cluster_count) -
390 EXFAT_FIRST_DATA_CLUSTER;
391 if (le64_to_cpu(bitmap->size) < DIV_ROUND_UP(ef->cmap.size, 8))
393 exfat_error("invalid clusters bitmap size: %"PRIu64
394 " (expected at least %u)",
395 le64_to_cpu(bitmap->size),
396 DIV_ROUND_UP(ef->cmap.size, 8));
399 /* FIXME bitmap can be rather big, up to 512 MB */
400 ef->cmap.chunk_size = ef->cmap.size;
401 ef->cmap.chunk = malloc(BMAP_SIZE(ef->cmap.chunk_size));
402 if (ef->cmap.chunk == NULL)
404 exfat_error("failed to allocate clusters bitmap chunk "
405 "(%"PRIu64" bytes)", le64_to_cpu(bitmap->size));
410 if (exfat_pread(ef->dev, ef->cmap.chunk,
411 BMAP_SIZE(ef->cmap.chunk_size),
412 exfat_c2o(ef, ef->cmap.start_cluster)) < 0)
414 exfat_error("failed to read clusters bitmap "
415 "(%"PRIu64" bytes starting at cluster %#x)",
416 le64_to_cpu(bitmap->size), ef->cmap.start_cluster);
421 case EXFAT_ENTRY_LABEL:
422 label = (const struct exfat_entry_label*) entry;
423 if (label->length > EXFAT_ENAME_MAX)
425 exfat_error("too long label (%hhu chars)", label->length);
428 if (utf16_to_utf8(ef->label, label->name,
429 sizeof(ef->label) - 1, EXFAT_ENAME_MAX) != 0)
434 if (entry->type & EXFAT_ENTRY_VALID)
436 exfat_error("unknown entry type 0x%hhx", entry->type);
442 if (fetch_next_entry(ef, parent, it) != 0)
445 /* we never reach here */
453 int exfat_cache_directory(struct exfat* ef, struct exfat_node* dir)
457 struct exfat_node* node;
458 struct exfat_node* current = NULL;
460 if (dir->flags & EXFAT_ATTRIB_CACHED)
461 return 0; /* already cached */
463 rc = opendir(ef, dir, &it);
466 while ((rc = readdir(ef, dir, &node, &it)) == 0)
471 current->next = node;
472 node->prev = current;
484 for (current = dir->child; current; current = node)
486 node = current->next;
493 dir->flags |= EXFAT_ATTRIB_CACHED;
497 static void tree_attach(struct exfat_node* dir, struct exfat_node* node)
502 dir->child->prev = node;
503 node->next = dir->child;
508 static void tree_detach(struct exfat_node* node)
511 node->prev->next = node->next;
512 else /* this is the first node in the list */
513 node->parent->child = node->next;
515 node->next->prev = node->prev;
521 static void reset_cache(struct exfat* ef, struct exfat_node* node)
525 struct exfat_node* p = node->child;
530 node->flags &= ~EXFAT_ATTRIB_CACHED;
531 if (node->references != 0)
533 char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
534 exfat_get_name(node, buffer, sizeof(buffer) - 1);
535 exfat_warn("non-zero reference counter (%d) for `%s'",
536 node->references, buffer);
538 while (node->references)
539 exfat_put_node(ef, node);
542 void exfat_reset_cache(struct exfat* ef)
544 reset_cache(ef, ef->root);
547 static bool next_entry(struct exfat* ef, const struct exfat_node* parent,
548 cluster_t* cluster, off_t* offset)
550 *offset += sizeof(struct exfat_entry);
551 if (*offset % CLUSTER_SIZE(*ef->sb) == 0)
553 *cluster = exfat_next_cluster(ef, parent, *cluster);
554 if (CLUSTER_INVALID(*cluster))
556 exfat_error("invalid cluster %#x while getting next entry",
564 int exfat_flush_node(struct exfat* ef, struct exfat_node* node)
568 off_t meta1_offset, meta2_offset;
569 struct exfat_entry_meta1 meta1;
570 struct exfat_entry_meta2 meta2;
572 if (!(node->flags & EXFAT_ATTRIB_DIRTY))
573 return 0; /* no need to flush */
576 exfat_bug("unable to flush node to read-only FS");
578 if (node->parent == NULL)
579 return 0; /* do not flush unlinked node */
581 cluster = node->entry_cluster;
582 offset = node->entry_offset;
583 meta1_offset = co2o(ef, cluster, offset);
584 if (!next_entry(ef, node->parent, &cluster, &offset))
586 meta2_offset = co2o(ef, cluster, offset);
588 if (exfat_pread(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
590 exfat_error("failed to read meta1 entry on flush");
593 if (meta1.type != EXFAT_ENTRY_FILE)
594 exfat_bug("invalid type of meta1: 0x%hhx", meta1.type);
595 meta1.attrib = cpu_to_le16(node->flags);
596 exfat_unix2exfat(node->mtime, &meta1.mdate, &meta1.mtime, &meta1.mtime_cs);
597 exfat_unix2exfat(node->atime, &meta1.adate, &meta1.atime, NULL);
599 if (exfat_pread(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
601 exfat_error("failed to read meta2 entry on flush");
604 if (meta2.type != EXFAT_ENTRY_FILE_INFO)
605 exfat_bug("invalid type of meta2: 0x%hhx", meta2.type);
606 meta2.size = meta2.real_size = cpu_to_le64(node->size);
607 meta2.start_cluster = cpu_to_le32(node->start_cluster);
608 meta2.flags = EXFAT_FLAG_ALWAYS1;
609 /* empty files must not be marked as contiguous */
610 if (node->size != 0 && IS_CONTIGUOUS(*node))
611 meta2.flags |= EXFAT_FLAG_CONTIGUOUS;
612 /* name hash remains unchanged, no need to recalculate it */
614 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);
616 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
618 exfat_error("failed to write meta1 entry on flush");
621 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
623 exfat_error("failed to write meta2 entry on flush");
627 node->flags &= ~EXFAT_ATTRIB_DIRTY;
631 static bool erase_entry(struct exfat* ef, struct exfat_node* node)
633 cluster_t cluster = node->entry_cluster;
634 off_t offset = node->entry_offset;
635 int name_entries = DIV_ROUND_UP(utf16_length(node->name), EXFAT_ENAME_MAX);
638 entry_type = EXFAT_ENTRY_FILE & ~EXFAT_ENTRY_VALID;
639 if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
641 exfat_error("failed to erase meta1 entry");
645 if (!next_entry(ef, node->parent, &cluster, &offset))
647 entry_type = EXFAT_ENTRY_FILE_INFO & ~EXFAT_ENTRY_VALID;
648 if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
650 exfat_error("failed to erase meta2 entry");
654 while (name_entries--)
656 if (!next_entry(ef, node->parent, &cluster, &offset))
658 entry_type = EXFAT_ENTRY_FILE_NAME & ~EXFAT_ENTRY_VALID;
659 if (exfat_pwrite(ef->dev, &entry_type, 1,
660 co2o(ef, cluster, offset)) < 0)
662 exfat_error("failed to erase name entry");
669 static int shrink_directory(struct exfat* ef, struct exfat_node* dir,
670 off_t deleted_offset)
672 const struct exfat_node* node;
673 const struct exfat_node* last_node;
674 uint64_t entries = 0;
677 if (!(dir->flags & EXFAT_ATTRIB_DIR))
678 exfat_bug("attempted to shrink a file");
679 if (!(dir->flags & EXFAT_ATTRIB_CACHED))
680 exfat_bug("attempted to shrink uncached directory");
682 for (last_node = node = dir->child; node; node = node->next)
684 if (deleted_offset < node->entry_offset)
686 /* there are other entries after the removed one, no way to shrink
690 if (last_node->entry_offset < node->entry_offset)
696 /* offset of the last entry */
697 entries += last_node->entry_offset / sizeof(struct exfat_entry);
698 /* two subentries with meta info */
700 /* subentries with file name */
701 entries += DIV_ROUND_UP(utf16_length(last_node->name),
705 new_size = DIV_ROUND_UP(entries * sizeof(struct exfat_entry),
706 CLUSTER_SIZE(*ef->sb)) * CLUSTER_SIZE(*ef->sb);
707 if (new_size == 0) /* directory always has at least 1 cluster */
708 new_size = CLUSTER_SIZE(*ef->sb);
709 if (new_size == dir->size)
711 return exfat_truncate(ef, dir, new_size, true);
714 static int delete(struct exfat* ef, struct exfat_node* node)
716 struct exfat_node* parent = node->parent;
717 off_t deleted_offset = node->entry_offset;
720 exfat_get_node(parent);
721 if (!erase_entry(ef, node))
723 exfat_put_node(ef, parent);
726 exfat_update_mtime(parent);
728 rc = shrink_directory(ef, parent, deleted_offset);
729 exfat_put_node(ef, parent);
730 /* file clusters will be freed when node reference counter becomes 0 */
731 node->flags |= EXFAT_ATTRIB_UNLINKED;
735 int exfat_unlink(struct exfat* ef, struct exfat_node* node)
737 if (node->flags & EXFAT_ATTRIB_DIR)
739 return delete(ef, node);
742 int exfat_rmdir(struct exfat* ef, struct exfat_node* node)
744 if (!(node->flags & EXFAT_ATTRIB_DIR))
746 /* check that directory is empty */
747 exfat_cache_directory(ef, node);
750 return delete(ef, node);
753 static int grow_directory(struct exfat* ef, struct exfat_node* dir,
754 uint64_t asize, uint32_t difference)
756 return exfat_truncate(ef, dir,
757 DIV_ROUND_UP(asize + difference, CLUSTER_SIZE(*ef->sb))
758 * CLUSTER_SIZE(*ef->sb), true);
761 static int find_slot(struct exfat* ef, struct exfat_node* dir,
762 cluster_t* cluster, off_t* offset, int subentries)
766 const struct exfat_entry* entry;
769 rc = opendir(ef, dir, &it);
776 *cluster = it.cluster;
779 entry = get_entry_ptr(ef, &it);
780 if (entry->type & EXFAT_ENTRY_VALID)
784 if (contiguous == subentries)
785 break; /* suitable slot is found */
786 if (it.offset + sizeof(struct exfat_entry) >= dir->size)
788 rc = grow_directory(ef, dir, dir->size,
789 (subentries - contiguous) * sizeof(struct exfat_entry));
796 if (fetch_next_entry(ef, dir, &it) != 0)
806 static int write_entry(struct exfat* ef, struct exfat_node* dir,
807 const le16_t* name, cluster_t cluster, off_t offset, uint16_t attrib)
809 struct exfat_node* node;
810 struct exfat_entry_meta1 meta1;
811 struct exfat_entry_meta2 meta2;
812 const size_t name_length = utf16_length(name);
813 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
816 node = allocate_node();
819 node->entry_cluster = cluster;
820 node->entry_offset = offset;
821 memcpy(node->name, name, name_length * sizeof(le16_t));
823 memset(&meta1, 0, sizeof(meta1));
824 meta1.type = EXFAT_ENTRY_FILE;
825 meta1.continuations = 1 + name_entries;
826 meta1.attrib = cpu_to_le16(attrib);
827 exfat_unix2exfat(time(NULL), &meta1.crdate, &meta1.crtime,
829 meta1.adate = meta1.mdate = meta1.crdate;
830 meta1.atime = meta1.mtime = meta1.crtime;
831 meta1.mtime_cs = meta1.crtime_cs; /* there is no atime_cs */
833 memset(&meta2, 0, sizeof(meta2));
834 meta2.type = EXFAT_ENTRY_FILE_INFO;
835 meta2.flags = EXFAT_FLAG_ALWAYS1;
836 meta2.name_length = name_length;
837 meta2.name_hash = exfat_calc_name_hash(ef, node->name);
838 meta2.start_cluster = cpu_to_le32(EXFAT_CLUSTER_FREE);
840 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);
842 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
843 co2o(ef, cluster, offset)) < 0)
845 exfat_error("failed to write meta1 entry");
848 if (!next_entry(ef, dir, &cluster, &offset))
850 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
851 co2o(ef, cluster, offset)) < 0)
853 exfat_error("failed to write meta2 entry");
856 for (i = 0; i < name_entries; i++)
858 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
859 memcpy(name_entry.name, node->name + i * EXFAT_ENAME_MAX,
860 MIN(EXFAT_ENAME_MAX, EXFAT_NAME_MAX - i * EXFAT_ENAME_MAX) *
862 if (!next_entry(ef, dir, &cluster, &offset))
864 if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
865 co2o(ef, cluster, offset)) < 0)
867 exfat_error("failed to write name entry");
872 init_node_meta1(node, &meta1);
873 init_node_meta2(node, &meta2);
875 tree_attach(dir, node);
876 exfat_update_mtime(dir);
880 static int create(struct exfat* ef, const char* path, uint16_t attrib)
882 struct exfat_node* dir;
883 struct exfat_node* existing;
884 cluster_t cluster = EXFAT_CLUSTER_BAD;
886 le16_t name[EXFAT_NAME_MAX + 1];
889 rc = exfat_split(ef, &dir, &existing, name, path);
892 if (existing != NULL)
894 exfat_put_node(ef, existing);
895 exfat_put_node(ef, dir);
899 rc = find_slot(ef, dir, &cluster, &offset,
900 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
903 exfat_put_node(ef, dir);
906 rc = write_entry(ef, dir, name, cluster, offset, attrib);
907 exfat_put_node(ef, dir);
911 int exfat_mknod(struct exfat* ef, const char* path)
913 return create(ef, path, EXFAT_ATTRIB_ARCH);
916 int exfat_mkdir(struct exfat* ef, const char* path)
919 struct exfat_node* node;
921 rc = create(ef, path, EXFAT_ATTRIB_ARCH | EXFAT_ATTRIB_DIR);
924 rc = exfat_lookup(ef, &node, path);
927 /* directories always have at least one cluster */
928 rc = exfat_truncate(ef, node, CLUSTER_SIZE(*ef->sb), true);
932 exfat_put_node(ef, node);
935 exfat_put_node(ef, node);
939 static int rename_entry(struct exfat* ef, struct exfat_node* dir,
940 struct exfat_node* node, const le16_t* name, cluster_t new_cluster,
943 struct exfat_entry_meta1 meta1;
944 struct exfat_entry_meta2 meta2;
945 cluster_t old_cluster = node->entry_cluster;
946 off_t old_offset = node->entry_offset;
947 const size_t name_length = utf16_length(name);
948 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
951 if (exfat_pread(ef->dev, &meta1, sizeof(meta1),
952 co2o(ef, old_cluster, old_offset)) < 0)
954 exfat_error("failed to read meta1 entry on rename");
957 if (!next_entry(ef, node->parent, &old_cluster, &old_offset))
959 if (exfat_pread(ef->dev, &meta2, sizeof(meta2),
960 co2o(ef, old_cluster, old_offset)) < 0)
962 exfat_error("failed to read meta2 entry on rename");
965 meta1.continuations = 1 + name_entries;
966 meta2.name_hash = exfat_calc_name_hash(ef, name);
967 meta2.name_length = name_length;
968 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, name);
970 if (!erase_entry(ef, node))
973 node->entry_cluster = new_cluster;
974 node->entry_offset = new_offset;
976 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
977 co2o(ef, new_cluster, new_offset)) < 0)
979 exfat_error("failed to write meta1 entry on rename");
982 if (!next_entry(ef, dir, &new_cluster, &new_offset))
984 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
985 co2o(ef, new_cluster, new_offset)) < 0)
987 exfat_error("failed to write meta2 entry on rename");
991 for (i = 0; i < name_entries; i++)
993 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
994 memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX,
995 EXFAT_ENAME_MAX * sizeof(le16_t));
996 if (!next_entry(ef, dir, &new_cluster, &new_offset))
998 if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
999 co2o(ef, new_cluster, new_offset)) < 0)
1001 exfat_error("failed to write name entry on rename");
1006 memcpy(node->name, name, (EXFAT_NAME_MAX + 1) * sizeof(le16_t));
1008 tree_attach(dir, node);
1012 int exfat_rename(struct exfat* ef, const char* old_path, const char* new_path)
1014 struct exfat_node* node;
1015 struct exfat_node* existing;
1016 struct exfat_node* dir;
1017 cluster_t cluster = EXFAT_CLUSTER_BAD;
1019 le16_t name[EXFAT_NAME_MAX + 1];
1022 rc = exfat_lookup(ef, &node, old_path);
1026 rc = exfat_split(ef, &dir, &existing, name, new_path);
1029 exfat_put_node(ef, node);
1033 /* check that target is not a subdirectory of the source */
1034 if (node->flags & EXFAT_ATTRIB_DIR)
1036 struct exfat_node* p;
1038 for (p = dir; p; p = p->parent)
1041 if (existing != NULL)
1042 exfat_put_node(ef, existing);
1043 exfat_put_node(ef, dir);
1044 exfat_put_node(ef, node);
1049 if (existing != NULL)
1051 /* remove target if it's not the same node as source */
1052 if (existing != node)
1054 if (existing->flags & EXFAT_ATTRIB_DIR)
1056 if (node->flags & EXFAT_ATTRIB_DIR)
1057 rc = exfat_rmdir(ef, existing);
1063 if (!(node->flags & EXFAT_ATTRIB_DIR))
1064 rc = exfat_unlink(ef, existing);
1068 exfat_put_node(ef, existing);
1071 exfat_put_node(ef, dir);
1072 exfat_put_node(ef, node);
1077 exfat_put_node(ef, existing);
1080 rc = find_slot(ef, dir, &cluster, &offset,
1081 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
1084 exfat_put_node(ef, dir);
1085 exfat_put_node(ef, node);
1088 rc = rename_entry(ef, dir, node, name, cluster, offset);
1089 exfat_put_node(ef, dir);
1090 exfat_put_node(ef, node);
1094 void exfat_utimes(struct exfat_node* node, const struct timespec tv[2])
1096 node->atime = tv[0].tv_sec;
1097 node->mtime = tv[1].tv_sec;
1098 node->flags |= EXFAT_ATTRIB_DIRTY;
1101 void exfat_update_atime(struct exfat_node* node)
1103 node->atime = time(NULL);
1104 node->flags |= EXFAT_ATTRIB_DIRTY;
1107 void exfat_update_mtime(struct exfat_node* node)
1109 node->mtime = time(NULL);
1110 node->flags |= EXFAT_ATTRIB_DIRTY;
1113 const char* exfat_get_label(struct exfat* ef)
1118 static int find_label(struct exfat* ef, cluster_t* cluster, off_t* offset)
1123 rc = opendir(ef, ef->root, &it);
1129 if (it.offset >= ef->root->size)
1135 if (get_entry_ptr(ef, &it)->type == EXFAT_ENTRY_LABEL)
1137 *cluster = it.cluster;
1138 *offset = it.offset;
1143 if (fetch_next_entry(ef, ef->root, &it) != 0)
1151 int exfat_set_label(struct exfat* ef, const char* label)
1153 le16_t label_utf16[EXFAT_ENAME_MAX + 1];
1157 struct exfat_entry_label entry;
1159 memset(label_utf16, 0, sizeof(label_utf16));
1160 rc = utf8_to_utf16(label_utf16, label, EXFAT_ENAME_MAX, strlen(label));
1164 rc = find_label(ef, &cluster, &offset);
1166 rc = find_slot(ef, ef->root, &cluster, &offset, 1);
1170 entry.type = EXFAT_ENTRY_LABEL;
1171 entry.length = utf16_length(label_utf16);
1172 memcpy(entry.name, label_utf16, sizeof(entry.name));
1173 if (entry.length == 0)
1174 entry.type ^= EXFAT_ENTRY_VALID;
1176 if (exfat_pwrite(ef->dev, &entry, sizeof(struct exfat_entry_label),
1177 co2o(ef, cluster, offset)) < 0)
1179 exfat_error("failed to write label entry");
1182 strcpy(ef->label, label);