filesystems.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 <asm/uaccess.h>
  15. /*
  16. * Handling of filesystem drivers list.
  17. * Rules:
  18. * Inclusion to/removals from/scanning of list are protected by spinlock.
  19. * During the unload module must call unregister_filesystem().
  20. * We can access the fields of list element if:
  21. * 1) spinlock is held or
  22. * 2) we hold the reference to the module.
  23. * The latter can be guaranteed by call of try_module_get(); if it
  24. * returned 0 we must skip the element, otherwise we got the reference.
  25. * Once the reference is obtained we can drop the spinlock.
  26. */
  27. static struct file_system_type *file_systems;
  28. static DEFINE_RWLOCK(file_systems_lock);
  29. /* WARNING: This can be used only if we _already_ own a reference */
  30. void get_filesystem(struct file_system_type *fs)
  31. {
  32. __module_get(fs->owner);
  33. }
  34. void put_filesystem(struct file_system_type *fs)
  35. {
  36. module_put(fs->owner);
  37. }
  38. static struct file_system_type **find_filesystem(const char *name, unsigned len)
  39. {
  40. struct file_system_type **p;
  41. for (p=&file_systems; *p; p=&(*p)->next)
  42. if (strlen((*p)->name) == len &&
  43. strncmp((*p)->name, name, len) == 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. BUG_ON(strchr(fs->name, '.'));
  64. if (fs->next)
  65. return -EBUSY;
  66. INIT_LIST_HEAD(&fs->fs_supers);
  67. write_lock(&file_systems_lock);
  68. p = find_filesystem(fs->name, strlen(fs->name));
  69. if (*p)
  70. res = -EBUSY;
  71. else
  72. *p = fs;
  73. write_unlock(&file_systems_lock);
  74. return res;
  75. }
  76. EXPORT_SYMBOL(register_filesystem);
  77. /**
  78. * unregister_filesystem - unregister a file system
  79. * @fs: filesystem to unregister
  80. *
  81. * Remove a file system that was previously successfully registered
  82. * with the kernel. An error is returned if the file system is not found.
  83. * Zero is returned on a success.
  84. *
  85. * Once this function has returned the &struct file_system_type structure
  86. * may be freed or reused.
  87. */
  88. int unregister_filesystem(struct file_system_type * fs)
  89. {
  90. struct file_system_type ** tmp;
  91. write_lock(&file_systems_lock);
  92. tmp = &file_systems;
  93. while (*tmp) {
  94. if (fs == *tmp) {
  95. *tmp = fs->next;
  96. fs->next = NULL;
  97. write_unlock(&file_systems_lock);
  98. return 0;
  99. }
  100. tmp = &(*tmp)->next;
  101. }
  102. write_unlock(&file_systems_lock);
  103. return -EINVAL;
  104. }
  105. EXPORT_SYMBOL(unregister_filesystem);
  106. static int fs_index(const char __user * __name)
  107. {
  108. struct file_system_type * tmp;
  109. char * name;
  110. int err, index;
  111. name = getname(__name);
  112. err = PTR_ERR(name);
  113. if (IS_ERR(name))
  114. return err;
  115. err = -EINVAL;
  116. read_lock(&file_systems_lock);
  117. for (tmp=file_systems, index=0 ; tmp ; tmp=tmp->next, index++) {
  118. if (strcmp(tmp->name,name) == 0) {
  119. err = index;
  120. break;
  121. }
  122. }
  123. read_unlock(&file_systems_lock);
  124. putname(name);
  125. return err;
  126. }
  127. static int fs_name(unsigned int index, char __user * buf)
  128. {
  129. struct file_system_type * tmp;
  130. int len, res;
  131. read_lock(&file_systems_lock);
  132. for (tmp = file_systems; tmp; tmp = tmp->next, index--)
  133. if (index <= 0 && try_module_get(tmp->owner))
  134. break;
  135. read_unlock(&file_systems_lock);
  136. if (!tmp)
  137. return -EINVAL;
  138. /* OK, we got the reference, so we can safely block */
  139. len = strlen(tmp->name) + 1;
  140. res = copy_to_user(buf, tmp->name, len) ? -EFAULT : 0;
  141. put_filesystem(tmp);
  142. return res;
  143. }
  144. static int fs_maxindex(void)
  145. {
  146. struct file_system_type * tmp;
  147. int index;
  148. read_lock(&file_systems_lock);
  149. for (tmp = file_systems, index = 0 ; tmp ; tmp = tmp->next, index++)
  150. ;
  151. read_unlock(&file_systems_lock);
  152. return index;
  153. }
  154. /*
  155. * Whee.. Weird sysv syscall.
  156. */
  157. asmlinkage long sys_sysfs(int option, unsigned long arg1, unsigned long arg2)
  158. {
  159. int retval = -EINVAL;
  160. switch (option) {
  161. case 1:
  162. retval = fs_index((const char __user *) arg1);
  163. break;
  164. case 2:
  165. retval = fs_name(arg1, (char __user *) arg2);
  166. break;
  167. case 3:
  168. retval = fs_maxindex();
  169. break;
  170. }
  171. return retval;
  172. }
  173. int get_filesystem_list(char * buf)
  174. {
  175. int len = 0;
  176. struct file_system_type * tmp;
  177. read_lock(&file_systems_lock);
  178. tmp = file_systems;
  179. while (tmp && len < PAGE_SIZE - 80) {
  180. len += sprintf(buf+len, "%s\t%s\n",
  181. (tmp->fs_flags & FS_REQUIRES_DEV) ? "" : "nodev",
  182. tmp->name);
  183. tmp = tmp->next;
  184. }
  185. read_unlock(&file_systems_lock);
  186. return len;
  187. }
  188. struct file_system_type *get_fs_type(const char *name)
  189. {
  190. struct file_system_type *fs;
  191. const char *dot = strchr(name, '.');
  192. unsigned len = dot ? dot - name : strlen(name);
  193. read_lock(&file_systems_lock);
  194. fs = *(find_filesystem(name, len));
  195. if (fs && !try_module_get(fs->owner))
  196. fs = NULL;
  197. read_unlock(&file_systems_lock);
  198. if (!fs && (request_module("%.*s", len, name) == 0)) {
  199. read_lock(&file_systems_lock);
  200. fs = *(find_filesystem(name, len));
  201. if (fs && !try_module_get(fs->owner))
  202. fs = NULL;
  203. read_unlock(&file_systems_lock);
  204. }
  205. if (dot && fs && !(fs->fs_flags & FS_HAS_SUBTYPE)) {
  206. put_filesystem(fs);
  207. fs = NULL;
  208. }
  209. return fs;
  210. }
  211. EXPORT_SYMBOL(get_fs_type);