1 /* $NetBSD: subr_disklabel.c,v 1.5 2026/01/04 03:16:53 riastradh Exp $ */ 2 3 /* 4 * Copyright (c) 1982, 1986, 1988 Regents of the University of California. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * @(#)ufs_disksubr.c 7.16 (Berkeley) 5/4/91 32 */ 33 34 #include <sys/cdefs.h> 35 __KERNEL_RCSID(0, "$NetBSD: subr_disklabel.c,v 1.5 2026/01/04 03:16:53 riastradh Exp $"); 36 37 #include <sys/param.h> 38 #include <sys/types.h> 39 40 #include <sys/disklabel.h> 41 #include <sys/sdt.h> 42 #include <sys/systm.h> 43 44 #if !defined(__HAVE_SETDISKLABEL) || defined(_RUMPKERNEL) 45 46 #ifdef DEBUG 47 #define DPRINTF(a, ...) printf(a, ##__VA_ARGS__) 48 #else 49 #define DPRINTF(a, ...) __nothing 50 #endif 51 52 /* 53 * Check new disk label for sensibility 54 * before setting it. 55 */ 56 int 57 setdisklabel(struct disklabel *olp, struct disklabel *nlp, u_long openmask, 58 struct cpu_disklabel *osdep) 59 { 60 int i; 61 struct partition *opp, *npp; 62 63 /* sanity clause */ 64 if (nlp->d_secpercyl == 0 || nlp->d_secsize == 0 65 || (nlp->d_secsize % DEV_BSIZE) != 0) { 66 DPRINTF("%s: secpercyl/secsize %u/%u\n", __func__, 67 nlp->d_secpercyl, nlp->d_secsize); 68 return SET_ERROR(EINVAL); 69 } 70 71 /* special case to allow disklabel to be invalidated */ 72 if (nlp->d_magic == 0xffffffff) { 73 *olp = *nlp; 74 return 0; 75 } 76 77 if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC || 78 nlp->d_npartitions > MAXPARTITIONS || dkcksum(nlp) != 0) { 79 DPRINTF("%s: bad magic %#x/%#x != %#x, partitions %u != %u" 80 ", bad sum=%#x\n", __func__, 81 nlp->d_magic, nlp->d_magic2, DISKMAGIC, 82 nlp->d_npartitions, MAXPARTITIONS, dkcksum(nlp)); 83 return SET_ERROR(EINVAL); 84 } 85 86 while (openmask != 0) { 87 i = ffs(openmask) - 1; 88 openmask &= ~(1 << i); 89 if (i >= nlp->d_npartitions) { 90 DPRINTF("%s: partition not found\n", __func__); 91 return SET_ERROR(EBUSY); 92 } 93 opp = &olp->d_partitions[i]; 94 npp = &nlp->d_partitions[i]; 95 /* 96 * Copy internally-set partition information 97 * if new label doesn't include it. XXX 98 */ 99 if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) { 100 *npp = *opp; 101 continue; 102 } 103 if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size) 104 { 105 DPRINTF("%s: mismatched offset/size", __func__); 106 return SET_ERROR(EBUSY); 107 } 108 } 109 nlp->d_checksum = 0; 110 nlp->d_checksum = dkcksum(nlp); 111 *olp = *nlp; 112 return 0; 113 } 114 #endif 115