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