nfssvc.c 14 KB

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