From patchwork Wed Dec 11 07:57:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ojaswin Mujoo X-Patchwork-Id: 13903062 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 2DB1A1CEE8E; Wed, 11 Dec 2024 07:58:10 +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=1733903892; cv=none; b=naj84js4fwhmYSI5eKooejPNuJtxqBQexY22sW0+Vnb/Hu+VMkR21wvOrX2qTDjXJruacLBMNh4cOClnUV6ynjkz/snRAyOeuESxqLZYJ98cRZMzmE7Os05MPgojauuFcCGxGgAh7u6pUOkjxgu84OM2lqUs5WFcms8ZHzjRIVE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733903892; c=relaxed/simple; bh=LhAu+y4eh7uBInO6pzoNLzVwDm4InfmVOP9NXryJpnw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hqcZwRU150hvrUAN1zLpywKzWxxgl8YTuy2G0K32WVm9fD0QGIgDmTYAdk+9YHVvtZgTCMxuLzRJb5y3Ki0CKWVWyd1tGAgRQGN7S6EzeeGO+tFbfJ2HP3upsv/t+QrNANEh7eYHtz7Ivz2bdfMrqLeWKwvdPYigFqtRrsrqiWM= 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=XkG5WYR7; 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="XkG5WYR7" 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 4BANlEiA026694; Wed, 11 Dec 2024 07:58:03 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=5T9S+bbcyyEgnAyNW Nlw7btXuellk7TodMjJZJarOwo=; b=XkG5WYR7pxkdDlmeqnjnBebpC7LDvkrrZ RxRFijLMwhcVJU0EH90PMPM4O2TVVxUfjIutZiGomVfjHKGz7fO28WesjY5Ig6a2 VZKZuQX/UBzeX534HyCr5QGXJf+XRsTrlNH7N7SzPSs84srq1FSi0VW73U35uMOG y/c7BNy59YP8SsvdNa5WEXTMfxkqEhOKKjGy11cU4aaTlNCTSX2FTQUEV0ejrh0n 0dPMmnAxZtXVYYUxQF9OUGgN16RvlPkzkOU9StYhIZ7GgY3kxlXha0llSQK4M/eI csf4h2hnX187+sgJtUsuwuNOk4hECF4+TEsiYnNEi8Z5bEzGFrx+Q== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 43cbsqb3wy-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 11 Dec 2024 07:58:03 +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 4BB7qd0u030650; Wed, 11 Dec 2024 07:58:02 GMT Received: from ppma13.dal12v.mail.ibm.com (dd.9e.1632.ip4.static.sl-reverse.com [50.22.158.221]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 43cbsqb3wv-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 11 Dec 2024 07:58:02 +0000 (GMT) Received: from pps.filterd (ppma13.dal12v.mail.ibm.com [127.0.0.1]) by ppma13.dal12v.mail.ibm.com (8.18.1.2/8.18.1.2) with ESMTP id 4BB7MK06023169; Wed, 11 Dec 2024 07:58:01 GMT Received: from smtprelay01.fra02v.mail.ibm.com ([9.218.2.227]) by ppma13.dal12v.mail.ibm.com (PPS) with ESMTPS id 43d2wjyxv6-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 11 Dec 2024 07:58:01 +0000 Received: from smtpav04.fra02v.mail.ibm.com (smtpav04.fra02v.mail.ibm.com [10.20.54.103]) by smtprelay01.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 4BB7w0Ts50397510 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 11 Dec 2024 07:58:00 GMT Received: from smtpav04.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 1823C20040; Wed, 11 Dec 2024 07:58:00 +0000 (GMT) Received: from smtpav04.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 383FA20043; Wed, 11 Dec 2024 07:57:58 +0000 (GMT) Received: from li-bb2b2a4c-3307-11b2-a85c-8fa5c3a69313.ibm.com.com (unknown [9.39.30.217]) by smtpav04.fra02v.mail.ibm.com (Postfix) with ESMTP; Wed, 11 Dec 2024 07:57:58 +0000 (GMT) From: Ojaswin Mujoo To: linux-ext4@vger.kernel.org, "Theodore Ts'o" Cc: Ritesh Harjani , linux-kernel@vger.kernel.org, "Darrick J . Wong" , linux-fsdevel@vger.kernel.org, John Garry , dchinner@redhat.com, Nirjhar Roy Subject: [RFC v2 1/6] ext4: add aligned allocation hint in mballoc Date: Wed, 11 Dec 2024 13:27:50 +0530 Message-ID: <9a10ed758f3f39229da9cb0ecd81f529859ebb81.1733901374.git.ojaswin@linux.ibm.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: i2nCQPW3u-jhITL-HfuNC9AUcbP3tbTp X-Proofpoint-GUID: xctrxo-yoZlu7WR_wA9s5euGLRayboUU X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1051,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-10-15_01,2024-10-11_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxlogscore=999 adultscore=0 lowpriorityscore=0 clxscore=1015 phishscore=0 impostorscore=0 suspectscore=0 spamscore=0 mlxscore=0 priorityscore=1501 malwarescore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2411120000 definitions=main-2412110056 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 | 60 +++++++++++++++++++++++++++++++++---- include/trace/events/ext4.h | 1 + 3 files changed, 58 insertions(+), 5 deletions(-) diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h index bbffb76d9a90..fbcde1e71b03 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 +/* The allocation must respect alignment requirements for physical blocks */ +#define EXT4_MB_HINT_ALIGNED 0x40000 struct ext4_allocation_request { /* target inode for block we're allocating */ diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c index b25a27c86696..9763349598e9 100644 --- a/fs/ext4/mballoc.c +++ b/fs/ext4/mballoc.c @@ -2177,8 +2177,11 @@ static void ext4_mb_use_best_found(struct ext4_allocation_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); + } } @@ -2814,10 +2817,15 @@ ext4_mb_regular_allocator(struct ext4_allocation_context *ac) BUG_ON(ac->ac_status == AC_STATUS_FOUND); - /* first, try the goal */ - err = ext4_mb_find_by_goal(ac, &e4b); - if (err || ac->ac_status == 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 = ext4_mb_find_by_goal(ac, &e4b); + if (err || ac->ac_status == AC_STATUS_FOUND) + goto out; + } if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY)) goto out; @@ -2858,9 +2866,22 @@ ext4_mb_regular_allocator(struct ext4_allocation_context *ac) */ if (ac->ac_2order) cr = CR_POWER2_ALIGNED; + else + WARN_ON_ONCE(ac->ac_g_ex.fe_len > 1 && + ac->ac_flags & EXT4_MB_HINT_ALIGNED); repeat: for (; cr < EXT4_MB_NUM_CRS && ac->ac_status == AC_STATUS_CONTINUE; cr++) { ac->ac_criteria = 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 &= ~EXT4_MB_HINT_ALIGNED; + } + /* * searching for the right group start * from the goal value specified @@ -2993,6 +3014,24 @@ ext4_mb_regular_allocator(struct ext4_allocation_context *ac) if (!err && ac->ac_status != AC_STATUS_FOUND && first_err) err = first_err; + if (ac->ac_flags & EXT4_MB_HINT_ALIGNED && ac->ac_status == AC_STATUS_FOUND) { + ext4_fsblk_t start = ext4_grp_offs_to_block(sb, &ac->ac_b_ex); + ext4_grpblk_t len = 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 = 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 != 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); @@ -4440,6 +4479,13 @@ ext4_mb_normalize_request(struct ext4_allocation_context *ac, if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC) return; + /* + * 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 ; @@ -4794,6 +4840,10 @@ ext4_mb_use_preallocated(struct ext4_allocation_context *ac) if (!(ac->ac_flags & EXT4_MB_HINT_DATA)) return false; + /* 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 156908641e68..79cc4224fbbd 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" }) #define show_map_flags(flags) __print_flags(flags, "|", \ From patchwork Wed Dec 11 07:57:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ojaswin Mujoo X-Patchwork-Id: 13903063 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 E65C11D6199; Wed, 11 Dec 2024 07:58:12 +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=1733903894; cv=none; b=cXMzTY4OJnZMFCuhrA9tN3V6vTy6IcQqEmFTJkW7XwBKm59K0VOQxciRRBWWWpmc6TqSdRw1/7CY8W2NXpN4xToCw/mxGu02lVdv6FNNcN7zXZg3FVDFDY5osJw7CwYD7h1SdIEdhQXHTMlerS5j7reSIMOKQsUovTVt+a5Nw2w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733903894; c=relaxed/simple; bh=tgkjsBdLe6XjB+3Og3UJ9Af08aioXP5i4O/7SFI+lys=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mGrHtBEaDm8uKrAi7YDds1m1FTLWZJRlUoWM7LWdAAh+/KOTHY1rnL3OEK77hIife0XsKoBwhCuZC/GV09hhb+qhgXs3T1DcwkqDl11KEk5MuqbJSdRtHLLOR1k5UlQyrT/s6/6rLSwZbOfd2dnGcvtnKhlI/DcZ9PCtgtuPrhc= 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=C3MaTwRW; 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="C3MaTwRW" 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 4BAM1jOT009037; Wed, 11 Dec 2024 07:58:05 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=YCSqPevAtP19vU3Po oeadSd6bxNvOLLOnMFkzwR1LOg=; b=C3MaTwRWbKsbltoAqzidadbacxBUlYS14 pAfTzxKB7YuROzixv2mOt/T7Ez4LUZj547Z2fVMyWu9N5LEgdMfQP44goclhCxF+ gKCg07zAYJMQVNzMFUkbSQR1NzbyK8QY+AFXvMEf7nZK/+g9UCzrhfYfXSftpiIO UX7bTmj/5xFv5nLPZjOEirg3RO5PHK3tc8EuhJUK+pdZxIArC5JQa0zAFkrj16pI zNX5lwu1D8nwzNUSLjme2mdiPDPPjriyXq4nDnJRVplw3dvtl4sLDDk7baXw8d3K kW6sOp4cr4MuUi+u/UpMWrdYJR7viOj1GLCeWxx2C+udxkCABosHQ== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 43ce38uve7-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 11 Dec 2024 07:58:05 +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 4BB7trnG025864; Wed, 11 Dec 2024 07:58:04 GMT Received: from ppma13.dal12v.mail.ibm.com (dd.9e.1632.ip4.static.sl-reverse.com [50.22.158.221]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 43ce38uve4-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 11 Dec 2024 07:58:04 +0000 (GMT) Received: from pps.filterd (ppma13.dal12v.mail.ibm.com [127.0.0.1]) by ppma13.dal12v.mail.ibm.com (8.18.1.2/8.18.1.2) with ESMTP id 4BB7NM1L023047; Wed, 11 Dec 2024 07:58:03 GMT Received: from smtprelay05.fra02v.mail.ibm.com ([9.218.2.225]) by ppma13.dal12v.mail.ibm.com (PPS) with ESMTPS id 43d2wjyxvc-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 11 Dec 2024 07:58:03 +0000 Received: from smtpav04.fra02v.mail.ibm.com (smtpav04.fra02v.mail.ibm.com [10.20.54.103]) by smtprelay05.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 4BB7w25G36897122 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 11 Dec 2024 07:58:02 GMT Received: from smtpav04.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 1092520043; Wed, 11 Dec 2024 07:58:02 +0000 (GMT) Received: from smtpav04.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 5EC4F20040; Wed, 11 Dec 2024 07:58:00 +0000 (GMT) Received: from li-bb2b2a4c-3307-11b2-a85c-8fa5c3a69313.ibm.com.com (unknown [9.39.30.217]) by smtpav04.fra02v.mail.ibm.com (Postfix) with ESMTP; Wed, 11 Dec 2024 07:58:00 +0000 (GMT) From: Ojaswin Mujoo To: linux-ext4@vger.kernel.org, "Theodore Ts'o" Cc: Ritesh Harjani , linux-kernel@vger.kernel.org, "Darrick J . Wong" , linux-fsdevel@vger.kernel.org, John Garry , dchinner@redhat.com, Nirjhar Roy Subject: [RFC v2 2/6] ext4: allow inode preallocation for aligned alloc Date: Wed, 11 Dec 2024 13:27:51 +0530 Message-ID: <1e6ea627ac0b8caf202c1430e44ddd2b9d320644.1733901374.git.ojaswin@linux.ibm.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-GUID: x7sysE1pgs_vbWvxe30mgYU-8xZ57af5 X-Proofpoint-ORIG-GUID: HJahXmGBJSUC2Z5KPOdcMm4qyuER63p4 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1051,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-10-15_01,2024-10-11_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 adultscore=0 suspectscore=0 spamscore=0 clxscore=1015 priorityscore=1501 lowpriorityscore=0 impostorscore=0 bulkscore=0 mlxscore=0 malwarescore=0 mlxlogscore=999 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2411120000 definitions=main-2412110051 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 9763349598e9..a500a561c47d 100644 --- a/fs/ext4/mballoc.c +++ b/fs/ext4/mballoc.c @@ -2178,8 +2178,6 @@ static void ext4_mb_use_best_found(struct ext4_allocation_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); } @@ -3027,8 +3025,7 @@ ext4_mb_regular_allocator(struct ext4_allocation_context *ac) 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 != ac->ac_o_ex.fe_len); + WARN_ON_ONCE(ac->ac_b_ex.fe_len < ac->ac_o_ex.fe_len); } exit: @@ -4479,13 +4476,6 @@ ext4_mb_normalize_request(struct ext4_allocation_context *ac, if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC) return; - /* - * 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 ; @@ -4542,6 +4532,21 @@ ext4_mb_normalize_request(struct ext4_allocation_context *ac, size = (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 = ac->ac_o_ex.fe_logical << bsbits; + size = max_t(loff_t, size, + EXT4_C2B(sbi, ac->ac_o_ex.fe_len) << bsbits); + } size = size >> bsbits; start = start_off >> bsbits; @@ -4792,32 +4797,46 @@ ext4_mb_check_group_pa(ext4_fsblk_t goal_block, } /* - * 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 = EXT4_SB(ac->ac_sb); ext4_fsblk_t start; - 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; - /* - * 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 = 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) != 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 = 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) != start) + 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; + 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 = 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; + } return true; } @@ -4840,10 +4859,6 @@ ext4_mb_use_preallocated(struct ext4_allocation_context *ac) if (!(ac->ac_flags & EXT4_MB_HINT_DATA)) return false; - /* 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. * @@ -4949,7 +4964,7 @@ ext4_mb_use_preallocated(struct ext4_allocation_context *ac) goto try_group_pa; } - 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); @@ -4984,6 +4999,19 @@ ext4_mb_use_preallocated(struct ext4_allocation_context *ac) * pa_free == 0. */ WARN_ON_ONCE(tmp_pa->pa_free == 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 |= EXT4_MB_HINT_NOPREALLOC; } spin_unlock(&tmp_pa->pa_lock); try_group_pa: @@ -5790,6 +5818,7 @@ static void ext4_mb_group_or_file(struct ext4_allocation_context *ac) int bsbits = ac->ac_sb->s_blocksize_bits; loff_t size, isize; bool inode_pa_eligible, group_pa_eligible; + bool is_aligned = (ac->ac_flags & EXT4_MB_HINT_ALIGNED); if (!(ac->ac_flags & EXT4_MB_HINT_DATA)) return; @@ -5797,7 +5826,8 @@ static void ext4_mb_group_or_file(struct ext4_allocation_context *ac) if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY)) return; - group_pa_eligible = sbi->s_mb_group_prealloc > 0; + /* Aligned allocation does not support group pa */ + group_pa_eligible = (!is_aligned && sbi->s_mb_group_prealloc > 0); inode_pa_eligible = true; size = extent_logical_end(sbi, &ac->ac_o_ex); isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1) From patchwork Wed Dec 11 07:57:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ojaswin Mujoo X-Patchwork-Id: 13903065 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 DD6EE1D8A08; Wed, 11 Dec 2024 07:58:15 +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=1733903897; cv=none; b=SMnTCH7WwCEjwAWj+QLbB0YnopNrHCZmSw/gurmanOAU53whVLQDBlR5MSLRRXaA64l2YBcF2S/j5SVzQharl9D6AfQcnPiYhD8K4EYMTOrBJLw8cEr+tmX4q+HWKVBCzcNKsXvbIUwMVZVv20C6uKNuqNanQ4ON7AfrCyzBLVU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733903897; c=relaxed/simple; bh=Mgf1YHGsSe/K1NxZNnDPVQwbZ7+NkO89c6Lrz/zkKQ4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ti0ZdwKCWm/0FyY2iUv6iCxcUNTjcEIGPvHEX6GEOOuJainLg08xsWyKJNUrW5xtR0UijV6xKCNAK+D68kNsOg0vR0zHbUWetMYI04eJg2y8P1zeGvmLU6ota+nkiQJkkWEiI+TisbqG/Ov3ythWO1e99Vrc12XFMsxP6cvL05k= 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=DYnno/Cf; 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="DYnno/Cf" 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 4BAMXpBo025862; Wed, 11 Dec 2024 07:58:07 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=r3Elre7/yhg7vCeAT VTTxYqyq67KZzmrA0vLHG4TIUY=; b=DYnno/CfHKlTlQ6attVIRdc2xT3x89vB0 8cVxsFTz9dN5KhEsW+/P1m6daglWgY8k+cWnZKQ/bplCi/1ADDvknlQ9DnSYQOzw 8SFtkxbyK83imZbiTtph1nk/6zGo6g0Yzf8l1yU7UB7gOce3vZSQ4JdjQtxBEIlY me+jngKRAJu6EGTKRnWcBFXlCZzCQYC7wmnZlAwKlOCAxAysGHVo+XCYhRvtnJmy 05CvE4XYChgFMHFvqRH7Ofkp9ZNxWxjeC/L3AcJCDPC37j1mO8VVa66wtAhURZYW EcIjg1p3v1EUCVlvVDH0fnp3dtJIx2KiEAhArH3KufCL9W1SbI1gA== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 43cbsqb3xp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 11 Dec 2024 07:58:06 +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 4BB7riXv001005; Wed, 11 Dec 2024 07:58:06 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 43cbsqb3xg-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 11 Dec 2024 07:58:06 +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 4BB7Zk0X017428; Wed, 11 Dec 2024 07:58:05 GMT Received: from smtprelay07.fra02v.mail.ibm.com ([9.218.2.229]) by ppma11.dal12v.mail.ibm.com (PPS) with ESMTPS id 43d3d1qvdk-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 11 Dec 2024 07:58:05 +0000 Received: from smtpav04.fra02v.mail.ibm.com (smtpav04.fra02v.mail.ibm.com [10.20.54.103]) by smtprelay07.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 4BB7w49665077580 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 11 Dec 2024 07:58:04 GMT Received: from smtpav04.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id F1E9920040; Wed, 11 Dec 2024 07:58:03 +0000 (GMT) Received: from smtpav04.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 560CD20043; Wed, 11 Dec 2024 07:58:02 +0000 (GMT) Received: from li-bb2b2a4c-3307-11b2-a85c-8fa5c3a69313.ibm.com.com (unknown [9.39.30.217]) by smtpav04.fra02v.mail.ibm.com (Postfix) with ESMTP; Wed, 11 Dec 2024 07:58:02 +0000 (GMT) From: Ojaswin Mujoo To: linux-ext4@vger.kernel.org, "Theodore Ts'o" Cc: Ritesh Harjani , linux-kernel@vger.kernel.org, "Darrick J . Wong" , linux-fsdevel@vger.kernel.org, John Garry , dchinner@redhat.com, Nirjhar Roy Subject: [RFC v2 3/6] ext4: support for extsize hint using FS_IOC_FS(GET/SET)XATTR Date: Wed, 11 Dec 2024 13:27:52 +0530 Message-ID: <5002cd0f881bbcd5303455c51193807d7221f999.1733901374.git.ojaswin@linux.ibm.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: FxixmN85AHneV4wgg2uoJGuNeExO9RLB X-Proofpoint-GUID: q1x4GmmgaVrI8cXxN_nKIXaYxorizw-p X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1051,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-10-15_01,2024-10-11_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxlogscore=999 adultscore=0 lowpriorityscore=0 clxscore=1015 phishscore=0 impostorscore=0 suspectscore=0 spamscore=0 mlxscore=0 priorityscore=1501 malwarescore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2411120000 definitions=main-2412110056 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 fbcde1e71b03..6e889a92247c 100644 --- a/fs/ext4/ext4.h +++ b/fs/ext4/ext4.h @@ -1170,6 +1170,8 @@ struct ext4_inode_info { __u32 i_csum_seed; kprojid_t i_projid; + /* The extentsize hint for the inode in blocks */ + ext4_grpblk_t i_extsize; }; /* @@ -3031,6 +3033,10 @@ extern void ext4_da_update_reserve_space(struct inode *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 extsize); +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 extsize); /* indirect.c */ extern int ext4_ind_map_blocks(handle_t *handle, struct inode *inode, diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index aadd8bb3c679..73097d584ca9 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -4931,6 +4931,20 @@ struct inode *__ext4_iget(struct super_block *sb, unsigned long ino, } } + ret = ext4_inode_xattr_get_extsize(&ei->vfs_inode); + if (ret >= 0) { + ei->i_extsize = ret; + } else if (ret == -ENODATA) { + /* extsize is not set */ + ei->i_extsize = 0; + } else { + ext4_error_inode( + inode, function, line, 0, + "iget: error while retrieving extsize from xattr: %ld", ret); + ret = -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); @@ -6241,3 +6255,78 @@ vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf) ext4_journal_stop(handle); goto out; } + +/* + * 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 = 0; + ext4_grpblk_t extsize = 0; + + size = ext4_xattr_get(inode, EXT4_XATTR_INDEX_SYSTEM, "extsize", NULL, 0); + + if (size == -ENODATA || size == 0) { + return 0; + } else if (size < 0) { + ret = size; + goto exit; + } + + buf = (char *)kmalloc(size + 1, GFP_KERNEL); + if (!buf) { + ret = -ENOMEM; + goto exit; + } + + size = ext4_xattr_get(inode, EXT4_XATTR_INDEX_SYSTEM, "extsize", buf, + size); + if (size == -ENODATA) + /* No extsize is set */ + extsize = 0; + else if (size < 0) + ret = size; + else { + buf[size] = '\0'; + ret = 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 extsize) +{ + int err = 0; + /* max value of extsize should fit within 11 chars */ + char extsize_str[11]; + + err = 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 = ext4_xattr_set(inode, EXT4_XATTR_INDEX_SYSTEM, "extsize", + extsize_str, strlen(extsize_str), XATTR_REPLACE); + + if (err == -ENODATA) + err = 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 extsize) +{ + ei->i_extsize = extsize; +} diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c index 1c77400bd88e..ffe56a603456 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; } +static u32 ext4_ioctl_getextsize(struct inode *inode) +{ + ext4_grpblk_t extsize; + + extsize = ext4_inode_get_extsize(EXT4_I(inode)); + + return (u32) extsize << inode->i_blkbits; +} + + +static int ext4_ioctl_setextsize(struct inode *inode, u32 extsize, u32 xflags) +{ + int err; + ext4_grpblk_t extsize_blks = extsize >> inode->i_blkbits; + struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); + int blksize = 1 << inode->i_blkbits; + char *msg = NULL; + + if (!S_ISREG(inode->i_mode)) { + msg = "Cannot set extsize on non regular file"; + err = -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 = "Cannot set extsize on file with data"; + err = -EINVAL; + goto error; + } + + if (extsize % blksize) { + msg = "extsize must be multiple of blocksize"; + err = -EINVAL; + goto error; + } + + if (sbi->s_cluster_ratio > 1) { + msg = "Can't use extsize hint with bigalloc"; + err = -EINVAL; + goto error; + } + + if ((xflags & FS_XFLAG_EXTSIZE) && extsize == 0) { + msg = "fsx_extsize can't be 0 if FS_XFLAG_EXTSIZE is passed"; + err = -EINVAL; + goto error; + } + + if (extsize_blks > sbi->s_blocks_per_group) { + msg = "extsize cannot exceed number of bytes in block group"; + err = -EINVAL; + goto error; + } + + if (extsize && !is_power_of_2(extsize_blks)) { + msg = "extsize must be either power-of-2 in fs blocks or 0"; + err = -EINVAL; + goto error; + } + + if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { + msg = "extsize can't be set on non-extent based files"; + err = -EINVAL; + goto error; + } + + /* update the extsize in inode xattr */ + err = 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 fileattr *fa) struct inode *inode = d_inode(dentry); struct ext4_inode_info *ei = EXT4_I(inode); u32 flags = ei->i_flags & EXT4_FL_USER_VISIBLE; + u32 extsize = 0; if (S_ISREG(inode->i_mode)) flags &= ~FS_PROJINHERIT_FL; @@ -993,6 +1081,13 @@ int ext4_fileattr_get(struct dentry *dentry, struct fileattr *fa) if (ext4_has_feature_project(inode->i_sb)) fa->fsx_projid = from_kprojid(&init_user_ns, ei->i_projid); + extsize = ext4_ioctl_getextsize(inode); + /* Flag is only set if extsize is non zero */ + if (extsize > 0) { + fa->fsx_extsize = extsize; + fa->fsx_xflags |= FS_XFLAG_EXTSIZE; + } + return 0; } @@ -1022,6 +1117,33 @@ int ext4_fileattr_set(struct mnt_idmap *idmap, if (err) goto out; err = ext4_ioctl_setproject(inode, fa->fsx_projid); + if (err) + goto out; + + if (fa->fsx_xflags & FS_XFLAG_EXTSIZE) { + err = ext4_ioctl_setextsize(inode, fa->fsx_extsize, + fa->fsx_xflags); + if (err) + goto out; + } else if (fa->fsx_extsize == 0) { + /* + * Even when user explicitly passes extsize=0 the flag is cleared in + * fileattr_set_prepare(). + */ + if (ext4_inode_get_extsize(EXT4_I(inode)) != 0) { + err = ext4_ioctl_setextsize(inode, fa->fsx_extsize, + fa->fsx_xflags); + if (err) + goto out; + } + + } else { + /* Unexpected usage, reset extsize to 0 */ + err = ext4_ioctl_setextsize(inode, 0, fa->fsx_xflags); + if (err) + goto out; + fa->fsx_xflags = 0; + } out: return err; } diff --git a/fs/ext4/super.c b/fs/ext4/super.c index a09f4621b10d..7acdb6d51d80 100644 --- a/fs/ext4/super.c +++ b/fs/ext4/super.c @@ -1428,6 +1428,7 @@ static struct inode *ext4_alloc_inode(struct super_block *sb) spin_lock_init(&ei->i_completed_io_lock); ei->i_sync_tid = 0; ei->i_datasync_tid = 0; + ei->i_extsize = 0; atomic_set(&ei->i_unwritten, 0); INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work); ext4_fc_init_inode(&ei->vfs_inode); From patchwork Wed Dec 11 07:57:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ojaswin Mujoo X-Patchwork-Id: 13903064 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 B8B5E1D89FD; Wed, 11 Dec 2024 07:58:15 +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=1733903897; cv=none; b=A/GLFnjxgk1EEmktiYmufQTjZw0bJBAwjbfeypv46H0Eu8pRYhODXBVoQ1zf4A5pinvViX9aB2iISeIUTKNB08wPU2iMqdyV3+N4lrca7xjDCxSFDbeokQ/YW6kH/1D1KXjkKbItT2LQodmG8tP2jsukywy5AXNygQdCcqILdYc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733903897; c=relaxed/simple; bh=x6lR300+v3pYu2909ppa7a7w/98zMfnEokqzVY7l3oM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oFZWmmtNpy2wbxnD3gKnMVGnI+vXN3lRdE4CHIkbHvgbCdpqlbqhh8CnLVqkB/+gt161Ya9styMHI4dDJ8jhOnabcjAJCsBEwtSKQ89KU1pAhbTLOrulmDizzAJBYblpv0v4zn4+PQS2ZD9I3CS7P2rClcYSJi5ePDU9sTO770g= 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=Pb8U1qmZ; 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="Pb8U1qmZ" 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 4BB00c8Y007195; Wed, 11 Dec 2024 07:58:09 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=9LObTc9ZuVTH43XFG B13SIbNi89VdBVAkTkQAtQ3BPQ=; b=Pb8U1qmZcLsH0zNx6Bphk7dxqrWBJH0JN XOYCTlniACbOib0XQ1UEXwyMgWf8qJ4afdjSrYI5seK2sT+ptrsw+P9F/xIXqSe4 1dQocygggNLXVR34UIBQGPScq1pc+GjDpz9vQRvLbrT/D3E8ss+6z4OqgA+vREX1 uIwZo4Xk9dM+hITdRgzxdzOgPsqtfw3tfIfhzwW/Kc8X0myL60CNlUZdgJsQOT4T eaU+Aaek1OTpgDfHHj1yYSun/YvLhWHzfmR7y8agr/GldVxfN8xTf3hA9zoZJ1nV J/QvVk3NDJwOM/qULx6n6grPXj4+hc0oBbfzHismbysxDs3PomLNQ== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 43cdv8uw6x-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 11 Dec 2024 07:58:09 +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 4BB7lWnB024091; Wed, 11 Dec 2024 07:58:08 GMT Received: from ppma12.dal12v.mail.ibm.com (dc.9e.1632.ip4.static.sl-reverse.com [50.22.158.220]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 43cdv8uw6t-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 11 Dec 2024 07:58:08 +0000 (GMT) Received: from pps.filterd (ppma12.dal12v.mail.ibm.com [127.0.0.1]) by ppma12.dal12v.mail.ibm.com (8.18.1.2/8.18.1.2) with ESMTP id 4BB5GYUh032754; Wed, 11 Dec 2024 07:58:07 GMT Received: from smtprelay02.fra02v.mail.ibm.com ([9.218.2.226]) by ppma12.dal12v.mail.ibm.com (PPS) with ESMTPS id 43d0psgf77-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 11 Dec 2024 07:58:07 +0000 Received: from smtpav04.fra02v.mail.ibm.com (smtpav04.fra02v.mail.ibm.com [10.20.54.103]) by smtprelay02.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 4BB7w5oK34865530 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 11 Dec 2024 07:58:06 GMT Received: from smtpav04.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id DD70120043; Wed, 11 Dec 2024 07:58:05 +0000 (GMT) Received: from smtpav04.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 43AA020040; Wed, 11 Dec 2024 07:58:04 +0000 (GMT) Received: from li-bb2b2a4c-3307-11b2-a85c-8fa5c3a69313.ibm.com.com (unknown [9.39.30.217]) by smtpav04.fra02v.mail.ibm.com (Postfix) with ESMTP; Wed, 11 Dec 2024 07:58:04 +0000 (GMT) From: Ojaswin Mujoo To: linux-ext4@vger.kernel.org, "Theodore Ts'o" Cc: Ritesh Harjani , linux-kernel@vger.kernel.org, "Darrick J . Wong" , linux-fsdevel@vger.kernel.org, John Garry , dchinner@redhat.com, Nirjhar Roy Subject: [RFC v2 4/6] ext4: pass lblk and len explicitly to ext4_split_extent*() Date: Wed, 11 Dec 2024 13:27:53 +0530 Message-ID: <21b9db7b9978113077056744d26a2b7579af653b.1733901374.git.ojaswin@linux.ibm.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-GUID: xnOTm05UWH6WeAS6AZbp9E9KpqOT3tQe X-Proofpoint-ORIG-GUID: xr5E1xLvKx2aag3_jiS2OscVqlgoQPsG X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1051,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-10-15_01,2024-10-11_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 impostorscore=0 lowpriorityscore=0 spamscore=0 clxscore=1015 mlxscore=0 malwarescore=0 adultscore=0 phishscore=0 suspectscore=0 mlxlogscore=633 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2411120000 definitions=main-2412110056 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 a07a98a4b97a..4f2dd20d6da5 100644 --- a/fs/ext4/extents.c +++ b/fs/ext4/extents.c @@ -3347,7 +3347,8 @@ static struct ext4_ext_path *ext4_split_extent_at(handle_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) { @@ -3363,7 +3364,7 @@ static struct ext4_ext_path *ext4_split_extent(handle_t *handle, ee_len = ext4_ext_get_actual_len(ex); unwritten = ext4_ext_is_unwritten(ex); - if (map->m_lblk + map->m_len < ee_block + ee_len) { + if (lblk + len < ee_block + ee_len) { split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT; flags1 = flags | EXT4_GET_BLOCKS_PRE_IO; if (unwritten) @@ -3372,28 +3373,28 @@ static struct ext4_ext_path *ext4_split_extent(handle_t *handle, if (split_flag & EXT4_EXT_DATA_VALID2) split_flag1 |= EXT4_EXT_DATA_VALID1; path = 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 = ext4_find_extent(inode, map->m_lblk, path, flags); + path = ext4_find_extent(inode, lblk, path, flags); if (IS_ERR(path)) return path; depth = ext_depth(inode); ex = 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 = ext4_ext_is_unwritten(ex); } - if (map->m_lblk >= ee_block) { + if (lblk >= ee_block) { split_flag1 = split_flag & EXT4_EXT_DATA_VALID2; if (unwritten) { split_flag1 |= EXT4_EXT_MARK_UNWRIT1; @@ -3401,16 +3402,16 @@ static struct ext4_ext_path *ext4_split_extent(handle_t *handle, EXT4_EXT_MARK_UNWRIT2); } path = ext4_split_extent_at(handle, inode, path, - map->m_lblk, split_flag1, flags); + lblk, split_flag1, flags); if (IS_ERR(path)) return path; } if (allocated) { - if (map->m_lblk + map->m_len > ee_block + ee_len) - *allocated = ee_len - (map->m_lblk - ee_block); + if (lblk + len > ee_block + ee_len) + *allocated = ee_len - (lblk - ee_block); else - *allocated = map->m_len; + *allocated = len; } ext4_ext_show_leaf(inode, path); return path; @@ -3658,8 +3659,8 @@ ext4_ext_convert_to_initialized(handle_t *handle, struct inode *inode, } fallback: - path = ext4_split_extent(handle, inode, path, &split_map, split_flag, - flags, NULL); + path = 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: @@ -3699,11 +3700,11 @@ ext4_ext_convert_to_initialized(handle_t *handle, struct 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; @@ -3712,12 +3713,12 @@ static struct ext4_ext_path *ext4_split_convert_extents(handle_t *handle, int split_flag = 0, depth; ext_debug(inode, "logical block %llu, max_blocks %u\n", - (unsigned long long)map->m_lblk, map->m_len); + (unsigned long long)lblk, len); eof_block = (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 = map->m_lblk + map->m_len; + if (eof_block < lblk + len) + eof_block = lblk + len; /* * It is safe to convert extent to initialized via explicit * zeroout only if extent is fully inside i_size or new_size. @@ -3737,8 +3738,8 @@ static struct ext4_ext_path *ext4_split_convert_extents(handle_t *handle, split_flag |= (EXT4_EXT_MARK_UNWRIT2 | EXT4_EXT_DATA_VALID2); } flags |= 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); } static struct ext4_ext_path * @@ -3773,7 +3774,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 = ext4_split_convert_extents(handle, inode, map, path, + path = ext4_split_convert_extents(handle, inode, map->m_lblk, map->m_len, path, EXT4_GET_BLOCKS_CONVERT, NULL); if (IS_ERR(path)) return path; @@ -3837,8 +3838,9 @@ convert_initialized_extent(handle_t *handle, struct inode *inode, (unsigned long long)ee_block, ee_len); if (ee_block != map->m_lblk || ee_len > map->m_len) { - path = ext4_split_convert_extents(handle, inode, map, path, - EXT4_GET_BLOCKS_CONVERT_UNWRITTEN, NULL); + path = 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; @@ -3909,8 +3911,9 @@ ext4_ext_handle_unwritten_extents(handle_t *handle, struct inode *inode, /* get_block() before submitting IO, split the extent */ if (flags & EXT4_GET_BLOCKS_PRE_IO) { - path = ext4_split_convert_extents(handle, inode, map, path, - flags | EXT4_GET_BLOCKS_CONVERT, allocated); + path = 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; /* From patchwork Wed Dec 11 07:57:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ojaswin Mujoo X-Patchwork-Id: 13903066 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 8EA421E9B02; Wed, 11 Dec 2024 07:58:21 +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=1733903904; cv=none; b=YA+WFhakUaK549ZraT1aBb10GSAIwYTvdwricUwQ/6IJ+76Ic+m016TgPVYx7/dlReIGnIFjtuCD/x2uFXVPazO5A7bF6CpybowK4xcG4iQUQsC0zUEkMZtPPikEuXqkFJ2PKqNZA4SfE3kyJ9do17EadUjAzDaXMQNZPJ+9fI4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733903904; c=relaxed/simple; bh=dsnCv0hKz0qmhwAZg6jNuiHTo6QrwAOfFjUisTL2UjE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EVvkf2C+OQ4QsPBlpVUFrECvx14CAbbdb7TuH5zL0CcqZIKrXQ10I07CVliEUUX3oOjCVU+KYT3/qOe2bhXeW+X9oSyEypDrp62ooIsQlVLfO9MeCSqEyn1rPQoePveEnDB7K/eFPdiYM2XGgoarQe4Y1lAgeEUR+w99yCnGKT0= 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=bzDdUr8M; 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="bzDdUr8M" 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 4BANjFMm009013; Wed, 11 Dec 2024 07:58:13 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=L4BfIb+JNm9krBjrj 81q50x0c9JuJcHKJYrAkbfRgB8=; b=bzDdUr8Mp/dd8ssvFu8abU5aLZa5Y5ww9 5LgAj/UZeKK2kppeVBmOWglATprl9bgcObWt7n1qWWw15kN39rAyjJfha/m2J5ux dSa30HHM0rKrkf0Ipp41mJ61ynFDYXK01CYURWXoHb+DneLwaoiECd1KrsJTkcfN 6AEBDnEj3DyFr95oBJX1sPaBLfpbHUaJOnXkwSxwM9UnnofMRNnDe3Po8M1oLPwK v1idIbfJrU+Mhi4SviS//DmnSTUz+EDMAwzMFBgE8l5U7Fxr0IAijyewR1sLt+6Z Kulo4qNYMfHdSa66tyx9cFwGFLpM3B+ZTKoVNgAcrXZ9hUsRtWbfw== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 43ce38uvf2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 11 Dec 2024 07:58:13 +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 4BB7phhK016493; Wed, 11 Dec 2024 07:58:12 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 43ce38uvey-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 11 Dec 2024 07:58:12 +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 4BB6p23U018608; Wed, 11 Dec 2024 07:58:11 GMT Received: from smtprelay05.fra02v.mail.ibm.com ([9.218.2.225]) by ppma23.wdc07v.mail.ibm.com (PPS) with ESMTPS id 43d26kg59n-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 11 Dec 2024 07:58:11 +0000 Received: from smtpav04.fra02v.mail.ibm.com (smtpav04.fra02v.mail.ibm.com [10.20.54.103]) by smtprelay05.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 4BB7w9V756623466 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 11 Dec 2024 07:58:09 GMT Received: from smtpav04.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 0CE9720043; Wed, 11 Dec 2024 07:58:09 +0000 (GMT) Received: from smtpav04.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 309F320040; Wed, 11 Dec 2024 07:58:06 +0000 (GMT) Received: from li-bb2b2a4c-3307-11b2-a85c-8fa5c3a69313.ibm.com.com (unknown [9.39.30.217]) by smtpav04.fra02v.mail.ibm.com (Postfix) with ESMTP; Wed, 11 Dec 2024 07:58:06 +0000 (GMT) From: Ojaswin Mujoo To: linux-ext4@vger.kernel.org, "Theodore Ts'o" Cc: Ritesh Harjani , linux-kernel@vger.kernel.org, "Darrick J . Wong" , linux-fsdevel@vger.kernel.org, John Garry , dchinner@redhat.com, Nirjhar Roy Subject: [RFC v2 5/6] ext4: add extsize hint support Date: Wed, 11 Dec 2024 13:27:54 +0530 Message-ID: X-Mailer: git-send-email 2.43.5 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-GUID: -I__ci6HgDRroEOZKBT3VLTPdYuFx6xo X-Proofpoint-ORIG-GUID: TaozBwHKRTcK95SZwbfqPFwjTK_QGEKR X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1051,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-10-15_01,2024-10-11_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 adultscore=0 suspectscore=0 spamscore=0 clxscore=1015 priorityscore=1501 lowpriorityscore=0 impostorscore=0 bulkscore=0 mlxscore=0 malwarescore=0 mlxlogscore=999 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2411120000 definitions=main-2412110051 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 | 4 +- fs/ext4/ext4_jbd2.h | 15 ++ fs/ext4/extents.c | 169 +++++++++++++++-- fs/ext4/inode.c | 354 ++++++++++++++++++++++++++++++++---- include/trace/events/ext4.h | 1 + 5 files changed, 492 insertions(+), 51 deletions(-) diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h index 6e889a92247c..04ae42f985d4 100644 --- a/fs/ext4/ext4.h +++ b/fs/ext4/ext4.h @@ -722,6 +722,7 @@ enum { #define EXT4_GET_BLOCKS_IO_SUBMIT 0x0400 /* Caller is in the atomic contex, find extent if it has been cached */ #define EXT4_GET_BLOCKS_CACHED_NOWAIT 0x0800 +#define EXT4_GET_BLOCKS_EXTSIZE 0x1000 /* * The bit position of these flags must not overlap with any of the @@ -3696,7 +3697,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 0c77697d5e90..20bbf76c0556 100644 --- a/fs/ext4/ext4_jbd2.h +++ b/fs/ext4/ext4_jbd2.h @@ -513,4 +513,19 @@ static inline int ext4_should_dioread_nolock(struct inode *inode) return 1; } +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 4f2dd20d6da5..efb70edbf414 100644 --- a/fs/ext4/extents.c +++ b/fs/ext4/extents.c @@ -3889,15 +3889,24 @@ convert_initialized_extent(handle_t *handle, struct inode *inode, 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 = 0; - 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); + if (flags & EXT4_GET_BLOCKS_EXTSIZE) { + BUG_ON(extsize_map == NULL); + map = extsize_map; + } else + map = 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); /* @@ -3906,13 +3915,14 @@ ext4_ext_handle_unwritten_extents(handle_t *handle, struct inode *inode, */ flags |= EXT4_GET_BLOCKS_METADATA_NOFAIL; - trace_ext4_ext_handle_unwritten_extents(inode, map, flags, - *allocated, newblock); + trace_ext4_ext_handle_unwritten_extents(inode, map, flags, *allocated, + newblock); /* get_block() before submitting IO, split the extent */ if (flags & EXT4_GET_BLOCKS_PRE_IO) { + /* Split should always happen based on original mapping */ path = 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; @@ -3927,11 +3937,19 @@ ext4_ext_handle_unwritten_extents(handle_t *handle, struct inode *inode, err = -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 = orig_map->m_lblk; map->m_flags |= EXT4_MAP_UNWRITTEN; goto out; } /* IO end_io complete, convert the filled extent to written */ if (flags & EXT4_GET_BLOCKS_CONVERT) { + BUG_ON(map == extsize_map); path = ext4_convert_unwritten_extents_endio(handle, inode, map, path); if (IS_ERR(path)) @@ -4189,7 +4207,8 @@ static ext4_lblk_t ext4_ext_determine_insert_hole(struct 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 = NULL; struct ext4_extent newex, *ex, ex2; @@ -4200,6 +4219,17 @@ int ext4_ext_map_blocks(handle_t *handle, struct inode *inode, unsigned int allocated_clusters = 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 = NULL; +#endif + + if (flags & EXT4_GET_BLOCKS_EXTSIZE) { + BUG_ON(extsize_map == NULL); + map = extsize_map; + } else + map = orig_map; + 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); @@ -4256,6 +4286,7 @@ int ext4_ext_map_blocks(handle_t *handle, struct inode *inode, */ if ((!ext4_ext_is_unwritten(ex)) && (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) { + BUG_ON(map == extsize_map); path = convert_initialized_extent(handle, inode, map, path, &allocated); if (IS_ERR(path)) @@ -4272,8 +4303,8 @@ int ext4_ext_map_blocks(handle_t *handle, struct inode *inode, } path = 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 = PTR_ERR(path); goto out; @@ -4306,6 +4337,7 @@ int ext4_ext_map_blocks(handle_t *handle, struct inode *inode, */ if (cluster_offset && ex && get_implied_cluster_alloc(inode->i_sb, map, ex, path)) { + BUG_ON(map == extsize_map); ar.len = allocated = map->m_len; newblock = map->m_pblk; goto got_allocated_blocks; @@ -4325,6 +4357,7 @@ int ext4_ext_map_blocks(handle_t *handle, struct inode *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 == extsize_map); ar.len = allocated = map->m_len; newblock = map->m_pblk; err = 0; @@ -4379,6 +4412,8 @@ int ext4_ext_map_blocks(handle_t *handle, struct inode *inode, ar.flags |= EXT4_MB_DELALLOC_RESERVED; if (flags & EXT4_GET_BLOCKS_METADATA_NOFAIL) ar.flags |= EXT4_MB_USE_RESERVED; + if (flags & EXT4_GET_BLOCKS_EXTSIZE) + ar.flags |= EXT4_MB_HINT_ALIGNED; newblock = ext4_mb_new_blocks(handle, &ar, &err); if (!newblock) goto out; @@ -4400,9 +4435,114 @@ int ext4_ext_map_blocks(handle_t *handle, struct inode *inode, map->m_flags |= EXT4_MAP_UNWRITTEN; } - path = ext4_ext_insert_extent(handle, inode, path, &newex, flags); - if (IS_ERR(path)) { - err = 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 = le32_to_cpu(newex.ee_block); + loff_t newex_len = ext4_ext_get_actual_len(&newex); + + if (in_range(orig_map->m_lblk, newex_lblk, newex_len)) { + /* S1 */ + loff_t split_len = 0; + + BUG_ON(!ext4_ext_is_unwritten(&newex)); + + if (newex_lblk + newex_len >= + orig_map->m_lblk + (loff_t)orig_map->m_len) + split_len = orig_map->m_len; + else + split_len = newex_len - + (orig_map->m_lblk - newex_lblk); + + path = ext4_ext_insert_extent( + handle, inode, path, &newex, + (flags & ~EXT4_GET_BLOCKS_PRE_IO)); + if (IS_ERR(path)) { + err = 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 = ext4_find_extent(inode, orig_map->m_lblk, path, 0); + if (IS_ERR(path)) { + err = 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 = ext4_split_convert_extents( + handle, inode, orig_map->m_lblk, split_len, + path, flags | EXT4_GET_BLOCKS_CONVERT, NULL); + if (IS_ERR(path)) { + err = PTR_ERR(path); + goto insert_error; + } + +#ifdef CONFIG_EXT4_DEBUG + test_path = 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 = + test_path[ext_depth(inode)].p_ext; + WARN_ON(!ext4_ext_is_unwritten(test_ex)); + WARN_ON(test_ex->ee_block != orig_map->m_lblk); + WARN_ON(ext4_ext_get_actual_len(test_ex) != + orig_map->m_len); + kfree(test_path); + } +#endif + } else { + /* S2 */ + BUG_ON(orig_map->m_lblk < newex_lblk + newex_len); + + path = ext4_ext_insert_extent( + handle, inode, path, &newex, + (flags & ~EXT4_GET_BLOCKS_PRE_IO)); + if (IS_ERR(path)) { + err = PTR_ERR(path); + goto insert_error; + } + } + } else { + path = ext4_ext_insert_extent(handle, inode, path, &newex, + flags); + if (IS_ERR(path)) { + err = PTR_ERR(path); + goto insert_error; + } + } + +insert_error: + if (err) { if (allocated_clusters) { int fb_flags = 0; @@ -4798,6 +4938,9 @@ long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len) inode_lock(inode); + if (ext4_should_use_extsize(inode)) + flags |= EXT4_GET_BLOCKS_EXTSIZE; + /* * We only support preallocation for extent-based files only */ diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index 73097d584ca9..d511282ebdcc 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -434,7 +434,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 = ext4_ext_map_blocks(handle, inode, map, 0); + retval = ext4_ext_map_blocks(handle, inode, map, NULL, 0); } else { retval = ext4_ind_map_blocks(handle, inode, map, 0); } @@ -459,15 +459,33 @@ static void ext4_map_blocks_es_recheck(handle_t *handle, #endif /* ES_AGGRESSIVE_TEST */ static int ext4_map_query_blocks(handle_t *handle, struct inode *inode, - struct ext4_map_blocks *map) + struct ext4_map_blocks *orig_map, + struct ext4_map_blocks *extsize_map, + bool should_extsize) { unsigned int status; int retval; + struct ext4_map_blocks *map; + + if (should_extsize) { + BUG_ON(extsize_map == NULL); + map = extsize_map; + } else + map = orig_map; if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) - retval = ext4_ext_map_blocks(handle, inode, map, 0); - else + if (should_extsize) { + retval = ext4_ext_map_blocks(handle, inode, orig_map, + map, + EXT4_GET_BLOCKS_EXTSIZE); + } else { + retval = ext4_ext_map_blocks(handle, inode, map, NULL, + 0); + } + else { + BUG_ON(should_extsize); retval = ext4_ind_map_blocks(handle, inode, map, 0); + } if (retval <= 0) return retval; @@ -488,11 +506,20 @@ static int ext4_map_query_blocks(handle_t *handle, struct inode *inode, } 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, + bool should_extsize) { struct extent_status es; unsigned int status; int err, retval = 0; + struct ext4_map_blocks *map; + + if (should_extsize) { + BUG_ON(extsize_map == NULL); + map = extsize_map; + } else + map = orig_map; /* * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE @@ -513,8 +540,15 @@ static int ext4_map_create_blocks(handle_t *handle, struct inode *inode, * changed the inode type in between. */ if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { - retval = ext4_ext_map_blocks(handle, inode, map, flags); + if (should_extsize) { + retval = ext4_ext_map_blocks(handle, inode, orig_map, + map, flags); + } else { + retval = ext4_ext_map_blocks(handle, inode, map, NULL, + flags); + } } else { + BUG_ON(should_extsize); retval = ext4_ind_map_blocks(handle, inode, map, flags); /* @@ -569,6 +603,80 @@ static int ext4_map_create_blocks(handle_t *handle, struct inode *inode, return retval; } +/** + * Extsize hint will change the mapped range and hence we'll end up mapping 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 mapping + * 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 = (__u64)extsize_map->m_lblk + extsize_map->m_len; + + BUG_ON(cur_map != extsize_map || !(flags & EXT4_GET_BLOCKS_EXTSIZE)); + + orig_map->m_len = min(orig_map->m_len, map_end - orig_map->m_lblk); + orig_map->m_pblk = + extsize_map->m_pblk + (orig_map->m_lblk - extsize_map->m_lblk); + orig_map->m_flags = extsize_map->m_flags; + + return orig_map->m_len; +} + +/** + * ext4_error_adjust_map - Adjust map returned upon error in ext4_map_blocks() + * + * @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, however + * 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 caller is + * the one we were working with when error happened. Mostly useful when extsize + * 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 != orig_map) + memcpy(orig_map, cur_map, sizeof(*cur_map)); +} + +/* + * This functions resets the mapping to it's original state after it has been + * 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 normal + * 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 = orig_flags & ~EXT4_GET_BLOCKS_EXTSIZE; + map->m_lblk = orig_mlblk; + map->m_len = orig_mlen; + map->m_flags = 0; +} + /* * The ext4_map_blocks() function tries to look up the requested blocks, * and returns if the blocks are already mapped. @@ -593,31 +701,111 @@ static int ext4_map_create_blocks(handle_t *handle, struct 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 = 0; + + ext4_lblk_t orig_mlblk, extsize_mlblk; + unsigned int orig_mlen, extsize_mlen; + int orig_flags; + + struct ext4_map_blocks *map = NULL; + struct ext4_map_blocks extsize_map = {0}; + + __u32 extsize = ext4_inode_get_extsize(EXT4_I(inode)); + bool should_extsize = false; + #ifdef ES_AGGRESSIVE_TEST - struct ext4_map_blocks orig_map; + struct ext4_map_blocks test_map; - memcpy(&orig_map, map, sizeof(*map)); + memcpy(&test_map, map, sizeof(*map)); #endif - map->m_flags = 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 = 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); /* * ext4_map_blocks returns an int, and m_len is an unsigned int */ - if (unlikely(map->m_len > INT_MAX)) - map->m_len = INT_MAX; + if (unlikely(orig_map->m_len > INT_MAX)) + orig_map->m_len = INT_MAX; /* We can handle the block number less than EXT_MAX_BLOCKS */ - if (unlikely(map->m_lblk >= EXT_MAX_BLOCKS)) + if (unlikely(orig_map->m_lblk >= EXT_MAX_BLOCKS)) return -EFSCORRUPTED; + orig_mlblk = orig_map->m_lblk; + orig_mlen = orig_map->m_len; + orig_flags = flags; + +set_map: + should_extsize = (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 = ((1UL << 15) - 1); + loff_t end; + + align = orig_map->m_lblk % extsize; + len = orig_map->m_len + align; + + extsize_map.m_lblk = orig_map->m_lblk - align; + extsize_map.m_len = + 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 = (extsize_map.m_lblk + (loff_t)extsize_map.m_len) << inode->i_blkbits; + if (end > inode->i_size) { + flags = orig_flags & ~EXT4_GET_BLOCKS_EXTSIZE; + goto set_map; + } + + /* Fallback to normal allocation if we go beyond max len */ + if (extsize_map.m_len >= max_unwrit_len) { + flags = 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 (!(flags | EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT)) { + WARN_ON(true); + + /* Fallback to non extsize allocation */ + flags = orig_flags & ~EXT4_GET_BLOCKS_EXTSIZE; + goto set_map; + } + + extsize_mlblk = extsize_map.m_lblk; + extsize_mlen = extsize_map.m_len; + + extsize_map.m_flags = orig_map->m_flags; + map = &extsize_map; + } else + map = orig_map; + /* Lookup extent status tree firstly */ if (!(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) && ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) { @@ -647,7 +835,7 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode, return retval; #ifdef ES_AGGRESSIVE_TEST ext4_map_blocks_es_recheck(handle, inode, map, - &orig_map, flags); + &test_map, flags); #endif goto found; } @@ -663,19 +851,60 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode, * file system block. */ down_read(&EXT4_I(inode)->i_data_sem); - retval = ext4_map_query_blocks(handle, inode, map); + if (should_extsize) { + BUG_ON(map != &extsize_map); + retval = ext4_map_query_blocks(handle, inode, orig_map, map, + should_extsize); + } else { + BUG_ON(map != orig_map); + retval = ext4_map_query_blocks(handle, inode, map, NULL, + should_extsize); + } up_read((&EXT4_I(inode)->i_data_sem)); found: if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { ret = check_block_validity(inode, map); - if (ret != 0) + if (ret != 0) { + ext4_error_adjust_map(map, orig_map); return ret; + } } /* If it is only a block(s) look up */ - if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) + if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { + BUG_ON(flags & EXT4_GET_BLOCKS_EXTSIZE); return retval; + } + + /* Handle some special cases when extsize based allocation is needed */ + if (retval >= 0 && flags & EXT4_GET_BLOCKS_EXTSIZE) { + bool orig_in_range = + 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 != extsize_mlblk || + map->m_len != extsize_mlen) { + flags = orig_flags & ~EXT4_GET_BLOCKS_EXTSIZE; + goto set_map; + } + } /* * Returns if the blocks have already allocated @@ -699,12 +928,22 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode, * with create == 1 flag. */ down_write(&EXT4_I(inode)->i_data_sem); - retval = ext4_map_create_blocks(handle, inode, map, flags); + if (should_extsize) { + BUG_ON(map != &extsize_map); + retval = ext4_map_create_blocks(handle, inode, orig_map, map, flags, + should_extsize); + } else { + BUG_ON(map != orig_map); + retval = ext4_map_create_blocks(handle, inode, map, NULL, flags, + should_extsize); + } up_write((&EXT4_I(inode)->i_data_sem)); if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { ret = check_block_validity(inode, map); - if (ret != 0) + if (ret != 0) { + ext4_error_adjust_map(map, orig_map); return ret; + } /* * Inodes with freshly allocated blocks where contents will be @@ -726,16 +965,38 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode, else ret = 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 = 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; } @@ -771,18 +1032,20 @@ static int _ext4_get_block(struct inode *inode, sector_t iblock, { struct ext4_map_blocks map; int ret = 0; + unsigned int orig_mlen = bh->b_size >> inode->i_blkbits; if (ext4_has_inline_data(inode)) return -ERANGE; map.m_lblk = iblock; - map.m_len = bh->b_size >> inode->i_blkbits; + map.m_len = orig_mlen; ret = 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 != orig_mlen); bh->b_size = inode->i_sb->s_blocksize * map.m_len; ret = 0; } else if (ret == 0) { @@ -808,11 +1071,14 @@ int ext4_get_block_unwritten(struct inode *inode, sector_t iblock, struct buffer_head *bh_result, int create) { int ret = 0; + int flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT; + + if (ext4_should_use_extsize(inode)) + flags |= EXT4_GET_BLOCKS_EXTSIZE; ext4_debug("ext4_get_block_unwritten: inode %lu, create flag %d\n", inode->i_ino, create); - ret = _ext4_get_block(inode, iblock, bh_result, - EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT); + ret = _ext4_get_block(inode, iblock, bh_result, flags); /* * If the buffer is marked unwritten, mark it as new to make sure it is @@ -1162,7 +1428,8 @@ static int ext4_write_begin(struct file *file, struct address_space *mapping, from = pos & (PAGE_SIZE - 1); to = from + len; - 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 = ext4_try_to_write_inline_data(mapping, inode, pos, len, foliop); if (ret < 0) @@ -1210,7 +1477,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); - if (ext4_should_dioread_nolock(inode)) + if (ext4_should_use_unwrit_extents(inode)) ret = ext4_block_write_begin(handle, folio, pos, len, ext4_get_block_unwritten); else @@ -1800,7 +2067,7 @@ static int ext4_da_map_blocks(struct inode *inode, struct ext4_map_blocks *map) if (ext4_has_inline_data(inode)) retval = 0; else - retval = ext4_map_query_blocks(NULL, inode, map); + retval = ext4_map_query_blocks(NULL, inode, map, NULL, false); up_read(&EXT4_I(inode)->i_data_sem); if (retval) return retval < 0 ? retval : 0; @@ -1823,7 +2090,7 @@ static int ext4_da_map_blocks(struct inode *inode, struct ext4_map_blocks *map) goto found; } } else if (!ext4_has_inline_data(inode)) { - retval = ext4_map_query_blocks(NULL, inode, map); + retval = ext4_map_query_blocks(NULL, inode, map, NULL, false); if (retval) { up_write(&EXT4_I(inode)->i_data_sem); return retval < 0 ? retval : 0; @@ -2197,6 +2464,7 @@ static int mpage_map_one_extent(handle_t *handle, struct mpage_da_data *mpd) struct ext4_map_blocks *map = &mpd->map; int get_blocks_flags; int err, dioread_nolock; + int extsize = ext4_should_use_extsize(inode); trace_ext4_da_write_pages_extent(inode, map); /* @@ -2215,11 +2483,14 @@ static int mpage_map_one_extent(handle_t *handle, struct mpage_da_data *mpd) dioread_nolock = ext4_should_dioread_nolock(inode); if (dioread_nolock) get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT; + if (extsize) + get_blocks_flags |= EXT4_GET_BLOCKS_PRE_IO; err = 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 = handle->h_rsv_handle; @@ -2642,10 +2913,11 @@ static int ext4_do_writepages(struct mpage_da_data *mpd) } mpd->journalled_more_data = 0; - if (ext4_should_dioread_nolock(inode)) { + if (ext4_should_use_unwrit_extents(inode)) { /* - * We may need to convert up to one extent per block in - * the page 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. */ rsv_blocks = 1 + ext4_chunk_trans_blocks(inode, PAGE_SIZE >> inode->i_blkbits); @@ -2920,7 +3192,8 @@ static int ext4_da_write_begin(struct file *file, struct address_space *mapping, index = pos >> PAGE_SHIFT; - 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 = (void *)FALL_BACK_TO_NONDELALLOC; return ext4_write_begin(file, mapping, pos, len, foliop, fsdata); @@ -3367,12 +3640,19 @@ static int ext4_iomap_alloc(struct inode *inode, struct 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) >= i_size_read(inode)) m_flags = 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 = EXT4_GET_BLOCKS_IO_CREATE_EXT; + if (ext4_should_use_extsize(inode)) + m_flags |= EXT4_GET_BLOCKS_EXTSIZE; + } + ret = ext4_map_blocks(handle, inode, map, m_flags); /* @@ -6200,7 +6480,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 = ext4_get_block_unwritten; else get_block = ext4_get_block; diff --git a/include/trace/events/ext4.h b/include/trace/events/ext4.h index 79cc4224fbbd..d9464ee764af 100644 --- a/include/trace/events/ext4.h +++ b/include/trace/events/ext4.h @@ -50,6 +50,7 @@ struct partial_cluster; { EXT4_GET_BLOCKS_CONVERT_UNWRITTEN, "CONVERT_UNWRITTEN" }, \ { EXT4_GET_BLOCKS_ZERO, "ZERO" }, \ { EXT4_GET_BLOCKS_IO_SUBMIT, "IO_SUBMIT" }, \ + { EXT4_GET_BLOCKS_EXTSIZE, "EXTSIZE" }, \ { EXT4_EX_NOCACHE, "EX_NOCACHE" }) /* From patchwork Wed Dec 11 07:57:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ojaswin Mujoo X-Patchwork-Id: 13903067 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 677611D0E28; Wed, 11 Dec 2024 07:58:23 +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=1733903905; cv=none; b=tZ2rvxH1z8du6kHhGnct5JVlxJe9gl5kQLjW4gSEO8LLumMIQTXMIQ211xuuRv+B8i/HYfZTKzs26OESPYbuC3d25g4VsMQ4jlbnlR/xG+QJvMiW/znBog8PWDojOaU0lAHGJ96T5nZI5upZ90fH64B/bnh79lDc2P4j+/28NXo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733903905; c=relaxed/simple; bh=tMpv5LeOHSfLNp3qLGN7hTotuOB8L59eDUlqDg/ma1o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ArnbxUoib3RQvPacjbw1BoyXmAMdNGvUhvvR5rHjP69xxUtsFeF+bXNNW0Cwd2VMWtso3oXouGMSEZiQA2NTPzkzmlge9AFy2eYp/D0GnqKOokOZub7zVXwdoMQt581uvEsSMhlXKTT1N04Q118wwyq59r/OSkRRdwnJOgOh5Yw= 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=hfa9xS2W; 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="hfa9xS2W" 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 4BB1SJLP025987; Wed, 11 Dec 2024 07:58:14 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=4qMIY5BJghurRRbbx ix/ChndzugGNuMDrQpQehDbW88=; b=hfa9xS2WGCf2FD36hdCE5gxOlCGugJXNe 7teJ3Vs7EDE6Io3vXmK4e+MTQGVXVQN1wYwlrBJqVSi31ymizvxDERsoeTKi36ep 5vR/L+js7Cmk/M976XWEgMO22qmnpTgUmMD6WdEE5FPQCrHeqsPEit5QWBveZMFc Qx7734mDSYQlhU0etmtnAkO8ErApNcRPUdhGKoyFRsgr4u88tsn3USp10eXq0Wif hf3cjAmbj441621f8XztNJxYA9qJlB3yZeWtVFqUb5dgVvHS1t4W4pZerbwAzBir ZO935FHrzfzvFm5XO/cF4xa9rvNTqBWKzpQM+jzJxW9UGL3ggxkgA== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 43cbsqb3yp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 11 Dec 2024 07:58:14 +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 4BB7sHo6002061; Wed, 11 Dec 2024 07:58:13 GMT Received: from ppma12.dal12v.mail.ibm.com (dc.9e.1632.ip4.static.sl-reverse.com [50.22.158.220]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 43cbsqb3yn-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 11 Dec 2024 07:58:13 +0000 (GMT) Received: from pps.filterd (ppma12.dal12v.mail.ibm.com [127.0.0.1]) by ppma12.dal12v.mail.ibm.com (8.18.1.2/8.18.1.2) with ESMTP id 4BB5kEJs032739; Wed, 11 Dec 2024 07:58:12 GMT Received: from smtprelay07.fra02v.mail.ibm.com ([9.218.2.229]) by ppma12.dal12v.mail.ibm.com (PPS) with ESMTPS id 43d0psgf7d-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 11 Dec 2024 07:58:12 +0000 Received: from smtpav04.fra02v.mail.ibm.com (smtpav04.fra02v.mail.ibm.com [10.20.54.103]) by smtprelay07.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 4BB7wBqR64422356 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 11 Dec 2024 07:58:11 GMT Received: from smtpav04.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 1209F20043; Wed, 11 Dec 2024 07:58:11 +0000 (GMT) Received: from smtpav04.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 5256920040; Wed, 11 Dec 2024 07:58:09 +0000 (GMT) Received: from li-bb2b2a4c-3307-11b2-a85c-8fa5c3a69313.ibm.com.com (unknown [9.39.30.217]) by smtpav04.fra02v.mail.ibm.com (Postfix) with ESMTP; Wed, 11 Dec 2024 07:58:09 +0000 (GMT) From: Ojaswin Mujoo To: linux-ext4@vger.kernel.org, "Theodore Ts'o" Cc: Ritesh Harjani , linux-kernel@vger.kernel.org, "Darrick J . Wong" , linux-fsdevel@vger.kernel.org, John Garry , dchinner@redhat.com, Nirjhar Roy Subject: [RFC v2 6/6] ext4: make extsize work with EOF allocations Date: Wed, 11 Dec 2024 13:27:55 +0530 Message-ID: <57264a45aabcf8d789278e09da5db297d2e6b192.1733901374.git.ojaswin@linux.ibm.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: bckmckhFq7qMo8N5MAMiIA3mggTcN_lC X-Proofpoint-GUID: lPj7xrGTsT-2TUMthEIgt0gxaPnwaFKK X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1051,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-10-15_01,2024-10-11_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxlogscore=999 adultscore=0 lowpriorityscore=0 clxscore=1015 phishscore=0 impostorscore=0 suspectscore=0 spamscore=0 mlxscore=0 priorityscore=1501 malwarescore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2411120000 definitions=main-2412110056 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 d511282ebdcc..d292e39a050a 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -756,7 +756,6 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode, * ext4_extents.h here? */ int max_unwrit_len = ((1UL << 15) - 1); - loff_t end; align = orig_map->m_lblk % extsize; len = orig_map->m_len + align; @@ -765,18 +764,6 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode, extsize_map.m_len = 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 = (extsize_map.m_lblk + (loff_t)extsize_map.m_len) << inode->i_blkbits; - if (end > inode->i_size) { - flags = orig_flags & ~EXT4_GET_BLOCKS_EXTSIZE; - goto set_map; - } - /* Fallback to normal allocation if we go beyond max len */ if (extsize_map.m_len >= max_unwrit_len) { flags = orig_flags & ~EXT4_GET_BLOCKS_EXTSIZE; @@ -3641,10 +3628,13 @@ static int ext4_iomap_alloc(struct inode *inode, struct 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) >= i_size_read(inode)) + else if (((loff_t)map->m_lblk << blkbits) >= i_size_read(inode) && + !ext4_should_use_extsize(inode)) m_flags = EXT4_GET_BLOCKS_CREATE; else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { m_flags = EXT4_GET_BLOCKS_IO_CREATE_EXT;