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_io_alloc_sqes(ublk_get_io(q, tag), sqe, s->nr + extra); 142 143 if (zc) { 144 io_uring_prep_buf_register(sqe[0], 0, tag, q->q_id, io->buf_index); 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, q->q_id, 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, q->q_id, 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, io->buf_index); 171 unreg->user_data = build_user_data( 172 tag, ublk_cmd_op_nr(unreg->cmd_op), 0, q->q_id, 1); 173 } 174 175 /* register buffer is skip_success */ 176 return s->nr + zc; 177 } 178 179 static int handle_flush(struct ublk_queue *q, const struct ublksrv_io_desc *iod, int tag) 180 { 181 const struct stripe_conf *conf = get_chunk_shift(q); 182 struct io_uring_sqe *sqe[NR_STRIPE]; 183 int i; 184 185 ublk_io_alloc_sqes(ublk_get_io(q, tag), sqe, conf->nr_files); 186 for (i = 0; i < conf->nr_files; i++) { 187 io_uring_prep_fsync(sqe[i], i + 1, IORING_FSYNC_DATASYNC); 188 io_uring_sqe_set_flags(sqe[i], IOSQE_FIXED_FILE); 189 sqe[i]->user_data = build_user_data(tag, UBLK_IO_OP_FLUSH, 0, q->q_id, 1); 190 } 191 return conf->nr_files; 192 } 193 194 static int stripe_queue_tgt_io(struct ublk_queue *q, int tag) 195 { 196 const struct ublksrv_io_desc *iod = ublk_get_iod(q, tag); 197 unsigned ublk_op = ublksrv_get_op(iod); 198 int ret = 0; 199 200 switch (ublk_op) { 201 case UBLK_IO_OP_FLUSH: 202 ret = handle_flush(q, iod, tag); 203 break; 204 case UBLK_IO_OP_WRITE_ZEROES: 205 case UBLK_IO_OP_DISCARD: 206 ret = -ENOTSUP; 207 break; 208 case UBLK_IO_OP_READ: 209 case UBLK_IO_OP_WRITE: 210 ret = stripe_queue_tgt_rw_io(q, iod, tag); 211 break; 212 default: 213 ret = -EINVAL; 214 break; 215 } 216 ublk_dbg(UBLK_DBG_IO, "%s: tag %d ublk io %x %llx %u ret %d\n", __func__, tag, 217 iod->op_flags, iod->start_sector, iod->nr_sectors << 9, ret); 218 return ret; 219 } 220 221 static int ublk_stripe_queue_io(struct ublk_queue *q, int tag) 222 { 223 int queued = stripe_queue_tgt_io(q, tag); 224 225 ublk_queued_tgt_io(q, tag, queued); 226 return 0; 227 } 228 229 static void ublk_stripe_io_done(struct ublk_queue *q, int tag, 230 const struct io_uring_cqe *cqe) 231 { 232 const struct ublksrv_io_desc *iod = ublk_get_iod(q, tag); 233 unsigned op = user_data_to_op(cqe->user_data); 234 struct ublk_io *io = ublk_get_io(q, tag); 235 int res = cqe->res; 236 237 if (res < 0 || op != ublk_cmd_op_nr(UBLK_U_IO_UNREGISTER_IO_BUF)) { 238 if (!io->result) 239 io->result = res; 240 if (res < 0) 241 ublk_err("%s: io failure %d tag %u\n", __func__, res, tag); 242 } 243 244 /* buffer register op is IOSQE_CQE_SKIP_SUCCESS */ 245 if (op == ublk_cmd_op_nr(UBLK_U_IO_REGISTER_IO_BUF)) 246 io->tgt_ios += 1; 247 248 /* fail short READ/WRITE simply */ 249 if (op == UBLK_IO_OP_READ || op == UBLK_IO_OP_WRITE) { 250 unsigned seq = user_data_to_tgt_data(cqe->user_data); 251 struct stripe_array *s = io->private_data; 252 253 if (res < s->s[seq].nr_sects << 9) { 254 io->result = -EIO; 255 ublk_err("%s: short rw op %u res %d exp %u tag %u\n", 256 __func__, op, res, s->s[seq].vec->iov_len, tag); 257 } 258 } 259 260 if (ublk_completed_tgt_io(q, tag)) { 261 int res = io->result; 262 263 if (!res) 264 res = iod->nr_sectors << 9; 265 266 ublk_complete_io(q, tag, res); 267 268 free_stripe_array(io->private_data); 269 io->private_data = NULL; 270 } 271 } 272 273 static int ublk_stripe_tgt_init(const struct dev_ctx *ctx, struct ublk_dev *dev) 274 { 275 struct ublk_params p = { 276 .types = UBLK_PARAM_TYPE_BASIC, 277 .basic = { 278 .attrs = UBLK_ATTR_VOLATILE_CACHE, 279 .logical_bs_shift = 9, 280 .physical_bs_shift = 12, 281 .io_opt_shift = 12, 282 .io_min_shift = 9, 283 .max_sectors = dev->dev_info.max_io_buf_bytes >> 9, 284 }, 285 }; 286 unsigned chunk_size = ctx->stripe.chunk_size; 287 struct stripe_conf *conf; 288 unsigned chunk_shift; 289 loff_t bytes = 0; 290 int ret, i, mul = 1; 291 292 if (ctx->auto_zc_fallback) { 293 ublk_err("%s: not support auto_zc_fallback\n", __func__); 294 return -EINVAL; 295 } 296 297 if ((chunk_size & (chunk_size - 1)) || !chunk_size) { 298 ublk_err("invalid chunk size %u\n", chunk_size); 299 return -EINVAL; 300 } 301 302 if (chunk_size < 4096 || chunk_size > 512 * 1024) { 303 ublk_err("invalid chunk size %u\n", chunk_size); 304 return -EINVAL; 305 } 306 307 chunk_shift = ilog2(chunk_size); 308 309 ret = backing_file_tgt_init(dev); 310 if (ret) 311 return ret; 312 313 if (!dev->tgt.nr_backing_files || dev->tgt.nr_backing_files > NR_STRIPE) 314 return -EINVAL; 315 316 assert(dev->nr_fds == dev->tgt.nr_backing_files + 1); 317 318 for (i = 0; i < dev->tgt.nr_backing_files; i++) 319 dev->tgt.backing_file_size[i] &= ~((1 << chunk_shift) - 1); 320 321 for (i = 0; i < dev->tgt.nr_backing_files; i++) { 322 unsigned long size = dev->tgt.backing_file_size[i]; 323 324 if (size != dev->tgt.backing_file_size[0]) 325 return -EINVAL; 326 bytes += size; 327 } 328 329 conf = malloc(sizeof(*conf)); 330 conf->shift = chunk_shift; 331 conf->nr_files = dev->tgt.nr_backing_files; 332 333 dev->private_data = conf; 334 dev->tgt.dev_size = bytes; 335 p.basic.dev_sectors = bytes >> 9; 336 dev->tgt.params = p; 337 338 if (dev->dev_info.flags & UBLK_F_SUPPORT_ZERO_COPY) 339 mul = 2; 340 dev->tgt.sq_depth = mul * dev->dev_info.queue_depth * conf->nr_files; 341 dev->tgt.cq_depth = mul * dev->dev_info.queue_depth * conf->nr_files; 342 343 printf("%s: shift %u files %u\n", __func__, conf->shift, conf->nr_files); 344 345 return 0; 346 } 347 348 static void ublk_stripe_tgt_deinit(struct ublk_dev *dev) 349 { 350 free(dev->private_data); 351 backing_file_tgt_deinit(dev); 352 } 353 354 static void ublk_stripe_cmd_line(struct dev_ctx *ctx, int argc, char *argv[]) 355 { 356 static const struct option longopts[] = { 357 { "chunk_size", 1, NULL, 0 }, 358 { 0, 0, 0, 0 } 359 }; 360 int option_idx, opt; 361 362 ctx->stripe.chunk_size = 65536; 363 while ((opt = getopt_long(argc, argv, "", 364 longopts, &option_idx)) != -1) { 365 switch (opt) { 366 case 0: 367 if (!strcmp(longopts[option_idx].name, "chunk_size")) 368 ctx->stripe.chunk_size = strtol(optarg, NULL, 10); 369 } 370 } 371 } 372 373 static void ublk_stripe_usage(const struct ublk_tgt_ops *ops) 374 { 375 printf("\tstripe: [--chunk_size chunk_size (default 65536)]\n"); 376 } 377 378 const struct ublk_tgt_ops stripe_tgt_ops = { 379 .name = "stripe", 380 .init_tgt = ublk_stripe_tgt_init, 381 .deinit_tgt = ublk_stripe_tgt_deinit, 382 .queue_io = ublk_stripe_queue_io, 383 .tgt_io_done = ublk_stripe_io_done, 384 .parse_cmd_line = ublk_stripe_cmd_line, 385 .usage = ublk_stripe_usage, 386 }; 387