clnt.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795
  1. /*
  2. * linux/net/sunrpc/clnt.c
  3. *
  4. * This file contains the high-level RPC interface.
  5. * It is modeled as a finite state machine to support both synchronous
  6. * and asynchronous requests.
  7. *
  8. * - RPC header generation and argument serialization.
  9. * - Credential refresh.
  10. * - TCP connect handling.
  11. * - Retry of operation when it is suspected the operation failed because
  12. * of uid squashing on the server, or when the credentials were stale
  13. * and need to be refreshed, or when a packet was damaged in transit.
  14. * This may be have to be moved to the VFS layer.
  15. *
  16. * NB: BSD uses a more intelligent approach to guessing when a request
  17. * or reply has been lost by keeping the RTO estimate for each procedure.
  18. * We currently make do with a constant timeout value.
  19. *
  20. * Copyright (C) 1992,1993 Rick Sladkey <jrs@world.std.com>
  21. * Copyright (C) 1995,1996 Olaf Kirch <okir@monad.swb.de>
  22. */
  23. #include <asm/system.h>
  24. #include <linux/module.h>
  25. #include <linux/types.h>
  26. #include <linux/kallsyms.h>
  27. #include <linux/mm.h>
  28. #include <linux/namei.h>
  29. #include <linux/mount.h>
  30. #include <linux/slab.h>
  31. #include <linux/utsname.h>
  32. #include <linux/workqueue.h>
  33. #include <linux/in6.h>
  34. #include <linux/sunrpc/clnt.h>
  35. #include <linux/sunrpc/rpc_pipe_fs.h>
  36. #include <linux/sunrpc/metrics.h>
  37. #include <linux/sunrpc/bc_xprt.h>
  38. #include "sunrpc.h"
  39. #ifdef RPC_DEBUG
  40. # define RPCDBG_FACILITY RPCDBG_CALL
  41. #endif
  42. #define dprint_status(t) \
  43. dprintk("RPC: %5u %s (status %d)\n", t->tk_pid, \
  44. __func__, t->tk_status)
  45. /*
  46. * All RPC clients are linked into this list
  47. */
  48. static LIST_HEAD(all_clients);
  49. static DEFINE_SPINLOCK(rpc_client_lock);
  50. static DECLARE_WAIT_QUEUE_HEAD(destroy_wait);
  51. static void call_start(struct rpc_task *task);
  52. static void call_reserve(struct rpc_task *task);
  53. static void call_reserveresult(struct rpc_task *task);
  54. static void call_allocate(struct rpc_task *task);
  55. static void call_decode(struct rpc_task *task);
  56. static void call_bind(struct rpc_task *task);
  57. static void call_bind_status(struct rpc_task *task);
  58. static void call_transmit(struct rpc_task *task);
  59. #if defined(CONFIG_NFS_V4_1)
  60. static void call_bc_transmit(struct rpc_task *task);
  61. #endif /* CONFIG_NFS_V4_1 */
  62. static void call_status(struct rpc_task *task);
  63. static void call_transmit_status(struct rpc_task *task);
  64. static void call_refresh(struct rpc_task *task);
  65. static void call_refreshresult(struct rpc_task *task);
  66. static void call_timeout(struct rpc_task *task);
  67. static void call_connect(struct rpc_task *task);
  68. static void call_connect_status(struct rpc_task *task);
  69. static __be32 *rpc_encode_header(struct rpc_task *task);
  70. static __be32 *rpc_verify_header(struct rpc_task *task);
  71. static int rpc_ping(struct rpc_clnt *clnt);
  72. static void rpc_register_client(struct rpc_clnt *clnt)
  73. {
  74. spin_lock(&rpc_client_lock);
  75. list_add(&clnt->cl_clients, &all_clients);
  76. spin_unlock(&rpc_client_lock);
  77. }
  78. static void rpc_unregister_client(struct rpc_clnt *clnt)
  79. {
  80. spin_lock(&rpc_client_lock);
  81. list_del(&clnt->cl_clients);
  82. spin_unlock(&rpc_client_lock);
  83. }
  84. static int
  85. rpc_setup_pipedir(struct rpc_clnt *clnt, char *dir_name)
  86. {
  87. static uint32_t clntid;
  88. struct nameidata nd;
  89. struct path path;
  90. char name[15];
  91. struct qstr q = {
  92. .name = name,
  93. };
  94. int error;
  95. clnt->cl_path.mnt = ERR_PTR(-ENOENT);
  96. clnt->cl_path.dentry = ERR_PTR(-ENOENT);
  97. if (dir_name == NULL)
  98. return 0;
  99. path.mnt = rpc_get_mount();
  100. if (IS_ERR(path.mnt))
  101. return PTR_ERR(path.mnt);
  102. error = vfs_path_lookup(path.mnt->mnt_root, path.mnt, dir_name, 0, &nd);
  103. if (error)
  104. goto err;
  105. for (;;) {
  106. q.len = snprintf(name, sizeof(name), "clnt%x", (unsigned int)clntid++);
  107. name[sizeof(name) - 1] = '\0';
  108. q.hash = full_name_hash(q.name, q.len);
  109. path.dentry = rpc_create_client_dir(nd.path.dentry, &q, clnt);
  110. if (!IS_ERR(path.dentry))
  111. break;
  112. error = PTR_ERR(path.dentry);
  113. if (error != -EEXIST) {
  114. printk(KERN_INFO "RPC: Couldn't create pipefs entry"
  115. " %s/%s, error %d\n",
  116. dir_name, name, error);
  117. goto err_path_put;
  118. }
  119. }
  120. path_put(&nd.path);
  121. clnt->cl_path = path;
  122. return 0;
  123. err_path_put:
  124. path_put(&nd.path);
  125. err:
  126. rpc_put_mount();
  127. return error;
  128. }
  129. static struct rpc_clnt * rpc_new_client(const struct rpc_create_args *args, struct rpc_xprt *xprt)
  130. {
  131. struct rpc_program *program = args->program;
  132. struct rpc_version *version;
  133. struct rpc_clnt *clnt = NULL;
  134. struct rpc_auth *auth;
  135. int err;
  136. size_t len;
  137. /* sanity check the name before trying to print it */
  138. err = -EINVAL;
  139. len = strlen(args->servername);
  140. if (len > RPC_MAXNETNAMELEN)
  141. goto out_no_rpciod;
  142. len++;
  143. dprintk("RPC: creating %s client for %s (xprt %p)\n",
  144. program->name, args->servername, xprt);
  145. err = rpciod_up();
  146. if (err)
  147. goto out_no_rpciod;
  148. err = -EINVAL;
  149. if (!xprt)
  150. goto out_no_xprt;
  151. if (args->version >= program->nrvers)
  152. goto out_err;
  153. version = program->version[args->version];
  154. if (version == NULL)
  155. goto out_err;
  156. err = -ENOMEM;
  157. clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
  158. if (!clnt)
  159. goto out_err;
  160. clnt->cl_parent = clnt;
  161. clnt->cl_server = clnt->cl_inline_name;
  162. if (len > sizeof(clnt->cl_inline_name)) {
  163. char *buf = kmalloc(len, GFP_KERNEL);
  164. if (buf != NULL)
  165. clnt->cl_server = buf;
  166. else
  167. len = sizeof(clnt->cl_inline_name);
  168. }
  169. strlcpy(clnt->cl_server, args->servername, len);
  170. clnt->cl_xprt = xprt;
  171. clnt->cl_procinfo = version->procs;
  172. clnt->cl_maxproc = version->nrprocs;
  173. clnt->cl_protname = program->name;
  174. clnt->cl_prog = args->prognumber ? : program->number;
  175. clnt->cl_vers = version->number;
  176. clnt->cl_stats = program->stats;
  177. clnt->cl_metrics = rpc_alloc_iostats(clnt);
  178. err = -ENOMEM;
  179. if (clnt->cl_metrics == NULL)
  180. goto out_no_stats;
  181. clnt->cl_program = program;
  182. INIT_LIST_HEAD(&clnt->cl_tasks);
  183. spin_lock_init(&clnt->cl_lock);
  184. if (!xprt_bound(clnt->cl_xprt))
  185. clnt->cl_autobind = 1;
  186. clnt->cl_timeout = xprt->timeout;
  187. if (args->timeout != NULL) {
  188. memcpy(&clnt->cl_timeout_default, args->timeout,
  189. sizeof(clnt->cl_timeout_default));
  190. clnt->cl_timeout = &clnt->cl_timeout_default;
  191. }
  192. clnt->cl_rtt = &clnt->cl_rtt_default;
  193. rpc_init_rtt(&clnt->cl_rtt_default, clnt->cl_timeout->to_initval);
  194. clnt->cl_principal = NULL;
  195. if (args->client_name) {
  196. clnt->cl_principal = kstrdup(args->client_name, GFP_KERNEL);
  197. if (!clnt->cl_principal)
  198. goto out_no_principal;
  199. }
  200. kref_init(&clnt->cl_kref);
  201. err = rpc_setup_pipedir(clnt, program->pipe_dir_name);
  202. if (err < 0)
  203. goto out_no_path;
  204. auth = rpcauth_create(args->authflavor, clnt);
  205. if (IS_ERR(auth)) {
  206. printk(KERN_INFO "RPC: Couldn't create auth handle (flavor %u)\n",
  207. args->authflavor);
  208. err = PTR_ERR(auth);
  209. goto out_no_auth;
  210. }
  211. /* save the nodename */
  212. clnt->cl_nodelen = strlen(init_utsname()->nodename);
  213. if (clnt->cl_nodelen > UNX_MAXNODENAME)
  214. clnt->cl_nodelen = UNX_MAXNODENAME;
  215. memcpy(clnt->cl_nodename, init_utsname()->nodename, clnt->cl_nodelen);
  216. rpc_register_client(clnt);
  217. return clnt;
  218. out_no_auth:
  219. if (!IS_ERR(clnt->cl_path.dentry)) {
  220. rpc_remove_client_dir(clnt->cl_path.dentry);
  221. rpc_put_mount();
  222. }
  223. out_no_path:
  224. kfree(clnt->cl_principal);
  225. out_no_principal:
  226. rpc_free_iostats(clnt->cl_metrics);
  227. out_no_stats:
  228. if (clnt->cl_server != clnt->cl_inline_name)
  229. kfree(clnt->cl_server);
  230. kfree(clnt);
  231. out_err:
  232. xprt_put(xprt);
  233. out_no_xprt:
  234. rpciod_down();
  235. out_no_rpciod:
  236. return ERR_PTR(err);
  237. }
  238. /*
  239. * rpc_create - create an RPC client and transport with one call
  240. * @args: rpc_clnt create argument structure
  241. *
  242. * Creates and initializes an RPC transport and an RPC client.
  243. *
  244. * It can ping the server in order to determine if it is up, and to see if
  245. * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
  246. * this behavior so asynchronous tasks can also use rpc_create.
  247. */
  248. struct rpc_clnt *rpc_create(struct rpc_create_args *args)
  249. {
  250. struct rpc_xprt *xprt;
  251. struct rpc_clnt *clnt;
  252. struct xprt_create xprtargs = {
  253. .ident = args->protocol,
  254. .srcaddr = args->saddress,
  255. .dstaddr = args->address,
  256. .addrlen = args->addrsize,
  257. .bc_xprt = args->bc_xprt,
  258. };
  259. char servername[48];
  260. /*
  261. * If the caller chooses not to specify a hostname, whip
  262. * up a string representation of the passed-in address.
  263. */
  264. if (args->servername == NULL) {
  265. servername[0] = '\0';
  266. switch (args->address->sa_family) {
  267. case AF_INET: {
  268. struct sockaddr_in *sin =
  269. (struct sockaddr_in *)args->address;
  270. snprintf(servername, sizeof(servername), "%pI4",
  271. &sin->sin_addr.s_addr);
  272. break;
  273. }
  274. case AF_INET6: {
  275. struct sockaddr_in6 *sin =
  276. (struct sockaddr_in6 *)args->address;
  277. snprintf(servername, sizeof(servername), "%pI6",
  278. &sin->sin6_addr);
  279. break;
  280. }
  281. default:
  282. /* caller wants default server name, but
  283. * address family isn't recognized. */
  284. return ERR_PTR(-EINVAL);
  285. }
  286. args->servername = servername;
  287. }
  288. xprt = xprt_create_transport(&xprtargs);
  289. if (IS_ERR(xprt))
  290. return (struct rpc_clnt *)xprt;
  291. /*
  292. * By default, kernel RPC client connects from a reserved port.
  293. * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
  294. * but it is always enabled for rpciod, which handles the connect
  295. * operation.
  296. */
  297. xprt->resvport = 1;
  298. if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
  299. xprt->resvport = 0;
  300. clnt = rpc_new_client(args, xprt);
  301. if (IS_ERR(clnt))
  302. return clnt;
  303. if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
  304. int err = rpc_ping(clnt);
  305. if (err != 0) {
  306. rpc_shutdown_client(clnt);
  307. return ERR_PTR(err);
  308. }
  309. }
  310. clnt->cl_softrtry = 1;
  311. if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
  312. clnt->cl_softrtry = 0;
  313. if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
  314. clnt->cl_autobind = 1;
  315. if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
  316. clnt->cl_discrtry = 1;
  317. if (!(args->flags & RPC_CLNT_CREATE_QUIET))
  318. clnt->cl_chatty = 1;
  319. return clnt;
  320. }
  321. EXPORT_SYMBOL_GPL(rpc_create);
  322. /*
  323. * This function clones the RPC client structure. It allows us to share the
  324. * same transport while varying parameters such as the authentication
  325. * flavour.
  326. */
  327. struct rpc_clnt *
  328. rpc_clone_client(struct rpc_clnt *clnt)
  329. {
  330. struct rpc_clnt *new;
  331. int err = -ENOMEM;
  332. new = kmemdup(clnt, sizeof(*new), GFP_KERNEL);
  333. if (!new)
  334. goto out_no_clnt;
  335. new->cl_parent = clnt;
  336. /* Turn off autobind on clones */
  337. new->cl_autobind = 0;
  338. INIT_LIST_HEAD(&new->cl_tasks);
  339. spin_lock_init(&new->cl_lock);
  340. rpc_init_rtt(&new->cl_rtt_default, clnt->cl_timeout->to_initval);
  341. new->cl_metrics = rpc_alloc_iostats(clnt);
  342. if (new->cl_metrics == NULL)
  343. goto out_no_stats;
  344. if (clnt->cl_principal) {
  345. new->cl_principal = kstrdup(clnt->cl_principal, GFP_KERNEL);
  346. if (new->cl_principal == NULL)
  347. goto out_no_principal;
  348. }
  349. kref_init(&new->cl_kref);
  350. err = rpc_setup_pipedir(new, clnt->cl_program->pipe_dir_name);
  351. if (err != 0)
  352. goto out_no_path;
  353. if (new->cl_auth)
  354. atomic_inc(&new->cl_auth->au_count);
  355. xprt_get(clnt->cl_xprt);
  356. kref_get(&clnt->cl_kref);
  357. rpc_register_client(new);
  358. rpciod_up();
  359. return new;
  360. out_no_path:
  361. kfree(new->cl_principal);
  362. out_no_principal:
  363. rpc_free_iostats(new->cl_metrics);
  364. out_no_stats:
  365. kfree(new);
  366. out_no_clnt:
  367. dprintk("RPC: %s: returned error %d\n", __func__, err);
  368. return ERR_PTR(err);
  369. }
  370. EXPORT_SYMBOL_GPL(rpc_clone_client);
  371. /*
  372. * Properly shut down an RPC client, terminating all outstanding
  373. * requests.
  374. */
  375. void rpc_shutdown_client(struct rpc_clnt *clnt)
  376. {
  377. dprintk("RPC: shutting down %s client for %s\n",
  378. clnt->cl_protname, clnt->cl_server);
  379. while (!list_empty(&clnt->cl_tasks)) {
  380. rpc_killall_tasks(clnt);
  381. wait_event_timeout(destroy_wait,
  382. list_empty(&clnt->cl_tasks), 1*HZ);
  383. }
  384. rpc_release_client(clnt);
  385. }
  386. EXPORT_SYMBOL_GPL(rpc_shutdown_client);
  387. /*
  388. * Free an RPC client
  389. */
  390. static void
  391. rpc_free_client(struct kref *kref)
  392. {
  393. struct rpc_clnt *clnt = container_of(kref, struct rpc_clnt, cl_kref);
  394. dprintk("RPC: destroying %s client for %s\n",
  395. clnt->cl_protname, clnt->cl_server);
  396. if (!IS_ERR(clnt->cl_path.dentry)) {
  397. rpc_remove_client_dir(clnt->cl_path.dentry);
  398. rpc_put_mount();
  399. }
  400. if (clnt->cl_parent != clnt) {
  401. rpc_release_client(clnt->cl_parent);
  402. goto out_free;
  403. }
  404. if (clnt->cl_server != clnt->cl_inline_name)
  405. kfree(clnt->cl_server);
  406. out_free:
  407. rpc_unregister_client(clnt);
  408. rpc_free_iostats(clnt->cl_metrics);
  409. kfree(clnt->cl_principal);
  410. clnt->cl_metrics = NULL;
  411. xprt_put(clnt->cl_xprt);
  412. rpciod_down();
  413. kfree(clnt);
  414. }
  415. /*
  416. * Free an RPC client
  417. */
  418. static void
  419. rpc_free_auth(struct kref *kref)
  420. {
  421. struct rpc_clnt *clnt = container_of(kref, struct rpc_clnt, cl_kref);
  422. if (clnt->cl_auth == NULL) {
  423. rpc_free_client(kref);
  424. return;
  425. }
  426. /*
  427. * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
  428. * release remaining GSS contexts. This mechanism ensures
  429. * that it can do so safely.
  430. */
  431. kref_init(kref);
  432. rpcauth_release(clnt->cl_auth);
  433. clnt->cl_auth = NULL;
  434. kref_put(kref, rpc_free_client);
  435. }
  436. /*
  437. * Release reference to the RPC client
  438. */
  439. void
  440. rpc_release_client(struct rpc_clnt *clnt)
  441. {
  442. dprintk("RPC: rpc_release_client(%p)\n", clnt);
  443. if (list_empty(&clnt->cl_tasks))
  444. wake_up(&destroy_wait);
  445. kref_put(&clnt->cl_kref, rpc_free_auth);
  446. }
  447. /**
  448. * rpc_bind_new_program - bind a new RPC program to an existing client
  449. * @old: old rpc_client
  450. * @program: rpc program to set
  451. * @vers: rpc program version
  452. *
  453. * Clones the rpc client and sets up a new RPC program. This is mainly
  454. * of use for enabling different RPC programs to share the same transport.
  455. * The Sun NFSv2/v3 ACL protocol can do this.
  456. */
  457. struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
  458. struct rpc_program *program,
  459. u32 vers)
  460. {
  461. struct rpc_clnt *clnt;
  462. struct rpc_version *version;
  463. int err;
  464. BUG_ON(vers >= program->nrvers || !program->version[vers]);
  465. version = program->version[vers];
  466. clnt = rpc_clone_client(old);
  467. if (IS_ERR(clnt))
  468. goto out;
  469. clnt->cl_procinfo = version->procs;
  470. clnt->cl_maxproc = version->nrprocs;
  471. clnt->cl_protname = program->name;
  472. clnt->cl_prog = program->number;
  473. clnt->cl_vers = version->number;
  474. clnt->cl_stats = program->stats;
  475. err = rpc_ping(clnt);
  476. if (err != 0) {
  477. rpc_shutdown_client(clnt);
  478. clnt = ERR_PTR(err);
  479. }
  480. out:
  481. return clnt;
  482. }
  483. EXPORT_SYMBOL_GPL(rpc_bind_new_program);
  484. /*
  485. * Default callback for async RPC calls
  486. */
  487. static void
  488. rpc_default_callback(struct rpc_task *task, void *data)
  489. {
  490. }
  491. static const struct rpc_call_ops rpc_default_ops = {
  492. .rpc_call_done = rpc_default_callback,
  493. };
  494. /**
  495. * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
  496. * @task_setup_data: pointer to task initialisation data
  497. */
  498. struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
  499. {
  500. struct rpc_task *task, *ret;
  501. task = rpc_new_task(task_setup_data);
  502. if (task == NULL) {
  503. rpc_release_calldata(task_setup_data->callback_ops,
  504. task_setup_data->callback_data);
  505. ret = ERR_PTR(-ENOMEM);
  506. goto out;
  507. }
  508. if (task->tk_status != 0) {
  509. ret = ERR_PTR(task->tk_status);
  510. rpc_put_task(task);
  511. goto out;
  512. }
  513. atomic_inc(&task->tk_count);
  514. rpc_execute(task);
  515. ret = task;
  516. out:
  517. return ret;
  518. }
  519. EXPORT_SYMBOL_GPL(rpc_run_task);
  520. /**
  521. * rpc_call_sync - Perform a synchronous RPC call
  522. * @clnt: pointer to RPC client
  523. * @msg: RPC call parameters
  524. * @flags: RPC call flags
  525. */
  526. int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags)
  527. {
  528. struct rpc_task *task;
  529. struct rpc_task_setup task_setup_data = {
  530. .rpc_client = clnt,
  531. .rpc_message = msg,
  532. .callback_ops = &rpc_default_ops,
  533. .flags = flags,
  534. };
  535. int status;
  536. BUG_ON(flags & RPC_TASK_ASYNC);
  537. task = rpc_run_task(&task_setup_data);
  538. if (IS_ERR(task))
  539. return PTR_ERR(task);
  540. status = task->tk_status;
  541. rpc_put_task(task);
  542. return status;
  543. }
  544. EXPORT_SYMBOL_GPL(rpc_call_sync);
  545. /**
  546. * rpc_call_async - Perform an asynchronous RPC call
  547. * @clnt: pointer to RPC client
  548. * @msg: RPC call parameters
  549. * @flags: RPC call flags
  550. * @tk_ops: RPC call ops
  551. * @data: user call data
  552. */
  553. int
  554. rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags,
  555. const struct rpc_call_ops *tk_ops, void *data)
  556. {
  557. struct rpc_task *task;
  558. struct rpc_task_setup task_setup_data = {
  559. .rpc_client = clnt,
  560. .rpc_message = msg,
  561. .callback_ops = tk_ops,
  562. .callback_data = data,
  563. .flags = flags|RPC_TASK_ASYNC,
  564. };
  565. task = rpc_run_task(&task_setup_data);
  566. if (IS_ERR(task))
  567. return PTR_ERR(task);
  568. rpc_put_task(task);
  569. return 0;
  570. }
  571. EXPORT_SYMBOL_GPL(rpc_call_async);
  572. #if defined(CONFIG_NFS_V4_1)
  573. /**
  574. * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
  575. * rpc_execute against it
  576. * @req: RPC request
  577. * @tk_ops: RPC call ops
  578. */
  579. struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req,
  580. const struct rpc_call_ops *tk_ops)
  581. {
  582. struct rpc_task *task;
  583. struct xdr_buf *xbufp = &req->rq_snd_buf;
  584. struct rpc_task_setup task_setup_data = {
  585. .callback_ops = tk_ops,
  586. };
  587. dprintk("RPC: rpc_run_bc_task req= %p\n", req);
  588. /*
  589. * Create an rpc_task to send the data
  590. */
  591. task = rpc_new_task(&task_setup_data);
  592. if (!task) {
  593. xprt_free_bc_request(req);
  594. task = ERR_PTR(-ENOMEM);
  595. goto out;
  596. }
  597. task->tk_rqstp = req;
  598. /*
  599. * Set up the xdr_buf length.
  600. * This also indicates that the buffer is XDR encoded already.
  601. */
  602. xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
  603. xbufp->tail[0].iov_len;
  604. task->tk_action = call_bc_transmit;
  605. atomic_inc(&task->tk_count);
  606. BUG_ON(atomic_read(&task->tk_count) != 2);
  607. rpc_execute(task);
  608. out:
  609. dprintk("RPC: rpc_run_bc_task: task= %p\n", task);
  610. return task;
  611. }
  612. #endif /* CONFIG_NFS_V4_1 */
  613. void
  614. rpc_call_start(struct rpc_task *task)
  615. {
  616. task->tk_action = call_start;
  617. }
  618. EXPORT_SYMBOL_GPL(rpc_call_start);
  619. /**
  620. * rpc_peeraddr - extract remote peer address from clnt's xprt
  621. * @clnt: RPC client structure
  622. * @buf: target buffer
  623. * @bufsize: length of target buffer
  624. *
  625. * Returns the number of bytes that are actually in the stored address.
  626. */
  627. size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
  628. {
  629. size_t bytes;
  630. struct rpc_xprt *xprt = clnt->cl_xprt;
  631. bytes = sizeof(xprt->addr);
  632. if (bytes > bufsize)
  633. bytes = bufsize;
  634. memcpy(buf, &clnt->cl_xprt->addr, bytes);
  635. return xprt->addrlen;
  636. }
  637. EXPORT_SYMBOL_GPL(rpc_peeraddr);
  638. /**
  639. * rpc_peeraddr2str - return remote peer address in printable format
  640. * @clnt: RPC client structure
  641. * @format: address format
  642. *
  643. */
  644. const char *rpc_peeraddr2str(struct rpc_clnt *clnt,
  645. enum rpc_display_format_t format)
  646. {
  647. struct rpc_xprt *xprt = clnt->cl_xprt;
  648. if (xprt->address_strings[format] != NULL)
  649. return xprt->address_strings[format];
  650. else
  651. return "unprintable";
  652. }
  653. EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
  654. void
  655. rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
  656. {
  657. struct rpc_xprt *xprt = clnt->cl_xprt;
  658. if (xprt->ops->set_buffer_size)
  659. xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
  660. }
  661. EXPORT_SYMBOL_GPL(rpc_setbufsize);
  662. /*
  663. * Return size of largest payload RPC client can support, in bytes
  664. *
  665. * For stream transports, this is one RPC record fragment (see RFC
  666. * 1831), as we don't support multi-record requests yet. For datagram
  667. * transports, this is the size of an IP packet minus the IP, UDP, and
  668. * RPC header sizes.
  669. */
  670. size_t rpc_max_payload(struct rpc_clnt *clnt)
  671. {
  672. return clnt->cl_xprt->max_payload;
  673. }
  674. EXPORT_SYMBOL_GPL(rpc_max_payload);
  675. /**
  676. * rpc_force_rebind - force transport to check that remote port is unchanged
  677. * @clnt: client to rebind
  678. *
  679. */
  680. void rpc_force_rebind(struct rpc_clnt *clnt)
  681. {
  682. if (clnt->cl_autobind)
  683. xprt_clear_bound(clnt->cl_xprt);
  684. }
  685. EXPORT_SYMBOL_GPL(rpc_force_rebind);
  686. /*
  687. * Restart an (async) RPC call from the call_prepare state.
  688. * Usually called from within the exit handler.
  689. */
  690. void
  691. rpc_restart_call_prepare(struct rpc_task *task)
  692. {
  693. if (RPC_ASSASSINATED(task))
  694. return;
  695. task->tk_action = rpc_prepare_task;
  696. }
  697. EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
  698. /*
  699. * Restart an (async) RPC call. Usually called from within the
  700. * exit handler.
  701. */
  702. void
  703. rpc_restart_call(struct rpc_task *task)
  704. {
  705. if (RPC_ASSASSINATED(task))
  706. return;
  707. task->tk_action = call_start;
  708. }
  709. EXPORT_SYMBOL_GPL(rpc_restart_call);
  710. #ifdef RPC_DEBUG
  711. static const char *rpc_proc_name(const struct rpc_task *task)
  712. {
  713. const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
  714. if (proc) {
  715. if (proc->p_name)
  716. return proc->p_name;
  717. else
  718. return "NULL";
  719. } else
  720. return "no proc";
  721. }
  722. #endif
  723. /*
  724. * 0. Initial state
  725. *
  726. * Other FSM states can be visited zero or more times, but
  727. * this state is visited exactly once for each RPC.
  728. */
  729. static void
  730. call_start(struct rpc_task *task)
  731. {
  732. struct rpc_clnt *clnt = task->tk_client;
  733. dprintk("RPC: %5u call_start %s%d proc %s (%s)\n", task->tk_pid,
  734. clnt->cl_protname, clnt->cl_vers,
  735. rpc_proc_name(task),
  736. (RPC_IS_ASYNC(task) ? "async" : "sync"));
  737. /* Increment call count */
  738. task->tk_msg.rpc_proc->p_count++;
  739. clnt->cl_stats->rpccnt++;
  740. task->tk_action = call_reserve;
  741. }
  742. /*
  743. * 1. Reserve an RPC call slot
  744. */
  745. static void
  746. call_reserve(struct rpc_task *task)
  747. {
  748. dprint_status(task);
  749. if (!rpcauth_uptodatecred(task)) {
  750. task->tk_action = call_refresh;
  751. return;
  752. }
  753. task->tk_status = 0;
  754. task->tk_action = call_reserveresult;
  755. xprt_reserve(task);
  756. }
  757. /*
  758. * 1b. Grok the result of xprt_reserve()
  759. */
  760. static void
  761. call_reserveresult(struct rpc_task *task)
  762. {
  763. int status = task->tk_status;
  764. dprint_status(task);
  765. /*
  766. * After a call to xprt_reserve(), we must have either
  767. * a request slot or else an error status.
  768. */
  769. task->tk_status = 0;
  770. if (status >= 0) {
  771. if (task->tk_rqstp) {
  772. task->tk_action = call_allocate;
  773. return;
  774. }
  775. printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
  776. __func__, status);
  777. rpc_exit(task, -EIO);
  778. return;
  779. }
  780. /*
  781. * Even though there was an error, we may have acquired
  782. * a request slot somehow. Make sure not to leak it.
  783. */
  784. if (task->tk_rqstp) {
  785. printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
  786. __func__, status);
  787. xprt_release(task);
  788. }
  789. switch (status) {
  790. case -EAGAIN: /* woken up; retry */
  791. task->tk_action = call_reserve;
  792. return;
  793. case -EIO: /* probably a shutdown */
  794. break;
  795. default:
  796. printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
  797. __func__, status);
  798. break;
  799. }
  800. rpc_exit(task, status);
  801. }
  802. /*
  803. * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
  804. * (Note: buffer memory is freed in xprt_release).
  805. */
  806. static void
  807. call_allocate(struct rpc_task *task)
  808. {
  809. unsigned int slack = task->tk_msg.rpc_cred->cr_auth->au_cslack;
  810. struct rpc_rqst *req = task->tk_rqstp;
  811. struct rpc_xprt *xprt = task->tk_xprt;
  812. struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
  813. dprint_status(task);
  814. task->tk_status = 0;
  815. task->tk_action = call_bind;
  816. if (req->rq_buffer)
  817. return;
  818. if (proc->p_proc != 0) {
  819. BUG_ON(proc->p_arglen == 0);
  820. if (proc->p_decode != NULL)
  821. BUG_ON(proc->p_replen == 0);
  822. }
  823. /*
  824. * Calculate the size (in quads) of the RPC call
  825. * and reply headers, and convert both values
  826. * to byte sizes.
  827. */
  828. req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen;
  829. req->rq_callsize <<= 2;
  830. req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen;
  831. req->rq_rcvsize <<= 2;
  832. req->rq_buffer = xprt->ops->buf_alloc(task,
  833. req->rq_callsize + req->rq_rcvsize);
  834. if (req->rq_buffer != NULL)
  835. return;
  836. dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
  837. if (RPC_IS_ASYNC(task) || !signalled()) {
  838. task->tk_action = call_allocate;
  839. rpc_delay(task, HZ>>4);
  840. return;
  841. }
  842. rpc_exit(task, -ERESTARTSYS);
  843. }
  844. static inline int
  845. rpc_task_need_encode(struct rpc_task *task)
  846. {
  847. return task->tk_rqstp->rq_snd_buf.len == 0;
  848. }
  849. static inline void
  850. rpc_task_force_reencode(struct rpc_task *task)
  851. {
  852. task->tk_rqstp->rq_snd_buf.len = 0;
  853. task->tk_rqstp->rq_bytes_sent = 0;
  854. }
  855. static inline void
  856. rpc_xdr_buf_init(struct xdr_buf *buf, void *start, size_t len)
  857. {
  858. buf->head[0].iov_base = start;
  859. buf->head[0].iov_len = len;
  860. buf->tail[0].iov_len = 0;
  861. buf->page_len = 0;
  862. buf->flags = 0;
  863. buf->len = 0;
  864. buf->buflen = len;
  865. }
  866. /*
  867. * 3. Encode arguments of an RPC call
  868. */
  869. static void
  870. rpc_xdr_encode(struct rpc_task *task)
  871. {
  872. struct rpc_rqst *req = task->tk_rqstp;
  873. kxdrproc_t encode;
  874. __be32 *p;
  875. dprint_status(task);
  876. rpc_xdr_buf_init(&req->rq_snd_buf,
  877. req->rq_buffer,
  878. req->rq_callsize);
  879. rpc_xdr_buf_init(&req->rq_rcv_buf,
  880. (char *)req->rq_buffer + req->rq_callsize,
  881. req->rq_rcvsize);
  882. p = rpc_encode_header(task);
  883. if (p == NULL) {
  884. printk(KERN_INFO "RPC: couldn't encode RPC header, exit EIO\n");
  885. rpc_exit(task, -EIO);
  886. return;
  887. }
  888. encode = task->tk_msg.rpc_proc->p_encode;
  889. if (encode == NULL)
  890. return;
  891. task->tk_status = rpcauth_wrap_req(task, encode, req, p,
  892. task->tk_msg.rpc_argp);
  893. }
  894. /*
  895. * 4. Get the server port number if not yet set
  896. */
  897. static void
  898. call_bind(struct rpc_task *task)
  899. {
  900. struct rpc_xprt *xprt = task->tk_xprt;
  901. dprint_status(task);
  902. task->tk_action = call_connect;
  903. if (!xprt_bound(xprt)) {
  904. task->tk_action = call_bind_status;
  905. task->tk_timeout = xprt->bind_timeout;
  906. xprt->ops->rpcbind(task);
  907. }
  908. }
  909. /*
  910. * 4a. Sort out bind result
  911. */
  912. static void
  913. call_bind_status(struct rpc_task *task)
  914. {
  915. int status = -EIO;
  916. if (task->tk_status >= 0) {
  917. dprint_status(task);
  918. task->tk_status = 0;
  919. task->tk_action = call_connect;
  920. return;
  921. }
  922. switch (task->tk_status) {
  923. case -ENOMEM:
  924. dprintk("RPC: %5u rpcbind out of memory\n", task->tk_pid);
  925. rpc_delay(task, HZ >> 2);
  926. goto retry_timeout;
  927. case -EACCES:
  928. dprintk("RPC: %5u remote rpcbind: RPC program/version "
  929. "unavailable\n", task->tk_pid);
  930. /* fail immediately if this is an RPC ping */
  931. if (task->tk_msg.rpc_proc->p_proc == 0) {
  932. status = -EOPNOTSUPP;
  933. break;
  934. }
  935. rpc_delay(task, 3*HZ);
  936. goto retry_timeout;
  937. case -ETIMEDOUT:
  938. dprintk("RPC: %5u rpcbind request timed out\n",
  939. task->tk_pid);
  940. goto retry_timeout;
  941. case -EPFNOSUPPORT:
  942. /* server doesn't support any rpcbind version we know of */
  943. dprintk("RPC: %5u unrecognized remote rpcbind service\n",
  944. task->tk_pid);
  945. break;
  946. case -EPROTONOSUPPORT:
  947. dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
  948. task->tk_pid);
  949. task->tk_status = 0;
  950. task->tk_action = call_bind;
  951. return;
  952. case -ECONNREFUSED: /* connection problems */
  953. case -ECONNRESET:
  954. case -ENOTCONN:
  955. case -EHOSTDOWN:
  956. case -EHOSTUNREACH:
  957. case -ENETUNREACH:
  958. case -EPIPE:
  959. dprintk("RPC: %5u remote rpcbind unreachable: %d\n",
  960. task->tk_pid, task->tk_status);
  961. if (!RPC_IS_SOFTCONN(task)) {
  962. rpc_delay(task, 5*HZ);
  963. goto retry_timeout;
  964. }
  965. status = task->tk_status;
  966. break;
  967. default:
  968. dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
  969. task->tk_pid, -task->tk_status);
  970. }
  971. rpc_exit(task, status);
  972. return;
  973. retry_timeout:
  974. task->tk_action = call_timeout;
  975. }
  976. /*
  977. * 4b. Connect to the RPC server
  978. */
  979. static void
  980. call_connect(struct rpc_task *task)
  981. {
  982. struct rpc_xprt *xprt = task->tk_xprt;
  983. dprintk("RPC: %5u call_connect xprt %p %s connected\n",
  984. task->tk_pid, xprt,
  985. (xprt_connected(xprt) ? "is" : "is not"));
  986. task->tk_action = call_transmit;
  987. if (!xprt_connected(xprt)) {
  988. task->tk_action = call_connect_status;
  989. if (task->tk_status < 0)
  990. return;
  991. xprt_connect(task);
  992. }
  993. }
  994. /*
  995. * 4c. Sort out connect result
  996. */
  997. static void
  998. call_connect_status(struct rpc_task *task)
  999. {
  1000. struct rpc_clnt *clnt = task->tk_client;
  1001. int status = task->tk_status;
  1002. dprint_status(task);
  1003. task->tk_status = 0;
  1004. if (status >= 0 || status == -EAGAIN) {
  1005. clnt->cl_stats->netreconn++;
  1006. task->tk_action = call_transmit;
  1007. return;
  1008. }
  1009. switch (status) {
  1010. /* if soft mounted, test if we've timed out */
  1011. case -ETIMEDOUT:
  1012. task->tk_action = call_timeout;
  1013. break;
  1014. default:
  1015. rpc_exit(task, -EIO);
  1016. }
  1017. }
  1018. /*
  1019. * 5. Transmit the RPC request, and wait for reply
  1020. */
  1021. static void
  1022. call_transmit(struct rpc_task *task)
  1023. {
  1024. dprint_status(task);
  1025. task->tk_action = call_status;
  1026. if (task->tk_status < 0)
  1027. return;
  1028. task->tk_status = xprt_prepare_transmit(task);
  1029. if (task->tk_status != 0)
  1030. return;
  1031. task->tk_action = call_transmit_status;
  1032. /* Encode here so that rpcsec_gss can use correct sequence number. */
  1033. if (rpc_task_need_encode(task)) {
  1034. BUG_ON(task->tk_rqstp->rq_bytes_sent != 0);
  1035. rpc_xdr_encode(task);
  1036. /* Did the encode result in an error condition? */
  1037. if (task->tk_status != 0) {
  1038. /* Was the error nonfatal? */
  1039. if (task->tk_status == -EAGAIN)
  1040. rpc_delay(task, HZ >> 4);
  1041. else
  1042. rpc_exit(task, task->tk_status);
  1043. return;
  1044. }
  1045. }
  1046. xprt_transmit(task);
  1047. if (task->tk_status < 0)
  1048. return;
  1049. /*
  1050. * On success, ensure that we call xprt_end_transmit() before sleeping
  1051. * in order to allow access to the socket to other RPC requests.
  1052. */
  1053. call_transmit_status(task);
  1054. if (rpc_reply_expected(task))
  1055. return;
  1056. task->tk_action = rpc_exit_task;
  1057. rpc_wake_up_queued_task(&task->tk_xprt->pending, task);
  1058. }
  1059. /*
  1060. * 5a. Handle cleanup after a transmission
  1061. */
  1062. static void
  1063. call_transmit_status(struct rpc_task *task)
  1064. {
  1065. task->tk_action = call_status;
  1066. /*
  1067. * Common case: success. Force the compiler to put this
  1068. * test first.
  1069. */
  1070. if (task->tk_status == 0) {
  1071. xprt_end_transmit(task);
  1072. rpc_task_force_reencode(task);
  1073. return;
  1074. }
  1075. switch (task->tk_status) {
  1076. case -EAGAIN:
  1077. break;
  1078. default:
  1079. dprint_status(task);
  1080. xprt_end_transmit(task);
  1081. rpc_task_force_reencode(task);
  1082. break;
  1083. /*
  1084. * Special cases: if we've been waiting on the
  1085. * socket's write_space() callback, or if the
  1086. * socket just returned a connection error,
  1087. * then hold onto the transport lock.
  1088. */
  1089. case -ECONNREFUSED:
  1090. case -EHOSTDOWN:
  1091. case -EHOSTUNREACH:
  1092. case -ENETUNREACH:
  1093. if (RPC_IS_SOFTCONN(task)) {
  1094. xprt_end_transmit(task);
  1095. rpc_exit(task, task->tk_status);
  1096. break;
  1097. }
  1098. case -ECONNRESET:
  1099. case -ENOTCONN:
  1100. case -EPIPE:
  1101. rpc_task_force_reencode(task);
  1102. }
  1103. }
  1104. #if defined(CONFIG_NFS_V4_1)
  1105. /*
  1106. * 5b. Send the backchannel RPC reply. On error, drop the reply. In
  1107. * addition, disconnect on connectivity errors.
  1108. */
  1109. static void
  1110. call_bc_transmit(struct rpc_task *task)
  1111. {
  1112. struct rpc_rqst *req = task->tk_rqstp;
  1113. BUG_ON(task->tk_status != 0);
  1114. task->tk_status = xprt_prepare_transmit(task);
  1115. if (task->tk_status == -EAGAIN) {
  1116. /*
  1117. * Could not reserve the transport. Try again after the
  1118. * transport is released.
  1119. */
  1120. task->tk_status = 0;
  1121. task->tk_action = call_bc_transmit;
  1122. return;
  1123. }
  1124. task->tk_action = rpc_exit_task;
  1125. if (task->tk_status < 0) {
  1126. printk(KERN_NOTICE "RPC: Could not send backchannel reply "
  1127. "error: %d\n", task->tk_status);
  1128. return;
  1129. }
  1130. xprt_transmit(task);
  1131. xprt_end_transmit(task);
  1132. dprint_status(task);
  1133. switch (task->tk_status) {
  1134. case 0:
  1135. /* Success */
  1136. break;
  1137. case -EHOSTDOWN:
  1138. case -EHOSTUNREACH:
  1139. case -ENETUNREACH:
  1140. case -ETIMEDOUT:
  1141. /*
  1142. * Problem reaching the server. Disconnect and let the
  1143. * forechannel reestablish the connection. The server will
  1144. * have to retransmit the backchannel request and we'll
  1145. * reprocess it. Since these ops are idempotent, there's no
  1146. * need to cache our reply at this time.
  1147. */
  1148. printk(KERN_NOTICE "RPC: Could not send backchannel reply "
  1149. "error: %d\n", task->tk_status);
  1150. xprt_conditional_disconnect(task->tk_xprt,
  1151. req->rq_connect_cookie);
  1152. break;
  1153. default:
  1154. /*
  1155. * We were unable to reply and will have to drop the
  1156. * request. The server should reconnect and retransmit.
  1157. */
  1158. BUG_ON(task->tk_status == -EAGAIN);
  1159. printk(KERN_NOTICE "RPC: Could not send backchannel reply "
  1160. "error: %d\n", task->tk_status);
  1161. break;
  1162. }
  1163. rpc_wake_up_queued_task(&req->rq_xprt->pending, task);
  1164. }
  1165. #endif /* CONFIG_NFS_V4_1 */
  1166. /*
  1167. * 6. Sort out the RPC call status
  1168. */
  1169. static void
  1170. call_status(struct rpc_task *task)
  1171. {
  1172. struct rpc_clnt *clnt = task->tk_client;
  1173. struct rpc_rqst *req = task->tk_rqstp;
  1174. int status;
  1175. if (req->rq_reply_bytes_recvd > 0 && !req->rq_bytes_sent)
  1176. task->tk_status = req->rq_reply_bytes_recvd;
  1177. dprint_status(task);
  1178. status = task->tk_status;
  1179. if (status >= 0) {
  1180. task->tk_action = call_decode;
  1181. return;
  1182. }
  1183. task->tk_status = 0;
  1184. switch(status) {
  1185. case -EHOSTDOWN:
  1186. case -EHOSTUNREACH:
  1187. case -ENETUNREACH:
  1188. /*
  1189. * Delay any retries for 3 seconds, then handle as if it
  1190. * were a timeout.
  1191. */
  1192. rpc_delay(task, 3*HZ);
  1193. case -ETIMEDOUT:
  1194. task->tk_action = call_timeout;
  1195. if (task->tk_client->cl_discrtry)
  1196. xprt_conditional_disconnect(task->tk_xprt,
  1197. req->rq_connect_cookie);
  1198. break;
  1199. case -ECONNRESET:
  1200. case -ECONNREFUSED:
  1201. rpc_force_rebind(clnt);
  1202. rpc_delay(task, 3*HZ);
  1203. case -EPIPE:
  1204. case -ENOTCONN:
  1205. task->tk_action = call_bind;
  1206. break;
  1207. case -EAGAIN:
  1208. task->tk_action = call_transmit;
  1209. break;
  1210. case -EIO:
  1211. /* shutdown or soft timeout */
  1212. rpc_exit(task, status);
  1213. break;
  1214. default:
  1215. if (clnt->cl_chatty)
  1216. printk("%s: RPC call returned error %d\n",
  1217. clnt->cl_protname, -status);
  1218. rpc_exit(task, status);
  1219. }
  1220. }
  1221. /*
  1222. * 6a. Handle RPC timeout
  1223. * We do not release the request slot, so we keep using the
  1224. * same XID for all retransmits.
  1225. */
  1226. static void
  1227. call_timeout(struct rpc_task *task)
  1228. {
  1229. struct rpc_clnt *clnt = task->tk_client;
  1230. if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
  1231. dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid);
  1232. goto retry;
  1233. }
  1234. dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
  1235. task->tk_timeouts++;
  1236. if (RPC_IS_SOFTCONN(task)) {
  1237. rpc_exit(task, -ETIMEDOUT);
  1238. return;
  1239. }
  1240. if (RPC_IS_SOFT(task)) {
  1241. if (clnt->cl_chatty)
  1242. printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
  1243. clnt->cl_protname, clnt->cl_server);
  1244. rpc_exit(task, -EIO);
  1245. return;
  1246. }
  1247. if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
  1248. task->tk_flags |= RPC_CALL_MAJORSEEN;
  1249. if (clnt->cl_chatty)
  1250. printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
  1251. clnt->cl_protname, clnt->cl_server);
  1252. }
  1253. rpc_force_rebind(clnt);
  1254. /*
  1255. * Did our request time out due to an RPCSEC_GSS out-of-sequence
  1256. * event? RFC2203 requires the server to drop all such requests.
  1257. */
  1258. rpcauth_invalcred(task);
  1259. retry:
  1260. clnt->cl_stats->rpcretrans++;
  1261. task->tk_action = call_bind;
  1262. task->tk_status = 0;
  1263. }
  1264. /*
  1265. * 7. Decode the RPC reply
  1266. */
  1267. static void
  1268. call_decode(struct rpc_task *task)
  1269. {
  1270. struct rpc_clnt *clnt = task->tk_client;
  1271. struct rpc_rqst *req = task->tk_rqstp;
  1272. kxdrproc_t decode = task->tk_msg.rpc_proc->p_decode;
  1273. __be32 *p;
  1274. dprintk("RPC: %5u call_decode (status %d)\n",
  1275. task->tk_pid, task->tk_status);
  1276. if (task->tk_flags & RPC_CALL_MAJORSEEN) {
  1277. if (clnt->cl_chatty)
  1278. printk(KERN_NOTICE "%s: server %s OK\n",
  1279. clnt->cl_protname, clnt->cl_server);
  1280. task->tk_flags &= ~RPC_CALL_MAJORSEEN;
  1281. }
  1282. /*
  1283. * Ensure that we see all writes made by xprt_complete_rqst()
  1284. * before it changed req->rq_reply_bytes_recvd.
  1285. */
  1286. smp_rmb();
  1287. req->rq_rcv_buf.len = req->rq_private_buf.len;
  1288. /* Check that the softirq receive buffer is valid */
  1289. WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
  1290. sizeof(req->rq_rcv_buf)) != 0);
  1291. if (req->rq_rcv_buf.len < 12) {
  1292. if (!RPC_IS_SOFT(task)) {
  1293. task->tk_action = call_bind;
  1294. clnt->cl_stats->rpcretrans++;
  1295. goto out_retry;
  1296. }
  1297. dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
  1298. clnt->cl_protname, task->tk_status);
  1299. task->tk_action = call_timeout;
  1300. goto out_retry;
  1301. }
  1302. p = rpc_verify_header(task);
  1303. if (IS_ERR(p)) {
  1304. if (p == ERR_PTR(-EAGAIN))
  1305. goto out_retry;
  1306. return;
  1307. }
  1308. task->tk_action = rpc_exit_task;
  1309. if (decode) {
  1310. task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
  1311. task->tk_msg.rpc_resp);
  1312. }
  1313. dprintk("RPC: %5u call_decode result %d\n", task->tk_pid,
  1314. task->tk_status);
  1315. return;
  1316. out_retry:
  1317. task->tk_status = 0;
  1318. /* Note: rpc_verify_header() may have freed the RPC slot */
  1319. if (task->tk_rqstp == req) {
  1320. req->rq_reply_bytes_recvd = req->rq_rcv_buf.len = 0;
  1321. if (task->tk_client->cl_discrtry)
  1322. xprt_conditional_disconnect(task->tk_xprt,
  1323. req->rq_connect_cookie);
  1324. }
  1325. }
  1326. /*
  1327. * 8. Refresh the credentials if rejected by the server
  1328. */
  1329. static void
  1330. call_refresh(struct rpc_task *task)
  1331. {
  1332. dprint_status(task);
  1333. task->tk_action = call_refreshresult;
  1334. task->tk_status = 0;
  1335. task->tk_client->cl_stats->rpcauthrefresh++;
  1336. rpcauth_refreshcred(task);
  1337. }
  1338. /*
  1339. * 8a. Process the results of a credential refresh
  1340. */
  1341. static void
  1342. call_refreshresult(struct rpc_task *task)
  1343. {
  1344. int status = task->tk_status;
  1345. dprint_status(task);
  1346. task->tk_status = 0;
  1347. task->tk_action = call_reserve;
  1348. if (status >= 0 && rpcauth_uptodatecred(task))
  1349. return;
  1350. if (status == -EACCES) {
  1351. rpc_exit(task, -EACCES);
  1352. return;
  1353. }
  1354. task->tk_action = call_refresh;
  1355. if (status != -ETIMEDOUT)
  1356. rpc_delay(task, 3*HZ);
  1357. return;
  1358. }
  1359. static __be32 *
  1360. rpc_encode_header(struct rpc_task *task)
  1361. {
  1362. struct rpc_clnt *clnt = task->tk_client;
  1363. struct rpc_rqst *req = task->tk_rqstp;
  1364. __be32 *p = req->rq_svec[0].iov_base;
  1365. /* FIXME: check buffer size? */
  1366. p = xprt_skip_transport_header(task->tk_xprt, p);
  1367. *p++ = req->rq_xid; /* XID */
  1368. *p++ = htonl(RPC_CALL); /* CALL */
  1369. *p++ = htonl(RPC_VERSION); /* RPC version */
  1370. *p++ = htonl(clnt->cl_prog); /* program number */
  1371. *p++ = htonl(clnt->cl_vers); /* program version */
  1372. *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
  1373. p = rpcauth_marshcred(task, p);
  1374. req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
  1375. return p;
  1376. }
  1377. static __be32 *
  1378. rpc_verify_header(struct rpc_task *task)
  1379. {
  1380. struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
  1381. int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
  1382. __be32 *p = iov->iov_base;
  1383. u32 n;
  1384. int error = -EACCES;
  1385. if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
  1386. /* RFC-1014 says that the representation of XDR data must be a
  1387. * multiple of four bytes
  1388. * - if it isn't pointer subtraction in the NFS client may give
  1389. * undefined results
  1390. */
  1391. dprintk("RPC: %5u %s: XDR representation not a multiple of"
  1392. " 4 bytes: 0x%x\n", task->tk_pid, __func__,
  1393. task->tk_rqstp->rq_rcv_buf.len);
  1394. goto out_eio;
  1395. }
  1396. if ((len -= 3) < 0)
  1397. goto out_overflow;
  1398. p += 1; /* skip XID */
  1399. if ((n = ntohl(*p++)) != RPC_REPLY) {
  1400. dprintk("RPC: %5u %s: not an RPC reply: %x\n",
  1401. task->tk_pid, __func__, n);
  1402. goto out_garbage;
  1403. }
  1404. if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
  1405. if (--len < 0)
  1406. goto out_overflow;
  1407. switch ((n = ntohl(*p++))) {
  1408. case RPC_AUTH_ERROR:
  1409. break;
  1410. case RPC_MISMATCH:
  1411. dprintk("RPC: %5u %s: RPC call version "
  1412. "mismatch!\n",
  1413. task->tk_pid, __func__);
  1414. error = -EPROTONOSUPPORT;
  1415. goto out_err;
  1416. default:
  1417. dprintk("RPC: %5u %s: RPC call rejected, "
  1418. "unknown error: %x\n",
  1419. task->tk_pid, __func__, n);
  1420. goto out_eio;
  1421. }
  1422. if (--len < 0)
  1423. goto out_overflow;
  1424. switch ((n = ntohl(*p++))) {
  1425. case RPC_AUTH_REJECTEDCRED:
  1426. case RPC_AUTH_REJECTEDVERF:
  1427. case RPCSEC_GSS_CREDPROBLEM:
  1428. case RPCSEC_GSS_CTXPROBLEM:
  1429. if (!task->tk_cred_retry)
  1430. break;
  1431. task->tk_cred_retry--;
  1432. dprintk("RPC: %5u %s: retry stale creds\n",
  1433. task->tk_pid, __func__);
  1434. rpcauth_invalcred(task);
  1435. /* Ensure we obtain a new XID! */
  1436. xprt_release(task);
  1437. task->tk_action = call_refresh;
  1438. goto out_retry;
  1439. case RPC_AUTH_BADCRED:
  1440. case RPC_AUTH_BADVERF:
  1441. /* possibly garbled cred/verf? */
  1442. if (!task->tk_garb_retry)
  1443. break;
  1444. task->tk_garb_retry--;
  1445. dprintk("RPC: %5u %s: retry garbled creds\n",
  1446. task->tk_pid, __func__);
  1447. task->tk_action = call_bind;
  1448. goto out_retry;
  1449. case RPC_AUTH_TOOWEAK:
  1450. printk(KERN_NOTICE "RPC: server %s requires stronger "
  1451. "authentication.\n", task->tk_client->cl_server);
  1452. break;
  1453. default:
  1454. dprintk("RPC: %5u %s: unknown auth error: %x\n",
  1455. task->tk_pid, __func__, n);
  1456. error = -EIO;
  1457. }
  1458. dprintk("RPC: %5u %s: call rejected %d\n",
  1459. task->tk_pid, __func__, n);
  1460. goto out_err;
  1461. }
  1462. if (!(p = rpcauth_checkverf(task, p))) {
  1463. dprintk("RPC: %5u %s: auth check failed\n",
  1464. task->tk_pid, __func__);
  1465. goto out_garbage; /* bad verifier, retry */
  1466. }
  1467. len = p - (__be32 *)iov->iov_base - 1;
  1468. if (len < 0)
  1469. goto out_overflow;
  1470. switch ((n = ntohl(*p++))) {
  1471. case RPC_SUCCESS:
  1472. return p;
  1473. case RPC_PROG_UNAVAIL:
  1474. dprintk("RPC: %5u %s: program %u is unsupported by server %s\n",
  1475. task->tk_pid, __func__,
  1476. (unsigned int)task->tk_client->cl_prog,
  1477. task->tk_client->cl_server);
  1478. error = -EPFNOSUPPORT;
  1479. goto out_err;
  1480. case RPC_PROG_MISMATCH:
  1481. dprintk("RPC: %5u %s: program %u, version %u unsupported by "
  1482. "server %s\n", task->tk_pid, __func__,
  1483. (unsigned int)task->tk_client->cl_prog,
  1484. (unsigned int)task->tk_client->cl_vers,
  1485. task->tk_client->cl_server);
  1486. error = -EPROTONOSUPPORT;
  1487. goto out_err;
  1488. case RPC_PROC_UNAVAIL:
  1489. dprintk("RPC: %5u %s: proc %s unsupported by program %u, "
  1490. "version %u on server %s\n",
  1491. task->tk_pid, __func__,
  1492. rpc_proc_name(task),
  1493. task->tk_client->cl_prog,
  1494. task->tk_client->cl_vers,
  1495. task->tk_client->cl_server);
  1496. error = -EOPNOTSUPP;
  1497. goto out_err;
  1498. case RPC_GARBAGE_ARGS:
  1499. dprintk("RPC: %5u %s: server saw garbage\n",
  1500. task->tk_pid, __func__);
  1501. break; /* retry */
  1502. default:
  1503. dprintk("RPC: %5u %s: server accept status: %x\n",
  1504. task->tk_pid, __func__, n);
  1505. /* Also retry */
  1506. }
  1507. out_garbage:
  1508. task->tk_client->cl_stats->rpcgarbage++;
  1509. if (task->tk_garb_retry) {
  1510. task->tk_garb_retry--;
  1511. dprintk("RPC: %5u %s: retrying\n",
  1512. task->tk_pid, __func__);
  1513. task->tk_action = call_bind;
  1514. out_retry:
  1515. return ERR_PTR(-EAGAIN);
  1516. }
  1517. out_eio:
  1518. error = -EIO;
  1519. out_err:
  1520. rpc_exit(task, error);
  1521. dprintk("RPC: %5u %s: call failed with error %d\n", task->tk_pid,
  1522. __func__, error);
  1523. return ERR_PTR(error);
  1524. out_overflow:
  1525. dprintk("RPC: %5u %s: server reply was truncated.\n", task->tk_pid,
  1526. __func__);
  1527. goto out_garbage;
  1528. }
  1529. static int rpcproc_encode_null(void *rqstp, __be32 *data, void *obj)
  1530. {
  1531. return 0;
  1532. }
  1533. static int rpcproc_decode_null(void *rqstp, __be32 *data, void *obj)
  1534. {
  1535. return 0;
  1536. }
  1537. static struct rpc_procinfo rpcproc_null = {
  1538. .p_encode = rpcproc_encode_null,
  1539. .p_decode = rpcproc_decode_null,
  1540. };
  1541. static int rpc_ping(struct rpc_clnt *clnt)
  1542. {
  1543. struct rpc_message msg = {
  1544. .rpc_proc = &rpcproc_null,
  1545. };
  1546. int err;
  1547. msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
  1548. err = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN);
  1549. put_rpccred(msg.rpc_cred);
  1550. return err;
  1551. }
  1552. struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
  1553. {
  1554. struct rpc_message msg = {
  1555. .rpc_proc = &rpcproc_null,
  1556. .rpc_cred = cred,
  1557. };
  1558. struct rpc_task_setup task_setup_data = {
  1559. .rpc_client = clnt,
  1560. .rpc_message = &msg,
  1561. .callback_ops = &rpc_default_ops,
  1562. .flags = flags,
  1563. };
  1564. return rpc_run_task(&task_setup_data);
  1565. }
  1566. EXPORT_SYMBOL_GPL(rpc_call_null);
  1567. #ifdef RPC_DEBUG
  1568. static void rpc_show_header(void)
  1569. {
  1570. printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
  1571. "-timeout ---ops--\n");
  1572. }
  1573. static void rpc_show_task(const struct rpc_clnt *clnt,
  1574. const struct rpc_task *task)
  1575. {
  1576. const char *rpc_waitq = "none";
  1577. char *p, action[KSYM_SYMBOL_LEN];
  1578. if (RPC_IS_QUEUED(task))
  1579. rpc_waitq = rpc_qname(task->tk_waitqueue);
  1580. /* map tk_action pointer to a function name; then trim off
  1581. * the "+0x0 [sunrpc]" */
  1582. sprint_symbol(action, (unsigned long)task->tk_action);
  1583. p = strchr(action, '+');
  1584. if (p)
  1585. *p = '\0';
  1586. printk(KERN_INFO "%5u %04x %6d %8p %8p %8ld %8p %sv%u %s a:%s q:%s\n",
  1587. task->tk_pid, task->tk_flags, task->tk_status,
  1588. clnt, task->tk_rqstp, task->tk_timeout, task->tk_ops,
  1589. clnt->cl_protname, clnt->cl_vers, rpc_proc_name(task),
  1590. action, rpc_waitq);
  1591. }
  1592. void rpc_show_tasks(void)
  1593. {
  1594. struct rpc_clnt *clnt;
  1595. struct rpc_task *task;
  1596. int header = 0;
  1597. spin_lock(&rpc_client_lock);
  1598. list_for_each_entry(clnt, &all_clients, cl_clients) {
  1599. spin_lock(&clnt->cl_lock);
  1600. list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
  1601. if (!header) {
  1602. rpc_show_header();
  1603. header++;
  1604. }
  1605. rpc_show_task(clnt, task);
  1606. }
  1607. spin_unlock(&clnt->cl_lock);
  1608. }
  1609. spin_unlock(&rpc_client_lock);
  1610. }
  1611. #endif