2 * Copyright (c) 2009 The NetBSD Foundation, Inc.
5 * This code is derived from software contributed to The NetBSD Foundation
8 * This code is further derived from software contributed to the
9 * DragonFly project by Alex Hornung and Matthew Dillon
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
32 * $NetBSD: dm_target_stripe.c,v 1.9 2010/01/04 00:14:41 haad Exp $
36 * This file implements initial version of device-mapper stripe target.
38 * DragonFly changes: Increase to an unlimited number of stripes
40 #include <sys/types.h>
43 #include <sys/malloc.h>
44 #include <sys/vnode.h>
46 #include <dev/disk/dm/dm.h>
47 MALLOC_DEFINE(M_DMSTRIPE, "dm_striped", "Device Mapper Target Striped");
49 #define MAX_STRIPES 32
51 struct target_stripe_dev {
56 typedef struct target_stripe_config {
57 struct target_stripe_dev stripe_devs[MAX_STRIPES];
59 uint64_t stripe_chunksize;
60 } dm_target_stripe_config_t;
62 static void dm_target_stripe_destroy_config(dm_target_stripe_config_t *tsc);
65 * Init function called from dm_table_load_ioctl.
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
72 dm_target_stripe_init(dm_table_entry_t *table_en, char *params)
74 dm_target_stripe_config_t *tsc;
84 ap = strsep(¶ms, " \t");
88 if (n < 0 || n > MAX_STRIPES) {
89 kprintf("dm: Error %d stripes not supported (%d max)\n",
94 tsc = kmalloc(sizeof(dm_target_stripe_config_t),
95 M_DMSTRIPE, M_WAITOK | M_ZERO);
98 ap = strsep(¶ms, " \t");
100 dm_target_stripe_destroy_config(tsc);
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);
116 kprintf("dm: Stripe %d devices chunk size %dKB\n",
117 (int)tsc->stripe_num,
118 (int)tsc->stripe_chunksize
121 for (n = 0; n < tsc->stripe_num; ++n) {
122 ap = strsep(¶ms, " \t");
125 tsc->stripe_devs[n].pdev = dm_pdev_insert(ap);
126 if (tsc->stripe_devs[n].pdev == NULL)
128 ap = strsep(¶ms, " \t");
131 tsc->stripe_devs[n].offset = atoi64(ap);
132 dm_table_add_deps(table_en, tsc->stripe_devs[n].pdev);
134 if (n != tsc->stripe_num) {
135 dm_target_stripe_destroy_config(tsc);
139 dm_table_init_target(table_en, DM_STRIPE_DEV, tsc);
145 * Info routine called to get params string.
148 dm_target_stripe_info(void *target_config)
150 dm_target_stripe_config_t *tsc;
159 len = DM_MAX_PARAMS_SIZE;
160 params = kmalloc(len, M_DM, M_WAITOK | M_ZERO);
163 ret = ksnprintf(ptr, len, "%d ", tsc->stripe_num);
167 for (i = 0; i < tsc->stripe_num; i++) {
168 ret = ksnprintf(ptr, len, "%s ",
169 tsc->stripe_devs[i].pdev->udev_name);
174 ret = ksnprintf(ptr, len, "1 ");
179 * stripe target currently has no error_count.
181 for (i = 0; i < tsc->stripe_num; i++) {
182 ret = ksnprintf(ptr, len, "A");
191 * Table routine called to get params string.
194 dm_target_stripe_table(void *target_config)
196 dm_target_stripe_config_t *tsc;
205 len = DM_MAX_PARAMS_SIZE;
206 params = kmalloc(len, M_DM, M_WAITOK | M_ZERO);
209 ret = ksnprintf(ptr, len, "%d %jd",
211 (intmax_t)tsc->stripe_chunksize);
215 for (i = 0; i < tsc->stripe_num; i++) {
216 ret = ksnprintf(ptr, len, " %s %jd",
217 tsc->stripe_devs[i].pdev->udev_name,
218 (intmax_t)tsc->stripe_devs[i].offset);
227 * Strategy routine called from dm_strategy.
230 dm_target_stripe_strategy(dm_table_entry_t *table_en, struct buf *bp)
232 dm_target_stripe_config_t *tsc;
233 struct bio *bio = &bp->b_bio1;
235 uint64_t blkno, blkoff;
236 uint64_t stripe, blknr;
237 uint32_t stripe_off, stripe_rest, num_blks, issue_blks;
240 tsc = table_en->target_config;
244 /* calculate extent of request */
245 KKASSERT(bp->b_resid % DEV_BSIZE == 0);
250 case BUF_CMD_FREEBLKS:
252 * Loop through to individual operations
254 blkno = bp->b_bio1.bio_offset / DEV_BSIZE;
256 num_blks = bp->b_resid / DEV_BSIZE;
259 while (num_blks > 0) {
260 /* blockno to strip piece nr */
261 stripe = blkno / tsc->stripe_chunksize;
262 stripe_off = blkno % tsc->stripe_chunksize;
264 /* where we are inside the strip */
265 devnr = stripe % tsc->stripe_num;
266 blknr = stripe / tsc->stripe_num;
268 /* how much is left before we hit a boundary */
269 stripe_rest = tsc->stripe_chunksize - stripe_off;
271 /* issue this piece on stripe `stripe' */
272 issue_blks = MIN(stripe_rest, num_blks);
273 nestbuf = getpbuf(NULL);
274 nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS;
276 nestiobuf_add(bio, nestbuf, blkoff,
277 issue_blks * DEV_BSIZE, NULL);
279 /* I need number of bytes. */
280 nestbuf->b_bio1.bio_offset =
281 blknr * tsc->stripe_chunksize + stripe_off;
282 nestbuf->b_bio1.bio_offset +=
283 tsc->stripe_devs[devnr].offset;
284 nestbuf->b_bio1.bio_offset *= DEV_BSIZE;
286 vn_strategy(tsc->stripe_devs[devnr].pdev->pdev_vnode,
290 blkoff += issue_blks * DEV_BSIZE;
291 num_blks -= issue_blks;
293 nestiobuf_start(bio);
297 for (devnr = 0; devnr < tsc->stripe_num; ++devnr) {
298 nestbuf = getpbuf(NULL);
299 nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS;
301 nestiobuf_add(bio, nestbuf, 0, 0, NULL);
302 nestbuf->b_bio1.bio_offset = 0;
303 vn_strategy(tsc->stripe_devs[devnr].pdev->pdev_vnode,
306 nestiobuf_start(bio);
309 bp->b_flags |= B_ERROR;
319 dm_target_stripe_dump(dm_table_entry_t *table_en, void *data, size_t length, off_t offset)
321 dm_target_stripe_config_t *tsc;
322 uint64_t blkno, blkoff;
323 uint64_t stripe, blknr;
324 uint32_t stripe_off, stripe_rest, num_blks, issue_blks;
328 tsc = table_en->target_config;
332 /* calculate extent of request */
333 KKASSERT(length % DEV_BSIZE == 0);
335 blkno = offset / DEV_BSIZE;
337 num_blks = length / DEV_BSIZE;
340 * 0 length means flush buffers and return
343 for (devnr = 0; devnr < tsc->stripe_num; ++devnr) {
344 if (tsc->stripe_devs[devnr].pdev->pdev_vnode->v_rdev == NULL)
347 dev_ddump(tsc->stripe_devs[devnr].pdev->pdev_vnode->v_rdev,
353 while (num_blks > 0) {
354 /* blockno to strip piece nr */
355 stripe = blkno / tsc->stripe_chunksize;
356 stripe_off = blkno % tsc->stripe_chunksize;
358 /* where we are inside the strip */
359 devnr = stripe % tsc->stripe_num;
360 blknr = stripe / tsc->stripe_num;
362 /* how much is left before we hit a boundary */
363 stripe_rest = tsc->stripe_chunksize - stripe_off;
365 /* issue this piece on stripe `stripe' */
366 issue_blks = MIN(stripe_rest, num_blks);
369 nestiobuf_add(bio, nestbuf, blkoff,
370 issue_blks * DEV_BSIZE);
372 len2 = issue_blks * DEV_BSIZE;
374 /* I need number of bytes. */
375 off2 = blknr * tsc->stripe_chunksize + stripe_off;
376 off2 += tsc->stripe_devs[devnr].offset;
378 off2 = dm_pdev_correct_dump_offset(tsc->stripe_devs[devnr].pdev,
381 if (tsc->stripe_devs[devnr].pdev->pdev_vnode->v_rdev == NULL)
384 dev_ddump(tsc->stripe_devs[devnr].pdev->pdev_vnode->v_rdev,
385 (char *)data + blkoff, 0, off2, len2);
388 blkoff += issue_blks * DEV_BSIZE;
389 num_blks -= issue_blks;
396 * Destroy a dm table entry for stripes.
399 dm_target_stripe_destroy(dm_table_entry_t *table_en)
401 dm_target_stripe_config_t *tsc;
403 if ((tsc = table_en->target_config) != NULL) {
404 table_en->target_config = NULL;
405 dm_target_stripe_destroy_config(tsc);
412 dm_target_stripe_destroy_config(dm_target_stripe_config_t *tsc)
416 for (n = 0; n < tsc->stripe_num; ++n) {
417 if (tsc->stripe_devs[n].pdev) {
418 dm_pdev_decr(tsc->stripe_devs[n].pdev);
419 tsc->stripe_devs[n].pdev = NULL;
422 kfree(tsc, M_DMSTRIPE);
426 * Unsupported for this target.
429 dm_target_stripe_upcall(dm_table_entry_t *table_en, struct buf *bp)
435 dmts_mod_handler(module_t mod, int type, void *unused)
437 dm_target_t *dmt = NULL;
442 if ((dmt = dm_target_lookup("striped")) != NULL) {
443 dm_target_unbusy(dmt);
446 dmt = dm_target_alloc("striped");
450 strlcpy(dmt->name, "striped", DM_MAX_TYPE_NAME);
451 dmt->init = &dm_target_stripe_init;
452 dmt->info = &dm_target_stripe_info;
453 dmt->table = &dm_target_stripe_table;
454 dmt->strategy = &dm_target_stripe_strategy;
455 dmt->destroy = &dm_target_stripe_destroy;
456 dmt->upcall = &dm_target_stripe_upcall;
457 dmt->dump = &dm_target_stripe_dump;
459 err = dm_target_insert(dmt);
461 kprintf("dm_target_striped: Successfully initialized\n");
465 err = dm_target_rem("striped");
467 kprintf("dm_target_striped: unloaded\n");
477 DM_TARGET_MODULE(dm_target_striped, dmts_mod_handler);