1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2019 Namjae Jeon <linkinjeon@kernel.org>
4 * Copyright (C) 2020 Hyunchul Lee <hyc.lee@gmail.com>
16 #include "exfat_ondisk.h"
21 struct fsck_user_input {
22 struct exfat_user_input ei;
23 enum fsck_ui_options options;
26 #define EXFAT_MAX_UPCASE_CHARS 0x10000
28 #ifdef WORDS_BIGENDIAN
29 typedef __u8 bitmap_t;
31 typedef __u32 bitmap_t;
34 #define BITS_PER (sizeof(bitmap_t) * 8)
35 #define BIT_MASK(__c) (1 << ((__c) % BITS_PER))
36 #define BIT_ENTRY(__c) ((__c) / BITS_PER)
38 #define EXFAT_BITMAP_SIZE(__c_count) \
39 (DIV_ROUND_UP(__c_count, BITS_PER) * sizeof(bitmap_t))
40 #define EXFAT_BITMAP_GET(__bmap, __c) \
41 (((bitmap_t *)(__bmap))[BIT_ENTRY(__c)] & BIT_MASK(__c))
42 #define EXFAT_BITMAP_SET(__bmap, __c) \
43 (((bitmap_t *)(__bmap))[BIT_ENTRY(__c)] |= \
46 #define FSCK_EXIT_NO_ERRORS 0x00
47 #define FSCK_EXIT_CORRECTED 0x01
48 #define FSCK_EXIT_NEED_REBOOT 0x02
49 #define FSCK_EXIT_ERRORS_LEFT 0x04
50 #define FSCK_EXIT_OPERATION_ERROR 0x08
51 #define FSCK_EXIT_SYNTAX_ERROR 0x10
52 #define FSCK_EXIT_USER_CANCEL 0x20
53 #define FSCK_EXIT_LIBRARY_ERROR 0x80
62 struct path_resolve_ctx {
63 struct exfat_inode *ancestors[255];
64 __le16 utf16_path[PATH_MAX + 2];
65 char local_path[PATH_MAX * MB_LEN_MAX + 1];
68 struct exfat_stat exfat_stat;
69 struct path_resolve_ctx path_resolve_ctx;
71 static struct option opts[] = {
72 {"repair", no_argument, NULL, 'r' },
73 {"repair-yes", no_argument, NULL, 'y' },
74 {"repair-no", no_argument, NULL, 'n' },
75 {"repair-auto", no_argument, NULL, 'p' },
76 {"version", no_argument, NULL, 'V' },
77 {"verbose", no_argument, NULL, 'v' },
78 {"help", no_argument, NULL, 'h' },
79 {"?", no_argument, NULL, '?' },
83 static void usage(char *name)
85 fprintf(stderr, "Usage: %s\n", name);
86 fprintf(stderr, "\t-r | --repair Repair interactively\n");
87 fprintf(stderr, "\t-y | --repair-yes Repair without ask\n");
88 fprintf(stderr, "\t-n | --repair-no No repair\n");
89 fprintf(stderr, "\t-p | --repair-auto Repair automatically\n");
90 fprintf(stderr, "\t-V | --version Show version\n");
91 fprintf(stderr, "\t-v | --verbose Print debug\n");
92 fprintf(stderr, "\t-h | --help Show help\n");
94 exit(FSCK_EXIT_SYNTAX_ERROR);
97 #define fsck_err(parent, inode, fmt, ...) \
99 resolve_path_parent(&path_resolve_ctx, \
101 exfat_err("ERROR: %s: " fmt, \
102 path_resolve_ctx.local_path, \
106 static struct exfat_inode *alloc_exfat_inode(__u16 attr)
108 struct exfat_inode *node;
111 size = offsetof(struct exfat_inode, name) + NAME_BUFFER_SIZE;
112 node = (struct exfat_inode *)calloc(1, size);
114 exfat_err("failed to allocate exfat_node\n");
119 INIT_LIST_HEAD(&node->children);
120 INIT_LIST_HEAD(&node->sibling);
121 INIT_LIST_HEAD(&node->list);
123 node->last_pclus = EXFAT_EOF_CLUSTER;
125 if (attr & ATTR_SUBDIR)
126 exfat_stat.dir_count++;
128 exfat_stat.file_count++;
132 static void free_exfat_inode(struct exfat_inode *node)
137 static void inode_free_children(struct exfat_inode *dir, bool file_only)
139 struct exfat_inode *node, *i;
141 list_for_each_entry_safe(node, i, &dir->children, sibling) {
143 if (!(node->attr & ATTR_SUBDIR)) {
144 list_del(&node->sibling);
145 free_exfat_inode(node);
148 list_del(&node->sibling);
149 list_del(&node->list);
150 free_exfat_inode(node);
155 static void inode_free_file_children(struct exfat_inode *dir)
157 inode_free_children(dir, true);
160 /* delete @child and all ancestors that does not have
163 static void inode_free_ancestors(struct exfat_inode *child)
165 struct exfat_inode *parent;
167 if (!list_empty(&child->children))
171 if (!(child->attr & ATTR_SUBDIR)) {
172 exfat_err("not directory.\n");
176 parent = child->parent;
177 list_del(&child->sibling);
178 free_exfat_inode(child);
181 } while (child && list_empty(&child->children));
186 static void free_exfat(struct exfat *exfat)
193 if (exfat->alloc_bitmap)
194 free(exfat->alloc_bitmap);
195 if (exfat->disk_bitmap)
196 free(exfat->disk_bitmap);
197 for (i = 0; i < 2; i++) {
198 if (exfat->buffer_desc[i].buffer)
199 free(exfat->buffer_desc[i].buffer);
200 if (exfat->buffer_desc[i].dirty)
201 free(exfat->buffer_desc[i].dirty);
207 static struct exfat *alloc_exfat(struct exfat_blk_dev *bd, struct pbr *bs)
212 exfat = (struct exfat *)calloc(1, sizeof(*exfat));
215 exfat_err("failed to allocate exfat\n");
219 INIT_LIST_HEAD(&exfat->dir_list);
222 exfat->clus_count = le32_to_cpu(bs->bsx.clu_count);
223 exfat->clus_size = EXFAT_CLUSTER_SIZE(bs);
224 exfat->sect_size = EXFAT_SECTOR_SIZE(bs);
226 /* TODO: bitmap could be very large. */
227 exfat->alloc_bitmap = (char *)calloc(1,
228 EXFAT_BITMAP_SIZE(exfat->clus_count));
229 if (!exfat->alloc_bitmap) {
230 exfat_err("failed to allocate bitmap\n");
234 exfat->disk_bitmap = (char *)malloc(
235 EXFAT_BITMAP_SIZE(exfat->clus_count));
236 if (!exfat->disk_bitmap) {
237 exfat_err("failed to allocate bitmap\n");
241 /* allocate cluster buffers */
242 for (i = 0; i < 2; i++) {
243 exfat->buffer_desc[i].buffer =
244 (char *)malloc(exfat->clus_size);
245 if (!exfat->buffer_desc[i].buffer)
247 exfat->buffer_desc[i].dirty =
249 (exfat->clus_size / exfat->sect_size), 1);
250 if (!exfat->buffer_desc[i].dirty)
259 static void exfat_free_dir_list(struct exfat *exfat)
261 struct exfat_inode *dir, *i;
263 list_for_each_entry_safe(dir, i, &exfat->dir_list, list) {
264 inode_free_file_children(dir);
265 list_del(&dir->list);
266 free_exfat_inode(dir);
271 * get references of ancestors that include @child until the count of
272 * ancesters is not larger than @count and the count of characters of
273 * their names is not larger than @max_char_len.
274 * return true if root is reached.
276 bool get_ancestors(struct exfat_inode *child,
277 struct exfat_inode **ancestors, int count,
281 struct exfat_inode *dir;
282 int name_len, char_len;
283 int root_depth, depth, i;
291 name_len = exfat_utf16_len(dir->name, NAME_BUFFER_SIZE);
292 if (char_len + name_len > max_char_len)
296 char_len += name_len + 1;
302 depth = MIN(root_depth, count);
304 for (dir = child, i = depth - 1; i >= 0; dir = dir->parent, i--)
307 *ancestor_count = depth;
311 static int resolve_path(struct path_resolve_ctx *ctx, struct exfat_inode *child)
316 static const __le16 utf16_slash = cpu_to_le16(0x002F);
317 static const __le16 utf16_null = cpu_to_le16(0x0000);
320 ctx->local_path[0] = '\0';
324 sizeof(ctx->ancestors) / sizeof(ctx->ancestors[0]),
328 utf16_path = ctx->utf16_path;
329 for (i = 0; i < depth; i++) {
330 name_len = exfat_utf16_len(ctx->ancestors[i]->name,
332 memcpy((char *)utf16_path, (char *)ctx->ancestors[i]->name,
334 utf16_path += name_len;
335 memcpy((char *)utf16_path, &utf16_slash, sizeof(utf16_slash));
341 memcpy((char *)utf16_path, &utf16_null, sizeof(utf16_null));
344 in_size = (utf16_path - ctx->utf16_path) * sizeof(__le16);
345 return exfat_utf16_dec(ctx->utf16_path, in_size,
346 ctx->local_path, sizeof(ctx->local_path));
349 static int resolve_path_parent(struct path_resolve_ctx *ctx,
350 struct exfat_inode *parent, struct exfat_inode *child)
353 struct exfat_inode *old;
356 child->parent = parent;
358 ret = resolve_path(ctx, child);
363 #define repair_file_ask(iter, inode, code, fmt, ...) \
365 resolve_path_parent(&path_resolve_ctx, \
366 (iter)->parent, inode); \
367 exfat_repair_ask((iter)->exfat, code, \
369 path_resolve_ctx.local_path, \
373 static inline bool heap_clus(struct exfat *exfat, clus_t clus)
375 return clus >= EXFAT_FIRST_CLUSTER &&
376 (clus - EXFAT_FIRST_CLUSTER) < exfat->clus_count;
379 int get_next_clus(struct exfat *exfat, struct exfat_inode *node,
380 clus_t clus, clus_t *next)
384 *next = EXFAT_EOF_CLUSTER;
386 if (!heap_clus(exfat, clus))
389 if (node->is_contiguous) {
394 offset = (off_t)le32_to_cpu(exfat->bs->bsx.fat_offset) <<
395 exfat->bs->bsx.sect_size_bits;
396 offset += sizeof(clus_t) * clus;
398 if (exfat_read(exfat->blk_dev->dev_fd, next, sizeof(*next), offset)
401 *next = le32_to_cpu(*next);
405 static int set_fat(struct exfat *exfat, clus_t clus, clus_t next_clus)
409 offset = le32_to_cpu(exfat->bs->bsx.fat_offset) <<
410 exfat->bs->bsx.sect_size_bits;
411 offset += sizeof(clus_t) * clus;
413 if (exfat_write(exfat->blk_dev->dev_fd, &next_clus, sizeof(next_clus),
414 offset) != sizeof(next_clus))
419 static int check_clus_chain(struct exfat *exfat, struct exfat_inode *node)
421 struct exfat_dentry *stream_de;
422 clus_t clus, prev, next;
423 uint64_t count, max_count;
425 clus = node->first_clus;
426 prev = EXFAT_EOF_CLUSTER;
428 max_count = DIV_ROUND_UP(node->size, exfat->clus_size);
430 if (node->size == 0 && node->first_clus == EXFAT_FREE_CLUSTER)
433 /* the first cluster is wrong */
434 if ((node->size == 0 && node->first_clus != EXFAT_FREE_CLUSTER) ||
435 (node->size > 0 && !heap_clus(exfat, node->first_clus))) {
436 if (repair_file_ask(&exfat->de_iter, node,
437 ER_FILE_FIRST_CLUS, "first cluster is wrong"))
443 while (clus != EXFAT_EOF_CLUSTER) {
444 if (count >= max_count) {
445 if (node->is_contiguous)
447 if (repair_file_ask(&exfat->de_iter, node,
448 ER_FILE_SMALLER_SIZE,
449 "more clusters are allocated. "
450 "truncate to %" PRIu64 " bytes",
451 count * exfat->clus_size))
458 * This cluster is already allocated. it may be shared with
459 * the other file, or there is a loop in cluster chain.
461 if (EXFAT_BITMAP_GET(exfat->alloc_bitmap,
462 clus - EXFAT_FIRST_CLUSTER)) {
463 if (repair_file_ask(&exfat->de_iter, node,
464 ER_FILE_DUPLICATED_CLUS,
465 "cluster is already allocated for "
466 "the other file. truncated to %"
468 count * exfat->clus_size))
474 if (!EXFAT_BITMAP_GET(exfat->disk_bitmap,
475 clus - EXFAT_FIRST_CLUSTER)) {
476 if (repair_file_ask(&exfat->de_iter, node,
477 ER_FILE_INVALID_CLUS,
478 "cluster is marked as free. truncate to %" PRIu64 " bytes",
479 count * exfat->clus_size))
486 /* This cluster is allocated or not */
487 if (get_next_clus(exfat, node, clus, &next))
489 if (!node->is_contiguous) {
490 if (!heap_clus(exfat, next) &&
491 next != EXFAT_EOF_CLUSTER) {
492 if (repair_file_ask(&exfat->de_iter, node,
493 ER_FILE_INVALID_CLUS,
494 "broken cluster chain. "
497 count * exfat->clus_size))
506 EXFAT_BITMAP_SET(exfat->alloc_bitmap,
507 clus - EXFAT_FIRST_CLUSTER);
512 if (count < max_count) {
513 if (repair_file_ask(&exfat->de_iter, node,
514 ER_FILE_LARGER_SIZE, "less clusters are allocated. "
515 "truncates to %" PRIu64 " bytes",
516 count * exfat->clus_size))
524 node->size = count * exfat->clus_size;
525 if (!heap_clus(exfat, prev))
526 node->first_clus = EXFAT_FREE_CLUSTER;
528 exfat_de_iter_get_dirty(&exfat->de_iter, 1, &stream_de);
529 if (count * exfat->clus_size <
530 le64_to_cpu(stream_de->stream_valid_size))
531 stream_de->stream_valid_size = cpu_to_le64(
532 count * exfat->clus_size);
533 if (!heap_clus(exfat, prev))
534 stream_de->stream_start_clu = EXFAT_FREE_CLUSTER;
535 stream_de->stream_size = cpu_to_le64(
536 count * exfat->clus_size);
538 /* remaining clusters will be freed while FAT is compared with
541 if (!node->is_contiguous && heap_clus(exfat, prev))
542 return set_fat(exfat, prev, EXFAT_EOF_CLUSTER);
546 static bool root_get_clus_count(struct exfat *exfat, struct exfat_inode *node,
551 clus = node->first_clus;
555 if (!heap_clus(exfat, clus)) {
556 exfat_err("/: bad cluster. 0x%x\n", clus);
560 if (EXFAT_BITMAP_GET(exfat->alloc_bitmap,
561 clus - EXFAT_FIRST_CLUSTER)) {
562 exfat_err("/: cluster is already allocated, or "
563 "there is a loop in cluster chain\n");
567 EXFAT_BITMAP_SET(exfat->alloc_bitmap,
568 clus - EXFAT_FIRST_CLUSTER);
570 if (get_next_clus(exfat, node, clus, &clus) != 0) {
571 exfat_err("/: broken cluster chain\n");
576 } while (clus != EXFAT_EOF_CLUSTER);
580 static off_t exfat_s2o(struct exfat *exfat, off_t sect)
582 return sect << exfat->bs->bsx.sect_size_bits;
585 off_t exfat_c2o(struct exfat *exfat, unsigned int clus)
587 if (clus < EXFAT_FIRST_CLUSTER)
590 return exfat_s2o(exfat, le32_to_cpu(exfat->bs->bsx.clu_offset) +
591 ((off_t)(clus - EXFAT_FIRST_CLUSTER) <<
592 exfat->bs->bsx.sect_per_clus_bits));
595 static int boot_region_checksum(struct exfat *exfat)
603 size = EXFAT_SECTOR_SIZE(exfat->bs);
610 boot_calc_checksum((unsigned char *)exfat->bs, size, true, &checksum);
611 for (i = 1; i < 11; i++) {
612 if (exfat_read(exfat->blk_dev->dev_fd, sect, size, i * size) !=
614 exfat_err("failed to read boot regions\n");
618 boot_calc_checksum(sect, size, false, &checksum);
621 if (exfat_read(exfat->blk_dev->dev_fd, sect, size, 11 * size) !=
623 exfat_err("failed to read a boot checksum sector\n");
628 for (i = 0; i < size/sizeof(checksum); i++) {
629 if (le32_to_cpu(((__le32 *)sect)[i]) != checksum) {
630 if (exfat_repair_ask(exfat, ER_BS_CHECKSUM,
631 "checksums of boot sector are not correct. "
632 "%#x, but expected %#x",
633 le32_to_cpu(((__le32 *)sect)[i]), checksum)) {
646 for (i = 0; i < size/sizeof(checksum); i++)
647 ((__le32 *)sect)[i] = cpu_to_le32(checksum);
649 if (exfat_write(exfat->blk_dev->dev_fd,
650 sect, size, size * 11) != size) {
651 exfat_err("failed to write checksum sector\n");
659 static int exfat_mark_volume_dirty(struct exfat *exfat, bool dirty)
663 if (!(exfat->options & FSCK_OPTS_REPAIR_WRITE))
666 flags = le16_to_cpu(exfat->bs->bsx.vol_flags);
672 exfat->bs->bsx.vol_flags = cpu_to_le16(flags);
673 if (exfat_write(exfat->blk_dev->dev_fd, exfat->bs,
674 sizeof(struct pbr), 0) != (ssize_t)sizeof(struct pbr)) {
675 exfat_err("failed to set VolumeDirty\n");
679 if (fsync(exfat->blk_dev->dev_fd) != 0) {
680 exfat_err("failed to set VolumeDirty\n");
686 static int read_boot_region(struct exfat_blk_dev *bd, struct pbr **pbr)
690 bs = (struct pbr *)malloc(sizeof(struct pbr));
692 exfat_err("failed to allocate memory\n");
696 if (exfat_read(bd->dev_fd, bs, sizeof(*bs), 0) !=
697 (ssize_t)sizeof(*bs)) {
698 exfat_err("failed to read a boot sector\n");
703 if (memcmp(bs->bpb.oem_name, "EXFAT ", 8) != 0) {
704 exfat_err("failed to find exfat file system.\n");
708 if (EXFAT_SECTOR_SIZE(bs) < 512 || EXFAT_SECTOR_SIZE(bs) > 4 * KB) {
709 exfat_err("too small or big sector size: %d\n",
710 EXFAT_SECTOR_SIZE(bs));
714 if (EXFAT_CLUSTER_SIZE(bs) > 32 * MB) {
715 exfat_err("too big cluster size: %d\n", EXFAT_CLUSTER_SIZE(bs));
719 if (bs->bsx.fs_version[1] != 1 || bs->bsx.fs_version[0] != 0) {
720 exfat_err("unsupported exfat version: %d.%d\n",
721 bs->bsx.fs_version[1], bs->bsx.fs_version[0]);
725 if (bs->bsx.num_fats != 1) {
726 exfat_err("unsupported FAT count: %d\n", bs->bsx.num_fats);
730 if (le64_to_cpu(bs->bsx.vol_length) * EXFAT_SECTOR_SIZE(bs) >
732 exfat_err("too large sector count: %" PRIu64 "\n, expected: %llu\n",
733 le64_to_cpu(bs->bsx.vol_length),
738 if (le32_to_cpu(bs->bsx.clu_count) * EXFAT_CLUSTER_SIZE(bs) >
740 exfat_err("too large cluster count: %u, expected: %u\n",
741 le32_to_cpu(bs->bsx.clu_count),
753 static void dentry_calc_checksum(struct exfat_dentry *dentry,
754 __le16 *checksum, bool primary)
759 bytes = (uint8_t *)dentry;
761 *checksum = ((*checksum << 15) | (*checksum >> 1)) + bytes[0];
762 *checksum = ((*checksum << 15) | (*checksum >> 1)) + bytes[1];
765 for (; i < sizeof(*dentry); i++) {
766 *checksum = ((*checksum << 15) | (*checksum >> 1)) + bytes[i];
770 static __le16 file_calc_checksum(struct exfat_de_iter *iter)
773 struct exfat_dentry *file_de, *de;
777 exfat_de_iter_get(iter, 0, &file_de);
779 dentry_calc_checksum(file_de, &checksum, true);
780 for (i = 1; i <= file_de->file_num_ext; i++) {
781 exfat_de_iter_get(iter, i, &de);
782 dentry_calc_checksum(de, &checksum, false);
789 * return 0 if there are no errors, or 1 if errors are fixed, or
792 static int check_inode(struct exfat_de_iter *iter, struct exfat_inode *node)
794 struct exfat *exfat = iter->exfat;
795 struct exfat_dentry *dentry;
799 ret = check_clus_chain(exfat, node);
803 if (node->size > le32_to_cpu(exfat->bs->bsx.clu_count) *
804 (uint64_t)exfat->clus_size) {
805 fsck_err(iter->parent, node,
806 "size %" PRIu64 " is greater than cluster heap\n",
811 if (node->size == 0 && node->is_contiguous) {
812 if (repair_file_ask(iter, node, ER_FILE_ZERO_NOFAT,
813 "empty, but has no Fat chain\n")) {
814 exfat_de_iter_get_dirty(iter, 1, &dentry);
815 dentry->stream_flags &= ~EXFAT_SF_CONTIGUOUS;
821 if ((node->attr & ATTR_SUBDIR) &&
822 node->size % exfat->clus_size != 0) {
823 fsck_err(iter->parent, node,
824 "directory size %" PRIu64 " is not divisible by %d\n",
825 node->size, exfat->clus_size);
829 checksum = file_calc_checksum(iter);
830 exfat_de_iter_get(iter, 0, &dentry);
831 if (checksum != le16_to_cpu(dentry->file_checksum)) {
832 if (repair_file_ask(iter, node, ER_DE_CHECKSUM,
833 "the checksum of a file is wrong")) {
834 exfat_de_iter_get_dirty(iter, 0, &dentry);
835 dentry->file_checksum = cpu_to_le16(checksum);
844 static int read_file_dentries(struct exfat_de_iter *iter,
845 struct exfat_inode **new_node, int *skip_dentries)
847 struct exfat_dentry *file_de, *stream_de, *name_de;
848 struct exfat_inode *node;
851 /* TODO: mtime, atime, ... */
853 ret = exfat_de_iter_get(iter, 0, &file_de);
854 if (ret || file_de->type != EXFAT_FILE) {
855 exfat_err("failed to get file dentry. %d\n", ret);
858 ret = exfat_de_iter_get(iter, 1, &stream_de);
859 if (ret || stream_de->type != EXFAT_STREAM) {
860 exfat_err("failed to get stream dentry. %d\n", ret);
865 node = alloc_exfat_inode(le16_to_cpu(file_de->file_attr));
869 if (file_de->file_num_ext < 2) {
870 exfat_err("too few secondary count. %d\n",
871 file_de->file_num_ext);
872 free_exfat_inode(node);
876 for (i = 2; i <= file_de->file_num_ext; i++) {
877 ret = exfat_de_iter_get(iter, i, &name_de);
878 if (ret || name_de->type != EXFAT_NAME) {
879 exfat_err("failed to get name dentry. %d\n", ret);
885 (i-2) * ENTRY_NAME_MAX, name_de->name_unicode,
886 sizeof(name_de->name_unicode));
889 node->first_clus = le32_to_cpu(stream_de->stream_start_clu);
890 node->is_contiguous =
891 ((stream_de->stream_flags & EXFAT_SF_CONTIGUOUS) != 0);
892 node->size = le64_to_cpu(stream_de->stream_size);
894 if (node->size < le64_to_cpu(stream_de->stream_valid_size)) {
895 if (repair_file_ask(iter, node, ER_FILE_VALID_SIZE,
896 "valid size %" PRIu64 " greater than size %" PRIu64,
897 le64_to_cpu(stream_de->stream_valid_size),
899 exfat_de_iter_get_dirty(iter, 1, &stream_de);
900 stream_de->stream_valid_size =
901 stream_de->stream_size;
908 *skip_dentries = (file_de->file_num_ext + 1);
914 free_exfat_inode(node);
918 static int read_file(struct exfat_de_iter *de_iter,
919 struct exfat_inode **new_node, int *dentry_count)
921 struct exfat_inode *node;
926 ret = read_file_dentries(de_iter, &node, dentry_count);
930 ret = check_inode(de_iter, node);
932 free_exfat_inode(node);
940 static bool read_volume_label(struct exfat_de_iter *iter)
943 struct exfat_dentry *dentry;
944 __le16 disk_label[VOLUME_LABEL_MAX_LEN];
947 if (exfat_de_iter_get(iter, 0, &dentry))
950 if (dentry->vol_char_cnt == 0)
953 if (dentry->vol_char_cnt > VOLUME_LABEL_MAX_LEN) {
954 exfat_err("too long label. %d\n", dentry->vol_char_cnt);
958 memcpy(disk_label, dentry->vol_label, sizeof(disk_label));
959 if (exfat_utf16_dec(disk_label, dentry->vol_char_cnt*2,
960 exfat->volume_label, sizeof(exfat->volume_label)) < 0) {
961 exfat_err("failed to decode volume label\n");
965 exfat_info("volume label [%s]\n", exfat->volume_label);
969 static void exfat_bitmap_set_range(struct exfat *exfat,
970 clus_t start_clus, clus_t count)
974 if (!heap_clus(exfat, start_clus) ||
975 !heap_clus(exfat, start_clus + count))
979 while (clus < start_clus + count) {
980 EXFAT_BITMAP_SET(exfat->alloc_bitmap,
981 clus - EXFAT_FIRST_CLUSTER);
986 static bool read_bitmap(struct exfat_de_iter *iter)
988 struct exfat_dentry *dentry;
992 if (exfat_de_iter_get(iter, 0, &dentry))
995 exfat_debug("start cluster %#x, size %#" PRIx64 "\n",
996 le32_to_cpu(dentry->bitmap_start_clu),
997 le64_to_cpu(dentry->bitmap_size));
999 if (le64_to_cpu(dentry->bitmap_size) <
1000 DIV_ROUND_UP(exfat->clus_count, 8)) {
1001 exfat_err("invalid size of allocation bitmap. 0x%" PRIx64 "\n",
1002 le64_to_cpu(dentry->bitmap_size));
1005 if (!heap_clus(exfat, le32_to_cpu(dentry->bitmap_start_clu))) {
1006 exfat_err("invalid start cluster of allocate bitmap. 0x%x\n",
1007 le32_to_cpu(dentry->bitmap_start_clu));
1011 exfat->disk_bitmap_clus = le32_to_cpu(dentry->bitmap_start_clu);
1012 exfat->disk_bitmap_size = DIV_ROUND_UP(exfat->clus_count, 8);
1014 exfat_bitmap_set_range(exfat, le64_to_cpu(dentry->bitmap_start_clu),
1015 DIV_ROUND_UP(exfat->disk_bitmap_size,
1018 if (exfat_read(exfat->blk_dev->dev_fd, exfat->disk_bitmap,
1019 exfat->disk_bitmap_size,
1020 exfat_c2o(exfat, exfat->disk_bitmap_clus)) !=
1021 (ssize_t)exfat->disk_bitmap_size)
1027 static bool read_upcase_table(struct exfat_de_iter *iter)
1029 struct exfat_dentry *dentry;
1030 struct exfat *exfat;
1035 exfat = iter->exfat;
1037 if (exfat_de_iter_get(iter, 0, &dentry))
1040 if (!heap_clus(exfat, le32_to_cpu(dentry->upcase_start_clu))) {
1041 exfat_err("invalid start cluster of upcase table. 0x%x\n",
1042 le32_to_cpu(dentry->upcase_start_clu));
1046 size = (ssize_t)le64_to_cpu(dentry->upcase_size);
1047 if (size > (ssize_t)(EXFAT_MAX_UPCASE_CHARS * sizeof(__le16)) ||
1048 size == 0 || size % sizeof(__le16)) {
1049 exfat_err("invalid size of upcase table. 0x%" PRIx64 "\n",
1050 le64_to_cpu(dentry->upcase_size));
1054 upcase = (__le16 *)malloc(size);
1056 exfat_err("failed to allocate upcase table\n");
1060 if (exfat_read(exfat->blk_dev->dev_fd, upcase, size,
1062 le32_to_cpu(dentry->upcase_start_clu))) != size) {
1063 exfat_err("failed to read upcase table\n");
1069 boot_calc_checksum((unsigned char *)upcase, size, false, &checksum);
1070 if (le32_to_cpu(dentry->upcase_checksum) != checksum) {
1071 exfat_err("corrupted upcase table %#x (expected: %#x)\n",
1072 checksum, le32_to_cpu(dentry->upcase_checksum));
1077 exfat_bitmap_set_range(exfat, le32_to_cpu(dentry->upcase_start_clu),
1078 DIV_ROUND_UP(le64_to_cpu(dentry->upcase_size),
1085 static int read_children(struct exfat *exfat, struct exfat_inode *dir)
1088 struct exfat_inode *node = NULL;
1089 struct exfat_dentry *dentry;
1091 struct exfat_de_iter *de_iter;
1093 de_iter = &exfat->de_iter;
1094 ret = exfat_de_iter_init(de_iter, exfat, dir);
1101 ret = exfat_de_iter_get(de_iter, 0, &dentry);
1105 fsck_err(dir->parent, dir,
1106 "failed to get a dentry. %d\n", ret);
1112 switch (dentry->type) {
1114 ret = read_file(de_iter, &node, &dentry_count);
1116 exfat_stat.error_count++;
1119 exfat_stat.error_count++;
1120 exfat_stat.fixed_count++;
1123 if ((node->attr & ATTR_SUBDIR) && node->size) {
1125 list_add_tail(&node->sibling, &dir->children);
1126 list_add_tail(&node->list, &exfat->dir_list);
1128 free_exfat_inode(node);
1131 if (!read_volume_label(de_iter)) {
1132 exfat_err("failed to verify volume label\n");
1138 if (!read_bitmap(de_iter)) {
1140 "failed to verify allocation bitmap\n");
1146 if (!read_upcase_table(de_iter)) {
1148 "failed to verify upcase table\n");
1156 if (IS_EXFAT_DELETED(dentry->type))
1158 exfat_err("unknown entry type. 0x%x\n", dentry->type);
1163 exfat_de_iter_advance(de_iter, dentry_count);
1166 exfat_de_iter_flush(de_iter);
1169 inode_free_children(dir, false);
1170 INIT_LIST_HEAD(&dir->children);
1171 exfat_de_iter_flush(de_iter);
1175 static int write_dirty_fat(struct exfat *exfat)
1177 struct buffer_desc *bd;
1180 size_t read_size, write_size;
1181 clus_t clus, last_clus, clus_count, i;
1185 last_clus = le32_to_cpu(exfat->bs->bsx.clu_count) + 2;
1186 bd = exfat->buffer_desc;
1188 offset = le32_to_cpu(exfat->bs->bsx.fat_offset) *
1190 read_size = exfat->clus_size;
1191 write_size = exfat->sect_size;
1193 while (clus < last_clus) {
1194 clus_count = MIN(read_size / sizeof(clus_t), last_clus - clus);
1195 len = exfat_read(exfat->blk_dev->dev_fd, bd[idx].buffer,
1196 clus_count * sizeof(clus_t), offset);
1197 if (len != (ssize_t)(sizeof(clus_t) * clus_count)) {
1198 exfat_err("failed to read fat entries, %zd\n", len);
1202 /* TODO: read ahead */
1204 for (i = clus ? clus : EXFAT_FIRST_CLUSTER;
1205 i < clus + clus_count; i++) {
1206 if (!EXFAT_BITMAP_GET(exfat->alloc_bitmap,
1207 i - EXFAT_FIRST_CLUSTER) &&
1208 ((clus_t *)bd[idx].buffer)[i - clus] !=
1209 EXFAT_FREE_CLUSTER) {
1210 ((clus_t *)bd[idx].buffer)[i - clus] =
1212 bd[idx].dirty[(i - clus) /
1213 (write_size / sizeof(clus_t))] = true;
1217 for (i = 0; i < read_size; i += write_size) {
1218 if (bd[idx].dirty[i / write_size]) {
1219 if (exfat_write(exfat->blk_dev->dev_fd,
1220 &bd[idx].buffer[i], write_size,
1222 (ssize_t)write_size) {
1223 exfat_err("failed to write "
1228 bd[idx].dirty[i / write_size] = false;
1233 clus = clus + clus_count;
1239 static int write_dirty_bitmap(struct exfat *exfat)
1241 struct buffer_desc *bd;
1242 off_t offset, last_offset, bitmap_offset;
1244 ssize_t read_size, write_size, i, size;
1247 offset = exfat_c2o(exfat, exfat->disk_bitmap_clus);
1248 last_offset = offset + exfat->disk_bitmap_size;
1250 read_size = exfat->clus_size;
1251 write_size = exfat->sect_size;
1253 bd = exfat->buffer_desc;
1256 while (offset < last_offset) {
1257 len = MIN(read_size, last_offset - offset);
1258 if (exfat_read(exfat->blk_dev->dev_fd, bd[idx].buffer,
1259 len, offset) != (ssize_t)len)
1262 /* TODO: read-ahead */
1264 for (i = 0; i < len; i += write_size) {
1265 size = MIN(write_size, len - i);
1266 if (memcmp(&bd[idx].buffer[i],
1267 exfat->alloc_bitmap + bitmap_offset + i,
1269 if (exfat_write(exfat->blk_dev->dev_fd,
1270 exfat->alloc_bitmap + bitmap_offset + i,
1271 size, offset + i) != size)
1278 bitmap_offset += len;
1283 static int reclaim_free_clusters(struct exfat *exfat)
1285 if (write_dirty_fat(exfat)) {
1286 exfat_err("failed to write fat entries\n");
1289 if (write_dirty_bitmap(exfat)) {
1290 exfat_err("failed to write bitmap\n");
1297 * for each directory in @dir_list.
1298 * 1. read all dentries and allocate exfat_nodes for files and directories.
1299 * and append directory exfat_nodes to the head of @dir_list
1300 * 2. free all of file exfat_nodes.
1301 * 3. if the directory does not have children, free its exfat_node.
1303 static int exfat_filesystem_check(struct exfat *exfat)
1305 struct exfat_inode *dir;
1306 int ret = 0, dir_errors;
1309 exfat_err("root is NULL\n");
1313 list_add(&exfat->root->list, &exfat->dir_list);
1315 while (!list_empty(&exfat->dir_list)) {
1316 dir = list_entry(exfat->dir_list.next, struct exfat_inode, list);
1318 if (!(dir->attr & ATTR_SUBDIR)) {
1319 fsck_err(dir->parent, dir,
1320 "failed to travel directories. "
1321 "the node is not directory\n");
1326 dir_errors = read_children(exfat, dir);
1328 resolve_path(&path_resolve_ctx, dir);
1329 exfat_debug("failed to check dentries: %s\n",
1330 path_resolve_ctx.local_path);
1334 list_del(&dir->list);
1335 inode_free_file_children(dir);
1336 inode_free_ancestors(dir);
1339 exfat_free_dir_list(exfat);
1341 if (exfat->dirty_fat && reclaim_free_clusters(exfat))
1346 static int exfat_root_dir_check(struct exfat *exfat)
1348 struct exfat_inode *root;
1351 root = alloc_exfat_inode(ATTR_SUBDIR);
1353 exfat_err("failed to allocate memory\n");
1357 root->first_clus = le32_to_cpu(exfat->bs->bsx.root_cluster);
1358 if (!root_get_clus_count(exfat, root, &clus_count)) {
1359 exfat_err("failed to follow the cluster chain of root\n");
1360 free_exfat_inode(root);
1363 root->size = clus_count * exfat->clus_size;
1366 exfat_debug("root directory: start cluster[0x%x] size[0x%" PRIx64 "]\n",
1367 root->first_clus, root->size);
1371 static char *bytes_to_human_readable(size_t bytes)
1373 static const char * const units[] = {"B", "KB", "MB", "GB", "TB", "PB"};
1374 static char buf[15*4];
1375 unsigned int i, shift, quoti, remain;
1378 for (i = 0; i < sizeof(units)/sizeof(units[0]); i++) {
1379 if (bytes / (1ULL << (shift + 10)) == 0)
1384 quoti = (unsigned int)(bytes / (1ULL << shift));
1387 remain = (unsigned int)
1388 ((bytes & ((1ULL << shift) - 1)) >> (shift - 10));
1389 remain = (remain * 100) / 1024;
1392 snprintf(buf, sizeof(buf), "%u.%02u %s", quoti, remain, units[i]);
1396 static void exfat_show_info(struct exfat *exfat, const char *dev_name,
1399 exfat_info("sector size: %s\n",
1400 bytes_to_human_readable(1 << exfat->bs->bsx.sect_size_bits));
1401 exfat_info("cluster size: %s\n",
1402 bytes_to_human_readable(exfat->clus_size));
1403 exfat_info("volume size: %s\n",
1404 bytes_to_human_readable(exfat->blk_dev->size));
1406 printf("%s: %s. directories %ld, files %ld\n", dev_name,
1407 errors ? "checking stopped" : "clean",
1408 exfat_stat.dir_count, exfat_stat.file_count);
1409 if (errors || exfat->dirty)
1410 printf("%s: files corrupted %ld, files fixed %ld\n", dev_name,
1411 exfat_stat.error_count, exfat_stat.fixed_count);
1414 int main(int argc, char * const argv[])
1416 struct fsck_user_input ui;
1417 struct exfat_blk_dev bd;
1418 struct exfat *exfat = NULL;
1419 struct pbr *bs = NULL;
1420 int c, ret, exit_code;
1421 bool version_only = false;
1423 memset(&ui, 0, sizeof(ui));
1424 memset(&bd, 0, sizeof(bd));
1426 print_level = EXFAT_ERROR;
1428 if (!setlocale(LC_CTYPE, ""))
1429 exfat_err("failed to init locale/codeset\n");
1432 while ((c = getopt_long(argc, argv, "rynpVvh", opts, NULL)) != EOF) {
1435 if (ui.options & FSCK_OPTS_REPAIR_ALL)
1437 ui.options |= FSCK_OPTS_REPAIR_NO;
1440 if (ui.options & FSCK_OPTS_REPAIR_ALL)
1442 ui.options |= FSCK_OPTS_REPAIR_ASK;
1445 if (ui.options & FSCK_OPTS_REPAIR_ALL)
1447 ui.options |= FSCK_OPTS_REPAIR_YES;
1450 if (ui.options & FSCK_OPTS_REPAIR_ALL)
1452 ui.options |= FSCK_OPTS_REPAIR_AUTO;
1455 version_only = true;
1458 if (print_level < EXFAT_DEBUG)
1469 if (optind != argc - 1)
1473 exit(FSCK_EXIT_SYNTAX_ERROR);
1474 if (ui.options & FSCK_OPTS_REPAIR_WRITE)
1475 ui.ei.writeable = true;
1477 ui.options |= FSCK_OPTS_REPAIR_NO;
1478 ui.ei.writeable = false;
1481 snprintf(ui.ei.dev_name, sizeof(ui.ei.dev_name), "%s", argv[optind]);
1482 ret = exfat_get_blk_dev_info(&ui.ei, &bd);
1484 exfat_err("failed to open %s. %d\n", ui.ei.dev_name, ret);
1485 return FSCK_EXIT_OPERATION_ERROR;
1488 ret = read_boot_region(&bd, &bs);
1492 exfat = alloc_exfat(&bd, bs);
1497 exfat->options = ui.options;
1499 if (exfat_mark_volume_dirty(exfat, true)) {
1504 ret = boot_region_checksum(exfat);
1508 exfat_debug("verifying root directory...\n");
1509 ret = exfat_root_dir_check(exfat);
1511 exfat_err("failed to verify root directory.\n");
1515 exfat_debug("verifying directory entries...\n");
1516 ret = exfat_filesystem_check(exfat);
1520 if (ui.ei.writeable && fsync(bd.dev_fd)) {
1521 exfat_err("failed to sync\n");
1525 exfat_mark_volume_dirty(exfat, false);
1528 exfat_show_info(exfat, ui.ei.dev_name, ret);
1531 exit_code = FSCK_EXIT_ERRORS_LEFT;
1533 exit_code = FSCK_EXIT_OPERATION_ERROR;
1534 else if (exfat->dirty)
1535 exit_code = FSCK_EXIT_CORRECTED;
1537 exit_code = FSCK_EXIT_NO_ERRORS;