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
;
25 char uuid
[DM_UUID_LEN
];
31 * in_sync_hint gets set when doing is_remote_recovering. It
32 * represents the first region that needs recovery. IOW, the
33 * first zero bit of sync_bits. This can be useful for to limit
34 * traffic for calls like is_remote_recovering and get_resync_work,
35 * but be take care in its use for anything else.
37 uint64_t in_sync_hint
;
39 spinlock_t flush_lock
;
40 struct list_head flush_list
; /* only for clear and mark requests */
43 static mempool_t
*flush_entry_pool
;
45 static void *flush_entry_alloc(gfp_t gfp_mask
, void *pool_data
)
47 return kmalloc(sizeof(struct flush_entry
), gfp_mask
);
50 static void flush_entry_free(void *element
, void *pool_data
)
55 static int userspace_do_request(struct log_c
*lc
, const char *uuid
,
56 int request_type
, char *data
, size_t data_size
,
57 char *rdata
, size_t *rdata_size
)
62 * If the server isn't there, -ESRCH is returned,
63 * and we must keep trying until the server is
67 r
= dm_consult_userspace(uuid
, lc
->luid
, request_type
, data
,
68 data_size
, rdata
, rdata_size
);
73 DMERR(" Userspace log server not found.");
75 set_current_state(TASK_INTERRUPTIBLE
);
76 schedule_timeout(2*HZ
);
77 DMWARN("Attempting to contact userspace log server...");
78 r
= dm_consult_userspace(uuid
, lc
->luid
, DM_ULOG_CTR
,
80 strlen(lc
->usr_argv_str
) + 1,
85 DMINFO("Reconnected to userspace log server... DM_ULOG_CTR complete");
86 r
= dm_consult_userspace(uuid
, lc
->luid
, DM_ULOG_RESUME
, NULL
,
91 DMERR("Error trying to resume userspace log: %d", r
);
96 static int build_constructor_string(struct dm_target
*ti
,
97 unsigned argc
, char **argv
,
105 for (i
= 0, str_size
= 0; i
< argc
; i
++)
106 str_size
+= strlen(argv
[i
]) + 1; /* +1 for space between args */
108 str_size
+= 20; /* Max number of chars in a printed u64 number */
110 str
= kzalloc(str_size
, GFP_KERNEL
);
112 DMWARN("Unable to allocate memory for constructor string");
116 str_size
= sprintf(str
, "%llu", (unsigned long long)ti
->len
);
117 for (i
= 0; i
< argc
; i
++)
118 str_size
+= sprintf(str
+ str_size
, " %s", argv
[i
]);
128 * <UUID> <other args>
129 * Where 'other args' is the userspace implementation specific log
130 * arguments. An example might be:
131 * <UUID> clustered_disk <arg count> <log dev> <region_size> [[no]sync]
133 * So, this module will strip off the <UUID> for identification purposes
134 * when communicating with userspace about a log; but will pass on everything
137 static int userspace_ctr(struct dm_dirty_log
*log
, struct dm_target
*ti
,
138 unsigned argc
, char **argv
)
142 char *ctr_str
= NULL
;
143 struct log_c
*lc
= NULL
;
145 size_t rdata_size
= sizeof(rdata
);
148 DMWARN("Too few arguments to userspace dirty log");
152 lc
= kmalloc(sizeof(*lc
), GFP_KERNEL
);
154 DMWARN("Unable to allocate userspace log context.");
158 /* The ptr value is sufficient for local unique id */
159 lc
->luid
= (unsigned long)lc
;
163 if (strlen(argv
[0]) > (DM_UUID_LEN
- 1)) {
164 DMWARN("UUID argument too long.");
169 strncpy(lc
->uuid
, argv
[0], DM_UUID_LEN
);
170 spin_lock_init(&lc
->flush_lock
);
171 INIT_LIST_HEAD(&lc
->flush_list
);
173 str_size
= build_constructor_string(ti
, argc
- 1, argv
+ 1, &ctr_str
);
179 /* Send table string */
180 r
= dm_consult_userspace(lc
->uuid
, lc
->luid
, DM_ULOG_CTR
,
181 ctr_str
, str_size
, NULL
, NULL
);
184 DMERR("Userspace log server not found");
188 /* Since the region size does not change, get it now */
189 rdata_size
= sizeof(rdata
);
190 r
= dm_consult_userspace(lc
->uuid
, lc
->luid
, DM_ULOG_GET_REGION_SIZE
,
191 NULL
, 0, (char *)&rdata
, &rdata_size
);
194 DMERR("Failed to get region size of dirty log");
198 lc
->region_size
= (uint32_t)rdata
;
199 lc
->region_count
= dm_sector_div_up(ti
->len
, lc
->region_size
);
206 lc
->usr_argv_str
= ctr_str
;
214 static void userspace_dtr(struct dm_dirty_log
*log
)
217 struct log_c
*lc
= log
->context
;
219 r
= dm_consult_userspace(lc
->uuid
, lc
->luid
, DM_ULOG_DTR
,
223 kfree(lc
->usr_argv_str
);
229 static int userspace_presuspend(struct dm_dirty_log
*log
)
232 struct log_c
*lc
= log
->context
;
234 r
= dm_consult_userspace(lc
->uuid
, lc
->luid
, DM_ULOG_PRESUSPEND
,
241 static int userspace_postsuspend(struct dm_dirty_log
*log
)
244 struct log_c
*lc
= log
->context
;
246 r
= dm_consult_userspace(lc
->uuid
, lc
->luid
, DM_ULOG_POSTSUSPEND
,
253 static int userspace_resume(struct dm_dirty_log
*log
)
256 struct log_c
*lc
= log
->context
;
258 lc
->in_sync_hint
= 0;
259 r
= dm_consult_userspace(lc
->uuid
, lc
->luid
, DM_ULOG_RESUME
,
266 static uint32_t userspace_get_region_size(struct dm_dirty_log
*log
)
268 struct log_c
*lc
= log
->context
;
270 return lc
->region_size
;
276 * Check whether a region is clean. If there is any sort of
277 * failure when consulting the server, we return not clean.
279 * Returns: 1 if clean, 0 otherwise
281 static int userspace_is_clean(struct dm_dirty_log
*log
, region_t region
)
284 uint64_t region64
= (uint64_t)region
;
287 struct log_c
*lc
= log
->context
;
289 rdata_size
= sizeof(is_clean
);
290 r
= userspace_do_request(lc
, lc
->uuid
, DM_ULOG_IS_CLEAN
,
291 (char *)®ion64
, sizeof(region64
),
292 (char *)&is_clean
, &rdata_size
);
294 return (r
) ? 0 : (int)is_clean
;
300 * Check if the region is in-sync. If there is any sort
301 * of failure when consulting the server, we assume that
302 * the region is not in sync.
304 * If 'can_block' is set, return immediately
306 * Returns: 1 if in-sync, 0 if not-in-sync, -EWOULDBLOCK
308 static int userspace_in_sync(struct dm_dirty_log
*log
, region_t region
,
312 uint64_t region64
= region
;
315 struct log_c
*lc
= log
->context
;
318 * We can never respond directly - even if in_sync_hint is
319 * set. This is because another machine could see a device
320 * failure and mark the region out-of-sync. If we don't go
321 * to userspace to ask, we might think the region is in-sync
322 * and allow a read to pick up data that is stale. (This is
323 * very unlikely if a device actually fails; but it is very
324 * likely if a connection to one device from one machine fails.)
326 * There still might be a problem if the mirror caches the region
327 * state as in-sync... but then this call would not be made. So,
328 * that is a mirror problem.
333 rdata_size
= sizeof(in_sync
);
334 r
= userspace_do_request(lc
, lc
->uuid
, DM_ULOG_IN_SYNC
,
335 (char *)®ion64
, sizeof(region64
),
336 (char *)&in_sync
, &rdata_size
);
337 return (r
) ? 0 : (int)in_sync
;
343 * This function is ok to block.
344 * The flush happens in two stages. First, it sends all
345 * clear/mark requests that are on the list. Then it
346 * tells the server to commit them. This gives the
347 * server a chance to optimise the commit, instead of
348 * doing it for every request.
350 * Additionally, we could implement another thread that
351 * sends the requests up to the server - reducing the
352 * load on flush. Then the flush would have less in
353 * the list and be responsible for the finishing commit.
355 * Returns: 0 on success, < 0 on failure
357 static int userspace_flush(struct dm_dirty_log
*log
)
361 struct log_c
*lc
= log
->context
;
362 LIST_HEAD(flush_list
);
363 struct flush_entry
*fe
, *tmp_fe
;
365 spin_lock_irqsave(&lc
->flush_lock
, flags
);
366 list_splice_init(&lc
->flush_list
, &flush_list
);
367 spin_unlock_irqrestore(&lc
->flush_lock
, flags
);
369 if (list_empty(&flush_list
))
373 * FIXME: Count up requests, group request types,
374 * allocate memory to stick all requests in and
375 * send to server in one go. Failing the allocation,
379 list_for_each_entry(fe
, &flush_list
, list
) {
380 r
= userspace_do_request(lc
, lc
->uuid
, fe
->type
,
388 r
= userspace_do_request(lc
, lc
->uuid
, DM_ULOG_FLUSH
,
389 NULL
, 0, NULL
, NULL
);
393 * We can safely remove these entries, even if failure.
394 * Calling code will receive an error and will know that
395 * the log facility has failed.
397 list_for_each_entry_safe(fe
, tmp_fe
, &flush_list
, list
) {
399 mempool_free(fe
, flush_entry_pool
);
403 dm_table_event(lc
->ti
->table
);
409 * userspace_mark_region
411 * This function should avoid blocking unless absolutely required.
412 * (Memory allocation is valid for blocking.)
414 static void userspace_mark_region(struct dm_dirty_log
*log
, region_t region
)
417 struct log_c
*lc
= log
->context
;
418 struct flush_entry
*fe
;
420 /* Wait for an allocation, but _never_ fail */
421 fe
= mempool_alloc(flush_entry_pool
, GFP_NOIO
);
424 spin_lock_irqsave(&lc
->flush_lock
, flags
);
425 fe
->type
= DM_ULOG_MARK_REGION
;
427 list_add(&fe
->list
, &lc
->flush_list
);
428 spin_unlock_irqrestore(&lc
->flush_lock
, flags
);
434 * userspace_clear_region
436 * This function must not block.
437 * So, the alloc can't block. In the worst case, it is ok to
438 * fail. It would simply mean we can't clear the region.
439 * Does nothing to current sync context, but does mean
440 * the region will be re-sync'ed on a reload of the mirror
441 * even though it is in-sync.
443 static void userspace_clear_region(struct dm_dirty_log
*log
, region_t region
)
446 struct log_c
*lc
= log
->context
;
447 struct flush_entry
*fe
;
450 * If we fail to allocate, we skip the clearing of
451 * the region. This doesn't hurt us in any way, except
452 * to cause the region to be resync'ed when the
453 * device is activated next time.
455 fe
= mempool_alloc(flush_entry_pool
, GFP_ATOMIC
);
457 DMERR("Failed to allocate memory to clear region.");
461 spin_lock_irqsave(&lc
->flush_lock
, flags
);
462 fe
->type
= DM_ULOG_CLEAR_REGION
;
464 list_add(&fe
->list
, &lc
->flush_list
);
465 spin_unlock_irqrestore(&lc
->flush_lock
, flags
);
471 * userspace_get_resync_work
473 * Get a region that needs recovery. It is valid to return
474 * an error for this function.
476 * Returns: 1 if region filled, 0 if no work, <0 on error
478 static int userspace_get_resync_work(struct dm_dirty_log
*log
, region_t
*region
)
482 struct log_c
*lc
= log
->context
;
484 int64_t i
; /* 64-bit for mix arch compatibility */
488 if (lc
->in_sync_hint
>= lc
->region_count
)
491 rdata_size
= sizeof(pkg
);
492 r
= userspace_do_request(lc
, lc
->uuid
, DM_ULOG_GET_RESYNC_WORK
,
494 (char *)&pkg
, &rdata_size
);
497 return (r
) ? r
: (int)pkg
.i
;
501 * userspace_set_region_sync
503 * Set the sync status of a given region. This function
506 static void userspace_set_region_sync(struct dm_dirty_log
*log
,
507 region_t region
, int in_sync
)
510 struct log_c
*lc
= log
->context
;
517 pkg
.i
= (int64_t)in_sync
;
519 r
= userspace_do_request(lc
, lc
->uuid
, DM_ULOG_SET_REGION_SYNC
,
520 (char *)&pkg
, sizeof(pkg
),
524 * It would be nice to be able to report failures.
525 * However, it is easy emough to detect and resolve.
531 * userspace_get_sync_count
533 * If there is any sort of failure when consulting the server,
534 * we assume that the sync count is zero.
536 * Returns: sync count on success, 0 on failure
538 static region_t
userspace_get_sync_count(struct dm_dirty_log
*log
)
543 struct log_c
*lc
= log
->context
;
545 rdata_size
= sizeof(sync_count
);
546 r
= userspace_do_request(lc
, lc
->uuid
, DM_ULOG_GET_SYNC_COUNT
,
548 (char *)&sync_count
, &rdata_size
);
553 if (sync_count
>= lc
->region_count
)
554 lc
->in_sync_hint
= lc
->region_count
;
556 return (region_t
)sync_count
;
562 * Returns: amount of space consumed
564 static int userspace_status(struct dm_dirty_log
*log
, status_type_t status_type
,
565 char *result
, unsigned maxlen
)
569 size_t sz
= (size_t)maxlen
;
570 struct log_c
*lc
= log
->context
;
572 switch (status_type
) {
573 case STATUSTYPE_INFO
:
574 r
= userspace_do_request(lc
, lc
->uuid
, DM_ULOG_STATUS_INFO
,
580 DMEMIT("%s 1 COM_FAILURE", log
->type
->name
);
583 case STATUSTYPE_TABLE
:
585 table_args
= strchr(lc
->usr_argv_str
, ' ');
586 BUG_ON(!table_args
); /* There will always be a ' ' */
589 DMEMIT("%s %u %s %s ", log
->type
->name
, lc
->usr_argc
,
590 lc
->uuid
, table_args
);
593 return (r
) ? 0 : (int)sz
;
597 * userspace_is_remote_recovering
599 * Returns: 1 if region recovering, 0 otherwise
601 static int userspace_is_remote_recovering(struct dm_dirty_log
*log
,
605 uint64_t region64
= region
;
606 struct log_c
*lc
= log
->context
;
607 static unsigned long long limit
;
609 int64_t is_recovering
;
610 uint64_t in_sync_hint
;
612 size_t rdata_size
= sizeof(pkg
);
615 * Once the mirror has been reported to be in-sync,
616 * it will never again ask for recovery work. So,
617 * we can safely say there is not a remote machine
618 * recovering if the device is in-sync. (in_sync_hint
619 * must be reset at resume time.)
621 if (region
< lc
->in_sync_hint
)
623 else if (jiffies
< limit
)
626 limit
= jiffies
+ (HZ
/ 4);
627 r
= userspace_do_request(lc
, lc
->uuid
, DM_ULOG_IS_REMOTE_RECOVERING
,
628 (char *)®ion64
, sizeof(region64
),
629 (char *)&pkg
, &rdata_size
);
633 lc
->in_sync_hint
= pkg
.in_sync_hint
;
635 return (int)pkg
.is_recovering
;
638 static struct dm_dirty_log_type _userspace_type
= {
640 .module
= THIS_MODULE
,
641 .ctr
= userspace_ctr
,
642 .dtr
= userspace_dtr
,
643 .presuspend
= userspace_presuspend
,
644 .postsuspend
= userspace_postsuspend
,
645 .resume
= userspace_resume
,
646 .get_region_size
= userspace_get_region_size
,
647 .is_clean
= userspace_is_clean
,
648 .in_sync
= userspace_in_sync
,
649 .flush
= userspace_flush
,
650 .mark_region
= userspace_mark_region
,
651 .clear_region
= userspace_clear_region
,
652 .get_resync_work
= userspace_get_resync_work
,
653 .set_region_sync
= userspace_set_region_sync
,
654 .get_sync_count
= userspace_get_sync_count
,
655 .status
= userspace_status
,
656 .is_remote_recovering
= userspace_is_remote_recovering
,
659 static int __init
userspace_dirty_log_init(void)
663 flush_entry_pool
= mempool_create(100, flush_entry_alloc
,
664 flush_entry_free
, NULL
);
666 if (!flush_entry_pool
) {
667 DMWARN("Unable to create flush_entry_pool: No memory.");
671 r
= dm_ulog_tfr_init();
673 DMWARN("Unable to initialize userspace log communications");
674 mempool_destroy(flush_entry_pool
);
678 r
= dm_dirty_log_type_register(&_userspace_type
);
680 DMWARN("Couldn't register userspace dirty log type");
682 mempool_destroy(flush_entry_pool
);
686 DMINFO("version 1.0.0 loaded");
690 static void __exit
userspace_dirty_log_exit(void)
692 dm_dirty_log_type_unregister(&_userspace_type
);
694 mempool_destroy(flush_entry_pool
);
696 DMINFO("version 1.0.0 unloaded");
700 module_init(userspace_dirty_log_init
);
701 module_exit(userspace_dirty_log_exit
);
703 MODULE_DESCRIPTION(DM_NAME
" userspace dirty log link");
704 MODULE_AUTHOR("Jonathan Brassow <dm-devel@redhat.com>");
705 MODULE_LICENSE("GPL");