1 // SPDX-License-Identifier: GPL-2.0 2 3 /* 4 * Fault injection ublk target. Hack this up however you like for 5 * testing specific behaviors of ublk_drv. Currently is a null target 6 * with a configurable delay before completing each I/O. This delay can 7 * be used to test ublk_drv's handling of I/O outstanding to the ublk 8 * server when it dies. 9 */ 10 11 #include "kublk.h" 12 13 static int ublk_fault_inject_tgt_init(const struct dev_ctx *ctx, 14 struct ublk_dev *dev) 15 { 16 const struct ublksrv_ctrl_dev_info *info = &dev->dev_info; 17 unsigned long dev_size = 250UL << 30; 18 19 if (ctx->auto_zc_fallback) { 20 ublk_err("%s: not support auto_zc_fallback\n", __func__); 21 return -EINVAL; 22 } 23 24 dev->tgt.dev_size = dev_size; 25 dev->tgt.params = (struct ublk_params) { 26 .types = UBLK_PARAM_TYPE_BASIC, 27 .basic = { 28 .logical_bs_shift = 9, 29 .physical_bs_shift = 12, 30 .io_opt_shift = 12, 31 .io_min_shift = 9, 32 .max_sectors = info->max_io_buf_bytes >> 9, 33 .dev_sectors = dev_size >> 9, 34 }, 35 }; 36 ublk_set_integrity_params(ctx, &dev->tgt.params); 37 38 dev->private_data = (void *)(unsigned long)(ctx->fault_inject.delay_us * 1000); 39 return 0; 40 } 41 42 static int ublk_fault_inject_queue_io(struct ublk_thread *t, 43 struct ublk_queue *q, int tag) 44 { 45 const struct ublksrv_io_desc *iod = ublk_get_iod(q, tag); 46 struct io_uring_sqe *sqe; 47 struct __kernel_timespec ts = { 48 .tv_nsec = (long long)q->dev->private_data, 49 }; 50 51 ublk_io_alloc_sqes(t, &sqe, 1); 52 io_uring_prep_timeout(sqe, &ts, 1, 0); 53 sqe->user_data = build_user_data(tag, ublksrv_get_op(iod), 0, q->q_id, 1); 54 55 ublk_queued_tgt_io(t, q, tag, 1); 56 57 return 0; 58 } 59 60 static void ublk_fault_inject_tgt_io_done(struct ublk_thread *t, 61 struct ublk_queue *q, 62 const struct io_uring_cqe *cqe) 63 { 64 unsigned tag = user_data_to_tag(cqe->user_data); 65 const struct ublksrv_io_desc *iod = ublk_get_iod(q, tag); 66 67 if (cqe->res != -ETIME) 68 ublk_err("%s: unexpected cqe res %d\n", __func__, cqe->res); 69 70 if (ublk_completed_tgt_io(t, q, tag)) 71 ublk_complete_io(t, q, tag, iod->nr_sectors << 9); 72 else 73 ublk_err("%s: io not complete after 1 cqe\n", __func__); 74 } 75 76 static void ublk_fault_inject_cmd_line(struct dev_ctx *ctx, int argc, char *argv[]) 77 { 78 static const struct option longopts[] = { 79 { "delay_us", 1, NULL, 0 }, 80 { 0, 0, 0, 0 } 81 }; 82 int option_idx, opt; 83 84 ctx->fault_inject.delay_us = 0; 85 while ((opt = getopt_long(argc, argv, "", 86 longopts, &option_idx)) != -1) { 87 switch (opt) { 88 case 0: 89 if (!strcmp(longopts[option_idx].name, "delay_us")) 90 ctx->fault_inject.delay_us = strtoll(optarg, NULL, 10); 91 } 92 } 93 } 94 95 static void ublk_fault_inject_usage(const struct ublk_tgt_ops *ops) 96 { 97 printf("\tfault_inject: [--delay_us us (default 0)]\n"); 98 } 99 100 const struct ublk_tgt_ops fault_inject_tgt_ops = { 101 .name = "fault_inject", 102 .init_tgt = ublk_fault_inject_tgt_init, 103 .queue_io = ublk_fault_inject_queue_io, 104 .tgt_io_done = ublk_fault_inject_tgt_io_done, 105 .parse_cmd_line = ublk_fault_inject_cmd_line, 106 .usage = ublk_fault_inject_usage, 107 }; 108