clnt.c 55 KB

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