ld.c revision 1.48 1 /* $NetBSD: ld.c,v 1.48 2007/07/21 19:51:47 ad Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran and Charles M. Hannum.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Disk driver for use by RAID controllers.
41 */
42
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: ld.c,v 1.48 2007/07/21 19:51:47 ad Exp $");
45
46 #include "rnd.h"
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/device.h>
52 #include <sys/queue.h>
53 #include <sys/proc.h>
54 #include <sys/buf.h>
55 #include <sys/bufq.h>
56 #include <sys/endian.h>
57 #include <sys/disklabel.h>
58 #include <sys/disk.h>
59 #include <sys/dkio.h>
60 #include <sys/stat.h>
61 #include <sys/lock.h>
62 #include <sys/conf.h>
63 #include <sys/fcntl.h>
64 #include <sys/vnode.h>
65 #include <sys/syslog.h>
66 #include <sys/mutex.h>
67 #if NRND > 0
68 #include <sys/rnd.h>
69 #endif
70
71 #include <dev/ldvar.h>
72
73 #include <prop/proplib.h>
74
75 static void ldgetdefaultlabel(struct ld_softc *, struct disklabel *);
76 static void ldgetdisklabel(struct ld_softc *);
77 static void ldminphys(struct buf *bp);
78 static void ldshutdown(void *);
79 static void ldstart(struct ld_softc *, struct buf *);
80 static void ld_set_properties(struct ld_softc *);
81 static void ld_config_interrupts (struct device *);
82
83 extern struct cfdriver ld_cd;
84
85 static dev_type_open(ldopen);
86 static dev_type_close(ldclose);
87 static dev_type_read(ldread);
88 static dev_type_write(ldwrite);
89 static dev_type_ioctl(ldioctl);
90 static dev_type_strategy(ldstrategy);
91 static dev_type_dump(lddump);
92 static dev_type_size(ldsize);
93
94 const struct bdevsw ld_bdevsw = {
95 ldopen, ldclose, ldstrategy, ldioctl, lddump, ldsize, D_DISK
96 };
97
98 const struct cdevsw ld_cdevsw = {
99 ldopen, ldclose, ldread, ldwrite, ldioctl,
100 nostop, notty, nopoll, nommap, nokqfilter, D_DISK
101 };
102
103 static struct dkdriver lddkdriver = { ldstrategy, ldminphys };
104 static void *ld_sdh;
105
106 void
107 ldattach(struct ld_softc *sc)
108 {
109 char tbuf[9];
110
111 mutex_init(&sc->sc_mutex, MUTEX_DRIVER, IPL_BIO);
112
113 if ((sc->sc_flags & LDF_ENABLED) == 0) {
114 aprint_normal("%s: disabled\n", sc->sc_dv.dv_xname);
115 return;
116 }
117
118 /* Initialise and attach the disk structure. */
119 sc->sc_dk.dk_driver = &lddkdriver;
120 sc->sc_dk.dk_name = sc->sc_dv.dv_xname;
121 disk_attach(&sc->sc_dk);
122
123 if (sc->sc_maxxfer > MAXPHYS)
124 sc->sc_maxxfer = MAXPHYS;
125
126 /* Build synthetic geometry if necessary. */
127 if (sc->sc_nheads == 0 || sc->sc_nsectors == 0 ||
128 sc->sc_ncylinders == 0) {
129 uint64_t ncyl;
130
131 if (sc->sc_secperunit <= 528 * 2048) /* 528MB */
132 sc->sc_nheads = 16;
133 else if (sc->sc_secperunit <= 1024 * 2048) /* 1GB */
134 sc->sc_nheads = 32;
135 else if (sc->sc_secperunit <= 21504 * 2048) /* 21GB */
136 sc->sc_nheads = 64;
137 else if (sc->sc_secperunit <= 43008 * 2048) /* 42GB */
138 sc->sc_nheads = 128;
139 else
140 sc->sc_nheads = 255;
141
142 sc->sc_nsectors = 63;
143 sc->sc_ncylinders = INT_MAX;
144 ncyl = sc->sc_secperunit /
145 (sc->sc_nheads * sc->sc_nsectors);
146 if (ncyl < INT_MAX)
147 sc->sc_ncylinders = (int)ncyl;
148 }
149
150 format_bytes(tbuf, sizeof(tbuf), sc->sc_secperunit *
151 sc->sc_secsize);
152 aprint_normal("%s: %s, %d cyl, %d head, %d sec, %d bytes/sect x %"PRIu64" sectors\n",
153 sc->sc_dv.dv_xname, tbuf, sc->sc_ncylinders, sc->sc_nheads,
154 sc->sc_nsectors, sc->sc_secsize, sc->sc_secperunit);
155
156 ld_set_properties(sc);
157
158 #if NRND > 0
159 /* Attach the device into the rnd source list. */
160 rnd_attach_source(&sc->sc_rnd_source, sc->sc_dv.dv_xname,
161 RND_TYPE_DISK, 0);
162 #endif
163
164 /* Set the `shutdownhook'. */
165 if (ld_sdh == NULL)
166 ld_sdh = shutdownhook_establish(ldshutdown, NULL);
167 bufq_alloc(&sc->sc_bufq, BUFQ_DISK_DEFAULT_STRAT, BUFQ_SORT_RAWBLOCK);
168
169 /* Discover wedges on this disk. */
170 config_interrupts(&sc->sc_dv, ld_config_interrupts);
171 }
172
173 int
174 ldadjqparam(struct ld_softc *sc, int xmax)
175 {
176 int s;
177
178 s = splbio();
179 sc->sc_maxqueuecnt = xmax;
180 splx(s);
181
182 return (0);
183 }
184
185 int
186 ldbegindetach(struct ld_softc *sc, int flags)
187 {
188 int s, rv = 0;
189
190 if ((sc->sc_flags & LDF_ENABLED) == 0)
191 return (0);
192
193 if ((flags & DETACH_FORCE) == 0 && sc->sc_dk.dk_openmask != 0)
194 return (EBUSY);
195
196 s = splbio();
197 sc->sc_maxqueuecnt = 0;
198 sc->sc_flags |= LDF_DETACH;
199 while (sc->sc_queuecnt > 0) {
200 sc->sc_flags |= LDF_DRAIN;
201 rv = tsleep(&sc->sc_queuecnt, PRIBIO, "lddrn", 0);
202 if (rv)
203 break;
204 }
205 splx(s);
206
207 return (rv);
208 }
209
210 void
211 ldenddetach(struct ld_softc *sc)
212 {
213 int s, bmaj, cmaj, i, mn;
214
215 if ((sc->sc_flags & LDF_ENABLED) == 0)
216 return;
217
218 /* Wait for commands queued with the hardware to complete. */
219 if (sc->sc_queuecnt != 0)
220 if (tsleep(&sc->sc_queuecnt, PRIBIO, "lddtch", 30 * hz))
221 printf("%s: not drained\n", sc->sc_dv.dv_xname);
222
223 /* Locate the major numbers. */
224 bmaj = bdevsw_lookup_major(&ld_bdevsw);
225 cmaj = cdevsw_lookup_major(&ld_cdevsw);
226
227 /* Kill off any queued buffers. */
228 s = splbio();
229 bufq_drain(sc->sc_bufq);
230 splx(s);
231
232 bufq_free(sc->sc_bufq);
233
234 /* Nuke the vnodes for any open instances. */
235 for (i = 0; i < MAXPARTITIONS; i++) {
236 mn = DISKMINOR(device_unit(&sc->sc_dv), i);
237 vdevgone(bmaj, mn, mn, VBLK);
238 vdevgone(cmaj, mn, mn, VCHR);
239 }
240
241 /* Delete all of our wedges. */
242 dkwedge_delall(&sc->sc_dk);
243
244 /* Detach from the disk list. */
245 disk_detach(&sc->sc_dk);
246
247 #if NRND > 0
248 /* Unhook the entropy source. */
249 rnd_detach_source(&sc->sc_rnd_source);
250 #endif
251
252 /*
253 * XXX We can't really flush the cache here, beceause the
254 * XXX device may already be non-existent from the controller's
255 * XXX perspective.
256 */
257 #if 0
258 /* Flush the device's cache. */
259 if (sc->sc_flush != NULL)
260 if ((*sc->sc_flush)(sc) != 0)
261 printf("%s: unable to flush cache\n",
262 sc->sc_dv.dv_xname);
263 #endif
264 }
265
266 /* ARGSUSED */
267 static void
268 ldshutdown(void *cookie)
269 {
270 struct ld_softc *sc;
271 int i;
272
273 for (i = 0; i < ld_cd.cd_ndevs; i++) {
274 if ((sc = device_lookup(&ld_cd, i)) == NULL)
275 continue;
276 if (sc->sc_flush != NULL && (*sc->sc_flush)(sc) != 0)
277 printf("%s: unable to flush cache\n",
278 sc->sc_dv.dv_xname);
279 }
280 }
281
282 /* ARGSUSED */
283 static int
284 ldopen(dev_t dev, int flags, int fmt, struct lwp *l)
285 {
286 struct ld_softc *sc;
287 int error, unit, part;
288
289 unit = DISKUNIT(dev);
290 if ((sc = device_lookup(&ld_cd, unit)) == NULL)
291 return (ENXIO);
292 if ((sc->sc_flags & LDF_ENABLED) == 0)
293 return (ENODEV);
294 part = DISKPART(dev);
295
296 mutex_enter(&sc->sc_dk.dk_openlock);
297
298 if (sc->sc_dk.dk_openmask == 0) {
299 /* Load the partition info if not already loaded. */
300 if ((sc->sc_flags & LDF_VLABEL) == 0)
301 ldgetdisklabel(sc);
302 }
303
304 /* Check that the partition exists. */
305 if (part != RAW_PART && (part >= sc->sc_dk.dk_label->d_npartitions ||
306 sc->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
307 error = ENXIO;
308 goto bad1;
309 }
310
311 /* Ensure only one open at a time. */
312 switch (fmt) {
313 case S_IFCHR:
314 sc->sc_dk.dk_copenmask |= (1 << part);
315 break;
316 case S_IFBLK:
317 sc->sc_dk.dk_bopenmask |= (1 << part);
318 break;
319 }
320 sc->sc_dk.dk_openmask =
321 sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
322
323 error = 0;
324 bad1:
325 mutex_exit(&sc->sc_dk.dk_openlock);
326 return (error);
327 }
328
329 /* ARGSUSED */
330 static int
331 ldclose(dev_t dev, int flags, int fmt, struct lwp *l)
332 {
333 struct ld_softc *sc;
334 int part, unit;
335
336 unit = DISKUNIT(dev);
337 part = DISKPART(dev);
338 sc = device_lookup(&ld_cd, unit);
339
340 mutex_enter(&sc->sc_dk.dk_openlock);
341
342 switch (fmt) {
343 case S_IFCHR:
344 sc->sc_dk.dk_copenmask &= ~(1 << part);
345 break;
346 case S_IFBLK:
347 sc->sc_dk.dk_bopenmask &= ~(1 << part);
348 break;
349 }
350 sc->sc_dk.dk_openmask =
351 sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
352
353 if (sc->sc_dk.dk_openmask == 0) {
354 if (sc->sc_flush != NULL && (*sc->sc_flush)(sc) != 0)
355 printf("%s: unable to flush cache\n",
356 sc->sc_dv.dv_xname);
357 if ((sc->sc_flags & LDF_KLABEL) == 0)
358 sc->sc_flags &= ~LDF_VLABEL;
359 }
360
361 mutex_exit(&sc->sc_dk.dk_openlock);
362 return (0);
363 }
364
365 /* ARGSUSED */
366 static int
367 ldread(dev_t dev, struct uio *uio, int ioflag)
368 {
369
370 return (physio(ldstrategy, NULL, dev, B_READ, ldminphys, uio));
371 }
372
373 /* ARGSUSED */
374 static int
375 ldwrite(dev_t dev, struct uio *uio, int ioflag)
376 {
377
378 return (physio(ldstrategy, NULL, dev, B_WRITE, ldminphys, uio));
379 }
380
381 /* ARGSUSED */
382 static int
383 ldioctl(dev_t dev, u_long cmd, void *addr, int32_t flag, struct lwp *l)
384 {
385 struct ld_softc *sc;
386 int part, unit, error;
387 #ifdef __HAVE_OLD_DISKLABEL
388 struct disklabel newlabel;
389 #endif
390 struct disklabel *lp;
391
392 unit = DISKUNIT(dev);
393 part = DISKPART(dev);
394 sc = device_lookup(&ld_cd, unit);
395
396 error = disk_ioctl(&sc->sc_dk, cmd, addr, flag, l);
397 if (error != EPASSTHROUGH)
398 return (error);
399
400 error = 0;
401 switch (cmd) {
402 case DIOCGDINFO:
403 memcpy(addr, sc->sc_dk.dk_label, sizeof(struct disklabel));
404 return (0);
405
406 #ifdef __HAVE_OLD_DISKLABEL
407 case ODIOCGDINFO:
408 newlabel = *(sc->sc_dk.dk_label);
409 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
410 return ENOTTY;
411 memcpy(addr, &newlabel, sizeof(struct olddisklabel));
412 return (0);
413 #endif
414
415 case DIOCGPART:
416 ((struct partinfo *)addr)->disklab = sc->sc_dk.dk_label;
417 ((struct partinfo *)addr)->part =
418 &sc->sc_dk.dk_label->d_partitions[part];
419 break;
420
421 case DIOCWDINFO:
422 case DIOCSDINFO:
423 #ifdef __HAVE_OLD_DISKLABEL
424 case ODIOCWDINFO:
425 case ODIOCSDINFO:
426
427 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
428 memset(&newlabel, 0, sizeof newlabel);
429 memcpy(&newlabel, addr, sizeof (struct olddisklabel));
430 lp = &newlabel;
431 } else
432 #endif
433 lp = (struct disklabel *)addr;
434
435 if ((flag & FWRITE) == 0)
436 return (EBADF);
437
438 mutex_enter(&sc->sc_dk.dk_openlock);
439 sc->sc_flags |= LDF_LABELLING;
440
441 error = setdisklabel(sc->sc_dk.dk_label,
442 lp, /*sc->sc_dk.dk_openmask : */0,
443 sc->sc_dk.dk_cpulabel);
444 if (error == 0 && (cmd == DIOCWDINFO
445 #ifdef __HAVE_OLD_DISKLABEL
446 || cmd == ODIOCWDINFO
447 #endif
448 ))
449 error = writedisklabel(
450 MAKEDISKDEV(major(dev), DISKUNIT(dev), RAW_PART),
451 ldstrategy, sc->sc_dk.dk_label,
452 sc->sc_dk.dk_cpulabel);
453
454 sc->sc_flags &= ~LDF_LABELLING;
455 mutex_exit(&sc->sc_dk.dk_openlock);
456 break;
457
458 case DIOCKLABEL:
459 if ((flag & FWRITE) == 0)
460 return (EBADF);
461 if (*(int *)addr)
462 sc->sc_flags |= LDF_KLABEL;
463 else
464 sc->sc_flags &= ~LDF_KLABEL;
465 break;
466
467 case DIOCWLABEL:
468 if ((flag & FWRITE) == 0)
469 return (EBADF);
470 if (*(int *)addr)
471 sc->sc_flags |= LDF_WLABEL;
472 else
473 sc->sc_flags &= ~LDF_WLABEL;
474 break;
475
476 case DIOCGDEFLABEL:
477 ldgetdefaultlabel(sc, (struct disklabel *)addr);
478 break;
479
480 #ifdef __HAVE_OLD_DISKLABEL
481 case ODIOCGDEFLABEL:
482 ldgetdefaultlabel(sc, &newlabel);
483 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
484 return ENOTTY;
485 memcpy(addr, &newlabel, sizeof (struct olddisklabel));
486 break;
487 #endif
488
489 case DIOCCACHESYNC:
490 /*
491 * XXX Do we really need to care about having a writable
492 * file descriptor here?
493 */
494 if ((flag & FWRITE) == 0)
495 error = EBADF;
496 else if (sc->sc_flush)
497 error = (*sc->sc_flush)(sc);
498 else
499 error = 0; /* XXX Error out instead? */
500 break;
501
502 case DIOCAWEDGE:
503 {
504 struct dkwedge_info *dkw = (void *) addr;
505
506 if ((flag & FWRITE) == 0)
507 return (EBADF);
508
509 /* If the ioctl happens here, the parent is us. */
510 strcpy(dkw->dkw_parent, sc->sc_dv.dv_xname);
511 return (dkwedge_add(dkw));
512 }
513
514 case DIOCDWEDGE:
515 {
516 struct dkwedge_info *dkw = (void *) addr;
517
518 if ((flag & FWRITE) == 0)
519 return (EBADF);
520
521 /* If the ioctl happens here, the parent is us. */
522 strcpy(dkw->dkw_parent, sc->sc_dv.dv_xname);
523 return (dkwedge_del(dkw));
524 }
525
526 case DIOCLWEDGES:
527 {
528 struct dkwedge_list *dkwl = (void *) addr;
529
530 return (dkwedge_list(&sc->sc_dk, dkwl, l));
531 }
532
533 default:
534 error = ENOTTY;
535 break;
536 }
537
538 return (error);
539 }
540
541 static void
542 ldstrategy(struct buf *bp)
543 {
544 struct ld_softc *sc;
545 struct disklabel *lp;
546 daddr_t blkno;
547 int s, part;
548
549 sc = device_lookup(&ld_cd, DISKUNIT(bp->b_dev));
550 part = DISKPART(bp->b_dev);
551
552 if ((sc->sc_flags & LDF_DETACH) != 0) {
553 bp->b_error = EIO;
554 goto bad;
555 }
556
557 lp = sc->sc_dk.dk_label;
558
559 /*
560 * The transfer must be a whole number of blocks and the offset must
561 * not be negative.
562 */
563 if ((bp->b_bcount % lp->d_secsize) != 0 || bp->b_blkno < 0) {
564 bp->b_error = EINVAL;
565 goto bad;
566 }
567
568 /* If it's a null transfer, return immediately. */
569 if (bp->b_bcount == 0)
570 goto done;
571
572 /*
573 * Do bounds checking and adjust the transfer. If error, process.
574 * If past the end of partition, just return.
575 */
576 if (part != RAW_PART &&
577 bounds_check_with_label(&sc->sc_dk, bp,
578 (sc->sc_flags & (LDF_WLABEL | LDF_LABELLING)) != 0) <= 0) {
579 goto done;
580 }
581
582 /*
583 * Convert the block number to absolute and put it in terms
584 * of the device's logical block size.
585 */
586 if (lp->d_secsize == DEV_BSIZE)
587 blkno = bp->b_blkno;
588 else if (lp->d_secsize > DEV_BSIZE)
589 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
590 else
591 blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
592
593 if (part != RAW_PART)
594 blkno += lp->d_partitions[part].p_offset;
595
596 bp->b_rawblkno = blkno;
597
598 s = splbio();
599 ldstart(sc, bp);
600 splx(s);
601 return;
602
603 bad:
604 bp->b_flags |= B_ERROR;
605 done:
606 bp->b_resid = bp->b_bcount;
607 biodone(bp);
608 }
609
610 static void
611 ldstart(struct ld_softc *sc, struct buf *bp)
612 {
613 int error;
614
615 mutex_enter(&sc->sc_mutex);
616
617 if (bp != NULL)
618 BUFQ_PUT(sc->sc_bufq, bp);
619
620 while (sc->sc_queuecnt < sc->sc_maxqueuecnt) {
621 /* See if there is work to do. */
622 if ((bp = BUFQ_PEEK(sc->sc_bufq)) == NULL)
623 break;
624
625 disk_busy(&sc->sc_dk);
626 sc->sc_queuecnt++;
627
628 if (__predict_true((error = (*sc->sc_start)(sc, bp)) == 0)) {
629 /*
630 * The back-end is running the job; remove it from
631 * the queue.
632 */
633 (void) BUFQ_GET(sc->sc_bufq);
634 } else {
635 disk_unbusy(&sc->sc_dk, 0, (bp->b_flags & B_READ));
636 sc->sc_queuecnt--;
637 if (error == EAGAIN) {
638 /*
639 * Temporary resource shortage in the
640 * back-end; just defer the job until
641 * later.
642 *
643 * XXX We might consider a watchdog timer
644 * XXX to make sure we are kicked into action.
645 */
646 break;
647 } else {
648 (void) BUFQ_GET(sc->sc_bufq);
649 bp->b_error = error;
650 bp->b_flags |= B_ERROR;
651 bp->b_resid = bp->b_bcount;
652 mutex_exit(&sc->sc_mutex);
653 biodone(bp);
654 mutex_enter(&sc->sc_mutex);
655 }
656 }
657 }
658
659 mutex_exit(&sc->sc_mutex);
660 }
661
662 void
663 lddone(struct ld_softc *sc, struct buf *bp)
664 {
665
666 if ((bp->b_flags & B_ERROR) != 0) {
667 diskerr(bp, "ld", "error", LOG_PRINTF, 0, sc->sc_dk.dk_label);
668 printf("\n");
669 }
670
671 disk_unbusy(&sc->sc_dk, bp->b_bcount - bp->b_resid,
672 (bp->b_flags & B_READ));
673 #if NRND > 0
674 rnd_add_uint32(&sc->sc_rnd_source, bp->b_rawblkno);
675 #endif
676 biodone(bp);
677
678 mutex_enter(&sc->sc_mutex);
679 if (--sc->sc_queuecnt <= sc->sc_maxqueuecnt) {
680 if ((sc->sc_flags & LDF_DRAIN) != 0) {
681 sc->sc_flags &= ~LDF_DRAIN;
682 wakeup(&sc->sc_queuecnt);
683 }
684 mutex_exit(&sc->sc_mutex);
685 ldstart(sc, NULL);
686 } else
687 mutex_exit(&sc->sc_mutex);
688 }
689
690 static int
691 ldsize(dev_t dev)
692 {
693 struct ld_softc *sc;
694 int part, unit, omask, size;
695
696 unit = DISKUNIT(dev);
697 if ((sc = device_lookup(&ld_cd, unit)) == NULL)
698 return (ENODEV);
699 if ((sc->sc_flags & LDF_ENABLED) == 0)
700 return (ENODEV);
701 part = DISKPART(dev);
702
703 omask = sc->sc_dk.dk_openmask & (1 << part);
704
705 if (omask == 0 && ldopen(dev, 0, S_IFBLK, NULL) != 0)
706 return (-1);
707 else if (sc->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
708 size = -1;
709 else
710 size = sc->sc_dk.dk_label->d_partitions[part].p_size *
711 (sc->sc_dk.dk_label->d_secsize / DEV_BSIZE);
712 if (omask == 0 && ldclose(dev, 0, S_IFBLK, NULL) != 0)
713 return (-1);
714
715 return (size);
716 }
717
718 /*
719 * Load the label information from the specified device.
720 */
721 static void
722 ldgetdisklabel(struct ld_softc *sc)
723 {
724 const char *errstring;
725
726 ldgetdefaultlabel(sc, sc->sc_dk.dk_label);
727
728 /* Call the generic disklabel extraction routine. */
729 errstring = readdisklabel(MAKEDISKDEV(0, device_unit(&sc->sc_dv),
730 RAW_PART), ldstrategy, sc->sc_dk.dk_label, sc->sc_dk.dk_cpulabel);
731 if (errstring != NULL)
732 printf("%s: %s\n", sc->sc_dv.dv_xname, errstring);
733
734 /* In-core label now valid. */
735 sc->sc_flags |= LDF_VLABEL;
736 }
737
738 /*
739 * Construct a ficticious label.
740 */
741 static void
742 ldgetdefaultlabel(struct ld_softc *sc, struct disklabel *lp)
743 {
744
745 memset(lp, 0, sizeof(struct disklabel));
746
747 lp->d_secsize = sc->sc_secsize;
748 lp->d_ntracks = sc->sc_nheads;
749 lp->d_nsectors = sc->sc_nsectors;
750 lp->d_ncylinders = sc->sc_ncylinders;
751 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
752 lp->d_type = DTYPE_LD;
753 strlcpy(lp->d_typename, "unknown", sizeof(lp->d_typename));
754 strlcpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
755 lp->d_secperunit = sc->sc_secperunit;
756 lp->d_rpm = 7200;
757 lp->d_interleave = 1;
758 lp->d_flags = 0;
759
760 lp->d_partitions[RAW_PART].p_offset = 0;
761 lp->d_partitions[RAW_PART].p_size =
762 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
763 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
764 lp->d_npartitions = RAW_PART + 1;
765
766 lp->d_magic = DISKMAGIC;
767 lp->d_magic2 = DISKMAGIC;
768 lp->d_checksum = dkcksum(lp);
769 }
770
771 /*
772 * Take a dump.
773 */
774 static int
775 lddump(dev_t dev, daddr_t blkno, void *vav, size_t size)
776 {
777 char *va = vav;
778 struct ld_softc *sc;
779 struct disklabel *lp;
780 int unit, part, nsects, sectoff, towrt, nblk, maxblkcnt, rv;
781 static int dumping;
782
783 unit = DISKUNIT(dev);
784 if ((sc = device_lookup(&ld_cd, unit)) == NULL)
785 return (ENXIO);
786 if ((sc->sc_flags & LDF_ENABLED) == 0)
787 return (ENODEV);
788 if (sc->sc_dump == NULL)
789 return (ENXIO);
790
791 /* Check if recursive dump; if so, punt. */
792 if (dumping)
793 return (EFAULT);
794 dumping = 1;
795
796 /* Convert to disk sectors. Request must be a multiple of size. */
797 part = DISKPART(dev);
798 lp = sc->sc_dk.dk_label;
799 if ((size % lp->d_secsize) != 0)
800 return (EFAULT);
801 towrt = size / lp->d_secsize;
802 blkno = dbtob(blkno) / lp->d_secsize; /* blkno in DEV_BSIZE units */
803
804 nsects = lp->d_partitions[part].p_size;
805 sectoff = lp->d_partitions[part].p_offset;
806
807 /* Check transfer bounds against partition size. */
808 if ((blkno < 0) || ((blkno + towrt) > nsects))
809 return (EINVAL);
810
811 /* Offset block number to start of partition. */
812 blkno += sectoff;
813
814 /* Start dumping and return when done. */
815 maxblkcnt = sc->sc_maxxfer / sc->sc_secsize - 1;
816 while (towrt > 0) {
817 nblk = min(maxblkcnt, towrt);
818
819 if ((rv = (*sc->sc_dump)(sc, va, blkno, nblk)) != 0)
820 return (rv);
821
822 towrt -= nblk;
823 blkno += nblk;
824 va += nblk * sc->sc_secsize;
825 }
826
827 dumping = 0;
828 return (0);
829 }
830
831 /*
832 * Adjust the size of a transfer.
833 */
834 static void
835 ldminphys(struct buf *bp)
836 {
837 struct ld_softc *sc;
838
839 sc = device_lookup(&ld_cd, DISKUNIT(bp->b_dev));
840
841 if (bp->b_bcount > sc->sc_maxxfer)
842 bp->b_bcount = sc->sc_maxxfer;
843 minphys(bp);
844 }
845
846 static void
847 ld_set_properties(struct ld_softc *ld)
848 {
849 prop_dictionary_t disk_info, odisk_info, geom;
850
851 disk_info = prop_dictionary_create();
852
853 geom = prop_dictionary_create();
854
855 prop_dictionary_set_uint64(geom, "sectors-per-unit",
856 ld->sc_secperunit);
857
858 prop_dictionary_set_uint32(geom, "sector-size",
859 ld->sc_secsize);
860
861 prop_dictionary_set_uint16(geom, "sectors-per-track",
862 ld->sc_nsectors);
863
864 prop_dictionary_set_uint16(geom, "tracks-per-cylinder",
865 ld->sc_nheads);
866
867 prop_dictionary_set_uint64(geom, "cylinders-per-unit",
868 ld->sc_ncylinders);
869
870 prop_dictionary_set(disk_info, "geometry", geom);
871 prop_object_release(geom);
872
873 prop_dictionary_set(device_properties(&ld->sc_dv),
874 "disk-info", disk_info);
875
876 /*
877 * Don't release disk_info here; we keep a reference to it.
878 * disk_detach() will release it when we go away.
879 */
880
881 odisk_info = ld->sc_dk.dk_info;
882 ld->sc_dk.dk_info = disk_info;
883 if (odisk_info)
884 prop_object_release(odisk_info);
885 }
886
887 static void
888 ld_config_interrupts (struct device *d)
889 {
890 struct ld_softc *sc = (struct ld_softc *)d;
891 dkwedge_discover(&sc->sc_dk);
892 }
893