1 /*	$NetBSD: libdm-common.c,v 1.7 2010/12/29 10:39:27 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 #ifdef __NetBSD__
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 	if (mknod(rpath, S_IFCHR | mode, rdev) < 0) {
454 		umask(old_mask);
455 		log_error("Unable to make device node for '%s'", raw_devname);
456 		return 0;
457 	}
458 	umask(old_mask);
459 
460 	if (chown(rpath, uid, gid) < 0) {
461 		log_sys_error("Raw device chown", rpath);
462 		return 0;
463 	}
464 #endif
465 
466 	_build_dev_path(path, sizeof(path), dev_name);
467 
468 	if (stat(path, &info) >= 0) {
469 		if (!S_ISBLK(info.st_mode)) {
470 			log_error("A non-block device file at '%s' "
471 				  "is already present", path);
472 			return 0;
473 		}
474 
475 		/* If right inode already exists we don't touch uid etc. */
476 		if (info.st_rdev == dev)
477 			return 1;
478 
479 		if (unlink(path) < 0) {
480 			log_error("Unable to unlink device node for '%s'",
481 				  dev_name);
482 			return 0;
483 		}
484 	} else if (dm_udev_get_sync_support() && check_udev)
485 		log_warn("%s not set up by udev: Falling back to direct "
486 			 "node creation.", path);
487 
488 	old_mask = umask(0);
489 	if (mknod(path, S_IFBLK | mode, dev) < 0) {
490 		umask(old_mask);
491 		log_error("Unable to make device node for '%s'", dev_name);
492 		return 0;
493 	}
494 	umask(old_mask);
495 
496 	if (chown(path, uid, gid) < 0) {
497 		log_sys_error("chown", path);
498 		return 0;
499 	}
500 
501 	log_debug("Created %s", path);
502 
503 	if (!dm_set_selinux_context(path, S_IFBLK))
504 		return 0;
505 
506 	return 1;
507 }
508 
509 static int _rm_dev_node(const char *dev_name, int check_udev)
510 {
511 	char path[PATH_MAX];
512 	struct stat info;
513 
514 #ifdef __NetBSD__
515 	char rpath[PATH_MAX];
516 	char raw_devname[DM_NAME_LEN+1]; /* r + other device name */
517 
518 	snprintf(raw_devname,sizeof(raw_devname),"r%s",dev_name);
519 
520 	_build_dev_path(rpath, sizeof(rpath), raw_devname);
521 
522 	if (stat(rpath, &info) < 0)
523 		return 1;
524 
525 	if (unlink(rpath) < 0) {
526 		log_error("Unable to unlink device node for '%s'", raw_devname);
527 		return 0;
528 	}
529 
530 	log_debug("Removed %s", rpath);
531 #endif
532 
533 	_build_dev_path(path, sizeof(path), dev_name);
534 
535 	if (stat(path, &info) < 0)
536 		return 1;
537 	else if (dm_udev_get_sync_support() && check_udev)
538 		log_warn("Node %s was not removed by udev. "
539 			 "Falling back to direct node removal.", path);
540 
541 	if (unlink(path) < 0) {
542 		log_error("Unable to unlink device node for '%s'", dev_name);
543 		return 0;
544 	}
545 
546 	log_debug("Removed %s", path);
547 
548 	return 1;
549 }
550 
551 static int _rename_dev_node(const char *old_name, const char *new_name,
552 			    int check_udev)
553 {
554 	char oldpath[PATH_MAX];
555 	char newpath[PATH_MAX];
556 	struct stat info;
557 
558 #ifdef __NetBSD__
559 	char rpath[PATH_MAX];
560 	char nrpath[PATH_MAX];
561 	char raw_devname[DM_NAME_LEN+1]; /* r + other device name */
562 	char nraw_devname[DM_NAME_LEN+1]; /* r + other device name */
563 
564 	snprintf(nraw_devname,sizeof(raw_devname),"r%s",new_name);
565 	snprintf(raw_devname,sizeof(raw_devname),"r%s",old_name);
566 
567 	_build_dev_path(nrpath, sizeof(nrpath), nraw_devname);
568 	_build_dev_path(rpath, sizeof(rpath), raw_devname);
569 
570 	if (stat(nrpath, &info) == 0) {
571 		if (S_ISBLK(info.st_mode)) {
572 			log_error("A block device file at '%s' "
573 			    "is present where raw device should be.", newpath);
574 			return 0;
575 		}
576 
577 		if (unlink(nrpath) < 0) {
578 			log_error("Unable to unlink device node for '%s'",
579 			    nraw_devname);
580 			return 0;
581 		}
582 	}
583 
584 	if (rename(rpath, nrpath) < 0) {
585 		log_error("Unable to rename device node from '%s' to '%s'",
586 		    raw_devname, nraw_devname);
587 		return 0;
588 	}
589 
590 	log_debug("Renamed %s to %s", rpath, nrpath);
591 
592 #endif
593 
594 	_build_dev_path(oldpath, sizeof(oldpath), old_name);
595 	_build_dev_path(newpath, sizeof(newpath), new_name);
596 
597 	if (stat(newpath, &info) == 0) {
598 		if (!S_ISBLK(info.st_mode)) {
599 			log_error("A non-block device file at '%s' "
600 				  "is already present", newpath);
601 			return 0;
602 		}
603 		else if (dm_udev_get_sync_support() && check_udev) {
604 			if (stat(oldpath, &info) < 0 &&
605 				 errno == ENOENT)
606 				/* assume udev already deleted this */
607 				return 1;
608 			else {
609 				log_warn("The node %s should have been renamed to %s "
610 					 "by udev but old node is still present. "
611 					 "Falling back to direct old node removal.",
612 					 oldpath, newpath);
613 				return _rm_dev_node(old_name, 0);
614 			}
615 		}
616 
617 		if (unlink(newpath) < 0) {
618 			if (errno == EPERM) {
619 				/* devfs, entry has already been renamed */
620 				return 1;
621 			}
622 			log_error("Unable to unlink device node for '%s'",
623 				  new_name);
624 			return 0;
625 		}
626 	}
627 	else if (dm_udev_get_sync_support() && check_udev)
628 		log_warn("The node %s should have been renamed to %s "
629 			 "by udev but new node is not present. "
630 			 "Falling back to direct node rename.",
631 			 oldpath, newpath);
632 
633 	if (rename(oldpath, newpath) < 0) {
634 		log_error("Unable to rename device node from '%s' to '%s'",
635 			  old_name, new_name);
636 		return 0;
637 	}
638 
639 	log_debug("Renamed %s to %s", oldpath, newpath);
640 
641 	return 1;
642 }
643 
644 #ifdef linux
645 static int _open_dev_node(const char *dev_name)
646 {
647 	int fd = -1;
648 	char path[PATH_MAX];
649 
650 	_build_dev_path(path, sizeof(path), dev_name);
651 
652 	if ((fd = open(path, O_RDONLY, 0)) < 0)
653 		log_sys_error("open", path);
654 
655 	return fd;
656 }
657 
658 int get_dev_node_read_ahead(const char *dev_name, uint32_t *read_ahead)
659 {
660 	int r = 1;
661 	int fd;
662 	long read_ahead_long;
663 
664 	if (!*dev_name) {
665 		log_error("Empty device name passed to BLKRAGET");
666 		return 0;
667 	}
668 
669 	if ((fd = _open_dev_node(dev_name)) < 0)
670 		return_0;
671 
672 	if (ioctl(fd, BLKRAGET, &read_ahead_long)) {
673 		log_sys_error("BLKRAGET", dev_name);
674 		*read_ahead = 0;
675 		r = 0;
676 	}  else {
677 		*read_ahead = (uint32_t) read_ahead_long;
678 		log_debug("%s: read ahead is %" PRIu32, dev_name, *read_ahead);
679 	}
680 
681 	if (close(fd))
682 		stack;
683 
684 	return r;
685 }
686 
687 static int _set_read_ahead(const char *dev_name, uint32_t read_ahead)
688 {
689 	int r = 1;
690 	int fd;
691 	long read_ahead_long = (long) read_ahead;
692 
693 	if (!*dev_name) {
694 		log_error("Empty device name passed to BLKRAGET");
695 		return 0;
696 	}
697 
698 	if ((fd = _open_dev_node(dev_name)) < 0)
699 		return_0;
700 
701 	log_debug("%s: Setting read ahead to %" PRIu32, dev_name, read_ahead);
702 
703 	if (ioctl(fd, BLKRASET, read_ahead_long)) {
704 		log_sys_error("BLKRASET", dev_name);
705 		r = 0;
706 	}
707 
708 	if (close(fd))
709 		stack;
710 
711 	return r;
712 }
713 
714 static int _set_dev_node_read_ahead(const char *dev_name, uint32_t read_ahead,
715 				    uint32_t read_ahead_flags)
716 {
717 	uint32_t current_read_ahead;
718 
719 	if (read_ahead == DM_READ_AHEAD_AUTO)
720 		return 1;
721 
722 	if (read_ahead == DM_READ_AHEAD_NONE)
723 		read_ahead = 0;
724 
725 	if (read_ahead_flags & DM_READ_AHEAD_MINIMUM_FLAG) {
726 		if (!get_dev_node_read_ahead(dev_name, &current_read_ahead))
727 			return_0;
728 
729 		if (current_read_ahead > read_ahead) {
730 			log_debug("%s: retaining kernel read ahead of %" PRIu32
731 				  " (requested %" PRIu32 ")",
732 				  dev_name, current_read_ahead, read_ahead);
733 			return 1;
734 		}
735 	}
736 
737 	return _set_read_ahead(dev_name, read_ahead);
738 }
739 
740 #else
741 
742 int get_dev_node_read_ahead(const char *dev_name, uint32_t *read_ahead)
743 {
744 	*read_ahead = 0;
745 
746 	return 1;
747 }
748 
749 static int _set_dev_node_read_ahead(const char *dev_name, uint32_t read_ahead,
750 				    uint32_t read_ahead_flags)
751 {
752 	return 1;
753 }
754 #endif
755 
756 typedef enum {
757 	NODE_ADD,
758 	NODE_DEL,
759 	NODE_RENAME,
760 	NODE_READ_AHEAD
761 } node_op_t;
762 
763 static int _do_node_op(node_op_t type, const char *dev_name, uint32_t major,
764 		       uint32_t minor, uid_t uid, gid_t gid, mode_t mode,
765 		       const char *old_name, uint32_t read_ahead,
766 		       uint32_t read_ahead_flags, int check_udev)
767 {
768 	switch (type) {
769 	case NODE_ADD:
770 		return _add_dev_node(dev_name, major, minor, uid, gid,
771 				     mode, check_udev);
772 	case NODE_DEL:
773 		return _rm_dev_node(dev_name, check_udev);
774 	case NODE_RENAME:
775 		return _rename_dev_node(old_name, dev_name, check_udev);
776 	case NODE_READ_AHEAD:
777 		return _set_dev_node_read_ahead(dev_name, read_ahead,
778 						read_ahead_flags);
779 	}
780 
781 	return 1;
782 }
783 
784 static DM_LIST_INIT(_node_ops);
785 
786 struct node_op_parms {
787 	struct dm_list list;
788 	node_op_t type;
789 	char *dev_name;
790 	uint32_t major;
791 	uint32_t minor;
792 	uid_t uid;
793 	gid_t gid;
794 	mode_t mode;
795 	uint32_t read_ahead;
796 	uint32_t read_ahead_flags;
797 	char *old_name;
798 	int check_udev;
799 	char names[0];
800 };
801 
802 static void _store_str(char **pos, char **ptr, const char *str)
803 {
804 	strcpy(*pos, str);
805 	*ptr = *pos;
806 	*pos += strlen(*ptr) + 1;
807 }
808 
809 static int _stack_node_op(node_op_t type, const char *dev_name, uint32_t major,
810 			  uint32_t minor, uid_t uid, gid_t gid, mode_t mode,
811 			  const char *old_name, uint32_t read_ahead,
812 			  uint32_t read_ahead_flags, int check_udev)
813 {
814 	struct node_op_parms *nop;
815 	struct dm_list *noph, *nopht;
816 	size_t len = strlen(dev_name) + strlen(old_name) + 2;
817 	char *pos;
818 
819 	/*
820 	 * Ignore any outstanding operations on the node if deleting it
821 	 */
822 	if (type == NODE_DEL) {
823 		dm_list_iterate_safe(noph, nopht, &_node_ops) {
824 			nop = dm_list_item(noph, struct node_op_parms);
825 			if (!strcmp(dev_name, nop->dev_name)) {
826 				dm_list_del(&nop->list);
827 				dm_free(nop);
828 			}
829 		}
830 	}
831 
832 	if (!(nop = dm_malloc(sizeof(*nop) + len))) {
833 		log_error("Insufficient memory to stack mknod operation");
834 		return 0;
835 	}
836 
837 	pos = nop->names;
838 	nop->type = type;
839 	nop->major = major;
840 	nop->minor = minor;
841 	nop->uid = uid;
842 	nop->gid = gid;
843 	nop->mode = mode;
844 	nop->read_ahead = read_ahead;
845 	nop->read_ahead_flags = read_ahead_flags;
846 	nop->check_udev = check_udev;
847 
848 	_store_str(&pos, &nop->dev_name, dev_name);
849 	_store_str(&pos, &nop->old_name, old_name);
850 
851 	dm_list_add(&_node_ops, &nop->list);
852 
853 	return 1;
854 }
855 
856 static void _pop_node_ops(void)
857 {
858 	struct dm_list *noph, *nopht;
859 	struct node_op_parms *nop;
860 
861 	dm_list_iterate_safe(noph, nopht, &_node_ops) {
862 		nop = dm_list_item(noph, struct node_op_parms);
863 		_do_node_op(nop->type, nop->dev_name, nop->major, nop->minor,
864 			    nop->uid, nop->gid, nop->mode, nop->old_name,
865 			    nop->read_ahead, nop->read_ahead_flags,
866 			    nop->check_udev);
867 		dm_list_del(&nop->list);
868 		dm_free(nop);
869 	}
870 }
871 
872 int add_dev_node(const char *dev_name, uint32_t major, uint32_t minor,
873 		 uid_t uid, gid_t gid, mode_t mode, int check_udev)
874 {
875 	log_debug("%s: Stacking NODE_ADD (%" PRIu32 ",%" PRIu32 ") %u:%u 0%o",
876 		  dev_name, major, minor, uid, gid, mode);
877 
878 	return _stack_node_op(NODE_ADD, dev_name, major, minor, uid,
879 			      gid, mode, "", 0, 0, check_udev);
880 }
881 
882 int rename_dev_node(const char *old_name, const char *new_name, int check_udev)
883 {
884 	log_debug("%s: Stacking NODE_RENAME to %s", old_name, new_name);
885 
886 	return _stack_node_op(NODE_RENAME, new_name, 0, 0, 0,
887 			      0, 0, old_name, 0, 0, check_udev);
888 }
889 
890 int rm_dev_node(const char *dev_name, int check_udev)
891 {
892 	log_debug("%s: Stacking NODE_DEL (replaces other stacked ops)", dev_name);
893 
894 	return _stack_node_op(NODE_DEL, dev_name, 0, 0, 0,
895 			      0, 0, "", 0, 0, check_udev);
896 }
897 
898 int set_dev_node_read_ahead(const char *dev_name, uint32_t read_ahead,
899 			    uint32_t read_ahead_flags)
900 {
901 	if (read_ahead == DM_READ_AHEAD_AUTO)
902 		return 1;
903 
904 	log_debug("%s: Stacking NODE_READ_AHEAD %" PRIu32 " (flags=%" PRIu32
905 		  ")", dev_name, read_ahead, read_ahead_flags);
906 
907 	return _stack_node_op(NODE_READ_AHEAD, dev_name, 0, 0, 0, 0,
908                               0, "", read_ahead, read_ahead_flags, 0);
909 }
910 
911 void update_devs(void)
912 {
913 	_pop_node_ops();
914 }
915 
916 int dm_set_dev_dir(const char *dev_dir)
917 {
918 	size_t len;
919 	const char *slash;
920 	if (*dev_dir != '/') {
921 		log_debug("Invalid dev_dir value, %s: "
922 			  "not an absolute name.", dev_dir);
923 		return 0;
924 	}
925 
926 	len = strlen(dev_dir);
927 	slash = dev_dir[len-1] == '/' ? "" : "/";
928 
929 	if (snprintf(_dm_dir, sizeof _dm_dir, "%s%s%s", dev_dir, slash, DM_DIR)
930 	    >= sizeof _dm_dir) {
931 		log_debug("Invalid dev_dir value, %s: name too long.", dev_dir);
932 		return 0;
933 	}
934 
935 	return 1;
936 }
937 
938 const char *dm_dir(void)
939 {
940 	return _dm_dir;
941 }
942 
943 int dm_mknodes(const char *name)
944 {
945 	struct dm_task *dmt;
946 	int r = 0;
947 
948 	if (!(dmt = dm_task_create(DM_DEVICE_MKNODES)))
949 		return 0;
950 
951 	if (name && !dm_task_set_name(dmt, name))
952 		goto out;
953 
954 	if (!dm_task_no_open_count(dmt))
955 		goto out;
956 
957 	r = dm_task_run(dmt);
958 
959 out:
960 	dm_task_destroy(dmt);
961 	return r;
962 }
963 
964 int dm_driver_version(char *version, size_t size)
965 {
966 	struct dm_task *dmt;
967 	int r = 0;
968 
969 	if (!(dmt = dm_task_create(DM_DEVICE_VERSION)))
970 		return 0;
971 
972 	if (!dm_task_run(dmt))
973 		log_error("Failed to get driver version");
974 
975 	if (!dm_task_get_driver_version(dmt, version, size))
976 		goto out;
977 
978 	r = 1;
979 
980 out:
981 	dm_task_destroy(dmt);
982 	return r;
983 }
984 
985 #ifndef UDEV_SYNC_SUPPORT
986 void dm_udev_set_sync_support(int sync_with_udev)
987 {
988 }
989 
990 int dm_udev_get_sync_support(void)
991 {
992 	return 0;
993 }
994 
995 int dm_task_set_cookie(struct dm_task *dmt, uint32_t *cookie, uint16_t flags)
996 {
997 	if (dm_cookie_supported())
998 		dmt->event_nr = flags << DM_UDEV_FLAGS_SHIFT;
999 	*cookie = 0;
1000 
1001 	return 1;
1002 }
1003 
1004 int dm_udev_complete(uint32_t cookie)
1005 {
1006 	return 1;
1007 }
1008 
1009 int dm_udev_wait(uint32_t cookie)
1010 {
1011 	return 1;
1012 }
1013 
1014 #else		/* UDEV_SYNC_SUPPORT */
1015 
1016 
1017 static int _check_udev_is_running(void)
1018 {
1019 
1020 #  ifndef HAVE_UDEV_QUEUE_GET_UDEV_IS_ACTIVE
1021 
1022 	log_debug("Could not get udev state because libudev library "
1023 		  "was not found and it was not compiled in. "
1024 		  "Assuming udev is not running.");
1025 	return 0;
1026 
1027 #  else	/* HAVE_UDEV_QUEUE_GET_UDEV_IS_ACTIVE */
1028 
1029 	struct udev *udev;
1030 	struct udev_queue *udev_queue;
1031 	int r;
1032 
1033 	if (!(udev = udev_new()))
1034 		goto_bad;
1035 
1036 	if (!(udev_queue = udev_queue_new(udev))) {
1037 		udev_unref(udev);
1038 		goto_bad;
1039 	}
1040 
1041 	if (!(r = udev_queue_get_udev_is_active(udev_queue)))
1042 		log_debug("Udev is not running. "
1043 			  "Not using udev synchronisation code.");
1044 
1045 	udev_queue_unref(udev_queue);
1046 	udev_unref(udev);
1047 
1048 	return r;
1049 
1050 bad:
1051 	log_error("Could not get udev state. Assuming udev is not running.");
1052 	return 0;
1053 
1054 #  endif	/* HAVE_UDEV_QUEUE_GET_UDEV_IS_ACTIVE */
1055 
1056 }
1057 
1058 void dm_udev_set_sync_support(int sync_with_udev)
1059 {
1060 	if (_udev_running < 0)
1061 		_udev_running = _check_udev_is_running();
1062 
1063 	_sync_with_udev = sync_with_udev;
1064 }
1065 
1066 int dm_udev_get_sync_support(void)
1067 {
1068 	if (_udev_running < 0)
1069 		_udev_running = _check_udev_is_running();
1070 
1071 	return dm_cookie_supported() && _udev_running && _sync_with_udev;
1072 }
1073 
1074 static int _get_cookie_sem(uint32_t cookie, int *semid)
1075 {
1076 	if (cookie >> 16 != DM_COOKIE_MAGIC) {
1077 		log_error("Could not continue to access notification "
1078 			  "semaphore identified by cookie value %"
1079 			  PRIu32 " (0x%x). Incorrect cookie prefix.",
1080 			  cookie, cookie);
1081 		return 0;
1082 	}
1083 
1084 	if ((*semid = semget((key_t) cookie, 1, 0)) >= 0)
1085 		return 1;
1086 
1087 	switch (errno) {
1088 		case ENOENT:
1089 			log_error("Could not find notification "
1090 				  "semaphore identified by cookie "
1091 				  "value %" PRIu32 " (0x%x)",
1092 				  cookie, cookie);
1093 			break;
1094 		case EACCES:
1095 			log_error("No permission to access "
1096 				  "notificaton semaphore identified "
1097 				  "by cookie value %" PRIu32 " (0x%x)",
1098 				  cookie, cookie);
1099 			break;
1100 		default:
1101 			log_error("Failed to access notification "
1102 				   "semaphore identified by cookie "
1103 				   "value %" PRIu32 " (0x%x): %s",
1104 				  cookie, cookie, strerror(errno));
1105 			break;
1106 	}
1107 
1108 	return 0;
1109 }
1110 
1111 static int _udev_notify_sem_inc(uint32_t cookie, int semid)
1112 {
1113 	struct sembuf sb = {0, 1, 0};
1114 
1115 	if (semop(semid, &sb, 1) < 0) {
1116 		log_error("semid %d: semop failed for cookie 0x%" PRIx32 ": %s",
1117 			  semid, cookie, strerror(errno));
1118 		return 0;
1119 	}
1120 
1121 	log_debug("Udev cookie 0x%" PRIx32 " (semid %d) incremented",
1122 		  cookie, semid);
1123 
1124 	return 1;
1125 }
1126 
1127 static int _udev_notify_sem_dec(uint32_t cookie, int semid)
1128 {
1129 	struct sembuf sb = {0, -1, IPC_NOWAIT};
1130 
1131 	if (semop(semid, &sb, 1) < 0) {
1132 		switch (errno) {
1133 			case EAGAIN:
1134 				log_error("semid %d: semop failed for cookie "
1135 					  "0x%" PRIx32 ": "
1136 					  "incorrect semaphore state",
1137 					  semid, cookie);
1138 				break;
1139 			default:
1140 				log_error("semid %d: semop failed for cookie "
1141 					  "0x%" PRIx32 ": %s",
1142 					  semid, cookie, strerror(errno));
1143 				break;
1144 		}
1145 		return 0;
1146 	}
1147 
1148 	log_debug("Udev cookie 0x%" PRIx32 " (semid %d) decremented",
1149 		  cookie, semid);
1150 
1151 	return 1;
1152 }
1153 
1154 static int _udev_notify_sem_destroy(uint32_t cookie, int semid)
1155 {
1156 	if (semctl(semid, 0, IPC_RMID, 0) < 0) {
1157 		log_error("Could not cleanup notification semaphore "
1158 			  "identified by cookie value %" PRIu32 " (0x%x): %s",
1159 			  cookie, cookie, strerror(errno));
1160 		return 0;
1161 	}
1162 
1163 	log_debug("Udev cookie 0x%" PRIx32 " (semid %d) destroyed", cookie,
1164 		  semid);
1165 
1166 	return 1;
1167 }
1168 
1169 static int _udev_notify_sem_create(uint32_t *cookie, int *semid)
1170 {
1171 	int fd;
1172 	int gen_semid;
1173 	uint16_t base_cookie;
1174 	uint32_t gen_cookie;
1175 
1176 	if ((fd = open("/dev/urandom", O_RDONLY)) < 0) {
1177 		log_error("Failed to open /dev/urandom "
1178 			  "to create random cookie value");
1179 		*cookie = 0;
1180 		return 0;
1181 	}
1182 
1183 	/* Generate random cookie value. Be sure it is unique and non-zero. */
1184 	do {
1185 		/* FIXME Handle non-error returns from read(). Move _io() into libdm? */
1186 		if (read(fd, &base_cookie, sizeof(base_cookie)) != sizeof(base_cookie)) {
1187 			log_error("Failed to initialize notification cookie");
1188 			goto bad;
1189 		}
1190 
1191 		gen_cookie = DM_COOKIE_MAGIC << 16 | base_cookie;
1192 
1193 		if (base_cookie && (gen_semid = semget((key_t) gen_cookie,
1194 				    1, 0600 | IPC_CREAT | IPC_EXCL)) < 0) {
1195 			switch (errno) {
1196 				case EEXIST:
1197 					/* if the semaphore key exists, we
1198 					 * simply generate another random one */
1199 					base_cookie = 0;
1200 					break;
1201 				case ENOMEM:
1202 					log_error("Not enough memory to create "
1203 						  "notification semaphore");
1204 					goto bad;
1205 				case ENOSPC:
1206 					log_error("Limit for the maximum number "
1207 						  "of semaphores reached. You can "
1208 						  "check and set the limits in "
1209 						  "/proc/sys/kernel/sem.");
1210 					goto bad;
1211 				default:
1212 					log_error("Failed to create notification "
1213 						  "semaphore: %s", strerror(errno));
1214 					goto bad;
1215 			}
1216 		}
1217 	} while (!base_cookie);
1218 
1219 	log_debug("Udev cookie 0x%" PRIx32 " (semid %d) created",
1220 		  gen_cookie, gen_semid);
1221 
1222 	if (semctl(gen_semid, 0, SETVAL, 1) < 0) {
1223 		log_error("semid %d: semctl failed: %s", gen_semid, strerror(errno));
1224 		/* We have to destroy just created semaphore
1225 		 * so it won't stay in the system. */
1226 		(void) _udev_notify_sem_destroy(gen_cookie, gen_semid);
1227 		goto bad;
1228 	}
1229 
1230 	log_debug("Udev cookie 0x%" PRIx32 " (semid %d) incremented",
1231 		  gen_cookie, gen_semid);
1232 
1233 	if (close(fd))
1234 		stack;
1235 
1236 	*semid = gen_semid;
1237 	*cookie = gen_cookie;
1238 
1239 	return 1;
1240 
1241 bad:
1242 	if (close(fd))
1243 		stack;
1244 
1245 	*cookie = 0;
1246 
1247 	return 0;
1248 }
1249 
1250 int dm_task_set_cookie(struct dm_task *dmt, uint32_t *cookie, uint16_t flags)
1251 {
1252 	int semid;
1253 
1254 	if (dm_cookie_supported())
1255 		dmt->event_nr = flags << DM_UDEV_FLAGS_SHIFT;
1256 
1257 	if (!dm_udev_get_sync_support()) {
1258 		*cookie = 0;
1259 		return 1;
1260 	}
1261 
1262 	if (*cookie) {
1263 		if (!_get_cookie_sem(*cookie, &semid))
1264 			goto_bad;
1265 	} else if (!_udev_notify_sem_create(cookie, &semid))
1266 		goto_bad;
1267 
1268 	if (!_udev_notify_sem_inc(*cookie, semid)) {
1269 		log_error("Could not set notification semaphore "
1270 			  "identified by cookie value %" PRIu32 " (0x%x)",
1271 			  *cookie, *cookie);
1272 		goto bad;
1273 	}
1274 
1275 	dmt->event_nr |= ~DM_UDEV_FLAGS_MASK & *cookie;
1276 	dmt->cookie_set = 1;
1277 
1278 	log_debug("Udev cookie 0x%" PRIx32 " (semid %d) assigned to dm_task "
1279 		  "with flags 0x%" PRIx16, *cookie, semid, flags);
1280 
1281 	return 1;
1282 
1283 bad:
1284 	dmt->event_nr = 0;
1285 	return 0;
1286 }
1287 
1288 int dm_udev_complete(uint32_t cookie)
1289 {
1290 	int semid;
1291 
1292 	if (!cookie || !dm_udev_get_sync_support())
1293 		return 1;
1294 
1295 	if (!_get_cookie_sem(cookie, &semid))
1296 		return_0;
1297 
1298 	if (!_udev_notify_sem_dec(cookie, semid)) {
1299 		log_error("Could not signal waiting process using notification "
1300 			  "semaphore identified by cookie value %" PRIu32 " (0x%x)",
1301 			  cookie, cookie);
1302 		return 0;
1303 	}
1304 
1305 	return 1;
1306 }
1307 
1308 int dm_udev_wait(uint32_t cookie)
1309 {
1310 	int semid;
1311 	struct sembuf sb = {0, 0, 0};
1312 
1313 	if (!cookie || !dm_udev_get_sync_support())
1314 		return 1;
1315 
1316 	if (!_get_cookie_sem(cookie, &semid))
1317 		return_0;
1318 
1319 	if (!_udev_notify_sem_dec(cookie, semid)) {
1320 		log_error("Failed to set a proper state for notification "
1321 			  "semaphore identified by cookie value %" PRIu32 " (0x%x) "
1322 			  "to initialize waiting for incoming notifications.",
1323 			  cookie, cookie);
1324 		(void) _udev_notify_sem_destroy(cookie, semid);
1325 		return 0;
1326 	}
1327 
1328 	log_debug("Udev cookie 0x%" PRIx32 " (semid %d): Waiting for zero",
1329 		  cookie, semid);
1330 
1331 repeat_wait:
1332 	if (semop(semid, &sb, 1) < 0) {
1333 		if (errno == EINTR)
1334 			goto repeat_wait;
1335 		else if (errno == EIDRM)
1336 			return 1;
1337 
1338 		log_error("Could not set wait state for notification semaphore "
1339 			  "identified by cookie value %" PRIu32 " (0x%x): %s",
1340 			  cookie, cookie, strerror(errno));
1341 		(void) _udev_notify_sem_destroy(cookie, semid);
1342 		return 0;
1343 	}
1344 
1345 	return _udev_notify_sem_destroy(cookie, semid);
1346 }
1347 
1348 #endif		/* UDEV_SYNC_SUPPORT */
1349