1ff56536eSAlex Hornung /* $NetBSD: device-mapper.c,v 1.22 2010/03/26 15:46:04 jakllsch Exp $ */ 2ff56536eSAlex Hornung 3ff56536eSAlex Hornung /* 4ba2ce341SAlex Hornung * Copyright (c) 2010-2011 Alex Hornung <alex@alexhornung.com> 5ff56536eSAlex Hornung * Copyright (c) 2010 The NetBSD Foundation, Inc. 6ff56536eSAlex Hornung * All rights reserved. 7ff56536eSAlex Hornung * 8ff56536eSAlex Hornung * This code is derived from software contributed to The NetBSD Foundation 9ff56536eSAlex Hornung * by Adam Hamsik. 10ff56536eSAlex Hornung * 11ff56536eSAlex Hornung * Redistribution and use in source and binary forms, with or without 12ff56536eSAlex Hornung * modification, are permitted provided that the following conditions 13ff56536eSAlex Hornung * are met: 14ff56536eSAlex Hornung * 1. Redistributions of source code must retain the above copyright 15ff56536eSAlex Hornung * notice, this list of conditions and the following disclaimer. 16ff56536eSAlex Hornung * 2. Redistributions in binary form must reproduce the above copyright 17ff56536eSAlex Hornung * notice, this list of conditions and the following disclaimer in the 18ff56536eSAlex Hornung * documentation and/or other materials provided with the distribution. 19ff56536eSAlex Hornung * 20ff56536eSAlex Hornung * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21ff56536eSAlex Hornung * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22ff56536eSAlex Hornung * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23ff56536eSAlex Hornung * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24ff56536eSAlex Hornung * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25ff56536eSAlex Hornung * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26ff56536eSAlex Hornung * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27ff56536eSAlex Hornung * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28ff56536eSAlex Hornung * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29ff56536eSAlex Hornung * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30ff56536eSAlex Hornung * POSSIBILITY OF SUCH DAMAGE. 31ff56536eSAlex Hornung */ 32ff56536eSAlex Hornung 33ff56536eSAlex Hornung /* 34ff56536eSAlex Hornung * I want to say thank you to all people who helped me with this project. 35ff56536eSAlex Hornung */ 36ff56536eSAlex Hornung 37ff56536eSAlex Hornung #include <sys/types.h> 38ff56536eSAlex Hornung #include <sys/param.h> 390ba7b1a4SSascha Wildner #include <sys/ctype.h> 40ff56536eSAlex Hornung 41ff56536eSAlex Hornung #include <sys/buf.h> 42ff56536eSAlex Hornung #include <sys/conf.h> 43ff56536eSAlex Hornung #include <sys/device.h> 44ff56536eSAlex Hornung #include <sys/disk.h> 45ff56536eSAlex Hornung #include <sys/disklabel.h> 465b279a20SAlex Hornung #include <sys/dtype.h> 475b279a20SAlex Hornung #include <sys/malloc.h> 485b279a20SAlex Hornung #include <sys/module.h> 49aadb5a11SAlex Hornung #include <sys/sysctl.h> 50a84e173eSAlex Hornung #include <dev/disk/dm/dm.h> 51ff56536eSAlex Hornung 52ff56536eSAlex Hornung #include "netbsd-dm.h" 53ff56536eSAlex Hornung 545b279a20SAlex Hornung static d_ioctl_t dmioctl; 555b279a20SAlex Hornung static d_open_t dmopen; 565b279a20SAlex Hornung static d_close_t dmclose; 575b279a20SAlex Hornung static d_psize_t dmsize; 585b279a20SAlex Hornung static d_strategy_t dmstrategy; 5979b7159fSAlex Hornung static d_dump_t dmdump; 60ff56536eSAlex Hornung 61ff56536eSAlex Hornung /* attach and detach routines */ 625b279a20SAlex Hornung static int dm_modcmd(module_t mod, int cmd, void *unused); 63ff56536eSAlex Hornung static int dmdestroy(void); 64ff56536eSAlex Hornung 65ff56536eSAlex Hornung static void dm_doinit(void); 66ff56536eSAlex Hornung 67ff56536eSAlex Hornung static int dm_cmd_to_fun(prop_dictionary_t); 685b279a20SAlex Hornung static int disk_ioctl_switch(cdev_t, u_long, void *); 69ff56536eSAlex Hornung static int dm_ioctl_switch(u_long); 705b279a20SAlex Hornung #if 0 71ff56536eSAlex Hornung static void dmminphys(struct buf *); 725b279a20SAlex Hornung #endif 73ff56536eSAlex Hornung 74ff56536eSAlex Hornung /* ***Variable-definitions*** */ 755b279a20SAlex Hornung struct dev_ops dm_ops = { 769f889dc4SMatthew Dillon { "dm", 0, D_DISK | D_MPSAFE }, 77ff56536eSAlex Hornung .d_open = dmopen, 78ff56536eSAlex Hornung .d_close = dmclose, 795b279a20SAlex Hornung .d_read = physread, 805b279a20SAlex Hornung .d_write = physwrite, 815b279a20SAlex Hornung .d_ioctl = dmioctl, 82ff56536eSAlex Hornung .d_strategy = dmstrategy, 83ff56536eSAlex Hornung .d_psize = dmsize, 8479b7159fSAlex Hornung .d_dump = dmdump, 855b279a20SAlex Hornung /* D_DISK */ 86ff56536eSAlex Hornung }; 87ff56536eSAlex Hornung 885b279a20SAlex Hornung MALLOC_DEFINE(M_DM, "dm", "Device Mapper allocations"); 89ff56536eSAlex Hornung 90aadb5a11SAlex Hornung int dm_debug_level = 0; 91aadb5a11SAlex Hornung 92ff56536eSAlex Hornung extern uint64_t dm_dev_counter; 93ff56536eSAlex Hornung 945b279a20SAlex Hornung static cdev_t dmcdev; 955b279a20SAlex Hornung 965b279a20SAlex Hornung static moduledata_t dm_mod = { 975b279a20SAlex Hornung "dm", 985b279a20SAlex Hornung dm_modcmd, 995b279a20SAlex Hornung NULL 1005b279a20SAlex Hornung }; 1015b279a20SAlex Hornung DECLARE_MODULE(dm, dm_mod, SI_SUB_RAID, SI_ORDER_ANY); 1027115a22bSAlex Hornung MODULE_VERSION(dm, 1); 1035b279a20SAlex Hornung 104ff56536eSAlex Hornung /* 105ff56536eSAlex Hornung * This array is used to translate cmd to function pointer. 106ff56536eSAlex Hornung * 107ff56536eSAlex Hornung * Interface between libdevmapper and lvm2tools uses different 108ff56536eSAlex Hornung * names for one IOCTL call because libdevmapper do another thing 109ff56536eSAlex Hornung * then. When I run "info" or "mknodes" libdevmapper will send same 110ff56536eSAlex Hornung * ioctl to kernel but will do another things in userspace. 111ff56536eSAlex Hornung * 112ff56536eSAlex Hornung */ 11361413047SAlex Hornung static struct cmd_function cmd_fn[] = { 114ff56536eSAlex Hornung { .cmd = "version", .fn = dm_get_version_ioctl}, 115ff56536eSAlex Hornung { .cmd = "targets", .fn = dm_list_versions_ioctl}, 116ff56536eSAlex Hornung { .cmd = "create", .fn = dm_dev_create_ioctl}, 117ff56536eSAlex Hornung { .cmd = "info", .fn = dm_dev_status_ioctl}, 118ff56536eSAlex Hornung { .cmd = "mknodes", .fn = dm_dev_status_ioctl}, 119ff56536eSAlex Hornung { .cmd = "names", .fn = dm_dev_list_ioctl}, 120ff56536eSAlex Hornung { .cmd = "suspend", .fn = dm_dev_suspend_ioctl}, 121ff56536eSAlex Hornung { .cmd = "remove", .fn = dm_dev_remove_ioctl}, 1229fada28aSAlex Hornung { .cmd = "remove_all", .fn = dm_dev_remove_all_ioctl}, 123ff56536eSAlex Hornung { .cmd = "rename", .fn = dm_dev_rename_ioctl}, 124ff56536eSAlex Hornung { .cmd = "resume", .fn = dm_dev_resume_ioctl}, 125ff56536eSAlex Hornung { .cmd = "clear", .fn = dm_table_clear_ioctl}, 126ff56536eSAlex Hornung { .cmd = "deps", .fn = dm_table_deps_ioctl}, 127ff56536eSAlex Hornung { .cmd = "reload", .fn = dm_table_load_ioctl}, 128ff56536eSAlex Hornung { .cmd = "status", .fn = dm_table_status_ioctl}, 129ff56536eSAlex Hornung { .cmd = "table", .fn = dm_table_status_ioctl}, 13051292ec6SAlex Hornung { .cmd = "message", .fn = dm_message_ioctl}, 131ff56536eSAlex Hornung {NULL, NULL} 132ff56536eSAlex Hornung }; 133ff56536eSAlex Hornung 134ff56536eSAlex Hornung /* New module handle routine */ 135ff56536eSAlex Hornung static int 1365b279a20SAlex Hornung dm_modcmd(module_t mod, int cmd, void *unused) 137ff56536eSAlex Hornung { 1383d089c23SSascha Wildner int error; 139ff56536eSAlex Hornung 140ff56536eSAlex Hornung error = 0; 141ff56536eSAlex Hornung 142ff56536eSAlex Hornung switch (cmd) { 1435b279a20SAlex Hornung case MOD_LOAD: 144ff56536eSAlex Hornung dm_doinit(); 145e8e2bcdaSAlex Hornung kprintf("Device Mapper version %d.%d.%d loaded\n", 1465b279a20SAlex Hornung DM_VERSION_MAJOR, DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL); 147ff56536eSAlex Hornung break; 148ff56536eSAlex Hornung 1495b279a20SAlex Hornung case MOD_UNLOAD: 150ff56536eSAlex Hornung /* 151ff56536eSAlex Hornung * Disable unloading of dm module if there are any devices 152ff56536eSAlex Hornung * defined in driver. This is probably too strong we need 153ff56536eSAlex Hornung * to disable auto-unload only if there is mounted dm device 154ff56536eSAlex Hornung * present. 155ff56536eSAlex Hornung */ 156ff56536eSAlex Hornung if (dm_dev_counter > 0) 157ff56536eSAlex Hornung return EBUSY; 158ff56536eSAlex Hornung 159ff56536eSAlex Hornung error = dmdestroy(); 160ff56536eSAlex Hornung if (error) 161ff56536eSAlex Hornung break; 162e8e2bcdaSAlex Hornung kprintf("Device Mapper unloaded\n"); 163ff56536eSAlex Hornung break; 164ff56536eSAlex Hornung 165ff56536eSAlex Hornung default: 1665b279a20SAlex Hornung break; 167ff56536eSAlex Hornung } 168ff56536eSAlex Hornung 169ff56536eSAlex Hornung return error; 170ff56536eSAlex Hornung } 171ff56536eSAlex Hornung 172ff56536eSAlex Hornung static void 173ff56536eSAlex Hornung dm_doinit(void) 174ff56536eSAlex Hornung { 175ff56536eSAlex Hornung dm_target_init(); 176ff56536eSAlex Hornung dm_dev_init(); 177ff56536eSAlex Hornung dm_pdev_init(); 1785b279a20SAlex Hornung dmcdev = make_dev(&dm_ops, 0, UID_ROOT, GID_OPERATOR, 0640, "mapper/control"); 179ff56536eSAlex Hornung } 180ff56536eSAlex Hornung 181ff56536eSAlex Hornung /* Destroy routine */ 182ff56536eSAlex Hornung static int 183ff56536eSAlex Hornung dmdestroy(void) 184ff56536eSAlex Hornung { 1855b279a20SAlex Hornung destroy_dev(dmcdev); 186ff56536eSAlex Hornung 1879fada28aSAlex Hornung dm_dev_uninit(); 1889fada28aSAlex Hornung dm_pdev_uninit(); 1899fada28aSAlex Hornung dm_target_uninit(); 190ff56536eSAlex Hornung 191ff56536eSAlex Hornung return 0; 192ff56536eSAlex Hornung } 193ff56536eSAlex Hornung 194ff56536eSAlex Hornung static int 1955b279a20SAlex Hornung dmopen(struct dev_open_args *ap) 196ff56536eSAlex Hornung { 197956c8d71SAlex Hornung cdev_t dev = ap->a_head.a_dev; 198956c8d71SAlex Hornung dm_dev_t *dmv; 199956c8d71SAlex Hornung 200956c8d71SAlex Hornung /* Shortcut for the control device */ 201956c8d71SAlex Hornung if (minor(dev) == 0) 202956c8d71SAlex Hornung return 0; 203956c8d71SAlex Hornung 204956c8d71SAlex Hornung if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL) 205956c8d71SAlex Hornung return ENXIO; 206956c8d71SAlex Hornung 207956c8d71SAlex Hornung dmv->is_open = 1; 208956c8d71SAlex Hornung dm_dev_unbusy(dmv); 209ff56536eSAlex Hornung 2105b279a20SAlex Hornung aprint_debug("dm open routine called %" PRIu32 "\n", 2115b279a20SAlex Hornung minor(ap->a_head.a_dev)); 212ff56536eSAlex Hornung return 0; 213ff56536eSAlex Hornung } 214ff56536eSAlex Hornung 215ff56536eSAlex Hornung static int 2165b279a20SAlex Hornung dmclose(struct dev_close_args *ap) 217ff56536eSAlex Hornung { 218956c8d71SAlex Hornung cdev_t dev = ap->a_head.a_dev; 219956c8d71SAlex Hornung dm_dev_t *dmv; 220956c8d71SAlex Hornung 221956c8d71SAlex Hornung /* Shortcut for the control device */ 222956c8d71SAlex Hornung if (minor(dev) == 0) 223956c8d71SAlex Hornung return 0; 224956c8d71SAlex Hornung 225956c8d71SAlex Hornung if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL) 226956c8d71SAlex Hornung return ENXIO; 227956c8d71SAlex Hornung 228956c8d71SAlex Hornung dmv->is_open = 0; 229956c8d71SAlex Hornung dm_dev_unbusy(dmv); 230ff56536eSAlex Hornung 2315b279a20SAlex Hornung aprint_debug("dm close routine called %" PRIu32 "\n", 2325b279a20SAlex Hornung minor(ap->a_head.a_dev)); 233ff56536eSAlex Hornung return 0; 234ff56536eSAlex Hornung } 235ff56536eSAlex Hornung 236ff56536eSAlex Hornung 237ff56536eSAlex Hornung static int 2385b279a20SAlex Hornung dmioctl(struct dev_ioctl_args *ap) 239ff56536eSAlex Hornung { 2405b279a20SAlex Hornung cdev_t dev = ap->a_head.a_dev; 2415b279a20SAlex Hornung u_long cmd = ap->a_cmd; 2425b279a20SAlex Hornung void *data = ap->a_data; 2435b279a20SAlex Hornung 244956c8d71SAlex Hornung int r, err; 245ff56536eSAlex Hornung prop_dictionary_t dm_dict_in; 246ff56536eSAlex Hornung 247956c8d71SAlex Hornung err = r = 0; 248ff56536eSAlex Hornung 249ff56536eSAlex Hornung aprint_debug("dmioctl called\n"); 250ff56536eSAlex Hornung 2515b279a20SAlex Hornung KKASSERT(data != NULL); 252ff56536eSAlex Hornung 253ff56536eSAlex Hornung if ((r = disk_ioctl_switch(dev, cmd, data)) == ENOTTY) { 254ff56536eSAlex Hornung struct plistref *pref = (struct plistref *) data; 255ff56536eSAlex Hornung 256ff56536eSAlex Hornung /* Check if we were called with NETBSD_DM_IOCTL ioctl 257ff56536eSAlex Hornung otherwise quit. */ 258ff56536eSAlex Hornung if ((r = dm_ioctl_switch(cmd)) != 0) 259ff56536eSAlex Hornung return r; 260ff56536eSAlex Hornung 261ff56536eSAlex Hornung if ((r = prop_dictionary_copyin_ioctl(pref, cmd, &dm_dict_in)) != 0) 262ff56536eSAlex Hornung return r; 263ff56536eSAlex Hornung 264ff56536eSAlex Hornung if ((r = dm_check_version(dm_dict_in)) != 0) 265ff56536eSAlex Hornung goto cleanup_exit; 266ff56536eSAlex Hornung 267ff56536eSAlex Hornung /* run ioctl routine */ 268956c8d71SAlex Hornung if ((err = dm_cmd_to_fun(dm_dict_in)) != 0) 269ff56536eSAlex Hornung goto cleanup_exit; 270ff56536eSAlex Hornung 271ff56536eSAlex Hornung cleanup_exit: 272ff56536eSAlex Hornung r = prop_dictionary_copyout_ioctl(pref, cmd, dm_dict_in); 273ff56536eSAlex Hornung prop_object_release(dm_dict_in); 274ff56536eSAlex Hornung } 275ff56536eSAlex Hornung 276956c8d71SAlex Hornung /* 277956c8d71SAlex Hornung * Return the error of the actual command if one one has 278956c8d71SAlex Hornung * happened. Otherwise return 'r' which indicates errors 279956c8d71SAlex Hornung * that occurred during helper operations. 280956c8d71SAlex Hornung */ 281956c8d71SAlex Hornung return (err != 0)?err:r; 282ff56536eSAlex Hornung } 283ff56536eSAlex Hornung 284ff56536eSAlex Hornung /* 285ff56536eSAlex Hornung * Translate command sent from libdevmapper to func. 286ff56536eSAlex Hornung */ 287ff56536eSAlex Hornung static int 28878869e36STomohiro Kusumi dm_cmd_to_fun(prop_dictionary_t dm_dict) 28978869e36STomohiro Kusumi { 290ff56536eSAlex Hornung int i, r; 291ff56536eSAlex Hornung prop_string_t command; 292ff56536eSAlex Hornung 293ff56536eSAlex Hornung r = 0; 294ff56536eSAlex Hornung 295ff56536eSAlex Hornung if ((command = prop_dictionary_get(dm_dict, DM_IOCTL_COMMAND)) == NULL) 296ff56536eSAlex Hornung return EINVAL; 297ff56536eSAlex Hornung 298ff56536eSAlex Hornung for (i = 0; cmd_fn[i].cmd != NULL; i++) 299ff56536eSAlex Hornung if (prop_string_equals_cstring(command, cmd_fn[i].cmd)) 300ff56536eSAlex Hornung break; 301ff56536eSAlex Hornung 302ff56536eSAlex Hornung if (cmd_fn[i].cmd == NULL) 303ff56536eSAlex Hornung return EINVAL; 304ff56536eSAlex Hornung 305ff56536eSAlex Hornung aprint_debug("ioctl %s called\n", cmd_fn[i].cmd); 306ff56536eSAlex Hornung r = cmd_fn[i].fn(dm_dict); 307ff56536eSAlex Hornung 308ff56536eSAlex Hornung return r; 309ff56536eSAlex Hornung } 310ff56536eSAlex Hornung 311ff56536eSAlex Hornung /* Call apropriate ioctl handler function. */ 312ff56536eSAlex Hornung static int 313ff56536eSAlex Hornung dm_ioctl_switch(u_long cmd) 314ff56536eSAlex Hornung { 315ff56536eSAlex Hornung 316ff56536eSAlex Hornung switch(cmd) { 317ff56536eSAlex Hornung 318ff56536eSAlex Hornung case NETBSD_DM_IOCTL: 319ff56536eSAlex Hornung aprint_debug("dm NetBSD_DM_IOCTL called\n"); 320ff56536eSAlex Hornung break; 321ff56536eSAlex Hornung default: 322ff56536eSAlex Hornung aprint_debug("dm unknown ioctl called\n"); 323ff56536eSAlex Hornung return ENOTTY; 324ff56536eSAlex Hornung break; /* NOT REACHED */ 325ff56536eSAlex Hornung } 326ff56536eSAlex Hornung 327ff56536eSAlex Hornung return 0; 328ff56536eSAlex Hornung } 329ff56536eSAlex Hornung 330ff56536eSAlex Hornung /* 331ff56536eSAlex Hornung * Check for disk specific ioctls. 332ff56536eSAlex Hornung */ 333ff56536eSAlex Hornung 334ff56536eSAlex Hornung static int 3355b279a20SAlex Hornung disk_ioctl_switch(cdev_t dev, u_long cmd, void *data) 336ff56536eSAlex Hornung { 337ff56536eSAlex Hornung dm_dev_t *dmv; 338ff56536eSAlex Hornung 339ff56536eSAlex Hornung /* disk ioctls make sense only on block devices */ 340ff56536eSAlex Hornung if (minor(dev) == 0) 341ff56536eSAlex Hornung return ENOTTY; 342ff56536eSAlex Hornung 343ff56536eSAlex Hornung switch(cmd) { 3445b279a20SAlex Hornung case DIOCGPART: 345ff56536eSAlex Hornung { 3465b279a20SAlex Hornung struct partinfo *dpart; 3475b279a20SAlex Hornung u_int64_t size; 3488062d810SSascha Wildner dpart = data; 3495b279a20SAlex Hornung bzero(dpart, sizeof(*dpart)); 350ff56536eSAlex Hornung 351ba65987cSAlex Hornung if ((dmv = dev->si_drv1) == NULL) 352ff56536eSAlex Hornung return ENODEV; 3535b279a20SAlex Hornung if (dmv->diskp->d_info.d_media_blksize == 0) { 354ff56536eSAlex Hornung return ENOTSUP; 3555b279a20SAlex Hornung } else { 3565b279a20SAlex Hornung size = dm_table_size(&dmv->table_head); 3575b279a20SAlex Hornung dpart->media_offset = 0; 3585b279a20SAlex Hornung dpart->media_size = size * DEV_BSIZE; 3595b279a20SAlex Hornung dpart->media_blocks = size; 3605b279a20SAlex Hornung dpart->media_blksize = DEV_BSIZE; 3615b279a20SAlex Hornung dpart->fstype = FS_BSDFFS; 3625b279a20SAlex Hornung } 363ff56536eSAlex Hornung break; 364ff56536eSAlex Hornung } 365ff56536eSAlex Hornung 366ff56536eSAlex Hornung default: 367ff56536eSAlex Hornung aprint_debug("unknown disk_ioctl called\n"); 368ff56536eSAlex Hornung return ENOTTY; 369ff56536eSAlex Hornung break; /* NOT REACHED */ 370ff56536eSAlex Hornung } 371ff56536eSAlex Hornung 372ff56536eSAlex Hornung return 0; 373ff56536eSAlex Hornung } 374ff56536eSAlex Hornung 375ff56536eSAlex Hornung /* 376ff56536eSAlex Hornung * Do all IO operations on dm logical devices. 377ff56536eSAlex Hornung */ 3785b279a20SAlex Hornung static int 3795b279a20SAlex Hornung dmstrategy(struct dev_strategy_args *ap) 380ff56536eSAlex Hornung { 3815b279a20SAlex Hornung cdev_t dev = ap->a_head.a_dev; 3825b279a20SAlex Hornung struct bio *bio = ap->a_bio; 3835b279a20SAlex Hornung struct buf *bp = bio->bio_buf; 3843adc52bcSMatthew Dillon int bypass; 3855b279a20SAlex Hornung 386ff56536eSAlex Hornung dm_dev_t *dmv; 387ff56536eSAlex Hornung dm_table_t *tbl; 388ff56536eSAlex Hornung dm_table_entry_t *table_en; 389ff56536eSAlex Hornung struct buf *nestbuf; 390ff56536eSAlex Hornung 391ff56536eSAlex Hornung uint64_t buf_start, buf_len, issued_len; 392ff56536eSAlex Hornung uint64_t table_start, table_end; 393ff56536eSAlex Hornung uint64_t start, end; 394ff56536eSAlex Hornung 3955b279a20SAlex Hornung buf_start = bio->bio_offset; 396ff56536eSAlex Hornung buf_len = bp->b_bcount; 397ff56536eSAlex Hornung 398ff56536eSAlex Hornung tbl = NULL; 399ff56536eSAlex Hornung 400ff56536eSAlex Hornung table_end = 0; 401ff56536eSAlex Hornung issued_len = 0; 402ff56536eSAlex Hornung 403ba65987cSAlex Hornung dmv = dev->si_drv1; 404ff56536eSAlex Hornung 4053adc52bcSMatthew Dillon switch(bp->b_cmd) { 4063adc52bcSMatthew Dillon case BUF_CMD_READ: 4073adc52bcSMatthew Dillon case BUF_CMD_WRITE: 4083adc52bcSMatthew Dillon case BUF_CMD_FREEBLKS: 4093adc52bcSMatthew Dillon bypass = 0; 4103adc52bcSMatthew Dillon break; 4113adc52bcSMatthew Dillon case BUF_CMD_FLUSH: 4123adc52bcSMatthew Dillon bypass = 1; 4133adc52bcSMatthew Dillon KKASSERT(buf_len == 0); 4143adc52bcSMatthew Dillon break; 4153adc52bcSMatthew Dillon default: 4163adc52bcSMatthew Dillon bp->b_error = EIO; 4173adc52bcSMatthew Dillon bp->b_resid = bp->b_bcount; 4183adc52bcSMatthew Dillon biodone(bio); 4193adc52bcSMatthew Dillon return 0; 4203adc52bcSMatthew Dillon } 4213adc52bcSMatthew Dillon 4223adc52bcSMatthew Dillon if (bypass == 0 && 4233adc52bcSMatthew Dillon bounds_check_with_mediasize(bio, DEV_BSIZE, 424ff56536eSAlex Hornung dm_table_size(&dmv->table_head)) <= 0) { 425ff56536eSAlex Hornung bp->b_resid = bp->b_bcount; 4265b279a20SAlex Hornung biodone(bio); 4275b279a20SAlex Hornung return 0; 428ff56536eSAlex Hornung } 429ff56536eSAlex Hornung 430ff56536eSAlex Hornung /* Select active table */ 431ff56536eSAlex Hornung tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE); 432ff56536eSAlex Hornung 4333adc52bcSMatthew Dillon nestiobuf_init(bio); 4343b48c3c1SAlex Hornung devstat_start_transaction(&dmv->stats); 435ff56536eSAlex Hornung 436ff56536eSAlex Hornung /* 437ff56536eSAlex Hornung * Find out what tables I want to select. 438ff56536eSAlex Hornung */ 4393adc52bcSMatthew Dillon SLIST_FOREACH(table_en, tbl, next) { 440ff56536eSAlex Hornung /* 4413adc52bcSMatthew Dillon * I need need number of bytes not blocks. 442ff56536eSAlex Hornung */ 4433adc52bcSMatthew Dillon table_start = table_en->start * DEV_BSIZE; 444ff56536eSAlex Hornung table_end = table_start + (table_en->length) * DEV_BSIZE; 445ff56536eSAlex Hornung 4463adc52bcSMatthew Dillon /* 4473adc52bcSMatthew Dillon * Calculate the start and end 4483adc52bcSMatthew Dillon */ 449ff56536eSAlex Hornung start = MAX(table_start, buf_start); 450ff56536eSAlex Hornung end = MIN(table_end, buf_start + buf_len); 451ff56536eSAlex Hornung 452*a3c6b8ecSTomohiro Kusumi if (dm_debug_level) { 453*a3c6b8ecSTomohiro Kusumi aprint_normal("----------------------------------------\n"); 454*a3c6b8ecSTomohiro Kusumi aprint_normal("table_start %010" PRIu64", table_end %010" 455ff56536eSAlex Hornung PRIu64 "\n", table_start, table_end); 456*a3c6b8ecSTomohiro Kusumi aprint_normal("buf_start %010" PRIu64", buf_len %010" 457ff56536eSAlex Hornung PRIu64"\n", buf_start, buf_len); 458*a3c6b8ecSTomohiro Kusumi aprint_normal("start-buf_start %010"PRIu64", end %010" 459ff56536eSAlex Hornung PRIu64"\n", start - buf_start, end); 460*a3c6b8ecSTomohiro Kusumi aprint_normal("start %010" PRIu64" , end %010" 461ff56536eSAlex Hornung PRIu64"\n", start, end); 462*a3c6b8ecSTomohiro Kusumi aprint_normal("\n----------------------------------------\n"); 463*a3c6b8ecSTomohiro Kusumi } 464ff56536eSAlex Hornung 4653adc52bcSMatthew Dillon if (bypass) { 4665b279a20SAlex Hornung nestbuf = getpbuf(NULL); 467f6221ad1SMatthew Dillon nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS; 468ff56536eSAlex Hornung 4693b48c3c1SAlex Hornung nestiobuf_add(bio, nestbuf, 0, 0, &dmv->stats); 4703adc52bcSMatthew Dillon nestbuf->b_bio1.bio_offset = 0; 4713adc52bcSMatthew Dillon table_en->target->strategy(table_en, nestbuf); 4723adc52bcSMatthew Dillon } else if (start < end) { 4733adc52bcSMatthew Dillon nestbuf = getpbuf(NULL); 474f6221ad1SMatthew Dillon nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS; 475f6221ad1SMatthew Dillon 4763adc52bcSMatthew Dillon nestiobuf_add(bio, nestbuf, 4773b48c3c1SAlex Hornung start - buf_start, (end - start), 4783b48c3c1SAlex Hornung &dmv->stats); 479ff56536eSAlex Hornung issued_len += end - start; 480ff56536eSAlex Hornung 4815b279a20SAlex Hornung nestbuf->b_bio1.bio_offset = (start - table_start); 482ff56536eSAlex Hornung table_en->target->strategy(table_en, nestbuf); 483ff56536eSAlex Hornung } 484ff56536eSAlex Hornung } 485ff56536eSAlex Hornung 486ff56536eSAlex Hornung if (issued_len < buf_len) 4873adc52bcSMatthew Dillon nestiobuf_error(bio, EINVAL); 4883adc52bcSMatthew Dillon nestiobuf_start(bio); 489ff56536eSAlex Hornung dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE); 490ff56536eSAlex Hornung 4915b279a20SAlex Hornung return 0; 492ff56536eSAlex Hornung } 493ff56536eSAlex Hornung 494ff56536eSAlex Hornung static int 49579b7159fSAlex Hornung dmdump(struct dev_dump_args *ap) 49679b7159fSAlex Hornung { 49779b7159fSAlex Hornung cdev_t dev = ap->a_head.a_dev; 49879b7159fSAlex Hornung dm_dev_t *dmv; 49979b7159fSAlex Hornung dm_table_t *tbl; 50079b7159fSAlex Hornung dm_table_entry_t *table_en; 50179b7159fSAlex Hornung uint64_t buf_start, buf_len, issued_len; 50279b7159fSAlex Hornung uint64_t table_start, table_end; 50379b7159fSAlex Hornung uint64_t start, end, data_offset; 50479b7159fSAlex Hornung off_t offset; 50579b7159fSAlex Hornung size_t length; 50679b7159fSAlex Hornung int error = 0; 50779b7159fSAlex Hornung 50879b7159fSAlex Hornung buf_start = ap->a_offset; 50979b7159fSAlex Hornung buf_len = ap->a_length; 51079b7159fSAlex Hornung 51179b7159fSAlex Hornung tbl = NULL; 51279b7159fSAlex Hornung 51379b7159fSAlex Hornung table_end = 0; 51479b7159fSAlex Hornung issued_len = 0; 51579b7159fSAlex Hornung 516ba65987cSAlex Hornung dmv = dev->si_drv1; 51779b7159fSAlex Hornung 51879b7159fSAlex Hornung /* Select active table */ 51979b7159fSAlex Hornung tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE); 52079b7159fSAlex Hornung 52179b7159fSAlex Hornung 52279b7159fSAlex Hornung /* 52379b7159fSAlex Hornung * Find out what tables I want to select. 52479b7159fSAlex Hornung */ 52579b7159fSAlex Hornung SLIST_FOREACH(table_en, tbl, next) { 52679b7159fSAlex Hornung /* 52779b7159fSAlex Hornung * I need need number of bytes not blocks. 52879b7159fSAlex Hornung */ 52979b7159fSAlex Hornung table_start = table_en->start * DEV_BSIZE; 53079b7159fSAlex Hornung table_end = table_start + (table_en->length) * DEV_BSIZE; 53179b7159fSAlex Hornung 53279b7159fSAlex Hornung /* 53379b7159fSAlex Hornung * Calculate the start and end 53479b7159fSAlex Hornung */ 53579b7159fSAlex Hornung start = MAX(table_start, buf_start); 53679b7159fSAlex Hornung end = MIN(table_end, buf_start + buf_len); 53779b7159fSAlex Hornung 53879b7159fSAlex Hornung if (ap->a_length == 0) { 53979b7159fSAlex Hornung if (table_en->target->dump == NULL) { 54079b7159fSAlex Hornung error = ENXIO; 54179b7159fSAlex Hornung goto out; 54279b7159fSAlex Hornung } 54379b7159fSAlex Hornung 54479b7159fSAlex Hornung table_en->target->dump(table_en, NULL, 0, 0); 54579b7159fSAlex Hornung } else if (start < end) { 54679b7159fSAlex Hornung data_offset = start - buf_start; 54779b7159fSAlex Hornung offset = start - table_start; 54879b7159fSAlex Hornung length = end - start; 54979b7159fSAlex Hornung 55079b7159fSAlex Hornung if (table_en->target->dump == NULL) { 55179b7159fSAlex Hornung error = ENXIO; 55279b7159fSAlex Hornung goto out; 55379b7159fSAlex Hornung } 55479b7159fSAlex Hornung 55579b7159fSAlex Hornung table_en->target->dump(table_en, 55679b7159fSAlex Hornung (char *)ap->a_virtual + data_offset, 55779b7159fSAlex Hornung length, offset); 55879b7159fSAlex Hornung 55979b7159fSAlex Hornung issued_len += end - start; 56079b7159fSAlex Hornung } 56179b7159fSAlex Hornung } 56279b7159fSAlex Hornung 56379b7159fSAlex Hornung if (issued_len < buf_len) 56479b7159fSAlex Hornung error = EINVAL; 56579b7159fSAlex Hornung 56679b7159fSAlex Hornung out: 56779b7159fSAlex Hornung dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE); 56879b7159fSAlex Hornung 56979b7159fSAlex Hornung return error; 57079b7159fSAlex Hornung } 57179b7159fSAlex Hornung 57279b7159fSAlex Hornung static int 5735b279a20SAlex Hornung dmsize(struct dev_psize_args *ap) 574ff56536eSAlex Hornung { 5755b279a20SAlex Hornung cdev_t dev = ap->a_head.a_dev; 576ff56536eSAlex Hornung dm_dev_t *dmv; 577ff56536eSAlex Hornung uint64_t size; 578ff56536eSAlex Hornung 579ff56536eSAlex Hornung size = 0; 580ff56536eSAlex Hornung 581ba65987cSAlex Hornung if ((dmv = dev->si_drv1) == NULL) 582ba65987cSAlex Hornung return ENXIO; 583ff56536eSAlex Hornung 584ff56536eSAlex Hornung size = dm_table_size(&dmv->table_head); 58527d04cb3SAlex Hornung ap->a_result = (int64_t)size; 58627d04cb3SAlex Hornung 58727d04cb3SAlex Hornung return 0; 588ff56536eSAlex Hornung } 589ff56536eSAlex Hornung 5905b279a20SAlex Hornung #if 0 591ff56536eSAlex Hornung static void 592ff56536eSAlex Hornung dmminphys(struct buf *bp) 593ff56536eSAlex Hornung { 594ff56536eSAlex Hornung 595ff56536eSAlex Hornung bp->b_bcount = MIN(bp->b_bcount, MAXPHYS); 596ff56536eSAlex Hornung } 5975b279a20SAlex Hornung #endif 5985b279a20SAlex Hornung 5995b279a20SAlex Hornung void 6005b279a20SAlex Hornung dmsetdiskinfo(struct disk *disk, dm_table_head_t *head) 6015b279a20SAlex Hornung { 6025b279a20SAlex Hornung struct disk_info info; 6033b48c3c1SAlex Hornung uint64_t dmp_size; 6045b279a20SAlex Hornung 6055b279a20SAlex Hornung dmp_size = dm_table_size(head); 6065b279a20SAlex Hornung 6075b279a20SAlex Hornung bzero(&info, sizeof(struct disk_info)); 6085b279a20SAlex Hornung info.d_media_blksize = DEV_BSIZE; 6095b279a20SAlex Hornung info.d_media_blocks = dmp_size; 61008cba728SAlex Hornung #if 0 6113af01d56SAlex Hornung /* this is set by disk_setdiskinfo */ 6125b279a20SAlex Hornung info.d_media_size = dmp_size * DEV_BSIZE; 61308cba728SAlex Hornung #endif 614c6bbf777SAlex Hornung info.d_dsflags = DSO_MBRQUIET | DSO_DEVICEMAPPER | DSO_RAWPSIZE; 6153af01d56SAlex Hornung 6165b279a20SAlex Hornung info.d_secpertrack = 32; 6175b279a20SAlex Hornung info.d_nheads = 64; 6185b279a20SAlex Hornung info.d_secpercyl = info.d_secpertrack * info.d_nheads; 6193af01d56SAlex Hornung info.d_ncylinders = dmp_size / info.d_secpercyl; 62008cba728SAlex Hornung 62108cba728SAlex Hornung disk_setdiskinfo(disk, &info); 622ff56536eSAlex Hornung } 623aadb5a11SAlex Hornung 6249fada28aSAlex Hornung /* 6259fada28aSAlex Hornung * Transform char s to uint64_t offset number. 6269fada28aSAlex Hornung */ 6279fada28aSAlex Hornung uint64_t 6289fada28aSAlex Hornung atoi64(const char *s) 6299fada28aSAlex Hornung { 6309fada28aSAlex Hornung uint64_t n; 6319fada28aSAlex Hornung n = 0; 6329fada28aSAlex Hornung 6339fada28aSAlex Hornung while (*s != '\0') { 6349fada28aSAlex Hornung if (!isdigit(*s)) 6359fada28aSAlex Hornung break; 6369fada28aSAlex Hornung 6379fada28aSAlex Hornung n = (10 * n) + (*s - '0'); 6389fada28aSAlex Hornung s++; 6399fada28aSAlex Hornung } 6409fada28aSAlex Hornung 6419fada28aSAlex Hornung return n; 6429fada28aSAlex Hornung } 6439fada28aSAlex Hornung 6447115a22bSAlex Hornung void 6457115a22bSAlex Hornung dm_builtin_init(void *arg) 6467115a22bSAlex Hornung { 6477115a22bSAlex Hornung modeventhand_t evh = (modeventhand_t)arg; 6487115a22bSAlex Hornung 6497115a22bSAlex Hornung KKASSERT(evh != NULL); 6507115a22bSAlex Hornung evh(NULL, MOD_LOAD, NULL); 6517115a22bSAlex Hornung } 6527115a22bSAlex Hornung 6537115a22bSAlex Hornung void 6547115a22bSAlex Hornung dm_builtin_uninit(void *arg) 6557115a22bSAlex Hornung { 6567115a22bSAlex Hornung modeventhand_t evh = (modeventhand_t)arg; 6577115a22bSAlex Hornung 6587115a22bSAlex Hornung KKASSERT(evh != NULL); 6597115a22bSAlex Hornung evh(NULL, MOD_UNLOAD, NULL); 6607115a22bSAlex Hornung } 6617115a22bSAlex Hornung 662aadb5a11SAlex Hornung TUNABLE_INT("debug.dm_debug", &dm_debug_level); 663aadb5a11SAlex Hornung SYSCTL_INT(_debug, OID_AUTO, dm_debug, CTLFLAG_RW, &dm_debug_level, 6642250180cSSascha Wildner 0, "Enable device mapper debugging"); 665aadb5a11SAlex Hornung 666