Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp2633788pxv; Sun, 11 Jul 2021 20:53:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz7vvIptZW1yZgrCROF+8CebKPkVwPQb7Fx7YlWtCH908ZJNmWxb5zsAW+9Ep2SDQDs5QKM X-Received: by 2002:a17:906:a38c:: with SMTP id k12mr1219032ejz.474.1626062025965; Sun, 11 Jul 2021 20:53:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626062025; cv=none; d=google.com; s=arc-20160816; b=Ytv/kJEJAAqaRTqlq0y8t3UZvjfF9waddxW0L5fYqMr8tXcJHtB7PtkMdQr+kixLNw miB0gjHSMip+xpCkrs/ZnRuOSkd6X1Sq6dxwA08N5y07TItakFke6kccR4UAW4yfKH8o t2I1ZMQs6tMRNo/rq21OL7a1yAP91EZWUV9rYS7xuQG0v6/hn6SrET2IlU7bLIDRRcXi 30G38x2h5E06PNbjKZ2jkWzbgAYp1oq5aKqdso8/zl+Bqctr+cS7n9goZ40vW8FjBOWh 32ipD9j6PZ5WC3aVe1Mr1oQdxqdFKThQ+lrz5FABPmurQyCx9VziseK1uH84YDaOTB2H xojQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=rig9TuSYZ8ISZ2eXwc2N6+sqR9xeb6/pxaFhZmO35Ps=; b=x3x/vanPkD5SFSRlmXTbi5tdeBuHq+H9fKsz7Py0ukAwxAwtAsN2n5xSKYArUonpwf dfWHpDGRucVs1h5FmBL1AGRe9ked1kGq/jt2F7+hFomv2v7I4LvX9AXJhyY2g5MhcIgR qydoj5w9U6DOfxNEGRaEG2xVODoWZdJf6cH9qGxvTIkCwctS1B6eriocr/YJdwbEHqf0 O5VxoBBYx5oBQcajR9ocfR/7JJC/J7kBMLLMCYzuTs/rnWSgKccQX3Tn/yTPszG7gij1 r3c+OWhPL9J3cXcgxgT85XG0IeNV4qXqFdzGH1kq2S42GYbQJjoKcSO3E4ejgQKDOQAt tEzQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=ZPmGJmAP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ze15si16106847ejb.253.2021.07.11.20.53.21; Sun, 11 Jul 2021 20:53:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-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=@infradead.org header.s=casper.20170209 header.b=ZPmGJmAP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233399AbhGLDwM (ORCPT + 99 others); Sun, 11 Jul 2021 23:52:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48546 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233330AbhGLDwM (ORCPT ); Sun, 11 Jul 2021 23:52:12 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7F5ECC0613DD; Sun, 11 Jul 2021 20:49:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=rig9TuSYZ8ISZ2eXwc2N6+sqR9xeb6/pxaFhZmO35Ps=; b=ZPmGJmAPlF369tcnWouschWr4i Vi8KJ+LH9ZOZqZEEQRVYGWAPvw+okopJZMnVndVTPz6X+HQhR6iQA2MmYXXKh6AtUghO0k0jI528g lT6i5eJdqAmM26OmpXFb96yltOqRGRTr9Vvq8vkMQ/81sePLvw1cOGLJFqZCxVjoo1eNYfOiQYH/F mEAc67r0ukOWrdoS5rIv09TXhJ8SxQG844TyD3ND4IStVCYQ23ovXjAOKKgOeA1ycDjRlXV9luTVe IkbMuTXthOACs8Xg+AhWsOkxglSTc4v/jpR9EYgJrAggGLYJeTbHCHwf2CoOpw6y61ufW/lXLYJLi s3aHsPZQ==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1m2mvi-00Gpgr-KY; Mon, 12 Jul 2021 03:48:40 +0000 From: "Matthew Wilcox (Oracle)" To: linux-kernel@vger.kernel.org Cc: "Matthew Wilcox (Oracle)" , linux-mm@kvack.org, linux-fsdevel@vger.kernel.org Subject: [PATCH v13 077/137] mm/filemap: Add folio_mkwrite_check_truncate() Date: Mon, 12 Jul 2021 04:06:01 +0100 Message-Id: <20210712030701.4000097-78-willy@infradead.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210712030701.4000097-1-willy@infradead.org> References: <20210712030701.4000097-1-willy@infradead.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This is the folio equivalent of page_mkwrite_check_truncate(). Signed-off-by: Matthew Wilcox (Oracle) --- include/linux/pagemap.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index 319e2b486c0d..1aee9f711de8 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h @@ -1121,6 +1121,34 @@ static inline unsigned long dir_pages(struct inode *inode) PAGE_SHIFT; } +/** + * folio_mkwrite_check_truncate - check if folio was truncated + * @folio: the folio to check + * @inode: the inode to check the folio against + * + * Return: the number of bytes in the folio up to EOF, + * or -EFAULT if the folio was truncated. + */ +static inline ssize_t folio_mkwrite_check_truncate(struct folio *folio, + struct inode *inode) +{ + loff_t size = i_size_read(inode); + pgoff_t index = size >> PAGE_SHIFT; + size_t offset = offset_in_folio(folio, size); + + if (!folio->mapping) + return -EFAULT; + + /* folio is wholly inside EOF */ + if (folio_next_index(folio) - 1 < index) + return folio_size(folio); + /* folio is wholly past EOF */ + if (folio->index > index || !offset) + return -EFAULT; + /* folio is partially inside EOF */ + return offset; +} + /** * page_mkwrite_check_truncate - check if page was truncated * @page: the page to check -- 2.30.2