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"
18 struct list_head list;
24 region_t region_count;
26 char uuid[DM_UUID_LEN];
32 * in_sync_hint gets set when doing is_remote_recovering. It
33 * represents the first region that needs recovery. IOW, the
34 * first zero bit of sync_bits. This can be useful for to limit
35 * traffic for calls like is_remote_recovering and get_resync_work,
36 * but be take care in its use for anything else.
38 uint64_t in_sync_hint;
40 spinlock_t flush_lock;
41 struct list_head flush_list; /* only for clear and mark requests */
44 static mempool_t *flush_entry_pool;
46 static void *flush_entry_alloc(gfp_t gfp_mask, void *pool_data)
48 return kmalloc(sizeof(struct flush_entry), gfp_mask);
51 static void flush_entry_free(void *element, void *pool_data)
56 static int userspace_do_request(struct log_c *lc, const char *uuid,
57 int request_type, char *data, size_t data_size,
58 char *rdata, size_t *rdata_size)
63 * If the server isn't there, -ESRCH is returned,
64 * and we must keep trying until the server is
68 r = dm_consult_userspace(uuid, lc->luid, request_type, data,
69 data_size, rdata, rdata_size);
74 DMERR(" Userspace log server not found.");
76 set_current_state(TASK_INTERRUPTIBLE);
77 schedule_timeout(2*HZ);
78 DMWARN("Attempting to contact userspace log server...");
79 r = dm_consult_userspace(uuid, lc->luid, DM_ULOG_CTR,
81 strlen(lc->usr_argv_str) + 1,
86 DMINFO("Reconnected to userspace log server... DM_ULOG_CTR complete");
87 r = dm_consult_userspace(uuid, lc->luid, DM_ULOG_RESUME, NULL,
92 DMERR("Error trying to resume userspace log: %d", r);
97 static int build_constructor_string(struct dm_target *ti,
98 unsigned argc, char **argv,
106 for (i = 0, str_size = 0; i < argc; i++)
107 str_size += strlen(argv[i]) + 1; /* +1 for space between args */
109 str_size += 20; /* Max number of chars in a printed u64 number */
111 str = kzalloc(str_size, GFP_KERNEL);
113 DMWARN("Unable to allocate memory for constructor string");
117 str_size = sprintf(str, "%llu", (unsigned long long)ti->len);
118 for (i = 0; i < argc; i++)
119 str_size += sprintf(str + str_size, " %s", argv[i]);
129 * <UUID> <other args>
130 * Where 'other args' is the userspace implementation specific log
131 * arguments. An example might be:
132 * <UUID> clustered_disk <arg count> <log dev> <region_size> [[no]sync]
134 * So, this module will strip off the <UUID> for identification purposes
135 * when communicating with userspace about a log; but will pass on everything
138 static int userspace_ctr(struct dm_dirty_log *log, struct dm_target *ti,
139 unsigned argc, char **argv)
143 char *ctr_str = NULL;
144 struct log_c *lc = NULL;
146 size_t rdata_size = sizeof(rdata);
149 DMWARN("Too few arguments to userspace dirty log");
153 lc = kmalloc(sizeof(*lc), GFP_KERNEL);
155 DMWARN("Unable to allocate userspace log context.");
159 /* The ptr value is sufficient for local unique id */
160 lc->luid = (unsigned long)lc;
164 if (strlen(argv[0]) > (DM_UUID_LEN - 1)) {
165 DMWARN("UUID argument too long.");
170 strncpy(lc->uuid, argv[0], DM_UUID_LEN);
171 spin_lock_init(&lc->flush_lock);
172 INIT_LIST_HEAD(&lc->flush_list);
174 str_size = build_constructor_string(ti, argc - 1, argv + 1, &ctr_str);
180 /* Send table string */
181 r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_CTR,
182 ctr_str, str_size, NULL, NULL);
185 DMERR("Userspace log server not found");
189 /* Since the region size does not change, get it now */
190 rdata_size = sizeof(rdata);
191 r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_GET_REGION_SIZE,
192 NULL, 0, (char *)&rdata, &rdata_size);
195 DMERR("Failed to get region size of dirty log");
199 lc->region_size = (uint32_t)rdata;
200 lc->region_count = dm_sector_div_up(ti->len, lc->region_size);
207 lc->usr_argv_str = ctr_str;
215 static void userspace_dtr(struct dm_dirty_log *log)
218 struct log_c *lc = log->context;
220 r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_DTR,
224 kfree(lc->usr_argv_str);
230 static int userspace_presuspend(struct dm_dirty_log *log)
233 struct log_c *lc = log->context;
235 r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_PRESUSPEND,
242 static int userspace_postsuspend(struct dm_dirty_log *log)
245 struct log_c *lc = log->context;
247 r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_POSTSUSPEND,
254 static int userspace_resume(struct dm_dirty_log *log)
257 struct log_c *lc = log->context;
259 lc->in_sync_hint = 0;
260 r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_RESUME,
267 static uint32_t userspace_get_region_size(struct dm_dirty_log *log)
269 struct log_c *lc = log->context;
271 return lc->region_size;
277 * Check whether a region is clean. If there is any sort of
278 * failure when consulting the server, we return not clean.
280 * Returns: 1 if clean, 0 otherwise
282 static int userspace_is_clean(struct dm_dirty_log *log, region_t region)
285 uint64_t region64 = (uint64_t)region;
288 struct log_c *lc = log->context;
290 rdata_size = sizeof(is_clean);
291 r = userspace_do_request(lc, lc->uuid, DM_ULOG_IS_CLEAN,
292 (char *)®ion64, sizeof(region64),
293 (char *)&is_clean, &rdata_size);
295 return (r) ? 0 : (int)is_clean;
301 * Check if the region is in-sync. If there is any sort
302 * of failure when consulting the server, we assume that
303 * the region is not in sync.
305 * If 'can_block' is set, return immediately
307 * Returns: 1 if in-sync, 0 if not-in-sync, -EWOULDBLOCK
309 static int userspace_in_sync(struct dm_dirty_log *log, region_t region,
313 uint64_t region64 = region;
316 struct log_c *lc = log->context;
319 * We can never respond directly - even if in_sync_hint is
320 * set. This is because another machine could see a device
321 * failure and mark the region out-of-sync. If we don't go
322 * to userspace to ask, we might think the region is in-sync
323 * and allow a read to pick up data that is stale. (This is
324 * very unlikely if a device actually fails; but it is very
325 * likely if a connection to one device from one machine fails.)
327 * There still might be a problem if the mirror caches the region
328 * state as in-sync... but then this call would not be made. So,
329 * that is a mirror problem.
334 rdata_size = sizeof(in_sync);
335 r = userspace_do_request(lc, lc->uuid, DM_ULOG_IN_SYNC,
336 (char *)®ion64, sizeof(region64),
337 (char *)&in_sync, &rdata_size);
338 return (r) ? 0 : (int)in_sync;
344 * This function is ok to block.
345 * The flush happens in two stages. First, it sends all
346 * clear/mark requests that are on the list. Then it
347 * tells the server to commit them. This gives the
348 * server a chance to optimise the commit, instead of
349 * doing it for every request.
351 * Additionally, we could implement another thread that
352 * sends the requests up to the server - reducing the
353 * load on flush. Then the flush would have less in
354 * the list and be responsible for the finishing commit.
356 * Returns: 0 on success, < 0 on failure
358 static int userspace_flush(struct dm_dirty_log *log)
362 struct log_c *lc = log->context;
363 LIST_HEAD(flush_list);
364 struct flush_entry *fe, *tmp_fe;
366 spin_lock_irqsave(&lc->flush_lock, flags);
367 list_splice_init(&lc->flush_list, &flush_list);
368 spin_unlock_irqrestore(&lc->flush_lock, flags);
370 if (list_empty(&flush_list))
374 * FIXME: Count up requests, group request types,
375 * allocate memory to stick all requests in and
376 * send to server in one go. Failing the allocation,
380 list_for_each_entry(fe, &flush_list, list) {
381 r = userspace_do_request(lc, lc->uuid, fe->type,
389 r = userspace_do_request(lc, lc->uuid, DM_ULOG_FLUSH,
390 NULL, 0, NULL, NULL);
394 * We can safely remove these entries, even if failure.
395 * Calling code will receive an error and will know that
396 * the log facility has failed.
398 list_for_each_entry_safe(fe, tmp_fe, &flush_list, list) {
400 mempool_free(fe, flush_entry_pool);
404 dm_table_event(lc->ti->table);
410 * userspace_mark_region
412 * This function should avoid blocking unless absolutely required.
413 * (Memory allocation is valid for blocking.)
415 static void userspace_mark_region(struct dm_dirty_log *log, region_t region)
418 struct log_c *lc = log->context;
419 struct flush_entry *fe;
421 /* Wait for an allocation, but _never_ fail */
422 fe = mempool_alloc(flush_entry_pool, GFP_NOIO);
425 spin_lock_irqsave(&lc->flush_lock, flags);
426 fe->type = DM_ULOG_MARK_REGION;
428 list_add(&fe->list, &lc->flush_list);
429 spin_unlock_irqrestore(&lc->flush_lock, flags);
435 * userspace_clear_region
437 * This function must not block.
438 * So, the alloc can't block. In the worst case, it is ok to
439 * fail. It would simply mean we can't clear the region.
440 * Does nothing to current sync context, but does mean
441 * the region will be re-sync'ed on a reload of the mirror
442 * even though it is in-sync.
444 static void userspace_clear_region(struct dm_dirty_log *log, region_t region)
447 struct log_c *lc = log->context;
448 struct flush_entry *fe;
451 * If we fail to allocate, we skip the clearing of
452 * the region. This doesn't hurt us in any way, except
453 * to cause the region to be resync'ed when the
454 * device is activated next time.
456 fe = mempool_alloc(flush_entry_pool, GFP_ATOMIC);
458 DMERR("Failed to allocate memory to clear region.");
462 spin_lock_irqsave(&lc->flush_lock, flags);
463 fe->type = DM_ULOG_CLEAR_REGION;
465 list_add(&fe->list, &lc->flush_list);
466 spin_unlock_irqrestore(&lc->flush_lock, flags);
472 * userspace_get_resync_work
474 * Get a region that needs recovery. It is valid to return
475 * an error for this function.
477 * Returns: 1 if region filled, 0 if no work, <0 on error
479 static int userspace_get_resync_work(struct dm_dirty_log *log, region_t *region)
483 struct log_c *lc = log->context;
485 int64_t i; /* 64-bit for mix arch compatibility */
489 if (lc->in_sync_hint >= lc->region_count)
492 rdata_size = sizeof(pkg);
493 r = userspace_do_request(lc, lc->uuid, DM_ULOG_GET_RESYNC_WORK,
495 (char *)&pkg, &rdata_size);
498 return (r) ? r : (int)pkg.i;
502 * userspace_set_region_sync
504 * Set the sync status of a given region. This function
507 static void userspace_set_region_sync(struct dm_dirty_log *log,
508 region_t region, int in_sync)
511 struct log_c *lc = log->context;
518 pkg.i = (int64_t)in_sync;
520 r = userspace_do_request(lc, lc->uuid, DM_ULOG_SET_REGION_SYNC,
521 (char *)&pkg, sizeof(pkg),
525 * It would be nice to be able to report failures.
526 * However, it is easy emough to detect and resolve.
532 * userspace_get_sync_count
534 * If there is any sort of failure when consulting the server,
535 * we assume that the sync count is zero.
537 * Returns: sync count on success, 0 on failure
539 static region_t userspace_get_sync_count(struct dm_dirty_log *log)
544 struct log_c *lc = log->context;
546 rdata_size = sizeof(sync_count);
547 r = userspace_do_request(lc, lc->uuid, DM_ULOG_GET_SYNC_COUNT,
549 (char *)&sync_count, &rdata_size);
554 if (sync_count >= lc->region_count)
555 lc->in_sync_hint = lc->region_count;
557 return (region_t)sync_count;
563 * Returns: amount of space consumed
565 static int userspace_status(struct dm_dirty_log *log, status_type_t status_type,
566 char *result, unsigned maxlen)
570 size_t sz = (size_t)maxlen;
571 struct log_c *lc = log->context;
573 switch (status_type) {
574 case STATUSTYPE_INFO:
575 r = userspace_do_request(lc, lc->uuid, DM_ULOG_STATUS_INFO,
581 DMEMIT("%s 1 COM_FAILURE", log->type->name);
584 case STATUSTYPE_TABLE:
586 table_args = strchr(lc->usr_argv_str, ' ');
587 BUG_ON(!table_args); /* There will always be a ' ' */
590 DMEMIT("%s %u %s %s ", log->type->name, lc->usr_argc,
591 lc->uuid, table_args);
594 return (r) ? 0 : (int)sz;
598 * userspace_is_remote_recovering
600 * Returns: 1 if region recovering, 0 otherwise
602 static int userspace_is_remote_recovering(struct dm_dirty_log *log,
606 uint64_t region64 = region;
607 struct log_c *lc = log->context;
608 static unsigned long long limit;
610 int64_t is_recovering;
611 uint64_t in_sync_hint;
613 size_t rdata_size = sizeof(pkg);
616 * Once the mirror has been reported to be in-sync,
617 * it will never again ask for recovery work. So,
618 * we can safely say there is not a remote machine
619 * recovering if the device is in-sync. (in_sync_hint
620 * must be reset at resume time.)
622 if (region < lc->in_sync_hint)
624 else if (jiffies < limit)
627 limit = jiffies + (HZ / 4);
628 r = userspace_do_request(lc, lc->uuid, DM_ULOG_IS_REMOTE_RECOVERING,
629 (char *)®ion64, sizeof(region64),
630 (char *)&pkg, &rdata_size);
634 lc->in_sync_hint = pkg.in_sync_hint;
636 return (int)pkg.is_recovering;
639 static struct dm_dirty_log_type _userspace_type = {
641 .module = THIS_MODULE,
642 .ctr = userspace_ctr,
643 .dtr = userspace_dtr,
644 .presuspend = userspace_presuspend,
645 .postsuspend = userspace_postsuspend,
646 .resume = userspace_resume,
647 .get_region_size = userspace_get_region_size,
648 .is_clean = userspace_is_clean,
649 .in_sync = userspace_in_sync,
650 .flush = userspace_flush,
651 .mark_region = userspace_mark_region,
652 .clear_region = userspace_clear_region,
653 .get_resync_work = userspace_get_resync_work,
654 .set_region_sync = userspace_set_region_sync,
655 .get_sync_count = userspace_get_sync_count,
656 .status = userspace_status,
657 .is_remote_recovering = userspace_is_remote_recovering,
660 static int __init userspace_dirty_log_init(void)
664 flush_entry_pool = mempool_create(100, flush_entry_alloc,
665 flush_entry_free, NULL);
667 if (!flush_entry_pool) {
668 DMWARN("Unable to create flush_entry_pool: No memory.");
672 r = dm_ulog_tfr_init();
674 DMWARN("Unable to initialize userspace log communications");
675 mempool_destroy(flush_entry_pool);
679 r = dm_dirty_log_type_register(&_userspace_type);
681 DMWARN("Couldn't register userspace dirty log type");
683 mempool_destroy(flush_entry_pool);
687 DMINFO("version 1.0.0 loaded");
691 static void __exit userspace_dirty_log_exit(void)
693 dm_dirty_log_type_unregister(&_userspace_type);
695 mempool_destroy(flush_entry_pool);
697 DMINFO("version 1.0.0 unloaded");
701 module_init(userspace_dirty_log_init);
702 module_exit(userspace_dirty_log_exit);
704 MODULE_DESCRIPTION(DM_NAME " userspace dirty log link");
705 MODULE_AUTHOR("Jonathan Brassow <dm-devel@redhat.com>");
706 MODULE_LICENSE("GPL");