disksubr.c revision 1.26
1/* $NetBSD: disksubr.c,v 1.26 2000/01/18 19:48:02 thorpej 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/disk.h> 44#include <sys/disklabel.h> 45 46#ifdef COMPAT_ULTRIX 47#include <machine/dec_boot.h> 48 49char *compat_label __P((dev_t dev, void (*strat) __P((struct buf *bp)), 50 struct disklabel *lp, struct cpu_disklabel *osdep)); /* XXX */ 51 52#endif 53 54/* 55 * Attempt to read a disk label from a device 56 * using the indicated stategy routine. 57 * The label must be partly set up before this: 58 * secpercyl and anything required in the strategy routine 59 * (e.g., sector size) must be filled in before calling us. 60 * Returns null on success and an error string on failure. 61 */ 62char * 63readdisklabel(dev, strat, lp, osdep) 64 dev_t dev; 65 void (*strat) __P((struct buf *bp)); 66 struct disklabel *lp; 67 struct cpu_disklabel *osdep; 68{ 69 struct buf *bp; 70 struct disklabel *dlp; 71 char *msg = NULL; 72 73 if (lp->d_secperunit == 0) 74 lp->d_secperunit = 0x1fffffff; 75 lp->d_npartitions = 1; 76 if (lp->d_partitions[0].p_size == 0) 77 lp->d_partitions[0].p_size = 0x1fffffff; 78 lp->d_partitions[0].p_offset = 0; 79 80 bp = geteblk((int)lp->d_secsize); 81 bp->b_dev = dev; 82 bp->b_blkno = LABELSECTOR; 83 bp->b_bcount = lp->d_secsize; 84 bp->b_flags = B_BUSY | B_READ; 85 bp->b_cylinder = LABELSECTOR / lp->d_secpercyl; 86 (*strat)(bp); 87 if (biowait(bp)) { 88 msg = "I/O error"; 89 } else for (dlp = (struct disklabel *)bp->b_un.b_addr; 90 dlp <= (struct disklabel *)(bp->b_un.b_addr+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 bp->b_flags = B_INVAL | B_AGE; 105 brelse(bp); 106 return (msg); 107} 108 109#ifdef COMPAT_ULTRIX 110/* 111 * Given a buffer bp, try and interpret it as an Ultrix disk label, 112 * putting the partition info into a native NetBSD label 113 */ 114char * 115compat_label(dev, strat, lp, osdep) 116 dev_t dev; 117 void (*strat) __P((struct buf *bp)); 118 struct disklabel *lp; 119 struct cpu_disklabel *osdep; 120{ 121 dec_disklabel *dlp; 122 struct buf *bp = NULL; 123 char *msg = NULL; 124 125 bp = geteblk((int)lp->d_secsize); 126 bp->b_dev = dev; 127 bp->b_blkno = DEC_LABEL_SECTOR; 128 bp->b_bcount = lp->d_secsize; 129 bp->b_flags = B_BUSY | B_READ; 130 bp->b_cylinder = DEC_LABEL_SECTOR / lp->d_secpercyl; 131 (*strat)(bp); 132 133 if (biowait(bp)) { 134 msg = "I/O error"; 135 goto done; 136 } 137 138 for (dlp = (dec_disklabel *)bp->b_un.b_addr; 139 dlp <= (dec_disklabel *)(bp->b_un.b_addr+DEV_BSIZE-sizeof(*dlp)); 140 dlp = (dec_disklabel *)((char *)dlp + sizeof(long))) { 141 142 int part; 143 144 if (dlp->magic != DEC_LABEL_MAGIC) { 145 printf("label: %x\n",dlp->magic); 146 msg = ((msg != NULL) ? msg: "no disk label"); 147 goto done; 148 } 149 150#ifdef DIAGNOSTIC 151/*XXX*/ printf("Interpreting Ultrix label\n"); 152#endif 153 154 lp->d_magic = DEC_LABEL_MAGIC; 155 lp->d_npartitions = 0; 156 for (part = 0; 157 part <((MAXPARTITIONS<DEC_NUM_DISK_PARTS) ? 158 MAXPARTITIONS : DEC_NUM_DISK_PARTS); 159 part++) { 160 lp->d_partitions[part].p_size = dlp->map[part].num_blocks; 161 lp->d_partitions[part].p_offset = dlp->map[part].start_block; 162 lp->d_partitions[part].p_fsize = 1024; 163 lp->d_partitions[part].p_fstype = 164 (part==1) ? FS_SWAP : FS_BSDFFS; 165 lp->d_npartitions += 1; 166 167#ifdef DIAGNOSTIC 168 printf(" Ultrix label rz%d%c: start %d len %d\n", 169 DISKUNIT(dev), "abcdefgh"[part], 170 lp->d_partitions[part].p_offset, 171 lp->d_partitions[part].p_size); 172#endif 173 } 174 break; 175 } 176 177done: 178 bp->b_flags = B_INVAL | B_AGE; 179 brelse(bp); 180 return (msg); 181} 182#endif /* COMPAT_ULTRIX */ 183 184 185/* 186 * Check new disk label for sensibility 187 * before setting it. 188 */ 189int 190setdisklabel(olp, nlp, openmask, osdep) 191 struct disklabel *olp, *nlp; 192 u_long openmask; 193 struct cpu_disklabel *osdep; 194{ 195 int i; 196 struct partition *opp, *npp; 197 198 if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC || 199 dkcksum(nlp) != 0) 200 return (EINVAL); 201 while ((i = ffs((long)openmask)) != 0) { 202 i--; 203 openmask &= ~(1 << i); 204 if (nlp->d_npartitions <= i) 205 return (EBUSY); 206 opp = &olp->d_partitions[i]; 207 npp = &nlp->d_partitions[i]; 208 if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size) 209 return (EBUSY); 210 /* 211 * Copy internally-set partition information 212 * if new label doesn't include it. XXX 213 */ 214 if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) { 215 npp->p_fstype = opp->p_fstype; 216 npp->p_fsize = opp->p_fsize; 217 npp->p_frag = opp->p_frag; 218 npp->p_cpg = opp->p_cpg; 219 } 220 } 221 nlp->d_checksum = 0; 222 nlp->d_checksum = dkcksum(nlp); 223 *olp = *nlp; 224 return (0); 225} 226 227/* encoding of disk minor numbers, should be elsewhere... */ 228#define dkunit(dev) (minor(dev) >> 3) 229#define dkpart(dev) (minor(dev) & 07) 230#define dkminor(unit, part) (((unit) << 3) | (part)) 231 232/* 233 * Write disk label back to device after modification. 234 */ 235int 236writedisklabel(dev, strat, lp, osdep) 237 dev_t dev; 238 void (*strat) __P((struct buf *bp)); 239 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)) != 0) 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 */ 284void 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; 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[RAW_PART].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