clnt.c 33 KB

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