clnt.c 30 KB

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