Initial import from FreeBSD RELENG_4:
[dragonfly.git] / lib / libdisk / libdisk.h
1 /*
2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@login.dknet.dk> wrote this file.  As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
8 *
9 * $FreeBSD: src/lib/libdisk/libdisk.h,v 1.32.2.8 2002/01/07 07:53:29 dillon Exp $
10 *
11 */
12
13 #define MAX_NO_DISKS    32
14 /* Max # of disks Disk_Names() will return */
15
16 #define MAX_SEC_SIZE    2048  /* maximum sector size that is supported */
17 #define MIN_SEC_SIZE    512   /* the sector size to end sensing at */
18
19 typedef enum {
20         whole,
21         unknown,
22         fat,
23         freebsd,
24         extended,
25         part,
26         unused
27 } chunk_e;
28
29 __BEGIN_DECLS
30 struct disk {
31         char            *name;
32         u_long          flags;
33 #               define DISK_ON_TRACK    1
34         u_long          bios_cyl;
35         u_long          bios_hd;
36         u_long          bios_sect;
37 #ifdef PC98
38         u_char          *bootipl;
39         size_t          bootipl_size;
40         u_char          *bootmenu;
41         size_t          bootmenu_size;
42 #else
43         u_char          *bootmgr;
44         size_t          bootmgr_size;
45 #endif
46         u_char          *boot1;
47 #if defined(__i386__)           /* the i386 needs extra help... */
48         u_char          *boot2;
49 #endif
50         struct chunk    *chunks;
51         u_long          sector_size; /* media sector size, a power of 2 */
52 };
53
54 struct chunk {
55         struct chunk    *next;
56         struct chunk    *part;
57         struct disk     *disk;
58         long            offset;
59         u_long          size;
60         u_long          end;
61 #ifdef PC98
62         char            *sname;
63 #endif
64         char            *name;
65         char            *oname;
66         /* Used during Fixup_Names() to avoid renaming more than
67          * absolutely needed.
68          */
69         chunk_e         type;
70         int             subtype;
71         u_long          flags;
72         void            (*private_free)(void*);
73         void            *(*private_clone)(void*);
74         void            *private_data;
75         /* For data private to the application, and the management
76          * thereof.  If the functions are not provided, no storage
77          * management is done, Cloning will just copy the pointer
78          * and freeing will just forget it.
79          */
80 };
81
82 /*
83  * flags:
84  *
85  * BSD_COMPAT   -       This chunk is in the BSD-compatibility, and has
86  *                      a short name too, ie wd0s4f -> wd0f
87  * ALIGN        -       This chunk should be aligned
88  * IS_ROOT      -       This 'part' is a rootfs, allocate 'a'
89  * ACTIVE       -       This is the active slice in the MBR
90  * FORCE_ALL    -       Force a dedicated disk for FreeBSD, bypassing
91  *                      all BIOS geometry considerations
92  * AUTO_SIZE    -       This chunk was auto-sized and can fill-out a
93  *                      following chunk if the following chunk is deleted.
94  * NEWFS        -       newfs pending, used to enable auto-resizing on
95  *                      delete (along with AUTO_SIZE).
96  */
97
98 #define CHUNK_BSD_COMPAT        0x0002
99 #define CHUNK_ALIGN             0x0008
100 #define CHUNK_IS_ROOT           0x0010
101 #define CHUNK_ACTIVE            0x0020
102 #define CHUNK_FORCE_ALL         0x0040  
103 #define CHUNK_AUTO_SIZE         0x0080
104 #define CHUNK_NEWFS             0x0100
105
106 #define DELCHUNK_NORMAL         0x0000
107 #define DELCHUNK_RECOVER        0x0001
108
109
110 extern const char *chunk_n[];
111
112 const char *
113 slice_type_name( int type, int subtype );
114 /* "chunk_n" for subtypes too
115  */
116
117 struct disk *
118 Open_Disk(const char *devname);
119 /* Will open the named disk, and return populated tree.
120  */
121
122 struct disk *
123 Clone_Disk(struct disk *disk);
124 /* Clone a copy of a tree.  Useful for "Undo" functionality
125  */
126
127 void
128 Free_Disk(struct disk *disk);
129 /* Free a tree made with Open_Disk() or Clone_Disk()
130  */
131
132 void
133 Debug_Disk(struct disk *disk);
134 /* Print the content of the tree to stdout
135  */
136
137 void
138 Set_Bios_Geom(struct disk *disk, u_long cyl, u_long heads, u_long sects);
139 /* Set the geometry the bios uses.
140  */
141
142 void
143 Sanitize_Bios_Geom(struct disk *disk);
144 /* Set the bios geometry to something sane
145  */
146
147 int
148 Delete_Chunk2(struct disk *disk, struct chunk *, int flags);
149 /* Free a chunk of disk_space modified by the passed
150  * flags.
151  */
152
153 int
154 Delete_Chunk(struct disk *disk, struct chunk *);
155 /* Free a chunk of disk_space
156  */
157
158 void
159 Collapse_Disk(struct disk *disk);
160 /* Experimental, do not use.
161  */
162 int
163 Collapse_Chunk(struct disk *disk, struct chunk *chunk);
164 /* Experimental, do not use.
165  */
166
167 int
168 #ifdef PC98
169 Create_Chunk(struct disk *disk, u_long offset, u_long size, chunk_e type,
170         int subtype, u_long flags, const char *);
171 #else
172 Create_Chunk(struct disk *disk, u_long offset, u_long size, chunk_e type,
173         int subtype, u_long flags);
174 #endif
175 /* Create a chunk with the specified paramters
176  */
177
178 void
179 All_FreeBSD(struct disk *d, int force_all);
180 /* Make one FreeBSD chunk covering the entire disk;
181  * if force_all is set, bypass all BIOS geometry
182  * considerations.
183  */
184
185 char *
186 CheckRules(struct disk *);
187 /* Return char* to warnings about broken design rules in this disklayout
188  */
189
190 char **
191 Disk_Names();
192 /* Return char** with all disk's names (wd0, wd1 ...).  You must free
193  * each pointer, as well as the array by hand
194  */
195
196 #ifdef PC98
197 void
198 Set_Boot_Mgr(struct disk *d, const u_char *bootipl, const size_t bootipl_size,
199              const u_char *bootmenu, const size_t bootmenu_size);
200 #else
201 void
202 Set_Boot_Mgr(struct disk *d, const u_char *bootmgr, const size_t bootmgr_size);
203 #endif
204 /* Use this boot-manager on this disk.  Gets written when Write_Disk()
205  * is called
206  */
207
208 int
209 Set_Boot_Blocks(struct disk *d, const u_char *_boot1, const u_char *_boot2);
210 /* Use these boot-blocks on this disk.  Gets written when Write_Disk()
211  * is called. Returns nonzero upon failure.
212  */
213
214 int
215 Write_Disk(struct disk *d);
216 /* Write all the MBRs, disklabels, bootblocks and boot managers
217  */
218
219 int
220 Cyl_Aligned(struct disk *d, u_long offset);
221 /* Check if offset is aligned on a cylinder according to the
222  * bios geometry
223  */
224
225 u_long
226 Next_Cyl_Aligned(struct disk *d, u_long offset);
227 /* Round offset up to next cylinder according to the bios-geometry
228  */
229
230 u_long
231 Prev_Cyl_Aligned(struct disk *d, u_long offset);
232 /* Round offset down to previous cylinder according to the bios-
233  * geometry
234  */
235
236 int
237 Track_Aligned(struct disk *d, u_long offset);
238 /* Check if offset is aligned on a track according to the
239  * bios geometry
240  */
241
242 u_long
243 Next_Track_Aligned(struct disk *d, u_long offset);
244 /* Round offset up to next track according to the bios-geometry
245  */
246
247 u_long
248 Prev_Track_Aligned(struct disk *d, u_long offset);
249 /* Check if offset is aligned on a track according to the
250  * bios geometry
251  */
252
253 struct chunk *
254 Create_Chunk_DWIM(struct disk *d, struct chunk *parent , u_long size,
255         chunk_e type, int subtype, u_long flags);
256 /* This one creates a partition inside the given parent of the given
257  * size, and returns a pointer to it.  The first unused chunk big
258  * enough is used.
259  */
260
261 int
262 MakeDev(struct chunk *c, const char *path);
263
264 int
265 MakeDevDisk(struct disk *d, const char *path);
266 /* Make device nodes for all chunks on this disk */
267
268 char *
269 ShowChunkFlags(struct chunk *c);
270 /* Return string to show flags. */
271
272 char *
273 ChunkCanBeRoot(struct chunk *c);
274 /* Return NULL if chunk can be /, explanation otherwise */
275
276 /*
277  * Implementation details  >>> DO NOT USE <<<
278  */
279
280 void Debug_Chunk(struct chunk *);
281 void Free_Chunk(struct chunk *);
282 struct chunk * Clone_Chunk(struct chunk *);
283 #ifdef PC98
284 int Add_Chunk(struct disk *, long, u_long, const char *, chunk_e, int, u_long, const char *);
285 #else
286 int Add_Chunk(struct disk *, long, u_long, const char *, chunk_e, int, u_long);
287 #endif
288 void * read_block(int, daddr_t, u_long);
289 int write_block(int, daddr_t, void *, u_long);
290 struct disklabel * read_disklabel(int, daddr_t, u_long);
291 struct chunk * Find_Mother_Chunk(struct chunk *, u_long, u_long, chunk_e);
292 struct disk * Int_Open_Disk(const char *name, u_long size);
293 int Fixup_Names(struct disk *);
294 __END_DECLS
295
296 #define dprintf printf
297
298 /* TODO
299  *
300  * Need a error string mechanism from the functions instead of warn()
301  *
302  * Make sure only FreeBSD start at offset==0
303  *
304  * Collapse must align.
305  *
306  * Make Write_Disk(struct disk*)
307  *
308  * Consider booting from OnTrack'ed disks.
309  *
310  * Get Bios-geom, ST506 & OnTrack from driver (or otherwise)
311  *
312  * Make Create_DWIM().
313  *
314  * Make Is_Unchanged(struct disk *d1, struct chunk *c1)
315  *
316  * don't rename slices unless we have to
317  *
318  *Sample output from tst01:
319  *
320  * Debug_Disk(wd0)  flags=0  bios_geom=0/0/0
321  * >>        0x3d040          0    1411200    1411199 wd0      0 whole    0 0
322  * >>>>      0x3d080          0     960120     960119 wd0s1    3 freebsd  0 8
323  * >>>>>>    0x3d100          0      40960      40959 wd0s1a   5 part     0 0
324  * >>>>>>    0x3d180      40960     131072     172031 wd0s1b   5 part     0 0
325  * >>>>>>    0x3d1c0     172032     409600     581631 wd0s1e   5 part     0 0
326  * >>>>>>    0x3d200     581632     378488     960119 wd0s1f   5 part     0 0
327  * >>>>      0x3d140     960120       5670     965789 wd0s2    4 extended 0 8
328  * >>>>>>    0x3d2c0     960120         63     960182 -        6 unused   0 0
329  * >>>>>>    0x3d0c0     960183       5607     965789 wd0s5    2 fat      0 8
330  * >>>>      0x3d280     965790       1890     967679 wd0s3    1 foo      -2 8
331  * >>>>      0x3d300     967680     443520    1411199 wd0s4    3 freebsd  0 8
332  * >>>>>>    0x3d340     967680     443520    1411199 wd0s4a   5 part     0 0
333  *
334  * ^            ^           ^          ^          ^     ^      ^ ^        ^ ^
335  * level    chunkptr      start      size        end  name    type  subtype flags
336  *
337  * Underlying data structure:
338  *
339  *      Legend:
340  *              <struct chunk> --> part
341  *                      |
342  *                      v next
343  *
344  *      <wd0> --> <wd0s1> --> <wd0s1a>
345  *                   |           |
346  *                   |           v
347  *                   |        <wd0s1b>
348  *                   |           |
349  *                   |           v
350  *                   |        <wd0s1e>
351  *                   |           |
352  *                   |           v
353  *                   |        <wd0s1f>
354  *                   |
355  *                   v
356  *                <wd0s2> --> <unused>
357  *                   |           |
358  *                   |           v
359  *                   |        <wd0s5>
360  *                   |
361  *                   v
362  *                <wd0s3>
363  *                   |
364  *                   v
365  *                <wd0s4> --> <wd0s4a>
366  *
367  *
368  */