clnt.c 41 KB

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