clnt.c 81 KB

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