1 /* $NetBSD: disksubr.c,v 1.14 2002/03/15 05:55:37 gmcgarry 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. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the University of 23 * California, Berkeley and its contributors. 24 * 4. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * @(#)ufs_disksubr.c 8.5 (Berkeley) 1/21/94 41 */ 42 43 #include <sys/cdefs.h> 44 __KERNEL_RCSID(0, "$NetBSD: disksubr.c,v 1.14 2002/03/15 05:55:37 gmcgarry Exp $"); 45 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/buf.h> 49 #include <sys/disklabel.h> 50 #include <sys/syslog.h> 51 52 #define b_cylinder b_resid 53 54 /* 55 * Attempt to read a disk label from a device using the indicated strategy 56 * routine. The label must be partly set up before this: secpercyl and 57 * anything required in the strategy routine (e.g., sector size) must be 58 * filled in before calling us. Returns null on success and an error 59 * string on failure. 60 */ 61 char * 62 readdisklabel(dev, strat, lp, osdep) 63 dev_t dev; 64 void (*strat) __P((struct buf *)); 65 struct disklabel *lp; 66 struct cpu_disklabel *osdep; 67 { 68 struct buf *bp; 69 struct disklabel *dlp; 70 char *msg = NULL; 71 72 if (lp->d_secperunit == 0) 73 lp->d_secperunit = 0x1fffffff; 74 lp->d_npartitions = RAW_PART + 1; 75 if (lp->d_partitions[0].p_size == 0) 76 lp->d_partitions[0].p_size = 0x1fffffff; 77 lp->d_partitions[0].p_offset = 0; 78 79 bp = geteblk((int)lp->d_secsize); 80 bp->b_dev = dev; 81 bp->b_blkno = LABELSECTOR; 82 bp->b_bcount = lp->d_secsize; 83 bp->b_flags |= B_READ; 84 bp->b_cylinder = LABELSECTOR / lp->d_secpercyl; 85 (*strat)(bp); 86 if (biowait(bp)) 87 msg = "I/O error"; 88 else for (dlp = (struct disklabel *)bp->b_data; 89 dlp <= (struct disklabel *)((char *)bp->b_data + 90 DEV_BSIZE - sizeof(*dlp)); 91 dlp = (struct disklabel *)((char *)dlp + sizeof(long))) { 92 if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) { 93 if (msg == NULL) 94 msg = "no disk label"; 95 } else if (dlp->d_npartitions > MAXPARTITIONS || 96 dkcksum(dlp) != 0) 97 msg = "disk label corrupted"; 98 else { 99 *lp = *dlp; 100 msg = NULL; 101 break; 102 } 103 } 104 brelse(bp); 105 return (msg); 106 } 107 108 /* 109 * Check new disk label for sensibility before setting it. 110 */ 111 int 112 setdisklabel(olp, nlp, openmask, osdep) 113 struct disklabel *olp, *nlp; 114 u_long openmask; 115 struct cpu_disklabel *osdep; 116 { 117 int i; 118 struct partition *opp, *npp; 119 120 if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC || 121 dkcksum(nlp) != 0) 122 return (EINVAL); 123 while ((i = ffs(openmask)) != 0) { 124 i--; 125 openmask &= ~(1 << i); 126 if (nlp->d_npartitions <= i) 127 return (EBUSY); 128 opp = &olp->d_partitions[i]; 129 npp = &nlp->d_partitions[i]; 130 if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size) 131 return (EBUSY); 132 /* 133 * Copy internally-set partition information 134 * if new label doesn't include it. XXX 135 */ 136 if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) { 137 npp->p_fstype = opp->p_fstype; 138 npp->p_fsize = opp->p_fsize; 139 npp->p_frag = opp->p_frag; 140 npp->p_cpg = opp->p_cpg; 141 } 142 } 143 nlp->d_checksum = 0; 144 nlp->d_checksum = dkcksum(nlp); 145 *olp = *nlp; 146 return (0); 147 } 148 149 /* 150 * Write disk label back to device after modification. 151 */ 152 int 153 writedisklabel(dev, strat, lp, osdep) 154 dev_t dev; 155 void (*strat) __P((struct buf *)); 156 struct disklabel *lp; 157 struct cpu_disklabel *osdep; 158 { 159 struct buf *bp; 160 struct disklabel *dlp; 161 int labelpart; 162 int error = 0; 163 164 labelpart = DISKPART(dev); 165 if (lp->d_partitions[labelpart].p_offset != 0) { 166 if (lp->d_partitions[0].p_offset != 0) 167 return (EXDEV); /* not quite right */ 168 labelpart = 0; 169 } 170 bp = geteblk((int)lp->d_secsize); 171 bp->b_dev = MAKEDISKDEV(major(dev), DISKUNIT(dev), labelpart); 172 bp->b_blkno = LABELSECTOR; 173 bp->b_bcount = lp->d_secsize; 174 bp->b_flags |= B_READ; 175 (*strat)(bp); 176 if ((error = biowait(bp))) 177 goto done; 178 for (dlp = (struct disklabel *)bp->b_data; 179 dlp <= (struct disklabel *) 180 ((char *)bp->b_data + lp->d_secsize - sizeof(*dlp)); 181 dlp = (struct disklabel *)((char *)dlp + sizeof(long))) { 182 if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC && 183 dkcksum(dlp) == 0) { 184 *dlp = *lp; 185 bp->b_flags &= ~(B_READ|B_DONE); 186 bp->b_flags |= B_WRITE; 187 (*strat)(bp); 188 error = biowait(bp); 189 goto done; 190 } 191 } 192 error = ESRCH; 193 done: 194 brelse(bp); 195 return (error); 196 } 197 198 /* 199 * Determine the size of the transfer, and make sure it is 200 * within the boundaries of the partition. Adjust transfer 201 * if needed, and signal errors or early completion. 202 */ 203 int 204 bounds_check_with_label(bp, lp, wlabel) 205 struct buf *bp; 206 struct disklabel *lp; 207 int wlabel; 208 { 209 struct partition *p = &lp->d_partitions[DISKPART(bp->b_dev)]; 210 int labelsect = lp->d_partitions[0].p_offset; 211 int maxsz = p->p_size; 212 int sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT; 213 214 /* Overwriting disk label? */ 215 if (bp->b_blkno + p->p_offset <= LABELSECTOR + labelsect && 216 (bp->b_flags & B_READ) == 0 && !wlabel) { 217 bp->b_error = EROFS; 218 goto bad; 219 } 220 221 /* beyond partition? */ 222 if (bp->b_blkno < 0 || bp->b_blkno + sz > maxsz) { 223 if (bp->b_blkno == maxsz) { 224 /* If exactly at end of disk, return EOF. */ 225 bp->b_resid = bp->b_bcount; 226 return (0); 227 } 228 /* ...or truncate if part of it fits */ 229 sz = maxsz - bp->b_blkno; 230 if (sz <= 0) { 231 bp->b_error = EINVAL; 232 goto bad; 233 } 234 bp->b_bcount = sz << DEV_BSHIFT; 235 } 236 237 /* calculate cylinder for disksort to order transfers with */ 238 bp->b_resid = (bp->b_blkno + p->p_offset) / lp->d_secpercyl; 239 return (1); 240 241 bad: 242 bp->b_flags |= B_ERROR; 243 return (-1); 244 } 245