clnt.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333
  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 (clnt->cl_noretranstimeo)
  683. task->tk_flags |= RPC_TASK_NO_RETRANS_TIMEOUT;
  684. if (sk_memalloc_socks()) {
  685. struct rpc_xprt *xprt;
  686. rcu_read_lock();
  687. xprt = rcu_dereference(clnt->cl_xprt);
  688. if (xprt->swapper)
  689. task->tk_flags |= RPC_TASK_SWAPPER;
  690. rcu_read_unlock();
  691. }
  692. /* Add to the client's list of all tasks */
  693. spin_lock(&clnt->cl_lock);
  694. list_add_tail(&task->tk_task, &clnt->cl_tasks);
  695. spin_unlock(&clnt->cl_lock);
  696. }
  697. }
  698. void rpc_task_reset_client(struct rpc_task *task, struct rpc_clnt *clnt)
  699. {
  700. rpc_task_release_client(task);
  701. rpc_task_set_client(task, clnt);
  702. }
  703. EXPORT_SYMBOL_GPL(rpc_task_reset_client);
  704. static void
  705. rpc_task_set_rpc_message(struct rpc_task *task, const struct rpc_message *msg)
  706. {
  707. if (msg != NULL) {
  708. task->tk_msg.rpc_proc = msg->rpc_proc;
  709. task->tk_msg.rpc_argp = msg->rpc_argp;
  710. task->tk_msg.rpc_resp = msg->rpc_resp;
  711. if (msg->rpc_cred != NULL)
  712. task->tk_msg.rpc_cred = get_rpccred(msg->rpc_cred);
  713. }
  714. }
  715. /*
  716. * Default callback for async RPC calls
  717. */
  718. static void
  719. rpc_default_callback(struct rpc_task *task, void *data)
  720. {
  721. }
  722. static const struct rpc_call_ops rpc_default_ops = {
  723. .rpc_call_done = rpc_default_callback,
  724. };
  725. /**
  726. * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
  727. * @task_setup_data: pointer to task initialisation data
  728. */
  729. struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
  730. {
  731. struct rpc_task *task;
  732. task = rpc_new_task(task_setup_data);
  733. if (IS_ERR(task))
  734. goto out;
  735. rpc_task_set_client(task, task_setup_data->rpc_client);
  736. rpc_task_set_rpc_message(task, task_setup_data->rpc_message);
  737. if (task->tk_action == NULL)
  738. rpc_call_start(task);
  739. atomic_inc(&task->tk_count);
  740. rpc_execute(task);
  741. out:
  742. return task;
  743. }
  744. EXPORT_SYMBOL_GPL(rpc_run_task);
  745. /**
  746. * rpc_call_sync - Perform a synchronous RPC call
  747. * @clnt: pointer to RPC client
  748. * @msg: RPC call parameters
  749. * @flags: RPC call flags
  750. */
  751. int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags)
  752. {
  753. struct rpc_task *task;
  754. struct rpc_task_setup task_setup_data = {
  755. .rpc_client = clnt,
  756. .rpc_message = msg,
  757. .callback_ops = &rpc_default_ops,
  758. .flags = flags,
  759. };
  760. int status;
  761. WARN_ON_ONCE(flags & RPC_TASK_ASYNC);
  762. if (flags & RPC_TASK_ASYNC) {
  763. rpc_release_calldata(task_setup_data.callback_ops,
  764. task_setup_data.callback_data);
  765. return -EINVAL;
  766. }
  767. task = rpc_run_task(&task_setup_data);
  768. if (IS_ERR(task))
  769. return PTR_ERR(task);
  770. status = task->tk_status;
  771. rpc_put_task(task);
  772. return status;
  773. }
  774. EXPORT_SYMBOL_GPL(rpc_call_sync);
  775. /**
  776. * rpc_call_async - Perform an asynchronous RPC call
  777. * @clnt: pointer to RPC client
  778. * @msg: RPC call parameters
  779. * @flags: RPC call flags
  780. * @tk_ops: RPC call ops
  781. * @data: user call data
  782. */
  783. int
  784. rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags,
  785. const struct rpc_call_ops *tk_ops, void *data)
  786. {
  787. struct rpc_task *task;
  788. struct rpc_task_setup task_setup_data = {
  789. .rpc_client = clnt,
  790. .rpc_message = msg,
  791. .callback_ops = tk_ops,
  792. .callback_data = data,
  793. .flags = flags|RPC_TASK_ASYNC,
  794. };
  795. task = rpc_run_task(&task_setup_data);
  796. if (IS_ERR(task))
  797. return PTR_ERR(task);
  798. rpc_put_task(task);
  799. return 0;
  800. }
  801. EXPORT_SYMBOL_GPL(rpc_call_async);
  802. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  803. /**
  804. * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
  805. * rpc_execute against it
  806. * @req: RPC request
  807. * @tk_ops: RPC call ops
  808. */
  809. struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req,
  810. const struct rpc_call_ops *tk_ops)
  811. {
  812. struct rpc_task *task;
  813. struct xdr_buf *xbufp = &req->rq_snd_buf;
  814. struct rpc_task_setup task_setup_data = {
  815. .callback_ops = tk_ops,
  816. };
  817. dprintk("RPC: rpc_run_bc_task req= %p\n", req);
  818. /*
  819. * Create an rpc_task to send the data
  820. */
  821. task = rpc_new_task(&task_setup_data);
  822. if (IS_ERR(task)) {
  823. xprt_free_bc_request(req);
  824. goto out;
  825. }
  826. task->tk_rqstp = req;
  827. /*
  828. * Set up the xdr_buf length.
  829. * This also indicates that the buffer is XDR encoded already.
  830. */
  831. xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
  832. xbufp->tail[0].iov_len;
  833. task->tk_action = call_bc_transmit;
  834. atomic_inc(&task->tk_count);
  835. WARN_ON_ONCE(atomic_read(&task->tk_count) != 2);
  836. rpc_execute(task);
  837. out:
  838. dprintk("RPC: rpc_run_bc_task: task= %p\n", task);
  839. return task;
  840. }
  841. #endif /* CONFIG_SUNRPC_BACKCHANNEL */
  842. void
  843. rpc_call_start(struct rpc_task *task)
  844. {
  845. task->tk_action = call_start;
  846. }
  847. EXPORT_SYMBOL_GPL(rpc_call_start);
  848. /**
  849. * rpc_peeraddr - extract remote peer address from clnt's xprt
  850. * @clnt: RPC client structure
  851. * @buf: target buffer
  852. * @bufsize: length of target buffer
  853. *
  854. * Returns the number of bytes that are actually in the stored address.
  855. */
  856. size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
  857. {
  858. size_t bytes;
  859. struct rpc_xprt *xprt;
  860. rcu_read_lock();
  861. xprt = rcu_dereference(clnt->cl_xprt);
  862. bytes = xprt->addrlen;
  863. if (bytes > bufsize)
  864. bytes = bufsize;
  865. memcpy(buf, &xprt->addr, bytes);
  866. rcu_read_unlock();
  867. return bytes;
  868. }
  869. EXPORT_SYMBOL_GPL(rpc_peeraddr);
  870. /**
  871. * rpc_peeraddr2str - return remote peer address in printable format
  872. * @clnt: RPC client structure
  873. * @format: address format
  874. *
  875. * NB: the lifetime of the memory referenced by the returned pointer is
  876. * the same as the rpc_xprt itself. As long as the caller uses this
  877. * pointer, it must hold the RCU read lock.
  878. */
  879. const char *rpc_peeraddr2str(struct rpc_clnt *clnt,
  880. enum rpc_display_format_t format)
  881. {
  882. struct rpc_xprt *xprt;
  883. xprt = rcu_dereference(clnt->cl_xprt);
  884. if (xprt->address_strings[format] != NULL)
  885. return xprt->address_strings[format];
  886. else
  887. return "unprintable";
  888. }
  889. EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
  890. static const struct sockaddr_in rpc_inaddr_loopback = {
  891. .sin_family = AF_INET,
  892. .sin_addr.s_addr = htonl(INADDR_ANY),
  893. };
  894. static const struct sockaddr_in6 rpc_in6addr_loopback = {
  895. .sin6_family = AF_INET6,
  896. .sin6_addr = IN6ADDR_ANY_INIT,
  897. };
  898. /*
  899. * Try a getsockname() on a connected datagram socket. Using a
  900. * connected datagram socket prevents leaving a socket in TIME_WAIT.
  901. * This conserves the ephemeral port number space.
  902. *
  903. * Returns zero and fills in "buf" if successful; otherwise, a
  904. * negative errno is returned.
  905. */
  906. static int rpc_sockname(struct net *net, struct sockaddr *sap, size_t salen,
  907. struct sockaddr *buf, int buflen)
  908. {
  909. struct socket *sock;
  910. int err;
  911. err = __sock_create(net, sap->sa_family,
  912. SOCK_DGRAM, IPPROTO_UDP, &sock, 1);
  913. if (err < 0) {
  914. dprintk("RPC: can't create UDP socket (%d)\n", err);
  915. goto out;
  916. }
  917. switch (sap->sa_family) {
  918. case AF_INET:
  919. err = kernel_bind(sock,
  920. (struct sockaddr *)&rpc_inaddr_loopback,
  921. sizeof(rpc_inaddr_loopback));
  922. break;
  923. case AF_INET6:
  924. err = kernel_bind(sock,
  925. (struct sockaddr *)&rpc_in6addr_loopback,
  926. sizeof(rpc_in6addr_loopback));
  927. break;
  928. default:
  929. err = -EAFNOSUPPORT;
  930. goto out;
  931. }
  932. if (err < 0) {
  933. dprintk("RPC: can't bind UDP socket (%d)\n", err);
  934. goto out_release;
  935. }
  936. err = kernel_connect(sock, sap, salen, 0);
  937. if (err < 0) {
  938. dprintk("RPC: can't connect UDP socket (%d)\n", err);
  939. goto out_release;
  940. }
  941. err = kernel_getsockname(sock, buf, &buflen);
  942. if (err < 0) {
  943. dprintk("RPC: getsockname failed (%d)\n", err);
  944. goto out_release;
  945. }
  946. err = 0;
  947. if (buf->sa_family == AF_INET6) {
  948. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
  949. sin6->sin6_scope_id = 0;
  950. }
  951. dprintk("RPC: %s succeeded\n", __func__);
  952. out_release:
  953. sock_release(sock);
  954. out:
  955. return err;
  956. }
  957. /*
  958. * Scraping a connected socket failed, so we don't have a useable
  959. * local address. Fallback: generate an address that will prevent
  960. * the server from calling us back.
  961. *
  962. * Returns zero and fills in "buf" if successful; otherwise, a
  963. * negative errno is returned.
  964. */
  965. static int rpc_anyaddr(int family, struct sockaddr *buf, size_t buflen)
  966. {
  967. switch (family) {
  968. case AF_INET:
  969. if (buflen < sizeof(rpc_inaddr_loopback))
  970. return -EINVAL;
  971. memcpy(buf, &rpc_inaddr_loopback,
  972. sizeof(rpc_inaddr_loopback));
  973. break;
  974. case AF_INET6:
  975. if (buflen < sizeof(rpc_in6addr_loopback))
  976. return -EINVAL;
  977. memcpy(buf, &rpc_in6addr_loopback,
  978. sizeof(rpc_in6addr_loopback));
  979. default:
  980. dprintk("RPC: %s: address family not supported\n",
  981. __func__);
  982. return -EAFNOSUPPORT;
  983. }
  984. dprintk("RPC: %s: succeeded\n", __func__);
  985. return 0;
  986. }
  987. /**
  988. * rpc_localaddr - discover local endpoint address for an RPC client
  989. * @clnt: RPC client structure
  990. * @buf: target buffer
  991. * @buflen: size of target buffer, in bytes
  992. *
  993. * Returns zero and fills in "buf" and "buflen" if successful;
  994. * otherwise, a negative errno is returned.
  995. *
  996. * This works even if the underlying transport is not currently connected,
  997. * or if the upper layer never previously provided a source address.
  998. *
  999. * The result of this function call is transient: multiple calls in
  1000. * succession may give different results, depending on how local
  1001. * networking configuration changes over time.
  1002. */
  1003. int rpc_localaddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t buflen)
  1004. {
  1005. struct sockaddr_storage address;
  1006. struct sockaddr *sap = (struct sockaddr *)&address;
  1007. struct rpc_xprt *xprt;
  1008. struct net *net;
  1009. size_t salen;
  1010. int err;
  1011. rcu_read_lock();
  1012. xprt = rcu_dereference(clnt->cl_xprt);
  1013. salen = xprt->addrlen;
  1014. memcpy(sap, &xprt->addr, salen);
  1015. net = get_net(xprt->xprt_net);
  1016. rcu_read_unlock();
  1017. rpc_set_port(sap, 0);
  1018. err = rpc_sockname(net, sap, salen, buf, buflen);
  1019. put_net(net);
  1020. if (err != 0)
  1021. /* Couldn't discover local address, return ANYADDR */
  1022. return rpc_anyaddr(sap->sa_family, buf, buflen);
  1023. return 0;
  1024. }
  1025. EXPORT_SYMBOL_GPL(rpc_localaddr);
  1026. void
  1027. rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
  1028. {
  1029. struct rpc_xprt *xprt;
  1030. rcu_read_lock();
  1031. xprt = rcu_dereference(clnt->cl_xprt);
  1032. if (xprt->ops->set_buffer_size)
  1033. xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
  1034. rcu_read_unlock();
  1035. }
  1036. EXPORT_SYMBOL_GPL(rpc_setbufsize);
  1037. /**
  1038. * rpc_protocol - Get transport protocol number for an RPC client
  1039. * @clnt: RPC client to query
  1040. *
  1041. */
  1042. int rpc_protocol(struct rpc_clnt *clnt)
  1043. {
  1044. int protocol;
  1045. rcu_read_lock();
  1046. protocol = rcu_dereference(clnt->cl_xprt)->prot;
  1047. rcu_read_unlock();
  1048. return protocol;
  1049. }
  1050. EXPORT_SYMBOL_GPL(rpc_protocol);
  1051. /**
  1052. * rpc_net_ns - Get the network namespace for this RPC client
  1053. * @clnt: RPC client to query
  1054. *
  1055. */
  1056. struct net *rpc_net_ns(struct rpc_clnt *clnt)
  1057. {
  1058. struct net *ret;
  1059. rcu_read_lock();
  1060. ret = rcu_dereference(clnt->cl_xprt)->xprt_net;
  1061. rcu_read_unlock();
  1062. return ret;
  1063. }
  1064. EXPORT_SYMBOL_GPL(rpc_net_ns);
  1065. /**
  1066. * rpc_max_payload - Get maximum payload size for a transport, in bytes
  1067. * @clnt: RPC client to query
  1068. *
  1069. * For stream transports, this is one RPC record fragment (see RFC
  1070. * 1831), as we don't support multi-record requests yet. For datagram
  1071. * transports, this is the size of an IP packet minus the IP, UDP, and
  1072. * RPC header sizes.
  1073. */
  1074. size_t rpc_max_payload(struct rpc_clnt *clnt)
  1075. {
  1076. size_t ret;
  1077. rcu_read_lock();
  1078. ret = rcu_dereference(clnt->cl_xprt)->max_payload;
  1079. rcu_read_unlock();
  1080. return ret;
  1081. }
  1082. EXPORT_SYMBOL_GPL(rpc_max_payload);
  1083. /**
  1084. * rpc_get_timeout - Get timeout for transport in units of HZ
  1085. * @clnt: RPC client to query
  1086. */
  1087. unsigned long rpc_get_timeout(struct rpc_clnt *clnt)
  1088. {
  1089. unsigned long ret;
  1090. rcu_read_lock();
  1091. ret = rcu_dereference(clnt->cl_xprt)->timeout->to_initval;
  1092. rcu_read_unlock();
  1093. return ret;
  1094. }
  1095. EXPORT_SYMBOL_GPL(rpc_get_timeout);
  1096. /**
  1097. * rpc_force_rebind - force transport to check that remote port is unchanged
  1098. * @clnt: client to rebind
  1099. *
  1100. */
  1101. void rpc_force_rebind(struct rpc_clnt *clnt)
  1102. {
  1103. if (clnt->cl_autobind) {
  1104. rcu_read_lock();
  1105. xprt_clear_bound(rcu_dereference(clnt->cl_xprt));
  1106. rcu_read_unlock();
  1107. }
  1108. }
  1109. EXPORT_SYMBOL_GPL(rpc_force_rebind);
  1110. /*
  1111. * Restart an (async) RPC call from the call_prepare state.
  1112. * Usually called from within the exit handler.
  1113. */
  1114. int
  1115. rpc_restart_call_prepare(struct rpc_task *task)
  1116. {
  1117. if (RPC_ASSASSINATED(task))
  1118. return 0;
  1119. task->tk_action = call_start;
  1120. if (task->tk_ops->rpc_call_prepare != NULL)
  1121. task->tk_action = rpc_prepare_task;
  1122. return 1;
  1123. }
  1124. EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
  1125. /*
  1126. * Restart an (async) RPC call. Usually called from within the
  1127. * exit handler.
  1128. */
  1129. int
  1130. rpc_restart_call(struct rpc_task *task)
  1131. {
  1132. if (RPC_ASSASSINATED(task))
  1133. return 0;
  1134. task->tk_action = call_start;
  1135. return 1;
  1136. }
  1137. EXPORT_SYMBOL_GPL(rpc_restart_call);
  1138. #ifdef RPC_DEBUG
  1139. static const char *rpc_proc_name(const struct rpc_task *task)
  1140. {
  1141. const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
  1142. if (proc) {
  1143. if (proc->p_name)
  1144. return proc->p_name;
  1145. else
  1146. return "NULL";
  1147. } else
  1148. return "no proc";
  1149. }
  1150. #endif
  1151. /*
  1152. * 0. Initial state
  1153. *
  1154. * Other FSM states can be visited zero or more times, but
  1155. * this state is visited exactly once for each RPC.
  1156. */
  1157. static void
  1158. call_start(struct rpc_task *task)
  1159. {
  1160. struct rpc_clnt *clnt = task->tk_client;
  1161. dprintk("RPC: %5u call_start %s%d proc %s (%s)\n", task->tk_pid,
  1162. clnt->cl_program->name, clnt->cl_vers,
  1163. rpc_proc_name(task),
  1164. (RPC_IS_ASYNC(task) ? "async" : "sync"));
  1165. /* Increment call count */
  1166. task->tk_msg.rpc_proc->p_count++;
  1167. clnt->cl_stats->rpccnt++;
  1168. task->tk_action = call_reserve;
  1169. }
  1170. /*
  1171. * 1. Reserve an RPC call slot
  1172. */
  1173. static void
  1174. call_reserve(struct rpc_task *task)
  1175. {
  1176. dprint_status(task);
  1177. task->tk_status = 0;
  1178. task->tk_action = call_reserveresult;
  1179. xprt_reserve(task);
  1180. }
  1181. static void call_retry_reserve(struct rpc_task *task);
  1182. /*
  1183. * 1b. Grok the result of xprt_reserve()
  1184. */
  1185. static void
  1186. call_reserveresult(struct rpc_task *task)
  1187. {
  1188. int status = task->tk_status;
  1189. dprint_status(task);
  1190. /*
  1191. * After a call to xprt_reserve(), we must have either
  1192. * a request slot or else an error status.
  1193. */
  1194. task->tk_status = 0;
  1195. if (status >= 0) {
  1196. if (task->tk_rqstp) {
  1197. task->tk_action = call_refresh;
  1198. return;
  1199. }
  1200. printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
  1201. __func__, status);
  1202. rpc_exit(task, -EIO);
  1203. return;
  1204. }
  1205. /*
  1206. * Even though there was an error, we may have acquired
  1207. * a request slot somehow. Make sure not to leak it.
  1208. */
  1209. if (task->tk_rqstp) {
  1210. printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
  1211. __func__, status);
  1212. xprt_release(task);
  1213. }
  1214. switch (status) {
  1215. case -ENOMEM:
  1216. rpc_delay(task, HZ >> 2);
  1217. case -EAGAIN: /* woken up; retry */
  1218. task->tk_action = call_retry_reserve;
  1219. return;
  1220. case -EIO: /* probably a shutdown */
  1221. break;
  1222. default:
  1223. printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
  1224. __func__, status);
  1225. break;
  1226. }
  1227. rpc_exit(task, status);
  1228. }
  1229. /*
  1230. * 1c. Retry reserving an RPC call slot
  1231. */
  1232. static void
  1233. call_retry_reserve(struct rpc_task *task)
  1234. {
  1235. dprint_status(task);
  1236. task->tk_status = 0;
  1237. task->tk_action = call_reserveresult;
  1238. xprt_retry_reserve(task);
  1239. }
  1240. /*
  1241. * 2. Bind and/or refresh the credentials
  1242. */
  1243. static void
  1244. call_refresh(struct rpc_task *task)
  1245. {
  1246. dprint_status(task);
  1247. task->tk_action = call_refreshresult;
  1248. task->tk_status = 0;
  1249. task->tk_client->cl_stats->rpcauthrefresh++;
  1250. rpcauth_refreshcred(task);
  1251. }
  1252. /*
  1253. * 2a. Process the results of a credential refresh
  1254. */
  1255. static void
  1256. call_refreshresult(struct rpc_task *task)
  1257. {
  1258. int status = task->tk_status;
  1259. dprint_status(task);
  1260. task->tk_status = 0;
  1261. task->tk_action = call_refresh;
  1262. switch (status) {
  1263. case 0:
  1264. if (rpcauth_uptodatecred(task))
  1265. task->tk_action = call_allocate;
  1266. return;
  1267. case -ETIMEDOUT:
  1268. rpc_delay(task, 3*HZ);
  1269. case -EAGAIN:
  1270. status = -EACCES;
  1271. case -EKEYEXPIRED:
  1272. if (!task->tk_cred_retry)
  1273. break;
  1274. task->tk_cred_retry--;
  1275. dprintk("RPC: %5u %s: retry refresh creds\n",
  1276. task->tk_pid, __func__);
  1277. return;
  1278. }
  1279. dprintk("RPC: %5u %s: refresh creds failed with error %d\n",
  1280. task->tk_pid, __func__, status);
  1281. rpc_exit(task, status);
  1282. }
  1283. /*
  1284. * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc.
  1285. * (Note: buffer memory is freed in xprt_release).
  1286. */
  1287. static void
  1288. call_allocate(struct rpc_task *task)
  1289. {
  1290. unsigned int slack = task->tk_rqstp->rq_cred->cr_auth->au_cslack;
  1291. struct rpc_rqst *req = task->tk_rqstp;
  1292. struct rpc_xprt *xprt = req->rq_xprt;
  1293. struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
  1294. dprint_status(task);
  1295. task->tk_status = 0;
  1296. task->tk_action = call_bind;
  1297. if (req->rq_buffer)
  1298. return;
  1299. if (proc->p_proc != 0) {
  1300. BUG_ON(proc->p_arglen == 0);
  1301. if (proc->p_decode != NULL)
  1302. BUG_ON(proc->p_replen == 0);
  1303. }
  1304. /*
  1305. * Calculate the size (in quads) of the RPC call
  1306. * and reply headers, and convert both values
  1307. * to byte sizes.
  1308. */
  1309. req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen;
  1310. req->rq_callsize <<= 2;
  1311. req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen;
  1312. req->rq_rcvsize <<= 2;
  1313. req->rq_buffer = xprt->ops->buf_alloc(task,
  1314. req->rq_callsize + req->rq_rcvsize);
  1315. if (req->rq_buffer != NULL)
  1316. return;
  1317. dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
  1318. if (RPC_IS_ASYNC(task) || !fatal_signal_pending(current)) {
  1319. task->tk_action = call_allocate;
  1320. rpc_delay(task, HZ>>4);
  1321. return;
  1322. }
  1323. rpc_exit(task, -ERESTARTSYS);
  1324. }
  1325. static inline int
  1326. rpc_task_need_encode(struct rpc_task *task)
  1327. {
  1328. return task->tk_rqstp->rq_snd_buf.len == 0;
  1329. }
  1330. static inline void
  1331. rpc_task_force_reencode(struct rpc_task *task)
  1332. {
  1333. task->tk_rqstp->rq_snd_buf.len = 0;
  1334. task->tk_rqstp->rq_bytes_sent = 0;
  1335. }
  1336. static inline void
  1337. rpc_xdr_buf_init(struct xdr_buf *buf, void *start, size_t len)
  1338. {
  1339. buf->head[0].iov_base = start;
  1340. buf->head[0].iov_len = len;
  1341. buf->tail[0].iov_len = 0;
  1342. buf->page_len = 0;
  1343. buf->flags = 0;
  1344. buf->len = 0;
  1345. buf->buflen = len;
  1346. }
  1347. /*
  1348. * 3. Encode arguments of an RPC call
  1349. */
  1350. static void
  1351. rpc_xdr_encode(struct rpc_task *task)
  1352. {
  1353. struct rpc_rqst *req = task->tk_rqstp;
  1354. kxdreproc_t encode;
  1355. __be32 *p;
  1356. dprint_status(task);
  1357. rpc_xdr_buf_init(&req->rq_snd_buf,
  1358. req->rq_buffer,
  1359. req->rq_callsize);
  1360. rpc_xdr_buf_init(&req->rq_rcv_buf,
  1361. (char *)req->rq_buffer + req->rq_callsize,
  1362. req->rq_rcvsize);
  1363. p = rpc_encode_header(task);
  1364. if (p == NULL) {
  1365. printk(KERN_INFO "RPC: couldn't encode RPC header, exit EIO\n");
  1366. rpc_exit(task, -EIO);
  1367. return;
  1368. }
  1369. encode = task->tk_msg.rpc_proc->p_encode;
  1370. if (encode == NULL)
  1371. return;
  1372. task->tk_status = rpcauth_wrap_req(task, encode, req, p,
  1373. task->tk_msg.rpc_argp);
  1374. }
  1375. /*
  1376. * 4. Get the server port number if not yet set
  1377. */
  1378. static void
  1379. call_bind(struct rpc_task *task)
  1380. {
  1381. struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
  1382. dprint_status(task);
  1383. task->tk_action = call_connect;
  1384. if (!xprt_bound(xprt)) {
  1385. task->tk_action = call_bind_status;
  1386. task->tk_timeout = xprt->bind_timeout;
  1387. xprt->ops->rpcbind(task);
  1388. }
  1389. }
  1390. /*
  1391. * 4a. Sort out bind result
  1392. */
  1393. static void
  1394. call_bind_status(struct rpc_task *task)
  1395. {
  1396. int status = -EIO;
  1397. if (task->tk_status >= 0) {
  1398. dprint_status(task);
  1399. task->tk_status = 0;
  1400. task->tk_action = call_connect;
  1401. return;
  1402. }
  1403. trace_rpc_bind_status(task);
  1404. switch (task->tk_status) {
  1405. case -ENOMEM:
  1406. dprintk("RPC: %5u rpcbind out of memory\n", task->tk_pid);
  1407. rpc_delay(task, HZ >> 2);
  1408. goto retry_timeout;
  1409. case -EACCES:
  1410. dprintk("RPC: %5u remote rpcbind: RPC program/version "
  1411. "unavailable\n", task->tk_pid);
  1412. /* fail immediately if this is an RPC ping */
  1413. if (task->tk_msg.rpc_proc->p_proc == 0) {
  1414. status = -EOPNOTSUPP;
  1415. break;
  1416. }
  1417. if (task->tk_rebind_retry == 0)
  1418. break;
  1419. task->tk_rebind_retry--;
  1420. rpc_delay(task, 3*HZ);
  1421. goto retry_timeout;
  1422. case -ETIMEDOUT:
  1423. dprintk("RPC: %5u rpcbind request timed out\n",
  1424. task->tk_pid);
  1425. goto retry_timeout;
  1426. case -EPFNOSUPPORT:
  1427. /* server doesn't support any rpcbind version we know of */
  1428. dprintk("RPC: %5u unrecognized remote rpcbind service\n",
  1429. task->tk_pid);
  1430. break;
  1431. case -EPROTONOSUPPORT:
  1432. dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
  1433. task->tk_pid);
  1434. task->tk_status = 0;
  1435. task->tk_action = call_bind;
  1436. return;
  1437. case -ECONNREFUSED: /* connection problems */
  1438. case -ECONNRESET:
  1439. case -ENOTCONN:
  1440. case -EHOSTDOWN:
  1441. case -EHOSTUNREACH:
  1442. case -ENETUNREACH:
  1443. case -EPIPE:
  1444. dprintk("RPC: %5u remote rpcbind unreachable: %d\n",
  1445. task->tk_pid, task->tk_status);
  1446. if (!RPC_IS_SOFTCONN(task)) {
  1447. rpc_delay(task, 5*HZ);
  1448. goto retry_timeout;
  1449. }
  1450. status = task->tk_status;
  1451. break;
  1452. default:
  1453. dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
  1454. task->tk_pid, -task->tk_status);
  1455. }
  1456. rpc_exit(task, status);
  1457. return;
  1458. retry_timeout:
  1459. task->tk_action = call_timeout;
  1460. }
  1461. /*
  1462. * 4b. Connect to the RPC server
  1463. */
  1464. static void
  1465. call_connect(struct rpc_task *task)
  1466. {
  1467. struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
  1468. dprintk("RPC: %5u call_connect xprt %p %s connected\n",
  1469. task->tk_pid, xprt,
  1470. (xprt_connected(xprt) ? "is" : "is not"));
  1471. task->tk_action = call_transmit;
  1472. if (!xprt_connected(xprt)) {
  1473. task->tk_action = call_connect_status;
  1474. if (task->tk_status < 0)
  1475. return;
  1476. if (task->tk_flags & RPC_TASK_NOCONNECT) {
  1477. rpc_exit(task, -ENOTCONN);
  1478. return;
  1479. }
  1480. xprt_connect(task);
  1481. }
  1482. }
  1483. /*
  1484. * 4c. Sort out connect result
  1485. */
  1486. static void
  1487. call_connect_status(struct rpc_task *task)
  1488. {
  1489. struct rpc_clnt *clnt = task->tk_client;
  1490. int status = task->tk_status;
  1491. dprint_status(task);
  1492. trace_rpc_connect_status(task, status);
  1493. switch (status) {
  1494. /* if soft mounted, test if we've timed out */
  1495. case -ETIMEDOUT:
  1496. task->tk_action = call_timeout;
  1497. return;
  1498. case -ECONNREFUSED:
  1499. case -ECONNRESET:
  1500. case -ENETUNREACH:
  1501. if (RPC_IS_SOFTCONN(task))
  1502. break;
  1503. /* retry with existing socket, after a delay */
  1504. case 0:
  1505. case -EAGAIN:
  1506. task->tk_status = 0;
  1507. clnt->cl_stats->netreconn++;
  1508. task->tk_action = call_transmit;
  1509. return;
  1510. }
  1511. rpc_exit(task, status);
  1512. }
  1513. /*
  1514. * 5. Transmit the RPC request, and wait for reply
  1515. */
  1516. static void
  1517. call_transmit(struct rpc_task *task)
  1518. {
  1519. dprint_status(task);
  1520. task->tk_action = call_status;
  1521. if (task->tk_status < 0)
  1522. return;
  1523. if (!xprt_prepare_transmit(task))
  1524. return;
  1525. task->tk_action = call_transmit_status;
  1526. /* Encode here so that rpcsec_gss can use correct sequence number. */
  1527. if (rpc_task_need_encode(task)) {
  1528. rpc_xdr_encode(task);
  1529. /* Did the encode result in an error condition? */
  1530. if (task->tk_status != 0) {
  1531. /* Was the error nonfatal? */
  1532. if (task->tk_status == -EAGAIN)
  1533. rpc_delay(task, HZ >> 4);
  1534. else
  1535. rpc_exit(task, task->tk_status);
  1536. return;
  1537. }
  1538. }
  1539. xprt_transmit(task);
  1540. if (task->tk_status < 0)
  1541. return;
  1542. /*
  1543. * On success, ensure that we call xprt_end_transmit() before sleeping
  1544. * in order to allow access to the socket to other RPC requests.
  1545. */
  1546. call_transmit_status(task);
  1547. if (rpc_reply_expected(task))
  1548. return;
  1549. task->tk_action = rpc_exit_task;
  1550. rpc_wake_up_queued_task(&task->tk_rqstp->rq_xprt->pending, task);
  1551. }
  1552. /*
  1553. * 5a. Handle cleanup after a transmission
  1554. */
  1555. static void
  1556. call_transmit_status(struct rpc_task *task)
  1557. {
  1558. task->tk_action = call_status;
  1559. /*
  1560. * Common case: success. Force the compiler to put this
  1561. * test first.
  1562. */
  1563. if (task->tk_status == 0) {
  1564. xprt_end_transmit(task);
  1565. rpc_task_force_reencode(task);
  1566. return;
  1567. }
  1568. switch (task->tk_status) {
  1569. case -EAGAIN:
  1570. break;
  1571. default:
  1572. dprint_status(task);
  1573. xprt_end_transmit(task);
  1574. rpc_task_force_reencode(task);
  1575. break;
  1576. /*
  1577. * Special cases: if we've been waiting on the
  1578. * socket's write_space() callback, or if the
  1579. * socket just returned a connection error,
  1580. * then hold onto the transport lock.
  1581. */
  1582. case -ECONNREFUSED:
  1583. case -EHOSTDOWN:
  1584. case -EHOSTUNREACH:
  1585. case -ENETUNREACH:
  1586. if (RPC_IS_SOFTCONN(task)) {
  1587. xprt_end_transmit(task);
  1588. rpc_exit(task, task->tk_status);
  1589. break;
  1590. }
  1591. case -ECONNRESET:
  1592. case -ENOTCONN:
  1593. case -EPIPE:
  1594. rpc_task_force_reencode(task);
  1595. }
  1596. }
  1597. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  1598. /*
  1599. * 5b. Send the backchannel RPC reply. On error, drop the reply. In
  1600. * addition, disconnect on connectivity errors.
  1601. */
  1602. static void
  1603. call_bc_transmit(struct rpc_task *task)
  1604. {
  1605. struct rpc_rqst *req = task->tk_rqstp;
  1606. if (!xprt_prepare_transmit(task)) {
  1607. /*
  1608. * Could not reserve the transport. Try again after the
  1609. * transport is released.
  1610. */
  1611. task->tk_status = 0;
  1612. task->tk_action = call_bc_transmit;
  1613. return;
  1614. }
  1615. task->tk_action = rpc_exit_task;
  1616. if (task->tk_status < 0) {
  1617. printk(KERN_NOTICE "RPC: Could not send backchannel reply "
  1618. "error: %d\n", task->tk_status);
  1619. return;
  1620. }
  1621. xprt_transmit(task);
  1622. xprt_end_transmit(task);
  1623. dprint_status(task);
  1624. switch (task->tk_status) {
  1625. case 0:
  1626. /* Success */
  1627. break;
  1628. case -EHOSTDOWN:
  1629. case -EHOSTUNREACH:
  1630. case -ENETUNREACH:
  1631. case -ETIMEDOUT:
  1632. /*
  1633. * Problem reaching the server. Disconnect and let the
  1634. * forechannel reestablish the connection. The server will
  1635. * have to retransmit the backchannel request and we'll
  1636. * reprocess it. Since these ops are idempotent, there's no
  1637. * need to cache our reply at this time.
  1638. */
  1639. printk(KERN_NOTICE "RPC: Could not send backchannel reply "
  1640. "error: %d\n", task->tk_status);
  1641. xprt_conditional_disconnect(req->rq_xprt,
  1642. req->rq_connect_cookie);
  1643. break;
  1644. default:
  1645. /*
  1646. * We were unable to reply and will have to drop the
  1647. * request. The server should reconnect and retransmit.
  1648. */
  1649. WARN_ON_ONCE(task->tk_status == -EAGAIN);
  1650. printk(KERN_NOTICE "RPC: Could not send backchannel reply "
  1651. "error: %d\n", task->tk_status);
  1652. break;
  1653. }
  1654. rpc_wake_up_queued_task(&req->rq_xprt->pending, task);
  1655. }
  1656. #endif /* CONFIG_SUNRPC_BACKCHANNEL */
  1657. /*
  1658. * 6. Sort out the RPC call status
  1659. */
  1660. static void
  1661. call_status(struct rpc_task *task)
  1662. {
  1663. struct rpc_clnt *clnt = task->tk_client;
  1664. struct rpc_rqst *req = task->tk_rqstp;
  1665. int status;
  1666. if (req->rq_reply_bytes_recvd > 0 && !req->rq_bytes_sent)
  1667. task->tk_status = req->rq_reply_bytes_recvd;
  1668. dprint_status(task);
  1669. status = task->tk_status;
  1670. if (status >= 0) {
  1671. task->tk_action = call_decode;
  1672. return;
  1673. }
  1674. trace_rpc_call_status(task);
  1675. task->tk_status = 0;
  1676. switch(status) {
  1677. case -EHOSTDOWN:
  1678. case -EHOSTUNREACH:
  1679. case -ENETUNREACH:
  1680. /*
  1681. * Delay any retries for 3 seconds, then handle as if it
  1682. * were a timeout.
  1683. */
  1684. rpc_delay(task, 3*HZ);
  1685. case -ETIMEDOUT:
  1686. task->tk_action = call_timeout;
  1687. if (!(task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT)
  1688. && task->tk_client->cl_discrtry)
  1689. xprt_conditional_disconnect(req->rq_xprt,
  1690. req->rq_connect_cookie);
  1691. break;
  1692. case -ECONNRESET:
  1693. case -ECONNREFUSED:
  1694. rpc_force_rebind(clnt);
  1695. rpc_delay(task, 3*HZ);
  1696. case -EPIPE:
  1697. case -ENOTCONN:
  1698. task->tk_action = call_bind;
  1699. break;
  1700. case -EAGAIN:
  1701. task->tk_action = call_transmit;
  1702. break;
  1703. case -EIO:
  1704. /* shutdown or soft timeout */
  1705. rpc_exit(task, status);
  1706. break;
  1707. default:
  1708. if (clnt->cl_chatty)
  1709. printk("%s: RPC call returned error %d\n",
  1710. clnt->cl_program->name, -status);
  1711. rpc_exit(task, status);
  1712. }
  1713. }
  1714. /*
  1715. * 6a. Handle RPC timeout
  1716. * We do not release the request slot, so we keep using the
  1717. * same XID for all retransmits.
  1718. */
  1719. static void
  1720. call_timeout(struct rpc_task *task)
  1721. {
  1722. struct rpc_clnt *clnt = task->tk_client;
  1723. if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
  1724. dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid);
  1725. goto retry;
  1726. }
  1727. dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
  1728. task->tk_timeouts++;
  1729. if (RPC_IS_SOFTCONN(task)) {
  1730. rpc_exit(task, -ETIMEDOUT);
  1731. return;
  1732. }
  1733. if (RPC_IS_SOFT(task)) {
  1734. if (clnt->cl_chatty) {
  1735. rcu_read_lock();
  1736. printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
  1737. clnt->cl_program->name,
  1738. rcu_dereference(clnt->cl_xprt)->servername);
  1739. rcu_read_unlock();
  1740. }
  1741. if (task->tk_flags & RPC_TASK_TIMEOUT)
  1742. rpc_exit(task, -ETIMEDOUT);
  1743. else
  1744. rpc_exit(task, -EIO);
  1745. return;
  1746. }
  1747. if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
  1748. task->tk_flags |= RPC_CALL_MAJORSEEN;
  1749. if (clnt->cl_chatty) {
  1750. rcu_read_lock();
  1751. printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
  1752. clnt->cl_program->name,
  1753. rcu_dereference(clnt->cl_xprt)->servername);
  1754. rcu_read_unlock();
  1755. }
  1756. }
  1757. rpc_force_rebind(clnt);
  1758. /*
  1759. * Did our request time out due to an RPCSEC_GSS out-of-sequence
  1760. * event? RFC2203 requires the server to drop all such requests.
  1761. */
  1762. rpcauth_invalcred(task);
  1763. retry:
  1764. clnt->cl_stats->rpcretrans++;
  1765. task->tk_action = call_bind;
  1766. task->tk_status = 0;
  1767. }
  1768. /*
  1769. * 7. Decode the RPC reply
  1770. */
  1771. static void
  1772. call_decode(struct rpc_task *task)
  1773. {
  1774. struct rpc_clnt *clnt = task->tk_client;
  1775. struct rpc_rqst *req = task->tk_rqstp;
  1776. kxdrdproc_t decode = task->tk_msg.rpc_proc->p_decode;
  1777. __be32 *p;
  1778. dprint_status(task);
  1779. if (task->tk_flags & RPC_CALL_MAJORSEEN) {
  1780. if (clnt->cl_chatty) {
  1781. rcu_read_lock();
  1782. printk(KERN_NOTICE "%s: server %s OK\n",
  1783. clnt->cl_program->name,
  1784. rcu_dereference(clnt->cl_xprt)->servername);
  1785. rcu_read_unlock();
  1786. }
  1787. task->tk_flags &= ~RPC_CALL_MAJORSEEN;
  1788. }
  1789. /*
  1790. * Ensure that we see all writes made by xprt_complete_rqst()
  1791. * before it changed req->rq_reply_bytes_recvd.
  1792. */
  1793. smp_rmb();
  1794. req->rq_rcv_buf.len = req->rq_private_buf.len;
  1795. /* Check that the softirq receive buffer is valid */
  1796. WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
  1797. sizeof(req->rq_rcv_buf)) != 0);
  1798. if (req->rq_rcv_buf.len < 12) {
  1799. if (!RPC_IS_SOFT(task)) {
  1800. task->tk_action = call_bind;
  1801. clnt->cl_stats->rpcretrans++;
  1802. goto out_retry;
  1803. }
  1804. dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
  1805. clnt->cl_program->name, task->tk_status);
  1806. task->tk_action = call_timeout;
  1807. goto out_retry;
  1808. }
  1809. p = rpc_verify_header(task);
  1810. if (IS_ERR(p)) {
  1811. if (p == ERR_PTR(-EAGAIN))
  1812. goto out_retry;
  1813. return;
  1814. }
  1815. task->tk_action = rpc_exit_task;
  1816. if (decode) {
  1817. task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
  1818. task->tk_msg.rpc_resp);
  1819. }
  1820. dprintk("RPC: %5u call_decode result %d\n", task->tk_pid,
  1821. task->tk_status);
  1822. return;
  1823. out_retry:
  1824. task->tk_status = 0;
  1825. /* Note: rpc_verify_header() may have freed the RPC slot */
  1826. if (task->tk_rqstp == req) {
  1827. req->rq_reply_bytes_recvd = req->rq_rcv_buf.len = 0;
  1828. if (task->tk_client->cl_discrtry)
  1829. xprt_conditional_disconnect(req->rq_xprt,
  1830. req->rq_connect_cookie);
  1831. }
  1832. }
  1833. static __be32 *
  1834. rpc_encode_header(struct rpc_task *task)
  1835. {
  1836. struct rpc_clnt *clnt = task->tk_client;
  1837. struct rpc_rqst *req = task->tk_rqstp;
  1838. __be32 *p = req->rq_svec[0].iov_base;
  1839. /* FIXME: check buffer size? */
  1840. p = xprt_skip_transport_header(req->rq_xprt, p);
  1841. *p++ = req->rq_xid; /* XID */
  1842. *p++ = htonl(RPC_CALL); /* CALL */
  1843. *p++ = htonl(RPC_VERSION); /* RPC version */
  1844. *p++ = htonl(clnt->cl_prog); /* program number */
  1845. *p++ = htonl(clnt->cl_vers); /* program version */
  1846. *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
  1847. p = rpcauth_marshcred(task, p);
  1848. req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
  1849. return p;
  1850. }
  1851. static __be32 *
  1852. rpc_verify_header(struct rpc_task *task)
  1853. {
  1854. struct rpc_clnt *clnt = task->tk_client;
  1855. struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
  1856. int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
  1857. __be32 *p = iov->iov_base;
  1858. u32 n;
  1859. int error = -EACCES;
  1860. if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
  1861. /* RFC-1014 says that the representation of XDR data must be a
  1862. * multiple of four bytes
  1863. * - if it isn't pointer subtraction in the NFS client may give
  1864. * undefined results
  1865. */
  1866. dprintk("RPC: %5u %s: XDR representation not a multiple of"
  1867. " 4 bytes: 0x%x\n", task->tk_pid, __func__,
  1868. task->tk_rqstp->rq_rcv_buf.len);
  1869. error = -EIO;
  1870. goto out_err;
  1871. }
  1872. if ((len -= 3) < 0)
  1873. goto out_overflow;
  1874. p += 1; /* skip XID */
  1875. if ((n = ntohl(*p++)) != RPC_REPLY) {
  1876. dprintk("RPC: %5u %s: not an RPC reply: %x\n",
  1877. task->tk_pid, __func__, n);
  1878. error = -EIO;
  1879. goto out_garbage;
  1880. }
  1881. if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
  1882. if (--len < 0)
  1883. goto out_overflow;
  1884. switch ((n = ntohl(*p++))) {
  1885. case RPC_AUTH_ERROR:
  1886. break;
  1887. case RPC_MISMATCH:
  1888. dprintk("RPC: %5u %s: RPC call version mismatch!\n",
  1889. task->tk_pid, __func__);
  1890. error = -EPROTONOSUPPORT;
  1891. goto out_err;
  1892. default:
  1893. dprintk("RPC: %5u %s: RPC call rejected, "
  1894. "unknown error: %x\n",
  1895. task->tk_pid, __func__, n);
  1896. error = -EIO;
  1897. goto out_err;
  1898. }
  1899. if (--len < 0)
  1900. goto out_overflow;
  1901. switch ((n = ntohl(*p++))) {
  1902. case RPC_AUTH_REJECTEDCRED:
  1903. case RPC_AUTH_REJECTEDVERF:
  1904. case RPCSEC_GSS_CREDPROBLEM:
  1905. case RPCSEC_GSS_CTXPROBLEM:
  1906. if (!task->tk_cred_retry)
  1907. break;
  1908. task->tk_cred_retry--;
  1909. dprintk("RPC: %5u %s: retry stale creds\n",
  1910. task->tk_pid, __func__);
  1911. rpcauth_invalcred(task);
  1912. /* Ensure we obtain a new XID! */
  1913. xprt_release(task);
  1914. task->tk_action = call_reserve;
  1915. goto out_retry;
  1916. case RPC_AUTH_BADCRED:
  1917. case RPC_AUTH_BADVERF:
  1918. /* possibly garbled cred/verf? */
  1919. if (!task->tk_garb_retry)
  1920. break;
  1921. task->tk_garb_retry--;
  1922. dprintk("RPC: %5u %s: retry garbled creds\n",
  1923. task->tk_pid, __func__);
  1924. task->tk_action = call_bind;
  1925. goto out_retry;
  1926. case RPC_AUTH_TOOWEAK:
  1927. rcu_read_lock();
  1928. printk(KERN_NOTICE "RPC: server %s requires stronger "
  1929. "authentication.\n",
  1930. rcu_dereference(clnt->cl_xprt)->servername);
  1931. rcu_read_unlock();
  1932. break;
  1933. default:
  1934. dprintk("RPC: %5u %s: unknown auth error: %x\n",
  1935. task->tk_pid, __func__, n);
  1936. error = -EIO;
  1937. }
  1938. dprintk("RPC: %5u %s: call rejected %d\n",
  1939. task->tk_pid, __func__, n);
  1940. goto out_err;
  1941. }
  1942. p = rpcauth_checkverf(task, p);
  1943. if (IS_ERR(p)) {
  1944. error = PTR_ERR(p);
  1945. dprintk("RPC: %5u %s: auth check failed with %d\n",
  1946. task->tk_pid, __func__, error);
  1947. goto out_garbage; /* bad verifier, retry */
  1948. }
  1949. len = p - (__be32 *)iov->iov_base - 1;
  1950. if (len < 0)
  1951. goto out_overflow;
  1952. switch ((n = ntohl(*p++))) {
  1953. case RPC_SUCCESS:
  1954. return p;
  1955. case RPC_PROG_UNAVAIL:
  1956. dprintk_rcu("RPC: %5u %s: program %u is unsupported "
  1957. "by server %s\n", task->tk_pid, __func__,
  1958. (unsigned int)clnt->cl_prog,
  1959. rcu_dereference(clnt->cl_xprt)->servername);
  1960. error = -EPFNOSUPPORT;
  1961. goto out_err;
  1962. case RPC_PROG_MISMATCH:
  1963. dprintk_rcu("RPC: %5u %s: program %u, version %u unsupported "
  1964. "by server %s\n", task->tk_pid, __func__,
  1965. (unsigned int)clnt->cl_prog,
  1966. (unsigned int)clnt->cl_vers,
  1967. rcu_dereference(clnt->cl_xprt)->servername);
  1968. error = -EPROTONOSUPPORT;
  1969. goto out_err;
  1970. case RPC_PROC_UNAVAIL:
  1971. dprintk_rcu("RPC: %5u %s: proc %s unsupported by program %u, "
  1972. "version %u on server %s\n",
  1973. task->tk_pid, __func__,
  1974. rpc_proc_name(task),
  1975. clnt->cl_prog, clnt->cl_vers,
  1976. rcu_dereference(clnt->cl_xprt)->servername);
  1977. error = -EOPNOTSUPP;
  1978. goto out_err;
  1979. case RPC_GARBAGE_ARGS:
  1980. dprintk("RPC: %5u %s: server saw garbage\n",
  1981. task->tk_pid, __func__);
  1982. break; /* retry */
  1983. default:
  1984. dprintk("RPC: %5u %s: server accept status: %x\n",
  1985. task->tk_pid, __func__, n);
  1986. /* Also retry */
  1987. }
  1988. out_garbage:
  1989. clnt->cl_stats->rpcgarbage++;
  1990. if (task->tk_garb_retry) {
  1991. task->tk_garb_retry--;
  1992. dprintk("RPC: %5u %s: retrying\n",
  1993. task->tk_pid, __func__);
  1994. task->tk_action = call_bind;
  1995. out_retry:
  1996. return ERR_PTR(-EAGAIN);
  1997. }
  1998. out_err:
  1999. rpc_exit(task, error);
  2000. dprintk("RPC: %5u %s: call failed with error %d\n", task->tk_pid,
  2001. __func__, error);
  2002. return ERR_PTR(error);
  2003. out_overflow:
  2004. dprintk("RPC: %5u %s: server reply was truncated.\n", task->tk_pid,
  2005. __func__);
  2006. goto out_garbage;
  2007. }
  2008. static void rpcproc_encode_null(void *rqstp, struct xdr_stream *xdr, void *obj)
  2009. {
  2010. }
  2011. static int rpcproc_decode_null(void *rqstp, struct xdr_stream *xdr, void *obj)
  2012. {
  2013. return 0;
  2014. }
  2015. static struct rpc_procinfo rpcproc_null = {
  2016. .p_encode = rpcproc_encode_null,
  2017. .p_decode = rpcproc_decode_null,
  2018. };
  2019. static int rpc_ping(struct rpc_clnt *clnt)
  2020. {
  2021. struct rpc_message msg = {
  2022. .rpc_proc = &rpcproc_null,
  2023. };
  2024. int err;
  2025. msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
  2026. err = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN);
  2027. put_rpccred(msg.rpc_cred);
  2028. return err;
  2029. }
  2030. struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
  2031. {
  2032. struct rpc_message msg = {
  2033. .rpc_proc = &rpcproc_null,
  2034. .rpc_cred = cred,
  2035. };
  2036. struct rpc_task_setup task_setup_data = {
  2037. .rpc_client = clnt,
  2038. .rpc_message = &msg,
  2039. .callback_ops = &rpc_default_ops,
  2040. .flags = flags,
  2041. };
  2042. return rpc_run_task(&task_setup_data);
  2043. }
  2044. EXPORT_SYMBOL_GPL(rpc_call_null);
  2045. #ifdef RPC_DEBUG
  2046. static void rpc_show_header(void)
  2047. {
  2048. printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
  2049. "-timeout ---ops--\n");
  2050. }
  2051. static void rpc_show_task(const struct rpc_clnt *clnt,
  2052. const struct rpc_task *task)
  2053. {
  2054. const char *rpc_waitq = "none";
  2055. if (RPC_IS_QUEUED(task))
  2056. rpc_waitq = rpc_qname(task->tk_waitqueue);
  2057. printk(KERN_INFO "%5u %04x %6d %8p %8p %8ld %8p %sv%u %s a:%ps q:%s\n",
  2058. task->tk_pid, task->tk_flags, task->tk_status,
  2059. clnt, task->tk_rqstp, task->tk_timeout, task->tk_ops,
  2060. clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task),
  2061. task->tk_action, rpc_waitq);
  2062. }
  2063. void rpc_show_tasks(struct net *net)
  2064. {
  2065. struct rpc_clnt *clnt;
  2066. struct rpc_task *task;
  2067. int header = 0;
  2068. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  2069. spin_lock(&sn->rpc_client_lock);
  2070. list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
  2071. spin_lock(&clnt->cl_lock);
  2072. list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
  2073. if (!header) {
  2074. rpc_show_header();
  2075. header++;
  2076. }
  2077. rpc_show_task(clnt, task);
  2078. }
  2079. spin_unlock(&clnt->cl_lock);
  2080. }
  2081. spin_unlock(&sn->rpc_client_lock);
  2082. }
  2083. #endif