filesystems.c 6.4 KB

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