From patchwork Fri Jan 31 12:00:38 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13955305 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 197B61ACEAC for ; Fri, 31 Jan 2025 12:01:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.137.202.133 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738324889; cv=none; b=edFJKWJ0E9CA0FXWfhyzlP+4+ug5zvX8X3VSdRKl688lTaKzDCxloSy+vCoInlp/xfn5o39MhOxyFsEviHzSe05N3ZgFSL0JrD9WD2/n1CUxkIqOhIQD0XlqSEYttHAyofMlcz6/4hbiPhFPsC17ZznlT1HKG42Lko3uxg/lL5M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738324889; c=relaxed/simple; bh=J4lSUVoBnwYAsLqKJJxQMjM7uNoDKuuCFOoVXoYZE1s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VedsApWBKIfmP6kmLpKwplZmybVTkCnKFcFeZN1xTEz9ktgg/wVtLnE6/0NN0EqDVbvjFR4y4kAfkvp4lODGTfAlxiUfY6Z9G3V4D0rox6XOqv900AEso1N2NZ1JEklUYUx7eLDlwyMdnkgivbaC0aiBAO+2N6niAKHMExN1HrI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de; spf=none smtp.mailfrom=bombadil.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=hVKAMcBe; arc=none smtp.client-ip=198.137.202.133 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bombadil.srs.infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="hVKAMcBe" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=rvuWOBK87ohTQCVy86FKvupTdhgrzXF+EJeoLIFFd6A=; b=hVKAMcBeav+Dodi86uW5Pbbp4R XtLzM5XG2zZMDSN8f/E9WVJxIS8cTi97RiDUnB+fPGUJHiLYeAP0sNfS9T2bWgiM+RWqapv1lTSAh nNug2uQctqeQsE02UsMhrak6Nx5r0V7iY9LfaF2QV5qwKGsvfXGKOTG4Yh1M46ENR+xDpshuIAfMS grcwBIhNlC4wME2ke0RhyTepMXYWb43djiDMG4o27zOYKGt1bRlU+RKhHbvJ55XhX/rGrQeV4E9aT fF3JDOkEfSC3Kib2jQ3VjiWTHBshkCy4ptGL0Rhra/qwosdVq8J7ArOayubY1wypdo2PByz3l0chq dUVl8oMw==; Received: from 2a02-8389-2341-5b80-85a0-dd45-e939-a129.cable.dynamic.v6.surfer.at ([2a02:8389:2341:5b80:85a0:dd45:e939:a129] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.98 #2 (Red Hat Linux)) id 1tdpiB-0000000AZCC-0Rtp; Fri, 31 Jan 2025 12:01:27 +0000 From: Christoph Hellwig To: Jens Axboe Cc: Damien Le Moal , linux-block@vger.kernel.org Subject: [PATCH 1/4] loop: factor out a loop_assign_backing_file helper Date: Fri, 31 Jan 2025 13:00:38 +0100 Message-ID: <20250131120120.1315125-2-hch@lst.de> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250131120120.1315125-1-hch@lst.de> References: <20250131120120.1315125-1-hch@lst.de> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Split the code for setting up a backing file into a helper in preparation of adding more code to this path. Signed-off-by: Christoph Hellwig Reviewed-by: Damien Le Moal --- drivers/block/loop.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index 1ec7417c7f00..85a6aa551bb5 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -573,6 +573,14 @@ static int loop_validate_file(struct file *file, struct block_device *bdev) return 0; } +static void loop_assign_backing_file(struct loop_device *lo, struct file *file) +{ + lo->lo_backing_file = file; + lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping); + mapping_set_gfp_mask(file->f_mapping, + lo->old_gfp_mask & ~(__GFP_IO | __GFP_FS)); +} + /* * loop_change_fd switched the backing store of a loopback device to * a new file. This is useful for operating system installers to free up @@ -625,10 +633,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev, disk_force_media_change(lo->lo_disk); blk_mq_freeze_queue(lo->lo_queue); mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask); - lo->lo_backing_file = file; - lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping); - mapping_set_gfp_mask(file->f_mapping, - lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); + loop_assign_backing_file(lo, file); loop_update_dio(lo); blk_mq_unfreeze_queue(lo->lo_queue); partscan = lo->lo_flags & LO_FLAGS_PARTSCAN; @@ -1018,7 +1023,6 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode, const struct loop_config *config) { struct file *file = fget(config->fd); - struct address_space *mapping; struct queue_limits lim; int error; loff_t size; @@ -1054,8 +1058,6 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode, if (error) goto out_unlock; - mapping = file->f_mapping; - if ((config->info.lo_flags & ~LOOP_CONFIGURE_SETTABLE_FLAGS) != 0) { error = -EINVAL; goto out_unlock; @@ -1087,9 +1089,7 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode, set_disk_ro(lo->lo_disk, (lo->lo_flags & LO_FLAGS_READ_ONLY) != 0); lo->lo_device = bdev; - lo->lo_backing_file = file; - lo->old_gfp_mask = mapping_gfp_mask(mapping); - mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); + loop_assign_backing_file(lo, file); lim = queue_limits_start_update(lo->lo_queue); loop_update_limits(lo, &lim, config->block_size); From patchwork Fri Jan 31 12:00:39 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13955306 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DC0201ACEAC for ; Fri, 31 Jan 2025 12:01:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.137.202.133 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738324892; cv=none; b=PUHNLrWW1jkVJEzmg/Q8499HIW4pkjh7BhU41XXAqhGOeRg/DfFQtXafZhANQq8ZpwvqkF2FcsjjDM3/LMOyyfu5ZLBqrt8m/LS06DeT7dYu0DlIdLZhTH1toJiiNhIPNoOzCx2CarPlTsiyRrnmxBpQZnPEDQE2U29GLs2oHFU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738324892; c=relaxed/simple; bh=gV3hvvhRyHVCLJh5raFxyfBNyNILchc6clVggqClIS8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=W6460g17+XwAwwmGh2ni1JhjLScKPDbh7vmiEPM55tlm2E60guoZUBGVwLhGLWyunewqbANCO5dHURzPLnoLfhKhnNtlRceCMqhlHS7vKSJtDA9eDJCh4H8Ci0x+bI9YR4Q2T2QGPiYXd77sGTwo/FstQtnPRBmsKmyk8APPK08= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de; spf=none smtp.mailfrom=bombadil.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=qQ78BNtd; arc=none smtp.client-ip=198.137.202.133 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bombadil.srs.infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="qQ78BNtd" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=Q4ah5uWFy9kdam0/QVIr8O+KheHAShKClNujSCNGJoA=; b=qQ78BNtdArXDEEdkdfFBpBwAGQ xQqZ8UErZNOc8nhySIsNFH5DxEX/uJi3omvXx22+5xmN3zni8E2785dFzrVRXjo5rDi4gFVR1ygIk kXjvNLEr1fApqz5/DloZp4ihgpUtc/FABOq0UW3rXSo8GalF3a8HCVemIMZcVlh+wZKDwGFQtKxBL RrXTYKTO4Rz0c5mFHg0WIpFI3nECbVluMciJVauqSesSf/5JjfN4uBnwuhliZDPQ/KoKxDMMjdGaf 3ixCPnTk2ShJ4qgzpTc7Ehp7tvB/PFiQpHyGE9M1s2RajGLV5+oNhzCGe3Z4t16WIG82Jl3Uq7uYI jPREK8YA==; Received: from 2a02-8389-2341-5b80-85a0-dd45-e939-a129.cable.dynamic.v6.surfer.at ([2a02:8389:2341:5b80:85a0:dd45:e939:a129] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.98 #2 (Red Hat Linux)) id 1tdpiD-0000000AZCJ-44qS; Fri, 31 Jan 2025 12:01:30 +0000 From: Christoph Hellwig To: Jens Axboe Cc: Damien Le Moal , linux-block@vger.kernel.org Subject: [PATCH 2/4] loop: set LO_FLAGS_DIRECT_IO in loop_assign_backing_file Date: Fri, 31 Jan 2025 13:00:39 +0100 Message-ID: <20250131120120.1315125-3-hch@lst.de> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250131120120.1315125-1-hch@lst.de> References: <20250131120120.1315125-1-hch@lst.de> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Assigning LO_FLAGS_DIRECT_IO from the O_DIRECT flag is related to assigning a new backing file. Move the assignment in preparation of using the flag more and earlier. Signed-off-by: Christoph Hellwig Reviewed-by: Damien Le Moal --- drivers/block/loop.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index 85a6aa551bb5..11f483d43bf4 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -211,8 +211,6 @@ static inline void loop_update_dio(struct loop_device *lo) WARN_ON_ONCE(lo->lo_state == Lo_bound && lo->lo_queue->mq_freeze_depth == 0); - if (lo->lo_backing_file->f_flags & O_DIRECT) - lo->lo_flags |= LO_FLAGS_DIRECT_IO; if ((lo->lo_flags & LO_FLAGS_DIRECT_IO) && !lo_can_use_dio(lo)) lo->lo_flags &= ~LO_FLAGS_DIRECT_IO; @@ -579,6 +577,8 @@ static void loop_assign_backing_file(struct loop_device *lo, struct file *file) lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping); mapping_set_gfp_mask(file->f_mapping, lo->old_gfp_mask & ~(__GFP_IO | __GFP_FS)); + if (lo->lo_backing_file->f_flags & O_DIRECT) + lo->lo_flags |= LO_FLAGS_DIRECT_IO; } /* From patchwork Fri Jan 31 12:00:40 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13955307 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 233C914F70 for ; Fri, 31 Jan 2025 12:01:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.137.202.133 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738324895; cv=none; b=b/A6vDL+5+doo3a6PYVKSLbzhTnwpZcw+TRf3r8kkdJJEAqwYpqp2+a27K9RYttCv8B8uCPPR8WFvidxHKEVYxEcEej4BIby3IDnW8I1GVo90Z9kpCLcNrLs9qNxX9ZO6kneEAQcC0ma5iGkjjRp0T8twbICzHrb2uj+3zgRWU0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738324895; c=relaxed/simple; bh=RYYUut+0GgqHKiTDpL7lhZb8oIfQGwwhZG/s7kwoP4A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ljcVZ03GxoA4w78HJyxC2E3qtYkCzmhoAnJt3xgnrK4OVXqrQLZeQVSsQPRSB3vlvNr+Fv/jt/LvEh9iEsYf4dn0wxJXQxSkyuB+7HUoL2gtgcU1+ABnVMiGhiCKNMkhwyi05wZYe/Qq9n30+YnxAnnMefo8lCNrgsncN9ck7DQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de; spf=none smtp.mailfrom=bombadil.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=3UaVTVYJ; arc=none smtp.client-ip=198.137.202.133 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bombadil.srs.infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="3UaVTVYJ" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=5RI2bTOpkU7V69JWbVFJx7CTYt5ZBdgUgqPfV9kw+5c=; b=3UaVTVYJu/6c3E70tc3CqbYiZk 6aKUg9sjZLeybnqoSrDvgfSYxSrnfZADZQ8jqeFqk/zOwWOlJXIR016ssOC/Xdz14fz7TIId1D4Uc 0BwBNQLKwHkLmtZOzODKIEU1bay1lFH2Tm9g2/FuVjc/Pf7isbrngrrTcQncFVBLnYwftwbHC74h7 wG+Er329MVfEF9GyoTeYVxH4lWydHss3b2KmSIVU2tPiiYwEX33RE2ATauniAUhAr2PThVuzquaU2 y8RBF1wL4JDWC58umvmzU5X2f827NR2qyeAuvJzy3bzc26Hxw6dbDlj2S6RCs2Ohppe927fxJxFg1 us8oB8lA==; Received: from 2a02-8389-2341-5b80-85a0-dd45-e939-a129.cable.dynamic.v6.surfer.at ([2a02:8389:2341:5b80:85a0:dd45:e939:a129] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.98 #2 (Red Hat Linux)) id 1tdpiH-0000000AZCp-1vEH; Fri, 31 Jan 2025 12:01:33 +0000 From: Christoph Hellwig To: Jens Axboe Cc: Damien Le Moal , linux-block@vger.kernel.org Subject: [PATCH 3/4] loop: check in LO_FLAGS_DIRECT_IO in loop_default_blocksize Date: Fri, 31 Jan 2025 13:00:40 +0100 Message-ID: <20250131120120.1315125-4-hch@lst.de> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250131120120.1315125-1-hch@lst.de> References: <20250131120120.1315125-1-hch@lst.de> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html We can't go below the minimum direct I/O size no matter if direct I/O is enabled by passing in an O_DIRECT file descriptor or due to the explicit flag. Now that LO_FLAGS_DIRECT_IO is set earlier after assigning a backing file, loop_default_blocksize can check it instead of the O_DIRECT flag to handle both conditions. Signed-off-by: Christoph Hellwig Reviewed-by: Damien Le Moal --- drivers/block/loop.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index 11f483d43bf4..36b01c36e06b 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -979,7 +979,7 @@ static unsigned int loop_default_blocksize(struct loop_device *lo, struct block_device *backing_bdev) { /* In case of direct I/O, match underlying block size */ - if ((lo->lo_backing_file->f_flags & O_DIRECT) && backing_bdev) + if ((lo->lo_flags & LO_FLAGS_DIRECT_IO) && backing_bdev) return bdev_logical_block_size(backing_bdev); return SECTOR_SIZE; } From patchwork Fri Jan 31 12:00:41 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13955308 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 942361ACEAC for ; Fri, 31 Jan 2025 12:01:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.137.202.133 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738324899; cv=none; b=OpoEUtm/58hXCOqh4GrmSTcCyhEQtUbTawRAIKN22e8C40Q8z0FDvpw4t+MaKjlB0D9GqCiw8LUtj8A/uh8OLGVmhP4sruKaSZAxJNgLJOevvo/L1kSAg4XoZGVKsocKdx0UYmbXMxLyc1H064/IE/g4u1z9epAG9QDpZY/VaM8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738324899; c=relaxed/simple; bh=2lgQxDw3uqZE2K7qSOjpxP3MmP+4avbA7ag/P/lZPEE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=khaoqb7/XGlYOwcpucA8khkRgWiOMFBxLlg1rvVmKpJiGOO5FwlehR5qTZ/he07okcswRmnNGOIT4U3XtCaJKhytsEfVE3xF6Yy4WjepwKSCeVMdreGQc4Augw/PLNgWdl+oLTwHmA8D3Q9V6CPrgu/jj0XnrxJibzUPmWmdrWY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de; spf=none smtp.mailfrom=bombadil.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=0Fuolnep; arc=none smtp.client-ip=198.137.202.133 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bombadil.srs.infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="0Fuolnep" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=TOxmbHnPV1NuDJsjbezoPZXmY5xU80JxoXt5teTXuWI=; b=0Fuolnepn0IwlFe1sEc5aMMIJN 7HJpSRI71rntmOOoulLitZtUItSYo9KqddioT/rWxYXwJU0GJoLxcm/2HuP2TV5tzccuC+D/LWEPT zmHyUr0cUeAAaAWMHXgJC5z0sHYybLxWU08rDOh1auJo9aW+eSuGTvB2Ak0MuNK6Ed+thhFVcKedQ kfaVkDv5OyumbALEq+chJhcmS0GQcaqNbeISQt+EjxZEIeU6WI/I9HXWNMZFNfKW4XD5rFinwPr2S OFPhF4xZo7/2tUj1EVH5khLgNP99wFGioOQYIDmfq1LIJGF/Zg6Qzza5au336qsbWdXaMMKLNQFeI hherXJCw==; Received: from 2a02-8389-2341-5b80-85a0-dd45-e939-a129.cable.dynamic.v6.surfer.at ([2a02:8389:2341:5b80:85a0:dd45:e939:a129] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.98 #2 (Red Hat Linux)) id 1tdpiK-0000000AZDA-1ZBr; Fri, 31 Jan 2025 12:01:37 +0000 From: Christoph Hellwig To: Jens Axboe Cc: Damien Le Moal , linux-block@vger.kernel.org Subject: [PATCH 4/4] loop: take the file system minimum dio alignment into account Date: Fri, 31 Jan 2025 13:00:41 +0100 Message-ID: <20250131120120.1315125-5-hch@lst.de> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250131120120.1315125-1-hch@lst.de> References: <20250131120120.1315125-1-hch@lst.de> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html The loop driver currently uses the logical block size of the underlying bdev as the lower bound of the loop device block size. While this works for many cases, it fails for file systems made up of multiple devices with different logic block size (e.g. XFS with a RT device that has a larger logical block size), or when the file systems doesn't support direct I/O writes at the sector size granularity (e.g. because it does out of place writes with a file system block size larger than the sector size). Fix this by querying the minimum direct I/O alignment from statx when available. Signed-off-by: Christoph Hellwig Reviewed-by: Damien Le Moal --- drivers/block/loop.c | 60 +++++++++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index 36b01c36e06b..89352a85b704 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -54,7 +54,8 @@ struct loop_device { int lo_flags; char lo_file_name[LO_NAME_SIZE]; - struct file * lo_backing_file; + struct file *lo_backing_file; + unsigned int lo_min_dio_size; struct block_device *lo_device; gfp_t old_gfp_mask; @@ -169,29 +170,14 @@ static loff_t get_loop_size(struct loop_device *lo, struct file *file) * of backing device, and the logical block size of loop is bigger than that of * the backing device. */ -static bool lo_bdev_can_use_dio(struct loop_device *lo, - struct block_device *backing_bdev) -{ - unsigned int sb_bsize = bdev_logical_block_size(backing_bdev); - - if (queue_logical_block_size(lo->lo_queue) < sb_bsize) - return false; - if (lo->lo_offset & (sb_bsize - 1)) - return false; - return true; -} - static bool lo_can_use_dio(struct loop_device *lo) { - struct inode *inode = lo->lo_backing_file->f_mapping->host; - if (!(lo->lo_backing_file->f_mode & FMODE_CAN_ODIRECT)) return false; - - if (S_ISBLK(inode->i_mode)) - return lo_bdev_can_use_dio(lo, I_BDEV(inode)); - if (inode->i_sb->s_bdev) - return lo_bdev_can_use_dio(lo, inode->i_sb->s_bdev); + if (queue_logical_block_size(lo->lo_queue) < lo->lo_min_dio_size) + return false; + if (lo->lo_offset & (lo->lo_min_dio_size - 1)) + return false; return true; } @@ -539,6 +525,28 @@ static void loop_reread_partitions(struct loop_device *lo) __func__, lo->lo_number, lo->lo_file_name, rc); } +static unsigned int loop_query_min_dio_size(struct loop_device *lo) +{ + struct file *file = lo->lo_backing_file; + struct block_device *sb_bdev = file->f_mapping->host->i_sb->s_bdev; + struct kstat st; + + /* + * Use the minimal dio alignment of the file system if provided. + */ + if (!vfs_getattr(&file->f_path, &st, STATX_DIOALIGN, 0) && + (st.result_mask & STATX_DIOALIGN)) + return st.dio_offset_align; + + /* + * In a perfect world this wouldn't be needed, but as of Linux 6.13 only + * a handful of file systems support the STATX_DIOALIGN flag. + */ + if (sb_bdev) + return bdev_logical_block_size(sb_bdev); + return SECTOR_SIZE; +} + static inline int is_loop_device(struct file *file) { struct inode *i = file->f_mapping->host; @@ -579,6 +587,7 @@ static void loop_assign_backing_file(struct loop_device *lo, struct file *file) lo->old_gfp_mask & ~(__GFP_IO | __GFP_FS)); if (lo->lo_backing_file->f_flags & O_DIRECT) lo->lo_flags |= LO_FLAGS_DIRECT_IO; + lo->lo_min_dio_size = loop_query_min_dio_size(lo); } /* @@ -975,12 +984,11 @@ loop_set_status_from_info(struct loop_device *lo, return 0; } -static unsigned int loop_default_blocksize(struct loop_device *lo, - struct block_device *backing_bdev) +static unsigned int loop_default_blocksize(struct loop_device *lo) { - /* In case of direct I/O, match underlying block size */ - if ((lo->lo_flags & LO_FLAGS_DIRECT_IO) && backing_bdev) - return bdev_logical_block_size(backing_bdev); + /* In case of direct I/O, match underlying minimum I/O size */ + if (lo->lo_flags & LO_FLAGS_DIRECT_IO) + return lo->lo_min_dio_size; return SECTOR_SIZE; } @@ -998,7 +1006,7 @@ static void loop_update_limits(struct loop_device *lo, struct queue_limits *lim, backing_bdev = inode->i_sb->s_bdev; if (!bsize) - bsize = loop_default_blocksize(lo, backing_bdev); + bsize = loop_default_blocksize(lo); loop_get_discard_config(lo, &granularity, &max_discard_sectors);