1 /* $NetBSD: disklabel.h,v 1.116 2013/11/05 00:36:02 msaitoh Exp $ */ 2 3 /* 4 * Copyright (c) 1987, 1988, 1993 5 * The Regents of the University of California. 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 * @(#)disklabel.h 8.2 (Berkeley) 7/10/94 32 */ 33 34 #ifndef _SYS_DISKLABEL_H_ 35 #define _SYS_DISKLABEL_H_ 36 37 /* 38 * We need <machine/types.h> for __HAVE_OLD_DISKLABEL 39 */ 40 #ifndef _LOCORE 41 #include <sys/types.h> 42 #endif 43 44 /* 45 * Each disk has a label which includes information about the hardware 46 * disk geometry, filesystem partitions, and drive specific information. 47 * The location of the label, as well as the number of partitions the 48 * label can describe and the number of the "whole disk" (raw) 49 * partition are machine dependent. 50 */ 51 #if HAVE_NBTOOL_CONFIG_H 52 #undef MAXPARTITIONS 53 #define MAXPARTITIONS MAXMAXPARTITIONS 54 #else 55 #include <machine/disklabel.h> 56 #endif /* HAVE_NBTOOL_CONFIG_H */ 57 58 /* 59 * The absolute maximum number of disk partitions allowed. 60 * This is the maximum value of MAXPARTITIONS for which 'struct disklabel' 61 * is <= DEV_BSIZE bytes long. If MAXPARTITIONS is greater than this, beware. 62 */ 63 #define MAXMAXPARTITIONS 22 64 #if MAXPARTITIONS > MAXMAXPARTITIONS 65 #warning beware: MAXPARTITIONS bigger than MAXMAXPARTITIONS 66 #endif 67 68 /* 69 * Ports can switch their MAXPARTITIONS once, as follows: 70 * 71 * - define OLDMAXPARTITIONS in <machine/disklabel.h> as the old number 72 * - define MAXPARTITIONS as the new number 73 * - define DISKUNIT, DISKPART and DISKMINOR macros in <machine/disklabel.h> 74 * as appropriate for the port (see the i386 one for an example). 75 * - define __HAVE_OLD_DISKLABEL in <machine/types.h> 76 */ 77 78 #if defined(_KERNEL) && defined(__HAVE_OLD_DISKLABEL) && \ 79 (MAXPARTITIONS < OLDMAXPARTITIONS) 80 #error "can only grow disklabel size" 81 #endif 82 83 84 /* 85 * Translate between device numbers and major/disk unit/disk partition. 86 */ 87 #ifndef __HAVE_OLD_DISKLABEL 88 #if !HAVE_NBTOOL_CONFIG_H 89 #define DISKUNIT(dev) (minor(dev) / MAXPARTITIONS) 90 #define DISKPART(dev) (minor(dev) % MAXPARTITIONS) 91 #define DISKMINOR(unit, part) \ 92 (((unit) * MAXPARTITIONS) + (part)) 93 #endif /* !HAVE_NBTOOL_CONFIG_H */ 94 #endif 95 #define MAKEDISKDEV(maj, unit, part) \ 96 (makedev((maj), DISKMINOR((unit), (part)))) 97 98 #define DISKMAGIC ((uint32_t)0x82564557) /* The disk magic number */ 99 100 #ifndef _LOCORE 101 struct disklabel { 102 uint32_t d_magic; /* the magic number */ 103 uint16_t d_type; /* drive type */ 104 uint16_t d_subtype; /* controller/d_type specific */ 105 char d_typename[16]; /* type name, e.g. "eagle" */ 106 107 /* 108 * d_packname contains the pack identifier and is returned when 109 * the disklabel is read off the disk or in-core copy. 110 * d_boot0 and d_boot1 are the (optional) names of the 111 * primary (block 0) and secondary (block 1-15) bootstraps 112 * as found in /usr/mdec. These are returned when using 113 * getdiskbyname(3) to retrieve the values from /etc/disktab. 114 */ 115 union { 116 char un_d_packname[16]; /* pack identifier */ 117 struct { 118 char *un_d_boot0; /* primary bootstrap name */ 119 char *un_d_boot1; /* secondary bootstrap name */ 120 } un_b; 121 uint64_t un_d_pad; /* force 8 byte alignment */ 122 } d_un; 123 #define d_packname d_un.un_d_packname 124 #define d_boot0 d_un.un_b.un_d_boot0 125 #define d_boot1 d_un.un_b.un_d_boot1 126 127 /* disk geometry: */ 128 uint32_t d_secsize; /* # of bytes per sector */ 129 uint32_t d_nsectors; /* # of data sectors per track */ 130 uint32_t d_ntracks; /* # of tracks per cylinder */ 131 uint32_t d_ncylinders; /* # of data cylinders per unit */ 132 uint32_t d_secpercyl; /* # of data sectors per cylinder */ 133 uint32_t d_secperunit; /* # of data sectors per unit */ 134 135 /* 136 * Spares (bad sector replacements) below are not counted in 137 * d_nsectors or d_secpercyl. Spare sectors are assumed to 138 * be physical sectors which occupy space at the end of each 139 * track and/or cylinder. 140 */ 141 uint16_t d_sparespertrack; /* # of spare sectors per track */ 142 uint16_t d_sparespercyl; /* # of spare sectors per cylinder */ 143 /* 144 * Alternative cylinders include maintenance, replacement, 145 * configuration description areas, etc. 146 */ 147 uint32_t d_acylinders; /* # of alt. cylinders per unit */ 148 149 /* hardware characteristics: */ 150 /* 151 * d_interleave, d_trackskew and d_cylskew describe perturbations 152 * in the media format used to compensate for a slow controller. 153 * Interleave is physical sector interleave, set up by the 154 * formatter or controller when formatting. When interleaving is 155 * in use, logically adjacent sectors are not physically 156 * contiguous, but instead are separated by some number of 157 * sectors. It is specified as the ratio of physical sectors 158 * traversed per logical sector. Thus an interleave of 1:1 159 * implies contiguous layout, while 2:1 implies that logical 160 * sector 0 is separated by one sector from logical sector 1. 161 * d_trackskew is the offset of sector 0 on track N relative to 162 * sector 0 on track N-1 on the same cylinder. Finally, d_cylskew 163 * is the offset of sector 0 on cylinder N relative to sector 0 164 * on cylinder N-1. 165 */ 166 uint16_t d_rpm; /* rotational speed */ 167 uint16_t d_interleave; /* hardware sector interleave */ 168 uint16_t d_trackskew; /* sector 0 skew, per track */ 169 uint16_t d_cylskew; /* sector 0 skew, per cylinder */ 170 uint32_t d_headswitch; /* head switch time, usec */ 171 uint32_t d_trkseek; /* track-to-track seek, usec */ 172 uint32_t d_flags; /* generic flags */ 173 #define NDDATA 5 174 uint32_t d_drivedata[NDDATA]; /* drive-type specific information */ 175 #define NSPARE 5 176 uint32_t d_spare[NSPARE]; /* reserved for future use */ 177 uint32_t d_magic2; /* the magic number (again) */ 178 uint16_t d_checksum; /* xor of data incl. partitions */ 179 180 /* filesystem and partition information: */ 181 uint16_t d_npartitions; /* number of partitions in following */ 182 uint32_t d_bbsize; /* size of boot area at sn0, bytes */ 183 uint32_t d_sbsize; /* max size of fs superblock, bytes */ 184 struct partition { /* the partition table */ 185 uint32_t p_size; /* number of sectors in partition */ 186 uint32_t p_offset; /* starting sector */ 187 union { 188 uint32_t fsize; /* FFS, ADOS: 189 filesystem basic fragment size */ 190 uint32_t cdsession; /* ISO9660: session offset */ 191 } __partition_u2; 192 #define p_fsize __partition_u2.fsize 193 #define p_cdsession __partition_u2.cdsession 194 uint8_t p_fstype; /* filesystem type, see below */ 195 uint8_t p_frag; /* filesystem fragments per block */ 196 union { 197 uint16_t cpg; /* UFS: FS cylinders per group */ 198 uint16_t sgs; /* LFS: FS segment shift */ 199 } __partition_u1; 200 #define p_cpg __partition_u1.cpg 201 #define p_sgs __partition_u1.sgs 202 } d_partitions[MAXPARTITIONS]; /* actually may be more */ 203 }; 204 205 #if defined(__HAVE_OLD_DISKLABEL) && !HAVE_NBTOOL_CONFIG_H 206 /* 207 * Same as above, but with OLDMAXPARTITIONS partitions. For use in 208 * the old DIOC* ioctl calls. 209 */ 210 struct olddisklabel { 211 uint32_t d_magic; 212 uint16_t d_type; 213 uint16_t d_subtype; 214 char d_typename[16]; 215 union { 216 char un_d_packname[16]; 217 struct { 218 char *un_d_boot0; 219 char *un_d_boot1; 220 } un_b; 221 } d_un; 222 uint32_t d_secsize; 223 uint32_t d_nsectors; 224 uint32_t d_ntracks; 225 uint32_t d_ncylinders; 226 uint32_t d_secpercyl; 227 uint32_t d_secperunit; 228 uint16_t d_sparespertrack; 229 uint16_t d_sparespercyl; 230 uint32_t d_acylinders; 231 uint16_t d_rpm; 232 uint16_t d_interleave; 233 uint16_t d_trackskew; 234 uint16_t d_cylskew; 235 uint32_t d_headswitch; 236 uint32_t d_trkseek; 237 uint32_t d_flags; 238 uint32_t d_drivedata[NDDATA]; 239 uint32_t d_spare[NSPARE]; 240 uint32_t d_magic2; 241 uint16_t d_checksum; 242 uint16_t d_npartitions; 243 uint32_t d_bbsize; 244 uint32_t d_sbsize; 245 struct opartition { 246 uint32_t p_size; 247 uint32_t p_offset; 248 union { 249 uint32_t fsize; 250 uint32_t cdsession; 251 } __partition_u2; 252 uint8_t p_fstype; 253 uint8_t p_frag; 254 union { 255 uint16_t cpg; 256 uint16_t sgs; 257 } __partition_u1; 258 } d_partitions[OLDMAXPARTITIONS]; 259 }; 260 #endif /* __HAVE_OLD_DISKLABEL */ 261 #else /* _LOCORE */ 262 /* 263 * offsets for asm boot files. 264 */ 265 .set d_secsize,40 266 .set d_nsectors,44 267 .set d_ntracks,48 268 .set d_ncylinders,52 269 .set d_secpercyl,56 270 .set d_secperunit,60 271 .set d_end_,148+(MAXPARTITIONS*16) 272 #endif /* _LOCORE */ 273 274 /* 275 * We normally use C99 initialisers (just in case the lists below are out 276 * of sequence, or have gaps), but lint doesn't grok them. 277 * Maybe some host compilers don't either, but many have for quite some time. 278 */ 279 280 #ifndef lint 281 #define ARRAY_INIT(element,value) [element]=value 282 #else 283 #define ARRAY_INIT(element,value) value 284 #endif 285 286 /* Use pre-processor magic to get all the parameters one one line... */ 287 288 /* d_type values: */ 289 #define DKTYPE_DEFN(x) \ 290 x(UNKNOWN, 0, "unknown") \ 291 x(SMD, 1, "SMD") /* SMD, XSMD; VAX hp/up */ \ 292 x(MSCP, 2, "MSCP") /* MSCP */ \ 293 x(DEC, 3, "old DEC") /* other DEC (rk, rl) */ \ 294 x(SCSI, 4, "SCSI") /* SCSI */ \ 295 x(ESDI, 5, "ESDI") /* ESDI interface */ \ 296 x(ST506, 6, "ST506") /* ST506 etc. */ \ 297 x(HPIB, 7, "HP-IB") /* CS/80 on HP-IB */ \ 298 x(HPFL, 8, "HP-FL") /* HP Fiber-link */ \ 299 x(TYPE_9, 9, "type 9") \ 300 x(FLOPPY, 10, "floppy") /* floppy */ \ 301 x(CCD, 11, "ccd") /* concatenated disk device */ \ 302 x(VND, 12, "vnd") /* uvnode pseudo-disk */ \ 303 x(ATAPI, 13, "ATAPI") /* ATAPI */ \ 304 x(RAID, 14, "RAID") /* RAIDframe */ \ 305 x(LD, 15, "ld") /* logical disk */ \ 306 x(JFS2, 16, "jfs") /* IBM JFS2 */ \ 307 x(CGD, 17, "cgd") /* cryptographic pseudo-disk */ \ 308 x(VINUM, 18, "vinum") /* vinum volume */ \ 309 x(FLASH, 19, "flash") /* flash memory devices */ \ 310 x(DM, 20, "dm") /* device-mapper pseudo-disk devices */\ 311 x(RUMPD, 21, "rumpd") /* rump virtual disk */ \ 312 313 #ifndef _LOCORE 314 #define DKTYPE_NUMS(tag, number, name) __CONCAT(DTYPE_,tag=number), 315 #ifndef DKTYPE_ENUMNAME 316 #define DKTYPE_ENUMNAME 317 #endif 318 enum DKTYPE_ENUMNAME { DKTYPE_DEFN(DKTYPE_NUMS) DKMAXTYPES }; 319 #undef DKTYPE_NUMS 320 #endif 321 322 #ifdef DKTYPENAMES 323 #define DKTYPE_NAMES(tag, number, name) ARRAY_INIT(number,name), 324 static const char *const dktypenames[] = { DKTYPE_DEFN(DKTYPE_NAMES) NULL }; 325 #undef DKTYPE_NAMES 326 #endif 327 328 /* 329 * Partition type names, numbers, label-names, fsck prog, and mount prog 330 */ 331 #define FSTYPE_DEFN(x) \ 332 x(UNUSED, 0, "unused", NULL, NULL) /* unused */ \ 333 x(SWAP, 1, "swap", NULL, NULL) /* swap */ \ 334 x(V6, 2, "Version 6", NULL, NULL) /* Sixth Edition */ \ 335 x(V7, 3, "Version 7", "v7fs", "v7fs") /* Seventh Edition */ \ 336 x(SYSV, 4, "System V", NULL, NULL) /* System V */ \ 337 x(V71K, 5, "4.1BSD", NULL, NULL) /* V7, 1K blocks (4.1, 2.9) */ \ 338 x(V8, 6, "Eighth Edition",NULL, NULL) /* Eighth Edition, 4K blocks */ \ 339 x(BSDFFS, 7, "4.2BSD", "ffs", "ffs") /* 4.2BSD fast file system */ \ 340 x(MSDOS, 8, "MSDOS", "msdos", "msdos") /* MSDOS file system */ \ 341 x(BSDLFS, 9, "4.4LFS", "lfs", "lfs") /* 4.4BSD log-structured FS */ \ 342 x(OTHER, 10, "unknown", NULL, NULL) /* in use, unknown/unsupported */\ 343 x(HPFS, 11, "HPFS", NULL, NULL) /* OS/2 high-performance FS */ \ 344 x(ISO9660, 12, "ISO9660", NULL, "cd9660")/* ISO 9660, normally CD-ROM */ \ 345 x(BOOT, 13, "boot", NULL, NULL) /* bootstrap code in partition */\ 346 x(ADOS, 14, "ADOS", NULL, "ados") /* AmigaDOS fast file system */ \ 347 x(HFS, 15, "HFS", NULL, NULL) /* Macintosh HFS */ \ 348 x(FILECORE,16, "FILECORE", NULL, "filecore")/* Acorn Filecore FS */ \ 349 x(EX2FS, 17, "Linux Ext2","ext2fs","ext2fs")/* Linux Extended 2 FS */ \ 350 x(NTFS, 18, "NTFS", NULL, "ntfs") /* Windows/NT file system */ \ 351 x(RAID, 19, "RAID", NULL, NULL) /* RAIDframe component */ \ 352 x(CCD, 20, "ccd", NULL, NULL) /* concatenated disk component */\ 353 x(JFS2, 21, "jfs", NULL, NULL) /* IBM JFS2 */ \ 354 x(APPLEUFS,22, "Apple UFS", "ffs", "ffs") /* Apple UFS */ \ 355 /* XXX this is not the same as FreeBSD. How to solve? */ \ 356 x(VINUM, 23, "vinum", NULL, NULL) /* Vinum */ \ 357 x(UDF, 24, "UDF", NULL, "udf") /* UDF */ \ 358 x(SYSVBFS, 25, "SysVBFS", NULL, "sysvbfs")/* System V boot file system */ \ 359 x(EFS, 26, "EFS", NULL, "efs") /* SGI's Extent Filesystem */ \ 360 x(NILFS, 27, "NiLFS", NULL, "nilfs") /* NTT's NiLFS(2) */ \ 361 x(CGD, 28, "cgd", NULL, NULL) /* Cryptographic disk */ \ 362 x(MINIXFS3,29, "MINIX FSv3", NULL, NULL) /* MINIX file system v3 */ 363 364 365 #ifndef _LOCORE 366 #define FS_TYPENUMS(tag, number, name, fsck, mount) __CONCAT(FS_,tag=number), 367 #ifndef FSTYPE_ENUMNAME 368 #define FSTYPE_ENUMNAME 369 #endif 370 enum FSTYPE_ENUMNAME { FSTYPE_DEFN(FS_TYPENUMS) FSMAXTYPES }; 371 #undef FS_TYPENUMS 372 #endif 373 374 #ifdef FSTYPENAMES 375 #define FS_TYPENAMES(tag, number, name, fsck, mount) ARRAY_INIT(number,name), 376 static const char *const fstypenames[] = { FSTYPE_DEFN(FS_TYPENAMES) NULL }; 377 #undef FS_TYPENAMES 378 #endif 379 380 #ifdef FSCKNAMES 381 /* These are the names MOUNT_XXX from <sys/mount.h> */ 382 #define FS_FSCKNAMES(tag, number, name, fsck, mount) ARRAY_INIT(number,fsck), 383 static const char *const fscknames[] = { FSTYPE_DEFN(FS_FSCKNAMES) NULL }; 384 #undef FS_FSCKNAMES 385 #define FSMAXNAMES FSMAXTYPES 386 #endif 387 388 #ifdef MOUNTNAMES 389 /* These are the names MOUNT_XXX from <sys/mount.h> */ 390 #define FS_MOUNTNAMES(tag, number, name, fsck, mount) ARRAY_INIT(number,mount), 391 static const char *const mountnames[] = { FSTYPE_DEFN(FS_MOUNTNAMES) NULL }; 392 #undef FS_MOUNTNAMES 393 #define FSMAXMOUNTNAMES FSMAXTYPES 394 #endif 395 396 /* 397 * flags shared by various drives: 398 */ 399 #define D_REMOVABLE 0x01 /* removable media */ 400 #define D_ECC 0x02 /* supports ECC */ 401 #define D_BADSECT 0x04 /* supports bad sector forw. */ 402 #define D_RAMDISK 0x08 /* disk emulator */ 403 #define D_CHAIN 0x10 /* can do back-back transfers */ 404 #define D_SCSI_MMC 0x20 /* SCSI MMC sessioned media */ 405 406 /* 407 * Drive data for SMD. 408 */ 409 #define d_smdflags d_drivedata[0] 410 #define D_SSE 0x1 /* supports skip sectoring */ 411 #define d_mindist d_drivedata[1] 412 #define d_maxdist d_drivedata[2] 413 #define d_sdist d_drivedata[3] 414 415 /* 416 * Drive data for ST506. 417 */ 418 #define d_precompcyl d_drivedata[0] 419 #define d_gap3 d_drivedata[1] /* used only when formatting */ 420 421 /* 422 * Drive data for SCSI. 423 */ 424 #define d_blind d_drivedata[0] 425 426 #ifndef _LOCORE 427 /* 428 * Structure used to perform a format or other raw operation, 429 * returning data and/or register values. Register identification 430 * and format are device- and driver-dependent. Currently unused. 431 */ 432 struct format_op { 433 char *df_buf; 434 int df_count; /* value-result */ 435 daddr_t df_startblk; 436 int df_reg[8]; /* result */ 437 }; 438 439 #ifdef _KERNEL 440 /* 441 * Structure used internally to retrieve information about a partition 442 * on a disk. 443 */ 444 struct partinfo { 445 struct disklabel *disklab; 446 struct partition *part; 447 }; 448 449 struct disk; 450 451 int disk_read_sectors(void (*)(struct buf *), const struct disklabel *, 452 struct buf *, unsigned int, int); 453 void diskerr(const struct buf *, const char *, const char *, int, 454 int, const struct disklabel *); 455 u_int dkcksum(struct disklabel *); 456 u_int dkcksum_sized(struct disklabel *, size_t); 457 int setdisklabel(struct disklabel *, struct disklabel *, u_long, 458 struct cpu_disklabel *); 459 const char *readdisklabel(dev_t, void (*)(struct buf *), 460 struct disklabel *, struct cpu_disklabel *); 461 int writedisklabel(dev_t, void (*)(struct buf *), struct disklabel *, 462 struct cpu_disklabel *); 463 const char *convertdisklabel(struct disklabel *, void (*)(struct buf *), 464 struct buf *, uint32_t); 465 int bounds_check_with_label(struct disk *, struct buf *, int); 466 int bounds_check_with_mediasize(struct buf *, int, uint64_t); 467 const char *getfstypename(int); 468 #endif 469 #endif /* _LOCORE */ 470 471 #if !defined(_KERNEL) && !defined(_LOCORE) 472 473 #include <sys/cdefs.h> 474 475 #endif 476 477 #endif /* !_SYS_DISKLABEL_H_ */ 478