Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/nfsd/nfssvc.c |
| 3 | * |
| 4 | * Central processing for nfsd. |
| 5 | * |
| 6 | * Authors: Olaf Kirch (okir@monad.swb.de) |
| 7 | * |
| 8 | * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de> |
| 9 | */ |
| 10 | |
| 11 | #include <linux/config.h> |
| 12 | #include <linux/module.h> |
| 13 | |
| 14 | #include <linux/time.h> |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/nfs.h> |
| 17 | #include <linux/in.h> |
| 18 | #include <linux/uio.h> |
| 19 | #include <linux/unistd.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/smp.h> |
| 22 | #include <linux/smp_lock.h> |
| 23 | #include <linux/fs_struct.h> |
| 24 | |
| 25 | #include <linux/sunrpc/types.h> |
| 26 | #include <linux/sunrpc/stats.h> |
| 27 | #include <linux/sunrpc/svc.h> |
| 28 | #include <linux/sunrpc/svcsock.h> |
| 29 | #include <linux/sunrpc/cache.h> |
| 30 | #include <linux/nfsd/nfsd.h> |
| 31 | #include <linux/nfsd/stats.h> |
| 32 | #include <linux/nfsd/cache.h> |
NeilBrown | 70c3b76 | 2005-11-07 01:00:25 -0800 | [diff] [blame^] | 33 | #include <linux/nfsd/syscall.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <linux/lockd/bind.h> |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 35 | #include <linux/nfsacl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | #define NFSDDBG_FACILITY NFSDDBG_SVC |
| 38 | |
| 39 | /* these signals will be delivered to an nfsd thread |
| 40 | * when handling a request |
| 41 | */ |
| 42 | #define ALLOWED_SIGS (sigmask(SIGKILL)) |
| 43 | /* these signals will be delivered to an nfsd thread |
| 44 | * when not handling a request. i.e. when waiting |
| 45 | */ |
| 46 | #define SHUTDOWN_SIGS (sigmask(SIGKILL) | sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT)) |
| 47 | /* if the last thread dies with SIGHUP, then the exports table is |
| 48 | * left unchanged ( like 2.4-{0-9} ). Any other signal will clear |
| 49 | * the exports table (like 2.2). |
| 50 | */ |
| 51 | #define SIG_NOCLEAN SIGHUP |
| 52 | |
| 53 | extern struct svc_program nfsd_program; |
| 54 | static void nfsd(struct svc_rqst *rqstp); |
| 55 | struct timeval nfssvc_boot; |
NeilBrown | 70c3b76 | 2005-11-07 01:00:25 -0800 | [diff] [blame^] | 56 | struct svc_serv *nfsd_serv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | static atomic_t nfsd_busy; |
| 58 | static unsigned long nfsd_last_call; |
| 59 | static DEFINE_SPINLOCK(nfsd_call_lock); |
| 60 | |
| 61 | struct nfsd_list { |
| 62 | struct list_head list; |
| 63 | struct task_struct *task; |
| 64 | }; |
| 65 | static struct list_head nfsd_list = LIST_HEAD_INIT(nfsd_list); |
| 66 | |
NeilBrown | 70c3b76 | 2005-11-07 01:00:25 -0800 | [diff] [blame^] | 67 | static struct svc_version * nfsd_version[] = { |
| 68 | [2] = &nfsd_version2, |
| 69 | #if defined(CONFIG_NFSD_V3) |
| 70 | [3] = &nfsd_version3, |
| 71 | #endif |
| 72 | #if defined(CONFIG_NFSD_V4) |
| 73 | [4] = &nfsd_version4, |
| 74 | #endif |
| 75 | }; |
| 76 | |
| 77 | #define NFSD_MINVERS 2 |
| 78 | #define NFSD_NRVERS (sizeof(nfsd_version)/sizeof(nfsd_version[0])) |
| 79 | static struct svc_version *nfsd_versions[NFSD_NRVERS]; |
| 80 | |
| 81 | struct svc_program nfsd_program = { |
| 82 | .pg_prog = NFS_PROGRAM, /* program number */ |
| 83 | .pg_nvers = NFSD_NRVERS, /* nr of entries in nfsd_version */ |
| 84 | .pg_vers = nfsd_versions, /* version table */ |
| 85 | .pg_name = "nfsd", /* program name */ |
| 86 | .pg_class = "nfsd", /* authentication class */ |
| 87 | .pg_stats = &nfsd_svcstats, /* version table */ |
| 88 | .pg_authenticate = &svc_set_client, /* export authentication */ |
| 89 | |
| 90 | }; |
| 91 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | /* |
| 93 | * Maximum number of nfsd processes |
| 94 | */ |
| 95 | #define NFSD_MAXSERVS 8192 |
| 96 | |
| 97 | int nfsd_nrthreads(void) |
| 98 | { |
| 99 | if (nfsd_serv == NULL) |
| 100 | return 0; |
| 101 | else |
| 102 | return nfsd_serv->sv_nrthreads; |
| 103 | } |
| 104 | |
| 105 | int |
| 106 | nfsd_svc(unsigned short port, int nrservs) |
| 107 | { |
| 108 | int error; |
NeilBrown | 70c3b76 | 2005-11-07 01:00:25 -0800 | [diff] [blame^] | 109 | int none_left, found_one, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | struct list_head *victim; |
| 111 | |
| 112 | lock_kernel(); |
NeilBrown | 70c3b76 | 2005-11-07 01:00:25 -0800 | [diff] [blame^] | 113 | dprintk("nfsd: creating service: vers 0x%x\n", |
| 114 | nfsd_versbits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | error = -EINVAL; |
| 116 | if (nrservs <= 0) |
| 117 | nrservs = 0; |
| 118 | if (nrservs > NFSD_MAXSERVS) |
| 119 | nrservs = NFSD_MAXSERVS; |
| 120 | |
| 121 | /* Readahead param cache - will no-op if it already exists */ |
| 122 | error = nfsd_racache_init(2*nrservs); |
| 123 | if (error<0) |
| 124 | goto out; |
NeilBrown | 76a3550 | 2005-06-23 22:03:26 -0700 | [diff] [blame] | 125 | error = nfs4_state_start(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | if (error<0) |
| 127 | goto out; |
| 128 | if (!nfsd_serv) { |
NeilBrown | 70c3b76 | 2005-11-07 01:00:25 -0800 | [diff] [blame^] | 129 | /* |
| 130 | * Use the nfsd_ctlbits to define which |
| 131 | * versions that will be advertised. |
| 132 | * If nfsd_ctlbits doesn't list any version, |
| 133 | * export them all. |
| 134 | */ |
| 135 | found_one = 0; |
| 136 | |
| 137 | for (i = NFSD_MINVERS; i < NFSD_NRVERS; i++) { |
| 138 | if (NFSCTL_VERISSET(nfsd_versbits, i)) { |
| 139 | nfsd_program.pg_vers[i] = nfsd_version[i]; |
| 140 | found_one = 1; |
| 141 | } else |
| 142 | nfsd_program.pg_vers[i] = NULL; |
| 143 | } |
| 144 | |
| 145 | if (!found_one) { |
| 146 | for (i = NFSD_MINVERS; i < NFSD_NRVERS; i++) |
| 147 | nfsd_program.pg_vers[i] = nfsd_version[i]; |
| 148 | } |
| 149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | atomic_set(&nfsd_busy, 0); |
| 151 | error = -ENOMEM; |
| 152 | nfsd_serv = svc_create(&nfsd_program, NFSD_BUFSIZE); |
| 153 | if (nfsd_serv == NULL) |
| 154 | goto out; |
| 155 | error = svc_makesock(nfsd_serv, IPPROTO_UDP, port); |
| 156 | if (error < 0) |
| 157 | goto failure; |
| 158 | |
| 159 | #ifdef CONFIG_NFSD_TCP |
| 160 | error = svc_makesock(nfsd_serv, IPPROTO_TCP, port); |
| 161 | if (error < 0) |
| 162 | goto failure; |
| 163 | #endif |
| 164 | do_gettimeofday(&nfssvc_boot); /* record boot time */ |
| 165 | } else |
| 166 | nfsd_serv->sv_nrthreads++; |
| 167 | nrservs -= (nfsd_serv->sv_nrthreads-1); |
| 168 | while (nrservs > 0) { |
| 169 | nrservs--; |
| 170 | __module_get(THIS_MODULE); |
| 171 | error = svc_create_thread(nfsd, nfsd_serv); |
| 172 | if (error < 0) { |
| 173 | module_put(THIS_MODULE); |
| 174 | break; |
| 175 | } |
| 176 | } |
| 177 | victim = nfsd_list.next; |
| 178 | while (nrservs < 0 && victim != &nfsd_list) { |
| 179 | struct nfsd_list *nl = |
| 180 | list_entry(victim,struct nfsd_list, list); |
| 181 | victim = victim->next; |
| 182 | send_sig(SIG_NOCLEAN, nl->task, 1); |
| 183 | nrservs++; |
| 184 | } |
| 185 | failure: |
| 186 | none_left = (nfsd_serv->sv_nrthreads == 1); |
| 187 | svc_destroy(nfsd_serv); /* Release server */ |
| 188 | if (none_left) { |
| 189 | nfsd_serv = NULL; |
| 190 | nfsd_racache_shutdown(); |
| 191 | nfs4_state_shutdown(); |
| 192 | } |
| 193 | out: |
| 194 | unlock_kernel(); |
| 195 | return error; |
| 196 | } |
| 197 | |
| 198 | static inline void |
| 199 | update_thread_usage(int busy_threads) |
| 200 | { |
| 201 | unsigned long prev_call; |
| 202 | unsigned long diff; |
| 203 | int decile; |
| 204 | |
| 205 | spin_lock(&nfsd_call_lock); |
| 206 | prev_call = nfsd_last_call; |
| 207 | nfsd_last_call = jiffies; |
| 208 | decile = busy_threads*10/nfsdstats.th_cnt; |
| 209 | if (decile>0 && decile <= 10) { |
| 210 | diff = nfsd_last_call - prev_call; |
| 211 | if ( (nfsdstats.th_usage[decile-1] += diff) >= NFSD_USAGE_WRAP) |
| 212 | nfsdstats.th_usage[decile-1] -= NFSD_USAGE_WRAP; |
| 213 | if (decile == 10) |
| 214 | nfsdstats.th_fullcnt++; |
| 215 | } |
| 216 | spin_unlock(&nfsd_call_lock); |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * This is the NFS server kernel thread |
| 221 | */ |
| 222 | static void |
| 223 | nfsd(struct svc_rqst *rqstp) |
| 224 | { |
| 225 | struct svc_serv *serv = rqstp->rq_server; |
| 226 | struct fs_struct *fsp; |
| 227 | int err; |
| 228 | struct nfsd_list me; |
| 229 | sigset_t shutdown_mask, allowed_mask; |
| 230 | |
| 231 | /* Lock module and set up kernel thread */ |
| 232 | lock_kernel(); |
| 233 | daemonize("nfsd"); |
| 234 | |
| 235 | /* After daemonize() this kernel thread shares current->fs |
| 236 | * with the init process. We need to create files with a |
| 237 | * umask of 0 instead of init's umask. */ |
| 238 | fsp = copy_fs_struct(current->fs); |
| 239 | if (!fsp) { |
| 240 | printk("Unable to start nfsd thread: out of memory\n"); |
| 241 | goto out; |
| 242 | } |
| 243 | exit_fs(current); |
| 244 | current->fs = fsp; |
| 245 | current->fs->umask = 0; |
| 246 | |
| 247 | siginitsetinv(&shutdown_mask, SHUTDOWN_SIGS); |
| 248 | siginitsetinv(&allowed_mask, ALLOWED_SIGS); |
| 249 | |
| 250 | nfsdstats.th_cnt++; |
| 251 | |
| 252 | lockd_up(); /* start lockd */ |
| 253 | |
| 254 | me.task = current; |
| 255 | list_add(&me.list, &nfsd_list); |
| 256 | |
| 257 | unlock_kernel(); |
| 258 | |
| 259 | /* |
| 260 | * We want less throttling in balance_dirty_pages() so that nfs to |
| 261 | * localhost doesn't cause nfsd to lock up due to all the client's |
| 262 | * dirty pages. |
| 263 | */ |
| 264 | current->flags |= PF_LESS_THROTTLE; |
| 265 | |
| 266 | /* |
| 267 | * The main request loop |
| 268 | */ |
| 269 | for (;;) { |
| 270 | /* Block all but the shutdown signals */ |
| 271 | sigprocmask(SIG_SETMASK, &shutdown_mask, NULL); |
| 272 | |
| 273 | /* |
| 274 | * Find a socket with data available and call its |
| 275 | * recvfrom routine. |
| 276 | */ |
| 277 | while ((err = svc_recv(serv, rqstp, |
| 278 | 60*60*HZ)) == -EAGAIN) |
| 279 | ; |
| 280 | if (err < 0) |
| 281 | break; |
| 282 | update_thread_usage(atomic_read(&nfsd_busy)); |
| 283 | atomic_inc(&nfsd_busy); |
| 284 | |
| 285 | /* Lock the export hash tables for reading. */ |
| 286 | exp_readlock(); |
| 287 | |
| 288 | /* Process request with signals blocked. */ |
| 289 | sigprocmask(SIG_SETMASK, &allowed_mask, NULL); |
| 290 | |
| 291 | svc_process(serv, rqstp); |
| 292 | |
| 293 | /* Unlock export hash tables */ |
| 294 | exp_readunlock(); |
| 295 | update_thread_usage(atomic_read(&nfsd_busy)); |
| 296 | atomic_dec(&nfsd_busy); |
| 297 | } |
| 298 | |
| 299 | if (err != -EINTR) { |
| 300 | printk(KERN_WARNING "nfsd: terminating on error %d\n", -err); |
| 301 | } else { |
| 302 | unsigned int signo; |
| 303 | |
| 304 | for (signo = 1; signo <= _NSIG; signo++) |
| 305 | if (sigismember(¤t->pending.signal, signo) && |
| 306 | !sigismember(¤t->blocked, signo)) |
| 307 | break; |
| 308 | err = signo; |
| 309 | } |
NeilBrown | 9e416052 | 2005-04-16 15:26:37 -0700 | [diff] [blame] | 310 | /* Clear signals before calling lockd_down() and svc_exit_thread() */ |
| 311 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | |
| 313 | lock_kernel(); |
| 314 | |
| 315 | /* Release lockd */ |
| 316 | lockd_down(); |
| 317 | |
| 318 | /* Check if this is last thread */ |
| 319 | if (serv->sv_nrthreads==1) { |
| 320 | |
| 321 | printk(KERN_WARNING "nfsd: last server has exited\n"); |
| 322 | if (err != SIG_NOCLEAN) { |
| 323 | printk(KERN_WARNING "nfsd: unexporting all filesystems\n"); |
| 324 | nfsd_export_flush(); |
| 325 | } |
| 326 | nfsd_serv = NULL; |
| 327 | nfsd_racache_shutdown(); /* release read-ahead cache */ |
| 328 | nfs4_state_shutdown(); |
| 329 | } |
| 330 | list_del(&me.list); |
| 331 | nfsdstats.th_cnt --; |
| 332 | |
| 333 | out: |
| 334 | /* Release the thread */ |
| 335 | svc_exit_thread(rqstp); |
| 336 | |
| 337 | /* Release module */ |
Steven Rostedt | c4f92db | 2005-08-17 14:25:23 -0400 | [diff] [blame] | 338 | unlock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | module_put_and_exit(0); |
| 340 | } |
| 341 | |
| 342 | int |
| 343 | nfsd_dispatch(struct svc_rqst *rqstp, u32 *statp) |
| 344 | { |
| 345 | struct svc_procedure *proc; |
| 346 | kxdrproc_t xdr; |
| 347 | u32 nfserr; |
| 348 | u32 *nfserrp; |
| 349 | |
| 350 | dprintk("nfsd_dispatch: vers %d proc %d\n", |
| 351 | rqstp->rq_vers, rqstp->rq_proc); |
| 352 | proc = rqstp->rq_procinfo; |
| 353 | |
| 354 | /* Check whether we have this call in the cache. */ |
| 355 | switch (nfsd_cache_lookup(rqstp, proc->pc_cachetype)) { |
| 356 | case RC_INTR: |
| 357 | case RC_DROPIT: |
| 358 | return 0; |
| 359 | case RC_REPLY: |
| 360 | return 1; |
| 361 | case RC_DOIT:; |
| 362 | /* do it */ |
| 363 | } |
| 364 | |
| 365 | /* Decode arguments */ |
| 366 | xdr = proc->pc_decode; |
| 367 | if (xdr && !xdr(rqstp, (u32*)rqstp->rq_arg.head[0].iov_base, |
| 368 | rqstp->rq_argp)) { |
| 369 | dprintk("nfsd: failed to decode arguments!\n"); |
| 370 | nfsd_cache_update(rqstp, RC_NOCACHE, NULL); |
| 371 | *statp = rpc_garbage_args; |
| 372 | return 1; |
| 373 | } |
| 374 | |
| 375 | /* need to grab the location to store the status, as |
| 376 | * nfsv4 does some encoding while processing |
| 377 | */ |
| 378 | nfserrp = rqstp->rq_res.head[0].iov_base |
| 379 | + rqstp->rq_res.head[0].iov_len; |
| 380 | rqstp->rq_res.head[0].iov_len += sizeof(u32); |
| 381 | |
| 382 | /* Now call the procedure handler, and encode NFS status. */ |
| 383 | nfserr = proc->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp); |
| 384 | if (nfserr == nfserr_jukebox && rqstp->rq_vers == 2) |
| 385 | nfserr = nfserr_dropit; |
| 386 | if (nfserr == nfserr_dropit) { |
| 387 | dprintk("nfsd: Dropping request due to malloc failure!\n"); |
| 388 | nfsd_cache_update(rqstp, RC_NOCACHE, NULL); |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | if (rqstp->rq_proc != 0) |
| 393 | *nfserrp++ = nfserr; |
| 394 | |
| 395 | /* Encode result. |
| 396 | * For NFSv2, additional info is never returned in case of an error. |
| 397 | */ |
| 398 | if (!(nfserr && rqstp->rq_vers == 2)) { |
| 399 | xdr = proc->pc_encode; |
| 400 | if (xdr && !xdr(rqstp, nfserrp, |
| 401 | rqstp->rq_resp)) { |
| 402 | /* Failed to encode result. Release cache entry */ |
| 403 | dprintk("nfsd: failed to encode result!\n"); |
| 404 | nfsd_cache_update(rqstp, RC_NOCACHE, NULL); |
| 405 | *statp = rpc_system_err; |
| 406 | return 1; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | /* Store reply in cache. */ |
| 411 | nfsd_cache_update(rqstp, proc->pc_cachetype, statp + 1); |
| 412 | return 1; |
| 413 | } |
| 414 | |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 415 | #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) |
| 416 | static struct svc_stat nfsd_acl_svcstats; |
| 417 | static struct svc_version * nfsd_acl_version[] = { |
| 418 | [2] = &nfsd_acl_version2, |
| 419 | [3] = &nfsd_acl_version3, |
| 420 | }; |
| 421 | |
| 422 | #define NFSD_ACL_NRVERS (sizeof(nfsd_acl_version)/sizeof(nfsd_acl_version[0])) |
| 423 | static struct svc_program nfsd_acl_program = { |
| 424 | .pg_prog = NFS_ACL_PROGRAM, |
| 425 | .pg_nvers = NFSD_ACL_NRVERS, |
| 426 | .pg_vers = nfsd_acl_version, |
| 427 | .pg_name = "nfsd", |
Andreas Gruenbacher | 2134842 | 2005-06-22 17:16:28 +0000 | [diff] [blame] | 428 | .pg_class = "nfsd", |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 429 | .pg_stats = &nfsd_acl_svcstats, |
| 430 | }; |
| 431 | |
| 432 | static struct svc_stat nfsd_acl_svcstats = { |
| 433 | .program = &nfsd_acl_program, |
| 434 | }; |
| 435 | |
| 436 | #define nfsd_acl_program_p &nfsd_acl_program |
| 437 | #else |
| 438 | #define nfsd_acl_program_p NULL |
| 439 | #endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */ |