xref: /openbmc/linux/fs/afs/main.c (revision 5481fc6eb8a7f4b76d8ad1be371d2e11b22bfb55)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2ec26815aSDavid Howells /* AFS client file system
31da177e4SLinus Torvalds  *
49b3f26c9SDavid Howells  * Copyright (C) 2002,5 Red Hat, Inc. All Rights Reserved.
51da177e4SLinus Torvalds  * Written by David Howells (dhowells@redhat.com)
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds #include <linux/module.h>
91da177e4SLinus Torvalds #include <linux/moduleparam.h>
101da177e4SLinus Torvalds #include <linux/init.h>
111da177e4SLinus Torvalds #include <linux/completion.h>
12e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
13e0661dfcSDavid Howells #include <linux/random.h>
145b86d4ffSDavid Howells #include <linux/proc_fs.h>
158e8d7f13SDavid Howells #define CREATE_TRACE_POINTS
161da177e4SLinus Torvalds #include "internal.h"
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds MODULE_DESCRIPTION("AFS Client File System");
191da177e4SLinus Torvalds MODULE_AUTHOR("Red Hat, Inc.");
201da177e4SLinus Torvalds MODULE_LICENSE("GPL");
211da177e4SLinus Torvalds 
2208e0e7c8SDavid Howells unsigned afs_debug;
2308e0e7c8SDavid Howells module_param_named(debug, afs_debug, uint, S_IWUSR | S_IRUGO);
24424b00e2SPaul Bolle MODULE_PARM_DESC(debug, "AFS debugging mask");
2508e0e7c8SDavid Howells 
261da177e4SLinus Torvalds static char *rootcell;
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds module_param(rootcell, charp, 0);
291da177e4SLinus Torvalds MODULE_PARM_DESC(rootcell, "root AFS cell name and VL server IP addr list");
301da177e4SLinus Torvalds 
310ad53eeeSTejun Heo struct workqueue_struct *afs_wq;
325b86d4ffSDavid Howells static struct proc_dir_entry *afs_proc_symlink;
33f044c884SDavid Howells 
346f8880d8SDavid Howells #if defined(CONFIG_ALPHA)
356f8880d8SDavid Howells const char afs_init_sysname[] = "alpha_linux26";
366f8880d8SDavid Howells #elif defined(CONFIG_X86_64)
376f8880d8SDavid Howells const char afs_init_sysname[] = "amd64_linux26";
386f8880d8SDavid Howells #elif defined(CONFIG_ARM)
396f8880d8SDavid Howells const char afs_init_sysname[] = "arm_linux26";
406f8880d8SDavid Howells #elif defined(CONFIG_ARM64)
416f8880d8SDavid Howells const char afs_init_sysname[] = "aarch64_linux26";
426f8880d8SDavid Howells #elif defined(CONFIG_X86_32)
436f8880d8SDavid Howells const char afs_init_sysname[] = "i386_linux26";
446f8880d8SDavid Howells #elif defined(CONFIG_IA64)
456f8880d8SDavid Howells const char afs_init_sysname[] = "ia64_linux26";
466f8880d8SDavid Howells #elif defined(CONFIG_PPC64)
476f8880d8SDavid Howells const char afs_init_sysname[] = "ppc64_linux26";
486f8880d8SDavid Howells #elif defined(CONFIG_PPC32)
496f8880d8SDavid Howells const char afs_init_sysname[] = "ppc_linux26";
506f8880d8SDavid Howells #elif defined(CONFIG_S390)
516f8880d8SDavid Howells #ifdef CONFIG_64BIT
526f8880d8SDavid Howells const char afs_init_sysname[] = "s390x_linux26";
536f8880d8SDavid Howells #else
546f8880d8SDavid Howells const char afs_init_sysname[] = "s390_linux26";
556f8880d8SDavid Howells #endif
566f8880d8SDavid Howells #elif defined(CONFIG_SPARC64)
576f8880d8SDavid Howells const char afs_init_sysname[] = "sparc64_linux26";
586f8880d8SDavid Howells #elif defined(CONFIG_SPARC32)
596f8880d8SDavid Howells const char afs_init_sysname[] = "sparc_linux26";
606f8880d8SDavid Howells #else
616f8880d8SDavid Howells const char afs_init_sysname[] = "unknown_linux26";
626f8880d8SDavid Howells #endif
636f8880d8SDavid Howells 
64f044c884SDavid Howells /*
65f044c884SDavid Howells  * Initialise an AFS network namespace record.
66f044c884SDavid Howells  */
675b86d4ffSDavid Howells static int __net_init afs_net_init(struct net *net_ns)
68f044c884SDavid Howells {
696f8880d8SDavid Howells 	struct afs_sysnames *sysnames;
705b86d4ffSDavid Howells 	struct afs_net *net = afs_net(net_ns);
71f044c884SDavid Howells 	int ret;
72f044c884SDavid Howells 
735b86d4ffSDavid Howells 	net->net = net_ns;
74f044c884SDavid Howells 	net->live = true;
75f044c884SDavid Howells 	generate_random_uuid((unsigned char *)&net->uuid);
76f044c884SDavid Howells 
77f044c884SDavid Howells 	INIT_WORK(&net->charge_preallocation_work, afs_charge_preallocation);
78f044c884SDavid Howells 	mutex_init(&net->socket_mutex);
79989782dcSDavid Howells 
80989782dcSDavid Howells 	net->cells = RB_ROOT;
81989782dcSDavid Howells 	seqlock_init(&net->cells_lock);
82989782dcSDavid Howells 	INIT_WORK(&net->cells_manager, afs_manage_cells);
83989782dcSDavid Howells 	timer_setup(&net->cells_timer, afs_cells_timer, 0);
84989782dcSDavid Howells 
858a070a96SDavid Howells 	mutex_init(&net->cells_alias_lock);
860da0b7fdSDavid Howells 	mutex_init(&net->proc_cells_lock);
876b3944e4SDavid Howells 	INIT_HLIST_HEAD(&net->proc_cells);
88989782dcSDavid Howells 
89d2ddc776SDavid Howells 	seqlock_init(&net->fs_lock);
90d2ddc776SDavid Howells 	net->fs_servers = RB_ROOT;
91f6cbb368SDavid Howells 	INIT_LIST_HEAD(&net->fs_probe_fast);
92f6cbb368SDavid Howells 	INIT_LIST_HEAD(&net->fs_probe_slow);
93d2ddc776SDavid Howells 	INIT_HLIST_HEAD(&net->fs_proc);
94d2ddc776SDavid Howells 
95d2ddc776SDavid Howells 	INIT_HLIST_HEAD(&net->fs_addresses4);
96d2ddc776SDavid Howells 	INIT_HLIST_HEAD(&net->fs_addresses6);
97d2ddc776SDavid Howells 	seqlock_init(&net->fs_addr_lock);
98d2ddc776SDavid Howells 
99d2ddc776SDavid Howells 	INIT_WORK(&net->fs_manager, afs_manage_servers);
100d2ddc776SDavid Howells 	timer_setup(&net->fs_timer, afs_servers_timer, 0);
101f6cbb368SDavid Howells 	INIT_WORK(&net->fs_prober, afs_fs_probe_dispatcher);
102f6cbb368SDavid Howells 	timer_setup(&net->fs_probe_timer, afs_fs_probe_timer, 0);
103*5481fc6eSDavid Howells 	atomic_set(&net->servers_outstanding, 1);
104f044c884SDavid Howells 
1056f8880d8SDavid Howells 	ret = -ENOMEM;
1066f8880d8SDavid Howells 	sysnames = kzalloc(sizeof(*sysnames), GFP_KERNEL);
1076f8880d8SDavid Howells 	if (!sysnames)
1086f8880d8SDavid Howells 		goto error_sysnames;
1096f8880d8SDavid Howells 	sysnames->subs[0] = (char *)&afs_init_sysname;
1106f8880d8SDavid Howells 	sysnames->nr = 1;
1116f8880d8SDavid Howells 	refcount_set(&sysnames->usage, 1);
1126f8880d8SDavid Howells 	net->sysnames = sysnames;
1136f8880d8SDavid Howells 	rwlock_init(&net->sysnames_lock);
1146f8880d8SDavid Howells 
115f044c884SDavid Howells 	/* Register the /proc stuff */
116f044c884SDavid Howells 	ret = afs_proc_init(net);
117f044c884SDavid Howells 	if (ret < 0)
118f044c884SDavid Howells 		goto error_proc;
119f044c884SDavid Howells 
120f044c884SDavid Howells 	/* Initialise the cell DB */
121f044c884SDavid Howells 	ret = afs_cell_init(net, rootcell);
122f044c884SDavid Howells 	if (ret < 0)
123f044c884SDavid Howells 		goto error_cell_init;
124f044c884SDavid Howells 
125f044c884SDavid Howells 	/* Create the RxRPC transport */
126f044c884SDavid Howells 	ret = afs_open_socket(net);
127f044c884SDavid Howells 	if (ret < 0)
128f044c884SDavid Howells 		goto error_open_socket;
129f044c884SDavid Howells 
130f044c884SDavid Howells 	return 0;
131f044c884SDavid Howells 
132f044c884SDavid Howells error_open_socket:
133989782dcSDavid Howells 	net->live = false;
134*5481fc6eSDavid Howells 	afs_fs_probe_cleanup(net);
135f044c884SDavid Howells 	afs_cell_purge(net);
136d2ddc776SDavid Howells 	afs_purge_servers(net);
137f044c884SDavid Howells error_cell_init:
138989782dcSDavid Howells 	net->live = false;
139f044c884SDavid Howells 	afs_proc_cleanup(net);
140f044c884SDavid Howells error_proc:
1416f8880d8SDavid Howells 	afs_put_sysnames(net->sysnames);
1426f8880d8SDavid Howells error_sysnames:
143989782dcSDavid Howells 	net->live = false;
144f044c884SDavid Howells 	return ret;
145f044c884SDavid Howells }
146f044c884SDavid Howells 
147f044c884SDavid Howells /*
148f044c884SDavid Howells  * Clean up and destroy an AFS network namespace record.
149f044c884SDavid Howells  */
1505b86d4ffSDavid Howells static void __net_exit afs_net_exit(struct net *net_ns)
151f044c884SDavid Howells {
1525b86d4ffSDavid Howells 	struct afs_net *net = afs_net(net_ns);
1535b86d4ffSDavid Howells 
154f044c884SDavid Howells 	net->live = false;
155*5481fc6eSDavid Howells 	afs_fs_probe_cleanup(net);
156f044c884SDavid Howells 	afs_cell_purge(net);
157d2ddc776SDavid Howells 	afs_purge_servers(net);
158e3b2ffe0SDavid Howells 	afs_close_socket(net);
159f044c884SDavid Howells 	afs_proc_cleanup(net);
1606f8880d8SDavid Howells 	afs_put_sysnames(net->sysnames);
161f044c884SDavid Howells }
162b908fe6bSDavid Howells 
1635b86d4ffSDavid Howells static struct pernet_operations afs_net_ops = {
1645b86d4ffSDavid Howells 	.init	= afs_net_init,
1655b86d4ffSDavid Howells 	.exit	= afs_net_exit,
1665b86d4ffSDavid Howells 	.id	= &afs_net_id,
1675b86d4ffSDavid Howells 	.size	= sizeof(struct afs_net),
1685b86d4ffSDavid Howells };
1695b86d4ffSDavid Howells 
170b908fe6bSDavid Howells /*
1711da177e4SLinus Torvalds  * initialise the AFS client FS module
1721da177e4SLinus Torvalds  */
1731da177e4SLinus Torvalds static int __init afs_init(void)
1741da177e4SLinus Torvalds {
175f044c884SDavid Howells 	int ret = -ENOMEM;
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 	printk(KERN_INFO "kAFS: Red Hat AFS client v0.1 registering.\n");
1781da177e4SLinus Torvalds 
1790ad53eeeSTejun Heo 	afs_wq = alloc_workqueue("afs", 0, 0);
1800ad53eeeSTejun Heo 	if (!afs_wq)
181f044c884SDavid Howells 		goto error_afs_wq;
182f044c884SDavid Howells 	afs_async_calls = alloc_workqueue("kafsd", WQ_MEM_RECLAIM, 0);
183f044c884SDavid Howells 	if (!afs_async_calls)
184f044c884SDavid Howells 		goto error_async;
185f044c884SDavid Howells 	afs_lock_manager = alloc_workqueue("kafs_lockd", WQ_MEM_RECLAIM, 0);
186f044c884SDavid Howells 	if (!afs_lock_manager)
187f044c884SDavid Howells 		goto error_lockmgr;
1881da177e4SLinus Torvalds 
1899b3f26c9SDavid Howells #ifdef CONFIG_AFS_FSCACHE
1901da177e4SLinus Torvalds 	/* we want to be able to cache */
1919b3f26c9SDavid Howells 	ret = fscache_register_netfs(&afs_cache_netfs);
1921da177e4SLinus Torvalds 	if (ret < 0)
1931da177e4SLinus Torvalds 		goto error_cache;
1941da177e4SLinus Torvalds #endif
1951da177e4SLinus Torvalds 
1965b86d4ffSDavid Howells 	ret = register_pernet_subsys(&afs_net_ops);
1971da177e4SLinus Torvalds 	if (ret < 0)
198f044c884SDavid Howells 		goto error_net;
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds 	/* register the filesystems */
2011da177e4SLinus Torvalds 	ret = afs_fs_init();
2021da177e4SLinus Torvalds 	if (ret < 0)
203ec26815aSDavid Howells 		goto error_fs;
2041da177e4SLinus Torvalds 
2055b86d4ffSDavid Howells 	afs_proc_symlink = proc_symlink("fs/afs", NULL, "../self/net/afs");
2065b86d4ffSDavid Howells 	if (IS_ERR(afs_proc_symlink)) {
2075b86d4ffSDavid Howells 		ret = PTR_ERR(afs_proc_symlink);
2085b86d4ffSDavid Howells 		goto error_proc;
2095b86d4ffSDavid Howells 	}
2105b86d4ffSDavid Howells 
2111da177e4SLinus Torvalds 	return ret;
2121da177e4SLinus Torvalds 
2135b86d4ffSDavid Howells error_proc:
2145b86d4ffSDavid Howells 	afs_fs_exit();
215ec26815aSDavid Howells error_fs:
2165b86d4ffSDavid Howells 	unregister_pernet_subsys(&afs_net_ops);
217f044c884SDavid Howells error_net:
2189b3f26c9SDavid Howells #ifdef CONFIG_AFS_FSCACHE
2199b3f26c9SDavid Howells 	fscache_unregister_netfs(&afs_cache_netfs);
220ec26815aSDavid Howells error_cache:
2211da177e4SLinus Torvalds #endif
222f044c884SDavid Howells 	destroy_workqueue(afs_lock_manager);
223f044c884SDavid Howells error_lockmgr:
224f044c884SDavid Howells 	destroy_workqueue(afs_async_calls);
225f044c884SDavid Howells error_async:
2260ad53eeeSTejun Heo 	destroy_workqueue(afs_wq);
227f044c884SDavid Howells error_afs_wq:
228416351f2SDavid Howells 	rcu_barrier();
2291da177e4SLinus Torvalds 	printk(KERN_ERR "kAFS: failed to register: %d\n", ret);
2301da177e4SLinus Torvalds 	return ret;
231ec26815aSDavid Howells }
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds /* XXX late_initcall is kludgy, but the only alternative seems to create
2341da177e4SLinus Torvalds  * a transport upon the first mount, which is worse. Or is it?
2351da177e4SLinus Torvalds  */
2361da177e4SLinus Torvalds late_initcall(afs_init);	/* must be called after net/ to create socket */
237ec26815aSDavid Howells 
2381da177e4SLinus Torvalds /*
2391da177e4SLinus Torvalds  * clean up on module removal
2401da177e4SLinus Torvalds  */
2411da177e4SLinus Torvalds static void __exit afs_exit(void)
2421da177e4SLinus Torvalds {
2431da177e4SLinus Torvalds 	printk(KERN_INFO "kAFS: Red Hat AFS client v0.1 unregistering.\n");
2441da177e4SLinus Torvalds 
2455b86d4ffSDavid Howells 	proc_remove(afs_proc_symlink);
2461da177e4SLinus Torvalds 	afs_fs_exit();
2475b86d4ffSDavid Howells 	unregister_pernet_subsys(&afs_net_ops);
2489b3f26c9SDavid Howells #ifdef CONFIG_AFS_FSCACHE
2499b3f26c9SDavid Howells 	fscache_unregister_netfs(&afs_cache_netfs);
2501da177e4SLinus Torvalds #endif
251f044c884SDavid Howells 	destroy_workqueue(afs_lock_manager);
252f044c884SDavid Howells 	destroy_workqueue(afs_async_calls);
253f044c884SDavid Howells 	destroy_workqueue(afs_wq);
254be080a6fSDavid Howells 	afs_clean_up_permit_cache();
255416351f2SDavid Howells 	rcu_barrier();
256ec26815aSDavid Howells }
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds module_exit(afs_exit);
259