1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 1991-1997,2002 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate /*LINTLIBRARY*/ 30*0Sstevel@tonic-gate 31*0Sstevel@tonic-gate 32*0Sstevel@tonic-gate #include <stdio.h> 33*0Sstevel@tonic-gate #include <errno.h> 34*0Sstevel@tonic-gate #include <memory.h> 35*0Sstevel@tonic-gate #include <unistd.h> 36*0Sstevel@tonic-gate #include <sys/types.h> 37*0Sstevel@tonic-gate #include <sys/param.h> 38*0Sstevel@tonic-gate #include <sys/dkio.h> 39*0Sstevel@tonic-gate #include <sys/vtoc.h> 40*0Sstevel@tonic-gate 41*0Sstevel@tonic-gate /* 42*0Sstevel@tonic-gate * Read VTOC - return partition number. 43*0Sstevel@tonic-gate */ 44*0Sstevel@tonic-gate int 45*0Sstevel@tonic-gate read_vtoc(int fd, struct vtoc *vtoc) 46*0Sstevel@tonic-gate { 47*0Sstevel@tonic-gate struct dk_cinfo dki_info; 48*0Sstevel@tonic-gate 49*0Sstevel@tonic-gate /* 50*0Sstevel@tonic-gate * Read the vtoc. 51*0Sstevel@tonic-gate */ 52*0Sstevel@tonic-gate if (ioctl(fd, DKIOCGVTOC, (caddr_t)vtoc) == -1) { 53*0Sstevel@tonic-gate switch (errno) { 54*0Sstevel@tonic-gate case EIO: 55*0Sstevel@tonic-gate return (VT_EIO); 56*0Sstevel@tonic-gate case EINVAL: 57*0Sstevel@tonic-gate return (VT_EINVAL); 58*0Sstevel@tonic-gate /* for disks > 1TB */ 59*0Sstevel@tonic-gate case ENOTSUP: 60*0Sstevel@tonic-gate return (VT_ENOTSUP); 61*0Sstevel@tonic-gate default: 62*0Sstevel@tonic-gate return (VT_ERROR); 63*0Sstevel@tonic-gate } 64*0Sstevel@tonic-gate } 65*0Sstevel@tonic-gate 66*0Sstevel@tonic-gate /* 67*0Sstevel@tonic-gate * Sanity-check the vtoc. 68*0Sstevel@tonic-gate */ 69*0Sstevel@tonic-gate if (vtoc->v_sanity != VTOC_SANE) { 70*0Sstevel@tonic-gate return (VT_EINVAL); 71*0Sstevel@tonic-gate } 72*0Sstevel@tonic-gate 73*0Sstevel@tonic-gate /* 74*0Sstevel@tonic-gate * Convert older-style vtoc's. 75*0Sstevel@tonic-gate */ 76*0Sstevel@tonic-gate switch (vtoc->v_version) { 77*0Sstevel@tonic-gate case 0: 78*0Sstevel@tonic-gate /* 79*0Sstevel@tonic-gate * No vtoc information. Install default 80*0Sstevel@tonic-gate * nparts/sectorsz and version. We are 81*0Sstevel@tonic-gate * assuming that the driver returns the 82*0Sstevel@tonic-gate * current partition information correctly. 83*0Sstevel@tonic-gate */ 84*0Sstevel@tonic-gate 85*0Sstevel@tonic-gate vtoc->v_version = V_VERSION; 86*0Sstevel@tonic-gate if (vtoc->v_nparts == 0) 87*0Sstevel@tonic-gate vtoc->v_nparts = V_NUMPAR; 88*0Sstevel@tonic-gate if (vtoc->v_sectorsz == 0) 89*0Sstevel@tonic-gate vtoc->v_sectorsz = DEV_BSIZE; 90*0Sstevel@tonic-gate 91*0Sstevel@tonic-gate break; 92*0Sstevel@tonic-gate 93*0Sstevel@tonic-gate case V_VERSION: 94*0Sstevel@tonic-gate break; 95*0Sstevel@tonic-gate 96*0Sstevel@tonic-gate default: 97*0Sstevel@tonic-gate return (VT_EINVAL); 98*0Sstevel@tonic-gate } 99*0Sstevel@tonic-gate 100*0Sstevel@tonic-gate /* 101*0Sstevel@tonic-gate * Return partition number for this file descriptor. 102*0Sstevel@tonic-gate */ 103*0Sstevel@tonic-gate if (ioctl(fd, DKIOCINFO, (caddr_t)&dki_info) == -1) { 104*0Sstevel@tonic-gate switch (errno) { 105*0Sstevel@tonic-gate case EIO: 106*0Sstevel@tonic-gate return (VT_EIO); 107*0Sstevel@tonic-gate case EINVAL: 108*0Sstevel@tonic-gate return (VT_EINVAL); 109*0Sstevel@tonic-gate default: 110*0Sstevel@tonic-gate return (VT_ERROR); 111*0Sstevel@tonic-gate } 112*0Sstevel@tonic-gate } 113*0Sstevel@tonic-gate if (dki_info.dki_partition > V_NUMPAR) { 114*0Sstevel@tonic-gate return (VT_EINVAL); 115*0Sstevel@tonic-gate } 116*0Sstevel@tonic-gate return ((int)dki_info.dki_partition); 117*0Sstevel@tonic-gate } 118*0Sstevel@tonic-gate 119*0Sstevel@tonic-gate /* 120*0Sstevel@tonic-gate * Write VTOC 121*0Sstevel@tonic-gate */ 122*0Sstevel@tonic-gate int 123*0Sstevel@tonic-gate write_vtoc(int fd, struct vtoc *vtoc) 124*0Sstevel@tonic-gate { 125*0Sstevel@tonic-gate int i; 126*0Sstevel@tonic-gate /* 127*0Sstevel@tonic-gate * Sanity-check the vtoc 128*0Sstevel@tonic-gate */ 129*0Sstevel@tonic-gate if (vtoc->v_sanity != VTOC_SANE || vtoc->v_nparts > V_NUMPAR) { 130*0Sstevel@tonic-gate return (-1); 131*0Sstevel@tonic-gate } 132*0Sstevel@tonic-gate 133*0Sstevel@tonic-gate /* 134*0Sstevel@tonic-gate * since many drivers won't allow opening a device make sure 135*0Sstevel@tonic-gate * all partitions aren't being set to zero. If all are zero then 136*0Sstevel@tonic-gate * we have no way to set them to something else 137*0Sstevel@tonic-gate */ 138*0Sstevel@tonic-gate 139*0Sstevel@tonic-gate for (i = 0; i < (int)vtoc->v_nparts; i++) 140*0Sstevel@tonic-gate if (vtoc->v_part[i].p_size > 0) 141*0Sstevel@tonic-gate break; 142*0Sstevel@tonic-gate if (i == (int)vtoc->v_nparts) 143*0Sstevel@tonic-gate return (-1); 144*0Sstevel@tonic-gate 145*0Sstevel@tonic-gate /* 146*0Sstevel@tonic-gate * Write the vtoc 147*0Sstevel@tonic-gate */ 148*0Sstevel@tonic-gate if (ioctl(fd, DKIOCSVTOC, (caddr_t)vtoc) == -1) { 149*0Sstevel@tonic-gate switch (errno) { 150*0Sstevel@tonic-gate case EIO: 151*0Sstevel@tonic-gate return (VT_EIO); 152*0Sstevel@tonic-gate case EINVAL: 153*0Sstevel@tonic-gate return (VT_EINVAL); 154*0Sstevel@tonic-gate /* for disks > 1TB */ 155*0Sstevel@tonic-gate case ENOTSUP: 156*0Sstevel@tonic-gate return (VT_ENOTSUP); 157*0Sstevel@tonic-gate default: 158*0Sstevel@tonic-gate return (VT_ERROR); 159*0Sstevel@tonic-gate } 160*0Sstevel@tonic-gate } 161*0Sstevel@tonic-gate return (0); 162*0Sstevel@tonic-gate } 163