1ff56536eSAlex Hornung /* $NetBSD: device-mapper.c,v 1.22 2010/03/26 15:46:04 jakllsch Exp $ */ 2ff56536eSAlex Hornung 3ff56536eSAlex Hornung /* 4ff56536eSAlex Hornung * Copyright (c) 2010 The NetBSD Foundation, Inc. 5ff56536eSAlex Hornung * All rights reserved. 6ff56536eSAlex Hornung * 7ff56536eSAlex Hornung * This code is derived from software contributed to The NetBSD Foundation 8ff56536eSAlex Hornung * by Adam Hamsik. 9ff56536eSAlex Hornung * 10ff56536eSAlex Hornung * Redistribution and use in source and binary forms, with or without 11ff56536eSAlex Hornung * modification, are permitted provided that the following conditions 12ff56536eSAlex Hornung * are met: 13ff56536eSAlex Hornung * 1. Redistributions of source code must retain the above copyright 14ff56536eSAlex Hornung * notice, this list of conditions and the following disclaimer. 15ff56536eSAlex Hornung * 2. Redistributions in binary form must reproduce the above copyright 16ff56536eSAlex Hornung * notice, this list of conditions and the following disclaimer in the 17ff56536eSAlex Hornung * documentation and/or other materials provided with the distribution. 18ff56536eSAlex Hornung * 19ff56536eSAlex Hornung * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20ff56536eSAlex Hornung * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21ff56536eSAlex Hornung * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22ff56536eSAlex Hornung * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23ff56536eSAlex Hornung * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24ff56536eSAlex Hornung * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25ff56536eSAlex Hornung * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26ff56536eSAlex Hornung * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27ff56536eSAlex Hornung * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28ff56536eSAlex Hornung * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29ff56536eSAlex Hornung * POSSIBILITY OF SUCH DAMAGE. 30ff56536eSAlex Hornung */ 31ff56536eSAlex Hornung 32ff56536eSAlex Hornung /* 33ff56536eSAlex Hornung * I want to say thank you to all people who helped me with this project. 34ff56536eSAlex Hornung */ 35ff56536eSAlex Hornung 36ff56536eSAlex Hornung #include <sys/types.h> 37ff56536eSAlex Hornung #include <sys/param.h> 38ff56536eSAlex Hornung 39ff56536eSAlex Hornung #include <sys/buf.h> 40ff56536eSAlex Hornung #include <sys/conf.h> 41ff56536eSAlex Hornung #include <sys/device.h> 42ff56536eSAlex Hornung #include <sys/disk.h> 43ff56536eSAlex Hornung #include <sys/disklabel.h> 445b279a20SAlex Hornung #include <sys/dtype.h> 45ff56536eSAlex Hornung #include <sys/ioccom.h> 465b279a20SAlex Hornung #include <sys/malloc.h> 475b279a20SAlex Hornung #include <sys/module.h> 48aadb5a11SAlex Hornung #include <sys/sysctl.h> 49ff56536eSAlex Hornung 50ff56536eSAlex Hornung #include "netbsd-dm.h" 51ff56536eSAlex Hornung #include "dm.h" 52ff56536eSAlex Hornung 535b279a20SAlex Hornung static d_ioctl_t dmioctl; 545b279a20SAlex Hornung static d_open_t dmopen; 555b279a20SAlex Hornung static d_close_t dmclose; 565b279a20SAlex Hornung static d_psize_t dmsize; 575b279a20SAlex Hornung static d_strategy_t dmstrategy; 5879b7159fSAlex Hornung static d_dump_t dmdump; 59ff56536eSAlex Hornung 60ff56536eSAlex Hornung /* attach and detach routines */ 61ff56536eSAlex Hornung void dmattach(int); 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); 1025b279a20SAlex Hornung 103ff56536eSAlex Hornung /* 104ff56536eSAlex Hornung * This array is used to translate cmd to function pointer. 105ff56536eSAlex Hornung * 106ff56536eSAlex Hornung * Interface between libdevmapper and lvm2tools uses different 107ff56536eSAlex Hornung * names for one IOCTL call because libdevmapper do another thing 108ff56536eSAlex Hornung * then. When I run "info" or "mknodes" libdevmapper will send same 109ff56536eSAlex Hornung * ioctl to kernel but will do another things in userspace. 110ff56536eSAlex Hornung * 111ff56536eSAlex Hornung */ 11261413047SAlex Hornung static struct cmd_function cmd_fn[] = { 113ff56536eSAlex Hornung { .cmd = "version", .fn = dm_get_version_ioctl}, 114ff56536eSAlex Hornung { .cmd = "targets", .fn = dm_list_versions_ioctl}, 115ff56536eSAlex Hornung { .cmd = "create", .fn = dm_dev_create_ioctl}, 116ff56536eSAlex Hornung { .cmd = "info", .fn = dm_dev_status_ioctl}, 117ff56536eSAlex Hornung { .cmd = "mknodes", .fn = dm_dev_status_ioctl}, 118ff56536eSAlex Hornung { .cmd = "names", .fn = dm_dev_list_ioctl}, 119ff56536eSAlex Hornung { .cmd = "suspend", .fn = dm_dev_suspend_ioctl}, 120ff56536eSAlex Hornung { .cmd = "remove", .fn = dm_dev_remove_ioctl}, 121ff56536eSAlex Hornung { .cmd = "rename", .fn = dm_dev_rename_ioctl}, 122ff56536eSAlex Hornung { .cmd = "resume", .fn = dm_dev_resume_ioctl}, 123ff56536eSAlex Hornung { .cmd = "clear", .fn = dm_table_clear_ioctl}, 124ff56536eSAlex Hornung { .cmd = "deps", .fn = dm_table_deps_ioctl}, 125ff56536eSAlex Hornung { .cmd = "reload", .fn = dm_table_load_ioctl}, 126ff56536eSAlex Hornung { .cmd = "status", .fn = dm_table_status_ioctl}, 127ff56536eSAlex Hornung { .cmd = "table", .fn = dm_table_status_ioctl}, 128ff56536eSAlex Hornung {NULL, NULL} 129ff56536eSAlex Hornung }; 130ff56536eSAlex Hornung 131ff56536eSAlex Hornung /* New module handle routine */ 132ff56536eSAlex Hornung static int 1335b279a20SAlex Hornung dm_modcmd(module_t mod, int cmd, void *unused) 134ff56536eSAlex Hornung { 135ff56536eSAlex Hornung int error, bmajor, cmajor; 136ff56536eSAlex Hornung 137ff56536eSAlex Hornung error = 0; 138ff56536eSAlex Hornung bmajor = -1; 139ff56536eSAlex Hornung cmajor = -1; 140ff56536eSAlex Hornung 141ff56536eSAlex Hornung switch (cmd) { 1425b279a20SAlex Hornung case MOD_LOAD: 143ff56536eSAlex Hornung dm_doinit(); 144e8e2bcdaSAlex Hornung kprintf("Device Mapper version %d.%d.%d loaded\n", 1455b279a20SAlex Hornung DM_VERSION_MAJOR, DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL); 146ff56536eSAlex Hornung break; 147ff56536eSAlex Hornung 1485b279a20SAlex Hornung case MOD_UNLOAD: 149ff56536eSAlex Hornung /* 150ff56536eSAlex Hornung * Disable unloading of dm module if there are any devices 151ff56536eSAlex Hornung * defined in driver. This is probably too strong we need 152ff56536eSAlex Hornung * to disable auto-unload only if there is mounted dm device 153ff56536eSAlex Hornung * present. 154ff56536eSAlex Hornung */ 155ff56536eSAlex Hornung if (dm_dev_counter > 0) 156ff56536eSAlex Hornung return EBUSY; 157ff56536eSAlex Hornung 158ff56536eSAlex Hornung error = dmdestroy(); 159ff56536eSAlex Hornung if (error) 160ff56536eSAlex Hornung break; 161e8e2bcdaSAlex Hornung kprintf("Device Mapper unloaded\n"); 162ff56536eSAlex Hornung break; 163ff56536eSAlex Hornung 164ff56536eSAlex Hornung default: 1655b279a20SAlex Hornung break; 166ff56536eSAlex Hornung } 167ff56536eSAlex Hornung 168ff56536eSAlex Hornung return error; 169ff56536eSAlex Hornung } 170ff56536eSAlex Hornung 171ff56536eSAlex Hornung /* 172ff56536eSAlex Hornung * dm_detach: 173ff56536eSAlex Hornung * 174ff56536eSAlex Hornung * Autoconfiguration detach function for pseudo-device glue. 175ff56536eSAlex Hornung * This routine is called by dm_ioctl::dm_dev_remove_ioctl and by autoconf to 176ff56536eSAlex Hornung * remove devices created in device-mapper. 177ff56536eSAlex Hornung */ 1785b279a20SAlex Hornung int 1795b279a20SAlex Hornung dm_detach(dm_dev_t *dmv) 180ff56536eSAlex Hornung { 1815b279a20SAlex Hornung disable_dev(dmv); 182ff56536eSAlex Hornung 183ff56536eSAlex Hornung /* Destroy active table first. */ 184ff56536eSAlex Hornung dm_table_destroy(&dmv->table_head, DM_TABLE_ACTIVE); 185ff56536eSAlex Hornung 186ff56536eSAlex Hornung /* Destroy inactive table if exits, too. */ 187ff56536eSAlex Hornung dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE); 188ff56536eSAlex Hornung 189ff56536eSAlex Hornung dm_table_head_destroy(&dmv->table_head); 190ff56536eSAlex Hornung 191*08cba728SAlex Hornung disk_destroy(dmv->diskp); 192*08cba728SAlex Hornung #if 0 1935b279a20SAlex Hornung destroy_dev(dmv->devt); 194*08cba728SAlex Hornung #endif 195ff56536eSAlex Hornung 196ff56536eSAlex Hornung /* Destroy device */ 197ff56536eSAlex Hornung (void)dm_dev_free(dmv); 198ff56536eSAlex Hornung 199ff56536eSAlex Hornung /* Decrement device counter After removing device */ 2005b279a20SAlex Hornung --dm_dev_counter; /* XXX: was atomic 64 */ 201ff56536eSAlex Hornung 202ff56536eSAlex Hornung return 0; 203ff56536eSAlex Hornung } 204ff56536eSAlex Hornung 205ff56536eSAlex Hornung static void 206ff56536eSAlex Hornung dm_doinit(void) 207ff56536eSAlex Hornung { 208ff56536eSAlex Hornung dm_target_init(); 209ff56536eSAlex Hornung dm_dev_init(); 210ff56536eSAlex Hornung dm_pdev_init(); 2115b279a20SAlex Hornung dmcdev = make_dev(&dm_ops, 0, UID_ROOT, GID_OPERATOR, 0640, "mapper/control"); 212ff56536eSAlex Hornung } 213ff56536eSAlex Hornung 214ff56536eSAlex Hornung /* Destroy routine */ 215ff56536eSAlex Hornung static int 216ff56536eSAlex Hornung dmdestroy(void) 217ff56536eSAlex Hornung { 2185b279a20SAlex Hornung destroy_dev(dmcdev); 219ff56536eSAlex Hornung 220ff56536eSAlex Hornung dm_dev_destroy(); 221ff56536eSAlex Hornung dm_pdev_destroy(); 222ff56536eSAlex Hornung dm_target_destroy(); 223ff56536eSAlex Hornung 224ff56536eSAlex Hornung return 0; 225ff56536eSAlex Hornung } 226ff56536eSAlex Hornung 227ff56536eSAlex Hornung static int 2285b279a20SAlex Hornung dmopen(struct dev_open_args *ap) 229ff56536eSAlex Hornung { 230ff56536eSAlex Hornung 2315b279a20SAlex Hornung aprint_debug("dm open routine called %" PRIu32 "\n", 2325b279a20SAlex Hornung minor(ap->a_head.a_dev)); 233ff56536eSAlex Hornung return 0; 234ff56536eSAlex Hornung } 235ff56536eSAlex Hornung 236ff56536eSAlex Hornung static int 2375b279a20SAlex Hornung dmclose(struct dev_close_args *ap) 238ff56536eSAlex Hornung { 239ff56536eSAlex Hornung 2405b279a20SAlex Hornung aprint_debug("dm close routine called %" PRIu32 "\n", 2415b279a20SAlex Hornung minor(ap->a_head.a_dev)); 242ff56536eSAlex Hornung return 0; 243ff56536eSAlex Hornung } 244ff56536eSAlex Hornung 245ff56536eSAlex Hornung 246ff56536eSAlex Hornung static int 2475b279a20SAlex Hornung dmioctl(struct dev_ioctl_args *ap) 248ff56536eSAlex Hornung { 2495b279a20SAlex Hornung cdev_t dev = ap->a_head.a_dev; 2505b279a20SAlex Hornung u_long cmd = ap->a_cmd; 2515b279a20SAlex Hornung void *data = ap->a_data; 2525b279a20SAlex Hornung 253ff56536eSAlex Hornung int r; 254ff56536eSAlex Hornung prop_dictionary_t dm_dict_in; 255ff56536eSAlex Hornung 256ff56536eSAlex Hornung r = 0; 257ff56536eSAlex Hornung 258ff56536eSAlex Hornung aprint_debug("dmioctl called\n"); 259ff56536eSAlex Hornung 2605b279a20SAlex Hornung KKASSERT(data != NULL); 261ff56536eSAlex Hornung 262ff56536eSAlex Hornung if (( r = disk_ioctl_switch(dev, cmd, data)) == ENOTTY) { 263ff56536eSAlex Hornung struct plistref *pref = (struct plistref *) data; 264ff56536eSAlex Hornung 265ff56536eSAlex Hornung /* Check if we were called with NETBSD_DM_IOCTL ioctl 266ff56536eSAlex Hornung otherwise quit. */ 267ff56536eSAlex Hornung if ((r = dm_ioctl_switch(cmd)) != 0) 268ff56536eSAlex Hornung return r; 269ff56536eSAlex Hornung 270ff56536eSAlex Hornung if((r = prop_dictionary_copyin_ioctl(pref, cmd, &dm_dict_in)) != 0) 271ff56536eSAlex Hornung return r; 272ff56536eSAlex Hornung 273ff56536eSAlex Hornung if ((r = dm_check_version(dm_dict_in)) != 0) 274ff56536eSAlex Hornung goto cleanup_exit; 275ff56536eSAlex Hornung 276ff56536eSAlex Hornung /* run ioctl routine */ 277ff56536eSAlex Hornung if ((r = dm_cmd_to_fun(dm_dict_in)) != 0) 278ff56536eSAlex Hornung goto cleanup_exit; 279ff56536eSAlex Hornung 280ff56536eSAlex Hornung cleanup_exit: 281ff56536eSAlex Hornung r = prop_dictionary_copyout_ioctl(pref, cmd, dm_dict_in); 282ff56536eSAlex Hornung prop_object_release(dm_dict_in); 283ff56536eSAlex Hornung } 284ff56536eSAlex Hornung 285ff56536eSAlex Hornung return r; 286ff56536eSAlex Hornung } 287ff56536eSAlex Hornung 288ff56536eSAlex Hornung /* 289ff56536eSAlex Hornung * Translate command sent from libdevmapper to func. 290ff56536eSAlex Hornung */ 291ff56536eSAlex Hornung static int 292ff56536eSAlex Hornung dm_cmd_to_fun(prop_dictionary_t dm_dict){ 293ff56536eSAlex Hornung int i, r; 294ff56536eSAlex Hornung prop_string_t command; 295ff56536eSAlex Hornung 296ff56536eSAlex Hornung r = 0; 297ff56536eSAlex Hornung 298ff56536eSAlex Hornung if ((command = prop_dictionary_get(dm_dict, DM_IOCTL_COMMAND)) == NULL) 299ff56536eSAlex Hornung return EINVAL; 300ff56536eSAlex Hornung 301ff56536eSAlex Hornung for(i = 0; cmd_fn[i].cmd != NULL; i++) 302ff56536eSAlex Hornung if (prop_string_equals_cstring(command, cmd_fn[i].cmd)) 303ff56536eSAlex Hornung break; 304ff56536eSAlex Hornung 305ff56536eSAlex Hornung if (cmd_fn[i].cmd == NULL) 306ff56536eSAlex Hornung return EINVAL; 307ff56536eSAlex Hornung 308ff56536eSAlex Hornung aprint_debug("ioctl %s called\n", cmd_fn[i].cmd); 309ff56536eSAlex Hornung r = cmd_fn[i].fn(dm_dict); 310ff56536eSAlex Hornung 311ff56536eSAlex Hornung return r; 312ff56536eSAlex Hornung } 313ff56536eSAlex Hornung 314ff56536eSAlex Hornung /* Call apropriate ioctl handler function. */ 315ff56536eSAlex Hornung static int 316ff56536eSAlex Hornung dm_ioctl_switch(u_long cmd) 317ff56536eSAlex Hornung { 318ff56536eSAlex Hornung 319ff56536eSAlex Hornung switch(cmd) { 320ff56536eSAlex Hornung 321ff56536eSAlex Hornung case NETBSD_DM_IOCTL: 322ff56536eSAlex Hornung aprint_debug("dm NetBSD_DM_IOCTL called\n"); 323ff56536eSAlex Hornung break; 324ff56536eSAlex Hornung default: 325ff56536eSAlex Hornung aprint_debug("dm unknown ioctl called\n"); 326ff56536eSAlex Hornung return ENOTTY; 327ff56536eSAlex Hornung break; /* NOT REACHED */ 328ff56536eSAlex Hornung } 329ff56536eSAlex Hornung 330ff56536eSAlex Hornung return 0; 331ff56536eSAlex Hornung } 332ff56536eSAlex Hornung 333ff56536eSAlex Hornung /* 334ff56536eSAlex Hornung * Check for disk specific ioctls. 335ff56536eSAlex Hornung */ 336ff56536eSAlex Hornung 337ff56536eSAlex Hornung static int 3385b279a20SAlex Hornung disk_ioctl_switch(cdev_t dev, u_long cmd, void *data) 339ff56536eSAlex Hornung { 340ff56536eSAlex Hornung dm_dev_t *dmv; 341ff56536eSAlex Hornung 342ff56536eSAlex Hornung /* disk ioctls make sense only on block devices */ 343ff56536eSAlex Hornung if (minor(dev) == 0) 344ff56536eSAlex Hornung return ENOTTY; 345ff56536eSAlex Hornung 346ff56536eSAlex Hornung switch(cmd) { 3475b279a20SAlex Hornung case DIOCGPART: 348ff56536eSAlex Hornung { 3495b279a20SAlex Hornung struct partinfo *dpart; 3505b279a20SAlex Hornung u_int64_t size; 3515b279a20SAlex Hornung dpart = (void *)data; 3525b279a20SAlex Hornung bzero(dpart, sizeof(*dpart)); 353ff56536eSAlex Hornung 354ff56536eSAlex Hornung if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL) 355ff56536eSAlex Hornung return ENODEV; 3565b279a20SAlex Hornung if (dmv->diskp->d_info.d_media_blksize == 0) { 357ff56536eSAlex Hornung dm_dev_unbusy(dmv); 358ff56536eSAlex Hornung return ENOTSUP; 3595b279a20SAlex Hornung } else { 3605b279a20SAlex Hornung size = dm_table_size(&dmv->table_head); 3615b279a20SAlex Hornung dpart->media_offset = 0; 3625b279a20SAlex Hornung dpart->media_size = size * DEV_BSIZE; 3635b279a20SAlex Hornung dpart->media_blocks = size; 3645b279a20SAlex Hornung dpart->media_blksize = DEV_BSIZE; 3655b279a20SAlex Hornung dpart->fstype = FS_BSDFFS; 3665b279a20SAlex Hornung } 367ff56536eSAlex Hornung dm_dev_unbusy(dmv); 368ff56536eSAlex Hornung break; 369ff56536eSAlex Hornung } 370ff56536eSAlex Hornung 371ff56536eSAlex Hornung default: 372ff56536eSAlex Hornung aprint_debug("unknown disk_ioctl called\n"); 373ff56536eSAlex Hornung return ENOTTY; 374ff56536eSAlex Hornung break; /* NOT REACHED */ 375ff56536eSAlex Hornung } 376ff56536eSAlex Hornung 377ff56536eSAlex Hornung return 0; 378ff56536eSAlex Hornung } 379ff56536eSAlex Hornung 380ff56536eSAlex Hornung /* 381ff56536eSAlex Hornung * Do all IO operations on dm logical devices. 382ff56536eSAlex Hornung */ 3835b279a20SAlex Hornung static int 3845b279a20SAlex Hornung dmstrategy(struct dev_strategy_args *ap) 385ff56536eSAlex Hornung { 3865b279a20SAlex Hornung cdev_t dev = ap->a_head.a_dev; 3875b279a20SAlex Hornung struct bio *bio = ap->a_bio; 3885b279a20SAlex Hornung struct buf *bp = bio->bio_buf; 3893adc52bcSMatthew Dillon int bypass; 3905b279a20SAlex Hornung 391ff56536eSAlex Hornung dm_dev_t *dmv; 392ff56536eSAlex Hornung dm_table_t *tbl; 393ff56536eSAlex Hornung dm_table_entry_t *table_en; 394ff56536eSAlex Hornung struct buf *nestbuf; 395ff56536eSAlex Hornung 396ff56536eSAlex Hornung uint32_t dev_type; 397ff56536eSAlex Hornung 398ff56536eSAlex Hornung uint64_t buf_start, buf_len, issued_len; 399ff56536eSAlex Hornung uint64_t table_start, table_end; 400ff56536eSAlex Hornung uint64_t start, end; 401ff56536eSAlex Hornung 4025b279a20SAlex Hornung buf_start = bio->bio_offset; 403ff56536eSAlex Hornung buf_len = bp->b_bcount; 404ff56536eSAlex Hornung 405ff56536eSAlex Hornung tbl = NULL; 406ff56536eSAlex Hornung 407ff56536eSAlex Hornung table_end = 0; 408ff56536eSAlex Hornung dev_type = 0; 409ff56536eSAlex Hornung issued_len = 0; 410ff56536eSAlex Hornung 4115b279a20SAlex Hornung if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL) { 412ff56536eSAlex Hornung bp->b_error = EIO; 413ff56536eSAlex Hornung bp->b_resid = bp->b_bcount; 4145b279a20SAlex Hornung biodone(bio); 4155b279a20SAlex Hornung return 0; 416ff56536eSAlex Hornung } 417ff56536eSAlex Hornung 4183adc52bcSMatthew Dillon switch(bp->b_cmd) { 4193adc52bcSMatthew Dillon case BUF_CMD_READ: 4203adc52bcSMatthew Dillon case BUF_CMD_WRITE: 4213adc52bcSMatthew Dillon case BUF_CMD_FREEBLKS: 4223adc52bcSMatthew Dillon bypass = 0; 4233adc52bcSMatthew Dillon break; 4243adc52bcSMatthew Dillon case BUF_CMD_FLUSH: 4253adc52bcSMatthew Dillon bypass = 1; 4263adc52bcSMatthew Dillon KKASSERT(buf_len == 0); 4273adc52bcSMatthew Dillon break; 4283adc52bcSMatthew Dillon default: 4293adc52bcSMatthew Dillon dm_dev_unbusy(dmv); 4303adc52bcSMatthew Dillon bp->b_error = EIO; 4313adc52bcSMatthew Dillon bp->b_resid = bp->b_bcount; 4323adc52bcSMatthew Dillon biodone(bio); 4333adc52bcSMatthew Dillon return 0; 4343adc52bcSMatthew Dillon } 4353adc52bcSMatthew Dillon 4363adc52bcSMatthew Dillon if (bypass == 0 && 4373adc52bcSMatthew Dillon bounds_check_with_mediasize(bio, DEV_BSIZE, 438ff56536eSAlex Hornung dm_table_size(&dmv->table_head)) <= 0) { 439ff56536eSAlex Hornung dm_dev_unbusy(dmv); 440ff56536eSAlex Hornung bp->b_resid = bp->b_bcount; 4415b279a20SAlex Hornung biodone(bio); 4425b279a20SAlex Hornung return 0; 443ff56536eSAlex Hornung } 444ff56536eSAlex Hornung 445ff56536eSAlex Hornung /* Select active table */ 446ff56536eSAlex Hornung tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE); 447ff56536eSAlex Hornung 4483adc52bcSMatthew Dillon nestiobuf_init(bio); 449ff56536eSAlex Hornung 450ff56536eSAlex Hornung /* 451ff56536eSAlex Hornung * Find out what tables I want to select. 452ff56536eSAlex Hornung */ 4533adc52bcSMatthew Dillon SLIST_FOREACH(table_en, tbl, next) { 454ff56536eSAlex Hornung /* 4553adc52bcSMatthew Dillon * I need need number of bytes not blocks. 456ff56536eSAlex Hornung */ 4573adc52bcSMatthew Dillon table_start = table_en->start * DEV_BSIZE; 458ff56536eSAlex Hornung table_end = table_start + (table_en->length) * DEV_BSIZE; 459ff56536eSAlex Hornung 4603adc52bcSMatthew Dillon /* 4613adc52bcSMatthew Dillon * Calculate the start and end 4623adc52bcSMatthew Dillon */ 463ff56536eSAlex Hornung start = MAX(table_start, buf_start); 464ff56536eSAlex Hornung end = MIN(table_end, buf_start + buf_len); 465ff56536eSAlex Hornung 466ff56536eSAlex Hornung aprint_debug("----------------------------------------\n"); 467ff56536eSAlex Hornung aprint_debug("table_start %010" PRIu64", table_end %010" 468ff56536eSAlex Hornung PRIu64 "\n", table_start, table_end); 469ff56536eSAlex Hornung aprint_debug("buf_start %010" PRIu64", buf_len %010" 470ff56536eSAlex Hornung PRIu64"\n", buf_start, buf_len); 471ff56536eSAlex Hornung aprint_debug("start-buf_start %010"PRIu64", end %010" 472ff56536eSAlex Hornung PRIu64"\n", start - buf_start, end); 473ff56536eSAlex Hornung aprint_debug("start %010" PRIu64" , end %010" 474ff56536eSAlex Hornung PRIu64"\n", start, end); 475ff56536eSAlex Hornung aprint_debug("\n----------------------------------------\n"); 476ff56536eSAlex Hornung 4773adc52bcSMatthew Dillon if (bypass) { 4785b279a20SAlex Hornung nestbuf = getpbuf(NULL); 479f6221ad1SMatthew Dillon nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS; 480ff56536eSAlex Hornung 4813adc52bcSMatthew Dillon nestiobuf_add(bio, nestbuf, 0, 0); 4823adc52bcSMatthew Dillon nestbuf->b_bio1.bio_offset = 0; 4833adc52bcSMatthew Dillon table_en->target->strategy(table_en, nestbuf); 4843adc52bcSMatthew Dillon } else if (start < end) { 4853adc52bcSMatthew Dillon nestbuf = getpbuf(NULL); 486f6221ad1SMatthew Dillon nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS; 487f6221ad1SMatthew Dillon 4883adc52bcSMatthew Dillon nestiobuf_add(bio, nestbuf, 4893adc52bcSMatthew Dillon start - buf_start, (end - start)); 490ff56536eSAlex Hornung issued_len += end - start; 491ff56536eSAlex Hornung 4925b279a20SAlex Hornung nestbuf->b_bio1.bio_offset = (start - table_start); 493ff56536eSAlex Hornung table_en->target->strategy(table_en, nestbuf); 494ff56536eSAlex Hornung } 495ff56536eSAlex Hornung } 496ff56536eSAlex Hornung 497ff56536eSAlex Hornung if (issued_len < buf_len) 4983adc52bcSMatthew Dillon nestiobuf_error(bio, EINVAL); 4993adc52bcSMatthew Dillon nestiobuf_start(bio); 500ff56536eSAlex Hornung dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE); 501ff56536eSAlex Hornung dm_dev_unbusy(dmv); 502ff56536eSAlex Hornung 5035b279a20SAlex Hornung return 0; 504ff56536eSAlex Hornung } 505ff56536eSAlex Hornung 506ff56536eSAlex Hornung static int 50779b7159fSAlex Hornung dmdump(struct dev_dump_args *ap) 50879b7159fSAlex Hornung { 50979b7159fSAlex Hornung cdev_t dev = ap->a_head.a_dev; 51079b7159fSAlex Hornung dm_dev_t *dmv; 51179b7159fSAlex Hornung dm_table_t *tbl; 51279b7159fSAlex Hornung dm_table_entry_t *table_en; 51379b7159fSAlex Hornung uint32_t dev_type; 51479b7159fSAlex Hornung uint64_t buf_start, buf_len, issued_len; 51579b7159fSAlex Hornung uint64_t table_start, table_end; 51679b7159fSAlex Hornung uint64_t start, end, data_offset; 51779b7159fSAlex Hornung off_t offset; 51879b7159fSAlex Hornung size_t length; 51979b7159fSAlex Hornung int error = 0; 52079b7159fSAlex Hornung 52179b7159fSAlex Hornung buf_start = ap->a_offset; 52279b7159fSAlex Hornung buf_len = ap->a_length; 52379b7159fSAlex Hornung 52479b7159fSAlex Hornung tbl = NULL; 52579b7159fSAlex Hornung 52679b7159fSAlex Hornung table_end = 0; 52779b7159fSAlex Hornung dev_type = 0; 52879b7159fSAlex Hornung issued_len = 0; 52979b7159fSAlex Hornung 53079b7159fSAlex Hornung if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL) { 53179b7159fSAlex Hornung return EIO; 53279b7159fSAlex Hornung } 53379b7159fSAlex Hornung 53479b7159fSAlex Hornung /* Select active table */ 53579b7159fSAlex Hornung tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE); 53679b7159fSAlex Hornung 53779b7159fSAlex Hornung 53879b7159fSAlex Hornung /* 53979b7159fSAlex Hornung * Find out what tables I want to select. 54079b7159fSAlex Hornung */ 54179b7159fSAlex Hornung SLIST_FOREACH(table_en, tbl, next) { 54279b7159fSAlex Hornung /* 54379b7159fSAlex Hornung * I need need number of bytes not blocks. 54479b7159fSAlex Hornung */ 54579b7159fSAlex Hornung table_start = table_en->start * DEV_BSIZE; 54679b7159fSAlex Hornung table_end = table_start + (table_en->length) * DEV_BSIZE; 54779b7159fSAlex Hornung 54879b7159fSAlex Hornung /* 54979b7159fSAlex Hornung * Calculate the start and end 55079b7159fSAlex Hornung */ 55179b7159fSAlex Hornung start = MAX(table_start, buf_start); 55279b7159fSAlex Hornung end = MIN(table_end, buf_start + buf_len); 55379b7159fSAlex Hornung 55479b7159fSAlex Hornung if (ap->a_length == 0) { 55579b7159fSAlex Hornung if (table_en->target->dump == NULL) { 55679b7159fSAlex Hornung error = ENXIO; 55779b7159fSAlex Hornung goto out; 55879b7159fSAlex Hornung } 55979b7159fSAlex Hornung 56079b7159fSAlex Hornung table_en->target->dump(table_en, NULL, 0, 0); 56179b7159fSAlex Hornung } else if (start < end) { 56279b7159fSAlex Hornung data_offset = start - buf_start; 56379b7159fSAlex Hornung offset = start - table_start; 56479b7159fSAlex Hornung length = end - start; 56579b7159fSAlex Hornung 56679b7159fSAlex Hornung if (table_en->target->dump == NULL) { 56779b7159fSAlex Hornung error = ENXIO; 56879b7159fSAlex Hornung goto out; 56979b7159fSAlex Hornung } 57079b7159fSAlex Hornung 57179b7159fSAlex Hornung table_en->target->dump(table_en, 57279b7159fSAlex Hornung (char *)ap->a_virtual + data_offset, 57379b7159fSAlex Hornung length, offset); 57479b7159fSAlex Hornung 57579b7159fSAlex Hornung issued_len += end - start; 57679b7159fSAlex Hornung } 57779b7159fSAlex Hornung } 57879b7159fSAlex Hornung 57979b7159fSAlex Hornung if (issued_len < buf_len) 58079b7159fSAlex Hornung error = EINVAL; 58179b7159fSAlex Hornung 58279b7159fSAlex Hornung out: 58379b7159fSAlex Hornung dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE); 58479b7159fSAlex Hornung dm_dev_unbusy(dmv); 58579b7159fSAlex Hornung 58679b7159fSAlex Hornung return error; 58779b7159fSAlex Hornung } 58879b7159fSAlex Hornung 58979b7159fSAlex Hornung static int 5905b279a20SAlex Hornung dmsize(struct dev_psize_args *ap) 591ff56536eSAlex Hornung { 5925b279a20SAlex Hornung cdev_t dev = ap->a_head.a_dev; 593ff56536eSAlex Hornung dm_dev_t *dmv; 594ff56536eSAlex Hornung uint64_t size; 595ff56536eSAlex Hornung 596ff56536eSAlex Hornung size = 0; 597ff56536eSAlex Hornung 598ff56536eSAlex Hornung if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL) 59927d04cb3SAlex Hornung return ENOENT; 600ff56536eSAlex Hornung 601ff56536eSAlex Hornung size = dm_table_size(&dmv->table_head); 602ff56536eSAlex Hornung dm_dev_unbusy(dmv); 603ff56536eSAlex Hornung 60427d04cb3SAlex Hornung ap->a_result = (int64_t)size; 60527d04cb3SAlex Hornung 60627d04cb3SAlex Hornung return 0; 607ff56536eSAlex Hornung } 608ff56536eSAlex Hornung 6095b279a20SAlex Hornung #if 0 610ff56536eSAlex Hornung static void 611ff56536eSAlex Hornung dmminphys(struct buf *bp) 612ff56536eSAlex Hornung { 613ff56536eSAlex Hornung 614ff56536eSAlex Hornung bp->b_bcount = MIN(bp->b_bcount, MAXPHYS); 615ff56536eSAlex Hornung } 6165b279a20SAlex Hornung #endif 6175b279a20SAlex Hornung 6185b279a20SAlex Hornung void 6195b279a20SAlex Hornung dmsetdiskinfo(struct disk *disk, dm_table_head_t *head) 6205b279a20SAlex Hornung { 6215b279a20SAlex Hornung struct disk_info info; 6225b279a20SAlex Hornung int dmp_size; 6235b279a20SAlex Hornung 6245b279a20SAlex Hornung dmp_size = dm_table_size(head); 6255b279a20SAlex Hornung 6265b279a20SAlex Hornung bzero(&info, sizeof(struct disk_info)); 6275b279a20SAlex Hornung info.d_media_blksize = DEV_BSIZE; 6285b279a20SAlex Hornung info.d_media_blocks = dmp_size; 629*08cba728SAlex Hornung #if 0 630*08cba728SAlex Hornung /* XXX: this is set by disk_setdiskinfo */ 6315b279a20SAlex Hornung info.d_media_size = dmp_size * DEV_BSIZE; 632*08cba728SAlex Hornung #endif 6335b279a20SAlex Hornung info.d_dsflags = DSO_MBRQUIET; /* XXX */ 6345b279a20SAlex Hornung info.d_secpertrack = 32; 6355b279a20SAlex Hornung info.d_nheads = 64; 6365b279a20SAlex Hornung info.d_secpercyl = info.d_secpertrack * info.d_nheads; 6375b279a20SAlex Hornung info.d_ncylinders = dmp_size / 2048; 638*08cba728SAlex Hornung 639*08cba728SAlex Hornung disk_setdiskinfo(disk, &info); 6405b279a20SAlex Hornung } 6415b279a20SAlex Hornung 6425b279a20SAlex Hornung prop_dictionary_t 6435b279a20SAlex Hornung dmgetdiskinfo(struct disk *disk) 6445b279a20SAlex Hornung { 6455b279a20SAlex Hornung prop_dictionary_t disk_info, geom; 6465b279a20SAlex Hornung struct disk_info *pinfo; 6475b279a20SAlex Hornung 6485b279a20SAlex Hornung pinfo = &disk->d_info; 6495b279a20SAlex Hornung 6505b279a20SAlex Hornung disk_info = prop_dictionary_create(); 6515b279a20SAlex Hornung geom = prop_dictionary_create(); 6525b279a20SAlex Hornung 6535b279a20SAlex Hornung prop_dictionary_set_cstring_nocopy(disk_info, "type", "ESDI"); 6545b279a20SAlex Hornung prop_dictionary_set_uint64(geom, "sectors-per-unit", pinfo->d_media_blocks); 6555b279a20SAlex Hornung prop_dictionary_set_uint32(geom, "sector-size", 6565b279a20SAlex Hornung DEV_BSIZE /* XXX 512? */); 6575b279a20SAlex Hornung prop_dictionary_set_uint32(geom, "sectors-per-track", 32); 6585b279a20SAlex Hornung prop_dictionary_set_uint32(geom, "tracks-per-cylinder", 64); 6595b279a20SAlex Hornung prop_dictionary_set_uint32(geom, "cylinders-per-unit", 6605b279a20SAlex Hornung pinfo->d_media_blocks / 2048); 6615b279a20SAlex Hornung prop_dictionary_set(disk_info, "geometry", geom); 6625b279a20SAlex Hornung prop_object_release(geom); 6635b279a20SAlex Hornung 6645b279a20SAlex Hornung return disk_info; 6655b279a20SAlex Hornung } 666ff56536eSAlex Hornung 667ff56536eSAlex Hornung void 668ff56536eSAlex Hornung dmgetproperties(struct disk *disk, dm_table_head_t *head) 669ff56536eSAlex Hornung { 6705b279a20SAlex Hornung #if 0 671ff56536eSAlex Hornung prop_dictionary_t disk_info, odisk_info, geom; 672ff56536eSAlex Hornung int dmp_size; 673ff56536eSAlex Hornung 674ff56536eSAlex Hornung dmp_size = dm_table_size(head); 675ff56536eSAlex Hornung disk_info = prop_dictionary_create(); 676ff56536eSAlex Hornung geom = prop_dictionary_create(); 677ff56536eSAlex Hornung 678ff56536eSAlex Hornung prop_dictionary_set_cstring_nocopy(disk_info, "type", "ESDI"); 679ff56536eSAlex Hornung prop_dictionary_set_uint64(geom, "sectors-per-unit", dmp_size); 680ff56536eSAlex Hornung prop_dictionary_set_uint32(geom, "sector-size", 681ff56536eSAlex Hornung DEV_BSIZE /* XXX 512? */); 682ff56536eSAlex Hornung prop_dictionary_set_uint32(geom, "sectors-per-track", 32); 683ff56536eSAlex Hornung prop_dictionary_set_uint32(geom, "tracks-per-cylinder", 64); 684ff56536eSAlex Hornung prop_dictionary_set_uint32(geom, "cylinders-per-unit", dmp_size / 2048); 685ff56536eSAlex Hornung prop_dictionary_set(disk_info, "geometry", geom); 686ff56536eSAlex Hornung prop_object_release(geom); 687ff56536eSAlex Hornung 688ff56536eSAlex Hornung odisk_info = disk->dk_info; 689ff56536eSAlex Hornung disk->dk_info = disk_info; 690ff56536eSAlex Hornung 691ff56536eSAlex Hornung if (odisk_info != NULL) 692ff56536eSAlex Hornung prop_object_release(odisk_info); 6935b279a20SAlex Hornung #endif 694ff56536eSAlex Hornung } 695aadb5a11SAlex Hornung 696aadb5a11SAlex Hornung TUNABLE_INT("debug.dm_debug", &dm_debug_level); 697aadb5a11SAlex Hornung SYSCTL_INT(_debug, OID_AUTO, dm_debug, CTLFLAG_RW, &dm_debug_level, 698aadb5a11SAlex Hornung 0, "Eanble device mapper debugging"); 699aadb5a11SAlex Hornung 700