2 * Copyright (c) 2016-2019 The DragonFly Project
3 * Copyright (c) 2016-2019 Tomohiro Kusumi <tkusumi@netbsd.org>
6 * This software was developed by Edward Tomasz Napierala under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
40 #include <sys/types.h>
42 #include "hammer_disk.h"
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);
49 static hammer_volume_ondisk_t
52 hammer_volume_ondisk_t ondisk;
54 ondisk = read_buf(fp, 0, sizeof(*ondisk));
56 err(1, "failed to read ondisk");
62 test_ondisk(const hammer_volume_ondisk_t ondisk)
65 static hammer_uuid_t fsid, fstype;
66 static char label[64];
68 if (ondisk->vol_signature != HAMMER_FSBUF_VOLUME &&
69 ondisk->vol_signature != HAMMER_FSBUF_VOLUME_REV)
71 if (ondisk->vol_rootvol != HAMMER_ROOT_VOLNO)
73 if (ondisk->vol_no < 0 || ondisk->vol_no > HAMMER_MAX_VOLUMES - 1)
75 if (ondisk->vol_count < 1 || ondisk->vol_count > HAMMER_MAX_VOLUMES)
79 count = ondisk->vol_count;
81 memcpy(&fsid, &ondisk->vol_fsid, sizeof(fsid));
82 memcpy(&fstype, &ondisk->vol_fstype, sizeof(fstype));
83 strlcpy(label, ondisk->vol_label, sizeof(label));
85 if (ondisk->vol_count != count)
87 if (memcmp(&ondisk->vol_fsid, &fsid, sizeof(fsid)))
89 if (memcmp(&ondisk->vol_fstype, &fstype, sizeof(fstype)))
91 if (strcmp(ondisk->vol_label, label))
99 fstyp_hammer(FILE *fp, char *label, size_t size)
101 hammer_volume_ondisk_t ondisk;
104 ondisk = read_ondisk(fp);
105 if (ondisk->vol_no != HAMMER_ROOT_VOLNO)
107 if (ondisk->vol_count != 1)
109 if (test_ondisk(ondisk))
112 strlcpy(label, ondisk->vol_label, size);
120 test_volume(const char *volpath)
122 hammer_volume_ondisk_t ondisk;
126 if ((fp = fopen(volpath, "r")) == NULL)
127 err(1, "failed to open %s", volpath);
129 ondisk = read_ondisk(fp);
131 if (test_ondisk(ondisk))
134 volno = ondisk->vol_no;
141 __fsvtyp_hammer(const char *blkdevs, char *label, size_t size, int partial)
143 hammer_volume_ondisk_t ondisk = NULL;
145 char *dup, *p, *volpath, x[HAMMER_MAX_VOLUMES];
146 int i, volno, error = 1;
151 memset(x, 0, sizeof(x));
152 dup = strdup(blkdevs);
159 if ((p = strchr(p, ':')) != NULL)
161 if ((volno = test_volume(volpath)) == -1)
164 assert(volno < HAMMER_MAX_VOLUMES);
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);
182 for (i = 0; i < HAMMER_MAX_VOLUMES; i++)
185 for (i = 0; i < HAMMER_MAX_VOLUMES; i++)
188 if (ondisk->vol_count != i)
190 for (; i < HAMMER_MAX_VOLUMES; i++)
194 strlcpy(label, ondisk->vol_label, size);
202 fsvtyp_hammer(const char *blkdevs, char *label, size_t size)
204 return (__fsvtyp_hammer(blkdevs, label, size, 0));
208 fsvtyp_hammer_partial(const char *blkdevs, char *label, size_t size)
210 return (__fsvtyp_hammer(blkdevs, label, size, 1));