David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 1 | /* AFS client file system |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 3 | * Copyright (C) 2002,5 Red Hat, Inc. All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * Written by David Howells (dhowells@redhat.com) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/moduleparam.h> |
| 14 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/completion.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 16 | #include <linux/sched.h> |
David Howells | e0661df | 2016-08-30 16:05:14 +0100 | [diff] [blame] | 17 | #include <linux/random.h> |
David Howells | 5b86d4f | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 18 | #include <linux/proc_fs.h> |
David Howells | 8e8d7f1 | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 19 | #define CREATE_TRACE_POINTS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "internal.h" |
| 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | MODULE_DESCRIPTION("AFS Client File System"); |
| 23 | MODULE_AUTHOR("Red Hat, Inc."); |
| 24 | MODULE_LICENSE("GPL"); |
| 25 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 26 | unsigned afs_debug; |
| 27 | module_param_named(debug, afs_debug, uint, S_IWUSR | S_IRUGO); |
Paul Bolle | 424b00e | 2008-04-16 11:08:22 +0100 | [diff] [blame] | 28 | MODULE_PARM_DESC(debug, "AFS debugging mask"); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | static char *rootcell; |
| 31 | |
| 32 | module_param(rootcell, charp, 0); |
| 33 | MODULE_PARM_DESC(rootcell, "root AFS cell name and VL server IP addr list"); |
| 34 | |
Tejun Heo | 0ad53ee | 2011-01-14 15:56:37 +0000 | [diff] [blame] | 35 | struct workqueue_struct *afs_wq; |
David Howells | 5b86d4f | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 36 | static struct proc_dir_entry *afs_proc_symlink; |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 37 | |
David Howells | 6f8880d | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 38 | #if defined(CONFIG_ALPHA) |
| 39 | const char afs_init_sysname[] = "alpha_linux26"; |
| 40 | #elif defined(CONFIG_X86_64) |
| 41 | const char afs_init_sysname[] = "amd64_linux26"; |
| 42 | #elif defined(CONFIG_ARM) |
| 43 | const char afs_init_sysname[] = "arm_linux26"; |
| 44 | #elif defined(CONFIG_ARM64) |
| 45 | const char afs_init_sysname[] = "aarch64_linux26"; |
| 46 | #elif defined(CONFIG_X86_32) |
| 47 | const char afs_init_sysname[] = "i386_linux26"; |
| 48 | #elif defined(CONFIG_IA64) |
| 49 | const char afs_init_sysname[] = "ia64_linux26"; |
| 50 | #elif defined(CONFIG_PPC64) |
| 51 | const char afs_init_sysname[] = "ppc64_linux26"; |
| 52 | #elif defined(CONFIG_PPC32) |
| 53 | const char afs_init_sysname[] = "ppc_linux26"; |
| 54 | #elif defined(CONFIG_S390) |
| 55 | #ifdef CONFIG_64BIT |
| 56 | const char afs_init_sysname[] = "s390x_linux26"; |
| 57 | #else |
| 58 | const char afs_init_sysname[] = "s390_linux26"; |
| 59 | #endif |
| 60 | #elif defined(CONFIG_SPARC64) |
| 61 | const char afs_init_sysname[] = "sparc64_linux26"; |
| 62 | #elif defined(CONFIG_SPARC32) |
| 63 | const char afs_init_sysname[] = "sparc_linux26"; |
| 64 | #else |
| 65 | const char afs_init_sysname[] = "unknown_linux26"; |
| 66 | #endif |
| 67 | |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 68 | /* |
| 69 | * Initialise an AFS network namespace record. |
| 70 | */ |
David Howells | 5b86d4f | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 71 | static int __net_init afs_net_init(struct net *net_ns) |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 72 | { |
David Howells | 6f8880d | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 73 | struct afs_sysnames *sysnames; |
David Howells | 5b86d4f | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 74 | struct afs_net *net = afs_net(net_ns); |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 75 | int ret; |
| 76 | |
David Howells | 5b86d4f | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 77 | net->net = net_ns; |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 78 | net->live = true; |
| 79 | generate_random_uuid((unsigned char *)&net->uuid); |
| 80 | |
| 81 | INIT_WORK(&net->charge_preallocation_work, afs_charge_preallocation); |
| 82 | mutex_init(&net->socket_mutex); |
David Howells | 989782d | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 83 | |
| 84 | net->cells = RB_ROOT; |
| 85 | seqlock_init(&net->cells_lock); |
| 86 | INIT_WORK(&net->cells_manager, afs_manage_cells); |
| 87 | timer_setup(&net->cells_timer, afs_cells_timer, 0); |
| 88 | |
David Howells | 0da0b7f | 2018-06-15 15:19:22 +0100 | [diff] [blame] | 89 | mutex_init(&net->proc_cells_lock); |
David Howells | 6b3944e | 2018-10-11 22:45:49 +0100 | [diff] [blame^] | 90 | INIT_HLIST_HEAD(&net->proc_cells); |
David Howells | 989782d | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 91 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 92 | seqlock_init(&net->fs_lock); |
| 93 | net->fs_servers = RB_ROOT; |
| 94 | INIT_LIST_HEAD(&net->fs_updates); |
| 95 | INIT_HLIST_HEAD(&net->fs_proc); |
| 96 | |
| 97 | INIT_HLIST_HEAD(&net->fs_addresses4); |
| 98 | INIT_HLIST_HEAD(&net->fs_addresses6); |
| 99 | seqlock_init(&net->fs_addr_lock); |
| 100 | |
| 101 | INIT_WORK(&net->fs_manager, afs_manage_servers); |
| 102 | timer_setup(&net->fs_timer, afs_servers_timer, 0); |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 103 | |
David Howells | 6f8880d | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 104 | ret = -ENOMEM; |
| 105 | sysnames = kzalloc(sizeof(*sysnames), GFP_KERNEL); |
| 106 | if (!sysnames) |
| 107 | goto error_sysnames; |
| 108 | sysnames->subs[0] = (char *)&afs_init_sysname; |
| 109 | sysnames->nr = 1; |
| 110 | refcount_set(&sysnames->usage, 1); |
| 111 | net->sysnames = sysnames; |
| 112 | rwlock_init(&net->sysnames_lock); |
| 113 | |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 114 | /* Register the /proc stuff */ |
| 115 | ret = afs_proc_init(net); |
| 116 | if (ret < 0) |
| 117 | goto error_proc; |
| 118 | |
| 119 | /* Initialise the cell DB */ |
| 120 | ret = afs_cell_init(net, rootcell); |
| 121 | if (ret < 0) |
| 122 | goto error_cell_init; |
| 123 | |
| 124 | /* Create the RxRPC transport */ |
| 125 | ret = afs_open_socket(net); |
| 126 | if (ret < 0) |
| 127 | goto error_open_socket; |
| 128 | |
| 129 | return 0; |
| 130 | |
| 131 | error_open_socket: |
David Howells | 989782d | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 132 | net->live = false; |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 133 | afs_cell_purge(net); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 134 | afs_purge_servers(net); |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 135 | error_cell_init: |
David Howells | 989782d | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 136 | net->live = false; |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 137 | afs_proc_cleanup(net); |
| 138 | error_proc: |
David Howells | 6f8880d | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 139 | afs_put_sysnames(net->sysnames); |
| 140 | error_sysnames: |
David Howells | 989782d | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 141 | net->live = false; |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 142 | return ret; |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * Clean up and destroy an AFS network namespace record. |
| 147 | */ |
David Howells | 5b86d4f | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 148 | static void __net_exit afs_net_exit(struct net *net_ns) |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 149 | { |
David Howells | 5b86d4f | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 150 | struct afs_net *net = afs_net(net_ns); |
| 151 | |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 152 | net->live = false; |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 153 | afs_cell_purge(net); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 154 | afs_purge_servers(net); |
David Howells | e3b2ffe | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 155 | afs_close_socket(net); |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 156 | afs_proc_cleanup(net); |
David Howells | 6f8880d | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 157 | afs_put_sysnames(net->sysnames); |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 158 | } |
David Howells | b908fe6 | 2007-04-26 15:58:17 -0700 | [diff] [blame] | 159 | |
David Howells | 5b86d4f | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 160 | static struct pernet_operations afs_net_ops = { |
| 161 | .init = afs_net_init, |
| 162 | .exit = afs_net_exit, |
| 163 | .id = &afs_net_id, |
| 164 | .size = sizeof(struct afs_net), |
| 165 | }; |
| 166 | |
David Howells | b908fe6 | 2007-04-26 15:58:17 -0700 | [diff] [blame] | 167 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | * initialise the AFS client FS module |
| 169 | */ |
| 170 | static int __init afs_init(void) |
| 171 | { |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 172 | int ret = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
| 174 | printk(KERN_INFO "kAFS: Red Hat AFS client v0.1 registering.\n"); |
| 175 | |
Tejun Heo | 0ad53ee | 2011-01-14 15:56:37 +0000 | [diff] [blame] | 176 | afs_wq = alloc_workqueue("afs", 0, 0); |
| 177 | if (!afs_wq) |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 178 | goto error_afs_wq; |
| 179 | afs_async_calls = alloc_workqueue("kafsd", WQ_MEM_RECLAIM, 0); |
| 180 | if (!afs_async_calls) |
| 181 | goto error_async; |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 182 | afs_lock_manager = alloc_workqueue("kafs_lockd", WQ_MEM_RECLAIM, 0); |
| 183 | if (!afs_lock_manager) |
| 184 | goto error_lockmgr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 186 | #ifdef CONFIG_AFS_FSCACHE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | /* we want to be able to cache */ |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 188 | ret = fscache_register_netfs(&afs_cache_netfs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | goto error_cache; |
| 191 | #endif |
| 192 | |
David Howells | 5b86d4f | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 193 | ret = register_pernet_subsys(&afs_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | if (ret < 0) |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 195 | goto error_net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
| 197 | /* register the filesystems */ |
| 198 | ret = afs_fs_init(); |
| 199 | if (ret < 0) |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 200 | goto error_fs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
David Howells | 5b86d4f | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 202 | afs_proc_symlink = proc_symlink("fs/afs", NULL, "../self/net/afs"); |
| 203 | if (IS_ERR(afs_proc_symlink)) { |
| 204 | ret = PTR_ERR(afs_proc_symlink); |
| 205 | goto error_proc; |
| 206 | } |
| 207 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | return ret; |
| 209 | |
David Howells | 5b86d4f | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 210 | error_proc: |
| 211 | afs_fs_exit(); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 212 | error_fs: |
David Howells | 5b86d4f | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 213 | unregister_pernet_subsys(&afs_net_ops); |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 214 | error_net: |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 215 | #ifdef CONFIG_AFS_FSCACHE |
| 216 | fscache_unregister_netfs(&afs_cache_netfs); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 217 | error_cache: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | #endif |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 219 | destroy_workqueue(afs_lock_manager); |
| 220 | error_lockmgr: |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 221 | destroy_workqueue(afs_async_calls); |
| 222 | error_async: |
Tejun Heo | 0ad53ee | 2011-01-14 15:56:37 +0000 | [diff] [blame] | 223 | destroy_workqueue(afs_wq); |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 224 | error_afs_wq: |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 225 | rcu_barrier(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | printk(KERN_ERR "kAFS: failed to register: %d\n", ret); |
| 227 | return ret; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 228 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
| 230 | /* XXX late_initcall is kludgy, but the only alternative seems to create |
| 231 | * a transport upon the first mount, which is worse. Or is it? |
| 232 | */ |
| 233 | late_initcall(afs_init); /* must be called after net/ to create socket */ |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 234 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | /* |
| 236 | * clean up on module removal |
| 237 | */ |
| 238 | static void __exit afs_exit(void) |
| 239 | { |
| 240 | printk(KERN_INFO "kAFS: Red Hat AFS client v0.1 unregistering.\n"); |
| 241 | |
David Howells | 5b86d4f | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 242 | proc_remove(afs_proc_symlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | afs_fs_exit(); |
David Howells | 5b86d4f | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 244 | unregister_pernet_subsys(&afs_net_ops); |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 245 | #ifdef CONFIG_AFS_FSCACHE |
| 246 | fscache_unregister_netfs(&afs_cache_netfs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | #endif |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 248 | destroy_workqueue(afs_lock_manager); |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 249 | destroy_workqueue(afs_async_calls); |
| 250 | destroy_workqueue(afs_wq); |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 251 | afs_clean_up_permit_cache(); |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 252 | rcu_barrier(); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 253 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
| 255 | module_exit(afs_exit); |