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