2 * Copyright (C) 2006-2009 Red Hat, Inc.
4 * This file is released under the LGPL.
8 #include <linux/slab.h>
9 #include <linux/dm-dirty-log.h>
10 #include <linux/device-mapper.h>
11 #include <linux/dm-log-userspace.h>
13 #include "dm-log-userspace-transfer.h"
15 #define DM_LOG_USERSPACE_VSN "1.1.0"
20 struct list_head list;
24 * This limit on the number of mark and clear request is, to a degree,
25 * arbitrary. However, there is some basis for the choice in the limits
26 * imposed on the size of data payload by dm-log-userspace-transfer.c:
27 * dm_consult_userspace().
29 #define MAX_FLUSH_GROUP_COUNT 32
33 struct dm_dev *log_dev;
35 region_t region_count;
37 char uuid[DM_UUID_LEN];
43 * in_sync_hint gets set when doing is_remote_recovering. It
44 * represents the first region that needs recovery. IOW, the
45 * first zero bit of sync_bits. This can be useful for to limit
46 * traffic for calls like is_remote_recovering and get_resync_work,
47 * but be take care in its use for anything else.
49 uint64_t in_sync_hint;
52 * Mark and clear requests are held until a flush is issued
53 * so that we can group, and thereby limit, the amount of
54 * network traffic between kernel and userspace. The 'flush_lock'
55 * is used to protect these lists.
57 spinlock_t flush_lock;
58 struct list_head mark_list;
59 struct list_head clear_list;
62 static mempool_t *flush_entry_pool;
64 static void *flush_entry_alloc(gfp_t gfp_mask, void *pool_data)
66 return kmalloc(sizeof(struct flush_entry), gfp_mask);
69 static void flush_entry_free(void *element, void *pool_data)
74 static int userspace_do_request(struct log_c *lc, const char *uuid,
75 int request_type, char *data, size_t data_size,
76 char *rdata, size_t *rdata_size)
81 * If the server isn't there, -ESRCH is returned,
82 * and we must keep trying until the server is
86 r = dm_consult_userspace(uuid, lc->luid, request_type, data,
87 data_size, rdata, rdata_size);
92 DMERR(" Userspace log server not found.");
94 set_current_state(TASK_INTERRUPTIBLE);
95 schedule_timeout(2*HZ);
96 DMWARN("Attempting to contact userspace log server...");
97 r = dm_consult_userspace(uuid, lc->luid, DM_ULOG_CTR,
99 strlen(lc->usr_argv_str) + 1,
104 DMINFO("Reconnected to userspace log server... DM_ULOG_CTR complete");
105 r = dm_consult_userspace(uuid, lc->luid, DM_ULOG_RESUME, NULL,
110 DMERR("Error trying to resume userspace log: %d", r);
115 static int build_constructor_string(struct dm_target *ti,
116 unsigned argc, char **argv,
124 for (i = 0, str_size = 0; i < argc; i++)
125 str_size += strlen(argv[i]) + 1; /* +1 for space between args */
127 str_size += 20; /* Max number of chars in a printed u64 number */
129 str = kzalloc(str_size, GFP_KERNEL);
131 DMWARN("Unable to allocate memory for constructor string");
135 str_size = sprintf(str, "%llu", (unsigned long long)ti->len);
136 for (i = 0; i < argc; i++)
137 str_size += sprintf(str + str_size, " %s", argv[i]);
147 * <UUID> <other args>
148 * Where 'other args' is the userspace implementation specific log
149 * arguments. An example might be:
150 * <UUID> clustered-disk <arg count> <log dev> <region_size> [[no]sync]
152 * So, this module will strip off the <UUID> for identification purposes
153 * when communicating with userspace about a log; but will pass on everything
156 static int userspace_ctr(struct dm_dirty_log *log, struct dm_target *ti,
157 unsigned argc, char **argv)
161 char *ctr_str = NULL;
162 struct log_c *lc = NULL;
164 size_t rdata_size = sizeof(rdata);
165 char *devices_rdata = NULL;
166 size_t devices_rdata_size = DM_NAME_LEN;
169 DMWARN("Too few arguments to userspace dirty log");
173 lc = kzalloc(sizeof(*lc), GFP_KERNEL);
175 DMWARN("Unable to allocate userspace log context.");
179 /* The ptr value is sufficient for local unique id */
180 lc->luid = (unsigned long)lc;
184 if (strlen(argv[0]) > (DM_UUID_LEN - 1)) {
185 DMWARN("UUID argument too long.");
190 strncpy(lc->uuid, argv[0], DM_UUID_LEN);
191 spin_lock_init(&lc->flush_lock);
192 INIT_LIST_HEAD(&lc->mark_list);
193 INIT_LIST_HEAD(&lc->clear_list);
195 str_size = build_constructor_string(ti, argc - 1, argv + 1, &ctr_str);
201 devices_rdata = kzalloc(devices_rdata_size, GFP_KERNEL);
202 if (!devices_rdata) {
203 DMERR("Failed to allocate memory for device information");
209 * Send table string and get back any opened device.
211 r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_CTR,
213 devices_rdata, &devices_rdata_size);
217 DMERR("Userspace log server not found");
219 DMERR("Userspace log server failed to create log");
223 /* Since the region size does not change, get it now */
224 rdata_size = sizeof(rdata);
225 r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_GET_REGION_SIZE,
226 NULL, 0, (char *)&rdata, &rdata_size);
229 DMERR("Failed to get region size of dirty log");
233 lc->region_size = (uint32_t)rdata;
234 lc->region_count = dm_sector_div_up(ti->len, lc->region_size);
236 if (devices_rdata_size) {
237 if (devices_rdata[devices_rdata_size - 1] != '\0') {
238 DMERR("DM_ULOG_CTR device return string not properly terminated");
242 r = dm_get_device(ti, devices_rdata,
243 dm_table_get_mode(ti->table), &lc->log_dev);
245 DMERR("Failed to register %s with device-mapper",
249 kfree(devices_rdata);
254 lc->usr_argv_str = ctr_str;
262 static void userspace_dtr(struct dm_dirty_log *log)
264 struct log_c *lc = log->context;
266 (void) dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_DTR,
271 dm_put_device(lc->ti, lc->log_dev);
273 kfree(lc->usr_argv_str);
279 static int userspace_presuspend(struct dm_dirty_log *log)
282 struct log_c *lc = log->context;
284 r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_PRESUSPEND,
291 static int userspace_postsuspend(struct dm_dirty_log *log)
294 struct log_c *lc = log->context;
296 r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_POSTSUSPEND,
303 static int userspace_resume(struct dm_dirty_log *log)
306 struct log_c *lc = log->context;
308 lc->in_sync_hint = 0;
309 r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_RESUME,
316 static uint32_t userspace_get_region_size(struct dm_dirty_log *log)
318 struct log_c *lc = log->context;
320 return lc->region_size;
326 * Check whether a region is clean. If there is any sort of
327 * failure when consulting the server, we return not clean.
329 * Returns: 1 if clean, 0 otherwise
331 static int userspace_is_clean(struct dm_dirty_log *log, region_t region)
334 uint64_t region64 = (uint64_t)region;
337 struct log_c *lc = log->context;
339 rdata_size = sizeof(is_clean);
340 r = userspace_do_request(lc, lc->uuid, DM_ULOG_IS_CLEAN,
341 (char *)®ion64, sizeof(region64),
342 (char *)&is_clean, &rdata_size);
344 return (r) ? 0 : (int)is_clean;
350 * Check if the region is in-sync. If there is any sort
351 * of failure when consulting the server, we assume that
352 * the region is not in sync.
354 * If 'can_block' is set, return immediately
356 * Returns: 1 if in-sync, 0 if not-in-sync, -EWOULDBLOCK
358 static int userspace_in_sync(struct dm_dirty_log *log, region_t region,
362 uint64_t region64 = region;
365 struct log_c *lc = log->context;
368 * We can never respond directly - even if in_sync_hint is
369 * set. This is because another machine could see a device
370 * failure and mark the region out-of-sync. If we don't go
371 * to userspace to ask, we might think the region is in-sync
372 * and allow a read to pick up data that is stale. (This is
373 * very unlikely if a device actually fails; but it is very
374 * likely if a connection to one device from one machine fails.)
376 * There still might be a problem if the mirror caches the region
377 * state as in-sync... but then this call would not be made. So,
378 * that is a mirror problem.
383 rdata_size = sizeof(in_sync);
384 r = userspace_do_request(lc, lc->uuid, DM_ULOG_IN_SYNC,
385 (char *)®ion64, sizeof(region64),
386 (char *)&in_sync, &rdata_size);
387 return (r) ? 0 : (int)in_sync;
390 static int flush_one_by_one(struct log_c *lc, struct list_head *flush_list)
393 struct flush_entry *fe;
395 list_for_each_entry(fe, flush_list, list) {
396 r = userspace_do_request(lc, lc->uuid, fe->type,
407 static int flush_by_group(struct log_c *lc, struct list_head *flush_list)
412 struct flush_entry *fe, *tmp_fe;
414 uint64_t group[MAX_FLUSH_GROUP_COUNT];
417 * Group process the requests
419 while (!list_empty(flush_list)) {
422 list_for_each_entry_safe(fe, tmp_fe, flush_list, list) {
423 group[count] = fe->region;
426 list_move(&fe->list, &tmp_list);
429 if (count >= MAX_FLUSH_GROUP_COUNT)
433 r = userspace_do_request(lc, lc->uuid, type,
435 count * sizeof(uint64_t),
438 /* Group send failed. Attempt one-by-one. */
439 list_splice_init(&tmp_list, flush_list);
440 r = flush_one_by_one(lc, flush_list);
446 * Must collect flush_entrys that were successfully processed
447 * as a group so that they will be free'd by the caller.
449 list_splice_init(&tmp_list, flush_list);
457 * This function is ok to block.
458 * The flush happens in two stages. First, it sends all
459 * clear/mark requests that are on the list. Then it
460 * tells the server to commit them. This gives the
461 * server a chance to optimise the commit, instead of
462 * doing it for every request.
464 * Additionally, we could implement another thread that
465 * sends the requests up to the server - reducing the
466 * load on flush. Then the flush would have less in
467 * the list and be responsible for the finishing commit.
469 * Returns: 0 on success, < 0 on failure
471 static int userspace_flush(struct dm_dirty_log *log)
475 struct log_c *lc = log->context;
476 LIST_HEAD(mark_list);
477 LIST_HEAD(clear_list);
478 struct flush_entry *fe, *tmp_fe;
480 spin_lock_irqsave(&lc->flush_lock, flags);
481 list_splice_init(&lc->mark_list, &mark_list);
482 list_splice_init(&lc->clear_list, &clear_list);
483 spin_unlock_irqrestore(&lc->flush_lock, flags);
485 if (list_empty(&mark_list) && list_empty(&clear_list))
488 r = flush_by_group(lc, &mark_list);
492 r = flush_by_group(lc, &clear_list);
496 r = userspace_do_request(lc, lc->uuid, DM_ULOG_FLUSH,
497 NULL, 0, NULL, NULL);
501 * We can safely remove these entries, even if failure.
502 * Calling code will receive an error and will know that
503 * the log facility has failed.
505 list_for_each_entry_safe(fe, tmp_fe, &mark_list, list) {
507 mempool_free(fe, flush_entry_pool);
509 list_for_each_entry_safe(fe, tmp_fe, &clear_list, list) {
511 mempool_free(fe, flush_entry_pool);
515 dm_table_event(lc->ti->table);
521 * userspace_mark_region
523 * This function should avoid blocking unless absolutely required.
524 * (Memory allocation is valid for blocking.)
526 static void userspace_mark_region(struct dm_dirty_log *log, region_t region)
529 struct log_c *lc = log->context;
530 struct flush_entry *fe;
532 /* Wait for an allocation, but _never_ fail */
533 fe = mempool_alloc(flush_entry_pool, GFP_NOIO);
536 spin_lock_irqsave(&lc->flush_lock, flags);
537 fe->type = DM_ULOG_MARK_REGION;
539 list_add(&fe->list, &lc->mark_list);
540 spin_unlock_irqrestore(&lc->flush_lock, flags);
546 * userspace_clear_region
548 * This function must not block.
549 * So, the alloc can't block. In the worst case, it is ok to
550 * fail. It would simply mean we can't clear the region.
551 * Does nothing to current sync context, but does mean
552 * the region will be re-sync'ed on a reload of the mirror
553 * even though it is in-sync.
555 static void userspace_clear_region(struct dm_dirty_log *log, region_t region)
558 struct log_c *lc = log->context;
559 struct flush_entry *fe;
562 * If we fail to allocate, we skip the clearing of
563 * the region. This doesn't hurt us in any way, except
564 * to cause the region to be resync'ed when the
565 * device is activated next time.
567 fe = mempool_alloc(flush_entry_pool, GFP_ATOMIC);
569 DMERR("Failed to allocate memory to clear region.");
573 spin_lock_irqsave(&lc->flush_lock, flags);
574 fe->type = DM_ULOG_CLEAR_REGION;
576 list_add(&fe->list, &lc->clear_list);
577 spin_unlock_irqrestore(&lc->flush_lock, flags);
583 * userspace_get_resync_work
585 * Get a region that needs recovery. It is valid to return
586 * an error for this function.
588 * Returns: 1 if region filled, 0 if no work, <0 on error
590 static int userspace_get_resync_work(struct dm_dirty_log *log, region_t *region)
594 struct log_c *lc = log->context;
596 int64_t i; /* 64-bit for mix arch compatibility */
600 if (lc->in_sync_hint >= lc->region_count)
603 rdata_size = sizeof(pkg);
604 r = userspace_do_request(lc, lc->uuid, DM_ULOG_GET_RESYNC_WORK,
606 (char *)&pkg, &rdata_size);
609 return (r) ? r : (int)pkg.i;
613 * userspace_set_region_sync
615 * Set the sync status of a given region. This function
618 static void userspace_set_region_sync(struct dm_dirty_log *log,
619 region_t region, int in_sync)
622 struct log_c *lc = log->context;
629 pkg.i = (int64_t)in_sync;
631 r = userspace_do_request(lc, lc->uuid, DM_ULOG_SET_REGION_SYNC,
632 (char *)&pkg, sizeof(pkg),
636 * It would be nice to be able to report failures.
637 * However, it is easy emough to detect and resolve.
643 * userspace_get_sync_count
645 * If there is any sort of failure when consulting the server,
646 * we assume that the sync count is zero.
648 * Returns: sync count on success, 0 on failure
650 static region_t userspace_get_sync_count(struct dm_dirty_log *log)
655 struct log_c *lc = log->context;
657 rdata_size = sizeof(sync_count);
658 r = userspace_do_request(lc, lc->uuid, DM_ULOG_GET_SYNC_COUNT,
660 (char *)&sync_count, &rdata_size);
665 if (sync_count >= lc->region_count)
666 lc->in_sync_hint = lc->region_count;
668 return (region_t)sync_count;
674 * Returns: amount of space consumed
676 static int userspace_status(struct dm_dirty_log *log, status_type_t status_type,
677 char *result, unsigned maxlen)
681 size_t sz = (size_t)maxlen;
682 struct log_c *lc = log->context;
684 switch (status_type) {
685 case STATUSTYPE_INFO:
686 r = userspace_do_request(lc, lc->uuid, DM_ULOG_STATUS_INFO,
692 DMEMIT("%s 1 COM_FAILURE", log->type->name);
695 case STATUSTYPE_TABLE:
697 table_args = strchr(lc->usr_argv_str, ' ');
698 BUG_ON(!table_args); /* There will always be a ' ' */
701 DMEMIT("%s %u %s %s ", log->type->name, lc->usr_argc,
702 lc->uuid, table_args);
705 return (r) ? 0 : (int)sz;
709 * userspace_is_remote_recovering
711 * Returns: 1 if region recovering, 0 otherwise
713 static int userspace_is_remote_recovering(struct dm_dirty_log *log,
717 uint64_t region64 = region;
718 struct log_c *lc = log->context;
719 static unsigned long long limit;
721 int64_t is_recovering;
722 uint64_t in_sync_hint;
724 size_t rdata_size = sizeof(pkg);
727 * Once the mirror has been reported to be in-sync,
728 * it will never again ask for recovery work. So,
729 * we can safely say there is not a remote machine
730 * recovering if the device is in-sync. (in_sync_hint
731 * must be reset at resume time.)
733 if (region < lc->in_sync_hint)
735 else if (jiffies < limit)
738 limit = jiffies + (HZ / 4);
739 r = userspace_do_request(lc, lc->uuid, DM_ULOG_IS_REMOTE_RECOVERING,
740 (char *)®ion64, sizeof(region64),
741 (char *)&pkg, &rdata_size);
745 lc->in_sync_hint = pkg.in_sync_hint;
747 return (int)pkg.is_recovering;
750 static struct dm_dirty_log_type _userspace_type = {
752 .module = THIS_MODULE,
753 .ctr = userspace_ctr,
754 .dtr = userspace_dtr,
755 .presuspend = userspace_presuspend,
756 .postsuspend = userspace_postsuspend,
757 .resume = userspace_resume,
758 .get_region_size = userspace_get_region_size,
759 .is_clean = userspace_is_clean,
760 .in_sync = userspace_in_sync,
761 .flush = userspace_flush,
762 .mark_region = userspace_mark_region,
763 .clear_region = userspace_clear_region,
764 .get_resync_work = userspace_get_resync_work,
765 .set_region_sync = userspace_set_region_sync,
766 .get_sync_count = userspace_get_sync_count,
767 .status = userspace_status,
768 .is_remote_recovering = userspace_is_remote_recovering,
771 static int __init userspace_dirty_log_init(void)
775 flush_entry_pool = mempool_create(100, flush_entry_alloc,
776 flush_entry_free, NULL);
778 if (!flush_entry_pool) {
779 DMWARN("Unable to create flush_entry_pool: No memory.");
783 r = dm_ulog_tfr_init();
785 DMWARN("Unable to initialize userspace log communications");
786 mempool_destroy(flush_entry_pool);
790 r = dm_dirty_log_type_register(&_userspace_type);
792 DMWARN("Couldn't register userspace dirty log type");
794 mempool_destroy(flush_entry_pool);
798 DMINFO("version " DM_LOG_USERSPACE_VSN " loaded");
802 static void __exit userspace_dirty_log_exit(void)
804 dm_dirty_log_type_unregister(&_userspace_type);
806 mempool_destroy(flush_entry_pool);
808 DMINFO("version " DM_LOG_USERSPACE_VSN " unloaded");
812 module_init(userspace_dirty_log_init);
813 module_exit(userspace_dirty_log_exit);
815 MODULE_DESCRIPTION(DM_NAME " userspace dirty log link");
816 MODULE_AUTHOR("Jonathan Brassow <dm-devel@redhat.com>");
817 MODULE_LICENSE("GPL");