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