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>
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);
52 #ifdef DM_TARGET_MODULE
54 * Every target can be compiled directly to dm driver or as a
55 * separate module this part of target is used for loading targets
57 * Target can be unloaded from kernel only if there are no users of
58 * it e.g. there are no devices which uses that target.
60 #include <sys/kernel.h>
61 #include <sys/module.h>
64 dm_target_stripe_modcmd(modcmd_t cmd, void *arg)
72 if ((dmt = dm_target_lookup("stripe")) != NULL) {
73 dm_target_unbusy(dmt);
76 dmt = dm_target_alloc("stripe");
81 strlcpy(dmt->name, "stripe", DM_MAX_TYPE_NAME);
82 dmt->init = &dm_target_stripe_init;
83 dmt->status = &dm_target_stripe_status;
84 dmt->strategy = &dm_target_stripe_strategy;
85 dmt->deps = &dm_target_stripe_deps;
86 dmt->destroy = &dm_target_stripe_destroy;
87 dmt->upcall = &dm_target_stripe_upcall;
89 r = dm_target_insert(dmt);
94 r = dm_target_rem("stripe");
109 * Init function called from dm_table_load_ioctl.
111 * Example line sent to dm from lvm tools when using striped target.
112 * start length striped #stripes chunk_size device1 offset1 ... deviceN offsetN
113 * 0 65536 striped 2 512 /dev/hda 0 /dev/hdb 0
116 dm_target_stripe_init(dm_dev_t *dmv, void **target_config, char *params)
118 dm_target_stripe_config_t *tsc;
128 ap = strsep(¶ms, " \t");
132 if (n < 0 || n > MAX_STRIPES) {
133 kprintf("dm: Error %d stripes not supported (%d max)\n",
138 tsc = kmalloc(sizeof(dm_target_stripe_config_t),
139 M_DMSTRIPE, M_WAITOK | M_ZERO);
142 ap = strsep(¶ms, " \t");
145 tsc->stripe_chunksize = atoi64(ap);
146 if (tsc->stripe_chunksize < 1 ||
147 tsc->stripe_chunksize * DEV_BSIZE > MAXPHYS) {
148 kprintf("dm: Error unsupported chunk size %jdKB\n",
149 (intmax_t)tsc->stripe_chunksize * DEV_BSIZE / 1024);
150 dm_target_stripe_destroy_config(tsc);
158 kprintf("dm: Stripe %d devices chunk size %dKB\n",
159 (int)tsc->stripe_num,
160 (int)tsc->stripe_chunksize
163 for (n = 0; n < tsc->stripe_num; ++n) {
164 ap = strsep(¶ms, " \t");
167 tsc->stripe_devs[n].pdev = dm_pdev_insert(ap);
168 if (tsc->stripe_devs[n].pdev == NULL)
170 ap = strsep(¶ms, " \t");
173 tsc->stripe_devs[n].offset = atoi64(ap);
175 if (n != tsc->stripe_num) {
176 dm_target_stripe_destroy_config(tsc);
180 *target_config = tsc;
182 dmv->dev_type = DM_STRIPE_DEV;
188 * Status routine called to get params string.
191 dm_target_stripe_status(void *target_config)
193 dm_target_stripe_config_t *tsc;
202 /* caller expects use of M_DM for returned params */
203 nlen = DM_MAX_PARAMS_SIZE;
204 params = kmalloc(nlen, M_DM, M_WAITOK);
207 ksnprintf(ptr, nlen, "%d %jd",
208 tsc->stripe_num, (intmax_t)tsc->stripe_chunksize);
209 len = strlen(params);
213 for (n = 0; n < tsc->stripe_num; ++n) {
214 ksnprintf(ptr, nlen, " %s %jd",
215 tsc->stripe_devs[n].pdev->name,
216 (intmax_t)tsc->stripe_devs[n].offset);
226 * Strategy routine called from dm_strategy.
229 dm_target_stripe_strategy(dm_table_entry_t *table_en, struct buf *bp)
231 dm_target_stripe_config_t *tsc;
232 struct bio *bio = &bp->b_bio1;
234 uint64_t blkno, blkoff;
235 uint64_t stripe, stripe_blknr;
236 uint32_t stripe_off, stripe_rest, num_blks, issue_blks;
239 tsc = table_en->target_config;
243 /* calculate extent of request */
244 KKASSERT(bp->b_resid % DEV_BSIZE == 0);
246 blkno = bp->b_bio1.bio_offset / DEV_BSIZE;
248 num_blks = bp->b_resid / DEV_BSIZE;
251 /* blockno to strip piece nr */
252 stripe = blkno / tsc->stripe_chunksize;
253 stripe_off = blkno % tsc->stripe_chunksize;
255 /* where we are inside the strip */
256 stripe_devnr = stripe % tsc->stripe_num;
257 stripe_blknr = stripe / tsc->stripe_num;
259 /* how much is left before we hit a boundary */
260 stripe_rest = tsc->stripe_chunksize - stripe_off;
262 /* issue this piece on stripe `stripe' */
263 issue_blks = MIN(stripe_rest, num_blks);
264 nestbuf = getpbuf(NULL);
266 nestiobuf_setup(bio, nestbuf, blkoff, issue_blks * DEV_BSIZE);
268 /* I need number of bytes. */
269 nestbuf->b_bio1.bio_offset =
270 stripe_blknr * tsc->stripe_chunksize + stripe_off;
271 nestbuf->b_bio1.bio_offset +=
272 tsc->stripe_devs[stripe_devnr].offset;
273 nestbuf->b_bio1.bio_offset *= DEV_BSIZE;
275 vn_strategy(tsc->stripe_devs[stripe_devnr].pdev->pdev_vnode,
279 blkoff += issue_blks * DEV_BSIZE;
280 num_blks -= issue_blks;
290 * Destroy a dm table entry for stripes.
293 dm_target_stripe_destroy(dm_table_entry_t *table_en)
295 dm_target_stripe_config_t *tsc;
297 if ((tsc = table_en->target_config) != NULL) {
298 table_en->target_config = NULL;
299 dm_target_stripe_destroy_config(tsc);
302 /* Unbusy target so we can unload it */
303 dm_target_unbusy(table_en->target);
309 dm_target_stripe_destroy_config(dm_target_stripe_config_t *tsc)
313 for (n = 0; n < tsc->stripe_num; ++n) {
314 if (tsc->stripe_devs[n].pdev) {
315 dm_pdev_decr(tsc->stripe_devs[n].pdev);
316 tsc->stripe_devs[n].pdev = NULL;
319 kfree(tsc, M_DMSTRIPE);
323 * Generate properties from stripe table entry.
326 dm_target_stripe_deps(dm_table_entry_t *table_en, prop_array_t prop_array)
328 dm_target_stripe_config_t *tsc;
333 if (table_en->target_config == NULL)
336 tsc = table_en->target_config;
338 for (n = 0; n < tsc->stripe_num; ++n) {
339 error = VOP_GETATTR(tsc->stripe_devs[n].pdev->pdev_vnode, &va);
342 prop_array_add_uint64(prop_array,
343 (uint64_t)makeudev(va.va_rmajor, va.va_rminor));
349 * Unsupported for this target.
352 dm_target_stripe_upcall(dm_table_entry_t * table_en, struct buf * bp)