disksubr.c revision 1.8
1/* $NetBSD: disksubr.c,v 1.8 1996/02/02 18:07:47 mycroft 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/disklabel.h> 42#include <sys/syslog.h> 43 44#define b_cylin b_resid 45 46#ifdef COMPAT_ULTRIX 47#include "../../stand/dec_boot.h" 48 49extern char * 50compat_label __P((dev_t dev, void (*strat)(), 51 struct disklabel *lp, struct cpu_disklabel *osdep)); 52 53#endif 54 55 56/* 57 * Attempt to read a disk label from a device 58 * using the indicated stategy routine. 59 * The label must be partly set up before this: 60 * secpercyl and anything required in the strategy routine 61 * (e.g., sector size) must be filled in before calling us. 62 * Returns null on success and an error string on failure. 63 */ 64char * 65readdisklabel(dev, strat, lp, osdep) 66 dev_t dev; 67 void (*strat)(); 68 register struct disklabel *lp; 69 struct cpu_disklabel *osdep; 70{ 71 register struct buf *bp; 72 struct disklabel *dlp; 73 char *msg = NULL; 74 75 if (lp->d_secperunit == 0) 76 lp->d_secperunit = 0x1fffffff; 77 lp->d_npartitions = 1; 78 if (lp->d_partitions[0].p_size == 0) 79 lp->d_partitions[0].p_size = 0x1fffffff; 80 lp->d_partitions[0].p_offset = 0; 81 82 bp = geteblk((int)lp->d_secsize); 83 bp->b_dev = dev; 84 bp->b_blkno = LABELSECTOR; 85 bp->b_bcount = lp->d_secsize; 86 bp->b_flags = B_BUSY | B_READ; 87 bp->b_cylin = LABELSECTOR / lp->d_secpercyl; 88 (*strat)(bp); 89 if (biowait(bp)) { 90 msg = "I/O error"; 91 } else for (dlp = (struct disklabel *)bp->b_un.b_addr; 92 dlp <= (struct disklabel *)(bp->b_un.b_addr+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 bp->b_flags = B_INVAL | B_AGE; 107 brelse(bp); 108 return (msg); 109} 110 111#ifdef COMPAT_ULTRIX 112/* 113 * Given a buffer bp, try and interpret it as an Ultrix disk label, 114 * putting the partition info into a native NetBSD label 115 */ 116char * 117compat_label(dev, strat, lp, osdep) 118 dev_t dev; 119 void (*strat)(); 120 register struct disklabel *lp; 121 struct cpu_disklabel *osdep; 122{ 123 Dec_DiskLabel *dlp; 124 struct buf *bp = NULL; 125 char *msg = NULL; 126 127 bp = geteblk((int)lp->d_secsize); 128 bp->b_dev = dev; 129 bp->b_blkno = DEC_LABEL_SECTOR; 130 bp->b_bcount = lp->d_secsize; 131 bp->b_flags = B_BUSY | B_READ; 132 bp->b_cylin = DEC_LABEL_SECTOR / lp->d_secpercyl; 133 (*strat)(bp); 134 135 if (biowait(bp)) { 136 msg = "I/O error"; 137 goto done; 138 } 139 140 for (dlp = (Dec_DiskLabel *)bp->b_un.b_addr; 141 dlp <= (Dec_DiskLabel *)(bp->b_un.b_addr+DEV_BSIZE-sizeof(*dlp)); 142 dlp = (Dec_DiskLabel *)((char *)dlp + sizeof(long))) { 143 144 int part; 145 146 if (dlp->magic != DEC_LABEL_MAGIC) { 147 printf("label: %x\n",dlp->magic); 148 msg = ((msg != NULL) ? msg: "no disk label"); 149 goto done; 150 } 151 152#ifdef DIAGNOSTIC 153/*XXX*/ printf("Interpreting Ultrix label\n"); 154#endif 155 156 lp->d_magic = DEC_LABEL_MAGIC; 157 lp->d_npartitions = 0; 158 for (part = 0; 159 part <((MAXPARTITIONS<DEC_NUM_DISK_PARTS) ? 160 MAXPARTITIONS : DEC_NUM_DISK_PARTS); 161 part++) { 162 lp->d_partitions[part].p_size = dlp->map[part].numBlocks; 163 lp->d_partitions[part].p_offset = dlp->map[part].startBlock; 164 lp->d_partitions[part].p_fsize = 1024; 165 lp->d_partitions[part].p_fstype = 166 (part==1) ? FS_SWAP : FS_BSDFFS; 167 lp->d_npartitions += 1; 168 169#ifdef DIAGNOSTIC 170 printf(" Ultrix label rz%d%c: start %d len %d\n", 171 DISKUNIT(dev), "abcdefgh"[part], 172 lp->d_partitions[part].p_offset, 173 lp->d_partitions[part].p_size); 174#endif 175 } 176 break; 177 } 178 179done: 180 bp->b_flags = B_INVAL | B_AGE; 181 brelse(bp); 182 return (msg); 183} 184#endif /* COMPAT_ULTRIX */ 185 186 187/* 188 * Check new disk label for sensibility 189 * before setting it. 190 */ 191setdisklabel(olp, nlp, openmask, osdep) 192 register struct disklabel *olp, *nlp; 193 u_long openmask; 194 struct cpu_disklabel *osdep; 195{ 196 register i; 197 register struct partition *opp, *npp; 198 199 if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC || 200 dkcksum(nlp) != 0) 201 return (EINVAL); 202 while ((i = ffs((long)openmask)) != 0) { 203 i--; 204 openmask &= ~(1 << i); 205 if (nlp->d_npartitions <= i) 206 return (EBUSY); 207 opp = &olp->d_partitions[i]; 208 npp = &nlp->d_partitions[i]; 209 if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size) 210 return (EBUSY); 211 /* 212 * Copy internally-set partition information 213 * if new label doesn't include it. XXX 214 */ 215 if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) { 216 npp->p_fstype = opp->p_fstype; 217 npp->p_fsize = opp->p_fsize; 218 npp->p_frag = opp->p_frag; 219 npp->p_cpg = opp->p_cpg; 220 } 221 } 222 nlp->d_checksum = 0; 223 nlp->d_checksum = dkcksum(nlp); 224 *olp = *nlp; 225 return (0); 226} 227 228/* encoding of disk minor numbers, should be elsewhere... */ 229#define dkunit(dev) (minor(dev) >> 3) 230#define dkpart(dev) (minor(dev) & 07) 231#define dkminor(unit, part) (((unit) << 3) | (part)) 232 233/* 234 * Write disk label back to device after modification. 235 */ 236writedisklabel(dev, strat, lp, osdep) 237 dev_t dev; 238 void (*strat)(); 239 register struct disklabel *lp; 240 struct cpu_disklabel *osdep; 241{ 242 struct buf *bp; 243 struct disklabel *dlp; 244 int labelpart; 245 int error = 0; 246 247 labelpart = dkpart(dev); 248 if (lp->d_partitions[labelpart].p_offset != 0) { 249 if (lp->d_partitions[0].p_offset != 0) 250 return (EXDEV); /* not quite right */ 251 labelpart = 0; 252 } 253 bp = geteblk((int)lp->d_secsize); 254 bp->b_dev = makedev(major(dev), dkminor(dkunit(dev), labelpart)); 255 bp->b_blkno = LABELSECTOR; 256 bp->b_bcount = lp->d_secsize; 257 bp->b_flags = B_READ; 258 (*strat)(bp); 259 if (error = biowait(bp)) 260 goto done; 261 for (dlp = (struct disklabel *)bp->b_un.b_addr; 262 dlp <= (struct disklabel *) 263 (bp->b_un.b_addr + lp->d_secsize - sizeof(*dlp)); 264 dlp = (struct disklabel *)((char *)dlp + sizeof(long))) { 265 if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC && 266 dkcksum(dlp) == 0) { 267 *dlp = *lp; 268 bp->b_flags = B_WRITE; 269 (*strat)(bp); 270 error = biowait(bp); 271 goto done; 272 } 273 } 274 error = ESRCH; 275done: 276 brelse(bp); 277 return (error); 278} 279 280 281/* 282 * was this the boot device ? 283 */ 284int 285dk_establish(dk, dev) 286 struct disk *dk; 287 struct device *dev; 288{ 289 /* see also arch/alpha/alpha/disksubr.c */ 290 printf("Warning: boot path unknown\n"); 291 return 1; 292} 293 294/* 295 * UNTESTED !! 296 * 297 * Determine the size of the transfer, and make sure it is 298 * within the boundaries of the partition. Adjust transfer 299 * if needed, and signal errors or early completion. 300 */ 301int 302bounds_check_with_label(bp, lp, wlabel) 303 struct buf *bp; 304 struct disklabel *lp; 305 int wlabel; 306{ 307 308 struct partition *p = lp->d_partitions + dkpart(bp->b_dev); 309 int labelsect = lp->d_partitions[0].p_offset; 310 int maxsz = p->p_size; 311 int sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT; 312 313 /* overwriting disk label ? */ 314 /* XXX should also protect bootstrap in first 8K */ 315 if (bp->b_blkno + p->p_offset <= LABELSECTOR + labelsect && 316 (bp->b_flags & B_READ) == 0 && wlabel == 0) { 317 bp->b_error = EROFS; 318 goto bad; 319 } 320 321 /* beyond partition? */ 322 if (bp->b_blkno < 0 || bp->b_blkno + sz > maxsz) { 323 /* if exactly at end of disk, return an EOF */ 324 if (bp->b_blkno == maxsz) { 325 bp->b_resid = bp->b_bcount; 326 return(0); 327 } 328 /* or truncate if part of it fits */ 329 sz = maxsz - bp->b_blkno; 330 if (sz <= 0) { 331 bp->b_error = EINVAL; 332 goto bad; 333 } 334 bp->b_bcount = sz << DEV_BSHIFT; 335 } 336 337 /* calculate cylinder for disksort to order transfers with */ 338 bp->b_resid = (bp->b_blkno + p->p_offset) / lp->d_secpercyl; 339 return(1); 340bad: 341 bp->b_flags |= B_ERROR; 342 return(-1); 343} 344