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