clnt.c 55 KB

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