clnt.c 34 KB

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