localedef: Automatically mark "(x)digits" as numbers
[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
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);
037a33c5 181 if (tdc->argc == 6) {
59146421
TK
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) {
037a33c5 222 if (di->delay) {
59146421
TK
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);
037a33c5 315 _destroy(&tdc->write);
59146421
TK
316
317 kfree(tdc, M_DMDELAY);
318 table_en->target_config = NULL;
319
320 return 0;
321}
322
323static void
324_destroy(struct dm_delay_info *di)
325{
326 _debug(di, "destroy");
327
328 lwkt_gettoken(&di->token);
329 di->enabled = 0;
330
331 mtx_lock(&di->cal_mtx);
332 if (callout_pending(&di->cal))
333 callout_stop_sync(&di->cal);
334 mtx_unlock(&di->cal_mtx);
335
336 _submit_queue(di, 1);
337 wakeup(di);
338 tsleep(&di->enabled, 0, "dmdldestroy", 0);
339 lwkt_reltoken(&di->token);
340
341 mtx_uninit(&di->cal_mtx);
342 mtx_uninit(&di->buf_mtx);
343
344 dm_pdev_decr(di->pdev);
345}
346
347static int
348dm_target_delay_deps(dm_table_entry_t *table_en, prop_array_t prop_array)
349{
350 dm_target_delay_config_t *tdc;
66fee7cc 351 uint64_t u1, u2;
59146421
TK
352
353 tdc = table_en->target_config;
354 if (tdc == NULL)
355 return ENOENT;
356
66fee7cc
TK
357 u1 = _makeudev(&tdc->read);
358 u2 = _makeudev(&tdc->write);
359
360 prop_array_add_uint64(prop_array, u1);
361 if (u1 != u2) {
362 prop_array_add_uint64(prop_array, u2);
59146421
TK
363 }
364 return 0;
365}
366
66fee7cc
TK
367static uint64_t
368_makeudev(struct dm_delay_info *di)
59146421
TK
369{
370 struct vattr va;
66fee7cc 371 uint64_t ret;
59146421
TK
372 int error;
373
66fee7cc 374 _debug(di, "makeudev");
59146421
TK
375
376 error = VOP_GETATTR(di->pdev->pdev_vnode, &va);
66fee7cc
TK
377 KKASSERT(error == 0);
378
379 ret = makeudev(va.va_rmajor, va.va_rminor);
219b70dd 380 return ret;
59146421
TK
381}
382
383static int
384dm_target_delay_upcall(dm_table_entry_t *table_en, struct buf *bp)
385{
386 return 0;
387}
388
389static void
390_timeout(void *arg)
391{
392 struct dm_delay_info *di = arg;
393
394 _debug(di, "timeout");
395 wakeup(di);
396}
397
398static void
399_thread(void *arg)
400{
401 struct dm_delay_info *di = arg;
402
403 _debug(di, "thread init");
404 lwkt_gettoken(&di->token);
405
406 while (di->enabled) {
407 tsleep(di, 0, "dmdlthread", 0);
408 _submit_queue(di, 0);
409 }
410
411 di->td = NULL;
412 wakeup(&di->enabled);
413
414 _debug(di, "thread exit");
415 lwkt_reltoken(&di->token);
416 lwkt_exit();
417}
418
419static __inline
420void
421_debug(struct dm_delay_info *di, const char *msg)
422{
423 aprint_debug("%-8s: %d pdev=%s offset=%ju delay=%d count=%d\n",
424 msg, di->enabled, di->pdev->name,
425 (uintmax_t)di->offset, di->delay, di->count);
426}
427
428static void
429_objcache_create(void)
430{
431 if (obj_cache == NULL) {
432 obj_cache = objcache_create("dmdlobj", 0, 0, NULL, NULL, NULL,
433 objcache_malloc_alloc,
434 objcache_malloc_free,
435 &obj_args);
436 }
437 KKASSERT(obj_cache);
438}
439
440static void
441_objcache_destroy(void)
442{
443 if (obj_cache) {
444 objcache_destroy(obj_cache);
445 obj_cache = NULL;
446 }
447}
448
449static int
450dmtd_mod_handler(module_t mod, int type, void *unused)
451{
452 dm_target_t *dmt = NULL;
453 int err = 0;
454
455 switch(type) {
456 case MOD_LOAD:
457 if ((dmt = dm_target_lookup("delay")) != NULL) {
458 dm_target_unbusy(dmt);
459 return EEXIST;
460 }
461 dmt = dm_target_alloc("delay");
462 dmt->version[0] = 1;
463 dmt->version[1] = 0;
464 dmt->version[2] = 0;
465 strlcpy(dmt->name, "delay", DM_MAX_TYPE_NAME);
466 dmt->init = &dm_target_delay_init;
467 dmt->status = &dm_target_delay_status;
468 dmt->strategy = &dm_target_delay_strategy;
469 dmt->destroy = &dm_target_delay_destroy;
470 dmt->deps = &dm_target_delay_deps;
471 dmt->upcall = &dm_target_delay_upcall;
472 dmt->dump = NULL;
473
474 _objcache_create();
475 err = dm_target_insert(dmt);
476 if (err == 0)
477 kprintf("dm_target_delay: Successfully initialized\n");
478 break;
479
480 case MOD_UNLOAD:
481 err = dm_target_rem("delay");
482 if (err == 0)
483 kprintf("dm_target_delay: unloaded\n");
484 _objcache_destroy();
485 break;
486 }
487
488 return err;
489}
490
491DM_TARGET_MODULE(dm_target_delay, dmtd_mod_handler);