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.13 2007/05/19 00:52:02 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>
72 #if defined(_KERNEL) && !defined(_SYS_CONF_H_)
73 #include <sys/conf.h> /* for make_sub_dev() */
76 #define BASE_SLICE 2 /* e.g. ad0s1 */
77 #define COMPATIBILITY_SLICE 0 /* e.g. ad0a-j */
78 #define DIOCGSLICEINFO _IOR('d', 111, struct diskslices)
79 #define DIOCSYNCSLICEINFO _IOW('d', 112, int)
83 * The whole-disk-slice does not try to interpret the MBR. The whole slice
84 * partition does not try to interpret the disklabel within the slice.
86 #define WHOLE_DISK_SLICE 1
87 #define WHOLE_SLICE_PART (DKMAXPARTITIONS - 1)
89 #ifdef MAXPARTITIONS /* XXX don't depend on disklabel.h */
90 #if MAXPARTITIONS != 16 /* but check consistency if possible */
91 #error "inconsistent MAXPARTITIONS"
94 #define MAXPARTITIONS 16
98 * diskslice structure - slices up the disk and indicates where the
99 * BSD labels are, if any.
101 * ds_skip_platform - sectors reserved by the platform abstraction,
102 * typically to hold boot sectors and other junk.
103 * The BSD label is placed after the reserved sectors.
105 * This field is typically non-zero for dos slices.
106 * It will always be 0 for the whole-disk slice.
108 * ds_skip_bsdlabel - sectors reserved by the BSD label. Always 0 when
109 * the disk is accessed via the whole-disk slice.
111 * This field includes any sectors reserved by the
112 * platform. e.g. in a dos slice the platform uses
113 * 1 sector (the boot code sector) and the disklabel
114 * uses 15 sectors. This field will be set to 16.
116 * This field would end up being set to one less for
117 * a directly labeled disk, at least for a standard
118 * bsd disklabel vs MBR + bsd disklabel.
121 u_int64_t ds_offset; /* starting sector */
122 u_int64_t ds_size; /* number of sectors */
123 u_int32_t ds_skip_platform; /* in sectors */
124 u_int32_t ds_skip_bsdlabel; /* in sectors */
125 int ds_type; /* (foreign) slice type */
126 struct disklabel *ds_label; /* BSD label, if any */
127 void *ds_dev; /* devfs token for raw whole slice */
128 void *ds_devs[MAXPARTITIONS]; /* XXX s.b. in label */
129 u_char ds_openmask; /* devs open */
130 u_char ds_wlabel; /* nonzero if label is writable */
134 struct cdevsw *dss_cdevsw; /* for containing device */
135 int dss_first_bsd_slice; /* COMPATIBILITY_SLICE is mapped here */
136 u_int dss_nslices; /* actual dimension of dss_slices[] */
137 u_int dss_oflags; /* copy of flags for "first" open */
138 int dss_secmult; /* block to sector multiplier */
139 int dss_secshift; /* block to sector shift (or -1) */
140 int dss_secsize; /* sector size */
142 dss_slices[MAX_SLICES]; /* actually usually less */
146 * DIOCGPART ioctl - returns information about a disk, slice, or partition.
147 * This ioctl is primarily used to get the block size and media size.
149 * NOTE: media_offset currently represents the byte offset on the raw device,
150 * it is not a partition relative offset.
152 * skip_platform and skip_bsdlabel work as with the diskslice
153 * structure. For partitions within a disklabel these fields are usually
154 * 0 except for partitions which overlap the label or slice reserved area
155 * itself. Those partitions will set these fields appropriately (relative
156 * to the partition). In particular, the 'a' and 'c' partitions are
160 u_int64_t media_offset; /* byte offset in parent layer */
161 u_int64_t media_size; /* media size in bytes */
162 u_int64_t media_blocks; /* media size in blocks */
163 int media_blksize; /* block size in bytes (sector size) */
165 u_int32_t skip_platform; /* in sectors */
166 u_int32_t skip_bsdlabel; /* in sectors */
167 int fstype; /* filesystem type if numeric */
168 char fstypestr[16]; /* filesystem type as ascii */
171 #define DIOCGPART _IOR('d', 104, struct partinfo) /* get partition */
174 * disk unit and slice helper functions
177 * 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
178 * _________________________________________________________________
179 * | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
180 * -----------------------------------------------------------------
181 * | SL2 | PART3 |UNIT_2 |P| SLICE | MAJOR? | UNIT |PART |
182 * -----------------------------------------------------------------
185 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
187 #define DKMAXUNITS 512
188 #define DKMAXSLICES 128
189 #define DKMAXPARTITIONS 256
192 * Build a minor device number.
194 static __inline u_int32_t
195 dkmakeminor(u_int32_t unit, u_int32_t slice, u_int32_t part)
199 val = ((unit & 0x001f) << 3) | ((unit & 0x01e0) << 16) |
200 ((slice & 0x000f) << 16) | ((slice & 0x0070) << 25) |
201 (part & 0x0007) | ((part & 0x0008) << 17) |
202 ((part & 0x00F0) << 21);
207 * Generate the minor number representing the entire disk, with no
208 * mbr or label interpretation.
210 static __inline u_int32_t
211 dkmakewholedisk(u_int32_t unit)
213 return(dkmakeminor(unit, WHOLE_DISK_SLICE, WHOLE_SLICE_PART));
217 * Generate the minor number representing an entire slice, with no
218 * recursive mbr, boot sector, or label interpretation.
220 static __inline u_int32_t
221 dkmakewholeslice(u_int32_t unit, u_int32_t slice)
223 return(dkmakeminor(unit, slice, WHOLE_SLICE_PART));
227 * Return the unit mask, used in calls to make_dev()
229 static __inline u_int32_t
236 * build minor number elements - encode unit number, slice, and partition
237 * (OR the results together).
239 static __inline u_int32_t
242 return(dkmakeminor((u_int32_t)unit, 0, 0));
245 static __inline u_int32_t
246 dkmakeslice(int slice)
248 return(dkmakeminor(0, (u_int32_t)slice, 0));
251 static __inline u_int32_t
254 return(dkmakeminor(0, 0, (u_int32_t)part));
260 * dk*() support functions operating on cdev_t's
267 u_int32_t val = minor(dev);
269 val = ((val >> 3) & 0x001f) | ((val >> 16) & 0x01e0);
273 static __inline u_int32_t
276 u_int32_t val = minor(dev);
278 val = ((val >> 16) & 0x000f) | ((val >> 25) & 0x0070);
282 static __inline u_int32_t
285 u_int32_t val = minor(dev);
287 val = (val & 0x0007) | ((val >> 17) & 0x0008) | ((val >> 21) & 0x00f0);
292 * dkmodpart() - create sub-device
294 static __inline cdev_t
295 dkmodpart(cdev_t dev, int part)
299 val = (minor(dev) & ~dkmakepart(-1)) | dkmakepart(part);
300 return (make_sub_dev(dev, val));
303 static __inline cdev_t
304 dkmodslice(cdev_t dev, int slice)
308 val = (minor(dev) & ~dkmakeslice(-1)) | dkmakeslice(slice);
309 return (make_sub_dev(dev, val));
315 * disk management functions
320 #define dsgetlabel(dev, ssp) (ssp->dss_slices[dkslice(dev)].ds_label)
326 struct bio_queue_head;
328 int mbrinit (cdev_t dev, struct disk_info *info,
329 struct diskslices **sspp);
331 dscheck (cdev_t dev, struct bio *bio, struct diskslices *ssp);
332 void dsclose (cdev_t dev, int mode, struct diskslices *ssp);
333 void dsgone (struct diskslices **sspp);
334 int dsioctl (cdev_t dev, u_long cmd, caddr_t data, int flags,
335 struct diskslices **sspp, struct disk_info *info);
336 int dsisopen (struct diskslices *ssp);
338 dsmakeslicestruct (int nslices, struct disk_info *info);
339 char *dsname (cdev_t dev, int unit, int slice, int part,
341 int dsopen (cdev_t dev, int mode, u_int flags,
342 struct diskslices **sspp, struct disk_info *info);
343 int64_t dssize (cdev_t dev, struct diskslices **sspp);
346 * Ancillary functions
349 void diskerr (struct bio *bio, cdev_t dev, const char *what, int pri,
351 void disksort (struct buf *ap, struct buf *bp);
352 void bioqdisksort (struct bio_queue_head *ap, struct bio *bio);
356 #endif /* !_SYS_DISKSLICE_H_ */