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>
41 #include <sys/param.h>
44 #include <sys/malloc.h>
45 #include <sys/vnode.h>
47 #include <dev/disk/dm/dm.h>
48 MALLOC_DEFINE(M_DMSTRIPE, "dm_stripe", "Device Mapper Target Stripe");
50 static void dm_target_stripe_destroy_config(dm_target_stripe_config_t *tsc);
53 * Init function called from dm_table_load_ioctl.
55 * Example line sent to dm from lvm tools when using striped target.
56 * start length striped #stripes chunk_size device1 offset1 ... deviceN offsetN
57 * 0 65536 striped 2 512 /dev/hda 0 /dev/hdb 0
60 dm_target_stripe_init(dm_dev_t *dmv, void **target_config, char *params)
62 dm_target_stripe_config_t *tsc;
72 ap = strsep(¶ms, " \t");
76 if (n < 0 || n > MAX_STRIPES) {
77 kprintf("dm: Error %d stripes not supported (%d max)\n",
82 tsc = kmalloc(sizeof(dm_target_stripe_config_t),
83 M_DMSTRIPE, M_WAITOK | M_ZERO);
86 ap = strsep(¶ms, " \t");
88 dm_target_stripe_destroy_config(tsc);
91 tsc->stripe_chunksize = atoi64(ap);
92 if (tsc->stripe_chunksize < 1 ||
93 tsc->stripe_chunksize * DEV_BSIZE > MAXPHYS) {
94 kprintf("dm: Error unsupported chunk size %jdKB\n",
95 (intmax_t)tsc->stripe_chunksize * DEV_BSIZE / 1024);
96 dm_target_stripe_destroy_config(tsc);
104 kprintf("dm: Stripe %d devices chunk size %dKB\n",
105 (int)tsc->stripe_num,
106 (int)tsc->stripe_chunksize
109 for (n = 0; n < tsc->stripe_num; ++n) {
110 ap = strsep(¶ms, " \t");
113 tsc->stripe_devs[n].pdev = dm_pdev_insert(ap);
114 if (tsc->stripe_devs[n].pdev == NULL)
116 ap = strsep(¶ms, " \t");
119 tsc->stripe_devs[n].offset = atoi64(ap);
121 if (n != tsc->stripe_num) {
122 dm_target_stripe_destroy_config(tsc);
126 *target_config = tsc;
128 dmv->dev_type = DM_STRIPE_DEV;
134 * Status routine called to get params string.
137 dm_target_stripe_status(void *target_config)
139 dm_target_stripe_config_t *tsc;
148 /* caller expects use of M_DM for returned params */
149 nlen = DM_MAX_PARAMS_SIZE;
150 params = kmalloc(nlen, M_DM, M_WAITOK);
153 ksnprintf(ptr, nlen, "%d %jd",
154 tsc->stripe_num, (intmax_t)tsc->stripe_chunksize);
155 len = strlen(params);
159 for (n = 0; n < tsc->stripe_num; ++n) {
160 ksnprintf(ptr, nlen, " %s %jd",
161 tsc->stripe_devs[n].pdev->name,
162 (intmax_t)tsc->stripe_devs[n].offset);
172 * Strategy routine called from dm_strategy.
175 dm_target_stripe_strategy(dm_table_entry_t *table_en, struct buf *bp)
177 dm_target_stripe_config_t *tsc;
178 struct bio *bio = &bp->b_bio1;
180 uint64_t blkno, blkoff;
181 uint64_t stripe, blknr;
182 uint32_t stripe_off, stripe_rest, num_blks, issue_blks;
185 tsc = table_en->target_config;
189 /* calculate extent of request */
190 KKASSERT(bp->b_resid % DEV_BSIZE == 0);
195 case BUF_CMD_FREEBLKS:
197 * Loop through to individual operations
199 blkno = bp->b_bio1.bio_offset / DEV_BSIZE;
201 num_blks = bp->b_resid / DEV_BSIZE;
204 while (num_blks > 0) {
205 /* blockno to strip piece nr */
206 stripe = blkno / tsc->stripe_chunksize;
207 stripe_off = blkno % tsc->stripe_chunksize;
209 /* where we are inside the strip */
210 devnr = stripe % tsc->stripe_num;
211 blknr = stripe / tsc->stripe_num;
213 /* how much is left before we hit a boundary */
214 stripe_rest = tsc->stripe_chunksize - stripe_off;
216 /* issue this piece on stripe `stripe' */
217 issue_blks = MIN(stripe_rest, num_blks);
218 nestbuf = getpbuf(NULL);
219 nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS;
221 nestiobuf_add(bio, nestbuf, blkoff,
222 issue_blks * DEV_BSIZE, NULL);
224 /* I need number of bytes. */
225 nestbuf->b_bio1.bio_offset =
226 blknr * tsc->stripe_chunksize + stripe_off;
227 nestbuf->b_bio1.bio_offset +=
228 tsc->stripe_devs[devnr].offset;
229 nestbuf->b_bio1.bio_offset *= DEV_BSIZE;
231 vn_strategy(tsc->stripe_devs[devnr].pdev->pdev_vnode,
235 blkoff += issue_blks * DEV_BSIZE;
236 num_blks -= issue_blks;
238 nestiobuf_start(bio);
242 for (devnr = 0; devnr < tsc->stripe_num; ++devnr) {
243 nestbuf = getpbuf(NULL);
244 nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS;
246 nestiobuf_add(bio, nestbuf, 0, 0, NULL);
247 nestbuf->b_bio1.bio_offset = 0;
248 vn_strategy(tsc->stripe_devs[devnr].pdev->pdev_vnode,
251 nestiobuf_start(bio);
254 bp->b_flags |= B_ERROR;
264 dm_target_stripe_dump(dm_table_entry_t *table_en, void *data, size_t length, off_t offset)
266 dm_target_stripe_config_t *tsc;
267 uint64_t blkno, blkoff;
268 uint64_t stripe, blknr;
269 uint32_t stripe_off, stripe_rest, num_blks, issue_blks;
273 tsc = table_en->target_config;
277 /* calculate extent of request */
278 KKASSERT(length % DEV_BSIZE == 0);
280 blkno = offset / DEV_BSIZE;
282 num_blks = length / DEV_BSIZE;
285 * 0 length means flush buffers and return
288 for (devnr = 0; devnr < tsc->stripe_num; ++devnr) {
289 if (tsc->stripe_devs[devnr].pdev->pdev_vnode->v_rdev == NULL)
292 dev_ddump(tsc->stripe_devs[devnr].pdev->pdev_vnode->v_rdev,
298 while (num_blks > 0) {
299 /* blockno to strip piece nr */
300 stripe = blkno / tsc->stripe_chunksize;
301 stripe_off = blkno % tsc->stripe_chunksize;
303 /* where we are inside the strip */
304 devnr = stripe % tsc->stripe_num;
305 blknr = stripe / tsc->stripe_num;
307 /* how much is left before we hit a boundary */
308 stripe_rest = tsc->stripe_chunksize - stripe_off;
310 /* issue this piece on stripe `stripe' */
311 issue_blks = MIN(stripe_rest, num_blks);
314 nestiobuf_add(bio, nestbuf, blkoff,
315 issue_blks * DEV_BSIZE);
317 len2 = issue_blks * DEV_BSIZE;
319 /* I need number of bytes. */
320 off2 = blknr * tsc->stripe_chunksize + stripe_off;
321 off2 += tsc->stripe_devs[devnr].offset;
323 off2 = dm_pdev_correct_dump_offset(tsc->stripe_devs[devnr].pdev,
326 if (tsc->stripe_devs[devnr].pdev->pdev_vnode->v_rdev == NULL)
329 dev_ddump(tsc->stripe_devs[devnr].pdev->pdev_vnode->v_rdev,
330 (char *)data + blkoff, 0, off2, len2);
333 blkoff += issue_blks * DEV_BSIZE;
334 num_blks -= issue_blks;
341 * Destroy a dm table entry for stripes.
344 dm_target_stripe_destroy(dm_table_entry_t *table_en)
346 dm_target_stripe_config_t *tsc;
348 if ((tsc = table_en->target_config) != NULL) {
349 table_en->target_config = NULL;
350 dm_target_stripe_destroy_config(tsc);
357 dm_target_stripe_destroy_config(dm_target_stripe_config_t *tsc)
361 for (n = 0; n < tsc->stripe_num; ++n) {
362 if (tsc->stripe_devs[n].pdev) {
363 dm_pdev_decr(tsc->stripe_devs[n].pdev);
364 tsc->stripe_devs[n].pdev = NULL;
367 kfree(tsc, M_DMSTRIPE);
371 * Generate properties from stripe table entry.
374 dm_target_stripe_deps(dm_table_entry_t *table_en, prop_array_t prop_array)
376 dm_target_stripe_config_t *tsc;
381 if (table_en->target_config == NULL)
384 tsc = table_en->target_config;
386 for (n = 0; n < tsc->stripe_num; ++n) {
387 error = VOP_GETATTR(tsc->stripe_devs[n].pdev->pdev_vnode, &va);
390 prop_array_add_uint64(prop_array,
391 (uint64_t)makeudev(va.va_rmajor, va.va_rminor));
397 * Unsupported for this target.
400 dm_target_stripe_upcall(dm_table_entry_t * table_en, struct buf * bp)
406 dmts_mod_handler(module_t mod, int type, void *unused)
408 dm_target_t *dmt = NULL;
413 if ((dmt = dm_target_lookup("striped")) != NULL) {
414 dm_target_unbusy(dmt);
417 dmt = dm_target_alloc("striped");
421 strlcpy(dmt->name, "striped", DM_MAX_TYPE_NAME);
422 dmt->init = &dm_target_stripe_init;
423 dmt->status = &dm_target_stripe_status;
424 dmt->strategy = &dm_target_stripe_strategy;
425 dmt->deps = &dm_target_stripe_deps;
426 dmt->destroy = &dm_target_stripe_destroy;
427 dmt->upcall = &dm_target_stripe_upcall;
428 dmt->dump = &dm_target_stripe_dump;
430 err = dm_target_insert(dmt);
432 kprintf("dm_target_stripe: Successfully initialized\n");
436 err = dm_target_rem("striped");
438 kprintf("dm_target_stripe: unloaded\n");
448 DM_TARGET_MODULE(dm_target_striped, dmts_mod_handler);