libdm_netbsd.c revision 1.2 1 /* $NetBSD: libdm_netbsd.c,v 1.2 2009/06/05 19:57:25 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 <string.h>
43 #include <unistd.h>
44
45 #include <netbsd-dm.h>
46
47 #include <dm-ioctl.h>
48
49 #include "lib.h"
50
51 struct nbsd_dm_target_param {
52 char *name;
53 prop_dictionary_t (*parse)(char *);
54 };
55
56 #define DMI_SIZE 16 * 1024
57
58 static int dm_list_versions(prop_dictionary_t, struct dm_ioctl *);
59 static int dm_list_devices(prop_dictionary_t, struct dm_ioctl *);
60 static int dm_dev_deps(prop_dictionary_t, struct dm_ioctl *);
61 static int dm_table_status(prop_dictionary_t, struct dm_ioctl *);
62
63 static prop_dictionary_t dm_parse_linear(char *);
64 static prop_dictionary_t dm_parse_stripe(char *);
65 static prop_dictionary_t dm_parse_mirror(char *);
66 static prop_dictionary_t dm_parse_snapshot(char *);
67 static prop_dictionary_t dm_parse_snapshot_origin(char *);
68
69 static struct nbsd_dm_target_param dmt_parse[] = {
70 {"linear", dm_parse_linear},
71 {"striped", dm_parse_stripe},
72 {"mirror", dm_parse_mirror},
73 {"snapshot", dm_parse_snapshot},
74 {"snapshot-origin", dm_parse_snapshot_origin},
75 {NULL, NULL},
76 };
77
78 /*
79 * Parse params string to target specific proplib dictionary.
80 *
81 * <key>params</key>
82 * <dict>
83 * <key>device</key>
84 * <array>
85 * <dict>
86 * <key>device</key>
87 * <string>/dev/wd1a</string>
88 * <key>offset</key>
89 * <integer>0x384</integer>
90 * </dict>
91 * </array>
92 * <!-- Other target config stuff -->
93 * </dict>
94 *
95 */
96 prop_dictionary_t
97 nbsd_dm_parse_param(const char *target, const char *params)
98 {
99 int i;
100 size_t slen, dlen;
101 prop_dictionary_t dict;
102 char *param;
103
104 dict = NULL;
105 slen = strlen(target);
106
107 printf("Parsing target %s, string %s\n", target, params);
108
109 /* copy parameter string to new buffer */
110 param = dm_malloc(strlen(params) * sizeof(char));
111 strlcpy(param, params, strlen(params)+1);
112
113 for(i = 0; dmt_parse[i].name != NULL; i++) {
114 dlen = strlen(dmt_parse[i].name);
115
116 if (slen != dlen)
117 continue;
118
119 if (strncmp(target, dmt_parse[i].name, slen) == 0)
120 break;
121 }
122
123 if (dmt_parse[i].name == NULL)
124 return NULL;
125
126 printf("target found %s, params %s\n", dmt_parse[i].name, params);
127
128 dict = dmt_parse[i].parse(param);
129
130 dm_free(param);
131
132 return dict;
133 }
134
135 /*
136 * Example line sent to dm from lvm tools when using linear target.
137 * start length linear device1 offset1
138 * 0 65536 linear /dev/hda 0
139 */
140 static prop_dictionary_t
141 dm_parse_linear(char *params)
142 {
143 prop_dictionary_t dict;
144 char **ap, *argv[3];
145
146 dict = prop_dictionary_create();
147
148 if (params == NULL)
149 return dict;
150
151 /*
152 * Parse a string, containing tokens delimited by white space,
153 * into an argument vector
154 */
155 for (ap = argv; ap < &argv[2] &&
156 (*ap = strsep(¶ms, " \t")) != NULL;) {
157 if (**ap != '\0')
158 ap++;
159 }
160 printf("Linear target params parsing routine called %s -- %d \n",
161 argv[1], strtol(argv[1], (char **)NULL, 10));
162
163 prop_dictionary_set_cstring(dict, DM_TARGET_LINEAR_DEVICE, argv[0]);
164 prop_dictionary_set_uint64(dict, DM_TARGET_LINEAR_OFFSET, strtoll(argv[1], (char **)NULL, 10));
165
166 return dict;
167 }
168
169 /*
170 * Example line sent to dm from lvm tools when using striped target.
171 * start length striped #stripes chunk_size device1 offset1 ... deviceN offsetN
172 * 0 65536 striped 2 512 /dev/hda 0 /dev/hdb 0
173 */
174 static prop_dictionary_t
175 dm_parse_stripe(char *params)
176 {
177 prop_dictionary_t dict, dev_dict;
178 prop_array_t dev_array;
179 char **ap, *argv[10]; /* Limit number of disk stripes to 10 */
180
181 dict = prop_dictionary_create();
182
183 if (params == NULL)
184 return dict;
185
186 /*
187 * Parse a string, containing tokens delimited by white space,
188 * into an argument vector
189 */
190 for (ap = argv; ap < &argv[9] &&
191 (*ap = strsep(¶ms, " \t")) != NULL;) {
192 if (**ap != '\0')
193 ap++;
194 }
195 printf("Stripe target params parsing routine called\n");
196
197 prop_dictionary_set_uint64(dict, DM_TARGET_STRIPE_STRIPES,
198 strtol(argv[0], (char **)NULL, 10));
199 prop_dictionary_set_uint64(dict, DM_TARGET_STRIPE_CHUNKSIZE,
200 strtol(argv[1], (char **)NULL, 10));
201
202 dev_array = prop_array_create();
203
204 dev_dict = prop_dictionary_create();
205 prop_dictionary_set_cstring(dev_dict, DM_TARGET_STRIPE_DEVICE, argv[2]);
206 prop_dictionary_set_uint64(dev_dict, DM_TARGET_STRIPE_OFFSET,
207 strtol(argv[3], (char **)NULL, 10));
208
209 prop_array_add(dev_array, dev_dict);
210 prop_object_release(dev_dict);
211
212 dev_dict = prop_dictionary_create();
213 prop_dictionary_set_cstring(dev_dict, DM_TARGET_STRIPE_DEVICE, argv[4]);
214 prop_dictionary_set_uint64(dev_dict, DM_TARGET_STRIPE_OFFSET,
215 strtol(argv[5], (char **)NULL, 10));
216
217 prop_array_add(dev_array, dev_dict);
218 prop_object_release(dev_dict);
219
220 prop_dictionary_set(dict, DM_TARGET_STRIPE_DEVARRAY, dev_array);
221 prop_object_release(dev_array);
222
223 return dict;
224 }
225
226 static prop_dictionary_t
227 dm_parse_mirror(char *params)
228 {
229 prop_dictionary_t dict;
230
231 dict = prop_dictionary_create();
232
233 return dict;
234 }
235
236 static prop_dictionary_t
237 dm_parse_snapshot(char *params)
238 {
239 prop_dictionary_t dict;
240
241 dict = prop_dictionary_create();
242
243 return dict;
244 }
245
246 static prop_dictionary_t
247 dm_parse_snapshot_origin(char *params)
248 {
249 prop_dictionary_t dict;
250
251 dict = prop_dictionary_create();
252
253 return dict;
254 }
255
256 int
257 nbsd_get_dm_major(uint32_t *major, int type)
258 {
259 size_t val_len,i;
260 struct kinfo_drivers *kd;
261
262 if (sysctlbyname("kern.drivers",NULL,&val_len,NULL,0) < 0) {
263 printf("sysctlbyname failed");
264 return 0;
265 }
266
267 if ((kd = malloc (val_len)) == NULL){
268 printf("malloc kd info error\n");
269 return 0;
270 }
271
272 if (sysctlbyname("kern.drivers", kd, &val_len, NULL, 0) < 0) {
273 printf("sysctlbyname failed kd");
274 return 0;
275 }
276
277 for (i = 0, val_len /= sizeof(*kd); i < val_len; i++) {
278
279 if (strncmp(kd[i].d_name,DM_NAME,strlen(kd[i].d_name)) == 0){
280
281 if (type == DM_CHAR_MAJOR)
282 /* Set major to dm-driver char major number. */
283 *major = kd[i].d_cmajor;
284 else
285 if (type == DM_BLOCK_MAJOR)
286 *major = kd[i].d_bmajor;
287
288 free(kd);
289
290 return 1;
291 }
292 }
293
294 free(kd);
295
296 return 0;
297 }
298
299 int
300 nbsd_dmi_add_version(const int *version, prop_dictionary_t dm_dict)
301 {
302 prop_array_t ver;
303 size_t i;
304
305 if ((ver = prop_array_create()) == NULL)
306 return -1;
307
308 for (i=0;i<3;i++)
309 prop_array_set_uint32(ver,i,version[i]);
310
311 if ((prop_dictionary_set(dm_dict,"version",ver)) == false)
312 return -1;
313
314 prop_object_release(ver);
315
316 return 0;
317 }
318
319 struct dm_ioctl*
320 nbsd_dm_dict_to_dmi(prop_dictionary_t dm_dict,const int cmd)
321 {
322 struct dm_ioctl *dmi;
323 prop_array_t ver;
324
325 size_t i;
326 int r;
327 char *name, *uuid;
328 uint32_t major,minor;
329
330 name = NULL;
331 uuid = NULL;
332 minor = 0;
333
334 nbsd_get_dm_major(&major, DM_BLOCK_MAJOR);
335
336 if (!(dmi = dm_malloc(DMI_SIZE)))
337 return NULL;
338
339 memset(dmi,0,DMI_SIZE);
340
341 prop_dictionary_get_int32(dm_dict, DM_IOCTL_OPEN, &dmi->open_count);
342 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_EVENT, &dmi->event_nr);
343 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &dmi->flags);
344 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_TARGET_COUNT,
345 &dmi->target_count);
346
347 if (prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor))
348 dmi->dev = MKDEV(major, minor);
349 else
350 dmi->dev = 0;
351
352 /* Copy name and uuid to dm_ioctl. */
353 if (prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME,
354 (const char **)&name)){
355 strlcpy(dmi->name, name, DM_NAME_LEN);
356 } else
357 dmi->name[0] = '\0';
358
359 if (prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID,
360 (const char **)&uuid)){
361 strlcpy(dmi->uuid, uuid, DM_UUID_LEN);
362 } else
363 dmi->uuid[0] = '\0';
364
365 /* dmi parsing values, size of dmi block and offset to data. */
366 dmi->data_size = DMI_SIZE;
367 dmi->data_start = sizeof(struct dm_ioctl);
368
369 /* Get kernel version from dm_dict. */
370 ver = prop_dictionary_get(dm_dict,DM_IOCTL_VERSION);
371
372 for(i=0; i<3; i++)
373 prop_array_get_uint32(ver,i,&dmi->version[i]);
374
375 switch (cmd){
376
377 case DM_LIST_VERSIONS:
378 r = dm_list_versions(dm_dict,dmi);
379 if (r >= 0)
380 dmi->target_count = r;
381 break;
382
383 case DM_LIST_DEVICES:
384 r = dm_list_devices(dm_dict,dmi);
385 if (r >= 0)
386 dmi->target_count = r;
387 break;
388
389 case DM_TABLE_STATUS:
390 r = dm_table_status(dm_dict,dmi);
391 if (r >= 0)
392 dmi->target_count = r;
393 break;
394
395 case DM_TABLE_DEPS:
396 r = dm_dev_deps(dm_dict,dmi);
397 if (r >= 0)
398 dmi->target_count = r;
399 break;
400 }
401
402 return dmi;
403 }
404
405 /*
406 * Parse dm_dict when targets command was called and fill dm_ioctl buffer with it.
407 *
408 * Return number of targets or if failed <0 error.
409 */
410
411 static int
412 dm_list_versions(prop_dictionary_t dm_dict, struct dm_ioctl *dmi)
413 {
414 struct dm_target_versions *dmtv,*odmtv;
415
416 prop_array_t targets,ver;
417 prop_dictionary_t target_dict;
418 prop_object_iterator_t iter;
419
420 char *name;
421 size_t j,i,slen,rec_size;
422
423 odmtv = NULL;
424 name = NULL;
425 j = 0;
426
427 dmtv = (struct dm_target_versions *)((uint8_t *)dmi + dmi->data_start);
428
429 /* printf("dmi: vers: %d.%d.%d data_size: %d data_start: %d name: %s t_count: %d\n",
430 dmi->version[0],dmi->version[1],dmi->version[2],dmi->data_size,dmi->data_start,
431 dmi->name,dmi->target_count);
432
433 printf("dmi: size: %d -- %p --- %p \n",sizeof(struct dm_ioctl),dmi,dmi+dmi->data_start);
434 printf("dmtv: size: %p --- %p\n",dmtv,(struct dm_target_versions *)(dmi+312));*/
435
436 /* get prop_array of target_version dictionaries */
437 if ((targets = prop_dictionary_get(dm_dict,DM_IOCTL_CMD_DATA))){
438
439 iter = prop_array_iterator(targets);
440 if (!iter)
441 err(EXIT_FAILURE,"dm_list_versions %s",__func__);
442
443 while((target_dict = prop_object_iterator_next(iter)) != NULL){
444 j++;
445
446 prop_dictionary_get_cstring_nocopy(target_dict,
447 DM_TARGETS_NAME,(const char **)&name);
448
449 slen = strlen(name) + 1;
450 rec_size = sizeof(struct dm_target_versions) + slen + 1;
451
452 if (rec_size > dmi->data_size)
453 return -ENOMEM;
454
455 ver = prop_dictionary_get(target_dict,DM_TARGETS_VERSION);
456
457 for (i=0; i<3; i++)
458 prop_array_get_uint32(ver,i,&dmtv->version[i]);
459
460 dmtv->next = rec_size;
461
462 strlcpy(dmtv->name,name,slen);
463
464 odmtv = dmtv;
465
466 dmtv =(struct dm_target_versions *)((uint8_t *)dmtv + rec_size);
467 }
468
469 if (odmtv != NULL)
470 odmtv->next = 0;
471 }
472
473 prop_object_iterator_release(iter);
474 return j;
475 }
476
477 /*
478 * List all available dm devices in system.
479 */
480 static int
481 dm_list_devices(prop_dictionary_t dm_dict, struct dm_ioctl *dmi)
482 {
483 struct dm_name_list *dml,*odml;
484
485 prop_array_t targets;
486 prop_dictionary_t target_dict;
487 prop_object_iterator_t iter;
488
489 uint32_t minor;
490 uint32_t major;
491
492 char *name;
493 size_t j,slen,rec_size;
494
495 odml = NULL;
496 name = NULL;
497 minor = 0;
498 j = 0;
499
500 nbsd_get_dm_major(&major,DM_BLOCK_MAJOR);
501
502 dml = (struct dm_name_list *)((uint8_t *)dmi + dmi->data_start);
503
504 if ((targets = prop_dictionary_get(dm_dict,DM_IOCTL_CMD_DATA))){
505
506 iter = prop_array_iterator(targets);
507 if (!iter)
508 err(EXIT_FAILURE,"dm_list_devices %s",__func__);
509
510 while((target_dict = prop_object_iterator_next(iter)) != NULL){
511
512 prop_dictionary_get_cstring_nocopy(target_dict,
513 DM_DEV_NAME,(const char **)&name);
514
515 prop_dictionary_get_uint32(target_dict,DM_DEV_DEV,&minor);
516
517 dml->dev = MKDEV(major,minor);
518
519 slen = strlen(name) + 1;
520 rec_size = sizeof(struct dm_name_list) + slen + 1;
521
522 if (rec_size > dmi->data_size)
523 return -ENOMEM;
524
525 dml->next = rec_size;
526
527 strlcpy(dml->name,name,slen);
528
529 odml = dml;
530
531 dml =(struct dm_name_list *)((uint8_t *)dml + rec_size);
532
533 j++;
534 }
535
536 if (odml != NULL)
537 odml->next = 0;
538 }
539 prop_object_iterator_release(iter);
540 return j;
541 }
542
543 /*
544 * Print status of each table, target arguments, start sector,
545 * size and target name.
546 */
547 static int
548 dm_table_status(prop_dictionary_t dm_dict,struct dm_ioctl *dmi)
549 {
550 struct dm_target_spec *dmts, *odmts;
551
552 prop_array_t targets;
553 prop_dictionary_t target_dict;
554 prop_object_iterator_t iter;
555
556 char *type,*params,*params_start;
557
558 bool prm;
559 size_t j,plen,rec_size,next;
560
561 j = 0;
562 next = 0;
563 params = NULL;
564 odmts = NULL;
565 rec_size = 0;
566 plen = -1;
567 prm = false;
568
569 dmts = (struct dm_target_spec *)((uint8_t *)dmi + dmi->data_start);
570
571 if ((targets = prop_dictionary_get(dm_dict,DM_IOCTL_CMD_DATA))){
572
573 iter = prop_array_iterator(targets);
574 if (!iter)
575 err(EXIT_FAILURE,"dm_table_status %s",__func__);
576
577 while((target_dict = prop_object_iterator_next(iter)) != NULL){
578
579 prop_dictionary_get_cstring_nocopy(target_dict,
580 DM_TABLE_TYPE,(const char **)&type);
581
582 prm = prop_dictionary_get_cstring_nocopy(target_dict,
583 DM_TABLE_PARAMS,(const char **)¶ms);
584
585 prop_dictionary_get_uint64(target_dict,DM_TABLE_START,&dmts->sector_start);
586 prop_dictionary_get_uint64(target_dict,DM_TABLE_LENGTH,&dmts->length);
587 prop_dictionary_get_int32(target_dict,DM_TABLE_STAT,&dmts->status);
588
589 if (prm)
590 plen = strlen(params) + 1;
591
592 rec_size = sizeof(struct dm_target_spec) + plen;
593
594 /*
595 * In linux when copying table status from kernel next is
596 * number of bytes from the start of the first dm_target_spec
597 * structure. I don't know why but, it has to be done this way.
598 */
599 next += rec_size;
600
601 if (rec_size > dmi->data_size)
602 return -ENOMEM;
603
604 dmts->next = next;
605
606 strlcpy(dmts->target_type, type, DM_MAX_TYPE_NAME);
607
608 params_start = (char *)dmts + sizeof(struct dm_target_spec);
609
610 if (prm)
611 strlcpy(params_start, params, plen);
612 else
613 params_start = "\0";
614
615
616 odmts = dmts;
617
618 dmts = (struct dm_target_spec *)((uint8_t *)dmts + rec_size);
619
620 j++;
621
622 }
623
624 if (odmts != NULL)
625 odmts->next = 0;
626 }
627 prop_object_iterator_release(iter);
628
629 return j;
630 }
631
632 /*
633 * Print dm device dependiences, get minor/major number for
634 * devices. From kernel I will receive major:minor number of
635 * block device used with target. I have to translate it to
636 * raw device numbers and use them, because all other parts of lvm2
637 * uses raw devices internaly.
638 */
639 static int
640 dm_dev_deps(prop_dictionary_t dm_dict, struct dm_ioctl *dmi)
641 {
642 struct dm_target_deps *dmtd;
643 struct kinfo_drivers *kd;
644
645 prop_array_t targets;
646 prop_object_iterator_t iter;
647
648 uint32_t major;
649
650 size_t val_len, i, j;
651
652 uint64_t dev_tmp;
653
654 dev_tmp = 0;
655 j = 0;
656 i = 0;
657
658 if (sysctlbyname("kern.drivers",NULL,&val_len,NULL,0) < 0) {
659 printf("sysctlbyname failed");
660 return 0;
661 }
662
663 if ((kd = malloc (val_len)) == NULL){
664 printf("malloc kd info error\n");
665 return 0;
666 }
667
668 if (sysctlbyname("kern.drivers", kd, &val_len, NULL, 0) < 0) {
669 printf("sysctlbyname failed kd");
670 return 0;
671 }
672
673 dmtd = (struct dm_target_deps *)((uint8_t *)dmi + dmi->data_start);
674
675 if ((targets = prop_dictionary_get(dm_dict, DM_IOCTL_CMD_DATA))){
676
677 iter = prop_array_iterator(targets);
678 if (!iter)
679 err(EXIT_FAILURE,"dm_target_deps %s", __func__);
680
681 while((prop_object_iterator_next(iter)) != NULL){
682
683 prop_array_get_uint64(targets, j, &dev_tmp);
684
685 for (i = 0, val_len /= sizeof(*kd); i < val_len; i++){
686 if (kd[i].d_bmajor == MAJOR(dev_tmp)) {
687 major = kd[i].d_cmajor;
688 break;
689 }
690 }
691
692 dmtd->dev[j] = MKDEV(major,MINOR(dev_tmp));
693
694 j++;
695 }
696 }
697
698 dmtd->count = j;
699
700 prop_object_iterator_release(iter);
701
702 return j;
703 }
704