clnt.c 53 KB

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