nfssvc.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  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. #include "filecache.h"
  29. #define NFSDDBG_FACILITY NFSDDBG_SVC
  30. bool inter_copy_offload_enable;
  31. EXPORT_SYMBOL_GPL(inter_copy_offload_enable);
  32. module_param(inter_copy_offload_enable, bool, 0644);
  33. MODULE_PARM_DESC(inter_copy_offload_enable,
  34. "Enable inter server to server copy offload. Default: false");
  35. extern struct svc_program nfsd_program;
  36. static int nfsd(void *vrqstp);
  37. #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
  38. static int nfsd_acl_rpcbind_set(struct net *,
  39. const struct svc_program *,
  40. u32, int,
  41. unsigned short,
  42. unsigned short);
  43. static __be32 nfsd_acl_init_request(struct svc_rqst *,
  44. const struct svc_program *,
  45. struct svc_process_info *);
  46. #endif
  47. static int nfsd_rpcbind_set(struct net *,
  48. const struct svc_program *,
  49. u32, int,
  50. unsigned short,
  51. unsigned short);
  52. static __be32 nfsd_init_request(struct svc_rqst *,
  53. const struct svc_program *,
  54. struct svc_process_info *);
  55. /*
  56. * nfsd_mutex protects nn->nfsd_serv -- both the pointer itself and the members
  57. * of the svc_serv struct. In particular, ->sv_nrthreads but also to some
  58. * extent ->sv_temp_socks and ->sv_permsocks. It also protects nfsdstats.th_cnt
  59. *
  60. * If (out side the lock) nn->nfsd_serv is non-NULL, then it must point to a
  61. * properly initialised 'struct svc_serv' with ->sv_nrthreads > 0. That number
  62. * of nfsd threads must exist and each must listed in ->sp_all_threads in each
  63. * entry of ->sv_pools[].
  64. *
  65. * Transitions of the thread count between zero and non-zero are of particular
  66. * interest since the svc_serv needs to be created and initialized at that
  67. * point, or freed.
  68. *
  69. * Finally, the nfsd_mutex also protects some of the global variables that are
  70. * accessed when nfsd starts and that are settable via the write_* routines in
  71. * nfsctl.c. In particular:
  72. *
  73. * user_recovery_dirname
  74. * user_lease_time
  75. * nfsd_versions
  76. */
  77. DEFINE_MUTEX(nfsd_mutex);
  78. /*
  79. * nfsd_drc_lock protects nfsd_drc_max_pages and nfsd_drc_pages_used.
  80. * nfsd_drc_max_pages limits the total amount of memory available for
  81. * version 4.1 DRC caches.
  82. * nfsd_drc_pages_used tracks the current version 4.1 DRC memory usage.
  83. */
  84. spinlock_t nfsd_drc_lock;
  85. unsigned long nfsd_drc_max_mem;
  86. unsigned long nfsd_drc_mem_used;
  87. #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
  88. static struct svc_stat nfsd_acl_svcstats;
  89. static const struct svc_version *nfsd_acl_version[] = {
  90. [2] = &nfsd_acl_version2,
  91. [3] = &nfsd_acl_version3,
  92. };
  93. #define NFSD_ACL_MINVERS 2
  94. #define NFSD_ACL_NRVERS ARRAY_SIZE(nfsd_acl_version)
  95. static struct svc_program nfsd_acl_program = {
  96. .pg_prog = NFS_ACL_PROGRAM,
  97. .pg_nvers = NFSD_ACL_NRVERS,
  98. .pg_vers = nfsd_acl_version,
  99. .pg_name = "nfsacl",
  100. .pg_class = "nfsd",
  101. .pg_stats = &nfsd_acl_svcstats,
  102. .pg_authenticate = &svc_set_client,
  103. .pg_init_request = nfsd_acl_init_request,
  104. .pg_rpcbind_set = nfsd_acl_rpcbind_set,
  105. };
  106. static struct svc_stat nfsd_acl_svcstats = {
  107. .program = &nfsd_acl_program,
  108. };
  109. #endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */
  110. static const struct svc_version *nfsd_version[] = {
  111. [2] = &nfsd_version2,
  112. #if defined(CONFIG_NFSD_V3)
  113. [3] = &nfsd_version3,
  114. #endif
  115. #if defined(CONFIG_NFSD_V4)
  116. [4] = &nfsd_version4,
  117. #endif
  118. };
  119. #define NFSD_MINVERS 2
  120. #define NFSD_NRVERS ARRAY_SIZE(nfsd_version)
  121. struct svc_program nfsd_program = {
  122. #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
  123. .pg_next = &nfsd_acl_program,
  124. #endif
  125. .pg_prog = NFS_PROGRAM, /* program number */
  126. .pg_nvers = NFSD_NRVERS, /* nr of entries in nfsd_version */
  127. .pg_vers = nfsd_version, /* version table */
  128. .pg_name = "nfsd", /* program name */
  129. .pg_class = "nfsd", /* authentication class */
  130. .pg_stats = &nfsd_svcstats, /* version table */
  131. .pg_authenticate = &svc_set_client, /* export authentication */
  132. .pg_init_request = nfsd_init_request,
  133. .pg_rpcbind_set = nfsd_rpcbind_set,
  134. };
  135. static bool
  136. nfsd_support_version(int vers)
  137. {
  138. if (vers >= NFSD_MINVERS && vers < NFSD_NRVERS)
  139. return nfsd_version[vers] != NULL;
  140. return false;
  141. }
  142. static bool *
  143. nfsd_alloc_versions(void)
  144. {
  145. bool *vers = kmalloc_array(NFSD_NRVERS, sizeof(bool), GFP_KERNEL);
  146. unsigned i;
  147. if (vers) {
  148. /* All compiled versions are enabled by default */
  149. for (i = 0; i < NFSD_NRVERS; i++)
  150. vers[i] = nfsd_support_version(i);
  151. }
  152. return vers;
  153. }
  154. static bool *
  155. nfsd_alloc_minorversions(void)
  156. {
  157. bool *vers = kmalloc_array(NFSD_SUPPORTED_MINOR_VERSION + 1,
  158. sizeof(bool), GFP_KERNEL);
  159. unsigned i;
  160. if (vers) {
  161. /* All minor versions are enabled by default */
  162. for (i = 0; i <= NFSD_SUPPORTED_MINOR_VERSION; i++)
  163. vers[i] = nfsd_support_version(4);
  164. }
  165. return vers;
  166. }
  167. void
  168. nfsd_netns_free_versions(struct nfsd_net *nn)
  169. {
  170. kfree(nn->nfsd_versions);
  171. kfree(nn->nfsd4_minorversions);
  172. nn->nfsd_versions = NULL;
  173. nn->nfsd4_minorversions = NULL;
  174. }
  175. static void
  176. nfsd_netns_init_versions(struct nfsd_net *nn)
  177. {
  178. if (!nn->nfsd_versions) {
  179. nn->nfsd_versions = nfsd_alloc_versions();
  180. nn->nfsd4_minorversions = nfsd_alloc_minorversions();
  181. if (!nn->nfsd_versions || !nn->nfsd4_minorversions)
  182. nfsd_netns_free_versions(nn);
  183. }
  184. }
  185. int nfsd_vers(struct nfsd_net *nn, int vers, enum vers_op change)
  186. {
  187. if (vers < NFSD_MINVERS || vers >= NFSD_NRVERS)
  188. return 0;
  189. switch(change) {
  190. case NFSD_SET:
  191. if (nn->nfsd_versions)
  192. nn->nfsd_versions[vers] = nfsd_support_version(vers);
  193. break;
  194. case NFSD_CLEAR:
  195. nfsd_netns_init_versions(nn);
  196. if (nn->nfsd_versions)
  197. nn->nfsd_versions[vers] = false;
  198. break;
  199. case NFSD_TEST:
  200. if (nn->nfsd_versions)
  201. return nn->nfsd_versions[vers];
  202. fallthrough;
  203. case NFSD_AVAIL:
  204. return nfsd_support_version(vers);
  205. }
  206. return 0;
  207. }
  208. static void
  209. nfsd_adjust_nfsd_versions4(struct nfsd_net *nn)
  210. {
  211. unsigned i;
  212. for (i = 0; i <= NFSD_SUPPORTED_MINOR_VERSION; i++) {
  213. if (nn->nfsd4_minorversions[i])
  214. return;
  215. }
  216. nfsd_vers(nn, 4, NFSD_CLEAR);
  217. }
  218. int nfsd_minorversion(struct nfsd_net *nn, u32 minorversion, enum vers_op change)
  219. {
  220. if (minorversion > NFSD_SUPPORTED_MINOR_VERSION &&
  221. change != NFSD_AVAIL)
  222. return -1;
  223. switch(change) {
  224. case NFSD_SET:
  225. if (nn->nfsd4_minorversions) {
  226. nfsd_vers(nn, 4, NFSD_SET);
  227. nn->nfsd4_minorversions[minorversion] =
  228. nfsd_vers(nn, 4, NFSD_TEST);
  229. }
  230. break;
  231. case NFSD_CLEAR:
  232. nfsd_netns_init_versions(nn);
  233. if (nn->nfsd4_minorversions) {
  234. nn->nfsd4_minorversions[minorversion] = false;
  235. nfsd_adjust_nfsd_versions4(nn);
  236. }
  237. break;
  238. case NFSD_TEST:
  239. if (nn->nfsd4_minorversions)
  240. return nn->nfsd4_minorversions[minorversion];
  241. return nfsd_vers(nn, 4, NFSD_TEST);
  242. case NFSD_AVAIL:
  243. return minorversion <= NFSD_SUPPORTED_MINOR_VERSION &&
  244. nfsd_vers(nn, 4, NFSD_AVAIL);
  245. }
  246. return 0;
  247. }
  248. /*
  249. * Maximum number of nfsd processes
  250. */
  251. #define NFSD_MAXSERVS 8192
  252. int nfsd_nrthreads(struct net *net)
  253. {
  254. int rv = 0;
  255. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  256. mutex_lock(&nfsd_mutex);
  257. if (nn->nfsd_serv)
  258. rv = nn->nfsd_serv->sv_nrthreads;
  259. mutex_unlock(&nfsd_mutex);
  260. return rv;
  261. }
  262. static int nfsd_init_socks(struct net *net, const struct cred *cred)
  263. {
  264. int error;
  265. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  266. if (!list_empty(&nn->nfsd_serv->sv_permsocks))
  267. return 0;
  268. error = svc_create_xprt(nn->nfsd_serv, "udp", net, PF_INET, NFS_PORT,
  269. SVC_SOCK_DEFAULTS, cred);
  270. if (error < 0)
  271. return error;
  272. error = svc_create_xprt(nn->nfsd_serv, "tcp", net, PF_INET, NFS_PORT,
  273. SVC_SOCK_DEFAULTS, cred);
  274. if (error < 0)
  275. return error;
  276. return 0;
  277. }
  278. static int nfsd_users = 0;
  279. static int nfsd_startup_generic(int nrservs)
  280. {
  281. int ret;
  282. if (nfsd_users++)
  283. return 0;
  284. ret = nfsd_file_cache_init();
  285. if (ret)
  286. goto dec_users;
  287. ret = nfs4_state_start();
  288. if (ret)
  289. goto out_file_cache;
  290. return 0;
  291. out_file_cache:
  292. nfsd_file_cache_shutdown();
  293. dec_users:
  294. nfsd_users--;
  295. return ret;
  296. }
  297. static void nfsd_shutdown_generic(void)
  298. {
  299. if (--nfsd_users)
  300. return;
  301. nfs4_state_shutdown();
  302. nfsd_file_cache_shutdown();
  303. }
  304. static bool nfsd_needs_lockd(struct nfsd_net *nn)
  305. {
  306. return nfsd_vers(nn, 2, NFSD_TEST) || nfsd_vers(nn, 3, NFSD_TEST);
  307. }
  308. void nfsd_copy_boot_verifier(__be32 verf[2], struct nfsd_net *nn)
  309. {
  310. int seq = 0;
  311. do {
  312. read_seqbegin_or_lock(&nn->boot_lock, &seq);
  313. /*
  314. * This is opaque to client, so no need to byte-swap. Use
  315. * __force to keep sparse happy. y2038 time_t overflow is
  316. * irrelevant in this usage
  317. */
  318. verf[0] = (__force __be32)nn->nfssvc_boot.tv_sec;
  319. verf[1] = (__force __be32)nn->nfssvc_boot.tv_nsec;
  320. } while (need_seqretry(&nn->boot_lock, seq));
  321. done_seqretry(&nn->boot_lock, seq);
  322. }
  323. static void nfsd_reset_boot_verifier_locked(struct nfsd_net *nn)
  324. {
  325. ktime_get_real_ts64(&nn->nfssvc_boot);
  326. }
  327. void nfsd_reset_boot_verifier(struct nfsd_net *nn)
  328. {
  329. write_seqlock(&nn->boot_lock);
  330. nfsd_reset_boot_verifier_locked(nn);
  331. write_sequnlock(&nn->boot_lock);
  332. }
  333. static int nfsd_startup_net(int nrservs, struct net *net, const struct cred *cred)
  334. {
  335. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  336. int ret;
  337. if (nn->nfsd_net_up)
  338. return 0;
  339. ret = nfsd_startup_generic(nrservs);
  340. if (ret)
  341. return ret;
  342. ret = nfsd_init_socks(net, cred);
  343. if (ret)
  344. goto out_socks;
  345. if (nfsd_needs_lockd(nn) && !nn->lockd_up) {
  346. ret = lockd_up(net, cred);
  347. if (ret)
  348. goto out_socks;
  349. nn->lockd_up = true;
  350. }
  351. ret = nfsd_file_cache_start_net(net);
  352. if (ret)
  353. goto out_lockd;
  354. ret = nfs4_state_start_net(net);
  355. if (ret)
  356. goto out_filecache;
  357. nn->nfsd_net_up = true;
  358. return 0;
  359. out_filecache:
  360. nfsd_file_cache_shutdown_net(net);
  361. out_lockd:
  362. if (nn->lockd_up) {
  363. lockd_down(net);
  364. nn->lockd_up = false;
  365. }
  366. out_socks:
  367. nfsd_shutdown_generic();
  368. return ret;
  369. }
  370. static void nfsd_shutdown_net(struct net *net)
  371. {
  372. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  373. nfsd_file_cache_shutdown_net(net);
  374. nfs4_state_shutdown_net(net);
  375. if (nn->lockd_up) {
  376. lockd_down(net);
  377. nn->lockd_up = false;
  378. }
  379. nn->nfsd_net_up = false;
  380. nfsd_shutdown_generic();
  381. }
  382. static int nfsd_inetaddr_event(struct notifier_block *this, unsigned long event,
  383. void *ptr)
  384. {
  385. struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
  386. struct net_device *dev = ifa->ifa_dev->dev;
  387. struct net *net = dev_net(dev);
  388. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  389. struct sockaddr_in sin;
  390. if ((event != NETDEV_DOWN) ||
  391. !atomic_inc_not_zero(&nn->ntf_refcnt))
  392. goto out;
  393. if (nn->nfsd_serv) {
  394. dprintk("nfsd_inetaddr_event: removed %pI4\n", &ifa->ifa_local);
  395. sin.sin_family = AF_INET;
  396. sin.sin_addr.s_addr = ifa->ifa_local;
  397. svc_age_temp_xprts_now(nn->nfsd_serv, (struct sockaddr *)&sin);
  398. }
  399. atomic_dec(&nn->ntf_refcnt);
  400. wake_up(&nn->ntf_wq);
  401. out:
  402. return NOTIFY_DONE;
  403. }
  404. static struct notifier_block nfsd_inetaddr_notifier = {
  405. .notifier_call = nfsd_inetaddr_event,
  406. };
  407. #if IS_ENABLED(CONFIG_IPV6)
  408. static int nfsd_inet6addr_event(struct notifier_block *this,
  409. unsigned long event, void *ptr)
  410. {
  411. struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
  412. struct net_device *dev = ifa->idev->dev;
  413. struct net *net = dev_net(dev);
  414. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  415. struct sockaddr_in6 sin6;
  416. if ((event != NETDEV_DOWN) ||
  417. !atomic_inc_not_zero(&nn->ntf_refcnt))
  418. goto out;
  419. if (nn->nfsd_serv) {
  420. dprintk("nfsd_inet6addr_event: removed %pI6\n", &ifa->addr);
  421. sin6.sin6_family = AF_INET6;
  422. sin6.sin6_addr = ifa->addr;
  423. if (ipv6_addr_type(&sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
  424. sin6.sin6_scope_id = ifa->idev->dev->ifindex;
  425. svc_age_temp_xprts_now(nn->nfsd_serv, (struct sockaddr *)&sin6);
  426. }
  427. atomic_dec(&nn->ntf_refcnt);
  428. wake_up(&nn->ntf_wq);
  429. out:
  430. return NOTIFY_DONE;
  431. }
  432. static struct notifier_block nfsd_inet6addr_notifier = {
  433. .notifier_call = nfsd_inet6addr_event,
  434. };
  435. #endif
  436. /* Only used under nfsd_mutex, so this atomic may be overkill: */
  437. static atomic_t nfsd_notifier_refcount = ATOMIC_INIT(0);
  438. static void nfsd_last_thread(struct svc_serv *serv, struct net *net)
  439. {
  440. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  441. atomic_dec(&nn->ntf_refcnt);
  442. /* check if the notifier still has clients */
  443. if (atomic_dec_return(&nfsd_notifier_refcount) == 0) {
  444. unregister_inetaddr_notifier(&nfsd_inetaddr_notifier);
  445. #if IS_ENABLED(CONFIG_IPV6)
  446. unregister_inet6addr_notifier(&nfsd_inet6addr_notifier);
  447. #endif
  448. }
  449. wait_event(nn->ntf_wq, atomic_read(&nn->ntf_refcnt) == 0);
  450. /*
  451. * write_ports can create the server without actually starting
  452. * any threads--if we get shut down before any threads are
  453. * started, then nfsd_last_thread will be run before any of this
  454. * other initialization has been done except the rpcb information.
  455. */
  456. svc_rpcb_cleanup(serv, net);
  457. if (!nn->nfsd_net_up)
  458. return;
  459. nfsd_shutdown_net(net);
  460. pr_info("nfsd: last server has exited, flushing export cache\n");
  461. nfsd_export_flush(net);
  462. }
  463. void nfsd_reset_versions(struct nfsd_net *nn)
  464. {
  465. int i;
  466. for (i = 0; i < NFSD_NRVERS; i++)
  467. if (nfsd_vers(nn, i, NFSD_TEST))
  468. return;
  469. for (i = 0; i < NFSD_NRVERS; i++)
  470. if (i != 4)
  471. nfsd_vers(nn, i, NFSD_SET);
  472. else {
  473. int minor = 0;
  474. while (nfsd_minorversion(nn, minor, NFSD_SET) >= 0)
  475. minor++;
  476. }
  477. }
  478. /*
  479. * Each session guarantees a negotiated per slot memory cache for replies
  480. * which in turn consumes memory beyond the v2/v3/v4.0 server. A dedicated
  481. * NFSv4.1 server might want to use more memory for a DRC than a machine
  482. * with mutiple services.
  483. *
  484. * Impose a hard limit on the number of pages for the DRC which varies
  485. * according to the machines free pages. This is of course only a default.
  486. *
  487. * For now this is a #defined shift which could be under admin control
  488. * in the future.
  489. */
  490. static void set_max_drc(void)
  491. {
  492. #define NFSD_DRC_SIZE_SHIFT 7
  493. nfsd_drc_max_mem = (nr_free_buffer_pages()
  494. >> NFSD_DRC_SIZE_SHIFT) * PAGE_SIZE;
  495. nfsd_drc_mem_used = 0;
  496. spin_lock_init(&nfsd_drc_lock);
  497. dprintk("%s nfsd_drc_max_mem %lu \n", __func__, nfsd_drc_max_mem);
  498. }
  499. static int nfsd_get_default_max_blksize(void)
  500. {
  501. struct sysinfo i;
  502. unsigned long long target;
  503. unsigned long ret;
  504. si_meminfo(&i);
  505. target = (i.totalram - i.totalhigh) << PAGE_SHIFT;
  506. /*
  507. * Aim for 1/4096 of memory per thread This gives 1MB on 4Gig
  508. * machines, but only uses 32K on 128M machines. Bottom out at
  509. * 8K on 32M and smaller. Of course, this is only a default.
  510. */
  511. target >>= 12;
  512. ret = NFSSVC_MAXBLKSIZE;
  513. while (ret > target && ret >= 8*1024*2)
  514. ret /= 2;
  515. return ret;
  516. }
  517. static const struct svc_serv_ops nfsd_thread_sv_ops = {
  518. .svo_shutdown = nfsd_last_thread,
  519. .svo_function = nfsd,
  520. .svo_enqueue_xprt = svc_xprt_do_enqueue,
  521. .svo_setup = svc_set_num_threads,
  522. .svo_module = THIS_MODULE,
  523. };
  524. bool i_am_nfsd(void)
  525. {
  526. return kthread_func(current) == nfsd;
  527. }
  528. int nfsd_create_serv(struct net *net)
  529. {
  530. int error;
  531. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  532. WARN_ON(!mutex_is_locked(&nfsd_mutex));
  533. if (nn->nfsd_serv) {
  534. svc_get(nn->nfsd_serv);
  535. return 0;
  536. }
  537. if (nfsd_max_blksize == 0)
  538. nfsd_max_blksize = nfsd_get_default_max_blksize();
  539. nfsd_reset_versions(nn);
  540. nn->nfsd_serv = svc_create_pooled(&nfsd_program, nfsd_max_blksize,
  541. &nfsd_thread_sv_ops);
  542. if (nn->nfsd_serv == NULL)
  543. return -ENOMEM;
  544. nn->nfsd_serv->sv_maxconn = nn->max_connections;
  545. error = svc_bind(nn->nfsd_serv, net);
  546. if (error < 0) {
  547. svc_destroy(nn->nfsd_serv);
  548. return error;
  549. }
  550. set_max_drc();
  551. /* check if the notifier is already set */
  552. if (atomic_inc_return(&nfsd_notifier_refcount) == 1) {
  553. register_inetaddr_notifier(&nfsd_inetaddr_notifier);
  554. #if IS_ENABLED(CONFIG_IPV6)
  555. register_inet6addr_notifier(&nfsd_inet6addr_notifier);
  556. #endif
  557. }
  558. atomic_inc(&nn->ntf_refcnt);
  559. nfsd_reset_boot_verifier(nn);
  560. return 0;
  561. }
  562. int nfsd_nrpools(struct net *net)
  563. {
  564. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  565. if (nn->nfsd_serv == NULL)
  566. return 0;
  567. else
  568. return nn->nfsd_serv->sv_nrpools;
  569. }
  570. int nfsd_get_nrthreads(int n, int *nthreads, struct net *net)
  571. {
  572. int i = 0;
  573. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  574. if (nn->nfsd_serv != NULL) {
  575. for (i = 0; i < nn->nfsd_serv->sv_nrpools && i < n; i++)
  576. nthreads[i] = nn->nfsd_serv->sv_pools[i].sp_nrthreads;
  577. }
  578. return 0;
  579. }
  580. void nfsd_destroy(struct net *net)
  581. {
  582. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  583. int destroy = (nn->nfsd_serv->sv_nrthreads == 1);
  584. if (destroy)
  585. svc_shutdown_net(nn->nfsd_serv, net);
  586. svc_destroy(nn->nfsd_serv);
  587. if (destroy)
  588. nn->nfsd_serv = NULL;
  589. }
  590. int nfsd_set_nrthreads(int n, int *nthreads, struct net *net)
  591. {
  592. int i = 0;
  593. int tot = 0;
  594. int err = 0;
  595. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  596. WARN_ON(!mutex_is_locked(&nfsd_mutex));
  597. if (nn->nfsd_serv == NULL || n <= 0)
  598. return 0;
  599. if (n > nn->nfsd_serv->sv_nrpools)
  600. n = nn->nfsd_serv->sv_nrpools;
  601. /* enforce a global maximum number of threads */
  602. tot = 0;
  603. for (i = 0; i < n; i++) {
  604. nthreads[i] = min(nthreads[i], NFSD_MAXSERVS);
  605. tot += nthreads[i];
  606. }
  607. if (tot > NFSD_MAXSERVS) {
  608. /* total too large: scale down requested numbers */
  609. for (i = 0; i < n && tot > 0; i++) {
  610. int new = nthreads[i] * NFSD_MAXSERVS / tot;
  611. tot -= (nthreads[i] - new);
  612. nthreads[i] = new;
  613. }
  614. for (i = 0; i < n && tot > 0; i++) {
  615. nthreads[i]--;
  616. tot--;
  617. }
  618. }
  619. /*
  620. * There must always be a thread in pool 0; the admin
  621. * can't shut down NFS completely using pool_threads.
  622. */
  623. if (nthreads[0] == 0)
  624. nthreads[0] = 1;
  625. /* apply the new numbers */
  626. svc_get(nn->nfsd_serv);
  627. for (i = 0; i < n; i++) {
  628. err = nn->nfsd_serv->sv_ops->svo_setup(nn->nfsd_serv,
  629. &nn->nfsd_serv->sv_pools[i], nthreads[i]);
  630. if (err)
  631. break;
  632. }
  633. nfsd_destroy(net);
  634. return err;
  635. }
  636. /*
  637. * Adjust the number of threads and return the new number of threads.
  638. * This is also the function that starts the server if necessary, if
  639. * this is the first time nrservs is nonzero.
  640. */
  641. int
  642. nfsd_svc(int nrservs, struct net *net, const struct cred *cred)
  643. {
  644. int error;
  645. bool nfsd_up_before;
  646. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  647. mutex_lock(&nfsd_mutex);
  648. dprintk("nfsd: creating service\n");
  649. nrservs = max(nrservs, 0);
  650. nrservs = min(nrservs, NFSD_MAXSERVS);
  651. error = 0;
  652. if (nrservs == 0 && nn->nfsd_serv == NULL)
  653. goto out;
  654. strlcpy(nn->nfsd_name, utsname()->nodename,
  655. sizeof(nn->nfsd_name));
  656. error = nfsd_create_serv(net);
  657. if (error)
  658. goto out;
  659. nfsd_up_before = nn->nfsd_net_up;
  660. error = nfsd_startup_net(nrservs, net, cred);
  661. if (error)
  662. goto out_destroy;
  663. error = nn->nfsd_serv->sv_ops->svo_setup(nn->nfsd_serv,
  664. NULL, nrservs);
  665. if (error)
  666. goto out_shutdown;
  667. /* We are holding a reference to nn->nfsd_serv which
  668. * we don't want to count in the return value,
  669. * so subtract 1
  670. */
  671. error = nn->nfsd_serv->sv_nrthreads - 1;
  672. out_shutdown:
  673. if (error < 0 && !nfsd_up_before)
  674. nfsd_shutdown_net(net);
  675. out_destroy:
  676. nfsd_destroy(net); /* Release server */
  677. out:
  678. mutex_unlock(&nfsd_mutex);
  679. return error;
  680. }
  681. #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
  682. static bool
  683. nfsd_support_acl_version(int vers)
  684. {
  685. if (vers >= NFSD_ACL_MINVERS && vers < NFSD_ACL_NRVERS)
  686. return nfsd_acl_version[vers] != NULL;
  687. return false;
  688. }
  689. static int
  690. nfsd_acl_rpcbind_set(struct net *net, const struct svc_program *progp,
  691. u32 version, int family, unsigned short proto,
  692. unsigned short port)
  693. {
  694. if (!nfsd_support_acl_version(version) ||
  695. !nfsd_vers(net_generic(net, nfsd_net_id), version, NFSD_TEST))
  696. return 0;
  697. return svc_generic_rpcbind_set(net, progp, version, family,
  698. proto, port);
  699. }
  700. static __be32
  701. nfsd_acl_init_request(struct svc_rqst *rqstp,
  702. const struct svc_program *progp,
  703. struct svc_process_info *ret)
  704. {
  705. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  706. int i;
  707. if (likely(nfsd_support_acl_version(rqstp->rq_vers) &&
  708. nfsd_vers(nn, rqstp->rq_vers, NFSD_TEST)))
  709. return svc_generic_init_request(rqstp, progp, ret);
  710. ret->mismatch.lovers = NFSD_ACL_NRVERS;
  711. for (i = NFSD_ACL_MINVERS; i < NFSD_ACL_NRVERS; i++) {
  712. if (nfsd_support_acl_version(rqstp->rq_vers) &&
  713. nfsd_vers(nn, i, NFSD_TEST)) {
  714. ret->mismatch.lovers = i;
  715. break;
  716. }
  717. }
  718. if (ret->mismatch.lovers == NFSD_ACL_NRVERS)
  719. return rpc_prog_unavail;
  720. ret->mismatch.hivers = NFSD_ACL_MINVERS;
  721. for (i = NFSD_ACL_NRVERS - 1; i >= NFSD_ACL_MINVERS; i--) {
  722. if (nfsd_support_acl_version(rqstp->rq_vers) &&
  723. nfsd_vers(nn, i, NFSD_TEST)) {
  724. ret->mismatch.hivers = i;
  725. break;
  726. }
  727. }
  728. return rpc_prog_mismatch;
  729. }
  730. #endif
  731. static int
  732. nfsd_rpcbind_set(struct net *net, const struct svc_program *progp,
  733. u32 version, int family, unsigned short proto,
  734. unsigned short port)
  735. {
  736. if (!nfsd_vers(net_generic(net, nfsd_net_id), version, NFSD_TEST))
  737. return 0;
  738. return svc_generic_rpcbind_set(net, progp, version, family,
  739. proto, port);
  740. }
  741. static __be32
  742. nfsd_init_request(struct svc_rqst *rqstp,
  743. const struct svc_program *progp,
  744. struct svc_process_info *ret)
  745. {
  746. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  747. int i;
  748. if (likely(nfsd_vers(nn, rqstp->rq_vers, NFSD_TEST)))
  749. return svc_generic_init_request(rqstp, progp, ret);
  750. ret->mismatch.lovers = NFSD_NRVERS;
  751. for (i = NFSD_MINVERS; i < NFSD_NRVERS; i++) {
  752. if (nfsd_vers(nn, i, NFSD_TEST)) {
  753. ret->mismatch.lovers = i;
  754. break;
  755. }
  756. }
  757. if (ret->mismatch.lovers == NFSD_NRVERS)
  758. return rpc_prog_unavail;
  759. ret->mismatch.hivers = NFSD_MINVERS;
  760. for (i = NFSD_NRVERS - 1; i >= NFSD_MINVERS; i--) {
  761. if (nfsd_vers(nn, i, NFSD_TEST)) {
  762. ret->mismatch.hivers = i;
  763. break;
  764. }
  765. }
  766. return rpc_prog_mismatch;
  767. }
  768. /*
  769. * This is the NFS server kernel thread
  770. */
  771. static int
  772. nfsd(void *vrqstp)
  773. {
  774. struct svc_rqst *rqstp = (struct svc_rqst *) vrqstp;
  775. struct svc_xprt *perm_sock = list_entry(rqstp->rq_server->sv_permsocks.next, typeof(struct svc_xprt), xpt_list);
  776. struct net *net = perm_sock->xpt_net;
  777. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  778. int err;
  779. /* Lock module and set up kernel thread */
  780. mutex_lock(&nfsd_mutex);
  781. /* At this point, the thread shares current->fs
  782. * with the init process. We need to create files with the
  783. * umask as defined by the client instead of init's umask. */
  784. if (unshare_fs_struct() < 0) {
  785. printk("Unable to start nfsd thread: out of memory\n");
  786. goto out;
  787. }
  788. current->fs->umask = 0;
  789. /*
  790. * thread is spawned with all signals set to SIG_IGN, re-enable
  791. * the ones that will bring down the thread
  792. */
  793. allow_signal(SIGKILL);
  794. allow_signal(SIGHUP);
  795. allow_signal(SIGINT);
  796. allow_signal(SIGQUIT);
  797. nfsdstats.th_cnt++;
  798. mutex_unlock(&nfsd_mutex);
  799. set_freezable();
  800. /*
  801. * The main request loop
  802. */
  803. for (;;) {
  804. /* Update sv_maxconn if it has changed */
  805. rqstp->rq_server->sv_maxconn = nn->max_connections;
  806. /*
  807. * Find a socket with data available and call its
  808. * recvfrom routine.
  809. */
  810. while ((err = svc_recv(rqstp, 60*60*HZ)) == -EAGAIN)
  811. ;
  812. if (err == -EINTR)
  813. break;
  814. validate_process_creds();
  815. svc_process(rqstp);
  816. validate_process_creds();
  817. }
  818. /* Clear signals before calling svc_exit_thread() */
  819. flush_signals(current);
  820. mutex_lock(&nfsd_mutex);
  821. nfsdstats.th_cnt --;
  822. out:
  823. rqstp->rq_server = NULL;
  824. /* Release the thread */
  825. svc_exit_thread(rqstp);
  826. nfsd_destroy(net);
  827. /* Release module */
  828. mutex_unlock(&nfsd_mutex);
  829. module_put_and_exit(0);
  830. return 0;
  831. }
  832. /*
  833. * A write procedure can have a large argument, and a read procedure can
  834. * have a large reply, but no NFSv2 or NFSv3 procedure has argument and
  835. * reply that can both be larger than a page. The xdr code has taken
  836. * advantage of this assumption to be a sloppy about bounds checking in
  837. * some cases. Pending a rewrite of the NFSv2/v3 xdr code to fix that
  838. * problem, we enforce these assumptions here:
  839. */
  840. static bool nfs_request_too_big(struct svc_rqst *rqstp,
  841. const struct svc_procedure *proc)
  842. {
  843. /*
  844. * The ACL code has more careful bounds-checking and is not
  845. * susceptible to this problem:
  846. */
  847. if (rqstp->rq_prog != NFS_PROGRAM)
  848. return false;
  849. /*
  850. * Ditto NFSv4 (which can in theory have argument and reply both
  851. * more than a page):
  852. */
  853. if (rqstp->rq_vers >= 4)
  854. return false;
  855. /* The reply will be small, we're OK: */
  856. if (proc->pc_xdrressize > 0 &&
  857. proc->pc_xdrressize < XDR_QUADLEN(PAGE_SIZE))
  858. return false;
  859. return rqstp->rq_arg.len > PAGE_SIZE;
  860. }
  861. /**
  862. * nfsd_dispatch - Process an NFS or NFSACL Request
  863. * @rqstp: incoming request
  864. * @statp: pointer to location of accept_stat field in RPC Reply buffer
  865. *
  866. * This RPC dispatcher integrates the NFS server's duplicate reply cache.
  867. *
  868. * Return values:
  869. * %0: Processing complete; do not send a Reply
  870. * %1: Processing complete; send Reply in rqstp->rq_res
  871. */
  872. int nfsd_dispatch(struct svc_rqst *rqstp, __be32 *statp)
  873. {
  874. const struct svc_procedure *proc = rqstp->rq_procinfo;
  875. struct kvec *argv = &rqstp->rq_arg.head[0];
  876. struct kvec *resv = &rqstp->rq_res.head[0];
  877. __be32 *p;
  878. dprintk("nfsd_dispatch: vers %d proc %d\n",
  879. rqstp->rq_vers, rqstp->rq_proc);
  880. if (nfs_request_too_big(rqstp, proc))
  881. goto out_too_large;
  882. /*
  883. * Give the xdr decoder a chance to change this if it wants
  884. * (necessary in the NFSv4.0 compound case)
  885. */
  886. rqstp->rq_cachetype = proc->pc_cachetype;
  887. if (!proc->pc_decode(rqstp, argv->iov_base))
  888. goto out_decode_err;
  889. switch (nfsd_cache_lookup(rqstp)) {
  890. case RC_DOIT:
  891. break;
  892. case RC_REPLY:
  893. goto out_cached_reply;
  894. case RC_DROPIT:
  895. goto out_dropit;
  896. }
  897. /*
  898. * Need to grab the location to store the status, as
  899. * NFSv4 does some encoding while processing
  900. */
  901. p = resv->iov_base + resv->iov_len;
  902. resv->iov_len += sizeof(__be32);
  903. *statp = proc->pc_func(rqstp);
  904. if (*statp == rpc_drop_reply || test_bit(RQ_DROPME, &rqstp->rq_flags))
  905. goto out_update_drop;
  906. if (!proc->pc_encode(rqstp, p))
  907. goto out_encode_err;
  908. nfsd_cache_update(rqstp, rqstp->rq_cachetype, statp + 1);
  909. out_cached_reply:
  910. return 1;
  911. out_too_large:
  912. dprintk("nfsd: NFSv%d argument too large\n", rqstp->rq_vers);
  913. *statp = rpc_garbage_args;
  914. return 1;
  915. out_decode_err:
  916. dprintk("nfsd: failed to decode arguments!\n");
  917. *statp = rpc_garbage_args;
  918. return 1;
  919. out_update_drop:
  920. dprintk("nfsd: Dropping request; may be revisited later\n");
  921. nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
  922. out_dropit:
  923. return 0;
  924. out_encode_err:
  925. dprintk("nfsd: failed to encode result!\n");
  926. nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
  927. *statp = rpc_system_err;
  928. return 1;
  929. }
  930. int nfsd_pool_stats_open(struct inode *inode, struct file *file)
  931. {
  932. int ret;
  933. struct nfsd_net *nn = net_generic(inode->i_sb->s_fs_info, nfsd_net_id);
  934. mutex_lock(&nfsd_mutex);
  935. if (nn->nfsd_serv == NULL) {
  936. mutex_unlock(&nfsd_mutex);
  937. return -ENODEV;
  938. }
  939. /* bump up the psudo refcount while traversing */
  940. svc_get(nn->nfsd_serv);
  941. ret = svc_pool_stats_open(nn->nfsd_serv, file);
  942. mutex_unlock(&nfsd_mutex);
  943. return ret;
  944. }
  945. int nfsd_pool_stats_release(struct inode *inode, struct file *file)
  946. {
  947. int ret = seq_release(inode, file);
  948. struct net *net = inode->i_sb->s_fs_info;
  949. mutex_lock(&nfsd_mutex);
  950. /* this function really, really should have been called svc_put() */
  951. nfsd_destroy(net);
  952. mutex_unlock(&nfsd_mutex);
  953. return ret;
  954. }