filesystems.c 6.2 KB

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