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