client.c 34 KB

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