From bf69e66204e0b45a15ed10d1c7bb10163bac0818 Mon Sep 17 00:00:00 2001
From: Helge Deller <deller@gmx.de>
Date: Thu, 14 Jul 2022 18:58:23 +0200
Subject: [PATCH] tmpfs-v5.18.patch


diff --git a/fs/xattr.c b/fs/xattr.c
index 998045165916..ba95e99c971c 100644
--- a/fs/xattr.c
+++ b/fs/xattr.c
@@ -1036,6 +1036,7 @@ int simple_xattr_set(struct simple_xattrs *xattrs, const char *name,
 				if (removed_size)
 					*removed_size = xattr->size;
 			} else {
+				--xattrs->count;
 				list_del(&xattr->list);
 				if (removed_size)
 					*removed_size = xattr->size;
@@ -1048,6 +1049,7 @@ int simple_xattr_set(struct simple_xattrs *xattrs, const char *name,
 		err = -ENODATA;
 	} else {
 		list_add(&new_xattr->list, &xattrs->head);
+		xattrs->count++;
 		xattr = NULL;
 	}
 out:
@@ -1130,5 +1132,6 @@ void simple_xattr_list_add(struct simple_xattrs *xattrs,
 {
 	spin_lock(&xattrs->lock);
 	list_add(&new_xattr->list, &xattrs->head);
+	xattrs->count++;
 	spin_unlock(&xattrs->lock);
 }
diff --git a/include/linux/xattr.h b/include/linux/xattr.h
index 4c379d23ec6e..ad9d6e4f7a1a 100644
--- a/include/linux/xattr.h
+++ b/include/linux/xattr.h
@@ -82,6 +82,7 @@ static inline const char *xattr_prefix(const struct xattr_handler *handler)
 struct simple_xattrs {
 	struct list_head head;
 	spinlock_t lock;
+	int count;
 };
 
 struct simple_xattr {
@@ -98,6 +99,7 @@ static inline void simple_xattrs_init(struct simple_xattrs *xattrs)
 {
 	INIT_LIST_HEAD(&xattrs->head);
 	spin_lock_init(&xattrs->lock);
+	xattrs->count = 0;
 }
 
 /*
@@ -111,6 +113,17 @@ static inline void simple_xattrs_free(struct simple_xattrs *xattrs)
 		kfree(xattr->name);
 		kvfree(xattr);
 	}
+	xattrs->count = 0;
+}
+
+static inline int simple_xattrs_count(struct simple_xattrs *xattrs)
+{
+	int ret;
+
+	spin_lock(&xattrs->lock);
+	ret = xattrs->count;
+	spin_unlock(&xattrs->lock);
+	return ret;
 }
 
 struct simple_xattr *simple_xattr_alloc(const void *value, size_t size);
diff --git a/mm/shmem.c b/mm/shmem.c
index 4b2fea33158e..2a90216921a9 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -3176,6 +3176,30 @@ static int shmem_xattr_handler_set(const struct xattr_handler *handler,
 	return simple_xattr_set(&info->xattrs, name, value, size, flags, NULL);
 }
 
+static int shmem_xattr_handler_set_user(const struct xattr_handler *handler, struct user_namespace *ns,
+				   struct dentry *unused, struct inode *inode,
+				   const char *name, const void *value,
+				   size_t size, int flags)
+{
+	struct shmem_inode_info *info = SHMEM_I(inode);
+	struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
+
+	if (value) {
+		if (size > 8192)
+			return -EINVAL;
+		if (simple_xattrs_count(&info->xattrs) >= sbinfo->max_inodes)
+			return -ENOSPC;
+	}
+	name = xattr_full_name(handler, name);
+	return simple_xattr_set(&info->xattrs, name, value, size, flags, NULL);
+}
+
+static const struct xattr_handler shmem_user_xattr_handler = {
+	.prefix = XATTR_USER_PREFIX,
+	.get = shmem_xattr_handler_get,
+	.set = shmem_xattr_handler_set_user,
+};
+
 static const struct xattr_handler shmem_security_xattr_handler = {
 	.prefix = XATTR_SECURITY_PREFIX,
 	.get = shmem_xattr_handler_get,
@@ -3193,6 +3217,7 @@ static const struct xattr_handler *shmem_xattr_handlers[] = {
 	&posix_acl_access_xattr_handler,
 	&posix_acl_default_xattr_handler,
 #endif
+	&shmem_user_xattr_handler,
 	&shmem_security_xattr_handler,
 	&shmem_trusted_xattr_handler,
 	NULL
