clnt.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  1. /*
  2. * linux/net/sunrpc/rpcclnt.c
  3. *
  4. * This file contains the high-level RPC interface.
  5. * It is modeled as a finite state machine to support both synchronous
  6. * and asynchronous requests.
  7. *
  8. * - RPC header generation and argument serialization.
  9. * - Credential refresh.
  10. * - TCP connect handling.
  11. * - Retry of operation when it is suspected the operation failed because
  12. * of uid squashing on the server, or when the credentials were stale
  13. * and need to be refreshed, or when a packet was damaged in transit.
  14. * This may be have to be moved to the VFS layer.
  15. *
  16. * NB: BSD uses a more intelligent approach to guessing when a request
  17. * or reply has been lost by keeping the RTO estimate for each procedure.
  18. * We currently make do with a constant timeout value.
  19. *
  20. * Copyright (C) 1992,1993 Rick Sladkey <jrs@world.std.com>
  21. * Copyright (C) 1995,1996 Olaf Kirch <okir@monad.swb.de>
  22. */
  23. #include <asm/system.h>
  24. #include <linux/module.h>
  25. #include <linux/types.h>
  26. #include <linux/mm.h>
  27. #include <linux/slab.h>
  28. #include <linux/in.h>
  29. #include <linux/utsname.h>
  30. #include <linux/sunrpc/clnt.h>
  31. #include <linux/workqueue.h>
  32. #include <linux/sunrpc/rpc_pipe_fs.h>
  33. #include <linux/nfs.h>
  34. #define RPC_SLACK_SPACE (1024) /* total overkill */
  35. #ifdef RPC_DEBUG
  36. # define RPCDBG_FACILITY RPCDBG_CALL
  37. #endif
  38. static DECLARE_WAIT_QUEUE_HEAD(destroy_wait);
  39. static void call_start(struct rpc_task *task);
  40. static void call_reserve(struct rpc_task *task);
  41. static void call_reserveresult(struct rpc_task *task);
  42. static void call_allocate(struct rpc_task *task);
  43. static void call_encode(struct rpc_task *task);
  44. static void call_decode(struct rpc_task *task);
  45. static void call_bind(struct rpc_task *task);
  46. static void call_transmit(struct rpc_task *task);
  47. static void call_status(struct rpc_task *task);
  48. static void call_refresh(struct rpc_task *task);
  49. static void call_refreshresult(struct rpc_task *task);
  50. static void call_timeout(struct rpc_task *task);
  51. static void call_connect(struct rpc_task *task);
  52. static void call_connect_status(struct rpc_task *task);
  53. static u32 * call_header(struct rpc_task *task);
  54. static u32 * call_verify(struct rpc_task *task);
  55. static int
  56. rpc_setup_pipedir(struct rpc_clnt *clnt, char *dir_name)
  57. {
  58. static uint32_t clntid;
  59. int error;
  60. if (dir_name == NULL)
  61. return 0;
  62. for (;;) {
  63. snprintf(clnt->cl_pathname, sizeof(clnt->cl_pathname),
  64. "%s/clnt%x", dir_name,
  65. (unsigned int)clntid++);
  66. clnt->cl_pathname[sizeof(clnt->cl_pathname) - 1] = '\0';
  67. clnt->cl_dentry = rpc_mkdir(clnt->cl_pathname, clnt);
  68. if (!IS_ERR(clnt->cl_dentry))
  69. return 0;
  70. error = PTR_ERR(clnt->cl_dentry);
  71. if (error != -EEXIST) {
  72. printk(KERN_INFO "RPC: Couldn't create pipefs entry %s, error %d\n",
  73. clnt->cl_pathname, error);
  74. return error;
  75. }
  76. }
  77. }
  78. /*
  79. * Create an RPC client
  80. * FIXME: This should also take a flags argument (as in task->tk_flags).
  81. * It's called (among others) from pmap_create_client, which may in
  82. * turn be called by an async task. In this case, rpciod should not be
  83. * made to sleep too long.
  84. */
  85. struct rpc_clnt *
  86. rpc_new_client(struct rpc_xprt *xprt, char *servname,
  87. struct rpc_program *program, u32 vers,
  88. rpc_authflavor_t flavor)
  89. {
  90. struct rpc_version *version;
  91. struct rpc_clnt *clnt = NULL;
  92. 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. if (xprt_connected(xprt))
  460. xprt_sock_setbufsize(xprt);
  461. }
  462. /*
  463. * Return size of largest payload RPC client can support, in bytes
  464. *
  465. * For stream transports, this is one RPC record fragment (see RFC
  466. * 1831), as we don't support multi-record requests yet. For datagram
  467. * transports, this is the size of an IP packet minus the IP, UDP, and
  468. * RPC header sizes.
  469. */
  470. size_t rpc_max_payload(struct rpc_clnt *clnt)
  471. {
  472. return clnt->cl_xprt->max_payload;
  473. }
  474. EXPORT_SYMBOL(rpc_max_payload);
  475. /*
  476. * Restart an (async) RPC call. Usually called from within the
  477. * exit handler.
  478. */
  479. void
  480. rpc_restart_call(struct rpc_task *task)
  481. {
  482. if (RPC_ASSASSINATED(task))
  483. return;
  484. task->tk_action = call_start;
  485. }
  486. /*
  487. * 0. Initial state
  488. *
  489. * Other FSM states can be visited zero or more times, but
  490. * this state is visited exactly once for each RPC.
  491. */
  492. static void
  493. call_start(struct rpc_task *task)
  494. {
  495. struct rpc_clnt *clnt = task->tk_client;
  496. dprintk("RPC: %4d call_start %s%d proc %d (%s)\n", task->tk_pid,
  497. clnt->cl_protname, clnt->cl_vers, task->tk_msg.rpc_proc->p_proc,
  498. (RPC_IS_ASYNC(task) ? "async" : "sync"));
  499. /* Increment call count */
  500. task->tk_msg.rpc_proc->p_count++;
  501. clnt->cl_stats->rpccnt++;
  502. task->tk_action = call_reserve;
  503. }
  504. /*
  505. * 1. Reserve an RPC call slot
  506. */
  507. static void
  508. call_reserve(struct rpc_task *task)
  509. {
  510. dprintk("RPC: %4d call_reserve\n", task->tk_pid);
  511. if (!rpcauth_uptodatecred(task)) {
  512. task->tk_action = call_refresh;
  513. return;
  514. }
  515. task->tk_status = 0;
  516. task->tk_action = call_reserveresult;
  517. xprt_reserve(task);
  518. }
  519. /*
  520. * 1b. Grok the result of xprt_reserve()
  521. */
  522. static void
  523. call_reserveresult(struct rpc_task *task)
  524. {
  525. int status = task->tk_status;
  526. dprintk("RPC: %4d call_reserveresult (status %d)\n",
  527. task->tk_pid, task->tk_status);
  528. /*
  529. * After a call to xprt_reserve(), we must have either
  530. * a request slot or else an error status.
  531. */
  532. task->tk_status = 0;
  533. if (status >= 0) {
  534. if (task->tk_rqstp) {
  535. task->tk_action = call_allocate;
  536. return;
  537. }
  538. printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
  539. __FUNCTION__, status);
  540. rpc_exit(task, -EIO);
  541. return;
  542. }
  543. /*
  544. * Even though there was an error, we may have acquired
  545. * a request slot somehow. Make sure not to leak it.
  546. */
  547. if (task->tk_rqstp) {
  548. printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
  549. __FUNCTION__, status);
  550. xprt_release(task);
  551. }
  552. switch (status) {
  553. case -EAGAIN: /* woken up; retry */
  554. task->tk_action = call_reserve;
  555. return;
  556. case -EIO: /* probably a shutdown */
  557. break;
  558. default:
  559. printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
  560. __FUNCTION__, status);
  561. break;
  562. }
  563. rpc_exit(task, status);
  564. }
  565. /*
  566. * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
  567. * (Note: buffer memory is freed in rpc_task_release).
  568. */
  569. static void
  570. call_allocate(struct rpc_task *task)
  571. {
  572. unsigned int bufsiz;
  573. dprintk("RPC: %4d call_allocate (status %d)\n",
  574. task->tk_pid, task->tk_status);
  575. task->tk_action = call_bind;
  576. if (task->tk_buffer)
  577. return;
  578. /* FIXME: compute buffer requirements more exactly using
  579. * auth->au_wslack */
  580. bufsiz = task->tk_msg.rpc_proc->p_bufsiz + RPC_SLACK_SPACE;
  581. if (rpc_malloc(task, bufsiz << 1) != NULL)
  582. return;
  583. printk(KERN_INFO "RPC: buffer allocation failed for task %p\n", task);
  584. if (RPC_IS_ASYNC(task) || !signalled()) {
  585. xprt_release(task);
  586. task->tk_action = call_reserve;
  587. rpc_delay(task, HZ>>4);
  588. return;
  589. }
  590. rpc_exit(task, -ERESTARTSYS);
  591. }
  592. /*
  593. * 3. Encode arguments of an RPC call
  594. */
  595. static void
  596. call_encode(struct rpc_task *task)
  597. {
  598. struct rpc_clnt *clnt = task->tk_client;
  599. struct rpc_rqst *req = task->tk_rqstp;
  600. struct xdr_buf *sndbuf = &req->rq_snd_buf;
  601. struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
  602. unsigned int bufsiz;
  603. kxdrproc_t encode;
  604. int status;
  605. u32 *p;
  606. dprintk("RPC: %4d call_encode (status %d)\n",
  607. task->tk_pid, task->tk_status);
  608. /* Default buffer setup */
  609. bufsiz = task->tk_bufsize >> 1;
  610. sndbuf->head[0].iov_base = (void *)task->tk_buffer;
  611. sndbuf->head[0].iov_len = bufsiz;
  612. sndbuf->tail[0].iov_len = 0;
  613. sndbuf->page_len = 0;
  614. sndbuf->len = 0;
  615. sndbuf->buflen = bufsiz;
  616. rcvbuf->head[0].iov_base = (void *)((char *)task->tk_buffer + bufsiz);
  617. rcvbuf->head[0].iov_len = bufsiz;
  618. rcvbuf->tail[0].iov_len = 0;
  619. rcvbuf->page_len = 0;
  620. rcvbuf->len = 0;
  621. rcvbuf->buflen = bufsiz;
  622. /* Encode header and provided arguments */
  623. encode = task->tk_msg.rpc_proc->p_encode;
  624. if (!(p = call_header(task))) {
  625. printk(KERN_INFO "RPC: call_header failed, exit EIO\n");
  626. rpc_exit(task, -EIO);
  627. return;
  628. }
  629. if (encode && (status = rpcauth_wrap_req(task, encode, req, p,
  630. task->tk_msg.rpc_argp)) < 0) {
  631. printk(KERN_WARNING "%s: can't encode arguments: %d\n",
  632. clnt->cl_protname, -status);
  633. rpc_exit(task, status);
  634. }
  635. }
  636. /*
  637. * 4. Get the server port number if not yet set
  638. */
  639. static void
  640. call_bind(struct rpc_task *task)
  641. {
  642. struct rpc_clnt *clnt = task->tk_client;
  643. struct rpc_xprt *xprt = clnt->cl_xprt;
  644. dprintk("RPC: %4d call_bind xprt %p %s connected\n", task->tk_pid,
  645. xprt, (xprt_connected(xprt) ? "is" : "is not"));
  646. task->tk_action = (xprt_connected(xprt)) ? call_transmit : call_connect;
  647. if (!clnt->cl_port) {
  648. task->tk_action = call_connect;
  649. task->tk_timeout = RPC_CONNECT_TIMEOUT;
  650. rpc_getport(task, clnt);
  651. }
  652. }
  653. /*
  654. * 4a. Connect to the RPC server (TCP case)
  655. */
  656. static void
  657. call_connect(struct rpc_task *task)
  658. {
  659. struct rpc_clnt *clnt = task->tk_client;
  660. dprintk("RPC: %4d call_connect status %d\n",
  661. task->tk_pid, task->tk_status);
  662. if (xprt_connected(clnt->cl_xprt)) {
  663. task->tk_action = call_transmit;
  664. return;
  665. }
  666. task->tk_action = call_connect_status;
  667. if (task->tk_status < 0)
  668. return;
  669. xprt_connect(task);
  670. }
  671. /*
  672. * 4b. Sort out connect result
  673. */
  674. static void
  675. call_connect_status(struct rpc_task *task)
  676. {
  677. struct rpc_clnt *clnt = task->tk_client;
  678. int status = task->tk_status;
  679. task->tk_status = 0;
  680. if (status >= 0) {
  681. clnt->cl_stats->netreconn++;
  682. task->tk_action = call_transmit;
  683. return;
  684. }
  685. /* Something failed: we may have to rebind */
  686. if (clnt->cl_autobind)
  687. clnt->cl_port = 0;
  688. switch (status) {
  689. case -ENOTCONN:
  690. case -ETIMEDOUT:
  691. case -EAGAIN:
  692. task->tk_action = (clnt->cl_port == 0) ? call_bind : call_connect;
  693. break;
  694. default:
  695. rpc_exit(task, -EIO);
  696. }
  697. }
  698. /*
  699. * 5. Transmit the RPC request, and wait for reply
  700. */
  701. static void
  702. call_transmit(struct rpc_task *task)
  703. {
  704. dprintk("RPC: %4d call_transmit (status %d)\n",
  705. task->tk_pid, task->tk_status);
  706. task->tk_action = call_status;
  707. if (task->tk_status < 0)
  708. return;
  709. task->tk_status = xprt_prepare_transmit(task);
  710. if (task->tk_status != 0)
  711. return;
  712. /* Encode here so that rpcsec_gss can use correct sequence number. */
  713. if (!task->tk_rqstp->rq_bytes_sent)
  714. call_encode(task);
  715. if (task->tk_status < 0)
  716. return;
  717. xprt_transmit(task);
  718. if (task->tk_status < 0)
  719. return;
  720. if (!task->tk_msg.rpc_proc->p_decode) {
  721. task->tk_action = NULL;
  722. rpc_wake_up_task(task);
  723. }
  724. }
  725. /*
  726. * 6. Sort out the RPC call status
  727. */
  728. static void
  729. call_status(struct rpc_task *task)
  730. {
  731. struct rpc_clnt *clnt = task->tk_client;
  732. struct rpc_rqst *req = task->tk_rqstp;
  733. int status;
  734. if (req->rq_received > 0 && !req->rq_bytes_sent)
  735. task->tk_status = req->rq_received;
  736. dprintk("RPC: %4d call_status (status %d)\n",
  737. task->tk_pid, task->tk_status);
  738. status = task->tk_status;
  739. if (status >= 0) {
  740. task->tk_action = call_decode;
  741. return;
  742. }
  743. task->tk_status = 0;
  744. switch(status) {
  745. case -ETIMEDOUT:
  746. task->tk_action = call_timeout;
  747. break;
  748. case -ECONNREFUSED:
  749. case -ENOTCONN:
  750. req->rq_bytes_sent = 0;
  751. if (clnt->cl_autobind)
  752. clnt->cl_port = 0;
  753. task->tk_action = call_bind;
  754. break;
  755. case -EAGAIN:
  756. task->tk_action = call_transmit;
  757. break;
  758. case -EIO:
  759. /* shutdown or soft timeout */
  760. rpc_exit(task, status);
  761. break;
  762. default:
  763. if (clnt->cl_chatty)
  764. printk("%s: RPC call returned error %d\n",
  765. clnt->cl_protname, -status);
  766. rpc_exit(task, status);
  767. break;
  768. }
  769. }
  770. /*
  771. * 6a. Handle RPC timeout
  772. * We do not release the request slot, so we keep using the
  773. * same XID for all retransmits.
  774. */
  775. static void
  776. call_timeout(struct rpc_task *task)
  777. {
  778. struct rpc_clnt *clnt = task->tk_client;
  779. if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
  780. dprintk("RPC: %4d call_timeout (minor)\n", task->tk_pid);
  781. goto retry;
  782. }
  783. dprintk("RPC: %4d call_timeout (major)\n", task->tk_pid);
  784. if (RPC_IS_SOFT(task)) {
  785. if (clnt->cl_chatty)
  786. printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
  787. clnt->cl_protname, clnt->cl_server);
  788. rpc_exit(task, -EIO);
  789. return;
  790. }
  791. if (clnt->cl_chatty && !(task->tk_flags & RPC_CALL_MAJORSEEN)) {
  792. task->tk_flags |= RPC_CALL_MAJORSEEN;
  793. printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
  794. clnt->cl_protname, clnt->cl_server);
  795. }
  796. if (clnt->cl_autobind)
  797. clnt->cl_port = 0;
  798. retry:
  799. clnt->cl_stats->rpcretrans++;
  800. task->tk_action = call_bind;
  801. task->tk_status = 0;
  802. }
  803. /*
  804. * 7. Decode the RPC reply
  805. */
  806. static void
  807. call_decode(struct rpc_task *task)
  808. {
  809. struct rpc_clnt *clnt = task->tk_client;
  810. struct rpc_rqst *req = task->tk_rqstp;
  811. kxdrproc_t decode = task->tk_msg.rpc_proc->p_decode;
  812. u32 *p;
  813. dprintk("RPC: %4d call_decode (status %d)\n",
  814. task->tk_pid, task->tk_status);
  815. if (clnt->cl_chatty && (task->tk_flags & RPC_CALL_MAJORSEEN)) {
  816. printk(KERN_NOTICE "%s: server %s OK\n",
  817. clnt->cl_protname, clnt->cl_server);
  818. task->tk_flags &= ~RPC_CALL_MAJORSEEN;
  819. }
  820. if (task->tk_status < 12) {
  821. if (!RPC_IS_SOFT(task)) {
  822. task->tk_action = call_bind;
  823. clnt->cl_stats->rpcretrans++;
  824. goto out_retry;
  825. }
  826. printk(KERN_WARNING "%s: too small RPC reply size (%d bytes)\n",
  827. clnt->cl_protname, task->tk_status);
  828. rpc_exit(task, -EIO);
  829. return;
  830. }
  831. req->rq_rcv_buf.len = req->rq_private_buf.len;
  832. /* Check that the softirq receive buffer is valid */
  833. WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
  834. sizeof(req->rq_rcv_buf)) != 0);
  835. /* Verify the RPC header */
  836. if (!(p = call_verify(task))) {
  837. if (task->tk_action == NULL)
  838. return;
  839. goto out_retry;
  840. }
  841. task->tk_action = NULL;
  842. if (decode)
  843. task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
  844. task->tk_msg.rpc_resp);
  845. dprintk("RPC: %4d call_decode result %d\n", task->tk_pid,
  846. task->tk_status);
  847. return;
  848. out_retry:
  849. req->rq_received = req->rq_private_buf.len = 0;
  850. task->tk_status = 0;
  851. }
  852. /*
  853. * 8. Refresh the credentials if rejected by the server
  854. */
  855. static void
  856. call_refresh(struct rpc_task *task)
  857. {
  858. dprintk("RPC: %4d call_refresh\n", task->tk_pid);
  859. xprt_release(task); /* Must do to obtain new XID */
  860. task->tk_action = call_refreshresult;
  861. task->tk_status = 0;
  862. task->tk_client->cl_stats->rpcauthrefresh++;
  863. rpcauth_refreshcred(task);
  864. }
  865. /*
  866. * 8a. Process the results of a credential refresh
  867. */
  868. static void
  869. call_refreshresult(struct rpc_task *task)
  870. {
  871. int status = task->tk_status;
  872. dprintk("RPC: %4d call_refreshresult (status %d)\n",
  873. task->tk_pid, task->tk_status);
  874. task->tk_status = 0;
  875. task->tk_action = call_reserve;
  876. if (status >= 0 && rpcauth_uptodatecred(task))
  877. return;
  878. if (status == -EACCES) {
  879. rpc_exit(task, -EACCES);
  880. return;
  881. }
  882. task->tk_action = call_refresh;
  883. if (status != -ETIMEDOUT)
  884. rpc_delay(task, 3*HZ);
  885. return;
  886. }
  887. /*
  888. * Call header serialization
  889. */
  890. static u32 *
  891. call_header(struct rpc_task *task)
  892. {
  893. struct rpc_clnt *clnt = task->tk_client;
  894. struct rpc_xprt *xprt = clnt->cl_xprt;
  895. struct rpc_rqst *req = task->tk_rqstp;
  896. u32 *p = req->rq_svec[0].iov_base;
  897. /* FIXME: check buffer size? */
  898. if (xprt->stream)
  899. *p++ = 0; /* fill in later */
  900. *p++ = req->rq_xid; /* XID */
  901. *p++ = htonl(RPC_CALL); /* CALL */
  902. *p++ = htonl(RPC_VERSION); /* RPC version */
  903. *p++ = htonl(clnt->cl_prog); /* program number */
  904. *p++ = htonl(clnt->cl_vers); /* program version */
  905. *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
  906. p = rpcauth_marshcred(task, p);
  907. req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
  908. return p;
  909. }
  910. /*
  911. * Reply header verification
  912. */
  913. static u32 *
  914. call_verify(struct rpc_task *task)
  915. {
  916. struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
  917. int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
  918. u32 *p = iov->iov_base, n;
  919. int error = -EACCES;
  920. if ((len -= 3) < 0)
  921. goto out_overflow;
  922. p += 1; /* skip XID */
  923. if ((n = ntohl(*p++)) != RPC_REPLY) {
  924. printk(KERN_WARNING "call_verify: not an RPC reply: %x\n", n);
  925. goto out_retry;
  926. }
  927. if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
  928. if (--len < 0)
  929. goto out_overflow;
  930. switch ((n = ntohl(*p++))) {
  931. case RPC_AUTH_ERROR:
  932. break;
  933. case RPC_MISMATCH:
  934. dprintk("%s: RPC call version mismatch!\n", __FUNCTION__);
  935. error = -EPROTONOSUPPORT;
  936. goto out_err;
  937. default:
  938. dprintk("%s: RPC call rejected, unknown error: %x\n", __FUNCTION__, n);
  939. goto out_eio;
  940. }
  941. if (--len < 0)
  942. goto out_overflow;
  943. switch ((n = ntohl(*p++))) {
  944. case RPC_AUTH_REJECTEDCRED:
  945. case RPC_AUTH_REJECTEDVERF:
  946. case RPCSEC_GSS_CREDPROBLEM:
  947. case RPCSEC_GSS_CTXPROBLEM:
  948. if (!task->tk_cred_retry)
  949. break;
  950. task->tk_cred_retry--;
  951. dprintk("RPC: %4d call_verify: retry stale creds\n",
  952. task->tk_pid);
  953. rpcauth_invalcred(task);
  954. task->tk_action = call_refresh;
  955. return NULL;
  956. case RPC_AUTH_BADCRED:
  957. case RPC_AUTH_BADVERF:
  958. /* possibly garbled cred/verf? */
  959. if (!task->tk_garb_retry)
  960. break;
  961. task->tk_garb_retry--;
  962. dprintk("RPC: %4d call_verify: retry garbled creds\n",
  963. task->tk_pid);
  964. task->tk_action = call_bind;
  965. return NULL;
  966. case RPC_AUTH_TOOWEAK:
  967. printk(KERN_NOTICE "call_verify: server requires stronger "
  968. "authentication.\n");
  969. break;
  970. default:
  971. printk(KERN_WARNING "call_verify: unknown auth error: %x\n", n);
  972. error = -EIO;
  973. }
  974. dprintk("RPC: %4d call_verify: call rejected %d\n",
  975. task->tk_pid, n);
  976. goto out_err;
  977. }
  978. if (!(p = rpcauth_checkverf(task, p))) {
  979. printk(KERN_WARNING "call_verify: auth check failed\n");
  980. goto out_retry; /* bad verifier, retry */
  981. }
  982. len = p - (u32 *)iov->iov_base - 1;
  983. if (len < 0)
  984. goto out_overflow;
  985. switch ((n = ntohl(*p++))) {
  986. case RPC_SUCCESS:
  987. return p;
  988. case RPC_PROG_UNAVAIL:
  989. dprintk("RPC: call_verify: program %u is unsupported by server %s\n",
  990. (unsigned int)task->tk_client->cl_prog,
  991. task->tk_client->cl_server);
  992. error = -EPFNOSUPPORT;
  993. goto out_err;
  994. case RPC_PROG_MISMATCH:
  995. dprintk("RPC: call_verify: program %u, version %u unsupported by server %s\n",
  996. (unsigned int)task->tk_client->cl_prog,
  997. (unsigned int)task->tk_client->cl_vers,
  998. task->tk_client->cl_server);
  999. error = -EPROTONOSUPPORT;
  1000. goto out_err;
  1001. case RPC_PROC_UNAVAIL:
  1002. dprintk("RPC: call_verify: proc %p unsupported by program %u, version %u on server %s\n",
  1003. task->tk_msg.rpc_proc,
  1004. task->tk_client->cl_prog,
  1005. task->tk_client->cl_vers,
  1006. task->tk_client->cl_server);
  1007. error = -EOPNOTSUPP;
  1008. goto out_err;
  1009. case RPC_GARBAGE_ARGS:
  1010. dprintk("RPC: %4d %s: server saw garbage\n", task->tk_pid, __FUNCTION__);
  1011. break; /* retry */
  1012. default:
  1013. printk(KERN_WARNING "call_verify: server accept status: %x\n", n);
  1014. /* Also retry */
  1015. }
  1016. out_retry:
  1017. task->tk_client->cl_stats->rpcgarbage++;
  1018. if (task->tk_garb_retry) {
  1019. task->tk_garb_retry--;
  1020. dprintk("RPC %s: retrying %4d\n", __FUNCTION__, task->tk_pid);
  1021. task->tk_action = call_bind;
  1022. return NULL;
  1023. }
  1024. printk(KERN_WARNING "RPC %s: retry failed, exit EIO\n", __FUNCTION__);
  1025. out_eio:
  1026. error = -EIO;
  1027. out_err:
  1028. rpc_exit(task, error);
  1029. return NULL;
  1030. out_overflow:
  1031. printk(KERN_WARNING "RPC %s: server reply was truncated.\n", __FUNCTION__);
  1032. goto out_retry;
  1033. }
  1034. static int rpcproc_encode_null(void *rqstp, u32 *data, void *obj)
  1035. {
  1036. return 0;
  1037. }
  1038. static int rpcproc_decode_null(void *rqstp, u32 *data, void *obj)
  1039. {
  1040. return 0;
  1041. }
  1042. static struct rpc_procinfo rpcproc_null = {
  1043. .p_encode = rpcproc_encode_null,
  1044. .p_decode = rpcproc_decode_null,
  1045. };
  1046. int rpc_ping(struct rpc_clnt *clnt, int flags)
  1047. {
  1048. struct rpc_message msg = {
  1049. .rpc_proc = &rpcproc_null,
  1050. };
  1051. int err;
  1052. msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
  1053. err = rpc_call_sync(clnt, &msg, flags);
  1054. put_rpccred(msg.rpc_cred);
  1055. return err;
  1056. }