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