From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.5 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8214DC2D0A3 for ; Mon, 26 Oct 2020 04:14:34 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 113202073A for ; Mon, 26 Oct 2020 04:14:34 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="SX+zpd7v" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 113202073A Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 79A526B0071; Mon, 26 Oct 2020 00:14:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7217B6B0072; Mon, 26 Oct 2020 00:14:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 525126B0073; Mon, 26 Oct 2020 00:14:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0216.hostedemail.com [216.40.44.216]) by kanga.kvack.org (Postfix) with ESMTP id 1FAE46B0071 for ; Mon, 26 Oct 2020 00:14:22 -0400 (EDT) Received: from smtpin15.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id A952A181AC9CB for ; Mon, 26 Oct 2020 04:14:21 +0000 (UTC) X-FDA: 77412759522.15.alarm49_480b90327270 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin15.hostedemail.com (Postfix) with ESMTP id 8B61D1814B0C1 for ; Mon, 26 Oct 2020 04:14:21 +0000 (UTC) X-HE-Tag: alarm49_480b90327270 X-Filterd-Recvd-Size: 7990 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf18.hostedemail.com (Postfix) with ESMTP for ; Mon, 26 Oct 2020 04:14:21 +0000 (UTC) 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=MWcn/Nhf9sR48o2zboUwMXLYP6nYp5d/8r8AJss6SUs=; b=SX+zpd7vntl7+TRHG3Xx0HdxWy dgdXvI69NjlzgIBoyJYaDRojtTlzb8QpoGhBvhButticsnPIDC6Qj1N+ubjszj8yreojIdux5fPsF 68dZNddppah/zu3uuk5v/X/z+PuA8UO4u4Vxw/2xZNhDdWuTe9DX4SXim55NaSC4aj4BbVDJrskqy C74ZbFSMvMfGZ3qFh4uVk1jthEu5YcMF+hPo4JrCbzaH6hTvH8zutOH2B4AinnN+iHojfpifcJKyC avZQQrk6u9Izv3EYCsyA67OdqUo9iNqX78jLTkSDKSykEeiMnGGCyI83XWlamuVCsd+Ekz43RTr/i ns9vugpg==; Received: from willy by casper.infradead.org with local (Exim 4.92.3 #3 (Red Hat Linux)) id 1kWttb-0006Zw-2E; Mon, 26 Oct 2020 04:14:11 +0000 From: "Matthew Wilcox (Oracle)" To: linux-mm@kvack.org Cc: "Matthew Wilcox (Oracle)" , linux-fsdevel@vger.kernel.org, Andrew Morton , Hugh Dickins , Johannes Weiner , Yang Shi , Dave Chinner , linux-kernel@vger.kernel.org, William Kucharski Subject: [PATCH v3 04/12] mm/filemap: Add mapping_seek_hole_data Date: Mon, 26 Oct 2020 04:14:00 +0000 Message-Id: <20201026041408.25230-5-willy@infradead.org> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20201026041408.25230-1-willy@infradead.org> References: <20201026041408.25230-1-willy@infradead.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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: Rewrite shmem_seek_hole_data() and move it to filemap.c. Signed-off-by: Matthew Wilcox (Oracle) Reviewed-by: William Kucharski --- include/linux/pagemap.h | 2 ++ mm/filemap.c | 76 +++++++++++++++++++++++++++++++++++++++++ mm/shmem.c | 72 +++----------------------------------- 3 files changed, 82 insertions(+), 68 deletions(-) diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index c77b7c31b2e4..5f3e829c91fd 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h @@ -760,6 +760,8 @@ extern void __delete_from_page_cache(struct page *pag= e, void *shadow); int replace_page_cache_page(struct page *old, struct page *new, gfp_t gf= p_mask); void delete_from_page_cache_batch(struct address_space *mapping, struct pagevec *pvec); +loff_t mapping_seek_hole_data(struct address_space *, loff_t start, loff= _t end, + int whence); =20 /* * Like add_to_page_cache_locked, but used to add newly allocated pages: diff --git a/mm/filemap.c b/mm/filemap.c index 00eaed59e797..3a55d258d9f2 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -2526,6 +2526,82 @@ generic_file_read_iter(struct kiocb *iocb, struct = iov_iter *iter) } EXPORT_SYMBOL(generic_file_read_iter); =20 +static inline loff_t page_seek_hole_data(struct page *page, + loff_t start, loff_t end, bool seek_data) +{ + if (xa_is_value(page) || PageUptodate(page)) + return seek_data ? start : end; + return seek_data ? end : start; +} + +static inline +unsigned int seek_page_size(struct xa_state *xas, struct page *page) +{ + if (xa_is_value(page)) + return PAGE_SIZE << xa_get_order(xas->xa, xas->xa_index); + return thp_size(page); +} + +/** + * mapping_seek_hole_data - Seek for SEEK_DATA / SEEK_HOLE in the page c= ache. + * @mapping: Address space to search. + * @start: First byte to consider. + * @end: Limit of search (exclusive). + * @whence: Either SEEK_HOLE or SEEK_DATA. + * + * If the page cache knows which blocks contain holes and which blocks + * contain data, your filesystem can use this function to implement + * SEEK_HOLE and SEEK_DATA. This is useful for filesystems which are + * entirely memory-based such as tmpfs, and filesystems which support + * unwritten extents. + * + * Return: The requested offset on successs, or -ENXIO if @whence specif= ies + * SEEK_DATA and there is no data after @start. There is an implicit ho= le + * after @end - 1, so SEEK_HOLE returns @end if all the bytes between @s= tart + * and @end contain data. + */ +loff_t mapping_seek_hole_data(struct address_space *mapping, loff_t star= t, + loff_t end, int whence) +{ + XA_STATE(xas, &mapping->i_pages, start >> PAGE_SHIFT); + pgoff_t max =3D (end - 1) / PAGE_SIZE; + bool seek_data =3D (whence =3D=3D SEEK_DATA); + struct page *page; + + if (end <=3D start) + return -ENXIO; + + rcu_read_lock(); + while ((page =3D xas_find_get_entry(&xas, max, XA_PRESENT))) { + loff_t pos =3D xas.xa_index * PAGE_SIZE; + + if (start < pos) { + if (!seek_data) + goto unlock; + start =3D pos; + } + + pos +=3D seek_page_size(&xas, page); + start =3D page_seek_hole_data(page, start, pos, seek_data); + if (start < pos) + goto unlock; + } + rcu_read_unlock(); + + if (seek_data) + return -ENXIO; + goto out; + +unlock: + rcu_read_unlock(); + if (!xa_is_value(page)) + put_page(page); +out: + if (start > end) + return end; + return start; +} + #ifdef CONFIG_MMU #define MMAP_LOTSAMISS (100) /* diff --git a/mm/shmem.c b/mm/shmem.c index a33972126b60..726cede653f5 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2670,85 +2670,21 @@ static ssize_t shmem_file_read_iter(struct kiocb = *iocb, struct iov_iter *to) return retval ? retval : error; } =20 -/* - * llseek SEEK_DATA or SEEK_HOLE through the page cache. - */ -static pgoff_t shmem_seek_hole_data(struct address_space *mapping, - pgoff_t index, pgoff_t end, int whence) -{ - struct page *page; - struct pagevec pvec; - pgoff_t indices[PAGEVEC_SIZE]; - bool done =3D false; - int i; - - pagevec_init(&pvec); - pvec.nr =3D 1; /* start small: we may be there already */ - while (!done) { - pvec.nr =3D find_get_entries(mapping, index, - pvec.nr, pvec.pages, indices); - if (!pvec.nr) { - if (whence =3D=3D SEEK_DATA) - index =3D end; - break; - } - for (i =3D 0; i < pvec.nr; i++, index++) { - if (index < indices[i]) { - if (whence =3D=3D SEEK_HOLE) { - done =3D true; - break; - } - index =3D indices[i]; - } - page =3D pvec.pages[i]; - if (page && !xa_is_value(page)) { - if (!PageUptodate(page)) - page =3D NULL; - } - if (index >=3D end || - (page && whence =3D=3D SEEK_DATA) || - (!page && whence =3D=3D SEEK_HOLE)) { - done =3D true; - break; - } - } - pagevec_remove_exceptionals(&pvec); - pagevec_release(&pvec); - pvec.nr =3D PAGEVEC_SIZE; - cond_resched(); - } - return index; -} - static loff_t shmem_file_llseek(struct file *file, loff_t offset, int wh= ence) { struct address_space *mapping =3D file->f_mapping; struct inode *inode =3D mapping->host; - pgoff_t start, end; - loff_t new_offset; =20 if (whence !=3D SEEK_DATA && whence !=3D SEEK_HOLE) return generic_file_llseek_size(file, offset, whence, MAX_LFS_FILESIZE, i_size_read(inode)); + if (offset < 0) + return -ENXIO; + inode_lock(inode); /* We're holding i_mutex so we can access i_size directly */ =20 - if (offset < 0 || offset >=3D inode->i_size) - offset =3D -ENXIO; - else { - start =3D offset >> PAGE_SHIFT; - end =3D (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT; - new_offset =3D shmem_seek_hole_data(mapping, start, end, whence); - new_offset <<=3D PAGE_SHIFT; - if (new_offset > offset) { - if (new_offset < inode->i_size) - offset =3D new_offset; - else if (whence =3D=3D SEEK_DATA) - offset =3D -ENXIO; - else - offset =3D inode->i_size; - } - } + offset =3D mapping_seek_hole_data(mapping, offset, inode->i_size, whenc= e); =20 if (offset >=3D 0) offset =3D vfs_setpos(file, offset, MAX_LFS_FILESIZE); --=20 2.28.0