xref: /freebsd/usr.sbin/fstyp/hammer.c (revision 0ab52bd3ebc24780827e96be84ca8dd462c7667b)
1509798eaSPedro F. Giffuni /*-
2509798eaSPedro F. Giffuni  * Copyright (c) 2016 The DragonFly Project
3509798eaSPedro F. Giffuni  * All rights reserved.
4509798eaSPedro F. Giffuni  *
5509798eaSPedro F. Giffuni  * This software was developed by Edward Tomasz Napierala under sponsorship
6509798eaSPedro F. Giffuni  * from the FreeBSD Foundation.
7509798eaSPedro F. Giffuni  *
8509798eaSPedro F. Giffuni  * Redistribution and use in source and binary forms, with or without
9509798eaSPedro F. Giffuni  * modification, are permitted provided that the following conditions
10509798eaSPedro F. Giffuni  * are met:
11509798eaSPedro F. Giffuni  * 1. Redistributions of source code must retain the above copyright
12509798eaSPedro F. Giffuni  *    notice, this list of conditions and the following disclaimer.
13509798eaSPedro F. Giffuni  * 2. Redistributions in binary form must reproduce the above copyright
14509798eaSPedro F. Giffuni  *    notice, this list of conditions and the following disclaimer in the
15509798eaSPedro F. Giffuni  *    documentation and/or other materials provided with the distribution.
16509798eaSPedro F. Giffuni  *
17509798eaSPedro F. Giffuni  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18509798eaSPedro F. Giffuni  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19509798eaSPedro F. Giffuni  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20509798eaSPedro F. Giffuni  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21509798eaSPedro F. Giffuni  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22509798eaSPedro F. Giffuni  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23509798eaSPedro F. Giffuni  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24509798eaSPedro F. Giffuni  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25509798eaSPedro F. Giffuni  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26509798eaSPedro F. Giffuni  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27509798eaSPedro F. Giffuni  * SUCH DAMAGE.
28509798eaSPedro F. Giffuni  */
29509798eaSPedro F. Giffuni 
30509798eaSPedro F. Giffuni #include <sys/cdefs.h>
31509798eaSPedro F. Giffuni __FBSDID("$FreeBSD$");
32509798eaSPedro F. Giffuni 
33509798eaSPedro F. Giffuni #include <stdio.h>
34509798eaSPedro F. Giffuni #include <stdlib.h>
35509798eaSPedro F. Giffuni #include <string.h>
36509798eaSPedro F. Giffuni #include <err.h>
37509798eaSPedro F. Giffuni #include <assert.h>
38509798eaSPedro F. Giffuni 
39509798eaSPedro F. Giffuni #include <sys/types.h>
40509798eaSPedro F. Giffuni 
41509798eaSPedro F. Giffuni #include "hammer_disk.h"
42509798eaSPedro F. Giffuni 
43509798eaSPedro F. Giffuni #include "fstyp.h"
44509798eaSPedro F. Giffuni 
45509798eaSPedro F. Giffuni static hammer_volume_ondisk_t
46509798eaSPedro F. Giffuni __read_ondisk(FILE *fp)
47509798eaSPedro F. Giffuni {
48509798eaSPedro F. Giffuni 	hammer_volume_ondisk_t ondisk;
49509798eaSPedro F. Giffuni 
50509798eaSPedro F. Giffuni 	ondisk = read_buf(fp, 0, sizeof(*ondisk));
51509798eaSPedro F. Giffuni 	if (ondisk == NULL)
52509798eaSPedro F. Giffuni 		err(1, "failed to read ondisk");
53509798eaSPedro F. Giffuni 
54509798eaSPedro F. Giffuni 	return (ondisk);
55509798eaSPedro F. Giffuni }
56509798eaSPedro F. Giffuni 
57509798eaSPedro F. Giffuni static int
58509798eaSPedro F. Giffuni __test_ondisk(const hammer_volume_ondisk_t ondisk)
59509798eaSPedro F. Giffuni {
60509798eaSPedro F. Giffuni 	static int count = 0;
61509798eaSPedro F. Giffuni 	static hammer_uuid_t fsid, fstype;
62509798eaSPedro F. Giffuni 	static char label[64];
63509798eaSPedro F. Giffuni 
64509798eaSPedro F. Giffuni 	if (ondisk->vol_signature != HAMMER_FSBUF_VOLUME &&
65509798eaSPedro F. Giffuni 	    ondisk->vol_signature != HAMMER_FSBUF_VOLUME_REV)
66509798eaSPedro F. Giffuni 		return (1);
67509798eaSPedro F. Giffuni 	if (ondisk->vol_rootvol != HAMMER_ROOT_VOLNO)
68509798eaSPedro F. Giffuni 		return (2);
69509798eaSPedro F. Giffuni 	if (ondisk->vol_no < 0 || ondisk->vol_no > HAMMER_MAX_VOLUMES - 1)
70509798eaSPedro F. Giffuni 		return (3);
71509798eaSPedro F. Giffuni 	if (ondisk->vol_count < 1 || ondisk->vol_count > HAMMER_MAX_VOLUMES)
72509798eaSPedro F. Giffuni 		return (4);
73509798eaSPedro F. Giffuni 
74509798eaSPedro F. Giffuni 	if (count == 0) {
75509798eaSPedro F. Giffuni 		count = ondisk->vol_count;
76509798eaSPedro F. Giffuni 		assert(count != 0);
77509798eaSPedro F. Giffuni 		memcpy(&fsid, &ondisk->vol_fsid, sizeof(fsid));
78509798eaSPedro F. Giffuni 		memcpy(&fstype, &ondisk->vol_fstype, sizeof(fstype));
79*0ab52bd3SEric van Gyzen 		strlcpy(label, ondisk->vol_label, sizeof(label));
80509798eaSPedro F. Giffuni 	} else {
81509798eaSPedro F. Giffuni 		if (ondisk->vol_count != count)
82509798eaSPedro F. Giffuni 			return (5);
83509798eaSPedro F. Giffuni 		if (memcmp(&ondisk->vol_fsid, &fsid, sizeof(fsid)))
84509798eaSPedro F. Giffuni 			return (6);
85509798eaSPedro F. Giffuni 		if (memcmp(&ondisk->vol_fstype, &fstype, sizeof(fstype)))
86509798eaSPedro F. Giffuni 			return (7);
87*0ab52bd3SEric van Gyzen 		if (strcmp(ondisk->vol_label, label))
88509798eaSPedro F. Giffuni 			return (8);
89509798eaSPedro F. Giffuni 	}
90509798eaSPedro F. Giffuni 
91509798eaSPedro F. Giffuni 	return (0);
92509798eaSPedro F. Giffuni }
93509798eaSPedro F. Giffuni 
94509798eaSPedro F. Giffuni int
95509798eaSPedro F. Giffuni fstyp_hammer(FILE *fp, char *label, size_t size)
96509798eaSPedro F. Giffuni {
97509798eaSPedro F. Giffuni 	hammer_volume_ondisk_t ondisk;
98509798eaSPedro F. Giffuni 	int error = 1;
99509798eaSPedro F. Giffuni 
100509798eaSPedro F. Giffuni 	ondisk = __read_ondisk(fp);
101509798eaSPedro F. Giffuni 	if (ondisk->vol_no != HAMMER_ROOT_VOLNO)
102509798eaSPedro F. Giffuni 		goto done;
103509798eaSPedro F. Giffuni 	if (ondisk->vol_count != 1)
104509798eaSPedro F. Giffuni 		goto done;
105509798eaSPedro F. Giffuni 	if (__test_ondisk(ondisk))
106509798eaSPedro F. Giffuni 		goto done;
107509798eaSPedro F. Giffuni 
108509798eaSPedro F. Giffuni 	strlcpy(label, ondisk->vol_label, size);
109509798eaSPedro F. Giffuni 	error = 0;
110509798eaSPedro F. Giffuni done:
111509798eaSPedro F. Giffuni 	free(ondisk);
112509798eaSPedro F. Giffuni 	return (error);
113509798eaSPedro F. Giffuni }
114509798eaSPedro F. Giffuni 
115509798eaSPedro F. Giffuni static int
116509798eaSPedro F. Giffuni __test_volume(const char *volpath)
117509798eaSPedro F. Giffuni {
118509798eaSPedro F. Giffuni 	hammer_volume_ondisk_t ondisk;
119509798eaSPedro F. Giffuni 	FILE *fp;
120509798eaSPedro F. Giffuni 	int volno = -1;
121509798eaSPedro F. Giffuni 
122509798eaSPedro F. Giffuni 	if ((fp = fopen(volpath, "r")) == NULL)
123509798eaSPedro F. Giffuni 		err(1, "failed to open %s", volpath);
124509798eaSPedro F. Giffuni 
125509798eaSPedro F. Giffuni 	ondisk = __read_ondisk(fp);
126509798eaSPedro F. Giffuni 	fclose(fp);
127509798eaSPedro F. Giffuni 	if (__test_ondisk(ondisk))
128509798eaSPedro F. Giffuni 		goto done;
129509798eaSPedro F. Giffuni 
130509798eaSPedro F. Giffuni 	volno = ondisk->vol_no;
131509798eaSPedro F. Giffuni done:
132509798eaSPedro F. Giffuni 	free(ondisk);
133509798eaSPedro F. Giffuni 	return (volno);
134509798eaSPedro F. Giffuni }
135509798eaSPedro F. Giffuni 
136509798eaSPedro F. Giffuni static int
137509798eaSPedro F. Giffuni __fsvtyp_hammer(const char *blkdevs, char *label, size_t size, int partial)
138509798eaSPedro F. Giffuni {
139509798eaSPedro F. Giffuni 	hammer_volume_ondisk_t ondisk;
140509798eaSPedro F. Giffuni 	FILE *fp;
141509798eaSPedro F. Giffuni 	char *dup, *p, *volpath, x[HAMMER_MAX_VOLUMES];
142509798eaSPedro F. Giffuni 	int i, volno, error = 1;
143509798eaSPedro F. Giffuni 
144509798eaSPedro F. Giffuni 	memset(x, 0, sizeof(x));
145509798eaSPedro F. Giffuni 	dup = strdup(blkdevs);
146509798eaSPedro F. Giffuni 	p = dup;
147509798eaSPedro F. Giffuni 
148509798eaSPedro F. Giffuni 	while (p) {
149509798eaSPedro F. Giffuni 		volpath = p;
150509798eaSPedro F. Giffuni 		if ((p = strchr(p, ':')) != NULL)
151509798eaSPedro F. Giffuni 			*p++ = '\0';
152509798eaSPedro F. Giffuni 		if ((volno = __test_volume(volpath)) == -1)
153509798eaSPedro F. Giffuni 			break;
154509798eaSPedro F. Giffuni 		x[volno]++;
155509798eaSPedro F. Giffuni 	}
156509798eaSPedro F. Giffuni 
157509798eaSPedro F. Giffuni 	if ((fp = fopen(volpath, "r")) == NULL)
158509798eaSPedro F. Giffuni 		err(1, "failed to open %s", volpath);
159509798eaSPedro F. Giffuni 	ondisk = __read_ondisk(fp);
160509798eaSPedro F. Giffuni 	fclose(fp);
161509798eaSPedro F. Giffuni 
162509798eaSPedro F. Giffuni 	free(dup);
163509798eaSPedro F. Giffuni 
164509798eaSPedro F. Giffuni 	if (volno == -1)
165509798eaSPedro F. Giffuni 		goto done;
166509798eaSPedro F. Giffuni 	if (partial)
167509798eaSPedro F. Giffuni 		goto success;
168509798eaSPedro F. Giffuni 
169509798eaSPedro F. Giffuni 	for (i = 0; i < HAMMER_MAX_VOLUMES; i++)
170509798eaSPedro F. Giffuni 		if (x[i] > 1)
171509798eaSPedro F. Giffuni 			goto done;
172509798eaSPedro F. Giffuni 	for (i = 0; i < HAMMER_MAX_VOLUMES; i++)
173509798eaSPedro F. Giffuni 		if (x[i] == 0)
174509798eaSPedro F. Giffuni 			break;
175509798eaSPedro F. Giffuni 	if (ondisk->vol_count != i)
176509798eaSPedro F. Giffuni 		goto done;
177509798eaSPedro F. Giffuni 	for (; i < HAMMER_MAX_VOLUMES; i++)
178509798eaSPedro F. Giffuni 		if (x[i] != 0)
179509798eaSPedro F. Giffuni 			goto done;
180509798eaSPedro F. Giffuni success:
181509798eaSPedro F. Giffuni 	strlcpy(label, ondisk->vol_label, size);
182509798eaSPedro F. Giffuni 	error = 0;
183509798eaSPedro F. Giffuni done:
184509798eaSPedro F. Giffuni 	free(ondisk);
185509798eaSPedro F. Giffuni 	return (error);
186509798eaSPedro F. Giffuni }
187509798eaSPedro F. Giffuni 
188509798eaSPedro F. Giffuni int
189509798eaSPedro F. Giffuni fsvtyp_hammer(const char *blkdevs, char *label, size_t size)
190509798eaSPedro F. Giffuni {
191509798eaSPedro F. Giffuni 	return (__fsvtyp_hammer(blkdevs, label, size, 0));
192509798eaSPedro F. Giffuni }
193509798eaSPedro F. Giffuni 
194509798eaSPedro F. Giffuni int
195509798eaSPedro F. Giffuni fsvtyp_hammer_partial(const char *blkdevs, char *label, size_t size)
196509798eaSPedro F. Giffuni {
197509798eaSPedro F. Giffuni 	return (__fsvtyp_hammer(blkdevs, label, size, 1));
198509798eaSPedro F. Giffuni }
199