Implement non-booting support for the DragonFly 64 bit disklabel:
[dragonfly.git] / sys / kern / subr_disklabel64.c
CommitLineData
ba0cc1ab
MD
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 *
0ffe40b3 34 * $DragonFly: src/sys/kern/subr_disklabel64.c,v 1.2 2007/06/19 02:53:56 dillon Exp $
ba0cc1ab
MD
35 */
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/kernel.h>
40#include <sys/conf.h>
41#include <sys/disklabel.h>
0ffe40b3 42#include <sys/disklabel64.h>
ba0cc1ab
MD
43#include <sys/diskslice.h>
44#include <sys/disk.h>
0ffe40b3 45#include <sys/kern_syscall.h>
ba0cc1ab
MD
46#include <sys/buf2.h>
47
0ffe40b3
MD
48/*
49 * Retrieve the partition start and extent, in blocks. Return 0 on success,
50 * EINVAL on error.
51 */
52static int
53l64_getpartbounds(struct diskslices *ssp, disklabel_t lp, u_int32_t part,
54 u_int64_t *start, u_int64_t *blocks)
55{
56 struct partition64 *pp;
57
58 if (part >= lp.lab64->d_npartitions)
59 return (EINVAL);
60
61 pp = &lp.lab64->d_partitions[part];
62
63 if ((pp->p_boffset & (ssp->dss_secsize - 1)) ||
64 (pp->p_bsize & (ssp->dss_secsize - 1))) {
65 return (EINVAL);
66 }
67 *start = pp->p_boffset / ssp->dss_secsize;
68 *blocks = pp->p_bsize / ssp->dss_secsize;
69 return(0);
70}
71
72/*
73 * Get the filesystem type XXX - diskslices code needs to use uuids
74 */
75static int
76l64_getpartfstype(disklabel_t lp, u_int32_t part)
77{
78 struct partition64 *pp;
79
80 if (part >= lp.lab64->d_npartitions)
81 return (0);
82 pp = &lp.lab64->d_partitions[part];
83 /* XXX */
84 return (0);
85}
86
87/*
88 * Get the number of partitions
89 */
90static u_int32_t
91l64_getnumparts(disklabel_t lp)
92{
93 return(lp.lab64->d_npartitions);
94}
95
96/*
97 * Attempt to read a disk label from a device. 64 bit disklabels are
98 * sector-agnostic and begin at offset 0 on the device. 64 bit disklabels
99 * may only be used with GPT partitioning schemes.
100 *
101 * Returns NULL on sucess, and an error string on failure.
102 */
103static const char *
104l64_readdisklabel(cdev_t dev, struct diskslice *sp, disklabel_t *lpp,
105 struct disk_info *info)
106{
107 struct buf *bp;
108 struct disklabel64 *dlp;
109 const char *msg;
110 uint32_t savecrc;
111 size_t dlpcrcsize;
112 size_t bpsize;
113 int secsize;
114
115 secsize = info->d_media_blksize;
116 bpsize = (sizeof(*dlp) + secsize - 1) & ~(secsize - 1);
117
118 bp = geteblk(bpsize);
119 bp->b_bio1.bio_offset = 0;
120 bp->b_bcount = bpsize;
121 bp->b_flags &= ~B_INVAL;
122 bp->b_cmd = BUF_CMD_READ;
123 dev_dstrategy(dev, &bp->b_bio1);
124
125 if (biowait(bp)) {
126 msg = "I/O error";
127 } else {
128 dlp = (struct disklabel64 *)bp->b_data;
129 dlpcrcsize = offsetof(struct disklabel64,
130 d_partitions[dlp->d_npartitions]) -
131 offsetof(struct disklabel64, d_magic);
132 savecrc = dlp->d_crc;
133 dlp->d_crc = 0;
134 if (dlp->d_magic != DISKMAGIC64) {
135 msg = "no disk label";
136 } else if (dlp->d_npartitions > MAXPARTITIONS64) {
137 msg = "disklabel64 corrupted, too many partitions";
138 } else if (savecrc != crc32(&dlp->d_magic, dlpcrcsize)) {
139 msg = "disklabel64 corrupted, bad CRC";
140 } else {
141 dlp->d_crc = savecrc;
142 (*lpp).lab64 = kmalloc(sizeof(*dlp),
143 M_DEVBUF, M_WAITOK|M_ZERO);
144 *(*lpp).lab64 = *dlp;
145 msg = NULL;
146 }
147 }
148 bp->b_flags |= B_INVAL | B_AGE;
149 brelse(bp);
150 return (msg);
151}
152
153/*
154 * If everything is good, copy olpx to nlpx. Check to see if any
155 * open partitions would change.
156 */
157static int
158l64_setdisklabel(disklabel_t olpx, disklabel_t nlpx, struct diskslices *ssp,
159 struct diskslice *sp, u_int32_t *openmask)
160{
161 struct disklabel64 *olp, *nlp;
162 struct partition64 *opp, *npp;
163 uint32_t savecrc;
164 uint64_t slicebsize;
165 size_t nlpcrcsize;
166 int part;
167 int i;
168
169 olp = olpx.lab64;
170 nlp = nlpx.lab64;
171
172 slicebsize = (uint64_t)sp->ds_size * ssp->dss_secsize;
173
174 if (nlp->d_magic != DISKMAGIC64)
175 return (EINVAL);
176 if (nlp->d_npartitions > MAXPARTITIONS64)
177 return (EINVAL);
178 savecrc = nlp->d_crc;
179 nlp->d_crc = 0;
180 nlpcrcsize = offsetof(struct disklabel64,
181 d_partitions[nlp->d_npartitions]) -
182 offsetof(struct disklabel64, d_magic);
183 if (crc32(&nlp->d_magic, nlpcrcsize) != savecrc) {
184 nlp->d_crc = savecrc;
185 return (EINVAL);
186 }
187 nlp->d_crc = savecrc;
188
189 /*
190 * Check if open partitions have changed
191 */
192 i = 0;
193 while (i < 128) {
194 if (openmask[i >> 5] == 0) {
195 i += 32;
196 continue;
197 }
198 if ((openmask[i >> 5] & (1 << (i & 31))) == 0) {
199 ++i;
200 continue;
201 }
202 if (nlp->d_npartitions <= i)
203 return (EBUSY);
204 opp = &olp->d_partitions[i];
205 npp = &nlp->d_partitions[i];
206 if (npp->p_boffset != opp->p_boffset ||
207 npp->p_bsize < opp->p_bsize) {
208 return (EBUSY);
209 }
210
211 /*
212 * Do not allow p_type_uuid or p_obj_uuid to change if
213 * the partition is currently open.
214 */
215 if (bcmp(&npp->p_type_uuid, &opp->p_type_uuid,
216 sizeof(npp->p_type_uuid)) != 0) {
217 return (EBUSY);
218 }
219 if (bcmp(&npp->p_obj_uuid, &opp->p_obj_uuid,
220 sizeof(npp->p_obj_uuid)) != 0) {
221 return (EBUSY);
222 }
223 ++i;
224 }
225
226 /*
227 * Make sure the label and partition offsets and sizes are sane.
228 */
229 if (nlp->d_total_size > slicebsize)
230 return (ENOSPC);
231 if (nlp->d_total_size & (ssp->dss_secsize - 1))
232 return (EINVAL);
233 if (nlp->d_bbase & (ssp->dss_secsize - 1))
234 return (EINVAL);
235 if (nlp->d_pbase & (ssp->dss_secsize - 1))
236 return (EINVAL);
237 if (nlp->d_pstop & (ssp->dss_secsize - 1))
238 return (EINVAL);
239 if (nlp->d_abase & (ssp->dss_secsize - 1))
240 return (EINVAL);
241
242 for (part = 0; part < nlp->d_npartitions; ++part) {
243 npp = &nlp->d_partitions[i];
244 if (npp->p_bsize == 0) {
245 if (npp->p_boffset != 0)
246 return (EINVAL);
247 continue;
248 }
249 if (npp->p_boffset & (ssp->dss_secsize - 1))
250 return (EINVAL);
251 if (npp->p_bsize & (ssp->dss_secsize - 1))
252 return (EINVAL);
253 if (npp->p_boffset < nlp->d_pbase)
254 return (ENOSPC);
255 if (npp->p_boffset + npp->p_bsize > nlp->d_total_size)
256 return (ENOSPC);
257 }
258
259 /*
260 * Structurally we may add code to make modifications above in the
261 * future, so regenerate the crc anyway.
262 */
263 nlp->d_crc = 0;
264 nlp->d_crc = crc32(&nlp->d_magic, nlpcrcsize);
265 *olp = *nlp;
266
267 return (0);
268}
269
270/*
271 * Write disk label back to device after modification.
272 */
273static int
274l64_writedisklabel(cdev_t dev, struct diskslices *ssp,
275 struct diskslice *sp, disklabel_t lpx)
276{
277 struct disklabel64 *lp;
278 struct disklabel64 *dlp;
279 struct buf *bp;
280 int error = 0;
281 size_t bpsize;
282 int secsize;
283
284 lp = lpx.lab64;
285
286 secsize = ssp->dss_secsize;
287 bpsize = (sizeof(*lp) + secsize - 1) & ~(secsize - 1);
288
289 bp = geteblk(bpsize);
290 bp->b_bio1.bio_offset = 0;
291 bp->b_bcount = bpsize;
292
293 /*
294 * Because our I/O is larger then the label, and because we do not
295 * write the d_reserved0[] area, do a read-modify-write.
296 */
297 bp->b_flags &= ~B_INVAL;
298 bp->b_cmd = BUF_CMD_READ;
299 dev_dstrategy(dkmodpart(dev, WHOLE_SLICE_PART), &bp->b_bio1);
300 error = biowait(bp);
301 if (error)
302 goto done;
303
304 dlp = (void *)bp->b_data;
305 bcopy(&lp->d_magic, &dlp->d_magic,
306 sizeof(*lp) - offsetof(struct disklabel64, d_magic));
307 bp->b_cmd = BUF_CMD_WRITE;
308 dev_dstrategy(dkmodpart(dev, WHOLE_SLICE_PART), &bp->b_bio1);
309 error = biowait(bp);
310done:
311 bp->b_flags |= B_INVAL | B_AGE;
312 brelse(bp);
313 return (error);
314}
315
316/*
317 * Create a disklabel based on a disk_info structure for the purposes of
318 * DSO_COMPATLABEL - cases where no real label exists on the storage medium.
319 *
320 * If a diskslice is passed, the label is truncated to the slice.
321 *
322 * NOTE! This is not a legal label because d_bbase and d_pbase are both
323 * set to 0.
324 */
325static disklabel_t
326l64_clone_label(struct disk_info *info, struct diskslice *sp)
327{
328 struct disklabel64 *lp;
329 disklabel_t res;
330 uint32_t blksize = info->d_media_blksize;
331 size_t lpcrcsize;
332
333 lp = kmalloc(sizeof *lp, M_DEVBUF, M_WAITOK | M_ZERO);
334
335 if (sp)
336 lp->d_total_size = (uint64_t)sp->ds_size * blksize;
337 else
338 lp->d_total_size = info->d_media_blocks * blksize;
339
340 lp->d_magic = DISKMAGIC64;
341 lp->d_align = blksize;
342 lp->d_npartitions = MAXPARTITIONS64;
343 lp->d_pstop = lp->d_total_size;
344
345 /*
346 * Create a dummy 'c' part and a dummy 'a' part (if requested).
347 * Note that the 'c' part is really a hack. 64 bit disklabels
348 * do not use 'c' to mean the raw partition.
349 */
350
351 lp->d_partitions[2].p_boffset = 0;
352 lp->d_partitions[2].p_bsize = lp->d_total_size;
353 /* XXX SET FS TYPE */
354
355 if (info->d_dsflags & DSO_COMPATPARTA) {
356 lp->d_partitions[0].p_boffset = 0;
357 lp->d_partitions[0].p_bsize = lp->d_total_size;
358 /* XXX SET FS TYPE */
359 }
360
361 lpcrcsize = offsetof(struct disklabel64,
362 d_partitions[lp->d_npartitions]) -
363 offsetof(struct disklabel64, d_magic);
364
365 lp->d_crc = crc32(&lp->d_magic, lpcrcsize);
366 res.lab64 = lp;
367 return (res);
368}
369
370/*
371 * Create a virgin disklabel64 suitable for writing to the media.
372 *
373 * disklabel64 always reserves 32KB for a boot area and leaves room
374 * for up to RESPARTITIONS64 partitions.
375 */
376static void
377l64_makevirginlabel(disklabel_t lpx, struct diskslices *ssp,
378 struct diskslice *sp, struct disk_info *info)
379{
380 struct disklabel64 *lp = lpx.lab64;
381 struct partition64 *pp;
382 uint32_t blksize;
383 uint32_t ressize;
384 uint64_t blkmask; /* 64 bits so we can ~ */
385 size_t lpcrcsize;
386
387 /*
388 * Setup the initial label. Use of a block size of at least 4KB
389 * for calculating the initial reserved areas to allow some degree
390 * of portability between media with different sector sizes.
391 *
392 * Note that the modified blksize is stored in d_align as a hint
393 * to the disklabeling program.
394 */
395 bzero(lp, sizeof(*lp));
396 if ((blksize = info->d_media_blksize) < 4096)
397 blksize = 4096;
398 blkmask = blksize - 1;
399
400 if (sp)
401 lp->d_total_size = (uint64_t)sp->ds_size * ssp->dss_secsize;
402 else
403 lp->d_total_size = info->d_media_blocks * info->d_media_blksize;
404
405 lp->d_magic = DISKMAGIC64;
406 lp->d_align = blksize;
407 lp->d_npartitions = MAXPARTITIONS64;
408 kern_uuidgen(&lp->d_obj_uuid, 1);
409
410 ressize = offsetof(struct disklabel64, d_partitions[RESPARTITIONS64]);
411 ressize = (ressize + (uint32_t)blkmask) & ~blkmask;
412
413 lp->d_bbase = ressize;
414 lp->d_pbase = lp->d_bbase + ((32768 + blkmask) & ~blkmask);
415 lp->d_pstop = (lp->d_total_size - lp->d_bbase) & ~blkmask;
416 lp->d_abase = lp->d_pstop;
417
418 /*
419 * All partitions are left empty unless DSO_COMPATPARTA is set
420 */
421
422 if (info->d_dsflags & DSO_COMPATPARTA) {
423 pp = &lp->d_partitions[0];
424 pp->p_boffset = lp->d_pbase;
425 pp->p_bsize = lp->d_pstop - lp->d_pbase;
426 /* XXX SET FS TYPE */
427 }
428
429 lpcrcsize = offsetof(struct disklabel64,
430 d_partitions[lp->d_npartitions]) -
431 offsetof(struct disklabel64, d_magic);
432 lp->d_crc = crc32(&lp->d_magic, lpcrcsize);
433}
434
435/*
436 * Set the number of blocks at the beginning of the slice which have
437 * been reserved for label operations. This area will be write-protected
438 * when accessed via the slice.
439 *
440 * For now just protect the label area proper. Do not protect the
441 * boot area. Note partitions in 64 bit disklabels do not overlap
442 * the disklabel or boot area.
443 */
444static void
445l64_adjust_label_reserved(struct diskslices *ssp, int slice,
446 struct diskslice *sp)
447{
448 struct disklabel64 *lp = sp->ds_label.lab64;
449
450 sp->ds_reserved = lp->d_bbase / ssp->dss_secsize;
451}
452
453struct disklabel_ops disklabel64_ops = {
454 .labelsize = sizeof(struct disklabel64),
455 .op_readdisklabel = l64_readdisklabel,
456 .op_setdisklabel = l64_setdisklabel,
457 .op_writedisklabel = l64_writedisklabel,
458 .op_clone_label = l64_clone_label,
459 .op_adjust_label_reserved = l64_adjust_label_reserved,
460 .op_getpartbounds = l64_getpartbounds,
461 .op_getpartfstype = l64_getpartfstype,
462 .op_getnumparts = l64_getnumparts,
463 .op_makevirginlabel = l64_makevirginlabel
464};
465