xref: /netbsd-src/sys/sys/disk.h (revision 1ad9454efb13a65cd7535ccf867508cb14d9d30e)
1 /*	$NetBSD: disk.h,v 1.38 2006/09/22 04:48:38 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996, 1997, 2004 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * Copyright (c) 1992, 1993
42  *	The Regents of the University of California.  All rights reserved.
43  *
44  * This software was developed by the Computer Systems Engineering group
45  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
46  * contributed to Berkeley.
47  *
48  * All advertising materials mentioning features or use of this software
49  * must display the following acknowledgement:
50  *	This product includes software developed by the University of
51  *	California, Lawrence Berkeley Laboratories.
52  *
53  * Redistribution and use in source and binary forms, with or without
54  * modification, are permitted provided that the following conditions
55  * are met:
56  * 1. Redistributions of source code must retain the above copyright
57  *    notice, this list of conditions and the following disclaimer.
58  * 2. Redistributions in binary form must reproduce the above copyright
59  *    notice, this list of conditions and the following disclaimer in the
60  *    documentation and/or other materials provided with the distribution.
61  * 3. Neither the name of the University nor the names of its contributors
62  *    may be used to endorse or promote products derived from this software
63  *    without specific prior written permission.
64  *
65  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
66  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
67  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
68  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
69  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
70  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
71  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
72  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
73  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
74  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
75  * SUCH DAMAGE.
76  *
77  * from: Header: disk.h,v 1.5 92/11/19 04:33:03 torek Exp  (LBL)
78  *
79  *	@(#)disk.h	8.2 (Berkeley) 1/9/95
80  */
81 
82 #ifndef _SYS_DISK_H_
83 #define _SYS_DISK_H_
84 
85 /*
86  * Disk device structures.
87  */
88 
89 #include <sys/dkio.h>
90 #include <sys/time.h>
91 #include <sys/queue.h>
92 #include <sys/lock.h>
93 #include <sys/iostat.h>
94 #include <prop/proplib.h>
95 
96 struct buf;
97 struct disk;
98 struct disklabel;
99 struct cpu_disklabel;
100 struct vnode;
101 
102 /*
103  * Disk information dictionary.
104  *
105  * This contains general infomation for disk devices.
106  *
107  *	<dict>
108  *		<key>type</key>
109  *		<string>...</string>
110  *		<key>geometry</key>
111  *		<dict>
112  *			<!-- See below for disk geometry dictionary
113  *			     contents. -->
114  *		</dict>
115  *
116  *		<!-- optional information -->
117  *		<key>rpm</key>
118  *		<integer>...</integer>
119  *		<key>sector-interleave</key>
120  *		<integer>...</integer>
121  *		<key>track-skew</key>
122  *		<integer>...</integer>
123  *		<key>cylinder-skew</key>
124  *		<integer>...</integer>
125  *		<key>head-switch-usecs</key>
126  *		<integer>...</integer>
127  *		<key>track-seek-usecs</key>
128  *		<integer>...</integer>
129  *		<key>removable</key>
130  *		<false/>
131  *		<key>ecc</key>
132  *		<false/>
133  *		<key>bad-sector-forwarding</key>
134  *		<true/>
135  *		<key>ramdisk</key>
136  *		<false/>
137  *		<key>back-to-back-transfers</key>
138  *		<true/>
139  *
140  *		<!-- additional information for SMD drives -->
141  *		<key>smd-skip-sectoring</key>
142  *		<false/>
143  *		<!-- XXX better names for these properties -->
144  *		<key>smd-mindist</key>
145  *		<integer>...</integer>
146  *		<key>smd-maxdist</key>
147  *		<integer>...</integer>
148  *		<key>smd-sdist</key>
149  *		<integer>...</integer>
150  *
151  *		<!-- additional information for ST506 drives -->
152  *		<!-- XXX better names for these properties -->
153  *		<key>st506-precompcyl</key>
154  *		<integer>...</integer>
155  *		<key>st506-gap3</key>
156  *		<integer>...</integer>
157  *
158  *		<!-- additional information for ATA drives -->
159  *		<!-- XXX -->
160  *
161  *		<!-- additional information for SCSI drives -->
162  *		<!-- XXX -->
163  *	</dict>
164  */
165 
166 /*
167  * dkwedge_info:
168  *
169  *	Information needed to configure (or query configuration of) a
170  *	disk wedge.
171  */
172 struct dkwedge_info {
173 	char		dkw_devname[16];/* device-style name (e.g. "dk0") */
174 	uint8_t		dkw_wname[128];	/* wedge name (Unicode, UTF-8) */
175 	char		dkw_parent[16];	/* parent disk device name */
176 	daddr_t		dkw_offset;	/* LBA offset of wedge in parent */
177 	uint64_t	dkw_size;	/* size of wedge in blocks */
178 	char		dkw_ptype[32];	/* partition type string */
179 };
180 
181 /*
182  * dkwedge_list:
183  *
184  *	Structure used to query a list of wedges.
185  */
186 struct dkwedge_list {
187 	void		*dkwl_buf;	/* storage for dkwedge_info array */
188 	size_t		dkwl_bufsize;	/* size of that buffer */
189 	u_int		dkwl_nwedges;	/* total number of wedges */
190 	u_int		dkwl_ncopied;	/* number actually copied */
191 };
192 
193 #ifdef _KERNEL
194 /*
195  * dkwedge_discovery_method:
196  *
197  *	Structure used to describe partition map parsing schemes
198  *	used for wedge autodiscovery.
199  */
200 struct dkwedge_discovery_method {
201 					/* link in wedge driver's list */
202 	LIST_ENTRY(dkwedge_discovery_method) ddm_list;
203 	const char	*ddm_name;	/* name of this method */
204 	int		ddm_priority;	/* search priority */
205 	int		(*ddm_discover)(struct disk *, struct vnode *);
206 };
207 
208 #define	DKWEDGE_DISCOVERY_METHOD_DECL(name, prio, discover)		\
209 static struct dkwedge_discovery_method name ## _ddm = {			\
210 	{ NULL, NULL },							\
211 	#name,								\
212 	prio,								\
213 	discover							\
214 };									\
215 __link_set_add_data(dkwedge_methods, name ## _ddm)
216 #endif /* _KERNEL */
217 
218 /* Some common partition types */
219 #define	DKW_PTYPE_UNKNOWN	""
220 #define	DKW_PTYPE_UNUSED	"unused"
221 #define	DKW_PTYPE_SWAP		"swap"
222 #define	DKW_PTYPE_FFS		"ffs"
223 #define	DKW_PTYPE_LFS		"lfs"
224 #define	DKW_PTYPE_EXT2FS	"ext2fs"
225 #define	DKW_PTYPE_ISO9660	"cd9660"
226 #define	DKW_PTYPE_AMIGADOS	"ados"
227 #define	DKW_PTYPE_APPLEHFS	"hfs"
228 #define	DKW_PTYPE_FAT		"msdos"
229 #define	DKW_PTYPE_FILECORE	"filecore"
230 #define	DKW_PTYPE_RAIDFRAME	"raidframe"
231 #define	DKW_PTYPE_CCD		"ccd"
232 #define	DKW_PTYPE_APPLEUFS	"appleufs"
233 #define	DKW_PTYPE_NTFS		"ntfs"
234 
235 /*
236  * Disk geometry dictionary.
237  *
238  * NOTE: Not all geometry information is relevant for every kind of disk.
239  *
240  *	<dict>
241  *		<key>sectors-per-unit</key>
242  *		<integer>...</integer>
243  *		<key>sector-size</key>
244  *		<integer>...</integer>
245  *		<key>sectors-per-track</key>
246  *		<integer>...</integer>
247  *		<key>tracks-per-cylinder</key>
248  *		<integer>...</integer>
249  *		<key>cylinders-per-unit</key>
250  *		<integer>...</integer>
251  *		<key>physical-cylinders-per-unit</key>
252  *		<integer>...</integer>
253  *		<key>spare-sectors-per-track</key>
254  *		<integer>...</integer>
255  *		<key>spare-sectors-per-cylinder</key>
256  *		<integer>...</integer>
257  *		<key>alternative-cylinders</key>
258  *		<integer>...</integer>
259  *	</dict>
260  * NOTE: Not all geometry information is relevant for every kind of disk.
261  */
262 
263 struct disk_geom {
264 	int64_t		dg_secperunit;	/* # of data sectors per unit */
265 	uint32_t	dg_secsize;	/* # of bytes per sector */
266 	uint32_t	dg_nsectors;	/* # of data sectors per track */
267 	uint32_t	dg_ntracks;	/* # of tracks per cylinder */
268 	uint32_t	dg_ncylinders;	/* # of data cylinders per unit */
269 	uint32_t	dg_secpercyl;	/* # of data sectors per cylinder */
270 	uint32_t	dg_pcylinders;	/* # of physical cylinders per unit */
271 
272 	/*
273 	 * Spares (bad sector replacements) below are not counted in
274 	 * dg_nsectors or dg_secpercyl.  Spare sectors are assumed to
275 	 * be physical sectors which occupy space at the end of each
276 	 * track and/or cylinder.
277 	 */
278 	uint32_t	dg_sparespertrack;
279 	uint32_t	dg_sparespercyl;
280 	/*
281 	 * Alternative cylinders include maintenance, replacement,
282 	 * configuration description areas, etc.
283 	 */
284 	uint32_t	dg_acylinders;
285 };
286 
287 /*
288  * Disk partition dictionary.
289  *
290  * A partition is represented as a dictionary containing generic partition
291  * properties (such as starting block and block count), as well as information
292  * that is specific to individual partition map formats.
293  *
294  *	<dict>
295  *		<key>start-block</key>
296  *		<integer>...</integer>
297  *		<key>block-count</key>
298  *		<integer>...</integer>
299  *		<!-- DKW_PTYPE strings ("" or missing if unknown) -->
300  *		<key>type</type>
301  *		<string>...</string>
302  *		<!-- optional -->
303  *		<key>name</key>
304  *		<string>...</string>
305  *
306  *		<!-- these are valid for GPT partition maps -->
307  *		<key>gpt-type-guid</key>
308  *		<string>...</string>
309  *		<key>gpt-partition-guid</key>
310  *		<string>...</string>
311  *		<key>gpt-platform-required</key>
312  *		<false/>
313  *
314  *		<!-- these are valid for 4.4BSD partition maps -->
315  *		<key>bsd44-partition-type</key>
316  *		<integer>...</integer>
317  *		<key>bsd44-fs-fragment-size</key>
318  *		<integer>...</integer>
319  *		<key>bsd44-iso9660-session-offset</key>
320  *		<integer>...</integer>
321  *		<key>bsd44-ffs-cylinders-per-group</key>
322  *		<integer>...</integer>
323  *		<key>bsd44-lfs-segment-shift</key>
324  *		<integer>...</integer>
325  *
326  *		<!-- these are valid for NeXT partition maps -->
327  *		<key>next-block-size</key>
328  *		<integer>...</integer>
329  *		<key>next-fs-fragment-size</key>
330  *		<integer>...</integer>
331  *		<key>next-fs-optimization</key>
332  *		<string>...</string>	<!-- "space" or "time" -->
333  *		<key>next-fs-cylinders-per-group</key>
334  *		<integer>...</integer>
335  *		<key>next-bytes-per-inode-density</key>
336  *		<integer>...</integer>
337  *		<key>next-minfree-percentage</key>
338  *		<integer>...</integer>
339  *		<key>next-run-newfs-during-init</key>
340  *		<false/>
341  *		<key>next-mount-point</key>
342  *		<string>...</string>
343  *		<key>next-automount</key>
344  *		<true/>
345  *		<key>next-partition-type</key>
346  *		<string>...</string>
347  *
348  *		<!-- these are valid for MBR partition maps -->
349  *		<key>mbr-start-head</key>
350  *		<integer>...</integer>
351  *		<key>mbr-start-sector</key>
352  *		<integer>...</integer>
353  *		<key>mbr-start-cylinder</key>
354  *		<integer>...</integer>
355  *		<key>mbr-partition-type</key>
356  *		<integer>...</integer>
357  *		<key>mbr-end-head</key>
358  *		<integer>...</integer>
359  *		<key>mbr-end-sector</key>
360  *		<integer>...</integer>
361  *		<key>mbr-end-cylinder</key>
362  *		<integer>...</integer>
363  *		<key>mbr-active-partition</key>
364  *		<false/>
365  *
366  *		<!-- these are valid for Apple partition maps -->
367  *		<key>apple-partition-type</key>
368  *		<string>...</string>
369  *		<!-- XXX What else do we need?  wrstuden? -->
370  *
371  *		<!-- these are valid for RISCiX partition maps -->
372  *		<key>riscix-partition-type</key>
373  *		<integer>...</integer>
374  *
375  *		<!-- these are valid for MIPS/SGI partition maps -->
376  *		<key>mips-partition-type</key>
377  *		<integer>...</integer>
378  *
379  *		<!-- SunOS 4 partition maps have no specific
380  *		     additional information.  Note, however,
381  *		     that SunOS 4 partitions must begin on
382  *		     cylinder boundaries. -->
383  *
384  *		<!-- XXX Need Amiga partition map info -->
385  *
386  *		<!-- these are valid for VTOC partition maps -->
387  *		<key>vtoc-tag</key>
388  *		<integer>...</integer>
389  *		<key>vtoc-unmount</key>
390  *		<false/>
391  *		<key>vtoc-read-only</key>
392  *		<false/>
393  *		<!-- XXX is this really part of the partition info? -->
394  *		<key>vtoc-timestamp</key>
395  *		<integer>...</integer>
396  *
397  *		<!-- mvme68k partition maps use 4.4BSD partition
398  *		     info stuffed into two different areas of the
399  *		     disk information label recognized by BUG. -->
400  *
401  *		<!-- XXX What else? -->
402  *	</dict>
403  */
404 
405 struct disk {
406 	TAILQ_ENTRY(disk) dk_link;	/* link in global disklist */
407 	char		*dk_name;	/* disk name */
408 	prop_dictionary_t dk_info;	/* reference to disk-info dictionary */
409 	int		dk_bopenmask;	/* block devices open */
410 	int		dk_copenmask;	/* character devices open */
411 	int		dk_openmask;	/* composite (bopen|copen) */
412 	int		dk_state;	/* label state   ### */
413 	int		dk_blkshift;	/* shift to convert DEV_BSIZE to blks */
414 	int		dk_byteshift;	/* shift to convert bytes to blks */
415 
416 	/*
417 	 * Metrics data; note that some metrics may have no meaning
418 	 * on certain types of disks.
419 	 */
420 	struct io_stats	*dk_stats;
421 
422 	struct	dkdriver *dk_driver;	/* pointer to driver */
423 
424 	/*
425 	 * Information required to be the parent of a disk wedge.
426 	 */
427 	struct lock	dk_rawlock;	/* lock on these fields */
428 	struct vnode	*dk_rawvp;	/* vnode for the RAW_PART bdev */
429 	u_int		dk_rawopens;	/* # of openes of rawvp */
430 
431 	struct lock	dk_openlock;	/* lock on these and openmask */
432 	u_int		dk_nwedges;	/* # of configured wedges */
433 					/* all wedges on this disk */
434 	LIST_HEAD(, dkwedge_softc) dk_wedges;
435 
436 	/*
437 	 * Disk label information.  Storage for the in-core disk label
438 	 * must be dynamically allocated, otherwise the size of this
439 	 * structure becomes machine-dependent.
440 	 */
441 	daddr_t		dk_labelsector;		/* sector containing label */
442 	struct disklabel *dk_label;	/* label */
443 	struct cpu_disklabel *dk_cpulabel;
444 };
445 
446 struct dkdriver {
447 	void	(*d_strategy)(struct buf *);
448 	void	(*d_minphys)(struct buf *);
449 #ifdef notyet
450 	int	(*d_open)(dev_t, int, int, struct proc *);
451 	int	(*d_close)(dev_t, int, int, struct proc *);
452 	int	(*d_ioctl)(dev_t, u_long, caddr_t, int, struct proc *);
453 	int	(*d_dump)(dev_t);
454 	void	(*d_start)(struct buf *, daddr_t);
455 	int	(*d_mklabel)(struct disk *);
456 #endif
457 };
458 
459 /* states */
460 #define	DK_CLOSED	0		/* drive is closed */
461 #define	DK_WANTOPEN	1		/* drive being opened */
462 #define	DK_WANTOPENRAW	2		/* drive being opened */
463 #define	DK_RDLABEL	3		/* label being read */
464 #define	DK_OPEN		4		/* label read, drive open */
465 #define	DK_OPENRAW	5		/* open without label */
466 
467 /*
468  * Bad sector lists per fixed disk
469  */
470 struct disk_badsectors {
471 	SLIST_ENTRY(disk_badsectors)	dbs_next;
472 	daddr_t		dbs_min;	/* min. sector number */
473 	daddr_t		dbs_max;	/* max. sector number */
474 	struct timeval	dbs_failedat;	/* first failure at */
475 };
476 
477 struct disk_badsecinfo {
478 	uint32_t	dbsi_bufsize;	/* size of region pointed to */
479 	uint32_t	dbsi_skip;	/* how many to skip past */
480 	uint32_t	dbsi_copied;	/* how many got copied back */
481 	uint32_t	dbsi_left;	/* remaining to copy */
482 	caddr_t		dbsi_buffer;	/* region to copy disk_badsectors to */
483 };
484 
485 #define	DK_STRATEGYNAMELEN	32
486 struct disk_strategy {
487 	char dks_name[DK_STRATEGYNAMELEN]; /* name of strategy */
488 	char *dks_param;		/* notyet; should be NULL */
489 	size_t dks_paramlen;		/* notyet; should be 0 */
490 };
491 
492 #ifdef _KERNEL
493 extern	int disk_count;			/* number of disks in global disklist */
494 
495 struct device;
496 struct proc;
497 
498 void	disk_attach(struct disk *);
499 void	disk_detach(struct disk *);
500 void	pseudo_disk_init(struct disk *);
501 void	pseudo_disk_attach(struct disk *);
502 void	pseudo_disk_detach(struct disk *);
503 void	disk_busy(struct disk *);
504 void	disk_unbusy(struct disk *, long, int);
505 struct disk *disk_find(const char *);
506 
507 int	dkwedge_add(struct dkwedge_info *);
508 int	dkwedge_del(struct dkwedge_info *);
509 void	dkwedge_delall(struct disk *);
510 int	dkwedge_list(struct disk *, struct dkwedge_list *, struct lwp *);
511 void	dkwedge_discover(struct disk *);
512 void	dkwedge_set_bootwedge(struct device *, daddr_t, uint64_t);
513 int	dkwedge_read(struct disk *, struct vnode *, daddr_t, void *, size_t);
514 #endif
515 
516 #endif /* _SYS_DISK_H_ */
517