clnt.c 34 KB

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