xref: /dragonfly/sys/kern/subr_diskslice.c (revision 9c600e7d)
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.4 2003/07/22 17:03:33 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 #include <sys/device.h>
62 
63 #include <ufs/ffs/fs.h>
64 
65 #define TRACE(str)	do { if (ds_debug) printf str; } while (0)
66 
67 typedef	u_char	bool_t;
68 
69 static volatile bool_t ds_debug;
70 
71 static struct disklabel *clone_label __P((struct disklabel *lp));
72 static void dsiodone __P((struct buf *bp));
73 static char *fixlabel __P((char *sname, struct diskslice *sp,
74 			   struct disklabel *lp, int writeflag));
75 static void free_ds_label __P((struct diskslices *ssp, int slice));
76 static void partition_info __P((char *sname, int part, struct partition *pp));
77 static void slice_info __P((char *sname, struct diskslice *sp));
78 static void set_ds_label __P((struct diskslices *ssp, int slice,
79 			      struct disklabel *lp));
80 static void set_ds_labeldevs __P((dev_t dev, struct diskslices *ssp));
81 static void set_ds_wlabel __P((struct diskslices *ssp, int slice,
82 			       int wlabel));
83 
84 /*
85  * Duplicate a label for the whole disk, and initialize defaults in the
86  * copy for fields that are not already initialized.  The caller only
87  * needs to initialize d_secsize and d_secperunit, and zero the fields
88  * that are to be defaulted.
89  */
90 static struct disklabel *
91 clone_label(lp)
92 	struct disklabel *lp;
93 {
94 	struct disklabel *lp1;
95 
96 	lp1 = malloc(sizeof *lp1, M_DEVBUF, M_WAITOK);
97 	*lp1 = *lp;
98 	lp = NULL;
99 	if (lp1->d_typename[0] == '\0')
100 		strncpy(lp1->d_typename, "amnesiac", sizeof(lp1->d_typename));
101 	if (lp1->d_packname[0] == '\0')
102 		strncpy(lp1->d_packname, "fictitious", sizeof(lp1->d_packname));
103 	if (lp1->d_nsectors == 0)
104 		lp1->d_nsectors = 32;
105 	if (lp1->d_ntracks == 0)
106 		lp1->d_ntracks = 64;
107 	lp1->d_secpercyl = lp1->d_nsectors * lp1->d_ntracks;
108 	lp1->d_ncylinders = lp1->d_secperunit / lp1->d_secpercyl;
109 	if (lp1->d_rpm == 0)
110 		lp1->d_rpm = 3600;
111 	if (lp1->d_interleave == 0)
112 		lp1->d_interleave = 1;
113 	if (lp1->d_npartitions < RAW_PART + 1)
114 		lp1->d_npartitions = MAXPARTITIONS;
115 	if (lp1->d_bbsize == 0)
116 		lp1->d_bbsize = BBSIZE;
117 	if (lp1->d_sbsize == 0)
118 		lp1->d_sbsize = SBSIZE;
119 	lp1->d_partitions[RAW_PART].p_size = lp1->d_secperunit;
120 	lp1->d_magic = DISKMAGIC;
121 	lp1->d_magic2 = DISKMAGIC;
122 	lp1->d_checksum = dkcksum(lp1);
123 	return (lp1);
124 }
125 
126 /*
127  * Determine the size of the transfer, and make sure it is
128  * within the boundaries of the partition. Adjust transfer
129  * if needed, and signal errors or early completion.
130  *
131  * XXX TODO:
132  *	o Split buffers that are too big for the device.
133  *	o Check for overflow.
134  *	o Finish cleaning this up.
135  */
136 int
137 dscheck(bp, ssp)
138 	struct buf *bp;
139 	struct diskslices *ssp;
140 {
141 	daddr_t	blkno;
142 	u_long	endsecno;
143 	daddr_t	labelsect;
144 	struct disklabel *lp;
145 	char *msg;
146 	long	nsec;
147 	struct partition *pp;
148 	daddr_t	secno;
149 	daddr_t	slicerel_secno;
150 	struct diskslice *sp;
151 	int s;
152 
153 	blkno = bp->b_blkno;
154 	if (blkno < 0) {
155 		printf("dscheck(%s): negative b_blkno %ld\n",
156 		    devtoname(bp->b_dev), (long)blkno);
157 		bp->b_error = EINVAL;
158 		goto bad;
159 	}
160 	sp = &ssp->dss_slices[dkslice(bp->b_dev)];
161 	lp = sp->ds_label;
162 	if (ssp->dss_secmult == 1) {
163 		if (bp->b_bcount % (u_long)DEV_BSIZE)
164 			goto bad_bcount;
165 		secno = blkno;
166 		nsec = bp->b_bcount >> DEV_BSHIFT;
167 	} else if (ssp->dss_secshift != -1) {
168 		if (bp->b_bcount & (ssp->dss_secsize - 1))
169 			goto bad_bcount;
170 		if (blkno & (ssp->dss_secmult - 1))
171 			goto bad_blkno;
172 		secno = blkno >> ssp->dss_secshift;
173 		nsec = bp->b_bcount >> (DEV_BSHIFT + ssp->dss_secshift);
174 	} else {
175 		if (bp->b_bcount % ssp->dss_secsize)
176 			goto bad_bcount;
177 		if (blkno % ssp->dss_secmult)
178 			goto bad_blkno;
179 		secno = blkno / ssp->dss_secmult;
180 		nsec = bp->b_bcount / ssp->dss_secsize;
181 	}
182 	if (lp == NULL) {
183 		labelsect = -LABELSECTOR - 1;
184 		endsecno = sp->ds_size;
185 		slicerel_secno = secno;
186 	} else {
187 		labelsect = lp->d_partitions[LABEL_PART].p_offset;
188 if (labelsect != 0) Debugger("labelsect != 0 in dscheck()");
189 		pp = &lp->d_partitions[dkpart(bp->b_dev)];
190 		endsecno = pp->p_size;
191 		slicerel_secno = pp->p_offset + secno;
192 	}
193 
194 	/* overwriting disk label ? */
195 	/* XXX should also protect bootstrap in first 8K */
196 	if (slicerel_secno <= LABELSECTOR + labelsect &&
197 #if LABELSECTOR != 0
198 	    slicerel_secno + nsec > LABELSECTOR + labelsect &&
199 #endif
200 	    (bp->b_flags & B_READ) == 0 && sp->ds_wlabel == 0) {
201 		bp->b_error = EROFS;
202 		goto bad;
203 	}
204 
205 #if defined(DOSBBSECTOR) && defined(notyet)
206 	/* overwriting master boot record? */
207 	if (slicerel_secno <= DOSBBSECTOR && (bp->b_flags & B_READ) == 0 &&
208 	    sp->ds_wlabel == 0) {
209 		bp->b_error = EROFS;
210 		goto bad;
211 	}
212 #endif
213 
214 	/* beyond partition? */
215 	if (secno + nsec > endsecno) {
216 		/* if exactly at end of disk, return an EOF */
217 		if (secno == endsecno) {
218 			bp->b_resid = bp->b_bcount;
219 			return (0);
220 		}
221 		/* or truncate if part of it fits */
222 		nsec = endsecno - secno;
223 		if (nsec <= 0) {
224 			bp->b_error = EINVAL;
225 			goto bad;
226 		}
227 		bp->b_bcount = nsec * ssp->dss_secsize;
228 	}
229 
230 	bp->b_pblkno = sp->ds_offset + slicerel_secno;
231 
232 	/*
233 	 * Snoop on label accesses if the slice offset is nonzero.  Fudge
234 	 * offsets in the label to keep the in-core label coherent with
235 	 * the on-disk one.
236 	 */
237 	if (slicerel_secno <= LABELSECTOR + labelsect
238 #if LABELSECTOR != 0
239 	    && slicerel_secno + nsec > LABELSECTOR + labelsect
240 #endif
241 	    && sp->ds_offset != 0) {
242 		struct iodone_chain *ic;
243 
244 		ic = malloc(sizeof *ic , M_DEVBUF, M_WAITOK);
245 		ic->ic_prev_flags = bp->b_flags;
246 		ic->ic_prev_iodone = bp->b_iodone;
247 		ic->ic_prev_iodone_chain = bp->b_iodone_chain;
248 		ic->ic_args[0].ia_long = (LABELSECTOR + labelsect -
249 		    slicerel_secno) * ssp->dss_secsize;
250 		ic->ic_args[1].ia_ptr = sp;
251 		bp->b_flags |= B_CALL;
252 		bp->b_iodone = dsiodone;
253 		bp->b_iodone_chain = ic;
254 		if (!(bp->b_flags & B_READ)) {
255 			/*
256 			 * XXX even disklabel(8) writes directly so we need
257 			 * to adjust writes.  Perhaps we should drop support
258 			 * for DIOCWLABEL (always write protect labels) and
259 			 * require the use of DIOCWDINFO.
260 			 *
261 			 * XXX probably need to copy the data to avoid even
262 			 * temporarily corrupting the in-core copy.
263 			 */
264 			if (bp->b_vp != NULL) {
265 				s = splbio();
266 				bp->b_vp->v_numoutput++;
267 				splx(s);
268 			}
269 			/* XXX need name here. */
270 			msg = fixlabel((char *)NULL, sp,
271 				       (struct disklabel *)
272 				       (bp->b_data + ic->ic_args[0].ia_long),
273 				       TRUE);
274 			if (msg != NULL) {
275 				printf("dscheck(%s): %s\n",
276 				    devtoname(bp->b_dev), msg);
277 				bp->b_error = EROFS;
278 				goto bad;
279 			}
280 		}
281 	}
282 	return (1);
283 
284 bad_bcount:
285 	printf(
286 	"dscheck(%s): b_bcount %ld is not on a sector boundary (ssize %d)\n",
287 	    devtoname(bp->b_dev), bp->b_bcount, ssp->dss_secsize);
288 	bp->b_error = EINVAL;
289 	goto bad;
290 
291 bad_blkno:
292 	printf(
293 	"dscheck(%s): b_blkno %ld is not on a sector boundary (ssize %d)\n",
294 	    devtoname(bp->b_dev), (long)blkno, ssp->dss_secsize);
295 	bp->b_error = EINVAL;
296 	goto bad;
297 
298 bad:
299 	bp->b_resid = bp->b_bcount;
300 	bp->b_flags |= B_ERROR;
301 	return (-1);
302 }
303 
304 void
305 dsclose(dev, mode, ssp)
306 	dev_t	dev;
307 	int	mode;
308 	struct diskslices *ssp;
309 {
310 	u_char	mask;
311 	struct diskslice *sp;
312 
313 	sp = &ssp->dss_slices[dkslice(dev)];
314 	mask = 1 << dkpart(dev);
315 	sp->ds_openmask &= ~mask;
316 }
317 
318 void
319 dsgone(sspp)
320 	struct diskslices **sspp;
321 {
322 	int	slice;
323 	struct diskslice *sp;
324 	struct diskslices *ssp;
325 
326 	for (slice = 0, ssp = *sspp; slice < ssp->dss_nslices; slice++) {
327 		sp = &ssp->dss_slices[slice];
328 		free_ds_label(ssp, slice);
329 	}
330 	free(ssp, M_DEVBUF);
331 	*sspp = NULL;
332 }
333 
334 /*
335  * For the "write" commands (DIOCSDINFO and DIOCWDINFO), this
336  * is subject to the same restriction as dsopen().
337  */
338 int
339 dsioctl(dev, cmd, data, flags, sspp)
340 	dev_t	dev;
341 	u_long	cmd;
342 	caddr_t	data;
343 	int	flags;
344 	struct diskslices **sspp;
345 {
346 	int	error;
347 	struct disklabel *lp;
348 	int	old_wlabel;
349 	u_char	openmask;
350 	int	part;
351 	int	slice;
352 	struct diskslice *sp;
353 	struct diskslices *ssp;
354 	struct partition *pp;
355 
356 	slice = dkslice(dev);
357 	ssp = *sspp;
358 	sp = &ssp->dss_slices[slice];
359 	lp = sp->ds_label;
360 	switch (cmd) {
361 
362 	case DIOCGDVIRGIN:
363 		lp = (struct disklabel *)data;
364 		if (ssp->dss_slices[WHOLE_DISK_SLICE].ds_label) {
365 			*lp = *ssp->dss_slices[WHOLE_DISK_SLICE].ds_label;
366 		} else {
367 			bzero(lp, sizeof(struct disklabel));
368 		}
369 
370 		lp->d_magic = DISKMAGIC;
371 		lp->d_magic2 = DISKMAGIC;
372 		pp = &lp->d_partitions[RAW_PART];
373 		pp->p_offset = 0;
374 		pp->p_size = sp->ds_size;
375 
376 		lp->d_npartitions = MAXPARTITIONS;
377 		if (lp->d_interleave == 0)
378 			lp->d_interleave = 1;
379 		if (lp->d_rpm == 0)
380 			lp->d_rpm = 3600;
381 		if (lp->d_nsectors == 0)
382 			lp->d_nsectors = 32;
383 		if (lp->d_ntracks == 0)
384 			lp->d_ntracks = 64;
385 
386 		lp->d_bbsize = BBSIZE;
387 		lp->d_sbsize = SBSIZE;
388 		lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
389 		lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
390 		lp->d_secperunit = sp->ds_size;
391 		lp->d_checksum = 0;
392 		lp->d_checksum = dkcksum(lp);
393 		return (0);
394 
395 	case DIOCGDINFO:
396 		if (lp == NULL)
397 			return (EINVAL);
398 		*(struct disklabel *)data = *lp;
399 		return (0);
400 
401 #ifdef notyet
402 	case DIOCGDINFOP:
403 		if (lp == NULL)
404 			return (EINVAL);
405 		*(struct disklabel **)data = lp;
406 		return (0);
407 #endif
408 
409 	case DIOCGPART:
410 		if (lp == NULL)
411 			return (EINVAL);
412 		((struct partinfo *)data)->disklab = lp;
413 		((struct partinfo *)data)->part
414 			= &lp->d_partitions[dkpart(dev)];
415 		return (0);
416 
417 	case DIOCGSLICEINFO:
418 		bcopy(ssp, data, (char *)&ssp->dss_slices[ssp->dss_nslices] -
419 				 (char *)ssp);
420 		return (0);
421 
422 	case DIOCSDINFO:
423 		if (slice == WHOLE_DISK_SLICE)
424 			return (ENODEV);
425 		if (!(flags & FWRITE))
426 			return (EBADF);
427 		lp = malloc(sizeof *lp, M_DEVBUF, M_WAITOK);
428 		if (sp->ds_label == NULL)
429 			bzero(lp, sizeof *lp);
430 		else
431 			bcopy(sp->ds_label, lp, sizeof *lp);
432 		if (sp->ds_label == NULL)
433 			openmask = 0;
434 		else {
435 			openmask = sp->ds_openmask;
436 			if (slice == COMPATIBILITY_SLICE)
437 				openmask |= ssp->dss_slices[
438 				    ssp->dss_first_bsd_slice].ds_openmask;
439 			else if (slice == ssp->dss_first_bsd_slice)
440 				openmask |= ssp->dss_slices[
441 				    COMPATIBILITY_SLICE].ds_openmask;
442 		}
443 		error = setdisklabel(lp, (struct disklabel *)data,
444 				     (u_long)openmask);
445 		/* XXX why doesn't setdisklabel() check this? */
446 		if (error == 0 && lp->d_partitions[RAW_PART].p_offset != 0)
447 			error = EXDEV;
448 		if (error == 0) {
449 			if (lp->d_secperunit > sp->ds_size)
450 				error = ENOSPC;
451 			for (part = 0; part < lp->d_npartitions; part++)
452 				if (lp->d_partitions[part].p_size > sp->ds_size)
453 					error = ENOSPC;
454 		}
455 		if (error != 0) {
456 			free(lp, M_DEVBUF);
457 			return (error);
458 		}
459 		free_ds_label(ssp, slice);
460 		set_ds_label(ssp, slice, lp);
461 		set_ds_labeldevs(dev, ssp);
462 		return (0);
463 
464 	case DIOCSYNCSLICEINFO:
465 		if (slice != WHOLE_DISK_SLICE || dkpart(dev) != RAW_PART)
466 			return (EINVAL);
467 		if (!*(int *)data)
468 			for (slice = 0; slice < ssp->dss_nslices; slice++) {
469 				openmask = ssp->dss_slices[slice].ds_openmask;
470 				if (openmask
471 				    && (slice != WHOLE_DISK_SLICE
472 					|| openmask & ~(1 << RAW_PART)))
473 					return (EBUSY);
474 			}
475 
476 		/*
477 		 * Temporarily forget the current slices struct and read
478 		 * the current one.
479 		 * XXX should wait for current accesses on this disk to
480 		 * complete, then lock out future accesses and opens.
481 		 */
482 		*sspp = NULL;
483 		lp = malloc(sizeof *lp, M_DEVBUF, M_WAITOK);
484 		*lp = *ssp->dss_slices[WHOLE_DISK_SLICE].ds_label;
485 		error = dsopen(dev, S_IFCHR, ssp->dss_oflags, sspp, lp);
486 		if (error != 0) {
487 			free(lp, M_DEVBUF);
488 			*sspp = ssp;
489 			return (error);
490 		}
491 
492 		/*
493 		 * Reopen everything.  This is a no-op except in the "force"
494 		 * case and when the raw bdev and cdev are both open.  Abort
495 		 * if anything fails.
496 		 */
497 		for (slice = 0; slice < ssp->dss_nslices; slice++) {
498 			for (openmask = ssp->dss_slices[slice].ds_openmask,
499 			     part = 0; openmask; openmask >>= 1, part++) {
500 				if (!(openmask & 1))
501 					continue;
502 				error = dsopen(dkmodslice(dkmodpart(dev, part),
503 							  slice),
504 					       S_IFCHR, ssp->dss_oflags, sspp,
505 					       lp);
506 				if (error != 0) {
507 					free(lp, M_DEVBUF);
508 					*sspp = ssp;
509 					return (EBUSY);
510 				}
511 			}
512 		}
513 
514 		free(lp, M_DEVBUF);
515 		dsgone(&ssp);
516 		return (0);
517 
518 	case DIOCWDINFO:
519 		error = dsioctl(dev, DIOCSDINFO, data, flags, &ssp);
520 		if (error != 0)
521 			return (error);
522 		/*
523 		 * XXX this used to hack on dk_openpart to fake opening
524 		 * partition 0 in case that is used instead of dkpart(dev).
525 		 */
526 		old_wlabel = sp->ds_wlabel;
527 		set_ds_wlabel(ssp, slice, TRUE);
528 		error = writedisklabel(dev, sp->ds_label);
529 		/* XXX should invalidate in-core label if write failed. */
530 		set_ds_wlabel(ssp, slice, old_wlabel);
531 		return (error);
532 
533 	case DIOCWLABEL:
534 #ifndef __alpha__
535 		if (slice == WHOLE_DISK_SLICE)
536 			return (ENODEV);
537 #endif
538 		if (!(flags & FWRITE))
539 			return (EBADF);
540 		set_ds_wlabel(ssp, slice, *(int *)data != 0);
541 		return (0);
542 
543 	default:
544 		return (ENOIOCTL);
545 	}
546 }
547 
548 static void
549 dsiodone(bp)
550 	struct buf *bp;
551 {
552 	struct iodone_chain *ic;
553 	char *msg;
554 
555 	ic = bp->b_iodone_chain;
556 	bp->b_flags = (ic->ic_prev_flags & B_CALL)
557 		      | (bp->b_flags & ~(B_CALL | B_DONE));
558 	bp->b_iodone = ic->ic_prev_iodone;
559 	bp->b_iodone_chain = ic->ic_prev_iodone_chain;
560 	if (!(bp->b_flags & B_READ)
561 	    || (!(bp->b_flags & B_ERROR) && bp->b_error == 0)) {
562 		msg = fixlabel((char *)NULL, ic->ic_args[1].ia_ptr,
563 			       (struct disklabel *)
564 			       (bp->b_data + ic->ic_args[0].ia_long),
565 			       FALSE);
566 		if (msg != NULL)
567 			printf("%s\n", msg);
568 	}
569 	free(ic, M_DEVBUF);
570 	biodone(bp);
571 }
572 
573 int
574 dsisopen(ssp)
575 	struct diskslices *ssp;
576 {
577 	int	slice;
578 
579 	if (ssp == NULL)
580 		return (0);
581 	for (slice = 0; slice < ssp->dss_nslices; slice++)
582 		if (ssp->dss_slices[slice].ds_openmask)
583 			return (1);
584 	return (0);
585 }
586 
587 /*
588  * Allocate a slices "struct" and initialize it to contain only an empty
589  * compatibility slice (pointing to itself), a whole disk slice (covering
590  * the disk as described by the label), and (nslices - BASE_SLICES) empty
591  * slices beginning at BASE_SLICE.
592  */
593 struct diskslices *
594 dsmakeslicestruct(nslices, lp)
595 	int nslices;
596 	struct disklabel *lp;
597 {
598 	struct diskslice *sp;
599 	struct diskslices *ssp;
600 
601 	ssp = malloc(offsetof(struct diskslices, dss_slices) +
602 		     nslices * sizeof *sp, M_DEVBUF, M_WAITOK);
603 	ssp->dss_first_bsd_slice = COMPATIBILITY_SLICE;
604 	ssp->dss_nslices = nslices;
605 	ssp->dss_oflags = 0;
606 	ssp->dss_secmult = lp->d_secsize / DEV_BSIZE;
607 	if (ssp->dss_secmult & (ssp->dss_secmult - 1))
608 		ssp->dss_secshift = -1;
609 	else
610 		ssp->dss_secshift = ffs(ssp->dss_secmult) - 1;
611 	ssp->dss_secsize = lp->d_secsize;
612 	sp = &ssp->dss_slices[0];
613 	bzero(sp, nslices * sizeof *sp);
614 	sp[WHOLE_DISK_SLICE].ds_size = lp->d_secperunit;
615 	return (ssp);
616 }
617 
618 char *
619 dsname(dev, unit, slice, part, partname)
620 	dev_t	dev;
621 	int	unit;
622 	int	slice;
623 	int	part;
624 	char	*partname;
625 {
626 	static char name[32];
627 	const char *dname;
628 
629 	dname = dev_dname(dev);
630 	if (strlen(dname) > 16)
631 		dname = "nametoolong";
632 	snprintf(name, sizeof(name), "%s%d", dname, unit);
633 	partname[0] = '\0';
634 	if (slice != WHOLE_DISK_SLICE || part != RAW_PART) {
635 		partname[0] = 'a' + part;
636 		partname[1] = '\0';
637 		if (slice != COMPATIBILITY_SLICE)
638 			snprintf(name + strlen(name),
639 			    sizeof(name) - strlen(name), "s%d", slice - 1);
640 	}
641 	return (name);
642 }
643 
644 /*
645  * This should only be called when the unit is inactive and the strategy
646  * routine should not allow it to become active unless we call it.  Our
647  * strategy routine must be special to allow activity.
648  */
649 int
650 dsopen(dev, mode, flags, sspp, lp)
651 	dev_t	dev;
652 	int	mode;
653 	u_int	flags;
654 	struct diskslices **sspp;
655 	struct disklabel *lp;
656 {
657 	dev_t	dev1;
658 	int	error;
659 	struct disklabel *lp1;
660 	char	*msg;
661 	u_char	mask;
662 	bool_t	need_init;
663 	int	part;
664 	char	partname[2];
665 	int	slice;
666 	char	*sname;
667 	struct diskslice *sp;
668 	struct diskslices *ssp;
669 	int	unit;
670 
671 	dev->si_bsize_phys = lp->d_secsize;
672 
673 	unit = dkunit(dev);
674 	if (lp->d_secsize % DEV_BSIZE) {
675 		printf("%s: invalid sector size %lu\n", devtoname(dev),
676 		    (u_long)lp->d_secsize);
677 		return (EINVAL);
678 	}
679 
680 	/*
681 	 * XXX reinitialize the slice table unless there is an open device
682 	 * on the unit.  This should only be done if the media has changed.
683 	 */
684 	ssp = *sspp;
685 	need_init = !dsisopen(ssp);
686 	if (ssp != NULL && need_init)
687 		dsgone(sspp);
688 	if (need_init) {
689 		/*
690 		 * Allocate a minimal slices "struct".  This will become
691 		 * the final slices "struct" if we don't want real slices
692 		 * or if we can't find any real slices.
693 		 */
694 		*sspp = dsmakeslicestruct(BASE_SLICE, lp);
695 
696 		if (!(flags & DSO_ONESLICE)) {
697 			TRACE(("dsinit\n"));
698 			error = dsinit(dev, lp, sspp);
699 			if (error != 0) {
700 				dsgone(sspp);
701 				return (error);
702 			}
703 		}
704 		ssp = *sspp;
705 		ssp->dss_oflags = flags;
706 
707 		/*
708 		 * If there are no real slices, then make the compatiblity
709 		 * slice cover the whole disk.
710 		 */
711 		if (ssp->dss_nslices == BASE_SLICE)
712 			ssp->dss_slices[COMPATIBILITY_SLICE].ds_size
713 				= lp->d_secperunit;
714 
715 		/* Point the compatibility slice at the BSD slice, if any. */
716 		for (slice = BASE_SLICE; slice < ssp->dss_nslices; slice++) {
717 			sp = &ssp->dss_slices[slice];
718 			if (sp->ds_type == DOSPTYP_386BSD /* XXX */) {
719 				ssp->dss_first_bsd_slice = slice;
720 				ssp->dss_slices[COMPATIBILITY_SLICE].ds_offset
721 					= sp->ds_offset;
722 				ssp->dss_slices[COMPATIBILITY_SLICE].ds_size
723 					= sp->ds_size;
724 				ssp->dss_slices[COMPATIBILITY_SLICE].ds_type
725 					= sp->ds_type;
726 				break;
727 			}
728 		}
729 
730 		ssp->dss_slices[WHOLE_DISK_SLICE].ds_label = clone_label(lp);
731 		ssp->dss_slices[WHOLE_DISK_SLICE].ds_wlabel = TRUE;
732 	}
733 
734 	/*
735 	 * Initialize secondary info for all slices.  It is needed for more
736 	 * than the current slice in the DEVFS case.  XXX DEVFS is no more.
737 	 */
738 	for (slice = 0; slice < ssp->dss_nslices; slice++) {
739 		sp = &ssp->dss_slices[slice];
740 		if (sp->ds_label != NULL
741 #ifdef __alpha__
742 		    && slice != WHOLE_DISK_SLICE
743 #endif
744 		    )
745 			continue;
746 		dev1 = dkmodslice(dkmodpart(dev, RAW_PART), slice);
747 		sname = dsname(dev, unit, slice, RAW_PART, partname);
748 		/*
749 		 * XXX this should probably only be done for the need_init
750 		 * case, but there may be a problem with DIOCSYNCSLICEINFO.
751 		 */
752 		set_ds_wlabel(ssp, slice, TRUE);	/* XXX invert */
753 		lp1 = clone_label(lp);
754 		TRACE(("readdisklabel\n"));
755 		if (flags & DSO_NOLABELS)
756 			msg = NULL;
757 		else {
758 			msg = readdisklabel(dev1, lp1);
759 
760 			/*
761 			 * readdisklabel() returns NULL for success, and an
762 			 * error string for failure.
763 			 *
764 			 * If there isn't a label on the disk, and if the
765 			 * DSO_COMPATLABEL is set, we want to use the
766 			 * faked-up label provided by the caller.
767 			 *
768 			 * So we set msg to NULL to indicate that there is
769 			 * no failure (since we have a faked-up label),
770 			 * free lp1, and then clone it again from lp.
771 			 * (In case readdisklabel() modified lp1.)
772 			 */
773 			if (msg != NULL && (flags & DSO_COMPATLABEL)) {
774 				msg = NULL;
775 				free(lp1, M_DEVBUF);
776 				lp1 = clone_label(lp);
777 			}
778 		}
779 		if (msg == NULL)
780 			msg = fixlabel(sname, sp, lp1, FALSE);
781 		if (msg == NULL && lp1->d_secsize != ssp->dss_secsize)
782 			msg = "inconsistent sector size";
783 		if (msg != NULL) {
784 			if (sp->ds_type == DOSPTYP_386BSD /* XXX */)
785 				log(LOG_WARNING, "%s: cannot find label (%s)\n",
786 				    sname, msg);
787 			free(lp1, M_DEVBUF);
788 			continue;
789 		}
790 		if (lp1->d_flags & D_BADSECT) {
791 			log(LOG_ERR, "%s: bad sector table not supported\n",
792 			    sname);
793 			free(lp1, M_DEVBUF);
794 			continue;
795 		}
796 		set_ds_label(ssp, slice, lp1);
797 		set_ds_labeldevs(dev1, ssp);
798 		set_ds_wlabel(ssp, slice, FALSE);
799 	}
800 
801 	slice = dkslice(dev);
802 	if (slice >= ssp->dss_nslices)
803 		return (ENXIO);
804 	sp = &ssp->dss_slices[slice];
805 	part = dkpart(dev);
806 	if (part != RAW_PART
807 	    && (sp->ds_label == NULL || part >= sp->ds_label->d_npartitions))
808 		return (EINVAL);	/* XXX needs translation */
809 	mask = 1 << part;
810 	sp->ds_openmask |= mask;
811 	return (0);
812 }
813 
814 int
815 dssize(dev, sspp)
816 	dev_t	dev;
817 	struct diskslices **sspp;
818 {
819 	struct disklabel *lp;
820 	int	part;
821 	int	slice;
822 	struct diskslices *ssp;
823 
824 	slice = dkslice(dev);
825 	part = dkpart(dev);
826 	ssp = *sspp;
827 	if (ssp == NULL || slice >= ssp->dss_nslices
828 	    || !(ssp->dss_slices[slice].ds_openmask & (1 << part))) {
829 		if (dev_dopen(dev, FREAD, S_IFCHR, NULL) != 0)
830 			return (-1);
831 		dev_dclose(dev, FREAD, S_IFCHR, 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