1 /*- 2 * Copyright (c) 2017-2019 The DragonFly Project 3 * All rights reserved. 4 * 5 * This software was developed by Edward Tomasz Napierala under sponsorship 6 * from the FreeBSD Foundation. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 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. 16 * 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 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <string.h> 36 #include <err.h> 37 38 #include <sys/types.h> 39 40 #include "hammer2_disk.h" 41 42 #include "fstyp.h" 43 44 static hammer2_volume_data_t* 45 __read_voldata(FILE *fp) 46 { 47 hammer2_volume_data_t *voldata; 48 49 voldata = read_buf(fp, 0, sizeof(*voldata)); 50 if (voldata == NULL) 51 err(1, "failed to read volume data"); 52 53 return (voldata); 54 } 55 56 static int 57 __test_voldata(const hammer2_volume_data_t *voldata) 58 { 59 if (voldata->magic != HAMMER2_VOLUME_ID_HBO && 60 voldata->magic != HAMMER2_VOLUME_ID_ABO) 61 return (1); 62 63 return (0); 64 } 65 66 static int 67 __read_label(FILE *fp, char *label, size_t size) 68 { 69 hammer2_blockref_t broot, best, *bref; 70 hammer2_media_data_t *vols[HAMMER2_NUM_VOLHDRS], *media; 71 hammer2_off_t io_off, io_base; 72 size_t bytes, io_bytes, boff; 73 int i, best_i, error = 0; 74 75 best_i = -1; 76 memset(&best, 0, sizeof(best)); 77 78 for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) { 79 memset(&broot, 0, sizeof(broot)); 80 broot.type = HAMMER2_BREF_TYPE_VOLUME; 81 broot.data_off = (i * HAMMER2_ZONE_BYTES64) | HAMMER2_PBUFRADIX; 82 vols[i] = read_buf(fp, broot.data_off & ~HAMMER2_OFF_MASK_RADIX, 83 sizeof(*vols[i])); 84 broot.mirror_tid = vols[i]->voldata.mirror_tid; 85 if (best_i < 0 || best.mirror_tid < broot.mirror_tid) { 86 best_i = i; 87 best = broot; 88 } 89 } 90 91 bref = &vols[best_i]->voldata.sroot_blockset.blockref[0]; 92 if (bref->type != HAMMER2_BREF_TYPE_INODE) { 93 warnx("Superroot blockref type is not inode"); 94 error = 2; 95 goto done; 96 } 97 98 bytes = bref->data_off & HAMMER2_OFF_MASK_RADIX; 99 if (bytes) 100 bytes = (size_t)1 << bytes; 101 if (bytes != sizeof(hammer2_inode_data_t)) { 102 warnx("Superroot blockref size does not match inode size"); 103 error = 3; 104 goto done; 105 } 106 107 io_off = bref->data_off & ~HAMMER2_OFF_MASK_RADIX; 108 io_base = io_off & ~(hammer2_off_t)(HAMMER2_MINIOSIZE - 1); 109 boff = io_off - io_base; 110 111 io_bytes = HAMMER2_MINIOSIZE; 112 while (io_bytes + boff < bytes) 113 io_bytes <<= 1; 114 if (io_bytes > sizeof(*media)) { 115 warnx("Invalid I/O bytes"); 116 error = 4; 117 goto done; 118 } 119 120 media = read_buf(fp, io_base, io_bytes); 121 if (boff) 122 memcpy(media, (char*)media + boff, bytes); 123 124 strlcpy(label, (char*)media->ipdata.filename, size); 125 free(media); 126 done: 127 for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) 128 free(vols[i]); 129 130 return (error); 131 } 132 133 int 134 fstyp_hammer2(FILE *fp, char *label, size_t size) 135 { 136 hammer2_volume_data_t *voldata; 137 int error = 1; 138 139 voldata = __read_voldata(fp); 140 if (__test_voldata(voldata)) 141 goto done; 142 143 error = __read_label(fp, label, size); 144 done: 145 free(voldata); 146 return (error); 147 } 148