2 * Copyright (c) 2017-2019 The DragonFly Project
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
38 #include <sys/types.h>
40 #include "hammer2_disk.h"
44 static hammer2_volume_data_t*
45 __read_voldata(FILE *fp)
47 hammer2_volume_data_t *voldata;
49 voldata = read_buf(fp, 0, sizeof(*voldata));
51 err(1, "failed to read volume data");
57 __test_voldata(const hammer2_volume_data_t *voldata)
59 if (voldata->magic != HAMMER2_VOLUME_ID_HBO &&
60 voldata->magic != HAMMER2_VOLUME_ID_ABO)
67 __read_label(FILE *fp, char *label, size_t size)
69 hammer2_blockref_t broot, best, *bref;
70 hammer2_media_data_t *vols[HAMMER2_NUM_VOLHDRS], *media;
71 hammer2_off_t io_off, io_base;
72 size_t bytes, io_bytes, boff;
73 int i, best_i, error = 0;
76 memset(&best, 0, sizeof(best));
78 for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) {
79 memset(&broot, 0, sizeof(broot));
80 broot.type = HAMMER2_BREF_TYPE_VOLUME;
81 broot.data_off = (i * HAMMER2_ZONE_BYTES64) | HAMMER2_PBUFRADIX;
82 vols[i] = read_buf(fp, broot.data_off & ~HAMMER2_OFF_MASK_RADIX,
84 broot.mirror_tid = vols[i]->voldata.mirror_tid;
85 if (best_i < 0 || best.mirror_tid < broot.mirror_tid) {
91 bref = &vols[best_i]->voldata.sroot_blockset.blockref[0];
92 if (bref->type != HAMMER2_BREF_TYPE_INODE) {
93 warnx("Superroot blockref type is not inode");
98 bytes = bref->data_off & HAMMER2_OFF_MASK_RADIX;
100 bytes = (size_t)1 << bytes;
101 if (bytes != sizeof(hammer2_inode_data_t)) {
102 warnx("Superroot blockref size does not match inode size");
107 io_off = bref->data_off & ~HAMMER2_OFF_MASK_RADIX;
108 io_base = io_off & ~(hammer2_off_t)(HAMMER2_MINIOSIZE - 1);
109 boff = io_off - io_base;
111 io_bytes = HAMMER2_MINIOSIZE;
112 while (io_bytes + boff < bytes)
114 if (io_bytes > sizeof(*media)) {
115 warnx("Invalid I/O bytes");
120 media = read_buf(fp, io_base, io_bytes);
122 memcpy(media, (char*)media + boff, bytes);
124 strlcpy(label, (char*)media->ipdata.filename, size);
127 for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++)
134 fstyp_hammer2(FILE *fp, char *label, size_t size)
136 hammer2_volume_data_t *voldata;
139 voldata = __read_voldata(fp);
140 if (__test_voldata(voldata))
143 error = __read_label(fp, label, size);