1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/vmalloc.h> 3 #include <linux/bitmap.h> 4 #include "null_blk.h" 5 6 #define CREATE_TRACE_POINTS 7 #include "trace.h" 8 9 #undef pr_fmt 10 #define pr_fmt(fmt) "null_blk: " fmt 11 12 #define NULL_ZONE_INVALID_WP ((sector_t)-1) 13 14 static inline sector_t mb_to_sects(unsigned long mb) 15 { 16 return ((sector_t)mb * SZ_1M) >> SECTOR_SHIFT; 17 } 18 19 static inline unsigned int null_zone_no(struct nullb_device *dev, sector_t sect) 20 { 21 if (WARN_ON_ONCE(!dev->zone_size_sects)) 22 return 0; 23 return sect >> ilog2(dev->zone_size_sects); 24 } 25 26 static inline void null_init_zone_lock(struct nullb_device *dev, 27 struct nullb_zone *zone) 28 { 29 if (!dev->memory_backed) 30 spin_lock_init(&zone->spinlock); 31 else 32 mutex_init(&zone->mutex); 33 } 34 35 static inline void null_lock_zone(struct nullb_device *dev, 36 struct nullb_zone *zone) 37 { 38 if (!dev->memory_backed) 39 spin_lock_irq(&zone->spinlock); 40 else 41 mutex_lock(&zone->mutex); 42 } 43 44 static inline void null_unlock_zone(struct nullb_device *dev, 45 struct nullb_zone *zone) 46 { 47 if (!dev->memory_backed) 48 spin_unlock_irq(&zone->spinlock); 49 else 50 mutex_unlock(&zone->mutex); 51 } 52 53 int null_init_zoned_dev(struct nullb_device *dev, 54 struct queue_limits *lim) 55 { 56 sector_t dev_capacity_sects, zone_capacity_sects; 57 struct nullb_zone *zone; 58 sector_t sector = 0; 59 unsigned int i; 60 61 if (!dev->zone_size || !is_power_of_2(dev->zone_size)) { 62 pr_err("zone_size must be non-zero power-of-two\n"); 63 return -EINVAL; 64 } 65 if (dev->zone_size > dev->size) { 66 pr_err("Zone size larger than device capacity\n"); 67 return -EINVAL; 68 } 69 70 if (!dev->zone_capacity) 71 dev->zone_capacity = dev->zone_size; 72 73 if (dev->zone_capacity > dev->zone_size) { 74 pr_err("zone capacity (%lu MB) larger than zone size (%lu MB)\n", 75 dev->zone_capacity, dev->zone_size); 76 return -EINVAL; 77 } 78 79 /* 80 * If a smaller zone capacity was requested, do not allow a smaller last 81 * zone at the same time as such zone configuration does not correspond 82 * to any real zoned device. 83 */ 84 if (dev->zone_capacity != dev->zone_size && 85 dev->size & (dev->zone_size - 1)) { 86 pr_err("A smaller last zone is not allowed with zone capacity smaller than zone size.\n"); 87 return -EINVAL; 88 } 89 90 zone_capacity_sects = mb_to_sects(dev->zone_capacity); 91 dev_capacity_sects = mb_to_sects(dev->size); 92 dev->zone_size_sects = mb_to_sects(dev->zone_size); 93 if (!dev->zone_size_sects) { 94 pr_err("zone_size too large or too small, leads to zero sectors\n"); 95 return -EINVAL; 96 } 97 dev->nr_zones = round_up(dev_capacity_sects, dev->zone_size_sects) 98 >> ilog2(dev->zone_size_sects); 99 100 dev->zones = kvmalloc_objs(struct nullb_zone, dev->nr_zones, 101 GFP_KERNEL | __GFP_ZERO); 102 if (!dev->zones) 103 return -ENOMEM; 104 105 spin_lock_init(&dev->zone_res_lock); 106 107 if (dev->zone_nr_conv >= dev->nr_zones) { 108 dev->zone_nr_conv = dev->nr_zones - 1; 109 pr_info("changed the number of conventional zones to %u", 110 dev->zone_nr_conv); 111 } 112 113 dev->zone_append_max_sectors = 114 min(ALIGN_DOWN(dev->zone_append_max_sectors, 115 dev->blocksize >> SECTOR_SHIFT), 116 zone_capacity_sects); 117 118 /* Max active zones has to be < nbr of seq zones in order to be enforceable */ 119 if (dev->zone_max_active >= dev->nr_zones - dev->zone_nr_conv) { 120 dev->zone_max_active = 0; 121 pr_info("zone_max_active limit disabled, limit >= zone count\n"); 122 } 123 124 /* Max open zones has to be <= max active zones */ 125 if (dev->zone_max_active && dev->zone_max_open > dev->zone_max_active) { 126 dev->zone_max_open = dev->zone_max_active; 127 pr_info("changed the maximum number of open zones to %u\n", 128 dev->zone_max_open); 129 } else if (dev->zone_max_open >= dev->nr_zones - dev->zone_nr_conv) { 130 dev->zone_max_open = 0; 131 pr_info("zone_max_open limit disabled, limit >= zone count\n"); 132 } 133 dev->need_zone_res_mgmt = dev->zone_max_active || dev->zone_max_open; 134 dev->imp_close_zone_no = dev->zone_nr_conv; 135 136 for (i = 0; i < dev->zone_nr_conv; i++) { 137 zone = &dev->zones[i]; 138 139 null_init_zone_lock(dev, zone); 140 zone->start = sector; 141 zone->len = dev->zone_size_sects; 142 zone->capacity = zone->len; 143 zone->wp = zone->start + zone->len; 144 zone->type = BLK_ZONE_TYPE_CONVENTIONAL; 145 zone->cond = BLK_ZONE_COND_NOT_WP; 146 147 sector += dev->zone_size_sects; 148 } 149 150 for (i = dev->zone_nr_conv; i < dev->nr_zones; i++) { 151 zone = &dev->zones[i]; 152 153 null_init_zone_lock(dev, zone); 154 zone->start = sector; 155 if (zone->start + dev->zone_size_sects > dev_capacity_sects) 156 zone->len = dev_capacity_sects - zone->start; 157 else 158 zone->len = dev->zone_size_sects; 159 zone->capacity = 160 min_t(sector_t, zone->len, zone_capacity_sects); 161 zone->type = BLK_ZONE_TYPE_SEQWRITE_REQ; 162 if (dev->zone_full) { 163 zone->cond = BLK_ZONE_COND_FULL; 164 zone->wp = zone->start + zone->capacity; 165 } else{ 166 zone->cond = BLK_ZONE_COND_EMPTY; 167 zone->wp = zone->start; 168 } 169 170 sector += dev->zone_size_sects; 171 } 172 173 lim->features |= BLK_FEAT_ZONED; 174 lim->chunk_sectors = dev->zone_size_sects; 175 lim->max_hw_zone_append_sectors = dev->zone_append_max_sectors; 176 lim->max_open_zones = dev->zone_max_open; 177 lim->max_active_zones = dev->zone_max_active; 178 return 0; 179 } 180 181 int null_register_zoned_dev(struct nullb *nullb) 182 { 183 struct request_queue *q = nullb->q; 184 struct gendisk *disk = nullb->disk; 185 186 pr_info("%s: using %s zone append\n", 187 disk->disk_name, 188 queue_emulates_zone_append(q) ? "emulated" : "native"); 189 190 return blk_revalidate_disk_zones(disk); 191 } 192 193 void null_free_zoned_dev(struct nullb_device *dev) 194 { 195 kvfree(dev->zones); 196 dev->zones = NULL; 197 } 198 199 int null_report_zones(struct gendisk *disk, sector_t sector, 200 unsigned int nr_zones, struct blk_report_zones_args *args) 201 { 202 struct nullb *nullb = disk->private_data; 203 struct nullb_device *dev = nullb->dev; 204 unsigned int first_zone, i; 205 struct nullb_zone *zone; 206 struct blk_zone blkz; 207 int error; 208 209 first_zone = null_zone_no(dev, sector); 210 if (first_zone >= dev->nr_zones) 211 return 0; 212 213 nr_zones = min(nr_zones, dev->nr_zones - first_zone); 214 trace_nullb_report_zones(nullb, nr_zones); 215 216 memset(&blkz, 0, sizeof(struct blk_zone)); 217 zone = &dev->zones[first_zone]; 218 for (i = 0; i < nr_zones; i++, zone++) { 219 /* 220 * Stacked DM target drivers will remap the zone information by 221 * modifying the zone information passed to the report callback. 222 * So use a local copy to avoid corruption of the device zone 223 * array. 224 */ 225 null_lock_zone(dev, zone); 226 blkz.start = zone->start; 227 blkz.len = zone->len; 228 blkz.wp = zone->wp; 229 blkz.type = zone->type; 230 blkz.cond = zone->cond; 231 blkz.capacity = zone->capacity; 232 null_unlock_zone(dev, zone); 233 234 error = disk_report_zone(disk, &blkz, i, args); 235 if (error) 236 return error; 237 } 238 239 return nr_zones; 240 } 241 242 /* 243 * This is called in the case of memory backing from null_process_cmd() 244 * with the target zone already locked. 245 */ 246 size_t null_zone_valid_read_len(struct nullb *nullb, 247 sector_t sector, unsigned int len) 248 { 249 struct nullb_device *dev = nullb->dev; 250 struct nullb_zone *zone = &dev->zones[null_zone_no(dev, sector)]; 251 unsigned int nr_sectors = DIV_ROUND_UP(len, SECTOR_SIZE); 252 253 /* Read must be below the write pointer position */ 254 if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL || 255 sector + nr_sectors <= zone->wp) 256 return len; 257 258 if (sector > zone->wp) 259 return 0; 260 261 return (zone->wp - sector) << SECTOR_SHIFT; 262 } 263 264 static void null_close_imp_open_zone(struct nullb_device *dev) 265 { 266 struct nullb_zone *zone; 267 unsigned int zno, i; 268 269 zno = dev->imp_close_zone_no; 270 if (zno >= dev->nr_zones) 271 zno = dev->zone_nr_conv; 272 273 for (i = dev->zone_nr_conv; i < dev->nr_zones; i++) { 274 zone = &dev->zones[zno]; 275 zno++; 276 if (zno >= dev->nr_zones) 277 zno = dev->zone_nr_conv; 278 279 if (zone->cond == BLK_ZONE_COND_IMP_OPEN) { 280 dev->nr_zones_imp_open--; 281 if (zone->wp == zone->start) { 282 zone->cond = BLK_ZONE_COND_EMPTY; 283 } else { 284 zone->cond = BLK_ZONE_COND_CLOSED; 285 dev->nr_zones_closed++; 286 } 287 dev->imp_close_zone_no = zno; 288 return; 289 } 290 } 291 } 292 293 static blk_status_t null_check_active(struct nullb_device *dev) 294 { 295 if (!dev->zone_max_active) 296 return BLK_STS_OK; 297 298 if (dev->nr_zones_exp_open + dev->nr_zones_imp_open + 299 dev->nr_zones_closed < dev->zone_max_active) 300 return BLK_STS_OK; 301 302 return BLK_STS_ZONE_ACTIVE_RESOURCE; 303 } 304 305 static blk_status_t null_check_open(struct nullb_device *dev) 306 { 307 if (!dev->zone_max_open) 308 return BLK_STS_OK; 309 310 if (dev->nr_zones_exp_open + dev->nr_zones_imp_open < dev->zone_max_open) 311 return BLK_STS_OK; 312 313 if (dev->nr_zones_imp_open) { 314 if (null_check_active(dev) == BLK_STS_OK) { 315 null_close_imp_open_zone(dev); 316 return BLK_STS_OK; 317 } 318 } 319 320 return BLK_STS_ZONE_OPEN_RESOURCE; 321 } 322 323 /* 324 * This function matches the manage open zone resources function in the ZBC standard, 325 * with the addition of max active zones support (added in the ZNS standard). 326 * 327 * The function determines if a zone can transition to implicit open or explicit open, 328 * while maintaining the max open zone (and max active zone) limit(s). It may close an 329 * implicit open zone in order to make additional zone resources available. 330 * 331 * ZBC states that an implicit open zone shall be closed only if there is not 332 * room within the open limit. However, with the addition of an active limit, 333 * it is not certain that closing an implicit open zone will allow a new zone 334 * to be opened, since we might already be at the active limit capacity. 335 */ 336 static blk_status_t null_check_zone_resources(struct nullb_device *dev, 337 struct nullb_zone *zone) 338 { 339 blk_status_t ret; 340 341 switch (zone->cond) { 342 case BLK_ZONE_COND_EMPTY: 343 ret = null_check_active(dev); 344 if (ret != BLK_STS_OK) 345 return ret; 346 fallthrough; 347 case BLK_ZONE_COND_CLOSED: 348 return null_check_open(dev); 349 default: 350 /* Should never be called for other states */ 351 WARN_ON(1); 352 return BLK_STS_IOERR; 353 } 354 } 355 356 static blk_status_t null_zone_write(struct nullb_cmd *cmd, sector_t sector, 357 unsigned int nr_sectors, bool append) 358 { 359 struct nullb_device *dev = cmd->nq->dev; 360 unsigned int zno = null_zone_no(dev, sector); 361 struct nullb_zone *zone = &dev->zones[zno]; 362 blk_status_t badblocks_ret = BLK_STS_OK; 363 blk_status_t ret; 364 365 trace_nullb_zone_op(cmd, zno, zone->cond); 366 367 if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL) { 368 if (append) 369 return BLK_STS_IOERR; 370 return null_process_cmd(cmd, REQ_OP_WRITE, sector, nr_sectors); 371 } 372 373 null_lock_zone(dev, zone); 374 375 /* 376 * Regular writes must be at the write pointer position. Zone append 377 * writes are automatically issued at the write pointer and the position 378 * returned using the request sector. Note that we do not check the zone 379 * condition because for FULL, READONLY and OFFLINE zones, the sector 380 * check against the zone write pointer will always result in failing 381 * the command. 382 */ 383 if (append) { 384 if (WARN_ON_ONCE(!dev->zone_append_max_sectors) || 385 zone->wp == NULL_ZONE_INVALID_WP) { 386 ret = BLK_STS_IOERR; 387 goto unlock_zone; 388 } 389 sector = zone->wp; 390 blk_mq_rq_from_pdu(cmd)->__sector = sector; 391 } 392 393 if (sector != zone->wp || 394 zone->wp + nr_sectors > zone->start + zone->capacity) { 395 ret = BLK_STS_IOERR; 396 goto unlock_zone; 397 } 398 399 if (zone->cond == BLK_ZONE_COND_CLOSED || 400 zone->cond == BLK_ZONE_COND_EMPTY) { 401 if (dev->need_zone_res_mgmt) { 402 spin_lock(&dev->zone_res_lock); 403 404 ret = null_check_zone_resources(dev, zone); 405 if (ret != BLK_STS_OK) { 406 spin_unlock(&dev->zone_res_lock); 407 goto unlock_zone; 408 } 409 if (zone->cond == BLK_ZONE_COND_CLOSED) { 410 dev->nr_zones_closed--; 411 dev->nr_zones_imp_open++; 412 } else if (zone->cond == BLK_ZONE_COND_EMPTY) { 413 dev->nr_zones_imp_open++; 414 } 415 416 spin_unlock(&dev->zone_res_lock); 417 } 418 419 zone->cond = BLK_ZONE_COND_IMP_OPEN; 420 } 421 422 if (dev->badblocks.shift != -1) { 423 badblocks_ret = null_handle_badblocks(cmd, sector, &nr_sectors); 424 if (badblocks_ret != BLK_STS_OK && !nr_sectors) { 425 ret = badblocks_ret; 426 goto unlock_zone; 427 } 428 } 429 430 if (dev->memory_backed) { 431 ret = null_handle_memory_backed(cmd, REQ_OP_WRITE, sector, 432 nr_sectors); 433 if (ret != BLK_STS_OK) 434 goto unlock_zone; 435 } 436 437 zone->wp += nr_sectors; 438 if (zone->wp == zone->start + zone->capacity) { 439 if (dev->need_zone_res_mgmt) { 440 spin_lock(&dev->zone_res_lock); 441 if (zone->cond == BLK_ZONE_COND_EXP_OPEN) 442 dev->nr_zones_exp_open--; 443 else if (zone->cond == BLK_ZONE_COND_IMP_OPEN) 444 dev->nr_zones_imp_open--; 445 spin_unlock(&dev->zone_res_lock); 446 } 447 zone->cond = BLK_ZONE_COND_FULL; 448 } 449 450 ret = badblocks_ret; 451 452 unlock_zone: 453 null_unlock_zone(dev, zone); 454 455 return ret; 456 } 457 458 static blk_status_t null_open_zone(struct nullb_device *dev, 459 struct nullb_zone *zone) 460 { 461 blk_status_t ret = BLK_STS_OK; 462 463 if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL) 464 return BLK_STS_IOERR; 465 466 switch (zone->cond) { 467 case BLK_ZONE_COND_EXP_OPEN: 468 /* Open operation on exp open is not an error */ 469 return BLK_STS_OK; 470 case BLK_ZONE_COND_EMPTY: 471 case BLK_ZONE_COND_IMP_OPEN: 472 case BLK_ZONE_COND_CLOSED: 473 break; 474 case BLK_ZONE_COND_FULL: 475 default: 476 return BLK_STS_IOERR; 477 } 478 479 if (dev->need_zone_res_mgmt) { 480 spin_lock(&dev->zone_res_lock); 481 482 switch (zone->cond) { 483 case BLK_ZONE_COND_EMPTY: 484 ret = null_check_zone_resources(dev, zone); 485 if (ret != BLK_STS_OK) { 486 spin_unlock(&dev->zone_res_lock); 487 return ret; 488 } 489 break; 490 case BLK_ZONE_COND_IMP_OPEN: 491 dev->nr_zones_imp_open--; 492 break; 493 case BLK_ZONE_COND_CLOSED: 494 ret = null_check_zone_resources(dev, zone); 495 if (ret != BLK_STS_OK) { 496 spin_unlock(&dev->zone_res_lock); 497 return ret; 498 } 499 dev->nr_zones_closed--; 500 break; 501 default: 502 break; 503 } 504 505 dev->nr_zones_exp_open++; 506 507 spin_unlock(&dev->zone_res_lock); 508 } 509 510 zone->cond = BLK_ZONE_COND_EXP_OPEN; 511 512 return BLK_STS_OK; 513 } 514 515 static blk_status_t null_close_zone(struct nullb_device *dev, 516 struct nullb_zone *zone) 517 { 518 if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL) 519 return BLK_STS_IOERR; 520 521 switch (zone->cond) { 522 case BLK_ZONE_COND_CLOSED: 523 /* close operation on closed is not an error */ 524 return BLK_STS_OK; 525 case BLK_ZONE_COND_IMP_OPEN: 526 case BLK_ZONE_COND_EXP_OPEN: 527 break; 528 case BLK_ZONE_COND_EMPTY: 529 case BLK_ZONE_COND_FULL: 530 default: 531 return BLK_STS_IOERR; 532 } 533 534 if (dev->need_zone_res_mgmt) { 535 spin_lock(&dev->zone_res_lock); 536 537 switch (zone->cond) { 538 case BLK_ZONE_COND_IMP_OPEN: 539 dev->nr_zones_imp_open--; 540 break; 541 case BLK_ZONE_COND_EXP_OPEN: 542 dev->nr_zones_exp_open--; 543 break; 544 default: 545 break; 546 } 547 548 if (zone->wp > zone->start) 549 dev->nr_zones_closed++; 550 551 spin_unlock(&dev->zone_res_lock); 552 } 553 554 if (zone->wp == zone->start) 555 zone->cond = BLK_ZONE_COND_EMPTY; 556 else 557 zone->cond = BLK_ZONE_COND_CLOSED; 558 559 return BLK_STS_OK; 560 } 561 562 static blk_status_t null_finish_zone(struct nullb_device *dev, 563 struct nullb_zone *zone) 564 { 565 blk_status_t ret = BLK_STS_OK; 566 567 if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL) 568 return BLK_STS_IOERR; 569 570 if (dev->need_zone_res_mgmt) { 571 spin_lock(&dev->zone_res_lock); 572 573 switch (zone->cond) { 574 case BLK_ZONE_COND_FULL: 575 /* Finish operation on full is not an error */ 576 spin_unlock(&dev->zone_res_lock); 577 return BLK_STS_OK; 578 case BLK_ZONE_COND_EMPTY: 579 ret = null_check_zone_resources(dev, zone); 580 if (ret != BLK_STS_OK) { 581 spin_unlock(&dev->zone_res_lock); 582 return ret; 583 } 584 break; 585 case BLK_ZONE_COND_IMP_OPEN: 586 dev->nr_zones_imp_open--; 587 break; 588 case BLK_ZONE_COND_EXP_OPEN: 589 dev->nr_zones_exp_open--; 590 break; 591 case BLK_ZONE_COND_CLOSED: 592 ret = null_check_zone_resources(dev, zone); 593 if (ret != BLK_STS_OK) { 594 spin_unlock(&dev->zone_res_lock); 595 return ret; 596 } 597 dev->nr_zones_closed--; 598 break; 599 default: 600 spin_unlock(&dev->zone_res_lock); 601 return BLK_STS_IOERR; 602 } 603 604 spin_unlock(&dev->zone_res_lock); 605 } 606 607 zone->cond = BLK_ZONE_COND_FULL; 608 zone->wp = zone->start + zone->len; 609 610 return BLK_STS_OK; 611 } 612 613 static blk_status_t null_reset_zone(struct nullb_device *dev, 614 struct nullb_zone *zone) 615 { 616 if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL) 617 return BLK_STS_IOERR; 618 619 if (dev->need_zone_res_mgmt) { 620 spin_lock(&dev->zone_res_lock); 621 622 switch (zone->cond) { 623 case BLK_ZONE_COND_IMP_OPEN: 624 dev->nr_zones_imp_open--; 625 break; 626 case BLK_ZONE_COND_EXP_OPEN: 627 dev->nr_zones_exp_open--; 628 break; 629 case BLK_ZONE_COND_CLOSED: 630 dev->nr_zones_closed--; 631 break; 632 case BLK_ZONE_COND_EMPTY: 633 case BLK_ZONE_COND_FULL: 634 break; 635 default: 636 spin_unlock(&dev->zone_res_lock); 637 return BLK_STS_IOERR; 638 } 639 640 spin_unlock(&dev->zone_res_lock); 641 } 642 643 zone->cond = BLK_ZONE_COND_EMPTY; 644 zone->wp = zone->start; 645 646 if (dev->memory_backed) 647 return null_handle_discard(dev, zone->start, zone->len); 648 649 return BLK_STS_OK; 650 } 651 652 static blk_status_t null_zone_mgmt(struct nullb_cmd *cmd, enum req_op op, 653 sector_t sector) 654 { 655 struct nullb_device *dev = cmd->nq->dev; 656 unsigned int zone_no; 657 struct nullb_zone *zone; 658 blk_status_t ret; 659 size_t i; 660 661 if (op == REQ_OP_ZONE_RESET_ALL) { 662 for (i = dev->zone_nr_conv; i < dev->nr_zones; i++) { 663 zone = &dev->zones[i]; 664 null_lock_zone(dev, zone); 665 if (zone->cond != BLK_ZONE_COND_EMPTY && 666 zone->cond != BLK_ZONE_COND_READONLY && 667 zone->cond != BLK_ZONE_COND_OFFLINE) { 668 null_reset_zone(dev, zone); 669 trace_nullb_zone_op(cmd, i, zone->cond); 670 } 671 null_unlock_zone(dev, zone); 672 } 673 return BLK_STS_OK; 674 } 675 676 zone_no = null_zone_no(dev, sector); 677 zone = &dev->zones[zone_no]; 678 679 null_lock_zone(dev, zone); 680 681 if (zone->cond == BLK_ZONE_COND_READONLY || 682 zone->cond == BLK_ZONE_COND_OFFLINE) { 683 ret = BLK_STS_IOERR; 684 goto unlock; 685 } 686 687 switch (op) { 688 case REQ_OP_ZONE_RESET: 689 ret = null_reset_zone(dev, zone); 690 break; 691 case REQ_OP_ZONE_OPEN: 692 ret = null_open_zone(dev, zone); 693 break; 694 case REQ_OP_ZONE_CLOSE: 695 ret = null_close_zone(dev, zone); 696 break; 697 case REQ_OP_ZONE_FINISH: 698 ret = null_finish_zone(dev, zone); 699 break; 700 default: 701 ret = BLK_STS_NOTSUPP; 702 break; 703 } 704 705 if (ret == BLK_STS_OK) 706 trace_nullb_zone_op(cmd, zone_no, zone->cond); 707 708 unlock: 709 null_unlock_zone(dev, zone); 710 711 return ret; 712 } 713 714 blk_status_t null_process_zoned_cmd(struct nullb_cmd *cmd, enum req_op op, 715 sector_t sector, sector_t nr_sectors) 716 { 717 struct nullb_device *dev; 718 struct nullb_zone *zone; 719 blk_status_t sts; 720 721 switch (op) { 722 case REQ_OP_WRITE: 723 return null_zone_write(cmd, sector, nr_sectors, false); 724 case REQ_OP_ZONE_APPEND: 725 return null_zone_write(cmd, sector, nr_sectors, true); 726 case REQ_OP_ZONE_RESET: 727 case REQ_OP_ZONE_RESET_ALL: 728 case REQ_OP_ZONE_OPEN: 729 case REQ_OP_ZONE_CLOSE: 730 case REQ_OP_ZONE_FINISH: 731 return null_zone_mgmt(cmd, op, sector); 732 default: 733 dev = cmd->nq->dev; 734 zone = &dev->zones[null_zone_no(dev, sector)]; 735 if (zone->cond == BLK_ZONE_COND_OFFLINE) 736 return BLK_STS_IOERR; 737 738 null_lock_zone(dev, zone); 739 sts = null_process_cmd(cmd, op, sector, nr_sectors); 740 null_unlock_zone(dev, zone); 741 return sts; 742 } 743 } 744 745 /* 746 * Set a zone in the read-only or offline condition. 747 */ 748 static void null_set_zone_cond(struct nullb_device *dev, 749 struct nullb_zone *zone, enum blk_zone_cond cond) 750 { 751 if (WARN_ON_ONCE(cond != BLK_ZONE_COND_READONLY && 752 cond != BLK_ZONE_COND_OFFLINE)) 753 return; 754 755 null_lock_zone(dev, zone); 756 757 /* 758 * If the read-only condition is requested again to zones already in 759 * read-only condition, restore back normal empty condition. Do the same 760 * if the offline condition is requested for offline zones. Otherwise, 761 * set the specified zone condition to the zones. Finish the zones 762 * beforehand to free up zone resources. 763 */ 764 if (zone->cond == cond) { 765 zone->cond = BLK_ZONE_COND_EMPTY; 766 zone->wp = zone->start; 767 if (dev->memory_backed) 768 null_handle_discard(dev, zone->start, zone->len); 769 } else { 770 if (zone->cond != BLK_ZONE_COND_READONLY && 771 zone->cond != BLK_ZONE_COND_OFFLINE) 772 null_finish_zone(dev, zone); 773 zone->cond = cond; 774 zone->wp = NULL_ZONE_INVALID_WP; 775 } 776 777 null_unlock_zone(dev, zone); 778 } 779 780 /* 781 * Identify a zone from the sector written to configfs file. Then set zone 782 * condition to the zone. 783 */ 784 ssize_t zone_cond_store(struct nullb_device *dev, const char *page, 785 size_t count, enum blk_zone_cond cond) 786 { 787 unsigned long long sector; 788 unsigned int zone_no; 789 int ret; 790 791 if (!dev->zoned) { 792 pr_err("null_blk device is not zoned\n"); 793 return -EINVAL; 794 } 795 796 if (!dev->zones) { 797 pr_err("null_blk device is not yet powered\n"); 798 return -EINVAL; 799 } 800 801 ret = kstrtoull(page, 0, §or); 802 if (ret < 0) 803 return ret; 804 805 zone_no = null_zone_no(dev, sector); 806 if (zone_no >= dev->nr_zones) { 807 pr_err("Sector out of range\n"); 808 return -EINVAL; 809 } 810 811 if (dev->zones[zone_no].type == BLK_ZONE_TYPE_CONVENTIONAL) { 812 pr_err("Can not change condition of conventional zones\n"); 813 return -EINVAL; 814 } 815 816 null_set_zone_cond(dev, &dev->zones[zone_no], cond); 817 818 return count; 819 } 820