1*636c04dbSrin /* $NetBSD: subr_disklabel.c,v 1.3 2019/04/07 02:58:02 rin Exp $ */
29a23e406Schristos
39a23e406Schristos /*
49a23e406Schristos * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
59a23e406Schristos * All rights reserved.
69a23e406Schristos *
79a23e406Schristos * Redistribution and use in source and binary forms, with or without
89a23e406Schristos * modification, are permitted provided that the following conditions
99a23e406Schristos * are met:
109a23e406Schristos * 1. Redistributions of source code must retain the above copyright
119a23e406Schristos * notice, this list of conditions and the following disclaimer.
129a23e406Schristos * 2. Redistributions in binary form must reproduce the above copyright
139a23e406Schristos * notice, this list of conditions and the following disclaimer in the
149a23e406Schristos * documentation and/or other materials provided with the distribution.
159a23e406Schristos * 3. Neither the name of the University nor the names of its contributors
169a23e406Schristos * may be used to endorse or promote products derived from this software
179a23e406Schristos * without specific prior written permission.
189a23e406Schristos *
199a23e406Schristos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209a23e406Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219a23e406Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229a23e406Schristos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239a23e406Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249a23e406Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259a23e406Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269a23e406Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279a23e406Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289a23e406Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299a23e406Schristos * SUCH DAMAGE.
309a23e406Schristos *
319a23e406Schristos * @(#)ufs_disksubr.c 7.16 (Berkeley) 5/4/91
329a23e406Schristos */
339a23e406Schristos
349a23e406Schristos #include <sys/cdefs.h>
35*636c04dbSrin __KERNEL_RCSID(0, "$NetBSD: subr_disklabel.c,v 1.3 2019/04/07 02:58:02 rin Exp $");
369a23e406Schristos
379a23e406Schristos #include <sys/param.h>
389a23e406Schristos #include <sys/systm.h>
399a23e406Schristos #include <sys/disklabel.h>
409a23e406Schristos
41*636c04dbSrin #if !defined(__HAVE_SETDISKLABEL) || defined(_RUMPKERNEL)
429a23e406Schristos
439a23e406Schristos #ifdef DEBUG
449a23e406Schristos #define DPRINTF(a, ...) printf(a, ##__VA_ARGS__)
459a23e406Schristos #else
469a23e406Schristos #define DPRINTF(a, ...) __nothing
479a23e406Schristos #endif
489a23e406Schristos
499a23e406Schristos /*
509a23e406Schristos * Check new disk label for sensibility
519a23e406Schristos * before setting it.
529a23e406Schristos */
539a23e406Schristos int
setdisklabel(struct disklabel * olp,struct disklabel * nlp,u_long openmask,struct cpu_disklabel * osdep)549a23e406Schristos setdisklabel(struct disklabel *olp, struct disklabel *nlp, u_long openmask,
559a23e406Schristos struct cpu_disklabel *osdep)
569a23e406Schristos {
579a23e406Schristos int i;
589a23e406Schristos struct partition *opp, *npp;
599a23e406Schristos
609a23e406Schristos /* sanity clause */
619a23e406Schristos if (nlp->d_secpercyl == 0 || nlp->d_secsize == 0
629a23e406Schristos || (nlp->d_secsize % DEV_BSIZE) != 0) {
639a23e406Schristos DPRINTF("%s: secpercyl/secsize %u/%u\n", __func__,
649a23e406Schristos nlp->d_secpercyl, nlp->d_secsize);
659a23e406Schristos return EINVAL;
669a23e406Schristos }
679a23e406Schristos
689a23e406Schristos /* special case to allow disklabel to be invalidated */
699a23e406Schristos if (nlp->d_magic == 0xffffffff) {
709a23e406Schristos *olp = *nlp;
719a23e406Schristos return 0;
729a23e406Schristos }
739a23e406Schristos
749a23e406Schristos if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC ||
759a23e406Schristos nlp->d_npartitions > MAXPARTITIONS || dkcksum(nlp) != 0) {
769a23e406Schristos DPRINTF("%s: bad magic %#x/%#x != %#x, partitions %u != %u"
779a23e406Schristos ", bad sum=%#x\n", __func__,
789a23e406Schristos nlp->d_magic, nlp->d_magic2, DISKMAGIC,
799a23e406Schristos nlp->d_npartitions, MAXPARTITIONS, dkcksum(nlp));
809a23e406Schristos return EINVAL;
819a23e406Schristos }
829a23e406Schristos
839a23e406Schristos while (openmask != 0) {
849a23e406Schristos i = ffs(openmask) - 1;
859a23e406Schristos openmask &= ~(1 << i);
869a23e406Schristos if (i >= nlp->d_npartitions) {
879a23e406Schristos DPRINTF("%s: partition not found\n", __func__);
889a23e406Schristos return EBUSY;
899a23e406Schristos }
909a23e406Schristos opp = &olp->d_partitions[i];
919a23e406Schristos npp = &nlp->d_partitions[i];
929a23e406Schristos /*
939a23e406Schristos * Copy internally-set partition information
949a23e406Schristos * if new label doesn't include it. XXX
959a23e406Schristos */
969a23e406Schristos if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) {
979a23e406Schristos *npp = *opp;
989a23e406Schristos continue;
999a23e406Schristos }
1009a23e406Schristos if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
1019a23e406Schristos {
1029a23e406Schristos DPRINTF("%s: mismatched offset/size", __func__);
1039a23e406Schristos return EBUSY;
1049a23e406Schristos }
1059a23e406Schristos }
1069a23e406Schristos nlp->d_checksum = 0;
1079a23e406Schristos nlp->d_checksum = dkcksum(nlp);
1089a23e406Schristos *olp = *nlp;
1099a23e406Schristos return 0;
1109a23e406Schristos }
1119a23e406Schristos #endif
112