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