filesystems.c 6.5 KB

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