Home | History | Annotate | Line # | Download | only in dev
dksubr.c revision 1.2
      1 /* $NetBSD: dksubr.c,v 1.2 2002/10/09 14:04:08 elric Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1996, 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe and Roland C. Dowdeswell.
      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 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/stat.h>
     42 #include <sys/proc.h>
     43 #include <sys/ioctl.h>
     44 #include <sys/device.h>
     45 #include <sys/disk.h>
     46 #include <sys/disklabel.h>
     47 #include <sys/vnode.h>
     48 #include <sys/fcntl.h>
     49 #include <sys/namei.h>
     50 
     51 #include <dev/dkvar.h>
     52 
     53 int	dkdebug = 0;
     54 
     55 #ifdef DEBUG
     56 #define DKDB_FOLLOW	0x1
     57 #define DKDB_INIT	0x2
     58 #define DKDB_VNODE	0x4
     59 
     60 #define IFDEBUG(x,y)		if (dkdebug & (x)) y
     61 #define DPRINTF(x,y)		IFDEBUG(x, printf y)
     62 #define DPRINTF_FOLLOW(y)	DPRINTF(DKDB_FOLLOW, y)
     63 #else
     64 #define IFDEBUG(x,y)
     65 #define DPRINTF(x,y)
     66 #define DPRINTF_FOLLOW(y)
     67 #endif
     68 
     69 #define DKLABELDEV(dev)	\
     70 	(MAKEDISKDEV(major((dev)), DISKUNIT((dev)), RAW_PART))
     71 
     72 void	dk_makedisklabel(struct dk_intf *, struct dk_softc *);
     73 
     74 void
     75 dk_sc_init(struct dk_softc *dksc, void *osc, char *xname)
     76 {
     77 
     78 	memset(dksc, 0x0, sizeof(*dksc));
     79 	dksc->sc_osc = osc;
     80 	strncpy(dksc->sc_xname, xname, DK_XNAME_SIZE);
     81 	dksc->sc_dkdev.dk_name = dksc->sc_xname;
     82 	lockinit(&dksc->sc_lock, PRIBIO, "dklk", 0, 0);
     83 }
     84 
     85 /* ARGSUSED */
     86 int
     87 dk_open(struct dk_intf *di, struct dk_softc *dksc, dev_t dev,
     88 	   int flags, int fmt, struct proc *p)
     89 {
     90 	struct	disklabel *lp = dksc->sc_dkdev.dk_label;
     91 	int	part = DISKPART(dev);
     92 	int	pmask = 1 << part;
     93 	int	ret = 0;
     94 
     95 	DPRINTF_FOLLOW(("dk_open(%s, %p, 0x%x, 0x%x)\n",
     96 	    di->di_dkname, dksc, dev, flags));
     97 
     98 	if ((ret = lockmgr(&dksc->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
     99 		return ret;
    100 
    101 	part = DISKPART(dev);
    102 	pmask = 1 << part;
    103 
    104 	/*
    105 	 * If we're init'ed and there are no other open partitions then
    106 	 * update the in-core disklabel.
    107 	 */
    108 	if ((dksc->sc_flags & DKF_INITED) && dksc->sc_dkdev.dk_openmask == 0)
    109 		dk_getdisklabel(di, dksc, dev);
    110 
    111 	/* Fail if we can't find the partition. */
    112 	if ((part != RAW_PART) &&
    113 	    (((dksc->sc_flags & DKF_INITED) == 0) ||
    114 	    ((part >= lp->d_npartitions) ||
    115 	    (lp->d_partitions[part].p_fstype == FS_UNUSED)))) {
    116 		ret = ENXIO;
    117 		goto done;
    118 	}
    119 
    120 	/* Mark our unit as open. */
    121 	switch (fmt) {
    122 	case S_IFCHR:
    123 		dksc->sc_dkdev.dk_copenmask |= pmask;
    124 		break;
    125 	case S_IFBLK:
    126 		dksc->sc_dkdev.dk_bopenmask |= pmask;
    127 		break;
    128 	}
    129 
    130 	dksc->sc_dkdev.dk_openmask =
    131 	    dksc->sc_dkdev.dk_copenmask | dksc->sc_dkdev.dk_bopenmask;
    132 
    133 done:
    134 	lockmgr(&dksc->sc_lock, LK_RELEASE, NULL);
    135 	return ret;
    136 }
    137 
    138 /* ARGSUSED */
    139 int
    140 dk_close(struct dk_intf *di, struct dk_softc *dksc, dev_t dev,
    141 	    int flags, int fmt, struct proc *p)
    142 {
    143 	int	part = DISKPART(dev);
    144 	int	pmask = 1 << part;
    145 	int	ret;
    146 
    147 	DPRINTF_FOLLOW(("dk_close(%s, %p, 0x%x, 0x%x)\n",
    148 	    di->di_dkname, dksc, dev, flags));
    149 
    150 	if ((ret = lockmgr(&dksc->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
    151 		return ret;
    152 
    153 	switch (fmt) {
    154 	case S_IFCHR:
    155 		dksc->sc_dkdev.dk_copenmask &= ~pmask;
    156 		break;
    157 	case S_IFBLK:
    158 		dksc->sc_dkdev.dk_bopenmask &= ~pmask;
    159 		break;
    160 	}
    161 	dksc->sc_dkdev.dk_openmask =
    162 	    dksc->sc_dkdev.dk_copenmask | dksc->sc_dkdev.dk_bopenmask;
    163 
    164 	lockmgr(&dksc->sc_lock, LK_RELEASE, NULL);
    165 	return 0;
    166 }
    167 
    168 void
    169 dk_strategy(struct dk_intf *di, struct dk_softc *dksc, struct buf *bp)
    170 {
    171 	struct	disklabel *lp = dksc->sc_dkdev.dk_label;
    172 	int	s;
    173 	int	wlabel;
    174 
    175 	DPRINTF_FOLLOW(("dk_strategy(%s, %p, %p)\n",
    176 	    di->di_dkname, dksc, bp));
    177 
    178 	if (!(dksc->sc_flags & DKF_INITED)) {
    179 		DPRINTF_FOLLOW(("dk_stragy: not inited\n"));
    180 		bp->b_error  = ENXIO;
    181 		bp->b_flags |= B_ERROR;
    182 		biodone(bp);
    183 		return;
    184 	}
    185 
    186 	/* XXX look for some more errors, c.f. ld.c */
    187 
    188 	bp->b_resid = bp->b_bcount;
    189 
    190 	/* If there is nothing to do, then we are done */
    191 	if (bp->b_bcount == 0) {
    192 		biodone(bp);
    193 		return;
    194 	}
    195 
    196 	wlabel = dksc->sc_flags & (DKF_WLABEL|DKF_LABELLING);
    197 	if (DISKPART(bp->b_dev) != RAW_PART &&
    198 	    bounds_check_with_label(bp, lp, wlabel) <= 0) {
    199 		biodone(bp);
    200 		return;
    201 	}
    202 
    203 	/*
    204 	 * Start the unit by calling the start routine
    205 	 * provided by the individual driver.
    206 	 */
    207 	s = splbio();
    208 	di->di_diskstart(dksc->sc_osc, bp);
    209 	splx(s);
    210 	return;
    211 }
    212 
    213 int
    214 dk_size(struct dk_intf *di, struct dk_softc *dksc, dev_t dev)
    215 {
    216 	struct	disklabel *lp;
    217 	int	is_open;
    218 	int	part;
    219 	int	size;
    220 
    221 	if ((dksc->sc_flags & DKF_INITED) == 0)
    222 		return -1;
    223 
    224 	part = DISKPART(dev);
    225 	is_open = dksc->sc_dkdev.dk_openmask & (1 << part);
    226 
    227 	if (!is_open && di->di_open(dev, 0, S_IFBLK, curproc))
    228 		return -1;
    229 
    230 	lp = dksc->sc_dkdev.dk_label;
    231 	if (lp->d_partitions[part].p_fstype != FS_SWAP)
    232 		size = -1;
    233 	else
    234 		size = lp->d_partitions[part].p_size *
    235 		    (lp->d_secsize / DEV_BSIZE);
    236 
    237 	if (!is_open && di->di_close(dev, 0, S_IFBLK, curproc))
    238 		return 1;
    239 
    240 	return size;
    241 }
    242 
    243 int
    244 dk_ioctl(struct dk_intf *di, struct dk_softc *dksc, dev_t dev,
    245 	    u_long cmd, caddr_t data, int flag, struct proc *p)
    246 {
    247 	struct	disklabel *lp;
    248 #ifdef __HAVE_OLD_DISKLABEL
    249 	struct	disklabel newlabel;
    250 #endif
    251 	int	error = 0;
    252 
    253 	DPRINTF_FOLLOW(("dk_ioctl(%s, %p, 0x%x, 0x%lx)\n",
    254 	    di->di_dkname, dksc, dev, cmd));
    255 
    256 	/* ensure that the pseudo disk is open for writes for these commands */
    257 	switch (cmd) {
    258 	case DIOCSDINFO:
    259 	case DIOCWDINFO:
    260 #ifdef __HAVE_OLD_DISKLABEL
    261 	case ODIOCSDINFO:
    262 	case ODIOCWDINFO:
    263 #endif
    264 	case DIOCWLABEL:
    265 		if ((flag & FWRITE) == 0)
    266 			return EBADF;
    267 	}
    268 
    269 	/* ensure that the pseudo-disk is initialized for these */
    270 	switch (cmd) {
    271 	case DIOCGDINFO:
    272 	case DIOCSDINFO:
    273 	case DIOCWDINFO:
    274 	case DIOCGPART:
    275 	case DIOCWLABEL:
    276 	case DIOCGDEFLABEL:
    277 #ifdef __HAVE_OLD_DISKLABEL
    278 	case ODIOCGDINFO:
    279 	case ODIOCSDINFO:
    280 	case ODIOCWDINFO:
    281 	case ODIOCGDEFLABEL:
    282 #endif
    283 		if ((dksc->sc_flags & DKF_INITED) == 0)
    284 			return ENXIO;
    285 	}
    286 
    287 	switch (cmd) {
    288 	case DIOCGDINFO:
    289 		*(struct disklabel *)data = *(dksc->sc_dkdev.dk_label);
    290 		break;
    291 
    292 #ifdef __HAVE_OLD_DISKLABEL
    293 	case ODIOCGDINFO:
    294 		newlabel = *(dksc->sc_dkdev.dk_label);
    295 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
    296 			return ENOTTY;
    297 		memcpy(data, &newlabel, sizeof (struct olddisklabel));
    298 		break;
    299 #endif
    300 
    301 	case DIOCGPART:
    302 		((struct partinfo *)data)->disklab = dksc->sc_dkdev.dk_label;
    303 		((struct partinfo *)data)->part =
    304 		    &dksc->sc_dkdev.dk_label->d_partitions[DISKPART(dev)];
    305 		break;
    306 
    307 	case DIOCWDINFO:
    308 	case DIOCSDINFO:
    309 #ifdef __HAVE_OLD_DISKLABEL
    310 	case ODIOCWDINFO:
    311 	case ODIOCSDINFO:
    312 #endif
    313 #ifdef __HAVE_OLD_DISKLABEL
    314 		if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
    315 			memset(&newlabel, 0, sizeof newlabel);
    316 			memcpy(&newlabel, data, sizeof (struct olddisklabel));
    317 			lp = &newlabel;
    318 		} else
    319 #endif
    320 		lp = (struct disklabel *)data;
    321 
    322 		dksc->sc_flags |= DKF_LABELLING;
    323 
    324 		error = setdisklabel(dksc->sc_dkdev.dk_label,
    325 		    lp, 0, dksc->sc_dkdev.dk_cpulabel);
    326 		if (error == 0) {
    327 			if (cmd == DIOCWDINFO
    328 #ifdef __HAVE_OLD_DISKLABEL
    329 			    || cmd == ODIOCWDINFO
    330 #endif
    331 			   )
    332 				error = writedisklabel(DKLABELDEV(dev),
    333 				    di->di_strategy, dksc->sc_dkdev.dk_label,
    334 				    dksc->sc_dkdev.dk_cpulabel);
    335 		}
    336 
    337 		dksc->sc_flags &= ~DKF_LABELLING;
    338 		break;
    339 
    340 	case DIOCWLABEL:
    341 		if (*(int *)data != 0)
    342 			dksc->sc_flags |= DKF_WLABEL;
    343 		else
    344 			dksc->sc_flags &= ~DKF_WLABEL;
    345 		break;
    346 
    347 	case DIOCGDEFLABEL:
    348 		dk_getdefaultlabel(di, dksc, (struct disklabel *)data);
    349 		break;
    350 
    351 #ifdef __HAVE_OLD_DISKLABEL
    352 	case ODIOCGDEFLABEL:
    353 		dk_getdefaultlabel(di, dksc, &newlabel);
    354 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
    355 			return ENOTTY;
    356 		memcpy(data, &newlabel, sizeof (struct olddisklabel));
    357 		break;
    358 #endif
    359 
    360 	default:
    361 		error = ENOTTY;
    362 	}
    363 
    364 	return error;
    365 }
    366 
    367 /*
    368  * dk_dump dumps all of physical memory into the partition specified.
    369  * This requires substantially more framework than {s,w}ddump, and hence
    370  * is probably much more fragile.
    371  *
    372  * XXX: we currently do not implement this.
    373  */
    374 
    375 #define DKF_READYFORDUMP	(DKF_INITED|DKF_TAKEDUMP)
    376 #define DKFF_READYFORDUMP(x)	(((x) & DKF_READYFORDUMP) == DKF_READYFORDUMP)
    377 static volatile int	dk_dumping = 0;
    378 
    379 /* ARGSUSED */
    380 int
    381 dk_dump(struct dk_intf *di, struct dk_softc *dksc, dev_t dev,
    382 	   daddr_t blkno, caddr_t va, size_t size)
    383 {
    384 
    385 	/*
    386 	 * ensure that we consider this device to be safe for dumping,
    387 	 * and that the device is configured.
    388 	 */
    389 	if (!DKFF_READYFORDUMP(dksc->sc_flags))
    390 		return ENXIO;
    391 
    392 	/* ensure that we are not already dumping */
    393 	if (dk_dumping)
    394 		return EFAULT;
    395 	dk_dumping = 1;
    396 
    397 	/* XXX: unimplemented */
    398 
    399 	dk_dumping = 0;
    400 
    401 	/* XXX: actually for now, we are going to leave this alone */
    402 	return ENXIO;
    403 }
    404 
    405 /* ARGSUSED */
    406 void
    407 dk_getdefaultlabel(struct dk_intf *di, struct dk_softc *dksc,
    408 		      struct disklabel *lp)
    409 {
    410 	struct dk_geom *pdg = &dksc->sc_geom;
    411 
    412 	lp->d_secperunit = dksc->sc_size;
    413 	lp->d_secsize = pdg->pdg_secsize;
    414 	lp->d_nsectors = pdg->pdg_nsectors;
    415 	lp->d_ntracks = pdg->pdg_ntracks;
    416 	lp->d_ncylinders = pdg->pdg_ncylinders;
    417 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
    418 
    419 	strncpy(lp->d_typename, di->di_dkname, sizeof(lp->d_typename));
    420 	lp->d_type = di->di_dtype;
    421 	strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
    422 	lp->d_rpm = 3600;
    423 	lp->d_interleave = 1;
    424 	lp->d_flags = 0;
    425 
    426 	lp->d_partitions[RAW_PART].p_offset = 0;
    427 	lp->d_partitions[RAW_PART].p_size = dksc->sc_size;
    428 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
    429 	lp->d_npartitions = RAW_PART + 1;
    430 
    431 	lp->d_magic = DISKMAGIC;
    432 	lp->d_magic2 = DISKMAGIC;
    433 	lp->d_checksum = dkcksum(dksc->sc_dkdev.dk_label);
    434 }
    435 
    436 /* ARGSUSED */
    437 void
    438 dk_getdisklabel(struct dk_intf *di, struct dk_softc *dksc, dev_t dev)
    439 {
    440 	struct	 disklabel *lp = dksc->sc_dkdev.dk_label;
    441 	struct	 cpu_disklabel *clp = dksc->sc_dkdev.dk_cpulabel;
    442 	struct	 partition *pp;
    443 	int	 i;
    444 	char	*errstring;
    445 
    446 	memset(clp, 0x0, sizeof(*clp));
    447 	dk_getdefaultlabel(di, dksc, lp);
    448 	errstring = readdisklabel(DKLABELDEV(dev), di->di_strategy,
    449 	    dksc->sc_dkdev.dk_label, dksc->sc_dkdev.dk_cpulabel);
    450 	if (errstring) {
    451 		dk_makedisklabel(di, dksc);
    452 		if (dksc->sc_flags & DKF_WARNLABEL)
    453 			printf("%s: %s\n", dksc->sc_xname, errstring);
    454 		return;
    455 	}
    456 
    457 	if ((dksc->sc_flags & DKF_LABELSANITY) == 0)
    458 		return;
    459 
    460 	/* Sanity check */
    461 	if (lp->d_secperunit != dksc->sc_size)
    462 		printf("WARNING: %s: total sector size in disklabel (%d) "
    463 		    "!= the size of %s (%lu)\n", dksc->sc_xname,
    464 		    lp->d_secperunit, di->di_dkname, (u_long)dksc->sc_size);
    465 
    466 	for (i=0; i < lp->d_npartitions; i++) {
    467 		pp = &lp->d_partitions[i];
    468 		if (pp->p_offset + pp->p_size > dksc->sc_size)
    469 			printf("WARNING: %s: end of partition `%c' exceeds "
    470 			    "the size of %s (%lu)\n", dksc->sc_xname,
    471 			    'a' + i, di->di_dkname, (u_long)dksc->sc_size);
    472 	}
    473 }
    474 
    475 /* ARGSUSED */
    476 void
    477 dk_makedisklabel(struct dk_intf *di, struct dk_softc *dksc)
    478 {
    479 	struct	disklabel *lp = dksc->sc_dkdev.dk_label;
    480 
    481 	lp->d_partitions[RAW_PART].p_fstype = FS_BSDFFS;
    482 	strncpy(lp->d_packname, "default label", sizeof(lp->d_packname));
    483 	lp->d_checksum = dkcksum(lp);
    484 }
    485 
    486 /* This function is taken from ccd.c:1.76  --rcd */
    487 
    488 /*
    489  * XXX this function looks too generic for dksubr.c, shouldn't we
    490  *     put it somewhere better?
    491  */
    492 
    493 /*
    494  * Lookup the provided name in the filesystem.  If the file exists,
    495  * is a valid block device, and isn't being used by anyone else,
    496  * set *vpp to the file's vnode.
    497  */
    498 int
    499 dk_lookup(path, p, vpp)
    500 	char *path;
    501 	struct proc *p;
    502 	struct vnode **vpp;	/* result */
    503 {
    504 	struct nameidata nd;
    505 	struct vnode *vp;
    506 	struct vattr va;
    507 	int error;
    508 
    509 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, path, p);
    510 	if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
    511 		DPRINTF((DKDB_FOLLOW|DKDB_INIT),
    512 		    ("dk_lookup: vn_open error = %d\n", error));
    513 		return (error);
    514 	}
    515 	vp = nd.ni_vp;
    516 
    517 	if (vp->v_usecount > 1) {
    518 		VOP_UNLOCK(vp, 0);
    519 		(void)vn_close(vp, FREAD|FWRITE, p->p_ucred, p);
    520 		return (EBUSY);
    521 	}
    522 
    523 	if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)) != 0) {
    524 		DPRINTF((DKDB_FOLLOW|DKDB_INIT),
    525 		    ("dk_lookup: getattr error = %d\n", error));
    526 		VOP_UNLOCK(vp, 0);
    527 		(void)vn_close(vp, FREAD|FWRITE, p->p_ucred, p);
    528 		return (error);
    529 	}
    530 
    531 	/* XXX: eventually we should handle VREG, too. */
    532 	if (va.va_type != VBLK) {
    533 		VOP_UNLOCK(vp, 0);
    534 		(void)vn_close(vp, FREAD|FWRITE, p->p_ucred, p);
    535 		return (ENOTBLK);
    536 	}
    537 
    538 	IFDEBUG(DKDB_VNODE, vprint("dk_lookup: vnode info", vp));
    539 
    540 	VOP_UNLOCK(vp, 0);
    541 	*vpp = vp;
    542 	return (0);
    543 }
    544