clnt.c 54 KB

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