main.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. unsigned afs_debug;
  20. module_param_named(debug, afs_debug, uint, S_IWUSR | S_IRUGO);
  21. MODULE_PARM_DESC(debug, "AFS debugging mask");
  22. static char *rootcell;
  23. module_param(rootcell, charp, 0);
  24. MODULE_PARM_DESC(rootcell, "root AFS cell name and VL server IP addr list");
  25. struct workqueue_struct *afs_wq;
  26. static struct proc_dir_entry *afs_proc_symlink;
  27. #if defined(CONFIG_ALPHA)
  28. const char afs_init_sysname[] = "alpha_linux26";
  29. #elif defined(CONFIG_X86_64)
  30. const char afs_init_sysname[] = "amd64_linux26";
  31. #elif defined(CONFIG_ARM)
  32. const char afs_init_sysname[] = "arm_linux26";
  33. #elif defined(CONFIG_ARM64)
  34. const char afs_init_sysname[] = "aarch64_linux26";
  35. #elif defined(CONFIG_X86_32)
  36. const char afs_init_sysname[] = "i386_linux26";
  37. #elif defined(CONFIG_PPC64)
  38. const char afs_init_sysname[] = "ppc64_linux26";
  39. #elif defined(CONFIG_PPC32)
  40. const char afs_init_sysname[] = "ppc_linux26";
  41. #elif defined(CONFIG_S390)
  42. #ifdef CONFIG_64BIT
  43. const char afs_init_sysname[] = "s390x_linux26";
  44. #else
  45. const char afs_init_sysname[] = "s390_linux26";
  46. #endif
  47. #elif defined(CONFIG_SPARC64)
  48. const char afs_init_sysname[] = "sparc64_linux26";
  49. #elif defined(CONFIG_SPARC32)
  50. const char afs_init_sysname[] = "sparc_linux26";
  51. #else
  52. const char afs_init_sysname[] = "unknown_linux26";
  53. #endif
  54. /*
  55. * Initialise an AFS network namespace record.
  56. */
  57. static int __net_init afs_net_init(struct net *net_ns)
  58. {
  59. struct afs_sysnames *sysnames;
  60. struct afs_net *net = afs_net(net_ns);
  61. int ret;
  62. net->net = net_ns;
  63. net->live = true;
  64. generate_random_uuid((unsigned char *)&net->uuid);
  65. INIT_WORK(&net->charge_preallocation_work, afs_charge_preallocation);
  66. INIT_WORK(&net->rx_oob_work, afs_process_oob_queue);
  67. mutex_init(&net->socket_mutex);
  68. net->cells = RB_ROOT;
  69. idr_init(&net->cells_dyn_ino);
  70. init_rwsem(&net->cells_lock);
  71. mutex_init(&net->cells_alias_lock);
  72. mutex_init(&net->proc_cells_lock);
  73. INIT_HLIST_HEAD(&net->proc_cells);
  74. seqlock_init(&net->fs_lock);
  75. INIT_LIST_HEAD(&net->fs_probe_fast);
  76. INIT_LIST_HEAD(&net->fs_probe_slow);
  77. INIT_HLIST_HEAD(&net->fs_proc);
  78. INIT_WORK(&net->fs_prober, afs_fs_probe_dispatcher);
  79. timer_setup(&net->fs_probe_timer, afs_fs_probe_timer, 0);
  80. atomic_set(&net->servers_outstanding, 1);
  81. ret = -ENOMEM;
  82. sysnames = kzalloc_obj(*sysnames);
  83. if (!sysnames)
  84. goto error_sysnames;
  85. sysnames->subs[0] = (char *)&afs_init_sysname;
  86. sysnames->nr = 1;
  87. refcount_set(&sysnames->usage, 1);
  88. net->sysnames = sysnames;
  89. rwlock_init(&net->sysnames_lock);
  90. /* Register the /proc stuff */
  91. ret = afs_proc_init(net);
  92. if (ret < 0)
  93. goto error_proc;
  94. /* Initialise the cell DB */
  95. ret = afs_cell_init(net, rootcell);
  96. if (ret < 0)
  97. goto error_cell_init;
  98. /* Create the RxRPC transport */
  99. ret = afs_open_socket(net);
  100. if (ret < 0)
  101. goto error_open_socket;
  102. return 0;
  103. error_open_socket:
  104. net->live = false;
  105. afs_fs_probe_cleanup(net);
  106. afs_cell_purge(net);
  107. afs_wait_for_servers(net);
  108. error_cell_init:
  109. net->live = false;
  110. afs_proc_cleanup(net);
  111. error_proc:
  112. afs_put_sysnames(net->sysnames);
  113. error_sysnames:
  114. idr_destroy(&net->cells_dyn_ino);
  115. net->live = false;
  116. return ret;
  117. }
  118. /*
  119. * Clean up and destroy an AFS network namespace record.
  120. */
  121. static void __net_exit afs_net_exit(struct net *net_ns)
  122. {
  123. struct afs_net *net = afs_net(net_ns);
  124. net->live = false;
  125. afs_fs_probe_cleanup(net);
  126. afs_cell_purge(net);
  127. afs_wait_for_servers(net);
  128. afs_close_socket(net);
  129. afs_proc_cleanup(net);
  130. afs_put_sysnames(net->sysnames);
  131. idr_destroy(&net->cells_dyn_ino);
  132. kfree_rcu(rcu_access_pointer(net->address_prefs), rcu);
  133. }
  134. static struct pernet_operations afs_net_ops = {
  135. .init = afs_net_init,
  136. .exit = afs_net_exit,
  137. .id = &afs_net_id,
  138. .size = sizeof(struct afs_net),
  139. };
  140. /*
  141. * initialise the AFS client FS module
  142. */
  143. static int __init afs_init(void)
  144. {
  145. int ret = -ENOMEM;
  146. printk(KERN_INFO "kAFS: Red Hat AFS client v0.1 registering.\n");
  147. afs_wq = alloc_workqueue("afs", WQ_PERCPU, 0);
  148. if (!afs_wq)
  149. goto error_afs_wq;
  150. afs_async_calls = alloc_workqueue("kafsd", WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
  151. if (!afs_async_calls)
  152. goto error_async;
  153. afs_lock_manager = alloc_workqueue("kafs_lockd", WQ_MEM_RECLAIM | WQ_PERCPU, 0);
  154. if (!afs_lock_manager)
  155. goto error_lockmgr;
  156. ret = register_pernet_device(&afs_net_ops);
  157. if (ret < 0)
  158. goto error_net;
  159. /* register the filesystems */
  160. ret = afs_fs_init();
  161. if (ret < 0)
  162. goto error_fs;
  163. afs_proc_symlink = proc_symlink("fs/afs", NULL, "../self/net/afs");
  164. if (!afs_proc_symlink) {
  165. ret = -ENOMEM;
  166. goto error_proc;
  167. }
  168. return ret;
  169. error_proc:
  170. afs_fs_exit();
  171. error_fs:
  172. unregister_pernet_device(&afs_net_ops);
  173. error_net:
  174. destroy_workqueue(afs_lock_manager);
  175. error_lockmgr:
  176. destroy_workqueue(afs_async_calls);
  177. error_async:
  178. destroy_workqueue(afs_wq);
  179. error_afs_wq:
  180. rcu_barrier();
  181. printk(KERN_ERR "kAFS: failed to register: %d\n", ret);
  182. return ret;
  183. }
  184. /* XXX late_initcall is kludgy, but the only alternative seems to create
  185. * a transport upon the first mount, which is worse. Or is it?
  186. */
  187. late_initcall(afs_init); /* must be called after net/ to create socket */
  188. /*
  189. * clean up on module removal
  190. */
  191. static void __exit afs_exit(void)
  192. {
  193. printk(KERN_INFO "kAFS: Red Hat AFS client v0.1 unregistering.\n");
  194. proc_remove(afs_proc_symlink);
  195. afs_fs_exit();
  196. unregister_pernet_device(&afs_net_ops);
  197. destroy_workqueue(afs_lock_manager);
  198. destroy_workqueue(afs_async_calls);
  199. destroy_workqueue(afs_wq);
  200. afs_clean_up_permit_cache();
  201. rcu_barrier();
  202. }
  203. module_exit(afs_exit);