nfssvc.c 15 KB

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