Home | History | Annotate | Line # | Download | only in pud
pud_dev.c revision 1.4.24.1
      1  1.4.24.1   yamt /*	$NetBSD: pud_dev.c,v 1.4.24.1 2010/03/11 15:04:01 yamt Exp $	*/
      2       1.1  pooka 
      3       1.1  pooka /*
      4       1.1  pooka  * Copyright (c) 2007  Antti Kantee.  All Rights Reserved.
      5       1.1  pooka  *
      6       1.1  pooka  * Development of this software was supported by the
      7       1.1  pooka  * Research Foundation of Helsinki University of Technology
      8       1.1  pooka  *
      9       1.1  pooka  * Redistribution and use in source and binary forms, with or without
     10       1.1  pooka  * modification, are permitted provided that the following conditions
     11       1.1  pooka  * are met:
     12       1.1  pooka  * 1. Redistributions of source code must retain the above copyright
     13       1.1  pooka  *    notice, this list of conditions and the following disclaimer.
     14       1.1  pooka  * 2. Redistributions in binary form must reproduce the above copyright
     15       1.1  pooka  *    notice, this list of conditions and the following disclaimer in the
     16       1.1  pooka  *    documentation and/or other materials provided with the distribution.
     17       1.1  pooka  *
     18       1.1  pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     19       1.1  pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20       1.1  pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21       1.1  pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22       1.1  pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23       1.1  pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     24       1.1  pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25       1.1  pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26       1.1  pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27       1.1  pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28       1.1  pooka  * SUCH DAMAGE.
     29       1.1  pooka  */
     30       1.1  pooka 
     31       1.1  pooka #include <sys/cdefs.h>
     32  1.4.24.1   yamt __KERNEL_RCSID(0, "$NetBSD: pud_dev.c,v 1.4.24.1 2010/03/11 15:04:01 yamt Exp $");
     33       1.1  pooka 
     34       1.1  pooka #include <sys/param.h>
     35       1.1  pooka #include <sys/buf.h>
     36       1.1  pooka #include <sys/conf.h>
     37       1.1  pooka #include <sys/event.h>
     38       1.4  pooka #include <sys/ioccom.h>
     39       1.1  pooka #include <sys/kmem.h>
     40       1.1  pooka #include <sys/poll.h>
     41       1.1  pooka #include <sys/socketvar.h>
     42       1.1  pooka 
     43       1.1  pooka #include <dev/pud/pud_sys.h>
     44       1.1  pooka 
     45       1.4  pooka /*
     46       1.4  pooka  * b/c independent helpers
     47       1.4  pooka  */
     48       1.4  pooka 
     49       1.3  pooka static int
     50       1.4  pooka doopenclose(dev_t dev, int flags, int fmt, int class, int type)
     51       1.3  pooka {
     52       1.3  pooka 	struct pud_req_openclose pc_oc; /* XXX: stack = stupid */
     53       1.3  pooka 
     54       1.3  pooka 	pc_oc.pm_flags = flags;
     55       1.3  pooka 	pc_oc.pm_fmt = fmt;
     56       1.3  pooka 
     57       1.3  pooka 	return pud_request(dev, &pc_oc, sizeof(pc_oc), class, type);
     58       1.3  pooka }
     59       1.3  pooka 
     60  1.4.24.1   yamt #include <sys/disklabel.h>
     61  1.4.24.1   yamt /*
     62  1.4.24.1   yamt  * XXX: this is not "reentrant".  But then again, partinfo isn't
     63  1.4.24.1   yamt  * exactly safe in any case.
     64  1.4.24.1   yamt  */
     65  1.4.24.1   yamt static struct disklabel dl_partinfo;
     66  1.4.24.1   yamt 
     67       1.4  pooka static int
     68       1.4  pooka doioctl(dev_t dev, u_long cmd, void *data, int flag, int class, int type)
     69       1.4  pooka {
     70       1.4  pooka 	struct pud_req_ioctl *pc_ioctl;
     71       1.4  pooka 	size_t dlen, allocsize;
     72  1.4.24.1   yamt 	u_long origcmd = cmd;
     73  1.4.24.1   yamt 	void *origdata = NULL; /* XXXgcc */
     74       1.4  pooka 	int error;
     75       1.4  pooka 
     76  1.4.24.1   yamt 	/*
     77  1.4.24.1   yamt 	 * XXX: kludge.  This is a horrible abstraction violation, but
     78  1.4.24.1   yamt 	 * then again DIOCGPART is a horrible ioctl (even more horrible
     79  1.4.24.1   yamt 	 * than the generic ioctl).  We handle it specially here since
     80  1.4.24.1   yamt 	 * the server in userspace has no chance to handle it.  And it's
     81  1.4.24.1   yamt 	 * a common operation used by most file systems.  But really, it
     82  1.4.24.1   yamt 	 * should be replaced by something a bit more ... transactional.
     83  1.4.24.1   yamt 	 */
     84  1.4.24.1   yamt 	if (cmd == DIOCGPART) {
     85  1.4.24.1   yamt 		cmd = DIOCGDINFO;
     86  1.4.24.1   yamt 		origdata = data;
     87  1.4.24.1   yamt 		flag = 0;
     88  1.4.24.1   yamt 		data = &dl_partinfo;
     89  1.4.24.1   yamt 	}
     90  1.4.24.1   yamt 
     91       1.4  pooka 	dlen = IOCPARM_LEN(cmd);
     92       1.4  pooka 	allocsize = sizeof(struct pud_req_ioctl) + dlen;
     93       1.4  pooka 	pc_ioctl = kmem_zalloc(allocsize, KM_SLEEP);
     94       1.4  pooka 
     95       1.4  pooka 	pc_ioctl->pm_iocmd = cmd;
     96       1.4  pooka 	pc_ioctl->pm_flag = flag;
     97       1.4  pooka 
     98       1.4  pooka 	if (cmd & IOC_IN)
     99       1.4  pooka 		memcpy(pc_ioctl->pm_data, data, dlen);
    100       1.4  pooka 	error = pud_request(dev, pc_ioctl, allocsize, class, type);
    101       1.4  pooka 	if (error)
    102       1.4  pooka 		goto out;
    103       1.4  pooka 	if (cmd & IOC_OUT)
    104       1.4  pooka 		memcpy(data, pc_ioctl->pm_data, dlen);
    105       1.4  pooka 
    106  1.4.24.1   yamt 	/*
    107  1.4.24.1   yamt 	 * In case doing the infamous DIOCGPART, issue the real
    108  1.4.24.1   yamt 	 * ioctl and do pointer arithmetic to figure out the right
    109  1.4.24.1   yamt 	 * partition.  We could use DISKPART() too, but this seems
    110  1.4.24.1   yamt 	 * "better".
    111  1.4.24.1   yamt 	 */
    112  1.4.24.1   yamt 	if (origcmd == DIOCGPART) {
    113  1.4.24.1   yamt 		struct partinfo *pi, *pi_user;
    114  1.4.24.1   yamt 		int labidx;
    115  1.4.24.1   yamt 
    116  1.4.24.1   yamt 		CTASSERT(sizeof(struct partinfo) <= sizeof(struct disklabel));
    117  1.4.24.1   yamt 
    118  1.4.24.1   yamt 		pc_ioctl->pm_iocmd = DIOCGPART;
    119  1.4.24.1   yamt 		pc_ioctl->pm_flag = 0;
    120  1.4.24.1   yamt 
    121  1.4.24.1   yamt 		error = pud_request(dev, pc_ioctl, allocsize, class, type);
    122  1.4.24.1   yamt 		if (error)
    123  1.4.24.1   yamt 			goto out;
    124  1.4.24.1   yamt 
    125  1.4.24.1   yamt 		pi_user = (struct partinfo *)pc_ioctl->pm_data;
    126  1.4.24.1   yamt 		labidx = pi_user->part - &pi_user->disklab->d_partitions[0];
    127  1.4.24.1   yamt 		/* userspace error, but punish caller, since we have no infra */
    128  1.4.24.1   yamt 		if (labidx >= MAXPARTITIONS) {
    129  1.4.24.1   yamt 			error = E2BIG;
    130  1.4.24.1   yamt 			goto out;
    131  1.4.24.1   yamt 		}
    132  1.4.24.1   yamt 
    133  1.4.24.1   yamt 		pi = origdata;
    134  1.4.24.1   yamt 		pi->disklab = &dl_partinfo;
    135  1.4.24.1   yamt 		pi->part = &dl_partinfo.d_partitions[labidx];
    136  1.4.24.1   yamt 
    137  1.4.24.1   yamt 	}
    138  1.4.24.1   yamt 
    139       1.4  pooka  out:
    140       1.4  pooka 	kmem_free(pc_ioctl, allocsize);
    141       1.4  pooka 	return error;
    142       1.4  pooka }
    143       1.4  pooka 
    144       1.1  pooka /*
    145       1.1  pooka  * Block de-vices
    146       1.1  pooka  */
    147       1.1  pooka 
    148       1.1  pooka static dev_type_open(pud_bdev_open);
    149       1.1  pooka static dev_type_close(pud_bdev_close);
    150       1.1  pooka static dev_type_strategy(pud_bdev_strategy);
    151       1.1  pooka static dev_type_ioctl(pud_bdev_ioctl);
    152       1.1  pooka #if 0
    153       1.1  pooka static dev_type_dump(pud_bdev_dump);
    154       1.1  pooka static dev_type_size(pud_bdev_size);
    155       1.1  pooka #endif
    156       1.1  pooka 
    157       1.1  pooka struct bdevsw pud_bdevsw = {
    158       1.1  pooka 	.d_open		= pud_bdev_open,
    159       1.1  pooka 	.d_close	= pud_bdev_close,
    160       1.1  pooka 	.d_strategy	= pud_bdev_strategy,
    161       1.1  pooka 	.d_ioctl	= pud_bdev_ioctl,
    162       1.1  pooka #if 0
    163       1.1  pooka 	.d_dump		= pud_bdev_dump,
    164       1.1  pooka 	.d_psize	= pud_bdev_size,
    165       1.1  pooka #endif
    166       1.1  pooka };
    167       1.1  pooka 
    168       1.1  pooka static int
    169       1.1  pooka pud_bdev_open(dev_t dev, int flags, int fmt, lwp_t *l)
    170       1.1  pooka {
    171       1.1  pooka 
    172       1.4  pooka 	return doopenclose(dev, flags, fmt, PUD_REQ_BDEV, PUD_BDEV_OPEN);
    173       1.1  pooka }
    174       1.1  pooka 
    175       1.1  pooka static int
    176       1.1  pooka pud_bdev_close(dev_t dev, int flags, int fmt, lwp_t *l)
    177       1.1  pooka {
    178       1.1  pooka 
    179       1.4  pooka 	return doopenclose(dev, flags, fmt, PUD_REQ_BDEV, PUD_BDEV_CLOSE);
    180       1.1  pooka }
    181       1.1  pooka 
    182       1.1  pooka static void
    183       1.1  pooka pud_bdev_strategy(struct buf *bp)
    184       1.1  pooka {
    185       1.3  pooka 	struct pud_req_readwrite *pc_rw;
    186       1.3  pooka 	size_t allocsize;
    187       1.3  pooka 	int error;
    188       1.3  pooka 
    189       1.3  pooka 	allocsize = sizeof(struct pud_req_readwrite) + bp->b_bcount;
    190       1.3  pooka 	pc_rw = kmem_zalloc(allocsize, KM_SLEEP);
    191       1.3  pooka 
    192       1.3  pooka 	pc_rw->pm_offset = bp->b_blkno << DEV_BSHIFT;
    193       1.3  pooka 	pc_rw->pm_resid = bp->b_bcount;
    194       1.3  pooka 
    195       1.3  pooka 	if (BUF_ISWRITE(bp))
    196       1.3  pooka 		memcpy(pc_rw->pm_data, bp->b_data, bp->b_bcount);
    197       1.1  pooka 
    198       1.3  pooka 	error = pud_request(bp->b_dev, pc_rw, allocsize, PUD_REQ_BDEV,
    199       1.3  pooka 	    BUF_ISREAD(bp) ? PUD_BDEV_STRATREAD : PUD_BDEV_STRATWRITE);
    200       1.3  pooka 	if (error)
    201       1.3  pooka 		goto out;
    202       1.3  pooka 
    203       1.3  pooka 	if (pc_rw->pm_resid > bp->b_bcount) {
    204       1.3  pooka 		error = EINVAL;
    205       1.3  pooka 		goto out;
    206       1.3  pooka 	}
    207       1.3  pooka 
    208       1.3  pooka 	if (BUF_ISREAD(bp))
    209       1.3  pooka 		memcpy(bp->b_data,pc_rw->pm_data,bp->b_bcount-pc_rw->pm_resid);
    210       1.3  pooka 
    211       1.3  pooka 	bp->b_resid = pc_rw->pm_resid;
    212       1.3  pooka 
    213       1.3  pooka  out:
    214       1.3  pooka 	kmem_free(pc_rw, allocsize);
    215       1.3  pooka 	bp->b_error = error;
    216       1.1  pooka 	biodone(bp);
    217       1.1  pooka }
    218       1.1  pooka 
    219       1.1  pooka int
    220       1.4  pooka pud_bdev_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
    221       1.1  pooka {
    222       1.1  pooka 
    223       1.4  pooka 	return doioctl(dev, cmd, data, flag, PUD_REQ_BDEV, PUD_BDEV_IOCTL);
    224       1.1  pooka }
    225       1.1  pooka 
    226       1.1  pooka /* hnmmm */
    227       1.1  pooka #if 0
    228       1.1  pooka int
    229       1.1  pooka pud_bdev_dump(dev_t dev, daddr_t addr, void *data, size_t sz)
    230       1.1  pooka {
    231       1.1  pooka 
    232       1.1  pooka 	return EOPNOTSUPP;
    233       1.1  pooka }
    234       1.1  pooka 
    235       1.1  pooka int
    236       1.1  pooka pud_bdev_size(dev_t dev)
    237       1.1  pooka {
    238       1.1  pooka 
    239       1.1  pooka 	return 0;
    240       1.1  pooka }
    241       1.1  pooka #endif
    242       1.1  pooka 
    243       1.1  pooka /*
    244       1.1  pooka  * Charrr devices
    245       1.1  pooka  */
    246       1.1  pooka 
    247       1.1  pooka static dev_type_open(pud_cdev_open);
    248       1.1  pooka static dev_type_close(pud_cdev_close);
    249       1.1  pooka static dev_type_read(pud_cdev_read);
    250       1.1  pooka static dev_type_write(pud_cdev_write);
    251       1.1  pooka static dev_type_ioctl(pud_cdev_ioctl);
    252       1.1  pooka static dev_type_poll(pud_cdev_poll);
    253       1.1  pooka static dev_type_mmap(pud_cdev_mmap);
    254       1.1  pooka static dev_type_kqfilter(pud_cdev_kqfilter);
    255       1.1  pooka 
    256       1.1  pooka struct cdevsw pud_cdevsw = {
    257       1.1  pooka 	.d_open		= pud_cdev_open,
    258       1.1  pooka 	.d_close	= pud_cdev_close,
    259       1.1  pooka 	.d_read		= pud_cdev_read,
    260       1.1  pooka 	.d_write	= pud_cdev_write,
    261       1.1  pooka 	.d_ioctl	= pud_cdev_ioctl,
    262       1.1  pooka #if 0
    263       1.1  pooka 	.d_stop		= pud_cdev_stop,
    264       1.1  pooka 	.d_tty		= pud_cdev_tty,
    265       1.1  pooka #endif
    266       1.1  pooka 	.d_poll		= pud_cdev_poll,
    267       1.1  pooka 	.d_mmap		= pud_cdev_mmap,
    268       1.1  pooka 	.d_kqfilter	= pud_cdev_kqfilter,
    269       1.1  pooka 	.d_flag		= D_OTHER,
    270       1.1  pooka };
    271       1.1  pooka 
    272       1.1  pooka static int
    273       1.1  pooka pud_cdev_open(dev_t dev, int flags, int fmt, lwp_t *l)
    274       1.1  pooka {
    275       1.1  pooka 
    276       1.4  pooka 	return doopenclose(dev, flags, fmt, PUD_REQ_CDEV, PUD_CDEV_OPEN);
    277       1.1  pooka }
    278       1.1  pooka 
    279       1.1  pooka static int
    280       1.1  pooka pud_cdev_close(dev_t dev, int flags, int fmt, lwp_t *l)
    281       1.1  pooka {
    282       1.1  pooka 
    283  1.4.24.1   yamt 	return doopenclose(dev, flags, fmt, PUD_REQ_CDEV, PUD_CDEV_CLOSE);
    284       1.1  pooka }
    285       1.1  pooka 
    286       1.1  pooka static int
    287       1.1  pooka pud_cdev_read(dev_t dev, struct uio *uio, int flag)
    288       1.1  pooka {
    289       1.1  pooka 	struct pud_creq_read *pc_read;
    290       1.1  pooka 	size_t allocsize;
    291       1.1  pooka 	int error;
    292       1.1  pooka 
    293       1.1  pooka 	allocsize = sizeof(struct pud_creq_read) + uio->uio_resid;
    294       1.1  pooka 	pc_read = kmem_zalloc(allocsize, KM_SLEEP);
    295       1.1  pooka 
    296       1.1  pooka 	pc_read->pm_offset = uio->uio_offset;
    297       1.1  pooka 	pc_read->pm_resid = uio->uio_resid;
    298       1.1  pooka 
    299       1.1  pooka 	error = pud_request(dev, pc_read, allocsize,
    300       1.1  pooka 	    PUD_REQ_CDEV, PUD_CDEV_READ);
    301       1.1  pooka 	if (error)
    302       1.1  pooka 		goto out;
    303       1.1  pooka 
    304       1.1  pooka 	if (pc_read->pm_resid > uio->uio_resid) {
    305       1.1  pooka 		error = EINVAL;
    306       1.1  pooka 		goto out;
    307       1.1  pooka 	}
    308       1.1  pooka 
    309       1.3  pooka 	error = uiomove(pc_read->pm_data,
    310       1.3  pooka 	    uio->uio_resid - pc_read->pm_resid, uio);
    311       1.1  pooka 
    312       1.1  pooka  out:
    313       1.1  pooka 	kmem_free(pc_read, allocsize);
    314       1.1  pooka 	return error;
    315       1.1  pooka }
    316       1.1  pooka 
    317       1.1  pooka static int
    318       1.1  pooka pud_cdev_write(dev_t dev, struct uio *uio, int flag)
    319       1.1  pooka {
    320       1.1  pooka 	struct pud_creq_write *pc_write;
    321       1.1  pooka 	size_t allocsize;
    322       1.1  pooka 	int error;
    323       1.1  pooka 
    324       1.1  pooka 	allocsize = sizeof(struct pud_creq_write) + uio->uio_resid;
    325       1.1  pooka 	pc_write = kmem_zalloc(allocsize, KM_SLEEP);
    326       1.1  pooka 
    327       1.1  pooka 	pc_write->pm_offset = uio->uio_offset;
    328       1.1  pooka 	pc_write->pm_resid = uio->uio_resid;
    329       1.1  pooka 
    330       1.1  pooka 	error = uiomove(pc_write->pm_data, uio->uio_resid, uio);
    331       1.1  pooka 	if (error)
    332       1.1  pooka 		goto out;
    333       1.1  pooka 
    334       1.1  pooka 	error = pud_request(dev, pc_write, allocsize,
    335       1.1  pooka 	    PUD_REQ_CDEV, PUD_CDEV_WRITE);
    336       1.1  pooka 	if (error)
    337       1.1  pooka 		goto out;
    338       1.1  pooka 
    339       1.1  pooka 	if (pc_write->pm_resid)
    340       1.1  pooka 		error = EIO;
    341       1.1  pooka 
    342       1.1  pooka  out:
    343       1.1  pooka 	kmem_free(pc_write, allocsize);
    344       1.1  pooka 	return error;
    345       1.1  pooka }
    346       1.1  pooka 
    347       1.1  pooka static int
    348       1.1  pooka pud_cdev_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
    349       1.1  pooka {
    350       1.1  pooka 
    351       1.4  pooka 	return doioctl(dev, cmd, data, flag, PUD_REQ_CDEV, PUD_CDEV_IOCTL);
    352       1.1  pooka }
    353       1.1  pooka 
    354       1.2  pooka static paddr_t
    355       1.2  pooka pud_cdev_mmap(dev_t dev, off_t off, int flag)
    356       1.1  pooka {
    357       1.1  pooka 
    358       1.2  pooka 	return (paddr_t)-1;
    359       1.1  pooka }
    360       1.1  pooka 
    361       1.2  pooka static int
    362       1.2  pooka pud_cdev_poll(dev_t dev, int flag, lwp_t *l)
    363       1.1  pooka {
    364       1.1  pooka 
    365       1.1  pooka 	return EOPNOTSUPP;
    366       1.1  pooka }
    367       1.1  pooka 
    368       1.1  pooka static int
    369       1.1  pooka pud_cdev_kqfilter(dev_t dev, struct knote *kn)
    370       1.1  pooka {
    371       1.1  pooka 
    372       1.1  pooka 	return EOPNOTSUPP;
    373       1.1  pooka }
    374