Received: by 2002:a89:48b:0:b0:1f5:f2ab:c469 with SMTP id a11csp1311704lqd; Thu, 25 Apr 2024 11:29:18 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVJ0CdxFEg/yajGcHwPpquh6xHGAffEBfFLlow99nebKDhCpKy2n1mzTOgDX/pfgMQ0Yzs/oPgB3275I1m4jZTvYhl27PJa8r9FS6l++w== X-Google-Smtp-Source: AGHT+IGxto5K3xSD17nXoeZQy2+F48csgKdqQcs41lJAF0Qf0lqVxz4D+WIBCMT7MWbiANrp3cRX X-Received: by 2002:a05:6870:6b04:b0:22e:af38:47ee with SMTP id mt4-20020a0568706b0400b0022eaf3847eemr170104oab.58.1714069757861; Thu, 25 Apr 2024 11:29:17 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714069757; cv=pass; d=google.com; s=arc-20160816; b=NFmoNNnVPQ5cX6BIxhv3KVyF4C/OYpNnrQ99QHnL6KgkKWebkfasQSg4VRcYCwvWps ldpKa4hhF47qv9b/nFyPjvKlyJaCWbAyKEqnB88HOYrzWoE1QLDs+SWoJe+Ztr/VnBvs lcoxwLW/nXzsIYE/kkXu2yu2xEwaBNuWbUDwi/hiQl4R0MBftbOp7a19gUvETr+BBOeC E4XGCLlrZ/m+52tB/+KyAtx3n+q6wbH/1BzyX59MEl3pwToz8Wg0eNIlsxBbLpww5mVx z2CnNIKHGgBfNePHTSMF1Oe5ycJAi6NgORIdZsJC57ipoaHUXQi/xQkKZaTWiDg5rvgJ HKhQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=gDcNU4ywdcvtlrZmICOJbgKUhp4UAayvtuYZ8t4lgBM=; fh=q6g59nchACuuGCR809v+guY5AXVZT3PSRfaUt4nzDMY=; b=MopcNlf4TGcdwLO30DdGmUHxD/4mgMuxzXDXlaKNktTlW1C1oAn/B5JYQmZY30d5S5 zTZYverrJhDVe5qts7cGpjReOvwHd+rjc6LKTfxoyZIQaou8Tx5+ePmjZQ6eUmVO5W6/ 2CnHGUWx5kNR6yM22l8JexXQhkw/WXpD+79q/6GM7i0IU/AHEzXvZYkNNLByzznZv2SN IbgRJ2iT3teLs5JWnzU4zVUIek9WMkMFJXCt0Z7YTB35LB+uJkgUtzDF+nLNHHywCwmO ENqC/fDTR/O5PSovYOBjTM2L/CW5PV4FyUi9vRnWsFbh1SCImikIJYO69Hh/dFSpuLb1 f5LA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=BzEl2DPB; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-159014-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-159014-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id i2-20020a636d02000000b005f7ddef45bfsi12546107pgc.789.2024.04.25.11.29.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Apr 2024 11:29:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-159014-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=BzEl2DPB; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-159014-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-159014-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 75C53285326 for ; Thu, 25 Apr 2024 18:29:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A63641509AE; Thu, 25 Apr 2024 18:29:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="BzEl2DPB" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 BDE9837152; Thu, 25 Apr 2024 18:29:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714069745; cv=none; b=o4zXg68unLlCrpgb+AMBlqQNapfvbfZehY/V2nnosMozkzifwCf2pzLCBKj1PFrv2JtOOwWPjERthviScikZOft++YMOpIouWHBty2dm52UEBEb3xgv7+kCySKra8vp5abtg6hECmqWeqsG7s3MnKXhlZtqAWVQxOK9Ek2i5MXw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714069745; c=relaxed/simple; bh=OjxtnIQqi9aFP14zSB3IGsqrO8gHisSDjKe8KENwCio=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=efGnblDlSh133lar6S8DYChUHZhU1LTjN2LUKi/RywHtcBA3pVbHaK4QHr2OfxRbDI8+ESa+iP+hyIZyCVs4aU08ybVPZZkEEqU3ve74TUsUckhRRMeCzI1DO8O0344QEEBVWNFu32KIqcc3qeEkapiuZJ1B5Agdwv+O3+UXBIs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=BzEl2DPB; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3B5A7C113CC; Thu, 25 Apr 2024 18:29:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714069745; bh=OjxtnIQqi9aFP14zSB3IGsqrO8gHisSDjKe8KENwCio=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=BzEl2DPBRHB0UySCs9wigyeH0aWNCdavF127izSUNeOSg1PrSkI3v6OzhS8zMYTLH C+Ca9cJNPgeZOIcmwB2swbAPB+oclc7ibWYLIYKd8Hper5KPXVNOzEsLo/cNtJFgRg Nbqa4g6DHAI/yzRZnOhZ6X0vJ7rpzRtmTMNUpxWosQ50HLhe7LaF3R1wjuPb+kB0WN oYnmIbE9asaNG+OR9TOg5t/5JhPy0ciLw69L1ZeUlwpBKuoljQwFMgDPkDYXDb9vPF cYTkvZWnbUBGKsQebiJxSAWjPKYXUcR1iytGkFHAFnuvQYepz5+9WzN0TZZ6NtA4+C q2q32WLJaHbtQ== Date: Thu, 25 Apr 2024 11:29:04 -0700 From: "Darrick J. Wong" To: Zhang Yi Cc: linux-xfs@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, hch@infradead.org, brauner@kernel.org, david@fromorbit.com, chandanbabu@kernel.org, tytso@mit.edu, jack@suse.cz, yi.zhang@huawei.com, chengzhihao1@huawei.com, yukuai3@huawei.com Subject: Re: [PATCH v5 4/9] xfs: convert delayed extents to unwritten when zeroing post eof blocks Message-ID: <20240425182904.GA360919@frogsfrogsfrogs> References: <20240425131335.878454-1-yi.zhang@huaweicloud.com> <20240425131335.878454-5-yi.zhang@huaweicloud.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240425131335.878454-5-yi.zhang@huaweicloud.com> On Thu, Apr 25, 2024 at 09:13:30PM +0800, Zhang Yi wrote: > From: Zhang Yi > > Current clone operation could be non-atomic if the destination of a file > is beyond EOF, user could get a file with corrupted (zeroed) data on > crash. > > The problem is about preallocations. If you write some data into a file: > > [A...B) > > and XFS decides to preallocate some post-eof blocks, then it can create > a delayed allocation reservation: > > [A.........D) > > The writeback path tries to convert delayed extents to real ones by > allocating blocks. If there aren't enough contiguous free space, we can > end up with two extents, the first real and the second still delalloc: > > [A....C)[C.D) > > After that, both the in-memory and the on-disk file sizes are still B. > If we clone into the range [E...F) from another file: > > [A....C)[C.D) [E...F) > > then xfs_reflink_zero_posteof() calls iomap_zero_range() to zero out the > range [B, E) beyond EOF and flush it. Since [C, D) is still a delalloc > extent, its pagecache will be zeroed and both the in-memory and on-disk > size will be updated to D after flushing but before cloning. This is > wrong, because the user can see the size change and read the zeroes > while the clone operation is ongoing. > > We need to keep the in-memory and on-disk size before the clone > operation starts, so instead of writing zeroes through the page cache > for delayed ranges beyond EOF, we convert these ranges to unwritten and > invalidate any cached data over that range beyond EOF. > > Suggested-by: Dave Chinner > Signed-off-by: Zhang Yi > --- > Changes since v4: > > Move the delalloc converting hunk before searching the COW fork. Because > if the file has been reflinked and copied on write, > xfs_bmap_extsize_align() aligned the range of COW delalloc extent, after > the writeback, there might be some unwritten extents left over in the > COW fork that overlaps the delalloc extent we found in data fork. > > data fork ...wwww|dddddddddd... > cow fork |uuuuuuuuuu... > ^ > i_size > > In my v4, we search the COW fork before checking the delalloc extent, > goto found_cow tag and return unconverted delalloc srcmap in the above > case, so the delayed extent in the data fork will have no chance to > convert to unwritten, it will lead to delalloc extent residue and break > generic/522 after merging patch 6. Hmmm. I suppose that works, but it feels a little funny to convert the delalloc mapping in the data fork to unwritten /while/ there's unwritten extents in the cow fork too. Would it make more sense to remap the cow fork extents here? OTOH unwritten extents in the cow fork get changed to written ones by all the cow remapping functions. Soooo maybe we don't want to go digging /that/ deep into the system. Reviewed-by: Darrick J. Wong --D > > fs/xfs/xfs_iomap.c | 29 +++++++++++++++++++++++++++++ > 1 file changed, 29 insertions(+) > > diff --git a/fs/xfs/xfs_iomap.c b/fs/xfs/xfs_iomap.c > index 236ee78aa75b..2857ef1b0272 100644 > --- a/fs/xfs/xfs_iomap.c > +++ b/fs/xfs/xfs_iomap.c > @@ -1022,6 +1022,24 @@ xfs_buffered_write_iomap_begin( > goto out_unlock; > } > > + /* > + * For zeroing, trim a delalloc extent that extends beyond the EOF > + * block. If it starts beyond the EOF block, convert it to an > + * unwritten extent. > + */ > + if ((flags & IOMAP_ZERO) && imap.br_startoff <= offset_fsb && > + isnullstartblock(imap.br_startblock)) { > + xfs_fileoff_t eof_fsb = XFS_B_TO_FSB(mp, XFS_ISIZE(ip)); > + > + if (offset_fsb >= eof_fsb) > + goto convert_delay; > + if (end_fsb > eof_fsb) { > + end_fsb = eof_fsb; > + xfs_trim_extent(&imap, offset_fsb, > + end_fsb - offset_fsb); > + } > + } > + > /* > * Search the COW fork extent list even if we did not find a data fork > * extent. This serves two purposes: first this implements the > @@ -1167,6 +1185,17 @@ xfs_buffered_write_iomap_begin( > xfs_iunlock(ip, lockmode); > return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, 0, seq); > > +convert_delay: > + xfs_iunlock(ip, lockmode); > + truncate_pagecache(inode, offset); > + error = xfs_bmapi_convert_delalloc(ip, XFS_DATA_FORK, offset, > + iomap, NULL); > + if (error) > + return error; > + > + trace_xfs_iomap_alloc(ip, offset, count, XFS_DATA_FORK, &imap); > + return 0; > + > found_cow: > seq = xfs_iomap_inode_sequence(ip, 0); > if (imap.br_startoff <= offset_fsb) { > -- > 2.39.2 > >