Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Central processing for nfsd. |
| 4 | * |
| 5 | * Authors: Olaf Kirch (okir@monad.swb.de) |
| 6 | * |
| 7 | * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de> |
| 8 | */ |
| 9 | |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 10 | #include <linux/sched/signal.h> |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 11 | #include <linux/freezer.h> |
Paul Gortmaker | 143cb49 | 2011-07-01 14:23:34 -0400 | [diff] [blame] | 12 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/fs_struct.h> |
Andy Adamson | c3d06f9 | 2009-04-03 08:28:18 +0300 | [diff] [blame] | 14 | #include <linux/swap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/sunrpc/stats.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/sunrpc/svcsock.h> |
Scott Mayhew | 3668499 | 2015-12-11 16:45:59 -0500 | [diff] [blame] | 18 | #include <linux/sunrpc/svc_xprt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/lockd/bind.h> |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 20 | #include <linux/nfsacl.h> |
Ryusei Yamaguchi | ed2d8ae | 2009-08-16 00:54:41 +0900 | [diff] [blame] | 21 | #include <linux/seq_file.h> |
Scott Mayhew | 3668499 | 2015-12-11 16:45:59 -0500 | [diff] [blame] | 22 | #include <linux/inetdevice.h> |
| 23 | #include <net/addrconf.h> |
| 24 | #include <net/ipv6.h> |
Pavel Emelyanov | fc5d00b | 2010-09-29 16:03:50 +0400 | [diff] [blame] | 25 | #include <net/net_namespace.h> |
Boaz Harrosh | 9a74af2 | 2009-12-03 20:30:56 +0200 | [diff] [blame] | 26 | #include "nfsd.h" |
| 27 | #include "cache.h" |
J. Bruce Fields | 0a3adad | 2009-11-04 18:12:35 -0500 | [diff] [blame] | 28 | #include "vfs.h" |
Stanislav Kinsbursky | 2c2fe29 | 2012-12-06 14:23:14 +0300 | [diff] [blame] | 29 | #include "netns.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
| 31 | #define NFSDDBG_FACILITY NFSDDBG_SVC |
| 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | extern struct svc_program nfsd_program; |
Jeff Layton | 9867d76 | 2008-06-10 08:40:38 -0400 | [diff] [blame] | 34 | static int nfsd(void *vrqstp); |
Trond Myklebust | 029be5d | 2019-04-09 11:46:18 -0400 | [diff] [blame] | 35 | #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) |
| 36 | static int nfsd_acl_rpcbind_set(struct net *, |
| 37 | const struct svc_program *, |
| 38 | u32, int, |
| 39 | unsigned short, |
| 40 | unsigned short); |
Trond Myklebust | e333f3b | 2019-04-09 11:46:19 -0400 | [diff] [blame] | 41 | static __be32 nfsd_acl_init_request(struct svc_rqst *, |
| 42 | const struct svc_program *, |
| 43 | struct svc_process_info *); |
Trond Myklebust | 029be5d | 2019-04-09 11:46:18 -0400 | [diff] [blame] | 44 | #endif |
| 45 | static int nfsd_rpcbind_set(struct net *, |
| 46 | const struct svc_program *, |
| 47 | u32, int, |
| 48 | unsigned short, |
| 49 | unsigned short); |
Trond Myklebust | e333f3b | 2019-04-09 11:46:19 -0400 | [diff] [blame] | 50 | static __be32 nfsd_init_request(struct svc_rqst *, |
| 51 | const struct svc_program *, |
| 52 | struct svc_process_info *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Neil Brown | bedbdd8 | 2008-06-10 08:40:35 -0400 | [diff] [blame] | 54 | /* |
Stanislav Kinsbursky | 9dd9845 | 2012-12-06 14:23:24 +0300 | [diff] [blame] | 55 | * nfsd_mutex protects nn->nfsd_serv -- both the pointer itself and the members |
Neil Brown | bedbdd8 | 2008-06-10 08:40:35 -0400 | [diff] [blame] | 56 | * of the svc_serv struct. In particular, ->sv_nrthreads but also to some |
| 57 | * extent ->sv_temp_socks and ->sv_permsocks. It also protects nfsdstats.th_cnt |
| 58 | * |
Stanislav Kinsbursky | 9dd9845 | 2012-12-06 14:23:24 +0300 | [diff] [blame] | 59 | * If (out side the lock) nn->nfsd_serv is non-NULL, then it must point to a |
Neil Brown | bedbdd8 | 2008-06-10 08:40:35 -0400 | [diff] [blame] | 60 | * properly initialised 'struct svc_serv' with ->sv_nrthreads > 0. That number |
| 61 | * of nfsd threads must exist and each must listed in ->sp_all_threads in each |
| 62 | * entry of ->sv_pools[]. |
| 63 | * |
| 64 | * Transitions of the thread count between zero and non-zero are of particular |
| 65 | * interest since the svc_serv needs to be created and initialized at that |
| 66 | * point, or freed. |
Jeff Layton | 3dd98a3 | 2008-06-10 08:40:36 -0400 | [diff] [blame] | 67 | * |
| 68 | * Finally, the nfsd_mutex also protects some of the global variables that are |
| 69 | * accessed when nfsd starts and that are settable via the write_* routines in |
| 70 | * nfsctl.c. In particular: |
| 71 | * |
| 72 | * user_recovery_dirname |
| 73 | * user_lease_time |
| 74 | * nfsd_versions |
Neil Brown | bedbdd8 | 2008-06-10 08:40:35 -0400 | [diff] [blame] | 75 | */ |
| 76 | DEFINE_MUTEX(nfsd_mutex); |
Neil Brown | bedbdd8 | 2008-06-10 08:40:35 -0400 | [diff] [blame] | 77 | |
Andy Adamson | 4bd9b0f4 | 2009-06-24 15:37:45 -0400 | [diff] [blame] | 78 | /* |
| 79 | * nfsd_drc_lock protects nfsd_drc_max_pages and nfsd_drc_pages_used. |
| 80 | * nfsd_drc_max_pages limits the total amount of memory available for |
| 81 | * version 4.1 DRC caches. |
| 82 | * nfsd_drc_pages_used tracks the current version 4.1 DRC memory usage. |
| 83 | */ |
| 84 | spinlock_t nfsd_drc_lock; |
Zhang Yanfei | 697ce9b | 2013-02-22 16:35:47 -0800 | [diff] [blame] | 85 | unsigned long nfsd_drc_max_mem; |
| 86 | unsigned long nfsd_drc_mem_used; |
Andy Adamson | 4bd9b0f4 | 2009-06-24 15:37:45 -0400 | [diff] [blame] | 87 | |
Andreas Gruenbacher | 3fb803a | 2006-02-01 03:04:34 -0800 | [diff] [blame] | 88 | #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) |
| 89 | static struct svc_stat nfsd_acl_svcstats; |
Christoph Hellwig | e967918 | 2017-05-12 16:21:37 +0200 | [diff] [blame] | 90 | static const struct svc_version *nfsd_acl_version[] = { |
Andreas Gruenbacher | 3fb803a | 2006-02-01 03:04:34 -0800 | [diff] [blame] | 91 | [2] = &nfsd_acl_version2, |
| 92 | [3] = &nfsd_acl_version3, |
| 93 | }; |
| 94 | |
| 95 | #define NFSD_ACL_MINVERS 2 |
Tobias Klauser | e8c96f8 | 2006-03-24 03:15:34 -0800 | [diff] [blame] | 96 | #define NFSD_ACL_NRVERS ARRAY_SIZE(nfsd_acl_version) |
Christoph Hellwig | e967918 | 2017-05-12 16:21:37 +0200 | [diff] [blame] | 97 | static const struct svc_version *nfsd_acl_versions[NFSD_ACL_NRVERS]; |
Andreas Gruenbacher | 3fb803a | 2006-02-01 03:04:34 -0800 | [diff] [blame] | 98 | |
| 99 | static struct svc_program nfsd_acl_program = { |
| 100 | .pg_prog = NFS_ACL_PROGRAM, |
| 101 | .pg_nvers = NFSD_ACL_NRVERS, |
| 102 | .pg_vers = nfsd_acl_versions, |
NeilBrown | 1a8eff6 | 2007-01-26 00:56:58 -0800 | [diff] [blame] | 103 | .pg_name = "nfsacl", |
Andreas Gruenbacher | 3fb803a | 2006-02-01 03:04:34 -0800 | [diff] [blame] | 104 | .pg_class = "nfsd", |
| 105 | .pg_stats = &nfsd_acl_svcstats, |
| 106 | .pg_authenticate = &svc_set_client, |
Trond Myklebust | e333f3b | 2019-04-09 11:46:19 -0400 | [diff] [blame] | 107 | .pg_init_request = nfsd_acl_init_request, |
Trond Myklebust | 029be5d | 2019-04-09 11:46:18 -0400 | [diff] [blame] | 108 | .pg_rpcbind_set = nfsd_acl_rpcbind_set, |
Andreas Gruenbacher | 3fb803a | 2006-02-01 03:04:34 -0800 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | static struct svc_stat nfsd_acl_svcstats = { |
| 112 | .program = &nfsd_acl_program, |
| 113 | }; |
| 114 | #endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */ |
| 115 | |
Christoph Hellwig | e967918 | 2017-05-12 16:21:37 +0200 | [diff] [blame] | 116 | static const struct svc_version *nfsd_version[] = { |
NeilBrown | 70c3b76 | 2005-11-07 01:00:25 -0800 | [diff] [blame] | 117 | [2] = &nfsd_version2, |
| 118 | #if defined(CONFIG_NFSD_V3) |
| 119 | [3] = &nfsd_version3, |
| 120 | #endif |
| 121 | #if defined(CONFIG_NFSD_V4) |
| 122 | [4] = &nfsd_version4, |
| 123 | #endif |
| 124 | }; |
| 125 | |
| 126 | #define NFSD_MINVERS 2 |
Tobias Klauser | e8c96f8 | 2006-03-24 03:15:34 -0800 | [diff] [blame] | 127 | #define NFSD_NRVERS ARRAY_SIZE(nfsd_version) |
NeilBrown | 70c3b76 | 2005-11-07 01:00:25 -0800 | [diff] [blame] | 128 | |
| 129 | struct svc_program nfsd_program = { |
Andreas Gruenbacher | 3fb803a | 2006-02-01 03:04:34 -0800 | [diff] [blame] | 130 | #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) |
| 131 | .pg_next = &nfsd_acl_program, |
| 132 | #endif |
NeilBrown | 70c3b76 | 2005-11-07 01:00:25 -0800 | [diff] [blame] | 133 | .pg_prog = NFS_PROGRAM, /* program number */ |
| 134 | .pg_nvers = NFSD_NRVERS, /* nr of entries in nfsd_version */ |
Trond Myklebust | e333f3b | 2019-04-09 11:46:19 -0400 | [diff] [blame] | 135 | .pg_vers = nfsd_version, /* version table */ |
NeilBrown | 70c3b76 | 2005-11-07 01:00:25 -0800 | [diff] [blame] | 136 | .pg_name = "nfsd", /* program name */ |
| 137 | .pg_class = "nfsd", /* authentication class */ |
| 138 | .pg_stats = &nfsd_svcstats, /* version table */ |
| 139 | .pg_authenticate = &svc_set_client, /* export authentication */ |
Trond Myklebust | e333f3b | 2019-04-09 11:46:19 -0400 | [diff] [blame] | 140 | .pg_init_request = nfsd_init_request, |
Trond Myklebust | 029be5d | 2019-04-09 11:46:18 -0400 | [diff] [blame] | 141 | .pg_rpcbind_set = nfsd_rpcbind_set, |
NeilBrown | 70c3b76 | 2005-11-07 01:00:25 -0800 | [diff] [blame] | 142 | }; |
| 143 | |
Trond Myklebust | e333f3b | 2019-04-09 11:46:19 -0400 | [diff] [blame] | 144 | static bool |
| 145 | nfsd_support_version(int vers) |
| 146 | { |
| 147 | if (vers >= NFSD_MINVERS && vers < NFSD_NRVERS) |
| 148 | return nfsd_version[vers] != NULL; |
| 149 | return false; |
| 150 | } |
Benny Halevy | 8daf220 | 2009-04-03 08:28:59 +0300 | [diff] [blame] | 151 | |
Trond Myklebust | e333f3b | 2019-04-09 11:46:19 -0400 | [diff] [blame] | 152 | static bool * |
| 153 | nfsd_alloc_versions(void) |
| 154 | { |
| 155 | bool *vers = kmalloc_array(NFSD_NRVERS, sizeof(bool), GFP_KERNEL); |
| 156 | unsigned i; |
| 157 | |
| 158 | if (vers) { |
| 159 | /* All compiled versions are enabled by default */ |
| 160 | for (i = 0; i < NFSD_NRVERS; i++) |
| 161 | vers[i] = nfsd_support_version(i); |
| 162 | } |
| 163 | return vers; |
| 164 | } |
| 165 | |
| 166 | static bool * |
| 167 | nfsd_alloc_minorversions(void) |
| 168 | { |
| 169 | bool *vers = kmalloc_array(NFSD_SUPPORTED_MINOR_VERSION + 1, |
| 170 | sizeof(bool), GFP_KERNEL); |
| 171 | unsigned i; |
| 172 | |
| 173 | if (vers) { |
| 174 | /* All minor versions are enabled by default */ |
| 175 | for (i = 0; i <= NFSD_SUPPORTED_MINOR_VERSION; i++) |
| 176 | vers[i] = nfsd_support_version(4); |
| 177 | } |
| 178 | return vers; |
| 179 | } |
| 180 | |
| 181 | void |
| 182 | nfsd_netns_free_versions(struct nfsd_net *nn) |
| 183 | { |
| 184 | kfree(nn->nfsd_versions); |
| 185 | kfree(nn->nfsd4_minorversions); |
| 186 | nn->nfsd_versions = NULL; |
| 187 | nn->nfsd4_minorversions = NULL; |
| 188 | } |
| 189 | |
| 190 | static void |
| 191 | nfsd_netns_init_versions(struct nfsd_net *nn) |
| 192 | { |
| 193 | if (!nn->nfsd_versions) { |
| 194 | nn->nfsd_versions = nfsd_alloc_versions(); |
| 195 | nn->nfsd4_minorversions = nfsd_alloc_minorversions(); |
| 196 | if (!nn->nfsd_versions || !nn->nfsd4_minorversions) |
| 197 | nfsd_netns_free_versions(nn); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | int nfsd_vers(struct nfsd_net *nn, int vers, enum vers_op change) |
NeilBrown | 6658d3a | 2006-10-02 02:17:46 -0700 | [diff] [blame] | 202 | { |
| 203 | if (vers < NFSD_MINVERS || vers >= NFSD_NRVERS) |
Pavel Emelyanov | 15ddb4a | 2010-05-14 15:33:36 +0400 | [diff] [blame] | 204 | return 0; |
NeilBrown | 6658d3a | 2006-10-02 02:17:46 -0700 | [diff] [blame] | 205 | switch(change) { |
| 206 | case NFSD_SET: |
Trond Myklebust | e333f3b | 2019-04-09 11:46:19 -0400 | [diff] [blame] | 207 | if (nn->nfsd_versions) |
| 208 | nn->nfsd_versions[vers] = nfsd_support_version(vers); |
NeilBrown | 1a8eff6 | 2007-01-26 00:56:58 -0800 | [diff] [blame] | 209 | break; |
NeilBrown | 6658d3a | 2006-10-02 02:17:46 -0700 | [diff] [blame] | 210 | case NFSD_CLEAR: |
Trond Myklebust | e333f3b | 2019-04-09 11:46:19 -0400 | [diff] [blame] | 211 | nfsd_netns_init_versions(nn); |
| 212 | if (nn->nfsd_versions) |
| 213 | nn->nfsd_versions[vers] = false; |
NeilBrown | 6658d3a | 2006-10-02 02:17:46 -0700 | [diff] [blame] | 214 | break; |
| 215 | case NFSD_TEST: |
Trond Myklebust | e333f3b | 2019-04-09 11:46:19 -0400 | [diff] [blame] | 216 | if (nn->nfsd_versions) |
| 217 | return nn->nfsd_versions[vers]; |
| 218 | /* Fallthrough */ |
NeilBrown | 6658d3a | 2006-10-02 02:17:46 -0700 | [diff] [blame] | 219 | case NFSD_AVAIL: |
Trond Myklebust | e333f3b | 2019-04-09 11:46:19 -0400 | [diff] [blame] | 220 | return nfsd_support_version(vers); |
NeilBrown | 6658d3a | 2006-10-02 02:17:46 -0700 | [diff] [blame] | 221 | } |
| 222 | return 0; |
| 223 | } |
Benny Halevy | 8daf220 | 2009-04-03 08:28:59 +0300 | [diff] [blame] | 224 | |
Trond Myklebust | d3635ff | 2017-02-22 18:35:32 -0500 | [diff] [blame] | 225 | static void |
Trond Myklebust | e333f3b | 2019-04-09 11:46:19 -0400 | [diff] [blame] | 226 | nfsd_adjust_nfsd_versions4(struct nfsd_net *nn) |
Trond Myklebust | d3635ff | 2017-02-22 18:35:32 -0500 | [diff] [blame] | 227 | { |
| 228 | unsigned i; |
| 229 | |
| 230 | for (i = 0; i <= NFSD_SUPPORTED_MINOR_VERSION; i++) { |
Trond Myklebust | e333f3b | 2019-04-09 11:46:19 -0400 | [diff] [blame] | 231 | if (nn->nfsd4_minorversions[i]) |
Trond Myklebust | d3635ff | 2017-02-22 18:35:32 -0500 | [diff] [blame] | 232 | return; |
| 233 | } |
Trond Myklebust | e333f3b | 2019-04-09 11:46:19 -0400 | [diff] [blame] | 234 | nfsd_vers(nn, 4, NFSD_CLEAR); |
Trond Myklebust | d3635ff | 2017-02-22 18:35:32 -0500 | [diff] [blame] | 235 | } |
| 236 | |
Trond Myklebust | e333f3b | 2019-04-09 11:46:19 -0400 | [diff] [blame] | 237 | int nfsd_minorversion(struct nfsd_net *nn, u32 minorversion, enum vers_op change) |
Benny Halevy | 8daf220 | 2009-04-03 08:28:59 +0300 | [diff] [blame] | 238 | { |
NeilBrown | 928c6fb | 2017-03-10 11:36:39 +1100 | [diff] [blame] | 239 | if (minorversion > NFSD_SUPPORTED_MINOR_VERSION && |
| 240 | change != NFSD_AVAIL) |
Benny Halevy | 8daf220 | 2009-04-03 08:28:59 +0300 | [diff] [blame] | 241 | return -1; |
Trond Myklebust | e333f3b | 2019-04-09 11:46:19 -0400 | [diff] [blame] | 242 | |
Benny Halevy | 8daf220 | 2009-04-03 08:28:59 +0300 | [diff] [blame] | 243 | switch(change) { |
| 244 | case NFSD_SET: |
Trond Myklebust | e333f3b | 2019-04-09 11:46:19 -0400 | [diff] [blame] | 245 | if (nn->nfsd4_minorversions) { |
| 246 | nfsd_vers(nn, 4, NFSD_SET); |
| 247 | nn->nfsd4_minorversions[minorversion] = |
| 248 | nfsd_vers(nn, 4, NFSD_TEST); |
| 249 | } |
Benny Halevy | 8daf220 | 2009-04-03 08:28:59 +0300 | [diff] [blame] | 250 | break; |
| 251 | case NFSD_CLEAR: |
Trond Myklebust | e333f3b | 2019-04-09 11:46:19 -0400 | [diff] [blame] | 252 | nfsd_netns_init_versions(nn); |
| 253 | if (nn->nfsd4_minorversions) { |
| 254 | nn->nfsd4_minorversions[minorversion] = false; |
| 255 | nfsd_adjust_nfsd_versions4(nn); |
| 256 | } |
Benny Halevy | 8daf220 | 2009-04-03 08:28:59 +0300 | [diff] [blame] | 257 | break; |
| 258 | case NFSD_TEST: |
Trond Myklebust | e333f3b | 2019-04-09 11:46:19 -0400 | [diff] [blame] | 259 | if (nn->nfsd4_minorversions) |
| 260 | return nn->nfsd4_minorversions[minorversion]; |
| 261 | return nfsd_vers(nn, 4, NFSD_TEST); |
Benny Halevy | 8daf220 | 2009-04-03 08:28:59 +0300 | [diff] [blame] | 262 | case NFSD_AVAIL: |
Trond Myklebust | e333f3b | 2019-04-09 11:46:19 -0400 | [diff] [blame] | 263 | return minorversion <= NFSD_SUPPORTED_MINOR_VERSION && |
| 264 | nfsd_vers(nn, 4, NFSD_AVAIL); |
Benny Halevy | 8daf220 | 2009-04-03 08:28:59 +0300 | [diff] [blame] | 265 | } |
| 266 | return 0; |
| 267 | } |
| 268 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | /* |
| 270 | * Maximum number of nfsd processes |
| 271 | */ |
| 272 | #define NFSD_MAXSERVS 8192 |
| 273 | |
Stanislav Kinsbursky | 9dd9845 | 2012-12-06 14:23:24 +0300 | [diff] [blame] | 274 | int nfsd_nrthreads(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | { |
Neil Brown | c7d106c | 2008-06-12 13:38:42 +1000 | [diff] [blame] | 276 | int rv = 0; |
Stanislav Kinsbursky | 9dd9845 | 2012-12-06 14:23:24 +0300 | [diff] [blame] | 277 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 278 | |
Neil Brown | c7d106c | 2008-06-12 13:38:42 +1000 | [diff] [blame] | 279 | mutex_lock(&nfsd_mutex); |
Stanislav Kinsbursky | 9dd9845 | 2012-12-06 14:23:24 +0300 | [diff] [blame] | 280 | if (nn->nfsd_serv) |
| 281 | rv = nn->nfsd_serv->sv_nrthreads; |
Neil Brown | c7d106c | 2008-06-12 13:38:42 +1000 | [diff] [blame] | 282 | mutex_unlock(&nfsd_mutex); |
| 283 | return rv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Trond Myklebust | 4df493a | 2019-04-09 12:13:37 -0400 | [diff] [blame] | 286 | static int nfsd_init_socks(struct net *net, const struct cred *cred) |
J. Bruce Fields | 59db4a0 | 2010-07-21 18:29:25 -0400 | [diff] [blame] | 287 | { |
| 288 | int error; |
Stanislav Kinsbursky | 9dd9845 | 2012-12-06 14:23:24 +0300 | [diff] [blame] | 289 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 290 | |
| 291 | if (!list_empty(&nn->nfsd_serv->sv_permsocks)) |
J. Bruce Fields | 59db4a0 | 2010-07-21 18:29:25 -0400 | [diff] [blame] | 292 | return 0; |
| 293 | |
Stanislav Kinsbursky | 9dd9845 | 2012-12-06 14:23:24 +0300 | [diff] [blame] | 294 | error = svc_create_xprt(nn->nfsd_serv, "udp", net, PF_INET, NFS_PORT, |
Trond Myklebust | 4df493a | 2019-04-09 12:13:37 -0400 | [diff] [blame] | 295 | SVC_SOCK_DEFAULTS, cred); |
J. Bruce Fields | 59db4a0 | 2010-07-21 18:29:25 -0400 | [diff] [blame] | 296 | if (error < 0) |
| 297 | return error; |
| 298 | |
Stanislav Kinsbursky | 9dd9845 | 2012-12-06 14:23:24 +0300 | [diff] [blame] | 299 | error = svc_create_xprt(nn->nfsd_serv, "tcp", net, PF_INET, NFS_PORT, |
Trond Myklebust | 4df493a | 2019-04-09 12:13:37 -0400 | [diff] [blame] | 300 | SVC_SOCK_DEFAULTS, cred); |
J. Bruce Fields | 59db4a0 | 2010-07-21 18:29:25 -0400 | [diff] [blame] | 301 | if (error < 0) |
| 302 | return error; |
| 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
Stanislav Kinsbursky | 4539f14 | 2012-12-06 14:23:39 +0300 | [diff] [blame] | 307 | static int nfsd_users = 0; |
Jeff Layton | 4ad9a34 | 2010-07-19 16:50:04 -0400 | [diff] [blame] | 308 | |
Stanislav Kinsbursky | bda9cac | 2012-12-06 14:23:29 +0300 | [diff] [blame] | 309 | static int nfsd_startup_generic(int nrservs) |
| 310 | { |
| 311 | int ret; |
| 312 | |
Stanislav Kinsbursky | 4539f14 | 2012-12-06 14:23:39 +0300 | [diff] [blame] | 313 | if (nfsd_users++) |
Stanislav Kinsbursky | bda9cac | 2012-12-06 14:23:29 +0300 | [diff] [blame] | 314 | return 0; |
| 315 | |
| 316 | /* |
| 317 | * Readahead param cache - will no-op if it already exists. |
| 318 | * (Note therefore results will be suboptimal if number of |
| 319 | * threads is modified after nfsd start.) |
| 320 | */ |
| 321 | ret = nfsd_racache_init(2*nrservs); |
| 322 | if (ret) |
Kinglong Mee | d9499a9 | 2014-07-30 21:26:05 +0800 | [diff] [blame] | 323 | goto dec_users; |
| 324 | |
Stanislav Kinsbursky | bda9cac | 2012-12-06 14:23:29 +0300 | [diff] [blame] | 325 | ret = nfs4_state_start(); |
| 326 | if (ret) |
| 327 | goto out_racache; |
| 328 | return 0; |
| 329 | |
| 330 | out_racache: |
| 331 | nfsd_racache_shutdown(); |
Kinglong Mee | d9499a9 | 2014-07-30 21:26:05 +0800 | [diff] [blame] | 332 | dec_users: |
| 333 | nfsd_users--; |
Stanislav Kinsbursky | bda9cac | 2012-12-06 14:23:29 +0300 | [diff] [blame] | 334 | return ret; |
| 335 | } |
| 336 | |
| 337 | static void nfsd_shutdown_generic(void) |
| 338 | { |
Stanislav Kinsbursky | 4539f14 | 2012-12-06 14:23:39 +0300 | [diff] [blame] | 339 | if (--nfsd_users) |
| 340 | return; |
| 341 | |
Stanislav Kinsbursky | bda9cac | 2012-12-06 14:23:29 +0300 | [diff] [blame] | 342 | nfs4_state_shutdown(); |
| 343 | nfsd_racache_shutdown(); |
| 344 | } |
| 345 | |
Trond Myklebust | e333f3b | 2019-04-09 11:46:19 -0400 | [diff] [blame] | 346 | static bool nfsd_needs_lockd(struct nfsd_net *nn) |
Kinglong Mee | 8ef6671 | 2013-12-31 13:17:30 +0800 | [diff] [blame] | 347 | { |
Trond Myklebust | e333f3b | 2019-04-09 11:46:19 -0400 | [diff] [blame] | 348 | return nfsd_vers(nn, 2, NFSD_TEST) || nfsd_vers(nn, 3, NFSD_TEST); |
Kinglong Mee | 8ef6671 | 2013-12-31 13:17:30 +0800 | [diff] [blame] | 349 | } |
| 350 | |
Trond Myklebust | 4df493a | 2019-04-09 12:13:37 -0400 | [diff] [blame] | 351 | static int nfsd_startup_net(int nrservs, struct net *net, const struct cred *cred) |
Stanislav Kinsbursky | 6ff50b3 | 2012-12-06 14:23:09 +0300 | [diff] [blame] | 352 | { |
Stanislav Kinsbursky | 2c2fe29 | 2012-12-06 14:23:14 +0300 | [diff] [blame] | 353 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Stanislav Kinsbursky | 6ff50b3 | 2012-12-06 14:23:09 +0300 | [diff] [blame] | 354 | int ret; |
| 355 | |
Stanislav Kinsbursky | 2c2fe29 | 2012-12-06 14:23:14 +0300 | [diff] [blame] | 356 | if (nn->nfsd_net_up) |
| 357 | return 0; |
| 358 | |
Stanislav Kinsbursky | 903d9bf | 2012-12-06 14:23:34 +0300 | [diff] [blame] | 359 | ret = nfsd_startup_generic(nrservs); |
| 360 | if (ret) |
| 361 | return ret; |
Trond Myklebust | 4df493a | 2019-04-09 12:13:37 -0400 | [diff] [blame] | 362 | ret = nfsd_init_socks(net, cred); |
Stanislav Kinsbursky | 6ff50b3 | 2012-12-06 14:23:09 +0300 | [diff] [blame] | 363 | if (ret) |
Stanislav Kinsbursky | 903d9bf | 2012-12-06 14:23:34 +0300 | [diff] [blame] | 364 | goto out_socks; |
Kinglong Mee | 8ef6671 | 2013-12-31 13:17:30 +0800 | [diff] [blame] | 365 | |
Trond Myklebust | e333f3b | 2019-04-09 11:46:19 -0400 | [diff] [blame] | 366 | if (nfsd_needs_lockd(nn) && !nn->lockd_up) { |
Trond Myklebust | 40373b1 | 2019-04-09 12:13:39 -0400 | [diff] [blame] | 367 | ret = lockd_up(net, cred); |
Kinglong Mee | 8ef6671 | 2013-12-31 13:17:30 +0800 | [diff] [blame] | 368 | if (ret) |
| 369 | goto out_socks; |
| 370 | nn->lockd_up = 1; |
| 371 | } |
| 372 | |
Stanislav Kinsbursky | 6ff50b3 | 2012-12-06 14:23:09 +0300 | [diff] [blame] | 373 | ret = nfs4_state_start_net(net); |
| 374 | if (ret) |
| 375 | goto out_lockd; |
| 376 | |
Stanislav Kinsbursky | 2c2fe29 | 2012-12-06 14:23:14 +0300 | [diff] [blame] | 377 | nn->nfsd_net_up = true; |
Stanislav Kinsbursky | 6ff50b3 | 2012-12-06 14:23:09 +0300 | [diff] [blame] | 378 | return 0; |
| 379 | |
| 380 | out_lockd: |
Kinglong Mee | 8ef6671 | 2013-12-31 13:17:30 +0800 | [diff] [blame] | 381 | if (nn->lockd_up) { |
| 382 | lockd_down(net); |
| 383 | nn->lockd_up = 0; |
| 384 | } |
Stanislav Kinsbursky | 903d9bf | 2012-12-06 14:23:34 +0300 | [diff] [blame] | 385 | out_socks: |
Stanislav Kinsbursky | bda9cac | 2012-12-06 14:23:29 +0300 | [diff] [blame] | 386 | nfsd_shutdown_generic(); |
Jeff Layton | 4ad9a34 | 2010-07-19 16:50:04 -0400 | [diff] [blame] | 387 | return ret; |
| 388 | } |
| 389 | |
Stanislav Kinsbursky | 6ff50b3 | 2012-12-06 14:23:09 +0300 | [diff] [blame] | 390 | static void nfsd_shutdown_net(struct net *net) |
| 391 | { |
Stanislav Kinsbursky | 2c2fe29 | 2012-12-06 14:23:14 +0300 | [diff] [blame] | 392 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 393 | |
Stanislav Kinsbursky | 6ff50b3 | 2012-12-06 14:23:09 +0300 | [diff] [blame] | 394 | nfs4_state_shutdown_net(net); |
Kinglong Mee | 8ef6671 | 2013-12-31 13:17:30 +0800 | [diff] [blame] | 395 | if (nn->lockd_up) { |
| 396 | lockd_down(net); |
| 397 | nn->lockd_up = 0; |
| 398 | } |
Stanislav Kinsbursky | 2c2fe29 | 2012-12-06 14:23:14 +0300 | [diff] [blame] | 399 | nn->nfsd_net_up = false; |
Stanislav Kinsbursky | 903d9bf | 2012-12-06 14:23:34 +0300 | [diff] [blame] | 400 | nfsd_shutdown_generic(); |
Stanislav Kinsbursky | 6ff50b3 | 2012-12-06 14:23:09 +0300 | [diff] [blame] | 401 | } |
| 402 | |
Scott Mayhew | 3668499 | 2015-12-11 16:45:59 -0500 | [diff] [blame] | 403 | static int nfsd_inetaddr_event(struct notifier_block *this, unsigned long event, |
| 404 | void *ptr) |
| 405 | { |
| 406 | struct in_ifaddr *ifa = (struct in_ifaddr *)ptr; |
| 407 | struct net_device *dev = ifa->ifa_dev->dev; |
| 408 | struct net *net = dev_net(dev); |
| 409 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 410 | struct sockaddr_in sin; |
| 411 | |
Vasily Averin | 2317dc5 | 2017-11-10 10:19:35 +0300 | [diff] [blame] | 412 | if ((event != NETDEV_DOWN) || |
| 413 | !atomic_inc_not_zero(&nn->ntf_refcnt)) |
Scott Mayhew | 3668499 | 2015-12-11 16:45:59 -0500 | [diff] [blame] | 414 | goto out; |
| 415 | |
| 416 | if (nn->nfsd_serv) { |
| 417 | dprintk("nfsd_inetaddr_event: removed %pI4\n", &ifa->ifa_local); |
| 418 | sin.sin_family = AF_INET; |
| 419 | sin.sin_addr.s_addr = ifa->ifa_local; |
| 420 | svc_age_temp_xprts_now(nn->nfsd_serv, (struct sockaddr *)&sin); |
| 421 | } |
Vasily Averin | 2317dc5 | 2017-11-10 10:19:35 +0300 | [diff] [blame] | 422 | atomic_dec(&nn->ntf_refcnt); |
| 423 | wake_up(&nn->ntf_wq); |
Scott Mayhew | 3668499 | 2015-12-11 16:45:59 -0500 | [diff] [blame] | 424 | |
| 425 | out: |
| 426 | return NOTIFY_DONE; |
| 427 | } |
| 428 | |
| 429 | static struct notifier_block nfsd_inetaddr_notifier = { |
| 430 | .notifier_call = nfsd_inetaddr_event, |
| 431 | }; |
| 432 | |
| 433 | #if IS_ENABLED(CONFIG_IPV6) |
| 434 | static int nfsd_inet6addr_event(struct notifier_block *this, |
| 435 | unsigned long event, void *ptr) |
| 436 | { |
| 437 | struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr; |
| 438 | struct net_device *dev = ifa->idev->dev; |
| 439 | struct net *net = dev_net(dev); |
| 440 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 441 | struct sockaddr_in6 sin6; |
| 442 | |
Vasily Averin | 2317dc5 | 2017-11-10 10:19:35 +0300 | [diff] [blame] | 443 | if ((event != NETDEV_DOWN) || |
| 444 | !atomic_inc_not_zero(&nn->ntf_refcnt)) |
Scott Mayhew | 3668499 | 2015-12-11 16:45:59 -0500 | [diff] [blame] | 445 | goto out; |
| 446 | |
| 447 | if (nn->nfsd_serv) { |
| 448 | dprintk("nfsd_inet6addr_event: removed %pI6\n", &ifa->addr); |
| 449 | sin6.sin6_family = AF_INET6; |
| 450 | sin6.sin6_addr = ifa->addr; |
Scott Mayhew | 7b19824 | 2017-01-05 16:34:49 -0500 | [diff] [blame] | 451 | if (ipv6_addr_type(&sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL) |
| 452 | sin6.sin6_scope_id = ifa->idev->dev->ifindex; |
Scott Mayhew | 3668499 | 2015-12-11 16:45:59 -0500 | [diff] [blame] | 453 | svc_age_temp_xprts_now(nn->nfsd_serv, (struct sockaddr *)&sin6); |
| 454 | } |
Vasily Averin | 2317dc5 | 2017-11-10 10:19:35 +0300 | [diff] [blame] | 455 | atomic_dec(&nn->ntf_refcnt); |
| 456 | wake_up(&nn->ntf_wq); |
Scott Mayhew | 3668499 | 2015-12-11 16:45:59 -0500 | [diff] [blame] | 457 | out: |
| 458 | return NOTIFY_DONE; |
| 459 | } |
| 460 | |
| 461 | static struct notifier_block nfsd_inet6addr_notifier = { |
| 462 | .notifier_call = nfsd_inet6addr_event, |
| 463 | }; |
| 464 | #endif |
| 465 | |
Vasily Averin | 1eca45f | 2016-09-21 15:33:05 +0300 | [diff] [blame] | 466 | /* Only used under nfsd_mutex, so this atomic may be overkill: */ |
| 467 | static atomic_t nfsd_notifier_refcount = ATOMIC_INIT(0); |
| 468 | |
Stanislav Kinsbursky | 541e864 | 2012-12-06 14:23:44 +0300 | [diff] [blame] | 469 | static void nfsd_last_thread(struct svc_serv *serv, struct net *net) |
Jeff Layton | 4ad9a34 | 2010-07-19 16:50:04 -0400 | [diff] [blame] | 470 | { |
Stanislav Kinsbursky | 903d9bf | 2012-12-06 14:23:34 +0300 | [diff] [blame] | 471 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 472 | |
Vasily Averin | 2317dc5 | 2017-11-10 10:19:35 +0300 | [diff] [blame] | 473 | atomic_dec(&nn->ntf_refcnt); |
Vasily Averin | 1eca45f | 2016-09-21 15:33:05 +0300 | [diff] [blame] | 474 | /* check if the notifier still has clients */ |
| 475 | if (atomic_dec_return(&nfsd_notifier_refcount) == 0) { |
| 476 | unregister_inetaddr_notifier(&nfsd_inetaddr_notifier); |
Scott Mayhew | 3668499 | 2015-12-11 16:45:59 -0500 | [diff] [blame] | 477 | #if IS_ENABLED(CONFIG_IPV6) |
Vasily Averin | 1eca45f | 2016-09-21 15:33:05 +0300 | [diff] [blame] | 478 | unregister_inet6addr_notifier(&nfsd_inet6addr_notifier); |
Scott Mayhew | 3668499 | 2015-12-11 16:45:59 -0500 | [diff] [blame] | 479 | #endif |
Vasily Averin | 1eca45f | 2016-09-21 15:33:05 +0300 | [diff] [blame] | 480 | } |
Vasily Averin | 2317dc5 | 2017-11-10 10:19:35 +0300 | [diff] [blame] | 481 | wait_event(nn->ntf_wq, atomic_read(&nn->ntf_refcnt) == 0); |
Vasily Averin | 1eca45f | 2016-09-21 15:33:05 +0300 | [diff] [blame] | 482 | |
Jeff Layton | 4ad9a34 | 2010-07-19 16:50:04 -0400 | [diff] [blame] | 483 | /* |
| 484 | * write_ports can create the server without actually starting |
| 485 | * any threads--if we get shut down before any threads are |
| 486 | * started, then nfsd_last_thread will be run before any of this |
Kinglong Mee | 691412b | 2016-01-04 11:15:21 +0800 | [diff] [blame] | 487 | * other initialization has been done except the rpcb information. |
Jeff Layton | 4ad9a34 | 2010-07-19 16:50:04 -0400 | [diff] [blame] | 488 | */ |
Kinglong Mee | 691412b | 2016-01-04 11:15:21 +0800 | [diff] [blame] | 489 | svc_rpcb_cleanup(serv, net); |
Stanislav Kinsbursky | 903d9bf | 2012-12-06 14:23:34 +0300 | [diff] [blame] | 490 | if (!nn->nfsd_net_up) |
Jeff Layton | 4ad9a34 | 2010-07-19 16:50:04 -0400 | [diff] [blame] | 491 | return; |
Kinglong Mee | 691412b | 2016-01-04 11:15:21 +0800 | [diff] [blame] | 492 | |
Stanislav Kinsbursky | 6ff50b3 | 2012-12-06 14:23:09 +0300 | [diff] [blame] | 493 | nfsd_shutdown_net(net); |
Jeff Layton | e096bbc | 2008-06-10 08:40:37 -0400 | [diff] [blame] | 494 | printk(KERN_WARNING "nfsd: last server has exited, flushing export " |
| 495 | "cache\n"); |
Stanislav Kinsbursky | b3853e0 | 2012-04-11 15:13:21 +0400 | [diff] [blame] | 496 | nfsd_export_flush(net); |
NeilBrown | bc591cc | 2006-10-02 02:17:44 -0700 | [diff] [blame] | 497 | } |
NeilBrown | 6658d3a | 2006-10-02 02:17:46 -0700 | [diff] [blame] | 498 | |
Trond Myklebust | e333f3b | 2019-04-09 11:46:19 -0400 | [diff] [blame] | 499 | void nfsd_reset_versions(struct nfsd_net *nn) |
NeilBrown | 6658d3a | 2006-10-02 02:17:46 -0700 | [diff] [blame] | 500 | { |
NeilBrown | 6658d3a | 2006-10-02 02:17:46 -0700 | [diff] [blame] | 501 | int i; |
| 502 | |
NeilBrown | 800a938 | 2017-03-10 11:36:39 +1100 | [diff] [blame] | 503 | for (i = 0; i < NFSD_NRVERS; i++) |
Trond Myklebust | e333f3b | 2019-04-09 11:46:19 -0400 | [diff] [blame] | 504 | if (nfsd_vers(nn, i, NFSD_TEST)) |
NeilBrown | 800a938 | 2017-03-10 11:36:39 +1100 | [diff] [blame] | 505 | return; |
NeilBrown | 6658d3a | 2006-10-02 02:17:46 -0700 | [diff] [blame] | 506 | |
NeilBrown | 800a938 | 2017-03-10 11:36:39 +1100 | [diff] [blame] | 507 | for (i = 0; i < NFSD_NRVERS; i++) |
| 508 | if (i != 4) |
Trond Myklebust | e333f3b | 2019-04-09 11:46:19 -0400 | [diff] [blame] | 509 | nfsd_vers(nn, i, NFSD_SET); |
NeilBrown | 800a938 | 2017-03-10 11:36:39 +1100 | [diff] [blame] | 510 | else { |
| 511 | int minor = 0; |
Trond Myklebust | e333f3b | 2019-04-09 11:46:19 -0400 | [diff] [blame] | 512 | while (nfsd_minorversion(nn, minor, NFSD_SET) >= 0) |
NeilBrown | 800a938 | 2017-03-10 11:36:39 +1100 | [diff] [blame] | 513 | minor++; |
| 514 | } |
NeilBrown | 6658d3a | 2006-10-02 02:17:46 -0700 | [diff] [blame] | 515 | } |
| 516 | |
Andy Adamson | c3d06f9 | 2009-04-03 08:28:18 +0300 | [diff] [blame] | 517 | /* |
| 518 | * Each session guarantees a negotiated per slot memory cache for replies |
| 519 | * which in turn consumes memory beyond the v2/v3/v4.0 server. A dedicated |
| 520 | * NFSv4.1 server might want to use more memory for a DRC than a machine |
| 521 | * with mutiple services. |
| 522 | * |
| 523 | * Impose a hard limit on the number of pages for the DRC which varies |
| 524 | * according to the machines free pages. This is of course only a default. |
| 525 | * |
| 526 | * For now this is a #defined shift which could be under admin control |
| 527 | * in the future. |
| 528 | */ |
| 529 | static void set_max_drc(void) |
| 530 | { |
J. Bruce Fields | 44d8660 | 2017-09-19 20:51:31 -0400 | [diff] [blame] | 531 | #define NFSD_DRC_SIZE_SHIFT 7 |
Andy Adamson | 0c19305 | 2009-07-27 19:09:19 -0400 | [diff] [blame] | 532 | nfsd_drc_max_mem = (nr_free_buffer_pages() |
| 533 | >> NFSD_DRC_SIZE_SHIFT) * PAGE_SIZE; |
| 534 | nfsd_drc_mem_used = 0; |
Andy Adamson | 4bd9b0f4 | 2009-06-24 15:37:45 -0400 | [diff] [blame] | 535 | spin_lock_init(&nfsd_drc_lock); |
Zhang Yanfei | 697ce9b | 2013-02-22 16:35:47 -0800 | [diff] [blame] | 536 | dprintk("%s nfsd_drc_max_mem %lu \n", __func__, nfsd_drc_max_mem); |
Andy Adamson | c3d06f9 | 2009-04-03 08:28:18 +0300 | [diff] [blame] | 537 | } |
Neil Brown | bedbdd8 | 2008-06-10 08:40:35 -0400 | [diff] [blame] | 538 | |
J. Bruce Fields | 87b0fc7 | 2012-01-30 16:18:35 -0500 | [diff] [blame] | 539 | static int nfsd_get_default_max_blksize(void) |
| 540 | { |
| 541 | struct sysinfo i; |
| 542 | unsigned long long target; |
| 543 | unsigned long ret; |
| 544 | |
| 545 | si_meminfo(&i); |
J. Bruce Fields | 508f922 | 2012-01-30 16:21:11 -0500 | [diff] [blame] | 546 | target = (i.totalram - i.totalhigh) << PAGE_SHIFT; |
J. Bruce Fields | 87b0fc7 | 2012-01-30 16:18:35 -0500 | [diff] [blame] | 547 | /* |
| 548 | * Aim for 1/4096 of memory per thread This gives 1MB on 4Gig |
| 549 | * machines, but only uses 32K on 128M machines. Bottom out at |
| 550 | * 8K on 32M and smaller. Of course, this is only a default. |
| 551 | */ |
| 552 | target >>= 12; |
| 553 | |
| 554 | ret = NFSSVC_MAXBLKSIZE; |
| 555 | while (ret > target && ret >= 8*1024*2) |
| 556 | ret /= 2; |
| 557 | return ret; |
| 558 | } |
| 559 | |
Chuck Lever | afea565 | 2017-08-01 12:00:06 -0400 | [diff] [blame] | 560 | static const struct svc_serv_ops nfsd_thread_sv_ops = { |
Jeff Layton | b9e13cd | 2015-06-08 12:06:51 -0700 | [diff] [blame] | 561 | .svo_shutdown = nfsd_last_thread, |
| 562 | .svo_function = nfsd, |
| 563 | .svo_enqueue_xprt = svc_xprt_do_enqueue, |
Jeff Layton | 598e235 | 2015-06-08 12:08:33 -0700 | [diff] [blame] | 564 | .svo_setup = svc_set_num_threads, |
Jeff Layton | b9e13cd | 2015-06-08 12:06:51 -0700 | [diff] [blame] | 565 | .svo_module = THIS_MODULE, |
Jeff Layton | ea126e7 | 2015-06-08 12:03:32 -0700 | [diff] [blame] | 566 | }; |
| 567 | |
Stanislav Kinsbursky | 6777436 | 2012-12-10 12:19:20 +0300 | [diff] [blame] | 568 | int nfsd_create_serv(struct net *net) |
NeilBrown | 02a375f | 2006-10-02 02:17:46 -0700 | [diff] [blame] | 569 | { |
Stanislav Kinsbursky | 9793f7c | 2012-05-02 16:08:38 +0400 | [diff] [blame] | 570 | int error; |
Stanislav Kinsbursky | b9c0ef8 | 2012-12-06 14:23:19 +0300 | [diff] [blame] | 571 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Stanislav Kinsbursky | 9793f7c | 2012-05-02 16:08:38 +0400 | [diff] [blame] | 572 | |
Neil Brown | bedbdd8 | 2008-06-10 08:40:35 -0400 | [diff] [blame] | 573 | WARN_ON(!mutex_is_locked(&nfsd_mutex)); |
Stanislav Kinsbursky | 9dd9845 | 2012-12-06 14:23:24 +0300 | [diff] [blame] | 574 | if (nn->nfsd_serv) { |
| 575 | svc_get(nn->nfsd_serv); |
NeilBrown | 02a375f | 2006-10-02 02:17:46 -0700 | [diff] [blame] | 576 | return 0; |
| 577 | } |
J. Bruce Fields | 87b0fc7 | 2012-01-30 16:18:35 -0500 | [diff] [blame] | 578 | if (nfsd_max_blksize == 0) |
| 579 | nfsd_max_blksize = nfsd_get_default_max_blksize(); |
Trond Myklebust | e333f3b | 2019-04-09 11:46:19 -0400 | [diff] [blame] | 580 | nfsd_reset_versions(nn); |
Stanislav Kinsbursky | 9dd9845 | 2012-12-06 14:23:24 +0300 | [diff] [blame] | 581 | nn->nfsd_serv = svc_create_pooled(&nfsd_program, nfsd_max_blksize, |
Jeff Layton | b9e13cd | 2015-06-08 12:06:51 -0700 | [diff] [blame] | 582 | &nfsd_thread_sv_ops); |
Stanislav Kinsbursky | 9dd9845 | 2012-12-06 14:23:24 +0300 | [diff] [blame] | 583 | if (nn->nfsd_serv == NULL) |
Jeff Layton | 628b368 | 2010-07-21 16:40:08 -0400 | [diff] [blame] | 584 | return -ENOMEM; |
Neil Brown | bedbdd8 | 2008-06-10 08:40:35 -0400 | [diff] [blame] | 585 | |
Jeff Layton | 5b8db00 | 2014-07-02 16:11:22 -0400 | [diff] [blame] | 586 | nn->nfsd_serv->sv_maxconn = nn->max_connections; |
Stanislav Kinsbursky | 9dd9845 | 2012-12-06 14:23:24 +0300 | [diff] [blame] | 587 | error = svc_bind(nn->nfsd_serv, net); |
Stanislav Kinsbursky | 9793f7c | 2012-05-02 16:08:38 +0400 | [diff] [blame] | 588 | if (error < 0) { |
Stanislav Kinsbursky | 9dd9845 | 2012-12-06 14:23:24 +0300 | [diff] [blame] | 589 | svc_destroy(nn->nfsd_serv); |
Stanislav Kinsbursky | 9793f7c | 2012-05-02 16:08:38 +0400 | [diff] [blame] | 590 | return error; |
| 591 | } |
| 592 | |
Jeff Layton | 628b368 | 2010-07-21 16:40:08 -0400 | [diff] [blame] | 593 | set_max_drc(); |
Vasily Averin | 1eca45f | 2016-09-21 15:33:05 +0300 | [diff] [blame] | 594 | /* check if the notifier is already set */ |
| 595 | if (atomic_inc_return(&nfsd_notifier_refcount) == 1) { |
| 596 | register_inetaddr_notifier(&nfsd_inetaddr_notifier); |
Scott Mayhew | 3668499 | 2015-12-11 16:45:59 -0500 | [diff] [blame] | 597 | #if IS_ENABLED(CONFIG_IPV6) |
Vasily Averin | 1eca45f | 2016-09-21 15:33:05 +0300 | [diff] [blame] | 598 | register_inet6addr_notifier(&nfsd_inet6addr_notifier); |
Scott Mayhew | 3668499 | 2015-12-11 16:45:59 -0500 | [diff] [blame] | 599 | #endif |
Vasily Averin | 1eca45f | 2016-09-21 15:33:05 +0300 | [diff] [blame] | 600 | } |
Vasily Averin | 2317dc5 | 2017-11-10 10:19:35 +0300 | [diff] [blame] | 601 | atomic_inc(&nn->ntf_refcnt); |
Arnd Bergmann | 256a89f | 2017-10-19 12:04:11 +0200 | [diff] [blame] | 602 | ktime_get_real_ts64(&nn->nfssvc_boot); /* record boot time */ |
J. Bruce Fields | 87b0fc7 | 2012-01-30 16:18:35 -0500 | [diff] [blame] | 603 | return 0; |
NeilBrown | 02a375f | 2006-10-02 02:17:46 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Stanislav Kinsbursky | 9dd9845 | 2012-12-06 14:23:24 +0300 | [diff] [blame] | 606 | int nfsd_nrpools(struct net *net) |
Greg Banks | eed2965 | 2006-10-02 02:18:02 -0700 | [diff] [blame] | 607 | { |
Stanislav Kinsbursky | 9dd9845 | 2012-12-06 14:23:24 +0300 | [diff] [blame] | 608 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 609 | |
| 610 | if (nn->nfsd_serv == NULL) |
Greg Banks | eed2965 | 2006-10-02 02:18:02 -0700 | [diff] [blame] | 611 | return 0; |
| 612 | else |
Stanislav Kinsbursky | 9dd9845 | 2012-12-06 14:23:24 +0300 | [diff] [blame] | 613 | return nn->nfsd_serv->sv_nrpools; |
Greg Banks | eed2965 | 2006-10-02 02:18:02 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Stanislav Kinsbursky | 9dd9845 | 2012-12-06 14:23:24 +0300 | [diff] [blame] | 616 | int nfsd_get_nrthreads(int n, int *nthreads, struct net *net) |
Greg Banks | eed2965 | 2006-10-02 02:18:02 -0700 | [diff] [blame] | 617 | { |
| 618 | int i = 0; |
Stanislav Kinsbursky | 9dd9845 | 2012-12-06 14:23:24 +0300 | [diff] [blame] | 619 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Greg Banks | eed2965 | 2006-10-02 02:18:02 -0700 | [diff] [blame] | 620 | |
Stanislav Kinsbursky | 9dd9845 | 2012-12-06 14:23:24 +0300 | [diff] [blame] | 621 | if (nn->nfsd_serv != NULL) { |
| 622 | for (i = 0; i < nn->nfsd_serv->sv_nrpools && i < n; i++) |
| 623 | nthreads[i] = nn->nfsd_serv->sv_pools[i].sp_nrthreads; |
Greg Banks | eed2965 | 2006-10-02 02:18:02 -0700 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | return 0; |
| 627 | } |
| 628 | |
Stanislav Kinsbursky | 9dd9845 | 2012-12-06 14:23:24 +0300 | [diff] [blame] | 629 | void nfsd_destroy(struct net *net) |
| 630 | { |
| 631 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 632 | int destroy = (nn->nfsd_serv->sv_nrthreads == 1); |
| 633 | |
| 634 | if (destroy) |
| 635 | svc_shutdown_net(nn->nfsd_serv, net); |
| 636 | svc_destroy(nn->nfsd_serv); |
| 637 | if (destroy) |
| 638 | nn->nfsd_serv = NULL; |
| 639 | } |
| 640 | |
Stanislav Kinsbursky | 3938a0d | 2012-12-10 12:19:30 +0300 | [diff] [blame] | 641 | int nfsd_set_nrthreads(int n, int *nthreads, struct net *net) |
Greg Banks | eed2965 | 2006-10-02 02:18:02 -0700 | [diff] [blame] | 642 | { |
| 643 | int i = 0; |
| 644 | int tot = 0; |
| 645 | int err = 0; |
Stanislav Kinsbursky | 9dd9845 | 2012-12-06 14:23:24 +0300 | [diff] [blame] | 646 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Greg Banks | eed2965 | 2006-10-02 02:18:02 -0700 | [diff] [blame] | 647 | |
Neil Brown | bedbdd8 | 2008-06-10 08:40:35 -0400 | [diff] [blame] | 648 | WARN_ON(!mutex_is_locked(&nfsd_mutex)); |
| 649 | |
Stanislav Kinsbursky | 9dd9845 | 2012-12-06 14:23:24 +0300 | [diff] [blame] | 650 | if (nn->nfsd_serv == NULL || n <= 0) |
Greg Banks | eed2965 | 2006-10-02 02:18:02 -0700 | [diff] [blame] | 651 | return 0; |
| 652 | |
Stanislav Kinsbursky | 9dd9845 | 2012-12-06 14:23:24 +0300 | [diff] [blame] | 653 | if (n > nn->nfsd_serv->sv_nrpools) |
| 654 | n = nn->nfsd_serv->sv_nrpools; |
Greg Banks | eed2965 | 2006-10-02 02:18:02 -0700 | [diff] [blame] | 655 | |
| 656 | /* enforce a global maximum number of threads */ |
| 657 | tot = 0; |
| 658 | for (i = 0; i < n; i++) { |
Kinglong Mee | 3c7aa15 | 2014-06-10 18:08:19 +0800 | [diff] [blame] | 659 | nthreads[i] = min(nthreads[i], NFSD_MAXSERVS); |
Greg Banks | eed2965 | 2006-10-02 02:18:02 -0700 | [diff] [blame] | 660 | tot += nthreads[i]; |
| 661 | } |
| 662 | if (tot > NFSD_MAXSERVS) { |
| 663 | /* total too large: scale down requested numbers */ |
| 664 | for (i = 0; i < n && tot > 0; i++) { |
| 665 | int new = nthreads[i] * NFSD_MAXSERVS / tot; |
| 666 | tot -= (nthreads[i] - new); |
| 667 | nthreads[i] = new; |
| 668 | } |
| 669 | for (i = 0; i < n && tot > 0; i++) { |
| 670 | nthreads[i]--; |
| 671 | tot--; |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | /* |
| 676 | * There must always be a thread in pool 0; the admin |
| 677 | * can't shut down NFS completely using pool_threads. |
| 678 | */ |
| 679 | if (nthreads[0] == 0) |
| 680 | nthreads[0] = 1; |
| 681 | |
| 682 | /* apply the new numbers */ |
Stanislav Kinsbursky | 9dd9845 | 2012-12-06 14:23:24 +0300 | [diff] [blame] | 683 | svc_get(nn->nfsd_serv); |
Greg Banks | eed2965 | 2006-10-02 02:18:02 -0700 | [diff] [blame] | 684 | for (i = 0; i < n; i++) { |
Jeff Layton | 598e235 | 2015-06-08 12:08:33 -0700 | [diff] [blame] | 685 | err = nn->nfsd_serv->sv_ops->svo_setup(nn->nfsd_serv, |
| 686 | &nn->nfsd_serv->sv_pools[i], nthreads[i]); |
Greg Banks | eed2965 | 2006-10-02 02:18:02 -0700 | [diff] [blame] | 687 | if (err) |
| 688 | break; |
| 689 | } |
Stanislav Kinsbursky | 19f7e2c | 2012-07-03 16:46:41 +0400 | [diff] [blame] | 690 | nfsd_destroy(net); |
Greg Banks | eed2965 | 2006-10-02 02:18:02 -0700 | [diff] [blame] | 691 | return err; |
| 692 | } |
| 693 | |
Jeff Layton | ac77efb | 2010-07-20 14:10:22 -0400 | [diff] [blame] | 694 | /* |
| 695 | * Adjust the number of threads and return the new number of threads. |
| 696 | * This is also the function that starts the server if necessary, if |
| 697 | * this is the first time nrservs is nonzero. |
| 698 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | int |
Trond Myklebust | 4df493a | 2019-04-09 12:13:37 -0400 | [diff] [blame] | 700 | nfsd_svc(int nrservs, struct net *net, const struct cred *cred) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | { |
| 702 | int error; |
J. Bruce Fields | 774f8bb | 2010-08-02 14:12:44 -0400 | [diff] [blame] | 703 | bool nfsd_up_before; |
Stanislav Kinsbursky | 9dd9845 | 2012-12-06 14:23:24 +0300 | [diff] [blame] | 704 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Neil Brown | bedbdd8 | 2008-06-10 08:40:35 -0400 | [diff] [blame] | 705 | |
| 706 | mutex_lock(&nfsd_mutex); |
NeilBrown | 6658d3a | 2006-10-02 02:17:46 -0700 | [diff] [blame] | 707 | dprintk("nfsd: creating service\n"); |
Kinglong Mee | 3c7aa15 | 2014-06-10 18:08:19 +0800 | [diff] [blame] | 708 | |
| 709 | nrservs = max(nrservs, 0); |
| 710 | nrservs = min(nrservs, NFSD_MAXSERVS); |
NeilBrown | 671e1fc | 2009-06-16 11:03:20 +1000 | [diff] [blame] | 711 | error = 0; |
Kinglong Mee | 3c7aa15 | 2014-06-10 18:08:19 +0800 | [diff] [blame] | 712 | |
Stanislav Kinsbursky | 9dd9845 | 2012-12-06 14:23:24 +0300 | [diff] [blame] | 713 | if (nrservs == 0 && nn->nfsd_serv == NULL) |
NeilBrown | 671e1fc | 2009-06-16 11:03:20 +1000 | [diff] [blame] | 714 | goto out; |
| 715 | |
Stanislav Kinsbursky | 6777436 | 2012-12-10 12:19:20 +0300 | [diff] [blame] | 716 | error = nfsd_create_serv(net); |
NeilBrown | 02a375f | 2006-10-02 02:17:46 -0700 | [diff] [blame] | 717 | if (error) |
J. Bruce Fields | 774f8bb | 2010-08-02 14:12:44 -0400 | [diff] [blame] | 718 | goto out; |
| 719 | |
Stanislav Kinsbursky | 903d9bf | 2012-12-06 14:23:34 +0300 | [diff] [blame] | 720 | nfsd_up_before = nn->nfsd_net_up; |
J. Bruce Fields | 774f8bb | 2010-08-02 14:12:44 -0400 | [diff] [blame] | 721 | |
Trond Myklebust | 4df493a | 2019-04-09 12:13:37 -0400 | [diff] [blame] | 722 | error = nfsd_startup_net(nrservs, net, cred); |
J. Bruce Fields | af4718f | 2010-07-21 18:31:42 -0400 | [diff] [blame] | 723 | if (error) |
| 724 | goto out_destroy; |
Jeff Layton | 598e235 | 2015-06-08 12:08:33 -0700 | [diff] [blame] | 725 | error = nn->nfsd_serv->sv_ops->svo_setup(nn->nfsd_serv, |
| 726 | NULL, nrservs); |
J. Bruce Fields | 774f8bb | 2010-08-02 14:12:44 -0400 | [diff] [blame] | 727 | if (error) |
| 728 | goto out_shutdown; |
Stanislav Kinsbursky | 9dd9845 | 2012-12-06 14:23:24 +0300 | [diff] [blame] | 729 | /* We are holding a reference to nn->nfsd_serv which |
J. Bruce Fields | af4718f | 2010-07-21 18:31:42 -0400 | [diff] [blame] | 730 | * we don't want to count in the return value, |
| 731 | * so subtract 1 |
| 732 | */ |
Stanislav Kinsbursky | 9dd9845 | 2012-12-06 14:23:24 +0300 | [diff] [blame] | 733 | error = nn->nfsd_serv->sv_nrthreads - 1; |
J. Bruce Fields | 774f8bb | 2010-08-02 14:12:44 -0400 | [diff] [blame] | 734 | out_shutdown: |
| 735 | if (error < 0 && !nfsd_up_before) |
Stanislav Kinsbursky | 541e864 | 2012-12-06 14:23:44 +0300 | [diff] [blame] | 736 | nfsd_shutdown_net(net); |
Jeff Layton | 4ad9a34 | 2010-07-19 16:50:04 -0400 | [diff] [blame] | 737 | out_destroy: |
Stanislav Kinsbursky | 19f7e2c | 2012-07-03 16:46:41 +0400 | [diff] [blame] | 738 | nfsd_destroy(net); /* Release server */ |
Jeff Layton | 4ad9a34 | 2010-07-19 16:50:04 -0400 | [diff] [blame] | 739 | out: |
Neil Brown | bedbdd8 | 2008-06-10 08:40:35 -0400 | [diff] [blame] | 740 | mutex_unlock(&nfsd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | return error; |
| 742 | } |
| 743 | |
Trond Myklebust | 029be5d | 2019-04-09 11:46:18 -0400 | [diff] [blame] | 744 | #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) |
| 745 | static bool |
| 746 | nfsd_support_acl_version(int vers) |
| 747 | { |
| 748 | if (vers >= NFSD_ACL_MINVERS && vers < NFSD_ACL_NRVERS) |
| 749 | return nfsd_acl_version[vers] != NULL; |
| 750 | return false; |
| 751 | } |
| 752 | |
| 753 | static int |
| 754 | nfsd_acl_rpcbind_set(struct net *net, const struct svc_program *progp, |
| 755 | u32 version, int family, unsigned short proto, |
| 756 | unsigned short port) |
| 757 | { |
| 758 | if (!nfsd_support_acl_version(version) || |
Trond Myklebust | e333f3b | 2019-04-09 11:46:19 -0400 | [diff] [blame] | 759 | !nfsd_vers(net_generic(net, nfsd_net_id), version, NFSD_TEST)) |
Trond Myklebust | 029be5d | 2019-04-09 11:46:18 -0400 | [diff] [blame] | 760 | return 0; |
| 761 | return svc_generic_rpcbind_set(net, progp, version, family, |
| 762 | proto, port); |
| 763 | } |
Trond Myklebust | e333f3b | 2019-04-09 11:46:19 -0400 | [diff] [blame] | 764 | |
| 765 | static __be32 |
| 766 | nfsd_acl_init_request(struct svc_rqst *rqstp, |
| 767 | const struct svc_program *progp, |
| 768 | struct svc_process_info *ret) |
| 769 | { |
| 770 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 771 | int i; |
| 772 | |
| 773 | if (likely(nfsd_support_acl_version(rqstp->rq_vers) && |
| 774 | nfsd_vers(nn, rqstp->rq_vers, NFSD_TEST))) |
| 775 | return svc_generic_init_request(rqstp, progp, ret); |
| 776 | |
| 777 | ret->mismatch.lovers = NFSD_ACL_NRVERS; |
| 778 | for (i = NFSD_ACL_MINVERS; i < NFSD_ACL_NRVERS; i++) { |
| 779 | if (nfsd_support_acl_version(rqstp->rq_vers) && |
| 780 | nfsd_vers(nn, i, NFSD_TEST)) { |
| 781 | ret->mismatch.lovers = i; |
| 782 | break; |
| 783 | } |
| 784 | } |
| 785 | if (ret->mismatch.lovers == NFSD_ACL_NRVERS) |
| 786 | return rpc_prog_unavail; |
| 787 | ret->mismatch.hivers = NFSD_ACL_MINVERS; |
| 788 | for (i = NFSD_ACL_NRVERS - 1; i >= NFSD_ACL_MINVERS; i--) { |
| 789 | if (nfsd_support_acl_version(rqstp->rq_vers) && |
| 790 | nfsd_vers(nn, i, NFSD_TEST)) { |
| 791 | ret->mismatch.hivers = i; |
| 792 | break; |
| 793 | } |
| 794 | } |
| 795 | return rpc_prog_mismatch; |
| 796 | } |
Trond Myklebust | 029be5d | 2019-04-09 11:46:18 -0400 | [diff] [blame] | 797 | #endif |
| 798 | |
| 799 | static int |
| 800 | nfsd_rpcbind_set(struct net *net, const struct svc_program *progp, |
| 801 | u32 version, int family, unsigned short proto, |
| 802 | unsigned short port) |
| 803 | { |
Trond Myklebust | e333f3b | 2019-04-09 11:46:19 -0400 | [diff] [blame] | 804 | if (!nfsd_vers(net_generic(net, nfsd_net_id), version, NFSD_TEST)) |
Trond Myklebust | 029be5d | 2019-04-09 11:46:18 -0400 | [diff] [blame] | 805 | return 0; |
| 806 | return svc_generic_rpcbind_set(net, progp, version, family, |
| 807 | proto, port); |
| 808 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | |
Trond Myklebust | e333f3b | 2019-04-09 11:46:19 -0400 | [diff] [blame] | 810 | static __be32 |
| 811 | nfsd_init_request(struct svc_rqst *rqstp, |
| 812 | const struct svc_program *progp, |
| 813 | struct svc_process_info *ret) |
| 814 | { |
| 815 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 816 | int i; |
| 817 | |
| 818 | if (likely(nfsd_vers(nn, rqstp->rq_vers, NFSD_TEST))) |
| 819 | return svc_generic_init_request(rqstp, progp, ret); |
| 820 | |
| 821 | ret->mismatch.lovers = NFSD_NRVERS; |
| 822 | for (i = NFSD_MINVERS; i < NFSD_NRVERS; i++) { |
| 823 | if (nfsd_vers(nn, i, NFSD_TEST)) { |
| 824 | ret->mismatch.lovers = i; |
| 825 | break; |
| 826 | } |
| 827 | } |
| 828 | if (ret->mismatch.lovers == NFSD_NRVERS) |
| 829 | return rpc_prog_unavail; |
| 830 | ret->mismatch.hivers = NFSD_MINVERS; |
| 831 | for (i = NFSD_NRVERS - 1; i >= NFSD_MINVERS; i--) { |
| 832 | if (nfsd_vers(nn, i, NFSD_TEST)) { |
| 833 | ret->mismatch.hivers = i; |
| 834 | break; |
| 835 | } |
| 836 | } |
| 837 | return rpc_prog_mismatch; |
| 838 | } |
| 839 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | /* |
| 841 | * This is the NFS server kernel thread |
| 842 | */ |
Jeff Layton | 9867d76 | 2008-06-10 08:40:38 -0400 | [diff] [blame] | 843 | static int |
| 844 | nfsd(void *vrqstp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | { |
Jeff Layton | 9867d76 | 2008-06-10 08:40:38 -0400 | [diff] [blame] | 846 | struct svc_rqst *rqstp = (struct svc_rqst *) vrqstp; |
Stanislav Kinsbursky | 88c4766 | 2012-12-06 18:34:42 +0300 | [diff] [blame] | 847 | struct svc_xprt *perm_sock = list_entry(rqstp->rq_server->sv_permsocks.next, typeof(struct svc_xprt), xpt_list); |
| 848 | struct net *net = perm_sock->xpt_net; |
Jeff Layton | 5b8db00 | 2014-07-02 16:11:22 -0400 | [diff] [blame] | 849 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
J. Bruce Fields | 5b444cc | 2012-08-17 21:47:53 -0400 | [diff] [blame] | 850 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | |
| 852 | /* Lock module and set up kernel thread */ |
Neil Brown | bedbdd8 | 2008-06-10 08:40:35 -0400 | [diff] [blame] | 853 | mutex_lock(&nfsd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | |
Jeff Layton | 9867d76 | 2008-06-10 08:40:38 -0400 | [diff] [blame] | 855 | /* At this point, the thread shares current->fs |
Andreas Gruenbacher | 47057ab | 2016-01-12 20:24:14 +0100 | [diff] [blame] | 856 | * with the init process. We need to create files with the |
| 857 | * umask as defined by the client instead of init's umask. */ |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 858 | if (unshare_fs_struct() < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | printk("Unable to start nfsd thread: out of memory\n"); |
| 860 | goto out; |
| 861 | } |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 862 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | current->fs->umask = 0; |
| 864 | |
Jeff Layton | 9867d76 | 2008-06-10 08:40:38 -0400 | [diff] [blame] | 865 | /* |
| 866 | * thread is spawned with all signals set to SIG_IGN, re-enable |
Jeff Layton | 100766f | 2008-06-30 14:09:46 -0400 | [diff] [blame] | 867 | * the ones that will bring down the thread |
Jeff Layton | 9867d76 | 2008-06-10 08:40:38 -0400 | [diff] [blame] | 868 | */ |
Jeff Layton | 100766f | 2008-06-30 14:09:46 -0400 | [diff] [blame] | 869 | allow_signal(SIGKILL); |
| 870 | allow_signal(SIGHUP); |
| 871 | allow_signal(SIGINT); |
| 872 | allow_signal(SIGQUIT); |
Neil Brown | bedbdd8 | 2008-06-10 08:40:35 -0400 | [diff] [blame] | 873 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | nfsdstats.th_cnt++; |
Neil Brown | bedbdd8 | 2008-06-10 08:40:35 -0400 | [diff] [blame] | 875 | mutex_unlock(&nfsd_mutex); |
| 876 | |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 877 | set_freezable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | |
| 879 | /* |
| 880 | * The main request loop |
| 881 | */ |
| 882 | for (;;) { |
Jeff Layton | 5b8db00 | 2014-07-02 16:11:22 -0400 | [diff] [blame] | 883 | /* Update sv_maxconn if it has changed */ |
| 884 | rqstp->rq_server->sv_maxconn = nn->max_connections; |
| 885 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | /* |
| 887 | * Find a socket with data available and call its |
| 888 | * recvfrom routine. |
| 889 | */ |
NeilBrown | 6fb2b47 | 2006-10-02 02:17:50 -0700 | [diff] [blame] | 890 | while ((err = svc_recv(rqstp, 60*60*HZ)) == -EAGAIN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | ; |
Jeff Layton | 9867d76 | 2008-06-10 08:40:38 -0400 | [diff] [blame] | 892 | if (err == -EINTR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | break; |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 894 | validate_process_creds(); |
NeilBrown | 6fb2b47 | 2006-10-02 02:17:50 -0700 | [diff] [blame] | 895 | svc_process(rqstp); |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 896 | validate_process_creds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | } |
| 898 | |
NeilBrown | 24e3666 | 2006-10-02 02:17:45 -0700 | [diff] [blame] | 899 | /* Clear signals before calling svc_exit_thread() */ |
NeilBrown | 9e416052 | 2005-04-16 15:26:37 -0700 | [diff] [blame] | 900 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | |
Neil Brown | bedbdd8 | 2008-06-10 08:40:35 -0400 | [diff] [blame] | 902 | mutex_lock(&nfsd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | nfsdstats.th_cnt --; |
| 904 | |
| 905 | out: |
Stanislav Kinsbursky | 57c8b13e | 2012-07-03 16:46:41 +0400 | [diff] [blame] | 906 | rqstp->rq_server = NULL; |
Stanislav Kinsbursky | 786185b | 2012-05-04 12:49:41 +0400 | [diff] [blame] | 907 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | /* Release the thread */ |
| 909 | svc_exit_thread(rqstp); |
| 910 | |
Stanislav Kinsbursky | 88c4766 | 2012-12-06 18:34:42 +0300 | [diff] [blame] | 911 | nfsd_destroy(net); |
Stanislav Kinsbursky | 57c8b13e | 2012-07-03 16:46:41 +0400 | [diff] [blame] | 912 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | /* Release module */ |
Neil Brown | bedbdd8 | 2008-06-10 08:40:35 -0400 | [diff] [blame] | 914 | mutex_unlock(&nfsd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | module_put_and_exit(0); |
Jeff Layton | 9867d76 | 2008-06-10 08:40:38 -0400 | [diff] [blame] | 916 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | } |
| 918 | |
Andy Adamson | 32c1eb0 | 2007-07-17 04:04:48 -0700 | [diff] [blame] | 919 | static __be32 map_new_errors(u32 vers, __be32 nfserr) |
| 920 | { |
| 921 | if (nfserr == nfserr_jukebox && vers == 2) |
| 922 | return nfserr_dropit; |
| 923 | if (nfserr == nfserr_wrongsec && vers < 4) |
| 924 | return nfserr_acces; |
| 925 | return nfserr; |
| 926 | } |
| 927 | |
J. Bruce Fields | e6838a2 | 2017-04-21 16:10:18 -0400 | [diff] [blame] | 928 | /* |
| 929 | * A write procedure can have a large argument, and a read procedure can |
| 930 | * have a large reply, but no NFSv2 or NFSv3 procedure has argument and |
| 931 | * reply that can both be larger than a page. The xdr code has taken |
| 932 | * advantage of this assumption to be a sloppy about bounds checking in |
| 933 | * some cases. Pending a rewrite of the NFSv2/v3 xdr code to fix that |
| 934 | * problem, we enforce these assumptions here: |
| 935 | */ |
| 936 | static bool nfs_request_too_big(struct svc_rqst *rqstp, |
Christoph Hellwig | 860bda2 | 2017-05-12 16:11:49 +0200 | [diff] [blame] | 937 | const struct svc_procedure *proc) |
J. Bruce Fields | e6838a2 | 2017-04-21 16:10:18 -0400 | [diff] [blame] | 938 | { |
| 939 | /* |
| 940 | * The ACL code has more careful bounds-checking and is not |
| 941 | * susceptible to this problem: |
| 942 | */ |
| 943 | if (rqstp->rq_prog != NFS_PROGRAM) |
| 944 | return false; |
| 945 | /* |
| 946 | * Ditto NFSv4 (which can in theory have argument and reply both |
| 947 | * more than a page): |
| 948 | */ |
| 949 | if (rqstp->rq_vers >= 4) |
| 950 | return false; |
| 951 | /* The reply will be small, we're OK: */ |
| 952 | if (proc->pc_xdrressize > 0 && |
| 953 | proc->pc_xdrressize < XDR_QUADLEN(PAGE_SIZE)) |
| 954 | return false; |
| 955 | |
| 956 | return rqstp->rq_arg.len > PAGE_SIZE; |
| 957 | } |
| 958 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | int |
Al Viro | c7afef1 | 2006-10-19 23:29:02 -0700 | [diff] [blame] | 960 | nfsd_dispatch(struct svc_rqst *rqstp, __be32 *statp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | { |
Christoph Hellwig | 860bda2 | 2017-05-12 16:11:49 +0200 | [diff] [blame] | 962 | const struct svc_procedure *proc; |
Al Viro | ad451d3 | 2006-10-19 23:28:55 -0700 | [diff] [blame] | 963 | __be32 nfserr; |
| 964 | __be32 *nfserrp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | |
| 966 | dprintk("nfsd_dispatch: vers %d proc %d\n", |
| 967 | rqstp->rq_vers, rqstp->rq_proc); |
| 968 | proc = rqstp->rq_procinfo; |
| 969 | |
J. Bruce Fields | e6838a2 | 2017-04-21 16:10:18 -0400 | [diff] [blame] | 970 | if (nfs_request_too_big(rqstp, proc)) { |
| 971 | dprintk("nfsd: NFSv%d argument too large\n", rqstp->rq_vers); |
| 972 | *statp = rpc_garbage_args; |
| 973 | return 1; |
| 974 | } |
J. Bruce Fields | 1091006 | 2011-01-24 12:11:02 -0500 | [diff] [blame] | 975 | /* |
| 976 | * Give the xdr decoder a chance to change this if it wants |
| 977 | * (necessary in the NFSv4.0 compound case) |
| 978 | */ |
| 979 | rqstp->rq_cachetype = proc->pc_cachetype; |
| 980 | /* Decode arguments */ |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 981 | if (proc->pc_decode && |
| 982 | !proc->pc_decode(rqstp, (__be32*)rqstp->rq_arg.head[0].iov_base)) { |
J. Bruce Fields | 1091006 | 2011-01-24 12:11:02 -0500 | [diff] [blame] | 983 | dprintk("nfsd: failed to decode arguments!\n"); |
| 984 | *statp = rpc_garbage_args; |
| 985 | return 1; |
| 986 | } |
| 987 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | /* Check whether we have this call in the cache. */ |
J. Bruce Fields | 1091006 | 2011-01-24 12:11:02 -0500 | [diff] [blame] | 989 | switch (nfsd_cache_lookup(rqstp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | case RC_DROPIT: |
| 991 | return 0; |
| 992 | case RC_REPLY: |
| 993 | return 1; |
| 994 | case RC_DOIT:; |
| 995 | /* do it */ |
| 996 | } |
| 997 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | /* need to grab the location to store the status, as |
| 999 | * nfsv4 does some encoding while processing |
| 1000 | */ |
| 1001 | nfserrp = rqstp->rq_res.head[0].iov_base |
| 1002 | + rqstp->rq_res.head[0].iov_len; |
Al Viro | ad451d3 | 2006-10-19 23:28:55 -0700 | [diff] [blame] | 1003 | rqstp->rq_res.head[0].iov_len += sizeof(__be32); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | |
| 1005 | /* Now call the procedure handler, and encode NFS status. */ |
Christoph Hellwig | a6beb73 | 2017-05-08 17:35:49 +0200 | [diff] [blame] | 1006 | nfserr = proc->pc_func(rqstp); |
Andy Adamson | 32c1eb0 | 2007-07-17 04:04:48 -0700 | [diff] [blame] | 1007 | nfserr = map_new_errors(rqstp->rq_vers, nfserr); |
Jeff Layton | 78b65eb | 2014-11-19 07:51:17 -0500 | [diff] [blame] | 1008 | if (nfserr == nfserr_dropit || test_bit(RQ_DROPME, &rqstp->rq_flags)) { |
J. Bruce Fields | 45457e0 | 2007-06-22 17:26:32 -0400 | [diff] [blame] | 1009 | dprintk("nfsd: Dropping request; may be revisited later\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | nfsd_cache_update(rqstp, RC_NOCACHE, NULL); |
| 1011 | return 0; |
| 1012 | } |
| 1013 | |
| 1014 | if (rqstp->rq_proc != 0) |
| 1015 | *nfserrp++ = nfserr; |
| 1016 | |
| 1017 | /* Encode result. |
| 1018 | * For NFSv2, additional info is never returned in case of an error. |
| 1019 | */ |
| 1020 | if (!(nfserr && rqstp->rq_vers == 2)) { |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 1021 | if (proc->pc_encode && !proc->pc_encode(rqstp, nfserrp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | /* Failed to encode result. Release cache entry */ |
| 1023 | dprintk("nfsd: failed to encode result!\n"); |
| 1024 | nfsd_cache_update(rqstp, RC_NOCACHE, NULL); |
| 1025 | *statp = rpc_system_err; |
| 1026 | return 1; |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | /* Store reply in cache. */ |
J. Bruce Fields | 57d276d | 2012-11-16 15:22:43 -0500 | [diff] [blame] | 1031 | nfsd_cache_update(rqstp, rqstp->rq_cachetype, statp + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | return 1; |
| 1033 | } |
Greg Banks | 03cf6c9 | 2009-01-13 21:26:36 +1100 | [diff] [blame] | 1034 | |
| 1035 | int nfsd_pool_stats_open(struct inode *inode, struct file *file) |
| 1036 | { |
Ryusei Yamaguchi | ed2d8ae | 2009-08-16 00:54:41 +0900 | [diff] [blame] | 1037 | int ret; |
Stanislav Kinsbursky | 11f7794 | 2013-02-01 15:56:12 +0300 | [diff] [blame] | 1038 | struct nfsd_net *nn = net_generic(inode->i_sb->s_fs_info, nfsd_net_id); |
Stanislav Kinsbursky | 9dd9845 | 2012-12-06 14:23:24 +0300 | [diff] [blame] | 1039 | |
Ryusei Yamaguchi | ed2d8ae | 2009-08-16 00:54:41 +0900 | [diff] [blame] | 1040 | mutex_lock(&nfsd_mutex); |
Stanislav Kinsbursky | 9dd9845 | 2012-12-06 14:23:24 +0300 | [diff] [blame] | 1041 | if (nn->nfsd_serv == NULL) { |
Ryusei Yamaguchi | ed2d8ae | 2009-08-16 00:54:41 +0900 | [diff] [blame] | 1042 | mutex_unlock(&nfsd_mutex); |
Greg Banks | 03cf6c9 | 2009-01-13 21:26:36 +1100 | [diff] [blame] | 1043 | return -ENODEV; |
Ryusei Yamaguchi | ed2d8ae | 2009-08-16 00:54:41 +0900 | [diff] [blame] | 1044 | } |
| 1045 | /* bump up the psudo refcount while traversing */ |
Stanislav Kinsbursky | 9dd9845 | 2012-12-06 14:23:24 +0300 | [diff] [blame] | 1046 | svc_get(nn->nfsd_serv); |
| 1047 | ret = svc_pool_stats_open(nn->nfsd_serv, file); |
Ryusei Yamaguchi | ed2d8ae | 2009-08-16 00:54:41 +0900 | [diff] [blame] | 1048 | mutex_unlock(&nfsd_mutex); |
| 1049 | return ret; |
| 1050 | } |
| 1051 | |
| 1052 | int nfsd_pool_stats_release(struct inode *inode, struct file *file) |
| 1053 | { |
| 1054 | int ret = seq_release(inode, file); |
Stanislav Kinsbursky | 11f7794 | 2013-02-01 15:56:12 +0300 | [diff] [blame] | 1055 | struct net *net = inode->i_sb->s_fs_info; |
Stanislav Kinsbursky | 786185b | 2012-05-04 12:49:41 +0400 | [diff] [blame] | 1056 | |
Ryusei Yamaguchi | ed2d8ae | 2009-08-16 00:54:41 +0900 | [diff] [blame] | 1057 | mutex_lock(&nfsd_mutex); |
| 1058 | /* this function really, really should have been called svc_put() */ |
Stanislav Kinsbursky | 19f7e2c | 2012-07-03 16:46:41 +0400 | [diff] [blame] | 1059 | nfsd_destroy(net); |
Ryusei Yamaguchi | ed2d8ae | 2009-08-16 00:54:41 +0900 | [diff] [blame] | 1060 | mutex_unlock(&nfsd_mutex); |
| 1061 | return ret; |
Greg Banks | 03cf6c9 | 2009-01-13 21:26:36 +1100 | [diff] [blame] | 1062 | } |