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