clnt.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  1. /*
  2. * linux/net/sunrpc/rpcclnt.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/in.h>
  29. #include <linux/utsname.h>
  30. #include <linux/sunrpc/clnt.h>
  31. #include <linux/workqueue.h>
  32. #include <linux/sunrpc/rpc_pipe_fs.h>
  33. #include <linux/nfs.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_transmit(struct rpc_task *task);
  47. static void call_status(struct rpc_task *task);
  48. static void call_refresh(struct rpc_task *task);
  49. static void call_refreshresult(struct rpc_task *task);
  50. static void call_timeout(struct rpc_task *task);
  51. static void call_connect(struct rpc_task *task);
  52. static void call_connect_status(struct rpc_task *task);
  53. static u32 * call_header(struct rpc_task *task);
  54. static u32 * call_verify(struct rpc_task *task);
  55. static int
  56. rpc_setup_pipedir(struct rpc_clnt *clnt, char *dir_name)
  57. {
  58. static uint32_t clntid;
  59. int error;
  60. if (dir_name == NULL)
  61. return 0;
  62. for (;;) {
  63. snprintf(clnt->cl_pathname, sizeof(clnt->cl_pathname),
  64. "%s/clnt%x", dir_name,
  65. (unsigned int)clntid++);
  66. clnt->cl_pathname[sizeof(clnt->cl_pathname) - 1] = '\0';
  67. clnt->cl_dentry = rpc_mkdir(clnt->cl_pathname, clnt);
  68. if (!IS_ERR(clnt->cl_dentry))
  69. return 0;
  70. error = PTR_ERR(clnt->cl_dentry);
  71. if (error != -EEXIST) {
  72. printk(KERN_INFO "RPC: Couldn't create pipefs entry %s, error %d\n",
  73. clnt->cl_pathname, error);
  74. return error;
  75. }
  76. }
  77. }
  78. /*
  79. * Create an RPC client
  80. * FIXME: This should also take a flags argument (as in task->tk_flags).
  81. * It's called (among others) from pmap_create_client, which may in
  82. * turn be called by an async task. In this case, rpciod should not be
  83. * made to sleep too long.
  84. */
  85. struct rpc_clnt *
  86. rpc_create_client(struct rpc_xprt *xprt, char *servname,
  87. struct rpc_program *program, u32 vers,
  88. rpc_authflavor_t flavor)
  89. {
  90. struct rpc_version *version;
  91. struct rpc_clnt *clnt = NULL;
  92. int err;
  93. int len;
  94. dprintk("RPC: creating %s client for %s (xprt %p)\n",
  95. program->name, servname, xprt);
  96. err = -EINVAL;
  97. if (!xprt)
  98. goto out_err;
  99. if (vers >= program->nrvers || !(version = program->version[vers]))
  100. goto out_err;
  101. err = -ENOMEM;
  102. clnt = (struct rpc_clnt *) kmalloc(sizeof(*clnt), GFP_KERNEL);
  103. if (!clnt)
  104. goto out_err;
  105. memset(clnt, 0, sizeof(*clnt));
  106. atomic_set(&clnt->cl_users, 0);
  107. atomic_set(&clnt->cl_count, 1);
  108. clnt->cl_parent = clnt;
  109. clnt->cl_server = clnt->cl_inline_name;
  110. len = strlen(servname) + 1;
  111. if (len > sizeof(clnt->cl_inline_name)) {
  112. char *buf = kmalloc(len, GFP_KERNEL);
  113. if (buf != 0)
  114. clnt->cl_server = buf;
  115. else
  116. len = sizeof(clnt->cl_inline_name);
  117. }
  118. strlcpy(clnt->cl_server, servname, len);
  119. clnt->cl_xprt = xprt;
  120. clnt->cl_procinfo = version->procs;
  121. clnt->cl_maxproc = version->nrprocs;
  122. clnt->cl_protname = program->name;
  123. clnt->cl_pmap = &clnt->cl_pmap_default;
  124. clnt->cl_port = xprt->addr.sin_port;
  125. clnt->cl_prog = program->number;
  126. clnt->cl_vers = version->number;
  127. clnt->cl_prot = xprt->prot;
  128. clnt->cl_stats = program->stats;
  129. rpc_init_wait_queue(&clnt->cl_pmap_default.pm_bindwait, "bindwait");
  130. if (!clnt->cl_port)
  131. clnt->cl_autobind = 1;
  132. clnt->cl_rtt = &clnt->cl_rtt_default;
  133. rpc_init_rtt(&clnt->cl_rtt_default, xprt->timeout.to_initval);
  134. err = rpc_setup_pipedir(clnt, program->pipe_dir_name);
  135. if (err < 0)
  136. goto out_no_path;
  137. err = -ENOMEM;
  138. if (!rpcauth_create(flavor, clnt)) {
  139. printk(KERN_INFO "RPC: Couldn't create auth handle (flavor %u)\n",
  140. flavor);
  141. goto out_no_auth;
  142. }
  143. /* save the nodename */
  144. clnt->cl_nodelen = strlen(system_utsname.nodename);
  145. if (clnt->cl_nodelen > UNX_MAXNODENAME)
  146. clnt->cl_nodelen = UNX_MAXNODENAME;
  147. memcpy(clnt->cl_nodename, system_utsname.nodename, clnt->cl_nodelen);
  148. return clnt;
  149. out_no_auth:
  150. rpc_rmdir(clnt->cl_pathname);
  151. out_no_path:
  152. if (clnt->cl_server != clnt->cl_inline_name)
  153. kfree(clnt->cl_server);
  154. kfree(clnt);
  155. out_err:
  156. xprt_destroy(xprt);
  157. return ERR_PTR(err);
  158. }
  159. /*
  160. * This function clones the RPC client structure. It allows us to share the
  161. * same transport while varying parameters such as the authentication
  162. * flavour.
  163. */
  164. struct rpc_clnt *
  165. rpc_clone_client(struct rpc_clnt *clnt)
  166. {
  167. struct rpc_clnt *new;
  168. new = (struct rpc_clnt *)kmalloc(sizeof(*new), GFP_KERNEL);
  169. if (!new)
  170. goto out_no_clnt;
  171. memcpy(new, clnt, sizeof(*new));
  172. atomic_set(&new->cl_count, 1);
  173. atomic_set(&new->cl_users, 0);
  174. new->cl_parent = clnt;
  175. atomic_inc(&clnt->cl_count);
  176. /* Duplicate portmapper */
  177. rpc_init_wait_queue(&new->cl_pmap_default.pm_bindwait, "bindwait");
  178. /* Turn off autobind on clones */
  179. new->cl_autobind = 0;
  180. new->cl_oneshot = 0;
  181. new->cl_dead = 0;
  182. rpc_init_rtt(&new->cl_rtt_default, clnt->cl_xprt->timeout.to_initval);
  183. if (new->cl_auth)
  184. atomic_inc(&new->cl_auth->au_count);
  185. return new;
  186. out_no_clnt:
  187. printk(KERN_INFO "RPC: out of memory in %s\n", __FUNCTION__);
  188. return ERR_PTR(-ENOMEM);
  189. }
  190. /*
  191. * Properly shut down an RPC client, terminating all outstanding
  192. * requests. Note that we must be certain that cl_oneshot and
  193. * cl_dead are cleared, or else the client would be destroyed
  194. * when the last task releases it.
  195. */
  196. int
  197. rpc_shutdown_client(struct rpc_clnt *clnt)
  198. {
  199. dprintk("RPC: shutting down %s client for %s, tasks=%d\n",
  200. clnt->cl_protname, clnt->cl_server,
  201. atomic_read(&clnt->cl_users));
  202. while (atomic_read(&clnt->cl_users) > 0) {
  203. /* Don't let rpc_release_client destroy us */
  204. clnt->cl_oneshot = 0;
  205. clnt->cl_dead = 0;
  206. rpc_killall_tasks(clnt);
  207. sleep_on_timeout(&destroy_wait, 1*HZ);
  208. }
  209. if (atomic_read(&clnt->cl_users) < 0) {
  210. printk(KERN_ERR "RPC: rpc_shutdown_client clnt %p tasks=%d\n",
  211. clnt, atomic_read(&clnt->cl_users));
  212. #ifdef RPC_DEBUG
  213. rpc_show_tasks();
  214. #endif
  215. BUG();
  216. }
  217. return rpc_destroy_client(clnt);
  218. }
  219. /*
  220. * Delete an RPC client
  221. */
  222. int
  223. rpc_destroy_client(struct rpc_clnt *clnt)
  224. {
  225. if (!atomic_dec_and_test(&clnt->cl_count))
  226. return 1;
  227. BUG_ON(atomic_read(&clnt->cl_users) != 0);
  228. dprintk("RPC: destroying %s client for %s\n",
  229. clnt->cl_protname, clnt->cl_server);
  230. if (clnt->cl_auth) {
  231. rpcauth_destroy(clnt->cl_auth);
  232. clnt->cl_auth = NULL;
  233. }
  234. if (clnt->cl_parent != clnt) {
  235. rpc_destroy_client(clnt->cl_parent);
  236. goto out_free;
  237. }
  238. if (clnt->cl_pathname[0])
  239. rpc_rmdir(clnt->cl_pathname);
  240. if (clnt->cl_xprt) {
  241. xprt_destroy(clnt->cl_xprt);
  242. clnt->cl_xprt = NULL;
  243. }
  244. if (clnt->cl_server != clnt->cl_inline_name)
  245. kfree(clnt->cl_server);
  246. out_free:
  247. kfree(clnt);
  248. return 0;
  249. }
  250. /*
  251. * Release an RPC client
  252. */
  253. void
  254. rpc_release_client(struct rpc_clnt *clnt)
  255. {
  256. dprintk("RPC: rpc_release_client(%p, %d)\n",
  257. clnt, atomic_read(&clnt->cl_users));
  258. if (!atomic_dec_and_test(&clnt->cl_users))
  259. return;
  260. wake_up(&destroy_wait);
  261. if (clnt->cl_oneshot || clnt->cl_dead)
  262. rpc_destroy_client(clnt);
  263. }
  264. /*
  265. * Default callback for async RPC calls
  266. */
  267. static void
  268. rpc_default_callback(struct rpc_task *task)
  269. {
  270. }
  271. /*
  272. * Export the signal mask handling for aysnchronous code that
  273. * sleeps on RPC calls
  274. */
  275. void rpc_clnt_sigmask(struct rpc_clnt *clnt, sigset_t *oldset)
  276. {
  277. unsigned long sigallow = sigmask(SIGKILL);
  278. unsigned long irqflags;
  279. /* Turn off various signals */
  280. if (clnt->cl_intr) {
  281. struct k_sigaction *action = current->sighand->action;
  282. if (action[SIGINT-1].sa.sa_handler == SIG_DFL)
  283. sigallow |= sigmask(SIGINT);
  284. if (action[SIGQUIT-1].sa.sa_handler == SIG_DFL)
  285. sigallow |= sigmask(SIGQUIT);
  286. }
  287. spin_lock_irqsave(&current->sighand->siglock, irqflags);
  288. *oldset = current->blocked;
  289. siginitsetinv(&current->blocked, sigallow & ~oldset->sig[0]);
  290. recalc_sigpending();
  291. spin_unlock_irqrestore(&current->sighand->siglock, irqflags);
  292. }
  293. void rpc_clnt_sigunmask(struct rpc_clnt *clnt, sigset_t *oldset)
  294. {
  295. unsigned long irqflags;
  296. spin_lock_irqsave(&current->sighand->siglock, irqflags);
  297. current->blocked = *oldset;
  298. recalc_sigpending();
  299. spin_unlock_irqrestore(&current->sighand->siglock, irqflags);
  300. }
  301. /*
  302. * New rpc_call implementation
  303. */
  304. int rpc_call_sync(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
  305. {
  306. struct rpc_task *task;
  307. sigset_t oldset;
  308. int status;
  309. /* If this client is slain all further I/O fails */
  310. if (clnt->cl_dead)
  311. return -EIO;
  312. BUG_ON(flags & RPC_TASK_ASYNC);
  313. rpc_clnt_sigmask(clnt, &oldset);
  314. status = -ENOMEM;
  315. task = rpc_new_task(clnt, NULL, flags);
  316. if (task == NULL)
  317. goto out;
  318. rpc_call_setup(task, msg, 0);
  319. /* Set up the call info struct and execute the task */
  320. if (task->tk_status == 0)
  321. status = rpc_execute(task);
  322. else {
  323. status = task->tk_status;
  324. rpc_release_task(task);
  325. }
  326. out:
  327. rpc_clnt_sigunmask(clnt, &oldset);
  328. return status;
  329. }
  330. /*
  331. * New rpc_call implementation
  332. */
  333. int
  334. rpc_call_async(struct rpc_clnt *clnt, struct rpc_message *msg, int flags,
  335. rpc_action callback, void *data)
  336. {
  337. struct rpc_task *task;
  338. sigset_t oldset;
  339. int status;
  340. /* If this client is slain all further I/O fails */
  341. if (clnt->cl_dead)
  342. return -EIO;
  343. flags |= RPC_TASK_ASYNC;
  344. rpc_clnt_sigmask(clnt, &oldset);
  345. /* Create/initialize a new RPC task */
  346. if (!callback)
  347. callback = rpc_default_callback;
  348. status = -ENOMEM;
  349. if (!(task = rpc_new_task(clnt, callback, flags)))
  350. goto out;
  351. task->tk_calldata = data;
  352. rpc_call_setup(task, msg, 0);
  353. /* Set up the call info struct and execute the task */
  354. status = task->tk_status;
  355. if (status == 0)
  356. rpc_execute(task);
  357. else
  358. rpc_release_task(task);
  359. out:
  360. rpc_clnt_sigunmask(clnt, &oldset);
  361. return status;
  362. }
  363. void
  364. rpc_call_setup(struct rpc_task *task, struct rpc_message *msg, int flags)
  365. {
  366. task->tk_msg = *msg;
  367. task->tk_flags |= flags;
  368. /* Bind the user cred */
  369. if (task->tk_msg.rpc_cred != NULL)
  370. rpcauth_holdcred(task);
  371. else
  372. rpcauth_bindcred(task);
  373. if (task->tk_status == 0)
  374. task->tk_action = call_start;
  375. else
  376. task->tk_action = NULL;
  377. }
  378. void
  379. rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
  380. {
  381. struct rpc_xprt *xprt = clnt->cl_xprt;
  382. xprt->sndsize = 0;
  383. if (sndsize)
  384. xprt->sndsize = sndsize + RPC_SLACK_SPACE;
  385. xprt->rcvsize = 0;
  386. if (rcvsize)
  387. xprt->rcvsize = rcvsize + RPC_SLACK_SPACE;
  388. if (xprt_connected(xprt))
  389. xprt_sock_setbufsize(xprt);
  390. }
  391. /*
  392. * Return size of largest payload RPC client can support, in bytes
  393. *
  394. * For stream transports, this is one RPC record fragment (see RFC
  395. * 1831), as we don't support multi-record requests yet. For datagram
  396. * transports, this is the size of an IP packet minus the IP, UDP, and
  397. * RPC header sizes.
  398. */
  399. size_t rpc_max_payload(struct rpc_clnt *clnt)
  400. {
  401. return clnt->cl_xprt->max_payload;
  402. }
  403. EXPORT_SYMBOL(rpc_max_payload);
  404. /*
  405. * Restart an (async) RPC call. Usually called from within the
  406. * exit handler.
  407. */
  408. void
  409. rpc_restart_call(struct rpc_task *task)
  410. {
  411. if (RPC_ASSASSINATED(task))
  412. return;
  413. task->tk_action = call_start;
  414. }
  415. /*
  416. * 0. Initial state
  417. *
  418. * Other FSM states can be visited zero or more times, but
  419. * this state is visited exactly once for each RPC.
  420. */
  421. static void
  422. call_start(struct rpc_task *task)
  423. {
  424. struct rpc_clnt *clnt = task->tk_client;
  425. dprintk("RPC: %4d call_start %s%d proc %d (%s)\n", task->tk_pid,
  426. clnt->cl_protname, clnt->cl_vers, task->tk_msg.rpc_proc->p_proc,
  427. (RPC_IS_ASYNC(task) ? "async" : "sync"));
  428. /* Increment call count */
  429. task->tk_msg.rpc_proc->p_count++;
  430. clnt->cl_stats->rpccnt++;
  431. task->tk_action = call_reserve;
  432. }
  433. /*
  434. * 1. Reserve an RPC call slot
  435. */
  436. static void
  437. call_reserve(struct rpc_task *task)
  438. {
  439. dprintk("RPC: %4d call_reserve\n", task->tk_pid);
  440. if (!rpcauth_uptodatecred(task)) {
  441. task->tk_action = call_refresh;
  442. return;
  443. }
  444. task->tk_status = 0;
  445. task->tk_action = call_reserveresult;
  446. xprt_reserve(task);
  447. }
  448. /*
  449. * 1b. Grok the result of xprt_reserve()
  450. */
  451. static void
  452. call_reserveresult(struct rpc_task *task)
  453. {
  454. int status = task->tk_status;
  455. dprintk("RPC: %4d call_reserveresult (status %d)\n",
  456. task->tk_pid, task->tk_status);
  457. /*
  458. * After a call to xprt_reserve(), we must have either
  459. * a request slot or else an error status.
  460. */
  461. task->tk_status = 0;
  462. if (status >= 0) {
  463. if (task->tk_rqstp) {
  464. task->tk_action = call_allocate;
  465. return;
  466. }
  467. printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
  468. __FUNCTION__, status);
  469. rpc_exit(task, -EIO);
  470. return;
  471. }
  472. /*
  473. * Even though there was an error, we may have acquired
  474. * a request slot somehow. Make sure not to leak it.
  475. */
  476. if (task->tk_rqstp) {
  477. printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
  478. __FUNCTION__, status);
  479. xprt_release(task);
  480. }
  481. switch (status) {
  482. case -EAGAIN: /* woken up; retry */
  483. task->tk_action = call_reserve;
  484. return;
  485. case -EIO: /* probably a shutdown */
  486. break;
  487. default:
  488. printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
  489. __FUNCTION__, status);
  490. break;
  491. }
  492. rpc_exit(task, status);
  493. }
  494. /*
  495. * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
  496. * (Note: buffer memory is freed in rpc_task_release).
  497. */
  498. static void
  499. call_allocate(struct rpc_task *task)
  500. {
  501. unsigned int bufsiz;
  502. dprintk("RPC: %4d call_allocate (status %d)\n",
  503. task->tk_pid, task->tk_status);
  504. task->tk_action = call_bind;
  505. if (task->tk_buffer)
  506. return;
  507. /* FIXME: compute buffer requirements more exactly using
  508. * auth->au_wslack */
  509. bufsiz = task->tk_msg.rpc_proc->p_bufsiz + RPC_SLACK_SPACE;
  510. if (rpc_malloc(task, bufsiz << 1) != NULL)
  511. return;
  512. printk(KERN_INFO "RPC: buffer allocation failed for task %p\n", task);
  513. if (RPC_IS_ASYNC(task) || !(task->tk_client->cl_intr && signalled())) {
  514. xprt_release(task);
  515. task->tk_action = call_reserve;
  516. rpc_delay(task, HZ>>4);
  517. return;
  518. }
  519. rpc_exit(task, -ERESTARTSYS);
  520. }
  521. /*
  522. * 3. Encode arguments of an RPC call
  523. */
  524. static void
  525. call_encode(struct rpc_task *task)
  526. {
  527. struct rpc_clnt *clnt = task->tk_client;
  528. struct rpc_rqst *req = task->tk_rqstp;
  529. struct xdr_buf *sndbuf = &req->rq_snd_buf;
  530. struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
  531. unsigned int bufsiz;
  532. kxdrproc_t encode;
  533. int status;
  534. u32 *p;
  535. dprintk("RPC: %4d call_encode (status %d)\n",
  536. task->tk_pid, task->tk_status);
  537. /* Default buffer setup */
  538. bufsiz = task->tk_bufsize >> 1;
  539. sndbuf->head[0].iov_base = (void *)task->tk_buffer;
  540. sndbuf->head[0].iov_len = bufsiz;
  541. sndbuf->tail[0].iov_len = 0;
  542. sndbuf->page_len = 0;
  543. sndbuf->len = 0;
  544. sndbuf->buflen = bufsiz;
  545. rcvbuf->head[0].iov_base = (void *)((char *)task->tk_buffer + bufsiz);
  546. rcvbuf->head[0].iov_len = bufsiz;
  547. rcvbuf->tail[0].iov_len = 0;
  548. rcvbuf->page_len = 0;
  549. rcvbuf->len = 0;
  550. rcvbuf->buflen = bufsiz;
  551. /* Encode header and provided arguments */
  552. encode = task->tk_msg.rpc_proc->p_encode;
  553. if (!(p = call_header(task))) {
  554. printk(KERN_INFO "RPC: call_header failed, exit EIO\n");
  555. rpc_exit(task, -EIO);
  556. return;
  557. }
  558. if (encode && (status = rpcauth_wrap_req(task, encode, req, p,
  559. task->tk_msg.rpc_argp)) < 0) {
  560. printk(KERN_WARNING "%s: can't encode arguments: %d\n",
  561. clnt->cl_protname, -status);
  562. rpc_exit(task, status);
  563. }
  564. }
  565. /*
  566. * 4. Get the server port number if not yet set
  567. */
  568. static void
  569. call_bind(struct rpc_task *task)
  570. {
  571. struct rpc_clnt *clnt = task->tk_client;
  572. struct rpc_xprt *xprt = clnt->cl_xprt;
  573. dprintk("RPC: %4d call_bind xprt %p %s connected\n", task->tk_pid,
  574. xprt, (xprt_connected(xprt) ? "is" : "is not"));
  575. task->tk_action = (xprt_connected(xprt)) ? call_transmit : call_connect;
  576. if (!clnt->cl_port) {
  577. task->tk_action = call_connect;
  578. task->tk_timeout = RPC_CONNECT_TIMEOUT;
  579. rpc_getport(task, clnt);
  580. }
  581. }
  582. /*
  583. * 4a. Connect to the RPC server (TCP case)
  584. */
  585. static void
  586. call_connect(struct rpc_task *task)
  587. {
  588. struct rpc_clnt *clnt = task->tk_client;
  589. dprintk("RPC: %4d call_connect status %d\n",
  590. task->tk_pid, task->tk_status);
  591. if (xprt_connected(clnt->cl_xprt)) {
  592. task->tk_action = call_transmit;
  593. return;
  594. }
  595. task->tk_action = call_connect_status;
  596. if (task->tk_status < 0)
  597. return;
  598. xprt_connect(task);
  599. }
  600. /*
  601. * 4b. Sort out connect result
  602. */
  603. static void
  604. call_connect_status(struct rpc_task *task)
  605. {
  606. struct rpc_clnt *clnt = task->tk_client;
  607. int status = task->tk_status;
  608. task->tk_status = 0;
  609. if (status >= 0) {
  610. clnt->cl_stats->netreconn++;
  611. task->tk_action = call_transmit;
  612. return;
  613. }
  614. /* Something failed: we may have to rebind */
  615. if (clnt->cl_autobind)
  616. clnt->cl_port = 0;
  617. switch (status) {
  618. case -ENOTCONN:
  619. case -ETIMEDOUT:
  620. case -EAGAIN:
  621. task->tk_action = (clnt->cl_port == 0) ? call_bind : call_connect;
  622. break;
  623. default:
  624. rpc_exit(task, -EIO);
  625. }
  626. }
  627. /*
  628. * 5. Transmit the RPC request, and wait for reply
  629. */
  630. static void
  631. call_transmit(struct rpc_task *task)
  632. {
  633. dprintk("RPC: %4d call_transmit (status %d)\n",
  634. task->tk_pid, task->tk_status);
  635. task->tk_action = call_status;
  636. if (task->tk_status < 0)
  637. return;
  638. task->tk_status = xprt_prepare_transmit(task);
  639. if (task->tk_status != 0)
  640. return;
  641. /* Encode here so that rpcsec_gss can use correct sequence number. */
  642. if (!task->tk_rqstp->rq_bytes_sent)
  643. call_encode(task);
  644. if (task->tk_status < 0)
  645. return;
  646. xprt_transmit(task);
  647. if (task->tk_status < 0)
  648. return;
  649. if (!task->tk_msg.rpc_proc->p_decode) {
  650. task->tk_action = NULL;
  651. rpc_wake_up_task(task);
  652. }
  653. }
  654. /*
  655. * 6. Sort out the RPC call status
  656. */
  657. static void
  658. call_status(struct rpc_task *task)
  659. {
  660. struct rpc_clnt *clnt = task->tk_client;
  661. struct rpc_rqst *req = task->tk_rqstp;
  662. int status;
  663. if (req->rq_received > 0 && !req->rq_bytes_sent)
  664. task->tk_status = req->rq_received;
  665. dprintk("RPC: %4d call_status (status %d)\n",
  666. task->tk_pid, task->tk_status);
  667. status = task->tk_status;
  668. if (status >= 0) {
  669. task->tk_action = call_decode;
  670. return;
  671. }
  672. task->tk_status = 0;
  673. switch(status) {
  674. case -ETIMEDOUT:
  675. task->tk_action = call_timeout;
  676. break;
  677. case -ECONNREFUSED:
  678. case -ENOTCONN:
  679. req->rq_bytes_sent = 0;
  680. if (clnt->cl_autobind)
  681. clnt->cl_port = 0;
  682. task->tk_action = call_bind;
  683. break;
  684. case -EAGAIN:
  685. task->tk_action = call_transmit;
  686. break;
  687. case -EIO:
  688. /* shutdown or soft timeout */
  689. rpc_exit(task, status);
  690. break;
  691. default:
  692. if (clnt->cl_chatty)
  693. printk("%s: RPC call returned error %d\n",
  694. clnt->cl_protname, -status);
  695. rpc_exit(task, status);
  696. break;
  697. }
  698. }
  699. /*
  700. * 6a. Handle RPC timeout
  701. * We do not release the request slot, so we keep using the
  702. * same XID for all retransmits.
  703. */
  704. static void
  705. call_timeout(struct rpc_task *task)
  706. {
  707. struct rpc_clnt *clnt = task->tk_client;
  708. if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
  709. dprintk("RPC: %4d call_timeout (minor)\n", task->tk_pid);
  710. goto retry;
  711. }
  712. dprintk("RPC: %4d call_timeout (major)\n", task->tk_pid);
  713. if (RPC_IS_SOFT(task)) {
  714. if (clnt->cl_chatty)
  715. printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
  716. clnt->cl_protname, clnt->cl_server);
  717. rpc_exit(task, -EIO);
  718. return;
  719. }
  720. if (clnt->cl_chatty && !(task->tk_flags & RPC_CALL_MAJORSEEN)) {
  721. task->tk_flags |= RPC_CALL_MAJORSEEN;
  722. printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
  723. clnt->cl_protname, clnt->cl_server);
  724. }
  725. if (clnt->cl_autobind)
  726. clnt->cl_port = 0;
  727. retry:
  728. clnt->cl_stats->rpcretrans++;
  729. task->tk_action = call_bind;
  730. task->tk_status = 0;
  731. }
  732. /*
  733. * 7. Decode the RPC reply
  734. */
  735. static void
  736. call_decode(struct rpc_task *task)
  737. {
  738. struct rpc_clnt *clnt = task->tk_client;
  739. struct rpc_rqst *req = task->tk_rqstp;
  740. kxdrproc_t decode = task->tk_msg.rpc_proc->p_decode;
  741. u32 *p;
  742. dprintk("RPC: %4d call_decode (status %d)\n",
  743. task->tk_pid, task->tk_status);
  744. if (clnt->cl_chatty && (task->tk_flags & RPC_CALL_MAJORSEEN)) {
  745. printk(KERN_NOTICE "%s: server %s OK\n",
  746. clnt->cl_protname, clnt->cl_server);
  747. task->tk_flags &= ~RPC_CALL_MAJORSEEN;
  748. }
  749. if (task->tk_status < 12) {
  750. if (!RPC_IS_SOFT(task)) {
  751. task->tk_action = call_bind;
  752. clnt->cl_stats->rpcretrans++;
  753. goto out_retry;
  754. }
  755. printk(KERN_WARNING "%s: too small RPC reply size (%d bytes)\n",
  756. clnt->cl_protname, task->tk_status);
  757. rpc_exit(task, -EIO);
  758. return;
  759. }
  760. req->rq_rcv_buf.len = req->rq_private_buf.len;
  761. /* Check that the softirq receive buffer is valid */
  762. WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
  763. sizeof(req->rq_rcv_buf)) != 0);
  764. /* Verify the RPC header */
  765. if (!(p = call_verify(task))) {
  766. if (task->tk_action == NULL)
  767. return;
  768. goto out_retry;
  769. }
  770. task->tk_action = NULL;
  771. if (decode)
  772. task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
  773. task->tk_msg.rpc_resp);
  774. dprintk("RPC: %4d call_decode result %d\n", task->tk_pid,
  775. task->tk_status);
  776. return;
  777. out_retry:
  778. req->rq_received = req->rq_private_buf.len = 0;
  779. task->tk_status = 0;
  780. }
  781. /*
  782. * 8. Refresh the credentials if rejected by the server
  783. */
  784. static void
  785. call_refresh(struct rpc_task *task)
  786. {
  787. dprintk("RPC: %4d call_refresh\n", task->tk_pid);
  788. xprt_release(task); /* Must do to obtain new XID */
  789. task->tk_action = call_refreshresult;
  790. task->tk_status = 0;
  791. task->tk_client->cl_stats->rpcauthrefresh++;
  792. rpcauth_refreshcred(task);
  793. }
  794. /*
  795. * 8a. Process the results of a credential refresh
  796. */
  797. static void
  798. call_refreshresult(struct rpc_task *task)
  799. {
  800. int status = task->tk_status;
  801. dprintk("RPC: %4d call_refreshresult (status %d)\n",
  802. task->tk_pid, task->tk_status);
  803. task->tk_status = 0;
  804. task->tk_action = call_reserve;
  805. if (status >= 0 && rpcauth_uptodatecred(task))
  806. return;
  807. if (status == -EACCES) {
  808. rpc_exit(task, -EACCES);
  809. return;
  810. }
  811. task->tk_action = call_refresh;
  812. if (status != -ETIMEDOUT)
  813. rpc_delay(task, 3*HZ);
  814. return;
  815. }
  816. /*
  817. * Call header serialization
  818. */
  819. static u32 *
  820. call_header(struct rpc_task *task)
  821. {
  822. struct rpc_clnt *clnt = task->tk_client;
  823. struct rpc_xprt *xprt = clnt->cl_xprt;
  824. struct rpc_rqst *req = task->tk_rqstp;
  825. u32 *p = req->rq_svec[0].iov_base;
  826. /* FIXME: check buffer size? */
  827. if (xprt->stream)
  828. *p++ = 0; /* fill in later */
  829. *p++ = req->rq_xid; /* XID */
  830. *p++ = htonl(RPC_CALL); /* CALL */
  831. *p++ = htonl(RPC_VERSION); /* RPC version */
  832. *p++ = htonl(clnt->cl_prog); /* program number */
  833. *p++ = htonl(clnt->cl_vers); /* program version */
  834. *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
  835. p = rpcauth_marshcred(task, p);
  836. req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
  837. return p;
  838. }
  839. /*
  840. * Reply header verification
  841. */
  842. static u32 *
  843. call_verify(struct rpc_task *task)
  844. {
  845. struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
  846. int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
  847. u32 *p = iov->iov_base, n;
  848. int error = -EACCES;
  849. if ((len -= 3) < 0)
  850. goto out_overflow;
  851. p += 1; /* skip XID */
  852. if ((n = ntohl(*p++)) != RPC_REPLY) {
  853. printk(KERN_WARNING "call_verify: not an RPC reply: %x\n", n);
  854. goto out_retry;
  855. }
  856. if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
  857. if (--len < 0)
  858. goto out_overflow;
  859. switch ((n = ntohl(*p++))) {
  860. case RPC_AUTH_ERROR:
  861. break;
  862. case RPC_MISMATCH:
  863. printk(KERN_WARNING "%s: RPC call version mismatch!\n", __FUNCTION__);
  864. goto out_eio;
  865. default:
  866. printk(KERN_WARNING "%s: RPC call rejected, unknown error: %x\n", __FUNCTION__, n);
  867. goto out_eio;
  868. }
  869. if (--len < 0)
  870. goto out_overflow;
  871. switch ((n = ntohl(*p++))) {
  872. case RPC_AUTH_REJECTEDCRED:
  873. case RPC_AUTH_REJECTEDVERF:
  874. case RPCSEC_GSS_CREDPROBLEM:
  875. case RPCSEC_GSS_CTXPROBLEM:
  876. if (!task->tk_cred_retry)
  877. break;
  878. task->tk_cred_retry--;
  879. dprintk("RPC: %4d call_verify: retry stale creds\n",
  880. task->tk_pid);
  881. rpcauth_invalcred(task);
  882. task->tk_action = call_refresh;
  883. return NULL;
  884. case RPC_AUTH_BADCRED:
  885. case RPC_AUTH_BADVERF:
  886. /* possibly garbled cred/verf? */
  887. if (!task->tk_garb_retry)
  888. break;
  889. task->tk_garb_retry--;
  890. dprintk("RPC: %4d call_verify: retry garbled creds\n",
  891. task->tk_pid);
  892. task->tk_action = call_bind;
  893. return NULL;
  894. case RPC_AUTH_TOOWEAK:
  895. printk(KERN_NOTICE "call_verify: server requires stronger "
  896. "authentication.\n");
  897. break;
  898. default:
  899. printk(KERN_WARNING "call_verify: unknown auth error: %x\n", n);
  900. error = -EIO;
  901. }
  902. dprintk("RPC: %4d call_verify: call rejected %d\n",
  903. task->tk_pid, n);
  904. goto out_err;
  905. }
  906. if (!(p = rpcauth_checkverf(task, p))) {
  907. printk(KERN_WARNING "call_verify: auth check failed\n");
  908. goto out_retry; /* bad verifier, retry */
  909. }
  910. len = p - (u32 *)iov->iov_base - 1;
  911. if (len < 0)
  912. goto out_overflow;
  913. switch ((n = ntohl(*p++))) {
  914. case RPC_SUCCESS:
  915. return p;
  916. case RPC_PROG_UNAVAIL:
  917. printk(KERN_WARNING "RPC: call_verify: program %u is unsupported by server %s\n",
  918. (unsigned int)task->tk_client->cl_prog,
  919. task->tk_client->cl_server);
  920. goto out_eio;
  921. case RPC_PROG_MISMATCH:
  922. printk(KERN_WARNING "RPC: call_verify: program %u, version %u unsupported by server %s\n",
  923. (unsigned int)task->tk_client->cl_prog,
  924. (unsigned int)task->tk_client->cl_vers,
  925. task->tk_client->cl_server);
  926. goto out_eio;
  927. case RPC_PROC_UNAVAIL:
  928. printk(KERN_WARNING "RPC: call_verify: proc %p unsupported by program %u, version %u on server %s\n",
  929. task->tk_msg.rpc_proc,
  930. task->tk_client->cl_prog,
  931. task->tk_client->cl_vers,
  932. task->tk_client->cl_server);
  933. goto out_eio;
  934. case RPC_GARBAGE_ARGS:
  935. dprintk("RPC: %4d %s: server saw garbage\n", task->tk_pid, __FUNCTION__);
  936. break; /* retry */
  937. default:
  938. printk(KERN_WARNING "call_verify: server accept status: %x\n", n);
  939. /* Also retry */
  940. }
  941. out_retry:
  942. task->tk_client->cl_stats->rpcgarbage++;
  943. if (task->tk_garb_retry) {
  944. task->tk_garb_retry--;
  945. dprintk(KERN_WARNING "RPC %s: retrying %4d\n", __FUNCTION__, task->tk_pid);
  946. task->tk_action = call_bind;
  947. return NULL;
  948. }
  949. printk(KERN_WARNING "RPC %s: retry failed, exit EIO\n", __FUNCTION__);
  950. out_eio:
  951. error = -EIO;
  952. out_err:
  953. rpc_exit(task, error);
  954. return NULL;
  955. out_overflow:
  956. printk(KERN_WARNING "RPC %s: server reply was truncated.\n", __FUNCTION__);
  957. goto out_retry;
  958. }