Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / contrib / lvm2 / dist / libdm / libdm-common.c
1 /*      $NetBSD: libdm-common.c,v 1.5 2009/12/05 11:42:24 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  *
7  * This file is part of the device-mapper userspace tools.
8  *
9  * This copyrighted material is made available to anyone wishing to use,
10  * modify, copy, or redistribute it subject to the terms and conditions
11  * of the GNU Lesser General Public License v.2.1.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; if not, write to the Free Software Foundation,
15  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  */
17
18 #include "dmlib.h"
19 #include "libdm-targets.h"
20 #include "libdm-common.h"
21 #ifdef linux
22 #include "kdev_t.h"
23 #endif
24 #include "dm-ioctl.h"
25
26 #include <stdarg.h>
27 #include <sys/param.h>
28 #include <sys/ioctl.h>
29 #include <fcntl.h>
30 #include <dirent.h>
31
32 #ifdef UDEV_SYNC_SUPPORT
33 #  include <sys/types.h>
34 #  include <sys/ipc.h>
35 #  include <sys/sem.h>
36 #ifdef HAVE_UDEV_QUEUE_GET_UDEV_IS_ACTIVE
37 #  define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE
38 #  include <libudev.h>
39 #endif
40 #endif
41
42 #ifdef linux
43 #  include <linux/fs.h>
44 #endif
45
46 #ifdef HAVE_SELINUX
47 #  include <selinux/selinux.h>
48 #endif
49
50 #if defined(__NetBSD__) || defined(__DragonFly__)
51 #include "libdm-netbsd.h"
52 #endif
53
54 #define DEV_DIR "/dev/"
55
56 static char _dm_dir[PATH_MAX] = DEV_DIR DM_DIR;
57
58 static int _verbose = 0;
59
60 #ifdef UDEV_SYNC_SUPPORT
61 static int _udev_running = -1;
62 static int _sync_with_udev = 1;
63 #endif
64
65 /*
66  * Library users can provide their own logging
67  * function.
68  */
69
70 static void _default_log_line(int level,
71             const char *file __attribute((unused)),
72             int line __attribute((unused)), int dm_errno, 
73             const char *f, va_list ap)
74 {
75         int use_stderr = level & _LOG_STDERR;
76
77         level &= ~_LOG_STDERR;
78
79         if (level > _LOG_WARN && !_verbose)
80                 return;
81
82         if (level < _LOG_WARN)
83                 vfprintf(stderr, f, ap);
84         else
85                 vfprintf(use_stderr ? stderr : stdout, f, ap);
86
87         if (level < _LOG_WARN)
88                 fprintf(stderr, "\n");
89         else
90                 fprintf(use_stderr ? stderr : stdout, "\n");
91 }
92
93 static void _default_log_with_errno(int level,
94             const char *file __attribute((unused)),
95             int line __attribute((unused)), int dm_errno, 
96             const char *f, ...)
97 {
98         va_list ap;
99
100         va_start(ap, f);
101         _default_log_line(level, file, line, dm_errno, f, ap);
102         va_end(ap);
103 }
104
105 static void _default_log(int level, const char *file,
106                          int line, const char *f, ...)
107 {
108         va_list ap;
109
110         va_start(ap, f);
111         _default_log_line(level, file, line, 0, f, ap);
112         va_end(ap);
113 }
114
115 dm_log_fn dm_log = _default_log;
116 dm_log_with_errno_fn dm_log_with_errno = _default_log_with_errno;
117
118 void dm_log_init(dm_log_fn fn)
119 {
120         if (fn)
121                 dm_log = fn;
122         else
123                 dm_log = _default_log;
124
125         dm_log_with_errno = _default_log_with_errno;
126 }
127
128 int dm_log_is_non_default(void)
129 {
130         return (dm_log == _default_log) ? 0 : 1;
131 }
132
133 void dm_log_with_errno_init(dm_log_with_errno_fn fn)
134 {
135         if (fn)
136                 dm_log_with_errno = fn;
137         else
138                 dm_log_with_errno = _default_log_with_errno;
139
140         dm_log = _default_log;
141 }
142
143 void dm_log_init_verbose(int level)
144 {
145         _verbose = level;
146 }
147
148 static void _build_dev_path(char *buffer, size_t len, const char *dev_name)
149 {
150         /* If there's a /, assume caller knows what they're doing */
151         if (strchr(dev_name, '/'))
152                 snprintf(buffer, len, "%s", dev_name);
153         else
154                 snprintf(buffer, len, "%s/%s", _dm_dir, dev_name);
155 }
156
157 int dm_get_library_version(char *version, size_t size)
158 {
159         strncpy(version, DM_LIB_VERSION, size);
160         return 1;
161 }
162
163 struct dm_task *dm_task_create(int type)
164 {
165         struct dm_task *dmt = dm_malloc(sizeof(*dmt));
166
167         if (!dmt) {
168                 log_error("dm_task_create: malloc(%" PRIsize_t ") failed",
169                           sizeof(*dmt));
170                 return NULL;
171         }
172
173         if (!dm_check_version()) {
174                 dm_free(dmt);
175                 return NULL;
176         }
177
178         memset(dmt, 0, sizeof(*dmt));
179
180         dmt->type = type;
181         dmt->minor = -1;
182         dmt->major = -1;
183         dmt->allow_default_major_fallback = 1;
184         dmt->uid = DM_DEVICE_UID;
185         dmt->gid = DM_DEVICE_GID;
186         dmt->mode = DM_DEVICE_MODE;
187         dmt->no_open_count = 0;
188         dmt->read_ahead = DM_READ_AHEAD_AUTO;
189         dmt->read_ahead_flags = 0;
190         dmt->event_nr = 0;
191         dmt->cookie_set = 0;
192         dmt->query_inactive_table = 0;
193
194         return dmt;
195 }
196
197 /*
198  * Find the name associated with a given device number by scanning _dm_dir.
199  */
200 static char *_find_dm_name_of_device(dev_t st_rdev)
201 {
202         const char *name;
203         char path[PATH_MAX];
204         struct dirent *dirent;
205         DIR *d;
206         struct stat buf;
207         char *new_name = NULL;
208
209         if (!(d = opendir(_dm_dir))) {
210                 log_sys_error("opendir", _dm_dir);
211                 return NULL;
212         }
213
214         while ((dirent = readdir(d))) {
215                 name = dirent->d_name;
216
217                 if (!strcmp(name, ".") || !strcmp(name, ".."))
218                         continue;
219
220                 if (dm_snprintf(path, sizeof(path), "%s/%s", _dm_dir,
221                                 name) == -1) {
222                         log_error("Couldn't create path for %s", name);
223                         continue;
224                 }
225
226                 if (stat(path, &buf))
227                         continue;
228
229                 if (buf.st_rdev == st_rdev) {
230                         if (!(new_name = dm_strdup(name)))
231                                 log_error("dm_task_set_name: strdup(%s) failed",
232                                           name);
233                         break;
234                 }
235         }
236
237         if (closedir(d))
238                 log_sys_error("closedir", _dm_dir);
239
240         return new_name;
241 }
242
243 int dm_task_set_name(struct dm_task *dmt, const char *name)
244 {
245         char *pos;
246         char *new_name = NULL;
247         char path[PATH_MAX];
248         struct stat st1, st2;
249
250         if (dmt->dev_name) {
251                 dm_free(dmt->dev_name);
252                 dmt->dev_name = NULL;
253         }
254
255         /*
256          * Path supplied for existing device?
257          */
258         if ((pos = strrchr(name, '/'))) {
259                 if (dmt->type == DM_DEVICE_CREATE) {
260                         log_error("Name \"%s\" invalid. It contains \"/\".", name);
261                         return 0;
262                 }
263
264                 if (stat(name, &st1)) {
265                         log_error("Device %s not found", name);
266                         return 0;
267                 }
268
269                 /*
270                  * If supplied path points to same device as last component
271                  * under /dev/mapper, use that name directly.  Otherwise call
272                  * _find_dm_name_of_device() to scan _dm_dir for a match.
273                  */
274                 if (dm_snprintf(path, sizeof(path), "%s/%s", _dm_dir,
275                                 pos + 1) == -1) {
276                         log_error("Couldn't create path for %s", pos + 1);
277                         return 0;
278                 }
279
280                 if (!stat(path, &st2) && (st1.st_rdev == st2.st_rdev))
281                         name = pos + 1;
282                 else if ((new_name = _find_dm_name_of_device(st1.st_rdev)))
283                         name = new_name;
284                 else {
285                         log_error("Device %s not found", name);
286                         return 0;
287                 }
288         }
289
290         if (strlen(name) >= DM_NAME_LEN) {
291                 log_error("Name \"%s\" too long", name);
292                 if (new_name)
293                         dm_free(new_name);
294                 return 0;
295         }
296
297         if (new_name)
298                 dmt->dev_name = new_name;
299         else if (!(dmt->dev_name = dm_strdup(name))) {
300                 log_error("dm_task_set_name: strdup(%s) failed", name);
301                 return 0;
302         }
303
304         return 1;
305 }
306
307 int dm_task_set_uuid(struct dm_task *dmt, const char *uuid)
308 {
309         if (dmt->uuid) {
310                 dm_free(dmt->uuid);
311                 dmt->uuid = NULL;
312         }
313
314         if (!(dmt->uuid = dm_strdup(uuid))) {
315                 log_error("dm_task_set_uuid: strdup(%s) failed", uuid);
316                 return 0;
317         }
318
319         return 1;
320 }
321
322 int dm_task_set_major(struct dm_task *dmt, int major)
323 {
324         dmt->major = major;
325         dmt->allow_default_major_fallback = 0;
326
327         return 1;
328 }
329
330 int dm_task_set_minor(struct dm_task *dmt, int minor)
331 {
332         dmt->minor = minor;
333
334         return 1;
335 }
336
337 int dm_task_set_major_minor(struct dm_task *dmt, int major, int minor,
338                             int allow_default_major_fallback)
339 {
340         dmt->major = major;
341         dmt->minor = minor;
342         dmt->allow_default_major_fallback = allow_default_major_fallback;
343
344         return 1;
345 }
346
347 int dm_task_set_uid(struct dm_task *dmt, uid_t uid)
348 {
349         dmt->uid = uid;
350
351         return 1;
352 }
353
354 int dm_task_set_gid(struct dm_task *dmt, gid_t gid)
355 {
356         dmt->gid = gid;
357
358         return 1;
359 }
360
361 int dm_task_set_mode(struct dm_task *dmt, mode_t mode)
362 {
363         dmt->mode = mode;
364
365         return 1;
366 }
367
368 int dm_task_add_target(struct dm_task *dmt, uint64_t start, uint64_t size,
369                        const char *ttype, const char *params)
370 {
371         struct target *t = create_target(start, size, ttype, params);
372
373         if (!t)
374                 return 0;
375
376         if (!dmt->head)
377                 dmt->head = dmt->tail = t;
378         else {
379                 dmt->tail->next = t;
380                 dmt->tail = t;
381         }
382
383         return 1;
384 }
385
386 int dm_set_selinux_context(const char *path, mode_t mode)
387 {
388 #ifdef HAVE_SELINUX
389         security_context_t scontext;
390
391         if (is_selinux_enabled() <= 0)
392                 return 1;
393
394         if (matchpathcon(path, mode, &scontext) < 0) {
395                 log_error("%s: matchpathcon %07o failed: %s", path, mode,
396                           strerror(errno));
397                 return 0;
398         }
399
400         log_debug("Setting SELinux context for %s to %s.", path, scontext);
401
402         if ((lsetfilecon(path, scontext) < 0) && (errno != ENOTSUP)) {
403                 log_sys_error("lsetfilecon", path);
404                 freecon(scontext);
405                 return 0;
406         }
407
408         freecon(scontext);
409 #endif
410         return 1;
411 }
412
413 static int _add_dev_node(const char *dev_name, uint32_t major, uint32_t minor,
414                          uid_t uid, gid_t gid, mode_t mode, int check_udev)
415 {
416         char path[PATH_MAX];
417         struct stat info;
418         dev_t dev = MKDEV(major, minor);
419         mode_t old_mask;
420
421         #ifdef __NetBSD__
422         char rpath[PATH_MAX];
423         uint32_t raw_major;
424         dev_t rdev;
425         char raw_devname[DM_NAME_LEN+1]; /* r + other device name */
426
427         nbsd_get_dm_major(&raw_major,DM_CHAR_MAJOR);
428         rdev = MKDEV(raw_major,minor);
429
430         snprintf(raw_devname,sizeof(raw_devname),"r%s",dev_name);
431
432         _build_dev_path(rpath, sizeof(rpath), raw_devname);
433
434         if (stat(rpath, &info) >= 0) {
435                 if (!S_ISCHR(info.st_mode)) {
436                         log_error("A non-raw device file at '%s' "
437                             "is already present", rpath);
438                         return 0;
439                 }
440
441                 /* If right inode already exists we don't touch uid etc. */
442                 if (info.st_rdev == rdev)
443                         return 1;
444
445                 if (unlink(rpath) < 0) {
446                         log_error("Unable to unlink device node for '%s'",
447                             raw_devname);
448                         return 0;
449                 }
450         }
451
452         old_mask = umask(0);
453
454         if (mknod(rpath, S_IFCHR | mode, rdev) < 0) {
455                 log_error("Unable to make device node for '%s'", raw_devname);
456                 return 0;
457         }
458 #elif defined(__DragonFly__)
459         _build_dev_path(path, sizeof(path), dev_name);
460         log_error("Skipping all sanity checks here");
461         return 1;
462 #endif
463         
464         _build_dev_path(path, sizeof(path), dev_name);
465
466         if (stat(path, &info) >= 0) {
467                 if (!S_ISBLK(info.st_mode)) {
468                         log_error("A non-block device file at '%s' "
469                                   "is already present", path);
470                         return 0;
471                 }
472
473                 /* If right inode already exists we don't touch uid etc. */
474                 if (info.st_rdev == dev)
475                         return 1;
476
477                 if (unlink(path) < 0) {
478                         log_error("Unable to unlink device node for '%s'",
479                                   dev_name);
480                         return 0;
481                 }
482         } else if (dm_udev_get_sync_support() && check_udev)
483                 log_warn("%s not set up by udev: Falling back to direct "
484                          "node creation.", path);
485
486         old_mask = umask(0);
487         if (mknod(path, S_IFBLK | mode, dev) < 0) {
488                 umask(old_mask);
489                 log_error("Unable to make device node for '%s'", dev_name);
490                 return 0;
491         }
492         umask(old_mask);
493
494         if (chown(path, uid, gid) < 0) {
495                 log_sys_error("chown", path);
496                 return 0;
497         }
498
499         log_debug("Created %s", path);
500
501         if (!dm_set_selinux_context(path, S_IFBLK))
502                 return 0;
503
504         return 1;
505 }
506
507 static int _rm_dev_node(const char *dev_name, int check_udev)
508 {
509         char path[PATH_MAX];
510         struct stat info;
511
512         #ifdef __NetBSD__
513         char rpath[PATH_MAX];
514         char raw_devname[DM_NAME_LEN+1]; /* r + other device name */
515
516         snprintf(raw_devname,sizeof(raw_devname),"r%s",dev_name);
517
518         _build_dev_path(rpath, sizeof(rpath), raw_devname);
519
520         if (stat(rpath, &info) < 0)
521                 return 1;
522
523         if (unlink(rpath) < 0) {
524                 log_error("Unable to unlink device node for '%s'", raw_devname);
525                 return 0;
526         }
527
528         log_debug("Removed %s", rpath);
529 #endif
530         
531         _build_dev_path(path, sizeof(path), dev_name);
532
533         if (stat(path, &info) < 0)
534                 return 1;
535         else if (dm_udev_get_sync_support() && check_udev)
536                 log_warn("Node %s was not removed by udev. "
537                          "Falling back to direct node removal.", path);
538
539         if (unlink(path) < 0) {
540                 log_error("Unable to unlink device node for '%s'", dev_name);
541                 return 0;
542         }
543
544         log_debug("Removed %s", path);
545
546         return 1;
547 }
548
549 static int _rename_dev_node(const char *old_name, const char *new_name,
550                             int check_udev)
551 {
552         char oldpath[PATH_MAX];
553         char newpath[PATH_MAX];
554         struct stat info;
555
556 #ifdef __NetBSD__
557         char rpath[PATH_MAX];
558         char nrpath[PATH_MAX];
559         char raw_devname[DM_NAME_LEN+1]; /* r + other device name */
560         char nraw_devname[DM_NAME_LEN+1]; /* r + other device name */
561
562         snprintf(nraw_devname,sizeof(raw_devname),"r%s",new_name);
563         snprintf(raw_devname,sizeof(raw_devname),"r%s",old_name);
564
565         _build_dev_path(nrpath, sizeof(nrpath), nraw_devname);
566         _build_dev_path(rpath, sizeof(rpath), raw_devname);
567
568         if (stat(nrpath, &info) == 0) {
569                 if (S_ISBLK(info.st_mode)) {
570                         log_error("A block device file at '%s' "
571                             "is present where raw device should be.", newpath);
572                         return 0;
573                 }
574
575                 if (unlink(nrpath) < 0) {
576                         log_error("Unable to unlink device node for '%s'",
577                             nraw_devname);
578                         return 0;
579                 }
580         }
581
582         if (rename(rpath, nrpath) < 0) {
583                 log_error("Unable to rename device node from '%s' to '%s'",
584                     raw_devname, nraw_devname);
585                 return 0;
586         }
587
588         log_debug("Renamed %s to %s", rpath, nrpath);
589
590 #endif
591         
592         _build_dev_path(oldpath, sizeof(oldpath), old_name);
593         _build_dev_path(newpath, sizeof(newpath), new_name);
594
595         if (stat(newpath, &info) == 0) {
596                 if (!S_ISBLK(info.st_mode)) {
597                         log_error("A non-block device file at '%s' "
598                                   "is already present", newpath);
599                         return 0;
600                 }
601                 else if (dm_udev_get_sync_support() && check_udev) {
602                         if (stat(oldpath, &info) < 0 &&
603                                  errno == ENOENT)
604                                 /* assume udev already deleted this */
605                                 return 1;
606                         else {
607                                 log_warn("The node %s should have been renamed to %s "
608                                          "by udev but old node is still present. "
609                                          "Falling back to direct old node removal.",
610                                          oldpath, newpath);
611                                 return _rm_dev_node(old_name, 0);
612                         }
613                 }
614
615                 if (unlink(newpath) < 0) {
616                         if (errno == EPERM) {
617                                 /* devfs, entry has already been renamed */
618                                 return 1;
619                         }
620                         log_error("Unable to unlink device node for '%s'",
621                                   new_name);
622                         return 0;
623                 }
624         }
625         else if (dm_udev_get_sync_support() && check_udev)
626                 log_warn("The node %s should have been renamed to %s "
627                          "by udev but new node is not present. "
628                          "Falling back to direct node rename.",
629                          oldpath, newpath);
630
631         if (rename(oldpath, newpath) < 0) {
632                 log_error("Unable to rename device node from '%s' to '%s'",
633                           old_name, new_name);
634                 return 0;
635         }
636
637         log_debug("Renamed %s to %s", oldpath, newpath);
638
639         return 1;
640 }
641
642 #ifdef linux
643 static int _open_dev_node(const char *dev_name)
644 {
645         int fd = -1;
646         char path[PATH_MAX];
647
648         _build_dev_path(path, sizeof(path), dev_name);
649
650         if ((fd = open(path, O_RDONLY, 0)) < 0)
651                 log_sys_error("open", path);
652
653         return fd;
654 }
655
656 int get_dev_node_read_ahead(const char *dev_name, uint32_t *read_ahead)
657 {
658         int r = 1;
659         int fd;
660         long read_ahead_long;
661
662         if (!*dev_name) {
663                 log_error("Empty device name passed to BLKRAGET");
664                 return 0;
665         }
666
667         if ((fd = _open_dev_node(dev_name)) < 0)
668                 return_0;
669
670         if (ioctl(fd, BLKRAGET, &read_ahead_long)) {
671                 log_sys_error("BLKRAGET", dev_name);
672                 *read_ahead = 0;
673                 r = 0;
674         }  else {
675                 *read_ahead = (uint32_t) read_ahead_long;
676                 log_debug("%s: read ahead is %" PRIu32, dev_name, *read_ahead);
677         }
678
679         if (close(fd))
680                 stack;
681
682         return r;
683 }
684
685 static int _set_read_ahead(const char *dev_name, uint32_t read_ahead)
686 {
687         int r = 1;
688         int fd;
689         long read_ahead_long = (long) read_ahead;
690
691         if (!*dev_name) {
692                 log_error("Empty device name passed to BLKRAGET");
693                 return 0;
694         }
695
696         if ((fd = _open_dev_node(dev_name)) < 0)
697                 return_0;
698
699         log_debug("%s: Setting read ahead to %" PRIu32, dev_name, read_ahead);
700
701         if (ioctl(fd, BLKRASET, read_ahead_long)) {
702                 log_sys_error("BLKRASET", dev_name);
703                 r = 0;
704         }
705
706         if (close(fd))
707                 stack;
708
709         return r;
710 }
711
712 static int _set_dev_node_read_ahead(const char *dev_name, uint32_t read_ahead,
713                                     uint32_t read_ahead_flags)
714 {
715         uint32_t current_read_ahead;
716
717         if (read_ahead == DM_READ_AHEAD_AUTO)
718                 return 1;
719
720         if (read_ahead == DM_READ_AHEAD_NONE)
721                 read_ahead = 0;
722
723         if (read_ahead_flags & DM_READ_AHEAD_MINIMUM_FLAG) {
724                 if (!get_dev_node_read_ahead(dev_name, &current_read_ahead))
725                         return_0;
726
727                 if (current_read_ahead > read_ahead) {
728                         log_debug("%s: retaining kernel read ahead of %" PRIu32
729                                   " (requested %" PRIu32 ")",           
730                                   dev_name, current_read_ahead, read_ahead);
731                         return 1;
732                 }
733         }
734
735         return _set_read_ahead(dev_name, read_ahead);
736 }
737
738 #else
739
740 int get_dev_node_read_ahead(const char *dev_name, uint32_t *read_ahead)
741 {
742         *read_ahead = 0;
743
744         return 1;
745 }
746
747 static int _set_dev_node_read_ahead(const char *dev_name, uint32_t read_ahead,
748                                     uint32_t read_ahead_flags)
749 {
750         return 1;
751 }
752 #endif
753
754 typedef enum {
755         NODE_ADD,
756         NODE_DEL,
757         NODE_RENAME,
758         NODE_READ_AHEAD
759 } node_op_t;
760
761 static int _do_node_op(node_op_t type, const char *dev_name, uint32_t major,
762                        uint32_t minor, uid_t uid, gid_t gid, mode_t mode,
763                        const char *old_name, uint32_t read_ahead,
764                        uint32_t read_ahead_flags, int check_udev)
765 {
766         switch (type) {
767         case NODE_ADD:
768                 return _add_dev_node(dev_name, major, minor, uid, gid,
769                                      mode, check_udev);
770         case NODE_DEL:
771                 return _rm_dev_node(dev_name, check_udev);
772         case NODE_RENAME:
773                 return _rename_dev_node(old_name, dev_name, check_udev);
774         case NODE_READ_AHEAD:
775                 return _set_dev_node_read_ahead(dev_name, read_ahead,
776                                                 read_ahead_flags);
777         }
778
779         return 1;
780 }
781
782 static DM_LIST_INIT(_node_ops);
783
784 struct node_op_parms {
785         struct dm_list list;
786         node_op_t type;
787         char *dev_name;
788         uint32_t major;
789         uint32_t minor;
790         uid_t uid;
791         gid_t gid;
792         mode_t mode;
793         uint32_t read_ahead;
794         uint32_t read_ahead_flags;
795         char *old_name;
796         int check_udev;
797         char names[0];
798 };
799
800 static void _store_str(char **pos, char **ptr, const char *str)
801 {
802         strcpy(*pos, str);
803         *ptr = *pos;
804         *pos += strlen(*ptr) + 1;
805 }
806
807 static int _stack_node_op(node_op_t type, const char *dev_name, uint32_t major,
808                           uint32_t minor, uid_t uid, gid_t gid, mode_t mode,
809                           const char *old_name, uint32_t read_ahead,
810                           uint32_t read_ahead_flags, int check_udev)
811 {
812         struct node_op_parms *nop;
813         struct dm_list *noph, *nopht;
814         size_t len = strlen(dev_name) + strlen(old_name) + 2;
815         char *pos;
816
817         /*
818          * Ignore any outstanding operations on the node if deleting it
819          */
820         if (type == NODE_DEL) {
821                 dm_list_iterate_safe(noph, nopht, &_node_ops) {
822                         nop = dm_list_item(noph, struct node_op_parms);
823                         if (!strcmp(dev_name, nop->dev_name)) {
824                                 dm_list_del(&nop->list);
825                                 dm_free(nop);
826                         }
827                 }
828         }
829
830         if (!(nop = dm_malloc(sizeof(*nop) + len))) {
831                 log_error("Insufficient memory to stack mknod operation");
832                 return 0;
833         }
834
835         pos = nop->names;
836         nop->type = type;
837         nop->major = major;
838         nop->minor = minor;
839         nop->uid = uid;
840         nop->gid = gid;
841         nop->mode = mode;
842         nop->read_ahead = read_ahead;
843         nop->read_ahead_flags = read_ahead_flags;
844         nop->check_udev = check_udev;
845
846         _store_str(&pos, &nop->dev_name, dev_name);
847         _store_str(&pos, &nop->old_name, old_name);
848
849         dm_list_add(&_node_ops, &nop->list);
850
851         return 1;
852 }
853
854 static void _pop_node_ops(void)
855 {
856         struct dm_list *noph, *nopht;
857         struct node_op_parms *nop;
858
859         dm_list_iterate_safe(noph, nopht, &_node_ops) {
860                 nop = dm_list_item(noph, struct node_op_parms);
861                 _do_node_op(nop->type, nop->dev_name, nop->major, nop->minor,
862                             nop->uid, nop->gid, nop->mode, nop->old_name,
863                             nop->read_ahead, nop->read_ahead_flags,
864                             nop->check_udev);
865                 dm_list_del(&nop->list);
866                 dm_free(nop);
867         }
868 }
869
870 int add_dev_node(const char *dev_name, uint32_t major, uint32_t minor,
871                  uid_t uid, gid_t gid, mode_t mode, int check_udev)
872 {
873         log_debug("%s: Stacking NODE_ADD (%" PRIu32 ",%" PRIu32 ") %u:%u 0%o",
874                   dev_name, major, minor, uid, gid, mode);
875
876         return _stack_node_op(NODE_ADD, dev_name, major, minor, uid,
877                               gid, mode, "", 0, 0, check_udev);
878 }
879
880 int rename_dev_node(const char *old_name, const char *new_name, int check_udev)
881 {
882         log_debug("%s: Stacking NODE_RENAME to %s", old_name, new_name);
883
884         return _stack_node_op(NODE_RENAME, new_name, 0, 0, 0,
885                               0, 0, old_name, 0, 0, check_udev);
886 }
887
888 int rm_dev_node(const char *dev_name, int check_udev)
889 {
890         log_debug("%s: Stacking NODE_DEL (replaces other stacked ops)", dev_name);
891
892         return _stack_node_op(NODE_DEL, dev_name, 0, 0, 0,
893                               0, 0, "", 0, 0, check_udev);
894 }
895
896 int set_dev_node_read_ahead(const char *dev_name, uint32_t read_ahead,
897                             uint32_t read_ahead_flags)
898 {
899         if (read_ahead == DM_READ_AHEAD_AUTO)
900                 return 1;
901
902         log_debug("%s: Stacking NODE_READ_AHEAD %" PRIu32 " (flags=%" PRIu32
903                   ")", dev_name, read_ahead, read_ahead_flags);
904
905         return _stack_node_op(NODE_READ_AHEAD, dev_name, 0, 0, 0, 0,
906                               0, "", read_ahead, read_ahead_flags, 0);
907 }
908
909 void update_devs(void)
910 {
911         _pop_node_ops();
912 }
913
914 int dm_set_dev_dir(const char *dev_dir)
915 {
916         size_t len;
917         const char *slash;
918         if (*dev_dir != '/') {
919                 log_debug("Invalid dev_dir value, %s: "
920                           "not an absolute name.", dev_dir);
921                 return 0;
922         }
923
924         len = strlen(dev_dir);
925         slash = dev_dir[len-1] == '/' ? "" : "/";
926
927         if (snprintf(_dm_dir, sizeof _dm_dir, "%s%s%s", dev_dir, slash, DM_DIR)
928             >= sizeof _dm_dir) {
929                 log_debug("Invalid dev_dir value, %s: name too long.", dev_dir);
930                 return 0;
931         }
932
933         return 1;
934 }
935
936 const char *dm_dir(void)
937 {
938         return _dm_dir;
939 }
940
941 int dm_mknodes(const char *name)
942 {
943         struct dm_task *dmt;
944         int r = 0;
945
946         if (!(dmt = dm_task_create(DM_DEVICE_MKNODES)))
947                 return 0;
948
949         if (name && !dm_task_set_name(dmt, name))
950                 goto out;
951
952         if (!dm_task_no_open_count(dmt))
953                 goto out;
954
955         r = dm_task_run(dmt);
956
957 out:
958         dm_task_destroy(dmt);
959         return r;
960 }
961
962 int dm_driver_version(char *version, size_t size)
963 {
964         struct dm_task *dmt;
965         int r = 0;
966
967         if (!(dmt = dm_task_create(DM_DEVICE_VERSION)))
968                 return 0;
969
970         if (!dm_task_run(dmt))
971                 log_error("Failed to get driver version");
972
973         if (!dm_task_get_driver_version(dmt, version, size))
974                 goto out;
975
976         r = 1;
977
978 out:
979         dm_task_destroy(dmt);
980         return r;
981 }
982
983 #ifndef UDEV_SYNC_SUPPORT
984 void dm_udev_set_sync_support(int sync_with_udev)
985 {
986 }
987
988 int dm_udev_get_sync_support(void)
989 {
990         return 0;
991 }
992
993 int dm_task_set_cookie(struct dm_task *dmt, uint32_t *cookie, uint16_t flags)
994 {
995         if (dm_cookie_supported())
996                 dmt->event_nr = flags << DM_UDEV_FLAGS_SHIFT;
997         *cookie = 0;
998
999         return 1;
1000 }
1001
1002 int dm_udev_complete(uint32_t cookie)
1003 {
1004         return 1;
1005 }
1006
1007 int dm_udev_wait(uint32_t cookie)
1008 {
1009         return 1;
1010 }
1011
1012 #else           /* UDEV_SYNC_SUPPORT */
1013
1014
1015 static int _check_udev_is_running(void)
1016 {
1017
1018 #  ifndef HAVE_UDEV_QUEUE_GET_UDEV_IS_ACTIVE
1019
1020         log_debug("Could not get udev state because libudev library "
1021                   "was not found and it was not compiled in. "
1022                   "Assuming udev is not running.");
1023         return 0;
1024
1025 #  else /* HAVE_UDEV_QUEUE_GET_UDEV_IS_ACTIVE */
1026
1027         struct udev *udev;
1028         struct udev_queue *udev_queue;
1029         int r;
1030
1031         if (!(udev = udev_new()))
1032                 goto_bad;
1033
1034         if (!(udev_queue = udev_queue_new(udev))) {
1035                 udev_unref(udev);
1036                 goto_bad;
1037         }
1038
1039         if (!(r = udev_queue_get_udev_is_active(udev_queue)))
1040                 log_debug("Udev is not running. "
1041                           "Not using udev synchronisation code.");
1042
1043         udev_queue_unref(udev_queue);
1044         udev_unref(udev);
1045
1046         return r;
1047
1048 bad:
1049         log_error("Could not get udev state. Assuming udev is not running.");
1050         return 0;
1051
1052 #  endif        /* HAVE_UDEV_QUEUE_GET_UDEV_IS_ACTIVE */
1053
1054 }
1055
1056 void dm_udev_set_sync_support(int sync_with_udev)
1057 {
1058         if (_udev_running < 0)
1059                 _udev_running = _check_udev_is_running();
1060
1061         _sync_with_udev = sync_with_udev;
1062 }
1063
1064 int dm_udev_get_sync_support(void)
1065 {
1066         if (_udev_running < 0)
1067                 _udev_running = _check_udev_is_running();
1068
1069         return dm_cookie_supported() && _udev_running && _sync_with_udev;
1070 }
1071
1072 static int _get_cookie_sem(uint32_t cookie, int *semid)
1073 {
1074         if (cookie >> 16 != DM_COOKIE_MAGIC) {
1075                 log_error("Could not continue to access notification "
1076                           "semaphore identified by cookie value %"
1077                           PRIu32 " (0x%x). Incorrect cookie prefix.",
1078                           cookie, cookie);
1079                 return 0;
1080         }
1081
1082         if ((*semid = semget((key_t) cookie, 1, 0)) >= 0)
1083                 return 1;
1084
1085         switch (errno) {
1086                 case ENOENT:
1087                         log_error("Could not find notification "
1088                                   "semaphore identified by cookie "
1089                                   "value %" PRIu32 " (0x%x)",
1090                                   cookie, cookie);
1091                         break;
1092                 case EACCES:
1093                         log_error("No permission to access "
1094                                   "notificaton semaphore identified "
1095                                   "by cookie value %" PRIu32 " (0x%x)",
1096                                   cookie, cookie);
1097                         break;
1098                 default:
1099                         log_error("Failed to access notification "
1100                                    "semaphore identified by cookie "
1101                                    "value %" PRIu32 " (0x%x): %s",
1102                                   cookie, cookie, strerror(errno));
1103                         break;
1104         }
1105
1106         return 0;
1107 }
1108
1109 static int _udev_notify_sem_inc(uint32_t cookie, int semid)
1110 {
1111         struct sembuf sb = {0, 1, 0};
1112
1113         if (semop(semid, &sb, 1) < 0) {
1114                 log_error("semid %d: semop failed for cookie 0x%" PRIx32 ": %s",
1115                           semid, cookie, strerror(errno));
1116                 return 0;
1117         }
1118
1119         log_debug("Udev cookie 0x%" PRIx32 " (semid %d) incremented",
1120                   cookie, semid);
1121
1122         return 1;
1123 }
1124
1125 static int _udev_notify_sem_dec(uint32_t cookie, int semid)
1126 {
1127         struct sembuf sb = {0, -1, IPC_NOWAIT};
1128
1129         if (semop(semid, &sb, 1) < 0) {
1130                 switch (errno) {
1131                         case EAGAIN:
1132                                 log_error("semid %d: semop failed for cookie "
1133                                           "0x%" PRIx32 ": "
1134                                           "incorrect semaphore state",
1135                                           semid, cookie);
1136                                 break;
1137                         default:
1138                                 log_error("semid %d: semop failed for cookie "
1139                                           "0x%" PRIx32 ": %s",
1140                                           semid, cookie, strerror(errno));
1141                                 break;
1142                 }
1143                 return 0;
1144         }
1145
1146         log_debug("Udev cookie 0x%" PRIx32 " (semid %d) decremented",
1147                   cookie, semid);
1148
1149         return 1;
1150 }
1151
1152 static int _udev_notify_sem_destroy(uint32_t cookie, int semid)
1153 {
1154         if (semctl(semid, 0, IPC_RMID, 0) < 0) {
1155                 log_error("Could not cleanup notification semaphore "
1156                           "identified by cookie value %" PRIu32 " (0x%x): %s",
1157                           cookie, cookie, strerror(errno));
1158                 return 0;
1159         }
1160
1161         log_debug("Udev cookie 0x%" PRIx32 " (semid %d) destroyed", cookie,
1162                   semid);
1163
1164         return 1;
1165 }
1166
1167 static int _udev_notify_sem_create(uint32_t *cookie, int *semid)
1168 {
1169         int fd;
1170         int gen_semid;
1171         uint16_t base_cookie;
1172         uint32_t gen_cookie;
1173
1174         if ((fd = open("/dev/urandom", O_RDONLY)) < 0) {
1175                 log_error("Failed to open /dev/urandom "
1176                           "to create random cookie value");
1177                 *cookie = 0;
1178                 return 0;
1179         }
1180
1181         /* Generate random cookie value. Be sure it is unique and non-zero. */
1182         do {
1183                 /* FIXME Handle non-error returns from read(). Move _io() into libdm? */
1184                 if (read(fd, &base_cookie, sizeof(base_cookie)) != sizeof(base_cookie)) {
1185                         log_error("Failed to initialize notification cookie");
1186                         goto bad;
1187                 }
1188
1189                 gen_cookie = DM_COOKIE_MAGIC << 16 | base_cookie;
1190
1191                 if (base_cookie && (gen_semid = semget((key_t) gen_cookie,
1192                                     1, 0600 | IPC_CREAT | IPC_EXCL)) < 0) {
1193                         switch (errno) {
1194                                 case EEXIST:
1195                                         /* if the semaphore key exists, we
1196                                          * simply generate another random one */
1197                                         base_cookie = 0;
1198                                         break;
1199                                 case ENOMEM:
1200                                         log_error("Not enough memory to create "
1201                                                   "notification semaphore");
1202                                         goto bad;
1203                                 case ENOSPC:
1204                                         log_error("Limit for the maximum number "
1205                                                   "of semaphores reached. You can "
1206                                                   "check and set the limits in "
1207                                                   "/proc/sys/kernel/sem.");
1208                                         goto bad;
1209                                 default:
1210                                         log_error("Failed to create notification "
1211                                                   "semaphore: %s", strerror(errno));
1212                                         goto bad;
1213                         }
1214                 }
1215         } while (!base_cookie);
1216
1217         log_debug("Udev cookie 0x%" PRIx32 " (semid %d) created",
1218                   gen_cookie, gen_semid);
1219
1220         if (semctl(gen_semid, 0, SETVAL, 1) < 0) {
1221                 log_error("semid %d: semctl failed: %s", gen_semid, strerror(errno));
1222                 /* We have to destroy just created semaphore
1223                  * so it won't stay in the system. */
1224                 (void) _udev_notify_sem_destroy(gen_cookie, gen_semid);
1225                 goto bad;
1226         }
1227
1228         log_debug("Udev cookie 0x%" PRIx32 " (semid %d) incremented",
1229                   gen_cookie, gen_semid);
1230
1231         if (close(fd))
1232                 stack;
1233
1234         *semid = gen_semid;
1235         *cookie = gen_cookie;
1236
1237         return 1;
1238
1239 bad:
1240         if (close(fd))
1241                 stack;
1242
1243         *cookie = 0;
1244
1245         return 0;
1246 }
1247
1248 int dm_task_set_cookie(struct dm_task *dmt, uint32_t *cookie, uint16_t flags)
1249 {
1250         int semid;
1251
1252         if (dm_cookie_supported())
1253                 dmt->event_nr = flags << DM_UDEV_FLAGS_SHIFT;
1254
1255         if (!dm_udev_get_sync_support()) {
1256                 *cookie = 0;
1257                 return 1;
1258         }
1259
1260         if (*cookie) {
1261                 if (!_get_cookie_sem(*cookie, &semid))
1262                         goto_bad;
1263         } else if (!_udev_notify_sem_create(cookie, &semid))
1264                 goto_bad;
1265
1266         if (!_udev_notify_sem_inc(*cookie, semid)) {
1267                 log_error("Could not set notification semaphore "
1268                           "identified by cookie value %" PRIu32 " (0x%x)",
1269                           *cookie, *cookie);
1270                 goto bad;
1271         }
1272
1273         dmt->event_nr |= ~DM_UDEV_FLAGS_MASK & *cookie;
1274         dmt->cookie_set = 1;
1275
1276         log_debug("Udev cookie 0x%" PRIx32 " (semid %d) assigned to dm_task "
1277                   "with flags 0x%" PRIx16, *cookie, semid, flags);
1278
1279         return 1;
1280
1281 bad:
1282         dmt->event_nr = 0;
1283         return 0;
1284 }
1285
1286 int dm_udev_complete(uint32_t cookie)
1287 {
1288         int semid;
1289
1290         if (!cookie || !dm_udev_get_sync_support())
1291                 return 1;
1292
1293         if (!_get_cookie_sem(cookie, &semid))
1294                 return_0;
1295
1296         if (!_udev_notify_sem_dec(cookie, semid)) {
1297                 log_error("Could not signal waiting process using notification "
1298                           "semaphore identified by cookie value %" PRIu32 " (0x%x)",
1299                           cookie, cookie);
1300                 return 0;
1301         }
1302
1303         return 1;
1304 }
1305
1306 int dm_udev_wait(uint32_t cookie)
1307 {
1308         int semid;
1309         struct sembuf sb = {0, 0, 0};
1310
1311         if (!cookie || !dm_udev_get_sync_support())
1312                 return 1;
1313
1314         if (!_get_cookie_sem(cookie, &semid))
1315                 return_0;
1316
1317         if (!_udev_notify_sem_dec(cookie, semid)) {
1318                 log_error("Failed to set a proper state for notification "
1319                           "semaphore identified by cookie value %" PRIu32 " (0x%x) "
1320                           "to initialize waiting for incoming notifications.",
1321                           cookie, cookie);
1322                 (void) _udev_notify_sem_destroy(cookie, semid);
1323                 return 0;
1324         }
1325
1326         log_debug("Udev cookie 0x%" PRIx32 " (semid %d): Waiting for zero",
1327                   cookie, semid);
1328
1329 repeat_wait:
1330         if (semop(semid, &sb, 1) < 0) {
1331                 if (errno == EINTR)
1332                         goto repeat_wait;
1333                 else if (errno == EIDRM)
1334                         return 1;
1335
1336                 log_error("Could not set wait state for notification semaphore "
1337                           "identified by cookie value %" PRIu32 " (0x%x): %s",
1338                           cookie, cookie, strerror(errno));
1339                 (void) _udev_notify_sem_destroy(cookie, semid);
1340                 return 0;
1341         }
1342
1343         return _udev_notify_sem_destroy(cookie, semid);
1344 }
1345
1346 #endif          /* UDEV_SYNC_SUPPORT */