From patchwork Wed Aug 23 10:48:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Kara X-Patchwork-Id: 13362082 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6D81DEE49B0 for ; Wed, 23 Aug 2023 10:49:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232986AbjHWKt3 (ORCPT ); Wed, 23 Aug 2023 06:49:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34028 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233969AbjHWKt0 (ORCPT ); Wed, 23 Aug 2023 06:49:26 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AA48E10C7; Wed, 23 Aug 2023 03:49:01 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 092CA20749; Wed, 23 Aug 2023 10:48:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1692787738; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ghGCwZSpe1jjXje+QeGVdWoKisQjNMiFoMYD1zQSFBY=; b=EToHCrFPZC+jYRiOjPJJSEzuHrhJm8y7c+O2wrSJ3oeFm0jAnzhVeWA8GijB2CfZPfmfTS N9wdu8FoMOnze9mdrOdOmfHzV8YnbAghwakHHBi8F9CiVqxVzV9CTBtsXKeUXeTPP6m2xZ 0tHp26gLfdnpKFt2zrlKTkCGHHC/r58= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1692787738; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ghGCwZSpe1jjXje+QeGVdWoKisQjNMiFoMYD1zQSFBY=; b=yIDKk2PjTADxIlH55Rn9852sJTuSa297plBZXLa5z7ZgmlhQPbw/qB8FOrZjr/usscqyhU 8hW6s2pwL8feRgAg== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id EC5B913458; Wed, 23 Aug 2023 10:48:57 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id Ji0zOhnk5WQpIAAAMHmgww (envelope-from ); Wed, 23 Aug 2023 10:48:57 +0000 Received: by quack3.suse.cz (Postfix, from userid 1000) id 1384BA06E5; Wed, 23 Aug 2023 12:48:57 +0200 (CEST) From: Jan Kara To: Christian Brauner Cc: Jens Axboe , , , Christoph Hellwig , Jan Kara , Christoph Hellwig Subject: [PATCH 01/29] block: Provide bdev_open_* functions Date: Wed, 23 Aug 2023 12:48:12 +0200 Message-Id: <20230823104857.11437-1-jack@suse.cz> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230818123232.2269-1-jack@suse.cz> References: <20230818123232.2269-1-jack@suse.cz> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3956; i=jack@suse.cz; h=from:subject; bh=MNi5VBQg0vOTXvNWN3F0ujVHalAnON6dVAKobEwzhHM=; b=owEBbQGS/pANAwAIAZydqgc/ZEDZAcsmYgBk5ePvSjs3Dz/BbOshXd92Bx/rCuKlgAC1BiAzjC6c cEcj61OJATMEAAEIAB0WIQSrWdEr1p4yirVVKBycnaoHP2RA2QUCZOXj7wAKCRCcnaoHP2RA2XwBB/ 4xaRQjek+oSyG4KHVPjbpPa/gKEK35G0TrlBnSzsAh5XCfXCVWH27pjkeWbkpxEBuYL0B8c8On0ugf bZ5ZlSgllI8ixwNCNyPALN2+DIleCV5PVg7cKpvZp2318xddm9BqclJWoJKvj4/A1crTB9aER4Wd0X 1dNzeLdKk96zwGfxlLCg7ZNdkVdsZ4MpR41utLmy1+kjmoa6q9JjpXukRNmv+cE2qraoSnwfqfwn/N FfKgNeUXW/AJuRQ8IbsZcAY0gxyIzSq/APsm5yJqBS3gHztzDeJYfMPwsck7k+PCiIicz5OKgatK9k dqLeYbY2RL7QTU/ZYpZ9kz0Z0Tfwz4 X-Developer-Key: i=jack@suse.cz; a=openpgp; fpr=93C6099A142276A28BBE35D815BC833443038D8C Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org Create struct bdev_handle that contains all parameters that need to be passed to blkdev_put() and provide bdev_open_* functions that return this structure instead of plain bdev pointer. This will eventually allow us to pass one more argument to blkdev_put() (renamed to bdev_release()) without too much hassle. Acked-by: Christoph Hellwig Signed-off-by: Jan Kara Reviewed-by: Christian Brauner --- block/bdev.c | 48 ++++++++++++++++++++++++++++++++++++++++++ include/linux/blkdev.h | 10 +++++++++ 2 files changed, 58 insertions(+) diff --git a/block/bdev.c b/block/bdev.c index a20263fa27a4..fce150f9e66d 100644 --- a/block/bdev.c +++ b/block/bdev.c @@ -829,6 +829,25 @@ struct block_device *blkdev_get_by_dev(dev_t dev, blk_mode_t mode, void *holder, } EXPORT_SYMBOL(blkdev_get_by_dev); +struct bdev_handle *bdev_open_by_dev(dev_t dev, blk_mode_t mode, void *holder, + const struct blk_holder_ops *hops) +{ + struct bdev_handle *handle = kmalloc(sizeof(*handle), GFP_KERNEL); + struct block_device *bdev; + + if (!handle) + return ERR_PTR(-ENOMEM); + bdev = blkdev_get_by_dev(dev, mode, holder, hops); + if (IS_ERR(bdev)) { + kfree(handle); + return ERR_CAST(bdev); + } + handle->bdev = bdev; + handle->holder = holder; + return handle; +} +EXPORT_SYMBOL(bdev_open_by_dev); + /** * blkdev_get_by_path - open a block device by name * @path: path to the block device to open @@ -867,6 +886,28 @@ struct block_device *blkdev_get_by_path(const char *path, blk_mode_t mode, } EXPORT_SYMBOL(blkdev_get_by_path); +struct bdev_handle *bdev_open_by_path(const char *path, blk_mode_t mode, + void *holder, const struct blk_holder_ops *hops) +{ + struct bdev_handle *handle; + dev_t dev; + int error; + + error = lookup_bdev(path, &dev); + if (error) + return ERR_PTR(error); + + handle = bdev_open_by_dev(dev, mode, holder, hops); + if (!IS_ERR(handle) && (mode & BLK_OPEN_WRITE) && + bdev_read_only(handle->bdev)) { + bdev_release(handle); + return ERR_PTR(-EACCES); + } + + return handle; +} +EXPORT_SYMBOL(bdev_open_by_path); + void blkdev_put(struct block_device *bdev, void *holder) { struct gendisk *disk = bdev->bd_disk; @@ -903,6 +944,13 @@ void blkdev_put(struct block_device *bdev, void *holder) } EXPORT_SYMBOL(blkdev_put); +void bdev_release(struct bdev_handle *handle) +{ + blkdev_put(handle->bdev, handle->holder); + kfree(handle); +} +EXPORT_SYMBOL(bdev_release); + /** * lookup_bdev() - Look up a struct block_device by name. * @pathname: Name of the block device in the filesystem. diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h index fa462d48ee96..6a942ec773b6 100644 --- a/include/linux/blkdev.h +++ b/include/linux/blkdev.h @@ -1478,14 +1478,24 @@ extern const struct blk_holder_ops fs_holder_ops; #define sb_open_mode(flags) \ (BLK_OPEN_READ | (((flags) & SB_RDONLY) ? 0 : BLK_OPEN_WRITE)) +struct bdev_handle { + struct block_device *bdev; + void *holder; +}; + struct block_device *blkdev_get_by_dev(dev_t dev, blk_mode_t mode, void *holder, const struct blk_holder_ops *hops); struct block_device *blkdev_get_by_path(const char *path, blk_mode_t mode, void *holder, const struct blk_holder_ops *hops); +struct bdev_handle *bdev_open_by_dev(dev_t dev, blk_mode_t mode, void *holder, + const struct blk_holder_ops *hops); +struct bdev_handle *bdev_open_by_path(const char *path, blk_mode_t mode, + void *holder, const struct blk_holder_ops *hops); int bd_prepare_to_claim(struct block_device *bdev, void *holder, const struct blk_holder_ops *hops); void bd_abort_claiming(struct block_device *bdev, void *holder); void blkdev_put(struct block_device *bdev, void *holder); +void bdev_release(struct bdev_handle *handle); /* just for blk-cgroup, don't use elsewhere */ struct block_device *blkdev_get_no_open(dev_t dev);