1
2
3
4
5
6
7
8
9#include <linux/fs.h>
10#include <linux/slab.h>
11#include <linux/kmod.h>
12#include <linux/module.h>
13#include <asm/uaccess.h>
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28static struct file_system_type *file_systems;
29static rwlock_t file_systems_lock = RW_LOCK_UNLOCKED;
30
31
32void get_filesystem(struct file_system_type *fs)
33{
34 if (fs->owner)
35 __MOD_INC_USE_COUNT(fs->owner);
36}
37
38void put_filesystem(struct file_system_type *fs)
39{
40 if (fs->owner)
41 __MOD_DEC_USE_COUNT(fs->owner);
42}
43
44static struct file_system_type **find_filesystem(const char *name)
45{
46 struct file_system_type **p;
47 for (p=&file_systems; *p; p=&(*p)->next)
48 if (strcmp((*p)->name,name) == 0)
49 break;
50 return p;
51}
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66int register_filesystem(struct file_system_type * fs)
67{
68 int res = 0;
69 struct file_system_type ** p;
70
71 if (!fs)
72 return -EINVAL;
73 if (fs->next)
74 return -EBUSY;
75 INIT_LIST_HEAD(&fs->fs_supers);
76 write_lock(&file_systems_lock);
77 p = find_filesystem(fs->name);
78 if (*p)
79 res = -EBUSY;
80 else
81 *p = fs;
82 write_unlock(&file_systems_lock);
83 return res;
84}
85
86
87
88
89
90
91
92
93
94
95
96
97
98int unregister_filesystem(struct file_system_type * fs)
99{
100 struct file_system_type ** tmp;
101
102 write_lock(&file_systems_lock);
103 tmp = &file_systems;
104 while (*tmp) {
105 if (fs == *tmp) {
106 *tmp = fs->next;
107 fs->next = NULL;
108 write_unlock(&file_systems_lock);
109 return 0;
110 }
111 tmp = &(*tmp)->next;
112 }
113 write_unlock(&file_systems_lock);
114 return -EINVAL;
115}
116
117static int fs_index(const char * __name)
118{
119 struct file_system_type * tmp;
120 char * name;
121 int err, index;
122
123 name = getname(__name);
124 err = PTR_ERR(name);
125 if (IS_ERR(name))
126 return err;
127
128 err = -EINVAL;
129 read_lock(&file_systems_lock);
130 for (tmp=file_systems, index=0 ; tmp ; tmp=tmp->next, index++) {
131 if (strcmp(tmp->name,name) == 0) {
132 err = index;
133 break;
134 }
135 }
136 read_unlock(&file_systems_lock);
137 putname(name);
138 return err;
139}
140
141static int fs_name(unsigned int index, char * buf)
142{
143 struct file_system_type * tmp;
144 int len, res;
145
146 read_lock(&file_systems_lock);
147 for (tmp = file_systems; tmp; tmp = tmp->next, index--)
148 if (index <= 0 && try_inc_mod_count(tmp->owner))
149 break;
150 read_unlock(&file_systems_lock);
151 if (!tmp)
152 return -EINVAL;
153
154
155 len = strlen(tmp->name) + 1;
156 res = copy_to_user(buf, tmp->name, len) ? -EFAULT : 0;
157 put_filesystem(tmp);
158 return res;
159}
160
161static int fs_maxindex(void)
162{
163 struct file_system_type * tmp;
164 int index;
165
166 read_lock(&file_systems_lock);
167 for (tmp = file_systems, index = 0 ; tmp ; tmp = tmp->next, index++)
168 ;
169 read_unlock(&file_systems_lock);
170 return index;
171}
172
173
174
175
176asmlinkage long sys_sysfs(int option, unsigned long arg1, unsigned long arg2)
177{
178 int retval = -EINVAL;
179
180 switch (option) {
181 case 1:
182 retval = fs_index((const char *) arg1);
183 break;
184
185 case 2:
186 retval = fs_name(arg1, (char *) arg2);
187 break;
188
189 case 3:
190 retval = fs_maxindex();
191 break;
192 }
193 return retval;
194}
195
196int get_filesystem_list(char * buf)
197{
198 int len = 0;
199 struct file_system_type * tmp;
200
201 read_lock(&file_systems_lock);
202 tmp = file_systems;
203 while (tmp && len < PAGE_SIZE - 80) {
204 len += sprintf(buf+len, "%s\t%s\n",
205 (tmp->fs_flags & FS_REQUIRES_DEV) ? "" : "nodev",
206 tmp->name);
207 tmp = tmp->next;
208 }
209 read_unlock(&file_systems_lock);
210 return len;
211}
212
213struct file_system_type *get_fs_type(const char *name)
214{
215 struct file_system_type *fs;
216
217 read_lock(&file_systems_lock);
218 fs = *(find_filesystem(name));
219 if (fs && !try_inc_mod_count(fs->owner))
220 fs = NULL;
221 read_unlock(&file_systems_lock);
222 if (!fs && (request_module(name) == 0)) {
223 read_lock(&file_systems_lock);
224 fs = *(find_filesystem(name));
225 if (fs && !try_inc_mod_count(fs->owner))
226 fs = NULL;
227 read_unlock(&file_systems_lock);
228 }
229 return fs;
230}
231