Sync usage with man page.
[netbsd-mini2440.git] / external / gpl2 / lvm2 / dist / libdm / libdm-common.c
blob27d42f096fdd99d20f94289314b4b9aea16d960f
1 /* $NetBSD: libdm-common.c,v 1.4 2009/12/02 00:58:03 haad Exp $ */
3 /*
4 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
7 * This file is part of the device-mapper userspace tools.
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.
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
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"
26 #include <stdarg.h>
27 #include <sys/param.h>
28 #include <sys/ioctl.h>
29 #include <fcntl.h>
30 #include <dirent.h>
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
42 #ifdef linux
43 # include <linux/fs.h>
44 #endif
46 #ifdef HAVE_SELINUX
47 # include <selinux/selinux.h>
48 #endif
50 #ifdef __NetBSD__
51 #include "libdm-netbsd.h"
52 #endif
54 #define DEV_DIR "/dev/"
56 static char _dm_dir[PATH_MAX] = DEV_DIR DM_DIR;
58 static int _verbose = 0;
60 #ifdef UDEV_SYNC_SUPPORT
61 static int _udev_running = -1;
62 static int _sync_with_udev = 1;
63 #endif
66 * Library users can provide their own logging
67 * function.
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)
75 int use_stderr = level & _LOG_STDERR;
77 level &= ~_LOG_STDERR;
79 if (level > _LOG_WARN && !_verbose)
80 return;
82 if (level < _LOG_WARN)
83 vfprintf(stderr, f, ap);
84 else
85 vfprintf(use_stderr ? stderr : stdout, f, ap);
87 if (level < _LOG_WARN)
88 fprintf(stderr, "\n");
89 else
90 fprintf(use_stderr ? stderr : stdout, "\n");
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, ...)
98 va_list ap;
100 va_start(ap, f);
101 _default_log_line(level, file, line, dm_errno, f, ap);
102 va_end(ap);
105 static void _default_log(int level, const char *file,
106 int line, const char *f, ...)
108 va_list ap;
110 va_start(ap, f);
111 _default_log_line(level, file, line, 0, f, ap);
112 va_end(ap);
115 dm_log_fn dm_log = _default_log;
116 dm_log_with_errno_fn dm_log_with_errno = _default_log_with_errno;
118 void dm_log_init(dm_log_fn fn)
120 if (fn)
121 dm_log = fn;
122 else
123 dm_log = _default_log;
125 dm_log_with_errno = _default_log_with_errno;
128 int dm_log_is_non_default(void)
130 return (dm_log == _default_log) ? 0 : 1;
133 void dm_log_with_errno_init(dm_log_with_errno_fn fn)
135 if (fn)
136 dm_log_with_errno = fn;
137 else
138 dm_log_with_errno = _default_log_with_errno;
140 dm_log = _default_log;
143 void dm_log_init_verbose(int level)
145 _verbose = level;
148 static void _build_dev_path(char *buffer, size_t len, const char *dev_name)
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);
157 int dm_get_library_version(char *version, size_t size)
159 strncpy(version, DM_LIB_VERSION, size);
160 return 1;
163 struct dm_task *dm_task_create(int type)
165 struct dm_task *dmt = dm_malloc(sizeof(*dmt));
167 if (!dmt) {
168 log_error("dm_task_create: malloc(%" PRIsize_t ") failed",
169 sizeof(*dmt));
170 return NULL;
173 if (!dm_check_version()) {
174 dm_free(dmt);
175 return NULL;
178 memset(dmt, 0, sizeof(*dmt));
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;
194 return dmt;
198 * Find the name associated with a given device number by scanning _dm_dir.
200 static char *_find_dm_name_of_device(dev_t st_rdev)
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;
209 if (!(d = opendir(_dm_dir))) {
210 log_sys_error("opendir", _dm_dir);
211 return NULL;
214 while ((dirent = readdir(d))) {
215 name = dirent->d_name;
217 if (!strcmp(name, ".") || !strcmp(name, ".."))
218 continue;
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;
226 if (stat(path, &buf))
227 continue;
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;
237 if (closedir(d))
238 log_sys_error("closedir", _dm_dir);
240 return new_name;
243 int dm_task_set_name(struct dm_task *dmt, const char *name)
245 char *pos;
246 char *new_name = NULL;
247 char path[PATH_MAX];
248 struct stat st1, st2;
250 if (dmt->dev_name) {
251 dm_free(dmt->dev_name);
252 dmt->dev_name = NULL;
256 * Path supplied for existing device?
258 if ((pos = strrchr(name, '/'))) {
259 if (dmt->type == DM_DEVICE_CREATE) {
260 log_error("Name \"%s\" invalid. It contains \"/\".", name);
261 return 0;
264 if (stat(name, &st1)) {
265 log_error("Device %s not found", name);
266 return 0;
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.
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;
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;
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;
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;
304 return 1;
307 int dm_task_set_uuid(struct dm_task *dmt, const char *uuid)
309 if (dmt->uuid) {
310 dm_free(dmt->uuid);
311 dmt->uuid = NULL;
314 if (!(dmt->uuid = dm_strdup(uuid))) {
315 log_error("dm_task_set_uuid: strdup(%s) failed", uuid);
316 return 0;
319 return 1;
322 int dm_task_set_major(struct dm_task *dmt, int major)
324 dmt->major = major;
325 dmt->allow_default_major_fallback = 0;
327 return 1;
330 int dm_task_set_minor(struct dm_task *dmt, int minor)
332 dmt->minor = minor;
334 return 1;
337 int dm_task_set_major_minor(struct dm_task *dmt, int major, int minor,
338 int allow_default_major_fallback)
340 dmt->major = major;
341 dmt->minor = minor;
342 dmt->allow_default_major_fallback = allow_default_major_fallback;
344 return 1;
347 int dm_task_set_uid(struct dm_task *dmt, uid_t uid)
349 dmt->uid = uid;
351 return 1;
354 int dm_task_set_gid(struct dm_task *dmt, gid_t gid)
356 dmt->gid = gid;
358 return 1;
361 int dm_task_set_mode(struct dm_task *dmt, mode_t mode)
363 dmt->mode = mode;
365 return 1;
368 int dm_task_add_target(struct dm_task *dmt, uint64_t start, uint64_t size,
369 const char *ttype, const char *params)
371 struct target *t = create_target(start, size, ttype, params);
373 if (!t)
374 return 0;
376 if (!dmt->head)
377 dmt->head = dmt->tail = t;
378 else {
379 dmt->tail->next = t;
380 dmt->tail = t;
383 return 1;
386 int dm_set_selinux_context(const char *path, mode_t mode)
388 #ifdef HAVE_SELINUX
389 security_context_t scontext;
391 if (is_selinux_enabled() <= 0)
392 return 1;
394 if (matchpathcon(path, mode, &scontext) < 0) {
395 log_error("%s: matchpathcon %07o failed: %s", path, mode,
396 strerror(errno));
397 return 0;
400 log_debug("Setting SELinux context for %s to %s.", path, scontext);
402 if ((lsetfilecon(path, scontext) < 0) && (errno != ENOTSUP)) {
403 log_sys_error("lsetfilecon", path);
404 freecon(scontext);
405 return 0;
408 freecon(scontext);
409 #endif
410 return 1;
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)
416 char path[PATH_MAX];
417 struct stat info;
418 dev_t dev = MKDEV(major, minor);
419 mode_t old_mask;
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 */
427 nbsd_get_dm_major(&raw_major,DM_CHAR_MAJOR);
428 rdev = MKDEV(raw_major,minor);
430 snprintf(raw_devname,sizeof(raw_devname),"r%s",dev_name);
432 _build_dev_path(rpath, sizeof(rpath), raw_devname);
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;
441 /* If right inode already exists we don't touch uid etc. */
442 if (info.st_rdev == rdev)
443 return 1;
445 if (unlink(rpath) < 0) {
446 log_error("Unable to unlink device node for '%s'",
447 raw_devname);
448 return 0;
452 old_mask = umask(0);
454 if (mknod(rpath, S_IFCHR | mode, rdev) < 0) {
455 log_error("Unable to make device node for '%s'", raw_devname);
456 return 0;
458 #endif
460 _build_dev_path(path, sizeof(path), dev_name);
462 if (stat(path, &info) >= 0) {
463 if (!S_ISBLK(info.st_mode)) {
464 log_error("A non-block device file at '%s' "
465 "is already present", path);
466 return 0;
469 /* If right inode already exists we don't touch uid etc. */
470 if (info.st_rdev == dev)
471 return 1;
473 if (unlink(path) < 0) {
474 log_error("Unable to unlink device node for '%s'",
475 dev_name);
476 return 0;
478 } else if (dm_udev_get_sync_support() && check_udev)
479 log_warn("%s not set up by udev: Falling back to direct "
480 "node creation.", path);
482 old_mask = umask(0);
483 if (mknod(path, S_IFBLK | mode, dev) < 0) {
484 umask(old_mask);
485 log_error("Unable to make device node for '%s'", dev_name);
486 return 0;
488 umask(old_mask);
490 if (chown(path, uid, gid) < 0) {
491 log_sys_error("chown", path);
492 return 0;
495 log_debug("Created %s", path);
497 if (!dm_set_selinux_context(path, S_IFBLK))
498 return 0;
500 return 1;
503 static int _rm_dev_node(const char *dev_name, int check_udev)
505 char path[PATH_MAX];
506 struct stat info;
508 #ifdef __NetBSD__
509 char rpath[PATH_MAX];
510 char raw_devname[DM_NAME_LEN+1]; /* r + other device name */
512 snprintf(raw_devname,sizeof(raw_devname),"r%s",dev_name);
514 _build_dev_path(rpath, sizeof(rpath), raw_devname);
516 if (stat(rpath, &info) < 0)
517 return 1;
519 if (unlink(rpath) < 0) {
520 log_error("Unable to unlink device node for '%s'", raw_devname);
521 return 0;
524 log_debug("Removed %s", rpath);
525 #endif
527 _build_dev_path(path, sizeof(path), dev_name);
529 if (stat(path, &info) < 0)
530 return 1;
531 else if (dm_udev_get_sync_support() && check_udev)
532 log_warn("Node %s was not removed by udev. "
533 "Falling back to direct node removal.", path);
535 if (unlink(path) < 0) {
536 log_error("Unable to unlink device node for '%s'", dev_name);
537 return 0;
540 log_debug("Removed %s", path);
542 return 1;
545 static int _rename_dev_node(const char *old_name, const char *new_name,
546 int check_udev)
548 char oldpath[PATH_MAX];
549 char newpath[PATH_MAX];
550 struct stat info;
552 #ifdef __NetBSD__
553 char rpath[PATH_MAX];
554 char nrpath[PATH_MAX];
555 char raw_devname[DM_NAME_LEN+1]; /* r + other device name */
556 char nraw_devname[DM_NAME_LEN+1]; /* r + other device name */
558 snprintf(nraw_devname,sizeof(raw_devname),"r%s",new_name);
559 snprintf(raw_devname,sizeof(raw_devname),"r%s",old_name);
561 _build_dev_path(nrpath, sizeof(nrpath), nraw_devname);
562 _build_dev_path(rpath, sizeof(rpath), raw_devname);
564 if (stat(nrpath, &info) == 0) {
565 if (S_ISBLK(info.st_mode)) {
566 log_error("A block device file at '%s' "
567 "is present where raw device should be.", newpath);
568 return 0;
571 if (unlink(nrpath) < 0) {
572 log_error("Unable to unlink device node for '%s'",
573 nraw_devname);
574 return 0;
578 if (rename(rpath, nrpath) < 0) {
579 log_error("Unable to rename device node from '%s' to '%s'",
580 raw_devname, nraw_devname);
581 return 0;
584 log_debug("Renamed %s to %s", rpath, nrpath);
586 #endif
588 _build_dev_path(oldpath, sizeof(oldpath), old_name);
589 _build_dev_path(newpath, sizeof(newpath), new_name);
591 if (stat(newpath, &info) == 0) {
592 if (!S_ISBLK(info.st_mode)) {
593 log_error("A non-block device file at '%s' "
594 "is already present", newpath);
595 return 0;
597 else if (dm_udev_get_sync_support() && check_udev) {
598 if (stat(oldpath, &info) < 0 &&
599 errno == ENOENT)
600 /* assume udev already deleted this */
601 return 1;
602 else {
603 log_warn("The node %s should have been renamed to %s "
604 "by udev but old node is still present. "
605 "Falling back to direct old node removal.",
606 oldpath, newpath);
607 return _rm_dev_node(old_name, 0);
611 if (unlink(newpath) < 0) {
612 if (errno == EPERM) {
613 /* devfs, entry has already been renamed */
614 return 1;
616 log_error("Unable to unlink device node for '%s'",
617 new_name);
618 return 0;
621 else if (dm_udev_get_sync_support() && check_udev)
622 log_warn("The node %s should have been renamed to %s "
623 "by udev but new node is not present. "
624 "Falling back to direct node rename.",
625 oldpath, newpath);
627 if (rename(oldpath, newpath) < 0) {
628 log_error("Unable to rename device node from '%s' to '%s'",
629 old_name, new_name);
630 return 0;
633 log_debug("Renamed %s to %s", oldpath, newpath);
635 return 1;
638 #ifdef linux
639 static int _open_dev_node(const char *dev_name)
641 int fd = -1;
642 char path[PATH_MAX];
644 _build_dev_path(path, sizeof(path), dev_name);
646 if ((fd = open(path, O_RDONLY, 0)) < 0)
647 log_sys_error("open", path);
649 return fd;
652 int get_dev_node_read_ahead(const char *dev_name, uint32_t *read_ahead)
654 int r = 1;
655 int fd;
656 long read_ahead_long;
658 if (!*dev_name) {
659 log_error("Empty device name passed to BLKRAGET");
660 return 0;
663 if ((fd = _open_dev_node(dev_name)) < 0)
664 return_0;
666 if (ioctl(fd, BLKRAGET, &read_ahead_long)) {
667 log_sys_error("BLKRAGET", dev_name);
668 *read_ahead = 0;
669 r = 0;
670 } else {
671 *read_ahead = (uint32_t) read_ahead_long;
672 log_debug("%s: read ahead is %" PRIu32, dev_name, *read_ahead);
675 if (close(fd))
676 stack;
678 return r;
681 static int _set_read_ahead(const char *dev_name, uint32_t read_ahead)
683 int r = 1;
684 int fd;
685 long read_ahead_long = (long) read_ahead;
687 if (!*dev_name) {
688 log_error("Empty device name passed to BLKRAGET");
689 return 0;
692 if ((fd = _open_dev_node(dev_name)) < 0)
693 return_0;
695 log_debug("%s: Setting read ahead to %" PRIu32, dev_name, read_ahead);
697 if (ioctl(fd, BLKRASET, read_ahead_long)) {
698 log_sys_error("BLKRASET", dev_name);
699 r = 0;
702 if (close(fd))
703 stack;
705 return r;
708 static int _set_dev_node_read_ahead(const char *dev_name, uint32_t read_ahead,
709 uint32_t read_ahead_flags)
711 uint32_t current_read_ahead;
713 if (read_ahead == DM_READ_AHEAD_AUTO)
714 return 1;
716 if (read_ahead == DM_READ_AHEAD_NONE)
717 read_ahead = 0;
719 if (read_ahead_flags & DM_READ_AHEAD_MINIMUM_FLAG) {
720 if (!get_dev_node_read_ahead(dev_name, &current_read_ahead))
721 return_0;
723 if (current_read_ahead > read_ahead) {
724 log_debug("%s: retaining kernel read ahead of %" PRIu32
725 " (requested %" PRIu32 ")",
726 dev_name, current_read_ahead, read_ahead);
727 return 1;
731 return _set_read_ahead(dev_name, read_ahead);
734 #else
736 int get_dev_node_read_ahead(const char *dev_name, uint32_t *read_ahead)
738 *read_ahead = 0;
740 return 1;
743 static int _set_dev_node_read_ahead(const char *dev_name, uint32_t read_ahead,
744 uint32_t read_ahead_flags)
746 return 1;
748 #endif
750 typedef enum {
751 NODE_ADD,
752 NODE_DEL,
753 NODE_RENAME,
754 NODE_READ_AHEAD
755 } node_op_t;
757 static int _do_node_op(node_op_t type, const char *dev_name, uint32_t major,
758 uint32_t minor, uid_t uid, gid_t gid, mode_t mode,
759 const char *old_name, uint32_t read_ahead,
760 uint32_t read_ahead_flags, int check_udev)
762 switch (type) {
763 case NODE_ADD:
764 return _add_dev_node(dev_name, major, minor, uid, gid,
765 mode, check_udev);
766 case NODE_DEL:
767 return _rm_dev_node(dev_name, check_udev);
768 case NODE_RENAME:
769 return _rename_dev_node(old_name, dev_name, check_udev);
770 case NODE_READ_AHEAD:
771 return _set_dev_node_read_ahead(dev_name, read_ahead,
772 read_ahead_flags);
775 return 1;
778 static DM_LIST_INIT(_node_ops);
780 struct node_op_parms {
781 struct dm_list list;
782 node_op_t type;
783 char *dev_name;
784 uint32_t major;
785 uint32_t minor;
786 uid_t uid;
787 gid_t gid;
788 mode_t mode;
789 uint32_t read_ahead;
790 uint32_t read_ahead_flags;
791 char *old_name;
792 int check_udev;
793 char names[0];
796 static void _store_str(char **pos, char **ptr, const char *str)
798 strcpy(*pos, str);
799 *ptr = *pos;
800 *pos += strlen(*ptr) + 1;
803 static int _stack_node_op(node_op_t type, const char *dev_name, uint32_t major,
804 uint32_t minor, uid_t uid, gid_t gid, mode_t mode,
805 const char *old_name, uint32_t read_ahead,
806 uint32_t read_ahead_flags, int check_udev)
808 struct node_op_parms *nop;
809 struct dm_list *noph, *nopht;
810 size_t len = strlen(dev_name) + strlen(old_name) + 2;
811 char *pos;
814 * Ignore any outstanding operations on the node if deleting it
816 if (type == NODE_DEL) {
817 dm_list_iterate_safe(noph, nopht, &_node_ops) {
818 nop = dm_list_item(noph, struct node_op_parms);
819 if (!strcmp(dev_name, nop->dev_name)) {
820 dm_list_del(&nop->list);
821 dm_free(nop);
826 if (!(nop = dm_malloc(sizeof(*nop) + len))) {
827 log_error("Insufficient memory to stack mknod operation");
828 return 0;
831 pos = nop->names;
832 nop->type = type;
833 nop->major = major;
834 nop->minor = minor;
835 nop->uid = uid;
836 nop->gid = gid;
837 nop->mode = mode;
838 nop->read_ahead = read_ahead;
839 nop->read_ahead_flags = read_ahead_flags;
840 nop->check_udev = check_udev;
842 _store_str(&pos, &nop->dev_name, dev_name);
843 _store_str(&pos, &nop->old_name, old_name);
845 dm_list_add(&_node_ops, &nop->list);
847 return 1;
850 static void _pop_node_ops(void)
852 struct dm_list *noph, *nopht;
853 struct node_op_parms *nop;
855 dm_list_iterate_safe(noph, nopht, &_node_ops) {
856 nop = dm_list_item(noph, struct node_op_parms);
857 _do_node_op(nop->type, nop->dev_name, nop->major, nop->minor,
858 nop->uid, nop->gid, nop->mode, nop->old_name,
859 nop->read_ahead, nop->read_ahead_flags,
860 nop->check_udev);
861 dm_list_del(&nop->list);
862 dm_free(nop);
866 int add_dev_node(const char *dev_name, uint32_t major, uint32_t minor,
867 uid_t uid, gid_t gid, mode_t mode, int check_udev)
869 log_debug("%s: Stacking NODE_ADD (%" PRIu32 ",%" PRIu32 ") %u:%u 0%o",
870 dev_name, major, minor, uid, gid, mode);
872 return _stack_node_op(NODE_ADD, dev_name, major, minor, uid,
873 gid, mode, "", 0, 0, check_udev);
876 int rename_dev_node(const char *old_name, const char *new_name, int check_udev)
878 log_debug("%s: Stacking NODE_RENAME to %s", old_name, new_name);
880 return _stack_node_op(NODE_RENAME, new_name, 0, 0, 0,
881 0, 0, old_name, 0, 0, check_udev);
884 int rm_dev_node(const char *dev_name, int check_udev)
886 log_debug("%s: Stacking NODE_DEL (replaces other stacked ops)", dev_name);
888 return _stack_node_op(NODE_DEL, dev_name, 0, 0, 0,
889 0, 0, "", 0, 0, check_udev);
892 int set_dev_node_read_ahead(const char *dev_name, uint32_t read_ahead,
893 uint32_t read_ahead_flags)
895 if (read_ahead == DM_READ_AHEAD_AUTO)
896 return 1;
898 log_debug("%s: Stacking NODE_READ_AHEAD %" PRIu32 " (flags=%" PRIu32
899 ")", dev_name, read_ahead, read_ahead_flags);
901 return _stack_node_op(NODE_READ_AHEAD, dev_name, 0, 0, 0, 0,
902 0, "", read_ahead, read_ahead_flags, 0);
905 void update_devs(void)
907 _pop_node_ops();
910 int dm_set_dev_dir(const char *dev_dir)
912 size_t len;
913 const char *slash;
914 if (*dev_dir != '/') {
915 log_debug("Invalid dev_dir value, %s: "
916 "not an absolute name.", dev_dir);
917 return 0;
920 len = strlen(dev_dir);
921 slash = dev_dir[len-1] == '/' ? "" : "/";
923 if (snprintf(_dm_dir, sizeof _dm_dir, "%s%s%s", dev_dir, slash, DM_DIR)
924 >= sizeof _dm_dir) {
925 log_debug("Invalid dev_dir value, %s: name too long.", dev_dir);
926 return 0;
929 return 1;
932 const char *dm_dir(void)
934 return _dm_dir;
937 int dm_mknodes(const char *name)
939 struct dm_task *dmt;
940 int r = 0;
942 if (!(dmt = dm_task_create(DM_DEVICE_MKNODES)))
943 return 0;
945 if (name && !dm_task_set_name(dmt, name))
946 goto out;
948 if (!dm_task_no_open_count(dmt))
949 goto out;
951 r = dm_task_run(dmt);
953 out:
954 dm_task_destroy(dmt);
955 return r;
958 int dm_driver_version(char *version, size_t size)
960 struct dm_task *dmt;
961 int r = 0;
963 if (!(dmt = dm_task_create(DM_DEVICE_VERSION)))
964 return 0;
966 if (!dm_task_run(dmt))
967 log_error("Failed to get driver version");
969 if (!dm_task_get_driver_version(dmt, version, size))
970 goto out;
972 r = 1;
974 out:
975 dm_task_destroy(dmt);
976 return r;
979 #ifndef UDEV_SYNC_SUPPORT
980 void dm_udev_set_sync_support(int sync_with_udev)
984 int dm_udev_get_sync_support(void)
986 return 0;
989 int dm_task_set_cookie(struct dm_task *dmt, uint32_t *cookie, uint16_t flags)
991 if (dm_cookie_supported())
992 dmt->event_nr = flags << DM_UDEV_FLAGS_SHIFT;
993 *cookie = 0;
995 return 1;
998 int dm_udev_complete(uint32_t cookie)
1000 return 1;
1003 int dm_udev_wait(uint32_t cookie)
1005 return 1;
1008 #else /* UDEV_SYNC_SUPPORT */
1011 static int _check_udev_is_running(void)
1014 # ifndef HAVE_UDEV_QUEUE_GET_UDEV_IS_ACTIVE
1016 log_debug("Could not get udev state because libudev library "
1017 "was not found and it was not compiled in. "
1018 "Assuming udev is not running.");
1019 return 0;
1021 # else /* HAVE_UDEV_QUEUE_GET_UDEV_IS_ACTIVE */
1023 struct udev *udev;
1024 struct udev_queue *udev_queue;
1025 int r;
1027 if (!(udev = udev_new()))
1028 goto_bad;
1030 if (!(udev_queue = udev_queue_new(udev))) {
1031 udev_unref(udev);
1032 goto_bad;
1035 if (!(r = udev_queue_get_udev_is_active(udev_queue)))
1036 log_debug("Udev is not running. "
1037 "Not using udev synchronisation code.");
1039 udev_queue_unref(udev_queue);
1040 udev_unref(udev);
1042 return r;
1044 bad:
1045 log_error("Could not get udev state. Assuming udev is not running.");
1046 return 0;
1048 # endif /* HAVE_UDEV_QUEUE_GET_UDEV_IS_ACTIVE */
1052 void dm_udev_set_sync_support(int sync_with_udev)
1054 if (_udev_running < 0)
1055 _udev_running = _check_udev_is_running();
1057 _sync_with_udev = sync_with_udev;
1060 int dm_udev_get_sync_support(void)
1062 if (_udev_running < 0)
1063 _udev_running = _check_udev_is_running();
1065 return dm_cookie_supported() && _udev_running && _sync_with_udev;
1068 static int _get_cookie_sem(uint32_t cookie, int *semid)
1070 if (cookie >> 16 != DM_COOKIE_MAGIC) {
1071 log_error("Could not continue to access notification "
1072 "semaphore identified by cookie value %"
1073 PRIu32 " (0x%x). Incorrect cookie prefix.",
1074 cookie, cookie);
1075 return 0;
1078 if ((*semid = semget((key_t) cookie, 1, 0)) >= 0)
1079 return 1;
1081 switch (errno) {
1082 case ENOENT:
1083 log_error("Could not find notification "
1084 "semaphore identified by cookie "
1085 "value %" PRIu32 " (0x%x)",
1086 cookie, cookie);
1087 break;
1088 case EACCES:
1089 log_error("No permission to access "
1090 "notificaton semaphore identified "
1091 "by cookie value %" PRIu32 " (0x%x)",
1092 cookie, cookie);
1093 break;
1094 default:
1095 log_error("Failed to access notification "
1096 "semaphore identified by cookie "
1097 "value %" PRIu32 " (0x%x): %s",
1098 cookie, cookie, strerror(errno));
1099 break;
1102 return 0;
1105 static int _udev_notify_sem_inc(uint32_t cookie, int semid)
1107 struct sembuf sb = {0, 1, 0};
1109 if (semop(semid, &sb, 1) < 0) {
1110 log_error("semid %d: semop failed for cookie 0x%" PRIx32 ": %s",
1111 semid, cookie, strerror(errno));
1112 return 0;
1115 log_debug("Udev cookie 0x%" PRIx32 " (semid %d) incremented",
1116 cookie, semid);
1118 return 1;
1121 static int _udev_notify_sem_dec(uint32_t cookie, int semid)
1123 struct sembuf sb = {0, -1, IPC_NOWAIT};
1125 if (semop(semid, &sb, 1) < 0) {
1126 switch (errno) {
1127 case EAGAIN:
1128 log_error("semid %d: semop failed for cookie "
1129 "0x%" PRIx32 ": "
1130 "incorrect semaphore state",
1131 semid, cookie);
1132 break;
1133 default:
1134 log_error("semid %d: semop failed for cookie "
1135 "0x%" PRIx32 ": %s",
1136 semid, cookie, strerror(errno));
1137 break;
1139 return 0;
1142 log_debug("Udev cookie 0x%" PRIx32 " (semid %d) decremented",
1143 cookie, semid);
1145 return 1;
1148 static int _udev_notify_sem_destroy(uint32_t cookie, int semid)
1150 if (semctl(semid, 0, IPC_RMID, 0) < 0) {
1151 log_error("Could not cleanup notification semaphore "
1152 "identified by cookie value %" PRIu32 " (0x%x): %s",
1153 cookie, cookie, strerror(errno));
1154 return 0;
1157 log_debug("Udev cookie 0x%" PRIx32 " (semid %d) destroyed", cookie,
1158 semid);
1160 return 1;
1163 static int _udev_notify_sem_create(uint32_t *cookie, int *semid)
1165 int fd;
1166 int gen_semid;
1167 uint16_t base_cookie;
1168 uint32_t gen_cookie;
1170 if ((fd = open("/dev/urandom", O_RDONLY)) < 0) {
1171 log_error("Failed to open /dev/urandom "
1172 "to create random cookie value");
1173 *cookie = 0;
1174 return 0;
1177 /* Generate random cookie value. Be sure it is unique and non-zero. */
1178 do {
1179 /* FIXME Handle non-error returns from read(). Move _io() into libdm? */
1180 if (read(fd, &base_cookie, sizeof(base_cookie)) != sizeof(base_cookie)) {
1181 log_error("Failed to initialize notification cookie");
1182 goto bad;
1185 gen_cookie = DM_COOKIE_MAGIC << 16 | base_cookie;
1187 if (base_cookie && (gen_semid = semget((key_t) gen_cookie,
1188 1, 0600 | IPC_CREAT | IPC_EXCL)) < 0) {
1189 switch (errno) {
1190 case EEXIST:
1191 /* if the semaphore key exists, we
1192 * simply generate another random one */
1193 base_cookie = 0;
1194 break;
1195 case ENOMEM:
1196 log_error("Not enough memory to create "
1197 "notification semaphore");
1198 goto bad;
1199 case ENOSPC:
1200 log_error("Limit for the maximum number "
1201 "of semaphores reached. You can "
1202 "check and set the limits in "
1203 "/proc/sys/kernel/sem.");
1204 goto bad;
1205 default:
1206 log_error("Failed to create notification "
1207 "semaphore: %s", strerror(errno));
1208 goto bad;
1211 } while (!base_cookie);
1213 log_debug("Udev cookie 0x%" PRIx32 " (semid %d) created",
1214 gen_cookie, gen_semid);
1216 if (semctl(gen_semid, 0, SETVAL, 1) < 0) {
1217 log_error("semid %d: semctl failed: %s", gen_semid, strerror(errno));
1218 /* We have to destroy just created semaphore
1219 * so it won't stay in the system. */
1220 (void) _udev_notify_sem_destroy(gen_cookie, gen_semid);
1221 goto bad;
1224 log_debug("Udev cookie 0x%" PRIx32 " (semid %d) incremented",
1225 gen_cookie, gen_semid);
1227 if (close(fd))
1228 stack;
1230 *semid = gen_semid;
1231 *cookie = gen_cookie;
1233 return 1;
1235 bad:
1236 if (close(fd))
1237 stack;
1239 *cookie = 0;
1241 return 0;
1244 int dm_task_set_cookie(struct dm_task *dmt, uint32_t *cookie, uint16_t flags)
1246 int semid;
1248 if (dm_cookie_supported())
1249 dmt->event_nr = flags << DM_UDEV_FLAGS_SHIFT;
1251 if (!dm_udev_get_sync_support()) {
1252 *cookie = 0;
1253 return 1;
1256 if (*cookie) {
1257 if (!_get_cookie_sem(*cookie, &semid))
1258 goto_bad;
1259 } else if (!_udev_notify_sem_create(cookie, &semid))
1260 goto_bad;
1262 if (!_udev_notify_sem_inc(*cookie, semid)) {
1263 log_error("Could not set notification semaphore "
1264 "identified by cookie value %" PRIu32 " (0x%x)",
1265 *cookie, *cookie);
1266 goto bad;
1269 dmt->event_nr |= ~DM_UDEV_FLAGS_MASK & *cookie;
1270 dmt->cookie_set = 1;
1272 log_debug("Udev cookie 0x%" PRIx32 " (semid %d) assigned to dm_task "
1273 "with flags 0x%" PRIx16, *cookie, semid, flags);
1275 return 1;
1277 bad:
1278 dmt->event_nr = 0;
1279 return 0;
1282 int dm_udev_complete(uint32_t cookie)
1284 int semid;
1286 if (!cookie || !dm_udev_get_sync_support())
1287 return 1;
1289 if (!_get_cookie_sem(cookie, &semid))
1290 return_0;
1292 if (!_udev_notify_sem_dec(cookie, semid)) {
1293 log_error("Could not signal waiting process using notification "
1294 "semaphore identified by cookie value %" PRIu32 " (0x%x)",
1295 cookie, cookie);
1296 return 0;
1299 return 1;
1302 int dm_udev_wait(uint32_t cookie)
1304 int semid;
1305 struct sembuf sb = {0, 0, 0};
1307 if (!cookie || !dm_udev_get_sync_support())
1308 return 1;
1310 if (!_get_cookie_sem(cookie, &semid))
1311 return_0;
1313 if (!_udev_notify_sem_dec(cookie, semid)) {
1314 log_error("Failed to set a proper state for notification "
1315 "semaphore identified by cookie value %" PRIu32 " (0x%x) "
1316 "to initialize waiting for incoming notifications.",
1317 cookie, cookie);
1318 (void) _udev_notify_sem_destroy(cookie, semid);
1319 return 0;
1322 log_debug("Udev cookie 0x%" PRIx32 " (semid %d): Waiting for zero",
1323 cookie, semid);
1325 repeat_wait:
1326 if (semop(semid, &sb, 1) < 0) {
1327 if (errno == EINTR)
1328 goto repeat_wait;
1329 else if (errno == EIDRM)
1330 return 1;
1332 log_error("Could not set wait state for notification semaphore "
1333 "identified by cookie value %" PRIu32 " (0x%x): %s",
1334 cookie, cookie, strerror(errno));
1335 (void) _udev_notify_sem_destroy(cookie, semid);
1336 return 0;
1339 return _udev_notify_sem_destroy(cookie, semid);
1342 #endif /* UDEV_SYNC_SUPPORT */