clnt.c 66 KB

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