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