From patchwork Wed Apr 26 10:20:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos Maiolino X-Patchwork-Id: 13224371 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 84D48C77B7F for ; Wed, 26 Apr 2023 10:20:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1324C6B00B6; Wed, 26 Apr 2023 06:20:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0E4886B00B7; Wed, 26 Apr 2023 06:20:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DDA896B00B8; Wed, 26 Apr 2023 06:20: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 CF00D6B00B6 for ; Wed, 26 Apr 2023 06:20:19 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id A6DC314014D for ; Wed, 26 Apr 2023 10:20:19 +0000 (UTC) X-FDA: 80723147358.17.AD46274 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf29.hostedemail.com (Postfix) with ESMTP id DFFB012000D for ; Wed, 26 Apr 2023 10:20:17 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=oHk783SY; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf29.hostedemail.com: domain of cem@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cem@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1682504418; 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=a4+Hi3kvLGZ7wX/I/MLyA8qhrImfspY9oCA46YvbNIs=; b=JbTk6jvNMaIBNOmmUU0ItJ10KbQJnElnbAwghhHkoIK0S/RETMWSXYKxc6YQbV9n/LLYIW 4poE+X5RQIei29PffqlIggKkB9/8Dgo+Lpo5ANUzaEmdskRs5BzIS/hsW7y/DT/KpDNKNR jJKyZMNvUz/sTLh/xvWsJKZXsO8E48w= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=oHk783SY; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf29.hostedemail.com: domain of cem@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cem@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1682504418; a=rsa-sha256; cv=none; b=L647MppJwEI6PXjs/gMCrZ0MTh5hXuFwtIj9orZVusbZ1xwZ3IllKNkuxq+cVjqUH4Q9rM WUfoJMqHPuCaDSIN550AlyQa+hOEuM/GF9oTqEzEHeKMpzesv47mfkYPPz/qKKoLz0RmUW 8MwEJIPQB4cK8M30Bqg1soZJxcRcN1w= 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 01EFE63200; Wed, 26 Apr 2023 10:20:17 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3C341C4339B; Wed, 26 Apr 2023 10:20:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1682504416; bh=yQxEH8B7hcia0LdUCOjtWF8FC0yJn1kyiVdffv5361U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oHk783SYrc9I9mMYpULwfhg3/zQNX6ji12MXF4SlZYPJ10EhLGFxJLhSwEMataGU7 HJNDyqcqXCqG7wspV3IQcYqnQg2b2ogrcCu1yJG00TdUGvkVAyodSQ6piMvgzeViaa a2CjwQ+FeoEROBTsiT9kvFXWXC6R4NOEpHIhwjWeR/BINgSIsHJb9rK3Avd9bV4BYQ I/GcekEsiB7AjnsW+/9WrPB/6N2TjEEqumaOBGSgJ82lqlI2cYPE8kphXIUr/7gsNS N+eCSjDyQeagwVgN3hy/RwCQhWVALX1cSYwI7kTqn3RtNjntAOkJ8rP0iOiWD4M6XE Bp9gdo1MXUR8g== 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 1/6] shmem: make shmem_inode_acct_block() return error Date: Wed, 26 Apr 2023 12:20:03 +0200 Message-Id: <20230426102008.2930932-2-cem@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230426102008.2930932-1-cem@kernel.org> References: <20230426102008.2930932-1-cem@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: DFFB012000D X-Stat-Signature: fwic5enosk45xftia1p78bq9jjoyjr6e X-HE-Tag: 1682504417-178368 X-HE-Meta: U2FsdGVkX1/Ev/gVmcLc/1TGuTlJx3l18Arl77+5zDt83F9mH8rkLYVIigpkkn1qzX8qykx8HFniWrpSFsPpMgiZcioZFC/v00h1aGwlW9hLpXcTteRU0al/mhogLE85lnqhRwbHELKmgTdnZLylMB4GebZE1q/e+uGKaYGx3x7Naa/h/Wn+HPo2BFYyJyQiyXPAl0D2VQPEo2Jh4ZOLc3QxN3vhWEeTLZZBF5CfgDn2k5Ei/f3NpittDwVlq/j0RwhVPNnaJH7ELvX6isBpD3BgD+KTZOOrY54u/DbF8KPq79K9ieq7yg1Q1PnJpBHksu4UZMBVbyR8aMrwmosiCOrrojR+aOWZFyw0JhyUB91+zIHY2dXHwIHlfAW+x2KNTvTmKd8W393+G/ouaXYaiOoiIMfo7AbAabezoQ08OqlefPy+7K/RF9BFIxfTshpvSmP7d2Rc1klR97xk0NqKAvvZZhXjn2/ZEP468s8kObBbFwaT4TA/mWvbXCxU4PeMerjmC1c+Vj+d5a2Tvv6w3Qi4VVvr0i+Gs5Nx5oxIr4KMpvFc9DWffpnizcKf/DPfgeiMp7tPJRovvbpBc5SRGq2LwxAb72ykY6bLNaCSEju3XXABGCLj3eVx7Cc/eA4gd3nuN3KrAZM5d6QOvBiyorprUyJcp5jzoNuGh4kp2TYyqID9fssLcUs6G5+AH9IKtiqAwkj7xeau7dxb2v91Z6DsUK6NQibDxqKAYePKdfPtLK8gdCskiueY/QdmEOlVa9qD3vebyybFTHv9rrEJ3/LX9Haebt3vAEsO1i8ziwgZjYATBI8WXmtZxphIz/6sfYQSJrkCfpNw8ZZjFFjKWjeEs0iqasMcYtEQMcMsnlltLkdKsidXZirbcHW8oxmK8B36DuWmLfdnhXhToaOhQgJkV4XPzLqo6AGXBUYq4PNdcfvqxECMfETcSu5ZCY2lJJ0wxrC6TqAuArOcQbG As/S6Acy mbjGkpiKKUkMOmUE7qyvo330eRgVYT5vXSdvoHAdy2C3uqBFV8c+FuT7AL2BjdQTyQHOmAWiBEeIHVSYuUffBTWzV44zyv1hOsVtwIjmRPABBVT+Zf/wTIkVijovGKIU53bT4Ye3sojkFspLyFO7yuDuPMhxe3fessWWAZwb+UdBCb70dXdvGR3gA0LSBLUBQsSv/4MBSElehYK/DI8IKrwYje5dq64AOx0iBYwREnwsZrdGTfWDx6rqRhRKkY6rnd+z7VAU6P+a3tIR5tUDFMwtkk+ERU5L27obRU5lTYCU5/QaNZrFUb6cUt02Vqjt2Peovkb+8vh8bc2jqhs5K3OfPfbM5Hr9EUknXrYVIuYSwX56TtMaZP8aiv030qaaNrfTG 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 --- 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 Wed Apr 26 10:20:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos Maiolino X-Patchwork-Id: 13224372 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 31E11C77B60 for ; Wed, 26 Apr 2023 10:20:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C08A26B00B7; Wed, 26 Apr 2023 06:20:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B6A4C6B00B8; Wed, 26 Apr 2023 06:20:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 995686B00B9; Wed, 26 Apr 2023 06:20:21 -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 8770D6B00B7 for ; Wed, 26 Apr 2023 06:20:21 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 20C1D120150 for ; Wed, 26 Apr 2023 10:20:21 +0000 (UTC) X-FDA: 80723147442.09.D1B55DE Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf06.hostedemail.com (Postfix) with ESMTP id 5DD93180010 for ; Wed, 26 Apr 2023 10:20:19 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="ckmSxZ/l"; spf=pass (imf06.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=1682504419; 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=fi266QtD7pncYx+Bdr5u2pfnyhH40cUtZh7HTIc1tVE=; b=ZyubY00Ocx0Zt0VSUIY5O4Le4+oTG9ZmVWYuE3fVQey6AaubkCm5BUZFQMNrq0LoCHA/xr Upo1bP/yqe9IxmzcUqdntwvZo0HhkDhSY2NgBQaX+H9snZjpUPOvAqIzzWQY+xTjGXNfuO 9VMpF+u1CWre2wgCjoHbV0h8xLVy3Os= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="ckmSxZ/l"; spf=pass (imf06.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=1682504419; a=rsa-sha256; cv=none; b=vs94H1rFsHGFn8ZToq9r9LQhdbtrChdMW30k0OXmhz5kYwsV8vqZmzSoK1jrYVxanyEP9t LUCFFUSnF/fCeSiOvK+J0XdiH/0tTs1fAtlPFAo1j/mIIbIiyt1kU4mYzvZoie2bxtiIsz HbcWFvypcVdm7K/346NG8t8JwNMoD/A= 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 8C22D62B5F; Wed, 26 Apr 2023 10:20:18 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CDEAAC433D2; Wed, 26 Apr 2023 10:20:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1682504418; bh=U5S+kRQj4gSwTfgUwNfPqcSXjoX9SHZQDONDtpUHvqo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ckmSxZ/l3kSiFerT2gtrmVWp8/BpmnHwwxOGuPm6FjCYUFZ+BDgzlyLXwKEa1Zqdi Mxo6ZrgyMA04y55Ca/BFFkRVHmpoUH9zj+cupuoPEwjwlKXRV4044uAZSb1ngTsH49 VeWB7Nne27L/ISqPY6qLYaStE918bvHdB/eS+TUSJAY95S2Rz7pyZ8+Bq682G5V0dM 6TsqTRRznXmE6bJp6e6TpKziAfmysracSiVCEv1Q7o/XFi6mLSt5QNMjdy5SULRCSG FNmF9GZohi1WmbJDM58sknCjt9c4rZlzhb1hN9mKwljhQEE7q+aPx/e8YCP6Vej+/8 aizeKYgQSbxDQ== 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 2/6] shmem: make shmem_get_inode() return ERR_PTR instead of NULL Date: Wed, 26 Apr 2023 12:20:04 +0200 Message-Id: <20230426102008.2930932-3-cem@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230426102008.2930932-1-cem@kernel.org> References: <20230426102008.2930932-1-cem@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 5DD93180010 X-Stat-Signature: i33omho3wd3urazeahttfqdwyi3e47ix X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1682504419-148993 X-HE-Meta: U2FsdGVkX1/2aH99lEFVMc0h5c8juDdSB+l5cs6rlo9Z6xxb34JgtH2NdbSxtVX8NyAXPKqvv5IA5DxPeFnD1/S5EtRTAAhlRhhnsTAjjZhdmOY0mGmj5JOqLgJhG8mxxEfe3NdaLh8FUHN8YfkC6DKNrkPx4jcfRiXe7HLWOxi1T1jw6LPXMtZi9Z8ybGviWoZYZjqTQPSbxrjLKUJD9htCcQH/vfy7f/jaDiQCa+8cENRKDlJGq5qT/23owzS8x9PYNjANKXX+vfSvAeeyDaszzjMZZR63R8S9xJvuJ4F5HE/5wxd0hkMRg0yDATzNPWj3rg4VfNSSJolgudenY++kPisqk1n14MJ4+h/c5+NFhTAG/mtPsOZvYs8NpNrEBx7dwzCgBJriqq7DdIWhw8jIKOUDMdY040SKoVQzcSbF2ws85scjA1SyYEMrSdCd9eGlpg2eprW2+W1y02/0EGSgO+C79Z3hOLc1BkM4/pudoIE/d9rci/lMIZOl14O0P2vJj5ccQFC7Ap9kZijr9Ww1uXweTDP8qJvD7qoDIy5eRZ6y7PCsNxyY3P/oqlG9YRpnI67aqs8ajIw91Z398N8GjO60D76rhnqQX+Czn5j7gAVOcSZNLQQ8fHy3K17E1Qij9dFoOCNBjrVqHENW/KMt1qJAtJtttRp9gMk44qbaz8Y2QQfexy+vD3x+hU514h0lNBxpCrfkH4/tS3TjNlVsdXOuwmAWEhBCQ1N1QFuQ6g9rRVImWv7iMM22UiL+68AQesZGdGg5NL5RoHJLdeHvLq3pHev667nZnreCj/jNBZqhw6qMxp/gi9/PjMpQa6/H3IouIM8ZGbQf3g1Aaj4Sc5TQAlNWMeihinraYrItZ5v4BvxJNfR9koVhqY4XFSrPO1iZkKGw5w9IyI0NfbiVzyClm2Tb53p3lEnUenihMd4fT2Toymm6W+r13fXGc6P4dPr78yetOiVAJ0Q 6rRZ1cer EzgmZcnslWcA3hrLkHOeFagktNi/w1I3Hl/wZzsRQ73/tpZaxHJYcRyQZWAUDufjrdGytQnLZnIfkDtIepOhmoDQPf8SAOHe+JwKZ0Irn8+mLWDfV4FYp7KPFSsjR3HQAmVsC8G/Tl9yOtuTVuiRJcccIc42oNWb4pg1TYD+QMcSdOqtZC1V+e+JJgnYrUwaf0ZZApqA2YmCUHYP6ix9Cvl5WzVOn7wqXbMJrt4fQB4FN5dV/yl4PZXRHy1Iy3vldQ33hIL+CMu0wKbfSzkasXA9PF9sYrHgU8p2Fg39iov+3Z0ccBGdyxXczpB0bZPFI8sqJBFtRg0wtDGXhoIbZfBC7wA== 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 Reviewed-by: Jan Kara --- mm/shmem.c | 206 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 116 insertions(+), 90 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index b1b3b826f6189..09a44f1b2c954 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 *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 Wed Apr 26 10:20:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos Maiolino X-Patchwork-Id: 13224373 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 9CB93C77B7F for ; Wed, 26 Apr 2023 10:20:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 048CB6B00B8; Wed, 26 Apr 2023 06:20:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F3AA36B00B9; Wed, 26 Apr 2023 06:20:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C2FDC6B00BA; Wed, 26 Apr 2023 06:20:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id B330F6B00B8 for ; Wed, 26 Apr 2023 06:20:22 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 8D0F0401B0 for ; Wed, 26 Apr 2023 10:20:22 +0000 (UTC) X-FDA: 80723147484.22.262D1F9 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf11.hostedemail.com (Postfix) with ESMTP id E14814001B for ; Wed, 26 Apr 2023 10:20:20 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=fQAOF4ij; spf=pass (imf11.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=1682504420; 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=7MRrBktFBuEq92wvtHH5xT3sCHKAyR+DATXhefC0CfaYp+cmiw1iaq10rjBsfk3R83DB7K /Z+K/Fma4y5sAl6hWSKVHmxlLXPRnKFT/8LyoKFjD+HtOq2w5zsQshivxFQjCATLVSQy76 WEtq6t1wCdPMkUT5hQQ7E69yLmITbuM= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=fQAOF4ij; spf=pass (imf11.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=1682504421; a=rsa-sha256; cv=none; b=4Dv5uiiqRmmpKI+5DE0VKfAHdmzGgNVE6zsuiji5zbk/n/0afpEiC9OatzMkZA/LrTPoie GpSZYY286vjKQms6GOe2+UFaQNhHIWs+gOhIztnbzU1ieiWQambv7BWCwIFDztWGex8Ojg pZh8pcL+8tRsca1Px585/r4yb1cXbBQ= 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 2E12C63200; Wed, 26 Apr 2023 10:20:20 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 69754C4339C; Wed, 26 Apr 2023 10:20:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1682504419; bh=0+8LxOKXGKCvqagfKq127FWqroGkUmeQVQasqMNW7Qw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fQAOF4ijN10Rs92SLGh+Nbw4EvoLumPTmPZT5tCintz8fYkrH7WKto9F/dpQya3EQ Sh1CEDBR3XjDlNrbhWdtOn4qb6lMaT9YNlZK/035mKjMotUadoXIqAe3th51WSnwsl lP+hez7gcRjvMJYaaSdB9ILygu/sSPYw55t7d2GkTw9Sil52hHLQjBWHguLbYQYxUR 4aWfUA1seR+9cU/nrULaCpoakoFpr4wxEMBqof0Pd2yP+2e+GEkmRnWv2YK76wMBPt NfZr9eT7AvRDszdK3UJmWAhyv6EOYB8jDkVMq3GYFBPLdgQXaISznDmywK9O7475GV nLJxOz0hYFXkw== 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: Wed, 26 Apr 2023 12:20:05 +0200 Message-Id: <20230426102008.2930932-4-cem@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230426102008.2930932-1-cem@kernel.org> References: <20230426102008.2930932-1-cem@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: E14814001B X-Rspam-User: X-Stat-Signature: 1tsshddq3xiurfrey96oknhr1qqw9u7b X-HE-Tag: 1682504420-309768 X-HE-Meta: U2FsdGVkX1+sU+YJQyNiSQNR+HC7ev9U9CoBW/PeZGuZrZmMf8aufBIUOKGTvytLkAlb8uPQaqq/RKdMpxAES03F135D6u5XSJhV2VksKa9VllIMXafNgFTAvFM1UkLUeKp3U4A9MwXBSBiWnBk50jV2c9QKdQb/Rm49yjtW2npCYJOpirD+t4NWHn2MXs2tvq63+LY8pLSE007+FbWHg2E4Z1c933cpwJsagYHbhEi3VhpVKyKyDjrWgmqJ3ug03lCFoe25/74HrCBjijtI4nWr+5O5HXrl8FjB4mMCRYUJDL7cwyScq5AHrr2UKKs4+j6SJhO6D1e9Shi+Ioj67KbN99v6QedxqBrY7CfX5gZ01ob6PQUepjKxaF45XNOmVf/Qx/RcdyuHKHCcXSZhBgIQ6e3jVQ60WmPMB47pgCp8TNJ9JwYUhh6OLShgO2RbZCGl+/WKKgSBAZSbsMDLC9ThkKzB/rW41PepUHR3K0XL2AEDdIwabeeRffwklXs5SfcRHd24gGecV9CRkrG5/pPx0gbh+ydyOj+97FU0HO6vDvQot7EVsYTuY+3+HYI0S618RaSmxu13Gm3+vYePHNUaKwJYtf5YTCHeYZLhE+DheSkU3lmnzksr7Up4XGTonURs9H/2RPt7Kt0+JoDh0uwjSBLYrUnMVAnaE3yoGPghEIe9ZVUyarUWANB/nWKWywOrzfzhgrHK3uoKHFF2wy4MZXygZUbW24+yPgf/8rJraS0O5IA4YIwEZfxSSOWU0RUx8J4lA7WqCy4QF53uQXdOTZWj2Kif4kQkpITrI1l5xTk5fLlR6YHz1KCiw/Z5IrPHM3byxa3qXM5/x3/PpEZRrjetMXhV6Nz6gqvC/fkT4gOynJXnNaR/e7FngH7YwBf/R0HYZNRTgc88z8Iyw2b4m+uDs5yEwdNVFIsNGGJAOVi6uNoxVyeYOpUkZLYGIcE9OnuZ1QCFgjp/kEH SqgrfHr/ Cyw2EnB8JW0vbHUmORJrjN+wkrpRFuFAABpKxiGgp2E542A3qCRuB6U9QhngRdwZLwIkieMn6HLAepOFSrT3kzr50Lij+qxGVtngKoTrpv5IVUoY1+g3GEcFyO+PyPyo5ZPQzX6DcjgtiFHZ3ZdwtPPg5H7b0rdn8ncQBbsJ0PGfPGcgp8TcEkwH1tUkHGFIFg3e6J+Kh5OLXqJxc/F5QyYDWWqK+VOiufbAEhShaP2P+74nRnD7ji17x6JIMK5v5xTfr9f+h44UGoao50ZbMvOLS0SMohsuaUceDi6iNCa1/3tLiNHZaV3ACIJJnh/4SRHguV9I/vCg9GC5A/mVy8xKBVA== 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 Wed Apr 26 10:20:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos Maiolino X-Patchwork-Id: 13224374 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 4422AC77B60 for ; Wed, 26 Apr 2023 10:20:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 48DED6B00B9; Wed, 26 Apr 2023 06:20:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 43E2F6B00BA; Wed, 26 Apr 2023 06:20:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2B9A56B00BB; Wed, 26 Apr 2023 06:20:24 -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 160006B00B9 for ; Wed, 26 Apr 2023 06:20:24 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id DECF81A0175 for ; Wed, 26 Apr 2023 10:20:23 +0000 (UTC) X-FDA: 80723147526.11.69802A7 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf06.hostedemail.com (Postfix) with ESMTP id 1BE7C180019 for ; Wed, 26 Apr 2023 10:20:21 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=RdwMwJKw; spf=pass (imf06.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=1682504422; 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=AGQ3m+cD857ELSKcquRn3D0XBGbXdhTI/1u0OLOTQK0=; b=StlwyN7bgOillM6P68QbvpPZ4nvzM5JGcrNnqSMiO7fZWF3s/9Nc1x/CqQESmkY6aoP0Ym HWXaX7qUDp0G3RtzMX9GOgHzVdVl2PEoL+HCQ3JzfASXlhyCnVJC1Gq+EMZDARk6USWMOU K7ubvFBpnOPhb+xwotfuKVOv/cQu1mc= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=RdwMwJKw; spf=pass (imf06.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=1682504422; a=rsa-sha256; cv=none; b=EKBJrzOTmUTufvWTgPlACyHHRbn2b67Sx8tR6SLmEREr+w0PdIvUqaOvcYbmdG3wSpDY0c /LQRz+6RfoQIph7NcLk+DNvLJ3JTeNOudy0FcEVfW3qJ14wvynFl0pv6Ql0+MoZU3cpRPT gk5atKkbqxjCJThwbkAW+Ko39MFjtgs= 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 B98B262B8C; Wed, 26 Apr 2023 10:20:21 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 04336C4339B; Wed, 26 Apr 2023 10:20:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1682504421; bh=Jz4T6S5Wia5LRzyrfW+bEjkytDVDTsytdSr6s8SlVjE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RdwMwJKwYSNBZr/WEUeWBw3g/2CfaUBm9eVxpsgbPwQVMRhN6ATe9RhaZFzPgwxI1 SxlGyE9fjHlTmos32Fhc+HDHYBSwSixfVKHT+mlyPl8tGaSRBp6IFQxwsCSWTZstZw 0xIQxOrEyZFWU7tkVHleDMoog+jzpmc0EsueBDzE9ARbwSDvwHBhKQT4LJOiHuqM0l dbSwpDFL33bhtl4/paxKTT61bx8zJnJAHPlcEOXZxmCp180AIJWGaCUV8OHXMnXhcl yheI57TT4f166AOsOKdeeG0NgOiiDcZ+rabsAkmX9rXjQeoFH4Jz+FiNyCE99JWe/C B+BeJrISJihHQ== 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 4/6] shmem: prepare shmem quota infrastructure Date: Wed, 26 Apr 2023 12:20:06 +0200 Message-Id: <20230426102008.2930932-5-cem@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230426102008.2930932-1-cem@kernel.org> References: <20230426102008.2930932-1-cem@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 1BE7C180019 X-Stat-Signature: wipe4d7knc66tct5pc4azzaf4o7efdj9 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1682504421-346328 X-HE-Meta: U2FsdGVkX1/M9cCYgHJ5UEY1IXJrKYmPlByAuBysortVAuKWWYw+id0+uV8pMKjz6TG7LuPLlRKmLS2s/F2M8QMtiQsiGLRvlMdIvjJyh/X4UWQ7d35NwJAHfZylpNJVfaW0p5kp0a3GncB2jVxKOQkCbpCPI6wTu9U53jy6o1RqZEP7CRqK5ZqlAXD7L1qFYzk9ZempFVrc3sL7/ePpf8A2PNGEerc17oqGHROZsaPcGQnK8SW4N/d9fFvDv4BrBigFAtMLqnCXjcXj19cWwU9oIKnbtnCRw8p510jD1As2naBiMAT8Lbh7DZ3vmALyqDL1nNvGkR5HZgbZFzQIxbdRJTteh8Ed0sKtvyZpHLadZ435wTb4AiLuxOshSs1FqDMNkdcE7lx+xcx9dGYLoKez/4JoE/aJZS0noxkCPV9qschGZmtDJMYB14VVbT74izGFMeV6RCmRx2edssAbJmnRNkvwi1+bBQP6Bm05weDREaPTLMODZPWM4m2aJ2GBGFkZz4N6wjGQRMYGl9tu3yLFL7PAv5bgXMvqhZNc9dNOm+8btRhwhSiM5mk4qgQpHJvs7dlxCoywruXjtJnNbXensp6p+CY4rQfxAR9PCW76oSgohiBsdlZeJhk7RLEfuyaYusXCO4oW0978EMGPMqKKXNERz7U7cOTCAMRU2oG1fHayms03M7sgYWaQhgRB6KQhGpvxjEuyvUFxDLRX5TqoVooK7F0njsAW1/vB07lLPa6mRxhtn/OibfzmRgOKYet2co7kEqfgPj46foMZhsf29yaatRtSa/MVPoMSyop4Xo6eCt3JrbRO1WVDa54uBrkDXQRKprkamG2edwVpxfx/JS57yQC5R4P2RcOghi83qgMT+LoB9VKJEyPwdpRmc5gYNsmNFuHjrWLtQZxsqncSKAvUzt0NFrdcDIvKTUnTVRr1Hhp9I+98W1eCvX5X4mkdiQjEVBU1g/yKpGl vzzdcoKt MVeSJdK8bf0gz2KcMl4DS2ZRiyNTxpdlU972fiSxuVqHC/voavpvCadUQlXZO1wMA/w9TsbVa2xx50GB7X+LqbHTcoDP9B2t2Fezm4/25RDoYCh4DZaaJ5spqi49YOCmxoIFYCmWtIt0L1mUFge00F+jGIy0Hp1raiBAQ1BvaVUKlfTP+CfqRyyHlQk73ggfd0y1k/1auCVq01JlxM7lSp/1ljYIQPchEGRexHoVkBnJ9qjxn6k2a5KIQ0DHkWwhlz/OqQFolgdgnin6OsW9+D30tOZCvGNVfO7AqJ67HmiLqLyY= 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 --- 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 Wed Apr 26 10:20:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos Maiolino X-Patchwork-Id: 13224375 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 17EE0C7618E for ; Wed, 26 Apr 2023 10:20:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 504E86B00BA; Wed, 26 Apr 2023 06:20:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 48E8D6B00BB; Wed, 26 Apr 2023 06:20:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 26A616B00BC; Wed, 26 Apr 2023 06:20:25 -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 0EA826B00BA for ; Wed, 26 Apr 2023 06:20:25 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id DCC2B140186 for ; Wed, 26 Apr 2023 10:20:24 +0000 (UTC) X-FDA: 80723147568.02.11E9688 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf11.hostedemail.com (Postfix) with ESMTP id 470E940016 for ; Wed, 26 Apr 2023 10:20:23 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="m1uaAS/P"; spf=pass (imf11.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=1682504423; 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=cfwFMnnVJucGE0ZrKZpcFuw5hXie3W/ZzhzjoroFODU=; b=yMauYnrQu0U0l8yjIYetGbe0N7JXQzo2jnbd/oM2lQxyRb4evC3+pVmsxFpmwAYaZkSP8y 2x5MV8CI3mhfEVyXluKMkBOAjPx+6Hvx/YxRghtrNDAtz71wiR8cPtkbwqzRPpxoK3jYch hpzhSkzaIIyBOIP+RR7kdA94fOQ7s5A= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="m1uaAS/P"; spf=pass (imf11.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=1682504423; a=rsa-sha256; cv=none; b=H+1niBDzEUVUx0vVw7JnGgmkOAdyt3G3NKV2ut4SwB0JBKXYCcjK3wKEv+hiFi/AQELUBt mEwUemKsGukutOBVf9xQzwGrVEpZZW6fSu5UurogP+Eedcu3QPCjb8NzaKwzmbHFxYJGqF Zl+4Kum0fSaZp42QXQehh6lBEKb23Sw= 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 EB23263523; Wed, 26 Apr 2023 10:20:22 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 955E9C433A0; Wed, 26 Apr 2023 10:20:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1682504422; bh=34BlOPmGOrFEgjuP0PoRcnDETBz4XOt6qPnyNe9xVuo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=m1uaAS/Pi3HvqGXZUM77oiRYyUEKeoNZH4A3mrrIC1grkXGCt5GCV/Iou7iLvnPSE RDXJ1c4f2Xu5/x0ZmNSKQ38XriZknp/wyxnoaSR1bTGqCMHJH2YV9XpXsjkEOvH/l2 38e9anw4OV1E8NDa1Drh485QoID7cPocwqFYwp76+lurS83aSTpu5uQsWynUuOBiGX cpzWnfUDPbghvhc6/wqfLGv7nP3d7aVZZlt3Zu1d0yhlVu8DexxZPld10GsFZVIMvk KZ7YvqUisrvGYmOy5aqJZlbprQj9Fzo3966Qv3YJz97ksgU6Mydd/u4E1GB4dMdW/M 5oXeoe49jSqMg== 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 5/6] shmem: quota support Date: Wed, 26 Apr 2023 12:20:07 +0200 Message-Id: <20230426102008.2930932-6-cem@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230426102008.2930932-1-cem@kernel.org> References: <20230426102008.2930932-1-cem@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 470E940016 X-Rspam-User: X-Stat-Signature: b4em8dtpnuyi7t7djkkg9p53snz85k6w X-HE-Tag: 1682504423-536695 X-HE-Meta: U2FsdGVkX18FM4RqVEotBqq89wMOHKKCcxirUOQz6vCrCsAjkAAUEKuMkAb6DE+yw+HS+wjBkzb60C4uOHe7o2jqQgc6D6cbunP5SWeoT17NTk0xZ5dVlZtrQIF84y43mEoAhb7q2u2KdKdriHV1RNYlw564XMUfb0kN5mDPLvEfPOuxX2dm3X7dipYPu4FtQC5O3+eRnTyEm/Lz7tR0mX3EDOd1aljt3EeuZOihX9UTvL3XK/vCpPwxwU4pRi8C/0nIAImEmI5yHEPYNtcS9A5mEZ+cTkypkk4Lz6Fn5Ojq4+xhZ2HICi4Tl0Yp6rYUAfQyovsJitaozjRZRVL2fJFIhXmjwZnjF+n504hRWhdXcu05wcIvRLelgdXRpPFS7O7RB3HBTricdQhieLxPkccGxRQJuf+i6ieLQruSAykDZDA/h9aZJcKSzc780rtlewVBsFF8JgeyZvlf1pBghI3mmEFP1X7N0jbA2ihSty6piQ1iB0IamY/gSgLPYRU+jihLI06/mUEN7KGusMv3a39DzuiXHWgauMaMRuSqkevU8RqrlvI0XWcUcWOAmPL9Zw5nDR9wFm1VDz+uFj36j8pI2R8pNZU/XO8YLvscR3RRwex4gQoCG/diYLFn5grkkkLAFKue7v7tI54+40eIUwXR29jtT2bIgDTlw+CedywqHGH58rvYijfHBehcD4Z1NMrW82VNukFGWYlyxj/LS69ICFxMsXfldxrfRv9ONKaCuIOZsGLrHDElPWkFlf2MNq9e5/DloRvaPqoOH8c8B0/fYhgosS3OuLsyf+y38clB6x98GfoV9qi5WcpuxEredeTJZykW8PcnNjuSBNt9M84LnHZK5M/nE0T1GXRlW/Uuv6fldrUpf5TlQ+XyLoDv9GJacrB1MIG3E4J+0nPnvYYez+1zoO0vHj9UibBg8TP5tPbcxYWfu+dhicxRxpEb7WFdLS4yZGh8RLI+cIs sr98NZoC 90nV3x3c0QJmx9+jLh3t7qeqDFyi5xRsylfoxBUQZ14E86lLB3H+b1x+df8WyIQC0exdNQeX2ouHhyUaJ+hvoICDBrNxN4o+O6wwFJTQhShiGF9zYv4M+7pTR1LzQOO6x4OTGTfalhDGkHdMCCZYqTRN9DzpYYPwbzNqAdqzZBPgrXYY3k6ZI4rdmF50aD1VmjSWm5T/iyT3T0j9V2o6LV5dEPx494h29nwWm9gGDn9DUUaOYPHAX3YQZo/Kl93QFl/vXV3JB5aitxJpG5woakpEg0J/S/bVBNCFvZ6x08jegzjI= 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 --- 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..595e607274afb 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 enabled 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 09a44f1b2c954..623d258af39f8 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 Wed Apr 26 10:20:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos Maiolino X-Patchwork-Id: 13224376 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 D5A4EC77B78 for ; Wed, 26 Apr 2023 10:20:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1D13E6B00BB; Wed, 26 Apr 2023 06:20:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 15A2C6B00BC; Wed, 26 Apr 2023 06:20:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F14CE6B00BD; Wed, 26 Apr 2023 06:20:27 -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 DA3F16B00BB for ; Wed, 26 Apr 2023 06:20:27 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 9AFB7C0242 for ; Wed, 26 Apr 2023 10:20:27 +0000 (UTC) X-FDA: 80723147694.10.B4BD972 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf02.hostedemail.com (Postfix) with ESMTP id E086680006 for ; Wed, 26 Apr 2023 10:20:25 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=tgoTSov1; spf=pass (imf02.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=1682504426; 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=i29nbLTD7VUPnvYBUWlCyjQJ7Rua+7re6Ffsnvy7qbU=; b=5lkHHp3EVyX+wCrNEv1nRRxqxv/tGT7aqM85n0YAu1F/itTt5VjClwBcN0QGCkSSGiVYUS nDlf4e9wkuPIy0uZttWWyNFMy3OOedyDdasAgX+qaTAy6sA5bs0FrPbzo2ukGKXC3CwlKX NS6oz5eXdfZRQlephlk4AqeodpxKTZg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1682504426; a=rsa-sha256; cv=none; b=R4euUdRk+Ery3yasrqoWSyquESGFFclLzrKRNZ2R3xaJn+Md2POJR7M2fESkf3r1FsgzLG AAcwAnTSasmtYJ8Np9yE29YaKDcp3Hqn5hnbnz8cMs6j2IPlrIzBqP5c2F0seooXNFuLjn nkkFNdcGFJ8tlLCDZIQolIREH1G2vw8= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=tgoTSov1; spf=pass (imf02.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 EA86D634D4; Wed, 26 Apr 2023 10:20:24 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 32DE3C4339B; Wed, 26 Apr 2023 10:20:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1682504424; bh=sTQFN1wAbKVSo99SrbidiVoxbcHbXA+5l/p0lvKIk1o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tgoTSov1mXjb7MsizAum60HLX/TOT8KM5xQuWsVVlAkdvQs2RqC/0GvQyi7tmtshc wOEE+hsQa/5VBr2Lz1LM1ewEh/emma1LXTSZxGtcnlIA9Hi9LUFvAyenMyQD82NJSj r63KdOQyk37weFVxvaO3YZWNG/bM0BDMh7obf+n/rYL5DIK2s8GurAxCz00wShXmSq lLl2zIwmGPey4UbyV+qchGFIdJPnTiQsER4q3B14MHkoEaqEnDGRXsTv4PClUHmgok FO6yF7rdCxmOYZRtHjkO0WOORG6NnBqm9/Mn8dN+snLTda8Htf+7KRTPDYQur2fNDW MPqYaQ7uWD4rQ== 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: Wed, 26 Apr 2023 12:20:08 +0200 Message-Id: <20230426102008.2930932-7-cem@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230426102008.2930932-1-cem@kernel.org> References: <20230426102008.2930932-1-cem@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: E086680006 X-Rspamd-Server: rspam09 X-Stat-Signature: w9psbbozux5wabdtonir8i8p9f1jz8d5 X-HE-Tag: 1682504425-689114 X-HE-Meta: U2FsdGVkX1/vEBbkY88h/z6fSxRRJTHxviMT00d0RBtcq70hC7kgUYoH+FTfG9/zC18KfuSKYhDjLtPfmHtjOSu0OaPtTj82GzYRo17Ah+E2X5hVbzmQyBx38PPVTWgcugOvlxKkrk3KoaLk1OJyrbJMZezsp3KnDZ0otco6lq4BZLsyyIXDPGr1rFXVA5vJDAzMInTYz2+s9p9JCu3FKmmTs7q+UIM8pflicYjrdWinL1/sHAktUg4sTs0kuNaCor/MJRh6orOccAqeriLfJEcJIqnA0VjTcTlaH7axR+ODquQEUmC1hs7tR0h5AieIzOqG5E+xUFbwrBVFbaTqhFOekSkiZfqa0yR1GQpMraJStrRTXdcpGY9l99qooyPQ2qYmY0GG1yETXt2v6ya3gilST2ldKEtyx3djik71rI0wcu9lPNWXq/djwU9WltrvPJvP+651uaxNucnTlXtp++J5QHnxCM7DJfUji8fMmNnYH8txDbQm9FQoUdfPEdt7gLZw9vl+7JfGkInAEs5c2td3A/wspEhch++1LgHt7Fzgn95KLhngbfpqNdNPf2jDy5HQfvyqi8cR4np+PuU3oKHZk9+aIvt9KkaEi1sMN4S1Fr340oY2IvF+TZIO4i5LZFgGhfupW+OZUV4XF4LM+CNuhF6Wnpee7rs1LtWsNaAZgFQXAYO+guHLyxdpq6MhieLwzTpnOSVFKuJ8u3ZnjqIGwkHVQt11Z3ULlXevZItW8aLyucBFseVGijuieS+W85nR/iQ7d0RIHONgaKjp/EjzNbtPHHqCF67xpDhEGB0HryIg8v1r83iKKl8S/v2WhITjYo/WGfU2ZFyZaEwWF0F5UCE5LueIkC7xqe2xPkIC9PmK1WF4rFOQ/q4u8kWr8InYRuOBPKPYTwej8OguTPXcbF/6vRyCvm6O/cn6F2brKNY5Cn59qkX0F43VtSPrRGtBwkFDmLnxFbzMb8l aBCIBz5U wFjWCpQVh+Zqp4w5LRpVGckE1G0zHc0ZhjVx9cET2ygv7DOjKIFz6oq8yyMf5xn85u8ZDfPp5SHkopk0s11C7IpNzehd9NqI+bTlpWfo6lOtoHpnJjfuzfjq7qR2Lu8MqcwXnjZ6qjmGmvG5WHf3T/PZY08BURPC2tsIXaBCWVi8x8Hn6DUrdekmpnZzrCBnnWTYZxs5exXrob5KdhtaJJukXNGBV05YOz/t5MjDMWNyNF0kykNe9Tp8EQe+0HgM4zh9Y5v+eY3PanXi1kHlCIn1aRRlMNAxgjDPVXroPdH7+W3s= 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 Reviewed-by: Jan Kara --- Documentation/filesystems/tmpfs.rst | 34 +++++++++++----- include/linux/shmem_fs.h | 8 ++++ mm/shmem.c | 61 +++++++++++++++++++++++++++++ mm/shmem_quota.c | 34 +++++++++++++++- 4 files changed, 127 insertions(+), 10 deletions(-) diff --git a/Documentation/filesystems/tmpfs.rst b/Documentation/filesystems/tmpfs.rst index 595e607274afb..0c7bfba34baa6 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 they +normally can be. Note that tmpfs quotas do not support user namespaces so no uid/gid translation is done if quotas are enabled 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 623d258af39f8..24bcb374ad27b 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,42 @@ 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; + 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; + 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; + 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; + break; } return 0; @@ -3819,6 +3864,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 +4045,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..e349c0901bced 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); @@ -224,6 +233,29 @@ static int shmem_acquire_dquot(struct dquot *dquot) return ret; } +static bool shmem_is_empty_dquot(struct dquot *dquot) +{ + struct shmem_sb_info *sbinfo = dquot->dq_sb->s_fs_info; + qsize_t bhardlimit; + qsize_t ihardlimit; + + if (dquot->dq_id.type == USRQUOTA) { + bhardlimit = sbinfo->qlimits.usrquota_bhardlimit; + ihardlimit = sbinfo->qlimits.usrquota_ihardlimit; + } else if (dquot->dq_id.type == GRPQUOTA) { + bhardlimit = sbinfo->qlimits.grpquota_bhardlimit; + ihardlimit = sbinfo->qlimits.grpquota_ihardlimit; + } + + if (test_bit(DQ_FAKE_B, &dquot->dq_flags) || + (dquot->dq_dqb.dqb_curspace == 0 && + dquot->dq_dqb.dqb_curinodes == 0 && + dquot->dq_dqb.dqb_bhardlimit == bhardlimit && + dquot->dq_dqb.dqb_ihardlimit == ihardlimit)) + return true; + + return false; +} /* * 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 @@ -261,7 +293,7 @@ static int shmem_release_dquot(struct dquot *dquot) return -ENOENT; found: - if (test_bit(DQ_FAKE_B, &dquot->dq_flags)) { + if (shmem_is_empty_dquot(dquot)) { /* Remove entry from the tree */ rb_erase(&entry->node, info->dqi_priv); kfree(entry);