1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright 2018 Google LLC
4 */
5 #include <linux/fs.h>
6 #include <linux/init.h>
7 #include <linux/module.h>
8
9 #include <uapi/linux/incrementalfs.h>
10
11 #include "vfs.h"
12
13 #define INCFS_NODE_FEATURES "features"
14
15 static struct file_system_type incfs_fs_type = {
16 .owner = THIS_MODULE,
17 .name = INCFS_NAME,
18 .mount = incfs_mount_fs,
19 .kill_sb = incfs_kill_sb,
20 .fs_flags = 0
21 };
22
23 static struct kobject *sysfs_root, *featurefs_root;
24
corefs_show(struct kobject * kobj,struct kobj_attribute * attr,char * buff)25 static ssize_t corefs_show(struct kobject *kobj,
26 struct kobj_attribute *attr, char *buff)
27 {
28 return snprintf(buff, PAGE_SIZE, "supported\n");
29 }
30
31 static struct kobj_attribute corefs_attr = __ATTR_RO(corefs);
32
33 static struct attribute *attributes[] = {
34 &corefs_attr.attr,
35 NULL,
36 };
37
38 static const struct attribute_group attr_group = {
39 .attrs = attributes,
40 };
41
init_sysfs(void)42 static int __init init_sysfs(void)
43 {
44 int res = 0;
45
46 sysfs_root = kobject_create_and_add(INCFS_NAME, fs_kobj);
47 if (!sysfs_root)
48 return -ENOMEM;
49
50 featurefs_root = kobject_create_and_add(INCFS_NODE_FEATURES,
51 sysfs_root);
52 if (!featurefs_root)
53 return -ENOMEM;
54
55 res = sysfs_create_group(featurefs_root, &attr_group);
56 if (res) {
57 kobject_put(sysfs_root);
58 sysfs_root = NULL;
59 }
60 return res;
61 }
62
cleanup_sysfs(void)63 static void cleanup_sysfs(void)
64 {
65 if (featurefs_root) {
66 sysfs_remove_group(featurefs_root, &attr_group);
67 kobject_put(featurefs_root);
68 featurefs_root = NULL;
69 }
70
71 if (sysfs_root) {
72 kobject_put(sysfs_root);
73 sysfs_root = NULL;
74 }
75 }
76
init_incfs_module(void)77 static int __init init_incfs_module(void)
78 {
79 int err = 0;
80
81 err = init_sysfs();
82 if (err)
83 return err;
84
85 err = register_filesystem(&incfs_fs_type);
86 if (err)
87 cleanup_sysfs();
88
89 return err;
90 }
91
cleanup_incfs_module(void)92 static void __exit cleanup_incfs_module(void)
93 {
94 cleanup_sysfs();
95 unregister_filesystem(&incfs_fs_type);
96 }
97
98 module_init(init_incfs_module);
99 module_exit(cleanup_incfs_module);
100
101 MODULE_LICENSE("GPL v2");
102 MODULE_AUTHOR("Eugene Zemtsov <ezemtsov@google.com>");
103 MODULE_DESCRIPTION("Incremental File System");
104