clnt.c 58 KB

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