clnt.c 42 KB

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