1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  linux/fs/filesystems.c
4  *
5  *  Copyright (C) 1991, 1992  Linus Torvalds
6  *
7  *  table of configured filesystems
8  */
9 
10 #include <linux/syscalls.h>
11 #include <linux/fs.h>
12 #include <linux/proc_fs.h>
13 #include <linux/seq_file.h>
14 #include <linux/kmod.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/uaccess.h>
19 
20 /*
21  * Handling of filesystem drivers list.
22  * Rules:
23  *	Inclusion to/removals from/scanning of list are protected by spinlock.
24  *	During the unload module must call unregister_filesystem().
25  *	We can access the fields of list element if:
26  *		1) spinlock is held or
27  *		2) we hold the reference to the module.
28  *	The latter can be guaranteed by call of try_module_get(); if it
29  *	returned 0 we must skip the element, otherwise we got the reference.
30  *	Once the reference is obtained we can drop the spinlock.
31  */
32 
33 static struct file_system_type *file_systems;
34 static DEFINE_RWLOCK(file_systems_lock);
35 
36 /* WARNING: This can be used only if we _already_ own a reference */
get_filesystem(struct file_system_type * fs)37 struct file_system_type *get_filesystem(struct file_system_type *fs)
38 {
39 	__module_get(fs->owner);
40 	return fs;
41 }
42 
put_filesystem(struct file_system_type * fs)43 void put_filesystem(struct file_system_type *fs)
44 {
45 	module_put(fs->owner);
46 }
47 
find_filesystem(const char * name,unsigned len)48 static struct file_system_type **find_filesystem(const char *name, unsigned len)
49 {
50 	struct file_system_type **p;
51 	for (p = &file_systems; *p; p = &(*p)->next)
52 		if (strncmp((*p)->name, name, len) == 0 &&
53 		    !(*p)->name[len])
54 			break;
55 	return p;
56 }
57 
58 /**
59  *	register_filesystem - register a new filesystem
60  *	@fs: the file system structure
61  *
62  *	Adds the file system passed to the list of file systems the kernel
63  *	is aware of for mount and other syscalls. Returns 0 on success,
64  *	or a negative errno code on an error.
65  *
66  *	The &struct file_system_type that is passed is linked into the kernel
67  *	structures and must not be freed until the file system has been
68  *	unregistered.
69  */
70 
register_filesystem(struct file_system_type * fs)71 int register_filesystem(struct file_system_type * fs)
72 {
73 	int res = 0;
74 	struct file_system_type ** p;
75 
76 	BUG_ON(strchr(fs->name, '.'));
77 	if (fs->next)
78 		return -EBUSY;
79 	write_lock(&file_systems_lock);
80 	p = find_filesystem(fs->name, strlen(fs->name));
81 	if (*p)
82 		res = -EBUSY;
83 	else
84 		*p = fs;
85 	write_unlock(&file_systems_lock);
86 	return res;
87 }
88 
89 EXPORT_SYMBOL(register_filesystem);
90 
91 /**
92  *	unregister_filesystem - unregister a file system
93  *	@fs: filesystem to unregister
94  *
95  *	Remove a file system that was previously successfully registered
96  *	with the kernel. An error is returned if the file system is not found.
97  *	Zero is returned on a success.
98  *
99  *	Once this function has returned the &struct file_system_type structure
100  *	may be freed or reused.
101  */
102 
unregister_filesystem(struct file_system_type * fs)103 int unregister_filesystem(struct file_system_type * fs)
104 {
105 	struct file_system_type ** tmp;
106 
107 	write_lock(&file_systems_lock);
108 	tmp = &file_systems;
109 	while (*tmp) {
110 		if (fs == *tmp) {
111 			*tmp = fs->next;
112 			fs->next = NULL;
113 			write_unlock(&file_systems_lock);
114 			synchronize_rcu();
115 			return 0;
116 		}
117 		tmp = &(*tmp)->next;
118 	}
119 	write_unlock(&file_systems_lock);
120 
121 	return -EINVAL;
122 }
123 
124 EXPORT_SYMBOL(unregister_filesystem);
125 
126 #ifdef CONFIG_SYSFS_SYSCALL
fs_index(const char __user * __name)127 static int fs_index(const char __user * __name)
128 {
129 	struct file_system_type * tmp;
130 	struct filename *name;
131 	int err, index;
132 
133 	name = getname(__name);
134 	err = PTR_ERR(name);
135 	if (IS_ERR(name))
136 		return err;
137 
138 	err = -EINVAL;
139 	read_lock(&file_systems_lock);
140 	for (tmp=file_systems, index=0 ; tmp ; tmp=tmp->next, index++) {
141 		if (strcmp(tmp->name, name->name) == 0) {
142 			err = index;
143 			break;
144 		}
145 	}
146 	read_unlock(&file_systems_lock);
147 	putname(name);
148 	return err;
149 }
150 
fs_name(unsigned int index,char __user * buf)151 static int fs_name(unsigned int index, char __user * buf)
152 {
153 	struct file_system_type * tmp;
154 	int len, res;
155 
156 	read_lock(&file_systems_lock);
157 	for (tmp = file_systems; tmp; tmp = tmp->next, index--)
158 		if (index <= 0 && try_module_get(tmp->owner))
159 			break;
160 	read_unlock(&file_systems_lock);
161 	if (!tmp)
162 		return -EINVAL;
163 
164 	/* OK, we got the reference, so we can safely block */
165 	len = strlen(tmp->name) + 1;
166 	res = copy_to_user(buf, tmp->name, len) ? -EFAULT : 0;
167 	put_filesystem(tmp);
168 	return res;
169 }
170 
fs_maxindex(void)171 static int fs_maxindex(void)
172 {
173 	struct file_system_type * tmp;
174 	int index;
175 
176 	read_lock(&file_systems_lock);
177 	for (tmp = file_systems, index = 0 ; tmp ; tmp = tmp->next, index++)
178 		;
179 	read_unlock(&file_systems_lock);
180 	return index;
181 }
182 
183 /*
184  * Whee.. Weird sysv syscall.
185  */
SYSCALL_DEFINE3(sysfs,int,option,unsigned long,arg1,unsigned long,arg2)186 SYSCALL_DEFINE3(sysfs, int, option, unsigned long, arg1, unsigned long, arg2)
187 {
188 	int retval = -EINVAL;
189 
190 	switch (option) {
191 		case 1:
192 			retval = fs_index((const char __user *) arg1);
193 			break;
194 
195 		case 2:
196 			retval = fs_name(arg1, (char __user *) arg2);
197 			break;
198 
199 		case 3:
200 			retval = fs_maxindex();
201 			break;
202 	}
203 	return retval;
204 }
205 #endif
206 
get_filesystem_list(char * buf)207 int __init get_filesystem_list(char *buf)
208 {
209 	int len = 0;
210 	struct file_system_type * tmp;
211 
212 	read_lock(&file_systems_lock);
213 	tmp = file_systems;
214 	while (tmp && len < PAGE_SIZE - 80) {
215 		len += sprintf(buf+len, "%s\t%s\n",
216 			(tmp->fs_flags & FS_REQUIRES_DEV) ? "" : "nodev",
217 			tmp->name);
218 		tmp = tmp->next;
219 	}
220 	read_unlock(&file_systems_lock);
221 	return len;
222 }
223 
224 #ifdef CONFIG_PROC_FS
filesystems_proc_show(struct seq_file * m,void * v)225 static int filesystems_proc_show(struct seq_file *m, void *v)
226 {
227 	struct file_system_type * tmp;
228 
229 	read_lock(&file_systems_lock);
230 	tmp = file_systems;
231 	while (tmp) {
232 		seq_printf(m, "%s\t%s\n",
233 			(tmp->fs_flags & FS_REQUIRES_DEV) ? "" : "nodev",
234 			tmp->name);
235 		tmp = tmp->next;
236 	}
237 	read_unlock(&file_systems_lock);
238 	return 0;
239 }
240 
proc_filesystems_init(void)241 static int __init proc_filesystems_init(void)
242 {
243 	proc_create_single("filesystems", 0, NULL, filesystems_proc_show);
244 	return 0;
245 }
246 module_init(proc_filesystems_init);
247 #endif
248 
__get_fs_type(const char * name,int len)249 static struct file_system_type *__get_fs_type(const char *name, int len)
250 {
251 	struct file_system_type *fs;
252 
253 	read_lock(&file_systems_lock);
254 	fs = *(find_filesystem(name, len));
255 	if (fs && !try_module_get(fs->owner))
256 		fs = NULL;
257 	read_unlock(&file_systems_lock);
258 	return fs;
259 }
260 
get_fs_type(const char * name)261 struct file_system_type *get_fs_type(const char *name)
262 {
263 	struct file_system_type *fs;
264 	const char *dot = strchr(name, '.');
265 	int len = dot ? dot - name : strlen(name);
266 
267 	fs = __get_fs_type(name, len);
268 	if (!fs && (request_module("fs-%.*s", len, name) == 0)) {
269 		fs = __get_fs_type(name, len);
270 		if (!fs)
271 			pr_warn_once("request_module fs-%.*s succeeded, but still no fs?\n",
272 				     len, name);
273 	}
274 
275 	if (dot && fs && !(fs->fs_flags & FS_HAS_SUBTYPE)) {
276 		put_filesystem(fs);
277 		fs = NULL;
278 	}
279 	return fs;
280 }
281 
282 EXPORT_SYMBOL(get_fs_type);
283