nfssvc.c 13 KB

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