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 null_close_imp_open_zone(dev); 315 return BLK_STS_OK; 316 } 317 318 return BLK_STS_ZONE_OPEN_RESOURCE; 319 } 320 321 /* 322 * This function matches the manage open zone resources function in the ZBC standard, 323 * with the addition of max active zones support (added in the ZNS standard). 324 * 325 * The function determines if a zone can transition to implicit open or explicit open, 326 * while maintaining the max open zone (and max active zone) limit(s). It may close an 327 * implicit open zone in order to make additional zone resources available. 328 * 329 * ZBC states that an implicit open zone shall be closed only if there is not 330 * room within the open limit. However, with the addition of an active limit, 331 * it is not certain that closing an implicit open zone will allow an empty 332 * zone to be opened, since we might already be at the active limit capacity. 333 * Therefore, the active limit is checked first for an empty zone. A closed 334 * zone already holds an active zone resource, so only the open limit has to 335 * be checked for a closed zone. 336 */ 337 static blk_status_t null_check_zone_resources(struct nullb_device *dev, 338 struct nullb_zone *zone) 339 { 340 blk_status_t ret; 341 342 switch (zone->cond) { 343 case BLK_ZONE_COND_EMPTY: 344 ret = null_check_active(dev); 345 if (ret != BLK_STS_OK) 346 return ret; 347 fallthrough; 348 case BLK_ZONE_COND_CLOSED: 349 return null_check_open(dev); 350 default: 351 /* Should never be called for other states */ 352 WARN_ON(1); 353 return BLK_STS_IOERR; 354 } 355 } 356 357 static blk_status_t null_zone_write(struct nullb_cmd *cmd, sector_t sector, 358 unsigned int nr_sectors, bool append) 359 { 360 struct nullb_device *dev = cmd->nq->dev; 361 unsigned int zno = null_zone_no(dev, sector); 362 struct nullb_zone *zone = &dev->zones[zno]; 363 blk_status_t badblocks_ret = BLK_STS_OK; 364 blk_status_t ret; 365 366 trace_nullb_zone_op(cmd, zno, zone->cond); 367 368 if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL) { 369 if (append) 370 return BLK_STS_IOERR; 371 return null_process_cmd(cmd, REQ_OP_WRITE, sector, nr_sectors); 372 } 373 374 null_lock_zone(dev, zone); 375 376 /* 377 * Regular writes must be at the write pointer position. Zone append 378 * writes are automatically issued at the write pointer and the position 379 * returned using the request sector. Note that we do not check the zone 380 * condition because for FULL, READONLY and OFFLINE zones, the sector 381 * check against the zone write pointer will always result in failing 382 * the command. 383 */ 384 if (append) { 385 if (WARN_ON_ONCE(!dev->zone_append_max_sectors) || 386 zone->wp == NULL_ZONE_INVALID_WP) { 387 ret = BLK_STS_IOERR; 388 goto unlock_zone; 389 } 390 sector = zone->wp; 391 blk_mq_rq_from_pdu(cmd)->__sector = sector; 392 } 393 394 if (sector != zone->wp || 395 zone->wp + nr_sectors > zone->start + zone->capacity) { 396 ret = BLK_STS_IOERR; 397 goto unlock_zone; 398 } 399 400 if (zone->cond == BLK_ZONE_COND_CLOSED || 401 zone->cond == BLK_ZONE_COND_EMPTY) { 402 if (dev->need_zone_res_mgmt) { 403 spin_lock(&dev->zone_res_lock); 404 405 ret = null_check_zone_resources(dev, zone); 406 if (ret != BLK_STS_OK) { 407 spin_unlock(&dev->zone_res_lock); 408 goto unlock_zone; 409 } 410 if (zone->cond == BLK_ZONE_COND_CLOSED) { 411 dev->nr_zones_closed--; 412 dev->nr_zones_imp_open++; 413 } else if (zone->cond == BLK_ZONE_COND_EMPTY) { 414 dev->nr_zones_imp_open++; 415 } 416 417 spin_unlock(&dev->zone_res_lock); 418 } 419 420 zone->cond = BLK_ZONE_COND_IMP_OPEN; 421 } 422 423 if (dev->badblocks.shift != -1) { 424 badblocks_ret = null_handle_badblocks(cmd, sector, &nr_sectors); 425 if (badblocks_ret != BLK_STS_OK && !nr_sectors) { 426 ret = badblocks_ret; 427 goto unlock_zone; 428 } 429 } 430 431 if (dev->memory_backed) { 432 ret = null_handle_memory_backed(cmd, REQ_OP_WRITE, sector, 433 nr_sectors); 434 if (ret != BLK_STS_OK) 435 goto unlock_zone; 436 } 437 438 zone->wp += nr_sectors; 439 if (zone->wp == zone->start + zone->capacity) { 440 if (dev->need_zone_res_mgmt) { 441 spin_lock(&dev->zone_res_lock); 442 if (zone->cond == BLK_ZONE_COND_EXP_OPEN) 443 dev->nr_zones_exp_open--; 444 else if (zone->cond == BLK_ZONE_COND_IMP_OPEN) 445 dev->nr_zones_imp_open--; 446 spin_unlock(&dev->zone_res_lock); 447 } 448 zone->cond = BLK_ZONE_COND_FULL; 449 } 450 451 ret = badblocks_ret; 452 453 unlock_zone: 454 null_unlock_zone(dev, zone); 455 456 return ret; 457 } 458 459 static blk_status_t null_open_zone(struct nullb_device *dev, 460 struct nullb_zone *zone) 461 { 462 blk_status_t ret = BLK_STS_OK; 463 464 if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL) 465 return BLK_STS_IOERR; 466 467 switch (zone->cond) { 468 case BLK_ZONE_COND_EXP_OPEN: 469 /* Open operation on exp open is not an error */ 470 return BLK_STS_OK; 471 case BLK_ZONE_COND_EMPTY: 472 case BLK_ZONE_COND_IMP_OPEN: 473 case BLK_ZONE_COND_CLOSED: 474 break; 475 case BLK_ZONE_COND_FULL: 476 default: 477 return BLK_STS_IOERR; 478 } 479 480 if (dev->need_zone_res_mgmt) { 481 spin_lock(&dev->zone_res_lock); 482 483 switch (zone->cond) { 484 case BLK_ZONE_COND_EMPTY: 485 ret = null_check_zone_resources(dev, zone); 486 if (ret != BLK_STS_OK) { 487 spin_unlock(&dev->zone_res_lock); 488 return ret; 489 } 490 break; 491 case BLK_ZONE_COND_IMP_OPEN: 492 dev->nr_zones_imp_open--; 493 break; 494 case BLK_ZONE_COND_CLOSED: 495 ret = null_check_zone_resources(dev, zone); 496 if (ret != BLK_STS_OK) { 497 spin_unlock(&dev->zone_res_lock); 498 return ret; 499 } 500 dev->nr_zones_closed--; 501 break; 502 default: 503 break; 504 } 505 506 dev->nr_zones_exp_open++; 507 508 spin_unlock(&dev->zone_res_lock); 509 } 510 511 zone->cond = BLK_ZONE_COND_EXP_OPEN; 512 513 return BLK_STS_OK; 514 } 515 516 static blk_status_t null_close_zone(struct nullb_device *dev, 517 struct nullb_zone *zone) 518 { 519 if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL) 520 return BLK_STS_IOERR; 521 522 switch (zone->cond) { 523 case BLK_ZONE_COND_CLOSED: 524 /* close operation on closed is not an error */ 525 return BLK_STS_OK; 526 case BLK_ZONE_COND_IMP_OPEN: 527 case BLK_ZONE_COND_EXP_OPEN: 528 break; 529 case BLK_ZONE_COND_EMPTY: 530 case BLK_ZONE_COND_FULL: 531 default: 532 return BLK_STS_IOERR; 533 } 534 535 if (dev->need_zone_res_mgmt) { 536 spin_lock(&dev->zone_res_lock); 537 538 switch (zone->cond) { 539 case BLK_ZONE_COND_IMP_OPEN: 540 dev->nr_zones_imp_open--; 541 break; 542 case BLK_ZONE_COND_EXP_OPEN: 543 dev->nr_zones_exp_open--; 544 break; 545 default: 546 break; 547 } 548 549 if (zone->wp > zone->start) 550 dev->nr_zones_closed++; 551 552 spin_unlock(&dev->zone_res_lock); 553 } 554 555 if (zone->wp == zone->start) 556 zone->cond = BLK_ZONE_COND_EMPTY; 557 else 558 zone->cond = BLK_ZONE_COND_CLOSED; 559 560 return BLK_STS_OK; 561 } 562 563 static blk_status_t null_finish_zone(struct nullb_device *dev, 564 struct nullb_zone *zone) 565 { 566 blk_status_t ret = BLK_STS_OK; 567 568 if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL) 569 return BLK_STS_IOERR; 570 571 if (dev->need_zone_res_mgmt) { 572 spin_lock(&dev->zone_res_lock); 573 574 switch (zone->cond) { 575 case BLK_ZONE_COND_FULL: 576 /* Finish operation on full is not an error */ 577 spin_unlock(&dev->zone_res_lock); 578 return BLK_STS_OK; 579 case BLK_ZONE_COND_EMPTY: 580 ret = null_check_zone_resources(dev, zone); 581 if (ret != BLK_STS_OK) { 582 spin_unlock(&dev->zone_res_lock); 583 return ret; 584 } 585 break; 586 case BLK_ZONE_COND_IMP_OPEN: 587 dev->nr_zones_imp_open--; 588 break; 589 case BLK_ZONE_COND_EXP_OPEN: 590 dev->nr_zones_exp_open--; 591 break; 592 case BLK_ZONE_COND_CLOSED: 593 ret = null_check_zone_resources(dev, zone); 594 if (ret != BLK_STS_OK) { 595 spin_unlock(&dev->zone_res_lock); 596 return ret; 597 } 598 dev->nr_zones_closed--; 599 break; 600 default: 601 spin_unlock(&dev->zone_res_lock); 602 return BLK_STS_IOERR; 603 } 604 605 spin_unlock(&dev->zone_res_lock); 606 } 607 608 zone->cond = BLK_ZONE_COND_FULL; 609 zone->wp = zone->start + zone->len; 610 611 return BLK_STS_OK; 612 } 613 614 static blk_status_t null_reset_zone(struct nullb_device *dev, 615 struct nullb_zone *zone) 616 { 617 if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL) 618 return BLK_STS_IOERR; 619 620 if (dev->need_zone_res_mgmt) { 621 spin_lock(&dev->zone_res_lock); 622 623 switch (zone->cond) { 624 case BLK_ZONE_COND_IMP_OPEN: 625 dev->nr_zones_imp_open--; 626 break; 627 case BLK_ZONE_COND_EXP_OPEN: 628 dev->nr_zones_exp_open--; 629 break; 630 case BLK_ZONE_COND_CLOSED: 631 dev->nr_zones_closed--; 632 break; 633 case BLK_ZONE_COND_EMPTY: 634 case BLK_ZONE_COND_FULL: 635 break; 636 default: 637 spin_unlock(&dev->zone_res_lock); 638 return BLK_STS_IOERR; 639 } 640 641 spin_unlock(&dev->zone_res_lock); 642 } 643 644 zone->cond = BLK_ZONE_COND_EMPTY; 645 zone->wp = zone->start; 646 647 if (dev->memory_backed) 648 return null_handle_discard(dev, zone->start, zone->len); 649 650 return BLK_STS_OK; 651 } 652 653 static blk_status_t null_zone_mgmt(struct nullb_cmd *cmd, enum req_op op, 654 sector_t sector) 655 { 656 struct nullb_device *dev = cmd->nq->dev; 657 unsigned int zone_no; 658 struct nullb_zone *zone; 659 blk_status_t ret; 660 size_t i; 661 662 if (op == REQ_OP_ZONE_RESET_ALL) { 663 for (i = dev->zone_nr_conv; i < dev->nr_zones; i++) { 664 zone = &dev->zones[i]; 665 null_lock_zone(dev, zone); 666 if (zone->cond != BLK_ZONE_COND_EMPTY && 667 zone->cond != BLK_ZONE_COND_READONLY && 668 zone->cond != BLK_ZONE_COND_OFFLINE) { 669 null_reset_zone(dev, zone); 670 trace_nullb_zone_op(cmd, i, zone->cond); 671 } 672 null_unlock_zone(dev, zone); 673 } 674 return BLK_STS_OK; 675 } 676 677 zone_no = null_zone_no(dev, sector); 678 zone = &dev->zones[zone_no]; 679 680 null_lock_zone(dev, zone); 681 682 if (zone->cond == BLK_ZONE_COND_READONLY || 683 zone->cond == BLK_ZONE_COND_OFFLINE) { 684 ret = BLK_STS_IOERR; 685 goto unlock; 686 } 687 688 switch (op) { 689 case REQ_OP_ZONE_RESET: 690 ret = null_reset_zone(dev, zone); 691 break; 692 case REQ_OP_ZONE_OPEN: 693 ret = null_open_zone(dev, zone); 694 break; 695 case REQ_OP_ZONE_CLOSE: 696 ret = null_close_zone(dev, zone); 697 break; 698 case REQ_OP_ZONE_FINISH: 699 ret = null_finish_zone(dev, zone); 700 break; 701 default: 702 ret = BLK_STS_NOTSUPP; 703 break; 704 } 705 706 if (ret == BLK_STS_OK) 707 trace_nullb_zone_op(cmd, zone_no, zone->cond); 708 709 unlock: 710 null_unlock_zone(dev, zone); 711 712 return ret; 713 } 714 715 blk_status_t null_process_zoned_cmd(struct nullb_cmd *cmd, enum req_op op, 716 sector_t sector, sector_t nr_sectors) 717 { 718 struct nullb_device *dev; 719 struct nullb_zone *zone; 720 blk_status_t sts; 721 722 switch (op) { 723 case REQ_OP_WRITE: 724 return null_zone_write(cmd, sector, nr_sectors, false); 725 case REQ_OP_ZONE_APPEND: 726 return null_zone_write(cmd, sector, nr_sectors, true); 727 case REQ_OP_ZONE_RESET: 728 case REQ_OP_ZONE_RESET_ALL: 729 case REQ_OP_ZONE_OPEN: 730 case REQ_OP_ZONE_CLOSE: 731 case REQ_OP_ZONE_FINISH: 732 return null_zone_mgmt(cmd, op, sector); 733 default: 734 dev = cmd->nq->dev; 735 zone = &dev->zones[null_zone_no(dev, sector)]; 736 if (zone->cond == BLK_ZONE_COND_OFFLINE) 737 return BLK_STS_IOERR; 738 739 null_lock_zone(dev, zone); 740 sts = null_process_cmd(cmd, op, sector, nr_sectors); 741 null_unlock_zone(dev, zone); 742 return sts; 743 } 744 } 745 746 /* 747 * Release the zone resources held by a zone, before changing the condition of 748 * the zone to a condition that does not hold any zone resources. 749 */ 750 static void null_release_zone_resources(struct nullb_device *dev, 751 struct nullb_zone *zone) 752 { 753 if (!dev->need_zone_res_mgmt) 754 return; 755 756 spin_lock(&dev->zone_res_lock); 757 switch (zone->cond) { 758 case BLK_ZONE_COND_IMP_OPEN: 759 dev->nr_zones_imp_open--; 760 break; 761 case BLK_ZONE_COND_EXP_OPEN: 762 dev->nr_zones_exp_open--; 763 break; 764 case BLK_ZONE_COND_CLOSED: 765 dev->nr_zones_closed--; 766 break; 767 default: 768 break; 769 } 770 spin_unlock(&dev->zone_res_lock); 771 } 772 773 static void null_set_zone_readonly_or_offline(struct nullb_device *dev, 774 struct nullb_zone *zone, 775 bool set_offline) 776 { 777 enum blk_zone_cond cond = 778 set_offline ? BLK_ZONE_COND_OFFLINE : BLK_ZONE_COND_READONLY; 779 780 null_lock_zone(dev, zone); 781 782 /* 783 * If the read-only condition is requested again to zones already in 784 * read-only condition, restore back normal empty condition. Do the same 785 * if the offline condition is requested for offline zones. Otherwise, 786 * set the specified zone condition to the zones. Release the zone 787 * resources held by the zones beforehand. 788 */ 789 if (zone->cond == cond) { 790 zone->cond = BLK_ZONE_COND_EMPTY; 791 zone->wp = zone->start; 792 if (dev->memory_backed) 793 null_handle_discard(dev, zone->start, zone->len); 794 } else { 795 null_release_zone_resources(dev, zone); 796 zone->cond = cond; 797 zone->wp = NULL_ZONE_INVALID_WP; 798 } 799 800 null_unlock_zone(dev, zone); 801 } 802 803 /* 804 * Identify a zone from the sector written to configfs file. Then set zone 805 * condition to the zone. 806 */ 807 ssize_t zone_cond_store(struct nullb_device *dev, const char *page, 808 size_t count, enum blk_zone_cond cond) 809 { 810 unsigned long long sector; 811 unsigned int zone_no; 812 int ret; 813 814 /* Read-only and offline are the only conditions supported for now */ 815 if (WARN_ON_ONCE(cond != BLK_ZONE_COND_READONLY && 816 cond != BLK_ZONE_COND_OFFLINE)) 817 return -EINVAL; 818 819 if (!dev->zoned) { 820 pr_err("null_blk device is not zoned\n"); 821 return -EINVAL; 822 } 823 824 if (!dev->zones) { 825 pr_err("null_blk device is not yet powered\n"); 826 return -EINVAL; 827 } 828 829 ret = kstrtoull(page, 0, §or); 830 if (ret < 0) 831 return ret; 832 833 zone_no = null_zone_no(dev, sector); 834 if (zone_no >= dev->nr_zones) { 835 pr_err("Sector out of range\n"); 836 return -EINVAL; 837 } 838 839 if (dev->zones[zone_no].type == BLK_ZONE_TYPE_CONVENTIONAL) { 840 pr_err("Can not change condition of conventional zones\n"); 841 return -EINVAL; 842 } 843 844 null_set_zone_readonly_or_offline(dev, &dev->zones[zone_no], 845 cond == BLK_ZONE_COND_OFFLINE); 846 847 return count; 848 } 849