clnt.c 52 KB

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