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