clnt.c 34 KB

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