Move all the code related to handling the current 32 bit disklabel
[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.21 2007/06/18 05:13:42 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_DISKLABEL_H_
70 #include <sys/disklabel.h>
71 #endif
72 #ifndef _SYS_IOCCOM_H_
73 #include <sys/ioccom.h>
74 #endif
75 #if defined(_KERNEL)
76 #ifndef _SYS_CONF_H_
77 #include <sys/conf.h>           /* for make_sub_dev() */
78 #endif
79 #ifndef _SYS_SYSTM_H_
80 #include <sys/systm.h>          /* for minor() */
81 #endif
82 #endif
83
84 #define BASE_SLICE              2       /* e.g. ad0s1 */
85 #define COMPATIBILITY_SLICE     0       /* e.g. ad0a-j */
86                                 /* 101 - compat disklabel DIOCGDINFO    */
87                                 /* 102 - compat disklabel DIOCSDINFO    */
88                                 /* 103 - compat disklabel DIOCWDINFO    */
89                                 /* 104 - DIOCGPART (see below)          */
90                                 /* 105 - compat disklabel DIOCGDVIRGIN  */
91 #define DIOCWLABEL              _IOW('d', 109, int)
92 #define DIOCGSLICEINFO          _IOR('d', 111, struct diskslices)
93 #define DIOCSYNCSLICEINFO       _IOW('d', 112, int)
94 #define MAX_SLICES              16
95
96 /*
97  * Support limits
98  */
99 #define DKMAXUNITS      512     /* maximum supported disk units */
100 #define DKMAXSLICES     128     /* maximum supported slices (0 & 1 special) */
101 #define DKRESPARTITIONS 128     /* 128+ have special meanings */
102 #define DKMAXPARTITIONS 256     /* maximum supported in-kernel partitions */
103
104 /*
105  * The whole-disk-slice does not try to interpret the MBR.  The whole slice
106  * partition does not try to interpret the disklabel within the slice.
107  */
108 #define WHOLE_DISK_SLICE        1
109 #define WHOLE_SLICE_PART        (DKMAXPARTITIONS - 1)
110
111 #ifdef MAXPARTITIONS                    /* XXX don't depend on disklabel.h */
112 #if MAXPARTITIONS !=    16              /* but check consistency if possible */
113 #error "inconsistent MAXPARTITIONS"
114 #endif
115 #else
116 #define MAXPARTITIONS   16
117 #endif
118
119 /*
120  * diskslice structure - slices up the disk and indicates where the
121  * BSD labels are, if any.
122  *
123  * ds_reserved       -  indicates read-only sectors due to an overlap with
124  *                      a parent partition or an in-band label.  BSD labels
125  *                      are in-band labels.  This field is also set if
126  *                      label snooping has been requested, even if there is
127  *                      no label present.
128  */
129 struct diskslice {
130         u_int64_t       ds_offset;      /* starting sector */
131         u_int64_t       ds_size;        /* number of sectors */
132         u_int32_t       ds_reserved;    /* sectors reserved parent overlap */
133         int             ds_type;        /* (foreign) slice type */
134         disklabel_t     ds_label;       /* label, if any */
135         struct disklabel_ops *ds_ops;   /* label ops (probe default) */
136         void            *ds_dev;        /* devfs token for raw whole slice */
137         void            *ds_devs[MAXPARTITIONS]; /* XXX s.b. in label */
138         u_int32_t       ds_openmask[DKMAXPARTITIONS/(sizeof(u_int32_t)*8)];
139                                         /* devs open */
140         u_char          ds_wlabel;      /* nonzero if label is writable */
141         int             ds_ttlopens;    /* total opens, incl slice & raw */
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  * NOTE: reserved_blocks indicates how many blocks at the beginning of the
164  * partition are read-only due to in-band sharing with the parent.  For
165  * example, if partition 'a' starts at block 0, it actually overlaps the
166  * disklabel itself so numerous sectors at the beginning of 'a' will be
167  * reserved.
168  */
169 struct partinfo {
170         u_int64_t       media_offset;   /* byte offset in parent layer */
171         u_int64_t       media_size;     /* media size in bytes */
172         u_int64_t       media_blocks;   /* media size in blocks */
173         int             media_blksize;  /* block size in bytes (sector size) */
174
175         u_int64_t       reserved_blocks;/* read-only, in sectors */
176         int             fstype;         /* filesystem type if numeric */
177         char            fstypestr[16];  /* filesystem type as ascii */
178
179         /*
180          * These fields are loaded from the diskinfo structure
181          */
182         u_int           d_nheads;
183         u_int           d_ncylinders;
184         u_int           d_secpertrack;
185         u_int           d_secpercyl;
186         u_int           d_reserved[16];
187 };
188
189 #define DIOCGPART       _IOR('d', 104, struct partinfo) /* get partition */
190
191 /*
192  * disk unit and slice helper functions
193  *
194  *     3                   2                   1                   0
195  *   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
196  *  _________________________________________________________________
197  *  | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
198  *  -----------------------------------------------------------------
199  *  | SL2 | PART3 |UNIT_2 |P| SLICE |  MAJOR?       |  UNIT   |PART |
200  *  -----------------------------------------------------------------
201  */
202
203 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
204
205 /*
206  * Build a minor device number.
207  */
208 static __inline u_int32_t
209 dkmakeminor(u_int32_t unit, u_int32_t slice, u_int32_t part)
210 {
211         u_int32_t val;
212
213         val = ((unit & 0x001f) << 3) | ((unit & 0x01e0) << 16) |
214               ((slice & 0x000f) << 16) | ((slice & 0x0070) << 25) |
215               (part & 0x0007) | ((part & 0x0008) << 17) |
216               ((part & 0x00F0) << 21);
217         return(val);
218 }
219
220 /*
221  * Generate the minor number representing the entire disk, with no
222  * mbr or label interpretation.
223  */
224 static __inline u_int32_t
225 dkmakewholedisk(u_int32_t unit)
226 {
227         return(dkmakeminor(unit, WHOLE_DISK_SLICE, WHOLE_SLICE_PART));
228 }
229
230 /*
231  * Generate the minor number representing an entire slice, with no
232  * recursive mbr, boot sector, or label interpretation.
233  */
234 static __inline u_int32_t
235 dkmakewholeslice(u_int32_t unit, u_int32_t slice)
236 {
237         return(dkmakeminor(unit, slice, WHOLE_SLICE_PART));
238 }
239
240 /*
241  * Return the unit mask, used in calls to make_dev()
242  */
243 static __inline u_int32_t
244 dkunitmask(void)
245 {
246         return (0x01e000f8);
247 }
248
249 /*
250  * build minor number elements - encode unit number, slice, and partition
251  * (OR the results together).
252  */
253 static __inline u_int32_t
254 dkmakeunit(int unit)
255 {
256         return(dkmakeminor((u_int32_t)unit, 0, 0));
257 }
258
259 static __inline u_int32_t
260 dkmakeslice(int slice)
261 {
262         return(dkmakeminor(0, (u_int32_t)slice, 0));
263 }
264
265 static __inline u_int32_t
266 dkmakepart(int part)
267 {
268         return(dkmakeminor(0, 0, (u_int32_t)part));
269 }
270
271 #endif
272
273 /*
274  * dk*() support functions operating on cdev_t's
275  */
276 #ifdef _KERNEL
277
278 static __inline int
279 dkunit(cdev_t dev)
280 {
281         u_int32_t val = minor(dev);
282
283         val = ((val >> 3) & 0x001f) | ((val >> 16) & 0x01e0);
284         return((int)val);
285 }
286
287 static __inline u_int32_t
288 dkslice(cdev_t dev)
289 {
290         u_int32_t val = minor(dev);
291
292         val = ((val >> 16) & 0x000f) | ((val >> 25) & 0x0070);
293         return(val);
294 }
295
296 static __inline u_int32_t
297 dkpart(cdev_t dev)
298 {
299         u_int32_t val = minor(dev);
300
301         val = (val & 0x0007) | ((val >> 17) & 0x0008) | ((val >> 21) & 0x00f0);
302         return(val);
303 }
304
305 /*
306  * dkmodpart() - create sub-device
307  */
308 static __inline cdev_t
309 dkmodpart(cdev_t dev, int part)
310 {
311         u_int32_t val;
312
313         val = (minor(dev) & ~dkmakepart(-1)) | dkmakepart(part);
314         return (make_sub_dev(dev, val));
315 }
316
317 static __inline cdev_t
318 dkmodslice(cdev_t dev, int slice)
319 {
320         u_int32_t val;
321
322         val = (minor(dev) & ~dkmakeslice(-1)) | dkmakeslice(slice);
323         return (make_sub_dev(dev, val));
324 }
325
326 #endif
327
328 /*
329  * Bitmask ops, keeping track of which partitions are open.
330  */
331 static __inline
332 void
333 dsclrmask(struct diskslice *ds, int part)
334 {
335         part &= (DKMAXPARTITIONS - 1);
336         ds->ds_openmask[part >> 5] &= ~(1 << (part & 31));
337 }
338
339 static __inline
340 void
341 dssetmask(struct diskslice *ds, int part)
342 {
343         part &= (DKMAXPARTITIONS - 1);
344         ds->ds_openmask[part >> 5] |= (1 << (part & 31));
345 }
346
347 static __inline
348 int
349 dschkmask(struct diskslice *ds, int part)
350 {
351         part &= (DKMAXPARTITIONS - 1);
352         return (ds->ds_openmask[part >> 5] & (1 << (part & 31)));
353 }
354
355 static __inline
356 int
357 dscountmask(struct diskslice *ds)
358 {
359         int count = 0;
360         int i;
361         int j;
362
363         for (i = 0; i < DKMAXPARTITIONS / 32; ++i) {
364                 if (ds->ds_openmask[i]) {
365                         for (j = 0; j < 32; ++j) {
366                                 if (ds->ds_openmask[i] & (1 << j))
367                                         ++count;
368                         }
369                 }
370         }
371         return(count);
372 }
373
374 static __inline
375 void
376 dssetmaskfrommask(struct diskslice *ds, u_int32_t *tmask)
377 {
378         int i;
379
380         for (i = 0; i < DKMAXPARTITIONS / 32; ++i)
381                 tmask[i] |= ds->ds_openmask[i];
382 }
383
384 /*
385  * disk management functions
386  */
387
388 #ifdef _KERNEL
389
390 struct buf;
391 struct bio;
392 struct disk_info;
393 struct bio_queue_head;
394
395 int     mbrinit (cdev_t dev, struct disk_info *info,
396                     struct diskslices **sspp);
397 int     gptinit (cdev_t dev, struct disk_info *info,
398                     struct diskslices **sspp);
399 struct bio *
400         dscheck (cdev_t dev, struct bio *bio, struct diskslices *ssp);
401 void    dsclose (cdev_t dev, int mode, struct diskslices *ssp);
402 void    dsgone (struct diskslices **sspp);
403 int     dsioctl (cdev_t dev, u_long cmd, caddr_t data, int flags,
404                     struct diskslices **sspp, struct disk_info *info);
405 int     dsisopen (struct diskslices *ssp);
406 struct diskslices *
407         dsmakeslicestruct (int nslices, struct disk_info *info);
408 char    *dsname (cdev_t dev, int unit, int slice, int part,
409                     char *partname);
410 int     dsopen (cdev_t dev, int mode, u_int flags,
411                     struct diskslices **sspp, struct disk_info *info);
412 int64_t dssize (cdev_t dev, struct diskslices **sspp);
413
414 /*
415  * Ancillary functions
416  */
417
418 void    diskerr (struct bio *bio, cdev_t dev, const char *what, int pri,
419                     int donecnt);
420 void    disksort (struct buf *ap, struct buf *bp);
421 void    bioqdisksort (struct bio_queue_head *ap, struct bio *bio);
422
423 #endif /* _KERNEL */
424
425 #endif /* !_SYS_DISKSLICE_H_ */