clnt.c 34 KB

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