summaryrefslogtreecommitdiff
path: root/drivers/md/dm-pcache/dm_pcache.c
blob: e5f5936fa6f08ff7a9d07897691d0b30e1fb3a77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
// SPDX-License-Identifier: GPL-2.0-or-later
#include <linux/module.h>
#include <linux/blkdev.h>
#include <linux/bio.h>

#include "../dm-core.h"
#include "cache_dev.h"
#include "backing_dev.h"
#include "cache.h"
#include "dm_pcache.h"

void pcache_defer_reqs_kick(struct dm_pcache *pcache)
{
	struct pcache_cache *cache = &pcache->cache;

	spin_lock(&cache->seg_map_lock);
	if (!cache->cache_full)
		queue_work(pcache->task_wq, &pcache->defered_req_work);
	spin_unlock(&cache->seg_map_lock);
}

static void defer_req(struct pcache_request *pcache_req)
{
	struct dm_pcache *pcache = pcache_req->pcache;

	BUG_ON(!list_empty(&pcache_req->list_node));

	spin_lock(&pcache->defered_req_list_lock);
	list_add(&pcache_req->list_node, &pcache->defered_req_list);
	pcache_defer_reqs_kick(pcache);
	spin_unlock(&pcache->defered_req_list_lock);
}

static void defered_req_fn(struct work_struct *work)
{
	struct dm_pcache *pcache = container_of(work, struct dm_pcache, defered_req_work);
	struct pcache_request *pcache_req;
	LIST_HEAD(tmp_list);
	int ret;

	if (pcache_is_stopping(pcache))
		return;

	spin_lock(&pcache->defered_req_list_lock);
	list_splice_init(&pcache->defered_req_list, &tmp_list);
	spin_unlock(&pcache->defered_req_list_lock);

	while (!list_empty(&tmp_list)) {
		pcache_req = list_first_entry(&tmp_list,
					    struct pcache_request, list_node);
		list_del_init(&pcache_req->list_node);
		pcache_req->ret = 0;
		ret = pcache_cache_handle_req(&pcache->cache, pcache_req);
		if (ret == -EBUSY)
			defer_req(pcache_req);
		else
			pcache_req_put(pcache_req, ret);
	}
}

void pcache_req_get(struct pcache_request *pcache_req)
{
	kref_get(&pcache_req->ref);
}

static void end_req(struct kref *ref)
{
	struct pcache_request *pcache_req = container_of(ref, struct pcache_request, ref);
	struct dm_pcache *pcache = pcache_req->pcache;
	struct bio *bio = pcache_req->bio;
	int ret = pcache_req->ret;

	if (ret == -EBUSY) {
		pcache_req_get(pcache_req);
		defer_req(pcache_req);
	} else {
		bio->bi_status = errno_to_blk_status(ret);
		bio_endio(bio);

		if (atomic_dec_and_test(&pcache->inflight_reqs))
			wake_up(&pcache->inflight_wq);
	}
}

void pcache_req_put(struct pcache_request *pcache_req, int ret)
{
	/* Set the return status if it is not already set */
	if (ret && !pcache_req->ret)
		pcache_req->ret = ret;

	kref_put(&pcache_req->ref, end_req);
}

static bool at_least_one_arg(struct dm_arg_set *as, char **error)
{
	if (!as->argc) {
		*error = "Insufficient args";
		return false;
	}

	return true;
}

static int parse_cache_dev(struct dm_pcache *pcache, struct dm_arg_set *as,
				char **error)
{
	int ret;

	if (!at_least_one_arg(as, error))
		return -EINVAL;
	ret = dm_get_device(pcache->ti, dm_shift_arg(as),
			  BLK_OPEN_READ | BLK_OPEN_WRITE,
			  &pcache->cache_dev.dm_dev);
	if (ret) {
		*error = "Error opening cache device";
		return ret;
	}

	return 0;
}

static int parse_backing_dev(struct dm_pcache *pcache, struct dm_arg_set *as,
				char **error)
{
	int ret;

	if (!at_least_one_arg(as, error))
		return -EINVAL;

	ret = dm_get_device(pcache->ti, dm_shift_arg(as),
			  BLK_OPEN_READ | BLK_OPEN_WRITE,
			  &pcache->backing_dev.dm_dev);
	if (ret) {
		*error = "Error opening backing device";
		return ret;
	}

	return 0;
}

static void pcache_init_opts(struct pcache_cache_options *opts)
{
	opts->cache_mode = PCACHE_CACHE_MODE_WRITEBACK;
	opts->data_crc = false;
}

static int parse_cache_opts(struct dm_pcache *pcache, struct dm_arg_set *as,
			    char **error)
{
	struct pcache_cache_options *opts = &pcache->opts;
	static const struct dm_arg _args[] = {
		{0, 4, "Invalid number of cache option arguments"},
	};
	unsigned int argc;
	const char *arg;
	int ret;

	pcache_init_opts(opts);
	if (!as->argc)
		return 0;

	ret = dm_read_arg_group(_args, as, &argc, error);
	if (ret)
		return -EINVAL;

	while (argc) {
		arg = dm_shift_arg(as);
		argc--;

		if (!strcmp(arg, "cache_mode")) {
			arg = dm_shift_arg(as);
			if (!strcmp(arg, "writeback")) {
				opts->cache_mode = PCACHE_CACHE_MODE_WRITEBACK;
			} else {
				*error = "Invalid cache mode parameter";
				return -EINVAL;
			}
			argc--;
		} else if (!strcmp(arg, "data_crc")) {
			arg = dm_shift_arg(as);
			if (!strcmp(arg, "true")) {
				opts->data_crc = true;
			} else if (!strcmp(arg, "false")) {
				opts->data_crc = false;
			} else {
				*error = "Invalid data crc parameter";
				return -EINVAL;
			}
			argc--;
		} else {
			*error = "Unrecognised cache option requested";
			return -EINVAL;
		}
	}

	return 0;
}

static int pcache_start(struct dm_pcache *pcache, char **error)
{
	int ret;

	ret = cache_dev_start(pcache);
	if (ret) {
		*error = "Failed to start cache dev";
		return ret;
	}

	ret = backing_dev_start(pcache);
	if (ret) {
		*error = "Failed to start backing dev";
		goto stop_cache;
	}

	ret = pcache_cache_start(pcache);
	if (ret) {
		*error = "Failed to start pcache";
		goto stop_backing;
	}

	return 0;
stop_backing:
	backing_dev_stop(pcache);
stop_cache:
	cache_dev_stop(pcache);

	return ret;
}

static void pcache_destroy_args(struct dm_pcache *pcache)
{
	if (pcache->cache_dev.dm_dev)
		dm_put_device(pcache->ti, pcache->cache_dev.dm_dev);
	if (pcache->backing_dev.dm_dev)
		dm_put_device(pcache->ti, pcache->backing_dev.dm_dev);
}

static int pcache_parse_args(struct dm_pcache *pcache, unsigned int argc, char **argv,
				char **error)
{
	struct dm_arg_set as;
	int ret;

	as.argc = argc;
	as.argv = argv;

	/*
	 * Parse cache device
	 */
	ret = parse_cache_dev(pcache, &as, error);
	if (ret)
		return ret;
	/*
	 * Parse backing device
	 */
	ret = parse_backing_dev(pcache, &as, error);
	if (ret)
		goto out;
	/*
	 * Parse optional arguments
	 */
	ret = parse_cache_opts(pcache, &as, error);
	if (ret)
		goto out;

	return 0;
out:
	pcache_destroy_args(pcache);
	return ret;
}

static int dm_pcache_ctr(struct dm_target *ti, unsigned int argc, char **argv)
{
	struct mapped_device *md = ti->table->md;
	struct dm_pcache *pcache;
	int ret;

	if (md->map) {
		ti->error = "Don't support table loading for live md";
		return -EOPNOTSUPP;
	}

	/* Allocate memory for the cache structure */
	pcache = kzalloc(sizeof(struct dm_pcache), GFP_KERNEL);
	if (!pcache)
		return -ENOMEM;

	pcache->task_wq = alloc_workqueue("pcache-%s-wq",  WQ_UNBOUND | WQ_MEM_RECLAIM,
					  0, md->name);
	if (!pcache->task_wq) {
		ret = -ENOMEM;
		goto free_pcache;
	}

	spin_lock_init(&pcache->defered_req_list_lock);
	INIT_LIST_HEAD(&pcache->defered_req_list);
	INIT_WORK(&pcache->defered_req_work, defered_req_fn);
	pcache->ti = ti;

	ret = pcache_parse_args(pcache, argc, argv, &ti->error);
	if (ret)
		goto destroy_wq;

	ret = pcache_start(pcache, &ti->error);
	if (ret)
		goto destroy_args;

	ti->num_flush_bios = 1;
	ti->flush_supported = true;
	ti->per_io_data_size = sizeof(struct pcache_request);
	ti->private = pcache;
	atomic_set(&pcache->inflight_reqs, 0);
	atomic_set(&pcache->state, PCACHE_STATE_RUNNING);
	init_waitqueue_head(&pcache->inflight_wq);

	return 0;
destroy_args:
	pcache_destroy_args(pcache);
destroy_wq:
	destroy_workqueue(pcache->task_wq);
free_pcache:
	kfree(pcache);

	return ret;
}

static void defer_req_stop(struct dm_pcache *pcache)
{
	struct pcache_request *pcache_req;
	LIST_HEAD(tmp_list);

	flush_work(&pcache->defered_req_work);

	spin_lock(&pcache->defered_req_list_lock);
	list_splice_init(&pcache->defered_req_list, &tmp_list);
	spin_unlock(&pcache->defered_req_list_lock);

	while (!list_empty(&tmp_list)) {
		pcache_req = list_first_entry(&tmp_list,
					    struct pcache_request, list_node);
		list_del_init(&pcache_req->list_node);
		pcache_req_put(pcache_req, -EIO);
	}
}

static void dm_pcache_dtr(struct dm_target *ti)
{
	struct dm_pcache *pcache;

	pcache = ti->private;
	atomic_set(&pcache->state, PCACHE_STATE_STOPPING);
	defer_req_stop(pcache);

	wait_event(pcache->inflight_wq,
			atomic_read(&pcache->inflight_reqs) == 0);

	pcache_cache_stop(pcache);
	backing_dev_stop(pcache);
	cache_dev_stop(pcache);

	pcache_destroy_args(pcache);
	drain_workqueue(pcache->task_wq);
	destroy_workqueue(pcache->task_wq);

	kfree(pcache);
}

static int dm_pcache_map_bio(struct dm_target *ti, struct bio *bio)
{
	struct pcache_request *pcache_req = dm_per_bio_data(bio, sizeof(struct pcache_request));
	struct dm_pcache *pcache = ti->private;
	int ret;

	pcache_req->pcache = pcache;
	kref_init(&pcache_req->ref);
	pcache_req->ret = 0;
	pcache_req->bio = bio;
	pcache_req->off = (u64)bio->bi_iter.bi_sector << SECTOR_SHIFT;
	pcache_req->data_len = bio->bi_iter.bi_size;
	INIT_LIST_HEAD(&pcache_req->list_node);
	atomic_inc(&pcache->inflight_reqs);

	ret = pcache_cache_handle_req(&pcache->cache, pcache_req);
	if (ret == -EBUSY)
		defer_req(pcache_req);
	else
		pcache_req_put(pcache_req, ret);

	return DM_MAPIO_SUBMITTED;
}

static void dm_pcache_status(struct dm_target *ti, status_type_t type,
			     unsigned int status_flags, char *result,
			     unsigned int maxlen)
{
	struct dm_pcache *pcache = ti->private;
	struct pcache_cache_dev *cache_dev = &pcache->cache_dev;
	struct pcache_backing_dev *backing_dev = &pcache->backing_dev;
	struct pcache_cache *cache = &pcache->cache;
	unsigned int sz = 0;

	switch (type) {
	case STATUSTYPE_INFO:
		DMEMIT("%x %u %u %u %u %x %u:%u %u:%u %u:%u",
		       cache_dev->sb_flags,
		       cache_dev->seg_num,
		       cache->n_segs,
		       bitmap_weight(cache->seg_map, cache->n_segs),
		       pcache_cache_get_gc_percent(cache),
		       cache->cache_info.flags,
		       cache->key_head.cache_seg->cache_seg_id,
		       cache->key_head.seg_off,
		       cache->dirty_tail.cache_seg->cache_seg_id,
		       cache->dirty_tail.seg_off,
		       cache->key_tail.cache_seg->cache_seg_id,
		       cache->key_tail.seg_off);
		break;
	case STATUSTYPE_TABLE:
		DMEMIT("%s %s 4 cache_mode writeback crc %s",
		       cache_dev->dm_dev->name,
		       backing_dev->dm_dev->name,
		       cache_data_crc_on(cache) ? "true" : "false");
		break;
	case STATUSTYPE_IMA:
		*result = '\0';
		break;
	}
}

static int dm_pcache_message(struct dm_target *ti, unsigned int argc,
			     char **argv, char *result, unsigned int maxlen)
{
	struct dm_pcache *pcache = ti->private;
	unsigned long val;

	if (argc != 2)
		goto err;

	if (!strcasecmp(argv[0], "gc_percent")) {
		if (kstrtoul(argv[1], 10, &val))
			goto err;

		return pcache_cache_set_gc_percent(&pcache->cache, val);
	}
err:
	return -EINVAL;
}

static struct target_type dm_pcache_target = {
	.name		= "pcache",
	.version	= {0, 1, 0},
	.module		= THIS_MODULE,
	.features	= DM_TARGET_SINGLETON,
	.ctr		= dm_pcache_ctr,
	.dtr		= dm_pcache_dtr,
	.map		= dm_pcache_map_bio,
	.status		= dm_pcache_status,
	.message	= dm_pcache_message,
};

static int __init dm_pcache_init(void)
{
	int ret;

	ret = pcache_backing_init();
	if (ret)
		goto err;

	ret = pcache_cache_init();
	if (ret)
		goto backing_exit;

	ret = dm_register_target(&dm_pcache_target);
	if (ret)
		goto cache_exit;
	return 0;

cache_exit:
	pcache_cache_exit();
backing_exit:
	pcache_backing_exit();
err:
	return ret;
}
module_init(dm_pcache_init);

static void __exit dm_pcache_exit(void)
{
	dm_unregister_target(&dm_pcache_target);
	pcache_cache_exit();
	pcache_backing_exit();
}
module_exit(dm_pcache_exit);

MODULE_DESCRIPTION("dm-pcache Persistent Cache for block device");
MODULE_AUTHOR("Dongsheng Yang <dongsheng.yang@linux.dev>");
MODULE_LICENSE("GPL");