clnt.c 30 KB

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