1 /*$NetBSD: dm_target_stripe.c,v 1.5 2009/04/06 22:48:26 haad 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 42 #include "dm.h" 43 44 #ifdef DM_TARGET_MODULE 45 /* 46 * Every target can be compiled directly to dm driver or as a 47 * separate module this part of target is used for loading targets 48 * to dm driver. 49 * Target can be unloaded from kernel only if there are no users of 50 * it e.g. there are no devices which uses that target. 51 */ 52 #include <sys/kernel.h> 53 #include <sys/module.h> 54 55 MODULE(MODULE_CLASS_MISC, dm_target_stripe, NULL); 56 57 static int 58 dm_target_stripe_modcmd(modcmd_t cmd, void *arg) 59 { 60 dm_target_t *dmt; 61 int r; 62 dmt = NULL; 63 64 switch (cmd) { 65 case MODULE_CMD_INIT: 66 if ((dmt = dm_target_lookup("stripe")) != NULL) { 67 dm_target_unbusy(dmt); 68 return EEXIST; 69 } 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->deps = &dm_target_stripe_deps; 81 dmt->destroy = &dm_target_stripe_destroy; 82 dmt->upcall = &dm_target_stripe_upcall; 83 84 r = dm_target_insert(dmt); 85 86 break; 87 88 case MODULE_CMD_FINI: 89 r = dm_target_rem("stripe"); 90 break; 91 92 case MODULE_CMD_STAT: 93 return ENOTTY; 94 95 default: 96 return ENOTTY; 97 } 98 99 return r; 100 } 101 102 #endif 103 104 /* 105 * Init function called from dm_table_load_ioctl. 106 * Example line sent to dm from lvm tools when using striped target. 107 * start length striped #stripes chunk_size device1 offset1 ... deviceN offsetN 108 * 0 65536 striped 2 512 /dev/hda 0 /dev/hdb 0 109 */ 110 int 111 dm_target_stripe_init(dm_dev_t *dmv, void **target_config, char *params) 112 { 113 dm_target_stripe_config_t *tsc; 114 size_t len; 115 char **ap, *argv[10]; 116 117 if(params == NULL) 118 return EINVAL; 119 120 len = strlen(params) + 1; 121 122 /* 123 * Parse a string, containing tokens delimited by white space, 124 * into an argument vector 125 */ 126 for (ap = argv; ap < &argv[9] && 127 (*ap = strsep(¶ms, " \t")) != NULL;) { 128 if (**ap != '\0') 129 ap++; 130 } 131 132 printf("Stripe target init function called!!\n"); 133 134 printf("Stripe target chunk size %s number of stripes %s\n", argv[1], argv[0]); 135 printf("Stripe target device name %s -- offset %s\n", argv[2], argv[3]); 136 printf("Stripe target device name %s -- offset %s\n", argv[4], argv[5]); 137 138 if (atoi(argv[0]) > MAX_STRIPES) 139 return ENOTSUP; 140 141 if ((tsc = kmem_alloc(sizeof(dm_target_stripe_config_t), KM_NOSLEEP)) 142 == NULL) 143 return ENOMEM; 144 145 /* Insert dmp to global pdev list */ 146 if ((tsc->stripe_devs[0].pdev = dm_pdev_insert(argv[2])) == NULL) 147 return ENOENT; 148 149 /* Insert dmp to global pdev list */ 150 if ((tsc->stripe_devs[1].pdev = dm_pdev_insert(argv[4])) == NULL) 151 return ENOENT; 152 153 tsc->stripe_devs[0].offset = atoi(argv[3]); 154 tsc->stripe_devs[1].offset = atoi(argv[5]); 155 156 /* Save length of param string */ 157 tsc->params_len = len; 158 tsc->stripe_chunksize = atoi(argv[1]); 159 tsc->stripe_num = (uint8_t)atoi(argv[0]); 160 161 *target_config = tsc; 162 163 dmv->dev_type = DM_STRIPE_DEV; 164 165 return 0; 166 } 167 168 /* Status routine called to get params string. */ 169 char * 170 dm_target_stripe_status(void *target_config) 171 { 172 dm_target_stripe_config_t *tsc; 173 char *params; 174 175 tsc = target_config; 176 177 if ((params = kmem_alloc(tsc->params_len, KM_NOSLEEP)) == NULL) 178 return NULL; 179 180 snprintf(params, tsc->params_len, "%d %"PRIu64" %s %"PRIu64" %s %"PRIu64, 181 tsc->stripe_num, tsc->stripe_chunksize, 182 tsc->stripe_devs[0].pdev->name, tsc->stripe_devs[0].offset, 183 tsc->stripe_devs[1].pdev->name, tsc->stripe_devs[1].offset); 184 185 return params; 186 } 187 188 /* Strategy routine called from dm_strategy. */ 189 int 190 dm_target_stripe_strategy(dm_table_entry_t *table_en, struct buf *bp) 191 { 192 dm_target_stripe_config_t *tsc; 193 struct buf *nestbuf; 194 uint64_t blkno, blkoff; 195 uint64_t stripe, stripe_blknr; 196 uint32_t stripe_off, stripe_rest, num_blks, issue_blks; 197 int stripe_devnr; 198 199 tsc = table_en->target_config; 200 if (tsc == NULL) 201 return 0; 202 203 /* printf("Stripe target read function called %" PRIu64 "!!\n", 204 tlc->offset);*/ 205 206 /* calculate extent of request */ 207 KASSERT(bp->b_resid % DEV_BSIZE == 0); 208 209 blkno = bp->b_blkno; 210 blkoff = 0; 211 num_blks = bp->b_resid / DEV_BSIZE; 212 for (;;) { 213 /* blockno to strip piece nr */ 214 stripe = blkno / tsc->stripe_chunksize; 215 stripe_off = blkno % tsc->stripe_chunksize; 216 217 /* where we are inside the strip */ 218 stripe_devnr = stripe % tsc->stripe_num; 219 stripe_blknr = stripe / tsc->stripe_num; 220 221 /* how much is left before we hit a boundary */ 222 stripe_rest = tsc->stripe_chunksize - stripe_off; 223 224 /* issue this piece on stripe `stripe' */ 225 issue_blks = MIN(stripe_rest, num_blks); 226 nestbuf = getiobuf(NULL, true); 227 228 nestiobuf_setup(bp, nestbuf, blkoff, issue_blks * DEV_BSIZE); 229 nestbuf->b_blkno = stripe_blknr * tsc->stripe_chunksize + stripe_off; 230 nestbuf->b_blkno += tsc->stripe_devs[stripe_devnr].offset; 231 232 VOP_STRATEGY(tsc->stripe_devs[stripe_devnr].pdev->pdev_vnode, nestbuf); 233 234 blkno += issue_blks; 235 blkoff += issue_blks * DEV_BSIZE; 236 num_blks -= issue_blks; 237 238 if (num_blks <= 0) 239 break; 240 } 241 242 return 0; 243 } 244 245 /* Doesn't do anything here. */ 246 int 247 dm_target_stripe_destroy(dm_table_entry_t *table_en) 248 { 249 dm_target_stripe_config_t *tsc; 250 251 tsc = table_en->target_config; 252 253 if (tsc == NULL) 254 return 0; 255 256 dm_pdev_decr(tsc->stripe_devs[0].pdev); 257 dm_pdev_decr(tsc->stripe_devs[1].pdev); 258 259 /* Unbusy target so we can unload it */ 260 dm_target_unbusy(table_en->target); 261 262 kmem_free(tsc, sizeof(dm_target_stripe_config_t)); 263 264 table_en->target_config = NULL; 265 266 return 0; 267 } 268 269 /* Doesn't not need to do anything here. */ 270 int 271 dm_target_stripe_deps(dm_table_entry_t *table_en, prop_array_t prop_array) 272 { 273 dm_target_stripe_config_t *tsc; 274 struct vattr va; 275 276 int error; 277 278 if (table_en->target_config == NULL) 279 return ENOENT; 280 281 tsc = table_en->target_config; 282 283 if ((error = VOP_GETATTR(tsc->stripe_devs[0].pdev->pdev_vnode, &va, curlwp->l_cred)) != 0) 284 return error; 285 286 prop_array_add_uint64(prop_array, (uint64_t)va.va_rdev); 287 288 if ((error = VOP_GETATTR(tsc->stripe_devs[1].pdev->pdev_vnode, &va, curlwp->l_cred)) != 0) 289 return error; 290 291 prop_array_add_uint64(prop_array, (uint64_t)va.va_rdev); 292 293 return 0; 294 } 295 296 /* Unsupported for this target. */ 297 int 298 dm_target_stripe_upcall(dm_table_entry_t *table_en, struct buf *bp) 299 { 300 return 0; 301 } 302