main.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. mutex_init(&net->socket_mutex);
  67. net->cells = RB_ROOT;
  68. init_rwsem(&net->cells_lock);
  69. INIT_WORK(&net->cells_manager, afs_manage_cells);
  70. timer_setup(&net->cells_timer, afs_cells_timer, 0);
  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. net->fs_servers = RB_ROOT;
  76. INIT_LIST_HEAD(&net->fs_probe_fast);
  77. INIT_LIST_HEAD(&net->fs_probe_slow);
  78. INIT_HLIST_HEAD(&net->fs_proc);
  79. INIT_HLIST_HEAD(&net->fs_addresses);
  80. seqlock_init(&net->fs_addr_lock);
  81. INIT_WORK(&net->fs_manager, afs_manage_servers);
  82. timer_setup(&net->fs_timer, afs_servers_timer, 0);
  83. INIT_WORK(&net->fs_prober, afs_fs_probe_dispatcher);
  84. timer_setup(&net->fs_probe_timer, afs_fs_probe_timer, 0);
  85. atomic_set(&net->servers_outstanding, 1);
  86. ret = -ENOMEM;
  87. sysnames = kzalloc(sizeof(*sysnames), GFP_KERNEL);
  88. if (!sysnames)
  89. goto error_sysnames;
  90. sysnames->subs[0] = (char *)&afs_init_sysname;
  91. sysnames->nr = 1;
  92. refcount_set(&sysnames->usage, 1);
  93. net->sysnames = sysnames;
  94. rwlock_init(&net->sysnames_lock);
  95. /* Register the /proc stuff */
  96. ret = afs_proc_init(net);
  97. if (ret < 0)
  98. goto error_proc;
  99. /* Initialise the cell DB */
  100. ret = afs_cell_init(net, rootcell);
  101. if (ret < 0)
  102. goto error_cell_init;
  103. /* Create the RxRPC transport */
  104. ret = afs_open_socket(net);
  105. if (ret < 0)
  106. goto error_open_socket;
  107. return 0;
  108. error_open_socket:
  109. net->live = false;
  110. afs_fs_probe_cleanup(net);
  111. afs_cell_purge(net);
  112. afs_purge_servers(net);
  113. error_cell_init:
  114. net->live = false;
  115. afs_proc_cleanup(net);
  116. error_proc:
  117. afs_put_sysnames(net->sysnames);
  118. error_sysnames:
  119. net->live = false;
  120. return ret;
  121. }
  122. /*
  123. * Clean up and destroy an AFS network namespace record.
  124. */
  125. static void __net_exit afs_net_exit(struct net *net_ns)
  126. {
  127. struct afs_net *net = afs_net(net_ns);
  128. net->live = false;
  129. afs_fs_probe_cleanup(net);
  130. afs_cell_purge(net);
  131. afs_purge_servers(net);
  132. afs_close_socket(net);
  133. afs_proc_cleanup(net);
  134. afs_put_sysnames(net->sysnames);
  135. kfree_rcu(rcu_access_pointer(net->address_prefs), rcu);
  136. }
  137. static struct pernet_operations afs_net_ops = {
  138. .init = afs_net_init,
  139. .exit = afs_net_exit,
  140. .id = &afs_net_id,
  141. .size = sizeof(struct afs_net),
  142. };
  143. /*
  144. * initialise the AFS client FS module
  145. */
  146. static int __init afs_init(void)
  147. {
  148. int ret = -ENOMEM;
  149. printk(KERN_INFO "kAFS: Red Hat AFS client v0.1 registering.\n");
  150. afs_wq = alloc_workqueue("afs", 0, 0);
  151. if (!afs_wq)
  152. goto error_afs_wq;
  153. afs_async_calls = alloc_workqueue("kafsd", WQ_MEM_RECLAIM, 0);
  154. if (!afs_async_calls)
  155. goto error_async;
  156. afs_lock_manager = alloc_workqueue("kafs_lockd", WQ_MEM_RECLAIM, 0);
  157. if (!afs_lock_manager)
  158. goto error_lockmgr;
  159. ret = register_pernet_device(&afs_net_ops);
  160. if (ret < 0)
  161. goto error_net;
  162. /* register the filesystems */
  163. ret = afs_fs_init();
  164. if (ret < 0)
  165. goto error_fs;
  166. afs_proc_symlink = proc_symlink("fs/afs", NULL, "../self/net/afs");
  167. if (!afs_proc_symlink) {
  168. ret = -ENOMEM;
  169. goto error_proc;
  170. }
  171. return ret;
  172. error_proc:
  173. afs_fs_exit();
  174. error_fs:
  175. unregister_pernet_device(&afs_net_ops);
  176. error_net:
  177. destroy_workqueue(afs_lock_manager);
  178. error_lockmgr:
  179. destroy_workqueue(afs_async_calls);
  180. error_async:
  181. destroy_workqueue(afs_wq);
  182. error_afs_wq:
  183. rcu_barrier();
  184. printk(KERN_ERR "kAFS: failed to register: %d\n", ret);
  185. return ret;
  186. }
  187. /* XXX late_initcall is kludgy, but the only alternative seems to create
  188. * a transport upon the first mount, which is worse. Or is it?
  189. */
  190. late_initcall(afs_init); /* must be called after net/ to create socket */
  191. /*
  192. * clean up on module removal
  193. */
  194. static void __exit afs_exit(void)
  195. {
  196. printk(KERN_INFO "kAFS: Red Hat AFS client v0.1 unregistering.\n");
  197. proc_remove(afs_proc_symlink);
  198. afs_fs_exit();
  199. unregister_pernet_device(&afs_net_ops);
  200. destroy_workqueue(afs_lock_manager);
  201. destroy_workqueue(afs_async_calls);
  202. destroy_workqueue(afs_wq);
  203. afs_clean_up_permit_cache();
  204. rcu_barrier();
  205. }
  206. module_exit(afs_exit);