clnt.c 55 KB

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