clnt.c 58 KB

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