Home | History | Annotate | Line # | Download | only in dm
device-mapper.c revision 1.1.2.1
      1  1.1.2.1  haad /*
      2  1.1.2.1  haad  * Copyright (c) 1996, 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
      3  1.1.2.1  haad  * All rights reserved.
      4  1.1.2.1  haad  *
      5  1.1.2.1  haad  * This code is derived from software contributed to The NetBSD Foundation
      6  1.1.2.1  haad  * by Adam Hamsik.
      7  1.1.2.1  haad  *
      8  1.1.2.1  haad  * Redistribution and use in source and binary forms, with or without
      9  1.1.2.1  haad  * modification, are permitted provided that the following conditions
     10  1.1.2.1  haad  * are met:
     11  1.1.2.1  haad  * 1. Redistributions of source code must retain the above copyright
     12  1.1.2.1  haad  *    notice, this list of conditions and the following disclaimer.
     13  1.1.2.1  haad  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1.2.1  haad  *    notice, this list of conditions and the following disclaimer in the
     15  1.1.2.1  haad  *    documentation and/or other materials provided with the distribution.
     16  1.1.2.1  haad  *
     17  1.1.2.1  haad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18  1.1.2.1  haad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  1.1.2.1  haad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  1.1.2.1  haad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21  1.1.2.1  haad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  1.1.2.1  haad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  1.1.2.1  haad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  1.1.2.1  haad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  1.1.2.1  haad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  1.1.2.1  haad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  1.1.2.1  haad  * POSSIBILITY OF SUCH DAMAGE.
     28  1.1.2.1  haad  */
     29  1.1.2.1  haad 
     30  1.1.2.1  haad /*
     31  1.1.2.1  haad  * I want to say thank you to all people who helped me with this awesome project.
     32  1.1.2.1  haad  */
     33  1.1.2.1  haad 
     34  1.1.2.1  haad #include <sys/types.h>
     35  1.1.2.1  haad #include <sys/param.h>
     36  1.1.2.1  haad 
     37  1.1.2.1  haad #include <sys/buf.h>
     38  1.1.2.1  haad #include <sys/conf.h>
     39  1.1.2.1  haad #include <sys/dkio.h>
     40  1.1.2.1  haad #include <sys/disk.h>
     41  1.1.2.1  haad #include <sys/disklabel.h>
     42  1.1.2.1  haad #include <sys/errno.h>
     43  1.1.2.1  haad #include <sys/ioctl.h>
     44  1.1.2.1  haad #include <sys/ioccom.h>
     45  1.1.2.1  haad #include <sys/kmem.h>
     46  1.1.2.1  haad #ifdef __LKM__
     47  1.1.2.1  haad #include <sys/lkm.h>
     48  1.1.2.1  haad #endif
     49  1.1.2.1  haad 
     50  1.1.2.1  haad #include "netbsd-dm.h"
     51  1.1.2.1  haad #include "dm.h"
     52  1.1.2.1  haad 
     53  1.1.2.1  haad static dev_type_open(dmopen);
     54  1.1.2.1  haad static dev_type_close(dmclose);
     55  1.1.2.1  haad static dev_type_read(dmread);
     56  1.1.2.1  haad static dev_type_write(dmwrite);
     57  1.1.2.1  haad static dev_type_ioctl(dmioctl);
     58  1.1.2.1  haad static dev_type_strategy(dmstrategy);
     59  1.1.2.1  haad static dev_type_dump(dmdump);
     60  1.1.2.1  haad static dev_type_size(dmsize);
     61  1.1.2.1  haad 
     62  1.1.2.1  haad /* attach and detach routines */
     63  1.1.2.1  haad int dmattach(void);
     64  1.1.2.1  haad int dmdestroy(void);
     65  1.1.2.1  haad 
     66  1.1.2.1  haad static int dm_cmd_to_fun(prop_dictionary_t);
     67  1.1.2.1  haad static int disk_ioctl_switch(dev_t, u_long, void *);
     68  1.1.2.1  haad static int dm_ioctl_switch(u_long);
     69  1.1.2.1  haad static void dmminphys(struct buf*);
     70  1.1.2.1  haad static void dmgetdisklabel(struct dm_dev*, dev_t);
     71  1.1.2.1  haad /* ***Variable-definitions*** */
     72  1.1.2.1  haad const struct bdevsw dm_bdevsw = {
     73  1.1.2.1  haad 	dmopen, dmclose, dmstrategy, dmioctl, dmdump, dmsize,
     74  1.1.2.1  haad 		D_DISK
     75  1.1.2.1  haad };
     76  1.1.2.1  haad 
     77  1.1.2.1  haad const struct cdevsw dm_cdevsw = {
     78  1.1.2.1  haad 	dmopen, dmclose, dmread, dmwrite, dmioctl,
     79  1.1.2.1  haad 		nostop, notty, nopoll, nommap, nokqfilter, D_DISK
     80  1.1.2.1  haad };
     81  1.1.2.1  haad 
     82  1.1.2.1  haad #ifdef __LKM__
     83  1.1.2.1  haad 
     84  1.1.2.1  haad /* lkm module init routine */
     85  1.1.2.1  haad int dm_lkmentry(struct lkm_table *, int, int);
     86  1.1.2.1  haad 
     87  1.1.2.1  haad /* lkm module handle routine */
     88  1.1.2.1  haad static int dm_handle(struct lkm_table *, int);
     89  1.1.2.1  haad 
     90  1.1.2.1  haad MOD_DEV("dm", "dm", &dm_bdevsw, -1, &dm_cdevsw, -1);
     91  1.1.2.1  haad 
     92  1.1.2.1  haad #endif
     93  1.1.2.1  haad 
     94  1.1.2.1  haad /* Info about all devices */
     95  1.1.2.1  haad struct dm_softc *dm_sc;
     96  1.1.2.1  haad 
     97  1.1.2.1  haad /*
     98  1.1.2.1  haad  * This array is used to translate cmd to function pointer.
     99  1.1.2.1  haad  *
    100  1.1.2.1  haad  * Interface between libdevmapper and lvm2tools uses different
    101  1.1.2.1  haad  * names for one IOCTL call because libdevmapper do another thing
    102  1.1.2.1  haad  * then. When I run "info" or "mknodes" libdevmapper will send same
    103  1.1.2.1  haad  * ioctl to kernel but will do another things in userspace.
    104  1.1.2.1  haad  *
    105  1.1.2.1  haad  */
    106  1.1.2.1  haad struct cmd_function cmd_fn[] = {
    107  1.1.2.1  haad 		{"version", dm_get_version_ioctl},
    108  1.1.2.1  haad 		{"targets", dm_list_versions_ioctl},
    109  1.1.2.1  haad 		{"create",  dm_dev_create_ioctl},
    110  1.1.2.1  haad 		{"info",    dm_dev_status_ioctl},
    111  1.1.2.1  haad 		{"mknodes", dm_dev_status_ioctl},
    112  1.1.2.1  haad 		{"names",   dm_dev_list_ioctl},
    113  1.1.2.1  haad 		{"suspend", dm_dev_suspend_ioctl},
    114  1.1.2.1  haad 		{"remove",  dm_dev_remove_ioctl},
    115  1.1.2.1  haad 		{"rename",  dm_dev_rename_ioctl},
    116  1.1.2.1  haad 		{"resume",  dm_dev_resume_ioctl},
    117  1.1.2.1  haad 		{"clear",   dm_table_clear_ioctl},
    118  1.1.2.1  haad 		{"deps",    dm_table_deps_ioctl},
    119  1.1.2.1  haad 		{"reload",  dm_table_load_ioctl},
    120  1.1.2.1  haad 		{"status",  dm_table_status_ioctl},
    121  1.1.2.1  haad 		{"table",   dm_table_status_ioctl},
    122  1.1.2.1  haad 		{NULL, NULL}
    123  1.1.2.1  haad };
    124  1.1.2.1  haad 
    125  1.1.2.1  haad /* attach routine */
    126  1.1.2.1  haad int
    127  1.1.2.1  haad dmattach(void)
    128  1.1.2.1  haad {
    129  1.1.2.1  haad 
    130  1.1.2.1  haad 	dm_sc = (struct dm_softc *)kmem_alloc(sizeof(struct dm_softc), KM_NOSLEEP);
    131  1.1.2.1  haad 
    132  1.1.2.1  haad 	if (dm_sc == NULL){
    133  1.1.2.1  haad 		aprint_error("Not enough memory for dm device.\n");
    134  1.1.2.1  haad 		return(ENOMEM);
    135  1.1.2.1  haad 	}
    136  1.1.2.1  haad 
    137  1.1.2.1  haad 	dm_sc->sc_minor_num = 0;
    138  1.1.2.1  haad 	dm_sc->sc_ref_count = 0;
    139  1.1.2.1  haad 
    140  1.1.2.1  haad 	dm_target_init();
    141  1.1.2.1  haad 
    142  1.1.2.1  haad 	dm_dev_init();
    143  1.1.2.1  haad 
    144  1.1.2.1  haad 	dm_pdev_init();
    145  1.1.2.1  haad 
    146  1.1.2.1  haad 	return 0;
    147  1.1.2.1  haad }
    148  1.1.2.1  haad 
    149  1.1.2.1  haad /* Destroy routine */
    150  1.1.2.1  haad int
    151  1.1.2.1  haad dmdestroy(void)
    152  1.1.2.1  haad {
    153  1.1.2.1  haad 
    154  1.1.2.1  haad 	(void)kmem_free(dm_sc,sizeof(struct dm_softc));
    155  1.1.2.1  haad 
    156  1.1.2.1  haad 	return 0;
    157  1.1.2.1  haad }
    158  1.1.2.1  haad 
    159  1.1.2.1  haad #ifdef __LKM__
    160  1.1.2.1  haad 
    161  1.1.2.1  haad /* lkm entry for ld */
    162  1.1.2.1  haad int
    163  1.1.2.1  haad dm_lkmentry(struct lkm_table *lkmtp, int cmd, int ver)
    164  1.1.2.1  haad {
    165  1.1.2.1  haad 	DISPATCH(lkmtp, cmd, ver, dm_handle, dm_handle, dm_handle);
    166  1.1.2.1  haad }
    167  1.1.2.1  haad 
    168  1.1.2.1  haad /* handle function for all load/stat/unload actions */
    169  1.1.2.1  haad static int
    170  1.1.2.1  haad dm_handle(struct lkm_table *lkmtp, int cmd)
    171  1.1.2.1  haad {
    172  1.1.2.1  haad 
    173  1.1.2.1  haad 	int r;
    174  1.1.2.1  haad 
    175  1.1.2.1  haad 	r=0;
    176  1.1.2.1  haad 
    177  1.1.2.1  haad 	switch(cmd) {
    178  1.1.2.1  haad 
    179  1.1.2.1  haad 	case  LKM_E_LOAD:
    180  1.1.2.1  haad 		aprint_error("Loading dm driver.\n");
    181  1.1.2.1  haad 		dmattach();
    182  1.1.2.1  haad 		break;
    183  1.1.2.1  haad 
    184  1.1.2.1  haad 	case  LKM_E_UNLOAD:
    185  1.1.2.1  haad 		aprint_error("Unloading dm driver.\n");
    186  1.1.2.1  haad 		dmdestroy();
    187  1.1.2.1  haad 		break;
    188  1.1.2.1  haad 
    189  1.1.2.1  haad 	case LKM_E_STAT:
    190  1.1.2.1  haad 		aprint_error("Opened dm devices: \n");
    191  1.1.2.1  haad 		break;
    192  1.1.2.1  haad 	}
    193  1.1.2.1  haad 
    194  1.1.2.1  haad 	return r;
    195  1.1.2.1  haad }
    196  1.1.2.1  haad 
    197  1.1.2.1  haad #endif
    198  1.1.2.1  haad 
    199  1.1.2.1  haad 
    200  1.1.2.1  haad static int
    201  1.1.2.1  haad dmopen(dev_t dev, int flags, int mode, struct lwp *l)
    202  1.1.2.1  haad {
    203  1.1.2.1  haad 
    204  1.1.2.1  haad 	struct dm_dev *dmv;
    205  1.1.2.1  haad 
    206  1.1.2.1  haad 	if ((dmv = dm_dev_lookup_minor(minor(dev))) != NULL) {
    207  1.1.2.1  haad 		if (dmv->dm_dk == NULL)
    208  1.1.2.1  haad 			dmgetdisklabel(dmv,dev);
    209  1.1.2.1  haad 
    210  1.1.2.1  haad 		dmv->ref_cnt++;
    211  1.1.2.1  haad 		}
    212  1.1.2.1  haad 
    213  1.1.2.1  haad 	aprint_verbose("open routine called %d\n",minor(dev));
    214  1.1.2.1  haad 
    215  1.1.2.1  haad 	return 0;
    216  1.1.2.1  haad }
    217  1.1.2.1  haad 
    218  1.1.2.1  haad 
    219  1.1.2.1  haad static int
    220  1.1.2.1  haad dmclose(dev_t dev, int flags, int mode, struct lwp *l)
    221  1.1.2.1  haad {
    222  1.1.2.1  haad 	struct dm_dev *dmv;
    223  1.1.2.1  haad 
    224  1.1.2.1  haad 	if ((dmv = dm_dev_lookup_minor(minor(dev))) != NULL)
    225  1.1.2.1  haad 	dmv->ref_cnt--;
    226  1.1.2.1  haad 
    227  1.1.2.1  haad 	aprint_verbose("CLOSE routine called\n");
    228  1.1.2.1  haad 
    229  1.1.2.1  haad 	return 0;
    230  1.1.2.1  haad }
    231  1.1.2.1  haad 
    232  1.1.2.1  haad /*
    233  1.1.2.1  haad  * Called after ioctl call on mapper/control or dm device.
    234  1.1.2.1  haad  */
    235  1.1.2.1  haad 
    236  1.1.2.1  haad static int
    237  1.1.2.1  haad dmioctl(dev_t dev, const u_long cmd, void *data, int flag, struct lwp *l)
    238  1.1.2.1  haad {
    239  1.1.2.1  haad 	int r;
    240  1.1.2.1  haad 	prop_dictionary_t dm_dict_in;
    241  1.1.2.1  haad 
    242  1.1.2.1  haad 	r = 0;
    243  1.1.2.1  haad 
    244  1.1.2.1  haad 	if (data == NULL)
    245  1.1.2.1  haad 		return(EINVAL);
    246  1.1.2.1  haad 
    247  1.1.2.1  haad 	if (disk_ioctl_switch(dev,cmd,data) != 0) {
    248  1.1.2.1  haad 		struct plistref *pref = (struct plistref *) data;
    249  1.1.2.1  haad 
    250  1.1.2.1  haad 		r = prop_dictionary_copyin_ioctl(pref, cmd, &dm_dict_in);
    251  1.1.2.1  haad 		if (r)
    252  1.1.2.1  haad 			return r;
    253  1.1.2.1  haad 
    254  1.1.2.1  haad 		dm_check_version(dm_dict_in);
    255  1.1.2.1  haad 
    256  1.1.2.1  haad 		/* call cmd selected function */
    257  1.1.2.1  haad 		r = dm_ioctl_switch(cmd);
    258  1.1.2.1  haad 		if (r < 0)
    259  1.1.2.1  haad 			goto out;
    260  1.1.2.1  haad 
    261  1.1.2.1  haad 		char *xml;
    262  1.1.2.1  haad 		xml = prop_dictionary_externalize(dm_dict_in);
    263  1.1.2.1  haad 		aprint_verbose("%s\n",xml);
    264  1.1.2.1  haad 
    265  1.1.2.1  haad 		r = dm_cmd_to_fun(dm_dict_in);
    266  1.1.2.1  haad 		if (r != 0)
    267  1.1.2.1  haad 			goto out;
    268  1.1.2.1  haad 
    269  1.1.2.1  haad 		r = prop_dictionary_copyout_ioctl(pref, cmd, dm_dict_in);
    270  1.1.2.1  haad 	}
    271  1.1.2.1  haad 
    272  1.1.2.1  haad out:
    273  1.1.2.1  haad 	return r;
    274  1.1.2.1  haad }
    275  1.1.2.1  haad 
    276  1.1.2.1  haad static int
    277  1.1.2.1  haad dm_cmd_to_fun(prop_dictionary_t dm_dict){
    278  1.1.2.1  haad 	int i,len,slen;
    279  1.1.2.1  haad 	int r;
    280  1.1.2.1  haad 	const char *command;
    281  1.1.2.1  haad 
    282  1.1.2.1  haad 	r = 0;
    283  1.1.2.1  haad 
    284  1.1.2.1  haad 	(void)prop_dictionary_get_cstring_nocopy(dm_dict,DM_IOCTL_COMMAND,&command);
    285  1.1.2.1  haad 
    286  1.1.2.1  haad 	len = strlen(command);
    287  1.1.2.1  haad 
    288  1.1.2.1  haad 	for(i=0;cmd_fn[i].cmd != NULL;i++){
    289  1.1.2.1  haad 		slen = strlen(cmd_fn[i].cmd);
    290  1.1.2.1  haad 
    291  1.1.2.1  haad 		if (len != slen)
    292  1.1.2.1  haad 			continue;
    293  1.1.2.1  haad 
    294  1.1.2.1  haad 		if ((strncmp(command,cmd_fn[i].cmd,slen)) == 0) {
    295  1.1.2.1  haad 			aprint_verbose("ioctl command: %s\n",command);
    296  1.1.2.1  haad 			r = cmd_fn[i].fn(dm_dict);
    297  1.1.2.1  haad 			break;
    298  1.1.2.1  haad 		}
    299  1.1.2.1  haad 	}
    300  1.1.2.1  haad 
    301  1.1.2.1  haad 	return r;
    302  1.1.2.1  haad }
    303  1.1.2.1  haad 
    304  1.1.2.1  haad /* Call apropriate ioctl handler function. */
    305  1.1.2.1  haad static int
    306  1.1.2.1  haad dm_ioctl_switch(u_long cmd)
    307  1.1.2.1  haad {
    308  1.1.2.1  haad 	int r;
    309  1.1.2.1  haad 
    310  1.1.2.1  haad 	r = 0;
    311  1.1.2.1  haad 
    312  1.1.2.1  haad 	switch(cmd) {
    313  1.1.2.1  haad 
    314  1.1.2.1  haad 	case NETBSD_DM_IOCTL:
    315  1.1.2.1  haad 		aprint_verbose("NetBSD_DM_IOCTL called\n");
    316  1.1.2.1  haad 		break;
    317  1.1.2.1  haad 
    318  1.1.2.1  haad 	default:
    319  1.1.2.1  haad 		 aprint_verbose("unknown ioctl called\n");
    320  1.1.2.1  haad 		 return EPASSTHROUGH;
    321  1.1.2.1  haad 		 break; /* NOT REACHED */
    322  1.1.2.1  haad 	}
    323  1.1.2.1  haad 
    324  1.1.2.1  haad 	 return r;
    325  1.1.2.1  haad  }
    326  1.1.2.1  haad 
    327  1.1.2.1  haad  /*
    328  1.1.2.1  haad   * Check for disk specific ioctls.
    329  1.1.2.1  haad   */
    330  1.1.2.1  haad 
    331  1.1.2.1  haad  static int
    332  1.1.2.1  haad  disk_ioctl_switch(dev_t dev, u_long cmd, void *data)
    333  1.1.2.1  haad  {
    334  1.1.2.1  haad 	 struct dm_dev *dmv;
    335  1.1.2.1  haad 
    336  1.1.2.1  haad 	 if ((dmv = dm_dev_lookup_minor(minor(dev))) == NULL)
    337  1.1.2.1  haad 		 return 1;
    338  1.1.2.1  haad 
    339  1.1.2.1  haad 	 switch(cmd) {
    340  1.1.2.1  haad 
    341  1.1.2.1  haad 	 case DIOCGWEDGEINFO:
    342  1.1.2.1  haad 	 {
    343  1.1.2.1  haad 		 struct dkwedge_info *dkw = (void *) data;
    344  1.1.2.1  haad 
    345  1.1.2.1  haad 		 aprint_verbose("DIOCGWEDGEINFO ioctl called\n");
    346  1.1.2.1  haad 
    347  1.1.2.1  haad 		 strlcpy(dkw->dkw_devname, dmv->name, 16);
    348  1.1.2.1  haad 		 strlcpy(dkw->dkw_wname, dmv->name, DM_NAME_LEN);
    349  1.1.2.1  haad 		 strlcpy(dkw->dkw_parent, dmv->name, 16);
    350  1.1.2.1  haad 
    351  1.1.2.1  haad 		 dkw->dkw_offset = 0;
    352  1.1.2.1  haad 		 dkw->dkw_size = dmsize(dev);
    353  1.1.2.1  haad 		 strcpy(dkw->dkw_ptype, DKW_PTYPE_FFS);
    354  1.1.2.1  haad 
    355  1.1.2.1  haad 		 break;
    356  1.1.2.1  haad 	 }
    357  1.1.2.1  haad 
    358  1.1.2.1  haad 	 case DIOCGDINFO:
    359  1.1.2.1  haad 		 *(struct disklabel *)data = *(dmv->dm_dk->dk_label);
    360  1.1.2.1  haad 		 break;
    361  1.1.2.1  haad 
    362  1.1.2.1  haad 	 case DIOCGPART:
    363  1.1.2.1  haad 	 case DIOCWDINFO:
    364  1.1.2.1  haad 	 case DIOCSDINFO:
    365  1.1.2.1  haad 	 case DIOCKLABEL:
    366  1.1.2.1  haad 	 case DIOCWLABEL:
    367  1.1.2.1  haad 	 case DIOCGDEFLABEL:
    368  1.1.2.1  haad 
    369  1.1.2.1  haad 	 default:
    370  1.1.2.1  haad 		 aprint_verbose("unknown disk_ioctl called\n");
    371  1.1.2.1  haad 		 return 1;
    372  1.1.2.1  haad 		 break; /* NOT REACHED */
    373  1.1.2.1  haad 	 }
    374  1.1.2.1  haad 
    375  1.1.2.1  haad 	 return 0;
    376  1.1.2.1  haad 
    377  1.1.2.1  haad  }
    378  1.1.2.1  haad 
    379  1.1.2.1  haad  static void
    380  1.1.2.1  haad dmstrategy(struct buf *bp)
    381  1.1.2.1  haad {
    382  1.1.2.1  haad 
    383  1.1.2.1  haad 	struct dm_dev *dmv;
    384  1.1.2.1  haad 	struct dm_table  *tbl;
    385  1.1.2.1  haad 
    386  1.1.2.1  haad 	struct dm_table_entry *table_en;
    387  1.1.2.1  haad 
    388  1.1.2.1  haad 	struct buf *nestbuf;
    389  1.1.2.1  haad 
    390  1.1.2.1  haad 	uint64_t table_start;
    391  1.1.2.1  haad 	uint64_t table_end;
    392  1.1.2.1  haad 
    393  1.1.2.1  haad 	uint64_t buf_start;
    394  1.1.2.1  haad 	uint64_t buf_len;
    395  1.1.2.1  haad 
    396  1.1.2.1  haad 	uint64_t start;
    397  1.1.2.1  haad 	uint64_t end;
    398  1.1.2.1  haad 
    399  1.1.2.1  haad 	uint64_t issued_len;
    400  1.1.2.1  haad 
    401  1.1.2.1  haad 	buf_start = bp->b_blkno * DEV_BSIZE;
    402  1.1.2.1  haad 	buf_len = bp->b_bcount;
    403  1.1.2.1  haad 
    404  1.1.2.1  haad 	tbl = NULL; /* XXX gcc uninitialized */
    405  1.1.2.1  haad 
    406  1.1.2.1  haad 	table_end = 0;
    407  1.1.2.1  haad 
    408  1.1.2.1  haad 	issued_len = 0;
    409  1.1.2.1  haad 
    410  1.1.2.1  haad 	aprint_verbose("dmstrategy routine called %d--%d\n",minor(bp->b_dev),bp->b_bcount);
    411  1.1.2.1  haad 
    412  1.1.2.1  haad 	if ( (dmv = dm_dev_lookup_minor(minor(bp->b_dev))) == NULL) {
    413  1.1.2.1  haad 		bp->b_error = EIO;
    414  1.1.2.1  haad 		bp->b_resid = bp->b_bcount;
    415  1.1.2.1  haad 		biodone(bp);
    416  1.1.2.1  haad 		return;
    417  1.1.2.1  haad 	}
    418  1.1.2.1  haad 
    419  1.1.2.1  haad 	/* Select active table */
    420  1.1.2.1  haad 	tbl = &dmv->tables[dmv->cur_active_table];
    421  1.1.2.1  haad 
    422  1.1.2.1  haad 	 /* Nested buffers count down to zero therefore I have
    423  1.1.2.1  haad 	    to set bp->b_resid to maximal value. */
    424  1.1.2.1  haad 	bp->b_resid = bp->b_bcount;
    425  1.1.2.1  haad 
    426  1.1.2.1  haad 	/*
    427  1.1.2.1  haad 	 * Find out what tables I want to select.
    428  1.1.2.1  haad 	 */
    429  1.1.2.1  haad 	SLIST_FOREACH(table_en, tbl, next)
    430  1.1.2.1  haad 	{
    431  1.1.2.1  haad 
    432  1.1.2.1  haad 		/* I need need number of bytes not blocks. */
    433  1.1.2.1  haad 		table_start = table_en->start * DEV_BSIZE;
    434  1.1.2.1  haad 		/* I have to sub 1 from table_en->length to prevent off by one error  */
    435  1.1.2.1  haad 		table_end = table_start + (table_en->length)* DEV_BSIZE;
    436  1.1.2.1  haad 
    437  1.1.2.1  haad 		start = MAX(table_start, buf_start);
    438  1.1.2.1  haad 
    439  1.1.2.1  haad 		end = MIN(table_end, buf_start + buf_len);
    440  1.1.2.1  haad 
    441  1.1.2.1  haad 		aprint_debug("----------------------------------------\n");
    442  1.1.2.1  haad 		aprint_debug("table_start %010" PRIu64", table_end %010" PRIu64 "\n",table_start,
    443  1.1.2.1  haad 		    table_end);
    444  1.1.2.1  haad 		aprint_debug("buf_start %010" PRIu64", buf_len %010" PRIu64"\n",buf_start,buf_len);
    445  1.1.2.1  haad 		aprint_debug("start-buf_start %010"PRIu64", end %010"PRIu64"\n",start - buf_start,
    446  1.1.2.1  haad 		    end);
    447  1.1.2.1  haad 		aprint_debug("end-start %010" PRIu64 "\n", end - start);
    448  1.1.2.1  haad 		aprint_debug("\n----------------------------------------\n");
    449  1.1.2.1  haad 
    450  1.1.2.1  haad 		if (start < end) {
    451  1.1.2.1  haad 			/* create nested buffer  */
    452  1.1.2.1  haad 			nestbuf = getiobuf(NULL, true);
    453  1.1.2.1  haad 
    454  1.1.2.1  haad 			nestiobuf_setup(bp, nestbuf, start - buf_start, (end-start));
    455  1.1.2.1  haad 
    456  1.1.2.1  haad 			issued_len += end-start;
    457  1.1.2.1  haad 
    458  1.1.2.1  haad 			/* I need number of blocks. */
    459  1.1.2.1  haad 			nestbuf->b_blkno = (start - table_start) / DEV_BSIZE;
    460  1.1.2.1  haad 
    461  1.1.2.1  haad 			table_en->target->strategy(table_en, nestbuf);
    462  1.1.2.1  haad 		}
    463  1.1.2.1  haad 	}
    464  1.1.2.1  haad 
    465  1.1.2.1  haad 	if (issued_len < buf_len)
    466  1.1.2.1  haad 		nestiobuf_done(bp, buf_len - issued_len, EINVAL);
    467  1.1.2.1  haad 
    468  1.1.2.1  haad 	return;
    469  1.1.2.1  haad 
    470  1.1.2.1  haad }
    471  1.1.2.1  haad 
    472  1.1.2.1  haad 
    473  1.1.2.1  haad static int
    474  1.1.2.1  haad dmread(dev_t dev, struct uio *uio, int flag)
    475  1.1.2.1  haad {
    476  1.1.2.1  haad 	return (physio(dmstrategy, NULL, dev, B_READ, dmminphys, uio));
    477  1.1.2.1  haad }
    478  1.1.2.1  haad 
    479  1.1.2.1  haad static int
    480  1.1.2.1  haad dmwrite(dev_t dev, struct uio *uio, int flag)
    481  1.1.2.1  haad {
    482  1.1.2.1  haad 	return (physio(dmstrategy, NULL, dev, B_WRITE, dmminphys, uio));
    483  1.1.2.1  haad }
    484  1.1.2.1  haad 
    485  1.1.2.1  haad static int
    486  1.1.2.1  haad dmdump(dev_t dev, daddr_t blkno, void *va, size_t size)
    487  1.1.2.1  haad {
    488  1.1.2.1  haad 	return ENODEV;
    489  1.1.2.1  haad }
    490  1.1.2.1  haad 
    491  1.1.2.1  haad static int
    492  1.1.2.1  haad dmsize(dev_t dev)
    493  1.1.2.1  haad {
    494  1.1.2.1  haad 	struct dm_dev *dmv;
    495  1.1.2.1  haad 	struct dm_table  *tbl;
    496  1.1.2.1  haad 	struct dm_table_entry *table_en;
    497  1.1.2.1  haad 
    498  1.1.2.1  haad 	uint64_t length;
    499  1.1.2.1  haad 
    500  1.1.2.1  haad 	length = 0;
    501  1.1.2.1  haad 
    502  1.1.2.1  haad 	aprint_debug("dmsize routine called %d\n",minor(dev));
    503  1.1.2.1  haad 
    504  1.1.2.1  haad 	if ( (dmv = dm_dev_lookup_minor(minor(dev))) == NULL)
    505  1.1.2.1  haad 		return ENODEV;
    506  1.1.2.1  haad 
    507  1.1.2.1  haad 	/* Select active table */
    508  1.1.2.1  haad 	tbl = &dmv->tables[dmv->cur_active_table];
    509  1.1.2.1  haad 
    510  1.1.2.1  haad 	/*
    511  1.1.2.1  haad 	 * Find out what tables I want to select.
    512  1.1.2.1  haad 	 * if length => rawblkno then we should used that table.
    513  1.1.2.1  haad 	 */
    514  1.1.2.1  haad 	SLIST_FOREACH(table_en, tbl, next)
    515  1.1.2.1  haad 	    length += table_en->length;
    516  1.1.2.1  haad 
    517  1.1.2.1  haad 
    518  1.1.2.1  haad 	return length;
    519  1.1.2.1  haad }
    520  1.1.2.1  haad 
    521  1.1.2.1  haad static void
    522  1.1.2.1  haad dmminphys(struct buf *bp)
    523  1.1.2.1  haad {
    524  1.1.2.1  haad 	bp->b_bcount = MIN(bp->b_bcount, MAXPHYS);
    525  1.1.2.1  haad }
    526  1.1.2.1  haad  /*
    527  1.1.2.1  haad   * Load the label information on the named device
    528  1.1.2.1  haad   * Actually fabricate a disklabel
    529  1.1.2.1  haad   *
    530  1.1.2.1  haad   * EVENTUALLY take information about different
    531  1.1.2.1  haad   * data tracks from the TOC and put it in the disklabel
    532  1.1.2.1  haad   */
    533  1.1.2.1  haad 
    534  1.1.2.1  haad 
    535  1.1.2.1  haad static void
    536  1.1.2.1  haad dmgetdisklabel(struct dm_dev *dmv, dev_t dev)
    537  1.1.2.1  haad {
    538  1.1.2.1  haad 	struct disklabel *lp = dmv->dm_dk->dk_label;
    539  1.1.2.1  haad 	const char *errstring;
    540  1.1.2.1  haad 
    541  1.1.2.1  haad 	memset(dmv->dm_dk->dk_cpulabel, 0, sizeof(struct cpu_disklabel));
    542  1.1.2.1  haad 
    543  1.1.2.1  haad 	/*
    544  1.1.2.1  haad 	 * Call the generic disklabel extraction routine
    545  1.1.2.1  haad 	 */
    546  1.1.2.1  haad 	errstring = readdisklabel(dev, dmstrategy, lp, dmv->dm_dk->dk_cpulabel);
    547  1.1.2.1  haad 
    548  1.1.2.1  haad 	/* if all went OK, we are passed a NULL error string */
    549  1.1.2.1  haad 	if (errstring == NULL)
    550  1.1.2.1  haad 		return;
    551  1.1.2.1  haad 
    552  1.1.2.1  haad 	aprint_debug("\t%s\t %u sectors \n",dmv->name,dmv->dm_dk->dk_label->d_secsize);
    553  1.1.2.1  haad }
    554  1.1.2.1  haad 
    555