clnt.c 59 KB

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