clnt.c 54 KB

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