1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright (C) 2022 Intel Corporation. 3 * Copyright (c) Peng Yu yupeng0921@gmail.com. 4 * All rights reserved. 5 */ 6 7 #include "bdev_raid.h" 8 9 #include "spdk/env.h" 10 #include "spdk/thread.h" 11 #include "spdk/string.h" 12 #include "spdk/util.h" 13 14 #include "spdk/log.h" 15 16 struct concat_block_range { 17 uint64_t start; 18 uint64_t length; 19 }; 20 21 /* 22 * brief: 23 * concat_bdev_io_completion function is called by lower layers to notify raid 24 * module that particular bdev_io is completed. 25 * params: 26 * bdev_io - pointer to bdev io submitted to lower layers, like child io 27 * success - bdev_io status 28 * cb_arg - function callback context (parent raid_bdev_io) 29 * returns: 30 * none 31 */ 32 static void 33 concat_bdev_io_completion(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 34 { 35 struct raid_bdev_io *raid_io = cb_arg; 36 37 spdk_bdev_free_io(bdev_io); 38 39 if (success) { 40 raid_bdev_io_complete(raid_io, SPDK_BDEV_IO_STATUS_SUCCESS); 41 } else { 42 raid_bdev_io_complete(raid_io, SPDK_BDEV_IO_STATUS_FAILED); 43 } 44 } 45 46 static void concat_submit_rw_request(struct raid_bdev_io *raid_io); 47 48 static void 49 _concat_submit_rw_request(void *_raid_io) 50 { 51 struct raid_bdev_io *raid_io = _raid_io; 52 53 concat_submit_rw_request(raid_io); 54 } 55 56 /* 57 * brief: 58 * concat_submit_rw_request function is used to submit I/O to the correct 59 * member disk for concat bdevs. 60 * params: 61 * raid_io 62 * returns: 63 * none 64 */ 65 static void 66 concat_submit_rw_request(struct raid_bdev_io *raid_io) 67 { 68 struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(raid_io); 69 struct raid_bdev_io_channel *raid_ch = raid_io->raid_ch; 70 struct raid_bdev *raid_bdev = raid_io->raid_bdev; 71 struct concat_block_range *block_range = raid_bdev->module_private; 72 uint64_t pd_lba; 73 uint64_t pd_blocks; 74 int pd_idx; 75 int ret = 0; 76 struct raid_base_bdev_info *base_info; 77 struct spdk_io_channel *base_ch; 78 int i; 79 80 pd_idx = -1; 81 for (i = 0; i < raid_bdev->num_base_bdevs; i++) { 82 if (block_range[i].start > bdev_io->u.bdev.offset_blocks) { 83 break; 84 } 85 pd_idx = i; 86 } 87 assert(pd_idx >= 0); 88 assert(bdev_io->u.bdev.offset_blocks >= block_range[pd_idx].start); 89 pd_lba = bdev_io->u.bdev.offset_blocks - block_range[pd_idx].start; 90 pd_blocks = bdev_io->u.bdev.num_blocks; 91 base_info = &raid_bdev->base_bdev_info[pd_idx]; 92 if (base_info->desc == NULL) { 93 SPDK_ERRLOG("base bdev desc null for pd_idx %u\n", pd_idx); 94 assert(0); 95 } 96 97 /* 98 * Submit child io to bdev layer with using base bdev descriptors, base 99 * bdev lba, base bdev child io length in blocks, buffer, completion 100 * function and function callback context 101 */ 102 assert(raid_ch != NULL); 103 assert(raid_ch->base_channel); 104 base_ch = raid_ch->base_channel[pd_idx]; 105 if (bdev_io->type == SPDK_BDEV_IO_TYPE_READ) { 106 ret = spdk_bdev_readv_blocks_ext(base_info->desc, base_ch, 107 bdev_io->u.bdev.iovs, bdev_io->u.bdev.iovcnt, 108 pd_lba, pd_blocks, concat_bdev_io_completion, 109 raid_io, bdev_io->u.bdev.ext_opts); 110 } else if (bdev_io->type == SPDK_BDEV_IO_TYPE_WRITE) { 111 ret = spdk_bdev_writev_blocks_ext(base_info->desc, base_ch, 112 bdev_io->u.bdev.iovs, bdev_io->u.bdev.iovcnt, 113 pd_lba, pd_blocks, concat_bdev_io_completion, 114 raid_io, bdev_io->u.bdev.ext_opts); 115 } else { 116 SPDK_ERRLOG("Recvd not supported io type %u\n", bdev_io->type); 117 assert(0); 118 } 119 120 if (ret == -ENOMEM) { 121 raid_bdev_queue_io_wait(raid_io, base_info->bdev, base_ch, 122 _concat_submit_rw_request); 123 } else if (ret != 0) { 124 SPDK_ERRLOG("bdev io submit error not due to ENOMEM, it should not happen\n"); 125 assert(false); 126 raid_bdev_io_complete(raid_io, SPDK_BDEV_IO_STATUS_FAILED); 127 } 128 } 129 130 static void concat_submit_null_payload_request(struct raid_bdev_io *raid_io); 131 132 static void 133 _concat_submit_null_payload_request(void *_raid_io) 134 { 135 struct raid_bdev_io *raid_io = _raid_io; 136 137 concat_submit_null_payload_request(raid_io); 138 } 139 140 static void 141 concat_base_io_complete(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 142 { 143 struct raid_bdev_io *raid_io = cb_arg; 144 145 raid_bdev_io_complete_part(raid_io, 1, success ? 146 SPDK_BDEV_IO_STATUS_SUCCESS : 147 SPDK_BDEV_IO_STATUS_FAILED); 148 149 spdk_bdev_free_io(bdev_io); 150 } 151 152 /* 153 * brief: 154 * concat_submit_null_payload_request function submits the next batch of 155 * io requests with range but without payload, like FLUSH and UNMAP, to member disks; 156 * it will submit as many as possible unless one base io request fails with -ENOMEM, 157 * in which case it will queue itself for later submission. 158 * params: 159 * bdev_io - pointer to parent bdev_io on raid bdev device 160 * returns: 161 * none 162 */ 163 static void 164 concat_submit_null_payload_request(struct raid_bdev_io *raid_io) 165 { 166 struct spdk_bdev_io *bdev_io; 167 struct raid_bdev *raid_bdev; 168 int ret; 169 struct raid_base_bdev_info *base_info; 170 struct spdk_io_channel *base_ch; 171 uint64_t pd_lba; 172 uint64_t pd_blocks; 173 uint64_t offset_blocks; 174 uint64_t num_blocks; 175 struct concat_block_range *block_range; 176 int i, start_idx, stop_idx; 177 178 bdev_io = spdk_bdev_io_from_ctx(raid_io); 179 raid_bdev = raid_io->raid_bdev; 180 block_range = raid_bdev->module_private; 181 182 offset_blocks = bdev_io->u.bdev.offset_blocks; 183 num_blocks = bdev_io->u.bdev.num_blocks; 184 start_idx = -1; 185 stop_idx = -1; 186 /* 187 * Go through all base bdevs, find the first bdev and the last bdev 188 */ 189 for (i = 0; i < raid_bdev->num_base_bdevs; i++) { 190 /* skip the bdevs before the offset_blocks */ 191 if (offset_blocks >= block_range[i].start + block_range[i].length) { 192 continue; 193 } 194 if (start_idx == -1) { 195 start_idx = i; 196 } else { 197 /* 198 * The offset_blocks might be at the middle of the first bdev. 199 * Besides the first bdev, the offset_blocks should be always 200 * at the start of the bdev. 201 */ 202 assert(offset_blocks == block_range[i].start); 203 } 204 pd_lba = offset_blocks - block_range[i].start; 205 pd_blocks = spdk_min(num_blocks, block_range[i].length - pd_lba); 206 offset_blocks += pd_blocks; 207 num_blocks -= pd_blocks; 208 if (num_blocks == 0) { 209 stop_idx = i; 210 break; 211 } 212 } 213 assert(start_idx >= 0); 214 assert(stop_idx >= 0); 215 216 if (raid_io->base_bdev_io_remaining == 0) { 217 raid_io->base_bdev_io_remaining = stop_idx - start_idx + 1; 218 } 219 offset_blocks = bdev_io->u.bdev.offset_blocks; 220 num_blocks = bdev_io->u.bdev.num_blocks; 221 for (i = start_idx; i <= stop_idx; i++) { 222 assert(offset_blocks >= block_range[i].start); 223 assert(offset_blocks < block_range[i].start + block_range[i].length); 224 pd_lba = offset_blocks - block_range[i].start; 225 pd_blocks = spdk_min(num_blocks, block_range[i].length - pd_lba); 226 offset_blocks += pd_blocks; 227 num_blocks -= pd_blocks; 228 /* 229 * Skip the IOs we have submitted 230 */ 231 if (i < start_idx + raid_io->base_bdev_io_submitted) { 232 continue; 233 } 234 base_info = &raid_bdev->base_bdev_info[i]; 235 base_ch = raid_io->raid_ch->base_channel[i]; 236 switch (bdev_io->type) { 237 case SPDK_BDEV_IO_TYPE_UNMAP: 238 ret = spdk_bdev_unmap_blocks(base_info->desc, base_ch, 239 pd_lba, pd_blocks, 240 concat_base_io_complete, raid_io); 241 break; 242 case SPDK_BDEV_IO_TYPE_FLUSH: 243 ret = spdk_bdev_flush_blocks(base_info->desc, base_ch, 244 pd_lba, pd_blocks, 245 concat_base_io_complete, raid_io); 246 break; 247 default: 248 SPDK_ERRLOG("submit request, invalid io type with null payload %u\n", bdev_io->type); 249 assert(false); 250 ret = -EIO; 251 } 252 if (ret == 0) { 253 raid_io->base_bdev_io_submitted++; 254 } else if (ret == -ENOMEM) { 255 raid_bdev_queue_io_wait(raid_io, base_info->bdev, base_ch, 256 _concat_submit_null_payload_request); 257 return; 258 } else { 259 SPDK_ERRLOG("bdev io submit error not due to ENOMEM, it should not happen\n"); 260 assert(false); 261 raid_bdev_io_complete(raid_io, SPDK_BDEV_IO_STATUS_FAILED); 262 return; 263 } 264 } 265 } 266 267 static int 268 concat_start(struct raid_bdev *raid_bdev) 269 { 270 uint64_t total_blockcnt = 0; 271 struct raid_base_bdev_info *base_info; 272 struct concat_block_range *block_range; 273 274 block_range = calloc(raid_bdev->num_base_bdevs, sizeof(struct concat_block_range)); 275 if (!block_range) { 276 SPDK_ERRLOG("Can not allocate block_range, num_base_bdevs: %u", 277 raid_bdev->num_base_bdevs); 278 return -ENOMEM; 279 } 280 281 int idx = 0; 282 RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { 283 uint64_t strip_cnt = base_info->bdev->blockcnt >> raid_bdev->strip_size_shift; 284 uint64_t pd_block_cnt = strip_cnt << raid_bdev->strip_size_shift; 285 286 block_range[idx].start = total_blockcnt; 287 block_range[idx].length = pd_block_cnt; 288 total_blockcnt += pd_block_cnt; 289 idx++; 290 } 291 292 raid_bdev->module_private = block_range; 293 294 SPDK_DEBUGLOG(bdev_concat, "total blockcount %" PRIu64 ", numbasedev %u, strip size shift %u\n", 295 total_blockcnt, raid_bdev->num_base_bdevs, raid_bdev->strip_size_shift); 296 raid_bdev->bdev.blockcnt = total_blockcnt; 297 298 raid_bdev->bdev.optimal_io_boundary = raid_bdev->strip_size; 299 raid_bdev->bdev.split_on_optimal_io_boundary = true; 300 301 return 0; 302 } 303 304 static bool 305 concat_stop(struct raid_bdev *raid_bdev) 306 { 307 struct concat_block_range *block_range = raid_bdev->module_private; 308 309 free(block_range); 310 311 return true; 312 } 313 314 static struct raid_bdev_module g_concat_module = { 315 .level = CONCAT, 316 .base_bdevs_min = 1, 317 .start = concat_start, 318 .stop = concat_stop, 319 .submit_rw_request = concat_submit_rw_request, 320 .submit_null_payload_request = concat_submit_null_payload_request, 321 }; 322 RAID_MODULE_REGISTER(&g_concat_module) 323 324 SPDK_LOG_REGISTER_COMPONENT(bdev_concat) 325