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