xref: /netbsd-src/sys/sys/disk.h (revision d48f14661dda8638fee055ba15d35bdfb29b9fa8)
1 /*	$NetBSD: disk.h,v 1.36 2006/04/21 13:53:30 yamt 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 
95 struct buf;
96 struct disk;
97 struct disklabel;
98 struct cpu_disklabel;
99 struct vnode;
100 
101 /*
102  * dkwedge_info:
103  *
104  *	Information needed to configure (or query configuration of) a
105  *	disk wedge.
106  */
107 struct dkwedge_info {
108 	char		dkw_devname[16];/* device-style name (e.g. "dk0") */
109 	uint8_t		dkw_wname[128];	/* wedge name (Unicode, UTF-8) */
110 	char		dkw_parent[16];	/* parent disk device name */
111 	daddr_t		dkw_offset;	/* LBA offset of wedge in parent */
112 	uint64_t	dkw_size;	/* size of wedge in blocks */
113 	char		dkw_ptype[32];	/* partition type string */
114 };
115 
116 /*
117  * dkwedge_list:
118  *
119  *	Structure used to query a list of wedges.
120  */
121 struct dkwedge_list {
122 	void		*dkwl_buf;	/* storage for dkwedge_info array */
123 	size_t		dkwl_bufsize;	/* size of that buffer */
124 	u_int		dkwl_nwedges;	/* total number of wedges */
125 	u_int		dkwl_ncopied;	/* number actually copied */
126 };
127 
128 #ifdef _KERNEL
129 /*
130  * dkwedge_discovery_method:
131  *
132  *	Structure used to describe partition map parsing schemes
133  *	used for wedge autodiscovery.
134  */
135 struct dkwedge_discovery_method {
136 					/* link in wedge driver's list */
137 	LIST_ENTRY(dkwedge_discovery_method) ddm_list;
138 	const char	*ddm_name;	/* name of this method */
139 	int		ddm_priority;	/* search priority */
140 	int		(*ddm_discover)(struct disk *, struct vnode *);
141 };
142 
143 #define	DKWEDGE_DISCOVERY_METHOD_DECL(name, prio, discover)		\
144 static struct dkwedge_discovery_method name ## _ddm = {			\
145 	{ 0 },								\
146 	#name,								\
147 	prio,								\
148 	discover							\
149 };									\
150 __link_set_add_data(dkwedge_methods, name ## _ddm)
151 #endif /* _KERNEL */
152 
153 /* Some common partition types */
154 #define	DKW_PTYPE_UNKNOWN	""
155 #define	DKW_PTYPE_UNUSED	"unused"
156 #define	DKW_PTYPE_SWAP		"swap"
157 #define	DKW_PTYPE_FFS		"ffs"
158 #define	DKW_PTYPE_LFS		"lfs"
159 #define	DKW_PTYPE_EXT2FS	"ext2fs"
160 #define	DKW_PTYPE_ISO9660	"cd9660"
161 #define	DKW_PTYPE_AMIGADOS	"ados"
162 #define	DKW_PTYPE_APPLEHFS	"hfs"
163 #define	DKW_PTYPE_FAT		"msdos"
164 #define	DKW_PTYPE_FILECORE	"filecore"
165 #define	DKW_PTYPE_RAIDFRAME	"raidframe"
166 #define	DKW_PTYPE_CCD		"ccd"
167 #define	DKW_PTYPE_APPLEUFS	"appleufs"
168 #define	DKW_PTYPE_NTFS		"ntfs"
169 
170 /*
171  * Disk geometry information.
172  *
173  * NOTE: Not all geometry information is relevant for every kind of disk.
174  */
175 struct disk_geom {
176 	int64_t		dg_secperunit;	/* # of data sectors per unit */
177 	uint32_t	dg_secsize;	/* # of bytes per sector */
178 	uint32_t	dg_nsectors;	/* # of data sectors per track */
179 	uint32_t	dg_ntracks;	/* # of tracks per cylinder */
180 	uint32_t	dg_ncylinders;	/* # of data cylinders per unit */
181 	uint32_t	dg_secpercyl;	/* # of data sectors per cylinder */
182 	uint32_t	dg_pcylinders;	/* # of physical cylinders per unit */
183 
184 	/*
185 	 * Spares (bad sector replacements) below are not counted in
186 	 * dg_nsectors or dg_secpercyl.  Spare sectors are assumed to
187 	 * be physical sectors which occupy space at the end of each
188 	 * track and/or cylinder.
189 	 */
190 	uint32_t	dg_sparespertrack;
191 	uint32_t	dg_sparespercyl;
192 	/*
193 	 * Alternative cylinders include maintenance, replacement,
194 	 * configuration description areas, etc.
195 	 */
196 	uint32_t	dg_acylinders;
197 };
198 
199 struct disk {
200 	TAILQ_ENTRY(disk) dk_link;	/* link in global disklist */
201 	char		*dk_name;	/* disk name */
202 	int		dk_bopenmask;	/* block devices open */
203 	int		dk_copenmask;	/* character devices open */
204 	int		dk_openmask;	/* composite (bopen|copen) */
205 	int		dk_state;	/* label state   ### */
206 	int		dk_blkshift;	/* shift to convert DEV_BSIZE to blks */
207 	int		dk_byteshift;	/* shift to convert bytes to blks */
208 
209 	/*
210 	 * Metrics data; note that some metrics may have no meaning
211 	 * on certain types of disks.
212 	 */
213 	struct io_stats	*dk_stats;
214 
215 	struct	dkdriver *dk_driver;	/* pointer to driver */
216 
217 	/*
218 	 * Information required to be the parent of a disk wedge.
219 	 */
220 	struct lock	dk_rawlock;	/* lock on these fields */
221 	struct vnode	*dk_rawvp;	/* vnode for the RAW_PART bdev */
222 	u_int		dk_rawopens;	/* # of openes of rawvp */
223 
224 	struct lock	dk_openlock;	/* lock on these and openmask */
225 	u_int		dk_nwedges;	/* # of configured wedges */
226 					/* all wedges on this disk */
227 	LIST_HEAD(, dkwedge_softc) dk_wedges;
228 
229 	/*
230 	 * Disk label information.  Storage for the in-core disk label
231 	 * must be dynamically allocated, otherwise the size of this
232 	 * structure becomes machine-dependent.
233 	 */
234 	daddr_t		dk_labelsector;		/* sector containing label */
235 	struct disklabel *dk_label;	/* label */
236 	struct cpu_disklabel *dk_cpulabel;
237 };
238 
239 struct dkdriver {
240 	void	(*d_strategy)(struct buf *);
241 	void	(*d_minphys)(struct buf *);
242 #ifdef notyet
243 	int	(*d_open)(dev_t, int, int, struct proc *);
244 	int	(*d_close)(dev_t, int, int, struct proc *);
245 	int	(*d_ioctl)(dev_t, u_long, caddr_t, int, struct proc *);
246 	int	(*d_dump)(dev_t);
247 	void	(*d_start)(struct buf *, daddr_t);
248 	int	(*d_mklabel)(struct disk *);
249 #endif
250 };
251 
252 /* states */
253 #define	DK_CLOSED	0		/* drive is closed */
254 #define	DK_WANTOPEN	1		/* drive being opened */
255 #define	DK_WANTOPENRAW	2		/* drive being opened */
256 #define	DK_RDLABEL	3		/* label being read */
257 #define	DK_OPEN		4		/* label read, drive open */
258 #define	DK_OPENRAW	5		/* open without label */
259 
260 /*
261  * Bad sector lists per fixed disk
262  */
263 struct disk_badsectors {
264 	SLIST_ENTRY(disk_badsectors)	dbs_next;
265 	daddr_t		dbs_min;	/* min. sector number */
266 	daddr_t		dbs_max;	/* max. sector number */
267 	struct timeval	dbs_failedat;	/* first failure at */
268 };
269 
270 struct disk_badsecinfo {
271 	uint32_t	dbsi_bufsize;	/* size of region pointed to */
272 	uint32_t	dbsi_skip;	/* how many to skip past */
273 	uint32_t	dbsi_copied;	/* how many got copied back */
274 	uint32_t	dbsi_left;	/* remaining to copy */
275 	caddr_t		dbsi_buffer;	/* region to copy disk_badsectors to */
276 };
277 
278 #define	DK_STRATEGYNAMELEN	32
279 struct disk_strategy {
280 	char dks_name[DK_STRATEGYNAMELEN]; /* name of strategy */
281 	char *dks_param;		/* notyet; should be NULL */
282 	size_t dks_paramlen;		/* notyet; should be 0 */
283 };
284 
285 #ifdef _KERNEL
286 extern	int disk_count;			/* number of disks in global disklist */
287 
288 struct device;
289 struct proc;
290 
291 void	disk_attach(struct disk *);
292 void	disk_detach(struct disk *);
293 void	pseudo_disk_init(struct disk *);
294 void	pseudo_disk_attach(struct disk *);
295 void	pseudo_disk_detach(struct disk *);
296 void	disk_busy(struct disk *);
297 void	disk_unbusy(struct disk *, long, int);
298 struct disk *disk_find(const char *);
299 
300 int	dkwedge_add(struct dkwedge_info *);
301 int	dkwedge_del(struct dkwedge_info *);
302 void	dkwedge_delall(struct disk *);
303 int	dkwedge_list(struct disk *, struct dkwedge_list *, struct lwp *);
304 void	dkwedge_discover(struct disk *);
305 void	dkwedge_set_bootwedge(struct device *, daddr_t, uint64_t);
306 int	dkwedge_read(struct disk *, struct vnode *, daddr_t, void *, size_t);
307 #endif
308 
309 #endif /* _SYS_DISK_H_ */
310