clnt.c 69 KB

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