clnt.c 30 KB

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