nfssvc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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. #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
  54. static struct svc_stat nfsd_acl_svcstats;
  55. static struct svc_version * nfsd_acl_version[] = {
  56. [2] = &nfsd_acl_version2,
  57. [3] = &nfsd_acl_version3,
  58. };
  59. #define NFSD_ACL_MINVERS 2
  60. #define NFSD_ACL_NRVERS ARRAY_SIZE(nfsd_acl_version)
  61. static struct svc_version *nfsd_acl_versions[NFSD_ACL_NRVERS];
  62. static struct svc_program nfsd_acl_program = {
  63. .pg_prog = NFS_ACL_PROGRAM,
  64. .pg_nvers = NFSD_ACL_NRVERS,
  65. .pg_vers = nfsd_acl_versions,
  66. .pg_name = "nfsd",
  67. .pg_class = "nfsd",
  68. .pg_stats = &nfsd_acl_svcstats,
  69. .pg_authenticate = &svc_set_client,
  70. };
  71. static struct svc_stat nfsd_acl_svcstats = {
  72. .program = &nfsd_acl_program,
  73. };
  74. #endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */
  75. static struct svc_version * nfsd_version[] = {
  76. [2] = &nfsd_version2,
  77. #if defined(CONFIG_NFSD_V3)
  78. [3] = &nfsd_version3,
  79. #endif
  80. #if defined(CONFIG_NFSD_V4)
  81. [4] = &nfsd_version4,
  82. #endif
  83. };
  84. #define NFSD_MINVERS 2
  85. #define NFSD_NRVERS ARRAY_SIZE(nfsd_version)
  86. static struct svc_version *nfsd_versions[NFSD_NRVERS];
  87. struct svc_program nfsd_program = {
  88. #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
  89. .pg_next = &nfsd_acl_program,
  90. #endif
  91. .pg_prog = NFS_PROGRAM, /* program number */
  92. .pg_nvers = NFSD_NRVERS, /* nr of entries in nfsd_version */
  93. .pg_vers = nfsd_versions, /* version table */
  94. .pg_name = "nfsd", /* program name */
  95. .pg_class = "nfsd", /* authentication class */
  96. .pg_stats = &nfsd_svcstats, /* version table */
  97. .pg_authenticate = &svc_set_client, /* export authentication */
  98. };
  99. int nfsd_vers(int vers, enum vers_op change)
  100. {
  101. if (vers < NFSD_MINVERS || vers >= NFSD_NRVERS)
  102. return -1;
  103. switch(change) {
  104. case NFSD_SET:
  105. nfsd_versions[vers] = nfsd_version[vers];
  106. break;
  107. #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
  108. if (vers < NFSD_ACL_NRVERS)
  109. nfsd_acl_version[vers] = nfsd_acl_version[vers];
  110. #endif
  111. case NFSD_CLEAR:
  112. nfsd_versions[vers] = NULL;
  113. #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
  114. if (vers < NFSD_ACL_NRVERS)
  115. nfsd_acl_version[vers] = NULL;
  116. #endif
  117. break;
  118. case NFSD_TEST:
  119. return nfsd_versions[vers] != NULL;
  120. case NFSD_AVAIL:
  121. return nfsd_version[vers] != NULL;
  122. }
  123. return 0;
  124. }
  125. /*
  126. * Maximum number of nfsd processes
  127. */
  128. #define NFSD_MAXSERVS 8192
  129. int nfsd_nrthreads(void)
  130. {
  131. if (nfsd_serv == NULL)
  132. return 0;
  133. else
  134. return nfsd_serv->sv_nrthreads;
  135. }
  136. static int killsig; /* signal that was used to kill last nfsd */
  137. static void nfsd_last_thread(struct svc_serv *serv)
  138. {
  139. /* When last nfsd thread exits we need to do some clean-up */
  140. struct svc_sock *svsk;
  141. list_for_each_entry(svsk, &serv->sv_permsocks, sk_list)
  142. lockd_down();
  143. nfsd_serv = NULL;
  144. nfsd_racache_shutdown();
  145. nfs4_state_shutdown();
  146. printk(KERN_WARNING "nfsd: last server has exited\n");
  147. if (killsig != SIG_NOCLEAN) {
  148. printk(KERN_WARNING "nfsd: unexporting all filesystems\n");
  149. nfsd_export_flush();
  150. }
  151. }
  152. void nfsd_reset_versions(void)
  153. {
  154. int found_one = 0;
  155. int i;
  156. for (i = NFSD_MINVERS; i < NFSD_NRVERS; i++) {
  157. if (nfsd_program.pg_vers[i])
  158. found_one = 1;
  159. }
  160. if (!found_one) {
  161. for (i = NFSD_MINVERS; i < NFSD_NRVERS; i++)
  162. nfsd_program.pg_vers[i] = nfsd_version[i];
  163. #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
  164. for (i = NFSD_ACL_MINVERS; i < NFSD_ACL_NRVERS; i++)
  165. nfsd_acl_program.pg_vers[i] =
  166. nfsd_acl_version[i];
  167. #endif
  168. }
  169. }
  170. int nfsd_create_serv(void)
  171. {
  172. int err = 0;
  173. lock_kernel();
  174. if (nfsd_serv) {
  175. svc_get(nfsd_serv);
  176. unlock_kernel();
  177. return 0;
  178. }
  179. atomic_set(&nfsd_busy, 0);
  180. nfsd_serv = svc_create_pooled(&nfsd_program, NFSD_BUFSIZE,
  181. nfsd_last_thread,
  182. nfsd, SIG_NOCLEAN, THIS_MODULE);
  183. if (nfsd_serv == NULL)
  184. err = -ENOMEM;
  185. unlock_kernel();
  186. do_gettimeofday(&nfssvc_boot); /* record boot time */
  187. return err;
  188. }
  189. static int nfsd_init_socks(int port)
  190. {
  191. int error;
  192. if (!list_empty(&nfsd_serv->sv_permsocks))
  193. return 0;
  194. error = lockd_up(IPPROTO_UDP);
  195. if (error >= 0) {
  196. error = svc_makesock(nfsd_serv, IPPROTO_UDP, port);
  197. if (error < 0)
  198. lockd_down();
  199. }
  200. if (error < 0)
  201. return error;
  202. #ifdef CONFIG_NFSD_TCP
  203. error = lockd_up(IPPROTO_TCP);
  204. if (error >= 0) {
  205. error = svc_makesock(nfsd_serv, IPPROTO_TCP, port);
  206. if (error < 0)
  207. lockd_down();
  208. }
  209. if (error < 0)
  210. return error;
  211. #endif
  212. return 0;
  213. }
  214. int nfsd_nrpools(void)
  215. {
  216. if (nfsd_serv == NULL)
  217. return 0;
  218. else
  219. return nfsd_serv->sv_nrpools;
  220. }
  221. int nfsd_get_nrthreads(int n, int *nthreads)
  222. {
  223. int i = 0;
  224. if (nfsd_serv != NULL) {
  225. for (i = 0; i < nfsd_serv->sv_nrpools && i < n; i++)
  226. nthreads[i] = nfsd_serv->sv_pools[i].sp_nrthreads;
  227. }
  228. return 0;
  229. }
  230. int nfsd_set_nrthreads(int n, int *nthreads)
  231. {
  232. int i = 0;
  233. int tot = 0;
  234. int err = 0;
  235. if (nfsd_serv == NULL || n <= 0)
  236. return 0;
  237. if (n > nfsd_serv->sv_nrpools)
  238. n = nfsd_serv->sv_nrpools;
  239. /* enforce a global maximum number of threads */
  240. tot = 0;
  241. for (i = 0; i < n; i++) {
  242. if (nthreads[i] > NFSD_MAXSERVS)
  243. nthreads[i] = NFSD_MAXSERVS;
  244. tot += nthreads[i];
  245. }
  246. if (tot > NFSD_MAXSERVS) {
  247. /* total too large: scale down requested numbers */
  248. for (i = 0; i < n && tot > 0; i++) {
  249. int new = nthreads[i] * NFSD_MAXSERVS / tot;
  250. tot -= (nthreads[i] - new);
  251. nthreads[i] = new;
  252. }
  253. for (i = 0; i < n && tot > 0; i++) {
  254. nthreads[i]--;
  255. tot--;
  256. }
  257. }
  258. /*
  259. * There must always be a thread in pool 0; the admin
  260. * can't shut down NFS completely using pool_threads.
  261. */
  262. if (nthreads[0] == 0)
  263. nthreads[0] = 1;
  264. /* apply the new numbers */
  265. lock_kernel();
  266. svc_get(nfsd_serv);
  267. for (i = 0; i < n; i++) {
  268. err = svc_set_num_threads(nfsd_serv, &nfsd_serv->sv_pools[i],
  269. nthreads[i]);
  270. if (err)
  271. break;
  272. }
  273. svc_destroy(nfsd_serv);
  274. unlock_kernel();
  275. return err;
  276. }
  277. int
  278. nfsd_svc(unsigned short port, int nrservs)
  279. {
  280. int error;
  281. lock_kernel();
  282. dprintk("nfsd: creating service\n");
  283. error = -EINVAL;
  284. if (nrservs <= 0)
  285. nrservs = 0;
  286. if (nrservs > NFSD_MAXSERVS)
  287. nrservs = NFSD_MAXSERVS;
  288. /* Readahead param cache - will no-op if it already exists */
  289. error = nfsd_racache_init(2*nrservs);
  290. if (error<0)
  291. goto out;
  292. error = nfs4_state_start();
  293. if (error<0)
  294. goto out;
  295. nfsd_reset_versions();
  296. error = nfsd_create_serv();
  297. if (error)
  298. goto out;
  299. error = nfsd_init_socks(port);
  300. if (error)
  301. goto failure;
  302. error = svc_set_num_threads(nfsd_serv, NULL, nrservs);
  303. failure:
  304. svc_destroy(nfsd_serv); /* Release server */
  305. out:
  306. unlock_kernel();
  307. return error;
  308. }
  309. static inline void
  310. update_thread_usage(int busy_threads)
  311. {
  312. unsigned long prev_call;
  313. unsigned long diff;
  314. int decile;
  315. spin_lock(&nfsd_call_lock);
  316. prev_call = nfsd_last_call;
  317. nfsd_last_call = jiffies;
  318. decile = busy_threads*10/nfsdstats.th_cnt;
  319. if (decile>0 && decile <= 10) {
  320. diff = nfsd_last_call - prev_call;
  321. if ( (nfsdstats.th_usage[decile-1] += diff) >= NFSD_USAGE_WRAP)
  322. nfsdstats.th_usage[decile-1] -= NFSD_USAGE_WRAP;
  323. if (decile == 10)
  324. nfsdstats.th_fullcnt++;
  325. }
  326. spin_unlock(&nfsd_call_lock);
  327. }
  328. /*
  329. * This is the NFS server kernel thread
  330. */
  331. static void
  332. nfsd(struct svc_rqst *rqstp)
  333. {
  334. struct fs_struct *fsp;
  335. int err;
  336. sigset_t shutdown_mask, allowed_mask;
  337. /* Lock module and set up kernel thread */
  338. lock_kernel();
  339. daemonize("nfsd");
  340. /* After daemonize() this kernel thread shares current->fs
  341. * with the init process. We need to create files with a
  342. * umask of 0 instead of init's umask. */
  343. fsp = copy_fs_struct(current->fs);
  344. if (!fsp) {
  345. printk("Unable to start nfsd thread: out of memory\n");
  346. goto out;
  347. }
  348. exit_fs(current);
  349. current->fs = fsp;
  350. current->fs->umask = 0;
  351. siginitsetinv(&shutdown_mask, SHUTDOWN_SIGS);
  352. siginitsetinv(&allowed_mask, ALLOWED_SIGS);
  353. nfsdstats.th_cnt++;
  354. rqstp->rq_task = current;
  355. unlock_kernel();
  356. /*
  357. * We want less throttling in balance_dirty_pages() so that nfs to
  358. * localhost doesn't cause nfsd to lock up due to all the client's
  359. * dirty pages.
  360. */
  361. current->flags |= PF_LESS_THROTTLE;
  362. /*
  363. * The main request loop
  364. */
  365. for (;;) {
  366. /* Block all but the shutdown signals */
  367. sigprocmask(SIG_SETMASK, &shutdown_mask, NULL);
  368. /*
  369. * Find a socket with data available and call its
  370. * recvfrom routine.
  371. */
  372. while ((err = svc_recv(rqstp, 60*60*HZ)) == -EAGAIN)
  373. ;
  374. if (err < 0)
  375. break;
  376. update_thread_usage(atomic_read(&nfsd_busy));
  377. atomic_inc(&nfsd_busy);
  378. /* Lock the export hash tables for reading. */
  379. exp_readlock();
  380. /* Process request with signals blocked. */
  381. sigprocmask(SIG_SETMASK, &allowed_mask, NULL);
  382. svc_process(rqstp);
  383. /* Unlock export hash tables */
  384. exp_readunlock();
  385. update_thread_usage(atomic_read(&nfsd_busy));
  386. atomic_dec(&nfsd_busy);
  387. }
  388. if (err != -EINTR) {
  389. printk(KERN_WARNING "nfsd: terminating on error %d\n", -err);
  390. } else {
  391. unsigned int signo;
  392. for (signo = 1; signo <= _NSIG; signo++)
  393. if (sigismember(&current->pending.signal, signo) &&
  394. !sigismember(&current->blocked, signo))
  395. break;
  396. killsig = signo;
  397. }
  398. /* Clear signals before calling svc_exit_thread() */
  399. flush_signals(current);
  400. lock_kernel();
  401. nfsdstats.th_cnt --;
  402. out:
  403. /* Release the thread */
  404. svc_exit_thread(rqstp);
  405. /* Release module */
  406. unlock_kernel();
  407. module_put_and_exit(0);
  408. }
  409. int
  410. nfsd_dispatch(struct svc_rqst *rqstp, u32 *statp)
  411. {
  412. struct svc_procedure *proc;
  413. kxdrproc_t xdr;
  414. u32 nfserr;
  415. u32 *nfserrp;
  416. dprintk("nfsd_dispatch: vers %d proc %d\n",
  417. rqstp->rq_vers, rqstp->rq_proc);
  418. proc = rqstp->rq_procinfo;
  419. /* Check whether we have this call in the cache. */
  420. switch (nfsd_cache_lookup(rqstp, proc->pc_cachetype)) {
  421. case RC_INTR:
  422. case RC_DROPIT:
  423. return 0;
  424. case RC_REPLY:
  425. return 1;
  426. case RC_DOIT:;
  427. /* do it */
  428. }
  429. /* Decode arguments */
  430. xdr = proc->pc_decode;
  431. if (xdr && !xdr(rqstp, (u32*)rqstp->rq_arg.head[0].iov_base,
  432. rqstp->rq_argp)) {
  433. dprintk("nfsd: failed to decode arguments!\n");
  434. nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
  435. *statp = rpc_garbage_args;
  436. return 1;
  437. }
  438. /* need to grab the location to store the status, as
  439. * nfsv4 does some encoding while processing
  440. */
  441. nfserrp = rqstp->rq_res.head[0].iov_base
  442. + rqstp->rq_res.head[0].iov_len;
  443. rqstp->rq_res.head[0].iov_len += sizeof(u32);
  444. /* Now call the procedure handler, and encode NFS status. */
  445. nfserr = proc->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp);
  446. if (nfserr == nfserr_jukebox && rqstp->rq_vers == 2)
  447. nfserr = nfserr_dropit;
  448. if (nfserr == nfserr_dropit) {
  449. dprintk("nfsd: Dropping request due to malloc failure!\n");
  450. nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
  451. return 0;
  452. }
  453. if (rqstp->rq_proc != 0)
  454. *nfserrp++ = nfserr;
  455. /* Encode result.
  456. * For NFSv2, additional info is never returned in case of an error.
  457. */
  458. if (!(nfserr && rqstp->rq_vers == 2)) {
  459. xdr = proc->pc_encode;
  460. if (xdr && !xdr(rqstp, nfserrp,
  461. rqstp->rq_resp)) {
  462. /* Failed to encode result. Release cache entry */
  463. dprintk("nfsd: failed to encode result!\n");
  464. nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
  465. *statp = rpc_system_err;
  466. return 1;
  467. }
  468. }
  469. /* Store reply in cache. */
  470. nfsd_cache_update(rqstp, proc->pc_cachetype, statp + 1);
  471. return 1;
  472. }