clnt.c 69 KB

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