clnt.c 53 KB

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