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