Merge from vendor branch LIBARCHIVE:
[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.17 2007/06/01 00:25:23 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)
73 #ifndef _SYS_CONF_H_
74 #include <sys/conf.h>           /* for make_sub_dev() */
75 #endif
76 #ifndef _SYS_SYSTM_H_
77 #include <sys/systm.h>          /* for minor() */
78 #endif
79 #endif
80
81 #define BASE_SLICE              2       /* e.g. ad0s1 */
82 #define COMPATIBILITY_SLICE     0       /* e.g. ad0a-j */
83                                 /* 101 - compat disklabel DIOCGDINFO    */
84                                 /* 102 - compat disklabel DIOCSDINFO    */
85                                 /* 103 - compat disklabel DIOCWDINFO    */
86                                 /* 104 - DIOCGPART (see below)          */
87                                 /* 105 - compat disklabel DIOCGDVIRGIN  */
88 #define DIOCWLABEL              _IOW('d', 109, int)
89 #define DIOCGSLICEINFO          _IOR('d', 111, struct diskslices)
90 #define DIOCSYNCSLICEINFO       _IOW('d', 112, int)
91 #define MAX_SLICES              16
92
93 /*
94  * The whole-disk-slice does not try to interpret the MBR.  The whole slice
95  * partition does not try to interpret the disklabel within the slice.
96  */
97 #define WHOLE_DISK_SLICE        1
98 #define WHOLE_SLICE_PART        (DKMAXPARTITIONS - 1)
99
100 #ifdef MAXPARTITIONS                    /* XXX don't depend on disklabel.h */
101 #if MAXPARTITIONS !=    16              /* but check consistency if possible */
102 #error "inconsistent MAXPARTITIONS"
103 #endif
104 #else
105 #define MAXPARTITIONS   16
106 #endif
107
108 /*
109  * diskslice structure - slices up the disk and indicates where the
110  * BSD labels are, if any.
111  *
112  * ds_skip_platform  -  sectors reserved by the platform abstraction,
113  *                      typically to hold boot sectors and other junk.
114  *                      The BSD label is placed after the reserved sectors.
115  *
116  *                      This field is typically non-zero for dos slices.
117  *                      It will always be 0 for the whole-disk slice.
118  *                      
119  * ds_skip_bsdlabel  -  sectors reserved by the BSD label.  Always 0 when
120  *                      the disk is accessed via the whole-disk slice.
121  *
122  *                      This field includes any sectors reserved by the
123  *                      platform. e.g. in a dos slice the platform uses
124  *                      1 sector (the boot code sector) and the disklabel
125  *                      uses 15 sectors.  This field will be set to 16.
126  *                      
127  *                      This field would end up being set to one less for
128  *                      a directly labeled disk, at least for a standard
129  *                      bsd disklabel vs MBR + bsd disklabel.
130  */
131 struct diskslice {
132         u_int64_t       ds_offset;      /* starting sector */
133         u_int64_t       ds_size;        /* number of sectors */
134         u_int32_t       ds_skip_platform;       /* in sectors */
135         u_int32_t       ds_skip_bsdlabel;       /* in sectors */
136         int             ds_type;        /* (foreign) slice type */
137         struct disklabel *ds_label;     /* BSD label, if any */
138         void            *ds_dev;        /* devfs token for raw whole slice */
139         void            *ds_devs[MAXPARTITIONS]; /* XXX s.b. in label */
140         u_char          ds_openmask;    /* devs open */
141         u_char          ds_wlabel;      /* nonzero if label is writable */
142 };
143
144 struct diskslices {
145         struct cdevsw *dss_cdevsw;      /* for containing device */
146         int     dss_first_bsd_slice;    /* COMPATIBILITY_SLICE is mapped here */
147         u_int   dss_nslices;            /* actual dimension of dss_slices[] */
148         u_int   dss_oflags;             /* copy of flags for "first" open */
149         int     dss_secmult;            /* block to sector multiplier */
150         int     dss_secshift;           /* block to sector shift (or -1) */
151         int     dss_secsize;            /* sector size */
152         struct diskslice
153                 dss_slices[MAX_SLICES]; /* actually usually less */
154 };
155
156 /*
157  * DIOCGPART ioctl - returns information about a disk, slice, or partition.
158  * This ioctl is primarily used to get the block size and media size.
159  *
160  * NOTE: media_offset currently represents the byte offset on the raw device,
161  * it is not a partition relative offset.
162  *
163  * skip_platform and skip_bsdlabel work as with the diskslice
164  * structure.  For partitions within a disklabel these fields are usually
165  * 0 except for partitions which overlap the label or slice reserved area
166  * itself.  Those partitions will set these fields appropriately (relative
167  * to the partition).  In particular, the 'a' and 'c' partitions are
168  * protected.
169  */
170 struct partinfo {
171         u_int64_t       media_offset;   /* byte offset in parent layer */
172         u_int64_t       media_size;     /* media size in bytes */
173         u_int64_t       media_blocks;   /* media size in blocks */
174         int             media_blksize;  /* block size in bytes (sector size) */
175
176         u_int32_t       skip_platform;  /* in sectors */
177         u_int32_t       skip_bsdlabel;  /* in sectors */
178         int             fstype;         /* filesystem type if numeric */
179         char            fstypestr[16];  /* filesystem type as ascii */
180
181         /*
182          * These fields are loaded from the diskinfo structure
183          */
184         u_int           d_nheads;
185         u_int           d_ncylinders;
186         u_int           d_secpertrack;
187         u_int           d_secpercyl;
188         u_int           d_reserved[16];
189 };
190
191 #define DIOCGPART       _IOR('d', 104, struct partinfo) /* get partition */
192
193 /*
194  * disk unit and slice helper functions
195  *
196  *     3                   2                   1                   0
197  *   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
198  *  _________________________________________________________________
199  *  | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
200  *  -----------------------------------------------------------------
201  *  | SL2 | PART3 |UNIT_2 |P| SLICE |  MAJOR?       |  UNIT   |PART |
202  *  -----------------------------------------------------------------
203  */
204
205 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
206
207 #define DKMAXUNITS      512
208 #define DKMAXSLICES     128
209 #define DKMAXPARTITIONS 256
210
211 /*
212  * Build a minor device number.
213  */
214 static __inline u_int32_t
215 dkmakeminor(u_int32_t unit, u_int32_t slice, u_int32_t part)
216 {
217         u_int32_t val;
218
219         val = ((unit & 0x001f) << 3) | ((unit & 0x01e0) << 16) |
220               ((slice & 0x000f) << 16) | ((slice & 0x0070) << 25) |
221               (part & 0x0007) | ((part & 0x0008) << 17) |
222               ((part & 0x00F0) << 21);
223         return(val);
224 }
225
226 /*
227  * Generate the minor number representing the entire disk, with no
228  * mbr or label interpretation.
229  */
230 static __inline u_int32_t
231 dkmakewholedisk(u_int32_t unit)
232 {
233         return(dkmakeminor(unit, WHOLE_DISK_SLICE, WHOLE_SLICE_PART));
234 }
235
236 /*
237  * Generate the minor number representing an entire slice, with no
238  * recursive mbr, boot sector, or label interpretation.
239  */
240 static __inline u_int32_t
241 dkmakewholeslice(u_int32_t unit, u_int32_t slice)
242 {
243         return(dkmakeminor(unit, slice, WHOLE_SLICE_PART));
244 }
245
246 /*
247  * Return the unit mask, used in calls to make_dev()
248  */
249 static __inline u_int32_t
250 dkunitmask(void)
251 {
252         return (0x01e000f8);
253 }
254
255 /*
256  * build minor number elements - encode unit number, slice, and partition
257  * (OR the results together).
258  */
259 static __inline u_int32_t
260 dkmakeunit(int unit)
261 {
262         return(dkmakeminor((u_int32_t)unit, 0, 0));
263 }
264
265 static __inline u_int32_t
266 dkmakeslice(int slice)
267 {
268         return(dkmakeminor(0, (u_int32_t)slice, 0));
269 }
270
271 static __inline u_int32_t
272 dkmakepart(int part)
273 {
274         return(dkmakeminor(0, 0, (u_int32_t)part));
275 }
276
277 #endif
278
279 /*
280  * dk*() support functions operating on cdev_t's
281  */
282 #ifdef _KERNEL
283
284 static __inline int
285 dkunit(cdev_t dev)
286 {
287         u_int32_t val = minor(dev);
288
289         val = ((val >> 3) & 0x001f) | ((val >> 16) & 0x01e0);
290         return((int)val);
291 }
292
293 static __inline u_int32_t
294 dkslice(cdev_t dev)
295 {
296         u_int32_t val = minor(dev);
297
298         val = ((val >> 16) & 0x000f) | ((val >> 25) & 0x0070);
299         return(val);
300 }
301
302 static __inline u_int32_t
303 dkpart(cdev_t dev)
304 {
305         u_int32_t val = minor(dev);
306
307         val = (val & 0x0007) | ((val >> 17) & 0x0008) | ((val >> 21) & 0x00f0);
308         return(val);
309 }
310
311 /*
312  * dkmodpart() - create sub-device
313  */
314 static __inline cdev_t
315 dkmodpart(cdev_t dev, int part)
316 {
317         u_int32_t val;
318
319         val = (minor(dev) & ~dkmakepart(-1)) | dkmakepart(part);
320         return (make_sub_dev(dev, val));
321 }
322
323 static __inline cdev_t
324 dkmodslice(cdev_t dev, int slice)
325 {
326         u_int32_t val;
327
328         val = (minor(dev) & ~dkmakeslice(-1)) | dkmakeslice(slice);
329         return (make_sub_dev(dev, val));
330 }
331
332 #endif
333
334 /*
335  * disk management functions
336  */
337
338 #ifdef _KERNEL
339
340 struct buf;
341 struct bio;
342 struct disklabel;
343 struct disk_info;
344 struct bio_queue_head;
345
346 int     mbrinit (cdev_t dev, struct disk_info *info,
347                     struct diskslices **sspp);
348 struct bio *
349         dscheck (cdev_t dev, struct bio *bio, struct diskslices *ssp);
350 void    dsclose (cdev_t dev, int mode, struct diskslices *ssp);
351 void    dsgone (struct diskslices **sspp);
352 int     dsioctl (cdev_t dev, u_long cmd, caddr_t data, int flags,
353                     struct diskslices **sspp, struct disk_info *info);
354 int     dsisopen (struct diskslices *ssp);
355 struct diskslices *
356         dsmakeslicestruct (int nslices, struct disk_info *info);
357 char    *dsname (cdev_t dev, int unit, int slice, int part,
358                     char *partname);
359 int     dsopen (cdev_t dev, int mode, u_int flags,
360                     struct diskslices **sspp, struct disk_info *info);
361 int64_t dssize (cdev_t dev, struct diskslices **sspp);
362
363 /*
364  * Ancillary functions
365  */
366
367 void    diskerr (struct bio *bio, cdev_t dev, const char *what, int pri,
368                     int donecnt);
369 void    disksort (struct buf *ap, struct buf *bp);
370 void    bioqdisksort (struct bio_queue_head *ap, struct bio *bio);
371
372 #endif /* _KERNEL */
373
374 #endif /* !_SYS_DISKSLICE_H_ */