clnt.c 28 KB

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