filesystems.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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/slab.h>
  11. #include <linux/kmod.h>
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/sched.h> /* for 'current' */
  15. #include <asm/uaccess.h>
  16. /*
  17. * Handling of filesystem drivers list.
  18. * Rules:
  19. * Inclusion to/removals from/scanning of list are protected by spinlock.
  20. * During the unload module must call unregister_filesystem().
  21. * We can access the fields of list element if:
  22. * 1) spinlock is held or
  23. * 2) we hold the reference to the module.
  24. * The latter can be guaranteed by call of try_module_get(); if it
  25. * returned 0 we must skip the element, otherwise we got the reference.
  26. * Once the reference is obtained we can drop the spinlock.
  27. */
  28. static struct file_system_type *file_systems;
  29. static DEFINE_RWLOCK(file_systems_lock);
  30. /* WARNING: This can be used only if we _already_ own a reference */
  31. void get_filesystem(struct file_system_type *fs)
  32. {
  33. __module_get(fs->owner);
  34. }
  35. void put_filesystem(struct file_system_type *fs)
  36. {
  37. module_put(fs->owner);
  38. }
  39. static struct file_system_type **find_filesystem(const char *name)
  40. {
  41. struct file_system_type **p;
  42. for (p=&file_systems; *p; p=&(*p)->next)
  43. if (strcmp((*p)->name,name) == 0)
  44. break;
  45. return p;
  46. }
  47. /**
  48. * register_filesystem - register a new filesystem
  49. * @fs: the file system structure
  50. *
  51. * Adds the file system passed to the list of file systems the kernel
  52. * is aware of for mount and other syscalls. Returns 0 on success,
  53. * or a negative errno code on an error.
  54. *
  55. * The &struct file_system_type that is passed is linked into the kernel
  56. * structures and must not be freed until the file system has been
  57. * unregistered.
  58. */
  59. int register_filesystem(struct file_system_type * fs)
  60. {
  61. int res = 0;
  62. struct file_system_type ** p;
  63. if (!fs)
  64. return -EINVAL;
  65. if (fs->next)
  66. return -EBUSY;
  67. INIT_LIST_HEAD(&fs->fs_supers);
  68. write_lock(&file_systems_lock);
  69. p = find_filesystem(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. return 0;
  100. }
  101. tmp = &(*tmp)->next;
  102. }
  103. write_unlock(&file_systems_lock);
  104. return -EINVAL;
  105. }
  106. EXPORT_SYMBOL(unregister_filesystem);
  107. static int fs_index(const char __user * __name)
  108. {
  109. struct file_system_type * tmp;
  110. char * name;
  111. int err, index;
  112. name = getname(__name);
  113. err = PTR_ERR(name);
  114. if (IS_ERR(name))
  115. return err;
  116. err = -EINVAL;
  117. read_lock(&file_systems_lock);
  118. for (tmp=file_systems, index=0 ; tmp ; tmp=tmp->next, index++) {
  119. if (strcmp(tmp->name,name) == 0) {
  120. err = index;
  121. break;
  122. }
  123. }
  124. read_unlock(&file_systems_lock);
  125. putname(name);
  126. return err;
  127. }
  128. static int fs_name(unsigned int index, char __user * buf)
  129. {
  130. struct file_system_type * tmp;
  131. int len, res;
  132. read_lock(&file_systems_lock);
  133. for (tmp = file_systems; tmp; tmp = tmp->next, index--)
  134. if (index <= 0 && try_module_get(tmp->owner))
  135. break;
  136. read_unlock(&file_systems_lock);
  137. if (!tmp)
  138. return -EINVAL;
  139. /* OK, we got the reference, so we can safely block */
  140. len = strlen(tmp->name) + 1;
  141. res = copy_to_user(buf, tmp->name, len) ? -EFAULT : 0;
  142. put_filesystem(tmp);
  143. return res;
  144. }
  145. static int fs_maxindex(void)
  146. {
  147. struct file_system_type * tmp;
  148. int index;
  149. read_lock(&file_systems_lock);
  150. for (tmp = file_systems, index = 0 ; tmp ; tmp = tmp->next, index++)
  151. ;
  152. read_unlock(&file_systems_lock);
  153. return index;
  154. }
  155. /*
  156. * Whee.. Weird sysv syscall.
  157. */
  158. asmlinkage long sys_sysfs(int option, unsigned long arg1, unsigned long arg2)
  159. {
  160. int retval = -EINVAL;
  161. switch (option) {
  162. case 1:
  163. retval = fs_index((const char __user *) arg1);
  164. break;
  165. case 2:
  166. retval = fs_name(arg1, (char __user *) arg2);
  167. break;
  168. case 3:
  169. retval = fs_maxindex();
  170. break;
  171. }
  172. return retval;
  173. }
  174. int get_filesystem_list(char * buf)
  175. {
  176. int len = 0;
  177. struct file_system_type * tmp;
  178. read_lock(&file_systems_lock);
  179. tmp = file_systems;
  180. while (tmp && len < PAGE_SIZE - 80) {
  181. len += sprintf(buf+len, "%s\t%s\n",
  182. (tmp->fs_flags & FS_REQUIRES_DEV) ? "" : "nodev",
  183. tmp->name);
  184. tmp = tmp->next;
  185. }
  186. read_unlock(&file_systems_lock);
  187. return len;
  188. }
  189. struct file_system_type *get_fs_type(const char *name)
  190. {
  191. struct file_system_type *fs;
  192. read_lock(&file_systems_lock);
  193. fs = *(find_filesystem(name));
  194. if (fs && !try_module_get(fs->owner))
  195. fs = NULL;
  196. read_unlock(&file_systems_lock);
  197. if (!fs && (request_module("%s", name) == 0)) {
  198. read_lock(&file_systems_lock);
  199. fs = *(find_filesystem(name));
  200. if (fs && !try_module_get(fs->owner))
  201. fs = NULL;
  202. read_unlock(&file_systems_lock);
  203. }
  204. return fs;
  205. }
  206. EXPORT_SYMBOL(get_fs_type);