From patchwork Fri Jan 26 13:28:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13532580 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 47A8AC47422 for ; Fri, 26 Jan 2024 13:30:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C19F86B0093; Fri, 26 Jan 2024 08:30:22 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BC78A6B0095; Fri, 26 Jan 2024 08:30:22 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A8F606B0096; Fri, 26 Jan 2024 08:30:22 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 99BE36B0093 for ; Fri, 26 Jan 2024 08:30:22 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 700A8120F6E for ; Fri, 26 Jan 2024 13:30:22 +0000 (UTC) X-FDA: 81721546284.02.1510414 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) by imf15.hostedemail.com (Postfix) with ESMTP id BF10BA000F for ; Fri, 26 Jan 2024 13:30:20 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=qIB7chEq; dmarc=none; spf=none (imf15.hostedemail.com: domain of BATV+8230b42af99c397292d7+7460+infradead.org+hch@bombadil.srs.infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=BATV+8230b42af99c397292d7+7460+infradead.org+hch@bombadil.srs.infradead.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706275820; 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=X21Ew2yPXjed2nTt3K1ULzd8XdBx85Zv7/geftx1aII=; b=6dTgRbIu03X9UV9j4P+SUUXPL2A3P0O/VQv9JrWi8YxOmF4RnFX46M8QsUEiZqfUZesEyH PrTm2BQvR8AO1Z9gLTCiHr2px3plTm675qQCWoOWc7I0ECnfbSulLx/V6n5qg0kecTRFFV ccb5kNqru/crxfJaWn/ILAEiidNt+Tw= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=qIB7chEq; dmarc=none; spf=none (imf15.hostedemail.com: domain of BATV+8230b42af99c397292d7+7460+infradead.org+hch@bombadil.srs.infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=BATV+8230b42af99c397292d7+7460+infradead.org+hch@bombadil.srs.infradead.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706275820; a=rsa-sha256; cv=none; b=zuwzdQBvULV43e2Aq6WbwoiVtHZ+euaMYobgejSKAfwX7yyLbHvOV6sAvhYjMkQk0XPMn4 jooK925ANPPhb+SbtdpPs8CLGovqx1hArI7JWRRAWpReQVkSYLMbzbKLtRq8JmkNx8AhVJ YdZlEwdoNPSapSvwBS37LUpWlLtMI20= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; 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=X21Ew2yPXjed2nTt3K1ULzd8XdBx85Zv7/geftx1aII=; b=qIB7chEqXgv/83F5fXU4aGvZaF cd1gr+xzZw1byceF6Z11y2RibY9b3sEMHeiM85ZZzIL5byv2cYYlZdM05BTpxcd61Rcu8tqNYNs/i ojQ0/qtmksiPtgScHRplfmQi7MD/YEpH5s7CiBjxVuhxk3Wbgw93TfJq6FDsFZ3uJI3IJHD7EvKzC cDbJA8pcexmkTiJBDFHKdiCXdiG/X9HRYys7MiXf0ydJ5RIsL5QhMTac1wg71uQ50ggIgC0LfBqn/ JELRJFtYSrnVxj22jJsPkF+yhbtPR2G5f7TJ/kJd/GUsZBwSLe0wsjDhiFUNJTmhLu1OaHRE7VfXO 90Sx2HDw==; Received: from 2a02-8389-2341-5b80-39d3-4735-9a3c-88d8.cable.dynamic.v6.surfer.at ([2a02:8389:2341:5b80:39d3:4735:9a3c:88d8] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTMHi-00000004Cw5-0efr; Fri, 26 Jan 2024 13:30:18 +0000 From: Christoph Hellwig To: Chandan Babu R , "Darrick J. Wong" , Hugh Dickins , Andrew Morton Cc: linux-xfs@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 17/21] xfs: add file_{get,put}_folio Date: Fri, 26 Jan 2024 14:28:59 +0100 Message-Id: <20240126132903.2700077-18-hch@lst.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240126132903.2700077-1-hch@lst.de> References: <20240126132903.2700077-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html X-Rspam-User: X-Stat-Signature: 8pd485d6h8s61nt1w36bajh3e1xrxwcw X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: BF10BA000F X-HE-Tag: 1706275820-286744 X-HE-Meta: U2FsdGVkX19VwSFsWGsYY80T/OwLsh+Rcjg8afTUm71tofbzNSOU/lJ/Ftcjbe4wQ+rfhwJNoh3lTfTM47BwxPBANecuBaQXswoS7Wf3KvSsxEkW90eJQVg5jmMqH0YEQ4tiQNZQxWqpD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tqRBs7LOPR4qzujemjVtKPB/zFmWwQGP3tC2G3w0wAL9A1+5XE80XB3RO+JngI/9OLeGMLlP41bw6A9ug7A6i15g/Ff6Xh6YyajArD5xh+q5JNTv78Hx+P2kv8VVKw7p0A9X9PTzRInewQb/pA54mn9c7/NN6ciSPADyeDcuayNK08Jcn6vUEGE2VuMoFZMpOTQygFh4AqZ1lvtNiFwSO0f8bSRgWJTsrMtSYkt+BCbqc4YUhtg501i2BOyggjBKCIEswds1Y2/QuNnSIs+znz7s= 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: From: "Darrick J. Wong" Add helper similar to file_{get,set}_page, but which deal with folios and don't allocate new folio unless explicitly asked to, which map to shmem_get_folio instead of calling into the aops. Signed-off-by: Darrick J. Wong Signed-off-by: Christoph Hellwig Reviewed-by: Kent Overstreet --- fs/xfs/scrub/trace.h | 2 ++ fs/xfs/scrub/xfile.c | 74 ++++++++++++++++++++++++++++++++++++++++++++ fs/xfs/scrub/xfile.h | 7 +++++ 3 files changed, 83 insertions(+) diff --git a/fs/xfs/scrub/trace.h b/fs/xfs/scrub/trace.h index 0327cab606b070..c61fa7a95ef522 100644 --- a/fs/xfs/scrub/trace.h +++ b/fs/xfs/scrub/trace.h @@ -908,6 +908,8 @@ DEFINE_XFILE_EVENT(xfile_store); DEFINE_XFILE_EVENT(xfile_seek_data); DEFINE_XFILE_EVENT(xfile_get_page); DEFINE_XFILE_EVENT(xfile_put_page); +DEFINE_XFILE_EVENT(xfile_get_folio); +DEFINE_XFILE_EVENT(xfile_put_folio); TRACE_EVENT(xfarray_create, TP_PROTO(struct xfarray *xfa, unsigned long long required_capacity), diff --git a/fs/xfs/scrub/xfile.c b/fs/xfs/scrub/xfile.c index 2d802c20a8ddfe..1c1db4ae1ba6ee 100644 --- a/fs/xfs/scrub/xfile.c +++ b/fs/xfs/scrub/xfile.c @@ -365,3 +365,77 @@ xfile_put_page( return -EIO; return 0; } + +/* + * Grab the (locked) folio for a memory object. The object cannot span a folio + * boundary. Returns the locked folio if successful, NULL if there was no + * folio or it didn't cover the range requested, or an ERR_PTR on failure. + */ +struct folio * +xfile_get_folio( + struct xfile *xf, + loff_t pos, + size_t len, + unsigned int flags) +{ + struct inode *inode = file_inode(xf->file); + struct folio *folio = NULL; + unsigned int pflags; + int error; + + if (inode->i_sb->s_maxbytes - pos < len) + return ERR_PTR(-ENOMEM); + + trace_xfile_get_folio(xf, pos, len); + + /* + * Increase the file size first so that shmem_get_folio(..., SGP_CACHE), + * actually allocates a folio instead of erroring out. + */ + if ((flags & XFILE_ALLOC) && pos + len > i_size_read(inode)) + i_size_write(inode, pos + len); + + pflags = memalloc_nofs_save(); + error = shmem_get_folio(inode, pos >> PAGE_SHIFT, &folio, + (flags & XFILE_ALLOC) ? SGP_CACHE : SGP_READ); + memalloc_nofs_restore(pflags); + if (error) + return ERR_PTR(error); + + if (!folio) + return NULL; + + if (len > folio_size(folio) - offset_in_folio(folio, pos)) { + folio_unlock(folio); + folio_put(folio); + return NULL; + } + + if (xfile_has_lost_data(inode, folio)) { + folio_unlock(folio); + folio_put(folio); + return ERR_PTR(-EIO); + } + + /* + * Mark the folio dirty so that it won't be reclaimed once we drop the + * (potentially last) reference in xfile_put_folio. + */ + if (flags & XFILE_ALLOC) + folio_set_dirty(folio); + return folio; +} + +/* + * Release the (locked) folio for a memory object. + */ +void +xfile_put_folio( + struct xfile *xf, + struct folio *folio) +{ + trace_xfile_put_folio(xf, folio_pos(folio), folio_size(folio)); + + folio_unlock(folio); + folio_put(folio); +} diff --git a/fs/xfs/scrub/xfile.h b/fs/xfs/scrub/xfile.h index 465b10f492b66d..afb75e9fbaf265 100644 --- a/fs/xfs/scrub/xfile.h +++ b/fs/xfs/scrub/xfile.h @@ -39,4 +39,11 @@ int xfile_get_page(struct xfile *xf, loff_t offset, unsigned int len, struct xfile_page *xbuf); int xfile_put_page(struct xfile *xf, struct xfile_page *xbuf); +#define XFILE_MAX_FOLIO_SIZE (PAGE_SIZE << MAX_PAGECACHE_ORDER) + +#define XFILE_ALLOC (1 << 0) /* allocate folio if not present */ +struct folio *xfile_get_folio(struct xfile *xf, loff_t offset, size_t len, + unsigned int flags); +void xfile_put_folio(struct xfile *xf, struct folio *folio); + #endif /* __XFS_SCRUB_XFILE_H__ */