clnt.c 60 KB

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