nfssvc.c 11 KB

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