clnt.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  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_transmit_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, void *data)
  338. {
  339. }
  340. static const struct rpc_call_ops rpc_default_ops = {
  341. .rpc_call_done = rpc_default_callback,
  342. };
  343. /*
  344. * Export the signal mask handling for synchronous code that
  345. * sleeps on RPC calls
  346. */
  347. #define RPC_INTR_SIGNALS (sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTERM))
  348. static void rpc_save_sigmask(sigset_t *oldset, int intr)
  349. {
  350. unsigned long sigallow = sigmask(SIGKILL);
  351. sigset_t sigmask;
  352. /* Block all signals except those listed in sigallow */
  353. if (intr)
  354. sigallow |= RPC_INTR_SIGNALS;
  355. siginitsetinv(&sigmask, sigallow);
  356. sigprocmask(SIG_BLOCK, &sigmask, oldset);
  357. }
  358. static inline void rpc_task_sigmask(struct rpc_task *task, sigset_t *oldset)
  359. {
  360. rpc_save_sigmask(oldset, !RPC_TASK_UNINTERRUPTIBLE(task));
  361. }
  362. static inline void rpc_restore_sigmask(sigset_t *oldset)
  363. {
  364. sigprocmask(SIG_SETMASK, oldset, NULL);
  365. }
  366. void rpc_clnt_sigmask(struct rpc_clnt *clnt, sigset_t *oldset)
  367. {
  368. rpc_save_sigmask(oldset, clnt->cl_intr);
  369. }
  370. void rpc_clnt_sigunmask(struct rpc_clnt *clnt, sigset_t *oldset)
  371. {
  372. rpc_restore_sigmask(oldset);
  373. }
  374. /*
  375. * New rpc_call implementation
  376. */
  377. int rpc_call_sync(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
  378. {
  379. struct rpc_task *task;
  380. sigset_t oldset;
  381. int status;
  382. /* If this client is slain all further I/O fails */
  383. if (clnt->cl_dead)
  384. return -EIO;
  385. BUG_ON(flags & RPC_TASK_ASYNC);
  386. status = -ENOMEM;
  387. task = rpc_new_task(clnt, flags, &rpc_default_ops, NULL);
  388. if (task == NULL)
  389. goto out;
  390. /* Mask signals on RPC calls _and_ GSS_AUTH upcalls */
  391. rpc_task_sigmask(task, &oldset);
  392. rpc_call_setup(task, msg, 0);
  393. /* Set up the call info struct and execute the task */
  394. status = task->tk_status;
  395. if (status == 0) {
  396. atomic_inc(&task->tk_count);
  397. status = rpc_execute(task);
  398. if (status == 0)
  399. status = task->tk_status;
  400. }
  401. rpc_restore_sigmask(&oldset);
  402. rpc_release_task(task);
  403. out:
  404. return status;
  405. }
  406. /*
  407. * New rpc_call implementation
  408. */
  409. int
  410. rpc_call_async(struct rpc_clnt *clnt, struct rpc_message *msg, int flags,
  411. const struct rpc_call_ops *tk_ops, void *data)
  412. {
  413. struct rpc_task *task;
  414. sigset_t oldset;
  415. int status;
  416. /* If this client is slain all further I/O fails */
  417. if (clnt->cl_dead)
  418. return -EIO;
  419. flags |= RPC_TASK_ASYNC;
  420. /* Create/initialize a new RPC task */
  421. status = -ENOMEM;
  422. if (!(task = rpc_new_task(clnt, flags, tk_ops, data)))
  423. goto out;
  424. /* Mask signals on GSS_AUTH upcalls */
  425. rpc_task_sigmask(task, &oldset);
  426. rpc_call_setup(task, msg, 0);
  427. /* Set up the call info struct and execute the task */
  428. status = task->tk_status;
  429. if (status == 0)
  430. rpc_execute(task);
  431. else
  432. rpc_release_task(task);
  433. rpc_restore_sigmask(&oldset);
  434. out:
  435. return status;
  436. }
  437. void
  438. rpc_call_setup(struct rpc_task *task, struct rpc_message *msg, int flags)
  439. {
  440. task->tk_msg = *msg;
  441. task->tk_flags |= flags;
  442. /* Bind the user cred */
  443. if (task->tk_msg.rpc_cred != NULL)
  444. rpcauth_holdcred(task);
  445. else
  446. rpcauth_bindcred(task);
  447. if (task->tk_status == 0)
  448. task->tk_action = call_start;
  449. else
  450. task->tk_action = rpc_exit_task;
  451. }
  452. void
  453. rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
  454. {
  455. struct rpc_xprt *xprt = clnt->cl_xprt;
  456. if (xprt->ops->set_buffer_size)
  457. xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
  458. }
  459. /*
  460. * Return size of largest payload RPC client can support, in bytes
  461. *
  462. * For stream transports, this is one RPC record fragment (see RFC
  463. * 1831), as we don't support multi-record requests yet. For datagram
  464. * transports, this is the size of an IP packet minus the IP, UDP, and
  465. * RPC header sizes.
  466. */
  467. size_t rpc_max_payload(struct rpc_clnt *clnt)
  468. {
  469. return clnt->cl_xprt->max_payload;
  470. }
  471. EXPORT_SYMBOL(rpc_max_payload);
  472. /*
  473. * Restart an (async) RPC call. Usually called from within the
  474. * exit handler.
  475. */
  476. void
  477. rpc_restart_call(struct rpc_task *task)
  478. {
  479. if (RPC_ASSASSINATED(task))
  480. return;
  481. task->tk_action = call_start;
  482. }
  483. /*
  484. * 0. Initial state
  485. *
  486. * Other FSM states can be visited zero or more times, but
  487. * this state is visited exactly once for each RPC.
  488. */
  489. static void
  490. call_start(struct rpc_task *task)
  491. {
  492. struct rpc_clnt *clnt = task->tk_client;
  493. dprintk("RPC: %4d call_start %s%d proc %d (%s)\n", task->tk_pid,
  494. clnt->cl_protname, clnt->cl_vers, task->tk_msg.rpc_proc->p_proc,
  495. (RPC_IS_ASYNC(task) ? "async" : "sync"));
  496. /* Increment call count */
  497. task->tk_msg.rpc_proc->p_count++;
  498. clnt->cl_stats->rpccnt++;
  499. task->tk_action = call_reserve;
  500. }
  501. /*
  502. * 1. Reserve an RPC call slot
  503. */
  504. static void
  505. call_reserve(struct rpc_task *task)
  506. {
  507. dprintk("RPC: %4d call_reserve\n", task->tk_pid);
  508. if (!rpcauth_uptodatecred(task)) {
  509. task->tk_action = call_refresh;
  510. return;
  511. }
  512. task->tk_status = 0;
  513. task->tk_action = call_reserveresult;
  514. xprt_reserve(task);
  515. }
  516. /*
  517. * 1b. Grok the result of xprt_reserve()
  518. */
  519. static void
  520. call_reserveresult(struct rpc_task *task)
  521. {
  522. int status = task->tk_status;
  523. dprintk("RPC: %4d call_reserveresult (status %d)\n",
  524. task->tk_pid, task->tk_status);
  525. /*
  526. * After a call to xprt_reserve(), we must have either
  527. * a request slot or else an error status.
  528. */
  529. task->tk_status = 0;
  530. if (status >= 0) {
  531. if (task->tk_rqstp) {
  532. task->tk_action = call_allocate;
  533. return;
  534. }
  535. printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
  536. __FUNCTION__, status);
  537. rpc_exit(task, -EIO);
  538. return;
  539. }
  540. /*
  541. * Even though there was an error, we may have acquired
  542. * a request slot somehow. Make sure not to leak it.
  543. */
  544. if (task->tk_rqstp) {
  545. printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
  546. __FUNCTION__, status);
  547. xprt_release(task);
  548. }
  549. switch (status) {
  550. case -EAGAIN: /* woken up; retry */
  551. task->tk_action = call_reserve;
  552. return;
  553. case -EIO: /* probably a shutdown */
  554. break;
  555. default:
  556. printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
  557. __FUNCTION__, status);
  558. break;
  559. }
  560. rpc_exit(task, status);
  561. }
  562. /*
  563. * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
  564. * (Note: buffer memory is freed in rpc_task_release).
  565. */
  566. static void
  567. call_allocate(struct rpc_task *task)
  568. {
  569. unsigned int bufsiz;
  570. dprintk("RPC: %4d call_allocate (status %d)\n",
  571. task->tk_pid, task->tk_status);
  572. task->tk_action = call_bind;
  573. if (task->tk_buffer)
  574. return;
  575. /* FIXME: compute buffer requirements more exactly using
  576. * auth->au_wslack */
  577. bufsiz = task->tk_msg.rpc_proc->p_bufsiz + RPC_SLACK_SPACE;
  578. if (rpc_malloc(task, bufsiz << 1) != NULL)
  579. return;
  580. printk(KERN_INFO "RPC: buffer allocation failed for task %p\n", task);
  581. if (RPC_IS_ASYNC(task) || !signalled()) {
  582. xprt_release(task);
  583. task->tk_action = call_reserve;
  584. rpc_delay(task, HZ>>4);
  585. return;
  586. }
  587. rpc_exit(task, -ERESTARTSYS);
  588. }
  589. static inline int
  590. rpc_task_need_encode(struct rpc_task *task)
  591. {
  592. return task->tk_rqstp->rq_snd_buf.len == 0;
  593. }
  594. static inline void
  595. rpc_task_force_reencode(struct rpc_task *task)
  596. {
  597. task->tk_rqstp->rq_snd_buf.len = 0;
  598. }
  599. /*
  600. * 3. Encode arguments of an RPC call
  601. */
  602. static void
  603. call_encode(struct rpc_task *task)
  604. {
  605. struct rpc_rqst *req = task->tk_rqstp;
  606. struct xdr_buf *sndbuf = &req->rq_snd_buf;
  607. struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
  608. unsigned int bufsiz;
  609. kxdrproc_t encode;
  610. u32 *p;
  611. dprintk("RPC: %4d call_encode (status %d)\n",
  612. task->tk_pid, task->tk_status);
  613. /* Default buffer setup */
  614. bufsiz = task->tk_bufsize >> 1;
  615. sndbuf->head[0].iov_base = (void *)task->tk_buffer;
  616. sndbuf->head[0].iov_len = bufsiz;
  617. sndbuf->tail[0].iov_len = 0;
  618. sndbuf->page_len = 0;
  619. sndbuf->len = 0;
  620. sndbuf->buflen = bufsiz;
  621. rcvbuf->head[0].iov_base = (void *)((char *)task->tk_buffer + bufsiz);
  622. rcvbuf->head[0].iov_len = bufsiz;
  623. rcvbuf->tail[0].iov_len = 0;
  624. rcvbuf->page_len = 0;
  625. rcvbuf->len = 0;
  626. rcvbuf->buflen = bufsiz;
  627. /* Encode header and provided arguments */
  628. encode = task->tk_msg.rpc_proc->p_encode;
  629. if (!(p = call_header(task))) {
  630. printk(KERN_INFO "RPC: call_header failed, exit EIO\n");
  631. rpc_exit(task, -EIO);
  632. return;
  633. }
  634. if (encode == NULL)
  635. return;
  636. task->tk_status = rpcauth_wrap_req(task, encode, req, p,
  637. task->tk_msg.rpc_argp);
  638. if (task->tk_status == -ENOMEM) {
  639. /* XXX: Is this sane? */
  640. rpc_delay(task, 3*HZ);
  641. task->tk_status = -EAGAIN;
  642. }
  643. }
  644. /*
  645. * 4. Get the server port number if not yet set
  646. */
  647. static void
  648. call_bind(struct rpc_task *task)
  649. {
  650. struct rpc_clnt *clnt = task->tk_client;
  651. dprintk("RPC: %4d call_bind (status %d)\n",
  652. task->tk_pid, task->tk_status);
  653. task->tk_action = call_connect;
  654. if (!clnt->cl_port) {
  655. task->tk_action = call_bind_status;
  656. task->tk_timeout = task->tk_xprt->bind_timeout;
  657. rpc_getport(task, clnt);
  658. }
  659. }
  660. /*
  661. * 4a. Sort out bind result
  662. */
  663. static void
  664. call_bind_status(struct rpc_task *task)
  665. {
  666. int status = -EACCES;
  667. if (task->tk_status >= 0) {
  668. dprintk("RPC: %4d call_bind_status (status %d)\n",
  669. task->tk_pid, task->tk_status);
  670. task->tk_status = 0;
  671. task->tk_action = call_connect;
  672. return;
  673. }
  674. switch (task->tk_status) {
  675. case -EACCES:
  676. dprintk("RPC: %4d remote rpcbind: RPC program/version unavailable\n",
  677. task->tk_pid);
  678. rpc_delay(task, 3*HZ);
  679. goto retry_bind;
  680. case -ETIMEDOUT:
  681. dprintk("RPC: %4d rpcbind request timed out\n",
  682. task->tk_pid);
  683. if (RPC_IS_SOFT(task)) {
  684. status = -EIO;
  685. break;
  686. }
  687. goto retry_bind;
  688. case -EPFNOSUPPORT:
  689. dprintk("RPC: %4d remote rpcbind service unavailable\n",
  690. task->tk_pid);
  691. break;
  692. case -EPROTONOSUPPORT:
  693. dprintk("RPC: %4d remote rpcbind version 2 unavailable\n",
  694. task->tk_pid);
  695. break;
  696. default:
  697. dprintk("RPC: %4d unrecognized rpcbind error (%d)\n",
  698. task->tk_pid, -task->tk_status);
  699. status = -EIO;
  700. break;
  701. }
  702. rpc_exit(task, status);
  703. return;
  704. retry_bind:
  705. task->tk_status = 0;
  706. task->tk_action = call_bind;
  707. return;
  708. }
  709. /*
  710. * 4b. Connect to the RPC server
  711. */
  712. static void
  713. call_connect(struct rpc_task *task)
  714. {
  715. struct rpc_xprt *xprt = task->tk_xprt;
  716. dprintk("RPC: %4d call_connect xprt %p %s connected\n",
  717. task->tk_pid, xprt,
  718. (xprt_connected(xprt) ? "is" : "is not"));
  719. task->tk_action = call_transmit;
  720. if (!xprt_connected(xprt)) {
  721. task->tk_action = call_connect_status;
  722. if (task->tk_status < 0)
  723. return;
  724. xprt_connect(task);
  725. }
  726. }
  727. /*
  728. * 4c. Sort out connect result
  729. */
  730. static void
  731. call_connect_status(struct rpc_task *task)
  732. {
  733. struct rpc_clnt *clnt = task->tk_client;
  734. int status = task->tk_status;
  735. dprintk("RPC: %5u call_connect_status (status %d)\n",
  736. task->tk_pid, task->tk_status);
  737. task->tk_status = 0;
  738. if (status >= 0) {
  739. clnt->cl_stats->netreconn++;
  740. task->tk_action = call_transmit;
  741. return;
  742. }
  743. /* Something failed: remote service port may have changed */
  744. if (clnt->cl_autobind)
  745. clnt->cl_port = 0;
  746. switch (status) {
  747. case -ENOTCONN:
  748. case -ETIMEDOUT:
  749. case -EAGAIN:
  750. task->tk_action = call_bind;
  751. break;
  752. default:
  753. rpc_exit(task, -EIO);
  754. break;
  755. }
  756. }
  757. /*
  758. * 5. Transmit the RPC request, and wait for reply
  759. */
  760. static void
  761. call_transmit(struct rpc_task *task)
  762. {
  763. dprintk("RPC: %4d call_transmit (status %d)\n",
  764. task->tk_pid, task->tk_status);
  765. task->tk_action = call_status;
  766. if (task->tk_status < 0)
  767. return;
  768. task->tk_status = xprt_prepare_transmit(task);
  769. if (task->tk_status != 0)
  770. return;
  771. /* Encode here so that rpcsec_gss can use correct sequence number. */
  772. if (rpc_task_need_encode(task)) {
  773. task->tk_rqstp->rq_bytes_sent = 0;
  774. call_encode(task);
  775. /* Did the encode result in an error condition? */
  776. if (task->tk_status != 0)
  777. goto out_nosend;
  778. }
  779. task->tk_action = call_transmit_status;
  780. xprt_transmit(task);
  781. if (task->tk_status < 0)
  782. return;
  783. if (!task->tk_msg.rpc_proc->p_decode) {
  784. task->tk_action = rpc_exit_task;
  785. rpc_wake_up_task(task);
  786. }
  787. return;
  788. out_nosend:
  789. /* release socket write lock before attempting to handle error */
  790. xprt_abort_transmit(task);
  791. rpc_task_force_reencode(task);
  792. }
  793. /*
  794. * 6. Sort out the RPC call status
  795. */
  796. static void
  797. call_status(struct rpc_task *task)
  798. {
  799. struct rpc_clnt *clnt = task->tk_client;
  800. struct rpc_rqst *req = task->tk_rqstp;
  801. int status;
  802. if (req->rq_received > 0 && !req->rq_bytes_sent)
  803. task->tk_status = req->rq_received;
  804. dprintk("RPC: %4d call_status (status %d)\n",
  805. task->tk_pid, task->tk_status);
  806. status = task->tk_status;
  807. if (status >= 0) {
  808. task->tk_action = call_decode;
  809. return;
  810. }
  811. task->tk_status = 0;
  812. switch(status) {
  813. case -ETIMEDOUT:
  814. task->tk_action = call_timeout;
  815. break;
  816. case -ECONNREFUSED:
  817. case -ENOTCONN:
  818. if (clnt->cl_autobind)
  819. clnt->cl_port = 0;
  820. task->tk_action = call_bind;
  821. break;
  822. case -EAGAIN:
  823. task->tk_action = call_transmit;
  824. break;
  825. case -EIO:
  826. /* shutdown or soft timeout */
  827. rpc_exit(task, status);
  828. break;
  829. default:
  830. if (clnt->cl_chatty)
  831. printk("%s: RPC call returned error %d\n",
  832. clnt->cl_protname, -status);
  833. rpc_exit(task, status);
  834. break;
  835. }
  836. }
  837. /*
  838. * 6a. Handle transmission errors.
  839. */
  840. static void
  841. call_transmit_status(struct rpc_task *task)
  842. {
  843. if (task->tk_status != -EAGAIN)
  844. rpc_task_force_reencode(task);
  845. call_status(task);
  846. }
  847. /*
  848. * 6b. Handle RPC timeout
  849. * We do not release the request slot, so we keep using the
  850. * same XID for all retransmits.
  851. */
  852. static void
  853. call_timeout(struct rpc_task *task)
  854. {
  855. struct rpc_clnt *clnt = task->tk_client;
  856. if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
  857. dprintk("RPC: %4d call_timeout (minor)\n", task->tk_pid);
  858. goto retry;
  859. }
  860. dprintk("RPC: %4d call_timeout (major)\n", task->tk_pid);
  861. if (RPC_IS_SOFT(task)) {
  862. if (clnt->cl_chatty)
  863. printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
  864. clnt->cl_protname, clnt->cl_server);
  865. rpc_exit(task, -EIO);
  866. return;
  867. }
  868. if (clnt->cl_chatty && !(task->tk_flags & RPC_CALL_MAJORSEEN)) {
  869. task->tk_flags |= RPC_CALL_MAJORSEEN;
  870. printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
  871. clnt->cl_protname, clnt->cl_server);
  872. }
  873. if (clnt->cl_autobind)
  874. clnt->cl_port = 0;
  875. retry:
  876. clnt->cl_stats->rpcretrans++;
  877. task->tk_action = call_bind;
  878. task->tk_status = 0;
  879. }
  880. /*
  881. * 7. Decode the RPC reply
  882. */
  883. static void
  884. call_decode(struct rpc_task *task)
  885. {
  886. struct rpc_clnt *clnt = task->tk_client;
  887. struct rpc_rqst *req = task->tk_rqstp;
  888. kxdrproc_t decode = task->tk_msg.rpc_proc->p_decode;
  889. u32 *p;
  890. dprintk("RPC: %4d call_decode (status %d)\n",
  891. task->tk_pid, task->tk_status);
  892. if (clnt->cl_chatty && (task->tk_flags & RPC_CALL_MAJORSEEN)) {
  893. printk(KERN_NOTICE "%s: server %s OK\n",
  894. clnt->cl_protname, clnt->cl_server);
  895. task->tk_flags &= ~RPC_CALL_MAJORSEEN;
  896. }
  897. if (task->tk_status < 12) {
  898. if (!RPC_IS_SOFT(task)) {
  899. task->tk_action = call_bind;
  900. clnt->cl_stats->rpcretrans++;
  901. goto out_retry;
  902. }
  903. printk(KERN_WARNING "%s: too small RPC reply size (%d bytes)\n",
  904. clnt->cl_protname, task->tk_status);
  905. rpc_exit(task, -EIO);
  906. return;
  907. }
  908. req->rq_rcv_buf.len = req->rq_private_buf.len;
  909. /* Check that the softirq receive buffer is valid */
  910. WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
  911. sizeof(req->rq_rcv_buf)) != 0);
  912. /* Verify the RPC header */
  913. p = call_verify(task);
  914. if (IS_ERR(p)) {
  915. if (p == ERR_PTR(-EAGAIN))
  916. goto out_retry;
  917. return;
  918. }
  919. task->tk_action = rpc_exit_task;
  920. if (decode)
  921. task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
  922. task->tk_msg.rpc_resp);
  923. dprintk("RPC: %4d call_decode result %d\n", task->tk_pid,
  924. task->tk_status);
  925. return;
  926. out_retry:
  927. req->rq_received = req->rq_private_buf.len = 0;
  928. task->tk_status = 0;
  929. }
  930. /*
  931. * 8. Refresh the credentials if rejected by the server
  932. */
  933. static void
  934. call_refresh(struct rpc_task *task)
  935. {
  936. dprintk("RPC: %4d call_refresh\n", task->tk_pid);
  937. xprt_release(task); /* Must do to obtain new XID */
  938. task->tk_action = call_refreshresult;
  939. task->tk_status = 0;
  940. task->tk_client->cl_stats->rpcauthrefresh++;
  941. rpcauth_refreshcred(task);
  942. }
  943. /*
  944. * 8a. Process the results of a credential refresh
  945. */
  946. static void
  947. call_refreshresult(struct rpc_task *task)
  948. {
  949. int status = task->tk_status;
  950. dprintk("RPC: %4d call_refreshresult (status %d)\n",
  951. task->tk_pid, task->tk_status);
  952. task->tk_status = 0;
  953. task->tk_action = call_reserve;
  954. if (status >= 0 && rpcauth_uptodatecred(task))
  955. return;
  956. if (status == -EACCES) {
  957. rpc_exit(task, -EACCES);
  958. return;
  959. }
  960. task->tk_action = call_refresh;
  961. if (status != -ETIMEDOUT)
  962. rpc_delay(task, 3*HZ);
  963. return;
  964. }
  965. /*
  966. * Call header serialization
  967. */
  968. static u32 *
  969. call_header(struct rpc_task *task)
  970. {
  971. struct rpc_clnt *clnt = task->tk_client;
  972. struct rpc_rqst *req = task->tk_rqstp;
  973. u32 *p = req->rq_svec[0].iov_base;
  974. /* FIXME: check buffer size? */
  975. p = xprt_skip_transport_header(task->tk_xprt, p);
  976. *p++ = req->rq_xid; /* XID */
  977. *p++ = htonl(RPC_CALL); /* CALL */
  978. *p++ = htonl(RPC_VERSION); /* RPC version */
  979. *p++ = htonl(clnt->cl_prog); /* program number */
  980. *p++ = htonl(clnt->cl_vers); /* program version */
  981. *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
  982. p = rpcauth_marshcred(task, p);
  983. req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
  984. return p;
  985. }
  986. /*
  987. * Reply header verification
  988. */
  989. static u32 *
  990. call_verify(struct rpc_task *task)
  991. {
  992. struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
  993. int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
  994. u32 *p = iov->iov_base, n;
  995. int error = -EACCES;
  996. if ((len -= 3) < 0)
  997. goto out_overflow;
  998. p += 1; /* skip XID */
  999. if ((n = ntohl(*p++)) != RPC_REPLY) {
  1000. printk(KERN_WARNING "call_verify: not an RPC reply: %x\n", n);
  1001. goto out_garbage;
  1002. }
  1003. if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
  1004. if (--len < 0)
  1005. goto out_overflow;
  1006. switch ((n = ntohl(*p++))) {
  1007. case RPC_AUTH_ERROR:
  1008. break;
  1009. case RPC_MISMATCH:
  1010. dprintk("%s: RPC call version mismatch!\n", __FUNCTION__);
  1011. error = -EPROTONOSUPPORT;
  1012. goto out_err;
  1013. default:
  1014. dprintk("%s: RPC call rejected, unknown error: %x\n", __FUNCTION__, n);
  1015. goto out_eio;
  1016. }
  1017. if (--len < 0)
  1018. goto out_overflow;
  1019. switch ((n = ntohl(*p++))) {
  1020. case RPC_AUTH_REJECTEDCRED:
  1021. case RPC_AUTH_REJECTEDVERF:
  1022. case RPCSEC_GSS_CREDPROBLEM:
  1023. case RPCSEC_GSS_CTXPROBLEM:
  1024. if (!task->tk_cred_retry)
  1025. break;
  1026. task->tk_cred_retry--;
  1027. dprintk("RPC: %4d call_verify: retry stale creds\n",
  1028. task->tk_pid);
  1029. rpcauth_invalcred(task);
  1030. task->tk_action = call_refresh;
  1031. goto out_retry;
  1032. case RPC_AUTH_BADCRED:
  1033. case RPC_AUTH_BADVERF:
  1034. /* possibly garbled cred/verf? */
  1035. if (!task->tk_garb_retry)
  1036. break;
  1037. task->tk_garb_retry--;
  1038. dprintk("RPC: %4d call_verify: retry garbled creds\n",
  1039. task->tk_pid);
  1040. task->tk_action = call_bind;
  1041. goto out_retry;
  1042. case RPC_AUTH_TOOWEAK:
  1043. printk(KERN_NOTICE "call_verify: server requires stronger "
  1044. "authentication.\n");
  1045. break;
  1046. default:
  1047. printk(KERN_WARNING "call_verify: unknown auth error: %x\n", n);
  1048. error = -EIO;
  1049. }
  1050. dprintk("RPC: %4d call_verify: call rejected %d\n",
  1051. task->tk_pid, n);
  1052. goto out_err;
  1053. }
  1054. if (!(p = rpcauth_checkverf(task, p))) {
  1055. printk(KERN_WARNING "call_verify: auth check failed\n");
  1056. goto out_garbage; /* bad verifier, retry */
  1057. }
  1058. len = p - (u32 *)iov->iov_base - 1;
  1059. if (len < 0)
  1060. goto out_overflow;
  1061. switch ((n = ntohl(*p++))) {
  1062. case RPC_SUCCESS:
  1063. return p;
  1064. case RPC_PROG_UNAVAIL:
  1065. dprintk("RPC: call_verify: program %u is unsupported by server %s\n",
  1066. (unsigned int)task->tk_client->cl_prog,
  1067. task->tk_client->cl_server);
  1068. error = -EPFNOSUPPORT;
  1069. goto out_err;
  1070. case RPC_PROG_MISMATCH:
  1071. dprintk("RPC: call_verify: program %u, version %u unsupported by server %s\n",
  1072. (unsigned int)task->tk_client->cl_prog,
  1073. (unsigned int)task->tk_client->cl_vers,
  1074. task->tk_client->cl_server);
  1075. error = -EPROTONOSUPPORT;
  1076. goto out_err;
  1077. case RPC_PROC_UNAVAIL:
  1078. dprintk("RPC: call_verify: proc %p unsupported by program %u, version %u on server %s\n",
  1079. task->tk_msg.rpc_proc,
  1080. task->tk_client->cl_prog,
  1081. task->tk_client->cl_vers,
  1082. task->tk_client->cl_server);
  1083. error = -EOPNOTSUPP;
  1084. goto out_err;
  1085. case RPC_GARBAGE_ARGS:
  1086. dprintk("RPC: %4d %s: server saw garbage\n", task->tk_pid, __FUNCTION__);
  1087. break; /* retry */
  1088. default:
  1089. printk(KERN_WARNING "call_verify: server accept status: %x\n", n);
  1090. /* Also retry */
  1091. }
  1092. out_garbage:
  1093. task->tk_client->cl_stats->rpcgarbage++;
  1094. if (task->tk_garb_retry) {
  1095. task->tk_garb_retry--;
  1096. dprintk("RPC %s: retrying %4d\n", __FUNCTION__, task->tk_pid);
  1097. task->tk_action = call_bind;
  1098. out_retry:
  1099. return ERR_PTR(-EAGAIN);
  1100. }
  1101. printk(KERN_WARNING "RPC %s: retry failed, exit EIO\n", __FUNCTION__);
  1102. out_eio:
  1103. error = -EIO;
  1104. out_err:
  1105. rpc_exit(task, error);
  1106. return ERR_PTR(error);
  1107. out_overflow:
  1108. printk(KERN_WARNING "RPC %s: server reply was truncated.\n", __FUNCTION__);
  1109. goto out_garbage;
  1110. }
  1111. static int rpcproc_encode_null(void *rqstp, u32 *data, void *obj)
  1112. {
  1113. return 0;
  1114. }
  1115. static int rpcproc_decode_null(void *rqstp, u32 *data, void *obj)
  1116. {
  1117. return 0;
  1118. }
  1119. static struct rpc_procinfo rpcproc_null = {
  1120. .p_encode = rpcproc_encode_null,
  1121. .p_decode = rpcproc_decode_null,
  1122. };
  1123. int rpc_ping(struct rpc_clnt *clnt, int flags)
  1124. {
  1125. struct rpc_message msg = {
  1126. .rpc_proc = &rpcproc_null,
  1127. };
  1128. int err;
  1129. msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
  1130. err = rpc_call_sync(clnt, &msg, flags);
  1131. put_rpccred(msg.rpc_cred);
  1132. return err;
  1133. }