1 /* $NetBSD: disksubr.c,v 1.1 2024/01/02 07:41:02 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 1982, 1986, 1988, 1993 5 * The Regents of the University of California. All rights reserved. 6 * (c) UNIX System Laboratories, Inc. 7 * All or some portions of this file are derived from material licensed 8 * to the University of California by American Telephone and Telegraph 9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 10 * the permission of UNIX System Laboratories, Inc. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)ufs_disksubr.c 8.5 (Berkeley) 1/21/94 37 */ 38 39 #include <sys/cdefs.h> 40 __KERNEL_RCSID(0, "$NetBSD: disksubr.c,v 1.1 2024/01/02 07:41:02 thorpej Exp $"); 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/buf.h> 45 #include <sys/disk.h> 46 #include <sys/disklabel.h> 47 48 /* 49 * Attempt to read a disk label from a device using the indicated strategy 50 * routine. The label must be partly set up before this: secpercyl and 51 * anything required in the strategy routine (e.g., sector size) must be 52 * filled in before calling us. Returns null on success and an error 53 * string on failure. 54 */ 55 const char * 56 readdisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp, 57 struct cpu_disklabel *osdep) 58 { 59 struct buf *bp; 60 struct disklabel *dlp; 61 const char *msg = NULL; 62 int i; 63 64 if (lp->d_secsize == 0) 65 lp->d_secsize = DEV_BSIZE; 66 if (lp->d_secperunit == 0) 67 lp->d_secperunit = 0x1fffffff; 68 if (lp->d_npartitions < RAW_PART + 1) 69 lp->d_npartitions = RAW_PART + 1; 70 for (i = 0; i < RAW_PART; i++) { 71 lp->d_partitions[i].p_size = 0; 72 lp->d_partitions[i].p_offset = 0; 73 } 74 if (lp->d_partitions[RAW_PART].p_size == 0) 75 lp->d_partitions[RAW_PART].p_size = lp->d_secperunit; 76 lp->d_partitions[RAW_PART].p_offset = 0; 77 78 lp->d_partitions[0].p_size = lp->d_partitions[RAW_PART].p_size; 79 lp->d_partitions[0].p_fstype = FS_BSDFFS; 80 81 bp = geteblk((int)lp->d_secsize); 82 bp->b_dev = dev; 83 bp->b_blkno = LABELSECTOR; 84 bp->b_bcount = lp->d_secsize; 85 bp->b_flags |= B_READ; 86 bp->b_cylinder = LABELSECTOR / lp->d_secpercyl; 87 (*strat)(bp); 88 if (biowait(bp)) 89 msg = "I/O error"; 90 else for (dlp = (struct disklabel *)bp->b_data; 91 dlp <= (struct disklabel *)((char *)bp->b_data + 92 DEV_BSIZE - sizeof(*dlp)); 93 dlp = (struct disklabel *)((char *)dlp + sizeof(long))) { 94 if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) { 95 if (msg == NULL) 96 msg = "no disk label"; 97 } else if (dlp->d_npartitions > MAXPARTITIONS || 98 dkcksum(dlp) != 0) 99 msg = "disk label corrupted"; 100 else { 101 *lp = *dlp; 102 msg = NULL; 103 break; 104 } 105 } 106 brelse(bp, 0); 107 return msg; 108 } 109 110 /* 111 * Write disk label back to device after modification. 112 */ 113 int 114 writedisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp, 115 struct cpu_disklabel *osdep) 116 { 117 struct buf *bp; 118 struct disklabel *dlp; 119 int labelpart; 120 int error = 0; 121 122 labelpart = DISKPART(dev); 123 if (lp->d_partitions[labelpart].p_offset != 0) { 124 if (lp->d_partitions[0].p_offset != 0) 125 return EXDEV; /* not quite right */ 126 labelpart = 0; 127 } 128 bp = geteblk((int)lp->d_secsize); 129 bp->b_dev = MAKEDISKDEV(major(dev), DISKUNIT(dev), labelpart); 130 bp->b_blkno = LABELSECTOR; 131 bp->b_bcount = lp->d_secsize; 132 bp->b_flags |= B_READ; 133 (*strat)(bp); 134 if ((error = biowait(bp)) != 0) 135 goto done; 136 for (dlp = (struct disklabel *)bp->b_data; 137 dlp <= (struct disklabel *) 138 ((char *)bp->b_data + lp->d_secsize - sizeof(*dlp)); 139 dlp = (struct disklabel *)((char *)dlp + sizeof(long))) { 140 if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC && 141 dkcksum(dlp) == 0) { 142 *dlp = *lp; 143 bp->b_oflags &= ~(BO_DONE); 144 bp->b_flags &= ~(B_READ); 145 bp->b_flags |= B_WRITE; 146 (*strat)(bp); 147 error = biowait(bp); 148 goto done; 149 } 150 } 151 error = ESRCH; 152 done: 153 brelse(bp, 0); 154 return error; 155 } 156