clnt.c 44 KB

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