From patchwork Thu Jul 13 13:48:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos Maiolino X-Patchwork-Id: 13312234 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 8EFF0C001B0 for ; Thu, 13 Jul 2023 13:49:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 089E890000D; Thu, 13 Jul 2023 09:49:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 039E690000B; Thu, 13 Jul 2023 09:49:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E447B90000D; Thu, 13 Jul 2023 09:49:04 -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 D73A190000B for ; Thu, 13 Jul 2023 09:49:04 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id A8E28402A9 for ; Thu, 13 Jul 2023 13:49:04 +0000 (UTC) X-FDA: 81006719808.05.7354CC1 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf23.hostedemail.com (Postfix) with ESMTP id EBA25140023 for ; Thu, 13 Jul 2023 13:49:01 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Rs1ObEZG; spf=pass (imf23.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=1689256142; 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=XNS0pZWYtiz3eLlRvQaHMyf1ULXVatSDvn1Y8OwTP3s=; b=F0QQGUbd4ucpzYFQde6p1AYY904UXRMqLt/LdBzX8Q+wl+BZjTXf8wftdjrtrbXaQEKbnS ciOFwX2dNngnmH/bgcxWJSoRU3NHwfpjKa6g2QykwyWOFLFrq/LzsGc8YurZsSpeMfeCcd Trk2leMM6Gda4GmmlrkpJtTay/UL6jM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1689256142; a=rsa-sha256; cv=none; b=a9y6xUNnmJFUO5KyLVz31iN6GI5KxMoqqmOXECAqp1daguQidTl5zbmrQjNCtztZZDCGAh 232KV4X2BGellGhrcQRf5dpwIXeUE016yrDczBBH9o22VS7JMQ5jz2btvgWGtvTzkosYDU 2mnG05IiqiFPXnavEQ2ZzMRfmCZhqbw= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Rs1ObEZG; spf=pass (imf23.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.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id D30D16153D; Thu, 13 Jul 2023 13:49:00 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id ECD39C433C9; Thu, 13 Jul 2023 13:48:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1689256140; bh=yJe4lhnTjof59vWoLkGO8Qqi3dVE6gVAeA03pQuGI1M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Rs1ObEZGwNKqvhMlpwjar7f75Lki51FGjVqCmYvs2qf9HPDqfPZbBD+gMa8rXM0vp tEnaAdW8rTV88g0jlrKy8uHObcWL/auXaMhWjP9Gl9wTxSxQbah1NbxSXw+Oso1U6K v2PwJU/lW5PlSE7gOtIH3tlAwsxb7KDVvyxe5oqFQ2TBZAhJcQNxAEdAO5x9WG8u/j pXw60UM7mT+51vi02bGPEJf+BeOhKgDEg29BR9z6ya1kaRvHLzLRmSfiA1Y45SGN0D gd3xSzqBtEHHjYQc7cw8Q2gtFvQFxPHxV8eZzTMmrW0KkEvonsTUjKgPf0bjUHtQql zd2BVzpqWvS6A== From: cem@kernel.org To: linux-fsdevel@vger.kernel.org Cc: jack@suse.cz, akpm@linux-foundation.org, viro@zeniv.linux.org.uk, linux-mm@kvack.org, djwong@kernel.org, hughd@google.com, brauner@kernel.org, mcgrof@kernel.org Subject: [PATCH 1/6] shmem: make shmem_inode_acct_block() return error Date: Thu, 13 Jul 2023 15:48:43 +0200 Message-Id: <20230713134848.249779-2-cem@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230713134848.249779-1-cem@kernel.org> References: <20230713134848.249779-1-cem@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: EBA25140023 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: mus9gazxy5q4aadjyzuumi3y1wfc9dna X-HE-Tag: 1689256141-40184 X-HE-Meta: U2FsdGVkX19TmFP3bkIVYdy8e9F1TcLGT9F7/i+YMrBRkanuDU/YlLfHeyuS7SGLSQwV9LjDKg+HiG04S8LQZZURAuq/Y1T7FtwYQdnahbWwa+nGAKAojXEcVxvpjB8FYjsNDG06H6FnqawyXmPtHroVtzHuF1xdEE2p4rlx1B/Abe8LyN2Yl7Ad+WdF/JcwO8QE+HpdtyVmGmy1qyCdE+w1x7Wrn6BH2rugOXrMMToGkzgRFd7xRud783sZlXyp5/5TiVbBdWSh6E19/tGudVoGQf2zriw7o5O3ltesiAL+rVfE7Nunhsq71kbQtNVTK33dxVIdZfxjgmg2lFUfSgj1bX0gTvA+QBDcUkrWUhCkqjQSSnt7dGv9yTyxDdrIwjziE3Owk7w6JdxNdKEsRUFeXurEE96dO/os+nQW8vcHtsPmX4MYe0eYvmJn4/9OJH3456sj4tVGZkt/+l1Jrbymx9KogYsMCcJ9OKSLqzvzbPqaoaTaRL5HYAB8CLhzslufiACHOiv0+uqPelIgRhGnsjeqrvgHO2DBrcKKrCBVHNIrWC5OS+V6Wovs1Kkoce886DXToBXCAu1dPEh4fVa+UbJ+lCQX3JNqj0WjctYck8wG2ADEL0UBmxfW5dXXDiiJxic71VEEQqVe50Zojq9zESfHBRRY5WYAGzJ2resGy7iYryHh0wOEVEXfhnPtbW0T4tCFJMi3mE6CR034p+VsekgeaTUgc/s+zTEU6085QW/gpaggSAeRukRMfmaE/n1yjoEHmYvWs8wSCtx79eGdiGDUykjomFUaV+4cPnnrPtxx6l6nMfV+jDUg6FV/LNXak6Mo+eR22sq/BG/RyDY6+zBOoX810hU3C2ThGbb8za+Rh5VQgQvTSUdGT1cLxNshLX2eD207/NIdMBCcT6T81+phzEY1gz4ctkYY0xLIwVXk8ieaOc4eZnZvCVzAlPbOfDBbR3yy9h9vRp3 Ujf2JPdx wWySgUMyQdMogyJSgcHlNq6kaYXE9tVSLFrZCIkDKxogE6hiJXEFw6qaj3uE/XNntR/KEUTFncDGOrJU+EuKCph1Vvu9MNyGHr0qGzYjzjKmQJ+07wpV2pIHlFi63NS1CE9w1mIKR+olIvDa3UoXODJTJiy4XgPv0KFqtrOODTcq/CPYM29/j7Zm13104lVxoUL9Y5UntH/RbQisfGM3n9urn8MgMyQ0POQ7TmkTKHYDuivpqZS/tqp9RDoK5CklZKXvOSSx4qr7EDd3LZgz6NkuCqKwF7gOH1/Jl 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 2f2e0e618072..51d17655a6e1 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -199,13 +199,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, @@ -214,11 +215,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) @@ -370,7 +371,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 */ @@ -1588,13 +1589,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) @@ -2445,7 +2447,7 @@ int shmem_mfill_atomic_pte(pmd_t *dst_pmd, 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 Jul 13 13:48:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos Maiolino X-Patchwork-Id: 13312235 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 97D90C001DF for ; Thu, 13 Jul 2023 13:49:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3493B90000E; Thu, 13 Jul 2023 09:49:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2F92690000B; Thu, 13 Jul 2023 09:49:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 19C3690000E; Thu, 13 Jul 2023 09:49:07 -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 06F8890000B for ; Thu, 13 Jul 2023 09:49:07 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id B8BDC40291 for ; Thu, 13 Jul 2023 13:49:06 +0000 (UTC) X-FDA: 81006719892.06.19A78BE Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf25.hostedemail.com (Postfix) with ESMTP id A0A82A0007 for ; Thu, 13 Jul 2023 13:49:04 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=KQ8f35bY; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf25.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=1689256144; 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=B3GO16526NHYnwj0fDZzCIg6e3HlOBvlXnXqZYtZHkg=; b=UT+482dwJ5S5PiIBtlj1odsF4+sL9x8BcA8el+x6hbb46S+o22MSmK1l4TqCLmoep27H1S RCjaGZKWzZmsf/BJmG9Wq2WyuID0ZBKuXrdiI+ML3cbXY2o25f7G5wEX9FkPXhlsZbVHsm QLgBctw+Kt30HMPr0Ameuba8XrIb5ZU= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=KQ8f35bY; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf25.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=1689256144; a=rsa-sha256; cv=none; b=pHAD5scUIM0xmVZ135LCBtYkh1pINFfxhu3k22tPIW5SQoGFKR4dfaLC98NAmTKJWVoDJZ hW67R9lygPn41D85qVnPd+54IUV2lXf+bxS/H78AornEvBOLJq2ui7dH8rVzolDddLvC3J TNGqkw9cJQxA3r0hjYT9lE/6GWaBVxU= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id C8BD061540; Thu, 13 Jul 2023 13:49:03 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E4252C433CC; Thu, 13 Jul 2023 13:49:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1689256143; bh=SD6/04+QDQ+xNYYAuNSqvTPEJG0+INw9+cuke4AzLi8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KQ8f35bYHKkYu0mik9CumCwSfV5X5EZBrP+3WYN7Y5d5qlVoPFd6DTq2rqYmCIHJP YnKAH+gEZ8BOLrew05kF+T1X/qCKEzAvi34BTi1oahvcmlGEYei9ALm2OAI0Uu9i8p riaxn1XhA34zOc2i7Bx1DEQGtp9vskMaFj02Bz5sd1SKq8pxmlZTvPfUAJWYVUn5rQ ComiSy7OceuOLHczk4IWMwOgW3xHTSFVotjkCq1lE7ElRg2ev/tREthozCuQLX6vTU XtnYRKQXy4AgtxX0UhZBH8ZDU2ugP+wQodLYuFkKr+bUDrtUhqLfJ0U2EQaViLFnfV pjre8Is3SuHuQ== From: cem@kernel.org To: linux-fsdevel@vger.kernel.org Cc: jack@suse.cz, akpm@linux-foundation.org, viro@zeniv.linux.org.uk, linux-mm@kvack.org, djwong@kernel.org, hughd@google.com, brauner@kernel.org, mcgrof@kernel.org Subject: [PATCH 2/6] shmem: make shmem_get_inode() return ERR_PTR instead of NULL Date: Thu, 13 Jul 2023 15:48:44 +0200 Message-Id: <20230713134848.249779-3-cem@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230713134848.249779-1-cem@kernel.org> References: <20230713134848.249779-1-cem@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: A0A82A0007 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: q5muwhnuyrt7iczmckyimkp11eguqmn9 X-HE-Tag: 1689256144-87484 X-HE-Meta: U2FsdGVkX199MVxQ3c6jlfFh5sodQTIkhaydC6xOhYsoi0sqdlzqn5Xg3WCBlSwhoGlD3aIZNMER8b6VAhseD8/lSancCniJQjiF0lb1xFrlQHYBV9W64IBsbpkxr9pCPBK+zXqOzS2YSiC9RzzfpGbCL1Sf3+hYqA5s52C/pYOYPTPL4ikbeUmr8TecOK754nkC/TqGej3BdWav8OsjArCPNJN6yL1Q16jM5IExm4EFbIzQF7BJLcu5plw90BYIFq1k3dyYt5UAlLiSxm922HtPDL1KNZBAVkyZ3aIwyiEodCJXXENjOPqmKLUW/prXs/+IH0/3QIYuxF4MqoY8o2Bc/L2TZX3BwNP8eyS3vOvI9DG1cG5hD//N310AWhgWV6ehv3XLUWDC9XlJqUsHbVGnK3zltiyJdQj460n3d6hBHkmbhN3O9H2iuU1MUarPWlHXvZuxGcaXJGjkCOg+GYvyaZa4r5oH7Bkd7reF5bX9UAK8QBorIhDoWZ7W4kVppjaQ9Lf5vwTQt/0DB8qlw8uQge4HF7TJD+fwRUyyI5nNP46Uf9wpEiseQgKs95wCtQotwerg4+P4vaj1DZ0ZFcdEXzCOGWFs5n6lbMqjZOENwAXzv78RAc+6d5aj3D3iXabw2RAWvVc0sj8RlHQOEK1viz08HGOI7N/erVQBrZ0Uicsx/w8Ee0v2Caws0LpmojknscNgUESjglTEMuJt+q6No8DT3UW9v9sWyVm/2SxnNwmVKGN9T/+S0YFJFilxZPQJEEbvD/rjPNR5tynIMJSDJIpWrG73NvW1R8JWh86OlH5//UXQ3jkHW8oVeQnTThVGl3oAtjyBuUtcdrY+Fhhk4ZU8bFc9gbfPQIGpjuAI3tXrUvXI8rwqWkscBV7BCS0bOuTcQT8L9pJTu9a+Y7rbph0hj+/G5qPF5ahNBw2IIPXzsrXxEAkb+7wpbWaf3npI86vVn2UPfLTk2Ax oibLcfbx zKc1tcNUmXexnMT9/5Cl5BtX9ALltoPXY0xyYYvD+M2n+zRxTu3Nu43rnCJQBblEYjKmeWH6Y2nq7Tbf9xX5n5Ev+OJu+QiaXa4AwMDHfRrwn+8VTbs9iuYA2gqAuX6LO07dcj2p+GyjbwcjX5A6R3iIhPBPvhQkJcmn8DOLa9jnpOQgoph0Wc/pCDrIUWNYRTXNv0bf6PHkWoBmRCbnbFBoVC/hrj14LV/yHCihj4ik5UDve6uA7zvbcOkea93HvYRXX5OsGQw2V4SwVj3wdOuI47iz4lKMkc+ZAjdInzBIYkwfKlI4fxIlbuA== 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: Carlos Maiolino 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 This patch conflicted with: 2c6efe9cf2d7 --- mm/shmem.c | 211 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 119 insertions(+), 92 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index 51d17655a6e1..2a7b8060b6f4 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2365,67 +2365,74 @@ 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); - if (sbinfo->noswap) - mapping_set_unevictable(inode->i_mapping); - 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); + INIT_LIST_HEAD(&info->swaplist); + if (sbinfo->noswap) + mapping_set_unevictable(inode->i_mapping); + 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; } @@ -3071,27 +3078,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; @@ -3102,20 +3112,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); @@ -3290,8 +3306,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); @@ -3929,12 +3946,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; @@ -3997,8 +4015,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); @@ -4008,7 +4028,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) @@ -4377,10 +4397,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 */ @@ -4405,9 +4431,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 Jul 13 13:48:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos Maiolino X-Patchwork-Id: 13312236 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 D1DACC0015E for ; Thu, 13 Jul 2023 13:49:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6CBBE90000F; Thu, 13 Jul 2023 09:49:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 67B6090000B; Thu, 13 Jul 2023 09:49:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5446C90000F; Thu, 13 Jul 2023 09:49:10 -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 45CFD90000B for ; Thu, 13 Jul 2023 09:49:10 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 1F2891A0256 for ; Thu, 13 Jul 2023 13:49:10 +0000 (UTC) X-FDA: 81006720060.05.85765F8 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf09.hostedemail.com (Postfix) with ESMTP id 22036140008 for ; Thu, 13 Jul 2023 13:49:07 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=RgP5nZT9; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf09.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=1689256148; 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=1IAdq4n5/oIzAGmJbwcaTZVO+ZHOV/slhcqGbtISZa0=; b=grY4ONXhMmyKnyfsvV7qPlqQgN2LmwkX1IMV4wSWUT2LPrVofKhgIna6+Cm7Vv5a7ABtj1 nhY/RzRbTtcl6RRomLrIrgcmlCoyhWlUb/1RWDpWgqEHWLwWMFcOYbTeDEDS7spZyg/vyP 6vRJ6DpHiOL+1IhPWyyqV+vYiya+LyE= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=RgP5nZT9; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf09.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=1689256148; a=rsa-sha256; cv=none; b=HotUYae2QAw8eH+Wzop/IFgZYJjD4Ss7alW2iKuKtxR92DmvfjIpNEIOVGj8s59EbGBZmL avO/qvOfp43Ehw/eI8lsGiV5qz/KPq1R8sdAIpaFiWMCRqktLg0XId1dcHQiln9+F5arW1 aWnGbmNt702KRJZnMvW5JhLPjsvPNXs= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 3686961539; Thu, 13 Jul 2023 13:49:07 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AED43C433CD; Thu, 13 Jul 2023 13:49:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1689256146; bh=okwg5m+9iRcDR+mIe+e593XoCBrrkAftdLKtFY7t22A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RgP5nZT990UkDxviYs+LUZ/HPon2RZaJfpBtPgi1kRxGWXeX6s+bh+amI0VXt09iG eHVT7xfKVNPXE2H6fNVrJovWYUYZynW5ITk9v9t9uU+9PX6T7Ub4+Xy7Bltj6ixQxz hnpTQB+PkAde7nzTp3V97Ph0hCRt8tl5VmXxnya+/zEDKkBma0fZT/V3+6RK4lGg5l AfkivvgOYA9ibjzCjxO4mAteKSKrKko5Ulm4ixlcGFgnjbN/05+javYG1cEoDT8Oo0 C8IXojGKpMu52V7SnhFqA7PD6S4UDvoXs8EWCf/Yq0OPePeslYAJrM286JBNRFjaze 5CAYK6mI+Ci7Q== From: cem@kernel.org To: linux-fsdevel@vger.kernel.org Cc: jack@suse.cz, akpm@linux-foundation.org, viro@zeniv.linux.org.uk, linux-mm@kvack.org, djwong@kernel.org, hughd@google.com, brauner@kernel.org, mcgrof@kernel.org Subject: [PATCH 3/6] quota: Check presence of quota operation structures instead of ->quota_read and ->quota_write callbacks Date: Thu, 13 Jul 2023 15:48:45 +0200 Message-Id: <20230713134848.249779-4-cem@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230713134848.249779-1-cem@kernel.org> References: <20230713134848.249779-1-cem@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 22036140008 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: sbyb8qoow73reifgj5u88rpw8rwaejp5 X-HE-Tag: 1689256147-591119 X-HE-Meta: U2FsdGVkX1+15QbPtauFqzo4GMe8/c54FV9JYr/rrULD9GiufCxG96I3roSQYHnxaL6T9yVQhYApPBVsNdcWP5cVan85Drdq7ZBFbBuR0xp9V+rp1KRbAEYyjmCQlQ3RSxt60Dt2HoeO49uGES3hUFhv6Ya0Yy8mmWzmMeKNy550h0cjcwcWeqkM+KOejCavVZoDtKTC4ltgSKgtaQo/fXiIJDxfMUAC3vqfctxX66sWJHCgEcV+MkcSuRsIGm9klicsjxoCmctsG+WKwkY0VUXS3oNMYYmB4+uuV4yBM2sqnkp4o/hc4LlR0/pHOp75CnGItbWpV90zsVBNorcbRXgTJnkoB1f+vJws/VSxZmKy7bvcWSTq3OY/7jNj01crKibfrq7yo/9elA48wHWYj8KuNNgSSPCUJfcukyYAE1JrUlROv0Y7fa2vER4D139WjZLOyo5XOR974+V5m22O5FulDBC4Qq9ETPDRPzVdMEjttMz7Dlr636j4g9S1TkiX9rkz95DY4riIJceWxhjC7Yl7XEb69kBiVxTPcUUTWUHmM/SGBuXISzDinFeGyJ6F06dmnfoLfT6guWRppkQCkOJCAdNLpLxM9etvDLtYhKCtc7a/4mamhZUpXn+lPYr1cf+cUfdfEisbyrHBm2umORTo/Y1IOm2jqIIwkVxo8YpdK9e96Je7y1qGd1eqTykFpM+qZFZYAgFJ5CcngjbqvOvrYuSNC0KlrwaAb069Ws5v7l2VeiAJQgc2PotubxHRxuuj/FC8z7bhYe0G99hCIjHRacJTR8uYzxB9pPZlK4LcJD44EoiW6abpGRabl4Zw8dqwgi+SvbhZFbyq2UuPE/vZZ/dDQH6Jz5baaRbMxh4XZJ6SODxGMjf2l10mwYQzPpyOaew55cbVdjAcDESogbP99nQR33Ax6/68JzjZc2vh/1a7F773KCPGwohRGUPLMlYmALlPKRTZ5oSuzG9 TgOtO1ET w+O6N5qjPxOSuXu327ImLkhrG3T9HZGI3CGYXaEQIP4I+o9al+Rc1/ygXLRnd6iTz2dq0aJWh5EV1WAP5quRBo9Z4Gesp/5vKqvheUCCPDXpXOdTbrDsm0XpksemMvtAUj5s8EllLnf4+Qw3Fnn+CIq1rs/mBqt5jl+mCVk2oimc9NuYApwJkxftvzNzFZqsPnfIYqwlhzxYCOcaWk25mD/wFqH5yJ95pGmdienMJUAPGDnTDOr4GFId4VBOe139T0qNv 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 e3e4f4047657..4d826c369da2 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 Jul 13 13:48:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos Maiolino X-Patchwork-Id: 13312237 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 A6C95C0015E for ; Thu, 13 Jul 2023 13:49:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 40CDB900010; Thu, 13 Jul 2023 09:49:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3BCBA90000B; Thu, 13 Jul 2023 09:49:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 25D4E900010; Thu, 13 Jul 2023 09:49: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 18CB990000B for ; Thu, 13 Jul 2023 09:49:13 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id E23891A01F1 for ; Thu, 13 Jul 2023 13:49:12 +0000 (UTC) X-FDA: 81006720144.08.232C43A Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf25.hostedemail.com (Postfix) with ESMTP id 1B67CA0007 for ; Thu, 13 Jul 2023 13:49:10 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=XpJYwMYD; 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=1689256151; 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=QrtdvJDPDhoNM+nIUOzreXjMnv8phOjCrSOh8GGkxoQ=; b=hwvYDZ3ZcOqDRlNTtAn551UfMTyVRvyOXDx7iRXWShw/jbqNhvX5n4ugWoTnunDsEd/7Vt w9Dy+MwAjGXXNt5A+JU+0zaBX1jOvunflLHa0Oy8QxGymm1F1SKs/i/0XldaSQUNm0GOrt y18Bcli7sfWpydODjK2PjFgU6e3OrPo= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=XpJYwMYD; 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=1689256151; a=rsa-sha256; cv=none; b=uIdmJw4mVrwSZtNEPI+a9Fa+rjcspC9GILUv2S9VhbZpLh+wAjle8nvfhjjqh8LaVlkUg3 Rmr45PH3onVHO0u2gEpW0kQ/ciuflA6GhF02yBDk8YCj1/dFPrBxE++Dnh62Urt46E1X8d qk4BRr/g6LuxaFkRQZ418mRBcxMamyQ= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 44E0A61540; Thu, 13 Jul 2023 13:49:10 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4CFD3C433C7; Thu, 13 Jul 2023 13:49:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1689256149; bh=7ujiWzBqUHHMtvILuGdMIIGD3amWSSvl55kgym4kt2c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XpJYwMYDUtSESTN7q8TnoIJi78aI7FAhNnKbEI7ZbQgpB0mSl+R9FbNfVttDkwQQw JdRPiDVMPnIpOAmBDrgPW5CC2ElN/UEgHR8jwVZD4V5r4jf/JdZA2v6qAI2zNm1r4m NAcsc0ugJU42e7wZjO9rIBECwN8nXDw+u5//seQhTWyiPe/Mxf8XnBcPKvx2uA/3IH VfowPIN3yIjwRKWusAFecpruGFsOohejD+1/1g7HJrfiR07PCxdPr3u2pp8lkwcC0N K8gu21AxIsmwm1SF7qe+4vnQjaFC+q2TtCvLgfda9yAADgnMC846PND0O4Frb67M3U nmZPCjHrDDk0Q== From: cem@kernel.org To: linux-fsdevel@vger.kernel.org Cc: jack@suse.cz, akpm@linux-foundation.org, viro@zeniv.linux.org.uk, linux-mm@kvack.org, djwong@kernel.org, hughd@google.com, brauner@kernel.org, mcgrof@kernel.org Subject: [PATCH 4/6] shmem: prepare shmem quota infrastructure Date: Thu, 13 Jul 2023 15:48:46 +0200 Message-Id: <20230713134848.249779-5-cem@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230713134848.249779-1-cem@kernel.org> References: <20230713134848.249779-1-cem@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 1B67CA0007 X-Rspam-User: X-Stat-Signature: c77gfxdxkmnoaq5iugfoc98bb6w46hgw X-Rspamd-Server: rspam01 X-HE-Tag: 1689256150-675039 X-HE-Meta: U2FsdGVkX19+3z6WYlJmfVVOKwsVaExSshJR5E+dpqKZVpo08gXruqYt3DJLhtbpdpIsjQybbbHoDg+NOmnZCNc9XcuNsGKiJC8RNnLvVTaGxqp0DSC/YhUjYdYQzYu+oe29JzHMnwkStEqHQcJX6DU3CTzEvCZm6IlTLk25K3SoOJw/kj55j2+pRuDBw8qYQ/Qk3GbEsHmS7xxk/3yciCnHnevuXJ8ArZ0GK7/oTkf945c5HVBo7OQ4Cp6oNqaFyMo3+6V2PJ7edGnYHuIYGPjqeZgT0IREsWY3gH/orcCxkuA0i7G4CptSAGPy8bPq6wezkU4gZMYR1QcGkIOL9RGKv2Bv9cnWnDUX+RZuXe3DJO4wNRI6uRokLAQq48p018f26ylhnG97f2n7w3JyZcrvTuF/wTXOAXOZs9dUSv/cffDfoZ183WqfvxZLlo/1LQ0gllG7IWVkhkVLdVXtzDW+0CcJobB5OdAQ/TW9gGz5E+fDHlE5FnQl2bfxOmvp69eV21dLM+uR/AySNryKKmewHoiJ1kj/lihjjMX0tk3B8GKuVNvBYgyUeu+cx7ke4P1l0XcPVZGGNxhbRpfLPoz0YgK/aAJT5g9YZ0hW1aQQGVqW4a3fIMQqHAMCkE1sP88hGflwSBdtMZ+FRhNaSeea+wKdJgEndVl8SA/rjqX4pR6fAnEUK8fLaQg2TklpO6O4xTvhtKhP9M0bSMNCIl1SdB6kyohOea9+fxc7CrOM4tmweeT2OkrdaLLJi5OnLcu1oDCs2eJ2kPkc7L82BR9f7c9N26wYMfOFIgMNvn7JVrOtcBv2PayGxfc61dGATIjAhtbUW7tL3uJmev590Mp9zhoZTJVRdNI1eaRShplTwna382lT4UHFDw03SCvmbWI/HP0eDg3Fb1qowsKkmpetJeXhI64ThSkDfbDBoPf/dor+l+Awl6p2JY2UE2M7MAbL4JYpJ7LmWop2MB9 Uuat552+ q3v3i9likCu6xq6z4fX2W963MoIWE1eqQ3rlAINU4ugxxFI8L3VcPGi7BefeJgbroYjHcctdOdU0Eo6W0myVxCHdMdZxpUoJ5KNWgXAOSibeO701sYwB9WOFxcFZk8a5BN46DGuu0e0onEPU5xefNd61PuRLjds2CZ7CSEZmTtMI/v9pzRzD9h0DDw/OESTyysq65fhfXKGZCx1Hsrj6ZwKYY0vgSdSdpLwgxwj6HSA+v0rojI/m/FnKjsFOjgv9z3qFa00vvl+736X0= 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: Carlos Maiolino 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 18d034ec7953..8218a71933f9 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 9029abd29b1c..7abfaf70b58a 100644 --- a/include/linux/shmem_fs.h +++ b/include/linux/shmem_fs.h @@ -13,6 +13,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 */ @@ -172,4 +176,12 @@ extern int shmem_mfill_atomic_pte(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 f17c9636a859..52090105b828 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 678530a07326..d4ee20988dd1 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -51,7 +51,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 show_mem.o\ + compaction.o show_mem.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 000000000000..c0b531e2ef68 --- /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 Jul 13 13:48:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos Maiolino X-Patchwork-Id: 13312238 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 6ABEBC001DF for ; Thu, 13 Jul 2023 13:49:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 06369900011; Thu, 13 Jul 2023 09:49:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0133A90000B; Thu, 13 Jul 2023 09:49:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DF5C1900011; Thu, 13 Jul 2023 09:49:16 -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 D09CF90000B for ; Thu, 13 Jul 2023 09:49:16 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 9A23F80285 for ; Thu, 13 Jul 2023 13:49:16 +0000 (UTC) X-FDA: 81006720312.05.76B045D Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf25.hostedemail.com (Postfix) with ESMTP id 2D69BA001D for ; Thu, 13 Jul 2023 13:49:13 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=iJhSbjr5; 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=1689256154; 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=vEMz05dNTgnhMuW4GRTt/t/XUhEswPIxjML571SVAjM=; b=2OKteXyc+hFLwe3G0zuIAgAfTVtxK407fFD8Dz+efMFOJm76dt+Jr8KrqW5YJIFhPUYvfR KbKbufykoQV3QYulrcIfabGgubwvF0BBXoeMQn893z1eQw9+w7QikPWDJjda2rFyQVCkqt ZN1SLrK5gu5rTsNBzXVWWxeBew8px+Q= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=iJhSbjr5; 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=1689256154; a=rsa-sha256; cv=none; b=Uob5qz3IZOzLC/vidUrWBgGeMBxDNr+F1a1GjXK2Z/LWqrcsub6qmsoK+RKCLNzK3SVsx7 aqj6xXQC/cb9Sw5XSLxBBH0xqr6czqKE5MnxDX1ckG+IJNH8FtiDWAm27UOdGnC9bgFyQq ESGYhh1t+m4bl3LfHPI3aI88nGAx5vA= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 0B94361542; Thu, 13 Jul 2023 13:49:13 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3DB29C433CB; Thu, 13 Jul 2023 13:49:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1689256152; bh=CsRiH0/5hH23KrZRoMfmKbplSRKi/0kCmESy+wzSO8E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iJhSbjr5t/F6hB9jPSUSZVr81PX4Q6lcosEy5ozD0DY6U/xio/xRUmP+LgoVIdsk1 tJnlAii67r2fFmeFcx6M+nlI9z3l4FnZwzdvliiJ5kXpteAwmQgm14YzMHS+F6TK4y 7rTU0unrbbAqgEOcxoEMouOEq8MWBXRV7LVJhPYaprEwckJPUS+4q1NO7mi76ztql/ vDCON5cswefkBNbcz+Sij0pkl7hgd5Cg7gAyUE5/uXz6EQhMyZv9bxOc5zJQ4Y92m3 pziSZN/QE2y1OH2JeH8gfwVJW2x7tlMbtUUUf5RxHhByW2tWBDWoDYVziyJrVNJHo4 coq8Xy+Dq6+fQ== From: cem@kernel.org To: linux-fsdevel@vger.kernel.org Cc: jack@suse.cz, akpm@linux-foundation.org, viro@zeniv.linux.org.uk, linux-mm@kvack.org, djwong@kernel.org, hughd@google.com, brauner@kernel.org, mcgrof@kernel.org Subject: [PATCH 5/6] shmem: quota support Date: Thu, 13 Jul 2023 15:48:47 +0200 Message-Id: <20230713134848.249779-6-cem@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230713134848.249779-1-cem@kernel.org> References: <20230713134848.249779-1-cem@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 2D69BA001D X-Rspam-User: X-Stat-Signature: nqkdngarbbicqzn7ks1zq545az7d69up X-Rspamd-Server: rspam01 X-HE-Tag: 1689256153-673680 X-HE-Meta: U2FsdGVkX1+rRSmrY6Pc3rH7XcLhdyC0P/Fq/Nc5x6C0R03vf0MKk+TYDSgXgHQfa6U7vq4/u+NZ8L0Y66akmm5bKlEQlk5fPTH8QjEJ23rvXhB71NTmXFbrE+JyduCSil+QdGuOiWxVMyZqDrstXKc6/jctO4LRROLDoQkQ66rbw08SY2zYASpsHtE2fzmzosO641BkgYdq78MN2Uykjq7SYS9VPcTgmxU3PT3WRCgZykbSOfjvtQovEDM0YJPu5kvxQbMXbDQn5WCHk5he6j0V5+FEcoXY8jGKP85487eX960OGyZk9FqM+N7tO7omBIBXQ2Wts2C4tGveZANDor9uWArlIbVJYkxuLoT9qpJTcvV1AHtVc+zNcygzdawFnF6j3diijm1amSobV1W1ZXQqysGbm1+BKzHrMRRvQ8sG2BmkV3uB6I+aCEiC3rnzQLNWE6QFJUuYvD/LifQkUvaiS9mDdL0HSxX29MDQ5nktgtQ4rzcULun7EqwY1OVOQxALWJhVx80HdzvtMPLHrrmBgc5ofeYxkZLT3TkdJtV5htoXey7pU9sc1tv2UPcI2xPoPqwhDLWtBqTZWxUW7+EoxmENTIhmt0vnhCsngvBNmtjdAGiz/PzFa7CeNvymrKyN5FWO3mknzkrRSVqo+ArE7ioWQW0MZZKSU9PSPTzhYNntrZJNIjkbWeEHVqvg+yiFgnyv/Z6oxD4CRlj+to6DUSJ5soYYRU2ektLIZlmGetmx5Cf71aVQXri/iblN7DYjUJMtcD5hRYLJfm8YNUMXAlHE8c5uqiIJgpJlRtW2xjxG4aEdMj/939F5IC66IXZuV2VQq75MaAlJDrv51qvPdwI7VOz+e7y/4/nfCzpn0LyVSKlwssHmE+g018S6jAbSBYKzhofuZ71OhJ6LKadUhOHqT2SY73ySBbqHTR5cxszZmQ/Pt25tneEOYIXr5MUhAuD7nYKc6vmiaiC P7C9hmZ7 4khEEk7Jk9Lg2vb3Jy+u5IdWTyXFrgpCE8/opYDswBxhCN3k3XHBPKZ6eZQrEKh9ul3fd9HKJ9YcIvpM/bwFGfUT/PLgEehl2OjxzMYLzj58KoKQhAYnzH8xB19U0Z8SiMtgxsTMuJ0aDTDkFDyGUm4F8moCTjeU16Wng6NUVsrK8IYOfMOBtnUySZqpRbtOyZdXuJfyqcPd/UAQKKpDNdaDzJPYFeQZXkHHR3rF1WR9SFWKLwlXSvUH7lv7bQlIbGbC+5A3AkXK8LO8= 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: Carlos Maiolino 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 f18f46be5c0c..0c7d8bd052f1 100644 --- a/Documentation/filesystems/tmpfs.rst +++ b/Documentation/filesystems/tmpfs.rst @@ -130,6 +130,21 @@ for emergency or testing purposes. The values you can set for shmem_enabled are: option, for testing == ============================================================ +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 7abfaf70b58a..1a568a0f542f 100644 --- a/include/linux/shmem_fs.h +++ b/include/linux/shmem_fs.h @@ -31,6 +31,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; }; @@ -184,4 +187,9 @@ extern int shmem_mfill_atomic_pte(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 2a7b8060b6f4..5022238dd68d 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -78,6 +78,7 @@ static struct vfsmount *shm_mnt; #include #include #include +#include #include @@ -116,11 +117,13 @@ struct shmem_options { int huge; int seen; bool noswap; + 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_NOSWAP 16 +#define SHMEM_SEEN_QUOTA 32 }; #ifdef CONFIG_TMPFS @@ -212,7 +215,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; @@ -227,6 +239,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); @@ -255,6 +269,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. @@ -361,7 +416,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); } } @@ -379,7 +433,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); @@ -395,7 +448,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); @@ -1141,6 +1193,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); @@ -1187,6 +1254,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, @@ -1986,7 +2057,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; @@ -2357,9 +2427,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; @@ -2436,6 +2507,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(pmd_t *dst_pmd, struct vm_area_struct *dst_vma, @@ -2538,7 +2646,6 @@ int shmem_mfill_atomic_pte(pmd_t *dst_pmd, spin_lock_irq(&info->lock); info->alloced++; - inode->i_blocks += BLOCKS_PER_PAGE; shmem_recalc_inode(inode); spin_unlock_irq(&info->lock); @@ -3516,6 +3623,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, @@ -3524,6 +3632,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, @@ -3623,6 +3732,9 @@ enum shmem_param { Opt_inode32, Opt_inode64, Opt_noswap, + Opt_quota, + Opt_usrquota, + Opt_grpquota, }; static const struct constant_table shmem_param_enums_huge[] = { @@ -3645,6 +3757,11 @@ const struct fs_parameter_spec shmem_fs_parameters[] = { fsparam_flag ("inode32", Opt_inode32), fsparam_flag ("inode64", Opt_inode64), fsparam_flag ("noswap", Opt_noswap), +#ifdef CONFIG_TMPFS_QUOTA + fsparam_flag ("quota", Opt_quota), + fsparam_flag ("usrquota", Opt_usrquota), + fsparam_flag ("grpquota", Opt_grpquota), +#endif {} }; @@ -3736,6 +3853,18 @@ static int shmem_parse_one(struct fs_context *fc, struct fs_parameter *param) ctx->noswap = true; ctx->seen |= SHMEM_SEEN_NOSWAP; 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; @@ -3843,6 +3972,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) @@ -3934,6 +4069,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); @@ -4013,6 +4151,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)) { @@ -4188,6 +4337,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, @@ -4254,6 +4406,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"); @@ -4278,6 +4438,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 Jul 13 13:48:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos Maiolino X-Patchwork-Id: 13312239 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 66D2DC001B0 for ; Thu, 13 Jul 2023 13:49:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F2FEB900012; Thu, 13 Jul 2023 09:49:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EEDEC90000B; Thu, 13 Jul 2023 09:49:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D8188900012; Thu, 13 Jul 2023 09:49:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id C945C90000B for ; Thu, 13 Jul 2023 09:49:18 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 95FE7A02BD for ; Thu, 13 Jul 2023 13:49:18 +0000 (UTC) X-FDA: 81006720396.12.1B7AB66 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf27.hostedemail.com (Postfix) with ESMTP id B6A2B4001D for ; Thu, 13 Jul 2023 13:49:16 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=rvYDjiFf; spf=pass (imf27.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=1689256156; 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=d6njbMHb9LR4qfJDNqCnXrwBbly4+XOxWDh1cwKoXHg=; b=c1AHxOKthp0Ig8rqkB+b17voLT19DeQVt+2nZyuujZg4tymZ9t7oj1DO509w5Y8pzztnTP WPD1clMPIAplvIC4Bsc7hZ384/JmrKoFotWQNmCCGZKZ7OwU2Ug//IU5FQIgHDWI+AZeho ZZUqzTFVJGPm5+P9hBEWbfPK0mTIwFo= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=rvYDjiFf; spf=pass (imf27.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=1689256156; a=rsa-sha256; cv=none; b=cArXH+0S2EFmBclP+pLuq1Yn6G1i2UjwZ/eJGW/ioMUbizlK0fULu4VdHK2CQVfdZJeo2F H/x+dl+92nVb98A1e+xuA2OzDVkqWo2cnFiNJFP6iFfDh8e+gFfUZJucjKBULXOAqhw2// Z008rEx4HjxfvChqzAJjPE9wouLEwrU= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id C746761539; Thu, 13 Jul 2023 13:49:15 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E94C9C433C7; Thu, 13 Jul 2023 13:49:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1689256155; bh=1msIUsxcH0sSO0HGrc5NqbncyPX9T0EgVc0/Kd8KxA8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rvYDjiFfvloQGVGVi9z/uIy9hjdG7A3pWmpHbZzUTjcISjc2M53/+QcOz+Lr/Rl6O 9qzBSvN3YydRq/ylDg29qeoWD5euvt4lJ9ZHiwRGRg7zqlyF1Be2Cj/s0qFpBWs9CG 6U2sgOJ5auumldyQkaqmv7rxxFIr6ksVMMcCFWbM5m8EXQi687QhsC9RtHrcR6B5mZ fsLuSTiuu/AIpHsg8Oz23V1n5I4oFUZU5q+cL7NdJ0RhvrF1DBc4k1TOmWQJ+pEH4F gcfs4fWhdmn6ol305GUHNVZLc7RzaEEB5QI39QjKgyP4TQCn1WE7C8nOOBkNkxNxHY 5xlQGjSK+GiHA== From: cem@kernel.org To: linux-fsdevel@vger.kernel.org Cc: jack@suse.cz, akpm@linux-foundation.org, viro@zeniv.linux.org.uk, linux-mm@kvack.org, djwong@kernel.org, hughd@google.com, brauner@kernel.org, mcgrof@kernel.org Subject: [PATCH 6/6] Add default quota limit mount options Date: Thu, 13 Jul 2023 15:48:48 +0200 Message-Id: <20230713134848.249779-7-cem@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230713134848.249779-1-cem@kernel.org> References: <20230713134848.249779-1-cem@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: B6A2B4001D X-Rspam-User: X-Stat-Signature: ty3d3rt74wxa67mkf6hzigs16tqqgnc4 X-Rspamd-Server: rspam01 X-HE-Tag: 1689256156-994883 X-HE-Meta: U2FsdGVkX1/fH1taAK5oA8nBACB3B1awBOweCf0dlEQyOoh3t4oc3Fg402bFlHI3EdRWunUcx6b+SmFfe+UDHMMpXHcpBU0ypVHQsV8qxlpdmVj1GwKnc9V4iqDtGwIHz4yiQpS84hE3mzPRmlzt3YsDl5KQzZPVV29MLYyNrGMX+yXOx12U8mRa/xvPgKrlLluUV97gztSkeGIMfHZyZx4dDOE9Fye7hohQ6p/TrkidVelwAEBgAVHxNX5rrHld8NjwWx377VEBKX4TOgVk5ny+KF7fO5/S00xzRExEWBmcnJp8X/JXmxKwKHjuDoLzf6LepVfdpifv32nT2l+G2DEm2yiVLhMevDBoVfBT2hn6QOoLiozyLDIxUYcX1aH/BbpRPCLXR/j4u0sDEIZSru8A5x5Fh2Ph+kAkOvEttpmT6kn+gGKQwDw46x2ZHr5VsvvVtbRpscT8BRVaO2aEB7G8vFoofWmGH1gIa/4ArngrPok0ZPpmNg0UlyhOLWE1569Pggicte126RLg5OP4BJsqytJ3BGZvgjmlmzwm/52/DTbYYrmrZpdmMry8S0lBVXrmvns6wCXNE2TbiEJC8tCENV4SjaiwXpaY/yJSPf1qbfj1B1bKnbKERRJY4awuIiHfJ9Kvarzmqy/AqncRriGB8j4v0eD4jWQCZRtF1C2FepCWAYFn/j6QR7UmlZKeP47UWMpOpsp6/KVoL2gB2zeomzhvix57Z9JCfju9v3ZMnDpA4kgnm6pnsUHA7poSnSN7R+V2GEfe18rp8VTDP37cI9RPQdVXY/QXXGvFs9FPHJdzEVeSyuAd4GiLVr1xuo5JSn6SZ44BEQnCXcKHY7AGBrq1NkvbpSLZeW/qn/jBvnxiPDwuq3WIpNUr1DownWBVF++GtXsY1lVXxaLdg3WVmiAgo46OYE9XJz/mxB9wI09iPzXL9xLHNJPB+x1ctKmQ6JYmNcMN3dez/TD zRZ/dmVX umn02RjoY0xg/84T2zAZqFwp36wyDTEgBQHIV1D3WmBfvxyaism7ClQCV16zejoVw9WM4lXkkF1YRAgQj2yI3ytkTZP3losoSTmuevCUjJsKlmw6scGDuLCjn80/di8B8x/052nq8FHoEy0jGqibMlDIOsrxFwXJ/WZ1Hsw/8tbhUggguCMsHPKpv+9FFEJe9Wb4kEAWQ9xelSGVl0gqlQWJvti6RW3QsuTQ0WL5oilHq73i1kLMeH14Svz5JpMgXUxAFgJagRQmiACs= 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 Signed-off-by: Carlos Maiolino --- 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 0c7d8bd052f1..f843dbbeb589 100644 --- a/Documentation/filesystems/tmpfs.rst +++ b/Documentation/filesystems/tmpfs.rst @@ -132,15 +132,31 @@ for emergency or testing purposes. The values you can set for shmem_enabled are: 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 1a568a0f542f..c0058f3bba70 100644 --- a/include/linux/shmem_fs.h +++ b/include/linux/shmem_fs.h @@ -42,6 +42,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 */ @@ -60,6 +67,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 5022238dd68d..083ce6b478e7 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -118,6 +118,7 @@ struct shmem_options { int seen; bool noswap; unsigned short quota_types; + struct shmem_quota_limits qlimits; #define SHMEM_SEEN_BLOCKS 1 #define SHMEM_SEEN_INODES 2 #define SHMEM_SEEN_HUGE 4 @@ -3735,6 +3736,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[] = { @@ -3761,6 +3766,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 {} }; @@ -3865,6 +3874,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; @@ -3978,6 +4023,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) @@ -4157,6 +4214,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 c0b531e2ef68..e349c0901bce 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);