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);