nfssvc.c 9.2 KB

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