1 /* $NetBSD: libdm_netbsd.c,v 1.5 2009/12/05 11:42:24 haad 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 <sys/types.h> 44 #include <sys/stat.h> 45 46 #include <dev/disk/dm/netbsd-dm.h> /* XXX */ 47 48 #include <dm-ioctl.h> 49 50 #include "lib.h" 51 #include "libdm-netbsd.h" 52 53 #define DMI_SIZE 16 * 1024 54 55 static int dm_list_versions(prop_dictionary_t, struct dm_ioctl *); 56 static int dm_list_devices(prop_dictionary_t, struct dm_ioctl *); 57 static int dm_dev_deps(prop_dictionary_t, struct dm_ioctl *); 58 static int dm_table_status(prop_dictionary_t, struct dm_ioctl *); 59 60 int 61 nbsd_get_dm_major(uint32_t *major, int type) 62 { 63 struct stat sb; 64 if (stat("/dev/mapper/control", &sb) < 0) { 65 printf("stat failed\n"); 66 return 0; 67 } 68 *major = major(sb.st_dev); 69 70 return 1; 71 } 72 73 int 74 nbsd_dmi_add_version(const int *version, prop_dictionary_t dm_dict) 75 { 76 prop_array_t ver; 77 size_t i; 78 79 if ((ver = prop_array_create()) == NULL) 80 return -1; 81 82 for (i=0;i<3;i++) 83 prop_array_set_uint32(ver,i,version[i]); 84 85 if ((prop_dictionary_set(dm_dict,"version",ver)) == false) 86 return -1; 87 88 prop_object_release(ver); 89 90 return 0; 91 } 92 93 struct dm_ioctl* 94 nbsd_dm_dict_to_dmi(prop_dictionary_t dm_dict,const int cmd) 95 { 96 struct dm_ioctl *dmi; 97 prop_array_t ver; 98 99 size_t i; 100 int r; 101 char *name, *uuid; 102 uint32_t major,minor; 103 104 name = NULL; 105 uuid = NULL; 106 minor = 0; 107 108 nbsd_get_dm_major(&major, DM_BLOCK_MAJOR); 109 110 if (!(dmi = dm_malloc(DMI_SIZE))) 111 return NULL; 112 113 memset(dmi,0,DMI_SIZE); 114 115 prop_dictionary_get_int32(dm_dict, DM_IOCTL_OPEN, &dmi->open_count); 116 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_EVENT, &dmi->event_nr); 117 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &dmi->flags); 118 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_TARGET_COUNT, 119 &dmi->target_count); 120 121 if (prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor)) 122 dmi->dev = MKDEV(major, minor); 123 else 124 dmi->dev = 0; 125 126 /* Copy name and uuid to dm_ioctl. */ 127 if (prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, 128 (const char **)&name)){ 129 strlcpy(dmi->name, name, DM_NAME_LEN); 130 } else 131 dmi->name[0] = '\0'; 132 133 if (prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, 134 (const char **)&uuid)){ 135 strlcpy(dmi->uuid, uuid, DM_UUID_LEN); 136 } else 137 dmi->uuid[0] = '\0'; 138 139 /* dmi parsing values, size of dmi block and offset to data. */ 140 dmi->data_size = DMI_SIZE; 141 dmi->data_start = sizeof(struct dm_ioctl); 142 143 /* Get kernel version from dm_dict. */ 144 ver = prop_dictionary_get(dm_dict,DM_IOCTL_VERSION); 145 146 for(i=0; i<3; i++) 147 prop_array_get_uint32(ver,i,&dmi->version[i]); 148 149 switch (cmd){ 150 151 case DM_LIST_VERSIONS: 152 r = dm_list_versions(dm_dict,dmi); 153 if (r >= 0) 154 dmi->target_count = r; 155 break; 156 157 case DM_LIST_DEVICES: 158 r = dm_list_devices(dm_dict,dmi); 159 if (r >= 0) 160 dmi->target_count = r; 161 break; 162 163 case DM_TABLE_STATUS: 164 r = dm_table_status(dm_dict,dmi); 165 if (r >= 0) 166 dmi->target_count = r; 167 break; 168 169 case DM_TABLE_DEPS: 170 r = dm_dev_deps(dm_dict,dmi); 171 if (r >= 0) 172 dmi->target_count = r; 173 break; 174 } 175 176 return dmi; 177 } 178 179 /* 180 * Parse dm_dict when targets command was called and fill dm_ioctl buffer with it. 181 * 182 * Return number of targets or if failed <0 error. 183 */ 184 185 static int 186 dm_list_versions(prop_dictionary_t dm_dict, struct dm_ioctl *dmi) 187 { 188 struct dm_target_versions *dmtv,*odmtv; 189 190 prop_array_t targets,ver; 191 prop_dictionary_t target_dict; 192 prop_object_iterator_t iter; 193 194 char *name; 195 size_t j,i,slen,rec_size; 196 197 odmtv = NULL; 198 name = NULL; 199 j = 0; 200 201 dmtv = (struct dm_target_versions *)((uint8_t *)dmi + dmi->data_start); 202 203 /* printf("dmi: vers: %d.%d.%d data_size: %d data_start: %d name: %s t_count: %d\n", 204 dmi->version[0],dmi->version[1],dmi->version[2],dmi->data_size,dmi->data_start, 205 dmi->name,dmi->target_count); 206 207 printf("dmi: size: %d -- %p --- %p \n",sizeof(struct dm_ioctl),dmi,dmi+dmi->data_start); 208 printf("dmtv: size: %p --- %p\n",dmtv,(struct dm_target_versions *)(dmi+312));*/ 209 210 /* get prop_array of target_version dictionaries */ 211 if ((targets = prop_dictionary_get(dm_dict,DM_IOCTL_CMD_DATA))){ 212 213 iter = prop_array_iterator(targets); 214 if (!iter) 215 err(EXIT_FAILURE,"dm_list_versions %s",__func__); 216 217 while((target_dict = prop_object_iterator_next(iter)) != NULL){ 218 j++; 219 220 prop_dictionary_get_cstring_nocopy(target_dict, 221 DM_TARGETS_NAME,(const char **)&name); 222 223 slen = strlen(name) + 1; 224 rec_size = sizeof(struct dm_target_versions) + slen + 1; 225 226 if (rec_size > dmi->data_size) 227 return -ENOMEM; 228 229 ver = prop_dictionary_get(target_dict,DM_TARGETS_VERSION); 230 231 for (i=0; i<3; i++) 232 prop_array_get_uint32(ver,i,&dmtv->version[i]); 233 234 dmtv->next = rec_size; 235 236 strlcpy(dmtv->name,name,slen); 237 238 odmtv = dmtv; 239 240 dmtv =(struct dm_target_versions *)((uint8_t *)dmtv + rec_size); 241 } 242 243 if (odmtv != NULL) 244 odmtv->next = 0; 245 } 246 247 prop_object_iterator_release(iter); 248 return j; 249 } 250 251 /* 252 * List all available dm devices in system. 253 */ 254 static int 255 dm_list_devices(prop_dictionary_t dm_dict, struct dm_ioctl *dmi) 256 { 257 struct dm_name_list *dml,*odml; 258 259 prop_array_t targets; 260 prop_dictionary_t target_dict; 261 prop_object_iterator_t iter; 262 263 uint32_t minor; 264 uint32_t major; 265 266 char *name; 267 size_t j,slen,rec_size; 268 269 odml = NULL; 270 name = NULL; 271 minor = 0; 272 j = 0; 273 274 nbsd_get_dm_major(&major,DM_BLOCK_MAJOR); 275 276 dml = (struct dm_name_list *)((uint8_t *)dmi + dmi->data_start); 277 278 if ((targets = prop_dictionary_get(dm_dict,DM_IOCTL_CMD_DATA))){ 279 280 iter = prop_array_iterator(targets); 281 if (!iter) 282 err(EXIT_FAILURE,"dm_list_devices %s",__func__); 283 284 while((target_dict = prop_object_iterator_next(iter)) != NULL){ 285 286 prop_dictionary_get_cstring_nocopy(target_dict, 287 DM_DEV_NAME,(const char **)&name); 288 289 prop_dictionary_get_uint32(target_dict,DM_DEV_DEV,&minor); 290 291 dml->dev = MKDEV(major,minor); 292 293 slen = strlen(name) + 1; 294 rec_size = sizeof(struct dm_name_list) + slen + 1; 295 296 if (rec_size > dmi->data_size) 297 return -ENOMEM; 298 299 dml->next = rec_size; 300 301 strlcpy(dml->name,name,slen); 302 303 odml = dml; 304 305 dml =(struct dm_name_list *)((uint8_t *)dml + rec_size); 306 307 j++; 308 } 309 310 if (odml != NULL) 311 odml->next = 0; 312 } 313 prop_object_iterator_release(iter); 314 return j; 315 } 316 317 /* 318 * Print status of each table, target arguments, start sector, 319 * size and target name. 320 */ 321 static int 322 dm_table_status(prop_dictionary_t dm_dict,struct dm_ioctl *dmi) 323 { 324 struct dm_target_spec *dmts, *odmts; 325 326 prop_array_t targets; 327 prop_dictionary_t target_dict; 328 prop_object_iterator_t iter; 329 330 char *type,*params,*params_start; 331 332 bool prm; 333 size_t j,plen,rec_size,next; 334 335 j = 0; 336 next = 0; 337 params = NULL; 338 odmts = NULL; 339 rec_size = 0; 340 plen = -1; 341 prm = false; 342 343 dmts = (struct dm_target_spec *)((uint8_t *)dmi + dmi->data_start); 344 345 if ((targets = prop_dictionary_get(dm_dict,DM_IOCTL_CMD_DATA))){ 346 347 iter = prop_array_iterator(targets); 348 if (!iter) 349 err(EXIT_FAILURE,"dm_table_status %s",__func__); 350 351 while((target_dict = prop_object_iterator_next(iter)) != NULL){ 352 353 prop_dictionary_get_cstring_nocopy(target_dict, 354 DM_TABLE_TYPE,(const char **)&type); 355 356 prm = prop_dictionary_get_cstring_nocopy(target_dict, 357 DM_TABLE_PARAMS,(const char **)¶ms); 358 359 prop_dictionary_get_uint64(target_dict,DM_TABLE_START,&dmts->sector_start); 360 prop_dictionary_get_uint64(target_dict,DM_TABLE_LENGTH,&dmts->length); 361 prop_dictionary_get_int32(target_dict,DM_TABLE_STAT,&dmts->status); 362 363 if (prm) 364 plen = strlen(params) + 1; 365 366 rec_size = sizeof(struct dm_target_spec) + plen; 367 368 /* 369 * In linux when copying table status from kernel next is 370 * number of bytes from the start of the first dm_target_spec 371 * structure. I don't know why but, it has to be done this way. 372 */ 373 next += rec_size; 374 375 if (rec_size > dmi->data_size) 376 return -ENOMEM; 377 378 dmts->next = next; 379 380 strlcpy(dmts->target_type, type, DM_MAX_TYPE_NAME); 381 382 params_start = (char *)dmts + sizeof(struct dm_target_spec); 383 384 if (prm) 385 strlcpy(params_start, params, plen); 386 else 387 params_start = "\0"; 388 389 390 odmts = dmts; 391 392 dmts = (struct dm_target_spec *)((uint8_t *)dmts + rec_size); 393 394 j++; 395 396 } 397 398 if (odmts != NULL) 399 odmts->next = 0; 400 } 401 prop_object_iterator_release(iter); 402 403 return j; 404 } 405 406 /* 407 * Print dm device dependiences, get minor/major number for 408 * devices. From kernel I will receive major:minor number of 409 * block device used with target. I have to translate it to 410 * raw device numbers and use them, because all other parts of lvm2 411 * uses raw devices internaly. 412 */ 413 static int 414 dm_dev_deps(prop_dictionary_t dm_dict, struct dm_ioctl *dmi) 415 { 416 struct dm_target_deps *dmtd; 417 418 prop_array_t targets; 419 prop_object_iterator_t iter; 420 421 uint32_t major; 422 423 size_t val_len, i, j; 424 425 uint64_t dev_tmp; 426 427 dev_tmp = 0; 428 j = 0; 429 i = 0; 430 431 432 dmtd = (struct dm_target_deps *)((uint8_t *)dmi + dmi->data_start); 433 434 if ((targets = prop_dictionary_get(dm_dict, DM_IOCTL_CMD_DATA))){ 435 436 iter = prop_array_iterator(targets); 437 if (!iter) 438 err(EXIT_FAILURE,"dm_target_deps %s", __func__); 439 440 while((prop_object_iterator_next(iter)) != NULL){ 441 442 prop_array_get_uint64(targets, j, &dev_tmp); 443 444 dmtd->dev[j] = MKDEV(MAJOR(dev_tmp),MINOR(dev_tmp)); 445 /* XXX: worth revisiting */ 446 j++; 447 } 448 } 449 450 dmtd->count = j; 451 452 prop_object_iterator_release(iter); 453 454 return j; 455 } 456