1 /* $NetBSD: libdm_netbsd.c,v 1.6 2010/12/23 17:44:33 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1996, 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Adam Hamsik. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 33 #include <sys/ioctl.h> 34 #include <sys/types.h> 35 #include <sys/sysctl.h> 36 37 #include <err.h> 38 #include <errno.h> 39 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <unistd.h> 43 #include <stdbool.h> 44 45 #include <dev/dm/netbsd-dm.h> 46 47 #include <dm-ioctl.h> 48 49 #include "lib.h" 50 #include "libdm-netbsd.h" 51 52 #define DMI_SIZE 16 * 1024 53 54 static int dm_list_versions(prop_dictionary_t, struct dm_ioctl *); 55 static int dm_list_devices(prop_dictionary_t, struct dm_ioctl *); 56 static int dm_dev_deps(prop_dictionary_t, struct dm_ioctl *); 57 static int dm_table_status(prop_dictionary_t, struct dm_ioctl *); 58 59 int 60 nbsd_get_dm_major(uint32_t *major, int type) 61 { 62 size_t val_len,i; 63 struct kinfo_drivers *kd; 64 65 if (sysctlbyname("kern.drivers",NULL,&val_len,NULL,0) < 0) { 66 printf("sysctlbyname failed"); 67 return 0; 68 } 69 70 if ((kd = malloc (val_len)) == NULL){ 71 printf("malloc kd info error\n"); 72 return 0; 73 } 74 75 if (sysctlbyname("kern.drivers", kd, &val_len, NULL, 0) < 0) { 76 printf("sysctlbyname failed kd"); 77 return 0; 78 } 79 80 for (i = 0, val_len /= sizeof(*kd); i < val_len; i++) { 81 82 if (strncmp(kd[i].d_name,DM_NAME,strlen(kd[i].d_name)) == 0){ 83 84 if (type == DM_CHAR_MAJOR) 85 /* Set major to dm-driver char major number. */ 86 *major = kd[i].d_cmajor; 87 else 88 if (type == DM_BLOCK_MAJOR) 89 *major = kd[i].d_bmajor; 90 91 free(kd); 92 93 return 1; 94 } 95 } 96 97 free(kd); 98 99 return 0; 100 } 101 102 int 103 nbsd_dmi_add_version(const int *version, prop_dictionary_t dm_dict) 104 { 105 prop_array_t ver; 106 size_t i; 107 108 if ((ver = prop_array_create()) == NULL) 109 return -1; 110 111 for (i=0;i<3;i++) 112 prop_array_set_uint32(ver,i,version[i]); 113 114 if ((prop_dictionary_set(dm_dict,"version",ver)) == false) 115 return -1; 116 117 prop_object_release(ver); 118 119 return 0; 120 } 121 122 struct dm_ioctl* 123 nbsd_dm_dict_to_dmi(prop_dictionary_t dm_dict,const int cmd) 124 { 125 struct dm_ioctl *dmi; 126 prop_array_t ver; 127 128 size_t i; 129 int r; 130 char *name, *uuid; 131 uint32_t major,minor; 132 133 name = NULL; 134 uuid = NULL; 135 minor = 0; 136 137 nbsd_get_dm_major(&major, DM_BLOCK_MAJOR); 138 139 if (!(dmi = dm_malloc(DMI_SIZE))) 140 return NULL; 141 142 memset(dmi,0,DMI_SIZE); 143 144 prop_dictionary_get_int32(dm_dict, DM_IOCTL_OPEN, &dmi->open_count); 145 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_EVENT, &dmi->event_nr); 146 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &dmi->flags); 147 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_TARGET_COUNT, 148 &dmi->target_count); 149 150 if (prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor)) 151 dmi->dev = MKDEV(major, minor); 152 else 153 dmi->dev = 0; 154 155 /* Copy name and uuid to dm_ioctl. */ 156 if (prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, 157 (const char **)&name)){ 158 strlcpy(dmi->name, name, DM_NAME_LEN); 159 } else 160 dmi->name[0] = '\0'; 161 162 if (prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, 163 (const char **)&uuid)){ 164 strlcpy(dmi->uuid, uuid, DM_UUID_LEN); 165 } else 166 dmi->uuid[0] = '\0'; 167 168 /* dmi parsing values, size of dmi block and offset to data. */ 169 dmi->data_size = DMI_SIZE; 170 dmi->data_start = sizeof(struct dm_ioctl); 171 172 /* Get kernel version from dm_dict. */ 173 ver = prop_dictionary_get(dm_dict,DM_IOCTL_VERSION); 174 175 for(i=0; i<3; i++) 176 prop_array_get_uint32(ver,i,&dmi->version[i]); 177 178 switch (cmd){ 179 180 case DM_LIST_VERSIONS: 181 r = dm_list_versions(dm_dict,dmi); 182 if (r >= 0) 183 dmi->target_count = r; 184 break; 185 186 case DM_LIST_DEVICES: 187 r = dm_list_devices(dm_dict,dmi); 188 if (r >= 0) 189 dmi->target_count = r; 190 break; 191 192 case DM_TABLE_STATUS: 193 r = dm_table_status(dm_dict,dmi); 194 if (r >= 0) 195 dmi->target_count = r; 196 break; 197 198 case DM_TABLE_DEPS: 199 r = dm_dev_deps(dm_dict,dmi); 200 if (r >= 0) 201 dmi->target_count = r; 202 break; 203 } 204 205 return dmi; 206 } 207 208 /* 209 * Parse dm_dict when targets command was called and fill dm_ioctl buffer with it. 210 * 211 * Return number of targets or if failed <0 error. 212 */ 213 214 static int 215 dm_list_versions(prop_dictionary_t dm_dict, struct dm_ioctl *dmi) 216 { 217 struct dm_target_versions *dmtv,*odmtv; 218 219 prop_array_t targets,ver; 220 prop_dictionary_t target_dict; 221 prop_object_iterator_t iter; 222 223 char *name; 224 size_t j,i,slen,rec_size; 225 226 odmtv = NULL; 227 name = NULL; 228 j = 0; 229 230 dmtv = (struct dm_target_versions *)((uint8_t *)dmi + dmi->data_start); 231 232 /* printf("dmi: vers: %d.%d.%d data_size: %d data_start: %d name: %s t_count: %d\n", 233 dmi->version[0],dmi->version[1],dmi->version[2],dmi->data_size,dmi->data_start, 234 dmi->name,dmi->target_count); 235 236 printf("dmi: size: %d -- %p --- %p \n",sizeof(struct dm_ioctl),dmi,dmi+dmi->data_start); 237 printf("dmtv: size: %p --- %p\n",dmtv,(struct dm_target_versions *)(dmi+312));*/ 238 239 /* get prop_array of target_version dictionaries */ 240 if ((targets = prop_dictionary_get(dm_dict,DM_IOCTL_CMD_DATA))){ 241 242 iter = prop_array_iterator(targets); 243 if (!iter) 244 err(EXIT_FAILURE,"dm_list_versions %s",__func__); 245 246 while((target_dict = prop_object_iterator_next(iter)) != NULL){ 247 j++; 248 249 prop_dictionary_get_cstring_nocopy(target_dict, 250 DM_TARGETS_NAME,(const char **)&name); 251 252 slen = strlen(name) + 1; 253 rec_size = sizeof(struct dm_target_versions) + slen + 1; 254 255 if (rec_size > dmi->data_size) 256 return -ENOMEM; 257 258 ver = prop_dictionary_get(target_dict,DM_TARGETS_VERSION); 259 260 for (i=0; i<3; i++) 261 prop_array_get_uint32(ver,i,&dmtv->version[i]); 262 263 dmtv->next = rec_size; 264 265 strlcpy(dmtv->name,name,slen); 266 267 odmtv = dmtv; 268 269 dmtv =(struct dm_target_versions *)((uint8_t *)dmtv + rec_size); 270 } 271 272 if (odmtv != NULL) 273 odmtv->next = 0; 274 } 275 276 prop_object_iterator_release(iter); 277 return j; 278 } 279 280 /* 281 * List all available dm devices in system. 282 */ 283 static int 284 dm_list_devices(prop_dictionary_t dm_dict, struct dm_ioctl *dmi) 285 { 286 struct dm_name_list *dml,*odml; 287 288 prop_array_t targets; 289 prop_dictionary_t target_dict; 290 prop_object_iterator_t iter; 291 292 uint32_t minor; 293 uint32_t major; 294 295 char *name; 296 size_t j,slen,rec_size; 297 298 odml = NULL; 299 name = NULL; 300 minor = 0; 301 j = 0; 302 303 nbsd_get_dm_major(&major,DM_BLOCK_MAJOR); 304 305 dml = (struct dm_name_list *)((uint8_t *)dmi + dmi->data_start); 306 307 if ((targets = prop_dictionary_get(dm_dict,DM_IOCTL_CMD_DATA))){ 308 309 iter = prop_array_iterator(targets); 310 if (!iter) 311 err(EXIT_FAILURE,"dm_list_devices %s",__func__); 312 313 while((target_dict = prop_object_iterator_next(iter)) != NULL){ 314 315 prop_dictionary_get_cstring_nocopy(target_dict, 316 DM_DEV_NAME,(const char **)&name); 317 318 prop_dictionary_get_uint32(target_dict,DM_DEV_DEV,&minor); 319 320 dml->dev = MKDEV(major,minor); 321 322 slen = strlen(name) + 1; 323 rec_size = sizeof(struct dm_name_list) + slen + 1; 324 325 if (rec_size > dmi->data_size) 326 return -ENOMEM; 327 328 dml->next = rec_size; 329 330 strlcpy(dml->name,name,slen); 331 332 odml = dml; 333 334 dml =(struct dm_name_list *)((uint8_t *)dml + rec_size); 335 336 j++; 337 } 338 339 if (odml != NULL) 340 odml->next = 0; 341 } 342 prop_object_iterator_release(iter); 343 return j; 344 } 345 346 /* 347 * Print status of each table, target arguments, start sector, 348 * size and target name. 349 */ 350 static int 351 dm_table_status(prop_dictionary_t dm_dict,struct dm_ioctl *dmi) 352 { 353 struct dm_target_spec *dmts, *odmts; 354 355 prop_array_t targets; 356 prop_dictionary_t target_dict; 357 prop_object_iterator_t iter; 358 359 char *type,*params,*params_start; 360 361 bool prm; 362 size_t j,plen,rec_size,next; 363 364 j = 0; 365 next = 0; 366 params = NULL; 367 odmts = NULL; 368 rec_size = 0; 369 plen = -1; 370 prm = false; 371 372 dmts = (struct dm_target_spec *)((uint8_t *)dmi + dmi->data_start); 373 374 if ((targets = prop_dictionary_get(dm_dict,DM_IOCTL_CMD_DATA))){ 375 376 iter = prop_array_iterator(targets); 377 if (!iter) 378 err(EXIT_FAILURE,"dm_table_status %s",__func__); 379 380 while((target_dict = prop_object_iterator_next(iter)) != NULL){ 381 382 prop_dictionary_get_cstring_nocopy(target_dict, 383 DM_TABLE_TYPE,(const char **)&type); 384 385 prm = prop_dictionary_get_cstring_nocopy(target_dict, 386 DM_TABLE_PARAMS,(const char **)¶ms); 387 388 prop_dictionary_get_uint64(target_dict,DM_TABLE_START,&dmts->sector_start); 389 prop_dictionary_get_uint64(target_dict,DM_TABLE_LENGTH,&dmts->length); 390 prop_dictionary_get_int32(target_dict,DM_TABLE_STAT,&dmts->status); 391 392 if (prm) 393 plen = strlen(params) + 1; 394 395 rec_size = sizeof(struct dm_target_spec) + plen; 396 397 /* 398 * In linux when copying table status from kernel next is 399 * number of bytes from the start of the first dm_target_spec 400 * structure. I don't know why but, it has to be done this way. 401 */ 402 next += rec_size; 403 404 if (rec_size > dmi->data_size) 405 return -ENOMEM; 406 407 dmts->next = next; 408 409 strlcpy(dmts->target_type, type, DM_MAX_TYPE_NAME); 410 411 params_start = (char *)dmts + sizeof(struct dm_target_spec); 412 413 if (prm) 414 strlcpy(params_start, params, plen); 415 else 416 params_start = "\0"; 417 418 419 odmts = dmts; 420 421 dmts = (struct dm_target_spec *)((uint8_t *)dmts + rec_size); 422 423 j++; 424 425 } 426 427 if (odmts != NULL) 428 odmts->next = 0; 429 } 430 prop_object_iterator_release(iter); 431 432 return j; 433 } 434 435 /* 436 * Print dm device dependiences, get minor/major number for 437 * devices. From kernel I will receive major:minor number of 438 * block device used with target. I have to translate it to 439 * raw device numbers and use them, because all other parts of lvm2 440 * uses raw devices internaly. 441 */ 442 static int 443 dm_dev_deps(prop_dictionary_t dm_dict, struct dm_ioctl *dmi) 444 { 445 struct dm_target_deps *dmtd; 446 struct kinfo_drivers *kd; 447 448 prop_array_t targets; 449 prop_object_iterator_t iter; 450 451 uint32_t major; 452 453 size_t val_len, i, j; 454 455 uint64_t dev_tmp; 456 457 dev_tmp = 0; 458 j = 0; 459 i = 0; 460 461 if (sysctlbyname("kern.drivers",NULL,&val_len,NULL,0) < 0) { 462 printf("sysctlbyname failed"); 463 return 0; 464 } 465 466 if ((kd = malloc (val_len)) == NULL){ 467 printf("malloc kd info error\n"); 468 return 0; 469 } 470 471 if (sysctlbyname("kern.drivers", kd, &val_len, NULL, 0) < 0) { 472 printf("sysctlbyname failed kd"); 473 return 0; 474 } 475 476 dmtd = (struct dm_target_deps *)((uint8_t *)dmi + dmi->data_start); 477 478 if ((targets = prop_dictionary_get(dm_dict, DM_IOCTL_CMD_DATA))){ 479 480 iter = prop_array_iterator(targets); 481 if (!iter) 482 err(EXIT_FAILURE,"dm_target_deps %s", __func__); 483 484 while((prop_object_iterator_next(iter)) != NULL){ 485 486 prop_array_get_uint64(targets, j, &dev_tmp); 487 488 for (i = 0, val_len /= sizeof(*kd); i < val_len; i++){ 489 if (kd[i].d_bmajor == MAJOR(dev_tmp)) { 490 major = kd[i].d_cmajor; 491 break; 492 } 493 } 494 495 dmtd->dev[j] = MKDEV(major,MINOR(dev_tmp)); 496 497 j++; 498 } 499 } 500 501 dmtd->count = j; 502 503 prop_object_iterator_release(iter); 504 505 return j; 506 } 507