filesystems.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. struct filename *name;
  119. int err, index;
  120. name = getname(__name);
  121. err = PTR_ERR(name);
  122. if (IS_ERR(name))
  123. return err;
  124. err = -EINVAL;
  125. read_lock(&file_systems_lock);
  126. for (tmp=file_systems, index=0 ; tmp ; tmp=tmp->next, index++) {
  127. if (strcmp(tmp->name, name->name) == 0) {
  128. err = index;
  129. break;
  130. }
  131. }
  132. read_unlock(&file_systems_lock);
  133. putname(name);
  134. return err;
  135. }
  136. static int fs_name(unsigned int index, char __user * buf)
  137. {
  138. struct file_system_type * tmp;
  139. int len, res;
  140. read_lock(&file_systems_lock);
  141. for (tmp = file_systems; tmp; tmp = tmp->next, index--)
  142. if (index <= 0 && try_module_get(tmp->owner))
  143. break;
  144. read_unlock(&file_systems_lock);
  145. if (!tmp)
  146. return -EINVAL;
  147. /* OK, we got the reference, so we can safely block */
  148. len = strlen(tmp->name) + 1;
  149. res = copy_to_user(buf, tmp->name, len) ? -EFAULT : 0;
  150. put_filesystem(tmp);
  151. return res;
  152. }
  153. static int fs_maxindex(void)
  154. {
  155. struct file_system_type * tmp;
  156. int index;
  157. read_lock(&file_systems_lock);
  158. for (tmp = file_systems, index = 0 ; tmp ; tmp = tmp->next, index++)
  159. ;
  160. read_unlock(&file_systems_lock);
  161. return index;
  162. }
  163. /*
  164. * Whee.. Weird sysv syscall.
  165. */
  166. SYSCALL_DEFINE3(sysfs, int, option, unsigned long, arg1, unsigned long, arg2)
  167. {
  168. int retval = -EINVAL;
  169. switch (option) {
  170. case 1:
  171. retval = fs_index((const char __user *) arg1);
  172. break;
  173. case 2:
  174. retval = fs_name(arg1, (char __user *) arg2);
  175. break;
  176. case 3:
  177. retval = fs_maxindex();
  178. break;
  179. }
  180. return retval;
  181. }
  182. #endif
  183. int __init get_filesystem_list(char *buf)
  184. {
  185. int len = 0;
  186. struct file_system_type * tmp;
  187. read_lock(&file_systems_lock);
  188. tmp = file_systems;
  189. while (tmp && len < PAGE_SIZE - 80) {
  190. len += sprintf(buf+len, "%s\t%s\n",
  191. (tmp->fs_flags & FS_REQUIRES_DEV) ? "" : "nodev",
  192. tmp->name);
  193. tmp = tmp->next;
  194. }
  195. read_unlock(&file_systems_lock);
  196. return len;
  197. }
  198. #ifdef CONFIG_PROC_FS
  199. static int filesystems_proc_show(struct seq_file *m, void *v)
  200. {
  201. struct file_system_type * tmp;
  202. read_lock(&file_systems_lock);
  203. tmp = file_systems;
  204. while (tmp) {
  205. seq_printf(m, "%s\t%s\n",
  206. (tmp->fs_flags & FS_REQUIRES_DEV) ? "" : "nodev",
  207. tmp->name);
  208. tmp = tmp->next;
  209. }
  210. read_unlock(&file_systems_lock);
  211. return 0;
  212. }
  213. static int __init proc_filesystems_init(void)
  214. {
  215. proc_create_single("filesystems", 0, NULL, filesystems_proc_show);
  216. return 0;
  217. }
  218. module_init(proc_filesystems_init);
  219. #endif
  220. static struct file_system_type *__get_fs_type(const char *name, int len)
  221. {
  222. struct file_system_type *fs;
  223. read_lock(&file_systems_lock);
  224. fs = *(find_filesystem(name, len));
  225. if (fs && !try_module_get(fs->owner))
  226. fs = NULL;
  227. read_unlock(&file_systems_lock);
  228. return fs;
  229. }
  230. struct file_system_type *get_fs_type(const char *name)
  231. {
  232. struct file_system_type *fs;
  233. const char *dot = strchr(name, '.');
  234. int len = dot ? dot - name : strlen(name);
  235. fs = __get_fs_type(name, len);
  236. if (!fs && (request_module("fs-%.*s", len, name) == 0)) {
  237. fs = __get_fs_type(name, len);
  238. if (!fs)
  239. pr_warn_once("request_module fs-%.*s succeeded, but still no fs?\n",
  240. len, name);
  241. }
  242. if (dot && fs && !(fs->fs_flags & FS_HAS_SUBTYPE)) {
  243. put_filesystem(fs);
  244. fs = NULL;
  245. }
  246. return fs;
  247. }
  248. EXPORT_SYMBOL(get_fs_type);