client.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  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. continue;
  258. atomic_inc(&clp->cl_count);
  259. return clp;
  260. }
  261. return NULL;
  262. }
  263. static bool nfs_client_init_is_complete(const struct nfs_client *clp)
  264. {
  265. return clp->cl_cons_state <= NFS_CS_READY;
  266. }
  267. int nfs_wait_client_init_complete(const struct nfs_client *clp)
  268. {
  269. return wait_event_killable(nfs_client_active_wq,
  270. nfs_client_init_is_complete(clp));
  271. }
  272. EXPORT_SYMBOL_GPL(nfs_wait_client_init_complete);
  273. /*
  274. * Found an existing client. Make sure it's ready before returning.
  275. */
  276. static struct nfs_client *
  277. nfs_found_client(const struct nfs_client_initdata *cl_init,
  278. struct nfs_client *clp)
  279. {
  280. int error;
  281. error = nfs_wait_client_init_complete(clp);
  282. if (error < 0) {
  283. nfs_put_client(clp);
  284. return ERR_PTR(-ERESTARTSYS);
  285. }
  286. if (clp->cl_cons_state < NFS_CS_READY) {
  287. error = clp->cl_cons_state;
  288. nfs_put_client(clp);
  289. return ERR_PTR(error);
  290. }
  291. smp_rmb();
  292. dprintk("<-- %s found nfs_client %p for %s\n",
  293. __func__, clp, cl_init->hostname ?: "");
  294. return clp;
  295. }
  296. /*
  297. * Look up a client by IP address and protocol version
  298. * - creates a new record if one doesn't yet exist
  299. */
  300. struct nfs_client *
  301. nfs_get_client(const struct nfs_client_initdata *cl_init,
  302. const struct rpc_timeout *timeparms,
  303. const char *ip_addr,
  304. rpc_authflavor_t authflavour)
  305. {
  306. struct nfs_client *clp, *new = NULL;
  307. struct nfs_net *nn = net_generic(cl_init->net, nfs_net_id);
  308. const struct nfs_rpc_ops *rpc_ops = cl_init->nfs_mod->rpc_ops;
  309. if (cl_init->hostname == NULL) {
  310. WARN_ON(1);
  311. return NULL;
  312. }
  313. dprintk("--> nfs_get_client(%s,v%u)\n",
  314. cl_init->hostname, rpc_ops->version);
  315. /* see if the client already exists */
  316. do {
  317. spin_lock(&nn->nfs_client_lock);
  318. clp = nfs_match_client(cl_init);
  319. if (clp) {
  320. spin_unlock(&nn->nfs_client_lock);
  321. if (new)
  322. new->rpc_ops->free_client(new);
  323. return nfs_found_client(cl_init, clp);
  324. }
  325. if (new) {
  326. list_add_tail(&new->cl_share_link,
  327. &nn->nfs_client_list);
  328. spin_unlock(&nn->nfs_client_lock);
  329. new->cl_flags = cl_init->init_flags;
  330. return rpc_ops->init_client(new, timeparms, ip_addr);
  331. }
  332. spin_unlock(&nn->nfs_client_lock);
  333. new = rpc_ops->alloc_client(cl_init);
  334. } while (!IS_ERR(new));
  335. dprintk("<-- nfs_get_client() Failed to find %s (%ld)\n",
  336. cl_init->hostname, PTR_ERR(new));
  337. return new;
  338. }
  339. EXPORT_SYMBOL_GPL(nfs_get_client);
  340. /*
  341. * Mark a server as ready or failed
  342. */
  343. void nfs_mark_client_ready(struct nfs_client *clp, int state)
  344. {
  345. smp_wmb();
  346. clp->cl_cons_state = state;
  347. wake_up_all(&nfs_client_active_wq);
  348. }
  349. EXPORT_SYMBOL_GPL(nfs_mark_client_ready);
  350. /*
  351. * Initialise the timeout values for a connection
  352. */
  353. void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
  354. unsigned int timeo, unsigned int retrans)
  355. {
  356. to->to_initval = timeo * HZ / 10;
  357. to->to_retries = retrans;
  358. switch (proto) {
  359. case XPRT_TRANSPORT_TCP:
  360. case XPRT_TRANSPORT_RDMA:
  361. if (to->to_retries == 0)
  362. to->to_retries = NFS_DEF_TCP_RETRANS;
  363. if (to->to_initval == 0)
  364. to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
  365. if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
  366. to->to_initval = NFS_MAX_TCP_TIMEOUT;
  367. to->to_increment = to->to_initval;
  368. to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
  369. if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
  370. to->to_maxval = NFS_MAX_TCP_TIMEOUT;
  371. if (to->to_maxval < to->to_initval)
  372. to->to_maxval = to->to_initval;
  373. to->to_exponential = 0;
  374. break;
  375. case XPRT_TRANSPORT_UDP:
  376. if (to->to_retries == 0)
  377. to->to_retries = NFS_DEF_UDP_RETRANS;
  378. if (!to->to_initval)
  379. to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
  380. if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
  381. to->to_initval = NFS_MAX_UDP_TIMEOUT;
  382. to->to_maxval = NFS_MAX_UDP_TIMEOUT;
  383. to->to_exponential = 1;
  384. break;
  385. default:
  386. BUG();
  387. }
  388. }
  389. EXPORT_SYMBOL_GPL(nfs_init_timeout_values);
  390. /*
  391. * Create an RPC client handle
  392. */
  393. int nfs_create_rpc_client(struct nfs_client *clp,
  394. const struct rpc_timeout *timeparms,
  395. rpc_authflavor_t flavor)
  396. {
  397. struct rpc_clnt *clnt = NULL;
  398. struct rpc_create_args args = {
  399. .net = clp->cl_net,
  400. .protocol = clp->cl_proto,
  401. .address = (struct sockaddr *)&clp->cl_addr,
  402. .addrsize = clp->cl_addrlen,
  403. .timeout = timeparms,
  404. .servername = clp->cl_hostname,
  405. .program = &nfs_program,
  406. .version = clp->rpc_ops->version,
  407. .authflavor = flavor,
  408. };
  409. if (test_bit(NFS_CS_DISCRTRY, &clp->cl_flags))
  410. args.flags |= RPC_CLNT_CREATE_DISCRTRY;
  411. if (test_bit(NFS_CS_NO_RETRANS_TIMEOUT, &clp->cl_flags))
  412. args.flags |= RPC_CLNT_CREATE_NO_RETRANS_TIMEOUT;
  413. if (test_bit(NFS_CS_NORESVPORT, &clp->cl_flags))
  414. args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
  415. if (test_bit(NFS_CS_INFINITE_SLOTS, &clp->cl_flags))
  416. args.flags |= RPC_CLNT_CREATE_INFINITE_SLOTS;
  417. if (!IS_ERR(clp->cl_rpcclient))
  418. return 0;
  419. clnt = rpc_create(&args);
  420. if (IS_ERR(clnt)) {
  421. dprintk("%s: cannot create RPC client. Error = %ld\n",
  422. __func__, PTR_ERR(clnt));
  423. return PTR_ERR(clnt);
  424. }
  425. clp->cl_rpcclient = clnt;
  426. return 0;
  427. }
  428. EXPORT_SYMBOL_GPL(nfs_create_rpc_client);
  429. /*
  430. * Version 2 or 3 client destruction
  431. */
  432. static void nfs_destroy_server(struct nfs_server *server)
  433. {
  434. if (server->nlm_host)
  435. nlmclnt_done(server->nlm_host);
  436. }
  437. /*
  438. * Version 2 or 3 lockd setup
  439. */
  440. static int nfs_start_lockd(struct nfs_server *server)
  441. {
  442. struct nlm_host *host;
  443. struct nfs_client *clp = server->nfs_client;
  444. struct nlmclnt_initdata nlm_init = {
  445. .hostname = clp->cl_hostname,
  446. .address = (struct sockaddr *)&clp->cl_addr,
  447. .addrlen = clp->cl_addrlen,
  448. .nfs_version = clp->rpc_ops->version,
  449. .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
  450. 1 : 0,
  451. .net = clp->cl_net,
  452. };
  453. if (nlm_init.nfs_version > 3)
  454. return 0;
  455. if ((server->flags & NFS_MOUNT_LOCAL_FLOCK) &&
  456. (server->flags & NFS_MOUNT_LOCAL_FCNTL))
  457. return 0;
  458. switch (clp->cl_proto) {
  459. default:
  460. nlm_init.protocol = IPPROTO_TCP;
  461. break;
  462. case XPRT_TRANSPORT_UDP:
  463. nlm_init.protocol = IPPROTO_UDP;
  464. }
  465. host = nlmclnt_init(&nlm_init);
  466. if (IS_ERR(host))
  467. return PTR_ERR(host);
  468. server->nlm_host = host;
  469. server->destroy = nfs_destroy_server;
  470. return 0;
  471. }
  472. /*
  473. * Create a general RPC client
  474. */
  475. int nfs_init_server_rpcclient(struct nfs_server *server,
  476. const struct rpc_timeout *timeo,
  477. rpc_authflavor_t pseudoflavour)
  478. {
  479. struct nfs_client *clp = server->nfs_client;
  480. server->client = rpc_clone_client_set_auth(clp->cl_rpcclient,
  481. pseudoflavour);
  482. if (IS_ERR(server->client)) {
  483. dprintk("%s: couldn't create rpc_client!\n", __func__);
  484. return PTR_ERR(server->client);
  485. }
  486. memcpy(&server->client->cl_timeout_default,
  487. timeo,
  488. sizeof(server->client->cl_timeout_default));
  489. server->client->cl_timeout = &server->client->cl_timeout_default;
  490. server->client->cl_softrtry = 0;
  491. if (server->flags & NFS_MOUNT_SOFT)
  492. server->client->cl_softrtry = 1;
  493. return 0;
  494. }
  495. EXPORT_SYMBOL_GPL(nfs_init_server_rpcclient);
  496. /**
  497. * nfs_init_client - Initialise an NFS2 or NFS3 client
  498. *
  499. * @clp: nfs_client to initialise
  500. * @timeparms: timeout parameters for underlying RPC transport
  501. * @ip_addr: IP presentation address (not used)
  502. *
  503. * Returns pointer to an NFS client, or an ERR_PTR value.
  504. */
  505. struct nfs_client *nfs_init_client(struct nfs_client *clp,
  506. const struct rpc_timeout *timeparms,
  507. const char *ip_addr)
  508. {
  509. int error;
  510. if (clp->cl_cons_state == NFS_CS_READY) {
  511. /* the client is already initialised */
  512. dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
  513. return clp;
  514. }
  515. /*
  516. * Create a client RPC handle for doing FSSTAT with UNIX auth only
  517. * - RFC 2623, sec 2.3.2
  518. */
  519. error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX);
  520. if (error < 0)
  521. goto error;
  522. nfs_mark_client_ready(clp, NFS_CS_READY);
  523. return clp;
  524. error:
  525. nfs_mark_client_ready(clp, error);
  526. nfs_put_client(clp);
  527. dprintk("<-- nfs_init_client() = xerror %d\n", error);
  528. return ERR_PTR(error);
  529. }
  530. EXPORT_SYMBOL_GPL(nfs_init_client);
  531. /*
  532. * Create a version 2 or 3 client
  533. */
  534. static int nfs_init_server(struct nfs_server *server,
  535. const struct nfs_parsed_mount_data *data,
  536. struct nfs_subversion *nfs_mod)
  537. {
  538. struct nfs_client_initdata cl_init = {
  539. .hostname = data->nfs_server.hostname,
  540. .addr = (const struct sockaddr *)&data->nfs_server.address,
  541. .addrlen = data->nfs_server.addrlen,
  542. .nfs_mod = nfs_mod,
  543. .proto = data->nfs_server.protocol,
  544. .net = data->net,
  545. };
  546. struct rpc_timeout timeparms;
  547. struct nfs_client *clp;
  548. int error;
  549. dprintk("--> nfs_init_server()\n");
  550. nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
  551. data->timeo, data->retrans);
  552. if (data->flags & NFS_MOUNT_NORESVPORT)
  553. set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags);
  554. /* Allocate or find a client reference we can use */
  555. clp = nfs_get_client(&cl_init, &timeparms, NULL, RPC_AUTH_UNIX);
  556. if (IS_ERR(clp)) {
  557. dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
  558. return PTR_ERR(clp);
  559. }
  560. server->nfs_client = clp;
  561. /* Initialise the client representation from the mount data */
  562. server->flags = data->flags;
  563. server->options = data->options;
  564. server->caps |= NFS_CAP_HARDLINKS|NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
  565. NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|NFS_CAP_OWNER_GROUP|
  566. NFS_CAP_ATIME|NFS_CAP_CTIME|NFS_CAP_MTIME;
  567. if (data->rsize)
  568. server->rsize = nfs_block_size(data->rsize, NULL);
  569. if (data->wsize)
  570. server->wsize = nfs_block_size(data->wsize, NULL);
  571. server->acregmin = data->acregmin * HZ;
  572. server->acregmax = data->acregmax * HZ;
  573. server->acdirmin = data->acdirmin * HZ;
  574. server->acdirmax = data->acdirmax * HZ;
  575. /* Start lockd here, before we might error out */
  576. error = nfs_start_lockd(server);
  577. if (error < 0)
  578. goto error;
  579. server->port = data->nfs_server.port;
  580. server->auth_info = data->auth_info;
  581. error = nfs_init_server_rpcclient(server, &timeparms,
  582. data->selected_flavor);
  583. if (error < 0)
  584. goto error;
  585. /* Preserve the values of mount_server-related mount options */
  586. if (data->mount_server.addrlen) {
  587. memcpy(&server->mountd_address, &data->mount_server.address,
  588. data->mount_server.addrlen);
  589. server->mountd_addrlen = data->mount_server.addrlen;
  590. }
  591. server->mountd_version = data->mount_server.version;
  592. server->mountd_port = data->mount_server.port;
  593. server->mountd_protocol = data->mount_server.protocol;
  594. server->namelen = data->namlen;
  595. dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
  596. return 0;
  597. error:
  598. server->nfs_client = NULL;
  599. nfs_put_client(clp);
  600. dprintk("<-- nfs_init_server() = xerror %d\n", error);
  601. return error;
  602. }
  603. /*
  604. * Load up the server record from information gained in an fsinfo record
  605. */
  606. static void nfs_server_set_fsinfo(struct nfs_server *server,
  607. struct nfs_fsinfo *fsinfo)
  608. {
  609. unsigned long max_rpc_payload;
  610. /* Work out a lot of parameters */
  611. if (server->rsize == 0)
  612. server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
  613. if (server->wsize == 0)
  614. server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
  615. if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
  616. server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
  617. if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
  618. server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
  619. max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
  620. if (server->rsize > max_rpc_payload)
  621. server->rsize = max_rpc_payload;
  622. if (server->rsize > NFS_MAX_FILE_IO_SIZE)
  623. server->rsize = NFS_MAX_FILE_IO_SIZE;
  624. server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  625. server->backing_dev_info.name = "nfs";
  626. server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
  627. if (server->wsize > max_rpc_payload)
  628. server->wsize = max_rpc_payload;
  629. if (server->wsize > NFS_MAX_FILE_IO_SIZE)
  630. server->wsize = NFS_MAX_FILE_IO_SIZE;
  631. server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  632. server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
  633. server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
  634. if (server->dtsize > PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES)
  635. server->dtsize = PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES;
  636. if (server->dtsize > server->rsize)
  637. server->dtsize = server->rsize;
  638. if (server->flags & NFS_MOUNT_NOAC) {
  639. server->acregmin = server->acregmax = 0;
  640. server->acdirmin = server->acdirmax = 0;
  641. }
  642. server->maxfilesize = fsinfo->maxfilesize;
  643. server->time_delta = fsinfo->time_delta;
  644. server->clone_blksize = fsinfo->clone_blksize;
  645. /* We're airborne Set socket buffersize */
  646. rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
  647. }
  648. /*
  649. * Probe filesystem information, including the FSID on v2/v3
  650. */
  651. int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
  652. {
  653. struct nfs_fsinfo fsinfo;
  654. struct nfs_client *clp = server->nfs_client;
  655. int error;
  656. dprintk("--> nfs_probe_fsinfo()\n");
  657. if (clp->rpc_ops->set_capabilities != NULL) {
  658. error = clp->rpc_ops->set_capabilities(server, mntfh);
  659. if (error < 0)
  660. goto out_error;
  661. }
  662. fsinfo.fattr = fattr;
  663. fsinfo.layouttype = 0;
  664. error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
  665. if (error < 0)
  666. goto out_error;
  667. nfs_server_set_fsinfo(server, &fsinfo);
  668. /* Get some general file system info */
  669. if (server->namelen == 0) {
  670. struct nfs_pathconf pathinfo;
  671. pathinfo.fattr = fattr;
  672. nfs_fattr_init(fattr);
  673. if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
  674. server->namelen = pathinfo.max_namelen;
  675. }
  676. dprintk("<-- nfs_probe_fsinfo() = 0\n");
  677. return 0;
  678. out_error:
  679. dprintk("nfs_probe_fsinfo: error = %d\n", -error);
  680. return error;
  681. }
  682. EXPORT_SYMBOL_GPL(nfs_probe_fsinfo);
  683. /*
  684. * Copy useful information when duplicating a server record
  685. */
  686. void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
  687. {
  688. target->flags = source->flags;
  689. target->rsize = source->rsize;
  690. target->wsize = source->wsize;
  691. target->acregmin = source->acregmin;
  692. target->acregmax = source->acregmax;
  693. target->acdirmin = source->acdirmin;
  694. target->acdirmax = source->acdirmax;
  695. target->caps = source->caps;
  696. target->options = source->options;
  697. target->auth_info = source->auth_info;
  698. }
  699. EXPORT_SYMBOL_GPL(nfs_server_copy_userdata);
  700. void nfs_server_insert_lists(struct nfs_server *server)
  701. {
  702. struct nfs_client *clp = server->nfs_client;
  703. struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
  704. spin_lock(&nn->nfs_client_lock);
  705. list_add_tail_rcu(&server->client_link, &clp->cl_superblocks);
  706. list_add_tail(&server->master_link, &nn->nfs_volume_list);
  707. clear_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
  708. spin_unlock(&nn->nfs_client_lock);
  709. }
  710. EXPORT_SYMBOL_GPL(nfs_server_insert_lists);
  711. void nfs_server_remove_lists(struct nfs_server *server)
  712. {
  713. struct nfs_client *clp = server->nfs_client;
  714. struct nfs_net *nn;
  715. if (clp == NULL)
  716. return;
  717. nn = net_generic(clp->cl_net, nfs_net_id);
  718. spin_lock(&nn->nfs_client_lock);
  719. list_del_rcu(&server->client_link);
  720. if (list_empty(&clp->cl_superblocks))
  721. set_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
  722. list_del(&server->master_link);
  723. spin_unlock(&nn->nfs_client_lock);
  724. synchronize_rcu();
  725. }
  726. EXPORT_SYMBOL_GPL(nfs_server_remove_lists);
  727. /*
  728. * Allocate and initialise a server record
  729. */
  730. struct nfs_server *nfs_alloc_server(void)
  731. {
  732. struct nfs_server *server;
  733. server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
  734. if (!server)
  735. return NULL;
  736. server->client = server->client_acl = ERR_PTR(-EINVAL);
  737. /* Zero out the NFS state stuff */
  738. INIT_LIST_HEAD(&server->client_link);
  739. INIT_LIST_HEAD(&server->master_link);
  740. INIT_LIST_HEAD(&server->delegations);
  741. INIT_LIST_HEAD(&server->layouts);
  742. INIT_LIST_HEAD(&server->state_owners_lru);
  743. atomic_set(&server->active, 0);
  744. server->io_stats = nfs_alloc_iostats();
  745. if (!server->io_stats) {
  746. kfree(server);
  747. return NULL;
  748. }
  749. if (bdi_init(&server->backing_dev_info)) {
  750. nfs_free_iostats(server->io_stats);
  751. kfree(server);
  752. return NULL;
  753. }
  754. ida_init(&server->openowner_id);
  755. ida_init(&server->lockowner_id);
  756. pnfs_init_server(server);
  757. return server;
  758. }
  759. EXPORT_SYMBOL_GPL(nfs_alloc_server);
  760. /*
  761. * Free up a server record
  762. */
  763. void nfs_free_server(struct nfs_server *server)
  764. {
  765. dprintk("--> nfs_free_server()\n");
  766. nfs_server_remove_lists(server);
  767. if (server->destroy != NULL)
  768. server->destroy(server);
  769. if (!IS_ERR(server->client_acl))
  770. rpc_shutdown_client(server->client_acl);
  771. if (!IS_ERR(server->client))
  772. rpc_shutdown_client(server->client);
  773. nfs_put_client(server->nfs_client);
  774. ida_destroy(&server->lockowner_id);
  775. ida_destroy(&server->openowner_id);
  776. nfs_free_iostats(server->io_stats);
  777. bdi_destroy(&server->backing_dev_info);
  778. kfree(server);
  779. nfs_release_automount_timer();
  780. dprintk("<-- nfs_free_server()\n");
  781. }
  782. EXPORT_SYMBOL_GPL(nfs_free_server);
  783. /*
  784. * Create a version 2 or 3 volume record
  785. * - keyed on server and FSID
  786. */
  787. struct nfs_server *nfs_create_server(struct nfs_mount_info *mount_info,
  788. struct nfs_subversion *nfs_mod)
  789. {
  790. struct nfs_server *server;
  791. struct nfs_fattr *fattr;
  792. int error;
  793. server = nfs_alloc_server();
  794. if (!server)
  795. return ERR_PTR(-ENOMEM);
  796. error = -ENOMEM;
  797. fattr = nfs_alloc_fattr();
  798. if (fattr == NULL)
  799. goto error;
  800. /* Get a client representation */
  801. error = nfs_init_server(server, mount_info->parsed, nfs_mod);
  802. if (error < 0)
  803. goto error;
  804. /* Probe the root fh to retrieve its FSID */
  805. error = nfs_probe_fsinfo(server, mount_info->mntfh, fattr);
  806. if (error < 0)
  807. goto error;
  808. if (server->nfs_client->rpc_ops->version == 3) {
  809. if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
  810. server->namelen = NFS3_MAXNAMLEN;
  811. if (!(mount_info->parsed->flags & NFS_MOUNT_NORDIRPLUS))
  812. server->caps |= NFS_CAP_READDIRPLUS;
  813. } else {
  814. if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
  815. server->namelen = NFS2_MAXNAMLEN;
  816. }
  817. if (!(fattr->valid & NFS_ATTR_FATTR)) {
  818. error = nfs_mod->rpc_ops->getattr(server, mount_info->mntfh, fattr, NULL);
  819. if (error < 0) {
  820. dprintk("nfs_create_server: getattr error = %d\n", -error);
  821. goto error;
  822. }
  823. }
  824. memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
  825. dprintk("Server FSID: %llx:%llx\n",
  826. (unsigned long long) server->fsid.major,
  827. (unsigned long long) server->fsid.minor);
  828. nfs_server_insert_lists(server);
  829. server->mount_time = jiffies;
  830. nfs_free_fattr(fattr);
  831. return server;
  832. error:
  833. nfs_free_fattr(fattr);
  834. nfs_free_server(server);
  835. return ERR_PTR(error);
  836. }
  837. EXPORT_SYMBOL_GPL(nfs_create_server);
  838. /*
  839. * Clone an NFS2, NFS3 or NFS4 server record
  840. */
  841. struct nfs_server *nfs_clone_server(struct nfs_server *source,
  842. struct nfs_fh *fh,
  843. struct nfs_fattr *fattr,
  844. rpc_authflavor_t flavor)
  845. {
  846. struct nfs_server *server;
  847. struct nfs_fattr *fattr_fsinfo;
  848. int error;
  849. dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
  850. (unsigned long long) fattr->fsid.major,
  851. (unsigned long long) fattr->fsid.minor);
  852. server = nfs_alloc_server();
  853. if (!server)
  854. return ERR_PTR(-ENOMEM);
  855. error = -ENOMEM;
  856. fattr_fsinfo = nfs_alloc_fattr();
  857. if (fattr_fsinfo == NULL)
  858. goto out_free_server;
  859. /* Copy data from the source */
  860. server->nfs_client = source->nfs_client;
  861. server->destroy = source->destroy;
  862. atomic_inc(&server->nfs_client->cl_count);
  863. nfs_server_copy_userdata(server, source);
  864. server->fsid = fattr->fsid;
  865. error = nfs_init_server_rpcclient(server,
  866. source->client->cl_timeout,
  867. flavor);
  868. if (error < 0)
  869. goto out_free_server;
  870. /* probe the filesystem info for this server filesystem */
  871. error = nfs_probe_fsinfo(server, fh, fattr_fsinfo);
  872. if (error < 0)
  873. goto out_free_server;
  874. if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
  875. server->namelen = NFS4_MAXNAMLEN;
  876. dprintk("Cloned FSID: %llx:%llx\n",
  877. (unsigned long long) server->fsid.major,
  878. (unsigned long long) server->fsid.minor);
  879. error = nfs_start_lockd(server);
  880. if (error < 0)
  881. goto out_free_server;
  882. nfs_server_insert_lists(server);
  883. server->mount_time = jiffies;
  884. nfs_free_fattr(fattr_fsinfo);
  885. dprintk("<-- nfs_clone_server() = %p\n", server);
  886. return server;
  887. out_free_server:
  888. nfs_free_fattr(fattr_fsinfo);
  889. nfs_free_server(server);
  890. dprintk("<-- nfs_clone_server() = error %d\n", error);
  891. return ERR_PTR(error);
  892. }
  893. EXPORT_SYMBOL_GPL(nfs_clone_server);
  894. void nfs_clients_init(struct net *net)
  895. {
  896. struct nfs_net *nn = net_generic(net, nfs_net_id);
  897. INIT_LIST_HEAD(&nn->nfs_client_list);
  898. INIT_LIST_HEAD(&nn->nfs_volume_list);
  899. #if IS_ENABLED(CONFIG_NFS_V4)
  900. idr_init(&nn->cb_ident_idr);
  901. #endif
  902. spin_lock_init(&nn->nfs_client_lock);
  903. nn->boot_time = CURRENT_TIME;
  904. }
  905. #ifdef CONFIG_PROC_FS
  906. static int nfs_server_list_open(struct inode *inode, struct file *file);
  907. static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
  908. static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
  909. static void nfs_server_list_stop(struct seq_file *p, void *v);
  910. static int nfs_server_list_show(struct seq_file *m, void *v);
  911. static const struct seq_operations nfs_server_list_ops = {
  912. .start = nfs_server_list_start,
  913. .next = nfs_server_list_next,
  914. .stop = nfs_server_list_stop,
  915. .show = nfs_server_list_show,
  916. };
  917. static const struct file_operations nfs_server_list_fops = {
  918. .open = nfs_server_list_open,
  919. .read = seq_read,
  920. .llseek = seq_lseek,
  921. .release = seq_release_net,
  922. .owner = THIS_MODULE,
  923. };
  924. static int nfs_volume_list_open(struct inode *inode, struct file *file);
  925. static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
  926. static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
  927. static void nfs_volume_list_stop(struct seq_file *p, void *v);
  928. static int nfs_volume_list_show(struct seq_file *m, void *v);
  929. static const struct seq_operations nfs_volume_list_ops = {
  930. .start = nfs_volume_list_start,
  931. .next = nfs_volume_list_next,
  932. .stop = nfs_volume_list_stop,
  933. .show = nfs_volume_list_show,
  934. };
  935. static const struct file_operations nfs_volume_list_fops = {
  936. .open = nfs_volume_list_open,
  937. .read = seq_read,
  938. .llseek = seq_lseek,
  939. .release = seq_release_net,
  940. .owner = THIS_MODULE,
  941. };
  942. /*
  943. * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
  944. * we're dealing
  945. */
  946. static int nfs_server_list_open(struct inode *inode, struct file *file)
  947. {
  948. return seq_open_net(inode, file, &nfs_server_list_ops,
  949. sizeof(struct seq_net_private));
  950. }
  951. /*
  952. * set up the iterator to start reading from the server list and return the first item
  953. */
  954. static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
  955. __acquires(&nn->nfs_client_lock)
  956. {
  957. struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id);
  958. /* lock the list against modification */
  959. spin_lock(&nn->nfs_client_lock);
  960. return seq_list_start_head(&nn->nfs_client_list, *_pos);
  961. }
  962. /*
  963. * move to next server
  964. */
  965. static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
  966. {
  967. struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id);
  968. return seq_list_next(v, &nn->nfs_client_list, pos);
  969. }
  970. /*
  971. * clean up after reading from the transports list
  972. */
  973. static void nfs_server_list_stop(struct seq_file *p, void *v)
  974. __releases(&nn->nfs_client_lock)
  975. {
  976. struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id);
  977. spin_unlock(&nn->nfs_client_lock);
  978. }
  979. /*
  980. * display a header line followed by a load of call lines
  981. */
  982. static int nfs_server_list_show(struct seq_file *m, void *v)
  983. {
  984. struct nfs_client *clp;
  985. struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id);
  986. /* display header on line 1 */
  987. if (v == &nn->nfs_client_list) {
  988. seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
  989. return 0;
  990. }
  991. /* display one transport per line on subsequent lines */
  992. clp = list_entry(v, struct nfs_client, cl_share_link);
  993. /* Check if the client is initialized */
  994. if (clp->cl_cons_state != NFS_CS_READY)
  995. return 0;
  996. rcu_read_lock();
  997. seq_printf(m, "v%u %s %s %3d %s\n",
  998. clp->rpc_ops->version,
  999. rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
  1000. rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
  1001. atomic_read(&clp->cl_count),
  1002. clp->cl_hostname);
  1003. rcu_read_unlock();
  1004. return 0;
  1005. }
  1006. /*
  1007. * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
  1008. */
  1009. static int nfs_volume_list_open(struct inode *inode, struct file *file)
  1010. {
  1011. return seq_open_net(inode, file, &nfs_volume_list_ops,
  1012. sizeof(struct seq_net_private));
  1013. }
  1014. /*
  1015. * set up the iterator to start reading from the volume list and return the first item
  1016. */
  1017. static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
  1018. __acquires(&nn->nfs_client_lock)
  1019. {
  1020. struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id);
  1021. /* lock the list against modification */
  1022. spin_lock(&nn->nfs_client_lock);
  1023. return seq_list_start_head(&nn->nfs_volume_list, *_pos);
  1024. }
  1025. /*
  1026. * move to next volume
  1027. */
  1028. static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
  1029. {
  1030. struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id);
  1031. return seq_list_next(v, &nn->nfs_volume_list, pos);
  1032. }
  1033. /*
  1034. * clean up after reading from the transports list
  1035. */
  1036. static void nfs_volume_list_stop(struct seq_file *p, void *v)
  1037. __releases(&nn->nfs_client_lock)
  1038. {
  1039. struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id);
  1040. spin_unlock(&nn->nfs_client_lock);
  1041. }
  1042. /*
  1043. * display a header line followed by a load of call lines
  1044. */
  1045. static int nfs_volume_list_show(struct seq_file *m, void *v)
  1046. {
  1047. struct nfs_server *server;
  1048. struct nfs_client *clp;
  1049. char dev[13]; // 8 for 2^24, 1 for ':', 3 for 2^8, 1 for '\0'
  1050. char fsid[34]; // 2 * 16 for %llx, 1 for ':', 1 for '\0'
  1051. struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id);
  1052. /* display header on line 1 */
  1053. if (v == &nn->nfs_volume_list) {
  1054. seq_puts(m, "NV SERVER PORT DEV FSID"
  1055. " FSC\n");
  1056. return 0;
  1057. }
  1058. /* display one transport per line on subsequent lines */
  1059. server = list_entry(v, struct nfs_server, master_link);
  1060. clp = server->nfs_client;
  1061. snprintf(dev, sizeof(dev), "%u:%u",
  1062. MAJOR(server->s_dev), MINOR(server->s_dev));
  1063. snprintf(fsid, sizeof(fsid), "%llx:%llx",
  1064. (unsigned long long) server->fsid.major,
  1065. (unsigned long long) server->fsid.minor);
  1066. rcu_read_lock();
  1067. seq_printf(m, "v%u %s %s %-12s %-33s %s\n",
  1068. clp->rpc_ops->version,
  1069. rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
  1070. rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
  1071. dev,
  1072. fsid,
  1073. nfs_server_fscache_state(server));
  1074. rcu_read_unlock();
  1075. return 0;
  1076. }
  1077. int nfs_fs_proc_net_init(struct net *net)
  1078. {
  1079. struct nfs_net *nn = net_generic(net, nfs_net_id);
  1080. struct proc_dir_entry *p;
  1081. nn->proc_nfsfs = proc_net_mkdir(net, "nfsfs", net->proc_net);
  1082. if (!nn->proc_nfsfs)
  1083. goto error_0;
  1084. /* a file of servers with which we're dealing */
  1085. p = proc_create("servers", S_IFREG|S_IRUGO,
  1086. nn->proc_nfsfs, &nfs_server_list_fops);
  1087. if (!p)
  1088. goto error_1;
  1089. /* a file of volumes that we have mounted */
  1090. p = proc_create("volumes", S_IFREG|S_IRUGO,
  1091. nn->proc_nfsfs, &nfs_volume_list_fops);
  1092. if (!p)
  1093. goto error_1;
  1094. return 0;
  1095. error_1:
  1096. remove_proc_subtree("nfsfs", net->proc_net);
  1097. error_0:
  1098. return -ENOMEM;
  1099. }
  1100. void nfs_fs_proc_net_exit(struct net *net)
  1101. {
  1102. remove_proc_subtree("nfsfs", net->proc_net);
  1103. }
  1104. /*
  1105. * initialise the /proc/fs/nfsfs/ directory
  1106. */
  1107. int __init nfs_fs_proc_init(void)
  1108. {
  1109. if (!proc_mkdir("fs/nfsfs", NULL))
  1110. goto error_0;
  1111. /* a file of servers with which we're dealing */
  1112. if (!proc_symlink("fs/nfsfs/servers", NULL, "../../net/nfsfs/servers"))
  1113. goto error_1;
  1114. /* a file of volumes that we have mounted */
  1115. if (!proc_symlink("fs/nfsfs/volumes", NULL, "../../net/nfsfs/volumes"))
  1116. goto error_1;
  1117. return 0;
  1118. error_1:
  1119. remove_proc_subtree("fs/nfsfs", NULL);
  1120. error_0:
  1121. return -ENOMEM;
  1122. }
  1123. /*
  1124. * clean up the /proc/fs/nfsfs/ directory
  1125. */
  1126. void nfs_fs_proc_exit(void)
  1127. {
  1128. remove_proc_subtree("fs/nfsfs", NULL);
  1129. }
  1130. #endif /* CONFIG_PROC_FS */