nfssvc.c 11 KB

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