xref: /dragonfly/sbin/hammer2/ondisk.c (revision 0982c5b8)
1 /*
2  * Copyright (c) 2020 Tomohiro Kusumi <tkusumi@netbsd.org>
3  * Copyright (c) 2020 The DragonFly Project
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to The DragonFly Project
7  * by Matthew Dillon <dillon@dragonflybsd.org>
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  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  * 3. Neither the name of The DragonFly Project nor the names of its
20  *    contributors may be used to endorse or promote products derived
21  *    from this software without specific, prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
27  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <unistd.h>
40 #include <fcntl.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <fstab.h>
45 #include <assert.h>
46 #include <errno.h>
47 #include <err.h>
48 
49 #include <vfs/hammer2/hammer2_disk.h>
50 
51 #include "hammer2_subs.h"
52 
53 static hammer2_ondisk_t fso;
54 static int hammer2_volumes_initialized;
55 
56 static void
57 hammer2_init_volume(hammer2_volume_t *vol)
58 {
59 	vol->fd = -1;
60 	vol->id = -1;
61 	vol->offset = (hammer2_off_t)-1;
62 	vol->size = (hammer2_off_t)-1;
63 }
64 
65 void
66 hammer2_init_ondisk(hammer2_ondisk_t *fsp)
67 {
68 	int i;
69 
70 	bzero(fsp, sizeof(*fsp));
71 	fsp->version = -1;
72 	for (i = 0; i < HAMMER2_MAX_VOLUMES; ++i)
73 		hammer2_init_volume(&fsp->volumes[i]);
74 }
75 
76 void
77 hammer2_install_volume(hammer2_volume_t *vol, int fd, int id, const char *path,
78 		       hammer2_off_t offset, hammer2_off_t size)
79 {
80 	bzero(vol, sizeof(*vol));
81 	vol->fd = fd;
82 	vol->id = id;
83 	vol->path = strdup(path);
84 	vol->offset = offset;
85 	vol->size = size;
86 }
87 
88 void
89 hammer2_uninstall_volume(hammer2_volume_t *vol)
90 {
91 	fsync(vol->fd);
92 	close(vol->fd);
93 	free(vol->path);
94 	hammer2_init_volume(vol);
95 }
96 
97 /*
98  * Locate a valid volume header.  If any of the four volume headers is good,
99  * we have a valid volume header and choose the best one based on mirror_tid.
100  */
101 static int
102 hammer2_read_volume_header(int fd, const char *path,
103 			   hammer2_volume_data_t *voldata)
104 {
105 	hammer2_volume_data_t vd;
106 	hammer2_tid_t mirror_tid = -1;
107 	hammer2_off_t size = check_volume(fd);
108 	hammer2_crc32_t crc0, crc1;
109 	const char *p;
110 	int i, zone = -1;
111 	ssize_t ret;
112 
113 	for (i = 0; i < HAMMER2_NUM_VOLHDRS; ++i) {
114 		if (i * HAMMER2_ZONE_BYTES64 >= size)
115 			break;
116 		if (lseek(fd, i * HAMMER2_ZONE_BYTES64, SEEK_SET) == -1)
117 			break;
118 		ret = read(fd, &vd, HAMMER2_PBUFSIZE);
119 		if (ret == -1) {
120 			fprintf(stderr, "%s #%d: read %s\n",
121 				path, i, strerror(errno));
122 			continue;
123 		}
124 		if (ret != HAMMER2_PBUFSIZE) {
125 			fprintf(stderr, "%s #%d: read %s\n",
126 				path, i, strerror(errno));
127 			continue;
128 		}
129 
130 		p = (const char*)&vd;
131 		/* verify volume header magic */
132 		if ((vd.magic != HAMMER2_VOLUME_ID_HBO) &&
133 		    (vd.magic != HAMMER2_VOLUME_ID_ABO))
134 		{
135 			fprintf(stderr, "%s #%d: bad magic\n", path, i);
136 			continue;
137 		}
138 
139 		if (vd.magic == HAMMER2_VOLUME_ID_ABO) {
140 			/* XXX: Reversed-endianness filesystem */
141 			fprintf(stderr,
142 				"%s #%d: reverse-endian filesystem detected",
143 				path, i);
144 			continue;
145 		}
146 
147 		/* verify volume header CRC's */
148 		crc0 = vd.icrc_sects[HAMMER2_VOL_ICRC_SECT0];
149 		crc1 = hammer2_icrc32(p + HAMMER2_VOLUME_ICRC0_OFF,
150 				      HAMMER2_VOLUME_ICRC0_SIZE);
151 		if (crc0 != crc1) {
152 			fprintf(stderr,
153 				"%s #%d: volume header crc mismatch "
154 				"sect0 %08x/%08x\n",
155 				path, i, crc0, crc1);
156 			continue;
157 		}
158 
159 		crc0 = vd.icrc_sects[HAMMER2_VOL_ICRC_SECT1];
160 		crc1 = hammer2_icrc32(p + HAMMER2_VOLUME_ICRC1_OFF,
161 				      HAMMER2_VOLUME_ICRC1_SIZE);
162 		if (crc0 != crc1) {
163 			fprintf(stderr,
164 				"%s #%d: volume header crc mismatch "
165 				"sect1 %08x/%08x",
166 				path, i, crc0, crc1);
167 			continue;
168 		}
169 
170 		crc0 = vd.icrc_volheader;
171 		crc1 = hammer2_icrc32(p + HAMMER2_VOLUME_ICRCVH_OFF,
172 				      HAMMER2_VOLUME_ICRCVH_SIZE);
173 		if (crc0 != crc1) {
174 			fprintf(stderr,
175 				"%s #%d: volume header crc mismatch "
176 				"vh %08x/%08x",
177 				path, i, crc0, crc1);
178 			continue;
179 		}
180 		if (zone == -1 || mirror_tid < vd.mirror_tid) {
181 			bcopy(&vd, voldata, sizeof(vd));
182 			mirror_tid = vd.mirror_tid;
183 			zone = i;
184 		}
185 	}
186 	return(zone);
187 }
188 
189 static void
190 hammer2_err_uuid_mismatch(uuid_t *uuid1, uuid_t *uuid2, const char *id)
191 {
192 	char *p1 = NULL, *p2 = NULL;
193 
194 	hammer2_uuid_to_str(uuid1, &p1);
195 	hammer2_uuid_to_str(uuid2, &p2);
196 
197 	errx(1, "%s uuid mismatch %s vs %s", id, p1, p2);
198 
199 	free(p1);
200 	free(p2);
201 }
202 
203 static void
204 hammer2_add_volume(const char *path, int rdonly)
205 {
206 	hammer2_volume_data_t voldata;
207 	hammer2_volume_t *vol;
208 	struct stat st;
209 	int fd, i;
210 
211 	fd = open(path, rdonly ? O_RDONLY : O_RDWR);
212 	if (fd == -1)
213 		err(1, "open");
214 
215 	if (fstat(fd, &st) == -1)
216 		err(1, "fstat");
217 	if (!S_ISCHR(st.st_mode) && !S_ISREG(st.st_mode))
218 		errx(1, "Unsupported file type");
219 
220 	if (hammer2_read_volume_header(fd, path, &voldata) >= 0) {
221 		i = voldata.volu_id;
222 		if (i < 0 || i >= HAMMER2_MAX_VOLUMES)
223 			errx(1, "%s has bad volume id %d", path, i);
224 		vol = &fso.volumes[i];
225 		if (vol->id != -1)
226 			errx(1, "%s already initialized", path);
227 		/* all headers must have the same version, nvolumes and uuid */
228 		if (!fso.nvolumes) {
229 			fso.version = voldata.version;
230 			fso.nvolumes = voldata.nvolumes;
231 			fso.fsid = voldata.fsid;
232 			fso.fstype = voldata.fstype;
233 		} else {
234 			if (fso.version != (int)voldata.version)
235 				errx(1, "Volume version mismatch %d vs %d",
236 				     fso.version, (int)voldata.version);
237 			if (fso.nvolumes != voldata.nvolumes)
238 				errx(1, "Volume count mismatch %d vs %d",
239 				     fso.nvolumes, voldata.nvolumes);
240 			if (!uuid_equal(&fso.fsid, &voldata.fsid, NULL))
241 				hammer2_err_uuid_mismatch(&fso.fsid,
242 							  &voldata.fsid,
243 							  "fsid");
244 			if (!uuid_equal(&fso.fstype, &voldata.fstype, NULL))
245 				hammer2_err_uuid_mismatch(&fso.fstype,
246 							  &voldata.fstype,
247 							  "fstype");
248 		}
249 		/* all per-volume tests passed */
250 		hammer2_install_volume(vol, fd, i, path,
251 				       voldata.volu_loff[i], voldata.volu_size);
252 		fso.total_size += vol->size;
253 	} else {
254 		errx(1, "No valid volume headers found!");
255 	}
256 }
257 
258 static void
259 hammer2_verify_volumes_common(const hammer2_ondisk_t *fsp)
260 {
261 	const hammer2_volume_t *vol;
262 	hammer2_off_t size;
263 	struct stat *st;
264 	const char *path;
265 	int i, j, nvolumes = 0;
266 
267 	if (fsp->version == -1)
268 		errx(1, "Bad volume version %d", fsp->version);
269 
270 	/* check initialized volume count */
271 	for (i = 0; i < HAMMER2_MAX_VOLUMES; ++i) {
272 		vol = &fsp->volumes[i];
273 		if (vol->id != -1)
274 			nvolumes++;
275 	}
276 
277 	/* fsp->nvolumes hasn't been verified yet, use nvolumes */
278 	st = calloc(nvolumes, sizeof(*st));
279 
280 	for (i = 0; i < HAMMER2_MAX_VOLUMES; ++i) {
281 		vol = &fsp->volumes[i];
282 		if (vol->id == -1)
283 			continue;
284 		path = vol->path;
285 		/* check volumes are unique */
286 		if (stat(path, &st[i]) != 0)
287 			errx(1, "Failed to stat %s", path);
288 		if (fstat(vol->fd, &st[i]) != 0)
289 			errx(1, "Failed to fstat %d", vol->fd);
290 		for (j = 0; j < i; ++j) {
291 			if ((st[i].st_ino == st[j].st_ino) &&
292 			    (st[i].st_dev == st[j].st_dev))
293 				errx(1, "%s specified more than once", path);
294 		}
295 		/* check volume fields are initialized */
296 		if (vol->fd == -1)
297 			errx(1, "%s has bad fd %d", path, vol->fd);
298 		if (vol->offset == (hammer2_off_t)-1)
299 			errx(1, "%s has bad offset 0x%016jx", path,
300 			     (intmax_t)vol->offset);
301 		if (vol->size == (hammer2_off_t)-1)
302 			errx(1, "%s has bad size 0x%016jx", path,
303 			     (intmax_t)vol->size);
304 		/* check volume size vs block device size */
305 		size = check_volume(vol->fd);
306 		printf("checkvolu header %d %016jx/%016jx\n", i, vol->size, size);
307 		if (vol->size > size)
308 			errx(1, "%s's size 0x%016jx exceeds device size 0x%016jx",
309 			     path, (intmax_t)vol->size, size);
310 	}
311 	free(st);
312 }
313 
314 static void
315 hammer2_verify_volumes_1(hammer2_ondisk_t *fsp,
316 			 const hammer2_volume_data_t *rootvoldata)
317 {
318 	const hammer2_volume_t *vol;
319 	hammer2_off_t off;
320 	const char *path;
321 	int i, nvolumes = 0;
322 
323 	/* check initialized volume count */
324 	for (i = 0; i < HAMMER2_MAX_VOLUMES; ++i) {
325 		vol = &fsp->volumes[i];
326 		if (vol->id != -1)
327 			nvolumes++;
328 	}
329 	if (nvolumes != 1)
330 		errx(1, "Only 1 volume supported");
331 	if (fsp->nvolumes)
332 		errx(1, "Volume count %d must be 0", fsp->nvolumes);
333 	fsp->nvolumes = nvolumes; /* adjust with actual count */
334 
335 	/* check volume header */
336 	if (rootvoldata) {
337 		if (rootvoldata->volu_id)
338 			errx(1, "Volume id %d must be 0", rootvoldata->volu_id);
339 		if (rootvoldata->nvolumes)
340 			errx(1, "Volume count %d must be 0",
341 			     rootvoldata->nvolumes);
342 		if (rootvoldata->total_size)
343 			errx(1, "Total size 0x%016jx must be 0",
344 			     (intmax_t)rootvoldata->total_size);
345 		for (i = 0; i < HAMMER2_MAX_VOLUMES; ++i) {
346 			off = rootvoldata->volu_loff[i];
347 			if (off)
348 				errx(1, "Volume offset[%d] 0x%016jx must be 0",
349 				     i, (intmax_t)off);
350 		}
351 	}
352 
353 	/* check volume */
354 	vol = &fsp->volumes[0];
355 	path = vol->path;
356 	if (vol->id)
357 		errx(1, "%s has non zero id %d", path, vol->id);
358 	if (vol->offset)
359 		errx(1, "%s has non zero offset 0x%016jx", path,
360 		     (intmax_t)vol->offset);
361 	if (vol->size & HAMMER2_VOLUME_ALIGNMASK64)
362 		errx(1, "%s's size is not 0x%016jx aligned", path,
363 		     (intmax_t)HAMMER2_VOLUME_ALIGN);
364 }
365 
366 static void
367 hammer2_verify_volumes_2(const hammer2_ondisk_t *fsp,
368 			 const hammer2_volume_data_t *rootvoldata)
369 {
370 	const hammer2_volume_t *vol;
371 	hammer2_off_t off;
372 	const char *path;
373 	int i, nvolumes = 0;
374 
375 	/* check initialized volume count */
376 	for (i = 0; i < HAMMER2_MAX_VOLUMES; ++i) {
377 		vol = &fsp->volumes[i];
378 		if (vol->id != -1)
379 			nvolumes++;
380 	}
381 	if (fsp->nvolumes != nvolumes)
382 		errx(1, "Volume count mismatch %d vs %d",
383 		     fsp->nvolumes, nvolumes);
384 
385 	/* check volume header */
386 	if (rootvoldata) {
387 		if (rootvoldata->volu_id != HAMMER2_ROOT_VOLUME)
388 			errx(1, "Volume id %d must be %d",
389 			     rootvoldata->volu_id, HAMMER2_ROOT_VOLUME);
390 		if (rootvoldata->nvolumes != fso.nvolumes)
391 			errx(1, "Volume header requires %d devices, %d specified",
392 			     rootvoldata->nvolumes, fso.nvolumes);
393 		if (rootvoldata->total_size != fso.total_size) {
394 			fprintf(stderr,
395 				"Total size 0x%016jx does not "
396 				"equal sum of volumes 0x%016jx",
397 			        rootvoldata->total_size, fso.total_size);
398 		}
399 		for (i = 0; i < nvolumes; ++i) {
400 			off = rootvoldata->volu_loff[i];
401 			if (off == (hammer2_off_t)-1)
402 				errx(1, "Volume offset[%d] 0x%016jx must not be -1",
403 				     i, (intmax_t)off);
404 		}
405 		for (i = nvolumes; i < HAMMER2_MAX_VOLUMES; ++i) {
406 			off = rootvoldata->volu_loff[i];
407 			if (off != (hammer2_off_t)-1)
408 				errx(1, "Volume offset[%d] 0x%016jx must be -1",
409 				     i, (intmax_t)off);
410 		}
411 	}
412 
413 	/* check volumes */
414 	for (i = 0; i < HAMMER2_MAX_VOLUMES; ++i) {
415 		vol = &fsp->volumes[i];
416 		if (vol->id == -1)
417 			continue;
418 		path = vol->path;
419 		/* check offset */
420 		if (vol->offset & HAMMER2_FREEMAP_LEVEL1_MASK)
421 			errx(1, "%s's offset 0x%016jx not 0x%016jx aligned",
422 			     path, (intmax_t)vol->offset,
423 			     HAMMER2_FREEMAP_LEVEL1_SIZE);
424 		/* check vs previous volume */
425 		if (i) {
426 			if (vol->id != (vol-1)->id + 1)
427 				errx(1, "%s has inconsistent id %d", path,
428 				     vol->id);
429 			if (vol->offset != (vol-1)->offset + (vol-1)->size)
430 				errx(1, "%s has inconsistent offset 0x%016jx",
431 				     path, (intmax_t)vol->offset);
432 		} else { /* first */
433 			if (vol->offset)
434 				errx(1, "%s has non zero offset 0x%016jx", path,
435 				     (intmax_t)vol->offset);
436 		}
437 		/* check size for non-last and last volumes */
438 		if (i != fsp->nvolumes - 1) {
439 			if (vol->size < HAMMER2_FREEMAP_LEVEL1_SIZE)
440 				errx(1, "%s's size must be >= 0x%016jx", path,
441 				     (intmax_t)HAMMER2_FREEMAP_LEVEL1_SIZE);
442 			if (vol->size & HAMMER2_FREEMAP_LEVEL1_MASK)
443 				errx(1, "%s's size is not 0x%016jx aligned",
444 				     path,
445 				     (intmax_t)HAMMER2_FREEMAP_LEVEL1_SIZE);
446 		} else { /* last */
447 			if (vol->size & HAMMER2_VOLUME_ALIGNMASK64)
448 				errx(1, "%s's size is not 0x%016jx aligned",
449 				     path, (intmax_t)HAMMER2_VOLUME_ALIGN);
450 		}
451 	}
452 }
453 
454 void
455 hammer2_verify_volumes(hammer2_ondisk_t *fsp,
456 		       const hammer2_volume_data_t *rootvoldata)
457 {
458 	hammer2_verify_volumes_common(fsp);
459 	if (fsp->version >= HAMMER2_VOL_VERSION_MULTI_VOLUMES)
460 		hammer2_verify_volumes_2(fsp, rootvoldata);
461 	else
462 		hammer2_verify_volumes_1(fsp, rootvoldata);
463 	assert(fsp->nvolumes > 0);
464 }
465 
466 void
467 hammer2_print_volumes(const hammer2_ondisk_t *fsp)
468 {
469 	const hammer2_volume_t *vol;
470 	int i, n, w = 0;
471 
472 	for (i = 0; i < fsp->nvolumes; ++i) {
473 		vol = &fsp->volumes[i];
474 		n = (int)strlen(vol->path);
475 		if (n > w)
476 			w = n;
477 	}
478 
479 	printf("total    %-*.*s 0x%016jx 0x%016jx\n",
480 		w, w, "", (intmax_t)0, (intmax_t)fsp->total_size);
481 
482 	for (i = 0; i < fsp->nvolumes; ++i) {
483 		vol = &fsp->volumes[i];
484 		printf("volume%-2d %-*.*s 0x%016jx 0x%016jx%s\n",
485 		       vol->id, w, w, vol->path, (intmax_t)vol->offset,
486 		       (intmax_t)vol->size,
487 		       (vol->id == HAMMER2_ROOT_VOLUME ?
488 		       " (root volume)" : ""));
489 	}
490 }
491 
492 void
493 hammer2_init_volumes(const char *blkdevs, int rdonly)
494 {
495 	hammer2_volume_data_t *rootvoldata;
496 	char *p, *devpath;
497 
498 	if (hammer2_volumes_initialized)
499 		errx(1, "Already initialized");
500 	if (!blkdevs)
501 		errx(1, "NULL blkdevs");
502 
503 	hammer2_init_ondisk(&fso);
504 	p = strdup(blkdevs);
505 	while ((devpath = p) != NULL) {
506 		if ((p = strchr(p, ':')) != NULL)
507 			*p++ = 0;
508 		devpath = getdevpath(devpath, 0);
509 		if (strchr(devpath, ':'))
510 			hammer2_init_volumes(devpath, rdonly);
511 		else
512 			hammer2_add_volume(devpath, rdonly);
513 		free(devpath);
514 	}
515 	free(p);
516 	hammer2_volumes_initialized = 1;
517 
518 	rootvoldata = hammer2_read_root_volume_header();
519 	hammer2_verify_volumes(&fso, rootvoldata);
520 	free(rootvoldata);
521 }
522 
523 void
524 hammer2_cleanup_volumes(void)
525 {
526 	hammer2_volume_t *vol;
527 	int i;
528 
529 	for (i = 0; i < HAMMER2_MAX_VOLUMES; ++i) {
530 		vol = &fso.volumes[i];
531 		if (vol->id == -1)
532 			continue;
533 		hammer2_uninstall_volume(vol);
534 	}
535 	hammer2_volumes_initialized = 0;
536 }
537 
538 typedef void (*callback)(const hammer2_volume_t*, void *data);
539 
540 static int
541 hammer2_get_volume_attr(hammer2_off_t offset, callback fn, void *data)
542 {
543 	hammer2_volume_t *vol;
544 	int i;
545 
546 	assert(hammer2_volumes_initialized == 1);
547 	offset &= ~HAMMER2_OFF_MASK_RADIX;
548 
549 	/* do binary search if users really use this many supported volumes */
550 	for (i = 0; i < fso.nvolumes; ++i) {
551 		vol = &fso.volumes[i];
552 		if ((offset >= vol->offset) &&
553 		    (offset < vol->offset + vol->size)) {
554 			fn(vol, data);
555 			return(0);
556 		}
557 	}
558 
559 	return(-1);
560 }
561 
562 /* fd */
563 static void
564 hammer2_volume_fd_cb(const hammer2_volume_t *vol, void *data)
565 {
566 	*(int*)data = vol->fd;
567 }
568 
569 int
570 hammer2_get_volume_fd(hammer2_off_t offset)
571 {
572 	int ret = 0;
573 
574 	if (hammer2_get_volume_attr(offset, hammer2_volume_fd_cb, &ret) < 0)
575 		return(-1);
576 	return(ret);
577 }
578 
579 int
580 hammer2_get_root_volume_fd(void)
581 {
582 	return(hammer2_get_volume_fd(0));
583 }
584 
585 /* id */
586 static void
587 hammer2_volume_id_cb(const hammer2_volume_t *vol, void *data)
588 {
589 	*(int*)data = vol->id;
590 }
591 
592 int
593 hammer2_get_volume_id(hammer2_off_t offset)
594 {
595 	int ret = 0;
596 
597 	if (hammer2_get_volume_attr(offset, hammer2_volume_id_cb, &ret) < 0)
598 		return(-1);
599 	return(ret);
600 }
601 
602 int
603 hammer2_get_root_volume_id(void)
604 {
605 	return(hammer2_get_volume_id(0));
606 }
607 
608 /* path */
609 static void
610 hammer2_volume_path_cb(const hammer2_volume_t *vol, void *data)
611 {
612 	*(const char**)data = vol->path;
613 }
614 
615 const char *
616 hammer2_get_volume_path(hammer2_off_t offset)
617 {
618 	const char *ret = NULL;
619 
620 	if (hammer2_get_volume_attr(offset, hammer2_volume_path_cb, &ret) < 0)
621 		return(NULL);
622 	return(ret);
623 }
624 
625 const char *
626 hammer2_get_root_volume_path(void)
627 {
628 	return(hammer2_get_volume_path(0));
629 }
630 
631 /* offset */
632 static void
633 hammer2_volume_offset_cb(const hammer2_volume_t *vol, void *data)
634 {
635 	*(hammer2_off_t*)data = vol->offset;
636 }
637 
638 hammer2_off_t
639 hammer2_get_volume_offset(hammer2_off_t offset)
640 {
641 	hammer2_off_t ret = 0;
642 
643 	if (hammer2_get_volume_attr(offset, hammer2_volume_offset_cb, &ret) < 0)
644 		return(-1);
645 	return(ret);
646 }
647 
648 hammer2_off_t
649 hammer2_get_root_volume_offset(void)
650 {
651 	return(hammer2_get_volume_offset(0));
652 }
653 
654 /* size */
655 static void
656 hammer2_volume_size_cb(const hammer2_volume_t *vol, void *data)
657 {
658 	*(hammer2_off_t*)data = vol->size;
659 }
660 
661 hammer2_off_t
662 hammer2_get_volume_size(hammer2_off_t offset)
663 {
664 	hammer2_off_t ret = 0;
665 
666 	if (hammer2_get_volume_attr(offset, hammer2_volume_size_cb, &ret) < 0)
667 		return(-1);
668 	return(ret);
669 }
670 
671 hammer2_off_t
672 hammer2_get_root_volume_size(void)
673 {
674 	return(hammer2_get_volume_size(0));
675 }
676 
677 /* total size */
678 hammer2_off_t
679 hammer2_get_total_size(void)
680 {
681 	return(fso.total_size);
682 }
683 
684 hammer2_volume_data_t*
685 hammer2_read_root_volume_header(void)
686 {
687 	hammer2_volume_data_t *voldata;
688 	int fd = hammer2_get_root_volume_fd();
689 	const char *path = hammer2_get_root_volume_path();
690 
691 	if (fd == -1)
692 		return(NULL);
693 
694 	voldata = calloc(1, sizeof(*voldata));
695 	if (hammer2_read_volume_header(fd, path, voldata) >= 0)
696 		return(voldata);
697 	else
698 		errx(1, "Failed to read volume header");
699 }
700