Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 2 | /* AFS client file system |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 4 | * Copyright (C) 2002,5 Red Hat, Inc. All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Written by David Howells (dhowells@redhat.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/moduleparam.h> |
| 10 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/completion.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 12 | #include <linux/sched.h> |
David Howells | e0661df | 2016-08-30 16:05:14 +0100 | [diff] [blame] | 13 | #include <linux/random.h> |
David Howells | 5b86d4f | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 14 | #include <linux/proc_fs.h> |
David Howells | 8e8d7f1 | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 15 | #define CREATE_TRACE_POINTS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include "internal.h" |
| 17 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | MODULE_DESCRIPTION("AFS Client File System"); |
| 19 | MODULE_AUTHOR("Red Hat, Inc."); |
| 20 | MODULE_LICENSE("GPL"); |
| 21 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 22 | unsigned afs_debug; |
| 23 | module_param_named(debug, afs_debug, uint, S_IWUSR | S_IRUGO); |
Paul Bolle | 424b00e | 2008-04-16 11:08:22 +0100 | [diff] [blame] | 24 | MODULE_PARM_DESC(debug, "AFS debugging mask"); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Tejun Heo | 0ad53ee | 2011-01-14 15:56:37 +0000 | [diff] [blame] | 31 | struct workqueue_struct *afs_wq; |
David Howells | 5b86d4f | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 32 | static struct proc_dir_entry *afs_proc_symlink; |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 33 | |
David Howells | 6f8880d | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 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 | |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 64 | /* |
| 65 | * Initialise an AFS network namespace record. |
| 66 | */ |
David Howells | 5b86d4f | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 67 | static int __net_init afs_net_init(struct net *net_ns) |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 68 | { |
David Howells | 6f8880d | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 69 | struct afs_sysnames *sysnames; |
David Howells | 5b86d4f | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 70 | struct afs_net *net = afs_net(net_ns); |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 71 | int ret; |
| 72 | |
David Howells | 5b86d4f | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 73 | net->net = net_ns; |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 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); |
David Howells | 989782d | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 79 | |
| 80 | net->cells = RB_ROOT; |
David Howells | 92e3cc9 | 2020-10-09 14:11:58 +0100 | [diff] [blame] | 81 | init_rwsem(&net->cells_lock); |
David Howells | 989782d | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 82 | INIT_WORK(&net->cells_manager, afs_manage_cells); |
| 83 | timer_setup(&net->cells_timer, afs_cells_timer, 0); |
| 84 | |
David Howells | 8a070a9 | 2020-04-25 10:26:02 +0100 | [diff] [blame] | 85 | mutex_init(&net->cells_alias_lock); |
David Howells | 0da0b7f | 2018-06-15 15:19:22 +0100 | [diff] [blame] | 86 | mutex_init(&net->proc_cells_lock); |
David Howells | 6b3944e | 2018-10-11 22:45:49 +0100 | [diff] [blame] | 87 | INIT_HLIST_HEAD(&net->proc_cells); |
David Howells | 989782d | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 88 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 89 | seqlock_init(&net->fs_lock); |
| 90 | net->fs_servers = RB_ROOT; |
David Howells | f6cbb36 | 2020-04-24 15:10:00 +0100 | [diff] [blame] | 91 | INIT_LIST_HEAD(&net->fs_probe_fast); |
| 92 | INIT_LIST_HEAD(&net->fs_probe_slow); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 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); |
David Howells | f6cbb36 | 2020-04-24 15:10:00 +0100 | [diff] [blame] | 101 | INIT_WORK(&net->fs_prober, afs_fs_probe_dispatcher); |
| 102 | timer_setup(&net->fs_probe_timer, afs_fs_probe_timer, 0); |
David Howells | 5481fc6 | 2020-06-19 23:39:36 +0100 | [diff] [blame] | 103 | atomic_set(&net->servers_outstanding, 1); |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 104 | |
David Howells | 6f8880d | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 105 | ret = -ENOMEM; |
| 106 | sysnames = kzalloc(sizeof(*sysnames), GFP_KERNEL); |
| 107 | if (!sysnames) |
| 108 | goto error_sysnames; |
| 109 | sysnames->subs[0] = (char *)&afs_init_sysname; |
| 110 | sysnames->nr = 1; |
| 111 | refcount_set(&sysnames->usage, 1); |
| 112 | net->sysnames = sysnames; |
| 113 | rwlock_init(&net->sysnames_lock); |
| 114 | |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 115 | /* Register the /proc stuff */ |
| 116 | ret = afs_proc_init(net); |
| 117 | if (ret < 0) |
| 118 | goto error_proc; |
| 119 | |
| 120 | /* Initialise the cell DB */ |
| 121 | ret = afs_cell_init(net, rootcell); |
| 122 | if (ret < 0) |
| 123 | goto error_cell_init; |
| 124 | |
| 125 | /* Create the RxRPC transport */ |
| 126 | ret = afs_open_socket(net); |
| 127 | if (ret < 0) |
| 128 | goto error_open_socket; |
| 129 | |
| 130 | return 0; |
| 131 | |
| 132 | error_open_socket: |
David Howells | 989782d | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 133 | net->live = false; |
David Howells | 5481fc6 | 2020-06-19 23:39:36 +0100 | [diff] [blame] | 134 | afs_fs_probe_cleanup(net); |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 135 | afs_cell_purge(net); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 136 | afs_purge_servers(net); |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 137 | error_cell_init: |
David Howells | 989782d | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 138 | net->live = false; |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 139 | afs_proc_cleanup(net); |
| 140 | error_proc: |
David Howells | 6f8880d | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 141 | afs_put_sysnames(net->sysnames); |
| 142 | error_sysnames: |
David Howells | 989782d | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 143 | net->live = false; |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 144 | return ret; |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * Clean up and destroy an AFS network namespace record. |
| 149 | */ |
David Howells | 5b86d4f | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 150 | static void __net_exit afs_net_exit(struct net *net_ns) |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 151 | { |
David Howells | 5b86d4f | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 152 | struct afs_net *net = afs_net(net_ns); |
| 153 | |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 154 | net->live = false; |
David Howells | 5481fc6 | 2020-06-19 23:39:36 +0100 | [diff] [blame] | 155 | afs_fs_probe_cleanup(net); |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 156 | afs_cell_purge(net); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 157 | afs_purge_servers(net); |
David Howells | e3b2ffe | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 158 | afs_close_socket(net); |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 159 | afs_proc_cleanup(net); |
David Howells | 6f8880d | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 160 | afs_put_sysnames(net->sysnames); |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 161 | } |
David Howells | b908fe6 | 2007-04-26 15:58:17 -0700 | [diff] [blame] | 162 | |
David Howells | 5b86d4f | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 163 | static struct pernet_operations afs_net_ops = { |
| 164 | .init = afs_net_init, |
| 165 | .exit = afs_net_exit, |
| 166 | .id = &afs_net_id, |
| 167 | .size = sizeof(struct afs_net), |
| 168 | }; |
| 169 | |
David Howells | b908fe6 | 2007-04-26 15:58:17 -0700 | [diff] [blame] | 170 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | * initialise the AFS client FS module |
| 172 | */ |
| 173 | static int __init afs_init(void) |
| 174 | { |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 175 | int ret = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
| 177 | printk(KERN_INFO "kAFS: Red Hat AFS client v0.1 registering.\n"); |
| 178 | |
Tejun Heo | 0ad53ee | 2011-01-14 15:56:37 +0000 | [diff] [blame] | 179 | afs_wq = alloc_workqueue("afs", 0, 0); |
| 180 | if (!afs_wq) |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 181 | goto error_afs_wq; |
| 182 | afs_async_calls = alloc_workqueue("kafsd", WQ_MEM_RECLAIM, 0); |
| 183 | if (!afs_async_calls) |
| 184 | goto error_async; |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 185 | afs_lock_manager = alloc_workqueue("kafs_lockd", WQ_MEM_RECLAIM, 0); |
| 186 | if (!afs_lock_manager) |
| 187 | goto error_lockmgr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
David Howells | 5399d52 | 2021-01-29 23:53:50 +0000 | [diff] [blame] | 189 | ret = register_pernet_device(&afs_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | if (ret < 0) |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 191 | goto error_net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | |
| 193 | /* register the filesystems */ |
| 194 | ret = afs_fs_init(); |
| 195 | if (ret < 0) |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 196 | goto error_fs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
David Howells | 5b86d4f | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 198 | afs_proc_symlink = proc_symlink("fs/afs", NULL, "../self/net/afs"); |
Dan Carpenter | a33d626 | 2021-06-15 08:39:52 +0100 | [diff] [blame] | 199 | if (!afs_proc_symlink) { |
| 200 | ret = -ENOMEM; |
David Howells | 5b86d4f | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 201 | goto error_proc; |
| 202 | } |
| 203 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | return ret; |
| 205 | |
David Howells | 5b86d4f | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 206 | error_proc: |
| 207 | afs_fs_exit(); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 208 | error_fs: |
David Howells | 5399d52 | 2021-01-29 23:53:50 +0000 | [diff] [blame] | 209 | unregister_pernet_device(&afs_net_ops); |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 210 | error_net: |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 211 | destroy_workqueue(afs_lock_manager); |
| 212 | error_lockmgr: |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 213 | destroy_workqueue(afs_async_calls); |
| 214 | error_async: |
Tejun Heo | 0ad53ee | 2011-01-14 15:56:37 +0000 | [diff] [blame] | 215 | destroy_workqueue(afs_wq); |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 216 | error_afs_wq: |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 217 | rcu_barrier(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | printk(KERN_ERR "kAFS: failed to register: %d\n", ret); |
| 219 | return ret; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 220 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | |
| 222 | /* XXX late_initcall is kludgy, but the only alternative seems to create |
| 223 | * a transport upon the first mount, which is worse. Or is it? |
| 224 | */ |
| 225 | late_initcall(afs_init); /* must be called after net/ to create socket */ |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 226 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | /* |
| 228 | * clean up on module removal |
| 229 | */ |
| 230 | static void __exit afs_exit(void) |
| 231 | { |
| 232 | printk(KERN_INFO "kAFS: Red Hat AFS client v0.1 unregistering.\n"); |
| 233 | |
David Howells | 5b86d4f | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 234 | proc_remove(afs_proc_symlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | afs_fs_exit(); |
David Howells | 5399d52 | 2021-01-29 23:53:50 +0000 | [diff] [blame] | 236 | unregister_pernet_device(&afs_net_ops); |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 237 | destroy_workqueue(afs_lock_manager); |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 238 | destroy_workqueue(afs_async_calls); |
| 239 | destroy_workqueue(afs_wq); |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 240 | afs_clean_up_permit_cache(); |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 241 | rcu_barrier(); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 242 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
| 244 | module_exit(afs_exit); |