xref: /kernel/linux/linux-5.10/fs/filesystems.c (revision 8c2ecf20)
18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  linux/fs/filesystems.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Copyright (C) 1991, 1992  Linus Torvalds
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci *  table of configured filesystems
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/syscalls.h>
118c2ecf20Sopenharmony_ci#include <linux/fs.h>
128c2ecf20Sopenharmony_ci#include <linux/proc_fs.h>
138c2ecf20Sopenharmony_ci#include <linux/seq_file.h>
148c2ecf20Sopenharmony_ci#include <linux/kmod.h>
158c2ecf20Sopenharmony_ci#include <linux/init.h>
168c2ecf20Sopenharmony_ci#include <linux/module.h>
178c2ecf20Sopenharmony_ci#include <linux/slab.h>
188c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
198c2ecf20Sopenharmony_ci#include <linux/fs_parser.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci/*
228c2ecf20Sopenharmony_ci * Handling of filesystem drivers list.
238c2ecf20Sopenharmony_ci * Rules:
248c2ecf20Sopenharmony_ci *	Inclusion to/removals from/scanning of list are protected by spinlock.
258c2ecf20Sopenharmony_ci *	During the unload module must call unregister_filesystem().
268c2ecf20Sopenharmony_ci *	We can access the fields of list element if:
278c2ecf20Sopenharmony_ci *		1) spinlock is held or
288c2ecf20Sopenharmony_ci *		2) we hold the reference to the module.
298c2ecf20Sopenharmony_ci *	The latter can be guaranteed by call of try_module_get(); if it
308c2ecf20Sopenharmony_ci *	returned 0 we must skip the element, otherwise we got the reference.
318c2ecf20Sopenharmony_ci *	Once the reference is obtained we can drop the spinlock.
328c2ecf20Sopenharmony_ci */
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistatic struct file_system_type *file_systems;
358c2ecf20Sopenharmony_cistatic DEFINE_RWLOCK(file_systems_lock);
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/* WARNING: This can be used only if we _already_ own a reference */
388c2ecf20Sopenharmony_cistruct file_system_type *get_filesystem(struct file_system_type *fs)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	__module_get(fs->owner);
418c2ecf20Sopenharmony_ci	return fs;
428c2ecf20Sopenharmony_ci}
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_civoid put_filesystem(struct file_system_type *fs)
458c2ecf20Sopenharmony_ci{
468c2ecf20Sopenharmony_ci	module_put(fs->owner);
478c2ecf20Sopenharmony_ci}
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cistatic struct file_system_type **find_filesystem(const char *name, unsigned len)
508c2ecf20Sopenharmony_ci{
518c2ecf20Sopenharmony_ci	struct file_system_type **p;
528c2ecf20Sopenharmony_ci	for (p = &file_systems; *p; p = &(*p)->next)
538c2ecf20Sopenharmony_ci		if (strncmp((*p)->name, name, len) == 0 &&
548c2ecf20Sopenharmony_ci		    !(*p)->name[len])
558c2ecf20Sopenharmony_ci			break;
568c2ecf20Sopenharmony_ci	return p;
578c2ecf20Sopenharmony_ci}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci/**
608c2ecf20Sopenharmony_ci *	register_filesystem - register a new filesystem
618c2ecf20Sopenharmony_ci *	@fs: the file system structure
628c2ecf20Sopenharmony_ci *
638c2ecf20Sopenharmony_ci *	Adds the file system passed to the list of file systems the kernel
648c2ecf20Sopenharmony_ci *	is aware of for mount and other syscalls. Returns 0 on success,
658c2ecf20Sopenharmony_ci *	or a negative errno code on an error.
668c2ecf20Sopenharmony_ci *
678c2ecf20Sopenharmony_ci *	The &struct file_system_type that is passed is linked into the kernel
688c2ecf20Sopenharmony_ci *	structures and must not be freed until the file system has been
698c2ecf20Sopenharmony_ci *	unregistered.
708c2ecf20Sopenharmony_ci */
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ciint register_filesystem(struct file_system_type * fs)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	int res = 0;
758c2ecf20Sopenharmony_ci	struct file_system_type ** p;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	if (fs->parameters &&
788c2ecf20Sopenharmony_ci	    !fs_validate_description(fs->name, fs->parameters))
798c2ecf20Sopenharmony_ci		return -EINVAL;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	BUG_ON(strchr(fs->name, '.'));
828c2ecf20Sopenharmony_ci	if (fs->next)
838c2ecf20Sopenharmony_ci		return -EBUSY;
848c2ecf20Sopenharmony_ci	write_lock(&file_systems_lock);
858c2ecf20Sopenharmony_ci	p = find_filesystem(fs->name, strlen(fs->name));
868c2ecf20Sopenharmony_ci	if (*p)
878c2ecf20Sopenharmony_ci		res = -EBUSY;
888c2ecf20Sopenharmony_ci	else
898c2ecf20Sopenharmony_ci		*p = fs;
908c2ecf20Sopenharmony_ci	write_unlock(&file_systems_lock);
918c2ecf20Sopenharmony_ci	return res;
928c2ecf20Sopenharmony_ci}
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ciEXPORT_SYMBOL(register_filesystem);
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci/**
978c2ecf20Sopenharmony_ci *	unregister_filesystem - unregister a file system
988c2ecf20Sopenharmony_ci *	@fs: filesystem to unregister
998c2ecf20Sopenharmony_ci *
1008c2ecf20Sopenharmony_ci *	Remove a file system that was previously successfully registered
1018c2ecf20Sopenharmony_ci *	with the kernel. An error is returned if the file system is not found.
1028c2ecf20Sopenharmony_ci *	Zero is returned on a success.
1038c2ecf20Sopenharmony_ci *
1048c2ecf20Sopenharmony_ci *	Once this function has returned the &struct file_system_type structure
1058c2ecf20Sopenharmony_ci *	may be freed or reused.
1068c2ecf20Sopenharmony_ci */
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ciint unregister_filesystem(struct file_system_type * fs)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	struct file_system_type ** tmp;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	write_lock(&file_systems_lock);
1138c2ecf20Sopenharmony_ci	tmp = &file_systems;
1148c2ecf20Sopenharmony_ci	while (*tmp) {
1158c2ecf20Sopenharmony_ci		if (fs == *tmp) {
1168c2ecf20Sopenharmony_ci			*tmp = fs->next;
1178c2ecf20Sopenharmony_ci			fs->next = NULL;
1188c2ecf20Sopenharmony_ci			write_unlock(&file_systems_lock);
1198c2ecf20Sopenharmony_ci			synchronize_rcu();
1208c2ecf20Sopenharmony_ci			return 0;
1218c2ecf20Sopenharmony_ci		}
1228c2ecf20Sopenharmony_ci		tmp = &(*tmp)->next;
1238c2ecf20Sopenharmony_ci	}
1248c2ecf20Sopenharmony_ci	write_unlock(&file_systems_lock);
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	return -EINVAL;
1278c2ecf20Sopenharmony_ci}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ciEXPORT_SYMBOL(unregister_filesystem);
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci#ifdef CONFIG_SYSFS_SYSCALL
1328c2ecf20Sopenharmony_cistatic int fs_index(const char __user * __name)
1338c2ecf20Sopenharmony_ci{
1348c2ecf20Sopenharmony_ci	struct file_system_type * tmp;
1358c2ecf20Sopenharmony_ci	struct filename *name;
1368c2ecf20Sopenharmony_ci	int err, index;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	name = getname(__name);
1398c2ecf20Sopenharmony_ci	err = PTR_ERR(name);
1408c2ecf20Sopenharmony_ci	if (IS_ERR(name))
1418c2ecf20Sopenharmony_ci		return err;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	err = -EINVAL;
1448c2ecf20Sopenharmony_ci	read_lock(&file_systems_lock);
1458c2ecf20Sopenharmony_ci	for (tmp=file_systems, index=0 ; tmp ; tmp=tmp->next, index++) {
1468c2ecf20Sopenharmony_ci		if (strcmp(tmp->name, name->name) == 0) {
1478c2ecf20Sopenharmony_ci			err = index;
1488c2ecf20Sopenharmony_ci			break;
1498c2ecf20Sopenharmony_ci		}
1508c2ecf20Sopenharmony_ci	}
1518c2ecf20Sopenharmony_ci	read_unlock(&file_systems_lock);
1528c2ecf20Sopenharmony_ci	putname(name);
1538c2ecf20Sopenharmony_ci	return err;
1548c2ecf20Sopenharmony_ci}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_cistatic int fs_name(unsigned int index, char __user * buf)
1578c2ecf20Sopenharmony_ci{
1588c2ecf20Sopenharmony_ci	struct file_system_type * tmp;
1598c2ecf20Sopenharmony_ci	int len, res;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	read_lock(&file_systems_lock);
1628c2ecf20Sopenharmony_ci	for (tmp = file_systems; tmp; tmp = tmp->next, index--)
1638c2ecf20Sopenharmony_ci		if (index <= 0 && try_module_get(tmp->owner))
1648c2ecf20Sopenharmony_ci			break;
1658c2ecf20Sopenharmony_ci	read_unlock(&file_systems_lock);
1668c2ecf20Sopenharmony_ci	if (!tmp)
1678c2ecf20Sopenharmony_ci		return -EINVAL;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	/* OK, we got the reference, so we can safely block */
1708c2ecf20Sopenharmony_ci	len = strlen(tmp->name) + 1;
1718c2ecf20Sopenharmony_ci	res = copy_to_user(buf, tmp->name, len) ? -EFAULT : 0;
1728c2ecf20Sopenharmony_ci	put_filesystem(tmp);
1738c2ecf20Sopenharmony_ci	return res;
1748c2ecf20Sopenharmony_ci}
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_cistatic int fs_maxindex(void)
1778c2ecf20Sopenharmony_ci{
1788c2ecf20Sopenharmony_ci	struct file_system_type * tmp;
1798c2ecf20Sopenharmony_ci	int index;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	read_lock(&file_systems_lock);
1828c2ecf20Sopenharmony_ci	for (tmp = file_systems, index = 0 ; tmp ; tmp = tmp->next, index++)
1838c2ecf20Sopenharmony_ci		;
1848c2ecf20Sopenharmony_ci	read_unlock(&file_systems_lock);
1858c2ecf20Sopenharmony_ci	return index;
1868c2ecf20Sopenharmony_ci}
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci/*
1898c2ecf20Sopenharmony_ci * Whee.. Weird sysv syscall.
1908c2ecf20Sopenharmony_ci */
1918c2ecf20Sopenharmony_ciSYSCALL_DEFINE3(sysfs, int, option, unsigned long, arg1, unsigned long, arg2)
1928c2ecf20Sopenharmony_ci{
1938c2ecf20Sopenharmony_ci	int retval = -EINVAL;
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	switch (option) {
1968c2ecf20Sopenharmony_ci		case 1:
1978c2ecf20Sopenharmony_ci			retval = fs_index((const char __user *) arg1);
1988c2ecf20Sopenharmony_ci			break;
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci		case 2:
2018c2ecf20Sopenharmony_ci			retval = fs_name(arg1, (char __user *) arg2);
2028c2ecf20Sopenharmony_ci			break;
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci		case 3:
2058c2ecf20Sopenharmony_ci			retval = fs_maxindex();
2068c2ecf20Sopenharmony_ci			break;
2078c2ecf20Sopenharmony_ci	}
2088c2ecf20Sopenharmony_ci	return retval;
2098c2ecf20Sopenharmony_ci}
2108c2ecf20Sopenharmony_ci#endif
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ciint __init get_filesystem_list(char *buf)
2138c2ecf20Sopenharmony_ci{
2148c2ecf20Sopenharmony_ci	int len = 0;
2158c2ecf20Sopenharmony_ci	struct file_system_type * tmp;
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	read_lock(&file_systems_lock);
2188c2ecf20Sopenharmony_ci	tmp = file_systems;
2198c2ecf20Sopenharmony_ci	while (tmp && len < PAGE_SIZE - 80) {
2208c2ecf20Sopenharmony_ci		len += sprintf(buf+len, "%s\t%s\n",
2218c2ecf20Sopenharmony_ci			(tmp->fs_flags & FS_REQUIRES_DEV) ? "" : "nodev",
2228c2ecf20Sopenharmony_ci			tmp->name);
2238c2ecf20Sopenharmony_ci		tmp = tmp->next;
2248c2ecf20Sopenharmony_ci	}
2258c2ecf20Sopenharmony_ci	read_unlock(&file_systems_lock);
2268c2ecf20Sopenharmony_ci	return len;
2278c2ecf20Sopenharmony_ci}
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci#ifdef CONFIG_PROC_FS
2308c2ecf20Sopenharmony_cistatic int filesystems_proc_show(struct seq_file *m, void *v)
2318c2ecf20Sopenharmony_ci{
2328c2ecf20Sopenharmony_ci	struct file_system_type * tmp;
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	read_lock(&file_systems_lock);
2358c2ecf20Sopenharmony_ci	tmp = file_systems;
2368c2ecf20Sopenharmony_ci	while (tmp) {
2378c2ecf20Sopenharmony_ci		seq_printf(m, "%s\t%s\n",
2388c2ecf20Sopenharmony_ci			(tmp->fs_flags & FS_REQUIRES_DEV) ? "" : "nodev",
2398c2ecf20Sopenharmony_ci			tmp->name);
2408c2ecf20Sopenharmony_ci		tmp = tmp->next;
2418c2ecf20Sopenharmony_ci	}
2428c2ecf20Sopenharmony_ci	read_unlock(&file_systems_lock);
2438c2ecf20Sopenharmony_ci	return 0;
2448c2ecf20Sopenharmony_ci}
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_cistatic int __init proc_filesystems_init(void)
2478c2ecf20Sopenharmony_ci{
2488c2ecf20Sopenharmony_ci	proc_create_single("filesystems", 0, NULL, filesystems_proc_show);
2498c2ecf20Sopenharmony_ci	return 0;
2508c2ecf20Sopenharmony_ci}
2518c2ecf20Sopenharmony_cimodule_init(proc_filesystems_init);
2528c2ecf20Sopenharmony_ci#endif
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_cistatic struct file_system_type *__get_fs_type(const char *name, int len)
2558c2ecf20Sopenharmony_ci{
2568c2ecf20Sopenharmony_ci	struct file_system_type *fs;
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	read_lock(&file_systems_lock);
2598c2ecf20Sopenharmony_ci	fs = *(find_filesystem(name, len));
2608c2ecf20Sopenharmony_ci	if (fs && !try_module_get(fs->owner))
2618c2ecf20Sopenharmony_ci		fs = NULL;
2628c2ecf20Sopenharmony_ci	read_unlock(&file_systems_lock);
2638c2ecf20Sopenharmony_ci	return fs;
2648c2ecf20Sopenharmony_ci}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_cistruct file_system_type *get_fs_type(const char *name)
2678c2ecf20Sopenharmony_ci{
2688c2ecf20Sopenharmony_ci	struct file_system_type *fs;
2698c2ecf20Sopenharmony_ci	const char *dot = strchr(name, '.');
2708c2ecf20Sopenharmony_ci	int len = dot ? dot - name : strlen(name);
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	fs = __get_fs_type(name, len);
2738c2ecf20Sopenharmony_ci	if (!fs && (request_module("fs-%.*s", len, name) == 0)) {
2748c2ecf20Sopenharmony_ci		fs = __get_fs_type(name, len);
2758c2ecf20Sopenharmony_ci		if (!fs)
2768c2ecf20Sopenharmony_ci			pr_warn_once("request_module fs-%.*s succeeded, but still no fs?\n",
2778c2ecf20Sopenharmony_ci				     len, name);
2788c2ecf20Sopenharmony_ci	}
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	if (dot && fs && !(fs->fs_flags & FS_HAS_SUBTYPE)) {
2818c2ecf20Sopenharmony_ci		put_filesystem(fs);
2828c2ecf20Sopenharmony_ci		fs = NULL;
2838c2ecf20Sopenharmony_ci	}
2848c2ecf20Sopenharmony_ci	return fs;
2858c2ecf20Sopenharmony_ci}
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ciEXPORT_SYMBOL(get_fs_type);
288