Received: by 2002:ab2:710b:0:b0:1ef:a325:1205 with SMTP id z11csp1039508lql; Tue, 12 Mar 2024 05:59:38 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXtkMQ0pDqN1H43zVkkUGFHM9aZymfn6bOH9g+VeOs9/Wumvjl9bJEFKwYI8wtjI6H+1BkvfnA3dtUmMqnGWqu/zMILd1J9vBaYkOJx4g== X-Google-Smtp-Source: AGHT+IFoVtkr6euY/RwRoc7GYDZcU7bkIsTaxrIFaSSiTmptFa/wLlOvxnwBGy8wE64ok9YLY6fC X-Received: by 2002:a50:9f8f:0:b0:566:1fbd:22bb with SMTP id c15-20020a509f8f000000b005661fbd22bbmr6603403edf.9.1710248378049; Tue, 12 Mar 2024 05:59:38 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710248378; cv=pass; d=google.com; s=arc-20160816; b=K6MS396SSSHPNuVzftErJw69rGs8x/F7j9Welj8Ok9k+qKkSa8eOA4spY32b/qETjM J/F3+v9rK0Xi32EwSI3jRPQKBAclev688IkrUXn6sGAdsxnQ2gFhJKvW+N8rOveDsMQR 8k/JBA7ZGjxdTEv6FRVRzR5kKO6NB7Wvyz2LlmW3PeUvO0JGk3aTkIaKIyW+vSDYEHhA otAgIDFnmdhoUjPIuDI9HOGBnhmnauonfUt9YeEBK2WFr2bgxlSVR1lGVwmWFPqZPdNT dubJX/FFiNxINKqulEUbU4LFqOtgG10EhLA6lhzNXs+6WatbX9YsWWVscWVgk+3IHY8+ 2Iwg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:content-language:in-reply-to:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:user-agent:date :message-id:from:references:cc:to:subject; bh=R2UjKGm8euoqaF+L40MvhlflzNVZP1uxYB+7FwSUHmU=; fh=+dID43e+zysSUQt2Cn5xtLzOUM3ZysVQHoyNPERjLwE=; b=gmmDccWmHX7o9jCEL3K+MKmXRWPOK1UiNpT3uAcfpFEdJtZDYWlmkSjfxG7tLJQwI+ D01h7fyQMErutHWzcZDeUzCYvO2fbd8OUjD3aM15XeRdvD5bPUvgfz1I7q0LnTOLhB2a HCsBptRYI/sM0Ip71kM/XtE2de86Ee69Zbe0+Qjovy+FrF2a7Y1jv6yXGiIby/cSrkPZ uT4wlMrFob+TjbAKp3KhjGHlX+2WR/oYWoaW6FqhDs/rEwA64bRdUhIi8pgLDXYvR+W9 26bbCLcFEKRX7hdJS4WL8lw3nO8ZjKSe8VwzzXYDPKEAQfi04X56XSruh4Tcw9cpA/NZ 32ig==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=huaweicloud.com); spf=pass (google.com: domain of linux-kernel+bounces-100274-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-100274-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id fg10-20020a056402548a00b005687b5cc2cfsi227506edb.182.2024.03.12.05.59.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Mar 2024 05:59:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-100274-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=huaweicloud.com); spf=pass (google.com: domain of linux-kernel+bounces-100274-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-100274-linux.lists.archive=gmail.com@vger.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 am.mirrors.kernel.org (Postfix) with ESMTPS id 9B73C1F23392 for ; Tue, 12 Mar 2024 12:59:37 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6526B7A146; Tue, 12 Mar 2024 12:59:26 +0000 (UTC) Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) (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 9323A1F95F; Tue, 12 Mar 2024 12:59:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.51 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710248365; cv=none; b=CpdQuV/rB5ScEej3EKT6R0/EXO/vtUeA5NSDqf7/Eksmm3wdJ591oe4jA/kMw6Cm1kEYvDIqhD7Cel/1ld4Fe8Tgl28PeRhJ/vmyCVUnNmWoGftLoby7GPJrCDqeI9xvlV/JdXWNJr7d+S4yOiCWqg0ixXuZzNz8HUvs3pe2rHg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710248365; c=relaxed/simple; bh=7tSnGJBsA/vNSf47BJiztKMZrl6p5LWxW1fFH8nv4jQ=; h=Subject:To:Cc:References:From:Message-ID:Date:MIME-Version: In-Reply-To:Content-Type; b=m0maPNgBDl0XW1uCO6WH/TGHlMQ8IFbf1TjqHBJiPnLf0yYMd41dBymwBWHk2m/XGs0re/hxFhXS0rOLLnVuXeWjHT3isN8dIZdtwaLK3yRWnBrfQeUvL02NLBUpb7cXT3SQvEy9IBgeymPWZZDy3TKbmi5mjDY2a7Q4Iyh5dJo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.216]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4TvDHL2ypGz4f3lfX; Tue, 12 Mar 2024 20:59:10 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.112]) by mail.maildlp.com (Postfix) with ESMTP id 1B4BC1A0199; Tue, 12 Mar 2024 20:59:18 +0800 (CST) Received: from [10.174.176.34] (unknown [10.174.176.34]) by APP1 (Coremail) with SMTP id cCh0CgBXKBGjUfBlZsnnGg--.25952S3; Tue, 12 Mar 2024 20:59:17 +0800 (CST) Subject: Re: [PATCH 3/4] iomap: don't increase i_size if it's not a write operation To: "Darrick J. Wong" 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, tytso@mit.edu, jack@suse.cz, yi.zhang@huawei.com, chengzhihao1@huawei.com, yukuai3@huawei.com References: <20240311122255.2637311-1-yi.zhang@huaweicloud.com> <20240311122255.2637311-4-yi.zhang@huaweicloud.com> <20240311154829.GU1927156@frogsfrogsfrogs> From: Zhang Yi Message-ID: <4a9e607e-36d1-4ea7-1754-c443906b3a1c@huaweicloud.com> Date: Tue, 12 Mar 2024 20:59:15 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.12.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 In-Reply-To: <20240311154829.GU1927156@frogsfrogsfrogs> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-CM-TRANSID:cCh0CgBXKBGjUfBlZsnnGg--.25952S3 X-Coremail-Antispam: 1UD129KBjvJXoWxt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x5xdzvxpfor3voofrz/ On 2024/3/11 23:48, Darrick J. Wong wrote: > On Mon, Mar 11, 2024 at 08:22:54PM +0800, Zhang Yi wrote: >> From: Zhang Yi >> >> Increase i_size in iomap_zero_range() and iomap_unshare_iter() is not >> needed, the caller should handle it. Especially, when truncate partial >> block, we could not increase i_size beyond the new EOF here. It doesn't >> affect xfs and gfs2 now because they set the new file size after zero >> out, it doesn't matter that a transient increase in i_size, but it will >> affect ext4 because it set file size before truncate. > >> At the same time, >> iomap_write_failed() is also not needed for above two cases too, so >> factor them out and move them to iomap_write_iter() and >> iomap_zero_iter(). > > This change should be a separate patch with its own justification. > Which is, AFAICT, something along the lines of: > > "Unsharing and zeroing can only happen within EOF, so there is never a > need to perform posteof pagecache truncation if write begin fails." Sure. > >> Signed-off-by: Zhang Yi > > Doesn't this patch fix a bug in ext4? Yeah, the same as Christoph answered. > >> --- >> fs/iomap/buffered-io.c | 59 +++++++++++++++++++++--------------------- >> 1 file changed, 30 insertions(+), 29 deletions(-) >> >> diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c >> index 093c4515b22a..19f91324c690 100644 >> --- a/fs/iomap/buffered-io.c >> +++ b/fs/iomap/buffered-io.c >> @@ -786,7 +786,6 @@ static int iomap_write_begin(struct iomap_iter *iter, loff_t pos, >> >> out_unlock: >> __iomap_put_folio(iter, pos, 0, folio); >> - iomap_write_failed(iter->inode, pos, len); >> >> return status; >> } >> @@ -838,34 +837,13 @@ static size_t iomap_write_end(struct iomap_iter *iter, loff_t pos, size_t len, >> size_t copied, struct folio *folio) >> { >> const struct iomap *srcmap = iomap_iter_srcmap(iter); >> - loff_t old_size = iter->inode->i_size; >> - size_t ret; >> - >> - if (srcmap->type == IOMAP_INLINE) { >> - ret = iomap_write_end_inline(iter, folio, pos, copied); >> - } else if (srcmap->flags & IOMAP_F_BUFFER_HEAD) { >> - ret = block_write_end(NULL, iter->inode->i_mapping, pos, len, >> - copied, &folio->page, NULL); >> - } else { >> - ret = __iomap_write_end(iter->inode, pos, len, copied, folio); >> - } >> >> - /* >> - * Update the in-memory inode size after copying the data into the page >> - * cache. It's up to the file system to write the updated size to disk, >> - * preferably after I/O completion so that no stale data is exposed. >> - */ >> - if (pos + ret > old_size) { >> - i_size_write(iter->inode, pos + ret); >> - iter->iomap.flags |= IOMAP_F_SIZE_CHANGED; >> - } >> - __iomap_put_folio(iter, pos, ret, folio); >> - >> - if (old_size < pos) >> - pagecache_isize_extended(iter->inode, old_size, pos); >> - if (ret < len) >> - iomap_write_failed(iter->inode, pos + ret, len - ret); >> - return ret; >> + if (srcmap->type == IOMAP_INLINE) >> + return iomap_write_end_inline(iter, folio, pos, copied); >> + if (srcmap->flags & IOMAP_F_BUFFER_HEAD) >> + return block_write_end(NULL, iter->inode->i_mapping, pos, len, >> + copied, &folio->page, NULL); >> + return __iomap_write_end(iter->inode, pos, len, copied, folio); >> } >> >> static loff_t iomap_write_iter(struct iomap_iter *iter, struct iov_iter *i) >> @@ -880,6 +858,7 @@ static loff_t iomap_write_iter(struct iomap_iter *iter, struct iov_iter *i) >> >> do { >> struct folio *folio; >> + loff_t old_size; >> size_t offset; /* Offset into folio */ >> size_t bytes; /* Bytes to write to folio */ >> size_t copied; /* Bytes copied from user */ >> @@ -912,8 +891,10 @@ static loff_t iomap_write_iter(struct iomap_iter *iter, struct iov_iter *i) >> } >> >> status = iomap_write_begin(iter, pos, bytes, &folio); >> - if (unlikely(status)) >> + if (unlikely(status)) { >> + iomap_write_failed(iter->inode, pos, bytes); >> break; >> + } >> if (iter->iomap.flags & IOMAP_F_STALE) >> break; >> >> @@ -927,6 +908,24 @@ static loff_t iomap_write_iter(struct iomap_iter *iter, struct iov_iter *i) >> copied = copy_folio_from_iter_atomic(folio, offset, bytes, i); >> status = iomap_write_end(iter, pos, bytes, copied, folio); >> >> + /* >> + * Update the in-memory inode size after copying the data into >> + * the page cache. It's up to the file system to write the >> + * updated size to disk, preferably after I/O completion so that >> + * no stale data is exposed. >> + */ >> + old_size = iter->inode->i_size; >> + if (pos + status > old_size) { >> + i_size_write(iter->inode, pos + status); >> + iter->iomap.flags |= IOMAP_F_SIZE_CHANGED; >> + } >> + __iomap_put_folio(iter, pos, status, folio); > > Why is it necessary to hoist the __iomap_put_folio calls from > iomap_write_end into iomap_write_iter, iomap_unshare_iter, and > iomap_zero_iter? None of those functions seem to use it, and it makes > more sense to me that iomap_write_end releases the folio that > iomap_write_begin returned. > Because we have to update i_size before __iomap_put_folio() in iomap_write_iter(). If not, once we unlock folio, it could be raced by the backgroud write back which could start writing back and call folio_zero_segment() (please see iomap_writepage_handle_eof()) to zero out the valid data beyond the not updated i_size. So we have to move out __iomap_put_folio() out together with the i_size updating. Thanks, Yi.