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