clnt.c 34 KB

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