Merge from vendor branch FILE:
[dragonfly.git] / sys / sys / diskslice.h
1 /*
2  * Copyright (c) 2007 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
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  * 
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 /*-
35  * Copyright (c) 1994 Bruce D. Evans.
36  * All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
48  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
51  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57  * SUCH DAMAGE.
58  *
59  * $FreeBSD: src/sys/sys/diskslice.h,v 1.36.2.1 2001/01/29 01:50:50 ken Exp $
60  * $DragonFly: src/sys/sys/diskslice.h,v 1.16 2007/05/20 23:21:36 dillon Exp $
61  */
62
63 #ifndef _SYS_DISKSLICE_H_
64 #define _SYS_DISKSLICE_H_
65
66 #ifndef _SYS_TYPES_H_
67 #include <sys/types.h>
68 #endif
69 #ifndef _SYS_IOCCOM_H_
70 #include <sys/ioccom.h>
71 #endif
72 #if defined(_KERNEL) && !defined(_SYS_CONF_H_)
73 #include <sys/conf.h>           /* for make_sub_dev() */
74 #endif
75
76 #define BASE_SLICE              2       /* e.g. ad0s1 */
77 #define COMPATIBILITY_SLICE     0       /* e.g. ad0a-j */
78                                 /* 101 - compat disklabel DIOCGDINFO    */
79                                 /* 102 - compat disklabel DIOCSDINFO    */
80                                 /* 103 - compat disklabel DIOCWDINFO    */
81                                 /* 104 - DIOCGPART (see below)          */
82                                 /* 105 - compat disklabel DIOCGDVIRGIN  */
83 #define DIOCWLABEL              _IOW('d', 109, int)
84 #define DIOCGSLICEINFO          _IOR('d', 111, struct diskslices)
85 #define DIOCSYNCSLICEINFO       _IOW('d', 112, int)
86 #define MAX_SLICES              16
87
88 /*
89  * The whole-disk-slice does not try to interpret the MBR.  The whole slice
90  * partition does not try to interpret the disklabel within the slice.
91  */
92 #define WHOLE_DISK_SLICE        1
93 #define WHOLE_SLICE_PART        (DKMAXPARTITIONS - 1)
94
95 #ifdef MAXPARTITIONS                    /* XXX don't depend on disklabel.h */
96 #if MAXPARTITIONS !=    16              /* but check consistency if possible */
97 #error "inconsistent MAXPARTITIONS"
98 #endif
99 #else
100 #define MAXPARTITIONS   16
101 #endif
102
103 /*
104  * diskslice structure - slices up the disk and indicates where the
105  * BSD labels are, if any.
106  *
107  * ds_skip_platform  -  sectors reserved by the platform abstraction,
108  *                      typically to hold boot sectors and other junk.
109  *                      The BSD label is placed after the reserved sectors.
110  *
111  *                      This field is typically non-zero for dos slices.
112  *                      It will always be 0 for the whole-disk slice.
113  *                      
114  * ds_skip_bsdlabel  -  sectors reserved by the BSD label.  Always 0 when
115  *                      the disk is accessed via the whole-disk slice.
116  *
117  *                      This field includes any sectors reserved by the
118  *                      platform. e.g. in a dos slice the platform uses
119  *                      1 sector (the boot code sector) and the disklabel
120  *                      uses 15 sectors.  This field will be set to 16.
121  *                      
122  *                      This field would end up being set to one less for
123  *                      a directly labeled disk, at least for a standard
124  *                      bsd disklabel vs MBR + bsd disklabel.
125  */
126 struct diskslice {
127         u_int64_t       ds_offset;      /* starting sector */
128         u_int64_t       ds_size;        /* number of sectors */
129         u_int32_t       ds_skip_platform;       /* in sectors */
130         u_int32_t       ds_skip_bsdlabel;       /* in sectors */
131         int             ds_type;        /* (foreign) slice type */
132         struct disklabel *ds_label;     /* BSD label, if any */
133         void            *ds_dev;        /* devfs token for raw whole slice */
134         void            *ds_devs[MAXPARTITIONS]; /* XXX s.b. in label */
135         u_char          ds_openmask;    /* devs open */
136         u_char          ds_wlabel;      /* nonzero if label is writable */
137 };
138
139 struct diskslices {
140         struct cdevsw *dss_cdevsw;      /* for containing device */
141         int     dss_first_bsd_slice;    /* COMPATIBILITY_SLICE is mapped here */
142         u_int   dss_nslices;            /* actual dimension of dss_slices[] */
143         u_int   dss_oflags;             /* copy of flags for "first" open */
144         int     dss_secmult;            /* block to sector multiplier */
145         int     dss_secshift;           /* block to sector shift (or -1) */
146         int     dss_secsize;            /* sector size */
147         struct diskslice
148                 dss_slices[MAX_SLICES]; /* actually usually less */
149 };
150
151 /*
152  * DIOCGPART ioctl - returns information about a disk, slice, or partition.
153  * This ioctl is primarily used to get the block size and media size.
154  *
155  * NOTE: media_offset currently represents the byte offset on the raw device,
156  * it is not a partition relative offset.
157  *
158  * skip_platform and skip_bsdlabel work as with the diskslice
159  * structure.  For partitions within a disklabel these fields are usually
160  * 0 except for partitions which overlap the label or slice reserved area
161  * itself.  Those partitions will set these fields appropriately (relative
162  * to the partition).  In particular, the 'a' and 'c' partitions are
163  * protected.
164  */
165 struct partinfo {
166         u_int64_t       media_offset;   /* byte offset in parent layer */
167         u_int64_t       media_size;     /* media size in bytes */
168         u_int64_t       media_blocks;   /* media size in blocks */
169         int             media_blksize;  /* block size in bytes (sector size) */
170
171         u_int32_t       skip_platform;  /* in sectors */
172         u_int32_t       skip_bsdlabel;  /* in sectors */
173         int             fstype;         /* filesystem type if numeric */
174         char            fstypestr[16];  /* filesystem type as ascii */
175
176         /*
177          * These fields are loaded from the diskinfo structure
178          */
179         u_int           d_nheads;
180         u_int           d_ncylinders;
181         u_int           d_secpertrack;
182         u_int           d_secpercyl;
183         u_int           d_reserved[16];
184 };
185
186 #define DIOCGPART       _IOR('d', 104, struct partinfo) /* get partition */
187
188 /*
189  * disk unit and slice helper functions
190  *
191  *     3                   2                   1                   0
192  *   1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
193  *  _________________________________________________________________
194  *  | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
195  *  -----------------------------------------------------------------
196  *  | SL2 | PART3 |UNIT_2 |P| SLICE |  MAJOR?       |  UNIT   |PART |
197  *  -----------------------------------------------------------------
198  */
199
200 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
201
202 #define DKMAXUNITS      512
203 #define DKMAXSLICES     128
204 #define DKMAXPARTITIONS 256
205
206 /*
207  * Build a minor device number.
208  */
209 static __inline u_int32_t
210 dkmakeminor(u_int32_t unit, u_int32_t slice, u_int32_t part)
211 {
212         u_int32_t val;
213
214         val = ((unit & 0x001f) << 3) | ((unit & 0x01e0) << 16) |
215               ((slice & 0x000f) << 16) | ((slice & 0x0070) << 25) |
216               (part & 0x0007) | ((part & 0x0008) << 17) |
217               ((part & 0x00F0) << 21);
218         return(val);
219 }
220
221 /*
222  * Generate the minor number representing the entire disk, with no
223  * mbr or label interpretation.
224  */
225 static __inline u_int32_t
226 dkmakewholedisk(u_int32_t unit)
227 {
228         return(dkmakeminor(unit, WHOLE_DISK_SLICE, WHOLE_SLICE_PART));
229 }
230
231 /*
232  * Generate the minor number representing an entire slice, with no
233  * recursive mbr, boot sector, or label interpretation.
234  */
235 static __inline u_int32_t
236 dkmakewholeslice(u_int32_t unit, u_int32_t slice)
237 {
238         return(dkmakeminor(unit, slice, WHOLE_SLICE_PART));
239 }
240
241 /*
242  * Return the unit mask, used in calls to make_dev()
243  */
244 static __inline u_int32_t
245 dkunitmask(void)
246 {
247         return (0x01e000f8);
248 }
249
250 /*
251  * build minor number elements - encode unit number, slice, and partition
252  * (OR the results together).
253  */
254 static __inline u_int32_t
255 dkmakeunit(int unit)
256 {
257         return(dkmakeminor((u_int32_t)unit, 0, 0));
258 }
259
260 static __inline u_int32_t
261 dkmakeslice(int slice)
262 {
263         return(dkmakeminor(0, (u_int32_t)slice, 0));
264 }
265
266 static __inline u_int32_t
267 dkmakepart(int part)
268 {
269         return(dkmakeminor(0, 0, (u_int32_t)part));
270 }
271
272 #endif
273
274 /*
275  * dk*() support functions operating on cdev_t's
276  */
277 #ifdef _KERNEL
278
279 static __inline int
280 dkunit(cdev_t dev)
281 {
282         u_int32_t val = minor(dev);
283
284         val = ((val >> 3) & 0x001f) | ((val >> 16) & 0x01e0);
285         return((int)val);
286 }
287
288 static __inline u_int32_t
289 dkslice(cdev_t dev)
290 {
291         u_int32_t val = minor(dev);
292
293         val = ((val >> 16) & 0x000f) | ((val >> 25) & 0x0070);
294         return(val);
295 }
296
297 static __inline u_int32_t
298 dkpart(cdev_t dev)
299 {
300         u_int32_t val = minor(dev);
301
302         val = (val & 0x0007) | ((val >> 17) & 0x0008) | ((val >> 21) & 0x00f0);
303         return(val);
304 }
305
306 /*
307  * dkmodpart() - create sub-device
308  */
309 static __inline cdev_t
310 dkmodpart(cdev_t dev, int part)
311 {
312         u_int32_t val;
313
314         val = (minor(dev) & ~dkmakepart(-1)) | dkmakepart(part);
315         return (make_sub_dev(dev, val));
316 }
317
318 static __inline cdev_t
319 dkmodslice(cdev_t dev, int slice)
320 {
321         u_int32_t val;
322
323         val = (minor(dev) & ~dkmakeslice(-1)) | dkmakeslice(slice);
324         return (make_sub_dev(dev, val));
325 }
326
327 #endif
328
329 /*
330  * disk management functions
331  */
332
333 #ifdef _KERNEL
334
335 struct buf;
336 struct bio;
337 struct disklabel;
338 struct disk_info;
339 struct bio_queue_head;
340
341 int     mbrinit (cdev_t dev, struct disk_info *info,
342                     struct diskslices **sspp);
343 struct bio *
344         dscheck (cdev_t dev, struct bio *bio, struct diskslices *ssp);
345 void    dsclose (cdev_t dev, int mode, struct diskslices *ssp);
346 void    dsgone (struct diskslices **sspp);
347 int     dsioctl (cdev_t dev, u_long cmd, caddr_t data, int flags,
348                     struct diskslices **sspp, struct disk_info *info);
349 int     dsisopen (struct diskslices *ssp);
350 struct diskslices *
351         dsmakeslicestruct (int nslices, struct disk_info *info);
352 char    *dsname (cdev_t dev, int unit, int slice, int part,
353                     char *partname);
354 int     dsopen (cdev_t dev, int mode, u_int flags,
355                     struct diskslices **sspp, struct disk_info *info);
356 int64_t dssize (cdev_t dev, struct diskslices **sspp);
357
358 /*
359  * Ancillary functions
360  */
361
362 void    diskerr (struct bio *bio, cdev_t dev, const char *what, int pri,
363                     int donecnt);
364 void    disksort (struct buf *ap, struct buf *bp);
365 void    bioqdisksort (struct bio_queue_head *ap, struct bio *bio);
366
367 #endif /* _KERNEL */
368
369 #endif /* !_SYS_DISKSLICE_H_ */