Home | History | Annotate | Line # | Download | only in dev
dksubr.c revision 1.42.12.1
      1  1.42.12.1       mrg /* $NetBSD: dksubr.c,v 1.42.12.1 2012/06/02 11:09:16 mrg Exp $ */
      2        1.1     elric 
      3        1.1     elric /*-
      4       1.34        ad  * Copyright (c) 1996, 1997, 1998, 1999, 2002, 2008 The NetBSD Foundation, Inc.
      5        1.1     elric  * All rights reserved.
      6        1.1     elric  *
      7        1.1     elric  * This code is derived from software contributed to The NetBSD Foundation
      8        1.1     elric  * by Jason R. Thorpe and Roland C. Dowdeswell.
      9        1.1     elric  *
     10        1.1     elric  * Redistribution and use in source and binary forms, with or without
     11        1.1     elric  * modification, are permitted provided that the following conditions
     12        1.1     elric  * are met:
     13        1.1     elric  * 1. Redistributions of source code must retain the above copyright
     14        1.1     elric  *    notice, this list of conditions and the following disclaimer.
     15        1.1     elric  * 2. Redistributions in binary form must reproduce the above copyright
     16        1.1     elric  *    notice, this list of conditions and the following disclaimer in the
     17        1.1     elric  *    documentation and/or other materials provided with the distribution.
     18        1.1     elric  *
     19        1.1     elric  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20        1.1     elric  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21        1.1     elric  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22        1.1     elric  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23        1.1     elric  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24        1.1     elric  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25        1.1     elric  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26        1.1     elric  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27        1.1     elric  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28        1.1     elric  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29        1.1     elric  * POSSIBILITY OF SUCH DAMAGE.
     30        1.1     elric  */
     31       1.10     lukem 
     32       1.10     lukem #include <sys/cdefs.h>
     33  1.42.12.1       mrg __KERNEL_RCSID(0, "$NetBSD: dksubr.c,v 1.42.12.1 2012/06/02 11:09:16 mrg Exp $");
     34        1.1     elric 
     35        1.1     elric #include <sys/param.h>
     36        1.1     elric #include <sys/systm.h>
     37        1.1     elric #include <sys/stat.h>
     38        1.1     elric #include <sys/proc.h>
     39        1.1     elric #include <sys/ioctl.h>
     40        1.1     elric #include <sys/device.h>
     41        1.1     elric #include <sys/disk.h>
     42        1.1     elric #include <sys/disklabel.h>
     43       1.14      yamt #include <sys/buf.h>
     44       1.14      yamt #include <sys/bufq.h>
     45        1.1     elric #include <sys/vnode.h>
     46        1.1     elric #include <sys/fcntl.h>
     47        1.1     elric #include <sys/namei.h>
     48        1.1     elric 
     49        1.1     elric #include <dev/dkvar.h>
     50        1.1     elric 
     51        1.1     elric int	dkdebug = 0;
     52        1.1     elric 
     53        1.1     elric #ifdef DEBUG
     54        1.1     elric #define DKDB_FOLLOW	0x1
     55        1.1     elric #define DKDB_INIT	0x2
     56        1.1     elric #define DKDB_VNODE	0x4
     57        1.1     elric 
     58        1.1     elric #define IFDEBUG(x,y)		if (dkdebug & (x)) y
     59        1.1     elric #define DPRINTF(x,y)		IFDEBUG(x, printf y)
     60        1.1     elric #define DPRINTF_FOLLOW(y)	DPRINTF(DKDB_FOLLOW, y)
     61        1.1     elric #else
     62        1.1     elric #define IFDEBUG(x,y)
     63        1.1     elric #define DPRINTF(x,y)
     64        1.1     elric #define DPRINTF_FOLLOW(y)
     65        1.1     elric #endif
     66        1.1     elric 
     67        1.1     elric #define DKLABELDEV(dev)	\
     68        1.1     elric 	(MAKEDISKDEV(major((dev)), DISKUNIT((dev)), RAW_PART))
     69        1.1     elric 
     70       1.13   thorpej static void	dk_makedisklabel(struct dk_intf *, struct dk_softc *);
     71        1.1     elric 
     72        1.1     elric void
     73  1.42.12.1       mrg dk_sc_init(struct dk_softc *dksc, const char *xname)
     74        1.1     elric {
     75        1.1     elric 
     76        1.1     elric 	memset(dksc, 0x0, sizeof(*dksc));
     77        1.1     elric 	strncpy(dksc->sc_xname, xname, DK_XNAME_SIZE);
     78        1.1     elric 	dksc->sc_dkdev.dk_name = dksc->sc_xname;
     79        1.1     elric }
     80        1.1     elric 
     81        1.1     elric /* ARGSUSED */
     82        1.1     elric int
     83        1.1     elric dk_open(struct dk_intf *di, struct dk_softc *dksc, dev_t dev,
     84       1.27  christos     int flags, int fmt, struct lwp *l)
     85        1.1     elric {
     86        1.1     elric 	struct	disklabel *lp = dksc->sc_dkdev.dk_label;
     87        1.1     elric 	int	part = DISKPART(dev);
     88        1.1     elric 	int	pmask = 1 << part;
     89        1.1     elric 	int	ret = 0;
     90       1.16      yamt 	struct disk *dk = &dksc->sc_dkdev;
     91        1.1     elric 
     92       1.39    cegger 	DPRINTF_FOLLOW(("dk_open(%s, %p, 0x%"PRIx64", 0x%x)\n",
     93        1.1     elric 	    di->di_dkname, dksc, dev, flags));
     94        1.1     elric 
     95       1.30        ad 	mutex_enter(&dk->dk_openlock);
     96        1.1     elric 	part = DISKPART(dev);
     97       1.16      yamt 
     98       1.16      yamt 	/*
     99       1.16      yamt 	 * If there are wedges, and this is not RAW_PART, then we
    100       1.16      yamt 	 * need to fail.
    101       1.16      yamt 	 */
    102       1.16      yamt 	if (dk->dk_nwedges != 0 && part != RAW_PART) {
    103       1.16      yamt 		ret = EBUSY;
    104       1.16      yamt 		goto done;
    105       1.16      yamt 	}
    106       1.16      yamt 
    107        1.1     elric 	pmask = 1 << part;
    108        1.1     elric 
    109        1.1     elric 	/*
    110        1.1     elric 	 * If we're init'ed and there are no other open partitions then
    111        1.1     elric 	 * update the in-core disklabel.
    112        1.1     elric 	 */
    113       1.16      yamt 	if ((dksc->sc_flags & DKF_INITED)) {
    114       1.16      yamt 		if (dk->dk_openmask == 0) {
    115       1.16      yamt 			dk_getdisklabel(di, dksc, dev);
    116       1.16      yamt 		}
    117       1.16      yamt 		/* XXX re-discover wedges? */
    118       1.16      yamt 	}
    119        1.1     elric 
    120        1.1     elric 	/* Fail if we can't find the partition. */
    121        1.1     elric 	if ((part != RAW_PART) &&
    122        1.1     elric 	    (((dksc->sc_flags & DKF_INITED) == 0) ||
    123        1.1     elric 	    ((part >= lp->d_npartitions) ||
    124        1.1     elric 	    (lp->d_partitions[part].p_fstype == FS_UNUSED)))) {
    125        1.1     elric 		ret = ENXIO;
    126        1.1     elric 		goto done;
    127        1.1     elric 	}
    128        1.1     elric 
    129        1.1     elric 	/* Mark our unit as open. */
    130        1.1     elric 	switch (fmt) {
    131        1.1     elric 	case S_IFCHR:
    132       1.16      yamt 		dk->dk_copenmask |= pmask;
    133        1.1     elric 		break;
    134        1.1     elric 	case S_IFBLK:
    135       1.16      yamt 		dk->dk_bopenmask |= pmask;
    136        1.1     elric 		break;
    137        1.1     elric 	}
    138        1.1     elric 
    139       1.16      yamt 	dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
    140        1.1     elric 
    141        1.1     elric done:
    142       1.30        ad 	mutex_exit(&dk->dk_openlock);
    143        1.1     elric 	return ret;
    144        1.1     elric }
    145        1.1     elric 
    146        1.1     elric /* ARGSUSED */
    147        1.1     elric int
    148       1.27  christos dk_close(struct dk_intf *di, struct dk_softc *dksc, dev_t dev,
    149       1.27  christos     int flags, int fmt, struct lwp *l)
    150        1.1     elric {
    151        1.1     elric 	int	part = DISKPART(dev);
    152        1.1     elric 	int	pmask = 1 << part;
    153       1.16      yamt 	struct disk *dk = &dksc->sc_dkdev;
    154        1.1     elric 
    155       1.39    cegger 	DPRINTF_FOLLOW(("dk_close(%s, %p, 0x%"PRIx64", 0x%x)\n",
    156        1.1     elric 	    di->di_dkname, dksc, dev, flags));
    157        1.1     elric 
    158       1.30        ad 	mutex_enter(&dk->dk_openlock);
    159        1.1     elric 
    160        1.1     elric 	switch (fmt) {
    161        1.1     elric 	case S_IFCHR:
    162       1.16      yamt 		dk->dk_copenmask &= ~pmask;
    163        1.1     elric 		break;
    164        1.1     elric 	case S_IFBLK:
    165       1.16      yamt 		dk->dk_bopenmask &= ~pmask;
    166        1.1     elric 		break;
    167        1.1     elric 	}
    168       1.16      yamt 	dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
    169        1.1     elric 
    170       1.30        ad 	mutex_exit(&dk->dk_openlock);
    171        1.1     elric 	return 0;
    172        1.1     elric }
    173        1.1     elric 
    174        1.1     elric void
    175        1.1     elric dk_strategy(struct dk_intf *di, struct dk_softc *dksc, struct buf *bp)
    176        1.1     elric {
    177        1.1     elric 	int	s;
    178        1.1     elric 	int	wlabel;
    179       1.18      yamt 	daddr_t	blkno;
    180        1.1     elric 
    181        1.1     elric 	DPRINTF_FOLLOW(("dk_strategy(%s, %p, %p)\n",
    182        1.1     elric 	    di->di_dkname, dksc, bp));
    183        1.1     elric 
    184        1.1     elric 	if (!(dksc->sc_flags & DKF_INITED)) {
    185       1.25       dan 		DPRINTF_FOLLOW(("dk_strategy: not inited\n"));
    186        1.1     elric 		bp->b_error  = ENXIO;
    187        1.2     elric 		biodone(bp);
    188        1.1     elric 		return;
    189        1.1     elric 	}
    190        1.1     elric 
    191        1.1     elric 	/* XXX look for some more errors, c.f. ld.c */
    192        1.1     elric 
    193        1.1     elric 	bp->b_resid = bp->b_bcount;
    194        1.1     elric 
    195        1.1     elric 	/* If there is nothing to do, then we are done */
    196        1.1     elric 	if (bp->b_bcount == 0) {
    197        1.1     elric 		biodone(bp);
    198        1.1     elric 		return;
    199        1.1     elric 	}
    200        1.1     elric 
    201        1.1     elric 	wlabel = dksc->sc_flags & (DKF_WLABEL|DKF_LABELLING);
    202        1.1     elric 	if (DISKPART(bp->b_dev) != RAW_PART &&
    203        1.6   thorpej 	    bounds_check_with_label(&dksc->sc_dkdev, bp, wlabel) <= 0) {
    204        1.1     elric 		biodone(bp);
    205        1.1     elric 		return;
    206        1.1     elric 	}
    207        1.1     elric 
    208       1.18      yamt 	blkno = bp->b_blkno;
    209       1.18      yamt 	if (DISKPART(bp->b_dev) != RAW_PART) {
    210       1.18      yamt 		struct partition *pp;
    211       1.18      yamt 
    212       1.18      yamt 		pp =
    213       1.18      yamt 		    &dksc->sc_dkdev.dk_label->d_partitions[DISKPART(bp->b_dev)];
    214       1.18      yamt 		blkno += pp->p_offset;
    215       1.18      yamt 	}
    216       1.18      yamt 	bp->b_rawblkno = blkno;
    217       1.18      yamt 
    218        1.1     elric 	/*
    219        1.1     elric 	 * Start the unit by calling the start routine
    220        1.1     elric 	 * provided by the individual driver.
    221        1.1     elric 	 */
    222        1.1     elric 	s = splbio();
    223       1.40      yamt 	bufq_put(dksc->sc_bufq, bp);
    224       1.11     elric 	dk_start(di, dksc);
    225        1.1     elric 	splx(s);
    226        1.1     elric 	return;
    227        1.1     elric }
    228        1.1     elric 
    229       1.11     elric void
    230       1.11     elric dk_start(struct dk_intf *di, struct dk_softc *dksc)
    231       1.11     elric {
    232       1.11     elric 	struct	buf *bp;
    233       1.11     elric 
    234       1.11     elric 	DPRINTF_FOLLOW(("dk_start(%s, %p)\n", di->di_dkname, dksc));
    235       1.11     elric 
    236       1.11     elric 	/* Process the work queue */
    237       1.40      yamt 	while ((bp = bufq_get(dksc->sc_bufq)) != NULL) {
    238       1.12   hannken 		if (di->di_diskstart(dksc, bp) != 0) {
    239       1.40      yamt 			bufq_put(dksc->sc_bufq, bp);
    240       1.11     elric 			break;
    241       1.12   hannken 		}
    242       1.11     elric 	}
    243       1.11     elric }
    244       1.11     elric 
    245       1.11     elric void
    246       1.11     elric dk_iodone(struct dk_intf *di, struct dk_softc *dksc)
    247       1.11     elric {
    248       1.11     elric 
    249       1.11     elric 	DPRINTF_FOLLOW(("dk_iodone(%s, %p)\n", di->di_dkname, dksc));
    250       1.11     elric 
    251       1.11     elric 	/* We kick the queue in case we are able to get more work done */
    252       1.11     elric 	dk_start(di, dksc);
    253       1.11     elric }
    254       1.11     elric 
    255        1.1     elric int
    256        1.1     elric dk_size(struct dk_intf *di, struct dk_softc *dksc, dev_t dev)
    257        1.1     elric {
    258        1.1     elric 	struct	disklabel *lp;
    259        1.1     elric 	int	is_open;
    260        1.1     elric 	int	part;
    261        1.1     elric 	int	size;
    262        1.1     elric 
    263        1.1     elric 	if ((dksc->sc_flags & DKF_INITED) == 0)
    264        1.1     elric 		return -1;
    265        1.1     elric 
    266        1.1     elric 	part = DISKPART(dev);
    267        1.1     elric 	is_open = dksc->sc_dkdev.dk_openmask & (1 << part);
    268        1.1     elric 
    269       1.19  christos 	if (!is_open && di->di_open(dev, 0, S_IFBLK, curlwp))
    270        1.1     elric 		return -1;
    271        1.1     elric 
    272        1.1     elric 	lp = dksc->sc_dkdev.dk_label;
    273        1.1     elric 	if (lp->d_partitions[part].p_fstype != FS_SWAP)
    274        1.1     elric 		size = -1;
    275        1.1     elric 	else
    276        1.1     elric 		size = lp->d_partitions[part].p_size *
    277        1.1     elric 		    (lp->d_secsize / DEV_BSIZE);
    278        1.1     elric 
    279       1.19  christos 	if (!is_open && di->di_close(dev, 0, S_IFBLK, curlwp))
    280        1.1     elric 		return 1;
    281        1.1     elric 
    282        1.1     elric 	return size;
    283        1.1     elric }
    284        1.1     elric 
    285        1.1     elric int
    286        1.1     elric dk_ioctl(struct dk_intf *di, struct dk_softc *dksc, dev_t dev,
    287       1.28  christos 	    u_long cmd, void *data, int flag, struct lwp *l)
    288        1.1     elric {
    289        1.1     elric 	struct	disklabel *lp;
    290       1.16      yamt 	struct	disk *dk;
    291        1.1     elric #ifdef __HAVE_OLD_DISKLABEL
    292        1.1     elric 	struct	disklabel newlabel;
    293        1.1     elric #endif
    294        1.1     elric 	int	error = 0;
    295        1.1     elric 
    296       1.39    cegger 	DPRINTF_FOLLOW(("dk_ioctl(%s, %p, 0x%"PRIx64", 0x%lx)\n",
    297        1.1     elric 	    di->di_dkname, dksc, dev, cmd));
    298        1.1     elric 
    299        1.1     elric 	/* ensure that the pseudo disk is open for writes for these commands */
    300        1.1     elric 	switch (cmd) {
    301        1.1     elric 	case DIOCSDINFO:
    302        1.1     elric 	case DIOCWDINFO:
    303        1.1     elric #ifdef __HAVE_OLD_DISKLABEL
    304        1.1     elric 	case ODIOCSDINFO:
    305        1.1     elric 	case ODIOCWDINFO:
    306        1.1     elric #endif
    307        1.1     elric 	case DIOCWLABEL:
    308  1.42.12.1       mrg 	case DIOCAWEDGE:
    309  1.42.12.1       mrg 	case DIOCDWEDGE:
    310        1.1     elric 		if ((flag & FWRITE) == 0)
    311        1.1     elric 			return EBADF;
    312        1.1     elric 	}
    313        1.1     elric 
    314        1.1     elric 	/* ensure that the pseudo-disk is initialized for these */
    315        1.1     elric 	switch (cmd) {
    316  1.42.12.1       mrg #ifdef DIOCGSECTORSIZE
    317  1.42.12.1       mrg 	case DIOCGSECTORSIZE:
    318  1.42.12.1       mrg 	case DIOCGMEDIASIZE:
    319  1.42.12.1       mrg #endif
    320        1.1     elric 	case DIOCGDINFO:
    321        1.1     elric 	case DIOCSDINFO:
    322        1.1     elric 	case DIOCWDINFO:
    323        1.1     elric 	case DIOCGPART:
    324        1.1     elric 	case DIOCWLABEL:
    325        1.1     elric 	case DIOCGDEFLABEL:
    326  1.42.12.1       mrg 	case DIOCAWEDGE:
    327  1.42.12.1       mrg 	case DIOCDWEDGE:
    328  1.42.12.1       mrg 	case DIOCLWEDGES:
    329  1.42.12.1       mrg 	case DIOCCACHESYNC:
    330        1.1     elric #ifdef __HAVE_OLD_DISKLABEL
    331        1.1     elric 	case ODIOCGDINFO:
    332        1.1     elric 	case ODIOCSDINFO:
    333        1.1     elric 	case ODIOCWDINFO:
    334        1.1     elric 	case ODIOCGDEFLABEL:
    335        1.1     elric #endif
    336        1.1     elric 		if ((dksc->sc_flags & DKF_INITED) == 0)
    337        1.1     elric 			return ENXIO;
    338        1.1     elric 	}
    339        1.1     elric 
    340        1.1     elric 	switch (cmd) {
    341  1.42.12.1       mrg #ifdef DIOCGSECTORSIZE
    342  1.42.12.1       mrg 	case DIOCGSECTORSIZE:
    343  1.42.12.1       mrg 		*(u_int *)data = dksc->sc_geom.pdg_secsize;
    344  1.42.12.1       mrg 		return 0;
    345  1.42.12.1       mrg 	case DIOCGMEDIASIZE:
    346  1.42.12.1       mrg 		*(off_t *)data =
    347  1.42.12.1       mrg 		    (off_t)dksc->sc_geom.pdg_secsize *
    348  1.42.12.1       mrg 		    dksc->sc_geom.pdg_nsectors;
    349  1.42.12.1       mrg 		return 0;
    350  1.42.12.1       mrg #endif
    351  1.42.12.1       mrg 
    352        1.1     elric 	case DIOCGDINFO:
    353        1.1     elric 		*(struct disklabel *)data = *(dksc->sc_dkdev.dk_label);
    354        1.1     elric 		break;
    355        1.1     elric 
    356        1.1     elric #ifdef __HAVE_OLD_DISKLABEL
    357        1.1     elric 	case ODIOCGDINFO:
    358        1.1     elric 		newlabel = *(dksc->sc_dkdev.dk_label);
    359        1.1     elric 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
    360        1.1     elric 			return ENOTTY;
    361        1.1     elric 		memcpy(data, &newlabel, sizeof (struct olddisklabel));
    362        1.1     elric 		break;
    363        1.1     elric #endif
    364        1.1     elric 
    365        1.1     elric 	case DIOCGPART:
    366        1.1     elric 		((struct partinfo *)data)->disklab = dksc->sc_dkdev.dk_label;
    367        1.1     elric 		((struct partinfo *)data)->part =
    368        1.1     elric 		    &dksc->sc_dkdev.dk_label->d_partitions[DISKPART(dev)];
    369        1.1     elric 		break;
    370        1.1     elric 
    371        1.1     elric 	case DIOCWDINFO:
    372        1.1     elric 	case DIOCSDINFO:
    373        1.1     elric #ifdef __HAVE_OLD_DISKLABEL
    374        1.1     elric 	case ODIOCWDINFO:
    375        1.1     elric 	case ODIOCSDINFO:
    376        1.1     elric #endif
    377        1.1     elric #ifdef __HAVE_OLD_DISKLABEL
    378        1.1     elric 		if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
    379        1.1     elric 			memset(&newlabel, 0, sizeof newlabel);
    380        1.1     elric 			memcpy(&newlabel, data, sizeof (struct olddisklabel));
    381        1.1     elric 			lp = &newlabel;
    382        1.1     elric 		} else
    383        1.1     elric #endif
    384        1.1     elric 		lp = (struct disklabel *)data;
    385        1.1     elric 
    386       1.16      yamt 		dk = &dksc->sc_dkdev;
    387       1.30        ad 		mutex_enter(&dk->dk_openlock);
    388        1.1     elric 		dksc->sc_flags |= DKF_LABELLING;
    389        1.1     elric 
    390        1.1     elric 		error = setdisklabel(dksc->sc_dkdev.dk_label,
    391        1.1     elric 		    lp, 0, dksc->sc_dkdev.dk_cpulabel);
    392        1.1     elric 		if (error == 0) {
    393        1.1     elric 			if (cmd == DIOCWDINFO
    394        1.1     elric #ifdef __HAVE_OLD_DISKLABEL
    395        1.1     elric 			    || cmd == ODIOCWDINFO
    396        1.1     elric #endif
    397        1.1     elric 			   )
    398        1.1     elric 				error = writedisklabel(DKLABELDEV(dev),
    399        1.1     elric 				    di->di_strategy, dksc->sc_dkdev.dk_label,
    400        1.1     elric 				    dksc->sc_dkdev.dk_cpulabel);
    401        1.1     elric 		}
    402        1.1     elric 
    403        1.1     elric 		dksc->sc_flags &= ~DKF_LABELLING;
    404       1.30        ad 		mutex_exit(&dk->dk_openlock);
    405        1.1     elric 		break;
    406        1.1     elric 
    407        1.1     elric 	case DIOCWLABEL:
    408        1.1     elric 		if (*(int *)data != 0)
    409        1.1     elric 			dksc->sc_flags |= DKF_WLABEL;
    410        1.1     elric 		else
    411        1.1     elric 			dksc->sc_flags &= ~DKF_WLABEL;
    412        1.1     elric 		break;
    413        1.1     elric 
    414        1.1     elric 	case DIOCGDEFLABEL:
    415        1.1     elric 		dk_getdefaultlabel(di, dksc, (struct disklabel *)data);
    416        1.1     elric 		break;
    417        1.1     elric 
    418        1.1     elric #ifdef __HAVE_OLD_DISKLABEL
    419        1.1     elric 	case ODIOCGDEFLABEL:
    420        1.1     elric 		dk_getdefaultlabel(di, dksc, &newlabel);
    421        1.1     elric 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
    422        1.1     elric 			return ENOTTY;
    423        1.1     elric 		memcpy(data, &newlabel, sizeof (struct olddisklabel));
    424        1.1     elric 		break;
    425        1.1     elric #endif
    426        1.1     elric 
    427       1.16      yamt 	case DIOCAWEDGE:
    428       1.16      yamt 	    {
    429       1.16      yamt 	    	struct dkwedge_info *dkw = (void *)data;
    430       1.16      yamt 
    431       1.16      yamt 		if ((flag & FWRITE) == 0)
    432       1.16      yamt 			return (EBADF);
    433       1.16      yamt 
    434       1.16      yamt 		/* If the ioctl happens here, the parent is us. */
    435       1.16      yamt 		strcpy(dkw->dkw_parent, dksc->sc_dkdev.dk_name);
    436       1.16      yamt 		return (dkwedge_add(dkw));
    437       1.16      yamt 	    }
    438       1.16      yamt 
    439       1.16      yamt 	case DIOCDWEDGE:
    440       1.16      yamt 	    {
    441       1.16      yamt 	    	struct dkwedge_info *dkw = (void *)data;
    442       1.16      yamt 
    443       1.16      yamt 		if ((flag & FWRITE) == 0)
    444       1.16      yamt 			return (EBADF);
    445       1.16      yamt 
    446       1.16      yamt 		/* If the ioctl happens here, the parent is us. */
    447       1.16      yamt 		strcpy(dkw->dkw_parent, dksc->sc_dkdev.dk_name);
    448       1.16      yamt 		return (dkwedge_del(dkw));
    449       1.16      yamt 	    }
    450       1.16      yamt 
    451       1.16      yamt 	case DIOCLWEDGES:
    452       1.16      yamt 	    {
    453       1.16      yamt 	    	struct dkwedge_list *dkwl = (void *)data;
    454       1.16      yamt 
    455       1.20    rpaulo 		return (dkwedge_list(&dksc->sc_dkdev, dkwl, l));
    456       1.16      yamt 	    }
    457       1.16      yamt 
    458       1.21      yamt 	case DIOCGSTRATEGY:
    459       1.21      yamt 	    {
    460       1.21      yamt 		struct disk_strategy *dks = (void *)data;
    461       1.21      yamt 		int s;
    462       1.21      yamt 
    463       1.21      yamt 		s = splbio();
    464       1.21      yamt 		strlcpy(dks->dks_name, bufq_getstrategyname(dksc->sc_bufq),
    465       1.21      yamt 		    sizeof(dks->dks_name));
    466       1.21      yamt 		splx(s);
    467       1.21      yamt 		dks->dks_paramlen = 0;
    468       1.21      yamt 
    469       1.21      yamt 		return 0;
    470       1.21      yamt 	    }
    471       1.21      yamt 
    472       1.21      yamt 	case DIOCSSTRATEGY:
    473       1.21      yamt 	    {
    474       1.21      yamt 		struct disk_strategy *dks = (void *)data;
    475       1.21      yamt 		struct bufq_state *new;
    476       1.21      yamt 		struct bufq_state *old;
    477       1.21      yamt 		int s;
    478       1.21      yamt 
    479       1.21      yamt 		if ((flag & FWRITE) == 0) {
    480       1.21      yamt 			return EBADF;
    481       1.21      yamt 		}
    482       1.21      yamt 		if (dks->dks_param != NULL) {
    483       1.21      yamt 			return EINVAL;
    484       1.21      yamt 		}
    485       1.21      yamt 		dks->dks_name[sizeof(dks->dks_name) - 1] = 0; /* ensure term */
    486       1.21      yamt 		error = bufq_alloc(&new, dks->dks_name,
    487       1.21      yamt 		    BUFQ_EXACT|BUFQ_SORT_RAWBLOCK);
    488       1.21      yamt 		if (error) {
    489       1.21      yamt 			return error;
    490       1.21      yamt 		}
    491       1.21      yamt 		s = splbio();
    492       1.21      yamt 		old = dksc->sc_bufq;
    493       1.21      yamt 		bufq_move(new, old);
    494       1.21      yamt 		dksc->sc_bufq = new;
    495       1.21      yamt 		splx(s);
    496       1.21      yamt 		bufq_free(old);
    497       1.21      yamt 
    498       1.21      yamt 		return 0;
    499       1.21      yamt 	    }
    500       1.21      yamt 
    501        1.1     elric 	default:
    502        1.1     elric 		error = ENOTTY;
    503        1.1     elric 	}
    504        1.1     elric 
    505        1.1     elric 	return error;
    506        1.1     elric }
    507        1.1     elric 
    508        1.1     elric /*
    509        1.1     elric  * dk_dump dumps all of physical memory into the partition specified.
    510        1.1     elric  * This requires substantially more framework than {s,w}ddump, and hence
    511        1.1     elric  * is probably much more fragile.
    512        1.1     elric  *
    513        1.1     elric  * XXX: we currently do not implement this.
    514        1.1     elric  */
    515        1.1     elric 
    516        1.1     elric #define DKF_READYFORDUMP	(DKF_INITED|DKF_TAKEDUMP)
    517        1.1     elric #define DKFF_READYFORDUMP(x)	(((x) & DKF_READYFORDUMP) == DKF_READYFORDUMP)
    518        1.1     elric static volatile int	dk_dumping = 0;
    519        1.1     elric 
    520        1.1     elric /* ARGSUSED */
    521        1.1     elric int
    522       1.27  christos dk_dump(struct dk_intf *di, struct dk_softc *dksc, dev_t dev,
    523       1.28  christos     daddr_t blkno, void *va, size_t size)
    524        1.1     elric {
    525        1.1     elric 
    526        1.1     elric 	/*
    527        1.1     elric 	 * ensure that we consider this device to be safe for dumping,
    528        1.1     elric 	 * and that the device is configured.
    529        1.1     elric 	 */
    530        1.1     elric 	if (!DKFF_READYFORDUMP(dksc->sc_flags))
    531        1.1     elric 		return ENXIO;
    532        1.1     elric 
    533        1.1     elric 	/* ensure that we are not already dumping */
    534        1.1     elric 	if (dk_dumping)
    535        1.1     elric 		return EFAULT;
    536        1.1     elric 	dk_dumping = 1;
    537        1.1     elric 
    538        1.1     elric 	/* XXX: unimplemented */
    539        1.1     elric 
    540        1.1     elric 	dk_dumping = 0;
    541        1.1     elric 
    542        1.1     elric 	/* XXX: actually for now, we are going to leave this alone */
    543        1.1     elric 	return ENXIO;
    544        1.1     elric }
    545        1.1     elric 
    546        1.1     elric /* ARGSUSED */
    547        1.1     elric void
    548        1.1     elric dk_getdefaultlabel(struct dk_intf *di, struct dk_softc *dksc,
    549        1.1     elric 		      struct disklabel *lp)
    550        1.1     elric {
    551        1.1     elric 	struct dk_geom *pdg = &dksc->sc_geom;
    552        1.4     elric 
    553        1.4     elric 	memset(lp, 0, sizeof(*lp));
    554        1.1     elric 
    555        1.1     elric 	lp->d_secperunit = dksc->sc_size;
    556        1.1     elric 	lp->d_secsize = pdg->pdg_secsize;
    557        1.1     elric 	lp->d_nsectors = pdg->pdg_nsectors;
    558        1.1     elric 	lp->d_ntracks = pdg->pdg_ntracks;
    559        1.1     elric 	lp->d_ncylinders = pdg->pdg_ncylinders;
    560        1.1     elric 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
    561        1.1     elric 
    562        1.1     elric 	strncpy(lp->d_typename, di->di_dkname, sizeof(lp->d_typename));
    563        1.1     elric 	lp->d_type = di->di_dtype;
    564        1.1     elric 	strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
    565        1.1     elric 	lp->d_rpm = 3600;
    566        1.1     elric 	lp->d_interleave = 1;
    567        1.1     elric 	lp->d_flags = 0;
    568        1.1     elric 
    569        1.1     elric 	lp->d_partitions[RAW_PART].p_offset = 0;
    570        1.1     elric 	lp->d_partitions[RAW_PART].p_size = dksc->sc_size;
    571        1.1     elric 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
    572        1.1     elric 	lp->d_npartitions = RAW_PART + 1;
    573        1.1     elric 
    574        1.1     elric 	lp->d_magic = DISKMAGIC;
    575        1.1     elric 	lp->d_magic2 = DISKMAGIC;
    576        1.1     elric 	lp->d_checksum = dkcksum(dksc->sc_dkdev.dk_label);
    577        1.1     elric }
    578        1.1     elric 
    579        1.1     elric /* ARGSUSED */
    580        1.1     elric void
    581        1.1     elric dk_getdisklabel(struct dk_intf *di, struct dk_softc *dksc, dev_t dev)
    582        1.1     elric {
    583        1.1     elric 	struct	 disklabel *lp = dksc->sc_dkdev.dk_label;
    584        1.1     elric 	struct	 cpu_disklabel *clp = dksc->sc_dkdev.dk_cpulabel;
    585        1.1     elric 	struct	 partition *pp;
    586        1.1     elric 	int	 i;
    587        1.5       dsl 	const char	*errstring;
    588        1.1     elric 
    589        1.1     elric 	memset(clp, 0x0, sizeof(*clp));
    590        1.1     elric 	dk_getdefaultlabel(di, dksc, lp);
    591        1.1     elric 	errstring = readdisklabel(DKLABELDEV(dev), di->di_strategy,
    592        1.1     elric 	    dksc->sc_dkdev.dk_label, dksc->sc_dkdev.dk_cpulabel);
    593        1.1     elric 	if (errstring) {
    594        1.1     elric 		dk_makedisklabel(di, dksc);
    595        1.1     elric 		if (dksc->sc_flags & DKF_WARNLABEL)
    596        1.1     elric 			printf("%s: %s\n", dksc->sc_xname, errstring);
    597        1.1     elric 		return;
    598        1.1     elric 	}
    599        1.1     elric 
    600        1.1     elric 	if ((dksc->sc_flags & DKF_LABELSANITY) == 0)
    601        1.1     elric 		return;
    602        1.1     elric 
    603        1.1     elric 	/* Sanity check */
    604        1.1     elric 	if (lp->d_secperunit != dksc->sc_size)
    605        1.1     elric 		printf("WARNING: %s: total sector size in disklabel (%d) "
    606        1.1     elric 		    "!= the size of %s (%lu)\n", dksc->sc_xname,
    607        1.1     elric 		    lp->d_secperunit, di->di_dkname, (u_long)dksc->sc_size);
    608        1.1     elric 
    609        1.1     elric 	for (i=0; i < lp->d_npartitions; i++) {
    610        1.1     elric 		pp = &lp->d_partitions[i];
    611        1.1     elric 		if (pp->p_offset + pp->p_size > dksc->sc_size)
    612        1.1     elric 			printf("WARNING: %s: end of partition `%c' exceeds "
    613        1.1     elric 			    "the size of %s (%lu)\n", dksc->sc_xname,
    614        1.1     elric 			    'a' + i, di->di_dkname, (u_long)dksc->sc_size);
    615        1.1     elric 	}
    616        1.1     elric }
    617        1.1     elric 
    618        1.1     elric /* ARGSUSED */
    619       1.13   thorpej static void
    620       1.27  christos dk_makedisklabel(struct dk_intf *di, struct dk_softc *dksc)
    621        1.1     elric {
    622        1.1     elric 	struct	disklabel *lp = dksc->sc_dkdev.dk_label;
    623        1.1     elric 
    624        1.1     elric 	lp->d_partitions[RAW_PART].p_fstype = FS_BSDFFS;
    625        1.1     elric 	strncpy(lp->d_packname, "default label", sizeof(lp->d_packname));
    626        1.1     elric 	lp->d_checksum = dkcksum(lp);
    627        1.1     elric }
    628        1.1     elric 
    629  1.42.12.1       mrg void
    630  1.42.12.1       mrg dk_set_properties(struct dk_intf *di, struct dk_softc *dksc)
    631  1.42.12.1       mrg {
    632  1.42.12.1       mrg 	prop_dictionary_t disk_info, odisk_info, geom;
    633  1.42.12.1       mrg 
    634  1.42.12.1       mrg 	disk_info = prop_dictionary_create();
    635  1.42.12.1       mrg 
    636  1.42.12.1       mrg 	geom = prop_dictionary_create();
    637  1.42.12.1       mrg 
    638  1.42.12.1       mrg 	prop_dictionary_set_uint64(geom, "sectors-per-unit", dksc->sc_size);
    639  1.42.12.1       mrg 
    640  1.42.12.1       mrg 	prop_dictionary_set_uint32(geom, "sector-size",
    641  1.42.12.1       mrg 	    dksc->sc_geom.pdg_secsize);
    642  1.42.12.1       mrg 
    643  1.42.12.1       mrg 	prop_dictionary_set_uint16(geom, "sectors-per-track",
    644  1.42.12.1       mrg 	    dksc->sc_geom.pdg_nsectors);
    645  1.42.12.1       mrg 
    646  1.42.12.1       mrg 	prop_dictionary_set_uint16(geom, "tracks-per-cylinder",
    647  1.42.12.1       mrg 	    dksc->sc_geom.pdg_ntracks);
    648  1.42.12.1       mrg 
    649  1.42.12.1       mrg 	prop_dictionary_set_uint64(geom, "cylinders-per-unit",
    650  1.42.12.1       mrg 	    dksc->sc_geom.pdg_ncylinders);
    651  1.42.12.1       mrg 
    652  1.42.12.1       mrg 	prop_dictionary_set(disk_info, "geometry", geom);
    653  1.42.12.1       mrg 	prop_object_release(geom);
    654  1.42.12.1       mrg 
    655  1.42.12.1       mrg 	prop_dictionary_set(device_properties(dksc->sc_dev),
    656  1.42.12.1       mrg 	    "disk-info", disk_info);
    657  1.42.12.1       mrg 
    658  1.42.12.1       mrg 	/*
    659  1.42.12.1       mrg 	 * Don't release disk_info here; we keep a reference to it.
    660  1.42.12.1       mrg 	 * disk_detach() will release it when we go away.
    661  1.42.12.1       mrg 	 */
    662  1.42.12.1       mrg 
    663  1.42.12.1       mrg 	odisk_info = dksc->sc_dkdev.dk_info;
    664  1.42.12.1       mrg 	dksc->sc_dkdev.dk_info = disk_info;
    665  1.42.12.1       mrg 	if (odisk_info)
    666  1.42.12.1       mrg 		prop_object_release(odisk_info);
    667  1.42.12.1       mrg }
    668  1.42.12.1       mrg 
    669        1.1     elric /* This function is taken from ccd.c:1.76  --rcd */
    670        1.1     elric 
    671        1.1     elric /*
    672        1.1     elric  * XXX this function looks too generic for dksubr.c, shouldn't we
    673        1.1     elric  *     put it somewhere better?
    674        1.1     elric  */
    675        1.1     elric 
    676        1.1     elric /*
    677        1.1     elric  * Lookup the provided name in the filesystem.  If the file exists,
    678        1.1     elric  * is a valid block device, and isn't being used by anyone else,
    679        1.1     elric  * set *vpp to the file's vnode.
    680        1.1     elric  */
    681        1.1     elric int
    682       1.42  dholland dk_lookup(struct pathbuf *pb, struct lwp *l, struct vnode **vpp)
    683        1.1     elric {
    684        1.1     elric 	struct nameidata nd;
    685        1.1     elric 	struct vnode *vp;
    686        1.1     elric 	struct vattr va;
    687       1.24  christos 	int     error;
    688        1.1     elric 
    689       1.24  christos 	if (l == NULL)
    690       1.24  christos 		return ESRCH;	/* Is ESRCH the best choice? */
    691       1.24  christos 
    692       1.42  dholland 	NDINIT(&nd, LOOKUP, FOLLOW, pb);
    693       1.24  christos 	if ((error = vn_open(&nd, FREAD | FWRITE, 0)) != 0) {
    694        1.1     elric 		DPRINTF((DKDB_FOLLOW|DKDB_INIT),
    695        1.1     elric 		    ("dk_lookup: vn_open error = %d\n", error));
    696       1.24  christos 		return error;
    697        1.1     elric 	}
    698       1.24  christos 
    699        1.1     elric 	vp = nd.ni_vp;
    700       1.32     pooka 	if ((error = VOP_GETATTR(vp, &va, l->l_cred)) != 0) {
    701        1.1     elric 		DPRINTF((DKDB_FOLLOW|DKDB_INIT),
    702        1.1     elric 		    ("dk_lookup: getattr error = %d\n", error));
    703       1.24  christos 		goto out;
    704        1.1     elric 	}
    705        1.1     elric 
    706        1.1     elric 	/* XXX: eventually we should handle VREG, too. */
    707        1.1     elric 	if (va.va_type != VBLK) {
    708       1.24  christos 		error = ENOTBLK;
    709       1.24  christos 		goto out;
    710       1.24  christos 	}
    711       1.24  christos 
    712        1.1     elric 	IFDEBUG(DKDB_VNODE, vprint("dk_lookup: vnode info", vp));
    713        1.1     elric 
    714       1.41   hannken 	VOP_UNLOCK(vp);
    715        1.1     elric 	*vpp = vp;
    716       1.24  christos 	return 0;
    717       1.24  christos out:
    718       1.41   hannken 	VOP_UNLOCK(vp);
    719       1.35        ad 	(void) vn_close(vp, FREAD | FWRITE, l->l_cred);
    720       1.24  christos 	return error;
    721        1.1     elric }
    722