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. INIT_LIST_HEAD(&fs->fs_supers);
  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. static int fs_index(const char __user * __name)
  110. {
  111. struct file_system_type * tmp;
  112. char * 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) == 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. int __init get_filesystem_list(char *buf)
  177. {
  178. int len = 0;
  179. struct file_system_type * tmp;
  180. read_lock(&file_systems_lock);
  181. tmp = file_systems;
  182. while (tmp && len < PAGE_SIZE - 80) {
  183. len += sprintf(buf+len, "%s\t%s\n",
  184. (tmp->fs_flags & FS_REQUIRES_DEV) ? "" : "nodev",
  185. tmp->name);
  186. tmp = tmp->next;
  187. }
  188. read_unlock(&file_systems_lock);
  189. return len;
  190. }
  191. #ifdef CONFIG_PROC_FS
  192. static int filesystems_proc_show(struct seq_file *m, void *v)
  193. {
  194. struct file_system_type * tmp;
  195. read_lock(&file_systems_lock);
  196. tmp = file_systems;
  197. while (tmp) {
  198. seq_printf(m, "%s\t%s\n",
  199. (tmp->fs_flags & FS_REQUIRES_DEV) ? "" : "nodev",
  200. tmp->name);
  201. tmp = tmp->next;
  202. }
  203. read_unlock(&file_systems_lock);
  204. return 0;
  205. }
  206. static int filesystems_proc_open(struct inode *inode, struct file *file)
  207. {
  208. return single_open(file, filesystems_proc_show, NULL);
  209. }
  210. static const struct file_operations filesystems_proc_fops = {
  211. .open = filesystems_proc_open,
  212. .read = seq_read,
  213. .llseek = seq_lseek,
  214. .release = single_release,
  215. };
  216. static int __init proc_filesystems_init(void)
  217. {
  218. proc_create("filesystems", 0, NULL, &filesystems_proc_fops);
  219. return 0;
  220. }
  221. module_init(proc_filesystems_init);
  222. #endif
  223. static struct file_system_type *__get_fs_type(const char *name, int len)
  224. {
  225. struct file_system_type *fs;
  226. read_lock(&file_systems_lock);
  227. fs = *(find_filesystem(name, len));
  228. if (fs && !try_module_get(fs->owner))
  229. fs = NULL;
  230. read_unlock(&file_systems_lock);
  231. return fs;
  232. }
  233. struct file_system_type *get_fs_type(const char *name)
  234. {
  235. struct file_system_type *fs;
  236. const char *dot = strchr(name, '.');
  237. int len = dot ? dot - name : strlen(name);
  238. fs = __get_fs_type(name, len);
  239. if (!fs && (request_module("%.*s", len, name) == 0))
  240. fs = __get_fs_type(name, len);
  241. if (dot && fs && !(fs->fs_flags & FS_HAS_SUBTYPE)) {
  242. put_filesystem(fs);
  243. fs = NULL;
  244. }
  245. return fs;
  246. }
  247. EXPORT_SYMBOL(get_fs_type);