Merge branch 'upstream' of git://ftp.linux-mips.org/pub/scm/upstream-linus
[linux-2.6-block.git] / fs / sysfs / mount.c
CommitLineData
1da177e4 1/*
6d66f5cd
TH
2 * fs/sysfs/symlink.c - operations for initializing and mounting sysfs
3 *
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
7 *
8 * This file is released under the GPLv2.
9 *
10 * Please see Documentation/filesystems/sysfs.txt for more information.
1da177e4
LT
11 */
12
13#define DEBUG
14
15#include <linux/fs.h>
16#include <linux/mount.h>
17#include <linux/pagemap.h>
18#include <linux/init.h>
f1282c84 19#include <linux/module.h>
8231f2f9 20#include <linux/magic.h>
1da177e4
LT
21
22#include "sysfs.h"
23
1da177e4 24
7d0c7d67 25static struct vfsmount *sysfs_mount;
1da177e4 26struct super_block * sysfs_sb = NULL;
e18b890b 27struct kmem_cache *sysfs_dir_cachep;
1da177e4 28
ee9b6d61 29static const struct super_operations sysfs_ops = {
1da177e4 30 .statfs = simple_statfs,
90bc6135 31 .drop_inode = generic_delete_inode,
04256b4a 32 .delete_inode = sysfs_delete_inode,
1da177e4
LT
33};
34
51225039 35struct sysfs_dirent sysfs_root = {
dc2f75f0 36 .s_name = "",
13b3086d 37 .s_count = ATOMIC_INIT(1),
dc2f75f0 38 .s_flags = SYSFS_DIR,
fc9f54b9 39 .s_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO,
dc351252 40 .s_ino = 1,
1da177e4
LT
41};
42
43static int sysfs_fill_super(struct super_block *sb, void *data, int silent)
44{
45 struct inode *inode;
46 struct dentry *root;
47
48 sb->s_blocksize = PAGE_CACHE_SIZE;
49 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
50 sb->s_magic = SYSFS_MAGIC;
51 sb->s_op = &sysfs_ops;
52 sb->s_time_gran = 1;
53 sysfs_sb = sb;
54
e080e436 55 /* get root inode, initialize and unlock it */
4a67a1bc 56 mutex_lock(&sysfs_mutex);
e080e436 57 inode = sysfs_get_inode(&sysfs_root);
4a67a1bc 58 mutex_unlock(&sysfs_mutex);
fc9f54b9 59 if (!inode) {
1da177e4
LT
60 pr_debug("sysfs: could not get root inode\n");
61 return -ENOMEM;
62 }
63
e080e436 64 /* instantiate and link root dentry */
1da177e4
LT
65 root = d_alloc_root(inode);
66 if (!root) {
8e24eea7 67 pr_debug("%s: could not get root dentry!\n",__func__);
1da177e4
LT
68 iput(inode);
69 return -ENOMEM;
70 }
71 root->d_fsdata = &sysfs_root;
72 sb->s_root = root;
73 return 0;
74}
75
454e2398
DH
76static int sysfs_get_sb(struct file_system_type *fs_type,
77 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
1da177e4 78{
454e2398 79 return get_sb_single(fs_type, flags, data, sysfs_fill_super, mnt);
1da177e4
LT
80}
81
82static struct file_system_type sysfs_fs_type = {
83 .name = "sysfs",
84 .get_sb = sysfs_get_sb,
0333cd8a 85 .kill_sb = kill_anon_super,
1da177e4
LT
86};
87
88int __init sysfs_init(void)
89{
90 int err = -ENOMEM;
91
92 sysfs_dir_cachep = kmem_cache_create("sysfs_dir_cache",
93 sizeof(struct sysfs_dirent),
20c2df83 94 0, 0, NULL);
1da177e4
LT
95 if (!sysfs_dir_cachep)
96 goto out;
97
e0bf68dd
PZ
98 err = sysfs_inode_init();
99 if (err)
100 goto out_err;
101
1da177e4
LT
102 err = register_filesystem(&sysfs_fs_type);
103 if (!err) {
104 sysfs_mount = kern_mount(&sysfs_fs_type);
105 if (IS_ERR(sysfs_mount)) {
106 printk(KERN_ERR "sysfs: could not mount!\n");
107 err = PTR_ERR(sysfs_mount);
108 sysfs_mount = NULL;
109 unregister_filesystem(&sysfs_fs_type);
110 goto out_err;
111 }
112 } else
113 goto out_err;
114out:
115 return err;
116out_err:
117 kmem_cache_destroy(sysfs_dir_cachep);
118 sysfs_dir_cachep = NULL;
119 goto out;
120}
f1282c84
NB
121
122#undef sysfs_get
123struct sysfs_dirent *sysfs_get(struct sysfs_dirent *sd)
124{
125 return __sysfs_get(sd);
126}
127EXPORT_SYMBOL_GPL(sysfs_get);
128
129#undef sysfs_put
130void sysfs_put(struct sysfs_dirent *sd)
131{
132 __sysfs_put(sd);
133}
134EXPORT_SYMBOL_GPL(sysfs_put);