sys/dev/disk/dm: Don't implement "status" as a subset of "table" [1/2]
[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;
037a33c5 70 int argc; /* either 3 or 6 */
59146421
TK
71} dm_target_delay_config_t;
72
037a33c5 73static int _init(struct dm_delay_info *di, char **argv, int id);
59146421
TK
74static int _status(struct dm_delay_info *di, char *p);
75static void _strategy(struct dm_delay_info *di, struct buf *bp);
76static void _submit(struct dm_delay_info *di, struct buf *bp);
77static void _submit_queue(struct dm_delay_info *di, int submit_all);
78static void _destroy(struct dm_delay_info *di);
66fee7cc 79static uint64_t _makeudev(struct dm_delay_info *di);
59146421
TK
80static void _timeout(void *arg);
81static void _thread(void *arg);
82static __inline void _debug(struct dm_delay_info *di, const char *msg);
83
84static struct objcache *obj_cache = NULL;
85static struct objcache_malloc_args obj_args = {
86 sizeof(struct dm_delay_buf), M_DMDELAY,
87};
88
89static int
90dm_target_delay_init(dm_dev_t *dmv, void **target_config, char *params)
91{
92 dm_target_delay_config_t *tdc;
93 int ret, argc;
94 char **ap, *argv[7];
95
96 if (params == NULL)
97 return EINVAL;
98
99 /* Parse params */
100 for (ap = argv; ap < &argv[6] &&
101 (*ap = strsep(&params, " \t")) != NULL;) {
102 if (**ap != '\0')
103 ap++;
104 }
105
106 argc = ap - argv;
107 aprint_debug("Delay target init: argc=%d\n", argc);
108 if (argc != 3 && argc != 6) {
109 kprintf("Delay target takes 3 or 6 args\n");
110 return EINVAL;
111 }
112
113 tdc = kmalloc(sizeof(*tdc), M_DMDELAY, M_WAITOK | M_ZERO);
037a33c5
TK
114 tdc->argc = argc;
115
116 ap = argv;
117 ret = _init(&tdc->read, ap, 0);
59146421
TK
118 if (ret) {
119 kfree(tdc, M_DMDELAY);
120 return ret;
121 }
122
037a33c5
TK
123 if (argc == 6)
124 ap += 3;
59146421 125
037a33c5 126 ret = _init(&tdc->write, ap, 1);
59146421
TK
127 if (ret) {
128 dm_pdev_decr(tdc->read.pdev);
129 kfree(tdc, M_DMDELAY);
130 return ret;
131 }
037a33c5 132
59146421
TK
133 *target_config = tdc;
134 dmv->dev_type = DM_DELAY_DEV;
135
136 return 0;
137}
138
139static int
037a33c5 140_init(struct dm_delay_info *di, char **argv, int id)
59146421
TK
141{
142 dm_pdev_t *dmp;
143 int tmp;
59146421
TK
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
037a33c5 162 di->enabled = 1;
59146421
TK
163 lwkt_create(_thread, di, &di->td, NULL, 0, -1, "dmdl%d", id);
164
165 _debug(di, "init");
166 return 0;
167}
168
f134a703
TK
169static char *
170dm_target_delay_info(void *target_config)
171{
172 dm_target_delay_config_t *tdc;
173 char *params;
174
175 tdc = target_config;
176 KKASSERT(tdc != NULL);
177
178 params = kmalloc(DM_MAX_PARAMS_SIZE, M_DM, M_WAITOK);
179 ksnprintf(params, DM_MAX_PARAMS_SIZE,
180 "%d %d", tdc->read.count, tdc->write.count);
181
182 return params;
183}
184
59146421
TK
185static char *
186dm_target_delay_status(void *target_config)
187{
188 dm_target_delay_config_t *tdc;
189 char *params, *p;
190
191 tdc = target_config;
192 KKASSERT(tdc != NULL);
193
194 params = kmalloc(DM_MAX_PARAMS_SIZE, M_DM, M_WAITOK);
195 p = params;
196 p += _status(&tdc->read, p);
037a33c5 197 if (tdc->argc == 6) {
59146421
TK
198 p += ksnprintf(p, DM_MAX_PARAMS_SIZE, " ");
199 _status(&tdc->write, p);
200 }
201
202 return params;
203}
204
205static int _status(struct dm_delay_info *di, char *p)
206{
207 int ret = 0;
208
209 ret += ksnprintf(p, DM_MAX_PARAMS_SIZE,
210 "%s %" PRIu64 " %d",
211 di->pdev->name, di->offset, di->delay);
212 return ret;
213}
214
215static int
216dm_target_delay_strategy(dm_table_entry_t *table_en, struct buf *bp)
217{
218 dm_target_delay_config_t *tdc;
219 struct dm_delay_info *di;
220
221 tdc = table_en->target_config;
222 KKASSERT(tdc != NULL);
223
224 switch (bp->b_cmd) {
225 case BUF_CMD_READ:
226 di = &tdc->read;
227 break;
228 case BUF_CMD_WRITE:
229 case BUF_CMD_FLUSH:
230 di = &tdc->write;
231 break;
232 default:
233 di = NULL;
234 break;
235 }
236
237 if (di) {
037a33c5 238 if (di->delay) {
59146421
TK
239 _strategy(di, bp);
240 } else {
241 _submit(di, bp);
242 }
243 } else {
244 /* XXX */
245 struct vnode *vnode = tdc->write.pdev->pdev_vnode;
246 vn_strategy(vnode, &bp->b_bio1);
247 }
248 return 0;
249}
250
251static void
252_strategy(struct dm_delay_info *di, struct buf *bp)
253{
254 struct dm_delay_buf *dp;
255
256 dp = objcache_get(obj_cache, M_WAITOK);
257 dp->bp = bp;
258 dp->expire = ticks + di->delay;
259
260 mtx_lock(&di->buf_mtx);
261 di->count++;
262 TAILQ_INSERT_TAIL(&di->buf_list, dp, entry);
263 mtx_unlock(&di->buf_mtx);
264
265 mtx_lock(&di->cal_mtx);
266 if (!callout_pending(&di->cal))
267 callout_reset(&di->cal, di->delay, _timeout, di);
268 mtx_unlock(&di->cal_mtx);
269}
270
271static void
272_submit(struct dm_delay_info *di, struct buf *bp)
273{
274 _debug(di, "submit");
275
276 bp->b_bio1.bio_offset += di->offset * DEV_BSIZE;
277 vn_strategy(di->pdev->pdev_vnode, &bp->b_bio1);
278}
279
280static void
281_submit_queue(struct dm_delay_info *di, int submit_all)
282{
283 struct dm_delay_buf *dp;
284 struct dm_delay_buf_list tmp_list;
285 int next = -1;
286 int reset = 0;
287
288 _debug(di, "submitq");
289 TAILQ_INIT(&tmp_list);
290
291 mtx_lock(&di->buf_mtx);
292 while ((dp = TAILQ_FIRST(&di->buf_list)) != NULL) {
293 if (submit_all || ticks > dp->expire) {
294 TAILQ_REMOVE(&di->buf_list, dp, entry);
295 TAILQ_INSERT_TAIL(&tmp_list, dp, entry);
296 di->count--;
297 continue;
298 }
299 if (reset == 0) {
300 reset = 1;
301 next = dp->expire;
302 } else {
303 next = min(next, dp->expire);
304 }
305 }
306 mtx_unlock(&di->buf_mtx);
307
308 if (reset) {
309 mtx_lock(&di->cal_mtx);
310 callout_reset(&di->cal, next - ticks, _timeout, di);
311 mtx_unlock(&di->cal_mtx);
312 }
313
314 while ((dp = TAILQ_FIRST(&tmp_list)) != NULL) {
315 TAILQ_REMOVE(&tmp_list, dp, entry);
316 _submit(di, dp->bp);
317 objcache_put(obj_cache, dp);
318 }
319}
320
321static int
322dm_target_delay_destroy(dm_table_entry_t *table_en)
323{
324 dm_target_delay_config_t *tdc;
325
326 tdc = table_en->target_config;
327 if (tdc == NULL)
328 return 0;
329
330 _destroy(&tdc->read);
037a33c5 331 _destroy(&tdc->write);
59146421
TK
332
333 kfree(tdc, M_DMDELAY);
334 table_en->target_config = NULL;
335
336 return 0;
337}
338
339static void
340_destroy(struct dm_delay_info *di)
341{
342 _debug(di, "destroy");
343
344 lwkt_gettoken(&di->token);
345 di->enabled = 0;
346
347 mtx_lock(&di->cal_mtx);
348 if (callout_pending(&di->cal))
349 callout_stop_sync(&di->cal);
350 mtx_unlock(&di->cal_mtx);
351
352 _submit_queue(di, 1);
353 wakeup(di);
354 tsleep(&di->enabled, 0, "dmdldestroy", 0);
355 lwkt_reltoken(&di->token);
356
357 mtx_uninit(&di->cal_mtx);
358 mtx_uninit(&di->buf_mtx);
359
360 dm_pdev_decr(di->pdev);
361}
362
363static int
364dm_target_delay_deps(dm_table_entry_t *table_en, prop_array_t prop_array)
365{
366 dm_target_delay_config_t *tdc;
66fee7cc 367 uint64_t u1, u2;
59146421
TK
368
369 tdc = table_en->target_config;
370 if (tdc == NULL)
371 return ENOENT;
372
66fee7cc
TK
373 u1 = _makeudev(&tdc->read);
374 u2 = _makeudev(&tdc->write);
375
376 prop_array_add_uint64(prop_array, u1);
377 if (u1 != u2) {
378 prop_array_add_uint64(prop_array, u2);
59146421
TK
379 }
380 return 0;
381}
382
66fee7cc
TK
383static uint64_t
384_makeudev(struct dm_delay_info *di)
59146421
TK
385{
386 struct vattr va;
66fee7cc 387 uint64_t ret;
59146421
TK
388 int error;
389
66fee7cc 390 _debug(di, "makeudev");
59146421
TK
391
392 error = VOP_GETATTR(di->pdev->pdev_vnode, &va);
66fee7cc
TK
393 KKASSERT(error == 0);
394
395 ret = makeudev(va.va_rmajor, va.va_rminor);
219b70dd 396 return ret;
59146421
TK
397}
398
399static int
400dm_target_delay_upcall(dm_table_entry_t *table_en, struct buf *bp)
401{
402 return 0;
403}
404
405static void
406_timeout(void *arg)
407{
408 struct dm_delay_info *di = arg;
409
410 _debug(di, "timeout");
411 wakeup(di);
412}
413
414static void
415_thread(void *arg)
416{
417 struct dm_delay_info *di = arg;
418
419 _debug(di, "thread init");
420 lwkt_gettoken(&di->token);
421
422 while (di->enabled) {
423 tsleep(di, 0, "dmdlthread", 0);
424 _submit_queue(di, 0);
425 }
426
427 di->td = NULL;
428 wakeup(&di->enabled);
429
430 _debug(di, "thread exit");
431 lwkt_reltoken(&di->token);
432 lwkt_exit();
433}
434
435static __inline
436void
437_debug(struct dm_delay_info *di, const char *msg)
438{
439 aprint_debug("%-8s: %d pdev=%s offset=%ju delay=%d count=%d\n",
440 msg, di->enabled, di->pdev->name,
441 (uintmax_t)di->offset, di->delay, di->count);
442}
443
444static void
445_objcache_create(void)
446{
447 if (obj_cache == NULL) {
448 obj_cache = objcache_create("dmdlobj", 0, 0, NULL, NULL, NULL,
449 objcache_malloc_alloc,
450 objcache_malloc_free,
451 &obj_args);
452 }
453 KKASSERT(obj_cache);
454}
455
456static void
457_objcache_destroy(void)
458{
459 if (obj_cache) {
460 objcache_destroy(obj_cache);
461 obj_cache = NULL;
462 }
463}
464
465static int
466dmtd_mod_handler(module_t mod, int type, void *unused)
467{
468 dm_target_t *dmt = NULL;
469 int err = 0;
470
471 switch(type) {
472 case MOD_LOAD:
473 if ((dmt = dm_target_lookup("delay")) != NULL) {
474 dm_target_unbusy(dmt);
475 return EEXIST;
476 }
477 dmt = dm_target_alloc("delay");
478 dmt->version[0] = 1;
479 dmt->version[1] = 0;
480 dmt->version[2] = 0;
481 strlcpy(dmt->name, "delay", DM_MAX_TYPE_NAME);
482 dmt->init = &dm_target_delay_init;
f134a703 483 dmt->info = &dm_target_delay_info;
59146421
TK
484 dmt->status = &dm_target_delay_status;
485 dmt->strategy = &dm_target_delay_strategy;
486 dmt->destroy = &dm_target_delay_destroy;
487 dmt->deps = &dm_target_delay_deps;
488 dmt->upcall = &dm_target_delay_upcall;
489 dmt->dump = NULL;
490
491 _objcache_create();
492 err = dm_target_insert(dmt);
493 if (err == 0)
494 kprintf("dm_target_delay: Successfully initialized\n");
495 break;
496
497 case MOD_UNLOAD:
498 err = dm_target_rem("delay");
499 if (err == 0)
500 kprintf("dm_target_delay: unloaded\n");
501 _objcache_destroy();
502 break;
503 }
504
505 return err;
506}
507
508DM_TARGET_MODULE(dm_target_delay, dmtd_mod_handler);