disksubr.c revision 1.53
11.53Stsutsui/* $NetBSD: disksubr.c,v 1.53 2009/07/20 17:05:13 tsutsui Exp $ */ 21.3Scgd 31.1Sderaadt/* 41.1Sderaadt * Copyright (c) 1982, 1986, 1988 Regents of the University of California. 51.1Sderaadt * All rights reserved. 61.1Sderaadt * 71.1Sderaadt * Redistribution and use in source and binary forms, with or without 81.1Sderaadt * modification, are permitted provided that the following conditions 91.1Sderaadt * are met: 101.1Sderaadt * 1. Redistributions of source code must retain the above copyright 111.1Sderaadt * notice, this list of conditions and the following disclaimer. 121.1Sderaadt * 2. Redistributions in binary form must reproduce the above copyright 131.1Sderaadt * notice, this list of conditions and the following disclaimer in the 141.1Sderaadt * documentation and/or other materials provided with the distribution. 151.41Sagc * 3. Neither the name of the University nor the names of its contributors 161.1Sderaadt * may be used to endorse or promote products derived from this software 171.1Sderaadt * without specific prior written permission. 181.1Sderaadt * 191.1Sderaadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 201.1Sderaadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 211.1Sderaadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 221.1Sderaadt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 231.1Sderaadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 241.1Sderaadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 251.1Sderaadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 261.1Sderaadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 271.1Sderaadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 281.1Sderaadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 291.1Sderaadt * SUCH DAMAGE. 301.1Sderaadt * 311.3Scgd * @(#)ufs_disksubr.c 7.16 (Berkeley) 5/4/91 321.1Sderaadt */ 331.40Slukem 341.40Slukem#include <sys/cdefs.h> 351.53Stsutsui__KERNEL_RCSID(0, "$NetBSD: disksubr.c,v 1.53 2009/07/20 17:05:13 tsutsui Exp $"); 361.1Sderaadt 371.8Smycroft#include <sys/param.h> 381.8Smycroft#include <sys/systm.h> 391.8Smycroft#include <sys/buf.h> 401.9Sjonathan#include <sys/disk.h> 411.8Smycroft#include <sys/disklabel.h> 421.1Sderaadt 431.33Smatt#include <dev/dec/dec_boot.h> 441.28Smhitch#include <ufs/ufs/dinode.h> /* XXX for fs.h */ 451.28Smhitch#include <ufs/ffs/fs.h> /* XXX for BBSIZE & SBSIZE */ 461.5Sjonathan 471.50Sdslconst char *compat_label(dev_t dev, void (*strat)(struct buf *bp), 481.50Sdsl struct disklabel *lp, struct cpu_disklabel *osdep); /* XXX */ 491.5Sjonathan 501.1Sderaadt/* 511.1Sderaadt * Attempt to read a disk label from a device 521.35Swiz * using the indicated strategy routine. 531.1Sderaadt * The label must be partly set up before this: 541.1Sderaadt * secpercyl and anything required in the strategy routine 551.1Sderaadt * (e.g., sector size) must be filled in before calling us. 561.1Sderaadt * Returns null on success and an error string on failure. 571.1Sderaadt */ 581.38Sdslconst char * 591.53Stsutsuireaddisklabel(dev_t dev, void (*strat)(struct buf *bp), struct disklabel *lp, 601.53Stsutsui struct cpu_disklabel *osdep) 611.1Sderaadt{ 621.20Ssimonb struct buf *bp; 631.1Sderaadt struct disklabel *dlp; 641.42Sdrochner const char *msg = NULL; 651.1Sderaadt 661.1Sderaadt if (lp->d_secperunit == 0) 671.1Sderaadt lp->d_secperunit = 0x1fffffff; 681.1Sderaadt lp->d_npartitions = 1; 691.1Sderaadt if (lp->d_partitions[0].p_size == 0) 701.1Sderaadt lp->d_partitions[0].p_size = 0x1fffffff; 711.1Sderaadt lp->d_partitions[0].p_offset = 0; 721.1Sderaadt 731.1Sderaadt bp = geteblk((int)lp->d_secsize); 741.1Sderaadt bp->b_dev = dev; 751.1Sderaadt bp->b_blkno = LABELSECTOR; 761.1Sderaadt bp->b_bcount = lp->d_secsize; 771.34Schs bp->b_flags |= B_READ; 781.26Sthorpej bp->b_cylinder = LABELSECTOR / lp->d_secpercyl; 791.1Sderaadt (*strat)(bp); 801.1Sderaadt if (biowait(bp)) { 811.1Sderaadt msg = "I/O error"; 821.32Sthorpej } else for (dlp = (struct disklabel *)bp->b_data; 831.45Ssimonb dlp <= (struct disklabel *) 841.45Ssimonb ((char *)bp->b_data + DEV_BSIZE - sizeof(*dlp)); 851.1Sderaadt dlp = (struct disklabel *)((char *)dlp + sizeof(long))) { 861.1Sderaadt if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) { 871.1Sderaadt if (msg == NULL) 881.1Sderaadt msg = "no disk label"; 891.1Sderaadt } else if (dlp->d_npartitions > MAXPARTITIONS || 901.1Sderaadt dkcksum(dlp) != 0) 911.1Sderaadt msg = "disk label corrupted"; 921.1Sderaadt else { 931.1Sderaadt *lp = *dlp; 941.1Sderaadt msg = NULL; 951.1Sderaadt break; 961.1Sderaadt } 971.1Sderaadt } 981.46Sad brelse(bp, 0); 991.28Smhitch /* 1001.28Smhitch * If no NetBSD label was found, check for an Ultrix label and 1011.28Smhitch * construct tne incore label from the Ultrix partition information. 1021.28Smhitch */ 1031.28Smhitch if (msg != NULL) { 1041.28Smhitch msg = compat_label(dev, strat, lp, osdep); 1051.28Smhitch if (msg == NULL) { 1061.28Smhitch printf("WARNING: using Ultrix partition information\n"); 1071.28Smhitch /* set geometry? */ 1081.28Smhitch } 1091.28Smhitch } 1101.28Smhitch/* XXX If no NetBSD label or Ultrix label found, generate default label here */ 1111.53Stsutsui return msg; 1121.5Sjonathan} 1131.5Sjonathan 1141.5Sjonathan/* 1151.5Sjonathan * Given a buffer bp, try and interpret it as an Ultrix disk label, 1161.5Sjonathan * putting the partition info into a native NetBSD label 1171.5Sjonathan */ 1181.42Sdrochnerconst char * 1191.53Stsutsuicompat_label(dev_t dev, void (*strat)(struct buf *bp), struct disklabel *lp, 1201.53Stsutsui struct cpu_disklabel *osdep) 1211.5Sjonathan{ 1221.21Ssimonb dec_disklabel *dlp; 1231.5Sjonathan struct buf *bp = NULL; 1241.42Sdrochner const char *msg = NULL; 1251.5Sjonathan 1261.5Sjonathan bp = geteblk((int)lp->d_secsize); 1271.5Sjonathan bp->b_dev = dev; 1281.5Sjonathan bp->b_blkno = DEC_LABEL_SECTOR; 1291.5Sjonathan bp->b_bcount = lp->d_secsize; 1301.34Schs bp->b_flags |= B_READ; 1311.26Sthorpej bp->b_cylinder = DEC_LABEL_SECTOR / lp->d_secpercyl; 1321.5Sjonathan (*strat)(bp); 1331.5Sjonathan 1341.5Sjonathan if (biowait(bp)) { 1351.4Sdean msg = "I/O error"; 1361.5Sjonathan goto done; 1371.5Sjonathan } 1381.5Sjonathan 1391.32Sthorpej for (dlp = (dec_disklabel *)bp->b_data; 1401.45Ssimonb dlp <= (dec_disklabel *) 1411.45Ssimonb ((char *)bp->b_data + DEV_BSIZE - sizeof(*dlp)); 1421.21Ssimonb dlp = (dec_disklabel *)((char *)dlp + sizeof(long))) { 1431.5Sjonathan 1441.5Sjonathan int part; 1451.5Sjonathan 1461.5Sjonathan if (dlp->magic != DEC_LABEL_MAGIC) { 1471.13Schristos printf("label: %x\n",dlp->magic); 1481.5Sjonathan msg = ((msg != NULL) ? msg: "no disk label"); 1491.5Sjonathan goto done; 1501.5Sjonathan } 1511.5Sjonathan 1521.6Sjonathan#ifdef DIAGNOSTIC 1531.13Schristos/*XXX*/ printf("Interpreting Ultrix label\n"); 1541.6Sjonathan#endif 1551.5Sjonathan 1561.5Sjonathan lp->d_magic = DEC_LABEL_MAGIC; 1571.5Sjonathan lp->d_npartitions = 0; 1581.28Smhitch strncpy(lp->d_packname, "Ultrix label", 16); 1591.28Smhitch lp->d_rpm = 3600; 1601.28Smhitch lp->d_interleave = 1; 1611.28Smhitch lp->d_flags = 0; 1621.28Smhitch lp->d_bbsize = BBSIZE; 1631.37Sdrochner lp->d_sbsize = SBLOCKSIZE; 1641.5Sjonathan for (part = 0; 1651.5Sjonathan part <((MAXPARTITIONS<DEC_NUM_DISK_PARTS) ? 1661.5Sjonathan MAXPARTITIONS : DEC_NUM_DISK_PARTS); 1671.5Sjonathan part++) { 1681.53Stsutsui lp->d_partitions[part].p_size = 1691.53Stsutsui dlp->map[part].num_blocks; 1701.53Stsutsui lp->d_partitions[part].p_offset = 1711.53Stsutsui dlp->map[part].start_block; 1721.5Sjonathan lp->d_partitions[part].p_fsize = 1024; 1731.5Sjonathan lp->d_partitions[part].p_fstype = 1741.5Sjonathan (part==1) ? FS_SWAP : FS_BSDFFS; 1751.5Sjonathan lp->d_npartitions += 1; 1761.5Sjonathan 1771.6Sjonathan#ifdef DIAGNOSTIC 1781.13Schristos printf(" Ultrix label rz%d%c: start %d len %d\n", 1791.5Sjonathan DISKUNIT(dev), "abcdefgh"[part], 1801.5Sjonathan lp->d_partitions[part].p_offset, 1811.5Sjonathan lp->d_partitions[part].p_size); 1821.19Ssimonb#endif 1831.5Sjonathan } 1841.5Sjonathan break; 1851.4Sdean } 1861.5Sjonathan 1871.5Sjonathandone: 1881.46Sad brelse(bp, 0); 1891.53Stsutsui return msg; 1901.1Sderaadt} 1911.1Sderaadt 1921.1Sderaadt/* 1931.1Sderaadt * Check new disk label for sensibility 1941.1Sderaadt * before setting it. 1951.1Sderaadt */ 1961.9Sjonathanint 1971.53Stsutsuisetdisklabel(struct disklabel *olp, struct disklabel *nlp, u_long openmask, 1981.53Stsutsui struct cpu_disklabel *osdep) 1991.1Sderaadt{ 2001.16Smrg int i; 2011.16Smrg struct partition *opp, *npp; 2021.1Sderaadt 2031.1Sderaadt if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC || 2041.1Sderaadt dkcksum(nlp) != 0) 2051.53Stsutsui return EINVAL; 2061.36Ssimonb while ((i = ffs(openmask)) != 0) { 2071.1Sderaadt i--; 2081.1Sderaadt openmask &= ~(1 << i); 2091.1Sderaadt if (nlp->d_npartitions <= i) 2101.53Stsutsui return EBUSY; 2111.1Sderaadt opp = &olp->d_partitions[i]; 2121.1Sderaadt npp = &nlp->d_partitions[i]; 2131.1Sderaadt if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size) 2141.53Stsutsui return EBUSY; 2151.1Sderaadt /* 2161.1Sderaadt * Copy internally-set partition information 2171.1Sderaadt * if new label doesn't include it. XXX 2181.1Sderaadt */ 2191.1Sderaadt if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) { 2201.1Sderaadt npp->p_fstype = opp->p_fstype; 2211.1Sderaadt npp->p_fsize = opp->p_fsize; 2221.1Sderaadt npp->p_frag = opp->p_frag; 2231.1Sderaadt npp->p_cpg = opp->p_cpg; 2241.1Sderaadt } 2251.1Sderaadt } 2261.1Sderaadt nlp->d_checksum = 0; 2271.1Sderaadt nlp->d_checksum = dkcksum(nlp); 2281.1Sderaadt *olp = *nlp; 2291.53Stsutsui return 0; 2301.1Sderaadt} 2311.1Sderaadt 2321.1Sderaadt/* 2331.1Sderaadt * Write disk label back to device after modification. 2341.1Sderaadt */ 2351.9Sjonathanint 2361.53Stsutsuiwritedisklabel(dev_t dev, void (*strat)(struct buf *bp), struct disklabel *lp, 2371.53Stsutsui struct cpu_disklabel *osdep) 2381.1Sderaadt{ 2391.1Sderaadt struct buf *bp; 2401.1Sderaadt struct disklabel *dlp; 2411.1Sderaadt int labelpart; 2421.1Sderaadt int error = 0; 2431.1Sderaadt 2441.30Stsutsui labelpart = DISKPART(dev); 2451.1Sderaadt if (lp->d_partitions[labelpart].p_offset != 0) { 2461.1Sderaadt if (lp->d_partitions[0].p_offset != 0) 2471.53Stsutsui return EXDEV; /* not quite right */ 2481.1Sderaadt labelpart = 0; 2491.1Sderaadt } 2501.1Sderaadt bp = geteblk((int)lp->d_secsize); 2511.30Stsutsui bp->b_dev = makedev(major(dev), DISKMINOR(DISKUNIT(dev), labelpart)); 2521.1Sderaadt bp->b_blkno = LABELSECTOR; 2531.1Sderaadt bp->b_bcount = lp->d_secsize; 2541.34Schs bp->b_flags |= B_READ; 2551.1Sderaadt (*strat)(bp); 2561.9Sjonathan if ((error = biowait(bp)) != 0) 2571.1Sderaadt goto done; 2581.32Sthorpej for (dlp = (struct disklabel *)bp->b_data; 2591.1Sderaadt dlp <= (struct disklabel *) 2601.45Ssimonb ((char *)bp->b_data + lp->d_secsize - sizeof(*dlp)); 2611.1Sderaadt dlp = (struct disklabel *)((char *)dlp + sizeof(long))) { 2621.1Sderaadt if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC && 2631.1Sderaadt dkcksum(dlp) == 0) { 2641.1Sderaadt *dlp = *lp; 2651.48Sad bp->b_oflags &= ~(BO_DONE); 2661.48Sad bp->b_flags &= ~(B_READ); 2671.34Schs bp->b_flags |= B_WRITE; 2681.1Sderaadt (*strat)(bp); 2691.1Sderaadt error = biowait(bp); 2701.1Sderaadt goto done; 2711.1Sderaadt } 2721.1Sderaadt } 2731.1Sderaadt error = ESRCH; 2741.1Sderaadtdone: 2751.46Sad brelse(bp, 0); 2761.53Stsutsui return error; 2771.6Sjonathan} 278