nfssvc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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/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. int nfsd_vers(int vers, enum vers_op change)
  110. {
  111. if (vers < NFSD_MINVERS || vers >= NFSD_NRVERS)
  112. return -1;
  113. switch(change) {
  114. case NFSD_SET:
  115. nfsd_versions[vers] = nfsd_version[vers];
  116. #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
  117. if (vers < NFSD_ACL_NRVERS)
  118. nfsd_acl_versions[vers] = nfsd_acl_version[vers];
  119. #endif
  120. break;
  121. case NFSD_CLEAR:
  122. nfsd_versions[vers] = NULL;
  123. #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
  124. if (vers < NFSD_ACL_NRVERS)
  125. nfsd_acl_versions[vers] = NULL;
  126. #endif
  127. break;
  128. case NFSD_TEST:
  129. return nfsd_versions[vers] != NULL;
  130. case NFSD_AVAIL:
  131. return nfsd_version[vers] != NULL;
  132. }
  133. return 0;
  134. }
  135. /*
  136. * Maximum number of nfsd processes
  137. */
  138. #define NFSD_MAXSERVS 8192
  139. int nfsd_nrthreads(void)
  140. {
  141. int rv = 0;
  142. mutex_lock(&nfsd_mutex);
  143. if (nfsd_serv)
  144. rv = nfsd_serv->sv_nrthreads;
  145. mutex_unlock(&nfsd_mutex);
  146. return rv;
  147. }
  148. static void nfsd_last_thread(struct svc_serv *serv)
  149. {
  150. /* When last nfsd thread exits we need to do some clean-up */
  151. struct svc_xprt *xprt;
  152. list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list)
  153. lockd_down();
  154. nfsd_serv = NULL;
  155. nfsd_racache_shutdown();
  156. nfs4_state_shutdown();
  157. printk(KERN_WARNING "nfsd: last server has exited, flushing export "
  158. "cache\n");
  159. nfsd_export_flush();
  160. }
  161. void nfsd_reset_versions(void)
  162. {
  163. int found_one = 0;
  164. int i;
  165. for (i = NFSD_MINVERS; i < NFSD_NRVERS; i++) {
  166. if (nfsd_program.pg_vers[i])
  167. found_one = 1;
  168. }
  169. if (!found_one) {
  170. for (i = NFSD_MINVERS; i < NFSD_NRVERS; i++)
  171. nfsd_program.pg_vers[i] = nfsd_version[i];
  172. #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
  173. for (i = NFSD_ACL_MINVERS; i < NFSD_ACL_NRVERS; i++)
  174. nfsd_acl_program.pg_vers[i] =
  175. nfsd_acl_version[i];
  176. #endif
  177. }
  178. }
  179. int nfsd_create_serv(void)
  180. {
  181. int err = 0;
  182. WARN_ON(!mutex_is_locked(&nfsd_mutex));
  183. if (nfsd_serv) {
  184. svc_get(nfsd_serv);
  185. return 0;
  186. }
  187. if (nfsd_max_blksize == 0) {
  188. /* choose a suitable default */
  189. struct sysinfo i;
  190. si_meminfo(&i);
  191. /* Aim for 1/4096 of memory per thread
  192. * This gives 1MB on 4Gig machines
  193. * But only uses 32K on 128M machines.
  194. * Bottom out at 8K on 32M and smaller.
  195. * Of course, this is only a default.
  196. */
  197. nfsd_max_blksize = NFSSVC_MAXBLKSIZE;
  198. i.totalram <<= PAGE_SHIFT - 12;
  199. while (nfsd_max_blksize > i.totalram &&
  200. nfsd_max_blksize >= 8*1024*2)
  201. nfsd_max_blksize /= 2;
  202. }
  203. nfsd_serv = svc_create_pooled(&nfsd_program, nfsd_max_blksize,
  204. AF_INET,
  205. nfsd_last_thread, nfsd, THIS_MODULE);
  206. if (nfsd_serv == NULL)
  207. err = -ENOMEM;
  208. do_gettimeofday(&nfssvc_boot); /* record boot time */
  209. return err;
  210. }
  211. static int nfsd_init_socks(int port)
  212. {
  213. int error;
  214. if (!list_empty(&nfsd_serv->sv_permsocks))
  215. return 0;
  216. error = svc_create_xprt(nfsd_serv, "udp", port,
  217. SVC_SOCK_DEFAULTS);
  218. if (error < 0)
  219. return error;
  220. error = lockd_up();
  221. if (error < 0)
  222. return error;
  223. error = svc_create_xprt(nfsd_serv, "tcp", port,
  224. SVC_SOCK_DEFAULTS);
  225. if (error < 0)
  226. return error;
  227. error = lockd_up();
  228. if (error < 0)
  229. return error;
  230. return 0;
  231. }
  232. int nfsd_nrpools(void)
  233. {
  234. if (nfsd_serv == NULL)
  235. return 0;
  236. else
  237. return nfsd_serv->sv_nrpools;
  238. }
  239. int nfsd_get_nrthreads(int n, int *nthreads)
  240. {
  241. int i = 0;
  242. if (nfsd_serv != NULL) {
  243. for (i = 0; i < nfsd_serv->sv_nrpools && i < n; i++)
  244. nthreads[i] = nfsd_serv->sv_pools[i].sp_nrthreads;
  245. }
  246. return 0;
  247. }
  248. int nfsd_set_nrthreads(int n, int *nthreads)
  249. {
  250. int i = 0;
  251. int tot = 0;
  252. int err = 0;
  253. WARN_ON(!mutex_is_locked(&nfsd_mutex));
  254. if (nfsd_serv == NULL || n <= 0)
  255. return 0;
  256. if (n > nfsd_serv->sv_nrpools)
  257. n = nfsd_serv->sv_nrpools;
  258. /* enforce a global maximum number of threads */
  259. tot = 0;
  260. for (i = 0; i < n; i++) {
  261. if (nthreads[i] > NFSD_MAXSERVS)
  262. nthreads[i] = NFSD_MAXSERVS;
  263. tot += nthreads[i];
  264. }
  265. if (tot > NFSD_MAXSERVS) {
  266. /* total too large: scale down requested numbers */
  267. for (i = 0; i < n && tot > 0; i++) {
  268. int new = nthreads[i] * NFSD_MAXSERVS / tot;
  269. tot -= (nthreads[i] - new);
  270. nthreads[i] = new;
  271. }
  272. for (i = 0; i < n && tot > 0; i++) {
  273. nthreads[i]--;
  274. tot--;
  275. }
  276. }
  277. /*
  278. * There must always be a thread in pool 0; the admin
  279. * can't shut down NFS completely using pool_threads.
  280. */
  281. if (nthreads[0] == 0)
  282. nthreads[0] = 1;
  283. /* apply the new numbers */
  284. svc_get(nfsd_serv);
  285. for (i = 0; i < n; i++) {
  286. err = svc_set_num_threads(nfsd_serv, &nfsd_serv->sv_pools[i],
  287. nthreads[i]);
  288. if (err)
  289. break;
  290. }
  291. svc_destroy(nfsd_serv);
  292. return err;
  293. }
  294. int
  295. nfsd_svc(unsigned short port, int nrservs)
  296. {
  297. int error;
  298. mutex_lock(&nfsd_mutex);
  299. dprintk("nfsd: creating service\n");
  300. error = -EINVAL;
  301. if (nrservs <= 0)
  302. nrservs = 0;
  303. if (nrservs > NFSD_MAXSERVS)
  304. nrservs = NFSD_MAXSERVS;
  305. /* Readahead param cache - will no-op if it already exists */
  306. error = nfsd_racache_init(2*nrservs);
  307. if (error<0)
  308. goto out;
  309. nfs4_state_start();
  310. nfsd_reset_versions();
  311. error = nfsd_create_serv();
  312. if (error)
  313. goto out;
  314. error = nfsd_init_socks(port);
  315. if (error)
  316. goto failure;
  317. error = svc_set_num_threads(nfsd_serv, NULL, nrservs);
  318. failure:
  319. svc_destroy(nfsd_serv); /* Release server */
  320. out:
  321. mutex_unlock(&nfsd_mutex);
  322. return error;
  323. }
  324. /*
  325. * This is the NFS server kernel thread
  326. */
  327. static int
  328. nfsd(void *vrqstp)
  329. {
  330. struct svc_rqst *rqstp = (struct svc_rqst *) vrqstp;
  331. struct fs_struct *fsp;
  332. int err, preverr = 0;
  333. /* Lock module and set up kernel thread */
  334. mutex_lock(&nfsd_mutex);
  335. /* At this point, the thread shares current->fs
  336. * with the init process. We need to create files with a
  337. * umask of 0 instead of init's umask. */
  338. fsp = copy_fs_struct(current->fs);
  339. if (!fsp) {
  340. printk("Unable to start nfsd thread: out of memory\n");
  341. goto out;
  342. }
  343. exit_fs(current);
  344. current->fs = fsp;
  345. current->fs->umask = 0;
  346. /*
  347. * thread is spawned with all signals set to SIG_IGN, re-enable
  348. * the ones that will bring down the thread
  349. */
  350. allow_signal(SIGKILL);
  351. allow_signal(SIGHUP);
  352. allow_signal(SIGINT);
  353. allow_signal(SIGQUIT);
  354. nfsdstats.th_cnt++;
  355. mutex_unlock(&nfsd_mutex);
  356. /*
  357. * We want less throttling in balance_dirty_pages() so that nfs to
  358. * localhost doesn't cause nfsd to lock up due to all the client's
  359. * dirty pages.
  360. */
  361. current->flags |= PF_LESS_THROTTLE;
  362. set_freezable();
  363. /*
  364. * The main request loop
  365. */
  366. for (;;) {
  367. /*
  368. * Find a socket with data available and call its
  369. * recvfrom routine.
  370. */
  371. while ((err = svc_recv(rqstp, 60*60*HZ)) == -EAGAIN)
  372. ;
  373. if (err == -EINTR)
  374. break;
  375. else if (err < 0) {
  376. if (err != preverr) {
  377. printk(KERN_WARNING "%s: unexpected error "
  378. "from svc_recv (%d)\n", __func__, -err);
  379. preverr = err;
  380. }
  381. schedule_timeout_uninterruptible(HZ);
  382. continue;
  383. }
  384. /* Lock the export hash tables for reading. */
  385. exp_readlock();
  386. svc_process(rqstp);
  387. /* Unlock export hash tables */
  388. exp_readunlock();
  389. }
  390. /* Clear signals before calling svc_exit_thread() */
  391. flush_signals(current);
  392. mutex_lock(&nfsd_mutex);
  393. nfsdstats.th_cnt --;
  394. out:
  395. /* Release the thread */
  396. svc_exit_thread(rqstp);
  397. /* Release module */
  398. mutex_unlock(&nfsd_mutex);
  399. module_put_and_exit(0);
  400. return 0;
  401. }
  402. static __be32 map_new_errors(u32 vers, __be32 nfserr)
  403. {
  404. if (nfserr == nfserr_jukebox && vers == 2)
  405. return nfserr_dropit;
  406. if (nfserr == nfserr_wrongsec && vers < 4)
  407. return nfserr_acces;
  408. return nfserr;
  409. }
  410. int
  411. nfsd_dispatch(struct svc_rqst *rqstp, __be32 *statp)
  412. {
  413. struct svc_procedure *proc;
  414. kxdrproc_t xdr;
  415. __be32 nfserr;
  416. __be32 *nfserrp;
  417. dprintk("nfsd_dispatch: vers %d proc %d\n",
  418. rqstp->rq_vers, rqstp->rq_proc);
  419. proc = rqstp->rq_procinfo;
  420. /* Check whether we have this call in the cache. */
  421. switch (nfsd_cache_lookup(rqstp, proc->pc_cachetype)) {
  422. case RC_INTR:
  423. case RC_DROPIT:
  424. return 0;
  425. case RC_REPLY:
  426. return 1;
  427. case RC_DOIT:;
  428. /* do it */
  429. }
  430. /* Decode arguments */
  431. xdr = proc->pc_decode;
  432. if (xdr && !xdr(rqstp, (__be32*)rqstp->rq_arg.head[0].iov_base,
  433. rqstp->rq_argp)) {
  434. dprintk("nfsd: failed to decode arguments!\n");
  435. nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
  436. *statp = rpc_garbage_args;
  437. return 1;
  438. }
  439. /* need to grab the location to store the status, as
  440. * nfsv4 does some encoding while processing
  441. */
  442. nfserrp = rqstp->rq_res.head[0].iov_base
  443. + rqstp->rq_res.head[0].iov_len;
  444. rqstp->rq_res.head[0].iov_len += sizeof(__be32);
  445. /* Now call the procedure handler, and encode NFS status. */
  446. nfserr = proc->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp);
  447. nfserr = map_new_errors(rqstp->rq_vers, nfserr);
  448. if (nfserr == nfserr_dropit) {
  449. dprintk("nfsd: Dropping request; may be revisited later\n");
  450. nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
  451. return 0;
  452. }
  453. if (rqstp->rq_proc != 0)
  454. *nfserrp++ = nfserr;
  455. /* Encode result.
  456. * For NFSv2, additional info is never returned in case of an error.
  457. */
  458. if (!(nfserr && rqstp->rq_vers == 2)) {
  459. xdr = proc->pc_encode;
  460. if (xdr && !xdr(rqstp, nfserrp,
  461. rqstp->rq_resp)) {
  462. /* Failed to encode result. Release cache entry */
  463. dprintk("nfsd: failed to encode result!\n");
  464. nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
  465. *statp = rpc_system_err;
  466. return 1;
  467. }
  468. }
  469. /* Store reply in cache. */
  470. nfsd_cache_update(rqstp, proc->pc_cachetype, statp + 1);
  471. return 1;
  472. }