sys/dev/disk/dm: Add info() handler for striped target for compatibility
[dragonfly.git] / sys / dev / disk / dm / targets / striped / dm_target_striped.c
CommitLineData
ff56536e
AH
1/*
2 * Copyright (c) 2009 The NetBSD Foundation, Inc.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Adam Hamsik.
7 *
1446934e
MD
8 * This code is further derived from software contributed to the
9 * DragonFly project by Alex Hornung and Matthew Dillon
10 *
ff56536e
AH
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
1446934e
MD
31 *
32 * $NetBSD: dm_target_stripe.c,v 1.9 2010/01/04 00:14:41 haad Exp $
ff56536e
AH
33 */
34
35/*
36 * This file implements initial version of device-mapper stripe target.
1446934e
MD
37 *
38 * DragonFly changes: Increase to an unlimited number of stripes
ff56536e
AH
39 */
40#include <sys/types.h>
ff56536e
AH
41
42#include <sys/buf.h>
5b279a20 43#include <sys/malloc.h>
ff56536e
AH
44#include <sys/vnode.h>
45
a84e173e 46#include <dev/disk/dm/dm.h>
5b279a20 47MALLOC_DEFINE(M_DMSTRIPE, "dm_stripe", "Device Mapper Target Stripe");
ff56536e 48
f3ef812b
TK
49#define MAX_STRIPES 32
50
51struct target_stripe_dev {
52 dm_pdev_t *pdev;
53 uint64_t offset;
54};
55
56typedef struct target_stripe_config {
57 struct target_stripe_dev stripe_devs[MAX_STRIPES];
58 int stripe_num;
59 uint64_t stripe_chunksize;
60} dm_target_stripe_config_t;
61
1446934e
MD
62static void dm_target_stripe_destroy_config(dm_target_stripe_config_t *tsc);
63
ff56536e
AH
64/*
65 * Init function called from dm_table_load_ioctl.
1446934e 66 *
ff56536e
AH
67 * Example line sent to dm from lvm tools when using striped target.
68 * start length striped #stripes chunk_size device1 offset1 ... deviceN offsetN
69 * 0 65536 striped 2 512 /dev/hda 0 /dev/hdb 0
70 */
5c411e8e 71static int
49784e7d 72dm_target_stripe_init(dm_table_entry_t *table_en, char *params)
ff56536e
AH
73{
74 dm_target_stripe_config_t *tsc;
1446934e
MD
75 int n;
76 char *ap;
ff56536e
AH
77
78 if (params == NULL)
79 return EINVAL;
80
ff56536e 81 /*
1446934e 82 * nstripes
ff56536e 83 */
1446934e
MD
84 ap = strsep(&params, " \t");
85 if (ap == NULL)
86 return EINVAL;
87 n = (int)atoi64(ap);
88 if (n < 0 || n > MAX_STRIPES) {
89 kprintf("dm: Error %d stripes not supported (%d max)\n",
90 n, MAX_STRIPES);
ff56536e 91 return ENOTSUP;
1446934e 92 }
ff56536e 93
1446934e
MD
94 tsc = kmalloc(sizeof(dm_target_stripe_config_t),
95 M_DMSTRIPE, M_WAITOK | M_ZERO);
96 tsc->stripe_num = n;
ff56536e 97
1446934e 98 ap = strsep(&params, " \t");
26131c8f
SW
99 if (ap == NULL) {
100 dm_target_stripe_destroy_config(tsc);
1446934e 101 return EINVAL;
26131c8f 102 }
1446934e
MD
103 tsc->stripe_chunksize = atoi64(ap);
104 if (tsc->stripe_chunksize < 1 ||
105 tsc->stripe_chunksize * DEV_BSIZE > MAXPHYS) {
106 kprintf("dm: Error unsupported chunk size %jdKB\n",
107 (intmax_t)tsc->stripe_chunksize * DEV_BSIZE / 1024);
108 dm_target_stripe_destroy_config(tsc);
109 return EINVAL;
110 }
ff56536e 111
1446934e
MD
112 /*
113 * Parse the devices
114 */
ff56536e 115
1446934e
MD
116 kprintf("dm: Stripe %d devices chunk size %dKB\n",
117 (int)tsc->stripe_num,
118 (int)tsc->stripe_chunksize
119 );
ff56536e 120
1446934e
MD
121 for (n = 0; n < tsc->stripe_num; ++n) {
122 ap = strsep(&params, " \t");
123 if (ap == NULL)
124 break;
125 tsc->stripe_devs[n].pdev = dm_pdev_insert(ap);
126 if (tsc->stripe_devs[n].pdev == NULL)
127 break;
128 ap = strsep(&params, " \t");
129 if (ap == NULL)
130 break;
131 tsc->stripe_devs[n].offset = atoi64(ap);
d471f1f9 132 dm_table_add_deps(table_en, tsc->stripe_devs[n].pdev);
1446934e
MD
133 }
134 if (n != tsc->stripe_num) {
135 dm_target_stripe_destroy_config(tsc);
136 return (ENOENT);
137 }
ff56536e 138
e620de1a 139 dm_table_init_target(table_en, DM_STRIPE_DEV, tsc);
ff56536e
AH
140
141 return 0;
142}
1446934e 143
b7405bb6
TK
144/*
145 * Info routine called to get params string.
146 */
147static char *
148dm_target_stripe_info(void *target_config)
149{
150 dm_target_stripe_config_t *tsc;
151 char *params;
152 char *ptr;
153 size_t len;
154 int ret;
155 int i;
156
157 tsc = target_config;
158
159 len = DM_MAX_PARAMS_SIZE;
160 params = kmalloc(len, M_DM, M_WAITOK | M_ZERO);
161 ptr = params;
162
163 ret = ksnprintf(ptr, len, "%d ", tsc->stripe_num);
164 ptr += ret;
165 len -= ret;
166
167 for (i = 0; i < tsc->stripe_num; i++) {
168 ret = ksnprintf(ptr, len, "%s ",
169 tsc->stripe_devs[i].pdev->udev_name);
170 ptr += ret;
171 len -= ret;
172 }
173
174 ret = ksnprintf(ptr, len, "1 ");
175 ptr += ret;
176 len -= ret;
177
178 /*
179 * stripe target currently has no error_count.
180 */
181 for (i = 0; i < tsc->stripe_num; i++) {
182 ret = ksnprintf(ptr, len, "A");
183 ptr += ret;
184 len -= ret;
185 }
186
187 return params;
188}
189
1446934e 190/*
54b60c60 191 * Table routine called to get params string.
1446934e 192 */
5c411e8e 193static char *
54b60c60 194dm_target_stripe_table(void *target_config)
ff56536e
AH
195{
196 dm_target_stripe_config_t *tsc;
197 char *params;
1446934e
MD
198 char *ptr;
199 size_t len;
200 size_t nlen;
201 int n;
ff56536e
AH
202
203 tsc = target_config;
204
1446934e
MD
205 /* caller expects use of M_DM for returned params */
206 nlen = DM_MAX_PARAMS_SIZE;
207 params = kmalloc(nlen, M_DM, M_WAITOK);
208 ptr = params;
209
210 ksnprintf(ptr, nlen, "%d %jd",
211 tsc->stripe_num, (intmax_t)tsc->stripe_chunksize);
212 len = strlen(params);
213 ptr += len;
214 nlen -= len;
215
216 for (n = 0; n < tsc->stripe_num; ++n) {
217 ksnprintf(ptr, nlen, " %s %jd",
65b2aaa7 218 tsc->stripe_devs[n].pdev->udev_name,
1446934e
MD
219 (intmax_t)tsc->stripe_devs[n].offset);
220 len = strlen(ptr);
221 ptr += len;
222 nlen -= len;
223 }
ff56536e
AH
224
225 return params;
226}
1446934e
MD
227
228/*
229 * Strategy routine called from dm_strategy.
230 */
5c411e8e 231static int
1446934e 232dm_target_stripe_strategy(dm_table_entry_t *table_en, struct buf *bp)
ff56536e
AH
233{
234 dm_target_stripe_config_t *tsc;
5b279a20 235 struct bio *bio = &bp->b_bio1;
ff56536e
AH
236 struct buf *nestbuf;
237 uint64_t blkno, blkoff;
3adc52bc 238 uint64_t stripe, blknr;
ff56536e 239 uint32_t stripe_off, stripe_rest, num_blks, issue_blks;
3adc52bc 240 int devnr;
ff56536e
AH
241
242 tsc = table_en->target_config;
243 if (tsc == NULL)
244 return 0;
245
ff56536e 246 /* calculate extent of request */
5b279a20 247 KKASSERT(bp->b_resid % DEV_BSIZE == 0);
ff56536e 248
3adc52bc
MD
249 switch(bp->b_cmd) {
250 case BUF_CMD_READ:
251 case BUF_CMD_WRITE:
252 case BUF_CMD_FREEBLKS:
253 /*
254 * Loop through to individual operations
255 */
256 blkno = bp->b_bio1.bio_offset / DEV_BSIZE;
257 blkoff = 0;
258 num_blks = bp->b_resid / DEV_BSIZE;
259 nestiobuf_init(bio);
260
261 while (num_blks > 0) {
262 /* blockno to strip piece nr */
263 stripe = blkno / tsc->stripe_chunksize;
264 stripe_off = blkno % tsc->stripe_chunksize;
265
266 /* where we are inside the strip */
267 devnr = stripe % tsc->stripe_num;
268 blknr = stripe / tsc->stripe_num;
269
270 /* how much is left before we hit a boundary */
271 stripe_rest = tsc->stripe_chunksize - stripe_off;
272
273 /* issue this piece on stripe `stripe' */
274 issue_blks = MIN(stripe_rest, num_blks);
275 nestbuf = getpbuf(NULL);
f6221ad1 276 nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS;
3adc52bc
MD
277
278 nestiobuf_add(bio, nestbuf, blkoff,
3b48c3c1 279 issue_blks * DEV_BSIZE, NULL);
3adc52bc
MD
280
281 /* I need number of bytes. */
282 nestbuf->b_bio1.bio_offset =
283 blknr * tsc->stripe_chunksize + stripe_off;
284 nestbuf->b_bio1.bio_offset +=
285 tsc->stripe_devs[devnr].offset;
286 nestbuf->b_bio1.bio_offset *= DEV_BSIZE;
287
288 vn_strategy(tsc->stripe_devs[devnr].pdev->pdev_vnode,
289 &nestbuf->b_bio1);
290
291 blkno += issue_blks;
292 blkoff += issue_blks * DEV_BSIZE;
293 num_blks -= issue_blks;
294 }
295 nestiobuf_start(bio);
296 break;
297 case BUF_CMD_FLUSH:
298 nestiobuf_init(bio);
299 for (devnr = 0; devnr < tsc->stripe_num; ++devnr) {
300 nestbuf = getpbuf(NULL);
f6221ad1 301 nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS;
3adc52bc 302
3b48c3c1 303 nestiobuf_add(bio, nestbuf, 0, 0, NULL);
3adc52bc
MD
304 nestbuf->b_bio1.bio_offset = 0;
305 vn_strategy(tsc->stripe_devs[devnr].pdev->pdev_vnode,
306 &nestbuf->b_bio1);
307 }
308 nestiobuf_start(bio);
309 break;
310 default:
311 bp->b_flags |= B_ERROR;
312 bp->b_error = EIO;
313 biodone(bio);
314 break;
ff56536e 315 }
ff56536e
AH
316 return 0;
317}
1446934e 318
79b7159f 319
5c411e8e 320static int
79b7159f
AH
321dm_target_stripe_dump(dm_table_entry_t *table_en, void *data, size_t length, off_t offset)
322{
323 dm_target_stripe_config_t *tsc;
324 uint64_t blkno, blkoff;
325 uint64_t stripe, blknr;
326 uint32_t stripe_off, stripe_rest, num_blks, issue_blks;
327 uint64_t off2, len2;
328 int devnr;
329
330 tsc = table_en->target_config;
331 if (tsc == NULL)
332 return 0;
333
334 /* calculate extent of request */
335 KKASSERT(length % DEV_BSIZE == 0);
336
337 blkno = offset / DEV_BSIZE;
338 blkoff = 0;
339 num_blks = length / DEV_BSIZE;
340
341 /*
342 * 0 length means flush buffers and return
343 */
344 if (length == 0) {
345 for (devnr = 0; devnr < tsc->stripe_num; ++devnr) {
346 if (tsc->stripe_devs[devnr].pdev->pdev_vnode->v_rdev == NULL)
347 return ENXIO;
348
349 dev_ddump(tsc->stripe_devs[devnr].pdev->pdev_vnode->v_rdev,
350 data, 0, offset, 0);
351 }
352 return 0;
353 }
354
355 while (num_blks > 0) {
356 /* blockno to strip piece nr */
357 stripe = blkno / tsc->stripe_chunksize;
358 stripe_off = blkno % tsc->stripe_chunksize;
359
360 /* where we are inside the strip */
361 devnr = stripe % tsc->stripe_num;
362 blknr = stripe / tsc->stripe_num;
363
364 /* how much is left before we hit a boundary */
365 stripe_rest = tsc->stripe_chunksize - stripe_off;
366
367 /* issue this piece on stripe `stripe' */
368 issue_blks = MIN(stripe_rest, num_blks);
369
370#if 0
371 nestiobuf_add(bio, nestbuf, blkoff,
372 issue_blks * DEV_BSIZE);
373#endif
374 len2 = issue_blks * DEV_BSIZE;
375
376 /* I need number of bytes. */
377 off2 = blknr * tsc->stripe_chunksize + stripe_off;
378 off2 += tsc->stripe_devs[devnr].offset;
379 off2 *= DEV_BSIZE;
380 off2 = dm_pdev_correct_dump_offset(tsc->stripe_devs[devnr].pdev,
381 off2);
382
383 if (tsc->stripe_devs[devnr].pdev->pdev_vnode->v_rdev == NULL)
384 return ENXIO;
385
386 dev_ddump(tsc->stripe_devs[devnr].pdev->pdev_vnode->v_rdev,
387 (char *)data + blkoff, 0, off2, len2);
388
389 blkno += issue_blks;
390 blkoff += issue_blks * DEV_BSIZE;
391 num_blks -= issue_blks;
392 }
393
394 return 0;
395}
396
1446934e
MD
397/*
398 * Destroy a dm table entry for stripes.
399 */
5c411e8e 400static int
1446934e 401dm_target_stripe_destroy(dm_table_entry_t *table_en)
ff56536e
AH
402{
403 dm_target_stripe_config_t *tsc;
404
1446934e
MD
405 if ((tsc = table_en->target_config) != NULL) {
406 table_en->target_config = NULL;
407 dm_target_stripe_destroy_config(tsc);
408 }
ff56536e 409
1446934e
MD
410 return 0;
411}
ff56536e 412
1446934e
MD
413static void
414dm_target_stripe_destroy_config(dm_target_stripe_config_t *tsc)
415{
416 int n;
ff56536e 417
1446934e
MD
418 for (n = 0; n < tsc->stripe_num; ++n) {
419 if (tsc->stripe_devs[n].pdev) {
420 dm_pdev_decr(tsc->stripe_devs[n].pdev);
421 tsc->stripe_devs[n].pdev = NULL;
422 }
423 }
424 kfree(tsc, M_DMSTRIPE);
ff56536e 425}
1446934e 426
1446934e
MD
427/*
428 * Unsupported for this target.
429 */
5c411e8e 430static int
bdf2de7b 431dm_target_stripe_upcall(dm_table_entry_t *table_en, struct buf *bp)
ff56536e
AH
432{
433 return 0;
434}
5c411e8e
AH
435
436static int
437dmts_mod_handler(module_t mod, int type, void *unused)
438{
439 dm_target_t *dmt = NULL;
440 int err = 0;
441
442 switch(type) {
443 case MOD_LOAD:
444 if ((dmt = dm_target_lookup("striped")) != NULL) {
445 dm_target_unbusy(dmt);
446 return EEXIST;
447 }
448 dmt = dm_target_alloc("striped");
449 dmt->version[0] = 1;
450 dmt->version[1] = 0;
451 dmt->version[2] = 3;
452 strlcpy(dmt->name, "striped", DM_MAX_TYPE_NAME);
453 dmt->init = &dm_target_stripe_init;
b7405bb6 454 dmt->info = &dm_target_stripe_info;
54b60c60 455 dmt->table = &dm_target_stripe_table;
5c411e8e 456 dmt->strategy = &dm_target_stripe_strategy;
5c411e8e
AH
457 dmt->destroy = &dm_target_stripe_destroy;
458 dmt->upcall = &dm_target_stripe_upcall;
459 dmt->dump = &dm_target_stripe_dump;
460
461 err = dm_target_insert(dmt);
462 if (err == 0)
463 kprintf("dm_target_stripe: Successfully initialized\n");
464 break;
465
466 case MOD_UNLOAD:
467 err = dm_target_rem("striped");
468 if (err == 0)
469 kprintf("dm_target_stripe: unloaded\n");
470 break;
471
472 default:
473 break;
474 }
475
476 return err;
477}
478
7115a22b 479DM_TARGET_MODULE(dm_target_striped, dmts_mod_handler);