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