clnt.c 34 KB

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