client.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074
  1. /* client.c: NFS client sharing and management code
  2. *
  3. * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/sched.h>
  14. #include <linux/time.h>
  15. #include <linux/kernel.h>
  16. #include <linux/mm.h>
  17. #include <linux/string.h>
  18. #include <linux/stat.h>
  19. #include <linux/errno.h>
  20. #include <linux/unistd.h>
  21. #include <linux/sunrpc/clnt.h>
  22. #include <linux/sunrpc/stats.h>
  23. #include <linux/sunrpc/metrics.h>
  24. #include <linux/sunrpc/xprtsock.h>
  25. #include <linux/sunrpc/xprtrdma.h>
  26. #include <linux/nfs_fs.h>
  27. #include <linux/nfs_mount.h>
  28. #include <linux/nfs4_mount.h>
  29. #include <linux/lockd/bind.h>
  30. #include <linux/seq_file.h>
  31. #include <linux/mount.h>
  32. #include <linux/nfs_idmap.h>
  33. #include <linux/vfs.h>
  34. #include <linux/inet.h>
  35. #include <linux/in6.h>
  36. #include <linux/slab.h>
  37. #include <net/ipv6.h>
  38. #include <linux/nfs_xdr.h>
  39. #include <linux/sunrpc/bc_xprt.h>
  40. #include <linux/nsproxy.h>
  41. #include <linux/pid_namespace.h>
  42. #include <asm/system.h>
  43. #include "nfs4_fs.h"
  44. #include "callback.h"
  45. #include "delegation.h"
  46. #include "iostat.h"
  47. #include "internal.h"
  48. #include "fscache.h"
  49. #include "pnfs.h"
  50. #include "netns.h"
  51. #define NFSDBG_FACILITY NFSDBG_CLIENT
  52. static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
  53. #ifdef CONFIG_NFS_V4
  54. /*
  55. * Get a unique NFSv4.0 callback identifier which will be used
  56. * by the V4.0 callback service to lookup the nfs_client struct
  57. */
  58. static int nfs_get_cb_ident_idr(struct nfs_client *clp, int minorversion)
  59. {
  60. int ret = 0;
  61. struct nfs_net *nn = net_generic(clp->net, nfs_net_id);
  62. if (clp->rpc_ops->version != 4 || minorversion != 0)
  63. return ret;
  64. retry:
  65. if (!idr_pre_get(&nn->cb_ident_idr, GFP_KERNEL))
  66. return -ENOMEM;
  67. spin_lock(&nn->nfs_client_lock);
  68. ret = idr_get_new(&nn->cb_ident_idr, clp, &clp->cl_cb_ident);
  69. spin_unlock(&nn->nfs_client_lock);
  70. if (ret == -EAGAIN)
  71. goto retry;
  72. return ret;
  73. }
  74. #endif /* CONFIG_NFS_V4 */
  75. /*
  76. * Turn off NFSv4 uid/gid mapping when using AUTH_SYS
  77. */
  78. static bool nfs4_disable_idmapping = true;
  79. /*
  80. * RPC cruft for NFS
  81. */
  82. static const struct rpc_version *nfs_version[5] = {
  83. [2] = &nfs_version2,
  84. #ifdef CONFIG_NFS_V3
  85. [3] = &nfs_version3,
  86. #endif
  87. #ifdef CONFIG_NFS_V4
  88. [4] = &nfs_version4,
  89. #endif
  90. };
  91. const struct rpc_program nfs_program = {
  92. .name = "nfs",
  93. .number = NFS_PROGRAM,
  94. .nrvers = ARRAY_SIZE(nfs_version),
  95. .version = nfs_version,
  96. .stats = &nfs_rpcstat,
  97. .pipe_dir_name = NFS_PIPE_DIRNAME,
  98. };
  99. struct rpc_stat nfs_rpcstat = {
  100. .program = &nfs_program
  101. };
  102. #ifdef CONFIG_NFS_V3_ACL
  103. static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program };
  104. static const struct rpc_version *nfsacl_version[] = {
  105. [3] = &nfsacl_version3,
  106. };
  107. const struct rpc_program nfsacl_program = {
  108. .name = "nfsacl",
  109. .number = NFS_ACL_PROGRAM,
  110. .nrvers = ARRAY_SIZE(nfsacl_version),
  111. .version = nfsacl_version,
  112. .stats = &nfsacl_rpcstat,
  113. };
  114. #endif /* CONFIG_NFS_V3_ACL */
  115. struct nfs_client_initdata {
  116. const char *hostname;
  117. const struct sockaddr *addr;
  118. size_t addrlen;
  119. const struct nfs_rpc_ops *rpc_ops;
  120. int proto;
  121. u32 minorversion;
  122. struct net *net;
  123. };
  124. /*
  125. * Allocate a shared client record
  126. *
  127. * Since these are allocated/deallocated very rarely, we don't
  128. * bother putting them in a slab cache...
  129. */
  130. static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
  131. {
  132. struct nfs_client *clp;
  133. struct rpc_cred *cred;
  134. int err = -ENOMEM;
  135. if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
  136. goto error_0;
  137. clp->rpc_ops = cl_init->rpc_ops;
  138. atomic_set(&clp->cl_count, 1);
  139. clp->cl_cons_state = NFS_CS_INITING;
  140. memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
  141. clp->cl_addrlen = cl_init->addrlen;
  142. if (cl_init->hostname) {
  143. err = -ENOMEM;
  144. clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
  145. if (!clp->cl_hostname)
  146. goto error_cleanup;
  147. }
  148. INIT_LIST_HEAD(&clp->cl_superblocks);
  149. clp->cl_rpcclient = ERR_PTR(-EINVAL);
  150. clp->cl_proto = cl_init->proto;
  151. clp->net = get_net(cl_init->net);
  152. #ifdef CONFIG_NFS_V4
  153. err = nfs_get_cb_ident_idr(clp, cl_init->minorversion);
  154. if (err)
  155. goto error_cleanup;
  156. spin_lock_init(&clp->cl_lock);
  157. INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
  158. rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
  159. clp->cl_boot_time = CURRENT_TIME;
  160. clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
  161. clp->cl_minorversion = cl_init->minorversion;
  162. clp->cl_mvops = nfs_v4_minor_ops[cl_init->minorversion];
  163. #endif
  164. cred = rpc_lookup_machine_cred("*");
  165. if (!IS_ERR(cred))
  166. clp->cl_machine_cred = cred;
  167. nfs_fscache_get_client_cookie(clp);
  168. return clp;
  169. error_cleanup:
  170. kfree(clp);
  171. error_0:
  172. return ERR_PTR(err);
  173. }
  174. #ifdef CONFIG_NFS_V4
  175. #ifdef CONFIG_NFS_V4_1
  176. static void nfs4_shutdown_session(struct nfs_client *clp)
  177. {
  178. if (nfs4_has_session(clp)) {
  179. nfs4_deviceid_purge_client(clp);
  180. nfs4_destroy_session(clp->cl_session);
  181. }
  182. }
  183. #else /* CONFIG_NFS_V4_1 */
  184. static void nfs4_shutdown_session(struct nfs_client *clp)
  185. {
  186. }
  187. #endif /* CONFIG_NFS_V4_1 */
  188. /*
  189. * Destroy the NFS4 callback service
  190. */
  191. static void nfs4_destroy_callback(struct nfs_client *clp)
  192. {
  193. if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
  194. nfs_callback_down(clp->cl_mvops->minor_version);
  195. }
  196. static void nfs4_shutdown_client(struct nfs_client *clp)
  197. {
  198. if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
  199. nfs4_kill_renewd(clp);
  200. nfs4_shutdown_session(clp);
  201. nfs4_destroy_callback(clp);
  202. if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
  203. nfs_idmap_delete(clp);
  204. rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
  205. }
  206. /* idr_remove_all is not needed as all id's are removed by nfs_put_client */
  207. void nfs_cleanup_cb_ident_idr(struct net *net)
  208. {
  209. struct nfs_net *nn = net_generic(net, nfs_net_id);
  210. idr_destroy(&nn->cb_ident_idr);
  211. }
  212. /* nfs_client_lock held */
  213. static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
  214. {
  215. struct nfs_net *nn = net_generic(clp->net, nfs_net_id);
  216. if (clp->cl_cb_ident)
  217. idr_remove(&nn->cb_ident_idr, clp->cl_cb_ident);
  218. }
  219. static void pnfs_init_server(struct nfs_server *server)
  220. {
  221. rpc_init_wait_queue(&server->roc_rpcwaitq, "pNFS ROC");
  222. }
  223. static void nfs4_destroy_server(struct nfs_server *server)
  224. {
  225. nfs4_purge_state_owners(server);
  226. }
  227. #else
  228. static void nfs4_shutdown_client(struct nfs_client *clp)
  229. {
  230. }
  231. void nfs_cleanup_cb_ident_idr(struct net *net)
  232. {
  233. }
  234. static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
  235. {
  236. }
  237. static void pnfs_init_server(struct nfs_server *server)
  238. {
  239. }
  240. #endif /* CONFIG_NFS_V4 */
  241. /*
  242. * Destroy a shared client record
  243. */
  244. static void nfs_free_client(struct nfs_client *clp)
  245. {
  246. dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
  247. nfs4_shutdown_client(clp);
  248. nfs_fscache_release_client_cookie(clp);
  249. /* -EIO all pending I/O */
  250. if (!IS_ERR(clp->cl_rpcclient))
  251. rpc_shutdown_client(clp->cl_rpcclient);
  252. if (clp->cl_machine_cred != NULL)
  253. put_rpccred(clp->cl_machine_cred);
  254. put_net(clp->net);
  255. kfree(clp->cl_hostname);
  256. kfree(clp->server_scope);
  257. kfree(clp->impl_id);
  258. kfree(clp);
  259. dprintk("<-- nfs_free_client()\n");
  260. }
  261. /*
  262. * Release a reference to a shared client record
  263. */
  264. void nfs_put_client(struct nfs_client *clp)
  265. {
  266. struct nfs_net *nn;
  267. if (!clp)
  268. return;
  269. dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
  270. nn = net_generic(clp->net, nfs_net_id);
  271. if (atomic_dec_and_lock(&clp->cl_count, &nn->nfs_client_lock)) {
  272. list_del(&clp->cl_share_link);
  273. nfs_cb_idr_remove_locked(clp);
  274. spin_unlock(&nn->nfs_client_lock);
  275. BUG_ON(!list_empty(&clp->cl_superblocks));
  276. nfs_free_client(clp);
  277. }
  278. }
  279. EXPORT_SYMBOL_GPL(nfs_put_client);
  280. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  281. /*
  282. * Test if two ip6 socket addresses refer to the same socket by
  283. * comparing relevant fields. The padding bytes specifically, are not
  284. * compared. sin6_flowinfo is not compared because it only affects QoS
  285. * and sin6_scope_id is only compared if the address is "link local"
  286. * because "link local" addresses need only be unique to a specific
  287. * link. Conversely, ordinary unicast addresses might have different
  288. * sin6_scope_id.
  289. *
  290. * The caller should ensure both socket addresses are AF_INET6.
  291. */
  292. static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
  293. const struct sockaddr *sa2)
  294. {
  295. const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
  296. const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
  297. if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
  298. return 0;
  299. else if (ipv6_addr_type(&sin1->sin6_addr) & IPV6_ADDR_LINKLOCAL)
  300. return sin1->sin6_scope_id == sin2->sin6_scope_id;
  301. return 1;
  302. }
  303. #else /* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */
  304. static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
  305. const struct sockaddr *sa2)
  306. {
  307. return 0;
  308. }
  309. #endif
  310. /*
  311. * Test if two ip4 socket addresses refer to the same socket, by
  312. * comparing relevant fields. The padding bytes specifically, are
  313. * not compared.
  314. *
  315. * The caller should ensure both socket addresses are AF_INET.
  316. */
  317. static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1,
  318. const struct sockaddr *sa2)
  319. {
  320. const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
  321. const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
  322. return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
  323. }
  324. static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1,
  325. const struct sockaddr *sa2)
  326. {
  327. const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
  328. const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
  329. return nfs_sockaddr_match_ipaddr6(sa1, sa2) &&
  330. (sin1->sin6_port == sin2->sin6_port);
  331. }
  332. static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
  333. const struct sockaddr *sa2)
  334. {
  335. const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
  336. const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
  337. return nfs_sockaddr_match_ipaddr4(sa1, sa2) &&
  338. (sin1->sin_port == sin2->sin_port);
  339. }
  340. /*
  341. * Test if two socket addresses represent the same actual socket,
  342. * by comparing (only) relevant fields, excluding the port number.
  343. */
  344. static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
  345. const struct sockaddr *sa2)
  346. {
  347. if (sa1->sa_family != sa2->sa_family)
  348. return 0;
  349. switch (sa1->sa_family) {
  350. case AF_INET:
  351. return nfs_sockaddr_match_ipaddr4(sa1, sa2);
  352. case AF_INET6:
  353. return nfs_sockaddr_match_ipaddr6(sa1, sa2);
  354. }
  355. return 0;
  356. }
  357. /*
  358. * Test if two socket addresses represent the same actual socket,
  359. * by comparing (only) relevant fields, including the port number.
  360. */
  361. static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
  362. const struct sockaddr *sa2)
  363. {
  364. if (sa1->sa_family != sa2->sa_family)
  365. return 0;
  366. switch (sa1->sa_family) {
  367. case AF_INET:
  368. return nfs_sockaddr_cmp_ip4(sa1, sa2);
  369. case AF_INET6:
  370. return nfs_sockaddr_cmp_ip6(sa1, sa2);
  371. }
  372. return 0;
  373. }
  374. /* Common match routine for v4.0 and v4.1 callback services */
  375. bool
  376. nfs4_cb_match_client(const struct sockaddr *addr, struct nfs_client *clp,
  377. u32 minorversion)
  378. {
  379. struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
  380. /* Don't match clients that failed to initialise */
  381. if (!(clp->cl_cons_state == NFS_CS_READY ||
  382. clp->cl_cons_state == NFS_CS_SESSION_INITING))
  383. return false;
  384. /* Match the version and minorversion */
  385. if (clp->rpc_ops->version != 4 ||
  386. clp->cl_minorversion != minorversion)
  387. return false;
  388. /* Match only the IP address, not the port number */
  389. if (!nfs_sockaddr_match_ipaddr(addr, clap))
  390. return false;
  391. return true;
  392. }
  393. /*
  394. * Find an nfs_client on the list that matches the initialisation data
  395. * that is supplied.
  396. */
  397. static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
  398. {
  399. struct nfs_client *clp;
  400. const struct sockaddr *sap = data->addr;
  401. struct nfs_net *nn = net_generic(data->net, nfs_net_id);
  402. list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
  403. const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
  404. /* Don't match clients that failed to initialise properly */
  405. if (clp->cl_cons_state < 0)
  406. continue;
  407. /* Different NFS versions cannot share the same nfs_client */
  408. if (clp->rpc_ops != data->rpc_ops)
  409. continue;
  410. if (clp->cl_proto != data->proto)
  411. continue;
  412. /* Match nfsv4 minorversion */
  413. if (clp->cl_minorversion != data->minorversion)
  414. continue;
  415. /* Match the full socket address */
  416. if (!nfs_sockaddr_cmp(sap, clap))
  417. continue;
  418. atomic_inc(&clp->cl_count);
  419. return clp;
  420. }
  421. return NULL;
  422. }
  423. /*
  424. * Look up a client by IP address and protocol version
  425. * - creates a new record if one doesn't yet exist
  426. */
  427. static struct nfs_client *
  428. nfs_get_client(const struct nfs_client_initdata *cl_init,
  429. const struct rpc_timeout *timeparms,
  430. const char *ip_addr,
  431. rpc_authflavor_t authflavour,
  432. int noresvport)
  433. {
  434. struct nfs_client *clp, *new = NULL;
  435. int error;
  436. struct nfs_net *nn = net_generic(cl_init->net, nfs_net_id);
  437. dprintk("--> nfs_get_client(%s,v%u)\n",
  438. cl_init->hostname ?: "", cl_init->rpc_ops->version);
  439. /* see if the client already exists */
  440. do {
  441. spin_lock(&nn->nfs_client_lock);
  442. clp = nfs_match_client(cl_init);
  443. if (clp)
  444. goto found_client;
  445. if (new)
  446. goto install_client;
  447. spin_unlock(&nn->nfs_client_lock);
  448. new = nfs_alloc_client(cl_init);
  449. } while (!IS_ERR(new));
  450. dprintk("--> nfs_get_client() = %ld [failed]\n", PTR_ERR(new));
  451. return new;
  452. /* install a new client and return with it unready */
  453. install_client:
  454. clp = new;
  455. list_add(&clp->cl_share_link, &nn->nfs_client_list);
  456. spin_unlock(&nn->nfs_client_lock);
  457. error = cl_init->rpc_ops->init_client(clp, timeparms, ip_addr,
  458. authflavour, noresvport);
  459. if (error < 0) {
  460. nfs_put_client(clp);
  461. return ERR_PTR(error);
  462. }
  463. dprintk("--> nfs_get_client() = %p [new]\n", clp);
  464. return clp;
  465. /* found an existing client
  466. * - make sure it's ready before returning
  467. */
  468. found_client:
  469. spin_unlock(&nn->nfs_client_lock);
  470. if (new)
  471. nfs_free_client(new);
  472. error = wait_event_killable(nfs_client_active_wq,
  473. clp->cl_cons_state < NFS_CS_INITING);
  474. if (error < 0) {
  475. nfs_put_client(clp);
  476. return ERR_PTR(-ERESTARTSYS);
  477. }
  478. if (clp->cl_cons_state < NFS_CS_READY) {
  479. error = clp->cl_cons_state;
  480. nfs_put_client(clp);
  481. return ERR_PTR(error);
  482. }
  483. BUG_ON(clp->cl_cons_state != NFS_CS_READY);
  484. dprintk("--> nfs_get_client() = %p [share]\n", clp);
  485. return clp;
  486. }
  487. /*
  488. * Mark a server as ready or failed
  489. */
  490. void nfs_mark_client_ready(struct nfs_client *clp, int state)
  491. {
  492. clp->cl_cons_state = state;
  493. wake_up_all(&nfs_client_active_wq);
  494. }
  495. /*
  496. * With sessions, the client is not marked ready until after a
  497. * successful EXCHANGE_ID and CREATE_SESSION.
  498. *
  499. * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
  500. * other versions of NFS can be tried.
  501. */
  502. int nfs4_check_client_ready(struct nfs_client *clp)
  503. {
  504. if (!nfs4_has_session(clp))
  505. return 0;
  506. if (clp->cl_cons_state < NFS_CS_READY)
  507. return -EPROTONOSUPPORT;
  508. return 0;
  509. }
  510. /*
  511. * Initialise the timeout values for a connection
  512. */
  513. static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
  514. unsigned int timeo, unsigned int retrans)
  515. {
  516. to->to_initval = timeo * HZ / 10;
  517. to->to_retries = retrans;
  518. switch (proto) {
  519. case XPRT_TRANSPORT_TCP:
  520. case XPRT_TRANSPORT_RDMA:
  521. if (to->to_retries == 0)
  522. to->to_retries = NFS_DEF_TCP_RETRANS;
  523. if (to->to_initval == 0)
  524. to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
  525. if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
  526. to->to_initval = NFS_MAX_TCP_TIMEOUT;
  527. to->to_increment = to->to_initval;
  528. to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
  529. if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
  530. to->to_maxval = NFS_MAX_TCP_TIMEOUT;
  531. if (to->to_maxval < to->to_initval)
  532. to->to_maxval = to->to_initval;
  533. to->to_exponential = 0;
  534. break;
  535. case XPRT_TRANSPORT_UDP:
  536. if (to->to_retries == 0)
  537. to->to_retries = NFS_DEF_UDP_RETRANS;
  538. if (!to->to_initval)
  539. to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
  540. if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
  541. to->to_initval = NFS_MAX_UDP_TIMEOUT;
  542. to->to_maxval = NFS_MAX_UDP_TIMEOUT;
  543. to->to_exponential = 1;
  544. break;
  545. default:
  546. BUG();
  547. }
  548. }
  549. /*
  550. * Create an RPC client handle
  551. */
  552. static int nfs_create_rpc_client(struct nfs_client *clp,
  553. const struct rpc_timeout *timeparms,
  554. rpc_authflavor_t flavor,
  555. int discrtry, int noresvport)
  556. {
  557. struct rpc_clnt *clnt = NULL;
  558. struct rpc_create_args args = {
  559. .net = clp->net,
  560. .protocol = clp->cl_proto,
  561. .address = (struct sockaddr *)&clp->cl_addr,
  562. .addrsize = clp->cl_addrlen,
  563. .timeout = timeparms,
  564. .servername = clp->cl_hostname,
  565. .program = &nfs_program,
  566. .version = clp->rpc_ops->version,
  567. .authflavor = flavor,
  568. };
  569. if (discrtry)
  570. args.flags |= RPC_CLNT_CREATE_DISCRTRY;
  571. if (noresvport)
  572. args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
  573. if (!IS_ERR(clp->cl_rpcclient))
  574. return 0;
  575. clnt = rpc_create(&args);
  576. if (IS_ERR(clnt)) {
  577. dprintk("%s: cannot create RPC client. Error = %ld\n",
  578. __func__, PTR_ERR(clnt));
  579. return PTR_ERR(clnt);
  580. }
  581. clp->cl_rpcclient = clnt;
  582. return 0;
  583. }
  584. /*
  585. * Version 2 or 3 client destruction
  586. */
  587. static void nfs_destroy_server(struct nfs_server *server)
  588. {
  589. if (!(server->flags & NFS_MOUNT_LOCAL_FLOCK) ||
  590. !(server->flags & NFS_MOUNT_LOCAL_FCNTL))
  591. nlmclnt_done(server->nlm_host);
  592. }
  593. /*
  594. * Version 2 or 3 lockd setup
  595. */
  596. static int nfs_start_lockd(struct nfs_server *server)
  597. {
  598. struct nlm_host *host;
  599. struct nfs_client *clp = server->nfs_client;
  600. struct nlmclnt_initdata nlm_init = {
  601. .hostname = clp->cl_hostname,
  602. .address = (struct sockaddr *)&clp->cl_addr,
  603. .addrlen = clp->cl_addrlen,
  604. .nfs_version = clp->rpc_ops->version,
  605. .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
  606. 1 : 0,
  607. .net = clp->net,
  608. };
  609. if (nlm_init.nfs_version > 3)
  610. return 0;
  611. if ((server->flags & NFS_MOUNT_LOCAL_FLOCK) &&
  612. (server->flags & NFS_MOUNT_LOCAL_FCNTL))
  613. return 0;
  614. switch (clp->cl_proto) {
  615. default:
  616. nlm_init.protocol = IPPROTO_TCP;
  617. break;
  618. case XPRT_TRANSPORT_UDP:
  619. nlm_init.protocol = IPPROTO_UDP;
  620. }
  621. host = nlmclnt_init(&nlm_init);
  622. if (IS_ERR(host))
  623. return PTR_ERR(host);
  624. server->nlm_host = host;
  625. server->destroy = nfs_destroy_server;
  626. return 0;
  627. }
  628. /*
  629. * Initialise an NFSv3 ACL client connection
  630. */
  631. #ifdef CONFIG_NFS_V3_ACL
  632. static void nfs_init_server_aclclient(struct nfs_server *server)
  633. {
  634. if (server->nfs_client->rpc_ops->version != 3)
  635. goto out_noacl;
  636. if (server->flags & NFS_MOUNT_NOACL)
  637. goto out_noacl;
  638. server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
  639. if (IS_ERR(server->client_acl))
  640. goto out_noacl;
  641. /* No errors! Assume that Sun nfsacls are supported */
  642. server->caps |= NFS_CAP_ACLS;
  643. return;
  644. out_noacl:
  645. server->caps &= ~NFS_CAP_ACLS;
  646. }
  647. #else
  648. static inline void nfs_init_server_aclclient(struct nfs_server *server)
  649. {
  650. server->flags &= ~NFS_MOUNT_NOACL;
  651. server->caps &= ~NFS_CAP_ACLS;
  652. }
  653. #endif
  654. /*
  655. * Create a general RPC client
  656. */
  657. static int nfs_init_server_rpcclient(struct nfs_server *server,
  658. const struct rpc_timeout *timeo,
  659. rpc_authflavor_t pseudoflavour)
  660. {
  661. struct nfs_client *clp = server->nfs_client;
  662. server->client = rpc_clone_client(clp->cl_rpcclient);
  663. if (IS_ERR(server->client)) {
  664. dprintk("%s: couldn't create rpc_client!\n", __func__);
  665. return PTR_ERR(server->client);
  666. }
  667. memcpy(&server->client->cl_timeout_default,
  668. timeo,
  669. sizeof(server->client->cl_timeout_default));
  670. server->client->cl_timeout = &server->client->cl_timeout_default;
  671. if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
  672. struct rpc_auth *auth;
  673. auth = rpcauth_create(pseudoflavour, server->client);
  674. if (IS_ERR(auth)) {
  675. dprintk("%s: couldn't create credcache!\n", __func__);
  676. return PTR_ERR(auth);
  677. }
  678. }
  679. server->client->cl_softrtry = 0;
  680. if (server->flags & NFS_MOUNT_SOFT)
  681. server->client->cl_softrtry = 1;
  682. return 0;
  683. }
  684. /*
  685. * Initialise an NFS2 or NFS3 client
  686. */
  687. int nfs_init_client(struct nfs_client *clp, const struct rpc_timeout *timeparms,
  688. const char *ip_addr, rpc_authflavor_t authflavour,
  689. int noresvport)
  690. {
  691. int error;
  692. if (clp->cl_cons_state == NFS_CS_READY) {
  693. /* the client is already initialised */
  694. dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
  695. return 0;
  696. }
  697. /*
  698. * Create a client RPC handle for doing FSSTAT with UNIX auth only
  699. * - RFC 2623, sec 2.3.2
  700. */
  701. error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX,
  702. 0, noresvport);
  703. if (error < 0)
  704. goto error;
  705. nfs_mark_client_ready(clp, NFS_CS_READY);
  706. return 0;
  707. error:
  708. nfs_mark_client_ready(clp, error);
  709. dprintk("<-- nfs_init_client() = xerror %d\n", error);
  710. return error;
  711. }
  712. /*
  713. * Create a version 2 or 3 client
  714. */
  715. static int nfs_init_server(struct nfs_server *server,
  716. const struct nfs_parsed_mount_data *data)
  717. {
  718. struct nfs_client_initdata cl_init = {
  719. .hostname = data->nfs_server.hostname,
  720. .addr = (const struct sockaddr *)&data->nfs_server.address,
  721. .addrlen = data->nfs_server.addrlen,
  722. .rpc_ops = &nfs_v2_clientops,
  723. .proto = data->nfs_server.protocol,
  724. .net = data->net,
  725. };
  726. struct rpc_timeout timeparms;
  727. struct nfs_client *clp;
  728. int error;
  729. dprintk("--> nfs_init_server()\n");
  730. #ifdef CONFIG_NFS_V3
  731. if (data->version == 3)
  732. cl_init.rpc_ops = &nfs_v3_clientops;
  733. #endif
  734. nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
  735. data->timeo, data->retrans);
  736. /* Allocate or find a client reference we can use */
  737. clp = nfs_get_client(&cl_init, &timeparms, NULL, RPC_AUTH_UNIX,
  738. data->flags & NFS_MOUNT_NORESVPORT);
  739. if (IS_ERR(clp)) {
  740. dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
  741. return PTR_ERR(clp);
  742. }
  743. server->nfs_client = clp;
  744. /* Initialise the client representation from the mount data */
  745. server->flags = data->flags;
  746. server->options = data->options;
  747. server->caps |= NFS_CAP_HARDLINKS|NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
  748. NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|NFS_CAP_OWNER_GROUP|
  749. NFS_CAP_ATIME|NFS_CAP_CTIME|NFS_CAP_MTIME;
  750. if (data->rsize)
  751. server->rsize = nfs_block_size(data->rsize, NULL);
  752. if (data->wsize)
  753. server->wsize = nfs_block_size(data->wsize, NULL);
  754. server->acregmin = data->acregmin * HZ;
  755. server->acregmax = data->acregmax * HZ;
  756. server->acdirmin = data->acdirmin * HZ;
  757. server->acdirmax = data->acdirmax * HZ;
  758. /* Start lockd here, before we might error out */
  759. error = nfs_start_lockd(server);
  760. if (error < 0)
  761. goto error;
  762. server->port = data->nfs_server.port;
  763. error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
  764. if (error < 0)
  765. goto error;
  766. /* Preserve the values of mount_server-related mount options */
  767. if (data->mount_server.addrlen) {
  768. memcpy(&server->mountd_address, &data->mount_server.address,
  769. data->mount_server.addrlen);
  770. server->mountd_addrlen = data->mount_server.addrlen;
  771. }
  772. server->mountd_version = data->mount_server.version;
  773. server->mountd_port = data->mount_server.port;
  774. server->mountd_protocol = data->mount_server.protocol;
  775. server->namelen = data->namlen;
  776. /* Create a client RPC handle for the NFSv3 ACL management interface */
  777. nfs_init_server_aclclient(server);
  778. dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
  779. return 0;
  780. error:
  781. server->nfs_client = NULL;
  782. nfs_put_client(clp);
  783. dprintk("<-- nfs_init_server() = xerror %d\n", error);
  784. return error;
  785. }
  786. /*
  787. * Load up the server record from information gained in an fsinfo record
  788. */
  789. static void nfs_server_set_fsinfo(struct nfs_server *server,
  790. struct nfs_fh *mntfh,
  791. struct nfs_fsinfo *fsinfo)
  792. {
  793. unsigned long max_rpc_payload;
  794. /* Work out a lot of parameters */
  795. if (server->rsize == 0)
  796. server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
  797. if (server->wsize == 0)
  798. server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
  799. if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
  800. server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
  801. if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
  802. server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
  803. max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
  804. if (server->rsize > max_rpc_payload)
  805. server->rsize = max_rpc_payload;
  806. if (server->rsize > NFS_MAX_FILE_IO_SIZE)
  807. server->rsize = NFS_MAX_FILE_IO_SIZE;
  808. server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  809. server->backing_dev_info.name = "nfs";
  810. server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
  811. if (server->wsize > max_rpc_payload)
  812. server->wsize = max_rpc_payload;
  813. if (server->wsize > NFS_MAX_FILE_IO_SIZE)
  814. server->wsize = NFS_MAX_FILE_IO_SIZE;
  815. server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  816. server->pnfs_blksize = fsinfo->blksize;
  817. set_pnfs_layoutdriver(server, mntfh, fsinfo->layouttype);
  818. server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
  819. server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
  820. if (server->dtsize > PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES)
  821. server->dtsize = PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES;
  822. if (server->dtsize > server->rsize)
  823. server->dtsize = server->rsize;
  824. if (server->flags & NFS_MOUNT_NOAC) {
  825. server->acregmin = server->acregmax = 0;
  826. server->acdirmin = server->acdirmax = 0;
  827. }
  828. server->maxfilesize = fsinfo->maxfilesize;
  829. server->time_delta = fsinfo->time_delta;
  830. /* We're airborne Set socket buffersize */
  831. rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
  832. }
  833. /*
  834. * Probe filesystem information, including the FSID on v2/v3
  835. */
  836. static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
  837. {
  838. struct nfs_fsinfo fsinfo;
  839. struct nfs_client *clp = server->nfs_client;
  840. int error;
  841. dprintk("--> nfs_probe_fsinfo()\n");
  842. if (clp->rpc_ops->set_capabilities != NULL) {
  843. error = clp->rpc_ops->set_capabilities(server, mntfh);
  844. if (error < 0)
  845. goto out_error;
  846. }
  847. fsinfo.fattr = fattr;
  848. fsinfo.layouttype = 0;
  849. error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
  850. if (error < 0)
  851. goto out_error;
  852. nfs_server_set_fsinfo(server, mntfh, &fsinfo);
  853. /* Get some general file system info */
  854. if (server->namelen == 0) {
  855. struct nfs_pathconf pathinfo;
  856. pathinfo.fattr = fattr;
  857. nfs_fattr_init(fattr);
  858. if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
  859. server->namelen = pathinfo.max_namelen;
  860. }
  861. dprintk("<-- nfs_probe_fsinfo() = 0\n");
  862. return 0;
  863. out_error:
  864. dprintk("nfs_probe_fsinfo: error = %d\n", -error);
  865. return error;
  866. }
  867. /*
  868. * Copy useful information when duplicating a server record
  869. */
  870. static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
  871. {
  872. target->flags = source->flags;
  873. target->rsize = source->rsize;
  874. target->wsize = source->wsize;
  875. target->acregmin = source->acregmin;
  876. target->acregmax = source->acregmax;
  877. target->acdirmin = source->acdirmin;
  878. target->acdirmax = source->acdirmax;
  879. target->caps = source->caps;
  880. target->options = source->options;
  881. }
  882. static void nfs_server_insert_lists(struct nfs_server *server)
  883. {
  884. struct nfs_client *clp = server->nfs_client;
  885. struct nfs_net *nn = net_generic(clp->net, nfs_net_id);
  886. spin_lock(&nn->nfs_client_lock);
  887. list_add_tail_rcu(&server->client_link, &clp->cl_superblocks);
  888. list_add_tail(&server->master_link, &nn->nfs_volume_list);
  889. clear_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
  890. spin_unlock(&nn->nfs_client_lock);
  891. }
  892. static void nfs_server_remove_lists(struct nfs_server *server)
  893. {
  894. struct nfs_client *clp = server->nfs_client;
  895. struct nfs_net *nn;
  896. if (clp == NULL)
  897. return;
  898. nn = net_generic(clp->net, nfs_net_id);
  899. spin_lock(&nn->nfs_client_lock);
  900. list_del_rcu(&server->client_link);
  901. if (list_empty(&clp->cl_superblocks))
  902. set_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
  903. list_del(&server->master_link);
  904. spin_unlock(&nn->nfs_client_lock);
  905. synchronize_rcu();
  906. }
  907. /*
  908. * Allocate and initialise a server record
  909. */
  910. static struct nfs_server *nfs_alloc_server(void)
  911. {
  912. struct nfs_server *server;
  913. server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
  914. if (!server)
  915. return NULL;
  916. server->client = server->client_acl = ERR_PTR(-EINVAL);
  917. /* Zero out the NFS state stuff */
  918. INIT_LIST_HEAD(&server->client_link);
  919. INIT_LIST_HEAD(&server->master_link);
  920. INIT_LIST_HEAD(&server->delegations);
  921. INIT_LIST_HEAD(&server->layouts);
  922. INIT_LIST_HEAD(&server->state_owners_lru);
  923. atomic_set(&server->active, 0);
  924. server->io_stats = nfs_alloc_iostats();
  925. if (!server->io_stats) {
  926. kfree(server);
  927. return NULL;
  928. }
  929. if (bdi_init(&server->backing_dev_info)) {
  930. nfs_free_iostats(server->io_stats);
  931. kfree(server);
  932. return NULL;
  933. }
  934. ida_init(&server->openowner_id);
  935. ida_init(&server->lockowner_id);
  936. pnfs_init_server(server);
  937. return server;
  938. }
  939. /*
  940. * Free up a server record
  941. */
  942. void nfs_free_server(struct nfs_server *server)
  943. {
  944. dprintk("--> nfs_free_server()\n");
  945. nfs_server_remove_lists(server);
  946. unset_pnfs_layoutdriver(server);
  947. if (server->destroy != NULL)
  948. server->destroy(server);
  949. if (!IS_ERR(server->client_acl))
  950. rpc_shutdown_client(server->client_acl);
  951. if (!IS_ERR(server->client))
  952. rpc_shutdown_client(server->client);
  953. nfs_put_client(server->nfs_client);
  954. ida_destroy(&server->lockowner_id);
  955. ida_destroy(&server->openowner_id);
  956. nfs_free_iostats(server->io_stats);
  957. bdi_destroy(&server->backing_dev_info);
  958. kfree(server);
  959. nfs_release_automount_timer();
  960. dprintk("<-- nfs_free_server()\n");
  961. }
  962. /*
  963. * Create a version 2 or 3 volume record
  964. * - keyed on server and FSID
  965. */
  966. struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
  967. struct nfs_fh *mntfh)
  968. {
  969. struct nfs_server *server;
  970. struct nfs_fattr *fattr;
  971. int error;
  972. server = nfs_alloc_server();
  973. if (!server)
  974. return ERR_PTR(-ENOMEM);
  975. error = -ENOMEM;
  976. fattr = nfs_alloc_fattr();
  977. if (fattr == NULL)
  978. goto error;
  979. /* Get a client representation */
  980. error = nfs_init_server(server, data);
  981. if (error < 0)
  982. goto error;
  983. BUG_ON(!server->nfs_client);
  984. BUG_ON(!server->nfs_client->rpc_ops);
  985. BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
  986. /* Probe the root fh to retrieve its FSID */
  987. error = nfs_probe_fsinfo(server, mntfh, fattr);
  988. if (error < 0)
  989. goto error;
  990. if (server->nfs_client->rpc_ops->version == 3) {
  991. if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
  992. server->namelen = NFS3_MAXNAMLEN;
  993. if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
  994. server->caps |= NFS_CAP_READDIRPLUS;
  995. } else {
  996. if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
  997. server->namelen = NFS2_MAXNAMLEN;
  998. }
  999. if (!(fattr->valid & NFS_ATTR_FATTR)) {
  1000. error = server->nfs_client->rpc_ops->getattr(server, mntfh, fattr);
  1001. if (error < 0) {
  1002. dprintk("nfs_create_server: getattr error = %d\n", -error);
  1003. goto error;
  1004. }
  1005. }
  1006. memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
  1007. dprintk("Server FSID: %llx:%llx\n",
  1008. (unsigned long long) server->fsid.major,
  1009. (unsigned long long) server->fsid.minor);
  1010. nfs_server_insert_lists(server);
  1011. server->mount_time = jiffies;
  1012. nfs_free_fattr(fattr);
  1013. return server;
  1014. error:
  1015. nfs_free_fattr(fattr);
  1016. nfs_free_server(server);
  1017. return ERR_PTR(error);
  1018. }
  1019. #ifdef CONFIG_NFS_V4
  1020. /*
  1021. * NFSv4.0 callback thread helper
  1022. *
  1023. * Find a client by callback identifier
  1024. */
  1025. struct nfs_client *
  1026. nfs4_find_client_ident(struct net *net, int cb_ident)
  1027. {
  1028. struct nfs_client *clp;
  1029. struct nfs_net *nn = net_generic(net, nfs_net_id);
  1030. spin_lock(&nn->nfs_client_lock);
  1031. clp = idr_find(&nn->cb_ident_idr, cb_ident);
  1032. if (clp)
  1033. atomic_inc(&clp->cl_count);
  1034. spin_unlock(&nn->nfs_client_lock);
  1035. return clp;
  1036. }
  1037. #if defined(CONFIG_NFS_V4_1)
  1038. /*
  1039. * NFSv4.1 callback thread helper
  1040. * For CB_COMPOUND calls, find a client by IP address, protocol version,
  1041. * minorversion, and sessionID
  1042. *
  1043. * Returns NULL if no such client
  1044. */
  1045. struct nfs_client *
  1046. nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
  1047. struct nfs4_sessionid *sid)
  1048. {
  1049. struct nfs_client *clp;
  1050. struct nfs_net *nn = net_generic(net, nfs_net_id);
  1051. spin_lock(&nn->nfs_client_lock);
  1052. list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
  1053. if (nfs4_cb_match_client(addr, clp, 1) == false)
  1054. continue;
  1055. if (!nfs4_has_session(clp))
  1056. continue;
  1057. /* Match sessionid*/
  1058. if (memcmp(clp->cl_session->sess_id.data,
  1059. sid->data, NFS4_MAX_SESSIONID_LEN) != 0)
  1060. continue;
  1061. atomic_inc(&clp->cl_count);
  1062. spin_unlock(&nn->nfs_client_lock);
  1063. return clp;
  1064. }
  1065. spin_unlock(&nn->nfs_client_lock);
  1066. return NULL;
  1067. }
  1068. #else /* CONFIG_NFS_V4_1 */
  1069. struct nfs_client *
  1070. nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
  1071. struct nfs4_sessionid *sid)
  1072. {
  1073. return NULL;
  1074. }
  1075. #endif /* CONFIG_NFS_V4_1 */
  1076. /*
  1077. * Initialize the NFS4 callback service
  1078. */
  1079. static int nfs4_init_callback(struct nfs_client *clp)
  1080. {
  1081. int error;
  1082. if (clp->rpc_ops->version == 4) {
  1083. struct rpc_xprt *xprt;
  1084. xprt = rcu_dereference_raw(clp->cl_rpcclient->cl_xprt);
  1085. if (nfs4_has_session(clp)) {
  1086. error = xprt_setup_backchannel(xprt,
  1087. NFS41_BC_MIN_CALLBACKS);
  1088. if (error < 0)
  1089. return error;
  1090. }
  1091. error = nfs_callback_up(clp->cl_mvops->minor_version, xprt);
  1092. if (error < 0) {
  1093. dprintk("%s: failed to start callback. Error = %d\n",
  1094. __func__, error);
  1095. return error;
  1096. }
  1097. __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
  1098. }
  1099. return 0;
  1100. }
  1101. /*
  1102. * Initialize the minor version specific parts of an NFS4 client record
  1103. */
  1104. static int nfs4_init_client_minor_version(struct nfs_client *clp)
  1105. {
  1106. #if defined(CONFIG_NFS_V4_1)
  1107. if (clp->cl_mvops->minor_version) {
  1108. struct nfs4_session *session = NULL;
  1109. /*
  1110. * Create the session and mark it expired.
  1111. * When a SEQUENCE operation encounters the expired session
  1112. * it will do session recovery to initialize it.
  1113. */
  1114. session = nfs4_alloc_session(clp);
  1115. if (!session)
  1116. return -ENOMEM;
  1117. clp->cl_session = session;
  1118. /*
  1119. * The create session reply races with the server back
  1120. * channel probe. Mark the client NFS_CS_SESSION_INITING
  1121. * so that the client back channel can find the
  1122. * nfs_client struct
  1123. */
  1124. clp->cl_cons_state = NFS_CS_SESSION_INITING;
  1125. }
  1126. #endif /* CONFIG_NFS_V4_1 */
  1127. return nfs4_init_callback(clp);
  1128. }
  1129. /*
  1130. * Initialise an NFS4 client record
  1131. */
  1132. int nfs4_init_client(struct nfs_client *clp,
  1133. const struct rpc_timeout *timeparms,
  1134. const char *ip_addr,
  1135. rpc_authflavor_t authflavour,
  1136. int noresvport)
  1137. {
  1138. char buf[INET6_ADDRSTRLEN + 1];
  1139. int error;
  1140. if (clp->cl_cons_state == NFS_CS_READY) {
  1141. /* the client is initialised already */
  1142. dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
  1143. return 0;
  1144. }
  1145. /* Check NFS protocol revision and initialize RPC op vector */
  1146. clp->rpc_ops = &nfs_v4_clientops;
  1147. error = nfs_create_rpc_client(clp, timeparms, authflavour,
  1148. 1, noresvport);
  1149. if (error < 0)
  1150. goto error;
  1151. /* If no clientaddr= option was specified, find a usable cb address */
  1152. if (ip_addr == NULL) {
  1153. struct sockaddr_storage cb_addr;
  1154. struct sockaddr *sap = (struct sockaddr *)&cb_addr;
  1155. error = rpc_localaddr(clp->cl_rpcclient, sap, sizeof(cb_addr));
  1156. if (error < 0)
  1157. goto error;
  1158. error = rpc_ntop(sap, buf, sizeof(buf));
  1159. if (error < 0)
  1160. goto error;
  1161. ip_addr = (const char *)buf;
  1162. }
  1163. strlcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
  1164. error = nfs_idmap_new(clp);
  1165. if (error < 0) {
  1166. dprintk("%s: failed to create idmapper. Error = %d\n",
  1167. __func__, error);
  1168. goto error;
  1169. }
  1170. __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
  1171. error = nfs4_init_client_minor_version(clp);
  1172. if (error < 0)
  1173. goto error;
  1174. if (!nfs4_has_session(clp))
  1175. nfs_mark_client_ready(clp, NFS_CS_READY);
  1176. return 0;
  1177. error:
  1178. nfs_mark_client_ready(clp, error);
  1179. dprintk("<-- nfs4_init_client() = xerror %d\n", error);
  1180. return error;
  1181. }
  1182. /*
  1183. * Set up an NFS4 client
  1184. */
  1185. static int nfs4_set_client(struct nfs_server *server,
  1186. const char *hostname,
  1187. const struct sockaddr *addr,
  1188. const size_t addrlen,
  1189. const char *ip_addr,
  1190. rpc_authflavor_t authflavour,
  1191. int proto, const struct rpc_timeout *timeparms,
  1192. u32 minorversion, struct net *net)
  1193. {
  1194. struct nfs_client_initdata cl_init = {
  1195. .hostname = hostname,
  1196. .addr = addr,
  1197. .addrlen = addrlen,
  1198. .rpc_ops = &nfs_v4_clientops,
  1199. .proto = proto,
  1200. .minorversion = minorversion,
  1201. .net = net,
  1202. };
  1203. struct nfs_client *clp;
  1204. int error;
  1205. dprintk("--> nfs4_set_client()\n");
  1206. /* Allocate or find a client reference we can use */
  1207. clp = nfs_get_client(&cl_init, timeparms, ip_addr, authflavour,
  1208. server->flags & NFS_MOUNT_NORESVPORT);
  1209. if (IS_ERR(clp)) {
  1210. error = PTR_ERR(clp);
  1211. goto error;
  1212. }
  1213. /*
  1214. * Query for the lease time on clientid setup or renewal
  1215. *
  1216. * Note that this will be set on nfs_clients that were created
  1217. * only for the DS role and did not set this bit, but now will
  1218. * serve a dual role.
  1219. */
  1220. set_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state);
  1221. server->nfs_client = clp;
  1222. dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
  1223. return 0;
  1224. error:
  1225. dprintk("<-- nfs4_set_client() = xerror %d\n", error);
  1226. return error;
  1227. }
  1228. /*
  1229. * Set up a pNFS Data Server client.
  1230. *
  1231. * Return any existing nfs_client that matches server address,port,version
  1232. * and minorversion.
  1233. *
  1234. * For a new nfs_client, use a soft mount (default), a low retrans and a
  1235. * low timeout interval so that if a connection is lost, we retry through
  1236. * the MDS.
  1237. */
  1238. struct nfs_client *nfs4_set_ds_client(struct nfs_client* mds_clp,
  1239. const struct sockaddr *ds_addr,
  1240. int ds_addrlen, int ds_proto)
  1241. {
  1242. struct nfs_client_initdata cl_init = {
  1243. .addr = ds_addr,
  1244. .addrlen = ds_addrlen,
  1245. .rpc_ops = &nfs_v4_clientops,
  1246. .proto = ds_proto,
  1247. .minorversion = mds_clp->cl_minorversion,
  1248. .net = mds_clp->net,
  1249. };
  1250. struct rpc_timeout ds_timeout = {
  1251. .to_initval = 15 * HZ,
  1252. .to_maxval = 15 * HZ,
  1253. .to_retries = 1,
  1254. .to_exponential = 1,
  1255. };
  1256. struct nfs_client *clp;
  1257. /*
  1258. * Set an authflavor equual to the MDS value. Use the MDS nfs_client
  1259. * cl_ipaddr so as to use the same EXCHANGE_ID co_ownerid as the MDS
  1260. * (section 13.1 RFC 5661).
  1261. */
  1262. clp = nfs_get_client(&cl_init, &ds_timeout, mds_clp->cl_ipaddr,
  1263. mds_clp->cl_rpcclient->cl_auth->au_flavor, 0);
  1264. dprintk("<-- %s %p\n", __func__, clp);
  1265. return clp;
  1266. }
  1267. EXPORT_SYMBOL_GPL(nfs4_set_ds_client);
  1268. /*
  1269. * Session has been established, and the client marked ready.
  1270. * Set the mount rsize and wsize with negotiated fore channel
  1271. * attributes which will be bound checked in nfs_server_set_fsinfo.
  1272. */
  1273. static void nfs4_session_set_rwsize(struct nfs_server *server)
  1274. {
  1275. #ifdef CONFIG_NFS_V4_1
  1276. struct nfs4_session *sess;
  1277. u32 server_resp_sz;
  1278. u32 server_rqst_sz;
  1279. if (!nfs4_has_session(server->nfs_client))
  1280. return;
  1281. sess = server->nfs_client->cl_session;
  1282. server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead;
  1283. server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead;
  1284. if (server->rsize > server_resp_sz)
  1285. server->rsize = server_resp_sz;
  1286. if (server->wsize > server_rqst_sz)
  1287. server->wsize = server_rqst_sz;
  1288. #endif /* CONFIG_NFS_V4_1 */
  1289. }
  1290. static int nfs4_server_common_setup(struct nfs_server *server,
  1291. struct nfs_fh *mntfh)
  1292. {
  1293. struct nfs_fattr *fattr;
  1294. int error;
  1295. BUG_ON(!server->nfs_client);
  1296. BUG_ON(!server->nfs_client->rpc_ops);
  1297. BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
  1298. /* data servers support only a subset of NFSv4.1 */
  1299. if (is_ds_only_client(server->nfs_client))
  1300. return -EPROTONOSUPPORT;
  1301. fattr = nfs_alloc_fattr();
  1302. if (fattr == NULL)
  1303. return -ENOMEM;
  1304. /* We must ensure the session is initialised first */
  1305. error = nfs4_init_session(server);
  1306. if (error < 0)
  1307. goto out;
  1308. /* Probe the root fh to retrieve its FSID and filehandle */
  1309. error = nfs4_get_rootfh(server, mntfh);
  1310. if (error < 0)
  1311. goto out;
  1312. dprintk("Server FSID: %llx:%llx\n",
  1313. (unsigned long long) server->fsid.major,
  1314. (unsigned long long) server->fsid.minor);
  1315. dprintk("Mount FH: %d\n", mntfh->size);
  1316. nfs4_session_set_rwsize(server);
  1317. error = nfs_probe_fsinfo(server, mntfh, fattr);
  1318. if (error < 0)
  1319. goto out;
  1320. if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
  1321. server->namelen = NFS4_MAXNAMLEN;
  1322. nfs_server_insert_lists(server);
  1323. server->mount_time = jiffies;
  1324. server->destroy = nfs4_destroy_server;
  1325. out:
  1326. nfs_free_fattr(fattr);
  1327. return error;
  1328. }
  1329. /*
  1330. * Create a version 4 volume record
  1331. */
  1332. static int nfs4_init_server(struct nfs_server *server,
  1333. const struct nfs_parsed_mount_data *data)
  1334. {
  1335. struct rpc_timeout timeparms;
  1336. int error;
  1337. dprintk("--> nfs4_init_server()\n");
  1338. nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
  1339. data->timeo, data->retrans);
  1340. /* Initialise the client representation from the mount data */
  1341. server->flags = data->flags;
  1342. server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR|NFS_CAP_POSIX_LOCK;
  1343. if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
  1344. server->caps |= NFS_CAP_READDIRPLUS;
  1345. server->options = data->options;
  1346. /* Get a client record */
  1347. error = nfs4_set_client(server,
  1348. data->nfs_server.hostname,
  1349. (const struct sockaddr *)&data->nfs_server.address,
  1350. data->nfs_server.addrlen,
  1351. data->client_address,
  1352. data->auth_flavors[0],
  1353. data->nfs_server.protocol,
  1354. &timeparms,
  1355. data->minorversion,
  1356. data->net);
  1357. if (error < 0)
  1358. goto error;
  1359. /*
  1360. * Don't use NFS uid/gid mapping if we're using AUTH_SYS or lower
  1361. * authentication.
  1362. */
  1363. if (nfs4_disable_idmapping && data->auth_flavors[0] == RPC_AUTH_UNIX)
  1364. server->caps |= NFS_CAP_UIDGID_NOMAP;
  1365. if (data->rsize)
  1366. server->rsize = nfs_block_size(data->rsize, NULL);
  1367. if (data->wsize)
  1368. server->wsize = nfs_block_size(data->wsize, NULL);
  1369. server->acregmin = data->acregmin * HZ;
  1370. server->acregmax = data->acregmax * HZ;
  1371. server->acdirmin = data->acdirmin * HZ;
  1372. server->acdirmax = data->acdirmax * HZ;
  1373. server->port = data->nfs_server.port;
  1374. error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
  1375. error:
  1376. /* Done */
  1377. dprintk("<-- nfs4_init_server() = %d\n", error);
  1378. return error;
  1379. }
  1380. /*
  1381. * Create a version 4 volume record
  1382. * - keyed on server and FSID
  1383. */
  1384. struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
  1385. struct nfs_fh *mntfh)
  1386. {
  1387. struct nfs_server *server;
  1388. int error;
  1389. dprintk("--> nfs4_create_server()\n");
  1390. server = nfs_alloc_server();
  1391. if (!server)
  1392. return ERR_PTR(-ENOMEM);
  1393. /* set up the general RPC client */
  1394. error = nfs4_init_server(server, data);
  1395. if (error < 0)
  1396. goto error;
  1397. error = nfs4_server_common_setup(server, mntfh);
  1398. if (error < 0)
  1399. goto error;
  1400. dprintk("<-- nfs4_create_server() = %p\n", server);
  1401. return server;
  1402. error:
  1403. nfs_free_server(server);
  1404. dprintk("<-- nfs4_create_server() = error %d\n", error);
  1405. return ERR_PTR(error);
  1406. }
  1407. /*
  1408. * Create an NFS4 referral server record
  1409. */
  1410. struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
  1411. struct nfs_fh *mntfh)
  1412. {
  1413. struct nfs_client *parent_client;
  1414. struct nfs_server *server, *parent_server;
  1415. int error;
  1416. dprintk("--> nfs4_create_referral_server()\n");
  1417. server = nfs_alloc_server();
  1418. if (!server)
  1419. return ERR_PTR(-ENOMEM);
  1420. parent_server = NFS_SB(data->sb);
  1421. parent_client = parent_server->nfs_client;
  1422. /* Initialise the client representation from the parent server */
  1423. nfs_server_copy_userdata(server, parent_server);
  1424. server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR;
  1425. /* Get a client representation.
  1426. * Note: NFSv4 always uses TCP, */
  1427. error = nfs4_set_client(server, data->hostname,
  1428. data->addr,
  1429. data->addrlen,
  1430. parent_client->cl_ipaddr,
  1431. data->authflavor,
  1432. rpc_protocol(parent_server->client),
  1433. parent_server->client->cl_timeout,
  1434. parent_client->cl_mvops->minor_version,
  1435. parent_client->net);
  1436. if (error < 0)
  1437. goto error;
  1438. error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
  1439. if (error < 0)
  1440. goto error;
  1441. error = nfs4_server_common_setup(server, mntfh);
  1442. if (error < 0)
  1443. goto error;
  1444. dprintk("<-- nfs_create_referral_server() = %p\n", server);
  1445. return server;
  1446. error:
  1447. nfs_free_server(server);
  1448. dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
  1449. return ERR_PTR(error);
  1450. }
  1451. #endif /* CONFIG_NFS_V4 */
  1452. /*
  1453. * Clone an NFS2, NFS3 or NFS4 server record
  1454. */
  1455. struct nfs_server *nfs_clone_server(struct nfs_server *source,
  1456. struct nfs_fh *fh,
  1457. struct nfs_fattr *fattr)
  1458. {
  1459. struct nfs_server *server;
  1460. struct nfs_fattr *fattr_fsinfo;
  1461. int error;
  1462. dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
  1463. (unsigned long long) fattr->fsid.major,
  1464. (unsigned long long) fattr->fsid.minor);
  1465. server = nfs_alloc_server();
  1466. if (!server)
  1467. return ERR_PTR(-ENOMEM);
  1468. error = -ENOMEM;
  1469. fattr_fsinfo = nfs_alloc_fattr();
  1470. if (fattr_fsinfo == NULL)
  1471. goto out_free_server;
  1472. /* Copy data from the source */
  1473. server->nfs_client = source->nfs_client;
  1474. server->destroy = source->destroy;
  1475. atomic_inc(&server->nfs_client->cl_count);
  1476. nfs_server_copy_userdata(server, source);
  1477. server->fsid = fattr->fsid;
  1478. error = nfs_init_server_rpcclient(server,
  1479. source->client->cl_timeout,
  1480. source->client->cl_auth->au_flavor);
  1481. if (error < 0)
  1482. goto out_free_server;
  1483. if (!IS_ERR(source->client_acl))
  1484. nfs_init_server_aclclient(server);
  1485. /* probe the filesystem info for this server filesystem */
  1486. error = nfs_probe_fsinfo(server, fh, fattr_fsinfo);
  1487. if (error < 0)
  1488. goto out_free_server;
  1489. if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
  1490. server->namelen = NFS4_MAXNAMLEN;
  1491. dprintk("Cloned FSID: %llx:%llx\n",
  1492. (unsigned long long) server->fsid.major,
  1493. (unsigned long long) server->fsid.minor);
  1494. error = nfs_start_lockd(server);
  1495. if (error < 0)
  1496. goto out_free_server;
  1497. nfs_server_insert_lists(server);
  1498. server->mount_time = jiffies;
  1499. nfs_free_fattr(fattr_fsinfo);
  1500. dprintk("<-- nfs_clone_server() = %p\n", server);
  1501. return server;
  1502. out_free_server:
  1503. nfs_free_fattr(fattr_fsinfo);
  1504. nfs_free_server(server);
  1505. dprintk("<-- nfs_clone_server() = error %d\n", error);
  1506. return ERR_PTR(error);
  1507. }
  1508. void nfs_clients_init(struct net *net)
  1509. {
  1510. struct nfs_net *nn = net_generic(net, nfs_net_id);
  1511. INIT_LIST_HEAD(&nn->nfs_client_list);
  1512. INIT_LIST_HEAD(&nn->nfs_volume_list);
  1513. #ifdef CONFIG_NFS_V4
  1514. idr_init(&nn->cb_ident_idr);
  1515. #endif
  1516. spin_lock_init(&nn->nfs_client_lock);
  1517. }
  1518. #ifdef CONFIG_PROC_FS
  1519. static struct proc_dir_entry *proc_fs_nfs;
  1520. static int nfs_server_list_open(struct inode *inode, struct file *file);
  1521. static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
  1522. static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
  1523. static void nfs_server_list_stop(struct seq_file *p, void *v);
  1524. static int nfs_server_list_show(struct seq_file *m, void *v);
  1525. static const struct seq_operations nfs_server_list_ops = {
  1526. .start = nfs_server_list_start,
  1527. .next = nfs_server_list_next,
  1528. .stop = nfs_server_list_stop,
  1529. .show = nfs_server_list_show,
  1530. };
  1531. static const struct file_operations nfs_server_list_fops = {
  1532. .open = nfs_server_list_open,
  1533. .read = seq_read,
  1534. .llseek = seq_lseek,
  1535. .release = seq_release,
  1536. .owner = THIS_MODULE,
  1537. };
  1538. static int nfs_volume_list_open(struct inode *inode, struct file *file);
  1539. static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
  1540. static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
  1541. static void nfs_volume_list_stop(struct seq_file *p, void *v);
  1542. static int nfs_volume_list_show(struct seq_file *m, void *v);
  1543. static const struct seq_operations nfs_volume_list_ops = {
  1544. .start = nfs_volume_list_start,
  1545. .next = nfs_volume_list_next,
  1546. .stop = nfs_volume_list_stop,
  1547. .show = nfs_volume_list_show,
  1548. };
  1549. static const struct file_operations nfs_volume_list_fops = {
  1550. .open = nfs_volume_list_open,
  1551. .read = seq_read,
  1552. .llseek = seq_lseek,
  1553. .release = seq_release,
  1554. .owner = THIS_MODULE,
  1555. };
  1556. /*
  1557. * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
  1558. * we're dealing
  1559. */
  1560. static int nfs_server_list_open(struct inode *inode, struct file *file)
  1561. {
  1562. struct seq_file *m;
  1563. int ret;
  1564. struct pid_namespace *pid_ns = file->f_dentry->d_sb->s_fs_info;
  1565. struct net *net = pid_ns->child_reaper->nsproxy->net_ns;
  1566. ret = seq_open(file, &nfs_server_list_ops);
  1567. if (ret < 0)
  1568. return ret;
  1569. m = file->private_data;
  1570. m->private = net;
  1571. return 0;
  1572. }
  1573. /*
  1574. * set up the iterator to start reading from the server list and return the first item
  1575. */
  1576. static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
  1577. {
  1578. struct nfs_net *nn = net_generic(m->private, nfs_net_id);
  1579. /* lock the list against modification */
  1580. spin_lock(&nn->nfs_client_lock);
  1581. return seq_list_start_head(&nn->nfs_client_list, *_pos);
  1582. }
  1583. /*
  1584. * move to next server
  1585. */
  1586. static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
  1587. {
  1588. struct nfs_net *nn = net_generic(p->private, nfs_net_id);
  1589. return seq_list_next(v, &nn->nfs_client_list, pos);
  1590. }
  1591. /*
  1592. * clean up after reading from the transports list
  1593. */
  1594. static void nfs_server_list_stop(struct seq_file *p, void *v)
  1595. {
  1596. struct nfs_net *nn = net_generic(p->private, nfs_net_id);
  1597. spin_unlock(&nn->nfs_client_lock);
  1598. }
  1599. /*
  1600. * display a header line followed by a load of call lines
  1601. */
  1602. static int nfs_server_list_show(struct seq_file *m, void *v)
  1603. {
  1604. struct nfs_client *clp;
  1605. struct nfs_net *nn = net_generic(m->private, nfs_net_id);
  1606. /* display header on line 1 */
  1607. if (v == &nn->nfs_client_list) {
  1608. seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
  1609. return 0;
  1610. }
  1611. /* display one transport per line on subsequent lines */
  1612. clp = list_entry(v, struct nfs_client, cl_share_link);
  1613. /* Check if the client is initialized */
  1614. if (clp->cl_cons_state != NFS_CS_READY)
  1615. return 0;
  1616. rcu_read_lock();
  1617. seq_printf(m, "v%u %s %s %3d %s\n",
  1618. clp->rpc_ops->version,
  1619. rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
  1620. rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
  1621. atomic_read(&clp->cl_count),
  1622. clp->cl_hostname);
  1623. rcu_read_unlock();
  1624. return 0;
  1625. }
  1626. /*
  1627. * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
  1628. */
  1629. static int nfs_volume_list_open(struct inode *inode, struct file *file)
  1630. {
  1631. struct seq_file *m;
  1632. int ret;
  1633. struct pid_namespace *pid_ns = file->f_dentry->d_sb->s_fs_info;
  1634. struct net *net = pid_ns->child_reaper->nsproxy->net_ns;
  1635. ret = seq_open(file, &nfs_volume_list_ops);
  1636. if (ret < 0)
  1637. return ret;
  1638. m = file->private_data;
  1639. m->private = net;
  1640. return 0;
  1641. }
  1642. /*
  1643. * set up the iterator to start reading from the volume list and return the first item
  1644. */
  1645. static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
  1646. {
  1647. struct nfs_net *nn = net_generic(m->private, nfs_net_id);
  1648. /* lock the list against modification */
  1649. spin_lock(&nn->nfs_client_lock);
  1650. return seq_list_start_head(&nn->nfs_volume_list, *_pos);
  1651. }
  1652. /*
  1653. * move to next volume
  1654. */
  1655. static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
  1656. {
  1657. struct nfs_net *nn = net_generic(p->private, nfs_net_id);
  1658. return seq_list_next(v, &nn->nfs_volume_list, pos);
  1659. }
  1660. /*
  1661. * clean up after reading from the transports list
  1662. */
  1663. static void nfs_volume_list_stop(struct seq_file *p, void *v)
  1664. {
  1665. struct nfs_net *nn = net_generic(p->private, nfs_net_id);
  1666. spin_unlock(&nn->nfs_client_lock);
  1667. }
  1668. /*
  1669. * display a header line followed by a load of call lines
  1670. */
  1671. static int nfs_volume_list_show(struct seq_file *m, void *v)
  1672. {
  1673. struct nfs_server *server;
  1674. struct nfs_client *clp;
  1675. char dev[8], fsid[17];
  1676. struct nfs_net *nn = net_generic(m->private, nfs_net_id);
  1677. /* display header on line 1 */
  1678. if (v == &nn->nfs_volume_list) {
  1679. seq_puts(m, "NV SERVER PORT DEV FSID FSC\n");
  1680. return 0;
  1681. }
  1682. /* display one transport per line on subsequent lines */
  1683. server = list_entry(v, struct nfs_server, master_link);
  1684. clp = server->nfs_client;
  1685. snprintf(dev, 8, "%u:%u",
  1686. MAJOR(server->s_dev), MINOR(server->s_dev));
  1687. snprintf(fsid, 17, "%llx:%llx",
  1688. (unsigned long long) server->fsid.major,
  1689. (unsigned long long) server->fsid.minor);
  1690. rcu_read_lock();
  1691. seq_printf(m, "v%u %s %s %-7s %-17s %s\n",
  1692. clp->rpc_ops->version,
  1693. rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
  1694. rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
  1695. dev,
  1696. fsid,
  1697. nfs_server_fscache_state(server));
  1698. rcu_read_unlock();
  1699. return 0;
  1700. }
  1701. /*
  1702. * initialise the /proc/fs/nfsfs/ directory
  1703. */
  1704. int __init nfs_fs_proc_init(void)
  1705. {
  1706. struct proc_dir_entry *p;
  1707. proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
  1708. if (!proc_fs_nfs)
  1709. goto error_0;
  1710. /* a file of servers with which we're dealing */
  1711. p = proc_create("servers", S_IFREG|S_IRUGO,
  1712. proc_fs_nfs, &nfs_server_list_fops);
  1713. if (!p)
  1714. goto error_1;
  1715. /* a file of volumes that we have mounted */
  1716. p = proc_create("volumes", S_IFREG|S_IRUGO,
  1717. proc_fs_nfs, &nfs_volume_list_fops);
  1718. if (!p)
  1719. goto error_2;
  1720. return 0;
  1721. error_2:
  1722. remove_proc_entry("servers", proc_fs_nfs);
  1723. error_1:
  1724. remove_proc_entry("fs/nfsfs", NULL);
  1725. error_0:
  1726. return -ENOMEM;
  1727. }
  1728. /*
  1729. * clean up the /proc/fs/nfsfs/ directory
  1730. */
  1731. void nfs_fs_proc_exit(void)
  1732. {
  1733. remove_proc_entry("volumes", proc_fs_nfs);
  1734. remove_proc_entry("servers", proc_fs_nfs);
  1735. remove_proc_entry("fs/nfsfs", NULL);
  1736. }
  1737. #endif /* CONFIG_PROC_FS */
  1738. module_param(nfs4_disable_idmapping, bool, 0644);
  1739. MODULE_PARM_DESC(nfs4_disable_idmapping,
  1740. "Turn off NFSv4 idmapping when using 'sec=sys'");