clnt.c 44 KB

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