2 * Copyright (c) 2019 Tomohiro Kusumi <tkusumi@netbsd.org>
3 * Copyright (c) 2019 The DragonFly Project
6 * This code is derived from software contributed to The DragonFly Project
7 * by Matthew Dillon <dillon@dragonflybsd.org>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
19 * 3. Neither the name of The DragonFly Project nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific, prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/types.h>
40 #include <sys/queue.h>
41 #include <sys/ttycom.h>
51 #include <openssl/sha.h>
53 #include <vfs/hammer2/hammer2_disk.h>
54 #include <vfs/hammer2/hammer2_xxhash.h>
56 #include "hammer2_subs.h"
57 #include "fsck_hammer2.h"
60 TAILQ_ENTRY(blockref_msg) entry;
61 hammer2_blockref_t bref;
65 TAILQ_HEAD(blockref_list, blockref_msg);
67 struct blockref_entry {
68 RB_ENTRY(blockref_entry) entry;
69 hammer2_off_t data_off;
70 struct blockref_list head;
74 blockref_cmp(struct blockref_entry *b1, struct blockref_entry *b2)
76 if (b1->data_off < b2->data_off)
78 if (b1->data_off > b2->data_off)
83 RB_HEAD(blockref_tree, blockref_entry);
84 RB_PROTOTYPE2(blockref_tree, blockref_entry, entry, blockref_cmp,
86 RB_GENERATE2(blockref_tree, blockref_entry, entry, blockref_cmp, hammer2_off_t,
90 struct blockref_tree root;
91 uint8_t type; /* HAMMER2_BREF_TYPE_VOLUME or FREEMAP */
92 uint64_t total_blockref;
94 uint64_t total_invalid;
97 /* use volume or freemap depending on type value */
100 uint64_t total_indirect;
102 uint64_t total_dirent;
105 uint64_t total_freemap_node;
106 uint64_t total_freemap_leaf;
111 static void print_blockref_entry(int, struct blockref_tree *);
112 static void init_blockref_stats(blockref_stats_t *, uint8_t);
113 static void cleanup_blockref_stats(blockref_stats_t *);
114 static void print_blockref_stats(const blockref_stats_t *, bool);
115 static int verify_volume_header(const hammer2_volume_data_t *);
116 static int read_media(int, const hammer2_blockref_t *, hammer2_media_data_t *,
118 static int verify_blockref(int, const hammer2_volume_data_t *,
119 const hammer2_blockref_t *, bool, blockref_stats_t *);
120 static int init_pfs_blockref(int, const hammer2_volume_data_t *,
121 const hammer2_blockref_t *, struct blockref_list *);
122 static void cleanup_pfs_blockref(struct blockref_list *);
123 static void print_media(FILE *, int, const hammer2_blockref_t *,
124 hammer2_media_data_t *, size_t);
126 static int best_zone = -1;
131 tfprintf(FILE *fp, int tab, const char *ctl, ...)
136 ret = fprintf(fp, "%*s", tab * TAB, "");
141 vfprintf(fp, ctl, va);
146 tsnprintf(char *str, size_t siz, int tab, const char *ctl, ...)
151 ret = snprintf(str, siz, "%*s", tab * TAB, "");
152 if (ret < 0 || ret >= (int)siz)
156 vsnprintf(str + ret, siz, ctl, va);
161 tprintf_zone(int tab, int i, const hammer2_blockref_t *bref)
163 tfprintf(stdout, tab, "zone.%d %016jx%s\n",
164 i, (uintmax_t)bref->data_off,
165 (!ScanBest && i == best_zone) ? " (best)" : "");
169 init_root_blockref(int fd, int i, uint8_t type, hammer2_blockref_t *bref)
171 assert(type == HAMMER2_BREF_TYPE_EMPTY ||
172 type == HAMMER2_BREF_TYPE_VOLUME ||
173 type == HAMMER2_BREF_TYPE_FREEMAP);
174 memset(bref, 0, sizeof(*bref));
176 bref->data_off = (i * HAMMER2_ZONE_BYTES64) | HAMMER2_PBUFRADIX;
178 return lseek(fd, bref->data_off & ~HAMMER2_OFF_MASK_RADIX, SEEK_SET);
182 find_best_zone(int fd)
184 hammer2_blockref_t best;
187 memset(&best, 0, sizeof(best));
189 for (i = 0; i < HAMMER2_NUM_VOLHDRS; ++i) {
190 hammer2_volume_data_t voldata;
191 hammer2_blockref_t broot;
194 init_root_blockref(fd, i, HAMMER2_BREF_TYPE_EMPTY, &broot);
195 ret = read(fd, &voldata, HAMMER2_PBUFSIZE);
196 if (ret == HAMMER2_PBUFSIZE) {
197 if ((voldata.magic != HAMMER2_VOLUME_ID_HBO) &&
198 (voldata.magic != HAMMER2_VOLUME_ID_ABO))
200 broot.mirror_tid = voldata.mirror_tid;
201 if (best_i < 0 || best.mirror_tid < broot.mirror_tid) {
205 } else if (ret == -1) {
209 tfprintf(stderr, 1, "Failed to read volume header\n");
218 test_volume_header(int fd)
223 for (i = 0; i < HAMMER2_NUM_VOLHDRS; ++i) {
224 hammer2_volume_data_t voldata;
225 hammer2_blockref_t broot;
228 if (ScanBest && i != best_zone)
230 init_root_blockref(fd, i, HAMMER2_BREF_TYPE_EMPTY, &broot);
231 ret = read(fd, &voldata, HAMMER2_PBUFSIZE);
232 if (ret == HAMMER2_PBUFSIZE) {
233 tprintf_zone(0, i, &broot);
234 if (verify_volume_header(&voldata) == -1)
236 } else if (ret == -1) {
240 tfprintf(stderr, 1, "Failed to read volume header\n");
245 return failed ? -1 : 0;
249 test_blockref(int fd, uint8_t type)
254 for (i = 0; i < HAMMER2_NUM_VOLHDRS; ++i) {
255 hammer2_volume_data_t voldata;
256 hammer2_blockref_t broot;
259 if (ScanBest && i != best_zone)
261 init_root_blockref(fd, i, type, &broot);
262 ret = read(fd, &voldata, HAMMER2_PBUFSIZE);
263 if (ret == HAMMER2_PBUFSIZE) {
264 blockref_stats_t bstats;
265 init_blockref_stats(&bstats, type);
266 tprintf_zone(0, i, &broot);
267 if (verify_blockref(fd, &voldata, &broot, false,
270 print_blockref_stats(&bstats, true);
271 print_blockref_entry(fd, &bstats.root);
272 cleanup_blockref_stats(&bstats);
273 } else if (ret == -1) {
277 tfprintf(stderr, 1, "Failed to read volume header\n");
282 return failed ? -1 : 0;
286 test_pfs_blockref(int fd)
288 uint8_t type = HAMMER2_BREF_TYPE_VOLUME;
292 for (i = 0; i < HAMMER2_NUM_VOLHDRS; ++i) {
293 hammer2_volume_data_t voldata;
294 hammer2_blockref_t broot;
297 if (ScanBest && i != best_zone)
299 init_root_blockref(fd, i, type, &broot);
300 ret = read(fd, &voldata, HAMMER2_PBUFSIZE);
301 if (ret == HAMMER2_PBUFSIZE) {
302 struct blockref_list blist;
303 struct blockref_msg *p;
306 tprintf_zone(0, i, &broot);
308 if (init_pfs_blockref(fd, &voldata, &broot, &blist) ==
310 tfprintf(stderr, 1, "Failed to read PFS "
315 if (TAILQ_EMPTY(&blist)) {
316 tfprintf(stderr, 1, "Failed to find PFS "
321 TAILQ_FOREACH(p, &blist, entry) {
322 blockref_stats_t bstats;
326 for (j = 0; j < NumPFSNames; j++)
327 if (!strcmp(PFSNames[j],
335 tfprintf(stdout, 1, "%s\n", p->msg);
336 init_blockref_stats(&bstats, type);
337 if (verify_blockref(fd, &voldata, &p->bref,
338 false, &bstats) == -1)
340 print_blockref_stats(&bstats, true);
341 print_blockref_entry(fd, &bstats.root);
342 cleanup_blockref_stats(&bstats);
344 cleanup_pfs_blockref(&blist);
345 if (NumPFSNames && !count) {
346 tfprintf(stderr, 1, "PFS not found\n");
349 } else if (ret == -1) {
353 tfprintf(stderr, 1, "Failed to read volume header\n");
358 return failed ? -1 : 0;
369 if (ioctl(0, TIOCGWINSZ, &ws) != -1)
371 if (columns == 0 && (cp = getenv("COLUMNS")))
374 columns = 80; /* last resort */
380 cleanup_blockref_msg(struct blockref_list *head)
382 struct blockref_msg *p;
384 while ((p = TAILQ_FIRST(head)) != NULL) {
385 TAILQ_REMOVE(head, p, entry);
389 assert(TAILQ_EMPTY(head));
393 cleanup_blockref_entry(struct blockref_tree *root)
395 struct blockref_entry *e;
397 while ((e = RB_ROOT(root)) != NULL) {
398 RB_REMOVE(blockref_tree, root, e);
399 cleanup_blockref_msg(&e->head);
402 assert(RB_EMPTY(root));
406 add_blockref_msg(struct blockref_list *head, const hammer2_blockref_t *bref,
409 struct blockref_msg *m;
411 m = calloc(1, sizeof(*m));
414 m->msg = strdup(msg);
416 TAILQ_INSERT_TAIL(head, m, entry);
420 add_blockref_entry(struct blockref_tree *root, const hammer2_blockref_t *bref,
423 struct blockref_entry *e;
425 e = RB_LOOKUP(blockref_tree, root, bref->data_off);
427 e = calloc(1, sizeof(*e));
429 TAILQ_INIT(&e->head);
430 e->data_off = bref->data_off;
433 add_blockref_msg(&e->head, bref, msg);
435 RB_INSERT(blockref_tree, root, e);
439 print_blockref_msg(int fd, struct blockref_list *head)
441 struct blockref_msg *m;
443 TAILQ_FOREACH(m, head, entry) {
444 hammer2_blockref_t *bref = &m->bref;
445 hammer2_media_data_t media;
447 tfprintf(stderr, 1, "%016jx %-12s %016jx/%-2d \"%s\"\n",
448 (uintmax_t)bref->data_off,
449 hammer2_breftype_to_str(bref->type),
450 (uintmax_t)bref->key,
454 if (fd != -1 && VerboseOpt > 0) {
456 if (!read_media(fd, bref, &media, &bytes))
457 print_media(stderr, 2, bref, &media, bytes);
459 tfprintf(stderr, 2, "Failed to read media\n");
465 print_blockref_entry(int fd, struct blockref_tree *root)
467 struct blockref_entry *e;
469 RB_FOREACH(e, blockref_tree, root)
470 print_blockref_msg(fd, &e->head);
474 init_blockref_stats(blockref_stats_t *bstats, uint8_t type)
476 memset(bstats, 0, sizeof(*bstats));
477 RB_INIT(&bstats->root);
482 cleanup_blockref_stats(blockref_stats_t *bstats)
484 cleanup_blockref_entry(&bstats->root);
488 print_blockref_stats(const blockref_stats_t *bstats, bool newline)
490 size_t siz = charsperline();
491 char *buf = calloc(1, siz);
497 snprintf(emptybuf, sizeof(emptybuf), ", %ju empty",
498 (uintmax_t)bstats->total_empty);
500 strlcpy(emptybuf, "", sizeof(emptybuf));
502 switch (bstats->type) {
503 case HAMMER2_BREF_TYPE_VOLUME:
504 tsnprintf(buf, siz, 1, "%ju blockref (%ju inode, %ju indirect, "
505 "%ju data, %ju dirent%s), %s",
506 (uintmax_t)bstats->total_blockref,
507 (uintmax_t)bstats->volume.total_inode,
508 (uintmax_t)bstats->volume.total_indirect,
509 (uintmax_t)bstats->volume.total_data,
510 (uintmax_t)bstats->volume.total_dirent,
512 sizetostr(bstats->total_bytes));
514 case HAMMER2_BREF_TYPE_FREEMAP:
515 tsnprintf(buf, siz, 1, "%ju blockref (%ju node, %ju leaf%s), "
517 (uintmax_t)bstats->total_blockref,
518 (uintmax_t)bstats->freemap.total_freemap_node,
519 (uintmax_t)bstats->freemap.total_freemap_leaf,
521 sizetostr(bstats->total_bytes));
538 verify_volume_header(const hammer2_volume_data_t *voldata)
540 hammer2_crc32_t crc0, crc1;
541 const char *p = (const char*)voldata;
543 if ((voldata->magic != HAMMER2_VOLUME_ID_HBO) &&
544 (voldata->magic != HAMMER2_VOLUME_ID_ABO)) {
545 tfprintf(stderr, 1, "Bad magic %jX\n", voldata->magic);
549 if (voldata->magic == HAMMER2_VOLUME_ID_ABO)
550 tfprintf(stderr, 1, "Reverse endian\n");
552 crc0 = voldata->icrc_sects[HAMMER2_VOL_ICRC_SECT0];
553 crc1 = hammer2_icrc32(p + HAMMER2_VOLUME_ICRC0_OFF,
554 HAMMER2_VOLUME_ICRC0_SIZE);
556 tfprintf(stderr, 1, "Bad HAMMER2_VOL_ICRC_SECT0 CRC\n");
560 crc0 = voldata->icrc_sects[HAMMER2_VOL_ICRC_SECT1];
561 crc1 = hammer2_icrc32(p + HAMMER2_VOLUME_ICRC1_OFF,
562 HAMMER2_VOLUME_ICRC1_SIZE);
564 tfprintf(stderr, 1, "Bad HAMMER2_VOL_ICRC_SECT1 CRC\n");
568 crc0 = voldata->icrc_volheader;
569 crc1 = hammer2_icrc32(p + HAMMER2_VOLUME_ICRCVH_OFF,
570 HAMMER2_VOLUME_ICRCVH_SIZE);
572 tfprintf(stderr, 1, "Bad volume header CRC\n");
580 read_media(int fd, const hammer2_blockref_t *bref, hammer2_media_data_t *media,
583 hammer2_off_t io_off, io_base;
584 size_t bytes, io_bytes, boff;
586 bytes = (bref->data_off & HAMMER2_OFF_MASK_RADIX);
588 bytes = (size_t)1 << bytes;
590 *media_bytes = bytes;
595 io_off = bref->data_off & ~HAMMER2_OFF_MASK_RADIX;
596 io_base = io_off & ~(hammer2_off_t)(HAMMER2_MINIOSIZE - 1);
597 boff = io_off - io_base;
599 io_bytes = HAMMER2_MINIOSIZE;
600 while (io_bytes + boff < bytes)
603 if (io_bytes > sizeof(*media))
605 if (lseek(fd, io_base, SEEK_SET) == -1)
607 if (read(fd, media, io_bytes) != (ssize_t)io_bytes)
610 memmove(media, (char *)media + boff, bytes);
616 verify_blockref(int fd, const hammer2_volume_data_t *voldata,
617 const hammer2_blockref_t *bref, bool norecurse, blockref_stats_t *bstats)
619 hammer2_media_data_t media;
620 hammer2_blockref_t *bscan;
630 uint8_t digest[SHA256_DIGEST_LENGTH];
631 uint64_t digest64[SHA256_DIGEST_LENGTH/8];
634 bstats->total_blockref++;
636 switch (bref->type) {
637 case HAMMER2_BREF_TYPE_EMPTY:
639 bstats->total_empty++;
641 bstats->total_blockref--;
643 case HAMMER2_BREF_TYPE_INODE:
644 bstats->volume.total_inode++;
646 case HAMMER2_BREF_TYPE_INDIRECT:
647 bstats->volume.total_indirect++;
649 case HAMMER2_BREF_TYPE_DATA:
650 bstats->volume.total_data++;
652 case HAMMER2_BREF_TYPE_DIRENT:
653 bstats->volume.total_dirent++;
655 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
656 bstats->freemap.total_freemap_node++;
658 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
659 bstats->freemap.total_freemap_leaf++;
661 case HAMMER2_BREF_TYPE_VOLUME:
662 bstats->total_blockref--;
664 case HAMMER2_BREF_TYPE_FREEMAP:
665 bstats->total_blockref--;
668 bstats->total_invalid++;
669 snprintf(msg, sizeof(msg), "Invalid blockref type %d",
671 add_blockref_entry(&bstats->root, bref, msg);
676 switch (read_media(fd, bref, &media, &bytes)) {
678 add_blockref_entry(&bstats->root, bref, "Bad I/O bytes");
681 add_blockref_entry(&bstats->root, bref, "Failed to read media");
687 if (bref->type != HAMMER2_BREF_TYPE_VOLUME &&
688 bref->type != HAMMER2_BREF_TYPE_FREEMAP)
689 bstats->total_bytes += bytes;
691 if (!CountEmpty && bref->type == HAMMER2_BREF_TYPE_EMPTY) {
693 bstats->total_bytes -= bytes;
696 if (QuietOpt <= 0 && (bstats->total_blockref % 100) == 0)
697 print_blockref_stats(bstats, false);
702 switch (HAMMER2_DEC_CHECK(bref->methods)) {
703 case HAMMER2_CHECK_ISCSI32:
704 cv = hammer2_icrc32(&media, bytes);
705 if (bref->check.iscsi32.value != cv) {
706 add_blockref_entry(&bstats->root, bref,
707 "Bad HAMMER2_CHECK_ISCSI32");
711 case HAMMER2_CHECK_XXHASH64:
712 cv64 = XXH64(&media, bytes, XXH_HAMMER2_SEED);
713 if (bref->check.xxhash64.value != cv64) {
714 add_blockref_entry(&bstats->root, bref,
715 "Bad HAMMER2_CHECK_XXHASH64");
719 case HAMMER2_CHECK_SHA192:
720 SHA256_Init(&hash_ctx);
721 SHA256_Update(&hash_ctx, &media, bytes);
722 SHA256_Final(u.digest, &hash_ctx);
723 u.digest64[2] ^= u.digest64[3];
724 if (memcmp(u.digest, bref->check.sha192.data,
725 sizeof(bref->check.sha192.data))) {
726 add_blockref_entry(&bstats->root, bref,
727 "Bad HAMMER2_CHECK_SHA192");
731 case HAMMER2_CHECK_FREEMAP:
732 cv = hammer2_icrc32(&media, bytes);
733 if (bref->check.freemap.icrc32 != cv) {
734 add_blockref_entry(&bstats->root, bref,
735 "Bad HAMMER2_CHECK_FREEMAP");
741 switch (bref->type) {
742 case HAMMER2_BREF_TYPE_INODE:
743 if (!(media.ipdata.meta.op_flags & HAMMER2_OPFLAG_DIRECTDATA)) {
744 bscan = &media.ipdata.u.blockset.blockref[0];
745 bcount = HAMMER2_SET_COUNT;
751 case HAMMER2_BREF_TYPE_INDIRECT:
752 bscan = &media.npdata[0];
753 bcount = bytes / sizeof(hammer2_blockref_t);
755 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
756 bscan = &media.npdata[0];
757 bcount = bytes / sizeof(hammer2_blockref_t);
759 case HAMMER2_BREF_TYPE_VOLUME:
760 bscan = &media.voldata.sroot_blockset.blockref[0];
761 bcount = HAMMER2_SET_COUNT;
763 case HAMMER2_BREF_TYPE_FREEMAP:
764 bscan = &media.voldata.freemap_blockset.blockref[0];
765 bcount = HAMMER2_SET_COUNT;
776 * If failed, no recurse, but still verify its direct children.
777 * Beyond that is probably garbage.
779 for (i = 0; norecurse == false && i < bcount; ++i)
780 if (verify_blockref(fd, voldata, &bscan[i], failed, bstats)
783 return failed ? -1 : 0;
787 init_pfs_blockref(int fd, const hammer2_volume_data_t *voldata,
788 const hammer2_blockref_t *bref, struct blockref_list *blist)
790 hammer2_media_data_t media;
791 hammer2_inode_data_t ipdata;
792 hammer2_blockref_t *bscan;
796 if (read_media(fd, bref, &media, &bytes))
801 switch (bref->type) {
802 case HAMMER2_BREF_TYPE_INODE:
803 ipdata = media.ipdata;
804 if (ipdata.meta.pfs_type & HAMMER2_PFSTYPE_SUPROOT) {
805 bscan = &ipdata.u.blockset.blockref[0];
806 bcount = HAMMER2_SET_COUNT;
810 if (ipdata.meta.op_flags & HAMMER2_OPFLAG_PFSROOT) {
811 struct blockref_msg *p;
812 p = calloc(1, sizeof(*p));
815 p->msg = strdup(ipdata.filename);
816 TAILQ_INSERT_TAIL(blist, p, entry);
818 assert(0); /* should only see SUPROOT or PFS */
821 case HAMMER2_BREF_TYPE_INDIRECT:
822 bscan = &media.npdata[0];
823 bcount = bytes / sizeof(hammer2_blockref_t);
825 case HAMMER2_BREF_TYPE_VOLUME:
826 bscan = &media.voldata.sroot_blockset.blockref[0];
827 bcount = HAMMER2_SET_COUNT;
835 for (i = 0; i < bcount; ++i)
836 if (init_pfs_blockref(fd, voldata, &bscan[i], blist) == -1)
842 cleanup_pfs_blockref(struct blockref_list *blist)
844 cleanup_blockref_msg(blist);
848 print_media(FILE *fp, int tab, const hammer2_blockref_t *bref,
849 hammer2_media_data_t *media, size_t media_bytes)
851 hammer2_blockref_t *bscan;
852 hammer2_inode_data_t *ipdata;
853 int i, bcount, namelen;
856 switch (bref->type) {
857 case HAMMER2_BREF_TYPE_INODE:
858 ipdata = &media->ipdata;
859 namelen = ipdata->meta.name_len;
860 if (namelen > HAMMER2_INODE_MAXNAME)
862 tfprintf(fp, tab, "filename \"%*.*s\"\n", namelen, namelen,
864 tfprintf(fp, tab, "version %d\n", ipdata->meta.version);
865 tfprintf(fp, tab, "pfs_subtype %d\n", ipdata->meta.pfs_subtype);
866 tfprintf(fp, tab, "uflags 0x%08x\n", ipdata->meta.uflags);
867 if (ipdata->meta.rmajor || ipdata->meta.rminor) {
868 tfprintf(fp, tab, "rmajor %d\n", ipdata->meta.rmajor);
869 tfprintf(fp, tab, "rminor %d\n", ipdata->meta.rminor);
871 tfprintf(fp, tab, "ctime %s\n",
872 hammer2_time64_to_str(ipdata->meta.ctime, &str));
873 tfprintf(fp, tab, "mtime %s\n",
874 hammer2_time64_to_str(ipdata->meta.mtime, &str));
875 tfprintf(fp, tab, "atime %s\n",
876 hammer2_time64_to_str(ipdata->meta.atime, &str));
877 tfprintf(fp, tab, "btime %s\n",
878 hammer2_time64_to_str(ipdata->meta.btime, &str));
879 tfprintf(fp, tab, "uid %s\n",
880 hammer2_uuid_to_str(&ipdata->meta.uid, &str));
881 tfprintf(fp, tab, "gid %s\n",
882 hammer2_uuid_to_str(&ipdata->meta.gid, &str));
883 tfprintf(fp, tab, "type %s\n",
884 hammer2_iptype_to_str(ipdata->meta.type));
885 tfprintf(fp, tab, "op_flags 0x%02x\n", ipdata->meta.op_flags);
886 tfprintf(fp, tab, "cap_flags 0x%04x\n", ipdata->meta.cap_flags);
887 tfprintf(fp, tab, "mode %-7o\n", ipdata->meta.mode);
888 tfprintf(fp, tab, "inum 0x%016jx\n", ipdata->meta.inum);
889 tfprintf(fp, tab, "size %ju ", (uintmax_t)ipdata->meta.size);
890 if (ipdata->meta.op_flags & HAMMER2_OPFLAG_DIRECTDATA &&
891 ipdata->meta.size <= HAMMER2_EMBEDDED_BYTES)
892 printf("(embedded data)\n");
895 tfprintf(fp, tab, "nlinks %ju\n",
896 (uintmax_t)ipdata->meta.nlinks);
897 tfprintf(fp, tab, "iparent 0x%016jx\n",
898 (uintmax_t)ipdata->meta.iparent);
899 tfprintf(fp, tab, "name_key 0x%016jx\n",
900 (uintmax_t)ipdata->meta.name_key);
901 tfprintf(fp, tab, "name_len %u\n", ipdata->meta.name_len);
902 tfprintf(fp, tab, "ncopies %u\n", ipdata->meta.ncopies);
903 tfprintf(fp, tab, "comp_algo %u\n", ipdata->meta.comp_algo);
904 tfprintf(fp, tab, "target_type %u\n", ipdata->meta.target_type);
905 tfprintf(fp, tab, "check_algo %u\n", ipdata->meta.check_algo);
906 if ((ipdata->meta.op_flags & HAMMER2_OPFLAG_PFSROOT) ||
907 ipdata->meta.pfs_type == HAMMER2_PFSTYPE_SUPROOT) {
908 tfprintf(fp, tab, "pfs_nmasters %u\n",
909 ipdata->meta.pfs_nmasters);
910 tfprintf(fp, tab, "pfs_type %u (%s)\n",
911 ipdata->meta.pfs_type,
912 hammer2_pfstype_to_str(ipdata->meta.pfs_type));
913 tfprintf(fp, tab, "pfs_inum 0x%016jx\n",
914 (uintmax_t)ipdata->meta.pfs_inum);
915 tfprintf(fp, tab, "pfs_clid %s\n",
916 hammer2_uuid_to_str(&ipdata->meta.pfs_clid, &str));
917 tfprintf(fp, tab, "pfs_fsid %s\n",
918 hammer2_uuid_to_str(&ipdata->meta.pfs_fsid, &str));
919 tfprintf(fp, tab, "pfs_lsnap_tid 0x%016jx\n",
920 (uintmax_t)ipdata->meta.pfs_lsnap_tid);
922 tfprintf(fp, tab, "data_quota %ju\n",
923 (uintmax_t)ipdata->meta.data_quota);
924 tfprintf(fp, tab, "data_count %ju\n",
925 (uintmax_t)bref->embed.stats.data_count);
926 tfprintf(fp, tab, "inode_quota %ju\n",
927 (uintmax_t)ipdata->meta.inode_quota);
928 tfprintf(fp, tab, "inode_count %ju\n",
929 (uintmax_t)bref->embed.stats.inode_count);
931 case HAMMER2_BREF_TYPE_INDIRECT:
932 bcount = media_bytes / sizeof(hammer2_blockref_t);
933 for (i = 0; i < bcount; ++i) {
934 bscan = &media->npdata[i];
935 tfprintf(fp, tab, "%3d %016jx %-12s %016jx/%-2d\n",
936 i, (uintmax_t)bscan->data_off,
937 hammer2_breftype_to_str(bscan->type),
938 (uintmax_t)bscan->key,
942 case HAMMER2_BREF_TYPE_DIRENT:
943 if (bref->embed.dirent.namlen <= sizeof(bref->check.buf)) {
944 tfprintf(fp, tab, "filename \"%*.*s\"\n",
945 bref->embed.dirent.namlen,
946 bref->embed.dirent.namlen,
949 tfprintf(fp, tab, "filename \"%*.*s\"\n",
950 bref->embed.dirent.namlen,
951 bref->embed.dirent.namlen,
954 tfprintf(fp, tab, "inum 0x%016jx\n",
955 (uintmax_t)bref->embed.dirent.inum);
956 tfprintf(fp, tab, "namlen %d\n",
957 (uintmax_t)bref->embed.dirent.namlen);
958 tfprintf(fp, tab, "type %s\n",
959 hammer2_iptype_to_str(bref->embed.dirent.type));
961 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
962 bcount = media_bytes / sizeof(hammer2_blockref_t);
963 for (i = 0; i < bcount; ++i) {
964 bscan = &media->npdata[i];
965 tfprintf(fp, tab, "%3d %016jx %-12s %016jx/%-2d\n",
966 i, (uintmax_t)bscan->data_off,
967 hammer2_breftype_to_str(bscan->type),
968 (uintmax_t)bscan->key,
972 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
973 for (i = 0; i < HAMMER2_FREEMAP_COUNT; ++i) {
974 hammer2_off_t data_off = bref->key +
975 i * HAMMER2_FREEMAP_LEVEL0_SIZE;
976 #if HAMMER2_BMAP_ELEMENTS != 8
977 #error "HAMMER2_BMAP_ELEMENTS != 8"
979 tfprintf(fp, tab, "%016jx %04d.%04x (avail=%7d) "
980 "%016jx %016jx %016jx %016jx "
981 "%016jx %016jx %016jx %016jx\n",
982 data_off, i, media->bmdata[i].class,
983 media->bmdata[i].avail,
984 media->bmdata[i].bitmapq[0],
985 media->bmdata[i].bitmapq[1],
986 media->bmdata[i].bitmapq[2],
987 media->bmdata[i].bitmapq[3],
988 media->bmdata[i].bitmapq[4],
989 media->bmdata[i].bitmapq[5],
990 media->bmdata[i].bitmapq[6],
991 media->bmdata[i].bitmapq[7]);
1002 test_hammer2(const char *devpath)
1005 bool failed = false;
1008 fd = open(devpath, O_RDONLY);
1014 if (fstat(fd, &st) == -1) {
1019 if (!S_ISCHR(st.st_mode)) {
1020 fprintf(stderr, "%s is not a block device\n", devpath);
1025 best_zone = find_best_zone(fd);
1026 if (best_zone == -1)
1027 fprintf(stderr, "Failed to find best zone\n");
1029 printf("volume header\n");
1030 if (test_volume_header(fd) == -1) {
1036 printf("freemap\n");
1037 if (test_blockref(fd, HAMMER2_BREF_TYPE_FREEMAP) == -1) {
1044 if (test_blockref(fd, HAMMER2_BREF_TYPE_VOLUME) == -1) {
1050 if (test_pfs_blockref(fd) == -1) {
1059 return failed ? -1 : 0;