From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9D857606A33E for ; Sun, 8 Nov 2026 20:48:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=xS87/ptx; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=vMzRtOUg DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Unsubscribe-Post: List-Owner; bh=ikmfrunlJJ/YKeUvGeyPy6Hr5fLbyjiGxROd9YPAoJ4=; b=xS87/ptx9yigai ZbZEizoLFbSu9jC+a4W8CJk0//GBMEg6EHKlUPbham4bpsSrbfXolSHSNN7Kkb1d6O0OnaulTVTd/ 9fZeVk1zR1nSVgQM8fargagUUOtTbPkWm7wcGsg09V75m+kH3xOOlA83Y4aiasE+G2d5r1quTMgHL rcJHFgNunbsfSQKTixzC8Skq/u2Q/oGrbXox4UvK1rD7VqxwmBkSdncbCRMqkgemECD60jDWz06BO IXt3E0hpOl0gI9QFr9Kh1KOtfxVeChNqrv5zHr6Gk6HOATQ9y86YmbqFdj1gPiUypkOk+7PIK+127 EurG7nQbSiI2fhfFc9Iw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xQ9o3-0000000GbkT-0nx8; Sun, 08 Nov 2026 20:48:03 +0000 Received: from out-30.mta1.migadu.com ([95.215.58.30] helo=mta1.migadu.com) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xQ9o0-0000000Gbhg-00R3 for linux-nvme@lists.infradead.org; Sun, 08 Nov 2026 20:48:01 +0000 X-Envelope-To: linux-nvme@lists.infradead.org DKIM-Signature: a=rsa-sha256; bh=RcUeH7gkhrSc5PnVsT18R86a50wGu0BU/15a/DDdUMk=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1794170878; v=1; x=1794775678; b=vMzRtOUgZFag1qjNoQ/W4Dozw9mdeZIO3GU6PidayqzLUuxTB2c3T7vyitO2ivBwa8/r0OWg Tv1lE6KRYD0ftkhX0beGWRoYZ87OD+KJ+gxEdN8oPdFcc0uclPrFLl4QgWKJeC8nR2PhDFcaK0T cEFz4G3TyE2ht4hNN5J2w1Y0= X-Envelope-To: linux-nvme@lists.infradead.org Received: by mta10.migadu.com with ESMTPS id 302a52f6cfd93f21; Sun, 08 Nov 2026 20:47:58 +0000 X-Mizu-Trace-ID: 302a52f6cfd93f21 X-Migadu-Flow: FLOW_OUT From: John Garry To: james.bottomley@hansenpartnership.com, mkp@kernel.org Cc: hare@suse.com, linux-nvme@lists.infradead.org, linux-scsi@vger.kernel.org, snitzer@kernel.org, jmeneghi@redhat.com, bmarzins@redhat.com, dm-devel@lists.linux.dev, hch@lst.de, michael.christie@oracle.com, John Garry Subject: [PATCH v9 03/29] libmultipath: Add basic gendisk support Date: Sun, 8 Nov 2026 20:47:07 +0000 Message-ID: <20261108204733.557995-4-john.garry@linux.dev> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261108204733.557995-1-john.garry@linux.dev> References: <20261108204733.557995-1-john.garry@linux.dev> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261108_124800_185567_ED49A4A4 X-CRM114-Status: GOOD ( 13.37 ) X-Spam-Score: -2.8 (--) X-BeenThere: linux-nvme@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Linux-nvme" X-Spam-Status: No, score=-18.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 Add support to allocate and free a multipath gendisk. NVMe has almost like-for-like equivalents here: - mpath_alloc_head_disk() -> nvme_mpath_alloc_disk() - multipath_partition_scan_work() -> nvme_partition_scan_work() - mpath_remove_disk() -> nvme_remove_head() - mpath_device_set_live() -> nvme_mpath_set_live() struct mpath_head_template is introduced as a method for drivers to provide custom multipath functionality. Signed-off-by: John Garry --- include/linux/multipath.h | 38 ++++++++++++++ lib/multipath.c | 103 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+) diff --git a/include/linux/multipath.h b/include/linux/multipath.h index 407d985cd31f..ca6e02505be4 100644 --- a/include/linux/multipath.h +++ b/include/linux/multipath.h @@ -5,11 +5,19 @@ #include #include +extern const struct block_device_operations mpath_ops; + struct mpath_device { + struct mpath_head *mpath_head; struct list_head siblings; struct gendisk *disk; }; +struct mpath_head_template { +}; + +#define MPATH_HEAD_DISK_LIVE 0 + struct mpath_head { struct srcu_struct srcu; struct list_head dev_list; /* list of all mpath_devs */ @@ -17,12 +25,42 @@ struct mpath_head { refcount_t refcount; + unsigned long flags; + struct gendisk *disk; + struct work_struct partition_scan_work; + struct device *parent; + const struct attribute_group **disk_groups; + const struct mpath_head_template *mpdt; struct mpath_device __rcu *current_path[MAX_NUMNODES]; }; +static inline struct mpath_head *mpath_bd_device_to_head(struct device *dev) +{ + return dev_get_drvdata(dev); +} + +static inline struct mpath_head *mpath_gendisk_to_head(struct gendisk *disk) +{ + return mpath_bd_device_to_head(disk_to_dev(disk)); +} + int mpath_get_head(struct mpath_head *mpath_head); void mpath_put_head(struct mpath_head *mpath_head); int mpath_head_init(struct mpath_head *mpath_head); void mpath_head_uninit(struct mpath_head *mpath_head); +void mpath_put_disk(struct mpath_head *mpath_head); +void mpath_remove_disk(struct mpath_head *mpath_head); +int mpath_alloc_head_disk(struct mpath_head *mpath_head, + struct queue_limits *lim, int numa_node); +void mpath_device_set_live(struct mpath_device *mpath_device); + +static inline bool is_mpath_disk(struct gendisk *disk) +{ + #if IS_ENABLED(CONFIG_LIBMULTIPATH) + return disk->fops == &mpath_ops; + #else + return false; + #endif +} #endif // _LIBMULTIPATH_H diff --git a/lib/multipath.c b/lib/multipath.c index 31ef98980ba4..93c0ca8b390b 100644 --- a/lib/multipath.c +++ b/lib/multipath.c @@ -37,6 +37,106 @@ void mpath_head_uninit(struct mpath_head *mpath_head) } EXPORT_SYMBOL_GPL(mpath_head_uninit); +static int mpath_bdev_open(struct gendisk *disk, blk_mode_t mode) +{ + struct mpath_head *mpath_head = disk->private_data; + + return mpath_get_head(mpath_head); +} + +static void mpath_bdev_release(struct gendisk *disk) +{ + struct mpath_head *mpath_head = disk->private_data; + + mpath_put_head(mpath_head); +} + +const struct block_device_operations mpath_ops = { + .owner = THIS_MODULE, + .open = mpath_bdev_open, + .release = mpath_bdev_release, +}; +EXPORT_SYMBOL_GPL(mpath_ops); + +static void multipath_partition_scan_work(struct work_struct *work) +{ + struct mpath_head *mpath_head = + container_of(work, struct mpath_head, partition_scan_work); + + if (WARN_ON_ONCE(!test_and_clear_bit(GD_SUPPRESS_PART_SCAN, + &mpath_head->disk->state))) + return; + + mutex_lock(&mpath_head->disk->open_mutex); + bdev_disk_changed(mpath_head->disk, false); + mutex_unlock(&mpath_head->disk->open_mutex); +} + +void mpath_remove_disk(struct mpath_head *mpath_head) +{ + if (test_and_clear_bit(MPATH_HEAD_DISK_LIVE, &mpath_head->flags)) { + struct gendisk *disk = mpath_head->disk; + + del_gendisk(disk); + } +} +EXPORT_SYMBOL_GPL(mpath_remove_disk); + +void mpath_put_disk(struct mpath_head *mpath_head) +{ + if (!mpath_head->disk) + return; + + /* make sure all pending bios are cleaned up */ + flush_work(&mpath_head->partition_scan_work); + put_disk(mpath_head->disk); + mpath_head->disk = NULL; +} +EXPORT_SYMBOL_GPL(mpath_put_disk); + +int mpath_alloc_head_disk(struct mpath_head *mpath_head, + struct queue_limits *lim, int numa_node) +{ + struct gendisk *disk; + + if (mpath_head->disk) + return -EINVAL; + + disk = blk_alloc_disk(lim, numa_node); + if (IS_ERR(disk)) + return PTR_ERR(disk); + + mpath_head->disk = disk; + mpath_head->disk->private_data = mpath_head; + mpath_head->disk->fops = &mpath_ops; + + set_bit(GD_SUPPRESS_PART_SCAN, &mpath_head->disk->state); + + return 0; +} +EXPORT_SYMBOL_GPL(mpath_alloc_head_disk); + +void mpath_device_set_live(struct mpath_device *mpath_device) +{ + struct mpath_head *mpath_head = mpath_device->mpath_head; + int ret; + + if (!mpath_head->disk) + return; + + if (!test_and_set_bit(MPATH_HEAD_DISK_LIVE, &mpath_head->flags)) { + dev_set_drvdata(disk_to_dev(mpath_head->disk), mpath_head); + ret = device_add_disk(mpath_head->parent, mpath_head->disk, + mpath_head->disk_groups); + if (ret) { + clear_bit(MPATH_HEAD_DISK_LIVE, &mpath_head->flags); + return; + } + queue_work(mpath_wq, &mpath_head->partition_scan_work); + } +} +EXPORT_SYMBOL_GPL(mpath_device_set_live); + int mpath_head_init(struct mpath_head *mpath_head) { memset(mpath_head, 0, sizeof(*mpath_head)); @@ -44,6 +144,9 @@ int mpath_head_init(struct mpath_head *mpath_head) mutex_init(&mpath_head->lock); refcount_set(&mpath_head->refcount, 1); + INIT_WORK(&mpath_head->partition_scan_work, + multipath_partition_scan_work); + return init_srcu_struct(&mpath_head->srcu); } EXPORT_SYMBOL_GPL(mpath_head_init); -- 2.43.0