1 // SPDX-License-Identifier: GPL-2.0 2 3 #include "kublk.h" 4 5 #define NR_STRIPE MAX_BACK_FILES 6 7 struct stripe_conf { 8 unsigned nr_files; 9 unsigned shift; 10 }; 11 12 struct stripe { 13 loff_t start; 14 unsigned nr_sects; 15 int seq; 16 17 struct iovec *vec; 18 unsigned nr_vec; 19 unsigned cap; 20 }; 21 22 struct stripe_array { 23 struct stripe s[NR_STRIPE]; 24 unsigned nr; 25 struct iovec _vec[]; 26 }; 27 28 static inline const struct stripe_conf *get_chunk_shift(const struct ublk_queue *q) 29 { 30 return (struct stripe_conf *)q->dev->private_data; 31 } 32 33 static inline unsigned calculate_nr_vec(const struct stripe_conf *conf, 34 const struct ublksrv_io_desc *iod) 35 { 36 const unsigned shift = conf->shift - 9; 37 const unsigned unit_sects = conf->nr_files << shift; 38 loff_t start = iod->start_sector; 39 loff_t end = start + iod->nr_sectors; 40 41 return (end / unit_sects) - (start / unit_sects) + 1; 42 } 43 44 static struct stripe_array *alloc_stripe_array(const struct stripe_conf *conf, 45 const struct ublksrv_io_desc *iod) 46 { 47 unsigned nr_vecs = calculate_nr_vec(conf, iod); 48 unsigned total = nr_vecs * conf->nr_files; 49 struct stripe_array *s; 50 int i; 51 52 s = malloc(sizeof(*s) + total * sizeof(struct iovec)); 53 54 s->nr = 0; 55 for (i = 0; i < conf->nr_files; i++) { 56 struct stripe *t = &s->s[i]; 57 58 t->nr_vec = 0; 59 t->vec = &s->_vec[i * nr_vecs]; 60 t->nr_sects = 0; 61 t->cap = nr_vecs; 62 } 63 64 return s; 65 } 66 67 static void free_stripe_array(struct stripe_array *s) 68 { 69 free(s); 70 } 71 72 static void calculate_stripe_array(const struct stripe_conf *conf, 73 const struct ublksrv_io_desc *iod, struct stripe_array *s, void *base) 74 { 75 const unsigned shift = conf->shift - 9; 76 const unsigned chunk_sects = 1 << shift; 77 const unsigned unit_sects = conf->nr_files << shift; 78 off64_t start = iod->start_sector; 79 off64_t end = start + iod->nr_sectors; 80 unsigned long done = 0; 81 unsigned idx = 0; 82 83 while (start < end) { 84 unsigned nr_sects = chunk_sects - (start & (chunk_sects - 1)); 85 loff_t unit_off = (start / unit_sects) * unit_sects; 86 unsigned seq = (start - unit_off) >> shift; 87 struct stripe *this = &s->s[idx]; 88 loff_t stripe_off = (unit_off / conf->nr_files) + 89 (start & (chunk_sects - 1)); 90 91 if (nr_sects > end - start) 92 nr_sects = end - start; 93 if (this->nr_sects == 0) { 94 this->nr_sects = nr_sects; 95 this->start = stripe_off; 96 this->seq = seq; 97 s->nr += 1; 98 } else { 99 assert(seq == this->seq); 100 assert(this->start + this->nr_sects == stripe_off); 101 this->nr_sects += nr_sects; 102 } 103 104 assert(this->nr_vec < this->cap); 105 this->vec[this->nr_vec].iov_base = (void *)(base + done); 106 this->vec[this->nr_vec++].iov_len = nr_sects << 9; 107 108 start += nr_sects; 109 done += nr_sects << 9; 110 idx = (idx + 1) % conf->nr_files; 111 } 112 } 113 114 static inline enum io_uring_op stripe_to_uring_op( 115 const struct ublksrv_io_desc *iod, int zc) 116 { 117 unsigned ublk_op = ublksrv_get_op(iod); 118 119 if (ublk_op == UBLK_IO_OP_READ) 120 return zc ? IORING_OP_READV_FIXED : IORING_OP_READV; 121 else if (ublk_op == UBLK_IO_OP_WRITE) 122 return zc ? IORING_OP_WRITEV_FIXED : IORING_OP_WRITEV; 123 assert(0); 124 } 125 126 static int stripe_queue_tgt_rw_io(struct ublk_queue *q, const struct ublksrv_io_desc *iod, int tag) 127 { 128 const struct stripe_conf *conf = get_chunk_shift(q); 129 unsigned auto_zc = (ublk_queue_use_auto_zc(q) != 0); 130 unsigned zc = (ublk_queue_use_zc(q) != 0); 131 enum io_uring_op op = stripe_to_uring_op(iod, zc | auto_zc); 132 struct io_uring_sqe *sqe[NR_STRIPE]; 133 struct stripe_array *s = alloc_stripe_array(conf, iod); 134 struct ublk_io *io = ublk_get_io(q, tag); 135 int i, extra = zc ? 2 : 0; 136 void *base = (zc | auto_zc) ? NULL : (void *)iod->addr; 137 138 io->private_data = s; 139 calculate_stripe_array(conf, iod, s, base); 140 141 ublk_queue_alloc_sqes(q, sqe, s->nr + extra); 142 143 if (zc) { 144 io_uring_prep_buf_register(sqe[0], 0, tag, q->q_id, tag); 145 sqe[0]->flags |= IOSQE_CQE_SKIP_SUCCESS | IOSQE_IO_HARDLINK; 146 sqe[0]->user_data = build_user_data(tag, 147 ublk_cmd_op_nr(sqe[0]->cmd_op), 0, 1); 148 } 149 150 for (i = zc; i < s->nr + extra - zc; i++) { 151 struct stripe *t = &s->s[i - zc]; 152 153 io_uring_prep_rw(op, sqe[i], 154 t->seq + 1, 155 (void *)t->vec, 156 t->nr_vec, 157 t->start << 9); 158 io_uring_sqe_set_flags(sqe[i], IOSQE_FIXED_FILE); 159 if (auto_zc || zc) { 160 sqe[i]->buf_index = tag; 161 if (zc) 162 sqe[i]->flags |= IOSQE_IO_HARDLINK; 163 } 164 /* bit63 marks us as tgt io */ 165 sqe[i]->user_data = build_user_data(tag, ublksrv_get_op(iod), i - zc, 1); 166 } 167 if (zc) { 168 struct io_uring_sqe *unreg = sqe[s->nr + 1]; 169 170 io_uring_prep_buf_unregister(unreg, 0, tag, q->q_id, tag); 171 unreg->user_data = build_user_data(tag, ublk_cmd_op_nr(unreg->cmd_op), 0, 1); 172 } 173 174 /* register buffer is skip_success */ 175 return s->nr + zc; 176 } 177 178 static int handle_flush(struct ublk_queue *q, const struct ublksrv_io_desc *iod, int tag) 179 { 180 const struct stripe_conf *conf = get_chunk_shift(q); 181 struct io_uring_sqe *sqe[NR_STRIPE]; 182 int i; 183 184 ublk_queue_alloc_sqes(q, sqe, conf->nr_files); 185 for (i = 0; i < conf->nr_files; i++) { 186 io_uring_prep_fsync(sqe[i], i + 1, IORING_FSYNC_DATASYNC); 187 io_uring_sqe_set_flags(sqe[i], IOSQE_FIXED_FILE); 188 sqe[i]->user_data = build_user_data(tag, UBLK_IO_OP_FLUSH, 0, 1); 189 } 190 return conf->nr_files; 191 } 192 193 static int stripe_queue_tgt_io(struct ublk_queue *q, int tag) 194 { 195 const struct ublksrv_io_desc *iod = ublk_get_iod(q, tag); 196 unsigned ublk_op = ublksrv_get_op(iod); 197 int ret = 0; 198 199 switch (ublk_op) { 200 case UBLK_IO_OP_FLUSH: 201 ret = handle_flush(q, iod, tag); 202 break; 203 case UBLK_IO_OP_WRITE_ZEROES: 204 case UBLK_IO_OP_DISCARD: 205 ret = -ENOTSUP; 206 break; 207 case UBLK_IO_OP_READ: 208 case UBLK_IO_OP_WRITE: 209 ret = stripe_queue_tgt_rw_io(q, iod, tag); 210 break; 211 default: 212 ret = -EINVAL; 213 break; 214 } 215 ublk_dbg(UBLK_DBG_IO, "%s: tag %d ublk io %x %llx %u ret %d\n", __func__, tag, 216 iod->op_flags, iod->start_sector, iod->nr_sectors << 9, ret); 217 return ret; 218 } 219 220 static int ublk_stripe_queue_io(struct ublk_queue *q, int tag) 221 { 222 int queued = stripe_queue_tgt_io(q, tag); 223 224 ublk_queued_tgt_io(q, tag, queued); 225 return 0; 226 } 227 228 static void ublk_stripe_io_done(struct ublk_queue *q, int tag, 229 const struct io_uring_cqe *cqe) 230 { 231 const struct ublksrv_io_desc *iod = ublk_get_iod(q, tag); 232 unsigned op = user_data_to_op(cqe->user_data); 233 struct ublk_io *io = ublk_get_io(q, tag); 234 int res = cqe->res; 235 236 if (res < 0 || op != ublk_cmd_op_nr(UBLK_U_IO_UNREGISTER_IO_BUF)) { 237 if (!io->result) 238 io->result = res; 239 if (res < 0) 240 ublk_err("%s: io failure %d tag %u\n", __func__, res, tag); 241 } 242 243 /* buffer register op is IOSQE_CQE_SKIP_SUCCESS */ 244 if (op == ublk_cmd_op_nr(UBLK_U_IO_REGISTER_IO_BUF)) 245 io->tgt_ios += 1; 246 247 /* fail short READ/WRITE simply */ 248 if (op == UBLK_IO_OP_READ || op == UBLK_IO_OP_WRITE) { 249 unsigned seq = user_data_to_tgt_data(cqe->user_data); 250 struct stripe_array *s = io->private_data; 251 252 if (res < s->s[seq].nr_sects << 9) { 253 io->result = -EIO; 254 ublk_err("%s: short rw op %u res %d exp %u tag %u\n", 255 __func__, op, res, s->s[seq].vec->iov_len, tag); 256 } 257 } 258 259 if (ublk_completed_tgt_io(q, tag)) { 260 int res = io->result; 261 262 if (!res) 263 res = iod->nr_sectors << 9; 264 265 ublk_complete_io(q, tag, res); 266 267 free_stripe_array(io->private_data); 268 io->private_data = NULL; 269 } 270 } 271 272 static int ublk_stripe_tgt_init(const struct dev_ctx *ctx, struct ublk_dev *dev) 273 { 274 struct ublk_params p = { 275 .types = UBLK_PARAM_TYPE_BASIC, 276 .basic = { 277 .attrs = UBLK_ATTR_VOLATILE_CACHE, 278 .logical_bs_shift = 9, 279 .physical_bs_shift = 12, 280 .io_opt_shift = 12, 281 .io_min_shift = 9, 282 .max_sectors = dev->dev_info.max_io_buf_bytes >> 9, 283 }, 284 }; 285 unsigned chunk_size = ctx->stripe.chunk_size; 286 struct stripe_conf *conf; 287 unsigned chunk_shift; 288 loff_t bytes = 0; 289 int ret, i, mul = 1; 290 291 if (ctx->auto_zc_fallback) { 292 ublk_err("%s: not support auto_zc_fallback\n", __func__); 293 return -EINVAL; 294 } 295 296 if ((chunk_size & (chunk_size - 1)) || !chunk_size) { 297 ublk_err("invalid chunk size %u\n", chunk_size); 298 return -EINVAL; 299 } 300 301 if (chunk_size < 4096 || chunk_size > 512 * 1024) { 302 ublk_err("invalid chunk size %u\n", chunk_size); 303 return -EINVAL; 304 } 305 306 chunk_shift = ilog2(chunk_size); 307 308 ret = backing_file_tgt_init(dev); 309 if (ret) 310 return ret; 311 312 if (!dev->tgt.nr_backing_files || dev->tgt.nr_backing_files > NR_STRIPE) 313 return -EINVAL; 314 315 assert(dev->nr_fds == dev->tgt.nr_backing_files + 1); 316 317 for (i = 0; i < dev->tgt.nr_backing_files; i++) 318 dev->tgt.backing_file_size[i] &= ~((1 << chunk_shift) - 1); 319 320 for (i = 0; i < dev->tgt.nr_backing_files; i++) { 321 unsigned long size = dev->tgt.backing_file_size[i]; 322 323 if (size != dev->tgt.backing_file_size[0]) 324 return -EINVAL; 325 bytes += size; 326 } 327 328 conf = malloc(sizeof(*conf)); 329 conf->shift = chunk_shift; 330 conf->nr_files = dev->tgt.nr_backing_files; 331 332 dev->private_data = conf; 333 dev->tgt.dev_size = bytes; 334 p.basic.dev_sectors = bytes >> 9; 335 dev->tgt.params = p; 336 337 if (dev->dev_info.flags & UBLK_F_SUPPORT_ZERO_COPY) 338 mul = 2; 339 dev->tgt.sq_depth = mul * dev->dev_info.queue_depth * conf->nr_files; 340 dev->tgt.cq_depth = mul * dev->dev_info.queue_depth * conf->nr_files; 341 342 printf("%s: shift %u files %u\n", __func__, conf->shift, conf->nr_files); 343 344 return 0; 345 } 346 347 static void ublk_stripe_tgt_deinit(struct ublk_dev *dev) 348 { 349 free(dev->private_data); 350 backing_file_tgt_deinit(dev); 351 } 352 353 static void ublk_stripe_cmd_line(struct dev_ctx *ctx, int argc, char *argv[]) 354 { 355 static const struct option longopts[] = { 356 { "chunk_size", 1, NULL, 0 }, 357 { 0, 0, 0, 0 } 358 }; 359 int option_idx, opt; 360 361 ctx->stripe.chunk_size = 65536; 362 while ((opt = getopt_long(argc, argv, "", 363 longopts, &option_idx)) != -1) { 364 switch (opt) { 365 case 0: 366 if (!strcmp(longopts[option_idx].name, "chunk_size")) 367 ctx->stripe.chunk_size = strtol(optarg, NULL, 10); 368 } 369 } 370 } 371 372 static void ublk_stripe_usage(const struct ublk_tgt_ops *ops) 373 { 374 printf("\tstripe: [--chunk_size chunk_size (default 65536)]\n"); 375 } 376 377 const struct ublk_tgt_ops stripe_tgt_ops = { 378 .name = "stripe", 379 .init_tgt = ublk_stripe_tgt_init, 380 .deinit_tgt = ublk_stripe_tgt_deinit, 381 .queue_io = ublk_stripe_queue_io, 382 .tgt_io_done = ublk_stripe_io_done, 383 .parse_cmd_line = ublk_stripe_cmd_line, 384 .usage = ublk_stripe_usage, 385 }; 386