disksubr.c revision 1.28
1/* $NetBSD: disksubr.c,v 1.28 2000/03/03 17:51:26 mhitch 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#include <ufs/ufs/dinode.h> /* XXX for fs.h */ 49#include <ufs/ffs/fs.h> /* XXX for BBSIZE & SBSIZE */ 50 51char *compat_label __P((dev_t dev, void (*strat) __P((struct buf *bp)), 52 struct disklabel *lp, struct cpu_disklabel *osdep)); /* XXX */ 53 54#endif 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) __P((struct buf *bp)); 68 struct disklabel *lp; 69 struct cpu_disklabel *osdep; 70{ 71 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_cylinder = 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#ifdef COMPAT_ULTRIX 109 /* 110 * If no NetBSD label was found, check for an Ultrix label and 111 * construct tne incore label from the Ultrix partition information. 112 */ 113 if (msg != NULL) { 114 msg = compat_label(dev, strat, lp, osdep); 115 if (msg == NULL) { 116 printf("WARNING: using Ultrix partition information\n"); 117 /* set geometry? */ 118 } 119 } 120#endif 121/* XXX If no NetBSD label or Ultrix label found, generate default label here */ 122 return (msg); 123} 124 125#ifdef COMPAT_ULTRIX 126/* 127 * Given a buffer bp, try and interpret it as an Ultrix disk label, 128 * putting the partition info into a native NetBSD label 129 */ 130char * 131compat_label(dev, strat, lp, osdep) 132 dev_t dev; 133 void (*strat) __P((struct buf *bp)); 134 struct disklabel *lp; 135 struct cpu_disklabel *osdep; 136{ 137 dec_disklabel *dlp; 138 struct buf *bp = NULL; 139 char *msg = NULL; 140 141 bp = geteblk((int)lp->d_secsize); 142 bp->b_dev = dev; 143 bp->b_blkno = DEC_LABEL_SECTOR; 144 bp->b_bcount = lp->d_secsize; 145 bp->b_flags = B_BUSY | B_READ; 146 bp->b_cylinder = DEC_LABEL_SECTOR / lp->d_secpercyl; 147 (*strat)(bp); 148 149 if (biowait(bp)) { 150 msg = "I/O error"; 151 goto done; 152 } 153 154 for (dlp = (dec_disklabel *)bp->b_un.b_addr; 155 dlp <= (dec_disklabel *)(bp->b_un.b_addr+DEV_BSIZE-sizeof(*dlp)); 156 dlp = (dec_disklabel *)((char *)dlp + sizeof(long))) { 157 158 int part; 159 160 if (dlp->magic != DEC_LABEL_MAGIC) { 161 printf("label: %x\n",dlp->magic); 162 msg = ((msg != NULL) ? msg: "no disk label"); 163 goto done; 164 } 165 166#ifdef DIAGNOSTIC 167/*XXX*/ printf("Interpreting Ultrix label\n"); 168#endif 169 170 lp->d_magic = DEC_LABEL_MAGIC; 171 lp->d_npartitions = 0; 172 strncpy(lp->d_packname, "Ultrix label", 16); 173 lp->d_rpm = 3600; 174 lp->d_interleave = 1; 175 lp->d_flags = 0; 176 lp->d_bbsize = BBSIZE; 177 lp->d_sbsize = SBSIZE; 178 for (part = 0; 179 part <((MAXPARTITIONS<DEC_NUM_DISK_PARTS) ? 180 MAXPARTITIONS : DEC_NUM_DISK_PARTS); 181 part++) { 182 lp->d_partitions[part].p_size = dlp->map[part].num_blocks; 183 lp->d_partitions[part].p_offset = dlp->map[part].start_block; 184 lp->d_partitions[part].p_fsize = 1024; 185 lp->d_partitions[part].p_fstype = 186 (part==1) ? FS_SWAP : FS_BSDFFS; 187 lp->d_npartitions += 1; 188 189#ifdef DIAGNOSTIC 190 printf(" Ultrix label rz%d%c: start %d len %d\n", 191 DISKUNIT(dev), "abcdefgh"[part], 192 lp->d_partitions[part].p_offset, 193 lp->d_partitions[part].p_size); 194#endif 195 } 196 break; 197 } 198 199done: 200 bp->b_flags = B_INVAL | B_AGE; 201 brelse(bp); 202 return (msg); 203} 204#endif /* COMPAT_ULTRIX */ 205 206 207/* 208 * Check new disk label for sensibility 209 * before setting it. 210 */ 211int 212setdisklabel(olp, nlp, openmask, osdep) 213 struct disklabel *olp, *nlp; 214 u_long openmask; 215 struct cpu_disklabel *osdep; 216{ 217 int i; 218 struct partition *opp, *npp; 219 220 if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC || 221 dkcksum(nlp) != 0) 222 return (EINVAL); 223 while ((i = ffs((long)openmask)) != 0) { 224 i--; 225 openmask &= ~(1 << i); 226 if (nlp->d_npartitions <= i) 227 return (EBUSY); 228 opp = &olp->d_partitions[i]; 229 npp = &nlp->d_partitions[i]; 230 if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size) 231 return (EBUSY); 232 /* 233 * Copy internally-set partition information 234 * if new label doesn't include it. XXX 235 */ 236 if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) { 237 npp->p_fstype = opp->p_fstype; 238 npp->p_fsize = opp->p_fsize; 239 npp->p_frag = opp->p_frag; 240 npp->p_cpg = opp->p_cpg; 241 } 242 } 243 nlp->d_checksum = 0; 244 nlp->d_checksum = dkcksum(nlp); 245 *olp = *nlp; 246 return (0); 247} 248 249/* encoding of disk minor numbers, should be elsewhere... */ 250#define dkunit(dev) (minor(dev) >> 3) 251#define dkpart(dev) (minor(dev) & 07) 252#define dkminor(unit, part) (((unit) << 3) | (part)) 253 254/* 255 * Write disk label back to device after modification. 256 */ 257int 258writedisklabel(dev, strat, lp, osdep) 259 dev_t dev; 260 void (*strat) __P((struct buf *bp)); 261 struct disklabel *lp; 262 struct cpu_disklabel *osdep; 263{ 264 struct buf *bp; 265 struct disklabel *dlp; 266 int labelpart; 267 int error = 0; 268 269 labelpart = dkpart(dev); 270 if (lp->d_partitions[labelpart].p_offset != 0) { 271 if (lp->d_partitions[0].p_offset != 0) 272 return (EXDEV); /* not quite right */ 273 labelpart = 0; 274 } 275 bp = geteblk((int)lp->d_secsize); 276 bp->b_dev = makedev(major(dev), dkminor(dkunit(dev), labelpart)); 277 bp->b_blkno = LABELSECTOR; 278 bp->b_bcount = lp->d_secsize; 279 bp->b_flags = B_READ; 280 (*strat)(bp); 281 if ((error = biowait(bp)) != 0) 282 goto done; 283 for (dlp = (struct disklabel *)bp->b_un.b_addr; 284 dlp <= (struct disklabel *) 285 (bp->b_un.b_addr + lp->d_secsize - sizeof(*dlp)); 286 dlp = (struct disklabel *)((char *)dlp + sizeof(long))) { 287 if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC && 288 dkcksum(dlp) == 0) { 289 *dlp = *lp; 290 bp->b_flags = B_WRITE; 291 (*strat)(bp); 292 error = biowait(bp); 293 goto done; 294 } 295 } 296 error = ESRCH; 297done: 298 brelse(bp); 299 return (error); 300} 301 302/* 303 * UNTESTED !! 304 * 305 * Determine the size of the transfer, and make sure it is 306 * within the boundaries of the partition. Adjust transfer 307 * if needed, and signal errors or early completion. 308 */ 309int 310bounds_check_with_label(bp, lp, wlabel) 311 struct buf *bp; 312 struct disklabel *lp; 313 int wlabel; 314{ 315 316 struct partition *p = lp->d_partitions + dkpart(bp->b_dev); 317 int labelsect = lp->d_partitions[RAW_PART].p_offset; 318 int maxsz = p->p_size; 319 int sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT; 320 321 /* overwriting disk label ? */ 322 /* XXX should also protect bootstrap in first 8K */ 323 if (bp->b_blkno + p->p_offset <= LABELSECTOR + labelsect && 324 (bp->b_flags & B_READ) == 0 && wlabel == 0) { 325 bp->b_error = EROFS; 326 goto bad; 327 } 328 329 /* beyond partition? */ 330 if (bp->b_blkno < 0 || bp->b_blkno + sz > maxsz) { 331 /* if exactly at end of disk, return an EOF */ 332 if (bp->b_blkno == maxsz) { 333 bp->b_resid = bp->b_bcount; 334 return(0); 335 } 336 /* or truncate if part of it fits */ 337 sz = maxsz - bp->b_blkno; 338 if (sz <= 0) { 339 bp->b_error = EINVAL; 340 goto bad; 341 } 342 bp->b_bcount = sz << DEV_BSHIFT; 343 } 344 345 /* calculate cylinder for disksort to order transfers with */ 346 bp->b_resid = (bp->b_blkno + p->p_offset) / lp->d_secpercyl; 347 return(1); 348bad: 349 bp->b_flags |= B_ERROR; 350 return(-1); 351} 352