+
+#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,11,0))
+
+#define LTTNG_PART_STRUCT_TYPE struct block_device
+
+static
+int lttng_get_part_name(struct gendisk *disk, struct block_device *part, char *name_buf)
+{
+ const char *p;
+
+ p = bdevname(part, name_buf);
+ if (!p)
+ return -ENOSYS;
+
+ return 0;
+}
+
+static
+dev_t lttng_get_part_devt(struct block_device *part)
+{
+ return part->bd_dev;
+}
+
+#else
+
+#define LTTNG_PART_STRUCT_TYPE struct hd_struct
+
+static
+int lttng_get_part_name(struct gendisk *disk, struct hd_struct *part, char *name_buf)
+{
+ const char *p;
+ struct block_device bdev;
+
+ /*
+ * Create a partial 'struct blockdevice' to use
+ * 'bdevname()' which is a simple wrapper over
+ * 'disk_name()' but has the honor to be EXPORT_SYMBOL.
+ */
+ bdev.bd_disk = disk;
+ bdev.bd_part = part;
+
+ p = bdevname(&bdev, name_buf);
+ if (!p)
+ return -ENOSYS;
+
+ return 0;
+}
+
+static
+dev_t lttng_get_part_devt(struct hd_struct *part)
+{
+ return part_devt(part);
+}
+#endif
+
+#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,12,0))
+static
+int lttng_statedump_each_block_device(struct lttng_session *session, struct gendisk *disk)
+{
+ struct block_device *part;
+ unsigned long idx;
+ int ret = 0;
+
+ /* Include partition 0 */
+ idx = 0;
+
+ rcu_read_lock();
+ xa_for_each(&disk->part_tbl, idx, part) {
+ char name_buf[BDEVNAME_SIZE];
+
+ /* Exclude non-partitions bdev and empty partitions. */
+ if (bdev_is_partition(part) && !bdev_nr_sectors(part))
+ continue;
+
+ if (lttng_get_part_name(disk, part, name_buf) == -ENOSYS) {
+ ret = -ENOSYS;
+ goto end;
+ }
+ trace_lttng_statedump_block_device(session, lttng_get_part_devt(part),
+ name_buf);
+ }
+end:
+ rcu_read_unlock();
+ return ret;
+}
+#else
+static
+int lttng_statedump_each_block_device(struct lttng_session *session, struct gendisk *disk)
+{
+ struct disk_part_iter piter;
+ LTTNG_PART_STRUCT_TYPE *part;
+
+ disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
+
+ while ((part = disk_part_iter_next(&piter))) {
+ char name_buf[BDEVNAME_SIZE];
+
+ if (lttng_get_part_name(disk, part, name_buf) == -ENOSYS) {
+ disk_part_iter_exit(&piter);
+ return -ENOSYS;
+ }
+ trace_lttng_statedump_block_device(session, lttng_get_part_devt(part),
+ name_buf);
+ }
+ disk_part_iter_exit(&piter);
+
+ return 0;
+}
+#endif
+