nfssvc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Central processing for nfsd.
  4. *
  5. * Authors: Olaf Kirch (okir@monad.swb.de)
  6. *
  7. * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de>
  8. */
  9. #include <linux/sched/signal.h>
  10. #include <linux/freezer.h>
  11. #include <linux/module.h>
  12. #include <linux/fs_struct.h>
  13. #include <linux/swap.h>
  14. #include <linux/sunrpc/stats.h>
  15. #include <linux/sunrpc/svcsock.h>
  16. #include <linux/sunrpc/svc_xprt.h>
  17. #include <linux/lockd/bind.h>
  18. #include <linux/nfsacl.h>
  19. #include <linux/seq_file.h>
  20. #include <linux/inetdevice.h>
  21. #include <net/addrconf.h>
  22. #include <net/ipv6.h>
  23. #include <net/net_namespace.h>
  24. #include "nfsd.h"
  25. #include "cache.h"
  26. #include "vfs.h"
  27. #include "netns.h"
  28. #define NFSDDBG_FACILITY NFSDDBG_SVC
  29. extern struct svc_program nfsd_program;
  30. static int nfsd(void *vrqstp);
  31. /*
  32. * nfsd_mutex protects nn->nfsd_serv -- both the pointer itself and the members
  33. * of the svc_serv struct. In particular, ->sv_nrthreads but also to some
  34. * extent ->sv_temp_socks and ->sv_permsocks. It also protects nfsdstats.th_cnt
  35. *
  36. * If (out side the lock) nn->nfsd_serv is non-NULL, then it must point to a
  37. * properly initialised 'struct svc_serv' with ->sv_nrthreads > 0. That number
  38. * of nfsd threads must exist and each must listed in ->sp_all_threads in each
  39. * entry of ->sv_pools[].
  40. *
  41. * Transitions of the thread count between zero and non-zero are of particular
  42. * interest since the svc_serv needs to be created and initialized at that
  43. * point, or freed.
  44. *
  45. * Finally, the nfsd_mutex also protects some of the global variables that are
  46. * accessed when nfsd starts and that are settable via the write_* routines in
  47. * nfsctl.c. In particular:
  48. *
  49. * user_recovery_dirname
  50. * user_lease_time
  51. * nfsd_versions
  52. */
  53. DEFINE_MUTEX(nfsd_mutex);
  54. /*
  55. * nfsd_drc_lock protects nfsd_drc_max_pages and nfsd_drc_pages_used.
  56. * nfsd_drc_max_pages limits the total amount of memory available for
  57. * version 4.1 DRC caches.
  58. * nfsd_drc_pages_used tracks the current version 4.1 DRC memory usage.
  59. */
  60. spinlock_t nfsd_drc_lock;
  61. unsigned long nfsd_drc_max_mem;
  62. unsigned long nfsd_drc_mem_used;
  63. #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
  64. static struct svc_stat nfsd_acl_svcstats;
  65. static const struct svc_version *nfsd_acl_version[] = {
  66. [2] = &nfsd_acl_version2,
  67. [3] = &nfsd_acl_version3,
  68. };
  69. #define NFSD_ACL_MINVERS 2
  70. #define NFSD_ACL_NRVERS ARRAY_SIZE(nfsd_acl_version)
  71. static const struct svc_version *nfsd_acl_versions[NFSD_ACL_NRVERS];
  72. static struct svc_program nfsd_acl_program = {
  73. .pg_prog = NFS_ACL_PROGRAM,
  74. .pg_nvers = NFSD_ACL_NRVERS,
  75. .pg_vers = nfsd_acl_versions,
  76. .pg_name = "nfsacl",
  77. .pg_class = "nfsd",
  78. .pg_stats = &nfsd_acl_svcstats,
  79. .pg_authenticate = &svc_set_client,
  80. };
  81. static struct svc_stat nfsd_acl_svcstats = {
  82. .program = &nfsd_acl_program,
  83. };
  84. #endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */
  85. static const struct svc_version *nfsd_version[] = {
  86. [2] = &nfsd_version2,
  87. #if defined(CONFIG_NFSD_V3)
  88. [3] = &nfsd_version3,
  89. #endif
  90. #if defined(CONFIG_NFSD_V4)
  91. [4] = &nfsd_version4,
  92. #endif
  93. };
  94. #define NFSD_MINVERS 2
  95. #define NFSD_NRVERS ARRAY_SIZE(nfsd_version)
  96. static const struct svc_version *nfsd_versions[NFSD_NRVERS];
  97. struct svc_program nfsd_program = {
  98. #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
  99. .pg_next = &nfsd_acl_program,
  100. #endif
  101. .pg_prog = NFS_PROGRAM, /* program number */
  102. .pg_nvers = NFSD_NRVERS, /* nr of entries in nfsd_version */
  103. .pg_vers = nfsd_versions, /* version table */
  104. .pg_name = "nfsd", /* program name */
  105. .pg_class = "nfsd", /* authentication class */
  106. .pg_stats = &nfsd_svcstats, /* version table */
  107. .pg_authenticate = &svc_set_client, /* export authentication */
  108. };
  109. static bool nfsd_supported_minorversions[NFSD_SUPPORTED_MINOR_VERSION + 1] = {
  110. [0] = 1,
  111. [1] = 1,
  112. [2] = 1,
  113. };
  114. int nfsd_vers(int vers, enum vers_op change)
  115. {
  116. if (vers < NFSD_MINVERS || vers >= NFSD_NRVERS)
  117. return 0;
  118. switch(change) {
  119. case NFSD_SET:
  120. nfsd_versions[vers] = nfsd_version[vers];
  121. #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
  122. if (vers < NFSD_ACL_NRVERS)
  123. nfsd_acl_versions[vers] = nfsd_acl_version[vers];
  124. #endif
  125. break;
  126. case NFSD_CLEAR:
  127. nfsd_versions[vers] = NULL;
  128. #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
  129. if (vers < NFSD_ACL_NRVERS)
  130. nfsd_acl_versions[vers] = NULL;
  131. #endif
  132. break;
  133. case NFSD_TEST:
  134. return nfsd_versions[vers] != NULL;
  135. case NFSD_AVAIL:
  136. return nfsd_version[vers] != NULL;
  137. }
  138. return 0;
  139. }
  140. static void
  141. nfsd_adjust_nfsd_versions4(void)
  142. {
  143. unsigned i;
  144. for (i = 0; i <= NFSD_SUPPORTED_MINOR_VERSION; i++) {
  145. if (nfsd_supported_minorversions[i])
  146. return;
  147. }
  148. nfsd_vers(4, NFSD_CLEAR);
  149. }
  150. int nfsd_minorversion(u32 minorversion, enum vers_op change)
  151. {
  152. if (minorversion > NFSD_SUPPORTED_MINOR_VERSION &&
  153. change != NFSD_AVAIL)
  154. return -1;
  155. switch(change) {
  156. case NFSD_SET:
  157. nfsd_supported_minorversions[minorversion] = true;
  158. nfsd_vers(4, NFSD_SET);
  159. break;
  160. case NFSD_CLEAR:
  161. nfsd_supported_minorversions[minorversion] = false;
  162. nfsd_adjust_nfsd_versions4();
  163. break;
  164. case NFSD_TEST:
  165. return nfsd_supported_minorversions[minorversion];
  166. case NFSD_AVAIL:
  167. return minorversion <= NFSD_SUPPORTED_MINOR_VERSION;
  168. }
  169. return 0;
  170. }
  171. /*
  172. * Maximum number of nfsd processes
  173. */
  174. #define NFSD_MAXSERVS 8192
  175. int nfsd_nrthreads(struct net *net)
  176. {
  177. int rv = 0;
  178. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  179. mutex_lock(&nfsd_mutex);
  180. if (nn->nfsd_serv)
  181. rv = nn->nfsd_serv->sv_nrthreads;
  182. mutex_unlock(&nfsd_mutex);
  183. return rv;
  184. }
  185. static int nfsd_init_socks(struct net *net)
  186. {
  187. int error;
  188. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  189. if (!list_empty(&nn->nfsd_serv->sv_permsocks))
  190. return 0;
  191. error = svc_create_xprt(nn->nfsd_serv, "udp", net, PF_INET, NFS_PORT,
  192. SVC_SOCK_DEFAULTS);
  193. if (error < 0)
  194. return error;
  195. error = svc_create_xprt(nn->nfsd_serv, "tcp", net, PF_INET, NFS_PORT,
  196. SVC_SOCK_DEFAULTS);
  197. if (error < 0)
  198. return error;
  199. return 0;
  200. }
  201. static int nfsd_users = 0;
  202. static int nfsd_startup_generic(int nrservs)
  203. {
  204. int ret;
  205. if (nfsd_users++)
  206. return 0;
  207. /*
  208. * Readahead param cache - will no-op if it already exists.
  209. * (Note therefore results will be suboptimal if number of
  210. * threads is modified after nfsd start.)
  211. */
  212. ret = nfsd_racache_init(2*nrservs);
  213. if (ret)
  214. goto dec_users;
  215. ret = nfs4_state_start();
  216. if (ret)
  217. goto out_racache;
  218. return 0;
  219. out_racache:
  220. nfsd_racache_shutdown();
  221. dec_users:
  222. nfsd_users--;
  223. return ret;
  224. }
  225. static void nfsd_shutdown_generic(void)
  226. {
  227. if (--nfsd_users)
  228. return;
  229. nfs4_state_shutdown();
  230. nfsd_racache_shutdown();
  231. }
  232. static bool nfsd_needs_lockd(void)
  233. {
  234. #if defined(CONFIG_NFSD_V3)
  235. return (nfsd_versions[2] != NULL) || (nfsd_versions[3] != NULL);
  236. #else
  237. return (nfsd_versions[2] != NULL);
  238. #endif
  239. }
  240. static int nfsd_startup_net(int nrservs, struct net *net)
  241. {
  242. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  243. int ret;
  244. if (nn->nfsd_net_up)
  245. return 0;
  246. ret = nfsd_startup_generic(nrservs);
  247. if (ret)
  248. return ret;
  249. ret = nfsd_init_socks(net);
  250. if (ret)
  251. goto out_socks;
  252. if (nfsd_needs_lockd() && !nn->lockd_up) {
  253. ret = lockd_up(net);
  254. if (ret)
  255. goto out_socks;
  256. nn->lockd_up = 1;
  257. }
  258. ret = nfs4_state_start_net(net);
  259. if (ret)
  260. goto out_lockd;
  261. nn->nfsd_net_up = true;
  262. return 0;
  263. out_lockd:
  264. if (nn->lockd_up) {
  265. lockd_down(net);
  266. nn->lockd_up = 0;
  267. }
  268. out_socks:
  269. nfsd_shutdown_generic();
  270. return ret;
  271. }
  272. static void nfsd_shutdown_net(struct net *net)
  273. {
  274. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  275. nfs4_state_shutdown_net(net);
  276. if (nn->lockd_up) {
  277. lockd_down(net);
  278. nn->lockd_up = 0;
  279. }
  280. nn->nfsd_net_up = false;
  281. nfsd_shutdown_generic();
  282. }
  283. static int nfsd_inetaddr_event(struct notifier_block *this, unsigned long event,
  284. void *ptr)
  285. {
  286. struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
  287. struct net_device *dev = ifa->ifa_dev->dev;
  288. struct net *net = dev_net(dev);
  289. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  290. struct sockaddr_in sin;
  291. if ((event != NETDEV_DOWN) ||
  292. !atomic_inc_not_zero(&nn->ntf_refcnt))
  293. goto out;
  294. if (nn->nfsd_serv) {
  295. dprintk("nfsd_inetaddr_event: removed %pI4\n", &ifa->ifa_local);
  296. sin.sin_family = AF_INET;
  297. sin.sin_addr.s_addr = ifa->ifa_local;
  298. svc_age_temp_xprts_now(nn->nfsd_serv, (struct sockaddr *)&sin);
  299. }
  300. atomic_dec(&nn->ntf_refcnt);
  301. wake_up(&nn->ntf_wq);
  302. out:
  303. return NOTIFY_DONE;
  304. }
  305. static struct notifier_block nfsd_inetaddr_notifier = {
  306. .notifier_call = nfsd_inetaddr_event,
  307. };
  308. #if IS_ENABLED(CONFIG_IPV6)
  309. static int nfsd_inet6addr_event(struct notifier_block *this,
  310. unsigned long event, void *ptr)
  311. {
  312. struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
  313. struct net_device *dev = ifa->idev->dev;
  314. struct net *net = dev_net(dev);
  315. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  316. struct sockaddr_in6 sin6;
  317. if ((event != NETDEV_DOWN) ||
  318. !atomic_inc_not_zero(&nn->ntf_refcnt))
  319. goto out;
  320. if (nn->nfsd_serv) {
  321. dprintk("nfsd_inet6addr_event: removed %pI6\n", &ifa->addr);
  322. sin6.sin6_family = AF_INET6;
  323. sin6.sin6_addr = ifa->addr;
  324. if (ipv6_addr_type(&sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
  325. sin6.sin6_scope_id = ifa->idev->dev->ifindex;
  326. svc_age_temp_xprts_now(nn->nfsd_serv, (struct sockaddr *)&sin6);
  327. }
  328. atomic_dec(&nn->ntf_refcnt);
  329. wake_up(&nn->ntf_wq);
  330. out:
  331. return NOTIFY_DONE;
  332. }
  333. static struct notifier_block nfsd_inet6addr_notifier = {
  334. .notifier_call = nfsd_inet6addr_event,
  335. };
  336. #endif
  337. /* Only used under nfsd_mutex, so this atomic may be overkill: */
  338. static atomic_t nfsd_notifier_refcount = ATOMIC_INIT(0);
  339. static void nfsd_last_thread(struct svc_serv *serv, struct net *net)
  340. {
  341. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  342. atomic_dec(&nn->ntf_refcnt);
  343. /* check if the notifier still has clients */
  344. if (atomic_dec_return(&nfsd_notifier_refcount) == 0) {
  345. unregister_inetaddr_notifier(&nfsd_inetaddr_notifier);
  346. #if IS_ENABLED(CONFIG_IPV6)
  347. unregister_inet6addr_notifier(&nfsd_inet6addr_notifier);
  348. #endif
  349. }
  350. wait_event(nn->ntf_wq, atomic_read(&nn->ntf_refcnt) == 0);
  351. /*
  352. * write_ports can create the server without actually starting
  353. * any threads--if we get shut down before any threads are
  354. * started, then nfsd_last_thread will be run before any of this
  355. * other initialization has been done except the rpcb information.
  356. */
  357. svc_rpcb_cleanup(serv, net);
  358. if (!nn->nfsd_net_up)
  359. return;
  360. nfsd_shutdown_net(net);
  361. printk(KERN_WARNING "nfsd: last server has exited, flushing export "
  362. "cache\n");
  363. nfsd_export_flush(net);
  364. }
  365. void nfsd_reset_versions(void)
  366. {
  367. int i;
  368. for (i = 0; i < NFSD_NRVERS; i++)
  369. if (nfsd_vers(i, NFSD_TEST))
  370. return;
  371. for (i = 0; i < NFSD_NRVERS; i++)
  372. if (i != 4)
  373. nfsd_vers(i, NFSD_SET);
  374. else {
  375. int minor = 0;
  376. while (nfsd_minorversion(minor, NFSD_SET) >= 0)
  377. minor++;
  378. }
  379. }
  380. /*
  381. * Each session guarantees a negotiated per slot memory cache for replies
  382. * which in turn consumes memory beyond the v2/v3/v4.0 server. A dedicated
  383. * NFSv4.1 server might want to use more memory for a DRC than a machine
  384. * with mutiple services.
  385. *
  386. * Impose a hard limit on the number of pages for the DRC which varies
  387. * according to the machines free pages. This is of course only a default.
  388. *
  389. * For now this is a #defined shift which could be under admin control
  390. * in the future.
  391. */
  392. static void set_max_drc(void)
  393. {
  394. #define NFSD_DRC_SIZE_SHIFT 7
  395. nfsd_drc_max_mem = (nr_free_buffer_pages()
  396. >> NFSD_DRC_SIZE_SHIFT) * PAGE_SIZE;
  397. nfsd_drc_mem_used = 0;
  398. spin_lock_init(&nfsd_drc_lock);
  399. dprintk("%s nfsd_drc_max_mem %lu \n", __func__, nfsd_drc_max_mem);
  400. }
  401. static int nfsd_get_default_max_blksize(void)
  402. {
  403. struct sysinfo i;
  404. unsigned long long target;
  405. unsigned long ret;
  406. si_meminfo(&i);
  407. target = (i.totalram - i.totalhigh) << PAGE_SHIFT;
  408. /*
  409. * Aim for 1/4096 of memory per thread This gives 1MB on 4Gig
  410. * machines, but only uses 32K on 128M machines. Bottom out at
  411. * 8K on 32M and smaller. Of course, this is only a default.
  412. */
  413. target >>= 12;
  414. ret = NFSSVC_MAXBLKSIZE;
  415. while (ret > target && ret >= 8*1024*2)
  416. ret /= 2;
  417. return ret;
  418. }
  419. static const struct svc_serv_ops nfsd_thread_sv_ops = {
  420. .svo_shutdown = nfsd_last_thread,
  421. .svo_function = nfsd,
  422. .svo_enqueue_xprt = svc_xprt_do_enqueue,
  423. .svo_setup = svc_set_num_threads,
  424. .svo_module = THIS_MODULE,
  425. };
  426. int nfsd_create_serv(struct net *net)
  427. {
  428. int error;
  429. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  430. WARN_ON(!mutex_is_locked(&nfsd_mutex));
  431. if (nn->nfsd_serv) {
  432. svc_get(nn->nfsd_serv);
  433. return 0;
  434. }
  435. if (nfsd_max_blksize == 0)
  436. nfsd_max_blksize = nfsd_get_default_max_blksize();
  437. nfsd_reset_versions();
  438. nn->nfsd_serv = svc_create_pooled(&nfsd_program, nfsd_max_blksize,
  439. &nfsd_thread_sv_ops);
  440. if (nn->nfsd_serv == NULL)
  441. return -ENOMEM;
  442. nn->nfsd_serv->sv_maxconn = nn->max_connections;
  443. error = svc_bind(nn->nfsd_serv, net);
  444. if (error < 0) {
  445. svc_destroy(nn->nfsd_serv);
  446. return error;
  447. }
  448. set_max_drc();
  449. /* check if the notifier is already set */
  450. if (atomic_inc_return(&nfsd_notifier_refcount) == 1) {
  451. register_inetaddr_notifier(&nfsd_inetaddr_notifier);
  452. #if IS_ENABLED(CONFIG_IPV6)
  453. register_inet6addr_notifier(&nfsd_inet6addr_notifier);
  454. #endif
  455. }
  456. atomic_inc(&nn->ntf_refcnt);
  457. ktime_get_real_ts64(&nn->nfssvc_boot); /* record boot time */
  458. return 0;
  459. }
  460. int nfsd_nrpools(struct net *net)
  461. {
  462. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  463. if (nn->nfsd_serv == NULL)
  464. return 0;
  465. else
  466. return nn->nfsd_serv->sv_nrpools;
  467. }
  468. int nfsd_get_nrthreads(int n, int *nthreads, struct net *net)
  469. {
  470. int i = 0;
  471. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  472. if (nn->nfsd_serv != NULL) {
  473. for (i = 0; i < nn->nfsd_serv->sv_nrpools && i < n; i++)
  474. nthreads[i] = nn->nfsd_serv->sv_pools[i].sp_nrthreads;
  475. }
  476. return 0;
  477. }
  478. void nfsd_destroy(struct net *net)
  479. {
  480. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  481. int destroy = (nn->nfsd_serv->sv_nrthreads == 1);
  482. if (destroy)
  483. svc_shutdown_net(nn->nfsd_serv, net);
  484. svc_destroy(nn->nfsd_serv);
  485. if (destroy)
  486. nn->nfsd_serv = NULL;
  487. }
  488. int nfsd_set_nrthreads(int n, int *nthreads, struct net *net)
  489. {
  490. int i = 0;
  491. int tot = 0;
  492. int err = 0;
  493. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  494. WARN_ON(!mutex_is_locked(&nfsd_mutex));
  495. if (nn->nfsd_serv == NULL || n <= 0)
  496. return 0;
  497. if (n > nn->nfsd_serv->sv_nrpools)
  498. n = nn->nfsd_serv->sv_nrpools;
  499. /* enforce a global maximum number of threads */
  500. tot = 0;
  501. for (i = 0; i < n; i++) {
  502. nthreads[i] = min(nthreads[i], NFSD_MAXSERVS);
  503. tot += nthreads[i];
  504. }
  505. if (tot > NFSD_MAXSERVS) {
  506. /* total too large: scale down requested numbers */
  507. for (i = 0; i < n && tot > 0; i++) {
  508. int new = nthreads[i] * NFSD_MAXSERVS / tot;
  509. tot -= (nthreads[i] - new);
  510. nthreads[i] = new;
  511. }
  512. for (i = 0; i < n && tot > 0; i++) {
  513. nthreads[i]--;
  514. tot--;
  515. }
  516. }
  517. /*
  518. * There must always be a thread in pool 0; the admin
  519. * can't shut down NFS completely using pool_threads.
  520. */
  521. if (nthreads[0] == 0)
  522. nthreads[0] = 1;
  523. /* apply the new numbers */
  524. svc_get(nn->nfsd_serv);
  525. for (i = 0; i < n; i++) {
  526. err = nn->nfsd_serv->sv_ops->svo_setup(nn->nfsd_serv,
  527. &nn->nfsd_serv->sv_pools[i], nthreads[i]);
  528. if (err)
  529. break;
  530. }
  531. nfsd_destroy(net);
  532. return err;
  533. }
  534. /*
  535. * Adjust the number of threads and return the new number of threads.
  536. * This is also the function that starts the server if necessary, if
  537. * this is the first time nrservs is nonzero.
  538. */
  539. int
  540. nfsd_svc(int nrservs, struct net *net)
  541. {
  542. int error;
  543. bool nfsd_up_before;
  544. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  545. mutex_lock(&nfsd_mutex);
  546. dprintk("nfsd: creating service\n");
  547. nrservs = max(nrservs, 0);
  548. nrservs = min(nrservs, NFSD_MAXSERVS);
  549. error = 0;
  550. if (nrservs == 0 && nn->nfsd_serv == NULL)
  551. goto out;
  552. error = nfsd_create_serv(net);
  553. if (error)
  554. goto out;
  555. nfsd_up_before = nn->nfsd_net_up;
  556. error = nfsd_startup_net(nrservs, net);
  557. if (error)
  558. goto out_destroy;
  559. error = nn->nfsd_serv->sv_ops->svo_setup(nn->nfsd_serv,
  560. NULL, nrservs);
  561. if (error)
  562. goto out_shutdown;
  563. /* We are holding a reference to nn->nfsd_serv which
  564. * we don't want to count in the return value,
  565. * so subtract 1
  566. */
  567. error = nn->nfsd_serv->sv_nrthreads - 1;
  568. out_shutdown:
  569. if (error < 0 && !nfsd_up_before)
  570. nfsd_shutdown_net(net);
  571. out_destroy:
  572. nfsd_destroy(net); /* Release server */
  573. out:
  574. mutex_unlock(&nfsd_mutex);
  575. return error;
  576. }
  577. /*
  578. * This is the NFS server kernel thread
  579. */
  580. static int
  581. nfsd(void *vrqstp)
  582. {
  583. struct svc_rqst *rqstp = (struct svc_rqst *) vrqstp;
  584. struct svc_xprt *perm_sock = list_entry(rqstp->rq_server->sv_permsocks.next, typeof(struct svc_xprt), xpt_list);
  585. struct net *net = perm_sock->xpt_net;
  586. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  587. int err;
  588. /* Lock module and set up kernel thread */
  589. mutex_lock(&nfsd_mutex);
  590. /* At this point, the thread shares current->fs
  591. * with the init process. We need to create files with the
  592. * umask as defined by the client instead of init's umask. */
  593. if (unshare_fs_struct() < 0) {
  594. printk("Unable to start nfsd thread: out of memory\n");
  595. goto out;
  596. }
  597. current->fs->umask = 0;
  598. /*
  599. * thread is spawned with all signals set to SIG_IGN, re-enable
  600. * the ones that will bring down the thread
  601. */
  602. allow_signal(SIGKILL);
  603. allow_signal(SIGHUP);
  604. allow_signal(SIGINT);
  605. allow_signal(SIGQUIT);
  606. nfsdstats.th_cnt++;
  607. mutex_unlock(&nfsd_mutex);
  608. set_freezable();
  609. /*
  610. * The main request loop
  611. */
  612. for (;;) {
  613. /* Update sv_maxconn if it has changed */
  614. rqstp->rq_server->sv_maxconn = nn->max_connections;
  615. /*
  616. * Find a socket with data available and call its
  617. * recvfrom routine.
  618. */
  619. while ((err = svc_recv(rqstp, 60*60*HZ)) == -EAGAIN)
  620. ;
  621. if (err == -EINTR)
  622. break;
  623. validate_process_creds();
  624. svc_process(rqstp);
  625. validate_process_creds();
  626. }
  627. /* Clear signals before calling svc_exit_thread() */
  628. flush_signals(current);
  629. mutex_lock(&nfsd_mutex);
  630. nfsdstats.th_cnt --;
  631. out:
  632. rqstp->rq_server = NULL;
  633. /* Release the thread */
  634. svc_exit_thread(rqstp);
  635. nfsd_destroy(net);
  636. /* Release module */
  637. mutex_unlock(&nfsd_mutex);
  638. module_put_and_exit(0);
  639. return 0;
  640. }
  641. static __be32 map_new_errors(u32 vers, __be32 nfserr)
  642. {
  643. if (nfserr == nfserr_jukebox && vers == 2)
  644. return nfserr_dropit;
  645. if (nfserr == nfserr_wrongsec && vers < 4)
  646. return nfserr_acces;
  647. return nfserr;
  648. }
  649. /*
  650. * A write procedure can have a large argument, and a read procedure can
  651. * have a large reply, but no NFSv2 or NFSv3 procedure has argument and
  652. * reply that can both be larger than a page. The xdr code has taken
  653. * advantage of this assumption to be a sloppy about bounds checking in
  654. * some cases. Pending a rewrite of the NFSv2/v3 xdr code to fix that
  655. * problem, we enforce these assumptions here:
  656. */
  657. static bool nfs_request_too_big(struct svc_rqst *rqstp,
  658. const struct svc_procedure *proc)
  659. {
  660. /*
  661. * The ACL code has more careful bounds-checking and is not
  662. * susceptible to this problem:
  663. */
  664. if (rqstp->rq_prog != NFS_PROGRAM)
  665. return false;
  666. /*
  667. * Ditto NFSv4 (which can in theory have argument and reply both
  668. * more than a page):
  669. */
  670. if (rqstp->rq_vers >= 4)
  671. return false;
  672. /* The reply will be small, we're OK: */
  673. if (proc->pc_xdrressize > 0 &&
  674. proc->pc_xdrressize < XDR_QUADLEN(PAGE_SIZE))
  675. return false;
  676. return rqstp->rq_arg.len > PAGE_SIZE;
  677. }
  678. int
  679. nfsd_dispatch(struct svc_rqst *rqstp, __be32 *statp)
  680. {
  681. const struct svc_procedure *proc;
  682. __be32 nfserr;
  683. __be32 *nfserrp;
  684. dprintk("nfsd_dispatch: vers %d proc %d\n",
  685. rqstp->rq_vers, rqstp->rq_proc);
  686. proc = rqstp->rq_procinfo;
  687. if (nfs_request_too_big(rqstp, proc)) {
  688. dprintk("nfsd: NFSv%d argument too large\n", rqstp->rq_vers);
  689. *statp = rpc_garbage_args;
  690. return 1;
  691. }
  692. /*
  693. * Give the xdr decoder a chance to change this if it wants
  694. * (necessary in the NFSv4.0 compound case)
  695. */
  696. rqstp->rq_cachetype = proc->pc_cachetype;
  697. /* Decode arguments */
  698. if (proc->pc_decode &&
  699. !proc->pc_decode(rqstp, (__be32*)rqstp->rq_arg.head[0].iov_base)) {
  700. dprintk("nfsd: failed to decode arguments!\n");
  701. *statp = rpc_garbage_args;
  702. return 1;
  703. }
  704. /* Check whether we have this call in the cache. */
  705. switch (nfsd_cache_lookup(rqstp)) {
  706. case RC_DROPIT:
  707. return 0;
  708. case RC_REPLY:
  709. return 1;
  710. case RC_DOIT:;
  711. /* do it */
  712. }
  713. /* need to grab the location to store the status, as
  714. * nfsv4 does some encoding while processing
  715. */
  716. nfserrp = rqstp->rq_res.head[0].iov_base
  717. + rqstp->rq_res.head[0].iov_len;
  718. rqstp->rq_res.head[0].iov_len += sizeof(__be32);
  719. /* Now call the procedure handler, and encode NFS status. */
  720. nfserr = proc->pc_func(rqstp);
  721. nfserr = map_new_errors(rqstp->rq_vers, nfserr);
  722. if (nfserr == nfserr_dropit || test_bit(RQ_DROPME, &rqstp->rq_flags)) {
  723. dprintk("nfsd: Dropping request; may be revisited later\n");
  724. nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
  725. return 0;
  726. }
  727. if (rqstp->rq_proc != 0)
  728. *nfserrp++ = nfserr;
  729. /* Encode result.
  730. * For NFSv2, additional info is never returned in case of an error.
  731. */
  732. if (!(nfserr && rqstp->rq_vers == 2)) {
  733. if (proc->pc_encode && !proc->pc_encode(rqstp, nfserrp)) {
  734. /* Failed to encode result. Release cache entry */
  735. dprintk("nfsd: failed to encode result!\n");
  736. nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
  737. *statp = rpc_system_err;
  738. return 1;
  739. }
  740. }
  741. /* Store reply in cache. */
  742. nfsd_cache_update(rqstp, rqstp->rq_cachetype, statp + 1);
  743. return 1;
  744. }
  745. int nfsd_pool_stats_open(struct inode *inode, struct file *file)
  746. {
  747. int ret;
  748. struct nfsd_net *nn = net_generic(inode->i_sb->s_fs_info, nfsd_net_id);
  749. mutex_lock(&nfsd_mutex);
  750. if (nn->nfsd_serv == NULL) {
  751. mutex_unlock(&nfsd_mutex);
  752. return -ENODEV;
  753. }
  754. /* bump up the psudo refcount while traversing */
  755. svc_get(nn->nfsd_serv);
  756. ret = svc_pool_stats_open(nn->nfsd_serv, file);
  757. mutex_unlock(&nfsd_mutex);
  758. return ret;
  759. }
  760. int nfsd_pool_stats_release(struct inode *inode, struct file *file)
  761. {
  762. int ret = seq_release(inode, file);
  763. struct net *net = inode->i_sb->s_fs_info;
  764. mutex_lock(&nfsd_mutex);
  765. /* this function really, really should have been called svc_put() */
  766. nfsd_destroy(net);
  767. mutex_unlock(&nfsd_mutex);
  768. return ret;
  769. }