From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.215.176 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796303192; b=HiJZCzn00dcJN82fNjkW2NsG5LDZ03AIAhJwf+P1mdM+fnCEb9URJrwloRxGgZqBywZR 0EHM0ZQR65Jetts7souCFFWww7E/KVMt2pH+HZQDcTBM6DsckWMNWRU9+C3l9ZKSOYHWQ iRbRIZXEhM300ie8ZSjiF6UOYHTEfeJ/EHtQuWPtkD+zqU+jAf326Qm3THjrUDUKi/vWY F5LBUGIw+7YVes/rvNEOXgjBVnI39zAqTnPPsCQAD2wgBwTh7R1SIuUv2Sd7n5Vd38og0 +iXXdysUyvVYHEFENG3tWf0cs8hbyNBSQ51RQMmNtXWo6RuPeScUfrm43A2anbmPOIw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796303192; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=jv4iCIweandI1njn3JGI3vasYkgXIWi/dd/j2sc0Ug0=; b=2oQjVF5Kq4w9mbokjujPmZE3PfiY8Kt5fW3Q0TuyBIYs4yotM9sq8IYs7RfDjSz5TXn+ u/+J+J0pdvDiE0GnMAsAuuhvHvLDwQ6PgT2qdCbU3kDA7CHN5e2IRUeCTThM4QA6qZRzu tjVTSnVzzBZ1shj6l+5EPOXtzciwfhJ+A5jMmOQH1RqM7uFg0dMXdkAz8es5V/U/VuhIc RzpcqXuN2Gb/lP3fIsVLldprlp9vKC92HsTkqJF6GAPC8FBK3K/teZQuxqRTqLo2NxXsy zJ1uwclzDaAkEnVxjdnPHuyb8rW31KygQc1VeNFptdlivdl/YpfxcAnxQ08qpBFE87A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=a8Qh839F; arc=none smtp.remote-ip=209.85.215.176 Received: from mail-pg1-f176.google.com (mail-pg1-f176.google.com [209.85.215.176]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2CD34606A32C for ; Thu, 3 Dec 2026 13:06:31 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2CD34606A32C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=a8Qh839F Received: by mail-pg1-f176.google.com with SMTP id 41be03b00d2f7-cdb2893531bso7744581a12.1 for ; Thu, 03 Dec 2026 05:06:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796303190; x=1796907990; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=jv4iCIweandI1njn3JGI3vasYkgXIWi/dd/j2sc0Ug0=; b=a8Qh839F1IrgVlkPQuyW7LNfv0SR+qNgD4Ts53tSLD6qG3B7KjIfuSgA5Aa6K1OBOr Bse7TqILCg/GYOqPUcg6O4Kjdk90CruLpJYS5Ow4DIwvPoju9NW8zztCQ0898RNEm0GE OyvGOOw+r46Dwe7RqHvYZ0lYWT6N+XtRkXVbM5UWliINluBPSmf22Hqkh+wJ4Aqb6PgL LPrIOpRMoXRJnfJW+59/GvnYMwneFWuYyi7diHy3XGraXpSDdBKttqoRCTi1VZMBZRb7 6s3e1Hn86WWREMrt/khJUJBiiTqfFe86QzYI5A4K68moAi25+Vdo7Q1naaZBtVVhUh1Z ADpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796303190; x=1796907990; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=jv4iCIweandI1njn3JGI3vasYkgXIWi/dd/j2sc0Ug0=; b=WitK7LEzWOCCrGF+Mt8eAWW4rG0LyBjyhXPp2Mpa+Lmbd7zvudJJGR4rBdrFrFKLfq mGh7NoK1rHJnI0hhVwlkTQzqIlEBfmDKp8U4zC29X+WWlSLFmbengXlliWvqYd+JaZOx /gtfxtI4Utnfcdak1DPL21xSr5DE4c9EVMRz+blbl2WpvrnUGYEF3JhlZQ9r+g5xwQPh 76DF5hjYExTaNH6AeITKs/TDBBsbvHTQls2MHPLzg8G4rj2kMA+cAVZiJR6Hmnx4RfD9 COxYiJ0O6KBFBf3ZYdCpcbov+o9Dc4T4nx+f5BJWzx79a3f1eN7QzG1/+G4ho8yRs8Wo FPiA== X-Forwarded-Encrypted: i=1; AL+hErEPRiCaALKIw3FPZ7695slFXTUHuejv8VOSbnIvOMt8Rra/32d56YlHmPDFRvIrWyl1n3nEgjilTGTU@vger.kernel.org X-Gm-Message-State: APuU4Z8MuzboiFAYxf8r4+FrQpE5v1933wiaxwwia+ftpUtaPg8SwK4r QgIjb+nUEkHWSKPaz4PNDu9RFSJsVsCiWjf5BsU5EX+ZpaowKZb+7EdT X-Gm-Gg: AZFdTZeepEJSDKVKwxprKZX1WxPSqeYmdKJ1hBDPOygubcnKZkyaH5cENN3vnw4wepS 5NljaSEDz5YIWtpPLU76VYHGqWC/FE6eSJ6aTfcUH+DvnIgLzu3zB8l3cELTt7F2qIUuPGEbwY8 lDosNNwMA0oGBXSv6Y+kkS9/+tKCowF4WmmKPlYagT33K7Ybn7R2q6EZz+oXtNsHTVArYwgZ1al HWHxrf8PRJN3Et2rUn4WwVn3/FMOBdIUqVtrVZ1YdBuWwzau1DSddPFMnKRK8nlxtXVGx1bcNmN euP52VJnfp8osQyB9X0naE7W7DaHyfmtu3fOes6aXMZk+TGPwUjSG460vVKelbODqmx2TKqpu/C Kjugub+TKt8fOOLACj/cjCYMWmT7ra2Y521mAm6tthG+4fMNqBXKVw84Eor94l5FEX4+Ic/ib/3 5A0kuxEuUAW9rh8K9dEa3xxISKxJVBChLeuyb9GPG46lxXyiOlH9Gqfs4oXg2hP32+x/1YrhNe8 VNn1+oIdZ5V7KkFgvHdRYunqRi9ckKjpN8oEGF+EAGKr2MWY60gvmIxIz/3qGYnJPk2P8SKveK1 kucJJRjImit0+zg0fYYUXUNyrA== X-Received: by 2002:a17:90b:2f4e:b0:3b5:4ce7:33de with SMTP id 98e67ed59e1d1-3b54ce73715mr19516911a91.64.1796303190356; Thu, 03 Dec 2026 05:06:30 -0800 (PST) Received: from localhost.localdomain ([2404:c140:2100:6::1b:9648]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-cde6d028a54sm22209945a12.18.2026.12.03.05.06.25 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 03 Dec 2026 05:06:30 -0800 (PST) From: Ding Feng To: tytso@mit.edu, adilger.kernel@dilger.ca Cc: jack@suse.cz, libaokun@linux.alibaba.com, ojaswin@linux.ibm.com, ritesh.list@gmail.com, yi.zhang@huawei.com, hughd@google.com, david@fromorbit.com, linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 2/2] ext4: punch hole beyond i_size Date: Thu, 3 Dec 2026 21:06:08 +0800 Message-ID: <20261203130608.64892-3-dingf3ng@gmail.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261203130608.64892-1-dingf3ng@gmail.com> References: <20261203130608.64892-1-dingf3ng@gmail.com> X-Mailing-List: linux-ext4@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit ext4_punch_hole() returns early when the hole starts at or past i_size, and otherwise ends it at the block that holds i_size. Blocks that fallocate(FALLOC_FL_KEEP_SIZE) allocated past EOF can therefore never be punched out; only a truncate frees them. XFS and tmpfs remove them. fallocate(2) says that "within the specified range, partial filesystem blocks are zeroed, and whole filesystem blocks are removed from the file". A 4096-byte file with 1 MiB allocated by FALLOC_FL_KEEP_SIZE, then PUNCH_HOLE|KEEP_SIZE over [4096, 1M), on v7.4-rc5 (x86_64, QEMU): ext4 st_blocks 2048 -> 2048 xfs st_blocks 2048 -> 8 tmpfs st_blocks 2048 -> 8 Hugh Dickins reported this in 2012 [1]. Allison Henderson's fix removed the early return [2], and Lukas Czerner carried it in a later series, but it was held back: the LSF 2012 discussion concluded to wait for range locks rather than take i_mutex for a punch beyond i_size [3]. Today ext4_punch_hole() always runs with the inode lock held. Remove extents up to the end of the range. A hole that reaches i_size still covers the whole block that holds i_size, as before, so that block is freed rather than zeroed. Zero partial blocks only for a hole that starts below i_size, and not past the block that holds i_size, so nothing past EOF is written. [1] https://lists.openwall.net/linux-ext4/2012/01/12/3 [2] https://lists.openwall.net/linux-ext4/2012/01/14/7 [3] https://lists.openwall.net/linux-ext4/2012/05/16/3 Reported-by: Hugh Dickins Fixes: 2be4751b21ae ("ext4: fix 2nd xfstests 127 punch hole failure") Signed-off-by: Ding Feng --- fs/ext4/inode.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index 26cf00ff8..f7d0a2e99 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -5356,6 +5356,7 @@ int ext4_punch_hole(struct file *file, loff_t offset, loff_t length) ext4_lblk_t start_lblk, end_lblk; loff_t max_end = sb->s_maxbytes; loff_t end = offset + length; + loff_t zero_end; handle_t *handle; unsigned int credits; unsigned int partial_zeroed = 0; @@ -5371,19 +5372,22 @@ int ext4_punch_hole(struct file *file, loff_t offset, loff_t length) if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) max_end = EXT4_SB(sb)->s_bitmap_maxbytes - sb->s_blocksize; - /* No need to punch hole beyond i_size */ - if (offset >= inode->i_size || offset >= max_end) + if (offset >= max_end) return 0; - /* - * If the hole extends beyond i_size, set the hole to end after - * the block that contains i_size to save pointless tail block zeroing. + * A hole that reaches i_size covers the rest of the block that holds + * i_size, so that block is freed rather than zeroed. */ if (end >= inode->i_size) - end = round_up(inode->i_size, sb->s_blocksize); + end = max_t(loff_t, end, round_up(inode->i_size, sb->s_blocksize)); if (end > max_end) end = max_end; length = end - offset; + /* + * Blocks past i_size (from FALLOC_FL_KEEP_SIZE) are removed too, but + * nothing after the block that contains i_size needs zeroing. + */ + zero_end = min_t(loff_t, end, round_up(inode->i_size, sb->s_blocksize)); ret = ext4_update_disksize_before_punch(inode, offset, length); if (ret) @@ -5394,9 +5398,12 @@ int ext4_punch_hole(struct file *file, loff_t offset, loff_t length) if (ret) return ret; - ret = ext4_zero_partial_blocks(inode, offset, length, &partial_zeroed); - if (ret) - return ret; + if (offset < inode->i_size) { + ret = ext4_zero_partial_blocks(inode, offset, zero_end - offset, + &partial_zeroed); + if (ret) + return ret; + } /* * On the iomap path, partial invalidate of a folio without an ifs * leaves sub-block dirty bits uncleared. Drain writeback before -- 2.39.5