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 int init_exfat(struct exfat *exfat, struct pbr *bs)
211 INIT_LIST_HEAD(&exfat->dir_list);
213 exfat->clus_count = le32_to_cpu(bs->bsx.clu_count);
214 exfat->clus_size = EXFAT_CLUSTER_SIZE(bs);
215 exfat->sect_size = EXFAT_SECTOR_SIZE(bs);
217 /* TODO: bitmap could be very large. */
218 exfat->alloc_bitmap = (char *)calloc(1,
219 EXFAT_BITMAP_SIZE(exfat->clus_count));
220 if (!exfat->alloc_bitmap) {
221 exfat_err("failed to allocate bitmap\n");
225 exfat->disk_bitmap = (char *)malloc(
226 EXFAT_BITMAP_SIZE(exfat->clus_count));
227 if (!exfat->disk_bitmap) {
228 exfat_err("failed to allocate bitmap\n");
232 /* allocate cluster buffers */
233 for (i = 0; i < 2; i++) {
234 exfat->buffer_desc[i].buffer =
235 (char *)malloc(exfat->clus_size);
236 if (!exfat->buffer_desc[i].buffer)
238 exfat->buffer_desc[i].dirty =
240 (exfat->clus_size / exfat->sect_size), 1);
241 if (!exfat->buffer_desc[i].dirty)
250 static void exfat_free_dir_list(struct exfat *exfat)
252 struct exfat_inode *dir, *i;
254 list_for_each_entry_safe(dir, i, &exfat->dir_list, list) {
255 inode_free_file_children(dir);
256 list_del(&dir->list);
257 free_exfat_inode(dir);
262 * get references of ancestors that include @child until the count of
263 * ancesters is not larger than @count and the count of characters of
264 * their names is not larger than @max_char_len.
265 * return true if root is reached.
267 bool get_ancestors(struct exfat_inode *child,
268 struct exfat_inode **ancestors, int count,
272 struct exfat_inode *dir;
273 int name_len, char_len;
274 int root_depth, depth, i;
282 name_len = exfat_utf16_len(dir->name, NAME_BUFFER_SIZE);
283 if (char_len + name_len > max_char_len)
287 char_len += name_len + 1;
293 depth = MIN(root_depth, count);
295 for (dir = child, i = depth - 1; i >= 0; dir = dir->parent, i--)
298 *ancestor_count = depth;
302 static int resolve_path(struct path_resolve_ctx *ctx, struct exfat_inode *child)
307 static const __le16 utf16_slash = cpu_to_le16(0x002F);
308 static const __le16 utf16_null = cpu_to_le16(0x0000);
311 ctx->local_path[0] = '\0';
315 sizeof(ctx->ancestors) / sizeof(ctx->ancestors[0]),
319 utf16_path = ctx->utf16_path;
320 for (i = 0; i < depth; i++) {
321 name_len = exfat_utf16_len(ctx->ancestors[i]->name,
323 memcpy((char *)utf16_path, (char *)ctx->ancestors[i]->name,
325 utf16_path += name_len;
326 memcpy((char *)utf16_path, &utf16_slash, sizeof(utf16_slash));
332 memcpy((char *)utf16_path, &utf16_null, sizeof(utf16_null));
335 in_size = (utf16_path - ctx->utf16_path) * sizeof(__le16);
336 return exfat_utf16_dec(ctx->utf16_path, in_size,
337 ctx->local_path, sizeof(ctx->local_path));
340 static int resolve_path_parent(struct path_resolve_ctx *ctx,
341 struct exfat_inode *parent, struct exfat_inode *child)
344 struct exfat_inode *old;
347 child->parent = parent;
349 ret = resolve_path(ctx, child);
354 #define repair_file_ask(iter, inode, code, fmt, ...) \
356 resolve_path_parent(&path_resolve_ctx, \
357 (iter)->parent, inode); \
358 exfat_repair_ask((iter)->exfat, code, \
360 path_resolve_ctx.local_path, \
364 static inline bool heap_clus(struct exfat *exfat, clus_t clus)
366 return clus >= EXFAT_FIRST_CLUSTER &&
367 (clus - EXFAT_FIRST_CLUSTER) < exfat->clus_count;
370 int get_next_clus(struct exfat *exfat, struct exfat_inode *node,
371 clus_t clus, clus_t *next)
375 *next = EXFAT_EOF_CLUSTER;
377 if (!heap_clus(exfat, clus))
380 if (node->is_contiguous) {
385 offset = (off_t)le32_to_cpu(exfat->bs->bsx.fat_offset) <<
386 exfat->bs->bsx.sect_size_bits;
387 offset += sizeof(clus_t) * clus;
389 if (exfat_read(exfat->blk_dev->dev_fd, next, sizeof(*next), offset)
392 *next = le32_to_cpu(*next);
396 static int set_fat(struct exfat *exfat, clus_t clus, clus_t next_clus)
400 offset = le32_to_cpu(exfat->bs->bsx.fat_offset) <<
401 exfat->bs->bsx.sect_size_bits;
402 offset += sizeof(clus_t) * clus;
404 if (exfat_write(exfat->blk_dev->dev_fd, &next_clus, sizeof(next_clus),
405 offset) != sizeof(next_clus))
410 static int check_clus_chain(struct exfat *exfat, struct exfat_inode *node)
412 struct exfat_dentry *stream_de;
413 clus_t clus, prev, next;
414 uint64_t count, max_count;
416 clus = node->first_clus;
417 prev = EXFAT_EOF_CLUSTER;
419 max_count = DIV_ROUND_UP(node->size, exfat->clus_size);
421 if (node->size == 0 && node->first_clus == EXFAT_FREE_CLUSTER)
424 /* the first cluster is wrong */
425 if ((node->size == 0 && node->first_clus != EXFAT_FREE_CLUSTER) ||
426 (node->size > 0 && !heap_clus(exfat, node->first_clus))) {
427 if (repair_file_ask(&exfat->de_iter, node,
428 ER_FILE_FIRST_CLUS, "first cluster is wrong"))
434 while (clus != EXFAT_EOF_CLUSTER) {
435 if (count >= max_count) {
436 if (node->is_contiguous)
438 if (repair_file_ask(&exfat->de_iter, node,
439 ER_FILE_SMALLER_SIZE,
440 "more clusters are allocated. "
441 "truncate to %" PRIu64 " bytes",
442 count * exfat->clus_size))
449 * This cluster is already allocated. it may be shared with
450 * the other file, or there is a loop in cluster chain.
452 if (EXFAT_BITMAP_GET(exfat->alloc_bitmap,
453 clus - EXFAT_FIRST_CLUSTER)) {
454 if (repair_file_ask(&exfat->de_iter, node,
455 ER_FILE_DUPLICATED_CLUS,
456 "cluster is already allocated for "
457 "the other file. truncated to %"
459 count * exfat->clus_size))
465 if (!EXFAT_BITMAP_GET(exfat->disk_bitmap,
466 clus - EXFAT_FIRST_CLUSTER)) {
467 if (repair_file_ask(&exfat->de_iter, node,
468 ER_FILE_INVALID_CLUS,
469 "cluster is marked as free. truncate to %" PRIu64 " bytes",
470 count * exfat->clus_size))
477 /* This cluster is allocated or not */
478 if (get_next_clus(exfat, node, clus, &next))
480 if (!node->is_contiguous) {
481 if (!heap_clus(exfat, next) &&
482 next != EXFAT_EOF_CLUSTER) {
483 if (repair_file_ask(&exfat->de_iter, node,
484 ER_FILE_INVALID_CLUS,
485 "broken cluster chain. "
488 count * exfat->clus_size))
497 EXFAT_BITMAP_SET(exfat->alloc_bitmap,
498 clus - EXFAT_FIRST_CLUSTER);
503 if (count < max_count) {
504 if (repair_file_ask(&exfat->de_iter, node,
505 ER_FILE_LARGER_SIZE, "less clusters are allocated. "
506 "truncates to %" PRIu64 " bytes",
507 count * exfat->clus_size))
515 node->size = count * exfat->clus_size;
516 if (!heap_clus(exfat, prev))
517 node->first_clus = EXFAT_FREE_CLUSTER;
519 exfat_de_iter_get_dirty(&exfat->de_iter, 1, &stream_de);
520 if (count * exfat->clus_size <
521 le64_to_cpu(stream_de->stream_valid_size))
522 stream_de->stream_valid_size = cpu_to_le64(
523 count * exfat->clus_size);
524 if (!heap_clus(exfat, prev))
525 stream_de->stream_start_clu = EXFAT_FREE_CLUSTER;
526 stream_de->stream_size = cpu_to_le64(
527 count * exfat->clus_size);
529 /* remaining clusters will be freed while FAT is compared with
532 if (!node->is_contiguous && heap_clus(exfat, prev))
533 return set_fat(exfat, prev, EXFAT_EOF_CLUSTER);
537 static bool root_get_clus_count(struct exfat *exfat, struct exfat_inode *node,
542 clus = node->first_clus;
546 if (!heap_clus(exfat, clus)) {
547 exfat_err("/: bad cluster. 0x%x\n", clus);
551 if (EXFAT_BITMAP_GET(exfat->alloc_bitmap,
552 clus - EXFAT_FIRST_CLUSTER)) {
553 exfat_err("/: cluster is already allocated, or "
554 "there is a loop in cluster chain\n");
558 EXFAT_BITMAP_SET(exfat->alloc_bitmap,
559 clus - EXFAT_FIRST_CLUSTER);
561 if (get_next_clus(exfat, node, clus, &clus) != 0) {
562 exfat_err("/: broken cluster chain\n");
567 } while (clus != EXFAT_EOF_CLUSTER);
571 static off_t exfat_s2o(struct exfat *exfat, off_t sect)
573 return sect << exfat->bs->bsx.sect_size_bits;
576 off_t exfat_c2o(struct exfat *exfat, unsigned int clus)
578 if (clus < EXFAT_FIRST_CLUSTER)
581 return exfat_s2o(exfat, le32_to_cpu(exfat->bs->bsx.clu_offset) +
582 ((off_t)(clus - EXFAT_FIRST_CLUSTER) <<
583 exfat->bs->bsx.sect_per_clus_bits));
586 static int boot_region_checksum(struct exfat_blk_dev *bd, int bs_offset)
594 size = bd->sector_size;
600 for (i = 0; i < 11; i++) {
601 if (exfat_read(bd->dev_fd, sect, size,
602 bs_offset * size + i * size) !=
604 exfat_err("failed to read boot region\n");
608 boot_calc_checksum(sect, size, i == 0, &checksum);
611 if (exfat_read(bd->dev_fd, sect, size,
612 bs_offset * size + 11 * size) !=
614 exfat_err("failed to read a boot checksum sector\n");
619 for (i = 0; i < size/sizeof(checksum); i++) {
620 if (le32_to_cpu(((__le32 *)sect)[i]) != checksum) {
621 exfat_err("checksum of boot region is not correct. %#x, but expected %#x\n",
622 le32_to_cpu(((__le32 *)sect)[i]), checksum);
632 static int exfat_mark_volume_dirty(struct exfat *exfat, bool dirty)
636 if (!(exfat->options & FSCK_OPTS_REPAIR_WRITE))
639 flags = le16_to_cpu(exfat->bs->bsx.vol_flags);
645 exfat->bs->bsx.vol_flags = cpu_to_le16(flags);
646 if (exfat_write(exfat->blk_dev->dev_fd, exfat->bs,
647 sizeof(struct pbr), 0) != (ssize_t)sizeof(struct pbr)) {
648 exfat_err("failed to set VolumeDirty\n");
652 if (fsync(exfat->blk_dev->dev_fd) != 0) {
653 exfat_err("failed to set VolumeDirty\n");
659 static int read_boot_region(struct exfat_blk_dev *bd, struct pbr **pbr,
666 bs = (struct pbr *)malloc(sizeof(struct pbr));
668 exfat_err("failed to allocate memory\n");
672 if (exfat_read(bd->dev_fd, bs, sizeof(*bs),
673 bs_offset * bd->sector_size) != (ssize_t)sizeof(*bs)) {
674 exfat_err("failed to read a boot sector\n");
679 if (memcmp(bs->bpb.oem_name, "EXFAT ", 8) != 0) {
680 exfat_err("failed to find exfat file system.\n");
684 ret = boot_region_checksum(bd, bs_offset);
689 if (EXFAT_SECTOR_SIZE(bs) < 512 || EXFAT_SECTOR_SIZE(bs) > 4 * KB) {
690 exfat_err("too small or big sector size: %d\n",
691 EXFAT_SECTOR_SIZE(bs));
695 if (EXFAT_CLUSTER_SIZE(bs) > 32 * MB) {
696 exfat_err("too big cluster size: %d\n", EXFAT_CLUSTER_SIZE(bs));
700 if (bs->bsx.fs_version[1] != 1 || bs->bsx.fs_version[0] != 0) {
701 exfat_err("unsupported exfat version: %d.%d\n",
702 bs->bsx.fs_version[1], bs->bsx.fs_version[0]);
706 if (bs->bsx.num_fats != 1) {
707 exfat_err("unsupported FAT count: %d\n", bs->bsx.num_fats);
711 if (le64_to_cpu(bs->bsx.vol_length) * EXFAT_SECTOR_SIZE(bs) >
713 exfat_err("too large sector count: %" PRIu64 ", expected: %llu\n",
714 le64_to_cpu(bs->bsx.vol_length),
719 if (le32_to_cpu(bs->bsx.clu_count) * EXFAT_CLUSTER_SIZE(bs) >
721 exfat_err("too large cluster count: %u, expected: %u\n",
722 le32_to_cpu(bs->bsx.clu_count),
734 static int restore_boot_region(struct exfat_blk_dev *bd)
739 sector = malloc(bd->sector_size);
743 for (i = 0; i < 12; i++) {
744 if (exfat_read(bd->dev_fd, sector, bd->sector_size,
745 BACKUP_BOOT_SEC_IDX * bd->sector_size +
746 i * bd->sector_size) !=
747 (ssize_t)bd->sector_size)
750 ((struct pbr *)sector)->bsx.perc_in_use = 0xff;
752 if (exfat_write(bd->dev_fd, sector, bd->sector_size,
753 BOOT_SEC_IDX * bd->sector_size +
754 i * bd->sector_size) !=
755 (ssize_t)bd->sector_size)
758 if (fsync(bd->dev_fd))
764 static int exfat_boot_region_check(struct exfat *exfat, struct pbr **bs)
768 ret = read_boot_region(exfat->blk_dev, bs, BOOT_SEC_IDX);
769 if (ret == -EINVAL && exfat_repair_ask(exfat, ER_BS_BOOT_REGION,
770 "boot region is corrupted. try to restore the region from backup"
772 ret = read_boot_region(exfat->blk_dev, bs, BACKUP_BOOT_SEC_IDX);
774 exfat_err("backup boot region is also corrupted\n");
777 ret = restore_boot_region(exfat->blk_dev);
779 exfat_err("failed to restore boot region from backup\n");
788 static void dentry_calc_checksum(struct exfat_dentry *dentry,
789 __le16 *checksum, bool primary)
794 bytes = (uint8_t *)dentry;
796 *checksum = ((*checksum << 15) | (*checksum >> 1)) + bytes[0];
797 *checksum = ((*checksum << 15) | (*checksum >> 1)) + bytes[1];
800 for (; i < sizeof(*dentry); i++) {
801 *checksum = ((*checksum << 15) | (*checksum >> 1)) + bytes[i];
805 static __le16 file_calc_checksum(struct exfat_de_iter *iter)
808 struct exfat_dentry *file_de, *de;
812 exfat_de_iter_get(iter, 0, &file_de);
814 dentry_calc_checksum(file_de, &checksum, true);
815 for (i = 1; i <= file_de->file_num_ext; i++) {
816 exfat_de_iter_get(iter, i, &de);
817 dentry_calc_checksum(de, &checksum, false);
824 * return 0 if there are no errors, or 1 if errors are fixed, or
827 static int check_inode(struct exfat_de_iter *iter, struct exfat_inode *node)
829 struct exfat *exfat = iter->exfat;
830 struct exfat_dentry *dentry;
835 ret = check_clus_chain(exfat, node);
839 if (node->size > le32_to_cpu(exfat->bs->bsx.clu_count) *
840 (uint64_t)exfat->clus_size) {
841 fsck_err(iter->parent, node,
842 "size %" PRIu64 " is greater than cluster heap\n",
847 if (node->size == 0 && node->is_contiguous) {
848 if (repair_file_ask(iter, node, ER_FILE_ZERO_NOFAT,
849 "empty, but has no Fat chain\n")) {
850 exfat_de_iter_get_dirty(iter, 1, &dentry);
851 dentry->stream_flags &= ~EXFAT_SF_CONTIGUOUS;
857 if ((node->attr & ATTR_SUBDIR) &&
858 node->size % exfat->clus_size != 0) {
859 fsck_err(iter->parent, node,
860 "directory size %" PRIu64 " is not divisible by %d\n",
861 node->size, exfat->clus_size);
865 checksum = file_calc_checksum(iter);
866 exfat_de_iter_get(iter, 0, &dentry);
867 if (checksum != le16_to_cpu(dentry->file_checksum)) {
868 if (repair_file_ask(iter, node, ER_DE_CHECKSUM,
869 "the checksum of a file is wrong")) {
870 exfat_de_iter_get_dirty(iter, 0, &dentry);
871 dentry->file_checksum = cpu_to_le16(checksum);
877 return valid ? ret : -EINVAL;
880 static int read_file_dentries(struct exfat_de_iter *iter,
881 struct exfat_inode **new_node, int *skip_dentries)
883 struct exfat_dentry *file_de, *stream_de, *name_de;
884 struct exfat_inode *node;
887 /* TODO: mtime, atime, ... */
889 ret = exfat_de_iter_get(iter, 0, &file_de);
890 if (ret || file_de->type != EXFAT_FILE) {
891 exfat_err("failed to get file dentry. %d\n", ret);
894 ret = exfat_de_iter_get(iter, 1, &stream_de);
895 if (ret || stream_de->type != EXFAT_STREAM) {
896 exfat_err("failed to get stream dentry. %d\n", ret);
901 node = alloc_exfat_inode(le16_to_cpu(file_de->file_attr));
905 if (file_de->file_num_ext < 2) {
906 exfat_err("too few secondary count. %d\n",
907 file_de->file_num_ext);
908 free_exfat_inode(node);
912 for (i = 2; i <= file_de->file_num_ext; i++) {
913 ret = exfat_de_iter_get(iter, i, &name_de);
914 if (ret || name_de->type != EXFAT_NAME) {
915 exfat_err("failed to get name dentry. %d\n", ret);
921 (i-2) * ENTRY_NAME_MAX, name_de->name_unicode,
922 sizeof(name_de->name_unicode));
925 node->first_clus = le32_to_cpu(stream_de->stream_start_clu);
926 node->is_contiguous =
927 ((stream_de->stream_flags & EXFAT_SF_CONTIGUOUS) != 0);
928 node->size = le64_to_cpu(stream_de->stream_size);
930 if (node->size < le64_to_cpu(stream_de->stream_valid_size)) {
931 if (repair_file_ask(iter, node, ER_FILE_VALID_SIZE,
932 "valid size %" PRIu64 " greater than size %" PRIu64,
933 le64_to_cpu(stream_de->stream_valid_size),
935 exfat_de_iter_get_dirty(iter, 1, &stream_de);
936 stream_de->stream_valid_size =
937 stream_de->stream_size;
944 *skip_dentries = (file_de->file_num_ext + 1);
950 free_exfat_inode(node);
954 static int read_file(struct exfat_de_iter *de_iter,
955 struct exfat_inode **new_node, int *dentry_count)
957 struct exfat_inode *node;
962 ret = read_file_dentries(de_iter, &node, dentry_count);
966 ret = check_inode(de_iter, node);
968 free_exfat_inode(node);
976 static bool read_volume_label(struct exfat_de_iter *iter)
979 struct exfat_dentry *dentry;
980 __le16 disk_label[VOLUME_LABEL_MAX_LEN];
983 if (exfat_de_iter_get(iter, 0, &dentry))
986 if (dentry->vol_char_cnt == 0)
989 if (dentry->vol_char_cnt > VOLUME_LABEL_MAX_LEN) {
990 exfat_err("too long label. %d\n", dentry->vol_char_cnt);
994 memcpy(disk_label, dentry->vol_label, sizeof(disk_label));
995 if (exfat_utf16_dec(disk_label, dentry->vol_char_cnt*2,
996 exfat->volume_label, sizeof(exfat->volume_label)) < 0) {
997 exfat_err("failed to decode volume label\n");
1001 exfat_info("volume label [%s]\n", exfat->volume_label);
1005 static void exfat_bitmap_set_range(struct exfat *exfat,
1006 clus_t start_clus, clus_t count)
1010 if (!heap_clus(exfat, start_clus) ||
1011 !heap_clus(exfat, start_clus + count))
1015 while (clus < start_clus + count) {
1016 EXFAT_BITMAP_SET(exfat->alloc_bitmap,
1017 clus - EXFAT_FIRST_CLUSTER);
1022 static bool read_bitmap(struct exfat_de_iter *iter)
1024 struct exfat_dentry *dentry;
1025 struct exfat *exfat;
1027 exfat = iter->exfat;
1028 if (exfat_de_iter_get(iter, 0, &dentry))
1031 exfat_debug("start cluster %#x, size %#" PRIx64 "\n",
1032 le32_to_cpu(dentry->bitmap_start_clu),
1033 le64_to_cpu(dentry->bitmap_size));
1035 if (le64_to_cpu(dentry->bitmap_size) <
1036 DIV_ROUND_UP(exfat->clus_count, 8)) {
1037 exfat_err("invalid size of allocation bitmap. 0x%" PRIx64 "\n",
1038 le64_to_cpu(dentry->bitmap_size));
1041 if (!heap_clus(exfat, le32_to_cpu(dentry->bitmap_start_clu))) {
1042 exfat_err("invalid start cluster of allocate bitmap. 0x%x\n",
1043 le32_to_cpu(dentry->bitmap_start_clu));
1047 exfat->disk_bitmap_clus = le32_to_cpu(dentry->bitmap_start_clu);
1048 exfat->disk_bitmap_size = DIV_ROUND_UP(exfat->clus_count, 8);
1050 exfat_bitmap_set_range(exfat, le64_to_cpu(dentry->bitmap_start_clu),
1051 DIV_ROUND_UP(exfat->disk_bitmap_size,
1054 if (exfat_read(exfat->blk_dev->dev_fd, exfat->disk_bitmap,
1055 exfat->disk_bitmap_size,
1056 exfat_c2o(exfat, exfat->disk_bitmap_clus)) !=
1057 (ssize_t)exfat->disk_bitmap_size)
1063 static bool read_upcase_table(struct exfat_de_iter *iter)
1065 struct exfat_dentry *dentry;
1066 struct exfat *exfat;
1071 exfat = iter->exfat;
1073 if (exfat_de_iter_get(iter, 0, &dentry))
1076 if (!heap_clus(exfat, le32_to_cpu(dentry->upcase_start_clu))) {
1077 exfat_err("invalid start cluster of upcase table. 0x%x\n",
1078 le32_to_cpu(dentry->upcase_start_clu));
1082 size = (ssize_t)le64_to_cpu(dentry->upcase_size);
1083 if (size > (ssize_t)(EXFAT_MAX_UPCASE_CHARS * sizeof(__le16)) ||
1084 size == 0 || size % sizeof(__le16)) {
1085 exfat_err("invalid size of upcase table. 0x%" PRIx64 "\n",
1086 le64_to_cpu(dentry->upcase_size));
1090 upcase = (__le16 *)malloc(size);
1092 exfat_err("failed to allocate upcase table\n");
1096 if (exfat_read(exfat->blk_dev->dev_fd, upcase, size,
1098 le32_to_cpu(dentry->upcase_start_clu))) != size) {
1099 exfat_err("failed to read upcase table\n");
1105 boot_calc_checksum((unsigned char *)upcase, size, false, &checksum);
1106 if (le32_to_cpu(dentry->upcase_checksum) != checksum) {
1107 exfat_err("corrupted upcase table %#x (expected: %#x)\n",
1108 checksum, le32_to_cpu(dentry->upcase_checksum));
1113 exfat_bitmap_set_range(exfat, le32_to_cpu(dentry->upcase_start_clu),
1114 DIV_ROUND_UP(le64_to_cpu(dentry->upcase_size),
1121 static int read_children(struct exfat *exfat, struct exfat_inode *dir)
1124 struct exfat_inode *node = NULL;
1125 struct exfat_dentry *dentry;
1127 struct exfat_de_iter *de_iter;
1129 de_iter = &exfat->de_iter;
1130 ret = exfat_de_iter_init(de_iter, exfat, dir);
1137 ret = exfat_de_iter_get(de_iter, 0, &dentry);
1141 fsck_err(dir->parent, dir,
1142 "failed to get a dentry. %d\n", ret);
1148 switch (dentry->type) {
1150 ret = read_file(de_iter, &node, &dentry_count);
1152 exfat_stat.error_count++;
1155 exfat_stat.error_count++;
1156 exfat_stat.fixed_count++;
1159 if ((node->attr & ATTR_SUBDIR) && node->size) {
1161 list_add_tail(&node->sibling, &dir->children);
1162 list_add_tail(&node->list, &exfat->dir_list);
1164 free_exfat_inode(node);
1167 if (!read_volume_label(de_iter)) {
1168 exfat_err("failed to verify volume label\n");
1174 if (!read_bitmap(de_iter)) {
1176 "failed to verify allocation bitmap\n");
1182 if (!read_upcase_table(de_iter)) {
1184 "failed to verify upcase table\n");
1192 if (IS_EXFAT_DELETED(dentry->type))
1194 exfat_err("unknown entry type. 0x%x\n", dentry->type);
1199 exfat_de_iter_advance(de_iter, dentry_count);
1202 exfat_de_iter_flush(de_iter);
1205 inode_free_children(dir, false);
1206 INIT_LIST_HEAD(&dir->children);
1207 exfat_de_iter_flush(de_iter);
1211 static int write_dirty_fat(struct exfat *exfat)
1213 struct buffer_desc *bd;
1216 size_t read_size, write_size;
1217 clus_t clus, last_clus, clus_count, i;
1221 last_clus = le32_to_cpu(exfat->bs->bsx.clu_count) + 2;
1222 bd = exfat->buffer_desc;
1224 offset = le32_to_cpu(exfat->bs->bsx.fat_offset) *
1226 read_size = exfat->clus_size;
1227 write_size = exfat->sect_size;
1229 while (clus < last_clus) {
1230 clus_count = MIN(read_size / sizeof(clus_t), last_clus - clus);
1231 len = exfat_read(exfat->blk_dev->dev_fd, bd[idx].buffer,
1232 clus_count * sizeof(clus_t), offset);
1233 if (len != (ssize_t)(sizeof(clus_t) * clus_count)) {
1234 exfat_err("failed to read fat entries, %zd\n", len);
1238 /* TODO: read ahead */
1240 for (i = clus ? clus : EXFAT_FIRST_CLUSTER;
1241 i < clus + clus_count; i++) {
1242 if (!EXFAT_BITMAP_GET(exfat->alloc_bitmap,
1243 i - EXFAT_FIRST_CLUSTER) &&
1244 ((clus_t *)bd[idx].buffer)[i - clus] !=
1245 EXFAT_FREE_CLUSTER) {
1246 ((clus_t *)bd[idx].buffer)[i - clus] =
1248 bd[idx].dirty[(i - clus) /
1249 (write_size / sizeof(clus_t))] = true;
1253 for (i = 0; i < read_size; i += write_size) {
1254 if (bd[idx].dirty[i / write_size]) {
1255 if (exfat_write(exfat->blk_dev->dev_fd,
1256 &bd[idx].buffer[i], write_size,
1258 (ssize_t)write_size) {
1259 exfat_err("failed to write "
1264 bd[idx].dirty[i / write_size] = false;
1269 clus = clus + clus_count;
1275 static int write_dirty_bitmap(struct exfat *exfat)
1277 struct buffer_desc *bd;
1278 off_t offset, last_offset, bitmap_offset;
1280 ssize_t read_size, write_size, i, size;
1283 offset = exfat_c2o(exfat, exfat->disk_bitmap_clus);
1284 last_offset = offset + exfat->disk_bitmap_size;
1286 read_size = exfat->clus_size;
1287 write_size = exfat->sect_size;
1289 bd = exfat->buffer_desc;
1292 while (offset < last_offset) {
1293 len = MIN(read_size, last_offset - offset);
1294 if (exfat_read(exfat->blk_dev->dev_fd, bd[idx].buffer,
1295 len, offset) != (ssize_t)len)
1298 /* TODO: read-ahead */
1300 for (i = 0; i < len; i += write_size) {
1301 size = MIN(write_size, len - i);
1302 if (memcmp(&bd[idx].buffer[i],
1303 exfat->alloc_bitmap + bitmap_offset + i,
1305 if (exfat_write(exfat->blk_dev->dev_fd,
1306 exfat->alloc_bitmap + bitmap_offset + i,
1307 size, offset + i) != size)
1314 bitmap_offset += len;
1319 static int reclaim_free_clusters(struct exfat *exfat)
1321 if (write_dirty_fat(exfat)) {
1322 exfat_err("failed to write fat entries\n");
1325 if (write_dirty_bitmap(exfat)) {
1326 exfat_err("failed to write bitmap\n");
1333 * for each directory in @dir_list.
1334 * 1. read all dentries and allocate exfat_nodes for files and directories.
1335 * and append directory exfat_nodes to the head of @dir_list
1336 * 2. free all of file exfat_nodes.
1337 * 3. if the directory does not have children, free its exfat_node.
1339 static int exfat_filesystem_check(struct exfat *exfat)
1341 struct exfat_inode *dir;
1342 int ret = 0, dir_errors;
1345 exfat_err("root is NULL\n");
1349 list_add(&exfat->root->list, &exfat->dir_list);
1351 while (!list_empty(&exfat->dir_list)) {
1352 dir = list_entry(exfat->dir_list.next, struct exfat_inode, list);
1354 if (!(dir->attr & ATTR_SUBDIR)) {
1355 fsck_err(dir->parent, dir,
1356 "failed to travel directories. "
1357 "the node is not directory\n");
1362 dir_errors = read_children(exfat, dir);
1364 resolve_path(&path_resolve_ctx, dir);
1365 exfat_debug("failed to check dentries: %s\n",
1366 path_resolve_ctx.local_path);
1370 list_del(&dir->list);
1371 inode_free_file_children(dir);
1372 inode_free_ancestors(dir);
1375 exfat_free_dir_list(exfat);
1377 if (exfat->dirty_fat && reclaim_free_clusters(exfat))
1382 static int exfat_root_dir_check(struct exfat *exfat)
1384 struct exfat_inode *root;
1387 root = alloc_exfat_inode(ATTR_SUBDIR);
1389 exfat_err("failed to allocate memory\n");
1393 root->first_clus = le32_to_cpu(exfat->bs->bsx.root_cluster);
1394 if (!root_get_clus_count(exfat, root, &clus_count)) {
1395 exfat_err("failed to follow the cluster chain of root\n");
1396 free_exfat_inode(root);
1399 root->size = clus_count * exfat->clus_size;
1402 exfat_debug("root directory: start cluster[0x%x] size[0x%" PRIx64 "]\n",
1403 root->first_clus, root->size);
1407 static char *bytes_to_human_readable(size_t bytes)
1409 static const char * const units[] = {"B", "KB", "MB", "GB", "TB", "PB"};
1410 static char buf[15*4];
1411 unsigned int i, shift, quoti, remain;
1414 for (i = 0; i < sizeof(units)/sizeof(units[0]); i++) {
1415 if (bytes / (1ULL << (shift + 10)) == 0)
1420 quoti = (unsigned int)(bytes / (1ULL << shift));
1423 remain = (unsigned int)
1424 ((bytes & ((1ULL << shift) - 1)) >> (shift - 10));
1425 remain = (remain * 100) / 1024;
1428 snprintf(buf, sizeof(buf), "%u.%02u %s", quoti, remain, units[i]);
1432 static void exfat_show_info(struct exfat *exfat, const char *dev_name,
1435 exfat_info("sector size: %s\n",
1436 bytes_to_human_readable(1 << exfat->bs->bsx.sect_size_bits));
1437 exfat_info("cluster size: %s\n",
1438 bytes_to_human_readable(exfat->clus_size));
1439 exfat_info("volume size: %s\n",
1440 bytes_to_human_readable(exfat->blk_dev->size));
1442 printf("%s: %s. directories %ld, files %ld\n", dev_name,
1443 errors ? "checking stopped" : "clean",
1444 exfat_stat.dir_count, exfat_stat.file_count);
1445 if (errors || exfat->dirty)
1446 printf("%s: files corrupted %ld, files fixed %ld\n", dev_name,
1447 exfat_stat.error_count, exfat_stat.fixed_count);
1450 int main(int argc, char * const argv[])
1452 struct fsck_user_input ui;
1453 struct exfat_blk_dev bd;
1454 struct exfat *exfat = NULL;
1455 struct pbr *bs = NULL;
1456 int c, ret, exit_code;
1457 bool version_only = false;
1459 memset(&ui, 0, sizeof(ui));
1460 memset(&bd, 0, sizeof(bd));
1462 print_level = EXFAT_ERROR;
1464 if (!setlocale(LC_CTYPE, ""))
1465 exfat_err("failed to init locale/codeset\n");
1468 while ((c = getopt_long(argc, argv, "rynpVvh", opts, NULL)) != EOF) {
1471 if (ui.options & FSCK_OPTS_REPAIR_ALL)
1473 ui.options |= FSCK_OPTS_REPAIR_NO;
1476 if (ui.options & FSCK_OPTS_REPAIR_ALL)
1478 ui.options |= FSCK_OPTS_REPAIR_ASK;
1481 if (ui.options & FSCK_OPTS_REPAIR_ALL)
1483 ui.options |= FSCK_OPTS_REPAIR_YES;
1486 if (ui.options & FSCK_OPTS_REPAIR_ALL)
1488 ui.options |= FSCK_OPTS_REPAIR_AUTO;
1491 version_only = true;
1494 if (print_level < EXFAT_DEBUG)
1505 if (optind != argc - 1)
1509 exit(FSCK_EXIT_SYNTAX_ERROR);
1510 if (ui.options & FSCK_OPTS_REPAIR_WRITE)
1511 ui.ei.writeable = true;
1513 ui.options |= FSCK_OPTS_REPAIR_NO;
1514 ui.ei.writeable = false;
1517 snprintf(ui.ei.dev_name, sizeof(ui.ei.dev_name), "%s", argv[optind]);
1518 ret = exfat_get_blk_dev_info(&ui.ei, &bd);
1520 exfat_err("failed to open %s. %d\n", ui.ei.dev_name, ret);
1521 return FSCK_EXIT_OPERATION_ERROR;
1524 exfat = (struct exfat *)calloc(1, sizeof(*exfat));
1526 exfat_err("failed to allocate exfat\n");
1530 exfat->blk_dev = &bd;
1531 exfat->options = ui.options;
1533 ret = exfat_boot_region_check(exfat, &bs);
1537 ret = init_exfat(exfat, bs);
1541 if (exfat_mark_volume_dirty(exfat, true)) {
1546 exfat_debug("verifying root directory...\n");
1547 ret = exfat_root_dir_check(exfat);
1549 exfat_err("failed to verify root directory.\n");
1553 exfat_debug("verifying directory entries...\n");
1554 ret = exfat_filesystem_check(exfat);
1558 if (ui.ei.writeable && fsync(bd.dev_fd)) {
1559 exfat_err("failed to sync\n");
1563 exfat_mark_volume_dirty(exfat, false);
1566 exfat_show_info(exfat, ui.ei.dev_name, ret);
1569 exit_code = FSCK_EXIT_ERRORS_LEFT;
1571 exit_code = FSCK_EXIT_OPERATION_ERROR;
1572 else if (exfat->dirty)
1573 exit_code = FSCK_EXIT_CORRECTED;
1575 exit_code = FSCK_EXIT_NO_ERRORS;