client.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709
  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 <net/ipv6.h>
  37. #include <linux/nfs_xdr.h>
  38. #include <asm/system.h>
  39. #include "nfs4_fs.h"
  40. #include "callback.h"
  41. #include "delegation.h"
  42. #include "iostat.h"
  43. #include "internal.h"
  44. #include "fscache.h"
  45. #define NFSDBG_FACILITY NFSDBG_CLIENT
  46. static DEFINE_SPINLOCK(nfs_client_lock);
  47. static LIST_HEAD(nfs_client_list);
  48. static LIST_HEAD(nfs_volume_list);
  49. static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
  50. /*
  51. * RPC cruft for NFS
  52. */
  53. static struct rpc_version *nfs_version[5] = {
  54. [2] = &nfs_version2,
  55. #ifdef CONFIG_NFS_V3
  56. [3] = &nfs_version3,
  57. #endif
  58. #ifdef CONFIG_NFS_V4
  59. [4] = &nfs_version4,
  60. #endif
  61. };
  62. struct rpc_program nfs_program = {
  63. .name = "nfs",
  64. .number = NFS_PROGRAM,
  65. .nrvers = ARRAY_SIZE(nfs_version),
  66. .version = nfs_version,
  67. .stats = &nfs_rpcstat,
  68. .pipe_dir_name = "/nfs",
  69. };
  70. struct rpc_stat nfs_rpcstat = {
  71. .program = &nfs_program
  72. };
  73. #ifdef CONFIG_NFS_V3_ACL
  74. static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program };
  75. static struct rpc_version * nfsacl_version[] = {
  76. [3] = &nfsacl_version3,
  77. };
  78. struct rpc_program nfsacl_program = {
  79. .name = "nfsacl",
  80. .number = NFS_ACL_PROGRAM,
  81. .nrvers = ARRAY_SIZE(nfsacl_version),
  82. .version = nfsacl_version,
  83. .stats = &nfsacl_rpcstat,
  84. };
  85. #endif /* CONFIG_NFS_V3_ACL */
  86. struct nfs_client_initdata {
  87. const char *hostname;
  88. const struct sockaddr *addr;
  89. size_t addrlen;
  90. const struct nfs_rpc_ops *rpc_ops;
  91. int proto;
  92. u32 minorversion;
  93. };
  94. /*
  95. * Allocate a shared client record
  96. *
  97. * Since these are allocated/deallocated very rarely, we don't
  98. * bother putting them in a slab cache...
  99. */
  100. static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
  101. {
  102. struct nfs_client *clp;
  103. struct rpc_cred *cred;
  104. if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
  105. goto error_0;
  106. clp->rpc_ops = cl_init->rpc_ops;
  107. if (cl_init->rpc_ops->version == 4) {
  108. if (nfs_callback_up() < 0)
  109. goto error_2;
  110. __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
  111. }
  112. atomic_set(&clp->cl_count, 1);
  113. clp->cl_cons_state = NFS_CS_INITING;
  114. memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
  115. clp->cl_addrlen = cl_init->addrlen;
  116. if (cl_init->hostname) {
  117. clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
  118. if (!clp->cl_hostname)
  119. goto error_3;
  120. }
  121. INIT_LIST_HEAD(&clp->cl_superblocks);
  122. clp->cl_rpcclient = ERR_PTR(-EINVAL);
  123. clp->cl_proto = cl_init->proto;
  124. #ifdef CONFIG_NFS_V4
  125. INIT_LIST_HEAD(&clp->cl_delegations);
  126. spin_lock_init(&clp->cl_lock);
  127. INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
  128. rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
  129. clp->cl_boot_time = CURRENT_TIME;
  130. clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
  131. clp->cl_minorversion = cl_init->minorversion;
  132. #endif
  133. cred = rpc_lookup_machine_cred();
  134. if (!IS_ERR(cred))
  135. clp->cl_machine_cred = cred;
  136. nfs_fscache_get_client_cookie(clp);
  137. return clp;
  138. error_3:
  139. if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
  140. nfs_callback_down();
  141. error_2:
  142. kfree(clp);
  143. error_0:
  144. return NULL;
  145. }
  146. static void nfs4_shutdown_client(struct nfs_client *clp)
  147. {
  148. #ifdef CONFIG_NFS_V4
  149. if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
  150. nfs4_kill_renewd(clp);
  151. BUG_ON(!RB_EMPTY_ROOT(&clp->cl_state_owners));
  152. if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
  153. nfs_idmap_delete(clp);
  154. rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
  155. #endif
  156. }
  157. /*
  158. * Clears/puts all minor version specific parts from an nfs_client struct
  159. * reverting it to minorversion 0.
  160. */
  161. static void nfs4_clear_client_minor_version(struct nfs_client *clp)
  162. {
  163. #ifdef CONFIG_NFS_V4_1
  164. if (nfs4_has_session(clp)) {
  165. nfs4_destroy_session(clp->cl_session);
  166. clp->cl_session = NULL;
  167. }
  168. clp->cl_call_sync = _nfs4_call_sync;
  169. #endif /* CONFIG_NFS_V4_1 */
  170. }
  171. /*
  172. * Destroy a shared client record
  173. */
  174. static void nfs_free_client(struct nfs_client *clp)
  175. {
  176. dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
  177. nfs4_clear_client_minor_version(clp);
  178. nfs4_shutdown_client(clp);
  179. nfs_fscache_release_client_cookie(clp);
  180. /* -EIO all pending I/O */
  181. if (!IS_ERR(clp->cl_rpcclient))
  182. rpc_shutdown_client(clp->cl_rpcclient);
  183. if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
  184. nfs_callback_down();
  185. if (clp->cl_machine_cred != NULL)
  186. put_rpccred(clp->cl_machine_cred);
  187. kfree(clp->cl_hostname);
  188. kfree(clp);
  189. dprintk("<-- nfs_free_client()\n");
  190. }
  191. /*
  192. * Release a reference to a shared client record
  193. */
  194. void nfs_put_client(struct nfs_client *clp)
  195. {
  196. if (!clp)
  197. return;
  198. dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
  199. if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
  200. list_del(&clp->cl_share_link);
  201. spin_unlock(&nfs_client_lock);
  202. BUG_ON(!list_empty(&clp->cl_superblocks));
  203. nfs_free_client(clp);
  204. }
  205. }
  206. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  207. /*
  208. * Test if two ip6 socket addresses refer to the same socket by
  209. * comparing relevant fields. The padding bytes specifically, are not
  210. * compared. sin6_flowinfo is not compared because it only affects QoS
  211. * and sin6_scope_id is only compared if the address is "link local"
  212. * because "link local" addresses need only be unique to a specific
  213. * link. Conversely, ordinary unicast addresses might have different
  214. * sin6_scope_id.
  215. *
  216. * The caller should ensure both socket addresses are AF_INET6.
  217. */
  218. static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
  219. const struct sockaddr *sa2)
  220. {
  221. const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
  222. const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
  223. if (ipv6_addr_scope(&sin1->sin6_addr) == IPV6_ADDR_SCOPE_LINKLOCAL &&
  224. sin1->sin6_scope_id != sin2->sin6_scope_id)
  225. return 0;
  226. return ipv6_addr_equal(&sin1->sin6_addr, &sin1->sin6_addr);
  227. }
  228. #else /* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */
  229. static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
  230. const struct sockaddr *sa2)
  231. {
  232. return 0;
  233. }
  234. #endif
  235. /*
  236. * Test if two ip4 socket addresses refer to the same socket, by
  237. * comparing relevant fields. The padding bytes specifically, are
  238. * not compared.
  239. *
  240. * The caller should ensure both socket addresses are AF_INET.
  241. */
  242. static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1,
  243. const struct sockaddr *sa2)
  244. {
  245. const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
  246. const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
  247. return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
  248. }
  249. static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1,
  250. const struct sockaddr *sa2)
  251. {
  252. const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
  253. const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
  254. return nfs_sockaddr_match_ipaddr6(sa1, sa2) &&
  255. (sin1->sin6_port == sin2->sin6_port);
  256. }
  257. static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
  258. const struct sockaddr *sa2)
  259. {
  260. const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
  261. const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
  262. return nfs_sockaddr_match_ipaddr4(sa1, sa2) &&
  263. (sin1->sin_port == sin2->sin_port);
  264. }
  265. /*
  266. * Test if two socket addresses represent the same actual socket,
  267. * by comparing (only) relevant fields, excluding the port number.
  268. */
  269. static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
  270. const struct sockaddr *sa2)
  271. {
  272. if (sa1->sa_family != sa2->sa_family)
  273. return 0;
  274. switch (sa1->sa_family) {
  275. case AF_INET:
  276. return nfs_sockaddr_match_ipaddr4(sa1, sa2);
  277. case AF_INET6:
  278. return nfs_sockaddr_match_ipaddr6(sa1, sa2);
  279. }
  280. return 0;
  281. }
  282. /*
  283. * Test if two socket addresses represent the same actual socket,
  284. * by comparing (only) relevant fields, including the port number.
  285. */
  286. static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
  287. const struct sockaddr *sa2)
  288. {
  289. if (sa1->sa_family != sa2->sa_family)
  290. return 0;
  291. switch (sa1->sa_family) {
  292. case AF_INET:
  293. return nfs_sockaddr_cmp_ip4(sa1, sa2);
  294. case AF_INET6:
  295. return nfs_sockaddr_cmp_ip6(sa1, sa2);
  296. }
  297. return 0;
  298. }
  299. /*
  300. * Find a client by IP address and protocol version
  301. * - returns NULL if no such client
  302. */
  303. struct nfs_client *nfs_find_client(const struct sockaddr *addr, u32 nfsversion)
  304. {
  305. struct nfs_client *clp;
  306. spin_lock(&nfs_client_lock);
  307. list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
  308. struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
  309. /* Don't match clients that failed to initialise properly */
  310. if (clp->cl_cons_state != NFS_CS_READY)
  311. continue;
  312. /* Different NFS versions cannot share the same nfs_client */
  313. if (clp->rpc_ops->version != nfsversion)
  314. continue;
  315. /* Match only the IP address, not the port number */
  316. if (!nfs_sockaddr_match_ipaddr(addr, clap))
  317. continue;
  318. atomic_inc(&clp->cl_count);
  319. spin_unlock(&nfs_client_lock);
  320. return clp;
  321. }
  322. spin_unlock(&nfs_client_lock);
  323. return NULL;
  324. }
  325. /*
  326. * Find a client by IP address and protocol version
  327. * - returns NULL if no such client
  328. */
  329. struct nfs_client *nfs_find_client_next(struct nfs_client *clp)
  330. {
  331. struct sockaddr *sap = (struct sockaddr *)&clp->cl_addr;
  332. u32 nfsvers = clp->rpc_ops->version;
  333. spin_lock(&nfs_client_lock);
  334. list_for_each_entry_continue(clp, &nfs_client_list, cl_share_link) {
  335. struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
  336. /* Don't match clients that failed to initialise properly */
  337. if (clp->cl_cons_state != NFS_CS_READY)
  338. continue;
  339. /* Different NFS versions cannot share the same nfs_client */
  340. if (clp->rpc_ops->version != nfsvers)
  341. continue;
  342. /* Match only the IP address, not the port number */
  343. if (!nfs_sockaddr_match_ipaddr(sap, clap))
  344. continue;
  345. atomic_inc(&clp->cl_count);
  346. spin_unlock(&nfs_client_lock);
  347. return clp;
  348. }
  349. spin_unlock(&nfs_client_lock);
  350. return NULL;
  351. }
  352. /*
  353. * Find an nfs_client on the list that matches the initialisation data
  354. * that is supplied.
  355. */
  356. static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
  357. {
  358. struct nfs_client *clp;
  359. const struct sockaddr *sap = data->addr;
  360. list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
  361. const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
  362. /* Don't match clients that failed to initialise properly */
  363. if (clp->cl_cons_state < 0)
  364. continue;
  365. /* Different NFS versions cannot share the same nfs_client */
  366. if (clp->rpc_ops != data->rpc_ops)
  367. continue;
  368. if (clp->cl_proto != data->proto)
  369. continue;
  370. /* Match nfsv4 minorversion */
  371. if (clp->cl_minorversion != data->minorversion)
  372. continue;
  373. /* Match the full socket address */
  374. if (!nfs_sockaddr_cmp(sap, clap))
  375. continue;
  376. atomic_inc(&clp->cl_count);
  377. return clp;
  378. }
  379. return NULL;
  380. }
  381. /*
  382. * Look up a client by IP address and protocol version
  383. * - creates a new record if one doesn't yet exist
  384. */
  385. static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
  386. {
  387. struct nfs_client *clp, *new = NULL;
  388. int error;
  389. dprintk("--> nfs_get_client(%s,v%u)\n",
  390. cl_init->hostname ?: "", cl_init->rpc_ops->version);
  391. /* see if the client already exists */
  392. do {
  393. spin_lock(&nfs_client_lock);
  394. clp = nfs_match_client(cl_init);
  395. if (clp)
  396. goto found_client;
  397. if (new)
  398. goto install_client;
  399. spin_unlock(&nfs_client_lock);
  400. new = nfs_alloc_client(cl_init);
  401. } while (new);
  402. return ERR_PTR(-ENOMEM);
  403. /* install a new client and return with it unready */
  404. install_client:
  405. clp = new;
  406. list_add(&clp->cl_share_link, &nfs_client_list);
  407. spin_unlock(&nfs_client_lock);
  408. dprintk("--> nfs_get_client() = %p [new]\n", clp);
  409. return clp;
  410. /* found an existing client
  411. * - make sure it's ready before returning
  412. */
  413. found_client:
  414. spin_unlock(&nfs_client_lock);
  415. if (new)
  416. nfs_free_client(new);
  417. error = wait_event_killable(nfs_client_active_wq,
  418. clp->cl_cons_state != NFS_CS_INITING);
  419. if (error < 0) {
  420. nfs_put_client(clp);
  421. return ERR_PTR(-ERESTARTSYS);
  422. }
  423. if (clp->cl_cons_state < NFS_CS_READY) {
  424. error = clp->cl_cons_state;
  425. nfs_put_client(clp);
  426. return ERR_PTR(error);
  427. }
  428. BUG_ON(clp->cl_cons_state != NFS_CS_READY);
  429. dprintk("--> nfs_get_client() = %p [share]\n", clp);
  430. return clp;
  431. }
  432. /*
  433. * Mark a server as ready or failed
  434. */
  435. static void nfs_mark_client_ready(struct nfs_client *clp, int state)
  436. {
  437. clp->cl_cons_state = state;
  438. wake_up_all(&nfs_client_active_wq);
  439. }
  440. /*
  441. * Initialise the timeout values for a connection
  442. */
  443. static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
  444. unsigned int timeo, unsigned int retrans)
  445. {
  446. to->to_initval = timeo * HZ / 10;
  447. to->to_retries = retrans;
  448. switch (proto) {
  449. case XPRT_TRANSPORT_TCP:
  450. case XPRT_TRANSPORT_RDMA:
  451. if (to->to_retries == 0)
  452. to->to_retries = NFS_DEF_TCP_RETRANS;
  453. if (to->to_initval == 0)
  454. to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
  455. if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
  456. to->to_initval = NFS_MAX_TCP_TIMEOUT;
  457. to->to_increment = to->to_initval;
  458. to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
  459. if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
  460. to->to_maxval = NFS_MAX_TCP_TIMEOUT;
  461. if (to->to_maxval < to->to_initval)
  462. to->to_maxval = to->to_initval;
  463. to->to_exponential = 0;
  464. break;
  465. case XPRT_TRANSPORT_UDP:
  466. if (to->to_retries == 0)
  467. to->to_retries = NFS_DEF_UDP_RETRANS;
  468. if (!to->to_initval)
  469. to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
  470. if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
  471. to->to_initval = NFS_MAX_UDP_TIMEOUT;
  472. to->to_maxval = NFS_MAX_UDP_TIMEOUT;
  473. to->to_exponential = 1;
  474. break;
  475. default:
  476. BUG();
  477. }
  478. }
  479. /*
  480. * Create an RPC client handle
  481. */
  482. static int nfs_create_rpc_client(struct nfs_client *clp,
  483. const struct rpc_timeout *timeparms,
  484. rpc_authflavor_t flavor,
  485. int discrtry, int noresvport)
  486. {
  487. struct rpc_clnt *clnt = NULL;
  488. struct rpc_create_args args = {
  489. .protocol = clp->cl_proto,
  490. .address = (struct sockaddr *)&clp->cl_addr,
  491. .addrsize = clp->cl_addrlen,
  492. .timeout = timeparms,
  493. .servername = clp->cl_hostname,
  494. .program = &nfs_program,
  495. .version = clp->rpc_ops->version,
  496. .authflavor = flavor,
  497. };
  498. if (discrtry)
  499. args.flags |= RPC_CLNT_CREATE_DISCRTRY;
  500. if (noresvport)
  501. args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
  502. if (!IS_ERR(clp->cl_rpcclient))
  503. return 0;
  504. clnt = rpc_create(&args);
  505. if (IS_ERR(clnt)) {
  506. dprintk("%s: cannot create RPC client. Error = %ld\n",
  507. __func__, PTR_ERR(clnt));
  508. return PTR_ERR(clnt);
  509. }
  510. clp->cl_rpcclient = clnt;
  511. return 0;
  512. }
  513. /*
  514. * Version 2 or 3 client destruction
  515. */
  516. static void nfs_destroy_server(struct nfs_server *server)
  517. {
  518. if (!(server->flags & NFS_MOUNT_NONLM))
  519. nlmclnt_done(server->nlm_host);
  520. }
  521. /*
  522. * Version 2 or 3 lockd setup
  523. */
  524. static int nfs_start_lockd(struct nfs_server *server)
  525. {
  526. struct nlm_host *host;
  527. struct nfs_client *clp = server->nfs_client;
  528. struct nlmclnt_initdata nlm_init = {
  529. .hostname = clp->cl_hostname,
  530. .address = (struct sockaddr *)&clp->cl_addr,
  531. .addrlen = clp->cl_addrlen,
  532. .protocol = server->flags & NFS_MOUNT_TCP ?
  533. IPPROTO_TCP : IPPROTO_UDP,
  534. .nfs_version = clp->rpc_ops->version,
  535. .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
  536. 1 : 0,
  537. };
  538. if (nlm_init.nfs_version > 3)
  539. return 0;
  540. if (server->flags & NFS_MOUNT_NONLM)
  541. return 0;
  542. host = nlmclnt_init(&nlm_init);
  543. if (IS_ERR(host))
  544. return PTR_ERR(host);
  545. server->nlm_host = host;
  546. server->destroy = nfs_destroy_server;
  547. return 0;
  548. }
  549. /*
  550. * Initialise an NFSv3 ACL client connection
  551. */
  552. #ifdef CONFIG_NFS_V3_ACL
  553. static void nfs_init_server_aclclient(struct nfs_server *server)
  554. {
  555. if (server->nfs_client->rpc_ops->version != 3)
  556. goto out_noacl;
  557. if (server->flags & NFS_MOUNT_NOACL)
  558. goto out_noacl;
  559. server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
  560. if (IS_ERR(server->client_acl))
  561. goto out_noacl;
  562. /* No errors! Assume that Sun nfsacls are supported */
  563. server->caps |= NFS_CAP_ACLS;
  564. return;
  565. out_noacl:
  566. server->caps &= ~NFS_CAP_ACLS;
  567. }
  568. #else
  569. static inline void nfs_init_server_aclclient(struct nfs_server *server)
  570. {
  571. server->flags &= ~NFS_MOUNT_NOACL;
  572. server->caps &= ~NFS_CAP_ACLS;
  573. }
  574. #endif
  575. /*
  576. * Create a general RPC client
  577. */
  578. static int nfs_init_server_rpcclient(struct nfs_server *server,
  579. const struct rpc_timeout *timeo,
  580. rpc_authflavor_t pseudoflavour)
  581. {
  582. struct nfs_client *clp = server->nfs_client;
  583. server->client = rpc_clone_client(clp->cl_rpcclient);
  584. if (IS_ERR(server->client)) {
  585. dprintk("%s: couldn't create rpc_client!\n", __func__);
  586. return PTR_ERR(server->client);
  587. }
  588. memcpy(&server->client->cl_timeout_default,
  589. timeo,
  590. sizeof(server->client->cl_timeout_default));
  591. server->client->cl_timeout = &server->client->cl_timeout_default;
  592. if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
  593. struct rpc_auth *auth;
  594. auth = rpcauth_create(pseudoflavour, server->client);
  595. if (IS_ERR(auth)) {
  596. dprintk("%s: couldn't create credcache!\n", __func__);
  597. return PTR_ERR(auth);
  598. }
  599. }
  600. server->client->cl_softrtry = 0;
  601. if (server->flags & NFS_MOUNT_SOFT)
  602. server->client->cl_softrtry = 1;
  603. return 0;
  604. }
  605. /*
  606. * Initialise an NFS2 or NFS3 client
  607. */
  608. static int nfs_init_client(struct nfs_client *clp,
  609. const struct rpc_timeout *timeparms,
  610. const struct nfs_parsed_mount_data *data)
  611. {
  612. int error;
  613. if (clp->cl_cons_state == NFS_CS_READY) {
  614. /* the client is already initialised */
  615. dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
  616. return 0;
  617. }
  618. /*
  619. * Create a client RPC handle for doing FSSTAT with UNIX auth only
  620. * - RFC 2623, sec 2.3.2
  621. */
  622. error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX,
  623. 0, data->flags & NFS_MOUNT_NORESVPORT);
  624. if (error < 0)
  625. goto error;
  626. nfs_mark_client_ready(clp, NFS_CS_READY);
  627. return 0;
  628. error:
  629. nfs_mark_client_ready(clp, error);
  630. dprintk("<-- nfs_init_client() = xerror %d\n", error);
  631. return error;
  632. }
  633. /*
  634. * Create a version 2 or 3 client
  635. */
  636. static int nfs_init_server(struct nfs_server *server,
  637. const struct nfs_parsed_mount_data *data)
  638. {
  639. struct nfs_client_initdata cl_init = {
  640. .hostname = data->nfs_server.hostname,
  641. .addr = (const struct sockaddr *)&data->nfs_server.address,
  642. .addrlen = data->nfs_server.addrlen,
  643. .rpc_ops = &nfs_v2_clientops,
  644. .proto = data->nfs_server.protocol,
  645. };
  646. struct rpc_timeout timeparms;
  647. struct nfs_client *clp;
  648. int error;
  649. dprintk("--> nfs_init_server()\n");
  650. #ifdef CONFIG_NFS_V3
  651. if (data->flags & NFS_MOUNT_VER3)
  652. cl_init.rpc_ops = &nfs_v3_clientops;
  653. #endif
  654. /* Allocate or find a client reference we can use */
  655. clp = nfs_get_client(&cl_init);
  656. if (IS_ERR(clp)) {
  657. dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
  658. return PTR_ERR(clp);
  659. }
  660. nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
  661. data->timeo, data->retrans);
  662. error = nfs_init_client(clp, &timeparms, data);
  663. if (error < 0)
  664. goto error;
  665. server->nfs_client = clp;
  666. /* Initialise the client representation from the mount data */
  667. server->flags = data->flags;
  668. server->options = data->options;
  669. if (data->rsize)
  670. server->rsize = nfs_block_size(data->rsize, NULL);
  671. if (data->wsize)
  672. server->wsize = nfs_block_size(data->wsize, NULL);
  673. server->acregmin = data->acregmin * HZ;
  674. server->acregmax = data->acregmax * HZ;
  675. server->acdirmin = data->acdirmin * HZ;
  676. server->acdirmax = data->acdirmax * HZ;
  677. /* Start lockd here, before we might error out */
  678. error = nfs_start_lockd(server);
  679. if (error < 0)
  680. goto error;
  681. server->port = data->nfs_server.port;
  682. error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
  683. if (error < 0)
  684. goto error;
  685. /* Preserve the values of mount_server-related mount options */
  686. if (data->mount_server.addrlen) {
  687. memcpy(&server->mountd_address, &data->mount_server.address,
  688. data->mount_server.addrlen);
  689. server->mountd_addrlen = data->mount_server.addrlen;
  690. }
  691. server->mountd_version = data->mount_server.version;
  692. server->mountd_port = data->mount_server.port;
  693. server->mountd_protocol = data->mount_server.protocol;
  694. server->namelen = data->namlen;
  695. /* Create a client RPC handle for the NFSv3 ACL management interface */
  696. nfs_init_server_aclclient(server);
  697. dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
  698. return 0;
  699. error:
  700. server->nfs_client = NULL;
  701. nfs_put_client(clp);
  702. dprintk("<-- nfs_init_server() = xerror %d\n", error);
  703. return error;
  704. }
  705. /*
  706. * Load up the server record from information gained in an fsinfo record
  707. */
  708. static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
  709. {
  710. unsigned long max_rpc_payload;
  711. /* Work out a lot of parameters */
  712. if (server->rsize == 0)
  713. server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
  714. if (server->wsize == 0)
  715. server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
  716. if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
  717. server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
  718. if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
  719. server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
  720. max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
  721. if (server->rsize > max_rpc_payload)
  722. server->rsize = max_rpc_payload;
  723. if (server->rsize > NFS_MAX_FILE_IO_SIZE)
  724. server->rsize = NFS_MAX_FILE_IO_SIZE;
  725. server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  726. server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
  727. if (server->wsize > max_rpc_payload)
  728. server->wsize = max_rpc_payload;
  729. if (server->wsize > NFS_MAX_FILE_IO_SIZE)
  730. server->wsize = NFS_MAX_FILE_IO_SIZE;
  731. server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  732. server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
  733. server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
  734. if (server->dtsize > PAGE_CACHE_SIZE)
  735. server->dtsize = PAGE_CACHE_SIZE;
  736. if (server->dtsize > server->rsize)
  737. server->dtsize = server->rsize;
  738. if (server->flags & NFS_MOUNT_NOAC) {
  739. server->acregmin = server->acregmax = 0;
  740. server->acdirmin = server->acdirmax = 0;
  741. }
  742. server->maxfilesize = fsinfo->maxfilesize;
  743. /* We're airborne Set socket buffersize */
  744. rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
  745. }
  746. /*
  747. * Probe filesystem information, including the FSID on v2/v3
  748. */
  749. static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
  750. {
  751. struct nfs_fsinfo fsinfo;
  752. struct nfs_client *clp = server->nfs_client;
  753. int error;
  754. dprintk("--> nfs_probe_fsinfo()\n");
  755. if (clp->rpc_ops->set_capabilities != NULL) {
  756. error = clp->rpc_ops->set_capabilities(server, mntfh);
  757. if (error < 0)
  758. goto out_error;
  759. }
  760. fsinfo.fattr = fattr;
  761. nfs_fattr_init(fattr);
  762. error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
  763. if (error < 0)
  764. goto out_error;
  765. nfs_server_set_fsinfo(server, &fsinfo);
  766. error = bdi_init(&server->backing_dev_info);
  767. if (error)
  768. goto out_error;
  769. /* Get some general file system info */
  770. if (server->namelen == 0) {
  771. struct nfs_pathconf pathinfo;
  772. pathinfo.fattr = fattr;
  773. nfs_fattr_init(fattr);
  774. if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
  775. server->namelen = pathinfo.max_namelen;
  776. }
  777. dprintk("<-- nfs_probe_fsinfo() = 0\n");
  778. return 0;
  779. out_error:
  780. dprintk("nfs_probe_fsinfo: error = %d\n", -error);
  781. return error;
  782. }
  783. /*
  784. * Copy useful information when duplicating a server record
  785. */
  786. static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
  787. {
  788. target->flags = source->flags;
  789. target->acregmin = source->acregmin;
  790. target->acregmax = source->acregmax;
  791. target->acdirmin = source->acdirmin;
  792. target->acdirmax = source->acdirmax;
  793. target->caps = source->caps;
  794. }
  795. /*
  796. * Allocate and initialise a server record
  797. */
  798. static struct nfs_server *nfs_alloc_server(void)
  799. {
  800. struct nfs_server *server;
  801. server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
  802. if (!server)
  803. return NULL;
  804. server->client = server->client_acl = ERR_PTR(-EINVAL);
  805. /* Zero out the NFS state stuff */
  806. INIT_LIST_HEAD(&server->client_link);
  807. INIT_LIST_HEAD(&server->master_link);
  808. atomic_set(&server->active, 0);
  809. server->io_stats = nfs_alloc_iostats();
  810. if (!server->io_stats) {
  811. kfree(server);
  812. return NULL;
  813. }
  814. return server;
  815. }
  816. /*
  817. * Free up a server record
  818. */
  819. void nfs_free_server(struct nfs_server *server)
  820. {
  821. dprintk("--> nfs_free_server()\n");
  822. spin_lock(&nfs_client_lock);
  823. list_del(&server->client_link);
  824. list_del(&server->master_link);
  825. spin_unlock(&nfs_client_lock);
  826. if (server->destroy != NULL)
  827. server->destroy(server);
  828. if (!IS_ERR(server->client_acl))
  829. rpc_shutdown_client(server->client_acl);
  830. if (!IS_ERR(server->client))
  831. rpc_shutdown_client(server->client);
  832. nfs_put_client(server->nfs_client);
  833. nfs_free_iostats(server->io_stats);
  834. bdi_destroy(&server->backing_dev_info);
  835. kfree(server);
  836. nfs_release_automount_timer();
  837. dprintk("<-- nfs_free_server()\n");
  838. }
  839. /*
  840. * Create a version 2 or 3 volume record
  841. * - keyed on server and FSID
  842. */
  843. struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
  844. struct nfs_fh *mntfh)
  845. {
  846. struct nfs_server *server;
  847. struct nfs_fattr fattr;
  848. int error;
  849. server = nfs_alloc_server();
  850. if (!server)
  851. return ERR_PTR(-ENOMEM);
  852. /* Get a client representation */
  853. error = nfs_init_server(server, data);
  854. if (error < 0)
  855. goto error;
  856. BUG_ON(!server->nfs_client);
  857. BUG_ON(!server->nfs_client->rpc_ops);
  858. BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
  859. /* Probe the root fh to retrieve its FSID */
  860. error = nfs_probe_fsinfo(server, mntfh, &fattr);
  861. if (error < 0)
  862. goto error;
  863. if (server->nfs_client->rpc_ops->version == 3) {
  864. if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
  865. server->namelen = NFS3_MAXNAMLEN;
  866. if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
  867. server->caps |= NFS_CAP_READDIRPLUS;
  868. } else {
  869. if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
  870. server->namelen = NFS2_MAXNAMLEN;
  871. }
  872. if (!(fattr.valid & NFS_ATTR_FATTR)) {
  873. error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
  874. if (error < 0) {
  875. dprintk("nfs_create_server: getattr error = %d\n", -error);
  876. goto error;
  877. }
  878. }
  879. memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
  880. dprintk("Server FSID: %llx:%llx\n",
  881. (unsigned long long) server->fsid.major,
  882. (unsigned long long) server->fsid.minor);
  883. BUG_ON(!server->nfs_client);
  884. BUG_ON(!server->nfs_client->rpc_ops);
  885. BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
  886. spin_lock(&nfs_client_lock);
  887. list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
  888. list_add_tail(&server->master_link, &nfs_volume_list);
  889. spin_unlock(&nfs_client_lock);
  890. server->mount_time = jiffies;
  891. return server;
  892. error:
  893. nfs_free_server(server);
  894. return ERR_PTR(error);
  895. }
  896. #ifdef CONFIG_NFS_V4
  897. /*
  898. * Initialize the minor version specific parts of an NFS4 client record
  899. */
  900. static int nfs4_init_client_minor_version(struct nfs_client *clp)
  901. {
  902. clp->cl_call_sync = _nfs4_call_sync;
  903. #if defined(CONFIG_NFS_V4_1)
  904. if (clp->cl_minorversion) {
  905. struct nfs4_session *session = NULL;
  906. /*
  907. * Create the session and mark it expired.
  908. * When a SEQUENCE operation encounters the expired session
  909. * it will do session recovery to initialize it.
  910. */
  911. session = nfs4_alloc_session(clp);
  912. if (!session)
  913. return -ENOMEM;
  914. clp->cl_session = session;
  915. clp->cl_call_sync = _nfs4_call_sync_session;
  916. }
  917. #endif /* CONFIG_NFS_V4_1 */
  918. return 0;
  919. }
  920. /*
  921. * Initialise an NFS4 client record
  922. */
  923. static int nfs4_init_client(struct nfs_client *clp,
  924. const struct rpc_timeout *timeparms,
  925. const char *ip_addr,
  926. rpc_authflavor_t authflavour,
  927. int flags)
  928. {
  929. int error;
  930. if (clp->cl_cons_state == NFS_CS_READY) {
  931. /* the client is initialised already */
  932. dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
  933. return 0;
  934. }
  935. /* Check NFS protocol revision and initialize RPC op vector */
  936. clp->rpc_ops = &nfs_v4_clientops;
  937. error = nfs_create_rpc_client(clp, timeparms, authflavour,
  938. 1, flags & NFS_MOUNT_NORESVPORT);
  939. if (error < 0)
  940. goto error;
  941. memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
  942. error = nfs_idmap_new(clp);
  943. if (error < 0) {
  944. dprintk("%s: failed to create idmapper. Error = %d\n",
  945. __func__, error);
  946. goto error;
  947. }
  948. __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
  949. error = nfs4_init_client_minor_version(clp);
  950. if (error < 0)
  951. goto error;
  952. nfs_mark_client_ready(clp, NFS_CS_READY);
  953. return 0;
  954. error:
  955. nfs_mark_client_ready(clp, error);
  956. dprintk("<-- nfs4_init_client() = xerror %d\n", error);
  957. return error;
  958. }
  959. /*
  960. * Set up an NFS4 client
  961. */
  962. static int nfs4_set_client(struct nfs_server *server,
  963. const char *hostname,
  964. const struct sockaddr *addr,
  965. const size_t addrlen,
  966. const char *ip_addr,
  967. rpc_authflavor_t authflavour,
  968. int proto, const struct rpc_timeout *timeparms,
  969. u32 minorversion)
  970. {
  971. struct nfs_client_initdata cl_init = {
  972. .hostname = hostname,
  973. .addr = addr,
  974. .addrlen = addrlen,
  975. .rpc_ops = &nfs_v4_clientops,
  976. .proto = proto,
  977. .minorversion = minorversion,
  978. };
  979. struct nfs_client *clp;
  980. int error;
  981. dprintk("--> nfs4_set_client()\n");
  982. /* Allocate or find a client reference we can use */
  983. clp = nfs_get_client(&cl_init);
  984. if (IS_ERR(clp)) {
  985. error = PTR_ERR(clp);
  986. goto error;
  987. }
  988. error = nfs4_init_client(clp, timeparms, ip_addr, authflavour,
  989. server->flags);
  990. if (error < 0)
  991. goto error_put;
  992. server->nfs_client = clp;
  993. dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
  994. return 0;
  995. error_put:
  996. nfs_put_client(clp);
  997. error:
  998. dprintk("<-- nfs4_set_client() = xerror %d\n", error);
  999. return error;
  1000. }
  1001. /*
  1002. * Initialize a session.
  1003. * Note: save the mount rsize and wsize for create_server negotiation.
  1004. */
  1005. static void nfs4_init_session(struct nfs_client *clp,
  1006. unsigned int wsize, unsigned int rsize)
  1007. {
  1008. #if defined(CONFIG_NFS_V4_1)
  1009. if (nfs4_has_session(clp)) {
  1010. clp->cl_session->fc_attrs.max_rqst_sz = wsize;
  1011. clp->cl_session->fc_attrs.max_resp_sz = rsize;
  1012. }
  1013. #endif /* CONFIG_NFS_V4_1 */
  1014. }
  1015. /*
  1016. * Create a version 4 volume record
  1017. */
  1018. static int nfs4_init_server(struct nfs_server *server,
  1019. const struct nfs_parsed_mount_data *data)
  1020. {
  1021. struct rpc_timeout timeparms;
  1022. int error;
  1023. dprintk("--> nfs4_init_server()\n");
  1024. nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
  1025. data->timeo, data->retrans);
  1026. /* Initialise the client representation from the mount data */
  1027. server->flags = data->flags;
  1028. server->caps |= NFS_CAP_ATOMIC_OPEN;
  1029. server->options = data->options;
  1030. /* Get a client record */
  1031. error = nfs4_set_client(server,
  1032. data->nfs_server.hostname,
  1033. (const struct sockaddr *)&data->nfs_server.address,
  1034. data->nfs_server.addrlen,
  1035. data->client_address,
  1036. data->auth_flavors[0],
  1037. data->nfs_server.protocol,
  1038. &timeparms,
  1039. data->minorversion);
  1040. if (error < 0)
  1041. goto error;
  1042. if (data->rsize)
  1043. server->rsize = nfs_block_size(data->rsize, NULL);
  1044. if (data->wsize)
  1045. server->wsize = nfs_block_size(data->wsize, NULL);
  1046. server->acregmin = data->acregmin * HZ;
  1047. server->acregmax = data->acregmax * HZ;
  1048. server->acdirmin = data->acdirmin * HZ;
  1049. server->acdirmax = data->acdirmax * HZ;
  1050. server->port = data->nfs_server.port;
  1051. error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
  1052. error:
  1053. /* Done */
  1054. dprintk("<-- nfs4_init_server() = %d\n", error);
  1055. return error;
  1056. }
  1057. /*
  1058. * Create a version 4 volume record
  1059. * - keyed on server and FSID
  1060. */
  1061. struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
  1062. struct nfs_fh *mntfh)
  1063. {
  1064. struct nfs_fattr fattr;
  1065. struct nfs_server *server;
  1066. int error;
  1067. dprintk("--> nfs4_create_server()\n");
  1068. server = nfs_alloc_server();
  1069. if (!server)
  1070. return ERR_PTR(-ENOMEM);
  1071. /* set up the general RPC client */
  1072. error = nfs4_init_server(server, data);
  1073. if (error < 0)
  1074. goto error;
  1075. BUG_ON(!server->nfs_client);
  1076. BUG_ON(!server->nfs_client->rpc_ops);
  1077. BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
  1078. nfs4_init_session(server->nfs_client, server->wsize, server->rsize);
  1079. /* Probe the root fh to retrieve its FSID */
  1080. error = nfs4_path_walk(server, mntfh, data->nfs_server.export_path);
  1081. if (error < 0)
  1082. goto error;
  1083. dprintk("Server FSID: %llx:%llx\n",
  1084. (unsigned long long) server->fsid.major,
  1085. (unsigned long long) server->fsid.minor);
  1086. dprintk("Mount FH: %d\n", mntfh->size);
  1087. error = nfs_probe_fsinfo(server, mntfh, &fattr);
  1088. if (error < 0)
  1089. goto error;
  1090. if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
  1091. server->namelen = NFS4_MAXNAMLEN;
  1092. BUG_ON(!server->nfs_client);
  1093. BUG_ON(!server->nfs_client->rpc_ops);
  1094. BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
  1095. spin_lock(&nfs_client_lock);
  1096. list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
  1097. list_add_tail(&server->master_link, &nfs_volume_list);
  1098. spin_unlock(&nfs_client_lock);
  1099. server->mount_time = jiffies;
  1100. dprintk("<-- nfs4_create_server() = %p\n", server);
  1101. return server;
  1102. error:
  1103. nfs_free_server(server);
  1104. dprintk("<-- nfs4_create_server() = error %d\n", error);
  1105. return ERR_PTR(error);
  1106. }
  1107. /*
  1108. * Create an NFS4 referral server record
  1109. */
  1110. struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
  1111. struct nfs_fh *mntfh)
  1112. {
  1113. struct nfs_client *parent_client;
  1114. struct nfs_server *server, *parent_server;
  1115. struct nfs_fattr fattr;
  1116. int error;
  1117. dprintk("--> nfs4_create_referral_server()\n");
  1118. server = nfs_alloc_server();
  1119. if (!server)
  1120. return ERR_PTR(-ENOMEM);
  1121. parent_server = NFS_SB(data->sb);
  1122. parent_client = parent_server->nfs_client;
  1123. /* Initialise the client representation from the parent server */
  1124. nfs_server_copy_userdata(server, parent_server);
  1125. server->caps |= NFS_CAP_ATOMIC_OPEN;
  1126. /* Get a client representation.
  1127. * Note: NFSv4 always uses TCP, */
  1128. error = nfs4_set_client(server, data->hostname,
  1129. data->addr,
  1130. data->addrlen,
  1131. parent_client->cl_ipaddr,
  1132. data->authflavor,
  1133. parent_server->client->cl_xprt->prot,
  1134. parent_server->client->cl_timeout,
  1135. parent_client->cl_minorversion);
  1136. if (error < 0)
  1137. goto error;
  1138. error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
  1139. if (error < 0)
  1140. goto error;
  1141. BUG_ON(!server->nfs_client);
  1142. BUG_ON(!server->nfs_client->rpc_ops);
  1143. BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
  1144. /* Probe the root fh to retrieve its FSID and filehandle */
  1145. error = nfs4_path_walk(server, mntfh, data->mnt_path);
  1146. if (error < 0)
  1147. goto error;
  1148. /* probe the filesystem info for this server filesystem */
  1149. error = nfs_probe_fsinfo(server, mntfh, &fattr);
  1150. if (error < 0)
  1151. goto error;
  1152. if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
  1153. server->namelen = NFS4_MAXNAMLEN;
  1154. dprintk("Referral FSID: %llx:%llx\n",
  1155. (unsigned long long) server->fsid.major,
  1156. (unsigned long long) server->fsid.minor);
  1157. spin_lock(&nfs_client_lock);
  1158. list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
  1159. list_add_tail(&server->master_link, &nfs_volume_list);
  1160. spin_unlock(&nfs_client_lock);
  1161. server->mount_time = jiffies;
  1162. dprintk("<-- nfs_create_referral_server() = %p\n", server);
  1163. return server;
  1164. error:
  1165. nfs_free_server(server);
  1166. dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
  1167. return ERR_PTR(error);
  1168. }
  1169. #endif /* CONFIG_NFS_V4 */
  1170. /*
  1171. * Clone an NFS2, NFS3 or NFS4 server record
  1172. */
  1173. struct nfs_server *nfs_clone_server(struct nfs_server *source,
  1174. struct nfs_fh *fh,
  1175. struct nfs_fattr *fattr)
  1176. {
  1177. struct nfs_server *server;
  1178. struct nfs_fattr fattr_fsinfo;
  1179. int error;
  1180. dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
  1181. (unsigned long long) fattr->fsid.major,
  1182. (unsigned long long) fattr->fsid.minor);
  1183. server = nfs_alloc_server();
  1184. if (!server)
  1185. return ERR_PTR(-ENOMEM);
  1186. /* Copy data from the source */
  1187. server->nfs_client = source->nfs_client;
  1188. atomic_inc(&server->nfs_client->cl_count);
  1189. nfs_server_copy_userdata(server, source);
  1190. server->fsid = fattr->fsid;
  1191. error = nfs_init_server_rpcclient(server,
  1192. source->client->cl_timeout,
  1193. source->client->cl_auth->au_flavor);
  1194. if (error < 0)
  1195. goto out_free_server;
  1196. if (!IS_ERR(source->client_acl))
  1197. nfs_init_server_aclclient(server);
  1198. /* probe the filesystem info for this server filesystem */
  1199. error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
  1200. if (error < 0)
  1201. goto out_free_server;
  1202. if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
  1203. server->namelen = NFS4_MAXNAMLEN;
  1204. dprintk("Cloned FSID: %llx:%llx\n",
  1205. (unsigned long long) server->fsid.major,
  1206. (unsigned long long) server->fsid.minor);
  1207. error = nfs_start_lockd(server);
  1208. if (error < 0)
  1209. goto out_free_server;
  1210. spin_lock(&nfs_client_lock);
  1211. list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
  1212. list_add_tail(&server->master_link, &nfs_volume_list);
  1213. spin_unlock(&nfs_client_lock);
  1214. server->mount_time = jiffies;
  1215. dprintk("<-- nfs_clone_server() = %p\n", server);
  1216. return server;
  1217. out_free_server:
  1218. nfs_free_server(server);
  1219. dprintk("<-- nfs_clone_server() = error %d\n", error);
  1220. return ERR_PTR(error);
  1221. }
  1222. #ifdef CONFIG_PROC_FS
  1223. static struct proc_dir_entry *proc_fs_nfs;
  1224. static int nfs_server_list_open(struct inode *inode, struct file *file);
  1225. static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
  1226. static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
  1227. static void nfs_server_list_stop(struct seq_file *p, void *v);
  1228. static int nfs_server_list_show(struct seq_file *m, void *v);
  1229. static struct seq_operations nfs_server_list_ops = {
  1230. .start = nfs_server_list_start,
  1231. .next = nfs_server_list_next,
  1232. .stop = nfs_server_list_stop,
  1233. .show = nfs_server_list_show,
  1234. };
  1235. static const struct file_operations nfs_server_list_fops = {
  1236. .open = nfs_server_list_open,
  1237. .read = seq_read,
  1238. .llseek = seq_lseek,
  1239. .release = seq_release,
  1240. .owner = THIS_MODULE,
  1241. };
  1242. static int nfs_volume_list_open(struct inode *inode, struct file *file);
  1243. static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
  1244. static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
  1245. static void nfs_volume_list_stop(struct seq_file *p, void *v);
  1246. static int nfs_volume_list_show(struct seq_file *m, void *v);
  1247. static struct seq_operations nfs_volume_list_ops = {
  1248. .start = nfs_volume_list_start,
  1249. .next = nfs_volume_list_next,
  1250. .stop = nfs_volume_list_stop,
  1251. .show = nfs_volume_list_show,
  1252. };
  1253. static const struct file_operations nfs_volume_list_fops = {
  1254. .open = nfs_volume_list_open,
  1255. .read = seq_read,
  1256. .llseek = seq_lseek,
  1257. .release = seq_release,
  1258. .owner = THIS_MODULE,
  1259. };
  1260. /*
  1261. * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
  1262. * we're dealing
  1263. */
  1264. static int nfs_server_list_open(struct inode *inode, struct file *file)
  1265. {
  1266. struct seq_file *m;
  1267. int ret;
  1268. ret = seq_open(file, &nfs_server_list_ops);
  1269. if (ret < 0)
  1270. return ret;
  1271. m = file->private_data;
  1272. m->private = PDE(inode)->data;
  1273. return 0;
  1274. }
  1275. /*
  1276. * set up the iterator to start reading from the server list and return the first item
  1277. */
  1278. static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
  1279. {
  1280. /* lock the list against modification */
  1281. spin_lock(&nfs_client_lock);
  1282. return seq_list_start_head(&nfs_client_list, *_pos);
  1283. }
  1284. /*
  1285. * move to next server
  1286. */
  1287. static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
  1288. {
  1289. return seq_list_next(v, &nfs_client_list, pos);
  1290. }
  1291. /*
  1292. * clean up after reading from the transports list
  1293. */
  1294. static void nfs_server_list_stop(struct seq_file *p, void *v)
  1295. {
  1296. spin_unlock(&nfs_client_lock);
  1297. }
  1298. /*
  1299. * display a header line followed by a load of call lines
  1300. */
  1301. static int nfs_server_list_show(struct seq_file *m, void *v)
  1302. {
  1303. struct nfs_client *clp;
  1304. /* display header on line 1 */
  1305. if (v == &nfs_client_list) {
  1306. seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
  1307. return 0;
  1308. }
  1309. /* display one transport per line on subsequent lines */
  1310. clp = list_entry(v, struct nfs_client, cl_share_link);
  1311. seq_printf(m, "v%u %s %s %3d %s\n",
  1312. clp->rpc_ops->version,
  1313. rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
  1314. rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
  1315. atomic_read(&clp->cl_count),
  1316. clp->cl_hostname);
  1317. return 0;
  1318. }
  1319. /*
  1320. * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
  1321. */
  1322. static int nfs_volume_list_open(struct inode *inode, struct file *file)
  1323. {
  1324. struct seq_file *m;
  1325. int ret;
  1326. ret = seq_open(file, &nfs_volume_list_ops);
  1327. if (ret < 0)
  1328. return ret;
  1329. m = file->private_data;
  1330. m->private = PDE(inode)->data;
  1331. return 0;
  1332. }
  1333. /*
  1334. * set up the iterator to start reading from the volume list and return the first item
  1335. */
  1336. static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
  1337. {
  1338. /* lock the list against modification */
  1339. spin_lock(&nfs_client_lock);
  1340. return seq_list_start_head(&nfs_volume_list, *_pos);
  1341. }
  1342. /*
  1343. * move to next volume
  1344. */
  1345. static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
  1346. {
  1347. return seq_list_next(v, &nfs_volume_list, pos);
  1348. }
  1349. /*
  1350. * clean up after reading from the transports list
  1351. */
  1352. static void nfs_volume_list_stop(struct seq_file *p, void *v)
  1353. {
  1354. spin_unlock(&nfs_client_lock);
  1355. }
  1356. /*
  1357. * display a header line followed by a load of call lines
  1358. */
  1359. static int nfs_volume_list_show(struct seq_file *m, void *v)
  1360. {
  1361. struct nfs_server *server;
  1362. struct nfs_client *clp;
  1363. char dev[8], fsid[17];
  1364. /* display header on line 1 */
  1365. if (v == &nfs_volume_list) {
  1366. seq_puts(m, "NV SERVER PORT DEV FSID FSC\n");
  1367. return 0;
  1368. }
  1369. /* display one transport per line on subsequent lines */
  1370. server = list_entry(v, struct nfs_server, master_link);
  1371. clp = server->nfs_client;
  1372. snprintf(dev, 8, "%u:%u",
  1373. MAJOR(server->s_dev), MINOR(server->s_dev));
  1374. snprintf(fsid, 17, "%llx:%llx",
  1375. (unsigned long long) server->fsid.major,
  1376. (unsigned long long) server->fsid.minor);
  1377. seq_printf(m, "v%u %s %s %-7s %-17s %s\n",
  1378. clp->rpc_ops->version,
  1379. rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
  1380. rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
  1381. dev,
  1382. fsid,
  1383. nfs_server_fscache_state(server));
  1384. return 0;
  1385. }
  1386. /*
  1387. * initialise the /proc/fs/nfsfs/ directory
  1388. */
  1389. int __init nfs_fs_proc_init(void)
  1390. {
  1391. struct proc_dir_entry *p;
  1392. proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
  1393. if (!proc_fs_nfs)
  1394. goto error_0;
  1395. /* a file of servers with which we're dealing */
  1396. p = proc_create("servers", S_IFREG|S_IRUGO,
  1397. proc_fs_nfs, &nfs_server_list_fops);
  1398. if (!p)
  1399. goto error_1;
  1400. /* a file of volumes that we have mounted */
  1401. p = proc_create("volumes", S_IFREG|S_IRUGO,
  1402. proc_fs_nfs, &nfs_volume_list_fops);
  1403. if (!p)
  1404. goto error_2;
  1405. return 0;
  1406. error_2:
  1407. remove_proc_entry("servers", proc_fs_nfs);
  1408. error_1:
  1409. remove_proc_entry("fs/nfsfs", NULL);
  1410. error_0:
  1411. return -ENOMEM;
  1412. }
  1413. /*
  1414. * clean up the /proc/fs/nfsfs/ directory
  1415. */
  1416. void nfs_fs_proc_exit(void)
  1417. {
  1418. remove_proc_entry("volumes", proc_fs_nfs);
  1419. remove_proc_entry("servers", proc_fs_nfs);
  1420. remove_proc_entry("fs/nfsfs", NULL);
  1421. }
  1422. #endif /* CONFIG_PROC_FS */