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