unicode: Expose available encodings in sysfs

A filesystem configuration utility has no way to detect which filename
encodings are supported by the running kernel.  This means, for
instance, mkfs has no way to tell if the generated filesystem will be
mountable in the current kernel or not.  Also, users have no easy way to
know if they can update the encoding in their filesystems and still have
something functional in the end.

This exposes details of the encodings available in the unicode
subsystem to fill that gap.

Cc: Theodore Ts'o <tytso@mit.edu>
Cc: Jaegeuk Kim <jaegeuk@kernel.org>
Signed-off-by: Gabriel Krisman Bertazi <krisman@collabora.com>
diff --git a/Documentation/ABI/testing/sysfs-fs-unicode b/Documentation/ABI/testing/sysfs-fs-unicode
new file mode 100644
index 0000000..b56e0dd
--- /dev/null
+++ b/Documentation/ABI/testing/sysfs-fs-unicode
@@ -0,0 +1,6 @@
+What:		/sys/fs/unicode/<encoding>/latest_version
+Date:		April 2020
+Contact:	Gabriel Krisman Bertazi <krisman@collabora.com>
+Description:
+		The latest version of the Unicode Standard supported by
+		this encoding in this kernel.
diff --git a/fs/unicode/utf8-core.c b/fs/unicode/utf8-core.c
index 2a878b7..a075d02 100644
--- a/fs/unicode/utf8-core.c
+++ b/fs/unicode/utf8-core.c
@@ -6,6 +6,7 @@
 #include <linux/parser.h>
 #include <linux/errno.h>
 #include <linux/unicode.h>
+#include <linux/fs.h>
 
 #include "utf8n.h"
 
@@ -212,4 +213,58 @@ void utf8_unload(struct unicode_map *um)
 }
 EXPORT_SYMBOL(utf8_unload);
 
+static ssize_t latest_version_show(struct kobject *kobj,
+				   struct kobj_attribute *attr, char *buf)
+{
+	int l = utf8version_latest();
+
+	return snprintf(buf, PAGE_SIZE, "%d.%d.%d\n",
+			UNICODE_AGE_MAJ(l), UNICODE_AGE_MIN(l),
+			UNICODE_AGE_REV(l));
+}
+
+#define UNICODE_ATTR(x)						\
+	static struct kobj_attribute x ## _attr = __ATTR_RO(x)
+
+UNICODE_ATTR(latest_version);
+
+static struct attribute *encoding_attrs[] = {
+	&latest_version_attr.attr,
+	NULL,
+};
+static const struct attribute_group encoding_attr_group = {
+	.attrs = encoding_attrs,
+};
+
+static struct kobject *unicode_kobj;
+static struct kobject *utf8_kobj;
+
+static int __init unicode_init(void)
+{
+	int ret = 0;
+
+	unicode_kobj = kobject_create_and_add("unicode", fs_kobj);
+	if (!unicode_kobj)
+		return -ENOMEM;
+
+	utf8_kobj = kobject_create_and_add("utf-8", unicode_kobj);
+	if (!utf8_kobj) {
+		ret = -ENOMEM;
+		goto fail_unicode;
+	}
+
+	ret = sysfs_create_group(utf8_kobj, &encoding_attr_group);
+	if (ret)
+		goto fail_utf8;
+
+	return 0;
+
+fail_utf8:
+	kobject_put(utf8_kobj);
+fail_unicode:
+	kobject_put(unicode_kobj);
+	return ret;
+}
+fs_initcall(unicode_init);
+
 MODULE_LICENSE("GPL v2");
diff --git a/fs/unicode/utf8n.h b/fs/unicode/utf8n.h
index 0acd530..a14fa1e 100644
--- a/fs/unicode/utf8n.h
+++ b/fs/unicode/utf8n.h
@@ -21,6 +21,10 @@
 	 ((unsigned int)(MIN) << UNICODE_MIN_SHIFT) |	\
 	 ((unsigned int)(REV)))
 
+#define UNICODE_AGE_MAJ(x) ((x) >> UNICODE_MAJ_SHIFT & 0xff)
+#define UNICODE_AGE_MIN(x) ((x) >> UNICODE_MIN_SHIFT & 0xff)
+#define UNICODE_AGE_REV(x) ((x) & 0xff)
+
 /* Highest unicode version supported by the data tables. */
 extern int utf8version_is_supported(u8 maj, u8 min, u8 rev);
 extern int utf8version_latest(void);