2 * Copyright (C) 2006-2009 Red Hat, Inc.
4 * This file is released under the LGPL.
8 #include <linux/dm-dirty-log.h>
9 #include <linux/device-mapper.h>
10 #include <linux/dm-log-userspace.h>
12 #include "dm-log-userspace-transfer.h"
17 struct list_head list;
23 region_t region_count;
24 char uuid[DM_UUID_LEN];
30 * in_sync_hint gets set when doing is_remote_recovering. It
31 * represents the first region that needs recovery. IOW, the
32 * first zero bit of sync_bits. This can be useful for to limit
33 * traffic for calls like is_remote_recovering and get_resync_work,
34 * but be take care in its use for anything else.
36 uint64_t in_sync_hint;
38 spinlock_t flush_lock;
39 struct list_head flush_list; /* only for clear and mark requests */
42 static mempool_t *flush_entry_pool;
44 static void *flush_entry_alloc(gfp_t gfp_mask, void *pool_data)
46 return kmalloc(sizeof(struct flush_entry), gfp_mask);
49 static void flush_entry_free(void *element, void *pool_data)
54 static int userspace_do_request(struct log_c *lc, const char *uuid,
55 int request_type, char *data, size_t data_size,
56 char *rdata, size_t *rdata_size)
61 * If the server isn't there, -ESRCH is returned,
62 * and we must keep trying until the server is
66 r = dm_consult_userspace(uuid, request_type, data,
67 data_size, rdata, rdata_size);
72 DMERR(" Userspace log server not found.");
74 set_current_state(TASK_INTERRUPTIBLE);
75 schedule_timeout(2*HZ);
76 DMWARN("Attempting to contact userspace log server...");
77 r = dm_consult_userspace(uuid, DM_ULOG_CTR, lc->usr_argv_str,
78 strlen(lc->usr_argv_str) + 1,
83 DMINFO("Reconnected to userspace log server... DM_ULOG_CTR complete");
84 r = dm_consult_userspace(uuid, DM_ULOG_RESUME, NULL,
89 DMERR("Error trying to resume userspace log: %d", r);
94 static int build_constructor_string(struct dm_target *ti,
95 unsigned argc, char **argv,
103 for (i = 0, str_size = 0; i < argc; i++)
104 str_size += strlen(argv[i]) + 1; /* +1 for space between args */
106 str_size += 20; /* Max number of chars in a printed u64 number */
108 str = kzalloc(str_size, GFP_KERNEL);
110 DMWARN("Unable to allocate memory for constructor string");
114 str_size = sprintf(str, "%llu", (unsigned long long)ti->len);
115 for (i = 0; i < argc; i++)
116 str_size += sprintf(str + str_size, " %s", argv[i]);
126 * <UUID> <other args>
127 * Where 'other args' is the userspace implementation specific log
128 * arguments. An example might be:
129 * <UUID> clustered_disk <arg count> <log dev> <region_size> [[no]sync]
131 * So, this module will strip off the <UUID> for identification purposes
132 * when communicating with userspace about a log; but will pass on everything
135 static int userspace_ctr(struct dm_dirty_log *log, struct dm_target *ti,
136 unsigned argc, char **argv)
140 char *ctr_str = NULL;
141 struct log_c *lc = NULL;
143 size_t rdata_size = sizeof(rdata);
146 DMWARN("Too few arguments to userspace dirty log");
150 lc = kmalloc(sizeof(*lc), GFP_KERNEL);
152 DMWARN("Unable to allocate userspace log context.");
158 if (strlen(argv[0]) > (DM_UUID_LEN - 1)) {
159 DMWARN("UUID argument too long.");
164 strncpy(lc->uuid, argv[0], DM_UUID_LEN);
165 spin_lock_init(&lc->flush_lock);
166 INIT_LIST_HEAD(&lc->flush_list);
168 str_size = build_constructor_string(ti, argc - 1, argv + 1, &ctr_str);
174 /* Send table string */
175 r = dm_consult_userspace(lc->uuid, DM_ULOG_CTR,
176 ctr_str, str_size, NULL, NULL);
179 DMERR("Userspace log server not found");
183 /* Since the region size does not change, get it now */
184 rdata_size = sizeof(rdata);
185 r = dm_consult_userspace(lc->uuid, DM_ULOG_GET_REGION_SIZE,
186 NULL, 0, (char *)&rdata, &rdata_size);
189 DMERR("Failed to get region size of dirty log");
193 lc->region_size = (uint32_t)rdata;
194 lc->region_count = dm_sector_div_up(ti->len, lc->region_size);
201 lc->usr_argv_str = ctr_str;
209 static void userspace_dtr(struct dm_dirty_log *log)
212 struct log_c *lc = log->context;
214 r = dm_consult_userspace(lc->uuid, DM_ULOG_DTR,
218 kfree(lc->usr_argv_str);
224 static int userspace_presuspend(struct dm_dirty_log *log)
227 struct log_c *lc = log->context;
229 r = dm_consult_userspace(lc->uuid, DM_ULOG_PRESUSPEND,
236 static int userspace_postsuspend(struct dm_dirty_log *log)
239 struct log_c *lc = log->context;
241 r = dm_consult_userspace(lc->uuid, DM_ULOG_POSTSUSPEND,
248 static int userspace_resume(struct dm_dirty_log *log)
251 struct log_c *lc = log->context;
253 lc->in_sync_hint = 0;
254 r = dm_consult_userspace(lc->uuid, DM_ULOG_RESUME,
261 static uint32_t userspace_get_region_size(struct dm_dirty_log *log)
263 struct log_c *lc = log->context;
265 return lc->region_size;
271 * Check whether a region is clean. If there is any sort of
272 * failure when consulting the server, we return not clean.
274 * Returns: 1 if clean, 0 otherwise
276 static int userspace_is_clean(struct dm_dirty_log *log, region_t region)
279 uint64_t region64 = (uint64_t)region;
282 struct log_c *lc = log->context;
284 rdata_size = sizeof(is_clean);
285 r = userspace_do_request(lc, lc->uuid, DM_ULOG_IS_CLEAN,
286 (char *)®ion64, sizeof(region64),
287 (char *)&is_clean, &rdata_size);
289 return (r) ? 0 : (int)is_clean;
295 * Check if the region is in-sync. If there is any sort
296 * of failure when consulting the server, we assume that
297 * the region is not in sync.
299 * If 'can_block' is set, return immediately
301 * Returns: 1 if in-sync, 0 if not-in-sync, -EWOULDBLOCK
303 static int userspace_in_sync(struct dm_dirty_log *log, region_t region,
307 uint64_t region64 = region;
310 struct log_c *lc = log->context;
313 * We can never respond directly - even if in_sync_hint is
314 * set. This is because another machine could see a device
315 * failure and mark the region out-of-sync. If we don't go
316 * to userspace to ask, we might think the region is in-sync
317 * and allow a read to pick up data that is stale. (This is
318 * very unlikely if a device actually fails; but it is very
319 * likely if a connection to one device from one machine fails.)
321 * There still might be a problem if the mirror caches the region
322 * state as in-sync... but then this call would not be made. So,
323 * that is a mirror problem.
328 rdata_size = sizeof(in_sync);
329 r = userspace_do_request(lc, lc->uuid, DM_ULOG_IN_SYNC,
330 (char *)®ion64, sizeof(region64),
331 (char *)&in_sync, &rdata_size);
332 return (r) ? 0 : (int)in_sync;
338 * This function is ok to block.
339 * The flush happens in two stages. First, it sends all
340 * clear/mark requests that are on the list. Then it
341 * tells the server to commit them. This gives the
342 * server a chance to optimise the commit, instead of
343 * doing it for every request.
345 * Additionally, we could implement another thread that
346 * sends the requests up to the server - reducing the
347 * load on flush. Then the flush would have less in
348 * the list and be responsible for the finishing commit.
350 * Returns: 0 on success, < 0 on failure
352 static int userspace_flush(struct dm_dirty_log *log)
356 struct log_c *lc = log->context;
357 LIST_HEAD(flush_list);
358 struct flush_entry *fe, *tmp_fe;
360 spin_lock_irqsave(&lc->flush_lock, flags);
361 list_splice_init(&lc->flush_list, &flush_list);
362 spin_unlock_irqrestore(&lc->flush_lock, flags);
364 if (list_empty(&flush_list))
368 * FIXME: Count up requests, group request types,
369 * allocate memory to stick all requests in and
370 * send to server in one go. Failing the allocation,
374 list_for_each_entry(fe, &flush_list, list) {
375 r = userspace_do_request(lc, lc->uuid, fe->type,
383 r = userspace_do_request(lc, lc->uuid, DM_ULOG_FLUSH,
384 NULL, 0, NULL, NULL);
388 * We can safely remove these entries, even if failure.
389 * Calling code will receive an error and will know that
390 * the log facility has failed.
392 list_for_each_entry_safe(fe, tmp_fe, &flush_list, list) {
394 mempool_free(fe, flush_entry_pool);
398 dm_table_event(lc->ti->table);
404 * userspace_mark_region
406 * This function should avoid blocking unless absolutely required.
407 * (Memory allocation is valid for blocking.)
409 static void userspace_mark_region(struct dm_dirty_log *log, region_t region)
412 struct log_c *lc = log->context;
413 struct flush_entry *fe;
415 /* Wait for an allocation, but _never_ fail */
416 fe = mempool_alloc(flush_entry_pool, GFP_NOIO);
419 spin_lock_irqsave(&lc->flush_lock, flags);
420 fe->type = DM_ULOG_MARK_REGION;
422 list_add(&fe->list, &lc->flush_list);
423 spin_unlock_irqrestore(&lc->flush_lock, flags);
429 * userspace_clear_region
431 * This function must not block.
432 * So, the alloc can't block. In the worst case, it is ok to
433 * fail. It would simply mean we can't clear the region.
434 * Does nothing to current sync context, but does mean
435 * the region will be re-sync'ed on a reload of the mirror
436 * even though it is in-sync.
438 static void userspace_clear_region(struct dm_dirty_log *log, region_t region)
441 struct log_c *lc = log->context;
442 struct flush_entry *fe;
445 * If we fail to allocate, we skip the clearing of
446 * the region. This doesn't hurt us in any way, except
447 * to cause the region to be resync'ed when the
448 * device is activated next time.
450 fe = mempool_alloc(flush_entry_pool, GFP_ATOMIC);
452 DMERR("Failed to allocate memory to clear region.");
456 spin_lock_irqsave(&lc->flush_lock, flags);
457 fe->type = DM_ULOG_CLEAR_REGION;
459 list_add(&fe->list, &lc->flush_list);
460 spin_unlock_irqrestore(&lc->flush_lock, flags);
466 * userspace_get_resync_work
468 * Get a region that needs recovery. It is valid to return
469 * an error for this function.
471 * Returns: 1 if region filled, 0 if no work, <0 on error
473 static int userspace_get_resync_work(struct dm_dirty_log *log, region_t *region)
477 struct log_c *lc = log->context;
479 int64_t i; /* 64-bit for mix arch compatibility */
483 if (lc->in_sync_hint >= lc->region_count)
486 rdata_size = sizeof(pkg);
487 r = userspace_do_request(lc, lc->uuid, DM_ULOG_GET_RESYNC_WORK,
489 (char *)&pkg, &rdata_size);
492 return (r) ? r : (int)pkg.i;
496 * userspace_set_region_sync
498 * Set the sync status of a given region. This function
501 static void userspace_set_region_sync(struct dm_dirty_log *log,
502 region_t region, int in_sync)
505 struct log_c *lc = log->context;
512 pkg.i = (int64_t)in_sync;
514 r = userspace_do_request(lc, lc->uuid, DM_ULOG_SET_REGION_SYNC,
515 (char *)&pkg, sizeof(pkg),
519 * It would be nice to be able to report failures.
520 * However, it is easy emough to detect and resolve.
526 * userspace_get_sync_count
528 * If there is any sort of failure when consulting the server,
529 * we assume that the sync count is zero.
531 * Returns: sync count on success, 0 on failure
533 static region_t userspace_get_sync_count(struct dm_dirty_log *log)
538 struct log_c *lc = log->context;
540 rdata_size = sizeof(sync_count);
541 r = userspace_do_request(lc, lc->uuid, DM_ULOG_GET_SYNC_COUNT,
543 (char *)&sync_count, &rdata_size);
548 if (sync_count >= lc->region_count)
549 lc->in_sync_hint = lc->region_count;
551 return (region_t)sync_count;
557 * Returns: amount of space consumed
559 static int userspace_status(struct dm_dirty_log *log, status_type_t status_type,
560 char *result, unsigned maxlen)
564 size_t sz = (size_t)maxlen;
565 struct log_c *lc = log->context;
567 switch (status_type) {
568 case STATUSTYPE_INFO:
569 r = userspace_do_request(lc, lc->uuid, DM_ULOG_STATUS_INFO,
575 DMEMIT("%s 1 COM_FAILURE", log->type->name);
578 case STATUSTYPE_TABLE:
580 table_args = strstr(lc->usr_argv_str, " ");
581 BUG_ON(!table_args); /* There will always be a ' ' */
584 DMEMIT("%s %u %s %s ", log->type->name, lc->usr_argc,
585 lc->uuid, table_args);
588 return (r) ? 0 : (int)sz;
592 * userspace_is_remote_recovering
594 * Returns: 1 if region recovering, 0 otherwise
596 static int userspace_is_remote_recovering(struct dm_dirty_log *log,
600 uint64_t region64 = region;
601 struct log_c *lc = log->context;
602 static unsigned long long limit;
604 int64_t is_recovering;
605 uint64_t in_sync_hint;
607 size_t rdata_size = sizeof(pkg);
610 * Once the mirror has been reported to be in-sync,
611 * it will never again ask for recovery work. So,
612 * we can safely say there is not a remote machine
613 * recovering if the device is in-sync. (in_sync_hint
614 * must be reset at resume time.)
616 if (region < lc->in_sync_hint)
618 else if (jiffies < limit)
621 limit = jiffies + (HZ / 4);
622 r = userspace_do_request(lc, lc->uuid, DM_ULOG_IS_REMOTE_RECOVERING,
623 (char *)®ion64, sizeof(region64),
624 (char *)&pkg, &rdata_size);
628 lc->in_sync_hint = pkg.in_sync_hint;
630 return (int)pkg.is_recovering;
633 static struct dm_dirty_log_type _userspace_type = {
635 .module = THIS_MODULE,
636 .ctr = userspace_ctr,
637 .dtr = userspace_dtr,
638 .presuspend = userspace_presuspend,
639 .postsuspend = userspace_postsuspend,
640 .resume = userspace_resume,
641 .get_region_size = userspace_get_region_size,
642 .is_clean = userspace_is_clean,
643 .in_sync = userspace_in_sync,
644 .flush = userspace_flush,
645 .mark_region = userspace_mark_region,
646 .clear_region = userspace_clear_region,
647 .get_resync_work = userspace_get_resync_work,
648 .set_region_sync = userspace_set_region_sync,
649 .get_sync_count = userspace_get_sync_count,
650 .status = userspace_status,
651 .is_remote_recovering = userspace_is_remote_recovering,
654 static int __init userspace_dirty_log_init(void)
658 flush_entry_pool = mempool_create(100, flush_entry_alloc,
659 flush_entry_free, NULL);
661 if (!flush_entry_pool) {
662 DMWARN("Unable to create flush_entry_pool: No memory.");
666 r = dm_ulog_tfr_init();
668 DMWARN("Unable to initialize userspace log communications");
669 mempool_destroy(flush_entry_pool);
673 r = dm_dirty_log_type_register(&_userspace_type);
675 DMWARN("Couldn't register userspace dirty log type");
677 mempool_destroy(flush_entry_pool);
681 DMINFO("version 1.0.0 loaded");
685 static void __exit userspace_dirty_log_exit(void)
687 dm_dirty_log_type_unregister(&_userspace_type);
689 mempool_destroy(flush_entry_pool);
691 DMINFO("version 1.0.0 unloaded");
695 module_init(userspace_dirty_log_init);
696 module_exit(userspace_dirty_log_exit);
698 MODULE_DESCRIPTION(DM_NAME " userspace dirty log link");
699 MODULE_AUTHOR("Jonathan Brassow <dm-devel@redhat.com>");
700 MODULE_LICENSE("GPL");