sys/dev/disk/dm: Add dm-delay target
[dragonfly.git] / sys / dev / disk / dm / targets / delay / dm_target_delay.c
CommitLineData
59146421
TK
1/*
2 * Copyright (c) 2015 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Tomohiro Kusumi <kusumi.tomohiro@gmail.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35#include <sys/types.h>
36#include <sys/buf.h>
37#include <sys/vnode.h>
38#include <sys/mutex2.h>
39#include <sys/objcache.h>
40#include <sys/callout.h>
41
42#include <dev/disk/dm/dm.h>
43
44MALLOC_DEFINE(M_DMDELAY, "dm_delay", "Device Mapper Target Delay");
45
46struct dm_delay_buf {
47 TAILQ_ENTRY(dm_delay_buf) entry;
48 struct buf *bp;
49 int expire;
50};
51TAILQ_HEAD(dm_delay_buf_list, dm_delay_buf);
52
53struct dm_delay_info {
54 dm_pdev_t *pdev;
55 uint64_t offset;
56 int delay;
57 int count;
58 int enabled;
59 struct dm_delay_buf_list buf_list;
60 struct callout cal;
61 struct mtx buf_mtx;
62 struct mtx cal_mtx;
63 struct lwkt_token token;
64 thread_t td;
65};
66
67typedef struct target_delay_config {
68 struct dm_delay_info read;
69 struct dm_delay_info write;
70} dm_target_delay_config_t;
71
72static int _init(struct dm_delay_info *di, char **argv);
73static int _status(struct dm_delay_info *di, char *p);
74static void _strategy(struct dm_delay_info *di, struct buf *bp);
75static void _submit(struct dm_delay_info *di, struct buf *bp);
76static void _submit_queue(struct dm_delay_info *di, int submit_all);
77static void _destroy(struct dm_delay_info *di);
78static int _deps(struct dm_delay_info *di, prop_array_t prop_array);
79static void _timeout(void *arg);
80static void _thread(void *arg);
81static __inline void _debug(struct dm_delay_info *di, const char *msg);
82
83static struct objcache *obj_cache = NULL;
84static struct objcache_malloc_args obj_args = {
85 sizeof(struct dm_delay_buf), M_DMDELAY,
86};
87
88static int
89dm_target_delay_init(dm_dev_t *dmv, void **target_config, char *params)
90{
91 dm_target_delay_config_t *tdc;
92 int ret, argc;
93 char **ap, *argv[7];
94
95 if (params == NULL)
96 return EINVAL;
97
98 /* Parse params */
99 for (ap = argv; ap < &argv[6] &&
100 (*ap = strsep(&params, " \t")) != NULL;) {
101 if (**ap != '\0')
102 ap++;
103 }
104
105 argc = ap - argv;
106 aprint_debug("Delay target init: argc=%d\n", argc);
107 if (argc != 3 && argc != 6) {
108 kprintf("Delay target takes 3 or 6 args\n");
109 return EINVAL;
110 }
111
112 tdc = kmalloc(sizeof(*tdc), M_DMDELAY, M_WAITOK | M_ZERO);
113 ret = _init(&tdc->read, argv);
114 if (ret) {
115 kfree(tdc, M_DMDELAY);
116 return ret;
117 }
118
119 if (argc == 3) {
120 kprintf("Delay target without write delay\n");
121 tdc->write.pdev = tdc->read.pdev;
122 goto end;
123 }
124
125 ret = _init(&tdc->write, argv + 3);
126 if (ret) {
127 dm_pdev_decr(tdc->read.pdev);
128 kfree(tdc, M_DMDELAY);
129 return ret;
130 }
131end:
132 *target_config = tdc;
133 dmv->dev_type = DM_DELAY_DEV;
134
135 return 0;
136}
137
138static int
139_init(struct dm_delay_info *di, char **argv)
140{
141 dm_pdev_t *dmp;
142 int tmp;
143 static int id = 0;
144
145 if (argv[0] == NULL)
146 return EINVAL;
147 if ((dmp = dm_pdev_insert(argv[0])) == NULL)
148 return ENOENT;
149
150 di->pdev = dmp;
151 di->offset = atoi64(argv[1]);
152 tmp = atoi64(argv[2]);
153 di->delay = tmp * hz / 1000;
154 di->count = 0;
155
156 TAILQ_INIT(&di->buf_list);
157 callout_init(&di->cal);
158 mtx_init(&di->buf_mtx, "dmdlbuf");
159 mtx_init(&di->cal_mtx, "dmdlcal");
160 lwkt_token_init(&di->token, "dmdlthr");
161
162 di->enabled = ++id;
163 lwkt_create(_thread, di, &di->td, NULL, 0, -1, "dmdl%d", id);
164
165 _debug(di, "init");
166 return 0;
167}
168
169static char *
170dm_target_delay_status(void *target_config)
171{
172 dm_target_delay_config_t *tdc;
173 char *params, *p;
174
175 tdc = target_config;
176 KKASSERT(tdc != NULL);
177
178 params = kmalloc(DM_MAX_PARAMS_SIZE, M_DM, M_WAITOK);
179 p = params;
180 p += _status(&tdc->read, p);
181 if (tdc->write.enabled) {
182 p += ksnprintf(p, DM_MAX_PARAMS_SIZE, " ");
183 _status(&tdc->write, p);
184 }
185
186 return params;
187}
188
189static int _status(struct dm_delay_info *di, char *p)
190{
191 int ret = 0;
192
193 ret += ksnprintf(p, DM_MAX_PARAMS_SIZE,
194 "%s %" PRIu64 " %d",
195 di->pdev->name, di->offset, di->delay);
196 return ret;
197}
198
199static int
200dm_target_delay_strategy(dm_table_entry_t *table_en, struct buf *bp)
201{
202 dm_target_delay_config_t *tdc;
203 struct dm_delay_info *di;
204
205 tdc = table_en->target_config;
206 KKASSERT(tdc != NULL);
207
208 switch (bp->b_cmd) {
209 case BUF_CMD_READ:
210 di = &tdc->read;
211 break;
212 case BUF_CMD_WRITE:
213 case BUF_CMD_FLUSH:
214 di = &tdc->write;
215 break;
216 default:
217 di = NULL;
218 break;
219 }
220
221 if (di) {
222 if (di->enabled) {
223 _strategy(di, bp);
224 } else {
225 _submit(di, bp);
226 }
227 } else {
228 /* XXX */
229 struct vnode *vnode = tdc->write.pdev->pdev_vnode;
230 vn_strategy(vnode, &bp->b_bio1);
231 }
232 return 0;
233}
234
235static void
236_strategy(struct dm_delay_info *di, struct buf *bp)
237{
238 struct dm_delay_buf *dp;
239
240 dp = objcache_get(obj_cache, M_WAITOK);
241 dp->bp = bp;
242 dp->expire = ticks + di->delay;
243
244 mtx_lock(&di->buf_mtx);
245 di->count++;
246 TAILQ_INSERT_TAIL(&di->buf_list, dp, entry);
247 mtx_unlock(&di->buf_mtx);
248
249 mtx_lock(&di->cal_mtx);
250 if (!callout_pending(&di->cal))
251 callout_reset(&di->cal, di->delay, _timeout, di);
252 mtx_unlock(&di->cal_mtx);
253}
254
255static void
256_submit(struct dm_delay_info *di, struct buf *bp)
257{
258 _debug(di, "submit");
259
260 bp->b_bio1.bio_offset += di->offset * DEV_BSIZE;
261 vn_strategy(di->pdev->pdev_vnode, &bp->b_bio1);
262}
263
264static void
265_submit_queue(struct dm_delay_info *di, int submit_all)
266{
267 struct dm_delay_buf *dp;
268 struct dm_delay_buf_list tmp_list;
269 int next = -1;
270 int reset = 0;
271
272 _debug(di, "submitq");
273 TAILQ_INIT(&tmp_list);
274
275 mtx_lock(&di->buf_mtx);
276 while ((dp = TAILQ_FIRST(&di->buf_list)) != NULL) {
277 if (submit_all || ticks > dp->expire) {
278 TAILQ_REMOVE(&di->buf_list, dp, entry);
279 TAILQ_INSERT_TAIL(&tmp_list, dp, entry);
280 di->count--;
281 continue;
282 }
283 if (reset == 0) {
284 reset = 1;
285 next = dp->expire;
286 } else {
287 next = min(next, dp->expire);
288 }
289 }
290 mtx_unlock(&di->buf_mtx);
291
292 if (reset) {
293 mtx_lock(&di->cal_mtx);
294 callout_reset(&di->cal, next - ticks, _timeout, di);
295 mtx_unlock(&di->cal_mtx);
296 }
297
298 while ((dp = TAILQ_FIRST(&tmp_list)) != NULL) {
299 TAILQ_REMOVE(&tmp_list, dp, entry);
300 _submit(di, dp->bp);
301 objcache_put(obj_cache, dp);
302 }
303}
304
305static int
306dm_target_delay_destroy(dm_table_entry_t *table_en)
307{
308 dm_target_delay_config_t *tdc;
309
310 tdc = table_en->target_config;
311 if (tdc == NULL)
312 return 0;
313
314 _destroy(&tdc->read);
315 if (tdc->write.enabled)
316 _destroy(&tdc->write);
317
318 kfree(tdc, M_DMDELAY);
319 table_en->target_config = NULL;
320
321 return 0;
322}
323
324static void
325_destroy(struct dm_delay_info *di)
326{
327 _debug(di, "destroy");
328
329 lwkt_gettoken(&di->token);
330 di->enabled = 0;
331
332 mtx_lock(&di->cal_mtx);
333 if (callout_pending(&di->cal))
334 callout_stop_sync(&di->cal);
335 mtx_unlock(&di->cal_mtx);
336
337 _submit_queue(di, 1);
338 wakeup(di);
339 tsleep(&di->enabled, 0, "dmdldestroy", 0);
340 lwkt_reltoken(&di->token);
341
342 mtx_uninit(&di->cal_mtx);
343 mtx_uninit(&di->buf_mtx);
344
345 dm_pdev_decr(di->pdev);
346}
347
348static int
349dm_target_delay_deps(dm_table_entry_t *table_en, prop_array_t prop_array)
350{
351 dm_target_delay_config_t *tdc;
352 int error;
353
354 tdc = table_en->target_config;
355 if (tdc == NULL)
356 return ENOENT;
357
358 error = _deps(&tdc->read, prop_array);
359 if (error)
360 return error;
361 if (tdc->write.enabled) {
362 error = _deps(&tdc->write, prop_array);
363 if (error)
364 return error;
365 }
366 return 0;
367}
368
369static int
370_deps(struct dm_delay_info *di, prop_array_t prop_array)
371{
372 struct vattr va;
373 int error;
374
375 _debug(di, "deps");
376
377 error = VOP_GETATTR(di->pdev->pdev_vnode, &va);
378 if (error)
379 return error;
380 prop_array_add_uint64(prop_array,
381 (uint64_t)makeudev(va.va_rmajor, va.va_rminor));
382 return 0;
383}
384
385static int
386dm_target_delay_upcall(dm_table_entry_t *table_en, struct buf *bp)
387{
388 return 0;
389}
390
391static void
392_timeout(void *arg)
393{
394 struct dm_delay_info *di = arg;
395
396 _debug(di, "timeout");
397 wakeup(di);
398}
399
400static void
401_thread(void *arg)
402{
403 struct dm_delay_info *di = arg;
404
405 _debug(di, "thread init");
406 lwkt_gettoken(&di->token);
407
408 while (di->enabled) {
409 tsleep(di, 0, "dmdlthread", 0);
410 _submit_queue(di, 0);
411 }
412
413 di->td = NULL;
414 wakeup(&di->enabled);
415
416 _debug(di, "thread exit");
417 lwkt_reltoken(&di->token);
418 lwkt_exit();
419}
420
421static __inline
422void
423_debug(struct dm_delay_info *di, const char *msg)
424{
425 aprint_debug("%-8s: %d pdev=%s offset=%ju delay=%d count=%d\n",
426 msg, di->enabled, di->pdev->name,
427 (uintmax_t)di->offset, di->delay, di->count);
428}
429
430static void
431_objcache_create(void)
432{
433 if (obj_cache == NULL) {
434 obj_cache = objcache_create("dmdlobj", 0, 0, NULL, NULL, NULL,
435 objcache_malloc_alloc,
436 objcache_malloc_free,
437 &obj_args);
438 }
439 KKASSERT(obj_cache);
440}
441
442static void
443_objcache_destroy(void)
444{
445 if (obj_cache) {
446 objcache_destroy(obj_cache);
447 obj_cache = NULL;
448 }
449}
450
451static int
452dmtd_mod_handler(module_t mod, int type, void *unused)
453{
454 dm_target_t *dmt = NULL;
455 int err = 0;
456
457 switch(type) {
458 case MOD_LOAD:
459 if ((dmt = dm_target_lookup("delay")) != NULL) {
460 dm_target_unbusy(dmt);
461 return EEXIST;
462 }
463 dmt = dm_target_alloc("delay");
464 dmt->version[0] = 1;
465 dmt->version[1] = 0;
466 dmt->version[2] = 0;
467 strlcpy(dmt->name, "delay", DM_MAX_TYPE_NAME);
468 dmt->init = &dm_target_delay_init;
469 dmt->status = &dm_target_delay_status;
470 dmt->strategy = &dm_target_delay_strategy;
471 dmt->destroy = &dm_target_delay_destroy;
472 dmt->deps = &dm_target_delay_deps;
473 dmt->upcall = &dm_target_delay_upcall;
474 dmt->dump = NULL;
475
476 _objcache_create();
477 err = dm_target_insert(dmt);
478 if (err == 0)
479 kprintf("dm_target_delay: Successfully initialized\n");
480 break;
481
482 case MOD_UNLOAD:
483 err = dm_target_rem("delay");
484 if (err == 0)
485 kprintf("dm_target_delay: unloaded\n");
486 _objcache_destroy();
487 break;
488 }
489
490 return err;
491}
492
493DM_TARGET_MODULE(dm_target_delay, dmtd_mod_handler);