nfssvc.c 15 KB

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