Skip to content

Commit

Permalink
fstyp: sync HAMMER1/2 detection support with DragonFly BSD.
Browse files Browse the repository at this point in the history
Submitted by:	Tomohiro Kusumi
PR:		243929
  • Loading branch information
pgiffuni committed Feb 6, 2020
1 parent ecafbba commit 9d529ed
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 60 deletions.
54 changes: 32 additions & 22 deletions usr.sbin/fstyp/hammer.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*-
* Copyright (c) 2016 The DragonFly Project
* Copyright (c) 2016-2019 The DragonFly Project
* Copyright (c) 2016-2019 Tomohiro Kusumi <[email protected]>
* All rights reserved.
*
* This software was developed by Edward Tomasz Napierala under sponsorship
Expand Down Expand Up @@ -43,7 +44,7 @@ __FBSDID("$FreeBSD$");
#include "fstyp.h"

static hammer_volume_ondisk_t
__read_ondisk(FILE *fp)
read_ondisk(FILE *fp)
{
hammer_volume_ondisk_t ondisk;

Expand All @@ -55,7 +56,7 @@ __read_ondisk(FILE *fp)
}

static int
__test_ondisk(const hammer_volume_ondisk_t ondisk)
test_ondisk(const hammer_volume_ondisk_t ondisk)
{
static int count = 0;
static hammer_uuid_t fsid, fstype;
Expand Down Expand Up @@ -97,23 +98,23 @@ fstyp_hammer(FILE *fp, char *label, size_t size)
hammer_volume_ondisk_t ondisk;
int error = 1;

ondisk = __read_ondisk(fp);
ondisk = read_ondisk(fp);
if (ondisk->vol_no != HAMMER_ROOT_VOLNO)
goto done;
goto fail;
if (ondisk->vol_count != 1)
goto done;
if (__test_ondisk(ondisk))
goto done;
goto fail;
if (test_ondisk(ondisk))
goto fail;

strlcpy(label, ondisk->vol_label, size);
error = 0;
done:
fail:
free(ondisk);
return (error);
}

static int
__test_volume(const char *volpath)
test_volume(const char *volpath)
{
hammer_volume_ondisk_t ondisk;
FILE *fp;
Expand All @@ -122,65 +123,74 @@ __test_volume(const char *volpath)
if ((fp = fopen(volpath, "r")) == NULL)
err(1, "failed to open %s", volpath);

ondisk = __read_ondisk(fp);
ondisk = read_ondisk(fp);
fclose(fp);
if (__test_ondisk(ondisk))
goto done;
if (test_ondisk(ondisk))
goto fail;

volno = ondisk->vol_no;
done:
fail:
free(ondisk);
return (volno);
}

static int
__fsvtyp_hammer(const char *blkdevs, char *label, size_t size, int partial)
{
hammer_volume_ondisk_t ondisk;
hammer_volume_ondisk_t ondisk = NULL;
FILE *fp;
char *dup, *p, *volpath, x[HAMMER_MAX_VOLUMES];
int i, volno, error = 1;

if (!blkdevs)
goto fail;

memset(x, 0, sizeof(x));
dup = strdup(blkdevs);
p = dup;

volpath = NULL;
volno = -1;
while (p) {
volpath = p;
if ((p = strchr(p, ':')) != NULL)
*p++ = '\0';
if ((volno = __test_volume(volpath)) == -1)
if ((volno = test_volume(volpath)) == -1)
break;
assert(volno >= 0);
assert(volno < HAMMER_MAX_VOLUMES);
x[volno]++;
}

if (!volpath)
err(1, "invalid path %s", blkdevs);
if ((fp = fopen(volpath, "r")) == NULL)
err(1, "failed to open %s", volpath);
ondisk = __read_ondisk(fp);
ondisk = read_ondisk(fp);
fclose(fp);

free(dup);

if (volno == -1)
goto done;
goto fail;
if (partial)
goto success;

for (i = 0; i < HAMMER_MAX_VOLUMES; i++)
if (x[i] > 1)
goto done;
goto fail;
for (i = 0; i < HAMMER_MAX_VOLUMES; i++)
if (x[i] == 0)
break;
if (ondisk->vol_count != i)
goto done;
goto fail;
for (; i < HAMMER_MAX_VOLUMES; i++)
if (x[i] != 0)
goto done;
goto fail;
success:
strlcpy(label, ondisk->vol_label, size);
error = 0;
done:
fail:
free(ondisk);
return (error);
}
Expand Down
Loading

0 comments on commit 9d529ed

Please sign in to comment.