blob: 1ed0094f064b351aa121e0858fbb31cbaee9f166 [file] [log] [blame]
Jonthan Brassowf5db4af2009-06-22 10:12:35 +01001/*
2 * Copyright (C) 2006-2009 Red Hat, Inc.
3 *
4 * This file is released under the LGPL.
5 */
6
7#include <linux/bio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Jonthan Brassowf5db4af2009-06-22 10:12:35 +01009#include <linux/dm-dirty-log.h>
10#include <linux/device-mapper.h>
11#include <linux/dm-log-userspace.h>
12
13#include "dm-log-userspace-transfer.h"
14
15struct flush_entry {
16 int type;
17 region_t region;
18 struct list_head list;
19};
20
21struct log_c {
22 struct dm_target *ti;
23 uint32_t region_size;
24 region_t region_count;
Jonathan Brassow7ec23d52009-09-04 20:40:34 +010025 uint64_t luid;
Jonthan Brassowf5db4af2009-06-22 10:12:35 +010026 char uuid[DM_UUID_LEN];
27
28 char *usr_argv_str;
29 uint32_t usr_argc;
30
31 /*
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.
37 */
38 uint64_t in_sync_hint;
39
40 spinlock_t flush_lock;
41 struct list_head flush_list; /* only for clear and mark requests */
42};
43
44static mempool_t *flush_entry_pool;
45
46static void *flush_entry_alloc(gfp_t gfp_mask, void *pool_data)
47{
48 return kmalloc(sizeof(struct flush_entry), gfp_mask);
49}
50
51static void flush_entry_free(void *element, void *pool_data)
52{
53 kfree(element);
54}
55
56static 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)
59{
60 int r;
61
62 /*
63 * If the server isn't there, -ESRCH is returned,
64 * and we must keep trying until the server is
65 * restored.
66 */
67retry:
Jonathan Brassow7ec23d52009-09-04 20:40:34 +010068 r = dm_consult_userspace(uuid, lc->luid, request_type, data,
Jonthan Brassowf5db4af2009-06-22 10:12:35 +010069 data_size, rdata, rdata_size);
70
71 if (r != -ESRCH)
72 return r;
73
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...");
Jonathan Brassow7ec23d52009-09-04 20:40:34 +010079 r = dm_consult_userspace(uuid, lc->luid, DM_ULOG_CTR,
80 lc->usr_argv_str,
Jonthan Brassowf5db4af2009-06-22 10:12:35 +010081 strlen(lc->usr_argv_str) + 1,
82 NULL, NULL);
83 if (!r)
84 break;
85 }
86 DMINFO("Reconnected to userspace log server... DM_ULOG_CTR complete");
Jonathan Brassow7ec23d52009-09-04 20:40:34 +010087 r = dm_consult_userspace(uuid, lc->luid, DM_ULOG_RESUME, NULL,
Jonthan Brassowf5db4af2009-06-22 10:12:35 +010088 0, NULL, NULL);
89 if (!r)
90 goto retry;
91
92 DMERR("Error trying to resume userspace log: %d", r);
93
94 return -ESRCH;
95}
96
97static int build_constructor_string(struct dm_target *ti,
98 unsigned argc, char **argv,
99 char **ctr_str)
100{
101 int i, str_size;
102 char *str = NULL;
103
104 *ctr_str = NULL;
105
106 for (i = 0, str_size = 0; i < argc; i++)
107 str_size += strlen(argv[i]) + 1; /* +1 for space between args */
108
109 str_size += 20; /* Max number of chars in a printed u64 number */
110
111 str = kzalloc(str_size, GFP_KERNEL);
112 if (!str) {
113 DMWARN("Unable to allocate memory for constructor string");
114 return -ENOMEM;
115 }
116
Jonathan Brassowb8313b62009-09-04 20:40:30 +0100117 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]);
Jonthan Brassowf5db4af2009-06-22 10:12:35 +0100120
121 *ctr_str = str;
122 return str_size;
123}
124
125/*
126 * userspace_ctr
127 *
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]
133 *
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.
137 */
138static int userspace_ctr(struct dm_dirty_log *log, struct dm_target *ti,
139 unsigned argc, char **argv)
140{
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);
147
148 if (argc < 3) {
149 DMWARN("Too few arguments to userspace dirty log");
150 return -EINVAL;
151 }
152
153 lc = kmalloc(sizeof(*lc), GFP_KERNEL);
154 if (!lc) {
155 DMWARN("Unable to allocate userspace log context.");
156 return -ENOMEM;
157 }
158
Jonathan Brassow7ec23d52009-09-04 20:40:34 +0100159 /* The ptr value is sufficient for local unique id */
Andrew Mortonbca915a2009-10-16 23:18:15 +0100160 lc->luid = (unsigned long)lc;
Jonathan Brassow7ec23d52009-09-04 20:40:34 +0100161
Jonthan Brassowf5db4af2009-06-22 10:12:35 +0100162 lc->ti = ti;
163
164 if (strlen(argv[0]) > (DM_UUID_LEN - 1)) {
165 DMWARN("UUID argument too long.");
166 kfree(lc);
167 return -EINVAL;
168 }
169
170 strncpy(lc->uuid, argv[0], DM_UUID_LEN);
171 spin_lock_init(&lc->flush_lock);
172 INIT_LIST_HEAD(&lc->flush_list);
173
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;
178 }
179
180 /* Send table string */
Jonathan Brassow7ec23d52009-09-04 20:40:34 +0100181 r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_CTR,
Jonthan Brassowf5db4af2009-06-22 10:12:35 +0100182 ctr_str, str_size, NULL, NULL);
183
184 if (r == -ESRCH) {
185 DMERR("Userspace log server not found");
186 goto out;
187 }
188
189 /* Since the region size does not change, get it now */
190 rdata_size = sizeof(rdata);
Jonathan Brassow7ec23d52009-09-04 20:40:34 +0100191 r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_GET_REGION_SIZE,
Jonthan Brassowf5db4af2009-06-22 10:12:35 +0100192 NULL, 0, (char *)&rdata, &rdata_size);
193
194 if (r) {
195 DMERR("Failed to get region size of dirty log");
196 goto out;
197 }
198
199 lc->region_size = (uint32_t)rdata;
200 lc->region_count = dm_sector_div_up(ti->len, lc->region_size);
201
202out:
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;
210 }
211
212 return r;
213}
214
215static void userspace_dtr(struct dm_dirty_log *log)
216{
217 int r;
218 struct log_c *lc = log->context;
219
Jonathan Brassow7ec23d52009-09-04 20:40:34 +0100220 r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_DTR,
Jonthan Brassowf5db4af2009-06-22 10:12:35 +0100221 NULL, 0,
222 NULL, NULL);
223
224 kfree(lc->usr_argv_str);
225 kfree(lc);
226
227 return;
228}
229
230static int userspace_presuspend(struct dm_dirty_log *log)
231{
232 int r;
233 struct log_c *lc = log->context;
234
Jonathan Brassow7ec23d52009-09-04 20:40:34 +0100235 r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_PRESUSPEND,
Jonthan Brassowf5db4af2009-06-22 10:12:35 +0100236 NULL, 0,
237 NULL, NULL);
238
239 return r;
240}
241
242static int userspace_postsuspend(struct dm_dirty_log *log)
243{
244 int r;
245 struct log_c *lc = log->context;
246
Jonathan Brassow7ec23d52009-09-04 20:40:34 +0100247 r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_POSTSUSPEND,
Jonthan Brassowf5db4af2009-06-22 10:12:35 +0100248 NULL, 0,
249 NULL, NULL);
250
251 return r;
252}
253
254static int userspace_resume(struct dm_dirty_log *log)
255{
256 int r;
257 struct log_c *lc = log->context;
258
259 lc->in_sync_hint = 0;
Jonathan Brassow7ec23d52009-09-04 20:40:34 +0100260 r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_RESUME,
Jonthan Brassowf5db4af2009-06-22 10:12:35 +0100261 NULL, 0,
262 NULL, NULL);
263
264 return r;
265}
266
267static uint32_t userspace_get_region_size(struct dm_dirty_log *log)
268{
269 struct log_c *lc = log->context;
270
271 return lc->region_size;
272}
273
274/*
275 * userspace_is_clean
276 *
277 * Check whether a region is clean. If there is any sort of
278 * failure when consulting the server, we return not clean.
279 *
280 * Returns: 1 if clean, 0 otherwise
281 */
282static int userspace_is_clean(struct dm_dirty_log *log, region_t region)
283{
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;
289
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);
294
295 return (r) ? 0 : (int)is_clean;
296}
297
298/*
299 * userspace_in_sync
300 *
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.
304 *
305 * If 'can_block' is set, return immediately
306 *
307 * Returns: 1 if in-sync, 0 if not-in-sync, -EWOULDBLOCK
308 */
309static int userspace_in_sync(struct dm_dirty_log *log, region_t region,
310 int can_block)
311{
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;
317
318 /*
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.)
326 *
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.
330 */
331 if (!can_block)
332 return -EWOULDBLOCK;
333
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;
339}
340
341/*
342 * userspace_flush
343 *
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.
350 *
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.
355 *
356 * Returns: 0 on success, < 0 on failure
357 */
358static int userspace_flush(struct dm_dirty_log *log)
359{
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;
365
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);
369
370 if (list_empty(&flush_list))
371 return 0;
372
373 /*
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.
378 */
379
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;
387 }
388
389 r = userspace_do_request(lc, lc->uuid, DM_ULOG_FLUSH,
390 NULL, 0, NULL, NULL);
391
392fail:
393 /*
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.
397 */
398 list_for_each_entry_safe(fe, tmp_fe, &flush_list, list) {
399 list_del(&fe->list);
400 mempool_free(fe, flush_entry_pool);
401 }
402
403 if (r)
404 dm_table_event(lc->ti->table);
405
406 return r;
407}
408
409/*
410 * userspace_mark_region
411 *
412 * This function should avoid blocking unless absolutely required.
413 * (Memory allocation is valid for blocking.)
414 */
415static void userspace_mark_region(struct dm_dirty_log *log, region_t region)
416{
417 unsigned long flags;
418 struct log_c *lc = log->context;
419 struct flush_entry *fe;
420
421 /* Wait for an allocation, but _never_ fail */
422 fe = mempool_alloc(flush_entry_pool, GFP_NOIO);
423 BUG_ON(!fe);
424
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);
430
431 return;
432}
433
434/*
435 * userspace_clear_region
436 *
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.
443 */
444static void userspace_clear_region(struct dm_dirty_log *log, region_t region)
445{
446 unsigned long flags;
447 struct log_c *lc = log->context;
448 struct flush_entry *fe;
449
450 /*
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.
455 */
456 fe = mempool_alloc(flush_entry_pool, GFP_ATOMIC);
457 if (!fe) {
458 DMERR("Failed to allocate memory to clear region.");
459 return;
460 }
461
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);
467
468 return;
469}
470
471/*
472 * userspace_get_resync_work
473 *
474 * Get a region that needs recovery. It is valid to return
475 * an error for this function.
476 *
477 * Returns: 1 if region filled, 0 if no work, <0 on error
478 */
479static int userspace_get_resync_work(struct dm_dirty_log *log, region_t *region)
480{
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;
488
489 if (lc->in_sync_hint >= lc->region_count)
490 return 0;
491
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);
496
497 *region = pkg.r;
498 return (r) ? r : (int)pkg.i;
499}
500
501/*
502 * userspace_set_region_sync
503 *
504 * Set the sync status of a given region. This function
505 * must not fail.
506 */
507static void userspace_set_region_sync(struct dm_dirty_log *log,
508 region_t region, int in_sync)
509{
510 int r;
511 struct log_c *lc = log->context;
512 struct {
513 region_t r;
514 int64_t i;
515 } pkg;
516
517 pkg.r = region;
518 pkg.i = (int64_t)in_sync;
519
520 r = userspace_do_request(lc, lc->uuid, DM_ULOG_SET_REGION_SYNC,
521 (char *)&pkg, sizeof(pkg),
522 NULL, NULL);
523
524 /*
525 * It would be nice to be able to report failures.
526 * However, it is easy emough to detect and resolve.
527 */
528 return;
529}
530
531/*
532 * userspace_get_sync_count
533 *
534 * If there is any sort of failure when consulting the server,
535 * we assume that the sync count is zero.
536 *
537 * Returns: sync count on success, 0 on failure
538 */
539static region_t userspace_get_sync_count(struct dm_dirty_log *log)
540{
541 int r;
542 size_t rdata_size;
543 uint64_t sync_count;
544 struct log_c *lc = log->context;
545
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);
550
551 if (r)
552 return 0;
553
554 if (sync_count >= lc->region_count)
555 lc->in_sync_hint = lc->region_count;
556
557 return (region_t)sync_count;
558}
559
560/*
561 * userspace_status
562 *
563 * Returns: amount of space consumed
564 */
565static int userspace_status(struct dm_dirty_log *log, status_type_t status_type,
566 char *result, unsigned maxlen)
567{
568 int r = 0;
Jonathan Brassowb8313b62009-09-04 20:40:30 +0100569 char *table_args;
Jonthan Brassowf5db4af2009-06-22 10:12:35 +0100570 size_t sz = (size_t)maxlen;
571 struct log_c *lc = log->context;
572
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);
578
579 if (r) {
580 sz = 0;
581 DMEMIT("%s 1 COM_FAILURE", log->type->name);
582 }
583 break;
584 case STATUSTYPE_TABLE:
585 sz = 0;
Geert Uytterhoeven0d03d592009-09-10 23:13:28 +0200586 table_args = strchr(lc->usr_argv_str, ' ');
Jonathan Brassowb8313b62009-09-04 20:40:30 +0100587 BUG_ON(!table_args); /* There will always be a ' ' */
588 table_args++;
589
590 DMEMIT("%s %u %s %s ", log->type->name, lc->usr_argc,
591 lc->uuid, table_args);
Jonthan Brassowf5db4af2009-06-22 10:12:35 +0100592 break;
593 }
594 return (r) ? 0 : (int)sz;
595}
596
597/*
598 * userspace_is_remote_recovering
599 *
600 * Returns: 1 if region recovering, 0 otherwise
601 */
602static int userspace_is_remote_recovering(struct dm_dirty_log *log,
603 region_t region)
604{
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);
614
615 /*
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.)
621 */
622 if (region < lc->in_sync_hint)
623 return 0;
624 else if (jiffies < limit)
625 return 1;
626
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;
633
634 lc->in_sync_hint = pkg.in_sync_hint;
635
636 return (int)pkg.is_recovering;
637}
638
639static 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,
658};
659
660static int __init userspace_dirty_log_init(void)
661{
662 int r = 0;
663
664 flush_entry_pool = mempool_create(100, flush_entry_alloc,
665 flush_entry_free, NULL);
666
667 if (!flush_entry_pool) {
668 DMWARN("Unable to create flush_entry_pool: No memory.");
669 return -ENOMEM;
670 }
671
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;
677 }
678
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;
685 }
686
687 DMINFO("version 1.0.0 loaded");
688 return 0;
689}
690
691static void __exit userspace_dirty_log_exit(void)
692{
693 dm_dirty_log_type_unregister(&_userspace_type);
694 dm_ulog_tfr_exit();
695 mempool_destroy(flush_entry_pool);
696
697 DMINFO("version 1.0.0 unloaded");
698 return;
699}
700
701module_init(userspace_dirty_log_init);
702module_exit(userspace_dirty_log_exit);
703
704MODULE_DESCRIPTION(DM_NAME " userspace dirty log link");
705MODULE_AUTHOR("Jonathan Brassow <dm-devel@redhat.com>");
706MODULE_LICENSE("GPL");