Style(9) cleanup to src/sys/netinet6:
[dragonfly.git] / sys / kern / subr_disk.c
1 /*
2  * ----------------------------------------------------------------------------
3  * "THE BEER-WARE LICENSE" (Revision 42):
4  * <phk@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
5  * can do whatever you want with this stuff. If we meet some day, and you think
6  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7  * ----------------------------------------------------------------------------
8  *
9  * Copyright (c) 2004 Matthew Dillon.
10  * Copyright (c) 1982, 1986, 1988, 1993
11  *      The Regents of the University of California.  All rights reserved.
12  * (c) UNIX System Laboratories, Inc.
13  * All or some portions of this file are derived from material licensed
14  * to the University of California by American Telephone and Telegraph
15  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
16  * the permission of UNIX System Laboratories, Inc.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
21  * 1. Redistributions of source code must retain the above copyright
22  *    notice, this list of conditions and the following disclaimer.
23  * 2. Redistributions in binary form must reproduce the above copyright
24  *    notice, this list of conditions and the following disclaimer in the
25  *    documentation and/or other materials provided with the distribution.
26  * 3. All advertising materials mentioning features or use of this software
27  *    must display the following acknowledgement:
28  *      This product includes software developed by the University of
29  *      California, Berkeley and its contributors.
30  * 4. Neither the name of the University nor the names of its contributors
31  *    may be used to endorse or promote products derived from this software
32  *    without specific prior written permission.
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44  * SUCH DAMAGE.
45  *
46  *      @(#)ufs_disksubr.c      8.5 (Berkeley) 1/21/94
47  * $FreeBSD: src/sys/kern/subr_disk.c,v 1.20.2.6 2001/10/05 07:14:57 peter Exp $
48  * $FreeBSD: src/sys/ufs/ufs/ufs_disksubr.c,v 1.44.2.3 2001/03/05 05:42:19 obrien Exp $
49  * $DragonFly: src/sys/kern/subr_disk.c,v 1.10 2004/05/19 22:52:58 dillon Exp $
50  */
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/proc.h>
56 #include <sys/sysctl.h>
57 #include <sys/buf.h>
58 #include <sys/conf.h>
59 #include <sys/disklabel.h>
60 #include <sys/diskslice.h>
61 #include <sys/disk.h>
62 #include <sys/malloc.h>
63 #include <sys/sysctl.h>
64 #include <machine/md_var.h>
65 #include <sys/ctype.h>
66 #include <sys/syslog.h>
67 #include <sys/device.h>
68 #include <sys/msgport.h>
69 #include <sys/msgport2.h>
70 #include <sys/buf2.h>
71
72 static MALLOC_DEFINE(M_DISK, "disk", "disk data");
73
74 static d_strategy_t diskstrategy;
75 static d_open_t diskopen;
76 static d_close_t diskclose; 
77 static d_ioctl_t diskioctl;
78 static d_psize_t diskpsize;
79 static d_clone_t diskclone;
80 static int disk_putport(lwkt_port_t port, lwkt_msg_t msg);
81
82 static LIST_HEAD(, disk) disklist = LIST_HEAD_INITIALIZER(&disklist);
83
84 /*
85  * Create a slice and unit managed disk.
86  *
87  * Our port layer will be responsible for assigning pblkno and handling
88  * high level partition operations, then forwarding the requests to the
89  * raw device.
90  *
91  * The raw device (based on rawsw) is returned to the caller, NOT the
92  * slice and unit managed cdev.  The caller typically sets various
93  * driver parameters and IO limits on the returned rawdev which we must
94  * inherit when our managed device is opened.
95  */
96 dev_t
97 disk_create(int unit, struct disk *dp, int flags, struct cdevsw *rawsw)
98 {
99         dev_t rawdev;
100         struct cdevsw *devsw;
101
102         /*
103          * Create the raw backing device
104          */
105         compile_devsw(rawsw);
106         rawdev = make_dev(rawsw,
107                             dkmakeminor(unit, WHOLE_DISK_SLICE, RAW_PART),
108                             UID_ROOT, GID_OPERATOR, 0640,
109                             "%s%d", rawsw->d_name, unit);
110
111         /*
112          * Initialize our intercept port
113          */
114         bzero(dp, sizeof(*dp));
115         lwkt_initport(&dp->d_port, NULL);
116         dp->d_port.mp_putport = disk_putport;
117         dp->d_rawsw = rawsw;
118
119         /*
120          * We install a custom cdevsw rather then the passed cdevsw,
121          * and save our disk structure in d_data so we can get at it easily
122          * without any complex cloning code.
123          */
124         devsw = cdevsw_add_override(rawdev, dkunitmask(), dkmakeunit(unit));
125         devsw->d_port = &dp->d_port;
126         devsw->d_data = dp;
127         devsw->d_clone = diskclone;
128         dp->d_devsw = devsw;
129         dp->d_rawdev = rawdev;
130         dp->d_cdev = make_dev(devsw, 
131                             dkmakeminor(unit, WHOLE_DISK_SLICE, RAW_PART),
132                             UID_ROOT, GID_OPERATOR, 0640,
133                             "%s%d", devsw->d_name, unit);
134
135         dp->d_dsflags = flags;
136         LIST_INSERT_HEAD(&disklist, dp, d_list);
137         return (dp->d_rawdev);
138 }
139
140 /*
141  * This routine is called when an adapter detaches.  The higher level
142  * managed disk device is destroyed while the lower level raw device is
143  * released.
144  */
145 void
146 disk_destroy(struct disk *disk)
147 {
148         if (disk->d_devsw) {
149             cdevsw_remove(disk->d_devsw, dkunitmask(), dkunit(disk->d_cdev));
150             LIST_REMOVE(disk, d_list);
151         }
152         if (disk->d_rawsw)
153             destroy_all_dev(disk->d_rawsw, dkunitmask(), dkunit(disk->d_rawdev));
154         bzero(disk, sizeof(*disk));
155 }
156
157 int
158 disk_dumpcheck(dev_t dev, u_int *count, u_int *blkno, u_int *secsize)
159 {
160         struct disk *dp;
161         struct disklabel *dl;
162         u_int boff;
163
164         dp = dev->si_disk;
165         if (!dp)
166                 return (ENXIO);
167         if (!dp->d_slice)
168                 return (ENXIO);
169         dl = dsgetlabel(dev, dp->d_slice);
170         if (!dl)
171                 return (ENXIO);
172         *count = Maxmem * (PAGE_SIZE / dl->d_secsize);
173         if (dumplo <= LABELSECTOR || 
174             (dumplo + *count > dl->d_partitions[dkpart(dev)].p_size))
175                 return (EINVAL);
176         boff = dl->d_partitions[dkpart(dev)].p_offset +
177             dp->d_slice->dss_slices[dkslice(dev)].ds_offset;
178         *blkno = boff + dumplo;
179         *secsize = dl->d_secsize;
180         return (0);
181         
182 }
183
184 void 
185 disk_invalidate (struct disk *disk)
186 {
187         if (disk->d_slice)
188                 dsgone(&disk->d_slice);
189 }
190
191 struct disk *
192 disk_enumerate(struct disk *disk)
193 {
194         if (!disk)
195                 return (LIST_FIRST(&disklist));
196         else
197                 return (LIST_NEXT(disk, d_list));
198 }
199
200 static int
201 sysctl_disks(SYSCTL_HANDLER_ARGS)
202 {
203         struct disk *disk;
204         int error, first;
205
206         disk = NULL;
207         first = 1;
208
209         while ((disk = disk_enumerate(disk))) {
210                 if (!first) {
211                         error = SYSCTL_OUT(req, " ", 1);
212                         if (error)
213                                 return error;
214                 } else {
215                         first = 0;
216                 }
217                 error = SYSCTL_OUT(req, disk->d_rawdev->si_name, strlen(disk->d_rawdev->si_name));
218                 if (error)
219                         return error;
220         }
221         error = SYSCTL_OUT(req, "", 1);
222         return error;
223 }
224  
225 SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD, 0, NULL, 
226     sysctl_disks, "A", "names of available disks");
227
228 /*
229  * The port intercept functions
230  */
231 static
232 int
233 disk_putport(lwkt_port_t port, lwkt_msg_t lmsg)
234 {
235         struct disk *disk = (struct disk *)port;
236         cdevallmsg_t msg = (cdevallmsg_t)lmsg;
237         int error;
238
239         switch(msg->am_lmsg.ms_cmd.cm_op) {
240         case CDEV_CMD_OPEN:
241                 error = diskopen(
242                             msg->am_open.msg.dev,
243                             msg->am_open.oflags,
244                             msg->am_open.devtype,
245                             msg->am_open.td);
246                 break;
247         case CDEV_CMD_CLOSE:
248                 error = diskclose(
249                             msg->am_close.msg.dev,
250                             msg->am_close.fflag,
251                             msg->am_close.devtype,
252                             msg->am_close.td);
253                 break;
254         case CDEV_CMD_IOCTL:
255                 error = diskioctl(
256                             msg->am_ioctl.msg.dev,
257                             msg->am_ioctl.cmd,
258                             msg->am_ioctl.data,
259                             msg->am_ioctl.fflag,
260                             msg->am_ioctl.td);
261                 break;
262         case CDEV_CMD_STRATEGY:
263                 diskstrategy(msg->am_strategy.bp);
264                 error = 0;
265                 break;
266         case CDEV_CMD_PSIZE:
267                 msg->am_psize.result = diskpsize(msg->am_psize.msg.dev);
268                 error = 0;      /* XXX */
269                 break;
270         case CDEV_CMD_READ:
271                 error = physio(msg->am_read.msg.dev, 
272                                 msg->am_read.uio, msg->am_read.ioflag);
273                 break;
274         case CDEV_CMD_WRITE:
275                 error = physio(msg->am_write.msg.dev, 
276                                 msg->am_write.uio, msg->am_write.ioflag);
277                 break;
278         case CDEV_CMD_POLL:
279         case CDEV_CMD_KQFILTER:
280                 error = ENODEV;
281         case CDEV_CMD_MMAP:
282                 error = -1;
283                 break;
284         case CDEV_CMD_DUMP:
285                 error = disk_dumpcheck(msg->am_dump.msg.dev,
286                                 &msg->am_dump.count,
287                                 &msg->am_dump.blkno,
288                                 &msg->am_dump.secsize);
289                 if (error == 0) {
290                         msg->am_dump.msg.dev = disk->d_rawdev;
291                         error = lwkt_forwardmsg(disk->d_rawdev->si_port,
292                                                 &msg->am_dump.msg.msg);
293                         printf("error2 %d\n", error);
294                 }
295                 break;
296         default:
297                 error = ENOTSUP;
298                 break;
299         }
300         return(error);
301 }
302
303 /*
304  * When new device entries are instantiated, make sure they inherit our
305  * si_disk structure and block and iosize limits from the raw device.
306  *
307  * This routine is always called synchronously in the context of the 
308  * client.
309  *
310  * XXX The various io and block size constraints are not always initialized
311  * properly by devices.
312  */
313 static
314 int
315 diskclone(dev_t dev)
316 {
317         struct disk *dp;
318
319         dp = dev->si_devsw->d_data;
320         KKASSERT(dp != NULL);
321         dev->si_disk = dp;
322         dev->si_iosize_max = dp->d_rawdev->si_iosize_max;
323         dev->si_bsize_phys = dp->d_rawdev->si_bsize_phys;
324         dev->si_bsize_best = dp->d_rawdev->si_bsize_best;
325         return(0);
326 }
327
328 /*
329  * Open a disk device or partition.
330  */
331 static int
332 diskopen(dev_t dev, int oflags, int devtype, struct thread *td)
333 {
334         struct disk *dp;
335         int error;
336
337         /*
338          * dp can't be NULL here XXX.
339          */
340         error = 0;
341         dp = dev->si_disk;
342         if (dp == NULL)
343                 return (ENXIO);
344
345         /*
346          * Deal with open races
347          */
348         while (dp->d_flags & DISKFLAG_LOCK) {
349                 dp->d_flags |= DISKFLAG_WANTED;
350                 error = tsleep(dp, PCATCH, "diskopen", hz);
351                 if (error)
352                         return (error);
353         }
354         dp->d_flags |= DISKFLAG_LOCK;
355
356         /*
357          * Open the underlying raw device.
358          */
359         if (!dsisopen(dp->d_slice)) {
360 #if 0
361                 if (!pdev->si_iosize_max)
362                         pdev->si_iosize_max = dev->si_iosize_max;
363 #endif
364                 error = dev_dopen(dp->d_rawdev, oflags, devtype, td);
365         }
366
367         /*
368          * Inherit properties from the underlying device now that it is
369          * open.
370          */
371         diskclone(dev);
372
373         if (error)
374                 goto out;
375         
376         error = dsopen(dev, devtype, dp->d_dsflags, &dp->d_slice, &dp->d_label);
377
378         if (!dsisopen(dp->d_slice)) 
379                 dev_dclose(dp->d_rawdev, oflags, devtype, td);
380 out:    
381         dp->d_flags &= ~DISKFLAG_LOCK;
382         if (dp->d_flags & DISKFLAG_WANTED) {
383                 dp->d_flags &= ~DISKFLAG_WANTED;
384                 wakeup(dp);
385         }
386         
387         return(error);
388 }
389
390 /*
391  * Close a disk device or partition
392  */
393 static int
394 diskclose(dev_t dev, int fflag, int devtype, struct thread *td)
395 {
396         struct disk *dp;
397         int error;
398
399         error = 0;
400         dp = dev->si_disk;
401
402         dsclose(dev, devtype, dp->d_slice);
403         if (!dsisopen(dp->d_slice))
404                 error = dev_dclose(dp->d_rawdev, fflag, devtype, td);
405         return (error);
406 }
407
408 /*
409  * Execute strategy routine
410  */
411 static void
412 diskstrategy(struct buf *bp)
413 {
414         struct disk *dp;
415
416         dp = bp->b_dev->si_disk;
417
418         if (dp == NULL) {
419                 bp->b_error = ENXIO;
420                 bp->b_flags |= B_ERROR;
421                 biodone(bp);
422                 return;
423         }
424         KKASSERT(bp->b_dev->si_disk == dp);
425
426         if (dscheck(bp, dp->d_slice) <= 0) {
427                 biodone(bp);
428                 return;
429         }
430         bp->b_dev = dp->d_rawdev;
431         dev_dstrategy(dp->d_rawdev, bp);
432 }
433
434 /*
435  * First execute the ioctl on the disk device, and if it isn't supported 
436  * try running it on the backing device.
437  */
438 static int
439 diskioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
440 {
441         struct disk *dp;
442         int error;
443
444         dp = dev->si_disk;
445         if (dp == NULL)
446                 return (ENXIO);
447
448         error = dsioctl(dev, cmd, data, fflag, &dp->d_slice);
449         if (error == ENOIOCTL)
450                 error = dev_dioctl(dp->d_rawdev, cmd, data, fflag, td);
451         return (error);
452 }
453
454 /*
455  *
456  */
457 static int
458 diskpsize(dev_t dev)
459 {
460         struct disk *dp;
461
462         dp = dev->si_disk;
463         if (dp == NULL)
464                 return (-1);
465         return(dssize(dev, &dp->d_slice));
466 #if 0
467         if (dp != dev->si_disk) {
468                 dev->si_drv1 = pdev->si_drv1;
469                 dev->si_drv2 = pdev->si_drv2;
470                 /* XXX: don't set bp->b_dev->si_disk (?) */
471         }
472 #endif
473 }
474
475 SYSCTL_DECL(_debug_sizeof);
476
477 SYSCTL_INT(_debug_sizeof, OID_AUTO, disklabel, CTLFLAG_RD, 
478     0, sizeof(struct disklabel), "sizeof(struct disklabel)");
479
480 SYSCTL_INT(_debug_sizeof, OID_AUTO, diskslices, CTLFLAG_RD, 
481     0, sizeof(struct diskslices), "sizeof(struct diskslices)");
482
483 SYSCTL_INT(_debug_sizeof, OID_AUTO, disk, CTLFLAG_RD, 
484     0, sizeof(struct disk), "sizeof(struct disk)");
485
486
487 /*
488  * Seek sort for disks.
489  *
490  * The buf_queue keep two queues, sorted in ascending block order.  The first
491  * queue holds those requests which are positioned after the current block
492  * (in the first request); the second, which starts at queue->switch_point,
493  * holds requests which came in after their block number was passed.  Thus
494  * we implement a one way scan, retracting after reaching the end of the drive
495  * to the first request on the second queue, at which time it becomes the
496  * first queue.
497  *
498  * A one-way scan is natural because of the way UNIX read-ahead blocks are
499  * allocated.
500  */
501 void
502 bufqdisksort(bufq, bp)
503         struct buf_queue_head *bufq;
504         struct buf *bp;
505 {
506         struct buf *bq;
507         struct buf *bn;
508         struct buf *be;
509         
510         be = TAILQ_LAST(&bufq->queue, buf_queue);
511         /*
512          * If the queue is empty or we are an
513          * ordered transaction, then it's easy.
514          */
515         if ((bq = bufq_first(bufq)) == NULL
516          || (bp->b_flags & B_ORDERED) != 0) {
517                 bufq_insert_tail(bufq, bp);
518                 return;
519         } else if (bufq->insert_point != NULL) {
520
521                 /*
522                  * A certain portion of the list is
523                  * "locked" to preserve ordering, so
524                  * we can only insert after the insert
525                  * point.
526                  */
527                 bq = bufq->insert_point;
528         } else {
529
530                 /*
531                  * If we lie before the last removed (currently active)
532                  * request, and are not inserting ourselves into the
533                  * "locked" portion of the list, then we must add ourselves
534                  * to the second request list.
535                  */
536                 if (bp->b_pblkno < bufq->last_pblkno) {
537
538                         bq = bufq->switch_point;
539                         /*
540                          * If we are starting a new secondary list,
541                          * then it's easy.
542                          */
543                         if (bq == NULL) {
544                                 bufq->switch_point = bp;
545                                 bufq_insert_tail(bufq, bp);
546                                 return;
547                         }
548                         /*
549                          * If we lie ahead of the current switch point,
550                          * insert us before the switch point and move
551                          * the switch point.
552                          */
553                         if (bp->b_pblkno < bq->b_pblkno) {
554                                 bufq->switch_point = bp;
555                                 TAILQ_INSERT_BEFORE(bq, bp, b_act);
556                                 return;
557                         }
558                 } else {
559                         if (bufq->switch_point != NULL)
560                                 be = TAILQ_PREV(bufq->switch_point,
561                                                 buf_queue, b_act);
562                         /*
563                          * If we lie between last_pblkno and bq,
564                          * insert before bq.
565                          */
566                         if (bp->b_pblkno < bq->b_pblkno) {
567                                 TAILQ_INSERT_BEFORE(bq, bp, b_act);
568                                 return;
569                         }
570                 }
571         }
572
573         /*
574          * Request is at/after our current position in the list.
575          * Optimize for sequential I/O by seeing if we go at the tail.
576          */
577         if (bp->b_pblkno > be->b_pblkno) {
578                 TAILQ_INSERT_AFTER(&bufq->queue, be, bp, b_act);
579                 return;
580         }
581
582         /* Otherwise, insertion sort */
583         while ((bn = TAILQ_NEXT(bq, b_act)) != NULL) {
584                 
585                 /*
586                  * We want to go after the current request if it is the end
587                  * of the first request list, or if the next request is a
588                  * larger cylinder than our request.
589                  */
590                 if (bn == bufq->switch_point
591                  || bp->b_pblkno < bn->b_pblkno)
592                         break;
593                 bq = bn;
594         }
595         TAILQ_INSERT_AFTER(&bufq->queue, bq, bp, b_act);
596 }
597
598
599 /*
600  * Attempt to read a disk label from a device using the indicated strategy
601  * routine.  The label must be partly set up before this: secpercyl, secsize
602  * and anything required in the strategy routine (e.g., dummy bounds for the
603  * partition containing the label) must be filled in before calling us.
604  * Returns NULL on success and an error string on failure.
605  */
606 char *
607 readdisklabel(dev_t dev, struct disklabel *lp)
608 {
609         struct buf *bp;
610         struct disklabel *dlp;
611         char *msg = NULL;
612
613         bp = geteblk((int)lp->d_secsize);
614         bp->b_dev = dev;
615         bp->b_blkno = LABELSECTOR * ((int)lp->d_secsize/DEV_BSIZE);
616         bp->b_bcount = lp->d_secsize;
617         bp->b_flags &= ~B_INVAL;
618         bp->b_flags |= B_READ;
619         BUF_STRATEGY(bp, 1);
620         if (biowait(bp))
621                 msg = "I/O error";
622         else for (dlp = (struct disklabel *)bp->b_data;
623             dlp <= (struct disklabel *)((char *)bp->b_data +
624             lp->d_secsize - sizeof(*dlp));
625             dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
626                 if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) {
627                         if (msg == NULL)
628                                 msg = "no disk label";
629                 } else if (dlp->d_npartitions > MAXPARTITIONS ||
630                            dkcksum(dlp) != 0)
631                         msg = "disk label corrupted";
632                 else {
633                         *lp = *dlp;
634                         msg = NULL;
635                         break;
636                 }
637         }
638         bp->b_flags |= B_INVAL | B_AGE;
639         brelse(bp);
640         return (msg);
641 }
642
643 /*
644  * Check new disk label for sensibility before setting it.
645  */
646 int
647 setdisklabel(olp, nlp, openmask)
648         struct disklabel *olp, *nlp;
649         u_long openmask;
650 {
651         int i;
652         struct partition *opp, *npp;
653
654         /*
655          * Check it is actually a disklabel we are looking at.
656          */
657         if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC ||
658             dkcksum(nlp) != 0)
659                 return (EINVAL);
660         /*
661          * For each partition that we think is open,
662          */
663         while ((i = ffs((long)openmask)) != 0) {
664                 i--;
665                 /*
666                  * Check it is not changing....
667                  */
668                 openmask &= ~(1 << i);
669                 if (nlp->d_npartitions <= i)
670                         return (EBUSY);
671                 opp = &olp->d_partitions[i];
672                 npp = &nlp->d_partitions[i];
673                 if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
674                         return (EBUSY);
675                 /*
676                  * Copy internally-set partition information
677                  * if new label doesn't include it.             XXX
678                  * (If we are using it then we had better stay the same type)
679                  * This is possibly dubious, as someone else noted (XXX)
680                  */
681                 if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) {
682                         npp->p_fstype = opp->p_fstype;
683                         npp->p_fsize = opp->p_fsize;
684                         npp->p_frag = opp->p_frag;
685                         npp->p_cpg = opp->p_cpg;
686                 }
687         }
688         nlp->d_checksum = 0;
689         nlp->d_checksum = dkcksum(nlp);
690         *olp = *nlp;
691         return (0);
692 }
693
694 /*
695  * Write disk label back to device after modification.
696  */
697 int
698 writedisklabel(dev_t dev, struct disklabel *lp)
699 {
700         struct buf *bp;
701         struct disklabel *dlp;
702         int error = 0;
703
704         if (lp->d_partitions[RAW_PART].p_offset != 0)
705                 return (EXDEV);                 /* not quite right */
706         bp = geteblk((int)lp->d_secsize);
707         bp->b_dev = dkmodpart(dev, RAW_PART);
708         bp->b_blkno = LABELSECTOR * ((int)lp->d_secsize/DEV_BSIZE);
709         bp->b_bcount = lp->d_secsize;
710 #if 1
711         /*
712          * We read the label first to see if it's there,
713          * in which case we will put ours at the same offset into the block..
714          * (I think this is stupid [Julian])
715          * Note that you can't write a label out over a corrupted label!
716          * (also stupid.. how do you write the first one? by raw writes?)
717          */
718         bp->b_flags &= ~B_INVAL;
719         bp->b_flags |= B_READ;
720         BUF_STRATEGY(bp, 1);
721         error = biowait(bp);
722         if (error)
723                 goto done;
724         for (dlp = (struct disklabel *)bp->b_data;
725             dlp <= (struct disklabel *)
726               ((char *)bp->b_data + lp->d_secsize - sizeof(*dlp));
727             dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
728                 if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC &&
729                     dkcksum(dlp) == 0) {
730                         *dlp = *lp;
731                         bp->b_flags &= ~(B_DONE | B_READ);
732                         bp->b_flags |= B_WRITE;
733                         bp->b_dev = dkmodpart(dev, RAW_PART);
734 #ifdef __alpha__
735                         alpha_fix_srm_checksum(bp);
736 #endif
737                         BUF_STRATEGY(bp, 1);
738                         error = biowait(bp);
739                         goto done;
740                 }
741         }
742         error = ESRCH;
743 done:
744 #else
745         bzero(bp->b_data, lp->d_secsize);
746         dlp = (struct disklabel *)bp->b_data;
747         *dlp = *lp;
748         bp->b_flags &= ~B_INVAL;
749         bp->b_flags |= B_WRITE;
750         BUF_STRATEGY(bp, 1);
751         error = biowait(bp);
752 #endif
753         bp->b_flags |= B_INVAL | B_AGE;
754         brelse(bp);
755         return (error);
756 }
757
758 /*
759  * Disk error is the preface to plaintive error messages
760  * about failing disk transfers.  It prints messages of the form
761
762 hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d)
763
764  * if the offset of the error in the transfer and a disk label
765  * are both available.  blkdone should be -1 if the position of the error
766  * is unknown; the disklabel pointer may be null from drivers that have not
767  * been converted to use them.  The message is printed with printf
768  * if pri is LOG_PRINTF, otherwise it uses log at the specified priority.
769  * The message should be completed (with at least a newline) with printf
770  * or addlog, respectively.  There is no trailing space.
771  */
772 void
773 diskerr(bp, what, pri, blkdone, lp)
774         struct buf *bp;
775         char *what;
776         int pri, blkdone;
777         struct disklabel *lp;
778 {
779         int unit = dkunit(bp->b_dev);
780         int slice = dkslice(bp->b_dev);
781         int part = dkpart(bp->b_dev);
782         char partname[2];
783         char *sname;
784         daddr_t sn;
785
786         sname = dsname(bp->b_dev, unit, slice, part, partname);
787         printf("%s%s: %s %sing fsbn ", sname, partname, what,
788               bp->b_flags & B_READ ? "read" : "writ");
789         sn = bp->b_blkno;
790         if (bp->b_bcount <= DEV_BSIZE)
791                 printf("%ld", (long)sn);
792         else {
793                 if (blkdone >= 0) {
794                         sn += blkdone;
795                         printf("%ld of ", (long)sn);
796                 }
797                 printf("%ld-%ld", (long)bp->b_blkno,
798                     (long)(bp->b_blkno + (bp->b_bcount - 1) / DEV_BSIZE));
799         }
800         if (lp && (blkdone >= 0 || bp->b_bcount <= lp->d_secsize)) {
801 #ifdef tahoe
802                 sn *= DEV_BSIZE / lp->d_secsize;                /* XXX */
803 #endif
804                 sn += lp->d_partitions[part].p_offset;
805                 /*
806                  * XXX should add slice offset and not print the slice,
807                  * but we don't know the slice pointer.
808                  * XXX should print bp->b_pblkno so that this will work
809                  * independent of slices, labels and bad sector remapping,
810                  * but some drivers don't set bp->b_pblkno.
811                  */
812                 printf(" (%s bn %ld; cn %ld", sname, (long)sn,
813                     (long)(sn / lp->d_secpercyl));
814                 sn %= (long)lp->d_secpercyl;
815                 printf(" tn %ld sn %ld)", (long)(sn / lp->d_nsectors),
816                     (long)(sn % lp->d_nsectors));
817         }
818 }