clnt.c 79 KB

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