clnt.c 53 KB

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