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