sys/dev/disk/dm/flakey/dm_target_flakey.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 | /* * Copyright (c) 2015 The DragonFly Project. All rights reserved. * * This code is derived from software contributed to The DragonFly Project * by Tomohiro Kusumi <tkusumi@netbsd.org> * * 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. * 3. Neither the name of The DragonFly Project nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific, prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 * COPYRIGHT HOLDERS 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. */ #include <sys/param.h> #include <sys/malloc.h> #include <dev/disk/dm/dm.h> MALLOC_DEFINE(M_DMFLAKEY, "dm_flakey", "Device Mapper Target Flakey"); /* dm_flakey never updates any field after initialization */ typedef struct target_flakey_config { dm_pdev_t *pdev; uint64_t offset; int up_int; int down_int; int offset_time; /* drop_writes feature */ int drop_writes; /* corrupt_bio_byte feature */ unsigned int corrupt_buf_byte; unsigned int corrupt_buf_rw; unsigned int corrupt_buf_value; unsigned int corrupt_buf_flags; /* for B_XXX flags */ } dm_target_flakey_config_t; #define FLAKEY_CORRUPT_DIR(tfc) \ ((tfc)->corrupt_buf_rw == BUF_CMD_READ ? 'r' : 'w') static int _init_features(dm_target_flakey_config_t*, int, char**); static __inline void _submit(dm_target_flakey_config_t*, struct bio*); static int _flakey_read(dm_target_flakey_config_t*, struct buf*); static int _flakey_write(dm_target_flakey_config_t*, struct buf*); static int _flakey_corrupt_buf(dm_target_flakey_config_t*, struct bio*); static int dm_target_flakey_init(dm_table_entry_t *table_en, int argc, char **argv) { dm_target_flakey_config_t *tfc; dm_pdev_t *dmp; int err; dmdebug("Flakey target init: argc=%d\n", argc); if (argc < 4) { kprintf("Flakey target takes 4 or more args\n"); return EINVAL; } tfc = kmalloc(sizeof(*tfc), M_DMFLAKEY, M_WAITOK | M_ZERO); if (tfc == NULL) return ENOMEM; if ((dmp = dm_pdev_insert(argv[0])) == NULL) { err = ENOENT; goto fail; } tfc->pdev = dmp; tfc->offset = atoi64(argv[1]); tfc->up_int = atoi64(argv[2]); tfc->down_int = atoi64(argv[3]); tfc->offset_time = ticks; if ((tfc->up_int + tfc->down_int) == 0) { kprintf("Sum of up/down interval is 0\n"); err = EINVAL; goto fail; } if (tfc->up_int + tfc->down_int < tfc->up_int) { kprintf("Interval time overflow\n"); err = EINVAL; goto fail; } err = _init_features(tfc, argc - 4, argv + 4); if (err) goto fail; dm_table_add_deps(table_en, dmp); dm_table_init_target(table_en, tfc); return 0; fail: kfree(tfc, M_DMFLAKEY); return err; } static int _init_features(dm_target_flakey_config_t *tfc, int argc, char **argv) { char *arg; unsigned int value; if (argc == 0) return 0; argc = atoi64(*argv++); /* # of args for features */ if (argc > 6) { kprintf("Invalid # of feature args %d\n", argc); return EINVAL; } while (argc) { argc--; arg = *argv++; /* drop_writes */ if (strcmp(arg, "drop_writes") == 0) { tfc->drop_writes = 1; continue; } /* corrupt_bio_byte <Nth_byte> <direction> <value> <flags> */ if (strcmp(arg, "corrupt_bio_byte") == 0) { if (argc < 4) { kprintf("Invalid # of feature args %d for " "corrupt_bio_byte\n", argc); return EINVAL; } /* <Nth_byte> */ argc--; value = atoi64(*argv++); if (value < 1) { kprintf("Invalid corrupt_bio_byte " "<Nth_byte> arg %u\n", value); return EINVAL; } tfc->corrupt_buf_byte = value; /* <direction> */ argc--; arg = *argv++; if (strcmp(arg, "r") == 0) { tfc->corrupt_buf_rw = BUF_CMD_READ; } else if (strcmp(arg, "w") == 0) { tfc->corrupt_buf_rw = BUF_CMD_WRITE; } else { kprintf("Invalid corrupt_bio_byte " "<direction> arg %s\n", arg); return EINVAL; } /* <value> */ argc--; value = atoi64(*argv++); if (value > 0xff) { kprintf("Invalid corrupt_bio_byte " "<value> arg %u\n", value); return EINVAL; } tfc->corrupt_buf_value = value; /* <flags> */ argc--; tfc->corrupt_buf_flags = atoi64(*argv++); continue; } kprintf("Unknown Flakey target feature %s\n", arg); return EINVAL; } if (tfc->drop_writes && (tfc->corrupt_buf_rw == BUF_CMD_WRITE)) { kprintf("Flakey target doesn't allow drop_writes feature " "and corrupt_bio_byte feature with 'w' set\n"); return EINVAL; } return 0; } static int dm_target_flakey_destroy(dm_table_entry_t *table_en) { dm_target_flakey_config_t *tfc; tfc = table_en->target_config; if (tfc == NULL) return 0; dm_pdev_decr(tfc->pdev); kfree(tfc, M_DMFLAKEY); return 0; } static int dm_target_flakey_strategy(dm_table_entry_t *table_en, struct buf *bp) { dm_target_flakey_config_t *tfc; int elapsed; tfc = table_en->target_config; elapsed = (ticks - tfc->offset_time) / hz; if (elapsed % (tfc->up_int + tfc->down_int) >= tfc->up_int) { switch (bp->b_cmd) { case BUF_CMD_READ: return _flakey_read(tfc, bp); case BUF_CMD_WRITE: return _flakey_write(tfc, bp); default: break; } } /* This is what linear target does */ _submit(tfc, &bp->b_bio1); return 0; } static __inline void _submit(dm_target_flakey_config_t *tfc, struct bio *bio) { bio->bio_offset += tfc->offset * DEV_BSIZE; vn_strategy(tfc->pdev->pdev_vnode, bio); } static __inline void _flakey_eio_buf(struct buf *bp) { bp->b_error = EIO; bp->b_resid = 0; } static void _flakey_read_iodone(struct bio *bio) { struct bio *obio; dm_target_flakey_config_t *tfc; tfc = bio->bio_caller_info1.ptr; obio = pop_bio(bio); /* * Linux dm-flakey has changed its read behavior in 2016. * This conditional is to sync with that change. */ if (tfc->corrupt_buf_byte && tfc->corrupt_buf_rw == BUF_CMD_READ) _flakey_corrupt_buf(tfc, obio); else if (!tfc->drop_writes) _flakey_eio_buf(bio->bio_buf); biodone(obio); } static int _flakey_read(dm_target_flakey_config_t *tfc, struct buf *bp) { struct bio *bio = &bp->b_bio1; struct bio *nbio; /* * Linux dm-flakey has changed its read behavior in 2016. * This conditional is to sync with that change. */ if (!tfc->corrupt_buf_byte && !tfc->drop_writes) { _flakey_eio_buf(bp); biodone(bio); return 0; } nbio = push_bio(bio); nbio->bio_done = _flakey_read_iodone; nbio->bio_caller_info1.ptr = tfc; nbio->bio_offset = pop_bio(nbio)->bio_offset; _submit(tfc, nbio); return 0; } static int _flakey_write(dm_target_flakey_config_t *tfc, struct buf *bp) { struct bio *bio = &bp->b_bio1; if (tfc->drop_writes) { dmdebug("bio=%p drop_writes offset=%ju\n", bio, bio->bio_offset); biodone(bio); return 0; } if (tfc->corrupt_buf_byte && tfc->corrupt_buf_rw == BUF_CMD_WRITE) { _flakey_corrupt_buf(tfc, bio); _submit(tfc, bio); return 0; } /* Error all I/Os if neither of the above two */ _flakey_eio_buf(bp); biodone(bio); return 0; } static int _flakey_corrupt_buf(dm_target_flakey_config_t *tfc, struct bio *bio) { struct buf *bp; bp = bio->bio_buf; if (bp->b_data == NULL) return 1; if (bp->b_error) return 1; /* Don't corrupt on error */ if (bp->b_bcount < tfc->corrupt_buf_byte) return 1; if ((bp->b_flags & tfc->corrupt_buf_flags) != tfc->corrupt_buf_flags) return 1; bp->b_data[tfc->corrupt_buf_byte - 1] = tfc->corrupt_buf_value; dmdebug("bio=%p dir=%c offset=%ju Nth=%u value=%u\n", bio, FLAKEY_CORRUPT_DIR(tfc), bio->bio_offset, tfc->corrupt_buf_byte, tfc->corrupt_buf_value); return 0; } static char * dm_target_flakey_table(void *target_config) { dm_target_flakey_config_t *tfc; char *params, *p; int drop_writes; tfc = target_config; KKASSERT(tfc != NULL); drop_writes = tfc->drop_writes; params = dm_alloc_string(DM_MAX_PARAMS_SIZE); p = params; p += ksnprintf(p, DM_MAX_PARAMS_SIZE, "%s %d %d %d %u ", tfc->pdev->udev_name, tfc->offset_time, tfc->up_int, tfc->down_int, drop_writes + (tfc->corrupt_buf_byte > 0) * 5); if (drop_writes) p += ksnprintf(p, DM_MAX_PARAMS_SIZE, "drop_writes "); if (tfc->corrupt_buf_byte) p += ksnprintf(p, DM_MAX_PARAMS_SIZE, "corrupt_bio_byte %u %c %u %u ", tfc->corrupt_buf_byte, FLAKEY_CORRUPT_DIR(tfc), tfc->corrupt_buf_value, tfc->corrupt_buf_flags); *(--p) = '\0'; return params; } static int dmtf_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("flakey")) != NULL) { dm_target_unbusy(dmt); return EEXIST; } dmt = dm_target_alloc("flakey"); dmt->version[0] = 1; dmt->version[1] = 0; dmt->version[2] = 0; strlcpy(dmt->name, "flakey", DM_MAX_TYPE_NAME); dmt->init = &dm_target_flakey_init; dmt->destroy = &dm_target_flakey_destroy; dmt->strategy = &dm_target_flakey_strategy; dmt->table = &dm_target_flakey_table; err = dm_target_insert(dmt); if (err == 0) kprintf("dm_target_flakey: Successfully initialized\n"); break; case MOD_UNLOAD: err = dm_target_remove("flakey"); if (err == 0) kprintf("dm_target_flakey: unloaded\n"); break; } return err; } DM_TARGET_MODULE(dm_target_flakey, dmtf_mod_handler); |