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);
70 * Cluster + offset from the beginning of the directory to absolute offset.
72 static off_t co2o(struct exfat* ef, cluster_t cluster, off_t offset)
74 return exfat_c2o(ef, cluster) + offset % CLUSTER_SIZE(*ef->sb);
77 static int opendir(struct exfat* ef, const struct exfat_node* dir,
80 if (!(dir->flags & EXFAT_ATTRIB_DIR))
81 exfat_bug("not a directory");
82 it->cluster = dir->start_cluster;
84 it->contiguous = IS_CONTIGUOUS(*dir);
85 it->chunk = malloc(CLUSTER_SIZE(*ef->sb));
86 if (it->chunk == NULL)
88 exfat_error("out of memory");
91 if (exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb),
92 exfat_c2o(ef, it->cluster)) < 0)
94 exfat_error("failed to read directory cluster %#x", it->cluster);
100 static void closedir(struct iterator* it)
109 static int fetch_next_entry(struct exfat* ef, const struct exfat_node* parent,
112 /* move iterator to the next entry in the directory */
113 it->offset += sizeof(struct exfat_entry);
114 /* fetch the next cluster if needed */
115 if ((it->offset & (CLUSTER_SIZE(*ef->sb) - 1)) == 0)
117 /* reached the end of directory; the caller should check this
119 if (it->offset >= parent->size)
121 it->cluster = exfat_next_cluster(ef, parent, it->cluster);
122 if (CLUSTER_INVALID(it->cluster))
124 exfat_error("invalid cluster 0x%x while reading directory",
128 if (exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb),
129 exfat_c2o(ef, it->cluster)) < 0)
131 exfat_error("failed to read the next directory cluster %#x",
139 static struct exfat_node* allocate_node(void)
141 struct exfat_node* node = malloc(sizeof(struct exfat_node));
144 exfat_error("failed to allocate node");
147 memset(node, 0, sizeof(struct exfat_node));
151 static void init_node_meta1(struct exfat_node* node,
152 const struct exfat_entry_meta1* meta1)
154 node->flags = le16_to_cpu(meta1->attrib);
155 node->mtime = exfat_exfat2unix(meta1->mdate, meta1->mtime,
157 /* there is no centiseconds field for atime */
158 node->atime = exfat_exfat2unix(meta1->adate, meta1->atime, 0);
161 static void init_node_meta2(struct exfat_node* node,
162 const struct exfat_entry_meta2* meta2)
164 node->size = le64_to_cpu(meta2->size);
165 node->start_cluster = le32_to_cpu(meta2->start_cluster);
166 node->fptr_cluster = node->start_cluster;
167 if (meta2->flags & EXFAT_FLAG_CONTIGUOUS)
168 node->flags |= EXFAT_ATTRIB_CONTIGUOUS;
171 static const struct exfat_entry* get_entry_ptr(const struct exfat* ef,
172 const struct iterator* it)
174 return (const struct exfat_entry*)
175 (it->chunk + it->offset % CLUSTER_SIZE(*ef->sb));
179 * Reads one entry in directory at position pointed by iterator and fills
182 static int readdir(struct exfat* ef, const struct exfat_node* parent,
183 struct exfat_node** node, struct iterator* it)
186 const struct exfat_entry* entry;
187 const struct exfat_entry_meta1* meta1;
188 const struct exfat_entry_meta2* meta2;
189 const struct exfat_entry_name* file_name;
190 const struct exfat_entry_upcase* upcase;
191 const struct exfat_entry_bitmap* bitmap;
192 const struct exfat_entry_label* label;
193 uint8_t continuations = 0;
194 le16_t* namep = NULL;
195 uint16_t reference_checksum = 0;
196 uint16_t actual_checksum = 0;
197 uint64_t real_size = 0;
203 if (it->offset >= parent->size)
205 if (continuations != 0)
207 exfat_error("expected %hhu continuations", continuations);
210 return -ENOENT; /* that's OK, means end of directory */
213 entry = get_entry_ptr(ef, it);
216 case EXFAT_ENTRY_FILE:
217 if (continuations != 0)
219 exfat_error("expected %hhu continuations before new entry",
223 meta1 = (const struct exfat_entry_meta1*) entry;
224 continuations = meta1->continuations;
225 /* each file entry must have at least 2 continuations:
227 if (continuations < 2)
229 exfat_error("too few continuations (%hhu)", continuations);
232 if (continuations > 1 +
233 DIV_ROUND_UP(EXFAT_NAME_MAX, EXFAT_ENAME_MAX))
235 exfat_error("too many continuations (%hhu)", continuations);
238 reference_checksum = le16_to_cpu(meta1->checksum);
239 actual_checksum = exfat_start_checksum(meta1);
240 *node = allocate_node();
246 /* new node has zero reference counter */
247 (*node)->entry_cluster = it->cluster;
248 (*node)->entry_offset = it->offset;
249 init_node_meta1(*node, meta1);
250 namep = (*node)->name;
253 case EXFAT_ENTRY_FILE_INFO:
254 if (continuations < 2)
256 exfat_error("unexpected continuation (%hhu)",
260 meta2 = (const struct exfat_entry_meta2*) entry;
261 if (meta2->flags & ~(EXFAT_FLAG_ALWAYS1 | EXFAT_FLAG_CONTIGUOUS))
263 exfat_error("unknown flags in meta2 (0x%hhx)", meta2->flags);
266 init_node_meta2(*node, meta2);
267 actual_checksum = exfat_add_checksum(entry, actual_checksum);
268 real_size = le64_to_cpu(meta2->real_size);
269 /* empty files must be marked as non-contiguous */
270 if ((*node)->size == 0 && (meta2->flags & EXFAT_FLAG_CONTIGUOUS))
272 exfat_error("empty file marked as contiguous (0x%hhx)",
276 /* directories must be aligned on at cluster boundary */
277 if (((*node)->flags & EXFAT_ATTRIB_DIR) &&
278 (*node)->size % CLUSTER_SIZE(*ef->sb) != 0)
280 exfat_error("directory has invalid size %"PRIu64" bytes",
287 case EXFAT_ENTRY_FILE_NAME:
288 if (continuations == 0)
290 exfat_error("unexpected continuation");
293 file_name = (const struct exfat_entry_name*) entry;
294 actual_checksum = exfat_add_checksum(entry, actual_checksum);
296 memcpy(namep, file_name->name,
298 ((*node)->name + EXFAT_NAME_MAX - namep)) *
300 namep += EXFAT_ENAME_MAX;
301 if (--continuations == 0)
304 There are two fields that contain file size. Maybe they
305 plan to add compression support in the future and one of
306 those fields is visible (uncompressed) size and the other
307 is real (compressed) size. Anyway, currently it looks like
308 exFAT does not support compression and both fields must be
311 There is an exception though: pagefile.sys (its real_size
314 if (real_size != (*node)->size)
316 char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
318 exfat_get_name(*node, buffer, sizeof(buffer) - 1);
319 exfat_error("`%s' real size does not equal to size "
320 "(%"PRIu64" != %"PRIu64")", buffer,
321 real_size, (*node)->size);
324 if (actual_checksum != reference_checksum)
326 char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
328 exfat_get_name(*node, buffer, sizeof(buffer) - 1);
329 exfat_error("`%s' has invalid checksum (0x%hx != 0x%hx)",
330 buffer, actual_checksum, reference_checksum);
333 if (fetch_next_entry(ef, parent, it) != 0)
335 return 0; /* entry completed */
339 case EXFAT_ENTRY_UPCASE:
340 if (ef->upcase != NULL)
342 upcase = (const struct exfat_entry_upcase*) entry;
343 if (CLUSTER_INVALID(le32_to_cpu(upcase->start_cluster)))
345 exfat_error("invalid cluster 0x%x in upcase table",
346 le32_to_cpu(upcase->start_cluster));
349 if (le64_to_cpu(upcase->size) == 0 ||
350 le64_to_cpu(upcase->size) > 0xffff * sizeof(uint16_t) ||
351 le64_to_cpu(upcase->size) % sizeof(uint16_t) != 0)
353 exfat_error("bad upcase table size (%"PRIu64" bytes)",
354 le64_to_cpu(upcase->size));
357 ef->upcase = malloc(le64_to_cpu(upcase->size));
358 if (ef->upcase == NULL)
360 exfat_error("failed to allocate upcase table (%"PRIu64" bytes)",
361 le64_to_cpu(upcase->size));
365 ef->upcase_chars = le64_to_cpu(upcase->size) / sizeof(le16_t);
367 if (exfat_pread(ef->dev, ef->upcase, le64_to_cpu(upcase->size),
368 exfat_c2o(ef, le32_to_cpu(upcase->start_cluster))) < 0)
370 exfat_error("failed to read upper case table "
371 "(%"PRIu64" bytes starting at cluster %#x)",
372 le64_to_cpu(upcase->size),
373 le32_to_cpu(upcase->start_cluster));
378 case EXFAT_ENTRY_BITMAP:
379 bitmap = (const struct exfat_entry_bitmap*) entry;
380 ef->cmap.start_cluster = le32_to_cpu(bitmap->start_cluster);
381 if (CLUSTER_INVALID(ef->cmap.start_cluster))
383 exfat_error("invalid cluster 0x%x in clusters bitmap",
384 ef->cmap.start_cluster);
387 ef->cmap.size = le32_to_cpu(ef->sb->cluster_count) -
388 EXFAT_FIRST_DATA_CLUSTER;
389 if (le64_to_cpu(bitmap->size) < DIV_ROUND_UP(ef->cmap.size, 8))
391 exfat_error("invalid clusters bitmap size: %"PRIu64
392 " (expected at least %u)",
393 le64_to_cpu(bitmap->size),
394 DIV_ROUND_UP(ef->cmap.size, 8));
397 /* FIXME bitmap can be rather big, up to 512 MB */
398 ef->cmap.chunk_size = ef->cmap.size;
399 ef->cmap.chunk = malloc(BMAP_SIZE(ef->cmap.chunk_size));
400 if (ef->cmap.chunk == NULL)
402 exfat_error("failed to allocate clusters bitmap chunk "
403 "(%"PRIu64" bytes)", le64_to_cpu(bitmap->size));
408 if (exfat_pread(ef->dev, ef->cmap.chunk,
409 BMAP_SIZE(ef->cmap.chunk_size),
410 exfat_c2o(ef, ef->cmap.start_cluster)) < 0)
412 exfat_error("failed to read clusters bitmap "
413 "(%"PRIu64" bytes starting at cluster %#x)",
414 le64_to_cpu(bitmap->size), ef->cmap.start_cluster);
419 case EXFAT_ENTRY_LABEL:
420 label = (const struct exfat_entry_label*) entry;
421 if (label->length > EXFAT_ENAME_MAX)
423 exfat_error("too long label (%hhu chars)", label->length);
426 if (utf16_to_utf8(ef->label, label->name,
427 sizeof(ef->label) - 1, EXFAT_ENAME_MAX) != 0)
432 if (entry->type & EXFAT_ENTRY_VALID)
434 exfat_error("unknown entry type 0x%hhx", entry->type);
440 if (fetch_next_entry(ef, parent, it) != 0)
443 /* we never reach here */
451 int exfat_cache_directory(struct exfat* ef, struct exfat_node* dir)
455 struct exfat_node* node;
456 struct exfat_node* current = NULL;
458 if (dir->flags & EXFAT_ATTRIB_CACHED)
459 return 0; /* already cached */
461 rc = opendir(ef, dir, &it);
464 while ((rc = readdir(ef, dir, &node, &it)) == 0)
469 current->next = node;
470 node->prev = current;
482 for (current = dir->child; current; current = node)
484 node = current->next;
491 dir->flags |= EXFAT_ATTRIB_CACHED;
495 static void tree_attach(struct exfat_node* dir, struct exfat_node* node)
500 dir->child->prev = node;
501 node->next = dir->child;
506 static void tree_detach(struct exfat_node* node)
509 node->prev->next = node->next;
510 else /* this is the first node in the list */
511 node->parent->child = node->next;
513 node->next->prev = node->prev;
519 static void reset_cache(struct exfat* ef, struct exfat_node* node)
523 struct exfat_node* p = node->child;
528 node->flags &= ~EXFAT_ATTRIB_CACHED;
529 if (node->references != 0)
531 char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
532 exfat_get_name(node, buffer, sizeof(buffer) - 1);
533 exfat_warn("non-zero reference counter (%d) for `%s'",
534 node->references, buffer);
536 while (node->references)
537 exfat_put_node(ef, node);
540 void exfat_reset_cache(struct exfat* ef)
542 reset_cache(ef, ef->root);
545 static void next_entry(struct exfat* ef, const struct exfat_node* parent,
546 cluster_t* cluster, off_t* offset)
548 *offset += sizeof(struct exfat_entry);
549 if (*offset % CLUSTER_SIZE(*ef->sb) == 0)
550 /* next cluster cannot be invalid */
551 *cluster = exfat_next_cluster(ef, parent, *cluster);
554 int exfat_flush_node(struct exfat* ef, struct exfat_node* node)
558 off_t meta1_offset, meta2_offset;
559 struct exfat_entry_meta1 meta1;
560 struct exfat_entry_meta2 meta2;
562 if (!(node->flags & EXFAT_ATTRIB_DIRTY))
563 return 0; /* no need to flush */
566 exfat_bug("unable to flush node to read-only FS");
568 if (node->parent == NULL)
569 return 0; /* do not flush unlinked node */
571 cluster = node->entry_cluster;
572 offset = node->entry_offset;
573 meta1_offset = co2o(ef, cluster, offset);
574 next_entry(ef, node->parent, &cluster, &offset);
575 meta2_offset = co2o(ef, cluster, offset);
577 if (exfat_pread(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
579 exfat_error("failed to read meta1 entry on flush");
582 if (meta1.type != EXFAT_ENTRY_FILE)
583 exfat_bug("invalid type of meta1: 0x%hhx", meta1.type);
584 meta1.attrib = cpu_to_le16(node->flags);
585 exfat_unix2exfat(node->mtime, &meta1.mdate, &meta1.mtime, &meta1.mtime_cs);
586 exfat_unix2exfat(node->atime, &meta1.adate, &meta1.atime, NULL);
588 if (exfat_pread(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
590 exfat_error("failed to read meta2 entry on flush");
593 if (meta2.type != EXFAT_ENTRY_FILE_INFO)
594 exfat_bug("invalid type of meta2: 0x%hhx", meta2.type);
595 meta2.size = meta2.real_size = cpu_to_le64(node->size);
596 meta2.start_cluster = cpu_to_le32(node->start_cluster);
597 meta2.flags = EXFAT_FLAG_ALWAYS1;
598 /* empty files must not be marked as contiguous */
599 if (node->size != 0 && IS_CONTIGUOUS(*node))
600 meta2.flags |= EXFAT_FLAG_CONTIGUOUS;
601 /* name hash remains unchanged, no need to recalculate it */
603 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);
605 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
607 exfat_error("failed to write meta1 entry on flush");
610 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
612 exfat_error("failed to write meta2 entry on flush");
616 node->flags &= ~EXFAT_ATTRIB_DIRTY;
620 static bool erase_entry(struct exfat* ef, struct exfat_node* node)
622 cluster_t cluster = node->entry_cluster;
623 off_t offset = node->entry_offset;
624 int name_entries = DIV_ROUND_UP(utf16_length(node->name), EXFAT_ENAME_MAX);
627 entry_type = EXFAT_ENTRY_FILE & ~EXFAT_ENTRY_VALID;
628 if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
630 exfat_error("failed to erase meta1 entry");
634 next_entry(ef, node->parent, &cluster, &offset);
635 entry_type = EXFAT_ENTRY_FILE_INFO & ~EXFAT_ENTRY_VALID;
636 if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
638 exfat_error("failed to erase meta2 entry");
642 while (name_entries--)
644 next_entry(ef, node->parent, &cluster, &offset);
645 entry_type = EXFAT_ENTRY_FILE_NAME & ~EXFAT_ENTRY_VALID;
646 if (exfat_pwrite(ef->dev, &entry_type, 1,
647 co2o(ef, cluster, offset)) < 0)
649 exfat_error("failed to erase name entry");
656 static int shrink_directory(struct exfat* ef, struct exfat_node* dir,
657 off_t deleted_offset)
659 const struct exfat_node* node;
660 const struct exfat_node* last_node;
661 uint64_t entries = 0;
665 if (!(dir->flags & EXFAT_ATTRIB_DIR))
666 exfat_bug("attempted to shrink a file");
667 if (!(dir->flags & EXFAT_ATTRIB_CACHED))
668 exfat_bug("attempted to shrink uncached directory");
670 for (last_node = node = dir->child; node; node = node->next)
672 if (deleted_offset < node->entry_offset)
674 /* there are other entries after the removed one, no way to shrink
678 if (last_node->entry_offset < node->entry_offset)
684 /* offset of the last entry */
685 entries += last_node->entry_offset / sizeof(struct exfat_entry);
686 /* two subentries with meta info */
688 /* subentries with file name */
689 entries += DIV_ROUND_UP(utf16_length(last_node->name),
693 new_size = DIV_ROUND_UP(entries * sizeof(struct exfat_entry),
694 CLUSTER_SIZE(*ef->sb)) * CLUSTER_SIZE(*ef->sb);
695 if (new_size == 0) /* directory always has at least 1 cluster */
696 new_size = CLUSTER_SIZE(*ef->sb);
697 if (new_size == dir->size)
699 rc = exfat_truncate(ef, dir, new_size, true);
705 static int delete(struct exfat* ef, struct exfat_node* node)
707 struct exfat_node* parent = node->parent;
708 off_t deleted_offset = node->entry_offset;
711 exfat_get_node(parent);
712 if (!erase_entry(ef, node))
714 exfat_put_node(ef, parent);
717 exfat_update_mtime(parent);
719 rc = shrink_directory(ef, parent, deleted_offset);
720 exfat_put_node(ef, parent);
721 /* file clusters will be freed when node reference counter becomes 0 */
722 node->flags |= EXFAT_ATTRIB_UNLINKED;
726 int exfat_unlink(struct exfat* ef, struct exfat_node* node)
728 if (node->flags & EXFAT_ATTRIB_DIR)
730 return delete(ef, node);
733 int exfat_rmdir(struct exfat* ef, struct exfat_node* node)
735 if (!(node->flags & EXFAT_ATTRIB_DIR))
737 /* check that directory is empty */
738 exfat_cache_directory(ef, node);
741 return delete(ef, node);
744 static int grow_directory(struct exfat* ef, struct exfat_node* dir,
745 uint64_t asize, uint32_t difference)
747 return exfat_truncate(ef, dir,
748 DIV_ROUND_UP(asize + difference, CLUSTER_SIZE(*ef->sb))
749 * CLUSTER_SIZE(*ef->sb), true);
752 static int find_slot(struct exfat* ef, struct exfat_node* dir,
753 cluster_t* cluster, off_t* offset, int subentries)
757 const struct exfat_entry* entry;
760 rc = opendir(ef, dir, &it);
767 *cluster = it.cluster;
770 entry = get_entry_ptr(ef, &it);
771 if (entry->type & EXFAT_ENTRY_VALID)
775 if (contiguous == subentries)
776 break; /* suitable slot is found */
777 if (it.offset + sizeof(struct exfat_entry) >= dir->size)
779 rc = grow_directory(ef, dir, dir->size,
780 (subentries - contiguous) * sizeof(struct exfat_entry));
787 if (fetch_next_entry(ef, dir, &it) != 0)
797 static int write_entry(struct exfat* ef, struct exfat_node* dir,
798 const le16_t* name, cluster_t cluster, off_t offset, uint16_t attrib)
800 struct exfat_node* node;
801 struct exfat_entry_meta1 meta1;
802 struct exfat_entry_meta2 meta2;
803 const size_t name_length = utf16_length(name);
804 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
807 node = allocate_node();
810 node->entry_cluster = cluster;
811 node->entry_offset = offset;
812 memcpy(node->name, name, name_length * sizeof(le16_t));
814 memset(&meta1, 0, sizeof(meta1));
815 meta1.type = EXFAT_ENTRY_FILE;
816 meta1.continuations = 1 + name_entries;
817 meta1.attrib = cpu_to_le16(attrib);
818 exfat_unix2exfat(time(NULL), &meta1.crdate, &meta1.crtime,
820 meta1.adate = meta1.mdate = meta1.crdate;
821 meta1.atime = meta1.mtime = meta1.crtime;
822 meta1.mtime_cs = meta1.crtime_cs; /* there is no atime_cs */
824 memset(&meta2, 0, sizeof(meta2));
825 meta2.type = EXFAT_ENTRY_FILE_INFO;
826 meta2.flags = EXFAT_FLAG_ALWAYS1;
827 meta2.name_length = name_length;
828 meta2.name_hash = exfat_calc_name_hash(ef, node->name);
829 meta2.start_cluster = cpu_to_le32(EXFAT_CLUSTER_FREE);
831 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);
833 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
834 co2o(ef, cluster, offset)) < 0)
836 exfat_error("failed to write meta1 entry");
839 next_entry(ef, dir, &cluster, &offset);
840 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
841 co2o(ef, cluster, offset)) < 0)
843 exfat_error("failed to write meta2 entry");
846 for (i = 0; i < name_entries; i++)
848 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
849 memcpy(name_entry.name, node->name + i * EXFAT_ENAME_MAX,
850 MIN(EXFAT_ENAME_MAX, EXFAT_NAME_MAX - i * EXFAT_ENAME_MAX) *
852 next_entry(ef, dir, &cluster, &offset);
853 if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
854 co2o(ef, cluster, offset)) < 0)
856 exfat_error("failed to write name entry");
861 init_node_meta1(node, &meta1);
862 init_node_meta2(node, &meta2);
864 tree_attach(dir, node);
865 exfat_update_mtime(dir);
869 static int create(struct exfat* ef, const char* path, uint16_t attrib)
871 struct exfat_node* dir;
872 struct exfat_node* existing;
873 cluster_t cluster = EXFAT_CLUSTER_BAD;
875 le16_t name[EXFAT_NAME_MAX + 1];
878 rc = exfat_split(ef, &dir, &existing, name, path);
881 if (existing != NULL)
883 exfat_put_node(ef, existing);
884 exfat_put_node(ef, dir);
888 rc = find_slot(ef, dir, &cluster, &offset,
889 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
892 exfat_put_node(ef, dir);
895 rc = write_entry(ef, dir, name, cluster, offset, attrib);
896 exfat_put_node(ef, dir);
900 int exfat_mknod(struct exfat* ef, const char* path)
902 return create(ef, path, EXFAT_ATTRIB_ARCH);
905 int exfat_mkdir(struct exfat* ef, const char* path)
908 struct exfat_node* node;
910 rc = create(ef, path, EXFAT_ATTRIB_ARCH | EXFAT_ATTRIB_DIR);
913 rc = exfat_lookup(ef, &node, path);
916 /* directories always have at least one cluster */
917 rc = exfat_truncate(ef, node, CLUSTER_SIZE(*ef->sb), true);
921 exfat_put_node(ef, node);
924 exfat_put_node(ef, node);
928 static int rename_entry(struct exfat* ef, struct exfat_node* dir,
929 struct exfat_node* node, const le16_t* name, cluster_t new_cluster,
932 struct exfat_entry_meta1 meta1;
933 struct exfat_entry_meta2 meta2;
934 cluster_t old_cluster = node->entry_cluster;
935 off_t old_offset = node->entry_offset;
936 const size_t name_length = utf16_length(name);
937 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
940 if (exfat_pread(ef->dev, &meta1, sizeof(meta1),
941 co2o(ef, old_cluster, old_offset)) < 0)
943 exfat_error("failed to read meta1 entry on rename");
946 next_entry(ef, node->parent, &old_cluster, &old_offset);
947 if (exfat_pread(ef->dev, &meta2, sizeof(meta2),
948 co2o(ef, old_cluster, old_offset)) < 0)
950 exfat_error("failed to read meta2 entry on rename");
953 meta1.continuations = 1 + name_entries;
954 meta2.name_hash = exfat_calc_name_hash(ef, name);
955 meta2.name_length = name_length;
956 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, name);
958 if (!erase_entry(ef, node))
961 node->entry_cluster = new_cluster;
962 node->entry_offset = new_offset;
964 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
965 co2o(ef, new_cluster, new_offset)) < 0)
967 exfat_error("failed to write meta1 entry on rename");
970 next_entry(ef, dir, &new_cluster, &new_offset);
971 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
972 co2o(ef, new_cluster, new_offset)) < 0)
974 exfat_error("failed to write meta2 entry on rename");
978 for (i = 0; i < name_entries; i++)
980 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
981 memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX,
982 EXFAT_ENAME_MAX * sizeof(le16_t));
983 next_entry(ef, dir, &new_cluster, &new_offset);
984 if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
985 co2o(ef, new_cluster, new_offset)) < 0)
987 exfat_error("failed to write name entry on rename");
992 memcpy(node->name, name, (EXFAT_NAME_MAX + 1) * sizeof(le16_t));
994 tree_attach(dir, node);
998 int exfat_rename(struct exfat* ef, const char* old_path, const char* new_path)
1000 struct exfat_node* node;
1001 struct exfat_node* existing;
1002 struct exfat_node* dir;
1003 cluster_t cluster = EXFAT_CLUSTER_BAD;
1005 le16_t name[EXFAT_NAME_MAX + 1];
1008 rc = exfat_lookup(ef, &node, old_path);
1012 rc = exfat_split(ef, &dir, &existing, name, new_path);
1015 exfat_put_node(ef, node);
1019 /* check that target is not a subdirectory of the source */
1020 if (node->flags & EXFAT_ATTRIB_DIR)
1022 struct exfat_node* p;
1024 for (p = dir; p; p = p->parent)
1027 if (existing != NULL)
1028 exfat_put_node(ef, existing);
1029 exfat_put_node(ef, dir);
1030 exfat_put_node(ef, node);
1035 if (existing != NULL)
1037 /* remove target if it's not the same node as source */
1038 if (existing != node)
1040 if (existing->flags & EXFAT_ATTRIB_DIR)
1042 if (node->flags & EXFAT_ATTRIB_DIR)
1043 rc = exfat_rmdir(ef, existing);
1049 if (!(node->flags & EXFAT_ATTRIB_DIR))
1050 rc = exfat_unlink(ef, existing);
1054 exfat_put_node(ef, existing);
1057 exfat_put_node(ef, dir);
1058 exfat_put_node(ef, node);
1063 exfat_put_node(ef, existing);
1066 rc = find_slot(ef, dir, &cluster, &offset,
1067 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
1070 exfat_put_node(ef, dir);
1071 exfat_put_node(ef, node);
1074 rc = rename_entry(ef, dir, node, name, cluster, offset);
1075 exfat_put_node(ef, dir);
1076 exfat_put_node(ef, node);
1080 void exfat_utimes(struct exfat_node* node, const struct timespec tv[2])
1082 node->atime = tv[0].tv_sec;
1083 node->mtime = tv[1].tv_sec;
1084 node->flags |= EXFAT_ATTRIB_DIRTY;
1087 void exfat_update_atime(struct exfat_node* node)
1089 node->atime = time(NULL);
1090 node->flags |= EXFAT_ATTRIB_DIRTY;
1093 void exfat_update_mtime(struct exfat_node* node)
1095 node->mtime = time(NULL);
1096 node->flags |= EXFAT_ATTRIB_DIRTY;
1099 const char* exfat_get_label(struct exfat* ef)
1104 static int find_label(struct exfat* ef, cluster_t* cluster, off_t* offset)
1109 rc = opendir(ef, ef->root, &it);
1115 if (it.offset >= ef->root->size)
1121 if (get_entry_ptr(ef, &it)->type == EXFAT_ENTRY_LABEL)
1123 *cluster = it.cluster;
1124 *offset = it.offset;
1129 if (fetch_next_entry(ef, ef->root, &it) != 0)
1137 int exfat_set_label(struct exfat* ef, const char* label)
1139 le16_t label_utf16[EXFAT_ENAME_MAX + 1];
1143 struct exfat_entry_label entry;
1145 memset(label_utf16, 0, sizeof(label_utf16));
1146 rc = utf8_to_utf16(label_utf16, label, EXFAT_ENAME_MAX, strlen(label));
1150 rc = find_label(ef, &cluster, &offset);
1152 rc = find_slot(ef, ef->root, &cluster, &offset, 1);
1156 entry.type = EXFAT_ENTRY_LABEL;
1157 entry.length = utf16_length(label_utf16);
1158 memcpy(entry.name, label_utf16, sizeof(entry.name));
1159 if (entry.length == 0)
1160 entry.type ^= EXFAT_ENTRY_VALID;
1162 if (exfat_pwrite(ef->dev, &entry, sizeof(struct exfat_entry_label),
1163 co2o(ef, cluster, offset)) < 0)
1165 exfat_error("failed to write label entry");
1168 strcpy(ef->label, label);