client.c 49 KB

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