nfssvc.c 10 KB

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