main.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* AFS client file system
  3. *
  4. * Copyright (C) 2002,5 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #include <linux/module.h>
  8. #include <linux/moduleparam.h>
  9. #include <linux/init.h>
  10. #include <linux/completion.h>
  11. #include <linux/sched.h>
  12. #include <linux/random.h>
  13. #include <linux/proc_fs.h>
  14. #define CREATE_TRACE_POINTS
  15. #include "internal.h"
  16. MODULE_DESCRIPTION("AFS Client File System");
  17. MODULE_AUTHOR("Red Hat, Inc.");
  18. MODULE_LICENSE("GPL");
  19. MODULE_IMPORT_NS(ANDROID_GKI_VFS_EXPORT_ONLY);
  20. unsigned afs_debug;
  21. module_param_named(debug, afs_debug, uint, S_IWUSR | S_IRUGO);
  22. MODULE_PARM_DESC(debug, "AFS debugging mask");
  23. static char *rootcell;
  24. module_param(rootcell, charp, 0);
  25. MODULE_PARM_DESC(rootcell, "root AFS cell name and VL server IP addr list");
  26. struct workqueue_struct *afs_wq;
  27. static struct proc_dir_entry *afs_proc_symlink;
  28. #if defined(CONFIG_ALPHA)
  29. const char afs_init_sysname[] = "alpha_linux26";
  30. #elif defined(CONFIG_X86_64)
  31. const char afs_init_sysname[] = "amd64_linux26";
  32. #elif defined(CONFIG_ARM)
  33. const char afs_init_sysname[] = "arm_linux26";
  34. #elif defined(CONFIG_ARM64)
  35. const char afs_init_sysname[] = "aarch64_linux26";
  36. #elif defined(CONFIG_X86_32)
  37. const char afs_init_sysname[] = "i386_linux26";
  38. #elif defined(CONFIG_IA64)
  39. const char afs_init_sysname[] = "ia64_linux26";
  40. #elif defined(CONFIG_PPC64)
  41. const char afs_init_sysname[] = "ppc64_linux26";
  42. #elif defined(CONFIG_PPC32)
  43. const char afs_init_sysname[] = "ppc_linux26";
  44. #elif defined(CONFIG_S390)
  45. #ifdef CONFIG_64BIT
  46. const char afs_init_sysname[] = "s390x_linux26";
  47. #else
  48. const char afs_init_sysname[] = "s390_linux26";
  49. #endif
  50. #elif defined(CONFIG_SPARC64)
  51. const char afs_init_sysname[] = "sparc64_linux26";
  52. #elif defined(CONFIG_SPARC32)
  53. const char afs_init_sysname[] = "sparc_linux26";
  54. #else
  55. const char afs_init_sysname[] = "unknown_linux26";
  56. #endif
  57. /*
  58. * Initialise an AFS network namespace record.
  59. */
  60. static int __net_init afs_net_init(struct net *net_ns)
  61. {
  62. struct afs_sysnames *sysnames;
  63. struct afs_net *net = afs_net(net_ns);
  64. int ret;
  65. net->net = net_ns;
  66. net->live = true;
  67. generate_random_uuid((unsigned char *)&net->uuid);
  68. INIT_WORK(&net->charge_preallocation_work, afs_charge_preallocation);
  69. mutex_init(&net->socket_mutex);
  70. net->cells = RB_ROOT;
  71. init_rwsem(&net->cells_lock);
  72. INIT_WORK(&net->cells_manager, afs_manage_cells);
  73. timer_setup(&net->cells_timer, afs_cells_timer, 0);
  74. mutex_init(&net->cells_alias_lock);
  75. mutex_init(&net->proc_cells_lock);
  76. INIT_HLIST_HEAD(&net->proc_cells);
  77. seqlock_init(&net->fs_lock);
  78. net->fs_servers = RB_ROOT;
  79. INIT_LIST_HEAD(&net->fs_probe_fast);
  80. INIT_LIST_HEAD(&net->fs_probe_slow);
  81. INIT_HLIST_HEAD(&net->fs_proc);
  82. INIT_HLIST_HEAD(&net->fs_addresses4);
  83. INIT_HLIST_HEAD(&net->fs_addresses6);
  84. seqlock_init(&net->fs_addr_lock);
  85. INIT_WORK(&net->fs_manager, afs_manage_servers);
  86. timer_setup(&net->fs_timer, afs_servers_timer, 0);
  87. INIT_WORK(&net->fs_prober, afs_fs_probe_dispatcher);
  88. timer_setup(&net->fs_probe_timer, afs_fs_probe_timer, 0);
  89. atomic_set(&net->servers_outstanding, 1);
  90. ret = -ENOMEM;
  91. sysnames = kzalloc(sizeof(*sysnames), GFP_KERNEL);
  92. if (!sysnames)
  93. goto error_sysnames;
  94. sysnames->subs[0] = (char *)&afs_init_sysname;
  95. sysnames->nr = 1;
  96. refcount_set(&sysnames->usage, 1);
  97. net->sysnames = sysnames;
  98. rwlock_init(&net->sysnames_lock);
  99. /* Register the /proc stuff */
  100. ret = afs_proc_init(net);
  101. if (ret < 0)
  102. goto error_proc;
  103. /* Initialise the cell DB */
  104. ret = afs_cell_init(net, rootcell);
  105. if (ret < 0)
  106. goto error_cell_init;
  107. /* Create the RxRPC transport */
  108. ret = afs_open_socket(net);
  109. if (ret < 0)
  110. goto error_open_socket;
  111. return 0;
  112. error_open_socket:
  113. net->live = false;
  114. afs_fs_probe_cleanup(net);
  115. afs_cell_purge(net);
  116. afs_purge_servers(net);
  117. error_cell_init:
  118. net->live = false;
  119. afs_proc_cleanup(net);
  120. error_proc:
  121. afs_put_sysnames(net->sysnames);
  122. error_sysnames:
  123. net->live = false;
  124. return ret;
  125. }
  126. /*
  127. * Clean up and destroy an AFS network namespace record.
  128. */
  129. static void __net_exit afs_net_exit(struct net *net_ns)
  130. {
  131. struct afs_net *net = afs_net(net_ns);
  132. net->live = false;
  133. afs_fs_probe_cleanup(net);
  134. afs_cell_purge(net);
  135. afs_purge_servers(net);
  136. afs_close_socket(net);
  137. afs_proc_cleanup(net);
  138. afs_put_sysnames(net->sysnames);
  139. }
  140. static struct pernet_operations afs_net_ops = {
  141. .init = afs_net_init,
  142. .exit = afs_net_exit,
  143. .id = &afs_net_id,
  144. .size = sizeof(struct afs_net),
  145. };
  146. /*
  147. * initialise the AFS client FS module
  148. */
  149. static int __init afs_init(void)
  150. {
  151. int ret = -ENOMEM;
  152. printk(KERN_INFO "kAFS: Red Hat AFS client v0.1 registering.\n");
  153. afs_wq = alloc_workqueue("afs", 0, 0);
  154. if (!afs_wq)
  155. goto error_afs_wq;
  156. afs_async_calls = alloc_workqueue("kafsd", WQ_MEM_RECLAIM, 0);
  157. if (!afs_async_calls)
  158. goto error_async;
  159. afs_lock_manager = alloc_workqueue("kafs_lockd", WQ_MEM_RECLAIM, 0);
  160. if (!afs_lock_manager)
  161. goto error_lockmgr;
  162. #ifdef CONFIG_AFS_FSCACHE
  163. /* we want to be able to cache */
  164. ret = fscache_register_netfs(&afs_cache_netfs);
  165. if (ret < 0)
  166. goto error_cache;
  167. #endif
  168. ret = register_pernet_device(&afs_net_ops);
  169. if (ret < 0)
  170. goto error_net;
  171. /* register the filesystems */
  172. ret = afs_fs_init();
  173. if (ret < 0)
  174. goto error_fs;
  175. afs_proc_symlink = proc_symlink("fs/afs", NULL, "../self/net/afs");
  176. if (!afs_proc_symlink) {
  177. ret = -ENOMEM;
  178. goto error_proc;
  179. }
  180. return ret;
  181. error_proc:
  182. afs_fs_exit();
  183. error_fs:
  184. unregister_pernet_device(&afs_net_ops);
  185. error_net:
  186. #ifdef CONFIG_AFS_FSCACHE
  187. fscache_unregister_netfs(&afs_cache_netfs);
  188. error_cache:
  189. #endif
  190. destroy_workqueue(afs_lock_manager);
  191. error_lockmgr:
  192. destroy_workqueue(afs_async_calls);
  193. error_async:
  194. destroy_workqueue(afs_wq);
  195. error_afs_wq:
  196. rcu_barrier();
  197. printk(KERN_ERR "kAFS: failed to register: %d\n", ret);
  198. return ret;
  199. }
  200. /* XXX late_initcall is kludgy, but the only alternative seems to create
  201. * a transport upon the first mount, which is worse. Or is it?
  202. */
  203. late_initcall(afs_init); /* must be called after net/ to create socket */
  204. /*
  205. * clean up on module removal
  206. */
  207. static void __exit afs_exit(void)
  208. {
  209. printk(KERN_INFO "kAFS: Red Hat AFS client v0.1 unregistering.\n");
  210. proc_remove(afs_proc_symlink);
  211. afs_fs_exit();
  212. unregister_pernet_device(&afs_net_ops);
  213. #ifdef CONFIG_AFS_FSCACHE
  214. fscache_unregister_netfs(&afs_cache_netfs);
  215. #endif
  216. destroy_workqueue(afs_lock_manager);
  217. destroy_workqueue(afs_async_calls);
  218. destroy_workqueue(afs_wq);
  219. afs_clean_up_permit_cache();
  220. rcu_barrier();
  221. }
  222. module_exit(afs_exit);