filesystems.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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->next)
  64. return -EBUSY;
  65. INIT_LIST_HEAD(&fs->fs_supers);
  66. write_lock(&file_systems_lock);
  67. p = find_filesystem(fs->name);
  68. if (*p)
  69. res = -EBUSY;
  70. else
  71. *p = fs;
  72. write_unlock(&file_systems_lock);
  73. return res;
  74. }
  75. EXPORT_SYMBOL(register_filesystem);
  76. /**
  77. * unregister_filesystem - unregister a file system
  78. * @fs: filesystem to unregister
  79. *
  80. * Remove a file system that was previously successfully registered
  81. * with the kernel. An error is returned if the file system is not found.
  82. * Zero is returned on a success.
  83. *
  84. * Once this function has returned the &struct file_system_type structure
  85. * may be freed or reused.
  86. */
  87. int unregister_filesystem(struct file_system_type * fs)
  88. {
  89. struct file_system_type ** tmp;
  90. write_lock(&file_systems_lock);
  91. tmp = &file_systems;
  92. while (*tmp) {
  93. if (fs == *tmp) {
  94. *tmp = fs->next;
  95. fs->next = NULL;
  96. write_unlock(&file_systems_lock);
  97. return 0;
  98. }
  99. tmp = &(*tmp)->next;
  100. }
  101. write_unlock(&file_systems_lock);
  102. return -EINVAL;
  103. }
  104. EXPORT_SYMBOL(unregister_filesystem);
  105. static int fs_index(const char __user * __name)
  106. {
  107. struct file_system_type * tmp;
  108. char * name;
  109. int err, index;
  110. name = getname(__name);
  111. err = PTR_ERR(name);
  112. if (IS_ERR(name))
  113. return err;
  114. err = -EINVAL;
  115. read_lock(&file_systems_lock);
  116. for (tmp=file_systems, index=0 ; tmp ; tmp=tmp->next, index++) {
  117. if (strcmp(tmp->name,name) == 0) {
  118. err = index;
  119. break;
  120. }
  121. }
  122. read_unlock(&file_systems_lock);
  123. putname(name);
  124. return err;
  125. }
  126. static int fs_name(unsigned int index, char __user * buf)
  127. {
  128. struct file_system_type * tmp;
  129. int len, res;
  130. read_lock(&file_systems_lock);
  131. for (tmp = file_systems; tmp; tmp = tmp->next, index--)
  132. if (index <= 0 && try_module_get(tmp->owner))
  133. break;
  134. read_unlock(&file_systems_lock);
  135. if (!tmp)
  136. return -EINVAL;
  137. /* OK, we got the reference, so we can safely block */
  138. len = strlen(tmp->name) + 1;
  139. res = copy_to_user(buf, tmp->name, len) ? -EFAULT : 0;
  140. put_filesystem(tmp);
  141. return res;
  142. }
  143. static int fs_maxindex(void)
  144. {
  145. struct file_system_type * tmp;
  146. int index;
  147. read_lock(&file_systems_lock);
  148. for (tmp = file_systems, index = 0 ; tmp ; tmp = tmp->next, index++)
  149. ;
  150. read_unlock(&file_systems_lock);
  151. return index;
  152. }
  153. /*
  154. * Whee.. Weird sysv syscall.
  155. */
  156. asmlinkage long sys_sysfs(int option, unsigned long arg1, unsigned long arg2)
  157. {
  158. int retval = -EINVAL;
  159. switch (option) {
  160. case 1:
  161. retval = fs_index((const char __user *) arg1);
  162. break;
  163. case 2:
  164. retval = fs_name(arg1, (char __user *) arg2);
  165. break;
  166. case 3:
  167. retval = fs_maxindex();
  168. break;
  169. }
  170. return retval;
  171. }
  172. int get_filesystem_list(char * buf)
  173. {
  174. int len = 0;
  175. struct file_system_type * tmp;
  176. read_lock(&file_systems_lock);
  177. tmp = file_systems;
  178. while (tmp && len < PAGE_SIZE - 80) {
  179. len += sprintf(buf+len, "%s\t%s\n",
  180. (tmp->fs_flags & FS_REQUIRES_DEV) ? "" : "nodev",
  181. tmp->name);
  182. tmp = tmp->next;
  183. }
  184. read_unlock(&file_systems_lock);
  185. return len;
  186. }
  187. struct file_system_type *get_fs_type(const char *name)
  188. {
  189. struct file_system_type *fs;
  190. read_lock(&file_systems_lock);
  191. fs = *(find_filesystem(name));
  192. if (fs && !try_module_get(fs->owner))
  193. fs = NULL;
  194. read_unlock(&file_systems_lock);
  195. if (!fs && (request_module("%s", name) == 0)) {
  196. read_lock(&file_systems_lock);
  197. fs = *(find_filesystem(name));
  198. if (fs && !try_module_get(fs->owner))
  199. fs = NULL;
  200. read_unlock(&file_systems_lock);
  201. }
  202. return fs;
  203. }
  204. EXPORT_SYMBOL(get_fs_type);