]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/fstyp/hammer2.c
MFV r356143:
[FreeBSD/FreeBSD.git] / usr.sbin / fstyp / hammer2.c
1 /*-
2  * Copyright (c) 2017-2019 The DragonFly Project
3  * All rights reserved.
4  *
5  * This software was developed by Edward Tomasz Napierala under sponsorship
6  * from the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <err.h>
37
38 #include <sys/types.h>
39
40 #include "hammer2_disk.h"
41
42 #include "fstyp.h"
43
44 static hammer2_volume_data_t*
45 __read_voldata(FILE *fp)
46 {
47         hammer2_volume_data_t *voldata;
48
49         voldata = read_buf(fp, 0, sizeof(*voldata));
50         if (voldata == NULL)
51                 err(1, "failed to read volume data");
52
53         return (voldata);
54 }
55
56 static int
57 __test_voldata(const hammer2_volume_data_t *voldata)
58 {
59         if (voldata->magic != HAMMER2_VOLUME_ID_HBO &&
60             voldata->magic != HAMMER2_VOLUME_ID_ABO)
61                 return (1);
62
63         return (0);
64 }
65
66 static int
67 __read_label(FILE *fp, char *label, size_t size)
68 {
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;
74
75         best_i = -1;
76         memset(&best, 0, sizeof(best));
77
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,
83                     sizeof(*vols[i]));
84                 broot.mirror_tid = vols[i]->voldata.mirror_tid;
85                 if (best_i < 0 || best.mirror_tid < broot.mirror_tid) {
86                         best_i = i;
87                         best = broot;
88                 }
89         }
90         if (best_i == -1) {
91                 warnx("Failed to find volume header from zones");
92                 error = 1;
93                 goto done;
94         }
95
96         bref = &vols[best_i]->voldata.sroot_blockset.blockref[0];
97         if (bref->type != HAMMER2_BREF_TYPE_INODE) {
98                 warnx("Superroot blockref type is not inode");
99                 error = 2;
100                 goto done;
101         }
102
103         bytes = bref->data_off & HAMMER2_OFF_MASK_RADIX;
104         if (bytes)
105                 bytes = (size_t)1 << bytes;
106         if (bytes != sizeof(hammer2_inode_data_t)) {
107                 warnx("Superroot blockref size does not match inode size");
108                 error = 3;
109                 goto done;
110         }
111
112         io_off = bref->data_off & ~HAMMER2_OFF_MASK_RADIX;
113         io_base = io_off & ~(hammer2_off_t)(HAMMER2_MINIOSIZE - 1);
114         boff = io_off - io_base;
115
116         io_bytes = HAMMER2_MINIOSIZE;
117         while (io_bytes + boff < bytes)
118                 io_bytes <<= 1;
119         if (io_bytes > sizeof(*media)) {
120                 warnx("Invalid I/O bytes");
121                 error = 4;
122                 goto done;
123         }
124
125         media = read_buf(fp, io_base, io_bytes);
126         if (boff)
127                 memcpy(media, (char*)media + boff, bytes);
128
129         strlcpy(label, (char*)media->ipdata.filename, size);
130         free(media);
131 done:
132         for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++)
133                 free(vols[i]);
134
135         return (error);
136 }
137
138 int
139 fstyp_hammer2(FILE *fp, char *label, size_t size)
140 {
141         hammer2_volume_data_t *voldata;
142         int error = 1;
143
144         voldata = __read_voldata(fp);
145         if (__test_voldata(voldata))
146                 goto done;
147
148         error = __read_label(fp, label, size);
149 done:
150         free(voldata);
151         return (error);
152 }