disksubr.c revision 1.13
1/* $NetBSD: disksubr.c,v 1.13 1996/10/13 03:39:49 christos 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. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)ufs_disksubr.c 7.16 (Berkeley) 5/4/91 36 */ 37 38#include <sys/param.h> 39#include <sys/systm.h> 40#include <sys/buf.h> 41#include <sys/device.h> 42#include <sys/disk.h> 43#include <sys/disklabel.h> 44#include <sys/syslog.h> 45 46#define b_cylin b_resid 47 48#ifdef COMPAT_ULTRIX 49#include "../../stand/dec_boot.h" 50 51extern char * 52compat_label __P((dev_t dev, void (*strat) __P((struct buf *bp)), 53 struct disklabel *lp, struct cpu_disklabel *osdep)); 54 55#endif 56 57char* readdisklabel __P((dev_t dev, void (*strat) __P((struct buf *bp)), 58 register struct disklabel *lp, 59 struct cpu_disklabel *osdep)); 60 61/* 62 * Attempt to read a disk label from a device 63 * using the indicated stategy routine. 64 * The label must be partly set up before this: 65 * secpercyl and anything required in the strategy routine 66 * (e.g., sector size) must be filled in before calling us. 67 * Returns null on success and an error string on failure. 68 */ 69char * 70readdisklabel(dev, strat, lp, osdep) 71 dev_t dev; 72 void (*strat) __P((struct buf *bp)); 73 register struct disklabel *lp; 74 struct cpu_disklabel *osdep; 75{ 76 register struct buf *bp; 77 struct disklabel *dlp; 78 char *msg = NULL; 79 80 if (lp->d_secperunit == 0) 81 lp->d_secperunit = 0x1fffffff; 82 lp->d_npartitions = 1; 83 if (lp->d_partitions[0].p_size == 0) 84 lp->d_partitions[0].p_size = 0x1fffffff; 85 lp->d_partitions[0].p_offset = 0; 86 87 bp = geteblk((int)lp->d_secsize); 88 bp->b_dev = dev; 89 bp->b_blkno = LABELSECTOR; 90 bp->b_bcount = lp->d_secsize; 91 bp->b_flags = B_BUSY | B_READ; 92 bp->b_cylin = LABELSECTOR / lp->d_secpercyl; 93 (*strat)(bp); 94 if (biowait(bp)) { 95 msg = "I/O error"; 96 } else for (dlp = (struct disklabel *)bp->b_un.b_addr; 97 dlp <= (struct disklabel *)(bp->b_un.b_addr+DEV_BSIZE-sizeof(*dlp)); 98 dlp = (struct disklabel *)((char *)dlp + sizeof(long))) { 99 if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) { 100 if (msg == NULL) 101 msg = "no disk label"; 102 } else if (dlp->d_npartitions > MAXPARTITIONS || 103 dkcksum(dlp) != 0) 104 msg = "disk label corrupted"; 105 else { 106 *lp = *dlp; 107 msg = NULL; 108 break; 109 } 110 } 111 bp->b_flags = B_INVAL | B_AGE; 112 brelse(bp); 113 return (msg); 114} 115 116#ifdef COMPAT_ULTRIX 117/* 118 * Given a buffer bp, try and interpret it as an Ultrix disk label, 119 * putting the partition info into a native NetBSD label 120 */ 121char * 122compat_label(dev, strat, lp, osdep) 123 dev_t dev; 124 void (*strat) __P((struct buf *bp)); 125 register struct disklabel *lp; 126 struct cpu_disklabel *osdep; 127{ 128 Dec_DiskLabel *dlp; 129 struct buf *bp = NULL; 130 char *msg = NULL; 131 132 bp = geteblk((int)lp->d_secsize); 133 bp->b_dev = dev; 134 bp->b_blkno = DEC_LABEL_SECTOR; 135 bp->b_bcount = lp->d_secsize; 136 bp->b_flags = B_BUSY | B_READ; 137 bp->b_cylin = DEC_LABEL_SECTOR / lp->d_secpercyl; 138 (*strat)(bp); 139 140 if (biowait(bp)) { 141 msg = "I/O error"; 142 goto done; 143 } 144 145 for (dlp = (Dec_DiskLabel *)bp->b_un.b_addr; 146 dlp <= (Dec_DiskLabel *)(bp->b_un.b_addr+DEV_BSIZE-sizeof(*dlp)); 147 dlp = (Dec_DiskLabel *)((char *)dlp + sizeof(long))) { 148 149 int part; 150 151 if (dlp->magic != DEC_LABEL_MAGIC) { 152 printf("label: %x\n",dlp->magic); 153 msg = ((msg != NULL) ? msg: "no disk label"); 154 goto done; 155 } 156 157#ifdef DIAGNOSTIC 158/*XXX*/ printf("Interpreting Ultrix label\n"); 159#endif 160 161 lp->d_magic = DEC_LABEL_MAGIC; 162 lp->d_npartitions = 0; 163 for (part = 0; 164 part <((MAXPARTITIONS<DEC_NUM_DISK_PARTS) ? 165 MAXPARTITIONS : DEC_NUM_DISK_PARTS); 166 part++) { 167 lp->d_partitions[part].p_size = dlp->map[part].numBlocks; 168 lp->d_partitions[part].p_offset = dlp->map[part].startBlock; 169 lp->d_partitions[part].p_fsize = 1024; 170 lp->d_partitions[part].p_fstype = 171 (part==1) ? FS_SWAP : FS_BSDFFS; 172 lp->d_npartitions += 1; 173 174#ifdef DIAGNOSTIC 175 printf(" Ultrix label rz%d%c: start %d len %d\n", 176 DISKUNIT(dev), "abcdefgh"[part], 177 lp->d_partitions[part].p_offset, 178 lp->d_partitions[part].p_size); 179#endif 180 } 181 break; 182 } 183 184done: 185 bp->b_flags = B_INVAL | B_AGE; 186 brelse(bp); 187 return (msg); 188} 189#endif /* COMPAT_ULTRIX */ 190 191 192/* 193 * Check new disk label for sensibility 194 * before setting it. 195 */ 196int 197setdisklabel(olp, nlp, openmask, osdep) 198 register struct disklabel *olp, *nlp; 199 u_long openmask; 200 struct cpu_disklabel *osdep; 201{ 202 register i; 203 register struct partition *opp, *npp; 204 205 if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC || 206 dkcksum(nlp) != 0) 207 return (EINVAL); 208 while ((i = ffs((long)openmask)) != 0) { 209 i--; 210 openmask &= ~(1 << i); 211 if (nlp->d_npartitions <= i) 212 return (EBUSY); 213 opp = &olp->d_partitions[i]; 214 npp = &nlp->d_partitions[i]; 215 if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size) 216 return (EBUSY); 217 /* 218 * Copy internally-set partition information 219 * if new label doesn't include it. XXX 220 */ 221 if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) { 222 npp->p_fstype = opp->p_fstype; 223 npp->p_fsize = opp->p_fsize; 224 npp->p_frag = opp->p_frag; 225 npp->p_cpg = opp->p_cpg; 226 } 227 } 228 nlp->d_checksum = 0; 229 nlp->d_checksum = dkcksum(nlp); 230 *olp = *nlp; 231 return (0); 232} 233 234/* encoding of disk minor numbers, should be elsewhere... */ 235#define dkunit(dev) (minor(dev) >> 3) 236#define dkpart(dev) (minor(dev) & 07) 237#define dkminor(unit, part) (((unit) << 3) | (part)) 238 239/* 240 * Write disk label back to device after modification. 241 */ 242int 243writedisklabel(dev, strat, lp, osdep) 244 dev_t dev; 245 void (*strat) __P((struct buf *bp)); 246 register struct disklabel *lp; 247 struct cpu_disklabel *osdep; 248{ 249 struct buf *bp; 250 struct disklabel *dlp; 251 int labelpart; 252 int error = 0; 253 254 labelpart = dkpart(dev); 255 if (lp->d_partitions[labelpart].p_offset != 0) { 256 if (lp->d_partitions[0].p_offset != 0) 257 return (EXDEV); /* not quite right */ 258 labelpart = 0; 259 } 260 bp = geteblk((int)lp->d_secsize); 261 bp->b_dev = makedev(major(dev), dkminor(dkunit(dev), labelpart)); 262 bp->b_blkno = LABELSECTOR; 263 bp->b_bcount = lp->d_secsize; 264 bp->b_flags = B_READ; 265 (*strat)(bp); 266 if ((error = biowait(bp)) != 0) 267 goto done; 268 for (dlp = (struct disklabel *)bp->b_un.b_addr; 269 dlp <= (struct disklabel *) 270 (bp->b_un.b_addr + lp->d_secsize - sizeof(*dlp)); 271 dlp = (struct disklabel *)((char *)dlp + sizeof(long))) { 272 if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC && 273 dkcksum(dlp) == 0) { 274 *dlp = *lp; 275 bp->b_flags = B_WRITE; 276 (*strat)(bp); 277 error = biowait(bp); 278 goto done; 279 } 280 } 281 error = ESRCH; 282done: 283 brelse(bp); 284 return (error); 285} 286 287 288/* 289 * was this the boot device ? 290 */ 291void 292dk_establish(dk, dev) 293 struct disk *dk; 294 struct device *dev; 295{ 296 /* see also arch/alpha/alpha/disksubr.c */ 297 printf("Warning: boot path unknown\n"); 298 return; 299} 300 301/* 302 * UNTESTED !! 303 * 304 * Determine the size of the transfer, and make sure it is 305 * within the boundaries of the partition. Adjust transfer 306 * if needed, and signal errors or early completion. 307 */ 308int 309bounds_check_with_label(bp, lp, wlabel) 310 struct buf *bp; 311 struct disklabel *lp; 312 int wlabel; 313{ 314 315 struct partition *p = lp->d_partitions + dkpart(bp->b_dev); 316 int labelsect = lp->d_partitions[0].p_offset; 317 int maxsz = p->p_size; 318 int sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT; 319 320 /* overwriting disk label ? */ 321 /* XXX should also protect bootstrap in first 8K */ 322 if (bp->b_blkno + p->p_offset <= LABELSECTOR + labelsect && 323 (bp->b_flags & B_READ) == 0 && wlabel == 0) { 324 bp->b_error = EROFS; 325 goto bad; 326 } 327 328 /* beyond partition? */ 329 if (bp->b_blkno < 0 || bp->b_blkno + sz > maxsz) { 330 /* if exactly at end of disk, return an EOF */ 331 if (bp->b_blkno == maxsz) { 332 bp->b_resid = bp->b_bcount; 333 return(0); 334 } 335 /* or truncate if part of it fits */ 336 sz = maxsz - bp->b_blkno; 337 if (sz <= 0) { 338 bp->b_error = EINVAL; 339 goto bad; 340 } 341 bp->b_bcount = sz << DEV_BSHIFT; 342 } 343 344 /* calculate cylinder for disksort to order transfers with */ 345 bp->b_resid = (bp->b_blkno + p->p_offset) / lp->d_secpercyl; 346 return(1); 347bad: 348 bp->b_flags |= B_ERROR; 349 return(-1); 350} 351