nfssvc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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/time.h>
  12. #include <linux/errno.h>
  13. #include <linux/nfs.h>
  14. #include <linux/in.h>
  15. #include <linux/uio.h>
  16. #include <linux/unistd.h>
  17. #include <linux/slab.h>
  18. #include <linux/smp.h>
  19. #include <linux/smp_lock.h>
  20. #include <linux/fs_struct.h>
  21. #include <linux/sunrpc/types.h>
  22. #include <linux/sunrpc/stats.h>
  23. #include <linux/sunrpc/svc.h>
  24. #include <linux/sunrpc/svcsock.h>
  25. #include <linux/sunrpc/cache.h>
  26. #include <linux/nfsd/nfsd.h>
  27. #include <linux/nfsd/stats.h>
  28. #include <linux/nfsd/cache.h>
  29. #include <linux/nfsd/syscall.h>
  30. #include <linux/lockd/bind.h>
  31. #include <linux/nfsacl.h>
  32. #define NFSDDBG_FACILITY NFSDDBG_SVC
  33. /* these signals will be delivered to an nfsd thread
  34. * when handling a request
  35. */
  36. #define ALLOWED_SIGS (sigmask(SIGKILL))
  37. /* these signals will be delivered to an nfsd thread
  38. * when not handling a request. i.e. when waiting
  39. */
  40. #define SHUTDOWN_SIGS (sigmask(SIGKILL) | sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT))
  41. /* if the last thread dies with SIGHUP, then the exports table is
  42. * left unchanged ( like 2.4-{0-9} ). Any other signal will clear
  43. * the exports table (like 2.2).
  44. */
  45. #define SIG_NOCLEAN SIGHUP
  46. extern struct svc_program nfsd_program;
  47. static void nfsd(struct svc_rqst *rqstp);
  48. struct timeval nfssvc_boot;
  49. struct svc_serv *nfsd_serv;
  50. static atomic_t nfsd_busy;
  51. static unsigned long nfsd_last_call;
  52. static DEFINE_SPINLOCK(nfsd_call_lock);
  53. struct nfsd_list {
  54. struct list_head list;
  55. struct task_struct *task;
  56. };
  57. static struct list_head nfsd_list = LIST_HEAD_INIT(nfsd_list);
  58. #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
  59. static struct svc_stat nfsd_acl_svcstats;
  60. static struct svc_version * nfsd_acl_version[] = {
  61. [2] = &nfsd_acl_version2,
  62. [3] = &nfsd_acl_version3,
  63. };
  64. #define NFSD_ACL_MINVERS 2
  65. #define NFSD_ACL_NRVERS ARRAY_SIZE(nfsd_acl_version)
  66. static struct svc_version *nfsd_acl_versions[NFSD_ACL_NRVERS];
  67. static struct svc_program nfsd_acl_program = {
  68. .pg_prog = NFS_ACL_PROGRAM,
  69. .pg_nvers = NFSD_ACL_NRVERS,
  70. .pg_vers = nfsd_acl_versions,
  71. .pg_name = "nfsd",
  72. .pg_class = "nfsd",
  73. .pg_stats = &nfsd_acl_svcstats,
  74. .pg_authenticate = &svc_set_client,
  75. };
  76. static struct svc_stat nfsd_acl_svcstats = {
  77. .program = &nfsd_acl_program,
  78. };
  79. #endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */
  80. static struct svc_version * nfsd_version[] = {
  81. [2] = &nfsd_version2,
  82. #if defined(CONFIG_NFSD_V3)
  83. [3] = &nfsd_version3,
  84. #endif
  85. #if defined(CONFIG_NFSD_V4)
  86. [4] = &nfsd_version4,
  87. #endif
  88. };
  89. #define NFSD_MINVERS 2
  90. #define NFSD_NRVERS ARRAY_SIZE(nfsd_version)
  91. static struct svc_version *nfsd_versions[NFSD_NRVERS];
  92. struct svc_program nfsd_program = {
  93. #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
  94. .pg_next = &nfsd_acl_program,
  95. #endif
  96. .pg_prog = NFS_PROGRAM, /* program number */
  97. .pg_nvers = NFSD_NRVERS, /* nr of entries in nfsd_version */
  98. .pg_vers = nfsd_versions, /* version table */
  99. .pg_name = "nfsd", /* program name */
  100. .pg_class = "nfsd", /* authentication class */
  101. .pg_stats = &nfsd_svcstats, /* version table */
  102. .pg_authenticate = &svc_set_client, /* export authentication */
  103. };
  104. int nfsd_vers(int vers, enum vers_op change)
  105. {
  106. if (vers < NFSD_MINVERS || vers >= NFSD_NRVERS)
  107. return -1;
  108. switch(change) {
  109. case NFSD_SET:
  110. nfsd_versions[vers] = nfsd_version[vers];
  111. break;
  112. #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
  113. if (vers < NFSD_ACL_NRVERS)
  114. nfsd_acl_version[vers] = nfsd_acl_version[vers];
  115. #endif
  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_version[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. /*
  131. * Maximum number of nfsd processes
  132. */
  133. #define NFSD_MAXSERVS 8192
  134. int nfsd_nrthreads(void)
  135. {
  136. if (nfsd_serv == NULL)
  137. return 0;
  138. else
  139. return nfsd_serv->sv_nrthreads;
  140. }
  141. static int killsig; /* signal that was used to kill last nfsd */
  142. static void nfsd_last_thread(struct svc_serv *serv)
  143. {
  144. /* When last nfsd thread exits we need to do some clean-up */
  145. struct svc_sock *svsk;
  146. list_for_each_entry(svsk, &serv->sv_permsocks, sk_list)
  147. lockd_down();
  148. nfsd_serv = NULL;
  149. nfsd_racache_shutdown();
  150. nfs4_state_shutdown();
  151. printk(KERN_WARNING "nfsd: last server has exited\n");
  152. if (killsig != SIG_NOCLEAN) {
  153. printk(KERN_WARNING "nfsd: unexporting all filesystems\n");
  154. nfsd_export_flush();
  155. }
  156. }
  157. void nfsd_reset_versions(void)
  158. {
  159. int found_one = 0;
  160. int i;
  161. for (i = NFSD_MINVERS; i < NFSD_NRVERS; i++) {
  162. if (nfsd_program.pg_vers[i])
  163. found_one = 1;
  164. }
  165. if (!found_one) {
  166. for (i = NFSD_MINVERS; i < NFSD_NRVERS; i++)
  167. nfsd_program.pg_vers[i] = nfsd_version[i];
  168. #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
  169. for (i = NFSD_ACL_MINVERS; i < NFSD_ACL_NRVERS; i++)
  170. nfsd_acl_program.pg_vers[i] =
  171. nfsd_acl_version[i];
  172. #endif
  173. }
  174. }
  175. static int nfsd_create_serv(void)
  176. {
  177. int err = 0;
  178. lock_kernel();
  179. if (nfsd_serv) {
  180. nfsd_serv->sv_nrthreads++;
  181. unlock_kernel();
  182. return 0;
  183. }
  184. atomic_set(&nfsd_busy, 0);
  185. nfsd_serv = svc_create(&nfsd_program, NFSD_BUFSIZE,
  186. nfsd_last_thread);
  187. if (nfsd_serv == NULL)
  188. err = -ENOMEM;
  189. else
  190. nfsd_serv->sv_nrthreads++;
  191. unlock_kernel();
  192. do_gettimeofday(&nfssvc_boot); /* record boot time */
  193. return err;
  194. }
  195. static int nfsd_init_socks(int port)
  196. {
  197. int error;
  198. if (!list_empty(&nfsd_serv->sv_permsocks))
  199. return 0;
  200. error = svc_makesock(nfsd_serv, IPPROTO_UDP, port);
  201. if (error < 0)
  202. return error;
  203. error = lockd_up(IPPROTO_UDP);
  204. if (error < 0)
  205. return error;
  206. #ifdef CONFIG_NFSD_TCP
  207. error = svc_makesock(nfsd_serv, IPPROTO_TCP, port);
  208. if (error < 0)
  209. return error;
  210. error = lockd_up(IPPROTO_TCP);
  211. if (error < 0)
  212. return error;
  213. #endif
  214. return 0;
  215. }
  216. int
  217. nfsd_svc(unsigned short port, int nrservs)
  218. {
  219. int error;
  220. struct list_head *victim;
  221. lock_kernel();
  222. dprintk("nfsd: creating service\n");
  223. error = -EINVAL;
  224. if (nrservs <= 0)
  225. nrservs = 0;
  226. if (nrservs > NFSD_MAXSERVS)
  227. nrservs = NFSD_MAXSERVS;
  228. /* Readahead param cache - will no-op if it already exists */
  229. error = nfsd_racache_init(2*nrservs);
  230. if (error<0)
  231. goto out;
  232. error = nfs4_state_start();
  233. if (error<0)
  234. goto out;
  235. nfsd_reset_versions();
  236. error = nfsd_create_serv();
  237. if (error)
  238. goto out;
  239. error = nfsd_init_socks(port);
  240. if (error)
  241. goto failure;
  242. nrservs -= (nfsd_serv->sv_nrthreads-1);
  243. while (nrservs > 0) {
  244. nrservs--;
  245. __module_get(THIS_MODULE);
  246. error = svc_create_thread(nfsd, nfsd_serv);
  247. if (error < 0) {
  248. module_put(THIS_MODULE);
  249. break;
  250. }
  251. }
  252. victim = nfsd_list.next;
  253. while (nrservs < 0 && victim != &nfsd_list) {
  254. struct nfsd_list *nl =
  255. list_entry(victim,struct nfsd_list, list);
  256. victim = victim->next;
  257. send_sig(SIG_NOCLEAN, nl->task, 1);
  258. nrservs++;
  259. }
  260. failure:
  261. svc_destroy(nfsd_serv); /* Release server */
  262. out:
  263. unlock_kernel();
  264. return error;
  265. }
  266. static inline void
  267. update_thread_usage(int busy_threads)
  268. {
  269. unsigned long prev_call;
  270. unsigned long diff;
  271. int decile;
  272. spin_lock(&nfsd_call_lock);
  273. prev_call = nfsd_last_call;
  274. nfsd_last_call = jiffies;
  275. decile = busy_threads*10/nfsdstats.th_cnt;
  276. if (decile>0 && decile <= 10) {
  277. diff = nfsd_last_call - prev_call;
  278. if ( (nfsdstats.th_usage[decile-1] += diff) >= NFSD_USAGE_WRAP)
  279. nfsdstats.th_usage[decile-1] -= NFSD_USAGE_WRAP;
  280. if (decile == 10)
  281. nfsdstats.th_fullcnt++;
  282. }
  283. spin_unlock(&nfsd_call_lock);
  284. }
  285. /*
  286. * This is the NFS server kernel thread
  287. */
  288. static void
  289. nfsd(struct svc_rqst *rqstp)
  290. {
  291. struct svc_serv *serv = rqstp->rq_server;
  292. struct fs_struct *fsp;
  293. int err;
  294. struct nfsd_list me;
  295. sigset_t shutdown_mask, allowed_mask;
  296. /* Lock module and set up kernel thread */
  297. lock_kernel();
  298. daemonize("nfsd");
  299. /* After daemonize() this kernel thread shares current->fs
  300. * with the init process. We need to create files with a
  301. * umask of 0 instead of init's umask. */
  302. fsp = copy_fs_struct(current->fs);
  303. if (!fsp) {
  304. printk("Unable to start nfsd thread: out of memory\n");
  305. goto out;
  306. }
  307. exit_fs(current);
  308. current->fs = fsp;
  309. current->fs->umask = 0;
  310. siginitsetinv(&shutdown_mask, SHUTDOWN_SIGS);
  311. siginitsetinv(&allowed_mask, ALLOWED_SIGS);
  312. nfsdstats.th_cnt++;
  313. me.task = current;
  314. list_add(&me.list, &nfsd_list);
  315. unlock_kernel();
  316. /*
  317. * We want less throttling in balance_dirty_pages() so that nfs to
  318. * localhost doesn't cause nfsd to lock up due to all the client's
  319. * dirty pages.
  320. */
  321. current->flags |= PF_LESS_THROTTLE;
  322. /*
  323. * The main request loop
  324. */
  325. for (;;) {
  326. /* Block all but the shutdown signals */
  327. sigprocmask(SIG_SETMASK, &shutdown_mask, NULL);
  328. /*
  329. * Find a socket with data available and call its
  330. * recvfrom routine.
  331. */
  332. while ((err = svc_recv(serv, rqstp,
  333. 60*60*HZ)) == -EAGAIN)
  334. ;
  335. if (err < 0)
  336. break;
  337. update_thread_usage(atomic_read(&nfsd_busy));
  338. atomic_inc(&nfsd_busy);
  339. /* Lock the export hash tables for reading. */
  340. exp_readlock();
  341. /* Process request with signals blocked. */
  342. sigprocmask(SIG_SETMASK, &allowed_mask, NULL);
  343. svc_process(serv, rqstp);
  344. /* Unlock export hash tables */
  345. exp_readunlock();
  346. update_thread_usage(atomic_read(&nfsd_busy));
  347. atomic_dec(&nfsd_busy);
  348. }
  349. if (err != -EINTR) {
  350. printk(KERN_WARNING "nfsd: terminating on error %d\n", -err);
  351. } else {
  352. unsigned int signo;
  353. for (signo = 1; signo <= _NSIG; signo++)
  354. if (sigismember(&current->pending.signal, signo) &&
  355. !sigismember(&current->blocked, signo))
  356. break;
  357. killsig = signo;
  358. }
  359. /* Clear signals before calling svc_exit_thread() */
  360. flush_signals(current);
  361. lock_kernel();
  362. list_del(&me.list);
  363. nfsdstats.th_cnt --;
  364. out:
  365. /* Release the thread */
  366. svc_exit_thread(rqstp);
  367. /* Release module */
  368. unlock_kernel();
  369. module_put_and_exit(0);
  370. }
  371. int
  372. nfsd_dispatch(struct svc_rqst *rqstp, u32 *statp)
  373. {
  374. struct svc_procedure *proc;
  375. kxdrproc_t xdr;
  376. u32 nfserr;
  377. u32 *nfserrp;
  378. dprintk("nfsd_dispatch: vers %d proc %d\n",
  379. rqstp->rq_vers, rqstp->rq_proc);
  380. proc = rqstp->rq_procinfo;
  381. /* Check whether we have this call in the cache. */
  382. switch (nfsd_cache_lookup(rqstp, proc->pc_cachetype)) {
  383. case RC_INTR:
  384. case RC_DROPIT:
  385. return 0;
  386. case RC_REPLY:
  387. return 1;
  388. case RC_DOIT:;
  389. /* do it */
  390. }
  391. /* Decode arguments */
  392. xdr = proc->pc_decode;
  393. if (xdr && !xdr(rqstp, (u32*)rqstp->rq_arg.head[0].iov_base,
  394. rqstp->rq_argp)) {
  395. dprintk("nfsd: failed to decode arguments!\n");
  396. nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
  397. *statp = rpc_garbage_args;
  398. return 1;
  399. }
  400. /* need to grab the location to store the status, as
  401. * nfsv4 does some encoding while processing
  402. */
  403. nfserrp = rqstp->rq_res.head[0].iov_base
  404. + rqstp->rq_res.head[0].iov_len;
  405. rqstp->rq_res.head[0].iov_len += sizeof(u32);
  406. /* Now call the procedure handler, and encode NFS status. */
  407. nfserr = proc->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp);
  408. if (nfserr == nfserr_jukebox && rqstp->rq_vers == 2)
  409. nfserr = nfserr_dropit;
  410. if (nfserr == nfserr_dropit) {
  411. dprintk("nfsd: Dropping request due to malloc failure!\n");
  412. nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
  413. return 0;
  414. }
  415. if (rqstp->rq_proc != 0)
  416. *nfserrp++ = nfserr;
  417. /* Encode result.
  418. * For NFSv2, additional info is never returned in case of an error.
  419. */
  420. if (!(nfserr && rqstp->rq_vers == 2)) {
  421. xdr = proc->pc_encode;
  422. if (xdr && !xdr(rqstp, nfserrp,
  423. rqstp->rq_resp)) {
  424. /* Failed to encode result. Release cache entry */
  425. dprintk("nfsd: failed to encode result!\n");
  426. nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
  427. *statp = rpc_system_err;
  428. return 1;
  429. }
  430. }
  431. /* Store reply in cache. */
  432. nfsd_cache_update(rqstp, proc->pc_cachetype, statp + 1);
  433. return 1;
  434. }