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