18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (C) 2006-2009 Red Hat, Inc.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * This file is released under the LGPL.
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/bio.h>
88c2ecf20Sopenharmony_ci#include <linux/slab.h>
98c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
108c2ecf20Sopenharmony_ci#include <linux/dm-dirty-log.h>
118c2ecf20Sopenharmony_ci#include <linux/device-mapper.h>
128c2ecf20Sopenharmony_ci#include <linux/dm-log-userspace.h>
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include "dm-log-userspace-transfer.h"
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#define DM_LOG_USERSPACE_VSN "1.3.0"
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#define FLUSH_ENTRY_POOL_SIZE 16
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistruct dm_dirty_log_flush_entry {
238c2ecf20Sopenharmony_ci	int type;
248c2ecf20Sopenharmony_ci	region_t region;
258c2ecf20Sopenharmony_ci	struct list_head list;
268c2ecf20Sopenharmony_ci};
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/*
298c2ecf20Sopenharmony_ci * This limit on the number of mark and clear request is, to a degree,
308c2ecf20Sopenharmony_ci * arbitrary.  However, there is some basis for the choice in the limits
318c2ecf20Sopenharmony_ci * imposed on the size of data payload by dm-log-userspace-transfer.c:
328c2ecf20Sopenharmony_ci * dm_consult_userspace().
338c2ecf20Sopenharmony_ci */
348c2ecf20Sopenharmony_ci#define MAX_FLUSH_GROUP_COUNT 32
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_cistruct log_c {
378c2ecf20Sopenharmony_ci	struct dm_target *ti;
388c2ecf20Sopenharmony_ci	struct dm_dev *log_dev;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	char *usr_argv_str;
418c2ecf20Sopenharmony_ci	uint32_t usr_argc;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	uint32_t region_size;
448c2ecf20Sopenharmony_ci	region_t region_count;
458c2ecf20Sopenharmony_ci	uint64_t luid;
468c2ecf20Sopenharmony_ci	char uuid[DM_UUID_LEN];
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	/*
498c2ecf20Sopenharmony_ci	 * Mark and clear requests are held until a flush is issued
508c2ecf20Sopenharmony_ci	 * so that we can group, and thereby limit, the amount of
518c2ecf20Sopenharmony_ci	 * network traffic between kernel and userspace.  The 'flush_lock'
528c2ecf20Sopenharmony_ci	 * is used to protect these lists.
538c2ecf20Sopenharmony_ci	 */
548c2ecf20Sopenharmony_ci	spinlock_t flush_lock;
558c2ecf20Sopenharmony_ci	struct list_head mark_list;
568c2ecf20Sopenharmony_ci	struct list_head clear_list;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	/*
598c2ecf20Sopenharmony_ci	 * in_sync_hint gets set when doing is_remote_recovering.  It
608c2ecf20Sopenharmony_ci	 * represents the first region that needs recovery.  IOW, the
618c2ecf20Sopenharmony_ci	 * first zero bit of sync_bits.  This can be useful for to limit
628c2ecf20Sopenharmony_ci	 * traffic for calls like is_remote_recovering and get_resync_work,
638c2ecf20Sopenharmony_ci	 * but be take care in its use for anything else.
648c2ecf20Sopenharmony_ci	 */
658c2ecf20Sopenharmony_ci	uint64_t in_sync_hint;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	/*
688c2ecf20Sopenharmony_ci	 * Workqueue for flush of clear region requests.
698c2ecf20Sopenharmony_ci	 */
708c2ecf20Sopenharmony_ci	struct workqueue_struct *dmlog_wq;
718c2ecf20Sopenharmony_ci	struct delayed_work flush_log_work;
728c2ecf20Sopenharmony_ci	atomic_t sched_flush;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	/*
758c2ecf20Sopenharmony_ci	 * Combine userspace flush and mark requests for efficiency.
768c2ecf20Sopenharmony_ci	 */
778c2ecf20Sopenharmony_ci	uint32_t integrated_flush;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	mempool_t flush_entry_pool;
808c2ecf20Sopenharmony_ci};
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistatic struct kmem_cache *_flush_entry_cache;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_cistatic int userspace_do_request(struct log_c *lc, const char *uuid,
858c2ecf20Sopenharmony_ci				int request_type, char *data, size_t data_size,
868c2ecf20Sopenharmony_ci				char *rdata, size_t *rdata_size)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	int r;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	/*
918c2ecf20Sopenharmony_ci	 * If the server isn't there, -ESRCH is returned,
928c2ecf20Sopenharmony_ci	 * and we must keep trying until the server is
938c2ecf20Sopenharmony_ci	 * restored.
948c2ecf20Sopenharmony_ci	 */
958c2ecf20Sopenharmony_ciretry:
968c2ecf20Sopenharmony_ci	r = dm_consult_userspace(uuid, lc->luid, request_type, data,
978c2ecf20Sopenharmony_ci				 data_size, rdata, rdata_size);
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	if (r != -ESRCH)
1008c2ecf20Sopenharmony_ci		return r;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	DMERR(" Userspace log server not found.");
1038c2ecf20Sopenharmony_ci	while (1) {
1048c2ecf20Sopenharmony_ci		set_current_state(TASK_INTERRUPTIBLE);
1058c2ecf20Sopenharmony_ci		schedule_timeout(2*HZ);
1068c2ecf20Sopenharmony_ci		DMWARN("Attempting to contact userspace log server...");
1078c2ecf20Sopenharmony_ci		r = dm_consult_userspace(uuid, lc->luid, DM_ULOG_CTR,
1088c2ecf20Sopenharmony_ci					 lc->usr_argv_str,
1098c2ecf20Sopenharmony_ci					 strlen(lc->usr_argv_str) + 1,
1108c2ecf20Sopenharmony_ci					 NULL, NULL);
1118c2ecf20Sopenharmony_ci		if (!r)
1128c2ecf20Sopenharmony_ci			break;
1138c2ecf20Sopenharmony_ci	}
1148c2ecf20Sopenharmony_ci	DMINFO("Reconnected to userspace log server... DM_ULOG_CTR complete");
1158c2ecf20Sopenharmony_ci	r = dm_consult_userspace(uuid, lc->luid, DM_ULOG_RESUME, NULL,
1168c2ecf20Sopenharmony_ci				 0, NULL, NULL);
1178c2ecf20Sopenharmony_ci	if (!r)
1188c2ecf20Sopenharmony_ci		goto retry;
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	DMERR("Error trying to resume userspace log: %d", r);
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	return -ESRCH;
1238c2ecf20Sopenharmony_ci}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_cistatic int build_constructor_string(struct dm_target *ti,
1268c2ecf20Sopenharmony_ci				    unsigned argc, char **argv,
1278c2ecf20Sopenharmony_ci				    char **ctr_str)
1288c2ecf20Sopenharmony_ci{
1298c2ecf20Sopenharmony_ci	int i, str_size;
1308c2ecf20Sopenharmony_ci	char *str = NULL;
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	*ctr_str = NULL;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	/*
1358c2ecf20Sopenharmony_ci	 * Determine overall size of the string.
1368c2ecf20Sopenharmony_ci	 */
1378c2ecf20Sopenharmony_ci	for (i = 0, str_size = 0; i < argc; i++)
1388c2ecf20Sopenharmony_ci		str_size += strlen(argv[i]) + 1; /* +1 for space between args */
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	str_size += 20; /* Max number of chars in a printed u64 number */
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	str = kzalloc(str_size, GFP_KERNEL);
1438c2ecf20Sopenharmony_ci	if (!str) {
1448c2ecf20Sopenharmony_ci		DMWARN("Unable to allocate memory for constructor string");
1458c2ecf20Sopenharmony_ci		return -ENOMEM;
1468c2ecf20Sopenharmony_ci	}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	str_size = sprintf(str, "%llu", (unsigned long long)ti->len);
1498c2ecf20Sopenharmony_ci	for (i = 0; i < argc; i++)
1508c2ecf20Sopenharmony_ci		str_size += sprintf(str + str_size, " %s", argv[i]);
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	*ctr_str = str;
1538c2ecf20Sopenharmony_ci	return str_size;
1548c2ecf20Sopenharmony_ci}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_cistatic void do_flush(struct work_struct *work)
1578c2ecf20Sopenharmony_ci{
1588c2ecf20Sopenharmony_ci	int r;
1598c2ecf20Sopenharmony_ci	struct log_c *lc = container_of(work, struct log_c, flush_log_work.work);
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	atomic_set(&lc->sched_flush, 0);
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	r = userspace_do_request(lc, lc->uuid, DM_ULOG_FLUSH, NULL, 0, NULL, NULL);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	if (r)
1668c2ecf20Sopenharmony_ci		dm_table_event(lc->ti->table);
1678c2ecf20Sopenharmony_ci}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci/*
1708c2ecf20Sopenharmony_ci * userspace_ctr
1718c2ecf20Sopenharmony_ci *
1728c2ecf20Sopenharmony_ci * argv contains:
1738c2ecf20Sopenharmony_ci *	<UUID> [integrated_flush] <other args>
1748c2ecf20Sopenharmony_ci * Where 'other args' are the userspace implementation-specific log
1758c2ecf20Sopenharmony_ci * arguments.
1768c2ecf20Sopenharmony_ci *
1778c2ecf20Sopenharmony_ci * Example:
1788c2ecf20Sopenharmony_ci *	<UUID> [integrated_flush] clustered-disk <arg count> <log dev>
1798c2ecf20Sopenharmony_ci *	<region_size> [[no]sync]
1808c2ecf20Sopenharmony_ci *
1818c2ecf20Sopenharmony_ci * This module strips off the <UUID> and uses it for identification
1828c2ecf20Sopenharmony_ci * purposes when communicating with userspace about a log.
1838c2ecf20Sopenharmony_ci *
1848c2ecf20Sopenharmony_ci * If integrated_flush is defined, the kernel combines flush
1858c2ecf20Sopenharmony_ci * and mark requests.
1868c2ecf20Sopenharmony_ci *
1878c2ecf20Sopenharmony_ci * The rest of the line, beginning with 'clustered-disk', is passed
1888c2ecf20Sopenharmony_ci * to the userspace ctr function.
1898c2ecf20Sopenharmony_ci */
1908c2ecf20Sopenharmony_cistatic int userspace_ctr(struct dm_dirty_log *log, struct dm_target *ti,
1918c2ecf20Sopenharmony_ci			 unsigned argc, char **argv)
1928c2ecf20Sopenharmony_ci{
1938c2ecf20Sopenharmony_ci	int r = 0;
1948c2ecf20Sopenharmony_ci	int str_size;
1958c2ecf20Sopenharmony_ci	char *ctr_str = NULL;
1968c2ecf20Sopenharmony_ci	struct log_c *lc = NULL;
1978c2ecf20Sopenharmony_ci	uint64_t rdata;
1988c2ecf20Sopenharmony_ci	size_t rdata_size = sizeof(rdata);
1998c2ecf20Sopenharmony_ci	char *devices_rdata = NULL;
2008c2ecf20Sopenharmony_ci	size_t devices_rdata_size = DM_NAME_LEN;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	if (argc < 3) {
2038c2ecf20Sopenharmony_ci		DMWARN("Too few arguments to userspace dirty log");
2048c2ecf20Sopenharmony_ci		return -EINVAL;
2058c2ecf20Sopenharmony_ci	}
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	lc = kzalloc(sizeof(*lc), GFP_KERNEL);
2088c2ecf20Sopenharmony_ci	if (!lc) {
2098c2ecf20Sopenharmony_ci		DMWARN("Unable to allocate userspace log context.");
2108c2ecf20Sopenharmony_ci		return -ENOMEM;
2118c2ecf20Sopenharmony_ci	}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	/* The ptr value is sufficient for local unique id */
2148c2ecf20Sopenharmony_ci	lc->luid = (unsigned long)lc;
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	lc->ti = ti;
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	if (strlen(argv[0]) > (DM_UUID_LEN - 1)) {
2198c2ecf20Sopenharmony_ci		DMWARN("UUID argument too long.");
2208c2ecf20Sopenharmony_ci		kfree(lc);
2218c2ecf20Sopenharmony_ci		return -EINVAL;
2228c2ecf20Sopenharmony_ci	}
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	lc->usr_argc = argc;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	strncpy(lc->uuid, argv[0], DM_UUID_LEN);
2278c2ecf20Sopenharmony_ci	argc--;
2288c2ecf20Sopenharmony_ci	argv++;
2298c2ecf20Sopenharmony_ci	spin_lock_init(&lc->flush_lock);
2308c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&lc->mark_list);
2318c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&lc->clear_list);
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	if (!strcasecmp(argv[0], "integrated_flush")) {
2348c2ecf20Sopenharmony_ci		lc->integrated_flush = 1;
2358c2ecf20Sopenharmony_ci		argc--;
2368c2ecf20Sopenharmony_ci		argv++;
2378c2ecf20Sopenharmony_ci	}
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	str_size = build_constructor_string(ti, argc, argv, &ctr_str);
2408c2ecf20Sopenharmony_ci	if (str_size < 0) {
2418c2ecf20Sopenharmony_ci		kfree(lc);
2428c2ecf20Sopenharmony_ci		return str_size;
2438c2ecf20Sopenharmony_ci	}
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	devices_rdata = kzalloc(devices_rdata_size, GFP_KERNEL);
2468c2ecf20Sopenharmony_ci	if (!devices_rdata) {
2478c2ecf20Sopenharmony_ci		DMERR("Failed to allocate memory for device information");
2488c2ecf20Sopenharmony_ci		r = -ENOMEM;
2498c2ecf20Sopenharmony_ci		goto out;
2508c2ecf20Sopenharmony_ci	}
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	r = mempool_init_slab_pool(&lc->flush_entry_pool, FLUSH_ENTRY_POOL_SIZE,
2538c2ecf20Sopenharmony_ci				   _flush_entry_cache);
2548c2ecf20Sopenharmony_ci	if (r) {
2558c2ecf20Sopenharmony_ci		DMERR("Failed to create flush_entry_pool");
2568c2ecf20Sopenharmony_ci		goto out;
2578c2ecf20Sopenharmony_ci	}
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	/*
2608c2ecf20Sopenharmony_ci	 * Send table string and get back any opened device.
2618c2ecf20Sopenharmony_ci	 */
2628c2ecf20Sopenharmony_ci	r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_CTR,
2638c2ecf20Sopenharmony_ci				 ctr_str, str_size,
2648c2ecf20Sopenharmony_ci				 devices_rdata, &devices_rdata_size);
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	if (r < 0) {
2678c2ecf20Sopenharmony_ci		if (r == -ESRCH)
2688c2ecf20Sopenharmony_ci			DMERR("Userspace log server not found");
2698c2ecf20Sopenharmony_ci		else
2708c2ecf20Sopenharmony_ci			DMERR("Userspace log server failed to create log");
2718c2ecf20Sopenharmony_ci		goto out;
2728c2ecf20Sopenharmony_ci	}
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	/* Since the region size does not change, get it now */
2758c2ecf20Sopenharmony_ci	rdata_size = sizeof(rdata);
2768c2ecf20Sopenharmony_ci	r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_GET_REGION_SIZE,
2778c2ecf20Sopenharmony_ci				 NULL, 0, (char *)&rdata, &rdata_size);
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	if (r) {
2808c2ecf20Sopenharmony_ci		DMERR("Failed to get region size of dirty log");
2818c2ecf20Sopenharmony_ci		goto out;
2828c2ecf20Sopenharmony_ci	}
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	lc->region_size = (uint32_t)rdata;
2858c2ecf20Sopenharmony_ci	lc->region_count = dm_sector_div_up(ti->len, lc->region_size);
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	if (devices_rdata_size) {
2888c2ecf20Sopenharmony_ci		if (devices_rdata[devices_rdata_size - 1] != '\0') {
2898c2ecf20Sopenharmony_ci			DMERR("DM_ULOG_CTR device return string not properly terminated");
2908c2ecf20Sopenharmony_ci			r = -EINVAL;
2918c2ecf20Sopenharmony_ci			goto out;
2928c2ecf20Sopenharmony_ci		}
2938c2ecf20Sopenharmony_ci		r = dm_get_device(ti, devices_rdata,
2948c2ecf20Sopenharmony_ci				  dm_table_get_mode(ti->table), &lc->log_dev);
2958c2ecf20Sopenharmony_ci		if (r)
2968c2ecf20Sopenharmony_ci			DMERR("Failed to register %s with device-mapper",
2978c2ecf20Sopenharmony_ci			      devices_rdata);
2988c2ecf20Sopenharmony_ci	}
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	if (lc->integrated_flush) {
3018c2ecf20Sopenharmony_ci		lc->dmlog_wq = alloc_workqueue("dmlogd", WQ_MEM_RECLAIM, 0);
3028c2ecf20Sopenharmony_ci		if (!lc->dmlog_wq) {
3038c2ecf20Sopenharmony_ci			DMERR("couldn't start dmlogd");
3048c2ecf20Sopenharmony_ci			r = -ENOMEM;
3058c2ecf20Sopenharmony_ci			goto out;
3068c2ecf20Sopenharmony_ci		}
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci		INIT_DELAYED_WORK(&lc->flush_log_work, do_flush);
3098c2ecf20Sopenharmony_ci		atomic_set(&lc->sched_flush, 0);
3108c2ecf20Sopenharmony_ci	}
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ciout:
3138c2ecf20Sopenharmony_ci	kfree(devices_rdata);
3148c2ecf20Sopenharmony_ci	if (r) {
3158c2ecf20Sopenharmony_ci		mempool_exit(&lc->flush_entry_pool);
3168c2ecf20Sopenharmony_ci		kfree(lc);
3178c2ecf20Sopenharmony_ci		kfree(ctr_str);
3188c2ecf20Sopenharmony_ci	} else {
3198c2ecf20Sopenharmony_ci		lc->usr_argv_str = ctr_str;
3208c2ecf20Sopenharmony_ci		log->context = lc;
3218c2ecf20Sopenharmony_ci	}
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	return r;
3248c2ecf20Sopenharmony_ci}
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_cistatic void userspace_dtr(struct dm_dirty_log *log)
3278c2ecf20Sopenharmony_ci{
3288c2ecf20Sopenharmony_ci	struct log_c *lc = log->context;
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	if (lc->integrated_flush) {
3318c2ecf20Sopenharmony_ci		/* flush workqueue */
3328c2ecf20Sopenharmony_ci		if (atomic_read(&lc->sched_flush))
3338c2ecf20Sopenharmony_ci			flush_delayed_work(&lc->flush_log_work);
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci		destroy_workqueue(lc->dmlog_wq);
3368c2ecf20Sopenharmony_ci	}
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	(void) dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_DTR,
3398c2ecf20Sopenharmony_ci				    NULL, 0, NULL, NULL);
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	if (lc->log_dev)
3428c2ecf20Sopenharmony_ci		dm_put_device(lc->ti, lc->log_dev);
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	mempool_exit(&lc->flush_entry_pool);
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	kfree(lc->usr_argv_str);
3478c2ecf20Sopenharmony_ci	kfree(lc);
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	return;
3508c2ecf20Sopenharmony_ci}
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_cistatic int userspace_presuspend(struct dm_dirty_log *log)
3538c2ecf20Sopenharmony_ci{
3548c2ecf20Sopenharmony_ci	int r;
3558c2ecf20Sopenharmony_ci	struct log_c *lc = log->context;
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_PRESUSPEND,
3588c2ecf20Sopenharmony_ci				 NULL, 0, NULL, NULL);
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	return r;
3618c2ecf20Sopenharmony_ci}
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_cistatic int userspace_postsuspend(struct dm_dirty_log *log)
3648c2ecf20Sopenharmony_ci{
3658c2ecf20Sopenharmony_ci	int r;
3668c2ecf20Sopenharmony_ci	struct log_c *lc = log->context;
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	/*
3698c2ecf20Sopenharmony_ci	 * Run planned flush earlier.
3708c2ecf20Sopenharmony_ci	 */
3718c2ecf20Sopenharmony_ci	if (lc->integrated_flush && atomic_read(&lc->sched_flush))
3728c2ecf20Sopenharmony_ci		flush_delayed_work(&lc->flush_log_work);
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_POSTSUSPEND,
3758c2ecf20Sopenharmony_ci				 NULL, 0, NULL, NULL);
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	return r;
3788c2ecf20Sopenharmony_ci}
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_cistatic int userspace_resume(struct dm_dirty_log *log)
3818c2ecf20Sopenharmony_ci{
3828c2ecf20Sopenharmony_ci	int r;
3838c2ecf20Sopenharmony_ci	struct log_c *lc = log->context;
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	lc->in_sync_hint = 0;
3868c2ecf20Sopenharmony_ci	r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_RESUME,
3878c2ecf20Sopenharmony_ci				 NULL, 0, NULL, NULL);
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	return r;
3908c2ecf20Sopenharmony_ci}
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_cistatic uint32_t userspace_get_region_size(struct dm_dirty_log *log)
3938c2ecf20Sopenharmony_ci{
3948c2ecf20Sopenharmony_ci	struct log_c *lc = log->context;
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	return lc->region_size;
3978c2ecf20Sopenharmony_ci}
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci/*
4008c2ecf20Sopenharmony_ci * userspace_is_clean
4018c2ecf20Sopenharmony_ci *
4028c2ecf20Sopenharmony_ci * Check whether a region is clean.  If there is any sort of
4038c2ecf20Sopenharmony_ci * failure when consulting the server, we return not clean.
4048c2ecf20Sopenharmony_ci *
4058c2ecf20Sopenharmony_ci * Returns: 1 if clean, 0 otherwise
4068c2ecf20Sopenharmony_ci */
4078c2ecf20Sopenharmony_cistatic int userspace_is_clean(struct dm_dirty_log *log, region_t region)
4088c2ecf20Sopenharmony_ci{
4098c2ecf20Sopenharmony_ci	int r;
4108c2ecf20Sopenharmony_ci	uint64_t region64 = (uint64_t)region;
4118c2ecf20Sopenharmony_ci	int64_t is_clean;
4128c2ecf20Sopenharmony_ci	size_t rdata_size;
4138c2ecf20Sopenharmony_ci	struct log_c *lc = log->context;
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	rdata_size = sizeof(is_clean);
4168c2ecf20Sopenharmony_ci	r = userspace_do_request(lc, lc->uuid, DM_ULOG_IS_CLEAN,
4178c2ecf20Sopenharmony_ci				 (char *)&region64, sizeof(region64),
4188c2ecf20Sopenharmony_ci				 (char *)&is_clean, &rdata_size);
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	return (r) ? 0 : (int)is_clean;
4218c2ecf20Sopenharmony_ci}
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci/*
4248c2ecf20Sopenharmony_ci * userspace_in_sync
4258c2ecf20Sopenharmony_ci *
4268c2ecf20Sopenharmony_ci * Check if the region is in-sync.  If there is any sort
4278c2ecf20Sopenharmony_ci * of failure when consulting the server, we assume that
4288c2ecf20Sopenharmony_ci * the region is not in sync.
4298c2ecf20Sopenharmony_ci *
4308c2ecf20Sopenharmony_ci * If 'can_block' is set, return immediately
4318c2ecf20Sopenharmony_ci *
4328c2ecf20Sopenharmony_ci * Returns: 1 if in-sync, 0 if not-in-sync, -EWOULDBLOCK
4338c2ecf20Sopenharmony_ci */
4348c2ecf20Sopenharmony_cistatic int userspace_in_sync(struct dm_dirty_log *log, region_t region,
4358c2ecf20Sopenharmony_ci			     int can_block)
4368c2ecf20Sopenharmony_ci{
4378c2ecf20Sopenharmony_ci	int r;
4388c2ecf20Sopenharmony_ci	uint64_t region64 = region;
4398c2ecf20Sopenharmony_ci	int64_t in_sync;
4408c2ecf20Sopenharmony_ci	size_t rdata_size;
4418c2ecf20Sopenharmony_ci	struct log_c *lc = log->context;
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	/*
4448c2ecf20Sopenharmony_ci	 * We can never respond directly - even if in_sync_hint is
4458c2ecf20Sopenharmony_ci	 * set.  This is because another machine could see a device
4468c2ecf20Sopenharmony_ci	 * failure and mark the region out-of-sync.  If we don't go
4478c2ecf20Sopenharmony_ci	 * to userspace to ask, we might think the region is in-sync
4488c2ecf20Sopenharmony_ci	 * and allow a read to pick up data that is stale.  (This is
4498c2ecf20Sopenharmony_ci	 * very unlikely if a device actually fails; but it is very
4508c2ecf20Sopenharmony_ci	 * likely if a connection to one device from one machine fails.)
4518c2ecf20Sopenharmony_ci	 *
4528c2ecf20Sopenharmony_ci	 * There still might be a problem if the mirror caches the region
4538c2ecf20Sopenharmony_ci	 * state as in-sync... but then this call would not be made.  So,
4548c2ecf20Sopenharmony_ci	 * that is a mirror problem.
4558c2ecf20Sopenharmony_ci	 */
4568c2ecf20Sopenharmony_ci	if (!can_block)
4578c2ecf20Sopenharmony_ci		return -EWOULDBLOCK;
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	rdata_size = sizeof(in_sync);
4608c2ecf20Sopenharmony_ci	r = userspace_do_request(lc, lc->uuid, DM_ULOG_IN_SYNC,
4618c2ecf20Sopenharmony_ci				 (char *)&region64, sizeof(region64),
4628c2ecf20Sopenharmony_ci				 (char *)&in_sync, &rdata_size);
4638c2ecf20Sopenharmony_ci	return (r) ? 0 : (int)in_sync;
4648c2ecf20Sopenharmony_ci}
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_cistatic int flush_one_by_one(struct log_c *lc, struct list_head *flush_list)
4678c2ecf20Sopenharmony_ci{
4688c2ecf20Sopenharmony_ci	int r = 0;
4698c2ecf20Sopenharmony_ci	struct dm_dirty_log_flush_entry *fe;
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	list_for_each_entry(fe, flush_list, list) {
4728c2ecf20Sopenharmony_ci		r = userspace_do_request(lc, lc->uuid, fe->type,
4738c2ecf20Sopenharmony_ci					 (char *)&fe->region,
4748c2ecf20Sopenharmony_ci					 sizeof(fe->region),
4758c2ecf20Sopenharmony_ci					 NULL, NULL);
4768c2ecf20Sopenharmony_ci		if (r)
4778c2ecf20Sopenharmony_ci			break;
4788c2ecf20Sopenharmony_ci	}
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci	return r;
4818c2ecf20Sopenharmony_ci}
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_cistatic int flush_by_group(struct log_c *lc, struct list_head *flush_list,
4848c2ecf20Sopenharmony_ci			  int flush_with_payload)
4858c2ecf20Sopenharmony_ci{
4868c2ecf20Sopenharmony_ci	int r = 0;
4878c2ecf20Sopenharmony_ci	int count;
4888c2ecf20Sopenharmony_ci	uint32_t type = 0;
4898c2ecf20Sopenharmony_ci	struct dm_dirty_log_flush_entry *fe, *tmp_fe;
4908c2ecf20Sopenharmony_ci	LIST_HEAD(tmp_list);
4918c2ecf20Sopenharmony_ci	uint64_t group[MAX_FLUSH_GROUP_COUNT];
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	/*
4948c2ecf20Sopenharmony_ci	 * Group process the requests
4958c2ecf20Sopenharmony_ci	 */
4968c2ecf20Sopenharmony_ci	while (!list_empty(flush_list)) {
4978c2ecf20Sopenharmony_ci		count = 0;
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_ci		list_for_each_entry_safe(fe, tmp_fe, flush_list, list) {
5008c2ecf20Sopenharmony_ci			group[count] = fe->region;
5018c2ecf20Sopenharmony_ci			count++;
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci			list_move(&fe->list, &tmp_list);
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci			type = fe->type;
5068c2ecf20Sopenharmony_ci			if (count >= MAX_FLUSH_GROUP_COUNT)
5078c2ecf20Sopenharmony_ci				break;
5088c2ecf20Sopenharmony_ci		}
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci		if (flush_with_payload) {
5118c2ecf20Sopenharmony_ci			r = userspace_do_request(lc, lc->uuid, DM_ULOG_FLUSH,
5128c2ecf20Sopenharmony_ci						 (char *)(group),
5138c2ecf20Sopenharmony_ci						 count * sizeof(uint64_t),
5148c2ecf20Sopenharmony_ci						 NULL, NULL);
5158c2ecf20Sopenharmony_ci			/*
5168c2ecf20Sopenharmony_ci			 * Integrated flush failed.
5178c2ecf20Sopenharmony_ci			 */
5188c2ecf20Sopenharmony_ci			if (r)
5198c2ecf20Sopenharmony_ci				break;
5208c2ecf20Sopenharmony_ci		} else {
5218c2ecf20Sopenharmony_ci			r = userspace_do_request(lc, lc->uuid, type,
5228c2ecf20Sopenharmony_ci						 (char *)(group),
5238c2ecf20Sopenharmony_ci						 count * sizeof(uint64_t),
5248c2ecf20Sopenharmony_ci						 NULL, NULL);
5258c2ecf20Sopenharmony_ci			if (r) {
5268c2ecf20Sopenharmony_ci				/*
5278c2ecf20Sopenharmony_ci				 * Group send failed.  Attempt one-by-one.
5288c2ecf20Sopenharmony_ci				 */
5298c2ecf20Sopenharmony_ci				list_splice_init(&tmp_list, flush_list);
5308c2ecf20Sopenharmony_ci				r = flush_one_by_one(lc, flush_list);
5318c2ecf20Sopenharmony_ci				break;
5328c2ecf20Sopenharmony_ci			}
5338c2ecf20Sopenharmony_ci		}
5348c2ecf20Sopenharmony_ci	}
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci	/*
5378c2ecf20Sopenharmony_ci	 * Must collect flush_entrys that were successfully processed
5388c2ecf20Sopenharmony_ci	 * as a group so that they will be free'd by the caller.
5398c2ecf20Sopenharmony_ci	 */
5408c2ecf20Sopenharmony_ci	list_splice_init(&tmp_list, flush_list);
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_ci	return r;
5438c2ecf20Sopenharmony_ci}
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci/*
5468c2ecf20Sopenharmony_ci * userspace_flush
5478c2ecf20Sopenharmony_ci *
5488c2ecf20Sopenharmony_ci * This function is ok to block.
5498c2ecf20Sopenharmony_ci * The flush happens in two stages.  First, it sends all
5508c2ecf20Sopenharmony_ci * clear/mark requests that are on the list.  Then it
5518c2ecf20Sopenharmony_ci * tells the server to commit them.  This gives the
5528c2ecf20Sopenharmony_ci * server a chance to optimise the commit, instead of
5538c2ecf20Sopenharmony_ci * doing it for every request.
5548c2ecf20Sopenharmony_ci *
5558c2ecf20Sopenharmony_ci * Additionally, we could implement another thread that
5568c2ecf20Sopenharmony_ci * sends the requests up to the server - reducing the
5578c2ecf20Sopenharmony_ci * load on flush.  Then the flush would have less in
5588c2ecf20Sopenharmony_ci * the list and be responsible for the finishing commit.
5598c2ecf20Sopenharmony_ci *
5608c2ecf20Sopenharmony_ci * Returns: 0 on success, < 0 on failure
5618c2ecf20Sopenharmony_ci */
5628c2ecf20Sopenharmony_cistatic int userspace_flush(struct dm_dirty_log *log)
5638c2ecf20Sopenharmony_ci{
5648c2ecf20Sopenharmony_ci	int r = 0;
5658c2ecf20Sopenharmony_ci	unsigned long flags;
5668c2ecf20Sopenharmony_ci	struct log_c *lc = log->context;
5678c2ecf20Sopenharmony_ci	LIST_HEAD(mark_list);
5688c2ecf20Sopenharmony_ci	LIST_HEAD(clear_list);
5698c2ecf20Sopenharmony_ci	int mark_list_is_empty;
5708c2ecf20Sopenharmony_ci	int clear_list_is_empty;
5718c2ecf20Sopenharmony_ci	struct dm_dirty_log_flush_entry *fe, *tmp_fe;
5728c2ecf20Sopenharmony_ci	mempool_t *flush_entry_pool = &lc->flush_entry_pool;
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ci	spin_lock_irqsave(&lc->flush_lock, flags);
5758c2ecf20Sopenharmony_ci	list_splice_init(&lc->mark_list, &mark_list);
5768c2ecf20Sopenharmony_ci	list_splice_init(&lc->clear_list, &clear_list);
5778c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&lc->flush_lock, flags);
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci	mark_list_is_empty = list_empty(&mark_list);
5808c2ecf20Sopenharmony_ci	clear_list_is_empty = list_empty(&clear_list);
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci	if (mark_list_is_empty && clear_list_is_empty)
5838c2ecf20Sopenharmony_ci		return 0;
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci	r = flush_by_group(lc, &clear_list, 0);
5868c2ecf20Sopenharmony_ci	if (r)
5878c2ecf20Sopenharmony_ci		goto out;
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci	if (!lc->integrated_flush) {
5908c2ecf20Sopenharmony_ci		r = flush_by_group(lc, &mark_list, 0);
5918c2ecf20Sopenharmony_ci		if (r)
5928c2ecf20Sopenharmony_ci			goto out;
5938c2ecf20Sopenharmony_ci		r = userspace_do_request(lc, lc->uuid, DM_ULOG_FLUSH,
5948c2ecf20Sopenharmony_ci					 NULL, 0, NULL, NULL);
5958c2ecf20Sopenharmony_ci		goto out;
5968c2ecf20Sopenharmony_ci	}
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci	/*
5998c2ecf20Sopenharmony_ci	 * Send integrated flush request with mark_list as payload.
6008c2ecf20Sopenharmony_ci	 */
6018c2ecf20Sopenharmony_ci	r = flush_by_group(lc, &mark_list, 1);
6028c2ecf20Sopenharmony_ci	if (r)
6038c2ecf20Sopenharmony_ci		goto out;
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci	if (mark_list_is_empty && !atomic_read(&lc->sched_flush)) {
6068c2ecf20Sopenharmony_ci		/*
6078c2ecf20Sopenharmony_ci		 * When there are only clear region requests,
6088c2ecf20Sopenharmony_ci		 * we schedule a flush in the future.
6098c2ecf20Sopenharmony_ci		 */
6108c2ecf20Sopenharmony_ci		queue_delayed_work(lc->dmlog_wq, &lc->flush_log_work, 3 * HZ);
6118c2ecf20Sopenharmony_ci		atomic_set(&lc->sched_flush, 1);
6128c2ecf20Sopenharmony_ci	} else {
6138c2ecf20Sopenharmony_ci		/*
6148c2ecf20Sopenharmony_ci		 * Cancel pending flush because we
6158c2ecf20Sopenharmony_ci		 * have already flushed in mark_region.
6168c2ecf20Sopenharmony_ci		 */
6178c2ecf20Sopenharmony_ci		cancel_delayed_work(&lc->flush_log_work);
6188c2ecf20Sopenharmony_ci		atomic_set(&lc->sched_flush, 0);
6198c2ecf20Sopenharmony_ci	}
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ciout:
6228c2ecf20Sopenharmony_ci	/*
6238c2ecf20Sopenharmony_ci	 * We can safely remove these entries, even after failure.
6248c2ecf20Sopenharmony_ci	 * Calling code will receive an error and will know that
6258c2ecf20Sopenharmony_ci	 * the log facility has failed.
6268c2ecf20Sopenharmony_ci	 */
6278c2ecf20Sopenharmony_ci	list_for_each_entry_safe(fe, tmp_fe, &mark_list, list) {
6288c2ecf20Sopenharmony_ci		list_del(&fe->list);
6298c2ecf20Sopenharmony_ci		mempool_free(fe, flush_entry_pool);
6308c2ecf20Sopenharmony_ci	}
6318c2ecf20Sopenharmony_ci	list_for_each_entry_safe(fe, tmp_fe, &clear_list, list) {
6328c2ecf20Sopenharmony_ci		list_del(&fe->list);
6338c2ecf20Sopenharmony_ci		mempool_free(fe, flush_entry_pool);
6348c2ecf20Sopenharmony_ci	}
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci	if (r)
6378c2ecf20Sopenharmony_ci		dm_table_event(lc->ti->table);
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_ci	return r;
6408c2ecf20Sopenharmony_ci}
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_ci/*
6438c2ecf20Sopenharmony_ci * userspace_mark_region
6448c2ecf20Sopenharmony_ci *
6458c2ecf20Sopenharmony_ci * This function should avoid blocking unless absolutely required.
6468c2ecf20Sopenharmony_ci * (Memory allocation is valid for blocking.)
6478c2ecf20Sopenharmony_ci */
6488c2ecf20Sopenharmony_cistatic void userspace_mark_region(struct dm_dirty_log *log, region_t region)
6498c2ecf20Sopenharmony_ci{
6508c2ecf20Sopenharmony_ci	unsigned long flags;
6518c2ecf20Sopenharmony_ci	struct log_c *lc = log->context;
6528c2ecf20Sopenharmony_ci	struct dm_dirty_log_flush_entry *fe;
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_ci	/* Wait for an allocation, but _never_ fail */
6558c2ecf20Sopenharmony_ci	fe = mempool_alloc(&lc->flush_entry_pool, GFP_NOIO);
6568c2ecf20Sopenharmony_ci	BUG_ON(!fe);
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_ci	spin_lock_irqsave(&lc->flush_lock, flags);
6598c2ecf20Sopenharmony_ci	fe->type = DM_ULOG_MARK_REGION;
6608c2ecf20Sopenharmony_ci	fe->region = region;
6618c2ecf20Sopenharmony_ci	list_add(&fe->list, &lc->mark_list);
6628c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&lc->flush_lock, flags);
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_ci	return;
6658c2ecf20Sopenharmony_ci}
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_ci/*
6688c2ecf20Sopenharmony_ci * userspace_clear_region
6698c2ecf20Sopenharmony_ci *
6708c2ecf20Sopenharmony_ci * This function must not block.
6718c2ecf20Sopenharmony_ci * So, the alloc can't block.  In the worst case, it is ok to
6728c2ecf20Sopenharmony_ci * fail.  It would simply mean we can't clear the region.
6738c2ecf20Sopenharmony_ci * Does nothing to current sync context, but does mean
6748c2ecf20Sopenharmony_ci * the region will be re-sync'ed on a reload of the mirror
6758c2ecf20Sopenharmony_ci * even though it is in-sync.
6768c2ecf20Sopenharmony_ci */
6778c2ecf20Sopenharmony_cistatic void userspace_clear_region(struct dm_dirty_log *log, region_t region)
6788c2ecf20Sopenharmony_ci{
6798c2ecf20Sopenharmony_ci	unsigned long flags;
6808c2ecf20Sopenharmony_ci	struct log_c *lc = log->context;
6818c2ecf20Sopenharmony_ci	struct dm_dirty_log_flush_entry *fe;
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci	/*
6848c2ecf20Sopenharmony_ci	 * If we fail to allocate, we skip the clearing of
6858c2ecf20Sopenharmony_ci	 * the region.  This doesn't hurt us in any way, except
6868c2ecf20Sopenharmony_ci	 * to cause the region to be resync'ed when the
6878c2ecf20Sopenharmony_ci	 * device is activated next time.
6888c2ecf20Sopenharmony_ci	 */
6898c2ecf20Sopenharmony_ci	fe = mempool_alloc(&lc->flush_entry_pool, GFP_ATOMIC);
6908c2ecf20Sopenharmony_ci	if (!fe) {
6918c2ecf20Sopenharmony_ci		DMERR("Failed to allocate memory to clear region.");
6928c2ecf20Sopenharmony_ci		return;
6938c2ecf20Sopenharmony_ci	}
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ci	spin_lock_irqsave(&lc->flush_lock, flags);
6968c2ecf20Sopenharmony_ci	fe->type = DM_ULOG_CLEAR_REGION;
6978c2ecf20Sopenharmony_ci	fe->region = region;
6988c2ecf20Sopenharmony_ci	list_add(&fe->list, &lc->clear_list);
6998c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&lc->flush_lock, flags);
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_ci	return;
7028c2ecf20Sopenharmony_ci}
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_ci/*
7058c2ecf20Sopenharmony_ci * userspace_get_resync_work
7068c2ecf20Sopenharmony_ci *
7078c2ecf20Sopenharmony_ci * Get a region that needs recovery.  It is valid to return
7088c2ecf20Sopenharmony_ci * an error for this function.
7098c2ecf20Sopenharmony_ci *
7108c2ecf20Sopenharmony_ci * Returns: 1 if region filled, 0 if no work, <0 on error
7118c2ecf20Sopenharmony_ci */
7128c2ecf20Sopenharmony_cistatic int userspace_get_resync_work(struct dm_dirty_log *log, region_t *region)
7138c2ecf20Sopenharmony_ci{
7148c2ecf20Sopenharmony_ci	int r;
7158c2ecf20Sopenharmony_ci	size_t rdata_size;
7168c2ecf20Sopenharmony_ci	struct log_c *lc = log->context;
7178c2ecf20Sopenharmony_ci	struct {
7188c2ecf20Sopenharmony_ci		int64_t i; /* 64-bit for mix arch compatibility */
7198c2ecf20Sopenharmony_ci		region_t r;
7208c2ecf20Sopenharmony_ci	} pkg;
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_ci	if (lc->in_sync_hint >= lc->region_count)
7238c2ecf20Sopenharmony_ci		return 0;
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_ci	rdata_size = sizeof(pkg);
7268c2ecf20Sopenharmony_ci	r = userspace_do_request(lc, lc->uuid, DM_ULOG_GET_RESYNC_WORK,
7278c2ecf20Sopenharmony_ci				 NULL, 0, (char *)&pkg, &rdata_size);
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ci	*region = pkg.r;
7308c2ecf20Sopenharmony_ci	return (r) ? r : (int)pkg.i;
7318c2ecf20Sopenharmony_ci}
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci/*
7348c2ecf20Sopenharmony_ci * userspace_set_region_sync
7358c2ecf20Sopenharmony_ci *
7368c2ecf20Sopenharmony_ci * Set the sync status of a given region.  This function
7378c2ecf20Sopenharmony_ci * must not fail.
7388c2ecf20Sopenharmony_ci */
7398c2ecf20Sopenharmony_cistatic void userspace_set_region_sync(struct dm_dirty_log *log,
7408c2ecf20Sopenharmony_ci				      region_t region, int in_sync)
7418c2ecf20Sopenharmony_ci{
7428c2ecf20Sopenharmony_ci	struct log_c *lc = log->context;
7438c2ecf20Sopenharmony_ci	struct {
7448c2ecf20Sopenharmony_ci		region_t r;
7458c2ecf20Sopenharmony_ci		int64_t i;
7468c2ecf20Sopenharmony_ci	} pkg;
7478c2ecf20Sopenharmony_ci
7488c2ecf20Sopenharmony_ci	pkg.r = region;
7498c2ecf20Sopenharmony_ci	pkg.i = (int64_t)in_sync;
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_ci	(void) userspace_do_request(lc, lc->uuid, DM_ULOG_SET_REGION_SYNC,
7528c2ecf20Sopenharmony_ci				    (char *)&pkg, sizeof(pkg), NULL, NULL);
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_ci	/*
7558c2ecf20Sopenharmony_ci	 * It would be nice to be able to report failures.
7568c2ecf20Sopenharmony_ci	 * However, it is easy enough to detect and resolve.
7578c2ecf20Sopenharmony_ci	 */
7588c2ecf20Sopenharmony_ci	return;
7598c2ecf20Sopenharmony_ci}
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_ci/*
7628c2ecf20Sopenharmony_ci * userspace_get_sync_count
7638c2ecf20Sopenharmony_ci *
7648c2ecf20Sopenharmony_ci * If there is any sort of failure when consulting the server,
7658c2ecf20Sopenharmony_ci * we assume that the sync count is zero.
7668c2ecf20Sopenharmony_ci *
7678c2ecf20Sopenharmony_ci * Returns: sync count on success, 0 on failure
7688c2ecf20Sopenharmony_ci */
7698c2ecf20Sopenharmony_cistatic region_t userspace_get_sync_count(struct dm_dirty_log *log)
7708c2ecf20Sopenharmony_ci{
7718c2ecf20Sopenharmony_ci	int r;
7728c2ecf20Sopenharmony_ci	size_t rdata_size;
7738c2ecf20Sopenharmony_ci	uint64_t sync_count;
7748c2ecf20Sopenharmony_ci	struct log_c *lc = log->context;
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci	rdata_size = sizeof(sync_count);
7778c2ecf20Sopenharmony_ci	r = userspace_do_request(lc, lc->uuid, DM_ULOG_GET_SYNC_COUNT,
7788c2ecf20Sopenharmony_ci				 NULL, 0, (char *)&sync_count, &rdata_size);
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci	if (r)
7818c2ecf20Sopenharmony_ci		return 0;
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci	if (sync_count >= lc->region_count)
7848c2ecf20Sopenharmony_ci		lc->in_sync_hint = lc->region_count;
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_ci	return (region_t)sync_count;
7878c2ecf20Sopenharmony_ci}
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_ci/*
7908c2ecf20Sopenharmony_ci * userspace_status
7918c2ecf20Sopenharmony_ci *
7928c2ecf20Sopenharmony_ci * Returns: amount of space consumed
7938c2ecf20Sopenharmony_ci */
7948c2ecf20Sopenharmony_cistatic int userspace_status(struct dm_dirty_log *log, status_type_t status_type,
7958c2ecf20Sopenharmony_ci			    char *result, unsigned maxlen)
7968c2ecf20Sopenharmony_ci{
7978c2ecf20Sopenharmony_ci	int r = 0;
7988c2ecf20Sopenharmony_ci	char *table_args;
7998c2ecf20Sopenharmony_ci	size_t sz = (size_t)maxlen;
8008c2ecf20Sopenharmony_ci	struct log_c *lc = log->context;
8018c2ecf20Sopenharmony_ci
8028c2ecf20Sopenharmony_ci	switch (status_type) {
8038c2ecf20Sopenharmony_ci	case STATUSTYPE_INFO:
8048c2ecf20Sopenharmony_ci		r = userspace_do_request(lc, lc->uuid, DM_ULOG_STATUS_INFO,
8058c2ecf20Sopenharmony_ci					 NULL, 0, result, &sz);
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_ci		if (r) {
8088c2ecf20Sopenharmony_ci			sz = 0;
8098c2ecf20Sopenharmony_ci			DMEMIT("%s 1 COM_FAILURE", log->type->name);
8108c2ecf20Sopenharmony_ci		}
8118c2ecf20Sopenharmony_ci		break;
8128c2ecf20Sopenharmony_ci	case STATUSTYPE_TABLE:
8138c2ecf20Sopenharmony_ci		sz = 0;
8148c2ecf20Sopenharmony_ci		table_args = strchr(lc->usr_argv_str, ' ');
8158c2ecf20Sopenharmony_ci		BUG_ON(!table_args); /* There will always be a ' ' */
8168c2ecf20Sopenharmony_ci		table_args++;
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_ci		DMEMIT("%s %u %s ", log->type->name, lc->usr_argc, lc->uuid);
8198c2ecf20Sopenharmony_ci		if (lc->integrated_flush)
8208c2ecf20Sopenharmony_ci			DMEMIT("integrated_flush ");
8218c2ecf20Sopenharmony_ci		DMEMIT("%s ", table_args);
8228c2ecf20Sopenharmony_ci		break;
8238c2ecf20Sopenharmony_ci	}
8248c2ecf20Sopenharmony_ci	return (r) ? 0 : (int)sz;
8258c2ecf20Sopenharmony_ci}
8268c2ecf20Sopenharmony_ci
8278c2ecf20Sopenharmony_ci/*
8288c2ecf20Sopenharmony_ci * userspace_is_remote_recovering
8298c2ecf20Sopenharmony_ci *
8308c2ecf20Sopenharmony_ci * Returns: 1 if region recovering, 0 otherwise
8318c2ecf20Sopenharmony_ci */
8328c2ecf20Sopenharmony_cistatic int userspace_is_remote_recovering(struct dm_dirty_log *log,
8338c2ecf20Sopenharmony_ci					  region_t region)
8348c2ecf20Sopenharmony_ci{
8358c2ecf20Sopenharmony_ci	int r;
8368c2ecf20Sopenharmony_ci	uint64_t region64 = region;
8378c2ecf20Sopenharmony_ci	struct log_c *lc = log->context;
8388c2ecf20Sopenharmony_ci	static unsigned long limit;
8398c2ecf20Sopenharmony_ci	struct {
8408c2ecf20Sopenharmony_ci		int64_t is_recovering;
8418c2ecf20Sopenharmony_ci		uint64_t in_sync_hint;
8428c2ecf20Sopenharmony_ci	} pkg;
8438c2ecf20Sopenharmony_ci	size_t rdata_size = sizeof(pkg);
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_ci	/*
8468c2ecf20Sopenharmony_ci	 * Once the mirror has been reported to be in-sync,
8478c2ecf20Sopenharmony_ci	 * it will never again ask for recovery work.  So,
8488c2ecf20Sopenharmony_ci	 * we can safely say there is not a remote machine
8498c2ecf20Sopenharmony_ci	 * recovering if the device is in-sync.  (in_sync_hint
8508c2ecf20Sopenharmony_ci	 * must be reset at resume time.)
8518c2ecf20Sopenharmony_ci	 */
8528c2ecf20Sopenharmony_ci	if (region < lc->in_sync_hint)
8538c2ecf20Sopenharmony_ci		return 0;
8548c2ecf20Sopenharmony_ci	else if (time_after(limit, jiffies))
8558c2ecf20Sopenharmony_ci		return 1;
8568c2ecf20Sopenharmony_ci
8578c2ecf20Sopenharmony_ci	limit = jiffies + (HZ / 4);
8588c2ecf20Sopenharmony_ci	r = userspace_do_request(lc, lc->uuid, DM_ULOG_IS_REMOTE_RECOVERING,
8598c2ecf20Sopenharmony_ci				 (char *)&region64, sizeof(region64),
8608c2ecf20Sopenharmony_ci				 (char *)&pkg, &rdata_size);
8618c2ecf20Sopenharmony_ci	if (r)
8628c2ecf20Sopenharmony_ci		return 1;
8638c2ecf20Sopenharmony_ci
8648c2ecf20Sopenharmony_ci	lc->in_sync_hint = pkg.in_sync_hint;
8658c2ecf20Sopenharmony_ci
8668c2ecf20Sopenharmony_ci	return (int)pkg.is_recovering;
8678c2ecf20Sopenharmony_ci}
8688c2ecf20Sopenharmony_ci
8698c2ecf20Sopenharmony_cistatic struct dm_dirty_log_type _userspace_type = {
8708c2ecf20Sopenharmony_ci	.name = "userspace",
8718c2ecf20Sopenharmony_ci	.module = THIS_MODULE,
8728c2ecf20Sopenharmony_ci	.ctr = userspace_ctr,
8738c2ecf20Sopenharmony_ci	.dtr = userspace_dtr,
8748c2ecf20Sopenharmony_ci	.presuspend = userspace_presuspend,
8758c2ecf20Sopenharmony_ci	.postsuspend = userspace_postsuspend,
8768c2ecf20Sopenharmony_ci	.resume = userspace_resume,
8778c2ecf20Sopenharmony_ci	.get_region_size = userspace_get_region_size,
8788c2ecf20Sopenharmony_ci	.is_clean = userspace_is_clean,
8798c2ecf20Sopenharmony_ci	.in_sync = userspace_in_sync,
8808c2ecf20Sopenharmony_ci	.flush = userspace_flush,
8818c2ecf20Sopenharmony_ci	.mark_region = userspace_mark_region,
8828c2ecf20Sopenharmony_ci	.clear_region = userspace_clear_region,
8838c2ecf20Sopenharmony_ci	.get_resync_work = userspace_get_resync_work,
8848c2ecf20Sopenharmony_ci	.set_region_sync = userspace_set_region_sync,
8858c2ecf20Sopenharmony_ci	.get_sync_count = userspace_get_sync_count,
8868c2ecf20Sopenharmony_ci	.status = userspace_status,
8878c2ecf20Sopenharmony_ci	.is_remote_recovering = userspace_is_remote_recovering,
8888c2ecf20Sopenharmony_ci};
8898c2ecf20Sopenharmony_ci
8908c2ecf20Sopenharmony_cistatic int __init userspace_dirty_log_init(void)
8918c2ecf20Sopenharmony_ci{
8928c2ecf20Sopenharmony_ci	int r = 0;
8938c2ecf20Sopenharmony_ci
8948c2ecf20Sopenharmony_ci	_flush_entry_cache = KMEM_CACHE(dm_dirty_log_flush_entry, 0);
8958c2ecf20Sopenharmony_ci	if (!_flush_entry_cache) {
8968c2ecf20Sopenharmony_ci		DMWARN("Unable to create flush_entry_cache: No memory.");
8978c2ecf20Sopenharmony_ci		return -ENOMEM;
8988c2ecf20Sopenharmony_ci	}
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_ci	r = dm_ulog_tfr_init();
9018c2ecf20Sopenharmony_ci	if (r) {
9028c2ecf20Sopenharmony_ci		DMWARN("Unable to initialize userspace log communications");
9038c2ecf20Sopenharmony_ci		kmem_cache_destroy(_flush_entry_cache);
9048c2ecf20Sopenharmony_ci		return r;
9058c2ecf20Sopenharmony_ci	}
9068c2ecf20Sopenharmony_ci
9078c2ecf20Sopenharmony_ci	r = dm_dirty_log_type_register(&_userspace_type);
9088c2ecf20Sopenharmony_ci	if (r) {
9098c2ecf20Sopenharmony_ci		DMWARN("Couldn't register userspace dirty log type");
9108c2ecf20Sopenharmony_ci		dm_ulog_tfr_exit();
9118c2ecf20Sopenharmony_ci		kmem_cache_destroy(_flush_entry_cache);
9128c2ecf20Sopenharmony_ci		return r;
9138c2ecf20Sopenharmony_ci	}
9148c2ecf20Sopenharmony_ci
9158c2ecf20Sopenharmony_ci	DMINFO("version " DM_LOG_USERSPACE_VSN " loaded");
9168c2ecf20Sopenharmony_ci	return 0;
9178c2ecf20Sopenharmony_ci}
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_cistatic void __exit userspace_dirty_log_exit(void)
9208c2ecf20Sopenharmony_ci{
9218c2ecf20Sopenharmony_ci	dm_dirty_log_type_unregister(&_userspace_type);
9228c2ecf20Sopenharmony_ci	dm_ulog_tfr_exit();
9238c2ecf20Sopenharmony_ci	kmem_cache_destroy(_flush_entry_cache);
9248c2ecf20Sopenharmony_ci
9258c2ecf20Sopenharmony_ci	DMINFO("version " DM_LOG_USERSPACE_VSN " unloaded");
9268c2ecf20Sopenharmony_ci	return;
9278c2ecf20Sopenharmony_ci}
9288c2ecf20Sopenharmony_ci
9298c2ecf20Sopenharmony_cimodule_init(userspace_dirty_log_init);
9308c2ecf20Sopenharmony_cimodule_exit(userspace_dirty_log_exit);
9318c2ecf20Sopenharmony_ci
9328c2ecf20Sopenharmony_ciMODULE_DESCRIPTION(DM_NAME " userspace dirty log link");
9338c2ecf20Sopenharmony_ciMODULE_AUTHOR("Jonathan Brassow <dm-devel@redhat.com>");
9348c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
935