From patchwork Thu Apr 6 15:06:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chao Yu X-Patchwork-Id: 80309 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1102105vqo; Thu, 6 Apr 2023 08:13:48 -0700 (PDT) X-Google-Smtp-Source: AKy350apwps4quDJBjZEVN/DjX8fBUtt7tlzxuMhD43smL7pG9zPs95mSVoRWgeprid88oGx1iRe X-Received: by 2002:a17:906:5298:b0:8e6:266c:d195 with SMTP id c24-20020a170906529800b008e6266cd195mr6810869ejm.38.1680794028271; Thu, 06 Apr 2023 08:13:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680794028; cv=none; d=google.com; s=arc-20160816; b=u/6yePRZkC3UZ874v1z4ILl3ZmLs06GMI9Qni4tyUpppAYzcjIUVsbv4MD2EUVkMP3 M4bEeMQRNCVBKg8LDWt0LHXnk5dpwRflCJPYhD88K29ZrJBMDr7XsNOBazzk3ipCM1t4 Fg65Sf/F9v6Jh2XQalKBLW3ILztEJeQkGE1GLFvvASaxmAIJAaQHqiCcz/ubrZM0z+j1 gemWNgr+HEIm3fqhwlpfVvKQ4j4RBBxT90KNgZPFsinL3UQYZzznjeoLdgGF+nGOJjI6 GAWZ1KtxgT70aCC1w2YKyhofvkLMmDq2GXrKY4jLhENTcl74AlcYuCAWvchzvcU+1XMJ ryvw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=SCccSYgxW3yiXpKwdMmu7fBkiFrGJ0NmL+LbXQMcX7A=; b=0V0W0ryzHTPJCKVepggFbAS9PHe5Y0Mz54RmKU5s7XlcLBEYQmixPJMXc7sgQvQg0P 3KmcmDYoarSySfrtiymmKabkDKWAS2EDNegF/AXjegPNp11H1V5Ad0P5zOENzcNZcBQa 2iK5XizhFIUBDh/TU9LHNbSeinE4IRmCz/Yf8kvRwgbuJGwQiHVOntZU16kLqr6WJHdE hxHgNydrsiUAwTmrENK+G/k07IONXE6pyxuEe2VOchAd4tJkljZ68IiktlQ5tJ3EgrFy L61nbGDuRpl9iYdNs8BkRziVgzBcPAqTHrnnHTgqL4+7UvlPBPBko+cOYrRRXCJsheEl KM2g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=TZNcy5xB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id dd18-20020a1709069b9200b009281f44dc1fsi1791046ejc.270.2023.04.06.08.13.23; Thu, 06 Apr 2023 08:13:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=TZNcy5xB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239552AbjDFPGd (ORCPT + 99 others); Thu, 6 Apr 2023 11:06:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56686 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239455AbjDFPGb (ORCPT ); Thu, 6 Apr 2023 11:06:31 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BA1E31AB; Thu, 6 Apr 2023 08:06:30 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 4BF2C618D3; Thu, 6 Apr 2023 15:06:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5A743C433D2; Thu, 6 Apr 2023 15:06:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1680793589; bh=/Qnxu3y/b9dYJicRMfW3kfoy3najo7T//JwcFvG+lLE=; h=From:To:Cc:Subject:Date:From; b=TZNcy5xBFCrr2jp9YctQlD+Z2BrBKdv6vudCxvkz8cJuGXr+3031ZlwTHdz2lS9Wo GegbTIGxoJi2BBvRgbmYlw1XUaWsQ1k8hc47ySkxYQWe0X3v2Lt2U+8vof5LrnKTio Nk356Yic+9tkc68SYCfKG77yHhfa9T+4Bx0aTH07lhO66blM4LLCZ1ExuqPT/n1qDf 8FaBV6MkK96K0/HoKnPGcq9HTdRt0PUdPK4hJqLv07SSdUI2xfpEIbyU+LNrWdieEm Q4yz+KqlhHfsOGz5gF/XtBJfFzADqr2D0FdRtZe9HaYFa+oRXW+58dVozhu77XGvLO vn6gSnGqYFi4w== From: Chao Yu To: Theodore Ts'o , Andreas Dilger Cc: linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Yu Subject: [PATCH] ext4: fix wrong calculation of minlen in ext4_trim_fs() Date: Thu, 6 Apr 2023 23:06:21 +0800 Message-Id: <20230406150621.3854298-1-chao@kernel.org> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Spam-Status: No, score=-2.5 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762440279137615899?= X-GMAIL-MSGID: =?utf-8?q?1762440279137615899?= As Ted pointed out as below: " However the minlen variable in ext4_trim_fs is in units of *clusters*. And so it gets rounded up two places. The first time is when it is converted into units of a cluster: minlen = EXT4_NUM_B2C(EXT4_SB(sb), range->minlen >> sb->s_blocksize_bits); Oh, and by the way, that first conversion is not correct as currently written in ext4_fs_trim(). It should be minlen = (range->minlen + EXT4_CLUSTER_SIZE(sb) - 1) >> (sb->s_blocksize_bits + EXT4_CLUSTER_BITS(sb)); " The reason is if range->minlen is smaller than block size of ext4, original calculation "range->minlen >> sb->s_blocksize_bits" may return zero, but since EXT4_NUM_B2C() expects a non-zero in-parameter, so it needs to round up range->minlen to cluster size directly as above. Link: https://lore.kernel.org/lkml/20230311031843.GF860405@mit.edu/ Suggested-by: Theodore Ts'o Signed-off-by: Chao Yu Reviewed-by: Jan Kara --- fs/ext4/mballoc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c index 5b2ae37a8b80..d8b9d6a83d1e 100644 --- a/fs/ext4/mballoc.c +++ b/fs/ext4/mballoc.c @@ -6478,8 +6478,8 @@ int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range) start = range->start >> sb->s_blocksize_bits; end = start + (range->len >> sb->s_blocksize_bits) - 1; - minlen = EXT4_NUM_B2C(EXT4_SB(sb), - range->minlen >> sb->s_blocksize_bits); + minlen = (range->minlen + EXT4_CLUSTER_SIZE(sb) - 1) >> + (sb->s_blocksize_bits + EXT4_CLUSTER_BITS(sb)); if (minlen > EXT4_CLUSTERS_PER_GROUP(sb) || start >= max_blks ||