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