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