clnt.c 42 KB

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