Home | History | Annotate | Line # | Download | only in dm
device-mapper.c revision 1.6.2.6
      1  1.6.2.6  yamt /*        $NetBSD: device-mapper.c,v 1.6.2.6 2010/08/11 22:53:20 yamt Exp $ */
      2  1.6.2.2  yamt 
      3  1.6.2.2  yamt /*
      4  1.6.2.5  yamt  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5  1.6.2.2  yamt  * All rights reserved.
      6  1.6.2.2  yamt  *
      7  1.6.2.2  yamt  * This code is derived from software contributed to The NetBSD Foundation
      8  1.6.2.2  yamt  * by Adam Hamsik.
      9  1.6.2.2  yamt  *
     10  1.6.2.2  yamt  * Redistribution and use in source and binary forms, with or without
     11  1.6.2.2  yamt  * modification, are permitted provided that the following conditions
     12  1.6.2.2  yamt  * are met:
     13  1.6.2.2  yamt  * 1. Redistributions of source code must retain the above copyright
     14  1.6.2.2  yamt  *    notice, this list of conditions and the following disclaimer.
     15  1.6.2.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.6.2.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     17  1.6.2.2  yamt  *    documentation and/or other materials provided with the distribution.
     18  1.6.2.2  yamt  *
     19  1.6.2.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.6.2.2  yamt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.6.2.2  yamt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.6.2.2  yamt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.6.2.2  yamt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.6.2.2  yamt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.6.2.2  yamt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.6.2.2  yamt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.6.2.2  yamt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.6.2.2  yamt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.6.2.2  yamt  * POSSIBILITY OF SUCH DAMAGE.
     30  1.6.2.2  yamt  */
     31  1.6.2.2  yamt 
     32  1.6.2.2  yamt /*
     33  1.6.2.2  yamt  * I want to say thank you to all people who helped me with this project.
     34  1.6.2.2  yamt  */
     35  1.6.2.2  yamt 
     36  1.6.2.2  yamt #include <sys/types.h>
     37  1.6.2.2  yamt #include <sys/param.h>
     38  1.6.2.2  yamt 
     39  1.6.2.2  yamt #include <sys/buf.h>
     40  1.6.2.2  yamt #include <sys/conf.h>
     41  1.6.2.5  yamt #include <sys/device.h>
     42  1.6.2.2  yamt #include <sys/dkio.h>
     43  1.6.2.2  yamt #include <sys/disk.h>
     44  1.6.2.2  yamt #include <sys/disklabel.h>
     45  1.6.2.2  yamt #include <sys/ioctl.h>
     46  1.6.2.2  yamt #include <sys/ioccom.h>
     47  1.6.2.2  yamt #include <sys/kmem.h>
     48  1.6.2.2  yamt 
     49  1.6.2.2  yamt #include "netbsd-dm.h"
     50  1.6.2.2  yamt #include "dm.h"
     51  1.6.2.2  yamt 
     52  1.6.2.2  yamt static dev_type_open(dmopen);
     53  1.6.2.2  yamt static dev_type_close(dmclose);
     54  1.6.2.2  yamt static dev_type_read(dmread);
     55  1.6.2.2  yamt static dev_type_write(dmwrite);
     56  1.6.2.2  yamt static dev_type_ioctl(dmioctl);
     57  1.6.2.2  yamt static dev_type_strategy(dmstrategy);
     58  1.6.2.2  yamt static dev_type_size(dmsize);
     59  1.6.2.2  yamt 
     60  1.6.2.2  yamt /* attach and detach routines */
     61  1.6.2.5  yamt void dmattach(int);
     62  1.6.2.6  yamt #ifdef _MODULE
     63  1.6.2.6  yamt static int dmdestroy(void);
     64  1.6.2.6  yamt #endif
     65  1.6.2.2  yamt 
     66  1.6.2.6  yamt static void dm_doinit(void);
     67  1.6.2.5  yamt 
     68  1.6.2.2  yamt static int dm_cmd_to_fun(prop_dictionary_t);
     69  1.6.2.2  yamt static int disk_ioctl_switch(dev_t, u_long, void *);
     70  1.6.2.2  yamt static int dm_ioctl_switch(u_long);
     71  1.6.2.2  yamt static void dmminphys(struct buf *);
     72  1.6.2.2  yamt 
     73  1.6.2.5  yamt /* CF attach/detach functions used for power management */
     74  1.6.2.5  yamt static int dm_detach(device_t, int);
     75  1.6.2.5  yamt static void dm_attach(device_t, device_t, void *);
     76  1.6.2.5  yamt static int dm_match(device_t, cfdata_t, void *);
     77  1.6.2.5  yamt 
     78  1.6.2.2  yamt /* ***Variable-definitions*** */
     79  1.6.2.2  yamt const struct bdevsw dm_bdevsw = {
     80  1.6.2.2  yamt 	.d_open = dmopen,
     81  1.6.2.2  yamt 	.d_close = dmclose,
     82  1.6.2.2  yamt 	.d_strategy = dmstrategy,
     83  1.6.2.2  yamt 	.d_ioctl = dmioctl,
     84  1.6.2.2  yamt 	.d_dump = nodump,
     85  1.6.2.2  yamt 	.d_psize = dmsize,
     86  1.6.2.2  yamt 	.d_flag = D_DISK | D_MPSAFE
     87  1.6.2.2  yamt };
     88  1.6.2.2  yamt 
     89  1.6.2.2  yamt const struct cdevsw dm_cdevsw = {
     90  1.6.2.2  yamt 	.d_open = dmopen,
     91  1.6.2.2  yamt 	.d_close = dmclose,
     92  1.6.2.2  yamt 	.d_read = dmread,
     93  1.6.2.2  yamt 	.d_write = dmwrite,
     94  1.6.2.2  yamt 	.d_ioctl = dmioctl,
     95  1.6.2.2  yamt 	.d_stop = nostop,
     96  1.6.2.2  yamt 	.d_tty = notty,
     97  1.6.2.2  yamt 	.d_poll = nopoll,
     98  1.6.2.2  yamt 	.d_mmap = nommap,
     99  1.6.2.2  yamt 	.d_kqfilter = nokqfilter,
    100  1.6.2.2  yamt 	.d_flag = D_DISK | D_MPSAFE
    101  1.6.2.2  yamt };
    102  1.6.2.2  yamt 
    103  1.6.2.2  yamt const struct dkdriver dmdkdriver = {
    104  1.6.2.2  yamt 	.d_strategy = dmstrategy
    105  1.6.2.2  yamt };
    106  1.6.2.2  yamt 
    107  1.6.2.5  yamt CFATTACH_DECL3_NEW(dm, 0,
    108  1.6.2.5  yamt      dm_match, dm_attach, dm_detach, NULL, NULL, NULL,
    109  1.6.2.5  yamt      DVF_DETACH_SHUTDOWN);
    110  1.6.2.5  yamt 
    111  1.6.2.5  yamt extern struct cfdriver dm_cd;
    112  1.6.2.5  yamt 
    113  1.6.2.5  yamt extern uint64_t dm_dev_counter;
    114  1.6.2.2  yamt 
    115  1.6.2.2  yamt /*
    116  1.6.2.2  yamt  * This array is used to translate cmd to function pointer.
    117  1.6.2.2  yamt  *
    118  1.6.2.2  yamt  * Interface between libdevmapper and lvm2tools uses different
    119  1.6.2.2  yamt  * names for one IOCTL call because libdevmapper do another thing
    120  1.6.2.2  yamt  * then. When I run "info" or "mknodes" libdevmapper will send same
    121  1.6.2.2  yamt  * ioctl to kernel but will do another things in userspace.
    122  1.6.2.2  yamt  *
    123  1.6.2.2  yamt  */
    124  1.6.2.2  yamt struct cmd_function cmd_fn[] = {
    125  1.6.2.2  yamt 		{ .cmd = "version", .fn = dm_get_version_ioctl},
    126  1.6.2.2  yamt 		{ .cmd = "targets", .fn = dm_list_versions_ioctl},
    127  1.6.2.2  yamt 		{ .cmd = "create",  .fn = dm_dev_create_ioctl},
    128  1.6.2.2  yamt 		{ .cmd = "info",    .fn = dm_dev_status_ioctl},
    129  1.6.2.2  yamt 		{ .cmd = "mknodes", .fn = dm_dev_status_ioctl},
    130  1.6.2.2  yamt 		{ .cmd = "names",   .fn = dm_dev_list_ioctl},
    131  1.6.2.2  yamt 		{ .cmd = "suspend", .fn = dm_dev_suspend_ioctl},
    132  1.6.2.2  yamt 		{ .cmd = "remove",  .fn = dm_dev_remove_ioctl},
    133  1.6.2.2  yamt 		{ .cmd = "rename",  .fn = dm_dev_rename_ioctl},
    134  1.6.2.2  yamt 		{ .cmd = "resume",  .fn = dm_dev_resume_ioctl},
    135  1.6.2.2  yamt 		{ .cmd = "clear",   .fn = dm_table_clear_ioctl},
    136  1.6.2.2  yamt 		{ .cmd = "deps",    .fn = dm_table_deps_ioctl},
    137  1.6.2.2  yamt 		{ .cmd = "reload",  .fn = dm_table_load_ioctl},
    138  1.6.2.2  yamt 		{ .cmd = "status",  .fn = dm_table_status_ioctl},
    139  1.6.2.2  yamt 		{ .cmd = "table",   .fn = dm_table_status_ioctl},
    140  1.6.2.2  yamt 		{NULL, NULL}
    141  1.6.2.2  yamt };
    142  1.6.2.2  yamt 
    143  1.6.2.6  yamt #ifdef _MODULE
    144  1.6.2.6  yamt #include <sys/module.h>
    145  1.6.2.6  yamt 
    146  1.6.2.6  yamt /* Autoconf defines */
    147  1.6.2.6  yamt CFDRIVER_DECL(dm, DV_DISK, NULL);
    148  1.6.2.6  yamt 
    149  1.6.2.2  yamt MODULE(MODULE_CLASS_DRIVER, dm, NULL);
    150  1.6.2.2  yamt 
    151  1.6.2.2  yamt /* New module handle routine */
    152  1.6.2.2  yamt static int
    153  1.6.2.2  yamt dm_modcmd(modcmd_t cmd, void *arg)
    154  1.6.2.2  yamt {
    155  1.6.2.6  yamt #ifdef _MODULE
    156  1.6.2.5  yamt 	int error, bmajor, cmajor;
    157  1.6.2.5  yamt 
    158  1.6.2.5  yamt 	error = 0;
    159  1.6.2.5  yamt 	bmajor = -1;
    160  1.6.2.5  yamt 	cmajor = -1;
    161  1.6.2.2  yamt 
    162  1.6.2.2  yamt 	switch (cmd) {
    163  1.6.2.2  yamt 	case MODULE_CMD_INIT:
    164  1.6.2.5  yamt 		error = config_cfdriver_attach(&dm_cd);
    165  1.6.2.5  yamt 		if (error)
    166  1.6.2.5  yamt 			break;
    167  1.6.2.5  yamt 
    168  1.6.2.6  yamt 		error = config_cfattach_attach(dm_cd.cd_name, &dm_ca);
    169  1.6.2.5  yamt 		if (error) {
    170  1.6.2.6  yamt 			aprint_error("%s: unable to register cfattach\n",
    171  1.6.2.6  yamt 			    dm_cd.cd_name);
    172  1.6.2.6  yamt 			return error;
    173  1.6.2.5  yamt 		}
    174  1.6.2.5  yamt 
    175  1.6.2.5  yamt 		error = devsw_attach(dm_cd.cd_name, &dm_bdevsw, &bmajor,
    176  1.6.2.2  yamt 		    &dm_cdevsw, &cmajor);
    177  1.6.2.5  yamt 		if (error) {
    178  1.6.2.5  yamt 			config_cfattach_detach(dm_cd.cd_name, &dm_ca);
    179  1.6.2.5  yamt 			config_cfdriver_detach(&dm_cd);
    180  1.6.2.5  yamt 			break;
    181  1.6.2.5  yamt 		}
    182  1.6.2.6  yamt 
    183  1.6.2.6  yamt 		dm_doinit();
    184  1.6.2.6  yamt 
    185  1.6.2.2  yamt 		break;
    186  1.6.2.2  yamt 
    187  1.6.2.2  yamt 	case MODULE_CMD_FINI:
    188  1.6.2.2  yamt 		/*
    189  1.6.2.2  yamt 		 * Disable unloading of dm module if there are any devices
    190  1.6.2.2  yamt 		 * defined in driver. This is probably too strong we need
    191  1.6.2.2  yamt 		 * to disable auto-unload only if there is mounted dm device
    192  1.6.2.2  yamt 		 * present.
    193  1.6.2.2  yamt 		 */
    194  1.6.2.5  yamt 		if (dm_dev_counter > 0)
    195  1.6.2.2  yamt 			return EBUSY;
    196  1.6.2.5  yamt 
    197  1.6.2.5  yamt 		error = dmdestroy();
    198  1.6.2.5  yamt 		if (error)
    199  1.6.2.5  yamt 			break;
    200  1.6.2.5  yamt 
    201  1.6.2.5  yamt 		config_cfdriver_detach(&dm_cd);
    202  1.6.2.5  yamt 
    203  1.6.2.5  yamt 		devsw_detach(&dm_bdevsw, &dm_cdevsw);
    204  1.6.2.2  yamt 		break;
    205  1.6.2.2  yamt 	case MODULE_CMD_STAT:
    206  1.6.2.2  yamt 		return ENOTTY;
    207  1.6.2.2  yamt 
    208  1.6.2.2  yamt 	default:
    209  1.6.2.2  yamt 		return ENOTTY;
    210  1.6.2.2  yamt 	}
    211  1.6.2.2  yamt 
    212  1.6.2.5  yamt 	return error;
    213  1.6.2.6  yamt #else
    214  1.6.2.6  yamt 	return ENOTTY;
    215  1.6.2.6  yamt #endif
    216  1.6.2.5  yamt }
    217  1.6.2.6  yamt #endif /* _MODULE */
    218  1.6.2.2  yamt 
    219  1.6.2.5  yamt /*
    220  1.6.2.5  yamt  * dm_match:
    221  1.6.2.5  yamt  *
    222  1.6.2.5  yamt  *	Autoconfiguration match function for pseudo-device glue.
    223  1.6.2.5  yamt  */
    224  1.6.2.5  yamt static int
    225  1.6.2.5  yamt dm_match(device_t parent, cfdata_t match,
    226  1.6.2.5  yamt     void *aux)
    227  1.6.2.5  yamt {
    228  1.6.2.5  yamt 
    229  1.6.2.5  yamt 	/* Pseudo-device; always present. */
    230  1.6.2.5  yamt 	return (1);
    231  1.6.2.2  yamt }
    232  1.6.2.2  yamt 
    233  1.6.2.5  yamt /*
    234  1.6.2.5  yamt  * dm_attach:
    235  1.6.2.5  yamt  *
    236  1.6.2.5  yamt  *	Autoconfiguration attach function for pseudo-device glue.
    237  1.6.2.5  yamt  */
    238  1.6.2.5  yamt static void
    239  1.6.2.5  yamt dm_attach(device_t parent, device_t self,
    240  1.6.2.5  yamt     void *aux)
    241  1.6.2.5  yamt {
    242  1.6.2.5  yamt 	return;
    243  1.6.2.5  yamt }
    244  1.6.2.2  yamt 
    245  1.6.2.5  yamt 
    246  1.6.2.5  yamt /*
    247  1.6.2.5  yamt  * dm_detach:
    248  1.6.2.5  yamt  *
    249  1.6.2.5  yamt  *	Autoconfiguration detach function for pseudo-device glue.
    250  1.6.2.5  yamt  * This routine is called by dm_ioctl::dm_dev_remove_ioctl and by autoconf to
    251  1.6.2.5  yamt  * remove devices created in device-mapper.
    252  1.6.2.5  yamt  */
    253  1.6.2.5  yamt static int
    254  1.6.2.5  yamt dm_detach(device_t self, int flags)
    255  1.6.2.2  yamt {
    256  1.6.2.5  yamt 	dm_dev_t *dmv;
    257  1.6.2.5  yamt 
    258  1.6.2.5  yamt 	/* Detach device from global device list */
    259  1.6.2.5  yamt 	if ((dmv = dm_dev_detach(self)) == NULL)
    260  1.6.2.5  yamt 		return ENOENT;
    261  1.6.2.5  yamt 
    262  1.6.2.5  yamt 	/* Destroy active table first.  */
    263  1.6.2.5  yamt 	dm_table_destroy(&dmv->table_head, DM_TABLE_ACTIVE);
    264  1.6.2.5  yamt 
    265  1.6.2.5  yamt 	/* Destroy inactive table if exits, too. */
    266  1.6.2.5  yamt 	dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
    267  1.6.2.5  yamt 
    268  1.6.2.5  yamt 	dm_table_head_destroy(&dmv->table_head);
    269  1.6.2.5  yamt 
    270  1.6.2.5  yamt 	/* Destroy disk device structure */
    271  1.6.2.5  yamt 	disk_detach(dmv->diskp);
    272  1.6.2.5  yamt 	disk_destroy(dmv->diskp);
    273  1.6.2.5  yamt 
    274  1.6.2.5  yamt 	/* Destroy device */
    275  1.6.2.5  yamt 	(void)dm_dev_free(dmv);
    276  1.6.2.5  yamt 
    277  1.6.2.5  yamt 	/* Decrement device counter After removing device */
    278  1.6.2.5  yamt 	atomic_dec_64(&dm_dev_counter);
    279  1.6.2.5  yamt 
    280  1.6.2.5  yamt 	return 0;
    281  1.6.2.5  yamt }
    282  1.6.2.5  yamt 
    283  1.6.2.6  yamt static void
    284  1.6.2.6  yamt dm_doinit(void)
    285  1.6.2.5  yamt {
    286  1.6.2.2  yamt 	dm_target_init();
    287  1.6.2.2  yamt 	dm_dev_init();
    288  1.6.2.2  yamt 	dm_pdev_init();
    289  1.6.2.2  yamt }
    290  1.6.2.2  yamt 
    291  1.6.2.5  yamt /* attach routine */
    292  1.6.2.5  yamt void
    293  1.6.2.5  yamt dmattach(int n)
    294  1.6.2.5  yamt {
    295  1.6.2.6  yamt 	int error;
    296  1.6.2.6  yamt 
    297  1.6.2.6  yamt 	error = config_cfattach_attach(dm_cd.cd_name, &dm_ca);
    298  1.6.2.6  yamt 	if (error) {
    299  1.6.2.6  yamt 		aprint_error("%s: unable to register cfattach\n",
    300  1.6.2.6  yamt 		    dm_cd.cd_name);
    301  1.6.2.6  yamt 	} else {
    302  1.6.2.6  yamt 		dm_doinit();
    303  1.6.2.6  yamt 	}
    304  1.6.2.5  yamt }
    305  1.6.2.5  yamt 
    306  1.6.2.6  yamt #ifdef _MODULE
    307  1.6.2.2  yamt /* Destroy routine */
    308  1.6.2.6  yamt static int
    309  1.6.2.2  yamt dmdestroy(void)
    310  1.6.2.2  yamt {
    311  1.6.2.5  yamt 	int error;
    312  1.6.2.5  yamt 
    313  1.6.2.5  yamt 	error = config_cfattach_detach(dm_cd.cd_name, &dm_ca);
    314  1.6.2.5  yamt 	if (error)
    315  1.6.2.5  yamt 		return error;
    316  1.6.2.5  yamt 
    317  1.6.2.2  yamt 	dm_dev_destroy();
    318  1.6.2.2  yamt 	dm_pdev_destroy();
    319  1.6.2.2  yamt 	dm_target_destroy();
    320  1.6.2.2  yamt 
    321  1.6.2.2  yamt 	return 0;
    322  1.6.2.2  yamt }
    323  1.6.2.6  yamt #endif /* _MODULE */
    324  1.6.2.2  yamt 
    325  1.6.2.2  yamt static int
    326  1.6.2.2  yamt dmopen(dev_t dev, int flags, int mode, struct lwp *l)
    327  1.6.2.2  yamt {
    328  1.6.2.5  yamt 
    329  1.6.2.5  yamt 	aprint_debug("dm open routine called %" PRIu32 "\n", minor(dev));
    330  1.6.2.2  yamt 	return 0;
    331  1.6.2.2  yamt }
    332  1.6.2.2  yamt 
    333  1.6.2.2  yamt static int
    334  1.6.2.2  yamt dmclose(dev_t dev, int flags, int mode, struct lwp *l)
    335  1.6.2.2  yamt {
    336  1.6.2.2  yamt 
    337  1.6.2.5  yamt 	aprint_debug("dm close routine called %" PRIu32 "\n", minor(dev));
    338  1.6.2.2  yamt 	return 0;
    339  1.6.2.2  yamt }
    340  1.6.2.2  yamt 
    341  1.6.2.2  yamt 
    342  1.6.2.2  yamt static int
    343  1.6.2.2  yamt dmioctl(dev_t dev, const u_long cmd, void *data, int flag, struct lwp *l)
    344  1.6.2.2  yamt {
    345  1.6.2.2  yamt 	int r;
    346  1.6.2.2  yamt 	prop_dictionary_t dm_dict_in;
    347  1.6.2.2  yamt 
    348  1.6.2.2  yamt 	r = 0;
    349  1.6.2.2  yamt 
    350  1.6.2.2  yamt 	aprint_debug("dmioctl called\n");
    351  1.6.2.2  yamt 
    352  1.6.2.2  yamt 	KASSERT(data != NULL);
    353  1.6.2.2  yamt 
    354  1.6.2.5  yamt 	if (( r = disk_ioctl_switch(dev, cmd, data)) == ENOTTY) {
    355  1.6.2.2  yamt 		struct plistref *pref = (struct plistref *) data;
    356  1.6.2.2  yamt 
    357  1.6.2.5  yamt 		/* Check if we were called with NETBSD_DM_IOCTL ioctl
    358  1.6.2.5  yamt 		   otherwise quit. */
    359  1.6.2.5  yamt 		if ((r = dm_ioctl_switch(cmd)) != 0)
    360  1.6.2.5  yamt 			return r;
    361  1.6.2.5  yamt 
    362  1.6.2.2  yamt 		if((r = prop_dictionary_copyin_ioctl(pref, cmd, &dm_dict_in)) != 0)
    363  1.6.2.2  yamt 			return r;
    364  1.6.2.2  yamt 
    365  1.6.2.5  yamt 		if ((r = dm_check_version(dm_dict_in)) != 0)
    366  1.6.2.5  yamt 			goto cleanup_exit;
    367  1.6.2.2  yamt 
    368  1.6.2.2  yamt 		/* run ioctl routine */
    369  1.6.2.5  yamt 		if ((r = dm_cmd_to_fun(dm_dict_in)) != 0)
    370  1.6.2.5  yamt 			goto cleanup_exit;
    371  1.6.2.2  yamt 
    372  1.6.2.5  yamt cleanup_exit:
    373  1.6.2.5  yamt 		r = prop_dictionary_copyout_ioctl(pref, cmd, dm_dict_in);
    374  1.6.2.2  yamt 		prop_object_release(dm_dict_in);
    375  1.6.2.2  yamt 	}
    376  1.6.2.2  yamt 
    377  1.6.2.2  yamt 	return r;
    378  1.6.2.2  yamt }
    379  1.6.2.2  yamt 
    380  1.6.2.2  yamt /*
    381  1.6.2.2  yamt  * Translate command sent from libdevmapper to func.
    382  1.6.2.2  yamt  */
    383  1.6.2.2  yamt static int
    384  1.6.2.2  yamt dm_cmd_to_fun(prop_dictionary_t dm_dict){
    385  1.6.2.2  yamt 	int i, r;
    386  1.6.2.2  yamt 	prop_string_t command;
    387  1.6.2.2  yamt 
    388  1.6.2.2  yamt 	r = 0;
    389  1.6.2.5  yamt 
    390  1.6.2.2  yamt 	if ((command = prop_dictionary_get(dm_dict, DM_IOCTL_COMMAND)) == NULL)
    391  1.6.2.2  yamt 		return EINVAL;
    392  1.6.2.5  yamt 
    393  1.6.2.2  yamt 	for(i = 0; cmd_fn[i].cmd != NULL; i++)
    394  1.6.2.2  yamt 		if (prop_string_equals_cstring(command, cmd_fn[i].cmd))
    395  1.6.2.2  yamt 			break;
    396  1.6.2.2  yamt 
    397  1.6.2.2  yamt 	if (cmd_fn[i].cmd == NULL)
    398  1.6.2.2  yamt 		return EINVAL;
    399  1.6.2.2  yamt 
    400  1.6.2.2  yamt 	aprint_debug("ioctl %s called\n", cmd_fn[i].cmd);
    401  1.6.2.2  yamt 	r = cmd_fn[i].fn(dm_dict);
    402  1.6.2.5  yamt 
    403  1.6.2.2  yamt 	return r;
    404  1.6.2.2  yamt }
    405  1.6.2.2  yamt 
    406  1.6.2.2  yamt /* Call apropriate ioctl handler function. */
    407  1.6.2.2  yamt static int
    408  1.6.2.2  yamt dm_ioctl_switch(u_long cmd)
    409  1.6.2.2  yamt {
    410  1.6.2.2  yamt 
    411  1.6.2.2  yamt 	switch(cmd) {
    412  1.6.2.5  yamt 
    413  1.6.2.2  yamt 	case NETBSD_DM_IOCTL:
    414  1.6.2.5  yamt 		aprint_debug("dm NetBSD_DM_IOCTL called\n");
    415  1.6.2.2  yamt 		break;
    416  1.6.2.2  yamt 	default:
    417  1.6.2.5  yamt 		 aprint_debug("dm unknown ioctl called\n");
    418  1.6.2.2  yamt 		 return ENOTTY;
    419  1.6.2.2  yamt 		 break; /* NOT REACHED */
    420  1.6.2.2  yamt 	}
    421  1.6.2.2  yamt 
    422  1.6.2.5  yamt 	 return 0;
    423  1.6.2.2  yamt }
    424  1.6.2.2  yamt 
    425  1.6.2.2  yamt  /*
    426  1.6.2.2  yamt   * Check for disk specific ioctls.
    427  1.6.2.2  yamt   */
    428  1.6.2.2  yamt 
    429  1.6.2.2  yamt static int
    430  1.6.2.2  yamt disk_ioctl_switch(dev_t dev, u_long cmd, void *data)
    431  1.6.2.2  yamt {
    432  1.6.2.2  yamt 	dm_dev_t *dmv;
    433  1.6.2.5  yamt 
    434  1.6.2.6  yamt 	/* disk ioctls make sense only on block devices */
    435  1.6.2.6  yamt 	if (minor(dev) == 0)
    436  1.6.2.6  yamt 		return ENOTTY;
    437  1.6.2.6  yamt 
    438  1.6.2.2  yamt 	switch(cmd) {
    439  1.6.2.2  yamt 	case DIOCGWEDGEINFO:
    440  1.6.2.2  yamt 	{
    441  1.6.2.2  yamt 		struct dkwedge_info *dkw = (void *) data;
    442  1.6.2.2  yamt 
    443  1.6.2.2  yamt 		if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
    444  1.6.2.5  yamt 			return ENODEV;
    445  1.6.2.5  yamt 
    446  1.6.2.5  yamt 		aprint_debug("DIOCGWEDGEINFO ioctl called\n");
    447  1.6.2.5  yamt 
    448  1.6.2.2  yamt 		strlcpy(dkw->dkw_devname, dmv->name, 16);
    449  1.6.2.2  yamt 		strlcpy(dkw->dkw_wname, dmv->name, DM_NAME_LEN);
    450  1.6.2.2  yamt 		strlcpy(dkw->dkw_parent, dmv->name, 16);
    451  1.6.2.5  yamt 
    452  1.6.2.2  yamt 		dkw->dkw_offset = 0;
    453  1.6.2.2  yamt 		dkw->dkw_size = dm_table_size(&dmv->table_head);
    454  1.6.2.2  yamt 		strcpy(dkw->dkw_ptype, DKW_PTYPE_FFS);
    455  1.6.2.2  yamt 
    456  1.6.2.2  yamt 		dm_dev_unbusy(dmv);
    457  1.6.2.2  yamt 		break;
    458  1.6.2.2  yamt 	}
    459  1.6.2.2  yamt 
    460  1.6.2.3  yamt 	case DIOCGDISKINFO:
    461  1.6.2.2  yamt 	{
    462  1.6.2.3  yamt 		struct plistref *pref = (struct plistref *) data;
    463  1.6.2.3  yamt 
    464  1.6.2.2  yamt 		if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
    465  1.6.2.5  yamt 			return ENODEV;
    466  1.6.2.5  yamt 
    467  1.6.2.3  yamt 		if (dmv->diskp->dk_info == NULL) {
    468  1.6.2.3  yamt 			dm_dev_unbusy(dmv);
    469  1.6.2.3  yamt 			return ENOTSUP;
    470  1.6.2.3  yamt 		} else
    471  1.6.2.3  yamt 			prop_dictionary_copyout_ioctl(pref, cmd,
    472  1.6.2.3  yamt 			    dmv->diskp->dk_info);
    473  1.6.2.2  yamt 
    474  1.6.2.2  yamt 		dm_dev_unbusy(dmv);
    475  1.6.2.2  yamt 		break;
    476  1.6.2.2  yamt 	}
    477  1.6.2.6  yamt 
    478  1.6.2.6  yamt 	case DIOCCACHESYNC:
    479  1.6.2.6  yamt 	{
    480  1.6.2.6  yamt 		dm_table_entry_t *table_en;
    481  1.6.2.6  yamt 		dm_table_t *tbl;
    482  1.6.2.6  yamt 		int err;
    483  1.6.2.6  yamt 
    484  1.6.2.6  yamt 		if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
    485  1.6.2.6  yamt 			return ENODEV;
    486  1.6.2.6  yamt 
    487  1.6.2.6  yamt 		/* Select active table */
    488  1.6.2.6  yamt 		tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
    489  1.6.2.6  yamt 
    490  1.6.2.6  yamt 		/*
    491  1.6.2.6  yamt 		 * Call sync target routine for all table entries. Target sync
    492  1.6.2.6  yamt 		 * routine basically call DIOCCACHESYNC on underlying devices.
    493  1.6.2.6  yamt 		 */
    494  1.6.2.6  yamt 		SLIST_FOREACH(table_en, tbl, next)
    495  1.6.2.6  yamt 		{
    496  1.6.2.6  yamt 			err = table_en->target->sync(table_en);
    497  1.6.2.6  yamt 		}
    498  1.6.2.6  yamt 		dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
    499  1.6.2.6  yamt 		dm_dev_unbusy(dmv);
    500  1.6.2.6  yamt 		break;
    501  1.6.2.6  yamt 	}
    502  1.6.2.6  yamt 
    503  1.6.2.3  yamt 
    504  1.6.2.2  yamt 	default:
    505  1.6.2.2  yamt 		aprint_debug("unknown disk_ioctl called\n");
    506  1.6.2.5  yamt 		return ENOTTY;
    507  1.6.2.2  yamt 		break; /* NOT REACHED */
    508  1.6.2.2  yamt 	}
    509  1.6.2.5  yamt 
    510  1.6.2.2  yamt 	return 0;
    511  1.6.2.2  yamt }
    512  1.6.2.2  yamt 
    513  1.6.2.2  yamt /*
    514  1.6.2.2  yamt  * Do all IO operations on dm logical devices.
    515  1.6.2.2  yamt  */
    516  1.6.2.2  yamt static void
    517  1.6.2.2  yamt dmstrategy(struct buf *bp)
    518  1.6.2.2  yamt {
    519  1.6.2.2  yamt 	dm_dev_t *dmv;
    520  1.6.2.2  yamt 	dm_table_t  *tbl;
    521  1.6.2.2  yamt 	dm_table_entry_t *table_en;
    522  1.6.2.2  yamt 	struct buf *nestbuf;
    523  1.6.2.2  yamt 
    524  1.6.2.2  yamt 	uint32_t dev_type;
    525  1.6.2.2  yamt 
    526  1.6.2.2  yamt 	uint64_t buf_start, buf_len, issued_len;
    527  1.6.2.2  yamt 	uint64_t table_start, table_end;
    528  1.6.2.2  yamt 	uint64_t start, end;
    529  1.6.2.5  yamt 
    530  1.6.2.2  yamt 	buf_start = bp->b_blkno * DEV_BSIZE;
    531  1.6.2.2  yamt 	buf_len = bp->b_bcount;
    532  1.6.2.2  yamt 
    533  1.6.2.2  yamt 	tbl = NULL;
    534  1.6.2.2  yamt 
    535  1.6.2.2  yamt 	table_end = 0;
    536  1.6.2.2  yamt 	dev_type = 0;
    537  1.6.2.2  yamt 	issued_len = 0;
    538  1.6.2.2  yamt 
    539  1.6.2.2  yamt 	if ((dmv = dm_dev_lookup(NULL, NULL, minor(bp->b_dev))) == NULL) {
    540  1.6.2.2  yamt 		bp->b_error = EIO;
    541  1.6.2.2  yamt 		bp->b_resid = bp->b_bcount;
    542  1.6.2.2  yamt 		biodone(bp);
    543  1.6.2.2  yamt 		return;
    544  1.6.2.2  yamt 	}
    545  1.6.2.2  yamt 
    546  1.6.2.4  yamt 	if (bounds_check_with_mediasize(bp, DEV_BSIZE,
    547  1.6.2.4  yamt 	    dm_table_size(&dmv->table_head)) <= 0) {
    548  1.6.2.4  yamt 		dm_dev_unbusy(dmv);
    549  1.6.2.4  yamt 		bp->b_resid = bp->b_bcount;
    550  1.6.2.4  yamt 		biodone(bp);
    551  1.6.2.4  yamt 		return;
    552  1.6.2.4  yamt 	}
    553  1.6.2.4  yamt 
    554  1.6.2.5  yamt 	/*
    555  1.6.2.5  yamt 	 * disk(9) is part of device structure and it can't be used without
    556  1.6.2.5  yamt 	 * mutual exclusion, use diskp_mtx until it will be fixed.
    557  1.6.2.5  yamt 	 */
    558  1.6.2.5  yamt 	mutex_enter(&dmv->diskp_mtx);
    559  1.6.2.2  yamt 	disk_busy(dmv->diskp);
    560  1.6.2.5  yamt 	mutex_exit(&dmv->diskp_mtx);
    561  1.6.2.5  yamt 
    562  1.6.2.2  yamt 	/* Select active table */
    563  1.6.2.2  yamt 	tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
    564  1.6.2.2  yamt 
    565  1.6.2.2  yamt 	 /* Nested buffers count down to zero therefore I have
    566  1.6.2.2  yamt 	    to set bp->b_resid to maximal value. */
    567  1.6.2.2  yamt 	bp->b_resid = bp->b_bcount;
    568  1.6.2.5  yamt 
    569  1.6.2.2  yamt 	/*
    570  1.6.2.2  yamt 	 * Find out what tables I want to select.
    571  1.6.2.2  yamt 	 */
    572  1.6.2.2  yamt 	SLIST_FOREACH(table_en, tbl, next)
    573  1.6.2.2  yamt 	{
    574  1.6.2.2  yamt 		/* I need need number of bytes not blocks. */
    575  1.6.2.2  yamt 		table_start = table_en->start * DEV_BSIZE;
    576  1.6.2.2  yamt 		/*
    577  1.6.2.2  yamt 		 * I have to sub 1 from table_en->length to prevent
    578  1.6.2.2  yamt 		 * off by one error
    579  1.6.2.2  yamt 		 */
    580  1.6.2.2  yamt 		table_end = table_start + (table_en->length)* DEV_BSIZE;
    581  1.6.2.2  yamt 
    582  1.6.2.2  yamt 		start = MAX(table_start, buf_start);
    583  1.6.2.2  yamt 
    584  1.6.2.2  yamt 		end = MIN(table_end, buf_start + buf_len);
    585  1.6.2.2  yamt 
    586  1.6.2.2  yamt 		aprint_debug("----------------------------------------\n");
    587  1.6.2.2  yamt 		aprint_debug("table_start %010" PRIu64", table_end %010"
    588  1.6.2.2  yamt 		    PRIu64 "\n", table_start, table_end);
    589  1.6.2.2  yamt 		aprint_debug("buf_start %010" PRIu64", buf_len %010"
    590  1.6.2.2  yamt 		    PRIu64"\n", buf_start, buf_len);
    591  1.6.2.2  yamt 		aprint_debug("start-buf_start %010"PRIu64", end %010"
    592  1.6.2.2  yamt 		    PRIu64"\n", start - buf_start, end);
    593  1.6.2.2  yamt 		aprint_debug("start %010" PRIu64" , end %010"
    594  1.6.2.2  yamt                     PRIu64"\n", start, end);
    595  1.6.2.2  yamt 		aprint_debug("\n----------------------------------------\n");
    596  1.6.2.2  yamt 
    597  1.6.2.2  yamt 		if (start < end) {
    598  1.6.2.2  yamt 			/* create nested buffer  */
    599  1.6.2.2  yamt 			nestbuf = getiobuf(NULL, true);
    600  1.6.2.2  yamt 
    601  1.6.2.2  yamt 			nestiobuf_setup(bp, nestbuf, start - buf_start,
    602  1.6.2.2  yamt 			    (end - start));
    603  1.6.2.2  yamt 
    604  1.6.2.2  yamt 			issued_len += end - start;
    605  1.6.2.5  yamt 
    606  1.6.2.2  yamt 			/* I need number of blocks. */
    607  1.6.2.2  yamt 			nestbuf->b_blkno = (start - table_start) / DEV_BSIZE;
    608  1.6.2.2  yamt 
    609  1.6.2.2  yamt 			table_en->target->strategy(table_en, nestbuf);
    610  1.6.2.2  yamt 		}
    611  1.6.2.4  yamt 	}
    612  1.6.2.2  yamt 
    613  1.6.2.2  yamt 	if (issued_len < buf_len)
    614  1.6.2.2  yamt 		nestiobuf_done(bp, buf_len - issued_len, EINVAL);
    615  1.6.2.2  yamt 
    616  1.6.2.5  yamt 	mutex_enter(&dmv->diskp_mtx);
    617  1.6.2.2  yamt 	disk_unbusy(dmv->diskp, buf_len, bp != NULL ? bp->b_flags & B_READ : 0);
    618  1.6.2.5  yamt 	mutex_exit(&dmv->diskp_mtx);
    619  1.6.2.5  yamt 
    620  1.6.2.2  yamt 	dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
    621  1.6.2.2  yamt 	dm_dev_unbusy(dmv);
    622  1.6.2.2  yamt 
    623  1.6.2.2  yamt 	return;
    624  1.6.2.2  yamt }
    625  1.6.2.2  yamt 
    626  1.6.2.2  yamt 
    627  1.6.2.2  yamt static int
    628  1.6.2.2  yamt dmread(dev_t dev, struct uio *uio, int flag)
    629  1.6.2.2  yamt {
    630  1.6.2.5  yamt 
    631  1.6.2.2  yamt 	return (physio(dmstrategy, NULL, dev, B_READ, dmminphys, uio));
    632  1.6.2.2  yamt }
    633  1.6.2.2  yamt 
    634  1.6.2.2  yamt static int
    635  1.6.2.2  yamt dmwrite(dev_t dev, struct uio *uio, int flag)
    636  1.6.2.2  yamt {
    637  1.6.2.5  yamt 
    638  1.6.2.2  yamt 	return (physio(dmstrategy, NULL, dev, B_WRITE, dmminphys, uio));
    639  1.6.2.2  yamt }
    640  1.6.2.2  yamt 
    641  1.6.2.2  yamt static int
    642  1.6.2.2  yamt dmsize(dev_t dev)
    643  1.6.2.2  yamt {
    644  1.6.2.2  yamt 	dm_dev_t *dmv;
    645  1.6.2.2  yamt 	uint64_t size;
    646  1.6.2.5  yamt 
    647  1.6.2.2  yamt 	size = 0;
    648  1.6.2.5  yamt 
    649  1.6.2.2  yamt 	if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
    650  1.6.2.2  yamt 			return -ENOENT;
    651  1.6.2.5  yamt 
    652  1.6.2.2  yamt 	size = dm_table_size(&dmv->table_head);
    653  1.6.2.2  yamt 	dm_dev_unbusy(dmv);
    654  1.6.2.2  yamt 
    655  1.6.2.2  yamt   	return size;
    656  1.6.2.2  yamt }
    657  1.6.2.2  yamt 
    658  1.6.2.2  yamt static void
    659  1.6.2.2  yamt dmminphys(struct buf *bp)
    660  1.6.2.2  yamt {
    661  1.6.2.5  yamt 
    662  1.6.2.2  yamt 	bp->b_bcount = MIN(bp->b_bcount, MAXPHYS);
    663  1.6.2.2  yamt }
    664  1.6.2.2  yamt 
    665  1.6.2.2  yamt void
    666  1.6.2.3  yamt dmgetproperties(struct disk *disk, dm_table_head_t *head)
    667  1.6.2.2  yamt {
    668  1.6.2.3  yamt 	prop_dictionary_t disk_info, odisk_info, geom;
    669  1.6.2.2  yamt 	int dmp_size;
    670  1.6.2.5  yamt 
    671  1.6.2.2  yamt 	dmp_size = dm_table_size(head);
    672  1.6.2.3  yamt 	disk_info = prop_dictionary_create();
    673  1.6.2.3  yamt 	geom = prop_dictionary_create();
    674  1.6.2.3  yamt 
    675  1.6.2.5  yamt 	prop_dictionary_set_cstring_nocopy(disk_info, "type", "ESDI");
    676  1.6.2.3  yamt 	prop_dictionary_set_uint64(geom, "sectors-per-unit", dmp_size);
    677  1.6.2.3  yamt 	prop_dictionary_set_uint32(geom, "sector-size",
    678  1.6.2.3  yamt 	    DEV_BSIZE /* XXX 512? */);
    679  1.6.2.3  yamt 	prop_dictionary_set_uint32(geom, "sectors-per-track", 32);
    680  1.6.2.3  yamt 	prop_dictionary_set_uint32(geom, "tracks-per-cylinder", 64);
    681  1.6.2.3  yamt 	prop_dictionary_set_uint32(geom, "cylinders-per-unit", dmp_size / 2048);
    682  1.6.2.3  yamt 	prop_dictionary_set(disk_info, "geometry", geom);
    683  1.6.2.3  yamt 	prop_object_release(geom);
    684  1.6.2.3  yamt 
    685  1.6.2.3  yamt 	odisk_info = disk->dk_info;
    686  1.6.2.3  yamt 	disk->dk_info = disk_info;
    687  1.6.2.3  yamt 
    688  1.6.2.3  yamt 	if (odisk_info != NULL)
    689  1.6.2.3  yamt 		prop_object_release(odisk_info);
    690  1.6.2.3  yamt }
    691