disksubr.c revision 1.45
11.45Ssimonb/* $NetBSD: disksubr.c,v 1.45 2007/03/06 22:31:36 simonb 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.45Ssimonb__KERNEL_RCSID(0, "$NetBSD: disksubr.c,v 1.45 2007/03/06 22:31:36 simonb Exp $"); 361.15Sthorpej 371.15Sthorpej#include "opt_compat_ultrix.h" 381.1Sderaadt 391.8Smycroft#include <sys/param.h> 401.8Smycroft#include <sys/systm.h> 411.8Smycroft#include <sys/buf.h> 421.9Sjonathan#include <sys/disk.h> 431.8Smycroft#include <sys/disklabel.h> 441.1Sderaadt 451.4Sdean#ifdef COMPAT_ULTRIX 461.33Smatt#include <dev/dec/dec_boot.h> 471.28Smhitch#include <ufs/ufs/dinode.h> /* XXX for fs.h */ 481.28Smhitch#include <ufs/ffs/fs.h> /* XXX for BBSIZE & SBSIZE */ 491.5Sjonathan 501.42Sdrochnerconst char *compat_label __P((dev_t dev, void (*strat) __P((struct buf *bp)), 511.42Sdrochner struct disklabel *lp, struct cpu_disklabel *osdep)); /* XXX */ 521.5Sjonathan 531.4Sdean#endif 541.9Sjonathan 551.1Sderaadt/* 561.1Sderaadt * Attempt to read a disk label from a device 571.35Swiz * using the indicated strategy routine. 581.1Sderaadt * The label must be partly set up before this: 591.1Sderaadt * secpercyl and anything required in the strategy routine 601.1Sderaadt * (e.g., sector size) must be filled in before calling us. 611.1Sderaadt * Returns null on success and an error string on failure. 621.1Sderaadt */ 631.38Sdslconst char * 641.1Sderaadtreaddisklabel(dev, strat, lp, osdep) 651.1Sderaadt dev_t dev; 661.11Sjonathan void (*strat) __P((struct buf *bp)); 671.20Ssimonb struct disklabel *lp; 681.1Sderaadt struct cpu_disklabel *osdep; 691.1Sderaadt{ 701.20Ssimonb struct buf *bp; 711.1Sderaadt struct disklabel *dlp; 721.42Sdrochner const char *msg = NULL; 731.1Sderaadt 741.1Sderaadt if (lp->d_secperunit == 0) 751.1Sderaadt lp->d_secperunit = 0x1fffffff; 761.1Sderaadt lp->d_npartitions = 1; 771.1Sderaadt if (lp->d_partitions[0].p_size == 0) 781.1Sderaadt lp->d_partitions[0].p_size = 0x1fffffff; 791.1Sderaadt lp->d_partitions[0].p_offset = 0; 801.1Sderaadt 811.1Sderaadt bp = geteblk((int)lp->d_secsize); 821.1Sderaadt bp->b_dev = dev; 831.1Sderaadt bp->b_blkno = LABELSECTOR; 841.1Sderaadt bp->b_bcount = lp->d_secsize; 851.34Schs bp->b_flags |= B_READ; 861.26Sthorpej bp->b_cylinder = LABELSECTOR / lp->d_secpercyl; 871.1Sderaadt (*strat)(bp); 881.1Sderaadt if (biowait(bp)) { 891.1Sderaadt msg = "I/O error"; 901.32Sthorpej } else for (dlp = (struct disklabel *)bp->b_data; 911.45Ssimonb dlp <= (struct disklabel *) 921.45Ssimonb ((char *)bp->b_data + DEV_BSIZE - sizeof(*dlp)); 931.1Sderaadt dlp = (struct disklabel *)((char *)dlp + sizeof(long))) { 941.1Sderaadt if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) { 951.1Sderaadt if (msg == NULL) 961.1Sderaadt msg = "no disk label"; 971.1Sderaadt } else if (dlp->d_npartitions > MAXPARTITIONS || 981.1Sderaadt dkcksum(dlp) != 0) 991.1Sderaadt msg = "disk label corrupted"; 1001.1Sderaadt else { 1011.1Sderaadt *lp = *dlp; 1021.1Sderaadt msg = NULL; 1031.1Sderaadt break; 1041.1Sderaadt } 1051.1Sderaadt } 1061.5Sjonathan brelse(bp); 1071.28Smhitch#ifdef COMPAT_ULTRIX 1081.28Smhitch /* 1091.28Smhitch * If no NetBSD label was found, check for an Ultrix label and 1101.28Smhitch * construct tne incore label from the Ultrix partition information. 1111.28Smhitch */ 1121.28Smhitch if (msg != NULL) { 1131.28Smhitch msg = compat_label(dev, strat, lp, osdep); 1141.28Smhitch if (msg == NULL) { 1151.28Smhitch printf("WARNING: using Ultrix partition information\n"); 1161.28Smhitch /* set geometry? */ 1171.28Smhitch } 1181.28Smhitch } 1191.28Smhitch#endif 1201.28Smhitch/* XXX If no NetBSD label or Ultrix label found, generate default label here */ 1211.5Sjonathan return (msg); 1221.5Sjonathan} 1231.5Sjonathan 1241.5Sjonathan#ifdef COMPAT_ULTRIX 1251.5Sjonathan/* 1261.5Sjonathan * Given a buffer bp, try and interpret it as an Ultrix disk label, 1271.5Sjonathan * putting the partition info into a native NetBSD label 1281.5Sjonathan */ 1291.42Sdrochnerconst char * 1301.5Sjonathancompat_label(dev, strat, lp, osdep) 1311.5Sjonathan dev_t dev; 1321.11Sjonathan void (*strat) __P((struct buf *bp)); 1331.20Ssimonb struct disklabel *lp; 1341.5Sjonathan struct cpu_disklabel *osdep; 1351.5Sjonathan{ 1361.21Ssimonb dec_disklabel *dlp; 1371.5Sjonathan struct buf *bp = NULL; 1381.42Sdrochner const char *msg = NULL; 1391.5Sjonathan 1401.5Sjonathan bp = geteblk((int)lp->d_secsize); 1411.5Sjonathan bp->b_dev = dev; 1421.5Sjonathan bp->b_blkno = DEC_LABEL_SECTOR; 1431.5Sjonathan bp->b_bcount = lp->d_secsize; 1441.34Schs bp->b_flags |= B_READ; 1451.26Sthorpej bp->b_cylinder = DEC_LABEL_SECTOR / lp->d_secpercyl; 1461.5Sjonathan (*strat)(bp); 1471.5Sjonathan 1481.5Sjonathan if (biowait(bp)) { 1491.4Sdean msg = "I/O error"; 1501.5Sjonathan goto done; 1511.5Sjonathan } 1521.5Sjonathan 1531.32Sthorpej for (dlp = (dec_disklabel *)bp->b_data; 1541.45Ssimonb dlp <= (dec_disklabel *) 1551.45Ssimonb ((char *)bp->b_data + DEV_BSIZE - sizeof(*dlp)); 1561.21Ssimonb dlp = (dec_disklabel *)((char *)dlp + sizeof(long))) { 1571.5Sjonathan 1581.5Sjonathan int part; 1591.5Sjonathan 1601.5Sjonathan if (dlp->magic != DEC_LABEL_MAGIC) { 1611.13Schristos printf("label: %x\n",dlp->magic); 1621.5Sjonathan msg = ((msg != NULL) ? msg: "no disk label"); 1631.5Sjonathan goto done; 1641.5Sjonathan } 1651.5Sjonathan 1661.6Sjonathan#ifdef DIAGNOSTIC 1671.13Schristos/*XXX*/ printf("Interpreting Ultrix label\n"); 1681.6Sjonathan#endif 1691.5Sjonathan 1701.5Sjonathan lp->d_magic = DEC_LABEL_MAGIC; 1711.5Sjonathan lp->d_npartitions = 0; 1721.28Smhitch strncpy(lp->d_packname, "Ultrix label", 16); 1731.28Smhitch lp->d_rpm = 3600; 1741.28Smhitch lp->d_interleave = 1; 1751.28Smhitch lp->d_flags = 0; 1761.28Smhitch lp->d_bbsize = BBSIZE; 1771.37Sdrochner lp->d_sbsize = SBLOCKSIZE; 1781.5Sjonathan for (part = 0; 1791.5Sjonathan part <((MAXPARTITIONS<DEC_NUM_DISK_PARTS) ? 1801.5Sjonathan MAXPARTITIONS : DEC_NUM_DISK_PARTS); 1811.5Sjonathan part++) { 1821.21Ssimonb lp->d_partitions[part].p_size = dlp->map[part].num_blocks; 1831.21Ssimonb lp->d_partitions[part].p_offset = dlp->map[part].start_block; 1841.5Sjonathan lp->d_partitions[part].p_fsize = 1024; 1851.5Sjonathan lp->d_partitions[part].p_fstype = 1861.5Sjonathan (part==1) ? FS_SWAP : FS_BSDFFS; 1871.5Sjonathan lp->d_npartitions += 1; 1881.5Sjonathan 1891.6Sjonathan#ifdef DIAGNOSTIC 1901.13Schristos printf(" Ultrix label rz%d%c: start %d len %d\n", 1911.5Sjonathan DISKUNIT(dev), "abcdefgh"[part], 1921.5Sjonathan lp->d_partitions[part].p_offset, 1931.5Sjonathan lp->d_partitions[part].p_size); 1941.19Ssimonb#endif 1951.5Sjonathan } 1961.5Sjonathan break; 1971.4Sdean } 1981.5Sjonathan 1991.5Sjonathandone: 2001.1Sderaadt brelse(bp); 2011.1Sderaadt return (msg); 2021.1Sderaadt} 2031.5Sjonathan#endif /* COMPAT_ULTRIX */ 2041.5Sjonathan 2051.1Sderaadt 2061.1Sderaadt/* 2071.1Sderaadt * Check new disk label for sensibility 2081.1Sderaadt * before setting it. 2091.1Sderaadt */ 2101.9Sjonathanint 2111.1Sderaadtsetdisklabel(olp, nlp, openmask, osdep) 2121.16Smrg struct disklabel *olp, *nlp; 2131.1Sderaadt u_long openmask; 2141.1Sderaadt struct cpu_disklabel *osdep; 2151.1Sderaadt{ 2161.16Smrg int i; 2171.16Smrg struct partition *opp, *npp; 2181.1Sderaadt 2191.1Sderaadt if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC || 2201.1Sderaadt dkcksum(nlp) != 0) 2211.1Sderaadt return (EINVAL); 2221.36Ssimonb while ((i = ffs(openmask)) != 0) { 2231.1Sderaadt i--; 2241.1Sderaadt openmask &= ~(1 << i); 2251.1Sderaadt if (nlp->d_npartitions <= i) 2261.1Sderaadt return (EBUSY); 2271.1Sderaadt opp = &olp->d_partitions[i]; 2281.1Sderaadt npp = &nlp->d_partitions[i]; 2291.1Sderaadt if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size) 2301.1Sderaadt return (EBUSY); 2311.1Sderaadt /* 2321.1Sderaadt * Copy internally-set partition information 2331.1Sderaadt * if new label doesn't include it. XXX 2341.1Sderaadt */ 2351.1Sderaadt if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) { 2361.1Sderaadt npp->p_fstype = opp->p_fstype; 2371.1Sderaadt npp->p_fsize = opp->p_fsize; 2381.1Sderaadt npp->p_frag = opp->p_frag; 2391.1Sderaadt npp->p_cpg = opp->p_cpg; 2401.1Sderaadt } 2411.1Sderaadt } 2421.1Sderaadt nlp->d_checksum = 0; 2431.1Sderaadt nlp->d_checksum = dkcksum(nlp); 2441.1Sderaadt *olp = *nlp; 2451.1Sderaadt return (0); 2461.1Sderaadt} 2471.1Sderaadt 2481.1Sderaadt/* 2491.1Sderaadt * Write disk label back to device after modification. 2501.1Sderaadt */ 2511.9Sjonathanint 2521.1Sderaadtwritedisklabel(dev, strat, lp, osdep) 2531.1Sderaadt dev_t dev; 2541.11Sjonathan void (*strat) __P((struct buf *bp)); 2551.20Ssimonb struct disklabel *lp; 2561.1Sderaadt struct cpu_disklabel *osdep; 2571.1Sderaadt{ 2581.1Sderaadt struct buf *bp; 2591.1Sderaadt struct disklabel *dlp; 2601.1Sderaadt int labelpart; 2611.1Sderaadt int error = 0; 2621.1Sderaadt 2631.30Stsutsui labelpart = DISKPART(dev); 2641.1Sderaadt if (lp->d_partitions[labelpart].p_offset != 0) { 2651.1Sderaadt if (lp->d_partitions[0].p_offset != 0) 2661.1Sderaadt return (EXDEV); /* not quite right */ 2671.1Sderaadt labelpart = 0; 2681.1Sderaadt } 2691.1Sderaadt bp = geteblk((int)lp->d_secsize); 2701.30Stsutsui bp->b_dev = makedev(major(dev), DISKMINOR(DISKUNIT(dev), labelpart)); 2711.1Sderaadt bp->b_blkno = LABELSECTOR; 2721.1Sderaadt bp->b_bcount = lp->d_secsize; 2731.34Schs bp->b_flags |= B_READ; 2741.1Sderaadt (*strat)(bp); 2751.9Sjonathan if ((error = biowait(bp)) != 0) 2761.1Sderaadt goto done; 2771.32Sthorpej for (dlp = (struct disklabel *)bp->b_data; 2781.1Sderaadt dlp <= (struct disklabel *) 2791.45Ssimonb ((char *)bp->b_data + lp->d_secsize - sizeof(*dlp)); 2801.1Sderaadt dlp = (struct disklabel *)((char *)dlp + sizeof(long))) { 2811.1Sderaadt if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC && 2821.1Sderaadt dkcksum(dlp) == 0) { 2831.1Sderaadt *dlp = *lp; 2841.34Schs bp->b_flags &= ~(B_READ|B_DONE); 2851.34Schs bp->b_flags |= B_WRITE; 2861.1Sderaadt (*strat)(bp); 2871.1Sderaadt error = biowait(bp); 2881.1Sderaadt goto done; 2891.1Sderaadt } 2901.1Sderaadt } 2911.1Sderaadt error = ESRCH; 2921.1Sderaadtdone: 2931.1Sderaadt brelse(bp); 2941.1Sderaadt return (error); 2951.6Sjonathan} 296