2 * Copyright (c) 2016 The DragonFly Project
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <vfs/hammer/hammer_disk.h>
39 static hammer_volume_ondisk_t
40 __read_ondisk(FILE *fp
)
42 hammer_volume_ondisk_t ondisk
;
44 ondisk
= read_buf(fp
, 0, sizeof(*ondisk
));
46 err(1, "failed to read ondisk");
52 __test_ondisk(const hammer_volume_ondisk_t ondisk
)
55 static uuid_t fsid
, fstype
;
56 static char label
[64];
58 if (ondisk
->vol_signature
!= HAMMER_FSBUF_VOLUME
&&
59 ondisk
->vol_signature
!= HAMMER_FSBUF_VOLUME_REV
)
61 if (ondisk
->vol_rootvol
!= HAMMER_ROOT_VOLNO
)
63 if (ondisk
->vol_no
< 0 || ondisk
->vol_no
> HAMMER_MAX_VOLUMES
- 1)
65 if (ondisk
->vol_count
< 1 || ondisk
->vol_count
> HAMMER_MAX_VOLUMES
)
69 count
= ondisk
->vol_count
;
71 memcpy(&fsid
, &ondisk
->vol_fsid
, sizeof(fsid
));
72 memcpy(&fstype
, &ondisk
->vol_fstype
, sizeof(fstype
));
73 strncpy(label
, ondisk
->vol_label
, sizeof(label
));
75 if (ondisk
->vol_count
!= count
)
77 if (memcmp(&ondisk
->vol_fsid
, &fsid
, sizeof(fsid
)))
79 if (memcmp(&ondisk
->vol_fstype
, &fstype
, sizeof(fstype
)))
81 if (strncmp(ondisk
->vol_label
, label
, sizeof(label
)))
89 fstyp_hammer(FILE *fp
, char *label
, size_t size
)
91 hammer_volume_ondisk_t ondisk
;
94 ondisk
= __read_ondisk(fp
);
95 if (ondisk
->vol_no
!= HAMMER_ROOT_VOLNO
)
97 if (ondisk
->vol_count
!= 1)
99 if (__test_ondisk(ondisk
))
102 strlcpy(label
, ondisk
->vol_label
, size
);
110 __test_volume(const char *volpath
)
112 hammer_volume_ondisk_t ondisk
;
116 if ((fp
= fopen(volpath
, "r")) == NULL
)
117 err(1, "failed to open %s", volpath
);
119 ondisk
= __read_ondisk(fp
);
121 if (__test_ondisk(ondisk
))
124 volno
= ondisk
->vol_no
;
131 __fsvtyp_hammer(const char *blkdevs
, char *label
, size_t size
, int partial
)
133 hammer_volume_ondisk_t ondisk
;
135 char *dup
, *p
, *volpath
, x
[HAMMER_MAX_VOLUMES
];
136 int i
, volno
, error
= 1;
138 memset(x
, 0, sizeof(x
));
139 dup
= strdup(blkdevs
);
144 if ((p
= strchr(p
, ':')) != NULL
)
146 if ((volno
= __test_volume(volpath
)) == -1)
151 if ((fp
= fopen(volpath
, "r")) == NULL
)
152 err(1, "failed to open %s", volpath
);
153 ondisk
= __read_ondisk(fp
);
163 for (i
= 0; i
< HAMMER_MAX_VOLUMES
; i
++)
166 for (i
= 0; i
< HAMMER_MAX_VOLUMES
; i
++)
169 if (ondisk
->vol_count
!= i
)
171 for (; i
< HAMMER_MAX_VOLUMES
; i
++)
175 strlcpy(label
, ondisk
->vol_label
, size
);
183 fsvtyp_hammer(const char *blkdevs
, char *label
, size_t size
)
185 return (__fsvtyp_hammer(blkdevs
, label
, size
, 0));
189 fsvtyp_hammer_partial(const char *blkdevs
, char *label
, size_t size
)
191 return (__fsvtyp_hammer(blkdevs
, label
, size
, 1));