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