Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp2460514pxm; Fri, 25 Feb 2022 02:20:39 -0800 (PST) X-Google-Smtp-Source: ABdhPJx5GWisB3zeOKRZYv+nxftOEffBm7CYlpbN92h12ab8+UPSCzop9OBWA5g9m9K7KzKnV2c9 X-Received: by 2002:a17:907:60cc:b0:6d6:5e7c:4b6e with SMTP id hv12-20020a17090760cc00b006d65e7c4b6emr556574ejc.334.1645784439159; Fri, 25 Feb 2022 02:20:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645784439; cv=none; d=google.com; s=arc-20160816; b=aFv3+Tn2QeZ4zxEBKuzRXn0CWVEt4+IDIOILKs+uH8QNGF4upNmuzVrG8lGYJLUu/8 1rR2P9WfD2KPYgsniwdoPeyNpduwT1c74bCdjXis3MeDVztNb1L5gwkwgFs1ytu3lvl/ 4rQ3NtUQ9PhwBN1zHy/1+TDD8hQ4H4iuQOl6MfFXmuhH/eUodLzbVJAd8WS6c6DNTqz/ t5uII7s6nNRrT2SIyjeW0aHfI/iLgNBBcZZZ2NrtauE0B1Ni3cCmopQd8OSPjZDIhyfa /FP8dbpgwFto/RNO/0XxQM2mKGMzvMcf92UeVBadjCFfTfg11vpr41mIv/Yk9ExW3PwY HUUQ== 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; bh=zG5iBEff4qTI/C3nyp/uXD+ygACTrFtDCjRIILop5dA=; b=Y9nkstvOjqaDxmCHMkzHgsqs9qGb6JEyKlMXnisXE67RalxjujV96H9Te4Oq/tfL7o 9Tb/dxmADC+ODlbPNZdZaoPvEfqJjRkI5dxrFZWSvC1eMtdQemwpX/etF6G22o3i5PP6 hXRm0KhNw5VpTbBzFizp30IVBRiwYcobW02ReVADBQ6WV2+dD18gYtdTXaQf4JaGNLZQ l1QmHb3XzNTSyi/gdGJgI3PO7FuhJti2zKDOVGDmKVAYI9Rd1SsAZSvvH7MgJGMa6Iw7 K1PBihkvTG3oW4IyHzPhdaTBYC00OBmcRvMpSnBy710FTJR4NxAr83GH0Mi0Y1vU9DGL cEZw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 18-20020a170906101200b006d1e6ab2369si1200976ejm.641.2022.02.25.02.20.10; Fri, 25 Feb 2022 02:20:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-ext4-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; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234486AbiBYKNl (ORCPT + 99 others); Fri, 25 Feb 2022 05:13:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55240 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230206AbiBYKNk (ORCPT ); Fri, 25 Feb 2022 05:13:40 -0500 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 05CC5C7D70 for ; Fri, 25 Feb 2022 02:13:07 -0800 (PST) Received: from canpemm500005.china.huawei.com (unknown [172.30.72.55]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4K4lpm37Y1zbbvS; Fri, 25 Feb 2022 18:08:32 +0800 (CST) Received: from huawei.com (10.175.127.227) by canpemm500005.china.huawei.com (7.192.104.229) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.21; Fri, 25 Feb 2022 18:13:05 +0800 From: Zhang Yi To: CC: , , , , Subject: [PATCH v2] ext4: fix underflow in ext4_max_bitmap_size() Date: Fri, 25 Feb 2022 18:28:37 +0800 Message-ID: <20220225102837.3048196-1-yi.zhang@huawei.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.127.227] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To canpemm500005.china.huawei.com (7.192.104.229) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham 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-ext4@vger.kernel.org The same to commit 1c2d14212b15 ("ext2: Fix underflow in ext2_max_size()") in ext2 filesystem, ext4 driver has the same issue with 64K block size and ^huge_file, fix this issue the same as ext2. This patch also revert commit 75ca6ad408f4 ("ext4: fix loff_t overflow in ext4_max_bitmap_size()") because it's no longer needed. Signed-off-by: Zhang Yi --- v2->v1: use DIV_ROUND_UP_ULL instead of DIV_ROUND_UP. fs/ext4/super.c | 46 +++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/fs/ext4/super.c b/fs/ext4/super.c index c5021ca0a28a..95608c2127e7 100644 --- a/fs/ext4/super.c +++ b/fs/ext4/super.c @@ -3468,8 +3468,9 @@ static loff_t ext4_max_size(int blkbits, int has_huge_files) */ static loff_t ext4_max_bitmap_size(int bits, int has_huge_files) { - unsigned long long upper_limit, res = EXT4_NDIR_BLOCKS; + loff_t upper_limit, res = EXT4_NDIR_BLOCKS; int meta_blocks; + unsigned int ppb = 1 << (bits - 2); /* * This is calculated to be the largest file size for a dense, block @@ -3501,27 +3502,42 @@ static loff_t ext4_max_bitmap_size(int bits, int has_huge_files) } - /* indirect blocks */ - meta_blocks = 1; - /* double indirect blocks */ - meta_blocks += 1 + (1LL << (bits-2)); - /* tripple indirect blocks */ - meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2))); - - upper_limit -= meta_blocks; - upper_limit <<= bits; - + /* Compute how many blocks we can address by block tree */ res += 1LL << (bits-2); res += 1LL << (2*(bits-2)); res += 1LL << (3*(bits-2)); + /* Compute how many metadata blocks are needed */ + meta_blocks = 1; + meta_blocks += 1 + ppb; + meta_blocks += 1 + ppb + ppb * ppb; + /* Does block tree limit file size? */ + if (res + meta_blocks <= upper_limit) + goto check_lfs; + + res = upper_limit; + /* How many metadata blocks are needed for addressing upper_limit? */ + upper_limit -= EXT4_NDIR_BLOCKS; + /* indirect blocks */ + meta_blocks = 1; + upper_limit -= ppb; + /* double indirect blocks */ + if (upper_limit < ppb * ppb) { + meta_blocks += 1 + DIV_ROUND_UP_ULL(upper_limit, ppb); + res -= meta_blocks; + goto check_lfs; + } + meta_blocks += 1 + ppb; + upper_limit -= ppb * ppb; + /* tripple indirect blocks for the rest */ + meta_blocks += 1 + DIV_ROUND_UP_ULL(upper_limit, ppb) + + DIV_ROUND_UP_ULL(upper_limit, ppb*ppb); + res -= meta_blocks; +check_lfs: res <<= bits; - if (res > upper_limit) - res = upper_limit; - if (res > MAX_LFS_FILESIZE) res = MAX_LFS_FILESIZE; - return (loff_t)res; + return res; } static ext4_fsblk_t descriptor_loc(struct super_block *sb, -- 2.31.1