2 * Copyright (c) 2007 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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
35 * Copyright (c) 1994 Bruce D. Evans.
36 * All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
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.
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
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.19 2007/06/17 03:51:11 dillon Exp $
63 #ifndef _SYS_DISKSLICE_H_
64 #define _SYS_DISKSLICE_H_
67 #include <sys/types.h>
69 #ifndef _SYS_IOCCOM_H_
70 #include <sys/ioccom.h>
74 #include <sys/conf.h> /* for make_sub_dev() */
77 #include <sys/systm.h> /* for minor() */
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 DIOCSETSNOOP _IOW('d', 113, int)
97 #define DKMAXUNITS 512 /* maximum supported disk units */
98 #define DKMAXSLICES 128 /* maximum supported slices (0 & 1 special) */
99 #define DKRESPARTITIONS 128 /* 128+ have special meanings */
100 #define DKMAXPARTITIONS 256 /* maximum supported in-kernel partitions */
103 * The whole-disk-slice does not try to interpret the MBR. The whole slice
104 * partition does not try to interpret the disklabel within the slice.
106 #define WHOLE_DISK_SLICE 1
107 #define WHOLE_SLICE_PART (DKMAXPARTITIONS - 1)
109 #ifdef MAXPARTITIONS /* XXX don't depend on disklabel.h */
110 #if MAXPARTITIONS != 16 /* but check consistency if possible */
111 #error "inconsistent MAXPARTITIONS"
114 #define MAXPARTITIONS 16
118 * diskslice structure - slices up the disk and indicates where the
119 * BSD labels are, if any.
121 * ds_reserved - indicates read-only sectors due to an overlap with
122 * a parent partition or an in-band label. BSD labels
123 * are in-band labels. This field is also set if
124 * label snooping has been requested, even if there is
128 u_int64_t ds_offset; /* starting sector */
129 u_int64_t ds_size; /* number of sectors */
130 u_int32_t ds_reserved; /* sectors reserved parent overlap */
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_int32_t ds_openmask[DKMAXPARTITIONS/sizeof(u_int32_t)];
137 u_char ds_wlabel; /* nonzero if label is writable */
138 int ds_ttlopens; /* total opens, incl slice & raw */
142 struct cdevsw *dss_cdevsw; /* for containing device */
143 int dss_first_bsd_slice; /* COMPATIBILITY_SLICE is mapped here */
144 u_int dss_nslices; /* actual dimension of dss_slices[] */
145 u_int dss_oflags; /* copy of flags for "first" open */
146 int dss_secmult; /* block to sector multiplier */
147 int dss_secshift; /* block to sector shift (or -1) */
148 int dss_secsize; /* sector size */
150 dss_slices[MAX_SLICES]; /* actually usually less */
154 * DIOCGPART ioctl - returns information about a disk, slice, or partition.
155 * This ioctl is primarily used to get the block size and media size.
157 * NOTE: media_offset currently represents the byte offset on the raw device,
158 * it is not a partition relative offset.
160 * NOTE: reserved_blocks indicates how many blocks at the beginning of the
161 * partition are read-only due to in-band sharing with the parent. For
162 * example, if partition 'a' starts at block 0, it actually overlaps the
163 * disklabel itself so numerous sectors at the beginning of 'a' will be
167 u_int64_t media_offset; /* byte offset in parent layer */
168 u_int64_t media_size; /* media size in bytes */
169 u_int64_t media_blocks; /* media size in blocks */
170 int media_blksize; /* block size in bytes (sector size) */
172 u_int64_t reserved_blocks;/* read-only, in sectors */
173 int fstype; /* filesystem type if numeric */
174 char fstypestr[16]; /* filesystem type as ascii */
177 * These fields are loaded from the diskinfo structure
183 u_int d_reserved[16];
186 #define DIOCGPART _IOR('d', 104, struct partinfo) /* get partition */
189 * disk unit and slice helper functions
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 * -----------------------------------------------------------------
200 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
203 * Build a minor device number.
205 static __inline u_int32_t
206 dkmakeminor(u_int32_t unit, u_int32_t slice, u_int32_t part)
210 val = ((unit & 0x001f) << 3) | ((unit & 0x01e0) << 16) |
211 ((slice & 0x000f) << 16) | ((slice & 0x0070) << 25) |
212 (part & 0x0007) | ((part & 0x0008) << 17) |
213 ((part & 0x00F0) << 21);
218 * Generate the minor number representing the entire disk, with no
219 * mbr or label interpretation.
221 static __inline u_int32_t
222 dkmakewholedisk(u_int32_t unit)
224 return(dkmakeminor(unit, WHOLE_DISK_SLICE, WHOLE_SLICE_PART));
228 * Generate the minor number representing an entire slice, with no
229 * recursive mbr, boot sector, or label interpretation.
231 static __inline u_int32_t
232 dkmakewholeslice(u_int32_t unit, u_int32_t slice)
234 return(dkmakeminor(unit, slice, WHOLE_SLICE_PART));
238 * Return the unit mask, used in calls to make_dev()
240 static __inline u_int32_t
247 * build minor number elements - encode unit number, slice, and partition
248 * (OR the results together).
250 static __inline u_int32_t
253 return(dkmakeminor((u_int32_t)unit, 0, 0));
256 static __inline u_int32_t
257 dkmakeslice(int slice)
259 return(dkmakeminor(0, (u_int32_t)slice, 0));
262 static __inline u_int32_t
265 return(dkmakeminor(0, 0, (u_int32_t)part));
271 * dk*() support functions operating on cdev_t's
278 u_int32_t val = minor(dev);
280 val = ((val >> 3) & 0x001f) | ((val >> 16) & 0x01e0);
284 static __inline u_int32_t
287 u_int32_t val = minor(dev);
289 val = ((val >> 16) & 0x000f) | ((val >> 25) & 0x0070);
293 static __inline u_int32_t
296 u_int32_t val = minor(dev);
298 val = (val & 0x0007) | ((val >> 17) & 0x0008) | ((val >> 21) & 0x00f0);
303 * dkmodpart() - create sub-device
305 static __inline cdev_t
306 dkmodpart(cdev_t dev, int part)
310 val = (minor(dev) & ~dkmakepart(-1)) | dkmakepart(part);
311 return (make_sub_dev(dev, val));
314 static __inline cdev_t
315 dkmodslice(cdev_t dev, int slice)
319 val = (minor(dev) & ~dkmakeslice(-1)) | dkmakeslice(slice);
320 return (make_sub_dev(dev, val));
326 * Bitmask ops, keeping track of which partitions are open.
330 dsclrmask(struct diskslice *ds, int part)
332 part &= (DKMAXPARTITIONS - 1);
333 ds->ds_openmask[part >> 5] &= ~(1 << (part & 31));
338 dssetmask(struct diskslice *ds, int part)
340 part &= (DKMAXPARTITIONS - 1);
341 ds->ds_openmask[part >> 5] |= (1 << (part & 31));
346 dschkmask(struct diskslice *ds, int part)
348 part &= (DKMAXPARTITIONS - 1);
349 return (ds->ds_openmask[part >> 5] & (1 << (part & 31)));
354 dscountmask(struct diskslice *ds)
360 for (i = 0; i < DKMAXPARTITIONS / 32; ++i) {
361 if (ds->ds_openmask[i]) {
362 for (j = 0; j < 32; ++j) {
363 if (ds->ds_openmask[i] & (1 << j))
373 dssetmaskfrommask(struct diskslice *ds, u_int32_t *tmask)
377 for (i = 0; i < DKMAXPARTITIONS / 32; ++i)
378 tmask[i] |= ds->ds_openmask[i];
382 * disk management functions
391 struct bio_queue_head;
393 int mbrinit (cdev_t dev, struct disk_info *info,
394 struct diskslices **sspp);
395 int gptinit (cdev_t dev, struct disk_info *info,
396 struct diskslices **sspp);
398 dscheck (cdev_t dev, struct bio *bio, struct diskslices *ssp);
399 void dsclose (cdev_t dev, int mode, struct diskslices *ssp);
400 void dsgone (struct diskslices **sspp);
401 int dsioctl (cdev_t dev, u_long cmd, caddr_t data, int flags,
402 struct diskslices **sspp, struct disk_info *info);
403 int dsisopen (struct diskslices *ssp);
405 dsmakeslicestruct (int nslices, struct disk_info *info);
406 char *dsname (cdev_t dev, int unit, int slice, int part,
408 int dsopen (cdev_t dev, int mode, u_int flags,
409 struct diskslices **sspp, struct disk_info *info);
410 int64_t dssize (cdev_t dev, struct diskslices **sspp);
413 * Ancillary functions
416 void diskerr (struct bio *bio, cdev_t dev, const char *what, int pri,
418 void disksort (struct buf *ap, struct buf *bp);
419 void bioqdisksort (struct bio_queue_head *ap, struct bio *bio);
423 #endif /* !_SYS_DISKSLICE_H_ */