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