| Seq | Timestamp | Type | Name | Duration |
|---|---|---|---|---|
| 0/0 | 2026/11/12 05:08 | flow | patch-triage |
9mArgs:null Results: {
"EnableConfigs": [
"BLK_DEV_ZONED",
"BLK_DEV_ZONED_LOOP",
"SCSI",
"BLK_DEV_SD"
],
"FocusSymbols": [
"blk_ioctl_zone_storage_elements",
"bdev_report_storage_elements",
"bdev_remove_storage_element",
"bdev_restore_storage_elements",
"disk_zone_is_offline",
"disk_revalidate_zones",
"disk_init_storage_element_mgmt",
"zloop_report_elements",
"zloop_remove_element",
"zloop_restore_elements",
"zloop_degrade_element",
"zloop_ctl_degrade_element",
"zloop_prep_rq",
"sd_zbc_report_storage_elements",
"sd_zbc_remove_storage_element",
"sd_zbc_restore_storage_elements",
"sd_zbc_revalidate_zones"
],
"KMSANReasoning": "The patch series introduces zoned block device storage element management (reporting, depopulation/removal, restoration, and degradation) across the block layer (blk-zoned), SCSI ZBC (sd_zbc), and the zoned loop driver (zloop).\n\nAnalysis of memory handling across the changes:\n1. UAPI structures and kernel-to-user copies: The ioctl BLKREPORTSTORELEMS uses struct blk_se_report and struct blk_se to return storage element descriptors to user space. Both structures are naturally aligned with no compiler padding holes (struct blk_se has explicit reserved[13] aligning it to 32 bytes; struct blk_se_report has a 64-bit reserved field before the flex array). Furthermore, the kernel allocation uses kzalloc_flex(), and individual elements are either memset to zero (sd_zbc_parse_storage_element) or copied from kzalloc_objs-allocated arrays (zloop_create_storage_elements).\n2. Heap and stack allocations: All new heap allocations use kzalloc, kzalloc_objs, or kzalloc_flex. Stack structures and command buffers (e.g. SCSI CDBs, zloop options) are zero-initialized with `{ }` or memset before use.\n3. Control flow and bounds: The changes do not introduce branching on uninitialized data or alter data lengths in a way that exposes uninitialized memory.\n\nAny potential bugs in these changes would involve bounds checking, device locking/serialization (covered by LOCKDEP), or memory corruption/out-of-bounds accesses (covered by KASAN). There is no risk of uninitialized memory reads or information leaks to userspace that would necessitate a dedicated KMSAN fuzzing session.",
"NeedsKMSAN": false,
"Reasoning": "The patch series adds storage element management for zoned block devices. It introduces new user-space block ioctls (BLKREPORTSTORELEMS, BLKREMOVESTORELEM, BLKRESTORESTORELEMS) in core block layer (block/blk-zoned.c and block/ioctl.c) and implements storage element emulation in the virtual zoned loopback driver (drivers/block/zloop.c) via /dev/zloop-control as well as SCSI ZBC devices (drivers/scsi/sd_zbc.c). This adds significant state management, queue freezing, revalidation, and caching invalidation logic that is fully reachable and warrants fuzzing.",
"WorthFuzzing": true
} |
| 1/1 | 2026/11/12 05:08 | action | read-patch-diff |
0mArgs:null Results: {
"PatchDiff": "commit ccd0317eb70f3d874172417cfe9ce93eeaabf238\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate: Thu Nov 12 05:08:32 2026 +0000\n\n syz-cluster: applied patch under review\n\ndiff --git a/Documentation/admin-guide/blockdev/zoned_loop.rst b/Documentation/admin-guide/blockdev/zoned_loop.rst\nindex 64277494fb36af..d378fbdc1983ef 100644\n--- a/Documentation/admin-guide/blockdev/zoned_loop.rst\n+++ b/Documentation/admin-guide/blockdev/zoned_loop.rst\n@@ -61,8 +61,9 @@ The options available for the add command can be listed by reading the\n /dev/zloop-control device::\n \n \t$ cat /dev/zloop-control\n- add id=%d,capacity_mb=%u,zone_size_mb=%u,zone_capacity_mb=%u,conv_zones=%u,max_open_zones=%u,base_dir=%s,nr_queues=%u,queue_depth=%u,buffered_io,zone_append=%u,ordered_zone_append,discard_write_cache\n+ add id=%d,capacity_mb=%u,zone_size_mb=%u,zone_capacity_mb=%u,conv_zones=%u,max_open_zones=%u,base_dir=%s,nr_queues=%u,queue_depth=%u,buffered_io,zone_append=%u,ordered_zone_append,discard_write_cache,stor_elements=%u\n remove id=%d\n+ degrade_element id=%d,element_id=%u\n \n In more details, the options that can be used with the \"add\" command are as\n follows.\n@@ -113,6 +114,15 @@ discard_write_cache Discard all data that was not explicitly persisted using a\n each zone file to the size recorded during the last flush\n operation. This simulates power fail events where\n uncommitted data is lost.\n+stor_elements Control storage element emulation. The default value is\n+ 0, indicating no emulation. A value of 1 indicates that\n+ storage elements are emulated as full access physical\n+ elements, which is equivalent to combined read+write heads\n+ of a hard-disk. A value of 2 enables storage element\n+ emulation as fractional access physical storage elements,\n+ which is equivalent to pairs of read and write heads of a\n+ hard-disk. The health state of storage elements is not\n+ preserved across a zloop device remove and add.\n =================== =========================================================\n \n 3) Deleting a Zoned Device\n@@ -135,7 +145,26 @@ the zoned device will not be created.\n To fully delete a zoned device, after executing the remove operation, the device\n base directory containing the backing files of the device zones must be deleted.\n \n-4) Example\n+4) Degrading a storage element\n+------------------------------\n+\n+When storage element emulation is enabled, storage elements can be set in a\n+degraded state to emulate defective storage elements (e.g. defective heads of a\n+hard disk). This is done by issuing the \"degrade_element\" command to\n+/dev/zloop-control, specifying the ID of the target device and the ID of the\n+storage element to degrade::\n+\n+ $ echo \"degrade_element id=X,element_id=Y\" \u003e /dev/zloop-control\n+\n+Depending on the type of the storage element specified, a read or write access\n+to sectors of a zone served by a degraded storage element will result in a\n+failure. For partial access storage elements, write accesses are always failed\n+if the write element serving the zone is degraded. Likewise, read accesses are\n+failed if the read element serving the zone is degraded. When storage elements\n+are emulated as all access physical elements, both read and write operations are\n+failed when the storage element serving the zones being accessed is degraded.\n+\n+5) Example\n ----------\n \n The following sequence of commands creates a 2GB zoned device with zones of 64\ndiff --git a/block/blk-zoned.c b/block/blk-zoned.c\nindex 4c588929619124..d56afbeb436a15 100644\n--- a/block/blk-zoned.c\n+++ b/block/blk-zoned.c\n@@ -18,6 +18,7 @@\n #include \u003clinux/mempool.h\u003e\n #include \u003clinux/kthread.h\u003e\n #include \u003clinux/freezer.h\u003e\n+#include \u003clinux/uaccess.h\u003e\n \n #include \u003ctrace/events/block.h\u003e\n \n@@ -292,6 +293,24 @@ static bool disk_zone_is_seq(struct gendisk *disk, sector_t sector)\n \treturn !blk_zstate_is_conv(zs);\n }\n \n+/**\n+ * disk_zone_is_offline - check if a sector belongs to an offline zone\n+ * @disk:\tgendisk to check\n+ * @sector:\tsector number\n+ *\n+ * Check if @sector on @disk is contained in an offline zone.\n+ * This function can be used only when the bdev queue was entered (BIOs\n+ * submitted but not yet completed) or when holding zone_revalidate_mutex.\n+ */\n+bool disk_zone_is_offline(struct gendisk *disk, sector_t sector)\n+{\n+\tif (!blk_queue_is_zoned(disk-\u003equeue))\n+\t\treturn false;\n+\n+\treturn disk_zone_get_cond(disk, sector) == BLK_ZONE_COND_OFFLINE;\n+}\n+EXPORT_SYMBOL_GPL(disk_zone_is_offline);\n+\n /**\n * bdev_zone_is_seq - check if a sector belongs to a sequential write zone\n * @bdev: block device to check\n@@ -2534,20 +2553,7 @@ static int blk_revalidate_zone_cb(struct blk_zone *zone, unsigned int idx,\n \treturn ret;\n }\n \n-/**\n- * blk_revalidate_disk_zones - (re)allocate and initialize zone write plugs\n- * @disk:\tTarget disk\n- *\n- * Helper function for low-level device drivers to check, (re) allocate and\n- * initialize resources used for managing zoned disks. This function should\n- * normally be called by blk-mq based drivers when a zoned gendisk is probed\n- * and when the zone configuration of the gendisk changes (e.g. after a format).\n- * Before calling this function, the device driver must already have set the\n- * device zone size (chunk_sector limit) and the max zone append limit.\n- * BIO based drivers can also use this function as long as the device queue\n- * can be safely frozen.\n- */\n-int blk_revalidate_disk_zones(struct gendisk *disk)\n+static int disk_revalidate_zones(struct gendisk *disk, bool drop_all_zwplugs)\n {\n \tstruct blk_revalidate_zone_args args = { };\n \tstruct blk_report_zones_args rep_args = {\n@@ -2558,19 +2564,13 @@ int blk_revalidate_disk_zones(struct gendisk *disk)\n \tint retries = 2;\n \tint ret;\n \n-\tif (WARN_ON_ONCE(!blk_queue_is_zoned(disk-\u003equeue)))\n-\t\treturn -EIO;\n-\n-\t/*\n-\t * Serialize calls to this function so that we can safely look at and\n-\t * eventually change the disk zone information.\n-\t */\n-\tmutex_lock(\u0026disk-\u003ezone_revalidate_mutex);\n+\tif (drop_all_zwplugs)\n+\t\tdisk_for_all_zone_wplugs(disk, disk_drop_zone_wplug, NULL);\n \n again:\n \tret = disk_revalidate_capacity(disk, \u0026args);\n \tif (ret)\n-\t\tgoto unlock;\n+\t\treturn ret;\n \n \t/*\n \t * Allocate zone resources if they are needed and we have not done\n@@ -2583,14 +2583,14 @@ int blk_revalidate_disk_zones(struct gendisk *disk)\n \t\tret = disk_alloc_zone_resources(disk, args.capacity);\n \t\tif (ret) {\n \t\t\tmemalloc_noio_restore(noio_flag);\n-\t\t\tgoto unlock;\n+\t\t\treturn ret;\n \t\t}\n \t}\n \n \tret = disk_init_revalidate_args(disk, \u0026args);\n \tif (ret) {\n \t\tmemalloc_noio_restore(noio_flag);\n-\t\tgoto unlock;\n+\t\treturn ret;\n \t}\n \n \tret = disk-\u003efops-\u003ereport_zones(disk, 0, UINT_MAX, \u0026rep_args);\n@@ -2607,8 +2607,6 @@ int blk_revalidate_disk_zones(struct gendisk *disk)\n \tif (ret)\n \t\tgoto free_args;\n \n-\tmutex_unlock(\u0026disk-\u003ezone_revalidate_mutex);\n-\n \treturn 0;\n \n free_args:\n@@ -2624,7 +2622,36 @@ int blk_revalidate_disk_zones(struct gendisk *disk)\n \t}\n \n \tpr_warn(\"%s: failed to revalidate zones\\n\", disk-\u003edisk_name);\n-unlock:\n+\n+\treturn ret;\n+}\n+\n+/**\n+ * blk_revalidate_disk_zones - (re)allocate and initialize zone write plugs\n+ * @disk:\tTarget disk\n+ *\n+ * Helper function for low-level device drivers to check, (re) allocate and\n+ * initialize resources used for managing zoned disks. This function should\n+ * normally be called by blk-mq based drivers when a zoned gendisk is probed\n+ * and when the zone configuration of the gendisk changes (e.g. after a format).\n+ * Before calling this function, the device driver must already have set the\n+ * device zone size (chunk_sector limit) and the max zone append limit.\n+ * BIO based drivers can also use this function as long as the device queue\n+ * can be safely frozen.\n+ */\n+int blk_revalidate_disk_zones(struct gendisk *disk)\n+{\n+\tint ret;\n+\n+\tif (WARN_ON_ONCE(!blk_queue_is_zoned(disk-\u003equeue)))\n+\t\treturn -EIO;\n+\n+\t/*\n+\t * Serialize calls to this function so that we can safely look at and\n+\t * eventually change the disk zone information.\n+\t */\n+\tmutex_lock(\u0026disk-\u003ezone_revalidate_mutex);\n+\tret = disk_revalidate_zones(disk, false);\n \tmutex_unlock(\u0026disk-\u003ezone_revalidate_mutex);\n \n \treturn ret;\n@@ -2710,3 +2737,370 @@ int queue_zone_wplugs_show(void *data, struct seq_file *m)\n }\n \n #endif\n+\n+/**\n+ * bdev_report_storage_elements - report the storage elements of a zoned\n+ *\t\t\t\t block device\n+ * @bdev:\tthe zoned block device to check\n+ * @elements:\tarray of storage element descriptors to fill\n+ * @nr_elements: maximum size of @elements\n+ *\n+ * Fill at most @nr_elements storage element descriptors in the array\n+ * @elements. The number of storage elements filled in the array is returned\n+ * using @nr_elements. If @elements is NULL, only @nr_elements is returned to\n+ * indicate the maximum number of storage elements of @bdev. This function may\n+ * sleep.\n+ *\n+ * Returns 0 on success and a negative error code on failure.\n+ */\n+int bdev_report_storage_elements(struct block_device *bdev,\n+\t\t\t\t struct blk_se *elements,\n+\t\t\t\t unsigned int *nr_elements)\n+{\n+\tstruct gendisk *disk = bdev-\u003ebd_disk;\n+\n+\tif (test_bit(GD_DEAD, \u0026disk-\u003estate))\n+\t\treturn -ENODEV;\n+\n+\tif (!blk_queue_is_zoned(disk-\u003equeue) || !disk-\u003enr_storage_elements)\n+\t\treturn -EOPNOTSUPP;\n+\n+\tif (!nr_elements)\n+\t\treturn -EINVAL;\n+\n+\tif (!elements) {\n+\t\t*nr_elements = disk-\u003enr_storage_elements;\n+\t\treturn 0;\n+\t}\n+\n+\t*nr_elements = min(*nr_elements, disk-\u003enr_storage_elements);\n+\tif (!*nr_elements)\n+\t\treturn -EINVAL;\n+\n+\treturn disk-\u003efops-\u003ese_ops-\u003ereport_elements(disk, elements,\n+\t\t\t\t\t\t nr_elements);\n+}\n+EXPORT_SYMBOL_GPL(bdev_report_storage_elements);\n+\n+static int blkdev_ioctl_report_storage_elements(struct block_device *bdev,\n+\t\t\t\t\t\tvoid __user *argp)\n+{\n+\tstruct gendisk *disk = bdev-\u003ebd_disk;\n+\tstruct blk_se_report *rep;\n+\tint ret = 0;\n+\n+\t/*\n+\t * Allocate the report header with an array of elements, assuming that\n+\t * we need the maximum number of storage elements. In practice, we may\n+\t * need less than that, but this potentially unnecessary preallocation\n+\t * simplifies the code.\n+\t */\n+\trep = kzalloc_flex(*rep, elements, disk-\u003enr_storage_elements);\n+\tif (!rep)\n+\t\treturn -ENOMEM;\n+\n+\tif (copy_from_user(rep, argp, sizeof(struct blk_se_report))) {\n+\t\tret = -EFAULT;\n+\t\tgoto free_rep;\n+\t}\n+\n+\tif (rep-\u003ereserved) {\n+\t\tret = -EINVAL;\n+\t\tgoto free_rep;\n+\t}\n+\n+\trep-\u003enr_reported_elements =\n+\t\tmin(rep-\u003enr_elements, disk-\u003enr_storage_elements);\n+\trep-\u003enr_elements = disk-\u003enr_storage_elements;\n+\n+\tif (rep-\u003enr_reported_elements) {\n+\t\tret = bdev_report_storage_elements(bdev, rep-\u003eelements,\n+\t\t\t\t\t\t \u0026rep-\u003enr_reported_elements);\n+\t\tif (ret)\n+\t\t\tgoto free_rep;\n+\t}\n+\n+\tif (copy_to_user(argp, rep,\n+\t\t\tstruct_size(rep, elements, rep-\u003enr_reported_elements)))\n+\t\tret = -EFAULT;\n+\n+free_rep:\n+\tkfree(rep);\n+\treturn ret;\n+}\n+\n+/**\n+ * bdev_remove_storage_element - Remove (depopulate) a storage element of a\n+ *\t\t\t\t zoned block device\n+ * @bdev:\tblock device to operate on\n+ * @element_id:\tID of the storage element to remove\n+ *\n+ * Remove (depopulate) the storage element identified by @element_id from the\n+ * block device @bdev. The caller is responsible for taking care of any\n+ * necessary dirty page and device write cache flush and invalidation of cached\n+ * data. This function will block until the storage element is removed or an\n+ * error occurs.\n+ *\n+ * Returns 0 on success and a negative error code on failure.\n+ */\n+int bdev_remove_storage_element(struct block_device *bdev,\n+\t\t\t\tunsigned int element_id)\n+{\n+\tstruct gendisk *disk = bdev-\u003ebd_disk;\n+\tunsigned int memflags;\n+\tint ret, err;\n+\n+\tif (test_bit(GD_DEAD, \u0026disk-\u003estate))\n+\t\treturn -ENODEV;\n+\n+\tif (!blk_queue_is_zoned(disk-\u003equeue) || !disk-\u003enr_storage_elements)\n+\t\treturn -EOPNOTSUPP;\n+\n+\t/* Zero is not a valid storage element ID. */\n+\tif (!element_id)\n+\t\treturn -EINVAL;\n+\n+\t/*\n+\t * Serialize against concurrent revalidation and concurrent storage\n+\t * element management operations so that we can see consistent zone\n+\t * condition changes.\n+\t */\n+\tmutex_lock(\u0026disk-\u003ezone_revalidate_mutex);\n+\n+\t/*\n+\t * Freeze and unfreeze the queue to flush any outstanding command.\n+\t * The caller is responsible for not queuing up more I/Os by higher\n+\t * level means.\n+\t */\n+\tmemflags = blk_mq_freeze_queue(disk-\u003equeue);\n+\tblk_mq_unfreeze_queue(disk-\u003equeue, memflags);\n+\n+\t/* Initiate the storage element removal and wait for its completion. */\n+\tret = disk-\u003efops-\u003ese_ops-\u003eremove_element(disk, element_id);\n+\n+\t/*\n+\t * Always revalidate zones, even in the case of an error as we do not\n+\t * know what condition the zones of the block device are in.\n+\t */\n+\terr = disk_revalidate_zones(disk, false);\n+\tif (err \u0026\u0026 !ret)\n+\t\tret = err;\n+\n+\tmutex_unlock(\u0026disk-\u003ezone_revalidate_mutex);\n+\n+\treturn ret;\n+}\n+EXPORT_SYMBOL_GPL(bdev_remove_storage_element);\n+\n+static int blkdev_ioctl_remove_storage_element(struct block_device *bdev,\n+\t\t\t\t\t blk_mode_t mode,\n+\t\t\t\t\t unsigned int __user *argp)\n+{\n+\tunsigned int element_id;\n+\tint ret;\n+\n+\t/*\n+\t * Note: we require an O_EXCL open here so that a user cannot trigger\n+\t * a storage element removal from user space under a mounted file system.\n+\t * An FS that wants to manage storage element can do so using the\n+\t * function bdev_remove_storage_element() from kernel context.\n+\t */\n+\tif (!(mode \u0026 BLK_OPEN_EXCL))\n+\t\treturn -EBADF;\n+\tif (!(mode \u0026 BLK_OPEN_WRITE))\n+\t\treturn -EBADF;\n+\tif (bdev_read_only(bdev))\n+\t\treturn -EPERM;\n+\n+\tif (get_user(element_id, argp))\n+\t\treturn -EFAULT;\n+\n+\t/*\n+\t * Sync the device dirty data, flush the volatile write cache, and\n+\t * invalidate all cached data so that reads do not return old data for\n+\t * zones that went offline.\n+\t * Since a user can only successfully write to sequential zones of a\n+\t * zoned devices using direct IOs, the sync should only handle dirty\n+\t * data from buffered writes to conventional zones, for devices that\n+\t * have such zones.\n+\t */\n+\tret = sync_blockdev(bdev);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\tinode_lock(bdev-\u003ebd_mapping-\u003ehost);\n+\tfilemap_invalidate_lock(bdev-\u003ebd_mapping);\n+\n+\tret = blkdev_issue_flush(bdev-\u003ebd_disk-\u003epart0);\n+\tif (ret)\n+\t\tgoto unlock;\n+\n+\tret = truncate_bdev_range(bdev, mode, 0, bdev_nr_bytes(bdev) - 1);\n+\tif (ret)\n+\t\tgoto unlock;\n+\n+\tret = bdev_remove_storage_element(bdev, element_id);\n+\n+unlock:\n+\tfilemap_invalidate_unlock(bdev-\u003ebd_mapping);\n+\tinode_unlock(bdev-\u003ebd_mapping-\u003ehost);\n+\n+\treturn ret;\n+}\n+\n+/**\n+ * bdev_restore_storage_elements - Restore all depopulated storage elements of\n+ *\t\t\t\t a zoned block device\n+ * @bdev:\tblock device to operate on\n+ *\n+ * Restore all storage elements of @bdev that have been depopulated. Not all\n+ * elements may be restored by this operation. This operation is destructive and\n+ * will result in all sequential zones to be transitioned to the empty condition.\n+ * The caller is responsible for taking care of any necessary invalidation of\n+ * cached data. This function will block until the storage elements are restored\n+ * or an error occurs.\n+ *\n+ * Returns 0 on success and a negative error code on failure.\n+ */\n+int bdev_restore_storage_elements(struct block_device *bdev)\n+{\n+\tstruct gendisk *disk = bdev-\u003ebd_disk;\n+\tunsigned int memflags;\n+\tint ret, err;\n+\n+\tif (test_bit(GD_DEAD, \u0026disk-\u003estate))\n+\t\treturn -ENODEV;\n+\n+\tif (!blk_queue_is_zoned(disk-\u003equeue) || !disk-\u003enr_storage_elements)\n+\t\treturn -EOPNOTSUPP;\n+\n+\t/*\n+\t * Serialize against concurrent revalidation and concurrent storage\n+\t * element management operations so that we can see consistent zone\n+\t * condition changes.\n+\t */\n+\tmutex_lock(\u0026disk-\u003ezone_revalidate_mutex);\n+\n+\t/*\n+\t * Freeze and unfreeze the queue to flush any outstanding commands.\n+\t * The caller is responsible for not queuing up more I/O by higher\n+\t * level means.\n+\t */\n+\tmemflags = blk_mq_freeze_queue(disk-\u003equeue);\n+\tblk_mq_unfreeze_queue(disk-\u003equeue, memflags);\n+\n+\t/*\n+\t * Initiate the storage elements restoration and wait for its\n+\t * completion.\n+\t */\n+\tret = disk-\u003efops-\u003ese_ops-\u003erestore_elements(disk);\n+\n+\t/*\n+\t * Always revalidate zones, even in the case of an error as we do not\n+\t * know what condition the zones of the block device are in. Note that\n+\t * since all sequential zones should have been transitioned to the empty\n+\t * condition, we can drop all zone write plugs. If an error occured and\n+\t * not all sequential zones are empty, the revalidation will reinstate\n+\t * the write plugs for these zones.\n+\t */\n+\terr = disk_revalidate_zones(disk, true);\n+\tif (err \u0026\u0026 !ret)\n+\t\tret = err;\n+\n+\tmutex_unlock(\u0026disk-\u003ezone_revalidate_mutex);\n+\n+\treturn ret;\n+}\n+EXPORT_SYMBOL_GPL(bdev_restore_storage_elements);\n+\n+static int blkdev_ioctl_restore_storage_elements(struct block_device *bdev,\n+\t\t\t\t\t\t blk_mode_t mode)\n+{\n+\tint ret;\n+\n+\t/*\n+\t * Note: because restoring storage elements is a destructive operation\n+\t * equivalent to reformatting the device, we require an O_EXCL open here\n+\t * so that a user cannot trigger with another active user (e.g. a\n+\t * mounted file system).\n+\t */\n+\tif (!(mode \u0026 BLK_OPEN_EXCL))\n+\t\treturn -EBADF;\n+\tif (!(mode \u0026 BLK_OPEN_WRITE))\n+\t\treturn -EBADF;\n+\tif (bdev_read_only(bdev))\n+\t\treturn -EPERM;\n+\n+\tinode_lock(bdev-\u003ebd_mapping-\u003ehost);\n+\tfilemap_invalidate_lock(bdev-\u003ebd_mapping);\n+\n+\t/*\n+\t * Storage element restoration is a destructive operation that will\n+\t * reset all zones. So invalidate all cached data that we may have so\n+\t * that old data cannot be read once the operation completes.\n+\t */\n+\tret = truncate_bdev_range(bdev, mode, 0, bdev_nr_bytes(bdev) - 1);\n+\tif (!ret)\n+\t\tret = bdev_restore_storage_elements(bdev);\n+\n+\tfilemap_invalidate_unlock(bdev-\u003ebd_mapping);\n+\tinode_unlock(bdev-\u003ebd_mapping-\u003ehost);\n+\n+\treturn ret;\n+}\n+\n+int disk_init_storage_element_mgmt(struct gendisk *disk)\n+{\n+\tint ret;\n+\n+\t/*\n+\t * Storage element operations are for zoned disks only and are\n+\t * optional...\n+\t */\n+\tif (!blk_queue_is_zoned(disk-\u003equeue) || !disk-\u003efops-\u003ese_ops)\n+\t\treturn 0;\n+\n+\t/* ...but if defined, all operation callbacks must be defined. */\n+\tif (!disk-\u003efops-\u003ese_ops-\u003ereport_elements ||\n+\t !disk-\u003efops-\u003ese_ops-\u003eremove_element ||\n+\t !disk-\u003efops-\u003ese_ops-\u003erestore_elements) {\n+\t\tpr_err(\"%s: invalid storage element operations\\n\",\n+\t\t disk-\u003edisk_name);\n+\t\treturn -EINVAL;\n+\t}\n+\n+\t/*\n+\t * Get the number of storage elements of the disk. If this fails,\n+\t * do not report an error as the failure may be because the zoned disk\n+\t * does not support storage element management.\n+\t */\n+\tret = disk-\u003efops-\u003ese_ops-\u003ereport_elements(disk, NULL,\n+\t\t\t\t\t\t \u0026disk-\u003enr_storage_elements);\n+\tif (ret)\n+\t\tdisk-\u003enr_storage_elements = 0;\n+\n+\treturn 0;\n+}\n+\n+int blk_ioctl_zone_storage_elements(struct block_device *bdev,\n+\t\t\t\t blk_mode_t mode, unsigned int cmd,\n+\t\t\t\t void __user *argp)\n+{\n+\tstruct gendisk *disk = bdev-\u003ebd_disk;\n+\n+\tif (!disk-\u003enr_storage_elements)\n+\t\treturn -ENOTTY;\n+\n+\tswitch (cmd) {\n+\tcase BLKREPORTSTORELEMS:\n+\t\treturn blkdev_ioctl_report_storage_elements(bdev, argp);\n+\tcase BLKREMOVESTORELEM:\n+\t\treturn blkdev_ioctl_remove_storage_element(bdev, mode, argp);\n+\tcase BLKRESTORESTORELEMS:\n+\t\treturn blkdev_ioctl_restore_storage_elements(bdev, mode);\n+\tdefault:\n+\t\tbreak;\n+\t}\n+\n+\treturn -ENOTTY;\n+}\ndiff --git a/block/blk.h b/block/blk.h\nindex 0e38e4b88cf544..44876b148237c5 100644\n--- a/block/blk.h\n+++ b/block/blk.h\n@@ -543,6 +543,7 @@ static inline void ioc_clear_queue(struct request_queue *q)\n #endif /* CONFIG_BLK_ICQ */\n \n #ifdef CONFIG_BLK_DEV_ZONED\n+int disk_init_storage_element_mgmt(struct gendisk *disk);\n void disk_init_zone_resources(struct gendisk *disk);\n void disk_release_zone_resources(struct gendisk *disk);\n static inline bool bio_zone_write_plugging(struct bio *bio)\n@@ -594,7 +595,14 @@ int blkdev_report_zones_ioctl(struct block_device *bdev, unsigned int cmd,\n int blkdev_zone_mgmt_ioctl(struct block_device *bdev, blk_mode_t mode,\n \t\tunsigned int cmd, unsigned long arg);\n bool bdev_zone_mgmt_allowed(struct block_device *bdev, sector_t sector);\n+int blk_ioctl_zone_storage_elements(struct block_device *bdev,\n+\t\t\t\t blk_mode_t mode, unsigned int cmd,\n+\t\t\t\t void __user *argp);\n #else /* CONFIG_BLK_DEV_ZONED */\n+static inline int disk_init_storage_element_mgmt(struct gendisk *disk)\n+{\n+\treturn 0;\n+}\n static inline void disk_init_zone_resources(struct gendisk *disk)\n {\n }\n@@ -641,6 +649,12 @@ static inline bool bdev_zone_mgmt_allowed(struct block_device *bdev,\n {\n \treturn false;\n }\n+static inline int blk_ioctl_zone_storage_elements(struct block_device *bdev,\n+\t\t\t\t\tblk_mode_t mode, unsigned int cmd,\n+\t\t\t\t\tvoid __user *argp)\n+{\n+\treturn -ENOTTY;\n+}\n #endif /* CONFIG_BLK_DEV_ZONED */\n \n struct block_device *bdev_alloc(struct gendisk *disk, u8 partno);\ndiff --git a/block/genhd.c b/block/genhd.c\nindex 69204d44adae11..251a5b07baff9c 100644\n--- a/block/genhd.c\n+++ b/block/genhd.c\n@@ -435,6 +435,10 @@ static int __add_disk(struct device *parent, struct gendisk *disk,\n \tif (WARN_ON_ONCE(bdev_nr_sectors(disk-\u003epart0) \u003e BLK_DEV_MAX_SECTORS))\n \t\treturn -EINVAL;\n \n+\tret = disk_init_storage_element_mgmt(disk);\n+\tif (ret)\n+\t\treturn ret;\n+\n \tif (queue_is_mq(disk-\u003equeue)) {\n \t\t/*\n \t\t * -\u003esubmit_bio and -\u003epoll_bio are bypassed for blk-mq drivers.\ndiff --git a/block/ioctl.c b/block/ioctl.c\nindex fb2f4b4d2505ed..0f043363048ca6 100644\n--- a/block/ioctl.c\n+++ b/block/ioctl.c\n@@ -679,6 +679,10 @@ static int blkdev_common_ioctl(struct block_device *bdev, blk_mode_t mode,\n \t\treturn put_uint(argp, bdev_zone_sectors(bdev));\n \tcase BLKGETNRZONES:\n \t\treturn put_uint(argp, bdev_nr_zones(bdev));\n+\tcase BLKREPORTSTORELEMS:\n+\tcase BLKREMOVESTORELEM:\n+\tcase BLKRESTORESTORELEMS:\n+\t\treturn blk_ioctl_zone_storage_elements(bdev, mode, cmd, argp);\n \tcase BLKROGET:\n \t\treturn put_int(argp, bdev_read_only(bdev) != 0);\n \tcase BLKSSZGET: /* get block device logical block size */\ndiff --git a/drivers/block/zloop.c b/drivers/block/zloop.c\nindex 96666742523bb7..c4a0a845a0963d 100644\n--- a/drivers/block/zloop.c\n+++ b/drivers/block/zloop.c\n@@ -18,6 +18,7 @@\n #include \u003clinux/parser.h\u003e\n #include \u003clinux/seq_file.h\u003e\n #include \u003clinux/xattr.h\u003e\n+#include \u003clinux/delay.h\u003e\n \n /*\n * Options for adding (and removing) a device.\n@@ -37,6 +38,8 @@ enum {\n \tZLOOP_OPT_ORDERED_ZONE_APPEND\t= (1 \u003c\u003c 10),\n \tZLOOP_OPT_DISCARD_WRITE_CACHE\t= (1 \u003c\u003c 11),\n \tZLOOP_OPT_MAX_OPEN_ZONES\t= (1 \u003c\u003c 12),\n+\tZLOOP_OPT_STOR_ELEMENTS\t\t= (1 \u003c\u003c 13),\n+\tZLOOP_OPT_ELEMENT_ID\t\t= (1 \u003c\u003c 14),\n };\n \n static const match_table_t zloop_opt_tokens = {\n@@ -51,11 +54,23 @@ static const match_table_t zloop_opt_tokens = {\n \t{ ZLOOP_OPT_BUFFERED_IO,\t\"buffered_io\"\t\t},\n \t{ ZLOOP_OPT_ZONE_APPEND,\t\"zone_append=%u\"\t},\n \t{ ZLOOP_OPT_ORDERED_ZONE_APPEND, \"ordered_zone_append\"\t},\n-\t{ ZLOOP_OPT_DISCARD_WRITE_CACHE, \"discard_write_cache\" },\n+\t{ ZLOOP_OPT_DISCARD_WRITE_CACHE, \"discard_write_cache\"\t},\n \t{ ZLOOP_OPT_MAX_OPEN_ZONES,\t\"max_open_zones=%u\"\t},\n+\t{ ZLOOP_OPT_STOR_ELEMENTS,\t\"stor_elements=%u\"\t},\n+\t{ ZLOOP_OPT_ELEMENT_ID,\t\t\"element_id=%u\"\t\t},\n \t{ ZLOOP_OPT_ERR,\t\tNULL\t\t\t}\n };\n \n+/* Storage elements emulation types. */\n+enum zloop_stor_elements {\n+\t/* No emulation. */\n+\tZLOOP_STOR_ELEMENTS_NONE,\n+\t/* Emulate read+write storage elements. */\n+\tZLOOP_STOR_ELEMENTS_RDWR,\n+\t/* Emulate pairs of associated read and write storage elements. */\n+\tZLOOP_STOR_ELEMENTS_PAIRS,\n+};\n+\n /* Default values for the \"add\" operation. */\n #define ZLOOP_DEF_ID\t\t\t-1\n #define ZLOOP_DEF_ZONE_SIZE\t\t((256ULL * SZ_1M) \u003e\u003e SECTOR_SHIFT)\n@@ -68,6 +83,8 @@ static const match_table_t zloop_opt_tokens = {\n #define ZLOOP_DEF_BUFFERED_IO\t\tfalse\n #define ZLOOP_DEF_ZONE_APPEND\t\ttrue\n #define ZLOOP_DEF_ORDERED_ZONE_APPEND\tfalse\n+#define ZLOOP_DEF_STOR_ELEMENTS\t\tZLOOP_STOR_ELEMENTS_NONE\n+#define ZLOOP_DEF_ELEMENT_ID\t\t0\n \n /* Arbitrary limit on the zone size (16GB). */\n #define ZLOOP_MAX_ZONE_SIZE_MB\t\t16384\n@@ -87,6 +104,8 @@ struct zloop_options {\n \tbool\t\t\tzone_append;\n \tbool\t\t\tordered_zone_append;\n \tbool\t\t\tdiscard_write_cache;\n+\tenum zloop_stor_elements stor_elements;\n+\tunsigned int\t\telement_id;\n };\n \n /*\n@@ -117,6 +136,8 @@ struct zloop_zone {\n \tenum blk_zone_cond\tcond;\n \tsector_t\t\tstart;\n \tsector_t\t\twp;\n+\tunsigned int\t\twr_se_id;\n+\tunsigned int\t\trd_se_id;\n \n \tgfp_t\t\t\told_gfp_mask;\n };\n@@ -133,6 +154,7 @@ struct zloop_device {\n \tbool\t\t\tzone_append;\n \tbool\t\t\tordered_zone_append;\n \tbool\t\t\tdiscard_write_cache;\n+\tenum zloop_stor_elements stor_elements;\n \n \tconst char\t\t*base_dir;\n \tstruct file\t\t*data_dir;\n@@ -150,6 +172,14 @@ struct zloop_device {\n \tstruct list_head\topen_zones_lru_list;\n \tunsigned int\t\tnr_open_zones;\n \n+\t/* For storage elements emulation. */\n+\tstruct mutex\t\tstor_elements_lock;\n+\tunsigned int\t\tnr_elements;\n+\tunsigned int\t\tnr_removed_elements;\n+\tunsigned int\t\tremove_element_id;\n+\tbool\t\t\trestore_in_progress;\n+\tstruct blk_se\t\t*elements;\n+\n \tstruct zloop_zone\tzones[] __counted_by(nr_zones);\n };\n \n@@ -289,22 +319,64 @@ static bool zloop_do_open_zone(struct zloop_device *zlo,\n \t}\n }\n \n-static void zloop_mark_full(struct zloop_device *zlo, struct zloop_zone *zone)\n+static void zloop_set_zone_cond(struct zloop_device *zlo,\n+\t\t\t\tstruct zloop_zone *zone,\n+\t\t\t\tenum blk_zone_cond cond)\n {\n \tlockdep_assert_held(\u0026zone-\u003ewp_lock);\n \n \tzloop_lru_remove_open_zone(zlo, zone);\n-\tzone-\u003econd = BLK_ZONE_COND_FULL;\n-\tzone-\u003ewp = ULLONG_MAX;\n+\tzone-\u003econd = cond;\n+\tif (cond == BLK_ZONE_COND_EMPTY)\n+\t\tzone-\u003ewp = zone-\u003estart;\n+\telse\n+\t\tzone-\u003ewp = ULLONG_MAX;\n+}\n+\n+static inline void zloop_set_zone_full(struct zloop_device *zlo,\n+\t\t\t\t struct zloop_zone *zone)\n+{\n+\tzloop_set_zone_cond(zlo, zone, BLK_ZONE_COND_FULL);\n+}\n+\n+static inline void zloop_set_zone_empty(struct zloop_device *zlo,\n+\t\t\t\t\tstruct zloop_zone *zone)\n+{\n+\tzloop_set_zone_cond(zlo, zone, BLK_ZONE_COND_EMPTY);\n }\n \n-static void zloop_mark_empty(struct zloop_device *zlo, struct zloop_zone *zone)\n+static bool zloop_zone_element_is_ok(struct zloop_device *zlo,\n+\t\t\t\t struct zloop_zone *zone, bool for_write)\n {\n+\tstruct blk_se *se;\n+\n \tlockdep_assert_held(\u0026zone-\u003ewp_lock);\n \n-\tzloop_lru_remove_open_zone(zlo, zone);\n-\tzone-\u003econd = BLK_ZONE_COND_EMPTY;\n-\tzone-\u003ewp = zone-\u003estart;\n+\tif (!zlo-\u003enr_elements)\n+\t\treturn true;\n+\n+\tif (for_write)\n+\t\tse = \u0026zlo-\u003eelements[zone-\u003ewr_se_id - 1];\n+\telse\n+\t\tse = \u0026zlo-\u003eelements[zone-\u003erd_se_id - 1];\n+\n+\treturn READ_ONCE(se-\u003estatus) == BLK_SE_STS_OK;\n+}\n+\n+static bool zloop_zone_is_ok(struct zloop_device *zlo, struct zloop_zone *zone)\n+{\n+\tbool ret;\n+\n+\tspin_lock(\u0026zone-\u003ewp_lock);\n+\n+\tret = zloop_zone_element_is_ok(zlo, zone, true);\n+\tif (ret)\n+\t\tret = zone-\u003econd != BLK_ZONE_COND_OFFLINE \u0026\u0026\n+\t\t\tzone-\u003econd != BLK_ZONE_COND_READONLY;\n+\n+\tspin_unlock(\u0026zone-\u003ewp_lock);\n+\n+\treturn ret;\n }\n \n static int zloop_update_seq_zone(struct zloop_device *zlo, unsigned int zone_no)\n@@ -316,6 +388,15 @@ static int zloop_update_seq_zone(struct zloop_device *zlo, unsigned int zone_no)\n \n \tlockdep_assert_held(\u0026zone-\u003elock);\n \n+\t/*\n+\t * If the zone is served by an element that is not in a good state, fail\n+\t * the operation.\n+\t */\n+\tif (!zloop_zone_is_ok(zlo, zone)) {\n+\t\tset_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags);\n+\t\treturn -EIO;\n+\t}\n+\n \tret = vfs_getattr(\u0026zone-\u003efile-\u003ef_path, \u0026stat, STATX_SIZE, 0);\n \tif (ret \u003c 0) {\n \t\tpr_err(\"%pg: Failed to get zone %u file stat (err=%d)\\n\",\n@@ -340,9 +421,9 @@ static int zloop_update_seq_zone(struct zloop_device *zlo, unsigned int zone_no)\n \n \tspin_lock(\u0026zone-\u003ewp_lock);\n \tif (!file_sectors) {\n-\t\tzloop_mark_empty(zlo, zone);\n+\t\tzloop_set_zone_empty(zlo, zone);\n \t} else if (file_sectors == zlo-\u003ezone_capacity) {\n-\t\tzloop_mark_full(zlo, zone);\n+\t\tzloop_set_zone_full(zlo, zone);\n \t} else {\n \t\tif (zone-\u003econd != BLK_ZONE_COND_IMP_OPEN \u0026\u0026\n \t\t zone-\u003econd != BLK_ZONE_COND_EXP_OPEN)\n@@ -364,6 +445,11 @@ static int zloop_open_zone(struct zloop_device *zlo, unsigned int zone_no)\n \n \tmutex_lock(\u0026zone-\u003elock);\n \n+\tif (!zloop_zone_is_ok(zlo, zone)) {\n+\t\tret = -EIO;\n+\t\tgoto unlock;\n+\t}\n+\n \tif (test_and_clear_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags)) {\n \t\tret = zloop_update_seq_zone(zlo, zone_no);\n \t\tif (ret)\n@@ -389,6 +475,11 @@ static int zloop_close_zone(struct zloop_device *zlo, unsigned int zone_no)\n \n \tmutex_lock(\u0026zone-\u003elock);\n \n+\tif (!zloop_zone_is_ok(zlo, zone)) {\n+\t\tret = -EIO;\n+\t\tgoto unlock;\n+\t}\n+\n \tif (test_and_clear_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags)) {\n \t\tret = zloop_update_seq_zone(zlo, zone_no);\n \t\tif (ret)\n@@ -421,7 +512,22 @@ static int zloop_close_zone(struct zloop_device *zlo, unsigned int zone_no)\n \treturn ret;\n }\n \n-static int zloop_reset_zone(struct zloop_device *zlo, unsigned int zone_no)\n+static int zloop_do_reset_zone(struct zloop_device *zlo,\n+\t\t\t struct zloop_zone *zone)\n+{\n+\tif (vfs_truncate(\u0026zone-\u003efile-\u003ef_path, 0))\n+\t\treturn -EIO;\n+\n+\tspin_lock(\u0026zone-\u003ewp_lock);\n+\tzloop_set_zone_empty(zlo, zone);\n+\tclear_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags);\n+\tspin_unlock(\u0026zone-\u003ewp_lock);\n+\n+\treturn 0;\n+}\n+\n+static int zloop_reset_zone(struct zloop_device *zlo, unsigned int zone_no,\n+\t\t\t bool all_zones)\n {\n \tstruct zloop_zone *zone = \u0026zlo-\u003ezones[zone_no];\n \tint ret = 0;\n@@ -431,20 +537,19 @@ static int zloop_reset_zone(struct zloop_device *zlo, unsigned int zone_no)\n \n \tmutex_lock(\u0026zone-\u003elock);\n \n+\tif (!zloop_zone_is_ok(zlo, zone)) {\n+\t\tif (!all_zones)\n+\t\t\tret = -EIO;\n+\t\tgoto unlock;\n+\t}\n+\n \tif (!test_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags) \u0026\u0026\n \t zone-\u003econd == BLK_ZONE_COND_EMPTY)\n \t\tgoto unlock;\n \n-\tif (vfs_truncate(\u0026zone-\u003efile-\u003ef_path, 0)) {\n+\tret = zloop_do_reset_zone(zlo, zone);\n+\tif (ret)\n \t\tset_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags);\n-\t\tret = -EIO;\n-\t\tgoto unlock;\n-\t}\n-\n-\tspin_lock(\u0026zone-\u003ewp_lock);\n-\tzloop_mark_empty(zlo, zone);\n-\tclear_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags);\n-\tspin_unlock(\u0026zone-\u003ewp_lock);\n \n unlock:\n \tmutex_unlock(\u0026zone-\u003elock);\n@@ -458,7 +563,7 @@ static int zloop_reset_all_zones(struct zloop_device *zlo)\n \tint ret;\n \n \tfor (i = zlo-\u003enr_conv_zones; i \u003c zlo-\u003enr_zones; i++) {\n-\t\tret = zloop_reset_zone(zlo, i);\n+\t\tret = zloop_reset_zone(zlo, i, true);\n \t\tif (ret)\n \t\t\treturn ret;\n \t}\n@@ -476,6 +581,11 @@ static int zloop_finish_zone(struct zloop_device *zlo, unsigned int zone_no)\n \n \tmutex_lock(\u0026zone-\u003elock);\n \n+\tif (!zloop_zone_is_ok(zlo, zone)) {\n+\t\tret = -EIO;\n+\t\tgoto unlock;\n+\t}\n+\n \tif (!test_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags) \u0026\u0026\n \t zone-\u003econd == BLK_ZONE_COND_FULL)\n \t\tgoto unlock;\n@@ -488,7 +598,7 @@ static int zloop_finish_zone(struct zloop_device *zlo, unsigned int zone_no)\n \t}\n \n \tspin_lock(\u0026zone-\u003ewp_lock);\n-\tzloop_mark_full(zlo, zone);\n+\tzloop_set_zone_full(zlo, zone);\n \tclear_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags);\n \tspin_unlock(\u0026zone-\u003ewp_lock);\n \n@@ -632,7 +742,7 @@ static int zloop_seq_write_prep(struct zloop_cmd *cmd)\n \tif (!is_append || !zlo-\u003eordered_zone_append) {\n \t\tzone-\u003ewp += nr_sectors;\n \t\tif (zone-\u003ewp == zone_end)\n-\t\t\tzloop_mark_full(zlo, zone);\n+\t\t\tzloop_set_zone_full(zlo, zone);\n \t}\n out_unlock:\n \tspin_unlock(\u0026zone-\u003ewp_lock);\n@@ -774,7 +884,7 @@ static void zloop_handle_cmd(struct zloop_cmd *cmd)\n \t\tcmd-\u003eret = zloop_flush(zlo);\n \t\tbreak;\n \tcase REQ_OP_ZONE_RESET:\n-\t\tcmd-\u003eret = zloop_reset_zone(zlo, rq_zone_no(rq));\n+\t\tcmd-\u003eret = zloop_reset_zone(zlo, rq_zone_no(rq), false);\n \t\tbreak;\n \tcase REQ_OP_ZONE_RESET_ALL:\n \t\tcmd-\u003eret = zloop_reset_all_zones(zlo);\n@@ -885,30 +995,65 @@ static void zloop_complete_rq(struct request *rq)\n \tblk_mq_end_request(rq, sts);\n }\n \n-static bool zloop_set_zone_append_sector(struct request *rq)\n+static bool zloop_set_zone_append_sector(struct zloop_device *zlo,\n+\t\t\t\t\t struct zloop_zone *zone,\n+\t\t\t\t\t struct request *rq)\n {\n-\tstruct zloop_device *zlo = rq-\u003eq-\u003equeuedata;\n-\tunsigned int zone_no = rq_zone_no(rq);\n-\tstruct zloop_zone *zone = \u0026zlo-\u003ezones[zone_no];\n \tsector_t zone_end = zone-\u003estart + zlo-\u003ezone_capacity;\n \tsector_t nr_sectors = blk_rq_sectors(rq);\n \n-\tspin_lock(\u0026zone-\u003ewp_lock);\n-\n \tif (zone-\u003econd == BLK_ZONE_COND_FULL ||\n-\t zone-\u003ewp + nr_sectors \u003e zone_end) {\n-\t\tspin_unlock(\u0026zone-\u003ewp_lock);\n+\t zone-\u003ewp + nr_sectors \u003e zone_end)\n \t\treturn false;\n-\t}\n \n \trq-\u003e__sector = zone-\u003ewp;\n \tzone-\u003ewp += blk_rq_sectors(rq);\n \tif (zone-\u003ewp \u003e= zone_end)\n-\t\tzloop_mark_full(zlo, zone);\n+\t\tzloop_set_zone_full(zlo, zone);\n+\n+\treturn true;\n+}\n+\n+static bool zloop_prep_rq(struct zloop_device *zlo, struct request *rq)\n+{\n+\tstruct zloop_zone *zone = \u0026zlo-\u003ezones[rq_zone_no(rq)];\n+\tbool is_write = op_is_write(req_op(rq));\n+\tbool ret = true;\n+\n+\tif (!zlo-\u003enr_elements \u0026\u0026\n+\t (!zlo-\u003eordered_zone_append || req_op(rq) != REQ_OP_ZONE_APPEND))\n+\t\treturn true;\n+\n+\tspin_lock(\u0026zone-\u003ewp_lock);\n \n+\t/*\n+\t * If storage element emulation is enabled, check the health state of\n+\t * the storage element serving the zone and the zone condition: do not\n+\t * allow any operation to zones served by an unusable storage element,\n+\t * operations to offline zones, and allow only read operations to\n+\t * read-only zones.\n+\t */\n+\tif (zlo-\u003enr_elements) {\n+\t\tif (!zloop_zone_element_is_ok(zlo, zone, is_write) ||\n+\t\t zone-\u003econd == BLK_ZONE_COND_OFFLINE ||\n+\t\t (zone-\u003econd == BLK_ZONE_COND_READONLY \u0026\u0026 is_write)) {\n+\t\t\tret = false;\n+\t\t\tgoto unlock;\n+\t\t}\n+\t}\n+\n+\t/*\n+\t * If we need to strongly order zone append operations, set the request\n+\t * sector to the zone write pointer location now instead of when the\n+\t * command work runs.\n+\t */\n+\tif (zlo-\u003eordered_zone_append \u0026\u0026 req_op(rq) == REQ_OP_ZONE_APPEND)\n+\t\tret = zloop_set_zone_append_sector(zlo, zone, rq);\n+\n+unlock:\n \tspin_unlock(\u0026zone-\u003ewp_lock);\n \n-\treturn true;\n+\treturn ret;\n }\n \n static blk_status_t zloop_queue_rq(struct blk_mq_hw_ctx *hctx,\n@@ -924,13 +1069,27 @@ static blk_status_t zloop_queue_rq(struct blk_mq_hw_ctx *hctx,\n \t}\n \n \t/*\n-\t * If we need to strongly order zone append operations, set the request\n-\t * sector to the zone write pointer location now instead of when the\n-\t * command work runs.\n+\t * Do not allow commands while the restoration of storage elements is\n+\t * in progress.\n+\t */\n+\tif (READ_ONCE(zlo-\u003erestore_in_progress))\n+\t\treturn BLK_STS_IOERR;\n+\n+\t/*\n+\t * Check if we can accept data access commands and prepare zone append.\n+\t * If storage element emulation is enabled, this will check if the data\n+\t * access can be allowed based on the health state of the storage\n+\t * element assigned to the zone being accessed.\n \t */\n-\tif (zlo-\u003eordered_zone_append \u0026\u0026 req_op(rq) == REQ_OP_ZONE_APPEND) {\n-\t\tif (!zloop_set_zone_append_sector(rq))\n+\tswitch (req_op(rq)) {\n+\tcase REQ_OP_READ:\n+\tcase REQ_OP_WRITE:\n+\tcase REQ_OP_ZONE_APPEND:\n+\t\tif (!zloop_prep_rq(zlo, rq))\n \t\t\treturn BLK_STS_IOERR;\n+\t\tbreak;\n+\tdefault:\n+\t\tbreak;\n \t}\n \n \tblk_mq_start_request(rq);\n@@ -992,8 +1151,8 @@ static int zloop_report_zones(struct gendisk *disk, sector_t sector,\n \t\tblkz.len = zlo-\u003ezone_size;\n \t\tspin_lock(\u0026zone-\u003ewp_lock);\n \t\tblkz.wp = zone-\u003ewp;\n-\t\tspin_unlock(\u0026zone-\u003ewp_lock);\n \t\tblkz.cond = zone-\u003econd;\n+\t\tspin_unlock(\u0026zone-\u003ewp_lock);\n \t\tif (test_bit(ZLOOP_ZONE_CONV, \u0026zone-\u003eflags)) {\n \t\t\tblkz.type = BLK_ZONE_TYPE_CONVENTIONAL;\n \t\t\tblkz.capacity = zlo-\u003ezone_size;\n@@ -1012,6 +1171,356 @@ static int zloop_report_zones(struct gendisk *disk, sector_t sector,\n \treturn nr_zones;\n }\n \n+static int zloop_report_elements(struct gendisk *disk,\n+\t\t\t\t struct blk_se *elements,\n+\t\t\t\t unsigned int *nr_elements)\n+{\n+\tstruct zloop_device *zlo = disk-\u003eprivate_data;\n+\tstruct blk_se *se;\n+\tunsigned int i;\n+\n+\tif (test_bit(GD_DEAD, \u0026disk-\u003estate) ||\n+\t data_race(READ_ONCE(zlo-\u003estate)) == Zlo_deleting)\n+\t\treturn -ENODEV;\n+\n+\tif (zlo-\u003estor_elements == ZLOOP_STOR_ELEMENTS_NONE)\n+\t\treturn -EOPNOTSUPP;\n+\n+\tmutex_lock(\u0026zlo-\u003estor_elements_lock);\n+\n+\tif (!elements) {\n+\t\t*nr_elements = zlo-\u003enr_elements;\n+\t\tgoto unlock;\n+\t}\n+\n+\t*nr_elements = min(*nr_elements, zlo-\u003enr_elements);\n+\tfor (i = 0, se = zlo-\u003eelements; i \u003c *nr_elements; i++, se++) {\n+\t\tswitch (READ_ONCE(se-\u003estatus)) {\n+\t\tcase BLK_SE_STS_REMOVED:\n+\t\tcase BLK_SE_STS_RESTORE_ERROR:\n+\t\t\tse-\u003erestore_allowed = 1;\n+\t\t\tbreak;\n+\t\tdefault:\n+\t\t\tse-\u003erestore_allowed = 0;\n+\t\t}\n+\t\tmemcpy(\u0026elements[i], se, sizeof(struct blk_se));\n+\t}\n+\n+unlock:\n+\tmutex_unlock(\u0026zlo-\u003estor_elements_lock);\n+\n+\treturn 0;\n+}\n+\n+static int zloop_degrade_element(struct zloop_device *zlo,\n+\t\t\t\t unsigned int element_id)\n+{\n+\tstruct blk_se *se;\n+\tint ret = 0;\n+\n+\tif (zlo-\u003estor_elements == ZLOOP_STOR_ELEMENTS_NONE)\n+\t\treturn -EOPNOTSUPP;\n+\n+\tif (!element_id || element_id \u003e zlo-\u003enr_elements)\n+\t\treturn -EINVAL;\n+\n+\tmutex_lock(\u0026zlo-\u003estor_elements_lock);\n+\n+\tse = \u0026zlo-\u003eelements[element_id - 1];\n+\tif (READ_ONCE(se-\u003estatus) == BLK_SE_STS_OK)\n+\t\tWRITE_ONCE(se-\u003estatus, BLK_SE_STS_DEGRADED);\n+\telse\n+\t\tret = -EINVAL;\n+\n+\tmutex_unlock(\u0026zlo-\u003estor_elements_lock);\n+\n+\treturn ret;\n+}\n+\n+static int zloop_execute_remove_element(struct zloop_device *zlo)\n+{\n+\tstruct gendisk *disk = zlo-\u003edisk;\n+\tstruct blk_se *se, *paired_se = NULL;\n+\tenum blk_zone_cond cond;\n+\tstruct zloop_zone *zone;\n+\tunsigned int memflags, i;\n+\tint ret = 0;\n+\n+\t/* Wait for all IOs to complete. */\n+\tmemflags = blk_mq_freeze_queue(disk-\u003equeue);\n+\tblk_mq_unfreeze_queue(disk-\u003equeue, memflags);\n+\n+\t/*\n+\t * If the element to remove is a read element, zones must go offline and\n+\t * the associated write element is also removed.\n+\t */\n+\tse = \u0026zlo-\u003eelements[zlo-\u003eremove_element_id - 1];\n+\tswitch (se-\u003etype) {\n+\tcase BLK_SE_TYPE_RDWR:\n+\t\tcond = BLK_ZONE_COND_OFFLINE;\n+\t\tbreak;\n+\tcase BLK_SE_TYPE_READ:\n+\t\tcond = BLK_ZONE_COND_OFFLINE;\n+\t\tpaired_se = \u0026zlo-\u003eelements[se-\u003epaired_id - 1];\n+\t\tbreak;\n+\tcase BLK_SE_TYPE_WRITE:\n+\t\tcond = BLK_ZONE_COND_READONLY;\n+\t\tbreak;\n+\tdefault:\n+\t\tWARN_ON_ONCE(1);\n+\t\treturn -EINVAL;\n+\t}\n+\n+\t/*\n+\t * Change the condition of the zones owned by the (pair of) elements\n+\t * being removed and mark the elements removed.\n+\t */\n+\tfor (i = 0, zone = zlo-\u003ezones; i \u003c zlo-\u003enr_zones; i++, zone++) {\n+\t\tif (zone-\u003ewr_se_id != se-\u003eid \u0026\u0026 zone-\u003erd_se_id != se-\u003eid)\n+\t\t\tcontinue;\n+\t\tmutex_lock(\u0026zone-\u003elock);\n+\t\tspin_lock(\u0026zone-\u003ewp_lock);\n+\t\tzloop_set_zone_cond(zlo, zone, cond);\n+\t\tclear_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags);\n+\t\tspin_unlock(\u0026zone-\u003ewp_lock);\n+\t\tmutex_unlock(\u0026zone-\u003elock);\n+\t}\n+\n+\tWRITE_ONCE(se-\u003estatus, BLK_SE_STS_REMOVED);\n+\tzlo-\u003enr_removed_elements++;\n+\tif (paired_se \u0026\u0026 READ_ONCE(paired_se-\u003estatus) != BLK_SE_STS_REMOVED) {\n+\t\tWRITE_ONCE(paired_se-\u003estatus, BLK_SE_STS_REMOVED);\n+\t\tzlo-\u003enr_removed_elements++;\n+\t}\n+\n+\treturn ret;\n+}\n+\n+static int zloop_remove_element(struct gendisk *disk, unsigned int element_id)\n+{\n+\tstruct zloop_device *zlo = disk-\u003eprivate_data;\n+\tstruct blk_se *se, *paired_se = NULL;\n+\tunsigned int nr_remove = 1;\n+\tint ret = 0;\n+\n+\tif (test_bit(GD_DEAD, \u0026disk-\u003estate) ||\n+\t data_race(READ_ONCE(zlo-\u003estate)) == Zlo_deleting)\n+\t\treturn -ENODEV;\n+\n+\tif (zlo-\u003estor_elements == ZLOOP_STOR_ELEMENTS_NONE)\n+\t\treturn -EOPNOTSUPP;\n+\n+\tif (!element_id || element_id \u003e zlo-\u003enr_elements)\n+\t\treturn -EINVAL;\n+\n+\tmutex_lock(\u0026zlo-\u003estor_elements_lock);\n+\n+\t/* If a remove or restore operation is already in progress, bail out. */\n+\tif (zlo-\u003eremove_element_id || READ_ONCE(zlo-\u003erestore_in_progress)) {\n+\t\tret = -EBUSY;\n+\t\tgoto unlock;\n+\t}\n+\n+\t/*\n+\t * Get the element to remove. If it is already removed, we have nothing\n+\t * to do.\n+\t */\n+\tse = \u0026zlo-\u003eelements[element_id - 1];\n+\tif (READ_ONCE(se-\u003estatus) == BLK_SE_STS_REMOVED)\n+\t\tgoto unlock;\n+\n+\t/*\n+\t * If the element to remove is a read element, the associated write\n+\t * element must also be removed.\n+\t */\n+\tif (se-\u003etype == BLK_SE_TYPE_READ) {\n+\t\tpaired_se = \u0026zlo-\u003eelements[se-\u003epaired_id - 1];\n+\t\tif (READ_ONCE(paired_se-\u003estatus) != BLK_SE_STS_REMOVED)\n+\t\t\tnr_remove = 2;\n+\t}\n+\n+\t/*\n+\t * Make sure we do not allow removing all storage elements as that does\n+\t * not make any sense. This is consistent with the device advertized\n+\t * limit of SCSI and ATA devices supporting the storage element\n+\t * depopulation feature.\n+\t */\n+\tif (zlo-\u003enr_removed_elements + nr_remove \u003e= zlo-\u003enr_elements) {\n+\t\tret = -EINVAL;\n+\t\tgoto unlock;\n+\t}\n+\n+\t/*\n+\t * Change the elements to be removed to the remove in progress state so\n+\t * that concurrent report elements can see the operation on-going.\n+\t */\n+\tWRITE_ONCE(se-\u003estatus, BLK_SE_STS_REMOVE_IN_PROGRESS);\n+\tif (paired_se \u0026\u0026 READ_ONCE(paired_se-\u003estatus) != BLK_SE_STS_REMOVED)\n+\t\tWRITE_ONCE(paired_se-\u003estatus, BLK_SE_STS_REMOVE_IN_PROGRESS);\n+\tzlo-\u003eremove_element_id = element_id;\n+\n+\tmutex_unlock(\u0026zlo-\u003estor_elements_lock);\n+\n+\t/*\n+\t * Wait a little so that this looks like a real device taking time to\n+\t * operate.\n+\t */\n+\tmsleep(2000);\n+\n+\tmutex_lock(\u0026zlo-\u003estor_elements_lock);\n+\n+\t/* Remove the element(s). */\n+\tret = zloop_execute_remove_element(zlo);\n+\tzlo-\u003eremove_element_id = 0;\n+\n+unlock:\n+\tmutex_unlock(\u0026zlo-\u003estor_elements_lock);\n+\n+\treturn ret;\n+}\n+\n+static int zloop_restore_zone(struct zloop_device *zlo,\n+\t\t\t struct zloop_zone *zone)\n+{\n+\tint ret = 0;\n+\n+\tmutex_lock(\u0026zone-\u003elock);\n+\n+\tif (!test_bit(ZLOOP_ZONE_CONV, \u0026zone-\u003eflags)) {\n+\t\tret = vfs_truncate(\u0026zone-\u003efile-\u003ef_path, 0);\n+\t\tif (ret) {\n+\t\t\tset_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags);\n+\t\t\tret = -EIO;\n+\t\t\tgoto unlock;\n+\t\t}\n+\t}\n+\n+\tspin_lock(\u0026zone-\u003ewp_lock);\n+\tif (test_bit(ZLOOP_ZONE_CONV, \u0026zone-\u003eflags)) {\n+\t\tzloop_set_zone_cond(zlo, zone, BLK_ZONE_COND_NOT_WP);\n+\t} else {\n+\t\tzloop_set_zone_empty(zlo, zone);\n+\t\tclear_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags);\n+\t}\n+\tspin_unlock(\u0026zone-\u003ewp_lock);\n+\n+unlock:\n+\tmutex_unlock(\u0026zone-\u003elock);\n+\n+\treturn ret;\n+}\n+\n+static int zloop_execute_restore_elements(struct zloop_device *zlo)\n+{\n+\tstruct gendisk *disk = zlo-\u003edisk;\n+\tstruct zloop_zone *zone;\n+\tstruct blk_se *se;\n+\tenum blk_se_status sts = BLK_SE_STS_OK;\n+\tunsigned int memflags, i;\n+\tint ret = 0;\n+\n+\t/* Wait for all IOs to complete. */\n+\tmemflags = blk_mq_freeze_queue(disk-\u003equeue);\n+\tblk_mq_unfreeze_queue(disk-\u003equeue, memflags);\n+\n+\t/*\n+\t * Truncate all files for sequential zones and restore the condition of\n+\t * all zones. If this fails, we will propagate the failure as a\n+\t * restoration error.\n+\t */\n+\tfor (i = 0, zone = zlo-\u003ezones; i \u003c zlo-\u003enr_zones; i++, zone++) {\n+\t\tret = zloop_restore_zone(zlo, zone);\n+\t\tif (ret) {\n+\t\t\tpr_err(\"zloop%d: restore zone %u failed %d\\n\",\n+\t\t\t zlo-\u003eid, i, ret);\n+\t\t\tsts = BLK_SE_STS_RESTORE_ERROR;\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\n+\t/*\n+\t * Update the status of all elements being restored based on the zone\n+\t * restoration result.\n+\t */\n+\tfor (i = 0, se = zlo-\u003eelements; i \u003c zlo-\u003enr_elements; i++, se++) {\n+\t\tif (READ_ONCE(se-\u003estatus) == BLK_SE_STS_RESTORE_IN_PROGRESS)\n+\t\t\tWRITE_ONCE(se-\u003estatus, sts);\n+\t}\n+\n+\t/*\n+\t * Clear the counter of removed elements so that even if we had an error\n+\t * and set the elements health to BLK_SE_STS_RESTORE_ERROR, then can be\n+\t * removed again.\n+\t */\n+\tzlo-\u003enr_removed_elements = 0;\n+\n+\treturn ret;\n+}\n+\n+static int zloop_restore_elements(struct gendisk *disk)\n+{\n+\tstruct zloop_device *zlo = disk-\u003eprivate_data;\n+\tstruct blk_se *se;\n+\tunsigned int i, n = 0;\n+\tint ret = 0;\n+\n+\tif (test_bit(GD_DEAD, \u0026disk-\u003estate) ||\n+\t data_race(READ_ONCE(zlo-\u003estate)) == Zlo_deleting)\n+\t\treturn -ENODEV;\n+\n+\tif (zlo-\u003estor_elements == ZLOOP_STOR_ELEMENTS_NONE)\n+\t\treturn -EOPNOTSUPP;\n+\n+\tmutex_lock(\u0026zlo-\u003estor_elements_lock);\n+\n+\t/* If a remove or restore operation is already in progress, bail out. */\n+\tif (zlo-\u003eremove_element_id || READ_ONCE(zlo-\u003erestore_in_progress)) {\n+\t\tret = -EBUSY;\n+\t\tgoto unlock;\n+\t}\n+\n+\t/*\n+\t * Set the restore in progress state for elements that will be\n+\t * restored.\n+\t */\n+\tfor (i = 0, se = zlo-\u003eelements; i \u003c zlo-\u003enr_elements; i++, se++) {\n+\t\tswitch (READ_ONCE(se-\u003estatus)) {\n+\t\tcase BLK_SE_STS_REMOVED:\n+\t\tcase BLK_SE_STS_RESTORE_ERROR:\n+\t\t\tWRITE_ONCE(se-\u003estatus, BLK_SE_STS_RESTORE_IN_PROGRESS);\n+\t\t\tn++;\n+\t\t\tbreak;\n+\t\tdefault:\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\n+\t/* If we have no removed or in error elements, we have nothing to do. */\n+\tif (!n)\n+\t\tgoto unlock;\n+\n+\tWRITE_ONCE(zlo-\u003erestore_in_progress, true);\n+\tmutex_unlock(\u0026zlo-\u003estor_elements_lock);\n+\n+\t/*\n+\t * Wait a little so that this looks like a real device taking time to\n+\t * operate.\n+\t */\n+\tmsleep(5000);\n+\n+\tmutex_lock(\u0026zlo-\u003estor_elements_lock);\n+\n+\t/* Restore the elements. */\n+\tret = zloop_execute_restore_elements(zlo);\n+\n+\tWRITE_ONCE(zlo-\u003erestore_in_progress, false);\n+\n+unlock:\n+\tmutex_unlock(\u0026zlo-\u003estor_elements_lock);\n+\n+\treturn ret;\n+}\n+\n static void zloop_free_disk(struct gendisk *disk)\n {\n \tstruct zloop_device *zlo = disk-\u003eprivate_data;\n@@ -1029,15 +1538,23 @@ static void zloop_free_disk(struct gendisk *disk)\n \n \tfput(zlo-\u003edata_dir);\n \tdestroy_workqueue(zlo-\u003eworkqueue);\n+\tkfree(zlo-\u003eelements);\n \tkfree(zlo-\u003ebase_dir);\n \tkvfree(zlo);\n }\n \n+static const struct blk_se_ops zloop_se_ops = {\n+\t.report_elements\t= zloop_report_elements,\n+\t.remove_element\t\t= zloop_remove_element,\n+\t.restore_elements\t= zloop_restore_elements,\n+};\n+\n static const struct block_device_operations zloop_fops = {\n \t.owner\t\t\t= THIS_MODULE,\n \t.open\t\t\t= zloop_open,\n \t.report_zones\t\t= zloop_report_zones,\n \t.free_disk\t\t= zloop_free_disk,\n+\t.se_ops\t\t\t= \u0026zloop_se_ops,\n };\n \n __printf(3, 4)\n@@ -1114,6 +1631,25 @@ static int zloop_init_zone(struct zloop_device *zlo, struct zloop_options *opts,\n \tspin_lock_init(\u0026zone-\u003ewp_lock);\n \tzone-\u003estart = (sector_t)zone_no \u003c\u003c zlo-\u003ezone_shift;\n \n+\t/* If we are emulating storage elements, assign one to the zone. */\n+\tif (zlo-\u003estor_elements != ZLOOP_STOR_ELEMENTS_NONE) {\n+\t\tunsigned int nr_elems = zlo-\u003enr_elements;\n+\t\tunsigned int se_idx;\n+\n+\t\tif (zlo-\u003estor_elements == ZLOOP_STOR_ELEMENTS_PAIRS)\n+\t\t\tnr_elems /= 2;\n+\t\tse_idx = zone_no % nr_elems;\n+\t\tzlo-\u003eelements[se_idx].nr_zones++;\n+\n+\t\tzone-\u003ewr_se_id = se_idx + 1;\n+\t\tzone-\u003erd_se_id = zone-\u003ewr_se_id;\n+\t\tif (zlo-\u003estor_elements == ZLOOP_STOR_ELEMENTS_PAIRS) {\n+\t\t\tzone-\u003erd_se_id += nr_elems;\n+\t\t\tzlo-\u003eelements[zone-\u003erd_se_id - 1].nr_zones =\n+\t\t\t\tzlo-\u003eelements[se_idx].nr_zones;\n+\t\t}\n+\t}\n+\n \tif (!restore)\n \t\toflags |= O_CREAT;\n \n@@ -1189,6 +1725,69 @@ static int zloop_init_zone(struct zloop_device *zlo, struct zloop_options *opts,\n \treturn ret;\n }\n \n+#define ZLOOP_MIN_STOR_ELEMENTS\t\t\t2\n+#define ZLOOP_MAX_STOR_ELEMENTS\t\t\t32\n+#define ZLOOP_MIN_ZONES_PER_STOR_ELEMENTS\t32\n+\n+static int zloop_create_storage_elements(struct zloop_device *zlo)\n+{\n+\tstruct blk_se *se, *paired_se;\n+\tunsigned int i, nr_elems, nr_elements;\n+\n+\t/*\n+\t * Calculate the number of storage elements we are going to emulate.\n+\t * To achieve a somewhat realistic emulation, we want at least 2 storage\n+\t * elements, and no more than 32, targeting at least 32 zones per\n+\t * element.\n+\t */\n+\tif (zlo-\u003enr_zones \u003c= 64)\n+\t\tnr_elements = ZLOOP_MIN_STOR_ELEMENTS;\n+\telse\n+\t\tnr_elements =\n+\t\t\tmin(ZLOOP_MAX_STOR_ELEMENTS,\n+\t\t\t zlo-\u003enr_zones / ZLOOP_MIN_ZONES_PER_STOR_ELEMENTS);\n+\n+\t/*\n+\t * If we are emulating pairs of read and write storage elements, we need\n+\t * double the number of storage element descriptors.\n+\t */\n+\tnr_elems = nr_elements;\n+\tif (zlo-\u003estor_elements == ZLOOP_STOR_ELEMENTS_PAIRS)\n+\t\tnr_elems *= 2;\n+\tzlo-\u003eelements = kzalloc_objs(struct blk_se, nr_elems);\n+\tif (!zlo-\u003eelements)\n+\t\treturn -ENOMEM;\n+\n+\tfor (i = 0, se = zlo-\u003eelements; i \u003c nr_elements; i++, se++) {\n+\t\tse-\u003eid = i + 1;\n+\t\tif (zlo-\u003estor_elements == ZLOOP_STOR_ELEMENTS_PAIRS)\n+\t\t\tse-\u003etype = BLK_SE_TYPE_WRITE;\n+\t\telse\n+\t\t\tse-\u003etype = BLK_SE_TYPE_RDWR;\n+\t\tse-\u003estatus = BLK_SE_STS_OK;\n+\t}\n+\n+\t/*\n+\t * If we are emulating pairs of read and write storage elements,\n+\t * initialize the read elements paired with the write elements we just\n+\t * initialized.\n+\t */\n+\tif (zlo-\u003estor_elements == ZLOOP_STOR_ELEMENTS_PAIRS) {\n+\t\tfor (i = 0; i \u003c nr_elements; i++, se++) {\n+\t\t\tse-\u003eid = nr_elements + i + 1;\n+\t\t\tpaired_se = \u0026zlo-\u003eelements[i];\n+\t\t\tse-\u003epaired_id = paired_se-\u003eid;\n+\t\t\tpaired_se-\u003epaired_id = se-\u003eid;\n+\t\t\tse-\u003etype = BLK_SE_TYPE_READ;\n+\t\t\tse-\u003estatus = BLK_SE_STS_OK;\n+\t\t}\n+\t}\n+\n+\tzlo-\u003enr_elements = nr_elems;\n+\n+\treturn 0;\n+}\n+\n static bool zloop_dev_exists(struct zloop_device *zlo)\n {\n \tstruct file *cnv, *seq;\n@@ -1221,6 +1820,11 @@ static int zloop_ctl_add(struct zloop_options *opts)\n \tint ret = -EINVAL;\n \tbool restore;\n \n+\tif (opts-\u003emask \u0026 ZLOOP_OPT_ELEMENT_ID) {\n+\t\tpr_err(\"Invalid option specified for add\\n\");\n+\t\treturn -EINVAL;\n+\t}\n+\n \t__module_get(THIS_MODULE);\n \n \tnr_zones = opts-\u003ecapacity \u003e\u003e ilog2(opts-\u003ezone_size);\n@@ -1244,6 +1848,7 @@ static int zloop_ctl_add(struct zloop_options *opts)\n \tWRITE_ONCE(zlo-\u003estate, Zlo_creating);\n \tspin_lock_init(\u0026zlo-\u003eopen_zones_lock);\n \tINIT_LIST_HEAD(\u0026zlo-\u003eopen_zones_lru_list);\n+\tmutex_init(\u0026zlo-\u003estor_elements_lock);\n \n \tret = mutex_lock_killable(\u0026zloop_ctl_mutex);\n \tif (ret)\n@@ -1277,12 +1882,19 @@ static int zloop_ctl_add(struct zloop_options *opts)\n \tif (zlo-\u003ezone_append)\n \t\tzlo-\u003eordered_zone_append = opts-\u003eordered_zone_append;\n \tzlo-\u003ediscard_write_cache = opts-\u003ediscard_write_cache;\n+\tzlo-\u003estor_elements = opts-\u003estor_elements;\n+\n+\tif (zlo-\u003estor_elements != ZLOOP_STOR_ELEMENTS_NONE) {\n+\t\tret = zloop_create_storage_elements(zlo);\n+\t\tif (ret)\n+\t\t\tgoto out_free_idr;\n+\t}\n \n \tzlo-\u003eworkqueue = alloc_workqueue(\"zloop%d\", WQ_UNBOUND | WQ_FREEZABLE,\n \t\t\t\topts-\u003enr_queues * opts-\u003equeue_depth, zlo-\u003eid);\n \tif (!zlo-\u003eworkqueue) {\n \t\tret = -ENOMEM;\n-\t\tgoto out_free_idr;\n+\t\tgoto out_destroy_storage_elements;\n \t}\n \n \tif (opts-\u003ebase_dir)\n@@ -1368,6 +1980,10 @@ static int zloop_ctl_add(struct zloop_options *opts)\n \t\tzlo-\u003eid, zlo-\u003enr_zones,\n \t\t((sector_t)zlo-\u003ezone_size \u003c\u003c SECTOR_SHIFT) \u003e\u003e 20,\n \t\tzlo-\u003eblock_size);\n+\tif (zlo-\u003enr_elements)\n+\t\tpr_info(\"zloop%d: %u storage elements\\n\",\n+\t\t\tzlo-\u003eid, zlo-\u003enr_elements);\n+\n \tpr_info(\"zloop%d: using %s%s zone append\\n\",\n \t\tzlo-\u003eid,\n \t\tzlo-\u003eordered_zone_append ? \"ordered \" : \"\",\n@@ -1391,6 +2007,8 @@ static int zloop_ctl_add(struct zloop_options *opts)\n \tkfree(zlo-\u003ebase_dir);\n out_destroy_workqueue:\n \tdestroy_workqueue(zlo-\u003eworkqueue);\n+out_destroy_storage_elements:\n+\tkfree(zlo-\u003eelements);\n out_free_idr:\n \tmutex_lock(\u0026zloop_ctl_mutex);\n \tidr_remove(\u0026zloop_index_idr, zlo-\u003eid);\n@@ -1490,6 +2108,49 @@ static int zloop_ctl_remove(struct zloop_options *opts)\n \treturn 0;\n }\n \n+static int zloop_ctl_degrade_element(struct zloop_options *opts)\n+{\n+\tstruct zloop_device *zlo;\n+\tint ret = 0;\n+\n+\tif (!(opts-\u003emask \u0026 ZLOOP_OPT_ID)) {\n+\t\tpr_err(\"No ID specified for degrade_element\\n\");\n+\t\treturn -EINVAL;\n+\t}\n+\n+\tif (!(opts-\u003emask \u0026 ZLOOP_OPT_ELEMENT_ID)) {\n+\t\tpr_err(\"No element ID specified for degrade_element\\n\");\n+\t\treturn -EINVAL;\n+\t}\n+\n+\tif (opts-\u003emask \u0026 ~(ZLOOP_OPT_ID | ZLOOP_OPT_ELEMENT_ID)) {\n+\t\tpr_err(\"Invalid option specified for degrade_element\\n\");\n+\t\treturn -EINVAL;\n+\t}\n+\n+\tret = mutex_lock_killable(\u0026zloop_ctl_mutex);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\tzlo = idr_find(\u0026zloop_index_idr, opts-\u003eid);\n+\tif (!zlo || zlo-\u003estate == Zlo_creating)\n+\t\tret = -ENODEV;\n+\telse if (zlo-\u003estate == Zlo_deleting)\n+\t\tret = -EINVAL;\n+\tif (ret)\n+\t\tgoto unlock;\n+\n+\tret = zloop_degrade_element(zlo, opts-\u003eelement_id);\n+\tif (!ret)\n+\t\tpr_info(\"%pg: degraded element %u\\n\",\n+\t\t\tzlo-\u003edisk-\u003epart0, opts-\u003eelement_id);\n+\n+unlock:\n+\tmutex_unlock(\u0026zloop_ctl_mutex);\n+\n+\treturn ret;\n+}\n+\n static int zloop_parse_options(struct zloop_options *opts, const char *buf)\n {\n \tsubstring_t args[MAX_OPT_ARGS];\n@@ -1509,6 +2170,8 @@ static int zloop_parse_options(struct zloop_options *opts, const char *buf)\n \topts-\u003ebuffered_io = ZLOOP_DEF_BUFFERED_IO;\n \topts-\u003ezone_append = ZLOOP_DEF_ZONE_APPEND;\n \topts-\u003eordered_zone_append = ZLOOP_DEF_ORDERED_ZONE_APPEND;\n+\topts-\u003estor_elements = ZLOOP_DEF_STOR_ELEMENTS;\n+\topts-\u003eelement_id = ZLOOP_DEF_ELEMENT_ID;\n \n \tif (!buf)\n \t\treturn 0;\n@@ -1643,6 +2306,30 @@ static int zloop_parse_options(struct zloop_options *opts, const char *buf)\n \t\tcase ZLOOP_OPT_DISCARD_WRITE_CACHE:\n \t\t\topts-\u003ediscard_write_cache = true;\n \t\t\tbreak;\n+\t\tcase ZLOOP_OPT_STOR_ELEMENTS:\n+\t\t\tif (match_uint(args, \u0026token)) {\n+\t\t\t\tret = -EINVAL;\n+\t\t\t\tgoto out;\n+\t\t\t}\n+\t\t\tswitch (token) {\n+\t\t\tcase ZLOOP_STOR_ELEMENTS_NONE:\n+\t\t\tcase ZLOOP_STOR_ELEMENTS_RDWR:\n+\t\t\tcase ZLOOP_STOR_ELEMENTS_PAIRS:\n+\t\t\t\tbreak;\n+\t\t\tdefault:\n+\t\t\t\tpr_err(\"Invalid stor_elements value\\n\");\n+\t\t\t\tret = -EINVAL;\n+\t\t\t\tgoto out;\n+\t\t\t}\n+\t\t\topts-\u003estor_elements = token;\n+\t\t\tbreak;\n+\t\tcase ZLOOP_OPT_ELEMENT_ID:\n+\t\t\tif (match_uint(args, \u0026token)) {\n+\t\t\t\tret = -EINVAL;\n+\t\t\t\tgoto out;\n+\t\t\t}\n+\t\t\topts-\u003eelement_id = token;\n+\t\t\tbreak;\n \t\tcase ZLOOP_OPT_ERR:\n \t\tdefault:\n \t\t\tpr_warn(\"unknown parameter or missing value '%s'\\n\", p);\n@@ -1671,14 +2358,16 @@ static int zloop_parse_options(struct zloop_options *opts, const char *buf)\n enum {\n \tZLOOP_CTL_ADD,\n \tZLOOP_CTL_REMOVE,\n+\tZLOOP_CTL_DEGRADE_ELEMENT,\n };\n \n static struct zloop_ctl_op {\n \tint\t\tcode;\n \tconst char\t*name;\n } zloop_ctl_ops[] = {\n-\t{ ZLOOP_CTL_ADD,\t\"add\" },\n-\t{ ZLOOP_CTL_REMOVE,\t\"remove\" },\n+\t{ ZLOOP_CTL_ADD,\t\t\"add\" },\n+\t{ ZLOOP_CTL_REMOVE,\t\t\"remove\" },\n+\t{ ZLOOP_CTL_DEGRADE_ELEMENT,\t\"degrade_element\" },\n \t{ -1,\tNULL },\n };\n \n@@ -1726,6 +2415,9 @@ static ssize_t zloop_ctl_write(struct file *file, const char __user *ubuf,\n \tcase ZLOOP_CTL_REMOVE:\n \t\tret = zloop_ctl_remove(\u0026opts);\n \t\tbreak;\n+\tcase ZLOOP_CTL_DEGRADE_ELEMENT:\n+\t\tret = zloop_ctl_degrade_element(\u0026opts);\n+\t\tbreak;\n \tdefault:\n \t\tpr_err(\"Invalid operation\\n\");\n \t\tret = -EINVAL;\n@@ -1749,6 +2441,8 @@ static int zloop_ctl_show(struct seq_file *seq_file, void *private)\n \t\ttok = \u0026zloop_opt_tokens[i];\n \t\tif (!tok-\u003epattern)\n \t\t\tbreak;\n+\t\tif (tok-\u003etoken == ZLOOP_OPT_ELEMENT_ID)\n+\t\t\tcontinue;\n \t\tif (i)\n \t\t\tseq_putc(seq_file, ',');\n \t\tseq_puts(seq_file, tok-\u003epattern);\n@@ -1759,6 +2453,10 @@ static int zloop_ctl_show(struct seq_file *seq_file, void *private)\n \tseq_puts(seq_file, zloop_ctl_ops[1].name);\n \tseq_puts(seq_file, \" id=%d\\n\");\n \n+\t/* Degrade element operation */\n+\tseq_puts(seq_file, zloop_ctl_ops[2].name);\n+\tseq_puts(seq_file, \" id=%d,element_id=%u\\n\");\n+\n \treturn 0;\n }\n \ndiff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c\nindex dc45df819c426a..8b1c05de541dd4 100644\n--- a/drivers/scsi/sd.c\n+++ b/drivers/scsi/sd.c\n@@ -1424,7 +1424,8 @@ static blk_status_t sd_setup_read_write_cmnd(struct scsi_cmnd *cmd)\n {\n \tstruct request *rq = scsi_cmd_to_rq(cmd);\n \tstruct scsi_device *sdp = cmd-\u003edevice;\n-\tstruct scsi_disk *sdkp = scsi_disk(rq-\u003eq-\u003edisk);\n+\tstruct gendisk *disk = rq-\u003eq-\u003edisk;\n+\tstruct scsi_disk *sdkp = scsi_disk(disk);\n \tsector_t lba = sectors_to_logical(sdp, blk_rq_pos(rq));\n \tsector_t threshold;\n \tunsigned int nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));\n@@ -1456,6 +1457,23 @@ static blk_status_t sd_setup_read_write_cmnd(struct scsi_cmnd *cmd)\n \t\tgoto fail;\n \t}\n \n+\t/*\n+\t * The block layer already prevents writes to offline zones of zoned\n+\t * block devices. But there are no checks for reads, so check this here.\n+\t * Since disk_zone_is_offline() uses the block layer cached zone\n+\t * condition, this is inherently racy with zone revalidation updating\n+\t * the zone condition. However, this will never prevent valid read\n+\t * operations as the only case where we could proceed is if the read is\n+\t * received after a restoration of the disk storage element completes\n+\t * but before the zones are revalidated. And in that case, all zones are\n+\t * empty and thus have no data to read.\n+\t * Note that we are quiet about the error (no message) to avoid flooding\n+\t * the kernel log.\n+\t */\n+\tif (sdkp-\u003edevice-\u003etype == TYPE_ZBC \u0026\u0026\n+\t disk_zone_is_offline(disk, blk_rq_pos(rq)))\n+\t\tgoto fail;\n+\n \t/*\n \t * Some SD card readers can't handle accesses which touch the\n \t * last one or two logical blocks. Split accesses as needed.\n@@ -3939,6 +3957,9 @@ static const struct block_device_operations sd_fops = {\n \t.get_unique_id\t\t= sd_get_unique_id,\n \t.free_disk\t\t= scsi_disk_free_disk,\n \t.pr_ops\t\t\t= \u0026sd_pr_ops,\n+#ifdef CONFIG_BLK_DEV_ZONED\n+\t.se_ops\t\t\t= \u0026sd_zbc_se_ops,\n+#endif\n };\n \n /**\ndiff --git a/drivers/scsi/sd.h b/drivers/scsi/sd.h\nindex 574af82430169e..8a32e5f771d8cb 100644\n--- a/drivers/scsi/sd.h\n+++ b/drivers/scsi/sd.h\n@@ -156,6 +156,9 @@ struct scsi_disk {\n \tunsigned\tignore_medium_access_errors : 1;\n \tunsigned\trscs : 1; /* reduced stream control support */\n \tunsigned\tuse_atomic_write_boundary : 1;\n+\tunsigned\tdepop_report_supported : 1;\n+\tunsigned\tdepop_modify_zones_supported : 1;\n+\tunsigned\tdepop_restore_supported : 1;\n };\n #define to_scsi_disk(obj) container_of(obj, struct scsi_disk, disk_dev)\n \n@@ -242,6 +245,8 @@ unsigned int sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes,\n int sd_zbc_report_zones(struct gendisk *disk, sector_t sector,\n \t\tunsigned int nr_zones, struct blk_report_zones_args *args);\n \n+extern const struct blk_se_ops sd_zbc_se_ops;\n+\n #else /* CONFIG_BLK_DEV_ZONED */\n \n static inline int sd_zbc_read_zones(struct scsi_disk *sdkp,\ndiff --git a/drivers/scsi/sd_zbc.c b/drivers/scsi/sd_zbc.c\nindex 8cbf171e6fabf1..3bad1392f0e259 100644\n--- a/drivers/scsi/sd_zbc.c\n+++ b/drivers/scsi/sd_zbc.c\n@@ -12,6 +12,7 @@\n #include \u003clinux/vmalloc.h\u003e\n #include \u003clinux/sched/mm.h\u003e\n #include \u003clinux/mutex.h\u003e\n+#include \u003clinux/delay.h\u003e\n \n #include \u003clinux/unaligned.h\u003e\n \n@@ -516,11 +517,50 @@ static int sd_zbc_check_capacity(struct scsi_disk *sdkp, unsigned char *buf,\n \treturn 0;\n }\n \n+/*\n+ * sd_zbc_check_depop_supported - Check if the device supports storage element\n+ *\t\t\t\t depopulation\n+ * @sdkp: Target SCSI disk\n+ * @buf: command buffer\n+ *\n+ * Check if the device supports the storage elements depopulation management\n+ * commands.\n+ */\n+static void sd_zbc_check_depop_supported(struct scsi_disk *sdkp,\n+\t\t\t\t\t unsigned char *buf)\n+{\n+\t/* Mandate support for reporting storage elements health. */\n+\tsdkp-\u003edepop_report_supported =\n+\t\tscsi_report_opcode(sdkp-\u003edevice, buf, SD_BUF_SIZE,\n+\t\t\t\t SERVICE_ACTION_IN_16,\n+\t\t\t\t SAI_GET_PHYSICAL_ELEMENT_STATUS) == 1;\n+\tif (!sdkp-\u003edepop_report_supported) {\n+\t\tsdkp-\u003edepop_modify_zones_supported = false;\n+\t\tsdkp-\u003edepop_restore_supported = false;\n+\t\treturn;\n+\t}\n+\n+\tsdkp-\u003edepop_modify_zones_supported =\n+\t\tscsi_report_opcode(sdkp-\u003edevice, buf, SD_BUF_SIZE,\n+\t\t\t\t SERVICE_ACTION_IN_16,\n+\t\t\t\t SAI_REMOVE_ELEMENT_AND_MODIFY_ZONES) == 1;\n+\n+\tsdkp-\u003edepop_restore_supported =\n+\t\tscsi_report_opcode(sdkp-\u003edevice, buf, SD_BUF_SIZE,\n+\t\t\t\t SERVICE_ACTION_IN_16,\n+\t\t\t\t SAI_RESTORE_ELEMENTS_AND_REBUILD) == 1;\n+}\n+\n static void sd_zbc_print_zones(struct scsi_disk *sdkp)\n {\n \tif (sdkp-\u003edevice-\u003etype != TYPE_ZBC || !sdkp-\u003ecapacity)\n \t\treturn;\n \n+\tif (sdkp-\u003edepop_modify_zones_supported)\n+\t\tsd_printk(KERN_NOTICE, sdkp, \"Depop modify zones supported\\n\");\n+\tif (sdkp-\u003edepop_restore_supported)\n+\t\tsd_printk(KERN_NOTICE, sdkp, \"Depop restoration supported\\n\");\n+\n \tif (sdkp-\u003ecapacity \u0026 (sdkp-\u003ezone_info.zone_blocks - 1))\n \t\tsd_printk(KERN_NOTICE, sdkp,\n \t\t\t \"%u zones of %u logical blocks + 1 runt zone\\n\",\n@@ -533,6 +573,462 @@ static void sd_zbc_print_zones(struct scsi_disk *sdkp)\n \t\t\t sdkp-\u003ezone_info.zone_blocks);\n }\n \n+static void sd_zbc_parse_storage_element(struct scsi_disk *sdkp, u8 *desc,\n+\t\t\t\t\t struct blk_se *element)\n+{\n+\tstruct scsi_device *sdp = sdkp-\u003edevice;\n+\tsector_t zone_sectors = sd_zbc_zone_sectors(sdkp);\n+\tu64 capacity;\n+\n+\tmemset(element, 0, sizeof(*element));\n+\n+\telement-\u003eid = get_unaligned_be32(\u0026desc[4]);\n+\telement-\u003erestore_allowed = desc[13] \u0026 0x01;\n+\n+\tswitch (desc[14]) {\n+\tcase SCSI_PHYS_ELEM_TYPE_ALL_ACCESS_STORAGE:\n+\t\telement-\u003etype = BLK_SE_TYPE_RDWR;\n+\t\tcapacity = get_unaligned_be64(\u0026desc[16]);\n+\t\tif (!zone_sectors || capacity == ULLONG_MAX)\n+\t\t\telement-\u003enr_zones = 0;\n+\t\telse\n+\t\t\telement-\u003enr_zones =\n+\t\t\t\tlogical_to_sectors(sdp, capacity) \u003e\u003e\n+\t\t\t\t\tilog2(zone_sectors);\n+\t\tbreak;\n+\tcase SCSI_PHYS_ELEM_TYPE_FRAC_ACCESS_STORAGE:\n+\t\telement-\u003epaired_id = get_unaligned_be32(\u0026desc[16]);\n+\t\tif (desc[20] \u0026 0x01)\n+\t\t\telement-\u003etype = BLK_SE_TYPE_READ;\n+\t\telse\n+\t\t\telement-\u003etype = BLK_SE_TYPE_WRITE;\n+\t\telement-\u003enr_zones = get_unaligned_be64(\u0026desc[24]);\n+\t\tbreak;\n+\tdefault:\n+\t\telement-\u003etype = BLK_SE_TYPE_UNKNOWN;\n+\t}\n+\n+\tif (desc[15] \u003e= SCSI_PHYS_ELEM_HEALTH_WITHIN_SPEC_LIMITS_MIN \u0026\u0026\n+\t desc[15] \u003c= SCSI_PHYS_ELEM_HEALTH_WITHIN_SPEC_LIMITS_MAX) {\n+\t\telement-\u003estatus = BLK_SE_STS_OK;\n+\t\treturn;\n+\t}\n+\n+\tif (desc[15] \u003e= SCSI_PHYS_ELEM_HEALTH_OUTSIDE_SPEC_LIMITS_MIN \u0026\u0026\n+\t desc[15] \u003c= SCSI_PHYS_ELEM_HEALTH_OUTSIDE_SPEC_LIMITS_MAX) {\n+\t\telement-\u003estatus = BLK_SE_STS_DEGRADED;\n+\t\treturn;\n+\t}\n+\n+\tswitch (desc[15]) {\n+\tcase SCSI_PHYS_ELEM_HEALTH_AT_SPEC_LIMITS:\n+\t\telement-\u003estatus = BLK_SE_STS_OK_LIMIT;\n+\t\tbreak;\n+\tcase SCSI_PHYS_ELEM_HEALTH_DEPOP_REVOKE_ERR:\n+\t\telement-\u003estatus = BLK_SE_STS_RESTORE_ERROR;\n+\t\tbreak;\n+\tcase SCSI_PHYS_ELEM_HEALTH_DEPOP_REVOKE_IN_PROGRESS:\n+\t\telement-\u003estatus = BLK_SE_STS_RESTORE_IN_PROGRESS;\n+\t\tbreak;\n+\tcase SCSI_PHYS_ELEM_HEALTH_DEPOP_ERR:\n+\t\telement-\u003estatus = BLK_SE_STS_REMOVE_ERROR;\n+\t\tbreak;\n+\tcase SCSI_PHYS_ELEM_HEALTH_DEPOP_IN_PROGRESS:\n+\t\telement-\u003estatus = BLK_SE_STS_REMOVE_IN_PROGRESS;\n+\t\tbreak;\n+\tcase SCSI_PHYS_ELEM_HEALTH_DEPOP_OK:\n+\t\telement-\u003estatus = BLK_SE_STS_REMOVED;\n+\t\tbreak;\n+\tcase SCSI_PHYS_ELEM_HEALTH_NOT_REPORTED:\n+\tdefault:\n+\t\telement-\u003estatus = BLK_SE_STS_UNKNOWN;\n+\t\tbreak;\n+\t}\n+}\n+\n+/*\n+ * Large hard limit on the number of storage elements. This accommodates all\n+ * known devices today and likely forever :)\n+ */\n+#define SD_ZBC_MAX_STORAGE_ELEMENTS\t255\n+\n+struct sd_zbc_se_report {\n+\tstruct blk_se *elements;\n+\tunsigned int nr_elements;\n+\tunsigned int nr_reported_elements;\n+\tbool need_free_elements;\n+\tu8 *buf;\n+\tunsigned int buf_size;\n+};\n+\n+static int sd_zbc_alloc_se_report(struct blk_se *elements,\n+\t\t\t\t unsigned int nr_elements,\n+\t\t\t\t bool alloc_elements,\n+\t\t\t\t struct sd_zbc_se_report *rep)\n+{\n+\t/*\n+\t * We need at least 32B for the report header and 32B for each\n+\t * descriptor. Note that we use GFP_KERNEL here, which may block. That\n+\t * is fine when called from ioctl context. When called from a file\n+\t * system, the FS can use memalloc_noio_save()/restore() or\n+\t * memalloc_nofs_save()/restore() if GFP_NOFS is required to avoid I/Os\n+\t * and/or FS re-entry.\n+\t */\n+\trep-\u003eneed_free_elements = alloc_elements;\n+\trep-\u003enr_elements = min(SD_ZBC_MAX_STORAGE_ELEMENTS, nr_elements);\n+\trep-\u003ebuf_size = ALIGN((rep-\u003enr_elements + 1) * 32, SECTOR_SIZE);\n+\trep-\u003ebuf = kzalloc(rep-\u003ebuf_size, GFP_KERNEL);\n+\tif (!rep-\u003ebuf)\n+\t\treturn -ENOMEM;\n+\n+\tif (!alloc_elements) {\n+\t\trep-\u003eelements = elements;\n+\t\treturn 0;\n+\t}\n+\n+\trep-\u003eelements =\n+\t\tkzalloc_objs(struct blk_se, rep-\u003enr_elements, GFP_KERNEL);\n+\tif (!rep-\u003eelements) {\n+\t\tkfree(rep-\u003ebuf);\n+\t\treturn -ENOMEM;\n+\t}\n+\n+\treturn 0;\n+}\n+\n+static void sd_zbc_free_se_report(struct sd_zbc_se_report *rep)\n+{\n+\tkfree(rep-\u003ebuf);\n+\tif (rep-\u003eneed_free_elements)\n+\t\tkfree(rep-\u003eelements);\n+}\n+\n+static int sd_zbc_report_se(struct scsi_disk *sdkp,\n+\t\t\t struct sd_zbc_se_report *rep)\n+{\n+\tstruct scsi_device *sdp = sdkp-\u003edevice;\n+\tconst int timeout = sdp-\u003erequest_queue-\u003erq_timeout;\n+\tstruct scsi_sense_hdr sshdr;\n+\tstruct scsi_failure failure_defs[] = {\n+\t\t/* Allow up to retry 3 times */\n+\t\t{\n+\t\t\t.result = SCMD_FAILURE_RESULT_ANY,\n+\t\t\t.allowed = 3,\n+\t\t},\n+\t\t{}\n+\t};\n+\tstruct scsi_failures failures = {\n+\t\t.failure_definitions = failure_defs,\n+\t};\n+\tconst struct scsi_exec_args exec_args = {\n+\t\t.sshdr = \u0026sshdr,\n+\t\t.failures = \u0026failures,\n+\t};\n+\tunsigned char cmd[16];\n+\tunsigned int nr_descs;\n+\tint i, result;\n+\tu8 *desc;\n+\n+\tmemset(cmd, 0, 16);\n+\tcmd[0] = SERVICE_ACTION_IN_16;\n+\tcmd[1] = SAI_GET_PHYSICAL_ELEMENT_STATUS;\n+\tput_unaligned_be32(rep-\u003ebuf_size, \u0026cmd[10]);\n+\n+\tresult = scsi_execute_cmd(sdp, cmd, REQ_OP_DRV_IN,\n+\t\t\trep-\u003ebuf, rep-\u003ebuf_size, timeout, 1, \u0026exec_args);\n+\tif (result) {\n+\t\tif (result \u003e 0) {\n+\t\t\tsd_print_result(sdkp, \"GET PHYSICAL ELEMENT STATUS\",\n+\t\t\t\t\tresult);\n+\t\t\tif (scsi_sense_valid(\u0026sshdr))\n+\t\t\t\tsd_print_sense_hdr(sdkp, \u0026sshdr);\n+\t\t\treturn -EIO;\n+\t\t}\n+\t\treturn result;\n+\t}\n+\n+\tnr_descs = get_unaligned_be32(\u0026rep-\u003ebuf[0]);\n+\tif (!nr_descs) {\n+\t\tsd_printk(KERN_ERR, sdkp,\n+\t\t\t \"Invalid number of phys element descriptors\\n\");\n+\t\treturn -EIO;\n+\t}\n+\tif (nr_descs \u003e SD_ZBC_MAX_STORAGE_ELEMENTS) {\n+\t\tsd_printk(KERN_ERR, sdkp,\n+\t\t\t \"Too many phys element descriptors\\n\");\n+\t\treturn -EIO;\n+\t}\n+\n+\tif (!rep-\u003eelements) {\n+\t\trep-\u003enr_reported_elements = nr_descs;\n+\t\treturn 0;\n+\t}\n+\n+\tnr_descs = get_unaligned_be32(\u0026rep-\u003ebuf[4]);\n+\tif (!nr_descs) {\n+\t\tsd_printk(KERN_ERR, sdkp,\n+\t\t\t\"Invalid number of reported phys element descriptors\\n\");\n+\t\treturn -EIO;\n+\t}\n+\n+\tdesc = \u0026rep-\u003ebuf[32];\n+\tfor (i = 0; i \u003c min(rep-\u003enr_elements, nr_descs); i++, desc += 32)\n+\t\tsd_zbc_parse_storage_element(sdkp, desc, \u0026rep-\u003eelements[i]);\n+\trep-\u003enr_reported_elements = i;\n+\n+\treturn 0;\n+}\n+\n+static int sd_zbc_report_storage_elements(struct gendisk *disk,\n+\t\t\t\t\t struct blk_se *elements,\n+\t\t\t\t\t unsigned int *nr_elements)\n+{\n+\tstruct scsi_disk *sdkp = scsi_disk(disk);\n+\tstruct sd_zbc_se_report rep;\n+\tint ret;\n+\n+\tif (!sdkp-\u003edepop_report_supported)\n+\t\treturn -EOPNOTSUPP;\n+\n+\tret = sd_zbc_alloc_se_report(elements, *nr_elements, false, \u0026rep);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\tret = sd_zbc_report_se(sdkp, \u0026rep);\n+\tif (!ret)\n+\t\t*nr_elements = rep.nr_reported_elements;\n+\n+\tsd_zbc_free_se_report(\u0026rep);\n+\n+\treturn ret;\n+}\n+\n+static int sd_zbc_execute_storage_element_cmd(struct scsi_disk *sdkp,\n+\t\t\t\t\t u8 action, u32 element_id)\n+{\n+\tstruct scsi_device *sdp = sdkp-\u003edevice;\n+\tconst int timeout = sdp-\u003erequest_queue-\u003erq_timeout;\n+\tstruct scsi_sense_hdr sshdr;\n+\tconst struct scsi_exec_args exec_args = {\n+\t\t.sshdr = \u0026sshdr,\n+\t};\n+\tunsigned char cmd[16];\n+\tconst char *cmd_name;\n+\tint result;\n+\n+\tmemset(cmd, 0, sizeof(cmd));\n+\tcmd[0] = SERVICE_ACTION_IN_16;\n+\tcmd[1] = action;\n+\tif (action == SAI_REMOVE_ELEMENT_AND_MODIFY_ZONES) {\n+\t\tput_unaligned_be32(element_id, \u0026cmd[10]);\n+\t\tcmd_name = \"REMOVE ELEMENT AND MODIFY ZONES\";\n+\t} else {\n+\t\tcmd_name = \"RESTORE ELEMENTS AND REBUILD\";\n+\t}\n+\n+\tresult = scsi_execute_cmd(sdp, cmd, REQ_OP_DRV_IN, NULL, 0,\n+\t\t\t\t timeout, 1, \u0026exec_args);\n+\tif (result) {\n+\t\tsd_printk(KERN_ERR, sdkp, \"%s failed\\n\", cmd_name);\n+\t\tif (result \u003e 0) {\n+\t\t\tif (scsi_sense_valid(\u0026sshdr))\n+\t\t\t\tsd_print_sense_hdr(sdkp, \u0026sshdr);\n+\t\t\tresult = -EIO;\n+\t\t}\n+\t\treturn result;\n+\t}\n+\n+\treturn 0;\n+}\n+\n+/*\n+ * Wait for the completion of a storage element remove operation. Note that the\n+ * standards advise to use pollable sense data with the REQUEST SENSE command to\n+ * detect if a remove operation is in progress. However, since REQUEST SENSE\n+ * will return sense data only for the last executed command, and since we are\n+ * not blocking commands, this method would be unreliable as we may end up\n+ * seeing sense data (or no sense data) for a command that is not the REMOVE\n+ * ELEMENT AND MODIFY ZONES we issued.\n+ */\n+static int sd_zbc_wait_for_se_removal(struct scsi_disk *sdkp,\n+\t\t\t\t struct sd_zbc_se_report *rep,\n+\t\t\t\t unsigned int element_id)\n+{\n+\tbool element_checked, element_removed;\n+\tint ret, in_progress;\n+\tunsigned int i;\n+\n+\t/*\n+\t * Poll the storage elements health to see that we get the element we\n+\t * are interested in and that its removal completed. Since we do not\n+\t * know how long the operation may take, we cannot have a timeout on\n+\t * this loop. So check for fatal signals so that the user can terminate\n+\t * this loop if the device misbehaves.\n+\t */\n+\tdo {\n+\t\t/* Let the operation proceed and check. */\n+\t\tmsleep(1000);\n+\n+\t\tret = sd_zbc_report_se(sdkp, rep);\n+\t\tif (ret)\n+\t\t\treturn ret;\n+\n+\t\telement_checked = false;\n+\t\telement_removed = false;\n+\t\tin_progress = 0;\n+\t\tfor (i = 0; i \u003c rep-\u003enr_elements; i++) {\n+\t\t\tif (rep-\u003eelements[i].id == element_id)\n+\t\t\t\telement_checked = true;\n+\t\t\tswitch (rep-\u003eelements[i].status) {\n+\t\t\tcase BLK_SE_STS_REMOVED:\n+\t\t\t\tif (rep-\u003eelements[i].id == element_id)\n+\t\t\t\t\telement_removed = true;\n+\t\t\t\tbreak;\n+\t\t\tcase BLK_SE_STS_REMOVE_IN_PROGRESS:\n+\t\t\t\tin_progress++;\n+\t\t\t\tbreak;\n+\t\t\tcase BLK_SE_STS_REMOVE_ERROR:\n+\t\t\t\tif (rep-\u003eelements[i].id == element_id)\n+\t\t\t\t\treturn -EIO;\n+\t\t\t\tbreak;\n+\t\t\tdefault:\n+\t\t\t\tbreak;\n+\t\t\t}\n+\t\t}\n+\n+\t\tif (!element_checked)\n+\t\t\treturn -EIO;\n+\t\tif (element_removed \u0026\u0026 !in_progress)\n+\t\t\treturn 0;\n+\t} while (!fatal_signal_pending(current));\n+\n+\treturn -EINTR;\n+}\n+\n+static int sd_zbc_remove_storage_element(struct gendisk *disk,\n+\t\t\t\t\t unsigned int element_id)\n+{\n+\tstruct scsi_disk *sdkp = scsi_disk(disk);\n+\tstruct sd_zbc_se_report rep;\n+\tint ret;\n+\n+\tif (!sdkp-\u003edepop_modify_zones_supported)\n+\t\treturn -EOPNOTSUPP;\n+\n+\t/*\n+\t * Get the resources we need to prepare for waiting for the operation to\n+\t * complete.\n+\t */\n+\tret = sd_zbc_alloc_se_report(NULL, disk-\u003enr_storage_elements,\n+\t\t\t\t true, \u0026rep);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\t/*\n+\t * Execute the remove command and wait for the element removal operation\n+\t * to complete.\n+\t */\n+\tret = sd_zbc_execute_storage_element_cmd(sdkp,\n+\t\t\tSAI_REMOVE_ELEMENT_AND_MODIFY_ZONES, element_id);\n+\tif (!ret)\n+\t\tret = sd_zbc_wait_for_se_removal(sdkp, \u0026rep, element_id);\n+\tif (!ret)\n+\t\tsd_printk(KERN_NOTICE, sdkp,\n+\t\t\t \"Removed storage element %u\\n\", element_id);\n+\n+\tsd_zbc_free_se_report(\u0026rep);\n+\n+\treturn ret;\n+}\n+\n+/*\n+ * Wait for the completion of the restoration of removed storage elements.\n+ * See the note for sd_zbc_wait_for_se_removal().\n+ */\n+static int sd_zbc_wait_for_se_restore(struct scsi_disk *sdkp,\n+\t\t\t\t struct sd_zbc_se_report *rep)\n+{\n+\tint ret, in_progress;\n+\tunsigned int i;\n+\n+\t/*\n+\t * Poll the storage elements health to see if we have any element still\n+\t * being restored or if we got errors. Since we do not know how long the\n+\t * operation may take, we cannot have a timeout on this loop. So check\n+\t * for fatal signals so that the user can terminate this retry loop if\n+\t * the device misbehaves.\n+\t */\n+\tdo {\n+\t\t/* Let the operation proceed and check. */\n+\t\tmsleep(1000);\n+\n+\t\tret = sd_zbc_report_se(sdkp, rep);\n+\t\tif (ret)\n+\t\t\treturn ret;\n+\n+\t\tin_progress = 0;\n+\t\tfor (i = 0; i \u003c rep-\u003enr_elements; i++) {\n+\t\t\tswitch (rep-\u003eelements[i].status) {\n+\t\t\tcase BLK_SE_STS_RESTORE_IN_PROGRESS:\n+\t\t\t\tin_progress++;\n+\t\t\t\tbreak;\n+\t\t\tcase BLK_SE_STS_REMOVED:\n+\t\t\tcase BLK_SE_STS_RESTORE_ERROR:\n+\t\t\t\tif (rep-\u003eelements[i].restore_allowed)\n+\t\t\t\t\tret = -EIO;\n+\t\t\t\tbreak;\n+\t\t\tdefault:\n+\t\t\t\tbreak;\n+\t\t\t}\n+\t\t}\n+\n+\t\tif (!in_progress)\n+\t\t\treturn ret;\n+\t} while (!fatal_signal_pending(current));\n+\n+\treturn -EINTR;\n+}\n+\n+static int sd_zbc_restore_storage_elements(struct gendisk *disk)\n+{\n+\tstruct scsi_disk *sdkp = scsi_disk(disk);\n+\tstruct sd_zbc_se_report rep;\n+\tint ret;\n+\n+\tif (!sdkp-\u003edepop_restore_supported)\n+\t\treturn -EOPNOTSUPP;\n+\n+\t/*\n+\t * Get the resources we need to prepare for waiting for the operation to\n+\t * complete.\n+\t */\n+\tret = sd_zbc_alloc_se_report(NULL, disk-\u003enr_storage_elements,\n+\t\t\t\t true, \u0026rep);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\t/*\n+\t * Execute the restore command and wait for the elements restoration\n+\t * operation to complete.\n+\t */\n+\tret = sd_zbc_execute_storage_element_cmd(sdkp,\n+\t\t\t\t\tSAI_RESTORE_ELEMENTS_AND_REBUILD, 0);\n+\tif (!ret)\n+\t\tret = sd_zbc_wait_for_se_restore(sdkp, \u0026rep);\n+\tif (!ret)\n+\t\tsd_printk(KERN_NOTICE, sdkp, \"Restored storage elements\\n\");\n+\n+\tsd_zbc_free_se_report(\u0026rep);\n+\n+\treturn ret;\n+}\n+\n+const struct blk_se_ops sd_zbc_se_ops = {\n+\t.report_elements\t= sd_zbc_report_storage_elements,\n+\t.remove_element\t\t= sd_zbc_remove_storage_element,\n+\t.restore_elements\t= sd_zbc_restore_storage_elements,\n+};\n+\n /*\n * Call blk_revalidate_disk_zones() if any of the zoned disk properties have\n * changed that make it necessary to call that function. Called by\n@@ -545,6 +1041,7 @@ int sd_zbc_revalidate_zones(struct scsi_disk *sdkp)\n \tu32 zone_blocks = sdkp-\u003eearly_zone_info.zone_blocks;\n \tunsigned int nr_zones = sdkp-\u003eearly_zone_info.nr_zones;\n \tunsigned int flags;\n+\tbool print_zones;\n \tint ret;\n \n \t/*\n@@ -554,11 +1051,22 @@ int sd_zbc_revalidate_zones(struct scsi_disk *sdkp)\n \tif (!blk_queue_is_zoned(q))\n \t\treturn 0;\n \n+\t/*\n+\t * If the zone size and number of zones has not changed, and the disk\n+\t * does not support depopulating heads, skip the rather slow call to\n+\t * blk_revalidate_disk_zones().\n+\t */\n \tif (sdkp-\u003ezone_info.zone_blocks == zone_blocks \u0026\u0026\n \t sdkp-\u003ezone_info.nr_zones == nr_zones \u0026\u0026\n-\t disk-\u003enr_zones == nr_zones)\n+\t disk-\u003enr_zones == nr_zones \u0026\u0026\n+\t !sdkp-\u003edepop_modify_zones_supported)\n \t\treturn 0;\n \n+\tprint_zones =\n+\t\tsdkp-\u003ezone_info.zone_blocks != zone_blocks ||\n+\t\tsdkp-\u003ezone_info.nr_zones != nr_zones ||\n+\t\tdisk-\u003enr_zones != nr_zones;\n+\n \tsdkp-\u003ezone_info.zone_blocks = zone_blocks;\n \tsdkp-\u003ezone_info.nr_zones = nr_zones;\n \n@@ -571,7 +1079,8 @@ int sd_zbc_revalidate_zones(struct scsi_disk *sdkp)\n \t\treturn ret;\n \t}\n \n-\tsd_zbc_print_zones(sdkp);\n+\tif (print_zones)\n+\t\tsd_zbc_print_zones(sdkp);\n \n \treturn 0;\n }\n@@ -610,6 +1119,9 @@ int sd_zbc_read_zones(struct scsi_disk *sdkp, struct queue_limits *lim,\n \tsdkp-\u003edevice-\u003euse_10_for_rw = 0;\n \tsdkp-\u003edevice-\u003euse_16_for_sync = 1;\n \n+\t/* Check if storage element depopulation is supported. */\n+\tsd_zbc_check_depop_supported(sdkp, buf);\n+\n \t/* Check zoned block device characteristics (unconstrained reads) */\n \tret = sd_zbc_check_zoned_characteristics(sdkp, buf);\n \tif (ret)\ndiff --git a/include/linux/blkdev.h b/include/linux/blkdev.h\nindex 215753171cef0e..63c6e43ecdda30 100644\n--- a/include/linux/blkdev.h\n+++ b/include/linux/blkdev.h\n@@ -189,6 +189,11 @@ struct gendisk {\n \tstruct disk_events *ev;\n \n #ifdef CONFIG_BLK_DEV_ZONED\n+\t/*\n+\t * Total number of storage elements of the disk. This is always 0 for\n+\t * disks that do not support storage element management.\n+\t */\n+\tunsigned int\t\tnr_storage_elements;\n \t/*\n \t * Zoned block device information. Reads of this information must be\n \t * protected with blk_queue_enter() / blk_queue_exit() or by holding a\n@@ -940,6 +945,9 @@ static inline unsigned int disk_zone_capacity(struct gendisk *disk,\n \t\treturn disk-\u003elast_zone_capacity;\n \treturn disk-\u003ezone_capacity;\n }\n+\n+bool disk_zone_is_offline(struct gendisk *disk, sector_t sector);\n+\n static inline unsigned int bdev_zone_capacity(struct block_device *bdev,\n \t\t\t\t\t sector_t pos)\n {\n@@ -948,12 +956,25 @@ static inline unsigned int bdev_zone_capacity(struct block_device *bdev,\n \n bool bdev_zone_is_seq(struct block_device *bdev, sector_t sector);\n \n+int bdev_report_storage_elements(struct block_device *bdev,\n+\t\t\t\t struct blk_se *elements,\n+\t\t\t\t unsigned int *nr_elements);\n+int bdev_remove_storage_element(struct block_device *bdev,\n+\t\t\t\tunsigned int element_id);\n+int bdev_restore_storage_elements(struct block_device *bdev);\n+\n #else /* CONFIG_BLK_DEV_ZONED */\n static inline unsigned int disk_nr_zones(struct gendisk *disk)\n {\n \treturn 0;\n }\n \n+static inline bool disk_zone_is_offline(struct gendisk *disk,\n+\t\t\t\t\tsector_t sector)\n+{\n+\treturn false;\n+}\n+\n static inline bool bdev_zone_is_seq(struct block_device *bdev, sector_t sector)\n {\n \treturn false;\n@@ -968,6 +989,25 @@ static inline bool blk_zone_plug_bio(struct bio *bio, unsigned int nr_segs)\n {\n \treturn false;\n }\n+\n+static inline int bdev_report_storage_elements(struct block_device *bdev,\n+\t\t\t\t\t struct blk_se *elements,\n+\t\t\t\t\t unsigned int *nr_elements)\n+{\n+\treturn -EOPNOTSUPP;\n+}\n+\n+static inline int bdev_remove_storage_element(struct block_device *bdev,\n+\t\t\t\t\t unsigned int element_id)\n+{\n+\treturn -EOPNOTSUPP;\n+}\n+\n+static inline int bdev_restore_storage_elements(struct block_device *bdev)\n+{\n+\treturn -EOPNOTSUPP;\n+}\n+\n #endif /* CONFIG_BLK_DEV_ZONED */\n \n static inline unsigned int bdev_nr_zones(struct block_device *bdev)\n@@ -1572,6 +1612,29 @@ enum blk_unique_id {\n \tBLK_UID_NAA\t= 3,\n };\n \n+/**\n+ * struct blk_se_ops - Storage element management operations.\n+ * @report_elements: Report at most nr_elements storage elements of the\n+ *\t\t specified disk. If the elements argument is NULL, only\n+ *\t\t report the number of storage elements of the target disk.\n+ * @remove_element: Remove (depopulate) the specified storage element from the\n+ *\t\t target disk. This is a blocking operation called with\n+ *\t\t zone_revalidate_mutex held. This callback must return only\n+ *\t\t after the remove operation completes or an error occurs.\n+ * @restore_elements: Restore (re-populate), if possible, all removed storage\n+ *\t\t elements of the target disk. This operation is called with\n+ *\t\t zone_revalidate_mutex held. This callback must return only\n+ *\t\t after the restoration operation completes or an error\n+ *\t\t occurs.\n+ */\n+struct blk_se_ops {\n+\tint (*report_elements)(struct gendisk *disk,\n+\t\t\t struct blk_se *elements,\n+\t\t\t unsigned int *nr_elements);\n+\tint (*remove_element)(struct gendisk *disk, unsigned int element_id);\n+\tint (*restore_elements)(struct gendisk *disk);\n+};\n+\n struct block_device_operations {\n \tvoid (*submit_bio)(struct bio *bio);\n \tint (*poll_bio)(struct bio *bio, struct io_comp_batch *iob,\n@@ -1601,6 +1664,7 @@ struct block_device_operations {\n \t\t\t\t struct dma_buf_io_ctx *);\n \tstruct module *owner;\n \tconst struct pr_ops *pr_ops;\n+\tconst struct blk_se_ops\t*se_ops;\n \n \t/*\n \t * Special callback for probing GPT entry at a given sector.\ndiff --git a/include/scsi/scsi_proto.h b/include/scsi/scsi_proto.h\nindex 9c7d31aa6bae59..f290a214e3e06e 100644\n--- a/include/scsi/scsi_proto.h\n+++ b/include/scsi/scsi_proto.h\n@@ -408,9 +408,11 @@ enum scsi_phys_element_type {\n /* SCSI physical element health. */\n enum scsi_phys_element_health {\n \tSCSI_PHYS_ELEM_HEALTH_NOT_REPORTED\t\t= 0x00,\n-\tSCSI_PHYS_ELEM_HEALTH_WITHIN_SPEC_LIMITS\t= 0x01,\n+\tSCSI_PHYS_ELEM_HEALTH_WITHIN_SPEC_LIMITS_MIN\t= 0x01,\n+\tSCSI_PHYS_ELEM_HEALTH_WITHIN_SPEC_LIMITS_MAX\t= 0x63,\n \tSCSI_PHYS_ELEM_HEALTH_AT_SPEC_LIMITS\t\t= 0x64,\n-\tSCSI_PHYS_ELEM_HEALTH_OUTSIDE_SPEC_LIMITS\t= 0x65,\n+\tSCSI_PHYS_ELEM_HEALTH_OUTSIDE_SPEC_LIMITS_MIN\t= 0x65,\n+\tSCSI_PHYS_ELEM_HEALTH_OUTSIDE_SPEC_LIMITS_MAX\t= 0xCF,\n \tSCSI_PHYS_ELEM_HEALTH_DEPOP_REVOKE_ERR\t\t= 0xFB,\n \tSCSI_PHYS_ELEM_HEALTH_DEPOP_REVOKE_IN_PROGRESS\t= 0xFC,\n \tSCSI_PHYS_ELEM_HEALTH_DEPOP_ERR\t\t\t= 0xFD,\ndiff --git a/include/uapi/linux/blkzoned.h b/include/uapi/linux/blkzoned.h\nindex 66383612096678..6463cdec8e864e 100644\n--- a/include/uapi/linux/blkzoned.h\n+++ b/include/uapi/linux/blkzoned.h\n@@ -208,4 +208,110 @@ struct blk_zone_range {\n #define BLKFINISHZONE\t_IOW(0x12, 136, struct blk_zone_range)\n #define BLKREPORTZONEV2\t_IOWR(0x12, 142, struct blk_zone_report)\n \n+/**\n+ * enum blk_se_type - Type of a zoned device storage element.\n+ *\n+ * @BLK_SE_TYPE_RDWR: The storage element handles both reads and writes.\n+ * @BLK_SE_TYPE_READ: The storage element handles reads only.\n+ * @BLK_SE_TYPE_WRITE: The storage element handles writes only.\n+ * @BLK_SE_TYPE_UNKNOWN: The storage element type is not known.\n+ */\n+enum blk_se_type {\n+\tBLK_SE_TYPE_RDWR\t\t= 0x01,\n+\tBLK_SE_TYPE_READ\t\t= 0x02,\n+\tBLK_SE_TYPE_WRITE\t\t= 0x03,\n+\tBLK_SE_TYPE_UNKNOWN\t\t= 0xFF,\n+};\n+\n+/**\n+ * enum blk_se_status - Status of a zoned device storage element.\n+ *\n+ * @BLK_SE_STS_OK: The storage element is operating normally.\n+ * @BLK_SE_STS_OK_LIMIT: The storage element is operating at the manufacturer\n+ *\t\t\t specifications limit.\n+ * @BLK_SE_STS_DEGRADED: The storage element has degraded and is not operating\n+ *\t\t\t normally.\n+ * @BLK_SE_STS_REMOVE_IN_PROGRESS: The storage element is being removed.\n+ * @BLK_SE_STS_REMOVE_ERROR: The storage element removal failed.\n+ * @BLK_SE_STS_RESTORE_IN_PROGRESS: The storage element is being restored.\n+ * @BLK_SE_STS_RESTORE_ERROR: The storage element restoration failed.\n+ * @BLK_SE_STS_REMOVED: The storage element was removed.\n+ * @BLK_SE_STS_UNKNOWN: The storage element status is unknown.\n+ */\n+enum blk_se_status {\n+\tBLK_SE_STS_OK\t\t\t= 0x01,\n+\tBLK_SE_STS_OK_LIMIT\t\t= 0x02,\n+\tBLK_SE_STS_DEGRADED\t\t= 0x03,\n+\tBLK_SE_STS_REMOVE_IN_PROGRESS\t= 0x04,\n+\tBLK_SE_STS_REMOVE_ERROR\t\t= 0x05,\n+\tBLK_SE_STS_RESTORE_IN_PROGRESS\t= 0x06,\n+\tBLK_SE_STS_RESTORE_ERROR\t= 0x07,\n+\tBLK_SE_STS_REMOVED\t\t= 0x08,\n+\tBLK_SE_STS_UNKNOWN\t\t= 0xFF,\n+};\n+\n+/**\n+ * struct blk_se - Zoned device storage element descriptor.\n+ *\n+ * @id: The ID of the element (cannot be 0).\n+ * @paired_id: The ID of the paired element for an element that is not\n+ *\t of type BLK_SE_TYPE_RDWR.\n+ * @nr_zones: The number of zones that the storage element handles.\n+ * @type: The type of the storage element (enum blk_se_type).\n+ * @status: The health status of the storage element\n+ *\t (enum blk_se_status).\n+ * @restore_allowed: Indicate if the storage element can be restored.\n+ * @reserved: unused field to align this structure size to 32B\n+ */\n+struct blk_se {\n+\t__u32\tid;\n+\t__u32\tpaired_id;\n+\t__u64\tnr_zones;\n+\t__u8\ttype;\n+\t__u8\tstatus;\n+\t__u8\trestore_allowed;\n+\t__u8\treserved[13];\n+};\n+\n+/**\n+ * struct blk_se_report - Zoned device storage element descriptor report for the\n+ *\t\t\t BLKREPORTSTORELEMS ioctl.\n+ *\n+ * @nr_elements: on input, this indicates the size of the @elements array. On\n+ *\t\t output, this indicates the maximum number of storage elements\n+ *\t\t of the device. If this field specifies 0 on input, the array\n+ *\t\t @elements is not filled and the maximum number of storage\n+ *\t\t elements of the device returned.\n+ * @nr_reported_elements: output only to indicate the number of storage element\n+ *\t\t\t descriptors filled in the array @elements.\n+ * @reserved: reserved space for future use and structure field alignment.\n+ * @elements: array of storage element descriptors with a maximum number of\n+ *\t entries specified by @nr_elements on input and by\n+ *\t @nr_reported_elements on output.\n+ */\n+struct blk_se_report {\n+\t__u32\t\tnr_elements;\n+\t__u32\t\tnr_reported_elements;\n+\t__u64\t\treserved;\n+\tstruct blk_se\telements[];\n+};\n+\n+/**\n+ * Zoned block device storage element management ioctl's:\n+ *\n+ * @BLKREPORTSTORELEMS: Get the number of storage elements of a device and\n+ *\t\t\tthe device storage element descriptors.\n+ * @BLKREMOVESTORELEM: Remove (depopulate) one storage element of a device. This\n+ *\t\t operation will flush the target device write cache and\n+ *\t\t invalidate all cached data. So the caller must ensure\n+ *\t\t that all dirty data written with buffered IOs are written\n+ *\t\t back first to avoid any data loss.\n+ * @BLKRESTORESTORELEMS: Restore (repopulate if possible) all storage elements\n+ *\t\t\t that have been removed. This is a destructive operation\n+ *\t\t\t equivalent to reformatting the target device.\n+ */\n+#define BLKREPORTSTORELEMS _IOWR(0x12, 143, struct blk_se_report)\n+#define BLKREMOVESTORELEM _IOW(0x12, 144, __u32)\n+#define BLKRESTORESTORELEMS _IO(0x12, 145)\n+\n #endif /* _UAPI_BLKZONED_H */\ndiff --git a/include/uapi/linux/fs.h b/include/uapi/linux/fs.h\nindex a46c33692aa21a..5b6f8248a4799b 100644\n--- a/include/uapi/linux/fs.h\n+++ b/include/uapi/linux/fs.h\n@@ -309,6 +309,7 @@ struct file_attr {\n /* 130-136 and 142 are used by zoned block device ioctls (uapi/linux/blkzoned.h) */\n /* 137-141 are used by blk-crypto ioctls (uapi/linux/blk-crypto.h) */\n #define BLKTRACESETUP2 _IOWR(0x12, 142, struct blk_user_trace_setup2)\n+/* 143-145 are used by storage element management for zoned block devices. */\n \n #define BMAP_IOCTL 1\t\t/* obsolete - kept for compatibility */\n #define FIBMAP\t _IO(0x00,1)\t/* bmap access */\n"
} |
| 2/1 | 2026/11/12 05:08 | agent | patch-evaluator |
3mModel:core Args: null Results: {
"EnableConfigs": [
"BLK_DEV_ZONED",
"BLK_DEV_ZONED_LOOP",
"SCSI",
"BLK_DEV_SD"
],
"FocusSymbols": [
"blk_ioctl_zone_storage_elements",
"bdev_report_storage_elements",
"bdev_remove_storage_element",
"bdev_restore_storage_elements",
"disk_zone_is_offline",
"disk_revalidate_zones",
"disk_init_storage_element_mgmt",
"zloop_report_elements",
"zloop_remove_element",
"zloop_restore_elements",
"zloop_degrade_element",
"zloop_ctl_degrade_element",
"zloop_prep_rq",
"sd_zbc_report_storage_elements",
"sd_zbc_remove_storage_element",
"sd_zbc_restore_storage_elements",
"sd_zbc_revalidate_zones"
],
"Reasoning": "The patch series adds storage element management for zoned block devices. It introduces new user-space block ioctls (BLKREPORTSTORELEMS, BLKREMOVESTORELEM, BLKRESTORESTORELEMS) in core block layer (block/blk-zoned.c and block/ioctl.c) and implements storage element emulation in the virtual zoned loopback driver (drivers/block/zloop.c) via /dev/zloop-control as well as SCSI ZBC devices (drivers/scsi/sd_zbc.c). This adds significant state management, queue freezing, revalidation, and caching invalidation logic that is fully reachable and warrants fuzzing.",
"WorthFuzzing": true
}Instruction: You are an expert Linux kernel maintainer and security engineer.
Your job is to review a provided patch series and evaluate whether it warrants fuzzing with syzkaller.
IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in
your workspace. Do NOT rely on internal assumptions. You must actively use your code access
tools to inspect the actual source code, callers, and surrounding context.
================================================================================
1. CORE TRIAGE PHILOSOPHY
================================================================================
The goal of patch fuzzing is to discover crashes, regressions, exposed latent bugs,
and newly triggered assertions introduced by the patch series.
- REACHABILITY IS THE PRIMARY GATE:
Fuzzing can only discover bugs in code that can actually execute in standard virtualized
environments (GCE or QEMU, utilizing software-emulated devices like USB gadgets, netdev, tun/tap).
If the modified code is structurally unreachable (see Section 2), it MUST NOT be fuzzed,
regardless of whether it adds assertions or complex logic.
- DO NOT BLINDLY TRUST "NO FUNCTIONAL CHANGE" (NFCI) OR "REFACTORING" CLAIMS:
Patch authors routinely label changes as "cleanups", "refactorings", or state
"No functional change intended". Do NOT take these claims at face value.
Code refactorings that rearrange logic, introduce helper functions, or alter state management
in core subsystems frequently introduce subtle semantic shifts or uncover latent kernel bugs.
If reachable executable code is modified or refactored, it MUST be fuzzed.
- NEW OR MODIFIED ASSERTIONS IN REACHABLE CODE MUST BE FUZZED:
When a patch introduces or modifies runtime checks or assertions (e.g., WARN_ON*, VM_WARN_ON*,
BUG_ON*, lockdep_assert*) in reachable code paths, it enforces new or stricter invariants.
Even if the author believes the invariant always holds, fuzzing is essential to verify whether
an unusual sequence of operations can violate it.
================================================================================
2. WHEN TO RETURN WorthFuzzing=false (NEGATIVE CRITERIA)
================================================================================
Return WorthFuzzing=false ONLY IF all modified code falls strictly into one or more of these categories:
- Non-kernel and non-executable changes:
* Modifications to Documentation/, comments, or spelling fixes.
* User-space directories, self-tests, samples, or scripts (e.g., tools/, samples/, scripts/, usr/)
that do not affect the compiled kernel image (vmlinux) or kernel modules.
* Purely decorative logging (e.g., message strings in pr_err, printk, dev_info) or tracepoints
that do not alter control flow or data structures.
* Build system or Kconfig changes that do not alter compiled C logic.
- Structurally unreachable hardware:
* Vendor-specific PCIe switches, SmartNICs, or GPU drivers (e.g., mlxsw, pds_core, qed,
ionic, amdgpu) requiring physical ASIC/PCIe cards not emulated in standard QEMU.
- Unreachable execution paths:
* Driver teardown callbacks (.remove, .shutdown, pci_unregister_driver) executed only during
physical PCI hot-unplug or manual sysfs driver unbinding.
* Code paths exclusive to architectures other than the target architecture.
================================================================================
3. WHEN TO RETURN WorthFuzzing=true (POSITIVE CRITERIA)
================================================================================
Return WorthFuzzing=true whenever the patch touches reachable executable code, including:
- Core Subsystems:
* Any logic modifications in memory management (mm/), synchronization/locking (kernel/locking/),
BPF, scheduler, core networking, VFS, or syscall handling.
- Refactorings and Code Cleanups:
* Any restructuring of reachable data structures, helper abstractions, or algorithm flows.
- Runtime Assertions and Defensive Checks:
* Any introduction or alteration of assertions (WARN_ON*, VM_WARN_ON*, BUG_ON*, etc.) in reachable paths.
- Reachable Drivers and Protocols:
* Drivers accessible via virtual buses (virtio, USB gadget, loopback, netlink, binder, sockets, etc.).
================================================================================
4. EXTRACTING FocusSymbols (PREVENTING DILUTION)
================================================================================
When WorthFuzzing=true, you must extract specific kernel functions into FocusSymbols to guide the fuzzer:
- AVOID UBIQUITOUS LIFECYCLE HOT-PATHS:
Do NOT list generic, ubiquitous functions called by almost every program in the corpus
(including, but not limited to: general memory allocators and deallocators, page fault
and trap handlers, or core synchronization primitives; this is not an exhaustive list).
Listing ubiquitous functions causes the fuzzer to classify thousands of unrelated tests as "focused",
which severely dilutes fuzzing effort away from the actual changes.
- TARGET SPECIFIC FEATURE LOGIC AND ENTRYPOINTS:
List functions that specifically implement the logic being added or altered, or direct API entrypoints
for the subsystem feature under review.
- HANDLING STATIC INLINE FUNCTIONS IN HEADERS (.h):
Compiler-inlined static functions (such as static inlines in mm/*.h or include/linux/*.h) lack
distinct symbol addresses in vmlinux and cannot be targeted directly by symbol coverage filters.
If the changes are primarily in static inline helpers, identify non-static, feature-specific caller
functions in .c files that exercise them (avoiding ubiquitous lifecycle wrappers).
================================================================================
5. IDENTIFYING EnableConfigs
================================================================================
Identify any specific CONFIG_ options required to properly compile and reach the modified code:
- Inspect Kconfig files and #ifdef guards; do not make assumptions.
- Check "depends on" lines in Kconfig to include any non-standard parent subsystem configs needed.
- Strip any 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4").
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt: Target architecture: amd64
For your convenience, here is the diff of the changes:
commit ccd0317eb70f3d874172417cfe9ce93eeaabf238
Author: syz-cluster <triage@syzkaller.com>
Date: Thu Nov 12 05:08:32 2026 +0000
syz-cluster: applied patch under review
diff --git a/Documentation/admin-guide/blockdev/zoned_loop.rst b/Documentation/admin-guide/blockdev/zoned_loop.rst
index 64277494fb36af..d378fbdc1983ef 100644
--- a/Documentation/admin-guide/blockdev/zoned_loop.rst
+++ b/Documentation/admin-guide/blockdev/zoned_loop.rst
@@ -61,8 +61,9 @@ The options available for the add command can be listed by reading the
/dev/zloop-control device::
$ cat /dev/zloop-control
- add id=%d,capacity_mb=%u,zone_size_mb=%u,zone_capacity_mb=%u,conv_zones=%u,max_open_zones=%u,base_dir=%s,nr_queues=%u,queue_depth=%u,buffered_io,zone_append=%u,ordered_zone_append,discard_write_cache
+ add id=%d,capacity_mb=%u,zone_size_mb=%u,zone_capacity_mb=%u,conv_zones=%u,max_open_zones=%u,base_dir=%s,nr_queues=%u,queue_depth=%u,buffered_io,zone_append=%u,ordered_zone_append,discard_write_cache,stor_elements=%u
remove id=%d
+ degrade_element id=%d,element_id=%u
In more details, the options that can be used with the "add" command are as
follows.
@@ -113,6 +114,15 @@ discard_write_cache Discard all data that was not explicitly persisted using a
each zone file to the size recorded during the last flush
operation. This simulates power fail events where
uncommitted data is lost.
+stor_elements Control storage element emulation. The default value is
+ 0, indicating no emulation. A value of 1 indicates that
+ storage elements are emulated as full access physical
+ elements, which is equivalent to combined read+write heads
+ of a hard-disk. A value of 2 enables storage element
+ emulation as fractional access physical storage elements,
+ which is equivalent to pairs of read and write heads of a
+ hard-disk. The health state of storage elements is not
+ preserved across a zloop device remove and add.
=================== =========================================================
3) Deleting a Zoned Device
@@ -135,7 +145,26 @@ the zoned device will not be created.
To fully delete a zoned device, after executing the remove operation, the device
base directory containing the backing files of the device zones must be deleted.
-4) Example
+4) Degrading a storage element
+------------------------------
+
+When storage element emulation is enabled, storage elements can be set in a
+degraded state to emulate defective storage elements (e.g. defective heads of a
+hard disk). This is done by issuing the "degrade_element" command to
+/dev/zloop-control, specifying the ID of the target device and the ID of the
+storage element to degrade::
+
+ $ echo "degrade_element id=X,element_id=Y" > /dev/zloop-control
+
+Depending on the type of the storage element specified, a read or write access
+to sectors of a zone served by a degraded storage element will result in a
+failure. For partial access storage elements, write accesses are always failed
+if the write element serving the zone is degraded. Likewise, read accesses are
+failed if the read element serving the zone is degraded. When storage elements
+are emulated as all access physical elements, both read and write operations are
+failed when the storage element serving the zones being accessed is degraded.
+
+5) Example
----------
The following sequence of commands creates a 2GB zoned device with zones of 64
diff --git a/block/blk-zoned.c b/block/blk-zoned.c
index 4c588929619124..d56afbeb436a15 100644
--- a/block/blk-zoned.c
+++ b/block/blk-zoned.c
@@ -18,6 +18,7 @@
#include <linux/mempool.h>
#include <linux/kthread.h>
#include <linux/freezer.h>
+#include <linux/uaccess.h>
#include <trace/events/block.h>
@@ -292,6 +293,24 @@ static bool disk_zone_is_seq(struct gendisk *disk, sector_t sector)
return !blk_zstate_is_conv(zs);
}
+/**
+ * disk_zone_is_offline - check if a sector belongs to an offline zone
+ * @disk: gendisk to check
+ * @sector: sector number
+ *
+ * Check if @sector on @disk is contained in an offline zone.
+ * This function can be used only when the bdev queue was entered (BIOs
+ * submitted but not yet completed) or when holding zone_revalidate_mutex.
+ */
+bool disk_zone_is_offline(struct gendisk *disk, sector_t sector)
+{
+ if (!blk_queue_is_zoned(disk->queue))
+ return false;
+
+ return disk_zone_get_cond(disk, sector) == BLK_ZONE_COND_OFFLINE;
+}
+EXPORT_SYMBOL_GPL(disk_zone_is_offline);
+
/**
* bdev_zone_is_seq - check if a sector belongs to a sequential write zone
* @bdev: block device to check
@@ -2534,20 +2553,7 @@ static int blk_revalidate_zone_cb(struct blk_zone *zone, unsigned int idx,
return ret;
}
-/**
- * blk_revalidate_disk_zones - (re)allocate and initialize zone write plugs
- * @disk: Target disk
- *
- * Helper function for low-level device drivers to check, (re) allocate and
- * initialize resources used for managing zoned disks. This function should
- * normally be called by blk-mq based drivers when a zoned gendisk is probed
- * and when the zone configuration of the gendisk changes (e.g. after a format).
- * Before calling this function, the device driver must already have set the
- * device zone size (chunk_sector limit) and the max zone append limit.
- * BIO based drivers can also use this function as long as the device queue
- * can be safely frozen.
- */
-int blk_revalidate_disk_zones(struct gendisk *disk)
+static int disk_revalidate_zones(struct gendisk *disk, bool drop_all_zwplugs)
{
struct blk_revalidate_zone_args args = { };
struct blk_report_zones_args rep_args = {
@@ -2558,19 +2564,13 @@ int blk_revalidate_disk_zones(struct gendisk *disk)
int retries = 2;
int ret;
- if (WARN_ON_ONCE(!blk_queue_is_zoned(disk->queue)))
- return -EIO;
-
- /*
- * Serialize calls to this function so that we can safely look at and
- * eventually change the disk zone information.
- */
- mutex_lock(&disk->zone_revalidate_mutex);
+ if (drop_all_zwplugs)
+ disk_for_all_zone_wplugs(disk, disk_drop_zone_wplug, NULL);
again:
ret = disk_revalidate_capacity(disk, &args);
if (ret)
- goto unlock;
+ return ret;
/*
* Allocate zone resources if they are needed and we have not done
@@ -2583,14 +2583,14 @@ int blk_revalidate_disk_zones(struct gendisk *disk)
ret = disk_alloc_zone_resources(disk, args.capacity);
if (ret) {
memalloc_noio_restore(noio_flag);
- goto unlock;
+ return ret;
}
}
ret = disk_init_revalidate_args(disk, &args);
if (ret) {
memalloc_noio_restore(noio_flag);
- goto unlock;
+ return ret;
}
ret = disk->fops->report_zones(disk, 0, UINT_MAX, &rep_args);
@@ -2607,8 +2607,6 @@ int blk_revalidate_disk_zones(struct gendisk *disk)
if (ret)
goto free_args;
- mutex_unlock(&disk->zone_revalidate_mutex);
-
return 0;
free_args:
@@ -2624,7 +2622,36 @@ int blk_revalidate_disk_zones(struct gendisk *disk)
}
pr_warn("%s: failed to revalidate zones\n", disk->disk_name);
-unlock:
+
+ return ret;
+}
+
+/**
+ * blk_revalidate_disk_zones - (re)allocate and initialize zone write plugs
+ * @disk: Target disk
+ *
+ * Helper function for low-level device drivers to check, (re) allocate and
+ * initialize resources used for managing zoned disks. This function should
+ * normally be called by blk-mq based drivers when a zoned gendisk is probed
+ * and when the zone configuration of the gendisk changes (e.g. after a format).
+ * Before calling this function, the device driver must already have set the
+ * device zone size (chunk_sector limit) and the max zone append limit.
+ * BIO based drivers can also use this function as long as the device queue
+ * can be safely frozen.
+ */
+int blk_revalidate_disk_zones(struct gendisk *disk)
+{
+ int ret;
+
+ if (WARN_ON_ONCE(!blk_queue_is_zoned(disk->queue)))
+ return -EIO;
+
+ /*
+ * Serialize calls to this function so that we can safely look at and
+ * eventually change the disk zone information.
+ */
+ mutex_lock(&disk->zone_revalidate_mutex);
+ ret = disk_revalidate_zones(disk, false);
mutex_unlock(&disk->zone_revalidate_mutex);
return ret;
@@ -2710,3 +2737,370 @@ int queue_zone_wplugs_show(void *data, struct seq_file *m)
}
#endif
+
+/**
+ * bdev_report_storage_elements - report the storage elements of a zoned
+ * block device
+ * @bdev: the zoned block device to check
+ * @elements: array of storage element descriptors to fill
+ * @nr_elements: maximum size of @elements
+ *
+ * Fill at most @nr_elements storage element descriptors in the array
+ * @elements. The number of storage elements filled in the array is returned
+ * using @nr_elements. If @elements is NULL, only @nr_elements is returned to
+ * indicate the maximum number of storage elements of @bdev. This function may
+ * sleep.
+ *
+ * Returns 0 on success and a negative error code on failure.
+ */
+int bdev_report_storage_elements(struct block_device *bdev,
+ struct blk_se *elements,
+ unsigned int *nr_elements)
+{
+ struct gendisk *disk = bdev->bd_disk;
+
+ if (test_bit(GD_DEAD, &disk->state))
+ return -ENODEV;
+
+ if (!blk_queue_is_zoned(disk->queue) || !disk->nr_storage_elements)
+ return -EOPNOTSUPP;
+
+ if (!nr_elements)
+ return -EINVAL;
+
+ if (!elements) {
+ *nr_elements = disk->nr_storage_elements;
+ return 0;
+ }
+
+ *nr_elements = min(*nr_elements, disk->nr_storage_elements);
+ if (!*nr_elements)
+ return -EINVAL;
+
+ return disk->fops->se_ops->report_elements(disk, elements,
+ nr_elements);
+}
+EXPORT_SYMBOL_GPL(bdev_report_storage_elements);
+
+static int blkdev_ioctl_report_storage_elements(struct block_device *bdev,
+ void __user *argp)
+{
+ struct gendisk *disk = bdev->bd_disk;
+ struct blk_se_report *rep;
+ int ret = 0;
+
+ /*
+ * Allocate the report header with an array of elements, assuming that
+ * we need the maximum number of storage elements. In practice, we may
+ * need less than that, but this potentially unnecessary preallocation
+ * simplifies the code.
+ */
+ rep = kzalloc_flex(*rep, elements, disk->nr_storage_elements);
+ if (!rep)
+ return -ENOMEM;
+
+ if (copy_from_user(rep, argp, sizeof(struct blk_se_report))) {
+ ret = -EFAULT;
+ goto free_rep;
+ }
+
+ if (rep->reserved) {
+ ret = -EINVAL;
+ goto free_rep;
+ }
+
+ rep->nr_reported_elements =
+ min(rep->nr_elements, disk->nr_storage_elements);
+ rep->nr_elements = disk->nr_storage_elements;
+
+ if (rep->nr_reported_elements) {
+ ret = bdev_report_storage_elements(bdev, rep->elements,
+ &rep->nr_reported_elements);
+ if (ret)
+ goto free_rep;
+ }
+
+ if (copy_to_user(argp, rep,
+ struct_size(rep, elements, rep->nr_reported_elements)))
+ ret = -EFAULT;
+
+free_rep:
+ kfree(rep);
+ return ret;
+}
+
+/**
+ * bdev_remove_storage_element - Remove (depopulate) a storage element of a
+ * zoned block device
+ * @bdev: block device to operate on
+ * @element_id: ID of the storage element to remove
+ *
+ * Remove (depopulate) the storage element identified by @element_id from the
+ * block device @bdev. The caller is responsible for taking care of any
+ * necessary dirty page and device write cache flush and invalidation of cached
+ * data. This function will block until the storage element is removed or an
+ * error occurs.
+ *
+ * Returns 0 on success and a negative error code on failure.
+ */
+int bdev_remove_storage_element(struct block_device *bdev,
+ unsigned int element_id)
+{
+ struct gendisk *disk = bdev->bd_disk;
+ unsigned int memflags;
+ int ret, err;
+
+ if (test_bit(GD_DEAD, &disk->state))
+ return -ENODEV;
+
+ if (!blk_queue_is_zoned(disk->queue) || !disk->nr_storage_elements)
+ return -EOPNOTSUPP;
+
+ /* Zero is not a valid storage element ID. */
+ if (!element_id)
+ return -EINVAL;
+
+ /*
+ * Serialize against concurrent revalidation and concurrent storage
+ * element management operations so that we can see consistent zone
+ * condition changes.
+ */
+ mutex_lock(&disk->zone_revalidate_mutex);
+
+ /*
+ * Freeze and unfreeze the queue to flush any outstanding command.
+ * The caller is responsible for not queuing up more I/Os by higher
+ * level means.
+ */
+ memflags = blk_mq_freeze_queue(disk->queue);
+ blk_mq_unfreeze_queue(disk->queue, memflags);
+
+ /* Initiate the storage element removal and wait for its completion. */
+ ret = disk->fops->se_ops->remove_element(disk, element_id);
+
+ /*
+ * Always revalidate zones, even in the case of an error as we do not
+ * know what condition the zones of the block device are in.
+ */
+ err = disk_revalidate_zones(disk, false);
+ if (err && !ret)
+ ret = err;
+
+ mutex_unlock(&disk->zone_revalidate_mutex);
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(bdev_remove_storage_element);
+
+static int blkdev_ioctl_remove_storage_element(struct block_device *bdev,
+ blk_mode_t mode,
+ unsigned int __user *argp)
+{
+ unsigned int element_id;
+ int ret;
+
+ /*
+ * Note: we require an O_EXCL open here so that a user cannot trigger
+ * a storage element removal from user space under a mounted file system.
+ * An FS that wants to manage storage element can do so using the
+ * function bdev_remove_storage_element() from kernel context.
+ */
+ if (!(mode & BLK_OPEN_EXCL))
+ return -EBADF;
+ if (!(mode & BLK_OPEN_WRITE))
+ return -EBADF;
+ if (bdev_read_only(bdev))
+ return -EPERM;
+
+ if (get_user(element_id, argp))
+ return -EFAULT;
+
+ /*
+ * Sync the device dirty data, flush the volatile write cache, and
+ * invalidate all cached data so that reads do not return old data for
+ * zones that went offline.
+ * Since a user can only successfully write to sequential zones of a
+ * zoned devices using direct IOs, the sync should only handle dirty
+ * data from buffered writes to conventional zones, for devices that
+ * have such zones.
+ */
+ ret = sync_blockdev(bdev);
+ if (ret)
+ return ret;
+
+ inode_lock(bdev->bd_mapping->host);
+ filemap_invalidate_lock(bdev->bd_mapping);
+
+ ret = blkdev_issue_flush(bdev->bd_disk->part0);
+ if (ret)
+ goto unlock;
+
+ ret = truncate_bdev_range(bdev, mode, 0, bdev_nr_bytes(bdev) - 1);
+ if (ret)
+ goto unlock;
+
+ ret = bdev_remove_storage_element(bdev, element_id);
+
+unlock:
+ filemap_invalidate_unlock(bdev->bd_mapping);
+ inode_unlock(bdev->bd_mapping->host);
+
+ return ret;
+}
+
+/**
+ * bdev_restore_storage_elements - Restore all depopulated storage elements of
+ * a zoned block device
+ * @bdev: block device to operate on
+ *
+ * Restore all storage elements of @bdev that have been depopulated. Not all
+ * elements may be restored by this operation. This operation is destructive and
+ * will result in all sequential zones to be transitioned to the empty condition.
+ * The caller is responsible for taking care of any necessary invalidation of
+ * cached data. This function will block until the storage elements are restored
+ * or an error occurs.
+ *
+ * Returns 0 on success and a negative error code on failure.
+ */
+int bdev_restore_storage_elements(struct block_device *bdev)
+{
+ struct gendisk *disk = bdev->bd_disk;
+ unsigned int memflags;
+ int ret, err;
+
+ if (test_bit(GD_DEAD, &disk->state))
+ return -ENODEV;
+
+ if (!blk_queue_is_zoned(disk->queue) || !disk->nr_storage_elements)
+ return -EOPNOTSUPP;
+
+ /*
+ * Serialize against concurrent revalidation and concurrent storage
+ * element management operations so that we can see consistent zone
+ * condition changes.
+ */
+ mutex_lock(&disk->zone_revalidate_mutex);
+
+ /*
+ * Freeze and unfreeze the queue to flush any outstanding commands.
+ * The caller is responsible for not queuing up more I/O by higher
+ * level means.
+ */
+ memflags = blk_mq_freeze_queue(disk->queue);
+ blk_mq_unfreeze_queue(disk->queue, memflags);
+
+ /*
+ * Initiate the storage elements restoration and wait for its
+ * completion.
+ */
+ ret = disk->fops->se_ops->restore_elements(disk);
+
+ /*
+ * Always revalidate zones, even in the case of an error as we do not
+ * know what condition the zones of the block device are in. Note that
+ * since all sequential zones should have been transitioned to the empty
+ * condition, we can drop all zone write plugs. If an error occured and
+ * not all sequential zones are empty, the revalidation will reinstate
+ * the write plugs for these zones.
+ */
+ err = disk_revalidate_zones(disk, true);
+ if (err && !ret)
+ ret = err;
+
+ mutex_unlock(&disk->zone_revalidate_mutex);
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(bdev_restore_storage_elements);
+
+static int blkdev_ioctl_restore_storage_elements(struct block_device *bdev,
+ blk_mode_t mode)
+{
+ int ret;
+
+ /*
+ * Note: because restoring storage elements is a destructive operation
+ * equivalent to reformatting the device, we require an O_EXCL open here
+ * so that a user cannot trigger with another active user (e.g. a
+ * mounted file system).
+ */
+ if (!(mode & BLK_OPEN_EXCL))
+ return -EBADF;
+ if (!(mode & BLK_OPEN_WRITE))
+ return -EBADF;
+ if (bdev_read_only(bdev))
+ return -EPERM;
+
+ inode_lock(bdev->bd_mapping->host);
+ filemap_invalidate_lock(bdev->bd_mapping);
+
+ /*
+ * Storage element restoration is a destructive operation that will
+ * reset all zones. So invalidate all cached data that we may have so
+ * that old data cannot be read once the operation completes.
+ */
+ ret = truncate_bdev_range(bdev, mode, 0, bdev_nr_bytes(bdev) - 1);
+ if (!ret)
+ ret = bdev_restore_storage_elements(bdev);
+
+ filemap_invalidate_unlock(bdev->bd_mapping);
+ inode_unlock(bdev->bd_mapping->host);
+
+ return ret;
+}
+
+int disk_init_storage_element_mgmt(struct gendisk *disk)
+{
+ int ret;
+
+ /*
+ * Storage element operations are for zoned disks only and are
+ * optional...
+ */
+ if (!blk_queue_is_zoned(disk->queue) || !disk->fops->se_ops)
+ return 0;
+
+ /* ...but if defined, all operation callbacks must be defined. */
+ if (!disk->fops->se_ops->report_elements ||
+ !disk->fops->se_ops->remove_element ||
+ !disk->fops->se_ops->restore_elements) {
+ pr_err("%s: invalid storage element operations\n",
+ disk->disk_name);
+ return -EINVAL;
+ }
+
+ /*
+ * Get the number of storage elements of the disk. If this fails,
+ * do not report an error as the failure may be because the zoned disk
+ * does not support storage element management.
+ */
+ ret = disk->fops->se_ops->report_elements(disk, NULL,
+ &disk->nr_storage_elements);
+ if (ret)
+ disk->nr_storage_elements = 0;
+
+ return 0;
+}
+
+int blk_ioctl_zone_storage_elements(struct block_device *bdev,
+ blk_mode_t mode, unsigned int cmd,
+ void __user *argp)
+{
+ struct gendisk *disk = bdev->bd_disk;
+
+ if (!disk->nr_storage_elements)
+ return -ENOTTY;
+
+ switch (cmd) {
+ case BLKREPORTSTORELEMS:
+ return blkdev_ioctl_report_storage_elements(bdev, argp);
+ case BLKREMOVESTORELEM:
+ return blkdev_ioctl_remove_storage_element(bdev, mode, argp);
+ case BLKRESTORESTORELEMS:
+ return blkdev_ioctl_restore_storage_elements(bdev, mode);
+ default:
+ break;
+ }
+
+ return -ENOTTY;
+}
diff --git a/block/blk.h b/block/blk.h
index 0e38e4b88cf544..44876b148237c5 100644
--- a/block/blk.h
+++ b/block/blk.h
@@ -543,6 +543,7 @@ static inline void ioc_clear_queue(struct request_queue *q)
#endif /* CONFIG_BLK_ICQ */
#ifdef CONFIG_BLK_DEV_ZONED
+int disk_init_storage_element_mgmt(struct gendisk *disk);
void disk_init_zone_resources(struct gendisk *disk);
void disk_release_zone_resources(struct gendisk *disk);
static inline bool bio_zone_write_plugging(struct bio *bio)
@@ -594,7 +595,14 @@ int blkdev_report_zones_ioctl(struct block_device *bdev, unsigned int cmd,
int blkdev_zone_mgmt_ioctl(struct block_device *bdev, blk_mode_t mode,
unsigned int cmd, unsigned long arg);
bool bdev_zone_mgmt_allowed(struct block_device *bdev, sector_t sector);
+int blk_ioctl_zone_storage_elements(struct block_device *bdev,
+ blk_mode_t mode, unsigned int cmd,
+ void __user *argp);
#else /* CONFIG_BLK_DEV_ZONED */
+static inline int disk_init_storage_element_mgmt(struct gendisk *disk)
+{
+ return 0;
+}
static inline void disk_init_zone_resources(struct gendisk *disk)
{
}
@@ -641,6 +649,12 @@ static inline bool bdev_zone_mgmt_allowed(struct block_device *bdev,
{
return false;
}
+static inline int blk_ioctl_zone_storage_elements(struct block_device *bdev,
+ blk_mode_t mode, unsigned int cmd,
+ void __user *argp)
+{
+ return -ENOTTY;
+}
#endif /* CONFIG_BLK_DEV_ZONED */
struct block_device *bdev_alloc(struct gendisk *disk, u8 partno);
diff --git a/block/genhd.c b/block/genhd.c
index 69204d44adae11..251a5b07baff9c 100644
--- a/block/genhd.c
+++ b/block/genhd.c
@@ -435,6 +435,10 @@ static int __add_disk(struct device *parent, struct gendisk *disk,
if (WARN_ON_ONCE(bdev_nr_sectors(disk->part0) > BLK_DEV_MAX_SECTORS))
return -EINVAL;
+ ret = disk_init_storage_element_mgmt(disk);
+ if (ret)
+ return ret;
+
if (queue_is_mq(disk->queue)) {
/*
* ->submit_bio and ->poll_bio are bypassed for blk-mq drivers.
diff --git a/block/ioctl.c b/block/ioctl.c
index fb2f4b4d2505ed..0f043363048ca6 100644
--- a/block/ioctl.c
+++ b/block/ioctl.c
@@ -679,6 +679,10 @@ static int blkdev_common_ioctl(struct block_device *bdev, blk_mode_t mode,
return put_uint(argp, bdev_zone_sectors(bdev));
case BLKGETNRZONES:
return put_uint(argp, bdev_nr_zones(bdev));
+ case BLKREPORTSTORELEMS:
+ case BLKREMOVESTORELEM:
+ case BLKRESTORESTORELEMS:
+ return blk_ioctl_zone_storage_elements(bdev, mode, cmd, argp);
case BLKROGET:
return put_int(argp, bdev_read_only(bdev) != 0);
case BLKSSZGET: /* get block device logical block size */
diff --git a/drivers/block/zloop.c b/drivers/block/zloop.c
index 96666742523bb7..c4a0a845a0963d 100644
--- a/drivers/block/zloop.c
+++ b/drivers/block/zloop.c
@@ -18,6 +18,7 @@
#include <linux/parser.h>
#include <linux/seq_file.h>
#include <linux/xattr.h>
+#include <linux/delay.h>
/*
* Options for adding (and removing) a device.
@@ -37,6 +38,8 @@ enum {
ZLOOP_OPT_ORDERED_ZONE_APPEND = (1 << 10),
ZLOOP_OPT_DISCARD_WRITE_CACHE = (1 << 11),
ZLOOP_OPT_MAX_OPEN_ZONES = (1 << 12),
+ ZLOOP_OPT_STOR_ELEMENTS = (1 << 13),
+ ZLOOP_OPT_ELEMENT_ID = (1 << 14),
};
static const match_table_t zloop_opt_tokens = {
@@ -51,11 +54,23 @@ static const match_table_t zloop_opt_tokens = {
{ ZLOOP_OPT_BUFFERED_IO, "buffered_io" },
{ ZLOOP_OPT_ZONE_APPEND, "zone_append=%u" },
{ ZLOOP_OPT_ORDERED_ZONE_APPEND, "ordered_zone_append" },
- { ZLOOP_OPT_DISCARD_WRITE_CACHE, "discard_write_cache" },
+ { ZLOOP_OPT_DISCARD_WRITE_CACHE, "discard_write_cache" },
{ ZLOOP_OPT_MAX_OPEN_ZONES, "max_open_zones=%u" },
+ { ZLOOP_OPT_STOR_ELEMENTS, "stor_elements=%u" },
+ { ZLOOP_OPT_ELEMENT_ID, "element_id=%u" },
{ ZLOOP_OPT_ERR, NULL }
};
+/* Storage elements emulation types. */
+enum zloop_stor_elements {
+ /* No emulation. */
+ ZLOOP_STOR_ELEMENTS_NONE,
+ /* Emulate read+write storage elements. */
+ ZLOOP_STOR_ELEMENTS_RDWR,
+ /* Emulate pairs of associated read and write storage elements. */
+ ZLOOP_STOR_ELEMENTS_PAIRS,
+};
+
/* Default values for the "add" operation. */
#define ZLOOP_DEF_ID -1
#define ZLOOP_DEF_ZONE_SIZE ((256ULL * SZ_1M) >> SECTOR_SHIFT)
@@ -68,6 +83,8 @@ static const match_table_t zloop_opt_tokens = {
#define ZLOOP_DEF_BUFFERED_IO false
#define ZLOOP_DEF_ZONE_APPEND true
#define ZLOOP_DEF_ORDERED_ZONE_APPEND false
+#define ZLOOP_DEF_STOR_ELEMENTS ZLOOP_STOR_ELEMENTS_NONE
+#define ZLOOP_DEF_ELEMENT_ID 0
/* Arbitrary limit on the zone size (16GB). */
#define ZLOOP_MAX_ZONE_SIZE_MB 16384
@@ -87,6 +104,8 @@ struct zloop_options {
bool zone_append;
bool ordered_zone_append;
bool discard_write_cache;
+ enum zloop_stor_elements stor_elements;
+ unsigned int element_id;
};
/*
@@ -117,6 +136,8 @@ struct zloop_zone {
enum blk_zone_cond cond;
sector_t start;
sector_t wp;
+ unsigned int wr_se_id;
+ unsigned int rd_se_id;
gfp_t old_gfp_mask;
};
@@ -133,6 +154,7 @@ struct zloop_device {
bool zone_append;
bool ordered_zone_append;
bool discard_write_cache;
+ enum zloop_stor_elements stor_elements;
const char *base_dir;
struct file *data_dir;
@@ -150,6 +172,14 @@ struct zloop_device {
struct list_head open_zones_lru_list;
unsigned int nr_open_zones;
+ /* For storage elements emulation. */
+ struct mutex stor_elements_lock;
+ unsigned int nr_elements;
+ unsigned int nr_removed_elements;
+ unsigned int remove_element_id;
+ bool restore_in_progress;
+ struct blk_se *elements;
+
struct zloop_zone zones[] __counted_by(nr_zones);
};
@@ -289,22 +319,64 @@ static bool zloop_do_open_zone(struct zloop_device *zlo,
}
}
-static void zloop_mark_full(struct zloop_device *zlo, struct zloop_zone *zone)
+static void zloop_set_zone_cond(struct zloop_device *zlo,
+ struct zloop_zone *zone,
+ enum blk_zone_cond cond)
{
lockdep_assert_held(&zone->wp_lock);
zloop_lru_remove_open_zone(zlo, zone);
- zone->cond = BLK_ZONE_COND_FULL;
- zone->wp = ULLONG_MAX;
+ zone->cond = cond;
+ if (cond == BLK_ZONE_COND_EMPTY)
+ zone->wp = zone->start;
+ else
+ zone->wp = ULLONG_MAX;
+}
+
+static inline void zloop_set_zone_full(struct zloop_device *zlo,
+ struct zloop_zone *zone)
+{
+ zloop_set_zone_cond(zlo, zone, BLK_ZONE_COND_FULL);
+}
+
+static inline void zloop_set_zone_empty(struct zloop_device *zlo,
+ struct zloop_zone *zone)
+{
+ zloop_set_zone_cond(zlo, zone, BLK_ZONE_COND_EMPTY);
}
-static void zloop_mark_empty(struct zloop_device *zlo, struct zloop_zone *zone)
+static bool zloop_zone_element_is_ok(struct zloop_device *zlo,
+ struct zloop_zone *zone, bool for_write)
{
+ struct blk_se *se;
+
lockdep_assert_held(&zone->wp_lock);
- zloop_lru_remove_open_zone(zlo, zone);
- zone->cond = BLK_ZONE_COND_EMPTY;
- zone->wp = zone->start;
+ if (!zlo->nr_elements)
+ return true;
+
+ if (for_write)
+ se = &zlo->elements[zone->wr_se_id - 1];
+ else
+ se = &zlo->elements[zone->rd_se_id - 1];
+
+ return READ_ONCE(se->status) == BLK_SE_STS_OK;
+}
+
+static bool zloop_zone_is_ok(struct zloop_device *zlo, struct zloop_zone *zone)
+{
+ bool ret;
+
+ spin_lock(&zone->wp_lock);
+
+ ret = zloop_zone_element_is_ok(zlo, zone, true);
+ if (ret)
+ ret = zone->cond != BLK_ZONE_COND_OFFLINE &&
+ zone->cond != BLK_ZONE_COND_READONLY;
+
+ spin_unlock(&zone->wp_lock);
+
+ return ret;
}
static int zloop_update_seq_zone(struct zloop_device *zlo, unsigned int zone_no)
@@ -316,6 +388,15 @@ static int zloop_update_seq_zone(struct zloop_device *zlo, unsigned int zone_no)
lockdep_assert_held(&zone->lock);
+ /*
+ * If the zone is served by an element that is not in a good state, fail
+ * the operation.
+ */
+ if (!zloop_zone_is_ok(zlo, zone)) {
+ set_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags);
+ return -EIO;
+ }
+
ret = vfs_getattr(&zone->file->f_path, &stat, STATX_SIZE, 0);
if (ret < 0) {
pr_err("%pg: Failed to get zone %u file stat (err=%d)\n",
@@ -340,9 +421,9 @@ static int zloop_update_seq_zone(struct zloop_device *zlo, unsigned int zone_no)
spin_lock(&zone->wp_lock);
if (!file_sectors) {
- zloop_mark_empty(zlo, zone);
+ zloop_set_zone_empty(zlo, zone);
} else if (file_sectors == zlo->zone_capacity) {
- zloop_mark_full(zlo, zone);
+ zloop_set_zone_full(zlo, zone);
} else {
if (zone->cond != BLK_ZONE_COND_IMP_OPEN &&
zone->cond != BLK_ZONE_COND_EXP_OPEN)
@@ -364,6 +445,11 @@ static int zloop_open_zone(struct zloop_device *zlo, unsigned int zone_no)
mutex_lock(&zone->lock);
+ if (!zloop_zone_is_ok(zlo, zone)) {
+ ret = -EIO;
+ goto unlock;
+ }
+
if (test_and_clear_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags)) {
ret = zloop_update_seq_zone(zlo, zone_no);
if (ret)
@@ -389,6 +475,11 @@ static int zloop_close_zone(struct zloop_device *zlo, unsigned int zone_no)
mutex_lock(&zone->lock);
+ if (!zloop_zone_is_ok(zlo, zone)) {
+ ret = -EIO;
+ goto unlock;
+ }
+
if (test_and_clear_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags)) {
ret = zloop_update_seq_zone(zlo, zone_no);
if (ret)
@@ -421,7 +512,22 @@ static int zloop_close_zone(struct zloop_device *zlo, unsigned int zone_no)
return ret;
}
-static int zloop_reset_zone(struct zloop_device *zlo, unsigned int zone_no)
+static int zloop_do_reset_zone(struct zloop_device *zlo,
+ struct zloop_zone *zone)
+{
+ if (vfs_truncate(&zone->file->f_path, 0))
+ return -EIO;
+
+ spin_lock(&zone->wp_lock);
+ zloop_set_zone_empty(zlo, zone);
+ clear_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags);
+ spin_unlock(&zone->wp_lock);
+
+ return 0;
+}
+
+static int zloop_reset_zone(struct zloop_device *zlo, unsigned int zone_no,
+ bool all_zones)
{
struct zloop_zone *zone = &zlo->zones[zone_no];
int ret = 0;
@@ -431,20 +537,19 @@ static int zloop_reset_zone(struct zloop_device *zlo, unsigned int zone_no)
mutex_lock(&zone->lock);
+ if (!zloop_zone_is_ok(zlo, zone)) {
+ if (!all_zones)
+ ret = -EIO;
+ goto unlock;
+ }
+
if (!test_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags) &&
zone->cond == BLK_ZONE_COND_EMPTY)
goto unlock;
- if (vfs_truncate(&zone->file->f_path, 0)) {
+ ret = zloop_do_reset_zone(zlo, zone);
+ if (ret)
set_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags);
- ret = -EIO;
- goto unlock;
- }
-
- spin_lock(&zone->wp_lock);
- zloop_mark_empty(zlo, zone);
- clear_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags);
- spin_unlock(&zone->wp_lock);
unlock:
mutex_unlock(&zone->lock);
@@ -458,7 +563,7 @@ static int zloop_reset_all_zones(struct zloop_device *zlo)
int ret;
for (i = zlo->nr_conv_zones; i < zlo->nr_zones; i++) {
- ret = zloop_reset_zone(zlo, i);
+ ret = zloop_reset_zone(zlo, i, true);
if (ret)
return ret;
}
@@ -476,6 +581,11 @@ static int zloop_finish_zone(struct zloop_device *zlo, unsigned int zone_no)
mutex_lock(&zone->lock);
+ if (!zloop_zone_is_ok(zlo, zone)) {
+ ret = -EIO;
+ goto unlock;
+ }
+
if (!test_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags) &&
zone->cond == BLK_ZONE_COND_FULL)
goto unlock;
@@ -488,7 +598,7 @@ static int zloop_finish_zone(struct zloop_device *zlo, unsigned int zone_no)
}
spin_lock(&zone->wp_lock);
- zloop_mark_full(zlo, zone);
+ zloop_set_zone_full(zlo, zone);
clear_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags);
spin_unlock(&zone->wp_lock);
@@ -632,7 +742,7 @@ static int zloop_seq_write_prep(struct zloop_cmd *cmd)
if (!is_append || !zlo->ordered_zone_append) {
zone->wp += nr_sectors;
if (zone->wp == zone_end)
- zloop_mark_full(zlo, zone);
+ zloop_set_zone_full(zlo, zone);
}
out_unlock:
spin_unlock(&zone->wp_lock);
@@ -774,7 +884,7 @@ static void zloop_handle_cmd(struct zloop_cmd *cmd)
cmd->ret = zloop_flush(zlo);
break;
case REQ_OP_ZONE_RESET:
- cmd->ret = zloop_reset_zone(zlo, rq_zone_no(rq));
+ cmd->ret = zloop_reset_zone(zlo, rq_zone_no(rq), false);
break;
case REQ_OP_ZONE_RESET_ALL:
cmd->ret = zloop_reset_all_zones(zlo);
@@ -885,30 +995,65 @@ static void zloop_complete_rq(struct request *rq)
blk_mq_end_request(rq, sts);
}
-static bool zloop_set_zone_append_sector(struct request *rq)
+static bool zloop_set_zone_append_sector(struct zloop_device *zlo,
+ struct zloop_zone *zone,
+ struct request *rq)
{
- struct zloop_device *zlo = rq->q->queuedata;
- unsigned int zone_no = rq_zone_no(rq);
- struct zloop_zone *zone = &zlo->zones[zone_no];
sector_t zone_end = zone->start + zlo->zone_capacity;
sector_t nr_sectors = blk_rq_sectors(rq);
- spin_lock(&zone->wp_lock);
-
if (zone->cond == BLK_ZONE_COND_FULL ||
- zone->wp + nr_sectors > zone_end) {
- spin_unlock(&zone->wp_lock);
+ zone->wp + nr_sectors > zone_end)
return false;
- }
rq->__sector = zone->wp;
zone->wp += blk_rq_sectors(rq);
if (zone->wp >= zone_end)
- zloop_mark_full(zlo, zone);
+ zloop_set_zone_full(zlo, zone);
+
+ return true;
+}
+
+static bool zloop_prep_rq(struct zloop_device *zlo, struct request *rq)
+{
+ struct zloop_zone *zone = &zlo->zones[rq_zone_no(rq)];
+ bool is_write = op_is_write(req_op(rq));
+ bool ret = true;
+
+ if (!zlo->nr_elements &&
+ (!zlo->ordered_zone_append || req_op(rq) != REQ_OP_ZONE_APPEND))
+ return true;
+
+ spin_lock(&zone->wp_lock);
+ /*
+ * If storage element emulation is enabled, check the health state of
+ * the storage element serving the zone and the zone condition: do not
+ * allow any operation to zones served by an unusable storage element,
+ * operations to offline zones, and allow only read operations to
+ * read-only zones.
+ */
+ if (zlo->nr_elements) {
+ if (!zloop_zone_element_is_ok(zlo, zone, is_write) ||
+ zone->cond == BLK_ZONE_COND_OFFLINE ||
+ (zone->cond == BLK_ZONE_COND_READONLY && is_write)) {
+ ret = false;
+ goto unlock;
+ }
+ }
+
+ /*
+ * If we need to strongly order zone append operations, set the request
+ * sector to the zone write pointer location now instead of when the
+ * command work runs.
+ */
+ if (zlo->ordered_zone_append && req_op(rq) == REQ_OP_ZONE_APPEND)
+ ret = zloop_set_zone_append_sector(zlo, zone, rq);
+
+unlock:
spin_unlock(&zone->wp_lock);
- return true;
+ return ret;
}
static blk_status_t zloop_queue_rq(struct blk_mq_hw_ctx *hctx,
@@ -924,13 +1069,27 @@ static blk_status_t zloop_queue_rq(struct blk_mq_hw_ctx *hctx,
}
/*
- * If we need to strongly order zone append operations, set the request
- * sector to the zone write pointer location now instead of when the
- * command work runs.
+ * Do not allow commands while the restoration of storage elements is
+ * in progress.
+ */
+ if (READ_ONCE(zlo->restore_in_progress))
+ return BLK_STS_IOERR;
+
+ /*
+ * Check if we can accept data access commands and prepare zone append.
+ * If storage element emulation is enabled, this will check if the data
+ * access can be allowed based on the health state of the storage
+ * element assigned to the zone being accessed.
*/
- if (zlo->ordered_zone_append && req_op(rq) == REQ_OP_ZONE_APPEND) {
- if (!zloop_set_zone_append_sector(rq))
+ switch (req_op(rq)) {
+ case REQ_OP_READ:
+ case REQ_OP_WRITE:
+ case REQ_OP_ZONE_APPEND:
+ if (!zloop_prep_rq(zlo, rq))
return BLK_STS_IOERR;
+ break;
+ default:
+ break;
}
blk_mq_start_request(rq);
@@ -992,8 +1151,8 @@ static int zloop_report_zones(struct gendisk *disk, sector_t sector,
blkz.len = zlo->zone_size;
spin_lock(&zone->wp_lock);
blkz.wp = zone->wp;
- spin_unlock(&zone->wp_lock);
blkz.cond = zone->cond;
+ spin_unlock(&zone->wp_lock);
if (test_bit(ZLOOP_ZONE_CONV, &zone->flags)) {
blkz.type = BLK_ZONE_TYPE_CONVENTIONAL;
blkz.capacity = zlo->zone_size;
@@ -1012,6 +1171,356 @@ static int zloop_report_zones(struct gendisk *disk, sector_t sector,
return nr_zones;
}
+static int zloop_report_elements(struct gendisk *disk,
+ struct blk_se *elements,
+ unsigned int *nr_elements)
+{
+ struct zloop_device *zlo = disk->private_data;
+ struct blk_se *se;
+ unsigned int i;
+
+ if (test_bit(GD_DEAD, &disk->state) ||
+ data_race(READ_ONCE(zlo->state)) == Zlo_deleting)
+ return -ENODEV;
+
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_NONE)
+ return -EOPNOTSUPP;
+
+ mutex_lock(&zlo->stor_elements_lock);
+
+ if (!elements) {
+ *nr_elements = zlo->nr_elements;
+ goto unlock;
+ }
+
+ *nr_elements = min(*nr_elements, zlo->nr_elements);
+ for (i = 0, se = zlo->elements; i < *nr_elements; i++, se++) {
+ switch (READ_ONCE(se->status)) {
+ case BLK_SE_STS_REMOVED:
+ case BLK_SE_STS_RESTORE_ERROR:
+ se->restore_allowed = 1;
+ break;
+ default:
+ se->restore_allowed = 0;
+ }
+ memcpy(&elements[i], se, sizeof(struct blk_se));
+ }
+
+unlock:
+ mutex_unlock(&zlo->stor_elements_lock);
+
+ return 0;
+}
+
+static int zloop_degrade_element(struct zloop_device *zlo,
+ unsigned int element_id)
+{
+ struct blk_se *se;
+ int ret = 0;
+
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_NONE)
+ return -EOPNOTSUPP;
+
+ if (!element_id || element_id > zlo->nr_elements)
+ return -EINVAL;
+
+ mutex_lock(&zlo->stor_elements_lock);
+
+ se = &zlo->elements[element_id - 1];
+ if (READ_ONCE(se->status) == BLK_SE_STS_OK)
+ WRITE_ONCE(se->status, BLK_SE_STS_DEGRADED);
+ else
+ ret = -EINVAL;
+
+ mutex_unlock(&zlo->stor_elements_lock);
+
+ return ret;
+}
+
+static int zloop_execute_remove_element(struct zloop_device *zlo)
+{
+ struct gendisk *disk = zlo->disk;
+ struct blk_se *se, *paired_se = NULL;
+ enum blk_zone_cond cond;
+ struct zloop_zone *zone;
+ unsigned int memflags, i;
+ int ret = 0;
+
+ /* Wait for all IOs to complete. */
+ memflags = blk_mq_freeze_queue(disk->queue);
+ blk_mq_unfreeze_queue(disk->queue, memflags);
+
+ /*
+ * If the element to remove is a read element, zones must go offline and
+ * the associated write element is also removed.
+ */
+ se = &zlo->elements[zlo->remove_element_id - 1];
+ switch (se->type) {
+ case BLK_SE_TYPE_RDWR:
+ cond = BLK_ZONE_COND_OFFLINE;
+ break;
+ case BLK_SE_TYPE_READ:
+ cond = BLK_ZONE_COND_OFFLINE;
+ paired_se = &zlo->elements[se->paired_id - 1];
+ break;
+ case BLK_SE_TYPE_WRITE:
+ cond = BLK_ZONE_COND_READONLY;
+ break;
+ default:
+ WARN_ON_ONCE(1);
+ return -EINVAL;
+ }
+
+ /*
+ * Change the condition of the zones owned by the (pair of) elements
+ * being removed and mark the elements removed.
+ */
+ for (i = 0, zone = zlo->zones; i < zlo->nr_zones; i++, zone++) {
+ if (zone->wr_se_id != se->id && zone->rd_se_id != se->id)
+ continue;
+ mutex_lock(&zone->lock);
+ spin_lock(&zone->wp_lock);
+ zloop_set_zone_cond(zlo, zone, cond);
+ clear_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags);
+ spin_unlock(&zone->wp_lock);
+ mutex_unlock(&zone->lock);
+ }
+
+ WRITE_ONCE(se->status, BLK_SE_STS_REMOVED);
+ zlo->nr_removed_elements++;
+ if (paired_se && READ_ONCE(paired_se->status) != BLK_SE_STS_REMOVED) {
+ WRITE_ONCE(paired_se->status, BLK_SE_STS_REMOVED);
+ zlo->nr_removed_elements++;
+ }
+
+ return ret;
+}
+
+static int zloop_remove_element(struct gendisk *disk, unsigned int element_id)
+{
+ struct zloop_device *zlo = disk->private_data;
+ struct blk_se *se, *paired_se = NULL;
+ unsigned int nr_remove = 1;
+ int ret = 0;
+
+ if (test_bit(GD_DEAD, &disk->state) ||
+ data_race(READ_ONCE(zlo->state)) == Zlo_deleting)
+ return -ENODEV;
+
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_NONE)
+ return -EOPNOTSUPP;
+
+ if (!element_id || element_id > zlo->nr_elements)
+ return -EINVAL;
+
+ mutex_lock(&zlo->stor_elements_lock);
+
+ /* If a remove or restore operation is already in progress, bail out. */
+ if (zlo->remove_element_id || READ_ONCE(zlo->restore_in_progress)) {
+ ret = -EBUSY;
+ goto unlock;
+ }
+
+ /*
+ * Get the element to remove. If it is already removed, we have nothing
+ * to do.
+ */
+ se = &zlo->elements[element_id - 1];
+ if (READ_ONCE(se->status) == BLK_SE_STS_REMOVED)
+ goto unlock;
+
+ /*
+ * If the element to remove is a read element, the associated write
+ * element must also be removed.
+ */
+ if (se->type == BLK_SE_TYPE_READ) {
+ paired_se = &zlo->elements[se->paired_id - 1];
+ if (READ_ONCE(paired_se->status) != BLK_SE_STS_REMOVED)
+ nr_remove = 2;
+ }
+
+ /*
+ * Make sure we do not allow removing all storage elements as that does
+ * not make any sense. This is consistent with the device advertized
+ * limit of SCSI and ATA devices supporting the storage element
+ * depopulation feature.
+ */
+ if (zlo->nr_removed_elements + nr_remove >= zlo->nr_elements) {
+ ret = -EINVAL;
+ goto unlock;
+ }
+
+ /*
+ * Change the elements to be removed to the remove in progress state so
+ * that concurrent report elements can see the operation on-going.
+ */
+ WRITE_ONCE(se->status, BLK_SE_STS_REMOVE_IN_PROGRESS);
+ if (paired_se && READ_ONCE(paired_se->status) != BLK_SE_STS_REMOVED)
+ WRITE_ONCE(paired_se->status, BLK_SE_STS_REMOVE_IN_PROGRESS);
+ zlo->remove_element_id = element_id;
+
+ mutex_unlock(&zlo->stor_elements_lock);
+
+ /*
+ * Wait a little so that this looks like a real device taking time to
+ * operate.
+ */
+ msleep(2000);
+
+ mutex_lock(&zlo->stor_elements_lock);
+
+ /* Remove the element(s). */
+ ret = zloop_execute_remove_element(zlo);
+ zlo->remove_element_id = 0;
+
+unlock:
+ mutex_unlock(&zlo->stor_elements_lock);
+
+ return ret;
+}
+
+static int zloop_restore_zone(struct zloop_device *zlo,
+ struct zloop_zone *zone)
+{
+ int ret = 0;
+
+ mutex_lock(&zone->lock);
+
+ if (!test_bit(ZLOOP_ZONE_CONV, &zone->flags)) {
+ ret = vfs_truncate(&zone->file->f_path, 0);
+ if (ret) {
+ set_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags);
+ ret = -EIO;
+ goto unlock;
+ }
+ }
+
+ spin_lock(&zone->wp_lock);
+ if (test_bit(ZLOOP_ZONE_CONV, &zone->flags)) {
+ zloop_set_zone_cond(zlo, zone, BLK_ZONE_COND_NOT_WP);
+ } else {
+ zloop_set_zone_empty(zlo, zone);
+ clear_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags);
+ }
+ spin_unlock(&zone->wp_lock);
+
+unlock:
+ mutex_unlock(&zone->lock);
+
+ return ret;
+}
+
+static int zloop_execute_restore_elements(struct zloop_device *zlo)
+{
+ struct gendisk *disk = zlo->disk;
+ struct zloop_zone *zone;
+ struct blk_se *se;
+ enum blk_se_status sts = BLK_SE_STS_OK;
+ unsigned int memflags, i;
+ int ret = 0;
+
+ /* Wait for all IOs to complete. */
+ memflags = blk_mq_freeze_queue(disk->queue);
+ blk_mq_unfreeze_queue(disk->queue, memflags);
+
+ /*
+ * Truncate all files for sequential zones and restore the condition of
+ * all zones. If this fails, we will propagate the failure as a
+ * restoration error.
+ */
+ for (i = 0, zone = zlo->zones; i < zlo->nr_zones; i++, zone++) {
+ ret = zloop_restore_zone(zlo, zone);
+ if (ret) {
+ pr_err("zloop%d: restore zone %u failed %d\n",
+ zlo->id, i, ret);
+ sts = BLK_SE_STS_RESTORE_ERROR;
+ break;
+ }
+ }
+
+ /*
+ * Update the status of all elements being restored based on the zone
+ * restoration result.
+ */
+ for (i = 0, se = zlo->elements; i < zlo->nr_elements; i++, se++) {
+ if (READ_ONCE(se->status) == BLK_SE_STS_RESTORE_IN_PROGRESS)
+ WRITE_ONCE(se->status, sts);
+ }
+
+ /*
+ * Clear the counter of removed elements so that even if we had an error
+ * and set the elements health to BLK_SE_STS_RESTORE_ERROR, then can be
+ * removed again.
+ */
+ zlo->nr_removed_elements = 0;
+
+ return ret;
+}
+
+static int zloop_restore_elements(struct gendisk *disk)
+{
+ struct zloop_device *zlo = disk->private_data;
+ struct blk_se *se;
+ unsigned int i, n = 0;
+ int ret = 0;
+
+ if (test_bit(GD_DEAD, &disk->state) ||
+ data_race(READ_ONCE(zlo->state)) == Zlo_deleting)
+ return -ENODEV;
+
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_NONE)
+ return -EOPNOTSUPP;
+
+ mutex_lock(&zlo->stor_elements_lock);
+
+ /* If a remove or restore operation is already in progress, bail out. */
+ if (zlo->remove_element_id || READ_ONCE(zlo->restore_in_progress)) {
+ ret = -EBUSY;
+ goto unlock;
+ }
+
+ /*
+ * Set the restore in progress state for elements that will be
+ * restored.
+ */
+ for (i = 0, se = zlo->elements; i < zlo->nr_elements; i++, se++) {
+ switch (READ_ONCE(se->status)) {
+ case BLK_SE_STS_REMOVED:
+ case BLK_SE_STS_RESTORE_ERROR:
+ WRITE_ONCE(se->status, BLK_SE_STS_RESTORE_IN_PROGRESS);
+ n++;
+ break;
+ default:
+ break;
+ }
+ }
+
+ /* If we have no removed or in error elements, we have nothing to do. */
+ if (!n)
+ goto unlock;
+
+ WRITE_ONCE(zlo->restore_in_progress, true);
+ mutex_unlock(&zlo->stor_elements_lock);
+
+ /*
+ * Wait a little so that this looks like a real device taking time to
+ * operate.
+ */
+ msleep(5000);
+
+ mutex_lock(&zlo->stor_elements_lock);
+
+ /* Restore the elements. */
+ ret = zloop_execute_restore_elements(zlo);
+
+ WRITE_ONCE(zlo->restore_in_progress, false);
+
+unlock:
+ mutex_unlock(&zlo->stor_elements_lock);
+
+ return ret;
+}
+
static void zloop_free_disk(struct gendisk *disk)
{
struct zloop_device *zlo = disk->private_data;
@@ -1029,15 +1538,23 @@ static void zloop_free_disk(struct gendisk *disk)
fput(zlo->data_dir);
destroy_workqueue(zlo->workqueue);
+ kfree(zlo->elements);
kfree(zlo->base_dir);
kvfree(zlo);
}
+static const struct blk_se_ops zloop_se_ops = {
+ .report_elements = zloop_report_elements,
+ .remove_element = zloop_remove_element,
+ .restore_elements = zloop_restore_elements,
+};
+
static const struct block_device_operations zloop_fops = {
.owner = THIS_MODULE,
.open = zloop_open,
.report_zones = zloop_report_zones,
.free_disk = zloop_free_disk,
+ .se_ops = &zloop_se_ops,
};
__printf(3, 4)
@@ -1114,6 +1631,25 @@ static int zloop_init_zone(struct zloop_device *zlo, struct zloop_options *opts,
spin_lock_init(&zone->wp_lock);
zone->start = (sector_t)zone_no << zlo->zone_shift;
+ /* If we are emulating storage elements, assign one to the zone. */
+ if (zlo->stor_elements != ZLOOP_STOR_ELEMENTS_NONE) {
+ unsigned int nr_elems = zlo->nr_elements;
+ unsigned int se_idx;
+
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_PAIRS)
+ nr_elems /= 2;
+ se_idx = zone_no % nr_elems;
+ zlo->elements[se_idx].nr_zones++;
+
+ zone->wr_se_id = se_idx + 1;
+ zone->rd_se_id = zone->wr_se_id;
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_PAIRS) {
+ zone->rd_se_id += nr_elems;
+ zlo->elements[zone->rd_se_id - 1].nr_zones =
+ zlo->elements[se_idx].nr_zones;
+ }
+ }
+
if (!restore)
oflags |= O_CREAT;
@@ -1189,6 +1725,69 @@ static int zloop_init_zone(struct zloop_device *zlo, struct zloop_options *opts,
return ret;
}
+#define ZLOOP_MIN_STOR_ELEMENTS 2
+#define ZLOOP_MAX_STOR_ELEMENTS 32
+#define ZLOOP_MIN_ZONES_PER_STOR_ELEMENTS 32
+
+static int zloop_create_storage_elements(struct zloop_device *zlo)
+{
+ struct blk_se *se, *paired_se;
+ unsigned int i, nr_elems, nr_elements;
+
+ /*
+ * Calculate the number of storage elements we are going to emulate.
+ * To achieve a somewhat realistic emulation, we want at least 2 storage
+ * elements, and no more than 32, targeting at least 32 zones per
+ * element.
+ */
+ if (zlo->nr_zones <= 64)
+ nr_elements = ZLOOP_MIN_STOR_ELEMENTS;
+ else
+ nr_elements =
+ min(ZLOOP_MAX_STOR_ELEMENTS,
+ zlo->nr_zones / ZLOOP_MIN_ZONES_PER_STOR_ELEMENTS);
+
+ /*
+ * If we are emulating pairs of read and write storage elements, we need
+ * double the number of storage element descriptors.
+ */
+ nr_elems = nr_elements;
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_PAIRS)
+ nr_elems *= 2;
+ zlo->elements = kzalloc_objs(struct blk_se, nr_elems);
+ if (!zlo->elements)
+ return -ENOMEM;
+
+ for (i = 0, se = zlo->elements; i < nr_elements; i++, se++) {
+ se->id = i + 1;
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_PAIRS)
+ se->type = BLK_SE_TYPE_WRITE;
+ else
+ se->type = BLK_SE_TYPE_RDWR;
+ se->status = BLK_SE_STS_OK;
+ }
+
+ /*
+ * If we are emulating pairs of read and write storage elements,
+ * initialize the read elements paired with the write elements we just
+ * initialized.
+ */
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_PAIRS) {
+ for (i = 0; i < nr_elements; i++, se++) {
+ se->id = nr_elements + i + 1;
+ paired_se = &zlo->elements[i];
+ se->paired_id = paired_se->id;
+ paired_se->paired_id = se->id;
+ se->type = BLK_SE_TYPE_READ;
+ se->status = BLK_SE_STS_OK;
+ }
+ }
+
+ zlo->nr_elements = nr_elems;
+
+ return 0;
+}
+
static bool zloop_dev_exists(struct zloop_device *zlo)
{
struct file *cnv, *seq;
@@ -1221,6 +1820,11 @@ static int zloop_ctl_add(struct zloop_options *opts)
int ret = -EINVAL;
bool restore;
+ if (opts->mask & ZLOOP_OPT_ELEMENT_ID) {
+ pr_err("Invalid option specified for add\n");
+ return -EINVAL;
+ }
+
__module_get(THIS_MODULE);
nr_zones = opts->capacity >> ilog2(opts->zone_size);
@@ -1244,6 +1848,7 @@ static int zloop_ctl_add(struct zloop_options *opts)
WRITE_ONCE(zlo->state, Zlo_creating);
spin_lock_init(&zlo->open_zones_lock);
INIT_LIST_HEAD(&zlo->open_zones_lru_list);
+ mutex_init(&zlo->stor_elements_lock);
ret = mutex_lock_killable(&zloop_ctl_mutex);
if (ret)
@@ -1277,12 +1882,19 @@ static int zloop_ctl_add(struct zloop_options *opts)
if (zlo->zone_append)
zlo->ordered_zone_append = opts->ordered_zone_append;
zlo->discard_write_cache = opts->discard_write_cache;
+ zlo->stor_elements = opts->stor_elements;
+
+ if (zlo->stor_elements != ZLOOP_STOR_ELEMENTS_NONE) {
+ ret = zloop_create_storage_elements(zlo);
+ if (ret)
+ goto out_free_idr;
+ }
zlo->workqueue = alloc_workqueue("zloop%d", WQ_UNBOUND | WQ_FREEZABLE,
opts->nr_queues * opts->queue_depth, zlo->id);
if (!zlo->workqueue) {
ret = -ENOMEM;
- goto out_free_idr;
+ goto out_destroy_storage_elements;
}
if (opts->base_dir)
@@ -1368,6 +1980,10 @@ static int zloop_ctl_add(struct zloop_options *opts)
zlo->id, zlo->nr_zones,
((sector_t)zlo->zone_size << SECTOR_SHIFT) >> 20,
zlo->block_size);
+ if (zlo->nr_elements)
+ pr_info("zloop%d: %u storage elements\n",
+ zlo->id, zlo->nr_elements);
+
pr_info("zloop%d: using %s%s zone append\n",
zlo->id,
zlo->ordered_zone_append ? "ordered " : "",
@@ -1391,6 +2007,8 @@ static int zloop_ctl_add(struct zloop_options *opts)
kfree(zlo->base_dir);
out_destroy_workqueue:
destroy_workqueue(zlo->workqueue);
+out_destroy_storage_elements:
+ kfree(zlo->elements);
out_free_idr:
mutex_lock(&zloop_ctl_mutex);
idr_remove(&zloop_index_idr, zlo->id);
@@ -1490,6 +2108,49 @@ static int zloop_ctl_remove(struct zloop_options *opts)
return 0;
}
+static int zloop_ctl_degrade_element(struct zloop_options *opts)
+{
+ struct zloop_device *zlo;
+ int ret = 0;
+
+ if (!(opts->mask & ZLOOP_OPT_ID)) {
+ pr_err("No ID specified for degrade_element\n");
+ return -EINVAL;
+ }
+
+ if (!(opts->mask & ZLOOP_OPT_ELEMENT_ID)) {
+ pr_err("No element ID specified for degrade_element\n");
+ return -EINVAL;
+ }
+
+ if (opts->mask & ~(ZLOOP_OPT_ID | ZLOOP_OPT_ELEMENT_ID)) {
+ pr_err("Invalid option specified for degrade_element\n");
+ return -EINVAL;
+ }
+
+ ret = mutex_lock_killable(&zloop_ctl_mutex);
+ if (ret)
+ return ret;
+
+ zlo = idr_find(&zloop_index_idr, opts->id);
+ if (!zlo || zlo->state == Zlo_creating)
+ ret = -ENODEV;
+ else if (zlo->state == Zlo_deleting)
+ ret = -EINVAL;
+ if (ret)
+ goto unlock;
+
+ ret = zloop_degrade_element(zlo, opts->element_id);
+ if (!ret)
+ pr_info("%pg: degraded element %u\n",
+ zlo->disk->part0, opts->element_id);
+
+unlock:
+ mutex_unlock(&zloop_ctl_mutex);
+
+ return ret;
+}
+
static int zloop_parse_options(struct zloop_options *opts, const char *buf)
{
substring_t args[MAX_OPT_ARGS];
@@ -1509,6 +2170,8 @@ static int zloop_parse_options(struct zloop_options *opts, const char *buf)
opts->buffered_io = ZLOOP_DEF_BUFFERED_IO;
opts->zone_append = ZLOOP_DEF_ZONE_APPEND;
opts->ordered_zone_append = ZLOOP_DEF_ORDERED_ZONE_APPEND;
+ opts->stor_elements = ZLOOP_DEF_STOR_ELEMENTS;
+ opts->element_id = ZLOOP_DEF_ELEMENT_ID;
if (!buf)
return 0;
@@ -1643,6 +2306,30 @@ static int zloop_parse_options(struct zloop_options *opts, const char *buf)
case ZLOOP_OPT_DISCARD_WRITE_CACHE:
opts->discard_write_cache = true;
break;
+ case ZLOOP_OPT_STOR_ELEMENTS:
+ if (match_uint(args, &token)) {
+ ret = -EINVAL;
+ goto out;
+ }
+ switch (token) {
+ case ZLOOP_STOR_ELEMENTS_NONE:
+ case ZLOOP_STOR_ELEMENTS_RDWR:
+ case ZLOOP_STOR_ELEMENTS_PAIRS:
+ break;
+ default:
+ pr_err("Invalid stor_elements value\n");
+ ret = -EINVAL;
+ goto out;
+ }
+ opts->stor_elements = token;
+ break;
+ case ZLOOP_OPT_ELEMENT_ID:
+ if (match_uint(args, &token)) {
+ ret = -EINVAL;
+ goto out;
+ }
+ opts->element_id = token;
+ break;
case ZLOOP_OPT_ERR:
default:
pr_warn("unknown parameter or missing value '%s'\n", p);
@@ -1671,14 +2358,16 @@ static int zloop_parse_options(struct zloop_options *opts, const char *buf)
enum {
ZLOOP_CTL_ADD,
ZLOOP_CTL_REMOVE,
+ ZLOOP_CTL_DEGRADE_ELEMENT,
};
static struct zloop_ctl_op {
int code;
const char *name;
} zloop_ctl_ops[] = {
- { ZLOOP_CTL_ADD, "add" },
- { ZLOOP_CTL_REMOVE, "remove" },
+ { ZLOOP_CTL_ADD, "add" },
+ { ZLOOP_CTL_REMOVE, "remove" },
+ { ZLOOP_CTL_DEGRADE_ELEMENT, "degrade_element" },
{ -1, NULL },
};
@@ -1726,6 +2415,9 @@ static ssize_t zloop_ctl_write(struct file *file, const char __user *ubuf,
case ZLOOP_CTL_REMOVE:
ret = zloop_ctl_remove(&opts);
break;
+ case ZLOOP_CTL_DEGRADE_ELEMENT:
+ ret = zloop_ctl_degrade_element(&opts);
+ break;
default:
pr_err("Invalid operation\n");
ret = -EINVAL;
@@ -1749,6 +2441,8 @@ static int zloop_ctl_show(struct seq_file *seq_file, void *private)
tok = &zloop_opt_tokens[i];
if (!tok->pattern)
break;
+ if (tok->token == ZLOOP_OPT_ELEMENT_ID)
+ continue;
if (i)
seq_putc(seq_file, ',');
seq_puts(seq_file, tok->pattern);
@@ -1759,6 +2453,10 @@ static int zloop_ctl_show(struct seq_file *seq_file, void *private)
seq_puts(seq_file, zloop_ctl_ops[1].name);
seq_puts(seq_file, " id=%d\n");
+ /* Degrade element operation */
+ seq_puts(seq_file, zloop_ctl_ops[2].name);
+ seq_puts(seq_file, " id=%d,element_id=%u\n");
+
return 0;
}
diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index dc45df819c426a..8b1c05de541dd4 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -1424,7 +1424,8 @@ static blk_status_t sd_setup_read_write_cmnd(struct scsi_cmnd *cmd)
{
struct request *rq = scsi_cmd_to_rq(cmd);
struct scsi_device *sdp = cmd->device;
- struct scsi_disk *sdkp = scsi_disk(rq->q->disk);
+ struct gendisk *disk = rq->q->disk;
+ struct scsi_disk *sdkp = scsi_disk(disk);
sector_t lba = sectors_to_logical(sdp, blk_rq_pos(rq));
sector_t threshold;
unsigned int nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));
@@ -1456,6 +1457,23 @@ static blk_status_t sd_setup_read_write_cmnd(struct scsi_cmnd *cmd)
goto fail;
}
+ /*
+ * The block layer already prevents writes to offline zones of zoned
+ * block devices. But there are no checks for reads, so check this here.
+ * Since disk_zone_is_offline() uses the block layer cached zone
+ * condition, this is inherently racy with zone revalidation updating
+ * the zone condition. However, this will never prevent valid read
+ * operations as the only case where we could proceed is if the read is
+ * received after a restoration of the disk storage element completes
+ * but before the zones are revalidated. And in that case, all zones are
+ * empty and thus have no data to read.
+ * Note that we are quiet about the error (no message) to avoid flooding
+ * the kernel log.
+ */
+ if (sdkp->device->type == TYPE_ZBC &&
+ disk_zone_is_offline(disk, blk_rq_pos(rq)))
+ goto fail;
+
/*
* Some SD card readers can't handle accesses which touch the
* last one or two logical blocks. Split accesses as needed.
@@ -3939,6 +3957,9 @@ static const struct block_device_operations sd_fops = {
.get_unique_id = sd_get_unique_id,
.free_disk = scsi_disk_free_disk,
.pr_ops = &sd_pr_ops,
+#ifdef CONFIG_BLK_DEV_ZONED
+ .se_ops = &sd_zbc_se_ops,
+#endif
};
/**
diff --git a/drivers/scsi/sd.h b/drivers/scsi/sd.h
index 574af82430169e..8a32e5f771d8cb 100644
--- a/drivers/scsi/sd.h
+++ b/drivers/scsi/sd.h
@@ -156,6 +156,9 @@ struct scsi_disk {
unsigned ignore_medium_access_errors : 1;
unsigned rscs : 1; /* reduced stream control support */
unsigned use_atomic_write_boundary : 1;
+ unsigned depop_report_supported : 1;
+ unsigned depop_modify_zones_supported : 1;
+ unsigned depop_restore_supported : 1;
};
#define to_scsi_disk(obj) container_of(obj, struct scsi_disk, disk_dev)
@@ -242,6 +245,8 @@ unsigned int sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes,
int sd_zbc_report_zones(struct gendisk *disk, sector_t sector,
unsigned int nr_zones, struct blk_report_zones_args *args);
+extern const struct blk_se_ops sd_zbc_se_ops;
+
#else /* CONFIG_BLK_DEV_ZONED */
static inline int sd_zbc_read_zones(struct scsi_disk *sdkp,
diff --git a/drivers/scsi/sd_zbc.c b/drivers/scsi/sd_zbc.c
index 8cbf171e6fabf1..3bad1392f0e259 100644
--- a/drivers/scsi/sd_zbc.c
+++ b/drivers/scsi/sd_zbc.c
@@ -12,6 +12,7 @@
#include <linux/vmalloc.h>
#include <linux/sched/mm.h>
#include <linux/mutex.h>
+#include <linux/delay.h>
#include <linux/unaligned.h>
@@ -516,11 +517,50 @@ static int sd_zbc_check_capacity(struct scsi_disk *sdkp, unsigned char *buf,
return 0;
}
+/*
+ * sd_zbc_check_depop_supported - Check if the device supports storage element
+ * depopulation
+ * @sdkp: Target SCSI disk
+ * @buf: command buffer
+ *
+ * Check if the device supports the storage elements depopulation management
+ * commands.
+ */
+static void sd_zbc_check_depop_supported(struct scsi_disk *sdkp,
+ unsigned char *buf)
+{
+ /* Mandate support for reporting storage elements health. */
+ sdkp->depop_report_supported =
+ scsi_report_opcode(sdkp->device, buf, SD_BUF_SIZE,
+ SERVICE_ACTION_IN_16,
+ SAI_GET_PHYSICAL_ELEMENT_STATUS) == 1;
+ if (!sdkp->depop_report_supported) {
+ sdkp->depop_modify_zones_supported = false;
+ sdkp->depop_restore_supported = false;
+ return;
+ }
+
+ sdkp->depop_modify_zones_supported =
+ scsi_report_opcode(sdkp->device, buf, SD_BUF_SIZE,
+ SERVICE_ACTION_IN_16,
+ SAI_REMOVE_ELEMENT_AND_MODIFY_ZONES) == 1;
+
+ sdkp->depop_restore_supported =
+ scsi_report_opcode(sdkp->device, buf, SD_BUF_SIZE,
+ SERVICE_ACTION_IN_16,
+ SAI_RESTORE_ELEMENTS_AND_REBUILD) == 1;
+}
+
static void sd_zbc_print_zones(struct scsi_disk *sdkp)
{
if (sdkp->device->type != TYPE_ZBC || !sdkp->capacity)
return;
+ if (sdkp->depop_modify_zones_supported)
+ sd_printk(KERN_NOTICE, sdkp, "Depop modify zones supported\n");
+ if (sdkp->depop_restore_supported)
+ sd_printk(KERN_NOTICE, sdkp, "Depop restoration supported\n");
+
if (sdkp->capacity & (sdkp->zone_info.zone_blocks - 1))
sd_printk(KERN_NOTICE, sdkp,
"%u zones of %u logical blocks + 1 runt zone\n",
@@ -533,6 +573,462 @@ static void sd_zbc_print_zones(struct scsi_disk *sdkp)
sdkp->zone_info.zone_blocks);
}
+static void sd_zbc_parse_storage_element(struct scsi_disk *sdkp, u8 *desc,
+ struct blk_se *element)
+{
+ struct scsi_device *sdp = sdkp->device;
+ sector_t zone_sectors = sd_zbc_zone_sectors(sdkp);
+ u64 capacity;
+
+ memset(element, 0, sizeof(*element));
+
+ element->id = get_unaligned_be32(&desc[4]);
+ element->restore_allowed = desc[13] & 0x01;
+
+ switch (desc[14]) {
+ case SCSI_PHYS_ELEM_TYPE_ALL_ACCESS_STORAGE:
+ element->type = BLK_SE_TYPE_RDWR;
+ capacity = get_unaligned_be64(&desc[16]);
+ if (!zone_sectors || capacity == ULLONG_MAX)
+ element->nr_zones = 0;
+ else
+ element->nr_zones =
+ logical_to_sectors(sdp, capacity) >>
+ ilog2(zone_sectors);
+ break;
+ case SCSI_PHYS_ELEM_TYPE_FRAC_ACCESS_STORAGE:
+ element->paired_id = get_unaligned_be32(&desc[16]);
+ if (desc[20] & 0x01)
+ element->type = BLK_SE_TYPE_READ;
+ else
+ element->type = BLK_SE_TYPE_WRITE;
+ element->nr_zones = get_unaligned_be64(&desc[24]);
+ break;
+ default:
+ element->type = BLK_SE_TYPE_UNKNOWN;
+ }
+
+ if (desc[15] >= SCSI_PHYS_ELEM_HEALTH_WITHIN_SPEC_LIMITS_MIN &&
+ desc[15] <= SCSI_PHYS_ELEM_HEALTH_WITHIN_SPEC_LIMITS_MAX) {
+ element->status = BLK_SE_STS_OK;
+ return;
+ }
+
+ if (desc[15] >= SCSI_PHYS_ELEM_HEALTH_OUTSIDE_SPEC_LIMITS_MIN &&
+ desc[15] <= SCSI_PHYS_ELEM_HEALTH_OUTSIDE_SPEC_LIMITS_MAX) {
+ element->status = BLK_SE_STS_DEGRADED;
+ return;
+ }
+
+ switch (desc[15]) {
+ case SCSI_PHYS_ELEM_HEALTH_AT_SPEC_LIMITS:
+ element->status = BLK_SE_STS_OK_LIMIT;
+ break;
+ case SCSI_PHYS_ELEM_HEALTH_DEPOP_REVOKE_ERR:
+ element->status = BLK_SE_STS_RESTORE_ERROR;
+ break;
+ case SCSI_PHYS_ELEM_HEALTH_DEPOP_REVOKE_IN_PROGRESS:
+ element->status = BLK_SE_STS_RESTORE_IN_PROGRESS;
+ break;
+ case SCSI_PHYS_ELEM_HEALTH_DEPOP_ERR:
+ element->status = BLK_SE_STS_REMOVE_ERROR;
+ break;
+ case SCSI_PHYS_ELEM_HEALTH_DEPOP_IN_PROGRESS:
+ element->status = BLK_SE_STS_REMOVE_IN_PROGRESS;
+ break;
+ case SCSI_PHYS_ELEM_HEALTH_DEPOP_OK:
+ element->status = BLK_SE_STS_REMOVED;
+ break;
+ case SCSI_PHYS_ELEM_HEALTH_NOT_REPORTED:
+ default:
+ element->status = BLK_SE_STS_UNKNOWN;
+ break;
+ }
+}
+
+/*
+ * Large hard limit on the number of storage elements. This accommodates all
+ * known devices today and likely forever :)
+ */
+#define SD_ZBC_MAX_STORAGE_ELEMENTS 255
+
+struct sd_zbc_se_report {
+ struct blk_se *elements;
+ unsigned int nr_elements;
+ unsigned int nr_reported_elements;
+ bool need_free_elements;
+ u8 *buf;
+ unsigned int buf_size;
+};
+
+static int sd_zbc_alloc_se_report(struct blk_se *elements,
+ unsigned int nr_elements,
+ bool alloc_elements,
+ struct sd_zbc_se_report *rep)
+{
+ /*
+ * We need at least 32B for the report header and 32B for each
+ * descriptor. Note that we use GFP_KERNEL here, which may block. That
+ * is fine when called from ioctl context. When called from a file
+ * system, the FS can use memalloc_noio_save()/restore() or
+ * memalloc_nofs_save()/restore() if GFP_NOFS is required to avoid I/Os
+ * and/or FS re-entry.
+ */
+ rep->need_free_elements = alloc_elements;
+ rep->nr_elements = min(SD_ZBC_MAX_STORAGE_ELEMENTS, nr_elements);
+ rep->buf_size = ALIGN((rep->nr_elements + 1) * 32, SECTOR_SIZE);
+ rep->buf = kzalloc(rep->buf_size, GFP_KERNEL);
+ if (!rep->buf)
+ return -ENOMEM;
+
+ if (!alloc_elements) {
+ rep->elements = elements;
+ return 0;
+ }
+
+ rep->elements =
+ kzalloc_objs(struct blk_se, rep->nr_elements, GFP_KERNEL);
+ if (!rep->elements) {
+ kfree(rep->buf);
+ return -ENOMEM;
+ }
+
+ return 0;
+}
+
+static void sd_zbc_free_se_report(struct sd_zbc_se_report *rep)
+{
+ kfree(rep->buf);
+ if (rep->need_free_elements)
+ kfree(rep->elements);
+}
+
+static int sd_zbc_report_se(struct scsi_disk *sdkp,
+ struct sd_zbc_se_report *rep)
+{
+ struct scsi_device *sdp = sdkp->device;
+ const int timeout = sdp->request_queue->rq_timeout;
+ struct scsi_sense_hdr sshdr;
+ struct scsi_failure failure_defs[] = {
+ /* Allow up to retry 3 times */
+ {
+ .result = SCMD_FAILURE_RESULT_ANY,
+ .allowed = 3,
+ },
+ {}
+ };
+ struct scsi_failures failures = {
+ .failure_definitions = failure_defs,
+ };
+ const struct scsi_exec_args exec_args = {
+ .sshdr = &sshdr,
+ .failures = &failures,
+ };
+ unsigned char cmd[16];
+ unsigned int nr_descs;
+ int i, result;
+ u8 *desc;
+
+ memset(cmd, 0, 16);
+ cmd[0] = SERVICE_ACTION_IN_16;
+ cmd[1] = SAI_GET_PHYSICAL_ELEMENT_STATUS;
+ put_unaligned_be32(rep->buf_size, &cmd[10]);
+
+ result = scsi_execute_cmd(sdp, cmd, REQ_OP_DRV_IN,
+ rep->buf, rep->buf_size, timeout, 1, &exec_args);
+ if (result) {
+ if (result > 0) {
+ sd_print_result(sdkp, "GET PHYSICAL ELEMENT STATUS",
+ result);
+ if (scsi_sense_valid(&sshdr))
+ sd_print_sense_hdr(sdkp, &sshdr);
+ return -EIO;
+ }
+ return result;
+ }
+
+ nr_descs = get_unaligned_be32(&rep->buf[0]);
+ if (!nr_descs) {
+ sd_printk(KERN_ERR, sdkp,
+ "Invalid number of phys element descriptors\n");
+ return -EIO;
+ }
+ if (nr_descs > SD_ZBC_MAX_STORAGE_ELEMENTS) {
+ sd_printk(KERN_ERR, sdkp,
+ "Too many phys element descriptors\n");
+ return -EIO;
+ }
+
+ if (!rep->elements) {
+ rep->nr_reported_elements = nr_descs;
+ return 0;
+ }
+
+ nr_descs = get_unaligned_be32(&rep->buf[4]);
+ if (!nr_descs) {
+ sd_printk(KERN_ERR, sdkp,
+ "Invalid number of reported phys element descriptors\n");
+ return -EIO;
+ }
+
+ desc = &rep->buf[32];
+ for (i = 0; i < min(rep->nr_elements, nr_descs); i++, desc += 32)
+ sd_zbc_parse_storage_element(sdkp, desc, &rep->elements[i]);
+ rep->nr_reported_elements = i;
+
+ return 0;
+}
+
+static int sd_zbc_report_storage_elements(struct gendisk *disk,
+ struct blk_se *elements,
+ unsigned int *nr_elements)
+{
+ struct scsi_disk *sdkp = scsi_disk(disk);
+ struct sd_zbc_se_report rep;
+ int ret;
+
+ if (!sdkp->depop_report_supported)
+ return -EOPNOTSUPP;
+
+ ret = sd_zbc_alloc_se_report(elements, *nr_elements, false, &rep);
+ if (ret)
+ return ret;
+
+ ret = sd_zbc_report_se(sdkp, &rep);
+ if (!ret)
+ *nr_elements = rep.nr_reported_elements;
+
+ sd_zbc_free_se_report(&rep);
+
+ return ret;
+}
+
+static int sd_zbc_execute_storage_element_cmd(struct scsi_disk *sdkp,
+ u8 action, u32 element_id)
+{
+ struct scsi_device *sdp = sdkp->device;
+ const int timeout = sdp->request_queue->rq_timeout;
+ struct scsi_sense_hdr sshdr;
+ const struct scsi_exec_args exec_args = {
+ .sshdr = &sshdr,
+ };
+ unsigned char cmd[16];
+ const char *cmd_name;
+ int result;
+
+ memset(cmd, 0, sizeof(cmd));
+ cmd[0] = SERVICE_ACTION_IN_16;
+ cmd[1] = action;
+ if (action == SAI_REMOVE_ELEMENT_AND_MODIFY_ZONES) {
+ put_unaligned_be32(element_id, &cmd[10]);
+ cmd_name = "REMOVE ELEMENT AND MODIFY ZONES";
+ } else {
+ cmd_name = "RESTORE ELEMENTS AND REBUILD";
+ }
+
+ result = scsi_execute_cmd(sdp, cmd, REQ_OP_DRV_IN, NULL, 0,
+ timeout, 1, &exec_args);
+ if (result) {
+ sd_printk(KERN_ERR, sdkp, "%s failed\n", cmd_name);
+ if (result > 0) {
+ if (scsi_sense_valid(&sshdr))
+ sd_print_sense_hdr(sdkp, &sshdr);
+ result = -EIO;
+ }
+ return result;
+ }
+
+ return 0;
+}
+
+/*
+ * Wait for the completion of a storage element remove operation. Note that the
+ * standards advise to use pollable sense data with the REQUEST SENSE command to
+ * detect if a remove operation is in progress. However, since REQUEST SENSE
+ * will return sense data only for the last executed command, and since we are
+ * not blocking commands, this method would be unreliable as we may end up
+ * seeing sense data (or no sense data) for a command that is not the REMOVE
+ * ELEMENT AND MODIFY ZONES we issued.
+ */
+static int sd_zbc_wait_for_se_removal(struct scsi_disk *sdkp,
+ struct sd_zbc_se_report *rep,
+ unsigned int element_id)
+{
+ bool element_checked, element_removed;
+ int ret, in_progress;
+ unsigned int i;
+
+ /*
+ * Poll the storage elements health to see that we get the element we
+ * are interested in and that its removal completed. Since we do not
+ * know how long the operation may take, we cannot have a timeout on
+ * this loop. So check for fatal signals so that the user can terminate
+ * this loop if the device misbehaves.
+ */
+ do {
+ /* Let the operation proceed and check. */
+ msleep(1000);
+
+ ret = sd_zbc_report_se(sdkp, rep);
+ if (ret)
+ return ret;
+
+ element_checked = false;
+ element_removed = false;
+ in_progress = 0;
+ for (i = 0; i < rep->nr_elements; i++) {
+ if (rep->elements[i].id == element_id)
+ element_checked = true;
+ switch (rep->elements[i].status) {
+ case BLK_SE_STS_REMOVED:
+ if (rep->elements[i].id == element_id)
+ element_removed = true;
+ break;
+ case BLK_SE_STS_REMOVE_IN_PROGRESS:
+ in_progress++;
+ break;
+ case BLK_SE_STS_REMOVE_ERROR:
+ if (rep->elements[i].id == element_id)
+ return -EIO;
+ break;
+ default:
+ break;
+ }
+ }
+
+ if (!element_checked)
+ return -EIO;
+ if (element_removed && !in_progress)
+ return 0;
+ } while (!fatal_signal_pending(current));
+
+ return -EINTR;
+}
+
+static int sd_zbc_remove_storage_element(struct gendisk *disk,
+ unsigned int element_id)
+{
+ struct scsi_disk *sdkp = scsi_disk(disk);
+ struct sd_zbc_se_report rep;
+ int ret;
+
+ if (!sdkp->depop_modify_zones_supported)
+ return -EOPNOTSUPP;
+
+ /*
+ * Get the resources we need to prepare for waiting for the operation to
+ * complete.
+ */
+ ret = sd_zbc_alloc_se_report(NULL, disk->nr_storage_elements,
+ true, &rep);
+ if (ret)
+ return ret;
+
+ /*
+ * Execute the remove command and wait for the element removal operation
+ * to complete.
+ */
+ ret = sd_zbc_execute_storage_element_cmd(sdkp,
+ SAI_REMOVE_ELEMENT_AND_MODIFY_ZONES, element_id);
+ if (!ret)
+ ret = sd_zbc_wait_for_se_removal(sdkp, &rep, element_id);
+ if (!ret)
+ sd_printk(KERN_NOTICE, sdkp,
+ "Removed storage element %u\n", element_id);
+
+ sd_zbc_free_se_report(&rep);
+
+ return ret;
+}
+
+/*
+ * Wait for the completion of the restoration of removed storage elements.
+ * See the note for sd_zbc_wait_for_se_removal().
+ */
+static int sd_zbc_wait_for_se_restore(struct scsi_disk *sdkp,
+ struct sd_zbc_se_report *rep)
+{
+ int ret, in_progress;
+ unsigned int i;
+
+ /*
+ * Poll the storage elements health to see if we have any element still
+ * being restored or if we got errors. Since we do not know how long the
+ * operation may take, we cannot have a timeout on this loop. So check
+ * for fatal signals so that the user can terminate this retry loop if
+ * the device misbehaves.
+ */
+ do {
+ /* Let the operation proceed and check. */
+ msleep(1000);
+
+ ret = sd_zbc_report_se(sdkp, rep);
+ if (ret)
+ return ret;
+
+ in_progress = 0;
+ for (i = 0; i < rep->nr_elements; i++) {
+ switch (rep->elements[i].status) {
+ case BLK_SE_STS_RESTORE_IN_PROGRESS:
+ in_progress++;
+ break;
+ case BLK_SE_STS_REMOVED:
+ case BLK_SE_STS_RESTORE_ERROR:
+ if (rep->elements[i].restore_allowed)
+ ret = -EIO;
+ break;
+ default:
+ break;
+ }
+ }
+
+ if (!in_progress)
+ return ret;
+ } while (!fatal_signal_pending(current));
+
+ return -EINTR;
+}
+
+static int sd_zbc_restore_storage_elements(struct gendisk *disk)
+{
+ struct scsi_disk *sdkp = scsi_disk(disk);
+ struct sd_zbc_se_report rep;
+ int ret;
+
+ if (!sdkp->depop_restore_supported)
+ return -EOPNOTSUPP;
+
+ /*
+ * Get the resources we need to prepare for waiting for the operation to
+ * complete.
+ */
+ ret = sd_zbc_alloc_se_report(NULL, disk->nr_storage_elements,
+ true, &rep);
+ if (ret)
+ return ret;
+
+ /*
+ * Execute the restore command and wait for the elements restoration
+ * operation to complete.
+ */
+ ret = sd_zbc_execute_storage_element_cmd(sdkp,
+ SAI_RESTORE_ELEMENTS_AND_REBUILD, 0);
+ if (!ret)
+ ret = sd_zbc_wait_for_se_restore(sdkp, &rep);
+ if (!ret)
+ sd_printk(KERN_NOTICE, sdkp, "Restored storage elements\n");
+
+ sd_zbc_free_se_report(&rep);
+
+ return ret;
+}
+
+const struct blk_se_ops sd_zbc_se_ops = {
+ .report_elements = sd_zbc_report_storage_elements,
+ .remove_element = sd_zbc_remove_storage_element,
+ .restore_elements = sd_zbc_restore_storage_elements,
+};
+
/*
* Call blk_revalidate_disk_zones() if any of the zoned disk properties have
* changed that make it necessary to call that function. Called by
@@ -545,6 +1041,7 @@ int sd_zbc_revalidate_zones(struct scsi_disk *sdkp)
u32 zone_blocks = sdkp->early_zone_info.zone_blocks;
unsigned int nr_zones = sdkp->early_zone_info.nr_zones;
unsigned int flags;
+ bool print_zones;
int ret;
/*
@@ -554,11 +1051,22 @@ int sd_zbc_revalidate_zones(struct scsi_disk *sdkp)
if (!blk_queue_is_zoned(q))
return 0;
+ /*
+ * If the zone size and number of zones has not changed, and the disk
+ * does not support depopulating heads, skip the rather slow call to
+ * blk_revalidate_disk_zones().
+ */
if (sdkp->zone_info.zone_blocks == zone_blocks &&
sdkp->zone_info.nr_zones == nr_zones &&
- disk->nr_zones == nr_zones)
+ disk->nr_zones == nr_zones &&
+ !sdkp->depop_modify_zones_supported)
return 0;
+ print_zones =
+ sdkp->zone_info.zone_blocks != zone_blocks ||
+ sdkp->zone_info.nr_zones != nr_zones ||
+ disk->nr_zones != nr_zones;
+
sdkp->zone_info.zone_blocks = zone_blocks;
sdkp->zone_info.nr_zones = nr_zones;
@@ -571,7 +1079,8 @@ int sd_zbc_revalidate_zones(struct scsi_disk *sdkp)
return ret;
}
- sd_zbc_print_zones(sdkp);
+ if (print_zones)
+ sd_zbc_print_zones(sdkp);
return 0;
}
@@ -610,6 +1119,9 @@ int sd_zbc_read_zones(struct scsi_disk *sdkp, struct queue_limits *lim,
sdkp->device->use_10_for_rw = 0;
sdkp->device->use_16_for_sync = 1;
+ /* Check if storage element depopulation is supported. */
+ sd_zbc_check_depop_supported(sdkp, buf);
+
/* Check zoned block device characteristics (unconstrained reads) */
ret = sd_zbc_check_zoned_characteristics(sdkp, buf);
if (ret)
diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index 215753171cef0e..63c6e43ecdda30 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -189,6 +189,11 @@ struct gendisk {
struct disk_events *ev;
#ifdef CONFIG_BLK_DEV_ZONED
+ /*
+ * Total number of storage elements of the disk. This is always 0 for
+ * disks that do not support storage element management.
+ */
+ unsigned int nr_storage_elements;
/*
* Zoned block device information. Reads of this information must be
* protected with blk_queue_enter() / blk_queue_exit() or by holding a
@@ -940,6 +945,9 @@ static inline unsigned int disk_zone_capacity(struct gendisk *disk,
return disk->last_zone_capacity;
return disk->zone_capacity;
}
+
+bool disk_zone_is_offline(struct gendisk *disk, sector_t sector);
+
static inline unsigned int bdev_zone_capacity(struct block_device *bdev,
sector_t pos)
{
@@ -948,12 +956,25 @@ static inline unsigned int bdev_zone_capacity(struct block_device *bdev,
bool bdev_zone_is_seq(struct block_device *bdev, sector_t sector);
+int bdev_report_storage_elements(struct block_device *bdev,
+ struct blk_se *elements,
+ unsigned int *nr_elements);
+int bdev_remove_storage_element(struct block_device *bdev,
+ unsigned int element_id);
+int bdev_restore_storage_elements(struct block_device *bdev);
+
#else /* CONFIG_BLK_DEV_ZONED */
static inline unsigned int disk_nr_zones(struct gendisk *disk)
{
return 0;
}
+static inline bool disk_zone_is_offline(struct gendisk *disk,
+ sector_t sector)
+{
+ return false;
+}
+
static inline bool bdev_zone_is_seq(struct block_device *bdev, sector_t sector)
{
return false;
@@ -968,6 +989,25 @@ static inline bool blk_zone_plug_bio(struct bio *bio, unsigned int nr_segs)
{
return false;
}
+
+static inline int bdev_report_storage_elements(struct block_device *bdev,
+ struct blk_se *elements,
+ unsigned int *nr_elements)
+{
+ return -EOPNOTSUPP;
+}
+
+static inline int bdev_remove_storage_element(struct block_device *bdev,
+ unsigned int element_id)
+{
+ return -EOPNOTSUPP;
+}
+
+static inline int bdev_restore_storage_elements(struct block_device *bdev)
+{
+ return -EOPNOTSUPP;
+}
+
#endif /* CONFIG_BLK_DEV_ZONED */
static inline unsigned int bdev_nr_zones(struct block_device *bdev)
@@ -1572,6 +1612,29 @@ enum blk_unique_id {
BLK_UID_NAA = 3,
};
+/**
+ * struct blk_se_ops - Storage element management operations.
+ * @report_elements: Report at most nr_elements storage elements of the
+ * specified disk. If the elements argument is NULL, only
+ * report the number of storage elements of the target disk.
+ * @remove_element: Remove (depopulate) the specified storage element from the
+ * target disk. This is a blocking operation called with
+ * zone_revalidate_mutex held. This callback must return only
+ * after the remove operation completes or an error occurs.
+ * @restore_elements: Restore (re-populate), if possible, all removed storage
+ * elements of the target disk. This operation is called with
+ * zone_revalidate_mutex held. This callback must return only
+ * after the restoration operation completes or an error
+ * occurs.
+ */
+struct blk_se_ops {
+ int (*report_elements)(struct gendisk *disk,
+ struct blk_se *elements,
+ unsigned int *nr_elements);
+ int (*remove_element)(struct gendisk *disk, unsigned int element_id);
+ int (*restore_elements)(struct gendisk *disk);
+};
+
struct block_device_operations {
void (*submit_bio)(struct bio *bio);
int (*poll_bio)(struct bio *bio, struct io_comp_batch *iob,
@@ -1601,6 +1664,7 @@ struct block_device_operations {
struct dma_buf_io_ctx *);
struct module *owner;
const struct pr_ops *pr_ops;
+ const struct blk_se_ops *se_ops;
/*
* Special callback for probing GPT entry at a given sector.
diff --git a/include/scsi/scsi_proto.h b/include/scsi/scsi_proto.h
index 9c7d31aa6bae59..f290a214e3e06e 100644
--- a/include/scsi/scsi_proto.h
+++ b/include/scsi/scsi_proto.h
@@ -408,9 +408,11 @@ enum scsi_phys_element_type {
/* SCSI physical element health. */
enum scsi_phys_element_health {
SCSI_PHYS_ELEM_HEALTH_NOT_REPORTED = 0x00,
- SCSI_PHYS_ELEM_HEALTH_WITHIN_SPEC_LIMITS = 0x01,
+ SCSI_PHYS_ELEM_HEALTH_WITHIN_SPEC_LIMITS_MIN = 0x01,
+ SCSI_PHYS_ELEM_HEALTH_WITHIN_SPEC_LIMITS_MAX = 0x63,
SCSI_PHYS_ELEM_HEALTH_AT_SPEC_LIMITS = 0x64,
- SCSI_PHYS_ELEM_HEALTH_OUTSIDE_SPEC_LIMITS = 0x65,
+ SCSI_PHYS_ELEM_HEALTH_OUTSIDE_SPEC_LIMITS_MIN = 0x65,
+ SCSI_PHYS_ELEM_HEALTH_OUTSIDE_SPEC_LIMITS_MAX = 0xCF,
SCSI_PHYS_ELEM_HEALTH_DEPOP_REVOKE_ERR = 0xFB,
SCSI_PHYS_ELEM_HEALTH_DEPOP_REVOKE_IN_PROGRESS = 0xFC,
SCSI_PHYS_ELEM_HEALTH_DEPOP_ERR = 0xFD,
diff --git a/include/uapi/linux/blkzoned.h b/include/uapi/linux/blkzoned.h
index 66383612096678..6463cdec8e864e 100644
--- a/include/uapi/linux/blkzoned.h
+++ b/include/uapi/linux/blkzoned.h
@@ -208,4 +208,110 @@ struct blk_zone_range {
#define BLKFINISHZONE _IOW(0x12, 136, struct blk_zone_range)
#define BLKREPORTZONEV2 _IOWR(0x12, 142, struct blk_zone_report)
+/**
+ * enum blk_se_type - Type of a zoned device storage element.
+ *
+ * @BLK_SE_TYPE_RDWR: The storage element handles both reads and writes.
+ * @BLK_SE_TYPE_READ: The storage element handles reads only.
+ * @BLK_SE_TYPE_WRITE: The storage element handles writes only.
+ * @BLK_SE_TYPE_UNKNOWN: The storage element type is not known.
+ */
+enum blk_se_type {
+ BLK_SE_TYPE_RDWR = 0x01,
+ BLK_SE_TYPE_READ = 0x02,
+ BLK_SE_TYPE_WRITE = 0x03,
+ BLK_SE_TYPE_UNKNOWN = 0xFF,
+};
+
+/**
+ * enum blk_se_status - Status of a zoned device storage element.
+ *
+ * @BLK_SE_STS_OK: The storage element is operating normally.
+ * @BLK_SE_STS_OK_LIMIT: The storage element is operating at the manufacturer
+ * specifications limit.
+ * @BLK_SE_STS_DEGRADED: The storage element has degraded and is not operating
+ * normally.
+ * @BLK_SE_STS_REMOVE_IN_PROGRESS: The storage element is being removed.
+ * @BLK_SE_STS_REMOVE_ERROR: The storage element removal failed.
+ * @BLK_SE_STS_RESTORE_IN_PROGRESS: The storage element is being restored.
+ * @BLK_SE_STS_RESTORE_ERROR: The storage element restoration failed.
+ * @BLK_SE_STS_REMOVED: The storage element was removed.
+ * @BLK_SE_STS_UNKNOWN: The storage element status is unknown.
+ */
+enum blk_se_status {
+ BLK_SE_STS_OK = 0x01,
+ BLK_SE_STS_OK_LIMIT = 0x02,
+ BLK_SE_STS_DEGRADED = 0x03,
+ BLK_SE_STS_REMOVE_IN_PROGRESS = 0x04,
+ BLK_SE_STS_REMOVE_ERROR = 0x05,
+ BLK_SE_STS_RESTORE_IN_PROGRESS = 0x06,
+ BLK_SE_STS_RESTORE_ERROR = 0x07,
+ BLK_SE_STS_REMOVED = 0x08,
+ BLK_SE_STS_UNKNOWN = 0xFF,
+};
+
+/**
+ * struct blk_se - Zoned device storage element descriptor.
+ *
+ * @id: The ID of the element (cannot be 0).
+ * @paired_id: The ID of the paired element for an element that is not
+ * of type BLK_SE_TYPE_RDWR.
+ * @nr_zones: The number of zones that the storage element handles.
+ * @type: The type of the storage element (enum blk_se_type).
+ * @status: The health status of the storage element
+ * (enum blk_se_status).
+ * @restore_allowed: Indicate if the storage element can be restored.
+ * @reserved: unused field to align this structure size to 32B
+ */
+struct blk_se {
+ __u32 id;
+ __u32 paired_id;
+ __u64 nr_zones;
+ __u8 type;
+ __u8 status;
+ __u8 restore_allowed;
+ __u8 reserved[13];
+};
+
+/**
+ * struct blk_se_report - Zoned device storage element descriptor report for the
+ * BLKREPORTSTORELEMS ioctl.
+ *
+ * @nr_elements: on input, this indicates the size of the @elements array. On
+ * output, this indicates the maximum number of storage elements
+ * of the device. If this field specifies 0 on input, the array
+ * @elements is not filled and the maximum number of storage
+ * elements of the device returned.
+ * @nr_reported_elements: output only to indicate the number of storage element
+ * descriptors filled in the array @elements.
+ * @reserved: reserved space for future use and structure field alignment.
+ * @elements: array of storage element descriptors with a maximum number of
+ * entries specified by @nr_elements on input and by
+ * @nr_reported_elements on output.
+ */
+struct blk_se_report {
+ __u32 nr_elements;
+ __u32 nr_reported_elements;
+ __u64 reserved;
+ struct blk_se elements[];
+};
+
+/**
+ * Zoned block device storage element management ioctl's:
+ *
+ * @BLKREPORTSTORELEMS: Get the number of storage elements of a device and
+ * the device storage element descriptors.
+ * @BLKREMOVESTORELEM: Remove (depopulate) one storage element of a device. This
+ * operation will flush the target device write cache and
+ * invalidate all cached data. So the caller must ensure
+ * that all dirty data written with buffered IOs are written
+ * back first to avoid any data loss.
+ * @BLKRESTORESTORELEMS: Restore (repopulate if possible) all storage elements
+ * that have been removed. This is a destructive operation
+ * equivalent to reformatting the target device.
+ */
+#define BLKREPORTSTORELEMS _IOWR(0x12, 143, struct blk_se_report)
+#define BLKREMOVESTORELEM _IOW(0x12, 144, __u32)
+#define BLKRESTORESTORELEMS _IO(0x12, 145)
+
#endif /* _UAPI_BLKZONED_H */
diff --git a/include/uapi/linux/fs.h b/include/uapi/linux/fs.h
index a46c33692aa21a..5b6f8248a4799b 100644
--- a/include/uapi/linux/fs.h
+++ b/include/uapi/linux/fs.h
@@ -309,6 +309,7 @@ struct file_attr {
/* 130-136 and 142 are used by zoned block device ioctls (uapi/linux/blkzoned.h) */
/* 137-141 are used by blk-crypto ioctls (uapi/linux/blk-crypto.h) */
#define BLKTRACESETUP2 _IOWR(0x12, 142, struct blk_user_trace_setup2)
+/* 143-145 are used by storage element management for zoned block devices. */
#define BMAP_IOCTL 1 /* obsolete - kept for compatibility */
#define FIBMAP _IO(0x00,1) /* bmap access */
|
| 3/2 | 2026/11/12 05:08 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 34875 output: 44 thoughts: 553 Thoughts: **Analyzing the Zoned Block Device Storage Element Patch** Okay, let's dive into this patch. My initial thought is to figure out the scope and impact here, and if we've got something that's fuzzable and warrants further attention. The first thing I see is that it introduces storage element management for zoned block devices. That’s interesting, and worth digging into. I can see the files touched, and it looks like a pretty comprehensive change, touching core block layer components, the loop device, and SCSI drivers. This could be significant. From what I can tell, it looks like this is what the patch accomplishes: first, it's adding storage element management to the core block layer in `block/blk-zoned.c`. I'm seeing new functions like `disk_zone_is_offline()`, refactoring of `disk_revalidate_zones()`, and the addition of `bdev_report_storage_elements()`, `blkdev_ioctl_report_storage_elements()`, and so on. Also, it’s adding new ioctls: `BLKREPORTSTORELEMS`, `BLKREMOVESTORELEM`, and `BLKRESTORESTORELEMS` to `block/ioctl.c`. Then, it adds storage element emulation to the `zloop` driver in `drivers/block/zloop.c`. I need to figure out how `zloop` works exactly, but the fact that it's a loop device driver makes me think about potential user-space interaction via `/dev/zloop-control`. That could be a juicy target for fuzzing. I'll need to check the Kconfig to see how zloop gets configured. Finally, it's adding storage element support to the SCSI ZBC code in `drivers/scsi/sd_zbc.c` and `drivers/scsi/sd.c`. That’s another potential entry point. The fact that it supports scsi_debug implies this is something that can be tested in an environment that is easily controlled. So that's another interesting avenue to explore. Alright, time to dive deeper into the `drivers/block/Kconfig` to figure out this `zloop`. |
| 4/2 | 2026/11/12 05:09 | tool | grepper |
0mArgs:{
"Expression": "CONFIG_BLK_DEV_ZLOOP|config BLK_DEV_ZLOOP|ZLOOP",
"PathPrefixes": [
"drivers/block"
]
}Results: {
"Output": "\nFull output is too long, showing 200 out of 418 lines.\nUse more precise expression or PathPrefixes if possible.\n\nNumber of matching lines per file (1 files in total):\ndrivers/block/zloop.c:143\n\ndrivers/block/zloop.c=26=enum {\ndrivers/block/zloop.c:27:\tZLOOP_OPT_ERR\t\t\t= 0,\ndrivers/block/zloop.c:28:\tZLOOP_OPT_ID\t\t\t= (1 \u003c\u003c 0),\ndrivers/block/zloop.c:29:\tZLOOP_OPT_CAPACITY\t\t= (1 \u003c\u003c 1),\ndrivers/block/zloop.c:30:\tZLOOP_OPT_ZONE_SIZE\t\t= (1 \u003c\u003c 2),\ndrivers/block/zloop.c:31:\tZLOOP_OPT_ZONE_CAPACITY\t\t= (1 \u003c\u003c 3),\ndrivers/block/zloop.c:32:\tZLOOP_OPT_NR_CONV_ZONES\t\t= (1 \u003c\u003c 4),\ndrivers/block/zloop.c:33:\tZLOOP_OPT_BASE_DIR\t\t= (1 \u003c\u003c 5),\ndrivers/block/zloop.c:34:\tZLOOP_OPT_NR_QUEUES\t\t= (1 \u003c\u003c 6),\ndrivers/block/zloop.c:35:\tZLOOP_OPT_QUEUE_DEPTH\t\t= (1 \u003c\u003c 7),\ndrivers/block/zloop.c:36:\tZLOOP_OPT_BUFFERED_IO\t\t= (1 \u003c\u003c 8),\ndrivers/block/zloop.c:37:\tZLOOP_OPT_ZONE_APPEND\t\t= (1 \u003c\u003c 9),\ndrivers/block/zloop.c:38:\tZLOOP_OPT_ORDERED_ZONE_APPEND\t= (1 \u003c\u003c 10),\ndrivers/block/zloop.c:39:\tZLOOP_OPT_DISCARD_WRITE_CACHE\t= (1 \u003c\u003c 11),\ndrivers/block/zloop.c:40:\tZLOOP_OPT_MAX_OPEN_ZONES\t= (1 \u003c\u003c 12),\ndrivers/block/zloop.c:41:\tZLOOP_OPT_STOR_ELEMENTS\t\t= (1 \u003c\u003c 13),\ndrivers/block/zloop.c:42:\tZLOOP_OPT_ELEMENT_ID\t\t= (1 \u003c\u003c 14),\ndrivers/block/zloop.c-43-};\n--\ndrivers/block/zloop.c=45=static const match_table_t zloop_opt_tokens = {\ndrivers/block/zloop.c:46:\t{ ZLOOP_OPT_ID,\t\t\t\"id=%d\"\t},\ndrivers/block/zloop.c:47:\t{ ZLOOP_OPT_CAPACITY,\t\t\"capacity_mb=%u\"\t},\ndrivers/block/zloop.c:48:\t{ ZLOOP_OPT_ZONE_SIZE,\t\t\"zone_size_mb=%u\"\t},\ndrivers/block/zloop.c:49:\t{ ZLOOP_OPT_ZONE_CAPACITY,\t\"zone_capacity_mb=%u\"\t},\ndrivers/block/zloop.c:50:\t{ ZLOOP_OPT_NR_CONV_ZONES,\t\"conv_zones=%u\"\t\t},\ndrivers/block/zloop.c:51:\t{ ZLOOP_OPT_BASE_DIR,\t\t\"base_dir=%s\"\t\t},\ndrivers/block/zloop.c:52:\t{ ZLOOP_OPT_NR_QUEUES,\t\t\"nr_queues=%u\"\t\t},\ndrivers/block/zloop.c:53:\t{ ZLOOP_OPT_QUEUE_DEPTH,\t\"queue_depth=%u\"\t},\ndrivers/block/zloop.c:54:\t{ ZLOOP_OPT_BUFFERED_IO,\t\"buffered_io\"\t\t},\ndrivers/block/zloop.c:55:\t{ ZLOOP_OPT_ZONE_APPEND,\t\"zone_append=%u\"\t},\ndrivers/block/zloop.c:56:\t{ ZLOOP_OPT_ORDERED_ZONE_APPEND, \"ordered_zone_append\"\t},\ndrivers/block/zloop.c:57:\t{ ZLOOP_OPT_DISCARD_WRITE_CACHE, \"discard_write_cache\"\t},\ndrivers/block/zloop.c:58:\t{ ZLOOP_OPT_MAX_OPEN_ZONES,\t\"max_open_zones=%u\"\t},\ndrivers/block/zloop.c:59:\t{ ZLOOP_OPT_STOR_ELEMENTS,\t\"stor_elements=%u\"\t},\ndrivers/block/zloop.c:60:\t{ ZLOOP_OPT_ELEMENT_ID,\t\t\"element_id=%u\"\t\t},\ndrivers/block/zloop.c:61:\t{ ZLOOP_OPT_ERR,\t\tNULL\t\t\t}\ndrivers/block/zloop.c-62-};\n--\ndrivers/block/zloop.c=65=enum zloop_stor_elements {\ndrivers/block/zloop.c-66-\t/* No emulation. */\ndrivers/block/zloop.c:67:\tZLOOP_STOR_ELEMENTS_NONE,\ndrivers/block/zloop.c-68-\t/* Emulate read+write storage elements. */\ndrivers/block/zloop.c:69:\tZLOOP_STOR_ELEMENTS_RDWR,\ndrivers/block/zloop.c-70-\t/* Emulate pairs of associated read and write storage elements. */\ndrivers/block/zloop.c:71:\tZLOOP_STOR_ELEMENTS_PAIRS,\ndrivers/block/zloop.c-72-};\n--\ndrivers/block/zloop.c-74-/* Default values for the \"add\" operation. */\ndrivers/block/zloop.c:75:#define ZLOOP_DEF_ID\t\t\t-1\ndrivers/block/zloop.c:76:#define ZLOOP_DEF_ZONE_SIZE\t\t((256ULL * SZ_1M) \u003e\u003e SECTOR_SHIFT)\ndrivers/block/zloop.c:77:#define ZLOOP_DEF_NR_ZONES\t\t64\ndrivers/block/zloop.c:78:#define ZLOOP_DEF_NR_CONV_ZONES\t\t8\ndrivers/block/zloop.c:79:#define ZLOOP_DEF_MAX_OPEN_ZONES\t0\ndrivers/block/zloop.c:80:#define ZLOOP_DEF_BASE_DIR\t\t\"/var/local/zloop\"\ndrivers/block/zloop.c:81:#define ZLOOP_DEF_NR_QUEUES\t\t1\ndrivers/block/zloop.c:82:#define ZLOOP_DEF_QUEUE_DEPTH\t\t128\ndrivers/block/zloop.c:83:#define ZLOOP_DEF_BUFFERED_IO\t\tfalse\ndrivers/block/zloop.c:84:#define ZLOOP_DEF_ZONE_APPEND\t\ttrue\ndrivers/block/zloop.c:85:#define ZLOOP_DEF_ORDERED_ZONE_APPEND\tfalse\ndrivers/block/zloop.c:86:#define ZLOOP_DEF_STOR_ELEMENTS\t\tZLOOP_STOR_ELEMENTS_NONE\ndrivers/block/zloop.c:87:#define ZLOOP_DEF_ELEMENT_ID\t\t0\ndrivers/block/zloop.c-88-\ndrivers/block/zloop.c-89-/* Arbitrary limit on the zone size (16GB). */\ndrivers/block/zloop.c:90:#define ZLOOP_MAX_ZONE_SIZE_MB\t\t16384\ndrivers/block/zloop.c-91-\n--\ndrivers/block/zloop.c=120=enum zloop_zone_flags {\ndrivers/block/zloop.c:121:\tZLOOP_ZONE_CONV = 0,\ndrivers/block/zloop.c:122:\tZLOOP_ZONE_SEQ_ERROR,\ndrivers/block/zloop.c-123-};\n--\ndrivers/block/zloop.c=382=static int zloop_update_seq_zone(struct zloop_device *zlo, unsigned int zone_no)\n--\ndrivers/block/zloop.c-395-\tif (!zloop_zone_is_ok(zlo, zone)) {\ndrivers/block/zloop.c:396:\t\tset_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags);\ndrivers/block/zloop.c-397-\t\treturn -EIO;\n--\ndrivers/block/zloop.c-403-\t\t zlo-\u003edisk-\u003epart0, zone_no, ret);\ndrivers/block/zloop.c:404:\t\tset_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags);\ndrivers/block/zloop.c-405-\t\treturn ret;\n--\ndrivers/block/zloop.c=438=static int zloop_open_zone(struct zloop_device *zlo, unsigned int zone_no)\n--\ndrivers/block/zloop.c-442-\ndrivers/block/zloop.c:443:\tif (test_bit(ZLOOP_ZONE_CONV, \u0026zone-\u003eflags))\ndrivers/block/zloop.c-444-\t\treturn -EIO;\n--\ndrivers/block/zloop.c-452-\ndrivers/block/zloop.c:453:\tif (test_and_clear_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags)) {\ndrivers/block/zloop.c-454-\t\tret = zloop_update_seq_zone(zlo, zone_no);\n--\ndrivers/block/zloop.c=468=static int zloop_close_zone(struct zloop_device *zlo, unsigned int zone_no)\n--\ndrivers/block/zloop.c-472-\ndrivers/block/zloop.c:473:\tif (test_bit(ZLOOP_ZONE_CONV, \u0026zone-\u003eflags))\ndrivers/block/zloop.c-474-\t\treturn -EIO;\n--\ndrivers/block/zloop.c-482-\ndrivers/block/zloop.c:483:\tif (test_and_clear_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags)) {\ndrivers/block/zloop.c-484-\t\tret = zloop_update_seq_zone(zlo, zone_no);\n--\ndrivers/block/zloop.c=515=static int zloop_do_reset_zone(struct zloop_device *zlo,\n--\ndrivers/block/zloop.c-522-\tzloop_set_zone_empty(zlo, zone);\ndrivers/block/zloop.c:523:\tclear_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags);\ndrivers/block/zloop.c-524-\tspin_unlock(\u0026zone-\u003ewp_lock);\n--\ndrivers/block/zloop.c=529=static int zloop_reset_zone(struct zloop_device *zlo, unsigned int zone_no,\n--\ndrivers/block/zloop.c-534-\ndrivers/block/zloop.c:535:\tif (test_bit(ZLOOP_ZONE_CONV, \u0026zone-\u003eflags))\ndrivers/block/zloop.c-536-\t\treturn -EIO;\n--\ndrivers/block/zloop.c-545-\ndrivers/block/zloop.c:546:\tif (!test_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags) \u0026\u0026\ndrivers/block/zloop.c-547-\t zone-\u003econd == BLK_ZONE_COND_EMPTY)\n--\ndrivers/block/zloop.c-551-\tif (ret)\ndrivers/block/zloop.c:552:\t\tset_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags);\ndrivers/block/zloop.c-553-\n--\ndrivers/block/zloop.c=574=static int zloop_finish_zone(struct zloop_device *zlo, unsigned int zone_no)\n--\ndrivers/block/zloop.c-578-\ndrivers/block/zloop.c:579:\tif (test_bit(ZLOOP_ZONE_CONV, \u0026zone-\u003eflags))\ndrivers/block/zloop.c-580-\t\treturn -EIO;\n--\ndrivers/block/zloop.c-588-\ndrivers/block/zloop.c:589:\tif (!test_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags) \u0026\u0026\ndrivers/block/zloop.c-590-\t zone-\u003econd == BLK_ZONE_COND_FULL)\n--\ndrivers/block/zloop.c-594-\t\t\t zlo-\u003ezone_capacity \u003c\u003c SECTOR_SHIFT)) {\ndrivers/block/zloop.c:595:\t\tset_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags);\ndrivers/block/zloop.c-596-\t\tret = -EIO;\n--\ndrivers/block/zloop.c-601-\tzloop_set_zone_full(zlo, zone);\ndrivers/block/zloop.c:602:\tclear_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags);\ndrivers/block/zloop.c-603-\tspin_unlock(\u0026zone-\u003ewp_lock);\n--\ndrivers/block/zloop.c=752=static void zloop_rw(struct zloop_cmd *cmd)\n--\ndrivers/block/zloop.c-784-\ndrivers/block/zloop.c:785:\tif (test_and_clear_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags)) {\ndrivers/block/zloop.c-786-\t\tmutex_lock(\u0026zone-\u003elock);\n--\ndrivers/block/zloop.c-792-\ndrivers/block/zloop.c:793:\tif (!test_bit(ZLOOP_ZONE_CONV, \u0026zone-\u003eflags) \u0026\u0026 is_write) {\ndrivers/block/zloop.c-794-\t\tmutex_lock(\u0026zone-\u003elock);\n--\ndrivers/block/zloop.c=942=static void zloop_complete_rq(struct request *rq)\n--\ndrivers/block/zloop.c-975-\ndrivers/block/zloop.c:976:\t\tif (cmd-\u003eret \u003c 0 \u0026\u0026 !test_bit(ZLOOP_ZONE_CONV, \u0026zone-\u003eflags)) {\ndrivers/block/zloop.c-977-\t\t\t/*\n--\ndrivers/block/zloop.c-981-\t\t\t */\ndrivers/block/zloop.c:982:\t\t\tset_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags);\ndrivers/block/zloop.c-983-\t\t\tbreak;\n--\ndrivers/block/zloop.c=1123=static int zloop_report_zones(struct gendisk *disk, sector_t sector,\n--\ndrivers/block/zloop.c-1141-\ndrivers/block/zloop.c:1142:\t\tif (test_and_clear_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags)) {\ndrivers/block/zloop.c-1143-\t\t\tret = zloop_update_seq_zone(zlo, zone_no);\n--\ndrivers/block/zloop.c-1155-\t\tspin_unlock(\u0026zone-\u003ewp_lock);\ndrivers/block/zloop.c:1156:\t\tif (test_bit(ZLOOP_ZONE_CONV, \u0026zone-\u003eflags)) {\ndrivers/block/zloop.c-1157-\t\t\tblkz.type = BLK_ZONE_TYPE_CONVENTIONAL;\n--\ndrivers/block/zloop.c=1174=static int zloop_report_elements(struct gendisk *disk,\n--\ndrivers/block/zloop.c-1185-\ndrivers/block/zloop.c:1186:\tif (zlo-\u003estor_elements == ZLOOP_STOR_ELEMENTS_NONE)\ndrivers/block/zloop.c-1187-\t\treturn -EOPNOTSUPP;\n--\ndrivers/block/zloop.c=1215=static int zloop_degrade_element(struct zloop_device *zlo,\n--\ndrivers/block/zloop.c-1220-\ndrivers/block/zloop.c:1221:\tif (zlo-\u003estor_elements == ZLOOP_STOR_ELEMENTS_NONE)\ndrivers/block/zloop.c-1222-\t\treturn -EOPNOTSUPP;\n--\ndrivers/block/zloop.c=1240=static int zloop_execute_remove_element(struct zloop_device *zlo)\n--\ndrivers/block/zloop.c-1283-\t\tzloop_set_zone_cond(zlo, zone, cond);\ndrivers/block/zloop.c:1284:\t\tclear_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags);\ndrivers/block/zloop.c-1285-\t\tspin_unlock(\u0026zone-\u003ewp_lock);\n--\ndrivers/block/zloop.c=1299=static int zloop_remove_element(struct gendisk *disk, unsigned int element_id)\n--\ndrivers/block/zloop.c-1309-\ndrivers/block/zloop.c:1310:\tif (zlo-\u003estor_elements == ZLOOP_STOR_ELEMENTS_NONE)\ndrivers/block/zloop.c-1311-\t\treturn -EOPNOTSUPP;\n--\ndrivers/block/zloop.c=1382=static int zloop_restore_zone(struct zloop_device *zlo,\n--\ndrivers/block/zloop.c-1388-\ndrivers/block/zloop.c:1389:\tif (!test_bit(ZLOOP_ZONE_CONV, \u0026zone-\u003eflags)) {\ndrivers/block/zloop.c-1390-\t\tret = vfs_truncate(\u0026zone-\u003efile-\u003ef_path, 0);\ndrivers/block/zloop.c-1391-\t\tif (ret) {\ndrivers/block/zloop.c:1392:\t\t\tset_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags);\n\n"
} |
| 5/2 | 2026/11/12 05:09 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 40449 output: 37 thoughts: 10 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience, presented in the first person: **Navigating the Kconfig for zloop** Right, let's dive into the Kconfig files for the zloop subsystem. I need to refresh my memory on what options are available and how they're structured. My initial focus is likely going to be on: * **Dependencies and Relationships:** I'll look for how the zloop configuration options relate to other kernel features. Are there any dependencies on specific drivers, hardware architectures, or other subsystems? What are the implications of enabling or disabling certain zloop features on the wider system? I'll probably start by grepping for "depends on" and "select" to quickly identify these interconnections. * **Configurable Parameters:** I'll want to quickly identify the range of configurable options. Things like enabling/disabling the module, tracepoints, or setting certain parameters, and what the default settings are. Are there any advanced or experimental features? I want to understand the knobs that I can tweak, and their impact. * **Help Texts and Documentation:** Hopefully, the Kconfig includes informative help texts. Good documentation is critical! I'm hoping it's clear and concise, detailing the purpose and implications of each option. Poor help text is a red flag, and I'll have to dig deeper, likely resorting to code inspection and cross-referencing with documentation to fully understand the features. * **Historical Context:** If possible, I'll search for commit messages related to the zloop Kconfig options. This might give me insights into the rationale behind particular design choices or the evolution of the configuration options over time. I'm essentially trying to build a mental map of the zloop's configurable landscape within the kernel. Armed with this understanding, I can then effectively configure the system, debug issues, and/or contribute improvements with greater confidence. |
| 6/2 | 2026/11/12 05:09 | tool | grepper |
0mArgs:{
"Expression": "zloop",
"PathPrefixes": [
"drivers/block/Kconfig",
"drivers/block/Makefile"
]
}Results: {
"Output": "drivers/block/Kconfig=356=config BLK_DEV_ZONED_LOOP\n--\ndrivers/block/Kconfig-367-\t echo \"add id=0,zone_size_mb=256,capacity_mb=16384,conv_zones=11\" \u003e \\\ndrivers/block/Kconfig:368:\t\t/dev/zloop-control\ndrivers/block/Kconfig-369-\n--\ndrivers/block/Makefile=40=obj-$(CONFIG_BLK_DEV_UBLK)\t\t\t+= ublk_drv.o\ndrivers/block/Makefile:41:obj-$(CONFIG_BLK_DEV_ZONED_LOOP) += zloop.o\ndrivers/block/Makefile-42-\n"
} |
| 7/2 | 2026/11/12 05:09 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 41092 output: 34 thoughts: 37 Thoughts: Here's my analysis of the provided text, as though it were my thought process: **Diving into the Zoned Block Device Loop Configuration** Okay, so I need to understand the dependencies of `CONFIG_BLK_DEV_ZONED_LOOP`. My initial instinct is to check the `Kconfig` file in the block driver directory. Specifically, I'll hone in on line 356, give or take a few lines, as that's usually where these configurations are defined. I anticipate that the dependencies, if any, will be clearly laid out within that section. This approach allows me to quickly ascertain whether `CONFIG_BLK_DEV_ZONED_LOOP` has any prerequisites, like needing another feature enabled, or if it's dependent on a specific hardware platform or kernel setting. Essentially, this Kconfig investigation should quickly clarify the context in which this loop device configuration can be used. |
| 8/2 | 2026/11/12 05:09 | tool | read-file |
0mArgs:{
"File": "drivers/block/Kconfig",
"FirstLine": 350,
"LineCount": 30
}Results: {
"Contents": " 350:\t\t Say N if you don't want to support legacy command opcode. It is\n 351:\t\t suggested to enable N if your application(ublk server) switches to\n 352:\t\t ioctl command encoding.\n 353:\t\n 354:\tsource \"drivers/block/rnbd/Kconfig\"\n 355:\t\n 356:\tconfig BLK_DEV_ZONED_LOOP\n 357:\t\ttristate \"Zoned loopback device support\"\n 358:\t\tdepends on BLK_DEV_ZONED\n 359:\t\thelp\n 360:\t\t Saying Y here will allow you to use create a zoned block device using\n 361:\t\t regular files for zones (one file per zones). This is useful to test\n 362:\t\t file systems, device mapper and applications that support zoned block\n 363:\t\t devices. To create a zoned loop device, no user utility is needed, a\n 364:\t\t zoned loop device can be created (or re-started) using a command\n 365:\t\t like:\n 366:\t\n 367:\t\t echo \"add id=0,zone_size_mb=256,capacity_mb=16384,conv_zones=11\" \u003e \\\n 368:\t\t\t/dev/zloop-control\n 369:\t\n 370:\t\t See Documentation/admin-guide/blockdev/zoned_loop.rst for usage\n 371:\t\t details.\n 372:\t\n 373:\t\t If unsure, say N.\n 374:\t\n 375:\tendif # BLK_DEV\n"
} |
| 9/2 | 2026/11/12 05:09 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 41791 output: 29 thoughts: 43 Thoughts: Okay, let's see. My first thought is that `BLK_DEV_ZONED_LOOP` obviously hinges on `BLK_DEV_ZONED`. No surprises there; it's a pretty straightforward dependency. **Thinking Through Zoned Block Devices and SCSI** But that brings up another question, I'm already starting to think about SCSI ZBC. I should check `drivers/scsi/Kconfig` to see if there's any related configuration or dependencies for SCSI Zoned Block Commands. Better to get a clear picture of the landscape, as I'm thinking about related aspects here. |
| 10/2 | 2026/11/12 05:09 | tool | grepper |
0mArgs:{
"Expression": "sd_zbc",
"PathPrefixes": [
"drivers/scsi"
]
}Results: {
"Output": "\nFull output is too long, showing 200 out of 363 lines.\nUse more precise expression or PathPrefixes if possible.\n\nNumber of matching lines per file (4 files in total):\ndrivers/scsi/Makefile:1\ndrivers/scsi/sd.c:10\ndrivers/scsi/sd.h:11\ndrivers/scsi/sd_zbc.c:77\n\ndrivers/scsi/Makefile=178=sd_mod-$(CONFIG_BLK_DEV_INTEGRITY) += sd_dif.o\ndrivers/scsi/Makefile:179:sd_mod-$(CONFIG_BLK_DEV_ZONED) += sd_zbc.o\ndrivers/scsi/Makefile-180-\n--\ndrivers/scsi/sd.c=1555=static blk_status_t sd_init_command(struct scsi_cmnd *cmd)\n--\ndrivers/scsi/sd.c-1580-\tcase REQ_OP_ZONE_RESET:\ndrivers/scsi/sd.c:1581:\t\treturn sd_zbc_setup_zone_mgmt_cmnd(cmd, ZO_RESET_WRITE_POINTER,\ndrivers/scsi/sd.c-1582-\t\t\t\t\t\t false);\ndrivers/scsi/sd.c-1583-\tcase REQ_OP_ZONE_RESET_ALL:\ndrivers/scsi/sd.c:1584:\t\treturn sd_zbc_setup_zone_mgmt_cmnd(cmd, ZO_RESET_WRITE_POINTER,\ndrivers/scsi/sd.c-1585-\t\t\t\t\t\t true);\ndrivers/scsi/sd.c-1586-\tcase REQ_OP_ZONE_OPEN:\ndrivers/scsi/sd.c:1587:\t\treturn sd_zbc_setup_zone_mgmt_cmnd(cmd, ZO_OPEN_ZONE, false);\ndrivers/scsi/sd.c-1588-\tcase REQ_OP_ZONE_CLOSE:\ndrivers/scsi/sd.c:1589:\t\treturn sd_zbc_setup_zone_mgmt_cmnd(cmd, ZO_CLOSE_ZONE, false);\ndrivers/scsi/sd.c-1590-\tcase REQ_OP_ZONE_FINISH:\ndrivers/scsi/sd.c:1591:\t\treturn sd_zbc_setup_zone_mgmt_cmnd(cmd, ZO_FINISH_ZONE, false);\ndrivers/scsi/sd.c-1592-\tdefault:\n--\ndrivers/scsi/sd.c=2388=static int sd_done(struct scsi_cmnd *SCpnt)\n--\ndrivers/scsi/sd.c-2494-\tif (sdkp-\u003edevice-\u003etype == TYPE_ZBC)\ndrivers/scsi/sd.c:2495:\t\tgood_bytes = sd_zbc_complete(SCpnt, good_bytes, \u0026sshdr);\ndrivers/scsi/sd.c-2496-\n--\ndrivers/scsi/sd.c=3793=static void sd_revalidate_disk(struct gendisk *disk)\n--\ndrivers/scsi/sd.c-3850-\t\t\tsd_read_block_characteristics(sdkp, lim);\ndrivers/scsi/sd.c:3851:\t\t\tsd_zbc_read_zones(sdkp, lim, buffer);\ndrivers/scsi/sd.c-3852-\t\t}\n--\ndrivers/scsi/sd.c-3917-\t */\ndrivers/scsi/sd.c:3918:\tif (sd_zbc_revalidate_zones(sdkp))\ndrivers/scsi/sd.c-3919-\t\tset_capacity_and_notify(disk, 0);\n--\ndrivers/scsi/sd.c=3947=static const struct block_device_operations sd_fops = {\n--\ndrivers/scsi/sd.c-3955-\t.unlock_native_capacity\t= sd_unlock_native_capacity,\ndrivers/scsi/sd.c:3956:\t.report_zones\t\t= sd_zbc_report_zones,\ndrivers/scsi/sd.c-3957-\t.get_unique_id\t\t= sd_get_unique_id,\n--\ndrivers/scsi/sd.c-3960-#ifdef CONFIG_BLK_DEV_ZONED\ndrivers/scsi/sd.c:3961:\t.se_ops\t\t\t= \u0026sd_zbc_se_ops,\ndrivers/scsi/sd.c-3962-#endif\n--\ndrivers/scsi/sd.h=234=void sd_dif_config_host(struct scsi_disk *sdkp, struct queue_limits *lim);\n--\ndrivers/scsi/sd.h-237-\ndrivers/scsi/sd.h:238:int sd_zbc_read_zones(struct scsi_disk *sdkp, struct queue_limits *lim,\ndrivers/scsi/sd.h-239-\t\tu8 buf[SD_BUF_SIZE]);\ndrivers/scsi/sd.h:240:int sd_zbc_revalidate_zones(struct scsi_disk *sdkp);\ndrivers/scsi/sd.h:241:blk_status_t sd_zbc_setup_zone_mgmt_cmnd(struct scsi_cmnd *cmd,\ndrivers/scsi/sd.h-242-\t\t\t\t\t unsigned char op, bool all);\ndrivers/scsi/sd.h:243:unsigned int sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes,\ndrivers/scsi/sd.h-244-\t\t\t struct scsi_sense_hdr *sshdr);\ndrivers/scsi/sd.h:245:int sd_zbc_report_zones(struct gendisk *disk, sector_t sector,\ndrivers/scsi/sd.h-246-\t\tunsigned int nr_zones, struct blk_report_zones_args *args);\ndrivers/scsi/sd.h-247-\ndrivers/scsi/sd.h:248:extern const struct blk_se_ops sd_zbc_se_ops;\ndrivers/scsi/sd.h-249-\n--\ndrivers/scsi/sd.h-251-\ndrivers/scsi/sd.h:252:static inline int sd_zbc_read_zones(struct scsi_disk *sdkp,\ndrivers/scsi/sd.h-253-\t\tstruct queue_limits *lim, u8 buf[SD_BUF_SIZE])\n--\ndrivers/scsi/sd.h-257-\ndrivers/scsi/sd.h:258:static inline int sd_zbc_revalidate_zones(struct scsi_disk *sdkp)\ndrivers/scsi/sd.h-259-{\n--\ndrivers/scsi/sd.h-262-\ndrivers/scsi/sd.h:263:static inline blk_status_t sd_zbc_setup_zone_mgmt_cmnd(struct scsi_cmnd *cmd,\ndrivers/scsi/sd.h-264-\t\t\t\t\t\t unsigned char op,\n--\ndrivers/scsi/sd.h-269-\ndrivers/scsi/sd.h:270:static inline unsigned int sd_zbc_complete(struct scsi_cmnd *cmd,\ndrivers/scsi/sd.h-271-\t\t\tunsigned int good_bytes, struct scsi_sense_hdr *sshdr)\n--\ndrivers/scsi/sd.h-275-\ndrivers/scsi/sd.h:276:#define sd_zbc_report_zones NULL\ndrivers/scsi/sd.h-277-\n--\ndrivers/scsi/sd_zbc.c-27-/* Whether or not a SCSI zone descriptor describes a gap zone. */\ndrivers/scsi/sd_zbc.c:28:static bool sd_zbc_is_gap_zone(const u8 buf[64])\ndrivers/scsi/sd_zbc.c-29-{\n--\ndrivers/scsi/sd_zbc.c-33-/**\ndrivers/scsi/sd_zbc.c:34: * sd_zbc_parse_report - Parse a SCSI zone descriptor\ndrivers/scsi/sd_zbc.c-35- * @sdkp: SCSI disk pointer.\n--\ndrivers/scsi/sd_zbc.c-37- * @idx: Index of the zone relative to the first zone reported by the current\ndrivers/scsi/sd_zbc.c:38: *\tsd_zbc_report_zones() call.\ndrivers/scsi/sd_zbc.c-39- * @args: report zones arguments (callback, etc)\n--\ndrivers/scsi/sd_zbc.c-45- */\ndrivers/scsi/sd_zbc.c:46:static int sd_zbc_parse_report(struct scsi_disk *sdkp, const u8 buf[64],\ndrivers/scsi/sd_zbc.c-47-\t\t\tunsigned int idx, struct blk_report_zones_args *args)\n--\ndrivers/scsi/sd_zbc.c-52-\ndrivers/scsi/sd_zbc.c:53:\tif (WARN_ON_ONCE(sd_zbc_is_gap_zone(buf)))\ndrivers/scsi/sd_zbc.c-54-\t\treturn -EINVAL;\n--\ndrivers/scsi/sd_zbc.c-92-/**\ndrivers/scsi/sd_zbc.c:93: * sd_zbc_do_report_zones - Issue a REPORT ZONES scsi command.\ndrivers/scsi/sd_zbc.c-94- * @sdkp: The target disk\n--\ndrivers/scsi/sd_zbc.c-105- */\ndrivers/scsi/sd_zbc.c:106:static int sd_zbc_do_report_zones(struct scsi_disk *sdkp, unsigned char *buf,\ndrivers/scsi/sd_zbc.c-107-\t\t\t\t unsigned int buflen, sector_t lba,\n--\ndrivers/scsi/sd_zbc.c-150-/**\ndrivers/scsi/sd_zbc.c:151: * sd_zbc_alloc_report_buffer() - Allocate a buffer for report zones reply.\ndrivers/scsi/sd_zbc.c-152- * @sdkp: The target disk\n--\ndrivers/scsi/sd_zbc.c-162- */\ndrivers/scsi/sd_zbc.c:163:static void *sd_zbc_alloc_report_buffer(struct scsi_disk *sdkp,\ndrivers/scsi/sd_zbc.c-164-\t\t\t\t\tunsigned int nr_zones, size_t *buflen)\n--\ndrivers/scsi/sd_zbc.c-201-/**\ndrivers/scsi/sd_zbc.c:202: * sd_zbc_zone_sectors - Get the device zone size in number of 512B sectors.\ndrivers/scsi/sd_zbc.c-203- * @sdkp: The target disk\ndrivers/scsi/sd_zbc.c-204- */\ndrivers/scsi/sd_zbc.c:205:static inline sector_t sd_zbc_zone_sectors(struct scsi_disk *sdkp)\ndrivers/scsi/sd_zbc.c-206-{\n--\ndrivers/scsi/sd_zbc.c-210-/**\ndrivers/scsi/sd_zbc.c:211: * sd_zbc_report_zones - SCSI .report_zones() callback.\ndrivers/scsi/sd_zbc.c-212- * @disk: Disk to report zones for.\n--\ndrivers/scsi/sd_zbc.c-219- */\ndrivers/scsi/sd_zbc.c:220:int sd_zbc_report_zones(struct gendisk *disk, sector_t sector,\ndrivers/scsi/sd_zbc.c-221-\t\t\tunsigned int nr_zones,\n--\ndrivers/scsi/sd_zbc.c-240-\ndrivers/scsi/sd_zbc.c:241:\tbuf = sd_zbc_alloc_report_buffer(sdkp, nr_zones, \u0026buflen);\ndrivers/scsi/sd_zbc.c-242-\tif (!buf)\n--\ndrivers/scsi/sd_zbc.c-245-\twhile (zone_idx \u003c nr_zones \u0026\u0026 lba \u003c sdkp-\u003ecapacity) {\ndrivers/scsi/sd_zbc.c:246:\t\tret = sd_zbc_do_report_zones(sdkp, buf, buflen, lba, true);\ndrivers/scsi/sd_zbc.c-247-\t\tif (ret)\n--\ndrivers/scsi/sd_zbc.c-270-\t\t\tlba = start_lba + zone_length;\ndrivers/scsi/sd_zbc.c:271:\t\t\tif (sd_zbc_is_gap_zone(\u0026buf[offset])) {\ndrivers/scsi/sd_zbc.c-272-\t\t\t\tif (sdkp-\u003ezone_starting_lba_gran)\n--\ndrivers/scsi/sd_zbc.c-279-\ndrivers/scsi/sd_zbc.c:280:\t\t\tret = sd_zbc_parse_report(sdkp, buf + offset, zone_idx,\ndrivers/scsi/sd_zbc.c-281-\t\t\t\t\t\t args);\n--\ndrivers/scsi/sd_zbc.c-294-\ndrivers/scsi/sd_zbc.c:295:static blk_status_t sd_zbc_cmnd_checks(struct scsi_cmnd *cmd)\ndrivers/scsi/sd_zbc.c-296-{\n--\ndrivers/scsi/sd_zbc.c-307-\ndrivers/scsi/sd_zbc.c:308:\tif (sector \u0026 (sd_zbc_zone_sectors(sdkp) - 1))\ndrivers/scsi/sd_zbc.c-309-\t\t/* Unaligned request */\n--\ndrivers/scsi/sd_zbc.c-315-/**\ndrivers/scsi/sd_zbc.c:316: * sd_zbc_setup_zone_mgmt_cmnd - Prepare a zone ZBC_OUT command. The operations\ndrivers/scsi/sd_zbc.c-317- *\t\t\tcan be RESET WRITE POINTER, OPEN, CLOSE or FINISH.\n--\ndrivers/scsi/sd_zbc.c-324- */\ndrivers/scsi/sd_zbc.c:325:blk_status_t sd_zbc_setup_zone_mgmt_cmnd(struct scsi_cmnd *cmd,\ndrivers/scsi/sd_zbc.c-326-\t\t\t\t\t unsigned char op, bool all)\n--\ndrivers/scsi/sd_zbc.c-333-\ndrivers/scsi/sd_zbc.c:334:\tret = sd_zbc_cmnd_checks(cmd);\ndrivers/scsi/sd_zbc.c-335-\tif (ret != BLK_STS_OK)\n--\ndrivers/scsi/sd_zbc.c-355-/**\ndrivers/scsi/sd_zbc.c:356: * sd_zbc_complete - ZBC command post processing.\ndrivers/scsi/sd_zbc.c-357- * @cmd: Completed command\n--\ndrivers/scsi/sd_zbc.c-363- */\ndrivers/scsi/sd_zbc.c:364:unsigned int sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes,\ndrivers/scsi/sd_zbc.c-365-\t\t struct scsi_sense_hdr *sshdr)\n--\ndrivers/scsi/sd_zbc.c-385-/**\ndrivers/scsi/sd_zbc.c:386: * sd_zbc_check_zoned_characteristics - Check zoned block device characteristics\ndrivers/scsi/sd_zbc.c-387- * @sdkp: Target disk\n--\ndrivers/scsi/sd_zbc.c-391- */\ndrivers/scsi/sd_zbc.c:392:static int sd_zbc_check_zoned_characteristics(struct scsi_disk *sdkp,\ndrivers/scsi/sd_zbc.c-393-\t\t\t\t\t unsigned char *buf)\n--\ndrivers/scsi/sd_zbc.c-456-/**\ndrivers/scsi/sd_zbc.c:457: * sd_zbc_check_capacity - Check the device capacity\ndrivers/scsi/sd_zbc.c-458- * @sdkp: Target disk\n--\ndrivers/scsi/sd_zbc.c-467- */\ndrivers/scsi/sd_zbc.c:468:static int sd_zbc_check_capacity(struct scsi_disk *sdkp, unsigned char *buf,\ndrivers/scsi/sd_zbc.c-469-\t\t\t\t u32 *zblocks)\n--\ndrivers/scsi/sd_zbc.c-476-\t/* Do a report zone to get max_lba and the size of the first zone */\ndrivers/scsi/sd_zbc.c:477:\tret = sd_zbc_do_report_zones(sdkp, buf, SD_BUF_SIZE, 0, false);\ndrivers/scsi/sd_zbc.c-478-\tif (ret)\n--\ndrivers/scsi/sd_zbc.c-520-/*\ndrivers/scsi/sd_zbc.c:521: * sd_zbc_check_depop_supported - Check if the device supports storage element\ndrivers/scsi/sd_zbc.c-522- *\t\t\t\t depopulation\n\n"
} |
| 11/2 | 2026/11/12 05:09 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 46483 output: 37 thoughts: 48 Thoughts: Okay, let's dive into this zloop situation. **Investigating zloop Device Creation and Registration** Right, so I'm curious about how this `zloop` device registration and its usage are handled. I need to understand the lifecycle of this thing. Specifically, how is that `/dev/zloop-control` device file actually created? Time to go digging! I'll need to check the source code, specifically `drivers/block/zloop.c`. Let's see what secrets that holds. I'm hoping the code provides the crucial clues to unravel this process. |
| 12/2 | 2026/11/12 05:09 | tool | grepper |
0mArgs:{
"Expression": "misc_register|zloop-control",
"PathPrefixes": [
"drivers/block/zloop.c"
]
}Results: {
"Output": "drivers/block/zloop.c=2482=static struct miscdevice zloop_misc = {\ndrivers/block/zloop.c-2483-\t.minor\t\t= MISC_DYNAMIC_MINOR,\ndrivers/block/zloop.c:2484:\t.name\t\t= \"zloop-control\",\ndrivers/block/zloop.c-2485-\t.fops\t\t= \u0026zloop_ctl_fops,\n--\ndrivers/block/zloop.c=2488=static int __init zloop_init(void)\n--\ndrivers/block/zloop.c-2491-\ndrivers/block/zloop.c:2492:\tret = misc_register(\u0026zloop_misc);\ndrivers/block/zloop.c-2493-\tif (ret) {\n"
} |
| 13/2 | 2026/11/12 05:09 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 46906 output: 37 thoughts: 264 Thoughts: **My Analysis of the Zloop Device Implementation** Okay, so I'm looking at how this `zloop` device is set up. From what I can gather, `/dev/zloop-control` is registered as a miscellaneous device using `misc_register(&zloop_misc)`. That makes sense - it's the control interface. Then, writing commands to this control device, like adding storage elements using a format like "add ... stor_elements=1 ...", is what creates the actual `zloop` block devices, `/dev/zloopX`. This implies a dynamic creation based on user commands. Crucially, once these `/dev/zloopX` devices are up, they are block devices with `zloop_se_ops` assigned as their operation structure. That's how we get block-level access. And aha! That's where it clicks. User space can then interact with the `/dev/zloopX` devices using block IOCTLs. Things like `BLKREPORTSTORELEMS`, `BLKREMOVESTORELEM`, and `BLKRESTORESTORELEMS` come to mind – those sound like they're for managing storage element properties. So, in effect, `/dev/zloopX` acts as the interface to the underlying storage elements. It provides both read/write access and the ability to control the storage elements using these specific ioctls. Now, I need to verify my assumptions, so let me quickly jump over to `block/blk-zoned.c`. I need to inspect the source code for those ioctls I mentioned. Specifically, `blkdev_ioctl_report_storage_elements`, `blkdev_ioctl_remove_storage_element`, and `blkdev_ioctl_restore_storage_elements`. Let's start around line 2737 and see what's really happening. |
| 14/2 | 2026/11/12 05:09 | tool | read-file |
0mArgs:{
"File": "block/blk-zoned.c",
"FirstLine": 2737,
"LineCount": 100
}Results: {
"Contents": "2737:\t}\n2738:\t\n2739:\t#endif\n2740:\t\n2741:\t/**\n2742:\t * bdev_report_storage_elements - report the storage elements of a zoned\n2743:\t *\t\t\t\t block device\n2744:\t * @bdev:\tthe zoned block device to check\n2745:\t * @elements:\tarray of storage element descriptors to fill\n2746:\t * @nr_elements: maximum size of @elements\n2747:\t *\n2748:\t * Fill at most @nr_elements storage element descriptors in the array\n2749:\t * @elements. The number of storage elements filled in the array is returned\n2750:\t * using @nr_elements. If @elements is NULL, only @nr_elements is returned to\n2751:\t * indicate the maximum number of storage elements of @bdev. This function may\n2752:\t * sleep.\n2753:\t *\n2754:\t * Returns 0 on success and a negative error code on failure.\n2755:\t */\n2756:\tint bdev_report_storage_elements(struct block_device *bdev,\n2757:\t\t\t\t\t struct blk_se *elements,\n2758:\t\t\t\t\t unsigned int *nr_elements)\n2759:\t{\n2760:\t\tstruct gendisk *disk = bdev-\u003ebd_disk;\n2761:\t\n2762:\t\tif (test_bit(GD_DEAD, \u0026disk-\u003estate))\n2763:\t\t\treturn -ENODEV;\n2764:\t\n2765:\t\tif (!blk_queue_is_zoned(disk-\u003equeue) || !disk-\u003enr_storage_elements)\n2766:\t\t\treturn -EOPNOTSUPP;\n2767:\t\n2768:\t\tif (!nr_elements)\n2769:\t\t\treturn -EINVAL;\n2770:\t\n2771:\t\tif (!elements) {\n2772:\t\t\t*nr_elements = disk-\u003enr_storage_elements;\n2773:\t\t\treturn 0;\n2774:\t\t}\n2775:\t\n2776:\t\t*nr_elements = min(*nr_elements, disk-\u003enr_storage_elements);\n2777:\t\tif (!*nr_elements)\n2778:\t\t\treturn -EINVAL;\n2779:\t\n2780:\t\treturn disk-\u003efops-\u003ese_ops-\u003ereport_elements(disk, elements,\n2781:\t\t\t\t\t\t\t nr_elements);\n2782:\t}\n2783:\tEXPORT_SYMBOL_GPL(bdev_report_storage_elements);\n2784:\t\n2785:\tstatic int blkdev_ioctl_report_storage_elements(struct block_device *bdev,\n2786:\t\t\t\t\t\t\tvoid __user *argp)\n2787:\t{\n2788:\t\tstruct gendisk *disk = bdev-\u003ebd_disk;\n2789:\t\tstruct blk_se_report *rep;\n2790:\t\tint ret = 0;\n2791:\t\n2792:\t\t/*\n2793:\t\t * Allocate the report header with an array of elements, assuming that\n2794:\t\t * we need the maximum number of storage elements. In practice, we may\n2795:\t\t * need less than that, but this potentially unnecessary preallocation\n2796:\t\t * simplifies the code.\n2797:\t\t */\n2798:\t\trep = kzalloc_flex(*rep, elements, disk-\u003enr_storage_elements);\n2799:\t\tif (!rep)\n2800:\t\t\treturn -ENOMEM;\n2801:\t\n2802:\t\tif (copy_from_user(rep, argp, sizeof(struct blk_se_report))) {\n2803:\t\t\tret = -EFAULT;\n2804:\t\t\tgoto free_rep;\n2805:\t\t}\n2806:\t\n2807:\t\tif (rep-\u003ereserved) {\n2808:\t\t\tret = -EINVAL;\n2809:\t\t\tgoto free_rep;\n2810:\t\t}\n2811:\t\n2812:\t\trep-\u003enr_reported_elements =\n2813:\t\t\tmin(rep-\u003enr_elements, disk-\u003enr_storage_elements);\n2814:\t\trep-\u003enr_elements = disk-\u003enr_storage_elements;\n2815:\t\n2816:\t\tif (rep-\u003enr_reported_elements) {\n2817:\t\t\tret = bdev_report_storage_elements(bdev, rep-\u003eelements,\n2818:\t\t\t\t\t\t\t \u0026rep-\u003enr_reported_elements);\n2819:\t\t\tif (ret)\n2820:\t\t\t\tgoto free_rep;\n2821:\t\t}\n2822:\t\n2823:\t\tif (copy_to_user(argp, rep,\n2824:\t\t\t\tstruct_size(rep, elements, rep-\u003enr_reported_elements)))\n2825:\t\t\tret = -EFAULT;\n2826:\t\n2827:\tfree_rep:\n2828:\t\tkfree(rep);\n2829:\t\treturn ret;\n2830:\t}\n2831:\t\n2832:\t/**\n2833:\t * bdev_remove_storage_element - Remove (depopulate) a storage element of a\n2834:\t *\t\t\t\t zoned block device\n2835:\t * @bdev:\tblock device to operate on\n2836:\t * @element_id:\tID of the storage element to remove\n"
} |
| 15/2 | 2026/11/12 05:09 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 49042 output: 37 thoughts: 16 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience: **Diving into block/blk-zoned.c** Alright, let's get down to brass tacks and dig deeper. I'm going to shift my focus and start perusing the `block/blk-zoned.c` file. I'm expecting to find the nitty-gritty details of how zoned block devices are managed and how the complexities are handled at a lower level. This likely contains the core logic for zone management, including operations on zone boundaries, potentially garbage collection strategies for SMR drives, and the handling of various zoned device types. Given that this is the core of the zoned block device implementation, I'm anticipating some interesting code structure and potentially some intricate locking strategies to deal with the concurrent access to zones. I am specifically interested in how this interacts with the generic block layer and how the abstractions are implemented. Time to put on my code-reading hat and see what gems I can unearth. |
| 16/2 | 2026/11/12 05:09 | tool | read-file |
0mArgs:{
"File": "block/blk-zoned.c",
"FirstLine": 2837,
"LineCount": 100
}Results: {
"Contents": "2837:\t *\n2838:\t * Remove (depopulate) the storage element identified by @element_id from the\n2839:\t * block device @bdev. The caller is responsible for taking care of any\n2840:\t * necessary dirty page and device write cache flush and invalidation of cached\n2841:\t * data. This function will block until the storage element is removed or an\n2842:\t * error occurs.\n2843:\t *\n2844:\t * Returns 0 on success and a negative error code on failure.\n2845:\t */\n2846:\tint bdev_remove_storage_element(struct block_device *bdev,\n2847:\t\t\t\t\tunsigned int element_id)\n2848:\t{\n2849:\t\tstruct gendisk *disk = bdev-\u003ebd_disk;\n2850:\t\tunsigned int memflags;\n2851:\t\tint ret, err;\n2852:\t\n2853:\t\tif (test_bit(GD_DEAD, \u0026disk-\u003estate))\n2854:\t\t\treturn -ENODEV;\n2855:\t\n2856:\t\tif (!blk_queue_is_zoned(disk-\u003equeue) || !disk-\u003enr_storage_elements)\n2857:\t\t\treturn -EOPNOTSUPP;\n2858:\t\n2859:\t\t/* Zero is not a valid storage element ID. */\n2860:\t\tif (!element_id)\n2861:\t\t\treturn -EINVAL;\n2862:\t\n2863:\t\t/*\n2864:\t\t * Serialize against concurrent revalidation and concurrent storage\n2865:\t\t * element management operations so that we can see consistent zone\n2866:\t\t * condition changes.\n2867:\t\t */\n2868:\t\tmutex_lock(\u0026disk-\u003ezone_revalidate_mutex);\n2869:\t\n2870:\t\t/*\n2871:\t\t * Freeze and unfreeze the queue to flush any outstanding command.\n2872:\t\t * The caller is responsible for not queuing up more I/Os by higher\n2873:\t\t * level means.\n2874:\t\t */\n2875:\t\tmemflags = blk_mq_freeze_queue(disk-\u003equeue);\n2876:\t\tblk_mq_unfreeze_queue(disk-\u003equeue, memflags);\n2877:\t\n2878:\t\t/* Initiate the storage element removal and wait for its completion. */\n2879:\t\tret = disk-\u003efops-\u003ese_ops-\u003eremove_element(disk, element_id);\n2880:\t\n2881:\t\t/*\n2882:\t\t * Always revalidate zones, even in the case of an error as we do not\n2883:\t\t * know what condition the zones of the block device are in.\n2884:\t\t */\n2885:\t\terr = disk_revalidate_zones(disk, false);\n2886:\t\tif (err \u0026\u0026 !ret)\n2887:\t\t\tret = err;\n2888:\t\n2889:\t\tmutex_unlock(\u0026disk-\u003ezone_revalidate_mutex);\n2890:\t\n2891:\t\treturn ret;\n2892:\t}\n2893:\tEXPORT_SYMBOL_GPL(bdev_remove_storage_element);\n2894:\t\n2895:\tstatic int blkdev_ioctl_remove_storage_element(struct block_device *bdev,\n2896:\t\t\t\t\t\t blk_mode_t mode,\n2897:\t\t\t\t\t\t unsigned int __user *argp)\n2898:\t{\n2899:\t\tunsigned int element_id;\n2900:\t\tint ret;\n2901:\t\n2902:\t\t/*\n2903:\t\t * Note: we require an O_EXCL open here so that a user cannot trigger\n2904:\t\t * a storage element removal from user space under a mounted file system.\n2905:\t\t * An FS that wants to manage storage element can do so using the\n2906:\t\t * function bdev_remove_storage_element() from kernel context.\n2907:\t\t */\n2908:\t\tif (!(mode \u0026 BLK_OPEN_EXCL))\n2909:\t\t\treturn -EBADF;\n2910:\t\tif (!(mode \u0026 BLK_OPEN_WRITE))\n2911:\t\t\treturn -EBADF;\n2912:\t\tif (bdev_read_only(bdev))\n2913:\t\t\treturn -EPERM;\n2914:\t\n2915:\t\tif (get_user(element_id, argp))\n2916:\t\t\treturn -EFAULT;\n2917:\t\n2918:\t\t/*\n2919:\t\t * Sync the device dirty data, flush the volatile write cache, and\n2920:\t\t * invalidate all cached data so that reads do not return old data for\n2921:\t\t * zones that went offline.\n2922:\t\t * Since a user can only successfully write to sequential zones of a\n2923:\t\t * zoned devices using direct IOs, the sync should only handle dirty\n2924:\t\t * data from buffered writes to conventional zones, for devices that\n2925:\t\t * have such zones.\n2926:\t\t */\n2927:\t\tret = sync_blockdev(bdev);\n2928:\t\tif (ret)\n2929:\t\t\treturn ret;\n2930:\t\n2931:\t\tinode_lock(bdev-\u003ebd_mapping-\u003ehost);\n2932:\t\tfilemap_invalidate_lock(bdev-\u003ebd_mapping);\n2933:\t\n2934:\t\tret = blkdev_issue_flush(bdev-\u003ebd_disk-\u003epart0);\n2935:\t\tif (ret)\n2936:\t\t\tgoto unlock;\n"
} |
| 17/2 | 2026/11/12 05:09 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 50787 output: 36 thoughts: 19 Thoughts: Here's my attempt at summarizing the thought process, tailored for an expert audience: **Diving Deeper into block/blk-zoned.c** Okay, so we've been looking at the core zoned block device functionality. Time to expand our scope and methodically review the rest of the functions within `block/blk-zoned.c`. I need to ensure we've got a comprehensive understanding of *how* these zoned devices are interacting with the block layer, and pinpoint any potential weak points or areas for optimization. I'm thinking, in particular, about how these functions handle zoned-specific commands, manage zone resources, and interact with the data placement policies. Specifically, I'm going to be looking for: * **Zone management:** How are zones allocated, tracked, and managed during device lifetime? Is there efficient zone management within this file? * **Command Handling:** How does this code handle zoned specific commands? Does it account for command limitations? Is the command handling efficient? * **Error Handling:** Are there robust error checks throughout? How are failures handled with respect to the zone's state? What's the strategy for dealing with write-related errors in particular? * **Locking:** Are the necessary locks in place to protect shared data structures and ensure data integrity? I'll be scrutinizing the lock granularity and potential contention points. * **Performance:** While we're looking at code, it's also worth thinking about performance. I'll mentally evaluate the complexity of each function, and look for any operations that might become bottlenecks under heavy I/O load. For instance, any synchronous operations that could block threads. * **Interaction with Upper Layers:** This code needs to mesh with the block layer and any higher-level storage management components. I'll need to keep an eye on how the API is exposed and whether the abstractions are clean. I'm aiming for a holistic understanding of how these functions fit into the overall picture. Let's see what we find. |
| 18/2 | 2026/11/12 05:09 | tool | read-file |
0mArgs:{
"File": "block/blk-zoned.c",
"FirstLine": 2937,
"LineCount": 80
}Results: {
"Contents": "2937:\t\n2938:\t\tret = truncate_bdev_range(bdev, mode, 0, bdev_nr_bytes(bdev) - 1);\n2939:\t\tif (ret)\n2940:\t\t\tgoto unlock;\n2941:\t\n2942:\t\tret = bdev_remove_storage_element(bdev, element_id);\n2943:\t\n2944:\tunlock:\n2945:\t\tfilemap_invalidate_unlock(bdev-\u003ebd_mapping);\n2946:\t\tinode_unlock(bdev-\u003ebd_mapping-\u003ehost);\n2947:\t\n2948:\t\treturn ret;\n2949:\t}\n2950:\t\n2951:\t/**\n2952:\t * bdev_restore_storage_elements - Restore all depopulated storage elements of\n2953:\t *\t\t\t\t a zoned block device\n2954:\t * @bdev:\tblock device to operate on\n2955:\t *\n2956:\t * Restore all storage elements of @bdev that have been depopulated. Not all\n2957:\t * elements may be restored by this operation. This operation is destructive and\n2958:\t * will result in all sequential zones to be transitioned to the empty condition.\n2959:\t * The caller is responsible for taking care of any necessary invalidation of\n2960:\t * cached data. This function will block until the storage elements are restored\n2961:\t * or an error occurs.\n2962:\t *\n2963:\t * Returns 0 on success and a negative error code on failure.\n2964:\t */\n2965:\tint bdev_restore_storage_elements(struct block_device *bdev)\n2966:\t{\n2967:\t\tstruct gendisk *disk = bdev-\u003ebd_disk;\n2968:\t\tunsigned int memflags;\n2969:\t\tint ret, err;\n2970:\t\n2971:\t\tif (test_bit(GD_DEAD, \u0026disk-\u003estate))\n2972:\t\t\treturn -ENODEV;\n2973:\t\n2974:\t\tif (!blk_queue_is_zoned(disk-\u003equeue) || !disk-\u003enr_storage_elements)\n2975:\t\t\treturn -EOPNOTSUPP;\n2976:\t\n2977:\t\t/*\n2978:\t\t * Serialize against concurrent revalidation and concurrent storage\n2979:\t\t * element management operations so that we can see consistent zone\n2980:\t\t * condition changes.\n2981:\t\t */\n2982:\t\tmutex_lock(\u0026disk-\u003ezone_revalidate_mutex);\n2983:\t\n2984:\t\t/*\n2985:\t\t * Freeze and unfreeze the queue to flush any outstanding commands.\n2986:\t\t * The caller is responsible for not queuing up more I/O by higher\n2987:\t\t * level means.\n2988:\t\t */\n2989:\t\tmemflags = blk_mq_freeze_queue(disk-\u003equeue);\n2990:\t\tblk_mq_unfreeze_queue(disk-\u003equeue, memflags);\n2991:\t\n2992:\t\t/*\n2993:\t\t * Initiate the storage elements restoration and wait for its\n2994:\t\t * completion.\n2995:\t\t */\n2996:\t\tret = disk-\u003efops-\u003ese_ops-\u003erestore_elements(disk);\n2997:\t\n2998:\t\t/*\n2999:\t\t * Always revalidate zones, even in the case of an error as we do not\n3000:\t\t * know what condition the zones of the block device are in. Note that\n3001:\t\t * since all sequential zones should have been transitioned to the empty\n3002:\t\t * condition, we can drop all zone write plugs. If an error occured and\n3003:\t\t * not all sequential zones are empty, the revalidation will reinstate\n3004:\t\t * the write plugs for these zones.\n3005:\t\t */\n3006:\t\terr = disk_revalidate_zones(disk, true);\n3007:\t\tif (err \u0026\u0026 !ret)\n3008:\t\t\tret = err;\n3009:\t\n3010:\t\tmutex_unlock(\u0026disk-\u003ezone_revalidate_mutex);\n3011:\t\n3012:\t\treturn ret;\n3013:\t}\n3014:\tEXPORT_SYMBOL_GPL(bdev_restore_storage_elements);\n3015:\t\n3016:\tstatic int blkdev_ioctl_restore_storage_elements(struct block_device *bdev,\n"
} |
| 19/2 | 2026/11/12 05:09 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 52457 output: 26 thoughts: 1108 Thoughts: **Worth Fuzzing Assessment: Zoned Block Device Enhancements** Okay, let's break this down. We're looking at code modifications across the block layer and some drivers, specifically focusing on zoned block device functionality. The files include `block/blk-zoned.c`, `block/ioctl.c`, `block/genhd.c`, `zloop.c`, and `sd_zbc.c`. My initial thought is, is this even *reachable* and therefore worth fuzzing? The patch fuzzing guidelines are clear: reachability is the primary criterion. If the code is unreachable in a standard virtualized environment, it's a non-starter. Based on the instructions, the question becomes are these core subsystems, refactorings, assertions, or drivers reachable via virtual buses? Looking at the files, I see that we are working with core block-layer elements such as storage element management functions, as well as the addition of ioctls related to storage element reporting, removal, and restoration. `zloop.c` presents itself as the key driver for us since it represents a software-emulated zoned block device, which could be spun up in QEMU / syzkaller. The `sd_zbc.c` is also important as it adds support for SCSI Zoned Block Commands storage elements. * `block/blk-zoned.c` includes functions related to zone validation, storage element reports, etc. * `block/genhd.c` calls `disk_init_storage_element_mgmt` via `__add_disk()`. * `block/ioctl.c` adds ioctls like `BLKREPORTSTORELEMS`, `BLKREMOVESTORELEM`, and `BLKRESTORESTORELEMS`. * `zloop.c` implements the virtual software zoned loop device accessible through `/dev/zloop-control`. * `sd_zbc.c` implements SCSI Zoned Block Commands storage elements. Considering this, the `zloop` driver is the critical piece because it's a software-based zoned block device. This means anyone in a QEMU or syzkaller environment can create it via `/dev/zloop-control`, and then use it. We're talking about direct user interaction via device nodes. They can write "add ... stor\_elements=1 ...", create the device, and execute the ioctls, submit I/O requests, trigger zone revalidation, and potentially induce error scenarios, races, etc. The `sd_zbc.c` is also a possibility, assuming a ZBC-capable SCSI device is available or can be emulated. `blkdev_common_ioctl()` is worth noting, as it handles the new ioctls for *any* block device, returning `-ENOTTY` if the device doesn't support them. However, with `zloop` or the proper SCSI ZBC device present, the full ioctl flow executes. So, yes, this is absolutely reachable. `WorthFuzzing = true`. Now, the next step is to figure out the required configurations... |
| 20/2 | 2026/11/12 05:10 | tool | grepper |
0mArgs:{
"Expression": "blk-zoned",
"PathPrefixes": [
"block/"
]
}Results: {
"Output": "block/Makefile=31=obj-$(CONFIG_BLK_DEV_INTEGRITY) += bio-integrity.o blk-integrity.o t10-pi.o \\\nblock/Makefile-32-\t\t\t\t bio-integrity-auto.o bio-integrity-fs.o\nblock/Makefile:33:obj-$(CONFIG_BLK_DEV_ZONED)\t+= blk-zoned.o\nblock/Makefile-34-obj-$(CONFIG_BLK_WBT)\t\t+= blk-wbt.o\n"
} |
| 21/2 | 2026/11/12 05:10 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 54386 output: 32 thoughts: 44 Thoughts: Here's my thought process, as requested: **Deconstructing the Build Configuration** Okay, so I see this object file, `block/blk-zoned.o`, and the build system is using `CONFIG_BLK_DEV_ZONED`. My initial assumption is that this object file is probably part of the Zoned Block Device (ZBD) support. Given that, let's confirm my understanding by diving into the `block/Kconfig` file. That's the typical place to find the configuration options related to a specific feature or subsystem within the block layer. I need to verify that `BLK_DEV_ZONED` is, in fact, the configuration option that enables ZBD functionality. Checking `Kconfig` will tell me if my mental model is correct, or if there's some other, possibly more subtle, interrelation I need to consider. Let's see what's in there... |
| 22/2 | 2026/11/12 05:10 | tool | grepper |
0mArgs:{
"Expression": "config BLK_DEV_ZONED",
"PathPrefixes": [
"block/"
]
}Results: {
"Output": "block/Kconfig=77=config BLK_DEV_WRITE_MOUNTED\n--\nblock/Kconfig-96-\nblock/Kconfig:97:config BLK_DEV_ZONED\nblock/Kconfig-98-\tbool \"Zoned block device support\"\n"
} |
| 23/2 | 2026/11/12 05:10 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 54735 output: 31 thoughts: 16 Thoughts: Okay, let's dive into this. Here's my take on those instructions, as if they're my internal thought process: **Diving into the Kernel Configuration Landscape** Right, so we're talking about kernel configuration, and specifically the `block/Kconfig` file. I'm assuming we're dealing with block device configuration, likely within the Linux kernel itself, given the context. My immediate focus is around line 97. That line likely holds a key piece of information, a pivotal configuration option, or perhaps the start of a new section or definition related to block device configuration. I need to understand what's *there* specifically. Is it a default setting, a conditional definition, a help text description, or something else entirely? Whatever it is, that's where I need to orient myself to understand the current configuration's scope. Line 97, here we go! |
| 24/2 | 2026/11/12 05:10 | tool | read-file |
0mArgs:{
"File": "block/Kconfig",
"FirstLine": 95,
"LineCount": 25
}Results: {
"Contents": " 95:\t\twith the bdev_allow_write_mounted boot option.\n 96:\t\n 97:\tconfig BLK_DEV_ZONED\n 98:\t\tbool \"Zoned block device support\"\n 99:\t\thelp\n 100:\t\tBlock layer zoned block device support. This option enables\n 101:\t\tsupport for ZAC/ZBC/ZNS host-managed and host-aware zoned block\n 102:\t\tdevices.\n 103:\t\n 104:\t\tSay yes here if you have a ZAC, ZBC, or ZNS storage device.\n 105:\t\n 106:\tconfig BLK_DEV_THROTTLING\n 107:\t\tbool \"Block layer bio throttling support\"\n 108:\t\tdepends on BLK_CGROUP\n 109:\t\tselect BLK_CGROUP_RWSTAT\n 110:\t\thelp\n 111:\t\tBlock layer bio throttling support. It can be used to limit\n 112:\t\tthe IO rate to a device. IO rate policies are per cgroup and\n 113:\t\tone needs to mount and use blkio cgroup controller for creating\n 114:\t\tcgroups and specifying per device IO rate policies.\n 115:\t\n 116:\t\tSee Documentation/admin-guide/cgroup-v1/blkio-controller.rst for more information.\n 117:\t\n 118:\tconfig BLK_WBT\n 119:\t\tbool \"Enable support for block device writeback throttling\"\n"
} |
| 25/2 | 2026/11/12 05:10 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 55364 output: 32 thoughts: 181 Thoughts: **My Kernel Configuration Musings** Okay, so I'm diving into these block device configurations. First up, `BLK_DEV_ZONED`—no dependencies, that's straightforward. But, hold on a sec, what about `BLK_DEV_ZONED_LOOP`? Let's check the `drivers/block/Kconfig` file. Ah, there it is: it *depends* on `BLK_DEV_ZONED`. Makes sense. Now, let's circle back and consider SCSI ZBC. I remember seeing `sd_zbc.o` included in the SCSI build based on the `BLK_DEV_ZONED` config. Confirmed in `drivers/scsi/Makefile`: `sd_mod-$(CONFIG_BLK_DEV_ZONED) += sd_zbc.o`. And, I also see conditional compilation `#ifdef CONFIG_BLK_DEV_ZONED` within `drivers/scsi/sd.c`. That's important. Speaking of `sd.c`, which builds SCSI disk, I should check the `drivers/scsi/Kconfig` to see which other configs build that, perhaps the `BLK_DEV_SD` config? That's the next logical step to understand the overall picture. |
| 26/2 | 2026/11/12 05:11 | tool | grepper |
0mArgs:{
"Expression": "config BLK_DEV_SD",
"PathPrefixes": [
"drivers/scsi"
]
}Results: {
"Output": "drivers/scsi/Kconfig=79=comment \"SCSI support type (disk, tape, CD-ROM)\"\n--\ndrivers/scsi/Kconfig-81-\ndrivers/scsi/Kconfig:82:config BLK_DEV_SD\ndrivers/scsi/Kconfig-83-\ttristate \"SCSI disk support\"\n"
} |
| 27/2 | 2026/11/12 05:11 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 55945 output: 34 thoughts: 22 Thoughts: Here's my thought process, as if I were considering this myself: **Investigating `BLK_DEV_SD` Dependencies** Okay, time to dig into this. I need to understand the dependencies of `BLK_DEV_SD`, the block device for SD cards, and that means I'm starting in `drivers/scsi/Kconfig`. This is where the configuration options for SCSI-related drivers are usually defined, and it's a good bet that block devices like SD cards, even if they aren't strictly SCSI, might have their configuration tied into the SCSI subsystem in some way. I'm assuming that the kernel developers chose this location because the storage system is similar. I want to look for what needs to be enabled for `BLK_DEV_SD` to become available, because I have a hunch it isn't completely independent. I'll need to identify any other Kconfig symbols that `BLK_DEV_SD` depends on, and the nature of those dependencies – are they "depends on", "selects", or something else? Understanding this will be critical in making the correct choices in my own configuration. I'll also be searching for any hints on when `BLK_DEV_SD` will come into existence. |
| 28/2 | 2026/11/12 05:11 | tool | read-file |
0mArgs:{
"File": "drivers/scsi/Kconfig",
"FirstLine": 80,
"LineCount": 25
}Results: {
"Contents": " 80:\t\tdepends on SCSI\n 81:\t\n 82:\tconfig BLK_DEV_SD\n 83:\t\ttristate \"SCSI disk support\"\n 84:\t\tdepends on SCSI\n 85:\t\thelp\n 86:\t\t If you want to use SCSI hard disks, Fibre Channel disks,\n 87:\t\t Serial ATA (SATA) or Parallel ATA (PATA) hard disks,\n 88:\t\t USB storage or the SCSI or parallel port version of\n 89:\t\t the IOMEGA ZIP drive, say Y and read the SCSI-HOWTO,\n 90:\t\t the Disk-HOWTO and the Multi-Disk-HOWTO, available from\n 91:\t\t \u003chttp://www.tldp.org/docs.html#howto\u003e. This is NOT for SCSI\n 92:\t\t CD-ROMs.\n 93:\t\n 94:\t\t To compile this driver as a module, choose M here and read\n 95:\t\t \u003cfile:Documentation/scsi/scsi.rst\u003e.\n 96:\t\t The module will be called sd_mod.\n 97:\t\n 98:\t\t Do not compile this driver as a module if your root file system\n 99:\t\t (the one containing the directory /) is located on a SCSI disk.\n 100:\t\t In this case, do not compile the driver for your SCSI host adapter\n 101:\t\t (below) as a module either.\n 102:\t\n 103:\tconfig CHR_DEV_ST\n 104:\t\ttristate \"SCSI tape support\"\n"
} |
| 29/2 | 2026/11/12 05:11 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 56666 output: 34 thoughts: 2325 Thoughts: **Focusing on Storage Element Management and Zoned Block Device Logic** Alright, let's get down to brass tacks. I'm looking at this patch and my goal is to distill the key functions and entry points relevant for fuzzing, specifically focusing on storage element management within zoned block devices. We're talking about things like zoned loop (`zloop`) device driver and how it emulates storage elements. I need to zero in on the added or modified features and associated functions. First, I see configurations: `BLK_DEV_ZONED`, `BLK_DEV_ZONED_LOOP`, and `BLK_DEV_SD` are all crucial here. Now, keeping in mind the need to avoid ubiquitous functions (allocators, traps, core synch primitives, etc.) and target specific feature logic, I see these core functions that I should hone in on: In `block/blk-zoned.c`, it's all about element mgmt: * `disk_zone_is_offline` * `bdev_report_storage_elements` * `bdev_remove_storage_element` * `bdev_restore_storage_elements` * `disk_init_storage_element_mgmt` * `blk_ioctl_zone_storage_elements` * `disk_revalidate_zones` (and the underlying `blk_revalidate_disk_zones`) `block/ioctl.c`'s `blkdev_common_ioctl` appears important due to calling `blk_ioctl_zone_storage_elements`, which I already tagged as relevant, and it handles `BLKREPORTSTORELEMS`, `BLKREMOVESTORELEM`, `BLKRESTORESTORELEMS`. `drivers/block/zloop.c` has these: * `zloop_set_zone_cond` * `zloop_report_elements` * `zloop_remove_element` * `zloop_restore_elements` * `zloop_ctl_degrade_element` * `zloop_prep_rq` * `zloop_degrade_element` * `zloop_create_storage_elements` * `zloop_ctl_write` (modified) And finally, `drivers/scsi/sd.c` and `drivers/scsi/sd_zbc.c`: * `sd_setup_read_write_cmnd` (in `sd.c`, calls `disk_zone_is_offline`) * `sd_zbc_report_storage_elements` * `sd_zbc_remove_storage_element` * `sd_zbc_restore_storage_elements` * `sd_zbc_revalidate_zones` I've already marked the ones that are likely entrypoints, but just to be sure, I should verify these aren't just generic utilities or inlined functions. No, these are all pretty specific to storage element mgmt, zone operations, and the `zloop` driver. I need to dig into `zloop` a bit more, as it's the core of the zoned device emulation and where most of the new logic is. I need to verify that I have enough key functions to focus the fuzzer. These would include ioctl and all related se_ops callbacks. I see some key things in `block/blk-zoned.c`: IOCTL calls, `copy_from_user` calls (good for fuzzing), and calls to `bdev_remove_storage_element` and `bdev_restore_storage_elements`, which have specific requirements (`BLK_OPEN_EXCL`, `BLK_OPEN_WRITE`) and trigger fs sync, invalidate, flush, and truncate operations. The `zloop` driver uses new options like `stor_elements` and `element_id`. IO prep checks are present, as well as modifications to zone conditions and element statuses. I'm particularly interested in the locking patterns, because concurrency issues are always an interesting target. I notice the mutex locking/unlocking around `disk->zone_revalidate_mutex` and queue freezing/unfreezing, then calls to the underlying storage element operations. I see similar locking in `zloop_remove_element` and `zloop_restore_elements`. There are delays with `msleep`, and all of this happens during restoration. Okay, this is ripe with potential for state machine fuzzing: state transitions, concurrent queue operations, zone revalidation, caching invalidation, file operations and ioctl dispatching. That's a lot of angles to cover! I am ready to start testing it. |
| 30/2 | 2026/11/12 05:11 | tool | grepper |
0mArgs:{
"Expression": "#ifdef|CONFIG_",
"PathPrefixes": [
"drivers/block/zloop.c"
]
}Results: {
"Output": "no matches"
} |
| 31/2 | 2026/11/12 05:11 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 60058 output: 389 thoughts: 928 Thoughts: **Zoned Block Device Feature Implementation Analysis** Okay, let's break this down. First, I wanted to confirm the configuration options related to zoned block device support. I revisited `drivers/block/Kconfig` and confirmed that `BLK_DEV_ZONED_LOOP` is indeed a tristate option dependent on `BLK_DEV_ZONED`, which in turn is a boolean. I also confirmed that the `sd_mod` driver, built by `CONFIG_BLK_DEV_SD` and depending on `CONFIG_SCSI`, incorporates `sd_zbc.o` when `CONFIG_BLK_DEV_ZONED` is enabled. No other configuration dependencies beyond the obvious SCSI dependency of the SD driver were of concern. Next, I compiled a list of key symbols—the 'FocusSymbols'—scattered across the code base that are directly involved in implementing the zoned block device functionality. I found the following functions in `block/blk-zoned.c`, `drivers/block/zloop.c`, and `drivers/scsi/sd_zbc.c`: - `blk_ioctl_zone_storage_elements` - `bdev_report_storage_elements` - `bdev_remove_storage_element` - `bdev_restore_storage_elements` - `disk_zone_is_offline` - `disk_revalidate_zones` - `disk_init_storage_element_mgmt` - `zloop_report_elements` - `zloop_remove_element` - `zloop_restore_elements` - `zloop_degrade_element` - `zloop_ctl_degrade_element` - `zloop_prep_rq` - `sd_zbc_report_storage_elements` - `sd_zbc_remove_storage_element` - `sd_zbc_restore_storage_elements` - `sd_zbc_revalidate_zones` This list is pretty comprehensive. These are the core building blocks across the block core, the zloop driver, and the SCSI ZBC layer. Finally, I ran a quick 'WorthFuzzing' sanity check. The patch modifies core subsystem code (`block/blk-zoned.c`, `block/ioctl.c`, `block/genhd.c`), yes. It includes changes to a virtual driver (`drivers/block/zloop.c`), yes. It introduces new user-facing ioctls (specifically, `BLKREPORTSTORELEMS`, `BLKREMOVESTORELEM`, and `BLKRESTORESTORELEMS`), yes. And there's a strong likelihood of complex locking, queue management, and memory manipulation, cache invalidation within this patch, yes. Given all this, "WorthFuzzing" is definitely set to true for this set of changes. Okay, that's enough analysis, time to invoke `set-results`. |
| 32/2 | 2026/11/12 05:12 | tool | set-results |
0mArgs:{
"EnableConfigs": [
"BLK_DEV_ZONED",
"BLK_DEV_ZONED_LOOP",
"SCSI",
"BLK_DEV_SD"
],
"FocusSymbols": [
"blk_ioctl_zone_storage_elements",
"bdev_report_storage_elements",
"bdev_remove_storage_element",
"bdev_restore_storage_elements",
"disk_zone_is_offline",
"disk_revalidate_zones",
"disk_init_storage_element_mgmt",
"zloop_report_elements",
"zloop_remove_element",
"zloop_restore_elements",
"zloop_degrade_element",
"zloop_ctl_degrade_element",
"zloop_prep_rq",
"sd_zbc_report_storage_elements",
"sd_zbc_remove_storage_element",
"sd_zbc_restore_storage_elements",
"sd_zbc_revalidate_zones"
],
"Reasoning": "The patch series adds storage element management for zoned block devices. It introduces new user-space block ioctls (BLKREPORTSTORELEMS, BLKREMOVESTORELEM, BLKRESTORESTORELEMS) in core block layer (block/blk-zoned.c and block/ioctl.c) and implements storage element emulation in the virtual zoned loopback driver (drivers/block/zloop.c) via /dev/zloop-control as well as SCSI ZBC devices (drivers/scsi/sd_zbc.c). This adds significant state management, queue freezing, revalidation, and caching invalidation logic that is fully reachable and warrants fuzzing.",
"WorthFuzzing": true
}Results: {
"EnableConfigs": [
"BLK_DEV_ZONED",
"BLK_DEV_ZONED_LOOP",
"SCSI",
"BLK_DEV_SD"
],
"FocusSymbols": [
"blk_ioctl_zone_storage_elements",
"bdev_report_storage_elements",
"bdev_remove_storage_element",
"bdev_restore_storage_elements",
"disk_zone_is_offline",
"disk_revalidate_zones",
"disk_init_storage_element_mgmt",
"zloop_report_elements",
"zloop_remove_element",
"zloop_restore_elements",
"zloop_degrade_element",
"zloop_ctl_degrade_element",
"zloop_prep_rq",
"sd_zbc_report_storage_elements",
"sd_zbc_remove_storage_element",
"sd_zbc_restore_storage_elements",
"sd_zbc_revalidate_zones"
],
"Reasoning": "The patch series adds storage element management for zoned block devices. It introduces new user-space block ioctls (BLKREPORTSTORELEMS, BLKREMOVESTORELEM, BLKRESTORESTORELEMS) in core block layer (block/blk-zoned.c and block/ioctl.c) and implements storage element emulation in the virtual zoned loopback driver (drivers/block/zloop.c) via /dev/zloop-control as well as SCSI ZBC devices (drivers/scsi/sd_zbc.c). This adds significant state management, queue freezing, revalidation, and caching invalidation logic that is fully reachable and warrants fuzzing.",
"WorthFuzzing": true
} |
| 33/1 | 2026/11/12 05:12 | action | If |
6mArgs:{
"WorthFuzzing": true
}Results: null |
| 34/2 | 2026/11/12 05:12 | agent | kmsan-evaluator |
6mModel:core Args: null Results: {
"KMSANReasoning": "The patch series introduces zoned block device storage element management (reporting, depopulation/removal, restoration, and degradation) across the block layer (blk-zoned), SCSI ZBC (sd_zbc), and the zoned loop driver (zloop).\n\nAnalysis of memory handling across the changes:\n1. UAPI structures and kernel-to-user copies: The ioctl BLKREPORTSTORELEMS uses struct blk_se_report and struct blk_se to return storage element descriptors to user space. Both structures are naturally aligned with no compiler padding holes (struct blk_se has explicit reserved[13] aligning it to 32 bytes; struct blk_se_report has a 64-bit reserved field before the flex array). Furthermore, the kernel allocation uses kzalloc_flex(), and individual elements are either memset to zero (sd_zbc_parse_storage_element) or copied from kzalloc_objs-allocated arrays (zloop_create_storage_elements).\n2. Heap and stack allocations: All new heap allocations use kzalloc, kzalloc_objs, or kzalloc_flex. Stack structures and command buffers (e.g. SCSI CDBs, zloop options) are zero-initialized with `{ }` or memset before use.\n3. Control flow and bounds: The changes do not introduce branching on uninitialized data or alter data lengths in a way that exposes uninitialized memory.\n\nAny potential bugs in these changes would involve bounds checking, device locking/serialization (covered by LOCKDEP), or memory corruption/out-of-bounds accesses (covered by KASAN). There is no risk of uninitialized memory reads or information leaks to userspace that would necessitate a dedicated KMSAN fuzzing session.",
"NeedsKMSAN": false
}Instruction: You are an expert Linux kernel security engineer specializing in kernel memory
error detectors (KASAN and KMSAN). Your job is to review the provided patch series and
determine if the code changes justify spawning a dedicated KMSAN (KernelMemorySanitizer)
fuzzing session in addition to standard KASAN fuzzing.
CRITICAL DISTINCTION BETWEEN KASAN AND KMSAN:
- Standard KASAN kernel builds (upstream-apparmor-kasan.config) already enable
a comprehensive suite of debugging tools and sanitizers, including KASAN
(out-of-bounds accesses, use-after-free, double free, invalid free), LOCKDEP
(locking bugs and deadlocks), UB-sanitizers, and memory corruption checks.
- KMSAN (KernelMemorySanitizer) detects reads of UNINITIALIZED memory (stack, heap,
or page allocations) and kernel-to-user memory info-leaks.
Rule: THERE IS NO SENSE IN RUNNING A KMSAN SESSION IF A BUG CAN BE CAUGHT BY KASAN,
LOCKDEP, OR OTHER STANDARD BUG DETECTORS.
A dedicated KMSAN fuzzing session incurs significant resource costs. You must ONLY
set NeedsKMSAN=true if the code changes introduce or expose UNINITIALIZED MEMORY risks
that are detected ONLY by KMSAN.
Look holistically at the patch series and surrounding code. Even if no direct
uninitialized field accesses or new buffer allocations are added in the diff itself,
a patch may alter control flow, bounds checking, or data length calculations in ways
that change how the rest of the code operates on existing buffers (e.g. allowing
uninitialized stack/heap memory to be read, copied to user space, or used in control
flow). Do not hesitate to use your code access tools to inspect the surrounding code,
called functions, and callers.
Set NeedsKMSAN=true ONLY IF the patch introduces or modifies:
1. Kernel structures sent to user space (via copy_to_user, put_user, netlink skb
attributes, ioctl output arguments, socket options, or BPF buffers) where fields
or structure padding might not be fully initialized/zeroed.
2. Conditional logic or branching that depends on potentially uninitialized variables
or struct fields.
3. Allocation or initialization of complex data structures where uninitialized fields
could be read later in reachable code paths.
4. Bounds checks, lengths, or logic in a way that allows surrounding code to access
uninitialized bytes of existing buffers.
Set NeedsKMSAN=false IF:
- The code changes primarily risk out-of-bounds access, array overflows, NULL pointer
dereferences, locking deadlocks, or use-after-free bugs (these are already caught
by KASAN, LOCKDEP, or standard bug detectors).
- All stack/heap structures touched or introduced by the patch are fully zeroed
or initialized (e.g. using = {0}, memset, kzalloc) before being read or copied.
- The patch does not introduce any risk of uninitialized memory usage or info-leaks.
Use your code access tools to inspect the surrounding code if necessary, then provide
detailed KMSANReasoning contrasting KASAN vs KMSAN applicability for this patch.
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt: Target architecture: amd64
For your convenience, here is the diff of the changes:
commit ccd0317eb70f3d874172417cfe9ce93eeaabf238
Author: syz-cluster <triage@syzkaller.com>
Date: Thu Nov 12 05:08:32 2026 +0000
syz-cluster: applied patch under review
diff --git a/Documentation/admin-guide/blockdev/zoned_loop.rst b/Documentation/admin-guide/blockdev/zoned_loop.rst
index 64277494fb36af..d378fbdc1983ef 100644
--- a/Documentation/admin-guide/blockdev/zoned_loop.rst
+++ b/Documentation/admin-guide/blockdev/zoned_loop.rst
@@ -61,8 +61,9 @@ The options available for the add command can be listed by reading the
/dev/zloop-control device::
$ cat /dev/zloop-control
- add id=%d,capacity_mb=%u,zone_size_mb=%u,zone_capacity_mb=%u,conv_zones=%u,max_open_zones=%u,base_dir=%s,nr_queues=%u,queue_depth=%u,buffered_io,zone_append=%u,ordered_zone_append,discard_write_cache
+ add id=%d,capacity_mb=%u,zone_size_mb=%u,zone_capacity_mb=%u,conv_zones=%u,max_open_zones=%u,base_dir=%s,nr_queues=%u,queue_depth=%u,buffered_io,zone_append=%u,ordered_zone_append,discard_write_cache,stor_elements=%u
remove id=%d
+ degrade_element id=%d,element_id=%u
In more details, the options that can be used with the "add" command are as
follows.
@@ -113,6 +114,15 @@ discard_write_cache Discard all data that was not explicitly persisted using a
each zone file to the size recorded during the last flush
operation. This simulates power fail events where
uncommitted data is lost.
+stor_elements Control storage element emulation. The default value is
+ 0, indicating no emulation. A value of 1 indicates that
+ storage elements are emulated as full access physical
+ elements, which is equivalent to combined read+write heads
+ of a hard-disk. A value of 2 enables storage element
+ emulation as fractional access physical storage elements,
+ which is equivalent to pairs of read and write heads of a
+ hard-disk. The health state of storage elements is not
+ preserved across a zloop device remove and add.
=================== =========================================================
3) Deleting a Zoned Device
@@ -135,7 +145,26 @@ the zoned device will not be created.
To fully delete a zoned device, after executing the remove operation, the device
base directory containing the backing files of the device zones must be deleted.
-4) Example
+4) Degrading a storage element
+------------------------------
+
+When storage element emulation is enabled, storage elements can be set in a
+degraded state to emulate defective storage elements (e.g. defective heads of a
+hard disk). This is done by issuing the "degrade_element" command to
+/dev/zloop-control, specifying the ID of the target device and the ID of the
+storage element to degrade::
+
+ $ echo "degrade_element id=X,element_id=Y" > /dev/zloop-control
+
+Depending on the type of the storage element specified, a read or write access
+to sectors of a zone served by a degraded storage element will result in a
+failure. For partial access storage elements, write accesses are always failed
+if the write element serving the zone is degraded. Likewise, read accesses are
+failed if the read element serving the zone is degraded. When storage elements
+are emulated as all access physical elements, both read and write operations are
+failed when the storage element serving the zones being accessed is degraded.
+
+5) Example
----------
The following sequence of commands creates a 2GB zoned device with zones of 64
diff --git a/block/blk-zoned.c b/block/blk-zoned.c
index 4c588929619124..d56afbeb436a15 100644
--- a/block/blk-zoned.c
+++ b/block/blk-zoned.c
@@ -18,6 +18,7 @@
#include <linux/mempool.h>
#include <linux/kthread.h>
#include <linux/freezer.h>
+#include <linux/uaccess.h>
#include <trace/events/block.h>
@@ -292,6 +293,24 @@ static bool disk_zone_is_seq(struct gendisk *disk, sector_t sector)
return !blk_zstate_is_conv(zs);
}
+/**
+ * disk_zone_is_offline - check if a sector belongs to an offline zone
+ * @disk: gendisk to check
+ * @sector: sector number
+ *
+ * Check if @sector on @disk is contained in an offline zone.
+ * This function can be used only when the bdev queue was entered (BIOs
+ * submitted but not yet completed) or when holding zone_revalidate_mutex.
+ */
+bool disk_zone_is_offline(struct gendisk *disk, sector_t sector)
+{
+ if (!blk_queue_is_zoned(disk->queue))
+ return false;
+
+ return disk_zone_get_cond(disk, sector) == BLK_ZONE_COND_OFFLINE;
+}
+EXPORT_SYMBOL_GPL(disk_zone_is_offline);
+
/**
* bdev_zone_is_seq - check if a sector belongs to a sequential write zone
* @bdev: block device to check
@@ -2534,20 +2553,7 @@ static int blk_revalidate_zone_cb(struct blk_zone *zone, unsigned int idx,
return ret;
}
-/**
- * blk_revalidate_disk_zones - (re)allocate and initialize zone write plugs
- * @disk: Target disk
- *
- * Helper function for low-level device drivers to check, (re) allocate and
- * initialize resources used for managing zoned disks. This function should
- * normally be called by blk-mq based drivers when a zoned gendisk is probed
- * and when the zone configuration of the gendisk changes (e.g. after a format).
- * Before calling this function, the device driver must already have set the
- * device zone size (chunk_sector limit) and the max zone append limit.
- * BIO based drivers can also use this function as long as the device queue
- * can be safely frozen.
- */
-int blk_revalidate_disk_zones(struct gendisk *disk)
+static int disk_revalidate_zones(struct gendisk *disk, bool drop_all_zwplugs)
{
struct blk_revalidate_zone_args args = { };
struct blk_report_zones_args rep_args = {
@@ -2558,19 +2564,13 @@ int blk_revalidate_disk_zones(struct gendisk *disk)
int retries = 2;
int ret;
- if (WARN_ON_ONCE(!blk_queue_is_zoned(disk->queue)))
- return -EIO;
-
- /*
- * Serialize calls to this function so that we can safely look at and
- * eventually change the disk zone information.
- */
- mutex_lock(&disk->zone_revalidate_mutex);
+ if (drop_all_zwplugs)
+ disk_for_all_zone_wplugs(disk, disk_drop_zone_wplug, NULL);
again:
ret = disk_revalidate_capacity(disk, &args);
if (ret)
- goto unlock;
+ return ret;
/*
* Allocate zone resources if they are needed and we have not done
@@ -2583,14 +2583,14 @@ int blk_revalidate_disk_zones(struct gendisk *disk)
ret = disk_alloc_zone_resources(disk, args.capacity);
if (ret) {
memalloc_noio_restore(noio_flag);
- goto unlock;
+ return ret;
}
}
ret = disk_init_revalidate_args(disk, &args);
if (ret) {
memalloc_noio_restore(noio_flag);
- goto unlock;
+ return ret;
}
ret = disk->fops->report_zones(disk, 0, UINT_MAX, &rep_args);
@@ -2607,8 +2607,6 @@ int blk_revalidate_disk_zones(struct gendisk *disk)
if (ret)
goto free_args;
- mutex_unlock(&disk->zone_revalidate_mutex);
-
return 0;
free_args:
@@ -2624,7 +2622,36 @@ int blk_revalidate_disk_zones(struct gendisk *disk)
}
pr_warn("%s: failed to revalidate zones\n", disk->disk_name);
-unlock:
+
+ return ret;
+}
+
+/**
+ * blk_revalidate_disk_zones - (re)allocate and initialize zone write plugs
+ * @disk: Target disk
+ *
+ * Helper function for low-level device drivers to check, (re) allocate and
+ * initialize resources used for managing zoned disks. This function should
+ * normally be called by blk-mq based drivers when a zoned gendisk is probed
+ * and when the zone configuration of the gendisk changes (e.g. after a format).
+ * Before calling this function, the device driver must already have set the
+ * device zone size (chunk_sector limit) and the max zone append limit.
+ * BIO based drivers can also use this function as long as the device queue
+ * can be safely frozen.
+ */
+int blk_revalidate_disk_zones(struct gendisk *disk)
+{
+ int ret;
+
+ if (WARN_ON_ONCE(!blk_queue_is_zoned(disk->queue)))
+ return -EIO;
+
+ /*
+ * Serialize calls to this function so that we can safely look at and
+ * eventually change the disk zone information.
+ */
+ mutex_lock(&disk->zone_revalidate_mutex);
+ ret = disk_revalidate_zones(disk, false);
mutex_unlock(&disk->zone_revalidate_mutex);
return ret;
@@ -2710,3 +2737,370 @@ int queue_zone_wplugs_show(void *data, struct seq_file *m)
}
#endif
+
+/**
+ * bdev_report_storage_elements - report the storage elements of a zoned
+ * block device
+ * @bdev: the zoned block device to check
+ * @elements: array of storage element descriptors to fill
+ * @nr_elements: maximum size of @elements
+ *
+ * Fill at most @nr_elements storage element descriptors in the array
+ * @elements. The number of storage elements filled in the array is returned
+ * using @nr_elements. If @elements is NULL, only @nr_elements is returned to
+ * indicate the maximum number of storage elements of @bdev. This function may
+ * sleep.
+ *
+ * Returns 0 on success and a negative error code on failure.
+ */
+int bdev_report_storage_elements(struct block_device *bdev,
+ struct blk_se *elements,
+ unsigned int *nr_elements)
+{
+ struct gendisk *disk = bdev->bd_disk;
+
+ if (test_bit(GD_DEAD, &disk->state))
+ return -ENODEV;
+
+ if (!blk_queue_is_zoned(disk->queue) || !disk->nr_storage_elements)
+ return -EOPNOTSUPP;
+
+ if (!nr_elements)
+ return -EINVAL;
+
+ if (!elements) {
+ *nr_elements = disk->nr_storage_elements;
+ return 0;
+ }
+
+ *nr_elements = min(*nr_elements, disk->nr_storage_elements);
+ if (!*nr_elements)
+ return -EINVAL;
+
+ return disk->fops->se_ops->report_elements(disk, elements,
+ nr_elements);
+}
+EXPORT_SYMBOL_GPL(bdev_report_storage_elements);
+
+static int blkdev_ioctl_report_storage_elements(struct block_device *bdev,
+ void __user *argp)
+{
+ struct gendisk *disk = bdev->bd_disk;
+ struct blk_se_report *rep;
+ int ret = 0;
+
+ /*
+ * Allocate the report header with an array of elements, assuming that
+ * we need the maximum number of storage elements. In practice, we may
+ * need less than that, but this potentially unnecessary preallocation
+ * simplifies the code.
+ */
+ rep = kzalloc_flex(*rep, elements, disk->nr_storage_elements);
+ if (!rep)
+ return -ENOMEM;
+
+ if (copy_from_user(rep, argp, sizeof(struct blk_se_report))) {
+ ret = -EFAULT;
+ goto free_rep;
+ }
+
+ if (rep->reserved) {
+ ret = -EINVAL;
+ goto free_rep;
+ }
+
+ rep->nr_reported_elements =
+ min(rep->nr_elements, disk->nr_storage_elements);
+ rep->nr_elements = disk->nr_storage_elements;
+
+ if (rep->nr_reported_elements) {
+ ret = bdev_report_storage_elements(bdev, rep->elements,
+ &rep->nr_reported_elements);
+ if (ret)
+ goto free_rep;
+ }
+
+ if (copy_to_user(argp, rep,
+ struct_size(rep, elements, rep->nr_reported_elements)))
+ ret = -EFAULT;
+
+free_rep:
+ kfree(rep);
+ return ret;
+}
+
+/**
+ * bdev_remove_storage_element - Remove (depopulate) a storage element of a
+ * zoned block device
+ * @bdev: block device to operate on
+ * @element_id: ID of the storage element to remove
+ *
+ * Remove (depopulate) the storage element identified by @element_id from the
+ * block device @bdev. The caller is responsible for taking care of any
+ * necessary dirty page and device write cache flush and invalidation of cached
+ * data. This function will block until the storage element is removed or an
+ * error occurs.
+ *
+ * Returns 0 on success and a negative error code on failure.
+ */
+int bdev_remove_storage_element(struct block_device *bdev,
+ unsigned int element_id)
+{
+ struct gendisk *disk = bdev->bd_disk;
+ unsigned int memflags;
+ int ret, err;
+
+ if (test_bit(GD_DEAD, &disk->state))
+ return -ENODEV;
+
+ if (!blk_queue_is_zoned(disk->queue) || !disk->nr_storage_elements)
+ return -EOPNOTSUPP;
+
+ /* Zero is not a valid storage element ID. */
+ if (!element_id)
+ return -EINVAL;
+
+ /*
+ * Serialize against concurrent revalidation and concurrent storage
+ * element management operations so that we can see consistent zone
+ * condition changes.
+ */
+ mutex_lock(&disk->zone_revalidate_mutex);
+
+ /*
+ * Freeze and unfreeze the queue to flush any outstanding command.
+ * The caller is responsible for not queuing up more I/Os by higher
+ * level means.
+ */
+ memflags = blk_mq_freeze_queue(disk->queue);
+ blk_mq_unfreeze_queue(disk->queue, memflags);
+
+ /* Initiate the storage element removal and wait for its completion. */
+ ret = disk->fops->se_ops->remove_element(disk, element_id);
+
+ /*
+ * Always revalidate zones, even in the case of an error as we do not
+ * know what condition the zones of the block device are in.
+ */
+ err = disk_revalidate_zones(disk, false);
+ if (err && !ret)
+ ret = err;
+
+ mutex_unlock(&disk->zone_revalidate_mutex);
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(bdev_remove_storage_element);
+
+static int blkdev_ioctl_remove_storage_element(struct block_device *bdev,
+ blk_mode_t mode,
+ unsigned int __user *argp)
+{
+ unsigned int element_id;
+ int ret;
+
+ /*
+ * Note: we require an O_EXCL open here so that a user cannot trigger
+ * a storage element removal from user space under a mounted file system.
+ * An FS that wants to manage storage element can do so using the
+ * function bdev_remove_storage_element() from kernel context.
+ */
+ if (!(mode & BLK_OPEN_EXCL))
+ return -EBADF;
+ if (!(mode & BLK_OPEN_WRITE))
+ return -EBADF;
+ if (bdev_read_only(bdev))
+ return -EPERM;
+
+ if (get_user(element_id, argp))
+ return -EFAULT;
+
+ /*
+ * Sync the device dirty data, flush the volatile write cache, and
+ * invalidate all cached data so that reads do not return old data for
+ * zones that went offline.
+ * Since a user can only successfully write to sequential zones of a
+ * zoned devices using direct IOs, the sync should only handle dirty
+ * data from buffered writes to conventional zones, for devices that
+ * have such zones.
+ */
+ ret = sync_blockdev(bdev);
+ if (ret)
+ return ret;
+
+ inode_lock(bdev->bd_mapping->host);
+ filemap_invalidate_lock(bdev->bd_mapping);
+
+ ret = blkdev_issue_flush(bdev->bd_disk->part0);
+ if (ret)
+ goto unlock;
+
+ ret = truncate_bdev_range(bdev, mode, 0, bdev_nr_bytes(bdev) - 1);
+ if (ret)
+ goto unlock;
+
+ ret = bdev_remove_storage_element(bdev, element_id);
+
+unlock:
+ filemap_invalidate_unlock(bdev->bd_mapping);
+ inode_unlock(bdev->bd_mapping->host);
+
+ return ret;
+}
+
+/**
+ * bdev_restore_storage_elements - Restore all depopulated storage elements of
+ * a zoned block device
+ * @bdev: block device to operate on
+ *
+ * Restore all storage elements of @bdev that have been depopulated. Not all
+ * elements may be restored by this operation. This operation is destructive and
+ * will result in all sequential zones to be transitioned to the empty condition.
+ * The caller is responsible for taking care of any necessary invalidation of
+ * cached data. This function will block until the storage elements are restored
+ * or an error occurs.
+ *
+ * Returns 0 on success and a negative error code on failure.
+ */
+int bdev_restore_storage_elements(struct block_device *bdev)
+{
+ struct gendisk *disk = bdev->bd_disk;
+ unsigned int memflags;
+ int ret, err;
+
+ if (test_bit(GD_DEAD, &disk->state))
+ return -ENODEV;
+
+ if (!blk_queue_is_zoned(disk->queue) || !disk->nr_storage_elements)
+ return -EOPNOTSUPP;
+
+ /*
+ * Serialize against concurrent revalidation and concurrent storage
+ * element management operations so that we can see consistent zone
+ * condition changes.
+ */
+ mutex_lock(&disk->zone_revalidate_mutex);
+
+ /*
+ * Freeze and unfreeze the queue to flush any outstanding commands.
+ * The caller is responsible for not queuing up more I/O by higher
+ * level means.
+ */
+ memflags = blk_mq_freeze_queue(disk->queue);
+ blk_mq_unfreeze_queue(disk->queue, memflags);
+
+ /*
+ * Initiate the storage elements restoration and wait for its
+ * completion.
+ */
+ ret = disk->fops->se_ops->restore_elements(disk);
+
+ /*
+ * Always revalidate zones, even in the case of an error as we do not
+ * know what condition the zones of the block device are in. Note that
+ * since all sequential zones should have been transitioned to the empty
+ * condition, we can drop all zone write plugs. If an error occured and
+ * not all sequential zones are empty, the revalidation will reinstate
+ * the write plugs for these zones.
+ */
+ err = disk_revalidate_zones(disk, true);
+ if (err && !ret)
+ ret = err;
+
+ mutex_unlock(&disk->zone_revalidate_mutex);
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(bdev_restore_storage_elements);
+
+static int blkdev_ioctl_restore_storage_elements(struct block_device *bdev,
+ blk_mode_t mode)
+{
+ int ret;
+
+ /*
+ * Note: because restoring storage elements is a destructive operation
+ * equivalent to reformatting the device, we require an O_EXCL open here
+ * so that a user cannot trigger with another active user (e.g. a
+ * mounted file system).
+ */
+ if (!(mode & BLK_OPEN_EXCL))
+ return -EBADF;
+ if (!(mode & BLK_OPEN_WRITE))
+ return -EBADF;
+ if (bdev_read_only(bdev))
+ return -EPERM;
+
+ inode_lock(bdev->bd_mapping->host);
+ filemap_invalidate_lock(bdev->bd_mapping);
+
+ /*
+ * Storage element restoration is a destructive operation that will
+ * reset all zones. So invalidate all cached data that we may have so
+ * that old data cannot be read once the operation completes.
+ */
+ ret = truncate_bdev_range(bdev, mode, 0, bdev_nr_bytes(bdev) - 1);
+ if (!ret)
+ ret = bdev_restore_storage_elements(bdev);
+
+ filemap_invalidate_unlock(bdev->bd_mapping);
+ inode_unlock(bdev->bd_mapping->host);
+
+ return ret;
+}
+
+int disk_init_storage_element_mgmt(struct gendisk *disk)
+{
+ int ret;
+
+ /*
+ * Storage element operations are for zoned disks only and are
+ * optional...
+ */
+ if (!blk_queue_is_zoned(disk->queue) || !disk->fops->se_ops)
+ return 0;
+
+ /* ...but if defined, all operation callbacks must be defined. */
+ if (!disk->fops->se_ops->report_elements ||
+ !disk->fops->se_ops->remove_element ||
+ !disk->fops->se_ops->restore_elements) {
+ pr_err("%s: invalid storage element operations\n",
+ disk->disk_name);
+ return -EINVAL;
+ }
+
+ /*
+ * Get the number of storage elements of the disk. If this fails,
+ * do not report an error as the failure may be because the zoned disk
+ * does not support storage element management.
+ */
+ ret = disk->fops->se_ops->report_elements(disk, NULL,
+ &disk->nr_storage_elements);
+ if (ret)
+ disk->nr_storage_elements = 0;
+
+ return 0;
+}
+
+int blk_ioctl_zone_storage_elements(struct block_device *bdev,
+ blk_mode_t mode, unsigned int cmd,
+ void __user *argp)
+{
+ struct gendisk *disk = bdev->bd_disk;
+
+ if (!disk->nr_storage_elements)
+ return -ENOTTY;
+
+ switch (cmd) {
+ case BLKREPORTSTORELEMS:
+ return blkdev_ioctl_report_storage_elements(bdev, argp);
+ case BLKREMOVESTORELEM:
+ return blkdev_ioctl_remove_storage_element(bdev, mode, argp);
+ case BLKRESTORESTORELEMS:
+ return blkdev_ioctl_restore_storage_elements(bdev, mode);
+ default:
+ break;
+ }
+
+ return -ENOTTY;
+}
diff --git a/block/blk.h b/block/blk.h
index 0e38e4b88cf544..44876b148237c5 100644
--- a/block/blk.h
+++ b/block/blk.h
@@ -543,6 +543,7 @@ static inline void ioc_clear_queue(struct request_queue *q)
#endif /* CONFIG_BLK_ICQ */
#ifdef CONFIG_BLK_DEV_ZONED
+int disk_init_storage_element_mgmt(struct gendisk *disk);
void disk_init_zone_resources(struct gendisk *disk);
void disk_release_zone_resources(struct gendisk *disk);
static inline bool bio_zone_write_plugging(struct bio *bio)
@@ -594,7 +595,14 @@ int blkdev_report_zones_ioctl(struct block_device *bdev, unsigned int cmd,
int blkdev_zone_mgmt_ioctl(struct block_device *bdev, blk_mode_t mode,
unsigned int cmd, unsigned long arg);
bool bdev_zone_mgmt_allowed(struct block_device *bdev, sector_t sector);
+int blk_ioctl_zone_storage_elements(struct block_device *bdev,
+ blk_mode_t mode, unsigned int cmd,
+ void __user *argp);
#else /* CONFIG_BLK_DEV_ZONED */
+static inline int disk_init_storage_element_mgmt(struct gendisk *disk)
+{
+ return 0;
+}
static inline void disk_init_zone_resources(struct gendisk *disk)
{
}
@@ -641,6 +649,12 @@ static inline bool bdev_zone_mgmt_allowed(struct block_device *bdev,
{
return false;
}
+static inline int blk_ioctl_zone_storage_elements(struct block_device *bdev,
+ blk_mode_t mode, unsigned int cmd,
+ void __user *argp)
+{
+ return -ENOTTY;
+}
#endif /* CONFIG_BLK_DEV_ZONED */
struct block_device *bdev_alloc(struct gendisk *disk, u8 partno);
diff --git a/block/genhd.c b/block/genhd.c
index 69204d44adae11..251a5b07baff9c 100644
--- a/block/genhd.c
+++ b/block/genhd.c
@@ -435,6 +435,10 @@ static int __add_disk(struct device *parent, struct gendisk *disk,
if (WARN_ON_ONCE(bdev_nr_sectors(disk->part0) > BLK_DEV_MAX_SECTORS))
return -EINVAL;
+ ret = disk_init_storage_element_mgmt(disk);
+ if (ret)
+ return ret;
+
if (queue_is_mq(disk->queue)) {
/*
* ->submit_bio and ->poll_bio are bypassed for blk-mq drivers.
diff --git a/block/ioctl.c b/block/ioctl.c
index fb2f4b4d2505ed..0f043363048ca6 100644
--- a/block/ioctl.c
+++ b/block/ioctl.c
@@ -679,6 +679,10 @@ static int blkdev_common_ioctl(struct block_device *bdev, blk_mode_t mode,
return put_uint(argp, bdev_zone_sectors(bdev));
case BLKGETNRZONES:
return put_uint(argp, bdev_nr_zones(bdev));
+ case BLKREPORTSTORELEMS:
+ case BLKREMOVESTORELEM:
+ case BLKRESTORESTORELEMS:
+ return blk_ioctl_zone_storage_elements(bdev, mode, cmd, argp);
case BLKROGET:
return put_int(argp, bdev_read_only(bdev) != 0);
case BLKSSZGET: /* get block device logical block size */
diff --git a/drivers/block/zloop.c b/drivers/block/zloop.c
index 96666742523bb7..c4a0a845a0963d 100644
--- a/drivers/block/zloop.c
+++ b/drivers/block/zloop.c
@@ -18,6 +18,7 @@
#include <linux/parser.h>
#include <linux/seq_file.h>
#include <linux/xattr.h>
+#include <linux/delay.h>
/*
* Options for adding (and removing) a device.
@@ -37,6 +38,8 @@ enum {
ZLOOP_OPT_ORDERED_ZONE_APPEND = (1 << 10),
ZLOOP_OPT_DISCARD_WRITE_CACHE = (1 << 11),
ZLOOP_OPT_MAX_OPEN_ZONES = (1 << 12),
+ ZLOOP_OPT_STOR_ELEMENTS = (1 << 13),
+ ZLOOP_OPT_ELEMENT_ID = (1 << 14),
};
static const match_table_t zloop_opt_tokens = {
@@ -51,11 +54,23 @@ static const match_table_t zloop_opt_tokens = {
{ ZLOOP_OPT_BUFFERED_IO, "buffered_io" },
{ ZLOOP_OPT_ZONE_APPEND, "zone_append=%u" },
{ ZLOOP_OPT_ORDERED_ZONE_APPEND, "ordered_zone_append" },
- { ZLOOP_OPT_DISCARD_WRITE_CACHE, "discard_write_cache" },
+ { ZLOOP_OPT_DISCARD_WRITE_CACHE, "discard_write_cache" },
{ ZLOOP_OPT_MAX_OPEN_ZONES, "max_open_zones=%u" },
+ { ZLOOP_OPT_STOR_ELEMENTS, "stor_elements=%u" },
+ { ZLOOP_OPT_ELEMENT_ID, "element_id=%u" },
{ ZLOOP_OPT_ERR, NULL }
};
+/* Storage elements emulation types. */
+enum zloop_stor_elements {
+ /* No emulation. */
+ ZLOOP_STOR_ELEMENTS_NONE,
+ /* Emulate read+write storage elements. */
+ ZLOOP_STOR_ELEMENTS_RDWR,
+ /* Emulate pairs of associated read and write storage elements. */
+ ZLOOP_STOR_ELEMENTS_PAIRS,
+};
+
/* Default values for the "add" operation. */
#define ZLOOP_DEF_ID -1
#define ZLOOP_DEF_ZONE_SIZE ((256ULL * SZ_1M) >> SECTOR_SHIFT)
@@ -68,6 +83,8 @@ static const match_table_t zloop_opt_tokens = {
#define ZLOOP_DEF_BUFFERED_IO false
#define ZLOOP_DEF_ZONE_APPEND true
#define ZLOOP_DEF_ORDERED_ZONE_APPEND false
+#define ZLOOP_DEF_STOR_ELEMENTS ZLOOP_STOR_ELEMENTS_NONE
+#define ZLOOP_DEF_ELEMENT_ID 0
/* Arbitrary limit on the zone size (16GB). */
#define ZLOOP_MAX_ZONE_SIZE_MB 16384
@@ -87,6 +104,8 @@ struct zloop_options {
bool zone_append;
bool ordered_zone_append;
bool discard_write_cache;
+ enum zloop_stor_elements stor_elements;
+ unsigned int element_id;
};
/*
@@ -117,6 +136,8 @@ struct zloop_zone {
enum blk_zone_cond cond;
sector_t start;
sector_t wp;
+ unsigned int wr_se_id;
+ unsigned int rd_se_id;
gfp_t old_gfp_mask;
};
@@ -133,6 +154,7 @@ struct zloop_device {
bool zone_append;
bool ordered_zone_append;
bool discard_write_cache;
+ enum zloop_stor_elements stor_elements;
const char *base_dir;
struct file *data_dir;
@@ -150,6 +172,14 @@ struct zloop_device {
struct list_head open_zones_lru_list;
unsigned int nr_open_zones;
+ /* For storage elements emulation. */
+ struct mutex stor_elements_lock;
+ unsigned int nr_elements;
+ unsigned int nr_removed_elements;
+ unsigned int remove_element_id;
+ bool restore_in_progress;
+ struct blk_se *elements;
+
struct zloop_zone zones[] __counted_by(nr_zones);
};
@@ -289,22 +319,64 @@ static bool zloop_do_open_zone(struct zloop_device *zlo,
}
}
-static void zloop_mark_full(struct zloop_device *zlo, struct zloop_zone *zone)
+static void zloop_set_zone_cond(struct zloop_device *zlo,
+ struct zloop_zone *zone,
+ enum blk_zone_cond cond)
{
lockdep_assert_held(&zone->wp_lock);
zloop_lru_remove_open_zone(zlo, zone);
- zone->cond = BLK_ZONE_COND_FULL;
- zone->wp = ULLONG_MAX;
+ zone->cond = cond;
+ if (cond == BLK_ZONE_COND_EMPTY)
+ zone->wp = zone->start;
+ else
+ zone->wp = ULLONG_MAX;
+}
+
+static inline void zloop_set_zone_full(struct zloop_device *zlo,
+ struct zloop_zone *zone)
+{
+ zloop_set_zone_cond(zlo, zone, BLK_ZONE_COND_FULL);
+}
+
+static inline void zloop_set_zone_empty(struct zloop_device *zlo,
+ struct zloop_zone *zone)
+{
+ zloop_set_zone_cond(zlo, zone, BLK_ZONE_COND_EMPTY);
}
-static void zloop_mark_empty(struct zloop_device *zlo, struct zloop_zone *zone)
+static bool zloop_zone_element_is_ok(struct zloop_device *zlo,
+ struct zloop_zone *zone, bool for_write)
{
+ struct blk_se *se;
+
lockdep_assert_held(&zone->wp_lock);
- zloop_lru_remove_open_zone(zlo, zone);
- zone->cond = BLK_ZONE_COND_EMPTY;
- zone->wp = zone->start;
+ if (!zlo->nr_elements)
+ return true;
+
+ if (for_write)
+ se = &zlo->elements[zone->wr_se_id - 1];
+ else
+ se = &zlo->elements[zone->rd_se_id - 1];
+
+ return READ_ONCE(se->status) == BLK_SE_STS_OK;
+}
+
+static bool zloop_zone_is_ok(struct zloop_device *zlo, struct zloop_zone *zone)
+{
+ bool ret;
+
+ spin_lock(&zone->wp_lock);
+
+ ret = zloop_zone_element_is_ok(zlo, zone, true);
+ if (ret)
+ ret = zone->cond != BLK_ZONE_COND_OFFLINE &&
+ zone->cond != BLK_ZONE_COND_READONLY;
+
+ spin_unlock(&zone->wp_lock);
+
+ return ret;
}
static int zloop_update_seq_zone(struct zloop_device *zlo, unsigned int zone_no)
@@ -316,6 +388,15 @@ static int zloop_update_seq_zone(struct zloop_device *zlo, unsigned int zone_no)
lockdep_assert_held(&zone->lock);
+ /*
+ * If the zone is served by an element that is not in a good state, fail
+ * the operation.
+ */
+ if (!zloop_zone_is_ok(zlo, zone)) {
+ set_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags);
+ return -EIO;
+ }
+
ret = vfs_getattr(&zone->file->f_path, &stat, STATX_SIZE, 0);
if (ret < 0) {
pr_err("%pg: Failed to get zone %u file stat (err=%d)\n",
@@ -340,9 +421,9 @@ static int zloop_update_seq_zone(struct zloop_device *zlo, unsigned int zone_no)
spin_lock(&zone->wp_lock);
if (!file_sectors) {
- zloop_mark_empty(zlo, zone);
+ zloop_set_zone_empty(zlo, zone);
} else if (file_sectors == zlo->zone_capacity) {
- zloop_mark_full(zlo, zone);
+ zloop_set_zone_full(zlo, zone);
} else {
if (zone->cond != BLK_ZONE_COND_IMP_OPEN &&
zone->cond != BLK_ZONE_COND_EXP_OPEN)
@@ -364,6 +445,11 @@ static int zloop_open_zone(struct zloop_device *zlo, unsigned int zone_no)
mutex_lock(&zone->lock);
+ if (!zloop_zone_is_ok(zlo, zone)) {
+ ret = -EIO;
+ goto unlock;
+ }
+
if (test_and_clear_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags)) {
ret = zloop_update_seq_zone(zlo, zone_no);
if (ret)
@@ -389,6 +475,11 @@ static int zloop_close_zone(struct zloop_device *zlo, unsigned int zone_no)
mutex_lock(&zone->lock);
+ if (!zloop_zone_is_ok(zlo, zone)) {
+ ret = -EIO;
+ goto unlock;
+ }
+
if (test_and_clear_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags)) {
ret = zloop_update_seq_zone(zlo, zone_no);
if (ret)
@@ -421,7 +512,22 @@ static int zloop_close_zone(struct zloop_device *zlo, unsigned int zone_no)
return ret;
}
-static int zloop_reset_zone(struct zloop_device *zlo, unsigned int zone_no)
+static int zloop_do_reset_zone(struct zloop_device *zlo,
+ struct zloop_zone *zone)
+{
+ if (vfs_truncate(&zone->file->f_path, 0))
+ return -EIO;
+
+ spin_lock(&zone->wp_lock);
+ zloop_set_zone_empty(zlo, zone);
+ clear_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags);
+ spin_unlock(&zone->wp_lock);
+
+ return 0;
+}
+
+static int zloop_reset_zone(struct zloop_device *zlo, unsigned int zone_no,
+ bool all_zones)
{
struct zloop_zone *zone = &zlo->zones[zone_no];
int ret = 0;
@@ -431,20 +537,19 @@ static int zloop_reset_zone(struct zloop_device *zlo, unsigned int zone_no)
mutex_lock(&zone->lock);
+ if (!zloop_zone_is_ok(zlo, zone)) {
+ if (!all_zones)
+ ret = -EIO;
+ goto unlock;
+ }
+
if (!test_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags) &&
zone->cond == BLK_ZONE_COND_EMPTY)
goto unlock;
- if (vfs_truncate(&zone->file->f_path, 0)) {
+ ret = zloop_do_reset_zone(zlo, zone);
+ if (ret)
set_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags);
- ret = -EIO;
- goto unlock;
- }
-
- spin_lock(&zone->wp_lock);
- zloop_mark_empty(zlo, zone);
- clear_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags);
- spin_unlock(&zone->wp_lock);
unlock:
mutex_unlock(&zone->lock);
@@ -458,7 +563,7 @@ static int zloop_reset_all_zones(struct zloop_device *zlo)
int ret;
for (i = zlo->nr_conv_zones; i < zlo->nr_zones; i++) {
- ret = zloop_reset_zone(zlo, i);
+ ret = zloop_reset_zone(zlo, i, true);
if (ret)
return ret;
}
@@ -476,6 +581,11 @@ static int zloop_finish_zone(struct zloop_device *zlo, unsigned int zone_no)
mutex_lock(&zone->lock);
+ if (!zloop_zone_is_ok(zlo, zone)) {
+ ret = -EIO;
+ goto unlock;
+ }
+
if (!test_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags) &&
zone->cond == BLK_ZONE_COND_FULL)
goto unlock;
@@ -488,7 +598,7 @@ static int zloop_finish_zone(struct zloop_device *zlo, unsigned int zone_no)
}
spin_lock(&zone->wp_lock);
- zloop_mark_full(zlo, zone);
+ zloop_set_zone_full(zlo, zone);
clear_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags);
spin_unlock(&zone->wp_lock);
@@ -632,7 +742,7 @@ static int zloop_seq_write_prep(struct zloop_cmd *cmd)
if (!is_append || !zlo->ordered_zone_append) {
zone->wp += nr_sectors;
if (zone->wp == zone_end)
- zloop_mark_full(zlo, zone);
+ zloop_set_zone_full(zlo, zone);
}
out_unlock:
spin_unlock(&zone->wp_lock);
@@ -774,7 +884,7 @@ static void zloop_handle_cmd(struct zloop_cmd *cmd)
cmd->ret = zloop_flush(zlo);
break;
case REQ_OP_ZONE_RESET:
- cmd->ret = zloop_reset_zone(zlo, rq_zone_no(rq));
+ cmd->ret = zloop_reset_zone(zlo, rq_zone_no(rq), false);
break;
case REQ_OP_ZONE_RESET_ALL:
cmd->ret = zloop_reset_all_zones(zlo);
@@ -885,30 +995,65 @@ static void zloop_complete_rq(struct request *rq)
blk_mq_end_request(rq, sts);
}
-static bool zloop_set_zone_append_sector(struct request *rq)
+static bool zloop_set_zone_append_sector(struct zloop_device *zlo,
+ struct zloop_zone *zone,
+ struct request *rq)
{
- struct zloop_device *zlo = rq->q->queuedata;
- unsigned int zone_no = rq_zone_no(rq);
- struct zloop_zone *zone = &zlo->zones[zone_no];
sector_t zone_end = zone->start + zlo->zone_capacity;
sector_t nr_sectors = blk_rq_sectors(rq);
- spin_lock(&zone->wp_lock);
-
if (zone->cond == BLK_ZONE_COND_FULL ||
- zone->wp + nr_sectors > zone_end) {
- spin_unlock(&zone->wp_lock);
+ zone->wp + nr_sectors > zone_end)
return false;
- }
rq->__sector = zone->wp;
zone->wp += blk_rq_sectors(rq);
if (zone->wp >= zone_end)
- zloop_mark_full(zlo, zone);
+ zloop_set_zone_full(zlo, zone);
+
+ return true;
+}
+
+static bool zloop_prep_rq(struct zloop_device *zlo, struct request *rq)
+{
+ struct zloop_zone *zone = &zlo->zones[rq_zone_no(rq)];
+ bool is_write = op_is_write(req_op(rq));
+ bool ret = true;
+
+ if (!zlo->nr_elements &&
+ (!zlo->ordered_zone_append || req_op(rq) != REQ_OP_ZONE_APPEND))
+ return true;
+
+ spin_lock(&zone->wp_lock);
+ /*
+ * If storage element emulation is enabled, check the health state of
+ * the storage element serving the zone and the zone condition: do not
+ * allow any operation to zones served by an unusable storage element,
+ * operations to offline zones, and allow only read operations to
+ * read-only zones.
+ */
+ if (zlo->nr_elements) {
+ if (!zloop_zone_element_is_ok(zlo, zone, is_write) ||
+ zone->cond == BLK_ZONE_COND_OFFLINE ||
+ (zone->cond == BLK_ZONE_COND_READONLY && is_write)) {
+ ret = false;
+ goto unlock;
+ }
+ }
+
+ /*
+ * If we need to strongly order zone append operations, set the request
+ * sector to the zone write pointer location now instead of when the
+ * command work runs.
+ */
+ if (zlo->ordered_zone_append && req_op(rq) == REQ_OP_ZONE_APPEND)
+ ret = zloop_set_zone_append_sector(zlo, zone, rq);
+
+unlock:
spin_unlock(&zone->wp_lock);
- return true;
+ return ret;
}
static blk_status_t zloop_queue_rq(struct blk_mq_hw_ctx *hctx,
@@ -924,13 +1069,27 @@ static blk_status_t zloop_queue_rq(struct blk_mq_hw_ctx *hctx,
}
/*
- * If we need to strongly order zone append operations, set the request
- * sector to the zone write pointer location now instead of when the
- * command work runs.
+ * Do not allow commands while the restoration of storage elements is
+ * in progress.
+ */
+ if (READ_ONCE(zlo->restore_in_progress))
+ return BLK_STS_IOERR;
+
+ /*
+ * Check if we can accept data access commands and prepare zone append.
+ * If storage element emulation is enabled, this will check if the data
+ * access can be allowed based on the health state of the storage
+ * element assigned to the zone being accessed.
*/
- if (zlo->ordered_zone_append && req_op(rq) == REQ_OP_ZONE_APPEND) {
- if (!zloop_set_zone_append_sector(rq))
+ switch (req_op(rq)) {
+ case REQ_OP_READ:
+ case REQ_OP_WRITE:
+ case REQ_OP_ZONE_APPEND:
+ if (!zloop_prep_rq(zlo, rq))
return BLK_STS_IOERR;
+ break;
+ default:
+ break;
}
blk_mq_start_request(rq);
@@ -992,8 +1151,8 @@ static int zloop_report_zones(struct gendisk *disk, sector_t sector,
blkz.len = zlo->zone_size;
spin_lock(&zone->wp_lock);
blkz.wp = zone->wp;
- spin_unlock(&zone->wp_lock);
blkz.cond = zone->cond;
+ spin_unlock(&zone->wp_lock);
if (test_bit(ZLOOP_ZONE_CONV, &zone->flags)) {
blkz.type = BLK_ZONE_TYPE_CONVENTIONAL;
blkz.capacity = zlo->zone_size;
@@ -1012,6 +1171,356 @@ static int zloop_report_zones(struct gendisk *disk, sector_t sector,
return nr_zones;
}
+static int zloop_report_elements(struct gendisk *disk,
+ struct blk_se *elements,
+ unsigned int *nr_elements)
+{
+ struct zloop_device *zlo = disk->private_data;
+ struct blk_se *se;
+ unsigned int i;
+
+ if (test_bit(GD_DEAD, &disk->state) ||
+ data_race(READ_ONCE(zlo->state)) == Zlo_deleting)
+ return -ENODEV;
+
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_NONE)
+ return -EOPNOTSUPP;
+
+ mutex_lock(&zlo->stor_elements_lock);
+
+ if (!elements) {
+ *nr_elements = zlo->nr_elements;
+ goto unlock;
+ }
+
+ *nr_elements = min(*nr_elements, zlo->nr_elements);
+ for (i = 0, se = zlo->elements; i < *nr_elements; i++, se++) {
+ switch (READ_ONCE(se->status)) {
+ case BLK_SE_STS_REMOVED:
+ case BLK_SE_STS_RESTORE_ERROR:
+ se->restore_allowed = 1;
+ break;
+ default:
+ se->restore_allowed = 0;
+ }
+ memcpy(&elements[i], se, sizeof(struct blk_se));
+ }
+
+unlock:
+ mutex_unlock(&zlo->stor_elements_lock);
+
+ return 0;
+}
+
+static int zloop_degrade_element(struct zloop_device *zlo,
+ unsigned int element_id)
+{
+ struct blk_se *se;
+ int ret = 0;
+
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_NONE)
+ return -EOPNOTSUPP;
+
+ if (!element_id || element_id > zlo->nr_elements)
+ return -EINVAL;
+
+ mutex_lock(&zlo->stor_elements_lock);
+
+ se = &zlo->elements[element_id - 1];
+ if (READ_ONCE(se->status) == BLK_SE_STS_OK)
+ WRITE_ONCE(se->status, BLK_SE_STS_DEGRADED);
+ else
+ ret = -EINVAL;
+
+ mutex_unlock(&zlo->stor_elements_lock);
+
+ return ret;
+}
+
+static int zloop_execute_remove_element(struct zloop_device *zlo)
+{
+ struct gendisk *disk = zlo->disk;
+ struct blk_se *se, *paired_se = NULL;
+ enum blk_zone_cond cond;
+ struct zloop_zone *zone;
+ unsigned int memflags, i;
+ int ret = 0;
+
+ /* Wait for all IOs to complete. */
+ memflags = blk_mq_freeze_queue(disk->queue);
+ blk_mq_unfreeze_queue(disk->queue, memflags);
+
+ /*
+ * If the element to remove is a read element, zones must go offline and
+ * the associated write element is also removed.
+ */
+ se = &zlo->elements[zlo->remove_element_id - 1];
+ switch (se->type) {
+ case BLK_SE_TYPE_RDWR:
+ cond = BLK_ZONE_COND_OFFLINE;
+ break;
+ case BLK_SE_TYPE_READ:
+ cond = BLK_ZONE_COND_OFFLINE;
+ paired_se = &zlo->elements[se->paired_id - 1];
+ break;
+ case BLK_SE_TYPE_WRITE:
+ cond = BLK_ZONE_COND_READONLY;
+ break;
+ default:
+ WARN_ON_ONCE(1);
+ return -EINVAL;
+ }
+
+ /*
+ * Change the condition of the zones owned by the (pair of) elements
+ * being removed and mark the elements removed.
+ */
+ for (i = 0, zone = zlo->zones; i < zlo->nr_zones; i++, zone++) {
+ if (zone->wr_se_id != se->id && zone->rd_se_id != se->id)
+ continue;
+ mutex_lock(&zone->lock);
+ spin_lock(&zone->wp_lock);
+ zloop_set_zone_cond(zlo, zone, cond);
+ clear_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags);
+ spin_unlock(&zone->wp_lock);
+ mutex_unlock(&zone->lock);
+ }
+
+ WRITE_ONCE(se->status, BLK_SE_STS_REMOVED);
+ zlo->nr_removed_elements++;
+ if (paired_se && READ_ONCE(paired_se->status) != BLK_SE_STS_REMOVED) {
+ WRITE_ONCE(paired_se->status, BLK_SE_STS_REMOVED);
+ zlo->nr_removed_elements++;
+ }
+
+ return ret;
+}
+
+static int zloop_remove_element(struct gendisk *disk, unsigned int element_id)
+{
+ struct zloop_device *zlo = disk->private_data;
+ struct blk_se *se, *paired_se = NULL;
+ unsigned int nr_remove = 1;
+ int ret = 0;
+
+ if (test_bit(GD_DEAD, &disk->state) ||
+ data_race(READ_ONCE(zlo->state)) == Zlo_deleting)
+ return -ENODEV;
+
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_NONE)
+ return -EOPNOTSUPP;
+
+ if (!element_id || element_id > zlo->nr_elements)
+ return -EINVAL;
+
+ mutex_lock(&zlo->stor_elements_lock);
+
+ /* If a remove or restore operation is already in progress, bail out. */
+ if (zlo->remove_element_id || READ_ONCE(zlo->restore_in_progress)) {
+ ret = -EBUSY;
+ goto unlock;
+ }
+
+ /*
+ * Get the element to remove. If it is already removed, we have nothing
+ * to do.
+ */
+ se = &zlo->elements[element_id - 1];
+ if (READ_ONCE(se->status) == BLK_SE_STS_REMOVED)
+ goto unlock;
+
+ /*
+ * If the element to remove is a read element, the associated write
+ * element must also be removed.
+ */
+ if (se->type == BLK_SE_TYPE_READ) {
+ paired_se = &zlo->elements[se->paired_id - 1];
+ if (READ_ONCE(paired_se->status) != BLK_SE_STS_REMOVED)
+ nr_remove = 2;
+ }
+
+ /*
+ * Make sure we do not allow removing all storage elements as that does
+ * not make any sense. This is consistent with the device advertized
+ * limit of SCSI and ATA devices supporting the storage element
+ * depopulation feature.
+ */
+ if (zlo->nr_removed_elements + nr_remove >= zlo->nr_elements) {
+ ret = -EINVAL;
+ goto unlock;
+ }
+
+ /*
+ * Change the elements to be removed to the remove in progress state so
+ * that concurrent report elements can see the operation on-going.
+ */
+ WRITE_ONCE(se->status, BLK_SE_STS_REMOVE_IN_PROGRESS);
+ if (paired_se && READ_ONCE(paired_se->status) != BLK_SE_STS_REMOVED)
+ WRITE_ONCE(paired_se->status, BLK_SE_STS_REMOVE_IN_PROGRESS);
+ zlo->remove_element_id = element_id;
+
+ mutex_unlock(&zlo->stor_elements_lock);
+
+ /*
+ * Wait a little so that this looks like a real device taking time to
+ * operate.
+ */
+ msleep(2000);
+
+ mutex_lock(&zlo->stor_elements_lock);
+
+ /* Remove the element(s). */
+ ret = zloop_execute_remove_element(zlo);
+ zlo->remove_element_id = 0;
+
+unlock:
+ mutex_unlock(&zlo->stor_elements_lock);
+
+ return ret;
+}
+
+static int zloop_restore_zone(struct zloop_device *zlo,
+ struct zloop_zone *zone)
+{
+ int ret = 0;
+
+ mutex_lock(&zone->lock);
+
+ if (!test_bit(ZLOOP_ZONE_CONV, &zone->flags)) {
+ ret = vfs_truncate(&zone->file->f_path, 0);
+ if (ret) {
+ set_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags);
+ ret = -EIO;
+ goto unlock;
+ }
+ }
+
+ spin_lock(&zone->wp_lock);
+ if (test_bit(ZLOOP_ZONE_CONV, &zone->flags)) {
+ zloop_set_zone_cond(zlo, zone, BLK_ZONE_COND_NOT_WP);
+ } else {
+ zloop_set_zone_empty(zlo, zone);
+ clear_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags);
+ }
+ spin_unlock(&zone->wp_lock);
+
+unlock:
+ mutex_unlock(&zone->lock);
+
+ return ret;
+}
+
+static int zloop_execute_restore_elements(struct zloop_device *zlo)
+{
+ struct gendisk *disk = zlo->disk;
+ struct zloop_zone *zone;
+ struct blk_se *se;
+ enum blk_se_status sts = BLK_SE_STS_OK;
+ unsigned int memflags, i;
+ int ret = 0;
+
+ /* Wait for all IOs to complete. */
+ memflags = blk_mq_freeze_queue(disk->queue);
+ blk_mq_unfreeze_queue(disk->queue, memflags);
+
+ /*
+ * Truncate all files for sequential zones and restore the condition of
+ * all zones. If this fails, we will propagate the failure as a
+ * restoration error.
+ */
+ for (i = 0, zone = zlo->zones; i < zlo->nr_zones; i++, zone++) {
+ ret = zloop_restore_zone(zlo, zone);
+ if (ret) {
+ pr_err("zloop%d: restore zone %u failed %d\n",
+ zlo->id, i, ret);
+ sts = BLK_SE_STS_RESTORE_ERROR;
+ break;
+ }
+ }
+
+ /*
+ * Update the status of all elements being restored based on the zone
+ * restoration result.
+ */
+ for (i = 0, se = zlo->elements; i < zlo->nr_elements; i++, se++) {
+ if (READ_ONCE(se->status) == BLK_SE_STS_RESTORE_IN_PROGRESS)
+ WRITE_ONCE(se->status, sts);
+ }
+
+ /*
+ * Clear the counter of removed elements so that even if we had an error
+ * and set the elements health to BLK_SE_STS_RESTORE_ERROR, then can be
+ * removed again.
+ */
+ zlo->nr_removed_elements = 0;
+
+ return ret;
+}
+
+static int zloop_restore_elements(struct gendisk *disk)
+{
+ struct zloop_device *zlo = disk->private_data;
+ struct blk_se *se;
+ unsigned int i, n = 0;
+ int ret = 0;
+
+ if (test_bit(GD_DEAD, &disk->state) ||
+ data_race(READ_ONCE(zlo->state)) == Zlo_deleting)
+ return -ENODEV;
+
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_NONE)
+ return -EOPNOTSUPP;
+
+ mutex_lock(&zlo->stor_elements_lock);
+
+ /* If a remove or restore operation is already in progress, bail out. */
+ if (zlo->remove_element_id || READ_ONCE(zlo->restore_in_progress)) {
+ ret = -EBUSY;
+ goto unlock;
+ }
+
+ /*
+ * Set the restore in progress state for elements that will be
+ * restored.
+ */
+ for (i = 0, se = zlo->elements; i < zlo->nr_elements; i++, se++) {
+ switch (READ_ONCE(se->status)) {
+ case BLK_SE_STS_REMOVED:
+ case BLK_SE_STS_RESTORE_ERROR:
+ WRITE_ONCE(se->status, BLK_SE_STS_RESTORE_IN_PROGRESS);
+ n++;
+ break;
+ default:
+ break;
+ }
+ }
+
+ /* If we have no removed or in error elements, we have nothing to do. */
+ if (!n)
+ goto unlock;
+
+ WRITE_ONCE(zlo->restore_in_progress, true);
+ mutex_unlock(&zlo->stor_elements_lock);
+
+ /*
+ * Wait a little so that this looks like a real device taking time to
+ * operate.
+ */
+ msleep(5000);
+
+ mutex_lock(&zlo->stor_elements_lock);
+
+ /* Restore the elements. */
+ ret = zloop_execute_restore_elements(zlo);
+
+ WRITE_ONCE(zlo->restore_in_progress, false);
+
+unlock:
+ mutex_unlock(&zlo->stor_elements_lock);
+
+ return ret;
+}
+
static void zloop_free_disk(struct gendisk *disk)
{
struct zloop_device *zlo = disk->private_data;
@@ -1029,15 +1538,23 @@ static void zloop_free_disk(struct gendisk *disk)
fput(zlo->data_dir);
destroy_workqueue(zlo->workqueue);
+ kfree(zlo->elements);
kfree(zlo->base_dir);
kvfree(zlo);
}
+static const struct blk_se_ops zloop_se_ops = {
+ .report_elements = zloop_report_elements,
+ .remove_element = zloop_remove_element,
+ .restore_elements = zloop_restore_elements,
+};
+
static const struct block_device_operations zloop_fops = {
.owner = THIS_MODULE,
.open = zloop_open,
.report_zones = zloop_report_zones,
.free_disk = zloop_free_disk,
+ .se_ops = &zloop_se_ops,
};
__printf(3, 4)
@@ -1114,6 +1631,25 @@ static int zloop_init_zone(struct zloop_device *zlo, struct zloop_options *opts,
spin_lock_init(&zone->wp_lock);
zone->start = (sector_t)zone_no << zlo->zone_shift;
+ /* If we are emulating storage elements, assign one to the zone. */
+ if (zlo->stor_elements != ZLOOP_STOR_ELEMENTS_NONE) {
+ unsigned int nr_elems = zlo->nr_elements;
+ unsigned int se_idx;
+
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_PAIRS)
+ nr_elems /= 2;
+ se_idx = zone_no % nr_elems;
+ zlo->elements[se_idx].nr_zones++;
+
+ zone->wr_se_id = se_idx + 1;
+ zone->rd_se_id = zone->wr_se_id;
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_PAIRS) {
+ zone->rd_se_id += nr_elems;
+ zlo->elements[zone->rd_se_id - 1].nr_zones =
+ zlo->elements[se_idx].nr_zones;
+ }
+ }
+
if (!restore)
oflags |= O_CREAT;
@@ -1189,6 +1725,69 @@ static int zloop_init_zone(struct zloop_device *zlo, struct zloop_options *opts,
return ret;
}
+#define ZLOOP_MIN_STOR_ELEMENTS 2
+#define ZLOOP_MAX_STOR_ELEMENTS 32
+#define ZLOOP_MIN_ZONES_PER_STOR_ELEMENTS 32
+
+static int zloop_create_storage_elements(struct zloop_device *zlo)
+{
+ struct blk_se *se, *paired_se;
+ unsigned int i, nr_elems, nr_elements;
+
+ /*
+ * Calculate the number of storage elements we are going to emulate.
+ * To achieve a somewhat realistic emulation, we want at least 2 storage
+ * elements, and no more than 32, targeting at least 32 zones per
+ * element.
+ */
+ if (zlo->nr_zones <= 64)
+ nr_elements = ZLOOP_MIN_STOR_ELEMENTS;
+ else
+ nr_elements =
+ min(ZLOOP_MAX_STOR_ELEMENTS,
+ zlo->nr_zones / ZLOOP_MIN_ZONES_PER_STOR_ELEMENTS);
+
+ /*
+ * If we are emulating pairs of read and write storage elements, we need
+ * double the number of storage element descriptors.
+ */
+ nr_elems = nr_elements;
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_PAIRS)
+ nr_elems *= 2;
+ zlo->elements = kzalloc_objs(struct blk_se, nr_elems);
+ if (!zlo->elements)
+ return -ENOMEM;
+
+ for (i = 0, se = zlo->elements; i < nr_elements; i++, se++) {
+ se->id = i + 1;
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_PAIRS)
+ se->type = BLK_SE_TYPE_WRITE;
+ else
+ se->type = BLK_SE_TYPE_RDWR;
+ se->status = BLK_SE_STS_OK;
+ }
+
+ /*
+ * If we are emulating pairs of read and write storage elements,
+ * initialize the read elements paired with the write elements we just
+ * initialized.
+ */
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_PAIRS) {
+ for (i = 0; i < nr_elements; i++, se++) {
+ se->id = nr_elements + i + 1;
+ paired_se = &zlo->elements[i];
+ se->paired_id = paired_se->id;
+ paired_se->paired_id = se->id;
+ se->type = BLK_SE_TYPE_READ;
+ se->status = BLK_SE_STS_OK;
+ }
+ }
+
+ zlo->nr_elements = nr_elems;
+
+ return 0;
+}
+
static bool zloop_dev_exists(struct zloop_device *zlo)
{
struct file *cnv, *seq;
@@ -1221,6 +1820,11 @@ static int zloop_ctl_add(struct zloop_options *opts)
int ret = -EINVAL;
bool restore;
+ if (opts->mask & ZLOOP_OPT_ELEMENT_ID) {
+ pr_err("Invalid option specified for add\n");
+ return -EINVAL;
+ }
+
__module_get(THIS_MODULE);
nr_zones = opts->capacity >> ilog2(opts->zone_size);
@@ -1244,6 +1848,7 @@ static int zloop_ctl_add(struct zloop_options *opts)
WRITE_ONCE(zlo->state, Zlo_creating);
spin_lock_init(&zlo->open_zones_lock);
INIT_LIST_HEAD(&zlo->open_zones_lru_list);
+ mutex_init(&zlo->stor_elements_lock);
ret = mutex_lock_killable(&zloop_ctl_mutex);
if (ret)
@@ -1277,12 +1882,19 @@ static int zloop_ctl_add(struct zloop_options *opts)
if (zlo->zone_append)
zlo->ordered_zone_append = opts->ordered_zone_append;
zlo->discard_write_cache = opts->discard_write_cache;
+ zlo->stor_elements = opts->stor_elements;
+
+ if (zlo->stor_elements != ZLOOP_STOR_ELEMENTS_NONE) {
+ ret = zloop_create_storage_elements(zlo);
+ if (ret)
+ goto out_free_idr;
+ }
zlo->workqueue = alloc_workqueue("zloop%d", WQ_UNBOUND | WQ_FREEZABLE,
opts->nr_queues * opts->queue_depth, zlo->id);
if (!zlo->workqueue) {
ret = -ENOMEM;
- goto out_free_idr;
+ goto out_destroy_storage_elements;
}
if (opts->base_dir)
@@ -1368,6 +1980,10 @@ static int zloop_ctl_add(struct zloop_options *opts)
zlo->id, zlo->nr_zones,
((sector_t)zlo->zone_size << SECTOR_SHIFT) >> 20,
zlo->block_size);
+ if (zlo->nr_elements)
+ pr_info("zloop%d: %u storage elements\n",
+ zlo->id, zlo->nr_elements);
+
pr_info("zloop%d: using %s%s zone append\n",
zlo->id,
zlo->ordered_zone_append ? "ordered " : "",
@@ -1391,6 +2007,8 @@ static int zloop_ctl_add(struct zloop_options *opts)
kfree(zlo->base_dir);
out_destroy_workqueue:
destroy_workqueue(zlo->workqueue);
+out_destroy_storage_elements:
+ kfree(zlo->elements);
out_free_idr:
mutex_lock(&zloop_ctl_mutex);
idr_remove(&zloop_index_idr, zlo->id);
@@ -1490,6 +2108,49 @@ static int zloop_ctl_remove(struct zloop_options *opts)
return 0;
}
+static int zloop_ctl_degrade_element(struct zloop_options *opts)
+{
+ struct zloop_device *zlo;
+ int ret = 0;
+
+ if (!(opts->mask & ZLOOP_OPT_ID)) {
+ pr_err("No ID specified for degrade_element\n");
+ return -EINVAL;
+ }
+
+ if (!(opts->mask & ZLOOP_OPT_ELEMENT_ID)) {
+ pr_err("No element ID specified for degrade_element\n");
+ return -EINVAL;
+ }
+
+ if (opts->mask & ~(ZLOOP_OPT_ID | ZLOOP_OPT_ELEMENT_ID)) {
+ pr_err("Invalid option specified for degrade_element\n");
+ return -EINVAL;
+ }
+
+ ret = mutex_lock_killable(&zloop_ctl_mutex);
+ if (ret)
+ return ret;
+
+ zlo = idr_find(&zloop_index_idr, opts->id);
+ if (!zlo || zlo->state == Zlo_creating)
+ ret = -ENODEV;
+ else if (zlo->state == Zlo_deleting)
+ ret = -EINVAL;
+ if (ret)
+ goto unlock;
+
+ ret = zloop_degrade_element(zlo, opts->element_id);
+ if (!ret)
+ pr_info("%pg: degraded element %u\n",
+ zlo->disk->part0, opts->element_id);
+
+unlock:
+ mutex_unlock(&zloop_ctl_mutex);
+
+ return ret;
+}
+
static int zloop_parse_options(struct zloop_options *opts, const char *buf)
{
substring_t args[MAX_OPT_ARGS];
@@ -1509,6 +2170,8 @@ static int zloop_parse_options(struct zloop_options *opts, const char *buf)
opts->buffered_io = ZLOOP_DEF_BUFFERED_IO;
opts->zone_append = ZLOOP_DEF_ZONE_APPEND;
opts->ordered_zone_append = ZLOOP_DEF_ORDERED_ZONE_APPEND;
+ opts->stor_elements = ZLOOP_DEF_STOR_ELEMENTS;
+ opts->element_id = ZLOOP_DEF_ELEMENT_ID;
if (!buf)
return 0;
@@ -1643,6 +2306,30 @@ static int zloop_parse_options(struct zloop_options *opts, const char *buf)
case ZLOOP_OPT_DISCARD_WRITE_CACHE:
opts->discard_write_cache = true;
break;
+ case ZLOOP_OPT_STOR_ELEMENTS:
+ if (match_uint(args, &token)) {
+ ret = -EINVAL;
+ goto out;
+ }
+ switch (token) {
+ case ZLOOP_STOR_ELEMENTS_NONE:
+ case ZLOOP_STOR_ELEMENTS_RDWR:
+ case ZLOOP_STOR_ELEMENTS_PAIRS:
+ break;
+ default:
+ pr_err("Invalid stor_elements value\n");
+ ret = -EINVAL;
+ goto out;
+ }
+ opts->stor_elements = token;
+ break;
+ case ZLOOP_OPT_ELEMENT_ID:
+ if (match_uint(args, &token)) {
+ ret = -EINVAL;
+ goto out;
+ }
+ opts->element_id = token;
+ break;
case ZLOOP_OPT_ERR:
default:
pr_warn("unknown parameter or missing value '%s'\n", p);
@@ -1671,14 +2358,16 @@ static int zloop_parse_options(struct zloop_options *opts, const char *buf)
enum {
ZLOOP_CTL_ADD,
ZLOOP_CTL_REMOVE,
+ ZLOOP_CTL_DEGRADE_ELEMENT,
};
static struct zloop_ctl_op {
int code;
const char *name;
} zloop_ctl_ops[] = {
- { ZLOOP_CTL_ADD, "add" },
- { ZLOOP_CTL_REMOVE, "remove" },
+ { ZLOOP_CTL_ADD, "add" },
+ { ZLOOP_CTL_REMOVE, "remove" },
+ { ZLOOP_CTL_DEGRADE_ELEMENT, "degrade_element" },
{ -1, NULL },
};
@@ -1726,6 +2415,9 @@ static ssize_t zloop_ctl_write(struct file *file, const char __user *ubuf,
case ZLOOP_CTL_REMOVE:
ret = zloop_ctl_remove(&opts);
break;
+ case ZLOOP_CTL_DEGRADE_ELEMENT:
+ ret = zloop_ctl_degrade_element(&opts);
+ break;
default:
pr_err("Invalid operation\n");
ret = -EINVAL;
@@ -1749,6 +2441,8 @@ static int zloop_ctl_show(struct seq_file *seq_file, void *private)
tok = &zloop_opt_tokens[i];
if (!tok->pattern)
break;
+ if (tok->token == ZLOOP_OPT_ELEMENT_ID)
+ continue;
if (i)
seq_putc(seq_file, ',');
seq_puts(seq_file, tok->pattern);
@@ -1759,6 +2453,10 @@ static int zloop_ctl_show(struct seq_file *seq_file, void *private)
seq_puts(seq_file, zloop_ctl_ops[1].name);
seq_puts(seq_file, " id=%d\n");
+ /* Degrade element operation */
+ seq_puts(seq_file, zloop_ctl_ops[2].name);
+ seq_puts(seq_file, " id=%d,element_id=%u\n");
+
return 0;
}
diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index dc45df819c426a..8b1c05de541dd4 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -1424,7 +1424,8 @@ static blk_status_t sd_setup_read_write_cmnd(struct scsi_cmnd *cmd)
{
struct request *rq = scsi_cmd_to_rq(cmd);
struct scsi_device *sdp = cmd->device;
- struct scsi_disk *sdkp = scsi_disk(rq->q->disk);
+ struct gendisk *disk = rq->q->disk;
+ struct scsi_disk *sdkp = scsi_disk(disk);
sector_t lba = sectors_to_logical(sdp, blk_rq_pos(rq));
sector_t threshold;
unsigned int nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));
@@ -1456,6 +1457,23 @@ static blk_status_t sd_setup_read_write_cmnd(struct scsi_cmnd *cmd)
goto fail;
}
+ /*
+ * The block layer already prevents writes to offline zones of zoned
+ * block devices. But there are no checks for reads, so check this here.
+ * Since disk_zone_is_offline() uses the block layer cached zone
+ * condition, this is inherently racy with zone revalidation updating
+ * the zone condition. However, this will never prevent valid read
+ * operations as the only case where we could proceed is if the read is
+ * received after a restoration of the disk storage element completes
+ * but before the zones are revalidated. And in that case, all zones are
+ * empty and thus have no data to read.
+ * Note that we are quiet about the error (no message) to avoid flooding
+ * the kernel log.
+ */
+ if (sdkp->device->type == TYPE_ZBC &&
+ disk_zone_is_offline(disk, blk_rq_pos(rq)))
+ goto fail;
+
/*
* Some SD card readers can't handle accesses which touch the
* last one or two logical blocks. Split accesses as needed.
@@ -3939,6 +3957,9 @@ static const struct block_device_operations sd_fops = {
.get_unique_id = sd_get_unique_id,
.free_disk = scsi_disk_free_disk,
.pr_ops = &sd_pr_ops,
+#ifdef CONFIG_BLK_DEV_ZONED
+ .se_ops = &sd_zbc_se_ops,
+#endif
};
/**
diff --git a/drivers/scsi/sd.h b/drivers/scsi/sd.h
index 574af82430169e..8a32e5f771d8cb 100644
--- a/drivers/scsi/sd.h
+++ b/drivers/scsi/sd.h
@@ -156,6 +156,9 @@ struct scsi_disk {
unsigned ignore_medium_access_errors : 1;
unsigned rscs : 1; /* reduced stream control support */
unsigned use_atomic_write_boundary : 1;
+ unsigned depop_report_supported : 1;
+ unsigned depop_modify_zones_supported : 1;
+ unsigned depop_restore_supported : 1;
};
#define to_scsi_disk(obj) container_of(obj, struct scsi_disk, disk_dev)
@@ -242,6 +245,8 @@ unsigned int sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes,
int sd_zbc_report_zones(struct gendisk *disk, sector_t sector,
unsigned int nr_zones, struct blk_report_zones_args *args);
+extern const struct blk_se_ops sd_zbc_se_ops;
+
#else /* CONFIG_BLK_DEV_ZONED */
static inline int sd_zbc_read_zones(struct scsi_disk *sdkp,
diff --git a/drivers/scsi/sd_zbc.c b/drivers/scsi/sd_zbc.c
index 8cbf171e6fabf1..3bad1392f0e259 100644
--- a/drivers/scsi/sd_zbc.c
+++ b/drivers/scsi/sd_zbc.c
@@ -12,6 +12,7 @@
#include <linux/vmalloc.h>
#include <linux/sched/mm.h>
#include <linux/mutex.h>
+#include <linux/delay.h>
#include <linux/unaligned.h>
@@ -516,11 +517,50 @@ static int sd_zbc_check_capacity(struct scsi_disk *sdkp, unsigned char *buf,
return 0;
}
+/*
+ * sd_zbc_check_depop_supported - Check if the device supports storage element
+ * depopulation
+ * @sdkp: Target SCSI disk
+ * @buf: command buffer
+ *
+ * Check if the device supports the storage elements depopulation management
+ * commands.
+ */
+static void sd_zbc_check_depop_supported(struct scsi_disk *sdkp,
+ unsigned char *buf)
+{
+ /* Mandate support for reporting storage elements health. */
+ sdkp->depop_report_supported =
+ scsi_report_opcode(sdkp->device, buf, SD_BUF_SIZE,
+ SERVICE_ACTION_IN_16,
+ SAI_GET_PHYSICAL_ELEMENT_STATUS) == 1;
+ if (!sdkp->depop_report_supported) {
+ sdkp->depop_modify_zones_supported = false;
+ sdkp->depop_restore_supported = false;
+ return;
+ }
+
+ sdkp->depop_modify_zones_supported =
+ scsi_report_opcode(sdkp->device, buf, SD_BUF_SIZE,
+ SERVICE_ACTION_IN_16,
+ SAI_REMOVE_ELEMENT_AND_MODIFY_ZONES) == 1;
+
+ sdkp->depop_restore_supported =
+ scsi_report_opcode(sdkp->device, buf, SD_BUF_SIZE,
+ SERVICE_ACTION_IN_16,
+ SAI_RESTORE_ELEMENTS_AND_REBUILD) == 1;
+}
+
static void sd_zbc_print_zones(struct scsi_disk *sdkp)
{
if (sdkp->device->type != TYPE_ZBC || !sdkp->capacity)
return;
+ if (sdkp->depop_modify_zones_supported)
+ sd_printk(KERN_NOTICE, sdkp, "Depop modify zones supported\n");
+ if (sdkp->depop_restore_supported)
+ sd_printk(KERN_NOTICE, sdkp, "Depop restoration supported\n");
+
if (sdkp->capacity & (sdkp->zone_info.zone_blocks - 1))
sd_printk(KERN_NOTICE, sdkp,
"%u zones of %u logical blocks + 1 runt zone\n",
@@ -533,6 +573,462 @@ static void sd_zbc_print_zones(struct scsi_disk *sdkp)
sdkp->zone_info.zone_blocks);
}
+static void sd_zbc_parse_storage_element(struct scsi_disk *sdkp, u8 *desc,
+ struct blk_se *element)
+{
+ struct scsi_device *sdp = sdkp->device;
+ sector_t zone_sectors = sd_zbc_zone_sectors(sdkp);
+ u64 capacity;
+
+ memset(element, 0, sizeof(*element));
+
+ element->id = get_unaligned_be32(&desc[4]);
+ element->restore_allowed = desc[13] & 0x01;
+
+ switch (desc[14]) {
+ case SCSI_PHYS_ELEM_TYPE_ALL_ACCESS_STORAGE:
+ element->type = BLK_SE_TYPE_RDWR;
+ capacity = get_unaligned_be64(&desc[16]);
+ if (!zone_sectors || capacity == ULLONG_MAX)
+ element->nr_zones = 0;
+ else
+ element->nr_zones =
+ logical_to_sectors(sdp, capacity) >>
+ ilog2(zone_sectors);
+ break;
+ case SCSI_PHYS_ELEM_TYPE_FRAC_ACCESS_STORAGE:
+ element->paired_id = get_unaligned_be32(&desc[16]);
+ if (desc[20] & 0x01)
+ element->type = BLK_SE_TYPE_READ;
+ else
+ element->type = BLK_SE_TYPE_WRITE;
+ element->nr_zones = get_unaligned_be64(&desc[24]);
+ break;
+ default:
+ element->type = BLK_SE_TYPE_UNKNOWN;
+ }
+
+ if (desc[15] >= SCSI_PHYS_ELEM_HEALTH_WITHIN_SPEC_LIMITS_MIN &&
+ desc[15] <= SCSI_PHYS_ELEM_HEALTH_WITHIN_SPEC_LIMITS_MAX) {
+ element->status = BLK_SE_STS_OK;
+ return;
+ }
+
+ if (desc[15] >= SCSI_PHYS_ELEM_HEALTH_OUTSIDE_SPEC_LIMITS_MIN &&
+ desc[15] <= SCSI_PHYS_ELEM_HEALTH_OUTSIDE_SPEC_LIMITS_MAX) {
+ element->status = BLK_SE_STS_DEGRADED;
+ return;
+ }
+
+ switch (desc[15]) {
+ case SCSI_PHYS_ELEM_HEALTH_AT_SPEC_LIMITS:
+ element->status = BLK_SE_STS_OK_LIMIT;
+ break;
+ case SCSI_PHYS_ELEM_HEALTH_DEPOP_REVOKE_ERR:
+ element->status = BLK_SE_STS_RESTORE_ERROR;
+ break;
+ case SCSI_PHYS_ELEM_HEALTH_DEPOP_REVOKE_IN_PROGRESS:
+ element->status = BLK_SE_STS_RESTORE_IN_PROGRESS;
+ break;
+ case SCSI_PHYS_ELEM_HEALTH_DEPOP_ERR:
+ element->status = BLK_SE_STS_REMOVE_ERROR;
+ break;
+ case SCSI_PHYS_ELEM_HEALTH_DEPOP_IN_PROGRESS:
+ element->status = BLK_SE_STS_REMOVE_IN_PROGRESS;
+ break;
+ case SCSI_PHYS_ELEM_HEALTH_DEPOP_OK:
+ element->status = BLK_SE_STS_REMOVED;
+ break;
+ case SCSI_PHYS_ELEM_HEALTH_NOT_REPORTED:
+ default:
+ element->status = BLK_SE_STS_UNKNOWN;
+ break;
+ }
+}
+
+/*
+ * Large hard limit on the number of storage elements. This accommodates all
+ * known devices today and likely forever :)
+ */
+#define SD_ZBC_MAX_STORAGE_ELEMENTS 255
+
+struct sd_zbc_se_report {
+ struct blk_se *elements;
+ unsigned int nr_elements;
+ unsigned int nr_reported_elements;
+ bool need_free_elements;
+ u8 *buf;
+ unsigned int buf_size;
+};
+
+static int sd_zbc_alloc_se_report(struct blk_se *elements,
+ unsigned int nr_elements,
+ bool alloc_elements,
+ struct sd_zbc_se_report *rep)
+{
+ /*
+ * We need at least 32B for the report header and 32B for each
+ * descriptor. Note that we use GFP_KERNEL here, which may block. That
+ * is fine when called from ioctl context. When called from a file
+ * system, the FS can use memalloc_noio_save()/restore() or
+ * memalloc_nofs_save()/restore() if GFP_NOFS is required to avoid I/Os
+ * and/or FS re-entry.
+ */
+ rep->need_free_elements = alloc_elements;
+ rep->nr_elements = min(SD_ZBC_MAX_STORAGE_ELEMENTS, nr_elements);
+ rep->buf_size = ALIGN((rep->nr_elements + 1) * 32, SECTOR_SIZE);
+ rep->buf = kzalloc(rep->buf_size, GFP_KERNEL);
+ if (!rep->buf)
+ return -ENOMEM;
+
+ if (!alloc_elements) {
+ rep->elements = elements;
+ return 0;
+ }
+
+ rep->elements =
+ kzalloc_objs(struct blk_se, rep->nr_elements, GFP_KERNEL);
+ if (!rep->elements) {
+ kfree(rep->buf);
+ return -ENOMEM;
+ }
+
+ return 0;
+}
+
+static void sd_zbc_free_se_report(struct sd_zbc_se_report *rep)
+{
+ kfree(rep->buf);
+ if (rep->need_free_elements)
+ kfree(rep->elements);
+}
+
+static int sd_zbc_report_se(struct scsi_disk *sdkp,
+ struct sd_zbc_se_report *rep)
+{
+ struct scsi_device *sdp = sdkp->device;
+ const int timeout = sdp->request_queue->rq_timeout;
+ struct scsi_sense_hdr sshdr;
+ struct scsi_failure failure_defs[] = {
+ /* Allow up to retry 3 times */
+ {
+ .result = SCMD_FAILURE_RESULT_ANY,
+ .allowed = 3,
+ },
+ {}
+ };
+ struct scsi_failures failures = {
+ .failure_definitions = failure_defs,
+ };
+ const struct scsi_exec_args exec_args = {
+ .sshdr = &sshdr,
+ .failures = &failures,
+ };
+ unsigned char cmd[16];
+ unsigned int nr_descs;
+ int i, result;
+ u8 *desc;
+
+ memset(cmd, 0, 16);
+ cmd[0] = SERVICE_ACTION_IN_16;
+ cmd[1] = SAI_GET_PHYSICAL_ELEMENT_STATUS;
+ put_unaligned_be32(rep->buf_size, &cmd[10]);
+
+ result = scsi_execute_cmd(sdp, cmd, REQ_OP_DRV_IN,
+ rep->buf, rep->buf_size, timeout, 1, &exec_args);
+ if (result) {
+ if (result > 0) {
+ sd_print_result(sdkp, "GET PHYSICAL ELEMENT STATUS",
+ result);
+ if (scsi_sense_valid(&sshdr))
+ sd_print_sense_hdr(sdkp, &sshdr);
+ return -EIO;
+ }
+ return result;
+ }
+
+ nr_descs = get_unaligned_be32(&rep->buf[0]);
+ if (!nr_descs) {
+ sd_printk(KERN_ERR, sdkp,
+ "Invalid number of phys element descriptors\n");
+ return -EIO;
+ }
+ if (nr_descs > SD_ZBC_MAX_STORAGE_ELEMENTS) {
+ sd_printk(KERN_ERR, sdkp,
+ "Too many phys element descriptors\n");
+ return -EIO;
+ }
+
+ if (!rep->elements) {
+ rep->nr_reported_elements = nr_descs;
+ return 0;
+ }
+
+ nr_descs = get_unaligned_be32(&rep->buf[4]);
+ if (!nr_descs) {
+ sd_printk(KERN_ERR, sdkp,
+ "Invalid number of reported phys element descriptors\n");
+ return -EIO;
+ }
+
+ desc = &rep->buf[32];
+ for (i = 0; i < min(rep->nr_elements, nr_descs); i++, desc += 32)
+ sd_zbc_parse_storage_element(sdkp, desc, &rep->elements[i]);
+ rep->nr_reported_elements = i;
+
+ return 0;
+}
+
+static int sd_zbc_report_storage_elements(struct gendisk *disk,
+ struct blk_se *elements,
+ unsigned int *nr_elements)
+{
+ struct scsi_disk *sdkp = scsi_disk(disk);
+ struct sd_zbc_se_report rep;
+ int ret;
+
+ if (!sdkp->depop_report_supported)
+ return -EOPNOTSUPP;
+
+ ret = sd_zbc_alloc_se_report(elements, *nr_elements, false, &rep);
+ if (ret)
+ return ret;
+
+ ret = sd_zbc_report_se(sdkp, &rep);
+ if (!ret)
+ *nr_elements = rep.nr_reported_elements;
+
+ sd_zbc_free_se_report(&rep);
+
+ return ret;
+}
+
+static int sd_zbc_execute_storage_element_cmd(struct scsi_disk *sdkp,
+ u8 action, u32 element_id)
+{
+ struct scsi_device *sdp = sdkp->device;
+ const int timeout = sdp->request_queue->rq_timeout;
+ struct scsi_sense_hdr sshdr;
+ const struct scsi_exec_args exec_args = {
+ .sshdr = &sshdr,
+ };
+ unsigned char cmd[16];
+ const char *cmd_name;
+ int result;
+
+ memset(cmd, 0, sizeof(cmd));
+ cmd[0] = SERVICE_ACTION_IN_16;
+ cmd[1] = action;
+ if (action == SAI_REMOVE_ELEMENT_AND_MODIFY_ZONES) {
+ put_unaligned_be32(element_id, &cmd[10]);
+ cmd_name = "REMOVE ELEMENT AND MODIFY ZONES";
+ } else {
+ cmd_name = "RESTORE ELEMENTS AND REBUILD";
+ }
+
+ result = scsi_execute_cmd(sdp, cmd, REQ_OP_DRV_IN, NULL, 0,
+ timeout, 1, &exec_args);
+ if (result) {
+ sd_printk(KERN_ERR, sdkp, "%s failed\n", cmd_name);
+ if (result > 0) {
+ if (scsi_sense_valid(&sshdr))
+ sd_print_sense_hdr(sdkp, &sshdr);
+ result = -EIO;
+ }
+ return result;
+ }
+
+ return 0;
+}
+
+/*
+ * Wait for the completion of a storage element remove operation. Note that the
+ * standards advise to use pollable sense data with the REQUEST SENSE command to
+ * detect if a remove operation is in progress. However, since REQUEST SENSE
+ * will return sense data only for the last executed command, and since we are
+ * not blocking commands, this method would be unreliable as we may end up
+ * seeing sense data (or no sense data) for a command that is not the REMOVE
+ * ELEMENT AND MODIFY ZONES we issued.
+ */
+static int sd_zbc_wait_for_se_removal(struct scsi_disk *sdkp,
+ struct sd_zbc_se_report *rep,
+ unsigned int element_id)
+{
+ bool element_checked, element_removed;
+ int ret, in_progress;
+ unsigned int i;
+
+ /*
+ * Poll the storage elements health to see that we get the element we
+ * are interested in and that its removal completed. Since we do not
+ * know how long the operation may take, we cannot have a timeout on
+ * this loop. So check for fatal signals so that the user can terminate
+ * this loop if the device misbehaves.
+ */
+ do {
+ /* Let the operation proceed and check. */
+ msleep(1000);
+
+ ret = sd_zbc_report_se(sdkp, rep);
+ if (ret)
+ return ret;
+
+ element_checked = false;
+ element_removed = false;
+ in_progress = 0;
+ for (i = 0; i < rep->nr_elements; i++) {
+ if (rep->elements[i].id == element_id)
+ element_checked = true;
+ switch (rep->elements[i].status) {
+ case BLK_SE_STS_REMOVED:
+ if (rep->elements[i].id == element_id)
+ element_removed = true;
+ break;
+ case BLK_SE_STS_REMOVE_IN_PROGRESS:
+ in_progress++;
+ break;
+ case BLK_SE_STS_REMOVE_ERROR:
+ if (rep->elements[i].id == element_id)
+ return -EIO;
+ break;
+ default:
+ break;
+ }
+ }
+
+ if (!element_checked)
+ return -EIO;
+ if (element_removed && !in_progress)
+ return 0;
+ } while (!fatal_signal_pending(current));
+
+ return -EINTR;
+}
+
+static int sd_zbc_remove_storage_element(struct gendisk *disk,
+ unsigned int element_id)
+{
+ struct scsi_disk *sdkp = scsi_disk(disk);
+ struct sd_zbc_se_report rep;
+ int ret;
+
+ if (!sdkp->depop_modify_zones_supported)
+ return -EOPNOTSUPP;
+
+ /*
+ * Get the resources we need to prepare for waiting for the operation to
+ * complete.
+ */
+ ret = sd_zbc_alloc_se_report(NULL, disk->nr_storage_elements,
+ true, &rep);
+ if (ret)
+ return ret;
+
+ /*
+ * Execute the remove command and wait for the element removal operation
+ * to complete.
+ */
+ ret = sd_zbc_execute_storage_element_cmd(sdkp,
+ SAI_REMOVE_ELEMENT_AND_MODIFY_ZONES, element_id);
+ if (!ret)
+ ret = sd_zbc_wait_for_se_removal(sdkp, &rep, element_id);
+ if (!ret)
+ sd_printk(KERN_NOTICE, sdkp,
+ "Removed storage element %u\n", element_id);
+
+ sd_zbc_free_se_report(&rep);
+
+ return ret;
+}
+
+/*
+ * Wait for the completion of the restoration of removed storage elements.
+ * See the note for sd_zbc_wait_for_se_removal().
+ */
+static int sd_zbc_wait_for_se_restore(struct scsi_disk *sdkp,
+ struct sd_zbc_se_report *rep)
+{
+ int ret, in_progress;
+ unsigned int i;
+
+ /*
+ * Poll the storage elements health to see if we have any element still
+ * being restored or if we got errors. Since we do not know how long the
+ * operation may take, we cannot have a timeout on this loop. So check
+ * for fatal signals so that the user can terminate this retry loop if
+ * the device misbehaves.
+ */
+ do {
+ /* Let the operation proceed and check. */
+ msleep(1000);
+
+ ret = sd_zbc_report_se(sdkp, rep);
+ if (ret)
+ return ret;
+
+ in_progress = 0;
+ for (i = 0; i < rep->nr_elements; i++) {
+ switch (rep->elements[i].status) {
+ case BLK_SE_STS_RESTORE_IN_PROGRESS:
+ in_progress++;
+ break;
+ case BLK_SE_STS_REMOVED:
+ case BLK_SE_STS_RESTORE_ERROR:
+ if (rep->elements[i].restore_allowed)
+ ret = -EIO;
+ break;
+ default:
+ break;
+ }
+ }
+
+ if (!in_progress)
+ return ret;
+ } while (!fatal_signal_pending(current));
+
+ return -EINTR;
+}
+
+static int sd_zbc_restore_storage_elements(struct gendisk *disk)
+{
+ struct scsi_disk *sdkp = scsi_disk(disk);
+ struct sd_zbc_se_report rep;
+ int ret;
+
+ if (!sdkp->depop_restore_supported)
+ return -EOPNOTSUPP;
+
+ /*
+ * Get the resources we need to prepare for waiting for the operation to
+ * complete.
+ */
+ ret = sd_zbc_alloc_se_report(NULL, disk->nr_storage_elements,
+ true, &rep);
+ if (ret)
+ return ret;
+
+ /*
+ * Execute the restore command and wait for the elements restoration
+ * operation to complete.
+ */
+ ret = sd_zbc_execute_storage_element_cmd(sdkp,
+ SAI_RESTORE_ELEMENTS_AND_REBUILD, 0);
+ if (!ret)
+ ret = sd_zbc_wait_for_se_restore(sdkp, &rep);
+ if (!ret)
+ sd_printk(KERN_NOTICE, sdkp, "Restored storage elements\n");
+
+ sd_zbc_free_se_report(&rep);
+
+ return ret;
+}
+
+const struct blk_se_ops sd_zbc_se_ops = {
+ .report_elements = sd_zbc_report_storage_elements,
+ .remove_element = sd_zbc_remove_storage_element,
+ .restore_elements = sd_zbc_restore_storage_elements,
+};
+
/*
* Call blk_revalidate_disk_zones() if any of the zoned disk properties have
* changed that make it necessary to call that function. Called by
@@ -545,6 +1041,7 @@ int sd_zbc_revalidate_zones(struct scsi_disk *sdkp)
u32 zone_blocks = sdkp->early_zone_info.zone_blocks;
unsigned int nr_zones = sdkp->early_zone_info.nr_zones;
unsigned int flags;
+ bool print_zones;
int ret;
/*
@@ -554,11 +1051,22 @@ int sd_zbc_revalidate_zones(struct scsi_disk *sdkp)
if (!blk_queue_is_zoned(q))
return 0;
+ /*
+ * If the zone size and number of zones has not changed, and the disk
+ * does not support depopulating heads, skip the rather slow call to
+ * blk_revalidate_disk_zones().
+ */
if (sdkp->zone_info.zone_blocks == zone_blocks &&
sdkp->zone_info.nr_zones == nr_zones &&
- disk->nr_zones == nr_zones)
+ disk->nr_zones == nr_zones &&
+ !sdkp->depop_modify_zones_supported)
return 0;
+ print_zones =
+ sdkp->zone_info.zone_blocks != zone_blocks ||
+ sdkp->zone_info.nr_zones != nr_zones ||
+ disk->nr_zones != nr_zones;
+
sdkp->zone_info.zone_blocks = zone_blocks;
sdkp->zone_info.nr_zones = nr_zones;
@@ -571,7 +1079,8 @@ int sd_zbc_revalidate_zones(struct scsi_disk *sdkp)
return ret;
}
- sd_zbc_print_zones(sdkp);
+ if (print_zones)
+ sd_zbc_print_zones(sdkp);
return 0;
}
@@ -610,6 +1119,9 @@ int sd_zbc_read_zones(struct scsi_disk *sdkp, struct queue_limits *lim,
sdkp->device->use_10_for_rw = 0;
sdkp->device->use_16_for_sync = 1;
+ /* Check if storage element depopulation is supported. */
+ sd_zbc_check_depop_supported(sdkp, buf);
+
/* Check zoned block device characteristics (unconstrained reads) */
ret = sd_zbc_check_zoned_characteristics(sdkp, buf);
if (ret)
diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index 215753171cef0e..63c6e43ecdda30 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -189,6 +189,11 @@ struct gendisk {
struct disk_events *ev;
#ifdef CONFIG_BLK_DEV_ZONED
+ /*
+ * Total number of storage elements of the disk. This is always 0 for
+ * disks that do not support storage element management.
+ */
+ unsigned int nr_storage_elements;
/*
* Zoned block device information. Reads of this information must be
* protected with blk_queue_enter() / blk_queue_exit() or by holding a
@@ -940,6 +945,9 @@ static inline unsigned int disk_zone_capacity(struct gendisk *disk,
return disk->last_zone_capacity;
return disk->zone_capacity;
}
+
+bool disk_zone_is_offline(struct gendisk *disk, sector_t sector);
+
static inline unsigned int bdev_zone_capacity(struct block_device *bdev,
sector_t pos)
{
@@ -948,12 +956,25 @@ static inline unsigned int bdev_zone_capacity(struct block_device *bdev,
bool bdev_zone_is_seq(struct block_device *bdev, sector_t sector);
+int bdev_report_storage_elements(struct block_device *bdev,
+ struct blk_se *elements,
+ unsigned int *nr_elements);
+int bdev_remove_storage_element(struct block_device *bdev,
+ unsigned int element_id);
+int bdev_restore_storage_elements(struct block_device *bdev);
+
#else /* CONFIG_BLK_DEV_ZONED */
static inline unsigned int disk_nr_zones(struct gendisk *disk)
{
return 0;
}
+static inline bool disk_zone_is_offline(struct gendisk *disk,
+ sector_t sector)
+{
+ return false;
+}
+
static inline bool bdev_zone_is_seq(struct block_device *bdev, sector_t sector)
{
return false;
@@ -968,6 +989,25 @@ static inline bool blk_zone_plug_bio(struct bio *bio, unsigned int nr_segs)
{
return false;
}
+
+static inline int bdev_report_storage_elements(struct block_device *bdev,
+ struct blk_se *elements,
+ unsigned int *nr_elements)
+{
+ return -EOPNOTSUPP;
+}
+
+static inline int bdev_remove_storage_element(struct block_device *bdev,
+ unsigned int element_id)
+{
+ return -EOPNOTSUPP;
+}
+
+static inline int bdev_restore_storage_elements(struct block_device *bdev)
+{
+ return -EOPNOTSUPP;
+}
+
#endif /* CONFIG_BLK_DEV_ZONED */
static inline unsigned int bdev_nr_zones(struct block_device *bdev)
@@ -1572,6 +1612,29 @@ enum blk_unique_id {
BLK_UID_NAA = 3,
};
+/**
+ * struct blk_se_ops - Storage element management operations.
+ * @report_elements: Report at most nr_elements storage elements of the
+ * specified disk. If the elements argument is NULL, only
+ * report the number of storage elements of the target disk.
+ * @remove_element: Remove (depopulate) the specified storage element from the
+ * target disk. This is a blocking operation called with
+ * zone_revalidate_mutex held. This callback must return only
+ * after the remove operation completes or an error occurs.
+ * @restore_elements: Restore (re-populate), if possible, all removed storage
+ * elements of the target disk. This operation is called with
+ * zone_revalidate_mutex held. This callback must return only
+ * after the restoration operation completes or an error
+ * occurs.
+ */
+struct blk_se_ops {
+ int (*report_elements)(struct gendisk *disk,
+ struct blk_se *elements,
+ unsigned int *nr_elements);
+ int (*remove_element)(struct gendisk *disk, unsigned int element_id);
+ int (*restore_elements)(struct gendisk *disk);
+};
+
struct block_device_operations {
void (*submit_bio)(struct bio *bio);
int (*poll_bio)(struct bio *bio, struct io_comp_batch *iob,
@@ -1601,6 +1664,7 @@ struct block_device_operations {
struct dma_buf_io_ctx *);
struct module *owner;
const struct pr_ops *pr_ops;
+ const struct blk_se_ops *se_ops;
/*
* Special callback for probing GPT entry at a given sector.
diff --git a/include/scsi/scsi_proto.h b/include/scsi/scsi_proto.h
index 9c7d31aa6bae59..f290a214e3e06e 100644
--- a/include/scsi/scsi_proto.h
+++ b/include/scsi/scsi_proto.h
@@ -408,9 +408,11 @@ enum scsi_phys_element_type {
/* SCSI physical element health. */
enum scsi_phys_element_health {
SCSI_PHYS_ELEM_HEALTH_NOT_REPORTED = 0x00,
- SCSI_PHYS_ELEM_HEALTH_WITHIN_SPEC_LIMITS = 0x01,
+ SCSI_PHYS_ELEM_HEALTH_WITHIN_SPEC_LIMITS_MIN = 0x01,
+ SCSI_PHYS_ELEM_HEALTH_WITHIN_SPEC_LIMITS_MAX = 0x63,
SCSI_PHYS_ELEM_HEALTH_AT_SPEC_LIMITS = 0x64,
- SCSI_PHYS_ELEM_HEALTH_OUTSIDE_SPEC_LIMITS = 0x65,
+ SCSI_PHYS_ELEM_HEALTH_OUTSIDE_SPEC_LIMITS_MIN = 0x65,
+ SCSI_PHYS_ELEM_HEALTH_OUTSIDE_SPEC_LIMITS_MAX = 0xCF,
SCSI_PHYS_ELEM_HEALTH_DEPOP_REVOKE_ERR = 0xFB,
SCSI_PHYS_ELEM_HEALTH_DEPOP_REVOKE_IN_PROGRESS = 0xFC,
SCSI_PHYS_ELEM_HEALTH_DEPOP_ERR = 0xFD,
diff --git a/include/uapi/linux/blkzoned.h b/include/uapi/linux/blkzoned.h
index 66383612096678..6463cdec8e864e 100644
--- a/include/uapi/linux/blkzoned.h
+++ b/include/uapi/linux/blkzoned.h
@@ -208,4 +208,110 @@ struct blk_zone_range {
#define BLKFINISHZONE _IOW(0x12, 136, struct blk_zone_range)
#define BLKREPORTZONEV2 _IOWR(0x12, 142, struct blk_zone_report)
+/**
+ * enum blk_se_type - Type of a zoned device storage element.
+ *
+ * @BLK_SE_TYPE_RDWR: The storage element handles both reads and writes.
+ * @BLK_SE_TYPE_READ: The storage element handles reads only.
+ * @BLK_SE_TYPE_WRITE: The storage element handles writes only.
+ * @BLK_SE_TYPE_UNKNOWN: The storage element type is not known.
+ */
+enum blk_se_type {
+ BLK_SE_TYPE_RDWR = 0x01,
+ BLK_SE_TYPE_READ = 0x02,
+ BLK_SE_TYPE_WRITE = 0x03,
+ BLK_SE_TYPE_UNKNOWN = 0xFF,
+};
+
+/**
+ * enum blk_se_status - Status of a zoned device storage element.
+ *
+ * @BLK_SE_STS_OK: The storage element is operating normally.
+ * @BLK_SE_STS_OK_LIMIT: The storage element is operating at the manufacturer
+ * specifications limit.
+ * @BLK_SE_STS_DEGRADED: The storage element has degraded and is not operating
+ * normally.
+ * @BLK_SE_STS_REMOVE_IN_PROGRESS: The storage element is being removed.
+ * @BLK_SE_STS_REMOVE_ERROR: The storage element removal failed.
+ * @BLK_SE_STS_RESTORE_IN_PROGRESS: The storage element is being restored.
+ * @BLK_SE_STS_RESTORE_ERROR: The storage element restoration failed.
+ * @BLK_SE_STS_REMOVED: The storage element was removed.
+ * @BLK_SE_STS_UNKNOWN: The storage element status is unknown.
+ */
+enum blk_se_status {
+ BLK_SE_STS_OK = 0x01,
+ BLK_SE_STS_OK_LIMIT = 0x02,
+ BLK_SE_STS_DEGRADED = 0x03,
+ BLK_SE_STS_REMOVE_IN_PROGRESS = 0x04,
+ BLK_SE_STS_REMOVE_ERROR = 0x05,
+ BLK_SE_STS_RESTORE_IN_PROGRESS = 0x06,
+ BLK_SE_STS_RESTORE_ERROR = 0x07,
+ BLK_SE_STS_REMOVED = 0x08,
+ BLK_SE_STS_UNKNOWN = 0xFF,
+};
+
+/**
+ * struct blk_se - Zoned device storage element descriptor.
+ *
+ * @id: The ID of the element (cannot be 0).
+ * @paired_id: The ID of the paired element for an element that is not
+ * of type BLK_SE_TYPE_RDWR.
+ * @nr_zones: The number of zones that the storage element handles.
+ * @type: The type of the storage element (enum blk_se_type).
+ * @status: The health status of the storage element
+ * (enum blk_se_status).
+ * @restore_allowed: Indicate if the storage element can be restored.
+ * @reserved: unused field to align this structure size to 32B
+ */
+struct blk_se {
+ __u32 id;
+ __u32 paired_id;
+ __u64 nr_zones;
+ __u8 type;
+ __u8 status;
+ __u8 restore_allowed;
+ __u8 reserved[13];
+};
+
+/**
+ * struct blk_se_report - Zoned device storage element descriptor report for the
+ * BLKREPORTSTORELEMS ioctl.
+ *
+ * @nr_elements: on input, this indicates the size of the @elements array. On
+ * output, this indicates the maximum number of storage elements
+ * of the device. If this field specifies 0 on input, the array
+ * @elements is not filled and the maximum number of storage
+ * elements of the device returned.
+ * @nr_reported_elements: output only to indicate the number of storage element
+ * descriptors filled in the array @elements.
+ * @reserved: reserved space for future use and structure field alignment.
+ * @elements: array of storage element descriptors with a maximum number of
+ * entries specified by @nr_elements on input and by
+ * @nr_reported_elements on output.
+ */
+struct blk_se_report {
+ __u32 nr_elements;
+ __u32 nr_reported_elements;
+ __u64 reserved;
+ struct blk_se elements[];
+};
+
+/**
+ * Zoned block device storage element management ioctl's:
+ *
+ * @BLKREPORTSTORELEMS: Get the number of storage elements of a device and
+ * the device storage element descriptors.
+ * @BLKREMOVESTORELEM: Remove (depopulate) one storage element of a device. This
+ * operation will flush the target device write cache and
+ * invalidate all cached data. So the caller must ensure
+ * that all dirty data written with buffered IOs are written
+ * back first to avoid any data loss.
+ * @BLKRESTORESTORELEMS: Restore (repopulate if possible) all storage elements
+ * that have been removed. This is a destructive operation
+ * equivalent to reformatting the target device.
+ */
+#define BLKREPORTSTORELEMS _IOWR(0x12, 143, struct blk_se_report)
+#define BLKREMOVESTORELEM _IOW(0x12, 144, __u32)
+#define BLKRESTORESTORELEMS _IO(0x12, 145)
+
#endif /* _UAPI_BLKZONED_H */
diff --git a/include/uapi/linux/fs.h b/include/uapi/linux/fs.h
index a46c33692aa21a..5b6f8248a4799b 100644
--- a/include/uapi/linux/fs.h
+++ b/include/uapi/linux/fs.h
@@ -309,6 +309,7 @@ struct file_attr {
/* 130-136 and 142 are used by zoned block device ioctls (uapi/linux/blkzoned.h) */
/* 137-141 are used by blk-crypto ioctls (uapi/linux/blk-crypto.h) */
#define BLKTRACESETUP2 _IOWR(0x12, 142, struct blk_user_trace_setup2)
+/* 143-145 are used by storage element management for zoned block devices. */
#define BMAP_IOCTL 1 /* obsolete - kept for compatibility */
#define FIBMAP _IO(0x00,1) /* bmap access */
|
| 35/3 | 2026/11/12 05:12 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 34087 output: 35 thoughts: 4499 Thoughts: **KMSAN Evaluation: blk-zoned ioctl Report** Okay, let's break this down. My goal is to determine if this patch series warrants a dedicated KMSAN fuzzing session. The primary area of concern is the new `BLKREPORTSTORELEMS` ioctl. My gut feeling leans towards "no," but I need to be thorough. First, I established the ground rules. KASAN is my first line of defense; if a bug is KASAN-detectable, KMSAN isn't needed. KMSAN is for catching reads of uninitialized memory. Only if the patch introduces or exposes risks associated with uninitialized memory will I mark `NeedsKMSAN=true`. Specifically, I need to look for uninitialized memory risks related to kernel structures being sent to user space (via `copy_to_user` etc), conditional logic based on potentially uninitialized variables, allocation of complex data structures with uninitialized fields, or bounds check logic that allows access of uninitialized bytes. I carefully scrutinized the code. The `blkdev_ioctl_report_storage_elements` function calls `kzalloc_flex` to allocate the `rep` structure. Importantly, `kzalloc_flex` *zeroes* the allocated memory. However, the code then copies data *from user space* into the `rep` structure using `copy_from_user`. This overwrites several fields of the `blk_se_report` struct (namely, `nr_elements`, `nr_reported_elements` and `reserved`). After that, `nr_elements` gets set to `disk->nr_storage_elements` and `nr_reported_elements` gets set to the min of user-provided and disk sizes. Subsequently, `bdev_report_storage_elements` is called, which calls into the block driver's `report_elements` function. I inspected two implementations: `zloop_report_elements` in `zloop.c` and `sd_zbc_report_storage_elements` in `sd_zbc.c`. In `zloop.c`, `zlo->elements` is initialized using `kzalloc_objs`, which zeroes. In the `zloop` case, `memcpy` is used to copy the values. So, zero-initialized memory is written, then overwritten. The SCSI driver (`sd_zbc.c`) takes a different approach. It calls `sd_zbc_alloc_se_report`, which allocates a `rep->buf` using `kzalloc`, so it is zeroed. Then the SCSI command is executed, and then `sd_zbc_parse_storage_element` is called for the storage elements and fills the `element` with `memset`, so it is zeroed, no matter if there were a partial result. Therefore, despite allocating a buffer and receiving data from the device, any data not populated is implicitly zero. I reviewed the structures involved: `struct blk_se` and `struct blk_se_report`. Neither has padding holes. The critical thing here is that regardless of the driver, the `elements` array within the report *always* begins with either zeroed memory or, more explicitly, zeroed memory in which only some fields may be overwritten. The `copy_to_user` copies from the `rep` structure, and the number of elements written will be bounded by `rep->nr_reported_elements`, and the memory has been zeroed (or explicitly filled by the drivers), so there is no uninitialized memory read or copied to user. I am confident that there are no uninitialized reads here. Therefore, this patch does *not* introduce a KMSAN risk. I will mark `NeedsKMSAN=false` and move on. |
| 36/3 | 2026/11/12 05:13 | tool | grepper |
0mArgs:{
"Expression": "zloop_ctl_write",
"PathPrefixes": [
"drivers/block/zloop.c"
]
}Results: {
"Output": "drivers/block/zloop.c=2364=static struct zloop_ctl_op {\n--\ndrivers/block/zloop.c-2373-\ndrivers/block/zloop.c:2374:static ssize_t zloop_ctl_write(struct file *file, const char __user *ubuf,\ndrivers/block/zloop.c-2375-\t\t\t size_t count, loff_t *pos)\n--\ndrivers/block/zloop.c=2474=static const struct file_operations zloop_ctl_fops = {\n--\ndrivers/block/zloop.c-2477-\t.release\t= zloop_ctl_release,\ndrivers/block/zloop.c:2478:\t.write\t\t= zloop_ctl_write,\ndrivers/block/zloop.c-2479-\t.read\t\t= seq_read,\n"
} |
| 37/3 | 2026/11/12 05:13 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 39649 output: 37 thoughts: 22 Thoughts: Here's a summary of my thought process: **Diving into the `zloop.c` Driver** Okay, time to get my hands dirty. I need to take a look at the `drivers/block/zloop.c` file. I'm specifically interested in what's happening around line 2374. This section likely deals with a particular function or mechanism within the zloop driver, and I need to understand its behavior in detail. I'm anticipating some complex interactions within the block layer, given the nature of zloop as a loopback device, but line 2374 will likely guide me to what I need to understand. |
| 38/3 | 2026/11/12 05:13 | tool | read-file |
0mArgs:{
"File": "drivers/block/zloop.c",
"FirstLine": 2370,
"LineCount": 60
}Results: {
"Contents": "2370:\t\t{ ZLOOP_CTL_DEGRADE_ELEMENT,\t\"degrade_element\" },\n2371:\t\t{ -1,\tNULL },\n2372:\t};\n2373:\t\n2374:\tstatic ssize_t zloop_ctl_write(struct file *file, const char __user *ubuf,\n2375:\t\t\t\t size_t count, loff_t *pos)\n2376:\t{\n2377:\t\tstruct zloop_options opts = { };\n2378:\t\tstruct zloop_ctl_op *op;\n2379:\t\tconst char *buf, *opts_buf;\n2380:\t\tint i, ret;\n2381:\t\n2382:\t\tif (count \u003e PAGE_SIZE)\n2383:\t\t\treturn -ENOMEM;\n2384:\t\n2385:\t\tbuf = memdup_user_nul(ubuf, count);\n2386:\t\tif (IS_ERR(buf))\n2387:\t\t\treturn PTR_ERR(buf);\n2388:\t\n2389:\t\tfor (i = 0; i \u003c ARRAY_SIZE(zloop_ctl_ops); i++) {\n2390:\t\t\top = \u0026zloop_ctl_ops[i];\n2391:\t\t\tif (!op-\u003ename) {\n2392:\t\t\t\tpr_err(\"Invalid operation\\n\");\n2393:\t\t\t\tret = -EINVAL;\n2394:\t\t\t\tgoto out;\n2395:\t\t\t}\n2396:\t\t\tif (!strncmp(buf, op-\u003ename, strlen(op-\u003ename)))\n2397:\t\t\t\tbreak;\n2398:\t\t}\n2399:\t\n2400:\t\tif (count \u003c= strlen(op-\u003ename))\n2401:\t\t\topts_buf = NULL;\n2402:\t\telse\n2403:\t\t\topts_buf = buf + strlen(op-\u003ename);\n2404:\t\n2405:\t\tret = zloop_parse_options(\u0026opts, opts_buf);\n2406:\t\tif (ret) {\n2407:\t\t\tpr_err(\"Failed to parse options\\n\");\n2408:\t\t\tgoto out;\n2409:\t\t}\n2410:\t\n2411:\t\tswitch (op-\u003ecode) {\n2412:\t\tcase ZLOOP_CTL_ADD:\n2413:\t\t\tret = zloop_ctl_add(\u0026opts);\n2414:\t\t\tbreak;\n2415:\t\tcase ZLOOP_CTL_REMOVE:\n2416:\t\t\tret = zloop_ctl_remove(\u0026opts);\n2417:\t\t\tbreak;\n2418:\t\tcase ZLOOP_CTL_DEGRADE_ELEMENT:\n2419:\t\t\tret = zloop_ctl_degrade_element(\u0026opts);\n2420:\t\t\tbreak;\n2421:\t\tdefault:\n2422:\t\t\tpr_err(\"Invalid operation\\n\");\n2423:\t\t\tret = -EINVAL;\n2424:\t\t\tgoto out;\n2425:\t\t}\n2426:\t\n2427:\tout:\n2428:\t\tkfree(opts.base_dir);\n2429:\t\tkfree(buf);\n"
} |
| 39/3 | 2026/11/12 05:13 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 40682 output: 21 thoughts: 2505 Thoughts: **Deep Dive into ZBC Storage Element Removal and Zone Handling** Okay, so I've been dissecting this ZBC code, and here's the breakdown of my thought process. First, `zloop_options` is initialized, and its various configuration options are populated in `zloop_parse_options`. All seems straightforward there. Now, I'm checking `sd_zbc.c`, specifically around storage element removal. I'm focusing on potential uninitialized variables, starting with `sd_zbc_report_storage_elements`. It allocates a report struct (`sd_zbc_se_report rep`). I noted that `rep->nr_reported_elements` isn't initialized explicitly in the allocation, which immediately raises a red flag. Where is it being set or used? I tracked down the use of `rep->nr_reported_elements`. It's initialized in `sd_zbc_report_se`, which is called by several functions, most notably by `sd_zbc_remove_storage_element` during the element removal process. `sd_zbc_report_se` seems to be responsible for parsing the storage element report and updating the `rep->elements` array. It reads the number of descriptors, checks the array's size, and iterates through descriptors to populate the `rep->elements` with information using `sd_zbc_parse_storage_element`. Crucially, it sets `rep->nr_reported_elements` to the number of *reported* elements. A critical point: in the loop in `sd_zbc_wait_for_se_removal`, the loop iterates up to `rep->nr_elements`. However, the report may have a *different* number of actual descriptors (`nr_descs`) retrieved from the device, and the loop in `sd_zbc_report_se` parses up to `min(rep->nr_elements, nr_descs)`. Here is where the potential issue arises if `nr_descs < rep->nr_elements`! I'm thinking: What if there is a mismatch? What are the values of `rep->elements` beyond what was parsed? It appears that `rep->elements` is allocated with `kzalloc_objs`, which means that it's zero-initialized. So, at the very least, it's not reading uninitialized memory, but it's *still* critical to understand how this can affect the removal logic. In the crucial loop in `sd_zbc_wait_for_se_removal` the code *does* read beyond `nr_descs`, but the zero initialization means that it will never match `element_id`. In `sd_zbc_wait_for_se_restore` it is also zero initialized. In `sd_setup_read_write_cmnd`, I see a zone condition check (`disk_zone_is_offline`). The code's comment implies a race condition between restoration and zone revalidation, which I need to analyze better, but it's not the critical path here. This function uses `disk_zone_is_offline`, and I can see its implementation which relies on `disk_zone_get_cond`, and I'm currently looking for the implementation of `disk_zone_get_cond` in the code base. |
| 40/3 | 2026/11/12 05:14 | tool | grepper |
0mArgs:{
"Expression": "disk_zone_get_cond"
}Results: {
"Output": "block/blk-zoned.c=250=static inline u8 disk_zone_get_state(struct gendisk *disk, sector_t sector)\n--\nblock/blk-zoned.c-265-\nblock/blk-zoned.c:266:static enum blk_zone_cond disk_zone_get_cond(struct gendisk *disk,\nblock/blk-zoned.c-267-\t\t\t\t\t sector_t sector)\n--\nblock/blk-zoned.c=281=static inline bool disk_zone_is_offline_or_readonly(struct gendisk *disk,\n--\nblock/blk-zoned.c-283-{\nblock/blk-zoned.c:284:\tenum blk_zone_cond cond = disk_zone_get_cond(disk, sector);\nblock/blk-zoned.c-285-\n--\nblock/blk-zoned.c=305=bool disk_zone_is_offline(struct gendisk *disk, sector_t sector)\n--\nblock/blk-zoned.c-309-\nblock/blk-zoned.c:310:\treturn disk_zone_get_cond(disk, sector) == BLK_ZONE_COND_OFFLINE;\nblock/blk-zoned.c-311-}\n"
} |
| 41/3 | 2026/11/12 05:14 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 44236 output: 35 thoughts: 33 Thoughts: Okay, let's dive into this. Here's how I'd approach this task, as if these were my internal thoughts: **Analyzing Zoned Block Device State Retrieval** Right, so we're talking about zoned block devices – interesting, always a fascinating area. My first move is to zero in on those key functions: `disk_zone_get_state` and `disk_zone_get_cond`, both located within `block/blk-zoned.c`. I need to understand how the state and condition of a zone are being retrieved. I'm assuming there's some sort of low-level interaction going on here, interacting with the hardware. I will need to understand what the data structure returned by each function represents and how these functions handle error conditions. Are they using the block device's metadata structures directly? Are there any locking mechanisms involved? Potential race conditions? Any assumptions about the underlying hardware's capabilities that I need to be aware of? My intuition tells me that getting the zone's state and condition is likely a critical operation, probably involved in scheduling I/O to the zoned device. From here I can probably move on to a higher level look at the code and understand what each of these functions is responsible for. |
| 42/3 | 2026/11/12 05:14 | tool | read-file |
0mArgs:{
"File": "block/blk-zoned.c",
"FirstLine": 245,
"LineCount": 40
}Results: {
"Contents": " 245:\t\t\tblk_zstate_set(zones_state, disk-\u003enr_zones, zno, cond,\n 246:\t\t\t\t blk_zstate_flags(zones_state[zno]));\n 247:\t\trcu_read_unlock();\n 248:\t}\n 249:\t\n 250:\tstatic inline u8 disk_zone_get_state(struct gendisk *disk, sector_t sector)\n 251:\t{\n 252:\t\tunsigned int zno = disk_zone_no(disk, sector);\n 253:\t\tu8 *zones_state, zs;\n 254:\t\n 255:\t\trcu_read_lock();\n 256:\t\tzones_state = rcu_dereference(disk-\u003ezones_state);\n 257:\t\tif (likely(zones_state \u0026\u0026 zno \u003c disk-\u003enr_zones))\n 258:\t\t\tzs = zones_state[zno];\n 259:\t\telse\n 260:\t\t\tzs = BLK_ZFLAG_CONV;\n 261:\t\trcu_read_unlock();\n 262:\t\n 263:\t\treturn zs;\n 264:\t}\n 265:\t\n 266:\tstatic enum blk_zone_cond disk_zone_get_cond(struct gendisk *disk,\n 267:\t\t\t\t\t\t sector_t sector)\n 268:\t{\n 269:\t\tu8 zs = disk_zone_get_state(disk, sector);\n 270:\t\n 271:\t\treturn blk_zstate_to_zone_cond(zs);\n 272:\t}\n 273:\t\n 274:\tstatic inline bool\n 275:\tdisk_zone_cond_is_offline_or_readonly(enum blk_zone_cond cond)\n 276:\t{\n 277:\t\treturn cond == BLK_ZONE_COND_READONLY ||\n 278:\t\t\tcond == BLK_ZONE_COND_OFFLINE;\n 279:\t}\n 280:\t\n 281:\tstatic inline bool disk_zone_is_offline_or_readonly(struct gendisk *disk,\n 282:\t\t\t\t\t\t\t sector_t sector)\n 283:\t{\n 284:\t\tenum blk_zone_cond cond = disk_zone_get_cond(disk, sector);\n"
} |
| 43/3 | 2026/11/12 05:14 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 45185 output: 29 thoughts: 2087 Thoughts: **Analysis of `blkdev_ioctl_report_storage_elements` and Related Functions**
Alright, let's break down this code piece by piece. First, `disk_zone_is_offline` safely handles RCU protected `disk->zones_state`, that's fine. Now, the `blkdev_ioctl_report_storage_elements` function is the core of this investigation. It reads the `disk->nr_storage_elements` value, allocating a report structure `rep` with `kzalloc_flex` assuming maximum number of elements. Then, data is copied from user space into `rep`, with basic checks for reserved bits. I need to pay close attention to how `rep->nr_reported_elements` is used.
I'm thinking, what happens if `bdev_report_storage_elements` modifies `rep->nr_reported_elements`? This is where it gets interesting. Inside `bdev_report_storage_elements`, `nr_elements` is modified. The key is the potential for the driver's `report_elements` function to increase the reported number of elements beyond what was initially allocated. Specifically, if the driver sets `*nr_elements` to a value *larger* than `disk->nr_storage_elements`.
If that happens, `copy_to_user` in the original function would be attempting an out-of-bounds read! This should be caught by KASAN! Good. But, what if the driver correctly reports within the allocated space but *doesn't* fill all elements? `kzalloc_flex` zero-initializes the allocated memory, right? So, even if the driver doesn't fill the memory, it's zeroed memory, which KMSAN will see as initialized, which is correct.
I'll check the buffer in the SCSI driver: `sd_zbc.c`, `sd_zbc_report_se`: `rep->buf = kzalloc(rep->buf_size, GFP_KERNEL);` followed by `scsi_execute_cmd(..., rep->buf, ...)`... I need to verify that KMSAN handles SCSI commands correctly. Does `scsi_execute_cmd` itself initialize the buffer? It's allocated with `kzalloc`, so shadow memory is initialized! Also, `sd_zbc_parse_storage_element` uses `memset`, which also guarantees zero initialization.
Now to other functions:
In `zloop_report_zones`, there's just a simple code rearrangement, moving the `spin_unlock`.
In `disk_revalidate_zones`, all is initialized to { }.
In `blkdev_ioctl_remove_storage_element`, the `element_id` is populated by `get_user`, that's simple.
Lastly, in `disk_init_storage_element_mgmt`, there's a vital bit: `ret = disk->fops->se_ops->report_elements(disk, NULL, &disk->nr_storage_elements);` and if that fails, `disk->nr_storage_elements` is set to 0. But what if it *doesn't* set `*nr_elements`? This needs deeper investigation, I need to check how `gendisk` is allocated to check initial values. Let's look at `alloc_disk`.
|
| 44/3 | 2026/11/12 05:14 | tool | grepper |
0mArgs:{
"Expression": "kzalloc.*struct gendisk",
"PathPrefixes": [
"block/"
]
}Results: {
"Output": "block/genhd.c=1458=struct gendisk *__alloc_disk_node(struct request_queue *q, int node_id,\n--\nblock/genhd.c-1462-\nblock/genhd.c:1463:\tdisk = kzalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);\nblock/genhd.c-1464-\tif (!disk)\n"
} |
| 45/3 | 2026/11/12 05:14 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 48130 output: 35 thoughts: 1512 Thoughts: **Analyzing Storage Element Reporting in Block Layer** Okay, so I'm looking at how storage element reporting is handled, particularly in the context of `gendisk` and potentially ZBC (Zoned Block Commands) devices, and I'm seeing a possible area of concern, though it seems okay on closer inspection. I started by tracing the initialization of the `disk->nr_storage_elements` variable. I've noted that it begins at zero due to the `kzalloc_node` call when the `gendisk` is allocated, so initially `disk->nr_storage_elements` is zero. I then went through the `sd_zbc_report_storage_elements` function, which, on its own, seemed fine initially. It calls `sd_zbc_alloc_se_report` with `elements` as NULL and `*nr_elements` as 0. Inside `sd_zbc_alloc_se_report`, it allocates a buffer (`rep->buf`) based on `nr_elements`, but critically, since `nr_elements` is zero, the initial size is small. `rep->elements` is set to NULL, and the function returns. The call to `sd_zbc_report_se` then uses this buffer to execute a SCSI command and retrieve element information. `rep->nr_reported_elements` gets populated if `rep->elements` is NULL. My attention was immediately caught by how `disk_init_storage_element_mgmt` calls `report_elements`. In this call `elements` is NULL and `nr_elements` points to `disk->nr_storage_elements`, which is initialized to zero. This felt like a potential issue – a NULL `elements` combined with a zero `nr_elements` could lead to problems. However, carefully tracing the subsequent calls within the `sd_zbc` functions reveals no immediate issue. It appears `sd_zbc_alloc_se_report` handles this specific case correctly by returning 0, allocating the buffer, and setting `rep->elements` to NULL. Furthermore, the `sd_zbc_report_se` function correctly uses the allocated buffer and writes to `rep->nr_reported_elements` when `elements` is NULL. The function then correctly returns and updates `*nr_elements` to the actual number reported, after which the buffer and the report structure are freed. There's also no uninitialized read since all values in `rep` that are read were written before. Then I had to check error paths. I had to ensure that the logic correctly handles the failure to report elements using `zloop_report_elements` when using the zloop driver. If `test_bit(GD_DEAD, ...)` returns true, or `data_race(READ_ONCE(zlo->state)) == Zlo_deleting` is true, the function returns -ENODEV and the calling function will set `disk->nr_storage_elements` to 0. I also checked how the `zloop_zone` data structure is handled, specifically looking at how `wr_se_id` and `rd_se_id` are used. I needed to ensure that no uninitialized values were being used. A quick grep of the codebase shows where these variables are initialized and used within `zloop`, and they all seem fine. In summary, despite the initial concern with the NULL `elements` and zero `nr_elements`, the code appears to handle this specific case safely. The initial zero value for `disk->nr_storage_elements` is managed correctly through allocation of a small buffer, subsequent SCSI command execution, and updates to the number of reported elements. The error paths also ensure that a zero value is maintained when no element is provided. |
| 46/3 | 2026/11/12 05:15 | tool | grepper |
0mArgs:{
"Expression": "zloop_init_zone",
"PathPrefixes": [
"drivers/block/zloop.c"
]
}Results: {
"Output": "drivers/block/zloop.c=1579=static int zloop_get_block_size(struct zloop_device *zlo,\n--\ndrivers/block/zloop.c-1621-\ndrivers/block/zloop.c:1622:static int zloop_init_zone(struct zloop_device *zlo, struct zloop_options *opts,\ndrivers/block/zloop.c-1623-\t\t\t unsigned int zone_no, bool restore)\n--\ndrivers/block/zloop.c=1810=static int zloop_ctl_add(struct zloop_options *opts)\n--\ndrivers/block/zloop.c-1919-\t * If we already have zone files, we are restoring a device created by a\ndrivers/block/zloop.c:1920:\t * previous add operation. In this case, zloop_init_zone() will check\ndrivers/block/zloop.c-1921-\t * that the zone files are consistent with the zone configuration given.\n--\ndrivers/block/zloop.c-1924-\tfor (i = 0; i \u003c nr_zones; i++) {\ndrivers/block/zloop.c:1925:\t\tret = zloop_init_zone(zlo, opts, i, restore);\ndrivers/block/zloop.c-1926-\t\tif (ret)\n"
} |
| 47/3 | 2026/11/12 05:15 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 50832 output: 37 thoughts: 47 Thoughts: Okay, let's dive into this. I need to understand how `zlo` gets allocated within `zloop_ctl_add`. My first step, naturally, is to go straight to the source. Let's pull up the code. Specifically, I'm going to look at lines 1830 through 1860 in `drivers/block/zloop.c`. That's where the action is likely happening. Let's see what the allocation strategy is, what dependencies exist, and if there are any potential race conditions or resource contention issues I should be aware of. |
| 48/3 | 2026/11/12 05:15 | tool | read-file |
0mArgs:{
"File": "drivers/block/zloop.c",
"FirstLine": 1830,
"LineCount": 40
}Results: {
"Contents": "1830:\t\tnr_zones = opts-\u003ecapacity \u003e\u003e ilog2(opts-\u003ezone_size);\n1831:\t\tif (opts-\u003enr_conv_zones \u003e= nr_zones) {\n1832:\t\t\tpr_err(\"Invalid number of conventional zones %u\\n\",\n1833:\t\t\t opts-\u003enr_conv_zones);\n1834:\t\t\tgoto out;\n1835:\t\t}\n1836:\t\n1837:\t\tif (opts-\u003emax_open_zones \u003e nr_zones - opts-\u003enr_conv_zones) {\n1838:\t\t\tpr_err(\"Invalid maximum number of open zones %u\\n\",\n1839:\t\t\t opts-\u003emax_open_zones);\n1840:\t\t\tgoto out;\n1841:\t\t}\n1842:\t\n1843:\t\tzlo = kvzalloc_flex(*zlo, zones, nr_zones);\n1844:\t\tif (!zlo) {\n1845:\t\t\tret = -ENOMEM;\n1846:\t\t\tgoto out;\n1847:\t\t}\n1848:\t\tWRITE_ONCE(zlo-\u003estate, Zlo_creating);\n1849:\t\tspin_lock_init(\u0026zlo-\u003eopen_zones_lock);\n1850:\t\tINIT_LIST_HEAD(\u0026zlo-\u003eopen_zones_lru_list);\n1851:\t\tmutex_init(\u0026zlo-\u003estor_elements_lock);\n1852:\t\n1853:\t\tret = mutex_lock_killable(\u0026zloop_ctl_mutex);\n1854:\t\tif (ret)\n1855:\t\t\tgoto out_free_dev;\n1856:\t\n1857:\t\t/* Allocate id, if @opts-\u003eid \u003e= 0, we're requesting that specific id */\n1858:\t\tif (opts-\u003eid \u003e= 0) {\n1859:\t\t\tret = idr_alloc(\u0026zloop_index_idr, zlo,\n1860:\t\t\t\t\t opts-\u003eid, opts-\u003eid + 1, GFP_KERNEL);\n1861:\t\t\tif (ret == -ENOSPC)\n1862:\t\t\t\tret = -EEXIST;\n1863:\t\t} else {\n1864:\t\t\tret = idr_alloc(\u0026zloop_index_idr, zlo, 0, 0, GFP_KERNEL);\n1865:\t\t}\n1866:\t\tmutex_unlock(\u0026zloop_ctl_mutex);\n1867:\t\tif (ret \u003c 0)\n1868:\t\t\tgoto out_free_dev;\n1869:\t\n"
} |
| 49/3 | 2026/11/12 05:15 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 51704 output: 34 thoughts: 3395 Thoughts: **Analyzing Storage Element Reporting in Zoned Block Devices** Okay, let's break this down systematically. I'm focusing on the integrity and security of how storage elements are reported and handled, specifically with regard to potential information leaks or uninitialized memory exposure. I've tracked the flow of data through various functions related to zoned block devices (zlo), SCSI drives (sd), and associated IOCTLs. Initially, `zlo` structures are allocated and zeroed. The code uses `kvzalloc_flex` or similar memory allocation functions which, critically, initialize all memory to zero. The storage element setup depends on the `stor_elements` flag. If elements are emulated, `wr_se_id` and `rd_se_id` are populated. Otherwise, those fields are zero. The core usage of these IDs are guarded by `zlo->nr_elements` check. The IOCTL path, specifically `BLKREPORTSTORELEMS`, is where things get interesting. The kernel allocates a `blk_se_report` structure using `kzalloc_flex`. This function zero-initializes the allocated memory, mitigating a whole class of potential vulnerabilities. Then, the code copies from user space into the `blk_se_report` structure. There's a check on the `reserved` field to ensure it is 0. The code then calculates `nr_reported_elements`, the minimum of the user-provided and disk's declared number of elements. The disk's declared number of elements then overwrites the user supplied value. This value is used to report storage elements via a series of function calls which include `bdev_report_storage_elements`, `zloop_report_elements`, and `sd_zbc_report_se`. I need to ensure that the driver can't modify `nr_reported_elements` to point to a buffer larger than the one that was allocated (and zeroed). It's crucial to check for potential integer overflows or other manipulation. I see that the kernel function `bdev_report_storage_elements` checks the number of reported elements against the maximum disk value. Both `zloop_report_elements` and the SCSI code in `sd_zbc.c` use `min` to limit the number of reported elements to the valid range. In all cases, the code consistently minimizes the number of reported elements. I drilled down into the details of storage element data structures (`struct blk_se` and `struct blk_se_report`). I verified there's no padding within the structures and ensured the size of the structure is a multiple of the alignment requirements. This confirms that there aren't hidden uninitialized bytes that could be leaked via `copy_to_user`. I've determined that due to the zero-initialization, the defined structure layout, and the checks, there's no way for uninitialized kernel memory to be leaked to user space. Furthermore, I checked the flow in `sd_zbc_parse_storage_element` which zeroes the `struct blk_se`. Finally, I checked the code that deals with depopulation and storage element reporting and the `sd_zbc_check_depop_supported` and confirmed the control flow. The code uses `memset` to zero storage element data, and carefully constrains the number of elements reported. So, I can assert with confidence that the storage element report code is safe with respect to information leakage. |
| 50/3 | 2026/11/12 05:16 | tool | grepper |
0mArgs:{
"Expression": "sd_zbc_read_zones",
"PathPrefixes": [
"drivers/scsi/"
]
}Results: {
"Output": "drivers/scsi/sd.c=3793=static void sd_revalidate_disk(struct gendisk *disk)\n--\ndrivers/scsi/sd.c-3850-\t\t\tsd_read_block_characteristics(sdkp, lim);\ndrivers/scsi/sd.c:3851:\t\t\tsd_zbc_read_zones(sdkp, lim, buffer);\ndrivers/scsi/sd.c-3852-\t\t}\n--\ndrivers/scsi/sd.h=234=void sd_dif_config_host(struct scsi_disk *sdkp, struct queue_limits *lim);\n--\ndrivers/scsi/sd.h-237-\ndrivers/scsi/sd.h:238:int sd_zbc_read_zones(struct scsi_disk *sdkp, struct queue_limits *lim,\ndrivers/scsi/sd.h-239-\t\tu8 buf[SD_BUF_SIZE]);\n--\ndrivers/scsi/sd.h=248=extern const struct blk_se_ops sd_zbc_se_ops;\n--\ndrivers/scsi/sd.h-251-\ndrivers/scsi/sd.h:252:static inline int sd_zbc_read_zones(struct scsi_disk *sdkp,\ndrivers/scsi/sd.h-253-\t\tstruct queue_limits *lim, u8 buf[SD_BUF_SIZE])\n--\ndrivers/scsi/sd_zbc.c=1037=int sd_zbc_revalidate_zones(struct scsi_disk *sdkp)\n--\ndrivers/scsi/sd_zbc.c-1088-/**\ndrivers/scsi/sd_zbc.c:1089: * sd_zbc_read_zones - Read zone information and update the request queue\ndrivers/scsi/sd_zbc.c-1090- * @sdkp: SCSI disk pointer.\n--\ndrivers/scsi/sd_zbc.c-1097- */\ndrivers/scsi/sd_zbc.c:1098:int sd_zbc_read_zones(struct scsi_disk *sdkp, struct queue_limits *lim,\ndrivers/scsi/sd_zbc.c-1099-\t\tu8 buf[SD_BUF_SIZE])\n"
} |
| 51/3 | 2026/11/12 05:16 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 56442 output: 33 thoughts: 30 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience, presented in the first person: **Diving into `sd_revalidate_disk`'s Buffer Allocation** Right, let's get down to brass tacks. I need to understand this `buffer` variable within `sd_revalidate_disk`. It's crucial for understanding the I/O flow during revalidation and how we're managing the data. I need to trace its allocation path, pinpoint exactly where the memory is coming from, and how it's being freed. Is it a static buffer, dynamically allocated, or perhaps borrowed from some pool? Is there a risk of memory leaks, buffer overflows, or potential race conditions if access isn't properly synchronized? I have to consider the size, its scope, and how it interacts with the underlying block device driver. This knowledge is fundamental to optimizing revalidation performance and ensuring the overall stability of the disk subsystem. Let's see if I can find the answer. |
| 52/3 | 2026/11/12 05:16 | tool | grepper |
0mArgs:{
"Expression": "unsigned char \\*buffer",
"PathPrefixes": [
"drivers/scsi/sd.c"
]
}Results: {
"Output": "drivers/scsi/sd.c=2508=sd_spinup_disk(struct scsi_disk *sdkp)\n--\ndrivers/scsi/sd.c-2663- */\ndrivers/scsi/sd.c:2664:static int sd_read_protection_type(struct scsi_disk *sdkp, unsigned char *buffer)\ndrivers/scsi/sd.c-2665-{\n--\ndrivers/scsi/sd.c=2743=static int read_capacity_16(struct scsi_disk *sdkp, struct scsi_device *sdp,\ndrivers/scsi/sd.c:2744:\t\tstruct queue_limits *lim, unsigned char *buffer)\ndrivers/scsi/sd.c-2745-{\n--\ndrivers/scsi/sd.c=2833=static int read_capacity_10(struct scsi_disk *sdkp, struct scsi_device *sdp,\ndrivers/scsi/sd.c:2834:\t\t\t\t\t\tunsigned char *buffer)\ndrivers/scsi/sd.c-2835-{\n--\ndrivers/scsi/sd.c=2929=sd_read_capacity(struct scsi_disk *sdkp, struct queue_limits *lim,\ndrivers/scsi/sd.c:2930:\t\tunsigned char *buffer)\ndrivers/scsi/sd.c-2931-{\n--\ndrivers/scsi/sd.c=3052=sd_do_mode_sense(struct scsi_disk *sdkp, int dbd, int modepage,\ndrivers/scsi/sd.c:3053:\t\t unsigned char *buffer, int len, struct scsi_mode_data *data,\ndrivers/scsi/sd.c-3054-\t\t struct scsi_sense_hdr *sshdr)\n--\ndrivers/scsi/sd.c=3071=static void\ndrivers/scsi/sd.c:3072:sd_read_write_protect_flag(struct scsi_disk *sdkp, unsigned char *buffer)\ndrivers/scsi/sd.c-3073-{\n--\ndrivers/scsi/sd.c=3130=static void\ndrivers/scsi/sd.c:3131:sd_read_cache_type(struct scsi_disk *sdkp, unsigned char *buffer)\ndrivers/scsi/sd.c-3132-{\n--\ndrivers/scsi/sd.c=3302=static bool sd_is_perm_stream(struct scsi_disk *sdkp, unsigned int stream_id)\n--\ndrivers/scsi/sd.c-3331-\ndrivers/scsi/sd.c:3332:static void sd_read_io_hints(struct scsi_disk *sdkp, unsigned char *buffer)\ndrivers/scsi/sd.c-3333-{\n--\ndrivers/scsi/sd.c-3374- */\ndrivers/scsi/sd.c:3375:static void sd_read_app_tag_own(struct scsi_disk *sdkp, unsigned char *buffer)\ndrivers/scsi/sd.c-3376-{\n--\ndrivers/scsi/sd.c=3540=static void sd_read_block_provisioning(struct scsi_disk *sdkp)\n--\ndrivers/scsi/sd.c-3561-\ndrivers/scsi/sd.c:3562:static void sd_read_write_same(struct scsi_disk *sdkp, unsigned char *buffer)\ndrivers/scsi/sd.c-3563-{\n--\ndrivers/scsi/sd.c-3589-\ndrivers/scsi/sd.c:3590:static void sd_read_security(struct scsi_disk *sdkp, unsigned char *buffer)\ndrivers/scsi/sd.c-3591-{\n--\ndrivers/scsi/sd.c=3613=static void sd_read_cpr(struct scsi_disk *sdkp)\n--\ndrivers/scsi/sd.c-3615-\tstruct blk_independent_access_ranges *iars = NULL;\ndrivers/scsi/sd.c:3616:\tunsigned char *buffer = NULL;\ndrivers/scsi/sd.c-3617-\tunsigned int nr_cpr = 0;\n--\ndrivers/scsi/sd.c=3793=static void sd_revalidate_disk(struct gendisk *disk)\n--\ndrivers/scsi/sd.c-3798-\tstruct queue_limits *lim = NULL;\ndrivers/scsi/sd.c:3799:\tunsigned char *buffer = NULL;\ndrivers/scsi/sd.c-3800-\tunsigned int dev_max;\n"
} |
| 53/3 | 2026/11/12 05:16 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 57905 output: 37 thoughts: 26 Thoughts: Okay, here's my take on those lines in `drivers/scsi/sd.c`, thinking through the implications as an expert in the field: **Diving into `drivers/scsi/sd.c` (lines 3800-3820)** Alright, time to get my hands dirty. Let's zero in on lines 3800-3820 within `drivers/scsi/sd.c`. My initial assumption is that we are likely dealing with some crucial part of the storage driver, most probably a SCSI disk driver (`sd.c` suggests this). Given the line range, my gut feeling says we are either in error handling, command queuing, or device initialization/de-initialization stage. I'm mentally preparing for a deep dive. I'm looking for the context. What is happening in the surrounding functions? Is this part of an interrupt handler? Is it related to a specific SCSI command, or perhaps error recovery from a transport layer problem? I am expecting to see conditions and branches. Maybe some bit-shifting and masking, dealing with SCSI sense data or some sort of error flags. The line range is very tight, so the logic here is very focussed. I'm expecting some kind of conditional statement or assignment. If it is complex, it is probably calling something else. I'm also considering the potential performance impact. If this code path is heavily traversed, any inefficiency here will be amplified across all disks using this driver. The goal is likely to be efficient and safe. Let's see what gems we can find in these lines. No doubt something important is being handled here! |
| 54/3 | 2026/11/12 05:16 | tool | read-file |
0mArgs:{
"File": "drivers/scsi/sd.c",
"FirstLine": 3800,
"LineCount": 25
}Results: {
"Contents": "3800:\t\tunsigned int dev_max;\n3801:\t\tint err;\n3802:\t\n3803:\t\tSCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp,\n3804:\t\t\t\t\t \"sd_revalidate_disk\\n\"));\n3805:\t\n3806:\t\t/*\n3807:\t\t * If the device is offline, don't try and read capacity or any\n3808:\t\t * of the other niceties.\n3809:\t\t */\n3810:\t\tif (!scsi_device_online(sdp))\n3811:\t\t\treturn;\n3812:\t\n3813:\t\tlim = kmalloc_obj(*lim);\n3814:\t\tif (!lim)\n3815:\t\t\treturn;\n3816:\t\n3817:\t\tbuffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL);\n3818:\t\tif (!buffer)\n3819:\t\t\tgoto out;\n3820:\t\n3821:\t\tsd_spinup_disk(sdkp);\n3822:\t\n3823:\t\t*lim = queue_limits_start_update(sdkp-\u003edisk-\u003equeue);\n3824:\t\n"
} |
| 55/3 | 2026/11/12 05:16 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 58649 output: 32 thoughts: 44 Thoughts: Okay, let's dive into this code. **Analyzing SCSI Report Opcode Initialization** Alright, I need to understand how the `scsi_report_opcode` function handles a buffer, `buf`. The critical question is whether it directly initializes `buf` within the function itself or if it simply passes `buf` off to `scsi_execute_cmd` to be populated. This is key to understanding how data is handled and where potential issues might arise. I'll start by taking a close look at `scsi_report_opcode` and tracing the usage of `buf`. The goal is to see if any initialization happens directly within that function before a potential call to `scsi_execute_cmd`. |
| 56/3 | 2026/11/12 05:17 | tool | grepper |
0mArgs:{
"Expression": "scsi_report_opcode",
"PathPrefixes": [
"drivers/scsi/"
]
}Results: {
"Output": "drivers/scsi/scsi.c=506=void scsi_attach_vpd(struct scsi_device *sdev)\n--\ndrivers/scsi/scsi.c-552-/**\ndrivers/scsi/scsi.c:553: * scsi_report_opcode - Find out if a given command is supported\ndrivers/scsi/scsi.c-554- * @sdev:\tscsi device to query\n--\ndrivers/scsi/scsi.c-565- */\ndrivers/scsi/scsi.c:566:int scsi_report_opcode(struct scsi_device *sdev, unsigned char *buffer,\ndrivers/scsi/scsi.c-567-\t\t unsigned int len, unsigned char opcode,\n--\ndrivers/scsi/scsi.c-617-}\ndrivers/scsi/scsi.c:618:EXPORT_SYMBOL(scsi_report_opcode);\ndrivers/scsi/scsi.c-619-\n--\ndrivers/scsi/scsi.c=622=static bool scsi_cdl_check_cmd(struct scsi_device *sdev, u8 opcode, u16 sa,\n--\ndrivers/scsi/scsi.c-628-\t/* Check operation code */\ndrivers/scsi/scsi.c:629:\tret = scsi_report_opcode(sdev, buf, SCSI_CDL_CHECK_BUF_LEN, opcode, sa);\ndrivers/scsi/scsi.c-630-\tif (ret \u003c= 0)\n--\ndrivers/scsi/sd.c=3562=static void sd_read_write_same(struct scsi_disk *sdkp, unsigned char *buffer)\n--\ndrivers/scsi/sd.c-3571-\ndrivers/scsi/sd.c:3572:\tif (scsi_report_opcode(sdev, buffer, SD_BUF_SIZE, INQUIRY, 0) \u003c 0) {\ndrivers/scsi/sd.c-3573-\t\tsdev-\u003eno_report_opcodes = 1;\n--\ndrivers/scsi/sd.c-3582-\ndrivers/scsi/sd.c:3583:\tif (scsi_report_opcode(sdev, buffer, SD_BUF_SIZE, WRITE_SAME_16, 0) == 1)\ndrivers/scsi/sd.c-3584-\t\tsdkp-\u003ews16 = 1;\ndrivers/scsi/sd.c-3585-\ndrivers/scsi/sd.c:3586:\tif (scsi_report_opcode(sdev, buffer, SD_BUF_SIZE, WRITE_SAME, 0) == 1)\ndrivers/scsi/sd.c-3587-\t\tsdkp-\u003ews10 = 1;\n--\ndrivers/scsi/sd.c=3590=static void sd_read_security(struct scsi_disk *sdkp, unsigned char *buffer)\n--\ndrivers/scsi/sd.c-3596-\ndrivers/scsi/sd.c:3597:\tif (scsi_report_opcode(sdev, buffer, SD_BUF_SIZE,\ndrivers/scsi/sd.c-3598-\t\t\tSECURITY_PROTOCOL_IN, 0) == 1 \u0026\u0026\ndrivers/scsi/sd.c:3599:\t scsi_report_opcode(sdev, buffer, SD_BUF_SIZE,\ndrivers/scsi/sd.c-3600-\t\t\tSECURITY_PROTOCOL_OUT, 0) == 1)\n--\ndrivers/scsi/sd_zbc.c=529=static void sd_zbc_check_depop_supported(struct scsi_disk *sdkp,\n--\ndrivers/scsi/sd_zbc.c-533-\tsdkp-\u003edepop_report_supported =\ndrivers/scsi/sd_zbc.c:534:\t\tscsi_report_opcode(sdkp-\u003edevice, buf, SD_BUF_SIZE,\ndrivers/scsi/sd_zbc.c-535-\t\t\t\t SERVICE_ACTION_IN_16,\n--\ndrivers/scsi/sd_zbc.c-543-\tsdkp-\u003edepop_modify_zones_supported =\ndrivers/scsi/sd_zbc.c:544:\t\tscsi_report_opcode(sdkp-\u003edevice, buf, SD_BUF_SIZE,\ndrivers/scsi/sd_zbc.c-545-\t\t\t\t SERVICE_ACTION_IN_16,\n--\ndrivers/scsi/sd_zbc.c-548-\tsdkp-\u003edepop_restore_supported =\ndrivers/scsi/sd_zbc.c:549:\t\tscsi_report_opcode(sdkp-\u003edevice, buf, SD_BUF_SIZE,\ndrivers/scsi/sd_zbc.c-550-\t\t\t\t SERVICE_ACTION_IN_16,\n"
} |
| 57/3 | 2026/11/12 05:17 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 60103 output: 37 thoughts: 41 Thoughts: Here's my thought process, as requested: **SCSI Opcode Investigation** Okay, so I need to understand how `scsi_report_opcode` is handled. I'm pretty sure it's a standard function, widely used throughout the SCSI layer. Now, let me confirm that suspicion and see how it's actually implemented. Time to dive into the kernel source, specifically `drivers/scsi/scsi.c`. That's where I'd expect the core SCSI functionality to reside, and thus where `scsi_report_opcode` should be defined or, at least, have its main implementation. I need to understand its behavior thoroughly. |
| 58/3 | 2026/11/12 05:17 | tool | read-file |
0mArgs:{
"File": "drivers/scsi/scsi.c",
"FirstLine": 565,
"LineCount": 45
}Results: {
"Contents": " 565:\t */\n 566:\tint scsi_report_opcode(struct scsi_device *sdev, unsigned char *buffer,\n 567:\t\t\t unsigned int len, unsigned char opcode,\n 568:\t\t\t unsigned short sa)\n 569:\t{\n 570:\t\tunsigned char cmd[16];\n 571:\t\tstruct scsi_sense_hdr sshdr;\n 572:\t\tint result, request_len;\n 573:\t\tconst struct scsi_exec_args exec_args = {\n 574:\t\t\t.sshdr = \u0026sshdr,\n 575:\t\t};\n 576:\t\n 577:\t\tif (sdev-\u003eno_report_opcodes || sdev-\u003escsi_level \u003c SCSI_SPC_3)\n 578:\t\t\treturn -EINVAL;\n 579:\t\n 580:\t\t/* RSOC header + size of command we are asking about */\n 581:\t\trequest_len = 4 + COMMAND_SIZE(opcode);\n 582:\t\tif (request_len \u003e len) {\n 583:\t\t\tdev_warn_once(\u0026sdev-\u003esdev_gendev,\n 584:\t\t\t\t \"%s: len %u bytes, opcode 0x%02x needs %u\\n\",\n 585:\t\t\t\t __func__, len, opcode, request_len);\n 586:\t\t\treturn -EINVAL;\n 587:\t\t}\n 588:\t\n 589:\t\tmemset(cmd, 0, 16);\n 590:\t\tcmd[0] = MAINTENANCE_IN;\n 591:\t\tcmd[1] = MI_REPORT_SUPPORTED_OPERATION_CODES;\n 592:\t\tif (!sa) {\n 593:\t\t\tcmd[2] = 1;\t/* One command format */\n 594:\t\t\tcmd[3] = opcode;\n 595:\t\t} else {\n 596:\t\t\tcmd[2] = 3;\t/* One command format with service action */\n 597:\t\t\tcmd[3] = opcode;\n 598:\t\t\tput_unaligned_be16(sa, \u0026cmd[4]);\n 599:\t\t}\n 600:\t\tput_unaligned_be32(request_len, \u0026cmd[6]);\n 601:\t\tmemset(buffer, 0, len);\n 602:\t\n 603:\t\tresult = scsi_execute_cmd(sdev, cmd, REQ_OP_DRV_IN, buffer,\n 604:\t\t\t\t\t request_len, 30 * HZ, 3, \u0026exec_args);\n 605:\t\tif (result \u003c 0)\n 606:\t\t\treturn result;\n 607:\t\tif (result \u0026\u0026 scsi_sense_valid(\u0026sshdr) \u0026\u0026\n 608:\t\t sshdr.sense_key == ILLEGAL_REQUEST \u0026\u0026\n 609:\t\t (sshdr.sense_code == INVALID_COMMAND_OP_CODE ||\n"
} |
| 59/3 | 2026/11/12 05:17 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 61091 output: 39 thoughts: 606 Thoughts: **My Analysis of Potential Uninitialized Variable Use in `sd_zbc.c`** Okay, let's break this down. My initial observation from looking at line 601 in `scsi_report_opcode` is that the buffer is zeroed out using `memset`. This is standard, good practice. Now, I'm digging deeper into `sd_zbc.c` and specifically focusing on potential uninitialized variable issues. I started with `sd_zbc_revalidate_zones`. Excellent! Here, `print_zones` is initialized before use with a clear condition. All variables involved in the evaluation seem properly set. No immediate concerns there. Moving on to `sd_zbc_execute_storage_element_cmd`, I see that the command buffer `cmd` is also initialized to zero using `memset`. Again, good. We're keeping things clean here. Next, I examined `sd_zbc_wait_for_se_removal`. Within the loop, I see the booleans `element_checked` and `element_removed`, as well as the integer `in_progress`, all being initialized before they are used. Looks solid, again. However, the real test is in `sd_zbc_wait_for_se_restore`. Here's where it gets interesting. I see that `in_progress` is set *within* the loop. But, I need to check the logic, because if the loop doesn't run, or if the return value `ret` is read before the loop, we could have a problem. I'm going to jump to lines 900-940 to see what the control flow is here. I have to look more closely to determine if `ret` is read before it is set. If not, it's a potential vulnerability. |
| 60/3 | 2026/11/12 05:17 | tool | read-file |
0mArgs:{
"File": "drivers/scsi/sd_zbc.c",
"FirstLine": 900,
"LineCount": 45
}Results: {
"Contents": " 900:\t\t\t\treturn -EIO;\n 901:\t\t\tif (element_removed \u0026\u0026 !in_progress)\n 902:\t\t\t\treturn 0;\n 903:\t\t} while (!fatal_signal_pending(current));\n 904:\t\n 905:\t\treturn -EINTR;\n 906:\t}\n 907:\t\n 908:\tstatic int sd_zbc_remove_storage_element(struct gendisk *disk,\n 909:\t\t\t\t\t\t unsigned int element_id)\n 910:\t{\n 911:\t\tstruct scsi_disk *sdkp = scsi_disk(disk);\n 912:\t\tstruct sd_zbc_se_report rep;\n 913:\t\tint ret;\n 914:\t\n 915:\t\tif (!sdkp-\u003edepop_modify_zones_supported)\n 916:\t\t\treturn -EOPNOTSUPP;\n 917:\t\n 918:\t\t/*\n 919:\t\t * Get the resources we need to prepare for waiting for the operation to\n 920:\t\t * complete.\n 921:\t\t */\n 922:\t\tret = sd_zbc_alloc_se_report(NULL, disk-\u003enr_storage_elements,\n 923:\t\t\t\t\t true, \u0026rep);\n 924:\t\tif (ret)\n 925:\t\t\treturn ret;\n 926:\t\n 927:\t\t/*\n 928:\t\t * Execute the remove command and wait for the element removal operation\n 929:\t\t * to complete.\n 930:\t\t */\n 931:\t\tret = sd_zbc_execute_storage_element_cmd(sdkp,\n 932:\t\t\t\tSAI_REMOVE_ELEMENT_AND_MODIFY_ZONES, element_id);\n 933:\t\tif (!ret)\n 934:\t\t\tret = sd_zbc_wait_for_se_removal(sdkp, \u0026rep, element_id);\n 935:\t\tif (!ret)\n 936:\t\t\tsd_printk(KERN_NOTICE, sdkp,\n 937:\t\t\t\t \"Removed storage element %u\\n\", element_id);\n 938:\t\n 939:\t\tsd_zbc_free_se_report(\u0026rep);\n 940:\t\n 941:\t\treturn ret;\n 942:\t}\n 943:\t\n 944:\t/*\n"
} |
| 61/3 | 2026/11/12 05:17 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 62755 output: 39 thoughts: 18 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience and presented in the first person: **Analyzing `sd_zbc_wait_for_se_restore`** Alright, let's dive into this `sd_zbc_wait_for_se_restore` function. My initial thought is to figure out precisely *why* we're waiting. Is this a critical path operation? If so, understanding the blocking conditions is paramount. I need to quickly ascertain the potential dependencies this has. What resources is it waiting on? Is it a hardware issue, a network hiccup, or something more fundamental within the storage subsystem? My mind is immediately racing through possible causes. Could it be a slow disk restore operation? Or perhaps we're waiting for a specific security event to complete before we can restore the data? I'll need to look at what calls this function, and what other functions it relies upon. I'll want to see the typical execution path - and the *un*typical ones. I'm expecting some error handling here; I hope it is robust! I'll need to determine the implications of this wait on overall system performance and the potential for cascading failures if this function hangs or times out in the wrong circumstances. Is there a defined timeout or a mechanism for detecting and mitigating indefinite waits? Furthermore, I have an interest in its interaction with the journaling/logging system, and its potential impact on data integrity during the restore operation. Are there checks for data consistency before and after the wait? Is this a synchronous or asynchronous call, and how does that affect the overall data flow? Basically, I need to know the 'how' and 'why' of this wait in order to assess its impact on the system. |
| 62/3 | 2026/11/12 05:17 | tool | read-file |
0mArgs:{
"File": "drivers/scsi/sd_zbc.c",
"FirstLine": 945,
"LineCount": 45
}Results: {
"Contents": " 945:\t * Wait for the completion of the restoration of removed storage elements.\n 946:\t * See the note for sd_zbc_wait_for_se_removal().\n 947:\t */\n 948:\tstatic int sd_zbc_wait_for_se_restore(struct scsi_disk *sdkp,\n 949:\t\t\t\t\t struct sd_zbc_se_report *rep)\n 950:\t{\n 951:\t\tint ret, in_progress;\n 952:\t\tunsigned int i;\n 953:\t\n 954:\t\t/*\n 955:\t\t * Poll the storage elements health to see if we have any element still\n 956:\t\t * being restored or if we got errors. Since we do not know how long the\n 957:\t\t * operation may take, we cannot have a timeout on this loop. So check\n 958:\t\t * for fatal signals so that the user can terminate this retry loop if\n 959:\t\t * the device misbehaves.\n 960:\t\t */\n 961:\t\tdo {\n 962:\t\t\t/* Let the operation proceed and check. */\n 963:\t\t\tmsleep(1000);\n 964:\t\n 965:\t\t\tret = sd_zbc_report_se(sdkp, rep);\n 966:\t\t\tif (ret)\n 967:\t\t\t\treturn ret;\n 968:\t\n 969:\t\t\tin_progress = 0;\n 970:\t\t\tfor (i = 0; i \u003c rep-\u003enr_elements; i++) {\n 971:\t\t\t\tswitch (rep-\u003eelements[i].status) {\n 972:\t\t\t\tcase BLK_SE_STS_RESTORE_IN_PROGRESS:\n 973:\t\t\t\t\tin_progress++;\n 974:\t\t\t\t\tbreak;\n 975:\t\t\t\tcase BLK_SE_STS_REMOVED:\n 976:\t\t\t\tcase BLK_SE_STS_RESTORE_ERROR:\n 977:\t\t\t\t\tif (rep-\u003eelements[i].restore_allowed)\n 978:\t\t\t\t\t\tret = -EIO;\n 979:\t\t\t\t\tbreak;\n 980:\t\t\t\tdefault:\n 981:\t\t\t\t\tbreak;\n 982:\t\t\t\t}\n 983:\t\t\t}\n 984:\t\n 985:\t\t\tif (!in_progress)\n 986:\t\t\t\treturn ret;\n 987:\t\t} while (!fatal_signal_pending(current));\n 988:\t\n 989:\t\treturn -EINTR;\n"
} |
| 63/3 | 2026/11/12 05:17 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 63832 output: 389 thoughts: 4250 Thoughts: **Analysis of Potential Uninitialized Memory Issues in Zoned Block Device Management Patch** Okay, let's break this down. My goal here is to identify if this patch series introduces any risk of leaking uninitialized memory. I'm going through it methodically, looking at every code path where data might be read or copied, and ensuring that everything is properly initialized before use. First, I examined the `sd_zbc_wait_for_se_restore` function and confirmed that the return value `ret` is correctly initialized before any potential return. That's a good starting point, and I did similar checks on several other key functions. I then did a sweep of the existing code, checking the ioctl handlers for any potential issues. Specifically, I focused on `blkdev_ioctl_report_storage_elements` since it handles user-provided data via the ioctl interface. I meticulously traced how `disk->nr_storage_elements` is used and how the `struct blk_se_report` is constructed and copied to userspace. I paid special attention to the cases where `rep->nr_elements` could be zero (or non-zero). The analysis showed that even with a zero value for `rep->nr_elements`, the structure is copied to userspace only with its initialized header, and any data regions are zeroed by `kzalloc_flex`. This is safe. The same analysis showed that if the number of elements reported by userspace is positive, then the code fills in the user array by means of `bdev_report_storage_elements` and `kzalloc_flex`, so no uninitialized data gets copied out. Then, I took a closer look at the memory layout of the `struct blk_se` and `struct blk_se_report` structures. Critically, these are *exposed* to userspace. It's important to make sure there are no compiler-inserted padding bytes. I checked the offsets and sizes of each member and confirmed that they are naturally aligned and tightly packed, with no potential for uninitialized data. Specifically, I checked the alignment and that the `reserved` arrays are explicitly defined to take up any potential padding bytes that could result from 32-bit vs. 64-bit builds. After those checks, I looked at every single function that was *modified* or *added* in this diff. I traced the initialization of all variables, including function arguments, local variables, and memory allocated using `kzalloc` and its variants. Functions like `disk_zone_is_offline`, `blk_revalidate_disk_zones`, `bdev_report_storage_elements`, and the various IOCTL handlers were carefully examined. I also considered data flow throughout `zloop.c`, including functions such as `zloop_open_zone`, `zloop_prep_rq`, and how storage elements are managed, restored, and removed in `zloop_execute_remove_element`, `zloop_restore_elements`, and `zloop_degrade_element`. The modified SCSI driver functions and `sd_zbc.c` functions were checked to ensure memory safety. Overall, I concluded that this patch series does *not* introduce any risk of uninitialized memory leaks. All memory allocations use `kzalloc` or similar zeroing allocators. Stack-based structures and buffers are initialized or explicitly zeroed using `memset`. The new UAPI structures have no compiler-inserted padding holes and everything that is copied to userspace is fully initialized by the kernel. Potential concurrency/race conditions, locking issues, or out-of-bounds access risks, which are separate concerns, are addressed by standard testing and debug tools, such as KASAN and LOCKDEP. There are no memory safety risks introduced by this patch. set-results NeedsKMSAN=false KMSANReasoning="This patch adds support for zoned device storage element management (depopulation/restoration/degradation) across the block layer, SCSI ZBC, and the zloop driver. All memory allocations for the new structures (e.g. blk_se, blk_se_report, zloop_device) use zeroing allocators (kzalloc, kzalloc_flex, kzalloc_objs), and local stack structures/buffers are explicitly initialized or zeroed via memset. The new UAPI structures (struct blk_se and struct blk_se_report) have no padding holes, and all fields copied to user space are fully initialized. Any potential issues (such as bounds checking, concurrent queue access, locking) fall squarely under KASAN and LOCKDEP." |
| 64/3 | 2026/11/12 05:18 | tool | set-results |
0mArgs:{
"KMSANReasoning": "The patch series introduces zoned block device storage element management (reporting, depopulation/removal, restoration, and degradation) across the block layer (blk-zoned), SCSI ZBC (sd_zbc), and the zoned loop driver (zloop).\n\nAnalysis of memory handling across the changes:\n1. UAPI structures and kernel-to-user copies: The ioctl BLKREPORTSTORELEMS uses struct blk_se_report and struct blk_se to return storage element descriptors to user space. Both structures are naturally aligned with no compiler padding holes (struct blk_se has explicit reserved[13] aligning it to 32 bytes; struct blk_se_report has a 64-bit reserved field before the flex array). Furthermore, the kernel allocation uses kzalloc_flex(), and individual elements are either memset to zero (sd_zbc_parse_storage_element) or copied from kzalloc_objs-allocated arrays (zloop_create_storage_elements).\n2. Heap and stack allocations: All new heap allocations use kzalloc, kzalloc_objs, or kzalloc_flex. Stack structures and command buffers (e.g. SCSI CDBs, zloop options) are zero-initialized with `{ }` or memset before use.\n3. Control flow and bounds: The changes do not introduce branching on uninitialized data or alter data lengths in a way that exposes uninitialized memory.\n\nAny potential bugs in these changes would involve bounds checking, device locking/serialization (covered by LOCKDEP), or memory corruption/out-of-bounds accesses (covered by KASAN). There is no risk of uninitialized memory reads or information leaks to userspace that would necessitate a dedicated KMSAN fuzzing session.",
"NeedsKMSAN": false
}Results: {
"KMSANReasoning": "The patch series introduces zoned block device storage element management (reporting, depopulation/removal, restoration, and degradation) across the block layer (blk-zoned), SCSI ZBC (sd_zbc), and the zoned loop driver (zloop).\n\nAnalysis of memory handling across the changes:\n1. UAPI structures and kernel-to-user copies: The ioctl BLKREPORTSTORELEMS uses struct blk_se_report and struct blk_se to return storage element descriptors to user space. Both structures are naturally aligned with no compiler padding holes (struct blk_se has explicit reserved[13] aligning it to 32 bytes; struct blk_se_report has a 64-bit reserved field before the flex array). Furthermore, the kernel allocation uses kzalloc_flex(), and individual elements are either memset to zero (sd_zbc_parse_storage_element) or copied from kzalloc_objs-allocated arrays (zloop_create_storage_elements).\n2. Heap and stack allocations: All new heap allocations use kzalloc, kzalloc_objs, or kzalloc_flex. Stack structures and command buffers (e.g. SCSI CDBs, zloop options) are zero-initialized with `{ }` or memset before use.\n3. Control flow and bounds: The changes do not introduce branching on uninitialized data or alter data lengths in a way that exposes uninitialized memory.\n\nAny potential bugs in these changes would involve bounds checking, device locking/serialization (covered by LOCKDEP), or memory corruption/out-of-bounds accesses (covered by KASAN). There is no risk of uninitialized memory reads or information leaks to userspace that would necessitate a dedicated KMSAN fuzzing session.",
"NeedsKMSAN": false
} |
| Total Calls | Total Tokens | Avg Tokens | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|
| Total Calls | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|