clnt.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735
  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/kallsyms.h>
  27. #include <linux/mm.h>
  28. #include <linux/slab.h>
  29. #include <linux/utsname.h>
  30. #include <linux/workqueue.h>
  31. #include <linux/in6.h>
  32. #include <linux/sunrpc/clnt.h>
  33. #include <linux/sunrpc/rpc_pipe_fs.h>
  34. #include <linux/sunrpc/metrics.h>
  35. #include <linux/sunrpc/bc_xprt.h>
  36. #include "sunrpc.h"
  37. #ifdef RPC_DEBUG
  38. # define RPCDBG_FACILITY RPCDBG_CALL
  39. #endif
  40. #define dprint_status(t) \
  41. dprintk("RPC: %5u %s (status %d)\n", t->tk_pid, \
  42. __func__, t->tk_status)
  43. /*
  44. * All RPC clients are linked into this list
  45. */
  46. static LIST_HEAD(all_clients);
  47. static DEFINE_SPINLOCK(rpc_client_lock);
  48. static DECLARE_WAIT_QUEUE_HEAD(destroy_wait);
  49. static void call_start(struct rpc_task *task);
  50. static void call_reserve(struct rpc_task *task);
  51. static void call_reserveresult(struct rpc_task *task);
  52. static void call_allocate(struct rpc_task *task);
  53. static void call_decode(struct rpc_task *task);
  54. static void call_bind(struct rpc_task *task);
  55. static void call_bind_status(struct rpc_task *task);
  56. static void call_transmit(struct rpc_task *task);
  57. #if defined(CONFIG_NFS_V4_1)
  58. static void call_bc_transmit(struct rpc_task *task);
  59. #endif /* CONFIG_NFS_V4_1 */
  60. static void call_status(struct rpc_task *task);
  61. static void call_transmit_status(struct rpc_task *task);
  62. static void call_refresh(struct rpc_task *task);
  63. static void call_refreshresult(struct rpc_task *task);
  64. static void call_timeout(struct rpc_task *task);
  65. static void call_connect(struct rpc_task *task);
  66. static void call_connect_status(struct rpc_task *task);
  67. static __be32 *rpc_encode_header(struct rpc_task *task);
  68. static __be32 *rpc_verify_header(struct rpc_task *task);
  69. static int rpc_ping(struct rpc_clnt *clnt, int flags);
  70. static void rpc_register_client(struct rpc_clnt *clnt)
  71. {
  72. spin_lock(&rpc_client_lock);
  73. list_add(&clnt->cl_clients, &all_clients);
  74. spin_unlock(&rpc_client_lock);
  75. }
  76. static void rpc_unregister_client(struct rpc_clnt *clnt)
  77. {
  78. spin_lock(&rpc_client_lock);
  79. list_del(&clnt->cl_clients);
  80. spin_unlock(&rpc_client_lock);
  81. }
  82. static int
  83. rpc_setup_pipedir(struct rpc_clnt *clnt, char *dir_name)
  84. {
  85. static uint32_t clntid;
  86. int error;
  87. clnt->cl_vfsmnt = ERR_PTR(-ENOENT);
  88. clnt->cl_dentry = ERR_PTR(-ENOENT);
  89. if (dir_name == NULL)
  90. return 0;
  91. clnt->cl_vfsmnt = rpc_get_mount();
  92. if (IS_ERR(clnt->cl_vfsmnt))
  93. return PTR_ERR(clnt->cl_vfsmnt);
  94. for (;;) {
  95. snprintf(clnt->cl_pathname, sizeof(clnt->cl_pathname),
  96. "%s/clnt%x", dir_name,
  97. (unsigned int)clntid++);
  98. clnt->cl_pathname[sizeof(clnt->cl_pathname) - 1] = '\0';
  99. clnt->cl_dentry = rpc_mkdir(clnt->cl_pathname, clnt);
  100. if (!IS_ERR(clnt->cl_dentry))
  101. return 0;
  102. error = PTR_ERR(clnt->cl_dentry);
  103. if (error != -EEXIST) {
  104. printk(KERN_INFO "RPC: Couldn't create pipefs entry %s, error %d\n",
  105. clnt->cl_pathname, error);
  106. rpc_put_mount();
  107. return error;
  108. }
  109. }
  110. }
  111. static struct rpc_clnt * rpc_new_client(const struct rpc_create_args *args, struct rpc_xprt *xprt)
  112. {
  113. struct rpc_program *program = args->program;
  114. struct rpc_version *version;
  115. struct rpc_clnt *clnt = NULL;
  116. struct rpc_auth *auth;
  117. int err;
  118. size_t len;
  119. /* sanity check the name before trying to print it */
  120. err = -EINVAL;
  121. len = strlen(args->servername);
  122. if (len > RPC_MAXNETNAMELEN)
  123. goto out_no_rpciod;
  124. len++;
  125. dprintk("RPC: creating %s client for %s (xprt %p)\n",
  126. program->name, args->servername, xprt);
  127. err = rpciod_up();
  128. if (err)
  129. goto out_no_rpciod;
  130. err = -EINVAL;
  131. if (!xprt)
  132. goto out_no_xprt;
  133. if (args->version >= program->nrvers)
  134. goto out_err;
  135. version = program->version[args->version];
  136. if (version == NULL)
  137. goto out_err;
  138. err = -ENOMEM;
  139. clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
  140. if (!clnt)
  141. goto out_err;
  142. clnt->cl_parent = clnt;
  143. clnt->cl_server = clnt->cl_inline_name;
  144. if (len > sizeof(clnt->cl_inline_name)) {
  145. char *buf = kmalloc(len, GFP_KERNEL);
  146. if (buf != NULL)
  147. clnt->cl_server = buf;
  148. else
  149. len = sizeof(clnt->cl_inline_name);
  150. }
  151. strlcpy(clnt->cl_server, args->servername, len);
  152. clnt->cl_xprt = xprt;
  153. clnt->cl_procinfo = version->procs;
  154. clnt->cl_maxproc = version->nrprocs;
  155. clnt->cl_protname = program->name;
  156. clnt->cl_prog = args->prognumber ? : program->number;
  157. clnt->cl_vers = version->number;
  158. clnt->cl_stats = program->stats;
  159. clnt->cl_metrics = rpc_alloc_iostats(clnt);
  160. err = -ENOMEM;
  161. if (clnt->cl_metrics == NULL)
  162. goto out_no_stats;
  163. clnt->cl_program = program;
  164. INIT_LIST_HEAD(&clnt->cl_tasks);
  165. spin_lock_init(&clnt->cl_lock);
  166. if (!xprt_bound(clnt->cl_xprt))
  167. clnt->cl_autobind = 1;
  168. clnt->cl_timeout = xprt->timeout;
  169. if (args->timeout != NULL) {
  170. memcpy(&clnt->cl_timeout_default, args->timeout,
  171. sizeof(clnt->cl_timeout_default));
  172. clnt->cl_timeout = &clnt->cl_timeout_default;
  173. }
  174. clnt->cl_rtt = &clnt->cl_rtt_default;
  175. rpc_init_rtt(&clnt->cl_rtt_default, clnt->cl_timeout->to_initval);
  176. clnt->cl_principal = NULL;
  177. if (args->client_name) {
  178. clnt->cl_principal = kstrdup(args->client_name, GFP_KERNEL);
  179. if (!clnt->cl_principal)
  180. goto out_no_principal;
  181. }
  182. kref_init(&clnt->cl_kref);
  183. err = rpc_setup_pipedir(clnt, program->pipe_dir_name);
  184. if (err < 0)
  185. goto out_no_path;
  186. auth = rpcauth_create(args->authflavor, clnt);
  187. if (IS_ERR(auth)) {
  188. printk(KERN_INFO "RPC: Couldn't create auth handle (flavor %u)\n",
  189. args->authflavor);
  190. err = PTR_ERR(auth);
  191. goto out_no_auth;
  192. }
  193. /* save the nodename */
  194. clnt->cl_nodelen = strlen(init_utsname()->nodename);
  195. if (clnt->cl_nodelen > UNX_MAXNODENAME)
  196. clnt->cl_nodelen = UNX_MAXNODENAME;
  197. memcpy(clnt->cl_nodename, init_utsname()->nodename, clnt->cl_nodelen);
  198. rpc_register_client(clnt);
  199. return clnt;
  200. out_no_auth:
  201. if (!IS_ERR(clnt->cl_dentry)) {
  202. rpc_rmdir(clnt->cl_dentry);
  203. rpc_put_mount();
  204. }
  205. out_no_path:
  206. kfree(clnt->cl_principal);
  207. out_no_principal:
  208. rpc_free_iostats(clnt->cl_metrics);
  209. out_no_stats:
  210. if (clnt->cl_server != clnt->cl_inline_name)
  211. kfree(clnt->cl_server);
  212. kfree(clnt);
  213. out_err:
  214. xprt_put(xprt);
  215. out_no_xprt:
  216. rpciod_down();
  217. out_no_rpciod:
  218. return ERR_PTR(err);
  219. }
  220. /*
  221. * rpc_create - create an RPC client and transport with one call
  222. * @args: rpc_clnt create argument structure
  223. *
  224. * Creates and initializes an RPC transport and an RPC client.
  225. *
  226. * It can ping the server in order to determine if it is up, and to see if
  227. * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
  228. * this behavior so asynchronous tasks can also use rpc_create.
  229. */
  230. struct rpc_clnt *rpc_create(struct rpc_create_args *args)
  231. {
  232. struct rpc_xprt *xprt;
  233. struct rpc_clnt *clnt;
  234. struct xprt_create xprtargs = {
  235. .ident = args->protocol,
  236. .srcaddr = args->saddress,
  237. .dstaddr = args->address,
  238. .addrlen = args->addrsize,
  239. };
  240. char servername[48];
  241. /*
  242. * If the caller chooses not to specify a hostname, whip
  243. * up a string representation of the passed-in address.
  244. */
  245. if (args->servername == NULL) {
  246. servername[0] = '\0';
  247. switch (args->address->sa_family) {
  248. case AF_INET: {
  249. struct sockaddr_in *sin =
  250. (struct sockaddr_in *)args->address;
  251. snprintf(servername, sizeof(servername), "%pI4",
  252. &sin->sin_addr.s_addr);
  253. break;
  254. }
  255. case AF_INET6: {
  256. struct sockaddr_in6 *sin =
  257. (struct sockaddr_in6 *)args->address;
  258. snprintf(servername, sizeof(servername), "%pI6",
  259. &sin->sin6_addr);
  260. break;
  261. }
  262. default:
  263. /* caller wants default server name, but
  264. * address family isn't recognized. */
  265. return ERR_PTR(-EINVAL);
  266. }
  267. args->servername = servername;
  268. }
  269. xprt = xprt_create_transport(&xprtargs);
  270. if (IS_ERR(xprt))
  271. return (struct rpc_clnt *)xprt;
  272. /*
  273. * By default, kernel RPC client connects from a reserved port.
  274. * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
  275. * but it is always enabled for rpciod, which handles the connect
  276. * operation.
  277. */
  278. xprt->resvport = 1;
  279. if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
  280. xprt->resvport = 0;
  281. clnt = rpc_new_client(args, xprt);
  282. if (IS_ERR(clnt))
  283. return clnt;
  284. if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
  285. int err = rpc_ping(clnt, RPC_TASK_SOFT);
  286. if (err != 0) {
  287. rpc_shutdown_client(clnt);
  288. return ERR_PTR(err);
  289. }
  290. }
  291. clnt->cl_softrtry = 1;
  292. if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
  293. clnt->cl_softrtry = 0;
  294. if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
  295. clnt->cl_autobind = 1;
  296. if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
  297. clnt->cl_discrtry = 1;
  298. if (!(args->flags & RPC_CLNT_CREATE_QUIET))
  299. clnt->cl_chatty = 1;
  300. return clnt;
  301. }
  302. EXPORT_SYMBOL_GPL(rpc_create);
  303. /*
  304. * This function clones the RPC client structure. It allows us to share the
  305. * same transport while varying parameters such as the authentication
  306. * flavour.
  307. */
  308. struct rpc_clnt *
  309. rpc_clone_client(struct rpc_clnt *clnt)
  310. {
  311. struct rpc_clnt *new;
  312. int err = -ENOMEM;
  313. new = kmemdup(clnt, sizeof(*new), GFP_KERNEL);
  314. if (!new)
  315. goto out_no_clnt;
  316. new->cl_parent = clnt;
  317. /* Turn off autobind on clones */
  318. new->cl_autobind = 0;
  319. INIT_LIST_HEAD(&new->cl_tasks);
  320. spin_lock_init(&new->cl_lock);
  321. rpc_init_rtt(&new->cl_rtt_default, clnt->cl_timeout->to_initval);
  322. new->cl_metrics = rpc_alloc_iostats(clnt);
  323. if (new->cl_metrics == NULL)
  324. goto out_no_stats;
  325. if (clnt->cl_principal) {
  326. new->cl_principal = kstrdup(clnt->cl_principal, GFP_KERNEL);
  327. if (new->cl_principal == NULL)
  328. goto out_no_principal;
  329. }
  330. kref_init(&new->cl_kref);
  331. err = rpc_setup_pipedir(new, clnt->cl_program->pipe_dir_name);
  332. if (err != 0)
  333. goto out_no_path;
  334. if (new->cl_auth)
  335. atomic_inc(&new->cl_auth->au_count);
  336. xprt_get(clnt->cl_xprt);
  337. kref_get(&clnt->cl_kref);
  338. rpc_register_client(new);
  339. rpciod_up();
  340. return new;
  341. out_no_path:
  342. kfree(new->cl_principal);
  343. out_no_principal:
  344. rpc_free_iostats(new->cl_metrics);
  345. out_no_stats:
  346. kfree(new);
  347. out_no_clnt:
  348. dprintk("RPC: %s: returned error %d\n", __func__, err);
  349. return ERR_PTR(err);
  350. }
  351. EXPORT_SYMBOL_GPL(rpc_clone_client);
  352. /*
  353. * Properly shut down an RPC client, terminating all outstanding
  354. * requests.
  355. */
  356. void rpc_shutdown_client(struct rpc_clnt *clnt)
  357. {
  358. dprintk("RPC: shutting down %s client for %s\n",
  359. clnt->cl_protname, clnt->cl_server);
  360. while (!list_empty(&clnt->cl_tasks)) {
  361. rpc_killall_tasks(clnt);
  362. wait_event_timeout(destroy_wait,
  363. list_empty(&clnt->cl_tasks), 1*HZ);
  364. }
  365. rpc_release_client(clnt);
  366. }
  367. EXPORT_SYMBOL_GPL(rpc_shutdown_client);
  368. /*
  369. * Free an RPC client
  370. */
  371. static void
  372. rpc_free_client(struct kref *kref)
  373. {
  374. struct rpc_clnt *clnt = container_of(kref, struct rpc_clnt, cl_kref);
  375. dprintk("RPC: destroying %s client for %s\n",
  376. clnt->cl_protname, clnt->cl_server);
  377. if (!IS_ERR(clnt->cl_dentry)) {
  378. rpc_rmdir(clnt->cl_dentry);
  379. rpc_put_mount();
  380. }
  381. if (clnt->cl_parent != clnt) {
  382. rpc_release_client(clnt->cl_parent);
  383. goto out_free;
  384. }
  385. if (clnt->cl_server != clnt->cl_inline_name)
  386. kfree(clnt->cl_server);
  387. out_free:
  388. rpc_unregister_client(clnt);
  389. rpc_free_iostats(clnt->cl_metrics);
  390. kfree(clnt->cl_principal);
  391. clnt->cl_metrics = NULL;
  392. xprt_put(clnt->cl_xprt);
  393. rpciod_down();
  394. kfree(clnt);
  395. }
  396. /*
  397. * Free an RPC client
  398. */
  399. static void
  400. rpc_free_auth(struct kref *kref)
  401. {
  402. struct rpc_clnt *clnt = container_of(kref, struct rpc_clnt, cl_kref);
  403. if (clnt->cl_auth == NULL) {
  404. rpc_free_client(kref);
  405. return;
  406. }
  407. /*
  408. * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
  409. * release remaining GSS contexts. This mechanism ensures
  410. * that it can do so safely.
  411. */
  412. kref_init(kref);
  413. rpcauth_release(clnt->cl_auth);
  414. clnt->cl_auth = NULL;
  415. kref_put(kref, rpc_free_client);
  416. }
  417. /*
  418. * Release reference to the RPC client
  419. */
  420. void
  421. rpc_release_client(struct rpc_clnt *clnt)
  422. {
  423. dprintk("RPC: rpc_release_client(%p)\n", clnt);
  424. if (list_empty(&clnt->cl_tasks))
  425. wake_up(&destroy_wait);
  426. kref_put(&clnt->cl_kref, rpc_free_auth);
  427. }
  428. /**
  429. * rpc_bind_new_program - bind a new RPC program to an existing client
  430. * @old: old rpc_client
  431. * @program: rpc program to set
  432. * @vers: rpc program version
  433. *
  434. * Clones the rpc client and sets up a new RPC program. This is mainly
  435. * of use for enabling different RPC programs to share the same transport.
  436. * The Sun NFSv2/v3 ACL protocol can do this.
  437. */
  438. struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
  439. struct rpc_program *program,
  440. u32 vers)
  441. {
  442. struct rpc_clnt *clnt;
  443. struct rpc_version *version;
  444. int err;
  445. BUG_ON(vers >= program->nrvers || !program->version[vers]);
  446. version = program->version[vers];
  447. clnt = rpc_clone_client(old);
  448. if (IS_ERR(clnt))
  449. goto out;
  450. clnt->cl_procinfo = version->procs;
  451. clnt->cl_maxproc = version->nrprocs;
  452. clnt->cl_protname = program->name;
  453. clnt->cl_prog = program->number;
  454. clnt->cl_vers = version->number;
  455. clnt->cl_stats = program->stats;
  456. err = rpc_ping(clnt, RPC_TASK_SOFT);
  457. if (err != 0) {
  458. rpc_shutdown_client(clnt);
  459. clnt = ERR_PTR(err);
  460. }
  461. out:
  462. return clnt;
  463. }
  464. EXPORT_SYMBOL_GPL(rpc_bind_new_program);
  465. /*
  466. * Default callback for async RPC calls
  467. */
  468. static void
  469. rpc_default_callback(struct rpc_task *task, void *data)
  470. {
  471. }
  472. static const struct rpc_call_ops rpc_default_ops = {
  473. .rpc_call_done = rpc_default_callback,
  474. };
  475. /**
  476. * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
  477. * @task_setup_data: pointer to task initialisation data
  478. */
  479. struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
  480. {
  481. struct rpc_task *task, *ret;
  482. task = rpc_new_task(task_setup_data);
  483. if (task == NULL) {
  484. rpc_release_calldata(task_setup_data->callback_ops,
  485. task_setup_data->callback_data);
  486. ret = ERR_PTR(-ENOMEM);
  487. goto out;
  488. }
  489. if (task->tk_status != 0) {
  490. ret = ERR_PTR(task->tk_status);
  491. rpc_put_task(task);
  492. goto out;
  493. }
  494. atomic_inc(&task->tk_count);
  495. rpc_execute(task);
  496. ret = task;
  497. out:
  498. return ret;
  499. }
  500. EXPORT_SYMBOL_GPL(rpc_run_task);
  501. /**
  502. * rpc_call_sync - Perform a synchronous RPC call
  503. * @clnt: pointer to RPC client
  504. * @msg: RPC call parameters
  505. * @flags: RPC call flags
  506. */
  507. int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags)
  508. {
  509. struct rpc_task *task;
  510. struct rpc_task_setup task_setup_data = {
  511. .rpc_client = clnt,
  512. .rpc_message = msg,
  513. .callback_ops = &rpc_default_ops,
  514. .flags = flags,
  515. };
  516. int status;
  517. BUG_ON(flags & RPC_TASK_ASYNC);
  518. task = rpc_run_task(&task_setup_data);
  519. if (IS_ERR(task))
  520. return PTR_ERR(task);
  521. status = task->tk_status;
  522. rpc_put_task(task);
  523. return status;
  524. }
  525. EXPORT_SYMBOL_GPL(rpc_call_sync);
  526. /**
  527. * rpc_call_async - Perform an asynchronous RPC call
  528. * @clnt: pointer to RPC client
  529. * @msg: RPC call parameters
  530. * @flags: RPC call flags
  531. * @tk_ops: RPC call ops
  532. * @data: user call data
  533. */
  534. int
  535. rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags,
  536. const struct rpc_call_ops *tk_ops, void *data)
  537. {
  538. struct rpc_task *task;
  539. struct rpc_task_setup task_setup_data = {
  540. .rpc_client = clnt,
  541. .rpc_message = msg,
  542. .callback_ops = tk_ops,
  543. .callback_data = data,
  544. .flags = flags|RPC_TASK_ASYNC,
  545. };
  546. task = rpc_run_task(&task_setup_data);
  547. if (IS_ERR(task))
  548. return PTR_ERR(task);
  549. rpc_put_task(task);
  550. return 0;
  551. }
  552. EXPORT_SYMBOL_GPL(rpc_call_async);
  553. #if defined(CONFIG_NFS_V4_1)
  554. /**
  555. * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
  556. * rpc_execute against it
  557. * @ops: RPC call ops
  558. */
  559. struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req,
  560. const struct rpc_call_ops *tk_ops)
  561. {
  562. struct rpc_task *task;
  563. struct xdr_buf *xbufp = &req->rq_snd_buf;
  564. struct rpc_task_setup task_setup_data = {
  565. .callback_ops = tk_ops,
  566. };
  567. dprintk("RPC: rpc_run_bc_task req= %p\n", req);
  568. /*
  569. * Create an rpc_task to send the data
  570. */
  571. task = rpc_new_task(&task_setup_data);
  572. if (!task) {
  573. xprt_free_bc_request(req);
  574. goto out;
  575. }
  576. task->tk_rqstp = req;
  577. /*
  578. * Set up the xdr_buf length.
  579. * This also indicates that the buffer is XDR encoded already.
  580. */
  581. xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
  582. xbufp->tail[0].iov_len;
  583. task->tk_action = call_bc_transmit;
  584. atomic_inc(&task->tk_count);
  585. BUG_ON(atomic_read(&task->tk_count) != 2);
  586. rpc_execute(task);
  587. out:
  588. dprintk("RPC: rpc_run_bc_task: task= %p\n", task);
  589. return task;
  590. }
  591. #endif /* CONFIG_NFS_V4_1 */
  592. void
  593. rpc_call_start(struct rpc_task *task)
  594. {
  595. task->tk_action = call_start;
  596. }
  597. EXPORT_SYMBOL_GPL(rpc_call_start);
  598. /**
  599. * rpc_peeraddr - extract remote peer address from clnt's xprt
  600. * @clnt: RPC client structure
  601. * @buf: target buffer
  602. * @bufsize: length of target buffer
  603. *
  604. * Returns the number of bytes that are actually in the stored address.
  605. */
  606. size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
  607. {
  608. size_t bytes;
  609. struct rpc_xprt *xprt = clnt->cl_xprt;
  610. bytes = sizeof(xprt->addr);
  611. if (bytes > bufsize)
  612. bytes = bufsize;
  613. memcpy(buf, &clnt->cl_xprt->addr, bytes);
  614. return xprt->addrlen;
  615. }
  616. EXPORT_SYMBOL_GPL(rpc_peeraddr);
  617. /**
  618. * rpc_peeraddr2str - return remote peer address in printable format
  619. * @clnt: RPC client structure
  620. * @format: address format
  621. *
  622. */
  623. const char *rpc_peeraddr2str(struct rpc_clnt *clnt,
  624. enum rpc_display_format_t format)
  625. {
  626. struct rpc_xprt *xprt = clnt->cl_xprt;
  627. if (xprt->address_strings[format] != NULL)
  628. return xprt->address_strings[format];
  629. else
  630. return "unprintable";
  631. }
  632. EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
  633. void
  634. rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
  635. {
  636. struct rpc_xprt *xprt = clnt->cl_xprt;
  637. if (xprt->ops->set_buffer_size)
  638. xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
  639. }
  640. EXPORT_SYMBOL_GPL(rpc_setbufsize);
  641. /*
  642. * Return size of largest payload RPC client can support, in bytes
  643. *
  644. * For stream transports, this is one RPC record fragment (see RFC
  645. * 1831), as we don't support multi-record requests yet. For datagram
  646. * transports, this is the size of an IP packet minus the IP, UDP, and
  647. * RPC header sizes.
  648. */
  649. size_t rpc_max_payload(struct rpc_clnt *clnt)
  650. {
  651. return clnt->cl_xprt->max_payload;
  652. }
  653. EXPORT_SYMBOL_GPL(rpc_max_payload);
  654. /**
  655. * rpc_force_rebind - force transport to check that remote port is unchanged
  656. * @clnt: client to rebind
  657. *
  658. */
  659. void rpc_force_rebind(struct rpc_clnt *clnt)
  660. {
  661. if (clnt->cl_autobind)
  662. xprt_clear_bound(clnt->cl_xprt);
  663. }
  664. EXPORT_SYMBOL_GPL(rpc_force_rebind);
  665. /*
  666. * Restart an (async) RPC call from the call_prepare state.
  667. * Usually called from within the exit handler.
  668. */
  669. void
  670. rpc_restart_call_prepare(struct rpc_task *task)
  671. {
  672. if (RPC_ASSASSINATED(task))
  673. return;
  674. task->tk_action = rpc_prepare_task;
  675. }
  676. EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
  677. /*
  678. * Restart an (async) RPC call. Usually called from within the
  679. * exit handler.
  680. */
  681. void
  682. rpc_restart_call(struct rpc_task *task)
  683. {
  684. if (RPC_ASSASSINATED(task))
  685. return;
  686. task->tk_action = call_start;
  687. }
  688. EXPORT_SYMBOL_GPL(rpc_restart_call);
  689. #ifdef RPC_DEBUG
  690. static const char *rpc_proc_name(const struct rpc_task *task)
  691. {
  692. const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
  693. if (proc) {
  694. if (proc->p_name)
  695. return proc->p_name;
  696. else
  697. return "NULL";
  698. } else
  699. return "no proc";
  700. }
  701. #endif
  702. /*
  703. * 0. Initial state
  704. *
  705. * Other FSM states can be visited zero or more times, but
  706. * this state is visited exactly once for each RPC.
  707. */
  708. static void
  709. call_start(struct rpc_task *task)
  710. {
  711. struct rpc_clnt *clnt = task->tk_client;
  712. dprintk("RPC: %5u call_start %s%d proc %s (%s)\n", task->tk_pid,
  713. clnt->cl_protname, clnt->cl_vers,
  714. rpc_proc_name(task),
  715. (RPC_IS_ASYNC(task) ? "async" : "sync"));
  716. /* Increment call count */
  717. task->tk_msg.rpc_proc->p_count++;
  718. clnt->cl_stats->rpccnt++;
  719. task->tk_action = call_reserve;
  720. }
  721. /*
  722. * 1. Reserve an RPC call slot
  723. */
  724. static void
  725. call_reserve(struct rpc_task *task)
  726. {
  727. dprint_status(task);
  728. if (!rpcauth_uptodatecred(task)) {
  729. task->tk_action = call_refresh;
  730. return;
  731. }
  732. task->tk_status = 0;
  733. task->tk_action = call_reserveresult;
  734. xprt_reserve(task);
  735. }
  736. /*
  737. * 1b. Grok the result of xprt_reserve()
  738. */
  739. static void
  740. call_reserveresult(struct rpc_task *task)
  741. {
  742. int status = task->tk_status;
  743. dprint_status(task);
  744. /*
  745. * After a call to xprt_reserve(), we must have either
  746. * a request slot or else an error status.
  747. */
  748. task->tk_status = 0;
  749. if (status >= 0) {
  750. if (task->tk_rqstp) {
  751. task->tk_action = call_allocate;
  752. return;
  753. }
  754. printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
  755. __func__, status);
  756. rpc_exit(task, -EIO);
  757. return;
  758. }
  759. /*
  760. * Even though there was an error, we may have acquired
  761. * a request slot somehow. Make sure not to leak it.
  762. */
  763. if (task->tk_rqstp) {
  764. printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
  765. __func__, status);
  766. xprt_release(task);
  767. }
  768. switch (status) {
  769. case -EAGAIN: /* woken up; retry */
  770. task->tk_action = call_reserve;
  771. return;
  772. case -EIO: /* probably a shutdown */
  773. break;
  774. default:
  775. printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
  776. __func__, status);
  777. break;
  778. }
  779. rpc_exit(task, status);
  780. }
  781. /*
  782. * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
  783. * (Note: buffer memory is freed in xprt_release).
  784. */
  785. static void
  786. call_allocate(struct rpc_task *task)
  787. {
  788. unsigned int slack = task->tk_msg.rpc_cred->cr_auth->au_cslack;
  789. struct rpc_rqst *req = task->tk_rqstp;
  790. struct rpc_xprt *xprt = task->tk_xprt;
  791. struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
  792. dprint_status(task);
  793. task->tk_status = 0;
  794. task->tk_action = call_bind;
  795. if (req->rq_buffer)
  796. return;
  797. if (proc->p_proc != 0) {
  798. BUG_ON(proc->p_arglen == 0);
  799. if (proc->p_decode != NULL)
  800. BUG_ON(proc->p_replen == 0);
  801. }
  802. /*
  803. * Calculate the size (in quads) of the RPC call
  804. * and reply headers, and convert both values
  805. * to byte sizes.
  806. */
  807. req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen;
  808. req->rq_callsize <<= 2;
  809. req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen;
  810. req->rq_rcvsize <<= 2;
  811. req->rq_buffer = xprt->ops->buf_alloc(task,
  812. req->rq_callsize + req->rq_rcvsize);
  813. if (req->rq_buffer != NULL)
  814. return;
  815. dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
  816. if (RPC_IS_ASYNC(task) || !signalled()) {
  817. task->tk_action = call_allocate;
  818. rpc_delay(task, HZ>>4);
  819. return;
  820. }
  821. rpc_exit(task, -ERESTARTSYS);
  822. }
  823. static inline int
  824. rpc_task_need_encode(struct rpc_task *task)
  825. {
  826. return task->tk_rqstp->rq_snd_buf.len == 0;
  827. }
  828. static inline void
  829. rpc_task_force_reencode(struct rpc_task *task)
  830. {
  831. task->tk_rqstp->rq_snd_buf.len = 0;
  832. }
  833. static inline void
  834. rpc_xdr_buf_init(struct xdr_buf *buf, void *start, size_t len)
  835. {
  836. buf->head[0].iov_base = start;
  837. buf->head[0].iov_len = len;
  838. buf->tail[0].iov_len = 0;
  839. buf->page_len = 0;
  840. buf->flags = 0;
  841. buf->len = 0;
  842. buf->buflen = len;
  843. }
  844. /*
  845. * 3. Encode arguments of an RPC call
  846. */
  847. static void
  848. rpc_xdr_encode(struct rpc_task *task)
  849. {
  850. struct rpc_rqst *req = task->tk_rqstp;
  851. kxdrproc_t encode;
  852. __be32 *p;
  853. dprint_status(task);
  854. rpc_xdr_buf_init(&req->rq_snd_buf,
  855. req->rq_buffer,
  856. req->rq_callsize);
  857. rpc_xdr_buf_init(&req->rq_rcv_buf,
  858. (char *)req->rq_buffer + req->rq_callsize,
  859. req->rq_rcvsize);
  860. p = rpc_encode_header(task);
  861. if (p == NULL) {
  862. printk(KERN_INFO "RPC: couldn't encode RPC header, exit EIO\n");
  863. rpc_exit(task, -EIO);
  864. return;
  865. }
  866. encode = task->tk_msg.rpc_proc->p_encode;
  867. if (encode == NULL)
  868. return;
  869. task->tk_status = rpcauth_wrap_req(task, encode, req, p,
  870. task->tk_msg.rpc_argp);
  871. }
  872. /*
  873. * 4. Get the server port number if not yet set
  874. */
  875. static void
  876. call_bind(struct rpc_task *task)
  877. {
  878. struct rpc_xprt *xprt = task->tk_xprt;
  879. dprint_status(task);
  880. task->tk_action = call_connect;
  881. if (!xprt_bound(xprt)) {
  882. task->tk_action = call_bind_status;
  883. task->tk_timeout = xprt->bind_timeout;
  884. xprt->ops->rpcbind(task);
  885. }
  886. }
  887. /*
  888. * 4a. Sort out bind result
  889. */
  890. static void
  891. call_bind_status(struct rpc_task *task)
  892. {
  893. int status = -EIO;
  894. if (task->tk_status >= 0) {
  895. dprint_status(task);
  896. task->tk_status = 0;
  897. task->tk_action = call_connect;
  898. return;
  899. }
  900. switch (task->tk_status) {
  901. case -ENOMEM:
  902. dprintk("RPC: %5u rpcbind out of memory\n", task->tk_pid);
  903. rpc_delay(task, HZ >> 2);
  904. goto retry_timeout;
  905. case -EACCES:
  906. dprintk("RPC: %5u remote rpcbind: RPC program/version "
  907. "unavailable\n", task->tk_pid);
  908. /* fail immediately if this is an RPC ping */
  909. if (task->tk_msg.rpc_proc->p_proc == 0) {
  910. status = -EOPNOTSUPP;
  911. break;
  912. }
  913. rpc_delay(task, 3*HZ);
  914. goto retry_timeout;
  915. case -ETIMEDOUT:
  916. dprintk("RPC: %5u rpcbind request timed out\n",
  917. task->tk_pid);
  918. goto retry_timeout;
  919. case -EPFNOSUPPORT:
  920. /* server doesn't support any rpcbind version we know of */
  921. dprintk("RPC: %5u remote rpcbind service unavailable\n",
  922. task->tk_pid);
  923. break;
  924. case -EPROTONOSUPPORT:
  925. dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
  926. task->tk_pid);
  927. task->tk_status = 0;
  928. task->tk_action = call_bind;
  929. return;
  930. default:
  931. dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
  932. task->tk_pid, -task->tk_status);
  933. }
  934. rpc_exit(task, status);
  935. return;
  936. retry_timeout:
  937. task->tk_action = call_timeout;
  938. }
  939. /*
  940. * 4b. Connect to the RPC server
  941. */
  942. static void
  943. call_connect(struct rpc_task *task)
  944. {
  945. struct rpc_xprt *xprt = task->tk_xprt;
  946. dprintk("RPC: %5u call_connect xprt %p %s connected\n",
  947. task->tk_pid, xprt,
  948. (xprt_connected(xprt) ? "is" : "is not"));
  949. task->tk_action = call_transmit;
  950. if (!xprt_connected(xprt)) {
  951. task->tk_action = call_connect_status;
  952. if (task->tk_status < 0)
  953. return;
  954. xprt_connect(task);
  955. }
  956. }
  957. /*
  958. * 4c. Sort out connect result
  959. */
  960. static void
  961. call_connect_status(struct rpc_task *task)
  962. {
  963. struct rpc_clnt *clnt = task->tk_client;
  964. int status = task->tk_status;
  965. dprint_status(task);
  966. task->tk_status = 0;
  967. if (status >= 0 || status == -EAGAIN) {
  968. clnt->cl_stats->netreconn++;
  969. task->tk_action = call_transmit;
  970. return;
  971. }
  972. switch (status) {
  973. /* if soft mounted, test if we've timed out */
  974. case -ETIMEDOUT:
  975. task->tk_action = call_timeout;
  976. break;
  977. default:
  978. rpc_exit(task, -EIO);
  979. }
  980. }
  981. /*
  982. * 5. Transmit the RPC request, and wait for reply
  983. */
  984. static void
  985. call_transmit(struct rpc_task *task)
  986. {
  987. dprint_status(task);
  988. task->tk_action = call_status;
  989. if (task->tk_status < 0)
  990. return;
  991. task->tk_status = xprt_prepare_transmit(task);
  992. if (task->tk_status != 0)
  993. return;
  994. task->tk_action = call_transmit_status;
  995. /* Encode here so that rpcsec_gss can use correct sequence number. */
  996. if (rpc_task_need_encode(task)) {
  997. BUG_ON(task->tk_rqstp->rq_bytes_sent != 0);
  998. rpc_xdr_encode(task);
  999. /* Did the encode result in an error condition? */
  1000. if (task->tk_status != 0) {
  1001. /* Was the error nonfatal? */
  1002. if (task->tk_status == -EAGAIN)
  1003. rpc_delay(task, HZ >> 4);
  1004. else
  1005. rpc_exit(task, task->tk_status);
  1006. return;
  1007. }
  1008. }
  1009. xprt_transmit(task);
  1010. if (task->tk_status < 0)
  1011. return;
  1012. /*
  1013. * On success, ensure that we call xprt_end_transmit() before sleeping
  1014. * in order to allow access to the socket to other RPC requests.
  1015. */
  1016. call_transmit_status(task);
  1017. if (rpc_reply_expected(task))
  1018. return;
  1019. task->tk_action = rpc_exit_task;
  1020. rpc_wake_up_queued_task(&task->tk_xprt->pending, task);
  1021. }
  1022. /*
  1023. * 5a. Handle cleanup after a transmission
  1024. */
  1025. static void
  1026. call_transmit_status(struct rpc_task *task)
  1027. {
  1028. task->tk_action = call_status;
  1029. switch (task->tk_status) {
  1030. case -EAGAIN:
  1031. break;
  1032. default:
  1033. xprt_end_transmit(task);
  1034. /*
  1035. * Special cases: if we've been waiting on the
  1036. * socket's write_space() callback, or if the
  1037. * socket just returned a connection error,
  1038. * then hold onto the transport lock.
  1039. */
  1040. case -ECONNREFUSED:
  1041. case -ECONNRESET:
  1042. case -ENOTCONN:
  1043. case -EHOSTDOWN:
  1044. case -EHOSTUNREACH:
  1045. case -ENETUNREACH:
  1046. case -EPIPE:
  1047. rpc_task_force_reencode(task);
  1048. }
  1049. }
  1050. #if defined(CONFIG_NFS_V4_1)
  1051. /*
  1052. * 5b. Send the backchannel RPC reply. On error, drop the reply. In
  1053. * addition, disconnect on connectivity errors.
  1054. */
  1055. static void
  1056. call_bc_transmit(struct rpc_task *task)
  1057. {
  1058. struct rpc_rqst *req = task->tk_rqstp;
  1059. BUG_ON(task->tk_status != 0);
  1060. task->tk_status = xprt_prepare_transmit(task);
  1061. if (task->tk_status == -EAGAIN) {
  1062. /*
  1063. * Could not reserve the transport. Try again after the
  1064. * transport is released.
  1065. */
  1066. task->tk_status = 0;
  1067. task->tk_action = call_bc_transmit;
  1068. return;
  1069. }
  1070. task->tk_action = rpc_exit_task;
  1071. if (task->tk_status < 0) {
  1072. printk(KERN_NOTICE "RPC: Could not send backchannel reply "
  1073. "error: %d\n", task->tk_status);
  1074. return;
  1075. }
  1076. xprt_transmit(task);
  1077. xprt_end_transmit(task);
  1078. dprint_status(task);
  1079. switch (task->tk_status) {
  1080. case 0:
  1081. /* Success */
  1082. break;
  1083. case -EHOSTDOWN:
  1084. case -EHOSTUNREACH:
  1085. case -ENETUNREACH:
  1086. case -ETIMEDOUT:
  1087. /*
  1088. * Problem reaching the server. Disconnect and let the
  1089. * forechannel reestablish the connection. The server will
  1090. * have to retransmit the backchannel request and we'll
  1091. * reprocess it. Since these ops are idempotent, there's no
  1092. * need to cache our reply at this time.
  1093. */
  1094. printk(KERN_NOTICE "RPC: Could not send backchannel reply "
  1095. "error: %d\n", task->tk_status);
  1096. xprt_conditional_disconnect(task->tk_xprt,
  1097. req->rq_connect_cookie);
  1098. break;
  1099. default:
  1100. /*
  1101. * We were unable to reply and will have to drop the
  1102. * request. The server should reconnect and retransmit.
  1103. */
  1104. BUG_ON(task->tk_status == -EAGAIN);
  1105. printk(KERN_NOTICE "RPC: Could not send backchannel reply "
  1106. "error: %d\n", task->tk_status);
  1107. break;
  1108. }
  1109. rpc_wake_up_queued_task(&req->rq_xprt->pending, task);
  1110. }
  1111. #endif /* CONFIG_NFS_V4_1 */
  1112. /*
  1113. * 6. Sort out the RPC call status
  1114. */
  1115. static void
  1116. call_status(struct rpc_task *task)
  1117. {
  1118. struct rpc_clnt *clnt = task->tk_client;
  1119. struct rpc_rqst *req = task->tk_rqstp;
  1120. int status;
  1121. if (req->rq_reply_bytes_recvd > 0 && !req->rq_bytes_sent)
  1122. task->tk_status = req->rq_reply_bytes_recvd;
  1123. dprint_status(task);
  1124. status = task->tk_status;
  1125. if (status >= 0) {
  1126. task->tk_action = call_decode;
  1127. return;
  1128. }
  1129. task->tk_status = 0;
  1130. switch(status) {
  1131. case -EHOSTDOWN:
  1132. case -EHOSTUNREACH:
  1133. case -ENETUNREACH:
  1134. /*
  1135. * Delay any retries for 3 seconds, then handle as if it
  1136. * were a timeout.
  1137. */
  1138. rpc_delay(task, 3*HZ);
  1139. case -ETIMEDOUT:
  1140. task->tk_action = call_timeout;
  1141. if (task->tk_client->cl_discrtry)
  1142. xprt_conditional_disconnect(task->tk_xprt,
  1143. req->rq_connect_cookie);
  1144. break;
  1145. case -ECONNRESET:
  1146. case -ECONNREFUSED:
  1147. rpc_force_rebind(clnt);
  1148. rpc_delay(task, 3*HZ);
  1149. case -EPIPE:
  1150. case -ENOTCONN:
  1151. task->tk_action = call_bind;
  1152. break;
  1153. case -EAGAIN:
  1154. task->tk_action = call_transmit;
  1155. break;
  1156. case -EIO:
  1157. /* shutdown or soft timeout */
  1158. rpc_exit(task, status);
  1159. break;
  1160. default:
  1161. if (clnt->cl_chatty)
  1162. printk("%s: RPC call returned error %d\n",
  1163. clnt->cl_protname, -status);
  1164. rpc_exit(task, status);
  1165. }
  1166. }
  1167. /*
  1168. * 6a. Handle RPC timeout
  1169. * We do not release the request slot, so we keep using the
  1170. * same XID for all retransmits.
  1171. */
  1172. static void
  1173. call_timeout(struct rpc_task *task)
  1174. {
  1175. struct rpc_clnt *clnt = task->tk_client;
  1176. if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
  1177. dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid);
  1178. goto retry;
  1179. }
  1180. dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
  1181. task->tk_timeouts++;
  1182. if (RPC_IS_SOFT(task)) {
  1183. if (clnt->cl_chatty)
  1184. printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
  1185. clnt->cl_protname, clnt->cl_server);
  1186. rpc_exit(task, -EIO);
  1187. return;
  1188. }
  1189. if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
  1190. task->tk_flags |= RPC_CALL_MAJORSEEN;
  1191. if (clnt->cl_chatty)
  1192. printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
  1193. clnt->cl_protname, clnt->cl_server);
  1194. }
  1195. rpc_force_rebind(clnt);
  1196. /*
  1197. * Did our request time out due to an RPCSEC_GSS out-of-sequence
  1198. * event? RFC2203 requires the server to drop all such requests.
  1199. */
  1200. rpcauth_invalcred(task);
  1201. retry:
  1202. clnt->cl_stats->rpcretrans++;
  1203. task->tk_action = call_bind;
  1204. task->tk_status = 0;
  1205. }
  1206. /*
  1207. * 7. Decode the RPC reply
  1208. */
  1209. static void
  1210. call_decode(struct rpc_task *task)
  1211. {
  1212. struct rpc_clnt *clnt = task->tk_client;
  1213. struct rpc_rqst *req = task->tk_rqstp;
  1214. kxdrproc_t decode = task->tk_msg.rpc_proc->p_decode;
  1215. __be32 *p;
  1216. dprintk("RPC: %5u call_decode (status %d)\n",
  1217. task->tk_pid, task->tk_status);
  1218. if (task->tk_flags & RPC_CALL_MAJORSEEN) {
  1219. if (clnt->cl_chatty)
  1220. printk(KERN_NOTICE "%s: server %s OK\n",
  1221. clnt->cl_protname, clnt->cl_server);
  1222. task->tk_flags &= ~RPC_CALL_MAJORSEEN;
  1223. }
  1224. /*
  1225. * Ensure that we see all writes made by xprt_complete_rqst()
  1226. * before it changed req->rq_reply_bytes_recvd.
  1227. */
  1228. smp_rmb();
  1229. req->rq_rcv_buf.len = req->rq_private_buf.len;
  1230. /* Check that the softirq receive buffer is valid */
  1231. WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
  1232. sizeof(req->rq_rcv_buf)) != 0);
  1233. if (req->rq_rcv_buf.len < 12) {
  1234. if (!RPC_IS_SOFT(task)) {
  1235. task->tk_action = call_bind;
  1236. clnt->cl_stats->rpcretrans++;
  1237. goto out_retry;
  1238. }
  1239. dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
  1240. clnt->cl_protname, task->tk_status);
  1241. task->tk_action = call_timeout;
  1242. goto out_retry;
  1243. }
  1244. p = rpc_verify_header(task);
  1245. if (IS_ERR(p)) {
  1246. if (p == ERR_PTR(-EAGAIN))
  1247. goto out_retry;
  1248. return;
  1249. }
  1250. task->tk_action = rpc_exit_task;
  1251. if (decode) {
  1252. task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
  1253. task->tk_msg.rpc_resp);
  1254. }
  1255. dprintk("RPC: %5u call_decode result %d\n", task->tk_pid,
  1256. task->tk_status);
  1257. return;
  1258. out_retry:
  1259. task->tk_status = 0;
  1260. /* Note: rpc_verify_header() may have freed the RPC slot */
  1261. if (task->tk_rqstp == req) {
  1262. req->rq_reply_bytes_recvd = req->rq_rcv_buf.len = 0;
  1263. if (task->tk_client->cl_discrtry)
  1264. xprt_conditional_disconnect(task->tk_xprt,
  1265. req->rq_connect_cookie);
  1266. }
  1267. }
  1268. /*
  1269. * 8. Refresh the credentials if rejected by the server
  1270. */
  1271. static void
  1272. call_refresh(struct rpc_task *task)
  1273. {
  1274. dprint_status(task);
  1275. task->tk_action = call_refreshresult;
  1276. task->tk_status = 0;
  1277. task->tk_client->cl_stats->rpcauthrefresh++;
  1278. rpcauth_refreshcred(task);
  1279. }
  1280. /*
  1281. * 8a. Process the results of a credential refresh
  1282. */
  1283. static void
  1284. call_refreshresult(struct rpc_task *task)
  1285. {
  1286. int status = task->tk_status;
  1287. dprint_status(task);
  1288. task->tk_status = 0;
  1289. task->tk_action = call_reserve;
  1290. if (status >= 0 && rpcauth_uptodatecred(task))
  1291. return;
  1292. if (status == -EACCES) {
  1293. rpc_exit(task, -EACCES);
  1294. return;
  1295. }
  1296. task->tk_action = call_refresh;
  1297. if (status != -ETIMEDOUT)
  1298. rpc_delay(task, 3*HZ);
  1299. return;
  1300. }
  1301. static __be32 *
  1302. rpc_encode_header(struct rpc_task *task)
  1303. {
  1304. struct rpc_clnt *clnt = task->tk_client;
  1305. struct rpc_rqst *req = task->tk_rqstp;
  1306. __be32 *p = req->rq_svec[0].iov_base;
  1307. /* FIXME: check buffer size? */
  1308. p = xprt_skip_transport_header(task->tk_xprt, p);
  1309. *p++ = req->rq_xid; /* XID */
  1310. *p++ = htonl(RPC_CALL); /* CALL */
  1311. *p++ = htonl(RPC_VERSION); /* RPC version */
  1312. *p++ = htonl(clnt->cl_prog); /* program number */
  1313. *p++ = htonl(clnt->cl_vers); /* program version */
  1314. *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
  1315. p = rpcauth_marshcred(task, p);
  1316. req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
  1317. return p;
  1318. }
  1319. static __be32 *
  1320. rpc_verify_header(struct rpc_task *task)
  1321. {
  1322. struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
  1323. int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
  1324. __be32 *p = iov->iov_base;
  1325. u32 n;
  1326. int error = -EACCES;
  1327. if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
  1328. /* RFC-1014 says that the representation of XDR data must be a
  1329. * multiple of four bytes
  1330. * - if it isn't pointer subtraction in the NFS client may give
  1331. * undefined results
  1332. */
  1333. dprintk("RPC: %5u %s: XDR representation not a multiple of"
  1334. " 4 bytes: 0x%x\n", task->tk_pid, __func__,
  1335. task->tk_rqstp->rq_rcv_buf.len);
  1336. goto out_eio;
  1337. }
  1338. if ((len -= 3) < 0)
  1339. goto out_overflow;
  1340. p += 1; /* skip XID */
  1341. if ((n = ntohl(*p++)) != RPC_REPLY) {
  1342. dprintk("RPC: %5u %s: not an RPC reply: %x\n",
  1343. task->tk_pid, __func__, n);
  1344. goto out_garbage;
  1345. }
  1346. if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
  1347. if (--len < 0)
  1348. goto out_overflow;
  1349. switch ((n = ntohl(*p++))) {
  1350. case RPC_AUTH_ERROR:
  1351. break;
  1352. case RPC_MISMATCH:
  1353. dprintk("RPC: %5u %s: RPC call version "
  1354. "mismatch!\n",
  1355. task->tk_pid, __func__);
  1356. error = -EPROTONOSUPPORT;
  1357. goto out_err;
  1358. default:
  1359. dprintk("RPC: %5u %s: RPC call rejected, "
  1360. "unknown error: %x\n",
  1361. task->tk_pid, __func__, n);
  1362. goto out_eio;
  1363. }
  1364. if (--len < 0)
  1365. goto out_overflow;
  1366. switch ((n = ntohl(*p++))) {
  1367. case RPC_AUTH_REJECTEDCRED:
  1368. case RPC_AUTH_REJECTEDVERF:
  1369. case RPCSEC_GSS_CREDPROBLEM:
  1370. case RPCSEC_GSS_CTXPROBLEM:
  1371. if (!task->tk_cred_retry)
  1372. break;
  1373. task->tk_cred_retry--;
  1374. dprintk("RPC: %5u %s: retry stale creds\n",
  1375. task->tk_pid, __func__);
  1376. rpcauth_invalcred(task);
  1377. /* Ensure we obtain a new XID! */
  1378. xprt_release(task);
  1379. task->tk_action = call_refresh;
  1380. goto out_retry;
  1381. case RPC_AUTH_BADCRED:
  1382. case RPC_AUTH_BADVERF:
  1383. /* possibly garbled cred/verf? */
  1384. if (!task->tk_garb_retry)
  1385. break;
  1386. task->tk_garb_retry--;
  1387. dprintk("RPC: %5u %s: retry garbled creds\n",
  1388. task->tk_pid, __func__);
  1389. task->tk_action = call_bind;
  1390. goto out_retry;
  1391. case RPC_AUTH_TOOWEAK:
  1392. printk(KERN_NOTICE "RPC: server %s requires stronger "
  1393. "authentication.\n", task->tk_client->cl_server);
  1394. break;
  1395. default:
  1396. dprintk("RPC: %5u %s: unknown auth error: %x\n",
  1397. task->tk_pid, __func__, n);
  1398. error = -EIO;
  1399. }
  1400. dprintk("RPC: %5u %s: call rejected %d\n",
  1401. task->tk_pid, __func__, n);
  1402. goto out_err;
  1403. }
  1404. if (!(p = rpcauth_checkverf(task, p))) {
  1405. dprintk("RPC: %5u %s: auth check failed\n",
  1406. task->tk_pid, __func__);
  1407. goto out_garbage; /* bad verifier, retry */
  1408. }
  1409. len = p - (__be32 *)iov->iov_base - 1;
  1410. if (len < 0)
  1411. goto out_overflow;
  1412. switch ((n = ntohl(*p++))) {
  1413. case RPC_SUCCESS:
  1414. return p;
  1415. case RPC_PROG_UNAVAIL:
  1416. dprintk("RPC: %5u %s: program %u is unsupported by server %s\n",
  1417. task->tk_pid, __func__,
  1418. (unsigned int)task->tk_client->cl_prog,
  1419. task->tk_client->cl_server);
  1420. error = -EPFNOSUPPORT;
  1421. goto out_err;
  1422. case RPC_PROG_MISMATCH:
  1423. dprintk("RPC: %5u %s: program %u, version %u unsupported by "
  1424. "server %s\n", task->tk_pid, __func__,
  1425. (unsigned int)task->tk_client->cl_prog,
  1426. (unsigned int)task->tk_client->cl_vers,
  1427. task->tk_client->cl_server);
  1428. error = -EPROTONOSUPPORT;
  1429. goto out_err;
  1430. case RPC_PROC_UNAVAIL:
  1431. dprintk("RPC: %5u %s: proc %s unsupported by program %u, "
  1432. "version %u on server %s\n",
  1433. task->tk_pid, __func__,
  1434. rpc_proc_name(task),
  1435. task->tk_client->cl_prog,
  1436. task->tk_client->cl_vers,
  1437. task->tk_client->cl_server);
  1438. error = -EOPNOTSUPP;
  1439. goto out_err;
  1440. case RPC_GARBAGE_ARGS:
  1441. dprintk("RPC: %5u %s: server saw garbage\n",
  1442. task->tk_pid, __func__);
  1443. break; /* retry */
  1444. default:
  1445. dprintk("RPC: %5u %s: server accept status: %x\n",
  1446. task->tk_pid, __func__, n);
  1447. /* Also retry */
  1448. }
  1449. out_garbage:
  1450. task->tk_client->cl_stats->rpcgarbage++;
  1451. if (task->tk_garb_retry) {
  1452. task->tk_garb_retry--;
  1453. dprintk("RPC: %5u %s: retrying\n",
  1454. task->tk_pid, __func__);
  1455. task->tk_action = call_bind;
  1456. out_retry:
  1457. return ERR_PTR(-EAGAIN);
  1458. }
  1459. out_eio:
  1460. error = -EIO;
  1461. out_err:
  1462. rpc_exit(task, error);
  1463. dprintk("RPC: %5u %s: call failed with error %d\n", task->tk_pid,
  1464. __func__, error);
  1465. return ERR_PTR(error);
  1466. out_overflow:
  1467. dprintk("RPC: %5u %s: server reply was truncated.\n", task->tk_pid,
  1468. __func__);
  1469. goto out_garbage;
  1470. }
  1471. static int rpcproc_encode_null(void *rqstp, __be32 *data, void *obj)
  1472. {
  1473. return 0;
  1474. }
  1475. static int rpcproc_decode_null(void *rqstp, __be32 *data, void *obj)
  1476. {
  1477. return 0;
  1478. }
  1479. static struct rpc_procinfo rpcproc_null = {
  1480. .p_encode = rpcproc_encode_null,
  1481. .p_decode = rpcproc_decode_null,
  1482. };
  1483. static int rpc_ping(struct rpc_clnt *clnt, int flags)
  1484. {
  1485. struct rpc_message msg = {
  1486. .rpc_proc = &rpcproc_null,
  1487. };
  1488. int err;
  1489. msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
  1490. err = rpc_call_sync(clnt, &msg, flags);
  1491. put_rpccred(msg.rpc_cred);
  1492. return err;
  1493. }
  1494. struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
  1495. {
  1496. struct rpc_message msg = {
  1497. .rpc_proc = &rpcproc_null,
  1498. .rpc_cred = cred,
  1499. };
  1500. struct rpc_task_setup task_setup_data = {
  1501. .rpc_client = clnt,
  1502. .rpc_message = &msg,
  1503. .callback_ops = &rpc_default_ops,
  1504. .flags = flags,
  1505. };
  1506. return rpc_run_task(&task_setup_data);
  1507. }
  1508. EXPORT_SYMBOL_GPL(rpc_call_null);
  1509. #ifdef RPC_DEBUG
  1510. static void rpc_show_header(void)
  1511. {
  1512. printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
  1513. "-timeout ---ops--\n");
  1514. }
  1515. static void rpc_show_task(const struct rpc_clnt *clnt,
  1516. const struct rpc_task *task)
  1517. {
  1518. const char *rpc_waitq = "none";
  1519. char *p, action[KSYM_SYMBOL_LEN];
  1520. if (RPC_IS_QUEUED(task))
  1521. rpc_waitq = rpc_qname(task->tk_waitqueue);
  1522. /* map tk_action pointer to a function name; then trim off
  1523. * the "+0x0 [sunrpc]" */
  1524. sprint_symbol(action, (unsigned long)task->tk_action);
  1525. p = strchr(action, '+');
  1526. if (p)
  1527. *p = '\0';
  1528. printk(KERN_INFO "%5u %04x %6d %8p %8p %8ld %8p %sv%u %s a:%s q:%s\n",
  1529. task->tk_pid, task->tk_flags, task->tk_status,
  1530. clnt, task->tk_rqstp, task->tk_timeout, task->tk_ops,
  1531. clnt->cl_protname, clnt->cl_vers, rpc_proc_name(task),
  1532. action, rpc_waitq);
  1533. }
  1534. void rpc_show_tasks(void)
  1535. {
  1536. struct rpc_clnt *clnt;
  1537. struct rpc_task *task;
  1538. int header = 0;
  1539. spin_lock(&rpc_client_lock);
  1540. list_for_each_entry(clnt, &all_clients, cl_clients) {
  1541. spin_lock(&clnt->cl_lock);
  1542. list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
  1543. if (!header) {
  1544. rpc_show_header();
  1545. header++;
  1546. }
  1547. rpc_show_task(clnt, task);
  1548. }
  1549. spin_unlock(&clnt->cl_lock);
  1550. }
  1551. spin_unlock(&rpc_client_lock);
  1552. }
  1553. #endif