clnt.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539
  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. break;
  1072. default:
  1073. dprintk("RPC: %s: address family not supported\n",
  1074. __func__);
  1075. return -EAFNOSUPPORT;
  1076. }
  1077. dprintk("RPC: %s: succeeded\n", __func__);
  1078. return 0;
  1079. }
  1080. /**
  1081. * rpc_localaddr - discover local endpoint address for an RPC client
  1082. * @clnt: RPC client structure
  1083. * @buf: target buffer
  1084. * @buflen: size of target buffer, in bytes
  1085. *
  1086. * Returns zero and fills in "buf" and "buflen" if successful;
  1087. * otherwise, a negative errno is returned.
  1088. *
  1089. * This works even if the underlying transport is not currently connected,
  1090. * or if the upper layer never previously provided a source address.
  1091. *
  1092. * The result of this function call is transient: multiple calls in
  1093. * succession may give different results, depending on how local
  1094. * networking configuration changes over time.
  1095. */
  1096. int rpc_localaddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t buflen)
  1097. {
  1098. struct sockaddr_storage address;
  1099. struct sockaddr *sap = (struct sockaddr *)&address;
  1100. struct rpc_xprt *xprt;
  1101. struct net *net;
  1102. size_t salen;
  1103. int err;
  1104. rcu_read_lock();
  1105. xprt = rcu_dereference(clnt->cl_xprt);
  1106. salen = xprt->addrlen;
  1107. memcpy(sap, &xprt->addr, salen);
  1108. net = get_net(xprt->xprt_net);
  1109. rcu_read_unlock();
  1110. rpc_set_port(sap, 0);
  1111. err = rpc_sockname(net, sap, salen, buf, buflen);
  1112. put_net(net);
  1113. if (err != 0)
  1114. /* Couldn't discover local address, return ANYADDR */
  1115. return rpc_anyaddr(sap->sa_family, buf, buflen);
  1116. return 0;
  1117. }
  1118. EXPORT_SYMBOL_GPL(rpc_localaddr);
  1119. void
  1120. rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
  1121. {
  1122. struct rpc_xprt *xprt;
  1123. rcu_read_lock();
  1124. xprt = rcu_dereference(clnt->cl_xprt);
  1125. if (xprt->ops->set_buffer_size)
  1126. xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
  1127. rcu_read_unlock();
  1128. }
  1129. EXPORT_SYMBOL_GPL(rpc_setbufsize);
  1130. /**
  1131. * rpc_protocol - Get transport protocol number for an RPC client
  1132. * @clnt: RPC client to query
  1133. *
  1134. */
  1135. int rpc_protocol(struct rpc_clnt *clnt)
  1136. {
  1137. int protocol;
  1138. rcu_read_lock();
  1139. protocol = rcu_dereference(clnt->cl_xprt)->prot;
  1140. rcu_read_unlock();
  1141. return protocol;
  1142. }
  1143. EXPORT_SYMBOL_GPL(rpc_protocol);
  1144. /**
  1145. * rpc_net_ns - Get the network namespace for this RPC client
  1146. * @clnt: RPC client to query
  1147. *
  1148. */
  1149. struct net *rpc_net_ns(struct rpc_clnt *clnt)
  1150. {
  1151. struct net *ret;
  1152. rcu_read_lock();
  1153. ret = rcu_dereference(clnt->cl_xprt)->xprt_net;
  1154. rcu_read_unlock();
  1155. return ret;
  1156. }
  1157. EXPORT_SYMBOL_GPL(rpc_net_ns);
  1158. /**
  1159. * rpc_max_payload - Get maximum payload size for a transport, in bytes
  1160. * @clnt: RPC client to query
  1161. *
  1162. * For stream transports, this is one RPC record fragment (see RFC
  1163. * 1831), as we don't support multi-record requests yet. For datagram
  1164. * transports, this is the size of an IP packet minus the IP, UDP, and
  1165. * RPC header sizes.
  1166. */
  1167. size_t rpc_max_payload(struct rpc_clnt *clnt)
  1168. {
  1169. size_t ret;
  1170. rcu_read_lock();
  1171. ret = rcu_dereference(clnt->cl_xprt)->max_payload;
  1172. rcu_read_unlock();
  1173. return ret;
  1174. }
  1175. EXPORT_SYMBOL_GPL(rpc_max_payload);
  1176. /**
  1177. * rpc_get_timeout - Get timeout for transport in units of HZ
  1178. * @clnt: RPC client to query
  1179. */
  1180. unsigned long rpc_get_timeout(struct rpc_clnt *clnt)
  1181. {
  1182. unsigned long ret;
  1183. rcu_read_lock();
  1184. ret = rcu_dereference(clnt->cl_xprt)->timeout->to_initval;
  1185. rcu_read_unlock();
  1186. return ret;
  1187. }
  1188. EXPORT_SYMBOL_GPL(rpc_get_timeout);
  1189. /**
  1190. * rpc_force_rebind - force transport to check that remote port is unchanged
  1191. * @clnt: client to rebind
  1192. *
  1193. */
  1194. void rpc_force_rebind(struct rpc_clnt *clnt)
  1195. {
  1196. if (clnt->cl_autobind) {
  1197. rcu_read_lock();
  1198. xprt_clear_bound(rcu_dereference(clnt->cl_xprt));
  1199. rcu_read_unlock();
  1200. }
  1201. }
  1202. EXPORT_SYMBOL_GPL(rpc_force_rebind);
  1203. /*
  1204. * Restart an (async) RPC call from the call_prepare state.
  1205. * Usually called from within the exit handler.
  1206. */
  1207. int
  1208. rpc_restart_call_prepare(struct rpc_task *task)
  1209. {
  1210. if (RPC_ASSASSINATED(task))
  1211. return 0;
  1212. task->tk_action = call_start;
  1213. task->tk_status = 0;
  1214. if (task->tk_ops->rpc_call_prepare != NULL)
  1215. task->tk_action = rpc_prepare_task;
  1216. return 1;
  1217. }
  1218. EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
  1219. /*
  1220. * Restart an (async) RPC call. Usually called from within the
  1221. * exit handler.
  1222. */
  1223. int
  1224. rpc_restart_call(struct rpc_task *task)
  1225. {
  1226. if (RPC_ASSASSINATED(task))
  1227. return 0;
  1228. task->tk_action = call_start;
  1229. task->tk_status = 0;
  1230. return 1;
  1231. }
  1232. EXPORT_SYMBOL_GPL(rpc_restart_call);
  1233. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  1234. const char
  1235. *rpc_proc_name(const struct rpc_task *task)
  1236. {
  1237. const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
  1238. if (proc) {
  1239. if (proc->p_name)
  1240. return proc->p_name;
  1241. else
  1242. return "NULL";
  1243. } else
  1244. return "no proc";
  1245. }
  1246. #endif
  1247. /*
  1248. * 0. Initial state
  1249. *
  1250. * Other FSM states can be visited zero or more times, but
  1251. * this state is visited exactly once for each RPC.
  1252. */
  1253. static void
  1254. call_start(struct rpc_task *task)
  1255. {
  1256. struct rpc_clnt *clnt = task->tk_client;
  1257. dprintk("RPC: %5u call_start %s%d proc %s (%s)\n", task->tk_pid,
  1258. clnt->cl_program->name, clnt->cl_vers,
  1259. rpc_proc_name(task),
  1260. (RPC_IS_ASYNC(task) ? "async" : "sync"));
  1261. /* Increment call count */
  1262. task->tk_msg.rpc_proc->p_count++;
  1263. clnt->cl_stats->rpccnt++;
  1264. task->tk_action = call_reserve;
  1265. }
  1266. /*
  1267. * 1. Reserve an RPC call slot
  1268. */
  1269. static void
  1270. call_reserve(struct rpc_task *task)
  1271. {
  1272. dprint_status(task);
  1273. task->tk_status = 0;
  1274. task->tk_action = call_reserveresult;
  1275. xprt_reserve(task);
  1276. }
  1277. static void call_retry_reserve(struct rpc_task *task);
  1278. /*
  1279. * 1b. Grok the result of xprt_reserve()
  1280. */
  1281. static void
  1282. call_reserveresult(struct rpc_task *task)
  1283. {
  1284. int status = task->tk_status;
  1285. dprint_status(task);
  1286. /*
  1287. * After a call to xprt_reserve(), we must have either
  1288. * a request slot or else an error status.
  1289. */
  1290. task->tk_status = 0;
  1291. if (status >= 0) {
  1292. if (task->tk_rqstp) {
  1293. task->tk_action = call_refresh;
  1294. return;
  1295. }
  1296. printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
  1297. __func__, status);
  1298. rpc_exit(task, -EIO);
  1299. return;
  1300. }
  1301. /*
  1302. * Even though there was an error, we may have acquired
  1303. * a request slot somehow. Make sure not to leak it.
  1304. */
  1305. if (task->tk_rqstp) {
  1306. printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
  1307. __func__, status);
  1308. xprt_release(task);
  1309. }
  1310. switch (status) {
  1311. case -ENOMEM:
  1312. rpc_delay(task, HZ >> 2);
  1313. case -EAGAIN: /* woken up; retry */
  1314. task->tk_action = call_retry_reserve;
  1315. return;
  1316. case -EIO: /* probably a shutdown */
  1317. break;
  1318. default:
  1319. printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
  1320. __func__, status);
  1321. break;
  1322. }
  1323. rpc_exit(task, status);
  1324. }
  1325. /*
  1326. * 1c. Retry reserving an RPC call slot
  1327. */
  1328. static void
  1329. call_retry_reserve(struct rpc_task *task)
  1330. {
  1331. dprint_status(task);
  1332. task->tk_status = 0;
  1333. task->tk_action = call_reserveresult;
  1334. xprt_retry_reserve(task);
  1335. }
  1336. /*
  1337. * 2. Bind and/or refresh the credentials
  1338. */
  1339. static void
  1340. call_refresh(struct rpc_task *task)
  1341. {
  1342. dprint_status(task);
  1343. task->tk_action = call_refreshresult;
  1344. task->tk_status = 0;
  1345. task->tk_client->cl_stats->rpcauthrefresh++;
  1346. rpcauth_refreshcred(task);
  1347. }
  1348. /*
  1349. * 2a. Process the results of a credential refresh
  1350. */
  1351. static void
  1352. call_refreshresult(struct rpc_task *task)
  1353. {
  1354. int status = task->tk_status;
  1355. dprint_status(task);
  1356. task->tk_status = 0;
  1357. task->tk_action = call_refresh;
  1358. switch (status) {
  1359. case 0:
  1360. if (rpcauth_uptodatecred(task)) {
  1361. task->tk_action = call_allocate;
  1362. return;
  1363. }
  1364. /* Use rate-limiting and a max number of retries if refresh
  1365. * had status 0 but failed to update the cred.
  1366. */
  1367. case -ETIMEDOUT:
  1368. rpc_delay(task, 3*HZ);
  1369. case -EAGAIN:
  1370. status = -EACCES;
  1371. case -EKEYEXPIRED:
  1372. if (!task->tk_cred_retry)
  1373. break;
  1374. task->tk_cred_retry--;
  1375. dprintk("RPC: %5u %s: retry refresh creds\n",
  1376. task->tk_pid, __func__);
  1377. return;
  1378. }
  1379. dprintk("RPC: %5u %s: refresh creds failed with error %d\n",
  1380. task->tk_pid, __func__, status);
  1381. rpc_exit(task, status);
  1382. }
  1383. /*
  1384. * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc.
  1385. * (Note: buffer memory is freed in xprt_release).
  1386. */
  1387. static void
  1388. call_allocate(struct rpc_task *task)
  1389. {
  1390. unsigned int slack = task->tk_rqstp->rq_cred->cr_auth->au_cslack;
  1391. struct rpc_rqst *req = task->tk_rqstp;
  1392. struct rpc_xprt *xprt = req->rq_xprt;
  1393. struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
  1394. dprint_status(task);
  1395. task->tk_status = 0;
  1396. task->tk_action = call_bind;
  1397. if (req->rq_buffer)
  1398. return;
  1399. if (proc->p_proc != 0) {
  1400. BUG_ON(proc->p_arglen == 0);
  1401. if (proc->p_decode != NULL)
  1402. BUG_ON(proc->p_replen == 0);
  1403. }
  1404. /*
  1405. * Calculate the size (in quads) of the RPC call
  1406. * and reply headers, and convert both values
  1407. * to byte sizes.
  1408. */
  1409. req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen;
  1410. req->rq_callsize <<= 2;
  1411. req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen;
  1412. req->rq_rcvsize <<= 2;
  1413. req->rq_buffer = xprt->ops->buf_alloc(task,
  1414. req->rq_callsize + req->rq_rcvsize);
  1415. if (req->rq_buffer != NULL)
  1416. return;
  1417. xprt_inject_disconnect(xprt);
  1418. dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
  1419. if (RPC_IS_ASYNC(task) || !fatal_signal_pending(current)) {
  1420. task->tk_action = call_allocate;
  1421. rpc_delay(task, HZ>>4);
  1422. return;
  1423. }
  1424. rpc_exit(task, -ERESTARTSYS);
  1425. }
  1426. static inline int
  1427. rpc_task_need_encode(struct rpc_task *task)
  1428. {
  1429. return task->tk_rqstp->rq_snd_buf.len == 0;
  1430. }
  1431. static inline void
  1432. rpc_task_force_reencode(struct rpc_task *task)
  1433. {
  1434. task->tk_rqstp->rq_snd_buf.len = 0;
  1435. task->tk_rqstp->rq_bytes_sent = 0;
  1436. }
  1437. static inline void
  1438. rpc_xdr_buf_init(struct xdr_buf *buf, void *start, size_t len)
  1439. {
  1440. buf->head[0].iov_base = start;
  1441. buf->head[0].iov_len = len;
  1442. buf->tail[0].iov_len = 0;
  1443. buf->page_len = 0;
  1444. buf->flags = 0;
  1445. buf->len = 0;
  1446. buf->buflen = len;
  1447. }
  1448. /*
  1449. * 3. Encode arguments of an RPC call
  1450. */
  1451. static void
  1452. rpc_xdr_encode(struct rpc_task *task)
  1453. {
  1454. struct rpc_rqst *req = task->tk_rqstp;
  1455. kxdreproc_t encode;
  1456. __be32 *p;
  1457. dprint_status(task);
  1458. rpc_xdr_buf_init(&req->rq_snd_buf,
  1459. req->rq_buffer,
  1460. req->rq_callsize);
  1461. rpc_xdr_buf_init(&req->rq_rcv_buf,
  1462. (char *)req->rq_buffer + req->rq_callsize,
  1463. req->rq_rcvsize);
  1464. p = rpc_encode_header(task);
  1465. if (p == NULL) {
  1466. printk(KERN_INFO "RPC: couldn't encode RPC header, exit EIO\n");
  1467. rpc_exit(task, -EIO);
  1468. return;
  1469. }
  1470. encode = task->tk_msg.rpc_proc->p_encode;
  1471. if (encode == NULL)
  1472. return;
  1473. task->tk_status = rpcauth_wrap_req(task, encode, req, p,
  1474. task->tk_msg.rpc_argp);
  1475. }
  1476. /*
  1477. * 4. Get the server port number if not yet set
  1478. */
  1479. static void
  1480. call_bind(struct rpc_task *task)
  1481. {
  1482. struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
  1483. dprint_status(task);
  1484. task->tk_action = call_connect;
  1485. if (!xprt_bound(xprt)) {
  1486. task->tk_action = call_bind_status;
  1487. task->tk_timeout = xprt->bind_timeout;
  1488. xprt->ops->rpcbind(task);
  1489. }
  1490. }
  1491. /*
  1492. * 4a. Sort out bind result
  1493. */
  1494. static void
  1495. call_bind_status(struct rpc_task *task)
  1496. {
  1497. int status = -EIO;
  1498. if (task->tk_status >= 0) {
  1499. dprint_status(task);
  1500. task->tk_status = 0;
  1501. task->tk_action = call_connect;
  1502. return;
  1503. }
  1504. trace_rpc_bind_status(task);
  1505. switch (task->tk_status) {
  1506. case -ENOMEM:
  1507. dprintk("RPC: %5u rpcbind out of memory\n", task->tk_pid);
  1508. rpc_delay(task, HZ >> 2);
  1509. goto retry_timeout;
  1510. case -EACCES:
  1511. dprintk("RPC: %5u remote rpcbind: RPC program/version "
  1512. "unavailable\n", task->tk_pid);
  1513. /* fail immediately if this is an RPC ping */
  1514. if (task->tk_msg.rpc_proc->p_proc == 0) {
  1515. status = -EOPNOTSUPP;
  1516. break;
  1517. }
  1518. if (task->tk_rebind_retry == 0)
  1519. break;
  1520. task->tk_rebind_retry--;
  1521. rpc_delay(task, 3*HZ);
  1522. goto retry_timeout;
  1523. case -ETIMEDOUT:
  1524. dprintk("RPC: %5u rpcbind request timed out\n",
  1525. task->tk_pid);
  1526. goto retry_timeout;
  1527. case -EPFNOSUPPORT:
  1528. /* server doesn't support any rpcbind version we know of */
  1529. dprintk("RPC: %5u unrecognized remote rpcbind service\n",
  1530. task->tk_pid);
  1531. break;
  1532. case -EPROTONOSUPPORT:
  1533. dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
  1534. task->tk_pid);
  1535. goto retry_timeout;
  1536. case -ECONNREFUSED: /* connection problems */
  1537. case -ECONNRESET:
  1538. case -ECONNABORTED:
  1539. case -ENOTCONN:
  1540. case -EHOSTDOWN:
  1541. case -EHOSTUNREACH:
  1542. case -ENETUNREACH:
  1543. case -ENOBUFS:
  1544. case -EPIPE:
  1545. dprintk("RPC: %5u remote rpcbind unreachable: %d\n",
  1546. task->tk_pid, task->tk_status);
  1547. if (!RPC_IS_SOFTCONN(task)) {
  1548. rpc_delay(task, 5*HZ);
  1549. goto retry_timeout;
  1550. }
  1551. status = task->tk_status;
  1552. break;
  1553. default:
  1554. dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
  1555. task->tk_pid, -task->tk_status);
  1556. }
  1557. rpc_exit(task, status);
  1558. return;
  1559. retry_timeout:
  1560. task->tk_status = 0;
  1561. task->tk_action = call_timeout;
  1562. }
  1563. /*
  1564. * 4b. Connect to the RPC server
  1565. */
  1566. static void
  1567. call_connect(struct rpc_task *task)
  1568. {
  1569. struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
  1570. dprintk("RPC: %5u call_connect xprt %p %s connected\n",
  1571. task->tk_pid, xprt,
  1572. (xprt_connected(xprt) ? "is" : "is not"));
  1573. task->tk_action = call_transmit;
  1574. if (!xprt_connected(xprt)) {
  1575. task->tk_action = call_connect_status;
  1576. if (task->tk_status < 0)
  1577. return;
  1578. if (task->tk_flags & RPC_TASK_NOCONNECT) {
  1579. rpc_exit(task, -ENOTCONN);
  1580. return;
  1581. }
  1582. xprt_connect(task);
  1583. }
  1584. }
  1585. /*
  1586. * 4c. Sort out connect result
  1587. */
  1588. static void
  1589. call_connect_status(struct rpc_task *task)
  1590. {
  1591. struct rpc_clnt *clnt = task->tk_client;
  1592. int status = task->tk_status;
  1593. dprint_status(task);
  1594. trace_rpc_connect_status(task, status);
  1595. task->tk_status = 0;
  1596. switch (status) {
  1597. case -ECONNREFUSED:
  1598. case -ECONNRESET:
  1599. case -ECONNABORTED:
  1600. case -ENETUNREACH:
  1601. case -EHOSTUNREACH:
  1602. case -EADDRINUSE:
  1603. case -ENOBUFS:
  1604. case -EPIPE:
  1605. if (RPC_IS_SOFTCONN(task))
  1606. break;
  1607. /* retry with existing socket, after a delay */
  1608. rpc_delay(task, 3*HZ);
  1609. case -EAGAIN:
  1610. /* Check for timeouts before looping back to call_bind */
  1611. case -ETIMEDOUT:
  1612. task->tk_action = call_timeout;
  1613. return;
  1614. case 0:
  1615. clnt->cl_stats->netreconn++;
  1616. task->tk_action = call_transmit;
  1617. return;
  1618. }
  1619. rpc_exit(task, status);
  1620. }
  1621. /*
  1622. * 5. Transmit the RPC request, and wait for reply
  1623. */
  1624. static void
  1625. call_transmit(struct rpc_task *task)
  1626. {
  1627. int is_retrans = RPC_WAS_SENT(task);
  1628. dprint_status(task);
  1629. task->tk_action = call_status;
  1630. if (task->tk_status < 0)
  1631. return;
  1632. if (!xprt_prepare_transmit(task))
  1633. return;
  1634. task->tk_action = call_transmit_status;
  1635. /* Encode here so that rpcsec_gss can use correct sequence number. */
  1636. if (rpc_task_need_encode(task)) {
  1637. rpc_xdr_encode(task);
  1638. /* Did the encode result in an error condition? */
  1639. if (task->tk_status != 0) {
  1640. /* Was the error nonfatal? */
  1641. if (task->tk_status == -EAGAIN)
  1642. rpc_delay(task, HZ >> 4);
  1643. else
  1644. rpc_exit(task, task->tk_status);
  1645. return;
  1646. }
  1647. }
  1648. xprt_transmit(task);
  1649. if (task->tk_status < 0)
  1650. return;
  1651. if (is_retrans)
  1652. task->tk_client->cl_stats->rpcretrans++;
  1653. /*
  1654. * On success, ensure that we call xprt_end_transmit() before sleeping
  1655. * in order to allow access to the socket to other RPC requests.
  1656. */
  1657. call_transmit_status(task);
  1658. if (rpc_reply_expected(task))
  1659. return;
  1660. task->tk_action = rpc_exit_task;
  1661. rpc_wake_up_queued_task(&task->tk_rqstp->rq_xprt->pending, task);
  1662. }
  1663. /*
  1664. * 5a. Handle cleanup after a transmission
  1665. */
  1666. static void
  1667. call_transmit_status(struct rpc_task *task)
  1668. {
  1669. task->tk_action = call_status;
  1670. /*
  1671. * Common case: success. Force the compiler to put this
  1672. * test first.
  1673. */
  1674. if (task->tk_status == 0) {
  1675. xprt_end_transmit(task);
  1676. rpc_task_force_reencode(task);
  1677. return;
  1678. }
  1679. switch (task->tk_status) {
  1680. case -EAGAIN:
  1681. case -ENOBUFS:
  1682. break;
  1683. default:
  1684. dprint_status(task);
  1685. xprt_end_transmit(task);
  1686. rpc_task_force_reencode(task);
  1687. break;
  1688. /*
  1689. * Special cases: if we've been waiting on the
  1690. * socket's write_space() callback, or if the
  1691. * socket just returned a connection error,
  1692. * then hold onto the transport lock.
  1693. */
  1694. case -ECONNREFUSED:
  1695. case -EHOSTDOWN:
  1696. case -EHOSTUNREACH:
  1697. case -ENETUNREACH:
  1698. case -EPERM:
  1699. if (RPC_IS_SOFTCONN(task)) {
  1700. xprt_end_transmit(task);
  1701. rpc_exit(task, task->tk_status);
  1702. break;
  1703. }
  1704. case -ECONNRESET:
  1705. case -ECONNABORTED:
  1706. case -EADDRINUSE:
  1707. case -ENOTCONN:
  1708. case -EPIPE:
  1709. rpc_task_force_reencode(task);
  1710. }
  1711. }
  1712. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  1713. /*
  1714. * 5b. Send the backchannel RPC reply. On error, drop the reply. In
  1715. * addition, disconnect on connectivity errors.
  1716. */
  1717. static void
  1718. call_bc_transmit(struct rpc_task *task)
  1719. {
  1720. struct rpc_rqst *req = task->tk_rqstp;
  1721. if (!xprt_prepare_transmit(task))
  1722. goto out_retry;
  1723. if (task->tk_status < 0) {
  1724. printk(KERN_NOTICE "RPC: Could not send backchannel reply "
  1725. "error: %d\n", task->tk_status);
  1726. goto out_done;
  1727. }
  1728. if (req->rq_connect_cookie != req->rq_xprt->connect_cookie)
  1729. req->rq_bytes_sent = 0;
  1730. xprt_transmit(task);
  1731. if (task->tk_status == -EAGAIN)
  1732. goto out_nospace;
  1733. xprt_end_transmit(task);
  1734. dprint_status(task);
  1735. switch (task->tk_status) {
  1736. case 0:
  1737. /* Success */
  1738. case -EHOSTDOWN:
  1739. case -EHOSTUNREACH:
  1740. case -ENETUNREACH:
  1741. case -ECONNRESET:
  1742. case -ECONNREFUSED:
  1743. case -EADDRINUSE:
  1744. case -ENOTCONN:
  1745. case -EPIPE:
  1746. break;
  1747. case -ETIMEDOUT:
  1748. /*
  1749. * Problem reaching the server. Disconnect and let the
  1750. * forechannel reestablish the connection. The server will
  1751. * have to retransmit the backchannel request and we'll
  1752. * reprocess it. Since these ops are idempotent, there's no
  1753. * need to cache our reply at this time.
  1754. */
  1755. printk(KERN_NOTICE "RPC: Could not send backchannel reply "
  1756. "error: %d\n", task->tk_status);
  1757. xprt_conditional_disconnect(req->rq_xprt,
  1758. req->rq_connect_cookie);
  1759. break;
  1760. default:
  1761. /*
  1762. * We were unable to reply and will have to drop the
  1763. * request. The server should reconnect and retransmit.
  1764. */
  1765. WARN_ON_ONCE(task->tk_status == -EAGAIN);
  1766. printk(KERN_NOTICE "RPC: Could not send backchannel reply "
  1767. "error: %d\n", task->tk_status);
  1768. break;
  1769. }
  1770. rpc_wake_up_queued_task(&req->rq_xprt->pending, task);
  1771. out_done:
  1772. task->tk_action = rpc_exit_task;
  1773. return;
  1774. out_nospace:
  1775. req->rq_connect_cookie = req->rq_xprt->connect_cookie;
  1776. out_retry:
  1777. task->tk_status = 0;
  1778. }
  1779. #endif /* CONFIG_SUNRPC_BACKCHANNEL */
  1780. /*
  1781. * 6. Sort out the RPC call status
  1782. */
  1783. static void
  1784. call_status(struct rpc_task *task)
  1785. {
  1786. struct rpc_clnt *clnt = task->tk_client;
  1787. struct rpc_rqst *req = task->tk_rqstp;
  1788. int status;
  1789. if (req->rq_reply_bytes_recvd > 0 && !req->rq_bytes_sent)
  1790. task->tk_status = req->rq_reply_bytes_recvd;
  1791. dprint_status(task);
  1792. status = task->tk_status;
  1793. if (status >= 0) {
  1794. task->tk_action = call_decode;
  1795. return;
  1796. }
  1797. trace_rpc_call_status(task);
  1798. task->tk_status = 0;
  1799. switch(status) {
  1800. case -EHOSTDOWN:
  1801. case -EHOSTUNREACH:
  1802. case -ENETUNREACH:
  1803. case -EPERM:
  1804. if (RPC_IS_SOFTCONN(task)) {
  1805. rpc_exit(task, status);
  1806. break;
  1807. }
  1808. /*
  1809. * Delay any retries for 3 seconds, then handle as if it
  1810. * were a timeout.
  1811. */
  1812. rpc_delay(task, 3*HZ);
  1813. case -ETIMEDOUT:
  1814. task->tk_action = call_timeout;
  1815. if (!(task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT)
  1816. && task->tk_client->cl_discrtry)
  1817. xprt_conditional_disconnect(req->rq_xprt,
  1818. req->rq_connect_cookie);
  1819. break;
  1820. case -ECONNREFUSED:
  1821. case -ECONNRESET:
  1822. case -ECONNABORTED:
  1823. rpc_force_rebind(clnt);
  1824. case -EADDRINUSE:
  1825. rpc_delay(task, 3*HZ);
  1826. case -EPIPE:
  1827. case -ENOTCONN:
  1828. task->tk_action = call_bind;
  1829. break;
  1830. case -ENOBUFS:
  1831. rpc_delay(task, HZ>>2);
  1832. case -EAGAIN:
  1833. task->tk_action = call_transmit;
  1834. break;
  1835. case -EIO:
  1836. /* shutdown or soft timeout */
  1837. rpc_exit(task, status);
  1838. break;
  1839. default:
  1840. if (clnt->cl_chatty)
  1841. printk("%s: RPC call returned error %d\n",
  1842. clnt->cl_program->name, -status);
  1843. rpc_exit(task, status);
  1844. }
  1845. }
  1846. /*
  1847. * 6a. Handle RPC timeout
  1848. * We do not release the request slot, so we keep using the
  1849. * same XID for all retransmits.
  1850. */
  1851. static void
  1852. call_timeout(struct rpc_task *task)
  1853. {
  1854. struct rpc_clnt *clnt = task->tk_client;
  1855. if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
  1856. dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid);
  1857. goto retry;
  1858. }
  1859. dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
  1860. task->tk_timeouts++;
  1861. if (RPC_IS_SOFTCONN(task)) {
  1862. rpc_exit(task, -ETIMEDOUT);
  1863. return;
  1864. }
  1865. if (RPC_IS_SOFT(task)) {
  1866. if (clnt->cl_chatty) {
  1867. rcu_read_lock();
  1868. printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
  1869. clnt->cl_program->name,
  1870. rcu_dereference(clnt->cl_xprt)->servername);
  1871. rcu_read_unlock();
  1872. }
  1873. if (task->tk_flags & RPC_TASK_TIMEOUT)
  1874. rpc_exit(task, -ETIMEDOUT);
  1875. else
  1876. rpc_exit(task, -EIO);
  1877. return;
  1878. }
  1879. if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
  1880. task->tk_flags |= RPC_CALL_MAJORSEEN;
  1881. if (clnt->cl_chatty) {
  1882. rcu_read_lock();
  1883. printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
  1884. clnt->cl_program->name,
  1885. rcu_dereference(clnt->cl_xprt)->servername);
  1886. rcu_read_unlock();
  1887. }
  1888. }
  1889. rpc_force_rebind(clnt);
  1890. /*
  1891. * Did our request time out due to an RPCSEC_GSS out-of-sequence
  1892. * event? RFC2203 requires the server to drop all such requests.
  1893. */
  1894. rpcauth_invalcred(task);
  1895. retry:
  1896. task->tk_action = call_bind;
  1897. task->tk_status = 0;
  1898. }
  1899. /*
  1900. * 7. Decode the RPC reply
  1901. */
  1902. static void
  1903. call_decode(struct rpc_task *task)
  1904. {
  1905. struct rpc_clnt *clnt = task->tk_client;
  1906. struct rpc_rqst *req = task->tk_rqstp;
  1907. kxdrdproc_t decode = task->tk_msg.rpc_proc->p_decode;
  1908. __be32 *p;
  1909. dprint_status(task);
  1910. if (task->tk_flags & RPC_CALL_MAJORSEEN) {
  1911. if (clnt->cl_chatty) {
  1912. rcu_read_lock();
  1913. printk(KERN_NOTICE "%s: server %s OK\n",
  1914. clnt->cl_program->name,
  1915. rcu_dereference(clnt->cl_xprt)->servername);
  1916. rcu_read_unlock();
  1917. }
  1918. task->tk_flags &= ~RPC_CALL_MAJORSEEN;
  1919. }
  1920. /*
  1921. * Ensure that we see all writes made by xprt_complete_rqst()
  1922. * before it changed req->rq_reply_bytes_recvd.
  1923. */
  1924. smp_rmb();
  1925. req->rq_rcv_buf.len = req->rq_private_buf.len;
  1926. /* Check that the softirq receive buffer is valid */
  1927. WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
  1928. sizeof(req->rq_rcv_buf)) != 0);
  1929. if (req->rq_rcv_buf.len < 12) {
  1930. if (!RPC_IS_SOFT(task)) {
  1931. task->tk_action = call_bind;
  1932. goto out_retry;
  1933. }
  1934. dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
  1935. clnt->cl_program->name, task->tk_status);
  1936. task->tk_action = call_timeout;
  1937. goto out_retry;
  1938. }
  1939. p = rpc_verify_header(task);
  1940. if (IS_ERR(p)) {
  1941. if (p == ERR_PTR(-EAGAIN))
  1942. goto out_retry;
  1943. return;
  1944. }
  1945. task->tk_action = rpc_exit_task;
  1946. if (decode) {
  1947. task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
  1948. task->tk_msg.rpc_resp);
  1949. }
  1950. dprintk("RPC: %5u call_decode result %d\n", task->tk_pid,
  1951. task->tk_status);
  1952. return;
  1953. out_retry:
  1954. task->tk_status = 0;
  1955. /* Note: rpc_verify_header() may have freed the RPC slot */
  1956. if (task->tk_rqstp == req) {
  1957. req->rq_reply_bytes_recvd = req->rq_rcv_buf.len = 0;
  1958. if (task->tk_client->cl_discrtry)
  1959. xprt_conditional_disconnect(req->rq_xprt,
  1960. req->rq_connect_cookie);
  1961. }
  1962. }
  1963. static __be32 *
  1964. rpc_encode_header(struct rpc_task *task)
  1965. {
  1966. struct rpc_clnt *clnt = task->tk_client;
  1967. struct rpc_rqst *req = task->tk_rqstp;
  1968. __be32 *p = req->rq_svec[0].iov_base;
  1969. /* FIXME: check buffer size? */
  1970. p = xprt_skip_transport_header(req->rq_xprt, p);
  1971. *p++ = req->rq_xid; /* XID */
  1972. *p++ = htonl(RPC_CALL); /* CALL */
  1973. *p++ = htonl(RPC_VERSION); /* RPC version */
  1974. *p++ = htonl(clnt->cl_prog); /* program number */
  1975. *p++ = htonl(clnt->cl_vers); /* program version */
  1976. *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
  1977. p = rpcauth_marshcred(task, p);
  1978. req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
  1979. return p;
  1980. }
  1981. static __be32 *
  1982. rpc_verify_header(struct rpc_task *task)
  1983. {
  1984. struct rpc_clnt *clnt = task->tk_client;
  1985. struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
  1986. int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
  1987. __be32 *p = iov->iov_base;
  1988. u32 n;
  1989. int error = -EACCES;
  1990. if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
  1991. /* RFC-1014 says that the representation of XDR data must be a
  1992. * multiple of four bytes
  1993. * - if it isn't pointer subtraction in the NFS client may give
  1994. * undefined results
  1995. */
  1996. dprintk("RPC: %5u %s: XDR representation not a multiple of"
  1997. " 4 bytes: 0x%x\n", task->tk_pid, __func__,
  1998. task->tk_rqstp->rq_rcv_buf.len);
  1999. error = -EIO;
  2000. goto out_err;
  2001. }
  2002. if ((len -= 3) < 0)
  2003. goto out_overflow;
  2004. p += 1; /* skip XID */
  2005. if ((n = ntohl(*p++)) != RPC_REPLY) {
  2006. dprintk("RPC: %5u %s: not an RPC reply: %x\n",
  2007. task->tk_pid, __func__, n);
  2008. error = -EIO;
  2009. goto out_garbage;
  2010. }
  2011. if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
  2012. if (--len < 0)
  2013. goto out_overflow;
  2014. switch ((n = ntohl(*p++))) {
  2015. case RPC_AUTH_ERROR:
  2016. break;
  2017. case RPC_MISMATCH:
  2018. dprintk("RPC: %5u %s: RPC call version mismatch!\n",
  2019. task->tk_pid, __func__);
  2020. error = -EPROTONOSUPPORT;
  2021. goto out_err;
  2022. default:
  2023. dprintk("RPC: %5u %s: RPC call rejected, "
  2024. "unknown error: %x\n",
  2025. task->tk_pid, __func__, n);
  2026. error = -EIO;
  2027. goto out_err;
  2028. }
  2029. if (--len < 0)
  2030. goto out_overflow;
  2031. switch ((n = ntohl(*p++))) {
  2032. case RPC_AUTH_REJECTEDCRED:
  2033. case RPC_AUTH_REJECTEDVERF:
  2034. case RPCSEC_GSS_CREDPROBLEM:
  2035. case RPCSEC_GSS_CTXPROBLEM:
  2036. if (!task->tk_cred_retry)
  2037. break;
  2038. task->tk_cred_retry--;
  2039. dprintk("RPC: %5u %s: retry stale creds\n",
  2040. task->tk_pid, __func__);
  2041. rpcauth_invalcred(task);
  2042. /* Ensure we obtain a new XID! */
  2043. xprt_release(task);
  2044. task->tk_action = call_reserve;
  2045. goto out_retry;
  2046. case RPC_AUTH_BADCRED:
  2047. case RPC_AUTH_BADVERF:
  2048. /* possibly garbled cred/verf? */
  2049. if (!task->tk_garb_retry)
  2050. break;
  2051. task->tk_garb_retry--;
  2052. dprintk("RPC: %5u %s: retry garbled creds\n",
  2053. task->tk_pid, __func__);
  2054. task->tk_action = call_bind;
  2055. goto out_retry;
  2056. case RPC_AUTH_TOOWEAK:
  2057. rcu_read_lock();
  2058. printk(KERN_NOTICE "RPC: server %s requires stronger "
  2059. "authentication.\n",
  2060. rcu_dereference(clnt->cl_xprt)->servername);
  2061. rcu_read_unlock();
  2062. break;
  2063. default:
  2064. dprintk("RPC: %5u %s: unknown auth error: %x\n",
  2065. task->tk_pid, __func__, n);
  2066. error = -EIO;
  2067. }
  2068. dprintk("RPC: %5u %s: call rejected %d\n",
  2069. task->tk_pid, __func__, n);
  2070. goto out_err;
  2071. }
  2072. p = rpcauth_checkverf(task, p);
  2073. if (IS_ERR(p)) {
  2074. error = PTR_ERR(p);
  2075. dprintk("RPC: %5u %s: auth check failed with %d\n",
  2076. task->tk_pid, __func__, error);
  2077. goto out_garbage; /* bad verifier, retry */
  2078. }
  2079. len = p - (__be32 *)iov->iov_base - 1;
  2080. if (len < 0)
  2081. goto out_overflow;
  2082. switch ((n = ntohl(*p++))) {
  2083. case RPC_SUCCESS:
  2084. return p;
  2085. case RPC_PROG_UNAVAIL:
  2086. dprintk_rcu("RPC: %5u %s: program %u is unsupported "
  2087. "by server %s\n", task->tk_pid, __func__,
  2088. (unsigned int)clnt->cl_prog,
  2089. rcu_dereference(clnt->cl_xprt)->servername);
  2090. error = -EPFNOSUPPORT;
  2091. goto out_err;
  2092. case RPC_PROG_MISMATCH:
  2093. dprintk_rcu("RPC: %5u %s: program %u, version %u unsupported "
  2094. "by server %s\n", task->tk_pid, __func__,
  2095. (unsigned int)clnt->cl_prog,
  2096. (unsigned int)clnt->cl_vers,
  2097. rcu_dereference(clnt->cl_xprt)->servername);
  2098. error = -EPROTONOSUPPORT;
  2099. goto out_err;
  2100. case RPC_PROC_UNAVAIL:
  2101. dprintk_rcu("RPC: %5u %s: proc %s unsupported by program %u, "
  2102. "version %u on server %s\n",
  2103. task->tk_pid, __func__,
  2104. rpc_proc_name(task),
  2105. clnt->cl_prog, clnt->cl_vers,
  2106. rcu_dereference(clnt->cl_xprt)->servername);
  2107. error = -EOPNOTSUPP;
  2108. goto out_err;
  2109. case RPC_GARBAGE_ARGS:
  2110. dprintk("RPC: %5u %s: server saw garbage\n",
  2111. task->tk_pid, __func__);
  2112. break; /* retry */
  2113. default:
  2114. dprintk("RPC: %5u %s: server accept status: %x\n",
  2115. task->tk_pid, __func__, n);
  2116. /* Also retry */
  2117. }
  2118. out_garbage:
  2119. clnt->cl_stats->rpcgarbage++;
  2120. if (task->tk_garb_retry) {
  2121. task->tk_garb_retry--;
  2122. dprintk("RPC: %5u %s: retrying\n",
  2123. task->tk_pid, __func__);
  2124. task->tk_action = call_bind;
  2125. out_retry:
  2126. return ERR_PTR(-EAGAIN);
  2127. }
  2128. out_err:
  2129. rpc_exit(task, error);
  2130. dprintk("RPC: %5u %s: call failed with error %d\n", task->tk_pid,
  2131. __func__, error);
  2132. return ERR_PTR(error);
  2133. out_overflow:
  2134. dprintk("RPC: %5u %s: server reply was truncated.\n", task->tk_pid,
  2135. __func__);
  2136. goto out_garbage;
  2137. }
  2138. static void rpcproc_encode_null(void *rqstp, struct xdr_stream *xdr, void *obj)
  2139. {
  2140. }
  2141. static int rpcproc_decode_null(void *rqstp, struct xdr_stream *xdr, void *obj)
  2142. {
  2143. return 0;
  2144. }
  2145. static struct rpc_procinfo rpcproc_null = {
  2146. .p_encode = rpcproc_encode_null,
  2147. .p_decode = rpcproc_decode_null,
  2148. };
  2149. static int rpc_ping(struct rpc_clnt *clnt)
  2150. {
  2151. struct rpc_message msg = {
  2152. .rpc_proc = &rpcproc_null,
  2153. };
  2154. int err;
  2155. msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
  2156. err = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN);
  2157. put_rpccred(msg.rpc_cred);
  2158. return err;
  2159. }
  2160. struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
  2161. {
  2162. struct rpc_message msg = {
  2163. .rpc_proc = &rpcproc_null,
  2164. .rpc_cred = cred,
  2165. };
  2166. struct rpc_task_setup task_setup_data = {
  2167. .rpc_client = clnt,
  2168. .rpc_message = &msg,
  2169. .callback_ops = &rpc_default_ops,
  2170. .flags = flags,
  2171. };
  2172. return rpc_run_task(&task_setup_data);
  2173. }
  2174. EXPORT_SYMBOL_GPL(rpc_call_null);
  2175. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  2176. static void rpc_show_header(void)
  2177. {
  2178. printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
  2179. "-timeout ---ops--\n");
  2180. }
  2181. static void rpc_show_task(const struct rpc_clnt *clnt,
  2182. const struct rpc_task *task)
  2183. {
  2184. const char *rpc_waitq = "none";
  2185. if (RPC_IS_QUEUED(task))
  2186. rpc_waitq = rpc_qname(task->tk_waitqueue);
  2187. printk(KERN_INFO "%5u %04x %6d %8p %8p %8ld %8p %sv%u %s a:%ps q:%s\n",
  2188. task->tk_pid, task->tk_flags, task->tk_status,
  2189. clnt, task->tk_rqstp, task->tk_timeout, task->tk_ops,
  2190. clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task),
  2191. task->tk_action, rpc_waitq);
  2192. }
  2193. void rpc_show_tasks(struct net *net)
  2194. {
  2195. struct rpc_clnt *clnt;
  2196. struct rpc_task *task;
  2197. int header = 0;
  2198. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  2199. spin_lock(&sn->rpc_client_lock);
  2200. list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
  2201. spin_lock(&clnt->cl_lock);
  2202. list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
  2203. if (!header) {
  2204. rpc_show_header();
  2205. header++;
  2206. }
  2207. rpc_show_task(clnt, task);
  2208. }
  2209. spin_unlock(&clnt->cl_lock);
  2210. }
  2211. spin_unlock(&sn->rpc_client_lock);
  2212. }
  2213. #endif
  2214. #if IS_ENABLED(CONFIG_SUNRPC_SWAP)
  2215. int
  2216. rpc_clnt_swap_activate(struct rpc_clnt *clnt)
  2217. {
  2218. int ret = 0;
  2219. struct rpc_xprt *xprt;
  2220. if (atomic_inc_return(&clnt->cl_swapper) == 1) {
  2221. retry:
  2222. rcu_read_lock();
  2223. xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
  2224. rcu_read_unlock();
  2225. if (!xprt) {
  2226. /*
  2227. * If we didn't get a reference, then we likely are
  2228. * racing with a migration event. Wait for a grace
  2229. * period and try again.
  2230. */
  2231. synchronize_rcu();
  2232. goto retry;
  2233. }
  2234. ret = xprt_enable_swap(xprt);
  2235. xprt_put(xprt);
  2236. }
  2237. return ret;
  2238. }
  2239. EXPORT_SYMBOL_GPL(rpc_clnt_swap_activate);
  2240. void
  2241. rpc_clnt_swap_deactivate(struct rpc_clnt *clnt)
  2242. {
  2243. struct rpc_xprt *xprt;
  2244. if (atomic_dec_if_positive(&clnt->cl_swapper) == 0) {
  2245. retry:
  2246. rcu_read_lock();
  2247. xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
  2248. rcu_read_unlock();
  2249. if (!xprt) {
  2250. /*
  2251. * If we didn't get a reference, then we likely are
  2252. * racing with a migration event. Wait for a grace
  2253. * period and try again.
  2254. */
  2255. synchronize_rcu();
  2256. goto retry;
  2257. }
  2258. xprt_disable_swap(xprt);
  2259. xprt_put(xprt);
  2260. }
  2261. }
  2262. EXPORT_SYMBOL_GPL(rpc_clnt_swap_deactivate);
  2263. #endif /* CONFIG_SUNRPC_SWAP */