Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp1581999pxf; Fri, 9 Apr 2021 12:01:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxJa1q70iPCRrKmp+/xgOclfq4Bkdspom6V651IkfB5bn20I+wYS/AQveILUObaDmNsWY25 X-Received: by 2002:a63:6d83:: with SMTP id i125mr14356874pgc.12.1617994896565; Fri, 09 Apr 2021 12:01:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617994896; cv=none; d=google.com; s=arc-20160816; b=tdoABsY9Kn2zYCdpGHicKt7y0BRpc+8AnLfiOOL3PnKAin+/+JKFJiR4GSMvG6oEay 5M5TLdc9MuBWvv0Rk5fmcpIOEtWRZZ3KGlL92F9ARRAQU3GYqWMcAsZ/4WCZ15ERnFRr /23/j9UXk6FNpwa6aCLaAO4eU8PcNYwi+hKvodkMVQfhhBv2TeDt4a8G0NPUzTtb5Vmo e7rzF3P4/lgcXvdQ8kkjBd9AbjJP44kUNLudcfgslVH4KXzxftUmioy5a71bQJxrBJHZ 1jHGA8hv2t4HuukjmPZviejrXKhLxXXG+bUNgXJ4invP5sncT8b9UZr3K4ruthsTT+6F yRxQ== 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=bXioRnHFjaOls7rNu6Os2u7zEhcPe7aVviL+hJtS1ts=; b=CpT+XTSv5yL2DKgnR+x/4rsf7VMngChM1DQ5EfDLogA+QutUojAMt/LIVzwIzBFiDX iOmXUlvrlsxlmU8C3qlzFEYfSNcKZWtbqpeuZ1ErjgUqRTUQYSPhHD32FQjuwiUdgrn+ Ma7ykBX0PI6q8nY730t9GZavmjrzE+rgcGDLKCJ1RnCrj5xi7f8vWk2HLU4xZxmyIhch fHN6TQKHR5Lp6Gi3ADQg3ieGjVNDFGY3EZgzwEBYPySWbD/CXt8goqwKlW6ThkPsKipW XjtmtgNevbOBDp7odtlbnxXgVsYv4oPI0FWTzbnHXysNqKGs1oRxDPl/2vshiy8002/U ps/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=Xv73+Xnm; 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 c16si3444123pgh.384.2021.04.09.12.01.23; Fri, 09 Apr 2021 12:01:36 -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=Xv73+Xnm; 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 S234826AbhDITAu (ORCPT + 99 others); Fri, 9 Apr 2021 15:00:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60240 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234692AbhDITAu (ORCPT ); Fri, 9 Apr 2021 15:00:50 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0E9F7C061762; Fri, 9 Apr 2021 12:00:37 -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=bXioRnHFjaOls7rNu6Os2u7zEhcPe7aVviL+hJtS1ts=; b=Xv73+XnmqZyjls63Tkh7gDKL3I JJUqizitHPWifnJJbqMewwF3WW5mtw9wR4petzC9B8LhfRj/k1Ggo1cwxq8nC7G1IwE0Rha880hKG 2FSR06wXdf2tuNn5hld/JBrTiqdTuQT98BrgAfBQ8zUs8QlxfRAMGz6+p3bOa/yG77YTnEL2eSfjP 3VzURVZtuT1D381PM5gt0J3xXQBU8WUVyc27axbhnRRfhV+D0cVrSv2MwDQMuskZLZmsyQJjtv4VQ KSvPvjv4o6WdJQyvsW1itmLf18JwTty5SYEwc8JMpSe2cln6EHFi6d73EuRcppvuidhQO5s5DZJaS nHCDQjPg==; Received: from willy by casper.infradead.org with local (Exim 4.94 #2 (Red Hat Linux)) id 1lUwLD-000niv-EU; Fri, 09 Apr 2021 18:59:04 +0000 From: "Matthew Wilcox (Oracle)" To: linux-mm@kvack.org Cc: "Matthew Wilcox (Oracle)" , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-cachefs@redhat.com, linux-afs@lists.infradead.org, Christoph Hellwig , Jeff Layton Subject: [PATCH v7 11/28] mm/filemap: Add folio_index, folio_file_page and folio_contains Date: Fri, 9 Apr 2021 19:50:48 +0100 Message-Id: <20210409185105.188284-12-willy@infradead.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210409185105.188284-1-willy@infradead.org> References: <20210409185105.188284-1-willy@infradead.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org folio_index() is the equivalent of page_index() for folios. folio_file_page() is the equivalent of find_subpage(). folio_contains() is the equivalent of thp_contains(). Signed-off-by: Matthew Wilcox (Oracle) Reviewed-by: Christoph Hellwig Acked-by: Jeff Layton --- include/linux/pagemap.h | 53 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index f2fd0b811c1b..a8b108a9ac6e 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h @@ -462,6 +462,59 @@ static inline bool thp_contains(struct page *head, pgoff_t index) return page_index(head) == (index & ~(thp_nr_pages(head) - 1UL)); } +#define swapcache_index(folio) __page_file_index(&(folio)->page) + +/** + * folio_index - File index of a folio. + * @folio: The folio. + * + * For a folio which is either in the page cache or the swap cache, + * return its index within the address_space it belongs to. If you know + * the page is definitely in the page cache, you can look at the folio's + * index directly. + * + * Return: The index (offset in units of pages) of a folio in its file. + */ +static inline pgoff_t folio_index(struct folio *folio) +{ + if (unlikely(FolioSwapCache(folio))) + return swapcache_index(folio); + return folio->index; +} + +/** + * folio_file_page - The page for a particular index. + * @folio: The folio which contains this index. + * @index: The index we want to look up. + * + * Sometimes after looking up a folio in the page cache, we need to + * obtain the specific page for an index (eg a page fault). + * + * Return: The page containing the file data for this index. + */ +static inline struct page *folio_file_page(struct folio *folio, pgoff_t index) +{ + return nth_page(&folio->page, index & (folio_nr_pages(folio) - 1)); +} + +/** + * folio_contains - Does this folio contain this index? + * @folio: The folio. + * @index: The page index within the file. + * + * Context: The caller should have the page locked in order to prevent + * (eg) shmem from moving the page between the page cache and swap cache + * and changing its index in the middle of the operation. + * Return: true or false. + */ +static inline bool folio_contains(struct folio *folio, pgoff_t index) +{ + /* HugeTLBfs indexes the page cache in units of hpage_size */ + if (PageHuge(&folio->page)) + return folio->index == index; + return index - folio_index(folio) < folio_nr_pages(folio); +} + /* * Given the page we found in the page cache, return the page corresponding * to this index in the file -- 2.30.2