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