xref: /freebsd/usr.sbin/fstyp/hammer.c (revision 5bb3134a8c21cb87b30e135ef168483f0333dabb)
1 /*-
2  * Copyright (c) 2016-2019 The DragonFly Project
3  * Copyright (c) 2016-2019 Tomohiro Kusumi <tkusumi@netbsd.org>
4  * All rights reserved.
5  *
6  * This software was developed by Edward Tomasz Napierala under sponsorship
7  * from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <err.h>
38 #include <assert.h>
39 
40 #include <sys/types.h>
41 
42 #include "hammer_disk.h"
43 
44 #include "fstyp.h"
45 
46 extern int fsvtyp_hammer(const char *blkdevs, char *label, size_t size);
47 extern int fsvtyp_hammer_partial(const char *blkdevs, char *label, size_t size);
48 
49 static hammer_volume_ondisk_t
50 read_ondisk(FILE *fp)
51 {
52 	hammer_volume_ondisk_t ondisk;
53 
54 	ondisk = read_buf(fp, 0, sizeof(*ondisk));
55 	if (ondisk == NULL)
56 		err(1, "failed to read ondisk");
57 
58 	return (ondisk);
59 }
60 
61 static int
62 test_ondisk(const hammer_volume_ondisk_t ondisk)
63 {
64 	static int count = 0;
65 	static hammer_uuid_t fsid, fstype;
66 	static char label[64];
67 
68 	if (ondisk->vol_signature != HAMMER_FSBUF_VOLUME &&
69 	    ondisk->vol_signature != HAMMER_FSBUF_VOLUME_REV)
70 		return (1);
71 	if (ondisk->vol_rootvol != HAMMER_ROOT_VOLNO)
72 		return (2);
73 	if (ondisk->vol_no < 0 || ondisk->vol_no > HAMMER_MAX_VOLUMES - 1)
74 		return (3);
75 	if (ondisk->vol_count < 1 || ondisk->vol_count > HAMMER_MAX_VOLUMES)
76 		return (4);
77 
78 	if (count == 0) {
79 		count = ondisk->vol_count;
80 		assert(count != 0);
81 		memcpy(&fsid, &ondisk->vol_fsid, sizeof(fsid));
82 		memcpy(&fstype, &ondisk->vol_fstype, sizeof(fstype));
83 		strlcpy(label, ondisk->vol_label, sizeof(label));
84 	} else {
85 		if (ondisk->vol_count != count)
86 			return (5);
87 		if (memcmp(&ondisk->vol_fsid, &fsid, sizeof(fsid)))
88 			return (6);
89 		if (memcmp(&ondisk->vol_fstype, &fstype, sizeof(fstype)))
90 			return (7);
91 		if (strcmp(ondisk->vol_label, label))
92 			return (8);
93 	}
94 
95 	return (0);
96 }
97 
98 int
99 fstyp_hammer(FILE *fp, char *label, size_t size)
100 {
101 	hammer_volume_ondisk_t ondisk;
102 	int error = 1;
103 
104 	ondisk = read_ondisk(fp);
105 	if (ondisk->vol_no != HAMMER_ROOT_VOLNO)
106 		goto fail;
107 	if (ondisk->vol_count != 1)
108 		goto fail;
109 	if (test_ondisk(ondisk))
110 		goto fail;
111 
112 	strlcpy(label, ondisk->vol_label, size);
113 	error = 0;
114 fail:
115 	free(ondisk);
116 	return (error);
117 }
118 
119 static int
120 test_volume(const char *volpath)
121 {
122 	hammer_volume_ondisk_t ondisk;
123 	FILE *fp;
124 	int volno = -1;
125 
126 	if ((fp = fopen(volpath, "r")) == NULL)
127 		err(1, "failed to open %s", volpath);
128 
129 	ondisk = read_ondisk(fp);
130 	fclose(fp);
131 	if (test_ondisk(ondisk))
132 		goto fail;
133 
134 	volno = ondisk->vol_no;
135 fail:
136 	free(ondisk);
137 	return (volno);
138 }
139 
140 static int
141 __fsvtyp_hammer(const char *blkdevs, char *label, size_t size, int partial)
142 {
143 	hammer_volume_ondisk_t ondisk = NULL;
144 	FILE *fp;
145 	char *dup, *p, *volpath, x[HAMMER_MAX_VOLUMES];
146 	int i, volno, error = 1;
147 
148 	if (!blkdevs)
149 		goto fail;
150 
151 	memset(x, 0, sizeof(x));
152 	dup = strdup(blkdevs);
153 	p = dup;
154 
155 	volpath = NULL;
156 	volno = -1;
157 	while (p) {
158 		volpath = p;
159 		if ((p = strchr(p, ':')) != NULL)
160 			*p++ = '\0';
161 		if ((volno = test_volume(volpath)) == -1)
162 			break;
163 		assert(volno >= 0);
164 		assert(volno < HAMMER_MAX_VOLUMES);
165 		x[volno]++;
166 	}
167 
168 	if (!volpath)
169 		err(1, "invalid path %s", blkdevs);
170 	if ((fp = fopen(volpath, "r")) == NULL)
171 		err(1, "failed to open %s", volpath);
172 	ondisk = read_ondisk(fp);
173 	fclose(fp);
174 
175 	free(dup);
176 
177 	if (volno == -1)
178 		goto fail;
179 	if (partial)
180 		goto success;
181 
182 	for (i = 0; i < HAMMER_MAX_VOLUMES; i++)
183 		if (x[i] > 1)
184 			goto fail;
185 	for (i = 0; i < HAMMER_MAX_VOLUMES; i++)
186 		if (x[i] == 0)
187 			break;
188 	if (ondisk->vol_count != i)
189 		goto fail;
190 	for (; i < HAMMER_MAX_VOLUMES; i++)
191 		if (x[i] != 0)
192 			goto fail;
193 success:
194 	strlcpy(label, ondisk->vol_label, size);
195 	error = 0;
196 fail:
197 	free(ondisk);
198 	return (error);
199 }
200 
201 int
202 fsvtyp_hammer(const char *blkdevs, char *label, size_t size)
203 {
204 	return (__fsvtyp_hammer(blkdevs, label, size, 0));
205 }
206 
207 int
208 fsvtyp_hammer_partial(const char *blkdevs, char *label, size_t size)
209 {
210 	return (__fsvtyp_hammer(blkdevs, label, size, 1));
211 }
212