1 /* $NetBSD: dmeventd_mirror.c,v 1.1.1.2 2009/12/02 00:27:12 haad Exp $ */
4 * Copyright (C) 2005 Red Hat, Inc. All rights reserved.
6 * This file is part of LVM2.
8 * This copyrighted material is made available to anyone wishing to use,
9 * modify, copy, or redistribute it subject to the terms and conditions
10 * of the GNU Lesser General Public License v.2.1.
12 * You should have received a copy of the GNU Lesser General Public License
13 * along with this program; if not, write to the Free Software Foundation,
14 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <libdevmapper.h>
21 #include <libdevmapper-event.h>
30 #include <syslog.h> /* FIXME Replace syslog with multilog */
31 /* FIXME Missing openlog? */
38 * register_device() is called first and performs initialisation.
39 * Only one device may be registered or unregistered at a time.
41 static pthread_mutex_t _register_mutex = PTHREAD_MUTEX_INITIALIZER;
44 * Number of active registrations.
46 static int _register_count = 0;
48 static struct dm_pool *_mem_pool = NULL;
49 static void *_lvm_handle = NULL;
52 * Currently only one event can be processed at a time.
54 static pthread_mutex_t _event_mutex = PTHREAD_MUTEX_INITIALIZER;
56 static int _get_mirror_event(char *params)
64 int log_argc, num_devs;
67 * dm core parms: 0 409600 mirror
68 * Mirror core parms: 2 253:4 253:5 400/400
69 * New-style failure params: 1 AA
70 * New-style log params: 3 cluster 253:3 A
75 /* number of devices */
76 if (!dm_split_words(params, 1, 0, &p))
79 if (!(num_devs = atoi(p)))
83 /* devices names + "400/400" + "1 AA" + 1 or 3 log parms + NULL */
84 args = dm_malloc((num_devs + 7) * sizeof(char *));
85 if (!args || dm_split_words(p, num_devs + 7, 0, args) < num_devs + 5)
88 dev_status_str = args[2 + num_devs];
89 log_argc = atoi(args[3 + num_devs]);
90 log_status_str = args[3 + num_devs + log_argc];
91 sync_str = args[num_devs];
93 /* Check for bad mirror devices */
94 for (i = 0; i < num_devs; i++)
95 if (dev_status_str[i] == 'D') {
96 syslog(LOG_ERR, "Mirror device, %s, has failed.\n", args[i]);
100 /* Check for bad disk log device */
101 if (log_argc > 1 && log_status_str[0] == 'D') {
102 syslog(LOG_ERR, "Log device, %s, has failed.\n",
103 args[2 + num_devs + log_argc]);
110 p = strstr(sync_str, "/");
113 if (strcmp(sync_str, p+1))
127 syslog(LOG_ERR, "Unable to parse mirror status string.");
131 static void _temporary_log_fn(int level,
132 const char *file __attribute((unused)),
133 int line __attribute((unused)),
134 int dm_errno __attribute((unused)),
137 if (!strncmp(format, "WARNING: ", 9) && (level < 5))
138 syslog(LOG_CRIT, "%s", format);
140 syslog(LOG_DEBUG, "%s", format);
143 static int _remove_failed_devices(const char *device)
146 #define CMD_SIZE 256 /* FIXME Use system restriction */
147 char cmd_str[CMD_SIZE];
148 char *vg = NULL, *lv = NULL, *layer = NULL;
150 if (strlen(device) > 200) /* FIXME Use real restriction */
151 return -ENAMETOOLONG; /* FIXME These return code distinctions are not used so remove them! */
153 if (!dm_split_lvm_name(_mem_pool, device, &vg, &lv, &layer)) {
154 syslog(LOG_ERR, "Unable to determine VG name from %s",
156 return -ENOMEM; /* FIXME Replace with generic error return - reason for failure has already got logged */
159 /* FIXME Is any sanity-checking required on %s? */
160 if (CMD_SIZE <= snprintf(cmd_str, CMD_SIZE, "lvconvert --config devices{ignore_suspended_devices=1} --repair --use-policies %s/%s", vg, lv)) {
161 /* this error should be caught above, but doesn't hurt to check again */
162 syslog(LOG_ERR, "Unable to form LVM command: Device name too long");
163 dm_pool_empty(_mem_pool); /* FIXME: not safe with multiple threads */
164 return -ENAMETOOLONG; /* FIXME Replace with generic error return - reason for failure has already got logged */
167 r = lvm2_run(_lvm_handle, cmd_str);
169 if (r == ECMD_PROCESSED) {
170 snprintf(cmd_str, CMD_SIZE, "vgreduce --removemissing %s", vg);
171 if (lvm2_run(_lvm_handle, cmd_str) != 1)
172 syslog(LOG_ERR, "Unable to remove failed PVs from VG %s", vg);
175 dm_pool_empty(_mem_pool); /* FIXME: not safe with multiple threads */
176 return (r == ECMD_PROCESSED) ? 0 : -1;
179 void process_event(struct dm_task *dmt,
180 enum dm_event_mask event __attribute((unused)),
181 void **unused __attribute((unused)))
184 uint64_t start, length;
185 char *target_type = NULL;
187 const char *device = dm_task_get_name(dmt);
189 if (pthread_mutex_trylock(&_event_mutex)) {
190 syslog(LOG_NOTICE, "Another thread is handling an event. Waiting...");
191 pthread_mutex_lock(&_event_mutex);
194 next = dm_get_next_target(dmt, next, &start, &length,
195 &target_type, ¶ms);
198 syslog(LOG_INFO, "%s mapping lost.\n", device);
202 if (strcmp(target_type, "mirror")) {
203 syslog(LOG_INFO, "%s has unmirrored portion.\n", device);
207 switch(_get_mirror_event(params)) {
209 /* FIXME: all we really know is that this
210 _part_ of the device is in sync
211 Also, this is not an error
213 syslog(LOG_NOTICE, "%s is now in-sync\n", device);
216 syslog(LOG_ERR, "Device failure in %s\n", device);
217 if (_remove_failed_devices(device))
218 /* FIXME Why are all the error return codes unused? Get rid of them? */
219 syslog(LOG_ERR, "Failed to remove faulty devices in %s\n",
221 /* Should check before warning user that device is now linear
223 syslog(LOG_NOTICE, "%s is now a linear device.\n",
230 /* FIXME Provide value then! */
231 syslog(LOG_INFO, "Unknown event received.\n");
235 pthread_mutex_unlock(&_event_mutex);
238 int register_device(const char *device,
239 const char *uuid __attribute((unused)),
240 int major __attribute((unused)),
241 int minor __attribute((unused)),
242 void **unused __attribute((unused)))
246 pthread_mutex_lock(&_register_mutex);
249 * Need some space for allocations. 1024 should be more
250 * than enough for what we need (device mapper name splitting)
252 if (!_mem_pool && !(_mem_pool = dm_pool_create("mirror_dso", 1024)))
256 lvm2_log_fn(_temporary_log_fn);
257 if (!(_lvm_handle = lvm2_init())) {
258 dm_pool_destroy(_mem_pool);
262 lvm2_log_level(_lvm_handle, LVM2_LOG_SUPPRESS);
263 /* FIXME Temporary: move to dmeventd core */
264 lvm2_run(_lvm_handle, "_memlock_inc");
267 syslog(LOG_INFO, "Monitoring mirror device %s for events\n", device);
273 pthread_mutex_unlock(&_register_mutex);
278 int unregister_device(const char *device,
279 const char *uuid __attribute((unused)),
280 int major __attribute((unused)),
281 int minor __attribute((unused)),
282 void **unused __attribute((unused)))
284 pthread_mutex_lock(&_register_mutex);
286 syslog(LOG_INFO, "No longer monitoring mirror device %s for events\n",
289 if (!--_register_count) {
290 dm_pool_destroy(_mem_pool);
292 lvm2_run(_lvm_handle, "_memlock_dec");
293 lvm2_exit(_lvm_handle);
297 pthread_mutex_unlock(&_register_mutex);