nfssvc.c 11 KB

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