sys/dev/disk/dm/striped/dm_target_striped.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 | /* * Copyright (c) 2009 The NetBSD Foundation, Inc. * All rights reserved. * * This code is derived from software contributed to The NetBSD Foundation * by Adam Hamsik. * * This code is further derived from software contributed to the * DragonFly project by Alex Hornung and Matthew Dillon * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * $NetBSD: dm_target_stripe.c,v 1.9 2010/01/04 00:14:41 haad Exp $ */ /* * This file implements initial version of device-mapper stripe target. * * DragonFly changes: Increase to an unlimited number of stripes */ #include <dev/disk/dm/dm.h> #include <sys/malloc.h> /* for malloc macros, dm.h includes sys/param.h */ MALLOC_DEFINE(M_DMSTRIPE, "dm_striped", "Device Mapper Target Striped"); #define MAX_STRIPES 32 /* #define USE_NUM_ERROR */ struct target_stripe_dev { dm_pdev_t *pdev; uint64_t offset; int num_error; }; typedef struct target_stripe_config { int stripe_num; uint64_t stripe_chunksize; struct target_stripe_dev stripe_devs[0]; } dm_target_stripe_config_t; static void dm_target_stripe_destroy_config(dm_target_stripe_config_t *tsc); /* * Init function called from dm_table_load_ioctl. * * Example line sent to dm from lvm tools when using striped target. * start length striped #stripes chunk_size device1 offset1 ... deviceN offsetN */ static int dm_target_stripe_init(dm_table_entry_t *table_en, int argc, char **argv) { dm_target_stripe_config_t *tsc; char *arg; int i, n, siz, chunksize; if (argc < 2) { kprintf("Striped target takes 2 or more args\n"); return EINVAL; } n = (int)atoi64(argv[0]); if (n <= 0 || n > MAX_STRIPES) { kprintf("dm: Error %d stripes not supported (%d max)\n", n, MAX_STRIPES); return ENOTSUP; } #if 0 if (table_en->length % n) { kprintf("dm: Target device size not multiple of stripes\n"); return EINVAL; } #endif if (argc != (2 + n * 2)) { kprintf("dm: Invalid argc %d for %d stripe devices\n", argc, n); return EINVAL; } chunksize = atoi64(argv[1]); if (chunksize < 1 || chunksize * DEV_BSIZE > MAXPHYS) { kprintf("dm: Error unsupported chunk size %jdKB\n", (intmax_t)chunksize * DEV_BSIZE / 1024); return EINVAL; } #if 0 if ((table_en->length / n) % chunksize) { kprintf("dm: Stripe device size not multiple of chunk size\n"); return EINVAL; } #endif siz = sizeof(dm_target_stripe_config_t) + n * sizeof(struct target_stripe_dev); tsc = kmalloc(siz, M_DMSTRIPE, M_WAITOK | M_ZERO); if (tsc == NULL) return ENOMEM; tsc->stripe_num = n; tsc->stripe_chunksize = chunksize; /* * Parse the devices */ kprintf("dm: Stripe %d devices chunk size %dKB\n", (int)tsc->stripe_num, (int)tsc->stripe_chunksize ); argv += 2; for (n = 0, i = 0; n < tsc->stripe_num; ++n) { arg = argv[i++]; KKASSERT(arg); tsc->stripe_devs[n].pdev = dm_pdev_insert(arg); if (tsc->stripe_devs[n].pdev == NULL) break; arg = argv[i++]; KKASSERT(arg); tsc->stripe_devs[n].offset = atoi64(arg); dm_table_add_deps(table_en, tsc->stripe_devs[n].pdev); } if (n != tsc->stripe_num) { dm_target_stripe_destroy_config(tsc); return (ENOENT); } dm_table_init_target(table_en, tsc); return 0; } /* * Info routine called to get params string. */ static char * dm_target_stripe_info(void *target_config) { dm_target_stripe_config_t *tsc; char *params; char *ptr; char buf[MAX_STRIPES + 1]; size_t len; int ret; int i; tsc = target_config; len = DM_MAX_PARAMS_SIZE; params = dm_alloc_string(len); ptr = params; ret = ksnprintf(ptr, len, "%d ", tsc->stripe_num); ptr += ret; len -= ret; memset(buf, 0, sizeof(buf)); for (i = 0; i < tsc->stripe_num; i++) { ret = ksnprintf(ptr, len, "%s ", tsc->stripe_devs[i].pdev->udev_name); if (tsc->stripe_devs[i].num_error) /* no lock */ buf[i] = 'D'; else buf[i] = 'A'; ptr += ret; len -= ret; } ret = ksnprintf(ptr, len, "1 %s", buf); ptr += ret; len -= ret; return params; } /* * Table routine called to get params string. */ static char * dm_target_stripe_table(void *target_config) { dm_target_stripe_config_t *tsc; char *params; char *ptr; size_t len; int ret; int i; tsc = target_config; len = DM_MAX_PARAMS_SIZE; params = dm_alloc_string(len); ptr = params; ret = ksnprintf(ptr, len, "%d %jd", tsc->stripe_num, (intmax_t)tsc->stripe_chunksize); ptr += ret; len -= ret; for (i = 0; i < tsc->stripe_num; i++) { ret = ksnprintf(ptr, len, " %s %jd", tsc->stripe_devs[i].pdev->udev_name, (intmax_t)tsc->stripe_devs[i].offset); ptr += ret; len -= ret; } return params; } #ifdef USE_NUM_ERROR static void dm_target_stripe_iodone(struct bio *bio) { struct bio *obio; struct buf *bp; dm_target_stripe_config_t *tsc; bp = bio->bio_buf; tsc = bio->bio_caller_info1.ptr; if (bp->b_error) { int devnr; uint64_t blkno, stripe; blkno = bio->bio_offset / DEV_BSIZE; stripe = blkno / tsc->stripe_chunksize; devnr = stripe % tsc->stripe_num; KKASSERT(devnr < MAX_STRIPES); tsc->stripe_devs[devnr].num_error++; dmdebug("device=%d error=%d\n", devnr, bp->b_error); } obio = pop_bio(bio); biodone(obio); } static __inline struct bio *get_stripe_bio(struct bio *bio, void *priv) { struct bio *nbio; nbio = push_bio(bio); nbio->bio_caller_info1.ptr = priv; nbio->bio_done = dm_target_stripe_iodone; return nbio; } #else static __inline struct bio *get_stripe_bio(struct bio *bio, void *priv __unused) { return bio; } #endif /* * Strategy routine called from dm_strategy. */ static int dm_target_stripe_strategy(dm_table_entry_t *table_en, struct buf *bp) { dm_target_stripe_config_t *tsc; struct bio *bio = &bp->b_bio1; struct bio *nbio; struct buf *nestbuf; struct target_stripe_dev *dev; uint64_t blkno, blkoff; uint64_t stripe, blknr; uint32_t stripe_off, stripe_rest, num_blks, issue_blks; int devnr; tsc = table_en->target_config; if (tsc == NULL) return 0; /* calculate extent of request */ KKASSERT(bp->b_resid % DEV_BSIZE == 0); switch(bp->b_cmd) { case BUF_CMD_READ: case BUF_CMD_WRITE: case BUF_CMD_FREEBLKS: /* * Loop through to individual operations */ blkno = bio->bio_offset / DEV_BSIZE; blkoff = 0; num_blks = bp->b_resid / DEV_BSIZE; nestiobuf_init(bio); while (num_blks > 0) { /* blockno to stripe piece nr */ stripe = blkno / tsc->stripe_chunksize; stripe_off = blkno % tsc->stripe_chunksize; /* where we are inside the stripe */ devnr = stripe % tsc->stripe_num; blknr = stripe / tsc->stripe_num; dev = &tsc->stripe_devs[devnr]; /* how much is left before we hit a boundary */ stripe_rest = tsc->stripe_chunksize - stripe_off; /* issue this piece on stripe `stripe' */ issue_blks = MIN(stripe_rest, num_blks); nestbuf = getpbuf(NULL); nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS; nestiobuf_add(bio, nestbuf, blkoff, issue_blks * DEV_BSIZE, NULL); nbio = get_stripe_bio(&nestbuf->b_bio1, tsc); nbio->bio_offset = blknr * tsc->stripe_chunksize; nbio->bio_offset += stripe_off; nbio->bio_offset += dev->offset; nbio->bio_offset *= DEV_BSIZE; vn_strategy(dev->pdev->pdev_vnode, nbio); blkno += issue_blks; blkoff += issue_blks * DEV_BSIZE; num_blks -= issue_blks; } nestiobuf_start(bio); break; case BUF_CMD_FLUSH: nestiobuf_init(bio); for (devnr = 0; devnr < tsc->stripe_num; ++devnr) { dev = &tsc->stripe_devs[devnr]; nestbuf = getpbuf(NULL); nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS; nestiobuf_add(bio, nestbuf, 0, 0, NULL); nbio = get_stripe_bio(&nestbuf->b_bio1, tsc); nbio->bio_offset = 0; vn_strategy(dev->pdev->pdev_vnode, nbio); } nestiobuf_start(bio); break; default: bp->b_flags |= B_ERROR; bp->b_error = EIO; biodone(bio); break; } return 0; } static int dm_target_stripe_dump(dm_table_entry_t *table_en, void *data, size_t length, off_t offset) { dm_target_stripe_config_t *tsc; uint64_t blkno, blkoff; uint64_t stripe, blknr; uint32_t stripe_off, stripe_rest, num_blks, issue_blks; uint64_t off2, len2; int devnr; tsc = table_en->target_config; if (tsc == NULL) return 0; /* calculate extent of request */ KKASSERT(length % DEV_BSIZE == 0); blkno = offset / DEV_BSIZE; blkoff = 0; num_blks = length / DEV_BSIZE; /* * 0 length means flush buffers and return */ if (length == 0) { for (devnr = 0; devnr < tsc->stripe_num; ++devnr) { if (tsc->stripe_devs[devnr].pdev->pdev_vnode->v_rdev == NULL) return ENXIO; dev_ddump(tsc->stripe_devs[devnr].pdev->pdev_vnode->v_rdev, data, 0, offset, 0); } return 0; } while (num_blks > 0) { /* blockno to stripe piece nr */ stripe = blkno / tsc->stripe_chunksize; stripe_off = blkno % tsc->stripe_chunksize; /* where we are inside the stripe */ devnr = stripe % tsc->stripe_num; blknr = stripe / tsc->stripe_num; /* how much is left before we hit a boundary */ stripe_rest = tsc->stripe_chunksize - stripe_off; /* issue this piece on stripe `stripe' */ issue_blks = MIN(stripe_rest, num_blks); #if 0 nestiobuf_add(bio, nestbuf, blkoff, issue_blks * DEV_BSIZE); #endif len2 = issue_blks * DEV_BSIZE; /* I need number of bytes. */ off2 = blknr * tsc->stripe_chunksize + stripe_off; off2 += tsc->stripe_devs[devnr].offset; off2 *= DEV_BSIZE; off2 = dm_pdev_correct_dump_offset(tsc->stripe_devs[devnr].pdev, off2); if (tsc->stripe_devs[devnr].pdev->pdev_vnode->v_rdev == NULL) return ENXIO; dev_ddump(tsc->stripe_devs[devnr].pdev->pdev_vnode->v_rdev, (char *)data + blkoff, 0, off2, len2); blkno += issue_blks; blkoff += issue_blks * DEV_BSIZE; num_blks -= issue_blks; } return 0; } /* * Destroy a dm table entry for stripes. */ static int dm_target_stripe_destroy(dm_table_entry_t *table_en) { if (table_en->target_config != NULL) dm_target_stripe_destroy_config(table_en->target_config); return 0; } static void dm_target_stripe_destroy_config(dm_target_stripe_config_t *tsc) { int n; for (n = 0; n < tsc->stripe_num; ++n) { if (tsc->stripe_devs[n].pdev) { dm_pdev_decr(tsc->stripe_devs[n].pdev); tsc->stripe_devs[n].pdev = NULL; } } kfree(tsc, M_DMSTRIPE); } static int dmts_mod_handler(module_t mod, int type, void *unused) { dm_target_t *dmt = NULL; int err = 0; switch(type) { case MOD_LOAD: if ((dmt = dm_target_lookup("striped")) != NULL) { dm_target_unbusy(dmt); return EEXIST; } dmt = dm_target_alloc("striped"); dmt->version[0] = 1; dmt->version[1] = 0; dmt->version[2] = 3; dmt->init = &dm_target_stripe_init; dmt->destroy = &dm_target_stripe_destroy; dmt->strategy = &dm_target_stripe_strategy; dmt->table = &dm_target_stripe_table; dmt->info = &dm_target_stripe_info; dmt->dump = &dm_target_stripe_dump; dmt->max_argc = 2 + (MAX_STRIPES * 2); err = dm_target_insert(dmt); if (err == 0) kprintf("dm_target_striped: Successfully initialized\n"); break; case MOD_UNLOAD: err = dm_target_remove("striped"); if (err == 0) kprintf("dm_target_striped: unloaded\n"); break; } return err; } DM_TARGET_MODULE(dm_target_striped, dmts_mod_handler); |