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 for (i = 0, str_size = 0; i < argc; i++)
115 str_size += sprintf(str + str_size, "%s ", argv[i]);
116 str_size += sprintf(str + str_size, "%llu",
117 (unsigned long long)ti->len);
127 * <UUID> <other args>
128 * Where 'other args' is the userspace implementation specific log
129 * arguments. An example might be:
130 * <UUID> clustered_disk <arg count> <log dev> <region_size> [[no]sync]
132 * So, this module will strip off the <UUID> for identification purposes
133 * when communicating with userspace about a log; but will pass on everything
136 static int userspace_ctr(struct dm_dirty_log *log, struct dm_target *ti,
137 unsigned argc, char **argv)
141 char *ctr_str = NULL;
142 struct log_c *lc = NULL;
144 size_t rdata_size = sizeof(rdata);
147 DMWARN("Too few arguments to userspace dirty log");
151 lc = kmalloc(sizeof(*lc), GFP_KERNEL);
153 DMWARN("Unable to allocate userspace log context.");
159 if (strlen(argv[0]) > (DM_UUID_LEN - 1)) {
160 DMWARN("UUID argument too long.");
165 strncpy(lc->uuid, argv[0], DM_UUID_LEN);
166 spin_lock_init(&lc->flush_lock);
167 INIT_LIST_HEAD(&lc->flush_list);
169 str_size = build_constructor_string(ti, argc - 1, argv + 1, &ctr_str);
175 /* Send table string */
176 r = dm_consult_userspace(lc->uuid, DM_ULOG_CTR,
177 ctr_str, str_size, NULL, NULL);
180 DMERR("Userspace log server not found");
184 /* Since the region size does not change, get it now */
185 rdata_size = sizeof(rdata);
186 r = dm_consult_userspace(lc->uuid, DM_ULOG_GET_REGION_SIZE,
187 NULL, 0, (char *)&rdata, &rdata_size);
190 DMERR("Failed to get region size of dirty log");
194 lc->region_size = (uint32_t)rdata;
195 lc->region_count = dm_sector_div_up(ti->len, lc->region_size);
202 lc->usr_argv_str = ctr_str;
210 static void userspace_dtr(struct dm_dirty_log *log)
213 struct log_c *lc = log->context;
215 r = dm_consult_userspace(lc->uuid, DM_ULOG_DTR,
219 kfree(lc->usr_argv_str);
225 static int userspace_presuspend(struct dm_dirty_log *log)
228 struct log_c *lc = log->context;
230 r = dm_consult_userspace(lc->uuid, DM_ULOG_PRESUSPEND,
237 static int userspace_postsuspend(struct dm_dirty_log *log)
240 struct log_c *lc = log->context;
242 r = dm_consult_userspace(lc->uuid, DM_ULOG_POSTSUSPEND,
249 static int userspace_resume(struct dm_dirty_log *log)
252 struct log_c *lc = log->context;
254 lc->in_sync_hint = 0;
255 r = dm_consult_userspace(lc->uuid, DM_ULOG_RESUME,
262 static uint32_t userspace_get_region_size(struct dm_dirty_log *log)
264 struct log_c *lc = log->context;
266 return lc->region_size;
272 * Check whether a region is clean. If there is any sort of
273 * failure when consulting the server, we return not clean.
275 * Returns: 1 if clean, 0 otherwise
277 static int userspace_is_clean(struct dm_dirty_log *log, region_t region)
280 uint64_t region64 = (uint64_t)region;
283 struct log_c *lc = log->context;
285 rdata_size = sizeof(is_clean);
286 r = userspace_do_request(lc, lc->uuid, DM_ULOG_IS_CLEAN,
287 (char *)®ion64, sizeof(region64),
288 (char *)&is_clean, &rdata_size);
290 return (r) ? 0 : (int)is_clean;
296 * Check if the region is in-sync. If there is any sort
297 * of failure when consulting the server, we assume that
298 * the region is not in sync.
300 * If 'can_block' is set, return immediately
302 * Returns: 1 if in-sync, 0 if not-in-sync, -EWOULDBLOCK
304 static int userspace_in_sync(struct dm_dirty_log *log, region_t region,
308 uint64_t region64 = region;
311 struct log_c *lc = log->context;
314 * We can never respond directly - even if in_sync_hint is
315 * set. This is because another machine could see a device
316 * failure and mark the region out-of-sync. If we don't go
317 * to userspace to ask, we might think the region is in-sync
318 * and allow a read to pick up data that is stale. (This is
319 * very unlikely if a device actually fails; but it is very
320 * likely if a connection to one device from one machine fails.)
322 * There still might be a problem if the mirror caches the region
323 * state as in-sync... but then this call would not be made. So,
324 * that is a mirror problem.
329 rdata_size = sizeof(in_sync);
330 r = userspace_do_request(lc, lc->uuid, DM_ULOG_IN_SYNC,
331 (char *)®ion64, sizeof(region64),
332 (char *)&in_sync, &rdata_size);
333 return (r) ? 0 : (int)in_sync;
339 * This function is ok to block.
340 * The flush happens in two stages. First, it sends all
341 * clear/mark requests that are on the list. Then it
342 * tells the server to commit them. This gives the
343 * server a chance to optimise the commit, instead of
344 * doing it for every request.
346 * Additionally, we could implement another thread that
347 * sends the requests up to the server - reducing the
348 * load on flush. Then the flush would have less in
349 * the list and be responsible for the finishing commit.
351 * Returns: 0 on success, < 0 on failure
353 static int userspace_flush(struct dm_dirty_log *log)
357 struct log_c *lc = log->context;
358 LIST_HEAD(flush_list);
359 struct flush_entry *fe, *tmp_fe;
361 spin_lock_irqsave(&lc->flush_lock, flags);
362 list_splice_init(&lc->flush_list, &flush_list);
363 spin_unlock_irqrestore(&lc->flush_lock, flags);
365 if (list_empty(&flush_list))
369 * FIXME: Count up requests, group request types,
370 * allocate memory to stick all requests in and
371 * send to server in one go. Failing the allocation,
375 list_for_each_entry(fe, &flush_list, list) {
376 r = userspace_do_request(lc, lc->uuid, fe->type,
384 r = userspace_do_request(lc, lc->uuid, DM_ULOG_FLUSH,
385 NULL, 0, NULL, NULL);
389 * We can safely remove these entries, even if failure.
390 * Calling code will receive an error and will know that
391 * the log facility has failed.
393 list_for_each_entry_safe(fe, tmp_fe, &flush_list, list) {
395 mempool_free(fe, flush_entry_pool);
399 dm_table_event(lc->ti->table);
405 * userspace_mark_region
407 * This function should avoid blocking unless absolutely required.
408 * (Memory allocation is valid for blocking.)
410 static void userspace_mark_region(struct dm_dirty_log *log, region_t region)
413 struct log_c *lc = log->context;
414 struct flush_entry *fe;
416 /* Wait for an allocation, but _never_ fail */
417 fe = mempool_alloc(flush_entry_pool, GFP_NOIO);
420 spin_lock_irqsave(&lc->flush_lock, flags);
421 fe->type = DM_ULOG_MARK_REGION;
423 list_add(&fe->list, &lc->flush_list);
424 spin_unlock_irqrestore(&lc->flush_lock, flags);
430 * userspace_clear_region
432 * This function must not block.
433 * So, the alloc can't block. In the worst case, it is ok to
434 * fail. It would simply mean we can't clear the region.
435 * Does nothing to current sync context, but does mean
436 * the region will be re-sync'ed on a reload of the mirror
437 * even though it is in-sync.
439 static void userspace_clear_region(struct dm_dirty_log *log, region_t region)
442 struct log_c *lc = log->context;
443 struct flush_entry *fe;
446 * If we fail to allocate, we skip the clearing of
447 * the region. This doesn't hurt us in any way, except
448 * to cause the region to be resync'ed when the
449 * device is activated next time.
451 fe = mempool_alloc(flush_entry_pool, GFP_ATOMIC);
453 DMERR("Failed to allocate memory to clear region.");
457 spin_lock_irqsave(&lc->flush_lock, flags);
458 fe->type = DM_ULOG_CLEAR_REGION;
460 list_add(&fe->list, &lc->flush_list);
461 spin_unlock_irqrestore(&lc->flush_lock, flags);
467 * userspace_get_resync_work
469 * Get a region that needs recovery. It is valid to return
470 * an error for this function.
472 * Returns: 1 if region filled, 0 if no work, <0 on error
474 static int userspace_get_resync_work(struct dm_dirty_log *log, region_t *region)
478 struct log_c *lc = log->context;
480 int64_t i; /* 64-bit for mix arch compatibility */
484 if (lc->in_sync_hint >= lc->region_count)
487 rdata_size = sizeof(pkg);
488 r = userspace_do_request(lc, lc->uuid, DM_ULOG_GET_RESYNC_WORK,
490 (char *)&pkg, &rdata_size);
493 return (r) ? r : (int)pkg.i;
497 * userspace_set_region_sync
499 * Set the sync status of a given region. This function
502 static void userspace_set_region_sync(struct dm_dirty_log *log,
503 region_t region, int in_sync)
506 struct log_c *lc = log->context;
513 pkg.i = (int64_t)in_sync;
515 r = userspace_do_request(lc, lc->uuid, DM_ULOG_SET_REGION_SYNC,
516 (char *)&pkg, sizeof(pkg),
520 * It would be nice to be able to report failures.
521 * However, it is easy emough to detect and resolve.
527 * userspace_get_sync_count
529 * If there is any sort of failure when consulting the server,
530 * we assume that the sync count is zero.
532 * Returns: sync count on success, 0 on failure
534 static region_t userspace_get_sync_count(struct dm_dirty_log *log)
539 struct log_c *lc = log->context;
541 rdata_size = sizeof(sync_count);
542 r = userspace_do_request(lc, lc->uuid, DM_ULOG_GET_SYNC_COUNT,
544 (char *)&sync_count, &rdata_size);
549 if (sync_count >= lc->region_count)
550 lc->in_sync_hint = lc->region_count;
552 return (region_t)sync_count;
558 * Returns: amount of space consumed
560 static int userspace_status(struct dm_dirty_log *log, status_type_t status_type,
561 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 DMEMIT("%s %u %s %s", log->type->name, lc->usr_argc + 1,
581 lc->uuid, lc->usr_argv_str);
584 return (r) ? 0 : (int)sz;
588 * userspace_is_remote_recovering
590 * Returns: 1 if region recovering, 0 otherwise
592 static int userspace_is_remote_recovering(struct dm_dirty_log *log,
596 uint64_t region64 = region;
597 struct log_c *lc = log->context;
598 static unsigned long long limit;
600 int64_t is_recovering;
601 uint64_t in_sync_hint;
603 size_t rdata_size = sizeof(pkg);
606 * Once the mirror has been reported to be in-sync,
607 * it will never again ask for recovery work. So,
608 * we can safely say there is not a remote machine
609 * recovering if the device is in-sync. (in_sync_hint
610 * must be reset at resume time.)
612 if (region < lc->in_sync_hint)
614 else if (jiffies < limit)
617 limit = jiffies + (HZ / 4);
618 r = userspace_do_request(lc, lc->uuid, DM_ULOG_IS_REMOTE_RECOVERING,
619 (char *)®ion64, sizeof(region64),
620 (char *)&pkg, &rdata_size);
624 lc->in_sync_hint = pkg.in_sync_hint;
626 return (int)pkg.is_recovering;
629 static struct dm_dirty_log_type _userspace_type = {
631 .module = THIS_MODULE,
632 .ctr = userspace_ctr,
633 .dtr = userspace_dtr,
634 .presuspend = userspace_presuspend,
635 .postsuspend = userspace_postsuspend,
636 .resume = userspace_resume,
637 .get_region_size = userspace_get_region_size,
638 .is_clean = userspace_is_clean,
639 .in_sync = userspace_in_sync,
640 .flush = userspace_flush,
641 .mark_region = userspace_mark_region,
642 .clear_region = userspace_clear_region,
643 .get_resync_work = userspace_get_resync_work,
644 .set_region_sync = userspace_set_region_sync,
645 .get_sync_count = userspace_get_sync_count,
646 .status = userspace_status,
647 .is_remote_recovering = userspace_is_remote_recovering,
650 static int __init userspace_dirty_log_init(void)
654 flush_entry_pool = mempool_create(100, flush_entry_alloc,
655 flush_entry_free, NULL);
657 if (!flush_entry_pool) {
658 DMWARN("Unable to create flush_entry_pool: No memory.");
662 r = dm_ulog_tfr_init();
664 DMWARN("Unable to initialize userspace log communications");
665 mempool_destroy(flush_entry_pool);
669 r = dm_dirty_log_type_register(&_userspace_type);
671 DMWARN("Couldn't register userspace dirty log type");
673 mempool_destroy(flush_entry_pool);
677 DMINFO("version 1.0.0 loaded");
681 static void __exit userspace_dirty_log_exit(void)
683 dm_dirty_log_type_unregister(&_userspace_type);
685 mempool_destroy(flush_entry_pool);
687 DMINFO("version 1.0.0 unloaded");
691 module_init(userspace_dirty_log_init);
692 module_exit(userspace_dirty_log_exit);
694 MODULE_DESCRIPTION(DM_NAME " userspace dirty log link");
695 MODULE_AUTHOR("Jonathan Brassow <dm-devel@redhat.com>");
696 MODULE_LICENSE("GPL");