Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp3086812pxb; Mon, 18 Oct 2021 08:00:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzaYDx4JZ51iuKL9X+co+AvHqLOWeuPDLvJLtoEa0HCBuG0xma/VJafb9vAXvJNWocpoSTD X-Received: by 2002:a17:907:7752:: with SMTP id kx18mr30530077ejc.276.1634569244795; Mon, 18 Oct 2021 08:00:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634569244; cv=none; d=google.com; s=arc-20160816; b=Z21cVHD60OuSkb9eQx+GuZuH8ArD6tkzhjw0k+F/TeIgUbGC561xlsi7kX4ZGNWcmO em4iCL0pE7+qG0osCgP0gmL870yhbI+LUgDKdYkhp3Ih1pfiraYWNG8Or7Gx/WmEN7UL V1NfoCJ1Vq0RWtOR0DkwDd3qdUGfLbyl5sZsY1nHnFOWAQNt62J5tralDIPe4xrOBf+r oRfMIxnl78vAk+oTmSl2apsOlZwhyr+LUmXBtcSrNDyirCbz4mS8mos6CWAFMr+7M+Xo MiW0+r6E5rx0ueTNiD8YLshAjA0l9XREJ5+xF0EQys8WyOOIyssl5HNLI/Sb4Ggbjzt+ ziZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:cc:to:from :subject:organization:dkim-signature; bh=jdB/32s/uc8yVIqnMp8H9Wpa3fu6IqGejTkXRVt6ecY=; b=DOGqGaEb2tbyFD0/U4feiIrql7IzKtsn+agX4dsPSt/1pvkC/RvZVKMG3od1/lyaBZ MEHzO/tLTc/Gn3YZ3hL2pDAKr+YODIsMLpaOTz87ruGtWCwnLROn/15wnVw354lXCrpW 8zRcYx6Y7Mgd0kyDjXCV+lXGX23lbNHrUNBBRDaCUkb49cgOqUDkzQEWiGLfkj4l/XTf tvzZa5gUMao73abSwc1V3WKDMe+u6mczWbVhpxcOVFUtyLaS84Ye24gf0IXJiWEUlxl8 NXI4Unny/D0RzLUPjkv8E4eHWOVvhYc/VlLX6gAXT0WjAb8uLim5AytGWXVevZG96xMQ UmOQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Y8Y7Vxz8; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c40si19297917edf.527.2021.10.18.08.00.18; Mon, 18 Oct 2021 08:00:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Y8Y7Vxz8; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232766AbhJRPCU (ORCPT + 99 others); Mon, 18 Oct 2021 11:02:20 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:45066 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232873AbhJRPCJ (ORCPT ); Mon, 18 Oct 2021 11:02:09 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1634569198; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jdB/32s/uc8yVIqnMp8H9Wpa3fu6IqGejTkXRVt6ecY=; b=Y8Y7Vxz8ercwX7XdgfP3wgQObjtRR15jv9EoQakCTnAsPucxChVdULL/DfyLE8outl2U6m MNQA+yxPVQwN6Q5Zgf+6kBO6pyhRApXsaCgXV0QAy9/ijBHziRpseB7S4wcY2fUvevXePU 6rsJCB4zEw8QO8q6EkEQa4zmSW84QLY= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-360-NIMBeaJPN3WF7tGHAL-ytg-1; Mon, 18 Oct 2021 10:59:54 -0400 X-MC-Unique: NIMBeaJPN3WF7tGHAL-ytg-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9D33B80710A; Mon, 18 Oct 2021 14:59:52 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.19]) by smtp.corp.redhat.com (Postfix) with ESMTP id 728301037F36; Mon, 18 Oct 2021 14:59:49 +0000 (UTC) Organization: Red Hat UK Ltd. Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SI4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 3798903 Subject: [PATCH 34/67] cachefiles: Make cachefiles_write_prepare() check for space From: David Howells To: linux-cachefs@redhat.com Cc: dhowells@redhat.com, Trond Myklebust , Anna Schumaker , Steve French , Dominique Martinet , Jeff Layton , Matthew Wilcox , Alexander Viro , Omar Sandoval , Linus Torvalds , linux-afs@lists.infradead.org, linux-nfs@vger.kernel.org, linux-cifs@vger.kernel.org, ceph-devel@vger.kernel.org, v9fs-developer@lists.sourceforge.net, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Date: Mon, 18 Oct 2021 15:59:48 +0100 Message-ID: <163456918862.2614702.15568944072409008939.stgit@warthog.procyon.org.uk> In-Reply-To: <163456861570.2614702.14754548462706508617.stgit@warthog.procyon.org.uk> References: <163456861570.2614702.14754548462706508617.stgit@warthog.procyon.org.uk> User-Agent: StGit/0.23 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org Make the cachefiles_write_prepare() function check that there's sufficient space to fully satisfy a proposed write. If we already checked for allocated data during read preparation, then this fact can be used to skip the more thorough checks here. If there's enough space in the cache, we just allow the write. If we're uncertain, then we use SEEK_DATA/SEEK_HOLE to check if the block is already fully allocated - and if it is, we just allow the write. However, if there's insufficient space for the whole write and there's partially allocated data in the file, we punch out that data and disallow the write. This frees up some space and removes old data from the cache. Signed-off-by: David Howells --- fs/cachefiles/io.c | 82 ++++++++++++++++++++++++++++++++++++++++++++---- fs/netfs/read_helper.c | 2 + include/linux/netfs.h | 3 +- 3 files changed, 79 insertions(+), 8 deletions(-) diff --git a/fs/cachefiles/io.c b/fs/cachefiles/io.c index c05f64cdfd0e..350243b45dd5 100644 --- a/fs/cachefiles/io.c +++ b/fs/cachefiles/io.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include "internal.h" @@ -385,8 +386,7 @@ static enum netfs_read_source cachefiles_prepare_read(struct netfs_read_subreque goto out; download_and_store: - if (cachefiles_has_space(cache, 0, (subreq->len + PAGE_SIZE - 1) / PAGE_SIZE) == 0) - __set_bit(NETFS_SREQ_WRITE_TO_CACHE, &subreq->flags); + __set_bit(NETFS_SREQ_WRITE_TO_CACHE, &subreq->flags); out: cachefiles_end_secure(cache, saved_cred); out_no_object: @@ -397,17 +397,87 @@ static enum netfs_read_source cachefiles_prepare_read(struct netfs_read_subreque /* * Prepare for a write to occur. */ -static int cachefiles_prepare_write(struct netfs_cache_resources *cres, - loff_t *_start, size_t *_len, loff_t i_size) +static int __cachefiles_prepare_write(struct netfs_cache_resources *cres, + loff_t *_start, size_t *_len, loff_t i_size, + bool no_space_allocated_yet) { - loff_t start = *_start; + struct cachefiles_object *object = cachefiles_cres_object(cres); + struct cachefiles_cache *cache = object->volume->cache; + struct file *file = cachefiles_cres_file(cres); + loff_t start = *_start, pos; size_t len = *_len, down; + int ret; /* Round to DIO size */ down = start - round_down(start, PAGE_SIZE); *_start = start - down; *_len = round_up(down + len, PAGE_SIZE); - return 0; + + /* We need to work out whether there's sufficient disk space to perform + * the write - but we can skip that check if we have space already + * allocated. + */ + if (no_space_allocated_yet) + goto check_space; + + pos = vfs_llseek(file, *_start, SEEK_DATA); + if (pos < 0 && pos >= (loff_t)-MAX_ERRNO) { + if (pos == -ENXIO) + goto check_space; /* Unallocated tail */ + return pos; + } + if ((u64)pos >= (u64)*_start + *_len) + goto check_space; /* Unallocated region */ + + /* We have a block that's at least partially filled - if we're low on + * space, we need to see if it's fully allocated. If it's not, we may + * want to cull it. + */ + if (cachefiles_has_space(cache, 0, *_len / PAGE_SIZE) == 0) + return 0; /* Enough space to simply overwrite the whole block */ + + pos = vfs_llseek(file, *_start, SEEK_HOLE); + if (pos < 0 && pos >= (loff_t)-MAX_ERRNO) + return pos; + if ((u64)pos >= (u64)*_start + *_len) + return 0; /* Fully allocated */ + + /* Partially allocated, but insufficient space: cull. */ + ret = vfs_fallocate(file, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, + *_start, *_len); + if (ret < 0) { + cachefiles_io_error_obj(object, + "CacheFiles: fallocate failed (%d)\n", ret); + ret = -EIO; + } + + return ret; + +check_space: + return cachefiles_has_space(cache, 0, *_len / PAGE_SIZE); +} + +static int cachefiles_prepare_write(struct netfs_cache_resources *cres, + loff_t *_start, size_t *_len, loff_t i_size, + bool no_space_allocated_yet) +{ + struct cachefiles_object *object = cachefiles_cres_object(cres); + struct cachefiles_cache *cache = object->volume->cache; + const struct cred *saved_cred; + int ret; + + if (!cachefiles_cres_file(cres)) { + if (!fscache_wait_for_operation(cres, FSCACHE_WANT_WRITE)) + return -ENOBUFS; + if (!cachefiles_cres_file(cres)) + return -ENOBUFS; + } + + cachefiles_begin_secure(cache, &saved_cred); + ret = __cachefiles_prepare_write(cres, _start, _len, i_size, + no_space_allocated_yet); + cachefiles_end_secure(cache, saved_cred); + return ret; } /* diff --git a/fs/netfs/read_helper.c b/fs/netfs/read_helper.c index dfc60c79a9f3..80f8e334371d 100644 --- a/fs/netfs/read_helper.c +++ b/fs/netfs/read_helper.c @@ -323,7 +323,7 @@ static void netfs_rreq_do_write_to_cache(struct netfs_read_request *rreq) } ret = cres->ops->prepare_write(cres, &subreq->start, &subreq->len, - rreq->i_size); + rreq->i_size, true); if (ret < 0) { trace_netfs_failure(rreq, subreq, ret, netfs_fail_prepare_write); trace_netfs_sreq(subreq, netfs_sreq_trace_write_skip); diff --git a/include/linux/netfs.h b/include/linux/netfs.h index 014fb502fd91..99137486d351 100644 --- a/include/linux/netfs.h +++ b/include/linux/netfs.h @@ -220,7 +220,8 @@ struct netfs_cache_ops { * actually do. */ int (*prepare_write)(struct netfs_cache_resources *cres, - loff_t *_start, size_t *_len, loff_t i_size); + loff_t *_start, size_t *_len, loff_t i_size, + bool no_space_allocated_yet); /* Prepare a write operation for the fallback fscache API, working out * whether we can cache a page or not.