disksubr.c revision 1.53
1/* $NetBSD: disksubr.c,v 1.53 2009/07/20 17:05:13 tsutsui 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.53 2009/07/20 17:05:13 tsutsui Exp $"); 36 37#include <sys/param.h> 38#include <sys/systm.h> 39#include <sys/buf.h> 40#include <sys/disk.h> 41#include <sys/disklabel.h> 42 43#include <dev/dec/dec_boot.h> 44#include <ufs/ufs/dinode.h> /* XXX for fs.h */ 45#include <ufs/ffs/fs.h> /* XXX for BBSIZE & SBSIZE */ 46 47const char *compat_label(dev_t dev, void (*strat)(struct buf *bp), 48 struct disklabel *lp, struct cpu_disklabel *osdep); /* XXX */ 49 50/* 51 * Attempt to read a disk label from a device 52 * using the indicated strategy routine. 53 * The label must be partly set up before this: 54 * secpercyl and anything required in the strategy routine 55 * (e.g., sector size) must be filled in before calling us. 56 * Returns null on success and an error string on failure. 57 */ 58const char * 59readdisklabel(dev_t dev, void (*strat)(struct buf *bp), struct disklabel *lp, 60 struct cpu_disklabel *osdep) 61{ 62 struct buf *bp; 63 struct disklabel *dlp; 64 const char *msg = NULL; 65 66 if (lp->d_secperunit == 0) 67 lp->d_secperunit = 0x1fffffff; 68 lp->d_npartitions = 1; 69 if (lp->d_partitions[0].p_size == 0) 70 lp->d_partitions[0].p_size = 0x1fffffff; 71 lp->d_partitions[0].p_offset = 0; 72 73 bp = geteblk((int)lp->d_secsize); 74 bp->b_dev = dev; 75 bp->b_blkno = LABELSECTOR; 76 bp->b_bcount = lp->d_secsize; 77 bp->b_flags |= B_READ; 78 bp->b_cylinder = LABELSECTOR / lp->d_secpercyl; 79 (*strat)(bp); 80 if (biowait(bp)) { 81 msg = "I/O error"; 82 } else for (dlp = (struct disklabel *)bp->b_data; 83 dlp <= (struct disklabel *) 84 ((char *)bp->b_data + DEV_BSIZE - sizeof(*dlp)); 85 dlp = (struct disklabel *)((char *)dlp + sizeof(long))) { 86 if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) { 87 if (msg == NULL) 88 msg = "no disk label"; 89 } else if (dlp->d_npartitions > MAXPARTITIONS || 90 dkcksum(dlp) != 0) 91 msg = "disk label corrupted"; 92 else { 93 *lp = *dlp; 94 msg = NULL; 95 break; 96 } 97 } 98 brelse(bp, 0); 99 /* 100 * If no NetBSD label was found, check for an Ultrix label and 101 * construct tne incore label from the Ultrix partition information. 102 */ 103 if (msg != NULL) { 104 msg = compat_label(dev, strat, lp, osdep); 105 if (msg == NULL) { 106 printf("WARNING: using Ultrix partition information\n"); 107 /* set geometry? */ 108 } 109 } 110/* XXX If no NetBSD label or Ultrix label found, generate default label here */ 111 return msg; 112} 113 114/* 115 * Given a buffer bp, try and interpret it as an Ultrix disk label, 116 * putting the partition info into a native NetBSD label 117 */ 118const char * 119compat_label(dev_t dev, void (*strat)(struct buf *bp), struct disklabel *lp, 120 struct cpu_disklabel *osdep) 121{ 122 dec_disklabel *dlp; 123 struct buf *bp = NULL; 124 const char *msg = NULL; 125 126 bp = geteblk((int)lp->d_secsize); 127 bp->b_dev = dev; 128 bp->b_blkno = DEC_LABEL_SECTOR; 129 bp->b_bcount = lp->d_secsize; 130 bp->b_flags |= B_READ; 131 bp->b_cylinder = DEC_LABEL_SECTOR / lp->d_secpercyl; 132 (*strat)(bp); 133 134 if (biowait(bp)) { 135 msg = "I/O error"; 136 goto done; 137 } 138 139 for (dlp = (dec_disklabel *)bp->b_data; 140 dlp <= (dec_disklabel *) 141 ((char *)bp->b_data + DEV_BSIZE - sizeof(*dlp)); 142 dlp = (dec_disklabel *)((char *)dlp + sizeof(long))) { 143 144 int part; 145 146 if (dlp->magic != DEC_LABEL_MAGIC) { 147 printf("label: %x\n",dlp->magic); 148 msg = ((msg != NULL) ? msg: "no disk label"); 149 goto done; 150 } 151 152#ifdef DIAGNOSTIC 153/*XXX*/ printf("Interpreting Ultrix label\n"); 154#endif 155 156 lp->d_magic = DEC_LABEL_MAGIC; 157 lp->d_npartitions = 0; 158 strncpy(lp->d_packname, "Ultrix label", 16); 159 lp->d_rpm = 3600; 160 lp->d_interleave = 1; 161 lp->d_flags = 0; 162 lp->d_bbsize = BBSIZE; 163 lp->d_sbsize = SBLOCKSIZE; 164 for (part = 0; 165 part <((MAXPARTITIONS<DEC_NUM_DISK_PARTS) ? 166 MAXPARTITIONS : DEC_NUM_DISK_PARTS); 167 part++) { 168 lp->d_partitions[part].p_size = 169 dlp->map[part].num_blocks; 170 lp->d_partitions[part].p_offset = 171 dlp->map[part].start_block; 172 lp->d_partitions[part].p_fsize = 1024; 173 lp->d_partitions[part].p_fstype = 174 (part==1) ? FS_SWAP : FS_BSDFFS; 175 lp->d_npartitions += 1; 176 177#ifdef DIAGNOSTIC 178 printf(" Ultrix label rz%d%c: start %d len %d\n", 179 DISKUNIT(dev), "abcdefgh"[part], 180 lp->d_partitions[part].p_offset, 181 lp->d_partitions[part].p_size); 182#endif 183 } 184 break; 185 } 186 187done: 188 brelse(bp, 0); 189 return msg; 190} 191 192/* 193 * Check new disk label for sensibility 194 * before setting it. 195 */ 196int 197setdisklabel(struct disklabel *olp, struct disklabel *nlp, u_long openmask, 198 struct cpu_disklabel *osdep) 199{ 200 int i; 201 struct partition *opp, *npp; 202 203 if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC || 204 dkcksum(nlp) != 0) 205 return EINVAL; 206 while ((i = ffs(openmask)) != 0) { 207 i--; 208 openmask &= ~(1 << i); 209 if (nlp->d_npartitions <= i) 210 return EBUSY; 211 opp = &olp->d_partitions[i]; 212 npp = &nlp->d_partitions[i]; 213 if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size) 214 return EBUSY; 215 /* 216 * Copy internally-set partition information 217 * if new label doesn't include it. XXX 218 */ 219 if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) { 220 npp->p_fstype = opp->p_fstype; 221 npp->p_fsize = opp->p_fsize; 222 npp->p_frag = opp->p_frag; 223 npp->p_cpg = opp->p_cpg; 224 } 225 } 226 nlp->d_checksum = 0; 227 nlp->d_checksum = dkcksum(nlp); 228 *olp = *nlp; 229 return 0; 230} 231 232/* 233 * Write disk label back to device after modification. 234 */ 235int 236writedisklabel(dev_t dev, void (*strat)(struct buf *bp), struct disklabel *lp, 237 struct cpu_disklabel *osdep) 238{ 239 struct buf *bp; 240 struct disklabel *dlp; 241 int labelpart; 242 int error = 0; 243 244 labelpart = DISKPART(dev); 245 if (lp->d_partitions[labelpart].p_offset != 0) { 246 if (lp->d_partitions[0].p_offset != 0) 247 return EXDEV; /* not quite right */ 248 labelpart = 0; 249 } 250 bp = geteblk((int)lp->d_secsize); 251 bp->b_dev = makedev(major(dev), DISKMINOR(DISKUNIT(dev), labelpart)); 252 bp->b_blkno = LABELSECTOR; 253 bp->b_bcount = lp->d_secsize; 254 bp->b_flags |= B_READ; 255 (*strat)(bp); 256 if ((error = biowait(bp)) != 0) 257 goto done; 258 for (dlp = (struct disklabel *)bp->b_data; 259 dlp <= (struct disklabel *) 260 ((char *)bp->b_data + lp->d_secsize - sizeof(*dlp)); 261 dlp = (struct disklabel *)((char *)dlp + sizeof(long))) { 262 if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC && 263 dkcksum(dlp) == 0) { 264 *dlp = *lp; 265 bp->b_oflags &= ~(BO_DONE); 266 bp->b_flags &= ~(B_READ); 267 bp->b_flags |= B_WRITE; 268 (*strat)(bp); 269 error = biowait(bp); 270 goto done; 271 } 272 } 273 error = ESRCH; 274done: 275 brelse(bp, 0); 276 return error; 277} 278