From 102a0743326a03cd1a1202ceda21e175b7d3575c Mon Sep 17 00:00:00 2001
From: hc <hc@nodka.com>
Date: Tue, 20 Feb 2024 01:20:52 +0000
Subject: [PATCH] add new system file

---
 kernel/mm/shmem.c |   61 +++++++++++++++++++-----------
 1 files changed, 39 insertions(+), 22 deletions(-)

diff --git a/kernel/mm/shmem.c b/kernel/mm/shmem.c
index 682a3e9..b812f26 100644
--- a/kernel/mm/shmem.c
+++ b/kernel/mm/shmem.c
@@ -285,10 +285,10 @@
 	ino_t ino;
 
 	if (!(sb->s_flags & SB_KERNMOUNT)) {
-		raw_spin_lock(&sbinfo->stat_lock);
+		spin_lock(&sbinfo->stat_lock);
 		if (sbinfo->max_inodes) {
 			if (!sbinfo->free_inodes) {
-				raw_spin_unlock(&sbinfo->stat_lock);
+				spin_unlock(&sbinfo->stat_lock);
 				return -ENOSPC;
 			}
 			sbinfo->free_inodes--;
@@ -311,7 +311,7 @@
 			}
 			*inop = ino;
 		}
-		raw_spin_unlock(&sbinfo->stat_lock);
+		spin_unlock(&sbinfo->stat_lock);
 	} else if (inop) {
 		/*
 		 * __shmem_file_setup, one of our callers, is lock-free: it
@@ -326,14 +326,13 @@
 		 * to worry about things like glibc compatibility.
 		 */
 		ino_t *next_ino;
-
 		next_ino = per_cpu_ptr(sbinfo->ino_batch, get_cpu());
 		ino = *next_ino;
 		if (unlikely(ino % SHMEM_INO_BATCH == 0)) {
-			raw_spin_lock(&sbinfo->stat_lock);
+			spin_lock(&sbinfo->stat_lock);
 			ino = sbinfo->next_ino;
 			sbinfo->next_ino += SHMEM_INO_BATCH;
-			raw_spin_unlock(&sbinfo->stat_lock);
+			spin_unlock(&sbinfo->stat_lock);
 			if (unlikely(is_zero_ino(ino)))
 				ino++;
 		}
@@ -349,9 +348,9 @@
 {
 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
 	if (sbinfo->max_inodes) {
-		raw_spin_lock(&sbinfo->stat_lock);
+		spin_lock(&sbinfo->stat_lock);
 		sbinfo->free_inodes++;
-		raw_spin_unlock(&sbinfo->stat_lock);
+		spin_unlock(&sbinfo->stat_lock);
 	}
 }
 
@@ -1493,10 +1492,10 @@
 {
 	struct mempolicy *mpol = NULL;
 	if (sbinfo->mpol) {
-		raw_spin_lock(&sbinfo->stat_lock);	/* prevent replace/use races */
+		spin_lock(&sbinfo->stat_lock);	/* prevent replace/use races */
 		mpol = sbinfo->mpol;
 		mpol_get(mpol);
-		raw_spin_unlock(&sbinfo->stat_lock);
+		spin_unlock(&sbinfo->stat_lock);
 	}
 	return mpol;
 }
@@ -3425,6 +3424,8 @@
 	unsigned long long size;
 	char *rest;
 	int opt;
+	kuid_t kuid;
+	kgid_t kgid;
 
 	opt = fs_parse(fc, shmem_fs_parameters, param, &result);
 	if (opt < 0)
@@ -3460,14 +3461,32 @@
 		ctx->mode = result.uint_32 & 07777;
 		break;
 	case Opt_uid:
-		ctx->uid = make_kuid(current_user_ns(), result.uint_32);
-		if (!uid_valid(ctx->uid))
+		kuid = make_kuid(current_user_ns(), result.uint_32);
+		if (!uid_valid(kuid))
 			goto bad_value;
+
+		/*
+		 * The requested uid must be representable in the
+		 * filesystem's idmapping.
+		 */
+		if (!kuid_has_mapping(fc->user_ns, kuid))
+			goto bad_value;
+
+		ctx->uid = kuid;
 		break;
 	case Opt_gid:
-		ctx->gid = make_kgid(current_user_ns(), result.uint_32);
-		if (!gid_valid(ctx->gid))
+		kgid = make_kgid(current_user_ns(), result.uint_32);
+		if (!gid_valid(kgid))
 			goto bad_value;
+
+		/*
+		 * The requested gid must be representable in the
+		 * filesystem's idmapping.
+		 */
+		if (!kgid_has_mapping(fc->user_ns, kgid))
+			goto bad_value;
+
+		ctx->gid = kgid;
 		break;
 	case Opt_huge:
 		ctx->huge = result.uint_32;
@@ -3563,10 +3582,9 @@
 	struct shmem_options *ctx = fc->fs_private;
 	struct shmem_sb_info *sbinfo = SHMEM_SB(fc->root->d_sb);
 	unsigned long inodes;
-	struct mempolicy *mpol = NULL;
 	const char *err;
 
-	raw_spin_lock(&sbinfo->stat_lock);
+	spin_lock(&sbinfo->stat_lock);
 	inodes = sbinfo->max_inodes - sbinfo->free_inodes;
 	if ((ctx->seen & SHMEM_SEEN_BLOCKS) && ctx->blocks) {
 		if (!sbinfo->max_blocks) {
@@ -3611,15 +3629,14 @@
 	 * Preserve previous mempolicy unless mpol remount option was specified.
 	 */
 	if (ctx->mpol) {
-		mpol = sbinfo->mpol;
+		mpol_put(sbinfo->mpol);
 		sbinfo->mpol = ctx->mpol;	/* transfers initial ref */
 		ctx->mpol = NULL;
 	}
-	raw_spin_unlock(&sbinfo->stat_lock);
-	mpol_put(mpol);
+	spin_unlock(&sbinfo->stat_lock);
 	return 0;
 out:
-	raw_spin_unlock(&sbinfo->stat_lock);
+	spin_unlock(&sbinfo->stat_lock);
 	return invalfc(fc, "%s", err);
 }
 
@@ -3736,7 +3753,7 @@
 	sbinfo->mpol = ctx->mpol;
 	ctx->mpol = NULL;
 
-	raw_spin_lock_init(&sbinfo->stat_lock);
+	spin_lock_init(&sbinfo->stat_lock);
 	if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL))
 		goto failed;
 	spin_lock_init(&sbinfo->shrinklist_lock);
@@ -4103,7 +4120,7 @@
 	.name		= "tmpfs",
 	.init_fs_context = ramfs_init_fs_context,
 	.parameters	= ramfs_fs_parameters,
-	.kill_sb	= kill_litter_super,
+	.kill_sb	= ramfs_kill_sb,
 	.fs_flags	= FS_USERNS_MOUNT,
 };
 

--
Gitblit v1.6.2