clnt.c 34 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_put(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. new->cl_xprt = xprt_get(clnt->cl_xprt);
  228. /* Turn off autobind on clones */
  229. new->cl_autobind = 0;
  230. new->cl_oneshot = 0;
  231. new->cl_dead = 0;
  232. if (!IS_ERR(new->cl_dentry))
  233. dget(new->cl_dentry);
  234. rpc_init_rtt(&new->cl_rtt_default, clnt->cl_xprt->timeout.to_initval);
  235. if (new->cl_auth)
  236. atomic_inc(&new->cl_auth->au_count);
  237. new->cl_metrics = rpc_alloc_iostats(clnt);
  238. return new;
  239. out_no_clnt:
  240. printk(KERN_INFO "RPC: out of memory in %s\n", __FUNCTION__);
  241. return ERR_PTR(-ENOMEM);
  242. }
  243. /*
  244. * Properly shut down an RPC client, terminating all outstanding
  245. * requests. Note that we must be certain that cl_oneshot and
  246. * cl_dead are cleared, or else the client would be destroyed
  247. * when the last task releases it.
  248. */
  249. int
  250. rpc_shutdown_client(struct rpc_clnt *clnt)
  251. {
  252. dprintk("RPC: shutting down %s client for %s, tasks=%d\n",
  253. clnt->cl_protname, clnt->cl_server,
  254. atomic_read(&clnt->cl_users));
  255. while (atomic_read(&clnt->cl_users) > 0) {
  256. /* Don't let rpc_release_client destroy us */
  257. clnt->cl_oneshot = 0;
  258. clnt->cl_dead = 0;
  259. rpc_killall_tasks(clnt);
  260. wait_event_timeout(destroy_wait,
  261. !atomic_read(&clnt->cl_users), 1*HZ);
  262. }
  263. if (atomic_read(&clnt->cl_users) < 0) {
  264. printk(KERN_ERR "RPC: rpc_shutdown_client clnt %p tasks=%d\n",
  265. clnt, atomic_read(&clnt->cl_users));
  266. #ifdef RPC_DEBUG
  267. rpc_show_tasks();
  268. #endif
  269. BUG();
  270. }
  271. return rpc_destroy_client(clnt);
  272. }
  273. /*
  274. * Delete an RPC client
  275. */
  276. int
  277. rpc_destroy_client(struct rpc_clnt *clnt)
  278. {
  279. if (!atomic_dec_and_test(&clnt->cl_count))
  280. return 1;
  281. BUG_ON(atomic_read(&clnt->cl_users) != 0);
  282. dprintk("RPC: destroying %s client for %s\n",
  283. clnt->cl_protname, clnt->cl_server);
  284. if (clnt->cl_auth) {
  285. rpcauth_destroy(clnt->cl_auth);
  286. clnt->cl_auth = NULL;
  287. }
  288. if (clnt->cl_parent != clnt) {
  289. if (!IS_ERR(clnt->cl_dentry))
  290. dput(clnt->cl_dentry);
  291. rpc_destroy_client(clnt->cl_parent);
  292. goto out_free;
  293. }
  294. if (!IS_ERR(clnt->cl_dentry)) {
  295. rpc_rmdir(clnt->cl_dentry);
  296. rpc_put_mount();
  297. }
  298. if (clnt->cl_server != clnt->cl_inline_name)
  299. kfree(clnt->cl_server);
  300. out_free:
  301. rpc_free_iostats(clnt->cl_metrics);
  302. clnt->cl_metrics = NULL;
  303. xprt_put(clnt->cl_xprt);
  304. kfree(clnt);
  305. return 0;
  306. }
  307. /*
  308. * Release an RPC client
  309. */
  310. void
  311. rpc_release_client(struct rpc_clnt *clnt)
  312. {
  313. dprintk("RPC: rpc_release_client(%p, %d)\n",
  314. clnt, atomic_read(&clnt->cl_users));
  315. if (!atomic_dec_and_test(&clnt->cl_users))
  316. return;
  317. wake_up(&destroy_wait);
  318. if (clnt->cl_oneshot || clnt->cl_dead)
  319. rpc_destroy_client(clnt);
  320. }
  321. /**
  322. * rpc_bind_new_program - bind a new RPC program to an existing client
  323. * @old - old rpc_client
  324. * @program - rpc program to set
  325. * @vers - rpc program version
  326. *
  327. * Clones the rpc client and sets up a new RPC program. This is mainly
  328. * of use for enabling different RPC programs to share the same transport.
  329. * The Sun NFSv2/v3 ACL protocol can do this.
  330. */
  331. struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
  332. struct rpc_program *program,
  333. int vers)
  334. {
  335. struct rpc_clnt *clnt;
  336. struct rpc_version *version;
  337. int err;
  338. BUG_ON(vers >= program->nrvers || !program->version[vers]);
  339. version = program->version[vers];
  340. clnt = rpc_clone_client(old);
  341. if (IS_ERR(clnt))
  342. goto out;
  343. clnt->cl_procinfo = version->procs;
  344. clnt->cl_maxproc = version->nrprocs;
  345. clnt->cl_protname = program->name;
  346. clnt->cl_prog = program->number;
  347. clnt->cl_vers = version->number;
  348. clnt->cl_stats = program->stats;
  349. err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
  350. if (err != 0) {
  351. rpc_shutdown_client(clnt);
  352. clnt = ERR_PTR(err);
  353. }
  354. out:
  355. return clnt;
  356. }
  357. /*
  358. * Default callback for async RPC calls
  359. */
  360. static void
  361. rpc_default_callback(struct rpc_task *task, void *data)
  362. {
  363. }
  364. static const struct rpc_call_ops rpc_default_ops = {
  365. .rpc_call_done = rpc_default_callback,
  366. };
  367. /*
  368. * Export the signal mask handling for synchronous code that
  369. * sleeps on RPC calls
  370. */
  371. #define RPC_INTR_SIGNALS (sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTERM))
  372. static void rpc_save_sigmask(sigset_t *oldset, int intr)
  373. {
  374. unsigned long sigallow = sigmask(SIGKILL);
  375. sigset_t sigmask;
  376. /* Block all signals except those listed in sigallow */
  377. if (intr)
  378. sigallow |= RPC_INTR_SIGNALS;
  379. siginitsetinv(&sigmask, sigallow);
  380. sigprocmask(SIG_BLOCK, &sigmask, oldset);
  381. }
  382. static inline void rpc_task_sigmask(struct rpc_task *task, sigset_t *oldset)
  383. {
  384. rpc_save_sigmask(oldset, !RPC_TASK_UNINTERRUPTIBLE(task));
  385. }
  386. static inline void rpc_restore_sigmask(sigset_t *oldset)
  387. {
  388. sigprocmask(SIG_SETMASK, oldset, NULL);
  389. }
  390. void rpc_clnt_sigmask(struct rpc_clnt *clnt, sigset_t *oldset)
  391. {
  392. rpc_save_sigmask(oldset, clnt->cl_intr);
  393. }
  394. void rpc_clnt_sigunmask(struct rpc_clnt *clnt, sigset_t *oldset)
  395. {
  396. rpc_restore_sigmask(oldset);
  397. }
  398. /*
  399. * New rpc_call implementation
  400. */
  401. int rpc_call_sync(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
  402. {
  403. struct rpc_task *task;
  404. sigset_t oldset;
  405. int status;
  406. /* If this client is slain all further I/O fails */
  407. if (clnt->cl_dead)
  408. return -EIO;
  409. BUG_ON(flags & RPC_TASK_ASYNC);
  410. status = -ENOMEM;
  411. task = rpc_new_task(clnt, flags, &rpc_default_ops, NULL);
  412. if (task == NULL)
  413. goto out;
  414. /* Mask signals on RPC calls _and_ GSS_AUTH upcalls */
  415. rpc_task_sigmask(task, &oldset);
  416. rpc_call_setup(task, msg, 0);
  417. /* Set up the call info struct and execute the task */
  418. status = task->tk_status;
  419. if (status == 0) {
  420. atomic_inc(&task->tk_count);
  421. status = rpc_execute(task);
  422. if (status == 0)
  423. status = task->tk_status;
  424. }
  425. rpc_restore_sigmask(&oldset);
  426. rpc_release_task(task);
  427. out:
  428. return status;
  429. }
  430. /*
  431. * New rpc_call implementation
  432. */
  433. int
  434. rpc_call_async(struct rpc_clnt *clnt, struct rpc_message *msg, int flags,
  435. const struct rpc_call_ops *tk_ops, void *data)
  436. {
  437. struct rpc_task *task;
  438. sigset_t oldset;
  439. int status;
  440. /* If this client is slain all further I/O fails */
  441. status = -EIO;
  442. if (clnt->cl_dead)
  443. goto out_release;
  444. flags |= RPC_TASK_ASYNC;
  445. /* Create/initialize a new RPC task */
  446. status = -ENOMEM;
  447. if (!(task = rpc_new_task(clnt, flags, tk_ops, data)))
  448. goto out_release;
  449. /* Mask signals on GSS_AUTH upcalls */
  450. rpc_task_sigmask(task, &oldset);
  451. rpc_call_setup(task, msg, 0);
  452. /* Set up the call info struct and execute the task */
  453. status = task->tk_status;
  454. if (status == 0)
  455. rpc_execute(task);
  456. else
  457. rpc_release_task(task);
  458. rpc_restore_sigmask(&oldset);
  459. return status;
  460. out_release:
  461. if (tk_ops->rpc_release != NULL)
  462. tk_ops->rpc_release(data);
  463. return status;
  464. }
  465. void
  466. rpc_call_setup(struct rpc_task *task, struct rpc_message *msg, int flags)
  467. {
  468. task->tk_msg = *msg;
  469. task->tk_flags |= flags;
  470. /* Bind the user cred */
  471. if (task->tk_msg.rpc_cred != NULL)
  472. rpcauth_holdcred(task);
  473. else
  474. rpcauth_bindcred(task);
  475. if (task->tk_status == 0)
  476. task->tk_action = call_start;
  477. else
  478. task->tk_action = rpc_exit_task;
  479. }
  480. /**
  481. * rpc_peeraddr - extract remote peer address from clnt's xprt
  482. * @clnt: RPC client structure
  483. * @buf: target buffer
  484. * @size: length of target buffer
  485. *
  486. * Returns the number of bytes that are actually in the stored address.
  487. */
  488. size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
  489. {
  490. size_t bytes;
  491. struct rpc_xprt *xprt = clnt->cl_xprt;
  492. bytes = sizeof(xprt->addr);
  493. if (bytes > bufsize)
  494. bytes = bufsize;
  495. memcpy(buf, &clnt->cl_xprt->addr, bytes);
  496. return xprt->addrlen;
  497. }
  498. EXPORT_SYMBOL_GPL(rpc_peeraddr);
  499. /**
  500. * rpc_peeraddr2str - return remote peer address in printable format
  501. * @clnt: RPC client structure
  502. * @format: address format
  503. *
  504. */
  505. char *rpc_peeraddr2str(struct rpc_clnt *clnt, enum rpc_display_format_t format)
  506. {
  507. struct rpc_xprt *xprt = clnt->cl_xprt;
  508. return xprt->ops->print_addr(xprt, format);
  509. }
  510. EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
  511. void
  512. rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
  513. {
  514. struct rpc_xprt *xprt = clnt->cl_xprt;
  515. if (xprt->ops->set_buffer_size)
  516. xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
  517. }
  518. /*
  519. * Return size of largest payload RPC client can support, in bytes
  520. *
  521. * For stream transports, this is one RPC record fragment (see RFC
  522. * 1831), as we don't support multi-record requests yet. For datagram
  523. * transports, this is the size of an IP packet minus the IP, UDP, and
  524. * RPC header sizes.
  525. */
  526. size_t rpc_max_payload(struct rpc_clnt *clnt)
  527. {
  528. return clnt->cl_xprt->max_payload;
  529. }
  530. EXPORT_SYMBOL_GPL(rpc_max_payload);
  531. /**
  532. * rpc_force_rebind - force transport to check that remote port is unchanged
  533. * @clnt: client to rebind
  534. *
  535. */
  536. void rpc_force_rebind(struct rpc_clnt *clnt)
  537. {
  538. if (clnt->cl_autobind)
  539. xprt_clear_bound(clnt->cl_xprt);
  540. }
  541. EXPORT_SYMBOL_GPL(rpc_force_rebind);
  542. /*
  543. * Restart an (async) RPC call. Usually called from within the
  544. * exit handler.
  545. */
  546. void
  547. rpc_restart_call(struct rpc_task *task)
  548. {
  549. if (RPC_ASSASSINATED(task))
  550. return;
  551. task->tk_action = call_start;
  552. }
  553. /*
  554. * 0. Initial state
  555. *
  556. * Other FSM states can be visited zero or more times, but
  557. * this state is visited exactly once for each RPC.
  558. */
  559. static void
  560. call_start(struct rpc_task *task)
  561. {
  562. struct rpc_clnt *clnt = task->tk_client;
  563. dprintk("RPC: %4d call_start %s%d proc %d (%s)\n", task->tk_pid,
  564. clnt->cl_protname, clnt->cl_vers, task->tk_msg.rpc_proc->p_proc,
  565. (RPC_IS_ASYNC(task) ? "async" : "sync"));
  566. /* Increment call count */
  567. task->tk_msg.rpc_proc->p_count++;
  568. clnt->cl_stats->rpccnt++;
  569. task->tk_action = call_reserve;
  570. }
  571. /*
  572. * 1. Reserve an RPC call slot
  573. */
  574. static void
  575. call_reserve(struct rpc_task *task)
  576. {
  577. dprintk("RPC: %4d call_reserve\n", task->tk_pid);
  578. if (!rpcauth_uptodatecred(task)) {
  579. task->tk_action = call_refresh;
  580. return;
  581. }
  582. task->tk_status = 0;
  583. task->tk_action = call_reserveresult;
  584. xprt_reserve(task);
  585. }
  586. /*
  587. * 1b. Grok the result of xprt_reserve()
  588. */
  589. static void
  590. call_reserveresult(struct rpc_task *task)
  591. {
  592. int status = task->tk_status;
  593. dprintk("RPC: %4d call_reserveresult (status %d)\n",
  594. task->tk_pid, task->tk_status);
  595. /*
  596. * After a call to xprt_reserve(), we must have either
  597. * a request slot or else an error status.
  598. */
  599. task->tk_status = 0;
  600. if (status >= 0) {
  601. if (task->tk_rqstp) {
  602. task->tk_action = call_allocate;
  603. return;
  604. }
  605. printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
  606. __FUNCTION__, status);
  607. rpc_exit(task, -EIO);
  608. return;
  609. }
  610. /*
  611. * Even though there was an error, we may have acquired
  612. * a request slot somehow. Make sure not to leak it.
  613. */
  614. if (task->tk_rqstp) {
  615. printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
  616. __FUNCTION__, status);
  617. xprt_release(task);
  618. }
  619. switch (status) {
  620. case -EAGAIN: /* woken up; retry */
  621. task->tk_action = call_reserve;
  622. return;
  623. case -EIO: /* probably a shutdown */
  624. break;
  625. default:
  626. printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
  627. __FUNCTION__, status);
  628. break;
  629. }
  630. rpc_exit(task, status);
  631. }
  632. /*
  633. * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
  634. * (Note: buffer memory is freed in xprt_release).
  635. */
  636. static void
  637. call_allocate(struct rpc_task *task)
  638. {
  639. struct rpc_rqst *req = task->tk_rqstp;
  640. struct rpc_xprt *xprt = task->tk_xprt;
  641. unsigned int bufsiz;
  642. dprintk("RPC: %4d call_allocate (status %d)\n",
  643. task->tk_pid, task->tk_status);
  644. task->tk_action = call_bind;
  645. if (req->rq_buffer)
  646. return;
  647. /* FIXME: compute buffer requirements more exactly using
  648. * auth->au_wslack */
  649. bufsiz = task->tk_msg.rpc_proc->p_bufsiz + RPC_SLACK_SPACE;
  650. if (xprt->ops->buf_alloc(task, bufsiz << 1) != NULL)
  651. return;
  652. printk(KERN_INFO "RPC: buffer allocation failed for task %p\n", task);
  653. if (RPC_IS_ASYNC(task) || !signalled()) {
  654. xprt_release(task);
  655. task->tk_action = call_reserve;
  656. rpc_delay(task, HZ>>4);
  657. return;
  658. }
  659. rpc_exit(task, -ERESTARTSYS);
  660. }
  661. static inline int
  662. rpc_task_need_encode(struct rpc_task *task)
  663. {
  664. return task->tk_rqstp->rq_snd_buf.len == 0;
  665. }
  666. static inline void
  667. rpc_task_force_reencode(struct rpc_task *task)
  668. {
  669. task->tk_rqstp->rq_snd_buf.len = 0;
  670. }
  671. /*
  672. * 3. Encode arguments of an RPC call
  673. */
  674. static void
  675. call_encode(struct rpc_task *task)
  676. {
  677. struct rpc_rqst *req = task->tk_rqstp;
  678. struct xdr_buf *sndbuf = &req->rq_snd_buf;
  679. struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
  680. unsigned int bufsiz;
  681. kxdrproc_t encode;
  682. u32 *p;
  683. dprintk("RPC: %4d call_encode (status %d)\n",
  684. task->tk_pid, task->tk_status);
  685. /* Default buffer setup */
  686. bufsiz = req->rq_bufsize >> 1;
  687. sndbuf->head[0].iov_base = (void *)req->rq_buffer;
  688. sndbuf->head[0].iov_len = bufsiz;
  689. sndbuf->tail[0].iov_len = 0;
  690. sndbuf->page_len = 0;
  691. sndbuf->len = 0;
  692. sndbuf->buflen = bufsiz;
  693. rcvbuf->head[0].iov_base = (void *)((char *)req->rq_buffer + bufsiz);
  694. rcvbuf->head[0].iov_len = bufsiz;
  695. rcvbuf->tail[0].iov_len = 0;
  696. rcvbuf->page_len = 0;
  697. rcvbuf->len = 0;
  698. rcvbuf->buflen = bufsiz;
  699. /* Encode header and provided arguments */
  700. encode = task->tk_msg.rpc_proc->p_encode;
  701. if (!(p = call_header(task))) {
  702. printk(KERN_INFO "RPC: call_header failed, exit EIO\n");
  703. rpc_exit(task, -EIO);
  704. return;
  705. }
  706. if (encode == NULL)
  707. return;
  708. task->tk_status = rpcauth_wrap_req(task, encode, req, p,
  709. task->tk_msg.rpc_argp);
  710. if (task->tk_status == -ENOMEM) {
  711. /* XXX: Is this sane? */
  712. rpc_delay(task, 3*HZ);
  713. task->tk_status = -EAGAIN;
  714. }
  715. }
  716. /*
  717. * 4. Get the server port number if not yet set
  718. */
  719. static void
  720. call_bind(struct rpc_task *task)
  721. {
  722. struct rpc_xprt *xprt = task->tk_xprt;
  723. dprintk("RPC: %4d call_bind (status %d)\n",
  724. task->tk_pid, task->tk_status);
  725. task->tk_action = call_connect;
  726. if (!xprt_bound(xprt)) {
  727. task->tk_action = call_bind_status;
  728. task->tk_timeout = xprt->bind_timeout;
  729. xprt->ops->rpcbind(task);
  730. }
  731. }
  732. /*
  733. * 4a. Sort out bind result
  734. */
  735. static void
  736. call_bind_status(struct rpc_task *task)
  737. {
  738. int status = -EACCES;
  739. if (task->tk_status >= 0) {
  740. dprintk("RPC: %4d call_bind_status (status %d)\n",
  741. task->tk_pid, task->tk_status);
  742. task->tk_status = 0;
  743. task->tk_action = call_connect;
  744. return;
  745. }
  746. switch (task->tk_status) {
  747. case -EACCES:
  748. dprintk("RPC: %4d remote rpcbind: RPC program/version unavailable\n",
  749. task->tk_pid);
  750. rpc_delay(task, 3*HZ);
  751. goto retry_timeout;
  752. case -ETIMEDOUT:
  753. dprintk("RPC: %4d rpcbind request timed out\n",
  754. task->tk_pid);
  755. goto retry_timeout;
  756. case -EPFNOSUPPORT:
  757. dprintk("RPC: %4d remote rpcbind service unavailable\n",
  758. task->tk_pid);
  759. break;
  760. case -EPROTONOSUPPORT:
  761. dprintk("RPC: %4d remote rpcbind version 2 unavailable\n",
  762. task->tk_pid);
  763. break;
  764. default:
  765. dprintk("RPC: %4d unrecognized rpcbind error (%d)\n",
  766. task->tk_pid, -task->tk_status);
  767. status = -EIO;
  768. }
  769. rpc_exit(task, status);
  770. return;
  771. retry_timeout:
  772. task->tk_action = call_timeout;
  773. }
  774. /*
  775. * 4b. Connect to the RPC server
  776. */
  777. static void
  778. call_connect(struct rpc_task *task)
  779. {
  780. struct rpc_xprt *xprt = task->tk_xprt;
  781. dprintk("RPC: %4d call_connect xprt %p %s connected\n",
  782. task->tk_pid, xprt,
  783. (xprt_connected(xprt) ? "is" : "is not"));
  784. task->tk_action = call_transmit;
  785. if (!xprt_connected(xprt)) {
  786. task->tk_action = call_connect_status;
  787. if (task->tk_status < 0)
  788. return;
  789. xprt_connect(task);
  790. }
  791. }
  792. /*
  793. * 4c. Sort out connect result
  794. */
  795. static void
  796. call_connect_status(struct rpc_task *task)
  797. {
  798. struct rpc_clnt *clnt = task->tk_client;
  799. int status = task->tk_status;
  800. dprintk("RPC: %5u call_connect_status (status %d)\n",
  801. task->tk_pid, task->tk_status);
  802. task->tk_status = 0;
  803. if (status >= 0) {
  804. clnt->cl_stats->netreconn++;
  805. task->tk_action = call_transmit;
  806. return;
  807. }
  808. /* Something failed: remote service port may have changed */
  809. rpc_force_rebind(clnt);
  810. switch (status) {
  811. case -ENOTCONN:
  812. case -EAGAIN:
  813. task->tk_action = call_bind;
  814. if (!RPC_IS_SOFT(task))
  815. return;
  816. /* if soft mounted, test if we've timed out */
  817. case -ETIMEDOUT:
  818. task->tk_action = call_timeout;
  819. return;
  820. }
  821. rpc_exit(task, -EIO);
  822. }
  823. /*
  824. * 5. Transmit the RPC request, and wait for reply
  825. */
  826. static void
  827. call_transmit(struct rpc_task *task)
  828. {
  829. dprintk("RPC: %4d call_transmit (status %d)\n",
  830. task->tk_pid, task->tk_status);
  831. task->tk_action = call_status;
  832. if (task->tk_status < 0)
  833. return;
  834. task->tk_status = xprt_prepare_transmit(task);
  835. if (task->tk_status != 0)
  836. return;
  837. task->tk_action = call_transmit_status;
  838. /* Encode here so that rpcsec_gss can use correct sequence number. */
  839. if (rpc_task_need_encode(task)) {
  840. BUG_ON(task->tk_rqstp->rq_bytes_sent != 0);
  841. call_encode(task);
  842. /* Did the encode result in an error condition? */
  843. if (task->tk_status != 0)
  844. return;
  845. }
  846. xprt_transmit(task);
  847. if (task->tk_status < 0)
  848. return;
  849. /*
  850. * On success, ensure that we call xprt_end_transmit() before sleeping
  851. * in order to allow access to the socket to other RPC requests.
  852. */
  853. call_transmit_status(task);
  854. if (task->tk_msg.rpc_proc->p_decode != NULL)
  855. return;
  856. task->tk_action = rpc_exit_task;
  857. rpc_wake_up_task(task);
  858. }
  859. /*
  860. * 5a. Handle cleanup after a transmission
  861. */
  862. static void
  863. call_transmit_status(struct rpc_task *task)
  864. {
  865. task->tk_action = call_status;
  866. /*
  867. * Special case: if we've been waiting on the socket's write_space()
  868. * callback, then don't call xprt_end_transmit().
  869. */
  870. if (task->tk_status == -EAGAIN)
  871. return;
  872. xprt_end_transmit(task);
  873. rpc_task_force_reencode(task);
  874. }
  875. /*
  876. * 6. Sort out the RPC call status
  877. */
  878. static void
  879. call_status(struct rpc_task *task)
  880. {
  881. struct rpc_clnt *clnt = task->tk_client;
  882. struct rpc_rqst *req = task->tk_rqstp;
  883. int status;
  884. if (req->rq_received > 0 && !req->rq_bytes_sent)
  885. task->tk_status = req->rq_received;
  886. dprintk("RPC: %4d call_status (status %d)\n",
  887. task->tk_pid, task->tk_status);
  888. status = task->tk_status;
  889. if (status >= 0) {
  890. task->tk_action = call_decode;
  891. return;
  892. }
  893. task->tk_status = 0;
  894. switch(status) {
  895. case -EHOSTDOWN:
  896. case -EHOSTUNREACH:
  897. case -ENETUNREACH:
  898. /*
  899. * Delay any retries for 3 seconds, then handle as if it
  900. * were a timeout.
  901. */
  902. rpc_delay(task, 3*HZ);
  903. case -ETIMEDOUT:
  904. task->tk_action = call_timeout;
  905. break;
  906. case -ECONNREFUSED:
  907. case -ENOTCONN:
  908. rpc_force_rebind(clnt);
  909. task->tk_action = call_bind;
  910. break;
  911. case -EAGAIN:
  912. task->tk_action = call_transmit;
  913. break;
  914. case -EIO:
  915. /* shutdown or soft timeout */
  916. rpc_exit(task, status);
  917. break;
  918. default:
  919. printk("%s: RPC call returned error %d\n",
  920. clnt->cl_protname, -status);
  921. rpc_exit(task, status);
  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. dprintk("%s: too small RPC reply size (%d bytes)\n",
  980. clnt->cl_protname, task->tk_status);
  981. task->tk_action = call_timeout;
  982. goto out_retry;
  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. }