nfssvc.c 21 KB

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