2 * linux/fs/filesystems.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * table of configured filesystems
10 #include <linux/slab.h>
11 #include <linux/kmod.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <asm/uaccess.h>
17 * Handling of filesystem drivers list.
19 * Inclusion to/removals from/scanning of list are protected by spinlock.
20 * During the unload module must call unregister_filesystem().
21 * We can access the fields of list element if:
22 * 1) spinlock is held or
23 * 2) we hold the reference to the module.
24 * The latter can be guaranteed by call of try_module_get(); if it
25 * returned 0 we must skip the element, otherwise we got the reference.
26 * Once the reference is obtained we can drop the spinlock.
29 static struct file_system_type
*file_systems
;
30 static rwlock_t file_systems_lock
= RW_LOCK_UNLOCKED
;
32 /* WARNING: This can be used only if we _already_ own a reference */
33 void get_filesystem(struct file_system_type
*fs
)
35 __module_get(fs
->owner
);
38 void put_filesystem(struct file_system_type
*fs
)
40 module_put(fs
->owner
);
43 static struct file_system_type
**find_filesystem(const char *name
)
45 struct file_system_type
**p
;
46 for (p
=&file_systems
; *p
; p
=&(*p
)->next
)
47 if (strcmp((*p
)->name
,name
) == 0)
53 * register_filesystem - register a new filesystem
54 * @fs: the file system structure
56 * Adds the file system passed to the list of file systems the kernel
57 * is aware of for mount and other syscalls. Returns 0 on success,
58 * or a negative errno code on an error.
60 * The &struct file_system_type that is passed is linked into the kernel
61 * structures and must not be freed until the file system has been
65 int register_filesystem(struct file_system_type
* fs
)
68 struct file_system_type
** p
;
74 INIT_LIST_HEAD(&fs
->fs_supers
);
75 write_lock(&file_systems_lock
);
76 p
= find_filesystem(fs
->name
);
81 write_unlock(&file_systems_lock
);
86 * unregister_filesystem - unregister a file system
87 * @fs: filesystem to unregister
89 * Remove a file system that was previously successfully registered
90 * with the kernel. An error is returned if the file system is not found.
91 * Zero is returned on a success.
93 * Once this function has returned the &struct file_system_type structure
94 * may be freed or reused.
97 int unregister_filesystem(struct file_system_type
* fs
)
99 struct file_system_type
** tmp
;
101 write_lock(&file_systems_lock
);
107 write_unlock(&file_systems_lock
);
112 write_unlock(&file_systems_lock
);
116 static int fs_index(const char __user
* __name
)
118 struct file_system_type
* tmp
;
122 name
= getname(__name
);
128 read_lock(&file_systems_lock
);
129 for (tmp
=file_systems
, index
=0 ; tmp
; tmp
=tmp
->next
, index
++) {
130 if (strcmp(tmp
->name
,name
) == 0) {
135 read_unlock(&file_systems_lock
);
140 static int fs_name(unsigned int index
, char __user
* buf
)
142 struct file_system_type
* tmp
;
145 read_lock(&file_systems_lock
);
146 for (tmp
= file_systems
; tmp
; tmp
= tmp
->next
, index
--)
147 if (index
<= 0 && try_module_get(tmp
->owner
))
149 read_unlock(&file_systems_lock
);
153 /* OK, we got the reference, so we can safely block */
154 len
= strlen(tmp
->name
) + 1;
155 res
= copy_to_user(buf
, tmp
->name
, len
) ? -EFAULT
: 0;
160 static int fs_maxindex(void)
162 struct file_system_type
* tmp
;
165 read_lock(&file_systems_lock
);
166 for (tmp
= file_systems
, index
= 0 ; tmp
; tmp
= tmp
->next
, index
++)
168 read_unlock(&file_systems_lock
);
173 * Whee.. Weird sysv syscall.
175 asmlinkage
long sys_sysfs(int option
, unsigned long arg1
, unsigned long arg2
)
177 int retval
= -EINVAL
;
181 retval
= fs_index((const char __user
*) arg1
);
185 retval
= fs_name(arg1
, (char __user
*) arg2
);
189 retval
= fs_maxindex();
195 int get_filesystem_list(char * buf
)
198 struct file_system_type
* tmp
;
200 read_lock(&file_systems_lock
);
202 while (tmp
&& len
< PAGE_SIZE
- 80) {
203 len
+= sprintf(buf
+len
, "%s\t%s\n",
204 (tmp
->fs_flags
& FS_REQUIRES_DEV
) ? "" : "nodev",
208 read_unlock(&file_systems_lock
);
212 struct file_system_type
*get_fs_type(const char *name
)
214 struct file_system_type
*fs
;
216 read_lock(&file_systems_lock
);
217 fs
= *(find_filesystem(name
));
218 if (fs
&& !try_module_get(fs
->owner
))
220 read_unlock(&file_systems_lock
);
221 if (!fs
&& (request_module("%s", name
) == 0)) {
222 read_lock(&file_systems_lock
);
223 fs
= *(find_filesystem(name
));
224 if (fs
&& !try_module_get(fs
->owner
))
226 read_unlock(&file_systems_lock
);