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