1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2006-2009 Red Hat, Inc.
4  *
5  * This file is released under the LGPL.
6  */
7 
8 #include <linux/bio.h>
9 #include <linux/slab.h>
10 #include <linux/jiffies.h>
11 #include <linux/dm-dirty-log.h>
12 #include <linux/device-mapper.h>
13 #include <linux/dm-log-userspace.h>
14 #include <linux/module.h>
15 #include <linux/workqueue.h>
16 
17 #include "dm-log-userspace-transfer.h"
18 
19 #define DM_LOG_USERSPACE_VSN "1.3.0"
20 
21 #define FLUSH_ENTRY_POOL_SIZE 16
22 
23 struct dm_dirty_log_flush_entry {
24 	int type;
25 	region_t region;
26 	struct list_head list;
27 };
28 
29 /*
30  * This limit on the number of mark and clear request is, to a degree,
31  * arbitrary.  However, there is some basis for the choice in the limits
32  * imposed on the size of data payload by dm-log-userspace-transfer.c:
33  * dm_consult_userspace().
34  */
35 #define MAX_FLUSH_GROUP_COUNT 32
36 
37 struct log_c {
38 	struct dm_target *ti;
39 	struct dm_dev *log_dev;
40 
41 	char *usr_argv_str;
42 	uint32_t usr_argc;
43 
44 	uint32_t region_size;
45 	region_t region_count;
46 	uint64_t luid;
47 	char uuid[DM_UUID_LEN];
48 
49 	/*
50 	 * Mark and clear requests are held until a flush is issued
51 	 * so that we can group, and thereby limit, the amount of
52 	 * network traffic between kernel and userspace.  The 'flush_lock'
53 	 * is used to protect these lists.
54 	 */
55 	spinlock_t flush_lock;
56 	struct list_head mark_list;
57 	struct list_head clear_list;
58 
59 	/*
60 	 * in_sync_hint gets set when doing is_remote_recovering.  It
61 	 * represents the first region that needs recovery.  IOW, the
62 	 * first zero bit of sync_bits.  This can be useful for to limit
63 	 * traffic for calls like is_remote_recovering and get_resync_work,
64 	 * but be take care in its use for anything else.
65 	 */
66 	uint64_t in_sync_hint;
67 
68 	/*
69 	 * Workqueue for flush of clear region requests.
70 	 */
71 	struct workqueue_struct *dmlog_wq;
72 	struct delayed_work flush_log_work;
73 	atomic_t sched_flush;
74 
75 	/*
76 	 * Combine userspace flush and mark requests for efficiency.
77 	 */
78 	uint32_t integrated_flush;
79 
80 	mempool_t flush_entry_pool;
81 };
82 
83 static struct kmem_cache *_flush_entry_cache;
84 
85 static int userspace_do_request(struct log_c *lc, const char *uuid,
86 				int request_type, char *data, size_t data_size,
87 				char *rdata, size_t *rdata_size)
88 {
89 	int r;
90 
91 	/*
92 	 * If the server isn't there, -ESRCH is returned,
93 	 * and we must keep trying until the server is
94 	 * restored.
95 	 */
96 retry:
97 	r = dm_consult_userspace(uuid, lc->luid, request_type, data,
98 				 data_size, rdata, rdata_size);
99 
100 	if (r != -ESRCH)
101 		return r;
102 
103 	DMERR(" Userspace log server not found.");
104 	while (1) {
105 		set_current_state(TASK_INTERRUPTIBLE);
106 		schedule_timeout(2*HZ);
107 		DMWARN("Attempting to contact userspace log server...");
108 		r = dm_consult_userspace(uuid, lc->luid, DM_ULOG_CTR,
109 					 lc->usr_argv_str,
110 					 strlen(lc->usr_argv_str) + 1,
111 					 NULL, NULL);
112 		if (!r)
113 			break;
114 	}
115 	DMINFO("Reconnected to userspace log server... DM_ULOG_CTR complete");
116 	r = dm_consult_userspace(uuid, lc->luid, DM_ULOG_RESUME, NULL,
117 				 0, NULL, NULL);
118 	if (!r)
119 		goto retry;
120 
121 	DMERR("Error trying to resume userspace log: %d", r);
122 
123 	return -ESRCH;
124 }
125 
126 static int build_constructor_string(struct dm_target *ti,
127 				    unsigned argc, char **argv,
128 				    char **ctr_str)
129 {
130 	int i, str_size;
131 	char *str = NULL;
132 
133 	*ctr_str = NULL;
134 
135 	/*
136 	 * Determine overall size of the string.
137 	 */
138 	for (i = 0, str_size = 0; i < argc; i++)
139 		str_size += strlen(argv[i]) + 1; /* +1 for space between args */
140 
141 	str_size += 20; /* Max number of chars in a printed u64 number */
142 
143 	str = kzalloc(str_size, GFP_KERNEL);
144 	if (!str) {
145 		DMWARN("Unable to allocate memory for constructor string");
146 		return -ENOMEM;
147 	}
148 
149 	str_size = sprintf(str, "%llu", (unsigned long long)ti->len);
150 	for (i = 0; i < argc; i++)
151 		str_size += sprintf(str + str_size, " %s", argv[i]);
152 
153 	*ctr_str = str;
154 	return str_size;
155 }
156 
157 static void do_flush(struct work_struct *work)
158 {
159 	int r;
160 	struct log_c *lc = container_of(work, struct log_c, flush_log_work.work);
161 
162 	atomic_set(&lc->sched_flush, 0);
163 
164 	r = userspace_do_request(lc, lc->uuid, DM_ULOG_FLUSH, NULL, 0, NULL, NULL);
165 
166 	if (r)
167 		dm_table_event(lc->ti->table);
168 }
169 
170 /*
171  * userspace_ctr
172  *
173  * argv contains:
174  *	<UUID> [integrated_flush] <other args>
175  * Where 'other args' are the userspace implementation-specific log
176  * arguments.
177  *
178  * Example:
179  *	<UUID> [integrated_flush] clustered-disk <arg count> <log dev>
180  *	<region_size> [[no]sync]
181  *
182  * This module strips off the <UUID> and uses it for identification
183  * purposes when communicating with userspace about a log.
184  *
185  * If integrated_flush is defined, the kernel combines flush
186  * and mark requests.
187  *
188  * The rest of the line, beginning with 'clustered-disk', is passed
189  * to the userspace ctr function.
190  */
191 static int userspace_ctr(struct dm_dirty_log *log, struct dm_target *ti,
192 			 unsigned argc, char **argv)
193 {
194 	int r = 0;
195 	int str_size;
196 	char *ctr_str = NULL;
197 	struct log_c *lc = NULL;
198 	uint64_t rdata;
199 	size_t rdata_size = sizeof(rdata);
200 	char *devices_rdata = NULL;
201 	size_t devices_rdata_size = DM_NAME_LEN;
202 
203 	if (argc < 3) {
204 		DMWARN("Too few arguments to userspace dirty log");
205 		return -EINVAL;
206 	}
207 
208 	lc = kzalloc(sizeof(*lc), GFP_KERNEL);
209 	if (!lc) {
210 		DMWARN("Unable to allocate userspace log context.");
211 		return -ENOMEM;
212 	}
213 
214 	/* The ptr value is sufficient for local unique id */
215 	lc->luid = (unsigned long)lc;
216 
217 	lc->ti = ti;
218 
219 	if (strlen(argv[0]) > (DM_UUID_LEN - 1)) {
220 		DMWARN("UUID argument too long.");
221 		kfree(lc);
222 		return -EINVAL;
223 	}
224 
225 	lc->usr_argc = argc;
226 
227 	strncpy(lc->uuid, argv[0], DM_UUID_LEN);
228 	argc--;
229 	argv++;
230 	spin_lock_init(&lc->flush_lock);
231 	INIT_LIST_HEAD(&lc->mark_list);
232 	INIT_LIST_HEAD(&lc->clear_list);
233 
234 	if (!strcasecmp(argv[0], "integrated_flush")) {
235 		lc->integrated_flush = 1;
236 		argc--;
237 		argv++;
238 	}
239 
240 	str_size = build_constructor_string(ti, argc, argv, &ctr_str);
241 	if (str_size < 0) {
242 		kfree(lc);
243 		return str_size;
244 	}
245 
246 	devices_rdata = kzalloc(devices_rdata_size, GFP_KERNEL);
247 	if (!devices_rdata) {
248 		DMERR("Failed to allocate memory for device information");
249 		r = -ENOMEM;
250 		goto out;
251 	}
252 
253 	r = mempool_init_slab_pool(&lc->flush_entry_pool, FLUSH_ENTRY_POOL_SIZE,
254 				   _flush_entry_cache);
255 	if (r) {
256 		DMERR("Failed to create flush_entry_pool");
257 		goto out;
258 	}
259 
260 	/*
261 	 * Send table string and get back any opened device.
262 	 */
263 	r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_CTR,
264 				 ctr_str, str_size,
265 				 devices_rdata, &devices_rdata_size);
266 
267 	if (r < 0) {
268 		if (r == -ESRCH)
269 			DMERR("Userspace log server not found");
270 		else
271 			DMERR("Userspace log server failed to create log");
272 		goto out;
273 	}
274 
275 	/* Since the region size does not change, get it now */
276 	rdata_size = sizeof(rdata);
277 	r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_GET_REGION_SIZE,
278 				 NULL, 0, (char *)&rdata, &rdata_size);
279 
280 	if (r) {
281 		DMERR("Failed to get region size of dirty log");
282 		goto out;
283 	}
284 
285 	lc->region_size = (uint32_t)rdata;
286 	lc->region_count = dm_sector_div_up(ti->len, lc->region_size);
287 
288 	if (devices_rdata_size) {
289 		if (devices_rdata[devices_rdata_size - 1] != '\0') {
290 			DMERR("DM_ULOG_CTR device return string not properly terminated");
291 			r = -EINVAL;
292 			goto out;
293 		}
294 		r = dm_get_device(ti, devices_rdata,
295 				  dm_table_get_mode(ti->table), &lc->log_dev);
296 		if (r)
297 			DMERR("Failed to register %s with device-mapper",
298 			      devices_rdata);
299 	}
300 
301 	if (lc->integrated_flush) {
302 		lc->dmlog_wq = alloc_workqueue("dmlogd", WQ_MEM_RECLAIM, 0);
303 		if (!lc->dmlog_wq) {
304 			DMERR("couldn't start dmlogd");
305 			r = -ENOMEM;
306 			goto out;
307 		}
308 
309 		INIT_DELAYED_WORK(&lc->flush_log_work, do_flush);
310 		atomic_set(&lc->sched_flush, 0);
311 	}
312 
313 out:
314 	kfree(devices_rdata);
315 	if (r) {
316 		mempool_exit(&lc->flush_entry_pool);
317 		kfree(lc);
318 		kfree(ctr_str);
319 	} else {
320 		lc->usr_argv_str = ctr_str;
321 		log->context = lc;
322 	}
323 
324 	return r;
325 }
326 
327 static void userspace_dtr(struct dm_dirty_log *log)
328 {
329 	struct log_c *lc = log->context;
330 
331 	if (lc->integrated_flush) {
332 		/* flush workqueue */
333 		if (atomic_read(&lc->sched_flush))
334 			flush_delayed_work(&lc->flush_log_work);
335 
336 		destroy_workqueue(lc->dmlog_wq);
337 	}
338 
339 	(void) dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_DTR,
340 				    NULL, 0, NULL, NULL);
341 
342 	if (lc->log_dev)
343 		dm_put_device(lc->ti, lc->log_dev);
344 
345 	mempool_exit(&lc->flush_entry_pool);
346 
347 	kfree(lc->usr_argv_str);
348 	kfree(lc);
349 
350 	return;
351 }
352 
353 static int userspace_presuspend(struct dm_dirty_log *log)
354 {
355 	int r;
356 	struct log_c *lc = log->context;
357 
358 	r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_PRESUSPEND,
359 				 NULL, 0, NULL, NULL);
360 
361 	return r;
362 }
363 
364 static int userspace_postsuspend(struct dm_dirty_log *log)
365 {
366 	int r;
367 	struct log_c *lc = log->context;
368 
369 	/*
370 	 * Run planned flush earlier.
371 	 */
372 	if (lc->integrated_flush && atomic_read(&lc->sched_flush))
373 		flush_delayed_work(&lc->flush_log_work);
374 
375 	r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_POSTSUSPEND,
376 				 NULL, 0, NULL, NULL);
377 
378 	return r;
379 }
380 
381 static int userspace_resume(struct dm_dirty_log *log)
382 {
383 	int r;
384 	struct log_c *lc = log->context;
385 
386 	lc->in_sync_hint = 0;
387 	r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_RESUME,
388 				 NULL, 0, NULL, NULL);
389 
390 	return r;
391 }
392 
393 static uint32_t userspace_get_region_size(struct dm_dirty_log *log)
394 {
395 	struct log_c *lc = log->context;
396 
397 	return lc->region_size;
398 }
399 
400 /*
401  * userspace_is_clean
402  *
403  * Check whether a region is clean.  If there is any sort of
404  * failure when consulting the server, we return not clean.
405  *
406  * Returns: 1 if clean, 0 otherwise
407  */
408 static int userspace_is_clean(struct dm_dirty_log *log, region_t region)
409 {
410 	int r;
411 	uint64_t region64 = (uint64_t)region;
412 	int64_t is_clean;
413 	size_t rdata_size;
414 	struct log_c *lc = log->context;
415 
416 	rdata_size = sizeof(is_clean);
417 	r = userspace_do_request(lc, lc->uuid, DM_ULOG_IS_CLEAN,
418 				 (char *)&region64, sizeof(region64),
419 				 (char *)&is_clean, &rdata_size);
420 
421 	return (r) ? 0 : (int)is_clean;
422 }
423 
424 /*
425  * userspace_in_sync
426  *
427  * Check if the region is in-sync.  If there is any sort
428  * of failure when consulting the server, we assume that
429  * the region is not in sync.
430  *
431  * If 'can_block' is set, return immediately
432  *
433  * Returns: 1 if in-sync, 0 if not-in-sync, -EWOULDBLOCK
434  */
435 static int userspace_in_sync(struct dm_dirty_log *log, region_t region,
436 			     int can_block)
437 {
438 	int r;
439 	uint64_t region64 = region;
440 	int64_t in_sync;
441 	size_t rdata_size;
442 	struct log_c *lc = log->context;
443 
444 	/*
445 	 * We can never respond directly - even if in_sync_hint is
446 	 * set.  This is because another machine could see a device
447 	 * failure and mark the region out-of-sync.  If we don't go
448 	 * to userspace to ask, we might think the region is in-sync
449 	 * and allow a read to pick up data that is stale.  (This is
450 	 * very unlikely if a device actually fails; but it is very
451 	 * likely if a connection to one device from one machine fails.)
452 	 *
453 	 * There still might be a problem if the mirror caches the region
454 	 * state as in-sync... but then this call would not be made.  So,
455 	 * that is a mirror problem.
456 	 */
457 	if (!can_block)
458 		return -EWOULDBLOCK;
459 
460 	rdata_size = sizeof(in_sync);
461 	r = userspace_do_request(lc, lc->uuid, DM_ULOG_IN_SYNC,
462 				 (char *)&region64, sizeof(region64),
463 				 (char *)&in_sync, &rdata_size);
464 	return (r) ? 0 : (int)in_sync;
465 }
466 
467 static int flush_one_by_one(struct log_c *lc, struct list_head *flush_list)
468 {
469 	int r = 0;
470 	struct dm_dirty_log_flush_entry *fe;
471 
472 	list_for_each_entry(fe, flush_list, list) {
473 		r = userspace_do_request(lc, lc->uuid, fe->type,
474 					 (char *)&fe->region,
475 					 sizeof(fe->region),
476 					 NULL, NULL);
477 		if (r)
478 			break;
479 	}
480 
481 	return r;
482 }
483 
484 static int flush_by_group(struct log_c *lc, struct list_head *flush_list,
485 			  int flush_with_payload)
486 {
487 	int r = 0;
488 	int count;
489 	uint32_t type = 0;
490 	struct dm_dirty_log_flush_entry *fe, *tmp_fe;
491 	LIST_HEAD(tmp_list);
492 	uint64_t group[MAX_FLUSH_GROUP_COUNT];
493 
494 	/*
495 	 * Group process the requests
496 	 */
497 	while (!list_empty(flush_list)) {
498 		count = 0;
499 
500 		list_for_each_entry_safe(fe, tmp_fe, flush_list, list) {
501 			group[count] = fe->region;
502 			count++;
503 
504 			list_move(&fe->list, &tmp_list);
505 
506 			type = fe->type;
507 			if (count >= MAX_FLUSH_GROUP_COUNT)
508 				break;
509 		}
510 
511 		if (flush_with_payload) {
512 			r = userspace_do_request(lc, lc->uuid, DM_ULOG_FLUSH,
513 						 (char *)(group),
514 						 count * sizeof(uint64_t),
515 						 NULL, NULL);
516 			/*
517 			 * Integrated flush failed.
518 			 */
519 			if (r)
520 				break;
521 		} else {
522 			r = userspace_do_request(lc, lc->uuid, type,
523 						 (char *)(group),
524 						 count * sizeof(uint64_t),
525 						 NULL, NULL);
526 			if (r) {
527 				/*
528 				 * Group send failed.  Attempt one-by-one.
529 				 */
530 				list_splice_init(&tmp_list, flush_list);
531 				r = flush_one_by_one(lc, flush_list);
532 				break;
533 			}
534 		}
535 	}
536 
537 	/*
538 	 * Must collect flush_entrys that were successfully processed
539 	 * as a group so that they will be free'd by the caller.
540 	 */
541 	list_splice_init(&tmp_list, flush_list);
542 
543 	return r;
544 }
545 
546 /*
547  * userspace_flush
548  *
549  * This function is ok to block.
550  * The flush happens in two stages.  First, it sends all
551  * clear/mark requests that are on the list.  Then it
552  * tells the server to commit them.  This gives the
553  * server a chance to optimise the commit, instead of
554  * doing it for every request.
555  *
556  * Additionally, we could implement another thread that
557  * sends the requests up to the server - reducing the
558  * load on flush.  Then the flush would have less in
559  * the list and be responsible for the finishing commit.
560  *
561  * Returns: 0 on success, < 0 on failure
562  */
563 static int userspace_flush(struct dm_dirty_log *log)
564 {
565 	int r = 0;
566 	unsigned long flags;
567 	struct log_c *lc = log->context;
568 	LIST_HEAD(mark_list);
569 	LIST_HEAD(clear_list);
570 	int mark_list_is_empty;
571 	int clear_list_is_empty;
572 	struct dm_dirty_log_flush_entry *fe, *tmp_fe;
573 	mempool_t *flush_entry_pool = &lc->flush_entry_pool;
574 
575 	spin_lock_irqsave(&lc->flush_lock, flags);
576 	list_splice_init(&lc->mark_list, &mark_list);
577 	list_splice_init(&lc->clear_list, &clear_list);
578 	spin_unlock_irqrestore(&lc->flush_lock, flags);
579 
580 	mark_list_is_empty = list_empty(&mark_list);
581 	clear_list_is_empty = list_empty(&clear_list);
582 
583 	if (mark_list_is_empty && clear_list_is_empty)
584 		return 0;
585 
586 	r = flush_by_group(lc, &clear_list, 0);
587 	if (r)
588 		goto out;
589 
590 	if (!lc->integrated_flush) {
591 		r = flush_by_group(lc, &mark_list, 0);
592 		if (r)
593 			goto out;
594 		r = userspace_do_request(lc, lc->uuid, DM_ULOG_FLUSH,
595 					 NULL, 0, NULL, NULL);
596 		goto out;
597 	}
598 
599 	/*
600 	 * Send integrated flush request with mark_list as payload.
601 	 */
602 	r = flush_by_group(lc, &mark_list, 1);
603 	if (r)
604 		goto out;
605 
606 	if (mark_list_is_empty && !atomic_read(&lc->sched_flush)) {
607 		/*
608 		 * When there are only clear region requests,
609 		 * we schedule a flush in the future.
610 		 */
611 		queue_delayed_work(lc->dmlog_wq, &lc->flush_log_work, 3 * HZ);
612 		atomic_set(&lc->sched_flush, 1);
613 	} else {
614 		/*
615 		 * Cancel pending flush because we
616 		 * have already flushed in mark_region.
617 		 */
618 		cancel_delayed_work(&lc->flush_log_work);
619 		atomic_set(&lc->sched_flush, 0);
620 	}
621 
622 out:
623 	/*
624 	 * We can safely remove these entries, even after failure.
625 	 * Calling code will receive an error and will know that
626 	 * the log facility has failed.
627 	 */
628 	list_for_each_entry_safe(fe, tmp_fe, &mark_list, list) {
629 		list_del(&fe->list);
630 		mempool_free(fe, flush_entry_pool);
631 	}
632 	list_for_each_entry_safe(fe, tmp_fe, &clear_list, list) {
633 		list_del(&fe->list);
634 		mempool_free(fe, flush_entry_pool);
635 	}
636 
637 	if (r)
638 		dm_table_event(lc->ti->table);
639 
640 	return r;
641 }
642 
643 /*
644  * userspace_mark_region
645  *
646  * This function should avoid blocking unless absolutely required.
647  * (Memory allocation is valid for blocking.)
648  */
649 static void userspace_mark_region(struct dm_dirty_log *log, region_t region)
650 {
651 	unsigned long flags;
652 	struct log_c *lc = log->context;
653 	struct dm_dirty_log_flush_entry *fe;
654 
655 	/* Wait for an allocation, but _never_ fail */
656 	fe = mempool_alloc(&lc->flush_entry_pool, GFP_NOIO);
657 	BUG_ON(!fe);
658 
659 	spin_lock_irqsave(&lc->flush_lock, flags);
660 	fe->type = DM_ULOG_MARK_REGION;
661 	fe->region = region;
662 	list_add(&fe->list, &lc->mark_list);
663 	spin_unlock_irqrestore(&lc->flush_lock, flags);
664 
665 	return;
666 }
667 
668 /*
669  * userspace_clear_region
670  *
671  * This function must not block.
672  * So, the alloc can't block.  In the worst case, it is ok to
673  * fail.  It would simply mean we can't clear the region.
674  * Does nothing to current sync context, but does mean
675  * the region will be re-sync'ed on a reload of the mirror
676  * even though it is in-sync.
677  */
678 static void userspace_clear_region(struct dm_dirty_log *log, region_t region)
679 {
680 	unsigned long flags;
681 	struct log_c *lc = log->context;
682 	struct dm_dirty_log_flush_entry *fe;
683 
684 	/*
685 	 * If we fail to allocate, we skip the clearing of
686 	 * the region.  This doesn't hurt us in any way, except
687 	 * to cause the region to be resync'ed when the
688 	 * device is activated next time.
689 	 */
690 	fe = mempool_alloc(&lc->flush_entry_pool, GFP_ATOMIC);
691 	if (!fe) {
692 		DMERR("Failed to allocate memory to clear region.");
693 		return;
694 	}
695 
696 	spin_lock_irqsave(&lc->flush_lock, flags);
697 	fe->type = DM_ULOG_CLEAR_REGION;
698 	fe->region = region;
699 	list_add(&fe->list, &lc->clear_list);
700 	spin_unlock_irqrestore(&lc->flush_lock, flags);
701 
702 	return;
703 }
704 
705 /*
706  * userspace_get_resync_work
707  *
708  * Get a region that needs recovery.  It is valid to return
709  * an error for this function.
710  *
711  * Returns: 1 if region filled, 0 if no work, <0 on error
712  */
713 static int userspace_get_resync_work(struct dm_dirty_log *log, region_t *region)
714 {
715 	int r;
716 	size_t rdata_size;
717 	struct log_c *lc = log->context;
718 	struct {
719 		int64_t i; /* 64-bit for mix arch compatibility */
720 		region_t r;
721 	} pkg;
722 
723 	if (lc->in_sync_hint >= lc->region_count)
724 		return 0;
725 
726 	rdata_size = sizeof(pkg);
727 	r = userspace_do_request(lc, lc->uuid, DM_ULOG_GET_RESYNC_WORK,
728 				 NULL, 0, (char *)&pkg, &rdata_size);
729 
730 	*region = pkg.r;
731 	return (r) ? r : (int)pkg.i;
732 }
733 
734 /*
735  * userspace_set_region_sync
736  *
737  * Set the sync status of a given region.  This function
738  * must not fail.
739  */
740 static void userspace_set_region_sync(struct dm_dirty_log *log,
741 				      region_t region, int in_sync)
742 {
743 	struct log_c *lc = log->context;
744 	struct {
745 		region_t r;
746 		int64_t i;
747 	} pkg;
748 
749 	pkg.r = region;
750 	pkg.i = (int64_t)in_sync;
751 
752 	(void) userspace_do_request(lc, lc->uuid, DM_ULOG_SET_REGION_SYNC,
753 				    (char *)&pkg, sizeof(pkg), NULL, NULL);
754 
755 	/*
756 	 * It would be nice to be able to report failures.
757 	 * However, it is easy enough to detect and resolve.
758 	 */
759 	return;
760 }
761 
762 /*
763  * userspace_get_sync_count
764  *
765  * If there is any sort of failure when consulting the server,
766  * we assume that the sync count is zero.
767  *
768  * Returns: sync count on success, 0 on failure
769  */
770 static region_t userspace_get_sync_count(struct dm_dirty_log *log)
771 {
772 	int r;
773 	size_t rdata_size;
774 	uint64_t sync_count;
775 	struct log_c *lc = log->context;
776 
777 	rdata_size = sizeof(sync_count);
778 	r = userspace_do_request(lc, lc->uuid, DM_ULOG_GET_SYNC_COUNT,
779 				 NULL, 0, (char *)&sync_count, &rdata_size);
780 
781 	if (r)
782 		return 0;
783 
784 	if (sync_count >= lc->region_count)
785 		lc->in_sync_hint = lc->region_count;
786 
787 	return (region_t)sync_count;
788 }
789 
790 /*
791  * userspace_status
792  *
793  * Returns: amount of space consumed
794  */
795 static int userspace_status(struct dm_dirty_log *log, status_type_t status_type,
796 			    char *result, unsigned maxlen)
797 {
798 	int r = 0;
799 	char *table_args;
800 	size_t sz = (size_t)maxlen;
801 	struct log_c *lc = log->context;
802 
803 	switch (status_type) {
804 	case STATUSTYPE_INFO:
805 		r = userspace_do_request(lc, lc->uuid, DM_ULOG_STATUS_INFO,
806 					 NULL, 0, result, &sz);
807 
808 		if (r) {
809 			sz = 0;
810 			DMEMIT("%s 1 COM_FAILURE", log->type->name);
811 		}
812 		break;
813 	case STATUSTYPE_TABLE:
814 		sz = 0;
815 		table_args = strchr(lc->usr_argv_str, ' ');
816 		BUG_ON(!table_args); /* There will always be a ' ' */
817 		table_args++;
818 
819 		DMEMIT("%s %u %s ", log->type->name, lc->usr_argc, lc->uuid);
820 		if (lc->integrated_flush)
821 			DMEMIT("integrated_flush ");
822 		DMEMIT("%s ", table_args);
823 		break;
824 	case STATUSTYPE_IMA:
825 		*result = '\0';
826 		break;
827 	}
828 	return (r) ? 0 : (int)sz;
829 }
830 
831 /*
832  * userspace_is_remote_recovering
833  *
834  * Returns: 1 if region recovering, 0 otherwise
835  */
836 static int userspace_is_remote_recovering(struct dm_dirty_log *log,
837 					  region_t region)
838 {
839 	int r;
840 	uint64_t region64 = region;
841 	struct log_c *lc = log->context;
842 	static unsigned long limit;
843 	struct {
844 		int64_t is_recovering;
845 		uint64_t in_sync_hint;
846 	} pkg;
847 	size_t rdata_size = sizeof(pkg);
848 
849 	/*
850 	 * Once the mirror has been reported to be in-sync,
851 	 * it will never again ask for recovery work.  So,
852 	 * we can safely say there is not a remote machine
853 	 * recovering if the device is in-sync.  (in_sync_hint
854 	 * must be reset at resume time.)
855 	 */
856 	if (region < lc->in_sync_hint)
857 		return 0;
858 	else if (time_after(limit, jiffies))
859 		return 1;
860 
861 	limit = jiffies + (HZ / 4);
862 	r = userspace_do_request(lc, lc->uuid, DM_ULOG_IS_REMOTE_RECOVERING,
863 				 (char *)&region64, sizeof(region64),
864 				 (char *)&pkg, &rdata_size);
865 	if (r)
866 		return 1;
867 
868 	lc->in_sync_hint = pkg.in_sync_hint;
869 
870 	return (int)pkg.is_recovering;
871 }
872 
873 static struct dm_dirty_log_type _userspace_type = {
874 	.name = "userspace",
875 	.module = THIS_MODULE,
876 	.ctr = userspace_ctr,
877 	.dtr = userspace_dtr,
878 	.presuspend = userspace_presuspend,
879 	.postsuspend = userspace_postsuspend,
880 	.resume = userspace_resume,
881 	.get_region_size = userspace_get_region_size,
882 	.is_clean = userspace_is_clean,
883 	.in_sync = userspace_in_sync,
884 	.flush = userspace_flush,
885 	.mark_region = userspace_mark_region,
886 	.clear_region = userspace_clear_region,
887 	.get_resync_work = userspace_get_resync_work,
888 	.set_region_sync = userspace_set_region_sync,
889 	.get_sync_count = userspace_get_sync_count,
890 	.status = userspace_status,
891 	.is_remote_recovering = userspace_is_remote_recovering,
892 };
893 
894 static int __init userspace_dirty_log_init(void)
895 {
896 	int r = 0;
897 
898 	_flush_entry_cache = KMEM_CACHE(dm_dirty_log_flush_entry, 0);
899 	if (!_flush_entry_cache) {
900 		DMWARN("Unable to create flush_entry_cache: No memory.");
901 		return -ENOMEM;
902 	}
903 
904 	r = dm_ulog_tfr_init();
905 	if (r) {
906 		DMWARN("Unable to initialize userspace log communications");
907 		kmem_cache_destroy(_flush_entry_cache);
908 		return r;
909 	}
910 
911 	r = dm_dirty_log_type_register(&_userspace_type);
912 	if (r) {
913 		DMWARN("Couldn't register userspace dirty log type");
914 		dm_ulog_tfr_exit();
915 		kmem_cache_destroy(_flush_entry_cache);
916 		return r;
917 	}
918 
919 	DMINFO("version " DM_LOG_USERSPACE_VSN " loaded");
920 	return 0;
921 }
922 
923 static void __exit userspace_dirty_log_exit(void)
924 {
925 	dm_dirty_log_type_unregister(&_userspace_type);
926 	dm_ulog_tfr_exit();
927 	kmem_cache_destroy(_flush_entry_cache);
928 
929 	DMINFO("version " DM_LOG_USERSPACE_VSN " unloaded");
930 	return;
931 }
932 
933 module_init(userspace_dirty_log_init);
934 module_exit(userspace_dirty_log_exit);
935 
936 MODULE_DESCRIPTION(DM_NAME " userspace dirty log link");
937 MODULE_AUTHOR("Jonathan Brassow <dm-devel@redhat.com>");
938 MODULE_LICENSE("GPL");
939