filesystems.c 6.4 KB

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