clnt.c 68 KB

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