nfssvc.c 13 KB

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