From 94fbd86c2bd3196ca94970f7172855e1fd1b796a Mon Sep 17 00:00:00 2001
From: Helge Deller <deller@gmx.de>
Date: Fri, 29 Jul 2022 21:12:32 +0200
Subject: [PATCH] tmpfs-v5.10.patch


diff --git a/fs/xattr.c b/fs/xattr.c
index cd7a563e8bcd..f7b3590b8ec0 100644
--- a/fs/xattr.c
+++ b/fs/xattr.c
@@ -1000,6 +1000,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;
@@ -1012,6 +1013,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:
@@ -1094,5 +1096,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 10b4dc2709f0..643683fad1fc 100644
--- a/include/linux/xattr.h
+++ b/include/linux/xattr.h
@@ -72,6 +72,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 {
@@ -88,6 +89,7 @@ static inline void simple_xattrs_init(struct simple_xattrs *xattrs)
 {
 	INIT_LIST_HEAD(&xattrs->head);
 	spin_lock_init(&xattrs->lock);
+	xattrs->count = 0;
 }
 
 /*
@@ -101,6 +103,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 d3d8c5e7a296..f4a5de5d20af 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -3289,6 +3289,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 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,
@@ -3306,6 +3330,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
