nfssvc.c 14 KB

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