nfssvc.c 13 KB

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