13bd94003SHeinz Mauelshagen // SPDX-License-Identifier: GPL-2.0-only
2f5db4af4SJonthan Brassow /*
3f5db4af4SJonthan Brassow * Copyright (C) 2006-2009 Red Hat, Inc.
4f5db4af4SJonthan Brassow *
5f5db4af4SJonthan Brassow * This file is released under the LGPL.
6f5db4af4SJonthan Brassow */
7f5db4af4SJonthan Brassow
8f5db4af4SJonthan Brassow #include <linux/bio.h>
95a0e3ad6STejun Heo #include <linux/slab.h>
100f30af98SManuel Schölling #include <linux/jiffies.h>
11f5db4af4SJonthan Brassow #include <linux/dm-dirty-log.h>
12f5db4af4SJonthan Brassow #include <linux/device-mapper.h>
13f5db4af4SJonthan Brassow #include <linux/dm-log-userspace.h>
14056075c7SPaul Gortmaker #include <linux/module.h>
155066a4dfSDongmao Zhang #include <linux/workqueue.h>
16f5db4af4SJonthan Brassow
17f5db4af4SJonthan Brassow #include "dm-log-userspace-transfer.h"
18f5db4af4SJonthan Brassow
195066a4dfSDongmao Zhang #define DM_LOG_USERSPACE_VSN "1.3.0"
2086a54a48SJonathan Brassow
21ac1f9ef2SMike Snitzer #define FLUSH_ENTRY_POOL_SIZE 16
22ac1f9ef2SMike Snitzer
23ac1f9ef2SMike Snitzer struct dm_dirty_log_flush_entry {
24f5db4af4SJonthan Brassow int type;
25f5db4af4SJonthan Brassow region_t region;
26f5db4af4SJonthan Brassow struct list_head list;
27f5db4af4SJonthan Brassow };
28f5db4af4SJonthan Brassow
29085ae065SJonathan Brassow /*
30085ae065SJonathan Brassow * This limit on the number of mark and clear request is, to a degree,
31085ae065SJonathan Brassow * arbitrary. However, there is some basis for the choice in the limits
32085ae065SJonathan Brassow * imposed on the size of data payload by dm-log-userspace-transfer.c:
33085ae065SJonathan Brassow * dm_consult_userspace().
34085ae065SJonathan Brassow */
35085ae065SJonathan Brassow #define MAX_FLUSH_GROUP_COUNT 32
36085ae065SJonathan Brassow
37f5db4af4SJonthan Brassow struct log_c {
38f5db4af4SJonthan Brassow struct dm_target *ti;
395a25f0ebSJonathan E Brassow struct dm_dev *log_dev;
40f5db4af4SJonthan Brassow
41f5db4af4SJonthan Brassow char *usr_argv_str;
42f5db4af4SJonthan Brassow uint32_t usr_argc;
43f5db4af4SJonthan Brassow
44ac1f9ef2SMike Snitzer uint32_t region_size;
45ac1f9ef2SMike Snitzer region_t region_count;
46ac1f9ef2SMike Snitzer uint64_t luid;
47ac1f9ef2SMike Snitzer char uuid[DM_UUID_LEN];
48f5db4af4SJonthan Brassow
49909cc4fbSJonathan Brassow /*
50909cc4fbSJonathan Brassow * Mark and clear requests are held until a flush is issued
51909cc4fbSJonathan Brassow * so that we can group, and thereby limit, the amount of
52909cc4fbSJonathan Brassow * network traffic between kernel and userspace. The 'flush_lock'
53909cc4fbSJonathan Brassow * is used to protect these lists.
54909cc4fbSJonathan Brassow */
55f5db4af4SJonthan Brassow spinlock_t flush_lock;
56909cc4fbSJonathan Brassow struct list_head mark_list;
57909cc4fbSJonathan Brassow struct list_head clear_list;
585066a4dfSDongmao Zhang
595066a4dfSDongmao Zhang /*
60ac1f9ef2SMike Snitzer * in_sync_hint gets set when doing is_remote_recovering. It
61ac1f9ef2SMike Snitzer * represents the first region that needs recovery. IOW, the
62ac1f9ef2SMike Snitzer * first zero bit of sync_bits. This can be useful for to limit
63ac1f9ef2SMike Snitzer * traffic for calls like is_remote_recovering and get_resync_work,
64ac1f9ef2SMike Snitzer * but be take care in its use for anything else.
65ac1f9ef2SMike Snitzer */
66ac1f9ef2SMike Snitzer uint64_t in_sync_hint;
67ac1f9ef2SMike Snitzer
68ac1f9ef2SMike Snitzer /*
695066a4dfSDongmao Zhang * Workqueue for flush of clear region requests.
705066a4dfSDongmao Zhang */
715066a4dfSDongmao Zhang struct workqueue_struct *dmlog_wq;
725066a4dfSDongmao Zhang struct delayed_work flush_log_work;
735066a4dfSDongmao Zhang atomic_t sched_flush;
745066a4dfSDongmao Zhang
755066a4dfSDongmao Zhang /*
765066a4dfSDongmao Zhang * Combine userspace flush and mark requests for efficiency.
775066a4dfSDongmao Zhang */
785066a4dfSDongmao Zhang uint32_t integrated_flush;
79ac1f9ef2SMike Snitzer
806f1c819cSKent Overstreet mempool_t flush_entry_pool;
81f5db4af4SJonthan Brassow };
82f5db4af4SJonthan Brassow
83ac1f9ef2SMike Snitzer static struct kmem_cache *_flush_entry_cache;
84f5db4af4SJonthan Brassow
userspace_do_request(struct log_c * lc,const char * uuid,int request_type,char * data,size_t data_size,char * rdata,size_t * rdata_size)85f5db4af4SJonthan Brassow static int userspace_do_request(struct log_c *lc, const char *uuid,
86f5db4af4SJonthan Brassow int request_type, char *data, size_t data_size,
87f5db4af4SJonthan Brassow char *rdata, size_t *rdata_size)
88f5db4af4SJonthan Brassow {
89f5db4af4SJonthan Brassow int r;
90f5db4af4SJonthan Brassow
91f5db4af4SJonthan Brassow /*
92f5db4af4SJonthan Brassow * If the server isn't there, -ESRCH is returned,
93f5db4af4SJonthan Brassow * and we must keep trying until the server is
94f5db4af4SJonthan Brassow * restored.
95f5db4af4SJonthan Brassow */
96f5db4af4SJonthan Brassow retry:
977ec23d50SJonathan Brassow r = dm_consult_userspace(uuid, lc->luid, request_type, data,
98f5db4af4SJonthan Brassow data_size, rdata, rdata_size);
99f5db4af4SJonthan Brassow
100f5db4af4SJonthan Brassow if (r != -ESRCH)
101f5db4af4SJonthan Brassow return r;
102f5db4af4SJonthan Brassow
103f5db4af4SJonthan Brassow DMERR(" Userspace log server not found.");
104f5db4af4SJonthan Brassow while (1) {
105f5db4af4SJonthan Brassow set_current_state(TASK_INTERRUPTIBLE);
106f5db4af4SJonthan Brassow schedule_timeout(2*HZ);
107f5db4af4SJonthan Brassow DMWARN("Attempting to contact userspace log server...");
1087ec23d50SJonathan Brassow r = dm_consult_userspace(uuid, lc->luid, DM_ULOG_CTR,
1097ec23d50SJonathan Brassow lc->usr_argv_str,
110f5db4af4SJonthan Brassow strlen(lc->usr_argv_str) + 1,
111f5db4af4SJonthan Brassow NULL, NULL);
112f5db4af4SJonthan Brassow if (!r)
113f5db4af4SJonthan Brassow break;
114f5db4af4SJonthan Brassow }
115f5db4af4SJonthan Brassow DMINFO("Reconnected to userspace log server... DM_ULOG_CTR complete");
1167ec23d50SJonathan Brassow r = dm_consult_userspace(uuid, lc->luid, DM_ULOG_RESUME, NULL,
117f5db4af4SJonthan Brassow 0, NULL, NULL);
118f5db4af4SJonthan Brassow if (!r)
119f5db4af4SJonthan Brassow goto retry;
120f5db4af4SJonthan Brassow
121f5db4af4SJonthan Brassow DMERR("Error trying to resume userspace log: %d", r);
122f5db4af4SJonthan Brassow
123f5db4af4SJonthan Brassow return -ESRCH;
124f5db4af4SJonthan Brassow }
125f5db4af4SJonthan Brassow
build_constructor_string(struct dm_target * ti,unsigned int argc,char ** argv,char ** ctr_str)126f5db4af4SJonthan Brassow static int build_constructor_string(struct dm_target *ti,
127*86a3238cSHeinz Mauelshagen unsigned int argc, char **argv,
128f5db4af4SJonthan Brassow char **ctr_str)
129f5db4af4SJonthan Brassow {
130f5db4af4SJonthan Brassow int i, str_size;
131f5db4af4SJonthan Brassow char *str = NULL;
132f5db4af4SJonthan Brassow
133f5db4af4SJonthan Brassow *ctr_str = NULL;
134f5db4af4SJonthan Brassow
1355066a4dfSDongmao Zhang /*
1365066a4dfSDongmao Zhang * Determine overall size of the string.
1375066a4dfSDongmao Zhang */
138f5db4af4SJonthan Brassow for (i = 0, str_size = 0; i < argc; i++)
139f5db4af4SJonthan Brassow str_size += strlen(argv[i]) + 1; /* +1 for space between args */
140f5db4af4SJonthan Brassow
141f5db4af4SJonthan Brassow str_size += 20; /* Max number of chars in a printed u64 number */
142f5db4af4SJonthan Brassow
143f5db4af4SJonthan Brassow str = kzalloc(str_size, GFP_KERNEL);
144f5db4af4SJonthan Brassow if (!str) {
145f5db4af4SJonthan Brassow DMWARN("Unable to allocate memory for constructor string");
146f5db4af4SJonthan Brassow return -ENOMEM;
147f5db4af4SJonthan Brassow }
148f5db4af4SJonthan Brassow
149b8313b6dSJonathan Brassow str_size = sprintf(str, "%llu", (unsigned long long)ti->len);
150b8313b6dSJonathan Brassow for (i = 0; i < argc; i++)
151f5db4af4SJonthan Brassow str_size += sprintf(str + str_size, " %s", argv[i]);
152f5db4af4SJonthan Brassow
153f5db4af4SJonthan Brassow *ctr_str = str;
154f5db4af4SJonthan Brassow return str_size;
155f5db4af4SJonthan Brassow }
156f5db4af4SJonthan Brassow
do_flush(struct work_struct * work)1575066a4dfSDongmao Zhang static void do_flush(struct work_struct *work)
1585066a4dfSDongmao Zhang {
1595066a4dfSDongmao Zhang int r;
1605066a4dfSDongmao Zhang struct log_c *lc = container_of(work, struct log_c, flush_log_work.work);
1615066a4dfSDongmao Zhang
1625066a4dfSDongmao Zhang atomic_set(&lc->sched_flush, 0);
1635066a4dfSDongmao Zhang
1645066a4dfSDongmao Zhang r = userspace_do_request(lc, lc->uuid, DM_ULOG_FLUSH, NULL, 0, NULL, NULL);
1655066a4dfSDongmao Zhang
1665066a4dfSDongmao Zhang if (r)
1675066a4dfSDongmao Zhang dm_table_event(lc->ti->table);
1685066a4dfSDongmao Zhang }
1695066a4dfSDongmao Zhang
170f5db4af4SJonthan Brassow /*
171f5db4af4SJonthan Brassow * userspace_ctr
172f5db4af4SJonthan Brassow *
173f5db4af4SJonthan Brassow * argv contains:
1745066a4dfSDongmao Zhang * <UUID> [integrated_flush] <other args>
1755066a4dfSDongmao Zhang * Where 'other args' are the userspace implementation-specific log
1765066a4dfSDongmao Zhang * arguments.
177f5db4af4SJonthan Brassow *
1785066a4dfSDongmao Zhang * Example:
1795066a4dfSDongmao Zhang * <UUID> [integrated_flush] clustered-disk <arg count> <log dev>
1805066a4dfSDongmao Zhang * <region_size> [[no]sync]
1815066a4dfSDongmao Zhang *
1825066a4dfSDongmao Zhang * This module strips off the <UUID> and uses it for identification
1835066a4dfSDongmao Zhang * purposes when communicating with userspace about a log.
1845066a4dfSDongmao Zhang *
1855066a4dfSDongmao Zhang * If integrated_flush is defined, the kernel combines flush
1865066a4dfSDongmao Zhang * and mark requests.
1875066a4dfSDongmao Zhang *
1885066a4dfSDongmao Zhang * The rest of the line, beginning with 'clustered-disk', is passed
1895066a4dfSDongmao Zhang * to the userspace ctr function.
190f5db4af4SJonthan Brassow */
userspace_ctr(struct dm_dirty_log * log,struct dm_target * ti,unsigned int argc,char ** argv)191f5db4af4SJonthan Brassow static int userspace_ctr(struct dm_dirty_log *log, struct dm_target *ti,
192*86a3238cSHeinz Mauelshagen unsigned int argc, char **argv)
193f5db4af4SJonthan Brassow {
194f5db4af4SJonthan Brassow int r = 0;
195f5db4af4SJonthan Brassow int str_size;
196f5db4af4SJonthan Brassow char *ctr_str = NULL;
197f5db4af4SJonthan Brassow struct log_c *lc = NULL;
198f5db4af4SJonthan Brassow uint64_t rdata;
199f5db4af4SJonthan Brassow size_t rdata_size = sizeof(rdata);
2005a25f0ebSJonathan E Brassow char *devices_rdata = NULL;
2015a25f0ebSJonathan E Brassow size_t devices_rdata_size = DM_NAME_LEN;
202f5db4af4SJonthan Brassow
203f5db4af4SJonthan Brassow if (argc < 3) {
204f5db4af4SJonthan Brassow DMWARN("Too few arguments to userspace dirty log");
205f5db4af4SJonthan Brassow return -EINVAL;
206f5db4af4SJonthan Brassow }
207f5db4af4SJonthan Brassow
2085a25f0ebSJonathan E Brassow lc = kzalloc(sizeof(*lc), GFP_KERNEL);
209f5db4af4SJonthan Brassow if (!lc) {
210f5db4af4SJonthan Brassow DMWARN("Unable to allocate userspace log context.");
211f5db4af4SJonthan Brassow return -ENOMEM;
212f5db4af4SJonthan Brassow }
213f5db4af4SJonthan Brassow
2147ec23d50SJonathan Brassow /* The ptr value is sufficient for local unique id */
215bca915aaSAndrew Morton lc->luid = (unsigned long)lc;
2167ec23d50SJonathan Brassow
217f5db4af4SJonthan Brassow lc->ti = ti;
218f5db4af4SJonthan Brassow
219f5db4af4SJonthan Brassow if (strlen(argv[0]) > (DM_UUID_LEN - 1)) {
220f5db4af4SJonthan Brassow DMWARN("UUID argument too long.");
221f5db4af4SJonthan Brassow kfree(lc);
222f5db4af4SJonthan Brassow return -EINVAL;
223f5db4af4SJonthan Brassow }
224f5db4af4SJonthan Brassow
2255066a4dfSDongmao Zhang lc->usr_argc = argc;
2265066a4dfSDongmao Zhang
227f5db4af4SJonthan Brassow strncpy(lc->uuid, argv[0], DM_UUID_LEN);
2285066a4dfSDongmao Zhang argc--;
2295066a4dfSDongmao Zhang argv++;
230f5db4af4SJonthan Brassow spin_lock_init(&lc->flush_lock);
231909cc4fbSJonathan Brassow INIT_LIST_HEAD(&lc->mark_list);
232909cc4fbSJonathan Brassow INIT_LIST_HEAD(&lc->clear_list);
233f5db4af4SJonthan Brassow
2345066a4dfSDongmao Zhang if (!strcasecmp(argv[0], "integrated_flush")) {
2355066a4dfSDongmao Zhang lc->integrated_flush = 1;
2365066a4dfSDongmao Zhang argc--;
2375066a4dfSDongmao Zhang argv++;
2385066a4dfSDongmao Zhang }
2395066a4dfSDongmao Zhang
2405066a4dfSDongmao Zhang str_size = build_constructor_string(ti, argc, argv, &ctr_str);
241f5db4af4SJonthan Brassow if (str_size < 0) {
242f5db4af4SJonthan Brassow kfree(lc);
243f5db4af4SJonthan Brassow return str_size;
244f5db4af4SJonthan Brassow }
245f5db4af4SJonthan Brassow
2465a25f0ebSJonathan E Brassow devices_rdata = kzalloc(devices_rdata_size, GFP_KERNEL);
2475a25f0ebSJonathan E Brassow if (!devices_rdata) {
2485a25f0ebSJonathan E Brassow DMERR("Failed to allocate memory for device information");
2495a25f0ebSJonathan E Brassow r = -ENOMEM;
2505a25f0ebSJonathan E Brassow goto out;
2515a25f0ebSJonathan E Brassow }
2525a25f0ebSJonathan E Brassow
2536f1c819cSKent Overstreet r = mempool_init_slab_pool(&lc->flush_entry_pool, FLUSH_ENTRY_POOL_SIZE,
254ac1f9ef2SMike Snitzer _flush_entry_cache);
2556f1c819cSKent Overstreet if (r) {
256ac1f9ef2SMike Snitzer DMERR("Failed to create flush_entry_pool");
257ac1f9ef2SMike Snitzer goto out;
258ac1f9ef2SMike Snitzer }
259ac1f9ef2SMike Snitzer
2605a25f0ebSJonathan E Brassow /*
2615a25f0ebSJonathan E Brassow * Send table string and get back any opened device.
2625a25f0ebSJonathan E Brassow */
2637ec23d50SJonathan Brassow r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_CTR,
2645a25f0ebSJonathan E Brassow ctr_str, str_size,
2655a25f0ebSJonathan E Brassow devices_rdata, &devices_rdata_size);
266f5db4af4SJonthan Brassow
2674a038677SJonathan Brassow if (r < 0) {
2684a038677SJonathan Brassow if (r == -ESRCH)
269f5db4af4SJonthan Brassow DMERR("Userspace log server not found");
2704a038677SJonathan Brassow else
2714a038677SJonathan Brassow DMERR("Userspace log server failed to create log");
272f5db4af4SJonthan Brassow goto out;
273f5db4af4SJonthan Brassow }
274f5db4af4SJonthan Brassow
275f5db4af4SJonthan Brassow /* Since the region size does not change, get it now */
276f5db4af4SJonthan Brassow rdata_size = sizeof(rdata);
2777ec23d50SJonathan Brassow r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_GET_REGION_SIZE,
278f5db4af4SJonthan Brassow NULL, 0, (char *)&rdata, &rdata_size);
279f5db4af4SJonthan Brassow
280f5db4af4SJonthan Brassow if (r) {
281f5db4af4SJonthan Brassow DMERR("Failed to get region size of dirty log");
282f5db4af4SJonthan Brassow goto out;
283f5db4af4SJonthan Brassow }
284f5db4af4SJonthan Brassow
285f5db4af4SJonthan Brassow lc->region_size = (uint32_t)rdata;
286f5db4af4SJonthan Brassow lc->region_count = dm_sector_div_up(ti->len, lc->region_size);
287f5db4af4SJonthan Brassow
2885a25f0ebSJonathan E Brassow if (devices_rdata_size) {
2895a25f0ebSJonathan E Brassow if (devices_rdata[devices_rdata_size - 1] != '\0') {
2905a25f0ebSJonathan E Brassow DMERR("DM_ULOG_CTR device return string not properly terminated");
2915a25f0ebSJonathan E Brassow r = -EINVAL;
2925a25f0ebSJonathan E Brassow goto out;
2935a25f0ebSJonathan E Brassow }
2945a25f0ebSJonathan E Brassow r = dm_get_device(ti, devices_rdata,
2955a25f0ebSJonathan E Brassow dm_table_get_mode(ti->table), &lc->log_dev);
2965a25f0ebSJonathan E Brassow if (r)
2975a25f0ebSJonathan E Brassow DMERR("Failed to register %s with device-mapper",
2985a25f0ebSJonathan E Brassow devices_rdata);
2995a25f0ebSJonathan E Brassow }
3005066a4dfSDongmao Zhang
3015066a4dfSDongmao Zhang if (lc->integrated_flush) {
3025066a4dfSDongmao Zhang lc->dmlog_wq = alloc_workqueue("dmlogd", WQ_MEM_RECLAIM, 0);
3035066a4dfSDongmao Zhang if (!lc->dmlog_wq) {
3045066a4dfSDongmao Zhang DMERR("couldn't start dmlogd");
3055066a4dfSDongmao Zhang r = -ENOMEM;
3065066a4dfSDongmao Zhang goto out;
3075066a4dfSDongmao Zhang }
3085066a4dfSDongmao Zhang
3095066a4dfSDongmao Zhang INIT_DELAYED_WORK(&lc->flush_log_work, do_flush);
3105066a4dfSDongmao Zhang atomic_set(&lc->sched_flush, 0);
3115066a4dfSDongmao Zhang }
3125066a4dfSDongmao Zhang
313f5db4af4SJonthan Brassow out:
3145a25f0ebSJonathan E Brassow kfree(devices_rdata);
315f5db4af4SJonthan Brassow if (r) {
3166f1c819cSKent Overstreet mempool_exit(&lc->flush_entry_pool);
317f5db4af4SJonthan Brassow kfree(lc);
318f5db4af4SJonthan Brassow kfree(ctr_str);
319f5db4af4SJonthan Brassow } else {
320f5db4af4SJonthan Brassow lc->usr_argv_str = ctr_str;
321f5db4af4SJonthan Brassow log->context = lc;
322f5db4af4SJonthan Brassow }
323f5db4af4SJonthan Brassow
324f5db4af4SJonthan Brassow return r;
325f5db4af4SJonthan Brassow }
326f5db4af4SJonthan Brassow
userspace_dtr(struct dm_dirty_log * log)327f5db4af4SJonthan Brassow static void userspace_dtr(struct dm_dirty_log *log)
328f5db4af4SJonthan Brassow {
329f5db4af4SJonthan Brassow struct log_c *lc = log->context;
330f5db4af4SJonthan Brassow
3315066a4dfSDongmao Zhang if (lc->integrated_flush) {
3325066a4dfSDongmao Zhang /* flush workqueue */
3335066a4dfSDongmao Zhang if (atomic_read(&lc->sched_flush))
3345066a4dfSDongmao Zhang flush_delayed_work(&lc->flush_log_work);
3355066a4dfSDongmao Zhang
3365066a4dfSDongmao Zhang destroy_workqueue(lc->dmlog_wq);
3375066a4dfSDongmao Zhang }
3385066a4dfSDongmao Zhang
3394a038677SJonathan Brassow (void) dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_DTR,
3405066a4dfSDongmao Zhang NULL, 0, NULL, NULL);
341f5db4af4SJonthan Brassow
3425a25f0ebSJonathan E Brassow if (lc->log_dev)
3435a25f0ebSJonathan E Brassow dm_put_device(lc->ti, lc->log_dev);
3445a25f0ebSJonathan E Brassow
3456f1c819cSKent Overstreet mempool_exit(&lc->flush_entry_pool);
346ac1f9ef2SMike Snitzer
347f5db4af4SJonthan Brassow kfree(lc->usr_argv_str);
348f5db4af4SJonthan Brassow kfree(lc);
349f5db4af4SJonthan Brassow }
350f5db4af4SJonthan Brassow
userspace_presuspend(struct dm_dirty_log * log)351f5db4af4SJonthan Brassow static int userspace_presuspend(struct dm_dirty_log *log)
352f5db4af4SJonthan Brassow {
353f5db4af4SJonthan Brassow int r;
354f5db4af4SJonthan Brassow struct log_c *lc = log->context;
355f5db4af4SJonthan Brassow
3567ec23d50SJonathan Brassow r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_PRESUSPEND,
3575066a4dfSDongmao Zhang NULL, 0, NULL, NULL);
358f5db4af4SJonthan Brassow
359f5db4af4SJonthan Brassow return r;
360f5db4af4SJonthan Brassow }
361f5db4af4SJonthan Brassow
userspace_postsuspend(struct dm_dirty_log * log)362f5db4af4SJonthan Brassow static int userspace_postsuspend(struct dm_dirty_log *log)
363f5db4af4SJonthan Brassow {
364f5db4af4SJonthan Brassow int r;
365f5db4af4SJonthan Brassow struct log_c *lc = log->context;
366f5db4af4SJonthan Brassow
3675066a4dfSDongmao Zhang /*
3685066a4dfSDongmao Zhang * Run planned flush earlier.
3695066a4dfSDongmao Zhang */
3705066a4dfSDongmao Zhang if (lc->integrated_flush && atomic_read(&lc->sched_flush))
3715066a4dfSDongmao Zhang flush_delayed_work(&lc->flush_log_work);
3725066a4dfSDongmao Zhang
3737ec23d50SJonathan Brassow r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_POSTSUSPEND,
3745066a4dfSDongmao Zhang NULL, 0, NULL, NULL);
375f5db4af4SJonthan Brassow
376f5db4af4SJonthan Brassow return r;
377f5db4af4SJonthan Brassow }
378f5db4af4SJonthan Brassow
userspace_resume(struct dm_dirty_log * log)379f5db4af4SJonthan Brassow static int userspace_resume(struct dm_dirty_log *log)
380f5db4af4SJonthan Brassow {
381f5db4af4SJonthan Brassow int r;
382f5db4af4SJonthan Brassow struct log_c *lc = log->context;
383f5db4af4SJonthan Brassow
384f5db4af4SJonthan Brassow lc->in_sync_hint = 0;
3857ec23d50SJonathan Brassow r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_RESUME,
3865066a4dfSDongmao Zhang NULL, 0, NULL, NULL);
387f5db4af4SJonthan Brassow
388f5db4af4SJonthan Brassow return r;
389f5db4af4SJonthan Brassow }
390f5db4af4SJonthan Brassow
userspace_get_region_size(struct dm_dirty_log * log)391f5db4af4SJonthan Brassow static uint32_t userspace_get_region_size(struct dm_dirty_log *log)
392f5db4af4SJonthan Brassow {
393f5db4af4SJonthan Brassow struct log_c *lc = log->context;
394f5db4af4SJonthan Brassow
395f5db4af4SJonthan Brassow return lc->region_size;
396f5db4af4SJonthan Brassow }
397f5db4af4SJonthan Brassow
398f5db4af4SJonthan Brassow /*
399f5db4af4SJonthan Brassow * userspace_is_clean
400f5db4af4SJonthan Brassow *
401f5db4af4SJonthan Brassow * Check whether a region is clean. If there is any sort of
402f5db4af4SJonthan Brassow * failure when consulting the server, we return not clean.
403f5db4af4SJonthan Brassow *
404f5db4af4SJonthan Brassow * Returns: 1 if clean, 0 otherwise
405f5db4af4SJonthan Brassow */
userspace_is_clean(struct dm_dirty_log * log,region_t region)406f5db4af4SJonthan Brassow static int userspace_is_clean(struct dm_dirty_log *log, region_t region)
407f5db4af4SJonthan Brassow {
408f5db4af4SJonthan Brassow int r;
409f5db4af4SJonthan Brassow uint64_t region64 = (uint64_t)region;
410f5db4af4SJonthan Brassow int64_t is_clean;
411f5db4af4SJonthan Brassow size_t rdata_size;
412f5db4af4SJonthan Brassow struct log_c *lc = log->context;
413f5db4af4SJonthan Brassow
414f5db4af4SJonthan Brassow rdata_size = sizeof(is_clean);
415f5db4af4SJonthan Brassow r = userspace_do_request(lc, lc->uuid, DM_ULOG_IS_CLEAN,
416f5db4af4SJonthan Brassow (char *)®ion64, sizeof(region64),
417f5db4af4SJonthan Brassow (char *)&is_clean, &rdata_size);
418f5db4af4SJonthan Brassow
419f5db4af4SJonthan Brassow return (r) ? 0 : (int)is_clean;
420f5db4af4SJonthan Brassow }
421f5db4af4SJonthan Brassow
422f5db4af4SJonthan Brassow /*
423f5db4af4SJonthan Brassow * userspace_in_sync
424f5db4af4SJonthan Brassow *
425f5db4af4SJonthan Brassow * Check if the region is in-sync. If there is any sort
426f5db4af4SJonthan Brassow * of failure when consulting the server, we assume that
427f5db4af4SJonthan Brassow * the region is not in sync.
428f5db4af4SJonthan Brassow *
429f5db4af4SJonthan Brassow * If 'can_block' is set, return immediately
430f5db4af4SJonthan Brassow *
431f5db4af4SJonthan Brassow * Returns: 1 if in-sync, 0 if not-in-sync, -EWOULDBLOCK
432f5db4af4SJonthan Brassow */
userspace_in_sync(struct dm_dirty_log * log,region_t region,int can_block)433f5db4af4SJonthan Brassow static int userspace_in_sync(struct dm_dirty_log *log, region_t region,
434f5db4af4SJonthan Brassow int can_block)
435f5db4af4SJonthan Brassow {
436f5db4af4SJonthan Brassow int r;
437f5db4af4SJonthan Brassow uint64_t region64 = region;
438f5db4af4SJonthan Brassow int64_t in_sync;
439f5db4af4SJonthan Brassow size_t rdata_size;
440f5db4af4SJonthan Brassow struct log_c *lc = log->context;
441f5db4af4SJonthan Brassow
442f5db4af4SJonthan Brassow /*
443f5db4af4SJonthan Brassow * We can never respond directly - even if in_sync_hint is
444f5db4af4SJonthan Brassow * set. This is because another machine could see a device
445f5db4af4SJonthan Brassow * failure and mark the region out-of-sync. If we don't go
446f5db4af4SJonthan Brassow * to userspace to ask, we might think the region is in-sync
447f5db4af4SJonthan Brassow * and allow a read to pick up data that is stale. (This is
448f5db4af4SJonthan Brassow * very unlikely if a device actually fails; but it is very
449f5db4af4SJonthan Brassow * likely if a connection to one device from one machine fails.)
450f5db4af4SJonthan Brassow *
451f5db4af4SJonthan Brassow * There still might be a problem if the mirror caches the region
452f5db4af4SJonthan Brassow * state as in-sync... but then this call would not be made. So,
453f5db4af4SJonthan Brassow * that is a mirror problem.
454f5db4af4SJonthan Brassow */
455f5db4af4SJonthan Brassow if (!can_block)
456f5db4af4SJonthan Brassow return -EWOULDBLOCK;
457f5db4af4SJonthan Brassow
458f5db4af4SJonthan Brassow rdata_size = sizeof(in_sync);
459f5db4af4SJonthan Brassow r = userspace_do_request(lc, lc->uuid, DM_ULOG_IN_SYNC,
460f5db4af4SJonthan Brassow (char *)®ion64, sizeof(region64),
461f5db4af4SJonthan Brassow (char *)&in_sync, &rdata_size);
462f5db4af4SJonthan Brassow return (r) ? 0 : (int)in_sync;
463f5db4af4SJonthan Brassow }
464f5db4af4SJonthan Brassow
flush_one_by_one(struct log_c * lc,struct list_head * flush_list)465085ae065SJonathan Brassow static int flush_one_by_one(struct log_c *lc, struct list_head *flush_list)
466085ae065SJonathan Brassow {
467085ae065SJonathan Brassow int r = 0;
468ac1f9ef2SMike Snitzer struct dm_dirty_log_flush_entry *fe;
469085ae065SJonathan Brassow
470085ae065SJonathan Brassow list_for_each_entry(fe, flush_list, list) {
471085ae065SJonathan Brassow r = userspace_do_request(lc, lc->uuid, fe->type,
472085ae065SJonathan Brassow (char *)&fe->region,
473085ae065SJonathan Brassow sizeof(fe->region),
474085ae065SJonathan Brassow NULL, NULL);
475085ae065SJonathan Brassow if (r)
476085ae065SJonathan Brassow break;
477085ae065SJonathan Brassow }
478085ae065SJonathan Brassow
479085ae065SJonathan Brassow return r;
480085ae065SJonathan Brassow }
481085ae065SJonathan Brassow
flush_by_group(struct log_c * lc,struct list_head * flush_list,int flush_with_payload)4825066a4dfSDongmao Zhang static int flush_by_group(struct log_c *lc, struct list_head *flush_list,
4835066a4dfSDongmao Zhang int flush_with_payload)
484085ae065SJonathan Brassow {
485085ae065SJonathan Brassow int r = 0;
486085ae065SJonathan Brassow int count;
487085ae065SJonathan Brassow uint32_t type = 0;
488ac1f9ef2SMike Snitzer struct dm_dirty_log_flush_entry *fe, *tmp_fe;
489085ae065SJonathan Brassow LIST_HEAD(tmp_list);
490085ae065SJonathan Brassow uint64_t group[MAX_FLUSH_GROUP_COUNT];
491085ae065SJonathan Brassow
492085ae065SJonathan Brassow /*
493085ae065SJonathan Brassow * Group process the requests
494085ae065SJonathan Brassow */
495085ae065SJonathan Brassow while (!list_empty(flush_list)) {
496085ae065SJonathan Brassow count = 0;
497085ae065SJonathan Brassow
498085ae065SJonathan Brassow list_for_each_entry_safe(fe, tmp_fe, flush_list, list) {
499085ae065SJonathan Brassow group[count] = fe->region;
500085ae065SJonathan Brassow count++;
501085ae065SJonathan Brassow
5026c9b27abSKirill A. Shutemov list_move(&fe->list, &tmp_list);
503085ae065SJonathan Brassow
504085ae065SJonathan Brassow type = fe->type;
505085ae065SJonathan Brassow if (count >= MAX_FLUSH_GROUP_COUNT)
506085ae065SJonathan Brassow break;
507085ae065SJonathan Brassow }
508085ae065SJonathan Brassow
5095066a4dfSDongmao Zhang if (flush_with_payload) {
5105066a4dfSDongmao Zhang r = userspace_do_request(lc, lc->uuid, DM_ULOG_FLUSH,
5115066a4dfSDongmao Zhang (char *)(group),
5125066a4dfSDongmao Zhang count * sizeof(uint64_t),
5135066a4dfSDongmao Zhang NULL, NULL);
5145066a4dfSDongmao Zhang /*
5155066a4dfSDongmao Zhang * Integrated flush failed.
5165066a4dfSDongmao Zhang */
5175066a4dfSDongmao Zhang if (r)
5185066a4dfSDongmao Zhang break;
5195066a4dfSDongmao Zhang } else {
520085ae065SJonathan Brassow r = userspace_do_request(lc, lc->uuid, type,
521085ae065SJonathan Brassow (char *)(group),
522085ae065SJonathan Brassow count * sizeof(uint64_t),
523085ae065SJonathan Brassow NULL, NULL);
524085ae065SJonathan Brassow if (r) {
5255066a4dfSDongmao Zhang /*
5265066a4dfSDongmao Zhang * Group send failed. Attempt one-by-one.
5275066a4dfSDongmao Zhang */
528085ae065SJonathan Brassow list_splice_init(&tmp_list, flush_list);
529085ae065SJonathan Brassow r = flush_one_by_one(lc, flush_list);
530085ae065SJonathan Brassow break;
531085ae065SJonathan Brassow }
532085ae065SJonathan Brassow }
5335066a4dfSDongmao Zhang }
534085ae065SJonathan Brassow
535085ae065SJonathan Brassow /*
536085ae065SJonathan Brassow * Must collect flush_entrys that were successfully processed
537085ae065SJonathan Brassow * as a group so that they will be free'd by the caller.
538085ae065SJonathan Brassow */
539085ae065SJonathan Brassow list_splice_init(&tmp_list, flush_list);
540085ae065SJonathan Brassow
541085ae065SJonathan Brassow return r;
542085ae065SJonathan Brassow }
543085ae065SJonathan Brassow
544f5db4af4SJonthan Brassow /*
545f5db4af4SJonthan Brassow * userspace_flush
546f5db4af4SJonthan Brassow *
547f5db4af4SJonthan Brassow * This function is ok to block.
548f5db4af4SJonthan Brassow * The flush happens in two stages. First, it sends all
549f5db4af4SJonthan Brassow * clear/mark requests that are on the list. Then it
550f5db4af4SJonthan Brassow * tells the server to commit them. This gives the
551f5db4af4SJonthan Brassow * server a chance to optimise the commit, instead of
552f5db4af4SJonthan Brassow * doing it for every request.
553f5db4af4SJonthan Brassow *
554f5db4af4SJonthan Brassow * Additionally, we could implement another thread that
555f5db4af4SJonthan Brassow * sends the requests up to the server - reducing the
556f5db4af4SJonthan Brassow * load on flush. Then the flush would have less in
557f5db4af4SJonthan Brassow * the list and be responsible for the finishing commit.
558f5db4af4SJonthan Brassow *
559f5db4af4SJonthan Brassow * Returns: 0 on success, < 0 on failure
560f5db4af4SJonthan Brassow */
userspace_flush(struct dm_dirty_log * log)561f5db4af4SJonthan Brassow static int userspace_flush(struct dm_dirty_log *log)
562f5db4af4SJonthan Brassow {
563f5db4af4SJonthan Brassow int r = 0;
564f5db4af4SJonthan Brassow unsigned long flags;
565f5db4af4SJonthan Brassow struct log_c *lc = log->context;
566909cc4fbSJonathan Brassow LIST_HEAD(mark_list);
567909cc4fbSJonathan Brassow LIST_HEAD(clear_list);
5685066a4dfSDongmao Zhang int mark_list_is_empty;
5695066a4dfSDongmao Zhang int clear_list_is_empty;
570ac1f9ef2SMike Snitzer struct dm_dirty_log_flush_entry *fe, *tmp_fe;
5716f1c819cSKent Overstreet mempool_t *flush_entry_pool = &lc->flush_entry_pool;
572f5db4af4SJonthan Brassow
573f5db4af4SJonthan Brassow spin_lock_irqsave(&lc->flush_lock, flags);
574909cc4fbSJonathan Brassow list_splice_init(&lc->mark_list, &mark_list);
575909cc4fbSJonathan Brassow list_splice_init(&lc->clear_list, &clear_list);
576f5db4af4SJonthan Brassow spin_unlock_irqrestore(&lc->flush_lock, flags);
577f5db4af4SJonthan Brassow
5785066a4dfSDongmao Zhang mark_list_is_empty = list_empty(&mark_list);
5795066a4dfSDongmao Zhang clear_list_is_empty = list_empty(&clear_list);
5805066a4dfSDongmao Zhang
5815066a4dfSDongmao Zhang if (mark_list_is_empty && clear_list_is_empty)
582f5db4af4SJonthan Brassow return 0;
583f5db4af4SJonthan Brassow
5845066a4dfSDongmao Zhang r = flush_by_group(lc, &clear_list, 0);
585909cc4fbSJonathan Brassow if (r)
5865066a4dfSDongmao Zhang goto out;
587909cc4fbSJonathan Brassow
5885066a4dfSDongmao Zhang if (!lc->integrated_flush) {
5895066a4dfSDongmao Zhang r = flush_by_group(lc, &mark_list, 0);
590f5db4af4SJonthan Brassow if (r)
5915066a4dfSDongmao Zhang goto out;
592f5db4af4SJonthan Brassow r = userspace_do_request(lc, lc->uuid, DM_ULOG_FLUSH,
593f5db4af4SJonthan Brassow NULL, 0, NULL, NULL);
5945066a4dfSDongmao Zhang goto out;
5955066a4dfSDongmao Zhang }
596f5db4af4SJonthan Brassow
597f5db4af4SJonthan Brassow /*
5985066a4dfSDongmao Zhang * Send integrated flush request with mark_list as payload.
5995066a4dfSDongmao Zhang */
6005066a4dfSDongmao Zhang r = flush_by_group(lc, &mark_list, 1);
6015066a4dfSDongmao Zhang if (r)
6025066a4dfSDongmao Zhang goto out;
6035066a4dfSDongmao Zhang
6045066a4dfSDongmao Zhang if (mark_list_is_empty && !atomic_read(&lc->sched_flush)) {
6055066a4dfSDongmao Zhang /*
6065066a4dfSDongmao Zhang * When there are only clear region requests,
6075066a4dfSDongmao Zhang * we schedule a flush in the future.
6085066a4dfSDongmao Zhang */
6095066a4dfSDongmao Zhang queue_delayed_work(lc->dmlog_wq, &lc->flush_log_work, 3 * HZ);
6105066a4dfSDongmao Zhang atomic_set(&lc->sched_flush, 1);
6115066a4dfSDongmao Zhang } else {
6125066a4dfSDongmao Zhang /*
6135066a4dfSDongmao Zhang * Cancel pending flush because we
6145066a4dfSDongmao Zhang * have already flushed in mark_region.
6155066a4dfSDongmao Zhang */
6165066a4dfSDongmao Zhang cancel_delayed_work(&lc->flush_log_work);
6175066a4dfSDongmao Zhang atomic_set(&lc->sched_flush, 0);
6185066a4dfSDongmao Zhang }
6195066a4dfSDongmao Zhang
6205066a4dfSDongmao Zhang out:
6215066a4dfSDongmao Zhang /*
6225066a4dfSDongmao Zhang * We can safely remove these entries, even after failure.
623f5db4af4SJonthan Brassow * Calling code will receive an error and will know that
624f5db4af4SJonthan Brassow * the log facility has failed.
625f5db4af4SJonthan Brassow */
626909cc4fbSJonathan Brassow list_for_each_entry_safe(fe, tmp_fe, &mark_list, list) {
627909cc4fbSJonathan Brassow list_del(&fe->list);
628909cc4fbSJonathan Brassow mempool_free(fe, flush_entry_pool);
629909cc4fbSJonathan Brassow }
630909cc4fbSJonathan Brassow list_for_each_entry_safe(fe, tmp_fe, &clear_list, list) {
631f5db4af4SJonthan Brassow list_del(&fe->list);
632f5db4af4SJonthan Brassow mempool_free(fe, flush_entry_pool);
633f5db4af4SJonthan Brassow }
634f5db4af4SJonthan Brassow
635f5db4af4SJonthan Brassow if (r)
636f5db4af4SJonthan Brassow dm_table_event(lc->ti->table);
637f5db4af4SJonthan Brassow
638f5db4af4SJonthan Brassow return r;
639f5db4af4SJonthan Brassow }
640f5db4af4SJonthan Brassow
641f5db4af4SJonthan Brassow /*
642f5db4af4SJonthan Brassow * userspace_mark_region
643f5db4af4SJonthan Brassow *
644f5db4af4SJonthan Brassow * This function should avoid blocking unless absolutely required.
645f5db4af4SJonthan Brassow * (Memory allocation is valid for blocking.)
646f5db4af4SJonthan Brassow */
userspace_mark_region(struct dm_dirty_log * log,region_t region)647f5db4af4SJonthan Brassow static void userspace_mark_region(struct dm_dirty_log *log, region_t region)
648f5db4af4SJonthan Brassow {
649f5db4af4SJonthan Brassow unsigned long flags;
650f5db4af4SJonthan Brassow struct log_c *lc = log->context;
651ac1f9ef2SMike Snitzer struct dm_dirty_log_flush_entry *fe;
652f5db4af4SJonthan Brassow
653f5db4af4SJonthan Brassow /* Wait for an allocation, but _never_ fail */
6546f1c819cSKent Overstreet fe = mempool_alloc(&lc->flush_entry_pool, GFP_NOIO);
655f5db4af4SJonthan Brassow BUG_ON(!fe);
656f5db4af4SJonthan Brassow
657f5db4af4SJonthan Brassow spin_lock_irqsave(&lc->flush_lock, flags);
658f5db4af4SJonthan Brassow fe->type = DM_ULOG_MARK_REGION;
659f5db4af4SJonthan Brassow fe->region = region;
660909cc4fbSJonathan Brassow list_add(&fe->list, &lc->mark_list);
661f5db4af4SJonthan Brassow spin_unlock_irqrestore(&lc->flush_lock, flags);
662f5db4af4SJonthan Brassow }
663f5db4af4SJonthan Brassow
664f5db4af4SJonthan Brassow /*
665f5db4af4SJonthan Brassow * userspace_clear_region
666f5db4af4SJonthan Brassow *
667f5db4af4SJonthan Brassow * This function must not block.
668f5db4af4SJonthan Brassow * So, the alloc can't block. In the worst case, it is ok to
669f5db4af4SJonthan Brassow * fail. It would simply mean we can't clear the region.
670f5db4af4SJonthan Brassow * Does nothing to current sync context, but does mean
671f5db4af4SJonthan Brassow * the region will be re-sync'ed on a reload of the mirror
672f5db4af4SJonthan Brassow * even though it is in-sync.
673f5db4af4SJonthan Brassow */
userspace_clear_region(struct dm_dirty_log * log,region_t region)674f5db4af4SJonthan Brassow static void userspace_clear_region(struct dm_dirty_log *log, region_t region)
675f5db4af4SJonthan Brassow {
676f5db4af4SJonthan Brassow unsigned long flags;
677f5db4af4SJonthan Brassow struct log_c *lc = log->context;
678ac1f9ef2SMike Snitzer struct dm_dirty_log_flush_entry *fe;
679f5db4af4SJonthan Brassow
680f5db4af4SJonthan Brassow /*
681f5db4af4SJonthan Brassow * If we fail to allocate, we skip the clearing of
682f5db4af4SJonthan Brassow * the region. This doesn't hurt us in any way, except
683f5db4af4SJonthan Brassow * to cause the region to be resync'ed when the
684f5db4af4SJonthan Brassow * device is activated next time.
685f5db4af4SJonthan Brassow */
6866f1c819cSKent Overstreet fe = mempool_alloc(&lc->flush_entry_pool, GFP_ATOMIC);
687f5db4af4SJonthan Brassow if (!fe) {
688f5db4af4SJonthan Brassow DMERR("Failed to allocate memory to clear region.");
689f5db4af4SJonthan Brassow return;
690f5db4af4SJonthan Brassow }
691f5db4af4SJonthan Brassow
692f5db4af4SJonthan Brassow spin_lock_irqsave(&lc->flush_lock, flags);
693f5db4af4SJonthan Brassow fe->type = DM_ULOG_CLEAR_REGION;
694f5db4af4SJonthan Brassow fe->region = region;
695909cc4fbSJonathan Brassow list_add(&fe->list, &lc->clear_list);
696f5db4af4SJonthan Brassow spin_unlock_irqrestore(&lc->flush_lock, flags);
697f5db4af4SJonthan Brassow }
698f5db4af4SJonthan Brassow
699f5db4af4SJonthan Brassow /*
700f5db4af4SJonthan Brassow * userspace_get_resync_work
701f5db4af4SJonthan Brassow *
702f5db4af4SJonthan Brassow * Get a region that needs recovery. It is valid to return
703f5db4af4SJonthan Brassow * an error for this function.
704f5db4af4SJonthan Brassow *
705f5db4af4SJonthan Brassow * Returns: 1 if region filled, 0 if no work, <0 on error
706f5db4af4SJonthan Brassow */
userspace_get_resync_work(struct dm_dirty_log * log,region_t * region)707f5db4af4SJonthan Brassow static int userspace_get_resync_work(struct dm_dirty_log *log, region_t *region)
708f5db4af4SJonthan Brassow {
709f5db4af4SJonthan Brassow int r;
710f5db4af4SJonthan Brassow size_t rdata_size;
711f5db4af4SJonthan Brassow struct log_c *lc = log->context;
712f5db4af4SJonthan Brassow struct {
713f5db4af4SJonthan Brassow int64_t i; /* 64-bit for mix arch compatibility */
714f5db4af4SJonthan Brassow region_t r;
715f5db4af4SJonthan Brassow } pkg;
716f5db4af4SJonthan Brassow
717f5db4af4SJonthan Brassow if (lc->in_sync_hint >= lc->region_count)
718f5db4af4SJonthan Brassow return 0;
719f5db4af4SJonthan Brassow
720f5db4af4SJonthan Brassow rdata_size = sizeof(pkg);
721f5db4af4SJonthan Brassow r = userspace_do_request(lc, lc->uuid, DM_ULOG_GET_RESYNC_WORK,
7225066a4dfSDongmao Zhang NULL, 0, (char *)&pkg, &rdata_size);
723f5db4af4SJonthan Brassow
724f5db4af4SJonthan Brassow *region = pkg.r;
725f5db4af4SJonthan Brassow return (r) ? r : (int)pkg.i;
726f5db4af4SJonthan Brassow }
727f5db4af4SJonthan Brassow
728f5db4af4SJonthan Brassow /*
729f5db4af4SJonthan Brassow * userspace_set_region_sync
730f5db4af4SJonthan Brassow *
731f5db4af4SJonthan Brassow * Set the sync status of a given region. This function
732f5db4af4SJonthan Brassow * must not fail.
733f5db4af4SJonthan Brassow */
userspace_set_region_sync(struct dm_dirty_log * log,region_t region,int in_sync)734f5db4af4SJonthan Brassow static void userspace_set_region_sync(struct dm_dirty_log *log,
735f5db4af4SJonthan Brassow region_t region, int in_sync)
736f5db4af4SJonthan Brassow {
737f5db4af4SJonthan Brassow struct log_c *lc = log->context;
738f5db4af4SJonthan Brassow struct {
739f5db4af4SJonthan Brassow region_t r;
740f5db4af4SJonthan Brassow int64_t i;
741f5db4af4SJonthan Brassow } pkg;
742f5db4af4SJonthan Brassow
743f5db4af4SJonthan Brassow pkg.r = region;
744f5db4af4SJonthan Brassow pkg.i = (int64_t)in_sync;
745f5db4af4SJonthan Brassow
74618cc980aSNicholas Mc Guire (void) userspace_do_request(lc, lc->uuid, DM_ULOG_SET_REGION_SYNC,
7475066a4dfSDongmao Zhang (char *)&pkg, sizeof(pkg), NULL, NULL);
748f5db4af4SJonthan Brassow
749f5db4af4SJonthan Brassow /*
750f5db4af4SJonthan Brassow * It would be nice to be able to report failures.
75118cc980aSNicholas Mc Guire * However, it is easy enough to detect and resolve.
752f5db4af4SJonthan Brassow */
753f5db4af4SJonthan Brassow }
754f5db4af4SJonthan Brassow
755f5db4af4SJonthan Brassow /*
756f5db4af4SJonthan Brassow * userspace_get_sync_count
757f5db4af4SJonthan Brassow *
758f5db4af4SJonthan Brassow * If there is any sort of failure when consulting the server,
759f5db4af4SJonthan Brassow * we assume that the sync count is zero.
760f5db4af4SJonthan Brassow *
761f5db4af4SJonthan Brassow * Returns: sync count on success, 0 on failure
762f5db4af4SJonthan Brassow */
userspace_get_sync_count(struct dm_dirty_log * log)763f5db4af4SJonthan Brassow static region_t userspace_get_sync_count(struct dm_dirty_log *log)
764f5db4af4SJonthan Brassow {
765f5db4af4SJonthan Brassow int r;
766f5db4af4SJonthan Brassow size_t rdata_size;
767f5db4af4SJonthan Brassow uint64_t sync_count;
768f5db4af4SJonthan Brassow struct log_c *lc = log->context;
769f5db4af4SJonthan Brassow
770f5db4af4SJonthan Brassow rdata_size = sizeof(sync_count);
771f5db4af4SJonthan Brassow r = userspace_do_request(lc, lc->uuid, DM_ULOG_GET_SYNC_COUNT,
7725066a4dfSDongmao Zhang NULL, 0, (char *)&sync_count, &rdata_size);
773f5db4af4SJonthan Brassow
774f5db4af4SJonthan Brassow if (r)
775f5db4af4SJonthan Brassow return 0;
776f5db4af4SJonthan Brassow
777f5db4af4SJonthan Brassow if (sync_count >= lc->region_count)
778f5db4af4SJonthan Brassow lc->in_sync_hint = lc->region_count;
779f5db4af4SJonthan Brassow
780f5db4af4SJonthan Brassow return (region_t)sync_count;
781f5db4af4SJonthan Brassow }
782f5db4af4SJonthan Brassow
783f5db4af4SJonthan Brassow /*
784f5db4af4SJonthan Brassow * userspace_status
785f5db4af4SJonthan Brassow *
786f5db4af4SJonthan Brassow * Returns: amount of space consumed
787f5db4af4SJonthan Brassow */
userspace_status(struct dm_dirty_log * log,status_type_t status_type,char * result,unsigned int maxlen)788f5db4af4SJonthan Brassow static int userspace_status(struct dm_dirty_log *log, status_type_t status_type,
789*86a3238cSHeinz Mauelshagen char *result, unsigned int maxlen)
790f5db4af4SJonthan Brassow {
791f5db4af4SJonthan Brassow int r = 0;
792b8313b6dSJonathan Brassow char *table_args;
793f5db4af4SJonthan Brassow size_t sz = (size_t)maxlen;
794f5db4af4SJonthan Brassow struct log_c *lc = log->context;
795f5db4af4SJonthan Brassow
796f5db4af4SJonthan Brassow switch (status_type) {
797f5db4af4SJonthan Brassow case STATUSTYPE_INFO:
798f5db4af4SJonthan Brassow r = userspace_do_request(lc, lc->uuid, DM_ULOG_STATUS_INFO,
7995066a4dfSDongmao Zhang NULL, 0, result, &sz);
800f5db4af4SJonthan Brassow
801f5db4af4SJonthan Brassow if (r) {
802f5db4af4SJonthan Brassow sz = 0;
803f5db4af4SJonthan Brassow DMEMIT("%s 1 COM_FAILURE", log->type->name);
804f5db4af4SJonthan Brassow }
805f5db4af4SJonthan Brassow break;
806f5db4af4SJonthan Brassow case STATUSTYPE_TABLE:
807f5db4af4SJonthan Brassow sz = 0;
8080d03d59dSGeert Uytterhoeven table_args = strchr(lc->usr_argv_str, ' ');
809b8313b6dSJonathan Brassow BUG_ON(!table_args); /* There will always be a ' ' */
810b8313b6dSJonathan Brassow table_args++;
811b8313b6dSJonathan Brassow
8125066a4dfSDongmao Zhang DMEMIT("%s %u %s ", log->type->name, lc->usr_argc, lc->uuid);
8135066a4dfSDongmao Zhang if (lc->integrated_flush)
8145066a4dfSDongmao Zhang DMEMIT("integrated_flush ");
8155066a4dfSDongmao Zhang DMEMIT("%s ", table_args);
816f5db4af4SJonthan Brassow break;
8178ec45662STushar Sugandhi case STATUSTYPE_IMA:
8188ec45662STushar Sugandhi *result = '\0';
8198ec45662STushar Sugandhi break;
820f5db4af4SJonthan Brassow }
821f5db4af4SJonthan Brassow return (r) ? 0 : (int)sz;
822f5db4af4SJonthan Brassow }
823f5db4af4SJonthan Brassow
824f5db4af4SJonthan Brassow /*
825f5db4af4SJonthan Brassow * userspace_is_remote_recovering
826f5db4af4SJonthan Brassow *
827f5db4af4SJonthan Brassow * Returns: 1 if region recovering, 0 otherwise
828f5db4af4SJonthan Brassow */
userspace_is_remote_recovering(struct dm_dirty_log * log,region_t region)829f5db4af4SJonthan Brassow static int userspace_is_remote_recovering(struct dm_dirty_log *log,
830f5db4af4SJonthan Brassow region_t region)
831f5db4af4SJonthan Brassow {
832f5db4af4SJonthan Brassow int r;
833f5db4af4SJonthan Brassow uint64_t region64 = region;
834f5db4af4SJonthan Brassow struct log_c *lc = log->context;
8350f30af98SManuel Schölling static unsigned long limit;
836f5db4af4SJonthan Brassow struct {
837f5db4af4SJonthan Brassow int64_t is_recovering;
838f5db4af4SJonthan Brassow uint64_t in_sync_hint;
839f5db4af4SJonthan Brassow } pkg;
840f5db4af4SJonthan Brassow size_t rdata_size = sizeof(pkg);
841f5db4af4SJonthan Brassow
842f5db4af4SJonthan Brassow /*
843f5db4af4SJonthan Brassow * Once the mirror has been reported to be in-sync,
844f5db4af4SJonthan Brassow * it will never again ask for recovery work. So,
845f5db4af4SJonthan Brassow * we can safely say there is not a remote machine
846f5db4af4SJonthan Brassow * recovering if the device is in-sync. (in_sync_hint
847f5db4af4SJonthan Brassow * must be reset at resume time.)
848f5db4af4SJonthan Brassow */
849f5db4af4SJonthan Brassow if (region < lc->in_sync_hint)
850f5db4af4SJonthan Brassow return 0;
8510f30af98SManuel Schölling else if (time_after(limit, jiffies))
852f5db4af4SJonthan Brassow return 1;
853f5db4af4SJonthan Brassow
854f5db4af4SJonthan Brassow limit = jiffies + (HZ / 4);
855f5db4af4SJonthan Brassow r = userspace_do_request(lc, lc->uuid, DM_ULOG_IS_REMOTE_RECOVERING,
856f5db4af4SJonthan Brassow (char *)®ion64, sizeof(region64),
857f5db4af4SJonthan Brassow (char *)&pkg, &rdata_size);
858f5db4af4SJonthan Brassow if (r)
859f5db4af4SJonthan Brassow return 1;
860f5db4af4SJonthan Brassow
861f5db4af4SJonthan Brassow lc->in_sync_hint = pkg.in_sync_hint;
862f5db4af4SJonthan Brassow
863f5db4af4SJonthan Brassow return (int)pkg.is_recovering;
864f5db4af4SJonthan Brassow }
865f5db4af4SJonthan Brassow
866f5db4af4SJonthan Brassow static struct dm_dirty_log_type _userspace_type = {
867f5db4af4SJonthan Brassow .name = "userspace",
868f5db4af4SJonthan Brassow .module = THIS_MODULE,
869f5db4af4SJonthan Brassow .ctr = userspace_ctr,
870f5db4af4SJonthan Brassow .dtr = userspace_dtr,
871f5db4af4SJonthan Brassow .presuspend = userspace_presuspend,
872f5db4af4SJonthan Brassow .postsuspend = userspace_postsuspend,
873f5db4af4SJonthan Brassow .resume = userspace_resume,
874f5db4af4SJonthan Brassow .get_region_size = userspace_get_region_size,
875f5db4af4SJonthan Brassow .is_clean = userspace_is_clean,
876f5db4af4SJonthan Brassow .in_sync = userspace_in_sync,
877f5db4af4SJonthan Brassow .flush = userspace_flush,
878f5db4af4SJonthan Brassow .mark_region = userspace_mark_region,
879f5db4af4SJonthan Brassow .clear_region = userspace_clear_region,
880f5db4af4SJonthan Brassow .get_resync_work = userspace_get_resync_work,
881f5db4af4SJonthan Brassow .set_region_sync = userspace_set_region_sync,
882f5db4af4SJonthan Brassow .get_sync_count = userspace_get_sync_count,
883f5db4af4SJonthan Brassow .status = userspace_status,
884f5db4af4SJonthan Brassow .is_remote_recovering = userspace_is_remote_recovering,
885f5db4af4SJonthan Brassow };
886f5db4af4SJonthan Brassow
userspace_dirty_log_init(void)887f5db4af4SJonthan Brassow static int __init userspace_dirty_log_init(void)
888f5db4af4SJonthan Brassow {
889f5db4af4SJonthan Brassow int r = 0;
890f5db4af4SJonthan Brassow
891ac1f9ef2SMike Snitzer _flush_entry_cache = KMEM_CACHE(dm_dirty_log_flush_entry, 0);
892ac1f9ef2SMike Snitzer if (!_flush_entry_cache) {
893ac1f9ef2SMike Snitzer DMWARN("Unable to create flush_entry_cache: No memory.");
894f5db4af4SJonthan Brassow return -ENOMEM;
895f5db4af4SJonthan Brassow }
896f5db4af4SJonthan Brassow
897f5db4af4SJonthan Brassow r = dm_ulog_tfr_init();
898f5db4af4SJonthan Brassow if (r) {
899f5db4af4SJonthan Brassow DMWARN("Unable to initialize userspace log communications");
900ac1f9ef2SMike Snitzer kmem_cache_destroy(_flush_entry_cache);
901f5db4af4SJonthan Brassow return r;
902f5db4af4SJonthan Brassow }
903f5db4af4SJonthan Brassow
904f5db4af4SJonthan Brassow r = dm_dirty_log_type_register(&_userspace_type);
905f5db4af4SJonthan Brassow if (r) {
906f5db4af4SJonthan Brassow DMWARN("Couldn't register userspace dirty log type");
907f5db4af4SJonthan Brassow dm_ulog_tfr_exit();
908ac1f9ef2SMike Snitzer kmem_cache_destroy(_flush_entry_cache);
909f5db4af4SJonthan Brassow return r;
910f5db4af4SJonthan Brassow }
911f5db4af4SJonthan Brassow
91286a54a48SJonathan Brassow DMINFO("version " DM_LOG_USERSPACE_VSN " loaded");
913f5db4af4SJonthan Brassow return 0;
914f5db4af4SJonthan Brassow }
915f5db4af4SJonthan Brassow
userspace_dirty_log_exit(void)916f5db4af4SJonthan Brassow static void __exit userspace_dirty_log_exit(void)
917f5db4af4SJonthan Brassow {
918f5db4af4SJonthan Brassow dm_dirty_log_type_unregister(&_userspace_type);
919f5db4af4SJonthan Brassow dm_ulog_tfr_exit();
920ac1f9ef2SMike Snitzer kmem_cache_destroy(_flush_entry_cache);
921f5db4af4SJonthan Brassow
92286a54a48SJonathan Brassow DMINFO("version " DM_LOG_USERSPACE_VSN " unloaded");
923f5db4af4SJonthan Brassow }
924f5db4af4SJonthan Brassow
925f5db4af4SJonthan Brassow module_init(userspace_dirty_log_init);
926f5db4af4SJonthan Brassow module_exit(userspace_dirty_log_exit);
927f5db4af4SJonthan Brassow
928f5db4af4SJonthan Brassow MODULE_DESCRIPTION(DM_NAME " userspace dirty log link");
929f5db4af4SJonthan Brassow MODULE_AUTHOR("Jonathan Brassow <dm-devel@redhat.com>");
930f5db4af4SJonthan Brassow MODULE_LICENSE("GPL");
931