nfssvc.c 11 KB

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