xref: /freebsd/usr.sbin/fstyp/hammer.c (revision 2e3f49888ec8851bafb22011533217487764fdb0)
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 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <err.h>
36 #include <assert.h>
37 
38 #include <sys/types.h>
39 
40 #include "hammer_disk.h"
41 
42 #include "fstyp.h"
43 
44 extern int fsvtyp_hammer(const char *blkdevs, char *label, size_t size);
45 extern int fsvtyp_hammer_partial(const char *blkdevs, char *label, size_t size);
46 
47 static hammer_volume_ondisk_t
48 read_ondisk(FILE *fp)
49 {
50 	hammer_volume_ondisk_t ondisk;
51 
52 	ondisk = read_buf(fp, 0, sizeof(*ondisk));
53 	if (ondisk == NULL)
54 		err(1, "failed to read ondisk");
55 
56 	return (ondisk);
57 }
58 
59 static int
60 test_ondisk(const hammer_volume_ondisk_t ondisk)
61 {
62 	static int count = 0;
63 	static hammer_uuid_t fsid, fstype;
64 	static char label[64];
65 
66 	if (ondisk->vol_signature != HAMMER_FSBUF_VOLUME &&
67 	    ondisk->vol_signature != HAMMER_FSBUF_VOLUME_REV)
68 		return (1);
69 	if (ondisk->vol_rootvol != HAMMER_ROOT_VOLNO)
70 		return (2);
71 	if (ondisk->vol_no < 0 || ondisk->vol_no > HAMMER_MAX_VOLUMES - 1)
72 		return (3);
73 	if (ondisk->vol_count < 1 || ondisk->vol_count > HAMMER_MAX_VOLUMES)
74 		return (4);
75 
76 	if (count == 0) {
77 		count = ondisk->vol_count;
78 		assert(count != 0);
79 		memcpy(&fsid, &ondisk->vol_fsid, sizeof(fsid));
80 		memcpy(&fstype, &ondisk->vol_fstype, sizeof(fstype));
81 		strlcpy(label, ondisk->vol_label, sizeof(label));
82 	} else {
83 		if (ondisk->vol_count != count)
84 			return (5);
85 		if (memcmp(&ondisk->vol_fsid, &fsid, sizeof(fsid)))
86 			return (6);
87 		if (memcmp(&ondisk->vol_fstype, &fstype, sizeof(fstype)))
88 			return (7);
89 		if (strcmp(ondisk->vol_label, label))
90 			return (8);
91 	}
92 
93 	return (0);
94 }
95 
96 int
97 fstyp_hammer(FILE *fp, char *label, size_t size)
98 {
99 	hammer_volume_ondisk_t ondisk;
100 	int error = 1;
101 
102 	ondisk = read_ondisk(fp);
103 	if (ondisk->vol_no != HAMMER_ROOT_VOLNO)
104 		goto fail;
105 	if (ondisk->vol_count != 1)
106 		goto fail;
107 	if (test_ondisk(ondisk))
108 		goto fail;
109 
110 	strlcpy(label, ondisk->vol_label, size);
111 	error = 0;
112 fail:
113 	free(ondisk);
114 	return (error);
115 }
116 
117 static int
118 test_volume(const char *volpath)
119 {
120 	hammer_volume_ondisk_t ondisk;
121 	FILE *fp;
122 	int volno = -1;
123 
124 	if ((fp = fopen(volpath, "r")) == NULL)
125 		err(1, "failed to open %s", volpath);
126 
127 	ondisk = read_ondisk(fp);
128 	fclose(fp);
129 	if (test_ondisk(ondisk))
130 		goto fail;
131 
132 	volno = ondisk->vol_no;
133 fail:
134 	free(ondisk);
135 	return (volno);
136 }
137 
138 static int
139 __fsvtyp_hammer(const char *blkdevs, char *label, size_t size, int partial)
140 {
141 	hammer_volume_ondisk_t ondisk = NULL;
142 	FILE *fp;
143 	char *dup, *p, *volpath, x[HAMMER_MAX_VOLUMES];
144 	int i, volno, error = 1;
145 
146 	if (!blkdevs)
147 		goto fail;
148 
149 	memset(x, 0, sizeof(x));
150 	dup = strdup(blkdevs);
151 	p = dup;
152 
153 	volpath = NULL;
154 	volno = -1;
155 	while (p) {
156 		volpath = p;
157 		if ((p = strchr(p, ':')) != NULL)
158 			*p++ = '\0';
159 		if ((volno = test_volume(volpath)) == -1)
160 			break;
161 		assert(volno >= 0);
162 		assert(volno < HAMMER_MAX_VOLUMES);
163 		x[volno]++;
164 	}
165 
166 	if (!volpath)
167 		err(1, "invalid path %s", blkdevs);
168 	if ((fp = fopen(volpath, "r")) == NULL)
169 		err(1, "failed to open %s", volpath);
170 	ondisk = read_ondisk(fp);
171 	fclose(fp);
172 
173 	free(dup);
174 
175 	if (volno == -1)
176 		goto fail;
177 	if (partial)
178 		goto success;
179 
180 	for (i = 0; i < HAMMER_MAX_VOLUMES; i++)
181 		if (x[i] > 1)
182 			goto fail;
183 	for (i = 0; i < HAMMER_MAX_VOLUMES; i++)
184 		if (x[i] == 0)
185 			break;
186 	if (ondisk->vol_count != i)
187 		goto fail;
188 	for (; i < HAMMER_MAX_VOLUMES; i++)
189 		if (x[i] != 0)
190 			goto fail;
191 success:
192 	strlcpy(label, ondisk->vol_label, size);
193 	error = 0;
194 fail:
195 	free(ondisk);
196 	return (error);
197 }
198 
199 int
200 fsvtyp_hammer(const char *blkdevs, char *label, size_t size)
201 {
202 	return (__fsvtyp_hammer(blkdevs, label, size, 0));
203 }
204 
205 int
206 fsvtyp_hammer_partial(const char *blkdevs, char *label, size_t size)
207 {
208 	return (__fsvtyp_hammer(blkdevs, label, size, 1));
209 }
210