clnt.c 80 KB

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