clnt.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756
  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, int flags);
  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, RPC_TASK_SOFT);
  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, RPC_TASK_SOFT);
  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. goto out;
  595. }
  596. task->tk_rqstp = req;
  597. /*
  598. * Set up the xdr_buf length.
  599. * This also indicates that the buffer is XDR encoded already.
  600. */
  601. xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
  602. xbufp->tail[0].iov_len;
  603. task->tk_action = call_bc_transmit;
  604. atomic_inc(&task->tk_count);
  605. BUG_ON(atomic_read(&task->tk_count) != 2);
  606. rpc_execute(task);
  607. out:
  608. dprintk("RPC: rpc_run_bc_task: task= %p\n", task);
  609. return task;
  610. }
  611. #endif /* CONFIG_NFS_V4_1 */
  612. void
  613. rpc_call_start(struct rpc_task *task)
  614. {
  615. task->tk_action = call_start;
  616. }
  617. EXPORT_SYMBOL_GPL(rpc_call_start);
  618. /**
  619. * rpc_peeraddr - extract remote peer address from clnt's xprt
  620. * @clnt: RPC client structure
  621. * @buf: target buffer
  622. * @bufsize: length of target buffer
  623. *
  624. * Returns the number of bytes that are actually in the stored address.
  625. */
  626. size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
  627. {
  628. size_t bytes;
  629. struct rpc_xprt *xprt = clnt->cl_xprt;
  630. bytes = sizeof(xprt->addr);
  631. if (bytes > bufsize)
  632. bytes = bufsize;
  633. memcpy(buf, &clnt->cl_xprt->addr, bytes);
  634. return xprt->addrlen;
  635. }
  636. EXPORT_SYMBOL_GPL(rpc_peeraddr);
  637. /**
  638. * rpc_peeraddr2str - return remote peer address in printable format
  639. * @clnt: RPC client structure
  640. * @format: address format
  641. *
  642. */
  643. const char *rpc_peeraddr2str(struct rpc_clnt *clnt,
  644. enum rpc_display_format_t format)
  645. {
  646. struct rpc_xprt *xprt = clnt->cl_xprt;
  647. if (xprt->address_strings[format] != NULL)
  648. return xprt->address_strings[format];
  649. else
  650. return "unprintable";
  651. }
  652. EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
  653. void
  654. rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
  655. {
  656. struct rpc_xprt *xprt = clnt->cl_xprt;
  657. if (xprt->ops->set_buffer_size)
  658. xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
  659. }
  660. EXPORT_SYMBOL_GPL(rpc_setbufsize);
  661. /*
  662. * Return size of largest payload RPC client can support, in bytes
  663. *
  664. * For stream transports, this is one RPC record fragment (see RFC
  665. * 1831), as we don't support multi-record requests yet. For datagram
  666. * transports, this is the size of an IP packet minus the IP, UDP, and
  667. * RPC header sizes.
  668. */
  669. size_t rpc_max_payload(struct rpc_clnt *clnt)
  670. {
  671. return clnt->cl_xprt->max_payload;
  672. }
  673. EXPORT_SYMBOL_GPL(rpc_max_payload);
  674. /**
  675. * rpc_force_rebind - force transport to check that remote port is unchanged
  676. * @clnt: client to rebind
  677. *
  678. */
  679. void rpc_force_rebind(struct rpc_clnt *clnt)
  680. {
  681. if (clnt->cl_autobind)
  682. xprt_clear_bound(clnt->cl_xprt);
  683. }
  684. EXPORT_SYMBOL_GPL(rpc_force_rebind);
  685. /*
  686. * Restart an (async) RPC call from the call_prepare state.
  687. * Usually called from within the exit handler.
  688. */
  689. void
  690. rpc_restart_call_prepare(struct rpc_task *task)
  691. {
  692. if (RPC_ASSASSINATED(task))
  693. return;
  694. task->tk_action = rpc_prepare_task;
  695. }
  696. EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
  697. /*
  698. * Restart an (async) RPC call. Usually called from within the
  699. * exit handler.
  700. */
  701. void
  702. rpc_restart_call(struct rpc_task *task)
  703. {
  704. if (RPC_ASSASSINATED(task))
  705. return;
  706. task->tk_action = call_start;
  707. }
  708. EXPORT_SYMBOL_GPL(rpc_restart_call);
  709. #ifdef RPC_DEBUG
  710. static const char *rpc_proc_name(const struct rpc_task *task)
  711. {
  712. const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
  713. if (proc) {
  714. if (proc->p_name)
  715. return proc->p_name;
  716. else
  717. return "NULL";
  718. } else
  719. return "no proc";
  720. }
  721. #endif
  722. /*
  723. * 0. Initial state
  724. *
  725. * Other FSM states can be visited zero or more times, but
  726. * this state is visited exactly once for each RPC.
  727. */
  728. static void
  729. call_start(struct rpc_task *task)
  730. {
  731. struct rpc_clnt *clnt = task->tk_client;
  732. dprintk("RPC: %5u call_start %s%d proc %s (%s)\n", task->tk_pid,
  733. clnt->cl_protname, clnt->cl_vers,
  734. rpc_proc_name(task),
  735. (RPC_IS_ASYNC(task) ? "async" : "sync"));
  736. /* Increment call count */
  737. task->tk_msg.rpc_proc->p_count++;
  738. clnt->cl_stats->rpccnt++;
  739. task->tk_action = call_reserve;
  740. }
  741. /*
  742. * 1. Reserve an RPC call slot
  743. */
  744. static void
  745. call_reserve(struct rpc_task *task)
  746. {
  747. dprint_status(task);
  748. if (!rpcauth_uptodatecred(task)) {
  749. task->tk_action = call_refresh;
  750. return;
  751. }
  752. task->tk_status = 0;
  753. task->tk_action = call_reserveresult;
  754. xprt_reserve(task);
  755. }
  756. /*
  757. * 1b. Grok the result of xprt_reserve()
  758. */
  759. static void
  760. call_reserveresult(struct rpc_task *task)
  761. {
  762. int status = task->tk_status;
  763. dprint_status(task);
  764. /*
  765. * After a call to xprt_reserve(), we must have either
  766. * a request slot or else an error status.
  767. */
  768. task->tk_status = 0;
  769. if (status >= 0) {
  770. if (task->tk_rqstp) {
  771. task->tk_action = call_allocate;
  772. return;
  773. }
  774. printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
  775. __func__, status);
  776. rpc_exit(task, -EIO);
  777. return;
  778. }
  779. /*
  780. * Even though there was an error, we may have acquired
  781. * a request slot somehow. Make sure not to leak it.
  782. */
  783. if (task->tk_rqstp) {
  784. printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
  785. __func__, status);
  786. xprt_release(task);
  787. }
  788. switch (status) {
  789. case -EAGAIN: /* woken up; retry */
  790. task->tk_action = call_reserve;
  791. return;
  792. case -EIO: /* probably a shutdown */
  793. break;
  794. default:
  795. printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
  796. __func__, status);
  797. break;
  798. }
  799. rpc_exit(task, status);
  800. }
  801. /*
  802. * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
  803. * (Note: buffer memory is freed in xprt_release).
  804. */
  805. static void
  806. call_allocate(struct rpc_task *task)
  807. {
  808. unsigned int slack = task->tk_msg.rpc_cred->cr_auth->au_cslack;
  809. struct rpc_rqst *req = task->tk_rqstp;
  810. struct rpc_xprt *xprt = task->tk_xprt;
  811. struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
  812. dprint_status(task);
  813. task->tk_status = 0;
  814. task->tk_action = call_bind;
  815. if (req->rq_buffer)
  816. return;
  817. if (proc->p_proc != 0) {
  818. BUG_ON(proc->p_arglen == 0);
  819. if (proc->p_decode != NULL)
  820. BUG_ON(proc->p_replen == 0);
  821. }
  822. /*
  823. * Calculate the size (in quads) of the RPC call
  824. * and reply headers, and convert both values
  825. * to byte sizes.
  826. */
  827. req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen;
  828. req->rq_callsize <<= 2;
  829. req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen;
  830. req->rq_rcvsize <<= 2;
  831. req->rq_buffer = xprt->ops->buf_alloc(task,
  832. req->rq_callsize + req->rq_rcvsize);
  833. if (req->rq_buffer != NULL)
  834. return;
  835. dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
  836. if (RPC_IS_ASYNC(task) || !signalled()) {
  837. task->tk_action = call_allocate;
  838. rpc_delay(task, HZ>>4);
  839. return;
  840. }
  841. rpc_exit(task, -ERESTARTSYS);
  842. }
  843. static inline int
  844. rpc_task_need_encode(struct rpc_task *task)
  845. {
  846. return task->tk_rqstp->rq_snd_buf.len == 0;
  847. }
  848. static inline void
  849. rpc_task_force_reencode(struct rpc_task *task)
  850. {
  851. task->tk_rqstp->rq_snd_buf.len = 0;
  852. task->tk_rqstp->rq_bytes_sent = 0;
  853. }
  854. static inline void
  855. rpc_xdr_buf_init(struct xdr_buf *buf, void *start, size_t len)
  856. {
  857. buf->head[0].iov_base = start;
  858. buf->head[0].iov_len = len;
  859. buf->tail[0].iov_len = 0;
  860. buf->page_len = 0;
  861. buf->flags = 0;
  862. buf->len = 0;
  863. buf->buflen = len;
  864. }
  865. /*
  866. * 3. Encode arguments of an RPC call
  867. */
  868. static void
  869. rpc_xdr_encode(struct rpc_task *task)
  870. {
  871. struct rpc_rqst *req = task->tk_rqstp;
  872. kxdrproc_t encode;
  873. __be32 *p;
  874. dprint_status(task);
  875. rpc_xdr_buf_init(&req->rq_snd_buf,
  876. req->rq_buffer,
  877. req->rq_callsize);
  878. rpc_xdr_buf_init(&req->rq_rcv_buf,
  879. (char *)req->rq_buffer + req->rq_callsize,
  880. req->rq_rcvsize);
  881. p = rpc_encode_header(task);
  882. if (p == NULL) {
  883. printk(KERN_INFO "RPC: couldn't encode RPC header, exit EIO\n");
  884. rpc_exit(task, -EIO);
  885. return;
  886. }
  887. encode = task->tk_msg.rpc_proc->p_encode;
  888. if (encode == NULL)
  889. return;
  890. task->tk_status = rpcauth_wrap_req(task, encode, req, p,
  891. task->tk_msg.rpc_argp);
  892. }
  893. /*
  894. * 4. Get the server port number if not yet set
  895. */
  896. static void
  897. call_bind(struct rpc_task *task)
  898. {
  899. struct rpc_xprt *xprt = task->tk_xprt;
  900. dprint_status(task);
  901. task->tk_action = call_connect;
  902. if (!xprt_bound(xprt)) {
  903. task->tk_action = call_bind_status;
  904. task->tk_timeout = xprt->bind_timeout;
  905. xprt->ops->rpcbind(task);
  906. }
  907. }
  908. /*
  909. * 4a. Sort out bind result
  910. */
  911. static void
  912. call_bind_status(struct rpc_task *task)
  913. {
  914. int status = -EIO;
  915. if (task->tk_status >= 0) {
  916. dprint_status(task);
  917. task->tk_status = 0;
  918. task->tk_action = call_connect;
  919. return;
  920. }
  921. switch (task->tk_status) {
  922. case -ENOMEM:
  923. dprintk("RPC: %5u rpcbind out of memory\n", task->tk_pid);
  924. rpc_delay(task, HZ >> 2);
  925. goto retry_timeout;
  926. case -EACCES:
  927. dprintk("RPC: %5u remote rpcbind: RPC program/version "
  928. "unavailable\n", task->tk_pid);
  929. /* fail immediately if this is an RPC ping */
  930. if (task->tk_msg.rpc_proc->p_proc == 0) {
  931. status = -EOPNOTSUPP;
  932. break;
  933. }
  934. rpc_delay(task, 3*HZ);
  935. goto retry_timeout;
  936. case -ETIMEDOUT:
  937. dprintk("RPC: %5u rpcbind request timed out\n",
  938. task->tk_pid);
  939. goto retry_timeout;
  940. case -EPFNOSUPPORT:
  941. /* server doesn't support any rpcbind version we know of */
  942. dprintk("RPC: %5u remote rpcbind service unavailable\n",
  943. task->tk_pid);
  944. break;
  945. case -EPROTONOSUPPORT:
  946. dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
  947. task->tk_pid);
  948. task->tk_status = 0;
  949. task->tk_action = call_bind;
  950. return;
  951. default:
  952. dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
  953. task->tk_pid, -task->tk_status);
  954. }
  955. rpc_exit(task, status);
  956. return;
  957. retry_timeout:
  958. task->tk_action = call_timeout;
  959. }
  960. /*
  961. * 4b. Connect to the RPC server
  962. */
  963. static void
  964. call_connect(struct rpc_task *task)
  965. {
  966. struct rpc_xprt *xprt = task->tk_xprt;
  967. dprintk("RPC: %5u call_connect xprt %p %s connected\n",
  968. task->tk_pid, xprt,
  969. (xprt_connected(xprt) ? "is" : "is not"));
  970. task->tk_action = call_transmit;
  971. if (!xprt_connected(xprt)) {
  972. task->tk_action = call_connect_status;
  973. if (task->tk_status < 0)
  974. return;
  975. xprt_connect(task);
  976. }
  977. }
  978. /*
  979. * 4c. Sort out connect result
  980. */
  981. static void
  982. call_connect_status(struct rpc_task *task)
  983. {
  984. struct rpc_clnt *clnt = task->tk_client;
  985. int status = task->tk_status;
  986. dprint_status(task);
  987. task->tk_status = 0;
  988. if (status >= 0 || status == -EAGAIN) {
  989. clnt->cl_stats->netreconn++;
  990. task->tk_action = call_transmit;
  991. return;
  992. }
  993. switch (status) {
  994. /* if soft mounted, test if we've timed out */
  995. case -ETIMEDOUT:
  996. task->tk_action = call_timeout;
  997. break;
  998. default:
  999. rpc_exit(task, -EIO);
  1000. }
  1001. }
  1002. /*
  1003. * 5. Transmit the RPC request, and wait for reply
  1004. */
  1005. static void
  1006. call_transmit(struct rpc_task *task)
  1007. {
  1008. dprint_status(task);
  1009. task->tk_action = call_status;
  1010. if (task->tk_status < 0)
  1011. return;
  1012. task->tk_status = xprt_prepare_transmit(task);
  1013. if (task->tk_status != 0)
  1014. return;
  1015. task->tk_action = call_transmit_status;
  1016. /* Encode here so that rpcsec_gss can use correct sequence number. */
  1017. if (rpc_task_need_encode(task)) {
  1018. BUG_ON(task->tk_rqstp->rq_bytes_sent != 0);
  1019. rpc_xdr_encode(task);
  1020. /* Did the encode result in an error condition? */
  1021. if (task->tk_status != 0) {
  1022. /* Was the error nonfatal? */
  1023. if (task->tk_status == -EAGAIN)
  1024. rpc_delay(task, HZ >> 4);
  1025. else
  1026. rpc_exit(task, task->tk_status);
  1027. return;
  1028. }
  1029. }
  1030. xprt_transmit(task);
  1031. if (task->tk_status < 0)
  1032. return;
  1033. /*
  1034. * On success, ensure that we call xprt_end_transmit() before sleeping
  1035. * in order to allow access to the socket to other RPC requests.
  1036. */
  1037. call_transmit_status(task);
  1038. if (rpc_reply_expected(task))
  1039. return;
  1040. task->tk_action = rpc_exit_task;
  1041. rpc_wake_up_queued_task(&task->tk_xprt->pending, task);
  1042. }
  1043. /*
  1044. * 5a. Handle cleanup after a transmission
  1045. */
  1046. static void
  1047. call_transmit_status(struct rpc_task *task)
  1048. {
  1049. task->tk_action = call_status;
  1050. switch (task->tk_status) {
  1051. case -EAGAIN:
  1052. break;
  1053. default:
  1054. xprt_end_transmit(task);
  1055. /*
  1056. * Special cases: if we've been waiting on the
  1057. * socket's write_space() callback, or if the
  1058. * socket just returned a connection error,
  1059. * then hold onto the transport lock.
  1060. */
  1061. case -ECONNREFUSED:
  1062. case -ECONNRESET:
  1063. case -ENOTCONN:
  1064. case -EHOSTDOWN:
  1065. case -EHOSTUNREACH:
  1066. case -ENETUNREACH:
  1067. case -EPIPE:
  1068. rpc_task_force_reencode(task);
  1069. }
  1070. }
  1071. #if defined(CONFIG_NFS_V4_1)
  1072. /*
  1073. * 5b. Send the backchannel RPC reply. On error, drop the reply. In
  1074. * addition, disconnect on connectivity errors.
  1075. */
  1076. static void
  1077. call_bc_transmit(struct rpc_task *task)
  1078. {
  1079. struct rpc_rqst *req = task->tk_rqstp;
  1080. BUG_ON(task->tk_status != 0);
  1081. task->tk_status = xprt_prepare_transmit(task);
  1082. if (task->tk_status == -EAGAIN) {
  1083. /*
  1084. * Could not reserve the transport. Try again after the
  1085. * transport is released.
  1086. */
  1087. task->tk_status = 0;
  1088. task->tk_action = call_bc_transmit;
  1089. return;
  1090. }
  1091. task->tk_action = rpc_exit_task;
  1092. if (task->tk_status < 0) {
  1093. printk(KERN_NOTICE "RPC: Could not send backchannel reply "
  1094. "error: %d\n", task->tk_status);
  1095. return;
  1096. }
  1097. xprt_transmit(task);
  1098. xprt_end_transmit(task);
  1099. dprint_status(task);
  1100. switch (task->tk_status) {
  1101. case 0:
  1102. /* Success */
  1103. break;
  1104. case -EHOSTDOWN:
  1105. case -EHOSTUNREACH:
  1106. case -ENETUNREACH:
  1107. case -ETIMEDOUT:
  1108. /*
  1109. * Problem reaching the server. Disconnect and let the
  1110. * forechannel reestablish the connection. The server will
  1111. * have to retransmit the backchannel request and we'll
  1112. * reprocess it. Since these ops are idempotent, there's no
  1113. * need to cache our reply at this time.
  1114. */
  1115. printk(KERN_NOTICE "RPC: Could not send backchannel reply "
  1116. "error: %d\n", task->tk_status);
  1117. xprt_conditional_disconnect(task->tk_xprt,
  1118. req->rq_connect_cookie);
  1119. break;
  1120. default:
  1121. /*
  1122. * We were unable to reply and will have to drop the
  1123. * request. The server should reconnect and retransmit.
  1124. */
  1125. BUG_ON(task->tk_status == -EAGAIN);
  1126. printk(KERN_NOTICE "RPC: Could not send backchannel reply "
  1127. "error: %d\n", task->tk_status);
  1128. break;
  1129. }
  1130. rpc_wake_up_queued_task(&req->rq_xprt->pending, task);
  1131. }
  1132. #endif /* CONFIG_NFS_V4_1 */
  1133. /*
  1134. * 6. Sort out the RPC call status
  1135. */
  1136. static void
  1137. call_status(struct rpc_task *task)
  1138. {
  1139. struct rpc_clnt *clnt = task->tk_client;
  1140. struct rpc_rqst *req = task->tk_rqstp;
  1141. int status;
  1142. if (req->rq_reply_bytes_recvd > 0 && !req->rq_bytes_sent)
  1143. task->tk_status = req->rq_reply_bytes_recvd;
  1144. dprint_status(task);
  1145. status = task->tk_status;
  1146. if (status >= 0) {
  1147. task->tk_action = call_decode;
  1148. return;
  1149. }
  1150. task->tk_status = 0;
  1151. switch(status) {
  1152. case -EHOSTDOWN:
  1153. case -EHOSTUNREACH:
  1154. case -ENETUNREACH:
  1155. /*
  1156. * Delay any retries for 3 seconds, then handle as if it
  1157. * were a timeout.
  1158. */
  1159. rpc_delay(task, 3*HZ);
  1160. case -ETIMEDOUT:
  1161. task->tk_action = call_timeout;
  1162. if (task->tk_client->cl_discrtry)
  1163. xprt_conditional_disconnect(task->tk_xprt,
  1164. req->rq_connect_cookie);
  1165. break;
  1166. case -ECONNRESET:
  1167. case -ECONNREFUSED:
  1168. rpc_force_rebind(clnt);
  1169. rpc_delay(task, 3*HZ);
  1170. case -EPIPE:
  1171. case -ENOTCONN:
  1172. task->tk_action = call_bind;
  1173. break;
  1174. case -EAGAIN:
  1175. task->tk_action = call_transmit;
  1176. break;
  1177. case -EIO:
  1178. /* shutdown or soft timeout */
  1179. rpc_exit(task, status);
  1180. break;
  1181. default:
  1182. if (clnt->cl_chatty)
  1183. printk("%s: RPC call returned error %d\n",
  1184. clnt->cl_protname, -status);
  1185. rpc_exit(task, status);
  1186. }
  1187. }
  1188. /*
  1189. * 6a. Handle RPC timeout
  1190. * We do not release the request slot, so we keep using the
  1191. * same XID for all retransmits.
  1192. */
  1193. static void
  1194. call_timeout(struct rpc_task *task)
  1195. {
  1196. struct rpc_clnt *clnt = task->tk_client;
  1197. if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
  1198. dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid);
  1199. goto retry;
  1200. }
  1201. dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
  1202. task->tk_timeouts++;
  1203. if (RPC_IS_SOFT(task)) {
  1204. if (clnt->cl_chatty)
  1205. printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
  1206. clnt->cl_protname, clnt->cl_server);
  1207. rpc_exit(task, -EIO);
  1208. return;
  1209. }
  1210. if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
  1211. task->tk_flags |= RPC_CALL_MAJORSEEN;
  1212. if (clnt->cl_chatty)
  1213. printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
  1214. clnt->cl_protname, clnt->cl_server);
  1215. }
  1216. rpc_force_rebind(clnt);
  1217. /*
  1218. * Did our request time out due to an RPCSEC_GSS out-of-sequence
  1219. * event? RFC2203 requires the server to drop all such requests.
  1220. */
  1221. rpcauth_invalcred(task);
  1222. retry:
  1223. clnt->cl_stats->rpcretrans++;
  1224. task->tk_action = call_bind;
  1225. task->tk_status = 0;
  1226. }
  1227. /*
  1228. * 7. Decode the RPC reply
  1229. */
  1230. static void
  1231. call_decode(struct rpc_task *task)
  1232. {
  1233. struct rpc_clnt *clnt = task->tk_client;
  1234. struct rpc_rqst *req = task->tk_rqstp;
  1235. kxdrproc_t decode = task->tk_msg.rpc_proc->p_decode;
  1236. __be32 *p;
  1237. dprintk("RPC: %5u call_decode (status %d)\n",
  1238. task->tk_pid, task->tk_status);
  1239. if (task->tk_flags & RPC_CALL_MAJORSEEN) {
  1240. if (clnt->cl_chatty)
  1241. printk(KERN_NOTICE "%s: server %s OK\n",
  1242. clnt->cl_protname, clnt->cl_server);
  1243. task->tk_flags &= ~RPC_CALL_MAJORSEEN;
  1244. }
  1245. /*
  1246. * Ensure that we see all writes made by xprt_complete_rqst()
  1247. * before it changed req->rq_reply_bytes_recvd.
  1248. */
  1249. smp_rmb();
  1250. req->rq_rcv_buf.len = req->rq_private_buf.len;
  1251. /* Check that the softirq receive buffer is valid */
  1252. WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
  1253. sizeof(req->rq_rcv_buf)) != 0);
  1254. if (req->rq_rcv_buf.len < 12) {
  1255. if (!RPC_IS_SOFT(task)) {
  1256. task->tk_action = call_bind;
  1257. clnt->cl_stats->rpcretrans++;
  1258. goto out_retry;
  1259. }
  1260. dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
  1261. clnt->cl_protname, task->tk_status);
  1262. task->tk_action = call_timeout;
  1263. goto out_retry;
  1264. }
  1265. p = rpc_verify_header(task);
  1266. if (IS_ERR(p)) {
  1267. if (p == ERR_PTR(-EAGAIN))
  1268. goto out_retry;
  1269. return;
  1270. }
  1271. task->tk_action = rpc_exit_task;
  1272. if (decode) {
  1273. task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
  1274. task->tk_msg.rpc_resp);
  1275. }
  1276. dprintk("RPC: %5u call_decode result %d\n", task->tk_pid,
  1277. task->tk_status);
  1278. return;
  1279. out_retry:
  1280. task->tk_status = 0;
  1281. /* Note: rpc_verify_header() may have freed the RPC slot */
  1282. if (task->tk_rqstp == req) {
  1283. req->rq_reply_bytes_recvd = req->rq_rcv_buf.len = 0;
  1284. if (task->tk_client->cl_discrtry)
  1285. xprt_conditional_disconnect(task->tk_xprt,
  1286. req->rq_connect_cookie);
  1287. }
  1288. }
  1289. /*
  1290. * 8. Refresh the credentials if rejected by the server
  1291. */
  1292. static void
  1293. call_refresh(struct rpc_task *task)
  1294. {
  1295. dprint_status(task);
  1296. task->tk_action = call_refreshresult;
  1297. task->tk_status = 0;
  1298. task->tk_client->cl_stats->rpcauthrefresh++;
  1299. rpcauth_refreshcred(task);
  1300. }
  1301. /*
  1302. * 8a. Process the results of a credential refresh
  1303. */
  1304. static void
  1305. call_refreshresult(struct rpc_task *task)
  1306. {
  1307. int status = task->tk_status;
  1308. dprint_status(task);
  1309. task->tk_status = 0;
  1310. task->tk_action = call_reserve;
  1311. if (status >= 0 && rpcauth_uptodatecred(task))
  1312. return;
  1313. if (status == -EACCES) {
  1314. rpc_exit(task, -EACCES);
  1315. return;
  1316. }
  1317. task->tk_action = call_refresh;
  1318. if (status != -ETIMEDOUT)
  1319. rpc_delay(task, 3*HZ);
  1320. return;
  1321. }
  1322. static __be32 *
  1323. rpc_encode_header(struct rpc_task *task)
  1324. {
  1325. struct rpc_clnt *clnt = task->tk_client;
  1326. struct rpc_rqst *req = task->tk_rqstp;
  1327. __be32 *p = req->rq_svec[0].iov_base;
  1328. /* FIXME: check buffer size? */
  1329. p = xprt_skip_transport_header(task->tk_xprt, p);
  1330. *p++ = req->rq_xid; /* XID */
  1331. *p++ = htonl(RPC_CALL); /* CALL */
  1332. *p++ = htonl(RPC_VERSION); /* RPC version */
  1333. *p++ = htonl(clnt->cl_prog); /* program number */
  1334. *p++ = htonl(clnt->cl_vers); /* program version */
  1335. *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
  1336. p = rpcauth_marshcred(task, p);
  1337. req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
  1338. return p;
  1339. }
  1340. static __be32 *
  1341. rpc_verify_header(struct rpc_task *task)
  1342. {
  1343. struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
  1344. int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
  1345. __be32 *p = iov->iov_base;
  1346. u32 n;
  1347. int error = -EACCES;
  1348. if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
  1349. /* RFC-1014 says that the representation of XDR data must be a
  1350. * multiple of four bytes
  1351. * - if it isn't pointer subtraction in the NFS client may give
  1352. * undefined results
  1353. */
  1354. dprintk("RPC: %5u %s: XDR representation not a multiple of"
  1355. " 4 bytes: 0x%x\n", task->tk_pid, __func__,
  1356. task->tk_rqstp->rq_rcv_buf.len);
  1357. goto out_eio;
  1358. }
  1359. if ((len -= 3) < 0)
  1360. goto out_overflow;
  1361. p += 1; /* skip XID */
  1362. if ((n = ntohl(*p++)) != RPC_REPLY) {
  1363. dprintk("RPC: %5u %s: not an RPC reply: %x\n",
  1364. task->tk_pid, __func__, n);
  1365. goto out_garbage;
  1366. }
  1367. if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
  1368. if (--len < 0)
  1369. goto out_overflow;
  1370. switch ((n = ntohl(*p++))) {
  1371. case RPC_AUTH_ERROR:
  1372. break;
  1373. case RPC_MISMATCH:
  1374. dprintk("RPC: %5u %s: RPC call version "
  1375. "mismatch!\n",
  1376. task->tk_pid, __func__);
  1377. error = -EPROTONOSUPPORT;
  1378. goto out_err;
  1379. default:
  1380. dprintk("RPC: %5u %s: RPC call rejected, "
  1381. "unknown error: %x\n",
  1382. task->tk_pid, __func__, n);
  1383. goto out_eio;
  1384. }
  1385. if (--len < 0)
  1386. goto out_overflow;
  1387. switch ((n = ntohl(*p++))) {
  1388. case RPC_AUTH_REJECTEDCRED:
  1389. case RPC_AUTH_REJECTEDVERF:
  1390. case RPCSEC_GSS_CREDPROBLEM:
  1391. case RPCSEC_GSS_CTXPROBLEM:
  1392. if (!task->tk_cred_retry)
  1393. break;
  1394. task->tk_cred_retry--;
  1395. dprintk("RPC: %5u %s: retry stale creds\n",
  1396. task->tk_pid, __func__);
  1397. rpcauth_invalcred(task);
  1398. /* Ensure we obtain a new XID! */
  1399. xprt_release(task);
  1400. task->tk_action = call_refresh;
  1401. goto out_retry;
  1402. case RPC_AUTH_BADCRED:
  1403. case RPC_AUTH_BADVERF:
  1404. /* possibly garbled cred/verf? */
  1405. if (!task->tk_garb_retry)
  1406. break;
  1407. task->tk_garb_retry--;
  1408. dprintk("RPC: %5u %s: retry garbled creds\n",
  1409. task->tk_pid, __func__);
  1410. task->tk_action = call_bind;
  1411. goto out_retry;
  1412. case RPC_AUTH_TOOWEAK:
  1413. printk(KERN_NOTICE "RPC: server %s requires stronger "
  1414. "authentication.\n", task->tk_client->cl_server);
  1415. break;
  1416. default:
  1417. dprintk("RPC: %5u %s: unknown auth error: %x\n",
  1418. task->tk_pid, __func__, n);
  1419. error = -EIO;
  1420. }
  1421. dprintk("RPC: %5u %s: call rejected %d\n",
  1422. task->tk_pid, __func__, n);
  1423. goto out_err;
  1424. }
  1425. if (!(p = rpcauth_checkverf(task, p))) {
  1426. dprintk("RPC: %5u %s: auth check failed\n",
  1427. task->tk_pid, __func__);
  1428. goto out_garbage; /* bad verifier, retry */
  1429. }
  1430. len = p - (__be32 *)iov->iov_base - 1;
  1431. if (len < 0)
  1432. goto out_overflow;
  1433. switch ((n = ntohl(*p++))) {
  1434. case RPC_SUCCESS:
  1435. return p;
  1436. case RPC_PROG_UNAVAIL:
  1437. dprintk("RPC: %5u %s: program %u is unsupported by server %s\n",
  1438. task->tk_pid, __func__,
  1439. (unsigned int)task->tk_client->cl_prog,
  1440. task->tk_client->cl_server);
  1441. error = -EPFNOSUPPORT;
  1442. goto out_err;
  1443. case RPC_PROG_MISMATCH:
  1444. dprintk("RPC: %5u %s: program %u, version %u unsupported by "
  1445. "server %s\n", task->tk_pid, __func__,
  1446. (unsigned int)task->tk_client->cl_prog,
  1447. (unsigned int)task->tk_client->cl_vers,
  1448. task->tk_client->cl_server);
  1449. error = -EPROTONOSUPPORT;
  1450. goto out_err;
  1451. case RPC_PROC_UNAVAIL:
  1452. dprintk("RPC: %5u %s: proc %s unsupported by program %u, "
  1453. "version %u on server %s\n",
  1454. task->tk_pid, __func__,
  1455. rpc_proc_name(task),
  1456. task->tk_client->cl_prog,
  1457. task->tk_client->cl_vers,
  1458. task->tk_client->cl_server);
  1459. error = -EOPNOTSUPP;
  1460. goto out_err;
  1461. case RPC_GARBAGE_ARGS:
  1462. dprintk("RPC: %5u %s: server saw garbage\n",
  1463. task->tk_pid, __func__);
  1464. break; /* retry */
  1465. default:
  1466. dprintk("RPC: %5u %s: server accept status: %x\n",
  1467. task->tk_pid, __func__, n);
  1468. /* Also retry */
  1469. }
  1470. out_garbage:
  1471. task->tk_client->cl_stats->rpcgarbage++;
  1472. if (task->tk_garb_retry) {
  1473. task->tk_garb_retry--;
  1474. dprintk("RPC: %5u %s: retrying\n",
  1475. task->tk_pid, __func__);
  1476. task->tk_action = call_bind;
  1477. out_retry:
  1478. return ERR_PTR(-EAGAIN);
  1479. }
  1480. out_eio:
  1481. error = -EIO;
  1482. out_err:
  1483. rpc_exit(task, error);
  1484. dprintk("RPC: %5u %s: call failed with error %d\n", task->tk_pid,
  1485. __func__, error);
  1486. return ERR_PTR(error);
  1487. out_overflow:
  1488. dprintk("RPC: %5u %s: server reply was truncated.\n", task->tk_pid,
  1489. __func__);
  1490. goto out_garbage;
  1491. }
  1492. static int rpcproc_encode_null(void *rqstp, __be32 *data, void *obj)
  1493. {
  1494. return 0;
  1495. }
  1496. static int rpcproc_decode_null(void *rqstp, __be32 *data, void *obj)
  1497. {
  1498. return 0;
  1499. }
  1500. static struct rpc_procinfo rpcproc_null = {
  1501. .p_encode = rpcproc_encode_null,
  1502. .p_decode = rpcproc_decode_null,
  1503. };
  1504. static int rpc_ping(struct rpc_clnt *clnt, int flags)
  1505. {
  1506. struct rpc_message msg = {
  1507. .rpc_proc = &rpcproc_null,
  1508. };
  1509. int err;
  1510. msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
  1511. err = rpc_call_sync(clnt, &msg, flags);
  1512. put_rpccred(msg.rpc_cred);
  1513. return err;
  1514. }
  1515. struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
  1516. {
  1517. struct rpc_message msg = {
  1518. .rpc_proc = &rpcproc_null,
  1519. .rpc_cred = cred,
  1520. };
  1521. struct rpc_task_setup task_setup_data = {
  1522. .rpc_client = clnt,
  1523. .rpc_message = &msg,
  1524. .callback_ops = &rpc_default_ops,
  1525. .flags = flags,
  1526. };
  1527. return rpc_run_task(&task_setup_data);
  1528. }
  1529. EXPORT_SYMBOL_GPL(rpc_call_null);
  1530. #ifdef RPC_DEBUG
  1531. static void rpc_show_header(void)
  1532. {
  1533. printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
  1534. "-timeout ---ops--\n");
  1535. }
  1536. static void rpc_show_task(const struct rpc_clnt *clnt,
  1537. const struct rpc_task *task)
  1538. {
  1539. const char *rpc_waitq = "none";
  1540. char *p, action[KSYM_SYMBOL_LEN];
  1541. if (RPC_IS_QUEUED(task))
  1542. rpc_waitq = rpc_qname(task->tk_waitqueue);
  1543. /* map tk_action pointer to a function name; then trim off
  1544. * the "+0x0 [sunrpc]" */
  1545. sprint_symbol(action, (unsigned long)task->tk_action);
  1546. p = strchr(action, '+');
  1547. if (p)
  1548. *p = '\0';
  1549. printk(KERN_INFO "%5u %04x %6d %8p %8p %8ld %8p %sv%u %s a:%s q:%s\n",
  1550. task->tk_pid, task->tk_flags, task->tk_status,
  1551. clnt, task->tk_rqstp, task->tk_timeout, task->tk_ops,
  1552. clnt->cl_protname, clnt->cl_vers, rpc_proc_name(task),
  1553. action, rpc_waitq);
  1554. }
  1555. void rpc_show_tasks(void)
  1556. {
  1557. struct rpc_clnt *clnt;
  1558. struct rpc_task *task;
  1559. int header = 0;
  1560. spin_lock(&rpc_client_lock);
  1561. list_for_each_entry(clnt, &all_clients, cl_clients) {
  1562. spin_lock(&clnt->cl_lock);
  1563. list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
  1564. if (!header) {
  1565. rpc_show_header();
  1566. header++;
  1567. }
  1568. rpc_show_task(clnt, task);
  1569. }
  1570. spin_unlock(&clnt->cl_lock);
  1571. }
  1572. spin_unlock(&rpc_client_lock);
  1573. }
  1574. #endif