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