From nobody Mon Oct 6 13:17:37 2025 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (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 7A3642264A8; Sun, 20 Jul 2025 20:58:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.163.156.1 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753045083; cv=none; b=CAl6Y4gQsZmH4UnpP7mMA0Dgbrft/zBDqEejO3sK7j6WfhjsMpH70Fs26kZlZBr8JJGAcmsK8IVm0OqyQYZ41froR6jOBMpOpsdB2kW44Jc3KCA4QNs/hWD0rCurgX+S4eKYxjcjc+oanHF+87PE8bzFAbYPYAqtjRSDDoBiLMY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753045083; c=relaxed/simple; bh=l6qAOXqpnKLpYhgY71vabipzsUrvP5AJE5tAAR22fDc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cJwOm0ewb7zzgPk7pvbyYYXqH9nlSOBqAF0yH5xNYi3Z28dSvLmOi/A0k1kdfF0l9nM/axLp1tC28GcrIzfES6j4NbmbMorEUEi0hvNze2Lar24lR8YT7oUktvB7j46i5GzrEiBYDKdA1UkGthjXqBz3t47N2T5xMio4iWiNKP4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com; spf=pass smtp.mailfrom=linux.ibm.com; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b=cwI6nu0X; arc=none smtp.client-ip=148.163.156.1 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="cwI6nu0X" Received: from pps.filterd (m0353729.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 56KJrJnS003936; Sun, 20 Jul 2025 20:57:44 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=pp1; bh=e9pbuHs1AKFmacSJP n9zsMkCsmz6wbHkOEKNdeI2sBk=; b=cwI6nu0Xu4znxc1DzOtfJ7vV6HncUknbm sGhMdqQFUzxRzzx5lEG6ao64Yqr2REguqH4w/ZqlObvj8CNPusEuF1zkOQDWYxU3 cJvd1vE0xkS9hjAH5O4Ux2GFIwky60exwwdGe07Glv/nPn7VQ2QKOxo68SPdQ1U8 Px3nCk9mINFUHei9bKX7dGU6CFeAveerHYRyzYIXlLaEDbapl+gfWEX3O2QPv+r4 WufR5NjioF4B3y2zyAWWLwPewJR7esC2J7VFYWVk1lYXhKFpdlPZ3MiHfLlRnOF3 JXJHheBrUkTWgAYzLlUcsBaf9bV25ovvKJRUKLnqsiaAPYtt1Igug== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 48069v5jx0-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 20 Jul 2025 20:57:43 +0000 (GMT) Received: from m0353729.ppops.net (m0353729.ppops.net [127.0.0.1]) by pps.reinject (8.18.0.8/8.18.0.8) with ESMTP id 56KKtY7J016970; Sun, 20 Jul 2025 20:57:43 GMT Received: from ppma22.wdc07v.mail.ibm.com (5c.69.3da9.ip4.static.sl-reverse.com [169.61.105.92]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 48069v5jww-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 20 Jul 2025 20:57:43 +0000 (GMT) Received: from pps.filterd (ppma22.wdc07v.mail.ibm.com [127.0.0.1]) by ppma22.wdc07v.mail.ibm.com (8.18.1.2/8.18.1.2) with ESMTP id 56KGklKJ012462; Sun, 20 Jul 2025 20:57:41 GMT Received: from smtprelay05.fra02v.mail.ibm.com ([9.218.2.225]) by ppma22.wdc07v.mail.ibm.com (PPS) with ESMTPS id 480p2yubbb-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 20 Jul 2025 20:57:41 +0000 Received: from smtpav02.fra02v.mail.ibm.com (smtpav02.fra02v.mail.ibm.com [10.20.54.101]) by smtprelay05.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 56KKvdsE38338906 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 20 Jul 2025 20:57:40 GMT Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id E3B2A20040; Sun, 20 Jul 2025 20:57:39 +0000 (GMT) Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 8BE1820043; Sun, 20 Jul 2025 20:57:37 +0000 (GMT) Received: from li-dc0c254c-257c-11b2-a85c-98b6c1322444.ibm.com (unknown [9.39.16.241]) by smtpav02.fra02v.mail.ibm.com (Postfix) with ESMTP; Sun, 20 Jul 2025 20:57:37 +0000 (GMT) From: Ojaswin Mujoo To: linux-ext4@vger.kernel.org, "Theodore Ts'o" Cc: Jan Kara , Baokun Li , Ritesh Harjani , Zhang Yi , linux-kernel@vger.kernel.org, "Darrick J . Wong" , linux-fsdevel@vger.kernel.org Subject: [RFC v4 1/7] ext4: add aligned allocation hint in mballoc Date: Mon, 21 Jul 2025 02:27:27 +0530 Message-ID: <6f38c1940a938c1b3b1f3a4980fb90f407715947.1753044253.git.ojaswin@linux.ibm.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: References: 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-TM-AS-GCONF: 00 X-Authority-Analysis: v=2.4 cv=QLdoRhLL c=1 sm=1 tr=0 ts=687d5847 cx=c_pps a=5BHTudwdYE3Te8bg5FgnPg==:117 a=5BHTudwdYE3Te8bg5FgnPg==:17 a=Wb1JkmetP80A:10 a=VnNF1IyMAAAA:8 a=jvNEtpLD9h76yb2HC5gA:9 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjUwNzIwMDE5OCBTYWx0ZWRfXwy3sU649GlLx vYDy1bMrXKyJIAVHW0cvnMAvdtThE0ZRPmsJiCVn9MbPIbxcvHhYzcOVaV0QXVlHEpIHpvaK9OL 1r92E3WBYb6nf+zh2O8AGiOntcB/VZgcFpMD781phhczBfJ2hyjZ6N6zfqO+XdRAVSCyWwNWYNJ 8hrqpjaoZf9E13/m3EBbz7eU6hFdryIjyDdV/zmCY5ecap10gPWF44ewdd7+Ar3RRSgaEHWcV8b dEdH3cy7UBHReCiwH9UUXzzUmJTUUxTeGmqMLWi6NXrjvmes8+vhwkVpeC1JCT+91PFU70hatwV S0zRDx0c2pS8OBiS7mIb20U0xABUJZg8n1FRPTy6ufiJcs9oZLM2rUXIxPhWNPtetFo5B7SXeRa akGsEyTJYR0co9OxkY0PmW48achL8AFqia/I2bxEm+8GlcCLZWCm92auaIX6Inwqf2r4MMkE X-Proofpoint-ORIG-GUID: EDWNzMp19NucH062MjYLsQOqHDWL7CaI X-Proofpoint-GUID: 1PL90zwc4rNdUwhCISN5z-w8ak7ai2UN X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1099,Hydra:6.1.9,FMLib:17.12.80.40 definitions=2025-07-20_01,2025-07-17_02,2025-03-28_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 phishscore=0 priorityscore=1501 bulkscore=0 malwarescore=0 spamscore=0 suspectscore=0 lowpriorityscore=0 mlxscore=0 mlxlogscore=999 clxscore=1011 adultscore=0 impostorscore=0 classifier=spam authscore=0 authtc=n/a authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.19.0-2505280000 definitions=main-2507200198 Content-Type: text/plain; charset="utf-8" Add support in mballoc for allocating blocks that are aligned to a certain power-of-2 offset. 1. We define a new flag EXT4_MB_ALIGNED_HINT to indicate that we want an aligned allocation. This is just a hint, mballoc tries its best to provide aligned blocks but if it can't then it'll fallback to normal allocation 2. The alignment is determined by the length of the allocation, for example if we ask for 8192 bytes, then the alignment of physical blocks will also be 8192 bytes aligned (ie 2 blocks aligned on 4k blocksize). 3. We dont yet support arbitrary alignment. For aligned writes, the length/alignment must be power of 2 in blocks, ie for 4k blocksize we can get 4k byte aligned, 8k byte aligned, 16k byte aligned ... allocation but not 12k byte aligned. 4. We use CR_POWER2_ALIGNED criteria for aligned allocation which by design allocates in an aligned manner. Since CR_POWER2_ALIGNED needs the ac->ac_g_ex.fe_len to be power of 2, thats where the restriction in point 3 above comes from. Since right now aligned allocation support is added mainly for atomic writes use case, this restriction should be fine since atomic write capable devices usually support only power of 2 alignments 5. For ease of review enabling inode preallocation support is done in upcoming patches and is disabled in this patch. Signed-off-by: Ojaswin Mujoo --- fs/ext4/ext4.h | 2 ++ fs/ext4/mballoc.c | 57 +++++++++++++++++++++++++++++++++---- include/trace/events/ext4.h | 1 + 3 files changed, 55 insertions(+), 5 deletions(-) diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h index 9ac0a7d4fa0c..7b353d1af580 100644 --- a/fs/ext4/ext4.h +++ b/fs/ext4/ext4.h @@ -222,6 +222,8 @@ enum criteria { /* Avg fragment size rb tree lookup succeeded at least once for * CR_BEST_AVAIL_LEN */ #define EXT4_MB_CR_BEST_AVAIL_LEN_OPTIMIZED 0x00020000 +/* mballoc will try to align physical start to length (aka natural alignme= nt) */ +#define EXT4_MB_HINT_ALIGNED 0x40000 =20 struct ext4_allocation_request { /* target inode for block we're allocating */ diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c index 1e98c5be4e0a..d8d9aa717a26 100644 --- a/fs/ext4/mballoc.c +++ b/fs/ext4/mballoc.c @@ -2177,8 +2177,11 @@ static void ext4_mb_use_best_found(struct ext4_alloc= ation_context *ac, * user requested originally, we store allocated * space in a special descriptor. */ - if (ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len) + if (ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len) { + /* Aligned allocation doesn't have preallocation support */ + WARN_ON(ac->ac_flags & EXT4_MB_HINT_ALIGNED); ext4_mb_new_preallocation(ac); + } =20 } =20 @@ -2814,10 +2817,15 @@ ext4_mb_regular_allocator(struct ext4_allocation_co= ntext *ac) =20 BUG_ON(ac->ac_status =3D=3D AC_STATUS_FOUND); =20 - /* first, try the goal */ - err =3D ext4_mb_find_by_goal(ac, &e4b); - if (err || ac->ac_status =3D=3D AC_STATUS_FOUND) - goto out; + /* + * first, try the goal. Skip trying goal for aligned allocations since + * goal determination logic is not alignment aware (yet) + */ + if (!(ac->ac_flags & EXT4_MB_HINT_ALIGNED)) { + err =3D ext4_mb_find_by_goal(ac, &e4b); + if (err || ac->ac_status =3D=3D AC_STATUS_FOUND) + goto out; + } =20 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY)) goto out; @@ -2861,6 +2869,16 @@ ext4_mb_regular_allocator(struct ext4_allocation_con= text *ac) repeat: for (; cr < EXT4_MB_NUM_CRS && ac->ac_status =3D=3D AC_STATUS_CONTINUE; c= r++) { ac->ac_criteria =3D cr; + + if (ac->ac_criteria > CR_POWER2_ALIGNED && + ac->ac_flags & EXT4_MB_HINT_ALIGNED && + ac->ac_g_ex.fe_len > 1) { + ext4_warning_inode( + ac->ac_inode, + "Aligned allocation not possible, using unaligned allocation"); + ac->ac_flags &=3D ~EXT4_MB_HINT_ALIGNED; + } + /* * searching for the right group start * from the goal value specified @@ -2993,6 +3011,24 @@ ext4_mb_regular_allocator(struct ext4_allocation_con= text *ac) if (!err && ac->ac_status !=3D AC_STATUS_FOUND && first_err) err =3D first_err; =20 + if (ac->ac_flags & EXT4_MB_HINT_ALIGNED && ac->ac_status =3D=3D AC_STATUS= _FOUND) { + ext4_fsblk_t start =3D ext4_grp_offs_to_block(sb, &ac->ac_b_ex); + ext4_grpblk_t len =3D EXT4_C2B(sbi, ac->ac_b_ex.fe_len); + + if (!len) { + ext4_warning_inode(ac->ac_inode, + "Expected a non zero len extent"); + ac->ac_status =3D AC_STATUS_BREAK; + goto exit; + } + + WARN_ON_ONCE(!is_power_of_2(len)); + WARN_ON_ONCE(start % len); + /* We don't support preallocation yet */ + WARN_ON_ONCE(ac->ac_b_ex.fe_len !=3D ac->ac_o_ex.fe_len); + } + + exit: mb_debug(sb, "Best len %d, origin len %d, ac_status %u, ac_flags 0x%x, cr= %d ret %d\n", ac->ac_b_ex.fe_len, ac->ac_o_ex.fe_len, ac->ac_status, ac->ac_flags, cr, err); @@ -4438,6 +4474,13 @@ ext4_mb_normalize_request(struct ext4_allocation_con= text *ac, if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC) return; =20 + /* + * caller may have strict alignment requirements. In this case, avoid + * normalization since it is not alignment aware. + */ + if (ac->ac_flags & EXT4_MB_HINT_ALIGNED) + return; + if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) { ext4_mb_normalize_group_request(ac); return ; @@ -4792,6 +4835,10 @@ ext4_mb_use_preallocated(struct ext4_allocation_cont= ext *ac) if (!(ac->ac_flags & EXT4_MB_HINT_DATA)) return false; =20 + /* using preallocated blocks is not alignment aware. */ + if (ac->ac_flags & EXT4_MB_HINT_ALIGNED) + return false; + /* * first, try per-file preallocation by searching the inode pa rbtree. * diff --git a/include/trace/events/ext4.h b/include/trace/events/ext4.h index 845451077c41..d5cec574984c 100644 --- a/include/trace/events/ext4.h +++ b/include/trace/events/ext4.h @@ -36,6 +36,7 @@ struct partial_cluster; { EXT4_MB_STREAM_ALLOC, "STREAM_ALLOC" }, \ { EXT4_MB_USE_ROOT_BLOCKS, "USE_ROOT_BLKS" }, \ { EXT4_MB_USE_RESERVED, "USE_RESV" }, \ + { EXT4_MB_HINT_ALIGNED, "HINT_ALIGNED" }, \ { EXT4_MB_STRICT_CHECK, "STRICT_CHECK" }) =20 #define show_map_flags(flags) __print_flags(flags, "|", \ --=20 2.49.0 From nobody Mon Oct 6 13:17:37 2025 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (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 CA8421AA7BF; Sun, 20 Jul 2025 20:57:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.163.156.1 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753045080; cv=none; b=LwMNRBKWdeuV0tDQqqM7J4no35RdmMmp2m/rDpEHumds+8aqD9yJaVbTq9rfUZrD6q6G8yMVLjdzJEL20gQel5K78Dp2fuCAXopKDx/QbZGZtOPpvo6lcUAVBVXlwyJ+mbQrLRX3kHtX1kAWM11agVLvP2jQbn+NcRBzsyt63Lg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753045080; c=relaxed/simple; bh=/GU7vBQ6UjTG90MT4vm51Z5Nxn6PZNIxjeSGR6/InMI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bKJuY2YKj59gyxXqg1wgDLc5SwM5OROqOezGXKedwJ1h+ibpfqlOh/XuBpyIneXfwyt71HBcYVbNy1B91PXahCReC3oPrWukwwsXV1WbImaPmXOptoOHedaSxewkDpgOkwEh+4Ub/0eQmvYHQdwAVa5evCGUomnKsyi8dx5vxic= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com; spf=pass smtp.mailfrom=linux.ibm.com; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b=RPUvnvuw; arc=none smtp.client-ip=148.163.156.1 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="RPUvnvuw" Received: from pps.filterd (m0353729.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 56KJrJnT003936; Sun, 20 Jul 2025 20:57:46 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=pp1; bh=B7QWFolN+U1nGufxS wbE6IB2XagE9Okr3SaoODqxGDQ=; b=RPUvnvuwIZte/HA2GIvJtcY56y/YX0OXY BiYbAKO6qCmSFlOPYrW6xLHTpLlx/+Do9RvymFlGtCNH/UnsYxoMCvGwHK2YnLr8 13VvRMwByDkbbvlNbGb+Hqx+yJDLbx9pYw9DXPAkz862hHSVVmh7y3jU+pun5O0q YnJn5+wLSvXCBUpDL7fS6pMis9tZbTNOutKHhBDqgF1dLKIHVfad6rvi+2f8X/UZ cazQzxyf+sFDtSTBZG6J4ojgxiph/mRZbmDzONOiib8mbuSIJ/e4yQWcB67L/NMm MkiwDXapMDWpooIXDmclQDA/acVBsWl4gHzbF//XXlyzkam4yAzKA== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 48069v5jx4-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 20 Jul 2025 20:57:45 +0000 (GMT) Received: from m0353729.ppops.net (m0353729.ppops.net [127.0.0.1]) by pps.reinject (8.18.0.8/8.18.0.8) with ESMTP id 56KKvjsa020982; Sun, 20 Jul 2025 20:57:45 GMT Received: from ppma11.dal12v.mail.ibm.com (db.9e.1632.ip4.static.sl-reverse.com [50.22.158.219]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 48069v5jx1-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 20 Jul 2025 20:57:45 +0000 (GMT) Received: from pps.filterd (ppma11.dal12v.mail.ibm.com [127.0.0.1]) by ppma11.dal12v.mail.ibm.com (8.18.1.2/8.18.1.2) with ESMTP id 56KIgIAj024744; Sun, 20 Jul 2025 20:57:44 GMT Received: from smtprelay02.fra02v.mail.ibm.com ([9.218.2.226]) by ppma11.dal12v.mail.ibm.com (PPS) with ESMTPS id 480rd22ydj-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 20 Jul 2025 20:57:44 +0000 Received: from smtpav02.fra02v.mail.ibm.com (smtpav02.fra02v.mail.ibm.com [10.20.54.101]) by smtprelay02.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 56KKvg9g49283336 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 20 Jul 2025 20:57:42 GMT Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id AC6E420043; Sun, 20 Jul 2025 20:57:42 +0000 (GMT) Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 4D2CB20040; Sun, 20 Jul 2025 20:57:40 +0000 (GMT) Received: from li-dc0c254c-257c-11b2-a85c-98b6c1322444.ibm.com (unknown [9.39.16.241]) by smtpav02.fra02v.mail.ibm.com (Postfix) with ESMTP; Sun, 20 Jul 2025 20:57:40 +0000 (GMT) From: Ojaswin Mujoo To: linux-ext4@vger.kernel.org, "Theodore Ts'o" Cc: Jan Kara , Baokun Li , Ritesh Harjani , Zhang Yi , linux-kernel@vger.kernel.org, "Darrick J . Wong" , linux-fsdevel@vger.kernel.org Subject: [RFC v4 2/7] ext4: allow inode preallocation for aligned alloc Date: Mon, 21 Jul 2025 02:27:28 +0530 Message-ID: <7dd90fa157d865b0352f15ff7cceb167d9d87f17.1753044253.git.ojaswin@linux.ibm.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: References: 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-TM-AS-GCONF: 00 X-Authority-Analysis: v=2.4 cv=QLdoRhLL c=1 sm=1 tr=0 ts=687d584a cx=c_pps a=aDMHemPKRhS1OARIsFnwRA==:117 a=aDMHemPKRhS1OARIsFnwRA==:17 a=Wb1JkmetP80A:10 a=VnNF1IyMAAAA:8 a=8OuK9DH78nEBFM-xf5kA:9 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjUwNzIwMDE5OCBTYWx0ZWRfXzB4UWaSVEl+G PA4t+aJ6EBUEX7kpeDD9ZbJXmOYz3PC6Td+JpQDSNVGmStUkQmfsvkK2CxPk57cDq8Llu3Yuh6V ffYg01j+Rv3bHP2u3mQ8u/lsRJFyyJZ9WVU/XS103e58crEZ1sf8JweEeakQb4lOHCNSIXYVy5b rJbMJTA70nW+B/naF7nTV9cstV3aPJbC2+rYgtY7Cirukt21q7zcineLqPTy6JOCwBU3627qILi NxTMhR1imR/Uauw3kju0PynCQZo1vZJ6MnwMPwa66mQWr8jhfyPQH1/eNxYbKO1UlO5HuOhWeNY tERGPrQHK+pfjSCtBW3Swu8TtBYX8oaR1li8J8r3zfFI2OolSFqhfHIxdt42VbQAeClqEXgGpAk TP+T7CfOm3LvGulI2NMZ/E9yVAWR77Pw9IiyFE/Jy1ZTA5fc+RKfH069z9E6UEjqZJ/q4FCO X-Proofpoint-ORIG-GUID: hlmlj8mqjmmu6NtPNnXN6UniW2sacPPl X-Proofpoint-GUID: CPoxY56B42-anEzAEF5v01MPK1-AwZyy X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1099,Hydra:6.1.9,FMLib:17.12.80.40 definitions=2025-07-20_01,2025-07-17_02,2025-03-28_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 phishscore=0 priorityscore=1501 bulkscore=0 malwarescore=0 spamscore=0 suspectscore=0 lowpriorityscore=0 mlxscore=0 mlxlogscore=999 clxscore=1015 adultscore=0 impostorscore=0 classifier=spam authscore=0 authtc=n/a authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.19.0-2505280000 definitions=main-2507200198 Content-Type: text/plain; charset="utf-8" Enable inode preallocation support for aligned allocations. Inode preallocation will only be used if the preallocated blocks are able to satisfy the length and alignment requirements of the allocations, else we disable preallocation for this particular allocation and proceed as usual. Disabling inode preallocation is required otherwise we might end up with overlapping preallocated ranges which can trigger a BUG() later. Further, during normalizing, we usually try to round it up to a power of 2 which can still give us aligned allocation. We also make sure not change the goal start so aligned allocation is more straightforward. If for whatever reason the goal is not power of 2 or doesn't contain the original request, then we throw a warning and proceed as normal. For now, group preallocation is disabled for aligned allocations. Signed-off-by: Ojaswin Mujoo --- fs/ext4/mballoc.c | 96 +++++++++++++++++++++++++++++++---------------- 1 file changed, 63 insertions(+), 33 deletions(-) diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c index d8d9aa717a26..090564b6e6d4 100644 --- a/fs/ext4/mballoc.c +++ b/fs/ext4/mballoc.c @@ -2178,8 +2178,6 @@ static void ext4_mb_use_best_found(struct ext4_alloca= tion_context *ac, * space in a special descriptor. */ if (ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len) { - /* Aligned allocation doesn't have preallocation support */ - WARN_ON(ac->ac_flags & EXT4_MB_HINT_ALIGNED); ext4_mb_new_preallocation(ac); } =20 @@ -3024,8 +3022,7 @@ ext4_mb_regular_allocator(struct ext4_allocation_cont= ext *ac) =20 WARN_ON_ONCE(!is_power_of_2(len)); WARN_ON_ONCE(start % len); - /* We don't support preallocation yet */ - WARN_ON_ONCE(ac->ac_b_ex.fe_len !=3D ac->ac_o_ex.fe_len); + WARN_ON_ONCE(ac->ac_b_ex.fe_len < ac->ac_o_ex.fe_len); } =20 exit: @@ -4474,13 +4471,6 @@ ext4_mb_normalize_request(struct ext4_allocation_con= text *ac, if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC) return; =20 - /* - * caller may have strict alignment requirements. In this case, avoid - * normalization since it is not alignment aware. - */ - if (ac->ac_flags & EXT4_MB_HINT_ALIGNED) - return; - if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) { ext4_mb_normalize_group_request(ac); return ; @@ -4537,6 +4527,21 @@ ext4_mb_normalize_request(struct ext4_allocation_con= text *ac, size =3D (loff_t) EXT4_C2B(sbi, ac->ac_o_ex.fe_len) << bsbits; } + + /* + * For aligned allocations, we need to ensure 2 things: + * + * 1. The start should remain same as original start so that finding + * aligned physical blocks for it is straight forward. + * + * 2. The new_size should not be less than the original len. This + * can sometimes happen due to the way we predict size above. + */ + if (ac->ac_flags & EXT4_MB_HINT_ALIGNED) { + start_off =3D ac->ac_o_ex.fe_logical << bsbits; + size =3D max_t(loff_t, size, + EXT4_C2B(sbi, ac->ac_o_ex.fe_len) << bsbits); + } size =3D size >> bsbits; start =3D start_off >> bsbits; =20 @@ -4787,32 +4792,46 @@ ext4_mb_check_group_pa(ext4_fsblk_t goal_block, } =20 /* - * check if found pa meets EXT4_MB_HINT_GOAL_ONLY + * check if found pa meets EXT4_MB_HINT_GOAL_ONLY or EXT4_MB_HINT_ALIGNED */ static bool -ext4_mb_pa_goal_check(struct ext4_allocation_context *ac, +ext4_mb_pa_check(struct ext4_allocation_context *ac, struct ext4_prealloc_space *pa) { struct ext4_sb_info *sbi =3D EXT4_SB(ac->ac_sb); ext4_fsblk_t start; =20 - if (likely(!(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))) + if (likely(!(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY || + ac->ac_flags & EXT4_MB_HINT_ALIGNED))) return true; =20 - /* - * If EXT4_MB_HINT_GOAL_ONLY is set, ac_g_ex will not be adjusted - * in ext4_mb_normalize_request and will keep same with ac_o_ex - * from ext4_mb_initialize_context. Choose ac_g_ex here to keep - * consistent with ext4_mb_find_by_goal. - */ - start =3D pa->pa_pstart + - (ac->ac_g_ex.fe_logical - pa->pa_lstart); - if (ext4_grp_offs_to_block(ac->ac_sb, &ac->ac_g_ex) !=3D start) - return false; + if (ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY) { + /* + * If EXT4_MB_HINT_GOAL_ONLY is set, ac_g_ex will not be adjusted + * in ext4_mb_normalize_request and will keep same with ac_o_ex + * from ext4_mb_initialize_context. Choose ac_g_ex here to keep + * consistent with ext4_mb_find_by_goal. + */ + start =3D pa->pa_pstart + + (ac->ac_g_ex.fe_logical - pa->pa_lstart); + if (ext4_grp_offs_to_block(ac->ac_sb, &ac->ac_g_ex) !=3D start) + return false; =20 - if (ac->ac_g_ex.fe_len > pa->pa_len - - EXT4_B2C(sbi, ac->ac_g_ex.fe_logical - pa->pa_lstart)) - return false; + if (ac->ac_g_ex.fe_len > + pa->pa_len - EXT4_B2C(sbi, ac->ac_g_ex.fe_logical - + pa->pa_lstart)) + return false; + } else if (ac->ac_flags & EXT4_MB_HINT_ALIGNED) { + start =3D pa->pa_pstart + + (ac->ac_g_ex.fe_logical - pa->pa_lstart); + if (start % EXT4_C2B(sbi, ac->ac_g_ex.fe_len)) + return false; + + if (EXT4_C2B(sbi, ac->ac_g_ex.fe_len) > + (EXT4_C2B(sbi, pa->pa_len) - + (ac->ac_g_ex.fe_logical - pa->pa_lstart))) + return false; + } =20 return true; } @@ -4835,10 +4854,6 @@ ext4_mb_use_preallocated(struct ext4_allocation_cont= ext *ac) if (!(ac->ac_flags & EXT4_MB_HINT_DATA)) return false; =20 - /* using preallocated blocks is not alignment aware. */ - if (ac->ac_flags & EXT4_MB_HINT_ALIGNED) - return false; - /* * first, try per-file preallocation by searching the inode pa rbtree. * @@ -4944,7 +4959,7 @@ ext4_mb_use_preallocated(struct ext4_allocation_conte= xt *ac) goto try_group_pa; } =20 - if (tmp_pa->pa_free && likely(ext4_mb_pa_goal_check(ac, tmp_pa))) { + if (tmp_pa->pa_free && likely(ext4_mb_pa_check(ac, tmp_pa))) { atomic_inc(&tmp_pa->pa_count); ext4_mb_use_inode_pa(ac, tmp_pa); spin_unlock(&tmp_pa->pa_lock); @@ -4979,6 +4994,19 @@ ext4_mb_use_preallocated(struct ext4_allocation_cont= ext *ac) * pa_free =3D=3D 0. */ WARN_ON_ONCE(tmp_pa->pa_free =3D=3D 0); + + /* + * If, for any reason, we reach here then we need to disable PA + * because otherwise ext4_mb_normalize_request() will try to + * allocate a new PA for this logical range where another PA + * already exists. This is not allowed and will trigger BUG_ONs. + * Hence, as a workaround we disable PA. + * + * NOTE: ideally we would want to have some logic to take care + * of the unusable PA. Maybe a more fine grained discard logic + * that could allow us to discard only specific PAs. + */ + ac->ac_flags |=3D EXT4_MB_HINT_NOPREALLOC; } spin_unlock(&tmp_pa->pa_lock); try_group_pa: @@ -5785,6 +5813,7 @@ static void ext4_mb_group_or_file(struct ext4_allocat= ion_context *ac) int bsbits =3D ac->ac_sb->s_blocksize_bits; loff_t size, isize; bool inode_pa_eligible, group_pa_eligible; + bool is_aligned =3D (ac->ac_flags & EXT4_MB_HINT_ALIGNED); =20 if (!(ac->ac_flags & EXT4_MB_HINT_DATA)) return; @@ -5792,7 +5821,8 @@ static void ext4_mb_group_or_file(struct ext4_allocat= ion_context *ac) if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY)) return; =20 - group_pa_eligible =3D sbi->s_mb_group_prealloc > 0; + /* Aligned allocation does not support group pa */ + group_pa_eligible =3D (!is_aligned && sbi->s_mb_group_prealloc > 0); inode_pa_eligible =3D true; size =3D extent_logical_end(sbi, &ac->ac_o_ex); isize =3D (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1) --=20 2.49.0 From nobody Mon Oct 6 13:17:37 2025 Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) (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 E03DC22A813; Sun, 20 Jul 2025 20:58:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.163.158.5 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753045084; cv=none; b=VF7OKMIZfzRNRlDGyoQCXSWM0CrsF2CP9nQZO5uNPP29HTpLiJv24uuXBXiL9vhLPVx6NnWxTVpDthpOFhHVowjv0F5rsSXUqu9CDzSwMCCaJHVXJY0HPf5oLwQ9a+VbwpkATmfNiM6Ku6Kuf9HjMHALLJ9iE2NWqej5/LCDssg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753045084; c=relaxed/simple; bh=dMWXZEpMPzPvqB8x+sjy03HPUuoNIXkVVXOj8G5xmpg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HW4yuei8WtM5Pz6RrlaQ0tjqd5nrerUbxyt9MLIlE0EcAO4S17BfuerZd80a3nQloXQrA37cg5yi0tTEkZ6zt7OD/TqBPBFBj9UWVJnAuhQwzRjeKQVEK4JSmrntGusT3YUXe5w+okNoQ5w2Cm73Qa3wHxTYYaA6PY3iTgALGSw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com; spf=pass smtp.mailfrom=linux.ibm.com; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b=cq//Y9k3; arc=none smtp.client-ip=148.163.158.5 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="cq//Y9k3" Received: from pps.filterd (m0356516.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 56KCTmtJ021072; Sun, 20 Jul 2025 20:57:49 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=pp1; bh=HdaDUyEXubSkBMogF soBBTRu8f8w+09MeBodlYXydAs=; b=cq//Y9k3MnPj/wcWqVYOF0DpD5teHDdgk 5NKqwPRgGsyDJVQVtiULg3xfoQO4CucYixKDekDGl3kC/m6WL1vUR2pa3TASB1W5 rdJBAgVBu3aD081CZbG+f3cOA0KB6UmBVeHFoD4uNtxfmJGKgSgxigv41QtGWdjF nDIkhxzx/bujQ83utOXJ4+vO1lWwoMGaz1B4H892Afm7g1IVmkBFxQUKQlsam4b+ ObWJKV9pS4zPdEtiT2+3XRzqOW9eRoC4SsSUuDPVDVROdCv+qEU1dhDBa/08YHpJ mNwfwzMU1+EsJkw7UWnSyU+4iF6vK5BeXZmc6Bi34PvYlUJaUu3ww== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4805hfnhsg-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 20 Jul 2025 20:57:48 +0000 (GMT) Received: from m0356516.ppops.net (m0356516.ppops.net [127.0.0.1]) by pps.reinject (8.18.0.8/8.18.0.8) with ESMTP id 56KKvmfZ020577; Sun, 20 Jul 2025 20:57:48 GMT Received: from ppma23.wdc07v.mail.ibm.com (5d.69.3da9.ip4.static.sl-reverse.com [169.61.105.93]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4805hfnhse-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 20 Jul 2025 20:57:48 +0000 (GMT) Received: from pps.filterd (ppma23.wdc07v.mail.ibm.com [127.0.0.1]) by ppma23.wdc07v.mail.ibm.com (8.18.1.2/8.18.1.2) with ESMTP id 56KH303Z005057; Sun, 20 Jul 2025 20:57:47 GMT Received: from smtprelay03.fra02v.mail.ibm.com ([9.218.2.224]) by ppma23.wdc07v.mail.ibm.com (PPS) with ESMTPS id 480u8fjcey-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 20 Jul 2025 20:57:47 +0000 Received: from smtpav02.fra02v.mail.ibm.com (smtpav02.fra02v.mail.ibm.com [10.20.54.101]) by smtprelay03.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 56KKvjE335127780 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 20 Jul 2025 20:57:45 GMT Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 5787E20043; Sun, 20 Jul 2025 20:57:45 +0000 (GMT) Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 1778D20040; Sun, 20 Jul 2025 20:57:43 +0000 (GMT) Received: from li-dc0c254c-257c-11b2-a85c-98b6c1322444.ibm.com (unknown [9.39.16.241]) by smtpav02.fra02v.mail.ibm.com (Postfix) with ESMTP; Sun, 20 Jul 2025 20:57:42 +0000 (GMT) From: Ojaswin Mujoo To: linux-ext4@vger.kernel.org, "Theodore Ts'o" Cc: Jan Kara , Baokun Li , Ritesh Harjani , Zhang Yi , linux-kernel@vger.kernel.org, "Darrick J . Wong" , linux-fsdevel@vger.kernel.org Subject: [RFC v4 3/7] ext4: support for extsize hint using FS_IOC_FS(GET/SET)XATTR Date: Mon, 21 Jul 2025 02:27:29 +0530 Message-ID: <24053b9376e369be0ffffea0d91eb22b7634aed4.1753044253.git.ojaswin@linux.ibm.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: References: 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-TM-AS-GCONF: 00 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjUwNzIwMDE5OCBTYWx0ZWRfXyPhEcTMOWmnr 8J2sY9G6vqOq4/hylG/BsRA77xDre9+NzTSkR3gHkN/B8bJPFCqxIs2kZha+rdQeQRyx+/xV1/k esWLYQdBbrzCcLN0lxh9xBpMtwyOmREq8t4GUdEroPVmSDVkcoQ5BibzTizOGdnqSQBbVdtUtZH iWM3aWp5iRQVi6aQL7r94v7FzVDdsI03hMrgdcGzWLAqlafIJ68lQ5G9u6HqCAyHfsCEhFIyfy+ K+mgGb+kn2NxpkFffdX0o9MNmUL0HO3BInDvCBKSG2uoVbXHmNALACTnoLZyvN+vBm7cPTEU7jF QjnzuQFLoVr/BJoGXnVo/NFZunqrwoFKFWI6Jkm+tTehDUKJZOzMCnTXcBfQOkoH13+YkUtXsfE oR5MpFkiEI4HgAQnnqWpW7k3/2iOVGorg1C237ijaWRPKQ/x7RrJe6qYbVa5oYe5Rro9VImV X-Proofpoint-GUID: naCah_AFQ0-zCT2pSK7zzV29cGqO6XuK X-Proofpoint-ORIG-GUID: F3UjfYlAQvjdf-9BjXQlLu6fTJ6mIMSS X-Authority-Analysis: v=2.4 cv=X9RSKHTe c=1 sm=1 tr=0 ts=687d584c cx=c_pps a=3Bg1Hr4SwmMryq2xdFQyZA==:117 a=3Bg1Hr4SwmMryq2xdFQyZA==:17 a=Wb1JkmetP80A:10 a=VnNF1IyMAAAA:8 a=Pq_v2OHg8FMu9Aes55EA:9 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1099,Hydra:6.1.9,FMLib:17.12.80.40 definitions=2025-07-20_01,2025-07-17_02,2025-03-28_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 impostorscore=0 priorityscore=1501 adultscore=0 lowpriorityscore=0 phishscore=0 malwarescore=0 clxscore=1015 mlxscore=0 spamscore=0 suspectscore=0 mlxlogscore=999 bulkscore=0 classifier=spam authscore=0 authtc=n/a authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.19.0-2505280000 definitions=main-2507200198 Content-Type: text/plain; charset="utf-8" This patch adds support for getting and setting extsize hint using FS_IOC_GETXATTR and FS_IOC_SETXATTR interface. The extsize is stored in xattr of type EXT4_XATTR_INDEX_SYSTEM. Restrictions on setting extsize: 1. extsize can't be set on files with data 2. extsize can't be set on non regular files 3. extsize hint can't be used with bigalloc (yet) 4. extsize (in blocks) should be power-of-2 for simplicity. 5. extsize must be a multiple of block size The ioctl behavior has been kept as close to the XFS equivalent as possible. Signed-off-by: Ojaswin Mujoo --- fs/ext4/ext4.h | 6 +++ fs/ext4/inode.c | 89 +++++++++++++++++++++++++++++++++++ fs/ext4/ioctl.c | 122 ++++++++++++++++++++++++++++++++++++++++++++++++ fs/ext4/super.c | 1 + 4 files changed, 218 insertions(+) diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h index 7b353d1af580..d00870cb15f2 100644 --- a/fs/ext4/ext4.h +++ b/fs/ext4/ext4.h @@ -1199,6 +1199,8 @@ struct ext4_inode_info { __u32 i_csum_seed; =20 kprojid_t i_projid; + /* The extentsize hint for the inode in blocks */ + ext4_grpblk_t i_extsize; }; =20 /* @@ -3081,6 +3083,10 @@ extern void ext4_da_update_reserve_space(struct inod= e *inode, int used, int quota_claim); extern int ext4_issue_zeroout(struct inode *inode, ext4_lblk_t lblk, ext4_fsblk_t pblk, ext4_lblk_t len); +int ext4_inode_xattr_get_extsize(struct inode *inode); +int ext4_inode_xattr_set_extsize(struct inode *inode, ext4_grpblk_t extsiz= e); +ext4_grpblk_t ext4_inode_get_extsize(struct ext4_inode_info *ei); +void ext4_inode_set_extsize(struct ext4_inode_info *ei, ext4_grpblk_t exts= ize); =20 static inline bool is_special_ino(struct super_block *sb, unsigned long in= o) { diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index 8bdf2029ebc7..664218228fd5 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -5432,6 +5432,20 @@ struct inode *__ext4_iget(struct super_block *sb, un= signed long ino, } } =20 + ret =3D ext4_inode_xattr_get_extsize(&ei->vfs_inode); + if (ret >=3D 0) { + ei->i_extsize =3D ret; + } else if (ret =3D=3D -ENODATA) { + /* extsize is not set */ + ei->i_extsize =3D 0; + } else { + ext4_error_inode( + inode, function, line, 0, + "iget: error while retrieving extsize from xattr: %ld", ret); + ret =3D -EFSCORRUPTED; + goto bad_inode; + } + EXT4_INODE_GET_CTIME(inode, raw_inode); EXT4_INODE_GET_ATIME(inode, raw_inode); EXT4_INODE_GET_MTIME(inode, raw_inode); @@ -6779,3 +6793,78 @@ vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf) sb_end_pagefault(inode->i_sb); return ret; } + +/* + * Returns positive extsize if set, 0 if not set else error + */ +ext4_grpblk_t ext4_inode_xattr_get_extsize(struct inode *inode) +{ + char *buf; + int size, ret =3D 0; + ext4_grpblk_t extsize =3D 0; + + size =3D ext4_xattr_get(inode, EXT4_XATTR_INDEX_SYSTEM, "extsize", NULL, = 0); + + if (size =3D=3D -ENODATA || size =3D=3D 0) { + return 0; + } else if (size < 0) { + ret =3D size; + goto exit; + } + + buf =3D kmalloc(size + 1, GFP_KERNEL); + if (!buf) { + ret =3D -ENOMEM; + goto exit; + } + + size =3D ext4_xattr_get(inode, EXT4_XATTR_INDEX_SYSTEM, "extsize", buf, + size); + if (size =3D=3D -ENODATA) + /* No extsize is set */ + extsize =3D 0; + else if (size < 0) + ret =3D size; + else { + buf[size] =3D '\0'; + ret =3D kstrtoint(buf, 10, &extsize); + } + + kfree(buf); +exit: + if (ret) + return ret; + return extsize; +} + +int ext4_inode_xattr_set_extsize(struct inode *inode, ext4_grpblk_t extsiz= e) +{ + int err =3D 0; + /* max value of extsize should fit within 11 chars */ + char extsize_str[11]; + + err =3D snprintf(extsize_str, 10, "%u", extsize); + if (err < 0) + return err; + + /* Try to replace the xattr if it exists, else try to create it */ + err =3D ext4_xattr_set(inode, EXT4_XATTR_INDEX_SYSTEM, "extsize", + extsize_str, strlen(extsize_str), XATTR_REPLACE); + + if (err =3D=3D -ENODATA) + err =3D ext4_xattr_set(inode, EXT4_XATTR_INDEX_SYSTEM, "extsize", + extsize_str, strlen(extsize_str), + XATTR_CREATE); + + return err; +} + +ext4_grpblk_t ext4_inode_get_extsize(struct ext4_inode_info *ei) +{ + return ei->i_extsize; +} + +void ext4_inode_set_extsize(struct ext4_inode_info *ei, ext4_grpblk_t exts= ize) +{ + ei->i_extsize =3D extsize; +} diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c index 5668a17458ae..64a394869317 100644 --- a/fs/ext4/ioctl.c +++ b/fs/ext4/ioctl.c @@ -708,6 +708,93 @@ static int ext4_ioctl_setflags(struct inode *inode, return err; } =20 +static u32 ext4_ioctl_getextsize(struct inode *inode) +{ + ext4_grpblk_t extsize; + + extsize =3D ext4_inode_get_extsize(EXT4_I(inode)); + + return (u32) extsize << inode->i_blkbits; +} + + +static int ext4_ioctl_setextsize(struct inode *inode, u32 extsize, u32 xfl= ags) +{ + int err; + ext4_grpblk_t extsize_blks =3D extsize >> inode->i_blkbits; + struct ext4_sb_info *sbi =3D EXT4_SB(inode->i_sb); + int blksize =3D 1 << inode->i_blkbits; + char *msg =3D NULL; + + if (!S_ISREG(inode->i_mode)) { + msg =3D "Cannot set extsize on non regular file"; + err =3D -EOPNOTSUPP; + goto error; + } + + /* + * We are okay with a non-zero i_size as long as there is no data. + */ + if (ext4_has_inline_data(inode) || + READ_ONCE(EXT4_I(inode)->i_disksize) || + EXT4_I(inode)->i_reserved_data_blocks) { + msg =3D "Cannot set extsize on file with data"; + err =3D -EINVAL; + goto error; + } + + if (extsize % blksize) { + msg =3D "extsize must be multiple of blocksize"; + err =3D -EINVAL; + goto error; + } + + if (sbi->s_cluster_ratio > 1) { + msg =3D "Can't use extsize hint with bigalloc"; + err =3D -EINVAL; + goto error; + } + + if ((xflags & FS_XFLAG_EXTSIZE) && extsize =3D=3D 0) { + msg =3D "fsx_extsize can't be 0 if FS_XFLAG_EXTSIZE is passed"; + err =3D -EINVAL; + goto error; + } + + if (extsize_blks > sbi->s_blocks_per_group) { + msg =3D "extsize cannot exceed number of bytes in block group"; + err =3D -EINVAL; + goto error; + } + + if (extsize && !is_power_of_2(extsize_blks)) { + msg =3D "extsize must be either power-of-2 in fs blocks or 0"; + err =3D -EINVAL; + goto error; + } + + if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { + msg =3D "extsize can't be set on non-extent based files"; + err =3D -EINVAL; + goto error; + } + + /* update the extsize in inode xattr */ + err =3D ext4_inode_xattr_set_extsize(inode, extsize_blks); + if (err < 0) + return err; + + /* Update the new extsize in the in-core inode */ + ext4_inode_set_extsize(EXT4_I(inode), extsize_blks); + return 0; + +error: + if (msg) + ext4_warning_inode(inode, "%s\n", msg); + + return err; +} + #ifdef CONFIG_QUOTA static int ext4_ioctl_setproject(struct inode *inode, __u32 projid) { @@ -985,6 +1072,7 @@ int ext4_fileattr_get(struct dentry *dentry, struct fi= leattr *fa) struct inode *inode =3D d_inode(dentry); struct ext4_inode_info *ei =3D EXT4_I(inode); u32 flags =3D ei->i_flags & EXT4_FL_USER_VISIBLE; + u32 extsize =3D 0; =20 if (S_ISREG(inode->i_mode)) flags &=3D ~FS_PROJINHERIT_FL; @@ -993,6 +1081,13 @@ int ext4_fileattr_get(struct dentry *dentry, struct f= ileattr *fa) if (ext4_has_feature_project(inode->i_sb)) fa->fsx_projid =3D from_kprojid(&init_user_ns, ei->i_projid); =20 + extsize =3D ext4_ioctl_getextsize(inode); + /* Flag is only set if extsize is non zero */ + if (extsize > 0) { + fa->fsx_extsize =3D extsize; + fa->fsx_xflags |=3D FS_XFLAG_EXTSIZE; + } + return 0; } =20 @@ -1022,6 +1117,33 @@ int ext4_fileattr_set(struct mnt_idmap *idmap, if (err) goto out; err =3D ext4_ioctl_setproject(inode, fa->fsx_projid); + if (err) + goto out; + + if (fa->fsx_xflags & FS_XFLAG_EXTSIZE) { + err =3D ext4_ioctl_setextsize(inode, fa->fsx_extsize, + fa->fsx_xflags); + if (err) + goto out; + } else if (fa->fsx_extsize =3D=3D 0) { + /* + * Even when user explicitly passes extsize=3D0 the flag is cleared in + * fileattr_set_prepare(). + */ + if (ext4_inode_get_extsize(EXT4_I(inode)) !=3D 0) { + err =3D ext4_ioctl_setextsize(inode, fa->fsx_extsize, + fa->fsx_xflags); + if (err) + goto out; + } + + } else { + /* Unexpected usage, reset extsize to 0 */ + err =3D ext4_ioctl_setextsize(inode, 0, fa->fsx_xflags); + if (err) + goto out; + fa->fsx_xflags =3D 0; + } out: return err; } diff --git a/fs/ext4/super.c b/fs/ext4/super.c index c7d39da7e733..2237cb2240f8 100644 --- a/fs/ext4/super.c +++ b/fs/ext4/super.c @@ -1409,6 +1409,7 @@ static struct inode *ext4_alloc_inode(struct super_bl= ock *sb) spin_lock_init(&ei->i_completed_io_lock); ei->i_sync_tid =3D 0; ei->i_datasync_tid =3D 0; + ei->i_extsize =3D 0; INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work); ext4_fc_init_inode(&ei->vfs_inode); spin_lock_init(&ei->i_fc_lock); --=20 2.49.0 From nobody Mon Oct 6 13:17:37 2025 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (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 A86D622172E; Sun, 20 Jul 2025 20:58:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.163.156.1 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753045082; cv=none; b=ufRh0xQlWRz23l0jHn/XtT1oJdj1ltzUASaYUHU+LA6WF8A1PNROBjwhH2ilowqXZhfdA9uBPhk6tRfpRJRgtfFxiAd0KDVqD+AWnyABMtZS9rQlvKG85N4/PyYwSfMNze12ygJPMK+N4rr3ci4MKn8f+gw/2tgR35O/HB9PrNE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753045082; c=relaxed/simple; bh=p6G53arM7YG3hIRsBRZW8bPJMmm7gC+E1ntAdgOXjh0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FMEi0ZS+t1zmwuKu6L3qtyWhRWE/FDxwGOIKz0hVATeJtKoTaw2OL9Jqo8TvomSNLap2lPlfEMcls4GN6F2XfkgmN0STorkxFvs+aL3fjZ34rA3gcjacQVtkgmNZOh3KVsQQUnJwWsp31QX3Z8EQOTVpO7ubdVWZqGZt2wiD0tQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com; spf=pass smtp.mailfrom=linux.ibm.com; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b=VPatiRD2; arc=none smtp.client-ip=148.163.156.1 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="VPatiRD2" Received: from pps.filterd (m0360083.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 56KKkZMp022328; Sun, 20 Jul 2025 20:57:51 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=pp1; bh=VAXeE+DPl5hNUGTS9 pA4mkC+e93Ylfvof8V5MeNC4K8=; b=VPatiRD2PZyjTA5L7qvchlAK4tzGmtD00 /Fp3IzAE7K/qWA5NZdvZgOOsPkJWBxzjZk+KIyLMgtEoauN1Eex3ylVlcsy4pvDZ iM6UISwJN2vwmku9B+5OYbJkFnrPb+SOceGDZ/BU9xUgoD4TzvIFFXKH74SkPk5E xz4AlWGJ4xbke4s6V835YX03kzMG3s/QGKamP7RvP0wcR1LcvxxJ1WVKGcPJLn/s zkk2dDwxJVSKbpxnW1j3zTqN8zmgBg5q8td96zePuyOoqThc4856IDp4RH6lxzVj buy4ofI3h3nq80w5PFqig5lMeYIELJYXTHA5sbs5ojilAZ7ovJPaQ== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4805uswmyt-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 20 Jul 2025 20:57:51 +0000 (GMT) Received: from m0360083.ppops.net (m0360083.ppops.net [127.0.0.1]) by pps.reinject (8.18.0.8/8.18.0.8) with ESMTP id 56KKvooj010106; Sun, 20 Jul 2025 20:57:50 GMT Received: from ppma11.dal12v.mail.ibm.com (db.9e.1632.ip4.static.sl-reverse.com [50.22.158.219]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4805uswmyr-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 20 Jul 2025 20:57:50 +0000 (GMT) Received: from pps.filterd (ppma11.dal12v.mail.ibm.com [127.0.0.1]) by ppma11.dal12v.mail.ibm.com (8.18.1.2/8.18.1.2) with ESMTP id 56KITBAf024766; Sun, 20 Jul 2025 20:57:49 GMT Received: from smtprelay01.fra02v.mail.ibm.com ([9.218.2.227]) by ppma11.dal12v.mail.ibm.com (PPS) with ESMTPS id 480rd22ydn-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 20 Jul 2025 20:57:49 +0000 Received: from smtpav02.fra02v.mail.ibm.com (smtpav02.fra02v.mail.ibm.com [10.20.54.101]) by smtprelay01.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 56KKvmbN50069992 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 20 Jul 2025 20:57:48 GMT Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 1E74020043; Sun, 20 Jul 2025 20:57:48 +0000 (GMT) Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id BACAC20040; Sun, 20 Jul 2025 20:57:45 +0000 (GMT) Received: from li-dc0c254c-257c-11b2-a85c-98b6c1322444.ibm.com (unknown [9.39.16.241]) by smtpav02.fra02v.mail.ibm.com (Postfix) with ESMTP; Sun, 20 Jul 2025 20:57:45 +0000 (GMT) From: Ojaswin Mujoo To: linux-ext4@vger.kernel.org, "Theodore Ts'o" Cc: Jan Kara , Baokun Li , Ritesh Harjani , Zhang Yi , linux-kernel@vger.kernel.org, "Darrick J . Wong" , linux-fsdevel@vger.kernel.org Subject: [RFC v4 4/7] ext4: pass lblk and len explicitly to ext4_split_extent*() Date: Mon, 21 Jul 2025 02:27:30 +0530 Message-ID: X-Mailer: git-send-email 2.49.0 In-Reply-To: References: 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-TM-AS-GCONF: 00 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjUwNzIwMDE5OCBTYWx0ZWRfX9qvckIOmhc4u kdDY6+wGYgSK4mDun3k8y39NsS0JBVXQq1+xvGHWM0+7rAk2Fw6YBCvAGLLq6loMKejOgquRjZm gkqWAtpmK2gxg92sZS/kc2TMVIexqFjU+F039r3kUhEqiwoMCopWxBxbHr88IP2kqAOuuUjlksA QHOmVnqPzWYckoUG7gD78gmgShQfKe/lLiFGcj6gos6sx2olKpJCDp01Utak5f8TIHM4iLhrq9v 1HJJVkdmvSDQtH2mSa36+IbmFU8Qpy0P4JbQHz0AK/+dpJF4PZpUM/zeRnd4VFi6fScty/DceFD 9CPjDYzDCAOn/SjcyGUTlKyNLeUn/zEZJS8qDsfTw7oN2M6RUr4GfEc5KoXDU9WhuvaTnxw8i+c tANbMFJO5bhlqt8MRx6MMQ9V+XE9a+/JNvrFcYGEexbBSkHAgzbrz2RosEAwoPbWddH+gHC7 X-Authority-Analysis: v=2.4 cv=Nd/m13D4 c=1 sm=1 tr=0 ts=687d584f cx=c_pps a=aDMHemPKRhS1OARIsFnwRA==:117 a=aDMHemPKRhS1OARIsFnwRA==:17 a=Wb1JkmetP80A:10 a=VnNF1IyMAAAA:8 a=Jo52ochBzWdcE45m7K4A:9 X-Proofpoint-ORIG-GUID: eY6voXG8wW-altNbdKSiSwXrSxzo4Ru0 X-Proofpoint-GUID: Jp1Mxt9QKFBtO6YC35X4XFNJvXwtgKGO X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1099,Hydra:6.1.9,FMLib:17.12.80.40 definitions=2025-07-20_01,2025-07-17_02,2025-03-28_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 mlxscore=0 priorityscore=1501 bulkscore=0 clxscore=1015 mlxlogscore=536 malwarescore=0 lowpriorityscore=0 suspectscore=0 adultscore=0 impostorscore=0 phishscore=0 classifier=spam authscore=0 authtc=n/a authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.19.0-2505280000 definitions=main-2507200198 Content-Type: text/plain; charset="utf-8" Since these functions only use the map to determine lblk and len of the split, pass them explicitly. This is in preparation for making them work with extent size hints cleanly. No functional change in this patch. Signed-off-by: Ojaswin Mujoo --- fs/ext4/extents.c | 57 +++++++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c index f0f155458697..3233ab89c99e 100644 --- a/fs/ext4/extents.c +++ b/fs/ext4/extents.c @@ -3350,7 +3350,8 @@ static struct ext4_ext_path *ext4_split_extent_at(han= dle_t *handle, static struct ext4_ext_path *ext4_split_extent(handle_t *handle, struct inode *inode, struct ext4_ext_path *path, - struct ext4_map_blocks *map, + ext4_lblk_t lblk, + unsigned int len, int split_flag, int flags, unsigned int *allocated) { @@ -3366,7 +3367,7 @@ static struct ext4_ext_path *ext4_split_extent(handle= _t *handle, ee_len =3D ext4_ext_get_actual_len(ex); unwritten =3D ext4_ext_is_unwritten(ex); =20 - if (map->m_lblk + map->m_len < ee_block + ee_len) { + if (lblk + len < ee_block + ee_len) { split_flag1 =3D split_flag & EXT4_EXT_MAY_ZEROOUT; flags1 =3D flags | EXT4_GET_BLOCKS_PRE_IO; if (unwritten) @@ -3375,28 +3376,28 @@ static struct ext4_ext_path *ext4_split_extent(hand= le_t *handle, if (split_flag & EXT4_EXT_DATA_VALID2) split_flag1 |=3D EXT4_EXT_DATA_VALID1; path =3D ext4_split_extent_at(handle, inode, path, - map->m_lblk + map->m_len, split_flag1, flags1); + lblk + len, split_flag1, flags1); if (IS_ERR(path)) return path; /* * Update path is required because previous ext4_split_extent_at * may result in split of original leaf or extent zeroout. */ - path =3D ext4_find_extent(inode, map->m_lblk, path, flags); + path =3D ext4_find_extent(inode, lblk, path, flags); if (IS_ERR(path)) return path; depth =3D ext_depth(inode); ex =3D path[depth].p_ext; if (!ex) { EXT4_ERROR_INODE(inode, "unexpected hole at %lu", - (unsigned long) map->m_lblk); + (unsigned long) lblk); ext4_free_ext_path(path); return ERR_PTR(-EFSCORRUPTED); } unwritten =3D ext4_ext_is_unwritten(ex); } =20 - if (map->m_lblk >=3D ee_block) { + if (lblk >=3D ee_block) { split_flag1 =3D split_flag & EXT4_EXT_DATA_VALID2; if (unwritten) { split_flag1 |=3D EXT4_EXT_MARK_UNWRIT1; @@ -3404,16 +3405,16 @@ static struct ext4_ext_path *ext4_split_extent(hand= le_t *handle, EXT4_EXT_MARK_UNWRIT2); } path =3D ext4_split_extent_at(handle, inode, path, - map->m_lblk, split_flag1, flags); + lblk, split_flag1, flags); if (IS_ERR(path)) return path; } =20 if (allocated) { - if (map->m_lblk + map->m_len > ee_block + ee_len) - *allocated =3D ee_len - (map->m_lblk - ee_block); + if (lblk + len > ee_block + ee_len) + *allocated =3D ee_len - (lblk - ee_block); else - *allocated =3D map->m_len; + *allocated =3D len; } ext4_ext_show_leaf(inode, path); return path; @@ -3661,8 +3662,8 @@ ext4_ext_convert_to_initialized(handle_t *handle, str= uct inode *inode, } =20 fallback: - path =3D ext4_split_extent(handle, inode, path, &split_map, split_flag, - flags, NULL); + path =3D ext4_split_extent(handle, inode, path, split_map.m_lblk, + split_map.m_len, split_flag, flags, NULL); if (IS_ERR(path)) return path; out: @@ -3702,11 +3703,11 @@ ext4_ext_convert_to_initialized(handle_t *handle, s= truct inode *inode, * allocated pointer. Return an extent path pointer on success, or an error * pointer on failure. */ -static struct ext4_ext_path *ext4_split_convert_extents(handle_t *handle, - struct inode *inode, - struct ext4_map_blocks *map, - struct ext4_ext_path *path, - int flags, unsigned int *allocated) +static struct ext4_ext_path * +ext4_split_convert_extents(handle_t *handle, struct inode *inode, + ext4_lblk_t lblk, unsigned int len, + struct ext4_ext_path *path, int flags, + unsigned int *allocated) { ext4_lblk_t eof_block; ext4_lblk_t ee_block; @@ -3715,12 +3716,12 @@ static struct ext4_ext_path *ext4_split_convert_ext= ents(handle_t *handle, int split_flag =3D 0, depth; =20 ext_debug(inode, "logical block %llu, max_blocks %u\n", - (unsigned long long)map->m_lblk, map->m_len); + (unsigned long long)lblk, len); =20 eof_block =3D (EXT4_I(inode)->i_disksize + inode->i_sb->s_blocksize - 1) >> inode->i_sb->s_blocksize_bits; - if (eof_block < map->m_lblk + map->m_len) - eof_block =3D map->m_lblk + map->m_len; + if (eof_block < lblk + len) + eof_block =3D lblk + len; /* * It is safe to convert extent to initialized via explicit * zeroout only if extent is fully inside i_size or new_size. @@ -3740,8 +3741,8 @@ static struct ext4_ext_path *ext4_split_convert_exten= ts(handle_t *handle, split_flag |=3D (EXT4_EXT_MARK_UNWRIT2 | EXT4_EXT_DATA_VALID2); } flags |=3D EXT4_GET_BLOCKS_PRE_IO; - return ext4_split_extent(handle, inode, path, map, split_flag, flags, - allocated); + return ext4_split_extent(handle, inode, path, lblk, len, split_flag, + flags, allocated); } =20 static struct ext4_ext_path * @@ -3776,7 +3777,7 @@ ext4_convert_unwritten_extents_endio(handle_t *handle= , struct inode *inode, inode->i_ino, (unsigned long long)ee_block, ee_len, (unsigned long long)map->m_lblk, map->m_len); #endif - path =3D ext4_split_convert_extents(handle, inode, map, path, + path =3D ext4_split_convert_extents(handle, inode, map->m_lblk, map->m_l= en, path, EXT4_GET_BLOCKS_CONVERT, NULL); if (IS_ERR(path)) return path; @@ -3840,8 +3841,9 @@ convert_initialized_extent(handle_t *handle, struct i= node *inode, (unsigned long long)ee_block, ee_len); =20 if (ee_block !=3D map->m_lblk || ee_len > map->m_len) { - path =3D ext4_split_convert_extents(handle, inode, map, path, - EXT4_GET_BLOCKS_CONVERT_UNWRITTEN, NULL); + path =3D ext4_split_convert_extents( + handle, inode, map->m_lblk, map->m_len, path, + EXT4_GET_BLOCKS_CONVERT_UNWRITTEN, NULL); if (IS_ERR(path)) return path; =20 @@ -3912,8 +3914,9 @@ ext4_ext_handle_unwritten_extents(handle_t *handle, s= truct inode *inode, =20 /* get_block() before submitting IO, split the extent */ if (flags & EXT4_GET_BLOCKS_PRE_IO) { - path =3D ext4_split_convert_extents(handle, inode, map, path, - flags | EXT4_GET_BLOCKS_CONVERT, allocated); + path =3D ext4_split_convert_extents( + handle, inode, map->m_lblk, map->m_len, path, + flags | EXT4_GET_BLOCKS_CONVERT, allocated); if (IS_ERR(path)) return path; /* --=20 2.49.0 From nobody Mon Oct 6 13:17:37 2025 Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) (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 76336223DDD; Sun, 20 Jul 2025 20:58:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.163.158.5 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753045097; cv=none; b=po34pnVGvyuj1U/Es0Eo+v9q8kpp1cb5O0R71t77CJ3KwSIzpmNOBjmD7Cxfcsa+gf1cvtNRctRLO8pZuYGwc5S+Nf3K7CP77GDhiew6bfEjDpxTGXficgcsVyhhZ7B6wd7m55lQ/egUvi/XLQQ6QMzTNIH07hq9gqpmatraKh8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753045097; c=relaxed/simple; bh=PTTFrl6Cd9hxxNHnWudsPqS4EzDCkfuCyCz6kF0pSHw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aMIIMSa7DYFexvZHlCDcJodBtynXT1k6PrN8FpzT2ISP70UNPui0YqHefCLHRWYqSV0d7wIbB47ZgoZaUAyctdSk/mJnDvGCynLzu9kFmm5jZfbaXAMYjNcDuZzJDyfs/sqBHH1jfsaREDrP4auI889RCKWyVfDawBSwJ/p27X8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com; spf=pass smtp.mailfrom=linux.ibm.com; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b=Nw6Iyvdr; arc=none smtp.client-ip=148.163.158.5 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="Nw6Iyvdr" Received: from pps.filterd (m0360072.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 56KAdMS2006887; Sun, 20 Jul 2025 20:57:55 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=pp1; bh=fuhU0cfDIvt1XI5VJ H1HL1G5dpQMSzc2Xe57uoNNAgs=; b=Nw6Iyvdr7uI1ogkP4m+EAYz/sZdvlUQCd 3RGCVj7GkBkzzHSgynCxZ7vW2vH1lq394Y/8MTFijq5B/TdMtO9JKllwNEG9fjAx CAkVaDa6zyvt13hNN2ndv0tog5T1P2TK9fZt8Igm5qADVtzjT39IK39Uf1YyC7lq E70m0FrabVWUerDOcOHSxBNDkuzdloTd022GCMMpGTJGBVOepJh4sxMPjkG54HCA OZzKINNjKrrZIASj6MjuiJJEjp6l6gIOT5z86Z3IGjtXfE2HzULNF4jrHZ2ZmIt6 MwaqcoeaUqzJdH52lfsyfNX9cyhmsRGPyh2sQWNI7WpK9aERAHINQ== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4805uqngmq-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 20 Jul 2025 20:57:54 +0000 (GMT) Received: from m0360072.ppops.net (m0360072.ppops.net [127.0.0.1]) by pps.reinject (8.18.0.8/8.18.0.8) with ESMTP id 56KKsiHI031542; Sun, 20 Jul 2025 20:57:54 GMT Received: from ppma11.dal12v.mail.ibm.com (db.9e.1632.ip4.static.sl-reverse.com [50.22.158.219]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4805uqngmm-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 20 Jul 2025 20:57:53 +0000 (GMT) Received: from pps.filterd (ppma11.dal12v.mail.ibm.com [127.0.0.1]) by ppma11.dal12v.mail.ibm.com (8.18.1.2/8.18.1.2) with ESMTP id 56KITBAh024766; Sun, 20 Jul 2025 20:57:53 GMT Received: from smtprelay07.fra02v.mail.ibm.com ([9.218.2.229]) by ppma11.dal12v.mail.ibm.com (PPS) with ESMTPS id 480rd22yds-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 20 Jul 2025 20:57:52 +0000 Received: from smtpav02.fra02v.mail.ibm.com (smtpav02.fra02v.mail.ibm.com [10.20.54.101]) by smtprelay07.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 56KKvpuQ35651884 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 20 Jul 2025 20:57:51 GMT Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 1653220043; Sun, 20 Jul 2025 20:57:51 +0000 (GMT) Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 80E8520040; Sun, 20 Jul 2025 20:57:48 +0000 (GMT) Received: from li-dc0c254c-257c-11b2-a85c-98b6c1322444.ibm.com (unknown [9.39.16.241]) by smtpav02.fra02v.mail.ibm.com (Postfix) with ESMTP; Sun, 20 Jul 2025 20:57:48 +0000 (GMT) From: Ojaswin Mujoo To: linux-ext4@vger.kernel.org, "Theodore Ts'o" Cc: Jan Kara , Baokun Li , Ritesh Harjani , Zhang Yi , linux-kernel@vger.kernel.org, "Darrick J . Wong" , linux-fsdevel@vger.kernel.org Subject: [RFC v4 5/7] ext4: add extsize hint support Date: Mon, 21 Jul 2025 02:27:31 +0530 Message-ID: <2dbc12e409a5a951f199caa2e9475b0764fac238.1753044253.git.ojaswin@linux.ibm.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: References: 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-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: ZRsnN8uMegDhtn5EjeJ_OHjcUKMe1qPb X-Proofpoint-Spam-Details-Enc: AW1haW4tMjUwNzIwMDE5OCBTYWx0ZWRfX1R4Pt+s9o4Kv XNdQZ63Q4NNTT910ZZK3D4Rb1lFU9FfkoH8ajIgOb2DLM+5ZZbCUzjxUm5BtvL1+8/H1x9E5J8r 5D7hcpnmyyPDLhqEn96j39DBtxSsOFswS3vViGxY5KBINp21HUsOuSFTYebimmAz5gBTtnXlDeI bYYtWCQZr8IorTJiBxpwmXLrFFMyJrycYDwm57M2fN40CrBEk/b0uRvbp6Oj7bNsQHo7u/fjURA PISLLRRrmivGUNMxtvsbc65RmhZtiz2Jlm6gs5HpdtCBgkC3cW3937ioL7CqH16xdmnIdLG9+Zo m40Yz/L+rLwvKu6djColloD8iBYs/LAR1vfAK2T0LeelnDcK2C5MMh76i+OLKTXz6YwtL6+hi1C zJQWTAXKwu3/SLEnlDQK93zrrqyi1SQgj0Qp9cWynFYXyFaWVFxe6DW5sL/P6aD0rFniGTTr X-Authority-Analysis: v=2.4 cv=dpnbC0g4 c=1 sm=1 tr=0 ts=687d5852 cx=c_pps a=aDMHemPKRhS1OARIsFnwRA==:117 a=aDMHemPKRhS1OARIsFnwRA==:17 a=Wb1JkmetP80A:10 a=VnNF1IyMAAAA:8 a=UwJ6RSJUFbryGgyKpEoA:9 X-Proofpoint-GUID: uQ7f2q9NjL_iC_MvJjHECDtOtAHeyEGZ X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1099,Hydra:6.1.9,FMLib:17.12.80.40 definitions=2025-07-20_01,2025-07-17_02,2025-03-28_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 impostorscore=0 suspectscore=0 adultscore=0 phishscore=0 lowpriorityscore=0 priorityscore=1501 bulkscore=0 mlxscore=0 malwarescore=0 mlxlogscore=999 clxscore=1015 classifier=spam authscore=0 authtc=n/a authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.19.0-2505280000 definitions=main-2507200198 Content-Type: text/plain; charset="utf-8" Now that the ioctl is in place, add the underlying infrastructure to support extent size hints. ** MOTIVATION ** 1. This feature allows us to ask the allocator for blocks that are logically AS WELL AS physically aligned to an extent size hint (aka extsize), that is generally a power of 2. 2. This means both start and the length of the physical and logical range should be aligned to the extsize. 3. This sets up the infra we'll eventually need for supporting non-torn/atomic writes that need to follow a certain alignment as required by hardware. 4. This can also be extent to other use cases like stripe alignment ** DESIGN NOTES ** * Physical Alignment * 1. Since the extsize is always a power-of-2 (for now) in fs blocks, we leverage CR_POWER2_ALIGNED allocation to get the blocks. This ensures the blocks are physically aligned 2. Since this is just a hint, incase we are not able to get any aligned blocks we simply drop back to non aligned allocation. * Logical Alignment * The flow of extsize aligned allocation with buffered and direct IO: +--------------------------------------------------------+ | Buffered IO | +--------------------------------------------------------+ | ext4_map_blocks() call with extsize allocation | +--------------------------------------------------------+ | +--------------------------------------------+ | Adjust lblk and len to align to extsize | +--------------------------------------------+ | +--------------------------------------------------------+ |Pre-existing written/unwritten blocks in extsize range? | +--------------------------+-----------------------------+ YES NO | | +---------------v---------------+ +----------------v--------------+ | Covers orig range? | | Allocate extsize range | +---------------+---------------+ +----------------+--------------+ | | | YES NO | | | +--------------v--------------+ +--------v-------+ +-------v---------+ | Mark allocated extent as | | Return blocks | | Fallback to | | unwritten | +----------------+ | non-extsize | +--------------+--------------+ | allocation | | +-----------------+ +--------------v--------------+ | Insert extsize extent | | into tree | +--------------+--------------+ | +--------------v--------------+ | Return allocated blocks | +-----------------------------+ +--------------------------------------------+ | During writeback: | +--------------------------------------------+ | Use PRE_IO to split only the dirty extent | +--------------------------------------------+ +--------------------------------------------+ | After IO: | +--------------------------------------------+ | Convert the extent under IO to written | +--------------------------------------------+ Same flow for direct IO: +----------------------------------------------------------------------+ | Direct IO | +----------------------------------------------------------------------+ | ext4_map_blocks() called with extsize allocation and PRE-IO | +----------------------------------------------------------------------+ | +----------------------------------------------------------------------+ | Adjust lblk and len to align to extsize | +----------------------------------------------------------------------+ | +----------------------------------------------------------------------+ | Pre-existing written blocks in extsize range? | +----------------------------------+-----------------------------------+ YES NO | | +---------v----------+ +------------------v-----------------+ | Covers orig range? | | Unwritten blocks in extsize range? | +---------+----------+ +------------------+-----------------+ | | | | YES NO YES NO | | | | +-------v----+ +-----v--------+ +----------v----------+ +-------v----------+ | Return | | Fallback to | | Call ext4_ext_map_ | | Allocate extsize | | blocks | | non-extsize | | blocks() ->ext4_ext | | range | +------------+ | allocation | | _handle_unwritten_ | +-------+----------+ +--------------+ | extents() | | +----------+----------+ +-------v----------+ | | Mark complete | +----------v----------+ | range unwritten | | Split orig range | | & insert in | | from bigger | | tree | | unwritten extent | +-------+----------+ +----------+----------+ | | +-------v----------+ +----------v----------+ | Split orig range | | Mark split extent | | from bigger | | as unwritten | | allocated extent | +----------+----------+ +-------+----------+ | | +----------v----------+ +-------v----------+ | Return split extent | | Mark split extent| | to user | | as unwritten | +---------------------+ +-------+----------+ | +-------v----------+ | Return split | | extent to user | +------------------+ +--------------------------------------------+ | After IO: | +--------------------------------------------+ | Convert the extent under IO to written | +--------------------------------------------+ ** IMPLEMENTATION NOTES ** * Callers of ext4_map_blocks work under the assumption that ext4_map_blocks will always only return as much as requested or less but now we might end up allocating more so make changes to ext4_map_blocks to make sure we adjust the allocated map to only return as much as user requested. * Further, we now maintain 2 maps in ext4_map_blocks - the original map and the extsize map that is used when extsize hint allocation is taking place. We also pass these 2 maps down because some functions might now need information of the original map as well as the extsize map. * For example, when we go for direct IO and there's a hole in the orig range requested, we allocate based on extsize range and then split the bigger unwritten extent onto smaller unwritten extents based on orig range. (Needed so we dont have to split after IO). For this, we need the information of extsize range as well as orig range hence 2 maps. * Since now we allocate more than the user requested, to avoid stale data exposure, we mark the bigger extsize extent as unwritten and then use the similar flow of dioread_nolock to only mark the extent under write as written. * We disable extsize hints when writes are beyond EOF (for now) * When extsize is set on an inode, we drop to no delalloc allocations similar to XFS. Signed-off-by: Ojaswin Mujoo --- fs/ext4/ext4.h | 7 +- fs/ext4/ext4_jbd2.h | 15 ++ fs/ext4/extents.c | 174 +++++++++++++++++++-- fs/ext4/inode.c | 359 +++++++++++++++++++++++++++++++++++++++----- 4 files changed, 497 insertions(+), 58 deletions(-) diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h index d00870cb15f2..4b69326a0f2f 100644 --- a/fs/ext4/ext4.h +++ b/fs/ext4/ext4.h @@ -746,6 +746,7 @@ enum { * Look EXT4_MAP_QUERY_LAST_IN_LEAF. */ #define EXT4_GET_BLOCKS_QUERY_LAST_IN_LEAF 0x1000 +#define EXT4_GET_BLOCKS_EXTSIZE 0x2000 =20 /* * The bit position of these flags must not overlap with any of the @@ -765,7 +766,8 @@ enum { */ #define EXT4_EX_QUERY_FILTER (EXT4_EX_NOCACHE | EXT4_EX_FORCE_CACHE |\ EXT4_EX_NOFAIL |\ - EXT4_GET_BLOCKS_QUERY_LAST_IN_LEAF) + EXT4_GET_BLOCKS_QUERY_LAST_IN_LEAF |\ + EXT4_GET_BLOCKS_EXTSIZE) =20 /* * Flags used by ext4_free_blocks @@ -3755,7 +3757,8 @@ struct ext4_extent; extern void ext4_ext_tree_init(handle_t *handle, struct inode *inode); extern int ext4_ext_index_trans_blocks(struct inode *inode, int extents); extern int ext4_ext_map_blocks(handle_t *handle, struct inode *inode, - struct ext4_map_blocks *map, int flags); + struct ext4_map_blocks *orig_map, + struct ext4_map_blocks *extsize_map, int flags); extern int ext4_ext_truncate(handle_t *, struct inode *); extern int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start, ext4_lblk_t end); diff --git a/fs/ext4/ext4_jbd2.h b/fs/ext4/ext4_jbd2.h index 63d17c5201b5..dbac8d4f7f78 100644 --- a/fs/ext4/ext4_jbd2.h +++ b/fs/ext4/ext4_jbd2.h @@ -458,4 +458,19 @@ static inline int ext4_journal_destroy(struct ext4_sb_= info *sbi, journal_t *jour return err; } =20 +static inline int ext4_should_use_extsize(struct inode *inode) +{ + if (!S_ISREG(inode->i_mode)) + return 0; + if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) + return 0; + return (ext4_inode_get_extsize(EXT4_I(inode)) > 0); +} + +static inline int ext4_should_use_unwrit_extents(struct inode *inode) +{ + return (ext4_should_dioread_nolock(inode) || + ext4_should_use_extsize(inode)); +} + #endif /* _EXT4_JBD2_H */ diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c index 3233ab89c99e..8ea8b03a4a16 100644 --- a/fs/ext4/extents.c +++ b/fs/ext4/extents.c @@ -3892,15 +3892,24 @@ convert_initialized_extent(handle_t *handle, struct= inode *inode, =20 static struct ext4_ext_path * ext4_ext_handle_unwritten_extents(handle_t *handle, struct inode *inode, - struct ext4_map_blocks *map, + struct ext4_map_blocks *orig_map, + struct ext4_map_blocks *extsize_map, struct ext4_ext_path *path, int flags, unsigned int *allocated, ext4_fsblk_t newblock) { + struct ext4_map_blocks *map; int err =3D 0; =20 - ext_debug(inode, "logical block %llu, max_blocks %u, flags 0x%x, allocate= d %u\n", - (unsigned long long)map->m_lblk, map->m_len, flags, - *allocated); + if (flags & EXT4_GET_BLOCKS_EXTSIZE) { + BUG_ON(extsize_map =3D=3D NULL); + map =3D extsize_map; + } else + map =3D orig_map; + + ext_debug( + inode, + "logical block %llu, max_blocks %u, flags 0x%x, allocated %u\n", + (unsigned long long)map->m_lblk, map->m_len, flags, *allocated); ext4_ext_show_leaf(inode, path); =20 /* @@ -3909,13 +3918,14 @@ ext4_ext_handle_unwritten_extents(handle_t *handle,= struct inode *inode, */ flags |=3D EXT4_GET_BLOCKS_METADATA_NOFAIL; =20 - trace_ext4_ext_handle_unwritten_extents(inode, map, flags, - *allocated, newblock); + trace_ext4_ext_handle_unwritten_extents(inode, map, flags, *allocated, + newblock); =20 /* get_block() before submitting IO, split the extent */ if (flags & EXT4_GET_BLOCKS_PRE_IO) { + /* Split should always happen based on original mapping */ path =3D ext4_split_convert_extents( - handle, inode, map->m_lblk, map->m_len, path, + handle, inode, orig_map->m_lblk, orig_map->m_len, path, flags | EXT4_GET_BLOCKS_CONVERT, allocated); if (IS_ERR(path)) return path; @@ -3930,11 +3940,19 @@ ext4_ext_handle_unwritten_extents(handle_t *handle,= struct inode *inode, err =3D -EFSCORRUPTED; goto errout; } + + /* + * For extsize case we need to adjust lblk to start of split + * extent because the m_len will be set to len of split extent. + * No change for non extsize case + */ + map->m_lblk =3D orig_map->m_lblk; map->m_flags |=3D EXT4_MAP_UNWRITTEN; goto out; } /* IO end_io complete, convert the filled extent to written */ if (flags & EXT4_GET_BLOCKS_CONVERT) { + BUG_ON(map =3D=3D extsize_map); path =3D ext4_convert_unwritten_extents_endio(handle, inode, map, path); if (IS_ERR(path)) @@ -4192,7 +4210,8 @@ static ext4_lblk_t ext4_ext_determine_insert_hole(str= uct inode *inode, * return < 0, error case. */ int ext4_ext_map_blocks(handle_t *handle, struct inode *inode, - struct ext4_map_blocks *map, int flags) + struct ext4_map_blocks *orig_map, + struct ext4_map_blocks *extsize_map, int flags) { struct ext4_ext_path *path =3D NULL; struct ext4_extent newex, *ex, ex2; @@ -4203,6 +4222,17 @@ int ext4_ext_map_blocks(handle_t *handle, struct ino= de *inode, unsigned int allocated_clusters =3D 0; struct ext4_allocation_request ar; ext4_lblk_t cluster_offset; + struct ext4_map_blocks *map; +#ifdef CONFIG_EXT4_DEBUG + struct ext4_ext_path *test_path =3D NULL; +#endif + + if (flags & EXT4_GET_BLOCKS_EXTSIZE) { + BUG_ON(extsize_map =3D=3D NULL); + map =3D extsize_map; + } else + map =3D orig_map; + =20 ext_debug(inode, "blocks %u/%u requested\n", map->m_lblk, map->m_len); trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags); @@ -4259,6 +4289,7 @@ int ext4_ext_map_blocks(handle_t *handle, struct inod= e *inode, */ if ((!ext4_ext_is_unwritten(ex)) && (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) { + BUG_ON(map =3D=3D extsize_map); path =3D convert_initialized_extent(handle, inode, map, path, &allocated); if (IS_ERR(path)) @@ -4275,8 +4306,8 @@ int ext4_ext_map_blocks(handle_t *handle, struct inod= e *inode, } =20 path =3D ext4_ext_handle_unwritten_extents( - handle, inode, map, path, flags, - &allocated, newblock); + handle, inode, orig_map, extsize_map, path, + flags, &allocated, newblock); if (IS_ERR(path)) err =3D PTR_ERR(path); goto out; @@ -4309,6 +4340,7 @@ int ext4_ext_map_blocks(handle_t *handle, struct inod= e *inode, */ if (cluster_offset && ex && get_implied_cluster_alloc(inode->i_sb, map, ex, path)) { + BUG_ON(map =3D=3D extsize_map); ar.len =3D allocated =3D map->m_len; newblock =3D map->m_pblk; goto got_allocated_blocks; @@ -4329,6 +4361,7 @@ int ext4_ext_map_blocks(handle_t *handle, struct inod= e *inode, * cluster we can use. */ if ((sbi->s_cluster_ratio > 1) && err && get_implied_cluster_alloc(inode->i_sb, map, &ex2, path)) { + BUG_ON(map =3D=3D extsize_map); ar.len =3D allocated =3D map->m_len; newblock =3D map->m_pblk; err =3D 0; @@ -4383,6 +4416,8 @@ int ext4_ext_map_blocks(handle_t *handle, struct inod= e *inode, ar.flags |=3D EXT4_MB_DELALLOC_RESERVED; if (flags & EXT4_GET_BLOCKS_METADATA_NOFAIL) ar.flags |=3D EXT4_MB_USE_RESERVED; + if (flags & EXT4_GET_BLOCKS_EXTSIZE) + ar.flags |=3D EXT4_MB_HINT_ALIGNED; newblock =3D ext4_mb_new_blocks(handle, &ar, &err); if (!newblock) goto out; @@ -4404,9 +4439,114 @@ int ext4_ext_map_blocks(handle_t *handle, struct in= ode *inode, map->m_flags |=3D EXT4_MAP_UNWRITTEN; } =20 - path =3D ext4_ext_insert_extent(handle, inode, path, &newex, flags); - if (IS_ERR(path)) { - err =3D PTR_ERR(path); + if ((flags & EXT4_GET_BLOCKS_EXTSIZE) && + (flags & EXT4_GET_BLOCKS_PRE_IO)) { + /* + * With EXTSIZE and PRE-IO (direct io case) we have to be careful + * because we want to insert the complete extent but split only the + * originally requested range. + * + * Below are the different (S)cenarios and the (A)ction we take: + * + * S1: New extent covers the original range completely/partially. + * A1: Insert new extent, allow merges. Then split the original + * range from this. Adjust the length of split if new extent only + * partially covers original. + * + * S2: New extent doesn't cover original range at all + * A2: Just insert this range and return. Rest is handled in + * ext4_map_blocks() + * NOTE: We can handle this as an error with EAGAIN in future. + */ + ext4_lblk_t newex_lblk =3D le32_to_cpu(newex.ee_block); + loff_t newex_len =3D ext4_ext_get_actual_len(&newex); + + if (in_range(orig_map->m_lblk, newex_lblk, newex_len)) { + /* S1 */ + loff_t split_len =3D 0; + + BUG_ON(!ext4_ext_is_unwritten(&newex)); + + if (newex_lblk + newex_len >=3D + orig_map->m_lblk + (loff_t)orig_map->m_len) + split_len =3D orig_map->m_len; + else + split_len =3D newex_len - + (orig_map->m_lblk - newex_lblk); + + path =3D ext4_ext_insert_extent( + handle, inode, path, &newex, + (flags & ~EXT4_GET_BLOCKS_PRE_IO)); + if (IS_ERR(path)) { + err =3D PTR_ERR(path); + goto insert_error; + } + + /* + * Update path before split + * NOTE: This might no longer be needed with recent + * changes in ext4_ext_insert_extent() + */ + path =3D ext4_find_extent(inode, orig_map->m_lblk, path, 0); + if (IS_ERR(path)) { + err =3D PTR_ERR(path); + goto insert_error; + } + + /* + * GET_BLOCKS_CONVERT is needed to make sure split + * extent is marked unwritten although the flags itself + * means that the extent should be converted to written. + * + * TODO: This is because ext4_split_convert_extents() + * doesn't respect the flags at all but fixing this + * needs more involved design changes. + */ + path =3D ext4_split_convert_extents( + handle, inode, orig_map->m_lblk, split_len, + path, flags | EXT4_GET_BLOCKS_CONVERT, NULL); + if (IS_ERR(path)) { + err =3D PTR_ERR(path); + goto insert_error; + } + +#ifdef CONFIG_EXT4_DEBUG + test_path =3D ext4_find_extent(inode, orig_map->m_lblk, + NULL, 0); + if (!IS_ERR(test_path)) { + /* Confirm we've correctly split and marked the extent unwritten */ + struct ext4_extent *test_ex =3D + test_path[ext_depth(inode)].p_ext; + WARN_ON(!ext4_ext_is_unwritten(test_ex)); + WARN_ON(test_ex->ee_block !=3D orig_map->m_lblk); + WARN_ON(ext4_ext_get_actual_len(test_ex) !=3D + orig_map->m_len); + kfree(test_path); + } +#endif + } else { + /* S2 */ + BUG_ON(orig_map->m_lblk < newex_lblk + newex_len); + + path =3D ext4_ext_insert_extent( + handle, inode, path, &newex, + (flags & ~EXT4_GET_BLOCKS_PRE_IO)); + if (IS_ERR(path)) { + err =3D PTR_ERR(path); + goto insert_error; + } + } + } else { + path =3D ext4_ext_insert_extent(handle, inode, path, &newex, + flags); + if (IS_ERR(path)) { + err =3D PTR_ERR(path); + goto insert_error; + } + } + +insert_error: + if (err) { if (allocated_clusters) { int fb_flags =3D 0; =20 @@ -4690,7 +4830,7 @@ static long ext4_do_fallocate(struct file *file, loff= _t offset, loff_t end =3D offset + len; loff_t new_size =3D 0; ext4_lblk_t start_lblk, len_lblk; - int ret; + int ret, flags; =20 trace_ext4_fallocate_enter(inode, offset, len, mode); WARN_ON_ONCE(!inode_is_locked(inode)); @@ -4712,8 +4852,12 @@ static long ext4_do_fallocate(struct file *file, lof= f_t offset, goto out; } =20 + flags =3D EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT; + if (ext4_should_use_extsize(inode)) + flags |=3D EXT4_GET_BLOCKS_EXTSIZE; + ret =3D ext4_alloc_file_blocks(file, start_lblk, len_lblk, new_size, - EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT); + flags); if (ret) goto out; =20 diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index 664218228fd5..385fbd745e12 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -459,7 +459,7 @@ static void ext4_map_blocks_es_recheck(handle_t *handle, */ down_read(&EXT4_I(inode)->i_data_sem); if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { - retval =3D ext4_ext_map_blocks(handle, inode, map, 0); + retval =3D ext4_ext_map_blocks(handle, inode, map, NULL, 0); } else { retval =3D ext4_ind_map_blocks(handle, inode, map, 0); } @@ -501,7 +501,7 @@ static int ext4_map_query_blocks_next_in_leaf(handle_t = *handle, map2.m_lblk =3D map->m_lblk + map->m_len; map2.m_len =3D orig_mlen - map->m_len; map2.m_flags =3D 0; - retval =3D ext4_ext_map_blocks(handle, inode, &map2, 0); + retval =3D ext4_ext_map_blocks(handle, inode, &map2, NULL, 0); =20 if (retval <=3D 0) { ext4_es_insert_extent(inode, map->m_lblk, map->m_len, @@ -539,17 +539,37 @@ static int ext4_map_query_blocks_next_in_leaf(handle_= t *handle, } =20 static int ext4_map_query_blocks(handle_t *handle, struct inode *inode, - struct ext4_map_blocks *map, int flags) + struct ext4_map_blocks *orig_map, + struct ext4_map_blocks *extsize_map, + int flags) { unsigned int status; int retval; - unsigned int orig_mlen =3D map->m_len; + unsigned int orig_mlen; + struct ext4_map_blocks *map; =20 flags &=3D EXT4_EX_QUERY_FILTER; + + if (flags & EXT4_GET_BLOCKS_EXTSIZE) { + BUG_ON(extsize_map =3D=3D NULL); + map =3D extsize_map; + } else + map =3D orig_map; + + orig_mlen =3D map->m_len; + if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) - retval =3D ext4_ext_map_blocks(handle, inode, map, flags); - else + if (flags & EXT4_GET_BLOCKS_EXTSIZE) { + retval =3D ext4_ext_map_blocks(handle, inode, orig_map, + map, flags); + } else { + retval =3D ext4_ext_map_blocks(handle, inode, map, NULL, + flags); + } + else { + BUG_ON(flags & EXT4_GET_BLOCKS_EXTSIZE); retval =3D ext4_ind_map_blocks(handle, inode, map, flags); + } =20 if (retval <=3D 0) return retval; @@ -581,11 +601,20 @@ static int ext4_map_query_blocks(handle_t *handle, st= ruct inode *inode, } =20 static int ext4_map_create_blocks(handle_t *handle, struct inode *inode, - struct ext4_map_blocks *map, int flags) + struct ext4_map_blocks *orig_map, + struct ext4_map_blocks *extsize_map, + int flags) { struct extent_status es; unsigned int status; int err, retval =3D 0; + struct ext4_map_blocks *map; + + if (flags & EXT4_GET_BLOCKS_EXTSIZE) { + BUG_ON(extsize_map =3D=3D NULL); + map =3D extsize_map; + } else + map =3D orig_map; =20 /* * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE @@ -606,8 +635,15 @@ static int ext4_map_create_blocks(handle_t *handle, st= ruct inode *inode, * changed the inode type in between. */ if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { - retval =3D ext4_ext_map_blocks(handle, inode, map, flags); + if (flags & EXT4_GET_BLOCKS_EXTSIZE) { + retval =3D ext4_ext_map_blocks(handle, inode, orig_map, + map, flags); + } else { + retval =3D ext4_ext_map_blocks(handle, inode, map, NULL, + flags); + } } else { + BUG_ON(flags & EXT4_GET_BLOCKS_EXTSIZE); retval =3D ext4_ind_map_blocks(handle, inode, map, flags); =20 /* @@ -662,6 +698,80 @@ static int ext4_map_create_blocks(handle_t *handle, st= ruct inode *inode, return retval; } =20 +/** + * Extsize hint will change the mapped range and hence we'll end up mappin= g more. + * To not confuse the caller, adjust the struct ext4_map_blocks to reflect= the + * original mapping requested by them. + * + * @cur_map: The block mapping we are working with (for sanity check) + * @orig_map: The originally requested mapping + * @extsize_map: The mapping after adjusting for extsize hint + * @flags Get block flags (for sanity check) + * + * This function assumes that the orig_mlblk is contained within the mappi= ng + * held in extsize_map. Caller must make sure this is true. + */ +static inline unsigned int ext4_extsize_adjust_map(struct ext4_map_blocks = *cur_map, + struct ext4_map_blocks *orig_map, + struct ext4_map_blocks *extsize_map, + int flags) +{ + __u64 map_end =3D (__u64)extsize_map->m_lblk + extsize_map->m_len; + + BUG_ON(cur_map !=3D extsize_map || !(flags & EXT4_GET_BLOCKS_EXTSIZE)); + + orig_map->m_len =3D min(orig_map->m_len, map_end - orig_map->m_lblk); + orig_map->m_pblk =3D + extsize_map->m_pblk + (orig_map->m_lblk - extsize_map->m_lblk); + orig_map->m_flags =3D extsize_map->m_flags; + + return orig_map->m_len; +} + +/** + * ext4_error_adjust_map - Adjust map returned upon error in ext4_map_bloc= ks() + * + * @cur_map: current map we are working with + * @orig_map: original map that would be returned to the user. + * + * Most of the callers of ext4_map_blocks() ignore the map on error, howev= er + * some use it for debug logging. In this case, they log state of the map = just + * before the error, hence this function ensures that map returned to call= er is + * the one we were working with when error happened. Mostly useful when ex= tsize + * hints are enabled. + */ +static inline void ext4_error_adjust_map(struct ext4_map_blocks *cur_map, + struct ext4_map_blocks *orig_map) +{ + if (cur_map !=3D orig_map) + memcpy(orig_map, cur_map, sizeof(*cur_map)); +} + +/* + * This functions resets the mapping to it's original state after it has b= een + * modified due to extent size hint and drops the extsize hint. To be used + * incase we want to fallback from extsize based aligned allocation to nor= mal + * allocation + * + * @map: The block mapping where lblk and len have been modified + * because of extsize hint + * @flags: The get_block flags + * @orig_mlblk: The originally requested logical block to map + * @orig_mlen: The originally requested len to map + * @orig_flags: The originally requested get_block flags + */ +static inline void ext4_extsize_reset_map(struct ext4_map_blocks *map, + int *flags, ext4_lblk_t orig_mlblk, + unsigned int orig_mlen, + int orig_flags) +{ + /* Drop the extsize hint from original flags */ + *flags =3D orig_flags & ~EXT4_GET_BLOCKS_EXTSIZE; + map->m_lblk =3D orig_mlblk; + map->m_len =3D orig_mlen; + map->m_flags =3D 0; +} + /* * The ext4_map_blocks() function tries to look up the requested blocks, * and returns if the blocks are already mapped. @@ -686,30 +796,40 @@ static int ext4_map_create_blocks(handle_t *handle, s= truct inode *inode, * It returns the error in case of allocation failure. */ int ext4_map_blocks(handle_t *handle, struct inode *inode, - struct ext4_map_blocks *map, int flags) + struct ext4_map_blocks *orig_map, int flags) { struct extent_status es; int retval; int ret =3D 0; - unsigned int orig_mlen =3D map->m_len; + + ext4_lblk_t orig_mlblk, extsize_mlblk; + unsigned int orig_mlen, extsize_mlen; + int orig_flags; + + struct ext4_map_blocks *map =3D NULL; + struct ext4_map_blocks extsize_map =3D {0}; + + __u32 extsize =3D ext4_inode_get_extsize(EXT4_I(inode)); + bool should_extsize =3D false; + #ifdef ES_AGGRESSIVE_TEST - struct ext4_map_blocks orig_map; + struct ext4_map_blocks test_map; =20 - memcpy(&orig_map, map, sizeof(*map)); + memcpy(&test_map, map, sizeof(*map)); #endif =20 - map->m_flags =3D 0; - ext_debug(inode, "flag 0x%x, max_blocks %u, logical block %lu\n", - flags, map->m_len, (unsigned long) map->m_lblk); + orig_map->m_flags =3D 0; + ext_debug(inode, "flag 0x%x, max_blocks %u, logical block %lu\n", flags, + orig_map->m_len, (unsigned long)orig_map->m_lblk); =20 /* * ext4_map_blocks returns an int, and m_len is an unsigned int */ - if (unlikely(map->m_len > INT_MAX)) - map->m_len =3D INT_MAX; + if (unlikely(orig_map->m_len > INT_MAX)) + orig_map->m_len =3D INT_MAX; =20 /* We can handle the block number less than EXT_MAX_BLOCKS */ - if (unlikely(map->m_lblk >=3D EXT_MAX_BLOCKS)) + if (unlikely(orig_map->m_lblk >=3D EXT_MAX_BLOCKS)) return -EFSCORRUPTED; =20 /* @@ -722,6 +842,73 @@ int ext4_map_blocks(handle_t *handle, struct inode *in= ode, else ext4_check_map_extents_env(inode); =20 + orig_mlblk =3D orig_map->m_lblk; + orig_mlen =3D orig_map->m_len; + orig_flags =3D flags; + +set_map: + should_extsize =3D (extsize && (flags & EXT4_GET_BLOCKS_CREATE) && + (flags & EXT4_GET_BLOCKS_EXTSIZE)); + if (should_extsize) { + /* + * We adjust the extent size here but we still return the + * original lblk and len while returning to keep the behavior + * compatible. + */ + int len, align; + /* + * NOTE: Should we import EXT_UNWRITTEN_MAX_LEN from + * ext4_extents.h here? + */ + int max_unwrit_len =3D ((1UL << 15) - 1); + loff_t end; + + align =3D orig_map->m_lblk % extsize; + len =3D orig_map->m_len + align; + + extsize_map.m_lblk =3D orig_map->m_lblk - align; + extsize_map.m_len =3D + max_t(unsigned int, roundup_pow_of_two(len), extsize); + + /* + * For now allocations beyond EOF don't use extsize hints so + * that we can avoid dealing with extra blocks allocated past + * EOF. We have inode lock since extsize allocations are + * non-delalloc so i_size can be accessed safely + */ + end =3D (extsize_map.m_lblk + (loff_t)extsize_map.m_len) << inode->i_blk= bits; + if (end > inode->i_size) { + flags =3D orig_flags & ~EXT4_GET_BLOCKS_EXTSIZE; + goto set_map; + } + + /* Fallback to normal allocation if we go beyond max len */ + if (extsize_map.m_len >=3D max_unwrit_len) { + flags =3D orig_flags & ~EXT4_GET_BLOCKS_EXTSIZE; + goto set_map; + } + + /* + * We are allocating more than requested. We'll have to convert + * the extent to unwritten and then convert only the part + * requested to written. For now we are using the same flow as + * dioread nolock to achieve this. Hence the caller has to pass + * CREATE_UNWRIT with EXTSIZE + */ + if (WARN_ON_ONCE(!(flags & EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT))) { + /* Fallback to non extsize allocation */ + flags =3D orig_flags & ~EXT4_GET_BLOCKS_EXTSIZE; + goto set_map; + } + + extsize_mlblk =3D extsize_map.m_lblk; + extsize_mlen =3D extsize_map.m_len; + + extsize_map.m_flags =3D orig_map->m_flags; + map =3D &extsize_map; + } else + map =3D orig_map; + /* Lookup extent status tree firstly */ if (ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) { if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) { @@ -750,7 +937,7 @@ int ext4_map_blocks(handle_t *handle, struct inode *ino= de, return retval; #ifdef ES_AGGRESSIVE_TEST ext4_map_blocks_es_recheck(handle, inode, map, - &orig_map, flags); + &test_map, flags); #endif if (!(flags & EXT4_GET_BLOCKS_QUERY_LAST_IN_LEAF) || orig_mlen =3D=3D map->m_len) @@ -770,19 +957,58 @@ int ext4_map_blocks(handle_t *handle, struct inode *i= node, * file system block. */ down_read(&EXT4_I(inode)->i_data_sem); - retval =3D ext4_map_query_blocks(handle, inode, map, flags); + if (should_extsize) { + BUG_ON(map !=3D &extsize_map); + retval =3D ext4_map_query_blocks(handle, inode, orig_map, map, flags); + } else { + BUG_ON(map !=3D orig_map); + retval =3D ext4_map_query_blocks(handle, inode, map, NULL, flags); + } up_read((&EXT4_I(inode)->i_data_sem)); =20 found: if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { ret =3D check_block_validity(inode, map); - if (ret !=3D 0) + if (ret !=3D 0) { + ext4_error_adjust_map(map, orig_map); return ret; + } } =20 /* If it is only a block(s) look up */ - if ((flags & EXT4_GET_BLOCKS_CREATE) =3D=3D 0) + if ((flags & EXT4_GET_BLOCKS_CREATE) =3D=3D 0) { + BUG_ON(flags & EXT4_GET_BLOCKS_EXTSIZE); return retval; + } + + /* Handle some special cases when extsize based allocation is needed */ + if (retval >=3D 0 && flags & EXT4_GET_BLOCKS_EXTSIZE) { + bool orig_in_range =3D + in_range(orig_mlblk, (__u64)map->m_lblk, map->m_len); + /* + * Special case: if the extsize range is mapped already and + * covers the original start, we return it. + */ + if (map->m_flags & EXT4_MAP_MAPPED && orig_in_range) { + /* + * We don't use EXTSIZE with CONVERT_UNWRITTEN so + * we can directly return the written extent + */ + return ext4_extsize_adjust_map(map, orig_map, &extsize_map, flags); + } + + /* + * Fallback case: if the found mapping (or hole) doesn't cover + * the extsize required, then just fall back to normal + * allocation to keep things simple. + */ + + if (map->m_lblk !=3D extsize_mlblk || + map->m_len !=3D extsize_mlen) { + flags =3D orig_flags & ~EXT4_GET_BLOCKS_EXTSIZE; + goto set_map; + } + } =20 /* * Returns if the blocks have already allocated @@ -808,12 +1034,22 @@ int ext4_map_blocks(handle_t *handle, struct inode *= inode, * with create =3D=3D 1 flag. */ down_write(&EXT4_I(inode)->i_data_sem); - retval =3D ext4_map_create_blocks(handle, inode, map, flags); + if (should_extsize) { + BUG_ON(map !=3D &extsize_map); + retval =3D ext4_map_create_blocks(handle, inode, orig_map, map, + flags); + } else { + BUG_ON(map !=3D orig_map); + retval =3D ext4_map_create_blocks(handle, inode, map, NULL, + flags); + } up_write((&EXT4_I(inode)->i_data_sem)); if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { ret =3D check_block_validity(inode, map); - if (ret !=3D 0) + if (ret !=3D 0) { + ext4_error_adjust_map(map, orig_map); return ret; + } =20 /* * Inodes with freshly allocated blocks where contents will be @@ -835,16 +1071,38 @@ int ext4_map_blocks(handle_t *handle, struct inode *= inode, else ret =3D ext4_jbd2_inode_add_write(handle, inode, start_byte, length); - if (ret) + if (ret) { + ext4_error_adjust_map(map, orig_map); return ret; + } } } if (retval > 0 && (map->m_flags & EXT4_MAP_UNWRITTEN || map->m_flags & EXT4_MAP_MAPPED)) ext4_fc_track_range(handle, inode, map->m_lblk, map->m_lblk + map->m_len - 1); - if (retval < 0) + + if (retval > 0 && flags & EXT4_GET_BLOCKS_EXTSIZE) { + /* + * In the rare case that we have a short allocation and orig + * lblk doesn't lie in mapped range just try to retry with + * actual allocation. This is not ideal but this should be an + * edge case near ENOSPC. + * + * NOTE: This has a side effect that blocks are allocated but + * not used. Can we avoid that? + */ + if (!in_range(orig_mlblk, (__u64)map->m_lblk, map->m_len)) { + flags =3D orig_flags & ~EXT4_GET_BLOCKS_EXTSIZE; + goto set_map; + } + return ext4_extsize_adjust_map(map, orig_map, &extsize_map, flags); + } + + if (retval < 0) { + ext4_error_adjust_map(map, orig_map); ext_debug(inode, "failed with err %d\n", retval); + } return retval; } =20 @@ -900,18 +1158,20 @@ static int _ext4_get_block(struct inode *inode, sect= or_t iblock, { struct ext4_map_blocks map; int ret =3D 0; + unsigned int orig_mlen =3D bh->b_size >> inode->i_blkbits; =20 if (ext4_has_inline_data(inode)) return -ERANGE; =20 map.m_lblk =3D iblock; - map.m_len =3D bh->b_size >> inode->i_blkbits; + map.m_len =3D orig_mlen; =20 ret =3D ext4_map_blocks(ext4_journal_current_handle(), inode, &map, flags); if (ret > 0) { map_bh(bh, inode->i_sb, map.m_pblk); ext4_update_bh_state(bh, map.m_flags); + WARN_ON(map.m_len !=3D orig_mlen); bh->b_size =3D inode->i_sb->s_blocksize * map.m_len; ret =3D 0; } else if (ret =3D=3D 0) { @@ -937,11 +1197,14 @@ int ext4_get_block_unwritten(struct inode *inode, se= ctor_t iblock, struct buffer_head *bh_result, int create) { int ret =3D 0; + int flags =3D EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT; + + if (ext4_should_use_extsize(inode)) + flags |=3D EXT4_GET_BLOCKS_EXTSIZE; =20 ext4_debug("ext4_get_block_unwritten: inode %lu, create flag %d\n", inode->i_ino, create); - ret =3D _ext4_get_block(inode, iblock, bh_result, - EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT); + ret =3D _ext4_get_block(inode, iblock, bh_result, flags); =20 /* * If the buffer is marked unwritten, mark it as new to make sure it is @@ -1298,7 +1561,8 @@ static int ext4_write_begin(struct file *file, struct= address_space *mapping, ext4_journal_blocks_per_folio(inode)) + 1; index =3D pos >> PAGE_SHIFT; =20 - if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { + if (!ext4_should_use_extsize(inode) && + ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { ret =3D ext4_try_to_write_inline_data(mapping, inode, pos, len, foliop); if (ret < 0) @@ -1354,7 +1618,7 @@ static int ext4_write_begin(struct file *file, struct= address_space *mapping, /* In case writeback began while the folio was unlocked */ folio_wait_stable(folio); =20 - if (ext4_should_dioread_nolock(inode)) + if (ext4_should_use_unwrit_extents(inode)) ret =3D ext4_block_write_begin(handle, folio, pos, len, ext4_get_block_unwritten); else @@ -1948,7 +2212,7 @@ static int ext4_da_map_blocks(struct inode *inode, st= ruct ext4_map_blocks *map) if (ext4_has_inline_data(inode)) retval =3D 0; else - retval =3D ext4_map_query_blocks(NULL, inode, map, 0); + retval =3D ext4_map_query_blocks(NULL, inode, map, NULL, 0); up_read(&EXT4_I(inode)->i_data_sem); if (retval) return retval < 0 ? retval : 0; @@ -1971,7 +2235,7 @@ static int ext4_da_map_blocks(struct inode *inode, st= ruct ext4_map_blocks *map) goto found; } } else if (!ext4_has_inline_data(inode)) { - retval =3D ext4_map_query_blocks(NULL, inode, map, 0); + retval =3D ext4_map_query_blocks(NULL, inode, map, NULL, 0); if (retval) { up_write(&EXT4_I(inode)->i_data_sem); return retval < 0 ? retval : 0; @@ -2344,6 +2608,7 @@ static int mpage_map_one_extent(handle_t *handle, str= uct mpage_da_data *mpd) struct ext4_map_blocks *map =3D &mpd->map; int get_blocks_flags; int err, dioread_nolock; + int extsize =3D ext4_should_use_extsize(inode); =20 /* Make sure transaction has enough credits for this extent */ err =3D ext4_journal_ensure_extent_credits(handle, inode); @@ -2371,11 +2636,14 @@ static int mpage_map_one_extent(handle_t *handle, s= truct mpage_da_data *mpd) dioread_nolock =3D ext4_should_dioread_nolock(inode); if (dioread_nolock) get_blocks_flags |=3D EXT4_GET_BLOCKS_IO_CREATE_EXT; + if (extsize) + get_blocks_flags |=3D EXT4_GET_BLOCKS_PRE_IO; =20 err =3D ext4_map_blocks(handle, inode, map, get_blocks_flags); if (err < 0) return err; - if (dioread_nolock && (map->m_flags & EXT4_MAP_UNWRITTEN)) { + if ((extsize || dioread_nolock) && + (map->m_flags & EXT4_MAP_UNWRITTEN)) { if (!mpd->io_submit.io_end->handle && ext4_handle_valid(handle)) { mpd->io_submit.io_end->handle =3D handle->h_rsv_handle; @@ -2832,12 +3100,13 @@ static int ext4_do_writepages(struct mpage_da_data = *mpd) } mpd->journalled_more_data =3D 0; =20 - if (ext4_should_dioread_nolock(inode)) { - int bpf =3D ext4_journal_blocks_per_folio(inode); + if (ext4_should_use_unwrit_extents(inode)) { /* - * We may need to convert up to one extent per block in - * the folio and we may dirty the inode. + * For extsize allocation or dioread_nolock, we may need to + * convert up to one extent per block in the page and we may + * dirty the inode. */ + int bpf =3D ext4_journal_blocks_per_folio(inode); rsv_blocks =3D 1 + ext4_ext_index_trans_blocks(inode, bpf); } =20 @@ -3125,7 +3394,8 @@ static int ext4_da_write_begin(struct file *file, str= uct address_space *mapping, =20 index =3D pos >> PAGE_SHIFT; =20 - if (ext4_nonda_switch(inode->i_sb) || ext4_verity_in_progress(inode)) { + if (ext4_nonda_switch(inode->i_sb) || ext4_verity_in_progress(inode) || + ext4_should_use_extsize(inode)) { *fsdata =3D (void *)FALL_BACK_TO_NONDELALLOC; return ext4_write_begin(file, mapping, pos, len, foliop, fsdata); @@ -3740,12 +4010,19 @@ static int ext4_iomap_alloc(struct inode *inode, st= ruct ext4_map_blocks *map, * can complete at any point during the I/O and subsequently push the * i_disksize out to i_size. This could be beyond where direct I/O is * happening and thus expose allocated blocks to direct I/O reads. + * + * NOTE for extsize hints: We only support it for writes inside + * EOF (for now) to not have to deal with blocks past EOF */ else if (((loff_t)map->m_lblk << blkbits) >=3D i_size_read(inode)) m_flags =3D EXT4_GET_BLOCKS_CREATE; - else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) + else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { m_flags =3D EXT4_GET_BLOCKS_IO_CREATE_EXT; =20 + if (ext4_should_use_extsize(inode)) + m_flags |=3D EXT4_GET_BLOCKS_EXTSIZE; + } + if (flags & IOMAP_ATOMIC) ret =3D ext4_map_blocks_atomic_write(handle, inode, map, m_flags, &force_commit); @@ -6778,7 +7055,7 @@ vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf) } folio_unlock(folio); /* OK, we need to fill the hole... */ - if (ext4_should_dioread_nolock(inode)) + if (ext4_should_use_unwrit_extents(inode)) get_block =3D ext4_get_block_unwritten; retry_alloc: /* Start journal and allocate blocks */ --=20 2.49.0 From nobody Mon Oct 6 13:17:37 2025 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (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 24EC722069A; Sun, 20 Jul 2025 20:58:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.163.156.1 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753045094; cv=none; b=d4/SxThP9yAEsCRgxjyJOQFxHSozMNkkMrjhlxN+F2yHlV5y+010oP2RpQddR072+sIbLQq3YOn6T/ZD3ncFSGtNbINYuflZLEJfRfVW/cG9ld5F1RM3VSbSc+wTZaKkGdq9sJoNqMYWszpCrpG+uhM6glUYtIwYLfO+HYtEbEg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753045094; c=relaxed/simple; bh=zPlbxH78VXxZ1Fd44pBywx7cXWN+Y9Fz/uLQUm7sreU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OUtY6PcCj3bRTdS/lLEc0Y4OsQ6euhAuE+rSgPz3FPv6c4vUPfaQ9cwgOILBMTBEIyCSA9ZES2B/7wyOG7VmokDhbyJWGBtfO0XdBYS3nlKjJqhGSJpsV4QJ/53lmBEFkhHSfBzkCqHS3epcRY0du9Mr/KvniGIDzBX0UPBLvwc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com; spf=pass smtp.mailfrom=linux.ibm.com; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b=M7GZZbJ9; arc=none smtp.client-ip=148.163.156.1 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="M7GZZbJ9" Received: from pps.filterd (m0356517.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 56KJjbkq030009; Sun, 20 Jul 2025 20:57:57 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=pp1; bh=xx9d5OUwWdIyY+yXB JW4VLQQ9V8gvlQn5d5t5pjUOe0=; b=M7GZZbJ9Lg6kJKXitSzPCDVkB/Ep0Qtzu i4/KYwDN90Yp1hgoyJ004gFjzj0EyXfSYJsyRWkXzhZGr4ZX6DPKBuku+UZ37TCp cDLlTvzgTQahEE9dwceRGDDnUJRCQTY+3/1DhSG7DXFLaPoTBc8LZiyKyfMtaKls JtzSNk0ZSouixHs3DwTT0fEvhjfvPsY6B0l5HJgKYOoj4k5xckqqVCjD68lcow2F Q9NKIb+Fv8hdzpHPtGXTD68QPvkiJHk0Iv9KYG8nxDTQIkIOzOxIFZeJouGD99FJ E4osLsw++q6BBv6QwOfNyRD2jus1e/2cP9XHrvyifdQrueOohx8hQ== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 48069v5kk0-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 20 Jul 2025 20:57:57 +0000 (GMT) Received: from m0356517.ppops.net (m0356517.ppops.net [127.0.0.1]) by pps.reinject (8.18.0.8/8.18.0.8) with ESMTP id 56KKvuat026625; Sun, 20 Jul 2025 20:57:56 GMT Received: from ppma11.dal12v.mail.ibm.com (db.9e.1632.ip4.static.sl-reverse.com [50.22.158.219]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 48069v5kjw-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 20 Jul 2025 20:57:56 +0000 (GMT) Received: from pps.filterd (ppma11.dal12v.mail.ibm.com [127.0.0.1]) by ppma11.dal12v.mail.ibm.com (8.18.1.2/8.18.1.2) with ESMTP id 56KIiGpo024704; Sun, 20 Jul 2025 20:57:55 GMT Received: from smtprelay04.fra02v.mail.ibm.com ([9.218.2.228]) by ppma11.dal12v.mail.ibm.com (PPS) with ESMTPS id 480rd22ydv-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 20 Jul 2025 20:57:55 +0000 Received: from smtpav02.fra02v.mail.ibm.com (smtpav02.fra02v.mail.ibm.com [10.20.54.101]) by smtprelay04.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 56KKvr9V10289428 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 20 Jul 2025 20:57:54 GMT Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id D107C20043; Sun, 20 Jul 2025 20:57:53 +0000 (GMT) Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 79D6E20040; Sun, 20 Jul 2025 20:57:51 +0000 (GMT) Received: from li-dc0c254c-257c-11b2-a85c-98b6c1322444.ibm.com (unknown [9.39.16.241]) by smtpav02.fra02v.mail.ibm.com (Postfix) with ESMTP; Sun, 20 Jul 2025 20:57:51 +0000 (GMT) From: Ojaswin Mujoo To: linux-ext4@vger.kernel.org, "Theodore Ts'o" Cc: Jan Kara , Baokun Li , Ritesh Harjani , Zhang Yi , linux-kernel@vger.kernel.org, "Darrick J . Wong" , linux-fsdevel@vger.kernel.org Subject: [RFC v4 6/7] ext4: make extsize work with EOF allocations Date: Mon, 21 Jul 2025 02:27:32 +0530 Message-ID: X-Mailer: git-send-email 2.49.0 In-Reply-To: References: 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-TM-AS-GCONF: 00 X-Proofpoint-GUID: oEiUvRUmALhPJFXyH_7BowBpQj3B_cIC X-Proofpoint-Spam-Details-Enc: AW1haW4tMjUwNzIwMDE5OCBTYWx0ZWRfX+tzFQjeZ1okv hjdGPPZJo8srVifuEw1oLFOKRki6yOy2ee6ag6YfG6pw+d2N7qaox7RZp7tAW3AFBsphWTygkbt GiAy+le/XUHxWb7zr2Ge6Ku63J6CyGbuaGjo/QpAVzuQ07WIWVKBNS0KppnLNDZSh8apsAYGWaX XqX1gAJEuLl0sVKzWnl3yjp9JQHb39GGjpR3QmyjemNMi2NNJIiXmB7/R8pC9NCJ96D2hqdboUp CwE3pf3lfoomGJ3qr/MzNOAfXcD+sfPHgXzKInVTEZohq7PtxFk0KkE/Q/ANQ1GJoiLMsA8qPpG 2xGSC1Vw42CELmyza3KuG/hYDAn2oUz6VWTnKbQToX1L7aFN/E5BNzlU+05DHNq5UdoAKjNk7q5 Y83W0NKdY+umhnb/FLm7oQBRXZTTBtqSuPZvnzWEzFi1Wgl+r91bXNWuHRwLi1f2i6bWQTep X-Proofpoint-ORIG-GUID: UL9ftHSosS7Tq5Cr1-Q7UGWSJKENMB8R X-Authority-Analysis: v=2.4 cv=JJQ7s9Kb c=1 sm=1 tr=0 ts=687d5855 cx=c_pps a=aDMHemPKRhS1OARIsFnwRA==:117 a=aDMHemPKRhS1OARIsFnwRA==:17 a=Wb1JkmetP80A:10 a=VnNF1IyMAAAA:8 a=pvUrSPAteZZR1bcYYl4A:9 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1099,Hydra:6.1.9,FMLib:17.12.80.40 definitions=2025-07-20_01,2025-07-17_02,2025-03-28_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxlogscore=999 impostorscore=0 clxscore=1015 mlxscore=0 spamscore=0 malwarescore=0 lowpriorityscore=0 phishscore=0 adultscore=0 bulkscore=0 priorityscore=1501 suspectscore=0 classifier=spam authscore=0 authtc=n/a authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.19.0-2505280000 definitions=main-2507200198 Content-Type: text/plain; charset="utf-8" Make extsize hints work with EOF allocations. We deviate from XFS here because in case we have blocks left past EOF, we don't truncate them. There are 2 main reasons: 1. Since the user is opting for extsize allocations, chances are that they will use the blocks in future. 2. If we start truncating all EOF blocks in ext4_release_file like XFS, then we will have to always truncate blocks even if they have been intentionally preallocated using fallocate w/ KEEP_SIZE which might cause confusion for users. This is mainly because ext4 doesn't have a way to distinguish if the blocks beyond EOF have been allocated intentionally. We can work around this by using an ondisk inode flag like XFS (XFS_DIFLAG_PREALLOC) but that would be an overkill. It's much simpler to just let the EOF blocks stick around. NOTE: One thing that changes in this patch is that for direct IO we need to pass the EXT4_GET_BLOCKS_IO_CREATE_EXT even if we are allocating beyond i_size. Signed-off-by: Ojaswin Mujoo --- fs/ext4/inode.c | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index 385fbd745e12..1b60e45a593e 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -861,7 +861,6 @@ int ext4_map_blocks(handle_t *handle, struct inode *ino= de, * ext4_extents.h here? */ int max_unwrit_len =3D ((1UL << 15) - 1); - loff_t end; =20 align =3D orig_map->m_lblk % extsize; len =3D orig_map->m_len + align; @@ -870,18 +869,6 @@ int ext4_map_blocks(handle_t *handle, struct inode *in= ode, extsize_map.m_len =3D max_t(unsigned int, roundup_pow_of_two(len), extsize); =20 - /* - * For now allocations beyond EOF don't use extsize hints so - * that we can avoid dealing with extra blocks allocated past - * EOF. We have inode lock since extsize allocations are - * non-delalloc so i_size can be accessed safely - */ - end =3D (extsize_map.m_lblk + (loff_t)extsize_map.m_len) << inode->i_blk= bits; - if (end > inode->i_size) { - flags =3D orig_flags & ~EXT4_GET_BLOCKS_EXTSIZE; - goto set_map; - } - /* Fallback to normal allocation if we go beyond max len */ if (extsize_map.m_len >=3D max_unwrit_len) { flags =3D orig_flags & ~EXT4_GET_BLOCKS_EXTSIZE; @@ -4011,10 +3998,13 @@ static int ext4_iomap_alloc(struct inode *inode, st= ruct ext4_map_blocks *map, * i_disksize out to i_size. This could be beyond where direct I/O is * happening and thus expose allocated blocks to direct I/O reads. * - * NOTE for extsize hints: We only support it for writes inside - * EOF (for now) to not have to deal with blocks past EOF + * NOTE: For extsize hint based EOF allocations, we still need + * IO_CREATE_EXT flag because we will be allocating more than the write + * hence the extra blocks need to be marked unwritten and split before + * the I/O. */ - else if (((loff_t)map->m_lblk << blkbits) >=3D i_size_read(inode)) + else if (((loff_t)map->m_lblk << blkbits) >=3D i_size_read(inode) && + !ext4_should_use_extsize(inode)) m_flags =3D EXT4_GET_BLOCKS_CREATE; else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { m_flags =3D EXT4_GET_BLOCKS_IO_CREATE_EXT; --=20 2.49.0 From nobody Mon Oct 6 13:17:37 2025 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (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 A0D4B224B14; Sun, 20 Jul 2025 20:58:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.163.156.1 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753045098; cv=none; b=YhELWm2h6JY+4SNYqU6cnlAo648OVSUBTbfUg8CSbqkqWIuSjspBogub6VKci82TgyNbw026xExk4Q9eCTGkSIFzr7RlaSqxOnpvwHWdVWtPP3+BrMsmj9jPx7AnJvMz3eqxmfLUcHh9fixbnvR2mGdbHokvlkNAm9ySxj6Eguo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753045098; c=relaxed/simple; bh=3VpNrGSLDTgpaTVBXL34gCyZusAFCU/AunfM3eAf/Tk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=erYfAr+JyzOpV+MMfV6BYQyjYndiA2661S11jyllPZ6TUEZEt+VfJn5C5V07ALjK6lf09Ng2Xiywbv4Sg9e/ktDtTxiW7cuvV1xRr4FluoOIZMWH4C2ROhX43/S3Gt3NtzXRG62bEeFTLV1AA5kuLL9uWB8LebltWweB7f1fSVw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com; spf=pass smtp.mailfrom=linux.ibm.com; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b=fIUMteZ/; arc=none smtp.client-ip=148.163.156.1 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="fIUMteZ/" Received: from pps.filterd (m0353729.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 56KJs8Ri005549; Sun, 20 Jul 2025 20:58:00 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=pp1; bh=8pcU36a3QVQA9/SFF vKQWwkrB68bBongYrNmMfiIH5k=; b=fIUMteZ/03+wHObeB2BxJ8UZRsrO3SyiB d7c9OwQKzlgFveN93k109oKqoB9kyROYBs2GfL9m1B+7po7oQB4gGZNiHnrZ6deA w4J+KFpaCNa+Vi2srnBd3Y53th/S6wR7g8DujAo8aztVeVXl8W1u7snAuIE3W2eK +nwzm2BqIueD9nAS8uWOATM0gDJYui76Qix7PP3JDhaEAc90n8DEllSdwJm+XEZb Q7y25rIatypkTcYVTdGerVmzIyJ2dJdUGT5PFOg2mhnD2CrQP7mcykD+wNJy1Pk1 mQ0dmpgkq9f6t+P8tzCnJHuKeJ4WmLGlj2s2/dPl+ok1/0gm8ODPw== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 48069v5jxm-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 20 Jul 2025 20:58:00 +0000 (GMT) Received: from m0353729.ppops.net (m0353729.ppops.net [127.0.0.1]) by pps.reinject (8.18.0.8/8.18.0.8) with ESMTP id 56KKvx6U021062; Sun, 20 Jul 2025 20:58:00 GMT Received: from ppma21.wdc07v.mail.ibm.com (5b.69.3da9.ip4.static.sl-reverse.com [169.61.105.91]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 48069v5jxj-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 20 Jul 2025 20:57:59 +0000 (GMT) Received: from pps.filterd (ppma21.wdc07v.mail.ibm.com [127.0.0.1]) by ppma21.wdc07v.mail.ibm.com (8.18.1.2/8.18.1.2) with ESMTP id 56KH84TG014391; Sun, 20 Jul 2025 20:57:58 GMT Received: from smtprelay02.fra02v.mail.ibm.com ([9.218.2.226]) by ppma21.wdc07v.mail.ibm.com (PPS) with ESMTPS id 480ppnu8mq-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 20 Jul 2025 20:57:58 +0000 Received: from smtpav02.fra02v.mail.ibm.com (smtpav02.fra02v.mail.ibm.com [10.20.54.101]) by smtprelay02.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 56KKvusH47710508 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 20 Jul 2025 20:57:56 GMT Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 97BAB20043; Sun, 20 Jul 2025 20:57:56 +0000 (GMT) Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 595A220040; Sun, 20 Jul 2025 20:57:54 +0000 (GMT) Received: from li-dc0c254c-257c-11b2-a85c-98b6c1322444.ibm.com (unknown [9.39.16.241]) by smtpav02.fra02v.mail.ibm.com (Postfix) with ESMTP; Sun, 20 Jul 2025 20:57:54 +0000 (GMT) From: Ojaswin Mujoo To: linux-ext4@vger.kernel.org, "Theodore Ts'o" Cc: Jan Kara , Baokun Li , Ritesh Harjani , Zhang Yi , linux-kernel@vger.kernel.org, "Darrick J . Wong" , linux-fsdevel@vger.kernel.org Subject: [RFC v4 7/7] ext4: add ext4_map_blocks_extsize() wrapper to handle overwrites Date: Mon, 21 Jul 2025 02:27:33 +0530 Message-ID: <4531e266d4b26b1c3d51da732ff305869e56dcf3.1753044253.git.ojaswin@linux.ibm.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: References: 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-TM-AS-GCONF: 00 X-Authority-Analysis: v=2.4 cv=QLdoRhLL c=1 sm=1 tr=0 ts=687d5858 cx=c_pps a=GFwsV6G8L6GxiO2Y/PsHdQ==:117 a=GFwsV6G8L6GxiO2Y/PsHdQ==:17 a=Wb1JkmetP80A:10 a=VnNF1IyMAAAA:8 a=XbQIVu7OBCAAiYVp_M0A:9 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjUwNzIwMDE5OCBTYWx0ZWRfXykg1hwk4nAgb xROE1waIrLTo/2VPt7oXhC1aLbWJ7xHHgc23el/aJOjGxUkh8rSRR3MhNj57I6X01jmcdHpsgYd vYIv8IAZoYUK8UxYg3pIdGSGx/x/IHtDZho12S0f1+RSoBd23vASha+ZQNhpY64QM1caSRpWQs8 EeEK9XBEMaWb66+Sb5Kn8LPDajYMYzhgJd5XoNzZYX6zetfq5s40iLM+odGXn/3Qv8Nhd6hC4r0 6v4jiaG3jIeFhCUsKfZwdHjzydTzAAAzfEZudaNNmmA8gr7599f2neTzwgfSMhGymKPzdb48Iag MXvofVTuRH1/7ni4R2qc3yi/Go3tEEEdLowQ0s/iCKy8GvuItmksMo26YgXUuDaIQCobP3ViDNE 8Hk9z9i3TmL//Rdx47uSS7pC7MDoUeYUp78vOmiMhEfBst3YXfIlNZhDR93NvYeoe7Wk7lDr X-Proofpoint-ORIG-GUID: KLoetVI8SyMxBp_7bMGJaqXj6PEX-jwy X-Proofpoint-GUID: 7NDJ-ZzSSvuTUBxfCTHBipT0Ep8YrumW X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1099,Hydra:6.1.9,FMLib:17.12.80.40 definitions=2025-07-20_01,2025-07-17_02,2025-03-28_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 phishscore=0 priorityscore=1501 bulkscore=0 malwarescore=0 spamscore=0 suspectscore=0 lowpriorityscore=0 mlxscore=0 mlxlogscore=999 clxscore=1011 adultscore=0 impostorscore=0 classifier=spam authscore=0 authtc=n/a authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.19.0-2505280000 definitions=main-2507200198 Content-Type: text/plain; charset="utf-8" Currently, with the extsize hints, if we consider a scenario where the hint is set to 16k and we do a write of (0,4k) we get the below mapping: [ 4k written ] [ 12k unwritten ] Now, if we do a (4k,4k) write, ext4_map_blocks will again try for a extsize aligned write, adjust the range to (0, 16k) and then run into issues since the new range is already has a mapping in it. Although this does not lead to a failure since we eventually fallback to a non extsize allocation, this is not a good approach. Hence, implement a wrapper over ext4_map_blocks() which detects if a mapping already exists for an extsize based allocation and then reuses the same mapping. In case the mapping completely covers the original request we simply disable extsize allocation and call map_blocks to correctly process the mapping and set the map flags. Otherwise, if there is a hole or partial mapping, then we just let ext4_map_blocks() handle the allocation. Signed-off-by: Ojaswin Mujoo --- fs/ext4/inode.c | 45 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index 1b60e45a593e..010ca890b29c 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -772,6 +772,41 @@ static inline void ext4_extsize_reset_map(struct ext4_= map_blocks *map, map->m_flags =3D 0; } =20 +static int ext4_map_blocks_extsize(handle_t *handle, struct inode *inode, + struct ext4_map_blocks *map, int flags) +{ + int orig_mlen =3D map->m_len; + int ret =3D 0; + int tmp_flags; + + WARN_ON(!ext4_inode_get_extsize(EXT4_I(inode))); + WARN_ON(!(flags & EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT)); + + /* + * First check if there are any existing allocations + */ + ret =3D ext4_map_blocks(handle, inode, map, 0); + if (ret < 0) + return ret; + + /* + * the present mapping fully covers the requested range. In this + * case just go for a non extsize based allocation. Note that we won't + * really be allocating new blocks but the call to ext4_map_blocks is + * important to ensure things like extent splitting and proper map flags + * are taken care of. For all other cases, just let ext4_map_blocks handle + * the allocations + */ + if (ret > 0 && map->m_len =3D=3D orig_mlen) + tmp_flags =3D flags & ~EXT4_GET_BLOCKS_EXTSIZE; + else + tmp_flags =3D flags; + + ret =3D ext4_map_blocks(handle, inode, map, tmp_flags); + + return ret; +} + /* * The ext4_map_blocks() function tries to look up the requested blocks, * and returns if the blocks are already mapped. @@ -1153,8 +1188,12 @@ static int _ext4_get_block(struct inode *inode, sect= or_t iblock, map.m_lblk =3D iblock; map.m_len =3D orig_mlen; =20 - ret =3D ext4_map_blocks(ext4_journal_current_handle(), inode, &map, - flags); + if ((flags & EXT4_GET_BLOCKS_CREATE) && ext4_should_use_extsize(inode)) + ret =3D ext4_map_blocks_extsize(ext4_journal_current_handle(), inode, + &map, flags); + else + ret =3D ext4_map_blocks(ext4_journal_current_handle(), inode, + &map, flags); if (ret > 0) { map_bh(bh, inode->i_sb, map.m_pblk); ext4_update_bh_state(bh, map.m_flags); @@ -4016,6 +4055,8 @@ static int ext4_iomap_alloc(struct inode *inode, stru= ct ext4_map_blocks *map, if (flags & IOMAP_ATOMIC) ret =3D ext4_map_blocks_atomic_write(handle, inode, map, m_flags, &force_commit); + else if (ext4_should_use_extsize(inode)) + ret =3D ext4_map_blocks_extsize(handle, inode, map, m_flags); else ret =3D ext4_map_blocks(handle, inode, map, m_flags); =20 --=20 2.49.0