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