client.c 38 KB

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