xref: /linux/fs/afs/main.c (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
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_PPC64)
456f8880d8SDavid Howells const char afs_init_sysname[] = "ppc64_linux26";
466f8880d8SDavid Howells #elif defined(CONFIG_PPC32)
476f8880d8SDavid Howells const char afs_init_sysname[] = "ppc_linux26";
486f8880d8SDavid Howells #elif defined(CONFIG_S390)
496f8880d8SDavid Howells #ifdef CONFIG_64BIT
506f8880d8SDavid Howells const char afs_init_sysname[] = "s390x_linux26";
516f8880d8SDavid Howells #else
526f8880d8SDavid Howells const char afs_init_sysname[] = "s390_linux26";
536f8880d8SDavid Howells #endif
546f8880d8SDavid Howells #elif defined(CONFIG_SPARC64)
556f8880d8SDavid Howells const char afs_init_sysname[] = "sparc64_linux26";
566f8880d8SDavid Howells #elif defined(CONFIG_SPARC32)
576f8880d8SDavid Howells const char afs_init_sysname[] = "sparc_linux26";
586f8880d8SDavid Howells #else
596f8880d8SDavid Howells const char afs_init_sysname[] = "unknown_linux26";
606f8880d8SDavid Howells #endif
616f8880d8SDavid Howells 
62f044c884SDavid Howells /*
63f044c884SDavid Howells  * Initialise an AFS network namespace record.
64f044c884SDavid Howells  */
afs_net_init(struct net * net_ns)655b86d4ffSDavid Howells static int __net_init afs_net_init(struct net *net_ns)
66f044c884SDavid Howells {
676f8880d8SDavid Howells 	struct afs_sysnames *sysnames;
685b86d4ffSDavid Howells 	struct afs_net *net = afs_net(net_ns);
69f044c884SDavid Howells 	int ret;
70f044c884SDavid Howells 
715b86d4ffSDavid Howells 	net->net = net_ns;
72f044c884SDavid Howells 	net->live = true;
73f044c884SDavid Howells 	generate_random_uuid((unsigned char *)&net->uuid);
74f044c884SDavid Howells 
75f044c884SDavid Howells 	INIT_WORK(&net->charge_preallocation_work, afs_charge_preallocation);
76f044c884SDavid Howells 	mutex_init(&net->socket_mutex);
77989782dcSDavid Howells 
78989782dcSDavid Howells 	net->cells = RB_ROOT;
7992e3cc91SDavid Howells 	init_rwsem(&net->cells_lock);
80989782dcSDavid Howells 	INIT_WORK(&net->cells_manager, afs_manage_cells);
81989782dcSDavid Howells 	timer_setup(&net->cells_timer, afs_cells_timer, 0);
82989782dcSDavid Howells 
838a070a96SDavid Howells 	mutex_init(&net->cells_alias_lock);
840da0b7fdSDavid Howells 	mutex_init(&net->proc_cells_lock);
856b3944e4SDavid Howells 	INIT_HLIST_HEAD(&net->proc_cells);
86989782dcSDavid Howells 
87d2ddc776SDavid Howells 	seqlock_init(&net->fs_lock);
88d2ddc776SDavid Howells 	net->fs_servers = RB_ROOT;
89f6cbb368SDavid Howells 	INIT_LIST_HEAD(&net->fs_probe_fast);
90f6cbb368SDavid Howells 	INIT_LIST_HEAD(&net->fs_probe_slow);
91d2ddc776SDavid Howells 	INIT_HLIST_HEAD(&net->fs_proc);
92d2ddc776SDavid Howells 
93*bfacaf71SMarc Dionne 	INIT_HLIST_HEAD(&net->fs_addresses);
94d2ddc776SDavid Howells 	seqlock_init(&net->fs_addr_lock);
95d2ddc776SDavid Howells 
96d2ddc776SDavid Howells 	INIT_WORK(&net->fs_manager, afs_manage_servers);
97d2ddc776SDavid Howells 	timer_setup(&net->fs_timer, afs_servers_timer, 0);
98f6cbb368SDavid Howells 	INIT_WORK(&net->fs_prober, afs_fs_probe_dispatcher);
99f6cbb368SDavid Howells 	timer_setup(&net->fs_probe_timer, afs_fs_probe_timer, 0);
1005481fc6eSDavid Howells 	atomic_set(&net->servers_outstanding, 1);
101f044c884SDavid Howells 
1026f8880d8SDavid Howells 	ret = -ENOMEM;
1036f8880d8SDavid Howells 	sysnames = kzalloc(sizeof(*sysnames), GFP_KERNEL);
1046f8880d8SDavid Howells 	if (!sysnames)
1056f8880d8SDavid Howells 		goto error_sysnames;
1066f8880d8SDavid Howells 	sysnames->subs[0] = (char *)&afs_init_sysname;
1076f8880d8SDavid Howells 	sysnames->nr = 1;
1086f8880d8SDavid Howells 	refcount_set(&sysnames->usage, 1);
1096f8880d8SDavid Howells 	net->sysnames = sysnames;
1106f8880d8SDavid Howells 	rwlock_init(&net->sysnames_lock);
1116f8880d8SDavid Howells 
112f044c884SDavid Howells 	/* Register the /proc stuff */
113f044c884SDavid Howells 	ret = afs_proc_init(net);
114f044c884SDavid Howells 	if (ret < 0)
115f044c884SDavid Howells 		goto error_proc;
116f044c884SDavid Howells 
117f044c884SDavid Howells 	/* Initialise the cell DB */
118f044c884SDavid Howells 	ret = afs_cell_init(net, rootcell);
119f044c884SDavid Howells 	if (ret < 0)
120f044c884SDavid Howells 		goto error_cell_init;
121f044c884SDavid Howells 
122f044c884SDavid Howells 	/* Create the RxRPC transport */
123f044c884SDavid Howells 	ret = afs_open_socket(net);
124f044c884SDavid Howells 	if (ret < 0)
125f044c884SDavid Howells 		goto error_open_socket;
126f044c884SDavid Howells 
127f044c884SDavid Howells 	return 0;
128f044c884SDavid Howells 
129f044c884SDavid Howells error_open_socket:
130989782dcSDavid Howells 	net->live = false;
1315481fc6eSDavid Howells 	afs_fs_probe_cleanup(net);
132f044c884SDavid Howells 	afs_cell_purge(net);
133d2ddc776SDavid Howells 	afs_purge_servers(net);
134f044c884SDavid Howells error_cell_init:
135989782dcSDavid Howells 	net->live = false;
136f044c884SDavid Howells 	afs_proc_cleanup(net);
137f044c884SDavid Howells error_proc:
1386f8880d8SDavid Howells 	afs_put_sysnames(net->sysnames);
1396f8880d8SDavid Howells error_sysnames:
140989782dcSDavid Howells 	net->live = false;
141f044c884SDavid Howells 	return ret;
142f044c884SDavid Howells }
143f044c884SDavid Howells 
144f044c884SDavid Howells /*
145f044c884SDavid Howells  * Clean up and destroy an AFS network namespace record.
146f044c884SDavid Howells  */
afs_net_exit(struct net * net_ns)1475b86d4ffSDavid Howells static void __net_exit afs_net_exit(struct net *net_ns)
148f044c884SDavid Howells {
1495b86d4ffSDavid Howells 	struct afs_net *net = afs_net(net_ns);
1505b86d4ffSDavid Howells 
151f044c884SDavid Howells 	net->live = false;
1525481fc6eSDavid Howells 	afs_fs_probe_cleanup(net);
153f044c884SDavid Howells 	afs_cell_purge(net);
154d2ddc776SDavid Howells 	afs_purge_servers(net);
155e3b2ffe0SDavid Howells 	afs_close_socket(net);
156f044c884SDavid Howells 	afs_proc_cleanup(net);
1576f8880d8SDavid Howells 	afs_put_sysnames(net->sysnames);
158f94f70d3SDavid Howells 	kfree_rcu(rcu_access_pointer(net->address_prefs), rcu);
159f044c884SDavid Howells }
160b908fe6bSDavid Howells 
1615b86d4ffSDavid Howells static struct pernet_operations afs_net_ops = {
1625b86d4ffSDavid Howells 	.init	= afs_net_init,
1635b86d4ffSDavid Howells 	.exit	= afs_net_exit,
1645b86d4ffSDavid Howells 	.id	= &afs_net_id,
1655b86d4ffSDavid Howells 	.size	= sizeof(struct afs_net),
1665b86d4ffSDavid Howells };
1675b86d4ffSDavid Howells 
168b908fe6bSDavid Howells /*
1691da177e4SLinus Torvalds  * initialise the AFS client FS module
1701da177e4SLinus Torvalds  */
afs_init(void)1711da177e4SLinus Torvalds static int __init afs_init(void)
1721da177e4SLinus Torvalds {
173f044c884SDavid Howells 	int ret = -ENOMEM;
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 	printk(KERN_INFO "kAFS: Red Hat AFS client v0.1 registering.\n");
1761da177e4SLinus Torvalds 
1770ad53eeeSTejun Heo 	afs_wq = alloc_workqueue("afs", 0, 0);
1780ad53eeeSTejun Heo 	if (!afs_wq)
179f044c884SDavid Howells 		goto error_afs_wq;
180f044c884SDavid Howells 	afs_async_calls = alloc_workqueue("kafsd", WQ_MEM_RECLAIM, 0);
181f044c884SDavid Howells 	if (!afs_async_calls)
182f044c884SDavid Howells 		goto error_async;
183f044c884SDavid Howells 	afs_lock_manager = alloc_workqueue("kafs_lockd", WQ_MEM_RECLAIM, 0);
184f044c884SDavid Howells 	if (!afs_lock_manager)
185f044c884SDavid Howells 		goto error_lockmgr;
1861da177e4SLinus Torvalds 
1875399d522SDavid Howells 	ret = register_pernet_device(&afs_net_ops);
1881da177e4SLinus Torvalds 	if (ret < 0)
189f044c884SDavid Howells 		goto error_net;
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds 	/* register the filesystems */
1921da177e4SLinus Torvalds 	ret = afs_fs_init();
1931da177e4SLinus Torvalds 	if (ret < 0)
194ec26815aSDavid Howells 		goto error_fs;
1951da177e4SLinus Torvalds 
1965b86d4ffSDavid Howells 	afs_proc_symlink = proc_symlink("fs/afs", NULL, "../self/net/afs");
197a33d6266SDan Carpenter 	if (!afs_proc_symlink) {
198a33d6266SDan Carpenter 		ret = -ENOMEM;
1995b86d4ffSDavid Howells 		goto error_proc;
2005b86d4ffSDavid Howells 	}
2015b86d4ffSDavid Howells 
2021da177e4SLinus Torvalds 	return ret;
2031da177e4SLinus Torvalds 
2045b86d4ffSDavid Howells error_proc:
2055b86d4ffSDavid Howells 	afs_fs_exit();
206ec26815aSDavid Howells error_fs:
2075399d522SDavid Howells 	unregister_pernet_device(&afs_net_ops);
208f044c884SDavid Howells error_net:
209f044c884SDavid Howells 	destroy_workqueue(afs_lock_manager);
210f044c884SDavid Howells error_lockmgr:
211f044c884SDavid Howells 	destroy_workqueue(afs_async_calls);
212f044c884SDavid Howells error_async:
2130ad53eeeSTejun Heo 	destroy_workqueue(afs_wq);
214f044c884SDavid Howells error_afs_wq:
215416351f2SDavid Howells 	rcu_barrier();
2161da177e4SLinus Torvalds 	printk(KERN_ERR "kAFS: failed to register: %d\n", ret);
2171da177e4SLinus Torvalds 	return ret;
218ec26815aSDavid Howells }
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds /* XXX late_initcall is kludgy, but the only alternative seems to create
2211da177e4SLinus Torvalds  * a transport upon the first mount, which is worse. Or is it?
2221da177e4SLinus Torvalds  */
2231da177e4SLinus Torvalds late_initcall(afs_init);	/* must be called after net/ to create socket */
224ec26815aSDavid Howells 
2251da177e4SLinus Torvalds /*
2261da177e4SLinus Torvalds  * clean up on module removal
2271da177e4SLinus Torvalds  */
afs_exit(void)2281da177e4SLinus Torvalds static void __exit afs_exit(void)
2291da177e4SLinus Torvalds {
2301da177e4SLinus Torvalds 	printk(KERN_INFO "kAFS: Red Hat AFS client v0.1 unregistering.\n");
2311da177e4SLinus Torvalds 
2325b86d4ffSDavid Howells 	proc_remove(afs_proc_symlink);
2331da177e4SLinus Torvalds 	afs_fs_exit();
2345399d522SDavid Howells 	unregister_pernet_device(&afs_net_ops);
235f044c884SDavid Howells 	destroy_workqueue(afs_lock_manager);
236f044c884SDavid Howells 	destroy_workqueue(afs_async_calls);
237f044c884SDavid Howells 	destroy_workqueue(afs_wq);
238be080a6fSDavid Howells 	afs_clean_up_permit_cache();
239416351f2SDavid Howells 	rcu_barrier();
240ec26815aSDavid Howells }
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds module_exit(afs_exit);
243