ld.c revision 1.3 1 /* $NetBSD: ld.c,v 1.3 2001/01/03 21:01:28 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 "rnd.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/device.h>
49 #include <sys/queue.h>
50 #include <sys/proc.h>
51 #include <sys/buf.h>
52 #include <sys/endian.h>
53 #include <sys/disklabel.h>
54 #include <sys/disk.h>
55 #include <sys/dkio.h>
56 #include <sys/stat.h>
57 #include <sys/lock.h>
58 #include <sys/conf.h>
59 #include <sys/fcntl.h>
60 #include <sys/vnode.h>
61 #include <sys/syslog.h>
62 #if NRND > 0
63 #include <sys/rnd.h>
64 #endif
65
66 #include <dev/ldvar.h>
67
68 static void ldgetdefaultlabel(struct ld_softc *, struct disklabel *);
69 static void ldgetdisklabel(struct ld_softc *);
70 static int ldlock(struct ld_softc *);
71 static void ldminphys(struct buf *bp);
72 static void ldshutdown(void *);
73 static int ldstart(struct ld_softc *, struct buf *);
74 static void ldunlock(struct ld_softc *);
75
76 extern struct cfdriver ld_cd;
77
78 static struct dkdriver lddkdriver = { ldstrategy };
79 static void *ld_sdh;
80
81 void
82 ldattach(struct ld_softc *sc)
83 {
84 char buf[9];
85
86 /* Initialise and attach the disk structure. */
87 sc->sc_dk.dk_driver = &lddkdriver;
88 sc->sc_dk.dk_name = sc->sc_dv.dv_xname;
89 disk_attach(&sc->sc_dk);
90
91 if ((sc->sc_flags & LDF_ENABLED) == 0) {
92 printf("%s: disabled\n", sc->sc_dv.dv_xname);
93 return;
94 }
95 if (sc->sc_maxxfer > MAXPHYS)
96 sc->sc_maxxfer = MAXPHYS;
97
98 format_bytes(buf, sizeof(buf), (u_int64_t)sc->sc_secperunit *
99 sc->sc_secsize);
100 printf("%s: %s, %d cyl, %d head, %d sec, %d bytes/sect x %d sectors\n",
101 sc->sc_dv.dv_xname, buf, sc->sc_ncylinders, sc->sc_nheads,
102 sc->sc_nsectors, sc->sc_secsize, sc->sc_secperunit);
103
104 #if NRND > 0
105 /* Attach the device into the rnd source list. */
106 rnd_attach_source(&sc->sc_rnd_source, sc->sc_dv.dv_xname,
107 RND_TYPE_DISK, 0);
108 #endif
109
110 /* Set the `shutdownhook'. */
111 if (ld_sdh == NULL)
112 ld_sdh = shutdownhook_establish(ldshutdown, NULL);
113 BUFQ_INIT(&sc->sc_bufq);
114 }
115
116 int
117 lddrain(struct ld_softc *sc, int flags)
118 {
119 int s;
120
121 if ((flags & DETACH_FORCE) == 0 && sc->sc_dk.dk_openmask != 0)
122 return (EBUSY);
123
124 s = splbio();
125 sc->sc_flags |= LDF_DRAIN;
126 splx(s);
127 return (0);
128 }
129
130 void
131 lddetach(struct ld_softc *sc)
132 {
133 struct buf *bp;
134 int s, bmaj, cmaj, mn;
135
136 /* Wait for commands queued with the hardware to complete. */
137 if (sc->sc_queuecnt != 0)
138 tsleep(&sc->sc_queuecnt, PRIBIO, "lddrn", 30 * hz);
139
140 /* Locate the major numbers. */
141 for (bmaj = 0; bmaj <= nblkdev; bmaj++)
142 if (bdevsw[bmaj].d_open == sdopen)
143 break;
144 for (cmaj = 0; cmaj <= nchrdev; cmaj++)
145 if (cdevsw[cmaj].d_open == sdopen)
146 break;
147
148 /* Kill off any queued buffers. */
149 s = splbio();
150 while ((bp = BUFQ_FIRST(&sc->sc_bufq)) != NULL) {
151 BUFQ_REMOVE(&sc->sc_bufq, bp);
152 bp->b_error = EIO;
153 bp->b_flags |= B_ERROR;
154 bp->b_resid = bp->b_bcount;
155 biodone(bp);
156 }
157 splx(s);
158
159 /* Nuke the vnodes for any open instances. */
160 mn = DISKUNIT(sc->sc_dv.dv_unit);
161 vdevgone(bmaj, mn, mn + (MAXPARTITIONS - 1), VBLK);
162 vdevgone(cmaj, mn, mn + (MAXPARTITIONS - 1), VCHR);
163
164 /* Detach from the disk list. */
165 disk_detach(&sc->sc_dk);
166
167 #if NRND > 0
168 /* Unhook the entropy source. */
169 rnd_detach_source(&sc->sc_rnd_source);
170 #endif
171
172 /* Flush the device's cache. */
173 if (sc->sc_flush != NULL)
174 if ((*sc->sc_flush)(sc) != 0)
175 printf("%s: unable to flush cache\n",
176 sc->sc_dv.dv_xname);
177 }
178
179 static void
180 ldshutdown(void *cookie)
181 {
182 struct ld_softc *sc;
183 int i;
184
185 for (i = 0; i < ld_cd.cd_ndevs; i++) {
186 if ((sc = device_lookup(&ld_cd, i)) == NULL)
187 continue;
188 if (sc->sc_flush != NULL && (*sc->sc_flush)(sc) != 0)
189 printf("%s: unable to flush cache\n",
190 sc->sc_dv.dv_xname);
191 }
192 }
193
194 int
195 ldopen(dev_t dev, int flags, int fmt, struct proc *p)
196 {
197 struct ld_softc *sc;
198 int unit, part;
199
200 unit = DISKUNIT(dev);
201 if ((sc = device_lookup(&ld_cd, unit))== NULL)
202 return (ENXIO);
203 if ((sc->sc_flags & LDF_ENABLED) == 0)
204 return (ENODEV);
205 part = DISKPART(dev);
206 ldlock(sc);
207
208 if (sc->sc_dk.dk_openmask == 0)
209 ldgetdisklabel(sc);
210
211 /* Check that the partition exists. */
212 if (part != RAW_PART && (part >= sc->sc_dk.dk_label->d_npartitions ||
213 sc->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
214 ldunlock(sc);
215 return (ENXIO);
216 }
217
218 /* Ensure only one open at a time. */
219 switch (fmt) {
220 case S_IFCHR:
221 sc->sc_dk.dk_copenmask |= (1 << part);
222 break;
223 case S_IFBLK:
224 sc->sc_dk.dk_bopenmask |= (1 << part);
225 break;
226 }
227 sc->sc_dk.dk_openmask =
228 sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
229
230 ldunlock(sc);
231 return (0);
232 }
233
234 int
235 ldclose(dev_t dev, int flags, int fmt, struct proc *p)
236 {
237 struct ld_softc *sc;
238 int part, unit;
239
240 unit = DISKUNIT(dev);
241 part = DISKPART(dev);
242 sc = device_lookup(&ld_cd, unit);
243 ldlock(sc);
244
245 switch (fmt) {
246 case S_IFCHR:
247 sc->sc_dk.dk_copenmask &= ~(1 << part);
248 break;
249 case S_IFBLK:
250 sc->sc_dk.dk_bopenmask &= ~(1 << part);
251 break;
252 }
253 sc->sc_dk.dk_openmask =
254 sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
255
256 if (sc->sc_dk.dk_openmask == 0 && 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
261 ldunlock(sc);
262 return (0);
263 }
264
265 int
266 ldread(dev_t dev, struct uio *uio, int ioflag)
267 {
268
269 return (physio(ldstrategy, NULL, dev, B_READ, ldminphys, uio));
270 }
271
272 int
273 ldwrite(dev_t dev, struct uio *uio, int ioflag)
274 {
275
276 return (physio(ldstrategy, NULL, dev, B_WRITE, ldminphys, uio));
277 }
278
279 int
280 ldioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct proc *p)
281 {
282 struct ld_softc *sc;
283 int part, unit, error;
284
285 unit = DISKUNIT(dev);
286 part = DISKPART(dev);
287 sc = device_lookup(&ld_cd, unit);
288 error = 0;
289
290 switch (cmd) {
291 case DIOCGDINFO:
292 memcpy(addr, sc->sc_dk.dk_label, sizeof(struct disklabel));
293 return (0);
294
295 case DIOCGPART:
296 ((struct partinfo *)addr)->disklab = sc->sc_dk.dk_label;
297 ((struct partinfo *)addr)->part =
298 &sc->sc_dk.dk_label->d_partitions[part];
299 break;
300
301 case DIOCWDINFO:
302 case DIOCSDINFO:
303 if ((flag & FWRITE) == 0)
304 return (EBADF);
305
306 if ((error = ldlock(sc)) != 0)
307 return (error);
308 sc->sc_flags |= LDF_LABELLING;
309
310 error = setdisklabel(sc->sc_dk.dk_label,
311 (struct disklabel *)addr, /*sc->sc_dk.dk_openmask : */0,
312 sc->sc_dk.dk_cpulabel);
313 if (error == 0 && cmd == DIOCWDINFO)
314 error = writedisklabel(
315 MAKEDISKDEV(major(dev), DISKUNIT(dev), RAW_PART),
316 ldstrategy, sc->sc_dk.dk_label,
317 sc->sc_dk.dk_cpulabel);
318
319 sc->sc_flags &= ~LDF_LABELLING;
320 ldunlock(sc);
321 break;
322
323 case DIOCWLABEL:
324 if ((flag & FWRITE) == 0)
325 return (EBADF);
326 if (*(int *)addr)
327 sc->sc_flags |= LDF_WLABEL;
328 else
329 sc->sc_flags &= ~LDF_WLABEL;
330 break;
331
332 case DIOCGDEFLABEL:
333 ldgetdefaultlabel(sc, (struct disklabel *)addr);
334 break;
335
336 default:
337 error = ENOTTY;
338 break;
339 }
340
341 return (error);
342 }
343
344 void
345 ldstrategy(struct buf *bp)
346 {
347 struct ld_softc *sc;
348 int s;
349
350 sc = device_lookup(&ld_cd, DISKUNIT(bp->b_dev));
351
352 s = splbio();
353 if (sc->sc_queuecnt == sc->sc_maxqueuecnt) {
354 BUFQ_INSERT_TAIL(&sc->sc_bufq, bp);
355 splx(s);
356 return;
357 }
358 splx(s);
359 ldstart(sc, bp);
360 }
361
362 static int
363 ldstart(struct ld_softc *sc, struct buf *bp)
364 {
365 struct disklabel *lp;
366 int part, s, rv;
367
368 if ((sc->sc_flags & LDF_DRAIN) != 0) {
369 bp->b_error = EIO;
370 bp->b_flags |= B_ERROR;
371 bp->b_resid = bp->b_bcount;
372 biodone(bp);
373 return (-1);
374 }
375
376 part = DISKPART(bp->b_dev);
377 lp = sc->sc_dk.dk_label;
378
379 /*
380 * The transfer must be a whole number of blocks and the offset must
381 * not be negative.
382 */
383 if ((bp->b_bcount % lp->d_secsize) != 0 || bp->b_blkno < 0) {
384 bp->b_flags |= B_ERROR;
385 biodone(bp);
386 return (-1);
387 }
388
389 /*
390 * If it's a null transfer, return.
391 */
392 if (bp->b_bcount == 0) {
393 bp->b_resid = bp->b_bcount;
394 biodone(bp);
395 return (-1);
396 }
397
398 /*
399 * Do bounds checking and adjust the transfer. If error, process.
400 * If past the end of partition, just return.
401 */
402 if (part != RAW_PART &&
403 bounds_check_with_label(bp, lp,
404 (sc->sc_flags & (LDF_WLABEL | LDF_LABELLING)) != 0) <= 0) {
405 bp->b_resid = bp->b_bcount;
406 biodone(bp);
407 return (-1);
408 }
409
410 /*
411 * Convert the logical block number to a physical one and put it in
412 * terms of the device's logical block size.
413 */
414 if (lp->d_secsize >= DEV_BSIZE)
415 bp->b_rawblkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
416 else
417 bp->b_rawblkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
418
419 if (bp->b_dev != RAW_PART)
420 bp->b_rawblkno += lp->d_partitions[part].p_offset;
421
422 s = splbio();
423 disk_busy(&sc->sc_dk);
424 sc->sc_queuecnt++;
425 splx(s);
426
427 if ((rv = (*sc->sc_start)(sc, bp)) != 0) {
428 bp->b_error = rv;
429 bp->b_flags |= B_ERROR;
430 bp->b_resid = bp->b_bcount;
431 s = splbio();
432 lddone(sc, bp);
433 splx(s);
434 }
435
436 return (0);
437 }
438
439 void
440 lddone(struct ld_softc *sc, struct buf *bp)
441 {
442
443 if ((bp->b_flags & B_ERROR) != 0) {
444 diskerr(bp, "ld", "error", LOG_PRINTF, 0, sc->sc_dk.dk_label);
445 printf("\n");
446 }
447
448 disk_unbusy(&sc->sc_dk, bp->b_bcount - bp->b_resid);
449 #if NRND > 0
450 rnd_add_uint32(&sc->sc_rnd_source, bp->b_rawblkno);
451 #endif
452 biodone(bp);
453 if (--sc->sc_queuecnt == 0 && (sc->sc_flags & LDF_DRAIN) != 0)
454 wakeup(&sc->sc_queuecnt);
455
456 while ((bp = BUFQ_FIRST(&sc->sc_bufq)) != NULL) {
457 BUFQ_REMOVE(&sc->sc_bufq, bp);
458 if (!ldstart(sc, bp))
459 break;
460 }
461 }
462
463 int
464 ldsize(dev_t dev)
465 {
466 struct ld_softc *sc;
467 int part, unit, omask, size;
468
469 unit = DISKUNIT(dev);
470 if ((sc = device_lookup(&ld_cd, unit)) == NULL)
471 return (ENODEV);
472 if ((sc->sc_flags & LDF_ENABLED) == 0)
473 return (ENODEV);
474 part = DISKPART(dev);
475
476 omask = sc->sc_dk.dk_openmask & (1 << part);
477
478 if (omask == 0 && ldopen(dev, 0, S_IFBLK, NULL) != 0)
479 return (-1);
480 else if (sc->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
481 size = -1;
482 else
483 size = sc->sc_dk.dk_label->d_partitions[part].p_size *
484 (sc->sc_dk.dk_label->d_secsize / DEV_BSIZE);
485 if (omask == 0 && ldclose(dev, 0, S_IFBLK, NULL) != 0)
486 return (-1);
487
488 return (size);
489 }
490
491 /*
492 * Load the label information from the specified device.
493 */
494 static void
495 ldgetdisklabel(struct ld_softc *sc)
496 {
497 const char *errstring;
498
499 ldgetdefaultlabel(sc, sc->sc_dk.dk_label);
500
501 /* Call the generic disklabel extraction routine. */
502 errstring = readdisklabel(MAKEDISKDEV(0, sc->sc_dv.dv_unit, RAW_PART),
503 ldstrategy, sc->sc_dk.dk_label, sc->sc_dk.dk_cpulabel);
504 if (errstring != NULL)
505 printf("%s: %s\n", sc->sc_dv.dv_xname, errstring);
506 }
507
508 /*
509 * Construct a ficticious label.
510 */
511 static void
512 ldgetdefaultlabel(struct ld_softc *sc, struct disklabel *lp)
513 {
514
515 memset(lp, 0, sizeof(struct disklabel));
516
517 lp->d_secsize = sc->sc_secsize;
518 lp->d_ntracks = sc->sc_nheads;
519 lp->d_nsectors = sc->sc_nsectors;
520 lp->d_ncylinders = sc->sc_ncylinders;
521 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
522 lp->d_type = DTYPE_LD;
523 strcpy(lp->d_typename, "unknown");
524 strcpy(lp->d_packname, "fictitious");
525 lp->d_secperunit = sc->sc_secperunit;
526 lp->d_rpm = 7200;
527 lp->d_interleave = 1;
528 lp->d_flags = 0;
529
530 lp->d_partitions[RAW_PART].p_offset = 0;
531 lp->d_partitions[RAW_PART].p_size =
532 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
533 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
534 lp->d_npartitions = RAW_PART + 1;
535
536 lp->d_magic = DISKMAGIC;
537 lp->d_magic2 = DISKMAGIC;
538 lp->d_checksum = dkcksum(lp);
539 }
540
541 /*
542 * Wait interruptibly for an exclusive lock.
543 *
544 * XXX Several drivers do this; it should be abstracted and made MP-safe.
545 */
546 static int
547 ldlock(struct ld_softc *sc)
548 {
549 int error;
550
551 while ((sc->sc_flags & LDF_LKHELD) != 0) {
552 sc->sc_flags |= LDF_LKWANTED;
553 if ((error = tsleep(sc, PRIBIO | PCATCH, "ldlck", 0)) != 0)
554 return (error);
555 }
556 sc->sc_flags |= LDF_LKHELD;
557 return (0);
558 }
559
560 /*
561 * Unlock and wake up any waiters.
562 */
563 static void
564 ldunlock(struct ld_softc *sc)
565 {
566
567 sc->sc_flags &= ~LDF_LKHELD;
568 if ((sc->sc_flags & LDF_LKWANTED) != 0) {
569 sc->sc_flags &= ~LDF_LKWANTED;
570 wakeup(sc);
571 }
572 }
573
574 /*
575 * Take a dump.
576 */
577 int
578 lddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
579 {
580 struct ld_softc *sc;
581 struct disklabel *lp;
582 int unit, part, nsects, sectoff, towrt, nblk, maxblkcnt, rv;
583 static int dumping;
584
585 unit = DISKUNIT(dev);
586 if ((sc = device_lookup(&ld_cd, unit)) == NULL)
587 return (ENXIO);
588 if ((sc->sc_flags & LDF_ENABLED) == 0)
589 return (ENODEV);
590 if (sc->sc_dump == NULL)
591 return (ENXIO);
592
593 /* Check if recursive dump; if so, punt. */
594 if (dumping)
595 return (EFAULT);
596 dumping = 1;
597
598 /* Convert to disk sectors. Request must be a multiple of size. */
599 part = DISKPART(dev);
600 lp = sc->sc_dk.dk_label;
601 if ((size % lp->d_secsize) != 0)
602 return (EFAULT);
603 towrt = size / lp->d_secsize;
604 blkno = dbtob(blkno) / lp->d_secsize; /* blkno in DEV_BSIZE units */
605
606 nsects = lp->d_partitions[part].p_size;
607 sectoff = lp->d_partitions[part].p_offset;
608
609 /* Check transfer bounds against partition size. */
610 if ((blkno < 0) || ((blkno + towrt) > nsects))
611 return (EINVAL);
612
613 /* Offset block number to start of partition. */
614 blkno += sectoff;
615
616 /* Start dumping and return when done. */
617 maxblkcnt = sc->sc_maxxfer / sc->sc_secsize - 1;
618 while (towrt > 0) {
619 nblk = min(maxblkcnt, towrt);
620
621 if ((rv = (*sc->sc_dump)(sc, va, blkno, nblk)) != 0)
622 return (rv);
623
624 towrt -= nblk;
625 blkno += nblk;
626 va += nblk * sc->sc_secsize;
627 }
628
629 dumping = 0;
630 return (0);
631 }
632
633 /*
634 * Adjust the size of a transfer.
635 */
636 static void
637 ldminphys(struct buf *bp)
638 {
639 struct ld_softc *sc;
640
641 sc = device_lookup(&ld_cd, DISKUNIT(bp->b_dev));
642
643 if (bp->b_bcount > sc->sc_maxxfer)
644 bp->b_bcount = sc->sc_maxxfer;
645 minphys(bp);
646 }
647