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);
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");
89 tsc->stripe_chunksize = atoi64(ap);
90 if (tsc->stripe_chunksize < 1 ||
91 tsc->stripe_chunksize * DEV_BSIZE > MAXPHYS) {
92 kprintf("dm: Error unsupported chunk size %jdKB\n",
93 (intmax_t)tsc->stripe_chunksize * DEV_BSIZE / 1024);
94 dm_target_stripe_destroy_config(tsc);
102 kprintf("dm: Stripe %d devices chunk size %dKB\n",
103 (int)tsc->stripe_num,
104 (int)tsc->stripe_chunksize
107 for (n = 0; n < tsc->stripe_num; ++n) {
108 ap = strsep(¶ms, " \t");
111 tsc->stripe_devs[n].pdev = dm_pdev_insert(ap);
112 if (tsc->stripe_devs[n].pdev == NULL)
114 ap = strsep(¶ms, " \t");
117 tsc->stripe_devs[n].offset = atoi64(ap);
119 if (n != tsc->stripe_num) {
120 dm_target_stripe_destroy_config(tsc);
124 *target_config = tsc;
126 dmv->dev_type = DM_STRIPE_DEV;
132 * Status routine called to get params string.
135 dm_target_stripe_status(void *target_config)
137 dm_target_stripe_config_t *tsc;
146 /* caller expects use of M_DM for returned params */
147 nlen = DM_MAX_PARAMS_SIZE;
148 params = kmalloc(nlen, M_DM, M_WAITOK);
151 ksnprintf(ptr, nlen, "%d %jd",
152 tsc->stripe_num, (intmax_t)tsc->stripe_chunksize);
153 len = strlen(params);
157 for (n = 0; n < tsc->stripe_num; ++n) {
158 ksnprintf(ptr, nlen, " %s %jd",
159 tsc->stripe_devs[n].pdev->name,
160 (intmax_t)tsc->stripe_devs[n].offset);
170 * Strategy routine called from dm_strategy.
173 dm_target_stripe_strategy(dm_table_entry_t *table_en, struct buf *bp)
175 dm_target_stripe_config_t *tsc;
176 struct bio *bio = &bp->b_bio1;
178 uint64_t blkno, blkoff;
179 uint64_t stripe, blknr;
180 uint32_t stripe_off, stripe_rest, num_blks, issue_blks;
183 tsc = table_en->target_config;
187 /* calculate extent of request */
188 KKASSERT(bp->b_resid % DEV_BSIZE == 0);
193 case BUF_CMD_FREEBLKS:
195 * Loop through to individual operations
197 blkno = bp->b_bio1.bio_offset / DEV_BSIZE;
199 num_blks = bp->b_resid / DEV_BSIZE;
202 while (num_blks > 0) {
203 /* blockno to strip piece nr */
204 stripe = blkno / tsc->stripe_chunksize;
205 stripe_off = blkno % tsc->stripe_chunksize;
207 /* where we are inside the strip */
208 devnr = stripe % tsc->stripe_num;
209 blknr = stripe / tsc->stripe_num;
211 /* how much is left before we hit a boundary */
212 stripe_rest = tsc->stripe_chunksize - stripe_off;
214 /* issue this piece on stripe `stripe' */
215 issue_blks = MIN(stripe_rest, num_blks);
216 nestbuf = getpbuf(NULL);
217 nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS;
219 nestiobuf_add(bio, nestbuf, blkoff,
220 issue_blks * DEV_BSIZE, NULL);
222 /* I need number of bytes. */
223 nestbuf->b_bio1.bio_offset =
224 blknr * tsc->stripe_chunksize + stripe_off;
225 nestbuf->b_bio1.bio_offset +=
226 tsc->stripe_devs[devnr].offset;
227 nestbuf->b_bio1.bio_offset *= DEV_BSIZE;
229 vn_strategy(tsc->stripe_devs[devnr].pdev->pdev_vnode,
233 blkoff += issue_blks * DEV_BSIZE;
234 num_blks -= issue_blks;
236 nestiobuf_start(bio);
240 for (devnr = 0; devnr < tsc->stripe_num; ++devnr) {
241 nestbuf = getpbuf(NULL);
242 nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS;
244 nestiobuf_add(bio, nestbuf, 0, 0, NULL);
245 nestbuf->b_bio1.bio_offset = 0;
246 vn_strategy(tsc->stripe_devs[devnr].pdev->pdev_vnode,
249 nestiobuf_start(bio);
252 bp->b_flags |= B_ERROR;
262 dm_target_stripe_dump(dm_table_entry_t *table_en, void *data, size_t length, off_t offset)
264 dm_target_stripe_config_t *tsc;
265 uint64_t blkno, blkoff;
266 uint64_t stripe, blknr;
267 uint32_t stripe_off, stripe_rest, num_blks, issue_blks;
271 tsc = table_en->target_config;
275 /* calculate extent of request */
276 KKASSERT(length % DEV_BSIZE == 0);
278 blkno = offset / DEV_BSIZE;
280 num_blks = length / DEV_BSIZE;
283 * 0 length means flush buffers and return
286 for (devnr = 0; devnr < tsc->stripe_num; ++devnr) {
287 if (tsc->stripe_devs[devnr].pdev->pdev_vnode->v_rdev == NULL)
290 dev_ddump(tsc->stripe_devs[devnr].pdev->pdev_vnode->v_rdev,
296 while (num_blks > 0) {
297 /* blockno to strip piece nr */
298 stripe = blkno / tsc->stripe_chunksize;
299 stripe_off = blkno % tsc->stripe_chunksize;
301 /* where we are inside the strip */
302 devnr = stripe % tsc->stripe_num;
303 blknr = stripe / tsc->stripe_num;
305 /* how much is left before we hit a boundary */
306 stripe_rest = tsc->stripe_chunksize - stripe_off;
308 /* issue this piece on stripe `stripe' */
309 issue_blks = MIN(stripe_rest, num_blks);
312 nestiobuf_add(bio, nestbuf, blkoff,
313 issue_blks * DEV_BSIZE);
315 len2 = issue_blks * DEV_BSIZE;
317 /* I need number of bytes. */
318 off2 = blknr * tsc->stripe_chunksize + stripe_off;
319 off2 += tsc->stripe_devs[devnr].offset;
321 off2 = dm_pdev_correct_dump_offset(tsc->stripe_devs[devnr].pdev,
324 if (tsc->stripe_devs[devnr].pdev->pdev_vnode->v_rdev == NULL)
327 dev_ddump(tsc->stripe_devs[devnr].pdev->pdev_vnode->v_rdev,
328 (char *)data + blkoff, 0, off2, len2);
331 blkoff += issue_blks * DEV_BSIZE;
332 num_blks -= issue_blks;
339 * Destroy a dm table entry for stripes.
342 dm_target_stripe_destroy(dm_table_entry_t *table_en)
344 dm_target_stripe_config_t *tsc;
346 if ((tsc = table_en->target_config) != NULL) {
347 table_en->target_config = NULL;
348 dm_target_stripe_destroy_config(tsc);
351 /* Unbusy target so we can unload it */
352 dm_target_unbusy(table_en->target);
358 dm_target_stripe_destroy_config(dm_target_stripe_config_t *tsc)
362 for (n = 0; n < tsc->stripe_num; ++n) {
363 if (tsc->stripe_devs[n].pdev) {
364 dm_pdev_decr(tsc->stripe_devs[n].pdev);
365 tsc->stripe_devs[n].pdev = NULL;
368 kfree(tsc, M_DMSTRIPE);
372 * Generate properties from stripe table entry.
375 dm_target_stripe_deps(dm_table_entry_t *table_en, prop_array_t prop_array)
377 dm_target_stripe_config_t *tsc;
382 if (table_en->target_config == NULL)
385 tsc = table_en->target_config;
387 for (n = 0; n < tsc->stripe_num; ++n) {
388 error = VOP_GETATTR(tsc->stripe_devs[n].pdev->pdev_vnode, &va);
391 prop_array_add_uint64(prop_array,
392 (uint64_t)makeudev(va.va_rmajor, va.va_rminor));
398 * Unsupported for this target.
401 dm_target_stripe_upcall(dm_table_entry_t * table_en, struct buf * bp)