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