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_stripe", "Device Mapper Target Stripe");
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_dev_t *dmv, void **target_config, 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);
133 if (n != tsc->stripe_num) {
134 dm_target_stripe_destroy_config(tsc);
138 *target_config = tsc;
140 dmv->dev_type = DM_STRIPE_DEV;
146 * Status routine called to get params string.
149 dm_target_stripe_status(void *target_config)
151 dm_target_stripe_config_t *tsc;
160 /* caller expects use of M_DM for returned params */
161 nlen = DM_MAX_PARAMS_SIZE;
162 params = kmalloc(nlen, M_DM, M_WAITOK);
165 ksnprintf(ptr, nlen, "%d %jd",
166 tsc->stripe_num, (intmax_t)tsc->stripe_chunksize);
167 len = strlen(params);
171 for (n = 0; n < tsc->stripe_num; ++n) {
172 ksnprintf(ptr, nlen, " %s %jd",
173 tsc->stripe_devs[n].pdev->name,
174 (intmax_t)tsc->stripe_devs[n].offset);
184 * Strategy routine called from dm_strategy.
187 dm_target_stripe_strategy(dm_table_entry_t *table_en, struct buf *bp)
189 dm_target_stripe_config_t *tsc;
190 struct bio *bio = &bp->b_bio1;
192 uint64_t blkno, blkoff;
193 uint64_t stripe, blknr;
194 uint32_t stripe_off, stripe_rest, num_blks, issue_blks;
197 tsc = table_en->target_config;
201 /* calculate extent of request */
202 KKASSERT(bp->b_resid % DEV_BSIZE == 0);
207 case BUF_CMD_FREEBLKS:
209 * Loop through to individual operations
211 blkno = bp->b_bio1.bio_offset / DEV_BSIZE;
213 num_blks = bp->b_resid / DEV_BSIZE;
216 while (num_blks > 0) {
217 /* blockno to strip piece nr */
218 stripe = blkno / tsc->stripe_chunksize;
219 stripe_off = blkno % tsc->stripe_chunksize;
221 /* where we are inside the strip */
222 devnr = stripe % tsc->stripe_num;
223 blknr = stripe / tsc->stripe_num;
225 /* how much is left before we hit a boundary */
226 stripe_rest = tsc->stripe_chunksize - stripe_off;
228 /* issue this piece on stripe `stripe' */
229 issue_blks = MIN(stripe_rest, num_blks);
230 nestbuf = getpbuf(NULL);
231 nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS;
233 nestiobuf_add(bio, nestbuf, blkoff,
234 issue_blks * DEV_BSIZE, NULL);
236 /* I need number of bytes. */
237 nestbuf->b_bio1.bio_offset =
238 blknr * tsc->stripe_chunksize + stripe_off;
239 nestbuf->b_bio1.bio_offset +=
240 tsc->stripe_devs[devnr].offset;
241 nestbuf->b_bio1.bio_offset *= DEV_BSIZE;
243 vn_strategy(tsc->stripe_devs[devnr].pdev->pdev_vnode,
247 blkoff += issue_blks * DEV_BSIZE;
248 num_blks -= issue_blks;
250 nestiobuf_start(bio);
254 for (devnr = 0; devnr < tsc->stripe_num; ++devnr) {
255 nestbuf = getpbuf(NULL);
256 nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS;
258 nestiobuf_add(bio, nestbuf, 0, 0, NULL);
259 nestbuf->b_bio1.bio_offset = 0;
260 vn_strategy(tsc->stripe_devs[devnr].pdev->pdev_vnode,
263 nestiobuf_start(bio);
266 bp->b_flags |= B_ERROR;
276 dm_target_stripe_dump(dm_table_entry_t *table_en, void *data, size_t length, off_t offset)
278 dm_target_stripe_config_t *tsc;
279 uint64_t blkno, blkoff;
280 uint64_t stripe, blknr;
281 uint32_t stripe_off, stripe_rest, num_blks, issue_blks;
285 tsc = table_en->target_config;
289 /* calculate extent of request */
290 KKASSERT(length % DEV_BSIZE == 0);
292 blkno = offset / DEV_BSIZE;
294 num_blks = length / DEV_BSIZE;
297 * 0 length means flush buffers and return
300 for (devnr = 0; devnr < tsc->stripe_num; ++devnr) {
301 if (tsc->stripe_devs[devnr].pdev->pdev_vnode->v_rdev == NULL)
304 dev_ddump(tsc->stripe_devs[devnr].pdev->pdev_vnode->v_rdev,
310 while (num_blks > 0) {
311 /* blockno to strip piece nr */
312 stripe = blkno / tsc->stripe_chunksize;
313 stripe_off = blkno % tsc->stripe_chunksize;
315 /* where we are inside the strip */
316 devnr = stripe % tsc->stripe_num;
317 blknr = stripe / tsc->stripe_num;
319 /* how much is left before we hit a boundary */
320 stripe_rest = tsc->stripe_chunksize - stripe_off;
322 /* issue this piece on stripe `stripe' */
323 issue_blks = MIN(stripe_rest, num_blks);
326 nestiobuf_add(bio, nestbuf, blkoff,
327 issue_blks * DEV_BSIZE);
329 len2 = issue_blks * DEV_BSIZE;
331 /* I need number of bytes. */
332 off2 = blknr * tsc->stripe_chunksize + stripe_off;
333 off2 += tsc->stripe_devs[devnr].offset;
335 off2 = dm_pdev_correct_dump_offset(tsc->stripe_devs[devnr].pdev,
338 if (tsc->stripe_devs[devnr].pdev->pdev_vnode->v_rdev == NULL)
341 dev_ddump(tsc->stripe_devs[devnr].pdev->pdev_vnode->v_rdev,
342 (char *)data + blkoff, 0, off2, len2);
345 blkoff += issue_blks * DEV_BSIZE;
346 num_blks -= issue_blks;
353 * Destroy a dm table entry for stripes.
356 dm_target_stripe_destroy(dm_table_entry_t *table_en)
358 dm_target_stripe_config_t *tsc;
360 if ((tsc = table_en->target_config) != NULL) {
361 table_en->target_config = NULL;
362 dm_target_stripe_destroy_config(tsc);
369 dm_target_stripe_destroy_config(dm_target_stripe_config_t *tsc)
373 for (n = 0; n < tsc->stripe_num; ++n) {
374 if (tsc->stripe_devs[n].pdev) {
375 dm_pdev_decr(tsc->stripe_devs[n].pdev);
376 tsc->stripe_devs[n].pdev = NULL;
379 kfree(tsc, M_DMSTRIPE);
383 * Generate properties from stripe table entry.
386 dm_target_stripe_deps(dm_table_entry_t *table_en, prop_array_t prop_array)
388 dm_target_stripe_config_t *tsc;
393 if (table_en->target_config == NULL)
396 tsc = table_en->target_config;
398 for (n = 0; n < tsc->stripe_num; ++n) {
399 error = VOP_GETATTR(tsc->stripe_devs[n].pdev->pdev_vnode, &va);
402 prop_array_add_uint64(prop_array,
403 (uint64_t)makeudev(va.va_rmajor, va.va_rminor));
409 * Unsupported for this target.
412 dm_target_stripe_upcall(dm_table_entry_t *table_en, struct buf *bp)
418 dmts_mod_handler(module_t mod, int type, void *unused)
420 dm_target_t *dmt = NULL;
425 if ((dmt = dm_target_lookup("striped")) != NULL) {
426 dm_target_unbusy(dmt);
429 dmt = dm_target_alloc("striped");
433 strlcpy(dmt->name, "striped", DM_MAX_TYPE_NAME);
434 dmt->init = &dm_target_stripe_init;
435 dmt->status = &dm_target_stripe_status;
436 dmt->strategy = &dm_target_stripe_strategy;
437 dmt->deps = &dm_target_stripe_deps;
438 dmt->destroy = &dm_target_stripe_destroy;
439 dmt->upcall = &dm_target_stripe_upcall;
440 dmt->dump = &dm_target_stripe_dump;
442 err = dm_target_insert(dmt);
444 kprintf("dm_target_stripe: Successfully initialized\n");
448 err = dm_target_rem("striped");
450 kprintf("dm_target_stripe: unloaded\n");
460 DM_TARGET_MODULE(dm_target_striped, dmts_mod_handler);