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