dm-delay.c 8.3 KB
Newer Older
H
Heinz Mauelshagen 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (C) 2005-2007 Red Hat GmbH
 *
 * A target that delays reads and/or writes and can send
 * them to different devices.
 *
 * This file is released under the GPL.
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/blkdev.h>
#include <linux/bio.h>
#include <linux/slab.h>

16 17
#include <linux/device-mapper.h>

H
Heinz Mauelshagen 已提交
18 19
#define DM_MSG_PREFIX "delay"

20 21 22 23 24 25 26
struct delay_class {
	struct dm_dev *dev;
	sector_t start;
	unsigned delay;
	unsigned ops;
};

H
Heinz Mauelshagen 已提交
27 28
struct delay_c {
	struct timer_list delay_timer;
A
Alasdair G Kergon 已提交
29
	struct mutex timer_lock;
30
	struct workqueue_struct *kdelayd_wq;
H
Heinz Mauelshagen 已提交
31 32 33 34
	struct work_struct flush_expired_bios;
	struct list_head delayed_bios;
	atomic_t may_delay;

35 36
	struct delay_class read;
	struct delay_class write;
37
	struct delay_class flush;
H
Heinz Mauelshagen 已提交
38

39
	int argc;
H
Heinz Mauelshagen 已提交
40 41
};

A
Alasdair G Kergon 已提交
42
struct dm_delay_info {
H
Heinz Mauelshagen 已提交
43
	struct delay_c *context;
44
	struct delay_class *class;
H
Heinz Mauelshagen 已提交
45 46 47 48 49 50
	struct list_head list;
	unsigned long expires;
};

static DEFINE_MUTEX(delayed_bios_lock);

51
static void handle_delayed_timer(struct timer_list *t)
H
Heinz Mauelshagen 已提交
52
{
53
	struct delay_c *dc = from_timer(dc, t, delay_timer);
H
Heinz Mauelshagen 已提交
54

55
	queue_work(dc->kdelayd_wq, &dc->flush_expired_bios);
H
Heinz Mauelshagen 已提交
56 57 58 59
}

static void queue_timeout(struct delay_c *dc, unsigned long expires)
{
A
Alasdair G Kergon 已提交
60
	mutex_lock(&dc->timer_lock);
H
Heinz Mauelshagen 已提交
61 62 63 64

	if (!timer_pending(&dc->delay_timer) || expires < dc->delay_timer.expires)
		mod_timer(&dc->delay_timer, expires);

A
Alasdair G Kergon 已提交
65
	mutex_unlock(&dc->timer_lock);
H
Heinz Mauelshagen 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
}

static void flush_bios(struct bio *bio)
{
	struct bio *n;

	while (bio) {
		n = bio->bi_next;
		bio->bi_next = NULL;
		generic_make_request(bio);
		bio = n;
	}
}

static struct bio *flush_delayed_bios(struct delay_c *dc, int flush_all)
{
A
Alasdair G Kergon 已提交
82
	struct dm_delay_info *delayed, *next;
H
Heinz Mauelshagen 已提交
83
	unsigned long next_expires = 0;
84
	unsigned long start_timer = 0;
A
Alasdair G Kergon 已提交
85
	struct bio_list flush_bios = { };
H
Heinz Mauelshagen 已提交
86 87 88 89

	mutex_lock(&delayed_bios_lock);
	list_for_each_entry_safe(delayed, next, &dc->delayed_bios, list) {
		if (flush_all || time_after_eq(jiffies, delayed->expires)) {
90 91
			struct bio *bio = dm_bio_from_per_bio_data(delayed,
						sizeof(struct dm_delay_info));
H
Heinz Mauelshagen 已提交
92
			list_del(&delayed->list);
93
			bio_list_add(&flush_bios, bio);
94
			delayed->class->ops--;
H
Heinz Mauelshagen 已提交
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
			continue;
		}

		if (!start_timer) {
			start_timer = 1;
			next_expires = delayed->expires;
		} else
			next_expires = min(next_expires, delayed->expires);
	}
	mutex_unlock(&delayed_bios_lock);

	if (start_timer)
		queue_timeout(dc, next_expires);

	return bio_list_get(&flush_bios);
}

static void flush_expired_bios(struct work_struct *work)
{
	struct delay_c *dc;

	dc = container_of(work, struct delay_c, flush_expired_bios);
	flush_bios(flush_delayed_bios(dc, 0));
}

120 121 122 123 124 125 126 127 128 129
static void delay_dtr(struct dm_target *ti)
{
	struct delay_c *dc = ti->private;

	destroy_workqueue(dc->kdelayd_wq);

	if (dc->read.dev)
		dm_put_device(ti, dc->read.dev);
	if (dc->write.dev)
		dm_put_device(ti, dc->write.dev);
130 131
	if (dc->flush.dev)
		dm_put_device(ti, dc->flush.dev);
132 133 134 135 136 137 138 139 140 141 142 143

	mutex_destroy(&dc->timer_lock);

	kfree(dc);
}

static int delay_class_ctr(struct dm_target *ti, struct delay_class *c, char **argv)
{
	int ret;
	unsigned long long tmpll;
	char dummy;

144
	if (sscanf(argv[1], "%llu%c", &tmpll, &dummy) != 1 || tmpll != (sector_t)tmpll) {
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
		ti->error = "Invalid device sector";
		return -EINVAL;
	}
	c->start = tmpll;

	if (sscanf(argv[2], "%u%c", &c->delay, &dummy) != 1) {
		ti->error = "Invalid delay";
		return -EINVAL;
	}

	ret = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &c->dev);
	if (ret) {
		ti->error = "Device lookup failed";
		return ret;
	}

	return 0;
}

H
Heinz Mauelshagen 已提交
164 165 166 167 168
/*
 * Mapping parameters:
 *    <device> <offset> <delay> [<write_device> <write_offset> <write_delay>]
 *
 * With separate write parameters, the first set is only used for reads.
169
 * Offsets are specified in sectors.
H
Heinz Mauelshagen 已提交
170 171 172 173 174
 * Delays are specified in milliseconds.
 */
static int delay_ctr(struct dm_target *ti, unsigned int argc, char **argv)
{
	struct delay_c *dc;
175
	int ret;
H
Heinz Mauelshagen 已提交
176

177 178
	if (argc != 3 && argc != 6 && argc != 9) {
		ti->error = "Requires exactly 3, 6 or 9 arguments";
H
Heinz Mauelshagen 已提交
179 180 181
		return -EINVAL;
	}

182
	dc = kzalloc(sizeof(*dc), GFP_KERNEL);
H
Heinz Mauelshagen 已提交
183 184 185 186 187
	if (!dc) {
		ti->error = "Cannot allocate context";
		return -ENOMEM;
	}

188 189 190 191 192 193 194
	ti->private = dc;
	timer_setup(&dc->delay_timer, handle_delayed_timer, 0);
	INIT_WORK(&dc->flush_expired_bios, flush_expired_bios);
	INIT_LIST_HEAD(&dc->delayed_bios);
	mutex_init(&dc->timer_lock);
	atomic_set(&dc->may_delay, 1);
	dc->argc = argc;
H
Heinz Mauelshagen 已提交
195

196 197
	ret = delay_class_ctr(ti, &dc->read, argv);
	if (ret)
H
Heinz Mauelshagen 已提交
198 199
		goto bad;

200 201
	if (argc == 3) {
		ret = delay_class_ctr(ti, &dc->write, argv);
202 203 204
		if (ret)
			goto bad;
		ret = delay_class_ctr(ti, &dc->flush, argv);
205 206
		if (ret)
			goto bad;
H
Heinz Mauelshagen 已提交
207 208 209
		goto out;
	}

210 211 212
	ret = delay_class_ctr(ti, &dc->write, argv + 3);
	if (ret)
		goto bad;
213 214 215 216 217 218 219 220 221 222
	if (argc == 6) {
		ret = delay_class_ctr(ti, &dc->flush, argv + 3);
		if (ret)
			goto bad;
		goto out;
	}

	ret = delay_class_ctr(ti, &dc->flush, argv + 6);
	if (ret)
		goto bad;
H
Heinz Mauelshagen 已提交
223 224

out:
225 226
	dc->kdelayd_wq = alloc_workqueue("kdelayd", WQ_MEM_RECLAIM, 0);
	if (!dc->kdelayd_wq) {
227
		ret = -EINVAL;
228
		DMERR("Couldn't start kdelayd");
229
		goto bad;
230 231
	}

232 233
	ti->num_flush_bios = 1;
	ti->num_discard_bios = 1;
234
	ti->per_io_data_size = sizeof(struct dm_delay_info);
H
Heinz Mauelshagen 已提交
235 236 237
	return 0;

bad:
238
	delay_dtr(ti);
239
	return ret;
H
Heinz Mauelshagen 已提交
240 241
}

242
static int delay_bio(struct delay_c *dc, struct delay_class *c, struct bio *bio)
H
Heinz Mauelshagen 已提交
243
{
A
Alasdair G Kergon 已提交
244
	struct dm_delay_info *delayed;
H
Heinz Mauelshagen 已提交
245 246
	unsigned long expires = 0;

247
	if (!c->delay || !atomic_read(&dc->may_delay))
248
		return DM_MAPIO_REMAPPED;
H
Heinz Mauelshagen 已提交
249

250
	delayed = dm_per_bio_data(bio, sizeof(struct dm_delay_info));
H
Heinz Mauelshagen 已提交
251 252

	delayed->context = dc;
253
	delayed->expires = expires = jiffies + msecs_to_jiffies(c->delay);
H
Heinz Mauelshagen 已提交
254 255

	mutex_lock(&delayed_bios_lock);
256
	c->ops++;
H
Heinz Mauelshagen 已提交
257 258 259 260 261
	list_add_tail(&delayed->list, &dc->delayed_bios);
	mutex_unlock(&delayed_bios_lock);

	queue_timeout(dc, expires);

262
	return DM_MAPIO_SUBMITTED;
H
Heinz Mauelshagen 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
}

static void delay_presuspend(struct dm_target *ti)
{
	struct delay_c *dc = ti->private;

	atomic_set(&dc->may_delay, 0);
	del_timer_sync(&dc->delay_timer);
	flush_bios(flush_delayed_bios(dc, 1));
}

static void delay_resume(struct dm_target *ti)
{
	struct delay_c *dc = ti->private;

	atomic_set(&dc->may_delay, 1);
}

M
Mikulas Patocka 已提交
281
static int delay_map(struct dm_target *ti, struct bio *bio)
H
Heinz Mauelshagen 已提交
282 283
{
	struct delay_c *dc = ti->private;
284 285
	struct delay_class *c;
	struct dm_delay_info *delayed = dm_per_bio_data(bio, sizeof(struct dm_delay_info));
H
Heinz Mauelshagen 已提交
286

287
	if (bio_data_dir(bio) == WRITE) {
288 289 290 291
		if (unlikely(bio->bi_opf & REQ_PREFLUSH))
			c = &dc->flush;
		else
			c = &dc->write;
292 293
	} else {
		c = &dc->read;
H
Heinz Mauelshagen 已提交
294
	}
295 296 297 298
	delayed->class = c;
	bio_set_dev(bio, c->dev->bdev);
	if (bio_sectors(bio))
		bio->bi_iter.bi_sector = c->start + dm_target_offset(ti, bio->bi_iter.bi_sector);
H
Heinz Mauelshagen 已提交
299

300
	return delay_bio(dc, c, bio);
H
Heinz Mauelshagen 已提交
301 302
}

303 304 305
#define DMEMIT_DELAY_CLASS(c) \
	DMEMIT("%s %llu %u", (c)->dev->name, (unsigned long long)(c)->start, (c)->delay)

306 307
static void delay_status(struct dm_target *ti, status_type_t type,
			 unsigned status_flags, char *result, unsigned maxlen)
H
Heinz Mauelshagen 已提交
308 309 310 311 312 313
{
	struct delay_c *dc = ti->private;
	int sz = 0;

	switch (type) {
	case STATUSTYPE_INFO:
314
		DMEMIT("%u %u %u", dc->read.ops, dc->write.ops, dc->flush.ops);
H
Heinz Mauelshagen 已提交
315 316 317
		break;

	case STATUSTYPE_TABLE:
318 319 320 321 322
		DMEMIT_DELAY_CLASS(&dc->read);
		if (dc->argc >= 6) {
			DMEMIT(" ");
			DMEMIT_DELAY_CLASS(&dc->write);
		}
323 324 325 326
		if (dc->argc >= 9) {
			DMEMIT(" ");
			DMEMIT_DELAY_CLASS(&dc->flush);
		}
H
Heinz Mauelshagen 已提交
327 328 329 330
		break;
	}
}

331 332 333 334 335 336
static int delay_iterate_devices(struct dm_target *ti,
				 iterate_devices_callout_fn fn, void *data)
{
	struct delay_c *dc = ti->private;
	int ret = 0;

337 338 339 340
	ret = fn(ti, dc->read.dev, dc->read.start, ti->len, data);
	if (ret)
		goto out;
	ret = fn(ti, dc->write.dev, dc->write.start, ti->len, data);
341 342
	if (ret)
		goto out;
343 344 345
	ret = fn(ti, dc->flush.dev, dc->flush.start, ti->len, data);
	if (ret)
		goto out;
346 347 348 349 350

out:
	return ret;
}

H
Heinz Mauelshagen 已提交
351 352
static struct target_type delay_target = {
	.name	     = "delay",
353
	.version     = {1, 2, 1},
354
	.features    = DM_TARGET_PASSES_INTEGRITY,
H
Heinz Mauelshagen 已提交
355 356 357 358 359 360 361
	.module      = THIS_MODULE,
	.ctr	     = delay_ctr,
	.dtr	     = delay_dtr,
	.map	     = delay_map,
	.presuspend  = delay_presuspend,
	.resume	     = delay_resume,
	.status	     = delay_status,
362
	.iterate_devices = delay_iterate_devices,
H
Heinz Mauelshagen 已提交
363 364 365 366
};

static int __init dm_delay_init(void)
{
367
	int r;
H
Heinz Mauelshagen 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382

	r = dm_register_target(&delay_target);
	if (r < 0) {
		DMERR("register failed %d", r);
		goto bad_register;
	}

	return 0;

bad_register:
	return r;
}

static void __exit dm_delay_exit(void)
{
383
	dm_unregister_target(&delay_target);
H
Heinz Mauelshagen 已提交
384 385 386 387 388 389 390 391 392
}

/* Module hooks */
module_init(dm_delay_init);
module_exit(dm_delay_exit);

MODULE_DESCRIPTION(DM_NAME " delay target");
MODULE_AUTHOR("Heinz Mauelshagen <mauelshagen@redhat.com>");
MODULE_LICENSE("GPL");