xref: /linux/fs/sysfs/mount.c (revision bf74b964775009071cf12f9d59d4dd5e388fbe0b)
1 /*
2  * mount.c - operations for initializing and mounting sysfs.
3  */
4 
5 #define DEBUG
6 
7 #include <linux/fs.h>
8 #include <linux/mount.h>
9 #include <linux/pagemap.h>
10 #include <linux/init.h>
11 #include <asm/semaphore.h>
12 
13 #include "sysfs.h"
14 
15 /* Random magic number */
16 #define SYSFS_MAGIC 0x62656572
17 
18 struct vfsmount *sysfs_mount;
19 struct super_block * sysfs_sb = NULL;
20 struct kmem_cache *sysfs_dir_cachep;
21 
22 static void sysfs_clear_inode(struct inode *inode);
23 
24 static const struct super_operations sysfs_ops = {
25 	.statfs		= simple_statfs,
26 	.drop_inode	= sysfs_delete_inode,
27 	.clear_inode	= sysfs_clear_inode,
28 };
29 
30 static struct sysfs_dirent sysfs_root = {
31 	.s_sibling	= LIST_HEAD_INIT(sysfs_root.s_sibling),
32 	.s_children	= LIST_HEAD_INIT(sysfs_root.s_children),
33 	.s_element	= NULL,
34 	.s_type		= SYSFS_ROOT,
35 	.s_iattr	= NULL,
36 	.s_ino		= 1,
37 };
38 
39 static void sysfs_clear_inode(struct inode *inode)
40 {
41 	kfree(inode->i_private);
42 }
43 
44 static int sysfs_fill_super(struct super_block *sb, void *data, int silent)
45 {
46 	struct inode *inode;
47 	struct dentry *root;
48 
49 	sb->s_blocksize = PAGE_CACHE_SIZE;
50 	sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
51 	sb->s_magic = SYSFS_MAGIC;
52 	sb->s_op = &sysfs_ops;
53 	sb->s_time_gran = 1;
54 	sysfs_sb = sb;
55 
56 	inode = sysfs_new_inode(S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO,
57 				 &sysfs_root);
58 	if (inode) {
59 		inode->i_op = &sysfs_dir_inode_operations;
60 		inode->i_fop = &sysfs_dir_operations;
61 		/* directory inodes start off with i_nlink == 2 (for "." entry) */
62 		inc_nlink(inode);
63 	} else {
64 		pr_debug("sysfs: could not get root inode\n");
65 		return -ENOMEM;
66 	}
67 
68 	root = d_alloc_root(inode);
69 	if (!root) {
70 		pr_debug("%s: could not get root dentry!\n",__FUNCTION__);
71 		iput(inode);
72 		return -ENOMEM;
73 	}
74 	root->d_fsdata = &sysfs_root;
75 	sb->s_root = root;
76 	return 0;
77 }
78 
79 static int sysfs_get_sb(struct file_system_type *fs_type,
80 	int flags, const char *dev_name, void *data, struct vfsmount *mnt)
81 {
82 	return get_sb_single(fs_type, flags, data, sysfs_fill_super, mnt);
83 }
84 
85 static struct file_system_type sysfs_fs_type = {
86 	.name		= "sysfs",
87 	.get_sb		= sysfs_get_sb,
88 	.kill_sb	= kill_litter_super,
89 };
90 
91 int __init sysfs_init(void)
92 {
93 	int err = -ENOMEM;
94 
95 	sysfs_dir_cachep = kmem_cache_create("sysfs_dir_cache",
96 					      sizeof(struct sysfs_dirent),
97 					      0, 0, NULL, NULL);
98 	if (!sysfs_dir_cachep)
99 		goto out;
100 
101 	err = register_filesystem(&sysfs_fs_type);
102 	if (!err) {
103 		sysfs_mount = kern_mount(&sysfs_fs_type);
104 		if (IS_ERR(sysfs_mount)) {
105 			printk(KERN_ERR "sysfs: could not mount!\n");
106 			err = PTR_ERR(sysfs_mount);
107 			sysfs_mount = NULL;
108 			unregister_filesystem(&sysfs_fs_type);
109 			goto out_err;
110 		}
111 	} else
112 		goto out_err;
113 out:
114 	return err;
115 out_err:
116 	kmem_cache_destroy(sysfs_dir_cachep);
117 	sysfs_dir_cachep = NULL;
118 	goto out;
119 }
120