Documentation: fix kbuild typos and wording
[linux-2.6/libata-dev.git] / drivers / md / dm-log-userspace-base.c
blob1ed0094f064b351aa121e0858fbb31cbaee9f166
1 /*
2 * Copyright (C) 2006-2009 Red Hat, Inc.
4 * This file is released under the LGPL.
5 */
7 #include <linux/bio.h>
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 struct flush_entry {
16 int type;
17 region_t region;
18 struct list_head list;
21 struct log_c {
22 struct dm_target *ti;
23 uint32_t region_size;
24 region_t region_count;
25 uint64_t luid;
26 char uuid[DM_UUID_LEN];
28 char *usr_argv_str;
29 uint32_t usr_argc;
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)
53 kfree(element);
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)
60 int r;
63 * If the server isn't there, -ESRCH is returned,
64 * and we must keep trying until the server is
65 * restored.
67 retry:
68 r = dm_consult_userspace(uuid, lc->luid, request_type, data,
69 data_size, rdata, rdata_size);
71 if (r != -ESRCH)
72 return r;
74 DMERR(" Userspace log server not found.");
75 while (1) {
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,
80 lc->usr_argv_str,
81 strlen(lc->usr_argv_str) + 1,
82 NULL, NULL);
83 if (!r)
84 break;
86 DMINFO("Reconnected to userspace log server... DM_ULOG_CTR complete");
87 r = dm_consult_userspace(uuid, lc->luid, DM_ULOG_RESUME, NULL,
88 0, NULL, NULL);
89 if (!r)
90 goto retry;
92 DMERR("Error trying to resume userspace log: %d", r);
94 return -ESRCH;
97 static int build_constructor_string(struct dm_target *ti,
98 unsigned argc, char **argv,
99 char **ctr_str)
101 int i, str_size;
102 char *str = NULL;
104 *ctr_str = NULL;
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);
112 if (!str) {
113 DMWARN("Unable to allocate memory for constructor string");
114 return -ENOMEM;
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]);
121 *ctr_str = str;
122 return str_size;
126 * userspace_ctr
128 * argv contains:
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
136 * else.
138 static int userspace_ctr(struct dm_dirty_log *log, struct dm_target *ti,
139 unsigned argc, char **argv)
141 int r = 0;
142 int str_size;
143 char *ctr_str = NULL;
144 struct log_c *lc = NULL;
145 uint64_t rdata;
146 size_t rdata_size = sizeof(rdata);
148 if (argc < 3) {
149 DMWARN("Too few arguments to userspace dirty log");
150 return -EINVAL;
153 lc = kmalloc(sizeof(*lc), GFP_KERNEL);
154 if (!lc) {
155 DMWARN("Unable to allocate userspace log context.");
156 return -ENOMEM;
159 /* The ptr value is sufficient for local unique id */
160 lc->luid = (unsigned long)lc;
162 lc->ti = ti;
164 if (strlen(argv[0]) > (DM_UUID_LEN - 1)) {
165 DMWARN("UUID argument too long.");
166 kfree(lc);
167 return -EINVAL;
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);
175 if (str_size < 0) {
176 kfree(lc);
177 return str_size;
180 /* Send table string */
181 r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_CTR,
182 ctr_str, str_size, NULL, NULL);
184 if (r == -ESRCH) {
185 DMERR("Userspace log server not found");
186 goto out;
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);
194 if (r) {
195 DMERR("Failed to get region size of dirty log");
196 goto out;
199 lc->region_size = (uint32_t)rdata;
200 lc->region_count = dm_sector_div_up(ti->len, lc->region_size);
202 out:
203 if (r) {
204 kfree(lc);
205 kfree(ctr_str);
206 } else {
207 lc->usr_argv_str = ctr_str;
208 lc->usr_argc = argc;
209 log->context = lc;
212 return r;
215 static void userspace_dtr(struct dm_dirty_log *log)
217 int r;
218 struct log_c *lc = log->context;
220 r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_DTR,
221 NULL, 0,
222 NULL, NULL);
224 kfree(lc->usr_argv_str);
225 kfree(lc);
227 return;
230 static int userspace_presuspend(struct dm_dirty_log *log)
232 int r;
233 struct log_c *lc = log->context;
235 r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_PRESUSPEND,
236 NULL, 0,
237 NULL, NULL);
239 return r;
242 static int userspace_postsuspend(struct dm_dirty_log *log)
244 int r;
245 struct log_c *lc = log->context;
247 r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_POSTSUSPEND,
248 NULL, 0,
249 NULL, NULL);
251 return r;
254 static int userspace_resume(struct dm_dirty_log *log)
256 int r;
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,
261 NULL, 0,
262 NULL, NULL);
264 return r;
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;
275 * userspace_is_clean
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)
284 int r;
285 uint64_t region64 = (uint64_t)region;
286 int64_t is_clean;
287 size_t rdata_size;
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 *)&region64, sizeof(region64),
293 (char *)&is_clean, &rdata_size);
295 return (r) ? 0 : (int)is_clean;
299 * userspace_in_sync
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,
310 int can_block)
312 int r;
313 uint64_t region64 = region;
314 int64_t in_sync;
315 size_t rdata_size;
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.
331 if (!can_block)
332 return -EWOULDBLOCK;
334 rdata_size = sizeof(in_sync);
335 r = userspace_do_request(lc, lc->uuid, DM_ULOG_IN_SYNC,
336 (char *)&region64, sizeof(region64),
337 (char *)&in_sync, &rdata_size);
338 return (r) ? 0 : (int)in_sync;
342 * userspace_flush
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)
360 int r = 0;
361 unsigned long flags;
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))
371 return 0;
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,
377 * do it one by one.
380 list_for_each_entry(fe, &flush_list, list) {
381 r = userspace_do_request(lc, lc->uuid, fe->type,
382 (char *)&fe->region,
383 sizeof(fe->region),
384 NULL, NULL);
385 if (r)
386 goto fail;
389 r = userspace_do_request(lc, lc->uuid, DM_ULOG_FLUSH,
390 NULL, 0, NULL, NULL);
392 fail:
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) {
399 list_del(&fe->list);
400 mempool_free(fe, flush_entry_pool);
403 if (r)
404 dm_table_event(lc->ti->table);
406 return r;
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)
417 unsigned long flags;
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);
423 BUG_ON(!fe);
425 spin_lock_irqsave(&lc->flush_lock, flags);
426 fe->type = DM_ULOG_MARK_REGION;
427 fe->region = region;
428 list_add(&fe->list, &lc->flush_list);
429 spin_unlock_irqrestore(&lc->flush_lock, flags);
431 return;
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)
446 unsigned long flags;
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);
457 if (!fe) {
458 DMERR("Failed to allocate memory to clear region.");
459 return;
462 spin_lock_irqsave(&lc->flush_lock, flags);
463 fe->type = DM_ULOG_CLEAR_REGION;
464 fe->region = region;
465 list_add(&fe->list, &lc->flush_list);
466 spin_unlock_irqrestore(&lc->flush_lock, flags);
468 return;
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)
481 int r;
482 size_t rdata_size;
483 struct log_c *lc = log->context;
484 struct {
485 int64_t i; /* 64-bit for mix arch compatibility */
486 region_t r;
487 } pkg;
489 if (lc->in_sync_hint >= lc->region_count)
490 return 0;
492 rdata_size = sizeof(pkg);
493 r = userspace_do_request(lc, lc->uuid, DM_ULOG_GET_RESYNC_WORK,
494 NULL, 0,
495 (char *)&pkg, &rdata_size);
497 *region = pkg.r;
498 return (r) ? r : (int)pkg.i;
502 * userspace_set_region_sync
504 * Set the sync status of a given region. This function
505 * must not fail.
507 static void userspace_set_region_sync(struct dm_dirty_log *log,
508 region_t region, int in_sync)
510 int r;
511 struct log_c *lc = log->context;
512 struct {
513 region_t r;
514 int64_t i;
515 } pkg;
517 pkg.r = region;
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),
522 NULL, NULL);
525 * It would be nice to be able to report failures.
526 * However, it is easy emough to detect and resolve.
528 return;
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)
541 int r;
542 size_t rdata_size;
543 uint64_t sync_count;
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,
548 NULL, 0,
549 (char *)&sync_count, &rdata_size);
551 if (r)
552 return 0;
554 if (sync_count >= lc->region_count)
555 lc->in_sync_hint = lc->region_count;
557 return (region_t)sync_count;
561 * userspace_status
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)
568 int r = 0;
569 char *table_args;
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,
576 NULL, 0,
577 result, &sz);
579 if (r) {
580 sz = 0;
581 DMEMIT("%s 1 COM_FAILURE", log->type->name);
583 break;
584 case STATUSTYPE_TABLE:
585 sz = 0;
586 table_args = strchr(lc->usr_argv_str, ' ');
587 BUG_ON(!table_args); /* There will always be a ' ' */
588 table_args++;
590 DMEMIT("%s %u %s %s ", log->type->name, lc->usr_argc,
591 lc->uuid, table_args);
592 break;
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,
603 region_t region)
605 int r;
606 uint64_t region64 = region;
607 struct log_c *lc = log->context;
608 static unsigned long long limit;
609 struct {
610 int64_t is_recovering;
611 uint64_t in_sync_hint;
612 } pkg;
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)
623 return 0;
624 else if (jiffies < limit)
625 return 1;
627 limit = jiffies + (HZ / 4);
628 r = userspace_do_request(lc, lc->uuid, DM_ULOG_IS_REMOTE_RECOVERING,
629 (char *)&region64, sizeof(region64),
630 (char *)&pkg, &rdata_size);
631 if (r)
632 return 1;
634 lc->in_sync_hint = pkg.in_sync_hint;
636 return (int)pkg.is_recovering;
639 static struct dm_dirty_log_type _userspace_type = {
640 .name = "userspace",
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)
662 int r = 0;
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.");
669 return -ENOMEM;
672 r = dm_ulog_tfr_init();
673 if (r) {
674 DMWARN("Unable to initialize userspace log communications");
675 mempool_destroy(flush_entry_pool);
676 return r;
679 r = dm_dirty_log_type_register(&_userspace_type);
680 if (r) {
681 DMWARN("Couldn't register userspace dirty log type");
682 dm_ulog_tfr_exit();
683 mempool_destroy(flush_entry_pool);
684 return r;
687 DMINFO("version 1.0.0 loaded");
688 return 0;
691 static void __exit userspace_dirty_log_exit(void)
693 dm_dirty_log_type_unregister(&_userspace_type);
694 dm_ulog_tfr_exit();
695 mempool_destroy(flush_entry_pool);
697 DMINFO("version 1.0.0 unloaded");
698 return;
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");