summaryrefslogtreecommitdiff
path: root/daemons/dmeventd/plugins/lvm2/dmeventd_lvm.c
blob: 5d5a46b802ca1ef88fee4d7a35276c660f2fc3c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * Copyright (C) 2010 Red Hat, Inc. All rights reserved.
 *
 * This file is part of LVM2.
 *
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
 * of the GNU Lesser General Public License v.2.1.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "lib.h"
#include "log.h"

#include "lvm2cmd.h"
#include "dmeventd_lvm.h"

#include <pthread.h>
#include <syslog.h>

extern int dmeventd_debug;

/*
 * register_device() is called first and performs initialisation.
 * Only one device may be registered or unregistered at a time.
 */
static pthread_mutex_t _register_mutex = PTHREAD_MUTEX_INITIALIZER;

/*
 * Number of active registrations.
 */
static int _register_count = 0;
static struct dm_pool *_mem_pool = NULL;
static void *_lvm_handle = NULL;

/*
 * Currently only one event can be processed at a time.
 */
static pthread_mutex_t _event_mutex = PTHREAD_MUTEX_INITIALIZER;

/*
 * FIXME Do not pass things directly to syslog, rather use the existing logging
 * facilities to sort logging ... however that mechanism needs to be somehow
 * configurable and we don't have that option yet
 */
static void _temporary_log_fn(int level,
			      const char *file __attribute__((unused)),
			      int line __attribute__((unused)),
			      int dm_errno __attribute__((unused)),
			      const char *message)
{
	level &= ~(_LOG_STDERR | _LOG_ONCE);

	switch (level) {
	case _LOG_DEBUG:
		if (dmeventd_debug >= 3)
			syslog(LOG_DEBUG, "%s", message);
		break;
	case _LOG_INFO:
		if (dmeventd_debug >= 2)
			syslog(LOG_INFO, "%s", message);
		break;
	case _LOG_NOTICE:
		if (dmeventd_debug >= 1)
			syslog(LOG_NOTICE, "%s", message);
		break;
	case _LOG_WARN:
		syslog(LOG_WARNING, "%s", message);
		break;
	case _LOG_ERR:
		syslog(LOG_ERR, "%s", message);
		break;
	default:
		syslog(LOG_CRIT, "%s", message);
	}
}

void dmeventd_lvm2_lock(void)
{
	pthread_mutex_lock(&_event_mutex);
}

void dmeventd_lvm2_unlock(void)
{
	pthread_mutex_unlock(&_event_mutex);
}

int dmeventd_lvm2_init(void)
{
	int r = 0;

	pthread_mutex_lock(&_register_mutex);

	/*
	 * Need some space for allocations.  1024 should be more
	 * than enough for what we need (device mapper name splitting)
	 */
	if (!_mem_pool && !(_mem_pool = dm_pool_create("mirror_dso", 1024)))
		goto out;

	if (!_lvm_handle) {
		lvm2_log_fn(_temporary_log_fn);
		if (!(_lvm_handle = lvm2_init())) {
			dm_pool_destroy(_mem_pool);
			_mem_pool = NULL;
			goto out;
		}
		lvm2_disable_dmeventd_monitoring(_lvm_handle);
		/* FIXME Temporary: move to dmeventd core */
		lvm2_run(_lvm_handle, "_memlock_inc");
	}

	_register_count++;
	r = 1;

out:
	pthread_mutex_unlock(&_register_mutex);
	return r;
}

void dmeventd_lvm2_exit(void)
{
	pthread_mutex_lock(&_register_mutex);

	if (!--_register_count) {
		lvm2_run(_lvm_handle, "_memlock_dec");
		dm_pool_destroy(_mem_pool);
		_mem_pool = NULL;
		lvm2_exit(_lvm_handle);
		_lvm_handle = NULL;
	}

	pthread_mutex_unlock(&_register_mutex);
}

struct dm_pool *dmeventd_lvm2_pool(void)
{
	return _mem_pool;
}

int dmeventd_lvm2_run(const char *cmdline)
{
	return lvm2_run(_lvm_handle, cmdline);
}

int dmeventd_lvm2_command(struct dm_pool *mem, char *buffer, size_t size,
			  const char *cmd, const char *device)
{
	char *vg = NULL, *lv = NULL, *layer;
	int r;

	if (!dm_split_lvm_name(mem, device, &vg, &lv, &layer)) {
		syslog(LOG_ERR, "Unable to determine VG name from %s.\n",
		       device);
		return 0;
	}

	/* strip off the mirror component designations */
	layer = strstr(lv, "_mlog");
	if (layer)
		*layer = '\0';

	r = dm_snprintf(buffer, size, "%s %s/%s", cmd, vg, lv);

	dm_pool_free(mem, vg);

	if (r < 0) {
		syslog(LOG_ERR, "Unable to form LVM command. (too long).\n");
		return 0;
	}

	return 1;
}