nfssvc.c 14 KB

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