blob: c9c45d7078bd184bc85417783160c9cb813f0222 [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 (dhowells@redhat.com)
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");
21
David Howells08e0e7c2007-04-26 15:55:03 -070022unsigned afs_debug;
23module_param_named(debug, afs_debug, uint, S_IWUSR | S_IRUGO);
Paul Bolle424b00e2008-04-16 11:08:22 +010024MODULE_PARM_DESC(debug, "AFS debugging mask");
David Howells08e0e7c2007-04-26 15:55:03 -070025
Linus Torvalds1da177e2005-04-16 15:20:36 -070026static char *rootcell;
27
28module_param(rootcell, charp, 0);
29MODULE_PARM_DESC(rootcell, "root AFS cell name and VL server IP addr list");
30
Tejun Heo0ad53ee2011-01-14 15:56:37 +000031struct workqueue_struct *afs_wq;
David Howells5b86d4f2018-05-18 11:46:15 +010032static struct proc_dir_entry *afs_proc_symlink;
David Howellsf044c882017-11-02 15:27:45 +000033
David Howells6f8880d2018-04-09 21:12:31 +010034#if defined(CONFIG_ALPHA)
35const char afs_init_sysname[] = "alpha_linux26";
36#elif defined(CONFIG_X86_64)
37const char afs_init_sysname[] = "amd64_linux26";
38#elif defined(CONFIG_ARM)
39const char afs_init_sysname[] = "arm_linux26";
40#elif defined(CONFIG_ARM64)
41const char afs_init_sysname[] = "aarch64_linux26";
42#elif defined(CONFIG_X86_32)
43const char afs_init_sysname[] = "i386_linux26";
44#elif defined(CONFIG_IA64)
45const char afs_init_sysname[] = "ia64_linux26";
46#elif defined(CONFIG_PPC64)
47const char afs_init_sysname[] = "ppc64_linux26";
48#elif defined(CONFIG_PPC32)
49const char afs_init_sysname[] = "ppc_linux26";
50#elif defined(CONFIG_S390)
51#ifdef CONFIG_64BIT
52const char afs_init_sysname[] = "s390x_linux26";
53#else
54const char afs_init_sysname[] = "s390_linux26";
55#endif
56#elif defined(CONFIG_SPARC64)
57const char afs_init_sysname[] = "sparc64_linux26";
58#elif defined(CONFIG_SPARC32)
59const char afs_init_sysname[] = "sparc_linux26";
60#else
61const char afs_init_sysname[] = "unknown_linux26";
62#endif
63
David Howellsf044c882017-11-02 15:27:45 +000064/*
65 * Initialise an AFS network namespace record.
66 */
David Howells5b86d4f2018-05-18 11:46:15 +010067static int __net_init afs_net_init(struct net *net_ns)
David Howellsf044c882017-11-02 15:27:45 +000068{
David Howells6f8880d2018-04-09 21:12:31 +010069 struct afs_sysnames *sysnames;
David Howells5b86d4f2018-05-18 11:46:15 +010070 struct afs_net *net = afs_net(net_ns);
David Howellsf044c882017-11-02 15:27:45 +000071 int ret;
72
David Howells5b86d4f2018-05-18 11:46:15 +010073 net->net = net_ns;
David Howellsf044c882017-11-02 15:27:45 +000074 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);
David Howells989782d2017-11-02 15:27:50 +000079
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
David Howells0da0b7f2018-06-15 15:19:22 +010085 mutex_init(&net->proc_cells_lock);
David Howells6b3944e2018-10-11 22:45:49 +010086 INIT_HLIST_HEAD(&net->proc_cells);
David Howells989782d2017-11-02 15:27:50 +000087
David Howellsd2ddc772017-11-02 15:27:50 +000088 seqlock_init(&net->fs_lock);
89 net->fs_servers = RB_ROOT;
90 INIT_LIST_HEAD(&net->fs_updates);
91 INIT_HLIST_HEAD(&net->fs_proc);
92
93 INIT_HLIST_HEAD(&net->fs_addresses4);
94 INIT_HLIST_HEAD(&net->fs_addresses6);
95 seqlock_init(&net->fs_addr_lock);
96
97 INIT_WORK(&net->fs_manager, afs_manage_servers);
98 timer_setup(&net->fs_timer, afs_servers_timer, 0);
David Howellsf044c882017-11-02 15:27:45 +000099
David Howells6f8880d2018-04-09 21:12:31 +0100100 ret = -ENOMEM;
101 sysnames = kzalloc(sizeof(*sysnames), GFP_KERNEL);
102 if (!sysnames)
103 goto error_sysnames;
104 sysnames->subs[0] = (char *)&afs_init_sysname;
105 sysnames->nr = 1;
106 refcount_set(&sysnames->usage, 1);
107 net->sysnames = sysnames;
108 rwlock_init(&net->sysnames_lock);
109
David Howellsf044c882017-11-02 15:27:45 +0000110 /* Register the /proc stuff */
111 ret = afs_proc_init(net);
112 if (ret < 0)
113 goto error_proc;
114
115 /* Initialise the cell DB */
116 ret = afs_cell_init(net, rootcell);
117 if (ret < 0)
118 goto error_cell_init;
119
120 /* Create the RxRPC transport */
121 ret = afs_open_socket(net);
122 if (ret < 0)
123 goto error_open_socket;
124
125 return 0;
126
127error_open_socket:
David Howells989782d2017-11-02 15:27:50 +0000128 net->live = false;
David Howellsf044c882017-11-02 15:27:45 +0000129 afs_cell_purge(net);
David Howellsd2ddc772017-11-02 15:27:50 +0000130 afs_purge_servers(net);
David Howellsf044c882017-11-02 15:27:45 +0000131error_cell_init:
David Howells989782d2017-11-02 15:27:50 +0000132 net->live = false;
David Howellsf044c882017-11-02 15:27:45 +0000133 afs_proc_cleanup(net);
134error_proc:
David Howells6f8880d2018-04-09 21:12:31 +0100135 afs_put_sysnames(net->sysnames);
136error_sysnames:
David Howells989782d2017-11-02 15:27:50 +0000137 net->live = false;
David Howellsf044c882017-11-02 15:27:45 +0000138 return ret;
139}
140
141/*
142 * Clean up and destroy an AFS network namespace record.
143 */
David Howells5b86d4f2018-05-18 11:46:15 +0100144static void __net_exit afs_net_exit(struct net *net_ns)
David Howellsf044c882017-11-02 15:27:45 +0000145{
David Howells5b86d4f2018-05-18 11:46:15 +0100146 struct afs_net *net = afs_net(net_ns);
147
David Howellsf044c882017-11-02 15:27:45 +0000148 net->live = false;
David Howellsf044c882017-11-02 15:27:45 +0000149 afs_cell_purge(net);
David Howellsd2ddc772017-11-02 15:27:50 +0000150 afs_purge_servers(net);
David Howellse3b2ffe2017-11-02 15:27:45 +0000151 afs_close_socket(net);
David Howellsf044c882017-11-02 15:27:45 +0000152 afs_proc_cleanup(net);
David Howells6f8880d2018-04-09 21:12:31 +0100153 afs_put_sysnames(net->sysnames);
David Howellsf044c882017-11-02 15:27:45 +0000154}
David Howellsb908fe62007-04-26 15:58:17 -0700155
David Howells5b86d4f2018-05-18 11:46:15 +0100156static struct pernet_operations afs_net_ops = {
157 .init = afs_net_init,
158 .exit = afs_net_exit,
159 .id = &afs_net_id,
160 .size = sizeof(struct afs_net),
161};
162
David Howellsb908fe62007-04-26 15:58:17 -0700163/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 * initialise the AFS client FS module
165 */
166static int __init afs_init(void)
167{
David Howellsf044c882017-11-02 15:27:45 +0000168 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 printk(KERN_INFO "kAFS: Red Hat AFS client v0.1 registering.\n");
171
Tejun Heo0ad53ee2011-01-14 15:56:37 +0000172 afs_wq = alloc_workqueue("afs", 0, 0);
173 if (!afs_wq)
David Howellsf044c882017-11-02 15:27:45 +0000174 goto error_afs_wq;
175 afs_async_calls = alloc_workqueue("kafsd", WQ_MEM_RECLAIM, 0);
176 if (!afs_async_calls)
177 goto error_async;
David Howellsf044c882017-11-02 15:27:45 +0000178 afs_lock_manager = alloc_workqueue("kafs_lockd", WQ_MEM_RECLAIM, 0);
179 if (!afs_lock_manager)
180 goto error_lockmgr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
David Howells9b3f26c2009-04-03 16:42:41 +0100182#ifdef CONFIG_AFS_FSCACHE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 /* we want to be able to cache */
David Howells9b3f26c2009-04-03 16:42:41 +0100184 ret = fscache_register_netfs(&afs_cache_netfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 goto error_cache;
187#endif
188
David Howells5b86d4f2018-05-18 11:46:15 +0100189 ret = register_pernet_subsys(&afs_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if (ret < 0)
David Howellsf044c882017-11-02 15:27:45 +0000191 goto error_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 /* register the filesystems */
194 ret = afs_fs_init();
195 if (ret < 0)
David Howellsec268152007-04-26 15:49:28 -0700196 goto error_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
David Howells5b86d4f2018-05-18 11:46:15 +0100198 afs_proc_symlink = proc_symlink("fs/afs", NULL, "../self/net/afs");
199 if (IS_ERR(afs_proc_symlink)) {
200 ret = PTR_ERR(afs_proc_symlink);
201 goto error_proc;
202 }
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return ret;
205
David Howells5b86d4f2018-05-18 11:46:15 +0100206error_proc:
207 afs_fs_exit();
David Howellsec268152007-04-26 15:49:28 -0700208error_fs:
David Howells5b86d4f2018-05-18 11:46:15 +0100209 unregister_pernet_subsys(&afs_net_ops);
David Howellsf044c882017-11-02 15:27:45 +0000210error_net:
David Howells9b3f26c2009-04-03 16:42:41 +0100211#ifdef CONFIG_AFS_FSCACHE
212 fscache_unregister_netfs(&afs_cache_netfs);
David Howellsec268152007-04-26 15:49:28 -0700213error_cache:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214#endif
David Howellsf044c882017-11-02 15:27:45 +0000215 destroy_workqueue(afs_lock_manager);
216error_lockmgr:
David Howellsf044c882017-11-02 15:27:45 +0000217 destroy_workqueue(afs_async_calls);
218error_async:
Tejun Heo0ad53ee2011-01-14 15:56:37 +0000219 destroy_workqueue(afs_wq);
David Howellsf044c882017-11-02 15:27:45 +0000220error_afs_wq:
David Howells416351f2007-05-09 02:33:45 -0700221 rcu_barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 printk(KERN_ERR "kAFS: failed to register: %d\n", ret);
223 return ret;
David Howellsec268152007-04-26 15:49:28 -0700224}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226/* XXX late_initcall is kludgy, but the only alternative seems to create
227 * a transport upon the first mount, which is worse. Or is it?
228 */
229late_initcall(afs_init); /* must be called after net/ to create socket */
David Howellsec268152007-04-26 15:49:28 -0700230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231/*
232 * clean up on module removal
233 */
234static void __exit afs_exit(void)
235{
236 printk(KERN_INFO "kAFS: Red Hat AFS client v0.1 unregistering.\n");
237
David Howells5b86d4f2018-05-18 11:46:15 +0100238 proc_remove(afs_proc_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 afs_fs_exit();
David Howells5b86d4f2018-05-18 11:46:15 +0100240 unregister_pernet_subsys(&afs_net_ops);
David Howells9b3f26c2009-04-03 16:42:41 +0100241#ifdef CONFIG_AFS_FSCACHE
242 fscache_unregister_netfs(&afs_cache_netfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243#endif
David Howellsf044c882017-11-02 15:27:45 +0000244 destroy_workqueue(afs_lock_manager);
David Howellsf044c882017-11-02 15:27:45 +0000245 destroy_workqueue(afs_async_calls);
246 destroy_workqueue(afs_wq);
David Howellsbe080a62017-11-02 15:27:49 +0000247 afs_clean_up_permit_cache();
David Howells416351f2007-05-09 02:33:45 -0700248 rcu_barrier();
David Howellsec268152007-04-26 15:49:28 -0700249}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251module_exit(afs_exit);