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