From patchwork Tue Jul 25 14:45:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos Maiolino X-Patchwork-Id: 13326571 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 A5179C04E69 for ; Tue, 25 Jul 2023 14:45:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3F2416B007E; Tue, 25 Jul 2023 10:45:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3A49E6B0080; Tue, 25 Jul 2023 10:45:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 21C3D8D0001; Tue, 25 Jul 2023 10:45:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 0A3E76B007E for ; Tue, 25 Jul 2023 10:45:37 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 9E8DC160E50 for ; Tue, 25 Jul 2023 14:45:36 +0000 (UTC) X-FDA: 81050407872.04.DB3DDBE Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf06.hostedemail.com (Postfix) with ESMTP id 82F59180021 for ; Tue, 25 Jul 2023 14:45:34 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ICbNbwGK; spf=pass (imf06.hostedemail.com: domain of cem@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cem@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1690296334; 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=pxoJg+Pt00xjW1wqRTQRQVCqBr/0yKn3dxhDDQn6B8k=; b=wI8Vf3gTRUcfr79ubrCfGxOwFxFgbv3w9LrCPiG11LPrRTs63iihf4YGWiXefrs1cLwwae HmJaspkrF0lLArsrgE2SheA5Zsp810AatsTrejS3PoJS5kt3PncmHioO8g/u3GGQLP0b8S 9X53zL7V5E4uJcUyfqahwyfO05Kv6qg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690296334; a=rsa-sha256; cv=none; b=d2eSoDwUlmoGTd0V6J3VBUNuB2lYTffTfvWzpo+gql9QGh7eZfusuEytnfqkuCMW6L6zDL kEKCKdxycjOr7gcGK3fYjtkYSzYKCSMnE35et7bJTDObksDnQOZ9a106W8Snyis6s5E9h9 0tJvN9uaMOdEXcNZ7+km6N6FlDXqQkE= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ICbNbwGK; spf=pass (imf06.hostedemail.com: domain of cem@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cem@kernel.org; dmarc=pass (policy=none) header.from=kernel.org 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 86A7961788; Tue, 25 Jul 2023 14:45:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CDE7EC433CD; Tue, 25 Jul 2023 14:45:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1690296333; bh=lISOjIRoZ8eyNsvIWJg3pEzAgdhmgsQyuciEDHNazkM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ICbNbwGK9vPDstPO+SP6gjRMfJPQmQ439ac+WmNnjrEofwPgn2a052ZfY/qgOVTJe wg6JS4Y8YJP3llQfB+CIthE7rEkkdZXUwdNC+IvP5BoiGTcnMZc3qEgwIjerzCkzgS OvXT44hXRcfvRnuo/JxCSgyha5SMALEDg9nuX1SrSgmSTbSrqWxcaFeiCkW/aAZvaI qj9imUV9s+f1KLnUWcV3ikA1Aas8emPYtwz+DwX47+xahOXLI3n6dv7X8lw7RmdPFq tKAL2tGGJsUQpGVxPqWPJF1bc2IAmFTsXztaAq+SOjlgoIfy883HaTlzclKu6eiTkD W4KsRrR4NCBzA== 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/7] shmem: Add default quota limit mount options Date: Tue, 25 Jul 2023 16:45:09 +0200 Message-Id: <20230725144510.253763-7-cem@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230725144510.253763-1-cem@kernel.org> References: <20230725144510.253763-1-cem@kernel.org> MIME-Version: 1.0 X-Stat-Signature: etict9skmub33synoa9c9q9o5c9iwapf X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 82F59180021 X-Rspam-User: X-HE-Tag: 1690296334-51697 X-HE-Meta: U2FsdGVkX1/ztYKVfD50XVnfHDuBipO6/IDy60O7zDRlP0ZW5WoOkIO8j8L+jk+SksmG0My42Mh+kvykbL+78QTbHixBebxrruIWiu+nH+1eIAvGwOhMvMmoHIlTLXDGRydsEPfvLNLQ8v4cF5u9qsZveiu+fC6hvpJVzkZPkxOSo1NasofosRecw6mQM6t6KhUmjvOWLOlS1WWrNOi/c/DzyoX1uE1uNyOyObVBi7dlfEUa5yJZEIJm6YEMGO/4PKGIWPJx4AUOxQCSyN534Z1PrC/gmE4+7L+XIbxgJMuojT9XJQcQpvhFhAlySEg3+nSfuGAd402djwRuinS4d0dVBcFWlywB+ZT0HMi6BnMjyk2Al/kpxT1AIJeFGWDP6TebSZeJ3F7KVTpkXRAyypWv/xUyIzpK1pyCb6AdDCGrQR4wbmBURznwKjDutyL0lWWYLIK7uG+8aFoV52RLthccFamKKTq0DNKGKaAp8ECntGK86AerKHNOJ+MWW7aZx2Ua7U50531B0wUSWdii/XFgEsuRpXr++khInQFafel6jRBRIP8dkF5zAbLx3ROCRgeoWy1QmxDpTaLfX4ev58ocui6iKW+aSvZDLPBB8taMSeYoLa5MaRnuLjW0e396SSZ9ROGdiOCrkj8iwcULtwNXTyuj9oaylm3U8HJC/iw/3QuhzvqYFDDtND5Zm6l3yJYPkxD9/6mI5fHCFpj1WcZV7yVLnYSvdn3JJH4sAn/U081JQOtMN3UMMfn1Tba43p5tQJjAbZJZtPmmsOXEaCbxsv6FlH8m6ToGjJXBX2I4Cu4RMlrZoe4sstmO2YqFkR0zcYmHJf5KxUZ8E51+lyQvXOqezf0g7DheDvdisfIXtTFjkPT/zCLy1Tqlhvt4/DG1hxr3gi/xDyEfyVr/1MdIhEFX3XwY2INBLFAYFjifNk67t97L06nI5XS0Z84w2WdowL3T6WXWhLBR2GW tZ6M/55a 1X623i92jhCaWl/HvBUwUTGBtuJpK06Igs5cVnELsbROCOfB8F+rhgah6Jk/b2UItx+xpK4W5lbvb2n9WF8JQEDNuEzNOlLleyI8ow40ienvUKpG93scQTMxghIjEUvJfBr2dFky6aofc0xoSVvsU2ZnTxetQLnf7W6aEM4NimJXpyw8frNu2JBDl3jfkEJ1bawrqSGUa7KXx3VNhhdBad31tszQzn6bBk34woyj4/c7A9johvS84XZ9CTruyrDZtPyuYyEMeRFBInPwiPLKzgbyWNFlJ3DroOwK5ocxLyq0h4l8= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Lukas Czerner Allow system administrator to set default global quota limits at tmpfs mount time. Signed-off-by: Lukas Czerner Signed-off-by: Carlos Maiolino Reviewed-by: Jan Kara --- Documentation/filesystems/tmpfs.rst | 34 +++++++++++----- include/linux/shmem_fs.h | 8 ++++ mm/shmem.c | 61 +++++++++++++++++++++++++++++ mm/shmem_quota.c | 34 +++++++++++++++- 4 files changed, 127 insertions(+), 10 deletions(-) diff --git a/Documentation/filesystems/tmpfs.rst b/Documentation/filesystems/tmpfs.rst index 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 7c75f30309ff..bd02909bacd6 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 {} }; @@ -3871,6 +3880,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; @@ -3984,6 +4029,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) @@ -4163,6 +4220,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 e92b8ece9880..062d1c1097ae 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);