blob: c6bd956454995906983460151a641cb13cffd9ed [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howellsec268152007-04-26 15:49:28 -07002/* AFS client file system
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
David Howells9b3f26c2009-04-03 16:42:41 +01004 * Copyright (C) 2002,5 Red Hat, Inc. All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Written by David Howells ([email protected])
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
8#include <linux/module.h>
9#include <linux/moduleparam.h>
10#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/completion.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040012#include <linux/sched.h>
David Howellse0661df2016-08-30 16:05:14 +010013#include <linux/random.h>
David Howells5b86d4f2018-05-18 11:46:15 +010014#include <linux/proc_fs.h>
David Howells8e8d7f12017-01-05 10:38:34 +000015#define CREATE_TRACE_POINTS
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "internal.h"
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018MODULE_DESCRIPTION("AFS Client File System");
19MODULE_AUTHOR("Red Hat, Inc.");
20MODULE_LICENSE("GPL");
Greg Kroah-Hartmand483eed2020-07-02 12:51:03 +020021MODULE_IMPORT_NS(ANDROID_GKI_VFS_EXPORT_ONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
David Howells08e0e7c2007-04-26 15:55:03 -070023unsigned afs_debug;
24module_param_named(debug, afs_debug, uint, S_IWUSR | S_IRUGO);
Paul Bolle424b00e2008-04-16 11:08:22 +010025MODULE_PARM_DESC(debug, "AFS debugging mask");
David Howells08e0e7c2007-04-26 15:55:03 -070026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027static char *rootcell;
28
29module_param(rootcell, charp, 0);
30MODULE_PARM_DESC(rootcell, "root AFS cell name and VL server IP addr list");
31
Tejun Heo0ad53ee2011-01-14 15:56:37 +000032struct workqueue_struct *afs_wq;
David Howells5b86d4f2018-05-18 11:46:15 +010033static struct proc_dir_entry *afs_proc_symlink;
David Howellsf044c882017-11-02 15:27:45 +000034
David Howells6f8880d2018-04-09 21:12:31 +010035#if defined(CONFIG_ALPHA)
36const char afs_init_sysname[] = "alpha_linux26";
37#elif defined(CONFIG_X86_64)
38const char afs_init_sysname[] = "amd64_linux26";
39#elif defined(CONFIG_ARM)
40const char afs_init_sysname[] = "arm_linux26";
41#elif defined(CONFIG_ARM64)
42const char afs_init_sysname[] = "aarch64_linux26";
43#elif defined(CONFIG_X86_32)
44const char afs_init_sysname[] = "i386_linux26";
45#elif defined(CONFIG_IA64)
46const char afs_init_sysname[] = "ia64_linux26";
47#elif defined(CONFIG_PPC64)
48const char afs_init_sysname[] = "ppc64_linux26";
49#elif defined(CONFIG_PPC32)
50const char afs_init_sysname[] = "ppc_linux26";
51#elif defined(CONFIG_S390)
52#ifdef CONFIG_64BIT
53const char afs_init_sysname[] = "s390x_linux26";
54#else
55const char afs_init_sysname[] = "s390_linux26";
56#endif
57#elif defined(CONFIG_SPARC64)
58const char afs_init_sysname[] = "sparc64_linux26";
59#elif defined(CONFIG_SPARC32)
60const char afs_init_sysname[] = "sparc_linux26";
61#else
62const char afs_init_sysname[] = "unknown_linux26";
63#endif
64
David Howellsf044c882017-11-02 15:27:45 +000065/*
66 * Initialise an AFS network namespace record.
67 */
David Howells5b86d4f2018-05-18 11:46:15 +010068static int __net_init afs_net_init(struct net *net_ns)
David Howellsf044c882017-11-02 15:27:45 +000069{
David Howells6f8880d2018-04-09 21:12:31 +010070 struct afs_sysnames *sysnames;
David Howells5b86d4f2018-05-18 11:46:15 +010071 struct afs_net *net = afs_net(net_ns);
David Howellsf044c882017-11-02 15:27:45 +000072 int ret;
73
David Howells5b86d4f2018-05-18 11:46:15 +010074 net->net = net_ns;
David Howellsf044c882017-11-02 15:27:45 +000075 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);
David Howells989782d2017-11-02 15:27:50 +000080
81 net->cells = RB_ROOT;
David Howells92e3cc92020-10-09 14:11:58 +010082 init_rwsem(&net->cells_lock);
David Howells989782d2017-11-02 15:27:50 +000083 INIT_WORK(&net->cells_manager, afs_manage_cells);
84 timer_setup(&net->cells_timer, afs_cells_timer, 0);
85
David Howells8a070a92020-04-25 10:26:02 +010086 mutex_init(&net->cells_alias_lock);
David Howells0da0b7f2018-06-15 15:19:22 +010087 mutex_init(&net->proc_cells_lock);
David Howells6b3944e2018-10-11 22:45:49 +010088 INIT_HLIST_HEAD(&net->proc_cells);
David Howells989782d2017-11-02 15:27:50 +000089
David Howellsd2ddc772017-11-02 15:27:50 +000090 seqlock_init(&net->fs_lock);
91 net->fs_servers = RB_ROOT;
David Howellsf6cbb362020-04-24 15:10:00 +010092 INIT_LIST_HEAD(&net->fs_probe_fast);
93 INIT_LIST_HEAD(&net->fs_probe_slow);
David Howellsd2ddc772017-11-02 15:27:50 +000094 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);
David Howellsf6cbb362020-04-24 15:10:00 +0100102 INIT_WORK(&net->fs_prober, afs_fs_probe_dispatcher);
103 timer_setup(&net->fs_probe_timer, afs_fs_probe_timer, 0);
David Howells5481fc62020-06-19 23:39:36 +0100104 atomic_set(&net->servers_outstanding, 1);
David Howellsf044c882017-11-02 15:27:45 +0000105
David Howells6f8880d2018-04-09 21:12:31 +0100106 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
David Howellsf044c882017-11-02 15:27:45 +0000116 /* 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
133error_open_socket:
David Howells989782d2017-11-02 15:27:50 +0000134 net->live = false;
David Howells5481fc62020-06-19 23:39:36 +0100135 afs_fs_probe_cleanup(net);
David Howellsf044c882017-11-02 15:27:45 +0000136 afs_cell_purge(net);
David Howellsd2ddc772017-11-02 15:27:50 +0000137 afs_purge_servers(net);
David Howellsf044c882017-11-02 15:27:45 +0000138error_cell_init:
David Howells989782d2017-11-02 15:27:50 +0000139 net->live = false;
David Howellsf044c882017-11-02 15:27:45 +0000140 afs_proc_cleanup(net);
141error_proc:
David Howells6f8880d2018-04-09 21:12:31 +0100142 afs_put_sysnames(net->sysnames);
143error_sysnames:
David Howells989782d2017-11-02 15:27:50 +0000144 net->live = false;
David Howellsf044c882017-11-02 15:27:45 +0000145 return ret;
146}
147
148/*
149 * Clean up and destroy an AFS network namespace record.
150 */
David Howells5b86d4f2018-05-18 11:46:15 +0100151static void __net_exit afs_net_exit(struct net *net_ns)
David Howellsf044c882017-11-02 15:27:45 +0000152{
David Howells5b86d4f2018-05-18 11:46:15 +0100153 struct afs_net *net = afs_net(net_ns);
154
David Howellsf044c882017-11-02 15:27:45 +0000155 net->live = false;
David Howells5481fc62020-06-19 23:39:36 +0100156 afs_fs_probe_cleanup(net);
David Howellsf044c882017-11-02 15:27:45 +0000157 afs_cell_purge(net);
David Howellsd2ddc772017-11-02 15:27:50 +0000158 afs_purge_servers(net);
David Howellse3b2ffe2017-11-02 15:27:45 +0000159 afs_close_socket(net);
David Howellsf044c882017-11-02 15:27:45 +0000160 afs_proc_cleanup(net);
David Howells6f8880d2018-04-09 21:12:31 +0100161 afs_put_sysnames(net->sysnames);
David Howellsf044c882017-11-02 15:27:45 +0000162}
David Howellsb908fe62007-04-26 15:58:17 -0700163
David Howells5b86d4f2018-05-18 11:46:15 +0100164static 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
David Howellsb908fe62007-04-26 15:58:17 -0700171/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 * initialise the AFS client FS module
173 */
174static int __init afs_init(void)
175{
David Howellsf044c882017-11-02 15:27:45 +0000176 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 printk(KERN_INFO "kAFS: Red Hat AFS client v0.1 registering.\n");
179
Tejun Heo0ad53ee2011-01-14 15:56:37 +0000180 afs_wq = alloc_workqueue("afs", 0, 0);
181 if (!afs_wq)
David Howellsf044c882017-11-02 15:27:45 +0000182 goto error_afs_wq;
183 afs_async_calls = alloc_workqueue("kafsd", WQ_MEM_RECLAIM, 0);
184 if (!afs_async_calls)
185 goto error_async;
David Howellsf044c882017-11-02 15:27:45 +0000186 afs_lock_manager = alloc_workqueue("kafs_lockd", WQ_MEM_RECLAIM, 0);
187 if (!afs_lock_manager)
188 goto error_lockmgr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
David Howells9b3f26c2009-04-03 16:42:41 +0100190#ifdef CONFIG_AFS_FSCACHE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 /* we want to be able to cache */
David Howells9b3f26c2009-04-03 16:42:41 +0100192 ret = fscache_register_netfs(&afs_cache_netfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 goto error_cache;
195#endif
196
David Howellse5ed4e02021-01-29 23:53:50 +0000197 ret = register_pernet_device(&afs_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (ret < 0)
David Howellsf044c882017-11-02 15:27:45 +0000199 goto error_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 /* register the filesystems */
202 ret = afs_fs_init();
203 if (ret < 0)
David Howellsec268152007-04-26 15:49:28 -0700204 goto error_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
David Howells5b86d4f2018-05-18 11:46:15 +0100206 afs_proc_symlink = proc_symlink("fs/afs", NULL, "../self/net/afs");
Dan Carpenter090b1bb2021-06-15 08:39:52 +0100207 if (!afs_proc_symlink) {
208 ret = -ENOMEM;
David Howells5b86d4f2018-05-18 11:46:15 +0100209 goto error_proc;
210 }
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return ret;
213
David Howells5b86d4f2018-05-18 11:46:15 +0100214error_proc:
215 afs_fs_exit();
David Howellsec268152007-04-26 15:49:28 -0700216error_fs:
David Howellse5ed4e02021-01-29 23:53:50 +0000217 unregister_pernet_device(&afs_net_ops);
David Howellsf044c882017-11-02 15:27:45 +0000218error_net:
David Howells9b3f26c2009-04-03 16:42:41 +0100219#ifdef CONFIG_AFS_FSCACHE
220 fscache_unregister_netfs(&afs_cache_netfs);
David Howellsec268152007-04-26 15:49:28 -0700221error_cache:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222#endif
David Howellsf044c882017-11-02 15:27:45 +0000223 destroy_workqueue(afs_lock_manager);
224error_lockmgr:
David Howellsf044c882017-11-02 15:27:45 +0000225 destroy_workqueue(afs_async_calls);
226error_async:
Tejun Heo0ad53ee2011-01-14 15:56:37 +0000227 destroy_workqueue(afs_wq);
David Howellsf044c882017-11-02 15:27:45 +0000228error_afs_wq:
David Howells416351f2007-05-09 02:33:45 -0700229 rcu_barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 printk(KERN_ERR "kAFS: failed to register: %d\n", ret);
231 return ret;
David Howellsec268152007-04-26 15:49:28 -0700232}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
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 */
237late_initcall(afs_init); /* must be called after net/ to create socket */
David Howellsec268152007-04-26 15:49:28 -0700238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239/*
240 * clean up on module removal
241 */
242static void __exit afs_exit(void)
243{
244 printk(KERN_INFO "kAFS: Red Hat AFS client v0.1 unregistering.\n");
245
David Howells5b86d4f2018-05-18 11:46:15 +0100246 proc_remove(afs_proc_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 afs_fs_exit();
David Howellse5ed4e02021-01-29 23:53:50 +0000248 unregister_pernet_device(&afs_net_ops);
David Howells9b3f26c2009-04-03 16:42:41 +0100249#ifdef CONFIG_AFS_FSCACHE
250 fscache_unregister_netfs(&afs_cache_netfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251#endif
David Howellsf044c882017-11-02 15:27:45 +0000252 destroy_workqueue(afs_lock_manager);
David Howellsf044c882017-11-02 15:27:45 +0000253 destroy_workqueue(afs_async_calls);
254 destroy_workqueue(afs_wq);
David Howellsbe080a62017-11-02 15:27:49 +0000255 afs_clean_up_permit_cache();
David Howells416351f2007-05-09 02:33:45 -0700256 rcu_barrier();
David Howellsec268152007-04-26 15:49:28 -0700257}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259module_exit(afs_exit);