clnt.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387
  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/mm.h>
  27. #include <linux/slab.h>
  28. #include <linux/utsname.h>
  29. #include <linux/workqueue.h>
  30. #include <linux/sunrpc/clnt.h>
  31. #include <linux/sunrpc/rpc_pipe_fs.h>
  32. #include <linux/sunrpc/metrics.h>
  33. #define RPC_SLACK_SPACE (1024) /* total overkill */
  34. #ifdef RPC_DEBUG
  35. # define RPCDBG_FACILITY RPCDBG_CALL
  36. #endif
  37. static DECLARE_WAIT_QUEUE_HEAD(destroy_wait);
  38. static void call_start(struct rpc_task *task);
  39. static void call_reserve(struct rpc_task *task);
  40. static void call_reserveresult(struct rpc_task *task);
  41. static void call_allocate(struct rpc_task *task);
  42. static void call_encode(struct rpc_task *task);
  43. static void call_decode(struct rpc_task *task);
  44. static void call_bind(struct rpc_task *task);
  45. static void call_bind_status(struct rpc_task *task);
  46. static void call_transmit(struct rpc_task *task);
  47. static void call_status(struct rpc_task *task);
  48. static void call_transmit_status(struct rpc_task *task);
  49. static void call_refresh(struct rpc_task *task);
  50. static void call_refreshresult(struct rpc_task *task);
  51. static void call_timeout(struct rpc_task *task);
  52. static void call_connect(struct rpc_task *task);
  53. static void call_connect_status(struct rpc_task *task);
  54. static __be32 * call_header(struct rpc_task *task);
  55. static __be32 * call_verify(struct rpc_task *task);
  56. static int
  57. rpc_setup_pipedir(struct rpc_clnt *clnt, char *dir_name)
  58. {
  59. static uint32_t clntid;
  60. int error;
  61. clnt->cl_vfsmnt = ERR_PTR(-ENOENT);
  62. clnt->cl_dentry = ERR_PTR(-ENOENT);
  63. if (dir_name == NULL)
  64. return 0;
  65. clnt->cl_vfsmnt = rpc_get_mount();
  66. if (IS_ERR(clnt->cl_vfsmnt))
  67. return PTR_ERR(clnt->cl_vfsmnt);
  68. for (;;) {
  69. snprintf(clnt->cl_pathname, sizeof(clnt->cl_pathname),
  70. "%s/clnt%x", dir_name,
  71. (unsigned int)clntid++);
  72. clnt->cl_pathname[sizeof(clnt->cl_pathname) - 1] = '\0';
  73. clnt->cl_dentry = rpc_mkdir(clnt->cl_pathname, clnt);
  74. if (!IS_ERR(clnt->cl_dentry))
  75. return 0;
  76. error = PTR_ERR(clnt->cl_dentry);
  77. if (error != -EEXIST) {
  78. printk(KERN_INFO "RPC: Couldn't create pipefs entry %s, error %d\n",
  79. clnt->cl_pathname, error);
  80. rpc_put_mount();
  81. return error;
  82. }
  83. }
  84. }
  85. static struct rpc_clnt * rpc_new_client(struct rpc_xprt *xprt, char *servname, struct rpc_program *program, u32 vers, rpc_authflavor_t flavor)
  86. {
  87. struct rpc_version *version;
  88. struct rpc_clnt *clnt = NULL;
  89. struct rpc_auth *auth;
  90. int err;
  91. int len;
  92. dprintk("RPC: creating %s client for %s (xprt %p)\n",
  93. program->name, servname, xprt);
  94. err = -EINVAL;
  95. if (!xprt)
  96. goto out_no_xprt;
  97. if (vers >= program->nrvers || !(version = program->version[vers]))
  98. goto out_err;
  99. err = -ENOMEM;
  100. clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
  101. if (!clnt)
  102. goto out_err;
  103. atomic_set(&clnt->cl_users, 0);
  104. atomic_set(&clnt->cl_count, 1);
  105. clnt->cl_parent = clnt;
  106. clnt->cl_server = clnt->cl_inline_name;
  107. len = strlen(servname) + 1;
  108. if (len > sizeof(clnt->cl_inline_name)) {
  109. char *buf = kmalloc(len, GFP_KERNEL);
  110. if (buf != 0)
  111. clnt->cl_server = buf;
  112. else
  113. len = sizeof(clnt->cl_inline_name);
  114. }
  115. strlcpy(clnt->cl_server, servname, len);
  116. clnt->cl_xprt = xprt;
  117. clnt->cl_procinfo = version->procs;
  118. clnt->cl_maxproc = version->nrprocs;
  119. clnt->cl_protname = program->name;
  120. clnt->cl_prog = program->number;
  121. clnt->cl_vers = version->number;
  122. clnt->cl_stats = program->stats;
  123. clnt->cl_metrics = rpc_alloc_iostats(clnt);
  124. if (!xprt_bound(clnt->cl_xprt))
  125. clnt->cl_autobind = 1;
  126. clnt->cl_rtt = &clnt->cl_rtt_default;
  127. rpc_init_rtt(&clnt->cl_rtt_default, xprt->timeout.to_initval);
  128. err = rpc_setup_pipedir(clnt, program->pipe_dir_name);
  129. if (err < 0)
  130. goto out_no_path;
  131. auth = rpcauth_create(flavor, clnt);
  132. if (IS_ERR(auth)) {
  133. printk(KERN_INFO "RPC: Couldn't create auth handle (flavor %u)\n",
  134. flavor);
  135. err = PTR_ERR(auth);
  136. goto out_no_auth;
  137. }
  138. /* save the nodename */
  139. clnt->cl_nodelen = strlen(system_utsname.nodename);
  140. if (clnt->cl_nodelen > UNX_MAXNODENAME)
  141. clnt->cl_nodelen = UNX_MAXNODENAME;
  142. memcpy(clnt->cl_nodename, system_utsname.nodename, clnt->cl_nodelen);
  143. return clnt;
  144. out_no_auth:
  145. if (!IS_ERR(clnt->cl_dentry)) {
  146. rpc_rmdir(clnt->cl_dentry);
  147. rpc_put_mount();
  148. }
  149. out_no_path:
  150. if (clnt->cl_server != clnt->cl_inline_name)
  151. kfree(clnt->cl_server);
  152. kfree(clnt);
  153. out_err:
  154. xprt_put(xprt);
  155. out_no_xprt:
  156. return ERR_PTR(err);
  157. }
  158. /*
  159. * rpc_create - create an RPC client and transport with one call
  160. * @args: rpc_clnt create argument structure
  161. *
  162. * Creates and initializes an RPC transport and an RPC client.
  163. *
  164. * It can ping the server in order to determine if it is up, and to see if
  165. * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
  166. * this behavior so asynchronous tasks can also use rpc_create.
  167. */
  168. struct rpc_clnt *rpc_create(struct rpc_create_args *args)
  169. {
  170. struct rpc_xprt *xprt;
  171. struct rpc_clnt *clnt;
  172. xprt = xprt_create_transport(args->protocol, args->address,
  173. args->addrsize, args->timeout);
  174. if (IS_ERR(xprt))
  175. return (struct rpc_clnt *)xprt;
  176. /*
  177. * By default, kernel RPC client connects from a reserved port.
  178. * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
  179. * but it is always enabled for rpciod, which handles the connect
  180. * operation.
  181. */
  182. xprt->resvport = 1;
  183. if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
  184. xprt->resvport = 0;
  185. dprintk("RPC: creating %s client for %s (xprt %p)\n",
  186. args->program->name, args->servername, xprt);
  187. clnt = rpc_new_client(xprt, args->servername, args->program,
  188. args->version, args->authflavor);
  189. if (IS_ERR(clnt))
  190. return clnt;
  191. if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
  192. int err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
  193. if (err != 0) {
  194. rpc_shutdown_client(clnt);
  195. return ERR_PTR(err);
  196. }
  197. }
  198. clnt->cl_softrtry = 1;
  199. if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
  200. clnt->cl_softrtry = 0;
  201. if (args->flags & RPC_CLNT_CREATE_INTR)
  202. clnt->cl_intr = 1;
  203. if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
  204. clnt->cl_autobind = 1;
  205. if (args->flags & RPC_CLNT_CREATE_ONESHOT)
  206. clnt->cl_oneshot = 1;
  207. return clnt;
  208. }
  209. EXPORT_SYMBOL_GPL(rpc_create);
  210. /*
  211. * This function clones the RPC client structure. It allows us to share the
  212. * same transport while varying parameters such as the authentication
  213. * flavour.
  214. */
  215. struct rpc_clnt *
  216. rpc_clone_client(struct rpc_clnt *clnt)
  217. {
  218. struct rpc_clnt *new;
  219. new = kmalloc(sizeof(*new), GFP_KERNEL);
  220. if (!new)
  221. goto out_no_clnt;
  222. memcpy(new, clnt, sizeof(*new));
  223. atomic_set(&new->cl_count, 1);
  224. atomic_set(&new->cl_users, 0);
  225. new->cl_parent = clnt;
  226. atomic_inc(&clnt->cl_count);
  227. new->cl_xprt = xprt_get(clnt->cl_xprt);
  228. /* Turn off autobind on clones */
  229. new->cl_autobind = 0;
  230. new->cl_oneshot = 0;
  231. new->cl_dead = 0;
  232. if (!IS_ERR(new->cl_dentry))
  233. dget(new->cl_dentry);
  234. rpc_init_rtt(&new->cl_rtt_default, clnt->cl_xprt->timeout.to_initval);
  235. if (new->cl_auth)
  236. atomic_inc(&new->cl_auth->au_count);
  237. new->cl_metrics = rpc_alloc_iostats(clnt);
  238. return new;
  239. out_no_clnt:
  240. printk(KERN_INFO "RPC: out of memory in %s\n", __FUNCTION__);
  241. return ERR_PTR(-ENOMEM);
  242. }
  243. /*
  244. * Properly shut down an RPC client, terminating all outstanding
  245. * requests. Note that we must be certain that cl_oneshot and
  246. * cl_dead are cleared, or else the client would be destroyed
  247. * when the last task releases it.
  248. */
  249. int
  250. rpc_shutdown_client(struct rpc_clnt *clnt)
  251. {
  252. dprintk("RPC: shutting down %s client for %s, tasks=%d\n",
  253. clnt->cl_protname, clnt->cl_server,
  254. atomic_read(&clnt->cl_users));
  255. while (atomic_read(&clnt->cl_users) > 0) {
  256. /* Don't let rpc_release_client destroy us */
  257. clnt->cl_oneshot = 0;
  258. clnt->cl_dead = 0;
  259. rpc_killall_tasks(clnt);
  260. wait_event_timeout(destroy_wait,
  261. !atomic_read(&clnt->cl_users), 1*HZ);
  262. }
  263. if (atomic_read(&clnt->cl_users) < 0) {
  264. printk(KERN_ERR "RPC: rpc_shutdown_client clnt %p tasks=%d\n",
  265. clnt, atomic_read(&clnt->cl_users));
  266. #ifdef RPC_DEBUG
  267. rpc_show_tasks();
  268. #endif
  269. BUG();
  270. }
  271. return rpc_destroy_client(clnt);
  272. }
  273. /*
  274. * Delete an RPC client
  275. */
  276. int
  277. rpc_destroy_client(struct rpc_clnt *clnt)
  278. {
  279. if (!atomic_dec_and_test(&clnt->cl_count))
  280. return 1;
  281. BUG_ON(atomic_read(&clnt->cl_users) != 0);
  282. dprintk("RPC: destroying %s client for %s\n",
  283. clnt->cl_protname, clnt->cl_server);
  284. if (clnt->cl_auth) {
  285. rpcauth_destroy(clnt->cl_auth);
  286. clnt->cl_auth = NULL;
  287. }
  288. if (clnt->cl_parent != clnt) {
  289. if (!IS_ERR(clnt->cl_dentry))
  290. dput(clnt->cl_dentry);
  291. rpc_destroy_client(clnt->cl_parent);
  292. goto out_free;
  293. }
  294. if (!IS_ERR(clnt->cl_dentry)) {
  295. rpc_rmdir(clnt->cl_dentry);
  296. rpc_put_mount();
  297. }
  298. if (clnt->cl_server != clnt->cl_inline_name)
  299. kfree(clnt->cl_server);
  300. out_free:
  301. rpc_free_iostats(clnt->cl_metrics);
  302. clnt->cl_metrics = NULL;
  303. xprt_put(clnt->cl_xprt);
  304. kfree(clnt);
  305. return 0;
  306. }
  307. /*
  308. * Release an RPC client
  309. */
  310. void
  311. rpc_release_client(struct rpc_clnt *clnt)
  312. {
  313. dprintk("RPC: rpc_release_client(%p, %d)\n",
  314. clnt, atomic_read(&clnt->cl_users));
  315. if (!atomic_dec_and_test(&clnt->cl_users))
  316. return;
  317. wake_up(&destroy_wait);
  318. if (clnt->cl_oneshot || clnt->cl_dead)
  319. rpc_destroy_client(clnt);
  320. }
  321. /**
  322. * rpc_bind_new_program - bind a new RPC program to an existing client
  323. * @old - old rpc_client
  324. * @program - rpc program to set
  325. * @vers - rpc program version
  326. *
  327. * Clones the rpc client and sets up a new RPC program. This is mainly
  328. * of use for enabling different RPC programs to share the same transport.
  329. * The Sun NFSv2/v3 ACL protocol can do this.
  330. */
  331. struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
  332. struct rpc_program *program,
  333. int vers)
  334. {
  335. struct rpc_clnt *clnt;
  336. struct rpc_version *version;
  337. int err;
  338. BUG_ON(vers >= program->nrvers || !program->version[vers]);
  339. version = program->version[vers];
  340. clnt = rpc_clone_client(old);
  341. if (IS_ERR(clnt))
  342. goto out;
  343. clnt->cl_procinfo = version->procs;
  344. clnt->cl_maxproc = version->nrprocs;
  345. clnt->cl_protname = program->name;
  346. clnt->cl_prog = program->number;
  347. clnt->cl_vers = version->number;
  348. clnt->cl_stats = program->stats;
  349. err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
  350. if (err != 0) {
  351. rpc_shutdown_client(clnt);
  352. clnt = ERR_PTR(err);
  353. }
  354. out:
  355. return clnt;
  356. }
  357. /*
  358. * Default callback for async RPC calls
  359. */
  360. static void
  361. rpc_default_callback(struct rpc_task *task, void *data)
  362. {
  363. }
  364. static const struct rpc_call_ops rpc_default_ops = {
  365. .rpc_call_done = rpc_default_callback,
  366. };
  367. /*
  368. * Export the signal mask handling for synchronous code that
  369. * sleeps on RPC calls
  370. */
  371. #define RPC_INTR_SIGNALS (sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTERM))
  372. static void rpc_save_sigmask(sigset_t *oldset, int intr)
  373. {
  374. unsigned long sigallow = sigmask(SIGKILL);
  375. sigset_t sigmask;
  376. /* Block all signals except those listed in sigallow */
  377. if (intr)
  378. sigallow |= RPC_INTR_SIGNALS;
  379. siginitsetinv(&sigmask, sigallow);
  380. sigprocmask(SIG_BLOCK, &sigmask, oldset);
  381. }
  382. static inline void rpc_task_sigmask(struct rpc_task *task, sigset_t *oldset)
  383. {
  384. rpc_save_sigmask(oldset, !RPC_TASK_UNINTERRUPTIBLE(task));
  385. }
  386. static inline void rpc_restore_sigmask(sigset_t *oldset)
  387. {
  388. sigprocmask(SIG_SETMASK, oldset, NULL);
  389. }
  390. void rpc_clnt_sigmask(struct rpc_clnt *clnt, sigset_t *oldset)
  391. {
  392. rpc_save_sigmask(oldset, clnt->cl_intr);
  393. }
  394. void rpc_clnt_sigunmask(struct rpc_clnt *clnt, sigset_t *oldset)
  395. {
  396. rpc_restore_sigmask(oldset);
  397. }
  398. /*
  399. * New rpc_call implementation
  400. */
  401. int rpc_call_sync(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
  402. {
  403. struct rpc_task *task;
  404. sigset_t oldset;
  405. int status;
  406. /* If this client is slain all further I/O fails */
  407. if (clnt->cl_dead)
  408. return -EIO;
  409. BUG_ON(flags & RPC_TASK_ASYNC);
  410. status = -ENOMEM;
  411. task = rpc_new_task(clnt, flags, &rpc_default_ops, NULL);
  412. if (task == NULL)
  413. goto out;
  414. /* Mask signals on RPC calls _and_ GSS_AUTH upcalls */
  415. rpc_task_sigmask(task, &oldset);
  416. rpc_call_setup(task, msg, 0);
  417. /* Set up the call info struct and execute the task */
  418. status = task->tk_status;
  419. if (status == 0) {
  420. atomic_inc(&task->tk_count);
  421. status = rpc_execute(task);
  422. if (status == 0)
  423. status = task->tk_status;
  424. }
  425. rpc_restore_sigmask(&oldset);
  426. rpc_release_task(task);
  427. out:
  428. return status;
  429. }
  430. /*
  431. * New rpc_call implementation
  432. */
  433. int
  434. rpc_call_async(struct rpc_clnt *clnt, struct rpc_message *msg, int flags,
  435. const struct rpc_call_ops *tk_ops, void *data)
  436. {
  437. struct rpc_task *task;
  438. sigset_t oldset;
  439. int status;
  440. /* If this client is slain all further I/O fails */
  441. status = -EIO;
  442. if (clnt->cl_dead)
  443. goto out_release;
  444. flags |= RPC_TASK_ASYNC;
  445. /* Create/initialize a new RPC task */
  446. status = -ENOMEM;
  447. if (!(task = rpc_new_task(clnt, flags, tk_ops, data)))
  448. goto out_release;
  449. /* Mask signals on GSS_AUTH upcalls */
  450. rpc_task_sigmask(task, &oldset);
  451. rpc_call_setup(task, msg, 0);
  452. /* Set up the call info struct and execute the task */
  453. status = task->tk_status;
  454. if (status == 0)
  455. rpc_execute(task);
  456. else
  457. rpc_release_task(task);
  458. rpc_restore_sigmask(&oldset);
  459. return status;
  460. out_release:
  461. if (tk_ops->rpc_release != NULL)
  462. tk_ops->rpc_release(data);
  463. return status;
  464. }
  465. void
  466. rpc_call_setup(struct rpc_task *task, struct rpc_message *msg, int flags)
  467. {
  468. task->tk_msg = *msg;
  469. task->tk_flags |= flags;
  470. /* Bind the user cred */
  471. if (task->tk_msg.rpc_cred != NULL)
  472. rpcauth_holdcred(task);
  473. else
  474. rpcauth_bindcred(task);
  475. if (task->tk_status == 0)
  476. task->tk_action = call_start;
  477. else
  478. task->tk_action = rpc_exit_task;
  479. }
  480. /**
  481. * rpc_peeraddr - extract remote peer address from clnt's xprt
  482. * @clnt: RPC client structure
  483. * @buf: target buffer
  484. * @size: length of target buffer
  485. *
  486. * Returns the number of bytes that are actually in the stored address.
  487. */
  488. size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
  489. {
  490. size_t bytes;
  491. struct rpc_xprt *xprt = clnt->cl_xprt;
  492. bytes = sizeof(xprt->addr);
  493. if (bytes > bufsize)
  494. bytes = bufsize;
  495. memcpy(buf, &clnt->cl_xprt->addr, bytes);
  496. return xprt->addrlen;
  497. }
  498. EXPORT_SYMBOL_GPL(rpc_peeraddr);
  499. /**
  500. * rpc_peeraddr2str - return remote peer address in printable format
  501. * @clnt: RPC client structure
  502. * @format: address format
  503. *
  504. */
  505. char *rpc_peeraddr2str(struct rpc_clnt *clnt, enum rpc_display_format_t format)
  506. {
  507. struct rpc_xprt *xprt = clnt->cl_xprt;
  508. return xprt->ops->print_addr(xprt, format);
  509. }
  510. EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
  511. void
  512. rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
  513. {
  514. struct rpc_xprt *xprt = clnt->cl_xprt;
  515. if (xprt->ops->set_buffer_size)
  516. xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
  517. }
  518. /*
  519. * Return size of largest payload RPC client can support, in bytes
  520. *
  521. * For stream transports, this is one RPC record fragment (see RFC
  522. * 1831), as we don't support multi-record requests yet. For datagram
  523. * transports, this is the size of an IP packet minus the IP, UDP, and
  524. * RPC header sizes.
  525. */
  526. size_t rpc_max_payload(struct rpc_clnt *clnt)
  527. {
  528. return clnt->cl_xprt->max_payload;
  529. }
  530. EXPORT_SYMBOL_GPL(rpc_max_payload);
  531. /**
  532. * rpc_force_rebind - force transport to check that remote port is unchanged
  533. * @clnt: client to rebind
  534. *
  535. */
  536. void rpc_force_rebind(struct rpc_clnt *clnt)
  537. {
  538. if (clnt->cl_autobind)
  539. xprt_clear_bound(clnt->cl_xprt);
  540. }
  541. EXPORT_SYMBOL_GPL(rpc_force_rebind);
  542. /*
  543. * Restart an (async) RPC call. Usually called from within the
  544. * exit handler.
  545. */
  546. void
  547. rpc_restart_call(struct rpc_task *task)
  548. {
  549. if (RPC_ASSASSINATED(task))
  550. return;
  551. task->tk_action = call_start;
  552. }
  553. /*
  554. * 0. Initial state
  555. *
  556. * Other FSM states can be visited zero or more times, but
  557. * this state is visited exactly once for each RPC.
  558. */
  559. static void
  560. call_start(struct rpc_task *task)
  561. {
  562. struct rpc_clnt *clnt = task->tk_client;
  563. dprintk("RPC: %4d call_start %s%d proc %d (%s)\n", task->tk_pid,
  564. clnt->cl_protname, clnt->cl_vers, task->tk_msg.rpc_proc->p_proc,
  565. (RPC_IS_ASYNC(task) ? "async" : "sync"));
  566. /* Increment call count */
  567. task->tk_msg.rpc_proc->p_count++;
  568. clnt->cl_stats->rpccnt++;
  569. task->tk_action = call_reserve;
  570. }
  571. /*
  572. * 1. Reserve an RPC call slot
  573. */
  574. static void
  575. call_reserve(struct rpc_task *task)
  576. {
  577. dprintk("RPC: %4d call_reserve\n", task->tk_pid);
  578. if (!rpcauth_uptodatecred(task)) {
  579. task->tk_action = call_refresh;
  580. return;
  581. }
  582. task->tk_status = 0;
  583. task->tk_action = call_reserveresult;
  584. xprt_reserve(task);
  585. }
  586. /*
  587. * 1b. Grok the result of xprt_reserve()
  588. */
  589. static void
  590. call_reserveresult(struct rpc_task *task)
  591. {
  592. int status = task->tk_status;
  593. dprintk("RPC: %4d call_reserveresult (status %d)\n",
  594. task->tk_pid, task->tk_status);
  595. /*
  596. * After a call to xprt_reserve(), we must have either
  597. * a request slot or else an error status.
  598. */
  599. task->tk_status = 0;
  600. if (status >= 0) {
  601. if (task->tk_rqstp) {
  602. task->tk_action = call_allocate;
  603. return;
  604. }
  605. printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
  606. __FUNCTION__, status);
  607. rpc_exit(task, -EIO);
  608. return;
  609. }
  610. /*
  611. * Even though there was an error, we may have acquired
  612. * a request slot somehow. Make sure not to leak it.
  613. */
  614. if (task->tk_rqstp) {
  615. printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
  616. __FUNCTION__, status);
  617. xprt_release(task);
  618. }
  619. switch (status) {
  620. case -EAGAIN: /* woken up; retry */
  621. task->tk_action = call_reserve;
  622. return;
  623. case -EIO: /* probably a shutdown */
  624. break;
  625. default:
  626. printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
  627. __FUNCTION__, status);
  628. break;
  629. }
  630. rpc_exit(task, status);
  631. }
  632. /*
  633. * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
  634. * (Note: buffer memory is freed in xprt_release).
  635. */
  636. static void
  637. call_allocate(struct rpc_task *task)
  638. {
  639. struct rpc_rqst *req = task->tk_rqstp;
  640. struct rpc_xprt *xprt = task->tk_xprt;
  641. unsigned int bufsiz;
  642. dprintk("RPC: %4d call_allocate (status %d)\n",
  643. task->tk_pid, task->tk_status);
  644. task->tk_action = call_bind;
  645. if (req->rq_buffer)
  646. return;
  647. /* FIXME: compute buffer requirements more exactly using
  648. * auth->au_wslack */
  649. bufsiz = task->tk_msg.rpc_proc->p_bufsiz + RPC_SLACK_SPACE;
  650. if (xprt->ops->buf_alloc(task, bufsiz << 1) != NULL)
  651. return;
  652. printk(KERN_INFO "RPC: buffer allocation failed for task %p\n", task);
  653. if (RPC_IS_ASYNC(task) || !signalled()) {
  654. xprt_release(task);
  655. task->tk_action = call_reserve;
  656. rpc_delay(task, HZ>>4);
  657. return;
  658. }
  659. rpc_exit(task, -ERESTARTSYS);
  660. }
  661. static inline int
  662. rpc_task_need_encode(struct rpc_task *task)
  663. {
  664. return task->tk_rqstp->rq_snd_buf.len == 0;
  665. }
  666. static inline void
  667. rpc_task_force_reencode(struct rpc_task *task)
  668. {
  669. task->tk_rqstp->rq_snd_buf.len = 0;
  670. }
  671. /*
  672. * 3. Encode arguments of an RPC call
  673. */
  674. static void
  675. call_encode(struct rpc_task *task)
  676. {
  677. struct rpc_rqst *req = task->tk_rqstp;
  678. struct xdr_buf *sndbuf = &req->rq_snd_buf;
  679. struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
  680. unsigned int bufsiz;
  681. kxdrproc_t encode;
  682. __be32 *p;
  683. dprintk("RPC: %4d call_encode (status %d)\n",
  684. task->tk_pid, task->tk_status);
  685. /* Default buffer setup */
  686. bufsiz = req->rq_bufsize >> 1;
  687. sndbuf->head[0].iov_base = (void *)req->rq_buffer;
  688. sndbuf->head[0].iov_len = bufsiz;
  689. sndbuf->tail[0].iov_len = 0;
  690. sndbuf->page_len = 0;
  691. sndbuf->len = 0;
  692. sndbuf->buflen = bufsiz;
  693. rcvbuf->head[0].iov_base = (void *)((char *)req->rq_buffer + bufsiz);
  694. rcvbuf->head[0].iov_len = bufsiz;
  695. rcvbuf->tail[0].iov_len = 0;
  696. rcvbuf->page_len = 0;
  697. rcvbuf->len = 0;
  698. rcvbuf->buflen = bufsiz;
  699. /* Encode header and provided arguments */
  700. encode = task->tk_msg.rpc_proc->p_encode;
  701. if (!(p = call_header(task))) {
  702. printk(KERN_INFO "RPC: call_header failed, exit EIO\n");
  703. rpc_exit(task, -EIO);
  704. return;
  705. }
  706. if (encode == NULL)
  707. return;
  708. task->tk_status = rpcauth_wrap_req(task, encode, req, p,
  709. task->tk_msg.rpc_argp);
  710. if (task->tk_status == -ENOMEM) {
  711. /* XXX: Is this sane? */
  712. rpc_delay(task, 3*HZ);
  713. task->tk_status = -EAGAIN;
  714. }
  715. }
  716. /*
  717. * 4. Get the server port number if not yet set
  718. */
  719. static void
  720. call_bind(struct rpc_task *task)
  721. {
  722. struct rpc_xprt *xprt = task->tk_xprt;
  723. dprintk("RPC: %4d call_bind (status %d)\n",
  724. task->tk_pid, task->tk_status);
  725. task->tk_action = call_connect;
  726. if (!xprt_bound(xprt)) {
  727. task->tk_action = call_bind_status;
  728. task->tk_timeout = xprt->bind_timeout;
  729. xprt->ops->rpcbind(task);
  730. }
  731. }
  732. /*
  733. * 4a. Sort out bind result
  734. */
  735. static void
  736. call_bind_status(struct rpc_task *task)
  737. {
  738. int status = -EACCES;
  739. if (task->tk_status >= 0) {
  740. dprintk("RPC: %4d call_bind_status (status %d)\n",
  741. task->tk_pid, task->tk_status);
  742. task->tk_status = 0;
  743. task->tk_action = call_connect;
  744. return;
  745. }
  746. switch (task->tk_status) {
  747. case -EACCES:
  748. dprintk("RPC: %4d remote rpcbind: RPC program/version unavailable\n",
  749. task->tk_pid);
  750. rpc_delay(task, 3*HZ);
  751. goto retry_timeout;
  752. case -ETIMEDOUT:
  753. dprintk("RPC: %4d rpcbind request timed out\n",
  754. task->tk_pid);
  755. goto retry_timeout;
  756. case -EPFNOSUPPORT:
  757. dprintk("RPC: %4d remote rpcbind service unavailable\n",
  758. task->tk_pid);
  759. break;
  760. case -EPROTONOSUPPORT:
  761. dprintk("RPC: %4d remote rpcbind version 2 unavailable\n",
  762. task->tk_pid);
  763. break;
  764. default:
  765. dprintk("RPC: %4d unrecognized rpcbind error (%d)\n",
  766. task->tk_pid, -task->tk_status);
  767. status = -EIO;
  768. }
  769. rpc_exit(task, status);
  770. return;
  771. retry_timeout:
  772. task->tk_action = call_timeout;
  773. }
  774. /*
  775. * 4b. Connect to the RPC server
  776. */
  777. static void
  778. call_connect(struct rpc_task *task)
  779. {
  780. struct rpc_xprt *xprt = task->tk_xprt;
  781. dprintk("RPC: %4d call_connect xprt %p %s connected\n",
  782. task->tk_pid, xprt,
  783. (xprt_connected(xprt) ? "is" : "is not"));
  784. task->tk_action = call_transmit;
  785. if (!xprt_connected(xprt)) {
  786. task->tk_action = call_connect_status;
  787. if (task->tk_status < 0)
  788. return;
  789. xprt_connect(task);
  790. }
  791. }
  792. /*
  793. * 4c. Sort out connect result
  794. */
  795. static void
  796. call_connect_status(struct rpc_task *task)
  797. {
  798. struct rpc_clnt *clnt = task->tk_client;
  799. int status = task->tk_status;
  800. dprintk("RPC: %5u call_connect_status (status %d)\n",
  801. task->tk_pid, task->tk_status);
  802. task->tk_status = 0;
  803. if (status >= 0) {
  804. clnt->cl_stats->netreconn++;
  805. task->tk_action = call_transmit;
  806. return;
  807. }
  808. /* Something failed: remote service port may have changed */
  809. rpc_force_rebind(clnt);
  810. switch (status) {
  811. case -ENOTCONN:
  812. case -EAGAIN:
  813. task->tk_action = call_bind;
  814. if (!RPC_IS_SOFT(task))
  815. return;
  816. /* if soft mounted, test if we've timed out */
  817. case -ETIMEDOUT:
  818. task->tk_action = call_timeout;
  819. return;
  820. }
  821. rpc_exit(task, -EIO);
  822. }
  823. /*
  824. * 5. Transmit the RPC request, and wait for reply
  825. */
  826. static void
  827. call_transmit(struct rpc_task *task)
  828. {
  829. dprintk("RPC: %4d call_transmit (status %d)\n",
  830. task->tk_pid, task->tk_status);
  831. task->tk_action = call_status;
  832. if (task->tk_status < 0)
  833. return;
  834. task->tk_status = xprt_prepare_transmit(task);
  835. if (task->tk_status != 0)
  836. return;
  837. task->tk_action = call_transmit_status;
  838. /* Encode here so that rpcsec_gss can use correct sequence number. */
  839. if (rpc_task_need_encode(task)) {
  840. BUG_ON(task->tk_rqstp->rq_bytes_sent != 0);
  841. call_encode(task);
  842. /* Did the encode result in an error condition? */
  843. if (task->tk_status != 0)
  844. return;
  845. }
  846. xprt_transmit(task);
  847. if (task->tk_status < 0)
  848. return;
  849. /*
  850. * On success, ensure that we call xprt_end_transmit() before sleeping
  851. * in order to allow access to the socket to other RPC requests.
  852. */
  853. call_transmit_status(task);
  854. if (task->tk_msg.rpc_proc->p_decode != NULL)
  855. return;
  856. task->tk_action = rpc_exit_task;
  857. rpc_wake_up_task(task);
  858. }
  859. /*
  860. * 5a. Handle cleanup after a transmission
  861. */
  862. static void
  863. call_transmit_status(struct rpc_task *task)
  864. {
  865. task->tk_action = call_status;
  866. /*
  867. * Special case: if we've been waiting on the socket's write_space()
  868. * callback, then don't call xprt_end_transmit().
  869. */
  870. if (task->tk_status == -EAGAIN)
  871. return;
  872. xprt_end_transmit(task);
  873. rpc_task_force_reencode(task);
  874. }
  875. /*
  876. * 6. Sort out the RPC call status
  877. */
  878. static void
  879. call_status(struct rpc_task *task)
  880. {
  881. struct rpc_clnt *clnt = task->tk_client;
  882. struct rpc_rqst *req = task->tk_rqstp;
  883. int status;
  884. if (req->rq_received > 0 && !req->rq_bytes_sent)
  885. task->tk_status = req->rq_received;
  886. dprintk("RPC: %4d call_status (status %d)\n",
  887. task->tk_pid, task->tk_status);
  888. status = task->tk_status;
  889. if (status >= 0) {
  890. task->tk_action = call_decode;
  891. return;
  892. }
  893. task->tk_status = 0;
  894. switch(status) {
  895. case -EHOSTDOWN:
  896. case -EHOSTUNREACH:
  897. case -ENETUNREACH:
  898. /*
  899. * Delay any retries for 3 seconds, then handle as if it
  900. * were a timeout.
  901. */
  902. rpc_delay(task, 3*HZ);
  903. case -ETIMEDOUT:
  904. task->tk_action = call_timeout;
  905. break;
  906. case -ECONNREFUSED:
  907. case -ENOTCONN:
  908. rpc_force_rebind(clnt);
  909. task->tk_action = call_bind;
  910. break;
  911. case -EAGAIN:
  912. task->tk_action = call_transmit;
  913. break;
  914. case -EIO:
  915. /* shutdown or soft timeout */
  916. rpc_exit(task, status);
  917. break;
  918. default:
  919. printk("%s: RPC call returned error %d\n",
  920. clnt->cl_protname, -status);
  921. rpc_exit(task, status);
  922. }
  923. }
  924. /*
  925. * 6a. Handle RPC timeout
  926. * We do not release the request slot, so we keep using the
  927. * same XID for all retransmits.
  928. */
  929. static void
  930. call_timeout(struct rpc_task *task)
  931. {
  932. struct rpc_clnt *clnt = task->tk_client;
  933. if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
  934. dprintk("RPC: %4d call_timeout (minor)\n", task->tk_pid);
  935. goto retry;
  936. }
  937. dprintk("RPC: %4d call_timeout (major)\n", task->tk_pid);
  938. task->tk_timeouts++;
  939. if (RPC_IS_SOFT(task)) {
  940. printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
  941. clnt->cl_protname, clnt->cl_server);
  942. rpc_exit(task, -EIO);
  943. return;
  944. }
  945. if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
  946. task->tk_flags |= RPC_CALL_MAJORSEEN;
  947. printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
  948. clnt->cl_protname, clnt->cl_server);
  949. }
  950. rpc_force_rebind(clnt);
  951. retry:
  952. clnt->cl_stats->rpcretrans++;
  953. task->tk_action = call_bind;
  954. task->tk_status = 0;
  955. }
  956. /*
  957. * 7. Decode the RPC reply
  958. */
  959. static void
  960. call_decode(struct rpc_task *task)
  961. {
  962. struct rpc_clnt *clnt = task->tk_client;
  963. struct rpc_rqst *req = task->tk_rqstp;
  964. kxdrproc_t decode = task->tk_msg.rpc_proc->p_decode;
  965. __be32 *p;
  966. dprintk("RPC: %4d call_decode (status %d)\n",
  967. task->tk_pid, task->tk_status);
  968. if (task->tk_flags & RPC_CALL_MAJORSEEN) {
  969. printk(KERN_NOTICE "%s: server %s OK\n",
  970. clnt->cl_protname, clnt->cl_server);
  971. task->tk_flags &= ~RPC_CALL_MAJORSEEN;
  972. }
  973. if (task->tk_status < 12) {
  974. if (!RPC_IS_SOFT(task)) {
  975. task->tk_action = call_bind;
  976. clnt->cl_stats->rpcretrans++;
  977. goto out_retry;
  978. }
  979. dprintk("%s: too small RPC reply size (%d bytes)\n",
  980. clnt->cl_protname, task->tk_status);
  981. task->tk_action = call_timeout;
  982. goto out_retry;
  983. }
  984. /*
  985. * Ensure that we see all writes made by xprt_complete_rqst()
  986. * before it changed req->rq_received.
  987. */
  988. smp_rmb();
  989. req->rq_rcv_buf.len = req->rq_private_buf.len;
  990. /* Check that the softirq receive buffer is valid */
  991. WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
  992. sizeof(req->rq_rcv_buf)) != 0);
  993. /* Verify the RPC header */
  994. p = call_verify(task);
  995. if (IS_ERR(p)) {
  996. if (p == ERR_PTR(-EAGAIN))
  997. goto out_retry;
  998. return;
  999. }
  1000. task->tk_action = rpc_exit_task;
  1001. if (decode)
  1002. task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
  1003. task->tk_msg.rpc_resp);
  1004. dprintk("RPC: %4d call_decode result %d\n", task->tk_pid,
  1005. task->tk_status);
  1006. return;
  1007. out_retry:
  1008. req->rq_received = req->rq_private_buf.len = 0;
  1009. task->tk_status = 0;
  1010. }
  1011. /*
  1012. * 8. Refresh the credentials if rejected by the server
  1013. */
  1014. static void
  1015. call_refresh(struct rpc_task *task)
  1016. {
  1017. dprintk("RPC: %4d call_refresh\n", task->tk_pid);
  1018. xprt_release(task); /* Must do to obtain new XID */
  1019. task->tk_action = call_refreshresult;
  1020. task->tk_status = 0;
  1021. task->tk_client->cl_stats->rpcauthrefresh++;
  1022. rpcauth_refreshcred(task);
  1023. }
  1024. /*
  1025. * 8a. Process the results of a credential refresh
  1026. */
  1027. static void
  1028. call_refreshresult(struct rpc_task *task)
  1029. {
  1030. int status = task->tk_status;
  1031. dprintk("RPC: %4d call_refreshresult (status %d)\n",
  1032. task->tk_pid, task->tk_status);
  1033. task->tk_status = 0;
  1034. task->tk_action = call_reserve;
  1035. if (status >= 0 && rpcauth_uptodatecred(task))
  1036. return;
  1037. if (status == -EACCES) {
  1038. rpc_exit(task, -EACCES);
  1039. return;
  1040. }
  1041. task->tk_action = call_refresh;
  1042. if (status != -ETIMEDOUT)
  1043. rpc_delay(task, 3*HZ);
  1044. return;
  1045. }
  1046. /*
  1047. * Call header serialization
  1048. */
  1049. static __be32 *
  1050. call_header(struct rpc_task *task)
  1051. {
  1052. struct rpc_clnt *clnt = task->tk_client;
  1053. struct rpc_rqst *req = task->tk_rqstp;
  1054. __be32 *p = req->rq_svec[0].iov_base;
  1055. /* FIXME: check buffer size? */
  1056. p = xprt_skip_transport_header(task->tk_xprt, p);
  1057. *p++ = req->rq_xid; /* XID */
  1058. *p++ = htonl(RPC_CALL); /* CALL */
  1059. *p++ = htonl(RPC_VERSION); /* RPC version */
  1060. *p++ = htonl(clnt->cl_prog); /* program number */
  1061. *p++ = htonl(clnt->cl_vers); /* program version */
  1062. *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
  1063. p = rpcauth_marshcred(task, p);
  1064. req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
  1065. return p;
  1066. }
  1067. /*
  1068. * Reply header verification
  1069. */
  1070. static __be32 *
  1071. call_verify(struct rpc_task *task)
  1072. {
  1073. struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
  1074. int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
  1075. __be32 *p = iov->iov_base;
  1076. u32 n;
  1077. int error = -EACCES;
  1078. if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
  1079. /* RFC-1014 says that the representation of XDR data must be a
  1080. * multiple of four bytes
  1081. * - if it isn't pointer subtraction in the NFS client may give
  1082. * undefined results
  1083. */
  1084. printk(KERN_WARNING
  1085. "call_verify: XDR representation not a multiple of"
  1086. " 4 bytes: 0x%x\n", task->tk_rqstp->rq_rcv_buf.len);
  1087. goto out_eio;
  1088. }
  1089. if ((len -= 3) < 0)
  1090. goto out_overflow;
  1091. p += 1; /* skip XID */
  1092. if ((n = ntohl(*p++)) != RPC_REPLY) {
  1093. printk(KERN_WARNING "call_verify: not an RPC reply: %x\n", n);
  1094. goto out_garbage;
  1095. }
  1096. if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
  1097. if (--len < 0)
  1098. goto out_overflow;
  1099. switch ((n = ntohl(*p++))) {
  1100. case RPC_AUTH_ERROR:
  1101. break;
  1102. case RPC_MISMATCH:
  1103. dprintk("%s: RPC call version mismatch!\n", __FUNCTION__);
  1104. error = -EPROTONOSUPPORT;
  1105. goto out_err;
  1106. default:
  1107. dprintk("%s: RPC call rejected, unknown error: %x\n", __FUNCTION__, n);
  1108. goto out_eio;
  1109. }
  1110. if (--len < 0)
  1111. goto out_overflow;
  1112. switch ((n = ntohl(*p++))) {
  1113. case RPC_AUTH_REJECTEDCRED:
  1114. case RPC_AUTH_REJECTEDVERF:
  1115. case RPCSEC_GSS_CREDPROBLEM:
  1116. case RPCSEC_GSS_CTXPROBLEM:
  1117. if (!task->tk_cred_retry)
  1118. break;
  1119. task->tk_cred_retry--;
  1120. dprintk("RPC: %4d call_verify: retry stale creds\n",
  1121. task->tk_pid);
  1122. rpcauth_invalcred(task);
  1123. task->tk_action = call_refresh;
  1124. goto out_retry;
  1125. case RPC_AUTH_BADCRED:
  1126. case RPC_AUTH_BADVERF:
  1127. /* possibly garbled cred/verf? */
  1128. if (!task->tk_garb_retry)
  1129. break;
  1130. task->tk_garb_retry--;
  1131. dprintk("RPC: %4d call_verify: retry garbled creds\n",
  1132. task->tk_pid);
  1133. task->tk_action = call_bind;
  1134. goto out_retry;
  1135. case RPC_AUTH_TOOWEAK:
  1136. printk(KERN_NOTICE "call_verify: server %s requires stronger "
  1137. "authentication.\n", task->tk_client->cl_server);
  1138. break;
  1139. default:
  1140. printk(KERN_WARNING "call_verify: unknown auth error: %x\n", n);
  1141. error = -EIO;
  1142. }
  1143. dprintk("RPC: %4d call_verify: call rejected %d\n",
  1144. task->tk_pid, n);
  1145. goto out_err;
  1146. }
  1147. if (!(p = rpcauth_checkverf(task, p))) {
  1148. printk(KERN_WARNING "call_verify: auth check failed\n");
  1149. goto out_garbage; /* bad verifier, retry */
  1150. }
  1151. len = p - (__be32 *)iov->iov_base - 1;
  1152. if (len < 0)
  1153. goto out_overflow;
  1154. switch ((n = ntohl(*p++))) {
  1155. case RPC_SUCCESS:
  1156. return p;
  1157. case RPC_PROG_UNAVAIL:
  1158. dprintk("RPC: call_verify: program %u is unsupported by server %s\n",
  1159. (unsigned int)task->tk_client->cl_prog,
  1160. task->tk_client->cl_server);
  1161. error = -EPFNOSUPPORT;
  1162. goto out_err;
  1163. case RPC_PROG_MISMATCH:
  1164. dprintk("RPC: call_verify: program %u, version %u unsupported by server %s\n",
  1165. (unsigned int)task->tk_client->cl_prog,
  1166. (unsigned int)task->tk_client->cl_vers,
  1167. task->tk_client->cl_server);
  1168. error = -EPROTONOSUPPORT;
  1169. goto out_err;
  1170. case RPC_PROC_UNAVAIL:
  1171. dprintk("RPC: call_verify: proc %p unsupported by program %u, version %u on server %s\n",
  1172. task->tk_msg.rpc_proc,
  1173. task->tk_client->cl_prog,
  1174. task->tk_client->cl_vers,
  1175. task->tk_client->cl_server);
  1176. error = -EOPNOTSUPP;
  1177. goto out_err;
  1178. case RPC_GARBAGE_ARGS:
  1179. dprintk("RPC: %4d %s: server saw garbage\n", task->tk_pid, __FUNCTION__);
  1180. break; /* retry */
  1181. default:
  1182. printk(KERN_WARNING "call_verify: server accept status: %x\n", n);
  1183. /* Also retry */
  1184. }
  1185. out_garbage:
  1186. task->tk_client->cl_stats->rpcgarbage++;
  1187. if (task->tk_garb_retry) {
  1188. task->tk_garb_retry--;
  1189. dprintk("RPC %s: retrying %4d\n", __FUNCTION__, task->tk_pid);
  1190. task->tk_action = call_bind;
  1191. out_retry:
  1192. return ERR_PTR(-EAGAIN);
  1193. }
  1194. printk(KERN_WARNING "RPC %s: retry failed, exit EIO\n", __FUNCTION__);
  1195. out_eio:
  1196. error = -EIO;
  1197. out_err:
  1198. rpc_exit(task, error);
  1199. return ERR_PTR(error);
  1200. out_overflow:
  1201. printk(KERN_WARNING "RPC %s: server reply was truncated.\n", __FUNCTION__);
  1202. goto out_garbage;
  1203. }
  1204. static int rpcproc_encode_null(void *rqstp, __be32 *data, void *obj)
  1205. {
  1206. return 0;
  1207. }
  1208. static int rpcproc_decode_null(void *rqstp, __be32 *data, void *obj)
  1209. {
  1210. return 0;
  1211. }
  1212. static struct rpc_procinfo rpcproc_null = {
  1213. .p_encode = rpcproc_encode_null,
  1214. .p_decode = rpcproc_decode_null,
  1215. };
  1216. int rpc_ping(struct rpc_clnt *clnt, int flags)
  1217. {
  1218. struct rpc_message msg = {
  1219. .rpc_proc = &rpcproc_null,
  1220. };
  1221. int err;
  1222. msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
  1223. err = rpc_call_sync(clnt, &msg, flags);
  1224. put_rpccred(msg.rpc_cred);
  1225. return err;
  1226. }