subr_disk_mbr.c revision 1.32 1 /* $NetBSD: subr_disk_mbr.c,v 1.32 2008/12/30 19:38:36 reinoud Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)ufs_disksubr.c 7.16 (Berkeley) 5/4/91
32 */
33
34 /*
35 * Code to find a NetBSD label on a disk that contains an i386 style MBR.
36 * The first NetBSD label found in the 2nd sector of a NetBSD partition
37 * is used.
38 * If we don't find a label searching the MBR, we look at the start of the
39 * disk, if that fails then a label is faked up from the MBR.
40 *
41 * If there isn't a disklabel or anything in the MBR then the disc is searched
42 * for ecma-167/iso9660/udf style partition indicators.
43 * Useful for media or files that contain single filesystems (etc).
44 *
45 * This code will read host endian netbsd labels from little endian MBR.
46 *
47 * Based on the i386 disksubr.c
48 *
49 * Since the mbr only has 32bit fields for sector addresses, we do the same.
50 *
51 * XXX There are potential problems writing labels to disks where there
52 * is only space for 8 netbsd partitions but this code has been compiled
53 * with MAXPARTITIONS=16.
54 */
55
56 #include <sys/cdefs.h>
57 __KERNEL_RCSID(0, "$NetBSD: subr_disk_mbr.c,v 1.32 2008/12/30 19:38:36 reinoud Exp $");
58
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/buf.h>
62 #include <sys/bootblock.h>
63 #include <sys/disklabel.h>
64 #include <sys/disk.h>
65 #include <sys/syslog.h>
66 #include <sys/vnode.h>
67 #include <sys/fcntl.h>
68 #include <sys/conf.h>
69 #include <sys/cdio.h>
70 #include <fs/udf/ecma167-udf.h>
71
72 #include <sys/kauth.h>
73
74 #include "opt_mbr.h"
75
76 typedef struct mbr_partition mbr_partition_t;
77
78 /*
79 * We allocate a buffer 2 sectors large, and look in both....
80 * That means we find labels written by other ports with different offsets.
81 * LABELSECTOR and LABELOFFSET are only used if the disk doesn't have a label.
82 */
83 #if LABELSECTOR > 1 || LABELOFFSET > 512
84 #error Invalid LABELSECTOR or LABELOFFSET
85 #endif
86
87 #define MBR_LABELSECTOR 1
88
89 #define SCAN_CONTINUE 0
90 #define SCAN_FOUND 1
91 #define SCAN_ERROR 2
92
93 typedef struct mbr_args {
94 struct disklabel *lp;
95 void (*strat)(struct buf *);
96 struct buf *bp;
97 const char *msg;
98 int error;
99 int written; /* number of times we wrote label */
100 int found_mbr; /* set if disk has a valid mbr */
101 uint label_sector; /* where we found the label */
102 int action;
103 uint32_t secperunit;
104 #define READ_LABEL 1
105 #define UPDATE_LABEL 2
106 #define WRITE_LABEL 3
107 } mbr_args_t;
108
109 static int validate_label(mbr_args_t *, uint);
110 static int look_netbsd_part(mbr_args_t *, mbr_partition_t *, int, uint);
111 static int write_netbsd_label(mbr_args_t *, mbr_partition_t *, int, uint);
112
113 static int
114 read_sector(mbr_args_t *a, uint sector, int count)
115 {
116 int error;
117
118 error = disk_read_sectors(a->strat, a->lp, a->bp, sector, count);
119 if (error != 0)
120 a->error = error;
121 return error;
122 }
123
124 /*
125 * Scan MBR for partitions, call 'action' routine for each.
126 */
127
128 static int
129 scan_mbr(mbr_args_t *a, int (*actn)(mbr_args_t *, mbr_partition_t *, int, uint))
130 {
131 mbr_partition_t ptns[MBR_PART_COUNT];
132 mbr_partition_t *dp;
133 struct mbr_sector *mbr;
134 uint ext_base, this_ext, next_ext;
135 int rval;
136 int i;
137 int j;
138 #ifdef COMPAT_386BSD_MBRPART
139 int dp_386bsd = -1;
140 int ap_386bsd = -1;
141 #endif
142
143 ext_base = 0;
144 this_ext = 0;
145 for (;;) {
146 if (read_sector(a, this_ext, 1)) {
147 a->msg = "dos partition I/O error";
148 return SCAN_ERROR;
149 }
150
151 /* Note: Magic number is little-endian. */
152 mbr = (void *)a->bp->b_data;
153 if (mbr->mbr_magic != htole16(MBR_MAGIC))
154 return SCAN_CONTINUE;
155
156 /* Copy data out of buffer so action can use bp */
157 memcpy(ptns, &mbr->mbr_parts, sizeof ptns);
158
159 /* Look for drivers and skip them */
160 if (ext_base == 0 && ptns[0].mbrp_type == MBR_PTYPE_DM6_DDO) {
161 /* We've found a DM6 DDO partition type (used by
162 * the Ontrack Disk Manager drivers).
163 *
164 * Ensure that there are no other partitions in the
165 * MBR and jump to the real partition table (stored
166 * in the first sector of the second track). */
167 bool ok = true;
168
169 for (i = 1; i < MBR_PART_COUNT; i++)
170 if (ptns[i].mbrp_type != MBR_PTYPE_UNUSED)
171 ok = false;
172
173 if (ok) {
174 this_ext = le32toh(a->lp->d_secpercyl /
175 a->lp->d_ntracks);
176 continue;
177 }
178 }
179
180 /* look for NetBSD partition */
181 next_ext = 0;
182 dp = ptns;
183 j = 0;
184 for (i = 0; i < MBR_PART_COUNT; i++, dp++) {
185 if (dp->mbrp_type == MBR_PTYPE_UNUSED)
186 continue;
187 /* Check end of partition is inside disk limits */
188 if ((uint64_t)ext_base + le32toh(dp->mbrp_start) +
189 le32toh(dp->mbrp_size) > a->lp->d_secperunit) {
190 /* This mbr doesn't look good.... */
191 a->msg = "mbr partition exceeds disk size";
192 /* ...but don't report this as an error (yet) */
193 return SCAN_CONTINUE;
194 }
195 a->found_mbr = 1;
196 if (MBR_IS_EXTENDED(dp->mbrp_type)) {
197 next_ext = le32toh(dp->mbrp_start);
198 continue;
199 }
200 #ifdef COMPAT_386BSD_MBRPART
201 if (dp->mbrp_type == MBR_PTYPE_386BSD) {
202 /*
203 * If more than one matches, take last,
204 * as NetBSD install tool does.
205 */
206 if (this_ext == 0) {
207 dp_386bsd = i;
208 ap_386bsd = j;
209 }
210 continue;
211 }
212 #endif
213 rval = (*actn)(a, dp, j, this_ext);
214 if (rval != SCAN_CONTINUE)
215 return rval;
216 j++;
217 }
218 if (next_ext == 0)
219 break;
220 if (ext_base == 0) {
221 ext_base = next_ext;
222 next_ext = 0;
223 }
224 next_ext += ext_base;
225 if (next_ext <= this_ext)
226 break;
227 this_ext = next_ext;
228 }
229 #ifdef COMPAT_386BSD_MBRPART
230 if (this_ext == 0 && dp_386bsd != -1)
231 return (*actn)(a, &ptns[dp_386bsd], ap_386bsd, 0);
232 #endif
233 return SCAN_CONTINUE;
234 }
235
236
237 static void
238 scan_iso_vrs_session(mbr_args_t *a, uint32_t first_sector,
239 int *is_iso9660, int *is_udf)
240 {
241 struct vrs_desc *vrsd;
242 uint64_t vrs;
243 int sector_size;
244 int blks;
245
246 sector_size = a->lp->d_secsize;
247 blks = sector_size / DEV_BSIZE;
248
249 /* by definition */
250 vrs = ((32*1024 + sector_size - 1) / sector_size)
251 + first_sector;
252
253 /* read first vrs sector */
254 if (read_sector(a, vrs * blks, blks))
255 return;
256
257 /* skip all CD001 records */
258 vrsd = a->bp->b_data;
259 while (memcmp(vrsd->identifier, "CD001", 5) == 0) {
260 /* for sure */
261 *is_iso9660 = first_sector;
262
263 vrs++;
264 if (read_sector(a, vrs * blks, blks))
265 return;
266 }
267
268 /* search for BEA01 */
269 vrsd = a->bp->b_data;
270 /* printf("vrsd->identifier = `%s`\n", vrsd->identifier); */
271 if (memcmp(vrsd->identifier, "BEA01", 5))
272 return;
273
274 /* read successor */
275 vrs++;
276 if (read_sector(a, vrs * blks, blks))
277 return;
278
279 /* check for NSR[23] */
280 vrsd = a->bp->b_data;
281 if (memcmp(vrsd->identifier, "NSR0", 4))
282 return;
283
284 *is_udf = first_sector;
285 }
286
287
288 /*
289 * Scan for ISO Volume Recognition Sequences
290 */
291
292 static int
293 scan_iso_vrs(mbr_args_t *a)
294 {
295 struct mmc_discinfo di;
296 struct mmc_trackinfo ti;
297 dev_t dev;
298 uint64_t sector;
299 int is_iso9660, is_udf;
300 int tracknr, sessionnr;
301 int new_session, error;
302
303 is_iso9660 = is_udf = -1;
304
305 /* parse all sessions of disc if we're on a SCSI MMC device */
306 if (a->lp->d_flags & D_SCSI_MMC) {
307 /* get disc info */
308 dev = a->bp->b_dev;
309 error = bdev_ioctl(dev, MMCGETDISCINFO, &di, FKIOCTL, curlwp);
310 if (error)
311 return SCAN_CONTINUE;
312
313 /* go trough all (data) tracks */
314 sessionnr = -1;
315 for (tracknr = di.first_track;
316 tracknr <= di.first_track_last_session; tracknr++)
317 {
318 ti.tracknr = tracknr;
319 error = bdev_ioctl(dev, MMCGETTRACKINFO, &ti,
320 FKIOCTL, curlwp);
321 if (error)
322 return SCAN_CONTINUE;
323 new_session = (ti.sessionnr != sessionnr);
324 sessionnr = ti.sessionnr;
325 if (new_session) {
326 if (ti.flags & MMC_TRACKINFO_BLANK)
327 continue;
328 if (!(ti.flags & MMC_TRACKINFO_DATA))
329 continue;
330 sector = ti.track_start;
331 scan_iso_vrs_session(a, sector,
332 &is_iso9660, &is_udf);
333 }
334 }
335 if (is_udf < 0) {
336 /* defaulting udf on the RAW partition */
337 is_udf = 0;
338 }
339 } else {
340 /* try start of disc */
341 sector = 0;
342 scan_iso_vrs_session(a, sector, &is_iso9660, &is_udf);
343 strncpy(a->lp->d_typename, "iso partition", 16);
344 }
345
346 if ((is_iso9660 < 0) && (is_udf < 0))
347 return SCAN_CONTINUE;
348
349 /* add iso9660 partition if found */
350 if (is_iso9660 >= 0) {
351 /* set 'a' partition to iso9660 */
352 a->lp->d_partitions[0].p_offset = 0;
353 a->lp->d_partitions[0].p_size = a->lp->d_secperunit;
354 a->lp->d_partitions[0].p_cdsession = is_iso9660;
355 a->lp->d_partitions[0].p_fstype = FS_ISO9660;
356 } else {
357 a->lp->d_partitions[0].p_size = 0;
358 a->lp->d_partitions[0].p_fstype = FS_UNUSED;
359 }
360
361 /* add udf partition if found */
362 if (is_udf >= 0) {
363 /* set the RAW partion to UDF for CD/USB stick etc */
364 a->lp->d_partitions[RAW_PART].p_fstype = FS_UDF;
365 /* UDF doesn't care about the cd session specified here */
366 }
367
368 return SCAN_FOUND;
369 }
370
371
372 /*
373 * Attempt to read a disk label from a device
374 * using the indicated strategy routine.
375 * The label must be partly set up before this:
376 * secpercyl, secsize and anything required for a block i/o read
377 * operation in the driver's strategy/start routines
378 * must be filled in before calling us.
379 *
380 * If dos partition table requested, attempt to load it and
381 * find disklabel inside a DOS partition. Also, if bad block
382 * table needed, attempt to extract it as well. Return buffer
383 * for use in signalling errors if requested.
384 *
385 * Returns null on success and an error string on failure.
386 */
387 const char *
388 readdisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp,
389 struct cpu_disklabel *osdep)
390 {
391 struct dkbad *bdp;
392 int rval;
393 int i;
394 mbr_args_t a;
395
396 memset(&a, 0, sizeof a);
397 a.lp = lp;
398 a.strat = strat;
399 a.action = READ_LABEL;
400
401 /* minimal requirements for architypal disk label */
402 if (lp->d_secsize == 0)
403 lp->d_secsize = DEV_BSIZE;
404 if (lp->d_secperunit == 0)
405 lp->d_secperunit = 0x1fffffff;
406 a.secperunit = lp->d_secperunit;
407 lp->d_npartitions = RAW_PART + 1;
408 for (i = 0; i < RAW_PART; i++) {
409 lp->d_partitions[i].p_size = 0;
410 lp->d_partitions[i].p_offset = 0;
411 }
412 if (lp->d_partitions[RAW_PART].p_size == 0)
413 lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
414 lp->d_partitions[RAW_PART].p_offset = 0;
415
416 /*
417 * Set partition 'a' to be the whole disk.
418 * Cleared if we find an mbr or a netbsd label.
419 */
420 lp->d_partitions[0].p_size = lp->d_partitions[RAW_PART].p_size;
421 lp->d_partitions[0].p_fstype = FS_BSDFFS;
422
423 /* get a buffer and initialize it */
424
425 /*
426 * XXX somehow memory is getting corrupted on 2048 byte sectors if its
427 * just 2 times 2048!! It even reads only 2048 bytes max in one go on
428 * optical media.
429 */
430
431 a.bp = geteblk(3 * (int)lp->d_secsize);
432 a.bp->b_dev = dev;
433
434 if (osdep)
435 /*
436 * Scan mbr searching for netbsd partition and saving
437 * bios partition information to use if the netbsd one
438 * is absent.
439 */
440 rval = scan_mbr(&a, look_netbsd_part);
441 else
442 rval = SCAN_CONTINUE;
443
444 if (rval == SCAN_CONTINUE) {
445 /* Look at start of disk */
446 rval = validate_label(&a, 0);
447 }
448
449 if (rval == SCAN_CONTINUE) {
450 rval = scan_iso_vrs(&a);
451 }
452 #if 0
453 /*
454 * Save sector where we found the label for the 'don't overwrite
455 * the label' check in bounds_check_with_label.
456 */
457 if (rval == SCAN_FOUND)
458 xxx->label_sector = a.label_sector;
459 #endif
460
461 /* Obtain bad sector table if requested and present */
462 if (rval == SCAN_FOUND && osdep && (lp->d_flags & D_BADSECT)) {
463 struct dkbad *db;
464 int blkno;
465
466 bdp = &osdep->bad;
467 i = 0;
468 rval = SCAN_ERROR;
469 do {
470 /* read a bad sector table */
471 blkno = lp->d_secperunit - lp->d_nsectors + i;
472 if (lp->d_secsize > DEV_BSIZE)
473 blkno *= lp->d_secsize / DEV_BSIZE;
474 else
475 blkno /= DEV_BSIZE / lp->d_secsize;
476 /* if successful, validate, otherwise try another */
477 if (read_sector(&a, blkno, 1)) {
478 a.msg = "bad sector table I/O error";
479 continue;
480 }
481 db = (struct dkbad *)(a.bp->b_data);
482 #define DKBAD_MAGIC 0x4321
483 if (db->bt_mbz != 0 || db->bt_flag != DKBAD_MAGIC) {
484 a.msg = "bad sector table corrupted";
485 continue;
486 }
487 rval = SCAN_FOUND;
488 *bdp = *db;
489 break;
490 } while (a.bp->b_error && (i += 2) < 10 &&
491 i < lp->d_nsectors);
492 }
493
494 brelse(a.bp, 0);
495 if (rval == SCAN_ERROR || rval == SCAN_CONTINUE)
496 return a.msg;
497 return NULL;
498 }
499
500 static int
501 look_netbsd_part(mbr_args_t *a, mbr_partition_t *dp, int slot, uint ext_base)
502 {
503 struct partition *pp;
504 int ptn_base = ext_base + le32toh(dp->mbrp_start);
505 int rval;
506
507 if (
508 #ifdef COMPAT_386BSD_MBRPART
509 dp->mbrp_type == MBR_PTYPE_386BSD ||
510 #endif
511 dp->mbrp_type == MBR_PTYPE_NETBSD) {
512 rval = validate_label(a, ptn_base);
513
514 #if RAW_PART == 3
515 /* Put actual location where we found the label into ptn 2 */
516 if (rval == SCAN_FOUND || a->lp->d_partitions[2].p_size == 0) {
517 a->lp->d_partitions[2].p_size = le32toh(dp->mbrp_size);
518 a->lp->d_partitions[2].p_offset = ptn_base;
519 }
520 #endif
521
522 /* If we got a netbsd label look no further */
523 if (rval == SCAN_FOUND)
524 return rval;
525 }
526
527 /* Install main partitions into e..h and extended into i+ */
528 if (ext_base == 0)
529 slot += 4;
530 else {
531 slot = 4 + MBR_PART_COUNT;
532 pp = &a->lp->d_partitions[slot];
533 for (; slot < MAXPARTITIONS; pp++, slot++) {
534 /* This gets called twice - avoid duplicates */
535 if (pp->p_offset == ptn_base &&
536 pp->p_size == le32toh(dp->mbrp_size))
537 break;
538 if (pp->p_size == 0)
539 break;
540 }
541 }
542
543 if (slot < MAXPARTITIONS) {
544 /* Stop 'a' being the entire disk */
545 a->lp->d_partitions[0].p_size = 0;
546 a->lp->d_partitions[0].p_fstype = 0;
547
548 /* save partition info */
549 pp = &a->lp->d_partitions[slot];
550 pp->p_offset = ptn_base;
551 pp->p_size = le32toh(dp->mbrp_size);
552 pp->p_fstype = xlat_mbr_fstype(dp->mbrp_type);
553
554 if (slot >= a->lp->d_npartitions)
555 a->lp->d_npartitions = slot + 1;
556 }
557
558 return SCAN_CONTINUE;
559 }
560
561
562 static int
563 validate_label(mbr_args_t *a, uint label_sector)
564 {
565 struct disklabel *dlp;
566 char *dlp_lim, *dlp_byte;
567 int error;
568
569 /* Next, dig out disk label */
570 if (read_sector(a, label_sector, 2)) {
571 a->msg = "disk label read failed";
572 return SCAN_ERROR;
573 }
574
575 /* Locate disk label within block and validate */
576 /*
577 * XXX (dsl) This search may be a waste of time, a lot of other i386
578 * code assumes the label is at offset LABELOFFSET (=0) in the sector.
579 *
580 * If we want to support disks from other netbsd ports, then the
581 * code should also allow for a shorter label nearer the end of
582 * the disk sector, and (IIRC) labels within 8k of the disk start.
583 */
584 dlp = (void *)a->bp->b_data;
585 dlp_lim = (char *)a->bp->b_data + a->bp->b_bcount - sizeof *dlp;
586 for (;; dlp = (void *)((char *)dlp + sizeof(long))) {
587 if ((char *)dlp > dlp_lim) {
588 if (a->action != WRITE_LABEL)
589 return SCAN_CONTINUE;
590 /* Write at arch. dependant default location */
591 dlp_byte = (char *)a->bp->b_data + LABELOFFSET;
592 if (label_sector)
593 dlp_byte += MBR_LABELSECTOR * a->lp->d_secsize;
594 else
595 dlp_byte += LABELSECTOR * a->lp->d_secsize;
596 dlp = (void *)dlp_byte;
597 break;
598 }
599 if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC)
600 continue;
601 if (dlp->d_npartitions > MAXPARTITIONS || dkcksum(dlp) != 0) {
602 a->msg = "disk label corrupted";
603 continue;
604 }
605 break;
606 }
607
608 switch (a->action) {
609 case READ_LABEL:
610 *a->lp = *dlp;
611 if ((a->msg = convertdisklabel(a->lp, a->strat, a->bp,
612 a->secperunit)) != NULL)
613 return SCAN_ERROR;
614 a->label_sector = label_sector;
615 return SCAN_FOUND;
616 case UPDATE_LABEL:
617 case WRITE_LABEL:
618 *dlp = *a->lp;
619 a->bp->b_oflags &= ~BO_DONE;
620 a->bp->b_flags &= ~B_READ;
621 a->bp->b_flags |= B_WRITE;
622 (*a->strat)(a->bp);
623 error = biowait(a->bp);
624 if (error != 0) {
625 a->error = error;
626 a->msg = "disk label write failed";
627 return SCAN_ERROR;
628 }
629 a->written++;
630 /* Write label to all mbr partitions */
631 return SCAN_CONTINUE;
632 default:
633 return SCAN_ERROR;
634 }
635 }
636
637 /*
638 * Check new disk label for sensibility
639 * before setting it.
640 */
641 int
642 setdisklabel(struct disklabel *olp, struct disklabel *nlp, u_long openmask,
643 struct cpu_disklabel *osdep)
644 {
645 int i;
646 struct partition *opp, *npp;
647
648 /* sanity clause */
649 if (nlp->d_secpercyl == 0 || nlp->d_secsize == 0
650 || (nlp->d_secsize % DEV_BSIZE) != 0)
651 return (EINVAL);
652
653 /* special case to allow disklabel to be invalidated */
654 if (nlp->d_magic == 0xffffffff) {
655 *olp = *nlp;
656 return (0);
657 }
658
659 if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC ||
660 dkcksum(nlp) != 0)
661 return (EINVAL);
662
663 /* XXX missing check if other dos partitions will be overwritten */
664
665 while (openmask != 0) {
666 i = ffs(openmask) - 1;
667 openmask &= ~(1 << i);
668 if (i > nlp->d_npartitions)
669 return (EBUSY);
670 opp = &olp->d_partitions[i];
671 npp = &nlp->d_partitions[i];
672 /*
673 * Copy internally-set partition information
674 * if new label doesn't include it. XXX
675 */
676 if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) {
677 *npp = *opp;
678 continue;
679 }
680 if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
681 return (EBUSY);
682 }
683 nlp->d_checksum = 0;
684 nlp->d_checksum = dkcksum(nlp);
685 *olp = *nlp;
686 return (0);
687 }
688
689
690 /*
691 * Write disk label back to device after modification.
692 */
693 int
694 writedisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp,
695 struct cpu_disklabel *osdep)
696 {
697 mbr_args_t a;
698
699 memset(&a, 0, sizeof a);
700 a.lp = lp;
701 a.strat = strat;
702
703 /* get a buffer and initialize it */
704 a.bp = geteblk(2 * (int)lp->d_secsize);
705 a.bp->b_dev = dev;
706
707 /* osdep => we expect an mbr with label in netbsd ptn */
708 a.action = osdep != NULL ? WRITE_LABEL : UPDATE_LABEL;
709
710 /* Write/update the label to every netbsd mbr partition */
711 scan_mbr(&a, write_netbsd_label);
712
713 /* Old write the label at the start of the volume on disks that
714 * don't have a valid mbr (always update an existing one) */
715 a.action = a.found_mbr ? UPDATE_LABEL : WRITE_LABEL;
716 validate_label(&a, 0);
717
718 if (a.written == 0 && a.error == 0)
719 a.error = ESRCH;
720
721 brelse(a.bp, 0);
722 return a.error;
723 }
724
725 static int
726 write_netbsd_label(mbr_args_t *a, mbr_partition_t *dp, int slot, uint ext_base)
727 {
728 int ptn_base = ext_base + le32toh(dp->mbrp_start);
729
730 if (dp->mbrp_type != MBR_PTYPE_NETBSD)
731 return SCAN_CONTINUE;
732
733 return validate_label(a, ptn_base);
734 }
735