| 1 | /*$NetBSD: dm_target_stripe.c,v 1.21 2014/08/19 14:43:28 christos Exp $*/ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 2009 The NetBSD Foundation, Inc. |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * This code is derived from software contributed to The NetBSD Foundation |
| 8 | * by Adam Hamsik. |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or without |
| 11 | * modification, are permitted provided that the following conditions |
| 12 | * are met: |
| 13 | * 1. Redistributions of source code must retain the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer. |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in the |
| 17 | * documentation and/or other materials provided with the distribution. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
| 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
| 23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | * POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | /* |
| 33 | * This file implements initial version of device-mapper stripe target. |
| 34 | */ |
| 35 | #include <sys/types.h> |
| 36 | #include <sys/param.h> |
| 37 | |
| 38 | #include <sys/buf.h> |
| 39 | #include <sys/kmem.h> |
| 40 | #include <sys/vnode.h> |
| 41 | #include <sys/lwp.h> |
| 42 | |
| 43 | #include "dm.h" |
| 44 | |
| 45 | #ifdef DM_TARGET_MODULE |
| 46 | /* |
| 47 | * Every target can be compiled directly to dm driver or as a |
| 48 | * separate module this part of target is used for loading targets |
| 49 | * to dm driver. |
| 50 | * Target can be unloaded from kernel only if there are no users of |
| 51 | * it e.g. there are no devices which uses that target. |
| 52 | */ |
| 53 | #include <sys/kernel.h> |
| 54 | #include <sys/module.h> |
| 55 | |
| 56 | MODULE(MODULE_CLASS_MISC, dm_target_stripe, NULL); |
| 57 | |
| 58 | static int |
| 59 | dm_target_stripe_modcmd(modcmd_t cmd, void *arg) |
| 60 | { |
| 61 | dm_target_t *dmt; |
| 62 | int r; |
| 63 | dmt = NULL; |
| 64 | |
| 65 | switch (cmd) { |
| 66 | case MODULE_CMD_INIT: |
| 67 | if ((dmt = dm_target_lookup("stripe" )) != NULL) { |
| 68 | dm_target_unbusy(dmt); |
| 69 | return EEXIST; |
| 70 | } |
| 71 | dmt = dm_target_alloc("stripe" ); |
| 72 | |
| 73 | dmt->version[0] = 1; |
| 74 | dmt->version[1] = 0; |
| 75 | dmt->version[2] = 0; |
| 76 | strlcpy(dmt->name, "stripe" , DM_MAX_TYPE_NAME); |
| 77 | dmt->init = &dm_target_stripe_init; |
| 78 | dmt->status = &dm_target_stripe_status; |
| 79 | dmt->strategy = &dm_target_stripe_strategy; |
| 80 | dmt->sync = &dm_target_stripe_sync; |
| 81 | dmt->deps = &dm_target_stripe_deps; |
| 82 | dmt->destroy = &dm_target_stripe_destroy; |
| 83 | dmt->upcall = &dm_target_stripe_upcall; |
| 84 | dmt->secsize = &dm_target_stripe_secsize; |
| 85 | |
| 86 | r = dm_target_insert(dmt); |
| 87 | |
| 88 | break; |
| 89 | |
| 90 | case MODULE_CMD_FINI: |
| 91 | r = dm_target_rem("stripe" ); |
| 92 | break; |
| 93 | |
| 94 | case MODULE_CMD_STAT: |
| 95 | return ENOTTY; |
| 96 | |
| 97 | default: |
| 98 | return ENOTTY; |
| 99 | } |
| 100 | |
| 101 | return r; |
| 102 | } |
| 103 | #endif |
| 104 | |
| 105 | static void |
| 106 | dm_target_stripe_fini(dm_target_stripe_config_t *tsc) |
| 107 | { |
| 108 | dm_target_linear_config_t *tlc; |
| 109 | |
| 110 | if (tsc == NULL) |
| 111 | return; |
| 112 | |
| 113 | while ((tlc = TAILQ_FIRST(&tsc->stripe_devs)) != NULL) { |
| 114 | TAILQ_REMOVE(&tsc->stripe_devs, tlc, entries); |
| 115 | dm_pdev_decr(tlc->pdev); |
| 116 | kmem_free(tlc, sizeof(*tlc)); |
| 117 | } |
| 118 | |
| 119 | kmem_free(tsc, sizeof(*tsc)); |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | * Init function called from dm_table_load_ioctl. |
| 124 | * DM_STRIPE_DEV_OFFSET should always hold the index of the first device-offset |
| 125 | * pair in the parameters. |
| 126 | * Example line sent to dm from lvm tools when using striped target. |
| 127 | * start length striped #stripes chunk_size device1 offset1 ... deviceN offsetN |
| 128 | * 0 65536 striped 2 512 /dev/hda 0 /dev/hdb 0 |
| 129 | */ |
| 130 | int |
| 131 | dm_target_stripe_init(dm_dev_t * dmv, void **target_config, char *params) |
| 132 | { |
| 133 | dm_target_linear_config_t *tlc; |
| 134 | dm_target_stripe_config_t *tsc; |
| 135 | size_t len; |
| 136 | char **ap, *argv[10]; |
| 137 | int strpc, strpi; |
| 138 | |
| 139 | if (params == NULL) |
| 140 | return EINVAL; |
| 141 | |
| 142 | len = strlen(params) + 1; |
| 143 | |
| 144 | /* |
| 145 | * Parse a string, containing tokens delimited by white space, |
| 146 | * into an argument vector |
| 147 | */ |
| 148 | for (ap = argv; ap <= &argv[9] && |
| 149 | (*ap = strsep(¶ms, " \t" )) != NULL;) { |
| 150 | if (**ap != '\0') |
| 151 | ap++; |
| 152 | } |
| 153 | |
| 154 | printf("Stripe target init function called!!\n" ); |
| 155 | |
| 156 | printf("Stripe target chunk size %s number of stripes %s\n" , |
| 157 | argv[1], argv[0]); |
| 158 | |
| 159 | if ((tsc = kmem_alloc(sizeof(*tsc), KM_NOSLEEP)) == NULL) |
| 160 | return ENOMEM; |
| 161 | |
| 162 | /* Initialize linked list for striping devices */ |
| 163 | TAILQ_INIT(&tsc->stripe_devs); |
| 164 | |
| 165 | /* Save length of param string */ |
| 166 | tsc->params_len = len; |
| 167 | tsc->stripe_chunksize = atoi(argv[1]); |
| 168 | tsc->stripe_num = (uint8_t) atoi(argv[0]); |
| 169 | |
| 170 | strpc = DM_STRIPE_DEV_OFFSET + (tsc->stripe_num * 2); |
| 171 | for (strpi = DM_STRIPE_DEV_OFFSET; strpi < strpc; strpi += 2) { |
| 172 | printf("Stripe target device name %s -- offset %s\n" , |
| 173 | argv[strpi], argv[strpi+1]); |
| 174 | |
| 175 | tlc = kmem_alloc(sizeof(*tlc), KM_NOSLEEP); |
| 176 | if ((tlc->pdev = dm_pdev_insert(argv[strpi])) == NULL) { |
| 177 | kmem_free(tlc, sizeof(*tlc)); |
| 178 | dm_target_stripe_fini(tsc); |
| 179 | return ENOENT; |
| 180 | } |
| 181 | tlc->offset = atoi(argv[strpi+1]); |
| 182 | |
| 183 | /* Insert striping device to linked list. */ |
| 184 | TAILQ_INSERT_TAIL(&tsc->stripe_devs, tlc, entries); |
| 185 | } |
| 186 | |
| 187 | *target_config = tsc; |
| 188 | |
| 189 | dmv->dev_type = DM_STRIPE_DEV; |
| 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | /* Status routine called to get params string. */ |
| 194 | char * |
| 195 | dm_target_stripe_status(void *target_config) |
| 196 | { |
| 197 | dm_target_linear_config_t *tlc; |
| 198 | dm_target_stripe_config_t *tsc; |
| 199 | char *params, *tmp; |
| 200 | |
| 201 | tsc = target_config; |
| 202 | |
| 203 | if ((params = kmem_alloc(DM_MAX_PARAMS_SIZE, KM_SLEEP)) == NULL) |
| 204 | return NULL; |
| 205 | |
| 206 | if ((tmp = kmem_alloc(DM_MAX_PARAMS_SIZE, KM_SLEEP)) == NULL) { |
| 207 | kmem_free(params, DM_MAX_PARAMS_SIZE); |
| 208 | return NULL; |
| 209 | } |
| 210 | |
| 211 | snprintf(params, DM_MAX_PARAMS_SIZE, "%d %" PRIu64, |
| 212 | tsc->stripe_num, tsc->stripe_chunksize); |
| 213 | |
| 214 | TAILQ_FOREACH(tlc, &tsc->stripe_devs, entries) { |
| 215 | snprintf(tmp, DM_MAX_PARAMS_SIZE, " %s %" PRIu64, |
| 216 | tlc->pdev->name, tlc->offset); |
| 217 | strcat(params, tmp); |
| 218 | } |
| 219 | |
| 220 | kmem_free(tmp, DM_MAX_PARAMS_SIZE); |
| 221 | |
| 222 | return params; |
| 223 | } |
| 224 | /* Strategy routine called from dm_strategy. */ |
| 225 | int |
| 226 | dm_target_stripe_strategy(dm_table_entry_t * table_en, struct buf * bp) |
| 227 | { |
| 228 | dm_target_linear_config_t *tlc; |
| 229 | dm_target_stripe_config_t *tsc; |
| 230 | struct buf *nestbuf; |
| 231 | uint64_t blkno, blkoff; |
| 232 | uint64_t stripe, stripe_blknr; |
| 233 | uint32_t stripe_off, stripe_rest, num_blks, issue_blks; |
| 234 | int i, stripe_devnr; |
| 235 | |
| 236 | tsc = table_en->target_config; |
| 237 | if (tsc == NULL) |
| 238 | return 0; |
| 239 | |
| 240 | /* printf("Stripe target read function called %" PRIu64 "!!\n", |
| 241 | tlc->offset);*/ |
| 242 | |
| 243 | /* calculate extent of request */ |
| 244 | KASSERT(bp->b_resid % DEV_BSIZE == 0); |
| 245 | |
| 246 | blkno = bp->b_blkno; |
| 247 | blkoff = 0; |
| 248 | num_blks = bp->b_resid / DEV_BSIZE; |
| 249 | for (;;) { |
| 250 | /* blockno to strip piece nr */ |
| 251 | stripe = blkno / tsc->stripe_chunksize; |
| 252 | stripe_off = blkno % tsc->stripe_chunksize; |
| 253 | |
| 254 | /* where we are inside the strip */ |
| 255 | stripe_devnr = stripe % tsc->stripe_num; |
| 256 | stripe_blknr = stripe / tsc->stripe_num; |
| 257 | |
| 258 | /* how much is left before we hit a boundary */ |
| 259 | stripe_rest = tsc->stripe_chunksize - stripe_off; |
| 260 | |
| 261 | /* issue this piece on stripe `stripe' */ |
| 262 | issue_blks = MIN(stripe_rest, num_blks); |
| 263 | nestbuf = getiobuf(NULL, true); |
| 264 | |
| 265 | nestiobuf_setup(bp, nestbuf, blkoff, issue_blks * DEV_BSIZE); |
| 266 | nestbuf->b_blkno = stripe_blknr * tsc->stripe_chunksize + stripe_off; |
| 267 | |
| 268 | tlc = TAILQ_FIRST(&tsc->stripe_devs); |
| 269 | for (i = 0; i < stripe_devnr && tlc != NULL; i++) |
| 270 | tlc = TAILQ_NEXT(tlc, entries); |
| 271 | |
| 272 | /* by this point we should have an tlc */ |
| 273 | KASSERT(tlc != NULL); |
| 274 | |
| 275 | nestbuf->b_blkno += tlc->offset; |
| 276 | |
| 277 | VOP_STRATEGY(tlc->pdev->pdev_vnode, nestbuf); |
| 278 | |
| 279 | blkno += issue_blks; |
| 280 | blkoff += issue_blks * DEV_BSIZE; |
| 281 | num_blks -= issue_blks; |
| 282 | |
| 283 | if (num_blks <= 0) |
| 284 | break; |
| 285 | } |
| 286 | |
| 287 | return 0; |
| 288 | } |
| 289 | /* Sync underlying disk caches. */ |
| 290 | int |
| 291 | dm_target_stripe_sync(dm_table_entry_t * table_en) |
| 292 | { |
| 293 | int cmd, err; |
| 294 | dm_target_stripe_config_t *tsc; |
| 295 | dm_target_linear_config_t *tlc; |
| 296 | |
| 297 | tsc = table_en->target_config; |
| 298 | |
| 299 | err = 0; |
| 300 | cmd = 1; |
| 301 | |
| 302 | TAILQ_FOREACH(tlc, &tsc->stripe_devs, entries) { |
| 303 | if ((err = VOP_IOCTL(tlc->pdev->pdev_vnode, DIOCCACHESYNC, |
| 304 | &cmd, FREAD|FWRITE, kauth_cred_get())) != 0) |
| 305 | return err; |
| 306 | } |
| 307 | |
| 308 | return err; |
| 309 | |
| 310 | } |
| 311 | /* Destroy target specific data. */ |
| 312 | int |
| 313 | dm_target_stripe_destroy(dm_table_entry_t * table_en) |
| 314 | { |
| 315 | dm_target_stripe_fini(table_en->target_config); |
| 316 | |
| 317 | /* Unbusy target so we can unload it */ |
| 318 | dm_target_unbusy(table_en->target); |
| 319 | |
| 320 | table_en->target_config = NULL; |
| 321 | return 0; |
| 322 | } |
| 323 | /* Doesn't not need to do anything here. */ |
| 324 | int |
| 325 | dm_target_stripe_deps(dm_table_entry_t * table_en, prop_array_t prop_array) |
| 326 | { |
| 327 | dm_target_stripe_config_t *tsc; |
| 328 | dm_target_linear_config_t *tlc; |
| 329 | |
| 330 | if (table_en->target_config == NULL) |
| 331 | return ENOENT; |
| 332 | |
| 333 | tsc = table_en->target_config; |
| 334 | |
| 335 | TAILQ_FOREACH(tlc, &tsc->stripe_devs, entries) { |
| 336 | prop_array_add_uint64(prop_array, |
| 337 | (uint64_t) tlc->pdev->pdev_vnode->v_rdev); |
| 338 | } |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | /* Unsupported for this target. */ |
| 343 | int |
| 344 | dm_target_stripe_upcall(dm_table_entry_t * table_en, struct buf * bp) |
| 345 | { |
| 346 | return 0; |
| 347 | } |
| 348 | /* |
| 349 | * Compute physical block size |
| 350 | * For a stripe target we chose the maximum sector size of all |
| 351 | * stripe devices. For the supported power-of-2 sizes this is equivalent |
| 352 | * to the least common multiple. |
| 353 | */ |
| 354 | int |
| 355 | dm_target_stripe_secsize(dm_table_entry_t * table_en, unsigned *secsizep) |
| 356 | { |
| 357 | dm_target_linear_config_t *tlc; |
| 358 | dm_target_stripe_config_t *tsc; |
| 359 | unsigned secsize; |
| 360 | |
| 361 | secsize = 0; |
| 362 | |
| 363 | tsc = table_en->target_config; |
| 364 | if (tsc != NULL) { |
| 365 | TAILQ_FOREACH(tlc, &tsc->stripe_devs, entries) { |
| 366 | if (secsize < tlc->pdev->pdev_secsize) |
| 367 | secsize = tlc->pdev->pdev_secsize; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | *secsizep = secsize; |
| 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | |