Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / contrib / lvm2 / dist / libdm / ioctl / libdm-dragonfly-iface.c
1 /*      $NetBSD: libdm-nbsd-iface.c,v 1.7 2010/03/12 16:24:40 haad Exp $        */
2
3 /*
4  * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5  * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
6  * Copyright (C) 2008 Adam Hamsik. All rights reserved.
7  * Copyright (C) 2010 Alex Hornung. All rights reserved.
8  *
9  * This file is part of the device-mapper userspace tools.
10  *
11  * This copyrighted material is made available to anyone wishing to use,
12  * modify, copy, or redistribute it subject to the terms and conditions
13  * of the GNU Lesser General Public License v.2.1.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #undef _XOPEN_SOURCE
21 #include "dmlib.h"
22 #include "libdm-targets.h"
23 #include "libdm-common.h"
24 #include "libdm-netbsd.h"
25
26 #include <sys/param.h>
27 #include <sys/ioctl.h>
28 #include <sys/sysctl.h>
29 #include <sys/stat.h>
30 #include <stdlib.h>
31 #include <fcntl.h>
32 #include <dirent.h>
33 #include <limits.h>
34
35 #include <netbsd-dm.h>
36 #include <dm-ioctl.h>
37
38 /*
39  * Ensure build compatibility.  
40  * The hard-coded versions here are the highest present 
41  * in the _cmd_data arrays.
42  */
43
44 #if !((DM_VERSION_MAJOR == 1 && DM_VERSION_MINOR >= 0) || \
45       (DM_VERSION_MAJOR == 4 && DM_VERSION_MINOR >= 0))
46 #error The version of dm-ioctl.h included is incompatible.
47 #endif
48
49 /* dm major version no for running kernel */
50 static unsigned _dm_version_minor = 0;
51 static unsigned _dm_version_patchlevel = 0;
52
53 static int _control_fd = -1;
54 static int _version_checked = 0;
55 static int _version_ok = 1;
56 static unsigned _ioctl_buffer_double_factor = 0;
57
58 /* *INDENT-OFF* */
59
60 /*
61  * XXX Remove this structure and write another own one
62  * I don't understand why ioctl calls has different
63  * names then dm task type
64  */
65 static struct cmd_data _cmd_data_v4[] = {
66         {"create",      DM_DEV_CREATE,          {4, 0, 0}},
67         {"reload",      DM_TABLE_LOAD,          {4, 0, 0}}, /* DM_DEVICE_RELOAD */
68         {"remove",      DM_DEV_REMOVE,          {4, 0, 0}},
69         {"remove_all",  DM_REMOVE_ALL,          {4, 0, 0}},
70         {"suspend",     DM_DEV_SUSPEND,         {4, 0, 0}},
71         {"resume",      DM_DEV_SUSPEND,         {4, 0, 0}},
72         {"info",        DM_DEV_STATUS,          {4, 0, 0}},
73         {"deps",        DM_TABLE_DEPS,          {4, 0, 0}}, /* DM_DEVICE_DEPS */
74         {"rename",      DM_DEV_RENAME,          {4, 0, 0}},
75         {"version",     DM_VERSION,             {4, 0, 0}},
76         {"status",      DM_TABLE_STATUS,        {4, 0, 0}},
77         {"table",       DM_TABLE_STATUS,        {4, 0, 0}}, /* DM_DEVICE_TABLE */
78         {"waitevent",   DM_DEV_WAIT,            {4, 0, 0}},
79         {"names",       DM_LIST_DEVICES,        {4, 0, 0}},
80         {"clear",       DM_TABLE_CLEAR,         {4, 0, 0}},
81         {"mknodes",     DM_DEV_STATUS,          {4, 0, 0}},
82 #ifdef DM_LIST_VERSIONS
83         {"targets",     DM_LIST_VERSIONS,       {4, 1, 0}},
84 #endif
85 #ifdef DM_TARGET_MSG
86         {"message",     DM_TARGET_MSG,          {4, 2, 0}},
87 #endif
88 #ifdef DM_DEV_SET_GEOMETRY
89         {"setgeometry", DM_DEV_SET_GEOMETRY,    {4, 6, 0}},
90 #endif
91 };
92 /* *INDENT-ON* */
93
94 /*
95  * In NetBSD we use sysctl to get kernel drivers info. control device
96  * has predefined minor number 0 and major number = char major number
97  * of dm driver. First slot is therefore ocupied with control device
98  * and minor device starts from 1;
99  */
100
101 static int _control_device_number(uint32_t *major, uint32_t *minor)
102 {
103
104         nbsd_get_dm_major(major, DM_CHAR_MAJOR);
105
106         *minor = 0;
107
108         return 1;
109 }
110
111 /*
112  * Returns 1 if exists; 0 if it doesn't; -1 if it's wrong
113  */
114 static int _control_exists(const char *control, uint32_t major, uint32_t minor)
115 {
116         struct stat buf;
117
118         if (stat(control, &buf) < 0) {
119                 if (errno != ENOENT)
120                         log_sys_error("stat", control);
121                 return 0;
122         }
123
124         if (!S_ISCHR(buf.st_mode)) {
125                 log_verbose("%s: Wrong inode type", control);
126                 if (!unlink(control))
127                         return 0;
128                 log_sys_error("unlink", control);
129                 return -1;
130         }
131
132         if (major && buf.st_rdev != MKDEV(major, minor)) {
133                 log_verbose("%s: Wrong device number: (%u, %u) instead of "
134                             "(%u, %u)", control,
135                             MAJOR(buf.st_mode), MINOR(buf.st_mode),
136                             major, minor);
137                 if (!unlink(control))
138                         return 0;
139                 log_sys_error("unlink", control);
140                 return -1;
141         }
142
143         return 1;
144 }
145
146 static int _create_control(const char *control, uint32_t major, uint32_t minor)
147 {
148         int ret;
149         mode_t old_umask;
150
151         if (!major)
152                 return 0;
153
154         old_umask = umask(0022);
155         ret = dm_create_dir(dm_dir());
156         umask(old_umask);
157
158         if (!ret)
159                 return 0;
160
161         log_verbose("Creating device %s (%u, %u)", control, major, minor);
162
163         if (mknod(control, S_IFCHR | S_IRUSR | S_IWUSR,
164                   MKDEV(major, minor)) < 0)  {
165                 log_sys_error("mknod", control);
166                 return 0;
167         }
168
169
170         return 1;
171 }
172
173 /* Check if major is device-mapper block device major number */
174 int dm_is_dm_major(uint32_t major)
175 {
176         uint32_t dm_major;
177
178         nbsd_get_dm_major(&dm_major, DM_BLOCK_MAJOR);
179
180         if (major == dm_major)
181                 return 1;
182
183         return 0;
184 }
185
186 /* Open control device if doesn't exist create it. */
187 static int _open_control(void)
188 {
189         char control[PATH_MAX];
190         uint32_t major = 0, minor = 0;
191
192         if (_control_fd != -1)
193                 return 1;
194
195         snprintf(control, sizeof(control), "%s/control", dm_dir());
196
197         if (!_control_device_number(&major, &minor))
198                 log_error("Is device-mapper driver missing from kernel?");
199
200         if ((_control_fd = open(control, O_RDWR)) < 0) {
201                 log_sys_error("open", control);
202                 goto error;
203         }
204
205         return 1;
206
207 error:
208         log_error("Failure to communicate with kernel device-mapper driver.");
209         return 0;
210 }
211
212 /*
213  * Destroy dm task structure there are some dynamically alocated values there.
214  * name, uuid, head, tail list.
215  */
216 void dm_task_destroy(struct dm_task *dmt)
217 {
218         struct target *t, *n;
219
220         for (t = dmt->head; t; t = n) {
221                 n = t->next;
222                 dm_free(t->params);
223                 dm_free(t->type);
224                 dm_free(t);
225         }
226
227         if (dmt->dev_name)
228                 dm_free(dmt->dev_name);
229
230         if (dmt->newname)
231                 dm_free(dmt->newname);
232
233         if (dmt->message)
234                 dm_free(dmt->message);
235
236         if (dmt->dmi.v4)
237                 dm_free(dmt->dmi.v4);
238
239         if (dmt->uuid)
240                 dm_free(dmt->uuid);
241
242         dm_free(dmt);
243
244 }
245
246 /* Get kernel driver version from dm_ioctl structure. */
247 int dm_task_get_driver_version(struct dm_task *dmt, char *version, size_t size)
248 {
249         unsigned *v;
250
251         if (!dmt->dmi.v4) {
252                 version[0] = '\0';
253                 return 0;
254         }
255
256         v = dmt->dmi.v4->version;
257         snprintf(version, size, "%u.%u.%u", v[0], v[1], v[2]);
258         _dm_version_minor = v[1];
259         _dm_version_patchlevel = v[2];
260
261         return 1;
262 }
263
264 /* Get kernel driver protocol version and comapre it with library version. */
265 static int _check_version(char *version, size_t size)
266 {
267         struct dm_task *task;
268         int r;
269
270         if (!(task = dm_task_create(DM_DEVICE_VERSION))) {
271                 log_error("Failed to get device-mapper version");
272                 version[0] = '\0';
273                 return 0;
274         }
275
276         r = dm_task_run(task);
277         dm_task_get_driver_version(task, version, size);
278         dm_task_destroy(task);
279
280         return r;
281 }
282
283 /*
284  * Find out device-mapper's major version number the first time 
285  * this is called and whether or not we support it.
286  */
287 int dm_check_version(void)
288 {
289         char dmversion[64];
290
291         if (_version_checked)
292                 return _version_ok;
293
294         _version_checked = 1;
295
296         if (_check_version(dmversion, sizeof(dmversion)))
297                 return 1;
298
299
300         return 0;
301 }
302
303 int dm_cookie_supported(void)
304 {
305         return (0);
306 }
307
308 /* Get next target(table description) from list pointed by dmt->head. */
309 void *dm_get_next_target(struct dm_task *dmt, void *next,
310                          uint64_t *start, uint64_t *length,
311                          char **target_type, char **params)
312 {
313         struct target *t = (struct target *) next;
314
315         if (!t)
316                 t = dmt->head;
317
318         if (!t)
319                 return NULL;
320
321         *start = t->start;
322         *length = t->length;
323         *target_type = t->type;
324         *params = t->params;
325
326         return t->next;
327 }
328
329 /* Unmarshall the target info returned from a status call */
330 static int _unmarshal_status(struct dm_task *dmt, struct dm_ioctl *dmi)
331 {
332         char *outbuf = (char *) dmi + dmi->data_start;
333         char *outptr = outbuf;
334         uint32_t i;
335         struct dm_target_spec *spec;
336
337         for (i = 0; i < dmi->target_count; i++) {
338                 spec = (struct dm_target_spec *) outptr;
339                 if (!dm_task_add_target(dmt, spec->sector_start,
340                                         spec->length,
341                                         spec->target_type,
342                                         outptr + sizeof(*spec))) {
343                         return 0;
344                 }
345
346                 outptr = outbuf + spec->next;
347         }
348
349         return 1;
350 }
351
352 static char *
353 get_dev_name(char *d_name, uint32_t d_major, uint32_t d_minor)
354 {
355         static char d_buf[MAXPATHLEN];
356         struct dirent *dire;
357         struct stat st;
358         DIR *dev_dir;
359
360         int err;
361         char *name;
362
363         dev_dir = opendir("/dev");
364
365         while ((dire = readdir(dev_dir)) != NULL) {
366
367                 if (strstr(dire->d_name, d_name) == NULL)
368                         continue;
369
370                 snprintf(d_buf, MAXPATHLEN, "/dev/%s", dire->d_name);
371
372                 if ((err = stat(d_buf, &st)) < 0)
373                         printf("stat failed with %d", err);
374
375                 if (st.st_mode & S_IFBLK){
376                         if ((major(st.st_rdev) == d_major) && (minor(st.st_rdev) == d_minor)) {
377                                 strncpy(d_buf, dire->d_name, strlen(dire->d_name) + 1);
378                                 name = d_buf;
379                                 break;
380                         }
381                 }
382
383                 memset(d_buf, '0', sizeof(d_buf));
384         }
385
386         (void)closedir(dev_dir);
387
388         return name;
389 }
390
391 /*
392  * @dev_major is major number of char device
393  *
394  * I have to find it's block device number and lookup dev in
395  * device database to find device path.
396  *
397  */
398
399 int dm_format_dev(char *buf, int bufsize, uint32_t dev_major,
400                   uint32_t dev_minor)
401 {
402         int r;
403         uint32_t major, dm_major;
404         char *name;
405         mode_t mode = 0;
406         dev_t dev;
407         size_t val_len,i;
408
409         dev = MKDEV(dev_major,dev_minor);
410         mode |= S_IFCHR;
411
412         name = devname(dev,mode);
413
414         r = snprintf(buf, (size_t) bufsize, "/dev/%s",name);
415
416         if (r < 0 || r > bufsize - 1 || name == NULL)
417                 return 0;
418
419         return 1;
420 }
421
422 /* Fill info from dm_ioctl structure. Look at DM_EXISTS_FLAG*/
423 int dm_task_get_info(struct dm_task *dmt, struct dm_info *info)
424 {
425         if (!dmt->dmi.v4)
426                 return 0;
427
428         memset(info, 0, sizeof(*info));
429
430         info->exists = dmt->dmi.v4->flags & DM_EXISTS_FLAG ? 1 : 0;
431         if (!info->exists)
432                 return 1;
433
434         info->suspended = dmt->dmi.v4->flags & DM_SUSPEND_FLAG ? 1 : 0;
435         info->read_only = dmt->dmi.v4->flags & DM_READONLY_FLAG ? 1 : 0;
436         info->live_table = dmt->dmi.v4->flags & DM_ACTIVE_PRESENT_FLAG ? 1 : 0;
437         info->inactive_table = dmt->dmi.v4->flags & DM_INACTIVE_PRESENT_FLAG ?
438             1 : 0;
439         info->target_count = dmt->dmi.v4->target_count;
440         info->open_count = dmt->dmi.v4->open_count;
441         info->event_nr = dmt->dmi.v4->event_nr;
442
443         nbsd_get_dm_major(&info->major, DM_BLOCK_MAJOR); /* get netbsd dm device major number */
444         info->minor = MINOR(dmt->dmi.v4->dev);
445
446         return 1;
447 }
448
449 /* Unsupported on NetBSD */
450 uint32_t dm_task_get_read_ahead(const struct dm_task *dmt, uint32_t *read_ahead)
451 {
452         *read_ahead = DM_READ_AHEAD_NONE;
453         return 1;
454 }
455
456 const char *dm_task_get_name(const struct dm_task *dmt)
457 {
458
459         return (dmt->dmi.v4->name);
460 }
461
462 const char *dm_task_get_uuid(const struct dm_task *dmt)
463 {
464
465         return (dmt->dmi.v4->uuid);
466 }
467
468 struct dm_deps *dm_task_get_deps(struct dm_task *dmt)
469 {
470         return (struct dm_deps *) (((void *) dmt->dmi.v4) +
471                                    dmt->dmi.v4->data_start);
472 }
473
474 struct dm_names *dm_task_get_names(struct dm_task *dmt)
475 {
476         return (struct dm_names *) (((void *) dmt->dmi.v4) +
477                                     dmt->dmi.v4->data_start);
478 }
479
480 struct dm_versions *dm_task_get_versions(struct dm_task *dmt)
481 {
482         return (struct dm_versions *) (((void *) dmt->dmi.v4) +
483                                        dmt->dmi.v4->data_start);
484 }
485
486 int dm_task_set_ro(struct dm_task *dmt)
487 {
488         dmt->read_only = 1;
489         return 1;
490 }
491
492 /* Unsupported on NetBSD */
493 int dm_task_set_read_ahead(struct dm_task *dmt, uint32_t read_ahead,
494                            uint32_t read_ahead_flags)
495 {
496         return 1;
497 }
498
499 int dm_task_suppress_identical_reload(struct dm_task *dmt)
500 {
501         dmt->suppress_identical_reload = 1;
502         return 1;
503 }
504
505 int dm_task_set_newname(struct dm_task *dmt, const char *newname)
506 {
507         if (!(dmt->newname = dm_strdup(newname))) {
508                 log_error("dm_task_set_newname: strdup(%s) failed", newname);
509                 return 0;
510         }
511
512         return 1;
513 }
514
515 int dm_task_set_message(struct dm_task *dmt, const char *message)
516 {
517         if (!(dmt->message = dm_strdup(message))) {
518                 log_error("dm_task_set_message: strdup(%s) failed", message);
519                 return 0;
520         }
521
522         return 1;
523 }
524
525 int dm_task_set_sector(struct dm_task *dmt, uint64_t sector)
526 {
527         dmt->sector = sector;
528
529         return 1;
530 }
531
532 /* Unsupported in NetBSD */
533 int dm_task_set_geometry(struct dm_task *dmt, const char *cylinders,
534     const char *heads, const char *sectors, const char *start)
535 {
536         return 0;
537 }
538
539 int dm_task_no_flush(struct dm_task *dmt)
540 {
541         dmt->no_flush = 1;
542
543         return 1;
544 }
545
546 int dm_task_no_open_count(struct dm_task *dmt)
547 {
548         dmt->no_open_count = 1;
549
550         return 1;
551 }
552
553 int dm_task_skip_lockfs(struct dm_task *dmt)
554 {
555         dmt->skip_lockfs = 1;
556
557         return 1;
558 }
559
560 int dm_task_query_inactive_table(struct dm_task *dmt)
561 {
562         dmt->query_inactive_table = 1;
563
564         return 1;
565 }
566
567 int dm_task_set_event_nr(struct dm_task *dmt, uint32_t event_nr)
568 {
569         dmt->event_nr = event_nr;
570
571         return 1;
572 }
573
574 /* Allocate one target(table description) entry. */
575 struct target *create_target(uint64_t start, uint64_t len, const char *type,
576                              const char *params)
577 {
578         struct target *t = dm_malloc(sizeof(*t));
579         log_verbose("params is at create_target: %s", params);
580         if (!t) {
581                 log_error("create_target: malloc(%" PRIsize_t ") failed",
582                           sizeof(*t));
583                 return NULL;
584         }
585
586         memset(t, 0, sizeof(*t));
587
588         if (!(t->params = dm_strdup(params))) {
589                 log_error("create_target: strdup(params) failed");
590                 goto bad;
591         }
592
593         if (!(t->type = dm_strdup(type))) {
594                 log_error("create_target: strdup(type) failed");
595                 goto bad;
596         }
597
598         t->start = start;
599         t->length = len;
600         return t;
601
602       bad:
603         dm_free(t->params);
604         dm_free(t->type);
605         dm_free(t);
606         return NULL;
607 }
608
609 /* Parse given dm task structure to proplib dictionary.  */
610 static int _flatten(struct dm_task *dmt, prop_dictionary_t dm_dict)
611 {
612         prop_array_t cmd_array;
613         prop_dictionary_t target_spec;
614
615         struct target *t;
616
617         size_t len;
618         char type[DM_MAX_TYPE_NAME];
619
620         uint32_t major, flags;
621         int count = 0;
622         char *str = NULL;
623         const int (*version)[3];
624
625         flags = 0;
626         version = &_cmd_data_v4[dmt->type].version;
627
628         cmd_array = prop_array_create();
629
630         for (t = dmt->head; t; t = t->next) {
631                 target_spec = prop_dictionary_create();
632
633                 prop_dictionary_set_uint64(target_spec,DM_TABLE_START,t->start);
634                 prop_dictionary_set_uint64(target_spec,DM_TABLE_LENGTH,t->length);
635
636                 strlcpy(type,t->type,DM_MAX_TYPE_NAME);
637
638                 prop_dictionary_set_cstring(target_spec,DM_TABLE_TYPE,type);
639                 prop_dictionary_set_cstring(target_spec,DM_TABLE_PARAMS,t->params);
640
641                 prop_dictionary_get_cstring(target_spec,
642                     DM_TABLE_PARAMS, (char **) &str);
643
644                 prop_array_set(cmd_array,count,target_spec);
645
646                 prop_object_release(target_spec);
647
648                 count++;
649         }
650
651
652         if (count && (dmt->sector || dmt->message)) {
653                 log_error("targets and message are incompatible");
654                 return -1;
655         }
656
657         if (count && dmt->newname) {
658                 log_error("targets and newname are incompatible");
659                 return -1;
660         }
661
662         if (count && dmt->geometry) {
663                 log_error("targets and geometry are incompatible");
664                 return -1;
665         }
666
667         if (dmt->newname && (dmt->sector || dmt->message)) {
668                 log_error("message and newname are incompatible");
669                 return -1;
670         }
671
672         if (dmt->newname && dmt->geometry) {
673                 log_error("geometry and newname are incompatible");
674                 return -1;
675         }
676
677         if (dmt->geometry && (dmt->sector || dmt->message)) {
678                 log_error("geometry and message are incompatible");
679                 return -1;
680         }
681
682         if (dmt->sector && !dmt->message) {
683                 log_error("message is required with sector");
684                 return -1;
685         }
686
687         if (dmt->newname)
688                 len += strlen(dmt->newname) + 1;
689
690         if (dmt->message)
691                 len += sizeof(struct dm_target_msg) + strlen(dmt->message) + 1;
692
693         if (dmt->geometry)
694                 len += strlen(dmt->geometry) + 1;
695
696         nbsd_dmi_add_version((*version), dm_dict);
697
698         nbsd_get_dm_major(&major, DM_BLOCK_MAJOR);
699         /* 
700          * Only devices with major which is equal to netbsd dm major 
701          * dm devices in NetBSD can't have more majors then one assigned to dm.
702          */
703         if (dmt->major != major && dmt->major != -1)
704                 return -1;
705
706         if (dmt->minor >= 0) {
707                 flags |= DM_PERSISTENT_DEV_FLAG;
708
709                 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmt->minor);
710         }
711
712         /* Set values to dictionary. */
713         if (dmt->dev_name)
714                 prop_dictionary_set_cstring(dm_dict, DM_IOCTL_NAME, dmt->dev_name);
715
716         if (dmt->uuid)
717                 prop_dictionary_set_cstring(dm_dict, DM_IOCTL_UUID, dmt->uuid);
718
719         if (dmt->type == DM_DEVICE_SUSPEND)
720                 flags |= DM_SUSPEND_FLAG;
721         if (dmt->no_flush)
722                 flags |= DM_NOFLUSH_FLAG;
723         if (dmt->read_only)
724                 flags |= DM_READONLY_FLAG;
725         if (dmt->skip_lockfs)
726                 flags |= DM_SKIP_LOCKFS_FLAG;
727
728         if (dmt->query_inactive_table) {
729                 if (_dm_version_minor < 16)
730                         log_warn("WARNING: Inactive table query unsupported "
731                                  "by kernel.  It will use live table.");
732                 flags |= DM_QUERY_INACTIVE_TABLE_FLAG;
733         }
734
735         prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, flags);
736
737         prop_dictionary_set_uint32(dm_dict, DM_IOCTL_EVENT, dmt->event_nr);
738
739         if (dmt->newname)
740                 prop_array_set_cstring(cmd_array, 0, dmt->newname);
741
742         /* Add array for all COMMAND specific data. */
743         prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, cmd_array);
744         prop_object_release(cmd_array);
745
746         return 0;
747 }
748
749 static int _process_mapper_dir(struct dm_task *dmt)
750 {
751         struct dirent *dirent;
752         DIR *d;
753         const char *dir;
754         int r = 1;
755
756         dir = dm_dir();
757         if (!(d = opendir(dir))) {
758                 log_sys_error("opendir", dir);
759                 return 0;
760         }
761
762         while ((dirent = readdir(d))) {
763                 if (!strcmp(dirent->d_name, ".") ||
764                     !strcmp(dirent->d_name, "..") ||
765                     !strcmp(dirent->d_name, "control"))
766                         continue;
767                 dm_task_set_name(dmt, dirent->d_name);
768                 dm_task_run(dmt);
769         }
770
771         if (closedir(d))
772                 log_sys_error("closedir", dir);
773
774         return r;
775 }
776
777 /* Get list of all devices. */
778 static int _process_all_v4(struct dm_task *dmt)
779 {
780         struct dm_task *task;
781         struct dm_names *names;
782         unsigned next = 0;
783         int r = 1;
784
785         if (!(task = dm_task_create(DM_DEVICE_LIST)))
786                 return 0;
787
788         if (!dm_task_run(task)) {
789                 r = 0;
790                 goto out;
791         }
792
793         if (!(names = dm_task_get_names(task))) {
794                 r = 0;
795                 goto out;
796         }
797
798         if (!names->dev)
799                 goto out;
800
801         do {
802                 names = (void *) names + next;
803                 if (!dm_task_set_name(dmt, names->name)) {
804                         r = 0;
805                         goto out;
806                 }
807                 if (!dm_task_run(dmt))
808                         r = 0;
809                 next = names->next;
810         } while (next);
811
812       out:
813         dm_task_destroy(task);
814         return r;
815 }
816
817 static int _mknodes_v4(struct dm_task *dmt)
818 {
819         (void) _process_mapper_dir(dmt);
820
821         return _process_all_v4(dmt);
822 }
823
824 /* Create new device and load table to it. */
825 static int _create_and_load_v4(struct dm_task *dmt)
826 {
827         struct dm_task *task;
828         int r;
829
830         log_verbose("create and load called");
831
832         /* Use new task struct to create the device */
833         if (!(task = dm_task_create(DM_DEVICE_CREATE))) {
834                 log_error("Failed to create device-mapper task struct");
835                 return 0;
836         }
837
838         /* Copy across relevant fields */
839         if (dmt->dev_name && !dm_task_set_name(task, dmt->dev_name)) {
840                 dm_task_destroy(task);
841                 return 0;
842         }
843
844         if (dmt->uuid && !dm_task_set_uuid(task, dmt->uuid)) {
845                 dm_task_destroy(task);
846                 return 0;
847         }
848
849         task->major = dmt->major;
850         task->minor = dmt->minor;
851         task->uid = dmt->uid;
852         task->gid = dmt->gid;
853         task->mode = dmt->mode;
854
855         r = dm_task_run(task);
856         dm_task_destroy(task);
857         if (!r)
858                 return r;
859
860         /* Next load the table */
861         if (!(task = dm_task_create(DM_DEVICE_RELOAD))) {
862                 log_error("Failed to create device-mapper task struct");
863                 return 0;
864         }
865
866         /* Copy across relevant fields */
867         if (dmt->dev_name && !dm_task_set_name(task, dmt->dev_name)) {
868                 dm_task_destroy(task);
869                 return 0;
870         }
871
872         task->read_only = dmt->read_only;
873         task->head = dmt->head;
874         task->tail = dmt->tail;
875
876         r = dm_task_run(task);
877
878         task->head = NULL;
879         task->tail = NULL;
880         dm_task_destroy(task);
881         if (!r)
882                 goto revert;
883
884         /* Use the original structure last so the info will be correct */
885         dmt->type = DM_DEVICE_RESUME;
886         dm_free(dmt->uuid);
887         dmt->uuid = NULL;
888
889         r = dm_task_run(dmt);
890
891         if (r)
892                 return r;
893
894       revert:
895         dmt->type = DM_DEVICE_REMOVE;
896         dm_free(dmt->uuid);
897         dmt->uuid = NULL;
898
899         if (!dm_task_run(dmt))
900                 log_error("Failed to revert device creation.");
901
902         return r;
903 }
904
905 uint64_t dm_task_get_existing_table_size(struct dm_task *dmt)
906 {
907         return dmt->existing_table_size;
908 }
909
910 static int _reload_with_suppression_v4(struct dm_task *dmt)
911 {
912         struct dm_task *task;
913         struct target *t1, *t2;
914         int r;
915
916         /* New task to get existing table information */
917         if (!(task = dm_task_create(DM_DEVICE_TABLE))) {
918                 log_error("Failed to create device-mapper task struct");
919                 return 0;
920         }
921
922         /* Copy across relevant fields */
923         if (dmt->dev_name && !dm_task_set_name(task, dmt->dev_name)) {
924                 dm_task_destroy(task);
925                 return 0;
926         }
927
928         if (dmt->uuid && !dm_task_set_uuid(task, dmt->uuid)) {
929                 dm_task_destroy(task);
930                 return 0;
931         }
932
933         task->major = dmt->major;
934         task->minor = dmt->minor;
935
936         r = dm_task_run(task);
937
938         if (!r) {
939                 dm_task_destroy(task);
940                 return r;
941         }
942
943         /* Store existing table size */
944         t2 = task->head;
945         while (t2 && t2->next)
946                 t2 = t2->next;
947         dmt->existing_table_size = t2 ? t2->start + t2->length : 0;
948
949         if ((task->dmi.v4->flags & DM_READONLY_FLAG) ? 1 : 0 != dmt->read_only)
950                 goto no_match;
951
952         t1 = dmt->head;
953         t2 = task->head;
954
955         while (t1 && t2) {
956                 while (t2->params[strlen(t2->params) - 1] == ' ')
957                         t2->params[strlen(t2->params) - 1] = '\0';
958                 if ((t1->start != t2->start) ||
959                     (t1->length != t2->length) ||
960                     (strcmp(t1->type, t2->type)) ||
961                     (strcmp(t1->params, t2->params)))
962                         goto no_match;
963                 t1 = t1->next;
964                 t2 = t2->next;
965         }
966
967         if (!t1 && !t2) {
968                 dmt->dmi.v4 = task->dmi.v4;
969                 task->dmi.v4 = NULL;
970                 dm_task_destroy(task);
971                 return 1;
972         }
973
974 no_match:
975         dm_task_destroy(task);
976
977         /* Now do the original reload */
978         dmt->suppress_identical_reload = 0;
979         r = dm_task_run(dmt);
980
981         return r;
982 }
983
984 /*
985  * This function is heart of NetBSD libdevmapper-> device-mapper kernel protocol
986  * It creates proplib_dictionary from dm task structure and sends it to NetBSD
987  * kernel driver. After succesfull ioctl it create dmi structure from returned
988  * proplib dictionary. This way I keep number of changes in NetBSD version of
989  * libdevmapper as small as posible.
990  */
991 static struct dm_ioctl *_do_dm_ioctl(struct dm_task *dmt, unsigned command)
992 {
993         struct dm_ioctl *dmi;
994         prop_dictionary_t dm_dict_in, dm_dict_out;
995
996         uint32_t flags;
997
998         dm_dict_in = NULL;
999
1000         dm_dict_in = prop_dictionary_create(); /* Dictionary send to kernel */
1001         dm_dict_out = prop_dictionary_create(); /* Dictionary received from kernel */
1002
1003         /* Set command name to dictionary */
1004         prop_dictionary_set_cstring(dm_dict_in, DM_IOCTL_COMMAND,
1005             _cmd_data_v4[dmt->type].name);
1006
1007         /* Parse dmi from libdevmapper to dictionary */
1008         if (_flatten(dmt, dm_dict_in) < 0)
1009                 goto bad;
1010
1011         prop_dictionary_get_uint32(dm_dict_in, DM_IOCTL_FLAGS, &flags);
1012
1013         if (dmt->type == DM_DEVICE_TABLE)
1014                 flags |= DM_STATUS_TABLE_FLAG;
1015
1016         if (dmt->no_open_count)
1017                 flags |= DM_SKIP_BDGET_FLAG;
1018
1019         flags |= DM_EXISTS_FLAG;
1020
1021         /* Set flags to dictionary. */
1022         prop_dictionary_set_uint32(dm_dict_in,DM_IOCTL_FLAGS,flags);
1023         log_very_verbose("Ioctl type  %s --- flags %d",_cmd_data_v4[dmt->type].name,flags);
1024         //printf("name %s, major %d minor %d\n uuid %s\n", 
1025         //dm_task_get_name(dmt), dmt->minor, dmt->major, dm_task_get_uuid(dmt));
1026         /* Send dictionary to kernel and wait for reply. */
1027
1028         if (prop_dictionary_sendrecv_ioctl(dm_dict_in,_control_fd,
1029                 NETBSD_DM_IOCTL,&dm_dict_out) != 0) {
1030
1031                 if (errno == ENOENT &&
1032                     ((dmt->type == DM_DEVICE_INFO) ||
1033                         (dmt->type == DM_DEVICE_MKNODES) ||
1034                         (dmt->type == DM_DEVICE_STATUS))) {
1035
1036                         /*
1037                          * Linux version doesn't fail when ENOENT is returned
1038                          * for nonexisting device after info, deps, mknodes call.
1039                          * It returns dmi sent to kernel with DM_EXISTS_FLAG = 0;
1040                          */
1041
1042                         dmi = nbsd_dm_dict_to_dmi(dm_dict_in,_cmd_data_v4[dmt->type].cmd);
1043
1044                         dmi->flags &= ~DM_EXISTS_FLAG;
1045
1046                         prop_object_release(dm_dict_in);
1047                         prop_object_release(dm_dict_out);
1048
1049                         goto out;
1050                 } else {
1051                         log_error("ioctl %s call failed: %s\n",
1052                             _cmd_data_v4[dmt->type].name, strerror(errno));
1053
1054                         prop_object_release(dm_dict_in);
1055                         prop_object_release(dm_dict_out);
1056
1057                         goto bad;
1058                 }
1059         }
1060
1061         /* Parse kernel dictionary to dmi structure and return it to libdevmapper. */
1062         dmi = nbsd_dm_dict_to_dmi(dm_dict_out,_cmd_data_v4[dmt->type].cmd);
1063
1064         prop_object_release(dm_dict_in);
1065         prop_object_release(dm_dict_out);
1066 out:
1067         return dmi;
1068 bad:
1069         return NULL;
1070 }
1071
1072 /* Create new edvice nodes in mapper/ dir. */
1073 void dm_task_update_nodes(void)
1074 {
1075         update_devs();
1076 }
1077
1078 static void _dm_rename_kern(struct dm_task *dmt)
1079 {
1080         prop_dictionary_t dm_dict_in, dm_dict_out;
1081
1082         dm_dict_in = prop_dictionary_create(); /* Dictionary send to kernel */
1083
1084         /* Set command name to dictionary */
1085         prop_dictionary_set_cstring(dm_dict_in, DM_IOCTL_COMMAND,
1086             "nrename");
1087         prop_dictionary_set_cstring(dm_dict_in, "dev_name", dmt->dev_name);
1088         prop_dictionary_set_cstring(dm_dict_in, "new_name", dmt->newname);
1089
1090         /* Set flags to dictionary. */
1091         prop_dictionary_set_uint32(dm_dict_in,DM_IOCTL_FLAGS,0);
1092
1093         if (prop_dictionary_sendrecv_ioctl(dm_dict_in,_control_fd,
1094                 NETBSD_DM_IOCTL,&dm_dict_out) != 0) {
1095                 log_error("rename failed");
1096         }
1097 }
1098
1099 /* Run dm command which is descirbed in dm_task structure. */
1100 int dm_task_run(struct dm_task *dmt)
1101 {
1102         struct dm_ioctl *dmi;
1103         unsigned command;
1104
1105         if ((unsigned) dmt->type >=
1106             (sizeof(_cmd_data_v4) / sizeof(*_cmd_data_v4))) {
1107                 log_error("Internal error: unknown device-mapper task %d",
1108                           dmt->type);
1109                 return 0;
1110         }
1111
1112         command = _cmd_data_v4[dmt->type].cmd;
1113
1114         /* Old-style creation had a table supplied */
1115         if (dmt->type == DM_DEVICE_CREATE && dmt->head)
1116                 return _create_and_load_v4(dmt);
1117
1118         if (dmt->type == DM_DEVICE_MKNODES && !dmt->dev_name &&
1119             !dmt->uuid && dmt->major <= 0)
1120                 return _mknodes_v4(dmt);
1121
1122         if ((dmt->type == DM_DEVICE_RELOAD) && dmt->suppress_identical_reload)
1123                 return _reload_with_suppression_v4(dmt);
1124
1125         if (!_open_control())
1126                 return 0;
1127
1128         if (!(dmi = _do_dm_ioctl(dmt, command)))
1129                 return 0;
1130
1131         switch (dmt->type) {
1132         case DM_DEVICE_CREATE:
1133                 log_verbose("create dmt->dev_name = %s", dmt->dev_name);
1134                 /* kernel takes care of this */
1135                 break;
1136
1137         case DM_DEVICE_REMOVE:
1138                 log_verbose("remove dmt->dev_name = %s", dmt->dev_name);
1139                 /* kernel takes care of this */
1140                 break;
1141
1142         case DM_DEVICE_RENAME:
1143                 /* FIXME Kernel needs to fill in dmi->name */
1144                 log_verbose("rename dmt->dev_name = %s", dmt->dev_name);
1145                  _dm_rename_kern(dmt);
1146                 break;
1147
1148         case DM_DEVICE_RESUME:
1149                 /* FIXME Kernel needs to fill in dmi->name */
1150                 set_dev_node_read_ahead(dmt->dev_name, dmt->read_ahead,
1151                                         dmt->read_ahead_flags);
1152                 break;
1153
1154         case DM_DEVICE_MKNODES:
1155                 log_verbose("mknodes dmt->dev_name = %s", dmt->dev_name);
1156                 break;
1157
1158         case DM_DEVICE_STATUS:
1159         case DM_DEVICE_TABLE:
1160         case DM_DEVICE_WAITEVENT:
1161                 if (!_unmarshal_status(dmt, dmi))
1162                         goto bad;
1163                 break;
1164         }
1165
1166         /* Was structure reused? */
1167         if (dmt->dmi.v4)
1168                 dm_free(dmt->dmi.v4);
1169
1170         dmt->dmi.v4 = dmi;
1171         return 1;
1172
1173       bad:
1174         dm_free(dmi);
1175         return 0;
1176 }
1177
1178 void dm_lib_release(void)
1179 {
1180         if (_control_fd != -1) {
1181                 close(_control_fd);
1182                 _control_fd = -1;
1183         }
1184         update_devs();
1185 }
1186
1187 void dm_lib_exit(void)
1188 {
1189         dm_lib_release();
1190         dm_dump_memory();
1191         _version_ok = 1;
1192         _version_checked = 0;
1193 }