nfssvc.c 14 KB

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