From patchwork Tue Aug 20 23:20:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 13770637 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7D26BC3DA4A for ; Tue, 20 Aug 2024 23:21:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0C13B6B0093; Tue, 20 Aug 2024 19:21:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 070F46B0095; Tue, 20 Aug 2024 19:21:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E07566B0096; Tue, 20 Aug 2024 19:21:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id BC5C76B0093 for ; Tue, 20 Aug 2024 19:21:43 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 76F391A0561 for ; Tue, 20 Aug 2024 23:21:43 +0000 (UTC) X-FDA: 82474198086.21.D77F156 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf26.hostedemail.com (Postfix) with ESMTP id C855E140014 for ; Tue, 20 Aug 2024 23:21:41 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=bL5YwMs7; spf=pass (imf26.hostedemail.com: domain of dhowells@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724196039; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=9pcRiiH/lYTDVoni9WSyylXIzj+0iyqYBGBDYoCG5zI=; b=H9yos/6ts2LsEIyCMwqyzMh6/t88LJL1gObWxIncgNbZ2L/6YqzxTGP3yCemz4BScEX5t9 WUy5RH0yg8vZ6crLb4/1+D9D1tqL0W8lBtwpqVYlWZM9ufWdrE4DHsKCQb+vRrQJKR7UTS tFoZiyGd4dpxVE5MzClrIexSx7D9bw4= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=bL5YwMs7; spf=pass (imf26.hostedemail.com: domain of dhowells@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724196039; a=rsa-sha256; cv=none; b=MWYwE4jwWWuKv6A6zALleknvtWQABAxmShI1b5r5GvSLBjuBroZIEA0Ynwgg4/EDIFOYHh +rZyx/bFMbZvc+9Y+fTXZpzhXgBR44rXi6cF2iHN7gEDwtAgKsIal3W4ZudGREpC0gMFj1 4uJvEMXWSUbIisYf9xtDilaGyHui7hw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724196101; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9pcRiiH/lYTDVoni9WSyylXIzj+0iyqYBGBDYoCG5zI=; b=bL5YwMs7SvgXFDIvahEHcYDnzj21cS7pigOHa0kpfCXFP95pm7NVWFzc3U8WEhkRVqVxed VvhHazVjG6h8medU/VUoPleNV5kLzyDQibsA7HTT/dkOlK/FWIRQ1ujU9xeIukcQ+FQ6D/ dfpWey0SapXPLFXt/Nj0NRS3Bxqw1oA= Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-329-JolRJguvP_SguD5LijFaFg-1; Tue, 20 Aug 2024 19:21:39 -0400 X-MC-Unique: JolRJguvP_SguD5LijFaFg-1 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id BA9DE19560AD; Tue, 20 Aug 2024 23:21:36 +0000 (UTC) Received: from warthog.procyon.org.uk.com (unknown [10.42.28.30]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id A9E6F1955F54; Tue, 20 Aug 2024 23:21:32 +0000 (UTC) From: David Howells To: Christian Brauner Cc: David Howells , Pankaj Raghav , Jeff Layton , Matthew Wilcox , netfs@lists.linux.dev, linux-afs@lists.infradead.org, linux-cifs@vger.kernel.org, linux-nfs@vger.kernel.org, ceph-devel@vger.kernel.org, v9fs@lists.linux.dev, linux-erofs@lists.ozlabs.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Marc Dionne Subject: [PATCH 4/4] netfs: Fix trimming of streaming-write folios in netfs_inval_folio() Date: Wed, 21 Aug 2024 00:20:58 +0100 Message-ID: <20240820232105.3792638-5-dhowells@redhat.com> In-Reply-To: <20240820232105.3792638-1-dhowells@redhat.com> References: <20240820232105.3792638-1-dhowells@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Stat-Signature: dm9e167g4icga3tc19a8q7szenjfm84j X-Rspam-User: X-Rspamd-Queue-Id: C855E140014 X-Rspamd-Server: rspam02 X-HE-Tag: 1724196101-127598 X-HE-Meta: U2FsdGVkX180ImLGacD5QmAz/czfrAhOEAKkZbHoxoGzLcewKRn1HggkQwCH67y0X1vVSRxbLtfS+G8Nw4UC7/s5lweeoaDBuJbYTmaCQgzg9iTbzPzBzTEpsfpK1jlYVvsl+RYm+MnwLFD0FTVOFs7uWAy4FL6YwhPxqGDYZQBcsIY4aweM/Ahy0hin+5XsjYxvy7523SwlVOxpR1Fc4z4gppabHcAi1XmFNPHREL1jo3HsGVF5WPxB8IUmtVnGsyWBkJ31vmUNCzR9S1/LyRq2wRQBgz83G37gp9/A32nHDYy7eSkcG08ztE8KhVTbY8bOgq5+IKPvwU8prmz7ljHsYumgZhXQuwOCZw6bmPQbrvtrulhx4StNuYqWZCo9OTtA8PC5hd9nHmloQGFMowyJKncpFVn2AY3XulKyhYHtBpFVw+f3n1n4rd3cqhewkaMzg2GiDQV0OGAhTXMMsRGP58ylu1ElosW7YqjBorx2/PF/1EI6asN5Zd/XguOdK9DAKfclXdCDlRlnPasD4OQ3g9cIg6G9tJCpXTyaoaL3w+VPUEP7uEVZHAivY5jZghbBT6828U9EODUvQ44aZafw0/WVwXIV4Uyg0DV5EbBp0n5yn6zsbfJ7LwVcuzmvoCKGPyLGmUiJ9aqOnIQPfQA3eHv424JlrPexUsk+2yyuQuZOCMIxmaFLFOKqnXktnpMkfD7gg3UzDwSRiqARZmV0Janpo0rUyRiaJ7r8PSm4jBqfn48nddGl6at2LxmrlxZfF5sFHJ29rHMKMXLKwKKLIuxFZcU9eqirMZaaioFI8vFZ2udu3mI2bsOWwb1C015n4Bp4ZTAz81CvzaUFCiDirSlWGYwsxC1WXsv6ObwfyH+CpEWTO4gTftUbq0ScR005bWWDO3cVFhsi+4N/xHXd72cEElOz4rZaFbNgqy/PY5JRqHM4S5XMTuxdLtaHUtdb3VF0jVre4SWJ2mE 4XU2ye4A Q2+0Jc/BPpVqdMEf2wkzzUzFhUHDV0tGfe0+nxKK9otN6G0XgTCIHHz1heOLPhFFzzzP84nAewgMHSLuY0/0w2kae81s3lPVQXbug8vq5h69HJzIKen/Fyt9OCu/w8eIcR+w1QKCDnNpbWIDpk6QLUzIkQuXbiPqHzuAIEb7F2ONuAJoRUMr8vxKmtumXPc91Hgscu0g0Ao3ENWdynvJNA+nhhYK/q2vcfLKfVknoNX6jKqLZTC/z3/bIxRF8iUTM3jRzbnQQGjEriv92jy/SnW9f7F6raSWZgDDw5O4iM7oAQz6pWliIvCXpul0kWtX+TRyasyMWvkilxot1dLIIoexNFQG2vh+SjUbv2eJMVC/bf0hduU5NQVugG8cMv3yfhY34Cmhp+FQTxPxn1me1MY6adL+ZkhSgOa9MRwNNGYUCvzTTuoewxh0CW9qxcm1bQoiED94VxhPG3XTuiOoteLa9J4eVLsLcCx9KgJylCksebERIDw5L54FNOD/4ndZmmyAqLCU8CQkfmE15aJ2MnuFk21yjqzlgXtNO6aiNLHM1Eu/wJRShJuCqZ3fmJp7YhZXfJJYd1Q3MN1mMb6dQAHXslXxcaX+pkIi9 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: When netfslib writes to a folio that it doesn't have data for, but that data exists on the server, it will make a 'streaming write' whereby it stores data in a folio that is marked dirty, but not uptodate. When it does this, it attaches a record to folio->private to track the dirty region. When truncate() or fallocate() wants to invalidate part of such a folio, it will call into ->invalidate_folio(), specifying the part of the folio that is to be invalidated. netfs_invalidate_folio(), on behalf of the filesystem, must then determine how to trim the streaming write record. In a couple of cases, however, it does this incorrectly (the reduce-length and move-start cases are switched over and don't, in any case, calculate the value correctly). Fix this by making the logic tree more obvious and fixing the cases. Fixes: 9ebff83e6481 ("netfs: Prep to use folio->private for write grouping and streaming write") Signed-off-by: David Howells cc: Matthew Wilcox (Oracle) cc: Pankaj Raghav cc: Jeff Layton cc: Marc Dionne cc: linux-afs@lists.infradead.org cc: netfs@lists.linux.dev cc: linux-mm@kvack.org cc: linux-fsdevel@vger.kernel.org --- fs/netfs/misc.c | 50 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/fs/netfs/misc.c b/fs/netfs/misc.c index 69324761fcf7..c1f321cf5999 100644 --- a/fs/netfs/misc.c +++ b/fs/netfs/misc.c @@ -97,10 +97,20 @@ EXPORT_SYMBOL(netfs_clear_inode_writeback); void netfs_invalidate_folio(struct folio *folio, size_t offset, size_t length) { struct netfs_folio *finfo; + struct netfs_inode *ctx = netfs_inode(folio_inode(folio)); size_t flen = folio_size(folio); _enter("{%lx},%zx,%zx", folio->index, offset, length); + if (offset == 0 && length == flen) { + unsigned long long i_size = i_size_read(&ctx->inode); + unsigned long long fpos = folio_pos(folio), end; + + end = umin(fpos + flen, i_size); + if (fpos < i_size && end > ctx->zero_point) + ctx->zero_point = end; + } + folio_wait_private_2(folio); /* [DEPRECATED] */ if (!folio_test_private(folio)) @@ -115,18 +125,34 @@ void netfs_invalidate_folio(struct folio *folio, size_t offset, size_t length) /* We have a partially uptodate page from a streaming write. */ unsigned int fstart = finfo->dirty_offset; unsigned int fend = fstart + finfo->dirty_len; - unsigned int end = offset + length; + unsigned int iend = offset + length; if (offset >= fend) return; - if (end <= fstart) + if (iend <= fstart) + return; + + /* The invalidation region overlaps the data. If the region + * covers the start of the data, we either move along the start + * or just erase the data entirely. + */ + if (offset <= fstart) { + if (iend >= fend) + goto erase_completely; + /* Move the start of the data. */ + finfo->dirty_len = fend - iend; + finfo->dirty_offset = offset; + return; + } + + /* Reduce the length of the data if the invalidation region + * covers the tail part. + */ + if (iend >= fend) { + finfo->dirty_len = offset - fstart; return; - if (offset <= fstart && end >= fend) - goto erase_completely; - if (offset <= fstart && end > fstart) - goto reduce_len; - if (offset > fstart && end >= fend) - goto move_start; + } + /* A partial write was split. The caller has already zeroed * it, so just absorb the hole. */ @@ -139,12 +165,6 @@ void netfs_invalidate_folio(struct folio *folio, size_t offset, size_t length) folio_clear_uptodate(folio); kfree(finfo); return; -reduce_len: - finfo->dirty_len = offset + length - finfo->dirty_offset; - return; -move_start: - finfo->dirty_len -= offset - finfo->dirty_offset; - finfo->dirty_offset = offset; } EXPORT_SYMBOL(netfs_invalidate_folio); @@ -164,7 +184,7 @@ bool netfs_release_folio(struct folio *folio, gfp_t gfp) if (folio_test_dirty(folio)) return false; - end = folio_pos(folio) + folio_size(folio); + end = umin(folio_pos(folio) + folio_size(folio), i_size_read(&ctx->inode)); if (end > ctx->zero_point) ctx->zero_point = end;