client.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463
  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/vfs.h>
  33. #include <linux/inet.h>
  34. #include <linux/in6.h>
  35. #include <linux/slab.h>
  36. #include <linux/idr.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 "nfs4_fs.h"
  43. #include "callback.h"
  44. #include "delegation.h"
  45. #include "iostat.h"
  46. #include "internal.h"
  47. #include "fscache.h"
  48. #include "pnfs.h"
  49. #include "nfs.h"
  50. #include "netns.h"
  51. #define NFSDBG_FACILITY NFSDBG_CLIENT
  52. static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
  53. static DEFINE_SPINLOCK(nfs_version_lock);
  54. static DEFINE_MUTEX(nfs_version_mutex);
  55. static LIST_HEAD(nfs_versions);
  56. /*
  57. * RPC cruft for NFS
  58. */
  59. static const struct rpc_version *nfs_version[5] = {
  60. [2] = NULL,
  61. [3] = NULL,
  62. [4] = NULL,
  63. };
  64. const struct rpc_program nfs_program = {
  65. .name = "nfs",
  66. .number = NFS_PROGRAM,
  67. .nrvers = ARRAY_SIZE(nfs_version),
  68. .version = nfs_version,
  69. .stats = &nfs_rpcstat,
  70. .pipe_dir_name = NFS_PIPE_DIRNAME,
  71. };
  72. struct rpc_stat nfs_rpcstat = {
  73. .program = &nfs_program
  74. };
  75. static struct nfs_subversion *find_nfs_version(unsigned int version)
  76. {
  77. struct nfs_subversion *nfs;
  78. spin_lock(&nfs_version_lock);
  79. list_for_each_entry(nfs, &nfs_versions, list) {
  80. if (nfs->rpc_ops->version == version) {
  81. spin_unlock(&nfs_version_lock);
  82. return nfs;
  83. }
  84. }
  85. spin_unlock(&nfs_version_lock);
  86. return ERR_PTR(-EPROTONOSUPPORT);
  87. }
  88. struct nfs_subversion *get_nfs_version(unsigned int version)
  89. {
  90. struct nfs_subversion *nfs = find_nfs_version(version);
  91. if (IS_ERR(nfs)) {
  92. mutex_lock(&nfs_version_mutex);
  93. request_module("nfsv%d", version);
  94. nfs = find_nfs_version(version);
  95. mutex_unlock(&nfs_version_mutex);
  96. }
  97. if (!IS_ERR(nfs) && !try_module_get(nfs->owner))
  98. return ERR_PTR(-EAGAIN);
  99. return nfs;
  100. }
  101. void put_nfs_version(struct nfs_subversion *nfs)
  102. {
  103. module_put(nfs->owner);
  104. }
  105. void register_nfs_version(struct nfs_subversion *nfs)
  106. {
  107. spin_lock(&nfs_version_lock);
  108. list_add(&nfs->list, &nfs_versions);
  109. nfs_version[nfs->rpc_ops->version] = nfs->rpc_vers;
  110. spin_unlock(&nfs_version_lock);
  111. }
  112. EXPORT_SYMBOL_GPL(register_nfs_version);
  113. void unregister_nfs_version(struct nfs_subversion *nfs)
  114. {
  115. spin_lock(&nfs_version_lock);
  116. nfs_version[nfs->rpc_ops->version] = NULL;
  117. list_del(&nfs->list);
  118. spin_unlock(&nfs_version_lock);
  119. }
  120. EXPORT_SYMBOL_GPL(unregister_nfs_version);
  121. /*
  122. * Allocate a shared client record
  123. *
  124. * Since these are allocated/deallocated very rarely, we don't
  125. * bother putting them in a slab cache...
  126. */
  127. struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
  128. {
  129. struct nfs_client *clp;
  130. struct rpc_cred *cred;
  131. int err = -ENOMEM;
  132. if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
  133. goto error_0;
  134. clp->cl_nfs_mod = cl_init->nfs_mod;
  135. if (!try_module_get(clp->cl_nfs_mod->owner))
  136. goto error_dealloc;
  137. clp->rpc_ops = clp->cl_nfs_mod->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->cl_net = get_net(cl_init->net);
  152. cred = rpc_lookup_machine_cred("*");
  153. if (!IS_ERR(cred))
  154. clp->cl_machine_cred = cred;
  155. nfs_fscache_get_client_cookie(clp);
  156. return clp;
  157. error_cleanup:
  158. put_nfs_version(clp->cl_nfs_mod);
  159. error_dealloc:
  160. kfree(clp);
  161. error_0:
  162. return ERR_PTR(err);
  163. }
  164. EXPORT_SYMBOL_GPL(nfs_alloc_client);
  165. #if IS_ENABLED(CONFIG_NFS_V4)
  166. void nfs_cleanup_cb_ident_idr(struct net *net)
  167. {
  168. struct nfs_net *nn = net_generic(net, nfs_net_id);
  169. idr_destroy(&nn->cb_ident_idr);
  170. }
  171. /* nfs_client_lock held */
  172. static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
  173. {
  174. struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
  175. if (clp->cl_cb_ident)
  176. idr_remove(&nn->cb_ident_idr, clp->cl_cb_ident);
  177. }
  178. static void pnfs_init_server(struct nfs_server *server)
  179. {
  180. rpc_init_wait_queue(&server->roc_rpcwaitq, "pNFS ROC");
  181. }
  182. #else
  183. void nfs_cleanup_cb_ident_idr(struct net *net)
  184. {
  185. }
  186. static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
  187. {
  188. }
  189. static void pnfs_init_server(struct nfs_server *server)
  190. {
  191. }
  192. #endif /* CONFIG_NFS_V4 */
  193. /*
  194. * Destroy a shared client record
  195. */
  196. void nfs_free_client(struct nfs_client *clp)
  197. {
  198. dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
  199. nfs_fscache_release_client_cookie(clp);
  200. /* -EIO all pending I/O */
  201. if (!IS_ERR(clp->cl_rpcclient))
  202. rpc_shutdown_client(clp->cl_rpcclient);
  203. if (clp->cl_machine_cred != NULL)
  204. put_rpccred(clp->cl_machine_cred);
  205. put_net(clp->cl_net);
  206. put_nfs_version(clp->cl_nfs_mod);
  207. kfree(clp->cl_hostname);
  208. kfree(clp->cl_acceptor);
  209. kfree(clp);
  210. dprintk("<-- nfs_free_client()\n");
  211. }
  212. EXPORT_SYMBOL_GPL(nfs_free_client);
  213. /*
  214. * Release a reference to a shared client record
  215. */
  216. void nfs_put_client(struct nfs_client *clp)
  217. {
  218. struct nfs_net *nn;
  219. if (!clp)
  220. return;
  221. dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
  222. nn = net_generic(clp->cl_net, nfs_net_id);
  223. if (atomic_dec_and_lock(&clp->cl_count, &nn->nfs_client_lock)) {
  224. list_del(&clp->cl_share_link);
  225. nfs_cb_idr_remove_locked(clp);
  226. spin_unlock(&nn->nfs_client_lock);
  227. WARN_ON_ONCE(!list_empty(&clp->cl_superblocks));
  228. clp->rpc_ops->free_client(clp);
  229. }
  230. }
  231. EXPORT_SYMBOL_GPL(nfs_put_client);
  232. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  233. /*
  234. * Test if two ip6 socket addresses refer to the same socket by
  235. * comparing relevant fields. The padding bytes specifically, are not
  236. * compared. sin6_flowinfo is not compared because it only affects QoS
  237. * and sin6_scope_id is only compared if the address is "link local"
  238. * because "link local" addresses need only be unique to a specific
  239. * link. Conversely, ordinary unicast addresses might have different
  240. * sin6_scope_id.
  241. *
  242. * The caller should ensure both socket addresses are AF_INET6.
  243. */
  244. static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
  245. const struct sockaddr *sa2)
  246. {
  247. const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
  248. const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
  249. if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
  250. return 0;
  251. else if (ipv6_addr_type(&sin1->sin6_addr) & IPV6_ADDR_LINKLOCAL)
  252. return sin1->sin6_scope_id == sin2->sin6_scope_id;
  253. return 1;
  254. }
  255. #else /* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */
  256. static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
  257. const struct sockaddr *sa2)
  258. {
  259. return 0;
  260. }
  261. #endif
  262. /*
  263. * Test if two ip4 socket addresses refer to the same socket, by
  264. * comparing relevant fields. The padding bytes specifically, are
  265. * not compared.
  266. *
  267. * The caller should ensure both socket addresses are AF_INET.
  268. */
  269. static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1,
  270. const struct sockaddr *sa2)
  271. {
  272. const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
  273. const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
  274. return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
  275. }
  276. static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1,
  277. const struct sockaddr *sa2)
  278. {
  279. const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
  280. const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
  281. return nfs_sockaddr_match_ipaddr6(sa1, sa2) &&
  282. (sin1->sin6_port == sin2->sin6_port);
  283. }
  284. static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
  285. const struct sockaddr *sa2)
  286. {
  287. const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
  288. const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
  289. return nfs_sockaddr_match_ipaddr4(sa1, sa2) &&
  290. (sin1->sin_port == sin2->sin_port);
  291. }
  292. #if defined(CONFIG_NFS_V4_1)
  293. /*
  294. * Test if two socket addresses represent the same actual socket,
  295. * by comparing (only) relevant fields, excluding the port number.
  296. */
  297. int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
  298. const struct sockaddr *sa2)
  299. {
  300. if (sa1->sa_family != sa2->sa_family)
  301. return 0;
  302. switch (sa1->sa_family) {
  303. case AF_INET:
  304. return nfs_sockaddr_match_ipaddr4(sa1, sa2);
  305. case AF_INET6:
  306. return nfs_sockaddr_match_ipaddr6(sa1, sa2);
  307. }
  308. return 0;
  309. }
  310. EXPORT_SYMBOL_GPL(nfs_sockaddr_match_ipaddr);
  311. #endif /* CONFIG_NFS_V4_1 */
  312. /*
  313. * Test if two socket addresses represent the same actual socket,
  314. * by comparing (only) relevant fields, including the port number.
  315. */
  316. static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
  317. const struct sockaddr *sa2)
  318. {
  319. if (sa1->sa_family != sa2->sa_family)
  320. return 0;
  321. switch (sa1->sa_family) {
  322. case AF_INET:
  323. return nfs_sockaddr_cmp_ip4(sa1, sa2);
  324. case AF_INET6:
  325. return nfs_sockaddr_cmp_ip6(sa1, sa2);
  326. }
  327. return 0;
  328. }
  329. /*
  330. * Find an nfs_client on the list that matches the initialisation data
  331. * that is supplied.
  332. */
  333. static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
  334. {
  335. struct nfs_client *clp;
  336. const struct sockaddr *sap = data->addr;
  337. struct nfs_net *nn = net_generic(data->net, nfs_net_id);
  338. list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
  339. const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
  340. /* Don't match clients that failed to initialise properly */
  341. if (clp->cl_cons_state < 0)
  342. continue;
  343. /* Different NFS versions cannot share the same nfs_client */
  344. if (clp->rpc_ops != data->nfs_mod->rpc_ops)
  345. continue;
  346. if (clp->cl_proto != data->proto)
  347. continue;
  348. /* Match nfsv4 minorversion */
  349. if (clp->cl_minorversion != data->minorversion)
  350. continue;
  351. /* Match the full socket address */
  352. if (!nfs_sockaddr_cmp(sap, clap))
  353. continue;
  354. atomic_inc(&clp->cl_count);
  355. return clp;
  356. }
  357. return NULL;
  358. }
  359. static bool nfs_client_init_is_complete(const struct nfs_client *clp)
  360. {
  361. return clp->cl_cons_state <= NFS_CS_READY;
  362. }
  363. int nfs_wait_client_init_complete(const struct nfs_client *clp)
  364. {
  365. return wait_event_killable(nfs_client_active_wq,
  366. nfs_client_init_is_complete(clp));
  367. }
  368. EXPORT_SYMBOL_GPL(nfs_wait_client_init_complete);
  369. /*
  370. * Found an existing client. Make sure it's ready before returning.
  371. */
  372. static struct nfs_client *
  373. nfs_found_client(const struct nfs_client_initdata *cl_init,
  374. struct nfs_client *clp)
  375. {
  376. int error;
  377. error = nfs_wait_client_init_complete(clp);
  378. if (error < 0) {
  379. nfs_put_client(clp);
  380. return ERR_PTR(-ERESTARTSYS);
  381. }
  382. if (clp->cl_cons_state < NFS_CS_READY) {
  383. error = clp->cl_cons_state;
  384. nfs_put_client(clp);
  385. return ERR_PTR(error);
  386. }
  387. smp_rmb();
  388. dprintk("<-- %s found nfs_client %p for %s\n",
  389. __func__, clp, cl_init->hostname ?: "");
  390. return clp;
  391. }
  392. /*
  393. * Look up a client by IP address and protocol version
  394. * - creates a new record if one doesn't yet exist
  395. */
  396. struct nfs_client *
  397. nfs_get_client(const struct nfs_client_initdata *cl_init,
  398. const struct rpc_timeout *timeparms,
  399. const char *ip_addr,
  400. rpc_authflavor_t authflavour)
  401. {
  402. struct nfs_client *clp, *new = NULL;
  403. struct nfs_net *nn = net_generic(cl_init->net, nfs_net_id);
  404. const struct nfs_rpc_ops *rpc_ops = cl_init->nfs_mod->rpc_ops;
  405. if (cl_init->hostname == NULL) {
  406. WARN_ON(1);
  407. return NULL;
  408. }
  409. dprintk("--> nfs_get_client(%s,v%u)\n",
  410. cl_init->hostname, rpc_ops->version);
  411. /* see if the client already exists */
  412. do {
  413. spin_lock(&nn->nfs_client_lock);
  414. clp = nfs_match_client(cl_init);
  415. if (clp) {
  416. spin_unlock(&nn->nfs_client_lock);
  417. if (new)
  418. new->rpc_ops->free_client(new);
  419. return nfs_found_client(cl_init, clp);
  420. }
  421. if (new) {
  422. list_add_tail(&new->cl_share_link,
  423. &nn->nfs_client_list);
  424. spin_unlock(&nn->nfs_client_lock);
  425. new->cl_flags = cl_init->init_flags;
  426. return rpc_ops->init_client(new, timeparms, ip_addr);
  427. }
  428. spin_unlock(&nn->nfs_client_lock);
  429. new = rpc_ops->alloc_client(cl_init);
  430. } while (!IS_ERR(new));
  431. dprintk("<-- nfs_get_client() Failed to find %s (%ld)\n",
  432. cl_init->hostname, PTR_ERR(new));
  433. return new;
  434. }
  435. EXPORT_SYMBOL_GPL(nfs_get_client);
  436. /*
  437. * Mark a server as ready or failed
  438. */
  439. void nfs_mark_client_ready(struct nfs_client *clp, int state)
  440. {
  441. smp_wmb();
  442. clp->cl_cons_state = state;
  443. wake_up_all(&nfs_client_active_wq);
  444. }
  445. EXPORT_SYMBOL_GPL(nfs_mark_client_ready);
  446. /*
  447. * Initialise the timeout values for a connection
  448. */
  449. void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
  450. unsigned int timeo, unsigned int retrans)
  451. {
  452. to->to_initval = timeo * HZ / 10;
  453. to->to_retries = retrans;
  454. switch (proto) {
  455. case XPRT_TRANSPORT_TCP:
  456. case XPRT_TRANSPORT_RDMA:
  457. if (to->to_retries == 0)
  458. to->to_retries = NFS_DEF_TCP_RETRANS;
  459. if (to->to_initval == 0)
  460. to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
  461. if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
  462. to->to_initval = NFS_MAX_TCP_TIMEOUT;
  463. to->to_increment = to->to_initval;
  464. to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
  465. if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
  466. to->to_maxval = NFS_MAX_TCP_TIMEOUT;
  467. if (to->to_maxval < to->to_initval)
  468. to->to_maxval = to->to_initval;
  469. to->to_exponential = 0;
  470. break;
  471. case XPRT_TRANSPORT_UDP:
  472. if (to->to_retries == 0)
  473. to->to_retries = NFS_DEF_UDP_RETRANS;
  474. if (!to->to_initval)
  475. to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
  476. if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
  477. to->to_initval = NFS_MAX_UDP_TIMEOUT;
  478. to->to_maxval = NFS_MAX_UDP_TIMEOUT;
  479. to->to_exponential = 1;
  480. break;
  481. default:
  482. BUG();
  483. }
  484. }
  485. EXPORT_SYMBOL_GPL(nfs_init_timeout_values);
  486. /*
  487. * Create an RPC client handle
  488. */
  489. int nfs_create_rpc_client(struct nfs_client *clp,
  490. const struct rpc_timeout *timeparms,
  491. rpc_authflavor_t flavor)
  492. {
  493. struct rpc_clnt *clnt = NULL;
  494. struct rpc_create_args args = {
  495. .net = clp->cl_net,
  496. .protocol = clp->cl_proto,
  497. .address = (struct sockaddr *)&clp->cl_addr,
  498. .addrsize = clp->cl_addrlen,
  499. .timeout = timeparms,
  500. .servername = clp->cl_hostname,
  501. .program = &nfs_program,
  502. .version = clp->rpc_ops->version,
  503. .authflavor = flavor,
  504. };
  505. if (test_bit(NFS_CS_DISCRTRY, &clp->cl_flags))
  506. args.flags |= RPC_CLNT_CREATE_DISCRTRY;
  507. if (test_bit(NFS_CS_NO_RETRANS_TIMEOUT, &clp->cl_flags))
  508. args.flags |= RPC_CLNT_CREATE_NO_RETRANS_TIMEOUT;
  509. if (test_bit(NFS_CS_NORESVPORT, &clp->cl_flags))
  510. args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
  511. if (test_bit(NFS_CS_INFINITE_SLOTS, &clp->cl_flags))
  512. args.flags |= RPC_CLNT_CREATE_INFINITE_SLOTS;
  513. if (!IS_ERR(clp->cl_rpcclient))
  514. return 0;
  515. clnt = rpc_create(&args);
  516. if (IS_ERR(clnt)) {
  517. dprintk("%s: cannot create RPC client. Error = %ld\n",
  518. __func__, PTR_ERR(clnt));
  519. return PTR_ERR(clnt);
  520. }
  521. clp->cl_rpcclient = clnt;
  522. return 0;
  523. }
  524. EXPORT_SYMBOL_GPL(nfs_create_rpc_client);
  525. /*
  526. * Version 2 or 3 client destruction
  527. */
  528. static void nfs_destroy_server(struct nfs_server *server)
  529. {
  530. if (server->nlm_host)
  531. nlmclnt_done(server->nlm_host);
  532. }
  533. /*
  534. * Version 2 or 3 lockd setup
  535. */
  536. static int nfs_start_lockd(struct nfs_server *server)
  537. {
  538. struct nlm_host *host;
  539. struct nfs_client *clp = server->nfs_client;
  540. struct nlmclnt_initdata nlm_init = {
  541. .hostname = clp->cl_hostname,
  542. .address = (struct sockaddr *)&clp->cl_addr,
  543. .addrlen = clp->cl_addrlen,
  544. .nfs_version = clp->rpc_ops->version,
  545. .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
  546. 1 : 0,
  547. .net = clp->cl_net,
  548. };
  549. if (nlm_init.nfs_version > 3)
  550. return 0;
  551. if ((server->flags & NFS_MOUNT_LOCAL_FLOCK) &&
  552. (server->flags & NFS_MOUNT_LOCAL_FCNTL))
  553. return 0;
  554. switch (clp->cl_proto) {
  555. default:
  556. nlm_init.protocol = IPPROTO_TCP;
  557. break;
  558. case XPRT_TRANSPORT_UDP:
  559. nlm_init.protocol = IPPROTO_UDP;
  560. }
  561. host = nlmclnt_init(&nlm_init);
  562. if (IS_ERR(host))
  563. return PTR_ERR(host);
  564. server->nlm_host = host;
  565. server->destroy = nfs_destroy_server;
  566. return 0;
  567. }
  568. /*
  569. * Create a general RPC client
  570. */
  571. int nfs_init_server_rpcclient(struct nfs_server *server,
  572. const struct rpc_timeout *timeo,
  573. rpc_authflavor_t pseudoflavour)
  574. {
  575. struct nfs_client *clp = server->nfs_client;
  576. server->client = rpc_clone_client_set_auth(clp->cl_rpcclient,
  577. pseudoflavour);
  578. if (IS_ERR(server->client)) {
  579. dprintk("%s: couldn't create rpc_client!\n", __func__);
  580. return PTR_ERR(server->client);
  581. }
  582. memcpy(&server->client->cl_timeout_default,
  583. timeo,
  584. sizeof(server->client->cl_timeout_default));
  585. server->client->cl_timeout = &server->client->cl_timeout_default;
  586. server->client->cl_softrtry = 0;
  587. if (server->flags & NFS_MOUNT_SOFT)
  588. server->client->cl_softrtry = 1;
  589. return 0;
  590. }
  591. EXPORT_SYMBOL_GPL(nfs_init_server_rpcclient);
  592. /**
  593. * nfs_init_client - Initialise an NFS2 or NFS3 client
  594. *
  595. * @clp: nfs_client to initialise
  596. * @timeparms: timeout parameters for underlying RPC transport
  597. * @ip_addr: IP presentation address (not used)
  598. *
  599. * Returns pointer to an NFS client, or an ERR_PTR value.
  600. */
  601. struct nfs_client *nfs_init_client(struct nfs_client *clp,
  602. const struct rpc_timeout *timeparms,
  603. const char *ip_addr)
  604. {
  605. int error;
  606. if (clp->cl_cons_state == NFS_CS_READY) {
  607. /* the client is already initialised */
  608. dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
  609. return clp;
  610. }
  611. /*
  612. * Create a client RPC handle for doing FSSTAT with UNIX auth only
  613. * - RFC 2623, sec 2.3.2
  614. */
  615. error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX);
  616. if (error < 0)
  617. goto error;
  618. nfs_mark_client_ready(clp, NFS_CS_READY);
  619. return clp;
  620. error:
  621. nfs_mark_client_ready(clp, error);
  622. nfs_put_client(clp);
  623. dprintk("<-- nfs_init_client() = xerror %d\n", error);
  624. return ERR_PTR(error);
  625. }
  626. EXPORT_SYMBOL_GPL(nfs_init_client);
  627. /*
  628. * Create a version 2 or 3 client
  629. */
  630. static int nfs_init_server(struct nfs_server *server,
  631. const struct nfs_parsed_mount_data *data,
  632. struct nfs_subversion *nfs_mod)
  633. {
  634. struct nfs_client_initdata cl_init = {
  635. .hostname = data->nfs_server.hostname,
  636. .addr = (const struct sockaddr *)&data->nfs_server.address,
  637. .addrlen = data->nfs_server.addrlen,
  638. .nfs_mod = nfs_mod,
  639. .proto = data->nfs_server.protocol,
  640. .net = data->net,
  641. };
  642. struct rpc_timeout timeparms;
  643. struct nfs_client *clp;
  644. int error;
  645. dprintk("--> nfs_init_server()\n");
  646. nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
  647. data->timeo, data->retrans);
  648. if (data->flags & NFS_MOUNT_NORESVPORT)
  649. set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags);
  650. /* Allocate or find a client reference we can use */
  651. clp = nfs_get_client(&cl_init, &timeparms, NULL, RPC_AUTH_UNIX);
  652. if (IS_ERR(clp)) {
  653. dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
  654. return PTR_ERR(clp);
  655. }
  656. server->nfs_client = clp;
  657. /* Initialise the client representation from the mount data */
  658. server->flags = data->flags;
  659. server->options = data->options;
  660. server->caps |= NFS_CAP_HARDLINKS|NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
  661. NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|NFS_CAP_OWNER_GROUP|
  662. NFS_CAP_ATIME|NFS_CAP_CTIME|NFS_CAP_MTIME|NFS_CAP_CHANGE_ATTR;
  663. if (data->rsize)
  664. server->rsize = nfs_block_size(data->rsize, NULL);
  665. if (data->wsize)
  666. server->wsize = nfs_block_size(data->wsize, NULL);
  667. server->acregmin = data->acregmin * HZ;
  668. server->acregmax = data->acregmax * HZ;
  669. server->acdirmin = data->acdirmin * HZ;
  670. server->acdirmax = data->acdirmax * HZ;
  671. /* Start lockd here, before we might error out */
  672. error = nfs_start_lockd(server);
  673. if (error < 0)
  674. goto error;
  675. server->port = data->nfs_server.port;
  676. server->auth_info = data->auth_info;
  677. error = nfs_init_server_rpcclient(server, &timeparms,
  678. data->selected_flavor);
  679. if (error < 0)
  680. goto error;
  681. /* Preserve the values of mount_server-related mount options */
  682. if (data->mount_server.addrlen) {
  683. memcpy(&server->mountd_address, &data->mount_server.address,
  684. data->mount_server.addrlen);
  685. server->mountd_addrlen = data->mount_server.addrlen;
  686. }
  687. server->mountd_version = data->mount_server.version;
  688. server->mountd_port = data->mount_server.port;
  689. server->mountd_protocol = data->mount_server.protocol;
  690. server->namelen = data->namlen;
  691. dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
  692. return 0;
  693. error:
  694. server->nfs_client = NULL;
  695. nfs_put_client(clp);
  696. dprintk("<-- nfs_init_server() = xerror %d\n", error);
  697. return error;
  698. }
  699. /*
  700. * Load up the server record from information gained in an fsinfo record
  701. */
  702. static void nfs_server_set_fsinfo(struct nfs_server *server,
  703. struct nfs_fsinfo *fsinfo)
  704. {
  705. unsigned long max_rpc_payload;
  706. /* Work out a lot of parameters */
  707. if (server->rsize == 0)
  708. server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
  709. if (server->wsize == 0)
  710. server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
  711. if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
  712. server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
  713. if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
  714. server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
  715. max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
  716. if (server->rsize > max_rpc_payload)
  717. server->rsize = max_rpc_payload;
  718. if (server->rsize > NFS_MAX_FILE_IO_SIZE)
  719. server->rsize = NFS_MAX_FILE_IO_SIZE;
  720. server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  721. server->backing_dev_info.name = "nfs";
  722. server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
  723. if (server->wsize > max_rpc_payload)
  724. server->wsize = max_rpc_payload;
  725. if (server->wsize > NFS_MAX_FILE_IO_SIZE)
  726. server->wsize = NFS_MAX_FILE_IO_SIZE;
  727. server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  728. server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
  729. server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
  730. if (server->dtsize > PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES)
  731. server->dtsize = PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES;
  732. if (server->dtsize > server->rsize)
  733. server->dtsize = server->rsize;
  734. if (server->flags & NFS_MOUNT_NOAC) {
  735. server->acregmin = server->acregmax = 0;
  736. server->acdirmin = server->acdirmax = 0;
  737. }
  738. server->maxfilesize = fsinfo->maxfilesize;
  739. server->time_delta = fsinfo->time_delta;
  740. /* We're airborne Set socket buffersize */
  741. rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
  742. }
  743. /*
  744. * Probe filesystem information, including the FSID on v2/v3
  745. */
  746. int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
  747. {
  748. struct nfs_fsinfo fsinfo;
  749. struct nfs_client *clp = server->nfs_client;
  750. int error;
  751. dprintk("--> nfs_probe_fsinfo()\n");
  752. if (clp->rpc_ops->set_capabilities != NULL) {
  753. error = clp->rpc_ops->set_capabilities(server, mntfh);
  754. if (error < 0)
  755. goto out_error;
  756. }
  757. fsinfo.fattr = fattr;
  758. fsinfo.layouttype = 0;
  759. error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
  760. if (error < 0)
  761. goto out_error;
  762. nfs_server_set_fsinfo(server, &fsinfo);
  763. /* Get some general file system info */
  764. if (server->namelen == 0) {
  765. struct nfs_pathconf pathinfo;
  766. pathinfo.fattr = fattr;
  767. nfs_fattr_init(fattr);
  768. if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
  769. server->namelen = pathinfo.max_namelen;
  770. }
  771. dprintk("<-- nfs_probe_fsinfo() = 0\n");
  772. return 0;
  773. out_error:
  774. dprintk("nfs_probe_fsinfo: error = %d\n", -error);
  775. return error;
  776. }
  777. EXPORT_SYMBOL_GPL(nfs_probe_fsinfo);
  778. /*
  779. * Copy useful information when duplicating a server record
  780. */
  781. void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
  782. {
  783. target->flags = source->flags;
  784. target->rsize = source->rsize;
  785. target->wsize = source->wsize;
  786. target->acregmin = source->acregmin;
  787. target->acregmax = source->acregmax;
  788. target->acdirmin = source->acdirmin;
  789. target->acdirmax = source->acdirmax;
  790. target->caps = source->caps;
  791. target->options = source->options;
  792. target->auth_info = source->auth_info;
  793. }
  794. EXPORT_SYMBOL_GPL(nfs_server_copy_userdata);
  795. void nfs_server_insert_lists(struct nfs_server *server)
  796. {
  797. struct nfs_client *clp = server->nfs_client;
  798. struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
  799. spin_lock(&nn->nfs_client_lock);
  800. list_add_tail_rcu(&server->client_link, &clp->cl_superblocks);
  801. list_add_tail(&server->master_link, &nn->nfs_volume_list);
  802. clear_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
  803. spin_unlock(&nn->nfs_client_lock);
  804. }
  805. EXPORT_SYMBOL_GPL(nfs_server_insert_lists);
  806. void nfs_server_remove_lists(struct nfs_server *server)
  807. {
  808. struct nfs_client *clp = server->nfs_client;
  809. struct nfs_net *nn;
  810. if (clp == NULL)
  811. return;
  812. nn = net_generic(clp->cl_net, nfs_net_id);
  813. spin_lock(&nn->nfs_client_lock);
  814. list_del_rcu(&server->client_link);
  815. if (list_empty(&clp->cl_superblocks))
  816. set_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
  817. list_del(&server->master_link);
  818. spin_unlock(&nn->nfs_client_lock);
  819. synchronize_rcu();
  820. }
  821. EXPORT_SYMBOL_GPL(nfs_server_remove_lists);
  822. /*
  823. * Allocate and initialise a server record
  824. */
  825. struct nfs_server *nfs_alloc_server(void)
  826. {
  827. struct nfs_server *server;
  828. server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
  829. if (!server)
  830. return NULL;
  831. server->client = server->client_acl = ERR_PTR(-EINVAL);
  832. /* Zero out the NFS state stuff */
  833. INIT_LIST_HEAD(&server->client_link);
  834. INIT_LIST_HEAD(&server->master_link);
  835. INIT_LIST_HEAD(&server->delegations);
  836. INIT_LIST_HEAD(&server->layouts);
  837. INIT_LIST_HEAD(&server->state_owners_lru);
  838. atomic_set(&server->active, 0);
  839. server->io_stats = nfs_alloc_iostats();
  840. if (!server->io_stats) {
  841. kfree(server);
  842. return NULL;
  843. }
  844. if (bdi_init(&server->backing_dev_info)) {
  845. nfs_free_iostats(server->io_stats);
  846. kfree(server);
  847. return NULL;
  848. }
  849. ida_init(&server->openowner_id);
  850. ida_init(&server->lockowner_id);
  851. pnfs_init_server(server);
  852. return server;
  853. }
  854. EXPORT_SYMBOL_GPL(nfs_alloc_server);
  855. /*
  856. * Free up a server record
  857. */
  858. void nfs_free_server(struct nfs_server *server)
  859. {
  860. dprintk("--> nfs_free_server()\n");
  861. nfs_server_remove_lists(server);
  862. if (server->destroy != NULL)
  863. server->destroy(server);
  864. if (!IS_ERR(server->client_acl))
  865. rpc_shutdown_client(server->client_acl);
  866. if (!IS_ERR(server->client))
  867. rpc_shutdown_client(server->client);
  868. nfs_put_client(server->nfs_client);
  869. ida_destroy(&server->lockowner_id);
  870. ida_destroy(&server->openowner_id);
  871. nfs_free_iostats(server->io_stats);
  872. bdi_destroy(&server->backing_dev_info);
  873. kfree(server);
  874. nfs_release_automount_timer();
  875. dprintk("<-- nfs_free_server()\n");
  876. }
  877. EXPORT_SYMBOL_GPL(nfs_free_server);
  878. /*
  879. * Create a version 2 or 3 volume record
  880. * - keyed on server and FSID
  881. */
  882. struct nfs_server *nfs_create_server(struct nfs_mount_info *mount_info,
  883. struct nfs_subversion *nfs_mod)
  884. {
  885. struct nfs_server *server;
  886. struct nfs_fattr *fattr;
  887. int error;
  888. server = nfs_alloc_server();
  889. if (!server)
  890. return ERR_PTR(-ENOMEM);
  891. error = -ENOMEM;
  892. fattr = nfs_alloc_fattr();
  893. if (fattr == NULL)
  894. goto error;
  895. /* Get a client representation */
  896. error = nfs_init_server(server, mount_info->parsed, nfs_mod);
  897. if (error < 0)
  898. goto error;
  899. /* Probe the root fh to retrieve its FSID */
  900. error = nfs_probe_fsinfo(server, mount_info->mntfh, fattr);
  901. if (error < 0)
  902. goto error;
  903. if (server->nfs_client->rpc_ops->version == 3) {
  904. if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
  905. server->namelen = NFS3_MAXNAMLEN;
  906. if (!(mount_info->parsed->flags & NFS_MOUNT_NORDIRPLUS))
  907. server->caps |= NFS_CAP_READDIRPLUS;
  908. } else {
  909. if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
  910. server->namelen = NFS2_MAXNAMLEN;
  911. }
  912. if (!(fattr->valid & NFS_ATTR_FATTR)) {
  913. error = nfs_mod->rpc_ops->getattr(server, mount_info->mntfh, fattr, NULL);
  914. if (error < 0) {
  915. dprintk("nfs_create_server: getattr error = %d\n", -error);
  916. goto error;
  917. }
  918. }
  919. memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
  920. dprintk("Server FSID: %llx:%llx\n",
  921. (unsigned long long) server->fsid.major,
  922. (unsigned long long) server->fsid.minor);
  923. nfs_server_insert_lists(server);
  924. server->mount_time = jiffies;
  925. nfs_free_fattr(fattr);
  926. return server;
  927. error:
  928. nfs_free_fattr(fattr);
  929. nfs_free_server(server);
  930. return ERR_PTR(error);
  931. }
  932. EXPORT_SYMBOL_GPL(nfs_create_server);
  933. /*
  934. * Clone an NFS2, NFS3 or NFS4 server record
  935. */
  936. struct nfs_server *nfs_clone_server(struct nfs_server *source,
  937. struct nfs_fh *fh,
  938. struct nfs_fattr *fattr,
  939. rpc_authflavor_t flavor)
  940. {
  941. struct nfs_server *server;
  942. struct nfs_fattr *fattr_fsinfo;
  943. int error;
  944. dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
  945. (unsigned long long) fattr->fsid.major,
  946. (unsigned long long) fattr->fsid.minor);
  947. server = nfs_alloc_server();
  948. if (!server)
  949. return ERR_PTR(-ENOMEM);
  950. error = -ENOMEM;
  951. fattr_fsinfo = nfs_alloc_fattr();
  952. if (fattr_fsinfo == NULL)
  953. goto out_free_server;
  954. /* Copy data from the source */
  955. server->nfs_client = source->nfs_client;
  956. server->destroy = source->destroy;
  957. atomic_inc(&server->nfs_client->cl_count);
  958. nfs_server_copy_userdata(server, source);
  959. server->fsid = fattr->fsid;
  960. error = nfs_init_server_rpcclient(server,
  961. source->client->cl_timeout,
  962. flavor);
  963. if (error < 0)
  964. goto out_free_server;
  965. /* probe the filesystem info for this server filesystem */
  966. error = nfs_probe_fsinfo(server, fh, fattr_fsinfo);
  967. if (error < 0)
  968. goto out_free_server;
  969. if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
  970. server->namelen = NFS4_MAXNAMLEN;
  971. dprintk("Cloned FSID: %llx:%llx\n",
  972. (unsigned long long) server->fsid.major,
  973. (unsigned long long) server->fsid.minor);
  974. error = nfs_start_lockd(server);
  975. if (error < 0)
  976. goto out_free_server;
  977. nfs_server_insert_lists(server);
  978. server->mount_time = jiffies;
  979. nfs_free_fattr(fattr_fsinfo);
  980. dprintk("<-- nfs_clone_server() = %p\n", server);
  981. return server;
  982. out_free_server:
  983. nfs_free_fattr(fattr_fsinfo);
  984. nfs_free_server(server);
  985. dprintk("<-- nfs_clone_server() = error %d\n", error);
  986. return ERR_PTR(error);
  987. }
  988. EXPORT_SYMBOL_GPL(nfs_clone_server);
  989. void nfs_clients_init(struct net *net)
  990. {
  991. struct nfs_net *nn = net_generic(net, nfs_net_id);
  992. INIT_LIST_HEAD(&nn->nfs_client_list);
  993. INIT_LIST_HEAD(&nn->nfs_volume_list);
  994. #if IS_ENABLED(CONFIG_NFS_V4)
  995. idr_init(&nn->cb_ident_idr);
  996. #endif
  997. spin_lock_init(&nn->nfs_client_lock);
  998. nn->boot_time = CURRENT_TIME;
  999. }
  1000. #ifdef CONFIG_PROC_FS
  1001. static int nfs_server_list_open(struct inode *inode, struct file *file);
  1002. static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
  1003. static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
  1004. static void nfs_server_list_stop(struct seq_file *p, void *v);
  1005. static int nfs_server_list_show(struct seq_file *m, void *v);
  1006. static const struct seq_operations nfs_server_list_ops = {
  1007. .start = nfs_server_list_start,
  1008. .next = nfs_server_list_next,
  1009. .stop = nfs_server_list_stop,
  1010. .show = nfs_server_list_show,
  1011. };
  1012. static const struct file_operations nfs_server_list_fops = {
  1013. .open = nfs_server_list_open,
  1014. .read = seq_read,
  1015. .llseek = seq_lseek,
  1016. .release = seq_release_net,
  1017. .owner = THIS_MODULE,
  1018. };
  1019. static int nfs_volume_list_open(struct inode *inode, struct file *file);
  1020. static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
  1021. static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
  1022. static void nfs_volume_list_stop(struct seq_file *p, void *v);
  1023. static int nfs_volume_list_show(struct seq_file *m, void *v);
  1024. static const struct seq_operations nfs_volume_list_ops = {
  1025. .start = nfs_volume_list_start,
  1026. .next = nfs_volume_list_next,
  1027. .stop = nfs_volume_list_stop,
  1028. .show = nfs_volume_list_show,
  1029. };
  1030. static const struct file_operations nfs_volume_list_fops = {
  1031. .open = nfs_volume_list_open,
  1032. .read = seq_read,
  1033. .llseek = seq_lseek,
  1034. .release = seq_release_net,
  1035. .owner = THIS_MODULE,
  1036. };
  1037. /*
  1038. * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
  1039. * we're dealing
  1040. */
  1041. static int nfs_server_list_open(struct inode *inode, struct file *file)
  1042. {
  1043. return seq_open_net(inode, file, &nfs_server_list_ops,
  1044. sizeof(struct seq_net_private));
  1045. }
  1046. /*
  1047. * set up the iterator to start reading from the server list and return the first item
  1048. */
  1049. static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
  1050. __acquires(&nn->nfs_client_lock)
  1051. {
  1052. struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id);
  1053. /* lock the list against modification */
  1054. spin_lock(&nn->nfs_client_lock);
  1055. return seq_list_start_head(&nn->nfs_client_list, *_pos);
  1056. }
  1057. /*
  1058. * move to next server
  1059. */
  1060. static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
  1061. {
  1062. struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id);
  1063. return seq_list_next(v, &nn->nfs_client_list, pos);
  1064. }
  1065. /*
  1066. * clean up after reading from the transports list
  1067. */
  1068. static void nfs_server_list_stop(struct seq_file *p, void *v)
  1069. __releases(&nn->nfs_client_lock)
  1070. {
  1071. struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id);
  1072. spin_unlock(&nn->nfs_client_lock);
  1073. }
  1074. /*
  1075. * display a header line followed by a load of call lines
  1076. */
  1077. static int nfs_server_list_show(struct seq_file *m, void *v)
  1078. {
  1079. struct nfs_client *clp;
  1080. struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id);
  1081. /* display header on line 1 */
  1082. if (v == &nn->nfs_client_list) {
  1083. seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
  1084. return 0;
  1085. }
  1086. /* display one transport per line on subsequent lines */
  1087. clp = list_entry(v, struct nfs_client, cl_share_link);
  1088. /* Check if the client is initialized */
  1089. if (clp->cl_cons_state != NFS_CS_READY)
  1090. return 0;
  1091. rcu_read_lock();
  1092. seq_printf(m, "v%u %s %s %3d %s\n",
  1093. clp->rpc_ops->version,
  1094. rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
  1095. rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
  1096. atomic_read(&clp->cl_count),
  1097. clp->cl_hostname);
  1098. rcu_read_unlock();
  1099. return 0;
  1100. }
  1101. /*
  1102. * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
  1103. */
  1104. static int nfs_volume_list_open(struct inode *inode, struct file *file)
  1105. {
  1106. return seq_open_net(inode, file, &nfs_volume_list_ops,
  1107. sizeof(struct seq_net_private));
  1108. }
  1109. /*
  1110. * set up the iterator to start reading from the volume list and return the first item
  1111. */
  1112. static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
  1113. __acquires(&nn->nfs_client_lock)
  1114. {
  1115. struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id);
  1116. /* lock the list against modification */
  1117. spin_lock(&nn->nfs_client_lock);
  1118. return seq_list_start_head(&nn->nfs_volume_list, *_pos);
  1119. }
  1120. /*
  1121. * move to next volume
  1122. */
  1123. static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
  1124. {
  1125. struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id);
  1126. return seq_list_next(v, &nn->nfs_volume_list, pos);
  1127. }
  1128. /*
  1129. * clean up after reading from the transports list
  1130. */
  1131. static void nfs_volume_list_stop(struct seq_file *p, void *v)
  1132. __releases(&nn->nfs_client_lock)
  1133. {
  1134. struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id);
  1135. spin_unlock(&nn->nfs_client_lock);
  1136. }
  1137. /*
  1138. * display a header line followed by a load of call lines
  1139. */
  1140. static int nfs_volume_list_show(struct seq_file *m, void *v)
  1141. {
  1142. struct nfs_server *server;
  1143. struct nfs_client *clp;
  1144. char dev[13]; // 8 for 2^24, 1 for ':', 3 for 2^8, 1 for '\0'
  1145. char fsid[34]; // 2 * 16 for %llx, 1 for ':', 1 for '\0'
  1146. struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id);
  1147. /* display header on line 1 */
  1148. if (v == &nn->nfs_volume_list) {
  1149. seq_puts(m, "NV SERVER PORT DEV FSID"
  1150. " FSC\n");
  1151. return 0;
  1152. }
  1153. /* display one transport per line on subsequent lines */
  1154. server = list_entry(v, struct nfs_server, master_link);
  1155. clp = server->nfs_client;
  1156. snprintf(dev, sizeof(dev), "%u:%u",
  1157. MAJOR(server->s_dev), MINOR(server->s_dev));
  1158. snprintf(fsid, sizeof(fsid), "%llx:%llx",
  1159. (unsigned long long) server->fsid.major,
  1160. (unsigned long long) server->fsid.minor);
  1161. rcu_read_lock();
  1162. seq_printf(m, "v%u %s %s %-12s %-33s %s\n",
  1163. clp->rpc_ops->version,
  1164. rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
  1165. rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
  1166. dev,
  1167. fsid,
  1168. nfs_server_fscache_state(server));
  1169. rcu_read_unlock();
  1170. return 0;
  1171. }
  1172. int nfs_fs_proc_net_init(struct net *net)
  1173. {
  1174. struct nfs_net *nn = net_generic(net, nfs_net_id);
  1175. struct proc_dir_entry *p;
  1176. nn->proc_nfsfs = proc_net_mkdir(net, "nfsfs", net->proc_net);
  1177. if (!nn->proc_nfsfs)
  1178. goto error_0;
  1179. /* a file of servers with which we're dealing */
  1180. p = proc_create("servers", S_IFREG|S_IRUGO,
  1181. nn->proc_nfsfs, &nfs_server_list_fops);
  1182. if (!p)
  1183. goto error_1;
  1184. /* a file of volumes that we have mounted */
  1185. p = proc_create("volumes", S_IFREG|S_IRUGO,
  1186. nn->proc_nfsfs, &nfs_volume_list_fops);
  1187. if (!p)
  1188. goto error_1;
  1189. return 0;
  1190. error_1:
  1191. remove_proc_subtree("nfsfs", net->proc_net);
  1192. error_0:
  1193. return -ENOMEM;
  1194. }
  1195. void nfs_fs_proc_net_exit(struct net *net)
  1196. {
  1197. remove_proc_subtree("nfsfs", net->proc_net);
  1198. }
  1199. /*
  1200. * initialise the /proc/fs/nfsfs/ directory
  1201. */
  1202. int __init nfs_fs_proc_init(void)
  1203. {
  1204. if (!proc_mkdir("fs/nfsfs", NULL))
  1205. goto error_0;
  1206. /* a file of servers with which we're dealing */
  1207. if (!proc_symlink("fs/nfsfs/servers", NULL, "../../net/nfsfs/servers"))
  1208. goto error_1;
  1209. /* a file of volumes that we have mounted */
  1210. if (!proc_symlink("fs/nfsfs/volumes", NULL, "../../net/nfsfs/volumes"))
  1211. goto error_1;
  1212. return 0;
  1213. error_1:
  1214. remove_proc_subtree("fs/nfsfs", NULL);
  1215. error_0:
  1216. return -ENOMEM;
  1217. }
  1218. /*
  1219. * clean up the /proc/fs/nfsfs/ directory
  1220. */
  1221. void nfs_fs_proc_exit(void)
  1222. {
  1223. remove_proc_subtree("fs/nfsfs", NULL);
  1224. }
  1225. #endif /* CONFIG_PROC_FS */