xref: /dragonfly/sys/kern/subr_diskslice.c (revision 1de703da)
1 /*-
2  * Copyright (c) 1994 Bruce D. Evans.
3  * All rights reserved.
4  *
5  * Copyright (c) 1990 The Regents of the University of California.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * William Jolitz.
10  *
11  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
12  * All rights reserved.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. All advertising materials mentioning features or use of this software
23  *    must display the following acknowledgement:
24  *	This product includes software developed by the University of
25  *	California, Berkeley and its contributors.
26  * 4. Neither the name of the University nor the names of its contributors
27  *    may be used to endorse or promote products derived from this software
28  *    without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40  * SUCH DAMAGE.
41  *
42  *	from: @(#)wd.c	7.2 (Berkeley) 5/9/91
43  *	from: wd.c,v 1.55 1994/10/22 01:57:12 phk Exp $
44  *	from: @(#)ufs_disksubr.c	7.16 (Berkeley) 5/4/91
45  *	from: ufs_disksubr.c,v 1.8 1994/06/07 01:21:39 phk Exp $
46  * $FreeBSD: src/sys/kern/subr_diskslice.c,v 1.82.2.6 2001/07/24 09:49:41 dd Exp $
47  * $DragonFly: src/sys/kern/subr_diskslice.c,v 1.2 2003/06/17 04:28:41 dillon Exp $
48  */
49 
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/buf.h>
53 #include <sys/conf.h>
54 #include <sys/disklabel.h>
55 #include <sys/diskslice.h>
56 #include <sys/fcntl.h>
57 #include <sys/malloc.h>
58 #include <sys/stat.h>
59 #include <sys/syslog.h>
60 #include <sys/vnode.h>
61 
62 #include <ufs/ffs/fs.h>
63 
64 #define TRACE(str)	do { if (ds_debug) printf str; } while (0)
65 
66 typedef	u_char	bool_t;
67 
68 static volatile bool_t ds_debug;
69 
70 static struct disklabel *clone_label __P((struct disklabel *lp));
71 static void dsiodone __P((struct buf *bp));
72 static char *fixlabel __P((char *sname, struct diskslice *sp,
73 			   struct disklabel *lp, int writeflag));
74 static void free_ds_label __P((struct diskslices *ssp, int slice));
75 static void partition_info __P((char *sname, int part, struct partition *pp));
76 static void slice_info __P((char *sname, struct diskslice *sp));
77 static void set_ds_label __P((struct diskslices *ssp, int slice,
78 			      struct disklabel *lp));
79 static void set_ds_labeldevs __P((dev_t dev, struct diskslices *ssp));
80 static void set_ds_wlabel __P((struct diskslices *ssp, int slice,
81 			       int wlabel));
82 
83 /*
84  * Duplicate a label for the whole disk, and initialize defaults in the
85  * copy for fields that are not already initialized.  The caller only
86  * needs to initialize d_secsize and d_secperunit, and zero the fields
87  * that are to be defaulted.
88  */
89 static struct disklabel *
90 clone_label(lp)
91 	struct disklabel *lp;
92 {
93 	struct disklabel *lp1;
94 
95 	lp1 = malloc(sizeof *lp1, M_DEVBUF, M_WAITOK);
96 	*lp1 = *lp;
97 	lp = NULL;
98 	if (lp1->d_typename[0] == '\0')
99 		strncpy(lp1->d_typename, "amnesiac", sizeof(lp1->d_typename));
100 	if (lp1->d_packname[0] == '\0')
101 		strncpy(lp1->d_packname, "fictitious", sizeof(lp1->d_packname));
102 	if (lp1->d_nsectors == 0)
103 		lp1->d_nsectors = 32;
104 	if (lp1->d_ntracks == 0)
105 		lp1->d_ntracks = 64;
106 	lp1->d_secpercyl = lp1->d_nsectors * lp1->d_ntracks;
107 	lp1->d_ncylinders = lp1->d_secperunit / lp1->d_secpercyl;
108 	if (lp1->d_rpm == 0)
109 		lp1->d_rpm = 3600;
110 	if (lp1->d_interleave == 0)
111 		lp1->d_interleave = 1;
112 	if (lp1->d_npartitions < RAW_PART + 1)
113 		lp1->d_npartitions = MAXPARTITIONS;
114 	if (lp1->d_bbsize == 0)
115 		lp1->d_bbsize = BBSIZE;
116 	if (lp1->d_sbsize == 0)
117 		lp1->d_sbsize = SBSIZE;
118 	lp1->d_partitions[RAW_PART].p_size = lp1->d_secperunit;
119 	lp1->d_magic = DISKMAGIC;
120 	lp1->d_magic2 = DISKMAGIC;
121 	lp1->d_checksum = dkcksum(lp1);
122 	return (lp1);
123 }
124 
125 /*
126  * Determine the size of the transfer, and make sure it is
127  * within the boundaries of the partition. Adjust transfer
128  * if needed, and signal errors or early completion.
129  *
130  * XXX TODO:
131  *	o Split buffers that are too big for the device.
132  *	o Check for overflow.
133  *	o Finish cleaning this up.
134  */
135 int
136 dscheck(bp, ssp)
137 	struct buf *bp;
138 	struct diskslices *ssp;
139 {
140 	daddr_t	blkno;
141 	u_long	endsecno;
142 	daddr_t	labelsect;
143 	struct disklabel *lp;
144 	char *msg;
145 	long	nsec;
146 	struct partition *pp;
147 	daddr_t	secno;
148 	daddr_t	slicerel_secno;
149 	struct diskslice *sp;
150 	int s;
151 
152 	blkno = bp->b_blkno;
153 	if (blkno < 0) {
154 		printf("dscheck(%s): negative b_blkno %ld\n",
155 		    devtoname(bp->b_dev), (long)blkno);
156 		bp->b_error = EINVAL;
157 		goto bad;
158 	}
159 	sp = &ssp->dss_slices[dkslice(bp->b_dev)];
160 	lp = sp->ds_label;
161 	if (ssp->dss_secmult == 1) {
162 		if (bp->b_bcount % (u_long)DEV_BSIZE)
163 			goto bad_bcount;
164 		secno = blkno;
165 		nsec = bp->b_bcount >> DEV_BSHIFT;
166 	} else if (ssp->dss_secshift != -1) {
167 		if (bp->b_bcount & (ssp->dss_secsize - 1))
168 			goto bad_bcount;
169 		if (blkno & (ssp->dss_secmult - 1))
170 			goto bad_blkno;
171 		secno = blkno >> ssp->dss_secshift;
172 		nsec = bp->b_bcount >> (DEV_BSHIFT + ssp->dss_secshift);
173 	} else {
174 		if (bp->b_bcount % ssp->dss_secsize)
175 			goto bad_bcount;
176 		if (blkno % ssp->dss_secmult)
177 			goto bad_blkno;
178 		secno = blkno / ssp->dss_secmult;
179 		nsec = bp->b_bcount / ssp->dss_secsize;
180 	}
181 	if (lp == NULL) {
182 		labelsect = -LABELSECTOR - 1;
183 		endsecno = sp->ds_size;
184 		slicerel_secno = secno;
185 	} else {
186 		labelsect = lp->d_partitions[LABEL_PART].p_offset;
187 if (labelsect != 0) Debugger("labelsect != 0 in dscheck()");
188 		pp = &lp->d_partitions[dkpart(bp->b_dev)];
189 		endsecno = pp->p_size;
190 		slicerel_secno = pp->p_offset + secno;
191 	}
192 
193 	/* overwriting disk label ? */
194 	/* XXX should also protect bootstrap in first 8K */
195 	if (slicerel_secno <= LABELSECTOR + labelsect &&
196 #if LABELSECTOR != 0
197 	    slicerel_secno + nsec > LABELSECTOR + labelsect &&
198 #endif
199 	    (bp->b_flags & B_READ) == 0 && sp->ds_wlabel == 0) {
200 		bp->b_error = EROFS;
201 		goto bad;
202 	}
203 
204 #if defined(DOSBBSECTOR) && defined(notyet)
205 	/* overwriting master boot record? */
206 	if (slicerel_secno <= DOSBBSECTOR && (bp->b_flags & B_READ) == 0 &&
207 	    sp->ds_wlabel == 0) {
208 		bp->b_error = EROFS;
209 		goto bad;
210 	}
211 #endif
212 
213 	/* beyond partition? */
214 	if (secno + nsec > endsecno) {
215 		/* if exactly at end of disk, return an EOF */
216 		if (secno == endsecno) {
217 			bp->b_resid = bp->b_bcount;
218 			return (0);
219 		}
220 		/* or truncate if part of it fits */
221 		nsec = endsecno - secno;
222 		if (nsec <= 0) {
223 			bp->b_error = EINVAL;
224 			goto bad;
225 		}
226 		bp->b_bcount = nsec * ssp->dss_secsize;
227 	}
228 
229 	bp->b_pblkno = sp->ds_offset + slicerel_secno;
230 
231 	/*
232 	 * Snoop on label accesses if the slice offset is nonzero.  Fudge
233 	 * offsets in the label to keep the in-core label coherent with
234 	 * the on-disk one.
235 	 */
236 	if (slicerel_secno <= LABELSECTOR + labelsect
237 #if LABELSECTOR != 0
238 	    && slicerel_secno + nsec > LABELSECTOR + labelsect
239 #endif
240 	    && sp->ds_offset != 0) {
241 		struct iodone_chain *ic;
242 
243 		ic = malloc(sizeof *ic , M_DEVBUF, M_WAITOK);
244 		ic->ic_prev_flags = bp->b_flags;
245 		ic->ic_prev_iodone = bp->b_iodone;
246 		ic->ic_prev_iodone_chain = bp->b_iodone_chain;
247 		ic->ic_args[0].ia_long = (LABELSECTOR + labelsect -
248 		    slicerel_secno) * ssp->dss_secsize;
249 		ic->ic_args[1].ia_ptr = sp;
250 		bp->b_flags |= B_CALL;
251 		bp->b_iodone = dsiodone;
252 		bp->b_iodone_chain = ic;
253 		if (!(bp->b_flags & B_READ)) {
254 			/*
255 			 * XXX even disklabel(8) writes directly so we need
256 			 * to adjust writes.  Perhaps we should drop support
257 			 * for DIOCWLABEL (always write protect labels) and
258 			 * require the use of DIOCWDINFO.
259 			 *
260 			 * XXX probably need to copy the data to avoid even
261 			 * temporarily corrupting the in-core copy.
262 			 */
263 			if (bp->b_vp != NULL) {
264 				s = splbio();
265 				bp->b_vp->v_numoutput++;
266 				splx(s);
267 			}
268 			/* XXX need name here. */
269 			msg = fixlabel((char *)NULL, sp,
270 				       (struct disklabel *)
271 				       (bp->b_data + ic->ic_args[0].ia_long),
272 				       TRUE);
273 			if (msg != NULL) {
274 				printf("dscheck(%s): %s\n",
275 				    devtoname(bp->b_dev), msg);
276 				bp->b_error = EROFS;
277 				goto bad;
278 			}
279 		}
280 	}
281 	return (1);
282 
283 bad_bcount:
284 	printf(
285 	"dscheck(%s): b_bcount %ld is not on a sector boundary (ssize %d)\n",
286 	    devtoname(bp->b_dev), bp->b_bcount, ssp->dss_secsize);
287 	bp->b_error = EINVAL;
288 	goto bad;
289 
290 bad_blkno:
291 	printf(
292 	"dscheck(%s): b_blkno %ld is not on a sector boundary (ssize %d)\n",
293 	    devtoname(bp->b_dev), (long)blkno, ssp->dss_secsize);
294 	bp->b_error = EINVAL;
295 	goto bad;
296 
297 bad:
298 	bp->b_resid = bp->b_bcount;
299 	bp->b_flags |= B_ERROR;
300 	return (-1);
301 }
302 
303 void
304 dsclose(dev, mode, ssp)
305 	dev_t	dev;
306 	int	mode;
307 	struct diskslices *ssp;
308 {
309 	u_char	mask;
310 	struct diskslice *sp;
311 
312 	sp = &ssp->dss_slices[dkslice(dev)];
313 	mask = 1 << dkpart(dev);
314 	sp->ds_openmask &= ~mask;
315 }
316 
317 void
318 dsgone(sspp)
319 	struct diskslices **sspp;
320 {
321 	int	slice;
322 	struct diskslice *sp;
323 	struct diskslices *ssp;
324 
325 	for (slice = 0, ssp = *sspp; slice < ssp->dss_nslices; slice++) {
326 		sp = &ssp->dss_slices[slice];
327 		free_ds_label(ssp, slice);
328 	}
329 	free(ssp, M_DEVBUF);
330 	*sspp = NULL;
331 }
332 
333 /*
334  * For the "write" commands (DIOCSDINFO and DIOCWDINFO), this
335  * is subject to the same restriction as dsopen().
336  */
337 int
338 dsioctl(dev, cmd, data, flags, sspp)
339 	dev_t	dev;
340 	u_long	cmd;
341 	caddr_t	data;
342 	int	flags;
343 	struct diskslices **sspp;
344 {
345 	int	error;
346 	struct disklabel *lp;
347 	int	old_wlabel;
348 	u_char	openmask;
349 	int	part;
350 	int	slice;
351 	struct diskslice *sp;
352 	struct diskslices *ssp;
353 	struct partition *pp;
354 
355 	slice = dkslice(dev);
356 	ssp = *sspp;
357 	sp = &ssp->dss_slices[slice];
358 	lp = sp->ds_label;
359 	switch (cmd) {
360 
361 	case DIOCGDVIRGIN:
362 		lp = (struct disklabel *)data;
363 		if (ssp->dss_slices[WHOLE_DISK_SLICE].ds_label) {
364 			*lp = *ssp->dss_slices[WHOLE_DISK_SLICE].ds_label;
365 		} else {
366 			bzero(lp, sizeof(struct disklabel));
367 		}
368 
369 		lp->d_magic = DISKMAGIC;
370 		lp->d_magic2 = DISKMAGIC;
371 		pp = &lp->d_partitions[RAW_PART];
372 		pp->p_offset = 0;
373 		pp->p_size = sp->ds_size;
374 
375 		lp->d_npartitions = MAXPARTITIONS;
376 		if (lp->d_interleave == 0)
377 			lp->d_interleave = 1;
378 		if (lp->d_rpm == 0)
379 			lp->d_rpm = 3600;
380 		if (lp->d_nsectors == 0)
381 			lp->d_nsectors = 32;
382 		if (lp->d_ntracks == 0)
383 			lp->d_ntracks = 64;
384 
385 		lp->d_bbsize = BBSIZE;
386 		lp->d_sbsize = SBSIZE;
387 		lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
388 		lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
389 		lp->d_secperunit = sp->ds_size;
390 		lp->d_checksum = 0;
391 		lp->d_checksum = dkcksum(lp);
392 		return (0);
393 
394 	case DIOCGDINFO:
395 		if (lp == NULL)
396 			return (EINVAL);
397 		*(struct disklabel *)data = *lp;
398 		return (0);
399 
400 #ifdef notyet
401 	case DIOCGDINFOP:
402 		if (lp == NULL)
403 			return (EINVAL);
404 		*(struct disklabel **)data = lp;
405 		return (0);
406 #endif
407 
408 	case DIOCGPART:
409 		if (lp == NULL)
410 			return (EINVAL);
411 		((struct partinfo *)data)->disklab = lp;
412 		((struct partinfo *)data)->part
413 			= &lp->d_partitions[dkpart(dev)];
414 		return (0);
415 
416 	case DIOCGSLICEINFO:
417 		bcopy(ssp, data, (char *)&ssp->dss_slices[ssp->dss_nslices] -
418 				 (char *)ssp);
419 		return (0);
420 
421 	case DIOCSDINFO:
422 		if (slice == WHOLE_DISK_SLICE)
423 			return (ENODEV);
424 		if (!(flags & FWRITE))
425 			return (EBADF);
426 		lp = malloc(sizeof *lp, M_DEVBUF, M_WAITOK);
427 		if (sp->ds_label == NULL)
428 			bzero(lp, sizeof *lp);
429 		else
430 			bcopy(sp->ds_label, lp, sizeof *lp);
431 		if (sp->ds_label == NULL)
432 			openmask = 0;
433 		else {
434 			openmask = sp->ds_openmask;
435 			if (slice == COMPATIBILITY_SLICE)
436 				openmask |= ssp->dss_slices[
437 				    ssp->dss_first_bsd_slice].ds_openmask;
438 			else if (slice == ssp->dss_first_bsd_slice)
439 				openmask |= ssp->dss_slices[
440 				    COMPATIBILITY_SLICE].ds_openmask;
441 		}
442 		error = setdisklabel(lp, (struct disklabel *)data,
443 				     (u_long)openmask);
444 		/* XXX why doesn't setdisklabel() check this? */
445 		if (error == 0 && lp->d_partitions[RAW_PART].p_offset != 0)
446 			error = EXDEV;
447 		if (error == 0) {
448 			if (lp->d_secperunit > sp->ds_size)
449 				error = ENOSPC;
450 			for (part = 0; part < lp->d_npartitions; part++)
451 				if (lp->d_partitions[part].p_size > sp->ds_size)
452 					error = ENOSPC;
453 		}
454 		if (error != 0) {
455 			free(lp, M_DEVBUF);
456 			return (error);
457 		}
458 		free_ds_label(ssp, slice);
459 		set_ds_label(ssp, slice, lp);
460 		set_ds_labeldevs(dev, ssp);
461 		return (0);
462 
463 	case DIOCSYNCSLICEINFO:
464 		if (slice != WHOLE_DISK_SLICE || dkpart(dev) != RAW_PART)
465 			return (EINVAL);
466 		if (!*(int *)data)
467 			for (slice = 0; slice < ssp->dss_nslices; slice++) {
468 				openmask = ssp->dss_slices[slice].ds_openmask;
469 				if (openmask
470 				    && (slice != WHOLE_DISK_SLICE
471 					|| openmask & ~(1 << RAW_PART)))
472 					return (EBUSY);
473 			}
474 
475 		/*
476 		 * Temporarily forget the current slices struct and read
477 		 * the current one.
478 		 * XXX should wait for current accesses on this disk to
479 		 * complete, then lock out future accesses and opens.
480 		 */
481 		*sspp = NULL;
482 		lp = malloc(sizeof *lp, M_DEVBUF, M_WAITOK);
483 		*lp = *ssp->dss_slices[WHOLE_DISK_SLICE].ds_label;
484 		error = dsopen(dev, S_IFCHR, ssp->dss_oflags, sspp, lp);
485 		if (error != 0) {
486 			free(lp, M_DEVBUF);
487 			*sspp = ssp;
488 			return (error);
489 		}
490 
491 		/*
492 		 * Reopen everything.  This is a no-op except in the "force"
493 		 * case and when the raw bdev and cdev are both open.  Abort
494 		 * if anything fails.
495 		 */
496 		for (slice = 0; slice < ssp->dss_nslices; slice++) {
497 			for (openmask = ssp->dss_slices[slice].ds_openmask,
498 			     part = 0; openmask; openmask >>= 1, part++) {
499 				if (!(openmask & 1))
500 					continue;
501 				error = dsopen(dkmodslice(dkmodpart(dev, part),
502 							  slice),
503 					       S_IFCHR, ssp->dss_oflags, sspp,
504 					       lp);
505 				if (error != 0) {
506 					free(lp, M_DEVBUF);
507 					*sspp = ssp;
508 					return (EBUSY);
509 				}
510 			}
511 		}
512 
513 		free(lp, M_DEVBUF);
514 		dsgone(&ssp);
515 		return (0);
516 
517 	case DIOCWDINFO:
518 		error = dsioctl(dev, DIOCSDINFO, data, flags, &ssp);
519 		if (error != 0)
520 			return (error);
521 		/*
522 		 * XXX this used to hack on dk_openpart to fake opening
523 		 * partition 0 in case that is used instead of dkpart(dev).
524 		 */
525 		old_wlabel = sp->ds_wlabel;
526 		set_ds_wlabel(ssp, slice, TRUE);
527 		error = writedisklabel(dev, sp->ds_label);
528 		/* XXX should invalidate in-core label if write failed. */
529 		set_ds_wlabel(ssp, slice, old_wlabel);
530 		return (error);
531 
532 	case DIOCWLABEL:
533 #ifndef __alpha__
534 		if (slice == WHOLE_DISK_SLICE)
535 			return (ENODEV);
536 #endif
537 		if (!(flags & FWRITE))
538 			return (EBADF);
539 		set_ds_wlabel(ssp, slice, *(int *)data != 0);
540 		return (0);
541 
542 	default:
543 		return (ENOIOCTL);
544 	}
545 }
546 
547 static void
548 dsiodone(bp)
549 	struct buf *bp;
550 {
551 	struct iodone_chain *ic;
552 	char *msg;
553 
554 	ic = bp->b_iodone_chain;
555 	bp->b_flags = (ic->ic_prev_flags & B_CALL)
556 		      | (bp->b_flags & ~(B_CALL | B_DONE));
557 	bp->b_iodone = ic->ic_prev_iodone;
558 	bp->b_iodone_chain = ic->ic_prev_iodone_chain;
559 	if (!(bp->b_flags & B_READ)
560 	    || (!(bp->b_flags & B_ERROR) && bp->b_error == 0)) {
561 		msg = fixlabel((char *)NULL, ic->ic_args[1].ia_ptr,
562 			       (struct disklabel *)
563 			       (bp->b_data + ic->ic_args[0].ia_long),
564 			       FALSE);
565 		if (msg != NULL)
566 			printf("%s\n", msg);
567 	}
568 	free(ic, M_DEVBUF);
569 	biodone(bp);
570 }
571 
572 int
573 dsisopen(ssp)
574 	struct diskslices *ssp;
575 {
576 	int	slice;
577 
578 	if (ssp == NULL)
579 		return (0);
580 	for (slice = 0; slice < ssp->dss_nslices; slice++)
581 		if (ssp->dss_slices[slice].ds_openmask)
582 			return (1);
583 	return (0);
584 }
585 
586 /*
587  * Allocate a slices "struct" and initialize it to contain only an empty
588  * compatibility slice (pointing to itself), a whole disk slice (covering
589  * the disk as described by the label), and (nslices - BASE_SLICES) empty
590  * slices beginning at BASE_SLICE.
591  */
592 struct diskslices *
593 dsmakeslicestruct(nslices, lp)
594 	int nslices;
595 	struct disklabel *lp;
596 {
597 	struct diskslice *sp;
598 	struct diskslices *ssp;
599 
600 	ssp = malloc(offsetof(struct diskslices, dss_slices) +
601 		     nslices * sizeof *sp, M_DEVBUF, M_WAITOK);
602 	ssp->dss_first_bsd_slice = COMPATIBILITY_SLICE;
603 	ssp->dss_nslices = nslices;
604 	ssp->dss_oflags = 0;
605 	ssp->dss_secmult = lp->d_secsize / DEV_BSIZE;
606 	if (ssp->dss_secmult & (ssp->dss_secmult - 1))
607 		ssp->dss_secshift = -1;
608 	else
609 		ssp->dss_secshift = ffs(ssp->dss_secmult) - 1;
610 	ssp->dss_secsize = lp->d_secsize;
611 	sp = &ssp->dss_slices[0];
612 	bzero(sp, nslices * sizeof *sp);
613 	sp[WHOLE_DISK_SLICE].ds_size = lp->d_secperunit;
614 	return (ssp);
615 }
616 
617 char *
618 dsname(dev, unit, slice, part, partname)
619 	dev_t	dev;
620 	int	unit;
621 	int	slice;
622 	int	part;
623 	char	*partname;
624 {
625 	static char name[32];
626 	const char *dname;
627 
628 	dname = devsw(dev)->d_name;
629 	if (strlen(dname) > 16)
630 		dname = "nametoolong";
631 	snprintf(name, sizeof(name), "%s%d", dname, unit);
632 	partname[0] = '\0';
633 	if (slice != WHOLE_DISK_SLICE || part != RAW_PART) {
634 		partname[0] = 'a' + part;
635 		partname[1] = '\0';
636 		if (slice != COMPATIBILITY_SLICE)
637 			snprintf(name + strlen(name),
638 			    sizeof(name) - strlen(name), "s%d", slice - 1);
639 	}
640 	return (name);
641 }
642 
643 /*
644  * This should only be called when the unit is inactive and the strategy
645  * routine should not allow it to become active unless we call it.  Our
646  * strategy routine must be special to allow activity.
647  */
648 int
649 dsopen(dev, mode, flags, sspp, lp)
650 	dev_t	dev;
651 	int	mode;
652 	u_int	flags;
653 	struct diskslices **sspp;
654 	struct disklabel *lp;
655 {
656 	dev_t	dev1;
657 	int	error;
658 	struct disklabel *lp1;
659 	char	*msg;
660 	u_char	mask;
661 	bool_t	need_init;
662 	int	part;
663 	char	partname[2];
664 	int	slice;
665 	char	*sname;
666 	struct diskslice *sp;
667 	struct diskslices *ssp;
668 	int	unit;
669 
670 	dev->si_bsize_phys = lp->d_secsize;
671 
672 	unit = dkunit(dev);
673 	if (lp->d_secsize % DEV_BSIZE) {
674 		printf("%s: invalid sector size %lu\n", devtoname(dev),
675 		    (u_long)lp->d_secsize);
676 		return (EINVAL);
677 	}
678 
679 	/*
680 	 * XXX reinitialize the slice table unless there is an open device
681 	 * on the unit.  This should only be done if the media has changed.
682 	 */
683 	ssp = *sspp;
684 	need_init = !dsisopen(ssp);
685 	if (ssp != NULL && need_init)
686 		dsgone(sspp);
687 	if (need_init) {
688 		/*
689 		 * Allocate a minimal slices "struct".  This will become
690 		 * the final slices "struct" if we don't want real slices
691 		 * or if we can't find any real slices.
692 		 */
693 		*sspp = dsmakeslicestruct(BASE_SLICE, lp);
694 
695 		if (!(flags & DSO_ONESLICE)) {
696 			TRACE(("dsinit\n"));
697 			error = dsinit(dev, lp, sspp);
698 			if (error != 0) {
699 				dsgone(sspp);
700 				return (error);
701 			}
702 		}
703 		ssp = *sspp;
704 		ssp->dss_oflags = flags;
705 
706 		/*
707 		 * If there are no real slices, then make the compatiblity
708 		 * slice cover the whole disk.
709 		 */
710 		if (ssp->dss_nslices == BASE_SLICE)
711 			ssp->dss_slices[COMPATIBILITY_SLICE].ds_size
712 				= lp->d_secperunit;
713 
714 		/* Point the compatibility slice at the BSD slice, if any. */
715 		for (slice = BASE_SLICE; slice < ssp->dss_nslices; slice++) {
716 			sp = &ssp->dss_slices[slice];
717 			if (sp->ds_type == DOSPTYP_386BSD /* XXX */) {
718 				ssp->dss_first_bsd_slice = slice;
719 				ssp->dss_slices[COMPATIBILITY_SLICE].ds_offset
720 					= sp->ds_offset;
721 				ssp->dss_slices[COMPATIBILITY_SLICE].ds_size
722 					= sp->ds_size;
723 				ssp->dss_slices[COMPATIBILITY_SLICE].ds_type
724 					= sp->ds_type;
725 				break;
726 			}
727 		}
728 
729 		ssp->dss_slices[WHOLE_DISK_SLICE].ds_label = clone_label(lp);
730 		ssp->dss_slices[WHOLE_DISK_SLICE].ds_wlabel = TRUE;
731 	}
732 
733 	/*
734 	 * Initialize secondary info for all slices.  It is needed for more
735 	 * than the current slice in the DEVFS case.  XXX DEVFS is no more.
736 	 */
737 	for (slice = 0; slice < ssp->dss_nslices; slice++) {
738 		sp = &ssp->dss_slices[slice];
739 		if (sp->ds_label != NULL
740 #ifdef __alpha__
741 		    && slice != WHOLE_DISK_SLICE
742 #endif
743 		    )
744 			continue;
745 		dev1 = dkmodslice(dkmodpart(dev, RAW_PART), slice);
746 		sname = dsname(dev, unit, slice, RAW_PART, partname);
747 		/*
748 		 * XXX this should probably only be done for the need_init
749 		 * case, but there may be a problem with DIOCSYNCSLICEINFO.
750 		 */
751 		set_ds_wlabel(ssp, slice, TRUE);	/* XXX invert */
752 		lp1 = clone_label(lp);
753 		TRACE(("readdisklabel\n"));
754 		if (flags & DSO_NOLABELS)
755 			msg = NULL;
756 		else {
757 			msg = readdisklabel(dev1, lp1);
758 
759 			/*
760 			 * readdisklabel() returns NULL for success, and an
761 			 * error string for failure.
762 			 *
763 			 * If there isn't a label on the disk, and if the
764 			 * DSO_COMPATLABEL is set, we want to use the
765 			 * faked-up label provided by the caller.
766 			 *
767 			 * So we set msg to NULL to indicate that there is
768 			 * no failure (since we have a faked-up label),
769 			 * free lp1, and then clone it again from lp.
770 			 * (In case readdisklabel() modified lp1.)
771 			 */
772 			if (msg != NULL && (flags & DSO_COMPATLABEL)) {
773 				msg = NULL;
774 				free(lp1, M_DEVBUF);
775 				lp1 = clone_label(lp);
776 			}
777 		}
778 		if (msg == NULL)
779 			msg = fixlabel(sname, sp, lp1, FALSE);
780 		if (msg == NULL && lp1->d_secsize != ssp->dss_secsize)
781 			msg = "inconsistent sector size";
782 		if (msg != NULL) {
783 			if (sp->ds_type == DOSPTYP_386BSD /* XXX */)
784 				log(LOG_WARNING, "%s: cannot find label (%s)\n",
785 				    sname, msg);
786 			free(lp1, M_DEVBUF);
787 			continue;
788 		}
789 		if (lp1->d_flags & D_BADSECT) {
790 			log(LOG_ERR, "%s: bad sector table not supported\n",
791 			    sname);
792 			free(lp1, M_DEVBUF);
793 			continue;
794 		}
795 		set_ds_label(ssp, slice, lp1);
796 		set_ds_labeldevs(dev1, ssp);
797 		set_ds_wlabel(ssp, slice, FALSE);
798 	}
799 
800 	slice = dkslice(dev);
801 	if (slice >= ssp->dss_nslices)
802 		return (ENXIO);
803 	sp = &ssp->dss_slices[slice];
804 	part = dkpart(dev);
805 	if (part != RAW_PART
806 	    && (sp->ds_label == NULL || part >= sp->ds_label->d_npartitions))
807 		return (EINVAL);	/* XXX needs translation */
808 	mask = 1 << part;
809 	sp->ds_openmask |= mask;
810 	return (0);
811 }
812 
813 int
814 dssize(dev, sspp)
815 	dev_t	dev;
816 	struct diskslices **sspp;
817 {
818 	struct disklabel *lp;
819 	int	part;
820 	int	slice;
821 	struct diskslices *ssp;
822 
823 	slice = dkslice(dev);
824 	part = dkpart(dev);
825 	ssp = *sspp;
826 	if (ssp == NULL || slice >= ssp->dss_nslices
827 	    || !(ssp->dss_slices[slice].ds_openmask & (1 << part))) {
828 		if (devsw(dev)->d_open(dev, FREAD, S_IFCHR,
829 		    (struct proc *)NULL) != 0)
830 			return (-1);
831 		devsw(dev)->d_close(dev, FREAD, S_IFCHR, (struct proc *)NULL);
832 		ssp = *sspp;
833 	}
834 	lp = ssp->dss_slices[slice].ds_label;
835 	if (lp == NULL)
836 		return (-1);
837 	return ((int)lp->d_partitions[part].p_size);
838 }
839 
840 static void
841 free_ds_label(ssp, slice)
842 	struct diskslices *ssp;
843 	int	slice;
844 {
845 	struct disklabel *lp;
846 	struct diskslice *sp;
847 
848 	sp = &ssp->dss_slices[slice];
849 	lp = sp->ds_label;
850 	if (lp == NULL)
851 		return;
852 	free(lp, M_DEVBUF);
853 	set_ds_label(ssp, slice, (struct disklabel *)NULL);
854 }
855 
856 static char *
857 fixlabel(sname, sp, lp, writeflag)
858 	char	*sname;
859 	struct diskslice *sp;
860 	struct disklabel *lp;
861 	int	writeflag;
862 {
863 	u_long	end;
864 	u_long	offset;
865 	int	part;
866 	struct partition *pp;
867 	u_long	start;
868 	bool_t	warned;
869 
870 	/* These errors "can't happen" so don't bother reporting details. */
871 	if (lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC)
872 		return ("fixlabel: invalid magic");
873 	if (dkcksum(lp) != 0)
874 		return ("fixlabel: invalid checksum");
875 
876 	pp = &lp->d_partitions[RAW_PART];
877 	if (writeflag) {
878 		start = 0;
879 		offset = sp->ds_offset;
880 	} else {
881 		start = sp->ds_offset;
882 		offset = -sp->ds_offset;
883 	}
884 	if (pp->p_offset != start) {
885 		if (sname != NULL) {
886 			printf(
887 "%s: rejecting BSD label: raw partition offset != slice offset\n",
888 			       sname);
889 			slice_info(sname, sp);
890 			partition_info(sname, RAW_PART, pp);
891 		}
892 		return ("fixlabel: raw partition offset != slice offset");
893 	}
894 	if (pp->p_size != sp->ds_size) {
895 		if (sname != NULL) {
896 			printf("%s: raw partition size != slice size\n", sname);
897 			slice_info(sname, sp);
898 			partition_info(sname, RAW_PART, pp);
899 		}
900 		if (pp->p_size > sp->ds_size) {
901 			if (sname == NULL)
902 				return ("fixlabel: raw partition size > slice size");
903 			printf("%s: truncating raw partition\n", sname);
904 			pp->p_size = sp->ds_size;
905 		}
906 	}
907 	end = start + sp->ds_size;
908 	if (start > end)
909 		return ("fixlabel: slice wraps");
910 	if (lp->d_secpercyl <= 0)
911 		return ("fixlabel: d_secpercyl <= 0");
912 	pp -= RAW_PART;
913 	warned = FALSE;
914 	for (part = 0; part < lp->d_npartitions; part++, pp++) {
915 		if (pp->p_offset != 0 || pp->p_size != 0) {
916 			if (pp->p_offset < start
917 			    || pp->p_offset + pp->p_size > end
918 			    || pp->p_offset + pp->p_size < pp->p_offset) {
919 				if (sname != NULL) {
920 					printf(
921 "%s: rejecting partition in BSD label: it isn't entirely within the slice\n",
922 					       sname);
923 					if (!warned) {
924 						slice_info(sname, sp);
925 						warned = TRUE;
926 					}
927 					partition_info(sname, part, pp);
928 				}
929 				/* XXX else silently discard junk. */
930 				bzero(pp, sizeof *pp);
931 			} else
932 				pp->p_offset += offset;
933 		}
934 	}
935 	lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
936 	lp->d_secperunit = sp->ds_size;
937  	lp->d_checksum = 0;
938  	lp->d_checksum = dkcksum(lp);
939 	return (NULL);
940 }
941 
942 static void
943 partition_info(sname, part, pp)
944 	char	*sname;
945 	int	part;
946 	struct partition *pp;
947 {
948 	printf("%s%c: start %lu, end %lu, size %lu\n", sname, 'a' + part,
949 	       (u_long)pp->p_offset, (u_long)(pp->p_offset + pp->p_size - 1),
950 	       (u_long)pp->p_size);
951 }
952 
953 static void
954 slice_info(sname, sp)
955 	char	*sname;
956 	struct diskslice *sp;
957 {
958 	printf("%s: start %lu, end %lu, size %lu\n", sname,
959 	       sp->ds_offset, sp->ds_offset + sp->ds_size - 1, sp->ds_size);
960 }
961 
962 static void
963 set_ds_label(ssp, slice, lp)
964 	struct diskslices *ssp;
965 	int	slice;
966 	struct disklabel *lp;
967 {
968 	ssp->dss_slices[slice].ds_label = lp;
969 	if (slice == COMPATIBILITY_SLICE)
970 		ssp->dss_slices[ssp->dss_first_bsd_slice].ds_label = lp;
971 	else if (slice == ssp->dss_first_bsd_slice)
972 		ssp->dss_slices[COMPATIBILITY_SLICE].ds_label = lp;
973 }
974 
975 /* XXX remove this? */
976 static void
977 set_ds_labeldevs(dev, ssp)
978 	dev_t	dev;
979 	struct diskslices *ssp;
980 {
981 }
982 
983 static void
984 set_ds_wlabel(ssp, slice, wlabel)
985 	struct diskslices *ssp;
986 	int	slice;
987 	int	wlabel;
988 {
989 	ssp->dss_slices[slice].ds_wlabel = wlabel;
990 	if (slice == COMPATIBILITY_SLICE)
991 		ssp->dss_slices[ssp->dss_first_bsd_slice].ds_wlabel = wlabel;
992 	else if (slice == ssp->dss_first_bsd_slice)
993 		ssp->dss_slices[COMPATIBILITY_SLICE].ds_wlabel = wlabel;
994 }
995