From patchwork Thu Apr 20 08:03:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos Maiolino X-Patchwork-Id: 13218085 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 99FB3C77B7A for ; Thu, 20 Apr 2023 08:04:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 25105900002; Thu, 20 Apr 2023 04:04:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1D7D5280001; Thu, 20 Apr 2023 04:04:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0515E900004; Thu, 20 Apr 2023 04:04:10 -0400 (EDT) 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 ED16D900002 for ; Thu, 20 Apr 2023 04:04:09 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id C9B041601D1 for ; Thu, 20 Apr 2023 08:04:09 +0000 (UTC) X-FDA: 80701031418.11.FE2A5AF Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf20.hostedemail.com (Postfix) with ESMTP id E8D551C0024 for ; Thu, 20 Apr 2023 08:04:07 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=VTF3rSZg; spf=pass (imf20.hostedemail.com: domain of cem@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cem@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1681977848; 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=12qeoW1RfL5fHYt58V94HmmufB4GThD+B5NpSiw3Wm0=; b=McFFpm5ZWQqvGIzbEZXmmy3BBR39bL3QguuJlnYkpTQAN4KtvF+lhFGWVLnME2/CBuXhai tB/7Fdv+xT0WMo5XCmBU6mASVJ6hVBuSH1Gqofw5+1o5Anf5D7X/IFSk0ML/pnrh5P2T3k DN3XEMfJra7cd/WieJAYihm4ozK8gW4= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=VTF3rSZg; spf=pass (imf20.hostedemail.com: domain of cem@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cem@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681977848; a=rsa-sha256; cv=none; b=8NnEDny9zOaCuoKY7ajB6yWx8X3KZedTFOWUlg7LOI8nOYRRPKJP+JxE1md++ToHD7MRC+ GR3wtUUMn16+CeoLl1n22a2BV+UE3FWlvED8UeZFcDEkTekDboi7evfvfyIoGeNlUCNP05 LRIutBMzx4wDIB7FLW4lJZ8ReFw0Fyc= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 163FF645C6; Thu, 20 Apr 2023 08:04:07 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 19701C4339C; Thu, 20 Apr 2023 08:04:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1681977846; bh=/U6gTaWZnnp3Yqw9JFrBzoFaHYbrueaBpixPomPgEIQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VTF3rSZgGHvHMKA6+CHecyqIN4ZwDN1K30wXjuWhCpUITWCjvr5oaWwMdaddusT5n c2QMzqaRTihAabgUhLAprD9EDTsD7xSG+3x48SXBx7HfsOmU15hzRRbmQOcxbKzxiD Idbh0omcPqbbXeyA+oQ+Wcl3xPYKZwafPxE+Oh4UV6rwr0+KH3JZvy5w0hVvwndsgB NkYpA3mNxjJ54KkA+zWkBBmYvBj6rtE8YJ/QGwa7u1QZXThCFb8FRA+LJqO/veIzXV D2CJC/QSCa5kU/Gr/0Q4T65izfeuN7HQtmxkr9yszrsWxMVW+HjkoZoeTYMH2ufisv QZh/lGcblxGHg== From: cem@kernel.org To: hughd@google.com Cc: jack@suse.cz, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, djwong@kernel.org Subject: [PATCH V2 1/6] shmem: make shmem_inode_acct_block() return error Date: Thu, 20 Apr 2023 10:03:54 +0200 Message-Id: <20230420080359.2551150-2-cem@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230420080359.2551150-1-cem@kernel.org> References: <20230420080359.2551150-1-cem@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: E8D551C0024 X-Stat-Signature: imjs4b81bnxp3efd8fbg8hbs1dg8m7og X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1681977847-811057 X-HE-Meta: U2FsdGVkX1/RxN25q/OCZsB2upkE+5v9vFBPviGUYojkBrNVpOlHcrAfPI+KWEwZvCKmdrmPwc+jGTgM+JFEDHjYJFG5ruOVNSY0eNzVjn6RPJ4iIymILPH5fjt1CL+WnsA6wAq46QRQwdP+HndvumUW0XDMS+/78K9K0V4e2raUxRumgYsFAybkfdNlVg/zfEz3AImCn8gyfaVbAbrgd5uk9wxAxRDXvOwONehzWi0qWUcxrs9X+WKQ2/fDX9oo9F4ZNK5PG+dJHpu3HRy8zWUqmryrvJHV7DU4L6V104dv2LTb6f7vCB9k54lqz5rTqMLO0xj5Kd/Y1z8I59Xi85ecb75El4bxHdupO3VfI3vwXK49499utHfwK0/v4EaJsAM6s8u9VG8HDypoUvqBJvfkxgIk68DQ9+i45H+KiADKepsqmgQY0aFu1687LCbCPY8Pipo/L4Ao69p3jj2b3u/hHx0d7OeX1WwKQGa6tfl6sR6wktJMdWGddS5Lqn32HA5iZ1WZwzRcgNbL58uu1tkefDf0QfVWPpUy6ih6Y1Wl6DWrszGIS7Bcc/7fDOt1bkul0PuG5Jkc4vN+4vbK+DTSY5zBuPP+hFycEV2huu8bfPGbiw2tbaSBbdTv19Q2F2YpcrgcVjQihbXOq/Absht+G+WAoRW/iCpghwXtPMyWS6iUaRm1PZ2KhkDYNl42va0WwwUOZ5xTk6G1wKrFNSpfup0cbSmkJrK7gExNfDybxYdewvVtin/Z39ziUcUbcTd3khgxBjsMrddDOSXfC3fQl3IKuu+nveKHxJDR3vX8s2byMQkUpZONa4D51znxE4P3YcMbHyMS7LS6SBbCkaHpPK/Ud6uMZ37EG0Jw7Y4Xi6riPs8Mnz8HXVLeXzn5EiAVazHs69APl5sDKZKsrbPgFoprSr4kb12nrB/0ZbL9rt0LD5MCnQl1iglh+5q2olTUpxa/PLjyceFe50y G3n+l+zo kmpbbyC+/4aZZNxZJKivluyagiTz9CWnznCcr9onNjF6+BAPhqijtUKi0/07Cw//zYEezXk19o98CaFnnygiIpON3GD9sL4arqiftoFSmRcTQc+g7tqyzi1Lvgbw9UJB6iS9/92ga3faj6cg6m/EUz5ZLgGsXgzhCmnbSxLOj4kZ/NPOORzwvhsQFtCH3bQX24uCDkR/yFG5SVr8Xr7luXzbx8WsJr7IbXETYkbn+N3wz+9RUh/AestqC1Gjgsjo512IUB4LrDWQZ1WzUKOYAet5Ltf8zm42ArQX3uBirTrzc0FwrzUC792FDEchm0hWRHW8V1DA/DnGA7Yubt+jGki0QfMbA4dbPsjj6XbXNWj82Rnk= 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: From: Lukas Czerner Make shmem_inode_acct_block() return proper error code instead of bool. This will be useful later when we introduce quota support. There should be no functional change. Signed-off-by: Lukas Czerner Signed-off-by: Carlos Maiolino Reviewed-by: Jan Kara --- V2: - remove unused variable initialization on shmem_alloc_and_acct_folio mm/shmem.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index 448f393d8ab2b..b1b3b826f6189 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -198,13 +198,14 @@ static inline void shmem_unacct_blocks(unsigned long flags, long pages) vm_unacct_memory(pages * VM_ACCT(PAGE_SIZE)); } -static inline bool shmem_inode_acct_block(struct inode *inode, long pages) +static inline int shmem_inode_acct_block(struct inode *inode, long pages) { struct shmem_inode_info *info = SHMEM_I(inode); struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); + int err = -ENOSPC; if (shmem_acct_block(info->flags, pages)) - return false; + return err; if (sbinfo->max_blocks) { if (percpu_counter_compare(&sbinfo->used_blocks, @@ -213,11 +214,11 @@ static inline bool shmem_inode_acct_block(struct inode *inode, long pages) percpu_counter_add(&sbinfo->used_blocks, pages); } - return true; + return 0; unacct: shmem_unacct_blocks(info->flags, pages); - return false; + return err; } static inline void shmem_inode_unacct_blocks(struct inode *inode, long pages) @@ -369,7 +370,7 @@ bool shmem_charge(struct inode *inode, long pages) struct shmem_inode_info *info = SHMEM_I(inode); unsigned long flags; - if (!shmem_inode_acct_block(inode, pages)) + if (shmem_inode_acct_block(inode, pages)) return false; /* nrpages adjustment first, then shmem_recalc_inode() when balanced */ @@ -1583,13 +1584,14 @@ static struct folio *shmem_alloc_and_acct_folio(gfp_t gfp, struct inode *inode, struct shmem_inode_info *info = SHMEM_I(inode); struct folio *folio; int nr; - int err = -ENOSPC; + int err; if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) huge = false; nr = huge ? HPAGE_PMD_NR : 1; - if (!shmem_inode_acct_block(inode, nr)) + err = shmem_inode_acct_block(inode, nr); + if (err) goto failed; if (huge) @@ -2433,7 +2435,7 @@ int shmem_mfill_atomic_pte(struct mm_struct *dst_mm, int ret; pgoff_t max_off; - if (!shmem_inode_acct_block(inode, 1)) { + if (shmem_inode_acct_block(inode, 1)) { /* * We may have got a page, returned -ENOENT triggering a retry, * and now we find ourselves with -ENOMEM. Release the page, to From patchwork Thu Apr 20 08:03:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos Maiolino X-Patchwork-Id: 13218087 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 90C44C7EE21 for ; Thu, 20 Apr 2023 08:04:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 20317280002; Thu, 20 Apr 2023 04:04:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1B2B9280001; Thu, 20 Apr 2023 04:04:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E82A7280002; Thu, 20 Apr 2023 04:04:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id DB561280001 for ; Thu, 20 Apr 2023 04:04:11 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id B1BA3ABE60 for ; Thu, 20 Apr 2023 08:04:11 +0000 (UTC) X-FDA: 80701031502.14.868D33F Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf25.hostedemail.com (Postfix) with ESMTP id D5B9EA001B for ; Thu, 20 Apr 2023 08:04:09 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Mdwpddx6; spf=pass (imf25.hostedemail.com: domain of cem@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cem@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1681977850; 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=AjpifMXVrTe0UJjE9dYjTSsTVNLs1S21Yj4eNVJSAVA=; b=HymzHJ5AFKNi/xfy7Sg+p3MKsE8pUAAViKOTFthmnjQakmCq32fHLgGYEl8M5Zv1OJKUyw dfX8OjQMTMLN5y3m7Mf4qP/VPbZbGhGs5HRPr4PXFdt1y+EEyC5pRg4EOeVO3g5Z7H7zKh EuOwK8udqWciHhtCR/o67xMVGCrGwx4= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Mdwpddx6; spf=pass (imf25.hostedemail.com: domain of cem@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cem@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681977850; a=rsa-sha256; cv=none; b=1slqt9Z55EJueq5T7KG+wwdyg1gbt2xmRF61U+c+1+KA9q33UmZifVN7XRUPxgPEowS3l+ 733SPD6+yeDsERbn6La/qK3NVY5O+rEn7GceJqAk70/kxsbkp8Xyxxujyv+EbKCj/JSCON OR5wyYaQSHs1xXj6IZJ+34/g/F9sFpc= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 02A1F645CB; Thu, 20 Apr 2023 08:04:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0C264C433A4; Thu, 20 Apr 2023 08:04:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1681977848; bh=9rk3qzi0LCLvNlotHj311TU2VhsyZ4IZ0t7XLl4Pciw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Mdwpddx6Sa0KMlLeUc+DKrn45oW7U6l7VoKU6Z16FUL9TmeMbJhwK8aCgGqd8f7SR qtlt/fO/ZP0u4GG02IIWrRhZmnh96+14qgPhA1+6kEnfMFz+QCb3uRorHLC+ve/Iq7 s06X/nxRPwwzVEPurhdigulbEpQ31Ikz5muxCyXKEo3CFWQh+Th0yiMswYkDHj2A8f e2QNfotAxLZIyDBT6HBTZK8NHBiPgp/SFtFZc9FXd5qJDSsYEj8/DZwkxSf1R9ZLDn hWgaciGCgJHfcORviAD87emyocxXWC0IJTHIf/axCr67AN6/TCc5B8OlHMVYhpo7zw EK44hbFcHEyYQ== From: cem@kernel.org To: hughd@google.com Cc: jack@suse.cz, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, djwong@kernel.org Subject: [PATCH V2 2/6] shmem: make shmem_get_inode() return ERR_PTR instead of NULL Date: Thu, 20 Apr 2023 10:03:55 +0200 Message-Id: <20230420080359.2551150-3-cem@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230420080359.2551150-1-cem@kernel.org> References: <20230420080359.2551150-1-cem@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: D5B9EA001B X-Stat-Signature: aazo685n775k64pjhocnga6wooyqfzgq X-Rspam-User: X-HE-Tag: 1681977849-456 X-HE-Meta: U2FsdGVkX19e0XbqR/I047Qf2cV0yXAwow64q1UCvNLSgvIJuYXTWX8j/tPwvSoAWZd6dAcXR9u2Q8viXZ4fiA9g1WDbz6ITtuwmf4ids8OhnjJgMvF83JFh7fMrCKkAq09FAZE5CBtYxW2knQjgB3+HJ5v8iPYazsIpP5vAUf0wVTnmIyFbH39QYTl0dBeezLLI4TJ9wYHouejhfi8l57dCf3suguksmM1T+jQSy4oINlu7ne7KLHlDYxvyd/MmE9O0Sn9f4R7l1gZV5YPXOncVSZc85nU0eeuChqbkZaWgVhrzLoaYBDptplGqfN5Tpxefiit6b/pQZlDGHFkX2kKzSXv233j7y8WifHbUEES2YUFdgqTWb3vcpksgZEhhKP7QhFr47Bf01S0f+sR01GB47C2fKpo9zq//tudyXoMiSdETKLymGX+nSz1khQFfKQUUEMjLw6AnDSiXp89h1vkeHGvnygmVSbeD58Aaed4mpy46asPYx0sdovigQbOyZyADPHM5iG+Uf8g2OVTiNpYI9ItwdCwqgSSyM2L7Xm1QWjOJzDL9JRm1U3pIu5STjJ+dpv18tHnRmVTcsDcc5BJcTqzP++9M0jyajRsZyPw8kDVnGzyIzW4wvNK95z44D99au3EiXAFYGJqNjO0U2N5+wCVTMLSuZ+WZPSaxM5QCmNQNFlCVc+VD5hYhLpF0LPlCarlJdYYfasHwDShpWwQIWQRRc/IO9fe6uCJF9BKA/224D/yGYz9KXUKfMLaoAvxFXkIbZuVMizqYTzc7h7t3j6jZIQPohryX86pCiquj8lHd43R10LwSiZ4xOeETd7omT7Yashj504z8z/Za7I/J0reVQixStj+K/zmSBPkxzOJd9wzNVU/LV632mDwaTbgs+lOPIEANA2ZmYq1vMvfHya8bgoi165wwdllIoGJ5kqKuYLxkY520DoOfNTWQlS1pU/mVK31ofkWFRAt lheWcHnj FfDvFjFm44RXhdWoeRwv+LqqygKza4Ljaqg/DK1YK2rY+3nn1EqRu4/JSVUhkP6uDfgoTWevarU7vQMmVu0WLRp2LlAURn+NBbyRQbnUdipFTCXUyj8jSDa5ux1/QrMtfK8r7oDv9f9DvcGtqxjwiAUYsC6pRf95X2sYaUOEGLBlTZt7DGLMiYZVtqI9L53xAeQ3W2Zsc2vVy+xglswYP+9526AhmcbEbe9LFkOoJQuCIP8I4oE4wyBwHsTYFXPmgUvL32Nyfixc8uSlWfyQq1VM8pFaTUxl4zQ7DwO37ZBKw6H1Oaefug1pAQBf0/4B6N34jNddoJQ38iMXdAvpSkFUwoa43yAknMWjiPrCwVMhk8Ivp+Tk630x45gK5yu7jpXKh 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: From: Lukas Czerner Make shmem_get_inode() return ERR_PTR instead of NULL on error. This will be useful later when we introduce quota support. There should be no functional change. Signed-off-by: Lukas Czerner Signed-off-by: Carlos Maiolino --- V2: - Fix error handling logic in shmem_tmpfile() - Add missing idmap argument to the ramfs version of shmem_get_inode(), reported by Intel's kernel test robot mm/shmem.c | 206 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 116 insertions(+), 90 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index b1b3b826f6189..afa1985230166 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2354,65 +2354,71 @@ static struct inode *shmem_get_inode(struct mnt_idmap *idmap, struct super_block struct shmem_inode_info *info; struct shmem_sb_info *sbinfo = SHMEM_SB(sb); ino_t ino; + int err; + + err = shmem_reserve_inode(sb, &ino); + if (err) + return ERR_PTR(err); - if (shmem_reserve_inode(sb, &ino)) - return NULL; inode = new_inode(sb); - if (inode) { - inode->i_ino = ino; - inode_init_owner(idmap, inode, dir, mode); - inode->i_blocks = 0; - inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); - inode->i_generation = get_random_u32(); - info = SHMEM_I(inode); - memset(info, 0, (char *)inode - (char *)info); - spin_lock_init(&info->lock); - atomic_set(&info->stop_eviction, 0); - info->seals = F_SEAL_SEAL; - info->flags = flags & VM_NORESERVE; - info->i_crtime = inode->i_mtime; - info->fsflags = (dir == NULL) ? 0 : - SHMEM_I(dir)->fsflags & SHMEM_FL_INHERITED; - if (info->fsflags) - shmem_set_inode_flags(inode, info->fsflags); - INIT_LIST_HEAD(&info->shrinklist); - INIT_LIST_HEAD(&info->swaplist); - simple_xattrs_init(&info->xattrs); - cache_no_acl(inode); - mapping_set_large_folios(inode->i_mapping); - - switch (mode & S_IFMT) { - default: - inode->i_op = &shmem_special_inode_operations; - init_special_inode(inode, mode, dev); - break; - case S_IFREG: - inode->i_mapping->a_ops = &shmem_aops; - inode->i_op = &shmem_inode_operations; - inode->i_fop = &shmem_file_operations; - mpol_shared_policy_init(&info->policy, - shmem_get_sbmpol(sbinfo)); - break; - case S_IFDIR: - inc_nlink(inode); - /* Some things misbehave if size == 0 on a directory */ - inode->i_size = 2 * BOGO_DIRENT_SIZE; - inode->i_op = &shmem_dir_inode_operations; - inode->i_fop = &simple_dir_operations; - break; - case S_IFLNK: - /* - * Must not load anything in the rbtree, - * mpol_free_shared_policy will not be called. - */ - mpol_shared_policy_init(&info->policy, NULL); - break; - } - lockdep_annotate_inode_mutex_key(inode); - } else + if (!inode) { shmem_free_inode(sb); + return ERR_PTR(-ENOSPC); + } + + inode->i_ino = ino; + inode_init_owner(idmap, inode, dir, mode); + inode->i_blocks = 0; + inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); + inode->i_generation = get_random_u32(); + info = SHMEM_I(inode); + memset(info, 0, (char *)inode - (char *)info); + spin_lock_init(&info->lock); + atomic_set(&info->stop_eviction, 0); + info->seals = F_SEAL_SEAL; + info->flags = flags & VM_NORESERVE; + info->i_crtime = inode->i_mtime; + info->fsflags = (dir == NULL) ? 0 : + SHMEM_I(dir)->fsflags & SHMEM_FL_INHERITED; + if (info->fsflags) + shmem_set_inode_flags(inode, info->fsflags); + INIT_LIST_HEAD(&info->shrinklist); + INIT_LIST_HEAD(&info->swaplist); + simple_xattrs_init(&info->xattrs); + cache_no_acl(inode); + mapping_set_large_folios(inode->i_mapping); + + switch (mode & S_IFMT) { + default: + inode->i_op = &shmem_special_inode_operations; + init_special_inode(inode, mode, dev); + break; + case S_IFREG: + inode->i_mapping->a_ops = &shmem_aops; + inode->i_op = &shmem_inode_operations; + inode->i_fop = &shmem_file_operations; + mpol_shared_policy_init(&info->policy, + shmem_get_sbmpol(sbinfo)); + break; + case S_IFDIR: + inc_nlink(inode); + /* Some things misbehave if size == 0 on a directory */ + inode->i_size = 2 * BOGO_DIRENT_SIZE; + inode->i_op = &shmem_dir_inode_operations; + inode->i_fop = &simple_dir_operations; + break; + case S_IFLNK: + /* + * Must not load anything in the rbtree, + * mpol_free_shared_policy will not be called. + */ + mpol_shared_policy_init(&info->policy, NULL); + break; + } + + lockdep_annotate_inode_mutex_key(inode); return inode; } @@ -2927,27 +2933,30 @@ shmem_mknod(struct mnt_idmap *idmap, struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) { struct inode *inode; - int error = -ENOSPC; + int error; inode = shmem_get_inode(idmap, dir->i_sb, dir, mode, dev, VM_NORESERVE); - if (inode) { - error = simple_acl_create(dir, inode); - if (error) - goto out_iput; - error = security_inode_init_security(inode, dir, - &dentry->d_name, - shmem_initxattrs, NULL); - if (error && error != -EOPNOTSUPP) - goto out_iput; - error = 0; - dir->i_size += BOGO_DIRENT_SIZE; - dir->i_ctime = dir->i_mtime = current_time(dir); - inode_inc_iversion(dir); - d_instantiate(dentry, inode); - dget(dentry); /* Extra count - pin the dentry in core */ - } + if (IS_ERR(inode)) + return PTR_ERR(inode); + + error = simple_acl_create(dir, inode); + if (error) + goto out_iput; + error = security_inode_init_security(inode, dir, + &dentry->d_name, + shmem_initxattrs, NULL); + if (error && error != -EOPNOTSUPP) + goto out_iput; + + error = 0; + dir->i_size += BOGO_DIRENT_SIZE; + dir->i_ctime = dir->i_mtime = current_time(dir); + inode_inc_iversion(dir); + d_instantiate(dentry, inode); + dget(dentry); /* Extra count - pin the dentry in core */ return error; + out_iput: iput(inode); return error; @@ -2958,20 +2967,26 @@ shmem_tmpfile(struct mnt_idmap *idmap, struct inode *dir, struct file *file, umode_t mode) { struct inode *inode; - int error = -ENOSPC; + int error; inode = shmem_get_inode(idmap, dir->i_sb, dir, mode, 0, VM_NORESERVE); - if (inode) { - error = security_inode_init_security(inode, dir, - NULL, - shmem_initxattrs, NULL); - if (error && error != -EOPNOTSUPP) - goto out_iput; - error = simple_acl_create(dir, inode); - if (error) - goto out_iput; - d_tmpfile(file, inode); + + if (IS_ERR(inode)) { + error = PTR_ERR(inode); + goto err_out; } + + error = security_inode_init_security(inode, dir, + NULL, + shmem_initxattrs, NULL); + if (error && error != -EOPNOTSUPP) + goto out_iput; + error = simple_acl_create(dir, inode); + if (error) + goto out_iput; + d_tmpfile(file, inode); + +err_out: return finish_open_simple(file, error); out_iput: iput(inode); @@ -3146,8 +3161,9 @@ static int shmem_symlink(struct mnt_idmap *idmap, struct inode *dir, inode = shmem_get_inode(idmap, dir->i_sb, dir, S_IFLNK | 0777, 0, VM_NORESERVE); - if (!inode) - return -ENOSPC; + + if (IS_ERR(inode)) + return PTR_ERR(inode); error = security_inode_init_security(inode, dir, &dentry->d_name, shmem_initxattrs, NULL); @@ -3762,12 +3778,13 @@ static int shmem_fill_super(struct super_block *sb, struct fs_context *fc) struct shmem_options *ctx = fc->fs_private; struct inode *inode; struct shmem_sb_info *sbinfo; + int error = -ENOMEM; /* Round up to L1_CACHE_BYTES to resist false sharing */ sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info), L1_CACHE_BYTES), GFP_KERNEL); if (!sbinfo) - return -ENOMEM; + return error; sb->s_fs_info = sbinfo; @@ -3829,8 +3846,10 @@ static int shmem_fill_super(struct super_block *sb, struct fs_context *fc) inode = shmem_get_inode(&nop_mnt_idmap, sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE); - if (!inode) + if (IS_ERR(inode)) { + error = PTR_ERR(inode); goto failed; + } inode->i_uid = sbinfo->uid; inode->i_gid = sbinfo->gid; sb->s_root = d_make_root(inode); @@ -3840,7 +3859,7 @@ static int shmem_fill_super(struct super_block *sb, struct fs_context *fc) failed: shmem_put_super(sb); - return -ENOMEM; + return error; } static int shmem_get_tree(struct fs_context *fc) @@ -4209,10 +4228,16 @@ EXPORT_SYMBOL_GPL(shmem_truncate_range); #define shmem_vm_ops generic_file_vm_ops #define shmem_anon_vm_ops generic_file_vm_ops #define shmem_file_operations ramfs_file_operations -#define shmem_get_inode(idmap, sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev) #define shmem_acct_size(flags, size) 0 #define shmem_unacct_size(flags, size) do {} while (0) +static inline struct inode *shmem_get_inode(struct mnt_idmap, struct super_block *sb, struct inode *dir, + umode_t mode, dev_t dev, unsigned long flags) +{ + struct inode *inode = ramfs_get_inode(sb, dir, mode, dev); + return inode ? inode : ERR_PTR(-ENOSPC); +} + #endif /* CONFIG_SHMEM */ /* common code */ @@ -4237,9 +4262,10 @@ static struct file *__shmem_file_setup(struct vfsmount *mnt, const char *name, l inode = shmem_get_inode(&nop_mnt_idmap, mnt->mnt_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags); - if (unlikely(!inode)) { + + if (IS_ERR(inode)) { shmem_unacct_size(flags, size); - return ERR_PTR(-ENOSPC); + return ERR_CAST(inode); } inode->i_flags |= i_flags; inode->i_size = size; From patchwork Thu Apr 20 08:03:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos Maiolino X-Patchwork-Id: 13218086 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 6931EC7EE22 for ; Thu, 20 Apr 2023 08:04:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E363F280003; Thu, 20 Apr 2023 04:04:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D97FF280001; Thu, 20 Apr 2023 04:04:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B9C5C280003; Thu, 20 Apr 2023 04:04:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id A3F0C280001 for ; Thu, 20 Apr 2023 04:04:13 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 7D3011601EB for ; Thu, 20 Apr 2023 08:04:13 +0000 (UTC) X-FDA: 80701031586.15.6FD4875 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf05.hostedemail.com (Postfix) with ESMTP id CECAC100009 for ; Thu, 20 Apr 2023 08:04:11 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=mBkZQ1F9; spf=pass (imf05.hostedemail.com: domain of cem@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cem@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1681977851; 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=0Bcp8NKJjo7BLV/J1m4qAwfZ3G/eRsQ/n6flfpgnhjE=; b=FSM2XJx8y8WasXu+0PQ6zrdQAZykzjRiDKiazmEAB08FYnGFqEq0oHmbpGoBmuAGsLzEMM uelf3uBzh45R99RhdRXa3+RKYG077wLsemmI6/Nx/GQOvD5Y9m+Efti1/xzfQjGYzwLaQ4 iPvaus+VUXDhjApKyv6ju9A0kJxCfYo= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=mBkZQ1F9; spf=pass (imf05.hostedemail.com: domain of cem@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cem@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681977851; a=rsa-sha256; cv=none; b=TuGDW4K2O0054vMJzeBF6qoRsJ1fSw4w8sZ0E2pJhNOwfpriDInNNmNjuoYPYJQpqZ02N3 tTSuX2azSAa1vrUH2NJpW4fSL/k+24kkMtJH9xx3xPho3L6I4VDJt3Fcp5NxPJcrxgZqKP W2cJ/nmhF1ybH7fsL2yxRSJyxR80iBE= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id ECAAF645C9; Thu, 20 Apr 2023 08:04:10 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F31E3C4339E; Thu, 20 Apr 2023 08:04:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1681977850; bh=0+8LxOKXGKCvqagfKq127FWqroGkUmeQVQasqMNW7Qw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mBkZQ1F9VgycRDHsoGbYIdJtpqkW9EUk1GP7t6FjuL6HKZv7NZYs7B4nA0uLo2WnL CSFRvDYZeyW8hv+NcwD2Y2x8arQ9PYEbA96I/8de0RT1d7N9KiT+ZQJvukutA3e9ad 02H2Sal8tVnAP2dVSrViq4u28Q4m9fIiuCNe2bfuo0DOK7oeazw4cOXsUHKlKihS2n S2+DHGwkIVVYCJKxC3MXg/XtgaksF232vwGWlfJ1gXvW8rGJoTuweYR+jxZzb/HOs/ FVvMUMKy+s5I1O72wGLNOvxYFHoyQfH+ibXFMqgP8qxP8st113hgaAUypx8yMmRtER wRDRR1RJ1P9Rg== From: cem@kernel.org To: hughd@google.com Cc: jack@suse.cz, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, djwong@kernel.org Subject: [PATCH 3/6] quota: Check presence of quota operation structures instead of ->quota_read and ->quota_write callbacks Date: Thu, 20 Apr 2023 10:03:56 +0200 Message-Id: <20230420080359.2551150-4-cem@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230420080359.2551150-1-cem@kernel.org> References: <20230420080359.2551150-1-cem@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: CECAC100009 X-Stat-Signature: e1smi1xhfundurouwjniq3uc5r5pjeyp X-Rspam-User: X-HE-Tag: 1681977851-850296 X-HE-Meta: U2FsdGVkX19q/kMEvCRQ+KHA6HoYehup0lH+CGtSCF9xJtKKQ45NafXksKPPw81WKOTjXPXXGBKjrT4+d3Rs2UwNwvrnA7SJCxxBBXTNuCvyPgeIMBmtUE8OUOks1BtRENVJ7g3WH/8JIc9XhY3oNn8lYyWrMYzca5r9S72fLs2YBd3KtK7Dduw0KOHFfEZ0CQ2Pildeq0OwsJLEUcxFoC3iXg/GZgHTuB7v9Gv26VrjsjbCWV/xOZWC447sedOfsYY+mcl7Mv/uqe0OrMQzaAgTRCVV8pTD7vDe5LjM/Xd+ds2eImbdkpE6PcyB8Hksna3dxgCyqYqX1T9ntGg0zlMBM1KHb7qDCdsvfPE2PlX4hECwG9syw4mpvjfFKWKF5Ne/QHq9Hie07qYRYj0USyIXFj4pE4xiOADXtCTwSS3RCGnDzgoiHDod+a6F6pd9i/ErqKD/Mf7gd72UGSynYgyKsrUCz1fqd8K0X4+uwAxiVnS4DVe8vxs7JfMpwJ2xi5XVYKgnvWI5rApV793En6B0AF7qHtRvLifPQyDSkVkH50EbyfMAujE7XW/YAlWT0TT4uEqTmWtsYC23M7lI+GjYDnPk5DG4vqLhkM8Nb7gH1Jivxu4zI1qKxO5JXC+qgVN3+n67EZQXA6kko/6Iy/dG1FCCEjw0VolBPBLnzAwNd3LG0+3jI9dXLGBhFnzx8zWlZR1xzGwLDeDcOWqEqN2IraUsnAAcD2ivGTDLu/J2xt90Euc+IKTU1/8AzTYG5/WhsWZ0oRE1ozrZsacQwcyNgL9eWQRDEB7npH4IZn6PmzJVclprxFHlO5SBj02YdaNEOIC3/zvLdLPDfD51navtvZHCKA3Z9Uq/82y1Ko+H87oBGuSFD+0yqY+Jg8eTqom6YstVViRP3tzc2vz4aG3jKGE3qubyDcuwdmBpk1/bBqJ7hJ0LpzvPLLyjqQ4NYEM1AaBKUCe71wGzqUD WhUJq2Xa Oo3SGCALPbxmUpm1PI2OOx6S0Gtj/w6NBJkyFV7Y2GyED5l/oGaEWCp2gbGt63FDGAcInY/HchnPFYevkIl+wMDv5gpStCRB/bauf5w0W4xwBmJtxdpqGN3ex+H0R7R2tR0GLp/pzkehW5LYw6ghtbXc31EixllknHvXiXv8/pKYnW2zNq37Uq1JPkSSa24vhCN0dESWqt/p+r12TMj0mbonhxnfclVvEEmVPvHBBU5lKYN5pEzhsWDmYFKUW5csGLNbkyDImmzCe07lXyPINz4oiQsCsjv7FTYXaTXohtgovmRx5J03Jp9eFvChBRfchBHSoTa7hVOPsdKjUGfy9/F6WOuok3jNSpLbDfB3Q3o3iKKc= 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: From: Jan Kara Currently we check whether superblock has ->quota_read and ->quota_write operations to check whether filesystem supports quotas. However for example for shmfs we will not read or write dquots so check whether quota operations are set in the superblock instead. Signed-off-by: Jan Kara Reviewed-by: Carlos Maiolino --- fs/quota/dquot.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/quota/dquot.c b/fs/quota/dquot.c index a6357f728034a..81563a83b609d 100644 --- a/fs/quota/dquot.c +++ b/fs/quota/dquot.c @@ -2367,7 +2367,7 @@ int dquot_load_quota_sb(struct super_block *sb, int type, int format_id, if (!fmt) return -ESRCH; - if (!sb->s_op->quota_write || !sb->s_op->quota_read || + if (!sb->dq_op || !sb->s_qcop || (type == PRJQUOTA && sb->dq_op->get_projid == NULL)) { error = -EINVAL; goto out_fmt; From patchwork Thu Apr 20 08:03:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos Maiolino X-Patchwork-Id: 13218088 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 D32F4C77B78 for ; Thu, 20 Apr 2023 08:04:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 49980280004; Thu, 20 Apr 2023 04:04:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3FD7B280001; Thu, 20 Apr 2023 04:04:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 24CEB280004; Thu, 20 Apr 2023 04:04:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 1743B280001 for ; Thu, 20 Apr 2023 04:04:16 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id DB5A8ABE60 for ; Thu, 20 Apr 2023 08:04:15 +0000 (UTC) X-FDA: 80701031670.17.34C221C Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf15.hostedemail.com (Postfix) with ESMTP id E95BCA000A for ; Thu, 20 Apr 2023 08:04:13 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Iw+7kUz7; spf=pass (imf15.hostedemail.com: domain of cem@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cem@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1681977854; 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=lcHM4ilGOn5jk6z69nhxUcV5LheSTQ159LoPj+6HEN0=; b=BhJb6kWIHMgz1eXEEx3ezqezCeJxy7qck1+rSgLCHihrioS7LXXJngFqCr7YbEq3u41iKx ewyNwmLk+iKau5rVqKhVWzKCksd+goLDr8vkjNTyFTeDvVY0UWiWaPS9rXfxQCRcyqGSIN bDQVOFl44QgyN/chVyLVdQoVHUT3Xx0= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Iw+7kUz7; spf=pass (imf15.hostedemail.com: domain of cem@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cem@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681977854; a=rsa-sha256; cv=none; b=ROKrN8x0b5tvrKMYvadFxrhzEygtGt2hPQurAyYsz4BeKcoglvdR8w4nT00bXl/d8KAKWQ 9V7iTXfNubjleCJH9bC/+qMAfASQfHtNJ0RVntcuZsO0Q614qSDPjR4/pmlCk7cD6kgWZZ m7fMNscWdRpBfMW/2wrLHr682zykD4A= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id E352A645CB; Thu, 20 Apr 2023 08:04:12 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E5C4EC433AF; Thu, 20 Apr 2023 08:04:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1681977852; bh=3IJ9BqE8ay7FCWM1ip3rhH3q+qJR7XvXG5rjsBARkz4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Iw+7kUz7MVxkuLxXn2ThnsVayfMhadvb2YoAmDM53fQu+jVCMVsOH4Z+vhzbBL+Ev qJTX/H9LSw26iXE+1thc81pnfXkEGFbRvkJjW8E3yvSHkr3R6NW6NXhKDukJxecGri JSxhTh6Py6lEyOoK0gmayLyxjepT3ODugxyzoTTP+ON/5E6I15O0eP1nHeyvzUFBkv Q/32vyMs9JMKeoMkeBmcNgndq7kBWcnVCjFrPuMdeV+7+PCHA9E1Dp9e1EspUamhn2 rUhcNUWbgKmua9Ss8Xumb7emftJTjxZFiw5JJtGues49qr39dLqTIJOISadar8+U8P mBoJF8TDUPhog== From: cem@kernel.org To: hughd@google.com Cc: jack@suse.cz, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, djwong@kernel.org Subject: [PATCH V2 4/6] shmem: prepare shmem quota infrastructure Date: Thu, 20 Apr 2023 10:03:57 +0200 Message-Id: <20230420080359.2551150-5-cem@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230420080359.2551150-1-cem@kernel.org> References: <20230420080359.2551150-1-cem@kernel.org> MIME-Version: 1.0 X-Stat-Signature: k45chn1y5ek3yg74dzkizt713fa1qrcs X-Rspam-User: X-Rspamd-Queue-Id: E95BCA000A X-Rspamd-Server: rspam06 X-HE-Tag: 1681977853-631033 X-HE-Meta: U2FsdGVkX1816MUoni2e8w9bvkJ5K/RYFtW5RaOYUi0X1Hfwsy+8YQy5AjGCa/7dSi0jGXxgCmGosZXQslP5vDmhu4qCau58z54t/lH1NN+eqqClx0C3Cwxj+dgPw6+QeHpZkFLPk1VirYJhGwa0LFXK8mX1zzLFGglYTKnZpDPfO5Dm4nPguhVaVZVrvyia6QV8obKUYL4DPZTDiQ+vUj4AT2FSm7aWqVjnhrWX4KQh4rmGzLCqWWuqvPfcH72K0nn8fs3fPVY01a2TalQMMVelLUHY4bE0oxKN3gWbN1tJRkFJ3k+z8JlZq4sPphMqDveJLJiqZoQXFzP2a7HluUQeMl5UJ+URtGvGOB/t0Te3IaMoZukRJWZPq1d8DPRtgpQiq313vrOIMEJnknyeadqodqAxJBjJRvG6j33+ojXaP/M2081CTFvLMIhnUHcRzxKukPQe6Dq7LSKmCDK2uBIq2gscuoMIm3jE/e/AOych87a+6NN3HhUFbCEwCJmC3thGkWk+c8miPWzpzWicb4mZngUUxC6tn4LXiOFqUymHtgbZZ1B38RVg4xR9YO17y1njx0+pN/W5zBuDgb7JGR/7E7rQ5gXUz82H0N9F2c7HphWQnuo0yhXR36LkLaXs6VwRPBpeoM4ReqHvUu7Y5SEmh05wvTQYJC338E258pVSVdc3VRLkrGYfuok5yXJnCN1VplMmalMhVBW3Q/UPOFUM01J9m+vfrBgkSI3LG7GXO+wrs9dH0JQKBvYhGLhx5aNZRBfimq4OCA8cNWZrH2fVQG0Os4X5eJtdBu4TT6x/A4vO9cWHcmiWqaEruHN6uJKrh2zz9Tu7AfGb+pBOvMw+LwJEli9MZbFY28AtdTAPG/y8m9Xbe31O55tIoQwnhDbcLS2LQicYytcC+HtF6qPKGfl1OfGc2Do/ogBdO4eLDNrEUnkuHPNHb6uEgwyIhCOxn87cVsjHbotpC9q tiHMAHY4 lMxPeMi2P1/9EzZTc/HISKb06we62MVnqYhC88h2t7XJMJ2ZG1MTT9ANJTEhhHLFPN46ak8aYb0sexzx8nmomhBfmc5i6RsWmq9TP1ETGHbQOB33UZUXVCR2tyXjKp1GyOCqqhaPiAZk47q2IQ1Vp6kkfNmP7pA8+rq2AK8IJRJGog56ahq+ZaatDyAABwBvoMgOR44zMskmIpfi7CjLdJ+gm7r4GlexC2L0x2Ge+fGltJOJhtRnbVExTFYM9JIc/6eepElukjp3BENS1UXk7XspE1j6WBLP01Z7QYdW/g0ZLMc967h/xadwPwwF8ovzQHJ0T 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: From: Lukas Czerner Add new shmem quota format, its quota_format_ops together with dquot_operations Signed-off-by: Lukas Czerner Signed-off-by: Carlos Maiolino Reviewed-by: Jan Kara --- V2: - define SHMEM_MAXQUOTAS in include/linux/shmem_fs.h instead of mm/shmem_quota.c - Add a warning and error return if shmem_release_dquot() does not find an entry in the rb tree. fs/Kconfig | 12 ++ include/linux/shmem_fs.h | 12 ++ include/uapi/linux/quota.h | 1 + mm/Makefile | 2 +- mm/shmem_quota.c | 318 +++++++++++++++++++++++++++++++++++++ 5 files changed, 344 insertions(+), 1 deletion(-) create mode 100644 mm/shmem_quota.c diff --git a/fs/Kconfig b/fs/Kconfig index e99830c650336..ba809882f1961 100644 --- a/fs/Kconfig +++ b/fs/Kconfig @@ -233,6 +233,18 @@ config TMPFS_INODE64 If unsure, say N. +config TMPFS_QUOTA + bool "Tmpfs quota support" + depends on TMPFS + select QUOTA + help + Quota support allows to set per user and group limits for tmpfs + usage. Say Y to enable quota support. Once enabled you can control + user and group quota enforcement with quota, usrquota and grpquota + mount options. + + If unsure, say N. + config ARCH_SUPPORTS_HUGETLBFS def_bool n diff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h index 103d1000a5a2e..92db49ebd5452 100644 --- a/include/linux/shmem_fs.h +++ b/include/linux/shmem_fs.h @@ -12,6 +12,10 @@ /* inode in-kernel data */ +#ifdef CONFIG_TMPFS_QUOTA +#define SHMEM_MAXQUOTAS 2 +#endif + struct shmem_inode_info { spinlock_t lock; unsigned int seals; /* shmem seals */ @@ -163,4 +167,12 @@ extern int shmem_mfill_atomic_pte(struct mm_struct *dst_mm, pmd_t *dst_pmd, #endif /* CONFIG_SHMEM */ #endif /* CONFIG_USERFAULTFD */ +/* + * Used space is stored as unsigned 64-bit value in bytes but + * quota core supports only signed 64-bit values so use that + * as a limit + */ +#define SHMEM_QUOTA_MAX_SPC_LIMIT 0x7fffffffffffffffLL /* 2^63-1 */ +#define SHMEM_QUOTA_MAX_INO_LIMIT 0x7fffffffffffffffLL + #endif diff --git a/include/uapi/linux/quota.h b/include/uapi/linux/quota.h index f17c9636a8593..52090105b8282 100644 --- a/include/uapi/linux/quota.h +++ b/include/uapi/linux/quota.h @@ -77,6 +77,7 @@ #define QFMT_VFS_V0 2 #define QFMT_OCFS2 3 #define QFMT_VFS_V1 4 +#define QFMT_SHMEM 5 /* Size of block in which space limits are passed through the quota * interface */ diff --git a/mm/Makefile b/mm/Makefile index 8e105e5b3e293..977d2b66775a9 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -52,7 +52,7 @@ obj-y := filemap.o mempool.o oom_kill.o fadvise.o \ readahead.o swap.o truncate.o vmscan.o shmem.o \ util.o mmzone.o vmstat.o backing-dev.o \ mm_init.o percpu.o slab_common.o \ - compaction.o \ + compaction.o shmem_quota.o\ interval_tree.o list_lru.o workingset.o \ debug.o gup.o mmap_lock.o $(mmu-y) diff --git a/mm/shmem_quota.c b/mm/shmem_quota.c new file mode 100644 index 0000000000000..c0b531e2ef688 --- /dev/null +++ b/mm/shmem_quota.c @@ -0,0 +1,318 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * In memory quota format relies on quota infrastructure to store dquot + * information for us. While conventional quota formats for file systems + * with persistent storage can load quota information into dquot from the + * storage on-demand and hence quota dquot shrinker can free any dquot + * that is not currently being used, it must be avoided here. Otherwise we + * can lose valuable information, user provided limits, because there is + * no persistent storage to load the information from afterwards. + * + * One information that in-memory quota format needs to keep track of is + * a sorted list of ids for each quota type. This is done by utilizing + * an rb tree which root is stored in mem_dqinfo->dqi_priv for each quota + * type. + * + * This format can be used to support quota on file system without persistent + * storage such as tmpfs. + * + * Author: Lukas Czerner + * Carlos Maiolino + * + * Copyright (C) 2023 Red Hat, Inc. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#ifdef CONFIG_TMPFS_QUOTA + +/* + * The following constants define the amount of time given a user + * before the soft limits are treated as hard limits (usually resulting + * in an allocation failure). The timer is started when the user crosses + * their soft limit, it is reset when they go below their soft limit. + */ +#define SHMEM_MAX_IQ_TIME 604800 /* (7*24*60*60) 1 week */ +#define SHMEM_MAX_DQ_TIME 604800 /* (7*24*60*60) 1 week */ + +struct quota_id { + struct rb_node node; + qid_t id; + qsize_t bhardlimit; + qsize_t bsoftlimit; + qsize_t ihardlimit; + qsize_t isoftlimit; +}; + +static int shmem_check_quota_file(struct super_block *sb, int type) +{ + /* There is no real quota file, nothing to do */ + return 1; +} + +/* + * There is no real quota file. Just allocate rb_root for quota ids and + * set limits + */ +static int shmem_read_file_info(struct super_block *sb, int type) +{ + struct quota_info *dqopt = sb_dqopt(sb); + struct mem_dqinfo *info = &dqopt->info[type]; + + info->dqi_priv = kzalloc(sizeof(struct rb_root), GFP_NOFS); + if (!info->dqi_priv) + return -ENOMEM; + + info->dqi_max_spc_limit = SHMEM_QUOTA_MAX_SPC_LIMIT; + info->dqi_max_ino_limit = SHMEM_QUOTA_MAX_INO_LIMIT; + + info->dqi_bgrace = SHMEM_MAX_DQ_TIME; + info->dqi_igrace = SHMEM_MAX_IQ_TIME; + info->dqi_flags = 0; + + return 0; +} + +static int shmem_write_file_info(struct super_block *sb, int type) +{ + /* There is no real quota file, nothing to do */ + return 0; +} + +/* + * Free all the quota_id entries in the rb tree and rb_root. + */ +static int shmem_free_file_info(struct super_block *sb, int type) +{ + struct mem_dqinfo *info = &sb_dqopt(sb)->info[type]; + struct rb_root *root = info->dqi_priv; + struct quota_id *entry; + struct rb_node *node; + + info->dqi_priv = NULL; + node = rb_first(root); + while (node) { + entry = rb_entry(node, struct quota_id, node); + node = rb_next(&entry->node); + + rb_erase(&entry->node, root); + kfree(entry); + } + + kfree(root); + return 0; +} + +static int shmem_get_next_id(struct super_block *sb, struct kqid *qid) +{ + struct mem_dqinfo *info = sb_dqinfo(sb, qid->type); + struct rb_node *node = ((struct rb_root *)info->dqi_priv)->rb_node; + qid_t id = from_kqid(&init_user_ns, *qid); + struct quota_info *dqopt = sb_dqopt(sb); + struct quota_id *entry = NULL; + int ret = 0; + + if (!sb_has_quota_active(sb, qid->type)) + return -ESRCH; + + down_read(&dqopt->dqio_sem); + while (node) { + entry = rb_entry(node, struct quota_id, node); + + if (id < entry->id) + node = node->rb_left; + else if (id > entry->id) + node = node->rb_right; + else + goto got_next_id; + } + + if (!entry) { + ret = -ENOENT; + goto out_unlock; + } + + if (id > entry->id) { + node = rb_next(&entry->node); + if (!node) { + ret = -ENOENT; + goto out_unlock; + } + entry = rb_entry(node, struct quota_id, node); + } + +got_next_id: + *qid = make_kqid(&init_user_ns, qid->type, entry->id); +out_unlock: + up_read(&dqopt->dqio_sem); + return ret; +} + +/* + * Load dquot with limits from existing entry, or create the new entry if + * it does not exist. + */ +static int shmem_acquire_dquot(struct dquot *dquot) +{ + struct mem_dqinfo *info = sb_dqinfo(dquot->dq_sb, dquot->dq_id.type); + struct rb_node **n = &((struct rb_root *)info->dqi_priv)->rb_node; + struct rb_node *parent = NULL, *new_node = NULL; + struct quota_id *new_entry, *entry; + qid_t id = from_kqid(&init_user_ns, dquot->dq_id); + struct quota_info *dqopt = sb_dqopt(dquot->dq_sb); + int ret = 0; + + mutex_lock(&dquot->dq_lock); + + down_write(&dqopt->dqio_sem); + while (*n) { + parent = *n; + entry = rb_entry(parent, struct quota_id, node); + + if (id < entry->id) + n = &(*n)->rb_left; + else if (id > entry->id) + n = &(*n)->rb_right; + else + goto found; + } + + /* We don't have entry for this id yet, create it */ + new_entry = kzalloc(sizeof(struct quota_id), GFP_NOFS); + if (!new_entry) { + ret = -ENOMEM; + goto out_unlock; + } + + new_entry->id = id; + new_node = &new_entry->node; + rb_link_node(new_node, parent, n); + rb_insert_color(new_node, (struct rb_root *)info->dqi_priv); + entry = new_entry; + +found: + /* Load the stored limits from the tree */ + spin_lock(&dquot->dq_dqb_lock); + dquot->dq_dqb.dqb_bhardlimit = entry->bhardlimit; + dquot->dq_dqb.dqb_bsoftlimit = entry->bsoftlimit; + dquot->dq_dqb.dqb_ihardlimit = entry->ihardlimit; + dquot->dq_dqb.dqb_isoftlimit = entry->isoftlimit; + + if (!dquot->dq_dqb.dqb_bhardlimit && + !dquot->dq_dqb.dqb_bsoftlimit && + !dquot->dq_dqb.dqb_ihardlimit && + !dquot->dq_dqb.dqb_isoftlimit) + set_bit(DQ_FAKE_B, &dquot->dq_flags); + spin_unlock(&dquot->dq_dqb_lock); + + /* Make sure flags update is visible after dquot has been filled */ + smp_mb__before_atomic(); + set_bit(DQ_ACTIVE_B, &dquot->dq_flags); +out_unlock: + up_write(&dqopt->dqio_sem); + mutex_unlock(&dquot->dq_lock); + return ret; +} + +/* + * Store limits from dquot in the tree unless it's fake. If it is fake + * remove the id from the tree since there is no useful information in + * there. + */ +static int shmem_release_dquot(struct dquot *dquot) +{ + struct mem_dqinfo *info = sb_dqinfo(dquot->dq_sb, dquot->dq_id.type); + struct rb_node *node = ((struct rb_root *)info->dqi_priv)->rb_node; + qid_t id = from_kqid(&init_user_ns, dquot->dq_id); + struct quota_info *dqopt = sb_dqopt(dquot->dq_sb); + struct quota_id *entry = NULL; + + mutex_lock(&dquot->dq_lock); + /* Check whether we are not racing with some other dqget() */ + if (dquot_is_busy(dquot)) + goto out_dqlock; + + down_write(&dqopt->dqio_sem); + while (node) { + entry = rb_entry(node, struct quota_id, node); + + if (id < entry->id) + node = node->rb_left; + else if (id > entry->id) + node = node->rb_right; + else + goto found; + } + + /* We should always find the entry in the rb tree */ + WARN_ONCE(1, "quota id %u from dquot %p, not in rb tree!\n", id, dquot); + up_write(&dqopt->dqio_sem); + mutex_unlock(&dquot->dq_lock); + return -ENOENT; + +found: + if (test_bit(DQ_FAKE_B, &dquot->dq_flags)) { + /* Remove entry from the tree */ + rb_erase(&entry->node, info->dqi_priv); + kfree(entry); + } else { + /* Store the limits in the tree */ + spin_lock(&dquot->dq_dqb_lock); + entry->bhardlimit = dquot->dq_dqb.dqb_bhardlimit; + entry->bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit; + entry->ihardlimit = dquot->dq_dqb.dqb_ihardlimit; + entry->isoftlimit = dquot->dq_dqb.dqb_isoftlimit; + spin_unlock(&dquot->dq_dqb_lock); + } + + clear_bit(DQ_ACTIVE_B, &dquot->dq_flags); + up_write(&dqopt->dqio_sem); + +out_dqlock: + mutex_unlock(&dquot->dq_lock); + return 0; +} + +int shmem_mark_dquot_dirty(struct dquot *dquot) +{ + return 0; +} + +int shmem_dquot_write_info(struct super_block *sb, int type) +{ + return 0; +} + +static const struct quota_format_ops shmem_format_ops = { + .check_quota_file = shmem_check_quota_file, + .read_file_info = shmem_read_file_info, + .write_file_info = shmem_write_file_info, + .free_file_info = shmem_free_file_info, +}; + +struct quota_format_type shmem_quota_format = { + .qf_fmt_id = QFMT_SHMEM, + .qf_ops = &shmem_format_ops, + .qf_owner = THIS_MODULE +}; + +const struct dquot_operations shmem_quota_operations = { + .acquire_dquot = shmem_acquire_dquot, + .release_dquot = shmem_release_dquot, + .alloc_dquot = dquot_alloc, + .destroy_dquot = dquot_destroy, + .write_info = shmem_dquot_write_info, + .mark_dirty = shmem_mark_dquot_dirty, + .get_next_id = shmem_get_next_id, +}; +#endif /* CONFIG_TMPFS_QUOTA */ From patchwork Thu Apr 20 08:03:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos Maiolino X-Patchwork-Id: 13218089 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 CA203C77B72 for ; Thu, 20 Apr 2023 08:04:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 69477280005; Thu, 20 Apr 2023 04:04:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5F6AB280001; Thu, 20 Apr 2023 04:04:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4972F280005; Thu, 20 Apr 2023 04:04:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 3807D280001 for ; Thu, 20 Apr 2023 04:04:18 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 07DA61201DA for ; Thu, 20 Apr 2023 08:04:18 +0000 (UTC) X-FDA: 80701031796.24.CF9EA26 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf22.hostedemail.com (Postfix) with ESMTP id 4A3BCC0022 for ; Thu, 20 Apr 2023 08:04:16 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Wk0NlYzX; spf=pass (imf22.hostedemail.com: domain of cem@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cem@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1681977856; 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=aFHIwYM7t0fu+QW3frVYyAY6gbNNn5ynqFNGX1EBRhg=; b=wk5TpLUbe3QzaL+0MlFIiCM2XGrdv5q0iWtUHbnL1XoMlGFInkmizbJBpWPGmvbUxO6/Np /SX/ykuNFGXgnRQouIYRPoYXXMHSmepJbfgxUrQpW62Yh+GebFc4oJBeOFOG7PPpto8cxq z3dBgCJZt3qgr1bRFT+sxNQ+x7faHdI= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681977856; a=rsa-sha256; cv=none; b=7ToykSi6aAQBonjmTGpgaR9YUgL5C/ac1e1vhcKnY9Dd0JKxV80MJWj8MyUpVBnb7QMoBi Z7P4fVJbn0Kbc2GefpNvC5ZBzq0yIIhENiFNhoC1dbc1MllsyztWCM/rEVb9kH+G4Ltl5N N/CQJlNm71rsvRBzizw0papTW4CqpWw= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Wk0NlYzX; spf=pass (imf22.hostedemail.com: domain of cem@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cem@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 73991645CA; Thu, 20 Apr 2023 08:04:15 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D890AC433D2; Thu, 20 Apr 2023 08:04:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1681977854; bh=Bz8jWy6uy3k4tfiy7uX5cXZN1fsySJ/ofRKbKLBLucE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Wk0NlYzXYFgyCkuhOZqEaAYWO6DJcIVRADIOLz7GeB0+abymjo6yl4y9ZgJYW6hty OpAZkauPc1COQnhOBGLiYXZcfPlRQ6vq7DkE0l0rnrxu+SYCMAWh+Ts3doXKs5SSbl PE7eMookyNnZwDbVsBS2K3aFYjUj3dBwMVmYlRXFPWSWzdw+/XGEPlmV2rF4k3QoaR Ghs4RDQgNk2v+fGmvIVeIj/hkpIZ/hYUeYJ8C5j7cGl9wOheGELuZrw5wa8Pj7YaBL fjfbvK4ysnk+hgMTULEs73n/lsNIaXOQxbOnWs9yhqXNJXVOGZ3YSD7Tk6T3fbGK9e Eib9ren8AKV8w== From: cem@kernel.org To: hughd@google.com Cc: jack@suse.cz, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, djwong@kernel.org Subject: [PATCH V2 5/6] shmem: quota support Date: Thu, 20 Apr 2023 10:03:58 +0200 Message-Id: <20230420080359.2551150-6-cem@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230420080359.2551150-1-cem@kernel.org> References: <20230420080359.2551150-1-cem@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 4A3BCC0022 X-Rspamd-Server: rspam09 X-Stat-Signature: h3a5wf49psf6i7ihnpotcc44saytj5gx X-HE-Tag: 1681977856-272067 X-HE-Meta: U2FsdGVkX1/wQHTRTOdSy0GoQ//TV3HsP4k3bURo2yhP29KAtuM8Y+uSFABLtCHWrITb2sSrJteuFV7RRwKOsd/JGKh3Pt46phwNDFA+WadqGQQecMg6f7U9rryhTGoEeC8mkWP/CyXhMCI7sho2ncQC7TKpEe4qQCwthDky1ERPpkvmhO+urm6P9sPOQfoZb0R6tBV1Z83PceM2xY5kN8x/V4j6ZvZ29Mwcsn8l38qUPzvhyxW8zeEcR8puuluS+w4uVZd+NCS3cHGoAwzP84xFDa6WZPbk+j4CFQIF0w1aEbEZoYhAhZbj+Hr4sbY3aA8/rAj8NbKm4euRTs70S7MOQZMfW5Rz2Od6747ho5EwjybWWyJM+eB14zYs+kseI67EsNW7dV/SLdWsUr4Uj6qxgS4SnPz1GUEpzkczYTiIYo1rXXgcCVQ3ElX0zi2/aqnWIrXCUBXvrxbT7FE9xJLi2YYAXv5Fi0lUymfppwMXZ5Vb3uic3mDA5TPoTvv8qcyUzQvAbWLUd3vzcdJIP2LNNFQI3p8Dnwrfb4rnMyZkAQvrppGJFZ/f6TOyAf+p9jQ0lwZZ+DPDqomApDTm3FGJIb1Fq8Et1poMYabVA42GKjGpqGV9D9jAH4WnGYbRaVsNiFmEGzK+aWUVZTaJZFTR8otSfE+gMZd6KSsBLCfwduayeu60yb1IYkd9FTSDk0CeK0saBL3IbrJmLSNkGEkJWGVdmcoz/+n1bmZQiCdR5K6vAIpSiyrzmQVNGPtVLGaiHoWKVgm8BhxFqrCnAtpsbC21MPoNzkC96XNSHIB4cdhUjbpx3IJr0JjyimOBzK0hz26GhMaCApulc4iN2XmOHnPtLEfby7CVIfo4SYt6jR4qv9uWVJKLk5amsxo+AAsrVFWAed8jiYOmfexwPKnLz+9r9xH1stlYLOGrOdMbwjYuIvaDV+NmxgVALsNcmSXolRKmCrA6niUB/o9 8XpRvjFY aQFTesvB/cr1Fj8fXWg+ThUUf1VX2b2qcqMq9QpOqFAZMJg9SRsTKyG0ZAQ9eeXYTXUgtG+F6VIz2E0t8mydoOH+AdL9PzxEr2o1oTahhyU+depSaXJ6BwJocdFO9L9/GUXRGHtxZODxdODRoP652A4JHZJzbXQNo1RJjbH46cy5POuHksaUroeOiGJ/OYoRD4sYxAlPifYs/r0nyYu1U59QHuHddtv9tVso/obCroSBswuOtMjLbPZC30mIlg7+xYCuYDs6IZ5LMvl4Lnnr1dHuZ7pmSzvVFRlV9y4fnxNFEcEVT0LO8/ZCsbnkWnt7pr1OU 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: From: Lukas Czerner Now the basic infra-structure is in place, enable quota support for tmpfs. This offers user and group quotas to tmpfs (project quotas will be added later). Also, as other filesystems, the tmpfs quota is not supported within user namespaces yet, so idmapping is not translated. Signed-off-by: Lukas Czerner Signed-off-by: Carlos Maiolino Reviewed-by: Jan Kara --- V2: - remove SHMEM_MAXQUOTAS definition from this patch, as it has been added to a previous patch - Don't use assignments within conditions - add a call to is_quota_modification() within shmem_setattr() - Add a note to the documentation describing user namespaces are not supported. - Refactor shmem_get_inode, and add a new inlined version if TMPFS_QUOTA is not enabled. This prevents quota-specific helpers from being called when CONFIG_TMPFS_QUOTA is not set. Documentation/filesystems/tmpfs.rst | 15 +++ include/linux/shmem_fs.h | 8 ++ mm/shmem.c | 180 ++++++++++++++++++++++++++-- 3 files changed, 195 insertions(+), 8 deletions(-) diff --git a/Documentation/filesystems/tmpfs.rst b/Documentation/filesystems/tmpfs.rst index 0408c245785e3..1d4ef4f7cca7e 100644 --- a/Documentation/filesystems/tmpfs.rst +++ b/Documentation/filesystems/tmpfs.rst @@ -86,6 +86,21 @@ use up all the memory on the machine; but enhances the scalability of that instance in a system with many CPUs making intensive use of it. +tmpfs also supports quota with the following mount options + +======== ============================================================= +quota User and group quota accounting and enforcement is enabled on + the mount. Tmpfs is using hidden system quota files that are + initialized on mount. +usrquota User quota accounting and enforcement is enabled on the + mount. +grpquota Group quota accounting and enforcement is enabled on the + mount. +======== ============================================================= + +Note that tmpfs quotas do not support user namespaces so no uid/gid +translation is done if quotas are enable inside user namespaces. + tmpfs has a mount option to set the NUMA memory allocation policy for all files in that instance (if CONFIG_NUMA is enabled) - which can be adjusted on the fly via 'mount -o remount ...' diff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h index 92db49ebd5452..b8e421e349868 100644 --- a/include/linux/shmem_fs.h +++ b/include/linux/shmem_fs.h @@ -30,6 +30,9 @@ struct shmem_inode_info { atomic_t stop_eviction; /* hold when working on inode */ struct timespec64 i_crtime; /* file creation time */ unsigned int fsflags; /* flags for FS_IOC_[SG]ETFLAGS */ +#ifdef CONFIG_TMPFS_QUOTA + struct dquot *i_dquot[MAXQUOTAS]; +#endif struct inode vfs_inode; }; @@ -175,4 +178,9 @@ extern int shmem_mfill_atomic_pte(struct mm_struct *dst_mm, pmd_t *dst_pmd, #define SHMEM_QUOTA_MAX_SPC_LIMIT 0x7fffffffffffffffLL /* 2^63-1 */ #define SHMEM_QUOTA_MAX_INO_LIMIT 0x7fffffffffffffffLL +#ifdef CONFIG_TMPFS_QUOTA +extern const struct dquot_operations shmem_quota_operations; +extern struct quota_format_type shmem_quota_format; +#endif /* CONFIG_TMPFS_QUOTA */ + #endif diff --git a/mm/shmem.c b/mm/shmem.c index afa1985230166..dd9faf2c5c875 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -79,6 +79,7 @@ static struct vfsmount *shm_mnt; #include #include #include +#include #include @@ -116,10 +117,12 @@ struct shmem_options { bool full_inums; int huge; int seen; + unsigned short quota_types; #define SHMEM_SEEN_BLOCKS 1 #define SHMEM_SEEN_INODES 2 #define SHMEM_SEEN_HUGE 4 #define SHMEM_SEEN_INUMS 8 +#define SHMEM_SEEN_QUOTA 16 }; #ifdef CONFIG_TMPFS @@ -211,7 +214,16 @@ static inline int shmem_inode_acct_block(struct inode *inode, long pages) if (percpu_counter_compare(&sbinfo->used_blocks, sbinfo->max_blocks - pages) > 0) goto unacct; + + err = dquot_alloc_block_nodirty(inode, pages); + if (err) + goto unacct; + percpu_counter_add(&sbinfo->used_blocks, pages); + } else { + err = dquot_alloc_block_nodirty(inode, pages); + if (err) + goto unacct; } return 0; @@ -226,6 +238,8 @@ static inline void shmem_inode_unacct_blocks(struct inode *inode, long pages) struct shmem_inode_info *info = SHMEM_I(inode); struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); + dquot_free_block_nodirty(inode, pages); + if (sbinfo->max_blocks) percpu_counter_sub(&sbinfo->used_blocks, pages); shmem_unacct_blocks(info->flags, pages); @@ -254,6 +268,47 @@ bool vma_is_shmem(struct vm_area_struct *vma) static LIST_HEAD(shmem_swaplist); static DEFINE_MUTEX(shmem_swaplist_mutex); +#ifdef CONFIG_TMPFS_QUOTA + +static int shmem_enable_quotas(struct super_block *sb, + unsigned short quota_types) +{ + int type, err = 0; + + sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY; + for (type = 0; type < SHMEM_MAXQUOTAS; type++) { + if (!(quota_types & (1 << type))) + continue; + err = dquot_load_quota_sb(sb, type, QFMT_SHMEM, + DQUOT_USAGE_ENABLED | + DQUOT_LIMITS_ENABLED); + if (err) + goto out_err; + } + return 0; + +out_err: + pr_warn("tmpfs: failed to enable quota tracking (type=%d, err=%d)\n", + type, err); + for (type--; type >= 0; type--) + dquot_quota_off(sb, type); + return err; +} + +static void shmem_disable_quotas(struct super_block *sb) +{ + int type; + + for (type = 0; type < SHMEM_MAXQUOTAS; type++) + dquot_quota_off(sb, type); +} + +static struct dquot **shmem_get_dquots(struct inode *inode) +{ + return SHMEM_I(inode)->i_dquot; +} +#endif /* CONFIG_TMPFS_QUOTA */ + /* * shmem_reserve_inode() performs bookkeeping to reserve a shmem inode, and * produces a novel ino for the newly allocated inode. @@ -360,7 +415,6 @@ static void shmem_recalc_inode(struct inode *inode) freed = info->alloced - info->swapped - inode->i_mapping->nrpages; if (freed > 0) { info->alloced -= freed; - inode->i_blocks -= freed * BLOCKS_PER_PAGE; shmem_inode_unacct_blocks(inode, freed); } } @@ -378,7 +432,6 @@ bool shmem_charge(struct inode *inode, long pages) spin_lock_irqsave(&info->lock, flags); info->alloced += pages; - inode->i_blocks += pages * BLOCKS_PER_PAGE; shmem_recalc_inode(inode); spin_unlock_irqrestore(&info->lock, flags); @@ -394,7 +447,6 @@ void shmem_uncharge(struct inode *inode, long pages) spin_lock_irqsave(&info->lock, flags); info->alloced -= pages; - inode->i_blocks -= pages * BLOCKS_PER_PAGE; shmem_recalc_inode(inode); spin_unlock_irqrestore(&info->lock, flags); @@ -1133,6 +1185,21 @@ static int shmem_setattr(struct mnt_idmap *idmap, } } + if (is_quota_modification(idmap, inode, attr)) { + error = dquot_initialize(inode); + if (error) + return error; + } + + /* Transfer quota accounting */ + if (i_uid_needs_update(idmap, attr, inode) || + i_gid_needs_update(idmap, attr, inode)) { + error = dquot_transfer(idmap, inode, attr); + + if (error) + return error; + } + setattr_copy(idmap, inode, attr); if (attr->ia_valid & ATTR_MODE) error = posix_acl_chmod(idmap, dentry, inode->i_mode); @@ -1179,6 +1246,10 @@ static void shmem_evict_inode(struct inode *inode) WARN_ON(inode->i_blocks); shmem_free_inode(inode->i_sb); clear_inode(inode); +#ifdef CONFIG_TMPFS_QUOTA + dquot_free_inode(inode); + dquot_drop(inode); +#endif } static int shmem_find_swap_entries(struct address_space *mapping, @@ -1975,7 +2046,6 @@ static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index, spin_lock_irq(&info->lock); info->alloced += folio_nr_pages(folio); - inode->i_blocks += (blkcnt_t)BLOCKS_PER_PAGE << folio_order(folio); shmem_recalc_inode(inode); spin_unlock_irq(&info->lock); alloced = true; @@ -2346,9 +2416,10 @@ static void shmem_set_inode_flags(struct inode *inode, unsigned int fsflags) #define shmem_initxattrs NULL #endif -static struct inode *shmem_get_inode(struct mnt_idmap *idmap, struct super_block *sb, - struct inode *dir, umode_t mode, dev_t dev, - unsigned long flags) +static struct inode *__shmem_get_inode(struct mnt_idmap *idmap, + struct super_block *sb, + struct inode *dir, umode_t mode, + dev_t dev, unsigned long flags) { struct inode *inode; struct shmem_inode_info *info; @@ -2422,6 +2493,43 @@ static struct inode *shmem_get_inode(struct mnt_idmap *idmap, struct super_block return inode; } +#ifdef CONFIG_TMPFS_QUOTA +static struct inode *shmem_get_inode(struct mnt_idmap *idmap, + struct super_block *sb, struct inode *dir, + umode_t mode, dev_t dev, unsigned long flags) +{ + int err; + struct inode *inode; + + inode = __shmem_get_inode(idmap, sb, dir, mode, dev, flags); + if (IS_ERR(inode)) + return inode; + + err = dquot_initialize(inode); + if (err) + goto errout; + + err = dquot_alloc_inode(inode); + if (err) { + dquot_drop(inode); + goto errout; + } + return inode; + +errout: + inode->i_flags |= S_NOQUOTA; + iput(inode); + return ERR_PTR(err); +} +#else +static inline struct inode *shmem_get_inode(struct mnt_idmap *idmap, + struct super_block *sb, struct inode *dir, + umode_t mode, dev_t dev, unsigned long flags) +{ + return __shmem_get_inode(idmap, sb, dir, mode, dev, flags); +} +#endif /* CONFIG_TMPFS_QUOTA */ + #ifdef CONFIG_USERFAULTFD int shmem_mfill_atomic_pte(struct mm_struct *dst_mm, pmd_t *dst_pmd, @@ -2525,7 +2633,6 @@ int shmem_mfill_atomic_pte(struct mm_struct *dst_mm, spin_lock_irq(&info->lock); info->alloced++; - inode->i_blocks += BLOCKS_PER_PAGE; shmem_recalc_inode(inode); spin_unlock_irq(&info->lock); @@ -3375,6 +3482,7 @@ static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size) static const struct inode_operations shmem_short_symlink_operations = { .getattr = shmem_getattr, + .setattr = shmem_setattr, .get_link = simple_get_link, #ifdef CONFIG_TMPFS_XATTR .listxattr = shmem_listxattr, @@ -3383,6 +3491,7 @@ static const struct inode_operations shmem_short_symlink_operations = { static const struct inode_operations shmem_symlink_inode_operations = { .getattr = shmem_getattr, + .setattr = shmem_setattr, .get_link = shmem_get_link, #ifdef CONFIG_TMPFS_XATTR .listxattr = shmem_listxattr, @@ -3481,6 +3590,9 @@ enum shmem_param { Opt_uid, Opt_inode32, Opt_inode64, + Opt_quota, + Opt_usrquota, + Opt_grpquota, }; static const struct constant_table shmem_param_enums_huge[] = { @@ -3502,6 +3614,11 @@ const struct fs_parameter_spec shmem_fs_parameters[] = { fsparam_u32 ("uid", Opt_uid), fsparam_flag ("inode32", Opt_inode32), fsparam_flag ("inode64", Opt_inode64), +#ifdef CONFIG_TMPFS_QUOTA + fsparam_flag ("quota", Opt_quota), + fsparam_flag ("usrquota", Opt_usrquota), + fsparam_flag ("grpquota", Opt_grpquota), +#endif {} }; @@ -3585,6 +3702,18 @@ static int shmem_parse_one(struct fs_context *fc, struct fs_parameter *param) ctx->full_inums = true; ctx->seen |= SHMEM_SEEN_INUMS; break; + case Opt_quota: + ctx->seen |= SHMEM_SEEN_QUOTA; + ctx->quota_types |= (QTYPE_MASK_USR | QTYPE_MASK_GRP); + break; + case Opt_usrquota: + ctx->seen |= SHMEM_SEEN_QUOTA; + ctx->quota_types |= QTYPE_MASK_USR; + break; + case Opt_grpquota: + ctx->seen |= SHMEM_SEEN_QUOTA; + ctx->quota_types |= QTYPE_MASK_GRP; + break; } return 0; @@ -3684,6 +3813,12 @@ static int shmem_reconfigure(struct fs_context *fc) goto out; } + if (ctx->seen & SHMEM_SEEN_QUOTA && + !sb_any_quota_loaded(fc->root->d_sb)) { + err = "Cannot enable quota on remount"; + goto out; + } + if (ctx->seen & SHMEM_SEEN_HUGE) sbinfo->huge = ctx->huge; if (ctx->seen & SHMEM_SEEN_INUMS) @@ -3766,6 +3901,9 @@ static void shmem_put_super(struct super_block *sb) { struct shmem_sb_info *sbinfo = SHMEM_SB(sb); +#ifdef CONFIG_TMPFS_QUOTA + shmem_disable_quotas(sb); +#endif free_percpu(sbinfo->ino_batch); percpu_counter_destroy(&sbinfo->used_blocks); mpol_put(sbinfo->mpol); @@ -3844,6 +3982,17 @@ static int shmem_fill_super(struct super_block *sb, struct fs_context *fc) #endif uuid_gen(&sb->s_uuid); +#ifdef CONFIG_TMPFS_QUOTA + if (ctx->seen & SHMEM_SEEN_QUOTA) { + sb->dq_op = &shmem_quota_operations; + sb->s_qcop = &dquot_quotactl_sysfile_ops; + sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP; + + if (shmem_enable_quotas(sb, ctx->quota_types)) + goto failed; + } +#endif /* CONFIG_TMPFS_QUOTA */ + inode = shmem_get_inode(&nop_mnt_idmap, sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE); if (IS_ERR(inode)) { @@ -4019,6 +4168,9 @@ static const struct super_operations shmem_ops = { #ifdef CONFIG_TMPFS .statfs = shmem_statfs, .show_options = shmem_show_options, +#endif +#ifdef CONFIG_TMPFS_QUOTA + .get_dquots = shmem_get_dquots, #endif .evict_inode = shmem_evict_inode, .drop_inode = generic_delete_inode, @@ -4085,6 +4237,14 @@ void __init shmem_init(void) shmem_init_inodecache(); +#ifdef CONFIG_TMPFS_QUOTA + error = register_quota_format(&shmem_quota_format); + if (error < 0) { + pr_err("Could not register quota format\n"); + goto out3; + } +#endif + error = register_filesystem(&shmem_fs_type); if (error) { pr_err("Could not register tmpfs\n"); @@ -4109,6 +4269,10 @@ void __init shmem_init(void) out1: unregister_filesystem(&shmem_fs_type); out2: +#ifdef CONFIG_TMPFS_QUOTA + unregister_quota_format(&shmem_quota_format); +out3: +#endif shmem_destroy_inodecache(); shm_mnt = ERR_PTR(error); } From patchwork Thu Apr 20 08:03:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos Maiolino X-Patchwork-Id: 13218090 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 94877C77B78 for ; Thu, 20 Apr 2023 08:04:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 005B9280006; Thu, 20 Apr 2023 04:04:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ECFFA280001; Thu, 20 Apr 2023 04:04:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D226C280006; Thu, 20 Apr 2023 04:04:19 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id BE185280001 for ; Thu, 20 Apr 2023 04:04:19 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 9AD861C5BA1 for ; Thu, 20 Apr 2023 08:04:19 +0000 (UTC) X-FDA: 80701031838.01.AC96E34 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf01.hostedemail.com (Postfix) with ESMTP id AD6574001A for ; Thu, 20 Apr 2023 08:04:17 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=W0RK+FFo; spf=pass (imf01.hostedemail.com: domain of cem@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cem@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1681977857; 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=ffnp+h5LLcg+I18XeFX79KGGO+eLO6b4TgfzTylvGH4=; b=IYINzmCV7qxj9+A05Rg41UgZkhhgY9YBQmPDxhE5+0zwTjfXnaVqq2tcHGHw1SyVCqr1H7 ek7IZ9xIsgA0Lsw9QZaFpBKgFgzRvjiuxMONeiuGQwiY745HcsDUN8sN0j6x92WAtw2tSF /gX1K2c7TKTTCE+wxn/cb/eDpB+zV8s= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=W0RK+FFo; spf=pass (imf01.hostedemail.com: domain of cem@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cem@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681977857; a=rsa-sha256; cv=none; b=ccp3T74SlQUdmvLPge2fE56NcDfF4keRee5ReVPsU7n3l8O5YxVhxvH6FBJ13kQo1xJW7p VajCsRhXmB3UiXcliKn+KizHG+Je9gQ03yt1tpUq6FfOeovvT5RZxwXaiDcmznUTu9WGIk nSXpdskH0Xr81Kzh8/zG85aJbzjY25I= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id C6A86645CE; Thu, 20 Apr 2023 08:04:16 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CB3B0C433EF; Thu, 20 Apr 2023 08:04:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1681977856; bh=cGeeh3hspmdcnn/mWZolyKdxnix61gLL5flCCHmoybE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=W0RK+FFoNsOxMQe8O1aGq3Cdf04tPeacy+xr02slsRb2jKA79li/7WOHhiiCyha9K RvhKTp0/LUrNQabJTVWfqOW83H0Zq54WPvFI/6gXeiRh0b0ZQJHPwuNrdLZyjJe0MD 7v2Irm8kXS4qzrLYCWT8WF35VVV7XOkwyhwEQLDg4sk4osOmgLLXjqquwgQtNZ4RO/ xMteYxUDCckPmj1xJlWe4IKzAejMYQ3nVTdOIuy5pW5Yqng3KE9K5jawlZw1ZEO+g9 G1DOHyr2rJMfX89qvtLoFVA8nK2qDZ+v65qKQPnDHtQgGlEvsB/P7jUtYFnEkZppAT SiNYwiaOAVnIQ== From: cem@kernel.org To: hughd@google.com Cc: jack@suse.cz, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, djwong@kernel.org Subject: [PATCH 6/6] Add default quota limit mount options Date: Thu, 20 Apr 2023 10:03:59 +0200 Message-Id: <20230420080359.2551150-7-cem@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230420080359.2551150-1-cem@kernel.org> References: <20230420080359.2551150-1-cem@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: AD6574001A X-Rspam-User: X-Stat-Signature: sy134exoot7onjm7qfrx8rmfjcjmwzqb X-HE-Tag: 1681977857-18912 X-HE-Meta: U2FsdGVkX18MTJIbegEPtcvYXJ5gGAv9zDW5oI4Dj4tZjResmP2R/8XLkPIVqbfJ1KylI2WS7cGCxjhFNw86UJ7FjWYixbvEFy2zOO+3rwJ2Al1Fum0glORcVVF6MNudv5J29Rp4Ki0hYI/K/VmPEWRTeo+we8Rz/Asu2/FTMWWoZ9t9kud0596ETLIUAk3OJnUnhjja5pVQd9lOMPAwf5xnEtUXweghG1t63HBVqu67dGURoFMo1jw1z1QJ5Rcu7/f7IUUHOfwa7yzpRbtFsjf4jNVNUL3Z2GtYDydVTr64rJmFxXg4ySDvJ1GWbQ0mVMtISGJ4RXHu4cIOLamhqyf80QB1mf8mMUwbnvQeJs2BLgEmm54dVcwPyMw0eBCfOeJE9IPt8mpMoQ+h37y2K4AiYgrRmBuJFYTMVeH8eTjbspyGC8H9nFp6E6B6bN5I+ZkGKiBiSGuf4oLv0mlNOOk0Xc0h6jQzCqbBet6PBk9Cb2cmo39Z/Au2foJu7S+cKwcKEmiWLF/2GXslObL2uYsWCls4Jynsj6V2TKdvQEj2ap2FY67pmrfNz4L8Y3l6vN6k/hJVi9Mm4vbTnfBTQYu5IAjPte5HezTsH98QQ2zA5vq1CA0nYrbQinTCutInQ3pTwW4njuebxjdYgTOPzEDHNK7HQKD91VZgc6e6eGy7TFuDSPWppEKNU8isP+Xzl/SqDR8+5YRlru7uIYYAOveRNP+g5Yt1b8/iMl7VVs9qTBr9iOCoGohjggvg6Nfc53Sv1RqtfvVYzkHBil9vp8GLj1cH7CC0apqk75Oc2uis+LHNyA2UxoKWFhEnqaIDvtuT75dvZiG4kOpYQab+xVJAXY9i/88axtlFekm2xZypn8i4RrZJPsqcGBrbvSv2s/uc0GQlFvoq40yO2Zfnpeu8Rt/vh2qdDtC7YVvEw40J1QJQg8xLQyuynhnJtS93Luik/jKbaKog8rxaffU 7762gnRa SZRb+NCVBfaazQDsX2DwAc3rUCh1KKswC0Y/ASD5ZUq/dF63sSKG6wSYLmTuEMwjBkcvWKtc1eRpGubR1z/+jLARRtkf15CsThtZKWWd+U9XU8pZXmt2nwnSYivjGa9Z+JQhHNIXuetF1mM1Fexq85TYqmQUdXJbwzlaNtYy+XglN3ZsTNxHY00nn9Q+3e9TF+mhr2SoMixdsLW1wHrRIGRVuxGvFRt1L+5zmxBimi4DAuXI1hmmvqvbMJUAuRWgcqA+J+Hk9tx45kWHTTeIvtjzkVyHoJM7Huam4uGDOJiLwhxWJwcpkRLYivzA/KSWYIA7A 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: From: Lukas Czerner Allow system administrator to set default global quota limits at tmpfs mount time. Signed-off-by: Lukas Czerner Signed-off-by: Carlos Maiolino --- Documentation/filesystems/tmpfs.rst | 34 ++++++++++---- include/linux/shmem_fs.h | 8 ++++ mm/shmem.c | 69 +++++++++++++++++++++++++++++ mm/shmem_quota.c | 9 ++++ 4 files changed, 111 insertions(+), 9 deletions(-) diff --git a/Documentation/filesystems/tmpfs.rst b/Documentation/filesystems/tmpfs.rst index 1d4ef4f7cca7e..241c11f86cd73 100644 --- a/Documentation/filesystems/tmpfs.rst +++ b/Documentation/filesystems/tmpfs.rst @@ -88,15 +88,31 @@ that instance in a system with many CPUs making intensive use of it. tmpfs also supports quota with the following mount options -======== ============================================================= -quota User and group quota accounting and enforcement is enabled on - the mount. Tmpfs is using hidden system quota files that are - initialized on mount. -usrquota User quota accounting and enforcement is enabled on the - mount. -grpquota Group quota accounting and enforcement is enabled on the - mount. -======== ============================================================= +======================== ================================================= +quota User and group quota accounting and enforcement + is enabled on the mount. Tmpfs is using hidden + system quota files that are initialized on mount. +usrquota User quota accounting and enforcement is enabled + on the mount. +grpquota Group quota accounting and enforcement is enabled + on the mount. +usrquota_block_hardlimit Set global user quota block hard limit. +usrquota_inode_hardlimit Set global user quota inode hard limit. +grpquota_block_hardlimit Set global group quota block hard limit. +grpquota_inode_hardlimit Set global group quota inode hard limit. +======================== ================================================= + +None of the quota related mount options can be set or changed on remount. + +Quota limit parameters accept a suffix k, m or g for kilo, mega and giga +and can't be changed on remount. Default global quota limits are taking +effect for any and all user/group/project except root the first time the +quota entry for user/group/project id is being accessed - typically the +first time an inode with a particular id ownership is being created after +the mount. In other words, instead of the limits being initialized to zero, +they are initialized with the particular value provided with these mount +options. The limits can be changed for any user/group id at any time as it +normally can. Note that tmpfs quotas do not support user namespaces so no uid/gid translation is done if quotas are enable inside user namespaces. diff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h index b8e421e349868..8ca5e969f00fc 100644 --- a/include/linux/shmem_fs.h +++ b/include/linux/shmem_fs.h @@ -41,6 +41,13 @@ struct shmem_inode_info { (FS_IMMUTABLE_FL | FS_APPEND_FL | FS_NODUMP_FL | FS_NOATIME_FL) #define SHMEM_FL_INHERITED (FS_NODUMP_FL | FS_NOATIME_FL) +struct shmem_quota_limits { + qsize_t usrquota_bhardlimit; /* Default user quota block hard limit */ + qsize_t usrquota_ihardlimit; /* Default user quota inode hard limit */ + qsize_t grpquota_bhardlimit; /* Default group quota block hard limit */ + qsize_t grpquota_ihardlimit; /* Default group quota inode hard limit */ +}; + struct shmem_sb_info { unsigned long max_blocks; /* How many blocks are allowed */ struct percpu_counter used_blocks; /* How many are allocated */ @@ -58,6 +65,7 @@ struct shmem_sb_info { spinlock_t shrinklist_lock; /* Protects shrinklist */ struct list_head shrinklist; /* List of shinkable inodes */ unsigned long shrinklist_len; /* Length of shrinklist */ + struct shmem_quota_limits qlimits; /* Default quota limits */ }; static inline struct shmem_inode_info *SHMEM_I(struct inode *inode) diff --git a/mm/shmem.c b/mm/shmem.c index dd9faf2c5c875..2832ace81515f 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -118,6 +118,7 @@ struct shmem_options { int huge; int seen; unsigned short quota_types; + struct shmem_quota_limits qlimits; #define SHMEM_SEEN_BLOCKS 1 #define SHMEM_SEEN_INODES 2 #define SHMEM_SEEN_HUGE 4 @@ -3593,6 +3594,10 @@ enum shmem_param { Opt_quota, Opt_usrquota, Opt_grpquota, + Opt_usrquota_block_hardlimit, + Opt_usrquota_inode_hardlimit, + Opt_grpquota_block_hardlimit, + Opt_grpquota_inode_hardlimit, }; static const struct constant_table shmem_param_enums_huge[] = { @@ -3618,6 +3623,10 @@ const struct fs_parameter_spec shmem_fs_parameters[] = { fsparam_flag ("quota", Opt_quota), fsparam_flag ("usrquota", Opt_usrquota), fsparam_flag ("grpquota", Opt_grpquota), + fsparam_string("usrquota_block_hardlimit", Opt_usrquota_block_hardlimit), + fsparam_string("usrquota_inode_hardlimit", Opt_usrquota_inode_hardlimit), + fsparam_string("grpquota_block_hardlimit", Opt_grpquota_block_hardlimit), + fsparam_string("grpquota_inode_hardlimit", Opt_grpquota_inode_hardlimit), #endif {} }; @@ -3714,6 +3723,50 @@ static int shmem_parse_one(struct fs_context *fc, struct fs_parameter *param) ctx->seen |= SHMEM_SEEN_QUOTA; ctx->quota_types |= QTYPE_MASK_GRP; break; + case Opt_usrquota_block_hardlimit: + size = memparse(param->string, &rest); + if (*rest || !size) + goto bad_value; + if (size > SHMEM_QUOTA_MAX_SPC_LIMIT) + return invalfc(fc, + "User quota block hardlimit too large."); + ctx->qlimits.usrquota_bhardlimit = size; + ctx->seen |= SHMEM_SEEN_QUOTA; + ctx->quota_types |= QTYPE_MASK_USR; + break; + case Opt_grpquota_block_hardlimit: + size = memparse(param->string, &rest); + if (*rest || !size) + goto bad_value; + if (size > SHMEM_QUOTA_MAX_SPC_LIMIT) + return invalfc(fc, + "Group quota block hardlimit too large."); + ctx->qlimits.grpquota_bhardlimit = size; + ctx->seen |= SHMEM_SEEN_QUOTA; + ctx->quota_types |= QTYPE_MASK_GRP; + break; + case Opt_usrquota_inode_hardlimit: + size = memparse(param->string, &rest); + if (*rest || !size) + goto bad_value; + if (size > SHMEM_QUOTA_MAX_INO_LIMIT) + return invalfc(fc, + "User quota inode hardlimit too large."); + ctx->qlimits.usrquota_ihardlimit = size; + ctx->seen |= SHMEM_SEEN_QUOTA; + ctx->quota_types |= QTYPE_MASK_USR; + break; + case Opt_grpquota_inode_hardlimit: + size = memparse(param->string, &rest); + if (*rest || !size) + goto bad_value; + if (size > SHMEM_QUOTA_MAX_INO_LIMIT) + return invalfc(fc, + "Group quota inode hardlimit too large."); + ctx->qlimits.grpquota_ihardlimit = size; + ctx->seen |= SHMEM_SEEN_QUOTA; + ctx->quota_types |= QTYPE_MASK_GRP; + break; } return 0; @@ -3819,6 +3872,18 @@ static int shmem_reconfigure(struct fs_context *fc) goto out; } +#ifdef CONFIG_TMPFS_QUOTA +#define CHANGED_LIMIT(name) \ + (ctx->qlimits.name## hardlimit && \ + (ctx->qlimits.name## hardlimit != sbinfo->qlimits.name## hardlimit)) + + if (CHANGED_LIMIT(usrquota_b) || CHANGED_LIMIT(usrquota_i) || + CHANGED_LIMIT(grpquota_b) || CHANGED_LIMIT(grpquota_i)) { + err = "Cannot change global quota limit on remount"; + goto out; + } +#endif /* CONFIG_TMPFS_QUOTA */ + if (ctx->seen & SHMEM_SEEN_HUGE) sbinfo->huge = ctx->huge; if (ctx->seen & SHMEM_SEEN_INUMS) @@ -3988,6 +4053,10 @@ static int shmem_fill_super(struct super_block *sb, struct fs_context *fc) sb->s_qcop = &dquot_quotactl_sysfile_ops; sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP; + /* Copy the default limits from ctx into sbinfo */ + memcpy(&sbinfo->qlimits, &ctx->qlimits, + sizeof(struct shmem_quota_limits)); + if (shmem_enable_quotas(sb, ctx->quota_types)) goto failed; } diff --git a/mm/shmem_quota.c b/mm/shmem_quota.c index c0b531e2ef688..3cc53f2c35e2c 100644 --- a/mm/shmem_quota.c +++ b/mm/shmem_quota.c @@ -166,6 +166,7 @@ static int shmem_acquire_dquot(struct dquot *dquot) { struct mem_dqinfo *info = sb_dqinfo(dquot->dq_sb, dquot->dq_id.type); struct rb_node **n = &((struct rb_root *)info->dqi_priv)->rb_node; + struct shmem_sb_info *sbinfo = dquot->dq_sb->s_fs_info; struct rb_node *parent = NULL, *new_node = NULL; struct quota_id *new_entry, *entry; qid_t id = from_kqid(&init_user_ns, dquot->dq_id); @@ -195,6 +196,14 @@ static int shmem_acquire_dquot(struct dquot *dquot) } new_entry->id = id; + if (dquot->dq_id.type == USRQUOTA) { + new_entry->bhardlimit = sbinfo->qlimits.usrquota_bhardlimit; + new_entry->ihardlimit = sbinfo->qlimits.usrquota_ihardlimit; + } else if (dquot->dq_id.type == GRPQUOTA) { + new_entry->bhardlimit = sbinfo->qlimits.grpquota_bhardlimit; + new_entry->ihardlimit = sbinfo->qlimits.grpquota_ihardlimit; + } + new_node = &new_entry->node; rb_link_node(new_node, parent, n); rb_insert_color(new_node, (struct rb_root *)info->dqi_priv);