2 * linux/fs/filesystems.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * table of configured filesystems
9 #include <linux/syscalls.h>
11 #include <linux/slab.h>
12 #include <linux/kmod.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <asm/uaccess.h>
18 * Handling of filesystem drivers list.
20 * Inclusion to/removals from/scanning of list are protected by spinlock.
21 * During the unload module must call unregister_filesystem().
22 * We can access the fields of list element if:
23 * 1) spinlock is held or
24 * 2) we hold the reference to the module.
25 * The latter can be guaranteed by call of try_module_get(); if it
26 * returned 0 we must skip the element, otherwise we got the reference.
27 * Once the reference is obtained we can drop the spinlock.
30 static struct file_system_type *file_systems;
31 static DEFINE_RWLOCK(file_systems_lock);
33 /* WARNING: This can be used only if we _already_ own a reference */
34 void get_filesystem(struct file_system_type *fs)
36 __module_get(fs->owner);
39 void put_filesystem(struct file_system_type *fs)
41 module_put(fs->owner);
44 static struct file_system_type **find_filesystem(const char *name)
46 struct file_system_type **p;
47 for (p=&file_systems; *p; p=&(*p)->next)
48 if (strcmp((*p)->name,name) == 0)
54 * register_filesystem - register a new filesystem
55 * @fs: the file system structure
57 * Adds the file system passed to the list of file systems the kernel
58 * is aware of for mount and other syscalls. Returns 0 on success,
59 * or a negative errno code on an error.
61 * The &struct file_system_type that is passed is linked into the kernel
62 * structures and must not be freed until the file system has been
66 int register_filesystem(struct file_system_type * fs)
69 struct file_system_type ** p;
73 INIT_LIST_HEAD(&fs->fs_supers);
74 write_lock(&file_systems_lock);
75 p = find_filesystem(fs->name);
80 write_unlock(&file_systems_lock);
84 EXPORT_SYMBOL(register_filesystem);
87 * unregister_filesystem - unregister a file system
88 * @fs: filesystem to unregister
90 * Remove a file system that was previously successfully registered
91 * with the kernel. An error is returned if the file system is not found.
92 * Zero is returned on a success.
94 * Once this function has returned the &struct file_system_type structure
95 * may be freed or reused.
98 int unregister_filesystem(struct file_system_type * fs)
100 struct file_system_type ** tmp;
102 write_lock(&file_systems_lock);
108 write_unlock(&file_systems_lock);
113 write_unlock(&file_systems_lock);
117 EXPORT_SYMBOL(unregister_filesystem);
119 static int fs_index(const char __user * __name)
121 struct file_system_type * tmp;
125 name = getname(__name);
131 read_lock(&file_systems_lock);
132 for (tmp=file_systems, index=0 ; tmp ; tmp=tmp->next, index++) {
133 if (strcmp(tmp->name,name) == 0) {
138 read_unlock(&file_systems_lock);
143 static int fs_name(unsigned int index, char __user * buf)
145 struct file_system_type * tmp;
148 read_lock(&file_systems_lock);
149 for (tmp = file_systems; tmp; tmp = tmp->next, index--)
150 if (index <= 0 && try_module_get(tmp->owner))
152 read_unlock(&file_systems_lock);
156 /* OK, we got the reference, so we can safely block */
157 len = strlen(tmp->name) + 1;
158 res = copy_to_user(buf, tmp->name, len) ? -EFAULT : 0;
163 static int fs_maxindex(void)
165 struct file_system_type * tmp;
168 read_lock(&file_systems_lock);
169 for (tmp = file_systems, index = 0 ; tmp ; tmp = tmp->next, index++)
171 read_unlock(&file_systems_lock);
176 * Whee.. Weird sysv syscall.
178 asmlinkage long sys_sysfs(int option, unsigned long arg1, unsigned long arg2)
180 int retval = -EINVAL;
184 retval = fs_index((const char __user *) arg1);
188 retval = fs_name(arg1, (char __user *) arg2);
192 retval = fs_maxindex();
198 int get_filesystem_list(char * buf)
201 struct file_system_type * tmp;
203 read_lock(&file_systems_lock);
205 while (tmp && len < PAGE_SIZE - 80) {
206 len += sprintf(buf+len, "%s\t%s\n",
207 (tmp->fs_flags & FS_REQUIRES_DEV) ? "" : "nodev",
211 read_unlock(&file_systems_lock);
215 struct file_system_type *get_fs_type(const char *name)
217 struct file_system_type *fs;
219 read_lock(&file_systems_lock);
220 fs = *(find_filesystem(name));
221 if (fs && !try_module_get(fs->owner))
223 read_unlock(&file_systems_lock);
224 if (!fs && (request_module("%s", name) == 0)) {
225 read_lock(&file_systems_lock);
226 fs = *(find_filesystem(name));
227 if (fs && !try_module_get(fs->owner))
229 read_unlock(&file_systems_lock);
234 EXPORT_SYMBOL(get_fs_type);