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