From nobody Mon Nov 25 07:59:24 2024 Received: from SHSQR01.spreadtrum.com (unknown [222.66.158.135]) (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 3221828E7 for ; Tue, 29 Oct 2024 06:16:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=222.66.158.135 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730182580; cv=none; b=Hx1DvfE8BJyw9UFBpaugXMyxKtuguWMG3FcxH7N2jDeaHupFxsqRDEv/lFvIPfFiXuuBotlXXcoAxAddeOiXGj1Nndr2R/SMzZzuOz0yHW8plGrSckfA3TsNWielunIiP0FBgmSx7L8SKPHCEXeYCiFhw7Fn5vrkCvCUk6F/0N4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730182580; c=relaxed/simple; bh=oeHv6ZFt7htXvQCMWCy1DJoAuO35y4Hj2M8WxG+RyaM=; h=From:To:CC:Subject:Date:Message-ID:MIME-Version:Content-Type; b=LHqDtYJkEmZRXGG+ld+qCJ9FjUV6WLWyw9xSXUk7VMUQKTsNuF27bE8IdWffBB58n0ZV0PyzMtoigedPiyoVvlRUVHWEANQuuA78tC/wS538L6qAApM2V4cfVpNYCt842rsujSo/S8iRo9d5qW5biJGcpQAOwbLE84yy1XPfz0s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=unisoc.com; spf=pass smtp.mailfrom=unisoc.com; arc=none smtp.client-ip=222.66.158.135 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=unisoc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=unisoc.com Received: from dlp.unisoc.com ([10.29.3.86]) by SHSQR01.spreadtrum.com with ESMTP id 49T6Ffvu064824; Tue, 29 Oct 2024 14:15:41 +0800 (+08) (envelope-from Xiuhong.Wang@unisoc.com) Received: from SHDLP.spreadtrum.com (bjmbx02.spreadtrum.com [10.0.64.8]) by dlp.unisoc.com (SkyGuard) with ESMTPS id 4Xd0NT58GQz2K9xtV; Tue, 29 Oct 2024 14:15:05 +0800 (CST) Received: from tj10379pcu1.spreadtrum.com (10.5.32.15) by BJMBX02.spreadtrum.com (10.0.64.8) with Microsoft SMTP Server (TLS) id 15.0.1497.23; Tue, 29 Oct 2024 14:15:38 +0800 From: Xiuhong Wang To: , , , CC: , , , Subject: [PATCH] f2fs: fix fiemap failure issue when page size is 16KB Date: Tue, 29 Oct 2024 14:15:35 +0800 Message-ID: <20241029061535.2676673-1-xiuhong.wang@unisoc.com> X-Mailer: git-send-email 2.25.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SHCAS03.spreadtrum.com (10.0.1.207) To BJMBX02.spreadtrum.com (10.0.64.8) X-MAIL: SHSQR01.spreadtrum.com 49T6Ffvu064824 Content-Type: text/plain; charset="utf-8" After enable 16K page size, an infinite loop may occur in fiemap (fm_length=3DUINT64_MAX) on a file, such as the 16KB scratch.img during the remount operation in Android. The condition for whether fiemap continues to map is to check whether the number of bytes corresponding to the next map.m_lblk exceeds blks_to_bytes(inode,max_inode_blocks(inode)) if there are HOLE. The latter does not take into account the maximum size of a file with 16KB page size, so the loop cannot be jumped out. The following is the fail trace: When f2fs_map_blocks reaches map.m_lblk=3D3936, it needs to go to the first direct node block, so the map is 3936 + 4090 =3D 8026, The next map is the second direct node block, that is, 8026 + 4090 =3D 12116, The next map is the first indirect node block, that is, 12116 + 4090 * 4090 =3D 16740216, The next map is the second indirect node block, that is, 16740216 + 4090 * 4090 =3D 33468316, The next map is the first double indirect node block, that is, 33468316 + 4090 * 4090 * 4090 =3D 68451397316 Since map.m_lblk represents the address of a block, which is 32 bits, truncation will occur, that is, 68451397316 becomes 4026887876, and the number of bytes corresponding to the block number does not exceed blks_to_bytes(inode,max_inode_blocks(inode)), so the loop will not be jumped out. The next time, it will be considered that it should still be a double indirect node block, that is, 4026887876 + 4090 * 4090 * 4090 =3D 72444816876, which will be truncated to 3725340140, and the loop will not be jumped out. 156.374871: f2fs_map_blocks: dev =3D (254,57), ino =3D 7449, file offset = =3D 0, start blkaddr =3D 0x8e00, len =3D 0x200, flags =3D 2,seg_type =3D 8,= may_create =3D 0, multidevice =3D 0, flag =3D 1, err =3D 0 156.374916: f2fs_map_blocks: dev =3D (254,57), ino =3D 7449, file offset = =3D 512, start blkaddr =3D 0x0, len =3D 0x0, flags =3D 0 , seg_type =3D 8, = may_create =3D 0, multidevice =3D 0, flag =3D 1, err =3D 0 156.374920: f2fs_map_blocks: dev =3D (254,57), ino =3D 7449, file offset = =3D 513, start blkaddr =3D 0x0, len =3D 0x0, flags =3D 0, seg_type =3D 8, m= ay_create =3D 0, multidevice =3D 0, flag =3D 1, err =3D 0 ...... 156.385747: f2fs_map_blocks: dev =3D (254,57), ino =3D 7449, file offset = =3D 3935, start blkaddr =3D 0x0, len =3D 0x0, flags =3D 0, seg_type =3D 8, = may_create =3D 0, multidevice =3D 0, flag =3D 1, err =3D 0 156.385752: f2fs_map_blocks: dev =3D (254,57), ino =3D 7449, file offset = =3D 3936, start blkaddr =3D 0x0, len =3D 0x0, flags =3D 0, seg_type =3D 8, = may_create =3D 0, multidevice =3D 0, flag =3D 1, err =3D 0 156.385755: f2fs_map_blocks: dev =3D (254,57), ino =3D 7449, file offset = =3D 8026, start blkaddr =3D 0x0, len =3D 0x0, flags =3D 0, seg_type =3D 8, = may_create =3D 0, multidevice =3D 0, flag =3D 1, err =3D 0 156.385758: f2fs_map_blocks: dev =3D (254,57), ino =3D 7449, file offset = =3D 12116, start blkaddr =3D 0x0, len =3D 0x0, flags =3D 0, seg_type =3D 8,= may_create =3D 0, multidevice =3D 0, flag =3D 1, err =3D 0 156.385761: f2fs_map_blocks: dev =3D (254,57), ino =3D 7449, file offset = =3D 16740216, start blkaddr =3D 0x0, len =3D 0x0, flags =3D 0, seg_type =3D= 8, may_create =3D 0, multidevice =3D 0, flag =3D 1, err =3D 0 156.385764: f2fs_map_blocks: dev =3D (254,57), ino =3D 7449, file offset = =3D 33468316, start blkaddr =3D 0x0, len =3D 0x0, flags =3D 0, seg_type =3D= 8, may_create =3D 0, multidevice =3D 0, flag =3D 1, err =3D 0 156.385767: f2fs_map_blocks: dev =3D (254,57), ino =3D 7449, file offset = =3D 4026887876, start blkaddr =3D 0x0, len =3D 0x0, flags =3D 0, seg_type = =3D 8, may_create =3D 0, multidevice =3D 0, flag =3D 1, err =3D 0 156.385770: f2fs_map_blocks: dev =3D (254,57), ino =3D 7449, file offset = =3D 3725340140, start blkaddr =3D 0x0, len =3D 0x0, flags =3D 0, seg_type = =3D 8, may_create =3D 0, multidevice =3D 0, flag =3D 1, err =3D 0 156.385772: f2fs_map_blocks: dev =3D (254,57), ino =3D 7449, file offset = =3D 4026887876, start blkaddr =3D 0x0, len =3D 0x0, flags =3D 0, seg_type = =3D 8, may_create =3D 0, multidevice =3D 0, flag =3D 1, err =3D 0 156.385775: f2fs_map_blocks: dev =3D (254,57), ino =3D 7449, file offset = =3D 3725340140, start blkaddr =3D 0x0, len =3D 0x0, flags =3D 0, seg_type = =3D 8, may_create =3D 0, multidevice =3D 0, flag =3D 1, err =3D 0 Commit a6a010f5def5 ("f2fs: Restrict max filesize for 16K f2fs") has set the maximum allowed file size to (U32_MAX + 1) * F2FS_BLKSIZE, so max_file_blocks should be used here to limit it, that is, maxbytes defined above. And the max_inode_blocks function is not called by other functions except here, so cleanup it. Signed-off-by: Xiuhong Wang Signed-off-by: Zhiguo Niu Reviewed-by: Chao Yu --- fs/f2fs/data.c | 22 +--------------------- 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c index 94f7b084f601..f33cdccb3e7f 100644 --- a/fs/f2fs/data.c +++ b/fs/f2fs/data.c @@ -1901,25 +1901,6 @@ static int f2fs_xattr_fiemap(struct inode *inode, return (err < 0 ? err : 0); } =20 -static loff_t max_inode_blocks(struct inode *inode) -{ - loff_t result =3D ADDRS_PER_INODE(inode); - loff_t leaf_count =3D ADDRS_PER_BLOCK(inode); - - /* two direct node blocks */ - result +=3D (leaf_count * 2); - - /* two indirect node blocks */ - leaf_count *=3D NIDS_PER_BLOCK; - result +=3D (leaf_count * 2); - - /* one double indirect node block */ - leaf_count *=3D NIDS_PER_BLOCK; - result +=3D leaf_count; - - return result; -} - int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, u64 start, u64 len) { @@ -1992,8 +1973,7 @@ int f2fs_fiemap(struct inode *inode, struct fiemap_ex= tent_info *fieinfo, if (!compr_cluster && !(map.m_flags & F2FS_MAP_FLAGS)) { start_blk =3D next_pgofs; =20 - if (blks_to_bytes(inode, start_blk) < blks_to_bytes(inode, - max_inode_blocks(inode))) + if (blks_to_bytes(inode, start_blk) < maxbytes) goto prep_next; =20 flags |=3D FIEMAP_EXTENT_LAST; --=20 2.25.1