filesystems.c 6.3 KB

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